nfsd.c revision 1.64 1 /* $NetBSD: nfsd.c,v 1.64 2015/09/12 13:50:55 joerg 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.64 2015/09/12 13:50:55 joerg 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 /* Global defs */
84 #ifdef DEBUG
85 #define syslog(e, s, args...) \
86 do { \
87 fprintf(stderr,(s), ## args); \
88 fprintf(stderr, "\n"); \
89 } while (/*CONSTCOND*/0)
90 static int debug = 1;
91 #else
92 static int debug = 0;
93 #endif
94
95 static void nonfs(int);
96 __dead static void usage(void);
97
98 static void *
99 worker(void *dummy)
100 {
101 struct nfsd_srvargs nsd;
102 int nfssvc_flag;
103
104 pthread_setname_np(pthread_self(), "slave", NULL);
105 nfssvc_flag = NFSSVC_NFSD;
106 memset(&nsd, 0, sizeof(nsd));
107 while (nfssvc(nfssvc_flag, &nsd) < 0) {
108 if (errno != ENEEDAUTH) {
109 syslog(LOG_ERR, "nfssvc: %m");
110 exit(1);
111 }
112 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
113 }
114
115 return NULL;
116 }
117
118 struct conf {
119 struct addrinfo *ai;
120 struct netconfig *nc;
121 struct netbuf nb;
122 struct pollfd pfd;
123 };
124
125 #define NFS_UDP4 0
126 #define NFS_TCP4 1
127 #define NFS_UDP6 2
128 #define NFS_TCP6 3
129
130 static int cfg_family[] = { PF_INET, PF_INET, PF_INET6, PF_INET6 };
131 static const char *cfg_netconf[] = { "udp", "tcp", "udp6", "tcp6" };
132 static int cfg_socktype[] = {
133 SOCK_DGRAM, SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM };
134 static int cfg_protocol[] = {
135 IPPROTO_UDP, IPPROTO_TCP, IPPROTO_UDP, IPPROTO_TCP };
136
137 static int
138 tryconf(struct conf *cfg, int t, int reregister)
139 {
140 struct addrinfo hints;
141 int ecode;
142
143 memset(&hints, 0, sizeof hints);
144 hints.ai_flags = AI_PASSIVE;
145 hints.ai_family = cfg_family[t];
146 hints.ai_socktype = cfg_socktype[t];
147 hints.ai_protocol = cfg_protocol[t];
148
149 ecode = getaddrinfo(NULL, "nfs", &hints, &cfg->ai);
150 if (ecode != 0) {
151 syslog(LOG_ERR, "getaddrinfo %s: %s", cfg_netconf[t],
152 gai_strerror(ecode));
153 return -1;
154 }
155
156 cfg->nc = getnetconfigent(cfg_netconf[t]);
157
158 if (cfg->nc == NULL) {
159 syslog(LOG_ERR, "getnetconfigent %s failed: %m",
160 cfg_netconf[t]);
161 goto out;
162 }
163
164 cfg->nb.buf = cfg->ai->ai_addr;
165 cfg->nb.len = cfg->nb.maxlen = cfg->ai->ai_addrlen;
166 if (reregister)
167 if (!rpcb_set(RPCPROG_NFS, 2, cfg->nc, &cfg->nb)) {
168 syslog(LOG_ERR, "rpcb_set %s failed", cfg_netconf[t]);
169 goto out1;
170 }
171 return 0;
172 out1:
173 freenetconfigent(cfg->nc);
174 cfg->nc = NULL;
175 out:
176 freeaddrinfo(cfg->ai);
177 cfg->ai = NULL;
178 return -1;
179 }
180
181 static int
182 setupsock(struct conf *cfg, struct pollfd *set, int p)
183 {
184 int sock;
185 struct nfsd_args nfsdargs;
186 struct addrinfo *ai = cfg->ai;
187 int on = 1;
188
189 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
190
191 if (sock == -1) {
192 syslog(LOG_ERR, "can't create %s socket: %m", cfg_netconf[p]);
193 return -1;
194 }
195 if (cfg_family[p] == PF_INET6) {
196 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on,
197 sizeof(on)) == -1) {
198 syslog(LOG_ERR, "can't set v6-only binding for %s "
199 "socket: %m", cfg_netconf[p]);
200 goto out;
201 }
202 }
203
204 if (cfg_protocol[p] == IPPROTO_TCP) {
205 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
206 sizeof(on)) == -1) {
207 syslog(LOG_ERR, "setsockopt SO_REUSEADDR for %s: %m",
208 cfg_netconf[p]);
209 goto out;
210 }
211 }
212
213 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
214 syslog(LOG_ERR, "can't bind %s addr: %m", cfg_netconf[p]);
215 goto out;
216 }
217
218 if (cfg_protocol[p] == IPPROTO_TCP) {
219 if (listen(sock, 5) == -1) {
220 syslog(LOG_ERR, "listen failed");
221 goto out;
222 }
223 }
224
225 if (!rpcb_set(RPCPROG_NFS, 2, cfg->nc, &cfg->nb) ||
226 !rpcb_set(RPCPROG_NFS, 3, cfg->nc, &cfg->nb)) {
227 syslog(LOG_ERR, "can't register with %s portmap",
228 cfg_netconf[p]);
229 goto out;
230 }
231
232
233 if (cfg_protocol[p] == IPPROTO_TCP)
234 set->fd = sock;
235 else {
236 nfsdargs.sock = sock;
237 nfsdargs.name = NULL;
238 nfsdargs.namelen = 0;
239 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
240 syslog(LOG_ERR, "can't add %s socket", cfg_netconf[p]);
241 goto out;
242 }
243 (void)close(sock);
244 }
245 return 0;
246 out:
247 (void)close(sock);
248 return -1;
249 }
250
251 /*
252 * The functions daemon2_fork() and daemon2_detach() below provide
253 * functionality similar to daemon(3) but split into two phases.
254 * daemon2_fork() is called early, before creating resources that
255 * cannot be inherited across a fork, such as threads or kqueues.
256 * When the daemon is ready to provide service, daemon2_detach()
257 * is called to complete the daemonization and signal the parent
258 * process to exit.
259 *
260 * These functions could potentially be moved to a library and
261 * shared by other daemons.
262 *
263 * The return value from daemon2_fork() is a file descriptor to
264 * be passed as the first argument to daemon2_detach().
265 */
266
267 static int
268 daemon2_fork(void)
269 {
270 int i;
271 int fd;
272 int r;
273 pid_t pid;
274 int detach_msg_pipe[2];
275
276 /*
277 * Set up a pipe for signalling the parent, making sure the
278 * write end does not get allocated one of the file
279 * descriptors that may be closed in daemon2_detach(). The
280 * read end does not need such protection.
281 */
282 for (i = 0; i < 3; i++) {
283 r = pipe2(detach_msg_pipe, O_CLOEXEC|O_NOSIGPIPE);
284 if (r < 0)
285 return -1;
286 if (detach_msg_pipe[1] <= STDERR_FILENO &&
287 (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
288 (void)dup2(fd, detach_msg_pipe[0]);
289 (void)dup2(fd, detach_msg_pipe[1]);
290 if (fd > STDERR_FILENO)
291 (void)close(fd);
292 continue;
293 }
294 break;
295 }
296
297 pid = fork();
298 switch (pid) {
299 case -1:
300 return -1;
301 case 0:
302 /* child */
303 (void)close(detach_msg_pipe[0]);
304 return detach_msg_pipe[1];
305 default:
306 break;
307 }
308
309 /* Parent */
310 (void)close(detach_msg_pipe[1]);
311
312 for (;;) {
313 ssize_t nread;
314 char dummy;
315 nread = read(detach_msg_pipe[0], &dummy, 1);
316 if (nread < 0) {
317 if (errno == EINTR)
318 continue;
319 _exit(1);
320 } else if (nread == 0) {
321 _exit(1);
322 } else { /* nread > 0 */
323 _exit(0);
324 }
325 }
326 }
327
328 static int
329 daemon2_detach(int parentfd, int nochdir, int noclose)
330 {
331 int fd;
332
333 if (setsid() == -1)
334 return -1;
335
336 if (!nochdir)
337 (void)chdir("/");
338
339 if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
340 (void)dup2(fd, STDIN_FILENO);
341 (void)dup2(fd, STDOUT_FILENO);
342 (void)dup2(fd, STDERR_FILENO);
343 if (fd > STDERR_FILENO)
344 (void)close(fd);
345 }
346
347 while (1) {
348 ssize_t r = write(parentfd, "", 1);
349 if (r < 0) {
350 if (errno == EINTR)
351 continue;
352 else if (errno == EPIPE)
353 break;
354 else
355 return -1;
356 } else if (r == 0) {
357 /* Should not happen */
358 return -1;
359 } else {
360 break;
361 }
362 }
363
364 (void)close(parentfd);
365
366 return 0;
367 }
368
369 /*
370 * Nfs server daemon mostly just a user context for nfssvc()
371 *
372 * 1 - do file descriptor and signal cleanup
373 * 2 - create the nfsd thread(s)
374 * 3 - create server socket(s)
375 * 4 - register socket with portmap
376 *
377 * For connectionless protocols, just pass the socket into the kernel via
378 * nfssvc().
379 * For connection based sockets, loop doing accepts. When you get a new
380 * socket from accept, pass the msgsock into the kernel via nfssvc().
381 * The arguments are:
382 * -r - reregister with portmapper
383 * -t - support only tcp nfs clients
384 * -u - support only udp nfs clients
385 * -n num how many threads to create.
386 * -4 - use only ipv4
387 * -6 - use only ipv6
388 */
389 int
390 main(int argc, char *argv[])
391 {
392 struct conf cfg[4];
393 struct pollfd set[__arraycount(cfg)];
394 int ch, connect_type_cnt;
395 size_t i, nfsdcnt;
396 int reregister;
397 int tcpflag, udpflag;
398 int ip6flag, ip4flag;
399 int s, compat;
400 int parent_fd = -1;
401
402 #define DEFNFSDCNT 4
403 nfsdcnt = DEFNFSDCNT;
404 compat = reregister = 0;
405 tcpflag = udpflag = 1;
406 ip6flag = ip4flag = 1;
407 #define GETOPT "46n:rtu"
408 #define USAGE "[-46rtu] [-n num_servers]"
409 while ((ch = getopt(argc, argv, GETOPT)) != -1) {
410 switch (ch) {
411 case '6':
412 ip6flag = 1;
413 ip4flag = 0;
414 s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
415 if (s < 0 && (errno == EPROTONOSUPPORT ||
416 errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
417 ip6flag = 0;
418 else
419 close(s);
420 break;
421 case '4':
422 ip6flag = 0;
423 ip4flag = 1;
424 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
425 if (s < 0 && (errno == EPROTONOSUPPORT ||
426 errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
427 ip4flag = 0;
428 else
429 close(s);
430 break;
431 case 'n':
432 nfsdcnt = atoi(optarg);
433 if (nfsdcnt < 1) {
434 warnx("nfsd count %zu; reset to %d", nfsdcnt,
435 DEFNFSDCNT);
436 nfsdcnt = DEFNFSDCNT;
437 }
438 break;
439 case 'r':
440 reregister = 1;
441 break;
442 case 't':
443 compat |= 2;
444 tcpflag = 1;
445 udpflag = 0;
446 break;
447 case 'u':
448 compat |= 1;
449 tcpflag = 0;
450 udpflag = 1;
451 break;
452 default:
453 case '?':
454 usage();
455 }
456 }
457 argv += optind;
458 argc -= optind;
459
460 if (compat == 3) {
461 warnx("Old -tu options detected; enabling both udp and tcp.");
462 warnx("This is the default behavior now and you can remove");
463 warnx("all options.");
464 tcpflag = udpflag = 1;
465 if (ip6flag == 1 && ip4flag == 0)
466 ip4flag = 1;
467 }
468
469 if (debug == 0) {
470 parent_fd = daemon2_fork();
471 }
472
473 openlog("nfsd", LOG_PID, LOG_DAEMON);
474
475 memset(cfg, 0, sizeof(cfg));
476 for (i = 0; i < __arraycount(cfg); i++) {
477 if (ip4flag == 0 && cfg_family[i] == PF_INET)
478 continue;
479 if (ip6flag == 0 && cfg_family[i] == PF_INET6)
480 continue;
481 if (tcpflag == 0 && cfg_protocol[i] == IPPROTO_TCP)
482 continue;
483 if (udpflag == 0 && cfg_protocol[i] == IPPROTO_UDP)
484 continue;
485 tryconf(&cfg[i], i, reregister);
486 }
487
488 for (i = 0; i < nfsdcnt; i++) {
489 pthread_t t;
490 int error;
491
492 error = pthread_create(&t, NULL, worker, NULL);
493 if (error) {
494 errno = error;
495 syslog(LOG_ERR, "pthread_create: %m");
496 exit(1);
497 }
498 }
499
500 connect_type_cnt = 0;
501 for (i = 0; i < __arraycount(cfg); i++) {
502 set[i].fd = -1;
503 set[i].events = POLLIN;
504 set[i].revents = 0;
505
506 if (cfg[i].nc == NULL)
507 continue;
508
509 setupsock(&cfg[i], &set[i], i);
510 if (set[i].fd != -1)
511 connect_type_cnt++;
512
513 }
514
515 if (connect_type_cnt == 0)
516 exit(0);
517
518 pthread_setname_np(pthread_self(), "master", NULL);
519
520 if (debug == 0) {
521 daemon2_detach(parent_fd, 0, 0);
522 (void)signal(SIGHUP, SIG_IGN);
523 (void)signal(SIGINT, SIG_IGN);
524 (void)signal(SIGQUIT, SIG_IGN);
525 (void)signal(SIGSYS, nonfs);
526 }
527
528 /*
529 * Loop forever accepting connections and passing the sockets
530 * into the kernel for the mounts.
531 */
532 for (;;) {
533 if (poll(set, __arraycount(set), INFTIM) == -1) {
534 syslog(LOG_ERR, "poll failed: %m");
535 exit(1);
536 }
537
538 for (i = 0; i < __arraycount(set); i++) {
539 struct nfsd_args nfsdargs;
540 struct sockaddr_storage ss;
541 socklen_t len;
542 int msgsock;
543 int on = 1;
544
545 if ((set[i].revents & POLLIN) == 0)
546 continue;
547 len = sizeof(ss);
548 if ((msgsock = accept(set[i].fd,
549 (struct sockaddr *)&ss, &len)) == -1) {
550 int serrno = errno;
551 syslog(LOG_ERR, "accept failed: %m");
552 if (serrno == EINTR || serrno == ECONNABORTED)
553 continue;
554 exit(1);
555 }
556 if (setsockopt(msgsock, SOL_SOCKET, SO_KEEPALIVE, &on,
557 sizeof(on)) == -1)
558 syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
559 nfsdargs.sock = msgsock;
560 nfsdargs.name = (void *)&ss;
561 nfsdargs.namelen = len;
562 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
563 (void)close(msgsock);
564 }
565 }
566 }
567
568 static void
569 usage(void)
570 {
571 (void)fprintf(stderr, "Usage: %s %s\n", getprogname(), USAGE);
572 exit(1);
573 }
574
575 static void
576 nonfs(int signo)
577 {
578 syslog(LOG_ERR, "missing system call: NFS not available.");
579 }
580