smb_conn.c revision 175635
1/*-
2 * Copyright (c) 2000-2001 Boris Popov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *    This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/*
34 * Connection engine.
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/netsmb/smb_conn.c 175635 2008-01-24 12:34:30Z attilio $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/malloc.h>
44#include <sys/priv.h>
45#include <sys/proc.h>
46#include <sys/lock.h>
47#include <sys/sysctl.h>
48#include <sys/socketvar.h>
49
50#include <sys/iconv.h>
51
52#include <netsmb/smb.h>
53#include <netsmb/smb_subr.h>
54#include <netsmb/smb_conn.h>
55#include <netsmb/smb_tran.h>
56#include <netsmb/smb_trantcp.h>
57
58static struct smb_connobj smb_vclist;
59static int smb_vcnext = 1;	/* next unique id for VC */
60
61SYSCTL_NODE(_net, OID_AUTO, smb, CTLFLAG_RW, NULL, "SMB protocol");
62
63MALLOC_DEFINE(M_SMBCONN, "smb_conn", "SMB connection");
64
65static void smb_co_init(struct smb_connobj *cp, int level, char *objname,
66	struct thread *td);
67static void smb_co_done(struct smb_connobj *cp);
68static int  smb_co_lockstatus(struct smb_connobj *cp, struct thread *td);
69
70static int  smb_vc_disconnect(struct smb_vc *vcp);
71static void smb_vc_free(struct smb_connobj *cp);
72static void smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred);
73static smb_co_free_t smb_share_free;
74static smb_co_gone_t smb_share_gone;
75
76static int  smb_sysctl_treedump(SYSCTL_HANDLER_ARGS);
77
78SYSCTL_PROC(_net_smb, OID_AUTO, treedump, CTLFLAG_RD | CTLTYPE_OPAQUE,
79	    NULL, 0, smb_sysctl_treedump, "S,treedump", "Requester tree");
80
81int
82smb_sm_init(void)
83{
84
85	smb_co_init(&smb_vclist, SMBL_SM, "smbsm", curthread);
86	smb_co_unlock(&smb_vclist, 0, curthread);
87	return 0;
88}
89
90int
91smb_sm_done(void)
92{
93
94	/* XXX: hold the mutex */
95	if (smb_vclist.co_usecount > 1) {
96		SMBERROR("%d connections still active\n", smb_vclist.co_usecount - 1);
97		return EBUSY;
98	}
99	lockmgr(&smb_vclist.co_lock, LK_DRAIN, 0);
100	smb_co_done(&smb_vclist);
101	return 0;
102}
103
104static int
105smb_sm_lockvclist(int flags, struct thread *td)
106{
107
108	return smb_co_lock(&smb_vclist, flags | LK_CANRECURSE, td);
109}
110
111static void
112smb_sm_unlockvclist(struct thread *td)
113{
114
115	smb_co_unlock(&smb_vclist, LK_RELEASE, td);
116}
117
118static int
119smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
120	struct smb_cred *scred,	struct smb_vc **vcpp)
121{
122	struct thread *td = scred->scr_td;
123	struct smb_connobj *scp;
124	struct smb_vc *vcp;
125	int exact = 1;
126	int error;
127
128	vcspec->shspec = shspec;
129	error = ENOENT;
130	vcp = NULL;
131	SMBCO_FOREACH(scp, &smb_vclist) {
132		vcp = (struct smb_vc *)scp;
133		error = smb_vc_lock(vcp, LK_EXCLUSIVE, td);
134		if (error)
135			continue;
136
137		if ((vcp->obj.co_flags & SMBV_PRIVATE) ||
138		    !CONNADDREQ(vcp->vc_paddr, vcspec->sap) ||
139		    strcmp(vcp->vc_username, vcspec->username) != 0)
140			goto err1;
141		if (vcspec->owner != SMBM_ANY_OWNER) {
142			if (vcp->vc_uid != vcspec->owner)
143				goto err1;
144		} else
145			exact = 0;
146		if (vcspec->group != SMBM_ANY_GROUP) {
147			if (vcp->vc_grp != vcspec->group)
148				goto err1;
149		} else
150			exact = 0;
151		if (vcspec->mode & SMBM_EXACT) {
152			if (!exact || (vcspec->mode & SMBM_MASK) !=
153			    vcp->vc_mode)
154				goto err1;
155		}
156		if (smb_vc_access(vcp, scred, vcspec->mode) != 0)
157			goto err1;
158		vcspec->ssp = NULL;
159		if (shspec) {
160			error = (int)smb_vc_lookupshare(vcp, shspec, scred,
161			    &vcspec->ssp);
162			if (error)
163				goto fail;
164		}
165		error = 0;
166		break;
167	err1:
168		error = 1;
169	fail:
170		smb_vc_unlock(vcp, 0, td);
171	}
172	if (vcp) {
173		smb_vc_ref(vcp);
174		*vcpp = vcp;
175	}
176	return (error);
177}
178
179int
180smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
181	struct smb_cred *scred,	struct smb_vc **vcpp)
182{
183	struct thread *td = scred->scr_td;
184	struct smb_vc *vcp;
185	struct smb_share *ssp = NULL;
186	int error;
187
188	*vcpp = vcp = NULL;
189
190	error = smb_sm_lockvclist(LK_EXCLUSIVE, td);
191	if (error)
192		return error;
193	error = smb_sm_lookupint(vcspec, shspec, scred, vcpp);
194	if (error == 0 || (vcspec->flags & SMBV_CREATE) == 0) {
195		smb_sm_unlockvclist(td);
196		return error;
197	}
198	error = smb_sm_lookupint(vcspec, NULL, scred, &vcp);
199	if (error) {
200		error = smb_vc_create(vcspec, scred, &vcp);
201		if (error)
202			goto out;
203		error = smb_vc_connect(vcp, scred);
204		if (error)
205			goto out;
206	}
207	if (shspec == NULL)
208		goto out;
209	error = smb_share_create(vcp, shspec, scred, &ssp);
210	if (error)
211		goto out;
212	error = smb_smb_treeconnect(ssp, scred);
213	if (error == 0)
214		vcspec->ssp = ssp;
215	else
216		smb_share_put(ssp, scred);
217out:
218	smb_sm_unlockvclist(td);
219	if (error == 0)
220		*vcpp = vcp;
221	else if (vcp)
222		smb_vc_put(vcp, scred);
223	return error;
224}
225
226/*
227 * Common code for connection object
228 */
229static void
230smb_co_init(struct smb_connobj *cp, int level, char *objname, struct thread *td)
231{
232	SLIST_INIT(&cp->co_children);
233	smb_sl_init(&cp->co_interlock, objname);
234	lockinit(&cp->co_lock, PZERO, objname, 0, 0);
235	cp->co_level = level;
236	cp->co_usecount = 1;
237	if (smb_co_lock(cp, LK_EXCLUSIVE, td) != 0)
238	    panic("smb_co_init: lock failed");
239}
240
241static void
242smb_co_done(struct smb_connobj *cp)
243{
244	smb_sl_destroy(&cp->co_interlock);
245	lockmgr(&cp->co_lock, LK_RELEASE, 0);
246	lockdestroy(&cp->co_lock);
247}
248
249static void
250smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred)
251{
252	struct smb_connobj *parent;
253
254	if (cp->co_gone)
255		cp->co_gone(cp, scred);
256	parent = cp->co_parent;
257	if (parent) {
258		smb_co_lock(parent, LK_EXCLUSIVE, scred->scr_td);
259		SLIST_REMOVE(&parent->co_children, cp, smb_connobj, co_next);
260		smb_co_put(parent, scred);
261	}
262	if (cp->co_free)
263		cp->co_free(cp);
264}
265
266void
267smb_co_ref(struct smb_connobj *cp)
268{
269
270	SMB_CO_LOCK(cp);
271	cp->co_usecount++;
272	SMB_CO_UNLOCK(cp);
273}
274
275void
276smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
277{
278
279	SMB_CO_LOCK(cp);
280	if (cp->co_usecount > 1) {
281		cp->co_usecount--;
282		SMB_CO_UNLOCK(cp);
283		return;
284	}
285	if (cp->co_usecount == 0) {
286		SMBERROR("negative use_count for object %d", cp->co_level);
287		SMB_CO_UNLOCK(cp);
288		return;
289	}
290	cp->co_usecount--;
291	cp->co_flags |= SMBO_GONE;
292
293	lockmgr(&cp->co_lock, LK_DRAIN | LK_INTERLOCK, &cp->co_interlock);
294	smb_co_gone(cp, scred);
295}
296
297int
298smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred)
299{
300	int error;
301
302	if ((flags & LK_INTERLOCK) == 0)
303		SMB_CO_LOCK(cp);
304	cp->co_usecount++;
305	error = smb_co_lock(cp, flags | LK_INTERLOCK, scred->scr_td);
306	if (error) {
307		SMB_CO_LOCK(cp);
308		cp->co_usecount--;
309		SMB_CO_UNLOCK(cp);
310		return error;
311	}
312	return 0;
313}
314
315void
316smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
317{
318
319	SMB_CO_LOCK(cp);
320	if (cp->co_usecount > 1) {
321		cp->co_usecount--;
322	} else if (cp->co_usecount == 1) {
323		cp->co_usecount--;
324		cp->co_flags |= SMBO_GONE;
325	} else {
326		SMBERROR("negative usecount");
327	}
328	lockmgr(&cp->co_lock, LK_RELEASE | LK_INTERLOCK, &cp->co_interlock);
329	if ((cp->co_flags & SMBO_GONE) == 0)
330		return;
331	lockmgr(&cp->co_lock, LK_DRAIN, NULL);
332	smb_co_gone(cp, scred);
333}
334
335int
336smb_co_lockstatus(struct smb_connobj *cp, struct thread *td)
337{
338	return lockstatus(&cp->co_lock, td);
339}
340
341int
342smb_co_lock(struct smb_connobj *cp, int flags, struct thread *td)
343{
344
345	if (cp->co_flags & SMBO_GONE)
346		return EINVAL;
347	if ((flags & LK_TYPE_MASK) == 0)
348		flags |= LK_EXCLUSIVE;
349	if (smb_co_lockstatus(cp, td) == LK_EXCLUSIVE &&
350	    (flags & LK_CANRECURSE) == 0) {
351		SMBERROR("recursive lock for object %d\n", cp->co_level);
352		return 0;
353	}
354	return lockmgr(&cp->co_lock, flags, &cp->co_interlock);
355}
356
357void
358smb_co_unlock(struct smb_connobj *cp, int flags, struct thread *td)
359{
360	(void)lockmgr(&cp->co_lock, flags | LK_RELEASE, &cp->co_interlock);
361}
362
363static void
364smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
365{
366	KASSERT(smb_co_lockstatus(parent, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: parent not locked"));
367	KASSERT(smb_co_lockstatus(child, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: child not locked"));
368
369	smb_co_ref(parent);
370	SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
371	child->co_parent = parent;
372}
373
374/*
375 * Session implementation
376 */
377
378int
379smb_vc_create(struct smb_vcspec *vcspec,
380	struct smb_cred *scred, struct smb_vc **vcpp)
381{
382	struct smb_vc *vcp;
383	struct thread *td = scred->scr_td;
384	struct ucred *cred = scred->scr_cred;
385	uid_t uid = vcspec->owner;
386	gid_t gid = vcspec->group;
387	uid_t realuid = cred->cr_uid;
388	char *domain = vcspec->domain;
389	int error, isroot;
390
391	isroot = smb_suser(cred) == 0;
392	/*
393	 * Only superuser can create VCs with different uid and gid
394	 */
395	if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
396		return EPERM;
397	if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
398		return EPERM;
399
400	vcp = smb_zmalloc(sizeof(*vcp), M_SMBCONN, M_WAITOK);
401	smb_co_init(VCTOCP(vcp), SMBL_VC, "smb_vc", td);
402	vcp->obj.co_free = smb_vc_free;
403	vcp->obj.co_gone = smb_vc_gone;
404	vcp->vc_number = smb_vcnext++;
405	vcp->vc_timo = SMB_DEFRQTIMO;
406	vcp->vc_smbuid = SMB_UID_UNKNOWN;
407	vcp->vc_mode = vcspec->rights & SMBM_MASK;
408	vcp->obj.co_flags = vcspec->flags & (SMBV_PRIVATE | SMBV_SINGLESHARE);
409	vcp->vc_tdesc = &smb_tran_nbtcp_desc;
410	vcp->vc_seqno = 0;
411	vcp->vc_mackey = NULL;
412	vcp->vc_mackeylen = 0;
413
414	if (uid == SMBM_ANY_OWNER)
415		uid = realuid;
416	if (gid == SMBM_ANY_GROUP)
417		gid = cred->cr_groups[0];
418	vcp->vc_uid = uid;
419	vcp->vc_grp = gid;
420
421	smb_sl_init(&vcp->vc_stlock, "vcstlock");
422	error = ENOMEM;
423
424	vcp->vc_paddr = sodupsockaddr(vcspec->sap, M_WAITOK);
425	if (vcp->vc_paddr == NULL)
426		goto fail;
427	vcp->vc_laddr = sodupsockaddr(vcspec->lap, M_WAITOK);
428	if (vcp->vc_laddr == NULL)
429		goto fail;
430	vcp->vc_pass = smb_strdup(vcspec->pass);
431	if (vcp->vc_pass == NULL)
432		goto fail;
433	vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain :
434	    "NODOMAIN");
435	if (vcp->vc_domain == NULL)
436		goto fail;
437	vcp->vc_srvname = smb_strdup(vcspec->srvname);
438	if (vcp->vc_srvname == NULL)
439		goto fail;
440	vcp->vc_username = smb_strdup(vcspec->username);
441	if (vcp->vc_username == NULL)
442		goto fail;
443	error = (int)iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower);
444	if (error)
445		goto fail;
446	error = (int)iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper);
447	if (error)
448		goto fail;
449	if (vcspec->servercs[0]) {
450		error = (int)iconv_open(vcspec->servercs, vcspec->localcs,
451		    &vcp->vc_toserver);
452		if (error)
453			goto fail;
454		error = (int)iconv_open(vcspec->localcs, vcspec->servercs,
455		    &vcp->vc_tolocal);
456		if (error)
457			goto fail;
458	}
459	error = (int)smb_iod_create(vcp);
460	if (error)
461		goto fail;
462	*vcpp = vcp;
463	smb_co_addchild(&smb_vclist, VCTOCP(vcp));
464	return (0);
465
466 fail:
467	smb_vc_put(vcp, scred);
468	return (error);
469}
470
471static void
472smb_vc_free(struct smb_connobj *cp)
473{
474	struct smb_vc *vcp = CPTOVC(cp);
475
476	if (vcp->vc_iod)
477		smb_iod_destroy(vcp->vc_iod);
478	SMB_STRFREE(vcp->vc_username);
479	SMB_STRFREE(vcp->vc_srvname);
480	SMB_STRFREE(vcp->vc_pass);
481	SMB_STRFREE(vcp->vc_domain);
482	if (vcp->vc_mackey)
483		free(vcp->vc_mackey, M_SMBTEMP);
484	if (vcp->vc_paddr)
485		free(vcp->vc_paddr, M_SONAME);
486	if (vcp->vc_laddr)
487		free(vcp->vc_laddr, M_SONAME);
488	if (vcp->vc_tolower)
489		iconv_close(vcp->vc_tolower);
490	if (vcp->vc_toupper)
491		iconv_close(vcp->vc_toupper);
492	if (vcp->vc_tolocal)
493		iconv_close(vcp->vc_tolocal);
494	if (vcp->vc_toserver)
495		iconv_close(vcp->vc_toserver);
496	smb_co_done(VCTOCP(vcp));
497	smb_sl_destroy(&vcp->vc_stlock);
498	free(vcp, M_SMBCONN);
499}
500
501/*
502 * Called when use count of VC dropped to zero.
503 * VC should be locked on enter with LK_DRAIN.
504 */
505static void
506smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred)
507{
508	struct smb_vc *vcp = CPTOVC(cp);
509
510	smb_vc_disconnect(vcp);
511}
512
513void
514smb_vc_ref(struct smb_vc *vcp)
515{
516	smb_co_ref(VCTOCP(vcp));
517}
518
519void
520smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
521{
522	smb_co_rele(VCTOCP(vcp), scred);
523}
524
525int
526smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred)
527{
528	return smb_co_get(VCTOCP(vcp), flags, scred);
529}
530
531void
532smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
533{
534	smb_co_put(VCTOCP(vcp), scred);
535}
536
537int
538smb_vc_lock(struct smb_vc *vcp, int flags, struct thread *td)
539{
540	return smb_co_lock(VCTOCP(vcp), flags, td);
541}
542
543void
544smb_vc_unlock(struct smb_vc *vcp, int flags, struct thread *td)
545{
546	smb_co_unlock(VCTOCP(vcp), flags, td);
547}
548
549int
550smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode)
551{
552	struct ucred *cred = scred->scr_cred;
553
554	if (smb_suser(cred) == 0 || cred->cr_uid == vcp->vc_uid)
555		return 0;
556	mode >>= 3;
557	if (!groupmember(vcp->vc_grp, cred))
558		mode >>= 3;
559	return (vcp->vc_mode & mode) == mode ? 0 : EACCES;
560}
561
562static int
563smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp)
564{
565	int exact = 1;
566
567	if (strcmp(ssp->ss_name, dp->name) != 0)
568		return 1;
569	if (dp->owner != SMBM_ANY_OWNER) {
570		if (ssp->ss_uid != dp->owner)
571			return 1;
572	} else
573		exact = 0;
574	if (dp->group != SMBM_ANY_GROUP) {
575		if (ssp->ss_grp != dp->group)
576			return 1;
577	} else
578		exact = 0;
579
580	if (dp->mode & SMBM_EXACT) {
581		if (!exact)
582			return 1;
583		return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1;
584	}
585	if (smb_share_access(ssp, dp->scred, dp->mode) != 0)
586		return 1;
587	return 0;
588}
589
590/*
591 * Lookup share in the given VC. Share referenced and locked on return.
592 * VC expected to be locked on entry and will be left locked on exit.
593 */
594int
595smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp,
596	struct smb_cred *scred,	struct smb_share **sspp)
597{
598	struct thread *td = scred->scr_td;
599	struct smb_connobj *scp = NULL;
600	struct smb_share *ssp = NULL;
601	int error;
602
603	*sspp = NULL;
604	dp->scred = scred;
605	SMBCO_FOREACH(scp, VCTOCP(vcp)) {
606		ssp = (struct smb_share *)scp;
607		error = smb_share_lock(ssp, LK_EXCLUSIVE, td);
608		if (error)
609			continue;
610		if (smb_vc_cmpshare(ssp, dp) == 0)
611			break;
612		smb_share_unlock(ssp, 0, td);
613	}
614	if (ssp) {
615		smb_share_ref(ssp);
616		*sspp = ssp;
617		error = 0;
618	} else
619		error = ENOENT;
620	return error;
621}
622
623int
624smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred)
625{
626
627	return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
628}
629
630/*
631 * Destroy VC to server, invalidate shares linked with it.
632 * Transport should be locked on entry.
633 */
634int
635smb_vc_disconnect(struct smb_vc *vcp)
636{
637
638	smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT | SMBIOD_EV_SYNC, NULL);
639	return 0;
640}
641
642static char smb_emptypass[] = "";
643
644const char *
645smb_vc_getpass(struct smb_vc *vcp)
646{
647	if (vcp->vc_pass)
648		return vcp->vc_pass;
649	return smb_emptypass;
650}
651
652static int
653smb_vc_getinfo(struct smb_vc *vcp, struct smb_vc_info *vip)
654{
655	bzero(vip, sizeof(struct smb_vc_info));
656	vip->itype = SMB_INFO_VC;
657	vip->usecount = vcp->obj.co_usecount;
658	vip->uid = vcp->vc_uid;
659	vip->gid = vcp->vc_grp;
660	vip->mode = vcp->vc_mode;
661	vip->flags = vcp->obj.co_flags;
662	vip->sopt = vcp->vc_sopt;
663	vip->iodstate = vcp->vc_iod->iod_state;
664	bzero(&vip->sopt.sv_skey, sizeof(vip->sopt.sv_skey));
665	snprintf(vip->srvname, sizeof(vip->srvname), "%s", vcp->vc_srvname);
666	snprintf(vip->vcname, sizeof(vip->vcname), "%s", vcp->vc_username);
667	return 0;
668}
669
670u_short
671smb_vc_nextmid(struct smb_vc *vcp)
672{
673	u_short r;
674
675	SMB_CO_LOCK(&vcp->obj);
676	r = vcp->vc_mid++;
677	SMB_CO_UNLOCK(&vcp->obj);
678	return r;
679}
680
681/*
682 * Share implementation
683 */
684/*
685 * Allocate share structure and attach it to the given VC
686 * Connection expected to be locked on entry. Share will be returned
687 * in locked state.
688 */
689int
690smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
691	struct smb_cred *scred, struct smb_share **sspp)
692{
693	struct smb_share *ssp;
694	struct thread *td = scred->scr_td;
695	struct ucred *cred = scred->scr_cred;
696	uid_t realuid = cred->cr_uid;
697	uid_t uid = shspec->owner;
698	gid_t gid = shspec->group;
699	int error, isroot;
700
701	isroot = smb_suser(cred) == 0;
702	/*
703	 * Only superuser can create shares with different uid and gid
704	 */
705	if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
706		return EPERM;
707	if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
708		return EPERM;
709	error = smb_vc_lookupshare(vcp, shspec, scred, &ssp);
710	if (!error) {
711		smb_share_put(ssp, scred);
712		return EEXIST;
713	}
714	if (uid == SMBM_ANY_OWNER)
715		uid = realuid;
716	if (gid == SMBM_ANY_GROUP)
717		gid = cred->cr_groups[0];
718	ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK);
719	smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss", td);
720	ssp->obj.co_free = smb_share_free;
721	ssp->obj.co_gone = smb_share_gone;
722	smb_sl_init(&ssp->ss_stlock, "ssstlock");
723	ssp->ss_name = smb_strdup(shspec->name);
724	if (shspec->pass && shspec->pass[0])
725		ssp->ss_pass = smb_strdup(shspec->pass);
726	ssp->ss_type = shspec->stype;
727	ssp->ss_tid = SMB_TID_UNKNOWN;
728	ssp->ss_uid = uid;
729	ssp->ss_grp = gid;
730	ssp->ss_mode = shspec->rights & SMBM_MASK;
731	smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp));
732	*sspp = ssp;
733	return 0;
734}
735
736static void
737smb_share_free(struct smb_connobj *cp)
738{
739	struct smb_share *ssp = CPTOSS(cp);
740
741	SMB_STRFREE(ssp->ss_name);
742	SMB_STRFREE(ssp->ss_pass);
743	smb_sl_destroy(&ssp->ss_stlock);
744	smb_co_done(SSTOCP(ssp));
745	free(ssp, M_SMBCONN);
746}
747
748static void
749smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred)
750{
751	struct smb_share *ssp = CPTOSS(cp);
752
753	smb_smb_treedisconnect(ssp, scred);
754}
755
756void
757smb_share_ref(struct smb_share *ssp)
758{
759	smb_co_ref(SSTOCP(ssp));
760}
761
762void
763smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
764{
765	smb_co_rele(SSTOCP(ssp), scred);
766}
767
768int
769smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred)
770{
771	return smb_co_get(SSTOCP(ssp), flags, scred);
772}
773
774void
775smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
776{
777	smb_co_put(SSTOCP(ssp), scred);
778}
779
780int
781smb_share_lock(struct smb_share *ssp, int flags, struct thread *td)
782{
783	return smb_co_lock(SSTOCP(ssp), flags, td);
784}
785
786void
787smb_share_unlock(struct smb_share *ssp, int flags, struct thread *td)
788{
789	smb_co_unlock(SSTOCP(ssp), flags, td);
790}
791
792int
793smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode)
794{
795	struct ucred *cred = scred->scr_cred;
796
797	if (smb_suser(cred) == 0 || cred->cr_uid == ssp->ss_uid)
798		return 0;
799	mode >>= 3;
800	if (!groupmember(ssp->ss_grp, cred))
801		mode >>= 3;
802	return (ssp->ss_mode & mode) == mode ? 0 : EACCES;
803}
804
805void
806smb_share_invalidate(struct smb_share *ssp)
807{
808	ssp->ss_tid = SMB_TID_UNKNOWN;
809}
810
811int
812smb_share_valid(struct smb_share *ssp)
813{
814	return ssp->ss_tid != SMB_TID_UNKNOWN &&
815	    ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid;
816}
817
818const char*
819smb_share_getpass(struct smb_share *ssp)
820{
821	struct smb_vc *vcp;
822
823	if (ssp->ss_pass)
824		return ssp->ss_pass;
825	vcp = SSTOVC(ssp);
826	if (vcp->vc_pass)
827		return vcp->vc_pass;
828	return smb_emptypass;
829}
830
831static int
832smb_share_getinfo(struct smb_share *ssp, struct smb_share_info *sip)
833{
834	bzero(sip, sizeof(struct smb_share_info));
835	sip->itype = SMB_INFO_SHARE;
836	sip->usecount = ssp->obj.co_usecount;
837	sip->tid  = ssp->ss_tid;
838	sip->type= ssp->ss_type;
839	sip->uid = ssp->ss_uid;
840	sip->gid = ssp->ss_grp;
841	sip->mode= ssp->ss_mode;
842	sip->flags = ssp->obj.co_flags;
843	snprintf(sip->sname, sizeof(sip->sname), "%s", ssp->ss_name);
844	return 0;
845}
846
847/*
848 * Dump an entire tree into sysctl call
849 */
850static int
851smb_sysctl_treedump(SYSCTL_HANDLER_ARGS)
852{
853	struct thread *td = req->td;
854	struct smb_cred scred;
855	struct smb_connobj *scp1, *scp2;
856	struct smb_vc *vcp;
857	struct smb_share *ssp;
858	struct smb_vc_info vci;
859	struct smb_share_info ssi;
860	int error, itype;
861
862	smb_makescred(&scred, td, td->td_ucred);
863	error = sysctl_wire_old_buffer(req, 0);
864	if (error)
865		return (error);
866	error = smb_sm_lockvclist(LK_SHARED, td);
867	if (error)
868		return error;
869	SMBCO_FOREACH(scp1, &smb_vclist) {
870		vcp = (struct smb_vc *)scp1;
871		error = smb_vc_lock(vcp, LK_SHARED, td);
872		if (error)
873			continue;
874		smb_vc_getinfo(vcp, &vci);
875		error = SYSCTL_OUT(req, &vci, sizeof(struct smb_vc_info));
876		if (error) {
877			smb_vc_unlock(vcp, 0, td);
878			break;
879		}
880		SMBCO_FOREACH(scp2, VCTOCP(vcp)) {
881			ssp = (struct smb_share *)scp2;
882			error = smb_share_lock(ssp, LK_SHARED, td);
883			if (error) {
884				error = 0;
885				continue;
886			}
887			smb_share_getinfo(ssp, &ssi);
888			smb_share_unlock(ssp, 0, td);
889			error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info));
890			if (error)
891				break;
892		}
893		smb_vc_unlock(vcp, 0, td);
894		if (error)
895			break;
896	}
897	if (!error) {
898		itype = SMB_INFO_NONE;
899		error = SYSCTL_OUT(req, &itype, sizeof(itype));
900	}
901	smb_sm_unlockvclist(td);
902	return error;
903}
904