The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netsmb/smb_conn.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 0ad1acf8558db8c478e66d239c863262


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.