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