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

Cache object: 049d87060fec4ed47325f8eaa0472665


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