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