nfsd.c revision 53096
1/*
2 * Copyright (c) 1989, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static const char copyright[] =
39"@(#) Copyright (c) 1989, 1993, 1994\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif not lint
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)nfsd.c	8.9 (Berkeley) 3/29/95";
46#endif
47static const char rcsid[] =
48  "$FreeBSD: head/usr.sbin/nfsd/nfsd.c 53096 1999-11-11 17:35:36Z dillon $";
49#endif not lint
50
51#include <sys/param.h>
52#include <sys/syslog.h>
53#include <sys/wait.h>
54#include <sys/mount.h>
55
56#include <rpc/rpc.h>
57#include <rpc/pmap_clnt.h>
58
59#include <netdb.h>
60#include <arpa/inet.h>
61#ifdef ISO
62#include <netiso/iso.h>
63#endif
64#include <nfs/rpcv2.h>
65#include <nfs/nfsproto.h>
66#include <nfs/nfs.h>
67
68#ifdef NFSKERB
69#include <kerberosIV/des.h>
70#include <kerberosIV/krb.h>
71#endif
72
73#include <err.h>
74#include <errno.h>
75#include <libutil.h>
76#include <stdio.h>
77#include <stdlib.h>
78#include <strings.h>
79#include <unistd.h>
80
81/* Global defs */
82#ifdef DEBUG
83#define	syslog(e, s)	fprintf(stderr,(s))
84int	debug = 1;
85#else
86int	debug = 0;
87#endif
88
89struct	nfsd_srvargs nsd;
90#ifdef OLD_SETPROCTITLE
91char	**Argv = NULL;		/* pointer to argument vector */
92char	*LastArg = NULL;	/* end of argv */
93#endif
94
95#ifdef NFSKERB
96char		lnam[ANAME_SZ];
97KTEXT_ST	kt;
98AUTH_DAT	kauth;
99char		inst[INST_SZ];
100struct nfsrpc_fullblock kin, kout;
101struct nfsrpc_fullverf kverf;
102NFSKERBKEY_T	kivec;
103struct timeval	ktv;
104NFSKERBKEYSCHED_T kerb_keysched;
105#endif
106
107void	nonfs __P((int));
108void	reapchild __P((int));
109void	setbindhost __P((struct sockaddr_in *ia, const char *bindhost));
110#ifdef OLD_SETPROCTITLE
111#ifdef __FreeBSD__
112void	setproctitle __P((char *));
113#endif
114#endif
115void	usage __P((void));
116
117/*
118 * Nfs server daemon mostly just a user context for nfssvc()
119 *
120 * 1 - do file descriptor and signal cleanup
121 * 2 - fork the nfsd(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 nfsds' to fork off
135 */
136int
137main(argc, argv, envp)
138	int argc;
139	char *argv[], *envp[];
140{
141	struct nfsd_args nfsdargs;
142	struct sockaddr_in inetaddr, inetpeer;
143#ifdef ISO
144	struct sockaddr_iso isoaddr, isopeer;
145	char *cp;
146#endif
147	fd_set ready, sockbits;
148	int ch, cltpflag, connect_type_cnt, i, len, maxsock, msgsock;
149	int nfsdcnt, nfssvc_flag, on, reregister, sock, tcpflag, tcpsock;
150	int tp4cnt, tp4flag, tpipcnt, tpipflag, udpflag;
151	int bindhostc = 0, bindanyflag;
152	char **bindhost = NULL;
153#ifdef notyet
154	int tp4sock, tpipsock;
155#endif
156#ifdef NFSKERB
157	struct group *grp;
158	struct passwd *pwd;
159	struct ucred *cr;
160	struct timeval ktv;
161	char **cpp;
162#endif
163#ifdef __FreeBSD__
164	struct vfsconf vfc;
165	int error;
166
167	error = getvfsbyname("nfs", &vfc);
168	if (error && vfsisloadable("nfs")) {
169		if (vfsload("nfs"))
170			err(1, "vfsload(nfs)");
171		endvfsent();	/* flush cache */
172		error = getvfsbyname("nfs", &vfc);
173	}
174	if (error)
175		errx(1, "NFS is not available in the running kernel");
176#endif
177
178#ifdef OLD_SETPROCTITLE
179	/* Save start and extent of argv for setproctitle. */
180	Argv = argv;
181	if (envp == 0 || *envp == 0)
182		envp = argv;
183	while (*envp)
184		envp++;
185	LastArg = envp[-1] + strlen(envp[-1]);
186#endif
187
188#define	MAXNFSDCNT	20
189#define	DEFNFSDCNT	 4
190	nfsdcnt = DEFNFSDCNT;
191	cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
192	bindanyflag = tpipflag = udpflag = 0;
193#ifdef ISO
194#define	GETOPT	"ach:n:rtu"
195#define	USAGE	"[-acrtu] [-n num_servers] [-h bindip]"
196#else
197#define	GETOPT	"ah:n:rtu"
198#define	USAGE	"[-artu] [-n num_servers] [-h bindip]"
199#endif
200	while ((ch = getopt(argc, argv, GETOPT)) != -1)
201		switch (ch) {
202		case 'a':
203			bindanyflag = 1;
204			break;
205		case 'n':
206			nfsdcnt = atoi(optarg);
207			if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
208				warnx("nfsd count %d; reset to %d", nfsdcnt,
209				    DEFNFSDCNT);
210				nfsdcnt = DEFNFSDCNT;
211			}
212			break;
213		case 'h':
214			bindhostc++;
215			bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
216			if (bindhost == NULL)
217				errx(1, "Out of memory");
218			bindhost[bindhostc-1] = strdup(optarg);
219			if (bindhost[bindhostc-1] == NULL)
220				errx(1, "Out of memory");
221			break;
222		case 'r':
223			reregister = 1;
224			break;
225		case 't':
226			tcpflag = 1;
227			break;
228		case 'u':
229			udpflag = 1;
230			break;
231#ifdef ISO
232		case 'c':
233			cltpflag = 1;
234			break;
235#ifdef notyet
236		case 'i':
237			tp4cnt = 1;
238			break;
239		case 'p':
240			tpipcnt = 1;
241			break;
242#endif /* notyet */
243#endif /* ISO */
244		default:
245		case '?':
246			usage();
247		};
248	if (!tcpflag && !udpflag)
249		udpflag = 1;
250	argv += optind;
251	argc -= optind;
252
253	/*
254	 * XXX
255	 * Backward compatibility, trailing number is the count of daemons.
256	 */
257	if (argc > 1)
258		usage();
259	if (argc == 1) {
260		nfsdcnt = atoi(argv[0]);
261		if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
262			warnx("nfsd count %d; reset to %d", nfsdcnt,
263			    DEFNFSDCNT);
264			nfsdcnt = DEFNFSDCNT;
265		}
266	}
267
268	if (bindhostc == 0 || bindanyflag) {
269		bindhostc++;
270		bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
271		if (bindhost == NULL)
272			errx(1, "Out of memory");
273		bindhost[bindhostc-1] = strdup("*");
274		if (bindhost[bindhostc-1] == NULL)
275			errx(1, "Out of memory");
276	}
277
278	if (debug == 0) {
279		daemon(0, 0);
280		(void)signal(SIGHUP, SIG_IGN);
281		(void)signal(SIGINT, SIG_IGN);
282		(void)signal(SIGQUIT, SIG_IGN);
283		(void)signal(SIGSYS, nonfs);
284		(void)signal(SIGTERM, SIG_IGN);
285	}
286	(void)signal(SIGCHLD, reapchild);
287
288	if (reregister) {
289		if (udpflag &&
290		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
291		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)))
292			err(1, "can't register with portmap for UDP");
293		if (tcpflag &&
294		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
295		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)))
296			err(1, "can't register with portmap for TCP");
297		exit(0);
298	}
299	openlog("nfsd:", LOG_PID, LOG_DAEMON);
300
301	for (i = 0; i < nfsdcnt; i++) {
302		switch (fork()) {
303		case -1:
304			syslog(LOG_ERR, "fork: %m");
305			exit (1);
306		case 0:
307			break;
308		default:
309			continue;
310		}
311
312		setproctitle("server");
313		nfssvc_flag = NFSSVC_NFSD;
314		nsd.nsd_nfsd = NULL;
315#ifdef NFSKERB
316		if (sizeof (struct nfsrpc_fullverf) != RPCX_FULLVERF ||
317		    sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK)
318		    syslog(LOG_ERR, "Yikes NFSKERB structs not packed!");
319		nsd.nsd_authstr = (u_char *)&kt;
320		nsd.nsd_authlen = sizeof (kt);
321		nsd.nsd_verfstr = (u_char *)&kverf;
322		nsd.nsd_verflen = sizeof (kverf);
323#endif
324		while (nfssvc(nfssvc_flag, &nsd) < 0) {
325			if (errno != ENEEDAUTH) {
326				syslog(LOG_ERR, "nfssvc: %m");
327				exit(1);
328			}
329			nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
330#ifdef NFSKERB
331			/*
332			 * Get the Kerberos ticket out of the authenticator
333			 * verify it and convert the principal name to a user
334			 * name. The user name is then converted to a set of
335			 * user credentials via the password and group file.
336			 * Finally, decrypt the timestamp and validate it.
337			 * For more info see the IETF Draft "Authentication
338			 * in ONC RPC".
339			 */
340			kt.length = ntohl(kt.length);
341			if (gettimeofday(&ktv, (struct timezone *)0) == 0 &&
342			    kt.length > 0 && kt.length <=
343			    (RPCAUTH_MAXSIZ - 3 * NFSX_UNSIGNED)) {
344			    kin.w1 = NFS_KERBW1(kt);
345			    kt.mbz = 0;
346			    (void)strcpy(inst, "*");
347			    if (krb_rd_req(&kt, NFS_KERBSRV,
348				inst, nsd.nsd_haddr, &kauth, "") == RD_AP_OK &&
349				krb_kntoln(&kauth, lnam) == KSUCCESS &&
350				(pwd = getpwnam(lnam)) != NULL) {
351				cr = &nsd.nsd_cr;
352				cr->cr_uid = pwd->pw_uid;
353				cr->cr_groups[0] = pwd->pw_gid;
354				cr->cr_ngroups = 1;
355				setgrent();
356				while ((grp = getgrent()) != NULL) {
357					if (grp->gr_gid == cr->cr_groups[0])
358						continue;
359					for (cpp = grp->gr_mem;
360					    *cpp != NULL; ++cpp)
361						if (!strcmp(*cpp, lnam))
362							break;
363					if (*cpp == NULL)
364						continue;
365					cr->cr_groups[cr->cr_ngroups++]
366					    = grp->gr_gid;
367					if (cr->cr_ngroups == NGROUPS)
368						break;
369				}
370				endgrent();
371
372				/*
373				 * Get the timestamp verifier out of the
374				 * authenticator and verifier strings.
375				 */
376				kin.t1 = kverf.t1;
377				kin.t2 = kverf.t2;
378				kin.w2 = kverf.w2;
379				bzero((caddr_t)kivec, sizeof (kivec));
380				bcopy((caddr_t)kauth.session,
381				    (caddr_t)nsd.nsd_key,sizeof(kauth.session));
382
383				/*
384				 * Decrypt the timestamp verifier in CBC mode.
385				 */
386				XXX
387
388				/*
389				 * Validate the timestamp verifier, to
390				 * check that the session key is ok.
391				 */
392				nsd.nsd_timestamp.tv_sec = ntohl(kout.t1);
393				nsd.nsd_timestamp.tv_usec = ntohl(kout.t2);
394				nsd.nsd_ttl = ntohl(kout.w1);
395				if ((nsd.nsd_ttl - 1) == ntohl(kout.w2))
396				    nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHIN;
397			}
398#endif /* NFSKERB */
399		}
400		exit(0);
401	}
402
403	/* If we are serving udp, set up the socket. */
404	for (i = 0; udpflag && i < bindhostc; i++) {
405		if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
406			syslog(LOG_ERR, "can't create udp socket");
407			exit(1);
408		}
409		setbindhost(&inetaddr, bindhost[i]);
410		if (bind(sock,
411		    (struct sockaddr *)&inetaddr, sizeof(inetaddr)) < 0) {
412			syslog(LOG_ERR, "can't bind udp addr %s: %m", bindhost[i]);
413			exit(1);
414		}
415		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
416		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
417			syslog(LOG_ERR, "can't register with udp portmap");
418			exit(1);
419		}
420		nfsdargs.sock = sock;
421		nfsdargs.name = NULL;
422		nfsdargs.namelen = 0;
423		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
424			syslog(LOG_ERR, "can't Add UDP socket");
425			exit(1);
426		}
427		(void)close(sock);
428	}
429
430#ifdef ISO
431	/* If we are serving cltp, set up the socket. */
432	if (cltpflag) {
433		if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
434			syslog(LOG_ERR, "can't create cltp socket");
435			exit(1);
436		}
437		memset(&isoaddr, 0, sizeof(isoaddr));
438		isoaddr.siso_family = AF_ISO;
439		isoaddr.siso_tlen = 2;
440		cp = TSEL(&isoaddr);
441		*cp++ = (NFS_PORT >> 8);
442		*cp = (NFS_PORT & 0xff);
443		isoaddr.siso_len = sizeof(isoaddr);
444		if (bind(sock,
445		    (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
446			syslog(LOG_ERR, "can't bind cltp addr");
447			exit(1);
448		}
449#ifdef notyet
450		/*
451		 * XXX
452		 * Someday this should probably use "rpcbind", the son of
453		 * portmap.
454		 */
455		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
456			syslog(LOG_ERR, "can't register with udp portmap");
457			exit(1);
458		}
459#endif /* notyet */
460		nfsdargs.sock = sock;
461		nfsdargs.name = NULL;
462		nfsdargs.namelen = 0;
463		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
464			syslog(LOG_ERR, "can't add UDP socket");
465			exit(1);
466		}
467		close(sock);
468	}
469#endif /* ISO */
470
471	/* Now set up the master server socket waiting for tcp connections. */
472	on = 1;
473	FD_ZERO(&sockbits);
474	connect_type_cnt = 0;
475	for (i = 0; tcpflag && i < bindhostc; i++) {
476		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
477			syslog(LOG_ERR, "can't create tcp socket");
478			exit(1);
479		}
480		if (setsockopt(tcpsock,
481		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
482			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
483		setbindhost(&inetaddr, bindhost[i]);
484		if (bind(tcpsock,
485		    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
486			syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
487			exit(1);
488		}
489		if (listen(tcpsock, 5) < 0) {
490			syslog(LOG_ERR, "listen failed");
491			exit(1);
492		}
493		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
494		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
495			syslog(LOG_ERR, "can't register tcp with portmap");
496			exit(1);
497		}
498		FD_SET(tcpsock, &sockbits);
499		maxsock = tcpsock;
500		connect_type_cnt++;
501	}
502
503#ifdef notyet
504	/* Now set up the master server socket waiting for tp4 connections. */
505	if (tp4flag) {
506		if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
507			syslog(LOG_ERR, "can't create tp4 socket");
508			exit(1);
509		}
510		if (setsockopt(tp4sock,
511		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
512			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
513		memset(&isoaddr, 0, sizeof(isoaddr));
514		isoaddr.siso_family = AF_ISO;
515		isoaddr.siso_tlen = 2;
516		cp = TSEL(&isoaddr);
517		*cp++ = (NFS_PORT >> 8);
518		*cp = (NFS_PORT & 0xff);
519		isoaddr.siso_len = sizeof(isoaddr);
520		if (bind(tp4sock,
521		    (struct sockaddr *)&isoaddr, sizeof (isoaddr)) < 0) {
522			syslog(LOG_ERR, "can't bind tp4 addr");
523			exit(1);
524		}
525		if (listen(tp4sock, 5) < 0) {
526			syslog(LOG_ERR, "listen failed");
527			exit(1);
528		}
529		/*
530		 * XXX
531		 * Someday this should probably use "rpcbind", the son of
532		 * portmap.
533		 */
534		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
535			syslog(LOG_ERR, "can't register tcp with portmap");
536			exit(1);
537		}
538		FD_SET(tp4sock, &sockbits);
539		maxsock = tp4sock;
540		connect_type_cnt++;
541	}
542
543	/* Now set up the master server socket waiting for tpip connections. */
544	for (i = 0; tpipflag && i < bindhostc; i++) {
545		if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
546			syslog(LOG_ERR, "can't create tpip socket");
547			exit(1);
548		}
549		if (setsockopt(tpipsock,
550		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
551			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
552		setbindhost(&inetaddr, bindhost[i]);
553		if (bind(tpipsock,
554		    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
555			syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
556			exit(1);
557		}
558		if (listen(tpipsock, 5) < 0) {
559			syslog(LOG_ERR, "listen failed");
560			exit(1);
561		}
562		/*
563		 * XXX
564		 * Someday this should probably use "rpcbind", the son of
565		 * portmap.
566		 */
567		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
568			syslog(LOG_ERR, "can't register tcp with portmap");
569			exit(1);
570		}
571		FD_SET(tpipsock, &sockbits);
572		maxsock = tpipsock;
573		connect_type_cnt++;
574	}
575#endif /* notyet */
576
577	if (connect_type_cnt == 0)
578		exit(0);
579
580	setproctitle("master");
581
582	/*
583	 * Loop forever accepting connections and passing the sockets
584	 * into the kernel for the mounts.
585	 */
586	for (;;) {
587		ready = sockbits;
588		if (connect_type_cnt > 1) {
589			if (select(maxsock + 1,
590			    &ready, NULL, NULL, NULL) < 1) {
591				syslog(LOG_ERR, "select failed: %m");
592				exit(1);
593			}
594		}
595		if (tcpflag && FD_ISSET(tcpsock, &ready)) {
596			len = sizeof(inetpeer);
597			if ((msgsock = accept(tcpsock,
598			    (struct sockaddr *)&inetpeer, &len)) < 0) {
599				syslog(LOG_ERR, "accept failed: %m");
600				exit(1);
601			}
602			memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
603			if (setsockopt(msgsock, SOL_SOCKET,
604			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
605				syslog(LOG_ERR,
606				    "setsockopt SO_KEEPALIVE: %m");
607			nfsdargs.sock = msgsock;
608			nfsdargs.name = (caddr_t)&inetpeer;
609			nfsdargs.namelen = sizeof(inetpeer);
610			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
611			(void)close(msgsock);
612		}
613#ifdef notyet
614		if (tp4flag && FD_ISSET(tp4sock, &ready)) {
615			len = sizeof(isopeer);
616			if ((msgsock = accept(tp4sock,
617			    (struct sockaddr *)&isopeer, &len)) < 0) {
618				syslog(LOG_ERR, "accept failed: %m");
619				exit(1);
620			}
621			if (setsockopt(msgsock, SOL_SOCKET,
622			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
623				syslog(LOG_ERR,
624				    "setsockopt SO_KEEPALIVE: %m");
625			nfsdargs.sock = msgsock;
626			nfsdargs.name = (caddr_t)&isopeer;
627			nfsdargs.namelen = len;
628			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
629			(void)close(msgsock);
630		}
631		if (tpipflag && FD_ISSET(tpipsock, &ready)) {
632			len = sizeof(inetpeer);
633			if ((msgsock = accept(tpipsock,
634			    (struct sockaddr *)&inetpeer, &len)) < 0) {
635				syslog(LOG_ERR, "accept failed: %m");
636				exit(1);
637			}
638			if (setsockopt(msgsock, SOL_SOCKET,
639			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
640				syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
641			nfsdargs.sock = msgsock;
642			nfsdargs.name = (caddr_t)&inetpeer;
643			nfsdargs.namelen = len;
644			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
645			(void)close(msgsock);
646		}
647#endif /* notyet */
648	}
649}
650
651void
652setbindhost(struct sockaddr_in *ia, const char *bindhost)
653{
654	ia->sin_family = AF_INET;
655	ia->sin_port = htons(NFS_PORT);
656	ia->sin_len = sizeof(*ia);
657	if (bindhost == NULL || strcmp(bindhost,"*") == 0) {
658		ia->sin_addr.s_addr = INADDR_ANY;
659	} else {
660		if (inet_aton(bindhost, &ia->sin_addr) == 0) {
661			struct hostent *he;
662
663			he = gethostbyname2(bindhost, ia->sin_family);
664			if (he == NULL) {
665				syslog(LOG_ERR, "gethostbyname of %s failed", bindhost);
666				exit(1);
667			}
668			bcopy(he->h_addr, &ia->sin_addr, he->h_length);
669		}
670	}
671}
672
673void
674usage()
675{
676	(void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
677	exit(1);
678}
679
680void
681nonfs(signo)
682	int signo;
683{
684	syslog(LOG_ERR, "missing system call: NFS not available");
685}
686
687void
688reapchild(signo)
689	int signo;
690{
691
692	while (wait3(NULL, WNOHANG, NULL) > 0);
693}
694
695#ifdef OLD_SETPROCTITLE
696#ifdef __FreeBSD__
697void
698setproctitle(a)
699	char *a;
700{
701	register char *cp;
702	char buf[80];
703
704	cp = Argv[0];
705	(void)snprintf(buf, sizeof(buf), "nfsd-%s", a);
706	(void)strncpy(cp, buf, LastArg - cp);
707	cp += strlen(cp);
708	while (cp < LastArg)
709		*cp++ = '\0';
710	Argv[1] = NULL;
711}
712#endif	/* __FreeBSD__ */
713#endif
714