1 /* $NetBSD: rpcbind.c,v 1.5 2015/08/21 14:19:10 christos Exp $ */
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.
29 * Mountain View, California 94043
32 * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
35 /* #ident "@(#)rpcbind.c 1.19 94/04/25 SMI" */
39 static char sccsid
[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro";
45 * Implements the program, version to address mapping for rpc.
49 #include <sys/types.h>
51 #include <sys/errno.h>
53 #include <sys/resource.h>
55 #include <sys/signal.h>
56 #include <sys/socket.h>
60 #include <netinet/in.h>
64 #include <netconfig.h>
71 #include <semaphore.h>
76 #include <rump/rump.h>
77 #include <rump/rump_syscalls.h>
79 /* Global variables */
80 int debugging
= 1; /* Tell me what's going on */
81 int doabort
= 0; /* When debugging, do an abort on errors */
82 rpcblist_ptr list_rbl
; /* A list of version 3/4 rpcbind services */
84 #include "svc_fdset.h"
86 /* who to suid to if -s is given */
87 #define RUN_AS "daemon"
91 int oldstyle_local
= 0;
96 static int warmstart
= 0; /* Grab a old copy of registrations */
100 struct pmaplist
*list_pml
; /* A list of version 2 rpcbind services */
101 const char *udptrans
; /* Name of UDP transport */
102 const char *tcptrans
; /* Name of TCP transport */
103 const char *udp_uaddr
; /* Universal UDP address */
104 const char *tcp_uaddr
; /* Universal TCP address */
106 static const char servname
[] = "sunrpc";
108 const char rpcbind_superuser
[] = "superuser";
109 const char rpcbind_unknown
[] = "unknown";
111 static int init_transport(struct netconfig
*);
112 static void rbllist_add(rpcprog_t
, rpcvers_t
, struct netconfig
*,
114 static void terminate(int);
116 static void parseargs(int, char *[]);
119 int rpcbind_main(void *);
121 rpcbind_main(void *arg
)
123 struct netconfig
*nconf
;
124 void *nc_handle
; /* Net config handle */
126 int maxrec
= RPC_MAXDATASIZE
;
130 parseargs(argc
, argv
);
135 getrlimit(RLIMIT_NOFILE
, &rl
);
136 if (rl
.rlim_cur
< 128) {
137 if (rl
.rlim_max
<= 128)
138 rl
.rlim_cur
= rl
.rlim_max
;
141 setrlimit(RLIMIT_NOFILE
, &rl
);
144 if (geteuid()) /* This command allowed only to root */
145 errx(1, "Sorry. You are not superuser");
147 nc_handle
= setnetconfig(); /* open netconfig file */
148 if (nc_handle
== NULL
)
149 errx(1, "could not read /etc/netconfig");
155 nconf
= getnetconfigent("local");
157 errx(1, "can't find local transport");
159 rpc_control(RPC_SVC_CONNMAXREC_SET
, &maxrec
);
161 init_transport(nconf
);
163 while ((nconf
= getnetconfig(nc_handle
))) {
164 if (nconf
->nc_flag
& NC_VISIBLE
)
165 init_transport(nconf
);
167 endnetconfig(nc_handle
);
169 /* catch the usual termination signals for graceful exit */
170 (void) signal(SIGCHLD
, reap
);
171 (void) signal(SIGINT
, terminate
);
172 (void) signal(SIGTERM
, terminate
);
173 (void) signal(SIGQUIT
, terminate
);
174 /* ignore others that could get sent */
175 (void) signal(SIGPIPE
, SIG_IGN
);
176 //(void) signal(SIGHUP, SIG_IGN); used by mountd
177 (void) signal(SIGUSR1
, SIG_IGN
);
178 (void) signal(SIGUSR2
, SIG_IGN
);
185 printf("rpcbind debugging enabled.");
187 printf(" Will abort on errors!\n");
193 err(1, "fork failed");
196 openlog("rpcbind", 0, LOG_DAEMON
);
202 if ((p
= getpwnam(RUN_AS
)) == NULL
) {
203 syslog(LOG_ERR
, "cannot get uid of daemon (%s)",
207 if (setuid(p
->pw_uid
) == -1) {
208 syslog(LOG_ERR
, "setuid to daemon failed: (%s)",
218 syslog(LOG_ERR
, "svc_run returned unexpectedly");
226 * Adds the entry into the rpcbind database.
227 * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
228 * Returns 0 if succeeds, else fails
231 init_transport(struct netconfig
*nconf
)
235 struct addrinfo hints
, *res
= NULL
;
236 struct __rpc_sockinfo si
;
238 int aicode
, status
, addrlen
;
240 struct sockaddr_un sun
;
243 if ((nconf
->nc_semantics
!= NC_TPI_CLTS
) &&
244 (nconf
->nc_semantics
!= NC_TPI_COTS
) &&
245 (nconf
->nc_semantics
!= NC_TPI_COTS_ORD
))
246 return 1; /* not my type */
252 (void)fprintf(stderr
, "%s: %ld lookup routines :\n",
253 nconf
->nc_netid
, nconf
->nc_nlookups
);
254 for (i
= 0, s
= nconf
->nc_lookups
; i
< nconf
->nc_nlookups
;
256 (void)fprintf(stderr
, "[%d] - %s\n", i
, *s
);
261 * XXX - using RPC library internal functions.
263 if ((fd
= __rpc_nconf2fd(nconf
)) < 0) {
264 if (errno
== EAFNOSUPPORT
)
266 warn("Cannot create socket for `%s'", nconf
->nc_netid
);
270 if (!__rpc_nconf2sockinfo(nconf
, &si
)) {
271 warnx("Cannot get information for `%s'", nconf
->nc_netid
);
275 if (si
.si_af
== AF_INET6
) {
277 * We're doing host-based access checks here, so don't allow
278 * v4-in-v6 to confuse things.
280 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &one
,
282 warn("Can't make socket ipv6 only");
288 if (!strcmp(nconf
->nc_netid
, "local")) {
289 (void)memset(&sun
, 0, sizeof sun
);
290 sun
.sun_family
= AF_LOCAL
;
291 (void)rump_sys_unlink(_PATH_RPCBINDSOCK
);
292 (void)strlcpy(sun
.sun_path
, _PATH_RPCBINDSOCK
,
293 sizeof(sun
.sun_path
));
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 (void)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
;
305 if ((aicode
= getaddrinfo(NULL
, servname
, &hints
, &res
)) != 0) {
306 warnx("Cannot get local address for `%s' (%s)",
307 nconf
->nc_netid
, gai_strerror(aicode
));
310 addrlen
= res
->ai_addrlen
;
311 sa
= (struct sockaddr
*)res
->ai_addr
;
314 if (bind(fd
, sa
, addrlen
) < 0) {
315 warn("Cannot bind `%s'", nconf
->nc_netid
);
321 if (sa
->sa_family
== AF_LOCAL
)
322 if (rump_sys_chmod(sun
.sun_path
, S_IRWXU
|S_IRWXG
|S_IRWXO
) == -1)
323 warn("Cannot chmod `%s'", sun
.sun_path
);
326 /* Copy the address */
327 taddr
.addr
.len
= taddr
.addr
.maxlen
= addrlen
;
328 taddr
.addr
.buf
= malloc(addrlen
);
329 if (taddr
.addr
.buf
== NULL
) {
330 warn("Cannot allocate memory for `%s' address",
336 (void)memcpy(taddr
.addr
.buf
, sa
, addrlen
);
339 /* for debugging print out our universal address */
344 nb
.len
= nb
.maxlen
= sa
->sa_len
;
345 uaddr
= taddr2uaddr(nconf
, &nb
);
346 (void)fprintf(stderr
, "rpcbind: my address is %s\n", uaddr
);
354 if (nconf
->nc_semantics
!= NC_TPI_CLTS
)
355 listen(fd
, SOMAXCONN
);
357 my_xprt
= (SVCXPRT
*)svc_tli_create(fd
, nconf
, &taddr
, RPC_MAXDATASIZE
,
359 if (my_xprt
== NULL
) {
360 warnx("Could not create service for `%s'", nconf
->nc_netid
);
366 * Register both the versions for tcp/ip, udp/ip and local.
368 if ((strcmp(nconf
->nc_protofmly
, NC_INET
) == 0 &&
369 (strcmp(nconf
->nc_proto
, NC_TCP
) == 0 ||
370 strcmp(nconf
->nc_proto
, NC_UDP
) == 0)) ||
371 strcmp(nconf
->nc_netid
, "local") == 0) {
372 struct pmaplist
*pml
;
374 if (!svc_register(my_xprt
, PMAPPROG
, PMAPVERS
,
376 warn("Could not register on `%s'", nconf
->nc_netid
);
379 pml
= malloc(sizeof (struct pmaplist
));
381 warn("Cannot allocate memory");
384 pml
->pml_map
.pm_prog
= PMAPPROG
;
385 pml
->pml_map
.pm_vers
= PMAPVERS
;
386 pml
->pml_map
.pm_port
= PMAPPORT
;
387 if (strcmp(nconf
->nc_proto
, NC_TCP
) == 0) {
390 "Cannot have more than one TCP transport");
394 tcptrans
= strdup(nconf
->nc_netid
);
395 if (tcptrans
== NULL
) {
397 warn("Cannot allocate memory");
400 pml
->pml_map
.pm_prot
= IPPROTO_TCP
;
402 /* Let's snarf the universal address */
403 /* "h1.h2.h3.h4.p1.p2" */
404 tcp_uaddr
= taddr2uaddr(nconf
, &taddr
.addr
);
405 } else if (strcmp(nconf
->nc_proto
, NC_UDP
) == 0) {
409 "Cannot have more than one UDP transport");
412 udptrans
= strdup(nconf
->nc_netid
);
413 if (udptrans
== NULL
) {
415 warn("Cannot allocate memory");
418 pml
->pml_map
.pm_prot
= IPPROTO_UDP
;
420 /* Let's snarf the universal address */
421 /* "h1.h2.h3.h4.p1.p2" */
422 udp_uaddr
= taddr2uaddr(nconf
, &taddr
.addr
);
424 pml
->pml_next
= list_pml
;
427 /* Add version 3 information */
428 pml
= malloc(sizeof (struct pmaplist
));
430 warn("Cannot allocate memory");
433 pml
->pml_map
= list_pml
->pml_map
;
434 pml
->pml_map
.pm_vers
= RPCBVERS
;
435 pml
->pml_next
= list_pml
;
438 /* Add version 4 information */
439 pml
= malloc(sizeof (struct pmaplist
));
441 warn("Cannot allocate memory");
444 pml
->pml_map
= list_pml
->pml_map
;
445 pml
->pml_map
.pm_vers
= RPCBVERS4
;
446 pml
->pml_next
= list_pml
;
449 /* Also add version 2 stuff to rpcbind list */
450 rbllist_add(PMAPPROG
, PMAPVERS
, nconf
, &taddr
.addr
);
454 /* version 3 registration */
455 if (!svc_reg(my_xprt
, RPCBPROG
, RPCBVERS
, rpcb_service_3
, NULL
)) {
456 warn("Could not register %s version 3", nconf
->nc_netid
);
459 rbllist_add(RPCBPROG
, RPCBVERS
, nconf
, &taddr
.addr
);
461 /* version 4 registration */
462 if (!svc_reg(my_xprt
, RPCBPROG
, RPCBVERS4
, rpcb_service_4
, NULL
)) {
463 warn("Could not register %s version 4", nconf
->nc_netid
);
466 rbllist_add(RPCBPROG
, RPCBVERS4
, nconf
, &taddr
.addr
);
468 /* decide if bound checking works for this transport */
469 status
= add_bndlist(nconf
, &taddr
.addr
);
473 fprintf(stderr
, "Error in finding bind status for %s\n",
475 } else if (status
== 0) {
476 fprintf(stderr
, "check binding for %s\n",
478 } else if (status
> 0) {
479 fprintf(stderr
, "No check binding for %s\n",
487 * rmtcall only supported on CLTS transports for now.
489 if (nconf
->nc_semantics
== NC_TPI_CLTS
) {
490 status
= create_rmtcall_fd(nconf
);
496 "Could not create rmtcall fd for %s\n",
499 fprintf(stderr
, "rmtcall fd for %s is %d\n",
500 nconf
->nc_netid
, status
);
507 (void)rump_sys_close(fd
);
512 rbllist_add(rpcprog_t prog
, rpcvers_t vers
, struct netconfig
*nconf
,
517 rbl
= malloc(sizeof(rpcblist
));
519 warn("Out of memory");
523 rbl
->rpcb_map
.r_prog
= prog
;
524 rbl
->rpcb_map
.r_vers
= vers
;
525 rbl
->rpcb_map
.r_netid
= strdup(nconf
->nc_netid
);
526 rbl
->rpcb_map
.r_addr
= taddr2uaddr(nconf
, addr
);
527 rbl
->rpcb_map
.r_owner
= strdup(rpcbind_superuser
);
528 rbl
->rpcb_next
= list_rbl
; /* Attach to global list */
533 * Catch the signal and die
540 "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
541 write_warmstart(); /* Dump yourself */
550 write_warmstart(); /* Dump yourself */
556 /* get command line options */
558 parseargs(int argc
, char *argv
[])
562 while ((c
= getopt(argc
, argv
, "dwailLs")) != -1) {
565 doabort
= 1; /* when debugging, do an abort on */
566 break; /* errors; for rpcbind developers */
589 fprintf(stderr
, "usage: rpcbind [-Idwils]\n");
593 if (doabort
&& !debugging
) {
595 "-a (abort) specified without -d (debugging) -- ignored.\n");
604 int save_errno
= errno
;
606 while (wait3(NULL
, WNOHANG
, NULL
) > 0)
612 toggle_verboselog(int dummy
)
614 verboselog
= !verboselog
;