2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)rpcbind.c 1.19 94/04/25 SMI; 1.35 89/04/21 Copyr 1984 Sun Micro
29 * $NetBSD: rpcbind.c,v 1.3 2002/11/08 00:16:40 fvdl Exp $
30 * $FreeBSD: src/usr.sbin/rpcbind/rpcbind.c,v 1.20 2008/02/14 20:12:23 yar Exp $
33 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
38 * Implements the program, version to address mapping for rpc.
42 #include <sys/types.h>
44 #include <sys/errno.h>
46 #include <sys/resource.h>
48 #include <sys/signal.h>
49 #include <sys/socket.h>
52 #include <rpc/rpc_com.h>
54 #include <netinet/in.h>
56 #include <arpa/inet.h>
60 #include <netconfig.h>
71 /* Global variables */
72 int debugging
= 0; /* Tell me what's going on */
73 int doabort
= 0; /* When debugging, do an abort on errors */
74 rpcblist_ptr list_rbl
; /* A list of version 3/4 rpcbind services */
76 /* who to suid to if -s is given */
77 #define RUN_AS "daemon"
79 #define RPCBINDDLOCK "/var/run/rpcbind.lock"
83 int oldstyle_local
= 0;
94 static int warmstart
= 0; /* Grab an old copy of registrations. */
98 struct pmaplist
*list_pml
; /* A list of version 2 rpcbind services */
99 char *udptrans
; /* Name of UDP transport */
100 char *tcptrans
; /* Name of TCP transport */
101 char *udp_uaddr
; /* Universal UDP address */
102 char *tcp_uaddr
; /* Universal TCP address */
104 static char servname
[] = "rpcbind";
105 static char superuser
[] = "superuser";
107 static int init_transport(struct netconfig
*);
108 static void rbllist_add(rpcprog_t
, rpcvers_t
, struct netconfig
*,
110 static void terminate(int);
111 static void parseargs(int, char *[]);
114 main(int argc
, char *argv
[])
116 struct netconfig
*nconf
;
117 void *nc_handle
; /* Net config handle */
119 int maxrec
= RPC_MAXDATASIZE
;
121 parseargs(argc
, argv
);
123 /* Check that another rpcbind isn't already running. */
124 if ((rpcbindlockfd
= (open(RPCBINDDLOCK
,
125 O_RDONLY
|O_CREAT
, 0444))) == -1)
126 err(1, "%s", RPCBINDDLOCK
);
128 if(flock(rpcbindlockfd
, LOCK_EX
|LOCK_NB
) == -1 && errno
== EWOULDBLOCK
)
129 errx(1, "another rpcbind is already running. Aborting");
131 getrlimit(RLIMIT_NOFILE
, &rl
);
132 if (rl
.rlim_cur
< 128) {
133 if (rl
.rlim_max
<= 128)
134 rl
.rlim_cur
= rl
.rlim_max
;
137 setrlimit(RLIMIT_NOFILE
, &rl
);
139 openlog("rpcbind", LOG_CONS
, LOG_DAEMON
);
140 if (geteuid()) { /* This command allowed only to root */
141 fprintf(stderr
, "Sorry. You are not superuser\n");
144 nc_handle
= setnetconfig(); /* open netconfig file */
145 if (nc_handle
== NULL
) {
146 syslog(LOG_ERR
, "could not read /etc/netconfig");
154 nconf
= getnetconfigent("local");
156 nconf
= getnetconfigent("unix");
158 syslog(LOG_ERR
, "%s: can't find local transport\n", argv
[0]);
162 rpc_control(RPC_SVC_CONNMAXREC_SET
, &maxrec
);
164 init_transport(nconf
);
166 while ((nconf
= getnetconfig(nc_handle
))) {
167 if (nconf
->nc_flag
& NC_VISIBLE
)
168 if (ipv6_only
!= 1 || strcmp(nconf
->nc_protofmly
, "inet") != 0)
169 init_transport(nconf
);
171 endnetconfig(nc_handle
);
173 /* catch the usual termination signals for graceful exit */
174 signal(SIGCHLD
, reap
);
175 signal(SIGINT
, terminate
);
176 signal(SIGTERM
, terminate
);
177 signal(SIGQUIT
, terminate
);
178 /* ignore others that could get sent */
179 signal(SIGPIPE
, SIG_IGN
);
180 signal(SIGHUP
, SIG_IGN
);
181 signal(SIGUSR1
, SIG_IGN
);
182 signal(SIGUSR2
, SIG_IGN
);
189 printf("rpcbind debugging enabled.");
191 printf(" Will abort on errors!\n");
197 err(1, "fork failed");
203 if((p
= getpwnam(RUN_AS
)) == NULL
) {
204 syslog(LOG_ERR
, "cannot get uid of daemon: %m");
207 if (setuid(p
->pw_uid
) == -1) {
208 syslog(LOG_ERR
, "setuid to daemon failed: %m");
216 syslog(LOG_ERR
, "svc_run returned unexpectedly");
224 * Adds the entry into the rpcbind database.
225 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
226 * Returns 0 if succeeds, else fails
229 init_transport(struct netconfig
*nconf
)
233 struct addrinfo hints
, *res
= NULL
;
234 struct __rpc_sockinfo si
;
236 int status
; /* bound checking ? */
242 u_int32_t host_addr
[4]; /* IPv4 or IPv6 */
243 struct sockaddr_un sun
;
246 if ((nconf
->nc_semantics
!= NC_TPI_CLTS
) &&
247 (nconf
->nc_semantics
!= NC_TPI_COTS
) &&
248 (nconf
->nc_semantics
!= NC_TPI_COTS_ORD
))
249 return (1); /* not my type */
255 fprintf(stderr
, "%s: %ld lookup routines :\n",
256 nconf
->nc_netid
, nconf
->nc_nlookups
);
257 for (i
= 0, s
= nconf
->nc_lookups
; i
< nconf
->nc_nlookups
;
259 fprintf(stderr
, "[%d] - %s\n", i
, *s
);
264 * XXX - using RPC library internal functions.
266 if ((strcmp(nconf
->nc_netid
, "local") == 0) ||
267 (strcmp(nconf
->nc_netid
, "unix") == 0)) {
269 * For other transports we call this later, for each socket we
272 if ((fd
= __rpc_nconf2fd(nconf
)) < 0) {
274 if (errno
== EPROTONOSUPPORT
)
276 syslog(non_fatal
?LOG_DEBUG
:LOG_ERR
, "cannot create socket for %s",
282 if (!__rpc_nconf2sockinfo(nconf
, &si
)) {
283 syslog(LOG_ERR
, "cannot get information for %s",
288 if ((strcmp(nconf
->nc_netid
, "local") == 0) ||
289 (strcmp(nconf
->nc_netid
, "unix") == 0)) {
290 memset(&sun
, 0, sizeof sun
);
291 sun
.sun_family
= AF_LOCAL
;
292 unlink(_PATH_RPCBINDSOCK
);
293 strcpy(sun
.sun_path
, _PATH_RPCBINDSOCK
);
294 sun
.sun_len
= SUN_LEN(&sun
);
295 addrlen
= sizeof (struct sockaddr_un
);
296 sa
= (struct sockaddr
*)&sun
;
298 /* Get rpcbind's address on this transport */
300 memset(&hints
, 0, sizeof hints
);
301 hints
.ai_flags
= AI_PASSIVE
;
302 hints
.ai_family
= si
.si_af
;
303 hints
.ai_socktype
= si
.si_socktype
;
304 hints
.ai_protocol
= si
.si_proto
;
307 if ((strcmp(nconf
->nc_netid
, "local") != 0) &&
308 (strcmp(nconf
->nc_netid
, "unix") != 0)) {
310 * If no hosts were specified, just bind to INADDR_ANY.
311 * Otherwise make sure 127.0.0.1 is added to the list.
315 hosts
= realloc(hosts
, nhostsbak
* sizeof(char *));
319 if (hints
.ai_family
== AF_INET
) {
320 hosts
[nhostsbak
- 1] = "127.0.0.1";
321 } else if (hints
.ai_family
== AF_INET6
) {
322 hosts
[nhostsbak
- 1] = "::1";
328 * Bind to specific IPs if asked to
331 while (nhostsbak
> 0) {
334 * XXX - using RPC library internal functions.
336 if ((fd
= __rpc_nconf2fd(nconf
)) < 0) {
338 if (errno
== EPROTONOSUPPORT
&&
339 nconf
->nc_semantics
!= NC_TPI_CLTS
)
341 syslog(non_fatal
? LOG_DEBUG
: LOG_ERR
,
342 "cannot create socket for %s", nconf
->nc_netid
);
345 switch (hints
.ai_family
) {
347 if (inet_pton(AF_INET
, hosts
[nhostsbak
],
349 hints
.ai_flags
&= AI_NUMERICHOST
;
352 * Skip if we have an AF_INET6 address.
354 if (inet_pton(AF_INET6
,
355 hosts
[nhostsbak
], host_addr
) == 1) {
362 if (inet_pton(AF_INET6
, hosts
[nhostsbak
],
364 hints
.ai_flags
&= AI_NUMERICHOST
;
367 * Skip if we have an AF_INET address.
369 if (inet_pton(AF_INET
, hosts
[nhostsbak
],
375 if (setsockopt(fd
, IPPROTO_IPV6
,
376 IPV6_V6ONLY
, &on
, sizeof on
) < 0) {
378 "can't set v6-only binding for "
388 * If no hosts were specified, just bind to INADDR_ANY
390 if (strcmp("*", hosts
[nhostsbak
]) == 0)
391 hosts
[nhostsbak
] = NULL
;
392 if ((strcmp(nconf
->nc_netid
, "local") != 0) &&
393 (strcmp(nconf
->nc_netid
, "unix") != 0)) {
394 if ((aicode
= getaddrinfo(hosts
[nhostsbak
],
395 servname
, &hints
, &res
)) != 0) {
397 "cannot get local address for %s: %s",
398 nconf
->nc_netid
, gai_strerror(aicode
));
401 addrlen
= res
->ai_addrlen
;
402 sa
= (struct sockaddr
*)res
->ai_addr
;
404 oldmask
= umask(S_IXUSR
|S_IXGRP
|S_IXOTH
);
405 if (bind(fd
, sa
, addrlen
) != 0) {
406 syslog(LOG_ERR
, "cannot bind %s on %s: %m",
407 (hosts
[nhostsbak
] == NULL
) ? "*" :
408 hosts
[nhostsbak
], nconf
->nc_netid
);
416 /* Copy the address */
417 taddr
.addr
.len
= taddr
.addr
.maxlen
= addrlen
;
418 taddr
.addr
.buf
= malloc(addrlen
);
419 if (taddr
.addr
.buf
== NULL
) {
421 "cannot allocate memory for %s address",
427 memcpy(taddr
.addr
.buf
, sa
, addrlen
);
431 * for debugging print out our universal
438 nb
.len
= nb
.maxlen
= sa
->sa_len
;
439 uaddr
= taddr2uaddr(nconf
, &nb
);
440 fprintf(stderr
, "rpcbind : my address is %s\n", uaddr
);
445 if (nconf
->nc_semantics
!= NC_TPI_CLTS
)
446 listen(fd
, SOMAXCONN
);
448 my_xprt
= (SVCXPRT
*)svc_tli_create(fd
, nconf
, &taddr
,
449 RPC_MAXDATASIZE
, RPC_MAXDATASIZE
);
450 if (my_xprt
== NULL
) {
451 syslog(LOG_ERR
, "%s: could not create service",
459 oldmask
= umask(S_IXUSR
|S_IXGRP
|S_IXOTH
);
460 if (bind(fd
, sa
, addrlen
) < 0) {
461 syslog(LOG_ERR
, "cannot bind %s: %m", nconf
->nc_netid
);
468 /* Copy the address */
469 taddr
.addr
.len
= taddr
.addr
.maxlen
= addrlen
;
470 taddr
.addr
.buf
= malloc(addrlen
);
471 if (taddr
.addr
.buf
== NULL
) {
472 syslog(LOG_ERR
, "cannot allocate memory for %s address",
478 memcpy(taddr
.addr
.buf
, sa
, addrlen
);
481 /* for debugging print out our universal address */
486 nb
.len
= nb
.maxlen
= sa
->sa_len
;
487 uaddr
= taddr2uaddr(nconf
, &nb
);
488 fprintf(stderr
, "rpcbind : my address is %s\n", uaddr
);
493 if (nconf
->nc_semantics
!= NC_TPI_CLTS
)
494 listen(fd
, SOMAXCONN
);
496 my_xprt
= (SVCXPRT
*)svc_tli_create(fd
, nconf
, &taddr
,
497 RPC_MAXDATASIZE
, RPC_MAXDATASIZE
);
498 if (my_xprt
== NULL
) {
499 syslog(LOG_ERR
, "%s: could not create service",
507 * Register both the versions for tcp/ip, udp/ip and local.
509 if ((strcmp(nconf
->nc_protofmly
, NC_INET
) == 0 &&
510 (strcmp(nconf
->nc_proto
, NC_TCP
) == 0 ||
511 strcmp(nconf
->nc_proto
, NC_UDP
) == 0)) ||
512 (strcmp(nconf
->nc_netid
, "unix") == 0) ||
513 (strcmp(nconf
->nc_netid
, "local") == 0)) {
514 struct pmaplist
*pml
;
516 if (!svc_register(my_xprt
, PMAPPROG
, PMAPVERS
,
518 syslog(LOG_ERR
, "could not register on %s",
522 pml
= malloc(sizeof (struct pmaplist
));
524 syslog(LOG_ERR
, "no memory!");
527 pml
->pml_map
.pm_prog
= PMAPPROG
;
528 pml
->pml_map
.pm_vers
= PMAPVERS
;
529 pml
->pml_map
.pm_port
= PMAPPORT
;
530 if (strcmp(nconf
->nc_proto
, NC_TCP
) == 0) {
533 "cannot have more than one TCP transport");
536 tcptrans
= strdup(nconf
->nc_netid
);
537 pml
->pml_map
.pm_prot
= IPPROTO_TCP
;
539 /* Let's snarf the universal address */
540 /* "h1.h2.h3.h4.p1.p2" */
541 tcp_uaddr
= taddr2uaddr(nconf
, &taddr
.addr
);
542 } else if (strcmp(nconf
->nc_proto
, NC_UDP
) == 0) {
545 "cannot have more than one UDP transport");
548 udptrans
= strdup(nconf
->nc_netid
);
549 pml
->pml_map
.pm_prot
= IPPROTO_UDP
;
551 /* Let's snarf the universal address */
552 /* "h1.h2.h3.h4.p1.p2" */
553 udp_uaddr
= taddr2uaddr(nconf
, &taddr
.addr
);
554 } else if (strcmp(nconf
->nc_netid
, "local") == 0)
555 pml
->pml_map
.pm_prot
= IPPROTO_ST
;
556 else if (strcmp(nconf
->nc_netid
, "unix") == 0)
557 pml
->pml_map
.pm_prot
= IPPROTO_ST
;
558 pml
->pml_next
= list_pml
;
561 /* Add version 3 information */
562 pml
= malloc(sizeof (struct pmaplist
));
564 syslog(LOG_ERR
, "no memory!");
567 pml
->pml_map
= list_pml
->pml_map
;
568 pml
->pml_map
.pm_vers
= RPCBVERS
;
569 pml
->pml_next
= list_pml
;
572 /* Add version 4 information */
573 pml
= malloc (sizeof (struct pmaplist
));
575 syslog(LOG_ERR
, "no memory!");
578 pml
->pml_map
= list_pml
->pml_map
;
579 pml
->pml_map
.pm_vers
= RPCBVERS4
;
580 pml
->pml_next
= list_pml
;
583 /* Also add version 2 stuff to rpcbind list */
584 rbllist_add(PMAPPROG
, PMAPVERS
, nconf
, &taddr
.addr
);
588 /* version 3 registration */
589 if (!svc_reg(my_xprt
, RPCBPROG
, RPCBVERS
, rpcb_service_3
, NULL
)) {
590 syslog(LOG_ERR
, "could not register %s version 3",
594 rbllist_add(RPCBPROG
, RPCBVERS
, nconf
, &taddr
.addr
);
596 /* version 4 registration */
597 if (!svc_reg(my_xprt
, RPCBPROG
, RPCBVERS4
, rpcb_service_4
, NULL
)) {
598 syslog(LOG_ERR
, "could not register %s version 4",
602 rbllist_add(RPCBPROG
, RPCBVERS4
, nconf
, &taddr
.addr
);
604 /* decide if bound checking works for this transport */
605 status
= add_bndlist(nconf
, &taddr
.addr
);
609 fprintf(stderr
, "Error in finding bind status for %s\n",
611 } else if (status
== 0) {
612 fprintf(stderr
, "check binding for %s\n",
614 } else if (status
> 0) {
615 fprintf(stderr
, "No check binding for %s\n",
621 * rmtcall only supported on CLTS transports for now.
623 if (nconf
->nc_semantics
== NC_TPI_CLTS
) {
624 status
= create_rmtcall_fd(nconf
);
630 "Could not create rmtcall fd for %s\n",
633 fprintf(stderr
, "rmtcall fd for %s is %d\n",
634 nconf
->nc_netid
, status
);
646 rbllist_add(rpcprog_t prog
, rpcvers_t vers
, struct netconfig
*nconf
,
651 rbl
= malloc(sizeof (rpcblist
));
653 syslog(LOG_ERR
, "no memory!");
657 rbl
->rpcb_map
.r_prog
= prog
;
658 rbl
->rpcb_map
.r_vers
= vers
;
659 rbl
->rpcb_map
.r_netid
= strdup(nconf
->nc_netid
);
660 rbl
->rpcb_map
.r_addr
= taddr2uaddr(nconf
, addr
);
661 rbl
->rpcb_map
.r_owner
= strdup(superuser
);
662 rbl
->rpcb_next
= list_rbl
; /* Attach to global list */
667 * Catch the signal and die
670 terminate(int dummy __unused
)
672 close(rpcbindlockfd
);
675 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
676 write_warmstart(); /* Dump yourself */
685 write_warmstart(); /* Dump yourself */
690 /* get command line options */
692 parseargs(int argc
, char *argv
[])
701 while ((c
= getopt(argc
, argv
, "6adh:iLls" WSOP
)) != -1) {
707 doabort
= 1; /* when debugging, do an abort on */
708 break; /* errors; for rpcbind developers */
715 hosts
= realloc(hosts
, nhosts
* sizeof(char *));
717 errx(1, "Out of memory");
718 hosts
[nhosts
- 1] = strdup(optarg
);
719 if (hosts
[nhosts
- 1] == NULL
)
720 errx(1, "Out of memory");
741 "usage: rpcbind [-6adiLls%s] [-h bindip]\n",
746 if (doabort
&& !debugging
) {
748 "-a (abort) specified without -d (debugging) -- ignored.\n");
755 reap(int dummy __unused
)
757 int save_errno
= errno
;
759 while (wait3(NULL
, WNOHANG
, NULL
) > 0)
765 toggle_verboselog(int dummy __unused
)
767 verboselog
= !verboselog
;