xref: /titanic_51/usr/src/cmd/rpcbind/rpcb_svc_4.c (revision 3db86aab554edbb4244c8d1a1c90f152eee768af)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * rpcb_svc_4.c
30  * The server procedure for the version 4 rpcbind.
31  *
32  */
33 
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <rpc/rpc.h>
39 #include <netconfig.h>
40 #include <syslog.h>
41 #include <netdir.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include "rpcbind.h"
45 
46 static void free_rpcb_entry_list();
47 
48 /*
49  * Called by svc_getreqset. There is a separate server handle for
50  * every transport that it waits on.
51  */
52 void
53 rpcb_service_4(rqstp, transp)
54 	register struct svc_req *rqstp;
55 	register SVCXPRT *transp;
56 {
57 	union {
58 		rpcb rpcbproc_set_4_arg;
59 		rpcb rpcbproc_unset_4_arg;
60 		rpcb rpcbproc_getaddr_4_arg;
61 		char *rpcbproc_uaddr2taddr_4_arg;
62 		struct netbuf rpcbproc_taddr2uaddr_4_arg;
63 	} argument;
64 	char *result;
65 	bool_t (*xdr_argument)(), (*xdr_result)();
66 	char *(*local)();
67 
68 	rpcbs_procinfo(RPCBVERS_4_STAT, rqstp->rq_proc);
69 
70 	RPCB_CHECK(transp, rqstp->rq_proc);
71 
72 	switch (rqstp->rq_proc) {
73 	case NULLPROC:
74 		/*
75 		 * Null proc call
76 		 */
77 #ifdef RPCBIND_DEBUG
78 		fprintf(stderr, "RPCBPROC_NULL\n");
79 #endif
80 		(void) svc_sendreply(transp, (xdrproc_t)xdr_void,
81 					(char *)NULL);
82 		return;
83 
84 	case RPCBPROC_SET:
85 		/*
86 		 * Check to see whether the message came from
87 		 * loopback transports (for security reasons)
88 		 */
89 		if (strcasecmp(transp->xp_netid, loopback_dg) &&
90 			strcasecmp(transp->xp_netid, loopback_vc) &&
91 			strcasecmp(transp->xp_netid, loopback_vc_ord)) {
92 			syslog(LOG_ERR, "non-local attempt to set");
93 			svcerr_weakauth(transp);
94 			return;
95 		}
96 		xdr_argument = xdr_rpcb;
97 		xdr_result = xdr_bool;
98 		local = (char *(*)()) rpcbproc_set_com;
99 		break;
100 
101 	case RPCBPROC_UNSET:
102 		/*
103 		 * Check to see whether the message came from
104 		 * loopback transports (for security reasons)
105 		 */
106 		if (strcasecmp(transp->xp_netid, loopback_dg) &&
107 			strcasecmp(transp->xp_netid, loopback_vc) &&
108 			strcasecmp(transp->xp_netid, loopback_vc_ord)) {
109 			syslog(LOG_ERR, "non-local attempt to unset");
110 			svcerr_weakauth(transp);
111 			return;
112 		}
113 		xdr_argument = xdr_rpcb;
114 		xdr_result = xdr_bool;
115 		local = (char *(*)()) rpcbproc_unset_com;
116 		break;
117 
118 	case RPCBPROC_GETADDR:
119 		xdr_argument = xdr_rpcb;
120 		xdr_result = xdr_wrapstring;
121 		local = (char *(*)()) rpcbproc_getaddr_4;
122 		break;
123 
124 	case RPCBPROC_GETVERSADDR:
125 #ifdef RPCBIND_DEBUG
126 		fprintf(stderr, "RPCBPROC_GETVERSADDR\n");
127 #endif
128 		xdr_argument = xdr_rpcb;
129 		xdr_result = xdr_wrapstring;
130 		local = (char *(*)()) rpcbproc_getversaddr_4;
131 		break;
132 
133 	case RPCBPROC_DUMP:
134 #ifdef RPCBIND_DEBUG
135 		fprintf(stderr, "RPCBPROC_DUMP\n");
136 #endif
137 		xdr_argument = xdr_void;
138 		xdr_result = xdr_rpcblist_ptr;
139 		local = (char *(*)()) rpcbproc_dump_4;
140 		break;
141 
142 	case RPCBPROC_INDIRECT:
143 #ifdef RPCBIND_DEBUG
144 		fprintf(stderr, "RPCBPROC_INDIRECT\n");
145 #endif
146 		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
147 		return;
148 
149 /*	case RPCBPROC_CALLIT: */
150 	case RPCBPROC_BCAST:
151 #ifdef RPCBIND_DEBUG
152 		fprintf(stderr, "RPCBPROC_BCAST\n");
153 #endif
154 		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
155 		return;
156 
157 	case RPCBPROC_GETTIME:
158 #ifdef RPCBIND_DEBUG
159 		fprintf(stderr, "RPCBPROC_GETTIME\n");
160 #endif
161 		xdr_argument = xdr_void;
162 		xdr_result = xdr_u_long;
163 		local = (char *(*)()) rpcbproc_gettime_com;
164 		break;
165 
166 	case RPCBPROC_UADDR2TADDR:
167 #ifdef RPCBIND_DEBUG
168 		fprintf(stderr, "RPCBPROC_UADDR2TADDR\n");
169 #endif
170 		xdr_argument = xdr_wrapstring;
171 		xdr_result = xdr_netbuf;
172 		local = (char *(*)()) rpcbproc_uaddr2taddr_com;
173 		break;
174 
175 	case RPCBPROC_TADDR2UADDR:
176 #ifdef RPCBIND_DEBUG
177 		fprintf(stderr, "RPCBPROC_TADDR2UADDR\n");
178 #endif
179 		xdr_argument = xdr_netbuf;
180 		xdr_result = xdr_wrapstring;
181 		local = (char *(*)()) rpcbproc_taddr2uaddr_com;
182 		break;
183 
184 	case RPCBPROC_GETADDRLIST:
185 #ifdef RPCBIND_DEBUG
186 		fprintf(stderr, "RPCBPROC_GETADDRLIST\n");
187 #endif
188 		xdr_argument = xdr_rpcb;
189 		xdr_result = xdr_rpcb_entry_list_ptr;
190 		local = (char *(*)()) rpcbproc_getaddrlist_4;
191 		break;
192 
193 	case RPCBPROC_GETSTAT:
194 #ifdef RPCBIND_DEBUG
195 		fprintf(stderr, "RPCBPROC_GETSTAT\n");
196 #endif
197 		xdr_argument = xdr_void;
198 		xdr_result = xdr_rpcb_stat_byvers;
199 		local = (char *(*)()) rpcbproc_getstat;
200 		break;
201 
202 	default:
203 		svcerr_noproc(transp);
204 		return;
205 	}
206 	memset((char *)&argument, 0, sizeof (argument));
207 	if (!svc_getargs(transp, (xdrproc_t)xdr_argument,
208 		(char *)&argument)) {
209 		svcerr_decode(transp);
210 		if (debugging)
211 			(void) fprintf(stderr, "rpcbind: could not decode\n");
212 		return;
213 	}
214 	result = (*local)(&argument, rqstp, transp, RPCBVERS4);
215 	if (result != NULL && !svc_sendreply(transp, (xdrproc_t)xdr_result,
216 						result)) {
217 		svcerr_systemerr(transp);
218 		if (debugging) {
219 			(void) fprintf(stderr, "rpcbind: svc_sendreply\n");
220 			if (doabort) {
221 				rpcbind_abort();
222 			}
223 		}
224 	}
225 	if (!svc_freeargs(transp, (xdrproc_t)xdr_argument,
226 				(char *)&argument)) {
227 		if (debugging) {
228 			(void) fprintf(stderr, "unable to free arguments\n");
229 			if (doabort) {
230 				rpcbind_abort();
231 			}
232 		}
233 	}
234 }
235 
236 /*
237  * Lookup the mapping for a program, version and return its
238  * address. Assuming that the caller wants the address of the
239  * server running on the transport on which the request came.
240  * Even if a service with a different version number is available,
241  * it will return that address.  The client should check with an
242  * clnt_call to verify whether the service is the one that is desired.
243  * We also try to resolve the universal address in terms of
244  * address of the caller.
245  */
246 /* ARGSUSED */
247 char **
248 rpcbproc_getaddr_4(regp, rqstp, transp, rpcbversnum)
249 	rpcb *regp;
250 	struct svc_req *rqstp;	/* Not used here */
251 	SVCXPRT *transp;
252 	int rpcbversnum; /* unused here */
253 {
254 #ifdef RPCBIND_DEBUG
255 	char *uaddr;
256 
257 	uaddr =	taddr2uaddr(rpcbind_get_conf(transp->xp_netid),
258 			    svc_getrpccaller(transp));
259 	fprintf(stderr, "RPCB_GETADDR request for (%lu, %lu, %s) from %s : ",
260 		regp->r_prog, regp->r_vers, transp->xp_netid, uaddr);
261 	free(uaddr);
262 #endif
263 	return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4,
264 					(ulong_t)RPCB_ALLVERS));
265 }
266 
267 /*
268  * Lookup the mapping for a program, version and return its
269  * address. Assuming that the caller wants the address of the
270  * server running on the transport on which the request came.
271  *
272  * We also try to resolve the universal address in terms of
273  * address of the caller.
274  */
275 /* ARGSUSED */
276 char **
277 rpcbproc_getversaddr_4(regp, rqstp, transp)
278 	rpcb *regp;
279 	struct svc_req *rqstp;	/* Not used here */
280 	SVCXPRT *transp;
281 {
282 #ifdef RPCBIND_DEBUG
283 	char *uaddr;
284 
285 	uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid),
286 			    svc_getrpccaller(transp));
287 	fprintf(stderr, "RPCB_GETVERSADDR rqst for (%lu, %lu, %s) from %s : ",
288 		regp->r_prog, regp->r_vers, transp->xp_netid, uaddr);
289 	free(uaddr);
290 #endif
291 	return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS4,
292 					(ulong_t)RPCB_ONEVERS));
293 }
294 
295 /*
296  * Lookup the mapping for a program, version and return the
297  * addresses for all transports in the current transport family.
298  * We return a merged address.
299  */
300 /* ARGSUSED */
301 rpcb_entry_list_ptr *
302 rpcbproc_getaddrlist_4(
303 	rpcb *regp,
304 	struct svc_req *rqstp,	/* Not used here */
305 	SVCXPRT *transp)
306 {
307 	static rpcb_entry_list_ptr rlist;
308 	rpcblist_ptr rbl, next, prev;
309 	rpcb_entry_list_ptr rp, tail;
310 	ulong_t prog, vers;
311 	rpcb_entry *a;
312 	struct netconfig *nconf;
313 	struct netconfig *reg_nconf;
314 	char *saddr, *maddr = NULL;
315 	struct netconfig *trans_conf;	/* transport netconfig */
316 
317 	/*
318 	 * Deal with a possible window during which we could return an IPv6
319 	 * address when the caller wanted IPv4.  See the comments in
320 	 * rpcbproc_getaddr_com() for more details.
321 	 */
322 	trans_conf = rpcbind_get_conf(transp->xp_netid);
323 	if (strcmp(trans_conf->nc_protofmly, NC_INET6) == 0) {
324 		struct sockaddr_in6 *rmtaddr;
325 
326 		rmtaddr = (struct sockaddr_in6 *)transp->xp_rtaddr.buf;
327 		if (IN6_IS_ADDR_V4MAPPED(&rmtaddr->sin6_addr)) {
328 			syslog(LOG_DEBUG,
329 			    "IPv4 GETADDRLIST request mapped "
330 			    "to IPv6: ignoring");
331 			return (NULL);
332 		}
333 	}
334 
335 	free_rpcb_entry_list(&rlist);
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 	fprintf(stderr, "r_addr: %s r_netid: %s nc_protofmly: %s\n",
348 		regp->r_addr, transp->xp_netid, reg_nconf->nc_protofmly);
349 #endif
350 	prev = NULL;
351 	for (rbl = list_rbl; rbl != NULL; rbl = next) {
352 	    next = 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 			prev = rbl;
361 			continue;	/* not same proto family */
362 		}
363 #ifdef RPCBIND_DEBUG
364 		fprintf(stderr, "\tmerge with: %s", rbl->rpcb_map.r_addr);
365 #endif
366 		if ((maddr = mergeaddr(transp, rbl->rpcb_map.r_netid,
367 				rbl->rpcb_map.r_addr, saddr)) == NULL) {
368 #ifdef RPCBIND_DEBUG
369 		fprintf(stderr, " FAILED\n");
370 #endif
371 			prev = rbl;
372 			continue;
373 		} else if (!maddr[0]) {
374 #ifdef RPCBIND_DEBUG
375 	fprintf(stderr, " SUCCEEDED, but port died -  maddr: nullstring\n");
376 #endif
377 			/*
378 			 * The server died, remove this rpcb_map element from
379 			 * the list and free it.
380 			 */
381 #ifdef PORTMAP
382 			(void) del_pmaplist(&rbl->rpcb_map);
383 #endif
384 			(void) delete_rbl(rbl);
385 
386 			if (prev == NULL)
387 				list_rbl = next;
388 			else
389 				prev->rpcb_next = next;
390 			continue;
391 		}
392 #ifdef RPCBIND_DEBUG
393 		fprintf(stderr, " SUCCEEDED maddr: %s\n", maddr);
394 #endif
395 		/*
396 		 * Add it to rlist.
397 		 */
398 		rp = (rpcb_entry_list_ptr)
399 			malloc((uint_t)sizeof (rpcb_entry_list));
400 		if (rp == NULL)
401 			goto fail;
402 		a = &rp->rpcb_entry_map;
403 		a->r_maddr = maddr;
404 		a->r_nc_netid = nconf->nc_netid;
405 		a->r_nc_semantics = nconf->nc_semantics;
406 		a->r_nc_protofmly = nconf->nc_protofmly;
407 		a->r_nc_proto = nconf->nc_proto;
408 		rp->rpcb_entry_next = NULL;
409 		if (rlist == NULL) {
410 			rlist = rp;
411 			tail = rp;
412 		} else {
413 			tail->rpcb_entry_next = rp;
414 			tail = rp;
415 		}
416 		rp = NULL;
417 	    }
418 	    prev = rbl;
419 	}
420 #ifdef RPCBIND_DEBUG
421 	for (rp = rlist; rp; rp = rp->rpcb_entry_next) {
422 		fprintf(stderr, "\t%s %s\n", rp->rpcb_entry_map.r_maddr,
423 			rp->rpcb_entry_map.r_nc_proto);
424 	}
425 #endif
426 	/*
427 	 * XXX: getaddrlist info is also being stuffed into getaddr.
428 	 * Perhaps wrong, but better than it not getting counted at all.
429 	 */
430 	rpcbs_getaddr(RPCBVERS4 - 2, prog, vers, transp->xp_netid, maddr);
431 	return (&rlist);
432 
433 fail:	free_rpcb_entry_list(&rlist);
434 	return (NULL);
435 }
436 
437 /*
438  * Free only the allocated structure, rest is all a pointer to some
439  * other data somewhere else.
440  */
441 void
442 free_rpcb_entry_list(rlistp)
443 	rpcb_entry_list_ptr *rlistp;
444 {
445 	register rpcb_entry_list_ptr rbl, tmp;
446 
447 	for (rbl = *rlistp; rbl != NULL; ) {
448 		tmp = rbl;
449 		rbl = rbl->rpcb_entry_next;
450 		free((char *)tmp->rpcb_entry_map.r_maddr);
451 		free((char *)tmp);
452 	}
453 	*rlistp = NULL;
454 }
455 
456 /* VARARGS */
457 rpcblist_ptr *
458 rpcbproc_dump_4()
459 {
460 	return ((rpcblist_ptr *)&list_rbl);
461 }
462