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.8 2005/11/06 12:44:49 swildner 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;
94 void setbindhost(struct sockaddr_in *ia, const char *bindhost);
95 #ifdef OLD_SETPROCTITLE
97 void setproctitle(char *);
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(int argc, char **argv, char **envp)
124 struct nfsd_args nfsdargs;
125 struct sockaddr_in inetaddr, inetpeer;
127 struct sockaddr_iso isoaddr, isopeer;
130 fd_set ready, sockbits;
131 int ch, cltpflag, connect_type_cnt, i, len, maxsock = -1, msgsock;
132 int nfsdcnt, nfssvc_flag, on, reregister, sock, tcpflag, tcpsock = -1;
133 int tp4cnt, tp4flag, tpipcnt, tpipflag, udpflag;
134 int bindhostc = 0, bindanyflag;
135 char **bindhost = NULL;
137 int tp4sock, tpipsock;
150 error = getvfsbyname("nfs", &vfc);
151 if (error && vfsisloadable("nfs")) {
153 err(1, "vfsload(nfs)");
154 endvfsent(); /* flush cache */
155 error = getvfsbyname("nfs", &vfc);
158 errx(1, "NFS is not available in the running kernel");
161 #ifdef OLD_SETPROCTITLE
162 /* Save start and extent of argv for setproctitle. */
164 if (envp == 0 || *envp == 0)
168 LastArg = envp[-1] + strlen(envp[-1]);
171 #define MAXNFSDCNT 20
173 nfsdcnt = DEFNFSDCNT;
174 cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
175 bindanyflag = tpipflag = udpflag = 0;
177 #define GETOPT "ach:n:rtu"
178 #define USAGE "[-acrtu] [-n num_servers] [-h bindip]"
180 #define GETOPT "ah:n:rtu"
181 #define USAGE "[-artu] [-n num_servers] [-h bindip]"
183 while ((ch = getopt(argc, argv, GETOPT)) != -1)
189 nfsdcnt = atoi(optarg);
190 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
191 warnx("nfsd count %d; reset to %d", nfsdcnt,
193 nfsdcnt = DEFNFSDCNT;
198 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
199 if (bindhost == NULL)
200 errx(1, "Out of memory");
201 bindhost[bindhostc-1] = strdup(optarg);
202 if (bindhost[bindhostc-1] == NULL)
203 errx(1, "Out of memory");
231 if (!tcpflag && !udpflag)
238 * Backward compatibility, trailing number is the count of daemons.
243 nfsdcnt = atoi(argv[0]);
244 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
245 warnx("nfsd count %d; reset to %d", nfsdcnt,
247 nfsdcnt = DEFNFSDCNT;
251 if (bindhostc == 0 || bindanyflag) {
253 bindhost = realloc(bindhost,sizeof(char *)*bindhostc);
254 if (bindhost == NULL)
255 errx(1, "Out of memory");
256 bindhost[bindhostc-1] = strdup("*");
257 if (bindhost[bindhostc-1] == NULL)
258 errx(1, "Out of memory");
263 signal(SIGHUP, SIG_IGN);
264 signal(SIGINT, SIG_IGN);
265 signal(SIGQUIT, SIG_IGN);
266 signal(SIGSYS, nonfs);
267 signal(SIGTERM, SIG_IGN);
269 signal(SIGCHLD, reapchild);
273 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
274 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)))
275 err(1, "can't register with portmap for UDP");
277 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
278 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)))
279 err(1, "can't register with portmap for TCP");
282 openlog("nfsd:", LOG_PID, LOG_DAEMON);
284 for (i = 0; i < nfsdcnt; i++) {
287 syslog(LOG_ERR, "fork: %m");
295 setproctitle("server");
296 nfssvc_flag = NFSSVC_NFSD;
299 if (sizeof (struct nfsrpc_fullverf) != RPCX_FULLVERF ||
300 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK)
301 syslog(LOG_ERR, "Yikes NFSKERB structs not packed!");
302 nsd.nsd_authstr = (u_char *)&kt;
303 nsd.nsd_authlen = sizeof (kt);
304 nsd.nsd_verfstr = (u_char *)&kverf;
305 nsd.nsd_verflen = sizeof (kverf);
307 while (nfssvc(nfssvc_flag, &nsd) < 0) {
308 if (errno != ENEEDAUTH) {
309 syslog(LOG_ERR, "nfssvc: %m");
312 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
315 * Get the Kerberos ticket out of the authenticator
316 * verify it and convert the principal name to a user
317 * name. The user name is then converted to a set of
318 * user credentials via the password and group file.
319 * Finally, decrypt the timestamp and validate it.
320 * For more info see the IETF Draft "Authentication
323 kt.length = ntohl(kt.length);
324 if (gettimeofday(&ktv, (struct timezone *)0) == 0 &&
325 kt.length > 0 && kt.length <=
326 (RPCAUTH_MAXSIZ - 3 * NFSX_UNSIGNED)) {
327 kin.w1 = NFS_KERBW1(kt);
330 if (krb_rd_req(&kt, NFS_KERBSRV,
331 inst, nsd.nsd_haddr, &kauth, "") == RD_AP_OK &&
332 krb_kntoln(&kauth, lnam) == KSUCCESS &&
333 (pwd = getpwnam(lnam)) != NULL) {
335 cr->cr_uid = pwd->pw_uid;
336 cr->cr_groups[0] = pwd->pw_gid;
339 while ((grp = getgrent()) != NULL) {
340 if (grp->gr_gid == cr->cr_groups[0])
342 for (cpp = grp->gr_mem;
344 if (!strcmp(*cpp, lnam))
348 cr->cr_groups[cr->cr_ngroups++]
350 if (cr->cr_ngroups == NGROUPS)
356 * Get the timestamp verifier out of the
357 * authenticator and verifier strings.
362 bzero((caddr_t)kivec, sizeof (kivec));
363 bcopy((caddr_t)kauth.session,
364 (caddr_t)nsd.nsd_key,sizeof(kauth.session));
367 * Decrypt the timestamp verifier in CBC mode.
372 * Validate the timestamp verifier, to
373 * check that the session key is ok.
375 nsd.nsd_timestamp.tv_sec = ntohl(kout.t1);
376 nsd.nsd_timestamp.tv_usec = ntohl(kout.t2);
377 nsd.nsd_ttl = ntohl(kout.w1);
378 if ((nsd.nsd_ttl - 1) == ntohl(kout.w2))
379 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHIN;
386 /* If we are serving udp, set up the socket. */
387 for (i = 0; udpflag && i < bindhostc; i++) {
388 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
389 syslog(LOG_ERR, "can't create udp socket");
392 setbindhost(&inetaddr, bindhost[i]);
394 (struct sockaddr *)&inetaddr, sizeof(inetaddr)) < 0) {
395 syslog(LOG_ERR, "can't bind udp addr %s: %m", bindhost[i]);
398 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
399 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
400 syslog(LOG_ERR, "can't register with udp portmap");
403 nfsdargs.sock = sock;
404 nfsdargs.name = NULL;
405 nfsdargs.namelen = 0;
406 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
407 syslog(LOG_ERR, "can't Add UDP socket");
414 /* If we are serving cltp, set up the socket. */
416 if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
417 syslog(LOG_ERR, "can't create cltp socket");
420 memset(&isoaddr, 0, sizeof(isoaddr));
421 isoaddr.siso_family = AF_ISO;
422 isoaddr.siso_tlen = 2;
424 *cp++ = (NFS_PORT >> 8);
425 *cp = (NFS_PORT & 0xff);
426 isoaddr.siso_len = sizeof(isoaddr);
428 (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
429 syslog(LOG_ERR, "can't bind cltp addr");
435 * Someday this should probably use "rpcbind", the son of
438 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
439 syslog(LOG_ERR, "can't register with udp portmap");
443 nfsdargs.sock = sock;
444 nfsdargs.name = NULL;
445 nfsdargs.namelen = 0;
446 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
447 syslog(LOG_ERR, "can't add UDP socket");
454 /* Now set up the master server socket waiting for tcp connections. */
457 connect_type_cnt = 0;
458 for (i = 0; tcpflag && i < bindhostc; i++) {
459 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
460 syslog(LOG_ERR, "can't create tcp socket");
463 if (setsockopt(tcpsock,
464 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
465 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
466 setbindhost(&inetaddr, bindhost[i]);
468 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
469 syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
472 if (listen(tcpsock, 5) < 0) {
473 syslog(LOG_ERR, "listen failed");
476 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
477 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
478 syslog(LOG_ERR, "can't register tcp with portmap");
481 FD_SET(tcpsock, &sockbits);
487 /* Now set up the master server socket waiting for tp4 connections. */
489 if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
490 syslog(LOG_ERR, "can't create tp4 socket");
493 if (setsockopt(tp4sock,
494 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
495 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
496 memset(&isoaddr, 0, sizeof(isoaddr));
497 isoaddr.siso_family = AF_ISO;
498 isoaddr.siso_tlen = 2;
500 *cp++ = (NFS_PORT >> 8);
501 *cp = (NFS_PORT & 0xff);
502 isoaddr.siso_len = sizeof(isoaddr);
504 (struct sockaddr *)&isoaddr, sizeof (isoaddr)) < 0) {
505 syslog(LOG_ERR, "can't bind tp4 addr");
508 if (listen(tp4sock, 5) < 0) {
509 syslog(LOG_ERR, "listen failed");
514 * Someday this should probably use "rpcbind", the son of
517 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
518 syslog(LOG_ERR, "can't register tcp with portmap");
521 FD_SET(tp4sock, &sockbits);
526 /* Now set up the master server socket waiting for tpip connections. */
527 for (i = 0; tpipflag && i < bindhostc; i++) {
528 if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
529 syslog(LOG_ERR, "can't create tpip socket");
532 if (setsockopt(tpipsock,
533 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
534 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
535 setbindhost(&inetaddr, bindhost[i]);
537 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
538 syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]);
541 if (listen(tpipsock, 5) < 0) {
542 syslog(LOG_ERR, "listen failed");
547 * Someday this should probably use "rpcbind", the son of
550 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
551 syslog(LOG_ERR, "can't register tcp with portmap");
554 FD_SET(tpipsock, &sockbits);
560 if (connect_type_cnt == 0)
563 setproctitle("master");
566 * Loop forever accepting connections and passing the sockets
567 * into the kernel for the mounts.
571 if (connect_type_cnt > 1) {
572 if (select(maxsock + 1,
573 &ready, NULL, NULL, NULL) < 1) {
574 syslog(LOG_ERR, "select failed: %m");
578 if (tcpflag && FD_ISSET(tcpsock, &ready)) {
579 len = sizeof(inetpeer);
580 if ((msgsock = accept(tcpsock,
581 (struct sockaddr *)&inetpeer, &len)) < 0) {
582 syslog(LOG_ERR, "accept failed: %m");
585 memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
586 if (setsockopt(msgsock, SOL_SOCKET,
587 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
589 "setsockopt SO_KEEPALIVE: %m");
590 nfsdargs.sock = msgsock;
591 nfsdargs.name = (caddr_t)&inetpeer;
592 nfsdargs.namelen = sizeof(inetpeer);
593 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
597 if (tp4flag && FD_ISSET(tp4sock, &ready)) {
598 len = sizeof(isopeer);
599 if ((msgsock = accept(tp4sock,
600 (struct sockaddr *)&isopeer, &len)) < 0) {
601 syslog(LOG_ERR, "accept failed: %m");
604 if (setsockopt(msgsock, SOL_SOCKET,
605 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
607 "setsockopt SO_KEEPALIVE: %m");
608 nfsdargs.sock = msgsock;
609 nfsdargs.name = (caddr_t)&isopeer;
610 nfsdargs.namelen = len;
611 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
614 if (tpipflag && FD_ISSET(tpipsock, &ready)) {
615 len = sizeof(inetpeer);
616 if ((msgsock = accept(tpipsock,
617 (struct sockaddr *)&inetpeer, &len)) < 0) {
618 syslog(LOG_ERR, "accept failed: %m");
621 if (setsockopt(msgsock, SOL_SOCKET,
622 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
623 syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
624 nfsdargs.sock = msgsock;
625 nfsdargs.name = (caddr_t)&inetpeer;
626 nfsdargs.namelen = len;
627 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
635 setbindhost(struct sockaddr_in *ia, const char *bindhost)
637 ia->sin_family = AF_INET;
638 ia->sin_port = htons(NFS_PORT);
639 ia->sin_len = sizeof(*ia);
640 if (bindhost == NULL || strcmp(bindhost,"*") == 0) {
641 ia->sin_addr.s_addr = INADDR_ANY;
643 if (inet_aton(bindhost, &ia->sin_addr) == 0) {
646 he = gethostbyname2(bindhost, ia->sin_family);
648 syslog(LOG_ERR, "gethostbyname of %s failed", bindhost);
651 bcopy(he->h_addr, &ia->sin_addr, he->h_length);
659 fprintf(stderr, "usage: nfsd %s\n", USAGE);
666 syslog(LOG_ERR, "missing system call: NFS not available");
673 while (wait3(NULL, WNOHANG, NULL) > 0);
676 #ifdef OLD_SETPROCTITLE
679 setproctitle(char *a)
685 snprintf(buf, sizeof(buf), "nfsd-%s", a);
686 strncpy(cp, buf, LastArg - cp);
692 #endif /* __FreeBSD__ */