rpcbind.c revision 1.23 1 1.23 christos /* $NetBSD: rpcbind.c,v 1.23 2015/11/08 16:36:28 christos Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.1 fvdl * unrestricted use provided that this legend is included on all tape
6 1.1 fvdl * media and as a part of the software program in whole or part. Users
7 1.1 fvdl * may copy or modify Sun RPC without charge, but are not authorized
8 1.1 fvdl * to license or distribute it to anyone else except as part of a product or
9 1.1 fvdl * program developed by the user.
10 1.1 fvdl *
11 1.1 fvdl * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.1 fvdl * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.1 fvdl * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.1 fvdl *
15 1.1 fvdl * Sun RPC is provided with no support and without any obligation on the
16 1.1 fvdl * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.1 fvdl * modification or enhancement.
18 1.1 fvdl *
19 1.1 fvdl * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.1 fvdl * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.1 fvdl * OR ANY PART THEREOF.
22 1.1 fvdl *
23 1.1 fvdl * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.1 fvdl * or profits or other special, indirect and consequential damages, even if
25 1.1 fvdl * Sun has been advised of the possibility of such damages.
26 1.1 fvdl *
27 1.1 fvdl * Sun Microsystems, Inc.
28 1.1 fvdl * 2550 Garcia Avenue
29 1.1 fvdl * Mountain View, California 94043
30 1.1 fvdl */
31 1.1 fvdl /*
32 1.1 fvdl * Copyright (c) 1984 - 1991 by Sun Microsystems, Inc.
33 1.1 fvdl */
34 1.1 fvdl
35 1.1 fvdl /* #ident "@(#)rpcbind.c 1.19 94/04/25 SMI" */
36 1.1 fvdl
37 1.1 fvdl #if 0
38 1.1 fvdl #ifndef lint
39 1.1 fvdl static char sccsid[] = "@(#)rpcbind.c 1.35 89/04/21 Copyr 1984 Sun Micro";
40 1.1 fvdl #endif
41 1.1 fvdl #endif
42 1.1 fvdl
43 1.1 fvdl /*
44 1.1 fvdl * rpcbind.c
45 1.1 fvdl * Implements the program, version to address mapping for rpc.
46 1.1 fvdl *
47 1.1 fvdl */
48 1.1 fvdl
49 1.1 fvdl #include <sys/types.h>
50 1.1 fvdl #include <sys/stat.h>
51 1.1 fvdl #include <sys/errno.h>
52 1.1 fvdl #include <sys/time.h>
53 1.1 fvdl #include <sys/resource.h>
54 1.1 fvdl #include <sys/wait.h>
55 1.1 fvdl #include <sys/signal.h>
56 1.1 fvdl #include <sys/socket.h>
57 1.1 fvdl #include <sys/un.h>
58 1.1 fvdl #include <rpc/rpc.h>
59 1.1 fvdl #ifdef PORTMAP
60 1.1 fvdl #include <netinet/in.h>
61 1.1 fvdl #endif
62 1.1 fvdl #include <netdb.h>
63 1.1 fvdl #include <stdio.h>
64 1.1 fvdl #include <netconfig.h>
65 1.1 fvdl #include <stdlib.h>
66 1.1 fvdl #include <unistd.h>
67 1.1 fvdl #include <syslog.h>
68 1.1 fvdl #include <err.h>
69 1.1 fvdl #include <util.h>
70 1.1 fvdl #include <pwd.h>
71 1.1 fvdl #include <string.h>
72 1.1 fvdl #include <errno.h>
73 1.1 fvdl #include "rpcbind.h"
74 1.1 fvdl
75 1.23 christos #ifdef RPCBIND_RUMP
76 1.23 christos #include <semaphore.h>
77 1.23 christos
78 1.23 christos #include <rump/rump.h>
79 1.23 christos #include <rump/rump_syscalls.h>
80 1.23 christos
81 1.23 christos #include "svc_fdset.h"
82 1.23 christos
83 1.23 christos extern sem_t gensem;
84 1.23 christos #define DEBUGGING 1
85 1.23 christos #else
86 1.23 christos #define DEBUGGING 0
87 1.23 christos #endif
88 1.23 christos
89 1.1 fvdl /* Global variables */
90 1.23 christos int debugging = DEBUGGING; /* Tell me what's going on */
91 1.1 fvdl int doabort = 0; /* When debugging, do an abort on errors */
92 1.1 fvdl rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */
93 1.1 fvdl
94 1.1 fvdl /* who to suid to if -s is given */
95 1.1 fvdl #define RUN_AS "daemon"
96 1.1 fvdl
97 1.1 fvdl int runasdaemon = 0;
98 1.1 fvdl int insecure = 0;
99 1.1 fvdl int oldstyle_local = 0;
100 1.1 fvdl int verboselog = 0;
101 1.1 fvdl
102 1.1 fvdl #ifdef WARMSTART
103 1.1 fvdl /* Local Variable */
104 1.1 fvdl static int warmstart = 0; /* Grab a old copy of registrations */
105 1.1 fvdl #endif
106 1.1 fvdl
107 1.1 fvdl #ifdef PORTMAP
108 1.1 fvdl struct pmaplist *list_pml; /* A list of version 2 rpcbind services */
109 1.11 christos const char *udptrans; /* Name of UDP transport */
110 1.11 christos const char *tcptrans; /* Name of TCP transport */
111 1.11 christos const char *udp_uaddr; /* Universal UDP address */
112 1.11 christos const char *tcp_uaddr; /* Universal TCP address */
113 1.1 fvdl #endif
114 1.10 christos static const char servname[] = "sunrpc";
115 1.1 fvdl
116 1.13 christos const char rpcbind_superuser[] = "superuser";
117 1.13 christos const char rpcbind_unknown[] = "unknown";
118 1.1 fvdl
119 1.11 christos static int init_transport(struct netconfig *);
120 1.11 christos static void rbllist_add(rpcprog_t, rpcvers_t, struct netconfig *,
121 1.11 christos struct netbuf *);
122 1.17 joerg __dead static void terminate(int);
123 1.23 christos #ifndef RPCBIND_RUMP
124 1.11 christos static void parseargs(int, char *[]);
125 1.1 fvdl
126 1.1 fvdl int
127 1.1 fvdl main(int argc, char *argv[])
128 1.23 christos #else
129 1.23 christos int rpcbind_main(void *);
130 1.23 christos int
131 1.23 christos rpcbind_main(void *arg)
132 1.23 christos #endif
133 1.1 fvdl {
134 1.1 fvdl struct netconfig *nconf;
135 1.1 fvdl void *nc_handle; /* Net config handle */
136 1.1 fvdl struct rlimit rl;
137 1.3 fvdl int maxrec = RPC_MAXDATASIZE;
138 1.1 fvdl
139 1.23 christos #ifdef RPCBIND_RUMP
140 1.23 christos svc_fdset_init(SVC_FDSET_MT);
141 1.23 christos #else
142 1.1 fvdl parseargs(argc, argv);
143 1.23 christos #endif
144 1.1 fvdl
145 1.22 christos if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
146 1.22 christos err(EXIT_FAILURE, "getrlimit(RLIMIT_NOFILE)");
147 1.22 christos
148 1.22 christos if (rl.rlim_cur < 128) {
149 1.1 fvdl if (rl.rlim_max <= 128)
150 1.1 fvdl rl.rlim_cur = rl.rlim_max;
151 1.1 fvdl else
152 1.1 fvdl rl.rlim_cur = 128;
153 1.22 christos if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
154 1.21 dholland err(EXIT_FAILURE, "setrlimit(RLIMIT_NOFILE)");
155 1.1 fvdl }
156 1.1 fvdl nc_handle = setnetconfig(); /* open netconfig file */
157 1.11 christos if (nc_handle == NULL)
158 1.22 christos errx(EXIT_FAILURE, "could not read /etc/netconfig");
159 1.1 fvdl #ifdef PORTMAP
160 1.1 fvdl udptrans = "";
161 1.1 fvdl tcptrans = "";
162 1.1 fvdl #endif
163 1.1 fvdl
164 1.1 fvdl nconf = getnetconfigent("local");
165 1.11 christos if (nconf == NULL)
166 1.22 christos errx(EXIT_FAILURE, "can't find local transport");
167 1.3 fvdl
168 1.3 fvdl rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
169 1.3 fvdl
170 1.1 fvdl init_transport(nconf);
171 1.1 fvdl
172 1.1 fvdl while ((nconf = getnetconfig(nc_handle))) {
173 1.1 fvdl if (nconf->nc_flag & NC_VISIBLE)
174 1.1 fvdl init_transport(nconf);
175 1.1 fvdl }
176 1.1 fvdl endnetconfig(nc_handle);
177 1.1 fvdl
178 1.1 fvdl /* catch the usual termination signals for graceful exit */
179 1.1 fvdl (void) signal(SIGCHLD, reap);
180 1.1 fvdl (void) signal(SIGINT, terminate);
181 1.1 fvdl (void) signal(SIGTERM, terminate);
182 1.1 fvdl (void) signal(SIGQUIT, terminate);
183 1.1 fvdl /* ignore others that could get sent */
184 1.1 fvdl (void) signal(SIGPIPE, SIG_IGN);
185 1.23 christos #ifndef RPCBIND_RUMP
186 1.1 fvdl (void) signal(SIGHUP, SIG_IGN);
187 1.23 christos #endif
188 1.1 fvdl (void) signal(SIGUSR1, SIG_IGN);
189 1.1 fvdl (void) signal(SIGUSR2, SIG_IGN);
190 1.1 fvdl #ifdef WARMSTART
191 1.1 fvdl if (warmstart) {
192 1.1 fvdl read_warmstart();
193 1.1 fvdl }
194 1.1 fvdl #endif
195 1.1 fvdl if (debugging) {
196 1.1 fvdl printf("rpcbind debugging enabled.");
197 1.1 fvdl if (doabort) {
198 1.1 fvdl printf(" Will abort on errors!\n");
199 1.1 fvdl } else {
200 1.1 fvdl printf("\n");
201 1.1 fvdl }
202 1.1 fvdl } else {
203 1.1 fvdl if (daemon(0, 0))
204 1.22 christos err(EXIT_FAILURE, "fork failed");
205 1.1 fvdl }
206 1.11 christos
207 1.11 christos openlog("rpcbind", 0, LOG_DAEMON);
208 1.1 fvdl pidfile(NULL);
209 1.1 fvdl
210 1.1 fvdl if (runasdaemon) {
211 1.1 fvdl struct passwd *p;
212 1.1 fvdl
213 1.1 fvdl if((p = getpwnam(RUN_AS)) == NULL) {
214 1.1 fvdl syslog(LOG_ERR, "cannot get uid of daemon: %m");
215 1.22 christos exit(EXIT_FAILURE);
216 1.1 fvdl }
217 1.1 fvdl if (setuid(p->pw_uid) == -1) {
218 1.1 fvdl syslog(LOG_ERR, "setuid to daemon failed: %m");
219 1.22 christos exit(EXIT_FAILURE);
220 1.1 fvdl }
221 1.1 fvdl }
222 1.1 fvdl
223 1.1 fvdl network_init();
224 1.1 fvdl
225 1.23 christos #ifdef RPCBIND_RUMP
226 1.23 christos sem_post(&gensem);
227 1.23 christos #endif
228 1.1 fvdl my_svc_run();
229 1.1 fvdl syslog(LOG_ERR, "svc_run returned unexpectedly");
230 1.1 fvdl rpcbind_abort();
231 1.1 fvdl /* NOTREACHED */
232 1.1 fvdl
233 1.22 christos return EXIT_SUCCESS;
234 1.1 fvdl }
235 1.1 fvdl
236 1.1 fvdl /*
237 1.1 fvdl * Adds the entry into the rpcbind database.
238 1.1 fvdl * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also
239 1.1 fvdl * Returns 0 if succeeds, else fails
240 1.1 fvdl */
241 1.1 fvdl static int
242 1.1 fvdl init_transport(struct netconfig *nconf)
243 1.1 fvdl {
244 1.1 fvdl int fd;
245 1.1 fvdl struct t_bind taddr;
246 1.1 fvdl struct addrinfo hints, *res = NULL;
247 1.1 fvdl struct __rpc_sockinfo si;
248 1.1 fvdl SVCXPRT *my_xprt;
249 1.1 fvdl int status; /* bound checking ? */
250 1.1 fvdl int aicode;
251 1.1 fvdl int addrlen;
252 1.1 fvdl struct sockaddr *sa;
253 1.1 fvdl struct sockaddr_un sun;
254 1.6 fvdl const int one = 1;
255 1.1 fvdl
256 1.1 fvdl if ((nconf->nc_semantics != NC_TPI_CLTS) &&
257 1.1 fvdl (nconf->nc_semantics != NC_TPI_COTS) &&
258 1.1 fvdl (nconf->nc_semantics != NC_TPI_COTS_ORD))
259 1.11 christos return 1; /* not my type */
260 1.15 dsl #ifdef RPCBIND_DEBUG
261 1.1 fvdl if (debugging) {
262 1.1 fvdl int i;
263 1.1 fvdl char **s;
264 1.1 fvdl
265 1.11 christos (void)fprintf(stderr, "%s: %ld lookup routines :\n",
266 1.11 christos nconf->nc_netid, nconf->nc_nlookups);
267 1.1 fvdl for (i = 0, s = nconf->nc_lookups; i < nconf->nc_nlookups;
268 1.1 fvdl i++, s++)
269 1.11 christos (void)fprintf(stderr, "[%d] - %s\n", i, *s);
270 1.1 fvdl }
271 1.1 fvdl #endif
272 1.1 fvdl
273 1.1 fvdl /*
274 1.1 fvdl * XXX - using RPC library internal functions.
275 1.1 fvdl */
276 1.1 fvdl if ((fd = __rpc_nconf2fd(nconf)) < 0) {
277 1.14 christos if (errno == EAFNOSUPPORT)
278 1.14 christos return 1;
279 1.14 christos warn("Cannot create socket for `%s'", nconf->nc_netid);
280 1.11 christos return 1;
281 1.1 fvdl }
282 1.1 fvdl
283 1.1 fvdl if (!__rpc_nconf2sockinfo(nconf, &si)) {
284 1.11 christos warnx("Cannot get information for `%s'", nconf->nc_netid);
285 1.11 christos return 1;
286 1.1 fvdl }
287 1.6 fvdl
288 1.6 fvdl if (si.si_af == AF_INET6) {
289 1.6 fvdl /*
290 1.6 fvdl * We're doing host-based access checks here, so don't allow
291 1.6 fvdl * v4-in-v6 to confuse things.
292 1.6 fvdl */
293 1.6 fvdl if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
294 1.6 fvdl sizeof one) < 0) {
295 1.11 christos warn("Can't make socket ipv6 only");
296 1.11 christos return 1;
297 1.6 fvdl }
298 1.6 fvdl }
299 1.6 fvdl
300 1.1 fvdl
301 1.1 fvdl if (!strcmp(nconf->nc_netid, "local")) {
302 1.11 christos (void)memset(&sun, 0, sizeof sun);
303 1.1 fvdl sun.sun_family = AF_LOCAL;
304 1.23 christos #ifdef RPCBIND_RUMP
305 1.23 christos (void)rump_sys_unlink(_PATH_RPCBINDSOCK);
306 1.23 christos #else
307 1.11 christos (void)unlink(_PATH_RPCBINDSOCK);
308 1.23 christos #endif
309 1.11 christos (void)strlcpy(sun.sun_path, _PATH_RPCBINDSOCK,
310 1.11 christos sizeof(sun.sun_path));
311 1.1 fvdl sun.sun_len = SUN_LEN(&sun);
312 1.11 christos addrlen = sizeof(struct sockaddr_un);
313 1.1 fvdl sa = (struct sockaddr *)&sun;
314 1.1 fvdl } else {
315 1.1 fvdl /* Get rpcbind's address on this transport */
316 1.1 fvdl
317 1.11 christos (void)memset(&hints, 0, sizeof hints);
318 1.1 fvdl hints.ai_flags = AI_PASSIVE;
319 1.1 fvdl hints.ai_family = si.si_af;
320 1.1 fvdl hints.ai_socktype = si.si_socktype;
321 1.1 fvdl hints.ai_protocol = si.si_proto;
322 1.1 fvdl if ((aicode = getaddrinfo(NULL, servname, &hints, &res)) != 0) {
323 1.11 christos warnx("Cannot get local address for `%s' (%s)",
324 1.1 fvdl nconf->nc_netid, gai_strerror(aicode));
325 1.1 fvdl return 1;
326 1.1 fvdl }
327 1.1 fvdl addrlen = res->ai_addrlen;
328 1.1 fvdl sa = (struct sockaddr *)res->ai_addr;
329 1.1 fvdl }
330 1.1 fvdl
331 1.1 fvdl if (bind(fd, sa, addrlen) < 0) {
332 1.11 christos warn("Cannot bind `%s'", nconf->nc_netid);
333 1.1 fvdl if (res != NULL)
334 1.1 fvdl freeaddrinfo(res);
335 1.1 fvdl return 1;
336 1.1 fvdl }
337 1.23 christos #ifndef RPCBIND_RUMP
338 1.12 christos if (sa->sa_family == AF_LOCAL)
339 1.12 christos if (chmod(sun.sun_path, S_IRWXU|S_IRWXG|S_IRWXO) == -1)
340 1.12 christos warn("Cannot chmod `%s'", sun.sun_path);
341 1.23 christos #endif
342 1.1 fvdl
343 1.1 fvdl /* Copy the address */
344 1.1 fvdl taddr.addr.len = taddr.addr.maxlen = addrlen;
345 1.1 fvdl taddr.addr.buf = malloc(addrlen);
346 1.1 fvdl if (taddr.addr.buf == NULL) {
347 1.11 christos warn("Cannot allocate memory for `%s' address",
348 1.1 fvdl nconf->nc_netid);
349 1.1 fvdl if (res != NULL)
350 1.1 fvdl freeaddrinfo(res);
351 1.1 fvdl return 1;
352 1.1 fvdl }
353 1.11 christos (void)memcpy(taddr.addr.buf, sa, addrlen);
354 1.15 dsl #ifdef RPCBIND_DEBUG
355 1.1 fvdl if (debugging) {
356 1.1 fvdl /* for debugging print out our universal address */
357 1.1 fvdl char *uaddr;
358 1.1 fvdl struct netbuf nb;
359 1.1 fvdl
360 1.1 fvdl nb.buf = sa;
361 1.1 fvdl nb.len = nb.maxlen = sa->sa_len;
362 1.1 fvdl uaddr = taddr2uaddr(nconf, &nb);
363 1.23 christos (void)fprintf(stderr, "rpcbind: my address is %s fd=%d\n",
364 1.23 christos uaddr, fd);
365 1.11 christos (void)free(uaddr);
366 1.1 fvdl }
367 1.1 fvdl #endif
368 1.1 fvdl
369 1.1 fvdl if (res != NULL)
370 1.1 fvdl freeaddrinfo(res);
371 1.1 fvdl
372 1.1 fvdl if (nconf->nc_semantics != NC_TPI_CLTS)
373 1.1 fvdl listen(fd, SOMAXCONN);
374 1.3 fvdl
375 1.3 fvdl my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, RPC_MAXDATASIZE,
376 1.3 fvdl RPC_MAXDATASIZE);
377 1.18 plunky if (my_xprt == NULL) {
378 1.11 christos warnx("Could not create service for `%s'", nconf->nc_netid);
379 1.1 fvdl goto error;
380 1.1 fvdl }
381 1.1 fvdl
382 1.1 fvdl #ifdef PORTMAP
383 1.1 fvdl /*
384 1.1 fvdl * Register both the versions for tcp/ip, udp/ip and local.
385 1.1 fvdl */
386 1.1 fvdl if ((strcmp(nconf->nc_protofmly, NC_INET) == 0 &&
387 1.1 fvdl (strcmp(nconf->nc_proto, NC_TCP) == 0 ||
388 1.1 fvdl strcmp(nconf->nc_proto, NC_UDP) == 0)) ||
389 1.1 fvdl strcmp(nconf->nc_netid, "local") == 0) {
390 1.1 fvdl struct pmaplist *pml;
391 1.1 fvdl
392 1.1 fvdl if (!svc_register(my_xprt, PMAPPROG, PMAPVERS,
393 1.5 fvdl pmap_service, 0)) {
394 1.11 christos warn("Could not register on `%s'", nconf->nc_netid);
395 1.1 fvdl goto error;
396 1.1 fvdl }
397 1.8 christos pml = malloc(sizeof (struct pmaplist));
398 1.8 christos if (pml == NULL) {
399 1.11 christos warn("Cannot allocate memory");
400 1.11 christos goto error;
401 1.1 fvdl }
402 1.1 fvdl pml->pml_map.pm_prog = PMAPPROG;
403 1.1 fvdl pml->pml_map.pm_vers = PMAPVERS;
404 1.1 fvdl pml->pml_map.pm_port = PMAPPORT;
405 1.1 fvdl if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
406 1.1 fvdl if (tcptrans[0]) {
407 1.11 christos warnx(
408 1.11 christos "Cannot have more than one TCP transport");
409 1.8 christos free(pml);
410 1.1 fvdl goto error;
411 1.1 fvdl }
412 1.1 fvdl tcptrans = strdup(nconf->nc_netid);
413 1.11 christos if (tcptrans == NULL) {
414 1.11 christos free(pml);
415 1.11 christos warn("Cannot allocate memory");
416 1.11 christos goto error;
417 1.11 christos }
418 1.1 fvdl pml->pml_map.pm_prot = IPPROTO_TCP;
419 1.1 fvdl
420 1.1 fvdl /* Let's snarf the universal address */
421 1.1 fvdl /* "h1.h2.h3.h4.p1.p2" */
422 1.1 fvdl tcp_uaddr = taddr2uaddr(nconf, &taddr.addr);
423 1.1 fvdl } else if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
424 1.1 fvdl if (udptrans[0]) {
425 1.11 christos free(pml);
426 1.11 christos warnx(
427 1.11 christos "Cannot have more than one UDP transport");
428 1.1 fvdl goto error;
429 1.1 fvdl }
430 1.1 fvdl udptrans = strdup(nconf->nc_netid);
431 1.11 christos if (udptrans == NULL) {
432 1.11 christos free(pml);
433 1.11 christos warn("Cannot allocate memory");
434 1.11 christos goto error;
435 1.11 christos }
436 1.1 fvdl pml->pml_map.pm_prot = IPPROTO_UDP;
437 1.1 fvdl
438 1.1 fvdl /* Let's snarf the universal address */
439 1.1 fvdl /* "h1.h2.h3.h4.p1.p2" */
440 1.1 fvdl udp_uaddr = taddr2uaddr(nconf, &taddr.addr);
441 1.1 fvdl }
442 1.1 fvdl pml->pml_next = list_pml;
443 1.1 fvdl list_pml = pml;
444 1.1 fvdl
445 1.1 fvdl /* Add version 3 information */
446 1.8 christos pml = malloc(sizeof (struct pmaplist));
447 1.8 christos if (pml == NULL) {
448 1.11 christos warn("Cannot allocate memory");
449 1.11 christos goto error;
450 1.1 fvdl }
451 1.1 fvdl pml->pml_map = list_pml->pml_map;
452 1.1 fvdl pml->pml_map.pm_vers = RPCBVERS;
453 1.1 fvdl pml->pml_next = list_pml;
454 1.1 fvdl list_pml = pml;
455 1.1 fvdl
456 1.1 fvdl /* Add version 4 information */
457 1.8 christos pml = malloc(sizeof (struct pmaplist));
458 1.8 christos if (pml == NULL) {
459 1.11 christos warn("Cannot allocate memory");
460 1.11 christos goto error;
461 1.1 fvdl }
462 1.1 fvdl pml->pml_map = list_pml->pml_map;
463 1.1 fvdl pml->pml_map.pm_vers = RPCBVERS4;
464 1.1 fvdl pml->pml_next = list_pml;
465 1.1 fvdl list_pml = pml;
466 1.1 fvdl
467 1.1 fvdl /* Also add version 2 stuff to rpcbind list */
468 1.1 fvdl rbllist_add(PMAPPROG, PMAPVERS, nconf, &taddr.addr);
469 1.1 fvdl }
470 1.1 fvdl #endif
471 1.1 fvdl
472 1.1 fvdl /* version 3 registration */
473 1.1 fvdl if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) {
474 1.11 christos warn("Could not register %s version 3", nconf->nc_netid);
475 1.1 fvdl goto error;
476 1.1 fvdl }
477 1.1 fvdl rbllist_add(RPCBPROG, RPCBVERS, nconf, &taddr.addr);
478 1.1 fvdl
479 1.1 fvdl /* version 4 registration */
480 1.1 fvdl if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) {
481 1.11 christos warn("Could not register %s version 4", nconf->nc_netid);
482 1.1 fvdl goto error;
483 1.1 fvdl }
484 1.1 fvdl rbllist_add(RPCBPROG, RPCBVERS4, nconf, &taddr.addr);
485 1.1 fvdl
486 1.1 fvdl /* decide if bound checking works for this transport */
487 1.1 fvdl status = add_bndlist(nconf, &taddr.addr);
488 1.15 dsl #ifdef RPCBIND_DEBUG
489 1.1 fvdl if (debugging) {
490 1.1 fvdl if (status < 0) {
491 1.1 fvdl fprintf(stderr, "Error in finding bind status for %s\n",
492 1.1 fvdl nconf->nc_netid);
493 1.1 fvdl } else if (status == 0) {
494 1.1 fvdl fprintf(stderr, "check binding for %s\n",
495 1.1 fvdl nconf->nc_netid);
496 1.1 fvdl } else if (status > 0) {
497 1.1 fvdl fprintf(stderr, "No check binding for %s\n",
498 1.1 fvdl nconf->nc_netid);
499 1.1 fvdl }
500 1.1 fvdl }
501 1.19 christos #else
502 1.19 christos __USE(status);
503 1.1 fvdl #endif
504 1.1 fvdl /*
505 1.1 fvdl * rmtcall only supported on CLTS transports for now.
506 1.1 fvdl */
507 1.1 fvdl if (nconf->nc_semantics == NC_TPI_CLTS) {
508 1.1 fvdl status = create_rmtcall_fd(nconf);
509 1.1 fvdl
510 1.15 dsl #ifdef RPCBIND_DEBUG
511 1.1 fvdl if (debugging) {
512 1.1 fvdl if (status < 0) {
513 1.1 fvdl fprintf(stderr,
514 1.1 fvdl "Could not create rmtcall fd for %s\n",
515 1.1 fvdl nconf->nc_netid);
516 1.1 fvdl } else {
517 1.1 fvdl fprintf(stderr, "rmtcall fd for %s is %d\n",
518 1.1 fvdl nconf->nc_netid, status);
519 1.1 fvdl }
520 1.1 fvdl }
521 1.1 fvdl #endif
522 1.1 fvdl }
523 1.1 fvdl return (0);
524 1.1 fvdl error:
525 1.23 christos #ifdef RPCBIND_RUMP
526 1.23 christos (void)rump_sys_close(fd);
527 1.23 christos #else
528 1.11 christos (void)close(fd);
529 1.23 christos #endif
530 1.1 fvdl return (1);
531 1.1 fvdl }
532 1.1 fvdl
533 1.1 fvdl static void
534 1.1 fvdl rbllist_add(rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
535 1.1 fvdl struct netbuf *addr)
536 1.1 fvdl {
537 1.1 fvdl rpcblist_ptr rbl;
538 1.1 fvdl
539 1.8 christos rbl = malloc(sizeof(rpcblist));
540 1.8 christos if (rbl == NULL) {
541 1.11 christos warn("Out of memory");
542 1.11 christos return;
543 1.1 fvdl }
544 1.1 fvdl
545 1.1 fvdl rbl->rpcb_map.r_prog = prog;
546 1.1 fvdl rbl->rpcb_map.r_vers = vers;
547 1.1 fvdl rbl->rpcb_map.r_netid = strdup(nconf->nc_netid);
548 1.1 fvdl rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr);
549 1.13 christos rbl->rpcb_map.r_owner = strdup(rpcbind_superuser);
550 1.1 fvdl rbl->rpcb_next = list_rbl; /* Attach to global list */
551 1.1 fvdl list_rbl = rbl;
552 1.1 fvdl }
553 1.1 fvdl
554 1.1 fvdl /*
555 1.1 fvdl * Catch the signal and die
556 1.1 fvdl */
557 1.1 fvdl static void
558 1.1 fvdl terminate(int dummy)
559 1.1 fvdl {
560 1.1 fvdl #ifdef WARMSTART
561 1.1 fvdl syslog(LOG_ERR,
562 1.1 fvdl "rpcbind terminating on signal. Restart with \"rpcbind -w\"");
563 1.1 fvdl write_warmstart(); /* Dump yourself */
564 1.1 fvdl #endif
565 1.23 christos #ifdef RPCBIND_RUMP
566 1.23 christos exit(2);
567 1.23 christos #else
568 1.22 christos exit(EXIT_FAILURE);
569 1.23 christos #endif
570 1.1 fvdl }
571 1.1 fvdl
572 1.1 fvdl void
573 1.1 fvdl rpcbind_abort()
574 1.1 fvdl {
575 1.1 fvdl #ifdef WARMSTART
576 1.1 fvdl write_warmstart(); /* Dump yourself */
577 1.1 fvdl #endif
578 1.1 fvdl abort();
579 1.1 fvdl }
580 1.1 fvdl
581 1.23 christos #ifndef RPCBIND_RUMP
582 1.1 fvdl /* get command line options */
583 1.1 fvdl static void
584 1.1 fvdl parseargs(int argc, char *argv[])
585 1.1 fvdl {
586 1.1 fvdl int c;
587 1.1 fvdl
588 1.1 fvdl while ((c = getopt(argc, argv, "dwailLs")) != -1) {
589 1.1 fvdl switch (c) {
590 1.1 fvdl case 'a':
591 1.1 fvdl doabort = 1; /* when debugging, do an abort on */
592 1.1 fvdl break; /* errors; for rpcbind developers */
593 1.1 fvdl /* only! */
594 1.1 fvdl case 'd':
595 1.1 fvdl debugging = 1;
596 1.1 fvdl break;
597 1.1 fvdl case 'i':
598 1.1 fvdl insecure = 1;
599 1.1 fvdl break;
600 1.1 fvdl case 'L':
601 1.1 fvdl oldstyle_local = 1;
602 1.1 fvdl break;
603 1.1 fvdl case 'l':
604 1.1 fvdl verboselog = 1;
605 1.1 fvdl break;
606 1.1 fvdl case 's':
607 1.1 fvdl runasdaemon = 1;
608 1.1 fvdl break;
609 1.1 fvdl #ifdef WARMSTART
610 1.1 fvdl case 'w':
611 1.1 fvdl warmstart = 1;
612 1.1 fvdl break;
613 1.1 fvdl #endif
614 1.1 fvdl default: /* error */
615 1.1 fvdl fprintf(stderr, "usage: rpcbind [-Idwils]\n");
616 1.22 christos exit(EXIT_FAILURE);
617 1.1 fvdl }
618 1.1 fvdl }
619 1.1 fvdl if (doabort && !debugging) {
620 1.1 fvdl fprintf(stderr,
621 1.1 fvdl "-a (abort) specified without -d (debugging) -- ignored.\n");
622 1.1 fvdl doabort = 0;
623 1.1 fvdl }
624 1.1 fvdl }
625 1.23 christos #endif
626 1.1 fvdl
627 1.1 fvdl void
628 1.1 fvdl reap(int dummy)
629 1.1 fvdl {
630 1.1 fvdl int save_errno = errno;
631 1.1 fvdl
632 1.1 fvdl while (wait3(NULL, WNOHANG, NULL) > 0)
633 1.1 fvdl ;
634 1.1 fvdl errno = save_errno;
635 1.1 fvdl }
636 1.1 fvdl
637 1.1 fvdl void
638 1.1 fvdl toggle_verboselog(int dummy)
639 1.1 fvdl {
640 1.1 fvdl verboselog = !verboselog;
641 1.1 fvdl }
642