2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1989, 1993, 1994
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
29 * Rick Macklem at The University of Guelph.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/param.h>
62 #include <sys/syslog.h>
63 #include <sys/ioctl.h>
67 #include <sys/ucred.h>
68 #include <sys/mount.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
73 #include <rpc/pmap_clnt.h>
74 #include <rpc/pmap_prot.h>
77 #include <netiso/iso.h>
79 #include <nfs/rpcv2.h>
80 #include <nfs/nfsproto.h>
84 #include <kerberosIV/des.h>
85 #include <kerberosIV/krb.h>
101 #define syslog(e, s) fprintf(stderr,(s))
107 struct nfsd_srvargs nsd
;
108 char **Argv
= NULL
; /* pointer to argument vector */
109 char *LastArg
= NULL
; /* end of argv */
116 struct nfsrpc_fullblock kin
, kout
;
117 struct nfsrpc_fullverf kverf
;
120 NFSKERBKEYSCHED_T kerb_keysched
;
123 void nonfs
__P((int));
124 void reapchild
__P((int));
125 void setproctitle
__P((char *));
126 void usage
__P((void));
129 * Nfs server daemon mostly just a user context for nfssvc()
131 * 1 - do file descriptor and signal cleanup
132 * 2 - fork the nfsd(s)
133 * 3 - create server socket(s)
134 * 4 - register socket with portmap
136 * For connectionless protocols, just pass the socket into the kernel via.
138 * For connection based sockets, loop doing accepts. When you get a new
139 * socket from accept, pass the msgsock into the kernel via. nfssvc().
141 * -c - support iso cltp clients
142 * -r - reregister with portmapper
143 * -t - support tcp nfs clients
144 * -u - support udp nfs clients
145 * followed by "n" which is the number of nfsds' to fork off
148 main(argc
, argv
, envp
)
150 char *argv
[], *envp
[];
154 struct nfsd_args nfsdargs
;
157 struct sockaddr_in inetaddr
, inetpeer
;
159 struct sockaddr_iso isoaddr
, isopeer
;
162 fd_set ready
, sockbits
;
163 int ch
, cltpflag
, connect_type_cnt
, i
, len
, maxsock
, msgsock
;
164 int nfsdcnt
, nfssvc_flag
, on
, reregister
, sock
, tcpflag
, tcpsock
;
165 int tp4cnt
, tp4flag
, tp4sock
, tpipcnt
, tpipflag
, tpipsock
, udpflag
;
168 /* Save start and extent of argv for setproctitle. */
170 if (envp
== 0 || *envp
== 0)
174 LastArg
= envp
[-1] + strlen(envp
[-1]);
176 #define MAXNFSDCNT 20
178 nfsdcnt
= DEFNFSDCNT
;
179 cltpflag
= reregister
= tcpflag
= tp4cnt
= tp4flag
= tpipcnt
= 0;
180 tpipflag
= udpflag
= 0;
182 #define GETOPT "cn:rtu"
183 #define USAGE "[-crtu] [-n num_servers]"
185 #define GETOPT "n:rtu"
186 #define USAGE "[-rtu] [-n num_servers]"
188 while ((ch
= getopt(argc
, argv
, GETOPT
)) != EOF
)
191 nfsdcnt
= atoi(optarg
);
192 if (nfsdcnt
< 1 || nfsdcnt
> MAXNFSDCNT
) {
193 warnx("nfsd count %d; reset to %d", DEFNFSDCNT
);
194 nfsdcnt
= DEFNFSDCNT
;
228 * Backward compatibility, trailing number is the count of daemons.
233 nfsdcnt
= atoi(argv
[0]);
234 if (nfsdcnt
< 1 || nfsdcnt
> MAXNFSDCNT
) {
235 warnx("nfsd count %d; reset to %d", DEFNFSDCNT
);
236 nfsdcnt
= DEFNFSDCNT
;
242 (void)signal(SIGHUP
, SIG_IGN
);
243 (void)signal(SIGINT
, SIG_IGN
);
244 (void)signal(SIGQUIT
, SIG_IGN
);
245 (void)signal(SIGSYS
, nonfs
);
246 (void)signal(SIGTERM
, SIG_IGN
);
248 (void)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
);
308 (void)strcpy(inst
, "*");
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. */
367 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
368 syslog(LOG_ERR
, "can't create udp socket");
371 inetaddr
.sin_family
= AF_INET
;
372 inetaddr
.sin_addr
.s_addr
= INADDR_ANY
;
373 inetaddr
.sin_port
= htons(NFS_PORT
);
374 inetaddr
.sin_len
= sizeof(inetaddr
);
376 (struct sockaddr
*)&inetaddr
, sizeof(inetaddr
)) < 0) {
377 syslog(LOG_ERR
, "can't bind udp addr");
380 if (!pmap_set(RPCPROG_NFS
, 2, IPPROTO_UDP
, NFS_PORT
) ||
381 !pmap_set(RPCPROG_NFS
, 3, IPPROTO_UDP
, NFS_PORT
)) {
382 syslog(LOG_ERR
, "can't register with udp portmap");
385 nfsdargs
.sock
= sock
;
386 nfsdargs
.name
= NULL
;
387 nfsdargs
.namelen
= 0;
388 if (nfssvc(NFSSVC_ADDSOCK
, &nfsdargs
) < 0) {
389 syslog(LOG_ERR
, "can't Add UDP socket");
396 /* If we are serving cltp, set up the socket. */
398 if ((sock
= socket(AF_ISO
, SOCK_DGRAM
, 0)) < 0) {
399 syslog(LOG_ERR
, "can't create cltp socket");
402 memset(&isoaddr
, 0, sizeof(isoaddr
));
403 isoaddr
.siso_family
= AF_ISO
;
404 isoaddr
.siso_tlen
= 2;
406 *cp
++ = (NFS_PORT
>> 8);
407 *cp
= (NFS_PORT
& 0xff);
408 isoaddr
.siso_len
= sizeof(isoaddr
);
410 (struct sockaddr
*)&isoaddr
, sizeof(isoaddr
)) < 0) {
411 syslog(LOG_ERR
, "can't bind cltp addr");
417 * Someday this should probably use "rpcbind", the son of
420 if (!pmap_set(RPCPROG_NFS
, NFS_VER2
, IPPROTO_UDP
, NFS_PORT
)) {
421 syslog(LOG_ERR
, "can't register with udp portmap");
425 nfsdargs
.sock
= sock
;
426 nfsdargs
.name
= NULL
;
427 nfsdargs
.namelen
= 0;
428 if (nfssvc(NFSSVC_ADDSOCK
, &nfsdargs
) < 0) {
429 syslog(LOG_ERR
, "can't add UDP socket");
436 /* Now set up the master server socket waiting for tcp connections. */
439 connect_type_cnt
= 0;
441 if ((tcpsock
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0) {
442 syslog(LOG_ERR
, "can't create tcp socket");
445 if (setsockopt(tcpsock
,
446 SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof(on
)) < 0)
447 syslog(LOG_ERR
, "setsockopt SO_REUSEADDR: %m");
448 inetaddr
.sin_family
= AF_INET
;
449 inetaddr
.sin_addr
.s_addr
= INADDR_ANY
;
450 inetaddr
.sin_port
= htons(NFS_PORT
);
451 inetaddr
.sin_len
= sizeof(inetaddr
);
453 (struct sockaddr
*)&inetaddr
, sizeof (inetaddr
)) < 0) {
454 syslog(LOG_ERR
, "can't bind tcp addr");
457 if (listen(tcpsock
, 5) < 0) {
458 syslog(LOG_ERR
, "listen failed");
461 if (!pmap_set(RPCPROG_NFS
, 2, IPPROTO_TCP
, NFS_PORT
) ||
462 !pmap_set(RPCPROG_NFS
, 3, IPPROTO_TCP
, NFS_PORT
)) {
463 syslog(LOG_ERR
, "can't register tcp with portmap");
466 FD_SET(tcpsock
, &sockbits
);
472 /* Now set up the master server socket waiting for tp4 connections. */
474 if ((tp4sock
= socket(AF_ISO
, SOCK_SEQPACKET
, 0)) < 0) {
475 syslog(LOG_ERR
, "can't create tp4 socket");
478 if (setsockopt(tp4sock
,
479 SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof(on
)) < 0)
480 syslog(LOG_ERR
, "setsockopt SO_REUSEADDR: %m");
481 memset(&isoaddr
, 0, sizeof(isoaddr
));
482 isoaddr
.siso_family
= AF_ISO
;
483 isoaddr
.siso_tlen
= 2;
485 *cp
++ = (NFS_PORT
>> 8);
486 *cp
= (NFS_PORT
& 0xff);
487 isoaddr
.siso_len
= sizeof(isoaddr
);
489 (struct sockaddr
*)&isoaddr
, sizeof (isoaddr
)) < 0) {
490 syslog(LOG_ERR
, "can't bind tp4 addr");
493 if (listen(tp4sock
, 5) < 0) {
494 syslog(LOG_ERR
, "listen failed");
499 * Someday this should probably use "rpcbind", the son of
502 if (!pmap_set(RPCPROG_NFS
, NFS_VER2
, IPPROTO_TCP
, NFS_PORT
)) {
503 syslog(LOG_ERR
, "can't register tcp with portmap");
506 FD_SET(tp4sock
, &sockbits
);
511 /* Now set up the master server socket waiting for tpip connections. */
513 if ((tpipsock
= socket(AF_INET
, SOCK_SEQPACKET
, 0)) < 0) {
514 syslog(LOG_ERR
, "can't create tpip socket");
517 if (setsockopt(tpipsock
,
518 SOL_SOCKET
, SO_REUSEADDR
, (char *)&on
, sizeof(on
)) < 0)
519 syslog(LOG_ERR
, "setsockopt SO_REUSEADDR: %m");
520 inetaddr
.sin_family
= AF_INET
;
521 inetaddr
.sin_addr
.s_addr
= INADDR_ANY
;
522 inetaddr
.sin_port
= htons(NFS_PORT
);
523 inetaddr
.sin_len
= sizeof(inetaddr
);
525 (struct sockaddr
*)&inetaddr
, sizeof (inetaddr
)) < 0) {
526 syslog(LOG_ERR
, "can't bind tcp addr");
529 if (listen(tpipsock
, 5) < 0) {
530 syslog(LOG_ERR
, "listen failed");
535 * Someday this should probably use "rpcbind", the son of
538 if (!pmap_set(RPCPROG_NFS
, NFS_VER2
, IPPROTO_TCP
, NFS_PORT
)) {
539 syslog(LOG_ERR
, "can't register tcp with portmap");
542 FD_SET(tpipsock
, &sockbits
);
548 if (connect_type_cnt
== 0)
551 setproctitle("master");
554 * Loop forever accepting connections and passing the sockets
555 * into the kernel for the mounts.
559 if (connect_type_cnt
> 1) {
560 if (select(maxsock
+ 1,
561 &ready
, NULL
, NULL
, NULL
) < 1) {
562 syslog(LOG_ERR
, "select failed: %m");
566 if (tcpflag
&& FD_ISSET(tcpsock
, &ready
)) {
567 len
= sizeof(inetpeer
);
568 if ((msgsock
= accept(tcpsock
,
569 (struct sockaddr
*)&inetpeer
, &len
)) < 0) {
570 syslog(LOG_ERR
, "accept failed: %m");
573 memset(inetpeer
.sin_zero
, 0, sizeof(inetpeer
.sin_zero
));
574 if (setsockopt(msgsock
, SOL_SOCKET
,
575 SO_KEEPALIVE
, (char *)&on
, sizeof(on
)) < 0)
577 "setsockopt SO_KEEPALIVE: %m");
578 nfsdargs
.sock
= msgsock
;
579 nfsdargs
.name
= (caddr_t
)&inetpeer
;
580 nfsdargs
.namelen
= sizeof(inetpeer
);
581 nfssvc(NFSSVC_ADDSOCK
, &nfsdargs
);
582 (void)close(msgsock
);
585 if (tp4flag
&& FD_ISSET(tp4sock
, &ready
)) {
586 len
= sizeof(isopeer
);
587 if ((msgsock
= accept(tp4sock
,
588 (struct sockaddr
*)&isopeer
, &len
)) < 0) {
589 syslog(LOG_ERR
, "accept failed: %m");
592 if (setsockopt(msgsock
, SOL_SOCKET
,
593 SO_KEEPALIVE
, (char *)&on
, sizeof(on
)) < 0)
595 "setsockopt SO_KEEPALIVE: %m");
596 nfsdargs
.sock
= msgsock
;
597 nfsdargs
.name
= (caddr_t
)&isopeer
;
598 nfsdargs
.namelen
= len
;
599 nfssvc(NFSSVC_ADDSOCK
, &nfsdargs
);
600 (void)close(msgsock
);
602 if (tpipflag
&& FD_ISSET(tpipsock
, &ready
)) {
603 len
= sizeof(inetpeer
);
604 if ((msgsock
= accept(tpipsock
,
605 (struct sockaddr
*)&inetpeer
, &len
)) < 0) {
606 syslog(LOG_ERR
, "Accept failed: %m");
609 if (setsockopt(msgsock
, SOL_SOCKET
,
610 SO_KEEPALIVE
, (char *)&on
, sizeof(on
)) < 0)
611 syslog(LOG_ERR
, "setsockopt SO_KEEPALIVE: %m");
612 nfsdargs
.sock
= msgsock
;
613 nfsdargs
.name
= (caddr_t
)&inetpeer
;
614 nfsdargs
.namelen
= len
;
615 nfssvc(NFSSVC_ADDSOCK
, &nfsdargs
);
616 (void)close(msgsock
);
625 (void)fprintf(stderr
, "usage: nfsd %s\n", USAGE
);
633 syslog(LOG_ERR
, "missing system call: NFS not available.");
641 while (wait3(NULL
, WNOHANG
, NULL
) > 0);
652 (void)snprintf(buf
, sizeof(buf
), "nfsd-%s", a
);
653 (void)strncpy(cp
, buf
, LastArg
- cp
);