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