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