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

Cache object: ae3a3ea5b15888fdcfb167cf7b61123e


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