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