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