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

Cache object: 8386773e6da9adec34eb5a268d36152b


[ 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.