xref: /dflybsd-src/usr.sbin/rpcbind/rpcb_svc.c (revision ce0e08e21d42c06c0014fae6b9d27144aa5109b0)
1*ce0e08e2SPeter Avalos /*
2*ce0e08e2SPeter Avalos  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3*ce0e08e2SPeter Avalos  * unrestricted use provided that this legend is included on all tape
4*ce0e08e2SPeter Avalos  * media and as a part of the software program in whole or part.  Users
5*ce0e08e2SPeter Avalos  * may copy or modify Sun RPC without charge, but are not authorized
6*ce0e08e2SPeter Avalos  * to license or distribute it to anyone else except as part of a product or
7*ce0e08e2SPeter Avalos  * program developed by the user.
8*ce0e08e2SPeter Avalos  *
9*ce0e08e2SPeter Avalos  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10*ce0e08e2SPeter Avalos  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11*ce0e08e2SPeter Avalos  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12*ce0e08e2SPeter Avalos  *
13*ce0e08e2SPeter Avalos  * Sun RPC is provided with no support and without any obligation on the
14*ce0e08e2SPeter Avalos  * part of Sun Microsystems, Inc. to assist in its use, correction,
15*ce0e08e2SPeter Avalos  * modification or enhancement.
16*ce0e08e2SPeter Avalos  *
17*ce0e08e2SPeter Avalos  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18*ce0e08e2SPeter Avalos  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19*ce0e08e2SPeter Avalos  * OR ANY PART THEREOF.
20*ce0e08e2SPeter Avalos  *
21*ce0e08e2SPeter Avalos  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22*ce0e08e2SPeter Avalos  * or profits or other special, indirect and consequential damages, even if
23*ce0e08e2SPeter Avalos  * Sun has been advised of the possibility of such damages.
24*ce0e08e2SPeter Avalos  *
25*ce0e08e2SPeter Avalos  * Sun Microsystems, Inc.
26*ce0e08e2SPeter Avalos  * 2550 Garcia Avenue
27*ce0e08e2SPeter Avalos  * Mountain View, California  94043
28*ce0e08e2SPeter Avalos  *
29*ce0e08e2SPeter Avalos  * @(#)rpcb_svc.c	1.16	93/07/05 SMI"
30*ce0e08e2SPeter Avalos  * $NetBSD: rpcb_svc.c,v 1.1 2000/06/02 23:15:41 fvdl Exp $
31*ce0e08e2SPeter Avalos  * $FreeBSD: src/usr.sbin/rpcbind/rpcb_svc.c,v 1.3 2007/11/07 10:53:39 kevlo Exp $
32*ce0e08e2SPeter Avalos  * $DragonFly$
33*ce0e08e2SPeter Avalos  */
34*ce0e08e2SPeter Avalos /*
35*ce0e08e2SPeter Avalos  * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc.
36*ce0e08e2SPeter Avalos  */
37*ce0e08e2SPeter Avalos 
38*ce0e08e2SPeter Avalos /*
39*ce0e08e2SPeter Avalos  * rpcb_svc.c
40*ce0e08e2SPeter Avalos  * The server procedure for the version 3 rpcbind (TLI).
41*ce0e08e2SPeter Avalos  *
42*ce0e08e2SPeter Avalos  * It maintains a separate list of all the registered services with the
43*ce0e08e2SPeter Avalos  * version 3 of rpcbind.
44*ce0e08e2SPeter Avalos  */
45*ce0e08e2SPeter Avalos #include <sys/types.h>
46*ce0e08e2SPeter Avalos #include <rpc/rpc.h>
47*ce0e08e2SPeter Avalos #include <rpc/rpcb_prot.h>
48*ce0e08e2SPeter Avalos #include <netconfig.h>
49*ce0e08e2SPeter Avalos #include <syslog.h>
50*ce0e08e2SPeter Avalos #include <stdlib.h>
51*ce0e08e2SPeter Avalos #include <stdio.h>
52*ce0e08e2SPeter Avalos #include <string.h>
53*ce0e08e2SPeter Avalos 
54*ce0e08e2SPeter Avalos #include "rpcbind.h"
55*ce0e08e2SPeter Avalos 
56*ce0e08e2SPeter Avalos static void	*rpcbproc_getaddr_3_local(void *, struct svc_req *, SVCXPRT *,
57*ce0e08e2SPeter Avalos 					  rpcvers_t);
58*ce0e08e2SPeter Avalos static void	*rpcbproc_dump_3_local(void *, struct svc_req *, SVCXPRT *,
59*ce0e08e2SPeter Avalos 				       rpcvers_t);
60*ce0e08e2SPeter Avalos 
61*ce0e08e2SPeter Avalos /*
62*ce0e08e2SPeter Avalos  * Called by svc_getreqset. There is a separate server handle for
63*ce0e08e2SPeter Avalos  * every transport that it waits on.
64*ce0e08e2SPeter Avalos  */
65*ce0e08e2SPeter Avalos void
66*ce0e08e2SPeter Avalos rpcb_service_3(struct svc_req *rqstp, SVCXPRT *transp)
67*ce0e08e2SPeter Avalos {
68*ce0e08e2SPeter Avalos 	union {
69*ce0e08e2SPeter Avalos 		RPCB rpcbproc_set_3_arg;
70*ce0e08e2SPeter Avalos 		RPCB rpcbproc_unset_3_arg;
71*ce0e08e2SPeter Avalos 		RPCB rpcbproc_getaddr_3_local_arg;
72*ce0e08e2SPeter Avalos 		struct rpcb_rmtcallargs rpcbproc_callit_3_arg;
73*ce0e08e2SPeter Avalos 		char *rpcbproc_uaddr2taddr_3_arg;
74*ce0e08e2SPeter Avalos 		struct netbuf rpcbproc_taddr2uaddr_3_arg;
75*ce0e08e2SPeter Avalos 	} argument;
76*ce0e08e2SPeter Avalos 	char *result;
77*ce0e08e2SPeter Avalos 	xdrproc_t xdr_argument, xdr_result;
78*ce0e08e2SPeter Avalos 	void *(*local)(void *, struct svc_req *, SVCXPRT *, rpcvers_t);
79*ce0e08e2SPeter Avalos 
80*ce0e08e2SPeter Avalos 	rpcbs_procinfo(RPCBVERS_3_STAT, rqstp->rq_proc);
81*ce0e08e2SPeter Avalos 
82*ce0e08e2SPeter Avalos 	switch (rqstp->rq_proc) {
83*ce0e08e2SPeter Avalos 	case NULLPROC:
84*ce0e08e2SPeter Avalos 		/*
85*ce0e08e2SPeter Avalos 		 * Null proc call
86*ce0e08e2SPeter Avalos 		 */
87*ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
88*ce0e08e2SPeter Avalos 		if (debugging)
89*ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_NULL\n");
90*ce0e08e2SPeter Avalos #endif
91*ce0e08e2SPeter Avalos 		/* This call just logs, no actual checks */
92*ce0e08e2SPeter Avalos 		check_access(transp, rqstp->rq_proc, NULL, RPCBVERS);
93*ce0e08e2SPeter Avalos 		svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
94*ce0e08e2SPeter Avalos 		return;
95*ce0e08e2SPeter Avalos 
96*ce0e08e2SPeter Avalos 	case RPCBPROC_SET:
97*ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t )xdr_rpcb;
98*ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t )xdr_bool;
99*ce0e08e2SPeter Avalos 		local = rpcbproc_set_com;
100*ce0e08e2SPeter Avalos 		break;
101*ce0e08e2SPeter Avalos 
102*ce0e08e2SPeter Avalos 	case RPCBPROC_UNSET:
103*ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_rpcb;
104*ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_bool;
105*ce0e08e2SPeter Avalos 		local = rpcbproc_unset_com;
106*ce0e08e2SPeter Avalos 		break;
107*ce0e08e2SPeter Avalos 
108*ce0e08e2SPeter Avalos 	case RPCBPROC_GETADDR:
109*ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_rpcb;
110*ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_wrapstring;
111*ce0e08e2SPeter Avalos 		local = rpcbproc_getaddr_3_local;
112*ce0e08e2SPeter Avalos 		break;
113*ce0e08e2SPeter Avalos 
114*ce0e08e2SPeter Avalos 	case RPCBPROC_DUMP:
115*ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
116*ce0e08e2SPeter Avalos 		if (debugging)
117*ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_DUMP\n");
118*ce0e08e2SPeter Avalos #endif
119*ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_void;
120*ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_rpcblist_ptr;
121*ce0e08e2SPeter Avalos 		local = rpcbproc_dump_3_local;
122*ce0e08e2SPeter Avalos 		break;
123*ce0e08e2SPeter Avalos 
124*ce0e08e2SPeter Avalos 	case RPCBPROC_CALLIT:
125*ce0e08e2SPeter Avalos 		rpcbproc_callit_com(rqstp, transp, rqstp->rq_proc, RPCBVERS);
126*ce0e08e2SPeter Avalos 		return;
127*ce0e08e2SPeter Avalos 
128*ce0e08e2SPeter Avalos 	case RPCBPROC_GETTIME:
129*ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
130*ce0e08e2SPeter Avalos 		if (debugging)
131*ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_GETTIME\n");
132*ce0e08e2SPeter Avalos #endif
133*ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_void;
134*ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_u_long;
135*ce0e08e2SPeter Avalos 		local = rpcbproc_gettime_com;
136*ce0e08e2SPeter Avalos 		break;
137*ce0e08e2SPeter Avalos 
138*ce0e08e2SPeter Avalos 	case RPCBPROC_UADDR2TADDR:
139*ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
140*ce0e08e2SPeter Avalos 		if (debugging)
141*ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_UADDR2TADDR\n");
142*ce0e08e2SPeter Avalos #endif
143*ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_wrapstring;
144*ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_netbuf;
145*ce0e08e2SPeter Avalos 		local = rpcbproc_uaddr2taddr_com;
146*ce0e08e2SPeter Avalos 		break;
147*ce0e08e2SPeter Avalos 
148*ce0e08e2SPeter Avalos 	case RPCBPROC_TADDR2UADDR:
149*ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
150*ce0e08e2SPeter Avalos 		if (debugging)
151*ce0e08e2SPeter Avalos 			fprintf(stderr, "RPCBPROC_TADDR2UADDR\n");
152*ce0e08e2SPeter Avalos #endif
153*ce0e08e2SPeter Avalos 		xdr_argument = (xdrproc_t)xdr_netbuf;
154*ce0e08e2SPeter Avalos 		xdr_result = (xdrproc_t)xdr_wrapstring;
155*ce0e08e2SPeter Avalos 		local = rpcbproc_taddr2uaddr_com;
156*ce0e08e2SPeter Avalos 		break;
157*ce0e08e2SPeter Avalos 
158*ce0e08e2SPeter Avalos 	default:
159*ce0e08e2SPeter Avalos 		svcerr_noproc(transp);
160*ce0e08e2SPeter Avalos 		return;
161*ce0e08e2SPeter Avalos 	}
162*ce0e08e2SPeter Avalos 	memset((char *)&argument, 0, sizeof (argument));
163*ce0e08e2SPeter Avalos 	if (!svc_getargs(transp, (xdrproc_t) xdr_argument,
164*ce0e08e2SPeter Avalos 				(char *) &argument)) {
165*ce0e08e2SPeter Avalos 		svcerr_decode(transp);
166*ce0e08e2SPeter Avalos 		if (debugging)
167*ce0e08e2SPeter Avalos 			fprintf(stderr, "rpcbind: could not decode\n");
168*ce0e08e2SPeter Avalos 		return;
169*ce0e08e2SPeter Avalos 	}
170*ce0e08e2SPeter Avalos 	if (!check_access(transp, rqstp->rq_proc, &argument, RPCBVERS)) {
171*ce0e08e2SPeter Avalos 		svcerr_weakauth(transp);
172*ce0e08e2SPeter Avalos 		goto done;
173*ce0e08e2SPeter Avalos 	}
174*ce0e08e2SPeter Avalos 	result = (*local)(&argument, rqstp, transp, RPCBVERS);
175*ce0e08e2SPeter Avalos 	if (result != NULL && !svc_sendreply(transp, (xdrproc_t)xdr_result,
176*ce0e08e2SPeter Avalos 						result)) {
177*ce0e08e2SPeter Avalos 		svcerr_systemerr(transp);
178*ce0e08e2SPeter Avalos 		if (debugging) {
179*ce0e08e2SPeter Avalos 			fprintf(stderr, "rpcbind: svc_sendreply\n");
180*ce0e08e2SPeter Avalos 			if (doabort) {
181*ce0e08e2SPeter Avalos 				rpcbind_abort();
182*ce0e08e2SPeter Avalos 			}
183*ce0e08e2SPeter Avalos 		}
184*ce0e08e2SPeter Avalos 	}
185*ce0e08e2SPeter Avalos done:
186*ce0e08e2SPeter Avalos 	if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)
187*ce0e08e2SPeter Avalos 				&argument)) {
188*ce0e08e2SPeter Avalos 		if (debugging) {
189*ce0e08e2SPeter Avalos 			fprintf(stderr, "unable to free arguments\n");
190*ce0e08e2SPeter Avalos 			if (doabort) {
191*ce0e08e2SPeter Avalos 				rpcbind_abort();
192*ce0e08e2SPeter Avalos 			}
193*ce0e08e2SPeter Avalos 		}
194*ce0e08e2SPeter Avalos 	}
195*ce0e08e2SPeter Avalos }
196*ce0e08e2SPeter Avalos 
197*ce0e08e2SPeter Avalos /*
198*ce0e08e2SPeter Avalos  * Lookup the mapping for a program, version and return its
199*ce0e08e2SPeter Avalos  * address. Assuming that the caller wants the address of the
200*ce0e08e2SPeter Avalos  * server running on the transport on which the request came.
201*ce0e08e2SPeter Avalos  *
202*ce0e08e2SPeter Avalos  * We also try to resolve the universal address in terms of
203*ce0e08e2SPeter Avalos  * address of the caller.
204*ce0e08e2SPeter Avalos  */
205*ce0e08e2SPeter Avalos /* ARGSUSED */
206*ce0e08e2SPeter Avalos static void *
207*ce0e08e2SPeter Avalos rpcbproc_getaddr_3_local(void *arg, struct svc_req *rqstp __unused,
208*ce0e08e2SPeter Avalos 			 SVCXPRT *transp __unused, rpcvers_t versnum __unused)
209*ce0e08e2SPeter Avalos {
210*ce0e08e2SPeter Avalos 	RPCB *regp = (RPCB *)arg;
211*ce0e08e2SPeter Avalos #ifdef RPCBIND_DEBUG
212*ce0e08e2SPeter Avalos 	if (debugging) {
213*ce0e08e2SPeter Avalos 		char *uaddr;
214*ce0e08e2SPeter Avalos 
215*ce0e08e2SPeter Avalos 		uaddr = taddr2uaddr(rpcbind_get_conf(transp->xp_netid),
216*ce0e08e2SPeter Avalos 			    svc_getrpccaller(transp));
217*ce0e08e2SPeter Avalos 		fprintf(stderr, "RPCB_GETADDR req for (%lu, %lu, %s) from %s: ",
218*ce0e08e2SPeter Avalos 		    (unsigned long)regp->r_prog, (unsigned long)regp->r_vers,
219*ce0e08e2SPeter Avalos 		    regp->r_netid, uaddr);
220*ce0e08e2SPeter Avalos 		free(uaddr);
221*ce0e08e2SPeter Avalos 	}
222*ce0e08e2SPeter Avalos #endif
223*ce0e08e2SPeter Avalos 	return (rpcbproc_getaddr_com(regp, rqstp, transp, RPCBVERS,
224*ce0e08e2SPeter Avalos 	    RPCB_ALLVERS));
225*ce0e08e2SPeter Avalos }
226*ce0e08e2SPeter Avalos 
227*ce0e08e2SPeter Avalos /* ARGSUSED */
228*ce0e08e2SPeter Avalos static void *
229*ce0e08e2SPeter Avalos rpcbproc_dump_3_local(void *arg __unused, struct svc_req *rqstp __unused,
230*ce0e08e2SPeter Avalos 		      SVCXPRT *transp __unused, rpcvers_t versnum __unused)
231*ce0e08e2SPeter Avalos {
232*ce0e08e2SPeter Avalos 	return ((void *)&list_rbl);
233*ce0e08e2SPeter Avalos }
234