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