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