2012-04-30 00:27:31 +04:00
|
|
|
/* $NetBSD: smb_conn.c,v 1.29 2012/04/29 20:27:31 dsl Exp $ */
|
2008-01-30 17:08:00 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2008 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
/*
|
2002-01-04 05:39:37 +03:00
|
|
|
* Copyright (c) 2000-2001 Boris Popov
|
2000-12-07 06:48:09 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Boris Popov.
|
|
|
|
* 4. Neither the name of the author nor the names of any co-contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2002-01-04 05:39:37 +03:00
|
|
|
*
|
|
|
|
* FreeBSD: src/sys/netsmb/smb_conn.c,v 1.3 2001/12/02 08:47:29 bp Exp
|
2000-12-07 06:48:09 +03:00
|
|
|
*/
|
|
|
|
|
2001-11-13 03:56:55 +03:00
|
|
|
#include <sys/cdefs.h>
|
2012-04-30 00:27:31 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: smb_conn.c,v 1.29 2012/04/29 20:27:31 dsl Exp $");
|
2002-01-04 05:39:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Connection engine.
|
|
|
|
*/
|
2001-11-13 03:56:55 +03:00
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/sysctl.h>
|
2003-02-26 14:12:28 +03:00
|
|
|
#include <sys/mbuf.h> /* for M_SONAME */
|
2006-05-15 01:19:33 +04:00
|
|
|
#include <sys/kauth.h>
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
#include <netsmb/iconv.h>
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
#include <netsmb/smb.h>
|
|
|
|
#include <netsmb/smb_subr.h>
|
2002-01-04 05:39:37 +03:00
|
|
|
#include <netsmb/smb_conn.h>
|
2000-12-07 06:48:09 +03:00
|
|
|
#include <netsmb/smb_tran.h>
|
|
|
|
#include <netsmb/smb_trantcp.h>
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
static struct smb_connobj smb_vclist;
|
|
|
|
static int smb_vcnext = 1; /* next unique id for VC */
|
2012-03-13 22:40:26 +04:00
|
|
|
static kauth_listener_t smb_listener;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
MALLOC_DEFINE(M_SMBCONN, "SMB conn", "SMB connection");
|
2012-04-30 00:27:31 +04:00
|
|
|
MALLOC_DECLARE(M_SMBCONN);
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2003-02-24 12:55:37 +03:00
|
|
|
static void smb_co_init(struct smb_connobj *cp, int level, const char *objname);
|
2002-01-04 05:39:37 +03:00
|
|
|
static void smb_co_done(struct smb_connobj *cp);
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
static int smb_vc_disconnect(struct smb_vc *vcp);
|
|
|
|
static void smb_vc_free(struct smb_connobj *cp);
|
|
|
|
static void smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred);
|
|
|
|
static smb_co_free_t smb_share_free;
|
|
|
|
static smb_co_gone_t smb_share_gone;
|
|
|
|
|
2012-03-13 22:40:26 +04:00
|
|
|
static int
|
|
|
|
smb_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
|
|
|
|
void *arg0, void *arg1, void *arg2, void *arg3)
|
|
|
|
{
|
|
|
|
int result, ismember = 0;
|
|
|
|
enum kauth_network_req req;
|
|
|
|
|
|
|
|
if (action != KAUTH_NETWORK_SMB)
|
|
|
|
return KAUTH_RESULT_DEFER;
|
|
|
|
|
|
|
|
result = KAUTH_RESULT_DEFER;
|
|
|
|
req = (enum kauth_network_req)arg0;
|
|
|
|
|
|
|
|
switch (req) {
|
|
|
|
case KAUTH_REQ_NETWORK_SMB_SHARE_ACCESS: {
|
|
|
|
struct smb_share *ssp = arg1;
|
|
|
|
mode_t mode = (mode_t)(uintptr_t)arg2;
|
|
|
|
|
|
|
|
/* Owner can access. */
|
|
|
|
if (kauth_cred_geteuid(cred) == ssp->ss_uid) {
|
|
|
|
result = KAUTH_RESULT_ALLOW;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try group permissions if member or other if not. */
|
|
|
|
mode >>= 3;
|
|
|
|
if (kauth_cred_ismember_gid(cred, ssp->ss_grp, &ismember) != 0 ||
|
|
|
|
!ismember)
|
|
|
|
mode >>= 3;
|
|
|
|
|
|
|
|
if ((ssp->ss_mode & mode) == mode)
|
|
|
|
result = KAUTH_RESULT_ALLOW;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case KAUTH_REQ_NETWORK_SMB_SHARE_CREATE: {
|
|
|
|
struct smb_sharespec *shspec = arg1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only superuser can create shares with different uid and gid
|
|
|
|
*/
|
|
|
|
if (shspec->owner != SMBM_ANY_OWNER &&
|
|
|
|
shspec->owner != kauth_cred_geteuid(cred))
|
|
|
|
break;
|
|
|
|
if (shspec->group != SMBM_ANY_GROUP &&
|
|
|
|
(kauth_cred_ismember_gid(cred, shspec->group, &ismember) != 0 || !ismember))
|
|
|
|
break;
|
|
|
|
|
|
|
|
result = KAUTH_RESULT_ALLOW;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case KAUTH_REQ_NETWORK_SMB_VC_ACCESS: {
|
|
|
|
struct smb_vc *vcp = arg1;
|
|
|
|
mode_t mode = (mode_t)(uintptr_t)arg2;
|
|
|
|
|
|
|
|
/* Owner can access. */
|
|
|
|
if (kauth_cred_geteuid(cred) == vcp->vc_uid) {
|
|
|
|
result = KAUTH_RESULT_ALLOW;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try group permissions if member or other if not. */
|
|
|
|
mode >>= 3;
|
|
|
|
if (kauth_cred_ismember_gid(cred, vcp->vc_grp, &ismember) != 0 ||
|
|
|
|
!ismember)
|
|
|
|
mode >>= 3;
|
|
|
|
|
|
|
|
if ((vcp->vc_mode & mode) == mode)
|
|
|
|
result = KAUTH_RESULT_ALLOW;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case KAUTH_REQ_NETWORK_SMB_VC_CREATE: {
|
|
|
|
struct smb_vcspec *vcspec = arg1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only superuser can create VCs with different uid and gid
|
|
|
|
*/
|
|
|
|
if (vcspec->owner != SMBM_ANY_OWNER &&
|
|
|
|
vcspec->owner != kauth_cred_geteuid(cred))
|
|
|
|
break;
|
|
|
|
if (vcspec->group != SMBM_ANY_GROUP &&
|
|
|
|
(kauth_cred_ismember_gid(cred, vcspec->group, &ismember) != 0 || !ismember))
|
|
|
|
break;
|
|
|
|
|
|
|
|
result = KAUTH_RESULT_ALLOW;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
smb_sm_init(void)
|
|
|
|
{
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_init(&smb_vclist, SMBL_SM, "smbsm");
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_enter(&smb_vclist.co_interlock);
|
|
|
|
smb_co_unlock(&smb_vclist);
|
|
|
|
mutex_exit(&smb_vclist.co_interlock);
|
2012-03-13 22:40:26 +04:00
|
|
|
smb_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
|
|
|
|
smb_listener_cb, NULL);
|
2000-12-07 06:48:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_sm_done(void)
|
|
|
|
{
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
/* XXX: hold the mutex */
|
2003-02-24 12:55:37 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (smb_vclist.co_usecount > 1)
|
2005-05-08 22:44:39 +04:00
|
|
|
panic("%d connections still active", smb_vclist.co_usecount - 1);
|
2003-02-24 12:55:37 +03:00
|
|
|
#endif
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_done(&smb_vclist);
|
2012-03-13 22:40:26 +04:00
|
|
|
kauth_unlisten_scope(smb_listener);
|
2000-12-07 06:48:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-07-01 17:00:54 +04:00
|
|
|
smb_sm_lockvclist(void)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2008-01-30 17:08:00 +03:00
|
|
|
int error;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_enter(&smb_vclist.co_interlock);
|
|
|
|
error = smb_co_lock(&smb_vclist);
|
|
|
|
mutex_exit(&smb_vclist.co_interlock);
|
|
|
|
|
|
|
|
return error;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
static void
|
|
|
|
smb_sm_unlockvclist(void)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_enter(&smb_vclist.co_interlock);
|
|
|
|
smb_co_unlock(&smb_vclist);
|
|
|
|
mutex_exit(&smb_vclist.co_interlock);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_sm_lookupint(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
|
|
|
|
struct smb_cred *scred, struct smb_vc **vcpp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2004-06-24 20:45:47 +04:00
|
|
|
struct smb_connobj *ocp;
|
2002-01-04 05:39:37 +03:00
|
|
|
int exact = 1;
|
2004-03-21 13:09:52 +03:00
|
|
|
int fail = 1;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
vcspec->shspec = shspec;
|
2004-06-24 20:45:47 +04:00
|
|
|
SMBCO_FOREACH(ocp, &smb_vclist) {
|
|
|
|
struct smb_vc *vcp = (struct smb_vc *)ocp;
|
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
if (smb_vc_lock(vcp) != 0)
|
2002-01-04 05:39:37 +03:00
|
|
|
continue;
|
2004-03-21 13:09:52 +03:00
|
|
|
|
|
|
|
do {
|
2004-06-24 20:45:47 +04:00
|
|
|
if ((ocp->co_flags & SMBV_PRIVATE) ||
|
2002-01-04 05:39:37 +03:00
|
|
|
!CONNADDREQ(vcp->vc_paddr, vcspec->sap) ||
|
|
|
|
strcmp(vcp->vc_username, vcspec->username) != 0)
|
2004-03-21 13:09:52 +03:00
|
|
|
break;
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
if (vcspec->owner != SMBM_ANY_OWNER) {
|
|
|
|
if (vcp->vc_uid != vcspec->owner)
|
2004-03-21 13:09:52 +03:00
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
} else
|
|
|
|
exact = 0;
|
|
|
|
if (vcspec->group != SMBM_ANY_GROUP) {
|
|
|
|
if (vcp->vc_grp != vcspec->group)
|
2004-03-21 13:09:52 +03:00
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
} else
|
|
|
|
exact = 0;
|
|
|
|
|
|
|
|
if (vcspec->mode & SMBM_EXACT) {
|
|
|
|
if (!exact ||
|
|
|
|
(vcspec->mode & SMBM_MASK) != vcp->vc_mode)
|
2004-03-21 13:09:52 +03:00
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
}
|
|
|
|
if (smb_vc_access(vcp, scred, vcspec->mode) != 0)
|
2004-03-21 13:09:52 +03:00
|
|
|
break;
|
2002-01-04 05:39:37 +03:00
|
|
|
vcspec->ssp = NULL;
|
2004-03-21 13:09:52 +03:00
|
|
|
if (shspec
|
|
|
|
&&smb_vc_lookupshare(vcp, shspec, scred, &vcspec->ssp) != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* if we get here, all checks succeeded */
|
|
|
|
smb_vc_ref(vcp);
|
|
|
|
*vcpp = vcp;
|
|
|
|
fail = 0;
|
|
|
|
goto out;
|
|
|
|
} while(0);
|
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_vc_unlock(vcp);
|
2002-01-04 05:39:37 +03:00
|
|
|
}
|
2004-03-21 13:09:52 +03:00
|
|
|
|
|
|
|
out:
|
|
|
|
return fail;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_sm_lookup(struct smb_vcspec *vcspec, struct smb_sharespec *shspec,
|
|
|
|
struct smb_cred *scred, struct smb_vc **vcpp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_vc *vcp;
|
|
|
|
struct smb_share *ssp = NULL;
|
2004-03-21 13:09:52 +03:00
|
|
|
int fail, error;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
*vcpp = vcp = NULL;
|
|
|
|
|
2010-07-01 17:00:54 +04:00
|
|
|
error = smb_sm_lockvclist();
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
2004-03-21 13:09:52 +03:00
|
|
|
fail = smb_sm_lookupint(vcspec, shspec, scred, vcpp);
|
|
|
|
if (!fail || (vcspec->flags & SMBV_CREATE) == 0) {
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_sm_unlockvclist();
|
2004-03-21 13:09:52 +03:00
|
|
|
return 0;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
2004-03-21 13:09:52 +03:00
|
|
|
fail = smb_sm_lookupint(vcspec, NULL, scred, &vcp);
|
|
|
|
if (fail) {
|
2002-01-04 05:39:37 +03:00
|
|
|
error = smb_vc_create(vcspec, scred, &vcp);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
error = smb_vc_connect(vcp, scred);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
if (shspec == NULL)
|
|
|
|
goto out;
|
|
|
|
error = smb_share_create(vcp, shspec, scred, &ssp);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (error)
|
2002-01-04 05:39:37 +03:00
|
|
|
goto out;
|
|
|
|
error = smb_smb_treeconnect(ssp, scred);
|
|
|
|
if (error == 0)
|
|
|
|
vcspec->ssp = ssp;
|
|
|
|
else
|
|
|
|
smb_share_put(ssp, scred);
|
|
|
|
out:
|
|
|
|
smb_sm_unlockvclist();
|
|
|
|
if (error == 0)
|
|
|
|
*vcpp = vcp;
|
|
|
|
else if (vcp)
|
|
|
|
smb_vc_put(vcp, scred);
|
2000-12-07 06:48:09 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-01-04 05:39:37 +03:00
|
|
|
* Common code for connection object
|
2000-12-07 06:48:09 +03:00
|
|
|
*/
|
|
|
|
static void
|
2003-02-24 12:55:37 +03:00
|
|
|
smb_co_init(struct smb_connobj *cp, int level, const char *objname)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
SLIST_INIT(&cp->co_children);
|
|
|
|
smb_sl_init(&cp->co_interlock, objname);
|
2008-01-30 17:08:00 +03:00
|
|
|
cv_init(&cp->co_lock, "smblock");
|
|
|
|
cp->co_lockcnt = 0;
|
|
|
|
cp->co_locker = NULL;
|
2002-01-04 05:39:37 +03:00
|
|
|
cp->co_level = level;
|
|
|
|
cp->co_usecount = 1;
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_enter(&cp->co_interlock);
|
|
|
|
smb_co_lock(cp);
|
|
|
|
mutex_exit(&cp->co_interlock);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
static void
|
|
|
|
smb_co_done(struct smb_connobj *cp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_sl_destroy(&cp->co_interlock);
|
2008-01-30 17:08:00 +03:00
|
|
|
cv_destroy(&cp->co_lock);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_gone(struct smb_connobj *cp, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_connobj *parent;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
if (cp->co_gone)
|
|
|
|
cp->co_gone(cp, scred);
|
|
|
|
parent = cp->co_parent;
|
|
|
|
if (parent) {
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_enter(&parent->co_interlock);
|
|
|
|
smb_co_lock(parent);
|
|
|
|
mutex_exit(&parent->co_interlock);
|
2002-01-04 05:39:37 +03:00
|
|
|
SLIST_REMOVE(&parent->co_children, cp, smb_connobj, co_next);
|
|
|
|
smb_co_put(parent, scred);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
2002-01-04 05:39:37 +03:00
|
|
|
if (cp->co_free)
|
|
|
|
cp->co_free(cp);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_ref(struct smb_connobj *cp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_enter(&cp->co_interlock);
|
2002-01-04 05:39:37 +03:00
|
|
|
cp->co_usecount++;
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_exit(&cp->co_interlock);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_enter(&cp->co_interlock);
|
|
|
|
smb_co_unlock(cp);
|
2002-01-04 05:39:37 +03:00
|
|
|
if (cp->co_usecount > 1) {
|
|
|
|
cp->co_usecount--;
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_exit(&cp->co_interlock);
|
2002-01-04 05:39:37 +03:00
|
|
|
return;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
2003-02-24 12:55:37 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (cp->co_usecount == 0)
|
|
|
|
panic("negative use_count for object %d", cp->co_level);
|
|
|
|
#endif
|
2002-01-04 05:39:37 +03:00
|
|
|
cp->co_usecount--;
|
|
|
|
cp->co_flags |= SMBO_GONE;
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_exit(&cp->co_interlock);
|
2002-01-04 05:39:37 +03:00
|
|
|
|
|
|
|
smb_co_gone(cp, scred);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_co_get(struct smb_connobj *cp, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
KASSERT(mutex_owned(&cp->co_interlock));
|
2002-01-04 05:39:37 +03:00
|
|
|
cp->co_usecount++;
|
2008-01-30 17:08:00 +03:00
|
|
|
error = smb_co_lock(cp);
|
|
|
|
if (error)
|
2002-01-04 05:39:37 +03:00
|
|
|
cp->co_usecount--;
|
2008-01-30 17:08:00 +03:00
|
|
|
return error;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
void
|
|
|
|
smb_co_put(struct smb_connobj *cp, struct smb_cred *scred)
|
|
|
|
{
|
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_enter(&cp->co_interlock);
|
2002-01-04 05:39:37 +03:00
|
|
|
if (cp->co_usecount > 1) {
|
|
|
|
cp->co_usecount--;
|
|
|
|
} else if (cp->co_usecount == 1) {
|
|
|
|
cp->co_usecount--;
|
|
|
|
cp->co_flags |= SMBO_GONE;
|
|
|
|
}
|
2003-02-24 12:55:37 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
else
|
|
|
|
panic("smb_co_put: negative usecount");
|
|
|
|
#endif
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_co_unlock(cp);
|
|
|
|
mutex_exit(&cp->co_interlock);
|
2002-01-04 05:39:37 +03:00
|
|
|
if ((cp->co_flags & SMBO_GONE) == 0)
|
|
|
|
return;
|
|
|
|
smb_co_gone(cp, scred);
|
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
|
|
|
|
int
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_co_lock(struct smb_connobj *cp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(mutex_owned(&cp->co_interlock));
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (cp->co_flags & SMBO_GONE)
|
|
|
|
return EINVAL;
|
|
|
|
if (cp->co_locker == NULL) {
|
|
|
|
cp->co_locker = curlwp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (cp->co_locker == curlwp) {
|
|
|
|
cp->co_lockcnt++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
cv_wait(&cp->co_lock, &cp->co_interlock);
|
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
void
|
|
|
|
smb_co_unlock(struct smb_connobj *cp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
KASSERT(mutex_owned(&cp->co_interlock));
|
|
|
|
KASSERT(cp->co_locker == curlwp);
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
if (cp->co_lockcnt != 0) {
|
|
|
|
cp->co_lockcnt--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cp->co_locker = NULL;
|
|
|
|
cv_signal(&cp->co_lock);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_addchild(struct smb_connobj *parent, struct smb_connobj *child)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_ref(parent);
|
|
|
|
SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
|
|
|
|
child->co_parent = parent;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Session implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_vc_create(struct smb_vcspec *vcspec,
|
|
|
|
struct smb_cred *scred, struct smb_vc **vcpp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
struct smb_vc *vcp;
|
2006-05-15 01:19:33 +04:00
|
|
|
kauth_cred_t cred = scred->scr_cred;
|
2000-12-07 06:48:09 +03:00
|
|
|
uid_t uid = vcspec->owner;
|
|
|
|
gid_t gid = vcspec->group;
|
2006-05-15 01:19:33 +04:00
|
|
|
uid_t realuid;
|
2002-01-04 05:39:37 +03:00
|
|
|
char *domain = vcspec->domain;
|
2012-03-13 22:40:26 +04:00
|
|
|
int error;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2012-03-13 22:40:26 +04:00
|
|
|
error = kauth_authorize_network(cred, KAUTH_NETWORK_SMB,
|
|
|
|
KAUTH_REQ_NETWORK_SMB_VC_CREATE, vcspec, NULL, NULL);
|
|
|
|
if (error)
|
2000-12-07 06:48:09 +03:00
|
|
|
return EPERM;
|
2006-05-15 01:19:33 +04:00
|
|
|
|
2012-03-13 22:40:26 +04:00
|
|
|
realuid = kauth_cred_geteuid(cred);
|
2002-01-04 05:39:37 +03:00
|
|
|
|
|
|
|
vcp = smb_zmalloc(sizeof(*vcp), M_SMBCONN, M_WAITOK);
|
|
|
|
smb_co_init(VCTOCP(vcp), SMBL_VC, "smb_vc");
|
|
|
|
vcp->obj.co_free = smb_vc_free;
|
|
|
|
vcp->obj.co_gone = smb_vc_gone;
|
|
|
|
vcp->vc_number = smb_vcnext++;
|
|
|
|
vcp->vc_smbuid = SMB_UID_UNKNOWN;
|
|
|
|
vcp->vc_mode = vcspec->rights & SMBM_MASK;
|
|
|
|
vcp->obj.co_flags = vcspec->flags & (SMBV_PRIVATE | SMBV_SINGLESHARE);
|
|
|
|
vcp->vc_tdesc = &smb_tran_nbtcp_desc;
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
if (uid == SMBM_ANY_OWNER)
|
|
|
|
uid = realuid;
|
|
|
|
if (gid == SMBM_ANY_GROUP)
|
2006-05-15 01:19:33 +04:00
|
|
|
gid = kauth_cred_group(cred, 0);
|
2000-12-07 06:48:09 +03:00
|
|
|
vcp->vc_uid = uid;
|
|
|
|
vcp->vc_grp = gid;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
|
|
|
smb_sl_init(&vcp->vc_stlock, "vcstlock");
|
2004-03-21 13:09:52 +03:00
|
|
|
error = ENOMEM;
|
|
|
|
if ((vcp->vc_paddr = dup_sockaddr(vcspec->sap, 1)) == NULL)
|
|
|
|
goto fail;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2004-03-21 13:09:52 +03:00
|
|
|
if ((vcp->vc_laddr = dup_sockaddr(vcspec->lap, 1)) == NULL)
|
|
|
|
goto fail;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2004-03-21 13:09:52 +03:00
|
|
|
if ((vcp->vc_pass = smb_strdup(vcspec->pass)) == NULL)
|
|
|
|
goto fail;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2004-03-21 13:09:52 +03:00
|
|
|
vcp->vc_domain = smb_strdup((domain && domain[0]) ? domain : "NODOMAIN");
|
|
|
|
if (vcp->vc_domain == NULL)
|
|
|
|
goto fail;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2004-03-21 13:09:52 +03:00
|
|
|
if ((vcp->vc_srvname = smb_strdup(vcspec->srvname)) == NULL)
|
|
|
|
goto fail;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2004-03-21 13:09:52 +03:00
|
|
|
if ((vcp->vc_username = smb_strdup(vcspec->username)) == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
#define ithrow(cmd) \
|
|
|
|
if ((error = cmd)) \
|
|
|
|
goto fail
|
|
|
|
|
|
|
|
ithrow(iconv_open("tolower", vcspec->localcs, &vcp->vc_tolower));
|
|
|
|
ithrow(iconv_open("toupper", vcspec->localcs, &vcp->vc_toupper));
|
|
|
|
if (vcspec->servercs[0]) {
|
|
|
|
ithrow(iconv_open(vcspec->servercs, vcspec->localcs,
|
|
|
|
&vcp->vc_toserver));
|
|
|
|
ithrow(iconv_open(vcspec->localcs, vcspec->servercs,
|
|
|
|
&vcp->vc_tolocal));
|
|
|
|
}
|
|
|
|
|
|
|
|
ithrow(smb_iod_create(vcp));
|
|
|
|
|
|
|
|
#undef ithrow
|
|
|
|
|
|
|
|
/* all is well, return success */
|
|
|
|
*vcpp = vcp;
|
|
|
|
smb_co_addchild(&smb_vclist, VCTOCP(vcp));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
smb_vc_put(vcp, scred);
|
|
|
|
return (error);
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_vc_free(struct smb_connobj *cp)
|
|
|
|
{
|
|
|
|
struct smb_vc *vcp = CPTOVC(cp);
|
|
|
|
|
|
|
|
if (vcp->vc_iod)
|
|
|
|
smb_iod_destroy(vcp->vc_iod);
|
|
|
|
SMB_STRFREE(vcp->vc_username);
|
|
|
|
SMB_STRFREE(vcp->vc_srvname);
|
|
|
|
SMB_STRFREE(vcp->vc_pass);
|
|
|
|
SMB_STRFREE(vcp->vc_domain);
|
|
|
|
if (vcp->vc_paddr)
|
|
|
|
free(vcp->vc_paddr, M_SONAME);
|
|
|
|
if (vcp->vc_laddr)
|
|
|
|
free(vcp->vc_laddr, M_SONAME);
|
|
|
|
if (vcp->vc_tolower)
|
|
|
|
iconv_close(vcp->vc_tolower);
|
|
|
|
if (vcp->vc_toupper)
|
|
|
|
iconv_close(vcp->vc_toupper);
|
|
|
|
if (vcp->vc_tolocal)
|
|
|
|
iconv_close(vcp->vc_tolocal);
|
|
|
|
if (vcp->vc_toserver)
|
|
|
|
iconv_close(vcp->vc_toserver);
|
|
|
|
smb_co_done(VCTOCP(vcp));
|
|
|
|
smb_sl_destroy(&vcp->vc_stlock);
|
2000-12-07 06:48:09 +03:00
|
|
|
free(vcp, M_SMBCONN);
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
/*
|
|
|
|
* Called when use count of VC dropped to zero.
|
|
|
|
* VC should be locked on enter with LK_DRAIN.
|
|
|
|
*/
|
|
|
|
static void
|
2006-11-16 04:32:37 +03:00
|
|
|
smb_vc_gone(struct smb_connobj *cp, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_vc *vcp = CPTOVC(cp);
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_vc_disconnect(vcp);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
void
|
|
|
|
smb_vc_ref(struct smb_vc *vcp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_ref(VCTOCP(vcp));
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
void
|
|
|
|
smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_rele(VCTOCP(vcp), scred);
|
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
int
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_vc_get(struct smb_vc *vcp, struct smb_cred *scred)
|
2002-01-04 05:39:37 +03:00
|
|
|
{
|
2008-01-30 17:08:00 +03:00
|
|
|
struct smb_connobj *cp = VCTOCP(vcp);
|
|
|
|
int error;
|
|
|
|
|
|
|
|
mutex_enter(&cp->co_interlock);
|
|
|
|
error = smb_co_get(cp, scred);
|
|
|
|
mutex_exit(&cp->co_interlock);
|
|
|
|
|
|
|
|
return error;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred)
|
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_put(VCTOCP(vcp), scred);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
int
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_vc_lock(struct smb_vc *vcp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2008-01-30 17:08:00 +03:00
|
|
|
struct smb_connobj *cp = VCTOCP(vcp);
|
|
|
|
int error;
|
|
|
|
|
|
|
|
mutex_enter(&cp->co_interlock);
|
|
|
|
error = smb_co_lock(cp);
|
|
|
|
mutex_exit(&cp->co_interlock);
|
|
|
|
|
|
|
|
return error;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_vc_unlock(struct smb_vc *vcp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2008-01-30 17:08:00 +03:00
|
|
|
struct smb_connobj *cp = VCTOCP(vcp);
|
|
|
|
|
|
|
|
mutex_enter(&cp->co_interlock);
|
|
|
|
smb_co_unlock(cp);
|
|
|
|
mutex_exit(&cp->co_interlock);
|
2002-01-04 05:39:37 +03:00
|
|
|
}
|
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
int
|
|
|
|
smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode)
|
|
|
|
{
|
2006-05-15 01:19:33 +04:00
|
|
|
kauth_cred_t cred = scred->scr_cred;
|
2012-03-13 22:40:26 +04:00
|
|
|
int error;
|
2002-01-04 05:39:37 +03:00
|
|
|
|
2012-03-13 22:40:26 +04:00
|
|
|
error = kauth_authorize_network(cred, KAUTH_NETWORK_SMB,
|
|
|
|
KAUTH_REQ_NETWORK_SMB_VC_ACCESS, vcp, KAUTH_ARG(mode), NULL);
|
|
|
|
if (error)
|
|
|
|
return EACCES;
|
|
|
|
|
|
|
|
return 0;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_vc_cmpshare(struct smb_share *ssp, struct smb_sharespec *dp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
int exact = 1;
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
if (strcmp(ssp->ss_name, dp->name) != 0)
|
2000-12-07 06:48:09 +03:00
|
|
|
return 1;
|
|
|
|
if (dp->owner != SMBM_ANY_OWNER) {
|
|
|
|
if (ssp->ss_uid != dp->owner)
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
exact = 0;
|
|
|
|
if (dp->group != SMBM_ANY_GROUP) {
|
|
|
|
if (ssp->ss_grp != dp->group)
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
exact = 0;
|
|
|
|
|
|
|
|
if (dp->mode & SMBM_EXACT) {
|
|
|
|
if (!exact)
|
|
|
|
return 1;
|
|
|
|
return (dp->mode & SMBM_MASK) == ssp->ss_mode ? 0 : 1;
|
|
|
|
}
|
|
|
|
if (smb_share_access(ssp, dp->scred, dp->mode) != 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup share in the given VC. Share referenced and locked on return.
|
|
|
|
* VC expected to be locked on entry and will be left locked on exit.
|
|
|
|
*/
|
|
|
|
int
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *dp,
|
2000-12-07 06:48:09 +03:00
|
|
|
struct smb_cred *scred, struct smb_share **sspp)
|
|
|
|
{
|
2004-06-24 20:45:47 +04:00
|
|
|
struct smb_connobj *osp;
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_share *ssp = NULL;
|
2000-12-07 06:48:09 +03:00
|
|
|
int error;
|
|
|
|
|
|
|
|
*sspp = NULL;
|
2002-01-04 05:39:37 +03:00
|
|
|
dp->scred = scred;
|
2004-06-24 20:45:47 +04:00
|
|
|
SMBCO_FOREACH(osp, VCTOCP(vcp)) {
|
|
|
|
ssp = (struct smb_share *)osp;
|
2008-01-30 17:08:00 +03:00
|
|
|
error = smb_share_lock(ssp);
|
2002-01-04 05:39:37 +03:00
|
|
|
if (error)
|
|
|
|
continue;
|
|
|
|
if (smb_vc_cmpshare(ssp, dp) == 0)
|
|
|
|
break;
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_share_unlock(ssp);
|
2002-01-04 05:39:37 +03:00
|
|
|
}
|
|
|
|
if (ssp) {
|
|
|
|
smb_share_ref(ssp);
|
|
|
|
*sspp = ssp;
|
|
|
|
error = 0;
|
2000-12-07 06:48:09 +03:00
|
|
|
} else
|
|
|
|
error = ENOENT;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-16 04:32:37 +03:00
|
|
|
smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
|
|
|
|
return smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy VC to server, invalidate shares linked with it.
|
|
|
|
* Transport should be locked on entry.
|
|
|
|
*/
|
|
|
|
int
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_vc_disconnect(struct smb_vc *vcp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_iod_request(vcp->vc_iod, SMBIOD_EV_DISCONNECT | SMBIOD_EV_SYNC, NULL);
|
2000-12-07 06:48:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-18 13:16:49 +03:00
|
|
|
static const char * const smb_emptypass = "";
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
const char *
|
|
|
|
smb_vc_getpass(struct smb_vc *vcp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
if (vcp->vc_pass)
|
|
|
|
return vcp->vc_pass;
|
|
|
|
return smb_emptypass;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u_short
|
|
|
|
smb_vc_nextmid(struct smb_vc *vcp)
|
|
|
|
{
|
|
|
|
u_short r;
|
|
|
|
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_enter(&vcp->obj.co_interlock);
|
2000-12-07 06:48:09 +03:00
|
|
|
r = vcp->vc_mid++;
|
2008-01-30 17:08:00 +03:00
|
|
|
mutex_exit(&vcp->obj.co_interlock);
|
2000-12-07 06:48:09 +03:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Share implementation
|
|
|
|
*/
|
|
|
|
/*
|
2002-01-04 05:39:37 +03:00
|
|
|
* Allocate share structure and attach it to the given VC
|
2000-12-07 06:48:09 +03:00
|
|
|
* Connection expected to be locked on entry. Share will be returned
|
|
|
|
* in locked state.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec,
|
|
|
|
struct smb_cred *scred, struct smb_share **sspp)
|
|
|
|
{
|
|
|
|
struct smb_share *ssp;
|
2006-05-15 01:19:33 +04:00
|
|
|
kauth_cred_t cred = scred->scr_cred;
|
|
|
|
uid_t realuid;
|
2000-12-07 06:48:09 +03:00
|
|
|
uid_t uid = shspec->owner;
|
|
|
|
gid_t gid = shspec->group;
|
2012-03-13 22:40:26 +04:00
|
|
|
int error;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2012-03-13 22:40:26 +04:00
|
|
|
error = kauth_authorize_network(cred, KAUTH_NETWORK_SMB,
|
|
|
|
KAUTH_REQ_NETWORK_SMB_SHARE_CREATE, shspec, NULL, NULL);
|
|
|
|
if (error)
|
2000-12-07 06:48:09 +03:00
|
|
|
return EPERM;
|
2012-03-13 22:40:26 +04:00
|
|
|
|
|
|
|
realuid = kauth_cred_geteuid(cred);
|
|
|
|
|
2000-12-07 06:48:09 +03:00
|
|
|
error = smb_vc_lookupshare(vcp, shspec, scred, &ssp);
|
|
|
|
if (!error) {
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_share_put(ssp, scred);
|
2000-12-07 06:48:09 +03:00
|
|
|
return EEXIST;
|
|
|
|
}
|
|
|
|
if (uid == SMBM_ANY_OWNER)
|
|
|
|
uid = realuid;
|
|
|
|
if (gid == SMBM_ANY_GROUP)
|
2006-05-15 01:19:33 +04:00
|
|
|
gid = kauth_cred_group(cred, 0);
|
2002-01-04 05:39:37 +03:00
|
|
|
ssp = smb_zmalloc(sizeof(*ssp), M_SMBCONN, M_WAITOK);
|
|
|
|
smb_co_init(SSTOCP(ssp), SMBL_SHARE, "smbss");
|
|
|
|
ssp->obj.co_free = smb_share_free;
|
|
|
|
ssp->obj.co_gone = smb_share_gone;
|
|
|
|
smb_sl_init(&ssp->ss_stlock, "ssstlock");
|
|
|
|
ssp->ss_name = smb_strdup(shspec->name);
|
|
|
|
if (shspec->pass && shspec->pass[0])
|
|
|
|
ssp->ss_pass = smb_strdup(shspec->pass);
|
2000-12-07 06:48:09 +03:00
|
|
|
ssp->ss_type = shspec->stype;
|
2002-01-04 05:39:37 +03:00
|
|
|
ssp->ss_tid = SMB_TID_UNKNOWN;
|
2000-12-07 06:48:09 +03:00
|
|
|
ssp->ss_uid = uid;
|
|
|
|
ssp->ss_grp = gid;
|
|
|
|
ssp->ss_mode = shspec->rights & SMBM_MASK;
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp));
|
2000-12-07 06:48:09 +03:00
|
|
|
*sspp = ssp;
|
2002-01-04 05:39:37 +03:00
|
|
|
return 0;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_share_free(struct smb_connobj *cp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_share *ssp = CPTOSS(cp);
|
|
|
|
|
|
|
|
SMB_STRFREE(ssp->ss_name);
|
|
|
|
SMB_STRFREE(ssp->ss_pass);
|
|
|
|
smb_sl_destroy(&ssp->ss_stlock);
|
|
|
|
smb_co_done(SSTOCP(ssp));
|
2000-12-07 06:48:09 +03:00
|
|
|
free(ssp, M_SMBCONN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_share_gone(struct smb_connobj *cp, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
struct smb_share *ssp = CPTOSS(cp);
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_smb_treedisconnect(ssp, scred);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
void
|
|
|
|
smb_share_ref(struct smb_share *ssp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_ref(SSTOCP(ssp));
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_share_rele(struct smb_share *ssp, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_rele(SSTOCP(ssp), scred);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
int
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_share_get(struct smb_share *ssp, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2008-01-30 17:08:00 +03:00
|
|
|
struct smb_connobj *cp = SSTOCP(ssp);
|
|
|
|
int error;
|
|
|
|
|
|
|
|
mutex_enter(&cp->co_interlock);
|
|
|
|
error = smb_co_get(cp, scred);
|
|
|
|
mutex_exit(&cp->co_interlock);
|
|
|
|
|
|
|
|
return error;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_share_put(struct smb_share *ssp, struct smb_cred *scred)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_co_put(SSTOCP(ssp), scred);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_share_lock(struct smb_share *ssp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2008-01-30 17:08:00 +03:00
|
|
|
struct smb_connobj *cp = SSTOCP(ssp);
|
|
|
|
int error;
|
|
|
|
|
|
|
|
mutex_enter(&cp->co_interlock);
|
|
|
|
error = smb_co_lock(cp);
|
|
|
|
mutex_exit(&cp->co_interlock);
|
|
|
|
|
|
|
|
return error;
|
2002-01-04 05:39:37 +03:00
|
|
|
}
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
void
|
2008-01-30 17:08:00 +03:00
|
|
|
smb_share_unlock(struct smb_share *ssp)
|
2002-01-04 05:39:37 +03:00
|
|
|
{
|
2008-01-30 17:08:00 +03:00
|
|
|
struct smb_connobj *cp = SSTOCP(ssp);
|
|
|
|
|
|
|
|
mutex_enter(&cp->co_interlock);
|
|
|
|
smb_co_unlock(cp);
|
|
|
|
mutex_exit(&cp->co_interlock);
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-01-04 05:39:37 +03:00
|
|
|
smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
2006-05-15 01:19:33 +04:00
|
|
|
kauth_cred_t cred = scred->scr_cred;
|
2012-03-13 22:40:26 +04:00
|
|
|
int error;
|
2000-12-07 06:48:09 +03:00
|
|
|
|
2012-03-13 22:40:26 +04:00
|
|
|
error = kauth_authorize_network(cred, KAUTH_NETWORK_SMB,
|
|
|
|
KAUTH_REQ_NETWORK_SMB_SHARE_ACCESS, ssp, KAUTH_ARG(mode), NULL);
|
|
|
|
if (error)
|
|
|
|
return EACCES;
|
|
|
|
|
|
|
|
return 0;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
smb_share_valid(struct smb_share *ssp)
|
|
|
|
{
|
2002-01-04 05:39:37 +03:00
|
|
|
return ssp->ss_tid != SMB_TID_UNKNOWN &&
|
|
|
|
ssp->ss_vcgenid == SSTOVC(ssp)->vc_genid;
|
2000-12-07 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
2002-01-04 05:39:37 +03:00
|
|
|
const char*
|
|
|
|
smb_share_getpass(struct smb_share *ssp)
|
2000-12-07 06:48:09 +03:00
|
|
|
{
|
|
|
|
struct smb_vc *vcp;
|
|
|
|
|
|
|
|
if (ssp->ss_pass)
|
|
|
|
return ssp->ss_pass;
|
2002-01-04 05:39:37 +03:00
|
|
|
vcp = SSTOVC(ssp);
|
2000-12-07 06:48:09 +03:00
|
|
|
if (vcp->vc_pass)
|
|
|
|
return vcp->vc_pass;
|
|
|
|
return smb_emptypass;
|
|
|
|
}
|
|
|
|
|
|
|
|
|