nfsd.c revision 1.58 1 /* $NetBSD: nfsd.c,v 1.58 2011/08/30 20:07:31 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.58 2011/08/30 20:07:31 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 <pwd.h>
73 #include <pthread.h>
74 #include <signal.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <syslog.h>
79 #include <unistd.h>
80 #include <netdb.h>
81
82 /* Global defs */
83 #ifdef DEBUG
84 #define syslog(e, s, args...) \
85 do { \
86 fprintf(stderr,(s), ## args); \
87 fprintf(stderr, "\n"); \
88 } while (/*CONSTCOND*/0)
89 static int debug = 1;
90 #else
91 static int debug = 0;
92 #endif
93
94 static void nonfs(int);
95 __dead static void usage(void);
96
97 static void *
98 worker(void *dummy)
99 {
100 struct nfsd_srvargs nsd;
101 int nfssvc_flag;
102
103 pthread_setname_np(pthread_self(), "slave", NULL);
104 nfssvc_flag = NFSSVC_NFSD;
105 memset(&nsd, 0, sizeof(nsd));
106 while (nfssvc(nfssvc_flag, &nsd) < 0) {
107 if (errno != ENEEDAUTH) {
108 syslog(LOG_ERR, "nfssvc: %m");
109 exit(1);
110 }
111 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
112 }
113
114 return NULL;
115 }
116
117 /*
118 * Nfs server daemon mostly just a user context for nfssvc()
119 *
120 * 1 - do file descriptor and signal cleanup
121 * 2 - create the nfsd thread(s)
122 * 3 - create server socket(s)
123 * 4 - register socket with portmap
124 *
125 * For connectionless protocols, just pass the socket into the kernel via
126 * nfssvc().
127 * For connection based sockets, loop doing accepts. When you get a new
128 * socket from accept, pass the msgsock into the kernel via nfssvc().
129 * The arguments are:
130 * -c - support iso cltp clients
131 * -r - reregister with portmapper
132 * -t - support tcp nfs clients
133 * -u - support udp nfs clients
134 * followed by "n" which is the number of nfsd threads to create
135 */
136 int
137 main(int argc, char *argv[])
138 {
139 struct nfsd_args nfsdargs;
140 struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
141 struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
142 struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
143 struct sockaddr_in inetpeer;
144 struct sockaddr_in6 inet6peer;
145 struct pollfd set[4];
146 socklen_t len;
147 int ch, cltpflag, connect_type_cnt, i, maxsock, msgsock, serrno;
148 int nfsdcnt, on = 1, reregister, sock, tcpflag, tcpsock;
149 int tcp6sock, ip6flag;
150 int tp4cnt, tp4flag, tpipcnt, tpipflag, udpflag, ecode, s;
151
152 #define DEFNFSDCNT 4
153 nfsdcnt = DEFNFSDCNT;
154 cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
155 tpipflag = udpflag = ip6flag = 0;
156 nconf_udp = nconf_tcp = nconf_udp6 = nconf_tcp6 = NULL;
157 maxsock = 0;
158 tcpsock = tcp6sock = -1;
159 #define GETOPT "6n:rtu"
160 #define USAGE "[-rtu] [-n num_servers]"
161 while ((ch = getopt(argc, argv, GETOPT)) != -1) {
162 switch (ch) {
163 case '6':
164 ip6flag = 1;
165 s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
166 if (s < 0 && (errno == EPROTONOSUPPORT ||
167 errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
168 ip6flag = 0;
169 else
170 close(s);
171 break;
172 case 'n':
173 nfsdcnt = atoi(optarg);
174 if (nfsdcnt < 1) {
175 warnx("nfsd count %d; reset to %d", nfsdcnt, DEFNFSDCNT);
176 nfsdcnt = DEFNFSDCNT;
177 }
178 break;
179 case 'r':
180 reregister = 1;
181 break;
182 case 't':
183 tcpflag = 1;
184 break;
185 case 'u':
186 udpflag = 1;
187 break;
188 default:
189 case '?':
190 usage();
191 };
192 }
193 argv += optind;
194 argc -= optind;
195
196 /*
197 * XXX
198 * Backward compatibility, trailing number is the count of daemons.
199 */
200 if (argc > 1)
201 usage();
202 if (argc == 1) {
203 nfsdcnt = atoi(argv[0]);
204 if (nfsdcnt < 1) {
205 warnx("nfsd count %d; reset to %d", nfsdcnt, DEFNFSDCNT);
206 nfsdcnt = DEFNFSDCNT;
207 }
208 }
209
210 /*
211 * If none of TCP or UDP are specified, default to UDP only.
212 */
213 if (tcpflag == 0 && udpflag == 0)
214 udpflag = 1;
215
216 if (debug == 0) {
217 daemon(0, 0);
218 (void)signal(SIGHUP, SIG_IGN);
219 (void)signal(SIGINT, SIG_IGN);
220 (void)signal(SIGQUIT, SIG_IGN);
221 (void)signal(SIGSYS, nonfs);
222 }
223
224 if (udpflag) {
225 memset(&hints, 0, sizeof hints);
226 hints.ai_flags = AI_PASSIVE;
227 hints.ai_family = PF_INET;
228 hints.ai_socktype = SOCK_DGRAM;
229 hints.ai_protocol = IPPROTO_UDP;
230
231 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
232 if (ecode != 0) {
233 syslog(LOG_ERR, "getaddrinfo udp: %s",
234 gai_strerror(ecode));
235 exit(1);
236 }
237
238 nconf_udp = getnetconfigent("udp");
239
240 if (nconf_udp == NULL)
241 err(1, "getnetconfigent udp failed");
242
243 nb_udp.buf = ai_udp->ai_addr;
244 nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
245 if (reregister)
246 if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp, &nb_udp))
247 err(1, "rpcb_set udp failed");
248 }
249
250 if (tcpflag) {
251 memset(&hints, 0, sizeof hints);
252 hints.ai_flags = AI_PASSIVE;
253 hints.ai_family = PF_INET;
254 hints.ai_socktype = SOCK_STREAM;
255 hints.ai_protocol = IPPROTO_TCP;
256
257 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
258 if (ecode != 0) {
259 syslog(LOG_ERR, "getaddrinfo tcp: %s",
260 gai_strerror(ecode));
261 exit(1);
262 }
263
264 nconf_tcp = getnetconfigent("tcp");
265
266 if (nconf_tcp == NULL)
267 err(1, "getnetconfigent tcp failed");
268
269 nb_tcp.buf = ai_tcp->ai_addr;
270 nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
271 if (reregister)
272 if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp, &nb_tcp))
273 err(1, "rpcb_set tcp failed");
274 }
275
276 if (udpflag && ip6flag) {
277 memset(&hints, 0, sizeof hints);
278 hints.ai_flags = AI_PASSIVE;
279 hints.ai_family = PF_INET6;
280 hints.ai_socktype = SOCK_DGRAM;
281 hints.ai_protocol = IPPROTO_UDP;
282
283 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
284 if (ecode != 0) {
285 syslog(LOG_ERR, "getaddrinfo udp: %s",
286 gai_strerror(ecode));
287 exit(1);
288 }
289
290 nconf_udp6 = getnetconfigent("udp6");
291
292 if (nconf_udp6 == NULL)
293 err(1, "getnetconfigent udp6 failed");
294
295 nb_udp6.buf = ai_udp6->ai_addr;
296 nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
297 if (reregister)
298 if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp6, &nb_udp6))
299 err(1, "rpcb_set udp6 failed");
300 }
301
302 if (tcpflag && ip6flag) {
303 memset(&hints, 0, sizeof hints);
304 hints.ai_flags = AI_PASSIVE;
305 hints.ai_family = PF_INET6;
306 hints.ai_socktype = SOCK_STREAM;
307 hints.ai_protocol = IPPROTO_TCP;
308
309 ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
310 if (ecode != 0) {
311 syslog(LOG_ERR, "getaddrinfo tcp: %s",
312 gai_strerror(ecode));
313 exit(1);
314 }
315
316 nconf_tcp6 = getnetconfigent("tcp6");
317
318 if (nconf_tcp6 == NULL)
319 err(1, "getnetconfigent tcp6 failed");
320
321 nb_tcp6.buf = ai_tcp6->ai_addr;
322 nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
323 if (reregister)
324 if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp6, &nb_tcp6))
325 err(1, "rpcb_set tcp6 failed");
326 }
327
328 openlog("nfsd", LOG_PID, LOG_DAEMON);
329
330 for (i = 0; i < nfsdcnt; i++) {
331 pthread_t t;
332 int error;
333
334 error = pthread_create(&t, NULL, worker, NULL);
335 if (error) {
336 errno = error;
337 syslog(LOG_ERR, "pthread_create: %m");
338 exit (1);
339 }
340 }
341
342 /* If we are serving udp, set up the socket. */
343 if (udpflag) {
344 if ((sock = socket(ai_udp->ai_family, ai_udp->ai_socktype,
345 ai_udp->ai_protocol)) < 0) {
346 syslog(LOG_ERR, "can't create udp socket");
347 exit(1);
348 }
349 if (bind(sock, ai_udp->ai_addr, ai_udp->ai_addrlen) < 0) {
350 syslog(LOG_ERR, "can't bind udp addr");
351 exit(1);
352 }
353 if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp, &nb_udp) ||
354 !rpcb_set(RPCPROG_NFS, 3, nconf_udp, &nb_udp)) {
355 syslog(LOG_ERR, "can't register with udp portmap");
356 exit(1);
357 }
358 nfsdargs.sock = sock;
359 nfsdargs.name = NULL;
360 nfsdargs.namelen = 0;
361 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
362 syslog(LOG_ERR, "can't add UDP socket");
363 exit(1);
364 }
365 (void)close(sock);
366 }
367
368 if (udpflag &&ip6flag) {
369 if ((sock = socket(ai_udp6->ai_family, ai_udp6->ai_socktype,
370 ai_udp6->ai_protocol)) < 0) {
371 syslog(LOG_ERR, "can't create udp socket");
372 exit(1);
373 }
374 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
375 &on, sizeof on) < 0) {
376 syslog(LOG_ERR, "can't set v6-only binding for udp6 "
377 "socket: %m");
378 exit(1);
379 }
380 if (bind(sock, ai_udp6->ai_addr, ai_udp6->ai_addrlen) < 0) {
381 syslog(LOG_ERR, "can't bind udp addr");
382 exit(1);
383 }
384 if (!rpcb_set(RPCPROG_NFS, 2, nconf_udp6, &nb_udp6) ||
385 !rpcb_set(RPCPROG_NFS, 3, nconf_udp6, &nb_udp6)) {
386 syslog(LOG_ERR, "can't register with udp portmap");
387 exit(1);
388 }
389 nfsdargs.sock = sock;
390 nfsdargs.name = NULL;
391 nfsdargs.namelen = 0;
392 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
393 syslog(LOG_ERR, "can't add UDP6 socket");
394 exit(1);
395 }
396 (void)close(sock);
397 }
398
399 /* Now set up the master server socket waiting for tcp connections. */
400 on = 1;
401 connect_type_cnt = 0;
402 if (tcpflag) {
403 if ((tcpsock = socket(ai_tcp->ai_family, ai_tcp->ai_socktype,
404 ai_tcp->ai_protocol)) < 0) {
405 syslog(LOG_ERR, "can't create tcp socket");
406 exit(1);
407 }
408 if (setsockopt(tcpsock,
409 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
410 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
411 if (bind(tcpsock, ai_tcp->ai_addr, ai_tcp->ai_addrlen) < 0) {
412 syslog(LOG_ERR, "can't bind tcp addr");
413 exit(1);
414 }
415 if (listen(tcpsock, 5) < 0) {
416 syslog(LOG_ERR, "listen failed");
417 exit(1);
418 }
419 if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp, &nb_tcp) ||
420 !rpcb_set(RPCPROG_NFS, 3, nconf_tcp, &nb_tcp)) {
421 syslog(LOG_ERR, "can't register tcp with rpcbind");
422 exit(1);
423 }
424 set[0].fd = tcpsock;
425 set[0].events = POLLIN;
426 connect_type_cnt++;
427 } else
428 set[0].fd = -1;
429
430 if (tcpflag && ip6flag) {
431 if ((tcp6sock = socket(ai_tcp6->ai_family, ai_tcp6->ai_socktype,
432 ai_tcp6->ai_protocol)) < 0) {
433 syslog(LOG_ERR, "can't create tcp socket");
434 exit(1);
435 }
436 if (setsockopt(tcp6sock,
437 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
438 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
439 if (setsockopt(tcp6sock, IPPROTO_IPV6, IPV6_V6ONLY,
440 &on, sizeof on) < 0) {
441 syslog(LOG_ERR, "can't set v6-only binding for tcp6 "
442 "socket: %m");
443 exit(1);
444 }
445 if (bind(tcp6sock, ai_tcp6->ai_addr, ai_tcp6->ai_addrlen) < 0) {
446 syslog(LOG_ERR, "can't bind tcp6 addr");
447 exit(1);
448 }
449 if (listen(tcp6sock, 5) < 0) {
450 syslog(LOG_ERR, "listen failed");
451 exit(1);
452 }
453 if (!rpcb_set(RPCPROG_NFS, 2, nconf_tcp6, &nb_tcp6) ||
454 !rpcb_set(RPCPROG_NFS, 3, nconf_tcp6, &nb_tcp6)) {
455 syslog(LOG_ERR, "can't register tcp6 with rpcbind");
456 exit(1);
457 }
458 set[1].fd = tcp6sock;
459 set[1].events = POLLIN;
460 connect_type_cnt++;
461 } else
462 set[1].fd = -1;
463
464 set[2].fd = -1;
465 set[3].fd = -1;
466
467 if (connect_type_cnt == 0)
468 exit(0);
469
470 pthread_setname_np(pthread_self(), "master", NULL);
471
472 /*
473 * Loop forever accepting connections and passing the sockets
474 * into the kernel for the mounts.
475 */
476 for (;;) {
477 if (poll(set, 4, INFTIM) < 1) {
478 syslog(LOG_ERR, "poll failed: %m");
479 exit(1);
480 }
481
482 if (set[0].revents & POLLIN) {
483 len = sizeof(inetpeer);
484 if ((msgsock = accept(tcpsock,
485 (struct sockaddr *)&inetpeer, &len)) < 0) {
486 serrno = errno;
487 syslog(LOG_ERR, "accept failed: %m");
488 if (serrno == EINTR || serrno == ECONNABORTED)
489 continue;
490 exit(1);
491 }
492 memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
493 if (setsockopt(msgsock, SOL_SOCKET,
494 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
495 syslog(LOG_ERR,
496 "setsockopt SO_KEEPALIVE: %m");
497 nfsdargs.sock = msgsock;
498 nfsdargs.name = (caddr_t)&inetpeer;
499 nfsdargs.namelen = sizeof(inetpeer);
500 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
501 (void)close(msgsock);
502 }
503
504 if (set[1].revents & POLLIN) {
505 len = sizeof(inet6peer);
506 if ((msgsock = accept(tcp6sock,
507 (struct sockaddr *)&inet6peer, &len)) < 0) {
508 serrno = errno;
509 syslog(LOG_ERR, "accept failed: %m");
510 if (serrno == EINTR || serrno == ECONNABORTED)
511 continue;
512 exit(1);
513 }
514 if (setsockopt(msgsock, SOL_SOCKET,
515 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
516 syslog(LOG_ERR,
517 "setsockopt SO_KEEPALIVE: %m");
518 nfsdargs.sock = msgsock;
519 nfsdargs.name = (caddr_t)&inet6peer;
520 nfsdargs.namelen = sizeof(inet6peer);
521 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
522 (void)close(msgsock);
523 }
524
525 #ifdef notyet
526 if (set[2].revents & POLLIN) {
527 len = sizeof(isopeer);
528 if ((msgsock = accept(tp4sock,
529 (struct sockaddr *)&isopeer, &len)) < 0) {
530 serrno = errno;
531 syslog(LOG_ERR, "accept failed: %m");
532 if (serrno == EINTR || serrno == ECONNABORTED)
533 continue;
534 exit(1);
535 }
536 if (setsockopt(msgsock, SOL_SOCKET,
537 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
538 syslog(LOG_ERR,
539 "setsockopt SO_KEEPALIVE: %m");
540 nfsdargs.sock = msgsock;
541 nfsdargs.name = (caddr_t)&isopeer;
542 nfsdargs.namelen = len;
543 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
544 (void)close(msgsock);
545 }
546
547 if (set[3].revents & POLLIN) {
548 len = sizeof(inetpeer);
549 if ((msgsock = accept(tpipsock,
550 (struct sockaddr *)&inetpeer, &len)) < 0) {
551 serrno = errno;
552 syslog(LOG_ERR, "accept failed: %m");
553 if (serrno == EINTR || serrno == ECONNABORTED)
554 continue;
555 exit(1);
556 }
557 if (setsockopt(msgsock, SOL_SOCKET,
558 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
559 syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
560 nfsdargs.sock = msgsock;
561 nfsdargs.name = (caddr_t)&inetpeer;
562 nfsdargs.namelen = len;
563 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
564 (void)close(msgsock);
565 }
566 #endif /* notyet */
567 }
568 }
569
570 static void
571 usage(void)
572 {
573 (void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
574 exit(1);
575 }
576
577 static void
578 nonfs(int signo)
579 {
580 syslog(LOG_ERR, "missing system call: NFS not available.");
581 }
582