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