xref: /freebsd/usr.sbin/rpcbind/rpcb_svc_com.c (revision 09d4f7789ebd4fa9685baf4f7fc548d5d24a8342)
18360efbdSAlfred Perlstein /*	$NetBSD: rpcb_svc_com.c,v 1.6 2000/08/03 00:07:22 fvdl Exp $	*/
28360efbdSAlfred Perlstein /*	$FreeBSD$ */
38360efbdSAlfred Perlstein 
48360efbdSAlfred Perlstein /*
58360efbdSAlfred Perlstein  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
68360efbdSAlfred Perlstein  * unrestricted use provided that this legend is included on all tape
78360efbdSAlfred Perlstein  * media and as a part of the software program in whole or part.  Users
88360efbdSAlfred Perlstein  * may copy or modify Sun RPC without charge, but are not authorized
98360efbdSAlfred Perlstein  * to license or distribute it to anyone else except as part of a product or
108360efbdSAlfred Perlstein  * program developed by the user.
118360efbdSAlfred Perlstein  *
128360efbdSAlfred Perlstein  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
138360efbdSAlfred Perlstein  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
148360efbdSAlfred Perlstein  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
158360efbdSAlfred Perlstein  *
168360efbdSAlfred Perlstein  * Sun RPC is provided with no support and without any obligation on the
178360efbdSAlfred Perlstein  * part of Sun Microsystems, Inc. to assist in its use, correction,
188360efbdSAlfred Perlstein  * modification or enhancement.
198360efbdSAlfred Perlstein  *
208360efbdSAlfred Perlstein  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
218360efbdSAlfred Perlstein  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
228360efbdSAlfred Perlstein  * OR ANY PART THEREOF.
238360efbdSAlfred Perlstein  *
248360efbdSAlfred Perlstein  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
258360efbdSAlfred Perlstein  * or profits or other special, indirect and consequential damages, even if
268360efbdSAlfred Perlstein  * Sun has been advised of the possibility of such damages.
278360efbdSAlfred Perlstein  *
288360efbdSAlfred Perlstein  * Sun Microsystems, Inc.
298360efbdSAlfred Perlstein  * 2550 Garcia Avenue
308360efbdSAlfred Perlstein  * Mountain View, California  94043
318360efbdSAlfred Perlstein  */
328360efbdSAlfred Perlstein /*
338360efbdSAlfred Perlstein  * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
348360efbdSAlfred Perlstein  */
358360efbdSAlfred Perlstein 
368360efbdSAlfred Perlstein /* #ident	"@(#)rpcb_svc_com.c	1.18	94/05/02 SMI" */
378360efbdSAlfred Perlstein 
388360efbdSAlfred Perlstein /*
398360efbdSAlfred Perlstein  * rpcb_svc_com.c
408360efbdSAlfred Perlstein  * The commom server procedure for the rpcbind.
418360efbdSAlfred Perlstein  */
428360efbdSAlfred Perlstein 
438360efbdSAlfred Perlstein #include <sys/types.h>
448360efbdSAlfred Perlstein #include <sys/stat.h>
458360efbdSAlfred Perlstein #include <sys/param.h>
468360efbdSAlfred Perlstein #include <sys/poll.h>
478360efbdSAlfred Perlstein #include <sys/socket.h>
488360efbdSAlfred Perlstein #include <rpc/rpc.h>
498360efbdSAlfred Perlstein #include <rpc/rpcb_prot.h>
508360efbdSAlfred Perlstein #include <rpc/svc_dg.h>
518360efbdSAlfred Perlstein #include <netconfig.h>
528360efbdSAlfred Perlstein #include <errno.h>
538360efbdSAlfred Perlstein #include <syslog.h>
548360efbdSAlfred Perlstein #include <unistd.h>
558360efbdSAlfred Perlstein #include <stdio.h>
568360efbdSAlfred Perlstein #ifdef PORTMAP
578360efbdSAlfred Perlstein #include <netinet/in.h>
588360efbdSAlfred Perlstein #include <rpc/pmap_prot.h>
598360efbdSAlfred Perlstein #endif /* PORTMAP */
608360efbdSAlfred Perlstein #include <string.h>
618360efbdSAlfred Perlstein #include <stdlib.h>
628360efbdSAlfred Perlstein 
638360efbdSAlfred Perlstein #include "rpcbind.h"
648360efbdSAlfred Perlstein 
658360efbdSAlfred Perlstein #define RPC_BUF_MAX	65536	/* can be raised if required */
668360efbdSAlfred Perlstein 
678360efbdSAlfred Perlstein static char *nullstring = "";
688360efbdSAlfred Perlstein static int rpcb_rmtcalls;
698360efbdSAlfred Perlstein 
708360efbdSAlfred Perlstein struct rmtcallfd_list {
718360efbdSAlfred Perlstein 	int fd;
728360efbdSAlfred Perlstein 	SVCXPRT *xprt;
738360efbdSAlfred Perlstein 	char *netid;
748360efbdSAlfred Perlstein 	struct rmtcallfd_list *next;
758360efbdSAlfred Perlstein };
768360efbdSAlfred Perlstein 
778360efbdSAlfred Perlstein #define NFORWARD        64
788360efbdSAlfred Perlstein #define MAXTIME_OFF     300     /* 5 minutes */
798360efbdSAlfred Perlstein 
808360efbdSAlfred Perlstein struct finfo {
818360efbdSAlfred Perlstein 	int             flag;
828360efbdSAlfred Perlstein #define FINFO_ACTIVE    0x1
838360efbdSAlfred Perlstein 	u_int32_t       caller_xid;
848360efbdSAlfred Perlstein         struct netbuf   *caller_addr;
858360efbdSAlfred Perlstein 	u_int32_t       forward_xid;
868360efbdSAlfred Perlstein 	int             forward_fd;
878360efbdSAlfred Perlstein 	char            *uaddr;
888360efbdSAlfred Perlstein 	rpcproc_t       reply_type;
898360efbdSAlfred Perlstein 	rpcvers_t       versnum;
908360efbdSAlfred Perlstein 	time_t          time;
918360efbdSAlfred Perlstein };
928360efbdSAlfred Perlstein static struct finfo     FINFO[NFORWARD];
938360efbdSAlfred Perlstein 
948360efbdSAlfred Perlstein 
958360efbdSAlfred Perlstein static bool_t xdr_encap_parms __P((XDR *, struct encap_parms *));
968360efbdSAlfred Perlstein static bool_t xdr_rmtcall_args __P((XDR *, struct r_rmtcall_args *));
978360efbdSAlfred Perlstein static bool_t xdr_rmtcall_result __P((XDR *, struct r_rmtcall_args *));
988360efbdSAlfred Perlstein static bool_t xdr_opaque_parms __P((XDR *, struct r_rmtcall_args *));
998360efbdSAlfred Perlstein static int find_rmtcallfd_by_netid __P((char *));
1008360efbdSAlfred Perlstein static SVCXPRT *find_rmtcallxprt_by_fd __P((int));
10198f36a35SIan Dowse static int forward_register __P((u_int32_t, struct netbuf *, int, char *,
10298f36a35SIan Dowse     rpcproc_t, rpcvers_t, u_int32_t *));
1038360efbdSAlfred Perlstein static struct finfo *forward_find __P((u_int32_t));
1048360efbdSAlfred Perlstein static int free_slot_by_xid __P((u_int32_t));
1058360efbdSAlfred Perlstein static int free_slot_by_index __P((int));
1068360efbdSAlfred Perlstein static int netbufcmp __P((struct netbuf *, struct netbuf *));
1078360efbdSAlfred Perlstein static struct netbuf *netbufdup __P((struct netbuf *));
1088360efbdSAlfred Perlstein static void netbuffree __P((struct netbuf *));
1098360efbdSAlfred Perlstein static int check_rmtcalls __P((struct pollfd *, int));
1108360efbdSAlfred Perlstein static void xprt_set_caller __P((SVCXPRT *, struct finfo *));
1118360efbdSAlfred Perlstein static void send_svcsyserr __P((SVCXPRT *, struct finfo *));
1128360efbdSAlfred Perlstein static void handle_reply __P((int, SVCXPRT *));
1138360efbdSAlfred Perlstein static void find_versions __P((rpcprog_t, char *, rpcvers_t *, rpcvers_t *));
1148360efbdSAlfred Perlstein static rpcblist_ptr find_service __P((rpcprog_t, rpcvers_t, char *));
1158360efbdSAlfred Perlstein static char *getowner __P((SVCXPRT *, char *, size_t));
1168360efbdSAlfred Perlstein static int add_pmaplist __P((RPCB *));
1178360efbdSAlfred Perlstein static int del_pmaplist __P((RPCB *));
1188360efbdSAlfred Perlstein 
1198360efbdSAlfred Perlstein /*
1208360efbdSAlfred Perlstein  * Set a mapping of program, version, netid
1218360efbdSAlfred Perlstein  */
1228360efbdSAlfred Perlstein /* ARGSUSED */
1238360efbdSAlfred Perlstein void *
1248360efbdSAlfred Perlstein rpcbproc_set_com(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
1258360efbdSAlfred Perlstein 		 rpcvers_t rpcbversnum)
1268360efbdSAlfred Perlstein {
1278360efbdSAlfred Perlstein 	RPCB *regp = (RPCB *)arg;
1288360efbdSAlfred Perlstein 	static bool_t ans;
1298360efbdSAlfred Perlstein 	char owner[64];
1308360efbdSAlfred Perlstein 
1318360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
1328360efbdSAlfred Perlstein 	if (debugging)
1338360efbdSAlfred Perlstein 		fprintf(stderr, "RPCB_SET request for (%lu, %lu, %s, %s) : ",
1348360efbdSAlfred Perlstein 		    (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
1358360efbdSAlfred Perlstein 		    regp->r_netid, regp->r_addr);
1368360efbdSAlfred Perlstein #endif
1378360efbdSAlfred Perlstein 	ans = map_set(regp, getowner(transp, owner, sizeof owner));
1388360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
1398360efbdSAlfred Perlstein 	if (debugging)
1408360efbdSAlfred Perlstein 		fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
1418360efbdSAlfred Perlstein #endif
1428360efbdSAlfred Perlstein 	/* XXX: should have used some defined constant here */
1438360efbdSAlfred Perlstein 	rpcbs_set(rpcbversnum - 2, ans);
1448360efbdSAlfred Perlstein 	return (void *)&ans;
1458360efbdSAlfred Perlstein }
1468360efbdSAlfred Perlstein 
1478360efbdSAlfred Perlstein bool_t
1488360efbdSAlfred Perlstein map_set(RPCB *regp, char *owner)
1498360efbdSAlfred Perlstein {
1508360efbdSAlfred Perlstein 	RPCB reg, *a;
1518360efbdSAlfred Perlstein 	rpcblist_ptr rbl, fnd;
1528360efbdSAlfred Perlstein 
1538360efbdSAlfred Perlstein 	reg = *regp;
1548360efbdSAlfred Perlstein 	/*
1558360efbdSAlfred Perlstein 	 * check to see if already used
1568360efbdSAlfred Perlstein 	 * find_service returns a hit even if
1578360efbdSAlfred Perlstein 	 * the versions don't match, so check for it
1588360efbdSAlfred Perlstein 	 */
1598360efbdSAlfred Perlstein 	fnd = find_service(reg.r_prog, reg.r_vers, reg.r_netid);
1608360efbdSAlfred Perlstein 	if (fnd && (fnd->rpcb_map.r_vers == reg.r_vers)) {
1618360efbdSAlfred Perlstein 		if (!strcmp(fnd->rpcb_map.r_addr, reg.r_addr))
1628360efbdSAlfred Perlstein 			/*
1638360efbdSAlfred Perlstein 			 * if these match then it is already
1648360efbdSAlfred Perlstein 			 * registered so just say "OK".
1658360efbdSAlfred Perlstein 			 */
1668360efbdSAlfred Perlstein 			return (TRUE);
1678360efbdSAlfred Perlstein 		else
1688360efbdSAlfred Perlstein 			return (FALSE);
1698360efbdSAlfred Perlstein 	}
1708360efbdSAlfred Perlstein 	/*
1718360efbdSAlfred Perlstein 	 * add to the end of the list
1728360efbdSAlfred Perlstein 	 */
1738360efbdSAlfred Perlstein 	rbl = (rpcblist_ptr) malloc((u_int)sizeof (RPCBLIST));
1748360efbdSAlfred Perlstein 	if (rbl == (rpcblist_ptr)NULL) {
1758360efbdSAlfred Perlstein 		return (FALSE);
1768360efbdSAlfred Perlstein 	}
1778360efbdSAlfred Perlstein 	a = &(rbl->rpcb_map);
1788360efbdSAlfred Perlstein 	a->r_prog = reg.r_prog;
1798360efbdSAlfred Perlstein 	a->r_vers = reg.r_vers;
1808360efbdSAlfred Perlstein 	a->r_netid = strdup(reg.r_netid);
1818360efbdSAlfred Perlstein 	a->r_addr = strdup(reg.r_addr);
1828360efbdSAlfred Perlstein 	a->r_owner = strdup(owner);
1838360efbdSAlfred Perlstein 	if (!a->r_addr || !a->r_netid || !a->r_owner) {
1848360efbdSAlfred Perlstein 		if (a->r_netid)
18509d4f778SIan Dowse 			free(a->r_netid);
1868360efbdSAlfred Perlstein 		if (a->r_addr)
18709d4f778SIan Dowse 			free(a->r_addr);
1888360efbdSAlfred Perlstein 		if (a->r_owner)
18909d4f778SIan Dowse 			free(a->r_owner);
19009d4f778SIan Dowse 		free(rbl);
1918360efbdSAlfred Perlstein 		return (FALSE);
1928360efbdSAlfred Perlstein 	}
1938360efbdSAlfred Perlstein 	rbl->rpcb_next = (rpcblist_ptr)NULL;
1948360efbdSAlfred Perlstein 	if (list_rbl == NULL) {
1958360efbdSAlfred Perlstein 		list_rbl = rbl;
1968360efbdSAlfred Perlstein 	} else {
1978360efbdSAlfred Perlstein 		for (fnd = list_rbl; fnd->rpcb_next;
1988360efbdSAlfred Perlstein 			fnd = fnd->rpcb_next)
1998360efbdSAlfred Perlstein 			;
2008360efbdSAlfred Perlstein 		fnd->rpcb_next = rbl;
2018360efbdSAlfred Perlstein 	}
2028360efbdSAlfred Perlstein #ifdef PORTMAP
2038360efbdSAlfred Perlstein 	(void) add_pmaplist(regp);
2048360efbdSAlfred Perlstein #endif
2058360efbdSAlfred Perlstein 	return (TRUE);
2068360efbdSAlfred Perlstein }
2078360efbdSAlfred Perlstein 
2088360efbdSAlfred Perlstein /*
2098360efbdSAlfred Perlstein  * Unset a mapping of program, version, netid
2108360efbdSAlfred Perlstein  */
2118360efbdSAlfred Perlstein /* ARGSUSED */
2128360efbdSAlfred Perlstein void *
2138360efbdSAlfred Perlstein rpcbproc_unset_com(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
2148360efbdSAlfred Perlstein 		   rpcvers_t rpcbversnum)
2158360efbdSAlfred Perlstein {
2168360efbdSAlfred Perlstein 	RPCB *regp = (RPCB *)arg;
2178360efbdSAlfred Perlstein 	static bool_t ans;
2188360efbdSAlfred Perlstein 	char owner[64];
2198360efbdSAlfred Perlstein 
2208360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
2218360efbdSAlfred Perlstein 	if (debugging)
2228360efbdSAlfred Perlstein 		fprintf(stderr, "RPCB_UNSET request for (%lu, %lu, %s) : ",
2238360efbdSAlfred Perlstein 		    (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
2248360efbdSAlfred Perlstein 		    regp->r_netid);
2258360efbdSAlfred Perlstein #endif
2268360efbdSAlfred Perlstein 	ans = map_unset(regp, getowner(transp, owner, sizeof owner));
2278360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
2288360efbdSAlfred Perlstein 	if (debugging)
2298360efbdSAlfred Perlstein 		fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
2308360efbdSAlfred Perlstein #endif
2318360efbdSAlfred Perlstein 	/* XXX: should have used some defined constant here */
2328360efbdSAlfred Perlstein 	rpcbs_unset(rpcbversnum - 2, ans);
2338360efbdSAlfred Perlstein 	return (void *)&ans;
2348360efbdSAlfred Perlstein }
2358360efbdSAlfred Perlstein 
2368360efbdSAlfred Perlstein bool_t
2378360efbdSAlfred Perlstein map_unset(RPCB *regp, char *owner)
2388360efbdSAlfred Perlstein {
2398360efbdSAlfred Perlstein 	int ans = 0;
2408360efbdSAlfred Perlstein 	rpcblist_ptr rbl, prev, tmp;
2418360efbdSAlfred Perlstein 
2428360efbdSAlfred Perlstein 	if (owner == NULL)
2438360efbdSAlfred Perlstein 		return (0);
2448360efbdSAlfred Perlstein 
2458360efbdSAlfred Perlstein 	for (prev = NULL, rbl = list_rbl; rbl; /* cstyle */) {
2468360efbdSAlfred Perlstein 		if ((rbl->rpcb_map.r_prog != regp->r_prog) ||
2478360efbdSAlfred Perlstein 			(rbl->rpcb_map.r_vers != regp->r_vers) ||
2488360efbdSAlfred Perlstein 			(regp->r_netid[0] && strcasecmp(regp->r_netid,
2498360efbdSAlfred Perlstein 				rbl->rpcb_map.r_netid))) {
2508360efbdSAlfred Perlstein 			/* both rbl & prev move forwards */
2518360efbdSAlfred Perlstein 			prev = rbl;
2528360efbdSAlfred Perlstein 			rbl = rbl->rpcb_next;
2538360efbdSAlfred Perlstein 			continue;
2548360efbdSAlfred Perlstein 		}
2558360efbdSAlfred Perlstein 		/*
2568360efbdSAlfred Perlstein 		 * Check whether appropriate uid. Unset only
2578360efbdSAlfred Perlstein 		 * if superuser or the owner itself.
2588360efbdSAlfred Perlstein 		 */
2598360efbdSAlfred Perlstein 		if (strcmp(owner, "superuser") &&
2608360efbdSAlfred Perlstein 			strcmp(rbl->rpcb_map.r_owner, owner))
2618360efbdSAlfred Perlstein 			return (0);
2628360efbdSAlfred Perlstein 		/* found it; rbl moves forward, prev stays */
2638360efbdSAlfred Perlstein 		ans = 1;
2648360efbdSAlfred Perlstein 		tmp = rbl;
2658360efbdSAlfred Perlstein 		rbl = rbl->rpcb_next;
2668360efbdSAlfred Perlstein 		if (prev == NULL)
2678360efbdSAlfred Perlstein 			list_rbl = rbl;
2688360efbdSAlfred Perlstein 		else
2698360efbdSAlfred Perlstein 			prev->rpcb_next = rbl;
27009d4f778SIan Dowse 		free(tmp->rpcb_map.r_addr);
27109d4f778SIan Dowse 		free(tmp->rpcb_map.r_netid);
27209d4f778SIan Dowse 		free(tmp->rpcb_map.r_owner);
27309d4f778SIan Dowse 		free(tmp);
2748360efbdSAlfred Perlstein 	}
2758360efbdSAlfred Perlstein #ifdef PORTMAP
2768360efbdSAlfred Perlstein 	if (ans)
2778360efbdSAlfred Perlstein 		(void) del_pmaplist(regp);
2788360efbdSAlfred Perlstein #endif
2798360efbdSAlfred Perlstein 	/*
2808360efbdSAlfred Perlstein 	 * We return 1 either when the entry was not there or it
2818360efbdSAlfred Perlstein 	 * was able to unset it.  It can come to this point only if
2828360efbdSAlfred Perlstein 	 * atleast one of the conditions is true.
2838360efbdSAlfred Perlstein 	 */
2848360efbdSAlfred Perlstein 	return (1);
2858360efbdSAlfred Perlstein }
2868360efbdSAlfred Perlstein 
2878360efbdSAlfred Perlstein void
2888360efbdSAlfred Perlstein delete_prog(int prog)
2898360efbdSAlfred Perlstein {
2908360efbdSAlfred Perlstein 	RPCB reg;
2918360efbdSAlfred Perlstein 	register rpcblist_ptr rbl;
2928360efbdSAlfred Perlstein 
2938360efbdSAlfred Perlstein 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
2948360efbdSAlfred Perlstein 		if ((rbl->rpcb_map.r_prog != prog))
2958360efbdSAlfred Perlstein 			continue;
2968360efbdSAlfred Perlstein 		if (is_bound(rbl->rpcb_map.r_netid, rbl->rpcb_map.r_addr))
2978360efbdSAlfred Perlstein 			continue;
2988360efbdSAlfred Perlstein 		reg.r_prog = rbl->rpcb_map.r_prog;
2998360efbdSAlfred Perlstein 		reg.r_vers = rbl->rpcb_map.r_vers;
3008360efbdSAlfred Perlstein 		reg.r_netid = strdup(rbl->rpcb_map.r_netid);
3018360efbdSAlfred Perlstein 		(void) map_unset(&reg, "superuser");
3028360efbdSAlfred Perlstein 		free(reg.r_netid);
3038360efbdSAlfred Perlstein 	}
3048360efbdSAlfred Perlstein }
3058360efbdSAlfred Perlstein 
3068360efbdSAlfred Perlstein void *
3078360efbdSAlfred Perlstein rpcbproc_getaddr_com(RPCB *regp, struct svc_req *rqstp, SVCXPRT *transp,
3088360efbdSAlfred Perlstein 		     rpcvers_t rpcbversnum, rpcvers_t verstype)
3098360efbdSAlfred Perlstein {
3108360efbdSAlfred Perlstein 	static char *uaddr;
3118360efbdSAlfred Perlstein 	char *saddr = NULL;
3128360efbdSAlfred Perlstein 	rpcblist_ptr fnd;
3138360efbdSAlfred Perlstein 
31409d4f778SIan Dowse 	if (uaddr != NULL && uaddr != nullstring) {
31509d4f778SIan Dowse 		free(uaddr);
31609d4f778SIan Dowse 		uaddr = NULL;
31709d4f778SIan Dowse 	}
3188360efbdSAlfred Perlstein 	fnd = find_service(regp->r_prog, regp->r_vers, transp->xp_netid);
3198360efbdSAlfred Perlstein 	if (fnd && ((verstype == RPCB_ALLVERS) ||
3208360efbdSAlfred Perlstein 		    (regp->r_vers == fnd->rpcb_map.r_vers))) {
3218360efbdSAlfred Perlstein 		if (*(regp->r_addr) != '\0') {  /* may contain a hint about */
3228360efbdSAlfred Perlstein 			saddr = regp->r_addr;   /* the interface that we    */
3238360efbdSAlfred Perlstein 		}				/* should use */
3248360efbdSAlfred Perlstein 		if (!(uaddr = mergeaddr(transp, transp->xp_netid,
3258360efbdSAlfred Perlstein 				fnd->rpcb_map.r_addr, saddr))) {
3268360efbdSAlfred Perlstein 			/* Try whatever we have */
3278360efbdSAlfred Perlstein 			uaddr = strdup(fnd->rpcb_map.r_addr);
3288360efbdSAlfred Perlstein 		} else if (!uaddr[0]) {
3298360efbdSAlfred Perlstein 			/*
3308360efbdSAlfred Perlstein 			 * The server died.  Unset all versions of this prog.
3318360efbdSAlfred Perlstein 			 */
3328360efbdSAlfred Perlstein 			delete_prog(regp->r_prog);
3338360efbdSAlfred Perlstein 			uaddr = nullstring;
3348360efbdSAlfred Perlstein 		}
3358360efbdSAlfred Perlstein 	} else {
3368360efbdSAlfred Perlstein 		uaddr = nullstring;
3378360efbdSAlfred Perlstein 	}
3388360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
3398360efbdSAlfred Perlstein 	if (debugging)
3408360efbdSAlfred Perlstein 		fprintf(stderr, "getaddr: %s\n", uaddr);
3418360efbdSAlfred Perlstein #endif
3428360efbdSAlfred Perlstein 	/* XXX: should have used some defined constant here */
3438360efbdSAlfred Perlstein 	rpcbs_getaddr(rpcbversnum - 2, regp->r_prog, regp->r_vers,
3448360efbdSAlfred Perlstein 		transp->xp_netid, uaddr);
3458360efbdSAlfred Perlstein 	return (void *)&uaddr;
3468360efbdSAlfred Perlstein }
3478360efbdSAlfred Perlstein 
3488360efbdSAlfred Perlstein /* ARGSUSED */
3498360efbdSAlfred Perlstein void *
3508360efbdSAlfred Perlstein rpcbproc_gettime_com(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
3518360efbdSAlfred Perlstein 		     rpcvers_t rpcbversnum)
3528360efbdSAlfred Perlstein {
3538360efbdSAlfred Perlstein 	static time_t curtime;
3548360efbdSAlfred Perlstein 
3558360efbdSAlfred Perlstein 	(void) time(&curtime);
3568360efbdSAlfred Perlstein 	return (void *)&curtime;
3578360efbdSAlfred Perlstein }
3588360efbdSAlfred Perlstein 
3598360efbdSAlfred Perlstein /*
3608360efbdSAlfred Perlstein  * Convert uaddr to taddr. Should be used only by
3618360efbdSAlfred Perlstein  * local servers/clients. (kernel level stuff only)
3628360efbdSAlfred Perlstein  */
3638360efbdSAlfred Perlstein /* ARGSUSED */
3648360efbdSAlfred Perlstein void *
3658360efbdSAlfred Perlstein rpcbproc_uaddr2taddr_com(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
3668360efbdSAlfred Perlstein 			 rpcvers_t rpcbversnum)
3678360efbdSAlfred Perlstein {
3688360efbdSAlfred Perlstein 	char **uaddrp = (char **)arg;
3698360efbdSAlfred Perlstein 	struct netconfig *nconf;
3708360efbdSAlfred Perlstein 	static struct netbuf nbuf;
3718360efbdSAlfred Perlstein 	static struct netbuf *taddr;
3728360efbdSAlfred Perlstein 
3738360efbdSAlfred Perlstein 	if (taddr) {
37409d4f778SIan Dowse 		free(taddr->buf);
37509d4f778SIan Dowse 		free(taddr);
37609d4f778SIan Dowse 		taddr = NULL;
3778360efbdSAlfred Perlstein 	}
3788360efbdSAlfred Perlstein 	if (((nconf = rpcbind_get_conf(transp->xp_netid)) == NULL) ||
3798360efbdSAlfred Perlstein 	    ((taddr = uaddr2taddr(nconf, *uaddrp)) == NULL)) {
3808360efbdSAlfred Perlstein 		(void) memset((char *)&nbuf, 0, sizeof (struct netbuf));
3818360efbdSAlfred Perlstein 		return (void *)&nbuf;
3828360efbdSAlfred Perlstein 	}
3838360efbdSAlfred Perlstein 	return (void *)taddr;
3848360efbdSAlfred Perlstein }
3858360efbdSAlfred Perlstein 
3868360efbdSAlfred Perlstein /*
3878360efbdSAlfred Perlstein  * Convert taddr to uaddr. Should be used only by
3888360efbdSAlfred Perlstein  * local servers/clients. (kernel level stuff only)
3898360efbdSAlfred Perlstein  */
3908360efbdSAlfred Perlstein /* ARGSUSED */
3918360efbdSAlfred Perlstein void *
3928360efbdSAlfred Perlstein rpcbproc_taddr2uaddr_com(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
3938360efbdSAlfred Perlstein 			 rpcvers_t rpcbversnum)
3948360efbdSAlfred Perlstein {
3958360efbdSAlfred Perlstein 	struct netbuf *taddr = (struct netbuf *)arg;
3968360efbdSAlfred Perlstein 	static char *uaddr;
3978360efbdSAlfred Perlstein 	struct netconfig *nconf;
3988360efbdSAlfred Perlstein 
3998360efbdSAlfred Perlstein #ifdef CHEW_FDS
4008360efbdSAlfred Perlstein 	int fd;
4018360efbdSAlfred Perlstein 
4028360efbdSAlfred Perlstein 	if ((fd = open("/dev/null", O_RDONLY)) == -1) {
4038360efbdSAlfred Perlstein 		uaddr = (char *)strerror(errno);
4048360efbdSAlfred Perlstein 		return (&uaddr);
4058360efbdSAlfred Perlstein 	}
4068360efbdSAlfred Perlstein #endif /* CHEW_FDS */
40709d4f778SIan Dowse 	if (uaddr != NULL && uaddr != nullstring) {
40809d4f778SIan Dowse 		free(uaddr);
40909d4f778SIan Dowse 		uaddr = NULL;
41009d4f778SIan Dowse 	}
4118360efbdSAlfred Perlstein 	if (((nconf = rpcbind_get_conf(transp->xp_netid)) == NULL) ||
4128360efbdSAlfred Perlstein 		((uaddr = taddr2uaddr(nconf, taddr)) == NULL)) {
4138360efbdSAlfred Perlstein 		uaddr = nullstring;
4148360efbdSAlfred Perlstein 	}
4158360efbdSAlfred Perlstein 	return (void *)&uaddr;
4168360efbdSAlfred Perlstein }
4178360efbdSAlfred Perlstein 
4188360efbdSAlfred Perlstein 
4198360efbdSAlfred Perlstein static bool_t
4208360efbdSAlfred Perlstein xdr_encap_parms(XDR *xdrs, struct encap_parms *epp)
4218360efbdSAlfred Perlstein {
4228360efbdSAlfred Perlstein 	return (xdr_bytes(xdrs, &(epp->args), (u_int *) &(epp->arglen), ~0));
4238360efbdSAlfred Perlstein }
4248360efbdSAlfred Perlstein 
4258360efbdSAlfred Perlstein /*
4268360efbdSAlfred Perlstein  * XDR remote call arguments.  It ignores the address part.
4278360efbdSAlfred Perlstein  * written for XDR_DECODE direction only
4288360efbdSAlfred Perlstein  */
4298360efbdSAlfred Perlstein static bool_t
4308360efbdSAlfred Perlstein xdr_rmtcall_args(XDR *xdrs, struct r_rmtcall_args *cap)
4318360efbdSAlfred Perlstein {
4328360efbdSAlfred Perlstein 	/* does not get the address or the arguments */
4338360efbdSAlfred Perlstein 	if (xdr_u_int32_t(xdrs, &(cap->rmt_prog)) &&
4348360efbdSAlfred Perlstein 	    xdr_u_int32_t(xdrs, &(cap->rmt_vers)) &&
4358360efbdSAlfred Perlstein 	    xdr_u_int32_t(xdrs, &(cap->rmt_proc))) {
4368360efbdSAlfred Perlstein 		return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
4378360efbdSAlfred Perlstein 	}
4388360efbdSAlfred Perlstein 	return (FALSE);
4398360efbdSAlfred Perlstein }
4408360efbdSAlfred Perlstein 
4418360efbdSAlfred Perlstein /*
4428360efbdSAlfred Perlstein  * XDR remote call results along with the address.  Ignore
4438360efbdSAlfred Perlstein  * program number, version  number and proc number.
4448360efbdSAlfred Perlstein  * Written for XDR_ENCODE direction only.
4458360efbdSAlfred Perlstein  */
4468360efbdSAlfred Perlstein static bool_t
4478360efbdSAlfred Perlstein xdr_rmtcall_result(XDR *xdrs, struct r_rmtcall_args *cap)
4488360efbdSAlfred Perlstein {
4498360efbdSAlfred Perlstein 	bool_t result;
4508360efbdSAlfred Perlstein 
4518360efbdSAlfred Perlstein #ifdef PORTMAP
4528360efbdSAlfred Perlstein 	if (cap->rmt_localvers == PMAPVERS) {
4538360efbdSAlfred Perlstein 		int h1, h2, h3, h4, p1, p2;
4548360efbdSAlfred Perlstein 		u_long port;
4558360efbdSAlfred Perlstein 
4568360efbdSAlfred Perlstein 		/* interpret the universal address for TCP/IP */
4578360efbdSAlfred Perlstein 		if (sscanf(cap->rmt_uaddr, "%d.%d.%d.%d.%d.%d",
4588360efbdSAlfred Perlstein 			&h1, &h2, &h3, &h4, &p1, &p2) != 6)
4598360efbdSAlfred Perlstein 			return (FALSE);
4608360efbdSAlfred Perlstein 		port = ((p1 & 0xff) << 8) + (p2 & 0xff);
4618360efbdSAlfred Perlstein 		result = xdr_u_long(xdrs, &port);
4628360efbdSAlfred Perlstein 	} else
4638360efbdSAlfred Perlstein #endif
4648360efbdSAlfred Perlstein 		if ((cap->rmt_localvers == RPCBVERS) ||
4658360efbdSAlfred Perlstein 		    (cap->rmt_localvers == RPCBVERS4)) {
4668360efbdSAlfred Perlstein 		result = xdr_wrapstring(xdrs, &(cap->rmt_uaddr));
4678360efbdSAlfred Perlstein 	} else {
4688360efbdSAlfred Perlstein 		return (FALSE);
4698360efbdSAlfred Perlstein 	}
4708360efbdSAlfred Perlstein 	if (result == TRUE)
4718360efbdSAlfred Perlstein 		return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
4728360efbdSAlfred Perlstein 	return (FALSE);
4738360efbdSAlfred Perlstein }
4748360efbdSAlfred Perlstein 
4758360efbdSAlfred Perlstein /*
4768360efbdSAlfred Perlstein  * only worries about the struct encap_parms part of struct r_rmtcall_args.
4778360efbdSAlfred Perlstein  * The arglen must already be set!!
4788360efbdSAlfred Perlstein  */
4798360efbdSAlfred Perlstein static bool_t
4808360efbdSAlfred Perlstein xdr_opaque_parms(XDR *xdrs, struct r_rmtcall_args *cap)
4818360efbdSAlfred Perlstein {
4828360efbdSAlfred Perlstein 	return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
4838360efbdSAlfred Perlstein }
4848360efbdSAlfred Perlstein 
4858360efbdSAlfred Perlstein static struct rmtcallfd_list *rmthead;
4868360efbdSAlfred Perlstein static struct rmtcallfd_list *rmttail;
4878360efbdSAlfred Perlstein 
4888360efbdSAlfred Perlstein int
4898360efbdSAlfred Perlstein create_rmtcall_fd(struct netconfig *nconf)
4908360efbdSAlfred Perlstein {
4918360efbdSAlfred Perlstein 	int fd;
4928360efbdSAlfred Perlstein 	struct rmtcallfd_list *rmt;
4938360efbdSAlfred Perlstein 	SVCXPRT *xprt;
4948360efbdSAlfred Perlstein 
4958360efbdSAlfred Perlstein 	if ((fd = __rpc_nconf2fd(nconf)) == -1) {
4968360efbdSAlfred Perlstein 		if (debugging)
4978360efbdSAlfred Perlstein 			fprintf(stderr,
4988360efbdSAlfred Perlstein 	"create_rmtcall_fd: couldn't open \"%s\" (errno %d)\n",
4998360efbdSAlfred Perlstein 			nconf->nc_device, errno);
5008360efbdSAlfred Perlstein 		return (-1);
5018360efbdSAlfred Perlstein 	}
5028360efbdSAlfred Perlstein 	xprt = svc_tli_create(fd, 0, (struct t_bind *) 0, 0, 0);
5038360efbdSAlfred Perlstein 	if (xprt == NULL) {
5048360efbdSAlfred Perlstein 		if (debugging)
5058360efbdSAlfred Perlstein 			fprintf(stderr,
5068360efbdSAlfred Perlstein 				"create_rmtcall_fd: svc_tli_create failed\n");
5078360efbdSAlfred Perlstein 		return (-1);
5088360efbdSAlfred Perlstein 	}
5098360efbdSAlfred Perlstein 	rmt = (struct rmtcallfd_list *)malloc((u_int)
5108360efbdSAlfred Perlstein 		sizeof (struct rmtcallfd_list));
5118360efbdSAlfred Perlstein 	if (rmt == NULL) {
5128360efbdSAlfred Perlstein 		syslog(LOG_ERR, "create_rmtcall_fd: no memory!");
5138360efbdSAlfred Perlstein 		return (-1);
5148360efbdSAlfred Perlstein 	}
5158360efbdSAlfred Perlstein 	rmt->xprt = xprt;
5168360efbdSAlfred Perlstein 	rmt->netid = strdup(nconf->nc_netid);
5178360efbdSAlfred Perlstein 	xprt->xp_netid = rmt->netid;
5188360efbdSAlfred Perlstein 	rmt->fd = fd;
5198360efbdSAlfred Perlstein 	rmt->next = NULL;
5208360efbdSAlfred Perlstein 	if (rmthead == NULL) {
5218360efbdSAlfred Perlstein 		rmthead = rmt;
5228360efbdSAlfred Perlstein 		rmttail = rmt;
5238360efbdSAlfred Perlstein 	} else {
5248360efbdSAlfred Perlstein 		rmttail->next = rmt;
5258360efbdSAlfred Perlstein 		rmttail = rmt;
5268360efbdSAlfred Perlstein 	}
5278360efbdSAlfred Perlstein 	/* XXX not threadsafe */
5288360efbdSAlfred Perlstein 	if (fd > svc_maxfd)
5298360efbdSAlfred Perlstein 		svc_maxfd = fd;
5308360efbdSAlfred Perlstein 	FD_SET(fd, &svc_fdset);
5318360efbdSAlfred Perlstein 	return (fd);
5328360efbdSAlfred Perlstein }
5338360efbdSAlfred Perlstein 
5348360efbdSAlfred Perlstein static int
5358360efbdSAlfred Perlstein find_rmtcallfd_by_netid(char *netid)
5368360efbdSAlfred Perlstein {
5378360efbdSAlfred Perlstein 	struct rmtcallfd_list *rmt;
5388360efbdSAlfred Perlstein 
5398360efbdSAlfred Perlstein 	for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
5408360efbdSAlfred Perlstein 		if (strcmp(netid, rmt->netid) == 0) {
5418360efbdSAlfred Perlstein 			return (rmt->fd);
5428360efbdSAlfred Perlstein 		}
5438360efbdSAlfred Perlstein 	}
5448360efbdSAlfred Perlstein 	return (-1);
5458360efbdSAlfred Perlstein }
5468360efbdSAlfred Perlstein 
5478360efbdSAlfred Perlstein static SVCXPRT *
5488360efbdSAlfred Perlstein find_rmtcallxprt_by_fd(int fd)
5498360efbdSAlfred Perlstein {
5508360efbdSAlfred Perlstein 	struct rmtcallfd_list *rmt;
5518360efbdSAlfred Perlstein 
5528360efbdSAlfred Perlstein 	for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
5538360efbdSAlfred Perlstein 		if (fd == rmt->fd) {
5548360efbdSAlfred Perlstein 			return (rmt->xprt);
5558360efbdSAlfred Perlstein 		}
5568360efbdSAlfred Perlstein 	}
5578360efbdSAlfred Perlstein 	return (NULL);
5588360efbdSAlfred Perlstein }
5598360efbdSAlfred Perlstein 
5608360efbdSAlfred Perlstein 
5618360efbdSAlfred Perlstein /*
5628360efbdSAlfred Perlstein  * Call a remote procedure service.  This procedure is very quiet when things
5638360efbdSAlfred Perlstein  * go wrong.  The proc is written to support broadcast rpc.  In the broadcast
5648360efbdSAlfred Perlstein  * case, a machine should shut-up instead of complain, lest the requestor be
5658360efbdSAlfred Perlstein  * overrun with complaints at the expense of not hearing a valid reply.
5668360efbdSAlfred Perlstein  * When receiving a request and verifying that the service exists, we
5678360efbdSAlfred Perlstein  *
5688360efbdSAlfred Perlstein  *	receive the request
5698360efbdSAlfred Perlstein  *
5708360efbdSAlfred Perlstein  *	open a new TLI endpoint on the same transport on which we received
5718360efbdSAlfred Perlstein  *	the original request
5728360efbdSAlfred Perlstein  *
5738360efbdSAlfred Perlstein  *	remember the original request's XID (which requires knowing the format
5748360efbdSAlfred Perlstein  *	of the svc_dg_data structure)
5758360efbdSAlfred Perlstein  *
5768360efbdSAlfred Perlstein  *	forward the request, with a new XID, to the requested service,
5778360efbdSAlfred Perlstein  *	remembering the XID used to send this request (for later use in
5788360efbdSAlfred Perlstein  *	reassociating the answer with the original request), the requestor's
5798360efbdSAlfred Perlstein  *	address, the file descriptor on which the forwarded request is
5808360efbdSAlfred Perlstein  *	made and the service's address.
5818360efbdSAlfred Perlstein  *
5828360efbdSAlfred Perlstein  *	mark the file descriptor on which we anticipate receiving a reply from
5838360efbdSAlfred Perlstein  *	the service and one to select for in our private svc_run procedure
5848360efbdSAlfred Perlstein  *
5858360efbdSAlfred Perlstein  * At some time in the future, a reply will be received from the service to
5868360efbdSAlfred Perlstein  * which we forwarded the request.  At that time, we detect that the socket
5878360efbdSAlfred Perlstein  * used was for forwarding (by looking through the finfo structures to see
5888360efbdSAlfred Perlstein  * whether the fd corresponds to one of those) and call handle_reply() to
5898360efbdSAlfred Perlstein  *
5908360efbdSAlfred Perlstein  *	receive the reply
5918360efbdSAlfred Perlstein  *
5928360efbdSAlfred Perlstein  *	bundle the reply, along with the service's universal address
5938360efbdSAlfred Perlstein  *
5948360efbdSAlfred Perlstein  *	create a SVCXPRT structure and use a version of svc_sendreply
5958360efbdSAlfred Perlstein  *	that allows us to specify the reply XID and destination, send the reply
5968360efbdSAlfred Perlstein  *	to the original requestor.
5978360efbdSAlfred Perlstein  */
5988360efbdSAlfred Perlstein 
5998360efbdSAlfred Perlstein void
6008360efbdSAlfred Perlstein rpcbproc_callit_com(struct svc_req *rqstp, SVCXPRT *transp,
6018360efbdSAlfred Perlstein 		    rpcproc_t reply_type, rpcvers_t versnum)
6028360efbdSAlfred Perlstein {
6038360efbdSAlfred Perlstein 	register rpcblist_ptr rbl;
6048360efbdSAlfred Perlstein 	struct netconfig *nconf;
6058360efbdSAlfred Perlstein 	struct netbuf *caller;
6068360efbdSAlfred Perlstein 	struct r_rmtcall_args a;
6078360efbdSAlfred Perlstein 	char *buf_alloc = NULL, *outbufp;
6088360efbdSAlfred Perlstein 	char *outbuf_alloc = NULL;
6098360efbdSAlfred Perlstein 	char buf[RPC_BUF_MAX], outbuf[RPC_BUF_MAX];
6108360efbdSAlfred Perlstein 	struct netbuf *na = (struct netbuf *) NULL;
6118360efbdSAlfred Perlstein 	struct rpc_msg call_msg;
6128360efbdSAlfred Perlstein 	int outlen;
6138360efbdSAlfred Perlstein 	u_int sendsz;
6148360efbdSAlfred Perlstein 	XDR outxdr;
6158360efbdSAlfred Perlstein 	AUTH *auth;
6168360efbdSAlfred Perlstein 	int fd = -1;
61709d4f778SIan Dowse 	char *uaddr, *m_uaddr = NULL, *local_uaddr = NULL;
6188360efbdSAlfred Perlstein 	u_int32_t *xidp;
6198360efbdSAlfred Perlstein 	struct __rpc_sockinfo si;
6208360efbdSAlfred Perlstein 	struct sockaddr *localsa;
6218360efbdSAlfred Perlstein 	struct netbuf tbuf;
6228360efbdSAlfred Perlstein 
6238360efbdSAlfred Perlstein 	if (!__rpc_fd2sockinfo(transp->xp_fd, &si)) {
6248360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
6258360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
6268360efbdSAlfred Perlstein 		return;
6278360efbdSAlfred Perlstein 	}
6288360efbdSAlfred Perlstein 	if (si.si_socktype != SOCK_DGRAM)
6298360efbdSAlfred Perlstein 		return;	/* Only datagram type accepted */
6308360efbdSAlfred Perlstein 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, UDPMSGSIZE);
6318360efbdSAlfred Perlstein 	if (sendsz == 0) {	/* data transfer not supported */
6328360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
6338360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
6348360efbdSAlfred Perlstein 		return;
6358360efbdSAlfred Perlstein 	}
6368360efbdSAlfred Perlstein 	/*
6378360efbdSAlfred Perlstein 	 * Should be multiple of 4 for XDR.
6388360efbdSAlfred Perlstein 	 */
6398360efbdSAlfred Perlstein 	sendsz = ((sendsz + 3) / 4) * 4;
6408360efbdSAlfred Perlstein 	if (sendsz > RPC_BUF_MAX) {
6418360efbdSAlfred Perlstein #ifdef	notyet
6428360efbdSAlfred Perlstein 		buf_alloc = alloca(sendsz);		/* not in IDR2? */
6438360efbdSAlfred Perlstein #else
6448360efbdSAlfred Perlstein 		buf_alloc = malloc(sendsz);
6458360efbdSAlfred Perlstein #endif	/* notyet */
6468360efbdSAlfred Perlstein 		if (buf_alloc == NULL) {
6478360efbdSAlfred Perlstein 			if (debugging)
6488360efbdSAlfred Perlstein 				fprintf(stderr,
6498360efbdSAlfred Perlstein 					"rpcbproc_callit_com:  No Memory!\n");
6508360efbdSAlfred Perlstein 			if (reply_type == RPCBPROC_INDIRECT)
6518360efbdSAlfred Perlstein 				svcerr_systemerr(transp);
6528360efbdSAlfred Perlstein 			return;
6538360efbdSAlfred Perlstein 		}
6548360efbdSAlfred Perlstein 		a.rmt_args.args = buf_alloc;
6558360efbdSAlfred Perlstein 	} else {
6568360efbdSAlfred Perlstein 		a.rmt_args.args = buf;
6578360efbdSAlfred Perlstein 	}
6588360efbdSAlfred Perlstein 
6598360efbdSAlfred Perlstein 	call_msg.rm_xid = 0;	/* For error checking purposes */
6608360efbdSAlfred Perlstein 	if (!svc_getargs(transp, (xdrproc_t) xdr_rmtcall_args, (char *) &a)) {
6618360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
6628360efbdSAlfred Perlstein 			svcerr_decode(transp);
6638360efbdSAlfred Perlstein 		if (debugging)
6648360efbdSAlfred Perlstein 			fprintf(stderr,
6658360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  svc_getargs failed\n");
6668360efbdSAlfred Perlstein 		goto error;
6678360efbdSAlfred Perlstein 	}
6688360efbdSAlfred Perlstein 
6698360efbdSAlfred Perlstein 	if (!check_callit(transp, &a, versnum)) {
6708360efbdSAlfred Perlstein 		svcerr_weakauth(transp);
6718360efbdSAlfred Perlstein 		goto error;
6728360efbdSAlfred Perlstein 	}
6738360efbdSAlfred Perlstein 
6748360efbdSAlfred Perlstein 	caller = svc_getrpccaller(transp);
6758360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
6768360efbdSAlfred Perlstein 	if (debugging) {
6778360efbdSAlfred Perlstein 		uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid), caller);
6788360efbdSAlfred Perlstein 		fprintf(stderr, "%s %s req for (%lu, %lu, %lu, %s) from %s : ",
6798360efbdSAlfred Perlstein 			versnum == PMAPVERS ? "pmap_rmtcall" :
6808360efbdSAlfred Perlstein 			versnum == RPCBVERS ? "rpcb_rmtcall" :
6818360efbdSAlfred Perlstein 			versnum == RPCBVERS4 ? "rpcb_indirect" : "unknown",
6828360efbdSAlfred Perlstein 			reply_type == RPCBPROC_INDIRECT ? "indirect" : "callit",
6838360efbdSAlfred Perlstein 			(unsigned long)a.rmt_prog, (unsigned long)a.rmt_vers,
6848360efbdSAlfred Perlstein 			(unsigned long)a.rmt_proc, transp->xp_netid,
6858360efbdSAlfred Perlstein 			uaddr ? uaddr : "unknown");
6868360efbdSAlfred Perlstein 		if (uaddr)
68709d4f778SIan Dowse 			free(uaddr);
6888360efbdSAlfred Perlstein 	}
6898360efbdSAlfred Perlstein #endif
6908360efbdSAlfred Perlstein 
6918360efbdSAlfred Perlstein 	rbl = find_service(a.rmt_prog, a.rmt_vers, transp->xp_netid);
6928360efbdSAlfred Perlstein 
6938360efbdSAlfred Perlstein 	rpcbs_rmtcall(versnum - 2, reply_type, a.rmt_prog, a.rmt_vers,
6948360efbdSAlfred Perlstein 			a.rmt_proc, transp->xp_netid, rbl);
6958360efbdSAlfred Perlstein 
6968360efbdSAlfred Perlstein 	if (rbl == (rpcblist_ptr)NULL) {
6978360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
6988360efbdSAlfred Perlstein 		if (debugging)
6998360efbdSAlfred Perlstein 			fprintf(stderr, "not found\n");
7008360efbdSAlfred Perlstein #endif
7018360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
7028360efbdSAlfred Perlstein 			svcerr_noprog(transp);
7038360efbdSAlfred Perlstein 		goto error;
7048360efbdSAlfred Perlstein 	}
7058360efbdSAlfred Perlstein 	if (rbl->rpcb_map.r_vers != a.rmt_vers) {
7068360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT) {
7078360efbdSAlfred Perlstein 			rpcvers_t vers_low, vers_high;
7088360efbdSAlfred Perlstein 
7098360efbdSAlfred Perlstein 			find_versions(a.rmt_prog, transp->xp_netid,
7108360efbdSAlfred Perlstein 				&vers_low, &vers_high);
7118360efbdSAlfred Perlstein 			svcerr_progvers(transp, vers_low, vers_high);
7128360efbdSAlfred Perlstein 		}
7138360efbdSAlfred Perlstein 		goto error;
7148360efbdSAlfred Perlstein 	}
7158360efbdSAlfred Perlstein 
7168360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
7178360efbdSAlfred Perlstein 	if (debugging)
7188360efbdSAlfred Perlstein 		fprintf(stderr, "found at uaddr %s\n", rbl->rpcb_map.r_addr);
7198360efbdSAlfred Perlstein #endif
7208360efbdSAlfred Perlstein 	/*
7218360efbdSAlfred Perlstein 	 *	Check whether this entry is valid and a server is present
7228360efbdSAlfred Perlstein 	 *	Mergeaddr() returns NULL if no such entry is present, and
7238360efbdSAlfred Perlstein 	 *	returns "" if the entry was present but the server is not
7248360efbdSAlfred Perlstein 	 *	present (i.e., it crashed).
7258360efbdSAlfred Perlstein 	 */
7268360efbdSAlfred Perlstein 	if (reply_type == RPCBPROC_INDIRECT) {
7278360efbdSAlfred Perlstein 		uaddr = mergeaddr(transp, transp->xp_netid,
7288360efbdSAlfred Perlstein 			rbl->rpcb_map.r_addr, NULL);
72909d4f778SIan Dowse 		if (uaddr == NULL || uaddr[0] == '\0') {
7308360efbdSAlfred Perlstein 			svcerr_noprog(transp);
73109d4f778SIan Dowse 			if (uaddr != NULL)
73209d4f778SIan Dowse 				free(uaddr);
7338360efbdSAlfred Perlstein 			goto error;
7348360efbdSAlfred Perlstein 		}
73509d4f778SIan Dowse 		free(uaddr);
7368360efbdSAlfred Perlstein 	}
7378360efbdSAlfred Perlstein 	nconf = rpcbind_get_conf(transp->xp_netid);
7388360efbdSAlfred Perlstein 	if (nconf == (struct netconfig *)NULL) {
7398360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
7408360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
7418360efbdSAlfred Perlstein 		if (debugging)
7428360efbdSAlfred Perlstein 			fprintf(stderr,
7438360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  rpcbind_get_conf failed\n");
7448360efbdSAlfred Perlstein 		goto error;
7458360efbdSAlfred Perlstein 	}
7468360efbdSAlfred Perlstein 	localsa = local_sa(((struct sockaddr *)caller->buf)->sa_family);
7478360efbdSAlfred Perlstein 	if (localsa == NULL) {
7488360efbdSAlfred Perlstein 		if (debugging)
7498360efbdSAlfred Perlstein 			fprintf(stderr,
7508360efbdSAlfred Perlstein 			"rpcbproc_callit_com: no local address\n");
7518360efbdSAlfred Perlstein 		goto error;
7528360efbdSAlfred Perlstein 	}
7538360efbdSAlfred Perlstein 	tbuf.len = tbuf.maxlen = localsa->sa_len;
7548360efbdSAlfred Perlstein 	tbuf.buf = localsa;
7558360efbdSAlfred Perlstein 	local_uaddr =
7568360efbdSAlfred Perlstein 	    addrmerge(&tbuf, rbl->rpcb_map.r_addr, NULL, nconf->nc_netid);
7578360efbdSAlfred Perlstein 	m_uaddr = addrmerge(caller, rbl->rpcb_map.r_addr, NULL,
7588360efbdSAlfred Perlstein 			nconf->nc_netid);
7598360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
7608360efbdSAlfred Perlstein 	if (debugging)
7618360efbdSAlfred Perlstein 		fprintf(stderr, "merged uaddr %s\n", m_uaddr);
7628360efbdSAlfred Perlstein #endif
7638360efbdSAlfred Perlstein 	if ((fd = find_rmtcallfd_by_netid(nconf->nc_netid)) == -1) {
7648360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
7658360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
7668360efbdSAlfred Perlstein 		goto error;
7678360efbdSAlfred Perlstein 	}
7688360efbdSAlfred Perlstein 	xidp = __rpcb_get_dg_xidp(transp);
76998f36a35SIan Dowse 	switch (forward_register(*xidp, caller, fd, m_uaddr, reply_type,
77098f36a35SIan Dowse 	    versnum, &call_msg.rm_xid)) {
77198f36a35SIan Dowse 	case 1:
77298f36a35SIan Dowse 		/* Success; forward_register() will free m_uaddr for us. */
77398f36a35SIan Dowse 		m_uaddr = NULL;
77498f36a35SIan Dowse 		break;
77598f36a35SIan Dowse 	case 0:
7768360efbdSAlfred Perlstein 		/*
7778360efbdSAlfred Perlstein 		 * A duplicate request for the slow server.  Let's not
7788360efbdSAlfred Perlstein 		 * beat on it any more.
7798360efbdSAlfred Perlstein 		 */
7808360efbdSAlfred Perlstein 		if (debugging)
7818360efbdSAlfred Perlstein 			fprintf(stderr,
7828360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  duplicate request\n");
7838360efbdSAlfred Perlstein 		goto error;
78498f36a35SIan Dowse 	case -1:
7858360efbdSAlfred Perlstein 		/*  forward_register failed.  Perhaps no memory. */
7868360efbdSAlfred Perlstein 		if (debugging)
7878360efbdSAlfred Perlstein 			fprintf(stderr,
7888360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  forward_register failed\n");
7898360efbdSAlfred Perlstein 		goto error;
7908360efbdSAlfred Perlstein 	}
7918360efbdSAlfred Perlstein 
7928360efbdSAlfred Perlstein #ifdef DEBUG_RMTCALL
7938360efbdSAlfred Perlstein 	if (debugging)
7948360efbdSAlfred Perlstein 		fprintf(stderr,
7958360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  original XID %x, new XID %x\n",
7968360efbdSAlfred Perlstein 				*xidp, call_msg.rm_xid);
7978360efbdSAlfred Perlstein #endif
7988360efbdSAlfred Perlstein 	call_msg.rm_direction = CALL;
7998360efbdSAlfred Perlstein 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
8008360efbdSAlfred Perlstein 	call_msg.rm_call.cb_prog = a.rmt_prog;
8018360efbdSAlfred Perlstein 	call_msg.rm_call.cb_vers = a.rmt_vers;
8028360efbdSAlfred Perlstein 	if (sendsz > RPC_BUF_MAX) {
8038360efbdSAlfred Perlstein #ifdef	notyet
8048360efbdSAlfred Perlstein 		outbuf_alloc = alloca(sendsz);	/* not in IDR2? */
8058360efbdSAlfred Perlstein #else
8068360efbdSAlfred Perlstein 		outbuf_alloc = malloc(sendsz);
8078360efbdSAlfred Perlstein #endif	/* notyet */
8088360efbdSAlfred Perlstein 		if (outbuf_alloc == NULL) {
8098360efbdSAlfred Perlstein 			if (reply_type == RPCBPROC_INDIRECT)
8108360efbdSAlfred Perlstein 				svcerr_systemerr(transp);
8118360efbdSAlfred Perlstein 			if (debugging)
8128360efbdSAlfred Perlstein 				fprintf(stderr,
8138360efbdSAlfred Perlstein 				"rpcbproc_callit_com:  No memory!\n");
8148360efbdSAlfred Perlstein 			goto error;
8158360efbdSAlfred Perlstein 		}
8168360efbdSAlfred Perlstein 		xdrmem_create(&outxdr, outbuf_alloc, sendsz, XDR_ENCODE);
8178360efbdSAlfred Perlstein 	} else {
8188360efbdSAlfred Perlstein 		xdrmem_create(&outxdr, outbuf, sendsz, XDR_ENCODE);
8198360efbdSAlfred Perlstein 	}
8208360efbdSAlfred Perlstein 	if (!xdr_callhdr(&outxdr, &call_msg)) {
8218360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8228360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8238360efbdSAlfred Perlstein 		if (debugging)
8248360efbdSAlfred Perlstein 			fprintf(stderr,
8258360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  xdr_callhdr failed\n");
8268360efbdSAlfred Perlstein 		goto error;
8278360efbdSAlfred Perlstein 	}
8288360efbdSAlfred Perlstein 	if (!xdr_u_int32_t(&outxdr, &(a.rmt_proc))) {
8298360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8308360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8318360efbdSAlfred Perlstein 		if (debugging)
8328360efbdSAlfred Perlstein 			fprintf(stderr,
8338360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  xdr_u_long failed\n");
8348360efbdSAlfred Perlstein 		goto error;
8358360efbdSAlfred Perlstein 	}
8368360efbdSAlfred Perlstein 
8378360efbdSAlfred Perlstein 	if (rqstp->rq_cred.oa_flavor == AUTH_NULL) {
8388360efbdSAlfred Perlstein 		auth = authnone_create();
8398360efbdSAlfred Perlstein 	} else if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
8408360efbdSAlfred Perlstein 		struct authunix_parms *au;
8418360efbdSAlfred Perlstein 
8428360efbdSAlfred Perlstein 		au = (struct authunix_parms *)rqstp->rq_clntcred;
8438360efbdSAlfred Perlstein 		auth = authunix_create(au->aup_machname,
8448360efbdSAlfred Perlstein 				au->aup_uid, au->aup_gid,
8458360efbdSAlfred Perlstein 				au->aup_len, au->aup_gids);
8468360efbdSAlfred Perlstein 		if (auth == NULL) /* fall back */
8478360efbdSAlfred Perlstein 			auth = authnone_create();
8488360efbdSAlfred Perlstein 	} else {
8498360efbdSAlfred Perlstein 		/* we do not support any other authentication scheme */
8508360efbdSAlfred Perlstein 		if (debugging)
8518360efbdSAlfred Perlstein 			fprintf(stderr,
8528360efbdSAlfred Perlstein "rpcbproc_callit_com:  oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
8538360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8548360efbdSAlfred Perlstein 			svcerr_weakauth(transp); /* XXX too strong.. */
8558360efbdSAlfred Perlstein 		goto error;
8568360efbdSAlfred Perlstein 	}
8578360efbdSAlfred Perlstein 	if (auth == NULL) {
8588360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8598360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8608360efbdSAlfred Perlstein 		if (debugging)
8618360efbdSAlfred Perlstein 			fprintf(stderr,
8628360efbdSAlfred Perlstein 		"rpcbproc_callit_com:  authwhatever_create returned NULL\n");
8638360efbdSAlfred Perlstein 		goto error;
8648360efbdSAlfred Perlstein 	}
8658360efbdSAlfred Perlstein 	if (!AUTH_MARSHALL(auth, &outxdr)) {
8668360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8678360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8688360efbdSAlfred Perlstein 		AUTH_DESTROY(auth);
8698360efbdSAlfred Perlstein 		if (debugging)
8708360efbdSAlfred Perlstein 			fprintf(stderr,
8718360efbdSAlfred Perlstein 		"rpcbproc_callit_com:  AUTH_MARSHALL failed\n");
8728360efbdSAlfred Perlstein 		goto error;
8738360efbdSAlfred Perlstein 	}
8748360efbdSAlfred Perlstein 	AUTH_DESTROY(auth);
8758360efbdSAlfred Perlstein 	if (!xdr_opaque_parms(&outxdr, &a)) {
8768360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8778360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8788360efbdSAlfred Perlstein 		if (debugging)
8798360efbdSAlfred Perlstein 			fprintf(stderr,
8808360efbdSAlfred Perlstein 		"rpcbproc_callit_com:  xdr_opaque_parms failed\n");
8818360efbdSAlfred Perlstein 		goto error;
8828360efbdSAlfred Perlstein 	}
8838360efbdSAlfred Perlstein 	outlen = (int) XDR_GETPOS(&outxdr);
8848360efbdSAlfred Perlstein 	if (outbuf_alloc)
8858360efbdSAlfred Perlstein 		outbufp = outbuf_alloc;
8868360efbdSAlfred Perlstein 	else
8878360efbdSAlfred Perlstein 		outbufp = outbuf;
8888360efbdSAlfred Perlstein 
8898360efbdSAlfred Perlstein 	na = uaddr2taddr(nconf, local_uaddr);
8908360efbdSAlfred Perlstein 	if (!na) {
8918360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8928360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8938360efbdSAlfred Perlstein 		goto error;
8948360efbdSAlfred Perlstein 	}
8958360efbdSAlfred Perlstein 
8968360efbdSAlfred Perlstein 	if (sendto(fd, outbufp, outlen, 0, (struct sockaddr *)na->buf, na->len)
8978360efbdSAlfred Perlstein 	    != outlen) {
8988360efbdSAlfred Perlstein 		if (debugging)
8998360efbdSAlfred Perlstein 			fprintf(stderr,
9008360efbdSAlfred Perlstein 	"rpcbproc_callit_com:  sendto failed:  errno %d\n", errno);
9018360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
9028360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
9038360efbdSAlfred Perlstein 		goto error;
9048360efbdSAlfred Perlstein 	}
9058360efbdSAlfred Perlstein 	goto out;
9068360efbdSAlfred Perlstein 
9078360efbdSAlfred Perlstein error:
9088360efbdSAlfred Perlstein 	if (call_msg.rm_xid != 0)
9098360efbdSAlfred Perlstein 		(void) free_slot_by_xid(call_msg.rm_xid);
9108360efbdSAlfred Perlstein out:
9118360efbdSAlfred Perlstein 	if (local_uaddr)
9128360efbdSAlfred Perlstein 		free(local_uaddr);
9138360efbdSAlfred Perlstein 	if (buf_alloc)
91409d4f778SIan Dowse 		free(buf_alloc);
9158360efbdSAlfred Perlstein 	if (outbuf_alloc)
91609d4f778SIan Dowse 		free(outbuf_alloc);
9178360efbdSAlfred Perlstein 	if (na) {
9188360efbdSAlfred Perlstein 		free(na->buf);
9198360efbdSAlfred Perlstein 		free(na);
9208360efbdSAlfred Perlstein 	}
92109d4f778SIan Dowse 	if (m_uaddr != NULL)
92209d4f778SIan Dowse 		free(m_uaddr);
9238360efbdSAlfred Perlstein }
9248360efbdSAlfred Perlstein 
9258360efbdSAlfred Perlstein /*
9268360efbdSAlfred Perlstein  * Makes an entry into the FIFO for the given request.
92798f36a35SIan Dowse  * Returns 1 on success, 0 if this is a duplicate request, or -1 on error.
92898f36a35SIan Dowse  * *callxidp is set to the xid of the call.
9298360efbdSAlfred Perlstein  */
93098f36a35SIan Dowse static int
9318360efbdSAlfred Perlstein forward_register(u_int32_t caller_xid, struct netbuf *caller_addr,
9328360efbdSAlfred Perlstein 		 int forward_fd, char *uaddr, rpcproc_t reply_type,
93398f36a35SIan Dowse 		 rpcvers_t versnum, u_int32_t *callxidp)
9348360efbdSAlfred Perlstein {
9358360efbdSAlfred Perlstein 	int		i;
9368360efbdSAlfred Perlstein 	int		j = 0;
9378360efbdSAlfred Perlstein 	time_t		min_time, time_now;
9388360efbdSAlfred Perlstein 	static u_int32_t	lastxid;
9398360efbdSAlfred Perlstein 	int		entry = -1;
9408360efbdSAlfred Perlstein 
9418360efbdSAlfred Perlstein 	min_time = FINFO[0].time;
9428360efbdSAlfred Perlstein 	time_now = time((time_t *)0);
9438360efbdSAlfred Perlstein 	/* initialization */
9448360efbdSAlfred Perlstein 	if (lastxid == 0)
9458360efbdSAlfred Perlstein 		lastxid = time_now * NFORWARD;
9468360efbdSAlfred Perlstein 
9478360efbdSAlfred Perlstein 	/*
9488360efbdSAlfred Perlstein 	 * Check if it is an duplicate entry. Then,
9498360efbdSAlfred Perlstein 	 * try to find an empty slot.  If not available, then
9508360efbdSAlfred Perlstein 	 * use the slot with the earliest time.
9518360efbdSAlfred Perlstein 	 */
9528360efbdSAlfred Perlstein 	for (i = 0; i < NFORWARD; i++) {
9538360efbdSAlfred Perlstein 		if (FINFO[i].flag & FINFO_ACTIVE) {
9548360efbdSAlfred Perlstein 			if ((FINFO[i].caller_xid == caller_xid) &&
9558360efbdSAlfred Perlstein 			    (FINFO[i].reply_type == reply_type) &&
9568360efbdSAlfred Perlstein 			    (FINFO[i].versnum == versnum) &&
9578360efbdSAlfred Perlstein 			    (!netbufcmp(FINFO[i].caller_addr,
9588360efbdSAlfred Perlstein 					    caller_addr))) {
9598360efbdSAlfred Perlstein 				FINFO[i].time = time((time_t *)0);
9608360efbdSAlfred Perlstein 				return (0);	/* Duplicate entry */
9618360efbdSAlfred Perlstein 			} else {
9628360efbdSAlfred Perlstein 				/* Should we wait any longer */
9638360efbdSAlfred Perlstein 				if ((time_now - FINFO[i].time) > MAXTIME_OFF)
9648360efbdSAlfred Perlstein 					(void) free_slot_by_index(i);
9658360efbdSAlfred Perlstein 			}
9668360efbdSAlfred Perlstein 		}
9678360efbdSAlfred Perlstein 		if (entry == -1) {
9688360efbdSAlfred Perlstein 			if ((FINFO[i].flag & FINFO_ACTIVE) == 0) {
9698360efbdSAlfred Perlstein 				entry = i;
9708360efbdSAlfred Perlstein 			} else if (FINFO[i].time < min_time) {
9718360efbdSAlfred Perlstein 				j = i;
9728360efbdSAlfred Perlstein 				min_time = FINFO[i].time;
9738360efbdSAlfred Perlstein 			}
9748360efbdSAlfred Perlstein 		}
9758360efbdSAlfred Perlstein 	}
9768360efbdSAlfred Perlstein 	if (entry != -1) {
9778360efbdSAlfred Perlstein 		/* use this empty slot */
9788360efbdSAlfred Perlstein 		j = entry;
9798360efbdSAlfred Perlstein 	} else {
9808360efbdSAlfred Perlstein 		(void) free_slot_by_index(j);
9818360efbdSAlfred Perlstein 	}
9828360efbdSAlfred Perlstein 	if ((FINFO[j].caller_addr = netbufdup(caller_addr)) == NULL) {
9838360efbdSAlfred Perlstein 		return (-1);
9848360efbdSAlfred Perlstein 	}
9858360efbdSAlfred Perlstein 	rpcb_rmtcalls++;	/* no of pending calls */
9868360efbdSAlfred Perlstein 	FINFO[j].flag = FINFO_ACTIVE;
9878360efbdSAlfred Perlstein 	FINFO[j].reply_type = reply_type;
9888360efbdSAlfred Perlstein 	FINFO[j].versnum = versnum;
9898360efbdSAlfred Perlstein 	FINFO[j].time = time_now;
9908360efbdSAlfred Perlstein 	FINFO[j].caller_xid = caller_xid;
9918360efbdSAlfred Perlstein 	FINFO[j].forward_fd = forward_fd;
9928360efbdSAlfred Perlstein 	/*
9938360efbdSAlfred Perlstein 	 * Though uaddr is not allocated here, it will still be freed
9948360efbdSAlfred Perlstein 	 * from free_slot_*().
9958360efbdSAlfred Perlstein 	 */
9968360efbdSAlfred Perlstein 	FINFO[j].uaddr = uaddr;
9978360efbdSAlfred Perlstein 	lastxid = lastxid + NFORWARD;
99898f36a35SIan Dowse 	/* Don't allow a zero xid below. */
99998f36a35SIan Dowse 	if ((u_int32_t)(lastxid + NFORWARD) <= NFORWARD)
100098f36a35SIan Dowse 		lastxid = NFORWARD;
10018360efbdSAlfred Perlstein 	FINFO[j].forward_xid = lastxid + j;	/* encode slot */
100298f36a35SIan Dowse 	*callxidp = FINFO[j].forward_xid;	/* forward on this xid */
100398f36a35SIan Dowse 	return (1);
10048360efbdSAlfred Perlstein }
10058360efbdSAlfred Perlstein 
10068360efbdSAlfred Perlstein static struct finfo *
10078360efbdSAlfred Perlstein forward_find(u_int32_t reply_xid)
10088360efbdSAlfred Perlstein {
10098360efbdSAlfred Perlstein 	int		i;
10108360efbdSAlfred Perlstein 
101198f36a35SIan Dowse 	i = reply_xid % (u_int32_t)NFORWARD;
10128360efbdSAlfred Perlstein 	if ((FINFO[i].flag & FINFO_ACTIVE) &&
10138360efbdSAlfred Perlstein 	    (FINFO[i].forward_xid == reply_xid)) {
10148360efbdSAlfred Perlstein 		return (&FINFO[i]);
10158360efbdSAlfred Perlstein 	}
10168360efbdSAlfred Perlstein 	return (NULL);
10178360efbdSAlfred Perlstein }
10188360efbdSAlfred Perlstein 
10198360efbdSAlfred Perlstein static int
10208360efbdSAlfred Perlstein free_slot_by_xid(u_int32_t xid)
10218360efbdSAlfred Perlstein {
10228360efbdSAlfred Perlstein 	int entry;
10238360efbdSAlfred Perlstein 
102498f36a35SIan Dowse 	entry = xid % (u_int32_t)NFORWARD;
10258360efbdSAlfred Perlstein 	return (free_slot_by_index(entry));
10268360efbdSAlfred Perlstein }
10278360efbdSAlfred Perlstein 
10288360efbdSAlfred Perlstein static int
10298360efbdSAlfred Perlstein free_slot_by_index(int index)
10308360efbdSAlfred Perlstein {
10318360efbdSAlfred Perlstein 	struct finfo	*fi;
10328360efbdSAlfred Perlstein 
10338360efbdSAlfred Perlstein 	fi = &FINFO[index];
10348360efbdSAlfred Perlstein 	if (fi->flag & FINFO_ACTIVE) {
10358360efbdSAlfred Perlstein 		netbuffree(fi->caller_addr);
10368360efbdSAlfred Perlstein 		/* XXX may be too big, but can't access xprt array here */
10378360efbdSAlfred Perlstein 		if (fi->forward_fd >= svc_maxfd)
10388360efbdSAlfred Perlstein 			svc_maxfd--;
103909d4f778SIan Dowse 		free(fi->uaddr);
10408360efbdSAlfred Perlstein 		fi->flag &= ~FINFO_ACTIVE;
10418360efbdSAlfred Perlstein 		rpcb_rmtcalls--;
10428360efbdSAlfred Perlstein 		return (1);
10438360efbdSAlfred Perlstein 	}
10448360efbdSAlfred Perlstein 	return (0);
10458360efbdSAlfred Perlstein }
10468360efbdSAlfred Perlstein 
10478360efbdSAlfred Perlstein static int
10488360efbdSAlfred Perlstein netbufcmp(struct netbuf *n1, struct netbuf *n2)
10498360efbdSAlfred Perlstein {
10508360efbdSAlfred Perlstein 	return ((n1->len != n2->len) || memcmp(n1->buf, n2->buf, n1->len));
10518360efbdSAlfred Perlstein }
10528360efbdSAlfred Perlstein 
10538360efbdSAlfred Perlstein static struct netbuf *
10548360efbdSAlfred Perlstein netbufdup(struct netbuf *ap)
10558360efbdSAlfred Perlstein {
10568360efbdSAlfred Perlstein 	struct netbuf  *np;
10578360efbdSAlfred Perlstein 
105809d4f778SIan Dowse 	if ((np = malloc(sizeof(struct netbuf))) == NULL)
105909d4f778SIan Dowse 		return (NULL);
106009d4f778SIan Dowse 	if ((np->buf = malloc(ap->len)) == NULL) {
106109d4f778SIan Dowse 		free(np);
106209d4f778SIan Dowse 		return (NULL);
10638360efbdSAlfred Perlstein 	}
106409d4f778SIan Dowse 	np->maxlen = np->len = ap->len;
106509d4f778SIan Dowse 	memcpy(np->buf, ap->buf, ap->len);
10668360efbdSAlfred Perlstein 	return (np);
10678360efbdSAlfred Perlstein }
10688360efbdSAlfred Perlstein 
10698360efbdSAlfred Perlstein static void
10708360efbdSAlfred Perlstein netbuffree(struct netbuf *ap)
10718360efbdSAlfred Perlstein {
107209d4f778SIan Dowse 	free(ap);
107309d4f778SIan Dowse 	free(ap->buf);
10748360efbdSAlfred Perlstein }
10758360efbdSAlfred Perlstein 
10768360efbdSAlfred Perlstein 
10778360efbdSAlfred Perlstein #define	MASKVAL	(POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
10788360efbdSAlfred Perlstein 
10798360efbdSAlfred Perlstein void
10808360efbdSAlfred Perlstein my_svc_run()
10818360efbdSAlfred Perlstein {
10828360efbdSAlfred Perlstein 	size_t nfds;
10838360efbdSAlfred Perlstein 	struct pollfd pollfds[FD_SETSIZE];
10848360efbdSAlfred Perlstein 	int poll_ret, check_ret;
10858360efbdSAlfred Perlstein 	int n;
10868360efbdSAlfred Perlstein #ifdef SVC_RUN_DEBUG
10878360efbdSAlfred Perlstein 	int i;
10888360efbdSAlfred Perlstein #endif
10898360efbdSAlfred Perlstein 	register struct pollfd	*p;
10908360efbdSAlfred Perlstein 
10918360efbdSAlfred Perlstein 	for (;;) {
10928360efbdSAlfred Perlstein 		p = pollfds;
10938360efbdSAlfred Perlstein 		for (n = 0; n <= svc_maxfd; n++) {
10948360efbdSAlfred Perlstein 			if (FD_ISSET(n, &svc_fdset)) {
10958360efbdSAlfred Perlstein 				p->fd = n;
10968360efbdSAlfred Perlstein 				p->events = MASKVAL;
10978360efbdSAlfred Perlstein 				p++;
10988360efbdSAlfred Perlstein 			}
10998360efbdSAlfred Perlstein 		}
11008360efbdSAlfred Perlstein 		nfds = p - pollfds;
11018360efbdSAlfred Perlstein 		poll_ret = 0;
11028360efbdSAlfred Perlstein #ifdef SVC_RUN_DEBUG
11038360efbdSAlfred Perlstein 		if (debugging) {
11048360efbdSAlfred Perlstein 			fprintf(stderr, "polling for read on fd < ");
11058360efbdSAlfred Perlstein 			for (i = 0, p = pollfds; i < nfds; i++, p++)
11068360efbdSAlfred Perlstein 				if (p->events)
11078360efbdSAlfred Perlstein 					fprintf(stderr, "%d ", p->fd);
11088360efbdSAlfred Perlstein 			fprintf(stderr, ">\n");
11098360efbdSAlfred Perlstein 		}
11108360efbdSAlfred Perlstein #endif
11118360efbdSAlfred Perlstein 		switch (poll_ret = poll(pollfds, nfds, INFTIM)) {
11128360efbdSAlfred Perlstein 		case -1:
11138360efbdSAlfred Perlstein 			/*
11148360efbdSAlfred Perlstein 			 * We ignore all errors, continuing with the assumption
11158360efbdSAlfred Perlstein 			 * that it was set by the signal handlers (or any
11168360efbdSAlfred Perlstein 			 * other outside event) and not caused by poll().
11178360efbdSAlfred Perlstein 			 */
11188360efbdSAlfred Perlstein 		case 0:
11198360efbdSAlfred Perlstein 			continue;
11208360efbdSAlfred Perlstein 		default:
11218360efbdSAlfred Perlstein #ifdef SVC_RUN_DEBUG
11228360efbdSAlfred Perlstein 			if (debugging) {
11238360efbdSAlfred Perlstein 				fprintf(stderr, "poll returned read fds < ");
11248360efbdSAlfred Perlstein 				for (i = 0, p = pollfds; i < nfds; i++, p++)
11258360efbdSAlfred Perlstein 					if (p->revents)
11268360efbdSAlfred Perlstein 						fprintf(stderr, "%d ", p->fd);
11278360efbdSAlfred Perlstein 				fprintf(stderr, ">\n");
11288360efbdSAlfred Perlstein 			}
11298360efbdSAlfred Perlstein #endif
11308360efbdSAlfred Perlstein 			/*
11318360efbdSAlfred Perlstein 			 * If we found as many replies on callback fds
11328360efbdSAlfred Perlstein 			 * as the number of descriptors selectable which
11338360efbdSAlfred Perlstein 			 * poll() returned, there can be no more so we
11348360efbdSAlfred Perlstein 			 * don't call svc_getreq_poll.  Otherwise, there
11358360efbdSAlfred Perlstein 			 * must be another so we must call svc_getreq_poll.
11368360efbdSAlfred Perlstein 			 */
11378360efbdSAlfred Perlstein 			if ((check_ret = check_rmtcalls(pollfds, nfds)) ==
11388360efbdSAlfred Perlstein 			    poll_ret)
11398360efbdSAlfred Perlstein 				continue;
11408360efbdSAlfred Perlstein 			svc_getreq_poll(pollfds, poll_ret-check_ret);
11418360efbdSAlfred Perlstein 		}
11428360efbdSAlfred Perlstein #ifdef SVC_RUN_DEBUG
11438360efbdSAlfred Perlstein 		if (debugging) {
11448360efbdSAlfred Perlstein 			fprintf(stderr, "svc_maxfd now %u\n", svc_maxfd);
11458360efbdSAlfred Perlstein 		}
11468360efbdSAlfred Perlstein #endif
11478360efbdSAlfred Perlstein 	}
11488360efbdSAlfred Perlstein }
11498360efbdSAlfred Perlstein 
11508360efbdSAlfred Perlstein static int
11518360efbdSAlfred Perlstein check_rmtcalls(struct pollfd *pfds, int nfds)
11528360efbdSAlfred Perlstein {
11538360efbdSAlfred Perlstein 	int j, ncallbacks_found = 0, rmtcalls_pending;
11548360efbdSAlfred Perlstein 	SVCXPRT *xprt;
11558360efbdSAlfred Perlstein 
11568360efbdSAlfred Perlstein 	if (rpcb_rmtcalls == 0)
11578360efbdSAlfred Perlstein 		return (0);
11588360efbdSAlfred Perlstein 
11598360efbdSAlfred Perlstein 	rmtcalls_pending = rpcb_rmtcalls;
11608360efbdSAlfred Perlstein 	for (j = 0; j < nfds; j++) {
11618360efbdSAlfred Perlstein 		if ((xprt = find_rmtcallxprt_by_fd(pfds[j].fd)) != NULL) {
11628360efbdSAlfred Perlstein 			if (pfds[j].revents) {
11638360efbdSAlfred Perlstein 				ncallbacks_found++;
11648360efbdSAlfred Perlstein #ifdef DEBUG_RMTCALL
11658360efbdSAlfred Perlstein 			if (debugging)
11668360efbdSAlfred Perlstein 				fprintf(stderr,
11678360efbdSAlfred Perlstein "my_svc_run:  polled on forwarding fd %d, netid %s - calling handle_reply\n",
11688360efbdSAlfred Perlstein 		pfds[j].fd, xprt->xp_netid);
11698360efbdSAlfred Perlstein #endif
11708360efbdSAlfred Perlstein 				handle_reply(pfds[j].fd, xprt);
11718360efbdSAlfred Perlstein 				pfds[j].revents = 0;
11728360efbdSAlfred Perlstein 				if (ncallbacks_found >= rmtcalls_pending) {
11738360efbdSAlfred Perlstein 					break;
11748360efbdSAlfred Perlstein 				}
11758360efbdSAlfred Perlstein 			}
11768360efbdSAlfred Perlstein 		}
11778360efbdSAlfred Perlstein 	}
11788360efbdSAlfred Perlstein 	return (ncallbacks_found);
11798360efbdSAlfred Perlstein }
11808360efbdSAlfred Perlstein 
11818360efbdSAlfred Perlstein static void
11828360efbdSAlfred Perlstein xprt_set_caller(SVCXPRT *xprt, struct finfo *fi)
11838360efbdSAlfred Perlstein {
11848360efbdSAlfred Perlstein 	u_int32_t *xidp;
11858360efbdSAlfred Perlstein 
11868360efbdSAlfred Perlstein 	*(svc_getrpccaller(xprt)) = *(fi->caller_addr);
11878360efbdSAlfred Perlstein 	xidp = __rpcb_get_dg_xidp(xprt);
11888360efbdSAlfred Perlstein 	*xidp = fi->caller_xid;
11898360efbdSAlfred Perlstein }
11908360efbdSAlfred Perlstein 
11918360efbdSAlfred Perlstein /*
11928360efbdSAlfred Perlstein  * Call svcerr_systemerr() only if RPCBVERS4
11938360efbdSAlfred Perlstein  */
11948360efbdSAlfred Perlstein static void
11958360efbdSAlfred Perlstein send_svcsyserr(SVCXPRT *xprt, struct finfo *fi)
11968360efbdSAlfred Perlstein {
11978360efbdSAlfred Perlstein 	if (fi->reply_type == RPCBPROC_INDIRECT) {
11988360efbdSAlfred Perlstein 		xprt_set_caller(xprt, fi);
11998360efbdSAlfred Perlstein 		svcerr_systemerr(xprt);
12008360efbdSAlfred Perlstein 	}
12018360efbdSAlfred Perlstein 	return;
12028360efbdSAlfred Perlstein }
12038360efbdSAlfred Perlstein 
12048360efbdSAlfred Perlstein static void
12058360efbdSAlfred Perlstein handle_reply(int fd, SVCXPRT *xprt)
12068360efbdSAlfred Perlstein {
12078360efbdSAlfred Perlstein 	XDR		reply_xdrs;
12088360efbdSAlfred Perlstein 	struct rpc_msg	reply_msg;
12098360efbdSAlfred Perlstein 	struct rpc_err	reply_error;
12108360efbdSAlfred Perlstein 	char		*buffer;
12118360efbdSAlfred Perlstein 	struct finfo	*fi;
12128360efbdSAlfred Perlstein 	int		inlen, pos, len;
12138360efbdSAlfred Perlstein 	struct r_rmtcall_args a;
12148360efbdSAlfred Perlstein 	struct sockaddr_storage ss;
12158360efbdSAlfred Perlstein 	socklen_t fromlen;
12168360efbdSAlfred Perlstein #ifdef SVC_RUN_DEBUG
12178360efbdSAlfred Perlstein 	char *uaddr;
12188360efbdSAlfred Perlstein #endif
12198360efbdSAlfred Perlstein 
12208360efbdSAlfred Perlstein 	buffer = malloc(RPC_BUF_MAX);
12218360efbdSAlfred Perlstein 	if (buffer == NULL)
12228360efbdSAlfred Perlstein 		goto done;
12238360efbdSAlfred Perlstein 
12248360efbdSAlfred Perlstein 	do {
12258360efbdSAlfred Perlstein 		inlen = recvfrom(fd, buffer, RPC_BUF_MAX, 0,
12268360efbdSAlfred Perlstein 			    (struct sockaddr *)&ss, &fromlen);
12278360efbdSAlfred Perlstein 	} while (inlen < 0 && errno == EINTR);
12288360efbdSAlfred Perlstein 	if (inlen < 0) {
12298360efbdSAlfred Perlstein 		if (debugging)
12308360efbdSAlfred Perlstein 			fprintf(stderr,
12318360efbdSAlfred Perlstein 	"handle_reply:  recvfrom returned %d, errno %d\n", inlen, errno);
12328360efbdSAlfred Perlstein 		goto done;
12338360efbdSAlfred Perlstein 	}
12348360efbdSAlfred Perlstein 
12358360efbdSAlfred Perlstein 	reply_msg.acpted_rply.ar_verf = _null_auth;
12368360efbdSAlfred Perlstein 	reply_msg.acpted_rply.ar_results.where = 0;
12378360efbdSAlfred Perlstein 	reply_msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
12388360efbdSAlfred Perlstein 
12398360efbdSAlfred Perlstein 	xdrmem_create(&reply_xdrs, buffer, (u_int)inlen, XDR_DECODE);
12408360efbdSAlfred Perlstein 	if (!xdr_replymsg(&reply_xdrs, &reply_msg)) {
12418360efbdSAlfred Perlstein 		if (debugging)
12428360efbdSAlfred Perlstein 			(void) fprintf(stderr,
12438360efbdSAlfred Perlstein 				"handle_reply:  xdr_replymsg failed\n");
12448360efbdSAlfred Perlstein 		goto done;
12458360efbdSAlfred Perlstein 	}
12468360efbdSAlfred Perlstein 	fi = forward_find(reply_msg.rm_xid);
12478360efbdSAlfred Perlstein #ifdef	SVC_RUN_DEBUG
12488360efbdSAlfred Perlstein 	if (debugging) {
12498360efbdSAlfred Perlstein 		fprintf(stderr, "handle_reply:  reply xid: %d fi addr: %p\n",
12508360efbdSAlfred Perlstein 			reply_msg.rm_xid, fi);
12518360efbdSAlfred Perlstein 	}
12528360efbdSAlfred Perlstein #endif
12538360efbdSAlfred Perlstein 	if (fi == NULL) {
12548360efbdSAlfred Perlstein 		goto done;
12558360efbdSAlfred Perlstein 	}
12568360efbdSAlfred Perlstein 	_seterr_reply(&reply_msg, &reply_error);
12578360efbdSAlfred Perlstein 	if (reply_error.re_status != RPC_SUCCESS) {
12588360efbdSAlfred Perlstein 		if (debugging)
12598360efbdSAlfred Perlstein 			(void) fprintf(stderr, "handle_reply:  %s\n",
12608360efbdSAlfred Perlstein 				clnt_sperrno(reply_error.re_status));
12618360efbdSAlfred Perlstein 		send_svcsyserr(xprt, fi);
12628360efbdSAlfred Perlstein 		goto done;
12638360efbdSAlfred Perlstein 	}
12648360efbdSAlfred Perlstein 	pos = XDR_GETPOS(&reply_xdrs);
12658360efbdSAlfred Perlstein 	len = inlen - pos;
12668360efbdSAlfred Perlstein 	a.rmt_args.args = &buffer[pos];
12678360efbdSAlfred Perlstein 	a.rmt_args.arglen = len;
12688360efbdSAlfred Perlstein 	a.rmt_uaddr = fi->uaddr;
12698360efbdSAlfred Perlstein 	a.rmt_localvers = fi->versnum;
12708360efbdSAlfred Perlstein 
12718360efbdSAlfred Perlstein 	xprt_set_caller(xprt, fi);
12728360efbdSAlfred Perlstein #ifdef	SVC_RUN_DEBUG
12738360efbdSAlfred Perlstein 	uaddr =	taddr2uaddr(rpcbind_get_conf("udp"),
12748360efbdSAlfred Perlstein 				    svc_getrpccaller(xprt));
12758360efbdSAlfred Perlstein 	if (debugging) {
12768360efbdSAlfred Perlstein 		fprintf(stderr, "handle_reply:  forwarding address %s to %s\n",
12778360efbdSAlfred Perlstein 			a.rmt_uaddr, uaddr ? uaddr : "unknown");
12788360efbdSAlfred Perlstein 	}
12798360efbdSAlfred Perlstein 	if (uaddr)
128009d4f778SIan Dowse 		free(uaddr);
12818360efbdSAlfred Perlstein #endif
12828360efbdSAlfred Perlstein 	svc_sendreply(xprt, (xdrproc_t) xdr_rmtcall_result, (char *) &a);
12838360efbdSAlfred Perlstein done:
12848360efbdSAlfred Perlstein 	if (buffer)
12858360efbdSAlfred Perlstein 		free(buffer);
12868360efbdSAlfred Perlstein 
12878360efbdSAlfred Perlstein 	if (reply_msg.rm_xid == 0) {
12888360efbdSAlfred Perlstein #ifdef	SVC_RUN_DEBUG
12898360efbdSAlfred Perlstein 	if (debugging) {
12908360efbdSAlfred Perlstein 		fprintf(stderr, "handle_reply:  NULL xid on exit!\n");
12918360efbdSAlfred Perlstein 	}
12928360efbdSAlfred Perlstein #endif
12938360efbdSAlfred Perlstein 	} else
12948360efbdSAlfred Perlstein 		(void) free_slot_by_xid(reply_msg.rm_xid);
12958360efbdSAlfred Perlstein 	return;
12968360efbdSAlfred Perlstein }
12978360efbdSAlfred Perlstein 
12988360efbdSAlfred Perlstein static void
12998360efbdSAlfred Perlstein find_versions(rpcprog_t prog, char *netid, rpcvers_t *lowvp, rpcvers_t *highvp)
13008360efbdSAlfred Perlstein {
13018360efbdSAlfred Perlstein 	register rpcblist_ptr rbl;
13028360efbdSAlfred Perlstein 	int lowv = 0;
13038360efbdSAlfred Perlstein 	int highv = 0;
13048360efbdSAlfred Perlstein 
13058360efbdSAlfred Perlstein 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
13068360efbdSAlfred Perlstein 		if ((rbl->rpcb_map.r_prog != prog) ||
13078360efbdSAlfred Perlstein 		    ((rbl->rpcb_map.r_netid != NULL) &&
13088360efbdSAlfred Perlstein 			(strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
13098360efbdSAlfred Perlstein 			continue;
13108360efbdSAlfred Perlstein 		if (lowv == 0) {
13118360efbdSAlfred Perlstein 			highv = rbl->rpcb_map.r_vers;
13128360efbdSAlfred Perlstein 			lowv = highv;
13138360efbdSAlfred Perlstein 		} else if (rbl->rpcb_map.r_vers < lowv) {
13148360efbdSAlfred Perlstein 			lowv = rbl->rpcb_map.r_vers;
13158360efbdSAlfred Perlstein 		} else if (rbl->rpcb_map.r_vers > highv) {
13168360efbdSAlfred Perlstein 			highv = rbl->rpcb_map.r_vers;
13178360efbdSAlfred Perlstein 		}
13188360efbdSAlfred Perlstein 	}
13198360efbdSAlfred Perlstein 	*lowvp = lowv;
13208360efbdSAlfred Perlstein 	*highvp = highv;
13218360efbdSAlfred Perlstein 	return;
13228360efbdSAlfred Perlstein }
13238360efbdSAlfred Perlstein 
13248360efbdSAlfred Perlstein /*
13258360efbdSAlfred Perlstein  * returns the item with the given program, version number and netid.
13268360efbdSAlfred Perlstein  * If that version number is not found, it returns the item with that
13278360efbdSAlfred Perlstein  * program number, so that address is now returned to the caller. The
13288360efbdSAlfred Perlstein  * caller when makes a call to this program, version number, the call
13298360efbdSAlfred Perlstein  * will fail and it will return with PROGVERS_MISMATCH. The user can
13308360efbdSAlfred Perlstein  * then determine the highest and the lowest version number for this
13318360efbdSAlfred Perlstein  * program using clnt_geterr() and use those program version numbers.
13328360efbdSAlfred Perlstein  *
13338360efbdSAlfred Perlstein  * Returns the RPCBLIST for the given prog, vers and netid
13348360efbdSAlfred Perlstein  */
13358360efbdSAlfred Perlstein static rpcblist_ptr
13368360efbdSAlfred Perlstein find_service(rpcprog_t prog, rpcvers_t vers, char *netid)
13378360efbdSAlfred Perlstein {
13388360efbdSAlfred Perlstein 	register rpcblist_ptr hit = NULL;
13398360efbdSAlfred Perlstein 	register rpcblist_ptr rbl;
13408360efbdSAlfred Perlstein 
13418360efbdSAlfred Perlstein 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
13428360efbdSAlfred Perlstein 		if ((rbl->rpcb_map.r_prog != prog) ||
13438360efbdSAlfred Perlstein 		    ((rbl->rpcb_map.r_netid != NULL) &&
13448360efbdSAlfred Perlstein 			(strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
13458360efbdSAlfred Perlstein 			continue;
13468360efbdSAlfred Perlstein 		hit = rbl;
13478360efbdSAlfred Perlstein 		if (rbl->rpcb_map.r_vers == vers)
13488360efbdSAlfred Perlstein 			break;
13498360efbdSAlfred Perlstein 	}
13508360efbdSAlfred Perlstein 	return (hit);
13518360efbdSAlfred Perlstein }
13528360efbdSAlfred Perlstein 
13538360efbdSAlfred Perlstein /*
13548360efbdSAlfred Perlstein  * Copies the name associated with the uid of the caller and returns
13558360efbdSAlfred Perlstein  * a pointer to it.  Similar to getwd().
13568360efbdSAlfred Perlstein  */
13578360efbdSAlfred Perlstein static char *
13588360efbdSAlfred Perlstein getowner(SVCXPRT *transp, char *owner, size_t ownersize)
13598360efbdSAlfred Perlstein {
13604ed6d634SAlfred Perlstein 	uid_t uid;
13618360efbdSAlfred Perlstein 
13624ed6d634SAlfred Perlstein 	if (__rpc_get_local_uid(transp, &uid) < 0)
13638360efbdSAlfred Perlstein                 strlcpy(owner, "unknown", ownersize);
13644ed6d634SAlfred Perlstein 	else if (uid == 0)
13658360efbdSAlfred Perlstein 		strlcpy(owner, "superuser", ownersize);
13668360efbdSAlfred Perlstein 	else
13674ed6d634SAlfred Perlstein 		snprintf(owner, ownersize, "%d", uid);
13688360efbdSAlfred Perlstein 
13698360efbdSAlfred Perlstein 	return owner;
13708360efbdSAlfred Perlstein }
13718360efbdSAlfred Perlstein 
13728360efbdSAlfred Perlstein #ifdef PORTMAP
13738360efbdSAlfred Perlstein /*
13748360efbdSAlfred Perlstein  * Add this to the pmap list only if it is UDP or TCP.
13758360efbdSAlfred Perlstein  */
13768360efbdSAlfred Perlstein static int
13778360efbdSAlfred Perlstein add_pmaplist(RPCB *arg)
13788360efbdSAlfred Perlstein {
13798360efbdSAlfred Perlstein 	struct pmap pmap;
13808360efbdSAlfred Perlstein 	struct pmaplist *pml;
13818360efbdSAlfred Perlstein 	int h1, h2, h3, h4, p1, p2;
13828360efbdSAlfred Perlstein 
13838360efbdSAlfred Perlstein 	if (strcmp(arg->r_netid, udptrans) == 0) {
13848360efbdSAlfred Perlstein 		/* It is UDP! */
13858360efbdSAlfred Perlstein 		pmap.pm_prot = IPPROTO_UDP;
13868360efbdSAlfred Perlstein 	} else if (strcmp(arg->r_netid, tcptrans) == 0) {
13878360efbdSAlfred Perlstein 		/* It is TCP */
13888360efbdSAlfred Perlstein 		pmap.pm_prot = IPPROTO_TCP;
13898360efbdSAlfred Perlstein 	} else
13908360efbdSAlfred Perlstein 		/* Not a IP protocol */
13918360efbdSAlfred Perlstein 		return (0);
13928360efbdSAlfred Perlstein 
13938360efbdSAlfred Perlstein 	/* interpret the universal address for TCP/IP */
13948360efbdSAlfred Perlstein 	if (sscanf(arg->r_addr, "%d.%d.%d.%d.%d.%d",
13958360efbdSAlfred Perlstein 		&h1, &h2, &h3, &h4, &p1, &p2) != 6)
13968360efbdSAlfred Perlstein 		return (0);
13978360efbdSAlfred Perlstein 	pmap.pm_port = ((p1 & 0xff) << 8) + (p2 & 0xff);
13988360efbdSAlfred Perlstein 	pmap.pm_prog = arg->r_prog;
13998360efbdSAlfred Perlstein 	pmap.pm_vers = arg->r_vers;
14008360efbdSAlfred Perlstein 	/*
14018360efbdSAlfred Perlstein 	 * add to END of list
14028360efbdSAlfred Perlstein 	 */
14038360efbdSAlfred Perlstein 	pml = (struct pmaplist *) malloc((u_int)sizeof (struct pmaplist));
14048360efbdSAlfred Perlstein 	if (pml == NULL) {
14058360efbdSAlfred Perlstein 		(void) syslog(LOG_ERR, "rpcbind: no memory!\n");
14068360efbdSAlfred Perlstein 		return (1);
14078360efbdSAlfred Perlstein 	}
14088360efbdSAlfred Perlstein 	pml->pml_map = pmap;
14098360efbdSAlfred Perlstein 	pml->pml_next = NULL;
14108360efbdSAlfred Perlstein 	if (list_pml == NULL) {
14118360efbdSAlfred Perlstein 		list_pml = pml;
14128360efbdSAlfred Perlstein 	} else {
14138360efbdSAlfred Perlstein 		struct pmaplist *fnd;
14148360efbdSAlfred Perlstein 
14158360efbdSAlfred Perlstein 		/* Attach to the end of the list */
14168360efbdSAlfred Perlstein 		for (fnd = list_pml; fnd->pml_next; fnd = fnd->pml_next)
14178360efbdSAlfred Perlstein 			;
14188360efbdSAlfred Perlstein 		fnd->pml_next = pml;
14198360efbdSAlfred Perlstein 	}
14208360efbdSAlfred Perlstein 	return (0);
14218360efbdSAlfred Perlstein }
14228360efbdSAlfred Perlstein 
14238360efbdSAlfred Perlstein /*
14248360efbdSAlfred Perlstein  * Delete this from the pmap list only if it is UDP or TCP.
14258360efbdSAlfred Perlstein  */
14268360efbdSAlfred Perlstein static int
14278360efbdSAlfred Perlstein del_pmaplist(RPCB *arg)
14288360efbdSAlfred Perlstein {
14298360efbdSAlfred Perlstein 	struct pmaplist *pml;
14308360efbdSAlfred Perlstein 	struct pmaplist *prevpml, *fnd;
14318360efbdSAlfred Perlstein 	long prot;
14328360efbdSAlfred Perlstein 
14338360efbdSAlfred Perlstein 	if (strcmp(arg->r_netid, udptrans) == 0) {
14348360efbdSAlfred Perlstein 		/* It is UDP! */
14358360efbdSAlfred Perlstein 		prot = IPPROTO_UDP;
14368360efbdSAlfred Perlstein 	} else if (strcmp(arg->r_netid, tcptrans) == 0) {
14378360efbdSAlfred Perlstein 		/* It is TCP */
14388360efbdSAlfred Perlstein 		prot = IPPROTO_TCP;
14398360efbdSAlfred Perlstein 	} else if (arg->r_netid[0] == NULL) {
14408360efbdSAlfred Perlstein 		prot = 0;	/* Remove all occurrences */
14418360efbdSAlfred Perlstein 	} else {
14428360efbdSAlfred Perlstein 		/* Not a IP protocol */
14438360efbdSAlfred Perlstein 		return (0);
14448360efbdSAlfred Perlstein 	}
14458360efbdSAlfred Perlstein 	for (prevpml = NULL, pml = list_pml; pml; /* cstyle */) {
14468360efbdSAlfred Perlstein 		if ((pml->pml_map.pm_prog != arg->r_prog) ||
14478360efbdSAlfred Perlstein 			(pml->pml_map.pm_vers != arg->r_vers) ||
14488360efbdSAlfred Perlstein 			(prot && (pml->pml_map.pm_prot != prot))) {
14498360efbdSAlfred Perlstein 			/* both pml & prevpml move forwards */
14508360efbdSAlfred Perlstein 			prevpml = pml;
14518360efbdSAlfred Perlstein 			pml = pml->pml_next;
14528360efbdSAlfred Perlstein 			continue;
14538360efbdSAlfred Perlstein 		}
14548360efbdSAlfred Perlstein 		/* found it; pml moves forward, prevpml stays */
14558360efbdSAlfred Perlstein 		fnd = pml;
14568360efbdSAlfred Perlstein 		pml = pml->pml_next;
14578360efbdSAlfred Perlstein 		if (prevpml == NULL)
14588360efbdSAlfred Perlstein 			list_pml = pml;
14598360efbdSAlfred Perlstein 		else
14608360efbdSAlfred Perlstein 			prevpml->pml_next = pml;
146109d4f778SIan Dowse 		free(fnd);
14628360efbdSAlfred Perlstein 	}
14638360efbdSAlfred Perlstein 	return (0);
14648360efbdSAlfred Perlstein }
14658360efbdSAlfred Perlstein #endif /* PORTMAP */
1466