rpcbind.c revision 1.20 1 /* $NetBSD: rpcbind.c,v 1.20 2015/05/09 13:10:50 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 rpcbind_superuser[] = "superuser";
103 const char rpcbind_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 __dead 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 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 && rl.rlim_cur < 128) {
122 if (rl.rlim_max <= 128)
123 rl.rlim_cur = rl.rlim_max;
124 else
125 rl.rlim_cur = 128;
126 setrlimit(RLIMIT_NOFILE, &rl);
127 }
128 nc_handle = setnetconfig(); /* open netconfig file */
129 if (nc_handle == NULL)
130 errx(1, "could not read /etc/netconfig");
131 #ifdef PORTMAP
132 udptrans = "";
133 tcptrans = "";
134 #endif
135
136 nconf = getnetconfigent("local");
137 if (nconf == NULL)
138 errx(1, "can't find local transport");
139
140 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
141
142 init_transport(nconf);
143
144 while ((nconf = getnetconfig(nc_handle))) {
145 if (nconf->nc_flag & NC_VISIBLE)
146 init_transport(nconf);
147 }
148 endnetconfig(nc_handle);
149
150 /* catch the usual termination signals for graceful exit */
151 (void) signal(SIGCHLD, reap);
152 (void) signal(SIGINT, terminate);
153 (void) signal(SIGTERM, terminate);
154 (void) signal(SIGQUIT, terminate);
155 /* ignore others that could get sent */
156 (void) signal(SIGPIPE, SIG_IGN);
157 (void) signal(SIGHUP, SIG_IGN);
158 (void) signal(SIGUSR1, SIG_IGN);
159 (void) signal(SIGUSR2, SIG_IGN);
160 #ifdef WARMSTART
161 if (warmstart) {
162 read_warmstart();
163 }
164 #endif
165 if (debugging) {
166 printf("rpcbind debugging enabled.");
167 if (doabort) {
168 printf(" Will abort on errors!\n");
169 } else {
170 printf("\n");
171 }
172 } else {
173 if (daemon(0, 0))
174 err(1, "fork failed");
175 }
176
177 openlog("rpcbind", 0, LOG_DAEMON);
178 pidfile(NULL);
179
180 if (runasdaemon) {
181 struct passwd *p;
182
183 if((p = getpwnam(RUN_AS)) == NULL) {
184 syslog(LOG_ERR, "cannot get uid of daemon: %m");
185 exit(1);
186 }
187 if (setuid(p->pw_uid) == -1) {
188 syslog(LOG_ERR, "setuid to daemon failed: %m");
189 exit(1);
190 }
191 }
192
193 network_init();
194
195 my_svc_run();
196 syslog(LOG_ERR, "svc_run returned unexpectedly");
197 rpcbind_abort();
198 /* NOTREACHED */
199
200 return 0;
201 }
202
203 /*
204 * Adds the entry into the rpcbind database.
205 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
206 * Returns 0 if succeeds, else fails
207 */
208 static int
209 init_transport(struct netconfig *nconf)
210 {
211 int fd;
212 struct t_bind taddr;
213 struct addrinfo hints, *res = NULL;
214 struct __rpc_sockinfo si;
215 SVCXPRT *my_xprt;
216 int status; /* bound checking ? */
217 int aicode;
218 int addrlen;
219 struct sockaddr *sa;
220 struct sockaddr_un sun;
221 const int one = 1;
222
223 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
224 (nconf->nc_semantics != NC_TPI_COTS) &&
225 (nconf->nc_semantics != NC_TPI_COTS_ORD))
226 return 1; /* not my type */
227 #ifdef RPCBIND_DEBUG
228 if (debugging) {
229 int i;
230 char **s;
231
232 (void)fprintf(stderr, "%s: %ld lookup routines :\n",
233 nconf->nc_netid, nconf->nc_nlookups);
234 for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
235 i++, s++)
236 (void)fprintf(stderr, "[%d] - %s\n", i, *s);
237 }
238 #endif
239
240 /*
241 * XXX - using RPC library internal functions.
242 */
243 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
244 if (errno == EAFNOSUPPORT)
245 return 1;
246 warn("Cannot create socket for `%s'", nconf->nc_netid);
247 return 1;
248 }
249
250 if (!__rpc_nconf2sockinfo(nconf, &si)) {
251 warnx("Cannot get information for `%s'", nconf->nc_netid);
252 return 1;
253 }
254
255 if (si.si_af == AF_INET6) {
256 /*
257 * We're doing host-based access checks here, so don't allow
258 * v4-in-v6 to confuse things.
259 */
260 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
261 sizeof one) < 0) {
262 warn("Can't make socket ipv6 only");
263 return 1;
264 }
265 }
266
267
268 if (!strcmp(nconf->nc_netid, "local")) {
269 (void)memset(&sun, 0, sizeof sun);
270 sun.sun_family = AF_LOCAL;
271 (void)unlink(_PATH_RPCBINDSOCK);
272 (void)strlcpy(sun.sun_path, _PATH_RPCBINDSOCK,
273 sizeof(sun.sun_path));
274 sun.sun_len = SUN_LEN(&sun);
275 addrlen = sizeof(struct sockaddr_un);
276 sa = (struct sockaddr *)&sun;
277 } else {
278 /* Get rpcbind's address on this transport */
279
280 (void)memset(&hints, 0, sizeof hints);
281 hints.ai_flags = AI_PASSIVE;
282 hints.ai_family = si.si_af;
283 hints.ai_socktype = si.si_socktype;
284 hints.ai_protocol = si.si_proto;
285 if ((aicode = getaddrinfo(NULL, servname, &hints, &res)) != 0) {
286 warnx("Cannot get local address for `%s' (%s)",
287 nconf->nc_netid, gai_strerror(aicode));
288 return 1;
289 }
290 addrlen = res->ai_addrlen;
291 sa = (struct sockaddr *)res->ai_addr;
292 }
293
294 if (bind(fd, sa, addrlen) < 0) {
295 warn("Cannot bind `%s'", nconf->nc_netid);
296 if (res != NULL)
297 freeaddrinfo(res);
298 return 1;
299 }
300 if (sa->sa_family == AF_LOCAL)
301 if (chmod(sun.sun_path, S_IRWXU|S_IRWXG|S_IRWXO) == -1)
302 warn("Cannot chmod `%s'", sun.sun_path);
303
304 /* Copy the address */
305 taddr.addr.len = taddr.addr.maxlen = addrlen;
306 taddr.addr.buf = malloc(addrlen);
307 if (taddr.addr.buf == NULL) {
308 warn("Cannot allocate memory for `%s' address",
309 nconf->nc_netid);
310 if (res != NULL)
311 freeaddrinfo(res);
312 return 1;
313 }
314 (void)memcpy(taddr.addr.buf, sa, addrlen);
315 #ifdef RPCBIND_DEBUG
316 if (debugging) {
317 /* for debugging print out our universal address */
318 char *uaddr;
319 struct netbuf nb;
320
321 nb.buf = sa;
322 nb.len = nb.maxlen = sa->sa_len;
323 uaddr = taddr2uaddr(nconf, &nb);
324 (void)fprintf(stderr, "rpcbind: my address is %s\n", uaddr);
325 (void)free(uaddr);
326 }
327 #endif
328
329 if (res != NULL)
330 freeaddrinfo(res);
331
332 if (nconf->nc_semantics != NC_TPI_CLTS)
333 listen(fd, SOMAXCONN);
334
335 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, RPC_MAXDATASIZE,
336 RPC_MAXDATASIZE);
337 if (my_xprt == NULL) {
338 warnx("Could not create service for `%s'", nconf->nc_netid);
339 goto error;
340 }
341
342 #ifdef PORTMAP
343 /*
344 * Register both the versions for tcp/ip, udp/ip and local.
345 */
346 if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
347 (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
348 strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
349 strcmp(nconf->nc_netid, "local") == 0) {
350 struct pmaplist *pml;
351
352 if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
353 pmap_service, 0)) {
354 warn("Could not register on `%s'", nconf->nc_netid);
355 goto error;
356 }
357 pml = malloc(sizeof (struct pmaplist));
358 if (pml == NULL) {
359 warn("Cannot allocate memory");
360 goto error;
361 }
362 pml->pml_map.pm_prog = PMAPPROG;
363 pml->pml_map.pm_vers = PMAPVERS;
364 pml->pml_map.pm_port = PMAPPORT;
365 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
366 if (tcptrans[0]) {
367 warnx(
368 "Cannot have more than one TCP transport");
369 free(pml);
370 goto error;
371 }
372 tcptrans = strdup(nconf->nc_netid);
373 if (tcptrans == NULL) {
374 free(pml);
375 warn("Cannot allocate memory");
376 goto error;
377 }
378 pml->pml_map.pm_prot = IPPROTO_TCP;
379
380 /* Let's snarf the universal address */
381 /* "h1.h2.h3.h4.p1.p2" */
382 tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
383 } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
384 if (udptrans[0]) {
385 free(pml);
386 warnx(
387 "Cannot have more than one UDP transport");
388 goto error;
389 }
390 udptrans = strdup(nconf->nc_netid);
391 if (udptrans == NULL) {
392 free(pml);
393 warn("Cannot allocate memory");
394 goto error;
395 }
396 pml->pml_map.pm_prot = IPPROTO_UDP;
397
398 /* Let's snarf the universal address */
399 /* "h1.h2.h3.h4.p1.p2" */
400 udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
401 }
402 pml->pml_next = list_pml;
403 list_pml = pml;
404
405 /* Add version 3 information */
406 pml = malloc(sizeof (struct pmaplist));
407 if (pml == NULL) {
408 warn("Cannot allocate memory");
409 goto error;
410 }
411 pml->pml_map = list_pml->pml_map;
412 pml->pml_map.pm_vers = RPCBVERS;
413 pml->pml_next = list_pml;
414 list_pml = pml;
415
416 /* Add version 4 information */
417 pml = malloc(sizeof (struct pmaplist));
418 if (pml == NULL) {
419 warn("Cannot allocate memory");
420 goto error;
421 }
422 pml->pml_map = list_pml->pml_map;
423 pml->pml_map.pm_vers = RPCBVERS4;
424 pml->pml_next = list_pml;
425 list_pml = pml;
426
427 /* Also add version 2 stuff to rpcbind list */
428 rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
429 }
430 #endif
431
432 /* version 3 registration */
433 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
434 warn("Could not register %s version 3", nconf->nc_netid);
435 goto error;
436 }
437 rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
438
439 /* version 4 registration */
440 if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
441 warn("Could not register %s version 4", nconf->nc_netid);
442 goto error;
443 }
444 rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
445
446 /* decide if bound checking works for this transport */
447 status = add_bndlist(nconf, &taddr.addr);
448 #ifdef RPCBIND_DEBUG
449 if (debugging) {
450 if (status < 0) {
451 fprintf(stderr, "Error in finding bind status for %s\n",
452 nconf->nc_netid);
453 } else if (status == 0) {
454 fprintf(stderr, "check binding for %s\n",
455 nconf->nc_netid);
456 } else if (status > 0) {
457 fprintf(stderr, "No check binding for %s\n",
458 nconf->nc_netid);
459 }
460 }
461 #else
462 __USE(status);
463 #endif
464 /*
465 * rmtcall only supported on CLTS transports for now.
466 */
467 if (nconf->nc_semantics == NC_TPI_CLTS) {
468 status = create_rmtcall_fd(nconf);
469
470 #ifdef RPCBIND_DEBUG
471 if (debugging) {
472 if (status < 0) {
473 fprintf(stderr,
474 "Could not create rmtcall fd for %s\n",
475 nconf->nc_netid);
476 } else {
477 fprintf(stderr, "rmtcall fd for %s is %d\n",
478 nconf->nc_netid, status);
479 }
480 }
481 #endif
482 }
483 return (0);
484 error:
485 (void)close(fd);
486 return (1);
487 }
488
489 static void
490 rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
491 struct netbuf *addr)
492 {
493 rpcblist_ptr rbl;
494
495 rbl = malloc(sizeof(rpcblist));
496 if (rbl == NULL) {
497 warn("Out of memory");
498 return;
499 }
500
501 rbl->rpcb_map.r_prog = prog;
502 rbl->rpcb_map.r_vers = vers;
503 rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
504 rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
505 rbl->rpcb_map.r_owner = strdup(rpcbind_superuser);
506 rbl->rpcb_next = list_rbl; /* Attach to global list */
507 list_rbl = rbl;
508 }
509
510 /*
511 * Catch the signal and die
512 */
513 static void
514 terminate(int dummy)
515 {
516 #ifdef WARMSTART
517 syslog(LOG_ERR,
518 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
519 write_warmstart(); /* Dump yourself */
520 #endif
521 exit(2);
522 }
523
524 void
525 rpcbind_abort()
526 {
527 #ifdef WARMSTART
528 write_warmstart(); /* Dump yourself */
529 #endif
530 abort();
531 }
532
533 /* get command line options */
534 static void
535 parseargs(int argc, char *argv[])
536 {
537 int c;
538
539 while ((c = getopt(argc, argv, "dwailLs")) != -1) {
540 switch (c) {
541 case 'a':
542 doabort = 1; /* when debugging, do an abort on */
543 break; /* errors; for rpcbind developers */
544 /* only! */
545 case 'd':
546 debugging = 1;
547 break;
548 case 'i':
549 insecure = 1;
550 break;
551 case 'L':
552 oldstyle_local = 1;
553 break;
554 case 'l':
555 verboselog = 1;
556 break;
557 case 's':
558 runasdaemon = 1;
559 break;
560 #ifdef WARMSTART
561 case 'w':
562 warmstart = 1;
563 break;
564 #endif
565 default: /* error */
566 fprintf(stderr, "usage: rpcbind [-Idwils]\n");
567 exit (1);
568 }
569 }
570 if (doabort && !debugging) {
571 fprintf(stderr,
572 "-a (abort) specified without -d (debugging) -- ignored.\n");
573 doabort = 0;
574 }
575 }
576
577 void
578 reap(int dummy)
579 {
580 int save_errno = errno;
581
582 while (wait3(NULL, WNOHANG, NULL) > 0)
583 ;
584 errno = save_errno;
585 }
586
587 void
588 toggle_verboselog(int dummy)
589 {
590 verboselog = !verboselog;
591 }
592