Home | History | Annotate | Line # | Download | only in nfsd
nfsd.c revision 1.17
      1  1.17      jtc /*	$NetBSD: nfsd.c,v 1.17 1995/05/28 05:31:45 jtc 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.16      cgd static char sccsid[] = "@(#)nfsd.c	8.7 (Berkeley) 2/22/94";
     48  1.16      cgd #else
     49  1.17      jtc static char rcsid[] = "$NetBSD: nfsd.c,v 1.17 1995/05/28 05:31:45 jtc Exp $";
     50  1.16      cgd #endif
     51   1.1      cgd #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.1      cgd #include <nfs/nfsv2.h>
     73  1.11  mycroft #include <nfs/nfs.h>
     74  1.11  mycroft 
     75  1.11  mycroft #ifdef KERBEROS
     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.11  mycroft #ifdef KERBEROS
    102  1.11  mycroft char		lnam[ANAME_SZ];
    103  1.11  mycroft KTEXT_ST	kt;
    104  1.11  mycroft AUTH_DAT	auth;
    105  1.11  mycroft char		inst[INST_SZ];
    106  1.11  mycroft #endif
    107  1.11  mycroft 
    108  1.11  mycroft void	nonfs __P((int));
    109  1.11  mycroft void	reapchild __P((int));
    110  1.11  mycroft void	usage __P((void));
    111   1.1      cgd 
    112   1.1      cgd /*
    113   1.1      cgd  * Nfs server daemon mostly just a user context for nfssvc()
    114  1.11  mycroft  *
    115   1.1      cgd  * 1 - do file descriptor and signal cleanup
    116  1.11  mycroft  * 2 - fork the nfsd(s)
    117  1.11  mycroft  * 3 - create server socket(s)
    118  1.11  mycroft  * 4 - register socket with portmap
    119  1.11  mycroft  *
    120  1.11  mycroft  * For connectionless protocols, just pass the socket into the kernel via.
    121  1.11  mycroft  * nfssvc().
    122  1.11  mycroft  * For connection based sockets, loop doing accepts. When you get a new
    123  1.11  mycroft  * socket from accept, pass the msgsock into the kernel via. nfssvc().
    124   1.1      cgd  * The arguments are:
    125  1.11  mycroft  *	-c - support iso cltp clients
    126  1.11  mycroft  *	-r - reregister with portmapper
    127  1.11  mycroft  *	-t - support tcp nfs clients
    128  1.11  mycroft  *	-u - support udp nfs clients
    129  1.11  mycroft  * followed by "n" which is the number of nfsds' to fork off
    130   1.1      cgd  */
    131  1.11  mycroft int
    132   1.1      cgd main(argc, argv, envp)
    133   1.1      cgd 	int argc;
    134   1.1      cgd 	char *argv[], *envp[];
    135   1.1      cgd {
    136   1.1      cgd 	extern int optind;
    137  1.11  mycroft 	struct group *grp;
    138  1.11  mycroft 	struct nfsd_args nfsdargs;
    139  1.11  mycroft 	struct passwd *pwd;
    140  1.11  mycroft 	struct ucred *cr;
    141  1.11  mycroft 	struct sockaddr_in inetaddr, inetpeer;
    142  1.11  mycroft #ifdef ISO
    143  1.11  mycroft 	struct sockaddr_iso isoaddr, isopeer;
    144  1.11  mycroft #endif
    145  1.11  mycroft 	fd_set ready, sockbits;
    146  1.11  mycroft 	int ch, cltpflag, connect_type_cnt, i, len, maxsock, msgsock;
    147  1.11  mycroft 	int nfsdcnt, nfssvc_flag, on, reregister, sock, tcpflag, tcpsock;
    148  1.11  mycroft 	int tp4cnt, tp4flag, tp4sock, tpipcnt, tpipflag, tpipsock, udpflag;
    149  1.11  mycroft 	char *cp, **cpp;
    150  1.11  mycroft 
    151  1.11  mycroft #define	MAXNFSDCNT	20
    152  1.11  mycroft #define	DEFNFSDCNT	 4
    153  1.11  mycroft 	nfsdcnt = DEFNFSDCNT;
    154  1.11  mycroft 	cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
    155  1.11  mycroft 	tpipflag = udpflag = 0;
    156  1.11  mycroft #ifdef ISO
    157  1.11  mycroft #define	GETOPT	"cn:rtu"
    158  1.11  mycroft #define	USAGE	"[-crtu] [-n num_servers]"
    159  1.11  mycroft #else
    160  1.11  mycroft #define	GETOPT	"n:rtu"
    161  1.11  mycroft #define	USAGE	"[-rtu] [-n num_servers]"
    162  1.11  mycroft #endif
    163  1.11  mycroft 	while ((ch = getopt(argc, argv, GETOPT)) != EOF)
    164  1.11  mycroft 		switch (ch) {
    165  1.11  mycroft 		case 'n':
    166  1.11  mycroft 			nfsdcnt = atoi(optarg);
    167  1.11  mycroft 			if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
    168  1.11  mycroft 				warnx("nfsd count %d; reset to %d", DEFNFSDCNT);
    169  1.11  mycroft 				nfsdcnt = DEFNFSDCNT;
    170  1.11  mycroft 			}
    171  1.11  mycroft 			break;
    172   1.1      cgd 		case 'r':
    173  1.11  mycroft 			reregister = 1;
    174   1.1      cgd 			break;
    175   1.1      cgd 		case 't':
    176  1.11  mycroft 			tcpflag = 1;
    177   1.1      cgd 			break;
    178   1.1      cgd 		case 'u':
    179  1.11  mycroft 			udpflag = 1;
    180  1.11  mycroft 			break;
    181  1.11  mycroft #ifdef ISO
    182  1.11  mycroft 		case 'c':
    183  1.11  mycroft 			cltpflag = 1;
    184   1.1      cgd 			break;
    185  1.11  mycroft #ifdef notyet
    186  1.11  mycroft 		case 'i':
    187  1.11  mycroft 			tp4cnt = 1;
    188  1.11  mycroft 			break;
    189  1.11  mycroft 		case 'p':
    190  1.11  mycroft 			tpipcnt = 1;
    191  1.11  mycroft 			break;
    192  1.11  mycroft #endif /* notyet */
    193  1.11  mycroft #endif /* ISO */
    194   1.1      cgd 		default:
    195   1.1      cgd 		case '?':
    196   1.1      cgd 			usage();
    197  1.11  mycroft 		};
    198  1.11  mycroft 	argv += optind;
    199  1.11  mycroft 	argc -= optind;
    200   1.1      cgd 
    201   1.1      cgd 	/*
    202  1.11  mycroft 	 * XXX
    203  1.11  mycroft 	 * Backward compatibility, trailing number is the count of daemons.
    204   1.1      cgd 	 */
    205  1.11  mycroft 	if (argc > 1)
    206  1.11  mycroft 		usage();
    207  1.11  mycroft 	if (argc == 1) {
    208  1.11  mycroft 		nfsdcnt = atoi(argv[0]);
    209  1.11  mycroft 		if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
    210  1.11  mycroft 			warnx("nfsd count %d; reset to %d", DEFNFSDCNT);
    211  1.11  mycroft 			nfsdcnt = DEFNFSDCNT;
    212  1.11  mycroft 		}
    213   1.1      cgd 	}
    214   1.1      cgd 
    215   1.1      cgd 	if (debug == 0) {
    216   1.1      cgd 		daemon(0, 0);
    217  1.11  mycroft 		(void)signal(SIGHUP, SIG_IGN);
    218  1.11  mycroft 		(void)signal(SIGINT, SIG_IGN);
    219  1.11  mycroft 		(void)signal(SIGQUIT, SIG_IGN);
    220  1.11  mycroft 		(void)signal(SIGSYS, nonfs);
    221  1.11  mycroft 		(void)signal(SIGTERM, SIG_IGN);
    222   1.1      cgd 	}
    223  1.11  mycroft 	(void)signal(SIGCHLD, reapchild);
    224  1.11  mycroft 
    225  1.11  mycroft 	if (reregister) {
    226  1.11  mycroft 		if (udpflag &&
    227  1.11  mycroft 		    !pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT))
    228  1.11  mycroft 			err(1, "can't register with portmap for UDP.");
    229  1.11  mycroft 		if (tcpflag &&
    230  1.11  mycroft 		    !pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT))
    231  1.11  mycroft 			err(1, "can't register with portmap for TCP.");
    232  1.11  mycroft 		exit(0);
    233   1.8      cgd 	}
    234  1.11  mycroft 	openlog("nfsd:", LOG_PID, LOG_DAEMON);
    235  1.11  mycroft 
    236  1.11  mycroft 	for (i = 0; i < nfsdcnt; i++) {
    237  1.11  mycroft 		switch (fork()) {
    238  1.11  mycroft 		case -1:
    239  1.11  mycroft 			syslog(LOG_ERR, "fork: %m");
    240  1.11  mycroft 			exit (1);
    241  1.11  mycroft 		case 0:
    242  1.11  mycroft 			break;
    243  1.11  mycroft 		default:
    244  1.11  mycroft 			continue;
    245   1.1      cgd 		}
    246  1.11  mycroft 
    247  1.13  mycroft 		setproctitle("server");
    248  1.11  mycroft 		nfssvc_flag = NFSSVC_NFSD;
    249  1.11  mycroft 		nsd.nsd_nfsd = NULL;
    250  1.11  mycroft #ifdef KERBEROS
    251  1.11  mycroft 		nsd.nsd_authstr = (char *)kt.dat;
    252  1.11  mycroft #endif
    253  1.11  mycroft 		while (nfssvc(nfssvc_flag, &nsd) < 0) {
    254  1.11  mycroft 			if (errno != ENEEDAUTH) {
    255  1.11  mycroft 				syslog(LOG_ERR, "nfssvc: %m");
    256  1.11  mycroft 				exit(1);
    257  1.11  mycroft 			}
    258  1.11  mycroft 			nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
    259  1.11  mycroft #ifdef KERBEROS
    260  1.11  mycroft 			kt.length = nsd.nsd_authlen;
    261  1.11  mycroft 			kt.mbz = 0;
    262  1.11  mycroft 			(void)strcpy(inst, "*");
    263  1.11  mycroft 			if (krb_rd_req(&kt, "rcmd",
    264  1.11  mycroft 			    inst, nsd.nsd_haddr, &auth, "") == RD_AP_OK &&
    265  1.11  mycroft 			    krb_kntoln(&auth, lnam) == KSUCCESS &&
    266  1.11  mycroft 			    (pwd = getpwnam(lnam)) != NULL) {
    267  1.11  mycroft 				cr = &nsd.nsd_cr;
    268  1.11  mycroft 				cr->cr_uid = pwd->pw_uid;
    269  1.11  mycroft 				cr->cr_groups[0] = pwd->pw_gid;
    270  1.11  mycroft 				cr->cr_ngroups = 1;
    271  1.11  mycroft 				setgrent();
    272  1.11  mycroft 				while ((grp = getgrent()) != NULL) {
    273  1.11  mycroft 					if (grp->gr_gid == cr->cr_groups[0])
    274  1.11  mycroft 						continue;
    275  1.11  mycroft 					for (cpp = grp->gr_mem;
    276  1.11  mycroft 					    *cpp != NULL; ++cpp)
    277  1.11  mycroft 						if (!strcmp(*cpp, lnam))
    278  1.11  mycroft 							break;
    279  1.11  mycroft 					if (*cpp == NULL)
    280  1.11  mycroft 						continue;
    281  1.11  mycroft 					cr->cr_groups[cr->cr_ngroups++]
    282  1.11  mycroft 					    = grp->gr_gid;
    283  1.11  mycroft 					if (cr->cr_ngroups == NGROUPS)
    284  1.11  mycroft 						break;
    285  1.11  mycroft 				}
    286  1.11  mycroft 				endgrent();
    287  1.11  mycroft 				nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHIN;
    288  1.11  mycroft 			}
    289  1.11  mycroft #endif /* KERBEROS */
    290   1.1      cgd 		}
    291   1.1      cgd 		exit(0);
    292   1.1      cgd 	}
    293  1.11  mycroft 
    294  1.11  mycroft 	/* If we are serving udp, set up the socket. */
    295   1.1      cgd 	if (udpflag) {
    296   1.1      cgd 		if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    297  1.11  mycroft 			syslog(LOG_ERR, "can't create udp socket");
    298   1.1      cgd 			exit(1);
    299   1.1      cgd 		}
    300  1.11  mycroft 		inetaddr.sin_family = AF_INET;
    301  1.11  mycroft 		inetaddr.sin_addr.s_addr = INADDR_ANY;
    302  1.11  mycroft 		inetaddr.sin_port = htons(NFS_PORT);
    303  1.11  mycroft 		inetaddr.sin_len = sizeof(inetaddr);
    304  1.11  mycroft 		if (bind(sock,
    305  1.11  mycroft 		    (struct sockaddr *)&inetaddr, sizeof(inetaddr)) < 0) {
    306  1.11  mycroft 			syslog(LOG_ERR, "can't bind udp addr");
    307   1.1      cgd 			exit(1);
    308   1.1      cgd 		}
    309   1.1      cgd 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
    310  1.11  mycroft 			syslog(LOG_ERR, "can't register with udp portmap");
    311  1.11  mycroft 			exit(1);
    312  1.11  mycroft 		}
    313  1.11  mycroft 		nfsdargs.sock = sock;
    314  1.11  mycroft 		nfsdargs.name = NULL;
    315  1.11  mycroft 		nfsdargs.namelen = 0;
    316  1.11  mycroft 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    317  1.11  mycroft 			syslog(LOG_ERR, "can't Add UDP socket");
    318  1.11  mycroft 			exit(1);
    319  1.11  mycroft 		}
    320  1.11  mycroft 		(void)close(sock);
    321  1.11  mycroft 	}
    322  1.11  mycroft 
    323  1.11  mycroft #ifdef ISO
    324  1.11  mycroft 	/* If we are serving cltp, set up the socket. */
    325  1.11  mycroft 	if (cltpflag) {
    326  1.11  mycroft 		if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
    327  1.11  mycroft 			syslog(LOG_ERR, "can't create cltp socket");
    328  1.11  mycroft 			exit(1);
    329  1.11  mycroft 		}
    330  1.11  mycroft 		memset(&isoaddr, 0, sizeof(isoaddr));
    331  1.11  mycroft 		isoaddr.siso_family = AF_ISO;
    332  1.11  mycroft 		isoaddr.siso_tlen = 2;
    333  1.11  mycroft 		cp = TSEL(&isoaddr);
    334  1.11  mycroft 		*cp++ = (NFS_PORT >> 8);
    335  1.11  mycroft 		*cp = (NFS_PORT & 0xff);
    336  1.11  mycroft 		isoaddr.siso_len = sizeof(isoaddr);
    337  1.11  mycroft 		if (bind(sock,
    338  1.11  mycroft 		    (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
    339  1.11  mycroft 			syslog(LOG_ERR, "can't bind cltp addr");
    340   1.1      cgd 			exit(1);
    341   1.1      cgd 		}
    342  1.11  mycroft #ifdef notyet
    343   1.1      cgd 		/*
    344  1.11  mycroft 		 * XXX
    345  1.11  mycroft 		 * Someday this should probably use "rpcbind", the son of
    346  1.11  mycroft 		 * portmap.
    347   1.1      cgd 		 */
    348  1.11  mycroft 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
    349  1.11  mycroft 			syslog(LOG_ERR, "can't register with udp portmap");
    350  1.11  mycroft 			exit(1);
    351  1.11  mycroft 		}
    352  1.11  mycroft #endif /* notyet */
    353  1.11  mycroft 		nfsdargs.sock = sock;
    354  1.11  mycroft 		nfsdargs.name = NULL;
    355  1.11  mycroft 		nfsdargs.namelen = 0;
    356  1.11  mycroft 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
    357  1.11  mycroft 			syslog(LOG_ERR, "can't add UDP socket");
    358  1.11  mycroft 			exit(1);
    359  1.11  mycroft 		}
    360   1.1      cgd 		close(sock);
    361   1.1      cgd 	}
    362  1.11  mycroft #endif /* ISO */
    363   1.1      cgd 
    364  1.11  mycroft 	/* Now set up the master server socket waiting for tcp connections. */
    365  1.11  mycroft 	on = 1;
    366  1.11  mycroft 	FD_ZERO(&sockbits);
    367  1.11  mycroft 	connect_type_cnt = 0;
    368   1.1      cgd 	if (tcpflag) {
    369  1.11  mycroft 		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    370  1.11  mycroft 			syslog(LOG_ERR, "can't create tcp socket");
    371  1.11  mycroft 			exit(1);
    372  1.11  mycroft 		}
    373  1.11  mycroft 		if (setsockopt(tcpsock,
    374  1.11  mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    375  1.11  mycroft 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    376  1.11  mycroft 		inetaddr.sin_family = AF_INET;
    377  1.11  mycroft 		inetaddr.sin_addr.s_addr = INADDR_ANY;
    378  1.11  mycroft 		inetaddr.sin_port = htons(NFS_PORT);
    379  1.11  mycroft 		inetaddr.sin_len = sizeof(inetaddr);
    380  1.11  mycroft 		if (bind(tcpsock,
    381  1.11  mycroft 		    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
    382  1.11  mycroft 			syslog(LOG_ERR, "can't bind tcp addr");
    383  1.11  mycroft 			exit(1);
    384  1.11  mycroft 		}
    385  1.11  mycroft 		if (listen(tcpsock, 5) < 0) {
    386  1.11  mycroft 			syslog(LOG_ERR, "listen failed");
    387  1.11  mycroft 			exit(1);
    388  1.11  mycroft 		}
    389  1.11  mycroft 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
    390  1.11  mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    391  1.11  mycroft 			exit(1);
    392  1.11  mycroft 		}
    393  1.11  mycroft 		FD_SET(tcpsock, &sockbits);
    394  1.11  mycroft 		maxsock = tcpsock;
    395  1.11  mycroft 		connect_type_cnt++;
    396  1.11  mycroft 	}
    397   1.1      cgd 
    398  1.11  mycroft #ifdef notyet
    399  1.11  mycroft 	/* Now set up the master server socket waiting for tp4 connections. */
    400  1.11  mycroft 	if (tp4flag) {
    401  1.11  mycroft 		if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
    402  1.11  mycroft 			syslog(LOG_ERR, "can't create tp4 socket");
    403   1.1      cgd 			exit(1);
    404   1.1      cgd 		}
    405  1.11  mycroft 		if (setsockopt(tp4sock,
    406  1.11  mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    407   1.1      cgd 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    408  1.11  mycroft 		memset(&isoaddr, 0, sizeof(isoaddr));
    409  1.11  mycroft 		isoaddr.siso_family = AF_ISO;
    410  1.11  mycroft 		isoaddr.siso_tlen = 2;
    411  1.11  mycroft 		cp = TSEL(&isoaddr);
    412  1.11  mycroft 		*cp++ = (NFS_PORT >> 8);
    413  1.11  mycroft 		*cp = (NFS_PORT & 0xff);
    414  1.11  mycroft 		isoaddr.siso_len = sizeof(isoaddr);
    415  1.11  mycroft 		if (bind(tp4sock,
    416  1.12  mycroft 		    (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
    417  1.11  mycroft 			syslog(LOG_ERR, "can't bind tp4 addr");
    418   1.1      cgd 			exit(1);
    419   1.1      cgd 		}
    420  1.11  mycroft 		if (listen(tp4sock, 5) < 0) {
    421  1.11  mycroft 			syslog(LOG_ERR, "listen failed");
    422   1.1      cgd 			exit(1);
    423   1.1      cgd 		}
    424  1.11  mycroft 		/*
    425  1.11  mycroft 		 * XXX
    426  1.11  mycroft 		 * Someday this should probably use "rpcbind", the son of
    427  1.11  mycroft 		 * portmap.
    428  1.11  mycroft 		 */
    429   1.1      cgd 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
    430  1.11  mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    431  1.11  mycroft 			exit(1);
    432  1.11  mycroft 		}
    433  1.11  mycroft 		FD_SET(tp4sock, &sockbits);
    434  1.11  mycroft 		maxsock = tp4sock;
    435  1.11  mycroft 		connect_type_cnt++;
    436  1.11  mycroft 	}
    437  1.11  mycroft 
    438  1.11  mycroft 	/* Now set up the master server socket waiting for tpip connections. */
    439  1.11  mycroft 	if (tpipflag) {
    440  1.11  mycroft 		if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
    441  1.11  mycroft 			syslog(LOG_ERR, "can't create tpip socket");
    442  1.11  mycroft 			exit(1);
    443  1.11  mycroft 		}
    444  1.11  mycroft 		if (setsockopt(tpipsock,
    445  1.11  mycroft 		    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
    446  1.11  mycroft 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
    447  1.11  mycroft 		inetaddr.sin_family = AF_INET;
    448  1.11  mycroft 		inetaddr.sin_addr.s_addr = INADDR_ANY;
    449  1.11  mycroft 		inetaddr.sin_port = htons(NFS_PORT);
    450  1.11  mycroft 		inetaddr.sin_len = sizeof(inetaddr);
    451  1.11  mycroft 		if (bind(tpipsock,
    452  1.11  mycroft 		    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
    453  1.11  mycroft 			syslog(LOG_ERR, "can't bind tcp addr");
    454  1.11  mycroft 			exit(1);
    455  1.11  mycroft 		}
    456  1.11  mycroft 		if (listen(tpipsock, 5) < 0) {
    457  1.11  mycroft 			syslog(LOG_ERR, "listen failed");
    458   1.1      cgd 			exit(1);
    459   1.1      cgd 		}
    460   1.1      cgd 		/*
    461  1.11  mycroft 		 * XXX
    462  1.11  mycroft 		 * Someday this should probably use "rpcbind", the son of
    463  1.11  mycroft 		 * portmap.
    464   1.1      cgd 		 */
    465  1.11  mycroft 		if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
    466  1.11  mycroft 			syslog(LOG_ERR, "can't register tcp with portmap");
    467  1.11  mycroft 			exit(1);
    468  1.11  mycroft 		}
    469  1.11  mycroft 		FD_SET(tpipsock, &sockbits);
    470  1.11  mycroft 		maxsock = tpipsock;
    471  1.11  mycroft 		connect_type_cnt++;
    472  1.11  mycroft 	}
    473  1.11  mycroft #endif /* notyet */
    474  1.11  mycroft 
    475  1.11  mycroft 	if (connect_type_cnt == 0)
    476  1.11  mycroft 		exit(0);
    477  1.11  mycroft 
    478  1.13  mycroft 	setproctitle("master");
    479  1.11  mycroft 
    480  1.11  mycroft 	/*
    481  1.11  mycroft 	 * Loop forever accepting connections and passing the sockets
    482  1.11  mycroft 	 * into the kernel for the mounts.
    483  1.11  mycroft 	 */
    484  1.11  mycroft 	for (;;) {
    485  1.11  mycroft 		ready = sockbits;
    486  1.11  mycroft 		if (connect_type_cnt > 1) {
    487  1.11  mycroft 			if (select(maxsock + 1,
    488  1.11  mycroft 			    &ready, NULL, NULL, NULL) < 1) {
    489  1.11  mycroft 				syslog(LOG_ERR, "select failed: %m");
    490  1.11  mycroft 				exit(1);
    491  1.11  mycroft 			}
    492  1.11  mycroft 		}
    493  1.11  mycroft 		if (tcpflag && FD_ISSET(tcpsock, &ready)) {
    494  1.11  mycroft 			len = sizeof(inetpeer);
    495  1.11  mycroft 			if ((msgsock = accept(tcpsock,
    496  1.11  mycroft 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
    497  1.11  mycroft 				syslog(LOG_ERR, "accept failed: %m");
    498   1.1      cgd 				exit(1);
    499   1.1      cgd 			}
    500  1.11  mycroft 			memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
    501  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    502  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    503  1.11  mycroft 				syslog(LOG_ERR,
    504  1.11  mycroft 				    "setsockopt SO_KEEPALIVE: %m");
    505  1.11  mycroft 			nfsdargs.sock = msgsock;
    506  1.11  mycroft 			nfsdargs.name = (caddr_t)&inetpeer;
    507  1.11  mycroft 			nfsdargs.namelen = sizeof(inetpeer);
    508  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    509  1.11  mycroft 			(void)close(msgsock);
    510  1.11  mycroft 		}
    511  1.11  mycroft #ifdef notyet
    512  1.11  mycroft 		if (tp4flag && FD_ISSET(tp4sock, &ready)) {
    513  1.11  mycroft 			len = sizeof(isopeer);
    514  1.11  mycroft 			if ((msgsock = accept(tp4sock,
    515  1.11  mycroft 			    (struct sockaddr *)&isopeer, &len)) < 0) {
    516  1.11  mycroft 				syslog(LOG_ERR, "accept failed: %m");
    517  1.11  mycroft 				exit(1);
    518   1.1      cgd 			}
    519  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    520  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    521  1.11  mycroft 				syslog(LOG_ERR,
    522  1.11  mycroft 				    "setsockopt SO_KEEPALIVE: %m");
    523  1.11  mycroft 			nfsdargs.sock = msgsock;
    524  1.11  mycroft 			nfsdargs.name = (caddr_t)&isopeer;
    525  1.11  mycroft 			nfsdargs.namelen = len;
    526  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    527  1.11  mycroft 			(void)close(msgsock);
    528  1.11  mycroft 		}
    529  1.11  mycroft 		if (tpipflag && FD_ISSET(tpipsock, &ready)) {
    530  1.11  mycroft 			len = sizeof(inetpeer);
    531  1.11  mycroft 			if ((msgsock = accept(tpipsock,
    532  1.11  mycroft 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
    533  1.11  mycroft 				syslog(LOG_ERR, "Accept failed: %m");
    534   1.1      cgd 				exit(1);
    535   1.1      cgd 			}
    536  1.11  mycroft 			if (setsockopt(msgsock, SOL_SOCKET,
    537  1.11  mycroft 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    538  1.11  mycroft 				syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
    539  1.11  mycroft 			nfsdargs.sock = msgsock;
    540  1.11  mycroft 			nfsdargs.name = (caddr_t)&inetpeer;
    541  1.11  mycroft 			nfsdargs.namelen = len;
    542  1.11  mycroft 			nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
    543  1.11  mycroft 			(void)close(msgsock);
    544   1.1      cgd 		}
    545  1.11  mycroft #endif /* notyet */
    546   1.1      cgd 	}
    547   1.1      cgd }
    548   1.1      cgd 
    549  1.11  mycroft void
    550   1.1      cgd usage()
    551   1.1      cgd {
    552  1.15  mycroft 
    553  1.15  mycroft 	(void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
    554   1.1      cgd 	exit(1);
    555   1.1      cgd }
    556   1.1      cgd 
    557   1.1      cgd void
    558  1.11  mycroft nonfs(signo)
    559  1.11  mycroft 	int signo;
    560   1.1      cgd {
    561  1.15  mycroft 
    562  1.11  mycroft 	syslog(LOG_ERR, "missing system call: NFS not available.");
    563   1.1      cgd }
    564   1.4    glass 
    565  1.11  mycroft void
    566  1.11  mycroft reapchild(signo)
    567  1.11  mycroft 	int signo;
    568   1.4    glass {
    569  1.11  mycroft 
    570  1.15  mycroft 	while (wait3((int *)0, WNOHANG, (struct rusage *)0) > 0);
    571   1.4    glass }
    572