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