]> git.neil.brown.name Git - portmap.git/blob - portmap.c
Only fail an 'unregister' attempt if nothing can be unregistered.
[portmap.git] / portmap.c
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39
40 /*
41 @(#)portmap.c   2.3 88/08/11 4.0 RPCSRC
42 static char sccsid[] = "@(#)portmap.c 1.32 87/08/06 Copyr 1984 Sun Micro";
43 */
44
45 /*
46  * portmap.c, Implements the program,version to port number mapping for
47  * rpc.
48  */
49
50 /*
51  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
52  * unrestricted use provided that this legend is included on all tape
53  * media and as a part of the software program in whole or part.  Users
54  * may copy or modify Sun RPC without charge, but are not authorized
55  * to license or distribute it to anyone else except as part of a product or
56  * program developed by the user.
57  * 
58  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
59  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
60  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
61  * 
62  * Sun RPC is provided with no support and without any obligation on the
63  * part of Sun Microsystems, Inc. to assist in its use, correction,
64  * modification or enhancement.
65  * 
66  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
67  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
68  * OR ANY PART THEREOF.
69  * 
70  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
71  * or profits or other special, indirect and consequential damages, even if
72  * Sun has been advised of the possibility of such damages.
73  * 
74  * Sun Microsystems, Inc.
75  * 2550 Garcia Avenue
76  * Mountain View, California  94043
77  */
78
79 #include <rpc/rpc.h>
80 #include <rpc/pmap_prot.h>
81 #include <stdio.h>
82 #include <unistd.h>
83 #include <syslog.h>
84 #include <netdb.h>
85 #include <errno.h>
86 #include <fcntl.h>
87 #include <sys/socket.h>
88 #include <sys/ioctl.h>
89 #include <sys/wait.h>
90 #include <sys/signal.h>
91 #include <sys/time.h>
92 #include <sys/resource.h>
93 #include <sys/types.h>
94 #ifdef SYSV40
95 #include <netinet/in.h>
96 #endif
97 #include <arpa/inet.h>
98
99 #include <stdlib.h>
100 #include <pwd.h>
101
102 #include "config.h"
103
104 /* Older SYSV. */
105 #if !defined(SIGCHLD) && defined(SIGCLD)
106 #define SIGCHLD      SIGCLD
107 #endif
108
109 #ifndef svc_getcaller           /* SYSV4 */
110 #  define svc_getcaller svc_getrpccaller
111 #endif
112
113 static void reg_service(struct svc_req *rqstp, SVCXPRT *xprt);
114 #ifndef IGNORE_SIGCHLD                  /* Lionel Cons <cons@dxcern.cern.ch> */
115 static void reap(int);
116 #endif
117 static void callit(struct svc_req *rqstp, SVCXPRT *xprt);
118 struct pmaplist *pmaplist;
119 int debugging = 0;
120 int store_fd = -1;
121 static void dump_table(void);
122 static void load_table(void);
123
124 #include "pmap_check.h"
125
126  /*
127   * How desperate can one be. It is possible to prevent an attacker from
128   * manipulating your portmapper tables from outside with requests that
129   * contain spoofed source address information. The countermeasure is to
130   * force all rpc servers to register and unregister with the portmapper via
131   * the loopback network interface, instead of via the primary network
132   * interface that every host can talk to. For this countermeasure to work it
133   * is necessary to #define LOOPBACK_SETUNSET, to disable source routing in
134   * the kernel, and to modify libc so that get_myaddress() chooses the
135   * loopback interface address.
136   */
137
138 #ifdef LOOPBACK_SETUNSET
139 static SVCXPRT *ludpxprt, *ltcpxprt;
140 static int on = 1;
141 #ifndef INADDR_LOOPBACK
142 #define INADDR_LOOPBACK ntohl(inet_addr("127.0.0.1"))
143 #endif
144 #endif
145
146 int daemon_port = PMAPPORT;
147 int daemon_uid = DAEMON_UID;
148 int daemon_gid = DAEMON_GID;
149 const char* mapping_file = PORTMAP_MAPPING_FILE;
150
151 /*
152  * We record with each registration a flag telling whether it was
153  * registered with a privilege port or not.
154  * If it was, it can only be unregistered with a privileged port.
155  * So that we can still use standard pmap xdr routines, we store
156  * this flag in a structure wrapped around a pmaplist.
157  */
158 struct flagged_pml {
159         struct pmaplist pml;
160         int priv;
161 };
162
163 static inline int __getuid(const char* username)
164 {
165         struct passwd* pw = getpwnam(username);
166
167         if (!pw)
168                 return 0;
169
170         daemon_uid = pw->pw_uid;
171         daemon_gid = pw->pw_gid;
172         return 1;
173 }
174
175 static void usage(char *progname)
176 {
177         fprintf(stderr, "usage: %s [-dfFlv] [-t dir] [-i address] "
178                         "[-u uid] [-g gid] [-U username] \n",
179                 progname);
180         fprintf(stderr, "-v             verbose logging\n");
181         fprintf(stderr, "-d             debugging mode\n");
182         fprintf(stderr, "-f             don't daemonize, log to standard error\n");
183         fprintf(stderr, "-F             don't daemonize, log as usual\n");
184         fprintf(stderr, "-t <dir>       chroot into dir\n");
185         fprintf(stderr, "-i <address>   bind to address\n");
186         fprintf(stderr, "-l             same as -i 127.0.0.1\n");
187         fprintf(stderr, "-u <uid>       run as this uid (default: %d)\n", DAEMON_UID);
188         fprintf(stderr, "-g <uid>       run as this gid (default: %d)\n", DAEMON_GID);
189         fprintf(stderr, "-p <port>      run on nonstandard port (default: %d)\n", daemon_port);
190         fprintf(stderr, "-U <username>  suid/sgid to this user\n");
191         fprintf(stderr, "-m <mapfile>   specify the mapping file name "
192                                         "(default: "PORTMAP_MAPPING_FILE")\n");
193 }
194
195 int
196 main(int argc, char **argv)
197 {
198         SVCXPRT *xprt;
199         int sock, c;
200         struct sockaddr_in addr;
201         int len = sizeof(struct sockaddr_in);
202         struct pmaplist *pml;
203         struct flagged_pml *fpml;
204         char *chroot_path = NULL;
205         struct in_addr bindaddr;
206         int have_bindaddr = 0;
207         int foreground = 0;
208         int have_uid = 0;
209
210         while ((c = getopt(argc, argv, "hVdfFlt:vi:u:U:g:m:p:")) != EOF) {
211                 switch (c) {
212
213                 case 'V':
214                         printf("portmap version 6.0.0.1 - 2008-05-10\n");
215                         exit(1);
216
217                 case 'm':
218                         mapping_file = optarg;
219                         break;
220
221                 case 'U':
222                         /* try to fetch user-given uid/gid by name */
223                         if (!__getuid(optarg))
224                         {
225                                 fprintf(stderr,
226                                         "portmap: illegal username: \"%s\"\n",
227                                         optarg);
228                                 exit(1);
229                         }
230                         have_uid = 1;
231                         break;
232                 case 'u':
233                         daemon_uid = atoi(optarg);
234                         if (daemon_uid <= 0) {
235                                 fprintf(stderr,
236                                         "portmap: illegal uid: %s\n", optarg);
237                                 exit(1);
238                         }
239                         have_uid = 1;
240                         break;
241                 case 'g':
242                         daemon_gid = atoi(optarg);
243                         if (daemon_gid <= 0) {
244                                 fprintf(stderr,
245                                         "portmap: illegal gid: %s\n", optarg);
246                                 exit(1);
247                         }
248                         have_uid = 1;
249                         break;
250                 case 'd':
251                         debugging = 1;
252                 case 'f':
253                         foreground = 1;
254                         break;
255                 case 'F':
256                         /* run in foreground, but still log as usual */
257                         foreground = 2;
258                         break;
259
260                 case 't':
261                         chroot_path = optarg;
262                         break;
263
264                 case 'v':
265                         verboselog = 1;
266                         break;
267
268                 case 'l':
269                         optarg = (char*)"127.0.0.1";
270                         /* FALL THROUGH */
271                 case 'i':
272                         have_bindaddr = inet_aton(optarg, &bindaddr);
273                         break;
274                 case 'p':
275                         daemon_port = atoi(optarg);
276                         break;
277                 case 'h':
278                 default:
279                         usage(argv[0]);
280                         exit(1);
281                 }
282         }
283
284         if (!foreground && daemon(0, 0)) {
285                 (void) fprintf(stderr, "portmap: fork: %s", strerror(errno));
286                 exit(1);
287         }
288
289 #ifdef LOG_DAEMON
290         openlog("portmap",
291                 LOG_PID|LOG_NDELAY | ( (foreground==1) ? LOG_PERROR : 0),
292                 FACILITY);
293 #else
294         openlog("portmap",
295                 LOG_PID|LOG_NDELAY | ( (foreground==1) ? LOG_PERROR : 0));
296 #endif
297
298 #ifdef RPCUSER
299         if (!have_uid) {
300                 if (!__getuid(RPCUSER))
301                         syslog(LOG_WARNING, "user '" RPCUSER
302                                "' not found, reverting to default uid");
303         }
304 #endif
305
306         if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
307                 syslog(LOG_ERR, "cannot create udp socket: %m");
308                 exit(1);
309         }
310 #ifdef LOOPBACK_SETUNSET
311         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
312 #endif
313
314         memset((char *) &addr, 0, sizeof(addr));
315         addr.sin_addr.s_addr = 0;
316         addr.sin_family = AF_INET;
317         addr.sin_port = htons(daemon_port);
318         if (have_bindaddr)
319                 memcpy(&addr.sin_addr, &bindaddr, sizeof(bindaddr));
320
321         if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
322                 syslog(LOG_ERR, "cannot bind udp: %m");
323                 exit(1);
324         }
325
326         if ((xprt = svcudp_create(sock)) == (SVCXPRT *)NULL) {
327                 syslog(LOG_ERR, "couldn't do udp_create");
328                 exit(1);
329         }
330         /* make an entry for ourself */
331         fpml = malloc(sizeof(struct flagged_pml));
332         pml = &fpml->pml;
333         fpml->priv = 1;
334         pml->pml_next = NULL;
335         pml->pml_map.pm_prog = PMAPPROG;
336         pml->pml_map.pm_vers = PMAPVERS;
337         pml->pml_map.pm_prot = IPPROTO_UDP;
338         pml->pml_map.pm_port = daemon_port;
339         pmaplist = pml;
340
341         if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
342                 syslog(LOG_ERR, "cannot create tcp socket: %m");
343                 exit(1);
344         }
345 #ifdef LOOPBACK_SETUNSET
346         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
347 #endif
348         if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
349                 syslog(LOG_ERR, "cannot bind tcp: %m");
350                 exit(1);
351         }
352         if ((xprt = svctcp_create(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE))
353             == (SVCXPRT *)NULL) {
354                 syslog(LOG_ERR, "couldn't do tcp_create");
355                 exit(1);
356         }
357         /* make an entry for ourself */
358         fpml = malloc(sizeof(struct flagged_pml));
359         pml = &fpml->pml;
360         fpml->priv = 1;
361         pml->pml_map.pm_prog = PMAPPROG;
362         pml->pml_map.pm_vers = PMAPVERS;
363         pml->pml_map.pm_prot = IPPROTO_TCP;
364         pml->pml_map.pm_port = daemon_port;
365         pml->pml_next = pmaplist;
366         pmaplist = pml;
367
368 #ifdef LOOPBACK_SETUNSET
369         if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
370                 syslog(LOG_ERR, "cannot create udp socket: %m");
371                 exit(1);
372         }
373         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
374
375         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
376         if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
377                 syslog(LOG_ERR, "cannot bind udp: %m");
378                 exit(1);
379         }
380
381         if ((ludpxprt = svcudp_create(sock)) == (SVCXPRT *)NULL) {
382                 syslog(LOG_ERR, "couldn't do udp_create");
383                 exit(1);
384         }
385         if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
386                 syslog(LOG_ERR, "cannot create tcp socket: %m");
387                 exit(1);
388         }
389         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
390         if (bind(sock, (struct sockaddr *)&addr, len) != 0) {
391                 syslog(LOG_ERR, "cannot bind tcp: %m");
392                 exit(1);
393         }
394         if ((ltcpxprt = svctcp_create(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE))
395             == (SVCXPRT *)NULL) {
396                 syslog(LOG_ERR, "couldn't do tcp_create");
397                 exit(1);
398         }
399 #endif
400
401         (void)svc_register(xprt, PMAPPROG, PMAPVERS, reg_service, FALSE);
402
403         store_fd = open(mapping_file, O_RDWR|O_CREAT, PORTMAP_MAPPING_FMODE);
404         load_table();
405
406         /* additional initializations */
407         if (chroot_path) {
408                 if (chroot(chroot_path) < 0) {
409                         syslog(LOG_ERR, "couldn't do chroot");
410                         exit(1);
411                 }
412         }
413         check_startup();
414 #ifdef IGNORE_SIGCHLD                   /* Lionel Cons <cons@dxcern.cern.ch> */
415         (void)signal(SIGCHLD, SIG_IGN);
416 #else
417         (void)signal(SIGCHLD, reap);
418 #endif
419         (void)signal(SIGPIPE, SIG_IGN);
420         svc_run();
421         syslog(LOG_ERR, "run_svc returned unexpectedly");
422         abort();
423 }
424
425 #ifndef NO_PERROR
426 /* need to override perror calls in rpc library */
427 void perror(const char *what)
428 {
429
430         syslog(LOG_ERR, "%s: %m", what);
431 }
432 #endif
433
434 static struct pmaplist *
435 find_service(u_long prog, u_long vers, u_long prot)
436 {
437         struct pmaplist *hit = NULL;
438         struct pmaplist *pml;
439
440         for (pml = pmaplist; pml != NULL; pml = pml->pml_next) {
441                 if ((pml->pml_map.pm_prog != prog) ||
442                         (pml->pml_map.pm_prot != prot))
443                         continue;
444                 hit = pml;
445                 if (pml->pml_map.pm_vers == vers)
446                     break;
447         }
448         return (hit);
449 }
450
451 /* 
452  * 1 OK, 0 not
453  */
454 static void reg_service(struct svc_req *rqstp, SVCXPRT *xprt)
455 {
456         struct pmap reg;
457         struct pmaplist *pml, *prevpml, *fnd;
458         struct flagged_pml *fpml;
459         int ans, port;
460         caddr_t t;
461         
462         /*
463          * Later wrappers change the logging severity on the fly. Reset to
464          * defaults before handling the next request.
465          */
466         allow_severity = LOG_INFO;
467         deny_severity = LOG_WARNING;
468
469         if (debugging)
470                 (void) fprintf(stderr, "server: about do a switch\n");
471         switch (rqstp->rq_proc) {
472
473         case PMAPPROC_NULL:
474                 /*
475                  * Null proc call
476                  */
477                 /* remote host authorization check */
478                 check_default(svc_getcaller(xprt), rqstp->rq_proc, (u_long) 0);
479                 if (!svc_sendreply(xprt, (xdrproc_t) xdr_void, (caddr_t)0)
480                     && debugging) {
481                         abort();
482                 }
483                 break;
484
485         case PMAPPROC_SET:
486                 /*
487                  * Set a program,version to port mapping
488                  */
489                 if (!svc_getargs(xprt, (xdrproc_t) xdr_pmap, (caddr_t)&reg))
490                         svcerr_decode(xprt);
491                 else {
492                         /* reject non-local requests, protect priv. ports */
493                         if (!CHECK_SETUNSET(xprt, ludpxprt, ltcpxprt,
494                             rqstp->rq_proc, reg.pm_prog, reg.pm_port)) {
495                                 ans = 0;
496                                 goto done;
497                         } 
498                         /*
499                          * check to see if already used
500                          * find_service returns a hit even if
501                          * the versions don't match, so check for it
502                          */
503                         fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
504                         if (fnd && fnd->pml_map.pm_vers == reg.pm_vers) {
505                                 if (fnd->pml_map.pm_port == reg.pm_port) {
506                                         ans = 1;
507                                         goto done;
508                                 }
509                                 else {
510                                         ans = 0;
511                                         goto done;
512                                 }
513                         } else {
514                                 /* 
515                                  * add to END of list
516                                  */
517                                 fpml = (struct flagged_pml *)
518                                     malloc((u_int)sizeof(struct flagged_pml));
519                                 pml = &fpml->pml;
520                                 fpml->priv =
521                                         (ntohs(svc_getcaller(xprt)->sin_port)
522                                          < IPPORT_RESERVED);
523                                 pml->pml_map = reg;
524                                 pml->pml_next = 0;
525
526                                 if (pmaplist == 0) {
527                                         pmaplist = pml;
528                                 } else {
529                                         for (fnd= pmaplist; fnd->pml_next != 0;
530                                             fnd = fnd->pml_next);
531                                         fnd->pml_next = pml;
532                                 }
533                                 ans = 1;
534                                 dump_table();
535                         }
536                 done:
537                         if ((!svc_sendreply(xprt, (xdrproc_t)xdr_int,
538                                             (caddr_t)&ans)) &&
539                             debugging) {
540                                 (void) fprintf(stderr, "svc_sendreply\n");
541                                 abort();
542                         }
543                 }
544                 break;
545
546         case PMAPPROC_UNSET:
547                 /*
548                  * Remove a program,version to port mapping.
549                  */
550                 if (!svc_getargs(xprt, (xdrproc_t)xdr_pmap, (caddr_t)&reg))
551                         svcerr_decode(xprt);
552                 else {
553                         ans = 0;
554                         /* reject non-local requests */
555                         if (!CHECK_SETUNSET(xprt, ludpxprt, ltcpxprt,
556                             rqstp->rq_proc, reg.pm_prog, (u_long) 0))
557                                 goto done;
558                         for (prevpml = NULL, pml = pmaplist; pml != NULL; ) {
559                                 if ((pml->pml_map.pm_prog != reg.pm_prog) ||
560                                         (pml->pml_map.pm_vers != reg.pm_vers)) {
561                                         /* both pml & prevpml move forwards */
562                                         prevpml = pml;
563                                         pml = pml->pml_next;
564                                         continue;
565                                 }
566                                 /* found it; pml moves forward, prevpml stays */
567                                 /* privileged port check */
568                                 if (!check_privileged_port(svc_getcaller(xprt), 
569                                     rqstp->rq_proc, 
570                                     reg.pm_prog, 
571                                     pml->pml_map.pm_port))
572                                         continue;
573
574                                 fpml = (struct flagged_pml*)pml;
575                                 if (fpml->priv &&
576                                     (ntohs(svc_getcaller(xprt)->sin_port)
577                                      >= IPPORT_RESERVED))
578                                         continue;
579
580                                 ans = 1;
581                                 t = (caddr_t)pml;
582                                 pml = pml->pml_next;
583                                 if (prevpml == NULL)
584                                         pmaplist = pml;
585                                 else
586                                         prevpml->pml_next = pml;
587                                 free(t);
588                                 dump_table();
589                         }
590                         if ((!svc_sendreply(xprt, (xdrproc_t)xdr_int,
591                                             (caddr_t)&ans)) &&
592                             debugging) {
593                                 (void) fprintf(stderr, "svc_sendreply\n");
594                                 abort();
595                         }
596                 }
597                 break;
598
599         case PMAPPROC_GETPORT:
600                 /*
601                  * Lookup the mapping for a program,version and return its port
602                  */
603                 if (!svc_getargs(xprt, (xdrproc_t)xdr_pmap, (caddr_t)&reg))
604                         svcerr_decode(xprt);
605                 else {
606                         /* remote host authorization check */
607                         if (!check_default(svc_getcaller(xprt), 
608                             rqstp->rq_proc, 
609                             reg.pm_prog)) {
610                                 ans = 0;
611                                 goto done;
612                         }
613                         fnd = find_service(reg.pm_prog, reg.pm_vers, reg.pm_prot);
614                         if (fnd)
615                                 port = fnd->pml_map.pm_port;
616                         else
617                                 port = 0;
618                         if ((!svc_sendreply(xprt, (xdrproc_t)xdr_int,
619                                             (caddr_t)&port)) &&
620                             debugging) {
621                                 (void) fprintf(stderr, "svc_sendreply\n");
622                                 abort();
623                         }
624                 }
625                 break;
626
627         case PMAPPROC_DUMP:
628                 /*
629                  * Return the current set of mapped program,version
630                  */
631                 if (!svc_getargs(xprt, (xdrproc_t)xdr_void, NULL))
632                         svcerr_decode(xprt);
633                 else {
634                         /* remote host authorization check */
635                         struct pmaplist *p;
636                         if (!check_default(svc_getcaller(xprt), 
637                             rqstp->rq_proc, (u_long) 0)) {
638                                 p = 0;  /* send empty list */
639                         } else {
640                                 p = pmaplist;
641                         }
642                         if ((!svc_sendreply(xprt, (xdrproc_t)xdr_pmaplist,
643                             (caddr_t)&p)) && debugging) {
644                                 (void) fprintf(stderr, "svc_sendreply\n");
645                                 abort();
646                         }
647                 }
648                 break;
649
650         case PMAPPROC_CALLIT:
651                 /*
652                  * Calls a procedure on the local machine.  If the requested
653                  * procedure is not registered this procedure does not return
654                  * error information!!
655                  * This procedure is only supported on rpc/udp and calls via 
656                  * rpc/udp.  It passes null authentication parameters.
657                  */
658                 callit(rqstp, xprt);
659                 break;
660
661         default:
662                 /* remote host authorization check */
663                 check_default(svc_getcaller(xprt), rqstp->rq_proc, (u_long) 0);
664                 svcerr_noproc(xprt);
665                 break;
666         }
667 }
668
669
670 /*
671  * Stuff for the rmtcall service
672  */
673 #define ARGSIZE 9000
674
675 struct encap_parms {
676         u_int arglen;
677         char *args;
678 };
679
680 static bool_t
681 xdr_encap_parms(XDR *xdrs, struct encap_parms *epp)
682 {
683
684         return (xdr_bytes(xdrs, &(epp->args), &(epp->arglen), ARGSIZE));
685 }
686
687 struct rmtcallargs {
688         u_long  rmt_prog;
689         u_long  rmt_vers;
690         u_long  rmt_port;
691         u_long  rmt_proc;
692         struct encap_parms rmt_args;
693 };
694
695 static bool_t
696 xdr_rmtcall_args(XDR *xdrs, struct rmtcallargs *cap)
697 {
698
699         /* does not get a port number */
700         if (xdr_u_long(xdrs, &(cap->rmt_prog)) &&
701             xdr_u_long(xdrs, &(cap->rmt_vers)) &&
702             xdr_u_long(xdrs, &(cap->rmt_proc))) {
703                 return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
704         }
705         return (FALSE);
706 }
707
708 static bool_t
709 xdr_rmtcall_result(XDR *xdrs, struct rmtcallargs *cap)
710 {
711         if (xdr_u_long(xdrs, &(cap->rmt_port)))
712                 return (xdr_encap_parms(xdrs, &(cap->rmt_args)));
713         return (FALSE);
714 }
715
716 /*
717  * only worries about the struct encap_parms part of struct rmtcallargs.
718  * The arglen must already be set!!
719  */
720 static bool_t
721 xdr_opaque_parms(XDR *xdrs, struct rmtcallargs *cap)
722 {
723
724         return (xdr_opaque(xdrs, cap->rmt_args.args, cap->rmt_args.arglen));
725 }
726
727 /*
728  * This routine finds and sets the length of incoming opaque paraters
729  * and then calls xdr_opaque_parms.
730  */
731 static bool_t
732 xdr_len_opaque_parms(XDR *xdrs, struct rmtcallargs *cap)
733 {
734         u_int beginpos, lowpos, highpos, currpos, pos;
735
736         beginpos = lowpos = pos = xdr_getpos(xdrs);
737         highpos = lowpos + ARGSIZE;
738         while ((int)(highpos - lowpos) >= 0) {
739                 currpos = (lowpos + highpos) / 2;
740                 if (xdr_setpos(xdrs, currpos)) {
741                         pos = currpos;
742                         lowpos = currpos + 1;
743                 } else {
744                         highpos = currpos - 1;
745                 }
746         }
747         xdr_setpos(xdrs, beginpos);
748         cap->rmt_args.arglen = pos - beginpos;
749         return (xdr_opaque_parms(xdrs, cap));
750 }
751
752 /*
753  * Call a remote procedure service
754  * This procedure is very quiet when things go wrong.
755  * The proc is written to support broadcast rpc.  In the broadcast case,
756  * a machine should shut-up instead of complain, less the requestor be
757  * overrun with complaints at the expense of not hearing a valid reply ...
758  *
759  * This now forks so that the program & process that it calls can call 
760  * back to the portmapper.
761  */
762 static void callit(struct svc_req *rqstp, SVCXPRT *xprt)
763 {
764         struct rmtcallargs a;
765         struct pmaplist *pml;
766         u_short port;
767         struct sockaddr_in me;
768         int pid, so = -1;
769         CLIENT *client;
770         struct authunix_parms *au = (struct authunix_parms *)rqstp->rq_clntcred;
771         struct timeval timeout;
772         char buf[ARGSIZE];
773
774         timeout.tv_sec = 5;
775         timeout.tv_usec = 0;
776         a.rmt_args.args = buf;
777         if (!svc_getargs(xprt, (xdrproc_t)xdr_rmtcall_args, (caddr_t)&a))
778                 return;
779         /* host and service access control */
780         if (!check_callit(svc_getcaller(xprt), 
781             rqstp->rq_proc, a.rmt_prog, a.rmt_proc))
782                 return;
783         if ((pml = find_service(a.rmt_prog, a.rmt_vers,
784             (u_long)IPPROTO_UDP)) == NULL)
785                 return;
786 #ifndef NO_FORK
787         /*
788          * fork a child to do the work.  Parent immediately returns.
789          * Child exits upon completion.
790          */
791         if ((pid = fork()) != 0) {
792                 if (pid < 0)
793                         syslog(LOG_ERR, "CALLIT (prog %lu): fork: %m",
794                             a.rmt_prog);
795                 return;
796         }
797 #endif
798         port = pml->pml_map.pm_port;
799         get_myaddress(&me);
800         me.sin_port = htons(port);
801         client = clntudp_create(&me, a.rmt_prog, a.rmt_vers, timeout, &so);
802         if (client != (CLIENT *)NULL) {
803                 if (rqstp->rq_cred.oa_flavor == AUTH_UNIX) {
804                         client->cl_auth = authunix_create(au->aup_machname,
805                            au->aup_uid, au->aup_gid, au->aup_len, au->aup_gids);
806                 }
807                 a.rmt_port = (u_long)port;
808                 if (clnt_call(client, a.rmt_proc, (xdrproc_t)xdr_opaque_parms,
809                               (caddr_t)&a, (xdrproc_t)xdr_len_opaque_parms,
810                               (caddr_t)&a, timeout) == RPC_SUCCESS) {
811                         svc_sendreply(xprt, (xdrproc_t)xdr_rmtcall_result,
812                                       (caddr_t)&a);
813                 }
814                 AUTH_DESTROY(client->cl_auth);
815                 clnt_destroy(client);
816         }
817         (void)close(so);
818 #ifndef NO_FORK
819         exit(0);
820 #endif
821 }
822
823 #ifndef IGNORE_SIGCHLD                  /* Lionel Cons <cons@dxcern.cern.ch> */
824 static void reap(int ignore)
825 {
826         int save_errno = errno;
827         while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
828         errno = save_errno;
829 }
830 #endif
831
832 /* Dump and restore mapping table so that we can survive kill/restart.
833  * To cope with chroot, an fd is opened early and we just write to that.
834  * If we are killed while writing the file, we lose, but that isn't
835  * very likely...
836  */
837
838 static void dump_table(void)
839 {
840         FILE *f;
841         struct pmaplist *pml;
842
843         if (store_fd < 0)
844                 return;
845         ftruncate(store_fd, 0);
846         lseek(store_fd, 0, 0);
847         f = fdopen(dup(store_fd), "w");
848         if (!f)
849                 return;
850
851         for (pml = pmaplist ; pml ; pml = pml->pml_next) {
852                 struct flagged_pml *fpml = (struct flagged_pml*)pml;
853
854                 fprintf(f, "%lu %lu %lu %lu %d\n",
855                         pml->pml_map.pm_prog,
856                         pml->pml_map.pm_vers,
857                         pml->pml_map.pm_prot,
858                         pml->pml_map.pm_port,
859                         fpml->priv);
860         }
861         fclose(f);
862 }
863
864 static void load_table(void)
865 {
866         FILE *f;
867         struct pmaplist **ep;
868         struct flagged_pml fpml, *fpmlp;
869
870         ep = &pmaplist;
871         while (*ep)
872                 ep = & (*ep)->pml_next;
873
874         if (store_fd < 0)
875                 return;
876         lseek(store_fd, 0, 0);
877         f = fdopen(dup(store_fd), "r");
878         if (f == NULL)
879                 return;
880
881         while (fscanf(f, "%lu %lu %lu %lu %d\n",
882                       &fpml.pml.pml_map.pm_prog,
883                       &fpml.pml.pml_map.pm_vers,
884                       &fpml.pml.pml_map.pm_prot,
885                       &fpml.pml.pml_map.pm_port,
886                       &fpml.priv) == 5) {
887                 if (fpml.pml.pml_map.pm_port == daemon_port)
888                         continue;
889                 fpmlp = malloc(sizeof(struct flagged_pml));
890                 if (!fpmlp)
891                         break;
892                 *fpmlp = fpml;
893                 *ep = &fpmlp->pml;
894                 ep = &fpmlp->pml.pml_next;
895                 *ep = NULL;
896         }
897         fclose(f);
898 }