rpcbind.c revision 1.11 1 /* $NetBSD: rpcbind.c,v 1.11 2007/05/13 20:03:47 christos 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 "@(#)rpcbind.c 1.19 94/04/25 SMI" */
36
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
42
43 /*
44 * rpcbind.c
45 * Implements the program, version to address mapping for rpc.
46 *
47 */
48
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"
74
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 */
79
80 /* who to suid to if -s is given */
81 #define RUN_AS "daemon"
82
83 int runasdaemon = 0;
84 int insecure = 0;
85 int oldstyle_local = 0;
86 int verboselog = 0;
87
88 #ifdef WARMSTART
89 /* Local Variable */
90 static int warmstart = 0; /* Grab a old copy of registrations */
91 #endif
92
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";
101
102 const char superuser[] = "superuser";
103 const char unknown[] = "unknown";
104
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 *[]);
110
111 int
112 main(int argc, char *argv[])
113 {
114 struct netconfig *nconf;
115 void *nc_handle; /* Net config handle */
116 struct rlimit rl;
117 int maxrec = RPC_MAXDATASIZE;
118
119 parseargs(argc, argv);
120
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);
128 }
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
138
139 nconf = getnetconfigent("local");
140 if (nconf == NULL)
141 errx(1, "can't find local transport");
142
143 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
144
145 init_transport(nconf);
146
147 while ((nconf = getnetconfig(nc_handle))) {
148 if (nconf->nc_flag & NC_VISIBLE)
149 init_transport(nconf);
150 }
151 endnetconfig(nc_handle);
152
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();
166 }
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");
174 }
175 } else {
176 if (daemon(0, 0))
177 err(1, "fork failed");
178 }
179
180 openlog("rpcbind", 0, LOG_DAEMON);
181 pidfile(NULL);
182
183 if (runasdaemon) {
184 struct passwd *p;
185
186 if((p = getpwnam(RUN_AS)) == NULL) {
187 syslog(LOG_ERR, "cannot get uid of daemon: %m");
188 exit(1);
189 }
190 if (setuid(p->pw_uid) == -1) {
191 syslog(LOG_ERR, "setuid to daemon failed: %m");
192 exit(1);
193 }
194 }
195
196 network_init();
197
198 my_svc_run();
199 syslog(LOG_ERR, "svc_run returned unexpectedly");
200 rpcbind_abort();
201 /* NOTREACHED */
202
203 return 0;
204 }
205
206 /*
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
210 */
211 static int
212 init_transport(struct netconfig *nconf)
213 {
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;
225
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 ND_DEBUG
231 if (debugging) {
232 int i;
233 char **s;
234
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);
240 }
241 #endif
242
243 /*
244 * XXX - using RPC library internal functions.
245 */
246 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
247 warn("Cannot create socket for `%s' (%m)", nconf->nc_netid);
248 return 1;
249 }
250
251 if (!__rpc_nconf2sockinfo(nconf, &si)) {
252 warnx("Cannot get information for `%s'", nconf->nc_netid);
253 return 1;
254 }
255
256 if (si.si_af == AF_INET6) {
257 /*
258 * We're doing host-based access checks here, so don't allow
259 * v4-in-v6 to confuse things.
260 */
261 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
262 sizeof one) < 0) {
263 warn("Can't make socket ipv6 only");
264 return 1;
265 }
266 }
267
268
269 if (!strcmp(nconf->nc_netid, "local")) {
270 (void)memset(&sun, 0, sizeof sun);
271 sun.sun_family = AF_LOCAL;
272 (void)unlink(_PATH_RPCBINDSOCK);
273 (void)strlcpy(sun.sun_path, _PATH_RPCBINDSOCK,
274 sizeof(sun.sun_path));
275 sun.sun_len = SUN_LEN(&sun);
276 addrlen = sizeof(struct sockaddr_un);
277 sa = (struct sockaddr *)&sun;
278 } else {
279 /* Get rpcbind's address on this transport */
280
281 (void)memset(&hints, 0, sizeof hints);
282 hints.ai_flags = AI_PASSIVE;
283 hints.ai_family = si.si_af;
284 hints.ai_socktype = si.si_socktype;
285 hints.ai_protocol = si.si_proto;
286 if ((aicode = getaddrinfo(NULL, servname, &hints, &res)) != 0) {
287 warnx("Cannot get local address for `%s' (%s)",
288 nconf->nc_netid, gai_strerror(aicode));
289 return 1;
290 }
291 addrlen = res->ai_addrlen;
292 sa = (struct sockaddr *)res->ai_addr;
293 }
294
295 if (bind(fd, sa, addrlen) < 0) {
296 warn("Cannot bind `%s'", nconf->nc_netid);
297 if (res != NULL)
298 freeaddrinfo(res);
299 return 1;
300 }
301
302 /* Copy the address */
303 taddr.addr.len = taddr.addr.maxlen = addrlen;
304 taddr.addr.buf = malloc(addrlen);
305 if (taddr.addr.buf == NULL) {
306 warn("Cannot allocate memory for `%s' address",
307 nconf->nc_netid);
308 if (res != NULL)
309 freeaddrinfo(res);
310 return 1;
311 }
312 (void)memcpy(taddr.addr.buf, sa, addrlen);
313 #ifdef ND_DEBUG
314 if (debugging) {
315 /* for debugging print out our universal address */
316 char *uaddr;
317 struct netbuf nb;
318
319 nb.buf = sa;
320 nb.len = nb.maxlen = sa->sa_len;
321 uaddr = taddr2uaddr(nconf, &nb);
322 (void)fprintf(stderr, "rpcbind: my address is %s\n", uaddr);
323 (void)free(uaddr);
324 }
325 #endif
326
327 if (res != NULL)
328 freeaddrinfo(res);
329
330 if (nconf->nc_semantics != NC_TPI_CLTS)
331 listen(fd, SOMAXCONN);
332
333 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, RPC_MAXDATASIZE,
334 RPC_MAXDATASIZE);
335 if (my_xprt == (SVCXPRT *)NULL) {
336 warnx("Could not create service for `%s'", nconf->nc_netid);
337 goto error;
338 }
339
340 #ifdef PORTMAP
341 /*
342 * Register both the versions for tcp/ip, udp/ip and local.
343 */
344 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
345 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
346 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
347 strcmp(nconf->nc_netid, "local") == 0) {
348 struct pmaplist *pml;
349
350 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
351 pmap_service, 0)) {
352 warn("Could not register on `%s'", nconf->nc_netid);
353 goto error;
354 }
355 pml = malloc(sizeof (struct pmaplist));
356 if (pml == NULL) {
357 warn("Cannot allocate memory");
358 goto error;
359 }
360 pml->pml_map.pm_prog = PMAPPROG;
361 pml->pml_map.pm_vers = PMAPVERS;
362 pml->pml_map.pm_port = PMAPPORT;
363 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
364 if (tcptrans[0]) {
365 warnx(
366 "Cannot have more than one TCP transport");
367 free(pml);
368 goto error;
369 }
370 tcptrans = strdup(nconf->nc_netid);
371 if (tcptrans == NULL) {
372 free(pml);
373 warn("Cannot allocate memory");
374 goto error;
375 }
376 pml->pml_map.pm_prot = IPPROTO_TCP;
377
378 /* Let's snarf the universal address */
379 /* "h1.h2.h3.h4.p1.p2" */
380 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
381 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
382 if (udptrans[0]) {
383 free(pml);
384 warnx(
385 "Cannot have more than one UDP transport");
386 goto error;
387 }
388 udptrans = strdup(nconf->nc_netid);
389 if (udptrans == NULL) {
390 free(pml);
391 warn("Cannot allocate memory");
392 goto error;
393 }
394 pml->pml_map.pm_prot = IPPROTO_UDP;
395
396 /* Let's snarf the universal address */
397 /* "h1.h2.h3.h4.p1.p2" */
398 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
399 }
400 pml->pml_next = list_pml;
401 list_pml = pml;
402
403 /* Add version 3 information */
404 pml = malloc(sizeof (struct pmaplist));
405 if (pml == NULL) {
406 warn("Cannot allocate memory");
407 goto error;
408 }
409 pml->pml_map = list_pml->pml_map;
410 pml->pml_map.pm_vers = RPCBVERS;
411 pml->pml_next = list_pml;
412 list_pml = pml;
413
414 /* Add version 4 information */
415 pml = malloc(sizeof (struct pmaplist));
416 if (pml == NULL) {
417 warn("Cannot allocate memory");
418 goto error;
419 }
420 pml->pml_map = list_pml->pml_map;
421 pml->pml_map.pm_vers = RPCBVERS4;
422 pml->pml_next = list_pml;
423 list_pml = pml;
424
425 /* Also add version 2 stuff to rpcbind list */
426 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
427 }
428 #endif
429
430 /* version 3 registration */
431 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
432 warn("Could not register %s version 3", nconf->nc_netid);
433 goto error;
434 }
435 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
436
437 /* version 4 registration */
438 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
439 warn("Could not register %s version 4", nconf->nc_netid);
440 goto error;
441 }
442 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
443
444 /* decide if bound checking works for this transport */
445 status = add_bndlist(nconf, &taddr.addr);
446 #ifdef BIND_DEBUG
447 if (debugging) {
448 if (status < 0) {
449 fprintf(stderr, "Error in finding bind status for %s\n",
450 nconf->nc_netid);
451 } else if (status == 0) {
452 fprintf(stderr, "check binding for %s\n",
453 nconf->nc_netid);
454 } else if (status > 0) {
455 fprintf(stderr, "No check binding for %s\n",
456 nconf->nc_netid);
457 }
458 }
459 #endif
460 /*
461 * rmtcall only supported on CLTS transports for now.
462 */
463 if (nconf->nc_semantics == NC_TPI_CLTS) {
464 status = create_rmtcall_fd(nconf);
465
466 #ifdef BIND_DEBUG
467 if (debugging) {
468 if (status < 0) {
469 fprintf(stderr,
470 "Could not create rmtcall fd for %s\n",
471 nconf->nc_netid);
472 } else {
473 fprintf(stderr, "rmtcall fd for %s is %d\n",
474 nconf->nc_netid, status);
475 }
476 }
477 #endif
478 }
479 return (0);
480 error:
481 (void)close(fd);
482 return (1);
483 }
484
485 static void
486 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
487 struct netbuf *addr)
488 {
489 rpcblist_ptr rbl;
490
491 rbl = malloc(sizeof(rpcblist));
492 if (rbl == NULL) {
493 warn("Out of memory");
494 return;
495 }
496
497 rbl->rpcb_map.r_prog = prog;
498 rbl->rpcb_map.r_vers = vers;
499 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
500 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
501 rbl->rpcb_map.r_owner = strdup(superuser);
502 rbl->rpcb_next = list_rbl; /* Attach to global list */
503 list_rbl = rbl;
504 }
505
506 /*
507 * Catch the signal and die
508 */
509 static void
510 terminate(int dummy)
511 {
512 #ifdef WARMSTART
513 syslog(LOG_ERR,
514 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
515 write_warmstart(); /* Dump yourself */
516 #endif
517 exit(2);
518 }
519
520 void
521 rpcbind_abort()
522 {
523 #ifdef WARMSTART
524 write_warmstart(); /* Dump yourself */
525 #endif
526 abort();
527 }
528
529 /* get command line options */
530 static void
531 parseargs(int argc, char *argv[])
532 {
533 int c;
534
535 while ((c = getopt(argc, argv, "dwailLs")) != -1) {
536 switch (c) {
537 case 'a':
538 doabort = 1; /* when debugging, do an abort on */
539 break; /* errors; for rpcbind developers */
540 /* only! */
541 case 'd':
542 debugging = 1;
543 break;
544 case 'i':
545 insecure = 1;
546 break;
547 case 'L':
548 oldstyle_local = 1;
549 break;
550 case 'l':
551 verboselog = 1;
552 break;
553 case 's':
554 runasdaemon = 1;
555 break;
556 #ifdef WARMSTART
557 case 'w':
558 warmstart = 1;
559 break;
560 #endif
561 default: /* error */
562 fprintf(stderr, "usage: rpcbind [-Idwils]\n");
563 exit (1);
564 }
565 }
566 if (doabort && !debugging) {
567 fprintf(stderr,
568 "-a (abort) specified without -d (debugging) -- ignored.\n");
569 doabort = 0;
570 }
571 }
572
573 void
574 reap(int dummy)
575 {
576 int save_errno = errno;
577
578 while (wait3(NULL, WNOHANG, NULL) > 0)
579 ;
580 errno = save_errno;
581 }
582
583 void
584 toggle_verboselog(int dummy)
585 {
586 verboselog = !verboselog;
587 }
588