Remove all remaining support for LK_DRAIN lockmgr locks. LK_DRAIN was a
[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.13 2006/04/23 02:41:14 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, NULL, scred->scr_td);
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, NULL, td);
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, NULL, scred->scr_td);
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         struct thread *td = scred->scr_td;
343
344         SMB_CO_LOCK(cp);
345         if (cp->co_usecount > 1) {
346                 cp->co_usecount--;
347                 SMB_CO_UNLOCK(cp);
348                 lockmgr(&cp->co_lock, LK_RELEASE, NULL, td);
349                 return;
350         }
351         if (cp->co_usecount <= 0) {
352                 SMBERROR("negative use_count for object %d", cp->co_level);
353                 SMB_CO_UNLOCK(cp);
354                 return;
355         }
356         cp->co_usecount = 0;
357         if ((cp->co_flags & SMBO_GONE) == 0) {
358                 cp->co_flags |= SMBO_GONE;
359                 SMB_CO_UNLOCK(cp);
360                 lockmgr(&cp->co_lock, LK_RELEASE, NULL, td);
361                 lockmgr(&cp->co_lock, LK_EXCLUSIVE, NULL, td);
362                 smb_co_gone(cp, scred);
363         } else {
364                 SMB_CO_UNLOCK(cp);
365                 lockmgr(&cp->co_lock, LK_RELEASE, NULL, td);
366                 wakeup(&cp->co_lock);
367         }
368 }
369
370 int
371 smb_co_lockstatus(struct smb_connobj *cp, struct thread *td)
372 {
373         return lockstatus(&cp->co_lock, td);
374 }
375
376 int
377 smb_co_lock(struct smb_connobj *cp, int flags, struct thread *td)
378 {
379         int error;
380
381         KKASSERT(cp->co_usecount > 0);
382         if (cp->co_flags & SMBO_GONE)
383                 return EINVAL;
384         if ((flags & LK_TYPE_MASK) == 0)
385                 flags |= LK_EXCLUSIVE;
386         if (smb_co_lockstatus(cp, td) == LK_EXCLUSIVE && 
387             (flags & LK_CANRECURSE) == 0) {
388                 SMBERROR("recursive lock for object %d\n", cp->co_level);
389                 return 0;
390         }
391         error = lockmgr(&cp->co_lock, flags, NULL, td);
392         if (error == 0 && (cp->co_flags & SMBO_GONE)) {
393                 lockmgr(&cp->co_lock, LK_RELEASE, NULL, td);
394                 error = EINVAL;
395         }
396         return (error);
397 }
398
399 void
400 smb_co_unlock(struct smb_connobj *cp, int flags, struct thread *td)
401 {
402         lockmgr(&cp->co_lock, flags | LK_RELEASE, NULL, td);
403 }
404
405 static void
406 smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
407 {
408         KASSERT(smb_co_lockstatus(parent, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: parent not locked"));
409         KASSERT(smb_co_lockstatus(child, curthread) == LK_EXCLUSIVE, ("smb_co_addchild: child not locked"));
410
411         smb_co_ref(parent, curthread);
412         SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
413         child->co_parent = parent;
414 }
415
416 /*
417  * Session implementation
418  */
419
420 int
421 smb_vc_create(struct smb_vcspec *vcspec,
422         struct smb_cred *scred, struct smb_vc **vcpp)
423 {
424         struct smb_vc *vcp;
425         struct thread *td = scred->scr_td;
426         struct ucred *cred = scred->scr_cred;
427         uid_t uid = vcspec->owner;
428         gid_t gid = vcspec->group;
429         uid_t realuid = cred->cr_uid;
430         char *domain = vcspec->domain;
431         int error, isroot;
432
433         isroot = smb_suser(cred) == 0;
434         /*
435          * Only superuser can create VCs with different uid and gid
436          */
437         if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
438                 return EPERM;
439         if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
440                 return EPERM;
441
442         vcp = smb_zmalloc(sizeof(*vcp), M_SMBCONN, M_WAITOK);
443         smb_co_init(VCTOCP(vcp), SMBL_VC, "smb_vc", td);
444         vcp->obj.co_free = smb_vc_free;
445         vcp->obj.co_gone = smb_vc_gone;
446         vcp->vc_number = smb_vcnext++;
447         vcp->vc_timo = SMB_DEFRQTIMO;
448         vcp->vc_smbuid = SMB_UID_UNKNOWN;
449         vcp->vc_mode = vcspec->rights & SMBM_MASK;
450         vcp->obj.co_flags = vcspec->flags & (SMBV_PRIVATE | SMBV_SINGLESHARE);
451         vcp->vc_tdesc = &smb_tran_nbtcp_desc;
452
453         if (uid == SMBM_ANY_OWNER)
454                 uid = realuid;
455         if (gid == SMBM_ANY_GROUP)
456                 gid = cred->cr_groups[0];
457         vcp->vc_uid = uid;
458         vcp->vc_grp = gid;
459
460         smb_sl_init(&vcp->vc_stlock, "vcstlock");
461         error = 0;
462         itry {
463                 vcp->vc_paddr = dup_sockaddr(vcspec->sap);
464                 ierror(vcp->vc_paddr == NULL, ENOMEM);
465
466                 vcp->vc_laddr = dup_sockaddr(vcspec->lap);
467                 ierror(vcp->vc_laddr == NULL, ENOMEM);
468
469                 ierror((vcp->vc_pass = smb_strdup(vcspec->pass)) == NULL, ENOMEM);
470
471                 vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain : "NODOMAIN");
472                 ierror(vcp->vc_domain == NULL, ENOMEM);
473
474                 ierror((vcp->vc_srvname = smb_strdup(vcspec->srvname)) == NULL, ENOMEM);
475                 ierror((vcp->vc_username = smb_strdup(vcspec->username)) == NULL, ENOMEM);
476
477                 ithrow(iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower));
478                 ithrow(iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper));
479                 if (vcspec->servercs[0]) {
480                         ithrow(iconv_open(vcspec->servercs, vcspec->localcs,
481                             &vcp->vc_toserver));
482                         ithrow(iconv_open(vcspec->localcs, vcspec->servercs,
483                             &vcp->vc_tolocal));
484                 }
485
486                 ithrow(smb_iod_create(vcp));
487                 *vcpp = vcp;
488                 smb_co_addchild(&smb_vclist, VCTOCP(vcp));
489         } icatch(error) {
490                 smb_vc_put(vcp, scred);
491         } ifinally {
492         } iendtry;
493         return error;
494 }
495
496 static void
497 smb_vc_free(struct smb_connobj *cp)
498 {
499         struct smb_vc *vcp = CPTOVC(cp);
500
501         if (vcp->vc_iod)
502                 smb_iod_destroy(vcp->vc_iod);
503         SMB_STRFREE(vcp->vc_username);
504         SMB_STRFREE(vcp->vc_srvname);
505         SMB_STRFREE(vcp->vc_pass);
506         SMB_STRFREE(vcp->vc_domain);
507         if (vcp->vc_paddr)
508                 free(vcp->vc_paddr, M_SONAME);
509         if (vcp->vc_laddr)
510                 free(vcp->vc_laddr, M_SONAME);
511         if (vcp->vc_tolower)
512                 iconv_close(vcp->vc_tolower);
513         if (vcp->vc_toupper)
514                 iconv_close(vcp->vc_toupper);
515         if (vcp->vc_tolocal)
516                 iconv_close(vcp->vc_tolocal);
517         if (vcp->vc_toserver)
518                 iconv_close(vcp->vc_toserver);
519         smb_co_done(VCTOCP(vcp));
520         smb_sl_destroy(&vcp->vc_stlock);
521         free(vcp, M_SMBCONN);
522 }
523
524 /*
525  * Called when use count of VC dropped to zero.
526  */
527 static void
528 smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred)
529 {
530         struct smb_vc *vcp = CPTOVC(cp);
531
532         smb_vc_disconnect(vcp);
533 }
534
535 void
536 smb_vc_ref(struct smb_vc *vcp, struct thread *td)
537 {
538         smb_co_ref(VCTOCP(vcp), td);
539 }
540
541 void
542 smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
543 {
544         smb_co_rele(VCTOCP(vcp), scred);
545 }
546
547 int
548 smb_vc_get(struct smb_vc *vcp, int flags, struct smb_cred *scred)
549 {
550         return smb_co_get(VCTOCP(vcp), flags, scred);
551 }
552
553 void
554 smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
555 {
556         smb_co_put(VCTOCP(vcp), scred);
557 }
558
559 int
560 smb_vc_lock(struct smb_vc *vcp, int flags, struct thread *td)
561 {
562         return smb_co_lock(VCTOCP(vcp), flags, td);
563 }
564
565 void
566 smb_vc_unlock(struct smb_vc *vcp, int flags, struct thread *td)
567 {
568         smb_co_unlock(VCTOCP(vcp), flags, td);
569 }
570
571 int
572 smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode)
573 {
574         struct ucred *cred = scred->scr_cred;
575
576         if (smb_suser(cred) == 0 || cred->cr_uid == vcp->vc_uid)
577                 return 0;
578         mode >>= 3;
579         if (!groupmember(vcp->vc_grp, cred))
580                 mode >>= 3;
581         return (vcp->vc_mode & mode) == mode ? 0 : EACCES;
582 }
583
584 static int
585 smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp)
586 {
587         int exact = 1;
588
589         if (strcmp(ssp->ss_name, dp->name) != 0)
590                 return 1;
591         if (dp->owner != SMBM_ANY_OWNER) {
592                 if (ssp->ss_uid != dp->owner)
593                         return 1;
594         } else
595                 exact = 0;
596         if (dp->group != SMBM_ANY_GROUP) {
597                 if (ssp->ss_grp != dp->group)
598                         return 1;
599         } else
600                 exact = 0;
601
602         if (dp->mode & SMBM_EXACT) {
603                 if (!exact)
604                         return 1;
605                 return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1;
606         }
607         if (smb_share_access(ssp, dp->scred, dp->mode) != 0)
608                 return 1;
609         return 0;
610 }
611
612 /*
613  * Lookup share in the given VC. Share referenced and locked on return.
614  * VC expected to be locked on entry and will be left locked on exit.
615  */
616 int
617 smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp,
618         struct smb_cred *scred, struct smb_share **sspp)
619 {
620         struct thread *td = scred->scr_td;
621         struct smb_share *ssp = NULL;
622         int error;
623
624         *sspp = NULL;
625         dp->scred = scred;
626         SMBCO_FOREACH((struct smb_connobj*)ssp, VCTOCP(vcp)) {
627                 error = smb_share_lock(ssp, LK_EXCLUSIVE, td);
628                 if (error)
629                         continue;
630                 if (smb_vc_cmpshare(ssp, dp) == 0)
631                         break;
632                 smb_share_unlock(ssp, 0, td);
633         }
634         if (ssp) {
635                 smb_share_ref(ssp, td);
636                 *sspp = ssp;
637                 error = 0;
638         } else
639                 error = ENOENT;
640         return error;
641 }
642
643 int
644 smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred)
645 {
646
647         return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
648 }
649
650 /*
651  * Destroy VC to server, invalidate shares linked with it.
652  * Transport should be locked on entry.
653  */
654 int
655 smb_vc_disconnect(struct smb_vc *vcp)
656 {
657
658         smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT | SMBIOD_EV_SYNC, NULL);
659         return 0;
660 }
661
662 static char smb_emptypass[] = "";
663
664 const char *
665 smb_vc_getpass(struct smb_vc *vcp)
666 {
667         if (vcp->vc_pass)
668                 return vcp->vc_pass;
669         return smb_emptypass;
670 }
671
672 static int
673 smb_vc_getinfo(struct smb_vc *vcp, struct smb_vc_info *vip)
674 {
675         bzero(vip, sizeof(struct smb_vc_info));
676         vip->itype = SMB_INFO_VC;
677         vip->usecount = vcp->obj.co_usecount;
678         vip->uid = vcp->vc_uid;
679         vip->gid = vcp->vc_grp;
680         vip->mode = vcp->vc_mode;
681         vip->flags = vcp->obj.co_flags;
682         vip->sopt = vcp->vc_sopt;
683         vip->iodstate = vcp->vc_iod->iod_state;
684         bzero(&vip->sopt.sv_skey, sizeof(vip->sopt.sv_skey));
685         snprintf(vip->srvname, sizeof(vip->srvname), "%s", vcp->vc_srvname);
686         snprintf(vip->vcname, sizeof(vip->vcname), "%s", vcp->vc_username);
687         return 0;
688 }
689
690 u_short
691 smb_vc_nextmid(struct smb_vc *vcp)
692 {
693         u_short r;
694
695         SMB_CO_LOCK(&vcp->obj);
696         r = vcp->vc_mid++;
697         SMB_CO_UNLOCK(&vcp->obj);
698         return r;
699 }
700
701 /*
702  * Share implementation
703  */
704 /*
705  * Allocate share structure and attach it to the given VC
706  * Connection expected to be locked on entry. Share will be returned
707  * in locked state.
708  */
709 int
710 smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
711         struct smb_cred *scred, struct smb_share **sspp)
712 {
713         struct smb_share *ssp;
714         struct thread *td = scred->scr_td;
715         struct ucred *cred = scred->scr_cred;
716         uid_t realuid = cred->cr_uid;
717         uid_t uid = shspec->owner;
718         gid_t gid = shspec->group;
719         int error, isroot;
720
721         isroot = smb_suser(cred) == 0;
722         /*
723          * Only superuser can create shares with different uid and gid
724          */
725         if (uid != SMBM_ANY_OWNER && uid != realuid && !isroot)
726                 return EPERM;
727         if (gid != SMBM_ANY_GROUP && !groupmember(gid, cred) && !isroot)
728                 return EPERM;
729         error = smb_vc_lookupshare(vcp, shspec, scred, &ssp);
730         if (!error) {
731                 smb_share_put(ssp, scred);
732                 return EEXIST;
733         }
734         if (uid == SMBM_ANY_OWNER)
735                 uid = realuid;
736         if (gid == SMBM_ANY_GROUP)
737                 gid = cred->cr_groups[0];
738         ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK);
739         smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss", td);
740         ssp->obj.co_free = smb_share_free;
741         ssp->obj.co_gone = smb_share_gone;
742         smb_sl_init(&ssp->ss_stlock, "ssstlock");
743         ssp->ss_name = smb_strdup(shspec->name);
744         if (shspec->pass && shspec->pass[0])
745                 ssp->ss_pass = smb_strdup(shspec->pass);
746         ssp->ss_type = shspec->stype;
747         ssp->ss_tid = SMB_TID_UNKNOWN;
748         ssp->ss_uid = uid;
749         ssp->ss_grp = gid;
750         ssp->ss_mode = shspec->rights & SMBM_MASK;
751         smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp));
752         *sspp = ssp;
753         return 0;
754 }
755
756 static void
757 smb_share_free(struct smb_connobj *cp)
758 {
759         struct smb_share *ssp = CPTOSS(cp);
760
761         SMB_STRFREE(ssp->ss_name);
762         SMB_STRFREE(ssp->ss_pass);
763         smb_sl_destroy(&ssp->ss_stlock);
764         smb_co_done(SSTOCP(ssp));
765         free(ssp, M_SMBCONN);
766 }
767
768 static void
769 smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred)
770 {
771         struct smb_share *ssp = CPTOSS(cp);
772
773         smb_smb_treedisconnect(ssp, scred);
774 }
775
776 void
777 smb_share_ref(struct smb_share *ssp, struct thread *td)
778 {
779         smb_co_ref(SSTOCP(ssp), td);
780 }
781
782 void
783 smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
784 {
785         smb_co_rele(SSTOCP(ssp), scred);
786 }
787
788 int
789 smb_share_get(struct smb_share *ssp, int flags, struct smb_cred *scred)
790 {
791         return smb_co_get(SSTOCP(ssp), flags, scred);
792 }
793
794 void
795 smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
796 {
797         smb_co_put(SSTOCP(ssp), scred);
798 }
799
800 int
801 smb_share_lock(struct smb_share *ssp, int flags, struct thread *td)
802 {
803         return smb_co_lock(SSTOCP(ssp), flags, td);
804 }
805
806 void
807 smb_share_unlock(struct smb_share *ssp, int flags, struct thread *td)
808 {
809         smb_co_unlock(SSTOCP(ssp), flags, td);
810 }
811
812 int
813 smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode)
814 {
815         struct ucred *cred = scred->scr_cred;
816
817         if (smb_suser(cred) == 0 || cred->cr_uid == ssp->ss_uid)
818                 return 0;
819         mode >>= 3;
820         if (!groupmember(ssp->ss_grp, cred))
821                 mode >>= 3;
822         return (ssp->ss_mode & mode) == mode ? 0 : EACCES;
823 }
824
825 void
826 smb_share_invalidate(struct smb_share *ssp)
827 {
828         ssp->ss_tid = SMB_TID_UNKNOWN;
829 }
830
831 int
832 smb_share_valid(struct smb_share *ssp)
833 {
834         return ssp->ss_tid != SMB_TID_UNKNOWN &&
835             ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid;
836 }
837
838 const char*
839 smb_share_getpass(struct smb_share *ssp)
840 {
841         struct smb_vc *vcp;
842
843         if (ssp->ss_pass)
844                 return ssp->ss_pass;
845         vcp = SSTOVC(ssp);
846         if (vcp->vc_pass)
847                 return vcp->vc_pass;
848         return smb_emptypass;
849 }
850
851 static int
852 smb_share_getinfo(struct smb_share *ssp, struct smb_share_info *sip)
853 {
854         bzero(sip, sizeof(struct smb_share_info));
855         sip->itype = SMB_INFO_SHARE;
856         sip->usecount = ssp->obj.co_usecount;
857         sip->tid  = ssp->ss_tid;
858         sip->type= ssp->ss_type;
859         sip->uid = ssp->ss_uid;
860         sip->gid = ssp->ss_grp;
861         sip->mode= ssp->ss_mode;
862         sip->flags = ssp->obj.co_flags;
863         snprintf(sip->sname, sizeof(sip->sname), "%s", ssp->ss_name);
864         return 0;
865 }
866
867 /*
868  * Dump an entire tree into sysctl call
869  */
870 static int
871 smb_sysctl_treedump(SYSCTL_HANDLER_ARGS)
872 {
873         struct thread *td = req->td;
874         struct ucred *ucred;
875         struct smb_cred scred;
876         struct smb_vc *vcp;
877         struct smb_share *ssp;
878         struct smb_vc_info vci;
879         struct smb_share_info ssi;
880         int error, itype;
881
882         KKASSERT(td->td_proc);
883         ucred = td->td_proc->p_ucred;
884
885         smb_makescred(&scred, td, ucred);
886         error = smb_sm_lockvclist(LK_SHARED, td);
887         if (error)
888                 return error;
889         SMBCO_FOREACH((struct smb_connobj*)vcp, &smb_vclist) {
890                 error = smb_vc_lock(vcp, LK_SHARED, td);
891                 if (error)
892                         continue;
893                 smb_vc_getinfo(vcp, &vci);
894                 error = SYSCTL_OUT(req, &vci, sizeof(struct smb_vc_info));
895                 if (error) {
896                         smb_vc_unlock(vcp, 0, td);
897                         break;
898                 }
899                 SMBCO_FOREACH((struct smb_connobj*)ssp, VCTOCP(vcp)) {
900                         error = smb_share_lock(ssp, LK_SHARED, td);
901                         if (error) {
902                                 error = 0;
903                                 continue;
904                         }
905                         smb_share_getinfo(ssp, &ssi);
906                         smb_share_unlock(ssp, 0, td);
907                         error = SYSCTL_OUT(req, &ssi, sizeof(struct smb_share_info));
908                         if (error)
909                                 break;
910                 }
911                 smb_vc_unlock(vcp, 0, td);
912                 if (error)
913                         break;
914         }
915         if (!error) {
916                 itype = SMB_INFO_NONE;
917                 error = SYSCTL_OUT(req, &itype, sizeof(itype));
918         }
919         smb_sm_unlockvclist(td);
920         return error;
921 }