nfsd.c revision 1.67 1 /* $NetBSD: nfsd.c,v 1.67 2016/08/22 16:08:51 christos Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)nfsd.c 8.9 (Berkeley) 3/29/95";
44 #else
45 __RCSID("$NetBSD: nfsd.c,v 1.67 2016/08/22 16:08:51 christos Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/ioctl.h>
51 #include <sys/stat.h>
52 #include <sys/wait.h>
53 #include <sys/uio.h>
54 #include <sys/ucred.h>
55 #include <sys/mount.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <poll.h>
59
60 #include <rpc/rpc.h>
61 #include <rpc/pmap_clnt.h>
62 #include <rpc/pmap_prot.h>
63
64 #include <nfs/rpcv2.h>
65 #include <nfs/nfsproto.h>
66 #include <nfs/nfs.h>
67
68 #include <err.h>
69 #include <errno.h>
70 #include <fcntl.h>
71 #include <grp.h>
72 #include <paths.h>
73 #include <pwd.h>
74 #include <pthread.h>
75 #include <signal.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <syslog.h>
80 #include <unistd.h>
81 #include <netdb.h>
82
83 #ifdef NFSD_RUMP
84 #include <rump/rump.h>
85 #include <rump/rump_syscalls.h>
86
87 #define nfssvc(a, b) rump_sys_nfssvc((a), (b))
88 #define close(a) rump_sys_close(a)
89 #define poll(a, b, c) rump_sys_poll((a), (b), (c))
90 #if 0
91 #define socket(a, b, c) rump_sys_socket((a), (b), (c))
92 #define setsockopt(a, b, c, d, e) rump_sys_setsockopt((a), (b), (c), (d), (e))
93 #define bind(a, b, c) rump_sys_bind((a), (b), (c))
94 #define listen(a, b) rump_sys_listen((a), (b))
95 #define accept(a, b, c) rump_sys_accept((a), (b), (c))
96 #endif
97 #define main nfsd_main
98 int nfsd_main(int, char *[]);
99 #endif
100
101 /* Global defs */
102 #if defined(DEBUG) || defined(NFSD_RUMP)
103 static int debug = 1;
104 #else
105 static int debug = 0;
106 #endif
107
108 #define logit(e, s, args...) \
109 do { \
110 if (debug) { \
111 fprintf(stderr,(s), ## args); \
112 fprintf(stderr, "\n"); \
113 } else { \
114 syslog(e, s, ## args); \
115 } \
116 } while (/*CONSTCOND*/0)
117
118 static void nonfs(int);
119 __dead static void usage(void);
120
121 static void *
122 worker(void *dummy)
123 {
124 struct nfsd_srvargs nsd;
125 int nfssvc_flag;
126
127 pthread_setname_np(pthread_self(), "slave", NULL);
128 nfssvc_flag = NFSSVC_NFSD;
129 memset(&nsd, 0, sizeof(nsd));
130 while (nfssvc(nfssvc_flag, &nsd) < 0) {
131 if (errno != ENEEDAUTH) {
132 logit(LOG_ERR, "nfssvc: %s", strerror(errno));
133 exit(1);
134 }
135 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
136 }
137
138 return NULL;
139 }
140
141 struct conf {
142 struct addrinfo *ai;
143 struct netconfig *nc;
144 struct netbuf nb;
145 struct pollfd pfd;
146 };
147
148 #define NFS_UDP4 0
149 #define NFS_TCP4 1
150 #define NFS_UDP6 2
151 #define NFS_TCP6 3
152
153 static int cfg_family[] = { PF_INET, PF_INET, PF_INET6, PF_INET6 };
154 static const char *cfg_netconf[] = { "udp", "tcp", "udp6", "tcp6" };
155 static int cfg_socktype[] = {
156 SOCK_DGRAM, SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM };
157 static int cfg_protocol[] = {
158 IPPROTO_UDP, IPPROTO_TCP, IPPROTO_UDP, IPPROTO_TCP };
159
160 static int
161 tryconf(struct conf *cfg, int t, int reregister)
162 {
163 struct addrinfo hints;
164 int ecode;
165
166 memset(&hints, 0, sizeof hints);
167 hints.ai_flags = AI_PASSIVE;
168 hints.ai_family = cfg_family[t];
169 hints.ai_socktype = cfg_socktype[t];
170 hints.ai_protocol = cfg_protocol[t];
171
172 ecode = getaddrinfo(NULL, "nfs", &hints, &cfg->ai);
173 if (ecode != 0) {
174 logit(LOG_ERR, "getaddrinfo %s: %s", cfg_netconf[t],
175 gai_strerror(ecode));
176 return -1;
177 }
178
179 cfg->nc = getnetconfigent(cfg_netconf[t]);
180
181 if (cfg->nc == NULL) {
182 logit(LOG_ERR, "getnetconfigent %s failed: %s",
183 cfg_netconf[t], strerror(errno));
184 goto out;
185 }
186
187 cfg->nb.buf = cfg->ai->ai_addr;
188 cfg->nb.len = cfg->nb.maxlen = cfg->ai->ai_addrlen;
189 if (reregister)
190 if (!rpcb_set(RPCPROG_NFS, 2, cfg->nc, &cfg->nb)) {
191 logit(LOG_ERR, "rpcb_set %s failed", cfg_netconf[t]);
192 goto out1;
193 }
194 return 0;
195 out1:
196 freenetconfigent(cfg->nc);
197 cfg->nc = NULL;
198 out:
199 freeaddrinfo(cfg->ai);
200 cfg->ai = NULL;
201 return -1;
202 }
203
204 static int
205 setupsock(struct conf *cfg, struct pollfd *set, int p)
206 {
207 int sock;
208 struct nfsd_args nfsdargs;
209 struct addrinfo *ai = cfg->ai;
210 int on = 1;
211
212 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
213
214 if (sock == -1) {
215 logit(LOG_ERR, "can't create %s socket: %s", cfg_netconf[p],
216 strerror(errno));
217 return -1;
218 }
219 if (cfg_family[p] == PF_INET6) {
220 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on,
221 sizeof(on)) == -1) {
222 logit(LOG_ERR, "can't set v6-only binding for %s "
223 "socket: %s", cfg_netconf[p], strerror(errno));
224 goto out;
225 }
226 }
227
228 if (cfg_protocol[p] == IPPROTO_TCP) {
229 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
230 sizeof(on)) == -1) {
231 logit(LOG_ERR, "setsockopt SO_REUSEADDR for %s: %s",
232 cfg_netconf[p], strerror(errno));
233 goto out;
234 }
235 }
236
237 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
238 logit(LOG_ERR, "can't bind %s addr: %s", cfg_netconf[p],
239 strerror(errno));
240 goto out;
241 }
242
243 if (cfg_protocol[p] == IPPROTO_TCP) {
244 if (listen(sock, 5) == -1) {
245 logit(LOG_ERR, "listen failed");
246 goto out;
247 }
248 }
249
250 if (!rpcb_set(RPCPROG_NFS, 2, cfg->nc, &cfg->nb) ||
251 !rpcb_set(RPCPROG_NFS, 3, cfg->nc, &cfg->nb)) {
252 logit(LOG_ERR, "can't register with %s portmap",
253 cfg_netconf[p]);
254 goto out;
255 }
256
257
258 if (cfg_protocol[p] == IPPROTO_TCP)
259 set->fd = sock;
260 else {
261 nfsdargs.sock = sock;
262 nfsdargs.name = NULL;
263 nfsdargs.namelen = 0;
264 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
265 logit(LOG_ERR, "can't add %s socket: %s",
266 cfg_netconf[p], strerror(errno));
267 goto out;
268 }
269 (void)close(sock);
270 }
271 return 0;
272 out:
273 (void)close(sock);
274 return -1;
275 }
276
277 /*
278 * The functions daemon2_fork() and daemon2_detach() below provide
279 * functionality similar to daemon(3) but split into two phases.
280 * daemon2_fork() is called early, before creating resources that
281 * cannot be inherited across a fork, such as threads or kqueues.
282 * When the daemon is ready to provide service, daemon2_detach()
283 * is called to complete the daemonization and signal the parent
284 * process to exit.
285 *
286 * These functions could potentially be moved to a library and
287 * shared by other daemons.
288 *
289 * The return value from daemon2_fork() is a file descriptor to
290 * be passed as the first argument to daemon2_detach().
291 */
292
293 static int
294 daemon2_fork(void)
295 {
296 int i;
297 int fd;
298 int r;
299 pid_t pid;
300 int detach_msg_pipe[2];
301
302 /*
303 * Set up a pipe for signalling the parent, making sure the
304 * write end does not get allocated one of the file
305 * descriptors that may be closed in daemon2_detach(). The
306 * read end does not need such protection.
307 */
308 for (i = 0; i < 3; i++) {
309 r = pipe2(detach_msg_pipe, O_CLOEXEC|O_NOSIGPIPE);
310 if (r < 0)
311 return -1;
312 if (detach_msg_pipe[1] <= STDERR_FILENO &&
313 (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
314 (void)dup2(fd, detach_msg_pipe[0]);
315 (void)dup2(fd, detach_msg_pipe[1]);
316 if (fd > STDERR_FILENO)
317 (void)close(fd);
318 continue;
319 }
320 break;
321 }
322
323 pid = fork();
324 switch (pid) {
325 case -1:
326 return -1;
327 case 0:
328 /* child */
329 (void)close(detach_msg_pipe[0]);
330 (void)write(detach_msg_pipe[1], "", 1);
331 return detach_msg_pipe[1];
332 default:
333 break;
334 }
335
336 /* Parent */
337 (void)close(detach_msg_pipe[1]);
338
339 for (;;) {
340 ssize_t nread;
341 char dummy;
342 nread = read(detach_msg_pipe[0], &dummy, 1);
343 if (nread < 0) {
344 if (errno == EINTR)
345 continue;
346 _exit(1);
347 } else if (nread == 0) {
348 _exit(1);
349 } else { /* nread > 0 */
350 _exit(0);
351 }
352 }
353 }
354
355 static int
356 daemon2_detach(int parentfd, int nochdir, int noclose)
357 {
358 int fd;
359
360 if (setsid() == -1)
361 return -1;
362
363 if (!nochdir)
364 (void)chdir("/");
365
366 if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
367 (void)dup2(fd, STDIN_FILENO);
368 (void)dup2(fd, STDOUT_FILENO);
369 (void)dup2(fd, STDERR_FILENO);
370 if (fd > STDERR_FILENO)
371 (void)close(fd);
372 }
373
374 while (1) {
375 ssize_t r = write(parentfd, "", 1);
376 if (r < 0) {
377 if (errno == EINTR)
378 continue;
379 else if (errno == EPIPE)
380 break;
381 else
382 return -1;
383 } else if (r == 0) {
384 /* Should not happen */
385 return -1;
386 } else {
387 break;
388 }
389 }
390
391 (void)close(parentfd);
392
393 return 0;
394 }
395
396 /*
397 * Nfs server daemon mostly just a user context for nfssvc()
398 *
399 * 1 - do file descriptor and signal cleanup
400 * 2 - create the nfsd thread(s)
401 * 3 - create server socket(s)
402 * 4 - register socket with portmap
403 *
404 * For connectionless protocols, just pass the socket into the kernel via
405 * nfssvc().
406 * For connection based sockets, loop doing accepts. When you get a new
407 * socket from accept, pass the msgsock into the kernel via nfssvc().
408 * The arguments are:
409 * -r - reregister with portmapper
410 * -t - support only tcp nfs clients
411 * -u - support only udp nfs clients
412 * -n num how many threads to create.
413 * -4 - use only ipv4
414 * -6 - use only ipv6
415 */
416 int
417 main(int argc, char *argv[])
418 {
419 struct conf cfg[4];
420 struct pollfd set[__arraycount(cfg)];
421 int ch, connect_type_cnt;
422 size_t i, nfsdcnt;
423 int reregister;
424 int tcpflag, udpflag;
425 int ip6flag, ip4flag;
426 int s, compat;
427 int parent_fd = -1;
428
429 #define DEFNFSDCNT 4
430 nfsdcnt = DEFNFSDCNT;
431 compat = reregister = 0;
432 tcpflag = udpflag = 1;
433 ip6flag = ip4flag = 1;
434 #define GETOPT "46dn:rtu"
435 #define USAGE "[-46drtu] [-n num_servers]"
436 while ((ch = getopt(argc, argv, GETOPT)) != -1) {
437 switch (ch) {
438 case '6':
439 ip6flag = 1;
440 ip4flag = 0;
441 s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
442 if (s < 0 && (errno == EPROTONOSUPPORT ||
443 errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
444 ip6flag = 0;
445 else
446 close(s);
447 break;
448 case '4':
449 ip6flag = 0;
450 ip4flag = 1;
451 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
452 if (s < 0 && (errno == EPROTONOSUPPORT ||
453 errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
454 ip4flag = 0;
455 else
456 close(s);
457 break;
458 case 'd':
459 debug++;
460 break;
461 case 'n':
462 nfsdcnt = atoi(optarg);
463 if (nfsdcnt < 1) {
464 warnx("nfsd count %zu; reset to %d", nfsdcnt,
465 DEFNFSDCNT);
466 nfsdcnt = DEFNFSDCNT;
467 }
468 break;
469 case 'r':
470 reregister = 1;
471 break;
472 case 't':
473 compat |= 2;
474 tcpflag = 1;
475 udpflag = 0;
476 break;
477 case 'u':
478 compat |= 1;
479 tcpflag = 0;
480 udpflag = 1;
481 break;
482 default:
483 case '?':
484 usage();
485 }
486 }
487 argv += optind;
488 argc -= optind;
489
490 if (compat == 3) {
491 warnx("Old -tu options detected; enabling both udp and tcp.");
492 warnx("This is the default behavior now and you can remove");
493 warnx("all options.");
494 tcpflag = udpflag = 1;
495 if (ip6flag == 1 && ip4flag == 0)
496 ip4flag = 1;
497 }
498
499 if (debug == 0) {
500 parent_fd = daemon2_fork();
501 if (parent_fd == -1)
502 logit(LOG_ERR, "daemon2_fork failed");
503 openlog("nfsd", LOG_PID, LOG_DAEMON);
504 }
505
506
507 memset(cfg, 0, sizeof(cfg));
508 for (i = 0; i < __arraycount(cfg); i++) {
509 if (ip4flag == 0 && cfg_family[i] == PF_INET)
510 continue;
511 if (ip6flag == 0 && cfg_family[i] == PF_INET6)
512 continue;
513 if (tcpflag == 0 && cfg_protocol[i] == IPPROTO_TCP)
514 continue;
515 if (udpflag == 0 && cfg_protocol[i] == IPPROTO_UDP)
516 continue;
517 tryconf(&cfg[i], i, reregister);
518 }
519
520 for (i = 0; i < nfsdcnt; i++) {
521 pthread_t t;
522 int error;
523
524 error = pthread_create(&t, NULL, worker, NULL);
525 if (error) {
526 errno = error;
527 logit(LOG_ERR, "pthread_create: %s", strerror(errno));
528 exit(1);
529 }
530 }
531
532 connect_type_cnt = 0;
533 for (i = 0; i < __arraycount(cfg); i++) {
534 set[i].fd = -1;
535 set[i].events = POLLIN;
536 set[i].revents = 0;
537
538 if (cfg[i].nc == NULL)
539 continue;
540
541 setupsock(&cfg[i], &set[i], i);
542 if (set[i].fd != -1)
543 connect_type_cnt++;
544
545 }
546
547 if (connect_type_cnt == 0)
548 exit(0);
549
550 pthread_setname_np(pthread_self(), "master", NULL);
551
552 if (debug == 0) {
553 daemon2_detach(parent_fd, 0, 0);
554 (void)signal(SIGHUP, SIG_IGN);
555 (void)signal(SIGINT, SIG_IGN);
556 (void)signal(SIGQUIT, SIG_IGN);
557 (void)signal(SIGSYS, nonfs);
558 }
559
560 /*
561 * Loop forever accepting connections and passing the sockets
562 * into the kernel for the mounts.
563 */
564 for (;;) {
565 if (poll(set, __arraycount(set), INFTIM) == -1) {
566 logit(LOG_ERR, "poll failed: %s", strerror(errno));
567 exit(1);
568 }
569
570 for (i = 0; i < __arraycount(set); i++) {
571 struct nfsd_args nfsdargs;
572 struct sockaddr_storage ss;
573 socklen_t len;
574 int msgsock;
575 int on = 1;
576
577 if ((set[i].revents & POLLIN) == 0)
578 continue;
579 len = sizeof(ss);
580 if ((msgsock = accept(set[i].fd,
581 (struct sockaddr *)&ss, &len)) == -1) {
582 int serrno = errno;
583 logit(LOG_ERR, "accept failed: %s",
584 strerror(errno));
585 if (serrno == EINTR || serrno == ECONNABORTED)
586 continue;
587 exit(1);
588 }
589 if (setsockopt(msgsock, SOL_SOCKET, SO_KEEPALIVE, &on,
590 sizeof(on)) == -1)
591 logit(LOG_ERR, "setsockopt SO_KEEPALIVE: %s",
592 strerror(errno));
593 nfsdargs.sock = msgsock;
594 nfsdargs.name = (void *)&ss;
595 nfsdargs.namelen = len;
596 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
597 (void)close(msgsock);
598 }
599 }
600 }
601
602 static void
603 usage(void)
604 {
605 (void)fprintf(stderr, "Usage: %s %s\n", getprogname(), USAGE);
606 exit(1);
607 }
608
609 static void
610 nonfs(int signo)
611 {
612 logit(LOG_ERR, "missing system call: NFS not available.");
613 }
614