xref: /netbsd-src/usr.sbin/rpcbind/pmap_svc.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: pmap_svc.c,v 1.4 2003/07/13 12:16:05 itojun 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) 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 portmapper.
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 __P((rpcprog_t, rpcvers_t,
62 					       rpcprot_t));
63 static bool_t pmapproc_change __P((struct svc_req *, SVCXPRT *, u_long));
64 static bool_t pmapproc_getport __P((struct svc_req *, SVCXPRT *));
65 static bool_t pmapproc_dump __P((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, SVCXPRT *xprt, unsigned long op)
170 {
171 	struct pmap reg;
172 	RPCB rpcbreg;
173 	long ans;
174 	struct sockaddr_in *who;
175 	struct sockcred *sc;
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 	sc = __svc_getcallercreds(xprt);
197 
198 	/*
199 	 * Can't use getpwnam here. We might end up calling ourselves
200 	 * and looping.
201 	 */
202 	if (sc == NULL)
203 		rpcbreg.r_owner = "unknown";
204 	else if (sc->sc_uid == 0)
205 		rpcbreg.r_owner = "superuser";
206 	else {
207 		/* r_owner will be strdup-ed later */
208 		snprintf(uidbuf, sizeof uidbuf, "%d", sc->sc_uid);
209 		rpcbreg.r_owner = uidbuf;
210 	}
211 
212 	rpcbreg.r_prog = reg.pm_prog;
213 	rpcbreg.r_vers = reg.pm_vers;
214 
215 	if (op == PMAPPROC_SET) {
216 		char buf[32];
217 
218 		snprintf(buf, sizeof(buf), "0.0.0.0.%d.%d",
219 		    (int)((reg.pm_port >> 8) & 0xff),
220 		    (int)(reg.pm_port & 0xff));
221 		rpcbreg.r_addr = buf;
222 		if (reg.pm_prot == IPPROTO_UDP) {
223 			rpcbreg.r_netid = udptrans;
224 		} else if (reg.pm_prot == IPPROTO_TCP) {
225 			rpcbreg.r_netid = tcptrans;
226 		} else {
227 			ans = FALSE;
228 			goto done_change;
229 		}
230 		ans = map_set(&rpcbreg, rpcbreg.r_owner);
231 	} else if (op == PMAPPROC_UNSET) {
232 		bool_t ans1, ans2;
233 
234 		rpcbreg.r_addr = NULL;
235 		rpcbreg.r_netid = tcptrans;
236 		ans1 = map_unset(&rpcbreg, rpcbreg.r_owner);
237 		rpcbreg.r_netid = udptrans;
238 		ans2 = map_unset(&rpcbreg, rpcbreg.r_owner);
239 		ans = ans1 || ans2;
240 	} else {
241 		ans = FALSE;
242 	}
243 done_change:
244 	if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t) &ans)) &&
245 	    debugging) {
246 		fprintf(stderr, "portmap: svc_sendreply\n");
247 		if (doabort) {
248 			rpcbind_abort();
249 		}
250 	}
251 #ifdef RPCBIND_DEBUG
252 	if (debugging)
253 		fprintf(stderr, "%s\n", ans == TRUE ? "succeeded" : "failed");
254 #endif
255 	if (op == PMAPPROC_SET)
256 		rpcbs_set(RPCBVERS_2_STAT, ans);
257 	else
258 		rpcbs_unset(RPCBVERS_2_STAT, ans);
259 	return (TRUE);
260 }
261 
262 /* ARGSUSED */
263 static bool_t
264 pmapproc_getport(struct svc_req *rqstp, SVCXPRT *xprt)
265 {
266 	struct pmap reg;
267 	long lport;
268 	int port = 0;
269 	struct pmaplist *fnd;
270 #ifdef RPCBIND_DEBUG
271 	char *uaddr;
272 #endif
273 
274 	if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (char *)&reg)) {
275 		svcerr_decode(xprt);
276 		return (FALSE);
277 	}
278 
279 	if (!check_access(xprt, PMAPPROC_GETPORT, &reg, PMAPVERS)) {
280 		svcerr_weakauth(xprt);
281 		return FALSE;
282 	}
283 
284 #ifdef RPCBIND_DEBUG
285 	if (debugging) {
286 		uaddr =  taddr2uaddr(rpcbind_get_conf(xprt->xp_netid),
287 			    svc_getrpccaller(xprt));
288 		fprintf(stderr, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :",
289 			reg.pm_prog, reg.pm_vers,
290 			reg.pm_prot == IPPROTO_UDP ? "udp" : "tcp", uaddr);
291 		free(uaddr);
292 	}
293 #endif
294 	fnd = find_service_pmap(reg.pm_prog, reg.pm_vers, reg.pm_prot);
295 	if (fnd) {
296 		char serveuaddr[32], *ua;
297 		int h1, h2, h3, h4, p1, p2;
298 		char *netid;
299 
300 		if (reg.pm_prot == IPPROTO_UDP) {
301 			ua = udp_uaddr;
302 			netid = udptrans;
303 		} else {
304 			ua = tcp_uaddr; /* To get the len */
305 			netid = tcptrans;
306 		}
307 		if (ua == NULL) {
308 			goto sendreply;
309 		}
310 		if (sscanf(ua, "%d.%d.%d.%d.%d.%d", &h1, &h2, &h3,
311 				&h4, &p1, &p2) == 6) {
312 			p1 = (fnd->pml_map.pm_port >> 8) & 0xff;
313 			p2 = (fnd->pml_map.pm_port) & 0xff;
314 			snprintf(serveuaddr, sizeof(serveuaddr),
315 			    "%d.%d.%d.%d.%d.%d", h1, h2, h3, h4, p1, p2);
316 			if (is_bound(netid, serveuaddr)) {
317 				port = fnd->pml_map.pm_port;
318 			} else { /* this service is dead; delete it */
319 				delete_prog(reg.pm_prog);
320 			}
321 		}
322 	}
323 sendreply:
324 	lport = port;
325 	if ((!svc_sendreply(xprt, (xdrproc_t) xdr_long, (caddr_t)&lport)) &&
326 			debugging) {
327 		(void) fprintf(stderr, "portmap: svc_sendreply\n");
328 		if (doabort) {
329 			rpcbind_abort();
330 		}
331 	}
332 #ifdef RPCBIND_DEBUG
333 	if (debugging)
334 		fprintf(stderr, "port = %d\n", port);
335 #endif
336 	rpcbs_getaddr(RPCBVERS_2_STAT, reg.pm_prog, reg.pm_vers,
337 		reg.pm_prot == IPPROTO_UDP ? udptrans : tcptrans,
338 		port ? udptrans : "");
339 
340 	return (TRUE);
341 }
342 
343 /* ARGSUSED */
344 static bool_t
345 pmapproc_dump(struct svc_req *rqstp, SVCXPRT *xprt)
346 {
347 	if (!svc_getargs(xprt, (xdrproc_t)xdr_void, NULL)) {
348 		svcerr_decode(xprt);
349 		return (FALSE);
350 	}
351 
352 	if (!check_access(xprt, PMAPPROC_DUMP, NULL, PMAPVERS)) {
353 		svcerr_weakauth(xprt);
354 		return FALSE;
355 	}
356 
357 	if ((!svc_sendreply(xprt, (xdrproc_t) xdr_pmaplist_ptr,
358 			(caddr_t)&list_pml)) && debugging) {
359 		if (debugging)
360 			(void) fprintf(stderr, "portmap: svc_sendreply\n");
361 		if (doabort) {
362 			rpcbind_abort();
363 		}
364 	}
365 	return (TRUE);
366 }
367 
368 #endif /* PORTMAP */
369