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
36 * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
37 * @(#)nfsd.c 8.9 (Berkeley) 3/29/95
38 * $FreeBSD: src/sbin/nfsd/nfsd.c,v 1.15.2.1 2000/09/16 22:52:23 brian Exp $
39 * $DragonFly: src/sbin/nfsd/nfsd.c,v 1.10 2006/12/27 23:06:29 corecode Exp $
42 #include <sys/param.h>
43 #include <sys/syslog.h>
45 #include <sys/mount.h>
48 #include <rpc/pmap_clnt.h>
51 #include <arpa/inet.h>
53 #include <netiso/iso.h>
55 #include <nfs/rpcv2.h>
56 #include <nfs/nfsproto.h>
68 #define syslog(e, s) fprintf(stderr,(s))
74 struct nfsd_srvargs nsd;
81 struct nfsrpc_fullblock kin, kout;
82 struct nfsrpc_fullverf kverf;
85 NFSKERBKEYSCHED_T kerb_keysched;
90 void setbindhost(struct sockaddr_in *ia, const char *bindhost);
94 * Nfs server daemon mostly just a user context for nfssvc()
96 * 1 - do file descriptor and signal cleanup
97 * 2 - fork the nfsd(s)
98 * 3 - create server socket(s)
99 * 4 - register socket with portmap
101 * For connectionless protocols, just pass the socket into the kernel via.
103 * For connection based sockets, loop doing accepts. When you get a new
104 * socket from accept, pass the msgsock into the kernel via. nfssvc().
106 * -c - support iso cltp clients
107 * -r - reregister with portmapper
108 * -t - support tcp nfs clients
109 * -u - support udp nfs clients
110 * followed by "n" which is the number of nfsds' to fork off
113 main(int argc, char **argv)
115 struct nfsd_args nfsdargs;
116 struct sockaddr_in inetaddr, inetpeer;
118 struct sockaddr_iso isoaddr, isopeer;
121 fd_set ready, sockbits;
122 int ch, cltpflag, connect_type_cnt, i, len, maxsock = -1, msgsock;
123 int nfsdcnt, nfssvc_flag, on, reregister, sock, tcpflag, tcpsock = -1;
124 int tp4cnt, tp4flag, tpipcnt, tpipflag, udpflag;
125 int bindhostc = 0, bindanyflag;
126 char **bindhost = NULL;
128 int tp4sock, tpipsock;
140 error = getvfsbyname("nfs", &vfc);
141 if (error && vfsisloadable("nfs")) {
143 err(1, "vfsload(nfs)");
144 endvfsent(); /* flush cache */
145 error = getvfsbyname("nfs", &vfc);
148 errx(1, "NFS is not available in the running kernel");
150 #define MAXNFSDCNT 20
152 nfsdcnt = DEFNFSDCNT;
153 cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
154 bindanyflag = tpipflag = udpflag = 0;
156 #define GETOPT "ach:n:rtu"
157 #define USAGE "[-acrtu] [-n num_servers] [-h bindip]"
159 #define GETOPT "ah:n:rtu"
160 #define USAGE "[-artu] [-n num_servers] [-h bindip]"
162 while ((ch = getopt(argc, argv, GETOPT)) != -1)
168 nfsdcnt = atoi(optarg);
169 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
170 warnx("nfsd count %d; reset to %d", nfsdcnt,
172 nfsdcnt = DEFNFSDCNT;
177 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
178 if (bindhost == NULL)
179 errx(1, "Out of memory");
180 bindhost[bindhostc-1] = strdup(optarg);
181 if (bindhost[bindhostc-1] == NULL)
182 errx(1, "Out of memory");
210 if (!tcpflag && !udpflag)
217 * Backward compatibility, trailing number is the count of daemons.
222 nfsdcnt = atoi(argv[0]);
223 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
224 warnx("nfsd count %d; reset to %d", nfsdcnt,
226 nfsdcnt = DEFNFSDCNT;
230 if (bindhostc == 0 || bindanyflag) {
232 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
233 if (bindhost == NULL)
234 errx(1, "Out of memory");
235 bindhost[bindhostc-1] = strdup("*");
236 if (bindhost[bindhostc-1] == NULL)
237 errx(1, "Out of memory");
242 signal(SIGHUP, SIG_IGN);
243 signal(SIGINT, SIG_IGN);
244 signal(SIGQUIT, SIG_IGN);
245 signal(SIGSYS, nonfs);
246 signal(SIGTERM, SIG_IGN);
248 signal(SIGCHLD, reapchild);
252 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
253 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)))
254 err(1, "can't register with portmap for UDP");
256 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
257 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)))
258 err(1, "can't register with portmap for TCP");
261 openlog("nfsd:", LOG_PID, LOG_DAEMON);
263 for (i = 0; i < nfsdcnt; i++) {
266 syslog(LOG_ERR, "fork: %m");
274 setproctitle("server");
275 nfssvc_flag = NFSSVC_NFSD;
278 if (sizeof (struct nfsrpc_fullverf) != RPCX_FULLVERF ||
279 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK)
280 syslog(LOG_ERR, "Yikes NFSKERB structs not packed!");
281 nsd.nsd_authstr = (u_char *)&kt;
282 nsd.nsd_authlen = sizeof (kt);
283 nsd.nsd_verfstr = (u_char *)&kverf;
284 nsd.nsd_verflen = sizeof (kverf);
286 while (nfssvc(nfssvc_flag, &nsd) < 0) {
287 if (errno != ENEEDAUTH) {
288 syslog(LOG_ERR, "nfssvc: %m");
291 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
294 * Get the Kerberos ticket out of the authenticator
295 * verify it and convert the principal name to a user
296 * name. The user name is then converted to a set of
297 * user credentials via the password and group file.
298 * Finally, decrypt the timestamp and validate it.
299 * For more info see the IETF Draft "Authentication
302 kt.length = ntohl(kt.length);
303 if (gettimeofday(&ktv, (struct timezone *)0) == 0 &&
304 kt.length > 0 && kt.length <=
305 (RPCAUTH_MAXSIZ - 3 * NFSX_UNSIGNED)) {
306 kin.w1 = NFS_KERBW1(kt);
309 if (krb_rd_req(&kt, NFS_KERBSRV,
310 inst, nsd.nsd_haddr, &kauth, "") == RD_AP_OK &&
311 krb_kntoln(&kauth, lnam) == KSUCCESS &&
312 (pwd = getpwnam(lnam)) != NULL) {
314 cr->cr_uid = pwd->pw_uid;
315 cr->cr_groups[0] = pwd->pw_gid;
318 while ((grp = getgrent()) != NULL) {
319 if (grp->gr_gid == cr->cr_groups[0])
321 for (cpp = grp->gr_mem;
323 if (!strcmp(*cpp, lnam))
327 cr->cr_groups[cr->cr_ngroups++]
329 if (cr->cr_ngroups == NGROUPS)
335 * Get the timestamp verifier out of the
336 * authenticator and verifier strings.
341 bzero((caddr_t)kivec, sizeof (kivec));
342 bcopy((caddr_t)kauth.session,
343 (caddr_t)nsd.nsd_key,sizeof(kauth.session));
346 * Decrypt the timestamp verifier in CBC mode.
351 * Validate the timestamp verifier, to
352 * check that the session key is ok.
354 nsd.nsd_timestamp.tv_sec = ntohl(kout.t1);
355 nsd.nsd_timestamp.tv_usec = ntohl(kout.t2);
356 nsd.nsd_ttl = ntohl(kout.w1);
357 if ((nsd.nsd_ttl - 1) == ntohl(kout.w2))
358 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHIN;
365 /* If we are serving udp, set up the socket. */
366 for (i = 0; udpflag && i < bindhostc; i++) {
367 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
368 syslog(LOG_ERR, "can't create udp socket");
371 setbindhost(&inetaddr, bindhost[i]);
373 (struct sockaddr *)&inetaddr, sizeof(inetaddr)) < 0) {
374 syslog(LOG_ERR, "can't bind udp addr %s: %m", bindhost[i]);
377 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
378 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
379 syslog(LOG_ERR, "can't register with udp portmap");
382 nfsdargs.sock = sock;
383 nfsdargs.name = NULL;
384 nfsdargs.namelen = 0;
385 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
386 syslog(LOG_ERR, "can't Add UDP socket");
393 /* If we are serving cltp, set up the socket. */
395 if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
396 syslog(LOG_ERR, "can't create cltp socket");
399 memset(&isoaddr, 0, sizeof(isoaddr));
400 isoaddr.siso_family = AF_ISO;
401 isoaddr.siso_tlen = 2;
403 *cp++ = (NFS_PORT >> 8);
404 *cp = (NFS_PORT & 0xff);
405 isoaddr.siso_len = sizeof(isoaddr);
407 (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
408 syslog(LOG_ERR, "can't bind cltp addr");
414 * Someday this should probably use "rpcbind", the son of
417 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
418 syslog(LOG_ERR, "can't register with udp portmap");
422 nfsdargs.sock = sock;
423 nfsdargs.name = NULL;
424 nfsdargs.namelen = 0;
425 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
426 syslog(LOG_ERR, "can't add UDP socket");
433 /* Now set up the master server socket waiting for tcp connections. */
436 connect_type_cnt = 0;
437 for (i = 0; tcpflag && i < bindhostc; i++) {
438 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
439 syslog(LOG_ERR, "can't create tcp socket");
442 if (setsockopt(tcpsock,
443 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
444 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
445 setbindhost(&inetaddr, bindhost[i]);
447 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
448 syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
451 if (listen(tcpsock, 5) < 0) {
452 syslog(LOG_ERR, "listen failed");
455 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
456 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
457 syslog(LOG_ERR, "can't register tcp with portmap");
460 FD_SET(tcpsock, &sockbits);
466 /* Now set up the master server socket waiting for tp4 connections. */
468 if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
469 syslog(LOG_ERR, "can't create tp4 socket");
472 if (setsockopt(tp4sock,
473 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
474 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
475 memset(&isoaddr, 0, sizeof(isoaddr));
476 isoaddr.siso_family = AF_ISO;
477 isoaddr.siso_tlen = 2;
479 *cp++ = (NFS_PORT >> 8);
480 *cp = (NFS_PORT & 0xff);
481 isoaddr.siso_len = sizeof(isoaddr);
483 (struct sockaddr *)&isoaddr, sizeof (isoaddr)) < 0) {
484 syslog(LOG_ERR, "can't bind tp4 addr");
487 if (listen(tp4sock, 5) < 0) {
488 syslog(LOG_ERR, "listen failed");
493 * Someday this should probably use "rpcbind", the son of
496 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
497 syslog(LOG_ERR, "can't register tcp with portmap");
500 FD_SET(tp4sock, &sockbits);
505 /* Now set up the master server socket waiting for tpip connections. */
506 for (i = 0; tpipflag && i < bindhostc; i++) {
507 if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
508 syslog(LOG_ERR, "can't create tpip socket");
511 if (setsockopt(tpipsock,
512 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
513 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
514 setbindhost(&inetaddr, bindhost[i]);
516 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
517 syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
520 if (listen(tpipsock, 5) < 0) {
521 syslog(LOG_ERR, "listen failed");
526 * Someday this should probably use "rpcbind", the son of
529 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
530 syslog(LOG_ERR, "can't register tcp with portmap");
533 FD_SET(tpipsock, &sockbits);
539 if (connect_type_cnt == 0)
542 setproctitle("master");
545 * Loop forever accepting connections and passing the sockets
546 * into the kernel for the mounts.
550 if (connect_type_cnt > 1) {
551 if (select(maxsock + 1,
552 &ready, NULL, NULL, NULL) < 1) {
553 syslog(LOG_ERR, "select failed: %m");
557 if (tcpflag && FD_ISSET(tcpsock, &ready)) {
558 len = sizeof(inetpeer);
559 if ((msgsock = accept(tcpsock,
560 (struct sockaddr *)&inetpeer, &len)) < 0) {
561 syslog(LOG_ERR, "accept failed: %m");
564 memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
565 if (setsockopt(msgsock, SOL_SOCKET,
566 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
568 "setsockopt SO_KEEPALIVE: %m");
569 nfsdargs.sock = msgsock;
570 nfsdargs.name = (caddr_t)&inetpeer;
571 nfsdargs.namelen = sizeof(inetpeer);
572 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
576 if (tp4flag && FD_ISSET(tp4sock, &ready)) {
577 len = sizeof(isopeer);
578 if ((msgsock = accept(tp4sock,
579 (struct sockaddr *)&isopeer, &len)) < 0) {
580 syslog(LOG_ERR, "accept failed: %m");
583 if (setsockopt(msgsock, SOL_SOCKET,
584 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
586 "setsockopt SO_KEEPALIVE: %m");
587 nfsdargs.sock = msgsock;
588 nfsdargs.name = (caddr_t)&isopeer;
589 nfsdargs.namelen = len;
590 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
593 if (tpipflag && FD_ISSET(tpipsock, &ready)) {
594 len = sizeof(inetpeer);
595 if ((msgsock = accept(tpipsock,
596 (struct sockaddr *)&inetpeer, &len)) < 0) {
597 syslog(LOG_ERR, "accept failed: %m");
600 if (setsockopt(msgsock, SOL_SOCKET,
601 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
602 syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
603 nfsdargs.sock = msgsock;
604 nfsdargs.name = (caddr_t)&inetpeer;
605 nfsdargs.namelen = len;
606 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
614 setbindhost(struct sockaddr_in *ia, const char *bindhost)
616 ia->sin_family = AF_INET;
617 ia->sin_port = htons(NFS_PORT);
618 ia->sin_len = sizeof(*ia);
619 if (bindhost == NULL || strcmp(bindhost,"*") == 0) {
620 ia->sin_addr.s_addr = INADDR_ANY;
622 if (inet_aton(bindhost, &ia->sin_addr) == 0) {
625 he = gethostbyname2(bindhost, ia->sin_family);
627 syslog(LOG_ERR, "gethostbyname of %s failed", bindhost);
630 bcopy(he->h_addr, &ia->sin_addr, he->h_length);
638 fprintf(stderr, "usage: nfsd %s\n", USAGE);
643 nonfs(int signo __unused)
645 syslog(LOG_ERR, "missing system call: NFS not available");
649 reapchild(int signo __unused)
652 while (wait3(NULL, WNOHANG, NULL) > 0);