rpcb_svc_com.c revision 36e852a172cba914383d7341c988128b2c667fbd
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
/*
* The commom server procedure for the rpcbind.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <rpc/rpcb_prot.h>
#include <rpcsvc/svc_dg_priv.h>
#include <netconfig.h>
#include <errno.h>
#include <zone.h>
#ifdef PORTMAP
#include <rpc/pmap_prot.h>
#endif /* PORTMAP */
#include <syslog.h>
#include <netdir.h>
#include <ucred.h>
#include <alloca.h>
#include <rpc/key_prot.h>
#include <rpcsvc/yppasswd.h>
#include "rpcbind.h"
static bool_t xdr_opaque_parms();
char *getowner();
static ulong_t forward_register();
static void handle_reply();
static int netbufcmp();
static int free_slot_by_xid();
static int free_slot_by_index();
static int check_rmtcalls();
static void netbuffree();
static void find_versions();
static rpcblist_ptr find_service();
static int add_pmaplist(RPCB *);
int del_pmaplist(RPCB *);
void delete_rbl(rpcblist_ptr);
static char *nullstring = "";
static int rpcb_rmtcalls;
/*
* Set a mapping of program, version, netid
*/
/* ARGSUSED */
bool_t *
int rpcbversnum;
{
char owner[64];
#ifdef RPCBIND_DEBUG
#endif
#ifdef RPCBIND_DEBUG
#endif
/* XXX: should have used some defined constant here */
return (&ans);
}
char *owner;
{
/*
* check to see if already used
* find_service returns a hit even if
* the versions don't match, so check for it
*/
/*
* if these match then it is already
* registered so just say "OK".
*/
return (TRUE);
else {
/*
* Check if server is up. If so, return FALSE.
* If not, cleanup old registrations for the
* program and register the new server.
*/
return (FALSE);
}
}
/*
* add to the end of the list
*/
return (FALSE);
}
return (FALSE);
}
} else {
;
}
#ifdef PORTMAP
(void) add_pmaplist(regp);
#endif
return (TRUE);
}
/*
* Unset a mapping of program, version, netid
*/
/* ARGSUSED */
bool_t *
int rpcbversnum;
{
char owner[64];
#ifdef RPCBIND_DEBUG
#endif
#ifdef RPCBIND_DEBUG
#endif
/* XXX: should have used some defined constant here */
return (&ans);
}
char *owner;
{
#ifdef PORTMAP
int ans = 0;
#endif
return (0);
/* prev moves forwards */
continue;
}
/*
* Check whether appropriate uid. Unset only
* if superuser or the owner itself.
*/
return (0);
/* prev stays */
#ifdef PORTMAP
ans = 1;
#endif
else
}
#ifdef PORTMAP
if (ans)
(void) del_pmaplist(regp);
#endif
/*
* We return 1 either when the entry was not there or it
* was able to unset it. It can come to this point only if
* at least one of the conditions is true.
*/
return (1);
}
void
{
}
void
unsigned long prog;
{
continue;
}
#ifdef PORTMAP
#endif
else
}
}
/*ARGSUSED*/
char **
{
static char *uaddr;
/*
* There is a potential window at startup during which rpcbind
* service has been established over IPv6 but not over IPv4. If an
* IPv4 request comes in during that window, the IP code will map
* it into IPv6. We could patch up the request so that it looks
* like IPv4 (so that rpcbind returns an IPv4 uaddr to the caller),
* but that requires some non-trivial code and it's hard to test.
* Instead, drop the request on the floor and force the caller to
* retransmit. By the time rpcbind sees the retransmission, IPv4
* service should be in place and it should see the request as
* IPv4, as desired.
*/
struct sockaddr_in6 *rmtaddr;
"IPv4 GETADDR request mapped to IPv6: ignoring");
return (NULL);
}
}
} /* should use */
/* Try whatever we have */
} else if (!uaddr[0]) {
/*
* The server died. Unset all versions of this prog.
*/
uaddr = nullstring;
}
} else {
uaddr = nullstring;
}
#ifdef RPCBIND_DEBUG
#endif
/* XXX: should have used some defined constant here */
return (&uaddr);
}
/* VARARGS */
ulong_t *
{
}
/*
* Convert uaddr to taddr. Should be used only by
*/
/* ARGSUSED */
struct netbuf *
char **uaddrp;
int rpcbversnum; /* Not used here */
{
if (taddr) {
}
return (&nbuf);
}
return (taddr);
}
/*
* Convert taddr to uaddr. Should be used only by
*/
/* ARGSUSED */
char **
int rpcbversnum; /* unused */
{
static char *uaddr;
#ifdef CHEW_FDS
int fd;
return (&uaddr);
}
#endif /* CHEW_FDS */
uaddr = nullstring;
}
return (&uaddr);
}
/*
* Stuff for the rmtcall service
*/
struct encap_parms {
char *args;
};
static bool_t
struct encap_parms *epp;
{
}
struct r_rmtcall_args {
int rmt_localvers; /* whether to send port # or uaddr */
char *rmt_uaddr;
struct encap_parms rmt_args;
};
/*
* XDR remote call arguments. It ignores the address part.
* written for XDR_DECODE direction only
*/
static bool_t
register struct r_rmtcall_args *cap;
{
/* does not get the address or the arguments */
}
return (FALSE);
}
/*
* XDR remote call results along with the address. Ignore
* program number, version number and proc number.
* Written for XDR_ENCODE direction only.
*/
static bool_t
register struct r_rmtcall_args *cap;
{
#ifdef PORTMAP
return (FALSE);
} else
#endif
} else {
return (FALSE);
}
return (FALSE);
}
/*
* only worries about the struct encap_parms part of struct r_rmtcall_args.
* The arglen must already be set!!
*/
static bool_t
struct r_rmtcall_args *cap;
{
}
struct rmtcallfd_list {
int fd;
char *netid;
struct rmtcallfd_list *next;
};
static struct rmtcallfd_list *rmthead;
static struct rmtcallfd_list *rmttail;
int
{
int fd;
struct rmtcallfd_list *rmt;
if (debugging)
"create_rmtcall_fd: couldn't open \"%s\" (errno %d, t_errno %d)\n",
return (-1);
}
(struct t_bind *)0) == -1) {
if (debugging)
"create_rmtcall_fd: couldn't bind to fd for \"%s\" (errno %d, t_errno %d)\n",
return (-1);
}
if (debugging)
"create_rmtcall_fd: svc_tli_create failed\n");
return (-1);
}
sizeof (struct rmtcallfd_list));
return (-1);
}
} else {
}
#if defined(DEBUG_RMTCALL) && defined(PORTMAP)
if (debugging) {
struct sockaddr_in *sin;
"create_rmtcall_fd %d, port %d\n",
}
#endif
return (fd);
}
static int
char *netid;
{
struct rmtcallfd_list *rmt;
}
}
return (-1);
}
static SVCXPRT *
int fd;
{
struct rmtcallfd_list *rmt;
}
}
return (NULL);
}
/*
* Call a remote procedure service. This procedure is very quiet when things
* go wrong. The proc is written to support broadcast rpc. In the broadcast
* case, a machine should shut-up instead of complain, lest the requestor be
* overrun with complaints at the expense of not hearing a valid reply.
* When receiving a request and verifying that the service exists, we
*
* receive the request
*
* open a new TLI endpoint on the same transport on which we received
* the original request
*
* remember the original request's XID (which requires knowing the format
* of the svc_dg_data structure)
*
* forward the request, with a new XID, to the requested service,
* remembering the XID used to send this request (for later use in
* reassociating the answer with the original request), the requestor's
* address, the file descriptor on which the forwarded request is
* made and the service's address.
*
* mark the file descriptor on which we anticipate receiving a reply from
* the service and one to select for in our private svc_run procedure
*
* At some time in the future, a reply will be received from the service to
* which we forwarded the request. At that time, we detect that the socket
* used was for forwarding (by looking through the finfo structures to see
* whether the fd corresponds to one of those) and call handle_reply() to
*
* receive the reply
*
* bundle the reply, along with the service's universal address
*
* create a SVCXPRT structure and use a version of svc_sendreply
* that allows us to specify the reply XID and destination, send the reply
* to the original requestor.
*/
/*
*/
void
{
register rpcblist_ptr rbl;
struct r_rmtcall_args a;
char *outbuf_alloc = NULL;
struct t_unitdata tu_data;
struct svc_dg_data *bd;
int outlen;
int fd = -1;
char *uaddr;
struct nd_mergearg ma;
int stat;
if (reply_type == RPCBPROC_INDIRECT)
return;
}
return; /* Only datagram type accepted */
if (sendsz == 0) { /* data transfer not supported */
if (reply_type == RPCBPROC_INDIRECT)
return;
}
/*
* Should be multiple of 4 for XDR.
*/
if (sendsz > RPC_BUF_MAX) {
#ifdef notyet
#else
#endif /* notyet */
if (debugging)
"rpcbproc_callit_com: No Memory!\n");
if (reply_type == RPCBPROC_INDIRECT)
return;
}
} else {
}
if (reply_type == RPCBPROC_INDIRECT)
if (debugging)
"rpcbproc_callit_com: svc_getargs failed\n");
goto error;
}
if (!allow_indirect)
goto error;
#ifdef RPCBIND_DEBUG
if (uaddr)
#endif
/*
* Disallow calling rpcbind for certain procedures.
* Allow calling NULLPROC - per man page on rpcb_rmtcall().
* switch is in alphabetical order.
*/
switch (a.rmt_prog) {
case KEY_PROG:
if (debugging)
"rpcbind: rejecting KEY_PROG(%d)\n",
a.rmt_proc);
goto error;
case MOUNTPROG:
if (a.rmt_proc != MOUNTPROC_MNT)
break;
/*
* In Solaris 2.6, the host-based accesss control
* is done by the NFS server on each request.
* Prior to 2.6 we rely on mountd.
*/
if (debugging)
"rpcbind: rejecting MOUNTPROG(%d)\n",
a.rmt_proc);
goto error;
case NFS_ACL_PROGRAM:
if (debugging)
"rpcbind: rejecting NFS_ACL_PROGRAM(%d)\n",
a.rmt_proc);
goto error;
case NFS_PROGRAM:
/* also NFS3_PROGRAM */
if (debugging)
"rpcbind: rejecting NFS_PROGRAM(%d)\n",
a.rmt_proc);
goto error;
case RPCBPROG:
/*
* Disallow calling rpcbind for certain procedures.
* procedure numbers. So, will not check for those.
*/
switch (a.rmt_proc) {
case RPCBPROC_SET:
case RPCBPROC_UNSET:
case RPCBPROC_CALLIT:
case RPCBPROC_INDIRECT:
if (reply_type == RPCBPROC_INDIRECT)
if (debugging)
"rpcbproc_callit_com: calling RPCBPROG procs SET, UNSET, CALLIT, or INDIRECT \
not allowed \n");
goto error;
default:
/*
* Ideally, we should have called rpcb_service()
* or pmap_service() with appropriate parameters
* instead of going about in a roundabout
* manner. Hopefully, this case should happen
* rarely.
*/
break;
}
break;
case RQUOTAPROG:
if (debugging)
"rpcbind: rejecting RQUOTAPROG(%d)\n",
a.rmt_proc);
goto error;
case YPPASSWDPROG:
if (debugging)
"rpcbind: rejecting YPPASSWDPROG(%d)\n",
a.rmt_proc);
goto error;
case YPU_PROG:
if (debugging)
"rpcbind: rejecting YPU_PROG(%d)\n",
a.rmt_proc);
goto error;
case YPBINDPROG:
if (a.rmt_proc != YPBINDPROC_SETDOM)
break;
if (debugging)
"rpcbind: rejecting YPBINDPROG(%d)\n",
a.rmt_proc);
goto error;
case YPPROG:
switch (a.rmt_proc) {
case YPPROC_FIRST:
case YPPROC_NEXT:
case YPPROC_MATCH:
case YPPROC_ALL:
if (debugging)
"rpcbind: rejecting YPPROG(%d)\n",
a.rmt_proc);
goto error;
default:
break;
}
break;
default:
break;
}
}
#ifdef RPCBIND_DEBUG
#endif
if (reply_type == RPCBPROC_INDIRECT)
goto error;
}
#ifdef RPCBIND_DEBUG
#endif
if (reply_type == RPCBPROC_INDIRECT) {
}
goto error;
}
#ifdef RPCBIND_DEBUG
#endif
/*
* Check whether this entry is valid and a server is present
* Mergeaddr() returns NULL if no such entry is present, and
* returns "" if the entry was present but the server is not
* present (i.e., it crashed).
*/
if (reply_type == RPCBPROC_INDIRECT) {
goto error;
} else {
}
}
if (reply_type == RPCBPROC_INDIRECT)
if (debugging)
"rpcbproc_callit_com: rpcbind_get_conf failed\n");
goto error;
}
/*
* A mergeaddr operation allocates a string, which it stores in
* ma.m_uaddr. It's passed to forward_register() and is
* eventually freed by free_slot_*().
*/
if (stat)
#ifdef ND_DEBUG
"rpcbproc_callit_com: s_uaddr = %s, c_uaddr = %s, merged m_uaddr = %s\n",
#endif
if (reply_type == RPCBPROC_INDIRECT)
goto error;
}
/*
* A duplicate request for the slow server. Let's not
* beat on it any more.
*/
if (debugging)
"rpcbproc_callit_com: duplicate request\n");
goto error;
/* forward_register failed. Perhaps no memory. */
if (debugging)
"rpcbproc_callit_com: forward_register failed\n");
goto error;
}
#ifdef DEBUG_RMTCALL
"rpcbproc_callit_com: original XID %x, new XID %x\n",
#endif
if (sendsz > RPC_BUF_MAX) {
#ifdef notyet
#else
#endif /* notyet */
if (outbuf_alloc == NULL) {
if (reply_type == RPCBPROC_INDIRECT)
if (debugging)
"rpcbproc_callit_com: No memory!\n");
goto error;
}
} else {
}
if (reply_type == RPCBPROC_INDIRECT)
if (debugging)
"rpcbproc_callit_com: xdr_callhdr failed\n");
goto error;
}
if (reply_type == RPCBPROC_INDIRECT)
if (debugging)
"rpcbproc_callit_com: xdr_u_long failed\n");
goto error;
}
auth = authnone_create();
struct authsys_parms *au;
auth = authnone_create();
} else {
/* we do not support any other authentication scheme */
if (debugging)
"rpcbproc_callit_com: oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
if (reply_type == RPCBPROC_INDIRECT)
goto error;
}
if (reply_type == RPCBPROC_INDIRECT)
if (debugging)
"rpcbproc_callit_com: authwhatever_create returned NULL\n");
goto error;
}
if (reply_type == RPCBPROC_INDIRECT)
if (debugging)
"rpcbproc_callit_com: AUTH_MARSHALL failed\n");
goto error;
}
if (!xdr_opaque_parms(&outxdr, &a)) {
if (reply_type == RPCBPROC_INDIRECT)
if (debugging)
"rpcbproc_callit_com: xdr_opaque_parms failed\n");
goto error;
}
if (outbuf_alloc)
else
if (!na) {
if (reply_type == RPCBPROC_INDIRECT)
goto error;
}
if (debugging)
"rpcbproc_callit_com: t_sndudata failed: t_errno %d, errno %d\n",
if (reply_type == RPCBPROC_INDIRECT)
goto error;
}
goto out;
out:
if (buf_alloc)
if (outbuf_alloc)
free((void *) outbuf_alloc);
if (na)
}
#define NFORWARD 64
struct finfo {
int flag;
#define FINFO_ACTIVE 0x1
struct netbuf *caller_addr;
int forward_fd;
char *uaddr;
};
/*
* Makes an entry into the FIFO for the given request.
* If duplicate request, returns a 0, else returns the xid of its call.
*/
static ulong_t
struct netbuf *caller_addr;
int forward_fd;
char *uaddr;
{
int i;
int j = 0;
int entry = -1;
/*
* initialization: once this has happened, lastxid will
* - always be a multiple of NFORWARD (which has to be a power of 2),
* - never be 0 again,
* - never be (ulong_t)(-NFORWARD)
* when entering or returning from this function.
*/
if (lastxid == 0) {
/*
* avoid lastxid wraparound to 0,
* and generating a forward_xid of -1
*/
}
/*
* Check if it is an duplicate entry. Then,
* try to find an empty slot. If not available, then
* use the slot with the earliest time.
*/
for (i = 0; i < NFORWARD; i++) {
caller_addr))) {
return (0); /* Duplicate entry */
} else {
/* Should we wait any longer */
(void) free_slot_by_index(i);
}
}
if (entry == -1) {
entry = i;
j = i;
}
}
}
if (entry != -1) {
/* use this empty slot */
j = entry;
} else {
(void) free_slot_by_index(j);
}
return ((ulong_t)-1);
}
rpcb_rmtcalls++; /* no of pending calls */
/*
* Though uaddr is not allocated here, it will still be freed
* from free_slot_*().
*/
/* avoid lastxid wraparound to 0, and generating a forward_xid of -1 */
}
static struct finfo *
char *uaddr;
{
int i;
if (i < 0)
i += NFORWARD;
return (&FINFO[i]);
}
return (NULL);
}
static int
char *uaddr;
{
int entry;
if (entry < 0)
return (free_slot_by_index(entry));
}
return (0);
}
static int
int index;
{
return (1);
}
return (0);
}
static int
{
}
static struct netbuf *
{
if (np) {
}
return (np);
}
static void
{
}
/*
* active_fd is used to determine whether an entry in svc_pollfd is:
* 1. not a forward fd (should be polled)
* 2. an active forward fd (should be polled)
* 3. an inactive forward fd (should not be polled)
*/
static bool_t
int fd;
{
int i;
return (TRUE);
if (rpcb_rmtcalls == 0)
return (FALSE);
for (i = 0; i < NFORWARD; i++)
/* Should we wait any longer */
(void) free_slot_by_index(i);
else
return (TRUE);
}
}
return (FALSE);
}
void
{
#ifdef SVC_RUN_DEBUG
int i;
#endif
register struct pollfd *p;
for (;;) {
{
register int n; /* loop counter */
/*
* compress the sparse svc_pollfd strcutre
* into pollfds
*/
p = pollfds;
n++, in++) {
p->revents = 0;
p++;
}
}
}
poll_ret = 0;
#ifdef SVC_RUN_DEBUG
if (debugging) {
if (p->events)
}
#endif
case -1:
/*
* We ignore all errors, continuing with the assumption
* that it was set by the signal handlers (or any
* other outside event) and not caused by poll().
* If it was our refresh signal, call the refresh
* function.
*/
if (sigrefresh) {
sigrefresh = 0;
}
case 0:
continue;
default:
#ifdef SVC_RUN_DEBUG
if (debugging) {
if (p->revents)
}
#endif
/*
* If we found as many replies on callback fds
* as the number of descriptors selectable which
* poll() returned, there can be no more so we
* don't call svc_getreq_poll. Otherwise, there
* must be another so we must call svc_getreq_poll.
*/
continue;
}
}
}
static int
int nfds;
{
int j, ncallbacks_found = 0;
/*
* This fd will not be polled if rpcb_rmtcalls == 0
*/
if (rpcb_rmtcalls == 0)
return (0);
for (j = 0; j < nfds; j++) {
#ifdef DEBUG_RMTCALL
if (debugging)
"my_svc_run: polled on forwarding fd %d, netid %s - calling handle_reply\n",
#endif
}
}
}
return (ncallbacks_found);
}
static void
{
struct svc_dg_data *bd;
}
/*
* Call svcerr_systemerr() only if RPCBVERS4
*/
static void
{
}
}
static void
int fd;
{
struct rpc_err reply_error;
char *buffer;
struct r_rmtcall_args a;
#ifdef SVC_RUN_DEBUG
if (debugging)
#endif
goto done;
}
/*
* If this fd is not active on the forward list, ignore it
* If the svc_pollfd structure has multiple settings
* of the same fd, then it will enter handle_reply() for the first one,
* set FINFO_ACTIVE false and then get another call to handle_reply()
* with the same, now inactive, fd.
*/
for (i = 0; i < NFORWARD; i++) {
break;
}
if (i == NFORWARD) {
#ifdef SVC_RUN_DEBUG
if (debugging) {
}
#endif
return;
}
if (debugging)
"handle_reply: t_alloc T_UNITDATA failed\n");
goto done;
}
do {
int moreflag;
moreflag = 0;
errno = 0;
/* Drop this packet - we have no more space. */
if (debugging)
"handle_reply: recvd packet with T_MORE flag set\n");
goto done;
}
if (res < 0) {
if (debugging)
"handle_reply: t_rcvudata returned %d, t_errno TLOOK\n", res);
}
if (debugging)
"handle_reply: t_rcvudata returned %d, t_errno %d, errno %d\n",
goto done;
}
goto done;
#ifdef DEBUG_MORE
if (debugging)
"handle_reply: t_rcvudata received %d-byte packet from %s\n",
#endif
goto done;
}
if (debugging)
"handle_reply: xdr_replymsg failed\n");
goto done;
}
goto done;
#ifdef SVC_RUN_DEBUG
if (debugging) {
}
#endif
if (debugging)
goto done;
}
/* XXX hack */
#ifdef SVC_RUN_DEBUG
if (uaddr)
if (debugging) {
}
#endif
done:
if (uaddr)
if (tr_data)
#ifdef SVC_RUN_DEBUG
if (debugging) {
}
#endif
} else
}
static void
char *netid; /* Transport Provider token */
{
register rpcblist_ptr rbl;
int lowv = 0;
int highv = 0;
continue;
if (lowv == 0) {
}
}
}
/*
* returns the item with the given program, version number and netid.
* If that version number is not found, it returns the item with that
* program number, so that address is now returned to the caller. The
* caller when makes a call to this program, version number, the call
* will fail and it will return with PROGVERS_MISMATCH. The user can
* then determine the highest and the lowest version number for this
* program using clnt_geterr() and use those program version numbers.
*
* Returns the RPCBLIST for the given prog, vers and netid
*/
static rpcblist_ptr
char *netid; /* Transport Provider token */
{
register rpcblist_ptr rbl;
continue;
break;
}
return (hit);
}
/*
* If the caller is from our zone and we know
* who it is, we return the uid.
*/
{
return (-1);
} else {
return (ucred_geteuid(uc));
}
}
/*
* Copies the name associated with the uid of the caller and returns
* a pointer to it. Similar to getwd().
*/
char *
char *owner;
{
switch (uid) {
case -1:
case 0:
default:
return (owner);
}
}
#ifdef PORTMAP
/*
* Add this to the pmap list only if it is UDP or TCP.
*/
static int
{
/* It is UDP! */
/* It is TCP */
} else
/* Not a IP protocol */
return (0);
return (0);
/*
* add to END of list
*/
return (1);
}
} else {
/* Attach to the end of the list */
;
}
return (0);
}
/*
* Delete this from the pmap list only if it is UDP or TCP.
*/
int
{
long prot;
/* It is UDP! */
prot = IPPROTO_UDP;
/* It is TCP */
prot = IPPROTO_TCP;
prot = 0; /* Remove all occurrences */
} else {
/* Not a IP protocol */
return (0);
}
/* both pml & prevpml move forwards */
continue;
}
/* found it; pml moves forward, prevpml stays */
else
}
return (0);
}
#endif /* PORTMAP */