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.2 2003/06/17 04:27:34 dillon 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>
60 #include <kerberosIV/des.h>
61 #include <kerberosIV/krb.h>
73 #define syslog(e, s) fprintf(stderr,(s))
79 struct nfsd_srvargs nsd;
80 #ifdef OLD_SETPROCTITLE
81 char **Argv = NULL; /* pointer to argument vector */
82 char *LastArg = NULL; /* end of argv */
90 struct nfsrpc_fullblock kin, kout;
91 struct nfsrpc_fullverf kverf;
94 NFSKERBKEYSCHED_T kerb_keysched;
97 void nonfs __P((int));
98 void reapchild __P((int));
99 void setbindhost __P((struct sockaddr_in *ia, const char *bindhost));
100 #ifdef OLD_SETPROCTITLE
102 void setproctitle __P((char *));
105 void usage __P((void));
108 * Nfs server daemon mostly just a user context for nfssvc()
110 * 1 - do file descriptor and signal cleanup
111 * 2 - fork the nfsd(s)
112 * 3 - create server socket(s)
113 * 4 - register socket with portmap
115 * For connectionless protocols, just pass the socket into the kernel via.
117 * For connection based sockets, loop doing accepts. When you get a new
118 * socket from accept, pass the msgsock into the kernel via. nfssvc().
120 * -c - support iso cltp clients
121 * -r - reregister with portmapper
122 * -t - support tcp nfs clients
123 * -u - support udp nfs clients
124 * followed by "n" which is the number of nfsds' to fork off
127 main(argc, argv, envp)
129 char *argv[], *envp[];
131 struct nfsd_args nfsdargs;
132 struct sockaddr_in inetaddr, inetpeer;
134 struct sockaddr_iso isoaddr, isopeer;
137 fd_set ready, sockbits;
138 int ch, cltpflag, connect_type_cnt, i, len, maxsock, msgsock;
139 int nfsdcnt, nfssvc_flag, on, reregister, sock, tcpflag, tcpsock;
140 int tp4cnt, tp4flag, tpipcnt, tpipflag, udpflag;
141 int bindhostc = 0, bindanyflag;
142 char **bindhost = NULL;
144 int tp4sock, tpipsock;
157 error = getvfsbyname("nfs", &vfc);
158 if (error && vfsisloadable("nfs")) {
160 err(1, "vfsload(nfs)");
161 endvfsent(); /* flush cache */
162 error = getvfsbyname("nfs", &vfc);
165 errx(1, "NFS is not available in the running kernel");
168 #ifdef OLD_SETPROCTITLE
169 /* Save start and extent of argv for setproctitle. */
171 if (envp == 0 || *envp == 0)
175 LastArg = envp[-1] + strlen(envp[-1]);
178 #define MAXNFSDCNT 20
180 nfsdcnt = DEFNFSDCNT;
181 cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
182 bindanyflag = tpipflag = udpflag = 0;
184 #define GETOPT "ach:n:rtu"
185 #define USAGE "[-acrtu] [-n num_servers] [-h bindip]"
187 #define GETOPT "ah:n:rtu"
188 #define USAGE "[-artu] [-n num_servers] [-h bindip]"
190 while ((ch = getopt(argc, argv, GETOPT)) != -1)
196 nfsdcnt = atoi(optarg);
197 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
198 warnx("nfsd count %d; reset to %d", nfsdcnt,
200 nfsdcnt = DEFNFSDCNT;
205 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
206 if (bindhost == NULL)
207 errx(1, "Out of memory");
208 bindhost[bindhostc-1] = strdup(optarg);
209 if (bindhost[bindhostc-1] == NULL)
210 errx(1, "Out of memory");
238 if (!tcpflag && !udpflag)
245 * Backward compatibility, trailing number is the count of daemons.
250 nfsdcnt = atoi(argv[0]);
251 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
252 warnx("nfsd count %d; reset to %d", nfsdcnt,
254 nfsdcnt = DEFNFSDCNT;
258 if (bindhostc == 0 || bindanyflag) {
260 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
261 if (bindhost == NULL)
262 errx(1, "Out of memory");
263 bindhost[bindhostc-1] = strdup("*");
264 if (bindhost[bindhostc-1] == NULL)
265 errx(1, "Out of memory");
270 (void)signal(SIGHUP, SIG_IGN);
271 (void)signal(SIGINT, SIG_IGN);
272 (void)signal(SIGQUIT, SIG_IGN);
273 (void)signal(SIGSYS, nonfs);
274 (void)signal(SIGTERM, SIG_IGN);
276 (void)signal(SIGCHLD, reapchild);
280 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
281 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)))
282 err(1, "can't register with portmap for UDP");
284 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
285 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)))
286 err(1, "can't register with portmap for TCP");
289 openlog("nfsd:", LOG_PID, LOG_DAEMON);
291 for (i = 0; i < nfsdcnt; i++) {
294 syslog(LOG_ERR, "fork: %m");
302 setproctitle("server");
303 nfssvc_flag = NFSSVC_NFSD;
306 if (sizeof (struct nfsrpc_fullverf) != RPCX_FULLVERF ||
307 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK)
308 syslog(LOG_ERR, "Yikes NFSKERB structs not packed!");
309 nsd.nsd_authstr = (u_char *)&kt;
310 nsd.nsd_authlen = sizeof (kt);
311 nsd.nsd_verfstr = (u_char *)&kverf;
312 nsd.nsd_verflen = sizeof (kverf);
314 while (nfssvc(nfssvc_flag, &nsd) < 0) {
315 if (errno != ENEEDAUTH) {
316 syslog(LOG_ERR, "nfssvc: %m");
319 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
322 * Get the Kerberos ticket out of the authenticator
323 * verify it and convert the principal name to a user
324 * name. The user name is then converted to a set of
325 * user credentials via the password and group file.
326 * Finally, decrypt the timestamp and validate it.
327 * For more info see the IETF Draft "Authentication
330 kt.length = ntohl(kt.length);
331 if (gettimeofday(&ktv, (struct timezone *)0) == 0 &&
332 kt.length > 0 && kt.length <=
333 (RPCAUTH_MAXSIZ - 3 * NFSX_UNSIGNED)) {
334 kin.w1 = NFS_KERBW1(kt);
336 (void)strcpy(inst, "*");
337 if (krb_rd_req(&kt, NFS_KERBSRV,
338 inst, nsd.nsd_haddr, &kauth, "") == RD_AP_OK &&
339 krb_kntoln(&kauth, lnam) == KSUCCESS &&
340 (pwd = getpwnam(lnam)) != NULL) {
342 cr->cr_uid = pwd->pw_uid;
343 cr->cr_groups[0] = pwd->pw_gid;
346 while ((grp = getgrent()) != NULL) {
347 if (grp->gr_gid == cr->cr_groups[0])
349 for (cpp = grp->gr_mem;
351 if (!strcmp(*cpp, lnam))
355 cr->cr_groups[cr->cr_ngroups++]
357 if (cr->cr_ngroups == NGROUPS)
363 * Get the timestamp verifier out of the
364 * authenticator and verifier strings.
369 bzero((caddr_t)kivec, sizeof (kivec));
370 bcopy((caddr_t)kauth.session,
371 (caddr_t)nsd.nsd_key,sizeof(kauth.session));
374 * Decrypt the timestamp verifier in CBC mode.
379 * Validate the timestamp verifier, to
380 * check that the session key is ok.
382 nsd.nsd_timestamp.tv_sec = ntohl(kout.t1);
383 nsd.nsd_timestamp.tv_usec = ntohl(kout.t2);
384 nsd.nsd_ttl = ntohl(kout.w1);
385 if ((nsd.nsd_ttl - 1) == ntohl(kout.w2))
386 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHIN;
393 /* If we are serving udp, set up the socket. */
394 for (i = 0; udpflag && i < bindhostc; i++) {
395 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
396 syslog(LOG_ERR, "can't create udp socket");
399 setbindhost(&inetaddr, bindhost[i]);
401 (struct sockaddr *)&inetaddr, sizeof(inetaddr)) < 0) {
402 syslog(LOG_ERR, "can't bind udp addr %s: %m", bindhost[i]);
405 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
406 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
407 syslog(LOG_ERR, "can't register with udp portmap");
410 nfsdargs.sock = sock;
411 nfsdargs.name = NULL;
412 nfsdargs.namelen = 0;
413 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
414 syslog(LOG_ERR, "can't Add UDP socket");
421 /* If we are serving cltp, set up the socket. */
423 if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
424 syslog(LOG_ERR, "can't create cltp socket");
427 memset(&isoaddr, 0, sizeof(isoaddr));
428 isoaddr.siso_family = AF_ISO;
429 isoaddr.siso_tlen = 2;
431 *cp++ = (NFS_PORT >> 8);
432 *cp = (NFS_PORT & 0xff);
433 isoaddr.siso_len = sizeof(isoaddr);
435 (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
436 syslog(LOG_ERR, "can't bind cltp addr");
442 * Someday this should probably use "rpcbind", the son of
445 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
446 syslog(LOG_ERR, "can't register with udp portmap");
450 nfsdargs.sock = sock;
451 nfsdargs.name = NULL;
452 nfsdargs.namelen = 0;
453 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
454 syslog(LOG_ERR, "can't add UDP socket");
461 /* Now set up the master server socket waiting for tcp connections. */
464 connect_type_cnt = 0;
465 for (i = 0; tcpflag && i < bindhostc; i++) {
466 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
467 syslog(LOG_ERR, "can't create tcp socket");
470 if (setsockopt(tcpsock,
471 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
472 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
473 setbindhost(&inetaddr, bindhost[i]);
475 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
476 syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
479 if (listen(tcpsock, 5) < 0) {
480 syslog(LOG_ERR, "listen failed");
483 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
484 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
485 syslog(LOG_ERR, "can't register tcp with portmap");
488 FD_SET(tcpsock, &sockbits);
494 /* Now set up the master server socket waiting for tp4 connections. */
496 if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
497 syslog(LOG_ERR, "can't create tp4 socket");
500 if (setsockopt(tp4sock,
501 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
502 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
503 memset(&isoaddr, 0, sizeof(isoaddr));
504 isoaddr.siso_family = AF_ISO;
505 isoaddr.siso_tlen = 2;
507 *cp++ = (NFS_PORT >> 8);
508 *cp = (NFS_PORT & 0xff);
509 isoaddr.siso_len = sizeof(isoaddr);
511 (struct sockaddr *)&isoaddr, sizeof (isoaddr)) < 0) {
512 syslog(LOG_ERR, "can't bind tp4 addr");
515 if (listen(tp4sock, 5) < 0) {
516 syslog(LOG_ERR, "listen failed");
521 * Someday this should probably use "rpcbind", the son of
524 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
525 syslog(LOG_ERR, "can't register tcp with portmap");
528 FD_SET(tp4sock, &sockbits);
533 /* Now set up the master server socket waiting for tpip connections. */
534 for (i = 0; tpipflag && i < bindhostc; i++) {
535 if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
536 syslog(LOG_ERR, "can't create tpip socket");
539 if (setsockopt(tpipsock,
540 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
541 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
542 setbindhost(&inetaddr, bindhost[i]);
544 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
545 syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
548 if (listen(tpipsock, 5) < 0) {
549 syslog(LOG_ERR, "listen failed");
554 * Someday this should probably use "rpcbind", the son of
557 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
558 syslog(LOG_ERR, "can't register tcp with portmap");
561 FD_SET(tpipsock, &sockbits);
567 if (connect_type_cnt == 0)
570 setproctitle("master");
573 * Loop forever accepting connections and passing the sockets
574 * into the kernel for the mounts.
578 if (connect_type_cnt > 1) {
579 if (select(maxsock + 1,
580 &ready, NULL, NULL, NULL) < 1) {
581 syslog(LOG_ERR, "select failed: %m");
585 if (tcpflag && FD_ISSET(tcpsock, &ready)) {
586 len = sizeof(inetpeer);
587 if ((msgsock = accept(tcpsock,
588 (struct sockaddr *)&inetpeer, &len)) < 0) {
589 syslog(LOG_ERR, "accept failed: %m");
592 memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
593 if (setsockopt(msgsock, SOL_SOCKET,
594 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
596 "setsockopt SO_KEEPALIVE: %m");
597 nfsdargs.sock = msgsock;
598 nfsdargs.name = (caddr_t)&inetpeer;
599 nfsdargs.namelen = sizeof(inetpeer);
600 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
601 (void)close(msgsock);
604 if (tp4flag && FD_ISSET(tp4sock, &ready)) {
605 len = sizeof(isopeer);
606 if ((msgsock = accept(tp4sock,
607 (struct sockaddr *)&isopeer, &len)) < 0) {
608 syslog(LOG_ERR, "accept failed: %m");
611 if (setsockopt(msgsock, SOL_SOCKET,
612 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
614 "setsockopt SO_KEEPALIVE: %m");
615 nfsdargs.sock = msgsock;
616 nfsdargs.name = (caddr_t)&isopeer;
617 nfsdargs.namelen = len;
618 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
619 (void)close(msgsock);
621 if (tpipflag && FD_ISSET(tpipsock, &ready)) {
622 len = sizeof(inetpeer);
623 if ((msgsock = accept(tpipsock,
624 (struct sockaddr *)&inetpeer, &len)) < 0) {
625 syslog(LOG_ERR, "accept failed: %m");
628 if (setsockopt(msgsock, SOL_SOCKET,
629 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
630 syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
631 nfsdargs.sock = msgsock;
632 nfsdargs.name = (caddr_t)&inetpeer;
633 nfsdargs.namelen = len;
634 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
635 (void)close(msgsock);
642 setbindhost(struct sockaddr_in *ia, const char *bindhost)
644 ia->sin_family = AF_INET;
645 ia->sin_port = htons(NFS_PORT);
646 ia->sin_len = sizeof(*ia);
647 if (bindhost == NULL || strcmp(bindhost,"*") == 0) {
648 ia->sin_addr.s_addr = INADDR_ANY;
650 if (inet_aton(bindhost, &ia->sin_addr) == 0) {
653 he = gethostbyname2(bindhost, ia->sin_family);
655 syslog(LOG_ERR, "gethostbyname of %s failed", bindhost);
658 bcopy(he->h_addr, &ia->sin_addr, he->h_length);
666 (void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
674 syslog(LOG_ERR, "missing system call: NFS not available");
682 while (wait3(NULL, WNOHANG, NULL) > 0);
685 #ifdef OLD_SETPROCTITLE
695 (void)snprintf(buf, sizeof(buf), "nfsd-%s", a);
696 (void)strncpy(cp, buf, LastArg - cp);
702 #endif /* __FreeBSD__ */