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