xref: /freebsd/usr.sbin/rpcbind/rpcb_svc_com.c (revision 8a7b61203704d362cfbc66c00619f51b24106b55)
1bcb53b16SMartin Blapp /*	$NetBSD: rpcb_svc_com.c,v 1.9 2002/11/08 00:16:39 fvdl Exp $	*/
28360efbdSAlfred Perlstein 
3a7c51fa1SHiroki Sato /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
6a7c51fa1SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
7a7c51fa1SHiroki Sato  * All rights reserved.
88360efbdSAlfred Perlstein  *
9a7c51fa1SHiroki Sato  * Redistribution and use in source and binary forms, with or without
10a7c51fa1SHiroki Sato  * modification, are permitted provided that the following conditions are met:
11a7c51fa1SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
12a7c51fa1SHiroki Sato  *   this list of conditions and the following disclaimer.
13a7c51fa1SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
14a7c51fa1SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
15a7c51fa1SHiroki Sato  *   and/or other materials provided with the distribution.
16a7c51fa1SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17a7c51fa1SHiroki Sato  *   contributors may be used to endorse or promote products derived
18a7c51fa1SHiroki Sato  *   from this software without specific prior written permission.
198360efbdSAlfred Perlstein  *
20a7c51fa1SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21a7c51fa1SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a7c51fa1SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23a7c51fa1SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24a7c51fa1SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25a7c51fa1SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26a7c51fa1SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27a7c51fa1SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28a7c51fa1SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29a7c51fa1SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30a7c51fa1SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
318360efbdSAlfred Perlstein  */
328360efbdSAlfred Perlstein /*
338360efbdSAlfred Perlstein  * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
348360efbdSAlfred Perlstein  */
358360efbdSAlfred Perlstein 
368360efbdSAlfred Perlstein /*
378360efbdSAlfred Perlstein  * rpcb_svc_com.c
388360efbdSAlfred Perlstein  * The commom server procedure for the rpcbind.
398360efbdSAlfred Perlstein  */
408360efbdSAlfred Perlstein 
418360efbdSAlfred Perlstein #include <sys/types.h>
428360efbdSAlfred Perlstein #include <sys/stat.h>
438360efbdSAlfred Perlstein #include <sys/param.h>
448360efbdSAlfred Perlstein #include <sys/poll.h>
458360efbdSAlfred Perlstein #include <sys/socket.h>
468360efbdSAlfred Perlstein #include <rpc/rpc.h>
478360efbdSAlfred Perlstein #include <rpc/rpcb_prot.h>
488360efbdSAlfred Perlstein #include <rpc/svc_dg.h>
49066c492aSXin LI #include <assert.h>
508360efbdSAlfred Perlstein #include <netconfig.h>
518360efbdSAlfred Perlstein #include <errno.h>
528360efbdSAlfred Perlstein #include <syslog.h>
538360efbdSAlfred Perlstein #include <stdio.h>
54b545252aSConrad Meyer #include <string.h>
55b545252aSConrad Meyer #include <stdlib.h>
56b545252aSConrad Meyer #include <unistd.h>
578360efbdSAlfred Perlstein #ifdef PORTMAP
588360efbdSAlfred Perlstein #include <netinet/in.h>
596448ec89SXin LI #include <rpc/rpc_com.h>
608360efbdSAlfred Perlstein #include <rpc/pmap_prot.h>
618360efbdSAlfred Perlstein #endif /* PORTMAP */
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 
95784bddbcSKevin Lo static bool_t xdr_encap_parms(XDR *, struct encap_parms *);
96784bddbcSKevin Lo static bool_t xdr_rmtcall_args(XDR *, struct r_rmtcall_args *);
97784bddbcSKevin Lo static bool_t xdr_rmtcall_result(XDR *, struct r_rmtcall_args *);
98784bddbcSKevin Lo static bool_t xdr_opaque_parms(XDR *, struct r_rmtcall_args *);
99784bddbcSKevin Lo static int find_rmtcallfd_by_netid(char *);
100784bddbcSKevin Lo static SVCXPRT *find_rmtcallxprt_by_fd(int);
101784bddbcSKevin Lo static int forward_register(u_int32_t, struct netbuf *, int, char *,
102784bddbcSKevin Lo     rpcproc_t, rpcvers_t, u_int32_t *);
103784bddbcSKevin Lo static struct finfo *forward_find(u_int32_t);
104784bddbcSKevin Lo static int free_slot_by_xid(u_int32_t);
105784bddbcSKevin Lo static int free_slot_by_index(int);
106784bddbcSKevin Lo static int netbufcmp(struct netbuf *, struct netbuf *);
107784bddbcSKevin Lo static struct netbuf *netbufdup(struct netbuf *);
108784bddbcSKevin Lo static void netbuffree(struct netbuf *);
109784bddbcSKevin Lo static int check_rmtcalls(struct pollfd *, int);
110784bddbcSKevin Lo static void xprt_set_caller(SVCXPRT *, struct finfo *);
111784bddbcSKevin Lo static void send_svcsyserr(SVCXPRT *, struct finfo *);
112784bddbcSKevin Lo static void handle_reply(int, SVCXPRT *);
113784bddbcSKevin Lo static void find_versions(rpcprog_t, char *, rpcvers_t *, rpcvers_t *);
114784bddbcSKevin Lo static rpcblist_ptr find_service(rpcprog_t, rpcvers_t, char *);
115784bddbcSKevin Lo static char *getowner(SVCXPRT *, char *, size_t);
116784bddbcSKevin Lo static int add_pmaplist(RPCB *);
117784bddbcSKevin Lo static int del_pmaplist(RPCB *);
1188360efbdSAlfred Perlstein 
1198360efbdSAlfred Perlstein /*
1208360efbdSAlfred Perlstein  * Set a mapping of program, version, netid
1218360efbdSAlfred Perlstein  */
1228360efbdSAlfred Perlstein /* ARGSUSED */
1238360efbdSAlfred Perlstein void *
rpcbproc_set_com(void * arg,struct svc_req * rqstp __unused,SVCXPRT * transp,rpcvers_t rpcbversnum)1244180788fSAlfred Perlstein rpcbproc_set_com(void *arg, struct svc_req *rqstp __unused, 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
map_set(RPCB * regp,char * owner)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 	 */
1732244ec01SJuli Mallett 	rbl = malloc(sizeof (RPCBLIST));
1742244ec01SJuli Mallett 	if (rbl == NULL)
1758360efbdSAlfred Perlstein 		return (FALSE);
1768360efbdSAlfred Perlstein 	a = &(rbl->rpcb_map);
1778360efbdSAlfred Perlstein 	a->r_prog = reg.r_prog;
1788360efbdSAlfred Perlstein 	a->r_vers = reg.r_vers;
1798360efbdSAlfred Perlstein 	a->r_netid = strdup(reg.r_netid);
1808360efbdSAlfred Perlstein 	a->r_addr = strdup(reg.r_addr);
1818360efbdSAlfred Perlstein 	a->r_owner = strdup(owner);
1828360efbdSAlfred Perlstein 	if (!a->r_addr || !a->r_netid || !a->r_owner) {
18309d4f778SIan Dowse 		free(a->r_netid);
18409d4f778SIan Dowse 		free(a->r_addr);
18509d4f778SIan Dowse 		free(a->r_owner);
18609d4f778SIan Dowse 		free(rbl);
1878360efbdSAlfred Perlstein 		return (FALSE);
1888360efbdSAlfred Perlstein 	}
1898360efbdSAlfred Perlstein 	rbl->rpcb_next = (rpcblist_ptr)NULL;
1908360efbdSAlfred Perlstein 	if (list_rbl == NULL) {
1918360efbdSAlfred Perlstein 		list_rbl = rbl;
1928360efbdSAlfred Perlstein 	} else {
1938360efbdSAlfred Perlstein 		for (fnd = list_rbl; fnd->rpcb_next;
1948360efbdSAlfred Perlstein 			fnd = fnd->rpcb_next)
1958360efbdSAlfred Perlstein 			;
1968360efbdSAlfred Perlstein 		fnd->rpcb_next = rbl;
1978360efbdSAlfred Perlstein 	}
1988360efbdSAlfred Perlstein #ifdef PORTMAP
1998360efbdSAlfred Perlstein 	(void) add_pmaplist(regp);
2008360efbdSAlfred Perlstein #endif
2018360efbdSAlfred Perlstein 	return (TRUE);
2028360efbdSAlfred Perlstein }
2038360efbdSAlfred Perlstein 
2048360efbdSAlfred Perlstein /*
2058360efbdSAlfred Perlstein  * Unset a mapping of program, version, netid
2068360efbdSAlfred Perlstein  */
2078360efbdSAlfred Perlstein /* ARGSUSED */
2088360efbdSAlfred Perlstein void *
rpcbproc_unset_com(void * arg,struct svc_req * rqstp __unused,SVCXPRT * transp,rpcvers_t rpcbversnum)2094180788fSAlfred Perlstein rpcbproc_unset_com(void *arg, struct svc_req *rqstp __unused, SVCXPRT *transp,
2108360efbdSAlfred Perlstein 		   rpcvers_t rpcbversnum)
2118360efbdSAlfred Perlstein {
2128360efbdSAlfred Perlstein 	RPCB *regp = (RPCB *)arg;
2138360efbdSAlfred Perlstein 	static bool_t ans;
2148360efbdSAlfred Perlstein 	char owner[64];
2158360efbdSAlfred Perlstein 
2168360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
2178360efbdSAlfred Perlstein 	if (debugging)
2188360efbdSAlfred Perlstein 		fprintf(stderr, "RPCB_UNSET request for (%lu, %lu, %s) : ",
2198360efbdSAlfred Perlstein 		    (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
2208360efbdSAlfred Perlstein 		    regp->r_netid);
2218360efbdSAlfred Perlstein #endif
2228360efbdSAlfred Perlstein 	ans = map_unset(regp, getowner(transp, owner, sizeof owner));
2238360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
2248360efbdSAlfred Perlstein 	if (debugging)
2258360efbdSAlfred Perlstein 		fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
2268360efbdSAlfred Perlstein #endif
2278360efbdSAlfred Perlstein 	/* XXX: should have used some defined constant here */
2288360efbdSAlfred Perlstein 	rpcbs_unset(rpcbversnum - 2, ans);
2298360efbdSAlfred Perlstein 	return (void *)&ans;
2308360efbdSAlfred Perlstein }
2318360efbdSAlfred Perlstein 
2328360efbdSAlfred Perlstein bool_t
map_unset(RPCB * regp,char * owner)2338360efbdSAlfred Perlstein map_unset(RPCB *regp, char *owner)
2348360efbdSAlfred Perlstein {
2358360efbdSAlfred Perlstein 	int ans = 0;
2368360efbdSAlfred Perlstein 	rpcblist_ptr rbl, prev, tmp;
2378360efbdSAlfred Perlstein 
2388360efbdSAlfred Perlstein 	if (owner == NULL)
2398360efbdSAlfred Perlstein 		return (0);
2408360efbdSAlfred Perlstein 
2418360efbdSAlfred Perlstein 	for (prev = NULL, rbl = list_rbl; rbl; /* cstyle */) {
2428360efbdSAlfred Perlstein 		if ((rbl->rpcb_map.r_prog != regp->r_prog) ||
2438360efbdSAlfred Perlstein 			(rbl->rpcb_map.r_vers != regp->r_vers) ||
2448360efbdSAlfred Perlstein 			(regp->r_netid[0] && strcasecmp(regp->r_netid,
2458360efbdSAlfred Perlstein 				rbl->rpcb_map.r_netid))) {
2468360efbdSAlfred Perlstein 			/* both rbl & prev move forwards */
2478360efbdSAlfred Perlstein 			prev = rbl;
2488360efbdSAlfred Perlstein 			rbl = rbl->rpcb_next;
2498360efbdSAlfred Perlstein 			continue;
2508360efbdSAlfred Perlstein 		}
2518360efbdSAlfred Perlstein 		/*
2528360efbdSAlfred Perlstein 		 * Check whether appropriate uid. Unset only
2538360efbdSAlfred Perlstein 		 * if superuser or the owner itself.
2548360efbdSAlfred Perlstein 		 */
2558360efbdSAlfred Perlstein 		if (strcmp(owner, "superuser") &&
2568360efbdSAlfred Perlstein 			strcmp(rbl->rpcb_map.r_owner, owner))
2578360efbdSAlfred Perlstein 			return (0);
2588360efbdSAlfred Perlstein 		/* found it; rbl moves forward, prev stays */
2598360efbdSAlfred Perlstein 		ans = 1;
2608360efbdSAlfred Perlstein 		tmp = rbl;
2618360efbdSAlfred Perlstein 		rbl = rbl->rpcb_next;
2628360efbdSAlfred Perlstein 		if (prev == NULL)
2638360efbdSAlfred Perlstein 			list_rbl = rbl;
2648360efbdSAlfred Perlstein 		else
2658360efbdSAlfred Perlstein 			prev->rpcb_next = rbl;
26609d4f778SIan Dowse 		free(tmp->rpcb_map.r_addr);
26709d4f778SIan Dowse 		free(tmp->rpcb_map.r_netid);
26809d4f778SIan Dowse 		free(tmp->rpcb_map.r_owner);
26909d4f778SIan Dowse 		free(tmp);
2708360efbdSAlfred Perlstein 	}
2718360efbdSAlfred Perlstein #ifdef PORTMAP
2728360efbdSAlfred Perlstein 	if (ans)
2738360efbdSAlfred Perlstein 		(void) del_pmaplist(regp);
2748360efbdSAlfred Perlstein #endif
2758360efbdSAlfred Perlstein 	/*
2768360efbdSAlfred Perlstein 	 * We return 1 either when the entry was not there or it
2778360efbdSAlfred Perlstein 	 * was able to unset it.  It can come to this point only if
2788360efbdSAlfred Perlstein 	 * atleast one of the conditions is true.
2798360efbdSAlfred Perlstein 	 */
2808360efbdSAlfred Perlstein 	return (1);
2818360efbdSAlfred Perlstein }
2828360efbdSAlfred Perlstein 
2838360efbdSAlfred Perlstein void
delete_prog(unsigned int prog)2844180788fSAlfred Perlstein delete_prog(unsigned int prog)
2858360efbdSAlfred Perlstein {
2868360efbdSAlfred Perlstein 	RPCB reg;
2878360efbdSAlfred Perlstein 	register rpcblist_ptr rbl;
2888360efbdSAlfred Perlstein 
2898360efbdSAlfred Perlstein 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
2908360efbdSAlfred Perlstein 		if ((rbl->rpcb_map.r_prog != prog))
2918360efbdSAlfred Perlstein 			continue;
2928360efbdSAlfred Perlstein 		if (is_bound(rbl->rpcb_map.r_netid, rbl->rpcb_map.r_addr))
2938360efbdSAlfred Perlstein 			continue;
2948360efbdSAlfred Perlstein 		reg.r_prog = rbl->rpcb_map.r_prog;
2958360efbdSAlfred Perlstein 		reg.r_vers = rbl->rpcb_map.r_vers;
2968360efbdSAlfred Perlstein 		reg.r_netid = strdup(rbl->rpcb_map.r_netid);
2978360efbdSAlfred Perlstein 		(void) map_unset(&reg, "superuser");
2988360efbdSAlfred Perlstein 		free(reg.r_netid);
2998360efbdSAlfred Perlstein 	}
3008360efbdSAlfred Perlstein }
3018360efbdSAlfred Perlstein 
3028360efbdSAlfred Perlstein void *
rpcbproc_getaddr_com(RPCB * regp,struct svc_req * rqstp __unused,SVCXPRT * transp,rpcvers_t rpcbversnum,rpcvers_t verstype)3034180788fSAlfred Perlstein rpcbproc_getaddr_com(RPCB *regp, struct svc_req *rqstp __unused,
3044180788fSAlfred Perlstein 		     SVCXPRT *transp, rpcvers_t rpcbversnum, rpcvers_t verstype)
3058360efbdSAlfred Perlstein {
3068360efbdSAlfred Perlstein 	static char *uaddr;
3078360efbdSAlfred Perlstein 	char *saddr = NULL;
3088360efbdSAlfred Perlstein 	rpcblist_ptr fnd;
3098360efbdSAlfred Perlstein 
31009d4f778SIan Dowse 	if (uaddr != NULL && uaddr != nullstring) {
31109d4f778SIan Dowse 		free(uaddr);
31209d4f778SIan Dowse 		uaddr = NULL;
31309d4f778SIan Dowse 	}
3148360efbdSAlfred Perlstein 	fnd = find_service(regp->r_prog, regp->r_vers, transp->xp_netid);
3158360efbdSAlfred Perlstein 	if (fnd && ((verstype == RPCB_ALLVERS) ||
3168360efbdSAlfred Perlstein 		    (regp->r_vers == fnd->rpcb_map.r_vers))) {
3178360efbdSAlfred Perlstein 		if (*(regp->r_addr) != '\0') {  /* may contain a hint about */
3188360efbdSAlfred Perlstein 			saddr = regp->r_addr;   /* the interface that we    */
3198360efbdSAlfred Perlstein 		}				/* should use */
3208360efbdSAlfred Perlstein 		if (!(uaddr = mergeaddr(transp, transp->xp_netid,
3218360efbdSAlfred Perlstein 				fnd->rpcb_map.r_addr, saddr))) {
3228360efbdSAlfred Perlstein 			/* Try whatever we have */
3238360efbdSAlfred Perlstein 			uaddr = strdup(fnd->rpcb_map.r_addr);
3248360efbdSAlfred Perlstein 		} else if (!uaddr[0]) {
3258360efbdSAlfred Perlstein 			/*
3268360efbdSAlfred Perlstein 			 * The server died.  Unset all versions of this prog.
3278360efbdSAlfred Perlstein 			 */
3288360efbdSAlfred Perlstein 			delete_prog(regp->r_prog);
3298360efbdSAlfred Perlstein 			uaddr = nullstring;
3308360efbdSAlfred Perlstein 		}
3318360efbdSAlfred Perlstein 	} else {
3328360efbdSAlfred Perlstein 		uaddr = nullstring;
3338360efbdSAlfred Perlstein 	}
3348360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
3358360efbdSAlfred Perlstein 	if (debugging)
3368360efbdSAlfred Perlstein 		fprintf(stderr, "getaddr: %s\n", uaddr);
3378360efbdSAlfred Perlstein #endif
3388360efbdSAlfred Perlstein 	/* XXX: should have used some defined constant here */
3398360efbdSAlfred Perlstein 	rpcbs_getaddr(rpcbversnum - 2, regp->r_prog, regp->r_vers,
3408360efbdSAlfred Perlstein 		transp->xp_netid, uaddr);
3418360efbdSAlfred Perlstein 	return (void *)&uaddr;
3428360efbdSAlfred Perlstein }
3438360efbdSAlfred Perlstein 
3448360efbdSAlfred Perlstein /* ARGSUSED */
3458360efbdSAlfred Perlstein void *
rpcbproc_gettime_com(void * arg __unused,struct svc_req * rqstp __unused,SVCXPRT * transp __unused,rpcvers_t rpcbversnum __unused)3464180788fSAlfred Perlstein rpcbproc_gettime_com(void *arg __unused, struct svc_req *rqstp __unused,
3474180788fSAlfred Perlstein 		     SVCXPRT *transp __unused, rpcvers_t rpcbversnum __unused)
3488360efbdSAlfred Perlstein {
3498360efbdSAlfred Perlstein 	static time_t curtime;
3508360efbdSAlfred Perlstein 
3518360efbdSAlfred Perlstein 	(void) time(&curtime);
3528360efbdSAlfred Perlstein 	return (void *)&curtime;
3538360efbdSAlfred Perlstein }
3548360efbdSAlfred Perlstein 
3558360efbdSAlfred Perlstein /*
3568360efbdSAlfred Perlstein  * Convert uaddr to taddr. Should be used only by
3578360efbdSAlfred Perlstein  * local servers/clients. (kernel level stuff only)
3588360efbdSAlfred Perlstein  */
3598360efbdSAlfred Perlstein /* ARGSUSED */
3608360efbdSAlfred Perlstein void *
rpcbproc_uaddr2taddr_com(void * arg,struct svc_req * rqstp __unused,SVCXPRT * transp,rpcvers_t rpcbversnum __unused)3614180788fSAlfred Perlstein rpcbproc_uaddr2taddr_com(void *arg, struct svc_req *rqstp __unused,
3624180788fSAlfred Perlstein 			 SVCXPRT *transp, rpcvers_t rpcbversnum __unused)
3638360efbdSAlfred Perlstein {
3648360efbdSAlfred Perlstein 	char **uaddrp = (char **)arg;
3658360efbdSAlfred Perlstein 	struct netconfig *nconf;
3668360efbdSAlfred Perlstein 	static struct netbuf nbuf;
3678360efbdSAlfred Perlstein 	static struct netbuf *taddr;
3688360efbdSAlfred Perlstein 
369*7ef6eb24SHiroki Sato 	netbuffree(taddr);
37009d4f778SIan Dowse 	taddr = NULL;
3718360efbdSAlfred Perlstein 	if (((nconf = rpcbind_get_conf(transp->xp_netid)) == NULL) ||
3728360efbdSAlfred Perlstein 	    ((taddr = uaddr2taddr(nconf, *uaddrp)) == NULL)) {
3738360efbdSAlfred Perlstein 		(void) memset((char *)&nbuf, 0, sizeof (struct netbuf));
3748360efbdSAlfred Perlstein 		return (void *)&nbuf;
3758360efbdSAlfred Perlstein 	}
3768360efbdSAlfred Perlstein 	return (void *)taddr;
3778360efbdSAlfred Perlstein }
3788360efbdSAlfred Perlstein 
3798360efbdSAlfred Perlstein /*
3808360efbdSAlfred Perlstein  * Convert taddr to uaddr. Should be used only by
3818360efbdSAlfred Perlstein  * local servers/clients. (kernel level stuff only)
3828360efbdSAlfred Perlstein  */
3838360efbdSAlfred Perlstein /* ARGSUSED */
3848360efbdSAlfred Perlstein void *
rpcbproc_taddr2uaddr_com(void * arg,struct svc_req * rqstp __unused,SVCXPRT * transp,rpcvers_t rpcbversnum __unused)3854180788fSAlfred Perlstein rpcbproc_taddr2uaddr_com(void *arg, struct svc_req *rqstp __unused,
3864180788fSAlfred Perlstein 			 SVCXPRT *transp, rpcvers_t rpcbversnum __unused)
3878360efbdSAlfred Perlstein {
3888360efbdSAlfred Perlstein 	struct netbuf *taddr = (struct netbuf *)arg;
3898360efbdSAlfred Perlstein 	static char *uaddr;
3908360efbdSAlfred Perlstein 	struct netconfig *nconf;
3918360efbdSAlfred Perlstein 
3928360efbdSAlfred Perlstein #ifdef CHEW_FDS
3938360efbdSAlfred Perlstein 	int fd;
3948360efbdSAlfred Perlstein 
3958360efbdSAlfred Perlstein 	if ((fd = open("/dev/null", O_RDONLY)) == -1) {
3968360efbdSAlfred Perlstein 		uaddr = (char *)strerror(errno);
3978360efbdSAlfred Perlstein 		return (&uaddr);
3988360efbdSAlfred Perlstein 	}
3998360efbdSAlfred Perlstein #endif /* CHEW_FDS */
40009d4f778SIan Dowse 	if (uaddr != NULL && uaddr != nullstring) {
40109d4f778SIan Dowse 		free(uaddr);
40209d4f778SIan Dowse 		uaddr = NULL;
40309d4f778SIan Dowse 	}
4048360efbdSAlfred Perlstein 	if (((nconf = rpcbind_get_conf(transp->xp_netid)) == NULL) ||
4058360efbdSAlfred Perlstein 		((uaddr = taddr2uaddr(nconf, taddr)) == NULL)) {
4068360efbdSAlfred Perlstein 		uaddr = nullstring;
4078360efbdSAlfred Perlstein 	}
4088360efbdSAlfred Perlstein 	return (void *)&uaddr;
4098360efbdSAlfred Perlstein }
4108360efbdSAlfred Perlstein 
4118360efbdSAlfred Perlstein 
4128360efbdSAlfred Perlstein static bool_t
xdr_encap_parms(XDR * xdrs,struct encap_parms * epp)4138360efbdSAlfred Perlstein xdr_encap_parms(XDR *xdrs, struct encap_parms *epp)
4148360efbdSAlfred Perlstein {
4156448ec89SXin LI 	return (xdr_bytes(xdrs, &(epp->args), (u_int *) &(epp->arglen),
4166448ec89SXin LI 	    RPC_MAXDATASIZE));
4178360efbdSAlfred Perlstein }
4188360efbdSAlfred Perlstein 
4198360efbdSAlfred Perlstein /*
4208360efbdSAlfred Perlstein  * XDR remote call arguments.  It ignores the address part.
4218360efbdSAlfred Perlstein  * written for XDR_DECODE direction only
4228360efbdSAlfred Perlstein  */
4238360efbdSAlfred Perlstein static bool_t
xdr_rmtcall_args(XDR * xdrs,struct r_rmtcall_args * cap)4248360efbdSAlfred Perlstein xdr_rmtcall_args(XDR *xdrs, struct r_rmtcall_args *cap)
4258360efbdSAlfred Perlstein {
4268360efbdSAlfred Perlstein 	/* does not get the address or the arguments */
427c7310cbcSPedro F. Giffuni 	if (xdr_rpcprog(xdrs, &(cap->rmt_prog)) &&
428c7310cbcSPedro F. Giffuni 	    xdr_rpcvers(xdrs, &(cap->rmt_vers)) &&
429c7310cbcSPedro F. Giffuni 	    xdr_rpcproc(xdrs, &(cap->rmt_proc))) {
4308360efbdSAlfred Perlstein 		return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
4318360efbdSAlfred Perlstein 	}
4328360efbdSAlfred Perlstein 	return (FALSE);
4338360efbdSAlfred Perlstein }
4348360efbdSAlfred Perlstein 
4358360efbdSAlfred Perlstein /*
4368360efbdSAlfred Perlstein  * XDR remote call results along with the address.  Ignore
4378360efbdSAlfred Perlstein  * program number, version  number and proc number.
4388360efbdSAlfred Perlstein  * Written for XDR_ENCODE direction only.
4398360efbdSAlfred Perlstein  */
4408360efbdSAlfred Perlstein static bool_t
xdr_rmtcall_result(XDR * xdrs,struct r_rmtcall_args * cap)4418360efbdSAlfred Perlstein xdr_rmtcall_result(XDR *xdrs, struct r_rmtcall_args *cap)
4428360efbdSAlfred Perlstein {
4438360efbdSAlfred Perlstein 	bool_t result;
4448360efbdSAlfred Perlstein 
4458360efbdSAlfred Perlstein #ifdef PORTMAP
4468360efbdSAlfred Perlstein 	if (cap->rmt_localvers == PMAPVERS) {
4478360efbdSAlfred Perlstein 		int h1, h2, h3, h4, p1, p2;
4488360efbdSAlfred Perlstein 		u_long port;
4498360efbdSAlfred Perlstein 
4508360efbdSAlfred Perlstein 		/* interpret the universal address for TCP/IP */
4518360efbdSAlfred Perlstein 		if (sscanf(cap->rmt_uaddr, "%d.%d.%d.%d.%d.%d",
4528360efbdSAlfred Perlstein 			&h1, &h2, &h3, &h4, &p1, &p2) != 6)
4538360efbdSAlfred Perlstein 			return (FALSE);
4548360efbdSAlfred Perlstein 		port = ((p1 & 0xff) << 8) + (p2 & 0xff);
4558360efbdSAlfred Perlstein 		result = xdr_u_long(xdrs, &port);
4568360efbdSAlfred Perlstein 	} else
4578360efbdSAlfred Perlstein #endif
4588360efbdSAlfred Perlstein 		if ((cap->rmt_localvers == RPCBVERS) ||
4598360efbdSAlfred Perlstein 		    (cap->rmt_localvers == RPCBVERS4)) {
4608360efbdSAlfred Perlstein 		result = xdr_wrapstring(xdrs, &(cap->rmt_uaddr));
4618360efbdSAlfred Perlstein 	} else {
4628360efbdSAlfred Perlstein 		return (FALSE);
4638360efbdSAlfred Perlstein 	}
4648360efbdSAlfred Perlstein 	if (result == TRUE)
4658360efbdSAlfred Perlstein 		return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
4668360efbdSAlfred Perlstein 	return (FALSE);
4678360efbdSAlfred Perlstein }
4688360efbdSAlfred Perlstein 
4698360efbdSAlfred Perlstein /*
4708360efbdSAlfred Perlstein  * only worries about the struct encap_parms part of struct r_rmtcall_args.
4718360efbdSAlfred Perlstein  * The arglen must already be set!!
4728360efbdSAlfred Perlstein  */
4738360efbdSAlfred Perlstein static bool_t
xdr_opaque_parms(XDR * xdrs,struct r_rmtcall_args * cap)4748360efbdSAlfred Perlstein xdr_opaque_parms(XDR *xdrs, struct r_rmtcall_args *cap)
4758360efbdSAlfred Perlstein {
4768360efbdSAlfred Perlstein 	return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
4778360efbdSAlfred Perlstein }
4788360efbdSAlfred Perlstein 
4798360efbdSAlfred Perlstein static struct rmtcallfd_list *rmthead;
4808360efbdSAlfred Perlstein static struct rmtcallfd_list *rmttail;
4818360efbdSAlfred Perlstein 
4828360efbdSAlfred Perlstein int
create_rmtcall_fd(struct netconfig * nconf)4838360efbdSAlfred Perlstein create_rmtcall_fd(struct netconfig *nconf)
4848360efbdSAlfred Perlstein {
4858360efbdSAlfred Perlstein 	int fd;
4868360efbdSAlfred Perlstein 	struct rmtcallfd_list *rmt;
4878360efbdSAlfred Perlstein 	SVCXPRT *xprt;
4888360efbdSAlfred Perlstein 
4898360efbdSAlfred Perlstein 	if ((fd = __rpc_nconf2fd(nconf)) == -1) {
4908360efbdSAlfred Perlstein 		if (debugging)
4918360efbdSAlfred Perlstein 			fprintf(stderr,
4928360efbdSAlfred Perlstein 	"create_rmtcall_fd: couldn't open \"%s\" (errno %d)\n",
4938360efbdSAlfred Perlstein 			nconf->nc_device, errno);
4948360efbdSAlfred Perlstein 		return (-1);
4958360efbdSAlfred Perlstein 	}
4968360efbdSAlfred Perlstein 	xprt = svc_tli_create(fd, 0, (struct t_bind *) 0, 0, 0);
4978360efbdSAlfred Perlstein 	if (xprt == NULL) {
4988360efbdSAlfred Perlstein 		if (debugging)
4998360efbdSAlfred Perlstein 			fprintf(stderr,
5008360efbdSAlfred Perlstein 				"create_rmtcall_fd: svc_tli_create failed\n");
5018360efbdSAlfred Perlstein 		return (-1);
5028360efbdSAlfred Perlstein 	}
5032244ec01SJuli Mallett 	rmt = malloc(sizeof (struct rmtcallfd_list));
5048360efbdSAlfred Perlstein 	if (rmt == NULL) {
5058360efbdSAlfred Perlstein 		syslog(LOG_ERR, "create_rmtcall_fd: no memory!");
5068360efbdSAlfred Perlstein 		return (-1);
5078360efbdSAlfred Perlstein 	}
5088360efbdSAlfred Perlstein 	rmt->xprt = xprt;
5098360efbdSAlfred Perlstein 	rmt->netid = strdup(nconf->nc_netid);
5108360efbdSAlfred Perlstein 	xprt->xp_netid = rmt->netid;
5118360efbdSAlfred Perlstein 	rmt->fd = fd;
5128360efbdSAlfred Perlstein 	rmt->next = NULL;
5138360efbdSAlfred Perlstein 	if (rmthead == NULL) {
5148360efbdSAlfred Perlstein 		rmthead = rmt;
5158360efbdSAlfred Perlstein 		rmttail = rmt;
5168360efbdSAlfred Perlstein 	} else {
5178360efbdSAlfred Perlstein 		rmttail->next = rmt;
5188360efbdSAlfred Perlstein 		rmttail = rmt;
5198360efbdSAlfred Perlstein 	}
5208360efbdSAlfred Perlstein 	/* XXX not threadsafe */
5218360efbdSAlfred Perlstein 	if (fd > svc_maxfd)
5228360efbdSAlfred Perlstein 		svc_maxfd = fd;
5238360efbdSAlfred Perlstein 	FD_SET(fd, &svc_fdset);
5248360efbdSAlfred Perlstein 	return (fd);
5258360efbdSAlfred Perlstein }
5268360efbdSAlfred Perlstein 
5278360efbdSAlfred Perlstein static int
find_rmtcallfd_by_netid(char * netid)5288360efbdSAlfred Perlstein find_rmtcallfd_by_netid(char *netid)
5298360efbdSAlfred Perlstein {
5308360efbdSAlfred Perlstein 	struct rmtcallfd_list *rmt;
5318360efbdSAlfred Perlstein 
5328360efbdSAlfred Perlstein 	for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
5338360efbdSAlfred Perlstein 		if (strcmp(netid, rmt->netid) == 0) {
5348360efbdSAlfred Perlstein 			return (rmt->fd);
5358360efbdSAlfred Perlstein 		}
5368360efbdSAlfred Perlstein 	}
5378360efbdSAlfred Perlstein 	return (-1);
5388360efbdSAlfred Perlstein }
5398360efbdSAlfred Perlstein 
5408360efbdSAlfred Perlstein static SVCXPRT *
find_rmtcallxprt_by_fd(int fd)5418360efbdSAlfred Perlstein find_rmtcallxprt_by_fd(int fd)
5428360efbdSAlfred Perlstein {
5438360efbdSAlfred Perlstein 	struct rmtcallfd_list *rmt;
5448360efbdSAlfred Perlstein 
5458360efbdSAlfred Perlstein 	for (rmt = rmthead; rmt != NULL; rmt = rmt->next) {
5468360efbdSAlfred Perlstein 		if (fd == rmt->fd) {
5478360efbdSAlfred Perlstein 			return (rmt->xprt);
5488360efbdSAlfred Perlstein 		}
5498360efbdSAlfred Perlstein 	}
5508360efbdSAlfred Perlstein 	return (NULL);
5518360efbdSAlfred Perlstein }
5528360efbdSAlfred Perlstein 
5538360efbdSAlfred Perlstein 
5548360efbdSAlfred Perlstein /*
5558360efbdSAlfred Perlstein  * Call a remote procedure service.  This procedure is very quiet when things
5568360efbdSAlfred Perlstein  * go wrong.  The proc is written to support broadcast rpc.  In the broadcast
5578360efbdSAlfred Perlstein  * case, a machine should shut-up instead of complain, lest the requestor be
5588360efbdSAlfred Perlstein  * overrun with complaints at the expense of not hearing a valid reply.
5598360efbdSAlfred Perlstein  * When receiving a request and verifying that the service exists, we
5608360efbdSAlfred Perlstein  *
5618360efbdSAlfred Perlstein  *	receive the request
5628360efbdSAlfred Perlstein  *
5638360efbdSAlfred Perlstein  *	open a new TLI endpoint on the same transport on which we received
5648360efbdSAlfred Perlstein  *	the original request
5658360efbdSAlfred Perlstein  *
5668360efbdSAlfred Perlstein  *	remember the original request's XID (which requires knowing the format
5678360efbdSAlfred Perlstein  *	of the svc_dg_data structure)
5688360efbdSAlfred Perlstein  *
5698360efbdSAlfred Perlstein  *	forward the request, with a new XID, to the requested service,
5708360efbdSAlfred Perlstein  *	remembering the XID used to send this request (for later use in
5718360efbdSAlfred Perlstein  *	reassociating the answer with the original request), the requestor's
5728360efbdSAlfred Perlstein  *	address, the file descriptor on which the forwarded request is
5738360efbdSAlfred Perlstein  *	made and the service's address.
5748360efbdSAlfred Perlstein  *
5758360efbdSAlfred Perlstein  *	mark the file descriptor on which we anticipate receiving a reply from
5768360efbdSAlfred Perlstein  *	the service and one to select for in our private svc_run procedure
5778360efbdSAlfred Perlstein  *
5788360efbdSAlfred Perlstein  * At some time in the future, a reply will be received from the service to
5798360efbdSAlfred Perlstein  * which we forwarded the request.  At that time, we detect that the socket
5808360efbdSAlfred Perlstein  * used was for forwarding (by looking through the finfo structures to see
5818360efbdSAlfred Perlstein  * whether the fd corresponds to one of those) and call handle_reply() to
5828360efbdSAlfred Perlstein  *
5838360efbdSAlfred Perlstein  *	receive the reply
5848360efbdSAlfred Perlstein  *
5858360efbdSAlfred Perlstein  *	bundle the reply, along with the service's universal address
5868360efbdSAlfred Perlstein  *
5878360efbdSAlfred Perlstein  *	create a SVCXPRT structure and use a version of svc_sendreply
5888360efbdSAlfred Perlstein  *	that allows us to specify the reply XID and destination, send the reply
5898360efbdSAlfred Perlstein  *	to the original requestor.
5908360efbdSAlfred Perlstein  */
5918360efbdSAlfred Perlstein 
5928360efbdSAlfred Perlstein void
rpcbproc_callit_com(struct svc_req * rqstp,SVCXPRT * transp,rpcproc_t reply_type,rpcvers_t versnum)5938360efbdSAlfred Perlstein rpcbproc_callit_com(struct svc_req *rqstp, SVCXPRT *transp,
5948360efbdSAlfred Perlstein 		    rpcproc_t reply_type, rpcvers_t versnum)
5958360efbdSAlfred Perlstein {
5968360efbdSAlfred Perlstein 	register rpcblist_ptr rbl;
5978360efbdSAlfred Perlstein 	struct netconfig *nconf;
5988360efbdSAlfred Perlstein 	struct netbuf *caller;
5998360efbdSAlfred Perlstein 	struct r_rmtcall_args a;
6008360efbdSAlfred Perlstein 	char *buf_alloc = NULL, *outbufp;
6018360efbdSAlfred Perlstein 	char *outbuf_alloc = NULL;
6028360efbdSAlfred Perlstein 	char buf[RPC_BUF_MAX], outbuf[RPC_BUF_MAX];
6038360efbdSAlfred Perlstein 	struct netbuf *na = (struct netbuf *) NULL;
6048360efbdSAlfred Perlstein 	struct rpc_msg call_msg;
6058360efbdSAlfred Perlstein 	int outlen;
6068360efbdSAlfred Perlstein 	u_int sendsz;
6078360efbdSAlfred Perlstein 	XDR outxdr;
6088360efbdSAlfred Perlstein 	AUTH *auth;
6098360efbdSAlfred Perlstein 	int fd = -1;
61009d4f778SIan Dowse 	char *uaddr, *m_uaddr = NULL, *local_uaddr = NULL;
6118360efbdSAlfred Perlstein 	u_int32_t *xidp;
6128360efbdSAlfred Perlstein 	struct __rpc_sockinfo si;
6138360efbdSAlfred Perlstein 	struct sockaddr *localsa;
6148360efbdSAlfred Perlstein 	struct netbuf tbuf;
6158360efbdSAlfred Perlstein 
6168360efbdSAlfred Perlstein 	if (!__rpc_fd2sockinfo(transp->xp_fd, &si)) {
6178360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
6188360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
6198360efbdSAlfred Perlstein 		return;
6208360efbdSAlfred Perlstein 	}
6218360efbdSAlfred Perlstein 	if (si.si_socktype != SOCK_DGRAM)
6228360efbdSAlfred Perlstein 		return;	/* Only datagram type accepted */
6238360efbdSAlfred Perlstein 	sendsz = __rpc_get_t_size(si.si_af, si.si_proto, UDPMSGSIZE);
6248360efbdSAlfred Perlstein 	if (sendsz == 0) {	/* data transfer not supported */
6258360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
6268360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
6278360efbdSAlfred Perlstein 		return;
6288360efbdSAlfred Perlstein 	}
6298360efbdSAlfred Perlstein 	/*
6308360efbdSAlfred Perlstein 	 * Should be multiple of 4 for XDR.
6318360efbdSAlfred Perlstein 	 */
632e9fc928fSPedro F. Giffuni 	sendsz = roundup(sendsz, 4);
6338360efbdSAlfred Perlstein 	if (sendsz > RPC_BUF_MAX) {
6348360efbdSAlfred Perlstein #ifdef	notyet
6358360efbdSAlfred Perlstein 		buf_alloc = alloca(sendsz);		/* not in IDR2? */
6368360efbdSAlfred Perlstein #else
6378360efbdSAlfred Perlstein 		buf_alloc = malloc(sendsz);
6388360efbdSAlfred Perlstein #endif	/* notyet */
6398360efbdSAlfred Perlstein 		if (buf_alloc == NULL) {
6408360efbdSAlfred Perlstein 			if (debugging)
6418360efbdSAlfred Perlstein 				fprintf(stderr,
6428360efbdSAlfred Perlstein 					"rpcbproc_callit_com:  No Memory!\n");
6438360efbdSAlfred Perlstein 			if (reply_type == RPCBPROC_INDIRECT)
6448360efbdSAlfred Perlstein 				svcerr_systemerr(transp);
6458360efbdSAlfred Perlstein 			return;
6468360efbdSAlfred Perlstein 		}
6478360efbdSAlfred Perlstein 		a.rmt_args.args = buf_alloc;
6488360efbdSAlfred Perlstein 	} else {
6498360efbdSAlfred Perlstein 		a.rmt_args.args = buf;
6508360efbdSAlfred Perlstein 	}
6518360efbdSAlfred Perlstein 
6528360efbdSAlfred Perlstein 	call_msg.rm_xid = 0;	/* For error checking purposes */
6538360efbdSAlfred Perlstein 	if (!svc_getargs(transp, (xdrproc_t) xdr_rmtcall_args, (char *) &a)) {
6548360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
6558360efbdSAlfred Perlstein 			svcerr_decode(transp);
6568360efbdSAlfred Perlstein 		if (debugging)
6578360efbdSAlfred Perlstein 			fprintf(stderr,
6588360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  svc_getargs failed\n");
6598360efbdSAlfred Perlstein 		goto error;
6608360efbdSAlfred Perlstein 	}
6618360efbdSAlfred Perlstein 
6628360efbdSAlfred Perlstein 	if (!check_callit(transp, &a, versnum)) {
6638360efbdSAlfred Perlstein 		svcerr_weakauth(transp);
6648360efbdSAlfred Perlstein 		goto error;
6658360efbdSAlfred Perlstein 	}
6668360efbdSAlfred Perlstein 
6678360efbdSAlfred Perlstein 	caller = svc_getrpccaller(transp);
6688360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
6698360efbdSAlfred Perlstein 	if (debugging) {
6708360efbdSAlfred Perlstein 		uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid), caller);
6718360efbdSAlfred Perlstein 		fprintf(stderr, "%s %s req for (%lu, %lu, %lu, %s) from %s : ",
6728360efbdSAlfred Perlstein 			versnum == PMAPVERS ? "pmap_rmtcall" :
6738360efbdSAlfred Perlstein 			versnum == RPCBVERS ? "rpcb_rmtcall" :
6748360efbdSAlfred Perlstein 			versnum == RPCBVERS4 ? "rpcb_indirect" : "unknown",
6758360efbdSAlfred Perlstein 			reply_type == RPCBPROC_INDIRECT ? "indirect" : "callit",
6768360efbdSAlfred Perlstein 			(unsigned long)a.rmt_prog, (unsigned long)a.rmt_vers,
6778360efbdSAlfred Perlstein 			(unsigned long)a.rmt_proc, transp->xp_netid,
6788360efbdSAlfred Perlstein 			uaddr ? uaddr : "unknown");
67909d4f778SIan Dowse 		free(uaddr);
6808360efbdSAlfred Perlstein 	}
6818360efbdSAlfred Perlstein #endif
6828360efbdSAlfred Perlstein 
6838360efbdSAlfred Perlstein 	rbl = find_service(a.rmt_prog, a.rmt_vers, transp->xp_netid);
6848360efbdSAlfred Perlstein 
6858360efbdSAlfred Perlstein 	rpcbs_rmtcall(versnum - 2, reply_type, a.rmt_prog, a.rmt_vers,
6868360efbdSAlfred Perlstein 			a.rmt_proc, transp->xp_netid, rbl);
6878360efbdSAlfred Perlstein 
6888360efbdSAlfred Perlstein 	if (rbl == (rpcblist_ptr)NULL) {
6898360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
6908360efbdSAlfred Perlstein 		if (debugging)
6918360efbdSAlfred Perlstein 			fprintf(stderr, "not found\n");
6928360efbdSAlfred Perlstein #endif
6938360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
6948360efbdSAlfred Perlstein 			svcerr_noprog(transp);
6958360efbdSAlfred Perlstein 		goto error;
6968360efbdSAlfred Perlstein 	}
6978360efbdSAlfred Perlstein 	if (rbl->rpcb_map.r_vers != a.rmt_vers) {
6988360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT) {
6998360efbdSAlfred Perlstein 			rpcvers_t vers_low, vers_high;
7008360efbdSAlfred Perlstein 
7018360efbdSAlfred Perlstein 			find_versions(a.rmt_prog, transp->xp_netid,
7028360efbdSAlfred Perlstein 				&vers_low, &vers_high);
7038360efbdSAlfred Perlstein 			svcerr_progvers(transp, vers_low, vers_high);
7048360efbdSAlfred Perlstein 		}
7058360efbdSAlfred Perlstein 		goto error;
7068360efbdSAlfred Perlstein 	}
7078360efbdSAlfred Perlstein 
7088360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
7098360efbdSAlfred Perlstein 	if (debugging)
7108360efbdSAlfred Perlstein 		fprintf(stderr, "found at uaddr %s\n", rbl->rpcb_map.r_addr);
7118360efbdSAlfred Perlstein #endif
7128360efbdSAlfred Perlstein 	/*
7138360efbdSAlfred Perlstein 	 *	Check whether this entry is valid and a server is present
7148360efbdSAlfred Perlstein 	 *	Mergeaddr() returns NULL if no such entry is present, and
7158360efbdSAlfred Perlstein 	 *	returns "" if the entry was present but the server is not
7168360efbdSAlfred Perlstein 	 *	present (i.e., it crashed).
7178360efbdSAlfred Perlstein 	 */
7188360efbdSAlfred Perlstein 	if (reply_type == RPCBPROC_INDIRECT) {
7198360efbdSAlfred Perlstein 		uaddr = mergeaddr(transp, transp->xp_netid,
7208360efbdSAlfred Perlstein 			rbl->rpcb_map.r_addr, NULL);
72109d4f778SIan Dowse 		if (uaddr == NULL || uaddr[0] == '\0') {
7228360efbdSAlfred Perlstein 			svcerr_noprog(transp);
72309d4f778SIan Dowse 			free(uaddr);
7248360efbdSAlfred Perlstein 			goto error;
7258360efbdSAlfred Perlstein 		}
72609d4f778SIan Dowse 		free(uaddr);
7278360efbdSAlfred Perlstein 	}
7288360efbdSAlfred Perlstein 	nconf = rpcbind_get_conf(transp->xp_netid);
7298360efbdSAlfred Perlstein 	if (nconf == (struct netconfig *)NULL) {
7308360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
7318360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
7328360efbdSAlfred Perlstein 		if (debugging)
7338360efbdSAlfred Perlstein 			fprintf(stderr,
7348360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  rpcbind_get_conf failed\n");
7358360efbdSAlfred Perlstein 		goto error;
7368360efbdSAlfred Perlstein 	}
7378360efbdSAlfred Perlstein 	localsa = local_sa(((struct sockaddr *)caller->buf)->sa_family);
7388360efbdSAlfred Perlstein 	if (localsa == NULL) {
7398360efbdSAlfred Perlstein 		if (debugging)
7408360efbdSAlfred Perlstein 			fprintf(stderr,
7418360efbdSAlfred Perlstein 			"rpcbproc_callit_com: no local address\n");
7428360efbdSAlfred Perlstein 		goto error;
7438360efbdSAlfred Perlstein 	}
7448360efbdSAlfred Perlstein 	tbuf.len = tbuf.maxlen = localsa->sa_len;
7458360efbdSAlfred Perlstein 	tbuf.buf = localsa;
7468360efbdSAlfred Perlstein 	local_uaddr =
7478360efbdSAlfred Perlstein 	    addrmerge(&tbuf, rbl->rpcb_map.r_addr, NULL, nconf->nc_netid);
7488360efbdSAlfred Perlstein 	m_uaddr = addrmerge(caller, rbl->rpcb_map.r_addr, NULL,
7498360efbdSAlfred Perlstein 			nconf->nc_netid);
7508360efbdSAlfred Perlstein #ifdef RPCBIND_DEBUG
7518360efbdSAlfred Perlstein 	if (debugging)
7528360efbdSAlfred Perlstein 		fprintf(stderr, "merged uaddr %s\n", m_uaddr);
7538360efbdSAlfred Perlstein #endif
7548360efbdSAlfred Perlstein 	if ((fd = find_rmtcallfd_by_netid(nconf->nc_netid)) == -1) {
7558360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
7568360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
7578360efbdSAlfred Perlstein 		goto error;
7588360efbdSAlfred Perlstein 	}
7598360efbdSAlfred Perlstein 	xidp = __rpcb_get_dg_xidp(transp);
76098f36a35SIan Dowse 	switch (forward_register(*xidp, caller, fd, m_uaddr, reply_type,
76198f36a35SIan Dowse 	    versnum, &call_msg.rm_xid)) {
76298f36a35SIan Dowse 	case 1:
76398f36a35SIan Dowse 		/* Success; forward_register() will free m_uaddr for us. */
76498f36a35SIan Dowse 		m_uaddr = NULL;
76598f36a35SIan Dowse 		break;
76698f36a35SIan Dowse 	case 0:
7678360efbdSAlfred Perlstein 		/*
7688360efbdSAlfred Perlstein 		 * A duplicate request for the slow server.  Let's not
7698360efbdSAlfred Perlstein 		 * beat on it any more.
7708360efbdSAlfred Perlstein 		 */
7718360efbdSAlfred Perlstein 		if (debugging)
7728360efbdSAlfred Perlstein 			fprintf(stderr,
7738360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  duplicate request\n");
7748360efbdSAlfred Perlstein 		goto error;
77598f36a35SIan Dowse 	case -1:
7768360efbdSAlfred Perlstein 		/*  forward_register failed.  Perhaps no memory. */
7778360efbdSAlfred Perlstein 		if (debugging)
7788360efbdSAlfred Perlstein 			fprintf(stderr,
7798360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  forward_register failed\n");
7808360efbdSAlfred Perlstein 		goto error;
7818360efbdSAlfred Perlstein 	}
7828360efbdSAlfred Perlstein 
7838360efbdSAlfred Perlstein #ifdef DEBUG_RMTCALL
7848360efbdSAlfred Perlstein 	if (debugging)
7858360efbdSAlfred Perlstein 		fprintf(stderr,
7868360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  original XID %x, new XID %x\n",
7878360efbdSAlfred Perlstein 				*xidp, call_msg.rm_xid);
7888360efbdSAlfred Perlstein #endif
7898360efbdSAlfred Perlstein 	call_msg.rm_direction = CALL;
7908360efbdSAlfred Perlstein 	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
7918360efbdSAlfred Perlstein 	call_msg.rm_call.cb_prog = a.rmt_prog;
7928360efbdSAlfred Perlstein 	call_msg.rm_call.cb_vers = a.rmt_vers;
7938360efbdSAlfred Perlstein 	if (sendsz > RPC_BUF_MAX) {
7948360efbdSAlfred Perlstein #ifdef	notyet
7958360efbdSAlfred Perlstein 		outbuf_alloc = alloca(sendsz);	/* not in IDR2? */
7968360efbdSAlfred Perlstein #else
7978360efbdSAlfred Perlstein 		outbuf_alloc = malloc(sendsz);
7988360efbdSAlfred Perlstein #endif	/* notyet */
7998360efbdSAlfred Perlstein 		if (outbuf_alloc == NULL) {
8008360efbdSAlfred Perlstein 			if (reply_type == RPCBPROC_INDIRECT)
8018360efbdSAlfred Perlstein 				svcerr_systemerr(transp);
8028360efbdSAlfred Perlstein 			if (debugging)
8038360efbdSAlfred Perlstein 				fprintf(stderr,
8048360efbdSAlfred Perlstein 				"rpcbproc_callit_com:  No memory!\n");
8058360efbdSAlfred Perlstein 			goto error;
8068360efbdSAlfred Perlstein 		}
8078360efbdSAlfred Perlstein 		xdrmem_create(&outxdr, outbuf_alloc, sendsz, XDR_ENCODE);
8088360efbdSAlfred Perlstein 	} else {
8098360efbdSAlfred Perlstein 		xdrmem_create(&outxdr, outbuf, sendsz, XDR_ENCODE);
8108360efbdSAlfred Perlstein 	}
8118360efbdSAlfred Perlstein 	if (!xdr_callhdr(&outxdr, &call_msg)) {
8128360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8138360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8148360efbdSAlfred Perlstein 		if (debugging)
8158360efbdSAlfred Perlstein 			fprintf(stderr,
8168360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  xdr_callhdr failed\n");
8178360efbdSAlfred Perlstein 		goto error;
8188360efbdSAlfred Perlstein 	}
8198360efbdSAlfred Perlstein 	if (!xdr_u_int32_t(&outxdr, &(a.rmt_proc))) {
8208360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8218360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8228360efbdSAlfred Perlstein 		if (debugging)
8238360efbdSAlfred Perlstein 			fprintf(stderr,
8248360efbdSAlfred Perlstein 			"rpcbproc_callit_com:  xdr_u_long failed\n");
8258360efbdSAlfred Perlstein 		goto error;
8268360efbdSAlfred Perlstein 	}
8278360efbdSAlfred Perlstein 
8288360efbdSAlfred Perlstein 	if (rqstp->rq_cred.oa_flavor == AUTH_NULL) {
8298360efbdSAlfred Perlstein 		auth = authnone_create();
8308360efbdSAlfred Perlstein 	} else if (rqstp->rq_cred.oa_flavor == AUTH_SYS) {
8318360efbdSAlfred Perlstein 		struct authunix_parms *au;
8328360efbdSAlfred Perlstein 
8338360efbdSAlfred Perlstein 		au = (struct authunix_parms *)rqstp->rq_clntcred;
8348360efbdSAlfred Perlstein 		auth = authunix_create(au->aup_machname,
8358360efbdSAlfred Perlstein 				au->aup_uid, au->aup_gid,
8368360efbdSAlfred Perlstein 				au->aup_len, au->aup_gids);
8378360efbdSAlfred Perlstein 		if (auth == NULL) /* fall back */
8388360efbdSAlfred Perlstein 			auth = authnone_create();
8398360efbdSAlfred Perlstein 	} else {
8408360efbdSAlfred Perlstein 		/* we do not support any other authentication scheme */
8418360efbdSAlfred Perlstein 		if (debugging)
8428360efbdSAlfred Perlstein 			fprintf(stderr,
8438360efbdSAlfred Perlstein "rpcbproc_callit_com:  oa_flavor != AUTH_NONE and oa_flavor != AUTH_SYS\n");
8448360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8458360efbdSAlfred Perlstein 			svcerr_weakauth(transp); /* XXX too strong.. */
8468360efbdSAlfred Perlstein 		goto error;
8478360efbdSAlfred Perlstein 	}
8488360efbdSAlfred Perlstein 	if (auth == NULL) {
8498360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8508360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8518360efbdSAlfred Perlstein 		if (debugging)
8528360efbdSAlfred Perlstein 			fprintf(stderr,
8538360efbdSAlfred Perlstein 		"rpcbproc_callit_com:  authwhatever_create returned NULL\n");
8548360efbdSAlfred Perlstein 		goto error;
8558360efbdSAlfred Perlstein 	}
8568360efbdSAlfred Perlstein 	if (!AUTH_MARSHALL(auth, &outxdr)) {
8578360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8588360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8598360efbdSAlfred Perlstein 		AUTH_DESTROY(auth);
8608360efbdSAlfred Perlstein 		if (debugging)
8618360efbdSAlfred Perlstein 			fprintf(stderr,
8628360efbdSAlfred Perlstein 		"rpcbproc_callit_com:  AUTH_MARSHALL failed\n");
8638360efbdSAlfred Perlstein 		goto error;
8648360efbdSAlfred Perlstein 	}
8658360efbdSAlfred Perlstein 	AUTH_DESTROY(auth);
8668360efbdSAlfred Perlstein 	if (!xdr_opaque_parms(&outxdr, &a)) {
8678360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8688360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8698360efbdSAlfred Perlstein 		if (debugging)
8708360efbdSAlfred Perlstein 			fprintf(stderr,
8718360efbdSAlfred Perlstein 		"rpcbproc_callit_com:  xdr_opaque_parms failed\n");
8728360efbdSAlfred Perlstein 		goto error;
8738360efbdSAlfred Perlstein 	}
8748360efbdSAlfred Perlstein 	outlen = (int) XDR_GETPOS(&outxdr);
8758360efbdSAlfred Perlstein 	if (outbuf_alloc)
8768360efbdSAlfred Perlstein 		outbufp = outbuf_alloc;
8778360efbdSAlfred Perlstein 	else
8788360efbdSAlfred Perlstein 		outbufp = outbuf;
8798360efbdSAlfred Perlstein 
8808360efbdSAlfred Perlstein 	na = uaddr2taddr(nconf, local_uaddr);
8818360efbdSAlfred Perlstein 	if (!na) {
8828360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8838360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8848360efbdSAlfred Perlstein 		goto error;
8858360efbdSAlfred Perlstein 	}
8868360efbdSAlfred Perlstein 
8878360efbdSAlfred Perlstein 	if (sendto(fd, outbufp, outlen, 0, (struct sockaddr *)na->buf, na->len)
8888360efbdSAlfred Perlstein 	    != outlen) {
8898360efbdSAlfred Perlstein 		if (debugging)
8908360efbdSAlfred Perlstein 			fprintf(stderr,
8918360efbdSAlfred Perlstein 	"rpcbproc_callit_com:  sendto failed:  errno %d\n", errno);
8928360efbdSAlfred Perlstein 		if (reply_type == RPCBPROC_INDIRECT)
8938360efbdSAlfred Perlstein 			svcerr_systemerr(transp);
8948360efbdSAlfred Perlstein 		goto error;
8958360efbdSAlfred Perlstein 	}
8968360efbdSAlfred Perlstein 	goto out;
8978360efbdSAlfred Perlstein 
8988360efbdSAlfred Perlstein error:
8998360efbdSAlfred Perlstein 	if (call_msg.rm_xid != 0)
9008360efbdSAlfred Perlstein 		(void) free_slot_by_xid(call_msg.rm_xid);
9018360efbdSAlfred Perlstein out:
9028360efbdSAlfred Perlstein 	free(local_uaddr);
90309d4f778SIan Dowse 	free(buf_alloc);
90409d4f778SIan Dowse 	free(outbuf_alloc);
905*7ef6eb24SHiroki Sato 	netbuffree(na);
90609d4f778SIan Dowse 	free(m_uaddr);
9078360efbdSAlfred Perlstein }
9088360efbdSAlfred Perlstein 
9098360efbdSAlfred Perlstein /*
9108360efbdSAlfred Perlstein  * Makes an entry into the FIFO for the given request.
91198f36a35SIan Dowse  * Returns 1 on success, 0 if this is a duplicate request, or -1 on error.
91298f36a35SIan Dowse  * *callxidp is set to the xid of the call.
9138360efbdSAlfred Perlstein  */
91498f36a35SIan Dowse static int
forward_register(u_int32_t caller_xid,struct netbuf * caller_addr,int forward_fd,char * uaddr,rpcproc_t reply_type,rpcvers_t versnum,u_int32_t * callxidp)9158360efbdSAlfred Perlstein forward_register(u_int32_t caller_xid, struct netbuf *caller_addr,
9168360efbdSAlfred Perlstein 		 int forward_fd, char *uaddr, rpcproc_t reply_type,
91798f36a35SIan Dowse 		 rpcvers_t versnum, u_int32_t *callxidp)
9188360efbdSAlfred Perlstein {
9198360efbdSAlfred Perlstein 	int		i;
9208360efbdSAlfred Perlstein 	int		j = 0;
9218360efbdSAlfred Perlstein 	time_t		min_time, time_now;
9228360efbdSAlfred Perlstein 	static u_int32_t	lastxid;
9238360efbdSAlfred Perlstein 	int		entry = -1;
9248360efbdSAlfred Perlstein 
9258360efbdSAlfred Perlstein 	min_time = FINFO[0].time;
9268360efbdSAlfred Perlstein 	time_now = time((time_t *)0);
9278360efbdSAlfred Perlstein 	/* initialization */
9288360efbdSAlfred Perlstein 	if (lastxid == 0)
9298360efbdSAlfred Perlstein 		lastxid = time_now * NFORWARD;
9308360efbdSAlfred Perlstein 
9318360efbdSAlfred Perlstein 	/*
932d64ada50SJens Schweikhardt 	 * Check if it is a duplicate entry. Then,
9338360efbdSAlfred Perlstein 	 * try to find an empty slot.  If not available, then
9348360efbdSAlfred Perlstein 	 * use the slot with the earliest time.
9358360efbdSAlfred Perlstein 	 */
9368360efbdSAlfred Perlstein 	for (i = 0; i < NFORWARD; i++) {
9378360efbdSAlfred Perlstein 		if (FINFO[i].flag & FINFO_ACTIVE) {
9388360efbdSAlfred Perlstein 			if ((FINFO[i].caller_xid == caller_xid) &&
9398360efbdSAlfred Perlstein 			    (FINFO[i].reply_type == reply_type) &&
9408360efbdSAlfred Perlstein 			    (FINFO[i].versnum == versnum) &&
9418360efbdSAlfred Perlstein 			    (!netbufcmp(FINFO[i].caller_addr,
9428360efbdSAlfred Perlstein 					    caller_addr))) {
9438360efbdSAlfred Perlstein 				FINFO[i].time = time((time_t *)0);
9448360efbdSAlfred Perlstein 				return (0);	/* Duplicate entry */
9458360efbdSAlfred Perlstein 			} else {
9468360efbdSAlfred Perlstein 				/* Should we wait any longer */
9478360efbdSAlfred Perlstein 				if ((time_now - FINFO[i].time) > MAXTIME_OFF)
9488360efbdSAlfred Perlstein 					(void) free_slot_by_index(i);
9498360efbdSAlfred Perlstein 			}
9508360efbdSAlfred Perlstein 		}
9518360efbdSAlfred Perlstein 		if (entry == -1) {
9528360efbdSAlfred Perlstein 			if ((FINFO[i].flag & FINFO_ACTIVE) == 0) {
9538360efbdSAlfred Perlstein 				entry = i;
9548360efbdSAlfred Perlstein 			} else if (FINFO[i].time < min_time) {
9558360efbdSAlfred Perlstein 				j = i;
9568360efbdSAlfred Perlstein 				min_time = FINFO[i].time;
9578360efbdSAlfred Perlstein 			}
9588360efbdSAlfred Perlstein 		}
9598360efbdSAlfred Perlstein 	}
9608360efbdSAlfred Perlstein 	if (entry != -1) {
9618360efbdSAlfred Perlstein 		/* use this empty slot */
9628360efbdSAlfred Perlstein 		j = entry;
9638360efbdSAlfred Perlstein 	} else {
9648360efbdSAlfred Perlstein 		(void) free_slot_by_index(j);
9658360efbdSAlfred Perlstein 	}
9668360efbdSAlfred Perlstein 	if ((FINFO[j].caller_addr = netbufdup(caller_addr)) == NULL) {
9678360efbdSAlfred Perlstein 		return (-1);
9688360efbdSAlfred Perlstein 	}
9698360efbdSAlfred Perlstein 	rpcb_rmtcalls++;	/* no of pending calls */
9708360efbdSAlfred Perlstein 	FINFO[j].flag = FINFO_ACTIVE;
9718360efbdSAlfred Perlstein 	FINFO[j].reply_type = reply_type;
9728360efbdSAlfred Perlstein 	FINFO[j].versnum = versnum;
9738360efbdSAlfred Perlstein 	FINFO[j].time = time_now;
9748360efbdSAlfred Perlstein 	FINFO[j].caller_xid = caller_xid;
9758360efbdSAlfred Perlstein 	FINFO[j].forward_fd = forward_fd;
9768360efbdSAlfred Perlstein 	/*
9778360efbdSAlfred Perlstein 	 * Though uaddr is not allocated here, it will still be freed
9788360efbdSAlfred Perlstein 	 * from free_slot_*().
9798360efbdSAlfred Perlstein 	 */
9808360efbdSAlfred Perlstein 	FINFO[j].uaddr = uaddr;
9818360efbdSAlfred Perlstein 	lastxid = lastxid + NFORWARD;
98298f36a35SIan Dowse 	/* Don't allow a zero xid below. */
98398f36a35SIan Dowse 	if ((u_int32_t)(lastxid + NFORWARD) <= NFORWARD)
98498f36a35SIan Dowse 		lastxid = NFORWARD;
9858360efbdSAlfred Perlstein 	FINFO[j].forward_xid = lastxid + j;	/* encode slot */
98698f36a35SIan Dowse 	*callxidp = FINFO[j].forward_xid;	/* forward on this xid */
98798f36a35SIan Dowse 	return (1);
9888360efbdSAlfred Perlstein }
9898360efbdSAlfred Perlstein 
9908360efbdSAlfred Perlstein static struct finfo *
forward_find(u_int32_t reply_xid)9918360efbdSAlfred Perlstein forward_find(u_int32_t reply_xid)
9928360efbdSAlfred Perlstein {
9938360efbdSAlfred Perlstein 	int		i;
9948360efbdSAlfred Perlstein 
99598f36a35SIan Dowse 	i = reply_xid % (u_int32_t)NFORWARD;
9968360efbdSAlfred Perlstein 	if ((FINFO[i].flag & FINFO_ACTIVE) &&
9978360efbdSAlfred Perlstein 	    (FINFO[i].forward_xid == reply_xid)) {
9988360efbdSAlfred Perlstein 		return (&FINFO[i]);
9998360efbdSAlfred Perlstein 	}
10008360efbdSAlfred Perlstein 	return (NULL);
10018360efbdSAlfred Perlstein }
10028360efbdSAlfred Perlstein 
10038360efbdSAlfred Perlstein static int
free_slot_by_xid(u_int32_t xid)10048360efbdSAlfred Perlstein free_slot_by_xid(u_int32_t xid)
10058360efbdSAlfred Perlstein {
10068360efbdSAlfred Perlstein 	int entry;
10078360efbdSAlfred Perlstein 
100898f36a35SIan Dowse 	entry = xid % (u_int32_t)NFORWARD;
10098360efbdSAlfred Perlstein 	return (free_slot_by_index(entry));
10108360efbdSAlfred Perlstein }
10118360efbdSAlfred Perlstein 
10128360efbdSAlfred Perlstein static int
free_slot_by_index(int index)10138360efbdSAlfred Perlstein free_slot_by_index(int index)
10148360efbdSAlfred Perlstein {
10158360efbdSAlfred Perlstein 	struct finfo	*fi;
10168360efbdSAlfred Perlstein 
10178360efbdSAlfred Perlstein 	fi = &FINFO[index];
10188360efbdSAlfred Perlstein 	if (fi->flag & FINFO_ACTIVE) {
10198360efbdSAlfred Perlstein 		netbuffree(fi->caller_addr);
10208360efbdSAlfred Perlstein 		/* XXX may be too big, but can't access xprt array here */
10218360efbdSAlfred Perlstein 		if (fi->forward_fd >= svc_maxfd)
10228360efbdSAlfred Perlstein 			svc_maxfd--;
102309d4f778SIan Dowse 		free(fi->uaddr);
10248360efbdSAlfred Perlstein 		fi->flag &= ~FINFO_ACTIVE;
10258360efbdSAlfred Perlstein 		rpcb_rmtcalls--;
10268360efbdSAlfred Perlstein 		return (1);
10278360efbdSAlfred Perlstein 	}
10288360efbdSAlfred Perlstein 	return (0);
10298360efbdSAlfred Perlstein }
10308360efbdSAlfred Perlstein 
10318360efbdSAlfred Perlstein static int
netbufcmp(struct netbuf * n1,struct netbuf * n2)10328360efbdSAlfred Perlstein netbufcmp(struct netbuf *n1, struct netbuf *n2)
10338360efbdSAlfred Perlstein {
10348360efbdSAlfred Perlstein 	return ((n1->len != n2->len) || memcmp(n1->buf, n2->buf, n1->len));
10358360efbdSAlfred Perlstein }
10368360efbdSAlfred Perlstein 
1037066c492aSXin LI static bool_t
netbuf_copybuf(struct netbuf * dst,const struct netbuf * src)1038066c492aSXin LI netbuf_copybuf(struct netbuf *dst, const struct netbuf *src)
1039066c492aSXin LI {
1040c1d0909aSHiroki Sato 	assert(src->len <= src->maxlen);
1041066c492aSXin LI 
1042c1d0909aSHiroki Sato 	if (dst->maxlen < src->len || dst->buf == NULL) {
10430ed633d0SXin LI 		free(dst->buf);
1044c1d0909aSHiroki Sato 		if ((dst->buf = calloc(1, src->maxlen)) == NULL)
1045066c492aSXin LI 			return (FALSE);
1046c1d0909aSHiroki Sato 		dst->maxlen = src->maxlen;
10470ed633d0SXin LI 	}
10480ed633d0SXin LI 
1049c1d0909aSHiroki Sato 	dst->len = src->len;
1050066c492aSXin LI 	memcpy(dst->buf, src->buf, src->len);
1051c1d0909aSHiroki Sato 
1052066c492aSXin LI 	return (TRUE);
1053066c492aSXin LI }
1054066c492aSXin LI 
10558360efbdSAlfred Perlstein static struct netbuf *
netbufdup(struct netbuf * ap)10568360efbdSAlfred Perlstein netbufdup(struct netbuf *ap)
10578360efbdSAlfred Perlstein {
10588360efbdSAlfred Perlstein 	struct netbuf  *np;
10598360efbdSAlfred Perlstein 
1060066c492aSXin LI 	if ((np = calloc(1, sizeof(struct netbuf))) == NULL)
106109d4f778SIan Dowse 		return (NULL);
1062066c492aSXin LI 	if (netbuf_copybuf(np, ap) == FALSE) {
106309d4f778SIan Dowse 		free(np);
106409d4f778SIan Dowse 		return (NULL);
10658360efbdSAlfred Perlstein 	}
10668360efbdSAlfred Perlstein 	return (np);
10678360efbdSAlfred Perlstein }
10688360efbdSAlfred Perlstein 
10698360efbdSAlfred Perlstein static void
netbuffree(struct netbuf * ap)10708360efbdSAlfred Perlstein netbuffree(struct netbuf *ap)
10718360efbdSAlfred Perlstein {
1072*7ef6eb24SHiroki Sato 
1073*7ef6eb24SHiroki Sato 	if (ap == NULL)
1074*7ef6eb24SHiroki Sato 		return;
107509d4f778SIan Dowse 	free(ap->buf);
1076066c492aSXin LI 	ap->buf = NULL;
1077abca534cSBrian Somers 	free(ap);
10788360efbdSAlfred Perlstein }
10798360efbdSAlfred Perlstein 
10808360efbdSAlfred Perlstein 
10818360efbdSAlfred Perlstein #define	MASKVAL	(POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
1082bcb53b16SMartin Blapp extern bool_t __svc_clean_idle(fd_set *, int, bool_t);
10838360efbdSAlfred Perlstein 
10848360efbdSAlfred Perlstein void
my_svc_run(void)108507de1c52SXin LI my_svc_run(void)
10868360efbdSAlfred Perlstein {
10878360efbdSAlfred Perlstein 	size_t nfds;
108835f85edcSConrad Meyer 	struct pollfd pollfds[FD_SETSIZE + 1];
10898360efbdSAlfred Perlstein 	int poll_ret, check_ret;
10908360efbdSAlfred Perlstein 	int n;
10918360efbdSAlfred Perlstein #ifdef SVC_RUN_DEBUG
10928360efbdSAlfred Perlstein 	int i;
10938360efbdSAlfred Perlstein #endif
10948360efbdSAlfred Perlstein 	register struct pollfd	*p;
1095bcb53b16SMartin Blapp 	fd_set cleanfds;
10968360efbdSAlfred Perlstein 
10978360efbdSAlfred Perlstein 	for (;;) {
10988360efbdSAlfred Perlstein 		p = pollfds;
109935f85edcSConrad Meyer 		p->fd = terminate_rfd;
110035f85edcSConrad Meyer 		p->events = MASKVAL;
110135f85edcSConrad Meyer 		p++;
11028360efbdSAlfred Perlstein 		for (n = 0; n <= svc_maxfd; n++) {
11038360efbdSAlfred Perlstein 			if (FD_ISSET(n, &svc_fdset)) {
11048360efbdSAlfred Perlstein 				p->fd = n;
11058360efbdSAlfred Perlstein 				p->events = MASKVAL;
11068360efbdSAlfred Perlstein 				p++;
11078360efbdSAlfred Perlstein 			}
11088360efbdSAlfred Perlstein 		}
11098360efbdSAlfred Perlstein 		nfds = p - pollfds;
11108360efbdSAlfred Perlstein 		poll_ret = 0;
11118360efbdSAlfred Perlstein #ifdef SVC_RUN_DEBUG
11128360efbdSAlfred Perlstein 		if (debugging) {
11138360efbdSAlfred Perlstein 			fprintf(stderr, "polling for read on fd < ");
11148360efbdSAlfred Perlstein 			for (i = 0, p = pollfds; i < nfds; i++, p++)
11158360efbdSAlfred Perlstein 				if (p->events)
11168360efbdSAlfred Perlstein 					fprintf(stderr, "%d ", p->fd);
11178360efbdSAlfred Perlstein 			fprintf(stderr, ">\n");
11188360efbdSAlfred Perlstein 		}
11198360efbdSAlfred Perlstein #endif
112035f85edcSConrad Meyer 		poll_ret = poll(pollfds, nfds, 30 * 1000);
112135f85edcSConrad Meyer 
1122055569f6SConrad Meyer 		if (doterminate != 0) {
1123055569f6SConrad Meyer 			close(rpcbindlockfd);
1124055569f6SConrad Meyer #ifdef WARMSTART
1125055569f6SConrad Meyer 			syslog(LOG_ERR,
1126055569f6SConrad Meyer 			    "rpcbind terminating on signal %d. Restart with \"rpcbind -w\"",
1127055569f6SConrad Meyer 			    (int)doterminate);
1128055569f6SConrad Meyer 			write_warmstart();	/* Dump yourself */
1129055569f6SConrad Meyer #endif
1130055569f6SConrad Meyer 			exit(2);
1131055569f6SConrad Meyer 		}
113235f85edcSConrad Meyer 
113335f85edcSConrad Meyer 		switch (poll_ret) {
113435f85edcSConrad Meyer 		case -1:
113535f85edcSConrad Meyer 			/*
113635f85edcSConrad Meyer 			 * We ignore all errors, continuing with the assumption
113735f85edcSConrad Meyer 			 * that it was set by the signal handlers (or any
113835f85edcSConrad Meyer 			 * other outside event) and not caused by poll().
113935f85edcSConrad Meyer 			 */
11408360efbdSAlfred Perlstein 		case 0:
1141bcb53b16SMartin Blapp 			cleanfds = svc_fdset;
1142bcb53b16SMartin Blapp 			__svc_clean_idle(&cleanfds, 30, FALSE);
11438360efbdSAlfred Perlstein 			continue;
11448360efbdSAlfred Perlstein 		default:
11458360efbdSAlfred Perlstein #ifdef SVC_RUN_DEBUG
11468360efbdSAlfred Perlstein 			if (debugging) {
11478360efbdSAlfred Perlstein 				fprintf(stderr, "poll returned read fds < ");
11488360efbdSAlfred Perlstein 				for (i = 0, p = pollfds; i < nfds; i++, p++)
11498360efbdSAlfred Perlstein 					if (p->revents)
11508360efbdSAlfred Perlstein 						fprintf(stderr, "%d ", p->fd);
11518360efbdSAlfred Perlstein 				fprintf(stderr, ">\n");
11528360efbdSAlfred Perlstein 			}
11538360efbdSAlfred Perlstein #endif
11548360efbdSAlfred Perlstein 			/*
11558360efbdSAlfred Perlstein 			 * If we found as many replies on callback fds
11568360efbdSAlfred Perlstein 			 * as the number of descriptors selectable which
11578360efbdSAlfred Perlstein 			 * poll() returned, there can be no more so we
11588360efbdSAlfred Perlstein 			 * don't call svc_getreq_poll.  Otherwise, there
11598360efbdSAlfred Perlstein 			 * must be another so we must call svc_getreq_poll.
11608360efbdSAlfred Perlstein 			 */
11618360efbdSAlfred Perlstein 			if ((check_ret = check_rmtcalls(pollfds, nfds)) ==
11628360efbdSAlfred Perlstein 			    poll_ret)
11638360efbdSAlfred Perlstein 				continue;
11648360efbdSAlfred Perlstein 			svc_getreq_poll(pollfds, poll_ret-check_ret);
11658360efbdSAlfred Perlstein 		}
11668360efbdSAlfred Perlstein #ifdef SVC_RUN_DEBUG
11678360efbdSAlfred Perlstein 		if (debugging) {
11688360efbdSAlfred Perlstein 			fprintf(stderr, "svc_maxfd now %u\n", svc_maxfd);
11698360efbdSAlfred Perlstein 		}
11708360efbdSAlfred Perlstein #endif
11718360efbdSAlfred Perlstein 	}
11728360efbdSAlfred Perlstein }
11738360efbdSAlfred Perlstein 
11748360efbdSAlfred Perlstein static int
check_rmtcalls(struct pollfd * pfds,int nfds)11758360efbdSAlfred Perlstein check_rmtcalls(struct pollfd *pfds, int nfds)
11768360efbdSAlfred Perlstein {
11778360efbdSAlfred Perlstein 	int j, ncallbacks_found = 0, rmtcalls_pending;
11788360efbdSAlfred Perlstein 	SVCXPRT *xprt;
11798360efbdSAlfred Perlstein 
11808360efbdSAlfred Perlstein 	if (rpcb_rmtcalls == 0)
11818360efbdSAlfred Perlstein 		return (0);
11828360efbdSAlfred Perlstein 
11838360efbdSAlfred Perlstein 	rmtcalls_pending = rpcb_rmtcalls;
11848360efbdSAlfred Perlstein 	for (j = 0; j < nfds; j++) {
11858360efbdSAlfred Perlstein 		if ((xprt = find_rmtcallxprt_by_fd(pfds[j].fd)) != NULL) {
11868360efbdSAlfred Perlstein 			if (pfds[j].revents) {
11878360efbdSAlfred Perlstein 				ncallbacks_found++;
11888360efbdSAlfred Perlstein #ifdef DEBUG_RMTCALL
11898360efbdSAlfred Perlstein 			if (debugging)
11908360efbdSAlfred Perlstein 				fprintf(stderr,
11918360efbdSAlfred Perlstein "my_svc_run:  polled on forwarding fd %d, netid %s - calling handle_reply\n",
11928360efbdSAlfred Perlstein 		pfds[j].fd, xprt->xp_netid);
11938360efbdSAlfred Perlstein #endif
11948360efbdSAlfred Perlstein 				handle_reply(pfds[j].fd, xprt);
11958360efbdSAlfred Perlstein 				pfds[j].revents = 0;
11968360efbdSAlfred Perlstein 				if (ncallbacks_found >= rmtcalls_pending) {
11978360efbdSAlfred Perlstein 					break;
11988360efbdSAlfred Perlstein 				}
11998360efbdSAlfred Perlstein 			}
12008360efbdSAlfred Perlstein 		}
12018360efbdSAlfred Perlstein 	}
12028360efbdSAlfred Perlstein 	return (ncallbacks_found);
12038360efbdSAlfred Perlstein }
12048360efbdSAlfred Perlstein 
12058360efbdSAlfred Perlstein static void
xprt_set_caller(SVCXPRT * xprt,struct finfo * fi)12068360efbdSAlfred Perlstein xprt_set_caller(SVCXPRT *xprt, struct finfo *fi)
12078360efbdSAlfred Perlstein {
12088360efbdSAlfred Perlstein 	u_int32_t *xidp;
12098360efbdSAlfred Perlstein 
1210066c492aSXin LI 	netbuf_copybuf(svc_getrpccaller(xprt), fi->caller_addr);
12118360efbdSAlfred Perlstein 	xidp = __rpcb_get_dg_xidp(xprt);
12128360efbdSAlfred Perlstein 	*xidp = fi->caller_xid;
12138360efbdSAlfred Perlstein }
12148360efbdSAlfred Perlstein 
12158360efbdSAlfred Perlstein /*
12168360efbdSAlfred Perlstein  * Call svcerr_systemerr() only if RPCBVERS4
12178360efbdSAlfred Perlstein  */
12188360efbdSAlfred Perlstein static void
send_svcsyserr(SVCXPRT * xprt,struct finfo * fi)12198360efbdSAlfred Perlstein send_svcsyserr(SVCXPRT *xprt, struct finfo *fi)
12208360efbdSAlfred Perlstein {
12218360efbdSAlfred Perlstein 	if (fi->reply_type == RPCBPROC_INDIRECT) {
12228360efbdSAlfred Perlstein 		xprt_set_caller(xprt, fi);
12238360efbdSAlfred Perlstein 		svcerr_systemerr(xprt);
12248360efbdSAlfred Perlstein 	}
12258360efbdSAlfred Perlstein }
12268360efbdSAlfred Perlstein 
12278360efbdSAlfred Perlstein static void
handle_reply(int fd,SVCXPRT * xprt)12288360efbdSAlfred Perlstein handle_reply(int fd, SVCXPRT *xprt)
12298360efbdSAlfred Perlstein {
12308360efbdSAlfred Perlstein 	XDR		reply_xdrs;
12318360efbdSAlfred Perlstein 	struct rpc_msg	reply_msg;
12328360efbdSAlfred Perlstein 	struct rpc_err	reply_error;
12338360efbdSAlfred Perlstein 	char		*buffer;
12348360efbdSAlfred Perlstein 	struct finfo	*fi;
12358360efbdSAlfred Perlstein 	int		inlen, pos, len;
12368360efbdSAlfred Perlstein 	struct r_rmtcall_args a;
12378360efbdSAlfred Perlstein 	struct sockaddr_storage ss;
12388360efbdSAlfred Perlstein 	socklen_t fromlen;
12398360efbdSAlfred Perlstein #ifdef SVC_RUN_DEBUG
12408360efbdSAlfred Perlstein 	char *uaddr;
12418360efbdSAlfred Perlstein #endif
12428360efbdSAlfred Perlstein 
12438360efbdSAlfred Perlstein 	buffer = malloc(RPC_BUF_MAX);
12448360efbdSAlfred Perlstein 	if (buffer == NULL)
12458360efbdSAlfred Perlstein 		goto done;
12468360efbdSAlfred Perlstein 
12478360efbdSAlfred Perlstein 	do {
1248200ad46cSWarner Losh 		fromlen = sizeof(ss);
12498360efbdSAlfred Perlstein 		inlen = recvfrom(fd, buffer, RPC_BUF_MAX, 0,
12508360efbdSAlfred Perlstein 			    (struct sockaddr *)&ss, &fromlen);
12518360efbdSAlfred Perlstein 	} while (inlen < 0 && errno == EINTR);
12528360efbdSAlfred Perlstein 	if (inlen < 0) {
12538360efbdSAlfred Perlstein 		if (debugging)
12548360efbdSAlfred Perlstein 			fprintf(stderr,
12558360efbdSAlfred Perlstein 	"handle_reply:  recvfrom returned %d, errno %d\n", inlen, errno);
12568360efbdSAlfred Perlstein 		goto done;
12578360efbdSAlfred Perlstein 	}
12588360efbdSAlfred Perlstein 
12598360efbdSAlfred Perlstein 	reply_msg.acpted_rply.ar_verf = _null_auth;
12608360efbdSAlfred Perlstein 	reply_msg.acpted_rply.ar_results.where = 0;
12618360efbdSAlfred Perlstein 	reply_msg.acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
12628360efbdSAlfred Perlstein 
12638360efbdSAlfred Perlstein 	xdrmem_create(&reply_xdrs, buffer, (u_int)inlen, XDR_DECODE);
12648360efbdSAlfred Perlstein 	if (!xdr_replymsg(&reply_xdrs, &reply_msg)) {
12658360efbdSAlfred Perlstein 		if (debugging)
12668360efbdSAlfred Perlstein 			(void) fprintf(stderr,
12678360efbdSAlfred Perlstein 				"handle_reply:  xdr_replymsg failed\n");
12688360efbdSAlfred Perlstein 		goto done;
12698360efbdSAlfred Perlstein 	}
12708360efbdSAlfred Perlstein 	fi = forward_find(reply_msg.rm_xid);
12718360efbdSAlfred Perlstein #ifdef	SVC_RUN_DEBUG
12728360efbdSAlfred Perlstein 	if (debugging) {
12738360efbdSAlfred Perlstein 		fprintf(stderr, "handle_reply:  reply xid: %d fi addr: %p\n",
12748360efbdSAlfred Perlstein 			reply_msg.rm_xid, fi);
12758360efbdSAlfred Perlstein 	}
12768360efbdSAlfred Perlstein #endif
12778360efbdSAlfred Perlstein 	if (fi == NULL) {
12788360efbdSAlfred Perlstein 		goto done;
12798360efbdSAlfred Perlstein 	}
12808360efbdSAlfred Perlstein 	_seterr_reply(&reply_msg, &reply_error);
12818360efbdSAlfred Perlstein 	if (reply_error.re_status != RPC_SUCCESS) {
12828360efbdSAlfred Perlstein 		if (debugging)
12838360efbdSAlfred Perlstein 			(void) fprintf(stderr, "handle_reply:  %s\n",
12848360efbdSAlfred Perlstein 				clnt_sperrno(reply_error.re_status));
12858360efbdSAlfred Perlstein 		send_svcsyserr(xprt, fi);
12868360efbdSAlfred Perlstein 		goto done;
12878360efbdSAlfred Perlstein 	}
12888360efbdSAlfred Perlstein 	pos = XDR_GETPOS(&reply_xdrs);
12898360efbdSAlfred Perlstein 	len = inlen - pos;
12908360efbdSAlfred Perlstein 	a.rmt_args.args = &buffer[pos];
12918360efbdSAlfred Perlstein 	a.rmt_args.arglen = len;
12928360efbdSAlfred Perlstein 	a.rmt_uaddr = fi->uaddr;
12938360efbdSAlfred Perlstein 	a.rmt_localvers = fi->versnum;
12948360efbdSAlfred Perlstein 
12958360efbdSAlfred Perlstein 	xprt_set_caller(xprt, fi);
12968360efbdSAlfred Perlstein #ifdef	SVC_RUN_DEBUG
12978360efbdSAlfred Perlstein 	uaddr =	taddr2uaddr(rpcbind_get_conf("udp"),
12988360efbdSAlfred Perlstein 				    svc_getrpccaller(xprt));
12998360efbdSAlfred Perlstein 	if (debugging) {
13008360efbdSAlfred Perlstein 		fprintf(stderr, "handle_reply:  forwarding address %s to %s\n",
13018360efbdSAlfred Perlstein 			a.rmt_uaddr, uaddr ? uaddr : "unknown");
13028360efbdSAlfred Perlstein 	}
130309d4f778SIan Dowse 	free(uaddr);
13048360efbdSAlfred Perlstein #endif
13058360efbdSAlfred Perlstein 	svc_sendreply(xprt, (xdrproc_t) xdr_rmtcall_result, (char *) &a);
13068360efbdSAlfred Perlstein done:
13078360efbdSAlfred Perlstein 	free(buffer);
13088360efbdSAlfred Perlstein 
13098360efbdSAlfred Perlstein 	if (reply_msg.rm_xid == 0) {
13108360efbdSAlfred Perlstein #ifdef	SVC_RUN_DEBUG
13118360efbdSAlfred Perlstein 	if (debugging) {
13128360efbdSAlfred Perlstein 		fprintf(stderr, "handle_reply:  NULL xid on exit!\n");
13138360efbdSAlfred Perlstein 	}
13148360efbdSAlfred Perlstein #endif
13158360efbdSAlfred Perlstein 	} else
13168360efbdSAlfred Perlstein 		(void) free_slot_by_xid(reply_msg.rm_xid);
13178360efbdSAlfred Perlstein }
13188360efbdSAlfred Perlstein 
13198360efbdSAlfred Perlstein static void
find_versions(rpcprog_t prog,char * netid,rpcvers_t * lowvp,rpcvers_t * highvp)13208360efbdSAlfred Perlstein find_versions(rpcprog_t prog, char *netid, rpcvers_t *lowvp, rpcvers_t *highvp)
13218360efbdSAlfred Perlstein {
13228360efbdSAlfred Perlstein 	register rpcblist_ptr rbl;
13234180788fSAlfred Perlstein 	unsigned int lowv = 0;
13244180788fSAlfred Perlstein 	unsigned int highv = 0;
13258360efbdSAlfred Perlstein 
13268360efbdSAlfred Perlstein 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
13278360efbdSAlfred Perlstein 		if ((rbl->rpcb_map.r_prog != prog) ||
13288360efbdSAlfred Perlstein 		    ((rbl->rpcb_map.r_netid != NULL) &&
13298360efbdSAlfred Perlstein 			(strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
13308360efbdSAlfred Perlstein 			continue;
13318360efbdSAlfred Perlstein 		if (lowv == 0) {
13328360efbdSAlfred Perlstein 			highv = rbl->rpcb_map.r_vers;
13338360efbdSAlfred Perlstein 			lowv = highv;
13348360efbdSAlfred Perlstein 		} else if (rbl->rpcb_map.r_vers < lowv) {
13358360efbdSAlfred Perlstein 			lowv = rbl->rpcb_map.r_vers;
13368360efbdSAlfred Perlstein 		} else if (rbl->rpcb_map.r_vers > highv) {
13378360efbdSAlfred Perlstein 			highv = rbl->rpcb_map.r_vers;
13388360efbdSAlfred Perlstein 		}
13398360efbdSAlfred Perlstein 	}
13408360efbdSAlfred Perlstein 	*lowvp = lowv;
13418360efbdSAlfred Perlstein 	*highvp = highv;
13428360efbdSAlfred Perlstein }
13438360efbdSAlfred Perlstein 
13448360efbdSAlfred Perlstein /*
13458360efbdSAlfred Perlstein  * returns the item with the given program, version number and netid.
13468360efbdSAlfred Perlstein  * If that version number is not found, it returns the item with that
13478360efbdSAlfred Perlstein  * program number, so that address is now returned to the caller. The
13488360efbdSAlfred Perlstein  * caller when makes a call to this program, version number, the call
13498360efbdSAlfred Perlstein  * will fail and it will return with PROGVERS_MISMATCH. The user can
13508360efbdSAlfred Perlstein  * then determine the highest and the lowest version number for this
13518360efbdSAlfred Perlstein  * program using clnt_geterr() and use those program version numbers.
13528360efbdSAlfred Perlstein  *
13538360efbdSAlfred Perlstein  * Returns the RPCBLIST for the given prog, vers and netid
13548360efbdSAlfred Perlstein  */
13558360efbdSAlfred Perlstein static rpcblist_ptr
find_service(rpcprog_t prog,rpcvers_t vers,char * netid)13568360efbdSAlfred Perlstein find_service(rpcprog_t prog, rpcvers_t vers, char *netid)
13578360efbdSAlfred Perlstein {
13588360efbdSAlfred Perlstein 	register rpcblist_ptr hit = NULL;
13598360efbdSAlfred Perlstein 	register rpcblist_ptr rbl;
13608360efbdSAlfred Perlstein 
13618360efbdSAlfred Perlstein 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
13628360efbdSAlfred Perlstein 		if ((rbl->rpcb_map.r_prog != prog) ||
13638360efbdSAlfred Perlstein 		    ((rbl->rpcb_map.r_netid != NULL) &&
13648360efbdSAlfred Perlstein 			(strcasecmp(rbl->rpcb_map.r_netid, netid) != 0)))
13658360efbdSAlfred Perlstein 			continue;
13668360efbdSAlfred Perlstein 		hit = rbl;
13678360efbdSAlfred Perlstein 		if (rbl->rpcb_map.r_vers == vers)
13688360efbdSAlfred Perlstein 			break;
13698360efbdSAlfred Perlstein 	}
13708360efbdSAlfred Perlstein 	return (hit);
13718360efbdSAlfred Perlstein }
13728360efbdSAlfred Perlstein 
13738360efbdSAlfred Perlstein /*
13748360efbdSAlfred Perlstein  * Copies the name associated with the uid of the caller and returns
13758360efbdSAlfred Perlstein  * a pointer to it.  Similar to getwd().
13768360efbdSAlfred Perlstein  */
13778360efbdSAlfred Perlstein static char *
getowner(SVCXPRT * transp,char * owner,size_t ownersize)13788360efbdSAlfred Perlstein getowner(SVCXPRT *transp, char *owner, size_t ownersize)
13798360efbdSAlfred Perlstein {
13804ed6d634SAlfred Perlstein 	uid_t uid;
13818360efbdSAlfred Perlstein 
13824ed6d634SAlfred Perlstein 	if (__rpc_get_local_uid(transp, &uid) < 0)
13838360efbdSAlfred Perlstein                 strlcpy(owner, "unknown", ownersize);
13844ed6d634SAlfred Perlstein 	else if (uid == 0)
13858360efbdSAlfred Perlstein 		strlcpy(owner, "superuser", ownersize);
13868360efbdSAlfred Perlstein 	else
13874ed6d634SAlfred Perlstein 		snprintf(owner, ownersize, "%d", uid);
13888360efbdSAlfred Perlstein 
13898360efbdSAlfred Perlstein 	return owner;
13908360efbdSAlfred Perlstein }
13918360efbdSAlfred Perlstein 
13928360efbdSAlfred Perlstein #ifdef PORTMAP
13938360efbdSAlfred Perlstein /*
13948360efbdSAlfred Perlstein  * Add this to the pmap list only if it is UDP or TCP.
13958360efbdSAlfred Perlstein  */
13968360efbdSAlfred Perlstein static int
add_pmaplist(RPCB * arg)13978360efbdSAlfred Perlstein add_pmaplist(RPCB *arg)
13988360efbdSAlfred Perlstein {
13998360efbdSAlfred Perlstein 	struct pmap pmap;
14008360efbdSAlfred Perlstein 	struct pmaplist *pml;
14018360efbdSAlfred Perlstein 	int h1, h2, h3, h4, p1, p2;
14028360efbdSAlfred Perlstein 
14038360efbdSAlfred Perlstein 	if (strcmp(arg->r_netid, udptrans) == 0) {
14048360efbdSAlfred Perlstein 		/* It is UDP! */
14058360efbdSAlfred Perlstein 		pmap.pm_prot = IPPROTO_UDP;
14068360efbdSAlfred Perlstein 	} else if (strcmp(arg->r_netid, tcptrans) == 0) {
14078360efbdSAlfred Perlstein 		/* It is TCP */
14088360efbdSAlfred Perlstein 		pmap.pm_prot = IPPROTO_TCP;
14098360efbdSAlfred Perlstein 	} else
14109d5abbddSJens Schweikhardt 		/* Not an IP protocol */
14118360efbdSAlfred Perlstein 		return (0);
14128360efbdSAlfred Perlstein 
14138360efbdSAlfred Perlstein 	/* interpret the universal address for TCP/IP */
14148360efbdSAlfred Perlstein 	if (sscanf(arg->r_addr, "%d.%d.%d.%d.%d.%d",
14158360efbdSAlfred Perlstein 		&h1, &h2, &h3, &h4, &p1, &p2) != 6)
14168360efbdSAlfred Perlstein 		return (0);
14178360efbdSAlfred Perlstein 	pmap.pm_port = ((p1 & 0xff) << 8) + (p2 & 0xff);
14188360efbdSAlfred Perlstein 	pmap.pm_prog = arg->r_prog;
14198360efbdSAlfred Perlstein 	pmap.pm_vers = arg->r_vers;
14208360efbdSAlfred Perlstein 	/*
14218360efbdSAlfred Perlstein 	 * add to END of list
14228360efbdSAlfred Perlstein 	 */
14232244ec01SJuli Mallett 	pml = malloc(sizeof (struct pmaplist));
14248360efbdSAlfred Perlstein 	if (pml == NULL) {
14258360efbdSAlfred Perlstein 		(void) syslog(LOG_ERR, "rpcbind: no memory!\n");
14268360efbdSAlfred Perlstein 		return (1);
14278360efbdSAlfred Perlstein 	}
14288360efbdSAlfred Perlstein 	pml->pml_map = pmap;
14298360efbdSAlfred Perlstein 	pml->pml_next = NULL;
14308360efbdSAlfred Perlstein 	if (list_pml == NULL) {
14318360efbdSAlfred Perlstein 		list_pml = pml;
14328360efbdSAlfred Perlstein 	} else {
14338360efbdSAlfred Perlstein 		struct pmaplist *fnd;
14348360efbdSAlfred Perlstein 
14358360efbdSAlfred Perlstein 		/* Attach to the end of the list */
14368360efbdSAlfred Perlstein 		for (fnd = list_pml; fnd->pml_next; fnd = fnd->pml_next)
14378360efbdSAlfred Perlstein 			;
14388360efbdSAlfred Perlstein 		fnd->pml_next = pml;
14398360efbdSAlfred Perlstein 	}
14408360efbdSAlfred Perlstein 	return (0);
14418360efbdSAlfred Perlstein }
14428360efbdSAlfred Perlstein 
14438360efbdSAlfred Perlstein /*
14448360efbdSAlfred Perlstein  * Delete this from the pmap list only if it is UDP or TCP.
14458360efbdSAlfred Perlstein  */
14468360efbdSAlfred Perlstein static int
del_pmaplist(RPCB * arg)14478360efbdSAlfred Perlstein del_pmaplist(RPCB *arg)
14488360efbdSAlfred Perlstein {
14498360efbdSAlfred Perlstein 	struct pmaplist *pml;
14508360efbdSAlfred Perlstein 	struct pmaplist *prevpml, *fnd;
14514180788fSAlfred Perlstein 	unsigned long prot;
14528360efbdSAlfred Perlstein 
14538360efbdSAlfred Perlstein 	if (strcmp(arg->r_netid, udptrans) == 0) {
14548360efbdSAlfred Perlstein 		/* It is UDP! */
14558360efbdSAlfred Perlstein 		prot = IPPROTO_UDP;
14568360efbdSAlfred Perlstein 	} else if (strcmp(arg->r_netid, tcptrans) == 0) {
14578360efbdSAlfred Perlstein 		/* It is TCP */
14588360efbdSAlfred Perlstein 		prot = IPPROTO_TCP;
1459545fbf37SMartin Blapp 	} else if (arg->r_netid[0] == 0) {
14608360efbdSAlfred Perlstein 		prot = 0;	/* Remove all occurrences */
14618360efbdSAlfred Perlstein 	} else {
14629d5abbddSJens Schweikhardt 		/* Not an IP protocol */
14638360efbdSAlfred Perlstein 		return (0);
14648360efbdSAlfred Perlstein 	}
14658360efbdSAlfred Perlstein 	for (prevpml = NULL, pml = list_pml; pml; /* cstyle */) {
14668360efbdSAlfred Perlstein 		if ((pml->pml_map.pm_prog != arg->r_prog) ||
14678360efbdSAlfred Perlstein 			(pml->pml_map.pm_vers != arg->r_vers) ||
14688360efbdSAlfred Perlstein 			(prot && (pml->pml_map.pm_prot != prot))) {
14698360efbdSAlfred Perlstein 			/* both pml & prevpml move forwards */
14708360efbdSAlfred Perlstein 			prevpml = pml;
14718360efbdSAlfred Perlstein 			pml = pml->pml_next;
14728360efbdSAlfred Perlstein 			continue;
14738360efbdSAlfred Perlstein 		}
14748360efbdSAlfred Perlstein 		/* found it; pml moves forward, prevpml stays */
14758360efbdSAlfred Perlstein 		fnd = pml;
14768360efbdSAlfred Perlstein 		pml = pml->pml_next;
14778360efbdSAlfred Perlstein 		if (prevpml == NULL)
14788360efbdSAlfred Perlstein 			list_pml = pml;
14798360efbdSAlfred Perlstein 		else
14808360efbdSAlfred Perlstein 			prevpml->pml_next = pml;
148109d4f778SIan Dowse 		free(fnd);
14828360efbdSAlfred Perlstein 	}
14838360efbdSAlfred Perlstein 	return (0);
14848360efbdSAlfred Perlstein }
14858360efbdSAlfred Perlstein #endif /* PORTMAP */
1486