xref: /netbsd-src/usr.sbin/nfsd/nfsd.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: nfsd.c,v 1.61 2012/08/15 00:16:06 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.61 2012/08/15 00:16:06 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 struct conf {
118 	struct addrinfo *ai;
119 	struct netconfig *nc;
120 	struct netbuf nb;
121 	struct pollfd pfd;
122 };
123 
124 #define NFS_UDP4	0
125 #define NFS_TCP4	1
126 #define NFS_UDP6	2
127 #define NFS_TCP6	3
128 
129 static int cfg_family[] = { PF_INET, PF_INET, PF_INET6, PF_INET6 };
130 static const char *cfg_netconf[] = { "udp", "tcp", "udp6", "tcp6" };
131 static int cfg_socktype[] = {
132     SOCK_DGRAM, SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM };
133 static int cfg_protocol[] = {
134     IPPROTO_UDP, IPPROTO_TCP, IPPROTO_UDP, IPPROTO_TCP };
135 
136 static int
137 tryconf(struct conf *cfg, int t, int reregister)
138 {
139 	struct addrinfo hints;
140 	int ecode;
141 
142 	memset(cfg, 0, sizeof(*cfg));
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  * Nfs server daemon mostly just a user context for nfssvc()
253  *
254  * 1 - do file descriptor and signal cleanup
255  * 2 - create the nfsd thread(s)
256  * 3 - create server socket(s)
257  * 4 - register socket with portmap
258  *
259  * For connectionless protocols, just pass the socket into the kernel via
260  * nfssvc().
261  * For connection based sockets, loop doing accepts. When you get a new
262  * socket from accept, pass the msgsock into the kernel via nfssvc().
263  * The arguments are:
264  *	-r - reregister with portmapper
265  *	-t - support only tcp nfs clients
266  *	-u - support only udp nfs clients
267  *	-n num how many threads to create.
268  *	-4 - use only ipv4
269  *	-6 - use only ipv6
270  */
271 int
272 main(int argc, char *argv[])
273 {
274 	struct conf cfg[4];
275 	struct pollfd set[__arraycount(cfg)];
276 	int ch, connect_type_cnt;
277 	size_t i, nfsdcnt;
278 	int reregister;
279 	int tcpflag, udpflag;
280 	int ip6flag, ip4flag;
281 	int s, compat;
282 
283 #define	DEFNFSDCNT	 4
284 	nfsdcnt = DEFNFSDCNT;
285 	compat = reregister = 0;
286 	tcpflag = udpflag = 1;
287 	ip6flag = ip4flag = 1;
288 #define	GETOPT	"46n:rtu"
289 #define	USAGE	"[-46rtu] [-n num_servers]"
290 	while ((ch = getopt(argc, argv, GETOPT)) != -1) {
291 		switch (ch) {
292 		case '6':
293 			ip6flag = 1;
294 			ip4flag = 0;
295 			s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
296 			if (s < 0 && (errno == EPROTONOSUPPORT ||
297 			    errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
298 				ip6flag = 0;
299 			else
300 				close(s);
301 			break;
302 		case '4':
303 			ip6flag = 0;
304 			ip4flag = 1;
305 			s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
306 			if (s < 0 && (errno == EPROTONOSUPPORT ||
307 			    errno == EPFNOSUPPORT || errno == EAFNOSUPPORT))
308 				ip4flag = 0;
309 			else
310 				close(s);
311 			break;
312 		case 'n':
313 			nfsdcnt = atoi(optarg);
314 			if (nfsdcnt < 1) {
315 				warnx("nfsd count %zu; reset to %d", nfsdcnt,
316 				    DEFNFSDCNT);
317 				nfsdcnt = DEFNFSDCNT;
318 			}
319 			break;
320 		case 'r':
321 			reregister = 1;
322 			break;
323 		case 't':
324 			compat |= 2;
325 			tcpflag = 1;
326 			udpflag = 0;
327 			break;
328 		case 'u':
329 			compat |= 1;
330 			tcpflag = 0;
331 			udpflag = 1;
332 			break;
333 		default:
334 		case '?':
335 			usage();
336 		}
337 	}
338 	argv += optind;
339 	argc -= optind;
340 
341 	if (compat == 3) {
342 		warnx("Old -tu options detected; enabling both udp and tcp.");
343 		warnx("This is the default behavior now and you can remove");
344 		warnx("all options.");
345 		tcpflag = udpflag = 1;
346 		if (ip6flag == 1 && ip4flag == 0)
347 			ip4flag = 1;
348 	}
349 
350 	if (debug == 0) {
351 		daemon(0, 0);
352 		(void)signal(SIGHUP, SIG_IGN);
353 		(void)signal(SIGINT, SIG_IGN);
354 		(void)signal(SIGQUIT, SIG_IGN);
355 		(void)signal(SIGSYS, nonfs);
356 	}
357 
358 	openlog("nfsd", LOG_PID, LOG_DAEMON);
359 
360 	for (i = 0; i < __arraycount(cfg); i++) {
361 		if (ip4flag == 0 && cfg_family[i] == PF_INET)
362 			continue;
363 		if (ip6flag == 0 && cfg_family[i] == PF_INET6)
364 			continue;
365 		if (tcpflag == 0 && cfg_protocol[i] == IPPROTO_TCP)
366 			continue;
367 		if (udpflag == 0 && cfg_protocol[i] == IPPROTO_UDP)
368 			continue;
369 		tryconf(&cfg[i], i, reregister);
370 	}
371 
372 	for (i = 0; i < nfsdcnt; i++) {
373 		pthread_t t;
374 		int error;
375 
376 		error = pthread_create(&t, NULL, worker, NULL);
377 		if (error) {
378 			errno = error;
379 			syslog(LOG_ERR, "pthread_create: %m");
380 			exit(1);
381 		}
382 	}
383 
384 	connect_type_cnt = 0;
385 	for (i = 0; i < __arraycount(cfg); i++) {
386 		set[i].fd = -1;
387 		set[i].events = POLLIN;
388 		set[i].revents = 0;
389 
390 		if (cfg[i].nc == NULL)
391 			continue;
392 
393 		setupsock(&cfg[i], &set[i], i);
394 		if (set[i].fd != -1)
395 			connect_type_cnt++;
396 
397 	}
398 
399 	if (connect_type_cnt == 0)
400 		exit(0);
401 
402 	pthread_setname_np(pthread_self(), "master", NULL);
403 
404 	/*
405 	 * Loop forever accepting connections and passing the sockets
406 	 * into the kernel for the mounts.
407 	 */
408 	for (;;) {
409 		if (poll(set, __arraycount(set), INFTIM) == -1) {
410 			syslog(LOG_ERR, "poll failed: %m");
411 			exit(1);
412 		}
413 
414 		for (i = 0; i < __arraycount(set); i++) {
415 			struct nfsd_args nfsdargs;
416 			struct sockaddr_storage ss;
417 			socklen_t len;
418 			int msgsock;
419 			int on = 1;
420 
421 			if ((set[i].revents & POLLIN) == 0)
422 				continue;
423 			len = sizeof(ss);
424 			if ((msgsock = accept(set[i].fd,
425 			    (struct sockaddr *)&ss, &len)) == -1) {
426 				int serrno = errno;
427 				syslog(LOG_ERR, "accept failed: %m");
428 				if (serrno == EINTR || serrno == ECONNABORTED)
429 					continue;
430 				exit(1);
431 			}
432 			if (setsockopt(msgsock, SOL_SOCKET, SO_KEEPALIVE, &on,
433 			    sizeof(on)) == -1)
434 				syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
435 			nfsdargs.sock = msgsock;
436 			nfsdargs.name = (void *)&ss;
437 			nfsdargs.namelen = len;
438 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
439 			(void)close(msgsock);
440 		}
441 	}
442 }
443 
444 static void
445 usage(void)
446 {
447 	(void)fprintf(stderr, "Usage: %s %s\n", getprogname(), USAGE);
448 	exit(1);
449 }
450 
451 static void
452 nonfs(int signo)
453 {
454 	syslog(LOG_ERR, "missing system call: NFS not available.");
455 }
456