xref: /freebsd/usr.sbin/rpcbind/rpcb_svc_4.c (revision 8a16b7a1)
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 <unistd.h>
52 #include <netconfig.h>
53 #include <syslog.h>
54 #include <string.h>
55 #include <stdlib.h>
56 #include "rpcbind.h"
57 
58 static void *rpcbproc_getaddr_4_local(void *, struct svc_req *, SVCXPRT *,
59 				      rpcvers_t);
60 static void *rpcbproc_getversaddr_4_local(void *, struct svc_req *, SVCXPRT *, rpcvers_t);
61 static void *rpcbproc_getaddrlist_4_local
62 	(void *, struct svc_req *, SVCXPRT *, rpcvers_t);
63 static void free_rpcb_entry_list(rpcb_entry_list_ptr *);
64 static void *rpcbproc_dump_4_local(void *, struct svc_req *, SVCXPRT *, rpcvers_t);
65 
66 /*
67  * Called by svc_getreqset. There is a separate server handle for
68  * every transport that it waits on.
69  */
70 void
71 rpcb_service_4(struct svc_req *rqstp, SVCXPRT *transp)
72 {
73 	union {
74 		rpcb rpcbproc_set_4_arg;
75 		rpcb rpcbproc_unset_4_arg;
76 		rpcb rpcbproc_getaddr_4_local_arg;
77 		char *rpcbproc_uaddr2taddr_4_arg;
78 		struct netbuf rpcbproc_taddr2uaddr_4_arg;
79 	} argument;
80 	char *result;
81 	xdrproc_t xdr_argument, xdr_result;
82 	void *(*local)(void *, struct svc_req *, SVCXPRT *, rpcvers_t);
83 
84 	rpcbs_procinfo(RPCBVERS_4_STAT, rqstp->rq_proc);
85 
86 	switch (rqstp->rq_proc) {
87 	case NULLPROC:
88 		/*
89 		 * Null proc call
90 		 */
91 #ifdef RPCBIND_DEBUG
92 		if (debugging)
93 			fprintf(stderr, "RPCBPROC_NULL\n");
94 #endif
95 		check_access(transp, rqstp->rq_proc, NULL, RPCBVERS4);
96 		(void) svc_sendreply(transp, (xdrproc_t) xdr_void,
97 					(char *)NULL);
98 		return;
99 
100 	case RPCBPROC_SET:
101 		/*
102 		 * Check to see whether the message came from
103 		 * loopback transports (for security reasons)
104 		 */
105 		xdr_argument = (xdrproc_t)xdr_rpcb;
106 		xdr_result = (xdrproc_t)xdr_bool;
107 		local = rpcbproc_set_com;
108 		break;
109 
110 	case RPCBPROC_UNSET:
111 		/*
112 		 * Check to see whether the message came from
113 		 * loopback transports (for security reasons)
114 		 */
115 		xdr_argument = (xdrproc_t)xdr_rpcb;
116 		xdr_result = (xdrproc_t)xdr_bool;
117 		local = rpcbproc_unset_com;
118 		break;
119 
120 	case RPCBPROC_GETADDR:
121 		xdr_argument = (xdrproc_t)xdr_rpcb;
122 		xdr_result = (xdrproc_t)xdr_wrapstring;
123 		local = rpcbproc_getaddr_4_local;
124 		break;
125 
126 	case RPCBPROC_GETVERSADDR:
127 #ifdef RPCBIND_DEBUG
128 		if (debugging)
129 			fprintf(stderr, "RPCBPROC_GETVERSADDR\n");
130 #endif
131 		xdr_argument = (xdrproc_t)xdr_rpcb;
132 		xdr_result = (xdrproc_t)xdr_wrapstring;
133 		local = rpcbproc_getversaddr_4_local;
134 		break;
135 
136 	case RPCBPROC_DUMP:
137 #ifdef RPCBIND_DEBUG
138 		if (debugging)
139 			fprintf(stderr, "RPCBPROC_DUMP\n");
140 #endif
141 		xdr_argument = (xdrproc_t)xdr_void;
142 		xdr_result = (xdrproc_t)xdr_rpcblist_ptr;
143 		local = rpcbproc_dump_4_local;
144 		break;
145 
146 	case RPCBPROC_INDIRECT:
147 #ifdef RPCBIND_DEBUG
148 		if (debugging)
149 			fprintf(stderr, "RPCBPROC_INDIRECT\n");
150 #endif
151 		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
152 		return;
153 
154 /*	case RPCBPROC_CALLIT: */
155 	case RPCBPROC_BCAST:
156 #ifdef RPCBIND_DEBUG
157 		if (debugging)
158 			fprintf(stderr, "RPCBPROC_BCAST\n");
159 #endif
160 		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
161 		return;
162 
163 	case RPCBPROC_GETTIME:
164 #ifdef RPCBIND_DEBUG
165 		if (debugging)
166 			fprintf(stderr, "RPCBPROC_GETTIME\n");
167 #endif
168 		xdr_argument = (xdrproc_t)xdr_void;
169 		xdr_result = (xdrproc_t)xdr_u_long;
170 		local = rpcbproc_gettime_com;
171 		break;
172 
173 	case RPCBPROC_UADDR2TADDR:
174 #ifdef RPCBIND_DEBUG
175 		if (debugging)
176 			fprintf(stderr, "RPCBPROC_UADDR2TADDR\n");
177 #endif
178 		xdr_argument = (xdrproc_t)xdr_wrapstring;
179 		xdr_result = (xdrproc_t)xdr_netbuf;
180 		local = rpcbproc_uaddr2taddr_com;
181 		break;
182 
183 	case RPCBPROC_TADDR2UADDR:
184 #ifdef RPCBIND_DEBUG
185 		if (debugging)
186 			fprintf(stderr, "RPCBPROC_TADDR2UADDR\n");
187 #endif
188 		xdr_argument = (xdrproc_t)xdr_netbuf;
189 		xdr_result = (xdrproc_t)xdr_wrapstring;
190 		local = rpcbproc_taddr2uaddr_com;
191 		break;
192 
193 	case RPCBPROC_GETADDRLIST:
194 #ifdef RPCBIND_DEBUG
195 		if (debugging)
196 			fprintf(stderr, "RPCBPROC_GETADDRLIST\n");
197 #endif
198 		xdr_argument = (xdrproc_t)xdr_rpcb;
199 		xdr_result = (xdrproc_t)xdr_rpcb_entry_list_ptr;
200 		local = rpcbproc_getaddrlist_4_local;
201 		break;
202 
203 	case RPCBPROC_GETSTAT:
204 #ifdef RPCBIND_DEBUG
205 		if (debugging)
206 			fprintf(stderr, "RPCBPROC_GETSTAT\n");
207 #endif
208 		xdr_argument = (xdrproc_t)xdr_void;
209 		xdr_result = (xdrproc_t)xdr_rpcb_stat_byvers;
210 		local = rpcbproc_getstat;
211 		break;
212 
213 	default:
214 		svcerr_noproc(transp);
215 		return;
216 	}
217 	memset((char *)&argument, 0, sizeof (argument));
218 	if (!svc_getargs(transp, (xdrproc_t) xdr_argument,
219 		(char *)&argument)) {
220 		svcerr_decode(transp);
221 		if (debugging)
222 			(void) fprintf(stderr, "rpcbind: could not decode\n");
223 		return;
224 	}
225 	if (!check_access(transp, rqstp->rq_proc, &argument, RPCBVERS4)) {
226 		svcerr_weakauth(transp);
227 		goto done;
228 	}
229 	result = (*local)(&argument, rqstp, transp, RPCBVERS4);
230 	if (result != NULL && !svc_sendreply(transp, (xdrproc_t) xdr_result,
231 						result)) {
232 		svcerr_systemerr(transp);
233 		if (debugging) {
234 			(void) fprintf(stderr, "rpcbind: svc_sendreply\n");
235 			if (doabort) {
236 				rpcbind_abort();
237 			}
238 		}
239 	}
240 done:
241 	if (!svc_freeargs(transp, (xdrproc_t) xdr_argument,
242 				(char *)&argument)) {
243 		if (debugging) {
244 			(void) fprintf(stderr, "unable to free arguments\n");
245 			if (doabort) {
246 				rpcbind_abort();
247 			}
248 		}
249 	}
250 	return;
251 }
252 
253 /*
254  * Lookup the mapping for a program, version and return its
255  * address. Assuming that the caller wants the address of the
256  * server running on the transport on which the request came.
257  * Even if a service with a different version number is available,
258  * it will return that address.  The client should check with an
259  * clnt_call to verify whether the service is the one that is desired.
260  * We also try to resolve the universal address in terms of
261  * address of the caller.
262  */
263 /* ARGSUSED */
264 static void *
265 rpcbproc_getaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
266 			 rpcvers_t rpcbversnum __unused)
267 {
268 	RPCB *regp = (RPCB *)arg;
269 #ifdef RPCBIND_DEBUG
270 	if (debugging) {
271 		char *uaddr;
272 
273 		uaddr =	taddr2uaddr(rpcbind_get_conf(transp->xp_netid),
274 			    svc_getrpccaller(transp));
275 		fprintf(stderr, "RPCB_GETADDR req for (%lu, %lu, %s) from %s: ",
276 		    (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
277 		    regp->r_netid, uaddr);
278 		free(uaddr);
279 	}
280 #endif
281 	return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4,
282 					RPCB_ALLVERS));
283 }
284 
285 /*
286  * Lookup the mapping for a program, version and return its
287  * address. Assuming that the caller wants the address of the
288  * server running on the transport on which the request came.
289  *
290  * We also try to resolve the universal address in terms of
291  * address of the caller.
292  */
293 /* ARGSUSED */
294 static void *
295 rpcbproc_getversaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
296 			     rpcvers_t versnum __unused)
297 {
298 	RPCB *regp = (RPCB *)arg;
299 #ifdef RPCBIND_DEBUG
300 	if (debugging) {
301 		char *uaddr;
302 
303 		uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid),
304 			    svc_getrpccaller(transp));
305 		fprintf(stderr, "RPCB_GETVERSADDR rqst for (%lu, %lu, %s)"
306 				" from %s : ",
307 		    (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
308 		    regp->r_netid, uaddr);
309 		free(uaddr);
310 	}
311 #endif
312 	return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4,
313 					RPCB_ONEVERS));
314 }
315 
316 /*
317  * Lookup the mapping for a program, version and return the
318  * addresses for all transports in the current transport family.
319  * We return a merged address.
320  */
321 /* ARGSUSED */
322 static void *
323 rpcbproc_getaddrlist_4_local(void *arg, struct svc_req *rqstp __unused,
324 			     SVCXPRT *transp, rpcvers_t versnum __unused)
325 {
326 	RPCB *regp = (RPCB *)arg;
327 	static rpcb_entry_list_ptr rlist;
328 	register rpcblist_ptr rbl;
329 	rpcb_entry_list_ptr rp, tail;
330 	rpcprog_t prog;
331 	rpcvers_t vers;
332 	rpcb_entry *a;
333 	struct netconfig *nconf;
334 	struct netconfig *reg_nconf;
335 	char *saddr, *maddr = NULL;
336 
337 	free_rpcb_entry_list(&rlist);
338 	tail = NULL;
339 	prog = regp->r_prog;
340 	vers = regp->r_vers;
341 	reg_nconf = rpcbind_get_conf(transp->xp_netid);
342 	if (reg_nconf == NULL)
343 		return (NULL);
344 	if (*(regp->r_addr) != '\0') {
345 		saddr = regp->r_addr;
346 	} else {
347 		saddr = NULL;
348 	}
349 #ifdef RPCBIND_DEBUG
350 	if (debugging) {
351 		fprintf(stderr, "r_addr: %s r_netid: %s nc_protofmly: %s\n",
352 		    regp->r_addr, regp->r_netid, reg_nconf->nc_protofmly);
353 	}
354 #endif
355 	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
356 	    if ((rbl->rpcb_map.r_prog == prog) &&
357 		(rbl->rpcb_map.r_vers == vers)) {
358 		nconf = rpcbind_get_conf(rbl->rpcb_map.r_netid);
359 		if (nconf == NULL)
360 			goto fail;
361 		if (strcmp(nconf->nc_protofmly, reg_nconf->nc_protofmly)
362 				!= 0) {
363 			continue;	/* not same proto family */
364 		}
365 #ifdef RPCBIND_DEBUG
366 		if (debugging)
367 			fprintf(stderr, "\tmerge with: %s\n",
368 			    rbl->rpcb_map.r_addr);
369 #endif
370 		if ((maddr = mergeaddr(transp, rbl->rpcb_map.r_netid,
371 				rbl->rpcb_map.r_addr, saddr)) == NULL) {
372 #ifdef RPCBIND_DEBUG
373 		if (debugging)
374 			fprintf(stderr, " FAILED\n");
375 #endif
376 			continue;
377 		} else if (!maddr[0]) {
378 #ifdef RPCBIND_DEBUG
379 	if (debugging)
380 		fprintf(stderr, " SUCCEEDED, but port died -  maddr: nullstring\n");
381 #endif
382 			/* The server died. Unset this combination */
383 			delete_prog(regp->r_prog);
384 			continue;
385 		}
386 #ifdef RPCBIND_DEBUG
387 		if (debugging)
388 			fprintf(stderr, " SUCCEEDED maddr: %s\n", maddr);
389 #endif
390 		/*
391 		 * Add it to rlist.
392 		 */
393 		rp = malloc(sizeof (rpcb_entry_list));
394 		if (rp == NULL)
395 			goto fail;
396 		a = &rp->rpcb_entry_map;
397 		a->r_maddr = maddr;
398 		a->r_nc_netid = nconf->nc_netid;
399 		a->r_nc_semantics = nconf->nc_semantics;
400 		a->r_nc_protofmly = nconf->nc_protofmly;
401 		a->r_nc_proto = nconf->nc_proto;
402 		rp->rpcb_entry_next = NULL;
403 		if (rlist == NULL) {
404 			rlist = rp;
405 			tail = rp;
406 		} else {
407 			tail->rpcb_entry_next = rp;
408 			tail = rp;
409 		}
410 		rp = NULL;
411 	    }
412 	}
413 #ifdef RPCBIND_DEBUG
414 	if (debugging) {
415 		for (rp = rlist; rp; rp = rp->rpcb_entry_next) {
416 			fprintf(stderr, "\t%s %s\n", rp->rpcb_entry_map.r_maddr,
417 				rp->rpcb_entry_map.r_nc_proto);
418 		}
419 	}
420 #endif
421 	/*
422 	 * XXX: getaddrlist info is also being stuffed into getaddr.
423 	 * Perhaps wrong, but better than it not getting counted at all.
424 	 */
425 	rpcbs_getaddr(RPCBVERS4 - 2, prog, vers, transp->xp_netid, maddr);
426 	return (void *)&rlist;
427 
428 fail:	free_rpcb_entry_list(&rlist);
429 	return (NULL);
430 }
431 
432 /*
433  * Free only the allocated structure, rest is all a pointer to some
434  * other data somewhere else.
435  */
436 static void
437 free_rpcb_entry_list(rpcb_entry_list_ptr *rlistp)
438 {
439 	register rpcb_entry_list_ptr rbl, tmp;
440 
441 	for (rbl = *rlistp; rbl != NULL; ) {
442 		tmp = rbl;
443 		rbl = rbl->rpcb_entry_next;
444 		free((char *)tmp->rpcb_entry_map.r_maddr);
445 		free((char *)tmp);
446 	}
447 	*rlistp = NULL;
448 }
449 
450 /* ARGSUSED */
451 static void *
452 rpcbproc_dump_4_local(void *arg __unused, struct svc_req *req __unused,
453     		      SVCXPRT *xprt __unused, rpcvers_t versnum __unused)
454 {
455 	return ((void *)&list_rbl);
456 }
457