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