/*-
* 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.
*
* 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.
*/
/*
* Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
*/
reg = *regp;
/*
* check to see if already used
* find_service returns a hit even if
* the versions don't match, so check for it
*/
fnd = find_service(reg.r_prog, reg.r_vers, reg.r_netid);
if (fnd && (fnd->rpcb_map.r_vers == reg.r_vers)) {
if (!strcmp(fnd->rpcb_map.r_addr, reg.r_addr))
/*
* if these match then it is already
* registered so just say "OK".
*/
return (TRUE);
else
return (FALSE);
}
/*
* add to the end of the list
*/
rbl = malloc(sizeof(*rbl));
if (rbl == NULL)
return (FALSE);
a = &(rbl->rpcb_map);
a->r_prog = reg.r_prog;
a->r_vers = reg.r_vers;
a->r_netid = strdup(reg.r_netid);
a->r_addr = strdup(reg.r_addr);
a->r_owner = strdup(owner);
if (!a->r_addr || !a->r_netid || !a->r_owner) {
if (a->r_netid)
free(a->r_netid);
if (a->r_addr)
free(a->r_addr);
if (a->r_owner)
free(a->r_owner);
free(rbl);
return (FALSE);
}
rbl->rpcb_next = NULL;
if (list_rbl == NULL) {
list_rbl = rbl;
} else {
for (fnd = list_rbl; fnd->rpcb_next;
fnd = fnd->rpcb_next)
;
fnd->rpcb_next = rbl;
}
#ifdef PORTMAP
(void) add_pmaplist(regp);
#endif
return (TRUE);
}
#ifdef RPCBIND_DEBUG
if (debugging)
fprintf(stderr, "%s: RPCB_UNSET request for (%lu, %lu, %s): ",
__func__, (unsigned long)regp->r_prog,
(unsigned long)regp->r_vers, regp->r_netid);
#endif
ans = map_unset(regp, getowner(transp, owner, sizeof owner));
#ifdef RPCBIND_DEBUG
if (debugging)
fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
#endif
/* XXX: should have used some defined constant here */
rpcbs_unset(rpcbversnum - 2, ans);
return (void *)&ans;
}
bool_t
map_unset(RPCB *regp, const char *owner)
{
int ans = 0;
rpcblist_ptr rbl, prev, tmp;
if (owner == NULL)
return (0);
for (prev = NULL, rbl = list_rbl; rbl; /* cstyle */) {
if ((rbl->rpcb_map.r_prog != regp->r_prog) ||
(rbl->rpcb_map.r_vers != regp->r_vers) ||
(regp->r_netid[0] && strcasecmp(regp->r_netid,
rbl->rpcb_map.r_netid))) {
/* both rbl & prev move forwards */
prev = rbl;
rbl = rbl->rpcb_next;
continue;
}
/*
* Check whether appropriate uid. Unset only
* if superuser or the owner itself.
*/
if (strcmp(owner, rpcbind_superuser) &&
strcmp(rbl->rpcb_map.r_owner, owner))
return (0);
/* found it; rbl moves forward, prev stays */
ans = 1;
tmp = rbl;
rbl = rbl->rpcb_next;
if (prev == NULL)
list_rbl = rbl;
else
prev->rpcb_next = rbl;
free(tmp->rpcb_map.r_addr);
free(tmp->rpcb_map.r_netid);
free(tmp->rpcb_map.r_owner);
free(tmp);
}
#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);
}
if (uaddr != NULL && uaddr != nullstring) {
free(uaddr);
uaddr = NULL;
}
fnd = find_service(regp->r_prog, regp->r_vers, transp->xp_netid);
if (fnd && ((verstype == RPCB_ALLVERS) ||
(regp->r_vers == fnd->rpcb_map.r_vers))) {
if (*(regp->r_addr) != '\0') { /* may contain a hint about */
saddr = regp->r_addr; /* the interface that we */
} /* should use */
if (!(uaddr = mergeaddr(transp, transp->xp_netid,
fnd->rpcb_map.r_addr, saddr))) {
/* Try whatever we have */
uaddr = strdup(fnd->rpcb_map.r_addr);
} else if (!uaddr[0]) {
/*
* The server died. Unset all versions of this prog.
*/
delete_prog(regp->r_prog);
uaddr = nullstring;
}
} else {
uaddr = nullstring;
}
#ifdef RPCBIND_DEBUG
if (debugging)
fprintf(stderr, "%s: %s\n", __func__, uaddr);
#endif
/* XXX: should have used some defined constant here */
rpcbs_getaddr(rpcbversnum - 2, regp->r_prog, regp->r_vers,
transp->xp_netid, uaddr);
return (void *)&uaddr;
}
/*
* XDR remote call arguments. It ignores the address part.
* written for XDR_DECODE direction only
*/
static bool_t
xdr_rmtcall_args(XDR *xdrs, struct r_rmtcall_args *cap)
{
/* does not get the address or the arguments */
if (xdr_rpcprog(xdrs, &(cap->rmt_prog)) &&
xdr_rpcvers(xdrs, &(cap->rmt_vers)) &&
xdr_rpcproc(xdrs, &(cap->rmt_proc))) {
return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
}
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
xdr_rmtcall_result(XDR *xdrs, struct r_rmtcall_args *cap)
{
bool_t result;
#ifdef PORTMAP
if (cap->rmt_localvers == PMAPVERS) {
int h1, h2, h3, h4, p1, p2;
u_long port;
/* interpret the universal address for TCP/IP */
if (sscanf(cap->rmt_uaddr, "%d.%d.%d.%d.%d.%d",
&h1, &h2, &h3, &h4, &p1, &p2) != 6)
return (FALSE);
port = ((p1 & 0xff) << 8) + (p2 & 0xff);
result = xdr_u_long(xdrs, &port);
} else
#endif
if ((cap->rmt_localvers == RPCBVERS) ||
(cap->rmt_localvers == RPCBVERS4)) {
result = xdr_wrapstring(xdrs, &(cap->rmt_uaddr));
} else {
return (FALSE);
}
if (result == TRUE)
return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
return (FALSE);
}
/*
* only worries about the struct encap_parms part of struct r_rmtcall_args.
* The arglen must already be set!!
*/
static bool_t
xdr_opaque_parms(XDR *xdrs, struct r_rmtcall_args *cap)
{
return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
}
/*
* 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.
*/
#ifdef RPCBIND_DEBUG
if (debugging)
fprintf(stderr, "found at uaddr %s\n", rbl->rpcb_map.r_addr);
#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) {
uaddr = mergeaddr(transp, transp->xp_netid,
rbl->rpcb_map.r_addr, NULL);
if (uaddr == NULL || uaddr[0] == '\0') {
svcerr_noprog(transp);
free(uaddr);
goto error;
}
free(uaddr);
}
nconf = rpcbind_get_conf(transp->xp_netid);
if (nconf == NULL) {
if (reply_type == RPCBPROC_INDIRECT)
svcerr_systemerr(transp);
if (debugging)
fprintf(stderr, "%s: rpcbind_get_conf failed\n",
__func__);
goto error;
}
localsa = local_sa(((struct sockaddr *)caller->buf)->sa_family);
if (localsa == NULL) {
if (debugging)
fprintf(stderr, "%s: no local address\n", __func__);
goto error;
}
tbuf.len = tbuf.maxlen = localsa->sa_len;
tbuf.buf = localsa;
local_uaddr =
addrmerge(&tbuf, rbl->rpcb_map.r_addr, NULL, nconf->nc_netid);
m_uaddr = addrmerge(caller, rbl->rpcb_map.r_addr, NULL,
nconf->nc_netid);
#ifdef RPCBIND_DEBUG
if (debugging)
fprintf(stderr, "%s: merged uaddr %s\n", __func__, m_uaddr);
#endif
if ((fd = find_rmtcallfd_by_netid(nconf->nc_netid)) == -1) {
if (reply_type == RPCBPROC_INDIRECT)
svcerr_systemerr(transp);
goto error;
}
xidp = __rpcb_get_dg_xidp(transp);
switch (forward_register(*xidp, caller, fd, m_uaddr, reply_type,
versnum, &call_msg.rm_xid)) {
case 1:
/* Success; forward_register() will free m_uaddr for us. */
m_uaddr = NULL;
break;
case 0:
/*
* A duplicate request for the slow server. Let's not
* beat on it any more.
*/
if (debugging)
fprintf(stderr, "%s: duplicate request\n", __func__);
goto error;
case -1:
/* forward_register failed. Perhaps no memory. */
if (debugging)
fprintf(stderr, "%s: forward_register failed\n",
__func__);
goto error;
}
#ifdef DEBUG_RMTCALL
if (debugging)
fprintf(stderr, "%s: original XID %x, new XID %x\n", __func__,
*xidp, call_msg.rm_xid);
#endif
call_msg.rm_direction = CALL;
call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
call_msg.rm_call.cb_prog = a.rmt_prog;
call_msg.rm_call.cb_vers = a.rmt_vers;
if (sendsz > RPC_BUF_MAX) {
#ifdef notyet
outbuf_alloc = alloca(sendsz); /* not in IDR2? */
#else
outbuf_alloc = malloc(sendsz);
#endif /* notyet */
if (outbuf_alloc == NULL) {
if (reply_type == RPCBPROC_INDIRECT)
svcerr_systemerr(transp);
syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
goto error;
}
xdrmem_create(&outxdr, outbuf_alloc, sendsz, XDR_ENCODE);
} else {
xdrmem_create(&outxdr, outbuf, sendsz, XDR_ENCODE);
}
if (!xdr_callhdr(&outxdr, &call_msg)) {
if (reply_type == RPCBPROC_INDIRECT)
svcerr_systemerr(transp);
if (debugging)
fprintf(stderr, "%s: xdr_callhdr failed\n", __func__);
goto error;
}
if (!xdr_u_int32_t(&outxdr, &(a.rmt_proc))) {
if (reply_type == RPCBPROC_INDIRECT)
svcerr_systemerr(transp);
if (debugging)
fprintf(stderr, "%s: xdr_u_long failed\n", __func__);
goto error;
}
if (rqstp->rq_cred.oa_flavor == AUTH_NULL) {
auth = authnone_create();
} else if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
struct authunix_parms *au;
au = (struct authunix_parms *)rqstp->rq_clntcred;
auth = authunix_create(au->aup_machname,
au->aup_uid, au->aup_gid,
au->aup_len, au->aup_gids);
if (auth == NULL) /* fall back */
auth = authnone_create();
} else {
/* we do not support any other authentication scheme */
if (debugging)
fprintf(stderr, "%s: oa_flavor != AUTH_NONE and"
" oa_flavor != AUTH_SYS\n", __func__);
if (reply_type == RPCBPROC_INDIRECT)
svcerr_weakauth(transp); /* XXX too strong.. */
goto error;
}
if (auth == NULL) {
if (reply_type == RPCBPROC_INDIRECT)
svcerr_systemerr(transp);
if (debugging)
fprintf(stderr,
"%s: authwhatever_create returned NULL\n",
__func__);
goto error;
}
if (!AUTH_MARSHALL(auth, &outxdr)) {
if (reply_type == RPCBPROC_INDIRECT)
svcerr_systemerr(transp);
AUTH_DESTROY(auth);
if (debugging)
fprintf(stderr,
"%s: AUTH_MARSHALL failed\n", __func__);
goto error;
}
AUTH_DESTROY(auth);
if (!xdr_opaque_parms(&outxdr, &a)) {
if (reply_type == RPCBPROC_INDIRECT)
svcerr_systemerr(transp);
if (debugging)
fprintf(stderr, "%s: xdr_opaque_parms failed\n",
__func__);
goto error;
}
outlen = (int) XDR_GETPOS(&outxdr);
if (outbuf_alloc)
outbufp = outbuf_alloc;
else
outbufp = outbuf;
na = uaddr2taddr(nconf, local_uaddr);
if (!na) {
if (reply_type == RPCBPROC_INDIRECT)
svcerr_systemerr(transp);
goto error;
}
error:
if (call_msg.rm_xid != 0)
(void) free_slot_by_xid(call_msg.rm_xid);
out:
if (local_uaddr)
free(local_uaddr);
if (buf_alloc)
free(buf_alloc);
if (outbuf_alloc)
free(outbuf_alloc);
if (na) {
free(na->buf);
free(na);
}
if (m_uaddr != NULL)
free(m_uaddr);
}
/*
* Makes an entry into the FIFO for the given request.
* Returns 1 on success, 0 if this is a duplicate request, or -1 on error.
* *callxidp is set to the xid of the call.
*/
static int
forward_register(u_int32_t caller_xid, struct netbuf *caller_addr,
int forward_fd, char *uaddr, rpcproc_t reply_type,
rpcvers_t versnum, u_int32_t *callxidp)
{
int i;
int j = 0;
time_t min_time, time_now;
static u_int32_t lastxid;
int entry = -1;
static int
free_slot_by_index(int idx)
{
struct finfo *fi;
fi = &FINFO[idx];
if (fi->flag & FINFO_ACTIVE) {
netbuffree(fi->caller_addr);
/* XXX may be too big, but can't access xprt array here */
if (fi->forward_fd >= *svc_fdset_getmax())
(*svc_fdset_getmax())--;
free(fi->uaddr);
fi->flag &= ~FINFO_ACTIVE;
rpcb_rmtcalls--;
return (1);
}
return (0);
}
void
my_svc_run(void)
{
size_t nfds;
struct pollfd *pollfds;
int npollfds, newfdcount;
int poll_ret, check_ret;
int n, *m;
#ifdef SVC_RUN_DEBUG
size_t i;
#endif
struct pollfd *p;
pollfds = NULL;
npollfds = 0;
for (;;) {
newfdcount = svc_fdset_getsize(0);
if (newfdcount != npollfds) {
if (reallocarr(&pollfds,
newfdcount, sizeof(*pollfds)) != 0) {
wait:
syslog(LOG_ERR, "Cannot allocate pollfds");
sleep(1);
continue;
}
npollfds = newfdcount;
}
p = pollfds;
if ((m = svc_fdset_getmax()) == NULL)
goto wait;
for (n = 0; n <= *m; n++) {
if (svc_fdset_isset(n)) {
p->fd = n;
p->events = MASKVAL;
p++;
}
}
nfds = p - pollfds;
poll_ret = 0;
#ifdef SVC_RUN_DEBUG
if (debugging > 1) {
fprintf(stderr, "%s: polling for read on fd < ",
__func__);
for (i = 0, p = pollfds; i < nfds; i++, p++)
if (p->events)
fprintf(stderr, "%d ", p->fd);
fprintf(stderr, ">\n");
}
#endif
#ifdef RPCBIND_RUMP
poll_ret = rump_sys_poll(pollfds, nfds, 30 * 1000);
#else
poll_ret = poll(pollfds, nfds, 30 * 1000);
#endif
switch (poll_ret) {
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().
*/
#ifdef SVC_RUN_DEBUG
if (debugging > 1) {
fprintf(stderr, "%s: poll returned %d (%s)\n",
__func__, poll_ret, strerror(errno));
}
#endif
/* FALLTHROUGH */
case 0:
__svc_clean_idle(NULL, 30, FALSE);
continue;
default:
#ifdef SVC_RUN_DEBUG
if (debugging) {
fprintf(stderr, "%s: poll returned read fds < ",
__func__);
for (i = 0, p = pollfds; i < nfds; i++, p++)
if (p->revents)
fprintf(stderr, "%d (%#x)",
p->fd, p->revents);
fprintf(stderr, ">\n");
}
#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.
*/
if ((check_ret = check_rmtcalls(pollfds, nfds)) ==
poll_ret)
continue;
svc_getreq_poll(pollfds, poll_ret-check_ret);
}
#ifdef SVC_RUN_DEBUG
if (debugging) {
fprintf(stderr, "%s: svc_maxfd now %u\n", __func__,
*svc_fdset_getmax());
}
#endif
}
}
static int
check_rmtcalls(struct pollfd *pfds, int nfds)
{
int j, ncallbacks_found = 0, rmtcalls_pending;
SVCXPRT *xprt;
/*
* 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
find_service(rpcprog_t prog, rpcvers_t vers, char *netid)
{
rpcblist_ptr hit = NULL;
rpcblist_ptr rbl;
for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
if ((rbl->rpcb_map.r_prog != prog) ||
((rbl->rpcb_map.r_netid != NULL) &&
(strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
continue;
hit = rbl;
if (rbl->rpcb_map.r_vers == vers)
break;
}
return (hit);
}
/*
* Copies the name associated with the uid of the caller and returns
* a pointer to it. Similar to getwd().
*/
static char *
getowner(SVCXPRT *transp, char *owner, size_t ownersize)
{
struct sockcred *sc;
#ifdef PORTMAP
/*
* Add this to the pmap list only if it is UDP or TCP.
*/
static int
add_pmaplist(RPCB *arg)
{
struct pmap pmap;
struct pmaplist *pml;
int h1, h2, h3, h4, p1, p2;
if (strcmp(arg->r_netid, udptrans) == 0) {
/* It is UDP! */
pmap.pm_prot = IPPROTO_UDP;
} else if (strcmp(arg->r_netid, tcptrans) == 0) {
/* It is TCP */
pmap.pm_prot = IPPROTO_TCP;
} else
/* Not an IP protocol */
return (0);
/* interpret the universal address for TCP/IP */
if (sscanf(arg->r_addr, "%d.%d.%d.%d.%d.%d",
&h1, &h2, &h3, &h4, &p1, &p2) != 6)
return (0);
pmap.pm_port = ((p1 & 0xff) << 8) + (p2 & 0xff);
pmap.pm_prog = arg->r_prog;
pmap.pm_vers = arg->r_vers;
/*
* add to END of list
*/
pml = malloc(sizeof(*pml));
if (pml == NULL) {
syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
return (1);
}
pml->pml_map = pmap;
pml->pml_next = NULL;
if (list_pml == NULL) {
list_pml = pml;
} else {
struct pmaplist *fnd;
/* Attach to the end of the list */
for (fnd = list_pml; fnd->pml_next; fnd = fnd->pml_next)
continue;
fnd->pml_next = pml;
}
return (0);
}
/*
* Delete this from the pmap list only if it is UDP or TCP.
*/
static int
del_pmaplist(RPCB *arg)
{
struct pmaplist *pml;
struct pmaplist *prevpml, *fnd;
unsigned long prot;
if (strcmp(arg->r_netid, udptrans) == 0) {
/* It is UDP! */
prot = IPPROTO_UDP;
} else if (strcmp(arg->r_netid, tcptrans) == 0) {
/* It is TCP */
prot = IPPROTO_TCP;
} else if (arg->r_netid[0] == 0) {
prot = 0; /* Remove all occurrences */
} else {
/* Not an IP protocol */
return (0);
}
for (prevpml = NULL, pml = list_pml; pml; /* cstyle */) {
if ((pml->pml_map.pm_prog != arg->r_prog) ||
(pml->pml_map.pm_vers != arg->r_vers) ||
(prot && (pml->pml_map.pm_prot != prot))) {
/* both pml & prevpml move forwards */
prevpml = pml;
pml = pml->pml_next;
continue;
}
/* found it; pml moves forward, prevpml stays */
fnd = pml;
pml = pml->pml_next;
if (prevpml == NULL)
list_pml = pml;
else
prevpml->pml_next = pml;
free(fnd);
}
return (0);
}
#endif /* PORTMAP */