xref: /freebsd/usr.sbin/rpcbind/pmap_svc.c (revision 5b9c547c)
1 /*	$NetBSD: pmap_svc.c,v 1.2 2000/10/20 11:49:40 fvdl Exp $	*/
2 /*	$FreeBSD$ */
3 
4 /*-
5  * Copyright (c) 2009, Sun Microsystems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  * - Redistributions of source code must retain the above copyright notice,
11  *   this list of conditions and the following disclaimer.
12  * - Redistributions in binary form must reproduce the above copyright notice,
13  *   this list of conditions and the following disclaimer in the documentation
14  *   and/or other materials provided with the distribution.
15  * - Neither the name of Sun Microsystems, Inc. nor the names of its
16  *   contributors may be used to endorse or promote products derived
17  *   from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*
32  * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
33  */
34 
35 /* #ident	"@(#)pmap_svc.c	1.14	93/07/05 SMI" */
36 
37 #if 0
38 #ifndef lint
39 static	char sccsid[] = "@(#)pmap_svc.c 1.23 89/04/05 Copyr 1984 Sun Micro";
40 #endif
41 #endif
42 
43 /*
44  * pmap_svc.c
45  * The server procedure for the version 2 portmaper.
46  * All the portmapper related interface from the portmap side.
47  */
48 
49 #ifdef PORTMAP
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <stdio.h>
53 #include <rpc/rpc.h>
54 #include <rpc/pmap_prot.h>
55 #include <rpc/rpcb_prot.h>
56 #ifdef RPCBIND_DEBUG
57 #include <stdlib.h>
58 #endif
59 #include "rpcbind.h"
60 
61 static struct pmaplist *find_service_pmap(rpcprog_t, rpcvers_t,
62 					       rpcprot_t);
63 static bool_t pmapproc_change(struct svc_req *, SVCXPRT *, u_long);
64 static bool_t pmapproc_getport(struct svc_req *, SVCXPRT *);
65 static bool_t pmapproc_dump(struct svc_req *, SVCXPRT *);
66 
67 /*
68  * Called for all the version 2 inquiries.
69  */
70 void
71 pmap_service(struct svc_req *rqstp, SVCXPRT *xprt)
72 {
73 	rpcbs_procinfo(RPCBVERS_2_STAT, rqstp->rq_proc);
74 	switch (rqstp->rq_proc) {
75 	case PMAPPROC_NULL:
76 		/*
77 		 * Null proc call
78 		 */
79 #ifdef RPCBIND_DEBUG
80 		if (debugging)
81 			fprintf(stderr, "PMAPPROC_NULL\n");
82 #endif
83 		check_access(xprt, rqstp->rq_proc, NULL, PMAPVERS);
84 		if ((!svc_sendreply(xprt, (xdrproc_t) xdr_void, NULL)) &&
85 			debugging) {
86 			if (doabort) {
87 				rpcbind_abort();
88 			}
89 		}
90 		break;
91 
92 	case PMAPPROC_SET:
93 		/*
94 		 * Set a program, version to port mapping
95 		 */
96 		pmapproc_change(rqstp, xprt, rqstp->rq_proc);
97 		break;
98 
99 	case PMAPPROC_UNSET:
100 		/*
101 		 * Remove a program, version to port mapping.
102 		 */
103 		pmapproc_change(rqstp, xprt, rqstp->rq_proc);
104 		break;
105 
106 	case PMAPPROC_GETPORT:
107 		/*
108 		 * Lookup the mapping for a program, version and return its
109 		 * port number.
110 		 */
111 		pmapproc_getport(rqstp, xprt);
112 		break;
113 
114 	case PMAPPROC_DUMP:
115 		/*
116 		 * Return the current set of mapped program, version
117 		 */
118 #ifdef RPCBIND_DEBUG
119 		if (debugging)
120 			fprintf(stderr, "PMAPPROC_DUMP\n");
121 #endif
122 		pmapproc_dump(rqstp, xprt);
123 		break;
124 
125 	case PMAPPROC_CALLIT:
126 		/*
127 		 * Calls a procedure on the local machine. If the requested
128 		 * procedure is not registered this procedure does not return
129 		 * error information!!
130 		 * This procedure is only supported on rpc/udp and calls via
131 		 * rpc/udp. It passes null authentication parameters.
132 		 */
133 		rpcbproc_callit_com(rqstp, xprt, PMAPPROC_CALLIT, PMAPVERS);
134 		break;
135 
136 	default:
137 		svcerr_noproc(xprt);
138 		break;
139 	}
140 }
141 
142 /*
143  * returns the item with the given program, version number. If that version
144  * number is not found, it returns the item with that program number, so that
145  * the port number is now returned to the caller. The caller when makes a
146  * call to this program, version number, the call will fail and it will
147  * return with PROGVERS_MISMATCH. The user can then determine the highest
148  * and the lowest version number for this program using clnt_geterr() and
149  * use those program version numbers.
150  */
151 static struct pmaplist *
152 find_service_pmap(rpcprog_t prog, rpcvers_t vers, rpcprot_t prot)
153 {
154 	register struct pmaplist *hit = NULL;
155 	register struct pmaplist *pml;
156 
157 	for (pml = list_pml; pml != NULL; pml = pml->pml_next) {
158 		if ((pml->pml_map.pm_prog != prog) ||
159 			(pml->pml_map.pm_prot != prot))
160 			continue;
161 		hit = pml;
162 		if (pml->pml_map.pm_vers == vers)
163 			break;
164 	}
165 	return (hit);
166 }
167 
168 static bool_t
169 pmapproc_change(struct svc_req *rqstp __unused, SVCXPRT *xprt, unsigned long op)
170 {
171 	struct pmap reg;
172 	RPCB rpcbreg;
173 	long ans;
174 	struct sockaddr_in *who;
175 	uid_t uid;
176 	char uidbuf[32];
177 
178 #ifdef RPCBIND_DEBUG
179 	if (debugging)
180 		fprintf(stderr, "%s request for (%lu, %lu) : ",
181 		    op == PMAPPROC_SET ? "PMAP_SET" : "PMAP_UNSET",
182 		    reg.pm_prog, reg.pm_vers);
183 #endif
184 
185 	if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)&reg)) {
186 		svcerr_decode(xprt);
187 		return (FALSE);
188 	}
189 
190 	if (!check_access(xprt, op, &reg, PMAPVERS)) {
191 		svcerr_weakauth(xprt);
192 		return FALSE;
193 	}
194 
195 	who = svc_getcaller(xprt);
196 
197 	/*
198 	 * Can't use getpwnam here. We might end up calling ourselves
199 	 * and looping.
200 	 */
201 	if (__rpc_get_local_uid(xprt, &uid) < 0)
202 		rpcbreg.r_owner = "unknown";
203 	else if (uid == 0)
204 		rpcbreg.r_owner = "superuser";
205 	else {
206 		/* r_owner will be strdup-ed later */
207 		snprintf(uidbuf, sizeof uidbuf, "%d", uid);
208 		rpcbreg.r_owner = uidbuf;
209 	}
210 
211 	rpcbreg.r_prog = reg.pm_prog;
212 	rpcbreg.r_vers = reg.pm_vers;
213 
214 	if (op == PMAPPROC_SET) {
215 		char buf[32];
216 
217 		snprintf(buf, sizeof buf, "0.0.0.0.%d.%d",
218 		    (int)((reg.pm_port >> 8) & 0xff),
219 		    (int)(reg.pm_port & 0xff));
220 		rpcbreg.r_addr = buf;
221 		if (reg.pm_prot == IPPROTO_UDP) {
222 			rpcbreg.r_netid = udptrans;
223 		} else if (reg.pm_prot == IPPROTO_TCP) {
224 			rpcbreg.r_netid = tcptrans;
225 		} else {
226 			ans = FALSE;
227 			goto done_change;
228 		}
229 		ans = map_set(&rpcbreg, rpcbreg.r_owner);
230 	} else if (op == PMAPPROC_UNSET) {
231 		bool_t ans1, ans2;
232 
233 		rpcbreg.r_addr = NULL;
234 		rpcbreg.r_netid = tcptrans;
235 		ans1 = map_unset(&rpcbreg, rpcbreg.r_owner);
236 		rpcbreg.r_netid = udptrans;
237 		ans2 = map_unset(&rpcbreg, rpcbreg.r_owner);
238 		ans = ans1 || ans2;
239 	} else {
240 		ans = FALSE;
241 	}
242 done_change:
243 	if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t) &ans)) &&
244 	    debugging) {
245 		fprintf(stderr, "portmap: svc_sendreply\n");
246 		if (doabort) {
247 			rpcbind_abort();
248 		}
249 	}
250 #ifdef RPCBIND_DEBUG
251 	if (debugging)
252 		fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
253 #endif
254 	if (op == PMAPPROC_SET)
255 		rpcbs_set(RPCBVERS_2_STAT, ans);
256 	else
257 		rpcbs_unset(RPCBVERS_2_STAT, ans);
258 	return (TRUE);
259 }
260 
261 /* ARGSUSED */
262 static bool_t
263 pmapproc_getport(struct svc_req *rqstp __unused, SVCXPRT *xprt)
264 {
265 	struct pmap reg;
266 	long lport;
267 	int port = 0;
268 	struct pmaplist *fnd;
269 #ifdef RPCBIND_DEBUG
270 	char *uaddr;
271 #endif
272 
273 	if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)&reg)) {
274 		svcerr_decode(xprt);
275 		return (FALSE);
276 	}
277 
278 	if (!check_access(xprt, PMAPPROC_GETPORT, &reg, PMAPVERS)) {
279 		svcerr_weakauth(xprt);
280 		return FALSE;
281 	}
282 
283 #ifdef RPCBIND_DEBUG
284 	if (debugging) {
285 		uaddr =  taddr2uaddr(rpcbind_get_conf(xprt->xp_netid),
286 			    svc_getrpccaller(xprt));
287 		fprintf(stderr, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :",
288 			reg.pm_prog, reg.pm_vers,
289 			reg.pm_prot == IPPROTO_UDP ? "udp" : "tcp", uaddr);
290 		free(uaddr);
291 	}
292 #endif
293 	fnd = find_service_pmap(reg.pm_prog, reg.pm_vers, reg.pm_prot);
294 	if (fnd) {
295 		char serveuaddr[32], *ua;
296 		int h1, h2, h3, h4, p1, p2;
297 		char *netid;
298 
299 		if (reg.pm_prot == IPPROTO_UDP) {
300 			ua = udp_uaddr;
301 			netid = udptrans;
302 		} else {
303 			ua = tcp_uaddr; /* To get the len */
304 			netid = tcptrans;
305 		}
306 		if (ua == NULL) {
307 			goto sendreply;
308 		}
309 		if (sscanf(ua, "%d.%d.%d.%d.%d.%d", &h1, &h2, &h3,
310 				&h4, &p1, &p2) == 6) {
311 			p1 = (fnd->pml_map.pm_port >> 8) & 0xff;
312 			p2 = (fnd->pml_map.pm_port) & 0xff;
313 			snprintf(serveuaddr, sizeof serveuaddr,
314 			    "%d.%d.%d.%d.%d.%d", h1, h2, h3, h4, p1, p2);
315 			if (is_bound(netid, serveuaddr)) {
316 				port = fnd->pml_map.pm_port;
317 			} else { /* this service is dead; delete it */
318 				delete_prog(reg.pm_prog);
319 			}
320 		}
321 	}
322 sendreply:
323 	lport = port;
324 	if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t)&lport)) &&
325 			debugging) {
326 		(void) fprintf(stderr, "portmap: svc_sendreply\n");
327 		if (doabort) {
328 			rpcbind_abort();
329 		}
330 	}
331 #ifdef RPCBIND_DEBUG
332 	if (debugging)
333 		fprintf(stderr, "port = %d\n", port);
334 #endif
335 	rpcbs_getaddr(RPCBVERS_2_STAT, reg.pm_prog, reg.pm_vers,
336 		reg.pm_prot == IPPROTO_UDP ? udptrans : tcptrans,
337 		port ? udptrans : "");
338 
339 	return (TRUE);
340 }
341 
342 /* ARGSUSED */
343 static bool_t
344 pmapproc_dump(struct svc_req *rqstp __unused, SVCXPRT *xprt)
345 {
346 	if (!svc_getargs(xprt, (xdrproc_t)xdr_void, NULL)) {
347 		svcerr_decode(xprt);
348 		return (FALSE);
349 	}
350 
351 	if (!check_access(xprt, PMAPPROC_DUMP, NULL, PMAPVERS)) {
352 		svcerr_weakauth(xprt);
353 		return FALSE;
354 	}
355 
356 	if ((!svc_sendreply(xprt, (xdrproc_t) xdr_pmaplist_ptr,
357 			(caddr_t)&list_pml)) && debugging) {
358 		if (debugging)
359 			(void) fprintf(stderr, "portmap: svc_sendreply\n");
360 		if (doabort) {
361 			rpcbind_abort();
362 		}
363 	}
364 	return (TRUE);
365 }
366 
367 #endif /* PORTMAP */
368