2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
2010-04-07 16:50:38 +00:00
|
|
|
* Copyright (c) 1999 Boris Popov
|
1999-10-02 04:06:24 +00: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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Connection tables
|
|
|
|
*/
|
2003-06-11 05:37:42 +00:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1999-10-02 04:06:24 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
2006-11-06 13:42:10 +00:00
|
|
|
#include <sys/priv.h>
|
1999-10-02 04:06:24 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <netncp/ncp.h>
|
2001-03-10 05:24:45 +00:00
|
|
|
#include <netncp/nwerror.h>
|
1999-10-02 04:06:24 +00:00
|
|
|
#include <netncp/ncp_subr.h>
|
|
|
|
#include <netncp/ncp_conn.h>
|
2001-03-10 05:24:45 +00:00
|
|
|
#include <netncp/ncp_sock.h>
|
|
|
|
#include <netncp/ncp_ncp.h>
|
1999-10-02 04:06:24 +00:00
|
|
|
|
2000-05-26 02:09:24 +00:00
|
|
|
SLIST_HEAD(ncp_handle_head,ncp_handle);
|
1999-10-02 04:06:24 +00:00
|
|
|
|
|
|
|
int ncp_burst_enabled = 1;
|
|
|
|
|
|
|
|
struct ncp_conn_head conn_list={NULL};
|
|
|
|
static int ncp_conn_cnt = 0;
|
|
|
|
static int ncp_next_ref = 1;
|
|
|
|
static struct lock listlock;
|
|
|
|
|
|
|
|
struct ncp_handle_head lhlist={NULL};
|
|
|
|
static int ncp_next_handle = 1;
|
|
|
|
static struct lock lhlock;
|
|
|
|
|
2000-07-04 11:25:35 +00:00
|
|
|
static int ncp_sysctl_connstat(SYSCTL_HANDLER_ARGS);
|
2003-02-26 21:25:55 +00:00
|
|
|
static int ncp_conn_lock_any(struct ncp_conn *conn, struct thread *td,
|
1999-10-02 04:06:24 +00:00
|
|
|
struct ucred *cred);
|
|
|
|
|
|
|
|
SYSCTL_DECL(_net_ncp);
|
|
|
|
SYSCTL_INT (_net_ncp, OID_AUTO, burst_enabled, CTLFLAG_RD, &ncp_burst_enabled, 0, "");
|
|
|
|
SYSCTL_INT (_net_ncp, OID_AUTO, conn_cnt, CTLFLAG_RD, &ncp_conn_cnt, 0, "");
|
|
|
|
SYSCTL_PROC(_net_ncp, OID_AUTO, conn_stat, CTLFLAG_RD|CTLTYPE_OPAQUE,
|
|
|
|
NULL, 0, ncp_sysctl_connstat, "S,connstat", "Connections list");
|
|
|
|
|
2005-10-31 15:41:29 +00:00
|
|
|
MALLOC_DEFINE(M_NCPDATA, "ncp_data", "NCP private data");
|
1999-10-02 04:06:24 +00:00
|
|
|
|
|
|
|
int
|
2001-03-10 05:24:45 +00:00
|
|
|
ncp_conn_init(void)
|
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
lockinit(&listlock, PSOCK, "ncpll", 0, 0);
|
|
|
|
lockinit(&lhlock, PSOCK, "ncplh", 0, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-10-04 01:29:17 +00:00
|
|
|
int
|
2001-03-10 05:24:45 +00:00
|
|
|
ncp_conn_destroy(void)
|
|
|
|
{
|
|
|
|
if (ncp_conn_cnt) {
|
|
|
|
NCPERROR("There are %d connections active\n", ncp_conn_cnt);
|
|
|
|
return EBUSY;
|
|
|
|
}
|
2000-10-04 01:29:17 +00:00
|
|
|
lockdestroy(&listlock);
|
|
|
|
lockdestroy(&lhlock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-10-02 04:06:24 +00:00
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_locklist(int flags, struct thread *td)
|
2001-03-10 05:24:45 +00:00
|
|
|
{
|
2008-01-24 12:34:30 +00:00
|
|
|
return lockmgr(&listlock, flags | LK_CANRECURSE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlocklist(struct thread *td)
|
2001-03-10 05:24:45 +00:00
|
|
|
{
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&listlock, LK_RELEASE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-03-10 05:24:45 +00:00
|
|
|
ncp_conn_access(struct ncp_conn *conn, struct ucred *cred, mode_t mode)
|
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
int error;
|
|
|
|
|
2000-04-05 10:54:02 +00:00
|
|
|
if (cred == NOCRED || ncp_suser(cred) == 0 ||
|
|
|
|
cred->cr_uid == conn->nc_owner->cr_uid)
|
1999-10-02 04:06:24 +00:00
|
|
|
return 0;
|
|
|
|
mode >>= 3;
|
|
|
|
if (!groupmember(conn->nc_group, cred))
|
|
|
|
mode >>= 3;
|
|
|
|
error = (conn->li.access_mode & mode) == mode ? 0 : EACCES;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_lock_any(struct ncp_conn *conn, struct thread *td, struct ucred *cred)
|
2001-03-10 05:24:45 +00:00
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (conn->nc_id == 0) return EACCES;
|
2008-01-24 12:34:30 +00:00
|
|
|
error = lockmgr(&conn->nc_lock, LK_EXCLUSIVE | LK_CANRECURSE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
if (error == ERESTART)
|
|
|
|
return EINTR;
|
2003-02-26 21:25:55 +00:00
|
|
|
error = ncp_chkintr(conn, td);
|
1999-10-02 04:06:24 +00:00
|
|
|
if (error) {
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&conn->nc_lock, LK_RELEASE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conn->nc_id == 0) {
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&conn->nc_lock, LK_RELEASE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
return EACCES;
|
|
|
|
}
|
2003-02-26 21:25:55 +00:00
|
|
|
conn->td = td; /* who currently operates */
|
1999-10-02 04:06:24 +00:00
|
|
|
conn->ucred = cred;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_lock(struct ncp_conn *conn, struct thread *td, struct ucred *cred, int mode)
|
2001-03-10 05:24:45 +00:00
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
int error;
|
|
|
|
|
2003-02-26 21:25:55 +00:00
|
|
|
error = ncp_conn_access(conn, cred, mode);
|
1999-10-02 04:06:24 +00:00
|
|
|
if (error) return error;
|
2003-02-26 21:25:55 +00:00
|
|
|
return ncp_conn_lock_any(conn, td, cred);
|
1999-10-02 04:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock conn but unlock connlist
|
|
|
|
*/
|
|
|
|
static int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_lock2(struct ncp_conn *conn, struct thread *td, struct ucred *cred, int mode)
|
2001-03-10 05:24:45 +00:00
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
int error;
|
|
|
|
|
2003-02-26 21:25:55 +00:00
|
|
|
error = ncp_conn_access(conn, cred, mode);
|
1999-10-02 04:06:24 +00:00
|
|
|
if (error) {
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlocklist(td);
|
1999-10-02 04:06:24 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
conn->nc_lwant++;
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlocklist(td);
|
|
|
|
error = ncp_conn_lock_any(conn, td, cred);
|
1999-10-02 04:06:24 +00:00
|
|
|
conn->nc_lwant--;
|
|
|
|
if (conn->nc_lwant == 0) {
|
|
|
|
wakeup(&conn->nc_lwant);
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlock(struct ncp_conn *conn, struct thread *td)
|
2001-03-10 05:24:45 +00:00
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
/*
|
|
|
|
* note, that LK_RELASE will do wakeup() instead of wakeup_one().
|
|
|
|
* this will do a little overhead
|
|
|
|
*/
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&conn->nc_lock, LK_RELEASE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
}
|
|
|
|
|
2003-02-26 21:25:55 +00:00
|
|
|
int
|
|
|
|
ncp_conn_assert_locked(struct ncp_conn *conn, const char *checker, struct thread *td)
|
|
|
|
{
|
2008-02-25 18:45:57 +00:00
|
|
|
if (lockstatus(&conn->nc_lock) == LK_EXCLUSIVE) return 0;
|
1999-10-02 04:06:24 +00:00
|
|
|
printf("%s: connection isn't locked!\n", checker);
|
|
|
|
return EIO;
|
|
|
|
}
|
|
|
|
|
2001-03-22 10:29:39 +00:00
|
|
|
void
|
|
|
|
ncp_conn_invalidate(struct ncp_conn *ncp)
|
|
|
|
{
|
|
|
|
ncp->flags &= ~(NCPFL_ATTACHED | NCPFL_LOGGED | NCPFL_INVALID);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ncp_conn_invalid(struct ncp_conn *ncp)
|
|
|
|
{
|
|
|
|
return ncp->flags & NCPFL_INVALID;
|
|
|
|
}
|
|
|
|
|
2003-02-26 21:25:55 +00:00
|
|
|
/*
|
1999-10-02 04:06:24 +00:00
|
|
|
* create, fill with defaults and return in locked state
|
|
|
|
*/
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_alloc(struct ncp_conn_args *cap, struct thread *td, struct ucred *cred,
|
2001-03-10 05:24:45 +00:00
|
|
|
struct ncp_conn **conn)
|
1999-10-02 04:06:24 +00:00
|
|
|
{
|
|
|
|
struct ncp_conn *ncp;
|
2001-03-10 05:24:45 +00:00
|
|
|
struct ucred *owner;
|
|
|
|
int error, isroot;
|
1999-10-02 04:06:24 +00:00
|
|
|
|
2001-03-10 05:24:45 +00:00
|
|
|
if (cap->saddr.sa_family != AF_INET && cap->saddr.sa_family != AF_IPX)
|
|
|
|
return EPROTONOSUPPORT;
|
|
|
|
/*
|
2007-05-27 17:14:33 +00:00
|
|
|
* Only root can change ownership.
|
2001-03-10 05:24:45 +00:00
|
|
|
*/
|
2007-05-27 17:14:33 +00:00
|
|
|
isroot = ncp_suser(cred) == 0;
|
2001-03-10 05:24:45 +00:00
|
|
|
if (cap->owner != NCP_DEFAULT_OWNER && !isroot)
|
|
|
|
return EPERM;
|
|
|
|
if (cap->group != NCP_DEFAULT_GROUP &&
|
|
|
|
!groupmember(cap->group, cred) && !isroot)
|
|
|
|
return EPERM;
|
|
|
|
if (cap->owner != NCP_DEFAULT_OWNER) {
|
|
|
|
owner = crget();
|
2007-05-27 17:14:33 +00:00
|
|
|
crcopy(owner, cred);
|
2001-03-10 05:24:45 +00:00
|
|
|
owner->cr_uid = cap->owner;
|
2001-10-11 23:38:17 +00:00
|
|
|
} else
|
|
|
|
owner = crhold(cred);
|
2008-10-23 15:53:51 +00:00
|
|
|
ncp = malloc(sizeof(struct ncp_conn),
|
2003-02-19 05:47:46 +00:00
|
|
|
M_NCPDATA, M_WAITOK | M_ZERO);
|
1999-10-02 04:06:24 +00:00
|
|
|
error = 0;
|
|
|
|
lockinit(&ncp->nc_lock, PZERO, "ncplck", 0, 0);
|
|
|
|
ncp_conn_cnt++;
|
|
|
|
ncp->nc_id = ncp_next_ref++;
|
2007-05-27 17:14:33 +00:00
|
|
|
ncp->nc_owner = owner;
|
1999-10-02 04:06:24 +00:00
|
|
|
ncp->seq = 0;
|
|
|
|
ncp->connid = 0xFFFF;
|
2001-03-10 05:24:45 +00:00
|
|
|
ncp->li = *cap;
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp->nc_group = (cap->group != NCP_DEFAULT_GROUP) ?
|
2001-03-10 05:24:45 +00:00
|
|
|
cap->group : cred->cr_groups[0];
|
|
|
|
|
|
|
|
if (cap->retry_count == 0)
|
|
|
|
ncp->li.retry_count = NCP_RETRY_COUNT;
|
|
|
|
if (cap->timeout == 0)
|
|
|
|
ncp->li.timeout = NCP_RETRY_TIMEOUT;
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_lock_any(ncp, td, ncp->nc_owner);
|
1999-10-02 04:06:24 +00:00
|
|
|
*conn = ncp;
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_locklist(LK_EXCLUSIVE, td);
|
1999-10-02 04:06:24 +00:00
|
|
|
SLIST_INSERT_HEAD(&conn_list,ncp,nc_next);
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlocklist(td);
|
1999-10-02 04:06:24 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the connection, on entry it must be locked
|
|
|
|
*/
|
|
|
|
int
|
2001-03-10 05:24:45 +00:00
|
|
|
ncp_conn_free(struct ncp_conn *ncp)
|
|
|
|
{
|
2003-02-26 21:25:55 +00:00
|
|
|
struct thread *td;
|
1999-10-02 04:06:24 +00:00
|
|
|
int error;
|
|
|
|
|
2001-03-10 05:24:45 +00:00
|
|
|
if (ncp == NULL) {
|
|
|
|
NCPFATAL("ncp == NULL\n");
|
|
|
|
return 0;
|
|
|
|
}
|
1999-10-02 04:06:24 +00:00
|
|
|
if (ncp->nc_id == 0) {
|
2001-03-10 05:24:45 +00:00
|
|
|
NCPERROR("nc_id == 0\n");
|
1999-10-02 04:06:24 +00:00
|
|
|
return EACCES;
|
|
|
|
}
|
2003-02-26 21:25:55 +00:00
|
|
|
td = ncp->td;
|
|
|
|
error = ncp_conn_assert_locked(ncp, __func__, td);
|
2001-03-10 05:24:45 +00:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
if (ncp->ref_cnt != 0 || (ncp->flags & NCPFL_PERMANENT))
|
|
|
|
return EBUSY;
|
|
|
|
if (ncp_conn_access(ncp, ncp->ucred, NCPM_WRITE))
|
|
|
|
return EACCES;
|
|
|
|
|
2001-03-22 10:29:39 +00:00
|
|
|
if (ncp->flags & NCPFL_ATTACHED)
|
2001-03-10 05:24:45 +00:00
|
|
|
ncp_ncp_disconnect(ncp);
|
|
|
|
ncp_sock_disconnect(ncp);
|
|
|
|
|
1999-10-02 04:06:24 +00:00
|
|
|
/*
|
2001-03-10 05:24:45 +00:00
|
|
|
* Mark conn as dead and wait for other process
|
1999-10-02 04:06:24 +00:00
|
|
|
*/
|
|
|
|
ncp->nc_id = 0;
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlock(ncp, td);
|
1999-10-02 04:06:24 +00:00
|
|
|
/*
|
|
|
|
* if signal is raised - how I do react ?
|
|
|
|
*/
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&ncp->nc_lock, LK_DRAIN, 0);
|
2008-03-30 18:16:33 +00:00
|
|
|
lockmgr(&ncp->nc_lock, LK_RELEASE, 0);
|
2000-10-04 01:29:17 +00:00
|
|
|
lockdestroy(&ncp->nc_lock);
|
1999-10-02 04:06:24 +00:00
|
|
|
while (ncp->nc_lwant) {
|
|
|
|
printf("lwant = %d\n", ncp->nc_lwant);
|
|
|
|
tsleep(&ncp->nc_lwant, PZERO,"ncpdr",2*hz);
|
|
|
|
}
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_locklist(LK_EXCLUSIVE, td);
|
2000-05-26 02:09:24 +00:00
|
|
|
SLIST_REMOVE(&conn_list, ncp, ncp_conn, nc_next);
|
1999-10-02 04:06:24 +00:00
|
|
|
ncp_conn_cnt--;
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlocklist(td);
|
2001-03-10 05:24:45 +00:00
|
|
|
if (ncp->li.user)
|
|
|
|
free(ncp->li.user, M_NCPDATA);
|
|
|
|
if (ncp->li.password)
|
|
|
|
free(ncp->li.password, M_NCPDATA);
|
1999-10-02 04:06:24 +00:00
|
|
|
crfree(ncp->nc_owner);
|
2008-10-23 15:53:51 +00:00
|
|
|
free(ncp, M_NCPDATA);
|
1999-10-02 04:06:24 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2001-03-10 05:24:45 +00:00
|
|
|
int
|
|
|
|
ncp_conn_reconnect(struct ncp_conn *ncp)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close opened sockets if any
|
|
|
|
*/
|
|
|
|
ncp_sock_disconnect(ncp);
|
|
|
|
error = ncp_sock_connect(ncp);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
error = ncp_ncp_connect(ncp);
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
error = ncp_renegotiate_connparam(ncp, NCP_DEFAULT_BUFSIZE, 0);
|
|
|
|
if (error == NWE_SIGNATURE_LEVEL_CONFLICT) {
|
|
|
|
printf("Unable to negotiate requested security level\n");
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
ncp_ncp_disconnect(ncp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#ifdef NCPBURST
|
|
|
|
error = ncp_burst_connect(ncp);
|
|
|
|
if (error) {
|
|
|
|
ncp_ncp_disconnect(ncp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-03-22 10:38:16 +00:00
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_login(struct ncp_conn *conn, struct thread *td, struct ucred *cred)
|
2001-03-22 10:38:16 +00:00
|
|
|
{
|
|
|
|
struct ncp_bindery_object user;
|
|
|
|
u_char ncp_key[8];
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = ncp_get_encryption_key(conn, ncp_key);
|
|
|
|
if (error) {
|
2001-12-10 08:09:49 +00:00
|
|
|
printf("%s: Warning: use unencrypted login\n", __func__);
|
2001-03-22 10:38:16 +00:00
|
|
|
error = ncp_login_unencrypted(conn, conn->li.objtype,
|
2003-02-26 21:25:55 +00:00
|
|
|
conn->li.user, conn->li.password, td, cred);
|
2001-03-22 10:38:16 +00:00
|
|
|
} else {
|
|
|
|
error = ncp_get_bindery_object_id(conn, conn->li.objtype,
|
2003-02-26 21:25:55 +00:00
|
|
|
conn->li.user, &user, td, cred);
|
2001-03-22 10:38:16 +00:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
error = ncp_login_encrypted(conn, &user, ncp_key,
|
2003-02-26 21:25:55 +00:00
|
|
|
conn->li.password, td, cred);
|
2001-03-22 10:38:16 +00:00
|
|
|
}
|
|
|
|
if (!error)
|
|
|
|
conn->flags |= NCPFL_LOGGED | NCPFL_WASLOGGED;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2003-02-26 21:25:55 +00:00
|
|
|
/*
|
|
|
|
* Lookup connection by handle, return a locked conn descriptor
|
1999-10-02 04:06:24 +00:00
|
|
|
*/
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_getbyref(int ref, struct thread *td, struct ucred *cred, int mode,
|
|
|
|
struct ncp_conn **connpp)
|
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
struct ncp_conn *ncp;
|
2003-02-26 21:25:55 +00:00
|
|
|
int error = 0;
|
1999-10-02 04:06:24 +00:00
|
|
|
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_locklist(LK_SHARED, td);
|
1999-10-02 04:06:24 +00:00
|
|
|
SLIST_FOREACH(ncp, &conn_list, nc_next)
|
|
|
|
if (ncp->nc_id == ref) break;
|
|
|
|
if (ncp == NULL) {
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlocklist(td);
|
1999-10-02 04:06:24 +00:00
|
|
|
return(EBADF);
|
|
|
|
}
|
2003-02-26 21:25:55 +00:00
|
|
|
error = ncp_conn_lock2(ncp, td, cred, mode);
|
1999-10-02 04:06:24 +00:00
|
|
|
if (!error)
|
|
|
|
*connpp = ncp;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* find attached, but not logged in connection to specified server
|
|
|
|
*/
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_getattached(struct ncp_conn_args *li, struct thread *td,
|
|
|
|
struct ucred *cred, int mode, struct ncp_conn **connpp)
|
|
|
|
{
|
|
|
|
struct ncp_conn *ncp, *ncp2 = NULL;
|
1999-10-02 04:06:24 +00:00
|
|
|
int error = 0;
|
|
|
|
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_locklist(LK_SHARED, td);
|
1999-10-02 04:06:24 +00:00
|
|
|
SLIST_FOREACH(ncp, &conn_list, nc_next) {
|
|
|
|
if ((ncp->flags & NCPFL_LOGGED) != 0 ||
|
2003-02-26 21:25:55 +00:00
|
|
|
strcmp(ncp->li.server,li->server) != 0 ||
|
1999-10-02 04:06:24 +00:00
|
|
|
ncp->li.saddr.sa_len != li->saddr.sa_len ||
|
1999-10-03 07:09:31 +00:00
|
|
|
bcmp(&ncp->li.saddr,&ncp->li.saddr,li->saddr.sa_len) != 0)
|
1999-10-02 04:06:24 +00:00
|
|
|
continue;
|
2003-02-26 21:25:55 +00:00
|
|
|
if (ncp_suser(cred) == 0 ||
|
1999-10-02 04:06:24 +00:00
|
|
|
cred->cr_uid == ncp->nc_owner->cr_uid)
|
|
|
|
break;
|
|
|
|
error = ncp_conn_access(ncp,cred,mode);
|
|
|
|
if (!error && ncp2 == NULL)
|
|
|
|
ncp2 = ncp;
|
|
|
|
}
|
|
|
|
if (ncp == NULL) ncp = ncp2;
|
|
|
|
if (ncp == NULL) {
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlocklist(td);
|
1999-10-02 04:06:24 +00:00
|
|
|
return(EBADF);
|
|
|
|
}
|
2003-02-26 21:25:55 +00:00
|
|
|
error = ncp_conn_lock2(ncp, td, cred, mode);
|
1999-10-02 04:06:24 +00:00
|
|
|
if (!error)
|
|
|
|
*connpp=ncp;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2003-02-26 21:25:55 +00:00
|
|
|
/*
|
1999-10-02 04:06:24 +00:00
|
|
|
* Lookup connection by server/user pair, return a locked conn descriptor.
|
2003-02-26 21:25:55 +00:00
|
|
|
* if li is NULL or server/user pair incomplete, try to select best connection
|
1999-10-02 04:06:24 +00:00
|
|
|
* based on owner.
|
|
|
|
* Connection selected in next order:
|
|
|
|
* 1. Try to search conn with ucred owner, if li is NULL also find a primary
|
|
|
|
* 2. If 1. fails try to get first suitable shared connection
|
|
|
|
* 3. If 2. fails then nothing can help to poor ucred owner
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_getbyli(struct ncp_conn_args *li, struct thread *td,
|
|
|
|
struct ucred *cred, int mode, struct ncp_conn **connpp)
|
|
|
|
{
|
|
|
|
struct ncp_conn *ncp, *ncp2 = NULL;
|
|
|
|
int error = 0, partial, haveserv;
|
1999-10-02 04:06:24 +00:00
|
|
|
|
|
|
|
partial = (li == NULL || li->server[0] == 0 || li->user == NULL);
|
|
|
|
haveserv = (li && li->server[0]);
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_locklist(LK_SHARED, td);
|
1999-10-02 04:06:24 +00:00
|
|
|
SLIST_FOREACH(ncp, &conn_list, nc_next) {
|
|
|
|
if (partial) {
|
|
|
|
if (cred->cr_uid == ncp->nc_owner->cr_uid) {
|
|
|
|
if (haveserv) {
|
|
|
|
if (strcmp(ncp->li.server,li->server) == 0)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (ncp->flags & NCPFL_PRIMARY)
|
|
|
|
break;
|
|
|
|
ncp2 = ncp;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strcmp(ncp->li.server,li->server) != 0 ||
|
|
|
|
ncp->li.user == NULL ||
|
|
|
|
strcmp(ncp->li.user,li->user) != 0)
|
|
|
|
continue;
|
|
|
|
if (cred->cr_uid == ncp->nc_owner->cr_uid)
|
|
|
|
break;
|
|
|
|
if (ncp_suser(cred) == 0)
|
|
|
|
ncp2 = ncp;
|
|
|
|
}
|
|
|
|
error = ncp_conn_access(ncp,cred,mode);
|
|
|
|
if (!error && ncp2 == NULL)
|
|
|
|
ncp2 = ncp;
|
|
|
|
}
|
|
|
|
if (ncp == NULL) ncp = ncp2;
|
|
|
|
if (ncp == NULL) {
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlocklist(td);
|
1999-10-02 04:06:24 +00:00
|
|
|
return(EBADF);
|
|
|
|
}
|
2003-02-26 21:25:55 +00:00
|
|
|
error = ncp_conn_lock2(ncp, td, cred,mode);
|
1999-10-02 04:06:24 +00:00
|
|
|
if (!error)
|
|
|
|
*connpp=ncp;
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set primary connection flag, since it have sence only for an owner,
|
|
|
|
* only owner can modify this flag.
|
|
|
|
* connection expected to be locked.
|
|
|
|
*/
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_setprimary(struct ncp_conn *conn, int on)
|
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
struct ncp_conn *ncp=NULL;
|
|
|
|
|
|
|
|
if (conn->ucred->cr_uid != conn->nc_owner->cr_uid)
|
|
|
|
return EACCES;
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_locklist(LK_SHARED, conn->td);
|
1999-10-02 04:06:24 +00:00
|
|
|
SLIST_FOREACH(ncp, &conn_list, nc_next) {
|
|
|
|
if (conn->ucred->cr_uid == ncp->nc_owner->cr_uid)
|
|
|
|
ncp->flags &= ~NCPFL_PRIMARY;
|
|
|
|
}
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlocklist(conn->td);
|
1999-10-02 04:06:24 +00:00
|
|
|
if (on)
|
|
|
|
conn->flags |= NCPFL_PRIMARY;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-02-26 21:25:55 +00:00
|
|
|
/*
|
1999-10-02 04:06:24 +00:00
|
|
|
* Lease conn to given proc, returning unique handle
|
|
|
|
* problem: how locks should be applied ?
|
|
|
|
*/
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_gethandle(struct ncp_conn *conn, struct thread *td, struct ncp_handle **handle)
|
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
struct ncp_handle *refp;
|
|
|
|
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&lhlock, LK_EXCLUSIVE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
SLIST_FOREACH(refp, &lhlist, nh_next)
|
2003-02-26 21:25:55 +00:00
|
|
|
if (refp->nh_conn == conn && td == refp->nh_td) break;
|
1999-10-02 04:06:24 +00:00
|
|
|
if (refp) {
|
|
|
|
conn->ref_cnt++;
|
|
|
|
refp->nh_ref++;
|
|
|
|
*handle = refp;
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&lhlock, LK_RELEASE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-10-23 15:53:51 +00:00
|
|
|
refp = malloc(sizeof(struct ncp_handle),M_NCPDATA,
|
2003-02-19 05:47:46 +00:00
|
|
|
M_WAITOK | M_ZERO);
|
1999-10-02 04:06:24 +00:00
|
|
|
SLIST_INSERT_HEAD(&lhlist,refp,nh_next);
|
|
|
|
refp->nh_ref++;
|
2003-02-26 21:25:55 +00:00
|
|
|
refp->nh_td = td;
|
1999-10-02 04:06:24 +00:00
|
|
|
refp->nh_conn = conn;
|
|
|
|
refp->nh_id = ncp_next_handle++;
|
|
|
|
*handle = refp;
|
|
|
|
conn->ref_cnt++;
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&lhlock, LK_RELEASE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* release reference, if force - ignore refcount
|
|
|
|
*/
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_puthandle(struct ncp_handle *handle, struct thread *td, int force)
|
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
struct ncp_handle *refp = handle;
|
|
|
|
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&lhlock, LK_EXCLUSIVE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
refp->nh_ref--;
|
|
|
|
refp->nh_conn->ref_cnt--;
|
|
|
|
if (force) {
|
|
|
|
refp->nh_conn->ref_cnt -= refp->nh_ref;
|
|
|
|
refp->nh_ref = 0;
|
|
|
|
}
|
|
|
|
if (refp->nh_ref == 0) {
|
2000-05-26 02:09:24 +00:00
|
|
|
SLIST_REMOVE(&lhlist, refp, ncp_handle, nh_next);
|
2008-10-23 15:53:51 +00:00
|
|
|
free(refp, M_NCPDATA);
|
1999-10-02 04:06:24 +00:00
|
|
|
}
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&lhlock, LK_RELEASE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* find a connHandle
|
|
|
|
*/
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_findhandle(int connHandle, struct thread *td, struct ncp_handle **handle) {
|
1999-10-02 04:06:24 +00:00
|
|
|
struct ncp_handle *refp;
|
|
|
|
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&lhlock, LK_SHARED, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
SLIST_FOREACH(refp, &lhlist, nh_next)
|
2003-02-26 21:25:55 +00:00
|
|
|
if (refp->nh_td == td && refp->nh_id == connHandle) break;
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&lhlock, LK_RELEASE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
if (refp == NULL) {
|
|
|
|
return EBADF;
|
|
|
|
}
|
|
|
|
*handle = refp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Clear handles associated with specified process
|
|
|
|
*/
|
|
|
|
int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_putprochandles(struct thread *td)
|
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
struct ncp_handle *hp, *nhp;
|
|
|
|
int haveone = 0;
|
|
|
|
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&lhlock, LK_EXCLUSIVE, 0);
|
2001-02-04 13:13:25 +00:00
|
|
|
for (hp = SLIST_FIRST(&lhlist); hp; hp = nhp) {
|
|
|
|
nhp = SLIST_NEXT(hp, nh_next);
|
2003-02-26 21:25:55 +00:00
|
|
|
if (hp->nh_td != td) continue;
|
1999-10-02 04:06:24 +00:00
|
|
|
haveone = 1;
|
|
|
|
hp->nh_conn->ref_cnt -= hp->nh_ref;
|
2000-05-26 02:09:24 +00:00
|
|
|
SLIST_REMOVE(&lhlist, hp, ncp_handle, nh_next);
|
2008-10-23 15:53:51 +00:00
|
|
|
free(hp, M_NCPDATA);
|
1999-10-02 04:06:24 +00:00
|
|
|
}
|
2008-01-24 12:34:30 +00:00
|
|
|
lockmgr(&lhlock, LK_RELEASE, 0);
|
1999-10-02 04:06:24 +00:00
|
|
|
return haveone;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* remove references in all possible connections,
|
|
|
|
* XXX - possible problem is a locked list.
|
|
|
|
*/
|
|
|
|
/*void
|
|
|
|
ncp_conn_list_rm_ref(pid_t pid) {
|
|
|
|
struct ncp_conn *ncp;
|
|
|
|
|
|
|
|
ncp_conn_locklist(LK_SHARED, NULL);
|
|
|
|
SLIST_FOREACH(ncp, &conn_list, nc_next) {
|
|
|
|
ncp_conn_rm_ref(ncp,pid,1);
|
|
|
|
}
|
|
|
|
ncp_conn_unlocklist(NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ncp_conn_getinfo(struct ncp_conn *ncp, struct ncp_conn_stat *ncs) {
|
|
|
|
bzero(ncs,sizeof(*ncs));
|
|
|
|
ncs->li = ncp->li;
|
|
|
|
ncs->li.user = ncs->user;
|
|
|
|
if (ncp->li.user)
|
|
|
|
strcpy(ncs->user, ncp->li.user);
|
|
|
|
ncs->li.password = NULL;
|
|
|
|
ncs->connRef = ncp->nc_id;
|
|
|
|
ncs->ref_cnt = ncp->ref_cnt;
|
|
|
|
ncs->connid = ncp->connid;
|
|
|
|
ncs->owner = ncp->nc_owner->cr_uid;
|
|
|
|
ncs->group = ncp->nc_group;
|
|
|
|
ncs->flags = ncp->flags;
|
|
|
|
ncs->buffer_size = ncp->buffer_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_sysctl_connstat(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
1999-10-02 04:06:24 +00:00
|
|
|
int error;
|
|
|
|
struct ncp_conn_stat ncs;
|
|
|
|
struct ncp_conn *ncp;
|
2003-03-20 21:17:40 +00:00
|
|
|
/* struct ucred *cred = req->td->td_ucred;*/
|
1999-10-02 04:06:24 +00:00
|
|
|
|
2004-02-26 00:27:04 +00:00
|
|
|
error = sysctl_wire_old_buffer(req, 0);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_locklist(LK_SHARED, req->td);
|
1999-10-02 04:06:24 +00:00
|
|
|
error = SYSCTL_OUT(req, &ncp_conn_cnt, sizeof(ncp_conn_cnt));
|
|
|
|
SLIST_FOREACH(ncp, &conn_list, nc_next) {
|
|
|
|
if (error) break;
|
|
|
|
/* I can't do conn_lock while list is locked */
|
|
|
|
ncp->nc_lwant++;
|
2006-01-14 11:40:32 +00:00
|
|
|
ncp_conn_getinfo(ncp, &ncs);
|
1999-10-02 04:06:24 +00:00
|
|
|
ncp->nc_lwant--;
|
|
|
|
error = SYSCTL_OUT(req, &ncs, sizeof(ncs));
|
|
|
|
}
|
2003-02-26 21:25:55 +00:00
|
|
|
ncp_conn_unlocklist(req->td);
|
1999-10-02 04:06:24 +00:00
|
|
|
return(error);
|
|
|
|
}
|