xref: /freebsd/sys/netsmb/smb_conn.c (revision cf02bf2407cb217c99cc82f78b7a2e7f0703c9ee)
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$");
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 
57 static struct smb_connobj smb_vclist;
58 static int smb_vcnext = 1;	/* next unique id for VC */
59 
60 SYSCTL_NODE(_net, OID_AUTO, smb, CTLFLAG_RW, NULL, "SMB protocol");
61 
62 MALLOC_DEFINE(M_SMBCONN, "SMB conn", "SMB connection");
63 
64 static void smb_co_init(struct smb_connobj *cp, int level, char *objname,
65 	struct thread *td);
66 static void smb_co_done(struct smb_connobj *cp);
67 static int  smb_co_lockstatus(struct smb_connobj *cp, struct thread *td);
68 
69 static int  smb_vc_disconnect(struct smb_vc *vcp);
70 static void smb_vc_free(struct smb_connobj *cp);
71 static void smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred);
72 static smb_co_free_t smb_share_free;
73 static smb_co_gone_t smb_share_gone;
74 
75 static int  smb_sysctl_treedump(SYSCTL_HANDLER_ARGS);
76 
77 SYSCTL_PROC(_net_smb, OID_AUTO, treedump, CTLFLAG_RD | CTLTYPE_OPAQUE,
78 	    NULL, 0, smb_sysctl_treedump, "S,treedump", "Requester tree");
79 
80 int
81 smb_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 
89 int
90 smb_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 	smb_co_done(&smb_vclist);
99 	return 0;
100 }
101 
102 static int
103 smb_sm_lockvclist(int flags, struct thread *td)
104 {
105 
106 	return smb_co_lock(&smb_vclist, flags | LK_CANRECURSE, td);
107 }
108 
109 static void
110 smb_sm_unlockvclist(struct thread *td)
111 {
112 
113 	smb_co_unlock(&smb_vclist, LK_RELEASE, td);
114 }
115 
116 static int
117 smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
118 	struct smb_cred *scred,	struct smb_vc **vcpp)
119 {
120 	struct thread *td = scred->scr_td;
121 	struct smb_connobj *scp;
122 	struct smb_vc *vcp;
123 	int exact = 1;
124 	int error;
125 
126 	vcspec->shspec = shspec;
127 	error = ENOENT;
128 	vcp = NULL;
129 	SMBCO_FOREACH(scp, &smb_vclist) {
130 		vcp = (struct smb_vc *)scp;
131 		error = smb_vc_lock(vcp, LK_EXCLUSIVE, td);
132 		if (error)
133 			continue;
134 
135 		if ((vcp->obj.co_flags & SMBV_PRIVATE) ||
136 		    !CONNADDREQ(vcp->vc_paddr, vcspec->sap) ||
137 		    strcmp(vcp->vc_username, vcspec->username) != 0)
138 			goto err1;
139 		if (vcspec->owner != SMBM_ANY_OWNER) {
140 			if (vcp->vc_uid != vcspec->owner)
141 				goto err1;
142 		} else
143 			exact = 0;
144 		if (vcspec->group != SMBM_ANY_GROUP) {
145 			if (vcp->vc_grp != vcspec->group)
146 				goto err1;
147 		} else
148 			exact = 0;
149 		if (vcspec->mode & SMBM_EXACT) {
150 			if (!exact || (vcspec->mode & SMBM_MASK) !=
151 			    vcp->vc_mode)
152 				goto err1;
153 		}
154 		if (smb_vc_access(vcp, scred, vcspec->mode) != 0)
155 			goto err1;
156 		vcspec->ssp = NULL;
157 		if (shspec) {
158 			error = (int)smb_vc_lookupshare(vcp, shspec, scred,
159 			    &vcspec->ssp);
160 			if (error)
161 				goto fail;
162 		}
163 		error = 0;
164 		break;
165 	err1:
166 		error = 1;
167 	fail:
168 		smb_vc_unlock(vcp, 0, td);
169 	}
170 	if (vcp) {
171 		smb_vc_ref(vcp);
172 		*vcpp = vcp;
173 	}
174 	return (error);
175 }
176 
177 int
178 smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
179 	struct smb_cred *scred,	struct smb_vc **vcpp)
180 {
181 	struct thread *td = scred->scr_td;
182 	struct smb_vc *vcp;
183 	struct smb_share *ssp = NULL;
184 	int error;
185 
186 	*vcpp = vcp = NULL;
187 
188 	error = smb_sm_lockvclist(LK_EXCLUSIVE, td);
189 	if (error)
190 		return error;
191 	error = smb_sm_lookupint(vcspec, shspec, scred, vcpp);
192 	if (error == 0 || (vcspec->flags & SMBV_CREATE) == 0) {
193 		smb_sm_unlockvclist(td);
194 		return error;
195 	}
196 	error = smb_sm_lookupint(vcspec, NULL, scred, &vcp);
197 	if (error) {
198 		error = smb_vc_create(vcspec, scred, &vcp);
199 		if (error)
200 			goto out;
201 		error = smb_vc_connect(vcp, scred);
202 		if (error)
203 			goto out;
204 	}
205 	if (shspec == NULL)
206 		goto out;
207 	error = smb_share_create(vcp, shspec, scred, &ssp);
208 	if (error)
209 		goto out;
210 	error = smb_smb_treeconnect(ssp, scred);
211 	if (error == 0)
212 		vcspec->ssp = ssp;
213 	else
214 		smb_share_put(ssp, scred);
215 out:
216 	smb_sm_unlockvclist(td);
217 	if (error == 0)
218 		*vcpp = vcp;
219 	else if (vcp)
220 		smb_vc_put(vcp, scred);
221 	return error;
222 }
223 
224 /*
225  * Common code for connection object
226  */
227 static void
228 smb_co_init(struct smb_connobj *cp, int level, char *objname, struct thread *td)
229 {
230 	SLIST_INIT(&cp->co_children);
231 	smb_sl_init(&cp->co_interlock, objname);
232 	lockinit(&cp->co_lock, PZERO, objname, 0, 0);
233 	cp->co_level = level;
234 	cp->co_usecount = 1;
235 	KASSERT(smb_co_lock(cp, LK_EXCLUSIVE, td) == 0, ("smb_co_init: lock failed"));
236 }
237 
238 static void
239 smb_co_done(struct smb_connobj *cp)
240 {
241 	smb_sl_destroy(&cp->co_interlock);
242 	lockdestroy(&cp->co_lock);
243 }
244 
245 static void
246 smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred)
247 {
248 	struct smb_connobj *parent;
249 
250 	if (cp->co_gone)
251 		cp->co_gone(cp, scred);
252 	parent = cp->co_parent;
253 	if (parent) {
254 		smb_co_lock(parent, LK_EXCLUSIVE, scred->scr_td);
255 		SLIST_REMOVE(&parent->co_children, cp, smb_connobj, co_next);
256 		smb_co_put(parent, scred);
257 	}
258 	if (cp->co_free)
259 		cp->co_free(cp);
260 }
261 
262 void
263 smb_co_ref(struct smb_connobj *cp)
264 {
265 
266 	SMB_CO_LOCK(cp);
267 	cp->co_usecount++;
268 	SMB_CO_UNLOCK(cp);
269 }
270 
271 void
272 smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
273 {
274 	struct thread *td = scred->scr_td;
275 
276 	SMB_CO_LOCK(cp);
277 	if (cp->co_usecount > 1) {
278 		cp->co_usecount--;
279 		SMB_CO_UNLOCK(cp);
280 		return;
281 	}
282 	if (cp->co_usecount == 0) {
283 		SMBERROR("negative use_count for object %d", cp->co_level);
284 		SMB_CO_UNLOCK(cp);
285 		return;
286 	}
287 	cp->co_usecount--;
288 	cp->co_flags |= SMBO_GONE;
289 
290 	lockmgr(&cp->co_lock, LK_DRAIN | LK_INTERLOCK, &cp->co_interlock, td);
291 	smb_co_gone(cp, scred);
292 }
293 
294 int
295 smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred)
296 {
297 	int error;
298 
299 	if ((flags & LK_INTERLOCK) == 0)
300 		SMB_CO_LOCK(cp);
301 	cp->co_usecount++;
302 	error = smb_co_lock(cp, flags | LK_INTERLOCK, scred->scr_td);
303 	if (error) {
304 		SMB_CO_LOCK(cp);
305 		cp->co_usecount--;
306 		SMB_CO_UNLOCK(cp);
307 		return error;
308 	}
309 	return 0;
310 }
311 
312 void
313 smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
314 {
315 	struct thread *td = scred->scr_td;
316 	int flags;
317 
318 	flags = LK_RELEASE;
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 		flags = LK_DRAIN;
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 
336 int
337 smb_co_lockstatus(struct smb_connobj *cp, struct thread *td)
338 {
339 	return lockstatus(&cp->co_lock, td);
340 }
341 
342 int
343 smb_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 
358 void
359 smb_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 
364 static void
365 smb_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 
379 int
380 smb_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 
472 static void
473 smb_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  */
506 static void
507 smb_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 
514 void
515 smb_vc_ref(struct smb_vc *vcp)
516 {
517 	smb_co_ref(VCTOCP(vcp));
518 }
519 
520 void
521 smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
522 {
523 	smb_co_rele(VCTOCP(vcp), scred);
524 }
525 
526 int
527 smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred)
528 {
529 	return smb_co_get(VCTOCP(vcp), flags, scred);
530 }
531 
532 void
533 smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
534 {
535 	smb_co_put(VCTOCP(vcp), scred);
536 }
537 
538 int
539 smb_vc_lock(struct smb_vc *vcp, int flags, struct thread *td)
540 {
541 	return smb_co_lock(VCTOCP(vcp), flags, td);
542 }
543 
544 void
545 smb_vc_unlock(struct smb_vc *vcp, int flags, struct thread *td)
546 {
547 	smb_co_unlock(VCTOCP(vcp), flags, td);
548 }
549 
550 int
551 smb_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 
563 static int
564 smb_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  */
595 int
596 smb_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 
624 int
625 smb_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  */
635 int
636 smb_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 
643 static char smb_emptypass[] = "";
644 
645 const char *
646 smb_vc_getpass(struct smb_vc *vcp)
647 {
648 	if (vcp->vc_pass)
649 		return vcp->vc_pass;
650 	return smb_emptypass;
651 }
652 
653 static int
654 smb_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 
671 u_short
672 smb_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  */
690 int
691 smb_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 
737 static void
738 smb_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 
749 static void
750 smb_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 
757 void
758 smb_share_ref(struct smb_share *ssp)
759 {
760 	smb_co_ref(SSTOCP(ssp));
761 }
762 
763 void
764 smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
765 {
766 	smb_co_rele(SSTOCP(ssp), scred);
767 }
768 
769 int
770 smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred)
771 {
772 	return smb_co_get(SSTOCP(ssp), flags, scred);
773 }
774 
775 void
776 smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
777 {
778 	smb_co_put(SSTOCP(ssp), scred);
779 }
780 
781 int
782 smb_share_lock(struct smb_share *ssp, int flags, struct thread *td)
783 {
784 	return smb_co_lock(SSTOCP(ssp), flags, td);
785 }
786 
787 void
788 smb_share_unlock(struct smb_share *ssp, int flags, struct thread *td)
789 {
790 	smb_co_unlock(SSTOCP(ssp), flags, td);
791 }
792 
793 int
794 smb_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 
806 void
807 smb_share_invalidate(struct smb_share *ssp)
808 {
809 	ssp->ss_tid = SMB_TID_UNKNOWN;
810 }
811 
812 int
813 smb_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 
819 const char*
820 smb_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 
832 static int
833 smb_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  */
851 static int
852 smb_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