xref: /dragonfly/sys/netproto/smb/smb_conn.c (revision fe76c4fb)
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.16 2006/05/05 21:15:09 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/lock.h>
46 #include <sys/sysctl.h>
47 #include <sys/socketvar.h>
48 
49 #include <sys/iconv.h>
50 
51 #include "smb.h"
52 #include "smb_subr.h"
53 #include "smb_conn.h"
54 #include "smb_tran.h"
55 #include "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 
73 /*
74  * Connection object
75  */
76 static void smb_co_ref(struct smb_connobj *cp, struct thread *td);
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, struct thread *td);
81 static void smb_co_unlock(struct smb_connobj *cp, int flags, struct thread *td);
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", curthread);
95 	smb_co_unlock(&smb_vclist, 0, curthread);
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, struct thread *td)
114 {
115 	return smb_co_lock(&smb_vclist, flags | LK_CANRECURSE, td);
116 }
117 
118 static void
119 smb_sm_unlockvclist(struct thread *td)
120 {
121 	smb_co_unlock(&smb_vclist, LK_RELEASE, td);
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 thread *td = scred->scr_td;
129 	struct smb_vc *vcp;
130 	int exact = 1;
131 	int error;
132 
133 	vcspec->shspec = shspec;
134 	error = ENOENT;
135 	SMBCO_FOREACH((struct smb_connobj*)vcp, &smb_vclist) {
136 		error = smb_vc_lock(vcp, LK_EXCLUSIVE, td);
137 		if (error)
138 			continue;
139 		itry {
140 			if ((vcp->obj.co_flags & SMBV_PRIVATE) ||
141 			    !CONNADDREQ(vcp->vc_paddr, vcspec->sap) ||
142 			    strcmp(vcp->vc_username, vcspec->username) != 0)
143 				ithrow(1);
144 			if (vcspec->owner != SMBM_ANY_OWNER) {
145 				if (vcp->vc_uid != vcspec->owner)
146 					ithrow(1);
147 			} else
148 				exact = 0;
149 			if (vcspec->group != SMBM_ANY_GROUP) {
150 				if (vcp->vc_grp != vcspec->group)
151 					ithrow(1);
152 			} else
153 				exact = 0;
154 
155 			if (vcspec->mode & SMBM_EXACT) {
156 				if (!exact ||
157 				    (vcspec->mode & SMBM_MASK) != vcp->vc_mode)
158 					ithrow(1);
159 			}
160 			if (smb_vc_access(vcp, scred, vcspec->mode) != 0)
161 				ithrow(1);
162 			vcspec->ssp = NULL;
163 			if (shspec)
164 				ithrow(smb_vc_lookupshare(vcp, shspec, scred, &vcspec->ssp));
165 			error = 0;
166 			break;
167 		} icatch(error) {
168 			smb_vc_unlock(vcp, 0, td);
169 		} ifinally {
170 		} iendtry;
171 		if (error == 0)
172 			break;
173 	}
174 	if (vcp) {
175 		smb_vc_ref(vcp, td);
176 		*vcpp = vcp;
177 	}
178 	return error;
179 }
180 
181 int
182 smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
183 	struct smb_cred *scred,	struct smb_vc **vcpp)
184 {
185 	struct thread *td = scred->scr_td;
186 	struct smb_vc *vcp;
187 	struct smb_share *ssp = NULL;
188 	int error;
189 
190 	*vcpp = vcp = NULL;
191 
192 	error = smb_sm_lockvclist(LK_EXCLUSIVE, td);
193 	if (error)
194 		return error;
195 	error = smb_sm_lookupint(vcspec, shspec, scred, vcpp);
196 	if (error == 0 || (vcspec->flags & SMBV_CREATE) == 0) {
197 		smb_sm_unlockvclist(td);
198 		return error;
199 	}
200 	error = smb_sm_lookupint(vcspec, NULL, scred, &vcp);
201 	if (error) {
202 		error = smb_vc_create(vcspec, scred, &vcp);
203 		if (error)
204 			goto out;
205 		error = smb_vc_connect(vcp, scred);
206 		if (error)
207 			goto out;
208 	}
209 	if (shspec == NULL)
210 		goto out;
211 	error = smb_share_create(vcp, shspec, scred, &ssp);
212 	if (error)
213 		goto out;
214 	error = smb_smb_treeconnect(ssp, scred);
215 	if (error == 0)
216 		vcspec->ssp = ssp;
217 	else
218 		smb_share_put(ssp, scred);
219 out:
220 	smb_sm_unlockvclist(td);
221 	if (error == 0)
222 		*vcpp = vcp;
223 	else if (vcp)
224 		smb_vc_put(vcp, scred);
225 	return error;
226 }
227 
228 /*
229  * Common code for connection object
230  */
231 static void
232 smb_co_init(struct smb_connobj *cp, int level, char *objname, struct thread *td)
233 {
234 	SLIST_INIT(&cp->co_children);
235 	smb_sl_init(&cp->co_interlock, objname);
236 	lockinit(&cp->co_lock, objname, 0, 0);
237 	cp->co_level = level;
238 	cp->co_usecount = 1;
239 	smb_co_lock(cp, LK_EXCLUSIVE, td);
240 }
241 
242 static void
243 smb_co_done(struct smb_connobj *cp)
244 {
245 	smb_sl_destroy(&cp->co_interlock);
246 	lockdestroy(&cp->co_lock);
247 }
248 
249 static void
250 smb_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 	lockmgr(&cp->co_lock, LK_RELEASE);
263 	while (cp->co_usecount > 0) {
264 		tsleep(&cp->co_lock, 0, "smbgone", hz);
265 	}
266 	if (cp->co_free)
267 		cp->co_free(cp);
268 }
269 
270 void
271 smb_co_ref(struct smb_connobj *cp, struct thread *td)
272 {
273 	SMB_CO_LOCK(cp);
274 	cp->co_usecount++;
275 	SMB_CO_UNLOCK(cp);
276 }
277 
278 void
279 smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
280 {
281 	SMB_CO_LOCK(cp);
282 	if (cp->co_usecount > 1) {
283 		cp->co_usecount--;
284 		SMB_CO_UNLOCK(cp);
285 		return;
286 	}
287 	if (cp->co_usecount <= 0) {
288 		SMBERROR("negative use_count for object %d", cp->co_level);
289 		SMB_CO_UNLOCK(cp);
290 		return;
291 	}
292 	cp->co_usecount = 0;
293 	if ((cp->co_flags & SMBO_GONE) == 0) {
294 		cp->co_flags |= SMBO_GONE;
295 		SMB_CO_UNLOCK(cp);
296 		lockmgr(&cp->co_lock, LK_EXCLUSIVE);
297 		smb_co_gone(cp, scred);
298 	} else {
299 		SMB_CO_UNLOCK(cp);
300 		wakeup(&cp->co_lock);
301 	}
302 }
303 
304 int
305 smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred)
306 {
307 	int error;
308 
309 	SMB_CO_LOCK(cp);
310 	cp->co_usecount++;
311 	SMB_CO_UNLOCK(cp);
312 	error = smb_co_lock(cp, flags, scred->scr_td);
313 	if (error) {
314 		SMB_CO_LOCK(cp);
315 		if (cp->co_usecount > 1) {
316 			cp->co_usecount--;
317 			SMB_CO_UNLOCK(cp);
318 		} else if (cp->co_usecount <= 0) {
319 			SMBERROR("negative use_count for object %d", cp->co_level);
320 			SMB_CO_UNLOCK(cp);
321 		} else {
322 			cp->co_usecount = 0;
323 			if ((cp->co_flags & SMBO_GONE) == 0) {
324 				cp->co_flags |= SMBO_GONE;
325 				SMB_CO_UNLOCK(cp);
326 				lockmgr(&cp->co_lock, LK_EXCLUSIVE);
327 				smb_co_gone(cp, scred);
328 			} else {
329 				SMB_CO_UNLOCK(cp);
330 			}
331 		}
332 		return error;
333 	}
334 	return 0;
335 }
336 
337 void
338 smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
339 {
340 	SMB_CO_LOCK(cp);
341 	if (cp->co_usecount > 1) {
342 		cp->co_usecount--;
343 		SMB_CO_UNLOCK(cp);
344 		lockmgr(&cp->co_lock, LK_RELEASE);
345 		return;
346 	}
347 	if (cp->co_usecount <= 0) {
348 		SMBERROR("negative use_count for object %d", cp->co_level);
349 		SMB_CO_UNLOCK(cp);
350 		return;
351 	}
352 	cp->co_usecount = 0;
353 	if ((cp->co_flags & SMBO_GONE) == 0) {
354 		cp->co_flags |= SMBO_GONE;
355 		SMB_CO_UNLOCK(cp);
356 		lockmgr(&cp->co_lock, LK_RELEASE);
357 		lockmgr(&cp->co_lock, LK_EXCLUSIVE);
358 		smb_co_gone(cp, scred);
359 	} else {
360 		SMB_CO_UNLOCK(cp);
361 		lockmgr(&cp->co_lock, LK_RELEASE);
362 		wakeup(&cp->co_lock);
363 	}
364 }
365 
366 int
367 smb_co_lockstatus(struct smb_connobj *cp, struct thread *td)
368 {
369 	return lockstatus(&cp->co_lock, td);
370 }
371 
372 int
373 smb_co_lock(struct smb_connobj *cp, int flags, struct thread *td)
374 {
375 	int error;
376 
377 	KKASSERT(cp->co_usecount > 0);
378 	if (cp->co_flags & SMBO_GONE)
379 		return EINVAL;
380 	if ((flags & LK_TYPE_MASK) == 0)
381 		flags |= LK_EXCLUSIVE;
382 	if (smb_co_lockstatus(cp, td) == LK_EXCLUSIVE &&
383 	    (flags & LK_CANRECURSE) == 0) {
384 		SMBERROR("recursive lock for object %d\n", cp->co_level);
385 		return 0;
386 	}
387 	error = lockmgr(&cp->co_lock, flags);
388 	if (error == 0 && (cp->co_flags & SMBO_GONE)) {
389 		lockmgr(&cp->co_lock, LK_RELEASE);
390 		error = EINVAL;
391 	}
392 	return (error);
393 }
394 
395 void
396 smb_co_unlock(struct smb_connobj *cp, int flags, struct thread *td)
397 {
398 	lockmgr(&cp->co_lock, flags | LK_RELEASE);
399 }
400 
401 static void
402 smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
403 {
404 	KASSERT(smb_co_lockstatus(parent, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: parent not locked"));
405 	KASSERT(smb_co_lockstatus(child, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: child not locked"));
406 
407 	smb_co_ref(parent, curthread);
408 	SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
409 	child->co_parent = parent;
410 }
411 
412 /*
413  * Session implementation
414  */
415 
416 int
417 smb_vc_create(struct smb_vcspec *vcspec,
418 	struct smb_cred *scred, struct smb_vc **vcpp)
419 {
420 	struct smb_vc *vcp;
421 	struct thread *td = scred->scr_td;
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", td);
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 		free(vcp->vc_paddr, M_SONAME);
505 	if (vcp->vc_laddr)
506 		free(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 	free(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, struct thread *td)
533 {
534 	smb_co_ref(VCTOCP(vcp), td);
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, struct thread *td)
557 {
558 	return smb_co_lock(VCTOCP(vcp), flags, td);
559 }
560 
561 void
562 smb_vc_unlock(struct smb_vc *vcp, int flags, struct thread *td)
563 {
564 	smb_co_unlock(VCTOCP(vcp), flags, td);
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 thread *td = scred->scr_td;
617 	struct smb_share *ssp = NULL;
618 	int error;
619 
620 	*sspp = NULL;
621 	dp->scred = scred;
622 	SMBCO_FOREACH((struct smb_connobj*)ssp, VCTOCP(vcp)) {
623 		error = smb_share_lock(ssp, LK_EXCLUSIVE, td);
624 		if (error)
625 			continue;
626 		if (smb_vc_cmpshare(ssp, dp) == 0)
627 			break;
628 		smb_share_unlock(ssp, 0, td);
629 	}
630 	if (ssp) {
631 		smb_share_ref(ssp, td);
632 		*sspp = ssp;
633 		error = 0;
634 	} else
635 		error = ENOENT;
636 	return error;
637 }
638 
639 int
640 smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred)
641 {
642 
643 	return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
644 }
645 
646 /*
647  * Destroy VC to server, invalidate shares linked with it.
648  * Transport should be locked on entry.
649  */
650 int
651 smb_vc_disconnect(struct smb_vc *vcp)
652 {
653 
654 	smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT | SMBIOD_EV_SYNC, NULL);
655 	return 0;
656 }
657 
658 static char smb_emptypass[] = "";
659 
660 const char *
661 smb_vc_getpass(struct smb_vc *vcp)
662 {
663 	if (vcp->vc_pass)
664 		return vcp->vc_pass;
665 	return smb_emptypass;
666 }
667 
668 static int
669 smb_vc_getinfo(struct smb_vc *vcp, struct smb_vc_info *vip)
670 {
671 	bzero(vip, sizeof(struct smb_vc_info));
672 	vip->itype = SMB_INFO_VC;
673 	vip->usecount = vcp->obj.co_usecount;
674 	vip->uid = vcp->vc_uid;
675 	vip->gid = vcp->vc_grp;
676 	vip->mode = vcp->vc_mode;
677 	vip->flags = vcp->obj.co_flags;
678 	vip->sopt = vcp->vc_sopt;
679 	vip->iodstate = vcp->vc_iod->iod_state;
680 	bzero(&vip->sopt.sv_skey, sizeof(vip->sopt.sv_skey));
681 	snprintf(vip->srvname, sizeof(vip->srvname), "%s", vcp->vc_srvname);
682 	snprintf(vip->vcname, sizeof(vip->vcname), "%s", vcp->vc_username);
683 	return 0;
684 }
685 
686 u_short
687 smb_vc_nextmid(struct smb_vc *vcp)
688 {
689 	u_short r;
690 
691 	SMB_CO_LOCK(&vcp->obj);
692 	r = vcp->vc_mid++;
693 	SMB_CO_UNLOCK(&vcp->obj);
694 	return r;
695 }
696 
697 /*
698  * Share implementation
699  */
700 /*
701  * Allocate share structure and attach it to the given VC
702  * Connection expected to be locked on entry. Share will be returned
703  * in locked state.
704  */
705 int
706 smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
707 	struct smb_cred *scred, struct smb_share **sspp)
708 {
709 	struct smb_share *ssp;
710 	struct thread *td = scred->scr_td;
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", td);
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 	free(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, struct thread *td)
774 {
775 	smb_co_ref(SSTOCP(ssp), td);
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, struct thread *td)
798 {
799 	return smb_co_lock(SSTOCP(ssp), flags, td);
800 }
801 
802 void
803 smb_share_unlock(struct smb_share *ssp, int flags, struct thread *td)
804 {
805 	smb_co_unlock(SSTOCP(ssp), flags, td);
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 	snprintf(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_vc *vcp;
873 	struct smb_share *ssp;
874 	struct smb_vc_info vci;
875 	struct smb_share_info ssi;
876 	int error, itype;
877 
878 	KKASSERT(td->td_proc);
879 	ucred = td->td_proc->p_ucred;
880 
881 	smb_makescred(&scred, td, ucred);
882 	error = smb_sm_lockvclist(LK_SHARED, td);
883 	if (error)
884 		return error;
885 	SMBCO_FOREACH((struct smb_connobj*)vcp, &smb_vclist) {
886 		error = smb_vc_lock(vcp, LK_SHARED, td);
887 		if (error)
888 			continue;
889 		smb_vc_getinfo(vcp, &vci);
890 		error = SYSCTL_OUT(req, &vci, sizeof(struct smb_vc_info));
891 		if (error) {
892 			smb_vc_unlock(vcp, 0, td);
893 			break;
894 		}
895 		SMBCO_FOREACH((struct smb_connobj*)ssp, VCTOCP(vcp)) {
896 			error = smb_share_lock(ssp, LK_SHARED, td);
897 			if (error) {
898 				error = 0;
899 				continue;
900 			}
901 			smb_share_getinfo(ssp, &ssi);
902 			smb_share_unlock(ssp, 0, td);
903 			error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info));
904 			if (error)
905 				break;
906 		}
907 		smb_vc_unlock(vcp, 0, td);
908 		if (error)
909 			break;
910 	}
911 	if (!error) {
912 		itype = SMB_INFO_NONE;
913 		error = SYSCTL_OUT(req, &itype, sizeof(itype));
914 	}
915 	smb_sm_unlockvclist(td);
916 	return error;
917 }
918