Home | History | Annotate | Line # | Download | only in mountd
mountd.c revision 1.14
      1   1.1      cgd /*
      2  1.12  mycroft  * Copyright (c) 1989, 1993
      3  1.12  mycroft  *	The Regents of the University of California.  All rights reserved.
      4   1.1      cgd  *
      5   1.1      cgd  * This code is derived from software contributed to Berkeley by
      6  1.12  mycroft  * Herb Hasler and Rick Macklem at The University of Guelph.
      7   1.1      cgd  *
      8   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1      cgd  * modification, are permitted provided that the following conditions
     10   1.1      cgd  * are met:
     11   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1      cgd  *    must display the following acknowledgement:
     18   1.1      cgd  *	This product includes software developed by the University of
     19   1.1      cgd  *	California, Berkeley and its contributors.
     20   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21   1.1      cgd  *    may be used to endorse or promote products derived from this software
     22   1.1      cgd  *    without specific prior written permission.
     23   1.1      cgd  *
     24   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1      cgd  * SUCH DAMAGE.
     35   1.1      cgd  */
     36   1.1      cgd 
     37   1.1      cgd #ifndef lint
     38  1.12  mycroft static char copyright[] =
     39  1.12  mycroft "@(#) Copyright (c) 1989, 1993\n\
     40  1.12  mycroft 	The Regents of the University of California.  All rights reserved.\n";
     41   1.1      cgd #endif not lint
     42   1.1      cgd 
     43   1.1      cgd #ifndef lint
     44  1.12  mycroft /*static char sccsid[] = "from: @(#)mountd.c	8.8 (Berkeley) 2/20/94";*/
     45  1.14      cgd static char *rcsid = "$Id: mountd.c,v 1.14 1994/08/12 04:19:52 cgd Exp $";
     46   1.1      cgd #endif not lint
     47   1.1      cgd 
     48   1.1      cgd #include <sys/param.h>
     49  1.12  mycroft #include <sys/file.h>
     50   1.1      cgd #include <sys/ioctl.h>
     51   1.1      cgd #include <sys/mount.h>
     52   1.1      cgd #include <sys/socket.h>
     53  1.12  mycroft #include <sys/stat.h>
     54  1.12  mycroft #include <sys/syslog.h>
     55  1.12  mycroft #include <sys/ucred.h>
     56  1.12  mycroft 
     57   1.1      cgd #include <rpc/rpc.h>
     58   1.1      cgd #include <rpc/pmap_clnt.h>
     59   1.1      cgd #include <rpc/pmap_prot.h>
     60  1.12  mycroft #ifdef ISO
     61  1.12  mycroft #include <netiso/iso.h>
     62  1.12  mycroft #endif
     63  1.12  mycroft #include <nfs/rpcv2.h>
     64   1.1      cgd #include <nfs/nfsv2.h>
     65  1.12  mycroft 
     66  1.12  mycroft #include <arpa/inet.h>
     67  1.12  mycroft 
     68  1.12  mycroft #include <ctype.h>
     69  1.12  mycroft #include <errno.h>
     70  1.12  mycroft #include <grp.h>
     71  1.12  mycroft #include <netdb.h>
     72  1.12  mycroft #include <pwd.h>
     73  1.12  mycroft #include <signal.h>
     74  1.12  mycroft #include <stdio.h>
     75  1.12  mycroft #include <stdlib.h>
     76  1.12  mycroft #include <string.h>
     77  1.12  mycroft #include <unistd.h>
     78   1.1      cgd #include "pathnames.h"
     79   1.1      cgd 
     80  1.12  mycroft #ifdef DEBUG
     81  1.12  mycroft #include <stdarg.h>
     82  1.12  mycroft #endif
     83  1.12  mycroft 
     84   1.1      cgd /*
     85   1.1      cgd  * Structures for keeping the mount list and export list
     86   1.1      cgd  */
     87   1.1      cgd struct mountlist {
     88   1.1      cgd 	struct mountlist *ml_next;
     89  1.12  mycroft 	char	ml_host[RPCMNT_NAMELEN+1];
     90  1.12  mycroft 	char	ml_dirp[RPCMNT_PATHLEN+1];
     91   1.1      cgd };
     92   1.1      cgd 
     93  1.12  mycroft struct dirlist {
     94  1.12  mycroft 	struct dirlist	*dp_left;
     95  1.12  mycroft 	struct dirlist	*dp_right;
     96  1.12  mycroft 	int		dp_flag;
     97  1.12  mycroft 	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
     98  1.12  mycroft 	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
     99  1.12  mycroft };
    100  1.12  mycroft /* dp_flag bits */
    101  1.12  mycroft #define	DP_DEFSET	0x1
    102  1.12  mycroft 
    103   1.1      cgd struct exportlist {
    104   1.1      cgd 	struct exportlist *ex_next;
    105  1.12  mycroft 	struct dirlist	*ex_dirl;
    106  1.12  mycroft 	struct dirlist	*ex_defdir;
    107  1.12  mycroft 	int		ex_flag;
    108  1.12  mycroft 	fsid_t		ex_fs;
    109  1.12  mycroft 	char		*ex_fsdir;
    110  1.12  mycroft };
    111  1.12  mycroft /* ex_flag bits */
    112  1.12  mycroft #define	EX_LINKED	0x1
    113  1.12  mycroft 
    114  1.12  mycroft struct netmsk {
    115  1.12  mycroft 	u_long	nt_net;
    116  1.12  mycroft 	u_long	nt_mask;
    117  1.12  mycroft 	char *nt_name;
    118  1.12  mycroft };
    119  1.12  mycroft 
    120  1.12  mycroft union grouptypes {
    121  1.12  mycroft 	struct hostent *gt_hostent;
    122  1.12  mycroft 	struct netmsk	gt_net;
    123  1.12  mycroft #ifdef ISO
    124  1.12  mycroft 	struct sockaddr_iso *gt_isoaddr;
    125  1.12  mycroft #endif
    126   1.1      cgd };
    127   1.1      cgd 
    128   1.1      cgd struct grouplist {
    129  1.12  mycroft 	int gr_type;
    130  1.12  mycroft 	union grouptypes gr_ptr;
    131   1.1      cgd 	struct grouplist *gr_next;
    132  1.12  mycroft };
    133  1.12  mycroft /* Group types */
    134  1.12  mycroft #define	GT_NULL		0x0
    135  1.12  mycroft #define	GT_HOST		0x1
    136  1.12  mycroft #define	GT_NET		0x2
    137  1.12  mycroft #define	GT_ISO		0x4
    138  1.12  mycroft 
    139  1.12  mycroft struct hostlist {
    140  1.12  mycroft 	struct grouplist *ht_grp;
    141  1.12  mycroft 	struct hostlist	 *ht_next;
    142   1.1      cgd };
    143   1.1      cgd 
    144   1.1      cgd /* Global defs */
    145  1.12  mycroft char	*add_expdir __P((struct dirlist **, char *, int));
    146  1.12  mycroft void	add_dlist __P((struct dirlist **, struct dirlist *,
    147  1.12  mycroft 				struct grouplist *));
    148  1.12  mycroft void	add_mlist __P((char *, char *));
    149  1.12  mycroft int	check_dirpath __P((char *));
    150  1.12  mycroft int	check_options __P((struct dirlist *));
    151  1.12  mycroft int	chk_host __P((struct dirlist *, u_long, int *));
    152  1.12  mycroft void	del_mlist __P((char *, char *));
    153  1.12  mycroft struct dirlist *dirp_search __P((struct dirlist *, char *));
    154  1.12  mycroft int	do_mount __P((struct exportlist *, struct grouplist *, int,
    155  1.12  mycroft 				struct ucred *, char *, int, struct statfs *));
    156  1.12  mycroft int	do_opt __P((char **, char **, struct exportlist *, struct grouplist *,
    157  1.12  mycroft 				int *, int *, struct ucred *));
    158  1.12  mycroft struct	exportlist *ex_search __P((fsid_t *));
    159  1.12  mycroft struct	exportlist *get_exp __P((void));
    160  1.12  mycroft void	free_dir __P((struct dirlist *));
    161  1.12  mycroft void	free_exp __P((struct exportlist *));
    162  1.12  mycroft void	free_grp __P((struct grouplist *));
    163  1.12  mycroft void	free_host __P((struct hostlist *));
    164  1.12  mycroft void	get_exportlist __P((void));
    165  1.12  mycroft int	get_host __P((char *, struct grouplist *));
    166  1.12  mycroft struct hostlist *get_ht __P((void));
    167  1.12  mycroft int	get_line __P((void));
    168  1.12  mycroft void	get_mountlist __P((void));
    169  1.12  mycroft int	get_net __P((char *, struct netmsk *, int));
    170  1.12  mycroft void	getexp_err __P((struct exportlist *, struct grouplist *));
    171  1.12  mycroft struct grouplist *get_grp __P((void));
    172  1.12  mycroft void	hang_dirp __P((struct dirlist *, struct grouplist *,
    173  1.12  mycroft 				struct exportlist *, int));
    174  1.12  mycroft void	mntsrv __P((struct svc_req *, SVCXPRT *));
    175  1.12  mycroft void	nextfield __P((char **, char **));
    176  1.12  mycroft void	out_of_mem __P((void));
    177  1.12  mycroft void	parsecred __P((char *, struct ucred *));
    178  1.12  mycroft int	put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
    179  1.12  mycroft int	scan_tree __P((struct dirlist *, u_long));
    180  1.12  mycroft void	send_umntall __P((void));
    181  1.12  mycroft int	umntall_each __P((caddr_t, struct sockaddr_in *));
    182  1.12  mycroft int	xdr_dir __P((XDR *, char *));
    183  1.12  mycroft int	xdr_explist __P((XDR *, caddr_t));
    184  1.12  mycroft int	xdr_fhs __P((XDR *, nfsv2fh_t *));
    185  1.12  mycroft int	xdr_mlist __P((XDR *, caddr_t));
    186  1.12  mycroft 
    187  1.12  mycroft /* C library */
    188  1.12  mycroft int	getnetgrent();
    189  1.12  mycroft void	endnetgrent();
    190  1.12  mycroft void	setnetgrent();
    191  1.12  mycroft 
    192  1.12  mycroft #ifdef ISO
    193  1.12  mycroft struct iso_addr *iso_addr();
    194  1.12  mycroft #endif
    195  1.12  mycroft 
    196  1.12  mycroft struct exportlist *exphead;
    197   1.1      cgd struct mountlist *mlhead;
    198  1.12  mycroft struct grouplist *grphead;
    199   1.1      cgd char exname[MAXPATHLEN];
    200  1.12  mycroft struct ucred def_anon = {
    201  1.12  mycroft 	1,
    202  1.12  mycroft 	(uid_t) -2,
    203  1.12  mycroft 	1,
    204  1.12  mycroft 	{ (gid_t) -2 }
    205  1.12  mycroft };
    206  1.14      cgd int resvport_only = 1;
    207  1.12  mycroft int opt_flags;
    208  1.12  mycroft /* Bits for above */
    209  1.12  mycroft #define	OP_MAPROOT	0x01
    210  1.12  mycroft #define	OP_MAPALL	0x02
    211  1.12  mycroft #define	OP_KERB		0x04
    212  1.12  mycroft #define	OP_MASK		0x08
    213  1.12  mycroft #define	OP_NET		0x10
    214  1.12  mycroft #define	OP_ISO		0x20
    215  1.12  mycroft #define	OP_ALLDIRS	0x40
    216  1.12  mycroft 
    217  1.12  mycroft #ifdef DEBUG
    218  1.12  mycroft int debug = 1;
    219  1.12  mycroft void	SYSLOG __P((int, const char *, ...));
    220  1.12  mycroft #define syslog SYSLOG
    221  1.12  mycroft #else
    222  1.12  mycroft int debug = 0;
    223  1.12  mycroft #endif
    224   1.1      cgd 
    225   1.1      cgd /*
    226   1.1      cgd  * Mountd server for NFS mount protocol as described in:
    227   1.1      cgd  * NFS: Network File System Protocol Specification, RFC1094, Appendix A
    228   1.1      cgd  * The optional arguments are the exports file name
    229   1.1      cgd  * default: _PATH_EXPORTS
    230   1.1      cgd  * and "-n" to allow nonroot mount.
    231   1.1      cgd  */
    232  1.12  mycroft int
    233   1.1      cgd main(argc, argv)
    234   1.1      cgd 	int argc;
    235   1.1      cgd 	char **argv;
    236   1.1      cgd {
    237  1.12  mycroft 	SVCXPRT *transp;
    238   1.6   brezak 	int c;
    239   1.1      cgd 
    240   1.1      cgd 	while ((c = getopt(argc, argv, "n")) != EOF)
    241   1.1      cgd 		switch (c) {
    242   1.1      cgd 		case 'n':
    243  1.14      cgd 			resvport_only = 0;
    244   1.1      cgd 			break;
    245   1.1      cgd 		default:
    246   1.1      cgd 			fprintf(stderr, "Usage: mountd [-n] [export_file]\n");
    247   1.1      cgd 			exit(1);
    248   1.1      cgd 		};
    249   1.1      cgd 	argc -= optind;
    250   1.1      cgd 	argv += optind;
    251  1.12  mycroft 	grphead = (struct grouplist *)NULL;
    252  1.12  mycroft 	exphead = (struct exportlist *)NULL;
    253  1.12  mycroft 	mlhead = (struct mountlist *)NULL;
    254   1.1      cgd 	if (argc == 1) {
    255   1.1      cgd 		strncpy(exname, *argv, MAXPATHLEN-1);
    256   1.1      cgd 		exname[MAXPATHLEN-1] = '\0';
    257   1.1      cgd 	} else
    258   1.1      cgd 		strcpy(exname, _PATH_EXPORTS);
    259  1.12  mycroft 	openlog("mountd", LOG_PID, LOG_DAEMON);
    260  1.12  mycroft 	if (debug)
    261  1.12  mycroft 		fprintf(stderr,"Getting export list.\n");
    262  1.12  mycroft 	get_exportlist();
    263  1.12  mycroft 	if (debug)
    264  1.12  mycroft 		fprintf(stderr,"Getting mount list.\n");
    265  1.12  mycroft 	get_mountlist();
    266  1.12  mycroft 	if (debug)
    267  1.12  mycroft 		fprintf(stderr,"Here we go.\n");
    268  1.12  mycroft 	if (debug == 0) {
    269   1.1      cgd 		daemon(0, 0);
    270   1.1      cgd 		signal(SIGINT, SIG_IGN);
    271   1.1      cgd 		signal(SIGQUIT, SIG_IGN);
    272   1.1      cgd 	}
    273  1.12  mycroft 	signal(SIGHUP, (void (*) __P((int))) get_exportlist);
    274  1.12  mycroft 	signal(SIGTERM, (void (*) __P((int))) send_umntall);
    275  1.12  mycroft 	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
    276  1.12  mycroft 	  if (pidfile != NULL) {
    277  1.12  mycroft 		fprintf(pidfile, "%d\n", getpid());
    278  1.12  mycroft 		fclose(pidfile);
    279  1.12  mycroft 	  }
    280  1.12  mycroft 	}
    281  1.12  mycroft 	if ((transp = svcudp_create(RPC_ANYSOCK)) == NULL) {
    282  1.12  mycroft 		syslog(LOG_ERR, "Can't create socket");
    283   1.1      cgd 		exit(1);
    284   1.1      cgd 	}
    285  1.12  mycroft 	pmap_unset(RPCPROG_MNT, RPCMNT_VER1);
    286  1.12  mycroft 	if (!svc_register(transp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
    287  1.12  mycroft 	    IPPROTO_UDP)) {
    288   1.1      cgd 		syslog(LOG_ERR, "Can't register mount");
    289   1.1      cgd 		exit(1);
    290   1.1      cgd 	}
    291   1.1      cgd 	svc_run();
    292   1.1      cgd 	syslog(LOG_ERR, "Mountd died");
    293   1.1      cgd 	exit(1);
    294   1.1      cgd }
    295   1.1      cgd 
    296   1.1      cgd /*
    297   1.1      cgd  * The mount rpc service
    298   1.1      cgd  */
    299   1.6   brezak void
    300   1.1      cgd mntsrv(rqstp, transp)
    301  1.12  mycroft 	struct svc_req *rqstp;
    302  1.12  mycroft 	SVCXPRT *transp;
    303   1.1      cgd {
    304  1.12  mycroft 	struct exportlist *ep;
    305  1.12  mycroft 	struct dirlist *dp;
    306   1.1      cgd 	nfsv2fh_t nfh;
    307   1.1      cgd 	struct stat stb;
    308  1.12  mycroft 	struct statfs fsb;
    309   1.1      cgd 	struct hostent *hp;
    310   1.1      cgd 	u_long saddr;
    311  1.14      cgd 	u_short sport;
    312  1.12  mycroft 	char rpcpath[RPCMNT_PATHLEN+1], dirpath[MAXPATHLEN];
    313  1.12  mycroft 	int bad = ENOENT, omask, defset;
    314   1.1      cgd 
    315   1.1      cgd 	saddr = transp->xp_raddr.sin_addr.s_addr;
    316  1.14      cgd 	sport = ntohs(transp->xp_raddr.sin_port);
    317  1.12  mycroft 	hp = (struct hostent *)NULL;
    318   1.1      cgd 	switch (rqstp->rq_proc) {
    319   1.1      cgd 	case NULLPROC:
    320  1.12  mycroft 		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
    321   1.1      cgd 			syslog(LOG_ERR, "Can't send reply");
    322   1.1      cgd 		return;
    323  1.12  mycroft 	case RPCMNT_MOUNT:
    324  1.14      cgd 		if (sport >= IPPORT_RESERVED && resvport_only) {
    325   1.1      cgd 			svcerr_weakauth(transp);
    326   1.1      cgd 			return;
    327   1.1      cgd 		}
    328  1.12  mycroft 		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
    329   1.1      cgd 			svcerr_decode(transp);
    330   1.1      cgd 			return;
    331   1.1      cgd 		}
    332   1.1      cgd 
    333  1.12  mycroft 		/*
    334  1.14      cgd 		 * Get the real pathname and make sure it is a file or
    335  1.14      cgd 		 * directory that exists.
    336  1.12  mycroft 		 */
    337  1.12  mycroft 		if (realpath(rpcpath, dirpath) == 0 ||
    338  1.12  mycroft 		    stat(dirpath, &stb) < 0 ||
    339  1.12  mycroft 		    (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) ||
    340  1.12  mycroft 		    statfs(dirpath, &fsb) < 0) {
    341  1.12  mycroft 			chdir("/");	/* Just in case realpath doesn't */
    342  1.12  mycroft 			if (debug)
    343  1.12  mycroft 				fprintf(stderr, "stat failed on %s\n", dirpath);
    344   1.1      cgd 			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
    345   1.1      cgd 				syslog(LOG_ERR, "Can't send reply");
    346   1.1      cgd 			return;
    347   1.1      cgd 		}
    348   1.1      cgd 
    349   1.1      cgd 		/* Check in the exports list */
    350   1.1      cgd 		omask = sigblock(sigmask(SIGHUP));
    351  1.12  mycroft 		ep = ex_search(&fsb.f_fsid);
    352  1.12  mycroft 		defset = 0;
    353  1.12  mycroft 		if (ep && (chk_host(ep->ex_defdir, saddr, &defset) ||
    354  1.12  mycroft 		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
    355  1.12  mycroft 		     chk_host(dp, saddr, &defset)) ||
    356  1.12  mycroft 		     (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
    357  1.12  mycroft 		      scan_tree(ep->ex_dirl, saddr) == 0))) {
    358  1.12  mycroft 			/* Get the file handle */
    359  1.12  mycroft 			bzero((caddr_t)&nfh, sizeof(nfh));
    360  1.12  mycroft 			if (getfh(dirpath, (fhandle_t *)&nfh) < 0) {
    361  1.12  mycroft 				bad = errno;
    362  1.12  mycroft 				syslog(LOG_ERR, "Can't get fh for %s", dirpath);
    363  1.12  mycroft 				if (!svc_sendreply(transp, xdr_long,
    364  1.12  mycroft 				    (caddr_t)&bad))
    365  1.12  mycroft 					syslog(LOG_ERR, "Can't send reply");
    366  1.12  mycroft 				sigsetmask(omask);
    367  1.12  mycroft 				return;
    368   1.1      cgd 			}
    369  1.12  mycroft 			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&nfh))
    370  1.12  mycroft 				syslog(LOG_ERR, "Can't send reply");
    371  1.12  mycroft 			if (hp == NULL)
    372  1.12  mycroft 				hp = gethostbyaddr((caddr_t)&saddr,
    373  1.12  mycroft 				    sizeof(saddr), AF_INET);
    374  1.12  mycroft 			if (hp)
    375  1.12  mycroft 				add_mlist(hp->h_name, dirpath);
    376  1.12  mycroft 			else
    377  1.12  mycroft 				add_mlist(inet_ntoa(transp->xp_raddr.sin_addr),
    378  1.12  mycroft 					dirpath);
    379  1.12  mycroft 			if (debug)
    380  1.12  mycroft 				fprintf(stderr,"Mount successfull.\n");
    381  1.12  mycroft 		} else {
    382   1.1      cgd 			bad = EACCES;
    383   1.1      cgd 			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
    384   1.1      cgd 				syslog(LOG_ERR, "Can't send reply");
    385   1.1      cgd 		}
    386  1.12  mycroft 		sigsetmask(omask);
    387   1.1      cgd 		return;
    388  1.12  mycroft 	case RPCMNT_DUMP:
    389  1.12  mycroft 		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
    390   1.1      cgd 			syslog(LOG_ERR, "Can't send reply");
    391   1.1      cgd 		return;
    392  1.12  mycroft 	case RPCMNT_UMOUNT:
    393  1.14      cgd 		if (sport >= IPPORT_RESERVED && resvport_only) {
    394   1.1      cgd 			svcerr_weakauth(transp);
    395   1.1      cgd 			return;
    396   1.1      cgd 		}
    397   1.1      cgd 		if (!svc_getargs(transp, xdr_dir, dirpath)) {
    398   1.1      cgd 			svcerr_decode(transp);
    399   1.1      cgd 			return;
    400   1.1      cgd 		}
    401  1.12  mycroft 		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
    402   1.1      cgd 			syslog(LOG_ERR, "Can't send reply");
    403   1.1      cgd 		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
    404   1.1      cgd 		if (hp)
    405   1.1      cgd 			del_mlist(hp->h_name, dirpath);
    406  1.12  mycroft 		del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), dirpath);
    407   1.1      cgd 		return;
    408  1.12  mycroft 	case RPCMNT_UMNTALL:
    409  1.14      cgd 		if (sport >= IPPORT_RESERVED && resvport_only) {
    410   1.1      cgd 			svcerr_weakauth(transp);
    411   1.1      cgd 			return;
    412   1.1      cgd 		}
    413  1.12  mycroft 		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
    414   1.1      cgd 			syslog(LOG_ERR, "Can't send reply");
    415   1.1      cgd 		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
    416   1.1      cgd 		if (hp)
    417  1.12  mycroft 			del_mlist(hp->h_name, (char *)NULL);
    418  1.12  mycroft 		del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), (char *)NULL);
    419   1.1      cgd 		return;
    420  1.12  mycroft 	case RPCMNT_EXPORT:
    421  1.12  mycroft 		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
    422   1.1      cgd 			syslog(LOG_ERR, "Can't send reply");
    423   1.1      cgd 		return;
    424   1.1      cgd 	default:
    425   1.1      cgd 		svcerr_noproc(transp);
    426   1.1      cgd 		return;
    427   1.1      cgd 	}
    428   1.1      cgd }
    429   1.1      cgd 
    430   1.1      cgd /*
    431   1.1      cgd  * Xdr conversion for a dirpath string
    432   1.1      cgd  */
    433  1.12  mycroft int
    434   1.1      cgd xdr_dir(xdrsp, dirp)
    435   1.1      cgd 	XDR *xdrsp;
    436   1.1      cgd 	char *dirp;
    437   1.1      cgd {
    438  1.12  mycroft 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    439   1.1      cgd }
    440   1.1      cgd 
    441   1.1      cgd /*
    442   1.1      cgd  * Xdr routine to generate fhstatus
    443   1.1      cgd  */
    444  1.12  mycroft int
    445   1.1      cgd xdr_fhs(xdrsp, nfh)
    446   1.1      cgd 	XDR *xdrsp;
    447   1.1      cgd 	nfsv2fh_t *nfh;
    448   1.1      cgd {
    449  1.12  mycroft 	int ok = 0;
    450   1.1      cgd 
    451   1.1      cgd 	if (!xdr_long(xdrsp, &ok))
    452   1.1      cgd 		return (0);
    453   1.1      cgd 	return (xdr_opaque(xdrsp, (caddr_t)nfh, NFSX_FH));
    454   1.1      cgd }
    455   1.1      cgd 
    456  1.12  mycroft int
    457   1.1      cgd xdr_mlist(xdrsp, cp)
    458   1.1      cgd 	XDR *xdrsp;
    459   1.1      cgd 	caddr_t cp;
    460   1.1      cgd {
    461  1.12  mycroft 	struct mountlist *mlp;
    462   1.1      cgd 	int true = 1;
    463   1.1      cgd 	int false = 0;
    464   1.1      cgd 	char *strp;
    465   1.1      cgd 
    466   1.1      cgd 	mlp = mlhead;
    467   1.1      cgd 	while (mlp) {
    468   1.1      cgd 		if (!xdr_bool(xdrsp, &true))
    469   1.1      cgd 			return (0);
    470   1.1      cgd 		strp = &mlp->ml_host[0];
    471  1.12  mycroft 		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
    472   1.1      cgd 			return (0);
    473   1.1      cgd 		strp = &mlp->ml_dirp[0];
    474  1.12  mycroft 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
    475   1.1      cgd 			return (0);
    476   1.1      cgd 		mlp = mlp->ml_next;
    477   1.1      cgd 	}
    478   1.1      cgd 	if (!xdr_bool(xdrsp, &false))
    479   1.1      cgd 		return (0);
    480   1.1      cgd 	return (1);
    481   1.1      cgd }
    482   1.1      cgd 
    483   1.1      cgd /*
    484   1.1      cgd  * Xdr conversion for export list
    485   1.1      cgd  */
    486  1.12  mycroft int
    487   1.1      cgd xdr_explist(xdrsp, cp)
    488   1.1      cgd 	XDR *xdrsp;
    489   1.1      cgd 	caddr_t cp;
    490   1.1      cgd {
    491  1.12  mycroft 	struct exportlist *ep;
    492   1.1      cgd 	int false = 0;
    493  1.12  mycroft 	int omask, putdef;
    494   1.1      cgd 
    495   1.1      cgd 	omask = sigblock(sigmask(SIGHUP));
    496  1.12  mycroft 	ep = exphead;
    497  1.12  mycroft 	while (ep) {
    498  1.12  mycroft 		putdef = 0;
    499  1.12  mycroft 		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
    500   1.1      cgd 			goto errout;
    501  1.12  mycroft 		if (ep->ex_defdir && putdef == 0 &&
    502  1.12  mycroft 			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
    503  1.12  mycroft 			&putdef))
    504   1.1      cgd 			goto errout;
    505   1.1      cgd 		ep = ep->ex_next;
    506   1.1      cgd 	}
    507   1.1      cgd 	sigsetmask(omask);
    508   1.1      cgd 	if (!xdr_bool(xdrsp, &false))
    509   1.1      cgd 		return (0);
    510   1.1      cgd 	return (1);
    511   1.1      cgd errout:
    512   1.1      cgd 	sigsetmask(omask);
    513   1.1      cgd 	return (0);
    514   1.1      cgd }
    515   1.1      cgd 
    516  1.12  mycroft /*
    517  1.12  mycroft  * Called from xdr_explist() to traverse the tree and export the
    518  1.12  mycroft  * directory paths.
    519  1.12  mycroft  */
    520  1.12  mycroft int
    521  1.12  mycroft put_exlist(dp, xdrsp, adp, putdefp)
    522  1.12  mycroft 	struct dirlist *dp;
    523  1.12  mycroft 	XDR *xdrsp;
    524  1.12  mycroft 	struct dirlist *adp;
    525  1.12  mycroft 	int *putdefp;
    526  1.12  mycroft {
    527  1.12  mycroft 	struct grouplist *grp;
    528  1.12  mycroft 	struct hostlist *hp;
    529  1.12  mycroft 	int true = 1;
    530  1.12  mycroft 	int false = 0;
    531  1.12  mycroft 	int gotalldir = 0;
    532  1.12  mycroft 	char *strp;
    533  1.12  mycroft 
    534  1.12  mycroft 	if (dp) {
    535  1.12  mycroft 		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
    536  1.12  mycroft 			return (1);
    537  1.12  mycroft 		if (!xdr_bool(xdrsp, &true))
    538  1.12  mycroft 			return (1);
    539  1.12  mycroft 		strp = dp->dp_dirp;
    540  1.12  mycroft 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
    541  1.12  mycroft 			return (1);
    542  1.12  mycroft 		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
    543  1.12  mycroft 			gotalldir = 1;
    544  1.12  mycroft 			*putdefp = 1;
    545  1.12  mycroft 		}
    546  1.12  mycroft 		if ((dp->dp_flag & DP_DEFSET) == 0 &&
    547  1.12  mycroft 		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
    548  1.12  mycroft 			hp = dp->dp_hosts;
    549  1.12  mycroft 			while (hp) {
    550  1.12  mycroft 				grp = hp->ht_grp;
    551  1.12  mycroft 				if (grp->gr_type == GT_HOST) {
    552  1.12  mycroft 					if (!xdr_bool(xdrsp, &true))
    553  1.12  mycroft 						return (1);
    554  1.12  mycroft 					strp = grp->gr_ptr.gt_hostent->h_name;
    555  1.12  mycroft 					if (!xdr_string(xdrsp, &strp,
    556  1.12  mycroft 					    RPCMNT_NAMELEN))
    557  1.12  mycroft 						return (1);
    558  1.12  mycroft 				} else if (grp->gr_type == GT_NET) {
    559  1.12  mycroft 					if (!xdr_bool(xdrsp, &true))
    560  1.12  mycroft 						return (1);
    561  1.12  mycroft 					strp = grp->gr_ptr.gt_net.nt_name;
    562  1.12  mycroft 					if (!xdr_string(xdrsp, &strp,
    563  1.12  mycroft 					    RPCMNT_NAMELEN))
    564  1.12  mycroft 						return (1);
    565  1.12  mycroft 				}
    566  1.12  mycroft 				hp = hp->ht_next;
    567  1.12  mycroft 				if (gotalldir && hp == (struct hostlist *)NULL) {
    568  1.12  mycroft 					hp = adp->dp_hosts;
    569  1.12  mycroft 					gotalldir = 0;
    570  1.12  mycroft 				}
    571  1.12  mycroft 			}
    572  1.12  mycroft 		}
    573  1.12  mycroft 		if (!xdr_bool(xdrsp, &false))
    574  1.12  mycroft 			return (1);
    575  1.12  mycroft 		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
    576  1.12  mycroft 			return (1);
    577  1.12  mycroft 	}
    578  1.12  mycroft 	return (0);
    579  1.12  mycroft }
    580  1.12  mycroft 
    581   1.1      cgd #define LINESIZ	10240
    582   1.1      cgd char line[LINESIZ];
    583  1.12  mycroft FILE *exp_file;
    584   1.1      cgd 
    585   1.1      cgd /*
    586   1.1      cgd  * Get the export list
    587   1.1      cgd  */
    588   1.1      cgd void
    589   1.1      cgd get_exportlist()
    590   1.1      cgd {
    591  1.12  mycroft 	struct exportlist *ep, *ep2;
    592  1.12  mycroft 	struct grouplist *grp, *tgrp;
    593  1.12  mycroft 	struct exportlist **epp;
    594  1.12  mycroft 	struct dirlist *dirhead;
    595  1.12  mycroft 	struct statfs fsb, *fsp;
    596  1.12  mycroft 	struct hostent *hpe;
    597  1.12  mycroft 	struct ucred anon;
    598  1.12  mycroft 	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
    599  1.12  mycroft 	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
    600  1.12  mycroft 
    601   1.1      cgd 	/*
    602   1.1      cgd 	 * First, get rid of the old list
    603   1.1      cgd 	 */
    604  1.12  mycroft 	ep = exphead;
    605  1.12  mycroft 	while (ep) {
    606   1.1      cgd 		ep2 = ep;
    607   1.1      cgd 		ep = ep->ex_next;
    608   1.1      cgd 		free_exp(ep2);
    609   1.1      cgd 	}
    610  1.12  mycroft 	exphead = (struct exportlist *)NULL;
    611  1.12  mycroft 
    612  1.12  mycroft 	grp = grphead;
    613  1.12  mycroft 	while (grp) {
    614  1.12  mycroft 		tgrp = grp;
    615  1.12  mycroft 		grp = grp->gr_next;
    616  1.12  mycroft 		free_grp(tgrp);
    617  1.12  mycroft 	}
    618  1.12  mycroft 	grphead = (struct grouplist *)NULL;
    619  1.12  mycroft 
    620  1.12  mycroft 	/*
    621  1.12  mycroft 	 * And delete exports that are in the kernel for all local
    622  1.12  mycroft 	 * file systems.
    623  1.12  mycroft 	 * XXX: Should know how to handle all local exportable file systems
    624  1.12  mycroft 	 *      instead of just MOUNT_UFS.
    625  1.12  mycroft 	 */
    626  1.12  mycroft 	num = getmntinfo(&fsp, MNT_NOWAIT);
    627  1.12  mycroft 	for (i = 0; i < num; i++) {
    628  1.12  mycroft 		union {
    629  1.12  mycroft 			struct ufs_args ua;
    630  1.12  mycroft 			struct iso_args ia;
    631  1.12  mycroft 			struct mfs_args ma;
    632  1.13       ws 			struct msdosfs_args da;
    633  1.12  mycroft 		} targs;
    634  1.12  mycroft 
    635  1.12  mycroft 		if (!strcmp(fsp->f_fstypename, MOUNT_MFS) ||
    636  1.12  mycroft 		    !strcmp(fsp->f_fstypename, MOUNT_UFS) ||
    637  1.13       ws 		    !strcmp(fsp->f_fstypename, MOUNT_MSDOS) ||
    638  1.12  mycroft 		    !strcmp(fsp->f_fstypename, MOUNT_CD9660)) {
    639  1.12  mycroft 			targs.ua.fspec = NULL;
    640  1.12  mycroft 			targs.ua.export.ex_flags = MNT_DELEXPORT;
    641  1.12  mycroft 			if (mount(fsp->f_fstypename, fsp->f_mntonname,
    642  1.12  mycroft 				  fsp->f_flags | MNT_UPDATE,
    643  1.12  mycroft 				  (caddr_t)&targs) < 0)
    644  1.12  mycroft 				syslog(LOG_ERR, "Can't delete exports for %s",
    645  1.12  mycroft 				       fsp->f_mntonname);
    646  1.12  mycroft 		}
    647  1.12  mycroft 		fsp++;
    648  1.12  mycroft 	}
    649   1.1      cgd 
    650   1.1      cgd 	/*
    651   1.1      cgd 	 * Read in the exports file and build the list, calling
    652  1.12  mycroft 	 * mount() as we go along to push the export rules into the kernel.
    653   1.1      cgd 	 */
    654  1.12  mycroft 	if ((exp_file = fopen(exname, "r")) == NULL) {
    655   1.1      cgd 		syslog(LOG_ERR, "Can't open %s", exname);
    656   1.1      cgd 		exit(2);
    657   1.1      cgd 	}
    658  1.12  mycroft 	dirhead = (struct dirlist *)NULL;
    659  1.12  mycroft 	while (get_line()) {
    660  1.12  mycroft 		if (debug)
    661  1.12  mycroft 			fprintf(stderr,"Got line %s\n",line);
    662   1.1      cgd 		cp = line;
    663   1.1      cgd 		nextfield(&cp, &endcp);
    664  1.12  mycroft 		if (*cp == '#')
    665  1.12  mycroft 			goto nextline;
    666   1.1      cgd 
    667   1.1      cgd 		/*
    668  1.12  mycroft 		 * Set defaults.
    669   1.1      cgd 		 */
    670  1.12  mycroft 		has_host = FALSE;
    671  1.12  mycroft 		anon = def_anon;
    672  1.12  mycroft 		exflags = MNT_EXPORTED;
    673  1.12  mycroft 		got_nondir = 0;
    674  1.12  mycroft 		opt_flags = 0;
    675  1.12  mycroft 		ep = (struct exportlist *)NULL;
    676   1.1      cgd 
    677   1.1      cgd 		/*
    678   1.1      cgd 		 * Create new exports list entry
    679   1.1      cgd 		 */
    680   1.1      cgd 		len = endcp-cp;
    681  1.12  mycroft 		tgrp = grp = get_grp();
    682   1.1      cgd 		while (len > 0) {
    683  1.12  mycroft 			if (len > RPCMNT_NAMELEN) {
    684  1.12  mycroft 			    getexp_err(ep, tgrp);
    685  1.12  mycroft 			    goto nextline;
    686  1.12  mycroft 			}
    687   1.1      cgd 			if (*cp == '-') {
    688  1.12  mycroft 			    if (ep == (struct exportlist *)NULL) {
    689  1.12  mycroft 				getexp_err(ep, tgrp);
    690  1.12  mycroft 				goto nextline;
    691  1.12  mycroft 			    }
    692  1.12  mycroft 			    if (debug)
    693  1.12  mycroft 				fprintf(stderr, "doing opt %s\n", cp);
    694  1.12  mycroft 			    got_nondir = 1;
    695  1.12  mycroft 			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
    696  1.12  mycroft 				&exflags, &anon)) {
    697  1.12  mycroft 				getexp_err(ep, tgrp);
    698  1.12  mycroft 				goto nextline;
    699  1.12  mycroft 			    }
    700  1.12  mycroft 			} else if (*cp == '/') {
    701  1.12  mycroft 			    savedc = *endcp;
    702  1.12  mycroft 			    *endcp = '\0';
    703  1.12  mycroft 			    if (check_dirpath(cp) &&
    704  1.12  mycroft 				statfs(cp, &fsb) >= 0) {
    705  1.12  mycroft 				if (got_nondir) {
    706  1.12  mycroft 				    syslog(LOG_ERR, "Dirs must be first");
    707  1.12  mycroft 				    getexp_err(ep, tgrp);
    708  1.12  mycroft 				    goto nextline;
    709  1.12  mycroft 				}
    710  1.12  mycroft 				if (ep) {
    711  1.12  mycroft 				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
    712  1.12  mycroft 					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
    713  1.12  mycroft 					getexp_err(ep, tgrp);
    714  1.12  mycroft 					goto nextline;
    715  1.12  mycroft 				    }
    716  1.12  mycroft 				} else {
    717  1.12  mycroft 				    /*
    718  1.12  mycroft 				     * See if this directory is already
    719  1.12  mycroft 				     * in the list.
    720  1.12  mycroft 				     */
    721  1.12  mycroft 				    ep = ex_search(&fsb.f_fsid);
    722  1.12  mycroft 				    if (ep == (struct exportlist *)NULL) {
    723  1.12  mycroft 					ep = get_exp();
    724  1.12  mycroft 					ep->ex_fs = fsb.f_fsid;
    725  1.12  mycroft 					ep->ex_fsdir = (char *)
    726  1.12  mycroft 					    malloc(strlen(fsb.f_mntonname) + 1);
    727  1.12  mycroft 					if (ep->ex_fsdir)
    728  1.12  mycroft 					    strcpy(ep->ex_fsdir,
    729  1.12  mycroft 						fsb.f_mntonname);
    730  1.12  mycroft 					else
    731  1.12  mycroft 					    out_of_mem();
    732  1.12  mycroft 					if (debug)
    733  1.12  mycroft 					  fprintf(stderr,
    734  1.12  mycroft 					      "Making new ep fs=0x%x,0x%x\n",
    735  1.12  mycroft 					      fsb.f_fsid.val[0],
    736  1.12  mycroft 					      fsb.f_fsid.val[1]);
    737  1.12  mycroft 				    } else if (debug)
    738  1.12  mycroft 					fprintf(stderr,
    739  1.12  mycroft 					    "Found ep fs=0x%x,0x%x\n",
    740  1.12  mycroft 					    fsb.f_fsid.val[0],
    741  1.12  mycroft 					    fsb.f_fsid.val[1]);
    742  1.12  mycroft 				}
    743  1.12  mycroft 
    744  1.12  mycroft 				/*
    745  1.12  mycroft 				 * Add dirpath to export mount point.
    746  1.12  mycroft 				 */
    747  1.12  mycroft 				dirp = add_expdir(&dirhead, cp, len);
    748  1.12  mycroft 				dirplen = len;
    749  1.12  mycroft 			    } else {
    750  1.12  mycroft 				getexp_err(ep, tgrp);
    751  1.12  mycroft 				goto nextline;
    752  1.12  mycroft 			    }
    753  1.12  mycroft 			    *endcp = savedc;
    754  1.12  mycroft 			} else {
    755  1.12  mycroft 			    savedc = *endcp;
    756  1.12  mycroft 			    *endcp = '\0';
    757  1.12  mycroft 			    got_nondir = 1;
    758  1.12  mycroft 			    if (ep == (struct exportlist *)NULL) {
    759  1.12  mycroft 				getexp_err(ep, tgrp);
    760  1.12  mycroft 				goto nextline;
    761  1.12  mycroft 			    }
    762  1.12  mycroft 
    763  1.12  mycroft 			    /*
    764  1.12  mycroft 			     * Get the host or netgroup.
    765  1.12  mycroft 			     */
    766  1.12  mycroft 			    setnetgrent(cp);
    767  1.12  mycroft 			    netgrp = getnetgrent(&hst, &usr, &dom);
    768  1.12  mycroft 			    do {
    769  1.12  mycroft 				if (has_host) {
    770  1.12  mycroft 				    grp->gr_next = get_grp();
    771  1.12  mycroft 				    grp = grp->gr_next;
    772   1.1      cgd 				}
    773  1.12  mycroft 				if (netgrp) {
    774  1.12  mycroft 				    if (get_host(hst, grp)) {
    775  1.12  mycroft 					syslog(LOG_ERR, "Bad netgroup %s", cp);
    776  1.12  mycroft 					getexp_err(ep, tgrp);
    777  1.12  mycroft 					goto nextline;
    778  1.12  mycroft 				    }
    779  1.12  mycroft 				} else if (get_host(cp, grp)) {
    780  1.12  mycroft 				    getexp_err(ep, tgrp);
    781  1.12  mycroft 				    goto nextline;
    782  1.12  mycroft 				}
    783  1.12  mycroft 				has_host = TRUE;
    784  1.12  mycroft 			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
    785  1.12  mycroft 			    endnetgrent();
    786  1.12  mycroft 			    *endcp = savedc;
    787   1.1      cgd 			}
    788   1.1      cgd 			cp = endcp;
    789   1.1      cgd 			nextfield(&cp, &endcp);
    790   1.1      cgd 			len = endcp - cp;
    791   1.1      cgd 		}
    792  1.12  mycroft 		if (check_options(dirhead)) {
    793  1.12  mycroft 			getexp_err(ep, tgrp);
    794  1.12  mycroft 			goto nextline;
    795  1.12  mycroft 		}
    796  1.12  mycroft 		if (!has_host) {
    797  1.12  mycroft 			grp->gr_type = GT_HOST;
    798  1.12  mycroft 			if (debug)
    799  1.12  mycroft 				fprintf(stderr,"Adding a default entry\n");
    800  1.12  mycroft 			/* add a default group and make the grp list NULL */
    801  1.12  mycroft 			hpe = (struct hostent *)malloc(sizeof(struct hostent));
    802  1.12  mycroft 			if (hpe == (struct hostent *)NULL)
    803  1.12  mycroft 				out_of_mem();
    804  1.12  mycroft 			hpe->h_name = "Default";
    805  1.12  mycroft 			hpe->h_addrtype = AF_INET;
    806  1.12  mycroft 			hpe->h_length = sizeof (u_long);
    807  1.12  mycroft 			hpe->h_addr_list = (char **)NULL;
    808  1.12  mycroft 			grp->gr_ptr.gt_hostent = hpe;
    809  1.12  mycroft 
    810  1.12  mycroft 		/*
    811  1.12  mycroft 		 * Don't allow a network export coincide with a list of
    812  1.12  mycroft 		 * host(s) on the same line.
    813  1.12  mycroft 		 */
    814  1.12  mycroft 		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
    815  1.12  mycroft 			getexp_err(ep, tgrp);
    816  1.12  mycroft 			goto nextline;
    817  1.12  mycroft 		}
    818  1.12  mycroft 
    819  1.12  mycroft 		/*
    820  1.12  mycroft 		 * Loop through hosts, pushing the exports into the kernel.
    821  1.12  mycroft 		 * After loop, tgrp points to the start of the list and
    822  1.12  mycroft 		 * grp points to the last entry in the list.
    823  1.12  mycroft 		 */
    824  1.12  mycroft 		grp = tgrp;
    825  1.12  mycroft 		do {
    826  1.12  mycroft 		    if (do_mount(ep, grp, exflags, &anon, dirp,
    827  1.12  mycroft 			dirplen, &fsb)) {
    828  1.12  mycroft 			getexp_err(ep, tgrp);
    829  1.12  mycroft 			goto nextline;
    830  1.12  mycroft 		    }
    831  1.12  mycroft 		} while (grp->gr_next && (grp = grp->gr_next));
    832  1.12  mycroft 
    833  1.12  mycroft 		/*
    834  1.12  mycroft 		 * Success. Update the data structures.
    835  1.12  mycroft 		 */
    836  1.12  mycroft 		if (has_host) {
    837  1.12  mycroft 			hang_dirp(dirhead, tgrp, ep, (opt_flags & OP_ALLDIRS));
    838  1.12  mycroft 			grp->gr_next = grphead;
    839  1.12  mycroft 			grphead = tgrp;
    840  1.12  mycroft 		} else {
    841  1.12  mycroft 			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
    842  1.12  mycroft 			(opt_flags & OP_ALLDIRS));
    843  1.12  mycroft 			free_grp(grp);
    844  1.12  mycroft 		}
    845  1.12  mycroft 		dirhead = (struct dirlist *)NULL;
    846  1.12  mycroft 		if ((ep->ex_flag & EX_LINKED) == 0) {
    847  1.12  mycroft 			ep2 = exphead;
    848  1.12  mycroft 			epp = &exphead;
    849  1.12  mycroft 
    850  1.12  mycroft 			/*
    851  1.12  mycroft 			 * Insert in the list in alphabetical order.
    852  1.12  mycroft 			 */
    853  1.12  mycroft 			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
    854  1.12  mycroft 				epp = &ep2->ex_next;
    855  1.12  mycroft 				ep2 = ep2->ex_next;
    856  1.12  mycroft 			}
    857  1.12  mycroft 			if (ep2)
    858  1.12  mycroft 				ep->ex_next = ep2;
    859  1.12  mycroft 			*epp = ep;
    860  1.12  mycroft 			ep->ex_flag |= EX_LINKED;
    861  1.12  mycroft 		}
    862  1.12  mycroft nextline:
    863  1.12  mycroft 		if (dirhead) {
    864  1.12  mycroft 			free_dir(dirhead);
    865  1.12  mycroft 			dirhead = (struct dirlist *)NULL;
    866  1.12  mycroft 		}
    867  1.12  mycroft 	}
    868  1.12  mycroft 	fclose(exp_file);
    869  1.12  mycroft }
    870  1.12  mycroft 
    871  1.12  mycroft /*
    872  1.12  mycroft  * Allocate an export list element
    873  1.12  mycroft  */
    874  1.12  mycroft struct exportlist *
    875  1.12  mycroft get_exp()
    876  1.12  mycroft {
    877  1.12  mycroft 	struct exportlist *ep;
    878  1.12  mycroft 
    879  1.12  mycroft 	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
    880  1.12  mycroft 	if (ep == (struct exportlist *)NULL)
    881  1.12  mycroft 		out_of_mem();
    882  1.12  mycroft 	bzero((caddr_t)ep, sizeof (struct exportlist));
    883  1.12  mycroft 	return (ep);
    884  1.12  mycroft }
    885  1.12  mycroft 
    886  1.12  mycroft /*
    887  1.12  mycroft  * Allocate a group list element
    888  1.12  mycroft  */
    889  1.12  mycroft struct grouplist *
    890  1.12  mycroft get_grp()
    891  1.12  mycroft {
    892  1.12  mycroft 	struct grouplist *gp;
    893  1.12  mycroft 
    894  1.12  mycroft 	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
    895  1.12  mycroft 	if (gp == (struct grouplist *)NULL)
    896  1.12  mycroft 		out_of_mem();
    897  1.12  mycroft 	bzero((caddr_t)gp, sizeof (struct grouplist));
    898  1.12  mycroft 	return (gp);
    899  1.12  mycroft }
    900  1.12  mycroft 
    901  1.12  mycroft /*
    902  1.12  mycroft  * Clean up upon an error in get_exportlist().
    903  1.12  mycroft  */
    904  1.12  mycroft void
    905  1.12  mycroft getexp_err(ep, grp)
    906  1.12  mycroft 	struct exportlist *ep;
    907  1.12  mycroft 	struct grouplist *grp;
    908  1.12  mycroft {
    909  1.12  mycroft 	struct grouplist *tgrp;
    910  1.12  mycroft 
    911  1.12  mycroft 	syslog(LOG_ERR, "Bad exports list line %s", line);
    912  1.12  mycroft 	if (ep && (ep->ex_flag & EX_LINKED) == 0)
    913  1.12  mycroft 		free_exp(ep);
    914  1.12  mycroft 	while (grp) {
    915  1.12  mycroft 		tgrp = grp;
    916  1.12  mycroft 		grp = grp->gr_next;
    917  1.12  mycroft 		free_grp(tgrp);
    918  1.12  mycroft 	}
    919  1.12  mycroft }
    920  1.12  mycroft 
    921  1.12  mycroft /*
    922  1.12  mycroft  * Search the export list for a matching fs.
    923  1.12  mycroft  */
    924  1.12  mycroft struct exportlist *
    925  1.12  mycroft ex_search(fsid)
    926  1.12  mycroft 	fsid_t *fsid;
    927  1.12  mycroft {
    928  1.12  mycroft 	struct exportlist *ep;
    929  1.12  mycroft 
    930  1.12  mycroft 	ep = exphead;
    931  1.12  mycroft 	while (ep) {
    932  1.12  mycroft 		if (ep->ex_fs.val[0] == fsid->val[0] &&
    933  1.12  mycroft 		    ep->ex_fs.val[1] == fsid->val[1])
    934  1.12  mycroft 			return (ep);
    935  1.12  mycroft 		ep = ep->ex_next;
    936  1.12  mycroft 	}
    937  1.12  mycroft 	return (ep);
    938  1.12  mycroft }
    939  1.12  mycroft 
    940  1.12  mycroft /*
    941  1.12  mycroft  * Add a directory path to the list.
    942  1.12  mycroft  */
    943  1.12  mycroft char *
    944  1.12  mycroft add_expdir(dpp, cp, len)
    945  1.12  mycroft 	struct dirlist **dpp;
    946  1.12  mycroft 	char *cp;
    947  1.12  mycroft 	int len;
    948  1.12  mycroft {
    949  1.12  mycroft 	struct dirlist *dp;
    950  1.12  mycroft 
    951  1.12  mycroft 	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
    952  1.12  mycroft 	dp->dp_left = *dpp;
    953  1.12  mycroft 	dp->dp_right = (struct dirlist *)NULL;
    954  1.12  mycroft 	dp->dp_flag = 0;
    955  1.12  mycroft 	dp->dp_hosts = (struct hostlist *)NULL;
    956  1.12  mycroft 	strcpy(dp->dp_dirp, cp);
    957  1.12  mycroft 	*dpp = dp;
    958  1.12  mycroft 	return (dp->dp_dirp);
    959  1.12  mycroft }
    960  1.12  mycroft 
    961  1.12  mycroft /*
    962  1.12  mycroft  * Hang the dir list element off the dirpath binary tree as required
    963  1.12  mycroft  * and update the entry for host.
    964  1.12  mycroft  */
    965  1.12  mycroft void
    966  1.12  mycroft hang_dirp(dp, grp, ep, alldirs)
    967  1.12  mycroft 	struct dirlist *dp;
    968  1.12  mycroft 	struct grouplist *grp;
    969  1.12  mycroft 	struct exportlist *ep;
    970  1.12  mycroft 	int alldirs;
    971  1.12  mycroft {
    972  1.12  mycroft 	struct hostlist *hp;
    973  1.12  mycroft 	struct dirlist *dp2;
    974  1.12  mycroft 
    975  1.12  mycroft 	if (alldirs) {
    976  1.12  mycroft 		if (ep->ex_defdir)
    977  1.12  mycroft 			free((caddr_t)dp);
    978  1.12  mycroft 		else
    979  1.12  mycroft 			ep->ex_defdir = dp;
    980  1.12  mycroft 		if (grp == (struct grouplist *)NULL)
    981  1.12  mycroft 			ep->ex_defdir->dp_flag |= DP_DEFSET;
    982  1.12  mycroft 		else while (grp) {
    983  1.12  mycroft 			hp = get_ht();
    984  1.12  mycroft 			hp->ht_grp = grp;
    985  1.12  mycroft 			hp->ht_next = ep->ex_defdir->dp_hosts;
    986  1.12  mycroft 			ep->ex_defdir->dp_hosts = hp;
    987  1.12  mycroft 			grp = grp->gr_next;
    988  1.12  mycroft 		}
    989  1.12  mycroft 	} else {
    990  1.12  mycroft 
    991  1.12  mycroft 		/*
    992  1.12  mycroft 		 * Loop throught the directories adding them to the tree.
    993  1.12  mycroft 		 */
    994  1.12  mycroft 		while (dp) {
    995  1.12  mycroft 			dp2 = dp->dp_left;
    996  1.12  mycroft 			add_dlist(&ep->ex_dirl, dp, grp);
    997  1.12  mycroft 			dp = dp2;
    998  1.12  mycroft 		}
    999  1.12  mycroft 	}
   1000  1.12  mycroft }
   1001  1.12  mycroft 
   1002  1.12  mycroft /*
   1003  1.12  mycroft  * Traverse the binary tree either updating a node that is already there
   1004  1.12  mycroft  * for the new directory or adding the new node.
   1005  1.12  mycroft  */
   1006  1.12  mycroft void
   1007  1.12  mycroft add_dlist(dpp, newdp, grp)
   1008  1.12  mycroft 	struct dirlist **dpp;
   1009  1.12  mycroft 	struct dirlist *newdp;
   1010  1.12  mycroft 	struct grouplist *grp;
   1011  1.12  mycroft {
   1012  1.12  mycroft 	struct dirlist *dp;
   1013  1.12  mycroft 	struct hostlist *hp;
   1014  1.12  mycroft 	int cmp;
   1015  1.12  mycroft 
   1016  1.12  mycroft 	dp = *dpp;
   1017  1.12  mycroft 	if (dp) {
   1018  1.12  mycroft 		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
   1019  1.12  mycroft 		if (cmp > 0) {
   1020  1.12  mycroft 			add_dlist(&dp->dp_left, newdp, grp);
   1021  1.12  mycroft 			return;
   1022  1.12  mycroft 		} else if (cmp < 0) {
   1023  1.12  mycroft 			add_dlist(&dp->dp_right, newdp, grp);
   1024  1.12  mycroft 			return;
   1025  1.12  mycroft 		} else
   1026  1.12  mycroft 			free((caddr_t)newdp);
   1027  1.12  mycroft 	} else {
   1028  1.12  mycroft 		dp = newdp;
   1029  1.12  mycroft 		dp->dp_left = (struct dirlist *)NULL;
   1030  1.12  mycroft 		*dpp = dp;
   1031  1.12  mycroft 	}
   1032  1.12  mycroft 	if (grp) {
   1033  1.12  mycroft 
   1034  1.12  mycroft 		/*
   1035  1.12  mycroft 		 * Hang all of the host(s) off of the directory point.
   1036  1.12  mycroft 		 */
   1037  1.12  mycroft 		do {
   1038  1.12  mycroft 			hp = get_ht();
   1039  1.12  mycroft 			hp->ht_grp = grp;
   1040  1.12  mycroft 			hp->ht_next = dp->dp_hosts;
   1041  1.12  mycroft 			dp->dp_hosts = hp;
   1042  1.12  mycroft 			grp = grp->gr_next;
   1043  1.12  mycroft 		} while (grp);
   1044  1.12  mycroft 	} else
   1045  1.12  mycroft 		dp->dp_flag |= DP_DEFSET;
   1046  1.12  mycroft }
   1047  1.12  mycroft 
   1048  1.12  mycroft /*
   1049  1.12  mycroft  * Search for a dirpath on the export point.
   1050  1.12  mycroft  */
   1051  1.12  mycroft struct dirlist *
   1052  1.12  mycroft dirp_search(dp, dirpath)
   1053  1.12  mycroft 	struct dirlist *dp;
   1054  1.12  mycroft 	char *dirpath;
   1055  1.12  mycroft {
   1056  1.12  mycroft 	int cmp;
   1057  1.12  mycroft 
   1058  1.12  mycroft 	if (dp) {
   1059  1.12  mycroft 		cmp = strcmp(dp->dp_dirp, dirpath);
   1060  1.12  mycroft 		if (cmp > 0)
   1061  1.12  mycroft 			return (dirp_search(dp->dp_left, dirpath));
   1062  1.12  mycroft 		else if (cmp < 0)
   1063  1.12  mycroft 			return (dirp_search(dp->dp_right, dirpath));
   1064  1.12  mycroft 		else
   1065  1.12  mycroft 			return (dp);
   1066  1.12  mycroft 	}
   1067  1.12  mycroft 	return (dp);
   1068  1.12  mycroft }
   1069  1.12  mycroft 
   1070  1.12  mycroft /*
   1071  1.12  mycroft  * Scan for a host match in a directory tree.
   1072  1.12  mycroft  */
   1073  1.12  mycroft int
   1074  1.12  mycroft chk_host(dp, saddr, defsetp)
   1075  1.12  mycroft 	struct dirlist *dp;
   1076  1.12  mycroft 	u_long saddr;
   1077  1.12  mycroft 	int *defsetp;
   1078  1.12  mycroft {
   1079  1.12  mycroft 	struct hostlist *hp;
   1080  1.12  mycroft 	struct grouplist *grp;
   1081  1.12  mycroft 	u_long **addrp;
   1082  1.12  mycroft 
   1083  1.12  mycroft 	if (dp) {
   1084  1.12  mycroft 		if (dp->dp_flag & DP_DEFSET)
   1085  1.12  mycroft 			*defsetp = 1;
   1086  1.12  mycroft 		hp = dp->dp_hosts;
   1087  1.12  mycroft 		while (hp) {
   1088  1.12  mycroft 			grp = hp->ht_grp;
   1089  1.12  mycroft 			switch (grp->gr_type) {
   1090  1.12  mycroft 			case GT_HOST:
   1091  1.12  mycroft 			    addrp = (u_long **)
   1092  1.12  mycroft 				grp->gr_ptr.gt_hostent->h_addr_list;
   1093  1.12  mycroft 			    while (*addrp) {
   1094  1.12  mycroft 				if (**addrp == saddr)
   1095  1.12  mycroft 				    return (1);
   1096  1.12  mycroft 				addrp++;
   1097  1.12  mycroft 			    }
   1098  1.12  mycroft 			    break;
   1099  1.12  mycroft 			case GT_NET:
   1100  1.12  mycroft 			    if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
   1101  1.12  mycroft 				grp->gr_ptr.gt_net.nt_net)
   1102  1.12  mycroft 				return (1);
   1103  1.12  mycroft 			    break;
   1104  1.12  mycroft 			};
   1105  1.12  mycroft 			hp = hp->ht_next;
   1106  1.12  mycroft 		}
   1107  1.12  mycroft 	}
   1108  1.12  mycroft 	return (0);
   1109  1.12  mycroft }
   1110  1.12  mycroft 
   1111  1.12  mycroft /*
   1112  1.12  mycroft  * Scan tree for a host that matches the address.
   1113  1.12  mycroft  */
   1114  1.12  mycroft int
   1115  1.12  mycroft scan_tree(dp, saddr)
   1116  1.12  mycroft 	struct dirlist *dp;
   1117  1.12  mycroft 	u_long saddr;
   1118  1.12  mycroft {
   1119  1.12  mycroft 	int defset;
   1120  1.12  mycroft 
   1121  1.12  mycroft 	if (dp) {
   1122  1.12  mycroft 		if (scan_tree(dp->dp_left, saddr))
   1123  1.12  mycroft 			return (1);
   1124  1.12  mycroft 		if (chk_host(dp, saddr, &defset))
   1125  1.12  mycroft 			return (1);
   1126  1.12  mycroft 		if (scan_tree(dp->dp_right, saddr))
   1127  1.12  mycroft 			return (1);
   1128  1.12  mycroft 	}
   1129  1.12  mycroft 	return (0);
   1130  1.12  mycroft }
   1131  1.12  mycroft 
   1132  1.12  mycroft /*
   1133  1.12  mycroft  * Traverse the dirlist tree and free it up.
   1134  1.12  mycroft  */
   1135  1.12  mycroft void
   1136  1.12  mycroft free_dir(dp)
   1137  1.12  mycroft 	struct dirlist *dp;
   1138  1.12  mycroft {
   1139  1.12  mycroft 
   1140  1.12  mycroft 	if (dp) {
   1141  1.12  mycroft 		free_dir(dp->dp_left);
   1142  1.12  mycroft 		free_dir(dp->dp_right);
   1143  1.12  mycroft 		free_host(dp->dp_hosts);
   1144  1.12  mycroft 		free((caddr_t)dp);
   1145  1.12  mycroft 	}
   1146  1.12  mycroft }
   1147  1.12  mycroft 
   1148  1.12  mycroft /*
   1149  1.12  mycroft  * Parse the option string and update fields.
   1150  1.12  mycroft  * Option arguments may either be -<option>=<value> or
   1151  1.12  mycroft  * -<option> <value>
   1152  1.12  mycroft  */
   1153  1.12  mycroft int
   1154  1.12  mycroft do_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
   1155  1.12  mycroft 	char **cpp, **endcpp;
   1156  1.12  mycroft 	struct exportlist *ep;
   1157  1.12  mycroft 	struct grouplist *grp;
   1158  1.12  mycroft 	int *has_hostp;
   1159  1.12  mycroft 	int *exflagsp;
   1160  1.12  mycroft 	struct ucred *cr;
   1161  1.12  mycroft {
   1162  1.12  mycroft 	char *cpoptarg, *cpoptend;
   1163  1.12  mycroft 	char *cp, *endcp, *cpopt, savedc, savedc2;
   1164  1.12  mycroft 	int allflag, usedarg;
   1165  1.12  mycroft 
   1166  1.12  mycroft 	cpopt = *cpp;
   1167  1.12  mycroft 	cpopt++;
   1168  1.12  mycroft 	cp = *endcpp;
   1169  1.12  mycroft 	savedc = *cp;
   1170  1.12  mycroft 	*cp = '\0';
   1171  1.12  mycroft 	while (cpopt && *cpopt) {
   1172  1.12  mycroft 		allflag = 1;
   1173  1.12  mycroft 		usedarg = -2;
   1174  1.12  mycroft 		if (cpoptend = index(cpopt, ',')) {
   1175  1.12  mycroft 			*cpoptend++ = '\0';
   1176  1.12  mycroft 			if (cpoptarg = index(cpopt, '='))
   1177  1.12  mycroft 				*cpoptarg++ = '\0';
   1178  1.12  mycroft 		} else {
   1179  1.12  mycroft 			if (cpoptarg = index(cpopt, '='))
   1180  1.12  mycroft 				*cpoptarg++ = '\0';
   1181  1.12  mycroft 			else {
   1182  1.12  mycroft 				*cp = savedc;
   1183  1.12  mycroft 				nextfield(&cp, &endcp);
   1184  1.12  mycroft 				**endcpp = '\0';
   1185  1.12  mycroft 				if (endcp > cp && *cp != '-') {
   1186  1.12  mycroft 					cpoptarg = cp;
   1187  1.12  mycroft 					savedc2 = *endcp;
   1188  1.12  mycroft 					*endcp = '\0';
   1189  1.12  mycroft 					usedarg = 0;
   1190   1.1      cgd 				}
   1191   1.1      cgd 			}
   1192  1.12  mycroft 		}
   1193  1.12  mycroft 		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
   1194  1.12  mycroft 			*exflagsp |= MNT_EXRDONLY;
   1195  1.12  mycroft 		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
   1196  1.12  mycroft 		    !(allflag = strcmp(cpopt, "mapall")) ||
   1197  1.12  mycroft 		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
   1198  1.12  mycroft 			usedarg++;
   1199  1.12  mycroft 			parsecred(cpoptarg, cr);
   1200  1.12  mycroft 			if (allflag == 0) {
   1201  1.12  mycroft 				*exflagsp |= MNT_EXPORTANON;
   1202  1.12  mycroft 				opt_flags |= OP_MAPALL;
   1203  1.12  mycroft 			} else
   1204  1.12  mycroft 				opt_flags |= OP_MAPROOT;
   1205  1.12  mycroft 		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
   1206  1.12  mycroft 			*exflagsp |= MNT_EXKERB;
   1207  1.12  mycroft 			opt_flags |= OP_KERB;
   1208  1.12  mycroft 		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
   1209  1.12  mycroft 			!strcmp(cpopt, "m"))) {
   1210  1.12  mycroft 			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
   1211  1.12  mycroft 				syslog(LOG_ERR, "Bad mask: %s", cpoptarg);
   1212  1.12  mycroft 				return (1);
   1213  1.12  mycroft 			}
   1214  1.12  mycroft 			usedarg++;
   1215  1.12  mycroft 			opt_flags |= OP_MASK;
   1216  1.12  mycroft 		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
   1217  1.12  mycroft 			!strcmp(cpopt, "n"))) {
   1218  1.12  mycroft 			if (grp->gr_type != GT_NULL) {
   1219  1.12  mycroft 				syslog(LOG_ERR, "Network/host conflict");
   1220  1.12  mycroft 				return (1);
   1221  1.12  mycroft 			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
   1222  1.12  mycroft 				syslog(LOG_ERR, "Bad net: %s", cpoptarg);
   1223  1.12  mycroft 				return (1);
   1224  1.12  mycroft 			}
   1225  1.12  mycroft 			grp->gr_type = GT_NET;
   1226  1.12  mycroft 			*has_hostp = 1;
   1227  1.12  mycroft 			usedarg++;
   1228  1.12  mycroft 			opt_flags |= OP_NET;
   1229  1.12  mycroft 		} else if (!strcmp(cpopt, "alldirs")) {
   1230  1.12  mycroft 			opt_flags |= OP_ALLDIRS;
   1231  1.12  mycroft #ifdef ISO
   1232  1.12  mycroft 		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
   1233  1.12  mycroft 			if (get_isoaddr(cpoptarg, grp)) {
   1234  1.12  mycroft 				syslog(LOG_ERR, "Bad iso addr: %s", cpoptarg);
   1235  1.12  mycroft 				return (1);
   1236  1.12  mycroft 			}
   1237  1.12  mycroft 			*has_hostp = 1;
   1238  1.12  mycroft 			usedarg++;
   1239  1.12  mycroft 			opt_flags |= OP_ISO;
   1240  1.12  mycroft #endif /* ISO */
   1241  1.12  mycroft 		} else {
   1242  1.12  mycroft 			syslog(LOG_ERR, "Bad opt %s", cpopt);
   1243  1.12  mycroft 			return (1);
   1244  1.12  mycroft 		}
   1245  1.12  mycroft 		if (usedarg >= 0) {
   1246  1.12  mycroft 			*endcp = savedc2;
   1247  1.12  mycroft 			**endcpp = savedc;
   1248  1.12  mycroft 			if (usedarg > 0) {
   1249  1.12  mycroft 				*cpp = cp;
   1250  1.12  mycroft 				*endcpp = endcp;
   1251  1.12  mycroft 			}
   1252  1.12  mycroft 			return (0);
   1253  1.12  mycroft 		}
   1254  1.12  mycroft 		cpopt = cpoptend;
   1255  1.12  mycroft 	}
   1256  1.12  mycroft 	**endcpp = savedc;
   1257  1.12  mycroft 	return (0);
   1258  1.12  mycroft }
   1259  1.12  mycroft 
   1260  1.12  mycroft /*
   1261  1.12  mycroft  * Translate a character string to the corresponding list of network
   1262  1.12  mycroft  * addresses for a hostname.
   1263  1.12  mycroft  */
   1264  1.12  mycroft int
   1265  1.12  mycroft get_host(cp, grp)
   1266  1.12  mycroft 	char *cp;
   1267  1.12  mycroft 	struct grouplist *grp;
   1268  1.12  mycroft {
   1269  1.12  mycroft 	struct hostent *hp, *nhp;
   1270  1.12  mycroft 	char **addrp, **naddrp;
   1271  1.12  mycroft 	struct hostent t_host;
   1272  1.12  mycroft 	int i;
   1273  1.12  mycroft 	u_long saddr;
   1274  1.12  mycroft 	char *aptr[2];
   1275  1.12  mycroft 
   1276  1.12  mycroft 	if (grp->gr_type != GT_NULL)
   1277  1.12  mycroft 		return (1);
   1278  1.12  mycroft 	if ((hp = gethostbyname(cp)) == NULL) {
   1279  1.12  mycroft 		if (isdigit(*cp)) {
   1280  1.12  mycroft 			saddr = inet_addr(cp);
   1281  1.12  mycroft 			if (saddr == -1) {
   1282  1.12  mycroft 				syslog(LOG_ERR, "Inet_addr failed");
   1283  1.12  mycroft 				return (1);
   1284  1.12  mycroft 			}
   1285  1.12  mycroft 			if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
   1286  1.12  mycroft 				AF_INET)) == NULL) {
   1287  1.12  mycroft 				hp = &t_host;
   1288  1.12  mycroft 				hp->h_name = cp;
   1289  1.12  mycroft 				hp->h_addrtype = AF_INET;
   1290  1.12  mycroft 				hp->h_length = sizeof (u_long);
   1291  1.12  mycroft 				hp->h_addr_list = aptr;
   1292  1.12  mycroft 				aptr[0] = (char *)&saddr;
   1293  1.12  mycroft 				aptr[1] = (char *)NULL;
   1294  1.12  mycroft 			}
   1295  1.12  mycroft 		} else {
   1296  1.12  mycroft 			syslog(LOG_ERR, "Gethostbyname failed");
   1297  1.12  mycroft 			return (1);
   1298  1.12  mycroft 		}
   1299  1.12  mycroft 	}
   1300  1.12  mycroft 	grp->gr_type = GT_HOST;
   1301  1.12  mycroft 	nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
   1302  1.12  mycroft 		malloc(sizeof(struct hostent));
   1303  1.12  mycroft 	if (nhp == (struct hostent *)NULL)
   1304  1.12  mycroft 		out_of_mem();
   1305  1.12  mycroft 	bcopy((caddr_t)hp, (caddr_t)nhp,
   1306  1.12  mycroft 		sizeof(struct hostent));
   1307  1.12  mycroft 	i = strlen(hp->h_name)+1;
   1308  1.12  mycroft 	nhp->h_name = (char *)malloc(i);
   1309  1.12  mycroft 	if (nhp->h_name == (char *)NULL)
   1310  1.12  mycroft 		out_of_mem();
   1311  1.12  mycroft 	bcopy(hp->h_name, nhp->h_name, i);
   1312  1.12  mycroft 	addrp = hp->h_addr_list;
   1313  1.12  mycroft 	i = 1;
   1314  1.12  mycroft 	while (*addrp++)
   1315  1.12  mycroft 		i++;
   1316  1.12  mycroft 	naddrp = nhp->h_addr_list = (char **)
   1317  1.12  mycroft 		malloc(i*sizeof(char *));
   1318  1.12  mycroft 	if (naddrp == (char **)NULL)
   1319  1.12  mycroft 		out_of_mem();
   1320  1.12  mycroft 	addrp = hp->h_addr_list;
   1321  1.12  mycroft 	while (*addrp) {
   1322  1.12  mycroft 		*naddrp = (char *)
   1323  1.12  mycroft 		    malloc(hp->h_length);
   1324  1.12  mycroft 		if (*naddrp == (char *)NULL)
   1325  1.12  mycroft 		    out_of_mem();
   1326  1.12  mycroft 		bcopy(*addrp, *naddrp,
   1327  1.12  mycroft 			hp->h_length);
   1328  1.12  mycroft 		addrp++;
   1329  1.12  mycroft 		naddrp++;
   1330  1.12  mycroft 	}
   1331  1.12  mycroft 	*naddrp = (char *)NULL;
   1332  1.12  mycroft 	if (debug)
   1333  1.12  mycroft 		fprintf(stderr, "got host %s\n", hp->h_name);
   1334  1.12  mycroft 	return (0);
   1335  1.12  mycroft }
   1336  1.12  mycroft 
   1337  1.12  mycroft /*
   1338  1.12  mycroft  * Free up an exports list component
   1339  1.12  mycroft  */
   1340  1.12  mycroft void
   1341  1.12  mycroft free_exp(ep)
   1342  1.12  mycroft 	struct exportlist *ep;
   1343  1.12  mycroft {
   1344  1.12  mycroft 
   1345  1.12  mycroft 	if (ep->ex_defdir) {
   1346  1.12  mycroft 		free_host(ep->ex_defdir->dp_hosts);
   1347  1.12  mycroft 		free((caddr_t)ep->ex_defdir);
   1348  1.12  mycroft 	}
   1349  1.12  mycroft 	if (ep->ex_fsdir)
   1350  1.12  mycroft 		free(ep->ex_fsdir);
   1351  1.12  mycroft 	free_dir(ep->ex_dirl);
   1352  1.12  mycroft 	free((caddr_t)ep);
   1353  1.12  mycroft }
   1354  1.12  mycroft 
   1355  1.12  mycroft /*
   1356  1.12  mycroft  * Free hosts.
   1357  1.12  mycroft  */
   1358  1.12  mycroft void
   1359  1.12  mycroft free_host(hp)
   1360  1.12  mycroft 	struct hostlist *hp;
   1361  1.12  mycroft {
   1362  1.12  mycroft 	struct hostlist *hp2;
   1363  1.12  mycroft 
   1364  1.12  mycroft 	while (hp) {
   1365  1.12  mycroft 		hp2 = hp;
   1366  1.12  mycroft 		hp = hp->ht_next;
   1367  1.12  mycroft 		free((caddr_t)hp2);
   1368  1.12  mycroft 	}
   1369  1.12  mycroft }
   1370  1.12  mycroft 
   1371  1.12  mycroft struct hostlist *
   1372  1.12  mycroft get_ht()
   1373  1.12  mycroft {
   1374  1.12  mycroft 	struct hostlist *hp;
   1375  1.12  mycroft 
   1376  1.12  mycroft 	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
   1377  1.12  mycroft 	if (hp == (struct hostlist *)NULL)
   1378  1.12  mycroft 		out_of_mem();
   1379  1.12  mycroft 	hp->ht_next = (struct hostlist *)NULL;
   1380  1.12  mycroft 	return (hp);
   1381  1.12  mycroft }
   1382  1.12  mycroft 
   1383  1.12  mycroft #ifdef ISO
   1384  1.12  mycroft /*
   1385  1.12  mycroft  * Translate an iso address.
   1386  1.12  mycroft  */
   1387  1.12  mycroft get_isoaddr(cp, grp)
   1388  1.12  mycroft 	char *cp;
   1389  1.12  mycroft 	struct grouplist *grp;
   1390  1.12  mycroft {
   1391  1.12  mycroft 	struct iso_addr *isop;
   1392  1.12  mycroft 	struct sockaddr_iso *isoaddr;
   1393  1.12  mycroft 
   1394  1.12  mycroft 	if (grp->gr_type != GT_NULL)
   1395  1.12  mycroft 		return (1);
   1396  1.12  mycroft 	if ((isop = iso_addr(cp)) == NULL) {
   1397  1.12  mycroft 		syslog(LOG_ERR,
   1398  1.12  mycroft 		    "iso_addr failed, ignored");
   1399  1.12  mycroft 		return (1);
   1400  1.12  mycroft 	}
   1401  1.12  mycroft 	isoaddr = (struct sockaddr_iso *)
   1402  1.12  mycroft 	    malloc(sizeof (struct sockaddr_iso));
   1403  1.12  mycroft 	if (isoaddr == (struct sockaddr_iso *)NULL)
   1404  1.12  mycroft 		out_of_mem();
   1405  1.12  mycroft 	bzero((caddr_t)isoaddr, sizeof (struct sockaddr_iso));
   1406  1.12  mycroft 	bcopy((caddr_t)isop, (caddr_t)&isoaddr->siso_addr,
   1407  1.12  mycroft 		sizeof (struct iso_addr));
   1408  1.12  mycroft 	isoaddr->siso_len = sizeof (struct sockaddr_iso);
   1409  1.12  mycroft 	isoaddr->siso_family = AF_ISO;
   1410  1.12  mycroft 	grp->gr_type = GT_ISO;
   1411  1.12  mycroft 	grp->gr_ptr.gt_isoaddr = isoaddr;
   1412  1.12  mycroft 	return (0);
   1413  1.12  mycroft }
   1414  1.12  mycroft #endif	/* ISO */
   1415  1.12  mycroft 
   1416  1.12  mycroft /*
   1417  1.12  mycroft  * Out of memory, fatal
   1418  1.12  mycroft  */
   1419  1.12  mycroft void
   1420  1.12  mycroft out_of_mem()
   1421  1.12  mycroft {
   1422  1.12  mycroft 
   1423  1.12  mycroft 	syslog(LOG_ERR, "Out of memory");
   1424  1.12  mycroft 	exit(2);
   1425  1.12  mycroft }
   1426  1.12  mycroft 
   1427  1.12  mycroft /*
   1428  1.12  mycroft  * Do the mount syscall with the update flag to push the export info into
   1429  1.12  mycroft  * the kernel.
   1430  1.12  mycroft  */
   1431  1.12  mycroft int
   1432  1.12  mycroft do_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
   1433  1.12  mycroft 	struct exportlist *ep;
   1434  1.12  mycroft 	struct grouplist *grp;
   1435  1.12  mycroft 	int exflags;
   1436  1.12  mycroft 	struct ucred *anoncrp;
   1437  1.12  mycroft 	char *dirp;
   1438  1.12  mycroft 	int dirplen;
   1439  1.12  mycroft 	struct statfs *fsb;
   1440  1.12  mycroft {
   1441  1.12  mycroft 	char *cp = (char *)NULL;
   1442  1.12  mycroft 	u_long **addrp;
   1443  1.12  mycroft 	int done;
   1444  1.12  mycroft 	char savedc = '\0';
   1445  1.12  mycroft 	struct sockaddr_in sin, imask;
   1446  1.12  mycroft 	union {
   1447  1.12  mycroft 		struct ufs_args ua;
   1448  1.12  mycroft 		struct iso_args ia;
   1449  1.12  mycroft 		struct mfs_args ma;
   1450  1.13       ws 		struct msdosfs_args da;
   1451  1.12  mycroft 	} args;
   1452  1.12  mycroft 	u_long net;
   1453  1.12  mycroft 
   1454  1.12  mycroft 	args.ua.fspec = 0;
   1455  1.12  mycroft 	args.ua.export.ex_flags = exflags;
   1456  1.12  mycroft 	args.ua.export.ex_anon = *anoncrp;
   1457  1.12  mycroft 	bzero((char *)&sin, sizeof(sin));
   1458  1.12  mycroft 	bzero((char *)&imask, sizeof(imask));
   1459  1.12  mycroft 	sin.sin_family = AF_INET;
   1460  1.12  mycroft 	sin.sin_len = sizeof(sin);
   1461  1.12  mycroft 	imask.sin_family = AF_INET;
   1462  1.12  mycroft 	imask.sin_len = sizeof(sin);
   1463  1.12  mycroft 	if (grp->gr_type == GT_HOST)
   1464  1.12  mycroft 		addrp = (u_long **)grp->gr_ptr.gt_hostent->h_addr_list;
   1465  1.12  mycroft 	else
   1466  1.12  mycroft 		addrp = (u_long **)NULL;
   1467  1.12  mycroft 	done = FALSE;
   1468  1.12  mycroft 	while (!done) {
   1469  1.12  mycroft 		switch (grp->gr_type) {
   1470  1.12  mycroft 		case GT_HOST:
   1471  1.12  mycroft 			if (addrp) {
   1472  1.12  mycroft 				sin.sin_addr.s_addr = **addrp;
   1473  1.12  mycroft 				args.ua.export.ex_addrlen = sizeof(sin);
   1474  1.12  mycroft 			} else
   1475  1.12  mycroft 				args.ua.export.ex_addrlen = 0;
   1476  1.12  mycroft 			args.ua.export.ex_addr = (struct sockaddr *)&sin;
   1477  1.12  mycroft 			args.ua.export.ex_masklen = 0;
   1478  1.12  mycroft 			break;
   1479  1.12  mycroft 		case GT_NET:
   1480  1.12  mycroft 			if (grp->gr_ptr.gt_net.nt_mask)
   1481  1.12  mycroft 			    imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
   1482  1.12  mycroft 			else {
   1483  1.12  mycroft 			    net = ntohl(grp->gr_ptr.gt_net.nt_net);
   1484  1.12  mycroft 			    if (IN_CLASSA(net))
   1485  1.12  mycroft 				imask.sin_addr.s_addr = inet_addr("255.0.0.0");
   1486  1.12  mycroft 			    else if (IN_CLASSB(net))
   1487  1.12  mycroft 				imask.sin_addr.s_addr =
   1488  1.12  mycroft 				    inet_addr("255.255.0.0");
   1489  1.12  mycroft 			    else
   1490  1.12  mycroft 				imask.sin_addr.s_addr =
   1491  1.12  mycroft 				    inet_addr("255.255.255.0");
   1492  1.12  mycroft 			    grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
   1493  1.12  mycroft 			}
   1494  1.12  mycroft 			sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
   1495  1.12  mycroft 			args.ua.export.ex_addr = (struct sockaddr *)&sin;
   1496  1.12  mycroft 			args.ua.export.ex_addrlen = sizeof (sin);
   1497  1.12  mycroft 			args.ua.export.ex_mask = (struct sockaddr *)&imask;
   1498  1.12  mycroft 			args.ua.export.ex_masklen = sizeof (imask);
   1499  1.12  mycroft 			break;
   1500  1.12  mycroft #ifdef ISO
   1501  1.12  mycroft 		case GT_ISO:
   1502  1.12  mycroft 			args.ua.export.ex_addr =
   1503  1.12  mycroft 				(struct sockaddr *)grp->gr_ptr.gt_isoaddr;
   1504  1.12  mycroft 			args.ua.export.ex_addrlen =
   1505  1.12  mycroft 				sizeof(struct sockaddr_iso);
   1506  1.12  mycroft 			args.ua.export.ex_masklen = 0;
   1507  1.12  mycroft 			break;
   1508  1.12  mycroft #endif	/* ISO */
   1509  1.12  mycroft 		default:
   1510  1.12  mycroft 			syslog(LOG_ERR, "Bad grouptype");
   1511   1.1      cgd 			if (cp)
   1512   1.1      cgd 				*cp = savedc;
   1513  1.12  mycroft 			return (1);
   1514  1.12  mycroft 		};
   1515  1.12  mycroft 
   1516  1.12  mycroft 		/*
   1517  1.12  mycroft 		 * XXX:
   1518  1.12  mycroft 		 * Maybe I should just use the fsb->f_mntonname path instead
   1519  1.12  mycroft 		 * of looping back up the dirp to the mount point??
   1520  1.12  mycroft 		 * Also, needs to know how to export all types of local
   1521  1.12  mycroft 		 * exportable file systems and not just MOUNT_UFS.
   1522  1.12  mycroft 		 */
   1523  1.12  mycroft 		while (mount(fsb->f_fstypename, dirp,
   1524  1.12  mycroft 		       fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
   1525  1.12  mycroft 			if (cp)
   1526  1.12  mycroft 				*cp-- = savedc;
   1527  1.12  mycroft 			else
   1528  1.12  mycroft 				cp = dirp + dirplen - 1;
   1529  1.12  mycroft 			if (errno == EPERM) {
   1530  1.12  mycroft 				syslog(LOG_ERR,
   1531  1.12  mycroft 				   "Can't change attributes for %s.\n", dirp);
   1532  1.12  mycroft 				return (1);
   1533  1.12  mycroft 			}
   1534  1.12  mycroft 			if (opt_flags & OP_ALLDIRS) {
   1535  1.12  mycroft 				syslog(LOG_ERR, "Not root dir");
   1536  1.12  mycroft 				return (1);
   1537  1.12  mycroft 			}
   1538  1.12  mycroft 			/* back up over the last component */
   1539  1.12  mycroft 			while (*cp == '/' && cp > dirp)
   1540  1.12  mycroft 				cp--;
   1541  1.12  mycroft 			while (*(cp - 1) != '/' && cp > dirp)
   1542  1.12  mycroft 				cp--;
   1543  1.12  mycroft 			if (cp == dirp) {
   1544  1.12  mycroft 				if (debug)
   1545  1.12  mycroft 					fprintf(stderr,"mnt unsucc\n");
   1546  1.12  mycroft 				syslog(LOG_ERR, "Can't export %s", dirp);
   1547  1.12  mycroft 				return (1);
   1548  1.12  mycroft 			}
   1549  1.12  mycroft 			savedc = *cp;
   1550  1.12  mycroft 			*cp = '\0';
   1551  1.12  mycroft 		}
   1552  1.12  mycroft 		if (addrp) {
   1553  1.12  mycroft 			++addrp;
   1554  1.12  mycroft 			if (*addrp == (u_long *)NULL)
   1555  1.12  mycroft 				done = TRUE;
   1556  1.12  mycroft 		} else
   1557  1.12  mycroft 			done = TRUE;
   1558  1.12  mycroft 	}
   1559  1.12  mycroft 	if (cp)
   1560  1.12  mycroft 		*cp = savedc;
   1561  1.12  mycroft 	return (0);
   1562  1.12  mycroft }
   1563  1.12  mycroft 
   1564  1.12  mycroft /*
   1565  1.12  mycroft  * Translate a net address.
   1566  1.12  mycroft  */
   1567  1.12  mycroft int
   1568  1.12  mycroft get_net(cp, net, maskflg)
   1569  1.12  mycroft 	char *cp;
   1570  1.12  mycroft 	struct netmsk *net;
   1571  1.12  mycroft 	int maskflg;
   1572  1.12  mycroft {
   1573  1.12  mycroft 	struct netent *np;
   1574  1.12  mycroft 	long netaddr;
   1575  1.12  mycroft 	struct in_addr inetaddr, inetaddr2;
   1576  1.12  mycroft 	char *name;
   1577  1.12  mycroft 
   1578  1.12  mycroft 	if (np = getnetbyname(cp))
   1579  1.12  mycroft 		inetaddr = inet_makeaddr(np->n_net, 0);
   1580  1.12  mycroft 	else if (isdigit(*cp)) {
   1581  1.12  mycroft 		if ((netaddr = inet_network(cp)) == -1)
   1582  1.12  mycroft 			return (1);
   1583  1.12  mycroft 		inetaddr = inet_makeaddr(netaddr, 0);
   1584  1.12  mycroft 		/*
   1585  1.12  mycroft 		 * Due to arbritrary subnet masks, you don't know how many
   1586  1.12  mycroft 		 * bits to shift the address to make it into a network,
   1587  1.12  mycroft 		 * however you do know how to make a network address into
   1588  1.12  mycroft 		 * a host with host == 0 and then compare them.
   1589  1.12  mycroft 		 * (What a pest)
   1590  1.12  mycroft 		 */
   1591  1.12  mycroft 		if (!maskflg) {
   1592  1.12  mycroft 			setnetent(0);
   1593  1.12  mycroft 			while (np = getnetent()) {
   1594  1.12  mycroft 				inetaddr2 = inet_makeaddr(np->n_net, 0);
   1595  1.12  mycroft 				if (inetaddr2.s_addr == inetaddr.s_addr)
   1596  1.12  mycroft 					break;
   1597   1.9       ws 			}
   1598  1.12  mycroft 			endnetent();
   1599  1.11       ws 		}
   1600  1.12  mycroft 	} else
   1601  1.12  mycroft 		return (1);
   1602  1.12  mycroft 	if (maskflg)
   1603  1.12  mycroft 		net->nt_mask = inetaddr.s_addr;
   1604  1.12  mycroft 	else {
   1605  1.12  mycroft 		if (np)
   1606  1.12  mycroft 			name = np->n_name;
   1607  1.12  mycroft 		else
   1608  1.12  mycroft 			name = inet_ntoa(inetaddr);
   1609  1.12  mycroft 		net->nt_name = (char *)malloc(strlen(name) + 1);
   1610  1.12  mycroft 		if (net->nt_name == (char *)NULL)
   1611  1.12  mycroft 			out_of_mem();
   1612  1.12  mycroft 		strcpy(net->nt_name, name);
   1613  1.12  mycroft 		net->nt_net = inetaddr.s_addr;
   1614   1.1      cgd 	}
   1615  1.12  mycroft 	return (0);
   1616   1.1      cgd }
   1617   1.1      cgd 
   1618   1.1      cgd /*
   1619   1.1      cgd  * Parse out the next white space separated field
   1620   1.1      cgd  */
   1621  1.12  mycroft void
   1622   1.1      cgd nextfield(cp, endcp)
   1623   1.1      cgd 	char **cp;
   1624   1.1      cgd 	char **endcp;
   1625   1.1      cgd {
   1626  1.12  mycroft 	char *p;
   1627   1.1      cgd 
   1628   1.1      cgd 	p = *cp;
   1629   1.1      cgd 	while (*p == ' ' || *p == '\t')
   1630   1.1      cgd 		p++;
   1631  1.12  mycroft 	if (*p == '\n' || *p == '\0')
   1632   1.1      cgd 		*cp = *endcp = p;
   1633  1.12  mycroft 	else {
   1634  1.12  mycroft 		*cp = p++;
   1635  1.12  mycroft 		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
   1636  1.12  mycroft 			p++;
   1637  1.12  mycroft 		*endcp = p;
   1638   1.1      cgd 	}
   1639   1.1      cgd }
   1640   1.1      cgd 
   1641   1.1      cgd /*
   1642  1.12  mycroft  * Get an exports file line. Skip over blank lines and handle line
   1643  1.12  mycroft  * continuations.
   1644  1.12  mycroft  */
   1645  1.12  mycroft int
   1646  1.12  mycroft get_line()
   1647  1.12  mycroft {
   1648  1.12  mycroft 	char *p, *cp;
   1649  1.12  mycroft 	int len;
   1650  1.12  mycroft 	int totlen, cont_line;
   1651  1.12  mycroft 
   1652  1.12  mycroft 	/*
   1653  1.12  mycroft 	 * Loop around ignoring blank lines and getting all continuation lines.
   1654  1.12  mycroft 	 */
   1655  1.12  mycroft 	p = line;
   1656  1.12  mycroft 	totlen = 0;
   1657  1.12  mycroft 	do {
   1658  1.12  mycroft 		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
   1659  1.12  mycroft 			return (0);
   1660  1.12  mycroft 		len = strlen(p);
   1661  1.12  mycroft 		cp = p + len - 1;
   1662  1.12  mycroft 		cont_line = 0;
   1663  1.12  mycroft 		while (cp >= p &&
   1664  1.12  mycroft 		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
   1665  1.12  mycroft 			if (*cp == '\\')
   1666  1.12  mycroft 				cont_line = 1;
   1667  1.12  mycroft 			cp--;
   1668  1.12  mycroft 			len--;
   1669  1.12  mycroft 		}
   1670  1.12  mycroft 		*++cp = '\0';
   1671  1.12  mycroft 		if (len > 0) {
   1672  1.12  mycroft 			totlen += len;
   1673  1.12  mycroft 			if (totlen >= LINESIZ) {
   1674  1.12  mycroft 				syslog(LOG_ERR, "Exports line too long");
   1675  1.12  mycroft 				exit(2);
   1676  1.12  mycroft 			}
   1677  1.12  mycroft 			p = cp;
   1678  1.12  mycroft 		}
   1679  1.12  mycroft 	} while (totlen == 0 || cont_line);
   1680  1.12  mycroft 	return (1);
   1681  1.12  mycroft }
   1682  1.12  mycroft 
   1683  1.12  mycroft /*
   1684  1.12  mycroft  * Parse a description of a credential.
   1685   1.1      cgd  */
   1686  1.12  mycroft void
   1687  1.12  mycroft parsecred(namelist, cr)
   1688  1.12  mycroft 	char *namelist;
   1689  1.12  mycroft 	struct ucred *cr;
   1690   1.1      cgd {
   1691  1.12  mycroft 	char *name;
   1692  1.12  mycroft 	int cnt;
   1693  1.12  mycroft 	char *names;
   1694  1.12  mycroft 	struct passwd *pw;
   1695  1.12  mycroft 	struct group *gr;
   1696  1.12  mycroft 	int ngroups, groups[NGROUPS + 1];
   1697   1.1      cgd 
   1698  1.12  mycroft 	/*
   1699  1.12  mycroft 	 * Set up the unpriviledged user.
   1700  1.12  mycroft 	 */
   1701  1.12  mycroft 	cr->cr_ref = 1;
   1702  1.12  mycroft 	cr->cr_uid = -2;
   1703  1.12  mycroft 	cr->cr_groups[0] = -2;
   1704  1.12  mycroft 	cr->cr_ngroups = 1;
   1705  1.12  mycroft 	/*
   1706  1.12  mycroft 	 * Get the user's password table entry.
   1707  1.12  mycroft 	 */
   1708  1.12  mycroft 	names = strsep(&namelist, " \t\n");
   1709  1.12  mycroft 	name = strsep(&names, ":");
   1710  1.12  mycroft 	if (isdigit(*name) || *name == '-')
   1711  1.12  mycroft 		pw = getpwuid(atoi(name));
   1712  1.12  mycroft 	else
   1713  1.12  mycroft 		pw = getpwnam(name);
   1714  1.12  mycroft 	/*
   1715  1.12  mycroft 	 * Credentials specified as those of a user.
   1716  1.12  mycroft 	 */
   1717  1.12  mycroft 	if (names == NULL) {
   1718  1.12  mycroft 		if (pw == NULL) {
   1719  1.12  mycroft 			syslog(LOG_ERR, "Unknown user: %s", name);
   1720  1.12  mycroft 			return;
   1721  1.12  mycroft 		}
   1722  1.12  mycroft 		cr->cr_uid = pw->pw_uid;
   1723  1.12  mycroft 		ngroups = NGROUPS + 1;
   1724  1.12  mycroft 		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
   1725  1.12  mycroft 			syslog(LOG_ERR, "Too many groups");
   1726  1.12  mycroft 		/*
   1727  1.12  mycroft 		 * Convert from int's to gid_t's and compress out duplicate
   1728  1.12  mycroft 		 */
   1729  1.12  mycroft 		cr->cr_ngroups = ngroups - 1;
   1730  1.12  mycroft 		cr->cr_groups[0] = groups[0];
   1731  1.12  mycroft 		for (cnt = 2; cnt < ngroups; cnt++)
   1732  1.12  mycroft 			cr->cr_groups[cnt - 1] = groups[cnt];
   1733  1.12  mycroft 		return;
   1734  1.12  mycroft 	}
   1735  1.12  mycroft 	/*
   1736  1.12  mycroft 	 * Explicit credential specified as a colon separated list:
   1737  1.12  mycroft 	 *	uid:gid:gid:...
   1738  1.12  mycroft 	 */
   1739  1.12  mycroft 	if (pw != NULL)
   1740  1.12  mycroft 		cr->cr_uid = pw->pw_uid;
   1741  1.12  mycroft 	else if (isdigit(*name) || *name == '-')
   1742  1.12  mycroft 		cr->cr_uid = atoi(name);
   1743  1.12  mycroft 	else {
   1744  1.12  mycroft 		syslog(LOG_ERR, "Unknown user: %s", name);
   1745  1.12  mycroft 		return;
   1746  1.12  mycroft 	}
   1747  1.12  mycroft 	cr->cr_ngroups = 0;
   1748  1.12  mycroft 	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
   1749  1.12  mycroft 		name = strsep(&names, ":");
   1750  1.12  mycroft 		if (isdigit(*name) || *name == '-') {
   1751  1.12  mycroft 			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
   1752  1.12  mycroft 		} else {
   1753  1.12  mycroft 			if ((gr = getgrnam(name)) == NULL) {
   1754  1.12  mycroft 				syslog(LOG_ERR, "Unknown group: %s", name);
   1755  1.12  mycroft 				continue;
   1756  1.12  mycroft 			}
   1757  1.12  mycroft 			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
   1758  1.12  mycroft 		}
   1759   1.1      cgd 	}
   1760  1.12  mycroft 	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
   1761  1.12  mycroft 		syslog(LOG_ERR, "Too many groups");
   1762   1.1      cgd }
   1763   1.1      cgd 
   1764  1.12  mycroft #define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
   1765   1.1      cgd /*
   1766   1.1      cgd  * Routines that maintain the remote mounttab
   1767   1.1      cgd  */
   1768  1.12  mycroft void
   1769  1.12  mycroft get_mountlist()
   1770   1.1      cgd {
   1771  1.12  mycroft 	struct mountlist *mlp, **mlpp;
   1772  1.12  mycroft 	char *eos, *dirp;
   1773   1.1      cgd 	int len;
   1774   1.1      cgd 	char str[STRSIZ];
   1775   1.1      cgd 	FILE *mlfile;
   1776   1.1      cgd 
   1777  1.11       ws 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
   1778  1.12  mycroft 		syslog(LOG_ERR, "Can't open %s", _PATH_RMOUNTLIST);
   1779   1.1      cgd 		return;
   1780   1.1      cgd 	}
   1781   1.1      cgd 	mlpp = &mlhead;
   1782   1.1      cgd 	while (fgets(str, STRSIZ, mlfile) != NULL) {
   1783   1.1      cgd 		if ((dirp = index(str, '\t')) == NULL &&
   1784   1.1      cgd 		    (dirp = index(str, ' ')) == NULL)
   1785   1.1      cgd 			continue;
   1786   1.1      cgd 		mlp = (struct mountlist *)malloc(sizeof (*mlp));
   1787   1.1      cgd 		len = dirp-str;
   1788  1.12  mycroft 		if (len > RPCMNT_NAMELEN)
   1789  1.12  mycroft 			len = RPCMNT_NAMELEN;
   1790   1.1      cgd 		bcopy(str, mlp->ml_host, len);
   1791   1.1      cgd 		mlp->ml_host[len] = '\0';
   1792   1.1      cgd 		while (*dirp == '\t' || *dirp == ' ')
   1793   1.1      cgd 			dirp++;
   1794   1.1      cgd 		if ((eos = index(dirp, '\t')) == NULL &&
   1795   1.1      cgd 		    (eos = index(dirp, ' ')) == NULL &&
   1796   1.1      cgd 		    (eos = index(dirp, '\n')) == NULL)
   1797   1.1      cgd 			len = strlen(dirp);
   1798   1.1      cgd 		else
   1799   1.1      cgd 			len = eos-dirp;
   1800  1.12  mycroft 		if (len > RPCMNT_PATHLEN)
   1801  1.12  mycroft 			len = RPCMNT_PATHLEN;
   1802   1.1      cgd 		bcopy(dirp, mlp->ml_dirp, len);
   1803   1.1      cgd 		mlp->ml_dirp[len] = '\0';
   1804  1.12  mycroft 		mlp->ml_next = (struct mountlist *)NULL;
   1805   1.1      cgd 		*mlpp = mlp;
   1806   1.1      cgd 		mlpp = &mlp->ml_next;
   1807   1.1      cgd 	}
   1808   1.1      cgd 	fclose(mlfile);
   1809   1.1      cgd }
   1810   1.1      cgd 
   1811  1.12  mycroft void
   1812  1.12  mycroft del_mlist(hostp, dirp)
   1813  1.12  mycroft 	char *hostp, *dirp;
   1814   1.1      cgd {
   1815  1.12  mycroft 	struct mountlist *mlp, **mlpp;
   1816  1.12  mycroft 	struct mountlist *mlp2;
   1817   1.1      cgd 	FILE *mlfile;
   1818   1.1      cgd 	int fnd = 0;
   1819   1.1      cgd 
   1820   1.1      cgd 	mlpp = &mlhead;
   1821   1.1      cgd 	mlp = mlhead;
   1822   1.1      cgd 	while (mlp) {
   1823   1.1      cgd 		if (!strcmp(mlp->ml_host, hostp) &&
   1824   1.1      cgd 		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
   1825   1.1      cgd 			fnd = 1;
   1826  1.12  mycroft 			mlp2 = mlp;
   1827  1.12  mycroft 			*mlpp = mlp = mlp->ml_next;
   1828  1.12  mycroft 			free((caddr_t)mlp2);
   1829  1.12  mycroft 		} else {
   1830  1.12  mycroft 			mlpp = &mlp->ml_next;
   1831  1.12  mycroft 			mlp = mlp->ml_next;
   1832   1.1      cgd 		}
   1833   1.1      cgd 	}
   1834   1.1      cgd 	if (fnd) {
   1835   1.1      cgd 		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
   1836  1.12  mycroft 			syslog(LOG_ERR,"Can't update %s", _PATH_RMOUNTLIST);
   1837   1.1      cgd 			return;
   1838   1.1      cgd 		}
   1839   1.1      cgd 		mlp = mlhead;
   1840   1.1      cgd 		while (mlp) {
   1841   1.1      cgd 			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
   1842   1.1      cgd 			mlp = mlp->ml_next;
   1843   1.1      cgd 		}
   1844   1.1      cgd 		fclose(mlfile);
   1845   1.1      cgd 	}
   1846   1.1      cgd }
   1847   1.1      cgd 
   1848  1.12  mycroft void
   1849  1.12  mycroft add_mlist(hostp, dirp)
   1850  1.12  mycroft 	char *hostp, *dirp;
   1851   1.1      cgd {
   1852  1.12  mycroft 	struct mountlist *mlp, **mlpp;
   1853   1.1      cgd 	FILE *mlfile;
   1854   1.1      cgd 
   1855   1.1      cgd 	mlpp = &mlhead;
   1856   1.1      cgd 	mlp = mlhead;
   1857   1.1      cgd 	while (mlp) {
   1858   1.1      cgd 		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
   1859   1.1      cgd 			return;
   1860   1.1      cgd 		mlpp = &mlp->ml_next;
   1861   1.1      cgd 		mlp = mlp->ml_next;
   1862   1.1      cgd 	}
   1863   1.1      cgd 	mlp = (struct mountlist *)malloc(sizeof (*mlp));
   1864  1.12  mycroft 	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
   1865  1.12  mycroft 	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
   1866  1.12  mycroft 	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
   1867  1.12  mycroft 	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
   1868  1.12  mycroft 	mlp->ml_next = (struct mountlist *)NULL;
   1869   1.1      cgd 	*mlpp = mlp;
   1870   1.1      cgd 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
   1871  1.12  mycroft 		syslog(LOG_ERR, "Can't update %s", _PATH_RMOUNTLIST);
   1872   1.1      cgd 		return;
   1873   1.1      cgd 	}
   1874   1.1      cgd 	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
   1875   1.1      cgd 	fclose(mlfile);
   1876   1.1      cgd }
   1877   1.1      cgd 
   1878   1.1      cgd /*
   1879   1.1      cgd  * This function is called via. SIGTERM when the system is going down.
   1880   1.1      cgd  * It sends a broadcast RPCMNT_UMNTALL.
   1881   1.1      cgd  */
   1882   1.1      cgd void
   1883   1.1      cgd send_umntall()
   1884   1.1      cgd {
   1885  1.12  mycroft 	(void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
   1886   1.1      cgd 		xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
   1887  1.12  mycroft 	exit(0);
   1888   1.1      cgd }
   1889   1.1      cgd 
   1890  1.12  mycroft int
   1891   1.1      cgd umntall_each(resultsp, raddr)
   1892   1.1      cgd 	caddr_t resultsp;
   1893   1.1      cgd 	struct sockaddr_in *raddr;
   1894   1.1      cgd {
   1895   1.1      cgd 	return (1);
   1896   1.1      cgd }
   1897   1.1      cgd 
   1898   1.1      cgd /*
   1899  1.12  mycroft  * Free up a group list.
   1900  1.12  mycroft  */
   1901  1.12  mycroft void
   1902  1.12  mycroft free_grp(grp)
   1903  1.12  mycroft 	struct grouplist *grp;
   1904  1.12  mycroft {
   1905  1.12  mycroft 	char **addrp;
   1906  1.12  mycroft 
   1907  1.12  mycroft 	if (grp->gr_type == GT_HOST) {
   1908  1.12  mycroft 		if (grp->gr_ptr.gt_hostent->h_name) {
   1909  1.12  mycroft 			addrp = grp->gr_ptr.gt_hostent->h_addr_list;
   1910  1.12  mycroft 			while (addrp && *addrp)
   1911  1.12  mycroft 				free(*addrp++);
   1912  1.12  mycroft 			free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
   1913  1.12  mycroft 			free(grp->gr_ptr.gt_hostent->h_name);
   1914  1.12  mycroft 		}
   1915  1.12  mycroft 		free((caddr_t)grp->gr_ptr.gt_hostent);
   1916  1.12  mycroft 	} else if (grp->gr_type == GT_NET) {
   1917  1.12  mycroft 		if (grp->gr_ptr.gt_net.nt_name)
   1918  1.12  mycroft 			free(grp->gr_ptr.gt_net.nt_name);
   1919  1.12  mycroft 	}
   1920  1.12  mycroft #ifdef ISO
   1921  1.12  mycroft 	else if (grp->gr_type == GT_ISO)
   1922  1.12  mycroft 		free((caddr_t)grp->gr_ptr.gt_isoaddr);
   1923  1.12  mycroft #endif
   1924  1.12  mycroft 	free((caddr_t)grp);
   1925  1.12  mycroft }
   1926  1.12  mycroft 
   1927  1.12  mycroft #ifdef DEBUG
   1928  1.12  mycroft void
   1929  1.12  mycroft SYSLOG(int pri, const char *fmt, ...)
   1930  1.12  mycroft {
   1931  1.12  mycroft 	va_list ap;
   1932  1.12  mycroft 
   1933  1.12  mycroft 	va_start(ap, fmt);
   1934  1.12  mycroft 	vfprintf(stderr, fmt, ap);
   1935  1.12  mycroft 	va_end(ap);
   1936  1.12  mycroft }
   1937  1.12  mycroft #endif /* DEBUG */
   1938  1.12  mycroft 
   1939  1.12  mycroft /*
   1940  1.12  mycroft  * Check options for consistency.
   1941  1.12  mycroft  */
   1942  1.12  mycroft int
   1943  1.12  mycroft check_options(dp)
   1944  1.12  mycroft 	struct dirlist *dp;
   1945  1.12  mycroft {
   1946  1.12  mycroft 
   1947  1.12  mycroft 	if (dp == (struct dirlist *)NULL)
   1948  1.12  mycroft 	    return (1);
   1949  1.12  mycroft 	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
   1950  1.12  mycroft 	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
   1951  1.12  mycroft 	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
   1952  1.12  mycroft 	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
   1953  1.12  mycroft 	    return (1);
   1954  1.12  mycroft 	}
   1955  1.12  mycroft 	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
   1956  1.12  mycroft 	    syslog(LOG_ERR, "-mask requires -net");
   1957  1.12  mycroft 	    return (1);
   1958  1.12  mycroft 	}
   1959  1.12  mycroft 	if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
   1960  1.12  mycroft 	    syslog(LOG_ERR, "-net and -iso mutually exclusive");
   1961  1.12  mycroft 	    return (1);
   1962  1.12  mycroft 	}
   1963  1.12  mycroft 	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
   1964  1.12  mycroft 	    syslog(LOG_ERR, "-alldir has multiple directories");
   1965  1.12  mycroft 	    return (1);
   1966  1.12  mycroft 	}
   1967  1.12  mycroft 	return (0);
   1968  1.12  mycroft }
   1969  1.12  mycroft 
   1970  1.12  mycroft /*
   1971  1.12  mycroft  * Check an absolute directory path for any symbolic links. Return true
   1972  1.12  mycroft  * if no symbolic links are found.
   1973   1.1      cgd  */
   1974  1.12  mycroft int
   1975  1.12  mycroft check_dirpath(dirp)
   1976  1.12  mycroft 	char *dirp;
   1977   1.1      cgd {
   1978  1.12  mycroft 	char *cp;
   1979  1.12  mycroft 	int ret = 1;
   1980  1.12  mycroft 	struct stat sb;
   1981  1.12  mycroft 
   1982  1.12  mycroft 	cp = dirp + 1;
   1983  1.12  mycroft 	while (*cp && ret) {
   1984  1.12  mycroft 		if (*cp == '/') {
   1985  1.12  mycroft 			*cp = '\0';
   1986  1.12  mycroft 			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
   1987  1.12  mycroft 				ret = 0;
   1988  1.12  mycroft 			*cp = '/';
   1989  1.12  mycroft 		}
   1990  1.12  mycroft 		cp++;
   1991   1.1      cgd 	}
   1992  1.12  mycroft 	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
   1993  1.12  mycroft 		ret = 0;
   1994  1.12  mycroft 	return (ret);
   1995   1.1      cgd }
   1996