mountd.c revision 31656
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1989, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * This code is derived from software contributed to Berkeley by
61558Srgrimes * Herb Hasler and Rick Macklem at The University of Guelph.
71558Srgrimes *
81558Srgrimes * Redistribution and use in source and binary forms, with or without
91558Srgrimes * modification, are permitted provided that the following conditions
101558Srgrimes * are met:
111558Srgrimes * 1. Redistributions of source code must retain the above copyright
121558Srgrimes *    notice, this list of conditions and the following disclaimer.
131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer in the
151558Srgrimes *    documentation and/or other materials provided with the distribution.
161558Srgrimes * 3. All advertising materials mentioning features or use of this software
171558Srgrimes *    must display the following acknowledgement:
181558Srgrimes *	This product includes software developed by the University of
191558Srgrimes *	California, Berkeley and its contributors.
201558Srgrimes * 4. Neither the name of the University nor the names of its contributors
211558Srgrimes *    may be used to endorse or promote products derived from this software
221558Srgrimes *    without specific prior written permission.
231558Srgrimes *
241558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341558Srgrimes * SUCH DAMAGE.
351558Srgrimes */
361558Srgrimes
371558Srgrimes#ifndef lint
381558Srgrimesstatic char copyright[] =
391558Srgrimes"@(#) Copyright (c) 1989, 1993\n\
401558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
412999Swollman#endif /*not lint*/
421558Srgrimes
431558Srgrimes#ifndef lint
4423681Speter/*static char sccsid[] = "@(#)mountd.c	8.15 (Berkeley) 5/1/95"; */
452999Swollmanstatic const char rcsid[] =
4631656Sguido	"$Id: mountd.c,v 1.24 1997/09/12 16:25:24 jlemon Exp $";
472999Swollman#endif /*not lint*/
481558Srgrimes
491558Srgrimes#include <sys/param.h>
501558Srgrimes#include <sys/file.h>
511558Srgrimes#include <sys/ioctl.h>
521558Srgrimes#include <sys/mount.h>
531558Srgrimes#include <sys/socket.h>
541558Srgrimes#include <sys/stat.h>
551558Srgrimes#include <sys/syslog.h>
561558Srgrimes#include <sys/ucred.h>
5724330Sguido#include <sys/sysctl.h>
581558Srgrimes
591558Srgrimes#include <rpc/rpc.h>
601558Srgrimes#include <rpc/pmap_clnt.h>
611558Srgrimes#include <rpc/pmap_prot.h>
621558Srgrimes#ifdef ISO
631558Srgrimes#include <netiso/iso.h>
641558Srgrimes#endif
651558Srgrimes#include <nfs/rpcv2.h>
669336Sdfr#include <nfs/nfsproto.h>
6724330Sguido#include <nfs/nfs.h>
6823681Speter#include <ufs/ufs/ufsmount.h>
6923681Speter#include <msdosfs/msdosfsmount.h>
7023681Speter#include <isofs/cd9660/cd9660_mount.h>	/* XXX need isofs in include */
711558Srgrimes
721558Srgrimes#include <arpa/inet.h>
731558Srgrimes
741558Srgrimes#include <ctype.h>
751558Srgrimes#include <errno.h>
761558Srgrimes#include <grp.h>
771558Srgrimes#include <netdb.h>
781558Srgrimes#include <pwd.h>
791558Srgrimes#include <signal.h>
801558Srgrimes#include <stdio.h>
811558Srgrimes#include <stdlib.h>
821558Srgrimes#include <string.h>
831558Srgrimes#include <unistd.h>
841558Srgrimes#include "pathnames.h"
851558Srgrimes
861558Srgrimes#ifdef DEBUG
871558Srgrimes#include <stdarg.h>
881558Srgrimes#endif
891558Srgrimes
901558Srgrimes/*
911558Srgrimes * Structures for keeping the mount list and export list
921558Srgrimes */
931558Srgrimesstruct mountlist {
941558Srgrimes	struct mountlist *ml_next;
951558Srgrimes	char	ml_host[RPCMNT_NAMELEN+1];
961558Srgrimes	char	ml_dirp[RPCMNT_PATHLEN+1];
971558Srgrimes};
981558Srgrimes
991558Srgrimesstruct dirlist {
1001558Srgrimes	struct dirlist	*dp_left;
1011558Srgrimes	struct dirlist	*dp_right;
1021558Srgrimes	int		dp_flag;
1031558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
1041558Srgrimes	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1051558Srgrimes};
1061558Srgrimes/* dp_flag bits */
1071558Srgrimes#define	DP_DEFSET	0x1
1089336Sdfr#define DP_HOSTSET	0x2
1099336Sdfr#define DP_KERB		0x4
1101558Srgrimes
1111558Srgrimesstruct exportlist {
1121558Srgrimes	struct exportlist *ex_next;
1131558Srgrimes	struct dirlist	*ex_dirl;
1141558Srgrimes	struct dirlist	*ex_defdir;
1151558Srgrimes	int		ex_flag;
1161558Srgrimes	fsid_t		ex_fs;
1171558Srgrimes	char		*ex_fsdir;
11827447Sdfr	char		*ex_indexfile;
1191558Srgrimes};
1201558Srgrimes/* ex_flag bits */
1211558Srgrimes#define	EX_LINKED	0x1
1221558Srgrimes
1231558Srgrimesstruct netmsk {
1241558Srgrimes	u_long	nt_net;
1251558Srgrimes	u_long	nt_mask;
1261558Srgrimes	char *nt_name;
1271558Srgrimes};
1281558Srgrimes
1291558Srgrimesunion grouptypes {
1301558Srgrimes	struct hostent *gt_hostent;
1311558Srgrimes	struct netmsk	gt_net;
1321558Srgrimes#ifdef ISO
1331558Srgrimes	struct sockaddr_iso *gt_isoaddr;
1341558Srgrimes#endif
1351558Srgrimes};
1361558Srgrimes
1371558Srgrimesstruct grouplist {
1381558Srgrimes	int gr_type;
1391558Srgrimes	union grouptypes gr_ptr;
1401558Srgrimes	struct grouplist *gr_next;
1411558Srgrimes};
1421558Srgrimes/* Group types */
1431558Srgrimes#define	GT_NULL		0x0
1441558Srgrimes#define	GT_HOST		0x1
1451558Srgrimes#define	GT_NET		0x2
1461558Srgrimes#define	GT_ISO		0x4
1477401Swpaul#define GT_IGNORE	0x5
1481558Srgrimes
1491558Srgrimesstruct hostlist {
1509336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1511558Srgrimes	struct grouplist *ht_grp;
1521558Srgrimes	struct hostlist	 *ht_next;
1531558Srgrimes};
1541558Srgrimes
1559336Sdfrstruct fhreturn {
1569336Sdfr	int	fhr_flag;
1579336Sdfr	int	fhr_vers;
1589336Sdfr	nfsfh_t	fhr_fh;
1599336Sdfr};
1609336Sdfr
1611558Srgrimes/* Global defs */
1621558Srgrimeschar	*add_expdir __P((struct dirlist **, char *, int));
1631558Srgrimesvoid	add_dlist __P((struct dirlist **, struct dirlist *,
1649336Sdfr				struct grouplist *, int));
1651558Srgrimesvoid	add_mlist __P((char *, char *));
1661558Srgrimesint	check_dirpath __P((char *));
1671558Srgrimesint	check_options __P((struct dirlist *));
1689336Sdfrint	chk_host __P((struct dirlist *, u_long, int *, int *));
1691558Srgrimesvoid	del_mlist __P((char *, char *));
1701558Srgrimesstruct dirlist *dirp_search __P((struct dirlist *, char *));
1711558Srgrimesint	do_mount __P((struct exportlist *, struct grouplist *, int,
1729336Sdfr		struct ucred *, char *, int, struct statfs *));
1731558Srgrimesint	do_opt __P((char **, char **, struct exportlist *, struct grouplist *,
1741558Srgrimes				int *, int *, struct ucred *));
1751558Srgrimesstruct	exportlist *ex_search __P((fsid_t *));
1761558Srgrimesstruct	exportlist *get_exp __P((void));
1771558Srgrimesvoid	free_dir __P((struct dirlist *));
1781558Srgrimesvoid	free_exp __P((struct exportlist *));
1791558Srgrimesvoid	free_grp __P((struct grouplist *));
1801558Srgrimesvoid	free_host __P((struct hostlist *));
1811558Srgrimesvoid	get_exportlist __P((void));
1827401Swpaulint	get_host __P((char *, struct grouplist *, struct grouplist *));
1839336Sdfrint	get_num __P((char *));
1841558Srgrimesstruct hostlist *get_ht __P((void));
1851558Srgrimesint	get_line __P((void));
1861558Srgrimesvoid	get_mountlist __P((void));
1871558Srgrimesint	get_net __P((char *, struct netmsk *, int));
1881558Srgrimesvoid	getexp_err __P((struct exportlist *, struct grouplist *));
1891558Srgrimesstruct grouplist *get_grp __P((void));
1901558Srgrimesvoid	hang_dirp __P((struct dirlist *, struct grouplist *,
1911558Srgrimes				struct exportlist *, int));
1921558Srgrimesvoid	mntsrv __P((struct svc_req *, SVCXPRT *));
1931558Srgrimesvoid	nextfield __P((char **, char **));
1941558Srgrimesvoid	out_of_mem __P((void));
1951558Srgrimesvoid	parsecred __P((char *, struct ucred *));
1961558Srgrimesint	put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
1971558Srgrimesint	scan_tree __P((struct dirlist *, u_long));
1981558Srgrimesvoid	send_umntall __P((void));
1991558Srgrimesint	umntall_each __P((caddr_t, struct sockaddr_in *));
2001558Srgrimesint	xdr_dir __P((XDR *, char *));
2011558Srgrimesint	xdr_explist __P((XDR *, caddr_t));
2029336Sdfrint	xdr_fhs __P((XDR *, caddr_t));
2031558Srgrimesint	xdr_mlist __P((XDR *, caddr_t));
2041558Srgrimes
2051558Srgrimes/* C library */
2061558Srgrimesint	getnetgrent();
2071558Srgrimesvoid	endnetgrent();
2081558Srgrimesvoid	setnetgrent();
2091558Srgrimes
2101558Srgrimes#ifdef ISO
2111558Srgrimesstruct iso_addr *iso_addr();
2121558Srgrimes#endif
2131558Srgrimes
2141558Srgrimesstruct exportlist *exphead;
2151558Srgrimesstruct mountlist *mlhead;
2161558Srgrimesstruct grouplist *grphead;
2171558Srgrimeschar exname[MAXPATHLEN];
2181558Srgrimesstruct ucred def_anon = {
2191558Srgrimes	1,
2201558Srgrimes	(uid_t) -2,
2211558Srgrimes	1,
2221558Srgrimes	{ (gid_t) -2 }
2231558Srgrimes};
22425087Sdfrint force_v2 = 0;
2259336Sdfrint resvport_only = 1;
2269336Sdfrint dir_only = 1;
22731656Sguidoint log = 1;
2281558Srgrimesint opt_flags;
2291558Srgrimes/* Bits for above */
2301558Srgrimes#define	OP_MAPROOT	0x01
2311558Srgrimes#define	OP_MAPALL	0x02
2321558Srgrimes#define	OP_KERB		0x04
2331558Srgrimes#define	OP_MASK		0x08
2341558Srgrimes#define	OP_NET		0x10
2351558Srgrimes#define	OP_ISO		0x20
2361558Srgrimes#define	OP_ALLDIRS	0x40
2371558Srgrimes
2381558Srgrimes#ifdef DEBUG
2391558Srgrimesint debug = 1;
2401558Srgrimesvoid	SYSLOG __P((int, const char *, ...));
2411558Srgrimes#define syslog SYSLOG
2421558Srgrimes#else
2431558Srgrimesint debug = 0;
2441558Srgrimes#endif
2451558Srgrimes
2461558Srgrimes/*
2471558Srgrimes * Mountd server for NFS mount protocol as described in:
2481558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2491558Srgrimes * The optional arguments are the exports file name
2501558Srgrimes * default: _PATH_EXPORTS
2511558Srgrimes * and "-n" to allow nonroot mount.
2521558Srgrimes */
2531558Srgrimesint
2541558Srgrimesmain(argc, argv)
2551558Srgrimes	int argc;
2561558Srgrimes	char **argv;
2571558Srgrimes{
2589202Srgrimes	SVCXPRT *udptransp, *tcptransp;
2591558Srgrimes	int c;
26024489Sbde	int mib[3];
2619336Sdfr#ifdef __FreeBSD__
26223681Speter	struct vfsconf vfc;
26323681Speter	int error;
2641558Srgrimes
26523681Speter	error = getvfsbyname("nfs", &vfc);
26623681Speter	if (error && vfsisloadable("nfs")) {
2672999Swollman		if(vfsload("nfs"))
2682999Swollman			err(1, "vfsload(nfs)");
2692999Swollman		endvfsent();	/* flush cache */
27023681Speter		error = getvfsbyname("nfs", &vfc);
2712999Swollman	}
27223681Speter	if (error)
2732999Swollman		errx(1, "NFS support is not available in the running kernel");
2749336Sdfr#endif	/* __FreeBSD__ */
2752999Swollman
27631656Sguido	while ((c = getopt(argc, argv, "2dnrl")) != -1)
2771558Srgrimes		switch (c) {
27825087Sdfr		case '2':
27925087Sdfr			force_v2 = 1;
28025087Sdfr			break;
2819336Sdfr		case 'n':
2829336Sdfr			resvport_only = 0;
2839336Sdfr			break;
2849336Sdfr		case 'r':
2859336Sdfr			dir_only = 0;
2869336Sdfr			break;
2878688Sphk		case 'd':
2888688Sphk			debug = debug ? 0 : 1;
2898688Sphk			break;
29031656Sguido		case 'l':
29131656Sguido			log = 1;
29231656Sguido			break;
2931558Srgrimes		default:
29431656Sguido			fprintf(stderr, "Usage: mountd [-d] [-r] [-n] [-l] [export_file]\n");
2951558Srgrimes			exit(1);
2961558Srgrimes		};
2971558Srgrimes	argc -= optind;
2981558Srgrimes	argv += optind;
2991558Srgrimes	grphead = (struct grouplist *)NULL;
3001558Srgrimes	exphead = (struct exportlist *)NULL;
3011558Srgrimes	mlhead = (struct mountlist *)NULL;
3021558Srgrimes	if (argc == 1) {
3031558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
3041558Srgrimes		exname[MAXPATHLEN-1] = '\0';
3051558Srgrimes	} else
3061558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3071558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3081558Srgrimes	if (debug)
3091558Srgrimes		fprintf(stderr,"Getting export list.\n");
3101558Srgrimes	get_exportlist();
3111558Srgrimes	if (debug)
3121558Srgrimes		fprintf(stderr,"Getting mount list.\n");
3131558Srgrimes	get_mountlist();
3141558Srgrimes	if (debug)
3151558Srgrimes		fprintf(stderr,"Here we go.\n");
3161558Srgrimes	if (debug == 0) {
3171558Srgrimes		daemon(0, 0);
3181558Srgrimes		signal(SIGINT, SIG_IGN);
3191558Srgrimes		signal(SIGQUIT, SIG_IGN);
3201558Srgrimes	}
3211558Srgrimes	signal(SIGHUP, (void (*) __P((int))) get_exportlist);
3221558Srgrimes	signal(SIGTERM, (void (*) __P((int))) send_umntall);
3231558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3241558Srgrimes	  if (pidfile != NULL) {
3251558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3261558Srgrimes		fclose(pidfile);
3271558Srgrimes	  }
3281558Srgrimes	}
32924330Sguido
33024759Sguido	if (!resvport_only) {
33124759Sguido		mib[0] = CTL_VFS;
33224759Sguido		mib[1] = MOUNT_NFS;
33324759Sguido		mib[2] = NFS_NFSPRIVPORT;
33424759Sguido		if (sysctl(mib, 3, NULL, NULL, &resvport_only,
33524759Sguido		    sizeof(resvport_only)) != 0 && errno != ENOENT) {
33624759Sguido			syslog(LOG_ERR, "sysctl: %m");
33724759Sguido			exit(1);
33824759Sguido		}
33924330Sguido	}
34024330Sguido
3419202Srgrimes	if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
3429202Srgrimes	    (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
3431558Srgrimes		syslog(LOG_ERR, "Can't create socket");
3441558Srgrimes		exit(1);
3451558Srgrimes	}
3469336Sdfr	pmap_unset(RPCPROG_MNT, 1);
3479336Sdfr	pmap_unset(RPCPROG_MNT, 3);
34825087Sdfr	if (!force_v2)
34925087Sdfr		if (!svc_register(udptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_UDP) ||
35025087Sdfr		    !svc_register(tcptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_TCP)) {
35125087Sdfr			syslog(LOG_ERR, "Can't register mount");
35225087Sdfr			exit(1);
35325087Sdfr		}
3549336Sdfr	if (!svc_register(udptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_UDP) ||
35525087Sdfr	    !svc_register(tcptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_TCP)) {
3561558Srgrimes		syslog(LOG_ERR, "Can't register mount");
3571558Srgrimes		exit(1);
3581558Srgrimes	}
3591558Srgrimes	svc_run();
3601558Srgrimes	syslog(LOG_ERR, "Mountd died");
3611558Srgrimes	exit(1);
3621558Srgrimes}
3631558Srgrimes
3641558Srgrimes/*
3651558Srgrimes * The mount rpc service
3661558Srgrimes */
3671558Srgrimesvoid
3681558Srgrimesmntsrv(rqstp, transp)
3691558Srgrimes	struct svc_req *rqstp;
3701558Srgrimes	SVCXPRT *transp;
3711558Srgrimes{
3721558Srgrimes	struct exportlist *ep;
3731558Srgrimes	struct dirlist *dp;
3749336Sdfr	struct fhreturn fhr;
3751558Srgrimes	struct stat stb;
3761558Srgrimes	struct statfs fsb;
3771558Srgrimes	struct hostent *hp;
37831656Sguido	struct in_addr saddrin;
3791558Srgrimes	u_long saddr;
3809336Sdfr	u_short sport;
38123681Speter	char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
38228911Sguido	int bad = 0, defset, hostset;
3839336Sdfr	sigset_t sighup_mask;
3841558Srgrimes
3859336Sdfr	sigemptyset(&sighup_mask);
3869336Sdfr	sigaddset(&sighup_mask, SIGHUP);
3871558Srgrimes	saddr = transp->xp_raddr.sin_addr.s_addr;
38831656Sguido	saddrin = transp->xp_raddr.sin_addr;
3899336Sdfr	sport = ntohs(transp->xp_raddr.sin_port);
3901558Srgrimes	hp = (struct hostent *)NULL;
3911558Srgrimes	switch (rqstp->rq_proc) {
3921558Srgrimes	case NULLPROC:
3931558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
3941558Srgrimes			syslog(LOG_ERR, "Can't send reply");
3951558Srgrimes		return;
3961558Srgrimes	case RPCMNT_MOUNT:
3979336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
39831656Sguido			syslog(LOG_NOTICE,
39931656Sguido			    "mount request from %s from unprivileged port",
40031656Sguido			    inet_ntoa(saddrin));
4011558Srgrimes			svcerr_weakauth(transp);
4021558Srgrimes			return;
4031558Srgrimes		}
4041558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
40531656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
40631656Sguido			    inet_ntoa(saddrin));
4071558Srgrimes			svcerr_decode(transp);
4081558Srgrimes			return;
4091558Srgrimes		}
4101558Srgrimes
4111558Srgrimes		/*
4121558Srgrimes		 * Get the real pathname and make sure it is a directory
4139336Sdfr		 * or a regular file if the -r option was specified
4149336Sdfr		 * and it exists.
4151558Srgrimes		 */
4161558Srgrimes		if (realpath(rpcpath, dirpath) == 0 ||
4171558Srgrimes		    stat(dirpath, &stb) < 0 ||
4189336Sdfr		    (!S_ISDIR(stb.st_mode) &&
4199336Sdfr		     (dir_only || !S_ISREG(stb.st_mode))) ||
4201558Srgrimes		    statfs(dirpath, &fsb) < 0) {
4211558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
42231656Sguido			syslog(LOG_NOTICE,
42331656Sguido			    "mount request from %s for non existant path %s",
42431656Sguido			    inet_ntoa(saddrin), dirpath);
4251558Srgrimes			if (debug)
4261558Srgrimes				fprintf(stderr, "stat failed on %s\n", dirpath);
42728911Sguido			bad = ENOENT;	/* We will send error reply later */
4281558Srgrimes		}
4291558Srgrimes
4301558Srgrimes		/* Check in the exports list */
4319336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
4321558Srgrimes		ep = ex_search(&fsb.f_fsid);
4339336Sdfr		hostset = defset = 0;
4349336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
4351558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
4369336Sdfr		     chk_host(dp, saddr, &defset, &hostset)) ||
4371558Srgrimes		     (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
4381558Srgrimes		      scan_tree(ep->ex_dirl, saddr) == 0))) {
43928911Sguido			if (bad) {
44028911Sguido				if (!svc_sendreply(transp, xdr_long,
44128911Sguido				    (caddr_t)&bad))
44228911Sguido					syslog(LOG_ERR, "Can't send reply");
44328911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
44428911Sguido				return;
44528911Sguido			}
4469336Sdfr			if (hostset & DP_HOSTSET)
4479336Sdfr				fhr.fhr_flag = hostset;
4489336Sdfr			else
4499336Sdfr				fhr.fhr_flag = defset;
4509336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
4511558Srgrimes			/* Get the file handle */
45223681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
4539336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
4541558Srgrimes				bad = errno;
4551558Srgrimes				syslog(LOG_ERR, "Can't get fh for %s", dirpath);
4561558Srgrimes				if (!svc_sendreply(transp, xdr_long,
4571558Srgrimes				    (caddr_t)&bad))
4581558Srgrimes					syslog(LOG_ERR, "Can't send reply");
4599336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
4601558Srgrimes				return;
4611558Srgrimes			}
4629336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
4631558Srgrimes				syslog(LOG_ERR, "Can't send reply");
4641558Srgrimes			if (hp == NULL)
4651558Srgrimes				hp = gethostbyaddr((caddr_t)&saddr,
4661558Srgrimes				    sizeof(saddr), AF_INET);
4671558Srgrimes			if (hp)
4681558Srgrimes				add_mlist(hp->h_name, dirpath);
4691558Srgrimes			else
47031656Sguido				add_mlist(inet_ntoa(saddrin),
4711558Srgrimes					dirpath);
4721558Srgrimes			if (debug)
4731558Srgrimes				fprintf(stderr,"Mount successfull.\n");
47431656Sguido			if (log)
47531656Sguido				syslog(LOG_NOTICE,
47631656Sguido				    "mount request succeeded from %s for %s",
47731656Sguido				    inet_ntoa(saddrin), dirpath);
47831656Sguido		} else {
4791558Srgrimes			bad = EACCES;
48031656Sguido			syslog(LOG_NOTICE,
48131656Sguido			    "mount request denied from %s for %s",
48231656Sguido			    inet_ntoa(saddrin), dirpath);
48331656Sguido		}
48428911Sguido
48528911Sguido		if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
48628911Sguido			syslog(LOG_ERR, "Can't send reply");
4879336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
4881558Srgrimes		return;
4891558Srgrimes	case RPCMNT_DUMP:
4901558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
4911558Srgrimes			syslog(LOG_ERR, "Can't send reply");
49231656Sguido		else if (log)
49331656Sguido			syslog(LOG_NOTICE,
49431656Sguido			    "dump request succeeded from %s",
49531656Sguido			    inet_ntoa(saddrin), dirpath);
4961558Srgrimes		return;
4971558Srgrimes	case RPCMNT_UMOUNT:
4989336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
49931656Sguido			syslog(LOG_NOTICE,
50031656Sguido			    "umount request from %s from unprivileged port",
50131656Sguido			    inet_ntoa(saddrin));
5021558Srgrimes			svcerr_weakauth(transp);
5031558Srgrimes			return;
5041558Srgrimes		}
5051558Srgrimes		if (!svc_getargs(transp, xdr_dir, dirpath)) {
50631656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
50731656Sguido			    inet_ntoa(saddrin));
5081558Srgrimes			svcerr_decode(transp);
5091558Srgrimes			return;
5101558Srgrimes		}
5111558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
5121558Srgrimes			syslog(LOG_ERR, "Can't send reply");
5131558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
5141558Srgrimes		if (hp)
5151558Srgrimes			del_mlist(hp->h_name, dirpath);
51631656Sguido		del_mlist(inet_ntoa(saddrin), dirpath);
51731656Sguido		if (log)
51831656Sguido			syslog(LOG_NOTICE,
51931656Sguido			    "umount request succeeded from %s for %s",
52031656Sguido			    inet_ntoa(saddrin), dirpath);
5211558Srgrimes		return;
5221558Srgrimes	case RPCMNT_UMNTALL:
5239336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
52431656Sguido			syslog(LOG_NOTICE,
52531656Sguido			    "umountall request from %s from unprivileged port",
52631656Sguido			    inet_ntoa(saddrin));
5271558Srgrimes			svcerr_weakauth(transp);
5281558Srgrimes			return;
5291558Srgrimes		}
5301558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
5311558Srgrimes			syslog(LOG_ERR, "Can't send reply");
5321558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
5331558Srgrimes		if (hp)
5341558Srgrimes			del_mlist(hp->h_name, (char *)NULL);
53531656Sguido		del_mlist(inet_ntoa(saddrin), (char *)NULL);
53631656Sguido		if (log)
53731656Sguido			syslog(LOG_NOTICE,
53831656Sguido			    "umountall request succeeded from %s",
53931656Sguido			    inet_ntoa(saddrin));
5401558Srgrimes		return;
5411558Srgrimes	case RPCMNT_EXPORT:
5421558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
5431558Srgrimes			syslog(LOG_ERR, "Can't send reply");
54431656Sguido		if (log)
54531656Sguido			syslog(LOG_NOTICE,
54631656Sguido			    "export request succeeded from %s",
54731656Sguido			    inet_ntoa(saddrin));
5481558Srgrimes		return;
5491558Srgrimes	default:
5501558Srgrimes		svcerr_noproc(transp);
5511558Srgrimes		return;
5521558Srgrimes	}
5531558Srgrimes}
5541558Srgrimes
5551558Srgrimes/*
5561558Srgrimes * Xdr conversion for a dirpath string
5571558Srgrimes */
5581558Srgrimesint
5591558Srgrimesxdr_dir(xdrsp, dirp)
5601558Srgrimes	XDR *xdrsp;
5611558Srgrimes	char *dirp;
5621558Srgrimes{
5631558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
5641558Srgrimes}
5651558Srgrimes
5661558Srgrimes/*
5679336Sdfr * Xdr routine to generate file handle reply
5681558Srgrimes */
5691558Srgrimesint
5709336Sdfrxdr_fhs(xdrsp, cp)
5711558Srgrimes	XDR *xdrsp;
5729336Sdfr	caddr_t cp;
5731558Srgrimes{
5749336Sdfr	register struct fhreturn *fhrp = (struct fhreturn *)cp;
5759336Sdfr	u_long ok = 0, len, auth;
5761558Srgrimes
5771558Srgrimes	if (!xdr_long(xdrsp, &ok))
5781558Srgrimes		return (0);
5799336Sdfr	switch (fhrp->fhr_vers) {
5809336Sdfr	case 1:
5819336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
5829336Sdfr	case 3:
5839336Sdfr		len = NFSX_V3FH;
5849336Sdfr		if (!xdr_long(xdrsp, &len))
5859336Sdfr			return (0);
5869336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
5879336Sdfr			return (0);
5889336Sdfr		if (fhrp->fhr_flag & DP_KERB)
5899336Sdfr			auth = RPCAUTH_KERB4;
5909336Sdfr		else
5919336Sdfr			auth = RPCAUTH_UNIX;
5929336Sdfr		len = 1;
5939336Sdfr		if (!xdr_long(xdrsp, &len))
5949336Sdfr			return (0);
5959336Sdfr		return (xdr_long(xdrsp, &auth));
5969336Sdfr	};
5979336Sdfr	return (0);
5981558Srgrimes}
5991558Srgrimes
6001558Srgrimesint
6011558Srgrimesxdr_mlist(xdrsp, cp)
6021558Srgrimes	XDR *xdrsp;
6031558Srgrimes	caddr_t cp;
6041558Srgrimes{
6051558Srgrimes	struct mountlist *mlp;
6061558Srgrimes	int true = 1;
6071558Srgrimes	int false = 0;
6081558Srgrimes	char *strp;
6091558Srgrimes
6101558Srgrimes	mlp = mlhead;
6111558Srgrimes	while (mlp) {
6121558Srgrimes		if (!xdr_bool(xdrsp, &true))
6131558Srgrimes			return (0);
6141558Srgrimes		strp = &mlp->ml_host[0];
6151558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
6161558Srgrimes			return (0);
6171558Srgrimes		strp = &mlp->ml_dirp[0];
6181558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6191558Srgrimes			return (0);
6201558Srgrimes		mlp = mlp->ml_next;
6211558Srgrimes	}
6221558Srgrimes	if (!xdr_bool(xdrsp, &false))
6231558Srgrimes		return (0);
6241558Srgrimes	return (1);
6251558Srgrimes}
6261558Srgrimes
6271558Srgrimes/*
6281558Srgrimes * Xdr conversion for export list
6291558Srgrimes */
6301558Srgrimesint
6311558Srgrimesxdr_explist(xdrsp, cp)
6321558Srgrimes	XDR *xdrsp;
6331558Srgrimes	caddr_t cp;
6341558Srgrimes{
6351558Srgrimes	struct exportlist *ep;
6361558Srgrimes	int false = 0;
6379336Sdfr	int putdef;
6389336Sdfr	sigset_t sighup_mask;
6391558Srgrimes
6409336Sdfr	sigemptyset(&sighup_mask);
6419336Sdfr	sigaddset(&sighup_mask, SIGHUP);
6429336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
6431558Srgrimes	ep = exphead;
6441558Srgrimes	while (ep) {
6451558Srgrimes		putdef = 0;
6461558Srgrimes		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
6471558Srgrimes			goto errout;
6481558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
6491558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
6501558Srgrimes			&putdef))
6511558Srgrimes			goto errout;
6521558Srgrimes		ep = ep->ex_next;
6531558Srgrimes	}
6549336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6551558Srgrimes	if (!xdr_bool(xdrsp, &false))
6561558Srgrimes		return (0);
6571558Srgrimes	return (1);
6581558Srgrimeserrout:
6599336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6601558Srgrimes	return (0);
6611558Srgrimes}
6621558Srgrimes
6631558Srgrimes/*
6641558Srgrimes * Called from xdr_explist() to traverse the tree and export the
6651558Srgrimes * directory paths.
6661558Srgrimes */
6671558Srgrimesint
6681558Srgrimesput_exlist(dp, xdrsp, adp, putdefp)
6691558Srgrimes	struct dirlist *dp;
6701558Srgrimes	XDR *xdrsp;
6711558Srgrimes	struct dirlist *adp;
6721558Srgrimes	int *putdefp;
6731558Srgrimes{
6741558Srgrimes	struct grouplist *grp;
6751558Srgrimes	struct hostlist *hp;
6761558Srgrimes	int true = 1;
6771558Srgrimes	int false = 0;
6781558Srgrimes	int gotalldir = 0;
6791558Srgrimes	char *strp;
6801558Srgrimes
6811558Srgrimes	if (dp) {
6821558Srgrimes		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
6831558Srgrimes			return (1);
6841558Srgrimes		if (!xdr_bool(xdrsp, &true))
6851558Srgrimes			return (1);
6861558Srgrimes		strp = dp->dp_dirp;
6871558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6881558Srgrimes			return (1);
6891558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
6901558Srgrimes			gotalldir = 1;
6911558Srgrimes			*putdefp = 1;
6921558Srgrimes		}
6931558Srgrimes		if ((dp->dp_flag & DP_DEFSET) == 0 &&
6941558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
6951558Srgrimes			hp = dp->dp_hosts;
6961558Srgrimes			while (hp) {
6971558Srgrimes				grp = hp->ht_grp;
6981558Srgrimes				if (grp->gr_type == GT_HOST) {
6991558Srgrimes					if (!xdr_bool(xdrsp, &true))
7001558Srgrimes						return (1);
7011558Srgrimes					strp = grp->gr_ptr.gt_hostent->h_name;
7028871Srgrimes					if (!xdr_string(xdrsp, &strp,
7031558Srgrimes					    RPCMNT_NAMELEN))
7041558Srgrimes						return (1);
7051558Srgrimes				} else if (grp->gr_type == GT_NET) {
7061558Srgrimes					if (!xdr_bool(xdrsp, &true))
7071558Srgrimes						return (1);
7081558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
7098871Srgrimes					if (!xdr_string(xdrsp, &strp,
7101558Srgrimes					    RPCMNT_NAMELEN))
7111558Srgrimes						return (1);
7121558Srgrimes				}
7131558Srgrimes				hp = hp->ht_next;
7141558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
7151558Srgrimes					hp = adp->dp_hosts;
7161558Srgrimes					gotalldir = 0;
7171558Srgrimes				}
7181558Srgrimes			}
7191558Srgrimes		}
7201558Srgrimes		if (!xdr_bool(xdrsp, &false))
7211558Srgrimes			return (1);
7221558Srgrimes		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
7231558Srgrimes			return (1);
7241558Srgrimes	}
7251558Srgrimes	return (0);
7261558Srgrimes}
7271558Srgrimes
7281558Srgrimes#define LINESIZ	10240
7291558Srgrimeschar line[LINESIZ];
7301558SrgrimesFILE *exp_file;
7311558Srgrimes
7321558Srgrimes/*
7331558Srgrimes * Get the export list
7341558Srgrimes */
7351558Srgrimesvoid
7361558Srgrimesget_exportlist()
7371558Srgrimes{
7381558Srgrimes	struct exportlist *ep, *ep2;
7391558Srgrimes	struct grouplist *grp, *tgrp;
7401558Srgrimes	struct exportlist **epp;
7411558Srgrimes	struct dirlist *dirhead;
7421558Srgrimes	struct statfs fsb, *fsp;
7431558Srgrimes	struct hostent *hpe;
7441558Srgrimes	struct ucred anon;
7451558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
7461558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
7471558Srgrimes
7481558Srgrimes	/*
7491558Srgrimes	 * First, get rid of the old list
7501558Srgrimes	 */
7511558Srgrimes	ep = exphead;
7521558Srgrimes	while (ep) {
7531558Srgrimes		ep2 = ep;
7541558Srgrimes		ep = ep->ex_next;
7551558Srgrimes		free_exp(ep2);
7561558Srgrimes	}
7571558Srgrimes	exphead = (struct exportlist *)NULL;
7581558Srgrimes
7591558Srgrimes	grp = grphead;
7601558Srgrimes	while (grp) {
7611558Srgrimes		tgrp = grp;
7621558Srgrimes		grp = grp->gr_next;
7631558Srgrimes		free_grp(tgrp);
7641558Srgrimes	}
7651558Srgrimes	grphead = (struct grouplist *)NULL;
7661558Srgrimes
7671558Srgrimes	/*
7681558Srgrimes	 * And delete exports that are in the kernel for all local
7691558Srgrimes	 * file systems.
7701558Srgrimes	 * XXX: Should know how to handle all local exportable file systems
77123681Speter	 *      instead of just "ufs".
7721558Srgrimes	 */
7731558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
7741558Srgrimes	for (i = 0; i < num; i++) {
7751558Srgrimes		union {
7761558Srgrimes			struct ufs_args ua;
7771558Srgrimes			struct iso_args ia;
7781558Srgrimes			struct mfs_args ma;
7799336Sdfr			struct msdosfs_args da;
7801558Srgrimes		} targs;
7811558Srgrimes
78223681Speter		if (!strcmp(fsp->f_fstypename, "mfs") ||
78323681Speter		    !strcmp(fsp->f_fstypename, "ufs") ||
78423681Speter		    !strcmp(fsp->f_fstypename, "msdos") ||
78523681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
7869336Sdfr			targs.ua.fspec = NULL;
7879336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
7889336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
7891558Srgrimes				  fsp->f_flags | MNT_UPDATE,
7901558Srgrimes				  (caddr_t)&targs) < 0)
7911558Srgrimes				syslog(LOG_ERR, "Can't delete exports for %s",
7921558Srgrimes				       fsp->f_mntonname);
7931558Srgrimes		}
7941558Srgrimes		fsp++;
7951558Srgrimes	}
7961558Srgrimes
7971558Srgrimes	/*
7981558Srgrimes	 * Read in the exports file and build the list, calling
7991558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
8001558Srgrimes	 */
8011558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
8021558Srgrimes		syslog(LOG_ERR, "Can't open %s", exname);
8031558Srgrimes		exit(2);
8041558Srgrimes	}
8051558Srgrimes	dirhead = (struct dirlist *)NULL;
8061558Srgrimes	while (get_line()) {
8071558Srgrimes		if (debug)
8081558Srgrimes			fprintf(stderr,"Got line %s\n",line);
8091558Srgrimes		cp = line;
8101558Srgrimes		nextfield(&cp, &endcp);
8111558Srgrimes		if (*cp == '#')
8121558Srgrimes			goto nextline;
8131558Srgrimes
8141558Srgrimes		/*
8151558Srgrimes		 * Set defaults.
8161558Srgrimes		 */
8171558Srgrimes		has_host = FALSE;
8181558Srgrimes		anon = def_anon;
8191558Srgrimes		exflags = MNT_EXPORTED;
8201558Srgrimes		got_nondir = 0;
8211558Srgrimes		opt_flags = 0;
8221558Srgrimes		ep = (struct exportlist *)NULL;
8231558Srgrimes
8241558Srgrimes		/*
8251558Srgrimes		 * Create new exports list entry
8261558Srgrimes		 */
8271558Srgrimes		len = endcp-cp;
8281558Srgrimes		tgrp = grp = get_grp();
8291558Srgrimes		while (len > 0) {
8301558Srgrimes			if (len > RPCMNT_NAMELEN) {
8311558Srgrimes			    getexp_err(ep, tgrp);
8321558Srgrimes			    goto nextline;
8331558Srgrimes			}
8341558Srgrimes			if (*cp == '-') {
8351558Srgrimes			    if (ep == (struct exportlist *)NULL) {
8361558Srgrimes				getexp_err(ep, tgrp);
8371558Srgrimes				goto nextline;
8381558Srgrimes			    }
8391558Srgrimes			    if (debug)
8401558Srgrimes				fprintf(stderr, "doing opt %s\n", cp);
8411558Srgrimes			    got_nondir = 1;
8421558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
8431558Srgrimes				&exflags, &anon)) {
8441558Srgrimes				getexp_err(ep, tgrp);
8451558Srgrimes				goto nextline;
8461558Srgrimes			    }
8471558Srgrimes			} else if (*cp == '/') {
8481558Srgrimes			    savedc = *endcp;
8491558Srgrimes			    *endcp = '\0';
8501558Srgrimes			    if (check_dirpath(cp) &&
8511558Srgrimes				statfs(cp, &fsb) >= 0) {
8521558Srgrimes				if (got_nondir) {
8531558Srgrimes				    syslog(LOG_ERR, "Dirs must be first");
8541558Srgrimes				    getexp_err(ep, tgrp);
8551558Srgrimes				    goto nextline;
8561558Srgrimes				}
8571558Srgrimes				if (ep) {
8581558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
8591558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
8601558Srgrimes					getexp_err(ep, tgrp);
8611558Srgrimes					goto nextline;
8621558Srgrimes				    }
8631558Srgrimes				} else {
8641558Srgrimes				    /*
8651558Srgrimes				     * See if this directory is already
8661558Srgrimes				     * in the list.
8671558Srgrimes				     */
8681558Srgrimes				    ep = ex_search(&fsb.f_fsid);
8691558Srgrimes				    if (ep == (struct exportlist *)NULL) {
8701558Srgrimes					ep = get_exp();
8711558Srgrimes					ep->ex_fs = fsb.f_fsid;
8721558Srgrimes					ep->ex_fsdir = (char *)
8731558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
8741558Srgrimes					if (ep->ex_fsdir)
8751558Srgrimes					    strcpy(ep->ex_fsdir,
8761558Srgrimes						fsb.f_mntonname);
8771558Srgrimes					else
8781558Srgrimes					    out_of_mem();
8791558Srgrimes					if (debug)
8801558Srgrimes					  fprintf(stderr,
8811558Srgrimes					      "Making new ep fs=0x%x,0x%x\n",
8821558Srgrimes					      fsb.f_fsid.val[0],
8831558Srgrimes					      fsb.f_fsid.val[1]);
8841558Srgrimes				    } else if (debug)
8851558Srgrimes					fprintf(stderr,
8861558Srgrimes					    "Found ep fs=0x%x,0x%x\n",
8871558Srgrimes					    fsb.f_fsid.val[0],
8881558Srgrimes					    fsb.f_fsid.val[1]);
8891558Srgrimes				}
8901558Srgrimes
8911558Srgrimes				/*
8921558Srgrimes				 * Add dirpath to export mount point.
8931558Srgrimes				 */
8941558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
8951558Srgrimes				dirplen = len;
8961558Srgrimes			    } else {
8971558Srgrimes				getexp_err(ep, tgrp);
8981558Srgrimes				goto nextline;
8991558Srgrimes			    }
9001558Srgrimes			    *endcp = savedc;
9011558Srgrimes			} else {
9021558Srgrimes			    savedc = *endcp;
9031558Srgrimes			    *endcp = '\0';
9041558Srgrimes			    got_nondir = 1;
9051558Srgrimes			    if (ep == (struct exportlist *)NULL) {
9061558Srgrimes				getexp_err(ep, tgrp);
9071558Srgrimes				goto nextline;
9081558Srgrimes			    }
9091558Srgrimes
9101558Srgrimes			    /*
9111558Srgrimes			     * Get the host or netgroup.
9121558Srgrimes			     */
9131558Srgrimes			    setnetgrent(cp);
9141558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
9151558Srgrimes			    do {
9161558Srgrimes				if (has_host) {
9171558Srgrimes				    grp->gr_next = get_grp();
9181558Srgrimes				    grp = grp->gr_next;
9191558Srgrimes				}
9201558Srgrimes				if (netgrp) {
9217401Swpaul				    if (get_host(hst, grp, tgrp)) {
92229317Sjlemon					syslog(LOG_ERR, "Bad host %s in netgroup %s, skipping", hst, cp);
92329317Sjlemon					grp->gr_type = GT_IGNORE;
9241558Srgrimes				    }
9257401Swpaul				} else if (get_host(cp, grp, tgrp)) {
92629317Sjlemon				    syslog(LOG_ERR, "Bad host %s, skipping", cp);
92729317Sjlemon				    grp->gr_type = GT_IGNORE;
9281558Srgrimes				}
9291558Srgrimes				has_host = TRUE;
9301558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
9311558Srgrimes			    endnetgrent();
9321558Srgrimes			    *endcp = savedc;
9331558Srgrimes			}
9341558Srgrimes			cp = endcp;
9351558Srgrimes			nextfield(&cp, &endcp);
9361558Srgrimes			len = endcp - cp;
9371558Srgrimes		}
9381558Srgrimes		if (check_options(dirhead)) {
9391558Srgrimes			getexp_err(ep, tgrp);
9401558Srgrimes			goto nextline;
9411558Srgrimes		}
9421558Srgrimes		if (!has_host) {
9431558Srgrimes			grp->gr_type = GT_HOST;
9441558Srgrimes			if (debug)
9451558Srgrimes				fprintf(stderr,"Adding a default entry\n");
9461558Srgrimes			/* add a default group and make the grp list NULL */
9471558Srgrimes			hpe = (struct hostent *)malloc(sizeof(struct hostent));
9481558Srgrimes			if (hpe == (struct hostent *)NULL)
9491558Srgrimes				out_of_mem();
95012348Sjoerg			hpe->h_name = strdup("Default");
9511558Srgrimes			hpe->h_addrtype = AF_INET;
9521558Srgrimes			hpe->h_length = sizeof (u_long);
9531558Srgrimes			hpe->h_addr_list = (char **)NULL;
9541558Srgrimes			grp->gr_ptr.gt_hostent = hpe;
9551558Srgrimes
9561558Srgrimes		/*
9571558Srgrimes		 * Don't allow a network export coincide with a list of
9581558Srgrimes		 * host(s) on the same line.
9591558Srgrimes		 */
9601558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
9611558Srgrimes			getexp_err(ep, tgrp);
9621558Srgrimes			goto nextline;
96329317Sjlemon
96429317Sjlemon        	/*
96529317Sjlemon	         * If an export list was specified on this line, make sure
96629317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
96729317Sjlemon		 */
96829317Sjlemon		} else {
96929317Sjlemon			grp = tgrp;
97029317Sjlemon        		while (grp && grp->gr_type == GT_IGNORE)
97129317Sjlemon				grp = grp->gr_next;
97229317Sjlemon			if (! grp) {
97329317Sjlemon			    getexp_err(ep, tgrp);
97429317Sjlemon			    goto nextline;
97529317Sjlemon			}
9761558Srgrimes		}
9771558Srgrimes
9781558Srgrimes		/*
9791558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
9801558Srgrimes		 * After loop, tgrp points to the start of the list and
9811558Srgrimes		 * grp points to the last entry in the list.
9821558Srgrimes		 */
9831558Srgrimes		grp = tgrp;
9841558Srgrimes		do {
9851558Srgrimes		    if (do_mount(ep, grp, exflags, &anon, dirp,
9861558Srgrimes			dirplen, &fsb)) {
9871558Srgrimes			getexp_err(ep, tgrp);
9881558Srgrimes			goto nextline;
9891558Srgrimes		    }
9901558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
9911558Srgrimes
9921558Srgrimes		/*
9931558Srgrimes		 * Success. Update the data structures.
9941558Srgrimes		 */
9951558Srgrimes		if (has_host) {
9969336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
9971558Srgrimes			grp->gr_next = grphead;
9981558Srgrimes			grphead = tgrp;
9991558Srgrimes		} else {
10001558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
10019336Sdfr				opt_flags);
10021558Srgrimes			free_grp(grp);
10031558Srgrimes		}
10041558Srgrimes		dirhead = (struct dirlist *)NULL;
10051558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
10061558Srgrimes			ep2 = exphead;
10071558Srgrimes			epp = &exphead;
10081558Srgrimes
10091558Srgrimes			/*
10101558Srgrimes			 * Insert in the list in alphabetical order.
10111558Srgrimes			 */
10121558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
10131558Srgrimes				epp = &ep2->ex_next;
10141558Srgrimes				ep2 = ep2->ex_next;
10151558Srgrimes			}
10161558Srgrimes			if (ep2)
10171558Srgrimes				ep->ex_next = ep2;
10181558Srgrimes			*epp = ep;
10191558Srgrimes			ep->ex_flag |= EX_LINKED;
10201558Srgrimes		}
10211558Srgrimesnextline:
10221558Srgrimes		if (dirhead) {
10231558Srgrimes			free_dir(dirhead);
10241558Srgrimes			dirhead = (struct dirlist *)NULL;
10251558Srgrimes		}
10261558Srgrimes	}
10271558Srgrimes	fclose(exp_file);
10281558Srgrimes}
10291558Srgrimes
10301558Srgrimes/*
10311558Srgrimes * Allocate an export list element
10321558Srgrimes */
10331558Srgrimesstruct exportlist *
10341558Srgrimesget_exp()
10351558Srgrimes{
10361558Srgrimes	struct exportlist *ep;
10371558Srgrimes
10381558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
10391558Srgrimes	if (ep == (struct exportlist *)NULL)
10401558Srgrimes		out_of_mem();
104123681Speter	memset(ep, 0, sizeof(struct exportlist));
10421558Srgrimes	return (ep);
10431558Srgrimes}
10441558Srgrimes
10451558Srgrimes/*
10461558Srgrimes * Allocate a group list element
10471558Srgrimes */
10481558Srgrimesstruct grouplist *
10491558Srgrimesget_grp()
10501558Srgrimes{
10511558Srgrimes	struct grouplist *gp;
10521558Srgrimes
10531558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
10541558Srgrimes	if (gp == (struct grouplist *)NULL)
10551558Srgrimes		out_of_mem();
105623681Speter	memset(gp, 0, sizeof(struct grouplist));
10571558Srgrimes	return (gp);
10581558Srgrimes}
10591558Srgrimes
10601558Srgrimes/*
10611558Srgrimes * Clean up upon an error in get_exportlist().
10621558Srgrimes */
10631558Srgrimesvoid
10641558Srgrimesgetexp_err(ep, grp)
10651558Srgrimes	struct exportlist *ep;
10661558Srgrimes	struct grouplist *grp;
10671558Srgrimes{
10681558Srgrimes	struct grouplist *tgrp;
10691558Srgrimes
10701558Srgrimes	syslog(LOG_ERR, "Bad exports list line %s", line);
10711558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
10721558Srgrimes		free_exp(ep);
10731558Srgrimes	while (grp) {
10741558Srgrimes		tgrp = grp;
10751558Srgrimes		grp = grp->gr_next;
10761558Srgrimes		free_grp(tgrp);
10771558Srgrimes	}
10781558Srgrimes}
10791558Srgrimes
10801558Srgrimes/*
10811558Srgrimes * Search the export list for a matching fs.
10821558Srgrimes */
10831558Srgrimesstruct exportlist *
10841558Srgrimesex_search(fsid)
10851558Srgrimes	fsid_t *fsid;
10861558Srgrimes{
10871558Srgrimes	struct exportlist *ep;
10881558Srgrimes
10891558Srgrimes	ep = exphead;
10901558Srgrimes	while (ep) {
10911558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
10921558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
10931558Srgrimes			return (ep);
10941558Srgrimes		ep = ep->ex_next;
10951558Srgrimes	}
10961558Srgrimes	return (ep);
10971558Srgrimes}
10981558Srgrimes
10991558Srgrimes/*
11001558Srgrimes * Add a directory path to the list.
11011558Srgrimes */
11021558Srgrimeschar *
11031558Srgrimesadd_expdir(dpp, cp, len)
11041558Srgrimes	struct dirlist **dpp;
11051558Srgrimes	char *cp;
11061558Srgrimes	int len;
11071558Srgrimes{
11081558Srgrimes	struct dirlist *dp;
11091558Srgrimes
11101558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
11111558Srgrimes	dp->dp_left = *dpp;
11121558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
11131558Srgrimes	dp->dp_flag = 0;
11141558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
11151558Srgrimes	strcpy(dp->dp_dirp, cp);
11161558Srgrimes	*dpp = dp;
11171558Srgrimes	return (dp->dp_dirp);
11181558Srgrimes}
11191558Srgrimes
11201558Srgrimes/*
11211558Srgrimes * Hang the dir list element off the dirpath binary tree as required
11221558Srgrimes * and update the entry for host.
11231558Srgrimes */
11241558Srgrimesvoid
11259336Sdfrhang_dirp(dp, grp, ep, flags)
11261558Srgrimes	struct dirlist *dp;
11271558Srgrimes	struct grouplist *grp;
11281558Srgrimes	struct exportlist *ep;
11299336Sdfr	int flags;
11301558Srgrimes{
11311558Srgrimes	struct hostlist *hp;
11321558Srgrimes	struct dirlist *dp2;
11331558Srgrimes
11349336Sdfr	if (flags & OP_ALLDIRS) {
11351558Srgrimes		if (ep->ex_defdir)
11361558Srgrimes			free((caddr_t)dp);
11371558Srgrimes		else
11381558Srgrimes			ep->ex_defdir = dp;
11399336Sdfr		if (grp == (struct grouplist *)NULL) {
11401558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
11419336Sdfr			if (flags & OP_KERB)
11429336Sdfr				ep->ex_defdir->dp_flag |= DP_KERB;
11439336Sdfr		} else while (grp) {
11441558Srgrimes			hp = get_ht();
11459336Sdfr			if (flags & OP_KERB)
11469336Sdfr				hp->ht_flag |= DP_KERB;
11471558Srgrimes			hp->ht_grp = grp;
11481558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
11491558Srgrimes			ep->ex_defdir->dp_hosts = hp;
11501558Srgrimes			grp = grp->gr_next;
11511558Srgrimes		}
11521558Srgrimes	} else {
11531558Srgrimes
11541558Srgrimes		/*
11551558Srgrimes		 * Loop throught the directories adding them to the tree.
11561558Srgrimes		 */
11571558Srgrimes		while (dp) {
11581558Srgrimes			dp2 = dp->dp_left;
11599336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
11601558Srgrimes			dp = dp2;
11611558Srgrimes		}
11621558Srgrimes	}
11631558Srgrimes}
11641558Srgrimes
11651558Srgrimes/*
11661558Srgrimes * Traverse the binary tree either updating a node that is already there
11671558Srgrimes * for the new directory or adding the new node.
11681558Srgrimes */
11691558Srgrimesvoid
11709336Sdfradd_dlist(dpp, newdp, grp, flags)
11711558Srgrimes	struct dirlist **dpp;
11721558Srgrimes	struct dirlist *newdp;
11731558Srgrimes	struct grouplist *grp;
11749336Sdfr	int flags;
11751558Srgrimes{
11761558Srgrimes	struct dirlist *dp;
11771558Srgrimes	struct hostlist *hp;
11781558Srgrimes	int cmp;
11791558Srgrimes
11801558Srgrimes	dp = *dpp;
11811558Srgrimes	if (dp) {
11821558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
11831558Srgrimes		if (cmp > 0) {
11849336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
11851558Srgrimes			return;
11861558Srgrimes		} else if (cmp < 0) {
11879336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
11881558Srgrimes			return;
11891558Srgrimes		} else
11901558Srgrimes			free((caddr_t)newdp);
11911558Srgrimes	} else {
11921558Srgrimes		dp = newdp;
11931558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
11941558Srgrimes		*dpp = dp;
11951558Srgrimes	}
11961558Srgrimes	if (grp) {
11971558Srgrimes
11981558Srgrimes		/*
11991558Srgrimes		 * Hang all of the host(s) off of the directory point.
12001558Srgrimes		 */
12011558Srgrimes		do {
12021558Srgrimes			hp = get_ht();
12039336Sdfr			if (flags & OP_KERB)
12049336Sdfr				hp->ht_flag |= DP_KERB;
12051558Srgrimes			hp->ht_grp = grp;
12061558Srgrimes			hp->ht_next = dp->dp_hosts;
12071558Srgrimes			dp->dp_hosts = hp;
12081558Srgrimes			grp = grp->gr_next;
12091558Srgrimes		} while (grp);
12109336Sdfr	} else {
12111558Srgrimes		dp->dp_flag |= DP_DEFSET;
12129336Sdfr		if (flags & OP_KERB)
12139336Sdfr			dp->dp_flag |= DP_KERB;
12149336Sdfr	}
12151558Srgrimes}
12161558Srgrimes
12171558Srgrimes/*
12181558Srgrimes * Search for a dirpath on the export point.
12191558Srgrimes */
12201558Srgrimesstruct dirlist *
12211558Srgrimesdirp_search(dp, dirpath)
12221558Srgrimes	struct dirlist *dp;
12231558Srgrimes	char *dirpath;
12241558Srgrimes{
12251558Srgrimes	int cmp;
12261558Srgrimes
12271558Srgrimes	if (dp) {
12281558Srgrimes		cmp = strcmp(dp->dp_dirp, dirpath);
12291558Srgrimes		if (cmp > 0)
12301558Srgrimes			return (dirp_search(dp->dp_left, dirpath));
12311558Srgrimes		else if (cmp < 0)
12321558Srgrimes			return (dirp_search(dp->dp_right, dirpath));
12331558Srgrimes		else
12341558Srgrimes			return (dp);
12351558Srgrimes	}
12361558Srgrimes	return (dp);
12371558Srgrimes}
12381558Srgrimes
12391558Srgrimes/*
12401558Srgrimes * Scan for a host match in a directory tree.
12411558Srgrimes */
12421558Srgrimesint
12439336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
12441558Srgrimes	struct dirlist *dp;
12451558Srgrimes	u_long saddr;
12461558Srgrimes	int *defsetp;
12479336Sdfr	int *hostsetp;
12481558Srgrimes{
12491558Srgrimes	struct hostlist *hp;
12501558Srgrimes	struct grouplist *grp;
12511558Srgrimes	u_long **addrp;
12521558Srgrimes
12531558Srgrimes	if (dp) {
12541558Srgrimes		if (dp->dp_flag & DP_DEFSET)
12559336Sdfr			*defsetp = dp->dp_flag;
12561558Srgrimes		hp = dp->dp_hosts;
12571558Srgrimes		while (hp) {
12581558Srgrimes			grp = hp->ht_grp;
12591558Srgrimes			switch (grp->gr_type) {
12601558Srgrimes			case GT_HOST:
12611558Srgrimes			    addrp = (u_long **)
12621558Srgrimes				grp->gr_ptr.gt_hostent->h_addr_list;
12631558Srgrimes			    while (*addrp) {
12649336Sdfr				if (**addrp == saddr) {
12659336Sdfr				    *hostsetp = (hp->ht_flag | DP_HOSTSET);
12661558Srgrimes				    return (1);
12679336Sdfr				}
12681558Srgrimes				addrp++;
12691558Srgrimes			    }
12701558Srgrimes			    break;
12711558Srgrimes			case GT_NET:
12721558Srgrimes			    if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
12739336Sdfr				grp->gr_ptr.gt_net.nt_net) {
12749336Sdfr				*hostsetp = (hp->ht_flag | DP_HOSTSET);
12751558Srgrimes				return (1);
12769336Sdfr			    }
12771558Srgrimes			    break;
12781558Srgrimes			};
12791558Srgrimes			hp = hp->ht_next;
12801558Srgrimes		}
12811558Srgrimes	}
12821558Srgrimes	return (0);
12831558Srgrimes}
12841558Srgrimes
12851558Srgrimes/*
12861558Srgrimes * Scan tree for a host that matches the address.
12871558Srgrimes */
12881558Srgrimesint
12891558Srgrimesscan_tree(dp, saddr)
12901558Srgrimes	struct dirlist *dp;
12911558Srgrimes	u_long saddr;
12921558Srgrimes{
12939336Sdfr	int defset, hostset;
12941558Srgrimes
12951558Srgrimes	if (dp) {
12961558Srgrimes		if (scan_tree(dp->dp_left, saddr))
12971558Srgrimes			return (1);
12989336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
12991558Srgrimes			return (1);
13001558Srgrimes		if (scan_tree(dp->dp_right, saddr))
13011558Srgrimes			return (1);
13021558Srgrimes	}
13031558Srgrimes	return (0);
13041558Srgrimes}
13051558Srgrimes
13061558Srgrimes/*
13071558Srgrimes * Traverse the dirlist tree and free it up.
13081558Srgrimes */
13091558Srgrimesvoid
13101558Srgrimesfree_dir(dp)
13111558Srgrimes	struct dirlist *dp;
13121558Srgrimes{
13131558Srgrimes
13141558Srgrimes	if (dp) {
13151558Srgrimes		free_dir(dp->dp_left);
13161558Srgrimes		free_dir(dp->dp_right);
13171558Srgrimes		free_host(dp->dp_hosts);
13181558Srgrimes		free((caddr_t)dp);
13191558Srgrimes	}
13201558Srgrimes}
13211558Srgrimes
13221558Srgrimes/*
13231558Srgrimes * Parse the option string and update fields.
13241558Srgrimes * Option arguments may either be -<option>=<value> or
13251558Srgrimes * -<option> <value>
13261558Srgrimes */
13271558Srgrimesint
13281558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
13291558Srgrimes	char **cpp, **endcpp;
13301558Srgrimes	struct exportlist *ep;
13311558Srgrimes	struct grouplist *grp;
13321558Srgrimes	int *has_hostp;
13331558Srgrimes	int *exflagsp;
13341558Srgrimes	struct ucred *cr;
13351558Srgrimes{
13361558Srgrimes	char *cpoptarg, *cpoptend;
13371558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
13381558Srgrimes	int allflag, usedarg;
13391558Srgrimes
13401558Srgrimes	cpopt = *cpp;
13411558Srgrimes	cpopt++;
13421558Srgrimes	cp = *endcpp;
13431558Srgrimes	savedc = *cp;
13441558Srgrimes	*cp = '\0';
13451558Srgrimes	while (cpopt && *cpopt) {
13461558Srgrimes		allflag = 1;
13471558Srgrimes		usedarg = -2;
134823681Speter		if (cpoptend = strchr(cpopt, ',')) {
13491558Srgrimes			*cpoptend++ = '\0';
135023681Speter			if (cpoptarg = strchr(cpopt, '='))
13511558Srgrimes				*cpoptarg++ = '\0';
13521558Srgrimes		} else {
135323681Speter			if (cpoptarg = strchr(cpopt, '='))
13541558Srgrimes				*cpoptarg++ = '\0';
13551558Srgrimes			else {
13561558Srgrimes				*cp = savedc;
13571558Srgrimes				nextfield(&cp, &endcp);
13581558Srgrimes				**endcpp = '\0';
13591558Srgrimes				if (endcp > cp && *cp != '-') {
13601558Srgrimes					cpoptarg = cp;
13611558Srgrimes					savedc2 = *endcp;
13621558Srgrimes					*endcp = '\0';
13631558Srgrimes					usedarg = 0;
13641558Srgrimes				}
13651558Srgrimes			}
13661558Srgrimes		}
13671558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
13681558Srgrimes			*exflagsp |= MNT_EXRDONLY;
13691558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
13701558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
13711558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
13721558Srgrimes			usedarg++;
13731558Srgrimes			parsecred(cpoptarg, cr);
13741558Srgrimes			if (allflag == 0) {
13751558Srgrimes				*exflagsp |= MNT_EXPORTANON;
13761558Srgrimes				opt_flags |= OP_MAPALL;
13771558Srgrimes			} else
13781558Srgrimes				opt_flags |= OP_MAPROOT;
13791558Srgrimes		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
13801558Srgrimes			*exflagsp |= MNT_EXKERB;
13811558Srgrimes			opt_flags |= OP_KERB;
13821558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
13831558Srgrimes			!strcmp(cpopt, "m"))) {
13841558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
13851558Srgrimes				syslog(LOG_ERR, "Bad mask: %s", cpoptarg);
13861558Srgrimes				return (1);
13871558Srgrimes			}
13881558Srgrimes			usedarg++;
13891558Srgrimes			opt_flags |= OP_MASK;
13901558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
13911558Srgrimes			!strcmp(cpopt, "n"))) {
13921558Srgrimes			if (grp->gr_type != GT_NULL) {
13931558Srgrimes				syslog(LOG_ERR, "Network/host conflict");
13941558Srgrimes				return (1);
13951558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
13961558Srgrimes				syslog(LOG_ERR, "Bad net: %s", cpoptarg);
13971558Srgrimes				return (1);
13981558Srgrimes			}
13991558Srgrimes			grp->gr_type = GT_NET;
14001558Srgrimes			*has_hostp = 1;
14011558Srgrimes			usedarg++;
14021558Srgrimes			opt_flags |= OP_NET;
14031558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
14041558Srgrimes			opt_flags |= OP_ALLDIRS;
140527447Sdfr		} else if (!strcmp(cpopt, "public")) {
140627447Sdfr			*exflagsp |= MNT_EXPUBLIC;
140727447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
140827447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
140927447Sdfr			opt_flags |= OP_MAPALL;
141027447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
141127447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
14121558Srgrimes#ifdef ISO
14131558Srgrimes		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
14141558Srgrimes			if (get_isoaddr(cpoptarg, grp)) {
14151558Srgrimes				syslog(LOG_ERR, "Bad iso addr: %s", cpoptarg);
14161558Srgrimes				return (1);
14171558Srgrimes			}
14181558Srgrimes			*has_hostp = 1;
14191558Srgrimes			usedarg++;
14201558Srgrimes			opt_flags |= OP_ISO;
14211558Srgrimes#endif /* ISO */
14221558Srgrimes		} else {
14231558Srgrimes			syslog(LOG_ERR, "Bad opt %s", cpopt);
14241558Srgrimes			return (1);
14251558Srgrimes		}
14261558Srgrimes		if (usedarg >= 0) {
14271558Srgrimes			*endcp = savedc2;
14281558Srgrimes			**endcpp = savedc;
14291558Srgrimes			if (usedarg > 0) {
14301558Srgrimes				*cpp = cp;
14311558Srgrimes				*endcpp = endcp;
14321558Srgrimes			}
14331558Srgrimes			return (0);
14341558Srgrimes		}
14351558Srgrimes		cpopt = cpoptend;
14361558Srgrimes	}
14371558Srgrimes	**endcpp = savedc;
14381558Srgrimes	return (0);
14391558Srgrimes}
14401558Srgrimes
14411558Srgrimes/*
14421558Srgrimes * Translate a character string to the corresponding list of network
14431558Srgrimes * addresses for a hostname.
14441558Srgrimes */
14451558Srgrimesint
14467401Swpaulget_host(cp, grp, tgrp)
14471558Srgrimes	char *cp;
14481558Srgrimes	struct grouplist *grp;
14497401Swpaul	struct grouplist *tgrp;
14501558Srgrimes{
14517401Swpaul	struct grouplist *checkgrp;
14521558Srgrimes	struct hostent *hp, *nhp;
14531558Srgrimes	char **addrp, **naddrp;
14541558Srgrimes	struct hostent t_host;
14551558Srgrimes	int i;
14561558Srgrimes	u_long saddr;
14571558Srgrimes	char *aptr[2];
14581558Srgrimes
14591558Srgrimes	if (grp->gr_type != GT_NULL)
14601558Srgrimes		return (1);
14611558Srgrimes	if ((hp = gethostbyname(cp)) == NULL) {
14621558Srgrimes		if (isdigit(*cp)) {
14631558Srgrimes			saddr = inet_addr(cp);
14641558Srgrimes			if (saddr == -1) {
14659336Sdfr 				syslog(LOG_ERR, "Inet_addr failed for %s", cp);
14661558Srgrimes				return (1);
14671558Srgrimes			}
14681558Srgrimes			if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
14691558Srgrimes				AF_INET)) == NULL) {
14701558Srgrimes				hp = &t_host;
14711558Srgrimes				hp->h_name = cp;
14721558Srgrimes				hp->h_addrtype = AF_INET;
14731558Srgrimes				hp->h_length = sizeof (u_long);
14741558Srgrimes				hp->h_addr_list = aptr;
14751558Srgrimes				aptr[0] = (char *)&saddr;
14761558Srgrimes				aptr[1] = (char *)NULL;
14771558Srgrimes			}
14781558Srgrimes		} else {
14799336Sdfr 			syslog(LOG_ERR, "Gethostbyname failed for %s", cp);
14801558Srgrimes			return (1);
14811558Srgrimes		}
14821558Srgrimes	}
14837401Swpaul        /*
14847401Swpaul         * Sanity check: make sure we don't already have an entry
14857401Swpaul         * for this host in the grouplist.
14867401Swpaul         */
14877401Swpaul        checkgrp = tgrp;
14887401Swpaul        while (checkgrp) {
148917887Swpaul		if (checkgrp->gr_type == GT_HOST &&
149017887Swpaul                    checkgrp->gr_ptr.gt_hostent != NULL &&
14917401Swpaul                    !strcmp(checkgrp->gr_ptr.gt_hostent->h_name, hp->h_name)) {
14927401Swpaul                        grp->gr_type = GT_IGNORE;
14937401Swpaul			return(0);
14947401Swpaul		}
14957401Swpaul                checkgrp = checkgrp->gr_next;
14967401Swpaul        }
14977401Swpaul
14981558Srgrimes	grp->gr_type = GT_HOST;
14991558Srgrimes	nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
15001558Srgrimes		malloc(sizeof(struct hostent));
15011558Srgrimes	if (nhp == (struct hostent *)NULL)
15021558Srgrimes		out_of_mem();
150323681Speter	memmove(nhp, hp, sizeof(struct hostent));
15041558Srgrimes	i = strlen(hp->h_name)+1;
15051558Srgrimes	nhp->h_name = (char *)malloc(i);
15061558Srgrimes	if (nhp->h_name == (char *)NULL)
15071558Srgrimes		out_of_mem();
150823681Speter	memmove(nhp->h_name, hp->h_name, i);
15091558Srgrimes	addrp = hp->h_addr_list;
15101558Srgrimes	i = 1;
15111558Srgrimes	while (*addrp++)
15121558Srgrimes		i++;
15131558Srgrimes	naddrp = nhp->h_addr_list = (char **)
15141558Srgrimes		malloc(i*sizeof(char *));
15151558Srgrimes	if (naddrp == (char **)NULL)
15161558Srgrimes		out_of_mem();
15171558Srgrimes	addrp = hp->h_addr_list;
15181558Srgrimes	while (*addrp) {
15191558Srgrimes		*naddrp = (char *)
15201558Srgrimes		    malloc(hp->h_length);
15211558Srgrimes		if (*naddrp == (char *)NULL)
15221558Srgrimes		    out_of_mem();
152323681Speter		memmove(*naddrp, *addrp, hp->h_length);
15241558Srgrimes		addrp++;
15251558Srgrimes		naddrp++;
15261558Srgrimes	}
15271558Srgrimes	*naddrp = (char *)NULL;
15281558Srgrimes	if (debug)
15291558Srgrimes		fprintf(stderr, "got host %s\n", hp->h_name);
15301558Srgrimes	return (0);
15311558Srgrimes}
15321558Srgrimes
15331558Srgrimes/*
15341558Srgrimes * Free up an exports list component
15351558Srgrimes */
15361558Srgrimesvoid
15371558Srgrimesfree_exp(ep)
15381558Srgrimes	struct exportlist *ep;
15391558Srgrimes{
15401558Srgrimes
15411558Srgrimes	if (ep->ex_defdir) {
15421558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
15431558Srgrimes		free((caddr_t)ep->ex_defdir);
15441558Srgrimes	}
15451558Srgrimes	if (ep->ex_fsdir)
15461558Srgrimes		free(ep->ex_fsdir);
154727447Sdfr	if (ep->ex_indexfile)
154827447Sdfr		free(ep->ex_indexfile);
15491558Srgrimes	free_dir(ep->ex_dirl);
15501558Srgrimes	free((caddr_t)ep);
15511558Srgrimes}
15521558Srgrimes
15531558Srgrimes/*
15541558Srgrimes * Free hosts.
15551558Srgrimes */
15561558Srgrimesvoid
15571558Srgrimesfree_host(hp)
15581558Srgrimes	struct hostlist *hp;
15591558Srgrimes{
15601558Srgrimes	struct hostlist *hp2;
15611558Srgrimes
15621558Srgrimes	while (hp) {
15631558Srgrimes		hp2 = hp;
15641558Srgrimes		hp = hp->ht_next;
15651558Srgrimes		free((caddr_t)hp2);
15661558Srgrimes	}
15671558Srgrimes}
15681558Srgrimes
15691558Srgrimesstruct hostlist *
15701558Srgrimesget_ht()
15711558Srgrimes{
15721558Srgrimes	struct hostlist *hp;
15731558Srgrimes
15741558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
15751558Srgrimes	if (hp == (struct hostlist *)NULL)
15761558Srgrimes		out_of_mem();
15771558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
15789336Sdfr	hp->ht_flag = 0;
15791558Srgrimes	return (hp);
15801558Srgrimes}
15811558Srgrimes
15821558Srgrimes#ifdef ISO
15831558Srgrimes/*
15841558Srgrimes * Translate an iso address.
15851558Srgrimes */
15861558Srgrimesget_isoaddr(cp, grp)
15871558Srgrimes	char *cp;
15881558Srgrimes	struct grouplist *grp;
15891558Srgrimes{
15901558Srgrimes	struct iso_addr *isop;
15911558Srgrimes	struct sockaddr_iso *isoaddr;
15921558Srgrimes
15931558Srgrimes	if (grp->gr_type != GT_NULL)
15941558Srgrimes		return (1);
15951558Srgrimes	if ((isop = iso_addr(cp)) == NULL) {
15961558Srgrimes		syslog(LOG_ERR,
15971558Srgrimes		    "iso_addr failed, ignored");
15981558Srgrimes		return (1);
15991558Srgrimes	}
16001558Srgrimes	isoaddr = (struct sockaddr_iso *)
16011558Srgrimes	    malloc(sizeof (struct sockaddr_iso));
16021558Srgrimes	if (isoaddr == (struct sockaddr_iso *)NULL)
16031558Srgrimes		out_of_mem();
160423681Speter	memset(isoaddr, 0, sizeof(struct sockaddr_iso));
160523681Speter	memmove(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
160623681Speter	isoaddr->siso_len = sizeof(struct sockaddr_iso);
16071558Srgrimes	isoaddr->siso_family = AF_ISO;
16081558Srgrimes	grp->gr_type = GT_ISO;
16091558Srgrimes	grp->gr_ptr.gt_isoaddr = isoaddr;
16101558Srgrimes	return (0);
16111558Srgrimes}
16121558Srgrimes#endif	/* ISO */
16131558Srgrimes
16141558Srgrimes/*
16151558Srgrimes * Out of memory, fatal
16161558Srgrimes */
16171558Srgrimesvoid
16181558Srgrimesout_of_mem()
16191558Srgrimes{
16201558Srgrimes
16211558Srgrimes	syslog(LOG_ERR, "Out of memory");
16221558Srgrimes	exit(2);
16231558Srgrimes}
16241558Srgrimes
16251558Srgrimes/*
16261558Srgrimes * Do the mount syscall with the update flag to push the export info into
16271558Srgrimes * the kernel.
16281558Srgrimes */
16291558Srgrimesint
16301558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
16311558Srgrimes	struct exportlist *ep;
16321558Srgrimes	struct grouplist *grp;
16331558Srgrimes	int exflags;
16341558Srgrimes	struct ucred *anoncrp;
16351558Srgrimes	char *dirp;
16361558Srgrimes	int dirplen;
16371558Srgrimes	struct statfs *fsb;
16381558Srgrimes{
16391558Srgrimes	char *cp = (char *)NULL;
16401558Srgrimes	u_long **addrp;
16411558Srgrimes	int done;
16421558Srgrimes	char savedc = '\0';
16431558Srgrimes	struct sockaddr_in sin, imask;
16441558Srgrimes	union {
16451558Srgrimes		struct ufs_args ua;
16461558Srgrimes		struct iso_args ia;
16471558Srgrimes		struct mfs_args ma;
16489336Sdfr#ifdef __NetBSD__
16499336Sdfr		struct msdosfs_args da;
16509336Sdfr#endif
16511558Srgrimes	} args;
16521558Srgrimes	u_long net;
16531558Srgrimes
16541558Srgrimes	args.ua.fspec = 0;
16551558Srgrimes	args.ua.export.ex_flags = exflags;
16561558Srgrimes	args.ua.export.ex_anon = *anoncrp;
165727447Sdfr	args.ua.export.ex_indexfile = ep->ex_indexfile;
165823681Speter	memset(&sin, 0, sizeof(sin));
165923681Speter	memset(&imask, 0, sizeof(imask));
16601558Srgrimes	sin.sin_family = AF_INET;
16611558Srgrimes	sin.sin_len = sizeof(sin);
16621558Srgrimes	imask.sin_family = AF_INET;
16631558Srgrimes	imask.sin_len = sizeof(sin);
16641558Srgrimes	if (grp->gr_type == GT_HOST)
16651558Srgrimes		addrp = (u_long **)grp->gr_ptr.gt_hostent->h_addr_list;
16661558Srgrimes	else
16671558Srgrimes		addrp = (u_long **)NULL;
16681558Srgrimes	done = FALSE;
16691558Srgrimes	while (!done) {
16701558Srgrimes		switch (grp->gr_type) {
16711558Srgrimes		case GT_HOST:
16721558Srgrimes			if (addrp) {
16731558Srgrimes				sin.sin_addr.s_addr = **addrp;
16741558Srgrimes				args.ua.export.ex_addrlen = sizeof(sin);
16751558Srgrimes			} else
16761558Srgrimes				args.ua.export.ex_addrlen = 0;
16771558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16781558Srgrimes			args.ua.export.ex_masklen = 0;
16791558Srgrimes			break;
16801558Srgrimes		case GT_NET:
16811558Srgrimes			if (grp->gr_ptr.gt_net.nt_mask)
16821558Srgrimes			    imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
16831558Srgrimes			else {
16841558Srgrimes			    net = ntohl(grp->gr_ptr.gt_net.nt_net);
16851558Srgrimes			    if (IN_CLASSA(net))
16861558Srgrimes				imask.sin_addr.s_addr = inet_addr("255.0.0.0");
16871558Srgrimes			    else if (IN_CLASSB(net))
16881558Srgrimes				imask.sin_addr.s_addr =
16891558Srgrimes				    inet_addr("255.255.0.0");
16901558Srgrimes			    else
16911558Srgrimes				imask.sin_addr.s_addr =
16921558Srgrimes				    inet_addr("255.255.255.0");
16931558Srgrimes			    grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
16941558Srgrimes			}
16951558Srgrimes			sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
16961558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16971558Srgrimes			args.ua.export.ex_addrlen = sizeof (sin);
16981558Srgrimes			args.ua.export.ex_mask = (struct sockaddr *)&imask;
16991558Srgrimes			args.ua.export.ex_masklen = sizeof (imask);
17001558Srgrimes			break;
17011558Srgrimes#ifdef ISO
17021558Srgrimes		case GT_ISO:
17031558Srgrimes			args.ua.export.ex_addr =
17041558Srgrimes				(struct sockaddr *)grp->gr_ptr.gt_isoaddr;
17051558Srgrimes			args.ua.export.ex_addrlen =
17061558Srgrimes				sizeof(struct sockaddr_iso);
17071558Srgrimes			args.ua.export.ex_masklen = 0;
17081558Srgrimes			break;
17091558Srgrimes#endif	/* ISO */
17107401Swpaul		case GT_IGNORE:
17117401Swpaul			return(0);
17127401Swpaul			break;
17131558Srgrimes		default:
17141558Srgrimes			syslog(LOG_ERR, "Bad grouptype");
17151558Srgrimes			if (cp)
17161558Srgrimes				*cp = savedc;
17171558Srgrimes			return (1);
17181558Srgrimes		};
17191558Srgrimes
17201558Srgrimes		/*
17211558Srgrimes		 * XXX:
17221558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
17231558Srgrimes		 * of looping back up the dirp to the mount point??
17241558Srgrimes		 * Also, needs to know how to export all types of local
172523681Speter		 * exportable file systems and not just "ufs".
17261558Srgrimes		 */
17279336Sdfr		while (mount(fsb->f_fstypename, dirp,
17281558Srgrimes		       fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
17291558Srgrimes			if (cp)
17301558Srgrimes				*cp-- = savedc;
17311558Srgrimes			else
17321558Srgrimes				cp = dirp + dirplen - 1;
17331558Srgrimes			if (errno == EPERM) {
17341558Srgrimes				syslog(LOG_ERR,
17351558Srgrimes				   "Can't change attributes for %s.\n", dirp);
17361558Srgrimes				return (1);
17371558Srgrimes			}
17381558Srgrimes			if (opt_flags & OP_ALLDIRS) {
17394895Swollman				syslog(LOG_ERR, "Could not remount %s: %m",
17404895Swollman					dirp);
17411558Srgrimes				return (1);
17421558Srgrimes			}
17431558Srgrimes			/* back up over the last component */
17441558Srgrimes			while (*cp == '/' && cp > dirp)
17451558Srgrimes				cp--;
17461558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
17471558Srgrimes				cp--;
17481558Srgrimes			if (cp == dirp) {
17491558Srgrimes				if (debug)
17501558Srgrimes					fprintf(stderr,"mnt unsucc\n");
17511558Srgrimes				syslog(LOG_ERR, "Can't export %s", dirp);
17521558Srgrimes				return (1);
17531558Srgrimes			}
17541558Srgrimes			savedc = *cp;
17551558Srgrimes			*cp = '\0';
17561558Srgrimes		}
17571558Srgrimes		if (addrp) {
17581558Srgrimes			++addrp;
17591558Srgrimes			if (*addrp == (u_long *)NULL)
17601558Srgrimes				done = TRUE;
17611558Srgrimes		} else
17621558Srgrimes			done = TRUE;
17631558Srgrimes	}
17641558Srgrimes	if (cp)
17651558Srgrimes		*cp = savedc;
17661558Srgrimes	return (0);
17671558Srgrimes}
17681558Srgrimes
17691558Srgrimes/*
17701558Srgrimes * Translate a net address.
17711558Srgrimes */
17721558Srgrimesint
17731558Srgrimesget_net(cp, net, maskflg)
17741558Srgrimes	char *cp;
17751558Srgrimes	struct netmsk *net;
17761558Srgrimes	int maskflg;
17771558Srgrimes{
17781558Srgrimes	struct netent *np;
17791558Srgrimes	long netaddr;
17801558Srgrimes	struct in_addr inetaddr, inetaddr2;
17811558Srgrimes	char *name;
17821558Srgrimes
178325318Spst	if (isdigit(*cp) && ((netaddr = inet_network(cp)) != -1)) {
17841558Srgrimes		inetaddr = inet_makeaddr(netaddr, 0);
17851558Srgrimes		/*
17861558Srgrimes		 * Due to arbritrary subnet masks, you don't know how many
17871558Srgrimes		 * bits to shift the address to make it into a network,
17881558Srgrimes		 * however you do know how to make a network address into
17891558Srgrimes		 * a host with host == 0 and then compare them.
17901558Srgrimes		 * (What a pest)
17911558Srgrimes		 */
17921558Srgrimes		if (!maskflg) {
17931558Srgrimes			setnetent(0);
17941558Srgrimes			while (np = getnetent()) {
17951558Srgrimes				inetaddr2 = inet_makeaddr(np->n_net, 0);
17961558Srgrimes				if (inetaddr2.s_addr == inetaddr.s_addr)
17971558Srgrimes					break;
17981558Srgrimes			}
17991558Srgrimes			endnetent();
18001558Srgrimes		}
180125318Spst	} else if ((np = getnetbyname(cp)) != NULL) {
180225318Spst		inetaddr = inet_makeaddr(np->n_net, 0);
18031558Srgrimes	} else
18041558Srgrimes		return (1);
180525318Spst
18061558Srgrimes	if (maskflg)
18071558Srgrimes		net->nt_mask = inetaddr.s_addr;
18081558Srgrimes	else {
18091558Srgrimes		if (np)
18101558Srgrimes			name = np->n_name;
18111558Srgrimes		else
18121558Srgrimes			name = inet_ntoa(inetaddr);
18131558Srgrimes		net->nt_name = (char *)malloc(strlen(name) + 1);
18141558Srgrimes		if (net->nt_name == (char *)NULL)
18151558Srgrimes			out_of_mem();
18161558Srgrimes		strcpy(net->nt_name, name);
18171558Srgrimes		net->nt_net = inetaddr.s_addr;
18181558Srgrimes	}
18191558Srgrimes	return (0);
18201558Srgrimes}
18211558Srgrimes
18221558Srgrimes/*
18231558Srgrimes * Parse out the next white space separated field
18241558Srgrimes */
18251558Srgrimesvoid
18261558Srgrimesnextfield(cp, endcp)
18271558Srgrimes	char **cp;
18281558Srgrimes	char **endcp;
18291558Srgrimes{
18301558Srgrimes	char *p;
18311558Srgrimes
18321558Srgrimes	p = *cp;
18331558Srgrimes	while (*p == ' ' || *p == '\t')
18341558Srgrimes		p++;
18351558Srgrimes	if (*p == '\n' || *p == '\0')
18361558Srgrimes		*cp = *endcp = p;
18371558Srgrimes	else {
18381558Srgrimes		*cp = p++;
18391558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
18401558Srgrimes			p++;
18411558Srgrimes		*endcp = p;
18421558Srgrimes	}
18431558Srgrimes}
18441558Srgrimes
18451558Srgrimes/*
18461558Srgrimes * Get an exports file line. Skip over blank lines and handle line
18471558Srgrimes * continuations.
18481558Srgrimes */
18491558Srgrimesint
18501558Srgrimesget_line()
18511558Srgrimes{
18521558Srgrimes	char *p, *cp;
18531558Srgrimes	int len;
18541558Srgrimes	int totlen, cont_line;
18551558Srgrimes
18561558Srgrimes	/*
18571558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
18581558Srgrimes	 */
18591558Srgrimes	p = line;
18601558Srgrimes	totlen = 0;
18611558Srgrimes	do {
18621558Srgrimes		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
18631558Srgrimes			return (0);
18641558Srgrimes		len = strlen(p);
18651558Srgrimes		cp = p + len - 1;
18661558Srgrimes		cont_line = 0;
18671558Srgrimes		while (cp >= p &&
18681558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
18691558Srgrimes			if (*cp == '\\')
18701558Srgrimes				cont_line = 1;
18711558Srgrimes			cp--;
18721558Srgrimes			len--;
18731558Srgrimes		}
18741558Srgrimes		*++cp = '\0';
18751558Srgrimes		if (len > 0) {
18761558Srgrimes			totlen += len;
18771558Srgrimes			if (totlen >= LINESIZ) {
18781558Srgrimes				syslog(LOG_ERR, "Exports line too long");
18791558Srgrimes				exit(2);
18801558Srgrimes			}
18811558Srgrimes			p = cp;
18821558Srgrimes		}
18831558Srgrimes	} while (totlen == 0 || cont_line);
18841558Srgrimes	return (1);
18851558Srgrimes}
18861558Srgrimes
18871558Srgrimes/*
18881558Srgrimes * Parse a description of a credential.
18891558Srgrimes */
18901558Srgrimesvoid
18911558Srgrimesparsecred(namelist, cr)
18921558Srgrimes	char *namelist;
18931558Srgrimes	struct ucred *cr;
18941558Srgrimes{
18951558Srgrimes	char *name;
18961558Srgrimes	int cnt;
18971558Srgrimes	char *names;
18981558Srgrimes	struct passwd *pw;
18991558Srgrimes	struct group *gr;
19001558Srgrimes	int ngroups, groups[NGROUPS + 1];
19011558Srgrimes
19021558Srgrimes	/*
19031558Srgrimes	 * Set up the unpriviledged user.
19041558Srgrimes	 */
19051558Srgrimes	cr->cr_ref = 1;
19061558Srgrimes	cr->cr_uid = -2;
19071558Srgrimes	cr->cr_groups[0] = -2;
19081558Srgrimes	cr->cr_ngroups = 1;
19091558Srgrimes	/*
19101558Srgrimes	 * Get the user's password table entry.
19111558Srgrimes	 */
19121558Srgrimes	names = strsep(&namelist, " \t\n");
19131558Srgrimes	name = strsep(&names, ":");
19141558Srgrimes	if (isdigit(*name) || *name == '-')
19151558Srgrimes		pw = getpwuid(atoi(name));
19161558Srgrimes	else
19171558Srgrimes		pw = getpwnam(name);
19181558Srgrimes	/*
19191558Srgrimes	 * Credentials specified as those of a user.
19201558Srgrimes	 */
19211558Srgrimes	if (names == NULL) {
19221558Srgrimes		if (pw == NULL) {
19231558Srgrimes			syslog(LOG_ERR, "Unknown user: %s", name);
19241558Srgrimes			return;
19251558Srgrimes		}
19261558Srgrimes		cr->cr_uid = pw->pw_uid;
19271558Srgrimes		ngroups = NGROUPS + 1;
19281558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
19291558Srgrimes			syslog(LOG_ERR, "Too many groups");
19301558Srgrimes		/*
19311558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
19321558Srgrimes		 */
19331558Srgrimes		cr->cr_ngroups = ngroups - 1;
19341558Srgrimes		cr->cr_groups[0] = groups[0];
19351558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
19361558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
19371558Srgrimes		return;
19381558Srgrimes	}
19391558Srgrimes	/*
19401558Srgrimes	 * Explicit credential specified as a colon separated list:
19411558Srgrimes	 *	uid:gid:gid:...
19421558Srgrimes	 */
19431558Srgrimes	if (pw != NULL)
19441558Srgrimes		cr->cr_uid = pw->pw_uid;
19451558Srgrimes	else if (isdigit(*name) || *name == '-')
19461558Srgrimes		cr->cr_uid = atoi(name);
19471558Srgrimes	else {
19481558Srgrimes		syslog(LOG_ERR, "Unknown user: %s", name);
19491558Srgrimes		return;
19501558Srgrimes	}
19511558Srgrimes	cr->cr_ngroups = 0;
19521558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
19531558Srgrimes		name = strsep(&names, ":");
19541558Srgrimes		if (isdigit(*name) || *name == '-') {
19551558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
19561558Srgrimes		} else {
19571558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
19581558Srgrimes				syslog(LOG_ERR, "Unknown group: %s", name);
19591558Srgrimes				continue;
19601558Srgrimes			}
19611558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
19621558Srgrimes		}
19631558Srgrimes	}
19641558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
19651558Srgrimes		syslog(LOG_ERR, "Too many groups");
19661558Srgrimes}
19671558Srgrimes
19681558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
19691558Srgrimes/*
19701558Srgrimes * Routines that maintain the remote mounttab
19711558Srgrimes */
19721558Srgrimesvoid
19731558Srgrimesget_mountlist()
19741558Srgrimes{
19751558Srgrimes	struct mountlist *mlp, **mlpp;
197623681Speter	char *host, *dirp, *cp;
19771558Srgrimes	int len;
19781558Srgrimes	char str[STRSIZ];
19791558Srgrimes	FILE *mlfile;
19801558Srgrimes
19811558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
19821558Srgrimes		syslog(LOG_ERR, "Can't open %s", _PATH_RMOUNTLIST);
19831558Srgrimes		return;
19841558Srgrimes	}
19851558Srgrimes	mlpp = &mlhead;
19861558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
198723681Speter		cp = str;
198823681Speter		host = strsep(&cp, " \t\n");
198923681Speter		dirp = strsep(&cp, " \t\n");
199023681Speter		if (host == NULL || dirp == NULL)
19911558Srgrimes			continue;
19921558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
199323681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
199423681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
199523681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
199623681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
19971558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
19981558Srgrimes		*mlpp = mlp;
19991558Srgrimes		mlpp = &mlp->ml_next;
20001558Srgrimes	}
20011558Srgrimes	fclose(mlfile);
20021558Srgrimes}
20031558Srgrimes
20041558Srgrimesvoid
20051558Srgrimesdel_mlist(hostp, dirp)
20061558Srgrimes	char *hostp, *dirp;
20071558Srgrimes{
20081558Srgrimes	struct mountlist *mlp, **mlpp;
20091558Srgrimes	struct mountlist *mlp2;
20101558Srgrimes	FILE *mlfile;
20111558Srgrimes	int fnd = 0;
20121558Srgrimes
20131558Srgrimes	mlpp = &mlhead;
20141558Srgrimes	mlp = mlhead;
20151558Srgrimes	while (mlp) {
20161558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
20171558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
20181558Srgrimes			fnd = 1;
20191558Srgrimes			mlp2 = mlp;
20201558Srgrimes			*mlpp = mlp = mlp->ml_next;
20211558Srgrimes			free((caddr_t)mlp2);
20221558Srgrimes		} else {
20231558Srgrimes			mlpp = &mlp->ml_next;
20241558Srgrimes			mlp = mlp->ml_next;
20251558Srgrimes		}
20261558Srgrimes	}
20271558Srgrimes	if (fnd) {
20281558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
20291558Srgrimes			syslog(LOG_ERR,"Can't update %s", _PATH_RMOUNTLIST);
20301558Srgrimes			return;
20311558Srgrimes		}
20321558Srgrimes		mlp = mlhead;
20331558Srgrimes		while (mlp) {
20341558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20351558Srgrimes			mlp = mlp->ml_next;
20361558Srgrimes		}
20371558Srgrimes		fclose(mlfile);
20381558Srgrimes	}
20391558Srgrimes}
20401558Srgrimes
20411558Srgrimesvoid
20421558Srgrimesadd_mlist(hostp, dirp)
20431558Srgrimes	char *hostp, *dirp;
20441558Srgrimes{
20451558Srgrimes	struct mountlist *mlp, **mlpp;
20461558Srgrimes	FILE *mlfile;
20471558Srgrimes
20481558Srgrimes	mlpp = &mlhead;
20491558Srgrimes	mlp = mlhead;
20501558Srgrimes	while (mlp) {
20511558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
20521558Srgrimes			return;
20531558Srgrimes		mlpp = &mlp->ml_next;
20541558Srgrimes		mlp = mlp->ml_next;
20551558Srgrimes	}
20561558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
20571558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
20581558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
20591558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
20601558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
20611558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
20621558Srgrimes	*mlpp = mlp;
20631558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
20641558Srgrimes		syslog(LOG_ERR, "Can't update %s", _PATH_RMOUNTLIST);
20651558Srgrimes		return;
20661558Srgrimes	}
20671558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20681558Srgrimes	fclose(mlfile);
20691558Srgrimes}
20701558Srgrimes
20711558Srgrimes/*
20721558Srgrimes * This function is called via. SIGTERM when the system is going down.
20731558Srgrimes * It sends a broadcast RPCMNT_UMNTALL.
20741558Srgrimes */
20751558Srgrimesvoid
20761558Srgrimessend_umntall()
20771558Srgrimes{
20781558Srgrimes	(void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
20791558Srgrimes		xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
20801558Srgrimes	exit(0);
20811558Srgrimes}
20821558Srgrimes
20831558Srgrimesint
20841558Srgrimesumntall_each(resultsp, raddr)
20851558Srgrimes	caddr_t resultsp;
20861558Srgrimes	struct sockaddr_in *raddr;
20871558Srgrimes{
20881558Srgrimes	return (1);
20891558Srgrimes}
20901558Srgrimes
20911558Srgrimes/*
20921558Srgrimes * Free up a group list.
20931558Srgrimes */
20941558Srgrimesvoid
20951558Srgrimesfree_grp(grp)
20961558Srgrimes	struct grouplist *grp;
20971558Srgrimes{
20981558Srgrimes	char **addrp;
20991558Srgrimes
21001558Srgrimes	if (grp->gr_type == GT_HOST) {
21011558Srgrimes		if (grp->gr_ptr.gt_hostent->h_name) {
21021558Srgrimes			addrp = grp->gr_ptr.gt_hostent->h_addr_list;
21031558Srgrimes			while (addrp && *addrp)
21041558Srgrimes				free(*addrp++);
21051558Srgrimes			free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
21061558Srgrimes			free(grp->gr_ptr.gt_hostent->h_name);
21071558Srgrimes		}
21081558Srgrimes		free((caddr_t)grp->gr_ptr.gt_hostent);
21091558Srgrimes	} else if (grp->gr_type == GT_NET) {
21101558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
21111558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
21121558Srgrimes	}
21131558Srgrimes#ifdef ISO
21141558Srgrimes	else if (grp->gr_type == GT_ISO)
21151558Srgrimes		free((caddr_t)grp->gr_ptr.gt_isoaddr);
21161558Srgrimes#endif
21171558Srgrimes	free((caddr_t)grp);
21181558Srgrimes}
21191558Srgrimes
21201558Srgrimes#ifdef DEBUG
21211558Srgrimesvoid
21221558SrgrimesSYSLOG(int pri, const char *fmt, ...)
21231558Srgrimes{
21241558Srgrimes	va_list ap;
21251558Srgrimes
21261558Srgrimes	va_start(ap, fmt);
21271558Srgrimes	vfprintf(stderr, fmt, ap);
21281558Srgrimes	va_end(ap);
21291558Srgrimes}
21301558Srgrimes#endif /* DEBUG */
21311558Srgrimes
21321558Srgrimes/*
21331558Srgrimes * Check options for consistency.
21341558Srgrimes */
21351558Srgrimesint
21361558Srgrimescheck_options(dp)
21371558Srgrimes	struct dirlist *dp;
21381558Srgrimes{
21391558Srgrimes
21401558Srgrimes	if (dp == (struct dirlist *)NULL)
21411558Srgrimes	    return (1);
21421558Srgrimes	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
21431558Srgrimes	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
21441558Srgrimes	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
21451558Srgrimes	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
21461558Srgrimes	    return (1);
21471558Srgrimes	}
21481558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
21491558Srgrimes	    syslog(LOG_ERR, "-mask requires -net");
21501558Srgrimes	    return (1);
21511558Srgrimes	}
21521558Srgrimes	if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
21531558Srgrimes	    syslog(LOG_ERR, "-net and -iso mutually exclusive");
21541558Srgrimes	    return (1);
21551558Srgrimes	}
21561558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
21571558Srgrimes	    syslog(LOG_ERR, "-alldir has multiple directories");
21581558Srgrimes	    return (1);
21591558Srgrimes	}
21601558Srgrimes	return (0);
21611558Srgrimes}
21621558Srgrimes
21631558Srgrimes/*
21641558Srgrimes * Check an absolute directory path for any symbolic links. Return true
21651558Srgrimes * if no symbolic links are found.
21661558Srgrimes */
21671558Srgrimesint
21681558Srgrimescheck_dirpath(dirp)
21691558Srgrimes	char *dirp;
21701558Srgrimes{
21711558Srgrimes	char *cp;
21721558Srgrimes	int ret = 1;
21731558Srgrimes	struct stat sb;
21741558Srgrimes
21751558Srgrimes	cp = dirp + 1;
21761558Srgrimes	while (*cp && ret) {
21771558Srgrimes		if (*cp == '/') {
21781558Srgrimes			*cp = '\0';
21799336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21801558Srgrimes				ret = 0;
21811558Srgrimes			*cp = '/';
21821558Srgrimes		}
21831558Srgrimes		cp++;
21841558Srgrimes	}
21859336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21861558Srgrimes		ret = 0;
21871558Srgrimes	return (ret);
21881558Srgrimes}
21899336Sdfr
21909336Sdfr/*
21919336Sdfr * Just translate an ascii string to an integer.
21929336Sdfr */
21939336Sdfrint
21949336Sdfrget_num(cp)
21959336Sdfr	register char *cp;
21969336Sdfr{
21979336Sdfr	register int res = 0;
21989336Sdfr
21999336Sdfr	while (*cp) {
22009336Sdfr		if (*cp < '0' || *cp > '9')
22019336Sdfr			return (-1);
22029336Sdfr		res = res * 10 + (*cp++ - '0');
22039336Sdfr	}
22049336Sdfr	return (res);
22059336Sdfr}
2206