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