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