Deleted Added
sdiff udiff text old ( 176518 ) new ( 176559 )
full compact
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: head/sys/netsmb/smb_conn.c 176518 2008-02-24 16:26:52Z attilio $");
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, struct thread *td);
67static void smb_co_done(struct smb_connobj *cp);
68static int smb_co_lockstatus(struct smb_connobj *cp, struct thread *td);
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", curthread);
86 smb_co_unlock(&smb_vclist, 0, curthread);
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, 0);
100 smb_co_done(&smb_vclist);
101 return 0;
102}
103
104static int
105smb_sm_lockvclist(int flags, struct thread *td)
106{
107
108 return smb_co_lock(&smb_vclist, flags | LK_CANRECURSE, td);
109}
110
111static void
112smb_sm_unlockvclist(struct thread *td)
113{
114
115 smb_co_unlock(&smb_vclist, LK_RELEASE, td);
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 thread *td = scred->scr_td;
123 struct smb_connobj *scp;
124 struct smb_vc *vcp;
125 int exact = 1;
126 int error;
127
128 vcspec->shspec = shspec;
129 error = ENOENT;
130 vcp = NULL;
131 SMBCO_FOREACH(scp, &smb_vclist) {
132 vcp = (struct smb_vc *)scp;
133 error = smb_vc_lock(vcp, LK_EXCLUSIVE, td);
134 if (error)
135 continue;
136
137 if ((vcp->obj.co_flags & SMBV_PRIVATE) ||
138 !CONNADDREQ(vcp->vc_paddr, vcspec->sap) ||
139 strcmp(vcp->vc_username, vcspec->username) != 0)
140 goto err1;
141 if (vcspec->owner != SMBM_ANY_OWNER) {
142 if (vcp->vc_uid != vcspec->owner)
143 goto err1;
144 } else
145 exact = 0;
146 if (vcspec->group != SMBM_ANY_GROUP) {
147 if (vcp->vc_grp != vcspec->group)
148 goto err1;
149 } else
150 exact = 0;
151 if (vcspec->mode & SMBM_EXACT) {
152 if (!exact || (vcspec->mode & SMBM_MASK) !=
153 vcp->vc_mode)
154 goto err1;
155 }
156 if (smb_vc_access(vcp, scred, vcspec->mode) != 0)
157 goto err1;
158 vcspec->ssp = NULL;
159 if (shspec) {
160 error = (int)smb_vc_lookupshare(vcp, shspec, scred,
161 &vcspec->ssp);
162 if (error)
163 goto fail;
164 }
165 error = 0;
166 break;
167 err1:
168 error = 1;
169 fail:
170 smb_vc_unlock(vcp, 0, td);
171 }
172 if (vcp) {
173 smb_vc_ref(vcp);
174 *vcpp = vcp;
175 }
176 return (error);
177}
178
179int
180smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
181 struct smb_cred *scred, struct smb_vc **vcpp)
182{
183 struct thread *td = scred->scr_td;
184 struct smb_vc *vcp;
185 struct smb_share *ssp = NULL;
186 int error;
187
188 *vcpp = vcp = NULL;
189
190 error = smb_sm_lockvclist(LK_EXCLUSIVE, td);
191 if (error)
192 return error;
193 error = smb_sm_lookupint(vcspec, shspec, scred, vcpp);
194 if (error == 0 || (vcspec->flags & SMBV_CREATE) == 0) {
195 smb_sm_unlockvclist(td);
196 return error;
197 }
198 error = smb_sm_lookupint(vcspec, NULL, scred, &vcp);
199 if (error) {
200 error = smb_vc_create(vcspec, scred, &vcp);
201 if (error)
202 goto out;
203 error = smb_vc_connect(vcp, scred);
204 if (error)
205 goto out;
206 }
207 if (shspec == NULL)
208 goto out;
209 error = smb_share_create(vcp, shspec, scred, &ssp);
210 if (error)
211 goto out;
212 error = smb_smb_treeconnect(ssp, scred);
213 if (error == 0)
214 vcspec->ssp = ssp;
215 else
216 smb_share_put(ssp, scred);
217out:
218 smb_sm_unlockvclist(td);
219 if (error == 0)
220 *vcpp = vcp;
221 else if (vcp)
222 smb_vc_put(vcp, scred);
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 struct thread *td)
232{
233 SLIST_INIT(&cp->co_children);
234 smb_sl_init(&cp->co_interlock, ilockname);
235 lockinit(&cp->co_lock, PZERO, lockname, 0, 0);
236 cp->co_level = level;
237 cp->co_usecount = 1;
238 if (smb_co_lock(cp, LK_EXCLUSIVE, td) != 0)
239 panic("smb_co_init: lock failed");
240}
241
242static void
243smb_co_done(struct smb_connobj *cp)
244{
245 smb_sl_destroy(&cp->co_interlock);
246 lockmgr(&cp->co_lock, LK_RELEASE, 0);
247 lockdestroy(&cp->co_lock);
248}
249
250static void
251smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred)
252{
253 struct smb_connobj *parent;
254
255 if (cp->co_gone)
256 cp->co_gone(cp, scred);
257 parent = cp->co_parent;
258 if (parent) {
259 smb_co_lock(parent, LK_EXCLUSIVE, scred->scr_td);
260 SLIST_REMOVE(&parent->co_children, cp, smb_connobj, co_next);
261 smb_co_put(parent, scred);
262 }
263 if (cp->co_free)
264 cp->co_free(cp);
265}
266
267void
268smb_co_ref(struct smb_connobj *cp)
269{
270
271 SMB_CO_LOCK(cp);
272 cp->co_usecount++;
273 SMB_CO_UNLOCK(cp);
274}
275
276void
277smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
278{
279
280 SMB_CO_LOCK(cp);
281 if (cp->co_usecount > 1) {
282 cp->co_usecount--;
283 SMB_CO_UNLOCK(cp);
284 return;
285 }
286 if (cp->co_usecount == 0) {
287 SMBERROR("negative use_count for object %d", cp->co_level);
288 SMB_CO_UNLOCK(cp);
289 return;
290 }
291 cp->co_usecount--;
292 cp->co_flags |= SMBO_GONE;
293
294 lockmgr(&cp->co_lock, LK_DRAIN | LK_INTERLOCK, &cp->co_interlock);
295 smb_co_gone(cp, scred);
296}
297
298int
299smb_co_get(struct smb_connobj *cp, int flags, struct smb_cred *scred)
300{
301 int error;
302
303 if ((flags & LK_INTERLOCK) == 0)
304 SMB_CO_LOCK(cp);
305 cp->co_usecount++;
306 error = smb_co_lock(cp, flags | LK_INTERLOCK, scred->scr_td);
307 if (error) {
308 SMB_CO_LOCK(cp);
309 cp->co_usecount--;
310 SMB_CO_UNLOCK(cp);
311 return error;
312 }
313 return 0;
314}
315
316void
317smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
318{
319
320 SMB_CO_LOCK(cp);
321 if (cp->co_usecount > 1) {
322 cp->co_usecount--;
323 } else if (cp->co_usecount == 1) {
324 cp->co_usecount--;
325 cp->co_flags |= SMBO_GONE;
326 } else {
327 SMBERROR("negative usecount");
328 }
329 lockmgr(&cp->co_lock, LK_RELEASE | LK_INTERLOCK, &cp->co_interlock);
330 if ((cp->co_flags & SMBO_GONE) == 0)
331 return;
332 lockmgr(&cp->co_lock, LK_DRAIN, NULL);
333 smb_co_gone(cp, scred);
334}
335
336int
337smb_co_lockstatus(struct smb_connobj *cp, struct thread *td)
338{
339 return lockstatus(&cp->co_lock, td);
340}
341
342int
343smb_co_lock(struct smb_connobj *cp, int flags, struct thread *td)
344{
345
346 if (cp->co_flags & SMBO_GONE)
347 return EINVAL;
348 if ((flags & LK_TYPE_MASK) == 0)
349 flags |= LK_EXCLUSIVE;
350 if (smb_co_lockstatus(cp, td) == LK_EXCLUSIVE &&
351 (flags & LK_CANRECURSE) == 0) {
352 SMBERROR("recursive lock for object %d\n", cp->co_level);
353 return 0;
354 }
355 return lockmgr(&cp->co_lock, flags, &cp->co_interlock);
356}
357
358void
359smb_co_unlock(struct smb_connobj *cp, int flags, struct thread *td)
360{
361 (void)lockmgr(&cp->co_lock, flags | LK_RELEASE, &cp->co_interlock);
362}
363
364static void
365smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
366{
367 KASSERT(smb_co_lockstatus(parent, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: parent not locked"));
368 KASSERT(smb_co_lockstatus(child, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: child not locked"));
369
370 smb_co_ref(parent);
371 SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
372 child->co_parent = parent;
373}
374
375/*
376 * Session implementation
377 */
378
379int
380smb_vc_create(struct smb_vcspec *vcspec,
381 struct smb_cred *scred, struct smb_vc **vcpp)
382{
383 struct smb_vc *vcp;
384 struct thread *td = scred->scr_td;
385 struct ucred *cred = scred->scr_cred;
386 uid_t uid = vcspec->owner;
387 gid_t gid = vcspec->group;
388 uid_t realuid = cred->cr_uid;
389 char *domain = vcspec->domain;
390 int error, isroot;
391
392 isroot = smb_suser(cred) == 0;
393 /*
394 * Only superuser can create VCs with different uid and gid
395 */
396 if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
397 return EPERM;
398 if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
399 return EPERM;
400
401 vcp = smb_zmalloc(sizeof(*vcp), M_SMBCONN, M_WAITOK);
402 smb_co_init(VCTOCP(vcp), SMBL_VC, "smb_vc ilock", "smb_vc", td);
403 vcp->obj.co_free = smb_vc_free;
404 vcp->obj.co_gone = smb_vc_gone;
405 vcp->vc_number = smb_vcnext++;
406 vcp->vc_timo = SMB_DEFRQTIMO;
407 vcp->vc_smbuid = SMB_UID_UNKNOWN;
408 vcp->vc_mode = vcspec->rights & SMBM_MASK;
409 vcp->obj.co_flags = vcspec->flags & (SMBV_PRIVATE | SMBV_SINGLESHARE);
410 vcp->vc_tdesc = &smb_tran_nbtcp_desc;
411 vcp->vc_seqno = 0;
412 vcp->vc_mackey = NULL;
413 vcp->vc_mackeylen = 0;
414
415 if (uid == SMBM_ANY_OWNER)
416 uid = realuid;
417 if (gid == SMBM_ANY_GROUP)
418 gid = cred->cr_groups[0];
419 vcp->vc_uid = uid;
420 vcp->vc_grp = gid;
421
422 smb_sl_init(&vcp->vc_stlock, "vcstlock");
423 error = ENOMEM;
424
425 vcp->vc_paddr = sodupsockaddr(vcspec->sap, M_WAITOK);
426 if (vcp->vc_paddr == NULL)
427 goto fail;
428 vcp->vc_laddr = sodupsockaddr(vcspec->lap, M_WAITOK);
429 if (vcp->vc_laddr == NULL)
430 goto fail;
431 vcp->vc_pass = smb_strdup(vcspec->pass);
432 if (vcp->vc_pass == NULL)
433 goto fail;
434 vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain :
435 "NODOMAIN");
436 if (vcp->vc_domain == NULL)
437 goto fail;
438 vcp->vc_srvname = smb_strdup(vcspec->srvname);
439 if (vcp->vc_srvname == NULL)
440 goto fail;
441 vcp->vc_username = smb_strdup(vcspec->username);
442 if (vcp->vc_username == NULL)
443 goto fail;
444 error = (int)iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower);
445 if (error)
446 goto fail;
447 error = (int)iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper);
448 if (error)
449 goto fail;
450 if (vcspec->servercs[0]) {
451 error = (int)iconv_open(vcspec->servercs, vcspec->localcs,
452 &vcp->vc_toserver);
453 if (error)
454 goto fail;
455 error = (int)iconv_open(vcspec->localcs, vcspec->servercs,
456 &vcp->vc_tolocal);
457 if (error)
458 goto fail;
459 }
460 error = (int)smb_iod_create(vcp);
461 if (error)
462 goto fail;
463 *vcpp = vcp;
464 smb_co_addchild(&smb_vclist, VCTOCP(vcp));
465 return (0);
466
467 fail:
468 smb_vc_put(vcp, scred);
469 return (error);
470}
471
472static void
473smb_vc_free(struct smb_connobj *cp)
474{
475 struct smb_vc *vcp = CPTOVC(cp);
476
477 if (vcp->vc_iod)
478 smb_iod_destroy(vcp->vc_iod);
479 SMB_STRFREE(vcp->vc_username);
480 SMB_STRFREE(vcp->vc_srvname);
481 SMB_STRFREE(vcp->vc_pass);
482 SMB_STRFREE(vcp->vc_domain);
483 if (vcp->vc_mackey)
484 free(vcp->vc_mackey, M_SMBTEMP);
485 if (vcp->vc_paddr)
486 free(vcp->vc_paddr, M_SONAME);
487 if (vcp->vc_laddr)
488 free(vcp->vc_laddr, M_SONAME);
489 if (vcp->vc_tolower)
490 iconv_close(vcp->vc_tolower);
491 if (vcp->vc_toupper)
492 iconv_close(vcp->vc_toupper);
493 if (vcp->vc_tolocal)
494 iconv_close(vcp->vc_tolocal);
495 if (vcp->vc_toserver)
496 iconv_close(vcp->vc_toserver);
497 smb_co_done(VCTOCP(vcp));
498 smb_sl_destroy(&vcp->vc_stlock);
499 free(vcp, M_SMBCONN);
500}
501
502/*
503 * Called when use count of VC dropped to zero.
504 * VC should be locked on enter with LK_DRAIN.
505 */
506static void
507smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred)
508{
509 struct smb_vc *vcp = CPTOVC(cp);
510
511 smb_vc_disconnect(vcp);
512}
513
514void
515smb_vc_ref(struct smb_vc *vcp)
516{
517 smb_co_ref(VCTOCP(vcp));
518}
519
520void
521smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
522{
523 smb_co_rele(VCTOCP(vcp), scred);
524}
525
526int
527smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred)
528{
529 return smb_co_get(VCTOCP(vcp), flags, scred);
530}
531
532void
533smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
534{
535 smb_co_put(VCTOCP(vcp), scred);
536}
537
538int
539smb_vc_lock(struct smb_vc *vcp, int flags, struct thread *td)
540{
541 return smb_co_lock(VCTOCP(vcp), flags, td);
542}
543
544void
545smb_vc_unlock(struct smb_vc *vcp, int flags, struct thread *td)
546{
547 smb_co_unlock(VCTOCP(vcp), flags, td);
548}
549
550int
551smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode)
552{
553 struct ucred *cred = scred->scr_cred;
554
555 if (smb_suser(cred) == 0 || cred->cr_uid == vcp->vc_uid)
556 return 0;
557 mode >>= 3;
558 if (!groupmember(vcp->vc_grp, cred))
559 mode >>= 3;
560 return (vcp->vc_mode & mode) == mode ? 0 : EACCES;
561}
562
563static int
564smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp)
565{
566 int exact = 1;
567
568 if (strcmp(ssp->ss_name, dp->name) != 0)
569 return 1;
570 if (dp->owner != SMBM_ANY_OWNER) {
571 if (ssp->ss_uid != dp->owner)
572 return 1;
573 } else
574 exact = 0;
575 if (dp->group != SMBM_ANY_GROUP) {
576 if (ssp->ss_grp != dp->group)
577 return 1;
578 } else
579 exact = 0;
580
581 if (dp->mode & SMBM_EXACT) {
582 if (!exact)
583 return 1;
584 return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1;
585 }
586 if (smb_share_access(ssp, dp->scred, dp->mode) != 0)
587 return 1;
588 return 0;
589}
590
591/*
592 * Lookup share in the given VC. Share referenced and locked on return.
593 * VC expected to be locked on entry and will be left locked on exit.
594 */
595int
596smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp,
597 struct smb_cred *scred, struct smb_share **sspp)
598{
599 struct thread *td = scred->scr_td;
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, td);
609 if (error)
610 continue;
611 if (smb_vc_cmpshare(ssp, dp) == 0)
612 break;
613 smb_share_unlock(ssp, 0, td);
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 thread *td = scred->scr_td;
696 struct ucred *cred = scred->scr_cred;
697 uid_t realuid = cred->cr_uid;
698 uid_t uid = shspec->owner;
699 gid_t gid = shspec->group;
700 int error, isroot;
701
702 isroot = smb_suser(cred) == 0;
703 /*
704 * Only superuser can create shares with different uid and gid
705 */
706 if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
707 return EPERM;
708 if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
709 return EPERM;
710 error = smb_vc_lookupshare(vcp, shspec, scred, &ssp);
711 if (!error) {
712 smb_share_put(ssp, scred);
713 return EEXIST;
714 }
715 if (uid == SMBM_ANY_OWNER)
716 uid = realuid;
717 if (gid == SMBM_ANY_GROUP)
718 gid = cred->cr_groups[0];
719 ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK);
720 smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss ilock", "smbss", td);
721 ssp->obj.co_free = smb_share_free;
722 ssp->obj.co_gone = smb_share_gone;
723 smb_sl_init(&ssp->ss_stlock, "ssstlock");
724 ssp->ss_name = smb_strdup(shspec->name);
725 if (shspec->pass && shspec->pass[0])
726 ssp->ss_pass = smb_strdup(shspec->pass);
727 ssp->ss_type = shspec->stype;
728 ssp->ss_tid = SMB_TID_UNKNOWN;
729 ssp->ss_uid = uid;
730 ssp->ss_grp = gid;
731 ssp->ss_mode = shspec->rights & SMBM_MASK;
732 smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp));
733 *sspp = ssp;
734 return 0;
735}
736
737static void
738smb_share_free(struct smb_connobj *cp)
739{
740 struct smb_share *ssp = CPTOSS(cp);
741
742 SMB_STRFREE(ssp->ss_name);
743 SMB_STRFREE(ssp->ss_pass);
744 smb_sl_destroy(&ssp->ss_stlock);
745 smb_co_done(SSTOCP(ssp));
746 free(ssp, M_SMBCONN);
747}
748
749static void
750smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred)
751{
752 struct smb_share *ssp = CPTOSS(cp);
753
754 smb_smb_treedisconnect(ssp, scred);
755}
756
757void
758smb_share_ref(struct smb_share *ssp)
759{
760 smb_co_ref(SSTOCP(ssp));
761}
762
763void
764smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
765{
766 smb_co_rele(SSTOCP(ssp), scred);
767}
768
769int
770smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred)
771{
772 return smb_co_get(SSTOCP(ssp), flags, scred);
773}
774
775void
776smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
777{
778 smb_co_put(SSTOCP(ssp), scred);
779}
780
781int
782smb_share_lock(struct smb_share *ssp, int flags, struct thread *td)
783{
784 return smb_co_lock(SSTOCP(ssp), flags, td);
785}
786
787void
788smb_share_unlock(struct smb_share *ssp, int flags, struct thread *td)
789{
790 smb_co_unlock(SSTOCP(ssp), flags, td);
791}
792
793int
794smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode)
795{
796 struct ucred *cred = scred->scr_cred;
797
798 if (smb_suser(cred) == 0 || cred->cr_uid == ssp->ss_uid)
799 return 0;
800 mode >>= 3;
801 if (!groupmember(ssp->ss_grp, cred))
802 mode >>= 3;
803 return (ssp->ss_mode & mode) == mode ? 0 : EACCES;
804}
805
806void
807smb_share_invalidate(struct smb_share *ssp)
808{
809 ssp->ss_tid = SMB_TID_UNKNOWN;
810}
811
812int
813smb_share_valid(struct smb_share *ssp)
814{
815 return ssp->ss_tid != SMB_TID_UNKNOWN &&
816 ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid;
817}
818
819const char*
820smb_share_getpass(struct smb_share *ssp)
821{
822 struct smb_vc *vcp;
823
824 if (ssp->ss_pass)
825 return ssp->ss_pass;
826 vcp = SSTOVC(ssp);
827 if (vcp->vc_pass)
828 return vcp->vc_pass;
829 return smb_emptypass;
830}
831
832static int
833smb_share_getinfo(struct smb_share *ssp, struct smb_share_info *sip)
834{
835 bzero(sip, sizeof(struct smb_share_info));
836 sip->itype = SMB_INFO_SHARE;
837 sip->usecount = ssp->obj.co_usecount;
838 sip->tid = ssp->ss_tid;
839 sip->type= ssp->ss_type;
840 sip->uid = ssp->ss_uid;
841 sip->gid = ssp->ss_grp;
842 sip->mode= ssp->ss_mode;
843 sip->flags = ssp->obj.co_flags;
844 snprintf(sip->sname, sizeof(sip->sname), "%s", ssp->ss_name);
845 return 0;
846}
847
848/*
849 * Dump an entire tree into sysctl call
850 */
851static int
852smb_sysctl_treedump(SYSCTL_HANDLER_ARGS)
853{
854 struct thread *td = req->td;
855 struct smb_cred scred;
856 struct smb_connobj *scp1, *scp2;
857 struct smb_vc *vcp;
858 struct smb_share *ssp;
859 struct smb_vc_info vci;
860 struct smb_share_info ssi;
861 int error, itype;
862
863 smb_makescred(&scred, td, td->td_ucred);
864 error = sysctl_wire_old_buffer(req, 0);
865 if (error)
866 return (error);
867 error = smb_sm_lockvclist(LK_SHARED, td);
868 if (error)
869 return error;
870 SMBCO_FOREACH(scp1, &smb_vclist) {
871 vcp = (struct smb_vc *)scp1;
872 error = smb_vc_lock(vcp, LK_SHARED, td);
873 if (error)
874 continue;
875 smb_vc_getinfo(vcp, &vci);
876 error = SYSCTL_OUT(req, &vci, sizeof(struct smb_vc_info));
877 if (error) {
878 smb_vc_unlock(vcp, 0, td);
879 break;
880 }
881 SMBCO_FOREACH(scp2, VCTOCP(vcp)) {
882 ssp = (struct smb_share *)scp2;
883 error = smb_share_lock(ssp, LK_SHARED, td);
884 if (error) {
885 error = 0;
886 continue;
887 }
888 smb_share_getinfo(ssp, &ssi);
889 smb_share_unlock(ssp, 0, td);
890 error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info));
891 if (error)
892 break;
893 }
894 smb_vc_unlock(vcp, 0, td);
895 if (error)
896 break;
897 }
898 if (!error) {
899 itype = SMB_INFO_NONE;
900 error = SYSCTL_OUT(req, &itype, sizeof(itype));
901 }
902 smb_sm_unlockvclist(td);
903 return error;
904}