xref: /freebsd/usr.sbin/nfsd/nfsd.c (revision f321956d9806bf0c171b9097a25b294c1498c9de)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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/param.h>
36 #include <sys/syslog.h>
37 #include <sys/wait.h>
38 #include <sys/mount.h>
39 #include <sys/fcntl.h>
40 #include <sys/linker.h>
41 #include <sys/module.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/sysctl.h>
45 #include <sys/ucred.h>
46 
47 #include <rpc/rpc.h>
48 #include <rpc/pmap_clnt.h>
49 #include <rpcsvc/nfs_prot.h>
50 
51 #include <netdb.h>
52 #include <arpa/inet.h>
53 #include <nfs/nfssvc.h>
54 
55 #include <fs/nfs/nfsproto.h>
56 #include <fs/nfs/nfskpiport.h>
57 #include <fs/nfs/nfs.h>
58 
59 #include <err.h>
60 #include <errno.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #include <sysexits.h>
67 
68 #include <getopt.h>
69 
70 static int	debug = 0;
71 static int	nofork = 0;
72 
73 #define	NFSD_STABLERESTART	"/var/db/nfs-stablerestart"
74 #define	NFSD_STABLEBACKUP	"/var/db/nfs-stablerestart.bak"
75 #define	MAXNFSDCNT	256
76 #define	DEFNFSDCNT	 4
77 #define	NFS_VER2	 2
78 #define NFS_VER3	 3
79 #define NFS_VER4	 4
80 static pid_t children[MAXNFSDCNT]; /* PIDs of children */
81 static pid_t masterpid;		   /* PID of master/parent */
82 static int nfsdcnt;		/* number of children */
83 static int nfsdcnt_set;
84 static int minthreads;
85 static int maxthreads;
86 static int nfssvc_nfsd;		/* Set to correct NFSSVC_xxx flag */
87 static int stablefd = -1;	/* Fd for the stable restart file */
88 static int backupfd;		/* Fd for the backup stable restart file */
89 static const char *getopt_shortopts;
90 static const char *getopt_usage;
91 static int nfs_minvers = NFS_VER2;
92 
93 static int minthreads_set;
94 static int maxthreads_set;
95 
96 static struct option longopts[] = {
97 	{ "debug", no_argument, &debug, 1 },
98 	{ "minthreads", required_argument, &minthreads_set, 1 },
99 	{ "maxthreads", required_argument, &maxthreads_set, 1 },
100 	{ "pnfs", required_argument, NULL, 'p' },
101 	{ "mirror", required_argument, NULL, 'm' },
102 	{ NULL, 0, NULL, 0}
103 };
104 
105 static void	cleanup(int);
106 static void	child_cleanup(int);
107 static void	killchildren(void);
108 static void	nfsd_exit(int);
109 static void	nonfs(int);
110 static void	reapchild(int);
111 static int	setbindhost(struct addrinfo **ia, const char *bindhost,
112 		    struct addrinfo hints);
113 static void	start_server(int, struct nfsd_nfsd_args *, const char *vhost);
114 static void	unregistration(void);
115 static void	usage(void);
116 static void	open_stable(int *, int *);
117 static void	copy_stable(int, int);
118 static void	backup_stable(int);
119 static void	set_nfsdcnt(int);
120 static void	parse_dsserver(const char *, struct nfsd_nfsd_args *);
121 
122 /*
123  * Nfs server daemon mostly just a user context for nfssvc()
124  *
125  * 1 - do file descriptor and signal cleanup
126  * 2 - fork the nfsd(s)
127  * 3 - create server socket(s)
128  * 4 - register socket with rpcbind
129  *
130  * For connectionless protocols, just pass the socket into the kernel via.
131  * nfssvc().
132  * For connection based sockets, loop doing accepts. When you get a new
133  * socket from accept, pass the msgsock into the kernel via. nfssvc().
134  * The arguments are:
135  *	-r - reregister with rpcbind
136  *	-d - unregister with rpcbind
137  *	-t - support tcp nfs clients
138  *	-u - support udp nfs clients
139  *	-e - forces it to run a server that supports nfsv4
140  *	-p - enable a pNFS service
141  *	-m - set the mirroring level for a pNFS service
142  * followed by "n" which is the number of nfsds' to fork off
143  */
144 int
main(int argc,char ** argv)145 main(int argc, char **argv)
146 {
147 	struct nfsd_addsock_args addsockargs;
148 	struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
149 	struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
150 	struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
151 	struct sockaddr_storage peer;
152 	fd_set ready, sockbits;
153 	int ch, connect_type_cnt, i, maxsock, msgsock;
154 	socklen_t len;
155 	int on = 1, unregister, reregister, sock;
156 	int tcp6sock, ip6flag, tcpflag, tcpsock;
157 	int udpflag, ecode, error, s;
158 	int bindhostc, bindanyflag, rpcbreg, rpcbregcnt;
159 	int nfssvc_addsock;
160 	int jailed, longindex = 0;
161 	size_t jailed_size, nfs_minvers_size;
162 	const char *lopt;
163 	char **bindhost = NULL;
164 	pid_t pid;
165 	struct nfsd_nfsd_args nfsdargs;
166 	const char *vhostname = NULL;
167 
168 	nfsdargs.mirrorcnt = 1;
169 	nfsdargs.addr = NULL;
170 	nfsdargs.addrlen = 0;
171 	nfsdcnt = DEFNFSDCNT;
172 	unregister = reregister = tcpflag = maxsock = 0;
173 	bindanyflag = udpflag = connect_type_cnt = bindhostc = 0;
174 	getopt_shortopts = "ah:n:rdtuep:m:V:N";
175 	getopt_usage =
176 	    "usage:\n"
177 	    "  nfsd [-ardtueN] [-h bindip]\n"
178 	    "       [-n numservers] [--minthreads #] [--maxthreads #]\n"
179 	    "       [-p/--pnfs dsserver0:/dsserver0-mounted-on-dir,...,"
180 	    "dsserverN:/dsserverN-mounted-on-dir] [-m mirrorlevel]\n"
181 	    "       [-V virtual_hostname]\n";
182 	while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts,
183 		    &longindex)) != -1)
184 		switch (ch) {
185 		case 'V':
186 			if (strlen(optarg) <= MAXHOSTNAMELEN)
187 				vhostname = optarg;
188 			else
189 				warnx("Virtual host name (%s) is too long",
190 				    optarg);
191 			break;
192 		case 'a':
193 			bindanyflag = 1;
194 			break;
195 		case 'n':
196 			set_nfsdcnt(atoi(optarg));
197 			break;
198 		case 'h':
199 			bindhostc++;
200 			bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
201 			if (bindhost == NULL)
202 				errx(1, "Out of memory");
203 			bindhost[bindhostc-1] = strdup(optarg);
204 			if (bindhost[bindhostc-1] == NULL)
205 				errx(1, "Out of memory");
206 			break;
207 		case 'r':
208 			reregister = 1;
209 			break;
210 		case 'd':
211 			unregister = 1;
212 			break;
213 		case 't':
214 			tcpflag = 1;
215 			break;
216 		case 'u':
217 			udpflag = 1;
218 			break;
219 		case 'e':
220 			/* now a no-op, since this is the default */
221 			break;
222 		case 'p':
223 			/* Parse out the DS server host names and mount pts. */
224 			parse_dsserver(optarg, &nfsdargs);
225 			break;
226 		case 'm':
227 			/* Set the mirror level for a pNFS service. */
228 			i = atoi(optarg);
229 			if (i < 2 || i > NFSDEV_MAXMIRRORS)
230 				errx(1, "Mirror level out of range 2<-->%d",
231 				    NFSDEV_MAXMIRRORS);
232 			nfsdargs.mirrorcnt = i;
233 			break;
234 		case 'N':
235 			nofork = 1;
236 			break;
237 		case 0:
238 			lopt = longopts[longindex].name;
239 			if (!strcmp(lopt, "minthreads")) {
240 				minthreads = atoi(optarg);
241 			} else if (!strcmp(lopt, "maxthreads")) {
242 				maxthreads = atoi(optarg);
243 			}
244 			break;
245 		default:
246 		case '?':
247 			usage();
248 		}
249 	if (!tcpflag && !udpflag)
250 		udpflag = 1;
251 	argv += optind;
252 	argc -= optind;
253 	if (minthreads_set && maxthreads_set && minthreads > maxthreads)
254 		errx(EX_USAGE,
255 		    "error: minthreads(%d) can't be greater than "
256 		    "maxthreads(%d)", minthreads, maxthreads);
257 
258 	/*
259 	 * XXX
260 	 * Backward compatibility, trailing number is the count of daemons.
261 	 */
262 	if (argc > 1)
263 		usage();
264 	if (argc == 1)
265 		set_nfsdcnt(atoi(argv[0]));
266 
267 	/*
268 	 * Unless the "-o" option was specified, try and run "nfsd".
269 	 * If "-o" was specified, try and run "nfsserver".
270 	 */
271 	if (modfind("nfsd") < 0) {
272 		/* Not present in kernel, try loading it */
273 		if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
274 			errx(1, "NFS server is not available");
275 	}
276 
277 	ip6flag = 1;
278 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
279 	if (s == -1) {
280 		if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
281 			err(1, "socket");
282 		ip6flag = 0;
283 	} else if (getnetconfigent("udp6") == NULL ||
284 		getnetconfigent("tcp6") == NULL) {
285 		ip6flag = 0;
286 	}
287 	if (s != -1)
288 		close(s);
289 
290 	if (bindhostc == 0 || bindanyflag) {
291 		bindhostc++;
292 		bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
293 		if (bindhost == NULL)
294 			errx(1, "Out of memory");
295 		bindhost[bindhostc-1] = strdup("*");
296 		if (bindhost[bindhostc-1] == NULL)
297 			errx(1, "Out of memory");
298 	}
299 
300 	if (unregister) {
301 		/*
302 		 * Unregister before setting nfs_minvers, in case the
303 		 * value of vfs.nfsd.server_min_nfsvers has changed
304 		 * since registering with rpcbind.
305 		 */
306 		unregistration();
307 		exit (0);
308 	}
309 
310 	nfs_minvers_size = sizeof(nfs_minvers);
311 	error = sysctlbyname("vfs.nfsd.server_min_nfsvers", &nfs_minvers,
312 	    &nfs_minvers_size, NULL, 0);
313 	if (error != 0 || nfs_minvers < NFS_VER2 || nfs_minvers > NFS_VER4) {
314 		warnx("sysctlbyname(vfs.nfsd.server_min_nfsvers) failed,"
315 		    " defaulting to NFSv2");
316 		nfs_minvers = NFS_VER2;
317 	}
318 
319 	if (reregister) {
320 		if (udpflag) {
321 			memset(&hints, 0, sizeof hints);
322 			hints.ai_flags = AI_PASSIVE;
323 			hints.ai_family = AF_INET;
324 			hints.ai_socktype = SOCK_DGRAM;
325 			hints.ai_protocol = IPPROTO_UDP;
326 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
327 			if (ecode != 0)
328 				err(1, "getaddrinfo udp: %s", gai_strerror(ecode));
329 			nconf_udp = getnetconfigent("udp");
330 			if (nconf_udp == NULL)
331 				err(1, "getnetconfigent udp failed");
332 			nb_udp.buf = ai_udp->ai_addr;
333 			nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
334 			if (nfs_minvers == NFS_VER2)
335 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
336 				    &nb_udp))
337 					err(1, "rpcb_set udp failed");
338 			if (nfs_minvers <= NFS_VER3)
339 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
340 				    &nb_udp))
341 					err(1, "rpcb_set udp failed");
342 			freeaddrinfo(ai_udp);
343 		}
344 		if (udpflag && ip6flag) {
345 			memset(&hints, 0, sizeof hints);
346 			hints.ai_flags = AI_PASSIVE;
347 			hints.ai_family = AF_INET6;
348 			hints.ai_socktype = SOCK_DGRAM;
349 			hints.ai_protocol = IPPROTO_UDP;
350 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
351 			if (ecode != 0)
352 				err(1, "getaddrinfo udp6: %s", gai_strerror(ecode));
353 			nconf_udp6 = getnetconfigent("udp6");
354 			if (nconf_udp6 == NULL)
355 				err(1, "getnetconfigent udp6 failed");
356 			nb_udp6.buf = ai_udp6->ai_addr;
357 			nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
358 			if (nfs_minvers == NFS_VER2)
359 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
360 				    &nb_udp6))
361 					err(1, "rpcb_set udp6 failed");
362 			if (nfs_minvers <= NFS_VER3)
363 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
364 				    &nb_udp6))
365 					err(1, "rpcb_set udp6 failed");
366 			freeaddrinfo(ai_udp6);
367 		}
368 		if (tcpflag) {
369 			memset(&hints, 0, sizeof hints);
370 			hints.ai_flags = AI_PASSIVE;
371 			hints.ai_family = AF_INET;
372 			hints.ai_socktype = SOCK_STREAM;
373 			hints.ai_protocol = IPPROTO_TCP;
374 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp);
375 			if (ecode != 0)
376 				err(1, "getaddrinfo tcp: %s", gai_strerror(ecode));
377 			nconf_tcp = getnetconfigent("tcp");
378 			if (nconf_tcp == NULL)
379 				err(1, "getnetconfigent tcp failed");
380 			nb_tcp.buf = ai_tcp->ai_addr;
381 			nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
382 			if (nfs_minvers == NFS_VER2)
383 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
384 				    &nb_tcp))
385 					err(1, "rpcb_set tcp failed");
386 			if (nfs_minvers <= NFS_VER3)
387 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
388 				    &nb_tcp))
389 					err(1, "rpcb_set tcp failed");
390 			freeaddrinfo(ai_tcp);
391 		}
392 		if (tcpflag && ip6flag) {
393 			memset(&hints, 0, sizeof hints);
394 			hints.ai_flags = AI_PASSIVE;
395 			hints.ai_family = AF_INET6;
396 			hints.ai_socktype = SOCK_STREAM;
397 			hints.ai_protocol = IPPROTO_TCP;
398 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
399 			if (ecode != 0)
400 				err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode));
401 			nconf_tcp6 = getnetconfigent("tcp6");
402 			if (nconf_tcp6 == NULL)
403 				err(1, "getnetconfigent tcp6 failed");
404 			nb_tcp6.buf = ai_tcp6->ai_addr;
405 			nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
406 			if (nfs_minvers == NFS_VER2)
407 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
408 				    &nb_tcp6))
409 					err(1, "rpcb_set tcp6 failed");
410 			if (nfs_minvers <= NFS_VER3)
411 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
412 				   &nb_tcp6))
413 					err(1, "rpcb_set tcp6 failed");
414 			freeaddrinfo(ai_tcp6);
415 		}
416 		exit (0);
417 	}
418 	if (debug == 0 && nofork == 0) {
419 		daemon(0, 0);
420 		(void)signal(SIGHUP, SIG_IGN);
421 		(void)signal(SIGINT, SIG_IGN);
422 		/*
423 		 * nfsd sits in the kernel most of the time.  It needs
424 		 * to ignore SIGTERM/SIGQUIT in order to stay alive as long
425 		 * as possible during a shutdown, otherwise loopback
426 		 * mounts will not be able to unmount.
427 		 */
428 		(void)signal(SIGTERM, SIG_IGN);
429 		(void)signal(SIGQUIT, SIG_IGN);
430 	}
431 	(void)signal(SIGSYS, nonfs);
432 	(void)signal(SIGCHLD, reapchild);
433 	(void)signal(SIGUSR2, backup_stable);
434 
435 	openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
436 
437 	/*
438 	 * For V4, we open the stablerestart file and call nfssvc()
439 	 * to get it loaded. This is done before the daemons do the
440 	 * regular nfssvc() call to service NFS requests.
441 	 * (This way the file remains open until the last nfsd is killed
442 	 *  off.)
443 	 * It and the backup copy will be created as empty files
444 	 * the first time this nfsd is started and should never be
445 	 * deleted/replaced if at all possible. It should live on a
446 	 * local, non-volatile storage device that does not do hardware
447 	 * level write-back caching. (See SCSI doc for more information
448 	 * on how to prevent write-back caching on SCSI disks.)
449 	 */
450 	open_stable(&stablefd, &backupfd);
451 	if (stablefd < 0) {
452 		syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART);
453 		exit(1);
454 	}
455 	/* This system call will fail for old kernels, but that's ok. */
456 	nfssvc(NFSSVC_BACKUPSTABLE, NULL);
457 	if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) {
458 		if (errno == EPERM) {
459 			jailed = 0;
460 			jailed_size = sizeof(jailed);
461 			sysctlbyname("security.jail.jailed", &jailed,
462 			    &jailed_size, NULL, 0);
463 			if (jailed != 0)
464 				syslog(LOG_ERR, "nfssvc stablerestart failed: "
465 				    "allow.nfsd might not be configured");
466 			else
467 				syslog(LOG_ERR, "nfssvc stablerestart failed");
468 		} else if (errno == ENXIO)
469 			syslog(LOG_ERR, "nfssvc stablerestart failed: is nfsd "
470 			    "already running?");
471 		else
472 			syslog(LOG_ERR, "Can't read stable storage file: %m\n");
473 		exit(1);
474 	}
475 	nfssvc_addsock = NFSSVC_NFSDADDSOCK;
476 	nfssvc_nfsd = NFSSVC_NFSDNFSD | NFSSVC_NEWSTRUCT;
477 
478 	if (tcpflag) {
479 		/*
480 		 * For TCP mode, we fork once to start the first
481 		 * kernel nfsd thread. The kernel will add more
482 		 * threads as needed.
483 		 */
484 		masterpid = getpid();
485 		pid = fork();
486 		if (pid == -1) {
487 			syslog(LOG_ERR, "fork: %m");
488 			nfsd_exit(1);
489 		}
490 		if (pid) {
491 			children[0] = pid;
492 		} else {
493 			(void)signal(SIGUSR1, child_cleanup);
494 			setproctitle("server");
495 			start_server(0, &nfsdargs, vhostname);
496 		}
497 	}
498 
499 	(void)signal(SIGUSR1, cleanup);
500 	FD_ZERO(&sockbits);
501 
502 	rpcbregcnt = 0;
503 	/* Set up the socket for udp and rpcb register it. */
504 	if (udpflag) {
505 		rpcbreg = 0;
506 		for (i = 0; i < bindhostc; i++) {
507 			memset(&hints, 0, sizeof hints);
508 			hints.ai_flags = AI_PASSIVE;
509 			hints.ai_family = AF_INET;
510 			hints.ai_socktype = SOCK_DGRAM;
511 			hints.ai_protocol = IPPROTO_UDP;
512 			if (setbindhost(&ai_udp, bindhost[i], hints) == 0) {
513 				rpcbreg = 1;
514 				rpcbregcnt++;
515 				if ((sock = socket(ai_udp->ai_family,
516 				    ai_udp->ai_socktype,
517 				    ai_udp->ai_protocol)) < 0) {
518 					syslog(LOG_ERR,
519 					    "can't create udp socket");
520 					nfsd_exit(1);
521 				}
522 				if (bind(sock, ai_udp->ai_addr,
523 				    ai_udp->ai_addrlen) < 0) {
524 					syslog(LOG_ERR,
525 					    "can't bind udp addr %s: %m",
526 					    bindhost[i]);
527 					nfsd_exit(1);
528 				}
529 				freeaddrinfo(ai_udp);
530 				addsockargs.sock = sock;
531 				addsockargs.name = NULL;
532 				addsockargs.namelen = 0;
533 				if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
534 					syslog(LOG_ERR, "can't Add UDP socket");
535 					nfsd_exit(1);
536 				}
537 				(void)close(sock);
538 			}
539 		}
540 		if (rpcbreg == 1) {
541 			memset(&hints, 0, sizeof hints);
542 			hints.ai_flags = AI_PASSIVE;
543 			hints.ai_family = AF_INET;
544 			hints.ai_socktype = SOCK_DGRAM;
545 			hints.ai_protocol = IPPROTO_UDP;
546 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp);
547 			if (ecode != 0) {
548 				syslog(LOG_ERR, "getaddrinfo udp: %s",
549 				   gai_strerror(ecode));
550 				nfsd_exit(1);
551 			}
552 			nconf_udp = getnetconfigent("udp");
553 			if (nconf_udp == NULL) {
554 				syslog(LOG_ERR, "getnetconfigent udp failed");
555 				nfsd_exit(1);
556 			}
557 			nb_udp.buf = ai_udp->ai_addr;
558 			nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen;
559 			if (nfs_minvers == NFS_VER2)
560 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp,
561 				    &nb_udp)) {
562 					syslog(LOG_ERR, "rpcb_set udp failed");
563 					nfsd_exit(1);
564 				}
565 			if (nfs_minvers <= NFS_VER3)
566 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp,
567 				    &nb_udp)) {
568 					syslog(LOG_ERR, "rpcb_set udp failed");
569 					nfsd_exit(1);
570 				}
571 			freeaddrinfo(ai_udp);
572 		}
573 	}
574 
575 	/* Set up the socket for udp6 and rpcb register it. */
576 	if (udpflag && ip6flag) {
577 		rpcbreg = 0;
578 		for (i = 0; i < bindhostc; i++) {
579 			memset(&hints, 0, sizeof hints);
580 			hints.ai_flags = AI_PASSIVE;
581 			hints.ai_family = AF_INET6;
582 			hints.ai_socktype = SOCK_DGRAM;
583 			hints.ai_protocol = IPPROTO_UDP;
584 			if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) {
585 				rpcbreg = 1;
586 				rpcbregcnt++;
587 				if ((sock = socket(ai_udp6->ai_family,
588 				    ai_udp6->ai_socktype,
589 				    ai_udp6->ai_protocol)) < 0) {
590 					syslog(LOG_ERR,
591 						"can't create udp6 socket");
592 					nfsd_exit(1);
593 				}
594 				if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
595 				    &on, sizeof on) < 0) {
596 					syslog(LOG_ERR,
597 					    "can't set v6-only binding for "
598 					    "udp6 socket: %m");
599 					nfsd_exit(1);
600 				}
601 				if (bind(sock, ai_udp6->ai_addr,
602 				    ai_udp6->ai_addrlen) < 0) {
603 					syslog(LOG_ERR,
604 					    "can't bind udp6 addr %s: %m",
605 					    bindhost[i]);
606 					nfsd_exit(1);
607 				}
608 				freeaddrinfo(ai_udp6);
609 				addsockargs.sock = sock;
610 				addsockargs.name = NULL;
611 				addsockargs.namelen = 0;
612 				if (nfssvc(nfssvc_addsock, &addsockargs) < 0) {
613 					syslog(LOG_ERR,
614 					    "can't add UDP6 socket");
615 					nfsd_exit(1);
616 				}
617 				(void)close(sock);
618 			}
619 		}
620 		if (rpcbreg == 1) {
621 			memset(&hints, 0, sizeof hints);
622 			hints.ai_flags = AI_PASSIVE;
623 			hints.ai_family = AF_INET6;
624 			hints.ai_socktype = SOCK_DGRAM;
625 			hints.ai_protocol = IPPROTO_UDP;
626 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6);
627 			if (ecode != 0) {
628 				syslog(LOG_ERR, "getaddrinfo udp6: %s",
629 				   gai_strerror(ecode));
630 				nfsd_exit(1);
631 			}
632 			nconf_udp6 = getnetconfigent("udp6");
633 			if (nconf_udp6 == NULL) {
634 				syslog(LOG_ERR, "getnetconfigent udp6 failed");
635 				nfsd_exit(1);
636 			}
637 			nb_udp6.buf = ai_udp6->ai_addr;
638 			nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen;
639 			if (nfs_minvers == NFS_VER2)
640 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6,
641 				    &nb_udp6)) {
642 					syslog(LOG_ERR,
643 					    "rpcb_set udp6 failed");
644 					nfsd_exit(1);
645 				}
646 			if (nfs_minvers <= NFS_VER3)
647 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6,
648 				    &nb_udp6)) {
649 					syslog(LOG_ERR,
650 					    "rpcb_set udp6 failed");
651 					nfsd_exit(1);
652 				}
653 			freeaddrinfo(ai_udp6);
654 		}
655 	}
656 
657 	/* Set up the socket for tcp and rpcb register it. */
658 	if (tcpflag) {
659 		rpcbreg = 0;
660 		for (i = 0; i < bindhostc; i++) {
661 			memset(&hints, 0, sizeof hints);
662 			hints.ai_flags = AI_PASSIVE;
663 			hints.ai_family = AF_INET;
664 			hints.ai_socktype = SOCK_STREAM;
665 			hints.ai_protocol = IPPROTO_TCP;
666 			if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) {
667 				rpcbreg = 1;
668 				rpcbregcnt++;
669 				if ((tcpsock = socket(AF_INET, SOCK_STREAM,
670 				    0)) < 0) {
671 					syslog(LOG_ERR,
672 					    "can't create tcp socket");
673 					nfsd_exit(1);
674 				}
675 				if (setsockopt(tcpsock, SOL_SOCKET,
676 				    SO_REUSEADDR,
677 				    (char *)&on, sizeof(on)) < 0)
678 					syslog(LOG_ERR,
679 					     "setsockopt SO_REUSEADDR: %m");
680 				if (bind(tcpsock, ai_tcp->ai_addr,
681 				    ai_tcp->ai_addrlen) < 0) {
682 					syslog(LOG_ERR,
683 					    "can't bind tcp addr %s: %m",
684 					    bindhost[i]);
685 					nfsd_exit(1);
686 				}
687 				if (listen(tcpsock, -1) < 0) {
688 					syslog(LOG_ERR, "listen failed");
689 					nfsd_exit(1);
690 				}
691 				freeaddrinfo(ai_tcp);
692 				FD_SET(tcpsock, &sockbits);
693 				maxsock = tcpsock;
694 				connect_type_cnt++;
695 			}
696 		}
697 		if (rpcbreg == 1) {
698 			memset(&hints, 0, sizeof hints);
699 			hints.ai_flags = AI_PASSIVE;
700 			hints.ai_family = AF_INET;
701 			hints.ai_socktype = SOCK_STREAM;
702 			hints.ai_protocol = IPPROTO_TCP;
703 			ecode = getaddrinfo(NULL, "nfs", &hints,
704 			     &ai_tcp);
705 			if (ecode != 0) {
706 				syslog(LOG_ERR, "getaddrinfo tcp: %s",
707 				   gai_strerror(ecode));
708 				nfsd_exit(1);
709 			}
710 			nconf_tcp = getnetconfigent("tcp");
711 			if (nconf_tcp == NULL) {
712 				syslog(LOG_ERR, "getnetconfigent tcp failed");
713 				nfsd_exit(1);
714 			}
715 			nb_tcp.buf = ai_tcp->ai_addr;
716 			nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen;
717 			if (nfs_minvers == NFS_VER2)
718 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp,
719 				    &nb_tcp)) {
720 					syslog(LOG_ERR, "rpcb_set tcp failed");
721 					nfsd_exit(1);
722 				}
723 			if (nfs_minvers <= NFS_VER3)
724 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp,
725 				    &nb_tcp)) {
726 					syslog(LOG_ERR, "rpcb_set tcp failed");
727 					nfsd_exit(1);
728 				}
729 			freeaddrinfo(ai_tcp);
730 		}
731 	}
732 
733 	/* Set up the socket for tcp6 and rpcb register it. */
734 	if (tcpflag && ip6flag) {
735 		rpcbreg = 0;
736 		for (i = 0; i < bindhostc; i++) {
737 			memset(&hints, 0, sizeof hints);
738 			hints.ai_flags = AI_PASSIVE;
739 			hints.ai_family = AF_INET6;
740 			hints.ai_socktype = SOCK_STREAM;
741 			hints.ai_protocol = IPPROTO_TCP;
742 			if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) {
743 				rpcbreg = 1;
744 				rpcbregcnt++;
745 				if ((tcp6sock = socket(ai_tcp6->ai_family,
746 				    ai_tcp6->ai_socktype,
747 				    ai_tcp6->ai_protocol)) < 0) {
748 					syslog(LOG_ERR,
749 					    "can't create tcp6 socket");
750 					nfsd_exit(1);
751 				}
752 				if (setsockopt(tcp6sock, SOL_SOCKET,
753 				    SO_REUSEADDR,
754 				    (char *)&on, sizeof(on)) < 0)
755 					syslog(LOG_ERR,
756 					    "setsockopt SO_REUSEADDR: %m");
757 				if (setsockopt(tcp6sock, IPPROTO_IPV6,
758 				    IPV6_V6ONLY, &on, sizeof on) < 0) {
759 					syslog(LOG_ERR,
760 					"can't set v6-only binding for tcp6 "
761 					    "socket: %m");
762 					nfsd_exit(1);
763 				}
764 				if (bind(tcp6sock, ai_tcp6->ai_addr,
765 				    ai_tcp6->ai_addrlen) < 0) {
766 					syslog(LOG_ERR,
767 					    "can't bind tcp6 addr %s: %m",
768 					    bindhost[i]);
769 					nfsd_exit(1);
770 				}
771 				if (listen(tcp6sock, -1) < 0) {
772 					syslog(LOG_ERR, "listen failed");
773 					nfsd_exit(1);
774 				}
775 				freeaddrinfo(ai_tcp6);
776 				FD_SET(tcp6sock, &sockbits);
777 				if (maxsock < tcp6sock)
778 					maxsock = tcp6sock;
779 				connect_type_cnt++;
780 			}
781 		}
782 		if (rpcbreg == 1) {
783 			memset(&hints, 0, sizeof hints);
784 			hints.ai_flags = AI_PASSIVE;
785 			hints.ai_family = AF_INET6;
786 			hints.ai_socktype = SOCK_STREAM;
787 			hints.ai_protocol = IPPROTO_TCP;
788 			ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6);
789 			if (ecode != 0) {
790 				syslog(LOG_ERR, "getaddrinfo tcp6: %s",
791 				   gai_strerror(ecode));
792 				nfsd_exit(1);
793 			}
794 			nconf_tcp6 = getnetconfigent("tcp6");
795 			if (nconf_tcp6 == NULL) {
796 				syslog(LOG_ERR, "getnetconfigent tcp6 failed");
797 				nfsd_exit(1);
798 			}
799 			nb_tcp6.buf = ai_tcp6->ai_addr;
800 			nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen;
801 			if (nfs_minvers == NFS_VER2)
802 				if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6,
803 				    &nb_tcp6)) {
804 					syslog(LOG_ERR, "rpcb_set tcp6 failed");
805 					nfsd_exit(1);
806 				}
807 			if (nfs_minvers <= NFS_VER3)
808 				if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6,
809 				    &nb_tcp6)) {
810 					syslog(LOG_ERR, "rpcb_set tcp6 failed");
811 					nfsd_exit(1);
812 				}
813 			freeaddrinfo(ai_tcp6);
814 		}
815 	}
816 
817 	if (rpcbregcnt == 0) {
818 		syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m");
819 		nfsd_exit(1);
820 	}
821 
822 	if (tcpflag && connect_type_cnt == 0) {
823 		syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m");
824 		nfsd_exit(1);
825 	}
826 
827 	setproctitle("master");
828 	/*
829 	 * We always want a master to have a clean way to shut nfsd down
830 	 * (with unregistration): if the master is killed, it unregisters and
831 	 * kills all children. If we run for UDP only (and so do not have to
832 	 * loop waiting for accept), we instead make the parent
833 	 * a "server" too. start_server will not return.
834 	 */
835 	if (!tcpflag)
836 		start_server(1, &nfsdargs, vhostname);
837 
838 	/*
839 	 * Loop forever accepting connections and passing the sockets
840 	 * into the kernel for the mounts.
841 	 */
842 	for (;;) {
843 		ready = sockbits;
844 		if (connect_type_cnt > 1) {
845 			if (select(maxsock + 1,
846 			    &ready, NULL, NULL, NULL) < 1) {
847 				error = errno;
848 				if (error == EINTR)
849 					continue;
850 				syslog(LOG_ERR, "select failed: %m");
851 				nfsd_exit(1);
852 			}
853 		}
854 		for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
855 			if (FD_ISSET(tcpsock, &ready)) {
856 				len = sizeof(peer);
857 				if ((msgsock = accept(tcpsock,
858 				    (struct sockaddr *)&peer, &len)) < 0) {
859 					error = errno;
860 					syslog(LOG_ERR, "accept failed: %m");
861 					if (error == ECONNABORTED ||
862 					    error == EINTR)
863 						continue;
864 					nfsd_exit(1);
865 				}
866 				if (setsockopt(msgsock, SOL_SOCKET,
867 				    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
868 					syslog(LOG_ERR,
869 					    "setsockopt SO_KEEPALIVE: %m");
870 				addsockargs.sock = msgsock;
871 				addsockargs.name = (caddr_t)&peer;
872 				addsockargs.namelen = len;
873 				nfssvc(nfssvc_addsock, &addsockargs);
874 				(void)close(msgsock);
875 			}
876 		}
877 	}
878 }
879 
880 static int
setbindhost(struct addrinfo ** ai,const char * bindhost,struct addrinfo hints)881 setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints)
882 {
883 	int ecode;
884 	u_int32_t host_addr[4];  /* IPv4 or IPv6 */
885 	const char *hostptr;
886 
887 	if (bindhost == NULL || strcmp("*", bindhost) == 0)
888 		hostptr = NULL;
889 	else
890 		hostptr = bindhost;
891 
892 	if (hostptr != NULL) {
893 		switch (hints.ai_family) {
894 		case AF_INET:
895 			if (inet_pton(AF_INET, hostptr, host_addr) == 1) {
896 				hints.ai_flags = AI_NUMERICHOST;
897 			} else {
898 				if (inet_pton(AF_INET6, hostptr,
899 				    host_addr) == 1)
900 					return (1);
901 			}
902 			break;
903 		case AF_INET6:
904 			if (inet_pton(AF_INET6, hostptr, host_addr) == 1) {
905 				hints.ai_flags = AI_NUMERICHOST;
906 			} else {
907 				if (inet_pton(AF_INET, hostptr,
908 				    host_addr) == 1)
909 					return (1);
910 			}
911 			break;
912 		default:
913 			break;
914 		}
915 	}
916 
917 	ecode = getaddrinfo(hostptr, "nfs", &hints, ai);
918 	if (ecode != 0) {
919 		syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost,
920 		    gai_strerror(ecode));
921 		return (1);
922 	}
923 	return (0);
924 }
925 
926 static void
set_nfsdcnt(int proposed)927 set_nfsdcnt(int proposed)
928 {
929 
930 	if (proposed < 1) {
931 		warnx("nfsd count too low %d; reset to %d", proposed,
932 		    DEFNFSDCNT);
933 		nfsdcnt = DEFNFSDCNT;
934 	} else if (proposed > MAXNFSDCNT) {
935 		warnx("nfsd count too high %d; truncated to %d", proposed,
936 		    MAXNFSDCNT);
937 		nfsdcnt = MAXNFSDCNT;
938 	} else
939 		nfsdcnt = proposed;
940 	nfsdcnt_set = 1;
941 }
942 
943 static void
usage(void)944 usage(void)
945 {
946 	(void)fprintf(stderr, "%s", getopt_usage);
947 	exit(1);
948 }
949 
950 static void
nonfs(__unused int signo)951 nonfs(__unused int signo)
952 {
953 	syslog(LOG_ERR, "missing system call: NFS not available");
954 }
955 
956 static void
reapchild(__unused int signo)957 reapchild(__unused int signo)
958 {
959 	pid_t pid;
960 	int i;
961 
962 	while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
963 		for (i = 0; i < nfsdcnt; i++)
964 			if (pid == children[i])
965 				children[i] = -1;
966 	}
967 }
968 
969 static void
unregistration(void)970 unregistration(void)
971 {
972 	if ((nfs_minvers == NFS_VER2 && !rpcb_unset(NFS_PROGRAM, 2, NULL)) ||
973 	    (nfs_minvers <= NFS_VER3 && !rpcb_unset(NFS_PROGRAM, 3, NULL)))
974 		syslog(LOG_ERR, "rpcb_unset failed");
975 }
976 
977 static void
killchildren(void)978 killchildren(void)
979 {
980 	int i;
981 
982 	for (i = 0; i < nfsdcnt; i++) {
983 		if (children[i] > 0)
984 			kill(children[i], SIGKILL);
985 	}
986 }
987 
988 /*
989  * Cleanup master after SIGUSR1.
990  */
991 static void
cleanup(__unused int signo)992 cleanup(__unused int signo)
993 {
994 	nfsd_exit(0);
995 }
996 
997 /*
998  * Cleanup child after SIGUSR1.
999  */
1000 static void
child_cleanup(__unused int signo)1001 child_cleanup(__unused int signo)
1002 {
1003 	exit(0);
1004 }
1005 
1006 static void
nfsd_exit(int status)1007 nfsd_exit(int status)
1008 {
1009 	killchildren();
1010 	unregistration();
1011 	exit(status);
1012 }
1013 
1014 static int
get_tuned_nfsdcount(void)1015 get_tuned_nfsdcount(void)
1016 {
1017 	int ncpu, error, tuned_nfsdcnt;
1018 	size_t ncpu_size;
1019 
1020 	ncpu_size = sizeof(ncpu);
1021 	error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
1022 	if (error) {
1023 		warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers",
1024 		    DEFNFSDCNT);
1025 		tuned_nfsdcnt = DEFNFSDCNT;
1026 	} else {
1027 		tuned_nfsdcnt = ncpu * 8;
1028 	}
1029 	return tuned_nfsdcnt;
1030 }
1031 
1032 static void
start_server(int master,struct nfsd_nfsd_args * nfsdargp,const char * vhost)1033 start_server(int master, struct nfsd_nfsd_args *nfsdargp, const char *vhost)
1034 {
1035 	char principal[MAXHOSTNAMELEN + 5];
1036 	int status, error;
1037 	char hostname[MAXHOSTNAMELEN + 1], *cp;
1038 	struct addrinfo *aip, hints;
1039 
1040 	status = 0;
1041 	if (vhost == NULL)
1042 		gethostname(hostname, sizeof (hostname));
1043 	else
1044 		strlcpy(hostname, vhost, sizeof (hostname));
1045 	snprintf(principal, sizeof (principal), "nfs@%s", hostname);
1046 	if ((cp = strchr(hostname, '.')) == NULL ||
1047 	    *(cp + 1) == '\0') {
1048 		/* If not fully qualified, try getaddrinfo() */
1049 		memset((void *)&hints, 0, sizeof (hints));
1050 		hints.ai_flags = AI_CANONNAME;
1051 		error = getaddrinfo(hostname, NULL, &hints, &aip);
1052 		if (error == 0) {
1053 			if (aip->ai_canonname != NULL &&
1054 			    (cp = strchr(aip->ai_canonname, '.')) !=
1055 			    NULL && *(cp + 1) != '\0')
1056 				snprintf(principal, sizeof (principal),
1057 				    "nfs@%s", aip->ai_canonname);
1058 			freeaddrinfo(aip);
1059 		}
1060 	}
1061 	nfsdargp->principal = principal;
1062 
1063 	if (nfsdcnt_set)
1064 		nfsdargp->minthreads = nfsdargp->maxthreads = nfsdcnt;
1065 	else {
1066 		nfsdargp->minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount();
1067 		nfsdargp->maxthreads = maxthreads_set ? maxthreads : nfsdargp->minthreads;
1068 		if (nfsdargp->maxthreads < nfsdargp->minthreads)
1069 			nfsdargp->maxthreads = nfsdargp->minthreads;
1070 	}
1071 	error = nfssvc(nfssvc_nfsd, nfsdargp);
1072 	if (error < 0 && errno == EAUTH) {
1073 		/*
1074 		 * This indicates that it could not register the
1075 		 * rpcsec_gss credentials, usually because the
1076 		 * gssd daemon isn't running.
1077 		 * (only the experimental server with nfsv4)
1078 		 */
1079 		syslog(LOG_ERR, "No gssd, using AUTH_SYS only");
1080 		principal[0] = '\0';
1081 		error = nfssvc(nfssvc_nfsd, nfsdargp);
1082 	}
1083 	if (error < 0) {
1084 		if (errno == ENXIO) {
1085 			syslog(LOG_ERR, "Bad -p option, cannot run");
1086 			if (masterpid != 0 && master == 0)
1087 				kill(masterpid, SIGUSR1);
1088 		} else
1089 			syslog(LOG_ERR, "nfssvc: %m");
1090 		status = 1;
1091 	}
1092 	if (master)
1093 		nfsd_exit(status);
1094 	else
1095 		exit(status);
1096 }
1097 
1098 /*
1099  * Open the stable restart file and return the file descriptor for it.
1100  */
1101 static void
open_stable(int * stable_fdp,int * backup_fdp)1102 open_stable(int *stable_fdp, int *backup_fdp)
1103 {
1104 	int stable_fd, backup_fd = -1, ret;
1105 	struct stat st, backup_st;
1106 
1107 	/* Open and stat the stable restart file. */
1108 	stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0);
1109 	if (stable_fd < 0)
1110 		stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600);
1111 	if (stable_fd >= 0) {
1112 		ret = fstat(stable_fd, &st);
1113 		if (ret < 0) {
1114 			close(stable_fd);
1115 			stable_fd = -1;
1116 		}
1117 	}
1118 
1119 	/* Open and stat the backup stable restart file. */
1120 	if (stable_fd >= 0) {
1121 		backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0);
1122 		if (backup_fd < 0)
1123 			backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT,
1124 			    0600);
1125 		if (backup_fd >= 0) {
1126 			ret = fstat(backup_fd, &backup_st);
1127 			if (ret < 0) {
1128 				close(backup_fd);
1129 				backup_fd = -1;
1130 			}
1131 		}
1132 		if (backup_fd < 0) {
1133 			close(stable_fd);
1134 			stable_fd = -1;
1135 		}
1136 	}
1137 
1138 	*stable_fdp = stable_fd;
1139 	*backup_fdp = backup_fd;
1140 	if (stable_fd < 0)
1141 		return;
1142 
1143 	/* Sync up the 2 files, as required. */
1144 	if (st.st_size > 0)
1145 		copy_stable(stable_fd, backup_fd);
1146 	else if (backup_st.st_size > 0)
1147 		copy_stable(backup_fd, stable_fd);
1148 }
1149 
1150 /*
1151  * Copy the stable restart file to the backup or vice versa.
1152  */
1153 static void
copy_stable(int from_fd,int to_fd)1154 copy_stable(int from_fd, int to_fd)
1155 {
1156 	int cnt, ret;
1157 	static char buf[1024];
1158 
1159 	ret = lseek(from_fd, (off_t)0, SEEK_SET);
1160 	if (ret >= 0)
1161 		ret = lseek(to_fd, (off_t)0, SEEK_SET);
1162 	if (ret >= 0)
1163 		ret = ftruncate(to_fd, (off_t)0);
1164 	if (ret >= 0)
1165 		do {
1166 			cnt = read(from_fd, buf, 1024);
1167 			if (cnt > 0)
1168 				ret = write(to_fd, buf, cnt);
1169 			else if (cnt < 0)
1170 				ret = cnt;
1171 		} while (cnt > 0 && ret >= 0);
1172 	if (ret >= 0)
1173 		ret = fsync(to_fd);
1174 	if (ret < 0)
1175 		syslog(LOG_ERR, "stable restart copy failure: %m");
1176 }
1177 
1178 /*
1179  * Back up the stable restart file when indicated by the kernel.
1180  */
1181 static void
backup_stable(__unused int signo)1182 backup_stable(__unused int signo)
1183 {
1184 
1185 	if (stablefd >= 0)
1186 		copy_stable(stablefd, backupfd);
1187 }
1188 
1189 /*
1190  * Parse the pNFS string and extract the DS servers and ports numbers.
1191  */
1192 static void
parse_dsserver(const char * optionarg,struct nfsd_nfsd_args * nfsdargp)1193 parse_dsserver(const char *optionarg, struct nfsd_nfsd_args *nfsdargp)
1194 {
1195 	char *cp, *cp2, *dsaddr, *dshost, *dspath, *dsvol, nfsprt[9];
1196 	char *mdspath, *mdsp, ip6[INET6_ADDRSTRLEN];
1197 	const char *ad;
1198 	int ecode;
1199 	u_int adsiz, dsaddrcnt, dshostcnt, dspathcnt, hostsiz, pathsiz;
1200 	u_int mdspathcnt;
1201 	size_t dsaddrsiz, dshostsiz, dspathsiz, nfsprtsiz, mdspathsiz;
1202 	struct addrinfo hints, *ai_tcp, *res;
1203 	struct sockaddr_in sin;
1204 	struct sockaddr_in6 sin6;
1205 
1206 	cp = strdup(optionarg);
1207 	if (cp == NULL)
1208 		errx(1, "Out of memory");
1209 
1210 	/* Now, do the host names. */
1211 	dspathsiz = 1024;
1212 	dspathcnt = 0;
1213 	dspath = malloc(dspathsiz);
1214 	if (dspath == NULL)
1215 		errx(1, "Out of memory");
1216 	dshostsiz = 1024;
1217 	dshostcnt = 0;
1218 	dshost = malloc(dshostsiz);
1219 	if (dshost == NULL)
1220 		errx(1, "Out of memory");
1221 	dsaddrsiz = 1024;
1222 	dsaddrcnt = 0;
1223 	dsaddr = malloc(dsaddrsiz);
1224 	if (dsaddr == NULL)
1225 		errx(1, "Out of memory");
1226 	mdspathsiz = 1024;
1227 	mdspathcnt = 0;
1228 	mdspath = malloc(mdspathsiz);
1229 	if (mdspath == NULL)
1230 		errx(1, "Out of memory");
1231 
1232 	/* Put the NFS port# in "." form. */
1233 	snprintf(nfsprt, 9, ".%d.%d", 2049 >> 8, 2049 & 0xff);
1234 	nfsprtsiz = strlen(nfsprt);
1235 
1236 	ai_tcp = NULL;
1237 	/* Loop around for each DS server name. */
1238 	do {
1239 		cp2 = strchr(cp, ',');
1240 		if (cp2 != NULL) {
1241 			/* Not the last DS in the list. */
1242 			*cp2++ = '\0';
1243 			if (*cp2 == '\0')
1244 				usage();
1245 		}
1246 
1247 		dsvol = strchr(cp, ':');
1248 		if (dsvol == NULL || *(dsvol + 1) == '\0')
1249 			usage();
1250 		*dsvol++ = '\0';
1251 
1252 		/* Optional path for MDS file system to be stored on DS. */
1253 		mdsp = strchr(dsvol, '#');
1254 		if (mdsp != NULL) {
1255 			if (*(mdsp + 1) == '\0' || mdsp <= dsvol)
1256 				usage();
1257 			*mdsp++ = '\0';
1258 		}
1259 
1260 		/* Append this pathname to dspath. */
1261 		pathsiz = strlen(dsvol);
1262 		if (dspathcnt + pathsiz + 1 > dspathsiz) {
1263 			dspathsiz *= 2;
1264 			dspath = realloc(dspath, dspathsiz);
1265 			if (dspath == NULL)
1266 				errx(1, "Out of memory");
1267 		}
1268 		strcpy(&dspath[dspathcnt], dsvol);
1269 		dspathcnt += pathsiz + 1;
1270 
1271 		/* Append this pathname to mdspath. */
1272 		if (mdsp != NULL)
1273 			pathsiz = strlen(mdsp);
1274 		else
1275 			pathsiz = 0;
1276 		if (mdspathcnt + pathsiz + 1 > mdspathsiz) {
1277 			mdspathsiz *= 2;
1278 			mdspath = realloc(mdspath, mdspathsiz);
1279 			if (mdspath == NULL)
1280 				errx(1, "Out of memory");
1281 		}
1282 		if (mdsp != NULL)
1283 			strcpy(&mdspath[mdspathcnt], mdsp);
1284 		else
1285 			mdspath[mdspathcnt] = '\0';
1286 		mdspathcnt += pathsiz + 1;
1287 
1288 		if (ai_tcp != NULL)
1289 			freeaddrinfo(ai_tcp);
1290 
1291 		/* Get the fully qualified domain name and IP address. */
1292 		memset(&hints, 0, sizeof(hints));
1293 		hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
1294 		hints.ai_family = PF_UNSPEC;
1295 		hints.ai_socktype = SOCK_STREAM;
1296 		hints.ai_protocol = IPPROTO_TCP;
1297 		ecode = getaddrinfo(cp, NULL, &hints, &ai_tcp);
1298 		if (ecode != 0)
1299 			err(1, "getaddrinfo pnfs: %s %s", cp,
1300 			    gai_strerror(ecode));
1301 		ad = NULL;
1302 		for (res = ai_tcp; res != NULL; res = res->ai_next) {
1303 			if (res->ai_addr->sa_family == AF_INET) {
1304 				if (res->ai_addrlen < sizeof(sin))
1305 					err(1, "getaddrinfo() returned "
1306 					    "undersized IPv4 address");
1307 				/*
1308 				 * Mips cares about sockaddr_in alignment,
1309 				 * so copy the address.
1310 				 */
1311 				memcpy(&sin, res->ai_addr, sizeof(sin));
1312 				ad = inet_ntoa(sin.sin_addr);
1313 				break;
1314 			} else if (res->ai_family == AF_INET6) {
1315 				if (res->ai_addrlen < sizeof(sin6))
1316 					err(1, "getaddrinfo() returned "
1317 					    "undersized IPv6 address");
1318 				/*
1319 				 * Mips cares about sockaddr_in6 alignment,
1320 				 * so copy the address.
1321 				 */
1322 				memcpy(&sin6, res->ai_addr, sizeof(sin6));
1323 				ad = inet_ntop(AF_INET6, &sin6.sin6_addr, ip6,
1324 				    sizeof(ip6));
1325 
1326 				/*
1327 				 * XXX
1328 				 * Since a link local address will only
1329 				 * work if the client and DS are in the
1330 				 * same scope zone, only use it if it is
1331 				 * the only address.
1332 				 */
1333 				if (ad != NULL &&
1334 				    !IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
1335 					break;
1336 			}
1337 		}
1338 		if (ad == NULL)
1339 			err(1, "No IP address for %s", cp);
1340 
1341 		/* Append this address to dsaddr. */
1342 		adsiz = strlen(ad);
1343 		if (dsaddrcnt + adsiz + nfsprtsiz + 1 > dsaddrsiz) {
1344 			dsaddrsiz *= 2;
1345 			dsaddr = realloc(dsaddr, dsaddrsiz);
1346 			if (dsaddr == NULL)
1347 				errx(1, "Out of memory");
1348 		}
1349 		strcpy(&dsaddr[dsaddrcnt], ad);
1350 		strcat(&dsaddr[dsaddrcnt], nfsprt);
1351 		dsaddrcnt += adsiz + nfsprtsiz + 1;
1352 
1353 		/* Append this hostname to dshost. */
1354 		hostsiz = strlen(ai_tcp->ai_canonname);
1355 		if (dshostcnt + hostsiz + 1 > dshostsiz) {
1356 			dshostsiz *= 2;
1357 			dshost = realloc(dshost, dshostsiz);
1358 			if (dshost == NULL)
1359 				errx(1, "Out of memory");
1360 		}
1361 		strcpy(&dshost[dshostcnt], ai_tcp->ai_canonname);
1362 		dshostcnt += hostsiz + 1;
1363 
1364 		cp = cp2;
1365 	} while (cp != NULL);
1366 
1367 	nfsdargp->addr = dsaddr;
1368 	nfsdargp->addrlen = dsaddrcnt;
1369 	nfsdargp->dnshost = dshost;
1370 	nfsdargp->dnshostlen = dshostcnt;
1371 	nfsdargp->dspath = dspath;
1372 	nfsdargp->dspathlen = dspathcnt;
1373 	nfsdargp->mdspath = mdspath;
1374 	nfsdargp->mdspathlen = mdspathcnt;
1375 	freeaddrinfo(ai_tcp);
1376 }
1377 
1378