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