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