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