1 /*	$OpenBSD: portmap.c,v 1.34 2005/05/22 21:19:39 henning Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 Theo de Raadt (OpenBSD). All rights reserved.
5  * Copyright (c) 1990 The Regents of the University of California.
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
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 
35 __COPYRIGHT("@(#) Copyright (c) 1990 The Regents of the University of California.\n\
36  All rights reserved.\n");
37 __SCCSID("@(#)portmap.c	5.4 (Berkeley) 4/19/91");
38 __RCSID("$MirOS: src/usr.sbin/portmap/portmap.c,v 1.2 2012/09/02 21:06:52 tg Exp $");
39 __SCCSID("@(#)portmap.c	2.3 88/08/11 4.0 RPCSRC");
40 __SCCSID("@(#)portmap.c 1.32 87/08/06 Copyr 1984 Sun Micro");
41 
42 /*
43  * portmap.c, Implements the program,version to port number mapping for
44  * rpc.
45  */
46 
47 /*
48  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
49  * unrestricted use provided that this legend is included on all tape
50  * media and as a part of the software program in whole or part.  Users
51  * may copy or modify Sun RPC without charge, but are not authorized
52  * to license or distribute it to anyone else except as part of a product or
53  * program developed by the user.
54  *
55  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
56  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
57  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
58  *
59  * Sun RPC is provided with no support and without any obligation on the
60  * part of Sun Microsystems, Inc. to assist in its use, correction,
61  * modification or enhancement.
62  *
63  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
64  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
65  * OR ANY PART THEREOF.
66  *
67  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
68  * or profits or other special, indirect and consequential damages, even if
69  * Sun has been advised of the possibility of such damages.
70  *
71  * Sun Microsystems, Inc.
72  * 2550 Garcia Avenue
73  * Mountain View, California  94043
74  */
75 
76 #include <rpc/rpc.h>
77 #include <rpc/pmap_prot.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <syslog.h>
82 #include <unistd.h>
83 #include <netdb.h>
84 #include <pwd.h>
85 #include <errno.h>
86 #include <sys/socket.h>
87 #include <sys/ioctl.h>
88 #include <sys/wait.h>
89 #include <sys/signal.h>
90 #include <sys/resource.h>
91 #include <rpcsvc/nfs_prot.h>
92 #include <arpa/inet.h>
93 
94 void reg_service(struct svc_req *, SVCXPRT *);
95 void reap(int);
96 void callit(struct svc_req *, SVCXPRT *);
97 int check_callit(struct sockaddr_in *, u_long, u_long, u_long);
98 
99 struct pmaplist *pmaplist;
100 int debugging = 0;
101 
102 SVCXPRT *ludpxprt, *ltcpxprt;
103 
104 int
main(int argc,char * argv[])105 main(int argc, char *argv[])
106 {
107 	int sock, lsock, c, on = 1, len = sizeof(struct sockaddr_in);
108 	struct sockaddr_in addr, laddr;
109 	struct pmaplist *pml;
110 	struct passwd *pw;
111 	SVCXPRT *xprt;
112 
113 	while ((c = getopt(argc, argv, "d")) != -1) {
114 		switch (c) {
115 
116 		case 'd':
117 			debugging = 1;
118 			break;
119 
120 		default:
121 			(void) fprintf(stderr, "usage: %s [-d]\n", argv[0]);
122 			exit(1);
123 		}
124 	}
125 
126 	if (!debugging && daemon(0, 0)) {
127 		(void) fprintf(stderr, "portmap: fork: %s", strerror(errno));
128 		exit(1);
129 	}
130 
131 	openlog("portmap", LOG_NDELAY | (debugging ? LOG_PID | LOG_PERROR : LOG_PID),
132 	    LOG_DAEMON);
133 
134 	bzero((char *)&addr, sizeof addr);
135 	addr.sin_addr.s_addr = 0;
136 	addr.sin_family = AF_INET;
137 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
138 	addr.sin_port = htons(PMAPPORT);
139 
140 	bzero((char *)&laddr, sizeof laddr);
141 	laddr.sin_addr.s_addr = 0;
142 	laddr.sin_family = AF_INET;
143 	laddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
144 	laddr.sin_port = htons(PMAPPORT);
145 
146 	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
147 		syslog(LOG_ERR, "cannot create udp socket: %m");
148 		exit(1);
149 	}
150 	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
151 	if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
152 		syslog(LOG_ERR, "cannot bind udp: %m");
153 		exit(1);
154 	}
155 
156 	if ((xprt = svcudp_create(sock)) == (SVCXPRT *)NULL) {
157 		syslog(LOG_ERR, "couldn't do udp_create");
158 		exit(1);
159 	}
160 
161 	if ((lsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
162 		syslog(LOG_ERR, "cannot create udp socket: %m");
163 		exit(1);
164 	}
165 	setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
166 	if (bind(lsock, (struct sockaddr *)&laddr, len) != 0) {
167 		syslog(LOG_ERR, "cannot bind local udp: %m");
168 		exit(1);
169 	}
170 
171 	if ((ludpxprt = svcudp_create(lsock)) == (SVCXPRT *)NULL) {
172 		syslog(LOG_ERR, "couldn't do udp_create");
173 		exit(1);
174 	}
175 
176 	/* make an entry for ourself */
177 	pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
178 	if (pml == NULL) {
179 		syslog(LOG_ERR, "out of memory");
180 		exit(1);
181 	}
182 	pml->pml_next = 0;
183 	pml->pml_map.pm_prog = PMAPPROG;
184 	pml->pml_map.pm_vers = PMAPVERS;
185 	pml->pml_map.pm_prot = IPPROTO_UDP;
186 	pml->pml_map.pm_port = PMAPPORT;
187 	pmaplist = pml;
188 
189 	if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
190 		syslog(LOG_ERR, "cannot create tcp socket: %m");
191 		exit(1);
192 	}
193 	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
194 	if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
195 		syslog(LOG_ERR, "cannot bind tcp: %m");
196 		exit(1);
197 	}
198 	if ((xprt = svctcp_create(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE))
199 	    == (SVCXPRT *)NULL) {
200 		syslog(LOG_ERR, "couldn't do tcp_create");
201 		exit(1);
202 	}
203 
204 	if ((lsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
205 		syslog(LOG_ERR, "cannot create tcp socket: %m");
206 		exit(1);
207 	}
208 	setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
209 	if (bind(lsock, (struct sockaddr *)&laddr, len) != 0) {
210 		syslog(LOG_ERR, "cannot bind tcp: %m");
211 		exit(1);
212 	}
213 	if ((ltcpxprt = svctcp_create(lsock, RPCSMALLMSGSIZE,
214 	    RPCSMALLMSGSIZE)) == (SVCXPRT *)NULL) {
215 		syslog(LOG_ERR, "couldn't do tcp_create");
216 		exit(1);
217 	}
218 
219 	/* make an entry for ourself */
220 	pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
221 	if (pml == NULL) {
222 		syslog(LOG_ERR, "out of memory");
223 		exit(1);
224 	}
225 	pml->pml_map.pm_prog = PMAPPROG;
226 	pml->pml_map.pm_vers = PMAPVERS;
227 	pml->pml_map.pm_prot = IPPROTO_TCP;
228 	pml->pml_map.pm_port = PMAPPORT;
229 	pml->pml_next = pmaplist;
230 	pmaplist = pml;
231 
232 	pw = getpwnam("_portmap");
233 	if (!pw)
234 		pw = getpwnam("nobody");
235 	if (chroot("/var/empty") == -1) {
236 		syslog(LOG_ERR, "cannot chdir to /var/empty.");
237 		exit(1);
238 	}
239 	chdir("/");
240 	if (pw) {
241 		if (setgroups(1, &pw->pw_gid) == -1 ||
242 		    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
243 		    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1) {
244 			syslog(LOG_ERR, "revoke privs: %s", strerror(errno));
245 			exit(1);
246 		}
247 	}
248 	endpwent();
249 
250 	if (svc_register(xprt, PMAPPROG, PMAPVERS, reg_service, FALSE) == 0) {
251 		syslog(LOG_ERR, "svc_register failed.");
252 		exit(1);
253 	}
254 
255 	(void)signal(SIGCHLD, (void (*)(int))reap);
256 	svc_run();
257 	syslog(LOG_ERR, "svc_run returned unexpectedly");
258 	abort();
259 }
260 
261 #ifndef lint
262 /* need to override perror calls in rpc library */
263 void
perror(const char * what)264 perror(const char *what)
265 {
266 
267 	syslog(LOG_ERR, "%s: %m", what);
268 }
269 #endif
270 
271 struct pmaplist *
find_service(u_long prog,u_long vers,u_long prot)272 find_service(u_long prog, u_long vers, u_long prot)
273 {
274 	struct pmaplist *hit = NULL;
275 	struct pmaplist *pml;
276 
277 	for (pml = pmaplist; pml != NULL; pml = pml->pml_next) {
278 		if ((pml->pml_map.pm_prog != prog) ||
279 		    (pml->pml_map.pm_prot != prot))
280 			continue;
281 		hit = pml;
282 		if (pml->pml_map.pm_vers == vers)
283 			break;
284 	}
285 	return (hit);
286 }
287 
288 /*
289  * 1 OK, 0 not
290  */
291 void
reg_service(struct svc_req * rqstp,SVCXPRT * xprt)292 reg_service(struct svc_req *rqstp, SVCXPRT *xprt)
293 {
294 	struct pmap reg;
295 	struct pmaplist *pml, *prevpml, *fnd;
296 	struct sockaddr_in *fromsin;
297 	long ans = 0, port;
298 	caddr_t t;
299 
300 	fromsin = svc_getcaller(xprt);
301 
302 	if (debugging)
303 		(void) fprintf(stderr, "server: about to do a switch\n");
304 	switch (rqstp->rq_proc) {
305 
306 	case PMAPPROC_NULL:
307 		/*
308 		 * Null proc call
309 		 */
310 		if (!svc_sendreply(xprt, xdr_void, (caddr_t)0) && debugging) {
311 			abort();
312 		}
313 		break;
314 
315 	case PMAPPROC_SET:
316 		/*
317 		 * Set a program,version to port mapping
318 		 */
319 		if (xprt != ltcpxprt && xprt != ludpxprt) {
320 			syslog(LOG_WARNING,
321 			    "non-local set attempt (might be from %s)",
322 			    inet_ntoa(fromsin->sin_addr));
323 			svcerr_noproc(xprt);
324 			return;
325 		}
326 		if (!svc_getargs(xprt, xdr_pmap, (caddr_t)&reg)) {
327 			svcerr_decode(xprt);
328 			break;
329 		}
330 
331 		/*
332 		 * check to see if already used
333 		 * find_service returns a hit even if
334 		 * the versions don't match, so check for it
335 		 */
336 		fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
337 		if (fnd && fnd->pml_map.pm_vers == reg.pm_vers) {
338 			if (fnd->pml_map.pm_port == reg.pm_port)
339 				ans = 1;
340 			goto done;
341 		}
342 
343 		if (debugging)
344 			printf("set: prog %lu vers %lu port %lu\n",
345 			    reg.pm_prog, reg.pm_vers, reg.pm_port);
346 
347 		if (reg.pm_port & ~0xffff)
348 			goto done;
349 
350 		/*
351 		 * only permit localhost root to create
352 		 * mappings pointing at sensitive ports
353 		 */
354 		if ((reg.pm_port < IPPORT_RESERVED ||
355 		    reg.pm_port == NFS_PORT) &&
356 		    htons(fromsin->sin_port) >= IPPORT_RESERVED) {
357 			syslog(LOG_WARNING,
358 			    "resvport set attempt by non-root");
359 			goto done;
360 		}
361 
362 		/*
363 		 * add to END of list
364 		 */
365 		pml = (struct pmaplist *)malloc(sizeof(struct pmaplist));
366 		if (pml == NULL) {
367 			syslog(LOG_ERR, "out of memory");
368 			svcerr_systemerr(xprt);
369 			return;
370 		}
371 
372 		pml->pml_map = reg;
373 		pml->pml_next = 0;
374 		if (pmaplist == 0) {
375 			pmaplist = pml;
376 		} else {
377 			for (fnd = pmaplist; fnd->pml_next != 0;
378 			    fnd = fnd->pml_next);
379 			fnd->pml_next = pml;
380 		}
381 		ans = 1;
382 	done:
383 		if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
384 		    debugging) {
385 			(void) fprintf(stderr, "svc_sendreply\n");
386 			abort();
387 		}
388 		break;
389 
390 	case PMAPPROC_UNSET:
391 		/*
392 		 * Remove a program,version to port mapping.
393 		 */
394 		if (xprt != ltcpxprt && xprt != ludpxprt) {
395 			syslog(LOG_WARNING,
396 			    "non-local unset attempt (might be from %s)",
397 			    inet_ntoa(fromsin->sin_addr));
398 			svcerr_noproc(xprt);
399 			return;
400 		}
401 		if (!svc_getargs(xprt, xdr_pmap, (caddr_t)&reg)) {
402 			svcerr_decode(xprt);
403 			break;
404 		}
405 		for (prevpml = NULL, pml = pmaplist; pml != NULL; ) {
406 			if ((pml->pml_map.pm_prog != reg.pm_prog) ||
407 			    (pml->pml_map.pm_vers != reg.pm_vers)) {
408 				/* both pml & prevpml move forwards */
409 				prevpml = pml;
410 				pml = pml->pml_next;
411 				continue;
412 			}
413 			if ((pml->pml_map.pm_port < IPPORT_RESERVED ||
414 			    pml->pml_map.pm_port == NFS_PORT) &&
415 			    htons(fromsin->sin_port) >= IPPORT_RESERVED) {
416 				syslog(LOG_WARNING,
417 				    "resvport unset attempt by non-root");
418 				break;
419 			}
420 
421 			/* found it; pml moves forward, prevpml stays */
422 			ans = 1;
423 			t = (caddr_t)pml;
424 			pml = pml->pml_next;
425 			if (prevpml == NULL)
426 				pmaplist = pml;
427 			else
428 				prevpml->pml_next = pml;
429 			free(t);
430 		}
431 		if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
432 		    debugging) {
433 			fprintf(stderr, "svc_sendreply\n");
434 			abort();
435 		}
436 		break;
437 
438 	case PMAPPROC_GETPORT:
439 		/*
440 		 * Lookup the mapping for a program,version and return its port
441 		 */
442 		if (!svc_getargs(xprt, xdr_pmap, (caddr_t)&reg)) {
443 			svcerr_decode(xprt);
444 			break;
445 		}
446 		fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
447 		if (fnd)
448 			port = fnd->pml_map.pm_port;
449 		else
450 			port = 0;
451 		if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&port)) &&
452 		    debugging) {
453 			fprintf(stderr, "svc_sendreply\n");
454 			abort();
455 		}
456 		break;
457 
458 	case PMAPPROC_DUMP:
459 		/*
460 		 * Return the current set of mapped program,version
461 		 */
462 		if (!svc_getargs(xprt, xdr_void, NULL)) {
463 			svcerr_decode(xprt);
464 			break;
465 		}
466 		if (!svc_sendreply(xprt, xdr_pmaplist, (caddr_t)&pmaplist) &&
467 		    debugging) {
468 			fprintf(stderr, "svc_sendreply\n");
469 			abort();
470 		}
471 		break;
472 
473 	case PMAPPROC_CALLIT:
474 		/*
475 		 * Calls a procedure on the local machine.  If the requested
476 		 * procedure is not registered this procedure does not return
477 		 * error information!!
478 		 * This procedure is only supported on rpc/udp and calls via
479 		 * rpc/udp.  It passes null authentication parameters.
480 		 */
481 		callit(rqstp, xprt);
482 		break;
483 
484 	default:
485 		svcerr_noproc(xprt);
486 		break;
487 	}
488 }
489 
490 
491 /*
492  * Stuff for the rmtcall service
493  */
494 #define ARGSIZE 9000
495 
496 struct encap_parms {
497 	u_int arglen;
498 	char *args;
499 };
500 
501 static bool_t
xdr_encap_parms(XDR * xdrs,struct encap_parms * epp)502 xdr_encap_parms(XDR *xdrs, struct encap_parms *epp)
503 {
504 
505 	return (xdr_bytes(xdrs, &(epp->args), &(epp->arglen), ARGSIZE));
506 }
507 
508 struct rmtcallargs {
509 	u_long	rmt_prog;
510 	u_long	rmt_vers;
511 	u_long	rmt_port;
512 	u_long	rmt_proc;
513 	struct encap_parms rmt_args;
514 };
515 
516 static bool_t
xdr_rmtcall_args(XDR * xdrs,struct rmtcallargs * cap)517 xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
518 {
519 
520 	/* does not get a port number */
521 	if (xdr_u_long(xdrs, &(cap->rmt_prog)) &&
522 	    xdr_u_long(xdrs, &(cap->rmt_vers)) &&
523 	    xdr_u_long(xdrs, &(cap->rmt_proc))) {
524 		return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
525 	}
526 	return (FALSE);
527 }
528 
529 static bool_t
xdr_rmtcall_result(XDR * xdrs,struct rmtcallargs * cap)530 xdr_rmtcall_result(XDR *xdrs, struct rmtcallargs *cap)
531 {
532 	if (xdr_u_long(xdrs, &(cap->rmt_port)))
533 		return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
534 	return (FALSE);
535 }
536 
537 /*
538  * only worries about the struct encap_parms part of struct rmtcallargs.
539  * The arglen must already be set!!
540  */
541 static bool_t
xdr_opaque_parms(XDR * xdrs,struct rmtcallargs * cap)542 xdr_opaque_parms(XDR *xdrs, struct rmtcallargs *cap)
543 {
544 
545 	return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
546 }
547 
548 /*
549  * This routine finds and sets the length of incoming opaque paraters
550  * and then calls xdr_opaque_parms.
551  */
552 static bool_t
xdr_len_opaque_parms(XDR * xdrs,struct rmtcallargs * cap)553 xdr_len_opaque_parms(XDR *xdrs, struct rmtcallargs *cap)
554 {
555 	u_int beginpos, lowpos, highpos, currpos, pos;
556 
557 	beginpos = lowpos = pos = xdr_getpos(xdrs);
558 	highpos = lowpos + ARGSIZE;
559 	while ((int)(highpos - lowpos) >= 0) {
560 		currpos = (lowpos + highpos) / 2;
561 		if (xdr_setpos(xdrs, currpos)) {
562 			pos = currpos;
563 			lowpos = currpos + 1;
564 		} else {
565 			highpos = currpos - 1;
566 		}
567 	}
568 	xdr_setpos(xdrs, beginpos);
569 	cap->rmt_args.arglen = pos - beginpos;
570 	return (xdr_opaque_parms(xdrs, cap));
571 }
572 
573 /*
574  * Call a remote procedure service
575  * This procedure is very quiet when things go wrong.
576  * The proc is written to support broadcast rpc.  In the broadcast case,
577  * a machine should shut-up instead of complain, less the requestor be
578  * overrun with complaints at the expense of not hearing a valid reply ...
579  *
580  * This now forks so that the program & process that it calls can call
581  * back to the portmapper.
582  */
583 void
callit(struct svc_req * rqstp,SVCXPRT * xprt)584 callit(struct svc_req *rqstp, SVCXPRT *xprt)
585 {
586 	struct rmtcallargs a;
587 	struct pmaplist *pml;
588 	u_short port;
589 	struct sockaddr_in me;
590 	pid_t pid;
591 	int so = -1, dontblock = 1;
592 	CLIENT *client;
593 	struct authunix_parms *au = (struct authunix_parms *)rqstp->rq_clntcred;
594 	struct timeval timeout;
595 	char buf[ARGSIZE];
596 
597 	timeout.tv_sec = 5;
598 	timeout.tv_usec = 0;
599 	a.rmt_args.args = buf;
600 	if (!svc_getargs(xprt, xdr_rmtcall_args, (caddr_t)&a))
601 		return;
602 	if (!check_callit(svc_getcaller(xprt), rqstp->rq_proc,
603 	    a.rmt_prog, a.rmt_proc))
604 		return;
605 	if ((pml = find_service(a.rmt_prog, a.rmt_vers,
606 	    (u_long)IPPROTO_UDP)) == NULL)
607 		return;
608 
609 	/*
610 	 * fork a child to do the work.  Parent immediately returns.
611 	 * Child exits upon completion.
612 	 */
613 	if ((pid = fork()) != 0) {
614 		if (pid == -1)
615 			syslog(LOG_ERR, "CALLIT (prog %lu): fork: %m",
616 			    a.rmt_prog);
617 		return;
618 	}
619 	port = pml->pml_map.pm_port;
620 	get_myaddress(&me);
621 	me.sin_port = htons(port);
622 
623 	/* Avoid implicit binding to reserved port by clntudp_create() */
624 	so = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
625 	if (so == -1)
626 		exit(1);
627 	if (ioctl(so, FIONBIO, &dontblock) == -1)
628 		exit(1);
629 
630 	client = clntudp_create(&me, a.rmt_prog, a.rmt_vers, timeout, &so);
631 	if (client != (CLIENT *)NULL) {
632 		if (rqstp->rq_cred.oa_flavor == AUTH_UNIX)
633 			client->cl_auth = authunix_create(au->aup_machname,
634 			    au->aup_uid, au->aup_gid, au->aup_len, au->aup_gids);
635 		a.rmt_port = (u_long)port;
636 		if (clnt_call(client, a.rmt_proc, xdr_opaque_parms, &a,
637 		    xdr_len_opaque_parms, &a, timeout) == RPC_SUCCESS)
638 			svc_sendreply(xprt, xdr_rmtcall_result, (caddr_t)&a);
639 		AUTH_DESTROY(client->cl_auth);
640 		clnt_destroy(client);
641 	}
642 	(void)close(so);
643 	exit(0);
644 }
645 
646 /* ARGSUSED */
647 void
reap(int signo)648 reap(int signo)
649 {
650 	int save_errno = errno;
651 
652 	while (wait3(NULL, WNOHANG, NULL) > 0)
653 		;
654 	errno = save_errno;
655 }
656 
657 #define	NFSPROG			((u_long) 100003)
658 #define	MOUNTPROG		((u_long) 100005)
659 #define	YPXPROG			((u_long) 100069)
660 #define	YPPROG			((u_long) 100004)
661 #define	YPPROC_DOMAIN_NONACK	((u_long) 2)
662 #define	MOUNTPROC_MNT		((u_long) 1)
663 #define XXXPROC_NOP		((u_long) 0)
664 
665 int
check_callit(struct sockaddr_in * addr,u_long proc,u_long prog,u_long aproc)666 check_callit(struct sockaddr_in *addr, u_long proc, u_long prog,
667     u_long aproc)
668 {
669 	if ((prog == PMAPPROG && aproc != XXXPROC_NOP) ||
670 	    (prog == NFSPROG && aproc != XXXPROC_NOP) ||
671 	    (prog == YPXPROG && aproc != XXXPROC_NOP) ||
672 	    (prog == MOUNTPROG && aproc == MOUNTPROC_MNT) ||
673 	    (prog == YPPROG && aproc != YPPROC_DOMAIN_NONACK)) {
674 		syslog(LOG_WARNING,
675 		    "callit prog %d aproc %d (might be from %s)",
676 		    (int)prog, (int)aproc, inet_ntoa(addr->sin_addr));
677 		return (FALSE);
678 	}
679 	return (TRUE);
680 }
681