Home | History | Annotate | Line # | Download | only in nfsd
nfsd.c revision 1.18
      1  1.18     fvdl /*	$NetBSD: nfsd.c,v 1.18 1996/02/18 11:58:25 fvdl 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.1      cgd #ifndef lint
     40  1.11  mycroft static char copyright[] =
     41  1.11  mycroft "@(#) Copyright (c) 1989, 1993, 1994\n\
     42  1.11  mycroft 	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.18     fvdl static char rcsid[] = "$NetBSD: nfsd.c,v 1.18 1996/02/18 11:58:25 fvdl 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.17      jtc #include <syslog.h>
     55   1.1      cgd #include <sys/ioctl.h>
     56   1.1      cgd #include <sys/stat.h>
     57   1.1      cgd #include <sys/wait.h>
     58  1.11  mycroft #include <sys/uio.h>
     59  1.11  mycroft #include <sys/ucred.h>
     60   1.1      cgd #include <sys/mount.h>
     61   1.1      cgd #include <sys/socket.h>
     62   1.1      cgd #include <sys/socketvar.h>
     63  1.11  mycroft 
     64   1.1      cgd #include <rpc/rpc.h>
     65   1.1      cgd #include <rpc/pmap_clnt.h>
     66   1.1      cgd #include <rpc/pmap_prot.h>
     67  1.11  mycroft 
     68  1.11  mycroft #ifdef ISO
     69  1.11  mycroft #include <netiso/iso.h>
     70  1.11  mycroft #endif
     71   1.1      cgd #include <nfs/rpcv2.h>
     72  1.18     fvdl #include <nfs/nfsproto.h>
     73  1.11  mycroft #include <nfs/nfs.h>
     74  1.11  mycroft 
     75  1.18     fvdl #ifdef NFSKERB
     76  1.11  mycroft #include <kerberosIV/des.h>
     77  1.11  mycroft #include <kerberosIV/krb.h>
     78  1.11  mycroft #endif
     79  1.11  mycroft 
     80  1.11  mycroft #include <err.h>
     81  1.11  mycroft #include <errno.h>
     82  1.11  mycroft #include <fcntl.h>
     83  1.11  mycroft #include <grp.h>
     84  1.11  mycroft #include <pwd.h>
     85  1.11  mycroft #include <signal.h>
     86  1.11  mycroft #include <stdio.h>
     87  1.11  mycroft #include <stdlib.h>
     88  1.11  mycroft #include <strings.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.11  mycroft void	nonfs __P((int));
    114  1.11  mycroft void	reapchild __P((int));
    115  1.11  mycroft void	usage __P((void));
    116   1.1      cgd 
    117   1.1      cgd /*
    118   1.1      cgd  * Nfs server daemon mostly just a user context for nfssvc()
    119  1.11  mycroft  *
    120   1.1      cgd  * 1 - do file descriptor and signal cleanup
    121  1.11  mycroft  * 2 - fork the nfsd(s)
    122  1.11  mycroft  * 3 - create server socket(s)
    123  1.11  mycroft  * 4 - register socket with portmap
    124  1.11  mycroft  *
    125  1.11  mycroft  * For connectionless protocols, just pass the socket into the kernel via.
    126  1.11  mycroft  * nfssvc().
    127  1.11  mycroft  * For connection based sockets, loop doing accepts. When you get a new
    128  1.11  mycroft  * socket from accept, pass the msgsock into the kernel via. nfssvc().
    129   1.1      cgd  * The arguments are:
    130  1.11  mycroft  *	-c - support iso cltp clients
    131  1.11  mycroft  *	-r - reregister with portmapper
    132  1.11  mycroft  *	-t - support tcp nfs clients
    133  1.11  mycroft  *	-u - support udp nfs clients
    134  1.11  mycroft  * followed by "n" which is the number of nfsds' to fork off
    135   1.1      cgd  */
    136  1.11  mycroft int
    137   1.1      cgd main(argc, argv, envp)
    138   1.1      cgd 	int argc;
    139   1.1      cgd 	char *argv[], *envp[];
    140   1.1      cgd {
    141   1.1      cgd 	extern int optind;
    142  1.11  mycroft 	struct group *grp;
    143  1.11  mycroft 	struct nfsd_args nfsdargs;
    144  1.11  mycroft 	struct passwd *pwd;
    145  1.11  mycroft 	struct ucred *cr;
    146  1.11  mycroft 	struct sockaddr_in inetaddr, inetpeer;
    147  1.11  mycroft #ifdef ISO
    148  1.11  mycroft 	struct sockaddr_iso isoaddr, isopeer;
    149  1.11  mycroft #endif
    150  1.18     fvdl 	struct timeval ktv;
    151  1.11  mycroft 	fd_set ready, sockbits;
    152  1.11  mycroft 	int ch, cltpflag, connect_type_cnt, i, len, maxsock, msgsock;
    153  1.11  mycroft 	int nfsdcnt, nfssvc_flag, on, reregister, sock, tcpflag, tcpsock;
    154  1.11  mycroft 	int tp4cnt, tp4flag, tp4sock, tpipcnt, tpipflag, tpipsock, udpflag;
    155  1.11  mycroft 	char *cp, **cpp;
    156  1.11  mycroft 
    157  1.11  mycroft #define	MAXNFSDCNT	20
    158  1.11  mycroft #define	DEFNFSDCNT	 4
    159  1.11  mycroft 	nfsdcnt = DEFNFSDCNT;
    160  1.11  mycroft 	cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
    161  1.11  mycroft 	tpipflag = udpflag = 0;
    162  1.11  mycroft #ifdef ISO
    163  1.11  mycroft #define	GETOPT	"cn:rtu"
    164  1.11  mycroft #define	USAGE	"[-crtu] [-n num_servers]"
    165  1.11  mycroft #else
    166  1.11  mycroft #define	GETOPT	"n:rtu"
    167  1.11  mycroft #define	USAGE	"[-rtu] [-n num_servers]"
    168  1.11  mycroft #endif
    169  1.11  mycroft 	while ((ch = getopt(argc, argv, GETOPT)) != EOF)
    170  1.11  mycroft 		switch (ch) {
    171  1.11  mycroft 		case 'n':
    172  1.11  mycroft 			nfsdcnt = atoi(optarg);
    173  1.11  mycroft 			if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
    174  1.11  mycroft 				warnx("nfsd count %d; reset to %d", DEFNFSDCNT);
    175  1.11  mycroft 				nfsdcnt = DEFNFSDCNT;
    176  1.11  mycroft 			}
    177  1.11  mycroft 			break;
    178   1.1      cgd 		case 'r':
    179  1.11  mycroft 			reregister = 1;
    180   1.1      cgd 			break;
    181   1.1      cgd 		case 't':
    182  1.11  mycroft 			tcpflag = 1;
    183   1.1      cgd 			break;
    184   1.1      cgd 		case 'u':
    185  1.11  mycroft 			udpflag = 1;
    186  1.11  mycroft 			break;
    187  1.11  mycroft #ifdef ISO
    188  1.11  mycroft 		case 'c':
    189  1.11  mycroft 			cltpflag = 1;
    190   1.1      cgd 			break;
    191  1.11  mycroft #ifdef notyet
    192  1.11  mycroft 		case 'i':
    193  1.11  mycroft 			tp4cnt = 1;
    194  1.11  mycroft 			break;
    195  1.11  mycroft 		case 'p':
    196  1.11  mycroft 			tpipcnt = 1;
    197  1.11  mycroft 			break;
    198  1.11  mycroft #endif /* notyet */
    199  1.11  mycroft #endif /* ISO */
    200   1.1      cgd 		default:
    201   1.1      cgd 		case '?':
    202   1.1      cgd 			usage();
    203  1.11  mycroft 		};
    204  1.11  mycroft 	argv += optind;
    205  1.11  mycroft 	argc -= optind;
    206   1.1      cgd 
    207   1.1      cgd 	/*
    208  1.11  mycroft 	 * XXX
    209  1.11  mycroft 	 * Backward compatibility, trailing number is the count of daemons.
    210   1.1      cgd 	 */
    211  1.11  mycroft 	if (argc > 1)
    212  1.11  mycroft 		usage();
    213  1.11  mycroft 	if (argc == 1) {
    214  1.11  mycroft 		nfsdcnt = atoi(argv[0]);
    215  1.11  mycroft 		if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
    216  1.11  mycroft 			warnx("nfsd count %d; reset to %d", DEFNFSDCNT);
    217  1.11  mycroft 			nfsdcnt = DEFNFSDCNT;
    218  1.11  mycroft 		}
    219   1.1      cgd 	}
    220   1.1      cgd 
    221   1.1      cgd 	if (debug == 0) {
    222   1.1      cgd 		daemon(0, 0);
    223  1.11  mycroft 		(void)signal(SIGHUP, SIG_IGN);
    224  1.11  mycroft 		(void)signal(SIGINT, SIG_IGN);
    225  1.11  mycroft 		(void)signal(SIGQUIT, SIG_IGN);
    226  1.11  mycroft 		(void)signal(SIGSYS, nonfs);
    227  1.11  mycroft 		(void)signal(SIGTERM, SIG_IGN);
    228   1.1      cgd 	}
    229  1.11  mycroft 	(void)signal(SIGCHLD, reapchild);
    230  1.11  mycroft 
    231  1.11  mycroft 	if (reregister) {
    232  1.11  mycroft 		if (udpflag &&
    233  1.18     fvdl 		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
    234  1.18     fvdl 		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)))
    235  1.11  mycroft 			err(1, "can't register with portmap for UDP.");
    236  1.11  mycroft 		if (tcpflag &&
    237  1.18     fvdl 		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
    238  1.18     fvdl 		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)))
    239  1.11  mycroft 			err(1, "can't register with portmap for TCP.");
    240  1.11  mycroft 		exit(0);
    241   1.8      cgd 	}
    242  1.11  mycroft 	openlog("nfsd:", LOG_PID, LOG_DAEMON);
    243  1.11  mycroft 
    244  1.11  mycroft 	for (i = 0; i < nfsdcnt; i++) {
    245  1.11  mycroft 		switch (fork()) {
    246  1.11  mycroft 		case -1:
    247  1.11  mycroft 			syslog(LOG_ERR, "fork: %m");
    248  1.11  mycroft 			exit (1);
    249  1.11  mycroft 		case 0:
    250  1.11  mycroft 			break;
    251  1.11  mycroft 		default:
    252  1.11  mycroft 			continue;
    253   1.1      cgd 		}
    254  1.11  mycroft 
    255  1.13  mycroft 		setproctitle("server");
    256  1.11  mycroft 		nfssvc_flag = NFSSVC_NFSD;
    257  1.11  mycroft 		nsd.nsd_nfsd = NULL;
    258  1.18     fvdl #ifdef NFSKERB
    259  1.18     fvdl 		if (sizeof (struct nfsrpc_fullverf) != RPCX_FULLVERF ||
    260  1.18     fvdl 		    sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK)
    261  1.18     fvdl 		    syslog(LOG_ERR, "Yikes NFSKERB structs not packed!");
    262  1.18     fvdl 		nsd.nsd_authstr = (u_char *)&kt;
    263  1.18     fvdl 		nsd.nsd_authlen = sizeof (kt);
    264  1.18     fvdl 		nsd.nsd_verfstr = (u_char *)&kverf;
    265  1.18     fvdl 		nsd.nsd_verflen = sizeof (kverf);
    266  1.11  mycroft #endif
    267  1.11  mycroft 		while (nfssvc(nfssvc_flag, &nsd) < 0) {
    268  1.11  mycroft 			if (errno != ENEEDAUTH) {
    269  1.11  mycroft 				syslog(LOG_ERR, "nfssvc: %m");
    270  1.11  mycroft 				exit(1);
    271  1.11  mycroft 			}
    272  1.11  mycroft 			nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
    273  1.18     fvdl #ifdef NFSKERB
    274  1.18     fvdl 			/*
    275  1.18     fvdl 			 * Get the Kerberos ticket out of the authenticator
    276  1.18     fvdl 			 * verify it and convert the principal name to a user
    277  1.18     fvdl 			 * name. The user name is then converted to a set of
    278  1.18     fvdl 			 * user credentials via the password and group file.
    279  1.18     fvdl 			 * Finally, decrypt the timestamp and validate it.
    280  1.18     fvdl 			 * For more info see the IETF Draft "Authentication
    281  1.18     fvdl 			 * in ONC RPC".
    282  1.18     fvdl 			 */
    283  1.18     fvdl 			kt.length = ntohl(kt.length);
    284  1.18     fvdl 			if (gettimeofday(&ktv, (struct timezone *)0) == 0 &&
    285  1.18     fvdl 			    kt.length > 0 && kt.length <=
    286  1.18     fvdl 			    (RPCAUTH_MAXSIZ - 3 * NFSX_UNSIGNED)) {
    287  1.18     fvdl 			    kin.w1 = NFS_KERBW1(kt);
    288  1.18     fvdl 			    kt.mbz = 0;
    289  1.18     fvdl 			    (void)strcpy(inst, "*");
    290  1.18     fvdl 			    if (krb_rd_req(&kt, NFS_KERBSRV,
    291  1.18     fvdl 				inst, nsd.nsd_haddr, &kauth, "") == RD_AP_OK &&
    292  1.18     fvdl 				krb_kntoln(&kauth, lnam) == KSUCCESS &&
    293  1.18     fvdl 				(pwd = getpwnam(lnam)) != NULL) {
    294  1.11  mycroft 				cr = &nsd.nsd_cr;
    295  1.11  mycroft 				cr->cr_uid = pwd->pw_uid;
    296  1.11  mycroft 				cr->cr_groups[0] = pwd->pw_gid;
    297  1.11  mycroft 				cr->cr_ngroups = 1;
    298  1.11  mycroft 				setgrent();
    299  1.11  mycroft 				while ((grp = getgrent()) != NULL) {
    300  1.11  mycroft 					if (grp->gr_gid == cr->cr_groups[0])
    301  1.11  mycroft 						continue;
    302  1.11  mycroft 					for (cpp = grp->gr_mem;
    303  1.11  mycroft 					    *cpp != NULL; ++cpp)
    304  1.11  mycroft 						if (!strcmp(*cpp, lnam))
    305  1.11  mycroft 							break;
    306  1.11  mycroft 					if (*cpp == NULL)
    307  1.11  mycroft 						continue;
    308  1.11  mycroft 					cr->cr_groups[cr->cr_ngroups++]
    309  1.11  mycroft 					    = grp->gr_gid;
    310  1.11  mycroft 					if (cr->cr_ngroups == NGROUPS)
    311  1.11  mycroft 						break;
    312  1.11  mycroft 				}
    313  1.11  mycroft 				endgrent();
    314  1.18     fvdl 
    315  1.18     fvdl 				/*
    316  1.18     fvdl 				 * Get the timestamp verifier out of the
    317  1.18     fvdl 				 * authenticator and verifier strings.
    318  1.18     fvdl 				 */
    319  1.18     fvdl 				kin.t1 = kverf.t1;
    320  1.18     fvdl 				kin.t2 = kverf.t2;
    321  1.18     fvdl 				kin.w2 = kverf.w2;
    322  1.18     fvdl 				bzero((caddr_t)kivec, sizeof (kivec));
    323  1.18     fvdl 				bcopy((caddr_t)kauth.session,
    324  1.18     fvdl 				    (caddr_t)nsd.nsd_key,sizeof(kauth.session));
    325  1.18     fvdl 
    326  1.18     fvdl 				/*
    327  1.18     fvdl 				 * Decrypt the timestamp verifier in CBC mode.
    328  1.18     fvdl 				 */
    329  1.18     fvdl 				XXX
    330  1.18     fvdl 
    331  1.18     fvdl 				/*
    332  1.18     fvdl 				 * Validate the timestamp verifier, to
    333  1.18     fvdl 				 * check that the session key is ok.
    334  1.18     fvdl 				 */
    335  1.18     fvdl 				nsd.nsd_timestamp.tv_sec = ntohl(kout.t1);
    336  1.18     fvdl 				nsd.nsd_timestamp.tv_usec = ntohl(kout.t2);
    337  1.18     fvdl 				nsd.nsd_ttl = ntohl(kout.w1);
    338  1.18     fvdl 				if ((nsd.nsd_ttl - 1) == ntohl(kout.w2))
    339  1.18     fvdl 				    nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHIN;
    340  1.11  mycroft 			}
    341  1.18     fvdl #endif /* NFSKERB */
    342   1.1      cgd 		}
    343   1.1      cgd 		exit(0);
    344   1.1      cgd 	}
    345  1.11  mycroft 
    346  1.11  mycroft 	/* If we are serving udp, set up the socket. */
    347   1.1      cgd 	if (udpflag) {
    348   1.1      cgd 		if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    349  1.11  mycroft 			syslog(LOG_ERR, "can't create udp socket");
    350   1.1      cgd 			exit(1);
    351   1.1      cgd 		}
    352  1.11  mycroft 		inetaddr.sin_family = AF_INET;
    353  1.11  mycroft 		inetaddr.sin_addr.s_addr = INADDR_ANY;
    354  1.11  mycroft 		inetaddr.sin_port = htons(NFS_PORT);
    355  1.11  mycroft 		inetaddr.sin_len = sizeof(inetaddr);
    356  1.11  mycroft 		if (bind(sock,
    357  1.11  mycroft 		    (struct sockaddr *)&inetaddr, sizeof(inetaddr)) < 0) {
    358  1.11  mycroft 			syslog(LOG_ERR, "can't bind udp addr");
    359   1.1      cgd 			exit(1);
    360   1.1      cgd 		}
    361  1.18     fvdl 		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
    362  1.18     fvdl 		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
    363  1.11  mycroft 			syslog(LOG_ERR, "can't register with udp portmap");
    364  1.11  mycroft 			exit(1);
    365  1.11  mycroft 		}
    366  1.11  mycroft 		nfsdargs.sock = sock;
    367  1.11  mycroft 		nfsdargs.name = NULL;
    368  1.11  mycroft 		nfsdargs.namelen = 0;
    369  1.11  mycroft 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    370  1.11  mycroft 			syslog(LOG_ERR, "can't Add UDP socket");
    371  1.11  mycroft 			exit(1);
    372  1.11  mycroft 		}
    373  1.11  mycroft 		(void)close(sock);
    374  1.11  mycroft 	}
    375  1.11  mycroft 
    376  1.11  mycroft #ifdef ISO
    377  1.11  mycroft 	/* If we are serving cltp, set up the socket. */
    378  1.11  mycroft 	if (cltpflag) {
    379  1.11  mycroft 		if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
    380  1.11  mycroft 			syslog(LOG_ERR, "can't create cltp socket");
    381  1.11  mycroft 			exit(1);
    382  1.11  mycroft 		}
    383  1.11  mycroft 		memset(&isoaddr, 0, sizeof(isoaddr));
    384  1.11  mycroft 		isoaddr.siso_family = AF_ISO;
    385  1.11  mycroft 		isoaddr.siso_tlen = 2;
    386  1.11  mycroft 		cp = TSEL(&isoaddr);
    387  1.11  mycroft 		*cp++ = (NFS_PORT >> 8);
    388  1.11  mycroft 		*cp = (NFS_PORT & 0xff);
    389  1.11  mycroft 		isoaddr.siso_len = sizeof(isoaddr);
    390  1.11  mycroft 		if (bind(sock,
    391  1.11  mycroft 		    (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
    392  1.11  mycroft 			syslog(LOG_ERR, "can't bind cltp addr");
    393   1.1      cgd 			exit(1);
    394   1.1      cgd 		}
    395  1.11  mycroft #ifdef notyet
    396   1.1      cgd 		/*
    397  1.11  mycroft 		 * XXX
    398  1.11  mycroft 		 * Someday this should probably use "rpcbind", the son of
    399  1.11  mycroft 		 * portmap.
    400   1.1      cgd 		 */
    401  1.11  mycroft 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
    402  1.11  mycroft 			syslog(LOG_ERR, "can't register with udp portmap");
    403  1.11  mycroft 			exit(1);
    404  1.11  mycroft 		}
    405  1.11  mycroft #endif /* notyet */
    406  1.11  mycroft 		nfsdargs.sock = sock;
    407  1.11  mycroft 		nfsdargs.name = NULL;
    408  1.11  mycroft 		nfsdargs.namelen = 0;
    409  1.11  mycroft 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    410  1.11  mycroft 			syslog(LOG_ERR, "can't add UDP socket");
    411  1.11  mycroft 			exit(1);
    412  1.11  mycroft 		}
    413   1.1      cgd 		close(sock);
    414   1.1      cgd 	}
    415  1.11  mycroft #endif /* ISO */
    416   1.1      cgd 
    417  1.11  mycroft 	/* Now set up the master server socket waiting for tcp connections. */
    418  1.11  mycroft 	on = 1;
    419  1.11  mycroft 	FD_ZERO(&sockbits);
    420  1.11  mycroft 	connect_type_cnt = 0;
    421   1.1      cgd 	if (tcpflag) {
    422  1.11  mycroft 		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    423  1.11  mycroft 			syslog(LOG_ERR, "can't create tcp socket");
    424  1.11  mycroft 			exit(1);
    425  1.11  mycroft 		}
    426  1.11  mycroft 		if (setsockopt(tcpsock,
    427  1.11  mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    428  1.11  mycroft 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    429  1.11  mycroft 		inetaddr.sin_family = AF_INET;
    430  1.11  mycroft 		inetaddr.sin_addr.s_addr = INADDR_ANY;
    431  1.11  mycroft 		inetaddr.sin_port = htons(NFS_PORT);
    432  1.11  mycroft 		inetaddr.sin_len = sizeof(inetaddr);
    433  1.11  mycroft 		if (bind(tcpsock,
    434  1.11  mycroft 		    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
    435  1.11  mycroft 			syslog(LOG_ERR, "can't bind tcp addr");
    436  1.11  mycroft 			exit(1);
    437  1.11  mycroft 		}
    438  1.11  mycroft 		if (listen(tcpsock, 5) < 0) {
    439  1.11  mycroft 			syslog(LOG_ERR, "listen failed");
    440  1.11  mycroft 			exit(1);
    441  1.11  mycroft 		}
    442  1.18     fvdl 		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
    443  1.18     fvdl 		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
    444  1.11  mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    445  1.11  mycroft 			exit(1);
    446  1.11  mycroft 		}
    447  1.11  mycroft 		FD_SET(tcpsock, &sockbits);
    448  1.11  mycroft 		maxsock = tcpsock;
    449  1.11  mycroft 		connect_type_cnt++;
    450  1.11  mycroft 	}
    451   1.1      cgd 
    452  1.11  mycroft #ifdef notyet
    453  1.11  mycroft 	/* Now set up the master server socket waiting for tp4 connections. */
    454  1.11  mycroft 	if (tp4flag) {
    455  1.11  mycroft 		if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
    456  1.11  mycroft 			syslog(LOG_ERR, "can't create tp4 socket");
    457   1.1      cgd 			exit(1);
    458   1.1      cgd 		}
    459  1.11  mycroft 		if (setsockopt(tp4sock,
    460  1.11  mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    461   1.1      cgd 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    462  1.11  mycroft 		memset(&isoaddr, 0, sizeof(isoaddr));
    463  1.11  mycroft 		isoaddr.siso_family = AF_ISO;
    464  1.11  mycroft 		isoaddr.siso_tlen = 2;
    465  1.11  mycroft 		cp = TSEL(&isoaddr);
    466  1.11  mycroft 		*cp++ = (NFS_PORT >> 8);
    467  1.11  mycroft 		*cp = (NFS_PORT & 0xff);
    468  1.11  mycroft 		isoaddr.siso_len = sizeof(isoaddr);
    469  1.11  mycroft 		if (bind(tp4sock,
    470  1.12  mycroft 		    (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
    471  1.11  mycroft 			syslog(LOG_ERR, "can't bind tp4 addr");
    472   1.1      cgd 			exit(1);
    473   1.1      cgd 		}
    474  1.11  mycroft 		if (listen(tp4sock, 5) < 0) {
    475  1.11  mycroft 			syslog(LOG_ERR, "listen failed");
    476   1.1      cgd 			exit(1);
    477   1.1      cgd 		}
    478  1.11  mycroft 		/*
    479  1.11  mycroft 		 * XXX
    480  1.11  mycroft 		 * Someday this should probably use "rpcbind", the son of
    481  1.11  mycroft 		 * portmap.
    482  1.11  mycroft 		 */
    483   1.1      cgd 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
    484  1.11  mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    485  1.11  mycroft 			exit(1);
    486  1.11  mycroft 		}
    487  1.11  mycroft 		FD_SET(tp4sock, &sockbits);
    488  1.11  mycroft 		maxsock = tp4sock;
    489  1.11  mycroft 		connect_type_cnt++;
    490  1.11  mycroft 	}
    491  1.11  mycroft 
    492  1.11  mycroft 	/* Now set up the master server socket waiting for tpip connections. */
    493  1.11  mycroft 	if (tpipflag) {
    494  1.11  mycroft 		if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
    495  1.11  mycroft 			syslog(LOG_ERR, "can't create tpip socket");
    496  1.11  mycroft 			exit(1);
    497  1.11  mycroft 		}
    498  1.11  mycroft 		if (setsockopt(tpipsock,
    499  1.11  mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    500  1.11  mycroft 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    501  1.11  mycroft 		inetaddr.sin_family = AF_INET;
    502  1.11  mycroft 		inetaddr.sin_addr.s_addr = INADDR_ANY;
    503  1.11  mycroft 		inetaddr.sin_port = htons(NFS_PORT);
    504  1.11  mycroft 		inetaddr.sin_len = sizeof(inetaddr);
    505  1.11  mycroft 		if (bind(tpipsock,
    506  1.11  mycroft 		    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
    507  1.11  mycroft 			syslog(LOG_ERR, "can't bind tcp addr");
    508  1.11  mycroft 			exit(1);
    509  1.11  mycroft 		}
    510  1.11  mycroft 		if (listen(tpipsock, 5) < 0) {
    511  1.11  mycroft 			syslog(LOG_ERR, "listen failed");
    512   1.1      cgd 			exit(1);
    513   1.1      cgd 		}
    514   1.1      cgd 		/*
    515  1.11  mycroft 		 * XXX
    516  1.11  mycroft 		 * Someday this should probably use "rpcbind", the son of
    517  1.11  mycroft 		 * portmap.
    518   1.1      cgd 		 */
    519  1.11  mycroft 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
    520  1.11  mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    521  1.11  mycroft 			exit(1);
    522  1.11  mycroft 		}
    523  1.11  mycroft 		FD_SET(tpipsock, &sockbits);
    524  1.11  mycroft 		maxsock = tpipsock;
    525  1.11  mycroft 		connect_type_cnt++;
    526  1.11  mycroft 	}
    527  1.11  mycroft #endif /* notyet */
    528  1.11  mycroft 
    529  1.11  mycroft 	if (connect_type_cnt == 0)
    530  1.11  mycroft 		exit(0);
    531  1.11  mycroft 
    532  1.13  mycroft 	setproctitle("master");
    533  1.11  mycroft 
    534  1.11  mycroft 	/*
    535  1.11  mycroft 	 * Loop forever accepting connections and passing the sockets
    536  1.11  mycroft 	 * into the kernel for the mounts.
    537  1.11  mycroft 	 */
    538  1.11  mycroft 	for (;;) {
    539  1.11  mycroft 		ready = sockbits;
    540  1.11  mycroft 		if (connect_type_cnt > 1) {
    541  1.11  mycroft 			if (select(maxsock + 1,
    542  1.11  mycroft 			    &ready, NULL, NULL, NULL) < 1) {
    543  1.11  mycroft 				syslog(LOG_ERR, "select failed: %m");
    544  1.11  mycroft 				exit(1);
    545  1.11  mycroft 			}
    546  1.11  mycroft 		}
    547  1.11  mycroft 		if (tcpflag && FD_ISSET(tcpsock, &ready)) {
    548  1.11  mycroft 			len = sizeof(inetpeer);
    549  1.11  mycroft 			if ((msgsock = accept(tcpsock,
    550  1.11  mycroft 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
    551  1.11  mycroft 				syslog(LOG_ERR, "accept failed: %m");
    552   1.1      cgd 				exit(1);
    553   1.1      cgd 			}
    554  1.11  mycroft 			memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
    555  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    556  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    557  1.11  mycroft 				syslog(LOG_ERR,
    558  1.11  mycroft 				    "setsockopt SO_KEEPALIVE: %m");
    559  1.11  mycroft 			nfsdargs.sock = msgsock;
    560  1.11  mycroft 			nfsdargs.name = (caddr_t)&inetpeer;
    561  1.11  mycroft 			nfsdargs.namelen = sizeof(inetpeer);
    562  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    563  1.11  mycroft 			(void)close(msgsock);
    564  1.11  mycroft 		}
    565  1.11  mycroft #ifdef notyet
    566  1.11  mycroft 		if (tp4flag && FD_ISSET(tp4sock, &ready)) {
    567  1.11  mycroft 			len = sizeof(isopeer);
    568  1.11  mycroft 			if ((msgsock = accept(tp4sock,
    569  1.11  mycroft 			    (struct sockaddr *)&isopeer, &len)) < 0) {
    570  1.11  mycroft 				syslog(LOG_ERR, "accept failed: %m");
    571  1.11  mycroft 				exit(1);
    572   1.1      cgd 			}
    573  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    574  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    575  1.11  mycroft 				syslog(LOG_ERR,
    576  1.11  mycroft 				    "setsockopt SO_KEEPALIVE: %m");
    577  1.11  mycroft 			nfsdargs.sock = msgsock;
    578  1.11  mycroft 			nfsdargs.name = (caddr_t)&isopeer;
    579  1.11  mycroft 			nfsdargs.namelen = len;
    580  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    581  1.11  mycroft 			(void)close(msgsock);
    582  1.11  mycroft 		}
    583  1.11  mycroft 		if (tpipflag && FD_ISSET(tpipsock, &ready)) {
    584  1.11  mycroft 			len = sizeof(inetpeer);
    585  1.11  mycroft 			if ((msgsock = accept(tpipsock,
    586  1.11  mycroft 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
    587  1.11  mycroft 				syslog(LOG_ERR, "Accept failed: %m");
    588   1.1      cgd 				exit(1);
    589   1.1      cgd 			}
    590  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    591  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    592  1.11  mycroft 				syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
    593  1.11  mycroft 			nfsdargs.sock = msgsock;
    594  1.11  mycroft 			nfsdargs.name = (caddr_t)&inetpeer;
    595  1.11  mycroft 			nfsdargs.namelen = len;
    596  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    597  1.11  mycroft 			(void)close(msgsock);
    598   1.1      cgd 		}
    599  1.11  mycroft #endif /* notyet */
    600   1.1      cgd 	}
    601   1.1      cgd }
    602   1.1      cgd 
    603  1.11  mycroft void
    604   1.1      cgd usage()
    605   1.1      cgd {
    606  1.15  mycroft 	(void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
    607   1.1      cgd 	exit(1);
    608   1.1      cgd }
    609   1.1      cgd 
    610   1.1      cgd void
    611  1.11  mycroft nonfs(signo)
    612  1.11  mycroft 	int signo;
    613   1.1      cgd {
    614  1.15  mycroft 
    615  1.11  mycroft 	syslog(LOG_ERR, "missing system call: NFS not available.");
    616   1.1      cgd }
    617   1.4    glass 
    618  1.11  mycroft void
    619  1.11  mycroft reapchild(signo)
    620  1.11  mycroft 	int signo;
    621   1.4    glass {
    622  1.11  mycroft 
    623  1.18     fvdl 	while (wait3(NULL, WNOHANG, NULL) > 0);
    624   1.4    glass }
    625