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.3 2003/08/05 07:45:42 asmodai 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;
75 #ifdef OLD_SETPROCTITLE
76 char **Argv = NULL; /* pointer to argument vector */
77 char *LastArg = NULL; /* end of argv */
85 struct nfsrpc_fullblock kin, kout;
86 struct nfsrpc_fullverf kverf;
89 NFSKERBKEYSCHED_T kerb_keysched;
92 void nonfs __P((int));
93 void reapchild __P((int));
94 void setbindhost __P((struct sockaddr_in *ia, const char *bindhost));
95 #ifdef OLD_SETPROCTITLE
97 void setproctitle __P((char *));
100 void usage __P((void));
103 * Nfs server daemon mostly just a user context for nfssvc()
105 * 1 - do file descriptor and signal cleanup
106 * 2 - fork the nfsd(s)
107 * 3 - create server socket(s)
108 * 4 - register socket with portmap
110 * For connectionless protocols, just pass the socket into the kernel via.
112 * For connection based sockets, loop doing accepts. When you get a new
113 * socket from accept, pass the msgsock into the kernel via. nfssvc().
115 * -c - support iso cltp clients
116 * -r - reregister with portmapper
117 * -t - support tcp nfs clients
118 * -u - support udp nfs clients
119 * followed by "n" which is the number of nfsds' to fork off
122 main(argc, argv, envp)
124 char *argv[], *envp[];
126 struct nfsd_args nfsdargs;
127 struct sockaddr_in inetaddr, inetpeer;
129 struct sockaddr_iso isoaddr, isopeer;
132 fd_set ready, sockbits;
133 int ch, cltpflag, connect_type_cnt, i, len, maxsock, msgsock;
134 int nfsdcnt, nfssvc_flag, on, reregister, sock, tcpflag, tcpsock;
135 int tp4cnt, tp4flag, tpipcnt, tpipflag, udpflag;
136 int bindhostc = 0, bindanyflag;
137 char **bindhost = NULL;
139 int tp4sock, tpipsock;
152 error = getvfsbyname("nfs", &vfc);
153 if (error && vfsisloadable("nfs")) {
155 err(1, "vfsload(nfs)");
156 endvfsent(); /* flush cache */
157 error = getvfsbyname("nfs", &vfc);
160 errx(1, "NFS is not available in the running kernel");
163 #ifdef OLD_SETPROCTITLE
164 /* Save start and extent of argv for setproctitle. */
166 if (envp == 0 || *envp == 0)
170 LastArg = envp[-1] + strlen(envp[-1]);
173 #define MAXNFSDCNT 20
175 nfsdcnt = DEFNFSDCNT;
176 cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
177 bindanyflag = tpipflag = udpflag = 0;
179 #define GETOPT "ach:n:rtu"
180 #define USAGE "[-acrtu] [-n num_servers] [-h bindip]"
182 #define GETOPT "ah:n:rtu"
183 #define USAGE "[-artu] [-n num_servers] [-h bindip]"
185 while ((ch = getopt(argc, argv, GETOPT)) != -1)
191 nfsdcnt = atoi(optarg);
192 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
193 warnx("nfsd count %d; reset to %d", nfsdcnt,
195 nfsdcnt = DEFNFSDCNT;
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");
233 if (!tcpflag && !udpflag)
240 * Backward compatibility, trailing number is the count of daemons.
245 nfsdcnt = atoi(argv[0]);
246 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
247 warnx("nfsd count %d; reset to %d", nfsdcnt,
249 nfsdcnt = DEFNFSDCNT;
253 if (bindhostc == 0 || bindanyflag) {
255 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
256 if (bindhost == NULL)
257 errx(1, "Out of memory");
258 bindhost[bindhostc-1] = strdup("*");
259 if (bindhost[bindhostc-1] == NULL)
260 errx(1, "Out of memory");
265 (void)signal(SIGHUP, SIG_IGN);
266 (void)signal(SIGINT, SIG_IGN);
267 (void)signal(SIGQUIT, SIG_IGN);
268 (void)signal(SIGSYS, nonfs);
269 (void)signal(SIGTERM, SIG_IGN);
271 (void)signal(SIGCHLD, reapchild);
275 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
276 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)))
277 err(1, "can't register with portmap for UDP");
279 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
280 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)))
281 err(1, "can't register with portmap for TCP");
284 openlog("nfsd:", LOG_PID, LOG_DAEMON);
286 for (i = 0; i < nfsdcnt; i++) {
289 syslog(LOG_ERR, "fork: %m");
297 setproctitle("server");
298 nfssvc_flag = NFSSVC_NFSD;
301 if (sizeof (struct nfsrpc_fullverf) != RPCX_FULLVERF ||
302 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK)
303 syslog(LOG_ERR, "Yikes NFSKERB structs not packed!");
304 nsd.nsd_authstr = (u_char *)&kt;
305 nsd.nsd_authlen = sizeof (kt);
306 nsd.nsd_verfstr = (u_char *)&kverf;
307 nsd.nsd_verflen = sizeof (kverf);
309 while (nfssvc(nfssvc_flag, &nsd) < 0) {
310 if (errno != ENEEDAUTH) {
311 syslog(LOG_ERR, "nfssvc: %m");
314 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
317 * Get the Kerberos ticket out of the authenticator
318 * verify it and convert the principal name to a user
319 * name. The user name is then converted to a set of
320 * user credentials via the password and group file.
321 * Finally, decrypt the timestamp and validate it.
322 * For more info see the IETF Draft "Authentication
325 kt.length = ntohl(kt.length);
326 if (gettimeofday(&ktv, (struct timezone *)0) == 0 &&
327 kt.length > 0 && kt.length <=
328 (RPCAUTH_MAXSIZ - 3 * NFSX_UNSIGNED)) {
329 kin.w1 = NFS_KERBW1(kt);
331 (void)strcpy(inst, "*");
332 if (krb_rd_req(&kt, NFS_KERBSRV,
333 inst, nsd.nsd_haddr, &kauth, "") == RD_AP_OK &&
334 krb_kntoln(&kauth, lnam) == KSUCCESS &&
335 (pwd = getpwnam(lnam)) != NULL) {
337 cr->cr_uid = pwd->pw_uid;
338 cr->cr_groups[0] = pwd->pw_gid;
341 while ((grp = getgrent()) != NULL) {
342 if (grp->gr_gid == cr->cr_groups[0])
344 for (cpp = grp->gr_mem;
346 if (!strcmp(*cpp, lnam))
350 cr->cr_groups[cr->cr_ngroups++]
352 if (cr->cr_ngroups == NGROUPS)
358 * Get the timestamp verifier out of the
359 * authenticator and verifier strings.
364 bzero((caddr_t)kivec, sizeof (kivec));
365 bcopy((caddr_t)kauth.session,
366 (caddr_t)nsd.nsd_key,sizeof(kauth.session));
369 * Decrypt the timestamp verifier in CBC mode.
374 * Validate the timestamp verifier, to
375 * check that the session key is ok.
377 nsd.nsd_timestamp.tv_sec = ntohl(kout.t1);
378 nsd.nsd_timestamp.tv_usec = ntohl(kout.t2);
379 nsd.nsd_ttl = ntohl(kout.w1);
380 if ((nsd.nsd_ttl - 1) == ntohl(kout.w2))
381 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHIN;
388 /* If we are serving udp, set up the socket. */
389 for (i = 0; udpflag && i < bindhostc; i++) {
390 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
391 syslog(LOG_ERR, "can't create udp socket");
394 setbindhost(&inetaddr, bindhost[i]);
396 (struct sockaddr *)&inetaddr, sizeof(inetaddr)) < 0) {
397 syslog(LOG_ERR, "can't bind udp addr %s: %m", bindhost[i]);
400 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
401 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
402 syslog(LOG_ERR, "can't register with udp portmap");
405 nfsdargs.sock = sock;
406 nfsdargs.name = NULL;
407 nfsdargs.namelen = 0;
408 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
409 syslog(LOG_ERR, "can't Add UDP socket");
416 /* If we are serving cltp, set up the socket. */
418 if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
419 syslog(LOG_ERR, "can't create cltp socket");
422 memset(&isoaddr, 0, sizeof(isoaddr));
423 isoaddr.siso_family = AF_ISO;
424 isoaddr.siso_tlen = 2;
426 *cp++ = (NFS_PORT >> 8);
427 *cp = (NFS_PORT & 0xff);
428 isoaddr.siso_len = sizeof(isoaddr);
430 (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
431 syslog(LOG_ERR, "can't bind cltp addr");
437 * Someday this should probably use "rpcbind", the son of
440 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
441 syslog(LOG_ERR, "can't register with udp portmap");
445 nfsdargs.sock = sock;
446 nfsdargs.name = NULL;
447 nfsdargs.namelen = 0;
448 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
449 syslog(LOG_ERR, "can't add UDP socket");
456 /* Now set up the master server socket waiting for tcp connections. */
459 connect_type_cnt = 0;
460 for (i = 0; tcpflag && i < bindhostc; i++) {
461 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
462 syslog(LOG_ERR, "can't create tcp socket");
465 if (setsockopt(tcpsock,
466 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
467 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
468 setbindhost(&inetaddr, bindhost[i]);
470 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
471 syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
474 if (listen(tcpsock, 5) < 0) {
475 syslog(LOG_ERR, "listen failed");
478 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
479 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
480 syslog(LOG_ERR, "can't register tcp with portmap");
483 FD_SET(tcpsock, &sockbits);
489 /* Now set up the master server socket waiting for tp4 connections. */
491 if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
492 syslog(LOG_ERR, "can't create tp4 socket");
495 if (setsockopt(tp4sock,
496 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
497 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
498 memset(&isoaddr, 0, sizeof(isoaddr));
499 isoaddr.siso_family = AF_ISO;
500 isoaddr.siso_tlen = 2;
502 *cp++ = (NFS_PORT >> 8);
503 *cp = (NFS_PORT & 0xff);
504 isoaddr.siso_len = sizeof(isoaddr);
506 (struct sockaddr *)&isoaddr, sizeof (isoaddr)) < 0) {
507 syslog(LOG_ERR, "can't bind tp4 addr");
510 if (listen(tp4sock, 5) < 0) {
511 syslog(LOG_ERR, "listen failed");
516 * Someday this should probably use "rpcbind", the son of
519 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
520 syslog(LOG_ERR, "can't register tcp with portmap");
523 FD_SET(tp4sock, &sockbits);
528 /* Now set up the master server socket waiting for tpip connections. */
529 for (i = 0; tpipflag && i < bindhostc; i++) {
530 if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
531 syslog(LOG_ERR, "can't create tpip socket");
534 if (setsockopt(tpipsock,
535 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
536 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
537 setbindhost(&inetaddr, bindhost[i]);
539 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
540 syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
543 if (listen(tpipsock, 5) < 0) {
544 syslog(LOG_ERR, "listen failed");
549 * Someday this should probably use "rpcbind", the son of
552 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
553 syslog(LOG_ERR, "can't register tcp with portmap");
556 FD_SET(tpipsock, &sockbits);
562 if (connect_type_cnt == 0)
565 setproctitle("master");
568 * Loop forever accepting connections and passing the sockets
569 * into the kernel for the mounts.
573 if (connect_type_cnt > 1) {
574 if (select(maxsock + 1,
575 &ready, NULL, NULL, NULL) < 1) {
576 syslog(LOG_ERR, "select failed: %m");
580 if (tcpflag && FD_ISSET(tcpsock, &ready)) {
581 len = sizeof(inetpeer);
582 if ((msgsock = accept(tcpsock,
583 (struct sockaddr *)&inetpeer, &len)) < 0) {
584 syslog(LOG_ERR, "accept failed: %m");
587 memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
588 if (setsockopt(msgsock, SOL_SOCKET,
589 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
591 "setsockopt SO_KEEPALIVE: %m");
592 nfsdargs.sock = msgsock;
593 nfsdargs.name = (caddr_t)&inetpeer;
594 nfsdargs.namelen = sizeof(inetpeer);
595 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
596 (void)close(msgsock);
599 if (tp4flag && FD_ISSET(tp4sock, &ready)) {
600 len = sizeof(isopeer);
601 if ((msgsock = accept(tp4sock,
602 (struct sockaddr *)&isopeer, &len)) < 0) {
603 syslog(LOG_ERR, "accept failed: %m");
606 if (setsockopt(msgsock, SOL_SOCKET,
607 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
609 "setsockopt SO_KEEPALIVE: %m");
610 nfsdargs.sock = msgsock;
611 nfsdargs.name = (caddr_t)&isopeer;
612 nfsdargs.namelen = len;
613 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
614 (void)close(msgsock);
616 if (tpipflag && FD_ISSET(tpipsock, &ready)) {
617 len = sizeof(inetpeer);
618 if ((msgsock = accept(tpipsock,
619 (struct sockaddr *)&inetpeer, &len)) < 0) {
620 syslog(LOG_ERR, "accept failed: %m");
623 if (setsockopt(msgsock, SOL_SOCKET,
624 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
625 syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
626 nfsdargs.sock = msgsock;
627 nfsdargs.name = (caddr_t)&inetpeer;
628 nfsdargs.namelen = len;
629 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
630 (void)close(msgsock);
637 setbindhost(struct sockaddr_in *ia, const char *bindhost)
639 ia->sin_family = AF_INET;
640 ia->sin_port = htons(NFS_PORT);
641 ia->sin_len = sizeof(*ia);
642 if (bindhost == NULL || strcmp(bindhost,"*") == 0) {
643 ia->sin_addr.s_addr = INADDR_ANY;
645 if (inet_aton(bindhost, &ia->sin_addr) == 0) {
648 he = gethostbyname2(bindhost, ia->sin_family);
650 syslog(LOG_ERR, "gethostbyname of %s failed", bindhost);
653 bcopy(he->h_addr, &ia->sin_addr, he->h_length);
661 (void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
669 syslog(LOG_ERR, "missing system call: NFS not available");
677 while (wait3(NULL, WNOHANG, NULL) > 0);
680 #ifdef OLD_SETPROCTITLE
690 (void)snprintf(buf, sizeof(buf), "nfsd-%s", a);
691 (void)strncpy(cp, buf, LastArg - cp);
697 #endif /* __FreeBSD__ */