Home | History | Annotate | Line # | Download | only in nfsd
nfsd.c revision 1.26
      1  1.26      mrg /*	$NetBSD: nfsd.c,v 1.26 1998/07/12 09:13:27 mrg Exp $	*/
      2  1.16      cgd 
      3   1.1      cgd /*
      4  1.11  mycroft  * Copyright (c) 1989, 1993, 1994
      5  1.11  mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * This code is derived from software contributed to Berkeley by
      8   1.1      cgd  * Rick Macklem at The University of Guelph.
      9   1.1      cgd  *
     10   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1      cgd  * modification, are permitted provided that the following conditions
     12   1.1      cgd  * are met:
     13   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1      cgd  *    must display the following acknowledgement:
     20   1.1      cgd  *	This product includes software developed by the University of
     21   1.1      cgd  *	California, Berkeley and its contributors.
     22   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1      cgd  *    may be used to endorse or promote products derived from this software
     24   1.1      cgd  *    without specific prior written permission.
     25   1.1      cgd  *
     26   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1      cgd  * SUCH DAMAGE.
     37   1.1      cgd  */
     38   1.1      cgd 
     39  1.23    lukem #include <sys/cdefs.h>
     40   1.1      cgd #ifndef lint
     41  1.23    lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
     42  1.23    lukem 	The Regents of the University of California.  All rights reserved.\n");
     43   1.1      cgd #endif not lint
     44   1.1      cgd 
     45   1.1      cgd #ifndef lint
     46  1.16      cgd #if 0
     47  1.18     fvdl static char sccsid[] = "@(#)nfsd.c	8.9 (Berkeley) 3/29/95";
     48  1.16      cgd #else
     49  1.26      mrg __RCSID("$NetBSD: nfsd.c,v 1.26 1998/07/12 09:13:27 mrg Exp $");
     50  1.16      cgd #endif
     51  1.18     fvdl #endif /* not lint */
     52   1.1      cgd 
     53  1.11  mycroft #include <sys/param.h>
     54   1.1      cgd #include <sys/ioctl.h>
     55   1.1      cgd #include <sys/stat.h>
     56   1.1      cgd #include <sys/wait.h>
     57  1.11  mycroft #include <sys/uio.h>
     58  1.11  mycroft #include <sys/ucred.h>
     59   1.1      cgd #include <sys/mount.h>
     60   1.1      cgd #include <sys/socket.h>
     61   1.1      cgd #include <sys/socketvar.h>
     62  1.11  mycroft 
     63   1.1      cgd #include <rpc/rpc.h>
     64   1.1      cgd #include <rpc/pmap_clnt.h>
     65   1.1      cgd #include <rpc/pmap_prot.h>
     66  1.11  mycroft 
     67  1.11  mycroft #ifdef ISO
     68  1.11  mycroft #include <netiso/iso.h>
     69  1.11  mycroft #endif
     70   1.1      cgd #include <nfs/rpcv2.h>
     71  1.18     fvdl #include <nfs/nfsproto.h>
     72  1.11  mycroft #include <nfs/nfs.h>
     73  1.11  mycroft 
     74  1.18     fvdl #ifdef NFSKERB
     75  1.11  mycroft #include <kerberosIV/des.h>
     76  1.11  mycroft #include <kerberosIV/krb.h>
     77  1.11  mycroft #endif
     78  1.11  mycroft 
     79  1.11  mycroft #include <err.h>
     80  1.11  mycroft #include <errno.h>
     81  1.11  mycroft #include <fcntl.h>
     82  1.11  mycroft #include <grp.h>
     83  1.11  mycroft #include <pwd.h>
     84  1.11  mycroft #include <signal.h>
     85  1.11  mycroft #include <stdio.h>
     86  1.11  mycroft #include <stdlib.h>
     87  1.25    perry #include <string.h>
     88  1.19  mycroft #include <syslog.h>
     89  1.11  mycroft #include <unistd.h>
     90   1.1      cgd 
     91   1.1      cgd /* Global defs */
     92   1.1      cgd #ifdef DEBUG
     93   1.1      cgd #define	syslog(e, s)	fprintf(stderr,(s))
     94   1.1      cgd int	debug = 1;
     95   1.1      cgd #else
     96   1.1      cgd int	debug = 0;
     97   1.1      cgd #endif
     98  1.11  mycroft 
     99  1.11  mycroft struct	nfsd_srvargs nsd;
    100  1.11  mycroft 
    101  1.18     fvdl #ifdef NFSKERB
    102  1.11  mycroft char		lnam[ANAME_SZ];
    103  1.11  mycroft KTEXT_ST	kt;
    104  1.18     fvdl AUTH_DAT	kauth;
    105  1.11  mycroft char		inst[INST_SZ];
    106  1.18     fvdl struct nfsrpc_fullblock kin, kout;
    107  1.18     fvdl struct nfsrpc_fullverf kverf;
    108  1.18     fvdl NFSKERBKEY_T	kivec;
    109  1.18     fvdl struct timeval	ktv;
    110  1.18     fvdl NFSKERBKEYSCHED_T kerb_keysched;
    111  1.11  mycroft #endif
    112  1.11  mycroft 
    113  1.23    lukem int	main __P((int, char **));
    114  1.11  mycroft void	nonfs __P((int));
    115  1.11  mycroft void	reapchild __P((int));
    116  1.11  mycroft void	usage __P((void));
    117   1.1      cgd 
    118   1.1      cgd /*
    119   1.1      cgd  * Nfs server daemon mostly just a user context for nfssvc()
    120  1.11  mycroft  *
    121   1.1      cgd  * 1 - do file descriptor and signal cleanup
    122  1.11  mycroft  * 2 - fork the nfsd(s)
    123  1.11  mycroft  * 3 - create server socket(s)
    124  1.11  mycroft  * 4 - register socket with portmap
    125  1.11  mycroft  *
    126  1.11  mycroft  * For connectionless protocols, just pass the socket into the kernel via.
    127  1.11  mycroft  * nfssvc().
    128  1.11  mycroft  * For connection based sockets, loop doing accepts. When you get a new
    129  1.11  mycroft  * socket from accept, pass the msgsock into the kernel via. nfssvc().
    130   1.1      cgd  * The arguments are:
    131  1.11  mycroft  *	-c - support iso cltp clients
    132  1.11  mycroft  *	-r - reregister with portmapper
    133  1.11  mycroft  *	-t - support tcp nfs clients
    134  1.11  mycroft  *	-u - support udp nfs clients
    135  1.11  mycroft  * followed by "n" which is the number of nfsds' to fork off
    136   1.1      cgd  */
    137  1.11  mycroft int
    138  1.23    lukem main(argc, argv)
    139   1.1      cgd 	int argc;
    140  1.23    lukem 	char *argv[];
    141   1.1      cgd {
    142  1.11  mycroft 	struct nfsd_args nfsdargs;
    143  1.11  mycroft 	struct sockaddr_in inetaddr, inetpeer;
    144  1.11  mycroft #ifdef ISO
    145  1.11  mycroft 	struct sockaddr_iso isoaddr, isopeer;
    146  1.11  mycroft #endif
    147  1.11  mycroft 	fd_set ready, sockbits;
    148  1.11  mycroft 	int ch, cltpflag, connect_type_cnt, i, len, maxsock, msgsock;
    149  1.11  mycroft 	int nfsdcnt, nfssvc_flag, on, reregister, sock, tcpflag, tcpsock;
    150  1.23    lukem 	int tp4cnt, tp4flag, tpipcnt, tpipflag, udpflag;
    151  1.23    lukem #ifdef NFSKERB
    152  1.23    lukem 	struct group *grp;
    153  1.23    lukem 	struct passwd *pwd;
    154  1.23    lukem 	struct ucred *cr;
    155  1.23    lukem 	struct timeval ktv;
    156  1.23    lukem 	int tp4sock, tpipsock;
    157  1.11  mycroft 	char *cp, **cpp;
    158  1.23    lukem #endif
    159  1.11  mycroft 
    160  1.11  mycroft #define	MAXNFSDCNT	20
    161  1.11  mycroft #define	DEFNFSDCNT	 4
    162  1.11  mycroft 	nfsdcnt = DEFNFSDCNT;
    163  1.11  mycroft 	cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
    164  1.11  mycroft 	tpipflag = udpflag = 0;
    165  1.23    lukem 	maxsock = tcpsock = 0;
    166  1.11  mycroft #ifdef ISO
    167  1.11  mycroft #define	GETOPT	"cn:rtu"
    168  1.11  mycroft #define	USAGE	"[-crtu] [-n num_servers]"
    169  1.11  mycroft #else
    170  1.11  mycroft #define	GETOPT	"n:rtu"
    171  1.11  mycroft #define	USAGE	"[-rtu] [-n num_servers]"
    172  1.11  mycroft #endif
    173  1.22   kleink 	while ((ch = getopt(argc, argv, GETOPT)) != -1)
    174  1.11  mycroft 		switch (ch) {
    175  1.11  mycroft 		case 'n':
    176  1.11  mycroft 			nfsdcnt = atoi(optarg);
    177  1.11  mycroft 			if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
    178  1.21     fvdl 				warnx("nfsd count %d; reset to %d", nfsdcnt, DEFNFSDCNT);
    179  1.11  mycroft 				nfsdcnt = DEFNFSDCNT;
    180  1.11  mycroft 			}
    181  1.11  mycroft 			break;
    182   1.1      cgd 		case 'r':
    183  1.11  mycroft 			reregister = 1;
    184   1.1      cgd 			break;
    185   1.1      cgd 		case 't':
    186  1.11  mycroft 			tcpflag = 1;
    187   1.1      cgd 			break;
    188   1.1      cgd 		case 'u':
    189  1.11  mycroft 			udpflag = 1;
    190  1.11  mycroft 			break;
    191  1.11  mycroft #ifdef ISO
    192  1.11  mycroft 		case 'c':
    193  1.11  mycroft 			cltpflag = 1;
    194   1.1      cgd 			break;
    195  1.11  mycroft #ifdef notyet
    196  1.11  mycroft 		case 'i':
    197  1.11  mycroft 			tp4cnt = 1;
    198  1.11  mycroft 			break;
    199  1.11  mycroft 		case 'p':
    200  1.11  mycroft 			tpipcnt = 1;
    201  1.11  mycroft 			break;
    202  1.11  mycroft #endif /* notyet */
    203  1.11  mycroft #endif /* ISO */
    204   1.1      cgd 		default:
    205   1.1      cgd 		case '?':
    206   1.1      cgd 			usage();
    207  1.11  mycroft 		};
    208  1.11  mycroft 	argv += optind;
    209  1.11  mycroft 	argc -= optind;
    210   1.1      cgd 
    211   1.1      cgd 	/*
    212  1.11  mycroft 	 * XXX
    213  1.11  mycroft 	 * Backward compatibility, trailing number is the count of daemons.
    214   1.1      cgd 	 */
    215  1.11  mycroft 	if (argc > 1)
    216  1.11  mycroft 		usage();
    217  1.11  mycroft 	if (argc == 1) {
    218  1.11  mycroft 		nfsdcnt = atoi(argv[0]);
    219  1.11  mycroft 		if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
    220  1.21     fvdl 			warnx("nfsd count %d; reset to %d", nfsdcnt, DEFNFSDCNT);
    221  1.11  mycroft 			nfsdcnt = DEFNFSDCNT;
    222  1.11  mycroft 		}
    223   1.1      cgd 	}
    224   1.1      cgd 
    225   1.1      cgd 	if (debug == 0) {
    226   1.1      cgd 		daemon(0, 0);
    227  1.11  mycroft 		(void)signal(SIGHUP, SIG_IGN);
    228  1.11  mycroft 		(void)signal(SIGINT, SIG_IGN);
    229  1.11  mycroft 		(void)signal(SIGQUIT, SIG_IGN);
    230  1.11  mycroft 		(void)signal(SIGSYS, nonfs);
    231  1.11  mycroft 		(void)signal(SIGTERM, SIG_IGN);
    232   1.1      cgd 	}
    233  1.11  mycroft 	(void)signal(SIGCHLD, reapchild);
    234  1.11  mycroft 
    235  1.11  mycroft 	if (reregister) {
    236  1.11  mycroft 		if (udpflag &&
    237  1.18     fvdl 		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
    238  1.18     fvdl 		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)))
    239  1.11  mycroft 			err(1, "can't register with portmap for UDP.");
    240  1.11  mycroft 		if (tcpflag &&
    241  1.18     fvdl 		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
    242  1.18     fvdl 		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)))
    243  1.11  mycroft 			err(1, "can't register with portmap for TCP.");
    244  1.11  mycroft 		exit(0);
    245   1.8      cgd 	}
    246  1.11  mycroft 	openlog("nfsd:", LOG_PID, LOG_DAEMON);
    247  1.11  mycroft 
    248  1.11  mycroft 	for (i = 0; i < nfsdcnt; i++) {
    249  1.11  mycroft 		switch (fork()) {
    250  1.11  mycroft 		case -1:
    251  1.11  mycroft 			syslog(LOG_ERR, "fork: %m");
    252  1.11  mycroft 			exit (1);
    253  1.11  mycroft 		case 0:
    254  1.11  mycroft 			break;
    255  1.11  mycroft 		default:
    256  1.11  mycroft 			continue;
    257   1.1      cgd 		}
    258  1.11  mycroft 
    259  1.13  mycroft 		setproctitle("server");
    260  1.11  mycroft 		nfssvc_flag = NFSSVC_NFSD;
    261  1.11  mycroft 		nsd.nsd_nfsd = NULL;
    262  1.18     fvdl #ifdef NFSKERB
    263  1.18     fvdl 		if (sizeof (struct nfsrpc_fullverf) != RPCX_FULLVERF ||
    264  1.18     fvdl 		    sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK)
    265  1.18     fvdl 		    syslog(LOG_ERR, "Yikes NFSKERB structs not packed!");
    266  1.18     fvdl 		nsd.nsd_authstr = (u_char *)&kt;
    267  1.18     fvdl 		nsd.nsd_authlen = sizeof (kt);
    268  1.18     fvdl 		nsd.nsd_verfstr = (u_char *)&kverf;
    269  1.18     fvdl 		nsd.nsd_verflen = sizeof (kverf);
    270  1.11  mycroft #endif
    271  1.11  mycroft 		while (nfssvc(nfssvc_flag, &nsd) < 0) {
    272  1.11  mycroft 			if (errno != ENEEDAUTH) {
    273  1.11  mycroft 				syslog(LOG_ERR, "nfssvc: %m");
    274  1.11  mycroft 				exit(1);
    275  1.11  mycroft 			}
    276  1.11  mycroft 			nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
    277  1.18     fvdl #ifdef NFSKERB
    278  1.18     fvdl 			/*
    279  1.18     fvdl 			 * Get the Kerberos ticket out of the authenticator
    280  1.18     fvdl 			 * verify it and convert the principal name to a user
    281  1.18     fvdl 			 * name. The user name is then converted to a set of
    282  1.18     fvdl 			 * user credentials via the password and group file.
    283  1.18     fvdl 			 * Finally, decrypt the timestamp and validate it.
    284  1.18     fvdl 			 * For more info see the IETF Draft "Authentication
    285  1.18     fvdl 			 * in ONC RPC".
    286  1.18     fvdl 			 */
    287  1.18     fvdl 			kt.length = ntohl(kt.length);
    288  1.18     fvdl 			if (gettimeofday(&ktv, (struct timezone *)0) == 0 &&
    289  1.18     fvdl 			    kt.length > 0 && kt.length <=
    290  1.18     fvdl 			    (RPCAUTH_MAXSIZ - 3 * NFSX_UNSIGNED)) {
    291  1.18     fvdl 			    kin.w1 = NFS_KERBW1(kt);
    292  1.18     fvdl 			    kt.mbz = 0;
    293  1.18     fvdl 			    (void)strcpy(inst, "*");
    294  1.18     fvdl 			    if (krb_rd_req(&kt, NFS_KERBSRV,
    295  1.18     fvdl 				inst, nsd.nsd_haddr, &kauth, "") == RD_AP_OK &&
    296  1.18     fvdl 				krb_kntoln(&kauth, lnam) == KSUCCESS &&
    297  1.18     fvdl 				(pwd = getpwnam(lnam)) != NULL) {
    298  1.11  mycroft 				cr = &nsd.nsd_cr;
    299  1.11  mycroft 				cr->cr_uid = pwd->pw_uid;
    300  1.11  mycroft 				cr->cr_groups[0] = pwd->pw_gid;
    301  1.11  mycroft 				cr->cr_ngroups = 1;
    302  1.11  mycroft 				setgrent();
    303  1.11  mycroft 				while ((grp = getgrent()) != NULL) {
    304  1.11  mycroft 					if (grp->gr_gid == cr->cr_groups[0])
    305  1.11  mycroft 						continue;
    306  1.11  mycroft 					for (cpp = grp->gr_mem;
    307  1.11  mycroft 					    *cpp != NULL; ++cpp)
    308  1.11  mycroft 						if (!strcmp(*cpp, lnam))
    309  1.11  mycroft 							break;
    310  1.11  mycroft 					if (*cpp == NULL)
    311  1.11  mycroft 						continue;
    312  1.11  mycroft 					cr->cr_groups[cr->cr_ngroups++]
    313  1.11  mycroft 					    = grp->gr_gid;
    314  1.11  mycroft 					if (cr->cr_ngroups == NGROUPS)
    315  1.11  mycroft 						break;
    316  1.11  mycroft 				}
    317  1.11  mycroft 				endgrent();
    318  1.18     fvdl 
    319  1.18     fvdl 				/*
    320  1.18     fvdl 				 * Get the timestamp verifier out of the
    321  1.18     fvdl 				 * authenticator and verifier strings.
    322  1.18     fvdl 				 */
    323  1.18     fvdl 				kin.t1 = kverf.t1;
    324  1.18     fvdl 				kin.t2 = kverf.t2;
    325  1.18     fvdl 				kin.w2 = kverf.w2;
    326  1.24    lukem 				memset((caddr_t)kivec, 0, sizeof (kivec));
    327  1.24    lukem 				memmove((caddr_t)nsd.nsd_key,
    328  1.24    lukem 				    (caddr_t)kauth.session,
    329  1.24    lukem 				    sizeof(kauth.session));
    330  1.18     fvdl 
    331  1.18     fvdl 				/*
    332  1.18     fvdl 				 * Decrypt the timestamp verifier in CBC mode.
    333  1.18     fvdl 				 */
    334  1.18     fvdl 				XXX
    335  1.18     fvdl 
    336  1.18     fvdl 				/*
    337  1.18     fvdl 				 * Validate the timestamp verifier, to
    338  1.18     fvdl 				 * check that the session key is ok.
    339  1.18     fvdl 				 */
    340  1.18     fvdl 				nsd.nsd_timestamp.tv_sec = ntohl(kout.t1);
    341  1.18     fvdl 				nsd.nsd_timestamp.tv_usec = ntohl(kout.t2);
    342  1.18     fvdl 				nsd.nsd_ttl = ntohl(kout.w1);
    343  1.18     fvdl 				if ((nsd.nsd_ttl - 1) == ntohl(kout.w2))
    344  1.18     fvdl 				    nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHIN;
    345  1.11  mycroft 			}
    346  1.18     fvdl #endif /* NFSKERB */
    347   1.1      cgd 		}
    348   1.1      cgd 		exit(0);
    349   1.1      cgd 	}
    350  1.11  mycroft 
    351  1.11  mycroft 	/* If we are serving udp, set up the socket. */
    352   1.1      cgd 	if (udpflag) {
    353   1.1      cgd 		if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    354  1.11  mycroft 			syslog(LOG_ERR, "can't create udp socket");
    355   1.1      cgd 			exit(1);
    356   1.1      cgd 		}
    357  1.11  mycroft 		inetaddr.sin_family = AF_INET;
    358  1.11  mycroft 		inetaddr.sin_addr.s_addr = INADDR_ANY;
    359  1.11  mycroft 		inetaddr.sin_port = htons(NFS_PORT);
    360  1.11  mycroft 		inetaddr.sin_len = sizeof(inetaddr);
    361  1.20  mycroft 		memset(inetaddr.sin_zero, 0, sizeof(inetaddr.sin_zero));
    362  1.11  mycroft 		if (bind(sock,
    363  1.11  mycroft 		    (struct sockaddr *)&inetaddr, sizeof(inetaddr)) < 0) {
    364  1.11  mycroft 			syslog(LOG_ERR, "can't bind udp addr");
    365   1.1      cgd 			exit(1);
    366   1.1      cgd 		}
    367  1.18     fvdl 		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
    368  1.18     fvdl 		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
    369  1.11  mycroft 			syslog(LOG_ERR, "can't register with udp portmap");
    370  1.11  mycroft 			exit(1);
    371  1.11  mycroft 		}
    372  1.11  mycroft 		nfsdargs.sock = sock;
    373  1.11  mycroft 		nfsdargs.name = NULL;
    374  1.11  mycroft 		nfsdargs.namelen = 0;
    375  1.11  mycroft 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    376  1.11  mycroft 			syslog(LOG_ERR, "can't Add UDP socket");
    377  1.11  mycroft 			exit(1);
    378  1.11  mycroft 		}
    379  1.11  mycroft 		(void)close(sock);
    380  1.11  mycroft 	}
    381  1.11  mycroft 
    382  1.11  mycroft #ifdef ISO
    383  1.11  mycroft 	/* If we are serving cltp, set up the socket. */
    384  1.11  mycroft 	if (cltpflag) {
    385  1.11  mycroft 		if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
    386  1.11  mycroft 			syslog(LOG_ERR, "can't create cltp socket");
    387  1.11  mycroft 			exit(1);
    388  1.11  mycroft 		}
    389  1.11  mycroft 		memset(&isoaddr, 0, sizeof(isoaddr));
    390  1.11  mycroft 		isoaddr.siso_family = AF_ISO;
    391  1.11  mycroft 		isoaddr.siso_tlen = 2;
    392  1.11  mycroft 		cp = TSEL(&isoaddr);
    393  1.11  mycroft 		*cp++ = (NFS_PORT >> 8);
    394  1.11  mycroft 		*cp = (NFS_PORT & 0xff);
    395  1.11  mycroft 		isoaddr.siso_len = sizeof(isoaddr);
    396  1.11  mycroft 		if (bind(sock,
    397  1.11  mycroft 		    (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
    398  1.11  mycroft 			syslog(LOG_ERR, "can't bind cltp addr");
    399   1.1      cgd 			exit(1);
    400   1.1      cgd 		}
    401  1.11  mycroft #ifdef notyet
    402   1.1      cgd 		/*
    403  1.11  mycroft 		 * XXX
    404  1.11  mycroft 		 * Someday this should probably use "rpcbind", the son of
    405  1.11  mycroft 		 * portmap.
    406   1.1      cgd 		 */
    407  1.11  mycroft 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
    408  1.11  mycroft 			syslog(LOG_ERR, "can't register with udp portmap");
    409  1.11  mycroft 			exit(1);
    410  1.11  mycroft 		}
    411  1.11  mycroft #endif /* notyet */
    412  1.11  mycroft 		nfsdargs.sock = sock;
    413  1.11  mycroft 		nfsdargs.name = NULL;
    414  1.11  mycroft 		nfsdargs.namelen = 0;
    415  1.11  mycroft 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    416  1.11  mycroft 			syslog(LOG_ERR, "can't add UDP socket");
    417  1.11  mycroft 			exit(1);
    418  1.11  mycroft 		}
    419   1.1      cgd 		close(sock);
    420   1.1      cgd 	}
    421  1.11  mycroft #endif /* ISO */
    422   1.1      cgd 
    423  1.11  mycroft 	/* Now set up the master server socket waiting for tcp connections. */
    424  1.11  mycroft 	on = 1;
    425  1.11  mycroft 	FD_ZERO(&sockbits);
    426  1.11  mycroft 	connect_type_cnt = 0;
    427   1.1      cgd 	if (tcpflag) {
    428  1.11  mycroft 		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    429  1.11  mycroft 			syslog(LOG_ERR, "can't create tcp socket");
    430  1.11  mycroft 			exit(1);
    431  1.11  mycroft 		}
    432  1.11  mycroft 		if (setsockopt(tcpsock,
    433  1.11  mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    434  1.11  mycroft 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    435  1.11  mycroft 		inetaddr.sin_family = AF_INET;
    436  1.11  mycroft 		inetaddr.sin_addr.s_addr = INADDR_ANY;
    437  1.11  mycroft 		inetaddr.sin_port = htons(NFS_PORT);
    438  1.11  mycroft 		inetaddr.sin_len = sizeof(inetaddr);
    439  1.20  mycroft 		memset(inetaddr.sin_zero, 0, sizeof(inetaddr.sin_zero));
    440  1.11  mycroft 		if (bind(tcpsock,
    441  1.11  mycroft 		    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
    442  1.11  mycroft 			syslog(LOG_ERR, "can't bind tcp addr");
    443  1.11  mycroft 			exit(1);
    444  1.11  mycroft 		}
    445  1.11  mycroft 		if (listen(tcpsock, 5) < 0) {
    446  1.11  mycroft 			syslog(LOG_ERR, "listen failed");
    447  1.11  mycroft 			exit(1);
    448  1.11  mycroft 		}
    449  1.18     fvdl 		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
    450  1.18     fvdl 		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
    451  1.11  mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    452  1.11  mycroft 			exit(1);
    453  1.11  mycroft 		}
    454  1.11  mycroft 		FD_SET(tcpsock, &sockbits);
    455  1.11  mycroft 		maxsock = tcpsock;
    456  1.11  mycroft 		connect_type_cnt++;
    457  1.11  mycroft 	}
    458   1.1      cgd 
    459  1.11  mycroft #ifdef notyet
    460  1.11  mycroft 	/* Now set up the master server socket waiting for tp4 connections. */
    461  1.11  mycroft 	if (tp4flag) {
    462  1.11  mycroft 		if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
    463  1.11  mycroft 			syslog(LOG_ERR, "can't create tp4 socket");
    464   1.1      cgd 			exit(1);
    465   1.1      cgd 		}
    466  1.11  mycroft 		if (setsockopt(tp4sock,
    467  1.11  mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    468   1.1      cgd 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    469  1.11  mycroft 		memset(&isoaddr, 0, sizeof(isoaddr));
    470  1.11  mycroft 		isoaddr.siso_family = AF_ISO;
    471  1.11  mycroft 		isoaddr.siso_tlen = 2;
    472  1.11  mycroft 		cp = TSEL(&isoaddr);
    473  1.11  mycroft 		*cp++ = (NFS_PORT >> 8);
    474  1.11  mycroft 		*cp = (NFS_PORT & 0xff);
    475  1.11  mycroft 		isoaddr.siso_len = sizeof(isoaddr);
    476  1.11  mycroft 		if (bind(tp4sock,
    477  1.12  mycroft 		    (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
    478  1.11  mycroft 			syslog(LOG_ERR, "can't bind tp4 addr");
    479   1.1      cgd 			exit(1);
    480   1.1      cgd 		}
    481  1.11  mycroft 		if (listen(tp4sock, 5) < 0) {
    482  1.11  mycroft 			syslog(LOG_ERR, "listen failed");
    483   1.1      cgd 			exit(1);
    484   1.1      cgd 		}
    485  1.11  mycroft 		/*
    486  1.11  mycroft 		 * XXX
    487  1.11  mycroft 		 * Someday this should probably use "rpcbind", the son of
    488  1.11  mycroft 		 * portmap.
    489  1.11  mycroft 		 */
    490   1.1      cgd 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
    491  1.11  mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    492  1.11  mycroft 			exit(1);
    493  1.11  mycroft 		}
    494  1.11  mycroft 		FD_SET(tp4sock, &sockbits);
    495  1.11  mycroft 		maxsock = tp4sock;
    496  1.11  mycroft 		connect_type_cnt++;
    497  1.11  mycroft 	}
    498  1.11  mycroft 
    499  1.11  mycroft 	/* Now set up the master server socket waiting for tpip connections. */
    500  1.11  mycroft 	if (tpipflag) {
    501  1.11  mycroft 		if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
    502  1.11  mycroft 			syslog(LOG_ERR, "can't create tpip socket");
    503  1.11  mycroft 			exit(1);
    504  1.11  mycroft 		}
    505  1.11  mycroft 		if (setsockopt(tpipsock,
    506  1.11  mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    507  1.11  mycroft 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    508  1.11  mycroft 		inetaddr.sin_family = AF_INET;
    509  1.11  mycroft 		inetaddr.sin_addr.s_addr = INADDR_ANY;
    510  1.11  mycroft 		inetaddr.sin_port = htons(NFS_PORT);
    511  1.11  mycroft 		inetaddr.sin_len = sizeof(inetaddr);
    512  1.20  mycroft 		memset(inetaddr.sin_zero, 0, sizeof(inetaddr.sin_zero));
    513  1.11  mycroft 		if (bind(tpipsock,
    514  1.11  mycroft 		    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
    515  1.11  mycroft 			syslog(LOG_ERR, "can't bind tcp addr");
    516  1.11  mycroft 			exit(1);
    517  1.11  mycroft 		}
    518  1.11  mycroft 		if (listen(tpipsock, 5) < 0) {
    519  1.11  mycroft 			syslog(LOG_ERR, "listen failed");
    520   1.1      cgd 			exit(1);
    521   1.1      cgd 		}
    522   1.1      cgd 		/*
    523  1.11  mycroft 		 * XXX
    524  1.11  mycroft 		 * Someday this should probably use "rpcbind", the son of
    525  1.11  mycroft 		 * portmap.
    526   1.1      cgd 		 */
    527  1.11  mycroft 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
    528  1.11  mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    529  1.11  mycroft 			exit(1);
    530  1.11  mycroft 		}
    531  1.11  mycroft 		FD_SET(tpipsock, &sockbits);
    532  1.11  mycroft 		maxsock = tpipsock;
    533  1.11  mycroft 		connect_type_cnt++;
    534  1.11  mycroft 	}
    535  1.11  mycroft #endif /* notyet */
    536  1.11  mycroft 
    537  1.11  mycroft 	if (connect_type_cnt == 0)
    538  1.11  mycroft 		exit(0);
    539  1.11  mycroft 
    540  1.13  mycroft 	setproctitle("master");
    541  1.11  mycroft 
    542  1.11  mycroft 	/*
    543  1.11  mycroft 	 * Loop forever accepting connections and passing the sockets
    544  1.11  mycroft 	 * into the kernel for the mounts.
    545  1.11  mycroft 	 */
    546  1.11  mycroft 	for (;;) {
    547  1.11  mycroft 		ready = sockbits;
    548  1.11  mycroft 		if (connect_type_cnt > 1) {
    549  1.11  mycroft 			if (select(maxsock + 1,
    550  1.11  mycroft 			    &ready, NULL, NULL, NULL) < 1) {
    551  1.11  mycroft 				syslog(LOG_ERR, "select failed: %m");
    552  1.11  mycroft 				exit(1);
    553  1.11  mycroft 			}
    554  1.11  mycroft 		}
    555  1.11  mycroft 		if (tcpflag && FD_ISSET(tcpsock, &ready)) {
    556  1.11  mycroft 			len = sizeof(inetpeer);
    557  1.11  mycroft 			if ((msgsock = accept(tcpsock,
    558  1.11  mycroft 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
    559  1.11  mycroft 				syslog(LOG_ERR, "accept failed: %m");
    560   1.1      cgd 				exit(1);
    561   1.1      cgd 			}
    562  1.11  mycroft 			memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
    563  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    564  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    565  1.11  mycroft 				syslog(LOG_ERR,
    566  1.11  mycroft 				    "setsockopt SO_KEEPALIVE: %m");
    567  1.11  mycroft 			nfsdargs.sock = msgsock;
    568  1.11  mycroft 			nfsdargs.name = (caddr_t)&inetpeer;
    569  1.11  mycroft 			nfsdargs.namelen = sizeof(inetpeer);
    570  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    571  1.11  mycroft 			(void)close(msgsock);
    572  1.11  mycroft 		}
    573  1.11  mycroft #ifdef notyet
    574  1.11  mycroft 		if (tp4flag && FD_ISSET(tp4sock, &ready)) {
    575  1.11  mycroft 			len = sizeof(isopeer);
    576  1.11  mycroft 			if ((msgsock = accept(tp4sock,
    577  1.11  mycroft 			    (struct sockaddr *)&isopeer, &len)) < 0) {
    578  1.11  mycroft 				syslog(LOG_ERR, "accept failed: %m");
    579  1.11  mycroft 				exit(1);
    580   1.1      cgd 			}
    581  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    582  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    583  1.11  mycroft 				syslog(LOG_ERR,
    584  1.11  mycroft 				    "setsockopt SO_KEEPALIVE: %m");
    585  1.11  mycroft 			nfsdargs.sock = msgsock;
    586  1.11  mycroft 			nfsdargs.name = (caddr_t)&isopeer;
    587  1.11  mycroft 			nfsdargs.namelen = len;
    588  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    589  1.11  mycroft 			(void)close(msgsock);
    590  1.11  mycroft 		}
    591  1.11  mycroft 		if (tpipflag && FD_ISSET(tpipsock, &ready)) {
    592  1.11  mycroft 			len = sizeof(inetpeer);
    593  1.11  mycroft 			if ((msgsock = accept(tpipsock,
    594  1.11  mycroft 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
    595  1.11  mycroft 				syslog(LOG_ERR, "Accept failed: %m");
    596   1.1      cgd 				exit(1);
    597   1.1      cgd 			}
    598  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    599  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    600  1.11  mycroft 				syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
    601  1.11  mycroft 			nfsdargs.sock = msgsock;
    602  1.11  mycroft 			nfsdargs.name = (caddr_t)&inetpeer;
    603  1.11  mycroft 			nfsdargs.namelen = len;
    604  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    605  1.11  mycroft 			(void)close(msgsock);
    606   1.1      cgd 		}
    607  1.11  mycroft #endif /* notyet */
    608   1.1      cgd 	}
    609   1.1      cgd }
    610   1.1      cgd 
    611  1.11  mycroft void
    612   1.1      cgd usage()
    613   1.1      cgd {
    614  1.26      mrg 
    615  1.15  mycroft 	(void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
    616   1.1      cgd 	exit(1);
    617   1.1      cgd }
    618   1.1      cgd 
    619   1.1      cgd void
    620  1.11  mycroft nonfs(signo)
    621  1.11  mycroft 	int signo;
    622   1.1      cgd {
    623  1.15  mycroft 
    624  1.11  mycroft 	syslog(LOG_ERR, "missing system call: NFS not available.");
    625   1.1      cgd }
    626   1.4    glass 
    627  1.11  mycroft void
    628  1.11  mycroft reapchild(signo)
    629  1.11  mycroft 	int signo;
    630   1.4    glass {
    631  1.11  mycroft 
    632  1.18     fvdl 	while (wait3(NULL, WNOHANG, NULL) > 0);
    633   1.4    glass }
    634