xref: /dflybsd-src/usr.sbin/rpcbind/rpcb_svc_4.c (revision bd91f5c31d05935d34ffa5befc63a95350bbf000)
1ce0e08e2SPeter Avalos /*
2*bd91f5c3SJustin C. Sherrill  * Copyright (c) 2009, Sun Microsystems, Inc.
3*bd91f5c3SJustin C. Sherrill  * All rights reserved.
4ce0e08e2SPeter Avalos  *
5*bd91f5c3SJustin C. Sherrill  * Redistribution and use in source and binary forms, with or without
6*bd91f5c3SJustin C. Sherrill  * modification, are permitted provided that the following conditions are met:
7*bd91f5c3SJustin C. Sherrill  * - Redistributions of source code must retain the above copyright notice,
8*bd91f5c3SJustin C. Sherrill  *   this list of conditions and the following disclaimer.
9*bd91f5c3SJustin C. Sherrill  * - Redistributions in binary form must reproduce the above copyright notice,
10*bd91f5c3SJustin C. Sherrill  *   this list of conditions and the following disclaimer in the documentation
11*bd91f5c3SJustin C. Sherrill  *   and/or other materials provided with the distribution.
12*bd91f5c3SJustin C. Sherrill  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*bd91f5c3SJustin C. Sherrill  *   contributors may be used to endorse or promote products derived
14*bd91f5c3SJustin C. Sherrill  *   from this software without specific prior written permission.
15ce0e08e2SPeter Avalos  *
16*bd91f5c3SJustin C. Sherrill  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*bd91f5c3SJustin C. Sherrill  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*bd91f5c3SJustin C. Sherrill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*bd91f5c3SJustin C. Sherrill  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*bd91f5c3SJustin C. Sherrill  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*bd91f5c3SJustin C. Sherrill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*bd91f5c3SJustin C. Sherrill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*bd91f5c3SJustin C. Sherrill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*bd91f5c3SJustin C. Sherrill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*bd91f5c3SJustin C. Sherrill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*bd91f5c3SJustin C. Sherrill  * POSSIBILITY OF SUCH DAMAGE.
27ce0e08e2SPeter Avalos  *
28ce0e08e2SPeter Avalos  * @(#)rpcb_svc_4.c	1.8	93/07/05 SMI
29ce0e08e2SPeter Avalos  * $NetBSD: rpcb_svc_4.c,v 1.1 2000/06/02 23:15:41 fvdl Exp $
30ce0e08e2SPeter Avalos  * $FreeBSD: src/usr.sbin/rpcbind/rpcb_svc_4.c,v 1.5 2007/11/07 10:53:39 kevlo Exp $
31ce0e08e2SPeter Avalos  */
32ce0e08e2SPeter Avalos /*
33ce0e08e2SPeter Avalos  * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
34ce0e08e2SPeter Avalos  */
35ce0e08e2SPeter Avalos 
36ce0e08e2SPeter Avalos /*
37ce0e08e2SPeter Avalos  * rpcb_svc_4.c
38ce0e08e2SPeter Avalos  * The server procedure for the version 4 rpcbind.
39ce0e08e2SPeter Avalos  *
40ce0e08e2SPeter Avalos  */
41ce0e08e2SPeter Avalos 
42ce0e08e2SPeter Avalos #include <sys/types.h>
43ce0e08e2SPeter Avalos #include <sys/stat.h>
44ce0e08e2SPeter Avalos #include <rpc/rpc.h>
45ce0e08e2SPeter Avalos #include <stdio.h>
46ce0e08e2SPeter Avalos #include <unistd.h>
47ce0e08e2SPeter Avalos #include <netconfig.h>
48ce0e08e2SPeter Avalos #include <syslog.h>
49ce0e08e2SPeter Avalos #include <string.h>
50ce0e08e2SPeter Avalos #include <stdlib.h>
51ce0e08e2SPeter Avalos #include "rpcbind.h"
52ce0e08e2SPeter Avalos 
53ce0e08e2SPeter Avalos static void	*rpcbproc_getaddr_4_local(void *, struct svc_req *, SVCXPRT *,
54ce0e08e2SPeter Avalos 					  rpcvers_t);
55ce0e08e2SPeter Avalos static void	*rpcbproc_getversaddr_4_local(void *, struct svc_req *,
56ce0e08e2SPeter Avalos 					      SVCXPRT *, rpcvers_t);
57ce0e08e2SPeter Avalos static void	*rpcbproc_getaddrlist_4_local(void *, struct svc_req *,
58ce0e08e2SPeter Avalos 					      SVCXPRT *, rpcvers_t);
59ce0e08e2SPeter Avalos static void	free_rpcb_entry_list(rpcb_entry_list_ptr *);
60ce0e08e2SPeter Avalos static void	*rpcbproc_dump_4_local(void *, struct svc_req *, SVCXPRT *,
61ce0e08e2SPeter Avalos 				       rpcvers_t);
62ce0e08e2SPeter Avalos 
63ce0e08e2SPeter Avalos /*
64ce0e08e2SPeter Avalos  * Called by svc_getreqset. There is a separate server handle for
65ce0e08e2SPeter Avalos  * every transport that it waits on.
66ce0e08e2SPeter Avalos  */
67ce0e08e2SPeter Avalos void
rpcb_service_4(struct svc_req * rqstp,SVCXPRT * transp)68ce0e08e2SPeter Avalos rpcb_service_4(struct svc_req *rqstp, SVCXPRT *transp)
69ce0e08e2SPeter Avalos {
70ce0e08e2SPeter Avalos 	union {
71ce0e08e2SPeter Avalos 		rpcb rpcbproc_set_4_arg;
72ce0e08e2SPeter Avalos 		rpcb rpcbproc_unset_4_arg;
73ce0e08e2SPeter Avalos 		rpcb rpcbproc_getaddr_4_local_arg;
74ce0e08e2SPeter Avalos 		char *rpcbproc_uaddr2taddr_4_arg;
75ce0e08e2SPeter Avalos 		struct netbuf rpcbproc_taddr2uaddr_4_arg;
76ce0e08e2SPeter Avalos 	} argument;
77ce0e08e2SPeter Avalos 	char *result;
78ce0e08e2SPeter Avalos 	xdrproc_t xdr_argument, xdr_result;
79ce0e08e2SPeter Avalos 	void *(*local)(void *, struct svc_req *, SVCXPRT *, rpcvers_t);
80ce0e08e2SPeter Avalos 
81ce0e08e2SPeter Avalos 	rpcbs_procinfo(RPCBVERS_4_STAT, rqstp->rq_proc);
82ce0e08e2SPeter Avalos 
83ce0e08e2SPeter Avalos 	switch (rqstp->rq_proc) {
84ce0e08e2SPeter Avalos 	case NULLPROC:
85ce0e08e2SPeter Avalos 		/*
86ce0e08e2SPeter Avalos 		 * Null proc call
87ce0e08e2SPeter Avalos 		 */
88ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
89ce0e08e2SPeter Avalos 		if (debugging)
90ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_NULL\n");
91ce0e08e2SPeter Avalos #endif
92ce0e08e2SPeter Avalos 		check_access(transp, rqstp->rq_proc, NULL, RPCBVERS4);
93ce0e08e2SPeter Avalos 		svc_sendreply(transp, (xdrproc_t) xdr_void, NULL);
94ce0e08e2SPeter Avalos 		return;
95ce0e08e2SPeter Avalos 
96ce0e08e2SPeter Avalos 	case RPCBPROC_SET:
97ce0e08e2SPeter Avalos 		/*
98ce0e08e2SPeter Avalos 		 * Check to see whether the message came from
99ce0e08e2SPeter Avalos 		 * loopback transports (for security reasons)
100ce0e08e2SPeter Avalos 		 */
101ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_rpcb;
102ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_bool;
103ce0e08e2SPeter Avalos 		local = rpcbproc_set_com;
104ce0e08e2SPeter Avalos 		break;
105ce0e08e2SPeter Avalos 
106ce0e08e2SPeter Avalos 	case RPCBPROC_UNSET:
107ce0e08e2SPeter Avalos 		/*
108ce0e08e2SPeter Avalos 		 * Check to see whether the message came from
109ce0e08e2SPeter Avalos 		 * loopback transports (for security reasons)
110ce0e08e2SPeter Avalos 		 */
111ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_rpcb;
112ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_bool;
113ce0e08e2SPeter Avalos 		local = rpcbproc_unset_com;
114ce0e08e2SPeter Avalos 		break;
115ce0e08e2SPeter Avalos 
116ce0e08e2SPeter Avalos 	case RPCBPROC_GETADDR:
117ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_rpcb;
118ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_wrapstring;
119ce0e08e2SPeter Avalos 		local = rpcbproc_getaddr_4_local;
120ce0e08e2SPeter Avalos 		break;
121ce0e08e2SPeter Avalos 
122ce0e08e2SPeter Avalos 	case RPCBPROC_GETVERSADDR:
123ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
124ce0e08e2SPeter Avalos 		if (debugging)
125ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_GETVERSADDR\n");
126ce0e08e2SPeter Avalos #endif
127ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_rpcb;
128ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_wrapstring;
129ce0e08e2SPeter Avalos 		local = rpcbproc_getversaddr_4_local;
130ce0e08e2SPeter Avalos 		break;
131ce0e08e2SPeter Avalos 
132ce0e08e2SPeter Avalos 	case RPCBPROC_DUMP:
133ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
134ce0e08e2SPeter Avalos 		if (debugging)
135ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_DUMP\n");
136ce0e08e2SPeter Avalos #endif
137ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_void;
138ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_rpcblist_ptr;
139ce0e08e2SPeter Avalos 		local = rpcbproc_dump_4_local;
140ce0e08e2SPeter Avalos 		break;
141ce0e08e2SPeter Avalos 
142ce0e08e2SPeter Avalos 	case RPCBPROC_INDIRECT:
143ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
144ce0e08e2SPeter Avalos 		if (debugging)
145ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_INDIRECT\n");
146ce0e08e2SPeter Avalos #endif
147ce0e08e2SPeter Avalos 		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
148ce0e08e2SPeter Avalos 		return;
149ce0e08e2SPeter Avalos 
150ce0e08e2SPeter Avalos /*	case RPCBPROC_CALLIT: */
151ce0e08e2SPeter Avalos 	case RPCBPROC_BCAST:
152ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
153ce0e08e2SPeter Avalos 		if (debugging)
154ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_BCAST\n");
155ce0e08e2SPeter Avalos #endif
156ce0e08e2SPeter Avalos 		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
157ce0e08e2SPeter Avalos 		return;
158ce0e08e2SPeter Avalos 
159ce0e08e2SPeter Avalos 	case RPCBPROC_GETTIME:
160ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
161ce0e08e2SPeter Avalos 		if (debugging)
162ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_GETTIME\n");
163ce0e08e2SPeter Avalos #endif
164ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_void;
165ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_u_long;
166ce0e08e2SPeter Avalos 		local = rpcbproc_gettime_com;
167ce0e08e2SPeter Avalos 		break;
168ce0e08e2SPeter Avalos 
169ce0e08e2SPeter Avalos 	case RPCBPROC_UADDR2TADDR:
170ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
171ce0e08e2SPeter Avalos 		if (debugging)
172ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_UADDR2TADDR\n");
173ce0e08e2SPeter Avalos #endif
174ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_wrapstring;
175ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_netbuf;
176ce0e08e2SPeter Avalos 		local = rpcbproc_uaddr2taddr_com;
177ce0e08e2SPeter Avalos 		break;
178ce0e08e2SPeter Avalos 
179ce0e08e2SPeter Avalos 	case RPCBPROC_TADDR2UADDR:
180ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
181ce0e08e2SPeter Avalos 		if (debugging)
182ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_TADDR2UADDR\n");
183ce0e08e2SPeter Avalos #endif
184ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_netbuf;
185ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_wrapstring;
186ce0e08e2SPeter Avalos 		local = rpcbproc_taddr2uaddr_com;
187ce0e08e2SPeter Avalos 		break;
188ce0e08e2SPeter Avalos 
189ce0e08e2SPeter Avalos 	case RPCBPROC_GETADDRLIST:
190ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
191ce0e08e2SPeter Avalos 		if (debugging)
192ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_GETADDRLIST\n");
193ce0e08e2SPeter Avalos #endif
194ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_rpcb;
195ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_rpcb_entry_list_ptr;
196ce0e08e2SPeter Avalos 		local = rpcbproc_getaddrlist_4_local;
197ce0e08e2SPeter Avalos 		break;
198ce0e08e2SPeter Avalos 
199ce0e08e2SPeter Avalos 	case RPCBPROC_GETSTAT:
200ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
201ce0e08e2SPeter Avalos 		if (debugging)
202ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_GETSTAT\n");
203ce0e08e2SPeter Avalos #endif
204ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_void;
205ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_rpcb_stat_byvers;
206ce0e08e2SPeter Avalos 		local = rpcbproc_getstat;
207ce0e08e2SPeter Avalos 		break;
208ce0e08e2SPeter Avalos 
209ce0e08e2SPeter Avalos 	default:
210ce0e08e2SPeter Avalos 		svcerr_noproc(transp);
211ce0e08e2SPeter Avalos 		return;
212ce0e08e2SPeter Avalos 	}
213ce0e08e2SPeter Avalos 	memset((char *)&argument, 0, sizeof (argument));
214ce0e08e2SPeter Avalos 	if (!svc_getargs(transp, (xdrproc_t) xdr_argument,
215ce0e08e2SPeter Avalos 		(char *)&argument)) {
216ce0e08e2SPeter Avalos 		svcerr_decode(transp);
217ce0e08e2SPeter Avalos 		if (debugging)
218ce0e08e2SPeter Avalos 			fprintf(stderr, "rpcbind: could not decode\n");
219ce0e08e2SPeter Avalos 		return;
220ce0e08e2SPeter Avalos 	}
221ce0e08e2SPeter Avalos 	if (!check_access(transp, rqstp->rq_proc, &argument, RPCBVERS4)) {
222ce0e08e2SPeter Avalos 		svcerr_weakauth(transp);
223ce0e08e2SPeter Avalos 		goto done;
224ce0e08e2SPeter Avalos 	}
225ce0e08e2SPeter Avalos 	result = (*local)(&argument, rqstp, transp, RPCBVERS4);
226ce0e08e2SPeter Avalos 	if (result != NULL && !svc_sendreply(transp, (xdrproc_t) xdr_result,
227ce0e08e2SPeter Avalos 						result)) {
228ce0e08e2SPeter Avalos 		svcerr_systemerr(transp);
229ce0e08e2SPeter Avalos 		if (debugging) {
230ce0e08e2SPeter Avalos 			fprintf(stderr, "rpcbind: svc_sendreply\n");
231ce0e08e2SPeter Avalos 			if (doabort) {
232ce0e08e2SPeter Avalos 				rpcbind_abort();
233ce0e08e2SPeter Avalos 			}
234ce0e08e2SPeter Avalos 		}
235ce0e08e2SPeter Avalos 	}
236ce0e08e2SPeter Avalos done:
237ce0e08e2SPeter Avalos 	if (!svc_freeargs(transp, (xdrproc_t) xdr_argument,
238ce0e08e2SPeter Avalos 				(char *)&argument)) {
239ce0e08e2SPeter Avalos 		if (debugging) {
240ce0e08e2SPeter Avalos 			fprintf(stderr, "unable to free arguments\n");
241ce0e08e2SPeter Avalos 			if (doabort) {
242ce0e08e2SPeter Avalos 				rpcbind_abort();
243ce0e08e2SPeter Avalos 			}
244ce0e08e2SPeter Avalos 		}
245ce0e08e2SPeter Avalos 	}
246ce0e08e2SPeter Avalos 	return;
247ce0e08e2SPeter Avalos }
248ce0e08e2SPeter Avalos 
249ce0e08e2SPeter Avalos /*
250ce0e08e2SPeter Avalos  * Lookup the mapping for a program, version and return its
251ce0e08e2SPeter Avalos  * address. Assuming that the caller wants the address of the
252ce0e08e2SPeter Avalos  * server running on the transport on which the request came.
253ce0e08e2SPeter Avalos  * Even if a service with a different version number is available,
254ce0e08e2SPeter Avalos  * it will return that address.  The client should check with an
255ce0e08e2SPeter Avalos  * clnt_call to verify whether the service is the one that is desired.
256ce0e08e2SPeter Avalos  * We also try to resolve the universal address in terms of
257ce0e08e2SPeter Avalos  * address of the caller.
258ce0e08e2SPeter Avalos  */
259ce0e08e2SPeter Avalos /* ARGSUSED */
260ce0e08e2SPeter Avalos static void *
rpcbproc_getaddr_4_local(void * arg,struct svc_req * rqstp,SVCXPRT * transp,rpcvers_t rpcbversnum __unused)261ce0e08e2SPeter Avalos rpcbproc_getaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
262ce0e08e2SPeter Avalos 			 rpcvers_t rpcbversnum __unused)
263ce0e08e2SPeter Avalos {
264ce0e08e2SPeter Avalos 	RPCB *regp = (RPCB *)arg;
265ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
266ce0e08e2SPeter Avalos 	if (debugging) {
267ce0e08e2SPeter Avalos 		char *uaddr;
268ce0e08e2SPeter Avalos 
269ce0e08e2SPeter Avalos 		uaddr =	taddr2uaddr(rpcbind_get_conf(transp->xp_netid),
270ce0e08e2SPeter Avalos 			    svc_getrpccaller(transp));
271ce0e08e2SPeter Avalos 		fprintf(stderr, "RPCB_GETADDR req for (%lu, %lu, %s) from %s: ",
272ce0e08e2SPeter Avalos 		    (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
273ce0e08e2SPeter Avalos 		    regp->r_netid, uaddr);
274ce0e08e2SPeter Avalos 		free(uaddr);
275ce0e08e2SPeter Avalos 	}
276ce0e08e2SPeter Avalos #endif
277ce0e08e2SPeter Avalos 	return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4,
278ce0e08e2SPeter Avalos 					RPCB_ALLVERS));
279ce0e08e2SPeter Avalos }
280ce0e08e2SPeter Avalos 
281ce0e08e2SPeter Avalos /*
282ce0e08e2SPeter Avalos  * Lookup the mapping for a program, version and return its
283ce0e08e2SPeter Avalos  * address. Assuming that the caller wants the address of the
284ce0e08e2SPeter Avalos  * server running on the transport on which the request came.
285ce0e08e2SPeter Avalos  *
286ce0e08e2SPeter Avalos  * We also try to resolve the universal address in terms of
287ce0e08e2SPeter Avalos  * address of the caller.
288ce0e08e2SPeter Avalos  */
289ce0e08e2SPeter Avalos /* ARGSUSED */
290ce0e08e2SPeter Avalos static void *
rpcbproc_getversaddr_4_local(void * arg,struct svc_req * rqstp,SVCXPRT * transp,rpcvers_t versnum __unused)291ce0e08e2SPeter Avalos rpcbproc_getversaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
292ce0e08e2SPeter Avalos 			     rpcvers_t versnum __unused)
293ce0e08e2SPeter Avalos {
294ce0e08e2SPeter Avalos 	RPCB *regp = (RPCB *)arg;
295ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
296ce0e08e2SPeter Avalos 	if (debugging) {
297ce0e08e2SPeter Avalos 		char *uaddr;
298ce0e08e2SPeter Avalos 
299ce0e08e2SPeter Avalos 		uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid),
300ce0e08e2SPeter Avalos 			    svc_getrpccaller(transp));
301ce0e08e2SPeter Avalos 		fprintf(stderr, "RPCB_GETVERSADDR rqst for (%lu, %lu, %s)"
302ce0e08e2SPeter Avalos 				" from %s : ",
303ce0e08e2SPeter Avalos 		    (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
304ce0e08e2SPeter Avalos 		    regp->r_netid, uaddr);
305ce0e08e2SPeter Avalos 		free(uaddr);
306ce0e08e2SPeter Avalos 	}
307ce0e08e2SPeter Avalos #endif
308ce0e08e2SPeter Avalos 	return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4,
309ce0e08e2SPeter Avalos 					RPCB_ONEVERS));
310ce0e08e2SPeter Avalos }
311ce0e08e2SPeter Avalos 
312ce0e08e2SPeter Avalos /*
313ce0e08e2SPeter Avalos  * Lookup the mapping for a program, version and return the
314ce0e08e2SPeter Avalos  * addresses for all transports in the current transport family.
315ce0e08e2SPeter Avalos  * We return a merged address.
316ce0e08e2SPeter Avalos  */
317ce0e08e2SPeter Avalos /* ARGSUSED */
318ce0e08e2SPeter Avalos static void *
rpcbproc_getaddrlist_4_local(void * arg,struct svc_req * rqstp __unused,SVCXPRT * transp,rpcvers_t versnum __unused)319ce0e08e2SPeter Avalos rpcbproc_getaddrlist_4_local(void *arg, struct svc_req *rqstp __unused,
320ce0e08e2SPeter Avalos 			     SVCXPRT *transp, rpcvers_t versnum __unused)
321ce0e08e2SPeter Avalos {
322ce0e08e2SPeter Avalos 	RPCB *regp = (RPCB *)arg;
323ce0e08e2SPeter Avalos 	static rpcb_entry_list_ptr rlist;
324ce0e08e2SPeter Avalos 	rpcblist_ptr rbl;
325ce0e08e2SPeter Avalos 	rpcb_entry_list_ptr rp, tail;
326ce0e08e2SPeter Avalos 	rpcprog_t prog;
327ce0e08e2SPeter Avalos 	rpcvers_t vers;
328ce0e08e2SPeter Avalos 	rpcb_entry *a;
329ce0e08e2SPeter Avalos 	struct netconfig *nconf;
330ce0e08e2SPeter Avalos 	struct netconfig *reg_nconf;
331ce0e08e2SPeter Avalos 	char *saddr, *maddr = NULL;
332ce0e08e2SPeter Avalos 
333ce0e08e2SPeter Avalos 	free_rpcb_entry_list(&rlist);
334ce0e08e2SPeter Avalos 	tail = NULL;
335ce0e08e2SPeter Avalos 	prog = regp->r_prog;
336ce0e08e2SPeter Avalos 	vers = regp->r_vers;
337ce0e08e2SPeter Avalos 	reg_nconf = rpcbind_get_conf(transp->xp_netid);
338ce0e08e2SPeter Avalos 	if (reg_nconf == NULL)
339ce0e08e2SPeter Avalos 		return (NULL);
340ce0e08e2SPeter Avalos 	if (*(regp->r_addr) != '\0') {
341ce0e08e2SPeter Avalos 		saddr = regp->r_addr;
342ce0e08e2SPeter Avalos 	} else {
343ce0e08e2SPeter Avalos 		saddr = NULL;
344ce0e08e2SPeter Avalos 	}
345ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
346ce0e08e2SPeter Avalos 	if (debugging) {
347ce0e08e2SPeter Avalos 		fprintf(stderr, "r_addr: %s r_netid: %s nc_protofmly: %s\n",
348ce0e08e2SPeter Avalos 		    regp->r_addr, regp->r_netid, reg_nconf->nc_protofmly);
349ce0e08e2SPeter Avalos 	}
350ce0e08e2SPeter Avalos #endif
351ce0e08e2SPeter Avalos 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
352ce0e08e2SPeter Avalos 	    if ((rbl->rpcb_map.r_prog == prog) &&
353ce0e08e2SPeter Avalos 		(rbl->rpcb_map.r_vers == vers)) {
354ce0e08e2SPeter Avalos 		nconf = rpcbind_get_conf(rbl->rpcb_map.r_netid);
355ce0e08e2SPeter Avalos 		if (nconf == NULL)
356ce0e08e2SPeter Avalos 			goto fail;
357ce0e08e2SPeter Avalos 		if (strcmp(nconf->nc_protofmly, reg_nconf->nc_protofmly)
358ce0e08e2SPeter Avalos 				!= 0) {
359ce0e08e2SPeter Avalos 			continue;	/* not same proto family */
360ce0e08e2SPeter Avalos 		}
361ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
362ce0e08e2SPeter Avalos 		if (debugging)
363ce0e08e2SPeter Avalos 			fprintf(stderr, "\tmerge with: %s\n",
364ce0e08e2SPeter Avalos 			    rbl->rpcb_map.r_addr);
365ce0e08e2SPeter Avalos #endif
366ce0e08e2SPeter Avalos 		if ((maddr = mergeaddr(transp, rbl->rpcb_map.r_netid,
367ce0e08e2SPeter Avalos 				rbl->rpcb_map.r_addr, saddr)) == NULL) {
368ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
369ce0e08e2SPeter Avalos 		if (debugging)
370ce0e08e2SPeter Avalos 			fprintf(stderr, " FAILED\n");
371ce0e08e2SPeter Avalos #endif
372ce0e08e2SPeter Avalos 			continue;
373ce0e08e2SPeter Avalos 		} else if (!maddr[0]) {
374ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
375ce0e08e2SPeter Avalos 	if (debugging)
376ce0e08e2SPeter Avalos 		fprintf(stderr, " SUCCEEDED, but port died -  maddr: nullstring\n");
377ce0e08e2SPeter Avalos #endif
378ce0e08e2SPeter Avalos 			/* The server died. Unset this combination */
379ce0e08e2SPeter Avalos 			delete_prog(regp->r_prog);
380ce0e08e2SPeter Avalos 			continue;
381ce0e08e2SPeter Avalos 		}
382ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
383ce0e08e2SPeter Avalos 		if (debugging)
384ce0e08e2SPeter Avalos 			fprintf(stderr, " SUCCEEDED maddr: %s\n", maddr);
385ce0e08e2SPeter Avalos #endif
386ce0e08e2SPeter Avalos 		/*
387ce0e08e2SPeter Avalos 		 * Add it to rlist.
388ce0e08e2SPeter Avalos 		 */
389ce0e08e2SPeter Avalos 		rp = malloc(sizeof (rpcb_entry_list));
390ce0e08e2SPeter Avalos 		if (rp == NULL)
391ce0e08e2SPeter Avalos 			goto fail;
392ce0e08e2SPeter Avalos 		a = &rp->rpcb_entry_map;
393ce0e08e2SPeter Avalos 		a->r_maddr = maddr;
394ce0e08e2SPeter Avalos 		a->r_nc_netid = nconf->nc_netid;
395ce0e08e2SPeter Avalos 		a->r_nc_semantics = nconf->nc_semantics;
396ce0e08e2SPeter Avalos 		a->r_nc_protofmly = nconf->nc_protofmly;
397ce0e08e2SPeter Avalos 		a->r_nc_proto = nconf->nc_proto;
398ce0e08e2SPeter Avalos 		rp->rpcb_entry_next = NULL;
399ce0e08e2SPeter Avalos 		if (rlist == NULL) {
400ce0e08e2SPeter Avalos 			rlist = rp;
401ce0e08e2SPeter Avalos 			tail = rp;
402ce0e08e2SPeter Avalos 		} else {
403ce0e08e2SPeter Avalos 			tail->rpcb_entry_next = rp;
404ce0e08e2SPeter Avalos 			tail = rp;
405ce0e08e2SPeter Avalos 		}
406ce0e08e2SPeter Avalos 		rp = NULL;
407ce0e08e2SPeter Avalos 	    }
408ce0e08e2SPeter Avalos 	}
409ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
410ce0e08e2SPeter Avalos 	if (debugging) {
411ce0e08e2SPeter Avalos 		for (rp = rlist; rp; rp = rp->rpcb_entry_next) {
412ce0e08e2SPeter Avalos 			fprintf(stderr, "\t%s %s\n", rp->rpcb_entry_map.r_maddr,
413ce0e08e2SPeter Avalos 				rp->rpcb_entry_map.r_nc_proto);
414ce0e08e2SPeter Avalos 		}
415ce0e08e2SPeter Avalos 	}
416ce0e08e2SPeter Avalos #endif
417ce0e08e2SPeter Avalos 	/*
418ce0e08e2SPeter Avalos 	 * XXX: getaddrlist info is also being stuffed into getaddr.
419ce0e08e2SPeter Avalos 	 * Perhaps wrong, but better than it not getting counted at all.
420ce0e08e2SPeter Avalos 	 */
421ce0e08e2SPeter Avalos 	rpcbs_getaddr(RPCBVERS4 - 2, prog, vers, transp->xp_netid, maddr);
422ce0e08e2SPeter Avalos 	return (void *)&rlist;
423ce0e08e2SPeter Avalos 
424ce0e08e2SPeter Avalos fail:	free_rpcb_entry_list(&rlist);
425ce0e08e2SPeter Avalos 	return (NULL);
426ce0e08e2SPeter Avalos }
427ce0e08e2SPeter Avalos 
428ce0e08e2SPeter Avalos /*
429ce0e08e2SPeter Avalos  * Free only the allocated structure, rest is all a pointer to some
430ce0e08e2SPeter Avalos  * other data somewhere else.
431ce0e08e2SPeter Avalos  */
432ce0e08e2SPeter Avalos static void
free_rpcb_entry_list(rpcb_entry_list_ptr * rlistp)433ce0e08e2SPeter Avalos free_rpcb_entry_list(rpcb_entry_list_ptr *rlistp)
434ce0e08e2SPeter Avalos {
435ce0e08e2SPeter Avalos 	rpcb_entry_list_ptr rbl, tmp;
436ce0e08e2SPeter Avalos 
437ce0e08e2SPeter Avalos 	for (rbl = *rlistp; rbl != NULL; ) {
438ce0e08e2SPeter Avalos 		tmp = rbl;
439ce0e08e2SPeter Avalos 		rbl = rbl->rpcb_entry_next;
440ce0e08e2SPeter Avalos 		free((char *)tmp->rpcb_entry_map.r_maddr);
441ce0e08e2SPeter Avalos 		free((char *)tmp);
442ce0e08e2SPeter Avalos 	}
443ce0e08e2SPeter Avalos 	*rlistp = NULL;
444ce0e08e2SPeter Avalos }
445ce0e08e2SPeter Avalos 
446ce0e08e2SPeter Avalos /* ARGSUSED */
447ce0e08e2SPeter Avalos static void *
rpcbproc_dump_4_local(void * arg __unused,struct svc_req * req __unused,SVCXPRT * xprt __unused,rpcvers_t versnum __unused)448ce0e08e2SPeter Avalos rpcbproc_dump_4_local(void *arg __unused, struct svc_req *req __unused,
449ce0e08e2SPeter Avalos 		      SVCXPRT *xprt __unused, rpcvers_t versnum __unused)
450ce0e08e2SPeter Avalos {
451ce0e08e2SPeter Avalos 	return ((void *)&list_rbl);
452ce0e08e2SPeter Avalos }
453