2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)nfsd.c 8.9 (Berkeley) 3/29/95";
47 static const char rcsid[] =
48 "$FreeBSD: src/sbin/nfsd/nfsd.c,v 1.15.2.1 2000/09/16 22:52:23 brian Exp $";
51 #include <sys/param.h>
52 #include <sys/syslog.h>
54 #include <sys/mount.h>
57 #include <rpc/pmap_clnt.h>
60 #include <arpa/inet.h>
62 #include <netiso/iso.h>
64 #include <nfs/rpcv2.h>
65 #include <nfs/nfsproto.h>
69 #include <kerberosIV/des.h>
70 #include <kerberosIV/krb.h>
82 #define syslog(e, s) fprintf(stderr,(s))
88 struct nfsd_srvargs nsd;
89 #ifdef OLD_SETPROCTITLE
90 char **Argv = NULL; /* pointer to argument vector */
91 char *LastArg = NULL; /* end of argv */
99 struct nfsrpc_fullblock kin, kout;
100 struct nfsrpc_fullverf kverf;
103 NFSKERBKEYSCHED_T kerb_keysched;
106 void nonfs __P((int));
107 void reapchild __P((int));
108 void setbindhost __P((struct sockaddr_in *ia, const char *bindhost));
109 #ifdef OLD_SETPROCTITLE
111 void setproctitle __P((char *));
114 void usage __P((void));
117 * Nfs server daemon mostly just a user context for nfssvc()
119 * 1 - do file descriptor and signal cleanup
120 * 2 - fork the nfsd(s)
121 * 3 - create server socket(s)
122 * 4 - register socket with portmap
124 * For connectionless protocols, just pass the socket into the kernel via.
126 * For connection based sockets, loop doing accepts. When you get a new
127 * socket from accept, pass the msgsock into the kernel via. nfssvc().
129 * -c - support iso cltp clients
130 * -r - reregister with portmapper
131 * -t - support tcp nfs clients
132 * -u - support udp nfs clients
133 * followed by "n" which is the number of nfsds' to fork off
136 main(argc, argv, envp)
138 char *argv[], *envp[];
140 struct nfsd_args nfsdargs;
141 struct sockaddr_in inetaddr, inetpeer;
143 struct sockaddr_iso isoaddr, isopeer;
146 fd_set ready, sockbits;
147 int ch, cltpflag, connect_type_cnt, i, len, maxsock, msgsock;
148 int nfsdcnt, nfssvc_flag, on, reregister, sock, tcpflag, tcpsock;
149 int tp4cnt, tp4flag, tpipcnt, tpipflag, udpflag;
150 int bindhostc = 0, bindanyflag;
151 char **bindhost = NULL;
153 int tp4sock, tpipsock;
166 error = getvfsbyname("nfs", &vfc);
167 if (error && vfsisloadable("nfs")) {
169 err(1, "vfsload(nfs)");
170 endvfsent(); /* flush cache */
171 error = getvfsbyname("nfs", &vfc);
174 errx(1, "NFS is not available in the running kernel");
177 #ifdef OLD_SETPROCTITLE
178 /* Save start and extent of argv for setproctitle. */
180 if (envp == 0 || *envp == 0)
184 LastArg = envp[-1] + strlen(envp[-1]);
187 #define MAXNFSDCNT 20
189 nfsdcnt = DEFNFSDCNT;
190 cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
191 bindanyflag = tpipflag = udpflag = 0;
193 #define GETOPT "ach:n:rtu"
194 #define USAGE "[-acrtu] [-n num_servers] [-h bindip]"
196 #define GETOPT "ah:n:rtu"
197 #define USAGE "[-artu] [-n num_servers] [-h bindip]"
199 while ((ch = getopt(argc, argv, GETOPT)) != -1)
205 nfsdcnt = atoi(optarg);
206 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
207 warnx("nfsd count %d; reset to %d", nfsdcnt,
209 nfsdcnt = DEFNFSDCNT;
214 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
215 if (bindhost == NULL)
216 errx(1, "Out of memory");
217 bindhost[bindhostc-1] = strdup(optarg);
218 if (bindhost[bindhostc-1] == NULL)
219 errx(1, "Out of memory");
247 if (!tcpflag && !udpflag)
254 * Backward compatibility, trailing number is the count of daemons.
259 nfsdcnt = atoi(argv[0]);
260 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
261 warnx("nfsd count %d; reset to %d", nfsdcnt,
263 nfsdcnt = DEFNFSDCNT;
267 if (bindhostc == 0 || bindanyflag) {
269 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
270 if (bindhost == NULL)
271 errx(1, "Out of memory");
272 bindhost[bindhostc-1] = strdup("*");
273 if (bindhost[bindhostc-1] == NULL)
274 errx(1, "Out of memory");
279 (void)signal(SIGHUP, SIG_IGN);
280 (void)signal(SIGINT, SIG_IGN);
281 (void)signal(SIGQUIT, SIG_IGN);
282 (void)signal(SIGSYS, nonfs);
283 (void)signal(SIGTERM, SIG_IGN);
285 (void)signal(SIGCHLD, reapchild);
289 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
290 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)))
291 err(1, "can't register with portmap for UDP");
293 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
294 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)))
295 err(1, "can't register with portmap for TCP");
298 openlog("nfsd:", LOG_PID, LOG_DAEMON);
300 for (i = 0; i < nfsdcnt; i++) {
303 syslog(LOG_ERR, "fork: %m");
311 setproctitle("server");
312 nfssvc_flag = NFSSVC_NFSD;
315 if (sizeof (struct nfsrpc_fullverf) != RPCX_FULLVERF ||
316 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK)
317 syslog(LOG_ERR, "Yikes NFSKERB structs not packed!");
318 nsd.nsd_authstr = (u_char *)&kt;
319 nsd.nsd_authlen = sizeof (kt);
320 nsd.nsd_verfstr = (u_char *)&kverf;
321 nsd.nsd_verflen = sizeof (kverf);
323 while (nfssvc(nfssvc_flag, &nsd) < 0) {
324 if (errno != ENEEDAUTH) {
325 syslog(LOG_ERR, "nfssvc: %m");
328 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
331 * Get the Kerberos ticket out of the authenticator
332 * verify it and convert the principal name to a user
333 * name. The user name is then converted to a set of
334 * user credentials via the password and group file.
335 * Finally, decrypt the timestamp and validate it.
336 * For more info see the IETF Draft "Authentication
339 kt.length = ntohl(kt.length);
340 if (gettimeofday(&ktv, (struct timezone *)0) == 0 &&
341 kt.length > 0 && kt.length <=
342 (RPCAUTH_MAXSIZ - 3 * NFSX_UNSIGNED)) {
343 kin.w1 = NFS_KERBW1(kt);
345 (void)strcpy(inst, "*");
346 if (krb_rd_req(&kt, NFS_KERBSRV,
347 inst, nsd.nsd_haddr, &kauth, "") == RD_AP_OK &&
348 krb_kntoln(&kauth, lnam) == KSUCCESS &&
349 (pwd = getpwnam(lnam)) != NULL) {
351 cr->cr_uid = pwd->pw_uid;
352 cr->cr_groups[0] = pwd->pw_gid;
355 while ((grp = getgrent()) != NULL) {
356 if (grp->gr_gid == cr->cr_groups[0])
358 for (cpp = grp->gr_mem;
360 if (!strcmp(*cpp, lnam))
364 cr->cr_groups[cr->cr_ngroups++]
366 if (cr->cr_ngroups == NGROUPS)
372 * Get the timestamp verifier out of the
373 * authenticator and verifier strings.
378 bzero((caddr_t)kivec, sizeof (kivec));
379 bcopy((caddr_t)kauth.session,
380 (caddr_t)nsd.nsd_key,sizeof(kauth.session));
383 * Decrypt the timestamp verifier in CBC mode.
388 * Validate the timestamp verifier, to
389 * check that the session key is ok.
391 nsd.nsd_timestamp.tv_sec = ntohl(kout.t1);
392 nsd.nsd_timestamp.tv_usec = ntohl(kout.t2);
393 nsd.nsd_ttl = ntohl(kout.w1);
394 if ((nsd.nsd_ttl - 1) == ntohl(kout.w2))
395 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHIN;
402 /* If we are serving udp, set up the socket. */
403 for (i = 0; udpflag && i < bindhostc; i++) {
404 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
405 syslog(LOG_ERR, "can't create udp socket");
408 setbindhost(&inetaddr, bindhost[i]);
410 (struct sockaddr *)&inetaddr, sizeof(inetaddr)) < 0) {
411 syslog(LOG_ERR, "can't bind udp addr %s: %m", bindhost[i]);
414 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
415 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
416 syslog(LOG_ERR, "can't register with udp portmap");
419 nfsdargs.sock = sock;
420 nfsdargs.name = NULL;
421 nfsdargs.namelen = 0;
422 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
423 syslog(LOG_ERR, "can't Add UDP socket");
430 /* If we are serving cltp, set up the socket. */
432 if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
433 syslog(LOG_ERR, "can't create cltp socket");
436 memset(&isoaddr, 0, sizeof(isoaddr));
437 isoaddr.siso_family = AF_ISO;
438 isoaddr.siso_tlen = 2;
440 *cp++ = (NFS_PORT >> 8);
441 *cp = (NFS_PORT & 0xff);
442 isoaddr.siso_len = sizeof(isoaddr);
444 (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
445 syslog(LOG_ERR, "can't bind cltp addr");
451 * Someday this should probably use "rpcbind", the son of
454 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
455 syslog(LOG_ERR, "can't register with udp portmap");
459 nfsdargs.sock = sock;
460 nfsdargs.name = NULL;
461 nfsdargs.namelen = 0;
462 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
463 syslog(LOG_ERR, "can't add UDP socket");
470 /* Now set up the master server socket waiting for tcp connections. */
473 connect_type_cnt = 0;
474 for (i = 0; tcpflag && i < bindhostc; i++) {
475 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
476 syslog(LOG_ERR, "can't create tcp socket");
479 if (setsockopt(tcpsock,
480 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
481 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
482 setbindhost(&inetaddr, bindhost[i]);
484 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
485 syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
488 if (listen(tcpsock, 5) < 0) {
489 syslog(LOG_ERR, "listen failed");
492 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
493 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
494 syslog(LOG_ERR, "can't register tcp with portmap");
497 FD_SET(tcpsock, &sockbits);
503 /* Now set up the master server socket waiting for tp4 connections. */
505 if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
506 syslog(LOG_ERR, "can't create tp4 socket");
509 if (setsockopt(tp4sock,
510 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
511 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
512 memset(&isoaddr, 0, sizeof(isoaddr));
513 isoaddr.siso_family = AF_ISO;
514 isoaddr.siso_tlen = 2;
516 *cp++ = (NFS_PORT >> 8);
517 *cp = (NFS_PORT & 0xff);
518 isoaddr.siso_len = sizeof(isoaddr);
520 (struct sockaddr *)&isoaddr, sizeof (isoaddr)) < 0) {
521 syslog(LOG_ERR, "can't bind tp4 addr");
524 if (listen(tp4sock, 5) < 0) {
525 syslog(LOG_ERR, "listen failed");
530 * Someday this should probably use "rpcbind", the son of
533 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
534 syslog(LOG_ERR, "can't register tcp with portmap");
537 FD_SET(tp4sock, &sockbits);
542 /* Now set up the master server socket waiting for tpip connections. */
543 for (i = 0; tpipflag && i < bindhostc; i++) {
544 if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
545 syslog(LOG_ERR, "can't create tpip socket");
548 if (setsockopt(tpipsock,
549 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
550 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
551 setbindhost(&inetaddr, bindhost[i]);
553 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
554 syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
557 if (listen(tpipsock, 5) < 0) {
558 syslog(LOG_ERR, "listen failed");
563 * Someday this should probably use "rpcbind", the son of
566 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
567 syslog(LOG_ERR, "can't register tcp with portmap");
570 FD_SET(tpipsock, &sockbits);
576 if (connect_type_cnt == 0)
579 setproctitle("master");
582 * Loop forever accepting connections and passing the sockets
583 * into the kernel for the mounts.
587 if (connect_type_cnt > 1) {
588 if (select(maxsock + 1,
589 &ready, NULL, NULL, NULL) < 1) {
590 syslog(LOG_ERR, "select failed: %m");
594 if (tcpflag && FD_ISSET(tcpsock, &ready)) {
595 len = sizeof(inetpeer);
596 if ((msgsock = accept(tcpsock,
597 (struct sockaddr *)&inetpeer, &len)) < 0) {
598 syslog(LOG_ERR, "accept failed: %m");
601 memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
602 if (setsockopt(msgsock, SOL_SOCKET,
603 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
605 "setsockopt SO_KEEPALIVE: %m");
606 nfsdargs.sock = msgsock;
607 nfsdargs.name = (caddr_t)&inetpeer;
608 nfsdargs.namelen = sizeof(inetpeer);
609 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
610 (void)close(msgsock);
613 if (tp4flag && FD_ISSET(tp4sock, &ready)) {
614 len = sizeof(isopeer);
615 if ((msgsock = accept(tp4sock,
616 (struct sockaddr *)&isopeer, &len)) < 0) {
617 syslog(LOG_ERR, "accept failed: %m");
620 if (setsockopt(msgsock, SOL_SOCKET,
621 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
623 "setsockopt SO_KEEPALIVE: %m");
624 nfsdargs.sock = msgsock;
625 nfsdargs.name = (caddr_t)&isopeer;
626 nfsdargs.namelen = len;
627 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
628 (void)close(msgsock);
630 if (tpipflag && FD_ISSET(tpipsock, &ready)) {
631 len = sizeof(inetpeer);
632 if ((msgsock = accept(tpipsock,
633 (struct sockaddr *)&inetpeer, &len)) < 0) {
634 syslog(LOG_ERR, "accept failed: %m");
637 if (setsockopt(msgsock, SOL_SOCKET,
638 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
639 syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
640 nfsdargs.sock = msgsock;
641 nfsdargs.name = (caddr_t)&inetpeer;
642 nfsdargs.namelen = len;
643 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
644 (void)close(msgsock);
651 setbindhost(struct sockaddr_in *ia, const char *bindhost)
653 ia->sin_family = AF_INET;
654 ia->sin_port = htons(NFS_PORT);
655 ia->sin_len = sizeof(*ia);
656 if (bindhost == NULL || strcmp(bindhost,"*") == 0) {
657 ia->sin_addr.s_addr = INADDR_ANY;
659 if (inet_aton(bindhost, &ia->sin_addr) == 0) {
662 he = gethostbyname2(bindhost, ia->sin_family);
664 syslog(LOG_ERR, "gethostbyname of %s failed", bindhost);
667 bcopy(he->h_addr, &ia->sin_addr, he->h_length);
675 (void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
683 syslog(LOG_ERR, "missing system call: NFS not available");
691 while (wait3(NULL, WNOHANG, NULL) > 0);
694 #ifdef OLD_SETPROCTITLE
704 (void)snprintf(buf, sizeof(buf), "nfsd-%s", a);
705 (void)strncpy(cp, buf, LastArg - cp);
711 #endif /* __FreeBSD__ */