Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / rpcbind / rpcbind.c
blob4e6944f42751d97677be6b6bfb16c6566ea40eac
1 /* $NetBSD: rpcbind.c,v 1.14 2007/06/04 18:00:51 christos Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
32 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
35 /* #ident "@(#)rpcbind.c 1.19 94/04/25 SMI" */
37 #if 0
38 #ifndef lint
39 static char sccsid[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro";
40 #endif
41 #endif
44 * rpcbind.c
45 * Implements the program, version to address mapping for rpc.
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/errno.h>
52 #include <sys/time.h>
53 #include <sys/resource.h>
54 #include <sys/wait.h>
55 #include <sys/signal.h>
56 #include <sys/socket.h>
57 #include <sys/un.h>
58 #include <rpc/rpc.h>
59 #ifdef PORTMAP
60 #include <netinet/in.h>
61 #endif
62 #include <netdb.h>
63 #include <stdio.h>
64 #include <netconfig.h>
65 #include <stdlib.h>
66 #include <unistd.h>
67 #include <syslog.h>
68 #include <err.h>
69 #include <util.h>
70 #include <pwd.h>
71 #include <string.h>
72 #include <errno.h>
73 #include "rpcbind.h"
75 /* Global variables */
76 int debugging = 0; /* Tell me what's going on */
77 int doabort = 0; /* When debugging, do an abort on errors */
78 rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
80 /* who to suid to if -s is given */
81 #define RUN_AS "daemon"
83 int runasdaemon = 0;
84 int insecure = 0;
85 int oldstyle_local = 0;
86 int verboselog = 0;
88 #ifdef WARMSTART
89 /* Local Variable */
90 static int warmstart = 0; /* Grab a old copy of registrations */
91 #endif
93 #ifdef PORTMAP
94 struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
95 const char *udptrans; /* Name of UDP transport */
96 const char *tcptrans; /* Name of TCP transport */
97 const char *udp_uaddr; /* Universal UDP address */
98 const char *tcp_uaddr; /* Universal TCP address */
99 #endif
100 static const char servname[] = "sunrpc";
102 const char rpcbind_superuser[] = "superuser";
103 const char rpcbind_unknown[] = "unknown";
105 static int init_transport(struct netconfig *);
106 static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
107 struct netbuf *);
108 static void terminate(int);
109 static void parseargs(int, char *[]);
112 main(int argc, char *argv[])
114 struct netconfig *nconf;
115 void *nc_handle; /* Net config handle */
116 struct rlimit rl;
117 int maxrec = RPC_MAXDATASIZE;
119 parseargs(argc, argv);
121 getrlimit(RLIMIT_NOFILE, &rl);
122 if (rl.rlim_cur < 128) {
123 if (rl.rlim_max <= 128)
124 rl.rlim_cur = rl.rlim_max;
125 else
126 rl.rlim_cur = 128;
127 setrlimit(RLIMIT_NOFILE, &rl);
129 if (geteuid()) /* This command allowed only to root */
130 errx(1, "Sorry. You are not superuser");
131 nc_handle = setnetconfig(); /* open netconfig file */
132 if (nc_handle == NULL)
133 errx(1, "could not read /etc/netconfig");
134 #ifdef PORTMAP
135 udptrans = "";
136 tcptrans = "";
137 #endif
139 nconf = getnetconfigent("local");
140 if (nconf == NULL)
141 errx(1, "can't find local transport");
143 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
145 init_transport(nconf);
147 while ((nconf = getnetconfig(nc_handle))) {
148 if (nconf->nc_flag & NC_VISIBLE)
149 init_transport(nconf);
151 endnetconfig(nc_handle);
153 /* catch the usual termination signals for graceful exit */
154 (void) signal(SIGCHLD, reap);
155 (void) signal(SIGINT, terminate);
156 (void) signal(SIGTERM, terminate);
157 (void) signal(SIGQUIT, terminate);
158 /* ignore others that could get sent */
159 (void) signal(SIGPIPE, SIG_IGN);
160 (void) signal(SIGHUP, SIG_IGN);
161 (void) signal(SIGUSR1, SIG_IGN);
162 (void) signal(SIGUSR2, SIG_IGN);
163 #ifdef WARMSTART
164 if (warmstart) {
165 read_warmstart();
167 #endif
168 if (debugging) {
169 printf("rpcbind debugging enabled.");
170 if (doabort) {
171 printf(" Will abort on errors!\n");
172 } else {
173 printf("\n");
175 } else {
176 if (daemon(0, 0))
177 err(1, "fork failed");
180 openlog("rpcbind", 0, LOG_DAEMON);
181 pidfile(NULL);
183 if (runasdaemon) {
184 struct passwd *p;
186 if((p = getpwnam(RUN_AS)) == NULL) {
187 syslog(LOG_ERR, "cannot get uid of daemon: %m");
188 exit(1);
190 if (setuid(p->pw_uid) == -1) {
191 syslog(LOG_ERR, "setuid to daemon failed: %m");
192 exit(1);
196 network_init();
198 my_svc_run();
199 syslog(LOG_ERR, "svc_run returned unexpectedly");
200 rpcbind_abort();
201 /* NOTREACHED */
203 return 0;
207 * Adds the entry into the rpcbind database.
208 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
209 * Returns 0 if succeeds, else fails
211 static int
212 init_transport(struct netconfig *nconf)
214 int fd;
215 struct t_bind taddr;
216 struct addrinfo hints, *res = NULL;
217 struct __rpc_sockinfo si;
218 SVCXPRT *my_xprt;
219 int status; /* bound checking ? */
220 int aicode;
221 int addrlen;
222 struct sockaddr *sa;
223 struct sockaddr_un sun;
224 const int one = 1;
226 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
227 (nconf->nc_semantics != NC_TPI_COTS) &&
228 (nconf->nc_semantics != NC_TPI_COTS_ORD))
229 return 1; /* not my type */
230 #ifdef RPCBIND_DEBUG
231 if (debugging) {
232 int i;
233 char **s;
235 (void)fprintf(stderr, "%s: %ld lookup routines :\n",
236 nconf->nc_netid, nconf->nc_nlookups);
237 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
238 i++, s++)
239 (void)fprintf(stderr, "[%d] - %s\n", i, *s);
241 #endif
244 * XXX - using RPC library internal functions.
246 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
247 if (errno == EAFNOSUPPORT)
248 return 1;
249 warn("Cannot create socket for `%s'", nconf->nc_netid);
250 return 1;
253 if (!__rpc_nconf2sockinfo(nconf, &si)) {
254 warnx("Cannot get information for `%s'", nconf->nc_netid);
255 return 1;
258 if (si.si_af == AF_INET6) {
260 * We're doing host-based access checks here, so don't allow
261 * v4-in-v6 to confuse things.
263 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
264 sizeof one) < 0) {
265 warn("Can't make socket ipv6 only");
266 return 1;
271 if (!strcmp(nconf->nc_netid, "local")) {
272 (void)memset(&sun, 0, sizeof sun);
273 sun.sun_family = AF_LOCAL;
274 (void)unlink(_PATH_RPCBINDSOCK);
275 (void)strlcpy(sun.sun_path, _PATH_RPCBINDSOCK,
276 sizeof(sun.sun_path));
277 sun.sun_len = SUN_LEN(&sun);
278 addrlen = sizeof(struct sockaddr_un);
279 sa = (struct sockaddr *)&sun;
280 } else {
281 /* Get rpcbind's address on this transport */
283 (void)memset(&hints, 0, sizeof hints);
284 hints.ai_flags = AI_PASSIVE;
285 hints.ai_family = si.si_af;
286 hints.ai_socktype = si.si_socktype;
287 hints.ai_protocol = si.si_proto;
288 if ((aicode = getaddrinfo(NULL, servname, &hints, &res)) != 0) {
289 warnx("Cannot get local address for `%s' (%s)",
290 nconf->nc_netid, gai_strerror(aicode));
291 return 1;
293 addrlen = res->ai_addrlen;
294 sa = (struct sockaddr *)res->ai_addr;
297 if (bind(fd, sa, addrlen) < 0) {
298 warn("Cannot bind `%s'", nconf->nc_netid);
299 if (res != NULL)
300 freeaddrinfo(res);
301 return 1;
303 if (sa->sa_family == AF_LOCAL)
304 if (chmod(sun.sun_path, S_IRWXU|S_IRWXG|S_IRWXO) == -1)
305 warn("Cannot chmod `%s'", sun.sun_path);
307 /* Copy the address */
308 taddr.addr.len = taddr.addr.maxlen = addrlen;
309 taddr.addr.buf = malloc(addrlen);
310 if (taddr.addr.buf == NULL) {
311 warn("Cannot allocate memory for `%s' address",
312 nconf->nc_netid);
313 if (res != NULL)
314 freeaddrinfo(res);
315 return 1;
317 (void)memcpy(taddr.addr.buf, sa, addrlen);
318 #ifdef RPCBIND_DEBUG
319 if (debugging) {
320 /* for debugging print out our universal address */
321 char *uaddr;
322 struct netbuf nb;
324 nb.buf = sa;
325 nb.len = nb.maxlen = sa->sa_len;
326 uaddr = taddr2uaddr(nconf, &nb);
327 (void)fprintf(stderr, "rpcbind: my address is %s\n", uaddr);
328 (void)free(uaddr);
330 #endif
332 if (res != NULL)
333 freeaddrinfo(res);
335 if (nconf->nc_semantics != NC_TPI_CLTS)
336 listen(fd, SOMAXCONN);
338 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, RPC_MAXDATASIZE,
339 RPC_MAXDATASIZE);
340 if (my_xprt == (SVCXPRT *)NULL) {
341 warnx("Could not create service for `%s'", nconf->nc_netid);
342 goto error;
345 #ifdef PORTMAP
347 * Register both the versions for tcp/ip, udp/ip and local.
349 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
350 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
351 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
352 strcmp(nconf->nc_netid, "local") == 0) {
353 struct pmaplist *pml;
355 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
356 pmap_service, 0)) {
357 warn("Could not register on `%s'", nconf->nc_netid);
358 goto error;
360 pml = malloc(sizeof (struct pmaplist));
361 if (pml == NULL) {
362 warn("Cannot allocate memory");
363 goto error;
365 pml->pml_map.pm_prog = PMAPPROG;
366 pml->pml_map.pm_vers = PMAPVERS;
367 pml->pml_map.pm_port = PMAPPORT;
368 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
369 if (tcptrans[0]) {
370 warnx(
371 "Cannot have more than one TCP transport");
372 free(pml);
373 goto error;
375 tcptrans = strdup(nconf->nc_netid);
376 if (tcptrans == NULL) {
377 free(pml);
378 warn("Cannot allocate memory");
379 goto error;
381 pml->pml_map.pm_prot = IPPROTO_TCP;
383 /* Let's snarf the universal address */
384 /* "h1.h2.h3.h4.p1.p2" */
385 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
386 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
387 if (udptrans[0]) {
388 free(pml);
389 warnx(
390 "Cannot have more than one UDP transport");
391 goto error;
393 udptrans = strdup(nconf->nc_netid);
394 if (udptrans == NULL) {
395 free(pml);
396 warn("Cannot allocate memory");
397 goto error;
399 pml->pml_map.pm_prot = IPPROTO_UDP;
401 /* Let's snarf the universal address */
402 /* "h1.h2.h3.h4.p1.p2" */
403 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
405 pml->pml_next = list_pml;
406 list_pml = pml;
408 /* Add version 3 information */
409 pml = malloc(sizeof (struct pmaplist));
410 if (pml == NULL) {
411 warn("Cannot allocate memory");
412 goto error;
414 pml->pml_map = list_pml->pml_map;
415 pml->pml_map.pm_vers = RPCBVERS;
416 pml->pml_next = list_pml;
417 list_pml = pml;
419 /* Add version 4 information */
420 pml = malloc(sizeof (struct pmaplist));
421 if (pml == NULL) {
422 warn("Cannot allocate memory");
423 goto error;
425 pml->pml_map = list_pml->pml_map;
426 pml->pml_map.pm_vers = RPCBVERS4;
427 pml->pml_next = list_pml;
428 list_pml = pml;
430 /* Also add version 2 stuff to rpcbind list */
431 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
433 #endif
435 /* version 3 registration */
436 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
437 warn("Could not register %s version 3", nconf->nc_netid);
438 goto error;
440 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
442 /* version 4 registration */
443 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
444 warn("Could not register %s version 4", nconf->nc_netid);
445 goto error;
447 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
449 /* decide if bound checking works for this transport */
450 status = add_bndlist(nconf, &taddr.addr);
451 #ifdef RPCBIND_DEBUG
452 if (debugging) {
453 if (status < 0) {
454 fprintf(stderr, "Error in finding bind status for %s\n",
455 nconf->nc_netid);
456 } else if (status == 0) {
457 fprintf(stderr, "check binding for %s\n",
458 nconf->nc_netid);
459 } else if (status > 0) {
460 fprintf(stderr, "No check binding for %s\n",
461 nconf->nc_netid);
464 #endif
466 * rmtcall only supported on CLTS transports for now.
468 if (nconf->nc_semantics == NC_TPI_CLTS) {
469 status = create_rmtcall_fd(nconf);
471 #ifdef RPCBIND_DEBUG
472 if (debugging) {
473 if (status < 0) {
474 fprintf(stderr,
475 "Could not create rmtcall fd for %s\n",
476 nconf->nc_netid);
477 } else {
478 fprintf(stderr, "rmtcall fd for %s is %d\n",
479 nconf->nc_netid, status);
482 #endif
484 return (0);
485 error:
486 (void)close(fd);
487 return (1);
490 static void
491 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
492 struct netbuf *addr)
494 rpcblist_ptr rbl;
496 rbl = malloc(sizeof(rpcblist));
497 if (rbl == NULL) {
498 warn("Out of memory");
499 return;
502 rbl->rpcb_map.r_prog = prog;
503 rbl->rpcb_map.r_vers = vers;
504 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
505 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
506 rbl->rpcb_map.r_owner = strdup(rpcbind_superuser);
507 rbl->rpcb_next = list_rbl; /* Attach to global list */
508 list_rbl = rbl;
512 * Catch the signal and die
514 static void
515 terminate(int dummy)
517 #ifdef WARMSTART
518 syslog(LOG_ERR,
519 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
520 write_warmstart(); /* Dump yourself */
521 #endif
522 exit(2);
525 void
526 rpcbind_abort()
528 #ifdef WARMSTART
529 write_warmstart(); /* Dump yourself */
530 #endif
531 abort();
534 /* get command line options */
535 static void
536 parseargs(int argc, char *argv[])
538 int c;
540 while ((c = getopt(argc, argv, "dwailLs")) != -1) {
541 switch (c) {
542 case 'a':
543 doabort = 1; /* when debugging, do an abort on */
544 break; /* errors; for rpcbind developers */
545 /* only! */
546 case 'd':
547 debugging = 1;
548 break;
549 case 'i':
550 insecure = 1;
551 break;
552 case 'L':
553 oldstyle_local = 1;
554 break;
555 case 'l':
556 verboselog = 1;
557 break;
558 case 's':
559 runasdaemon = 1;
560 break;
561 #ifdef WARMSTART
562 case 'w':
563 warmstart = 1;
564 break;
565 #endif
566 default: /* error */
567 fprintf(stderr, "usage: rpcbind [-Idwils]\n");
568 exit (1);
571 if (doabort && !debugging) {
572 fprintf(stderr,
573 "-a (abort) specified without -d (debugging) -- ignored.\n");
574 doabort = 0;
578 void
579 reap(int dummy)
581 int save_errno = errno;
583 while (wait3(NULL, WNOHANG, NULL) > 0)
585 errno = save_errno;
588 void
589 toggle_verboselog(int dummy)
591 verboselog = !verboselog;