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