Home | History | Annotate | Line # | Download | only in rpcbind
rpcb_svc_com.c revision 1.23
      1  1.23     kamil /*	$NetBSD: rpcb_svc_com.c,v 1.23 2020/06/17 00:16:22 kamil Exp $	*/
      2  1.20  christos /*	$FreeBSD: head/usr.sbin/rpcbind/rpcb_svc_com.c 301770 2016-06-09 22:25:00Z pfg $ */
      3   1.1      fvdl 
      4  1.20  christos /*-
      5  1.20  christos  * Copyright (c) 2009, Sun Microsystems, Inc.
      6  1.20  christos  * All rights reserved.
      7  1.20  christos  *
      8  1.20  christos  * Redistribution and use in source and binary forms, with or without
      9  1.20  christos  * modification, are permitted provided that the following conditions are met:
     10  1.20  christos  * - Redistributions of source code must retain the above copyright notice,
     11  1.20  christos  *   this list of conditions and the following disclaimer.
     12  1.20  christos  * - Redistributions in binary form must reproduce the above copyright notice,
     13  1.20  christos  *   this list of conditions and the following disclaimer in the documentation
     14  1.20  christos  *   and/or other materials provided with the distribution.
     15  1.20  christos  * - Neither the name of Sun Microsystems, Inc. nor the names of its
     16  1.20  christos  *   contributors may be used to endorse or promote products derived
     17  1.20  christos  *   from this software without specific prior written permission.
     18  1.20  christos  *
     19  1.20  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     20  1.20  christos  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.20  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.20  christos  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
     23  1.20  christos  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.20  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.20  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.20  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.20  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.20  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.20  christos  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      fvdl  */
     31   1.1      fvdl /*
     32   1.1      fvdl  * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
     33   1.1      fvdl  */
     34   1.1      fvdl 
     35   1.1      fvdl /* #ident	"@(#)rpcb_svc_com.c	1.18	94/05/02 SMI" */
     36   1.1      fvdl 
     37   1.1      fvdl /*
     38   1.1      fvdl  * rpcb_svc_com.c
     39   1.1      fvdl  * The commom server procedure for the rpcbind.
     40   1.1      fvdl  */
     41   1.1      fvdl 
     42   1.1      fvdl #include <sys/types.h>
     43   1.1      fvdl #include <sys/stat.h>
     44   1.1      fvdl #include <sys/param.h>
     45   1.1      fvdl #include <sys/socket.h>
     46   1.1      fvdl #include <rpc/rpc.h>
     47   1.1      fvdl #include <rpc/rpcb_prot.h>
     48  1.19  christos #include <rpc/rpc_com.h>
     49  1.20  christos #include <assert.h>
     50   1.1      fvdl #include <netconfig.h>
     51   1.1      fvdl #include <errno.h>
     52   1.1      fvdl #include <syslog.h>
     53   1.1      fvdl #include <unistd.h>
     54   1.1      fvdl #include <stdio.h>
     55   1.7    itojun #include <poll.h>
     56   1.1      fvdl #ifdef PORTMAP
     57   1.1      fvdl #include <netinet/in.h>
     58   1.1      fvdl #include <rpc/pmap_prot.h>
     59   1.1      fvdl #endif /* PORTMAP */
     60   1.1      fvdl #include <string.h>
     61   1.1      fvdl #include <stdlib.h>
     62   1.1      fvdl 
     63  1.17  christos #ifdef RPCBIND_RUMP
     64  1.17  christos #include <rump/rump.h>
     65  1.23     kamil #include <rump/rump_syscallshotgun.h>
     66  1.17  christos #include <rump/rump_syscalls.h>
     67  1.17  christos #endif
     68  1.17  christos 
     69  1.20  christos #include "svc_dg.h"
     70   1.1      fvdl #include "rpcbind.h"
     71  1.17  christos #ifdef RPCBIND_RUMP
     72  1.17  christos #include "svc_fdset.h"
     73  1.17  christos #endif
     74   1.1      fvdl 
     75   1.1      fvdl #define RPC_BUF_MAX	65536	/* can be raised if required */
     76   1.1      fvdl 
     77  1.20  christos static char nullstring[] = "";
     78   1.1      fvdl static int rpcb_rmtcalls;
     79   1.1      fvdl 
     80   1.1      fvdl struct rmtcallfd_list {
     81   1.1      fvdl 	int fd;
     82   1.1      fvdl 	SVCXPRT *xprt;
     83   1.1      fvdl 	char *netid;
     84   1.1      fvdl 	struct rmtcallfd_list *next;
     85   1.1      fvdl };
     86   1.1      fvdl 
     87   1.1      fvdl #define NFORWARD        64
     88   1.1      fvdl #define MAXTIME_OFF     300     /* 5 minutes */
     89   1.1      fvdl 
     90   1.1      fvdl struct finfo {
     91   1.1      fvdl 	int             flag;
     92   1.1      fvdl #define FINFO_ACTIVE    0x1
     93   1.1      fvdl 	u_int32_t       caller_xid;
     94   1.1      fvdl         struct netbuf   *caller_addr;
     95   1.1      fvdl 	u_int32_t       forward_xid;
     96   1.1      fvdl 	int             forward_fd;
     97   1.1      fvdl 	char            *uaddr;
     98   1.1      fvdl 	rpcproc_t       reply_type;
     99   1.1      fvdl 	rpcvers_t       versnum;
    100   1.1      fvdl 	time_t          time;
    101   1.1      fvdl };
    102   1.1      fvdl static struct finfo     FINFO[NFORWARD];
    103   1.1      fvdl 
    104   1.1      fvdl 
    105  1.11  christos static bool_t xdr_encap_parms(XDR *, struct encap_parms *);
    106  1.11  christos static bool_t xdr_rmtcall_args(XDR *, struct r_rmtcall_args *);
    107  1.11  christos static bool_t xdr_rmtcall_result(XDR *, struct r_rmtcall_args *);
    108  1.11  christos static bool_t xdr_opaque_parms(XDR *, struct r_rmtcall_args *);
    109  1.11  christos static int find_rmtcallfd_by_netid(char *);
    110  1.11  christos static SVCXPRT *find_rmtcallxprt_by_fd(int);
    111  1.20  christos static int forward_register(u_int32_t, struct netbuf *, int, char *,
    112  1.20  christos     rpcproc_t, rpcvers_t, u_int32_t *);
    113  1.11  christos static struct finfo *forward_find(u_int32_t);
    114  1.11  christos static int free_slot_by_xid(u_int32_t);
    115  1.11  christos static int free_slot_by_index(int);
    116  1.11  christos static int netbufcmp(struct netbuf *, struct netbuf *);
    117  1.11  christos static struct netbuf *netbufdup(struct netbuf *);
    118  1.11  christos static void netbuffree(struct netbuf *);
    119  1.11  christos static int check_rmtcalls(struct pollfd *, int);
    120  1.11  christos static void xprt_set_caller(SVCXPRT *, struct finfo *);
    121  1.11  christos static void send_svcsyserr(SVCXPRT *, struct finfo *);
    122  1.11  christos static void handle_reply(int, SVCXPRT *);
    123  1.11  christos static void find_versions(rpcprog_t, char *, rpcvers_t *, rpcvers_t *);
    124  1.11  christos static rpcblist_ptr find_service(rpcprog_t, rpcvers_t, char *);
    125  1.11  christos static char *getowner(SVCXPRT *, char *, size_t);
    126  1.11  christos static int add_pmaplist(RPCB *);
    127  1.11  christos static int del_pmaplist(RPCB *);
    128   1.1      fvdl 
    129   1.1      fvdl /*
    130   1.1      fvdl  * Set a mapping of program, version, netid
    131   1.1      fvdl  */
    132   1.1      fvdl /* ARGSUSED */
    133   1.1      fvdl void *
    134  1.20  christos rpcbproc_set_com(void *arg, struct svc_req *rqstp __unused, SVCXPRT *transp,
    135   1.1      fvdl 		 rpcvers_t rpcbversnum)
    136   1.1      fvdl {
    137  1.20  christos 	RPCB *regp = arg;
    138   1.1      fvdl 	static bool_t ans;
    139   1.1      fvdl 	char owner[64];
    140   1.1      fvdl 
    141   1.1      fvdl #ifdef RPCBIND_DEBUG
    142   1.1      fvdl 	if (debugging)
    143   1.1      fvdl 		fprintf(stderr, "RPCB_SET request for (%lu, %lu, %s, %s) : ",
    144   1.1      fvdl 		    (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
    145   1.1      fvdl 		    regp->r_netid, regp->r_addr);
    146   1.1      fvdl #endif
    147   1.1      fvdl 	ans = map_set(regp, getowner(transp, owner, sizeof owner));
    148   1.1      fvdl #ifdef RPCBIND_DEBUG
    149   1.1      fvdl 	if (debugging)
    150   1.1      fvdl 		fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
    151   1.1      fvdl #endif
    152   1.1      fvdl 	/* XXX: should have used some defined constant here */
    153   1.1      fvdl 	rpcbs_set(rpcbversnum - 2, ans);
    154   1.1      fvdl 	return (void *)&ans;
    155   1.1      fvdl }
    156   1.1      fvdl 
    157   1.1      fvdl bool_t
    158   1.1      fvdl map_set(RPCB *regp, char *owner)
    159   1.1      fvdl {
    160   1.1      fvdl 	RPCB reg, *a;
    161   1.1      fvdl 	rpcblist_ptr rbl, fnd;
    162   1.1      fvdl 
    163   1.1      fvdl 	reg = *regp;
    164   1.1      fvdl 	/*
    165   1.1      fvdl 	 * check to see if already used
    166   1.1      fvdl 	 * find_service returns a hit even if
    167   1.1      fvdl 	 * the versions don't match, so check for it
    168   1.1      fvdl 	 */
    169   1.1      fvdl 	fnd = find_service(reg.r_prog, reg.r_vers, reg.r_netid);
    170   1.1      fvdl 	if (fnd && (fnd->rpcb_map.r_vers == reg.r_vers)) {
    171   1.1      fvdl 		if (!strcmp(fnd->rpcb_map.r_addr, reg.r_addr))
    172   1.1      fvdl 			/*
    173   1.1      fvdl 			 * if these match then it is already
    174   1.1      fvdl 			 * registered so just say "OK".
    175   1.1      fvdl 			 */
    176   1.1      fvdl 			return (TRUE);
    177   1.1      fvdl 		else
    178   1.1      fvdl 			return (FALSE);
    179   1.1      fvdl 	}
    180   1.1      fvdl 	/*
    181   1.1      fvdl 	 * add to the end of the list
    182   1.1      fvdl 	 */
    183  1.21  christos 	rbl = malloc(sizeof(*rbl));
    184  1.20  christos 	if (rbl == NULL)
    185   1.1      fvdl 		return (FALSE);
    186   1.1      fvdl 	a = &(rbl->rpcb_map);
    187   1.1      fvdl 	a->r_prog = reg.r_prog;
    188   1.1      fvdl 	a->r_vers = reg.r_vers;
    189   1.1      fvdl 	a->r_netid = strdup(reg.r_netid);
    190   1.1      fvdl 	a->r_addr = strdup(reg.r_addr);
    191   1.1      fvdl 	a->r_owner = strdup(owner);
    192   1.1      fvdl 	if (!a->r_addr || !a->r_netid || !a->r_owner) {
    193   1.1      fvdl 		if (a->r_netid)
    194  1.20  christos 			free(a->r_netid);
    195   1.1      fvdl 		if (a->r_addr)
    196  1.20  christos 			free(a->r_addr);
    197   1.1      fvdl 		if (a->r_owner)
    198  1.20  christos 			free(a->r_owner);
    199  1.20  christos 		free(rbl);
    200   1.1      fvdl 		return (FALSE);
    201   1.1      fvdl 	}
    202  1.16    plunky 	rbl->rpcb_next = NULL;
    203   1.1      fvdl 	if (list_rbl == NULL) {
    204   1.1      fvdl 		list_rbl = rbl;
    205   1.1      fvdl 	} else {
    206   1.1      fvdl 		for (fnd = list_rbl; fnd->rpcb_next;
    207   1.1      fvdl 			fnd = fnd->rpcb_next)
    208   1.1      fvdl 			;
    209   1.1      fvdl 		fnd->rpcb_next = rbl;
    210   1.1      fvdl 	}
    211   1.1      fvdl #ifdef PORTMAP
    212   1.1      fvdl 	(void) add_pmaplist(regp);
    213   1.1      fvdl #endif
    214   1.1      fvdl 	return (TRUE);
    215   1.1      fvdl }
    216   1.1      fvdl 
    217   1.1      fvdl /*
    218   1.1      fvdl  * Unset a mapping of program, version, netid
    219   1.1      fvdl  */
    220   1.1      fvdl /* ARGSUSED */
    221   1.1      fvdl void *
    222  1.20  christos rpcbproc_unset_com(void *arg, struct svc_req *rqstp __unused, SVCXPRT *transp,
    223   1.1      fvdl 		   rpcvers_t rpcbversnum)
    224   1.1      fvdl {
    225  1.20  christos 	RPCB *regp = arg;
    226   1.1      fvdl 	static bool_t ans;
    227   1.1      fvdl 	char owner[64];
    228   1.1      fvdl 
    229   1.1      fvdl #ifdef RPCBIND_DEBUG
    230   1.1      fvdl 	if (debugging)
    231   1.1      fvdl 		fprintf(stderr, "RPCB_UNSET request for (%lu, %lu, %s) : ",
    232   1.1      fvdl 		    (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
    233   1.1      fvdl 		    regp->r_netid);
    234   1.1      fvdl #endif
    235   1.1      fvdl 	ans = map_unset(regp, getowner(transp, owner, sizeof owner));
    236   1.1      fvdl #ifdef RPCBIND_DEBUG
    237   1.1      fvdl 	if (debugging)
    238   1.1      fvdl 		fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
    239   1.1      fvdl #endif
    240   1.1      fvdl 	/* XXX: should have used some defined constant here */
    241   1.1      fvdl 	rpcbs_unset(rpcbversnum - 2, ans);
    242   1.1      fvdl 	return (void *)&ans;
    243   1.1      fvdl }
    244   1.1      fvdl 
    245   1.1      fvdl bool_t
    246  1.11  christos map_unset(RPCB *regp, const char *owner)
    247   1.1      fvdl {
    248   1.1      fvdl 	int ans = 0;
    249   1.1      fvdl 	rpcblist_ptr rbl, prev, tmp;
    250   1.1      fvdl 
    251   1.1      fvdl 	if (owner == NULL)
    252   1.1      fvdl 		return (0);
    253   1.1      fvdl 
    254   1.1      fvdl 	for (prev = NULL, rbl = list_rbl; rbl; /* cstyle */) {
    255   1.1      fvdl 		if ((rbl->rpcb_map.r_prog != regp->r_prog) ||
    256   1.1      fvdl 			(rbl->rpcb_map.r_vers != regp->r_vers) ||
    257   1.1      fvdl 			(regp->r_netid[0] && strcasecmp(regp->r_netid,
    258   1.1      fvdl 				rbl->rpcb_map.r_netid))) {
    259   1.1      fvdl 			/* both rbl & prev move forwards */
    260   1.1      fvdl 			prev = rbl;
    261   1.1      fvdl 			rbl = rbl->rpcb_next;
    262   1.1      fvdl 			continue;
    263   1.1      fvdl 		}
    264   1.1      fvdl 		/*
    265   1.1      fvdl 		 * Check whether appropriate uid. Unset only
    266   1.1      fvdl 		 * if superuser or the owner itself.
    267   1.1      fvdl 		 */
    268  1.12  christos 		if (strcmp(owner, rpcbind_superuser) &&
    269   1.1      fvdl 			strcmp(rbl->rpcb_map.r_owner, owner))
    270   1.1      fvdl 			return (0);
    271   1.1      fvdl 		/* found it; rbl moves forward, prev stays */
    272   1.1      fvdl 		ans = 1;
    273   1.1      fvdl 		tmp = rbl;
    274   1.1      fvdl 		rbl = rbl->rpcb_next;
    275   1.1      fvdl 		if (prev == NULL)
    276   1.1      fvdl 			list_rbl = rbl;
    277   1.1      fvdl 		else
    278   1.1      fvdl 			prev->rpcb_next = rbl;
    279  1.20  christos 		free(tmp->rpcb_map.r_addr);
    280  1.20  christos 		free(tmp->rpcb_map.r_netid);
    281  1.20  christos 		free(tmp->rpcb_map.r_owner);
    282  1.20  christos 		free(tmp);
    283   1.1      fvdl 	}
    284   1.1      fvdl #ifdef PORTMAP
    285   1.1      fvdl 	if (ans)
    286   1.1      fvdl 		(void) del_pmaplist(regp);
    287   1.1      fvdl #endif
    288   1.1      fvdl 	/*
    289   1.1      fvdl 	 * We return 1 either when the entry was not there or it
    290   1.1      fvdl 	 * was able to unset it.  It can come to this point only if
    291   1.1      fvdl 	 * atleast one of the conditions is true.
    292   1.1      fvdl 	 */
    293   1.1      fvdl 	return (1);
    294   1.1      fvdl }
    295   1.1      fvdl 
    296   1.1      fvdl void
    297  1.14     lukem delete_prog(rpcprog_t prog)
    298   1.1      fvdl {
    299   1.1      fvdl 	RPCB reg;
    300  1.17  christos 	rpcblist_ptr rbl;
    301   1.1      fvdl 
    302   1.1      fvdl 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
    303   1.1      fvdl 		if ((rbl->rpcb_map.r_prog != prog))
    304   1.1      fvdl 			continue;
    305   1.1      fvdl 		if (is_bound(rbl->rpcb_map.r_netid, rbl->rpcb_map.r_addr))
    306   1.1      fvdl 			continue;
    307   1.1      fvdl 		reg.r_prog = rbl->rpcb_map.r_prog;
    308   1.1      fvdl 		reg.r_vers = rbl->rpcb_map.r_vers;
    309   1.1      fvdl 		reg.r_netid = strdup(rbl->rpcb_map.r_netid);
    310  1.22  christos 		if (reg.r_netid == NULL)
    311  1.22  christos 			syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
    312  1.22  christos 		else {
    313  1.22  christos 			(void)map_unset(&reg, rpcbind_superuser);
    314  1.22  christos 			free(reg.r_netid);
    315  1.22  christos 		}
    316   1.1      fvdl 	}
    317   1.1      fvdl }
    318   1.1      fvdl 
    319   1.1      fvdl void *
    320  1.20  christos rpcbproc_getaddr_com(RPCB *regp, struct svc_req *rqstp __unused,
    321  1.20  christos     SVCXPRT *transp, rpcvers_t rpcbversnum, rpcvers_t verstype)
    322   1.1      fvdl {
    323   1.1      fvdl 	static char *uaddr;
    324   1.1      fvdl 	char *saddr = NULL;
    325   1.1      fvdl 	rpcblist_ptr fnd;
    326   1.1      fvdl 
    327  1.20  christos 	if (uaddr != NULL && uaddr != nullstring) {
    328  1.20  christos 		free(uaddr);
    329  1.20  christos 		uaddr = NULL;
    330  1.20  christos 	}
    331   1.1      fvdl 	fnd = find_service(regp->r_prog, regp->r_vers, transp->xp_netid);
    332   1.1      fvdl 	if (fnd && ((verstype == RPCB_ALLVERS) ||
    333   1.1      fvdl 		    (regp->r_vers == fnd->rpcb_map.r_vers))) {
    334   1.1      fvdl 		if (*(regp->r_addr) != '\0') {  /* may contain a hint about */
    335   1.1      fvdl 			saddr = regp->r_addr;   /* the interface that we    */
    336   1.1      fvdl 		}				/* should use */
    337   1.1      fvdl 		if (!(uaddr = mergeaddr(transp, transp->xp_netid,
    338   1.1      fvdl 				fnd->rpcb_map.r_addr, saddr))) {
    339   1.1      fvdl 			/* Try whatever we have */
    340   1.1      fvdl 			uaddr = strdup(fnd->rpcb_map.r_addr);
    341   1.1      fvdl 		} else if (!uaddr[0]) {
    342   1.1      fvdl 			/*
    343   1.1      fvdl 			 * The server died.  Unset all versions of this prog.
    344   1.1      fvdl 			 */
    345   1.1      fvdl 			delete_prog(regp->r_prog);
    346  1.20  christos 			uaddr = nullstring;
    347   1.1      fvdl 		}
    348   1.1      fvdl 	} else {
    349  1.20  christos 		uaddr = nullstring;
    350   1.1      fvdl 	}
    351   1.1      fvdl #ifdef RPCBIND_DEBUG
    352   1.1      fvdl 	if (debugging)
    353   1.1      fvdl 		fprintf(stderr, "getaddr: %s\n", uaddr);
    354   1.1      fvdl #endif
    355   1.1      fvdl 	/* XXX: should have used some defined constant here */
    356   1.1      fvdl 	rpcbs_getaddr(rpcbversnum - 2, regp->r_prog, regp->r_vers,
    357   1.1      fvdl 		transp->xp_netid, uaddr);
    358   1.1      fvdl 	return (void *)&uaddr;
    359   1.1      fvdl }
    360   1.1      fvdl 
    361   1.1      fvdl /* ARGSUSED */
    362   1.1      fvdl void *
    363  1.20  christos rpcbproc_gettime_com(void *arg __unused, struct svc_req *rqstp __unused,
    364  1.20  christos     SVCXPRT *transp __unused, rpcvers_t rpcbversnum __unused)
    365   1.1      fvdl {
    366   1.1      fvdl 	static time_t curtime;
    367   1.1      fvdl 
    368   1.1      fvdl 	(void) time(&curtime);
    369  1.20  christos 	return &curtime;
    370   1.1      fvdl }
    371   1.1      fvdl 
    372   1.1      fvdl /*
    373   1.1      fvdl  * Convert uaddr to taddr. Should be used only by
    374   1.1      fvdl  * local servers/clients. (kernel level stuff only)
    375   1.1      fvdl  */
    376   1.1      fvdl /* ARGSUSED */
    377   1.1      fvdl void *
    378  1.20  christos rpcbproc_uaddr2taddr_com(void *arg, struct svc_req *rqstp __unused,
    379  1.20  christos     SVCXPRT *transp, rpcvers_t rpcbversnum __unused)
    380   1.1      fvdl {
    381  1.20  christos 	char **uaddrp = arg;
    382   1.1      fvdl 	struct netconfig *nconf;
    383   1.1      fvdl 	static struct netbuf nbuf;
    384   1.1      fvdl 	static struct netbuf *taddr;
    385   1.1      fvdl 
    386   1.1      fvdl 	if (taddr) {
    387  1.20  christos 		free(taddr->buf);
    388  1.20  christos 		free(taddr);
    389  1.20  christos 		taddr = NULL;
    390   1.1      fvdl 	}
    391   1.1      fvdl 	if (((nconf = rpcbind_get_conf(transp->xp_netid)) == NULL) ||
    392   1.1      fvdl 	    ((taddr = uaddr2taddr(nconf, *uaddrp)) == NULL)) {
    393  1.20  christos 		(void) memset(&nbuf, 0, sizeof (struct netbuf));
    394  1.20  christos 		return &nbuf;
    395   1.1      fvdl 	}
    396  1.20  christos 	return taddr;
    397   1.1      fvdl }
    398   1.1      fvdl 
    399   1.1      fvdl /*
    400   1.1      fvdl  * Convert taddr to uaddr. Should be used only by
    401   1.1      fvdl  * local servers/clients. (kernel level stuff only)
    402   1.1      fvdl  */
    403   1.1      fvdl /* ARGSUSED */
    404   1.1      fvdl void *
    405  1.20  christos rpcbproc_taddr2uaddr_com(void *arg, struct svc_req *rqstp __unused,
    406  1.20  christos     SVCXPRT *transp, rpcvers_t rpcbversnum __unused)
    407   1.1      fvdl {
    408  1.20  christos 	struct netbuf *taddr = arg;
    409   1.1      fvdl 	static char *uaddr;
    410   1.1      fvdl 	struct netconfig *nconf;
    411   1.1      fvdl 
    412   1.1      fvdl #ifdef CHEW_FDS
    413   1.1      fvdl 	int fd;
    414   1.1      fvdl 
    415   1.1      fvdl 	if ((fd = open("/dev/null", O_RDONLY)) == -1) {
    416  1.11  christos 		uaddr = strerror(errno);
    417   1.1      fvdl 		return (&uaddr);
    418   1.1      fvdl 	}
    419   1.1      fvdl #endif /* CHEW_FDS */
    420  1.20  christos 	if (uaddr != NULL && uaddr != nullstring) {
    421  1.20  christos 		free(uaddr);
    422  1.20  christos 		uaddr = NULL;
    423  1.20  christos 	}
    424   1.1      fvdl 	if (((nconf = rpcbind_get_conf(transp->xp_netid)) == NULL) ||
    425   1.1      fvdl 		((uaddr = taddr2uaddr(nconf, taddr)) == NULL)) {
    426  1.20  christos 		uaddr = nullstring;
    427   1.1      fvdl 	}
    428   1.1      fvdl 	return (void *)&uaddr;
    429   1.1      fvdl }
    430   1.1      fvdl 
    431   1.1      fvdl 
    432   1.1      fvdl static bool_t
    433   1.1      fvdl xdr_encap_parms(XDR *xdrs, struct encap_parms *epp)
    434   1.1      fvdl {
    435  1.19  christos 	return (xdr_bytes(xdrs, &(epp->args), (u_int *) &(epp->arglen),
    436  1.19  christos 	    RPC_MAXDATASIZE));
    437   1.1      fvdl }
    438   1.1      fvdl 
    439   1.1      fvdl /*
    440   1.1      fvdl  * XDR remote call arguments.  It ignores the address part.
    441   1.1      fvdl  * written for XDR_DECODE direction only
    442   1.1      fvdl  */
    443   1.1      fvdl static bool_t
    444   1.1      fvdl xdr_rmtcall_args(XDR *xdrs, struct r_rmtcall_args *cap)
    445   1.1      fvdl {
    446   1.1      fvdl 	/* does not get the address or the arguments */
    447  1.20  christos 	if (xdr_rpcprog(xdrs, &(cap->rmt_prog)) &&
    448  1.20  christos 	    xdr_rpcvers(xdrs, &(cap->rmt_vers)) &&
    449  1.20  christos 	    xdr_rpcproc(xdrs, &(cap->rmt_proc))) {
    450   1.1      fvdl 		return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
    451   1.1      fvdl 	}
    452   1.1      fvdl 	return (FALSE);
    453   1.1      fvdl }
    454   1.1      fvdl 
    455   1.1      fvdl /*
    456   1.1      fvdl  * XDR remote call results along with the address.  Ignore
    457   1.1      fvdl  * program number, version  number and proc number.
    458   1.1      fvdl  * Written for XDR_ENCODE direction only.
    459   1.1      fvdl  */
    460   1.1      fvdl static bool_t
    461   1.1      fvdl xdr_rmtcall_result(XDR *xdrs, struct r_rmtcall_args *cap)
    462   1.1      fvdl {
    463   1.1      fvdl 	bool_t result;
    464   1.1      fvdl 
    465   1.1      fvdl #ifdef PORTMAP
    466   1.1      fvdl 	if (cap->rmt_localvers == PMAPVERS) {
    467   1.1      fvdl 		int h1, h2, h3, h4, p1, p2;
    468   1.1      fvdl 		u_long port;
    469   1.1      fvdl 
    470   1.1      fvdl 		/* interpret the universal address for TCP/IP */
    471   1.1      fvdl 		if (sscanf(cap->rmt_uaddr, "%d.%d.%d.%d.%d.%d",
    472   1.1      fvdl 			&h1, &h2, &h3, &h4, &p1, &p2) != 6)
    473   1.1      fvdl 			return (FALSE);
    474   1.1      fvdl 		port = ((p1 & 0xff) << 8) + (p2 & 0xff);
    475   1.1      fvdl 		result = xdr_u_long(xdrs, &port);
    476   1.1      fvdl 	} else
    477   1.1      fvdl #endif
    478   1.1      fvdl 		if ((cap->rmt_localvers == RPCBVERS) ||
    479   1.1      fvdl 		    (cap->rmt_localvers == RPCBVERS4)) {
    480   1.1      fvdl 		result = xdr_wrapstring(xdrs, &(cap->rmt_uaddr));
    481   1.1      fvdl 	} else {
    482   1.1      fvdl 		return (FALSE);
    483   1.1      fvdl 	}
    484   1.1      fvdl 	if (result == TRUE)
    485   1.1      fvdl 		return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
    486   1.1      fvdl 	return (FALSE);
    487   1.1      fvdl }
    488   1.1      fvdl 
    489   1.1      fvdl /*
    490   1.1      fvdl  * only worries about the struct encap_parms part of struct r_rmtcall_args.
    491   1.1      fvdl  * The arglen must already be set!!
    492   1.1      fvdl  */
    493   1.1      fvdl static bool_t
    494   1.1      fvdl xdr_opaque_parms(XDR *xdrs, struct r_rmtcall_args *cap)
    495   1.1      fvdl {
    496   1.1      fvdl 	return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
    497   1.1      fvdl }
    498   1.1      fvdl 
    499   1.1      fvdl static struct rmtcallfd_list *rmthead;
    500   1.1      fvdl static struct rmtcallfd_list *rmttail;
    501   1.1      fvdl 
    502   1.1      fvdl int
    503   1.1      fvdl create_rmtcall_fd(struct netconfig *nconf)
    504   1.1      fvdl {
    505   1.1      fvdl 	int fd;
    506   1.1      fvdl 	struct rmtcallfd_list *rmt;
    507   1.1      fvdl 	SVCXPRT *xprt;
    508   1.1      fvdl 
    509   1.1      fvdl 	if ((fd = __rpc_nconf2fd(nconf)) == -1) {
    510   1.1      fvdl 		if (debugging)
    511   1.1      fvdl 			fprintf(stderr,
    512   1.1      fvdl 	"create_rmtcall_fd: couldn't open \"%s\" (errno %d)\n",
    513   1.1      fvdl 			nconf->nc_device, errno);
    514   1.1      fvdl 		return (-1);
    515   1.1      fvdl 	}
    516  1.22  christos 	xprt = svc_tli_create(fd, 0, NULL, 0, 0);
    517   1.1      fvdl 	if (xprt == NULL) {
    518   1.1      fvdl 		if (debugging)
    519   1.1      fvdl 			fprintf(stderr,
    520  1.22  christos 				"%s: svc_tli_create failed\n", __func__);
    521   1.1      fvdl 		return (-1);
    522   1.1      fvdl 	}
    523  1.22  christos 	rmt = malloc(sizeof(*rmt));
    524   1.1      fvdl 	if (rmt == NULL) {
    525  1.22  christos 		syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
    526   1.1      fvdl 		return (-1);
    527   1.1      fvdl 	}
    528   1.1      fvdl 	rmt->xprt = xprt;
    529   1.1      fvdl 	rmt->netid = strdup(nconf->nc_netid);
    530   1.1      fvdl 	xprt->xp_netid = rmt->netid;
    531   1.1      fvdl 	rmt->fd = fd;
    532   1.1      fvdl 	rmt->next = NULL;
    533   1.1      fvdl 	if (rmthead == NULL) {
    534   1.1      fvdl 		rmthead = rmt;
    535   1.1      fvdl 		rmttail = rmt;
    536   1.1      fvdl 	} else {
    537   1.1      fvdl 		rmttail->next = rmt;
    538   1.1      fvdl 		rmttail = rmt;
    539   1.1      fvdl 	}
    540  1.17  christos 	svc_fdset_set(fd);
    541   1.1      fvdl 	return (fd);
    542   1.1      fvdl }
    543   1.1      fvdl 
    544   1.1      fvdl static int
    545   1.1      fvdl find_rmtcallfd_by_netid(char *netid)
    546   1.1      fvdl {
    547   1.1      fvdl 	struct rmtcallfd_list *rmt;
    548   1.1      fvdl 
    549   1.1      fvdl 	for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
    550   1.1      fvdl 		if (strcmp(netid, rmt->netid) == 0) {
    551   1.1      fvdl 			return (rmt->fd);
    552   1.1      fvdl 		}
    553   1.1      fvdl 	}
    554   1.1      fvdl 	return (-1);
    555   1.1      fvdl }
    556   1.1      fvdl 
    557   1.1      fvdl static SVCXPRT *
    558   1.1      fvdl find_rmtcallxprt_by_fd(int fd)
    559   1.1      fvdl {
    560   1.1      fvdl 	struct rmtcallfd_list *rmt;
    561   1.1      fvdl 
    562   1.1      fvdl 	for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
    563   1.1      fvdl 		if (fd == rmt->fd) {
    564   1.1      fvdl 			return (rmt->xprt);
    565   1.1      fvdl 		}
    566   1.1      fvdl 	}
    567   1.1      fvdl 	return (NULL);
    568   1.1      fvdl }
    569   1.1      fvdl 
    570   1.1      fvdl 
    571   1.1      fvdl /*
    572   1.1      fvdl  * Call a remote procedure service.  This procedure is very quiet when things
    573   1.1      fvdl  * go wrong.  The proc is written to support broadcast rpc.  In the broadcast
    574   1.1      fvdl  * case, a machine should shut-up instead of complain, lest the requestor be
    575   1.1      fvdl  * overrun with complaints at the expense of not hearing a valid reply.
    576   1.1      fvdl  * When receiving a request and verifying that the service exists, we
    577   1.1      fvdl  *
    578   1.1      fvdl  *	receive the request
    579   1.1      fvdl  *
    580   1.1      fvdl  *	open a new TLI endpoint on the same transport on which we received
    581   1.1      fvdl  *	the original request
    582   1.1      fvdl  *
    583   1.1      fvdl  *	remember the original request's XID (which requires knowing the format
    584   1.1      fvdl  *	of the svc_dg_data structure)
    585   1.1      fvdl  *
    586   1.1      fvdl  *	forward the request, with a new XID, to the requested service,
    587   1.1      fvdl  *	remembering the XID used to send this request (for later use in
    588   1.1      fvdl  *	reassociating the answer with the original request), the requestor's
    589   1.1      fvdl  *	address, the file descriptor on which the forwarded request is
    590   1.1      fvdl  *	made and the service's address.
    591   1.1      fvdl  *
    592   1.1      fvdl  *	mark the file descriptor on which we anticipate receiving a reply from
    593   1.1      fvdl  *	the service and one to select for in our private svc_run procedure
    594   1.1      fvdl  *
    595   1.1      fvdl  * At some time in the future, a reply will be received from the service to
    596   1.1      fvdl  * which we forwarded the request.  At that time, we detect that the socket
    597   1.1      fvdl  * used was for forwarding (by looking through the finfo structures to see
    598   1.1      fvdl  * whether the fd corresponds to one of those) and call handle_reply() to
    599   1.1      fvdl  *
    600   1.1      fvdl  *	receive the reply
    601   1.1      fvdl  *
    602   1.1      fvdl  *	bundle the reply, along with the service's universal address
    603   1.1      fvdl  *
    604   1.1      fvdl  *	create a SVCXPRT structure and use a version of svc_sendreply
    605   1.1      fvdl  *	that allows us to specify the reply XID and destination, send the reply
    606   1.1      fvdl  *	to the original requestor.
    607   1.1      fvdl  */
    608   1.1      fvdl 
    609   1.1      fvdl void
    610   1.1      fvdl rpcbproc_callit_com(struct svc_req *rqstp, SVCXPRT *transp,
    611   1.1      fvdl 		    rpcproc_t reply_type, rpcvers_t versnum)
    612   1.1      fvdl {
    613  1.17  christos 	rpcblist_ptr rbl;
    614   1.1      fvdl 	struct netconfig *nconf;
    615   1.1      fvdl 	struct netbuf *caller;
    616   1.1      fvdl 	struct r_rmtcall_args a;
    617   1.1      fvdl 	char *buf_alloc = NULL, *outbufp;
    618   1.1      fvdl 	char *outbuf_alloc = NULL;
    619   1.1      fvdl 	char buf[RPC_BUF_MAX], outbuf[RPC_BUF_MAX];
    620  1.16    plunky 	struct netbuf *na = NULL;
    621   1.1      fvdl 	struct rpc_msg call_msg;
    622   1.1      fvdl 	int outlen;
    623   1.1      fvdl 	u_int sendsz;
    624   1.1      fvdl 	XDR outxdr;
    625   1.1      fvdl 	AUTH *auth;
    626   1.1      fvdl 	int fd = -1;
    627  1.20  christos 	char *uaddr, *m_uaddr = NULL, *local_uaddr = NULL;
    628   1.1      fvdl 	u_int32_t *xidp;
    629   1.1      fvdl 	struct __rpc_sockinfo si;
    630   1.1      fvdl 	struct sockaddr *localsa;
    631   1.1      fvdl 	struct netbuf tbuf;
    632   1.1      fvdl 
    633   1.1      fvdl 	if (!__rpc_fd2sockinfo(transp->xp_fd, &si)) {
    634   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    635   1.1      fvdl 			svcerr_systemerr(transp);
    636   1.1      fvdl 		return;
    637   1.1      fvdl 	}
    638   1.1      fvdl 	if (si.si_socktype != SOCK_DGRAM)
    639   1.1      fvdl 		return;	/* Only datagram type accepted */
    640   1.1      fvdl 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, UDPMSGSIZE);
    641   1.1      fvdl 	if (sendsz == 0) {	/* data transfer not supported */
    642   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    643   1.1      fvdl 			svcerr_systemerr(transp);
    644   1.1      fvdl 		return;
    645   1.1      fvdl 	}
    646   1.1      fvdl 	/*
    647   1.1      fvdl 	 * Should be multiple of 4 for XDR.
    648   1.1      fvdl 	 */
    649  1.20  christos 	sendsz = roundup(sendsz, 4);
    650   1.1      fvdl 	if (sendsz > RPC_BUF_MAX) {
    651   1.1      fvdl #ifdef	notyet
    652   1.1      fvdl 		buf_alloc = alloca(sendsz);		/* not in IDR2? */
    653   1.1      fvdl #else
    654   1.1      fvdl 		buf_alloc = malloc(sendsz);
    655   1.1      fvdl #endif	/* notyet */
    656   1.1      fvdl 		if (buf_alloc == NULL) {
    657   1.1      fvdl 			if (debugging)
    658   1.1      fvdl 				fprintf(stderr,
    659   1.1      fvdl 					"rpcbproc_callit_com:  No Memory!\n");
    660   1.1      fvdl 			if (reply_type == RPCBPROC_INDIRECT)
    661   1.1      fvdl 				svcerr_systemerr(transp);
    662   1.1      fvdl 			return;
    663   1.1      fvdl 		}
    664   1.1      fvdl 		a.rmt_args.args = buf_alloc;
    665   1.1      fvdl 	} else {
    666   1.1      fvdl 		a.rmt_args.args = buf;
    667   1.1      fvdl 	}
    668   1.1      fvdl 
    669   1.1      fvdl 	call_msg.rm_xid = 0;	/* For error checking purposes */
    670   1.1      fvdl 	if (!svc_getargs(transp, (xdrproc_t) xdr_rmtcall_args, (char *) &a)) {
    671   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    672   1.1      fvdl 			svcerr_decode(transp);
    673   1.1      fvdl 		if (debugging)
    674   1.1      fvdl 			fprintf(stderr,
    675   1.1      fvdl 			"rpcbproc_callit_com:  svc_getargs failed\n");
    676   1.1      fvdl 		goto error;
    677   1.1      fvdl 	}
    678   1.1      fvdl 
    679   1.1      fvdl 	if (!check_callit(transp, &a, versnum)) {
    680   1.1      fvdl 		svcerr_weakauth(transp);
    681   1.1      fvdl 		goto error;
    682   1.1      fvdl 	}
    683   1.1      fvdl 
    684   1.1      fvdl 	caller = svc_getrpccaller(transp);
    685   1.1      fvdl #ifdef RPCBIND_DEBUG
    686   1.1      fvdl 	if (debugging) {
    687   1.1      fvdl 		uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid), caller);
    688   1.1      fvdl 		fprintf(stderr, "%s %s req for (%lu, %lu, %lu, %s) from %s : ",
    689   1.1      fvdl 			versnum == PMAPVERS ? "pmap_rmtcall" :
    690   1.1      fvdl 			versnum == RPCBVERS ? "rpcb_rmtcall" :
    691  1.12  christos 			versnum == RPCBVERS4 ? "rpcb_indirect" :
    692  1.12  christos 			rpcbind_unknown,
    693   1.1      fvdl 			reply_type == RPCBPROC_INDIRECT ? "indirect" : "callit",
    694   1.1      fvdl 			(unsigned long)a.rmt_prog, (unsigned long)a.rmt_vers,
    695   1.1      fvdl 			(unsigned long)a.rmt_proc, transp->xp_netid,
    696  1.12  christos 			uaddr ? uaddr : rpcbind_unknown);
    697   1.1      fvdl 		if (uaddr)
    698  1.20  christos 			free(uaddr);
    699   1.1      fvdl 	}
    700   1.1      fvdl #endif
    701   1.1      fvdl 
    702   1.1      fvdl 	rbl = find_service(a.rmt_prog, a.rmt_vers, transp->xp_netid);
    703   1.1      fvdl 
    704   1.1      fvdl 	rpcbs_rmtcall(versnum - 2, reply_type, a.rmt_prog, a.rmt_vers,
    705   1.1      fvdl 			a.rmt_proc, transp->xp_netid, rbl);
    706   1.1      fvdl 
    707  1.16    plunky 	if (rbl == NULL) {
    708   1.1      fvdl #ifdef RPCBIND_DEBUG
    709   1.1      fvdl 		if (debugging)
    710   1.1      fvdl 			fprintf(stderr, "not found\n");
    711   1.1      fvdl #endif
    712   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    713   1.1      fvdl 			svcerr_noprog(transp);
    714   1.1      fvdl 		goto error;
    715   1.1      fvdl 	}
    716   1.1      fvdl 	if (rbl->rpcb_map.r_vers != a.rmt_vers) {
    717   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT) {
    718   1.1      fvdl 			rpcvers_t vers_low, vers_high;
    719   1.1      fvdl 
    720   1.1      fvdl 			find_versions(a.rmt_prog, transp->xp_netid,
    721   1.1      fvdl 				&vers_low, &vers_high);
    722   1.1      fvdl 			svcerr_progvers(transp, vers_low, vers_high);
    723   1.1      fvdl 		}
    724   1.1      fvdl 		goto error;
    725   1.1      fvdl 	}
    726   1.1      fvdl 
    727   1.1      fvdl #ifdef RPCBIND_DEBUG
    728   1.1      fvdl 	if (debugging)
    729   1.1      fvdl 		fprintf(stderr, "found at uaddr %s\n", rbl->rpcb_map.r_addr);
    730   1.1      fvdl #endif
    731   1.1      fvdl 	/*
    732   1.1      fvdl 	 *	Check whether this entry is valid and a server is present
    733   1.1      fvdl 	 *	Mergeaddr() returns NULL if no such entry is present, and
    734   1.1      fvdl 	 *	returns "" if the entry was present but the server is not
    735   1.1      fvdl 	 *	present (i.e., it crashed).
    736   1.1      fvdl 	 */
    737   1.1      fvdl 	if (reply_type == RPCBPROC_INDIRECT) {
    738   1.1      fvdl 		uaddr = mergeaddr(transp, transp->xp_netid,
    739   1.1      fvdl 			rbl->rpcb_map.r_addr, NULL);
    740  1.20  christos 		if (uaddr == NULL || uaddr[0] == '\0') {
    741   1.1      fvdl 			svcerr_noprog(transp);
    742  1.20  christos 			free(uaddr);
    743   1.1      fvdl 			goto error;
    744   1.1      fvdl 		}
    745  1.20  christos 		free(uaddr);
    746   1.1      fvdl 	}
    747   1.1      fvdl 	nconf = rpcbind_get_conf(transp->xp_netid);
    748  1.16    plunky 	if (nconf == NULL) {
    749   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    750   1.1      fvdl 			svcerr_systemerr(transp);
    751   1.1      fvdl 		if (debugging)
    752   1.1      fvdl 			fprintf(stderr,
    753   1.1      fvdl 			"rpcbproc_callit_com:  rpcbind_get_conf failed\n");
    754   1.1      fvdl 		goto error;
    755   1.1      fvdl 	}
    756   1.1      fvdl 	localsa = local_sa(((struct sockaddr *)caller->buf)->sa_family);
    757   1.1      fvdl 	if (localsa == NULL) {
    758   1.1      fvdl 		if (debugging)
    759   1.1      fvdl 			fprintf(stderr,
    760   1.1      fvdl 			"rpcbproc_callit_com: no local address\n");
    761   1.1      fvdl 		goto error;
    762   1.1      fvdl 	}
    763   1.1      fvdl 	tbuf.len = tbuf.maxlen = localsa->sa_len;
    764   1.1      fvdl 	tbuf.buf = localsa;
    765   1.1      fvdl 	local_uaddr =
    766   1.1      fvdl 	    addrmerge(&tbuf, rbl->rpcb_map.r_addr, NULL, nconf->nc_netid);
    767   1.1      fvdl 	m_uaddr = addrmerge(caller, rbl->rpcb_map.r_addr, NULL,
    768   1.1      fvdl 			nconf->nc_netid);
    769   1.1      fvdl #ifdef RPCBIND_DEBUG
    770   1.1      fvdl 	if (debugging)
    771   1.1      fvdl 		fprintf(stderr, "merged uaddr %s\n", m_uaddr);
    772   1.1      fvdl #endif
    773   1.1      fvdl 	if ((fd = find_rmtcallfd_by_netid(nconf->nc_netid)) == -1) {
    774   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    775   1.1      fvdl 			svcerr_systemerr(transp);
    776   1.1      fvdl 		goto error;
    777   1.1      fvdl 	}
    778   1.1      fvdl 	xidp = __rpcb_get_dg_xidp(transp);
    779  1.20  christos 	switch (forward_register(*xidp, caller, fd, m_uaddr, reply_type,
    780  1.20  christos 	    versnum, &call_msg.rm_xid)) {
    781  1.20  christos 	case 1:
    782  1.20  christos 		/* Success; forward_register() will free m_uaddr for us. */
    783  1.20  christos 		m_uaddr = NULL;
    784  1.20  christos 		break;
    785  1.20  christos 	case 0:
    786   1.1      fvdl 		/*
    787   1.1      fvdl 		 * A duplicate request for the slow server.  Let's not
    788   1.1      fvdl 		 * beat on it any more.
    789   1.1      fvdl 		 */
    790   1.1      fvdl 		if (debugging)
    791   1.1      fvdl 			fprintf(stderr,
    792   1.1      fvdl 			"rpcbproc_callit_com:  duplicate request\n");
    793   1.1      fvdl 		goto error;
    794  1.20  christos 	case -1:
    795   1.1      fvdl 		/*  forward_register failed.  Perhaps no memory. */
    796   1.1      fvdl 		if (debugging)
    797   1.1      fvdl 			fprintf(stderr,
    798   1.1      fvdl 			"rpcbproc_callit_com:  forward_register failed\n");
    799   1.1      fvdl 		goto error;
    800   1.1      fvdl 	}
    801   1.1      fvdl 
    802   1.1      fvdl #ifdef DEBUG_RMTCALL
    803   1.1      fvdl 	if (debugging)
    804   1.1      fvdl 		fprintf(stderr,
    805   1.1      fvdl 			"rpcbproc_callit_com:  original XID %x, new XID %x\n",
    806   1.1      fvdl 				*xidp, call_msg.rm_xid);
    807   1.1      fvdl #endif
    808   1.1      fvdl 	call_msg.rm_direction = CALL;
    809   1.1      fvdl 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
    810   1.1      fvdl 	call_msg.rm_call.cb_prog = a.rmt_prog;
    811   1.1      fvdl 	call_msg.rm_call.cb_vers = a.rmt_vers;
    812   1.1      fvdl 	if (sendsz > RPC_BUF_MAX) {
    813   1.1      fvdl #ifdef	notyet
    814   1.1      fvdl 		outbuf_alloc = alloca(sendsz);	/* not in IDR2? */
    815   1.1      fvdl #else
    816   1.1      fvdl 		outbuf_alloc = malloc(sendsz);
    817   1.1      fvdl #endif	/* notyet */
    818   1.1      fvdl 		if (outbuf_alloc == NULL) {
    819   1.1      fvdl 			if (reply_type == RPCBPROC_INDIRECT)
    820   1.1      fvdl 				svcerr_systemerr(transp);
    821  1.22  christos 			syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
    822   1.1      fvdl 			goto error;
    823   1.1      fvdl 		}
    824   1.1      fvdl 		xdrmem_create(&outxdr, outbuf_alloc, sendsz, XDR_ENCODE);
    825   1.1      fvdl 	} else {
    826   1.1      fvdl 		xdrmem_create(&outxdr, outbuf, sendsz, XDR_ENCODE);
    827   1.1      fvdl 	}
    828   1.1      fvdl 	if (!xdr_callhdr(&outxdr, &call_msg)) {
    829   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    830   1.1      fvdl 			svcerr_systemerr(transp);
    831   1.1      fvdl 		if (debugging)
    832   1.1      fvdl 			fprintf(stderr,
    833   1.1      fvdl 			"rpcbproc_callit_com:  xdr_callhdr failed\n");
    834   1.1      fvdl 		goto error;
    835   1.1      fvdl 	}
    836   1.1      fvdl 	if (!xdr_u_int32_t(&outxdr, &(a.rmt_proc))) {
    837   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    838   1.1      fvdl 			svcerr_systemerr(transp);
    839   1.1      fvdl 		if (debugging)
    840   1.1      fvdl 			fprintf(stderr,
    841   1.1      fvdl 			"rpcbproc_callit_com:  xdr_u_long failed\n");
    842   1.1      fvdl 		goto error;
    843   1.1      fvdl 	}
    844   1.1      fvdl 
    845   1.1      fvdl 	if (rqstp->rq_cred.oa_flavor == AUTH_NULL) {
    846   1.1      fvdl 		auth = authnone_create();
    847   1.1      fvdl 	} else if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
    848   1.1      fvdl 		struct authunix_parms *au;
    849   1.1      fvdl 
    850   1.1      fvdl 		au = (struct authunix_parms *)rqstp->rq_clntcred;
    851   1.1      fvdl 		auth = authunix_create(au->aup_machname,
    852   1.1      fvdl 				au->aup_uid, au->aup_gid,
    853   1.1      fvdl 				au->aup_len, au->aup_gids);
    854   1.1      fvdl 		if (auth == NULL) /* fall back */
    855   1.1      fvdl 			auth = authnone_create();
    856   1.1      fvdl 	} else {
    857   1.1      fvdl 		/* we do not support any other authentication scheme */
    858   1.1      fvdl 		if (debugging)
    859   1.1      fvdl 			fprintf(stderr,
    860   1.1      fvdl "rpcbproc_callit_com:  oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
    861   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    862   1.1      fvdl 			svcerr_weakauth(transp); /* XXX too strong.. */
    863   1.1      fvdl 		goto error;
    864   1.1      fvdl 	}
    865   1.1      fvdl 	if (auth == NULL) {
    866   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    867   1.1      fvdl 			svcerr_systemerr(transp);
    868   1.1      fvdl 		if (debugging)
    869   1.1      fvdl 			fprintf(stderr,
    870   1.1      fvdl 		"rpcbproc_callit_com:  authwhatever_create returned NULL\n");
    871   1.1      fvdl 		goto error;
    872   1.1      fvdl 	}
    873   1.1      fvdl 	if (!AUTH_MARSHALL(auth, &outxdr)) {
    874   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    875   1.1      fvdl 			svcerr_systemerr(transp);
    876   1.1      fvdl 		AUTH_DESTROY(auth);
    877   1.1      fvdl 		if (debugging)
    878   1.1      fvdl 			fprintf(stderr,
    879   1.1      fvdl 		"rpcbproc_callit_com:  AUTH_MARSHALL failed\n");
    880   1.1      fvdl 		goto error;
    881   1.1      fvdl 	}
    882   1.1      fvdl 	AUTH_DESTROY(auth);
    883   1.1      fvdl 	if (!xdr_opaque_parms(&outxdr, &a)) {
    884   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    885   1.1      fvdl 			svcerr_systemerr(transp);
    886   1.1      fvdl 		if (debugging)
    887   1.1      fvdl 			fprintf(stderr,
    888   1.1      fvdl 		"rpcbproc_callit_com:  xdr_opaque_parms failed\n");
    889   1.1      fvdl 		goto error;
    890   1.1      fvdl 	}
    891   1.1      fvdl 	outlen = (int) XDR_GETPOS(&outxdr);
    892   1.1      fvdl 	if (outbuf_alloc)
    893   1.1      fvdl 		outbufp = outbuf_alloc;
    894   1.1      fvdl 	else
    895   1.1      fvdl 		outbufp = outbuf;
    896   1.1      fvdl 
    897   1.1      fvdl 	na = uaddr2taddr(nconf, local_uaddr);
    898   1.1      fvdl 	if (!na) {
    899   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    900   1.1      fvdl 			svcerr_systemerr(transp);
    901   1.1      fvdl 		goto error;
    902   1.1      fvdl 	}
    903   1.1      fvdl 
    904   1.1      fvdl 	if (sendto(fd, outbufp, outlen, 0, (struct sockaddr *)na->buf, na->len)
    905   1.1      fvdl 	    != outlen) {
    906   1.1      fvdl 		if (debugging)
    907   1.1      fvdl 			fprintf(stderr,
    908   1.1      fvdl 	"rpcbproc_callit_com:  sendto failed:  errno %d\n", errno);
    909   1.1      fvdl 		if (reply_type == RPCBPROC_INDIRECT)
    910   1.1      fvdl 			svcerr_systemerr(transp);
    911   1.1      fvdl 		goto error;
    912   1.1      fvdl 	}
    913   1.1      fvdl 	goto out;
    914   1.1      fvdl 
    915   1.1      fvdl error:
    916   1.1      fvdl 	if (call_msg.rm_xid != 0)
    917   1.1      fvdl 		(void) free_slot_by_xid(call_msg.rm_xid);
    918   1.1      fvdl out:
    919   1.5      fvdl 	if (local_uaddr)
    920   1.5      fvdl 		free(local_uaddr);
    921   1.1      fvdl 	if (buf_alloc)
    922  1.20  christos 		free(buf_alloc);
    923   1.1      fvdl 	if (outbuf_alloc)
    924  1.20  christos 		free(outbuf_alloc);
    925   1.3      fvdl 	if (na) {
    926   1.3      fvdl 		free(na->buf);
    927   1.3      fvdl 		free(na);
    928   1.3      fvdl 	}
    929  1.20  christos 	if (m_uaddr != NULL)
    930  1.20  christos 		free(m_uaddr);
    931   1.1      fvdl }
    932   1.1      fvdl 
    933   1.1      fvdl /*
    934   1.1      fvdl  * Makes an entry into the FIFO for the given request.
    935  1.20  christos  * Returns 1 on success, 0 if this is a duplicate request, or -1 on error.
    936  1.20  christos  * *callxidp is set to the xid of the call.
    937   1.1      fvdl  */
    938  1.20  christos static int
    939   1.1      fvdl forward_register(u_int32_t caller_xid, struct netbuf *caller_addr,
    940  1.20  christos     int forward_fd, char *uaddr, rpcproc_t reply_type,
    941  1.20  christos      rpcvers_t versnum, u_int32_t *callxidp)
    942   1.1      fvdl {
    943   1.1      fvdl 	int		i;
    944   1.1      fvdl 	int		j = 0;
    945   1.1      fvdl 	time_t		min_time, time_now;
    946   1.1      fvdl 	static u_int32_t	lastxid;
    947   1.1      fvdl 	int		entry = -1;
    948   1.1      fvdl 
    949   1.1      fvdl 	min_time = FINFO[0].time;
    950   1.1      fvdl 	time_now = time((time_t *)0);
    951   1.1      fvdl 	/* initialization */
    952   1.1      fvdl 	if (lastxid == 0)
    953   1.1      fvdl 		lastxid = time_now * NFORWARD;
    954   1.1      fvdl 
    955   1.1      fvdl 	/*
    956  1.20  christos 	 * Check if it is a duplicate entry. Then,
    957   1.1      fvdl 	 * try to find an empty slot.  If not available, then
    958   1.1      fvdl 	 * use the slot with the earliest time.
    959   1.1      fvdl 	 */
    960   1.1      fvdl 	for (i = 0; i < NFORWARD; i++) {
    961   1.1      fvdl 		if (FINFO[i].flag & FINFO_ACTIVE) {
    962   1.1      fvdl 			if ((FINFO[i].caller_xid == caller_xid) &&
    963   1.1      fvdl 			    (FINFO[i].reply_type == reply_type) &&
    964   1.1      fvdl 			    (FINFO[i].versnum == versnum) &&
    965   1.1      fvdl 			    (!netbufcmp(FINFO[i].caller_addr,
    966   1.1      fvdl 					    caller_addr))) {
    967   1.1      fvdl 				FINFO[i].time = time((time_t *)0);
    968   1.1      fvdl 				return (0);	/* Duplicate entry */
    969   1.1      fvdl 			} else {
    970   1.1      fvdl 				/* Should we wait any longer */
    971   1.1      fvdl 				if ((time_now - FINFO[i].time) > MAXTIME_OFF)
    972   1.1      fvdl 					(void) free_slot_by_index(i);
    973   1.1      fvdl 			}
    974   1.1      fvdl 		}
    975   1.1      fvdl 		if (entry == -1) {
    976   1.1      fvdl 			if ((FINFO[i].flag & FINFO_ACTIVE) == 0) {
    977   1.1      fvdl 				entry = i;
    978   1.1      fvdl 			} else if (FINFO[i].time < min_time) {
    979   1.1      fvdl 				j = i;
    980   1.1      fvdl 				min_time = FINFO[i].time;
    981   1.1      fvdl 			}
    982   1.1      fvdl 		}
    983   1.1      fvdl 	}
    984   1.1      fvdl 	if (entry != -1) {
    985   1.1      fvdl 		/* use this empty slot */
    986   1.1      fvdl 		j = entry;
    987   1.1      fvdl 	} else {
    988   1.1      fvdl 		(void) free_slot_by_index(j);
    989   1.1      fvdl 	}
    990   1.1      fvdl 	if ((FINFO[j].caller_addr = netbufdup(caller_addr)) == NULL) {
    991   1.1      fvdl 		return (-1);
    992   1.1      fvdl 	}
    993   1.1      fvdl 	rpcb_rmtcalls++;	/* no of pending calls */
    994   1.1      fvdl 	FINFO[j].flag = FINFO_ACTIVE;
    995   1.1      fvdl 	FINFO[j].reply_type = reply_type;
    996   1.1      fvdl 	FINFO[j].versnum = versnum;
    997   1.1      fvdl 	FINFO[j].time = time_now;
    998   1.1      fvdl 	FINFO[j].caller_xid = caller_xid;
    999   1.1      fvdl 	FINFO[j].forward_fd = forward_fd;
   1000   1.1      fvdl 	/*
   1001   1.1      fvdl 	 * Though uaddr is not allocated here, it will still be freed
   1002   1.1      fvdl 	 * from free_slot_*().
   1003   1.1      fvdl 	 */
   1004   1.1      fvdl 	FINFO[j].uaddr = uaddr;
   1005   1.1      fvdl 	lastxid = lastxid + NFORWARD;
   1006  1.20  christos 	/* Don't allow a zero xid below. */
   1007  1.20  christos 	if ((u_int32_t)(lastxid + NFORWARD) <= NFORWARD)
   1008  1.20  christos 		lastxid = NFORWARD;
   1009   1.1      fvdl 	FINFO[j].forward_xid = lastxid + j;	/* encode slot */
   1010  1.20  christos 	*callxidp = FINFO[j].forward_xid;	/* forward on this xid */
   1011  1.20  christos 	return (1);
   1012   1.1      fvdl }
   1013   1.1      fvdl 
   1014   1.1      fvdl static struct finfo *
   1015   1.1      fvdl forward_find(u_int32_t reply_xid)
   1016   1.1      fvdl {
   1017   1.1      fvdl 	int		i;
   1018   1.1      fvdl 
   1019   1.1      fvdl 	i = reply_xid % NFORWARD;
   1020   1.1      fvdl 	if (i < 0)
   1021   1.1      fvdl 		i += NFORWARD;
   1022   1.1      fvdl 	if ((FINFO[i].flag & FINFO_ACTIVE) &&
   1023   1.1      fvdl 	    (FINFO[i].forward_xid == reply_xid)) {
   1024   1.1      fvdl 		return (&FINFO[i]);
   1025   1.1      fvdl 	}
   1026   1.1      fvdl 	return (NULL);
   1027   1.1      fvdl }
   1028   1.1      fvdl 
   1029   1.1      fvdl static int
   1030   1.1      fvdl free_slot_by_xid(u_int32_t xid)
   1031   1.1      fvdl {
   1032   1.1      fvdl 	int entry;
   1033   1.1      fvdl 
   1034   1.1      fvdl 	entry = xid % NFORWARD;
   1035   1.1      fvdl 	if (entry < 0)
   1036   1.1      fvdl 		entry += NFORWARD;
   1037   1.1      fvdl 	return (free_slot_by_index(entry));
   1038   1.1      fvdl }
   1039   1.1      fvdl 
   1040   1.1      fvdl static int
   1041  1.11  christos free_slot_by_index(int idx)
   1042   1.1      fvdl {
   1043   1.1      fvdl 	struct finfo	*fi;
   1044   1.1      fvdl 
   1045  1.11  christos 	fi = &FINFO[idx];
   1046   1.1      fvdl 	if (fi->flag & FINFO_ACTIVE) {
   1047   1.1      fvdl 		netbuffree(fi->caller_addr);
   1048   1.1      fvdl 		/* XXX may be too big, but can't access xprt array here */
   1049  1.17  christos 		if (fi->forward_fd >= *svc_fdset_getmax())
   1050  1.17  christos 			(*svc_fdset_getmax())--;
   1051  1.20  christos 		free(fi->uaddr);
   1052   1.1      fvdl 		fi->flag &= ~FINFO_ACTIVE;
   1053   1.1      fvdl 		rpcb_rmtcalls--;
   1054   1.1      fvdl 		return (1);
   1055   1.1      fvdl 	}
   1056   1.1      fvdl 	return (0);
   1057   1.1      fvdl }
   1058   1.1      fvdl 
   1059   1.1      fvdl static int
   1060   1.1      fvdl netbufcmp(struct netbuf *n1, struct netbuf *n2)
   1061   1.1      fvdl {
   1062   1.1      fvdl 	return ((n1->len != n2->len) || memcmp(n1->buf, n2->buf, n1->len));
   1063   1.1      fvdl }
   1064   1.1      fvdl 
   1065  1.20  christos static bool_t
   1066  1.20  christos netbuf_copybuf(struct netbuf *dst, const struct netbuf *src)
   1067  1.20  christos {
   1068  1.20  christos 	assert(src->len <= src->maxlen);
   1069  1.20  christos 
   1070  1.20  christos 	if (dst->maxlen < src->len || dst->buf == NULL) {
   1071  1.20  christos 		if (dst->buf != NULL)
   1072  1.20  christos 			free(dst->buf);
   1073  1.20  christos 		if ((dst->buf = calloc(1, src->maxlen)) == NULL)
   1074  1.20  christos 			return (FALSE);
   1075  1.20  christos 		dst->maxlen = src->maxlen;
   1076  1.20  christos 	}
   1077  1.20  christos 
   1078  1.20  christos 	dst->len = src->len;
   1079  1.20  christos 	memcpy(dst->buf, src->buf, src->len);
   1080  1.20  christos 
   1081  1.20  christos 	return (TRUE);
   1082  1.20  christos }
   1083  1.20  christos 
   1084   1.1      fvdl static struct netbuf *
   1085   1.1      fvdl netbufdup(struct netbuf *ap)
   1086   1.1      fvdl {
   1087   1.1      fvdl 	struct netbuf  *np;
   1088   1.1      fvdl 
   1089  1.20  christos 	if ((np = calloc(1, sizeof(struct netbuf))) == NULL)
   1090  1.20  christos 		return (NULL);
   1091  1.20  christos 	if (netbuf_copybuf(np, ap) == FALSE) {
   1092  1.20  christos 		free(np);
   1093  1.20  christos 		return (NULL);
   1094   1.1      fvdl 	}
   1095   1.1      fvdl 	return (np);
   1096   1.1      fvdl }
   1097   1.1      fvdl 
   1098   1.1      fvdl static void
   1099   1.1      fvdl netbuffree(struct netbuf *ap)
   1100   1.1      fvdl {
   1101  1.20  christos 	free(ap->buf);
   1102  1.20  christos 	ap->buf = NULL;
   1103  1.20  christos 	free(ap);
   1104   1.1      fvdl }
   1105   1.1      fvdl 
   1106   1.1      fvdl 
   1107   1.1      fvdl #define	MASKVAL	(POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
   1108   1.9      fvdl extern bool_t __svc_clean_idle(fd_set *, int, bool_t);
   1109   1.1      fvdl 
   1110   1.1      fvdl void
   1111  1.15     joerg my_svc_run(void)
   1112   1.1      fvdl {
   1113   1.1      fvdl 	size_t nfds;
   1114  1.17  christos 	struct pollfd *pollfds;
   1115  1.17  christos 	int npollfds;
   1116   1.1      fvdl 	int poll_ret, check_ret;
   1117  1.18  christos 	int n, *m;
   1118   1.1      fvdl #ifdef SVC_RUN_DEBUG
   1119   1.1      fvdl 	int i;
   1120   1.1      fvdl #endif
   1121  1.17  christos 	struct pollfd	*p;
   1122  1.17  christos 
   1123  1.17  christos 	pollfds = NULL;
   1124  1.17  christos 	npollfds = 0;
   1125   1.1      fvdl 
   1126   1.1      fvdl 	for (;;) {
   1127  1.17  christos 		if (svc_fdset_getsize(0) != npollfds) {
   1128  1.17  christos 			npollfds = svc_fdset_getsize(0);
   1129  1.17  christos 			pollfds = realloc(pollfds, npollfds * sizeof(*pollfds));
   1130  1.17  christos 		}
   1131   1.1      fvdl 		p = pollfds;
   1132  1.18  christos 		if (p == NULL) {
   1133  1.18  christos out:
   1134  1.18  christos 			syslog(LOG_ERR, "Cannot allocate pollfds");
   1135  1.18  christos 			sleep(1);
   1136  1.18  christos 			continue;
   1137  1.18  christos 		}
   1138  1.18  christos 		if ((m = svc_fdset_getmax()) == NULL)
   1139  1.18  christos 			goto out;
   1140  1.18  christos 		for (n = 0; n <= *m; n++) {
   1141  1.17  christos 			if (svc_fdset_isset(n)) {
   1142   1.1      fvdl 				p->fd = n;
   1143   1.1      fvdl 				p->events = MASKVAL;
   1144   1.1      fvdl 				p++;
   1145   1.1      fvdl 			}
   1146   1.1      fvdl 		}
   1147   1.1      fvdl 		nfds = p - pollfds;
   1148   1.1      fvdl 		poll_ret = 0;
   1149   1.1      fvdl #ifdef SVC_RUN_DEBUG
   1150   1.1      fvdl 		if (debugging) {
   1151   1.1      fvdl 			fprintf(stderr, "polling for read on fd < ");
   1152   1.1      fvdl 			for (i = 0, p = pollfds; i < nfds; i++, p++)
   1153   1.1      fvdl 				if (p->events)
   1154   1.1      fvdl 					fprintf(stderr, "%d ", p->fd);
   1155   1.1      fvdl 			fprintf(stderr, ">\n");
   1156   1.1      fvdl 		}
   1157   1.1      fvdl #endif
   1158  1.17  christos #ifdef RPCBIND_RUMP
   1159  1.17  christos 		poll_ret = rump_sys_poll(pollfds, nfds, 30 * 1000);
   1160  1.17  christos #else
   1161  1.17  christos 		poll_ret = poll(pollfds, nfds, 30 * 1000);
   1162  1.17  christos #endif
   1163  1.17  christos 		switch (poll_ret) {
   1164   1.1      fvdl 		case -1:
   1165   1.1      fvdl 			/*
   1166   1.1      fvdl 			 * We ignore all errors, continuing with the assumption
   1167   1.1      fvdl 			 * that it was set by the signal handlers (or any
   1168   1.1      fvdl 			 * other outside event) and not caused by poll().
   1169   1.1      fvdl 			 */
   1170  1.17  christos #ifdef SVC_RUN_DEBUG
   1171  1.17  christos 			if (debugging) {
   1172  1.17  christos 				fprintf(stderr, "poll returned %d (%s)\n",
   1173  1.17  christos 				    poll_ret, strerror(errno));
   1174  1.17  christos 			}
   1175  1.17  christos #endif
   1176   1.1      fvdl 		case 0:
   1177  1.17  christos 			__svc_clean_idle(NULL, 30, FALSE);
   1178   1.1      fvdl 			continue;
   1179   1.1      fvdl 		default:
   1180   1.1      fvdl #ifdef SVC_RUN_DEBUG
   1181   1.1      fvdl 			if (debugging) {
   1182   1.1      fvdl 				fprintf(stderr, "poll returned read fds < ");
   1183   1.1      fvdl 				for (i = 0, p = pollfds; i < nfds; i++, p++)
   1184   1.1      fvdl 					if (p->revents)
   1185  1.20  christos 						fprintf(stderr, "%d (%#x)",
   1186  1.17  christos 						    p->fd, p->revents);
   1187   1.1      fvdl 				fprintf(stderr, ">\n");
   1188   1.1      fvdl 			}
   1189   1.1      fvdl #endif
   1190   1.1      fvdl 			/*
   1191   1.1      fvdl 			 * If we found as many replies on callback fds
   1192   1.1      fvdl 			 * as the number of descriptors selectable which
   1193   1.1      fvdl 			 * poll() returned, there can be no more so we
   1194   1.1      fvdl 			 * don't call svc_getreq_poll.  Otherwise, there
   1195   1.1      fvdl 			 * must be another so we must call svc_getreq_poll.
   1196   1.1      fvdl 			 */
   1197   1.1      fvdl 			if ((check_ret = check_rmtcalls(pollfds, nfds)) ==
   1198   1.1      fvdl 			    poll_ret)
   1199   1.1      fvdl 				continue;
   1200   1.1      fvdl 			svc_getreq_poll(pollfds, poll_ret-check_ret);
   1201   1.1      fvdl 		}
   1202   1.1      fvdl #ifdef SVC_RUN_DEBUG
   1203   1.1      fvdl 		if (debugging) {
   1204  1.17  christos 			fprintf(stderr, "svc_maxfd now %u\n",
   1205  1.17  christos 			    *svc_fdset_getmax());
   1206   1.1      fvdl 		}
   1207   1.1      fvdl #endif
   1208   1.1      fvdl 	}
   1209   1.1      fvdl }
   1210   1.1      fvdl 
   1211   1.1      fvdl static int
   1212   1.1      fvdl check_rmtcalls(struct pollfd *pfds, int nfds)
   1213   1.1      fvdl {
   1214   1.1      fvdl 	int j, ncallbacks_found = 0, rmtcalls_pending;
   1215   1.1      fvdl 	SVCXPRT *xprt;
   1216   1.1      fvdl 
   1217   1.1      fvdl 	if (rpcb_rmtcalls == 0)
   1218   1.1      fvdl 		return (0);
   1219   1.1      fvdl 
   1220   1.1      fvdl 	rmtcalls_pending = rpcb_rmtcalls;
   1221   1.1      fvdl 	for (j = 0; j < nfds; j++) {
   1222   1.1      fvdl 		if ((xprt = find_rmtcallxprt_by_fd(pfds[j].fd)) != NULL) {
   1223   1.1      fvdl 			if (pfds[j].revents) {
   1224   1.1      fvdl 				ncallbacks_found++;
   1225   1.1      fvdl #ifdef DEBUG_RMTCALL
   1226   1.1      fvdl 			if (debugging)
   1227   1.1      fvdl 				fprintf(stderr,
   1228   1.1      fvdl "my_svc_run:  polled on forwarding fd %d, netid %s - calling handle_reply\n",
   1229   1.1      fvdl 		pfds[j].fd, xprt->xp_netid);
   1230   1.1      fvdl #endif
   1231   1.1      fvdl 				handle_reply(pfds[j].fd, xprt);
   1232   1.1      fvdl 				pfds[j].revents = 0;
   1233   1.1      fvdl 				if (ncallbacks_found >= rmtcalls_pending) {
   1234   1.1      fvdl 					break;
   1235   1.1      fvdl 				}
   1236   1.1      fvdl 			}
   1237   1.1      fvdl 		}
   1238   1.1      fvdl 	}
   1239   1.1      fvdl 	return (ncallbacks_found);
   1240   1.1      fvdl }
   1241   1.1      fvdl 
   1242   1.1      fvdl static void
   1243   1.1      fvdl xprt_set_caller(SVCXPRT *xprt, struct finfo *fi)
   1244   1.1      fvdl {
   1245   1.1      fvdl 	u_int32_t *xidp;
   1246   1.1      fvdl 
   1247  1.20  christos 	netbuf_copybuf(svc_getrpccaller(xprt), fi->caller_addr);
   1248   1.1      fvdl 	xidp = __rpcb_get_dg_xidp(xprt);
   1249   1.1      fvdl 	*xidp = fi->caller_xid;
   1250   1.1      fvdl }
   1251   1.1      fvdl 
   1252   1.1      fvdl /*
   1253   1.1      fvdl  * Call svcerr_systemerr() only if RPCBVERS4
   1254   1.1      fvdl  */
   1255   1.1      fvdl static void
   1256   1.1      fvdl send_svcsyserr(SVCXPRT *xprt, struct finfo *fi)
   1257   1.1      fvdl {
   1258   1.1      fvdl 	if (fi->reply_type == RPCBPROC_INDIRECT) {
   1259   1.1      fvdl 		xprt_set_caller(xprt, fi);
   1260   1.1      fvdl 		svcerr_systemerr(xprt);
   1261   1.1      fvdl 	}
   1262   1.1      fvdl 	return;
   1263   1.1      fvdl }
   1264   1.1      fvdl 
   1265   1.1      fvdl static void
   1266   1.1      fvdl handle_reply(int fd, SVCXPRT *xprt)
   1267   1.1      fvdl {
   1268   1.1      fvdl 	XDR		reply_xdrs;
   1269   1.1      fvdl 	struct rpc_msg	reply_msg;
   1270   1.1      fvdl 	struct rpc_err	reply_error;
   1271   1.1      fvdl 	char		*buffer;
   1272   1.1      fvdl 	struct finfo	*fi;
   1273   1.6      fvdl 	int		inlen, pos, len;
   1274   1.1      fvdl 	struct r_rmtcall_args a;
   1275   1.1      fvdl 	struct sockaddr_storage ss;
   1276   1.1      fvdl 	socklen_t fromlen;
   1277   1.1      fvdl #ifdef SVC_RUN_DEBUG
   1278   1.1      fvdl 	char *uaddr;
   1279   1.1      fvdl #endif
   1280   1.1      fvdl 
   1281   1.1      fvdl 	buffer = malloc(RPC_BUF_MAX);
   1282   1.1      fvdl 	if (buffer == NULL)
   1283   1.1      fvdl 		goto done;
   1284   1.1      fvdl 
   1285   1.1      fvdl 	do {
   1286  1.20  christos 		fromlen = sizeof(ss);
   1287   1.1      fvdl 		inlen = recvfrom(fd, buffer, RPC_BUF_MAX, 0,
   1288   1.1      fvdl 			    (struct sockaddr *)&ss, &fromlen);
   1289   1.1      fvdl 	} while (inlen < 0 && errno == EINTR);
   1290   1.6      fvdl 	if (inlen < 0) {
   1291   1.1      fvdl 		if (debugging)
   1292   1.1      fvdl 			fprintf(stderr,
   1293   1.6      fvdl 	"handle_reply:  recvfrom returned %d, errno %d\n", inlen, errno);
   1294   1.1      fvdl 		goto done;
   1295   1.1      fvdl 	}
   1296   1.1      fvdl 
   1297   1.1      fvdl 	reply_msg.acpted_rply.ar_verf = _null_auth;
   1298   1.1      fvdl 	reply_msg.acpted_rply.ar_results.where = 0;
   1299   1.1      fvdl 	reply_msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
   1300   1.1      fvdl 
   1301   1.1      fvdl 	xdrmem_create(&reply_xdrs, buffer, (u_int)inlen, XDR_DECODE);
   1302   1.1      fvdl 	if (!xdr_replymsg(&reply_xdrs, &reply_msg)) {
   1303   1.1      fvdl 		if (debugging)
   1304   1.1      fvdl 			(void) fprintf(stderr,
   1305   1.1      fvdl 				"handle_reply:  xdr_replymsg failed\n");
   1306   1.1      fvdl 		goto done;
   1307   1.1      fvdl 	}
   1308   1.1      fvdl 	fi = forward_find(reply_msg.rm_xid);
   1309   1.1      fvdl #ifdef	SVC_RUN_DEBUG
   1310   1.1      fvdl 	if (debugging) {
   1311   1.1      fvdl 		fprintf(stderr, "handle_reply:  reply xid: %d fi addr: %p\n",
   1312   1.1      fvdl 			reply_msg.rm_xid, fi);
   1313   1.1      fvdl 	}
   1314   1.1      fvdl #endif
   1315   1.1      fvdl 	if (fi == NULL) {
   1316   1.1      fvdl 		goto done;
   1317   1.1      fvdl 	}
   1318   1.1      fvdl 	_seterr_reply(&reply_msg, &reply_error);
   1319   1.1      fvdl 	if (reply_error.re_status != RPC_SUCCESS) {
   1320   1.1      fvdl 		if (debugging)
   1321   1.1      fvdl 			(void) fprintf(stderr, "handle_reply:  %s\n",
   1322   1.1      fvdl 				clnt_sperrno(reply_error.re_status));
   1323   1.1      fvdl 		send_svcsyserr(xprt, fi);
   1324   1.1      fvdl 		goto done;
   1325   1.1      fvdl 	}
   1326   1.1      fvdl 	pos = XDR_GETPOS(&reply_xdrs);
   1327   1.1      fvdl 	len = inlen - pos;
   1328   1.1      fvdl 	a.rmt_args.args = &buffer[pos];
   1329   1.1      fvdl 	a.rmt_args.arglen = len;
   1330   1.1      fvdl 	a.rmt_uaddr = fi->uaddr;
   1331   1.1      fvdl 	a.rmt_localvers = fi->versnum;
   1332   1.1      fvdl 
   1333   1.1      fvdl 	xprt_set_caller(xprt, fi);
   1334   1.1      fvdl #ifdef	SVC_RUN_DEBUG
   1335   1.1      fvdl 	uaddr =	taddr2uaddr(rpcbind_get_conf("udp"),
   1336   1.1      fvdl 				    svc_getrpccaller(xprt));
   1337   1.1      fvdl 	if (debugging) {
   1338   1.1      fvdl 		fprintf(stderr, "handle_reply:  forwarding address %s to %s\n",
   1339  1.12  christos 			a.rmt_uaddr, uaddr ? uaddr : rpcbind_unknown);
   1340   1.1      fvdl 	}
   1341   1.1      fvdl 	if (uaddr)
   1342  1.20  christos 		free(uaddr);
   1343   1.1      fvdl #endif
   1344   1.1      fvdl 	svc_sendreply(xprt, (xdrproc_t) xdr_rmtcall_result, (char *) &a);
   1345   1.1      fvdl done:
   1346   1.4      fvdl 	if (buffer)
   1347   1.4      fvdl 		free(buffer);
   1348   1.4      fvdl 
   1349   1.1      fvdl 	if (reply_msg.rm_xid == 0) {
   1350   1.1      fvdl #ifdef	SVC_RUN_DEBUG
   1351   1.1      fvdl 	if (debugging) {
   1352   1.1      fvdl 		fprintf(stderr, "handle_reply:  NULL xid on exit!\n");
   1353   1.1      fvdl 	}
   1354   1.1      fvdl #endif
   1355   1.1      fvdl 	} else
   1356   1.1      fvdl 		(void) free_slot_by_xid(reply_msg.rm_xid);
   1357   1.1      fvdl 	return;
   1358   1.1      fvdl }
   1359   1.1      fvdl 
   1360   1.1      fvdl static void
   1361   1.1      fvdl find_versions(rpcprog_t prog, char *netid, rpcvers_t *lowvp, rpcvers_t *highvp)
   1362   1.1      fvdl {
   1363  1.17  christos 	rpcblist_ptr rbl;
   1364  1.14     lukem 	rpcvers_t lowv = 0;
   1365  1.14     lukem 	rpcvers_t highv = 0;
   1366   1.1      fvdl 
   1367   1.1      fvdl 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
   1368   1.1      fvdl 		if ((rbl->rpcb_map.r_prog != prog) ||
   1369   1.1      fvdl 		    ((rbl->rpcb_map.r_netid != NULL) &&
   1370   1.1      fvdl 			(strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
   1371   1.1      fvdl 			continue;
   1372   1.1      fvdl 		if (lowv == 0) {
   1373   1.1      fvdl 			highv = rbl->rpcb_map.r_vers;
   1374   1.1      fvdl 			lowv = highv;
   1375   1.1      fvdl 		} else if (rbl->rpcb_map.r_vers < lowv) {
   1376   1.1      fvdl 			lowv = rbl->rpcb_map.r_vers;
   1377   1.1      fvdl 		} else if (rbl->rpcb_map.r_vers > highv) {
   1378   1.1      fvdl 			highv = rbl->rpcb_map.r_vers;
   1379   1.1      fvdl 		}
   1380   1.1      fvdl 	}
   1381   1.1      fvdl 	*lowvp = lowv;
   1382   1.1      fvdl 	*highvp = highv;
   1383   1.1      fvdl 	return;
   1384   1.1      fvdl }
   1385   1.1      fvdl 
   1386   1.1      fvdl /*
   1387   1.1      fvdl  * returns the item with the given program, version number and netid.
   1388   1.1      fvdl  * If that version number is not found, it returns the item with that
   1389   1.1      fvdl  * program number, so that address is now returned to the caller. The
   1390   1.1      fvdl  * caller when makes a call to this program, version number, the call
   1391   1.1      fvdl  * will fail and it will return with PROGVERS_MISMATCH. The user can
   1392   1.1      fvdl  * then determine the highest and the lowest version number for this
   1393   1.1      fvdl  * program using clnt_geterr() and use those program version numbers.
   1394   1.1      fvdl  *
   1395   1.1      fvdl  * Returns the RPCBLIST for the given prog, vers and netid
   1396   1.1      fvdl  */
   1397   1.1      fvdl static rpcblist_ptr
   1398   1.1      fvdl find_service(rpcprog_t prog, rpcvers_t vers, char *netid)
   1399   1.1      fvdl {
   1400  1.17  christos 	rpcblist_ptr hit = NULL;
   1401  1.17  christos 	rpcblist_ptr rbl;
   1402   1.1      fvdl 
   1403   1.1      fvdl 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
   1404   1.1      fvdl 		if ((rbl->rpcb_map.r_prog != prog) ||
   1405   1.1      fvdl 		    ((rbl->rpcb_map.r_netid != NULL) &&
   1406   1.1      fvdl 			(strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
   1407   1.1      fvdl 			continue;
   1408   1.1      fvdl 		hit = rbl;
   1409   1.1      fvdl 		if (rbl->rpcb_map.r_vers == vers)
   1410   1.1      fvdl 			break;
   1411   1.1      fvdl 	}
   1412   1.1      fvdl 	return (hit);
   1413   1.1      fvdl }
   1414   1.1      fvdl 
   1415   1.1      fvdl /*
   1416   1.1      fvdl  * Copies the name associated with the uid of the caller and returns
   1417   1.1      fvdl  * a pointer to it.  Similar to getwd().
   1418   1.1      fvdl  */
   1419   1.1      fvdl static char *
   1420   1.1      fvdl getowner(SVCXPRT *transp, char *owner, size_t ownersize)
   1421   1.1      fvdl {
   1422   1.1      fvdl 	struct sockcred *sc;
   1423   1.1      fvdl 
   1424   1.1      fvdl 	sc = __svc_getcallercreds(transp);
   1425   1.1      fvdl 	if (sc == NULL)
   1426  1.12  christos 		strlcpy(owner, rpcbind_unknown, ownersize);
   1427   1.1      fvdl 	else if (sc->sc_uid == 0)
   1428  1.12  christos 		strlcpy(owner, rpcbind_superuser, ownersize);
   1429   1.1      fvdl 	else
   1430   1.1      fvdl 		snprintf(owner, ownersize, "%d", sc->sc_uid);
   1431   1.1      fvdl 
   1432   1.1      fvdl 	return owner;
   1433   1.1      fvdl }
   1434   1.1      fvdl 
   1435   1.1      fvdl #ifdef PORTMAP
   1436   1.1      fvdl /*
   1437   1.1      fvdl  * Add this to the pmap list only if it is UDP or TCP.
   1438   1.1      fvdl  */
   1439   1.1      fvdl static int
   1440   1.1      fvdl add_pmaplist(RPCB *arg)
   1441   1.1      fvdl {
   1442   1.1      fvdl 	struct pmap pmap;
   1443   1.1      fvdl 	struct pmaplist *pml;
   1444   1.1      fvdl 	int h1, h2, h3, h4, p1, p2;
   1445   1.1      fvdl 
   1446   1.1      fvdl 	if (strcmp(arg->r_netid, udptrans) == 0) {
   1447   1.1      fvdl 		/* It is UDP! */
   1448   1.1      fvdl 		pmap.pm_prot = IPPROTO_UDP;
   1449   1.1      fvdl 	} else if (strcmp(arg->r_netid, tcptrans) == 0) {
   1450   1.1      fvdl 		/* It is TCP */
   1451   1.1      fvdl 		pmap.pm_prot = IPPROTO_TCP;
   1452   1.1      fvdl 	} else
   1453  1.20  christos 		/* Not an IP protocol */
   1454   1.1      fvdl 		return (0);
   1455   1.1      fvdl 
   1456   1.1      fvdl 	/* interpret the universal address for TCP/IP */
   1457   1.1      fvdl 	if (sscanf(arg->r_addr, "%d.%d.%d.%d.%d.%d",
   1458   1.1      fvdl 		&h1, &h2, &h3, &h4, &p1, &p2) != 6)
   1459   1.1      fvdl 		return (0);
   1460   1.1      fvdl 	pmap.pm_port = ((p1 & 0xff) << 8) + (p2 & 0xff);
   1461   1.1      fvdl 	pmap.pm_prog = arg->r_prog;
   1462   1.1      fvdl 	pmap.pm_vers = arg->r_vers;
   1463   1.1      fvdl 	/*
   1464   1.1      fvdl 	 * add to END of list
   1465   1.1      fvdl 	 */
   1466  1.21  christos 	pml = malloc(sizeof(*pml));
   1467   1.1      fvdl 	if (pml == NULL) {
   1468  1.22  christos 		syslog(LOG_ERR, "%s: Cannot allocate memory", __func__);
   1469   1.1      fvdl 		return (1);
   1470   1.1      fvdl 	}
   1471   1.1      fvdl 	pml->pml_map = pmap;
   1472   1.1      fvdl 	pml->pml_next = NULL;
   1473   1.1      fvdl 	if (list_pml == NULL) {
   1474   1.1      fvdl 		list_pml = pml;
   1475   1.1      fvdl 	} else {
   1476   1.1      fvdl 		struct pmaplist *fnd;
   1477   1.1      fvdl 
   1478   1.1      fvdl 		/* Attach to the end of the list */
   1479   1.1      fvdl 		for (fnd = list_pml; fnd->pml_next; fnd = fnd->pml_next)
   1480  1.22  christos 			continue;
   1481   1.1      fvdl 		fnd->pml_next = pml;
   1482   1.1      fvdl 	}
   1483   1.1      fvdl 	return (0);
   1484   1.1      fvdl }
   1485   1.1      fvdl 
   1486   1.1      fvdl /*
   1487   1.1      fvdl  * Delete this from the pmap list only if it is UDP or TCP.
   1488   1.1      fvdl  */
   1489   1.1      fvdl static int
   1490   1.1      fvdl del_pmaplist(RPCB *arg)
   1491   1.1      fvdl {
   1492   1.1      fvdl 	struct pmaplist *pml;
   1493   1.1      fvdl 	struct pmaplist *prevpml, *fnd;
   1494  1.14     lukem 	unsigned long prot;
   1495   1.1      fvdl 
   1496   1.1      fvdl 	if (strcmp(arg->r_netid, udptrans) == 0) {
   1497   1.1      fvdl 		/* It is UDP! */
   1498   1.1      fvdl 		prot = IPPROTO_UDP;
   1499   1.1      fvdl 	} else if (strcmp(arg->r_netid, tcptrans) == 0) {
   1500   1.1      fvdl 		/* It is TCP */
   1501   1.1      fvdl 		prot = IPPROTO_TCP;
   1502  1.10      fvdl 	} else if (arg->r_netid[0] == 0) {
   1503   1.1      fvdl 		prot = 0;	/* Remove all occurrences */
   1504   1.1      fvdl 	} else {
   1505  1.20  christos 		/* Not an IP protocol */
   1506   1.1      fvdl 		return (0);
   1507   1.1      fvdl 	}
   1508   1.1      fvdl 	for (prevpml = NULL, pml = list_pml; pml; /* cstyle */) {
   1509   1.1      fvdl 		if ((pml->pml_map.pm_prog != arg->r_prog) ||
   1510   1.1      fvdl 			(pml->pml_map.pm_vers != arg->r_vers) ||
   1511   1.1      fvdl 			(prot && (pml->pml_map.pm_prot != prot))) {
   1512   1.1      fvdl 			/* both pml & prevpml move forwards */
   1513   1.1      fvdl 			prevpml = pml;
   1514   1.1      fvdl 			pml = pml->pml_next;
   1515   1.1      fvdl 			continue;
   1516   1.1      fvdl 		}
   1517   1.1      fvdl 		/* found it; pml moves forward, prevpml stays */
   1518   1.1      fvdl 		fnd = pml;
   1519   1.1      fvdl 		pml = pml->pml_next;
   1520   1.1      fvdl 		if (prevpml == NULL)
   1521   1.1      fvdl 			list_pml = pml;
   1522   1.1      fvdl 		else
   1523   1.1      fvdl 			prevpml->pml_next = pml;
   1524  1.20  christos 		free(fnd);
   1525   1.1      fvdl 	}
   1526   1.1      fvdl 	return (0);
   1527   1.1      fvdl }
   1528   1.1      fvdl #endif /* PORTMAP */
   1529