xref: /netbsd-src/usr.sbin/nfsd/nfsd.c (revision e6d6e05cb173f30287ab619b21120b27baa66ad6)
1 /*	$NetBSD: nfsd.c,v 1.52 2008/02/27 16:39:17 ad 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\n\
38 	The Regents of the University of California.  All rights reserved.\n");
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.52 2008/02/27 16:39:17 ad 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	MAXNFSDCNT	1024
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 || nfsdcnt > MAXNFSDCNT) {
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 || nfsdcnt > MAXNFSDCNT) {
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, child, 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 				syslog(LOG_ERR, "accept failed: %m");
487 				exit(1);
488 			}
489 			memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
490 			if (setsockopt(msgsock, SOL_SOCKET,
491 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
492 				syslog(LOG_ERR,
493 				    "setsockopt SO_KEEPALIVE: %m");
494 			nfsdargs.sock = msgsock;
495 			nfsdargs.name = (caddr_t)&inetpeer;
496 			nfsdargs.namelen = sizeof(inetpeer);
497 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
498 			(void)close(msgsock);
499 		}
500 
501 		if (set[1].revents & POLLIN) {
502 			len = sizeof(inet6peer);
503 			if ((msgsock = accept(tcp6sock,
504 			    (struct sockaddr *)&inet6peer, &len)) < 0) {
505 				syslog(LOG_ERR, "accept failed: %m");
506 				exit(1);
507 			}
508 			if (setsockopt(msgsock, SOL_SOCKET,
509 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
510 				syslog(LOG_ERR,
511 				    "setsockopt SO_KEEPALIVE: %m");
512 			nfsdargs.sock = msgsock;
513 			nfsdargs.name = (caddr_t)&inet6peer;
514 			nfsdargs.namelen = sizeof(inet6peer);
515 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
516 			(void)close(msgsock);
517 		}
518 
519 #ifdef notyet
520 		if (set[2].revents & POLLIN) {
521 			len = sizeof(isopeer);
522 			if ((msgsock = accept(tp4sock,
523 			    (struct sockaddr *)&isopeer, &len)) < 0) {
524 				syslog(LOG_ERR, "accept failed: %m");
525 				exit(1);
526 			}
527 			if (setsockopt(msgsock, SOL_SOCKET,
528 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
529 				syslog(LOG_ERR,
530 				    "setsockopt SO_KEEPALIVE: %m");
531 			nfsdargs.sock = msgsock;
532 			nfsdargs.name = (caddr_t)&isopeer;
533 			nfsdargs.namelen = len;
534 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
535 			(void)close(msgsock);
536 		}
537 
538 		if (set[3].revents & POLLIN) {
539 			len = sizeof(inetpeer);
540 			if ((msgsock = accept(tpipsock,
541 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
542 				syslog(LOG_ERR, "accept failed: %m");
543 				exit(1);
544 			}
545 			if (setsockopt(msgsock, SOL_SOCKET,
546 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
547 				syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
548 			nfsdargs.sock = msgsock;
549 			nfsdargs.name = (caddr_t)&inetpeer;
550 			nfsdargs.namelen = len;
551 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
552 			(void)close(msgsock);
553 		}
554 #endif /* notyet */
555 	}
556 }
557 
558 void
559 usage()
560 {
561 	(void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
562 	exit(1);
563 }
564 
565 void
566 nonfs(signo)
567 	int signo;
568 {
569 	syslog(LOG_ERR, "missing system call: NFS not available.");
570 }
571