2008-03-26 16:23:12 +01:00
|
|
|
/* $NetBSD: svc_generic.c,v 1.3 2000/07/06 03:10:35 christos Exp $ */
|
|
|
|
|
2013-11-25 20:04:36 +01:00
|
|
|
/*-
|
2017-11-20 20:43:44 +01:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2013-11-25 20:04:36 +01:00
|
|
|
* Copyright (c) 2009, Sun Microsystems, 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:
|
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* - 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.
|
|
|
|
* - Neither the name of Sun Microsystems, Inc. nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
2008-03-26 16:23:12 +01:00
|
|
|
*
|
2013-11-25 20:04:36 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
|
2008-03-26 16:23:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1986-1991 by Sun Microsystems Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* svc_generic.c, Server side for RPC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opt_inet6.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sx.h>
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
|
2009-08-24 12:09:30 +02:00
|
|
|
#include <net/vnet.h>
|
|
|
|
|
2008-03-26 16:23:12 +01:00
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpc/rpcb_clnt.h>
|
|
|
|
#include <rpc/nettype.h>
|
|
|
|
|
2008-03-28 10:50:32 +01:00
|
|
|
#include <rpc/rpc_com.h>
|
2008-03-26 16:23:12 +01:00
|
|
|
|
|
|
|
extern int __svc_vc_setflag(SVCXPRT *, int);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The highest level interface for server creation.
|
|
|
|
* It tries for all the nettokens in that particular class of token
|
|
|
|
* and returns the number of handles it can create and/or find.
|
|
|
|
*
|
|
|
|
* It creates a link list of all the handles it could create.
|
|
|
|
* If svc_create() is called multiple times, it uses the handle
|
|
|
|
* created earlier instead of creating a new handle every time.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
svc_create(
|
|
|
|
SVCPOOL *pool,
|
|
|
|
void (*dispatch)(struct svc_req *, SVCXPRT *),
|
|
|
|
rpcprog_t prognum, /* Program number */
|
|
|
|
rpcvers_t versnum, /* Version number */
|
|
|
|
const char *nettype) /* Networktype token */
|
|
|
|
{
|
2014-06-08 13:19:32 +02:00
|
|
|
int g, num = 0;
|
|
|
|
SVCGROUP *grp;
|
2008-03-26 16:23:12 +01:00
|
|
|
SVCXPRT *xprt;
|
|
|
|
struct netconfig *nconf;
|
|
|
|
void *handle;
|
|
|
|
|
|
|
|
if ((handle = __rpc_setconf(nettype)) == NULL) {
|
|
|
|
printf("svc_create: unknown protocol");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
while ((nconf = __rpc_getconf(handle)) != NULL) {
|
2014-06-08 13:19:32 +02:00
|
|
|
for (g = 0; g < SVC_MAXGROUPS; g++) {
|
|
|
|
grp = &pool->sp_groups[g];
|
|
|
|
mtx_lock(&grp->sg_lock);
|
|
|
|
TAILQ_FOREACH(xprt, &grp->sg_xlist, xp_link) {
|
|
|
|
if (strcmp(xprt->xp_netid, nconf->nc_netid))
|
|
|
|
continue;
|
2008-03-26 16:23:12 +01:00
|
|
|
/* Found an old one, use it */
|
2014-06-08 13:19:32 +02:00
|
|
|
mtx_unlock(&grp->sg_lock);
|
2008-03-26 16:23:12 +01:00
|
|
|
(void) rpcb_unset(prognum, versnum, nconf);
|
|
|
|
if (svc_reg(xprt, prognum, versnum,
|
|
|
|
dispatch, nconf) == FALSE) {
|
|
|
|
printf(
|
|
|
|
"svc_create: could not register prog %u vers %u on %s\n",
|
|
|
|
(unsigned)prognum, (unsigned)versnum,
|
|
|
|
nconf->nc_netid);
|
2014-06-08 13:19:32 +02:00
|
|
|
mtx_lock(&grp->sg_lock);
|
2008-03-26 16:23:12 +01:00
|
|
|
} else {
|
|
|
|
num++;
|
2014-06-08 13:19:32 +02:00
|
|
|
mtx_lock(&grp->sg_lock);
|
2008-03-26 16:23:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-08 13:19:32 +02:00
|
|
|
mtx_unlock(&grp->sg_lock);
|
2008-03-26 16:23:12 +01:00
|
|
|
}
|
|
|
|
if (xprt == NULL) {
|
|
|
|
/* It was not found. Now create a new one */
|
|
|
|
xprt = svc_tp_create(pool, dispatch, prognum, versnum,
|
|
|
|
NULL, nconf);
|
2009-06-18 00:50:26 +02:00
|
|
|
if (xprt) {
|
2008-03-26 16:23:12 +01:00
|
|
|
num++;
|
2009-06-18 00:50:26 +02:00
|
|
|
SVC_RELEASE(xprt);
|
|
|
|
}
|
2008-03-26 16:23:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
__rpc_endconf(handle);
|
|
|
|
/*
|
|
|
|
* In case of num == 0; the error messages are generated by the
|
|
|
|
* underlying layers; and hence not needed here.
|
|
|
|
*/
|
|
|
|
return (num);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The high level interface to svc_tli_create().
|
|
|
|
* It tries to create a server for "nconf" and registers the service
|
|
|
|
* with the rpcbind. It calls svc_tli_create();
|
|
|
|
*/
|
|
|
|
SVCXPRT *
|
|
|
|
svc_tp_create(
|
|
|
|
SVCPOOL *pool,
|
|
|
|
void (*dispatch)(struct svc_req *, SVCXPRT *),
|
|
|
|
rpcprog_t prognum, /* Program number */
|
|
|
|
rpcvers_t versnum, /* Version number */
|
|
|
|
const char *uaddr, /* Address (or null for default) */
|
|
|
|
const struct netconfig *nconf) /* Netconfig structure for the network */
|
|
|
|
{
|
|
|
|
struct netconfig nconfcopy;
|
|
|
|
struct netbuf *taddr;
|
|
|
|
struct t_bind bind;
|
|
|
|
SVCXPRT *xprt;
|
|
|
|
|
|
|
|
if (nconf == NULL) {
|
|
|
|
printf(
|
|
|
|
"svc_tp_create: invalid netconfig structure for prog %u vers %u\n",
|
|
|
|
(unsigned)prognum, (unsigned)versnum);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (uaddr) {
|
|
|
|
taddr = uaddr2taddr(nconf, uaddr);
|
|
|
|
bind.addr = *taddr;
|
|
|
|
free(taddr, M_RPC);
|
2015-04-07 12:25:27 +02:00
|
|
|
bind.qlen = -1;
|
2008-03-26 16:23:12 +01:00
|
|
|
xprt = svc_tli_create(pool, NULL, nconf, &bind, 0, 0);
|
|
|
|
free(bind.addr.buf, M_RPC);
|
|
|
|
} else {
|
|
|
|
xprt = svc_tli_create(pool, NULL, nconf, NULL, 0, 0);
|
|
|
|
}
|
|
|
|
if (xprt == NULL) {
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
/*LINTED const castaway*/
|
|
|
|
nconfcopy = *nconf;
|
|
|
|
(void) rpcb_unset(prognum, versnum, &nconfcopy);
|
|
|
|
if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) {
|
|
|
|
printf(
|
|
|
|
"svc_tp_create: Could not register prog %u vers %u on %s\n",
|
|
|
|
(unsigned)prognum, (unsigned)versnum,
|
|
|
|
nconf->nc_netid);
|
Implement support for RPCSEC_GSS authentication to both the NFS client
and server. This replaces the RPC implementation of the NFS client and
server with the newer RPC implementation originally developed
(actually ported from the userland sunrpc code) to support the NFS
Lock Manager. I have tested this code extensively and I believe it is
stable and that performance is at least equal to the legacy RPC
implementation.
The NFS code currently contains support for both the new RPC
implementation and the older legacy implementation inherited from the
original NFS codebase. The default is to use the new implementation -
add the NFS_LEGACYRPC option to fall back to the old code. When I
merge this support back to RELENG_7, I will probably change this so
that users have to 'opt in' to get the new code.
To use RPCSEC_GSS on either client or server, you must build a kernel
which includes the KGSSAPI option and the crypto device. On the
userland side, you must build at least a new libc, mountd, mount_nfs
and gssd. You must install new versions of /etc/rc.d/gssd and
/etc/rc.d/nfsd and add 'gssd_enable=YES' to /etc/rc.conf.
As long as gssd is running, you should be able to mount an NFS
filesystem from a server that requires RPCSEC_GSS authentication. The
mount itself can happen without any kerberos credentials but all
access to the filesystem will be denied unless the accessing user has
a valid ticket file in the standard place (/tmp/krb5cc_<uid>). There
is currently no support for situations where the ticket file is in a
different place, such as when the user logged in via SSH and has
delegated credentials from that login. This restriction is also
present in Solaris and Linux. In theory, we could improve this in
future, possibly using Brooks Davis' implementation of variant
symlinks.
Supporting RPCSEC_GSS on a server is nearly as simple. You must create
service creds for the server in the form 'nfs/<fqdn>@<REALM>' and
install them in /etc/krb5.keytab. The standard heimdal utility ktutil
makes this fairly easy. After the service creds have been created, you
can add a '-sec=krb5' option to /etc/exports and restart both mountd
and nfsd.
The only other difference an administrator should notice is that nfsd
doesn't fork to create service threads any more. In normal operation,
there will be two nfsd processes, one in userland waiting for TCP
connections and one in the kernel handling requests. The latter
process will create as many kthreads as required - these should be
visible via 'top -H'. The code has some support for varying the number
of service threads according to load but initially at least, nfsd uses
a fixed number of threads according to the value supplied to its '-n'
option.
Sponsored by: Isilon Systems
MFC after: 1 month
2008-11-03 11:38:00 +01:00
|
|
|
xprt_unregister(xprt);
|
2009-06-18 00:50:26 +02:00
|
|
|
SVC_RELEASE(xprt);
|
2008-03-26 16:23:12 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (xprt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If so is NULL, then it opens a socket for the given transport
|
|
|
|
* provider (nconf cannot be NULL then). If the t_state is T_UNBND and
|
|
|
|
* bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
|
|
|
|
* NULL bindadr and Connection oriented transports, the value of qlen
|
|
|
|
* is set to 8.
|
|
|
|
*
|
|
|
|
* If sendsz or recvsz are zero, their default values are chosen.
|
|
|
|
*/
|
|
|
|
SVCXPRT *
|
|
|
|
svc_tli_create(
|
|
|
|
SVCPOOL *pool,
|
|
|
|
struct socket *so, /* Connection end point */
|
|
|
|
const struct netconfig *nconf, /* Netconfig struct for nettoken */
|
|
|
|
const struct t_bind *bindaddr, /* Local bind address */
|
|
|
|
size_t sendsz, /* Max sendsize */
|
|
|
|
size_t recvsz) /* Max recvsize */
|
|
|
|
{
|
|
|
|
SVCXPRT *xprt = NULL; /* service handle */
|
|
|
|
bool_t madeso = FALSE; /* whether so opened here */
|
|
|
|
struct __rpc_sockinfo si;
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
|
|
|
|
if (!so) {
|
|
|
|
if (nconf == NULL) {
|
|
|
|
printf("svc_tli_create: invalid netconfig\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
so = __rpc_nconf2socket(nconf);
|
|
|
|
if (!so) {
|
|
|
|
printf(
|
|
|
|
"svc_tli_create: could not open connection for %s\n",
|
|
|
|
nconf->nc_netid);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
__rpc_nconf2sockinfo(nconf, &si);
|
|
|
|
madeso = TRUE;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* It is an open socket. Get the transport info.
|
|
|
|
*/
|
|
|
|
if (!__rpc_socket2sockinfo(so, &si)) {
|
|
|
|
printf(
|
|
|
|
"svc_tli_create: could not get transport information\n");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the socket is unbound, try to bind it.
|
|
|
|
*/
|
|
|
|
if (madeso || !__rpc_sockisbound(so)) {
|
|
|
|
if (bindaddr == NULL) {
|
|
|
|
if (bindresvport(so, NULL)) {
|
|
|
|
memset(&ss, 0, sizeof ss);
|
|
|
|
ss.ss_family = si.si_af;
|
|
|
|
ss.ss_len = si.si_alen;
|
|
|
|
if (sobind(so, (struct sockaddr *)&ss,
|
|
|
|
curthread)) {
|
|
|
|
printf(
|
|
|
|
"svc_tli_create: could not bind to anonymous port\n");
|
|
|
|
goto freedata;
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 12:25:27 +02:00
|
|
|
solisten(so, -1, curthread);
|
2008-03-26 16:23:12 +01:00
|
|
|
} else {
|
|
|
|
if (bindresvport(so,
|
|
|
|
(struct sockaddr *)bindaddr->addr.buf)) {
|
|
|
|
printf(
|
|
|
|
"svc_tli_create: could not bind to requested address\n");
|
|
|
|
goto freedata;
|
|
|
|
}
|
|
|
|
solisten(so, (int)bindaddr->qlen, curthread);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* call transport specific function.
|
|
|
|
*/
|
|
|
|
switch (si.si_socktype) {
|
|
|
|
case SOCK_STREAM:
|
|
|
|
#if 0
|
|
|
|
slen = sizeof ss;
|
|
|
|
if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
|
|
|
|
== 0) {
|
|
|
|
/* accepted socket */
|
|
|
|
xprt = svc_fd_create(fd, sendsz, recvsz);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
xprt = svc_vc_create(pool, so, sendsz, recvsz);
|
|
|
|
if (!nconf || !xprt)
|
|
|
|
break;
|
|
|
|
#if 0
|
|
|
|
/* XXX fvdl */
|
|
|
|
if (strcmp(nconf->nc_protofmly, "inet") == 0 ||
|
|
|
|
strcmp(nconf->nc_protofmly, "inet6") == 0)
|
|
|
|
(void) __svc_vc_setflag(xprt, TRUE);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
xprt = svc_dg_create(pool, so, sendsz, recvsz);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("svc_tli_create: bad service type");
|
|
|
|
goto freedata;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xprt == NULL)
|
|
|
|
/*
|
|
|
|
* The error messages here are spitted out by the lower layers:
|
|
|
|
* svc_vc_create(), svc_fd_create() and svc_dg_create().
|
|
|
|
*/
|
|
|
|
goto freedata;
|
|
|
|
|
|
|
|
/* Fill in type of service */
|
|
|
|
xprt->xp_type = __rpc_socktype2seman(si.si_socktype);
|
|
|
|
|
|
|
|
if (nconf) {
|
|
|
|
xprt->xp_netid = strdup(nconf->nc_netid, M_RPC);
|
|
|
|
}
|
|
|
|
return (xprt);
|
|
|
|
|
|
|
|
freedata:
|
|
|
|
if (madeso)
|
|
|
|
(void)soclose(so);
|
|
|
|
if (xprt) {
|
|
|
|
if (!madeso) /* so that svc_destroy doesnt close fd */
|
|
|
|
xprt->xp_socket = NULL;
|
Implement support for RPCSEC_GSS authentication to both the NFS client
and server. This replaces the RPC implementation of the NFS client and
server with the newer RPC implementation originally developed
(actually ported from the userland sunrpc code) to support the NFS
Lock Manager. I have tested this code extensively and I believe it is
stable and that performance is at least equal to the legacy RPC
implementation.
The NFS code currently contains support for both the new RPC
implementation and the older legacy implementation inherited from the
original NFS codebase. The default is to use the new implementation -
add the NFS_LEGACYRPC option to fall back to the old code. When I
merge this support back to RELENG_7, I will probably change this so
that users have to 'opt in' to get the new code.
To use RPCSEC_GSS on either client or server, you must build a kernel
which includes the KGSSAPI option and the crypto device. On the
userland side, you must build at least a new libc, mountd, mount_nfs
and gssd. You must install new versions of /etc/rc.d/gssd and
/etc/rc.d/nfsd and add 'gssd_enable=YES' to /etc/rc.conf.
As long as gssd is running, you should be able to mount an NFS
filesystem from a server that requires RPCSEC_GSS authentication. The
mount itself can happen without any kerberos credentials but all
access to the filesystem will be denied unless the accessing user has
a valid ticket file in the standard place (/tmp/krb5cc_<uid>). There
is currently no support for situations where the ticket file is in a
different place, such as when the user logged in via SSH and has
delegated credentials from that login. This restriction is also
present in Solaris and Linux. In theory, we could improve this in
future, possibly using Brooks Davis' implementation of variant
symlinks.
Supporting RPCSEC_GSS on a server is nearly as simple. You must create
service creds for the server in the form 'nfs/<fqdn>@<REALM>' and
install them in /etc/krb5.keytab. The standard heimdal utility ktutil
makes this fairly easy. After the service creds have been created, you
can add a '-sec=krb5' option to /etc/exports and restart both mountd
and nfsd.
The only other difference an administrator should notice is that nfsd
doesn't fork to create service threads any more. In normal operation,
there will be two nfsd processes, one in userland waiting for TCP
connections and one in the kernel handling requests. The latter
process will create as many kthreads as required - these should be
visible via 'top -H'. The code has some support for varying the number
of service threads according to load but initially at least, nfsd uses
a fixed number of threads according to the value supplied to its '-n'
option.
Sponsored by: Isilon Systems
MFC after: 1 month
2008-11-03 11:38:00 +01:00
|
|
|
xprt_unregister(xprt);
|
2008-03-26 16:23:12 +01:00
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|