1/*	$NetBSD: rpcb_svc_4.c,v 1.6 2007/05/13 20:03:47 christos Exp $	*/
2
3/*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part.  Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California  94043
30 */
31/*
32 * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
33 */
34
35/* #ident	"@(#)rpcb_svc_4.c	1.8	93/07/05 SMI" */
36
37/*
38 * rpcb_svc_4.c
39 * The server procedure for the version 4 rpcbind.
40 *
41 */
42
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <rpc/rpc.h>
46#include <stdio.h>
47#include <unistd.h>
48#include <netconfig.h>
49#include <syslog.h>
50#include <string.h>
51#include <stdlib.h>
52#include "rpcbind.h"
53
54static void *rpcbproc_getaddr_4_local(void *, struct svc_req *, SVCXPRT *,
55    rpcvers_t);
56static void *rpcbproc_getversaddr_4_local(void *, struct svc_req *, SVCXPRT *,
57    rpcvers_t);
58static void *rpcbproc_getaddrlist_4_local(void *, struct svc_req *, SVCXPRT *,
59    rpcvers_t);
60static void free_rpcb_entry_list(rpcb_entry_list_ptr *);
61static void *rpcbproc_dump_4_local(void *, struct svc_req *, SVCXPRT *,
62    rpcvers_t);
63
64/*
65 * Called by svc_getreqset. There is a separate server handle for
66 * every transport that it waits on.
67 */
68void
69rpcb_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, NULL);
95		return;
96
97	case RPCBPROC_SET:
98		/*
99		 * Check to see whether the message came from
100		 * loopback transports (for security reasons)
101		 */
102		xdr_argument = (xdrproc_t)xdr_rpcb;
103		xdr_result = (xdrproc_t)xdr_bool;
104		local = rpcbproc_set_com;
105		break;
106
107	case RPCBPROC_UNSET:
108		/*
109		 * Check to see whether the message came from
110		 * loopback transports (for security reasons)
111		 */
112		xdr_argument = (xdrproc_t)xdr_rpcb;
113		xdr_result = (xdrproc_t)xdr_bool;
114		local = rpcbproc_unset_com;
115		break;
116
117	case RPCBPROC_GETADDR:
118		xdr_argument = (xdrproc_t)xdr_rpcb;
119		xdr_result = (xdrproc_t)xdr_wrapstring;
120		local = rpcbproc_getaddr_4_local;
121		break;
122
123	case RPCBPROC_GETVERSADDR:
124#ifdef RPCBIND_DEBUG
125		if (debugging)
126			fprintf(stderr, "RPCBPROC_GETVERSADDR\n");
127#endif
128		xdr_argument = (xdrproc_t)xdr_rpcb;
129		xdr_result = (xdrproc_t)xdr_wrapstring;
130		local = rpcbproc_getversaddr_4_local;
131		break;
132
133	case RPCBPROC_DUMP:
134#ifdef RPCBIND_DEBUG
135		if (debugging)
136			fprintf(stderr, "RPCBPROC_DUMP\n");
137#endif
138		xdr_argument = (xdrproc_t)xdr_void;
139		xdr_result = (xdrproc_t)xdr_rpcblist_ptr;
140		local = rpcbproc_dump_4_local;
141		break;
142
143	case RPCBPROC_INDIRECT:
144#ifdef RPCBIND_DEBUG
145		if (debugging)
146			fprintf(stderr, "RPCBPROC_INDIRECT\n");
147#endif
148		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
149		return;
150
151/*	case RPCBPROC_CALLIT: */
152	case RPCBPROC_BCAST:
153#ifdef RPCBIND_DEBUG
154		if (debugging)
155			fprintf(stderr, "RPCBPROC_BCAST\n");
156#endif
157		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS4);
158		return;
159
160	case RPCBPROC_GETTIME:
161#ifdef RPCBIND_DEBUG
162		if (debugging)
163			fprintf(stderr, "RPCBPROC_GETTIME\n");
164#endif
165		xdr_argument = (xdrproc_t)xdr_void;
166		xdr_result = (xdrproc_t)xdr_u_long;
167		local = rpcbproc_gettime_com;
168		break;
169
170	case RPCBPROC_UADDR2TADDR:
171#ifdef RPCBIND_DEBUG
172		if (debugging)
173			fprintf(stderr, "RPCBPROC_UADDR2TADDR\n");
174#endif
175		xdr_argument = (xdrproc_t)xdr_wrapstring;
176		xdr_result = (xdrproc_t)xdr_netbuf;
177		local = rpcbproc_uaddr2taddr_com;
178		break;
179
180	case RPCBPROC_TADDR2UADDR:
181#ifdef RPCBIND_DEBUG
182		if (debugging)
183			fprintf(stderr, "RPCBPROC_TADDR2UADDR\n");
184#endif
185		xdr_argument = (xdrproc_t)xdr_netbuf;
186		xdr_result = (xdrproc_t)xdr_wrapstring;
187		local = rpcbproc_taddr2uaddr_com;
188		break;
189
190	case RPCBPROC_GETADDRLIST:
191#ifdef RPCBIND_DEBUG
192		if (debugging)
193			fprintf(stderr, "RPCBPROC_GETADDRLIST\n");
194#endif
195		xdr_argument = (xdrproc_t)xdr_rpcb;
196		xdr_result = (xdrproc_t)xdr_rpcb_entry_list_ptr;
197		local = rpcbproc_getaddrlist_4_local;
198		break;
199
200	case RPCBPROC_GETSTAT:
201#ifdef RPCBIND_DEBUG
202		if (debugging)
203			fprintf(stderr, "RPCBPROC_GETSTAT\n");
204#endif
205		xdr_argument = (xdrproc_t)xdr_void;
206		xdr_result = (xdrproc_t)xdr_rpcb_stat_byvers;
207		local = rpcbproc_getstat;
208		break;
209
210	default:
211		svcerr_noproc(transp);
212		return;
213	}
214	memset((char *)&argument, 0, sizeof (argument));
215	if (!svc_getargs(transp, (xdrproc_t) xdr_argument,
216		(char *)&argument)) {
217		svcerr_decode(transp);
218		if (debugging)
219			(void) fprintf(stderr, "rpcbind: could not decode\n");
220		return;
221	}
222	if (!check_access(transp, rqstp->rq_proc, &argument, RPCBVERS4)) {
223		svcerr_weakauth(transp);
224		goto done;
225	}
226	result = (*local)(&argument, rqstp, transp, RPCBVERS4);
227	if (result != NULL && !svc_sendreply(transp, (xdrproc_t) xdr_result,
228						result)) {
229		svcerr_systemerr(transp);
230		if (debugging) {
231			(void) fprintf(stderr, "rpcbind: svc_sendreply\n");
232			if (doabort) {
233				rpcbind_abort();
234			}
235		}
236	}
237done:
238	if (!svc_freeargs(transp, (xdrproc_t) xdr_argument,
239				(char *)&argument)) {
240		if (debugging) {
241			(void) fprintf(stderr, "unable to free arguments\n");
242			if (doabort) {
243				rpcbind_abort();
244			}
245		}
246	}
247	return;
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 */
261static void *
262rpcbproc_getaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
263			 rpcvers_t rpcbversnum)
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 */
291static void *
292rpcbproc_getversaddr_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
293			     rpcvers_t versnum)
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 */
319static void *
320rpcbproc_getaddrlist_4_local(void *arg, struct svc_req *rqstp, SVCXPRT *transp,
321			     rpcvers_t versnum)
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 = NULL;
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	prog = regp->r_prog;
336	vers = regp->r_vers;
337	reg_nconf = rpcbind_get_conf(transp->xp_netid);
338	if (reg_nconf == NULL)
339		return (NULL);
340	if (*(regp->r_addr) != '\0') {
341		saddr = regp->r_addr;
342	} else {
343		saddr = NULL;
344	}
345#ifdef RPCBIND_DEBUG
346	if (debugging) {
347		fprintf(stderr, "r_addr: %s r_netid: %s nc_protofmly: %s\n",
348		    regp->r_addr, regp->r_netid, reg_nconf->nc_protofmly);
349	}
350#endif
351	for (rbl = list_rbl; rbl != NULL; rbl = rbl->rpcb_next) {
352	    if ((rbl->rpcb_map.r_prog == prog) &&
353		(rbl->rpcb_map.r_vers == vers)) {
354		nconf = rpcbind_get_conf(rbl->rpcb_map.r_netid);
355		if (nconf == NULL)
356			goto fail;
357		if (strcmp(nconf->nc_protofmly, reg_nconf->nc_protofmly)
358				!= 0) {
359			continue;	/* not same proto family */
360		}
361#ifdef RPCBIND_DEBUG
362		if (debugging)
363			fprintf(stderr, "\tmerge with: %s\n", rbl->rpcb_map.r_addr);
364#endif
365		if ((maddr = mergeaddr(transp, rbl->rpcb_map.r_netid,
366				rbl->rpcb_map.r_addr, saddr)) == NULL) {
367#ifdef RPCBIND_DEBUG
368		if (debugging)
369			fprintf(stderr, " FAILED\n");
370#endif
371			continue;
372		} else if (!maddr[0]) {
373#ifdef RPCBIND_DEBUG
374	if (debugging)
375		fprintf(stderr, " SUCCEEDED, but port died -  maddr: nullstring\n");
376#endif
377			/* The server died. Unset this combination */
378			delete_prog(regp->r_prog);
379			free(maddr);
380			continue;
381		}
382#ifdef RPCBIND_DEBUG
383		if (debugging)
384			fprintf(stderr, " SUCCEEDED maddr: %s\n", maddr);
385#endif
386		/*
387		 * Add it to rlist.
388		 */
389		rp = (rpcb_entry_list_ptr)
390			malloc((u_int)sizeof (rpcb_entry_list));
391		if (rp == NULL) {
392			free(maddr);
393			goto fail;
394		}
395		a = &rp->rpcb_entry_map;
396		a->r_maddr = maddr;
397		a->r_nc_netid = nconf->nc_netid;
398		a->r_nc_semantics = nconf->nc_semantics;
399		a->r_nc_protofmly = nconf->nc_protofmly;
400		a->r_nc_proto = nconf->nc_proto;
401		rp->rpcb_entry_next = NULL;
402		if (rlist == NULL) {
403			rlist = rp;
404			tail = rp;
405		} else if (tail) {
406			tail->rpcb_entry_next = rp;
407			tail = rp;
408		}
409		rp = NULL;
410	    }
411	}
412#ifdef RPCBIND_DEBUG
413	if (debugging) {
414		for (rp = rlist; rp; rp = rp->rpcb_entry_next) {
415			fprintf(stderr, "\t%s %s\n", rp->rpcb_entry_map.r_maddr,
416				rp->rpcb_entry_map.r_nc_proto);
417		}
418	}
419#endif
420	/*
421	 * XXX: getaddrlist info is also being stuffed into getaddr.
422	 * Perhaps wrong, but better than it not getting counted at all.
423	 */
424	rpcbs_getaddr(RPCBVERS4 - 2, prog, vers, transp->xp_netid, maddr);
425	return (void *)&rlist;
426
427fail:	free_rpcb_entry_list(&rlist);
428	return (NULL);
429}
430
431/*
432 * Free only the allocated structure, rest is all a pointer to some
433 * other data somewhere else.
434 */
435static void
436free_rpcb_entry_list(rpcb_entry_list_ptr *rlistp)
437{
438	register rpcb_entry_list_ptr rbl, tmp;
439
440	for (rbl = *rlistp; rbl != NULL; ) {
441		tmp = rbl;
442		rbl = rbl->rpcb_entry_next;
443		free((char *)tmp->rpcb_entry_map.r_maddr);
444		free((char *)tmp);
445	}
446	*rlistp = NULL;
447}
448
449/* ARGSUSED */
450static void *
451rpcbproc_dump_4_local(void *arg, struct svc_req *req, SVCXPRT *xprt,
452		      rpcvers_t versnum)
453{
454	return ((void *)&list_rbl);
455}
456