mountd.c revision 37004
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[] =
4637004Sjoerg	"$Id: mountd.c,v 1.29 1998/06/15 15:41:41 joerg 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;
22731705Sguidoint log = 0;
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;
25932656Sbde	int c, error, mib[3];
26023681Speter	struct vfsconf vfc;
2611558Srgrimes
26223681Speter	error = getvfsbyname("nfs", &vfc);
26323681Speter	if (error && vfsisloadable("nfs")) {
2642999Swollman		if(vfsload("nfs"))
2652999Swollman			err(1, "vfsload(nfs)");
2662999Swollman		endvfsent();	/* flush cache */
26723681Speter		error = getvfsbyname("nfs", &vfc);
2682999Swollman	}
26923681Speter	if (error)
2702999Swollman		errx(1, "NFS support is not available in the running kernel");
2712999Swollman
27231665Sguido	while ((c = getopt(argc, argv, "2dlnr")) != -1)
2731558Srgrimes		switch (c) {
27425087Sdfr		case '2':
27525087Sdfr			force_v2 = 1;
27625087Sdfr			break;
2779336Sdfr		case 'n':
2789336Sdfr			resvport_only = 0;
2799336Sdfr			break;
2809336Sdfr		case 'r':
2819336Sdfr			dir_only = 0;
2829336Sdfr			break;
2838688Sphk		case 'd':
2848688Sphk			debug = debug ? 0 : 1;
2858688Sphk			break;
28631656Sguido		case 'l':
28731656Sguido			log = 1;
28831656Sguido			break;
2891558Srgrimes		default:
29031665Sguido			fprintf(stderr,
29131665Sguido"Usage: mountd [-d] [-l] [-r] [-n] [export_file]\n");
2921558Srgrimes			exit(1);
2931558Srgrimes		};
2941558Srgrimes	argc -= optind;
2951558Srgrimes	argv += optind;
2961558Srgrimes	grphead = (struct grouplist *)NULL;
2971558Srgrimes	exphead = (struct exportlist *)NULL;
2981558Srgrimes	mlhead = (struct mountlist *)NULL;
2991558Srgrimes	if (argc == 1) {
3001558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
3011558Srgrimes		exname[MAXPATHLEN-1] = '\0';
3021558Srgrimes	} else
3031558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3041558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3051558Srgrimes	if (debug)
3061558Srgrimes		fprintf(stderr,"Getting export list.\n");
3071558Srgrimes	get_exportlist();
3081558Srgrimes	if (debug)
3091558Srgrimes		fprintf(stderr,"Getting mount list.\n");
3101558Srgrimes	get_mountlist();
3111558Srgrimes	if (debug)
3121558Srgrimes		fprintf(stderr,"Here we go.\n");
3131558Srgrimes	if (debug == 0) {
3141558Srgrimes		daemon(0, 0);
3151558Srgrimes		signal(SIGINT, SIG_IGN);
3161558Srgrimes		signal(SIGQUIT, SIG_IGN);
3171558Srgrimes	}
3181558Srgrimes	signal(SIGHUP, (void (*) __P((int))) get_exportlist);
3191558Srgrimes	signal(SIGTERM, (void (*) __P((int))) send_umntall);
3201558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3211558Srgrimes	  if (pidfile != NULL) {
3221558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3231558Srgrimes		fclose(pidfile);
3241558Srgrimes	  }
3251558Srgrimes	}
32624759Sguido	if (!resvport_only) {
32724759Sguido		mib[0] = CTL_VFS;
32832656Sbde		mib[1] = vfc.vfc_typenum;
32924759Sguido		mib[2] = NFS_NFSPRIVPORT;
33024759Sguido		if (sysctl(mib, 3, NULL, NULL, &resvport_only,
33124759Sguido		    sizeof(resvport_only)) != 0 && errno != ENOENT) {
33224759Sguido			syslog(LOG_ERR, "sysctl: %m");
33324759Sguido			exit(1);
33424759Sguido		}
33524330Sguido	}
3369202Srgrimes	if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
3379202Srgrimes	    (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
3381558Srgrimes		syslog(LOG_ERR, "Can't create socket");
3391558Srgrimes		exit(1);
3401558Srgrimes	}
3419336Sdfr	pmap_unset(RPCPROG_MNT, 1);
3429336Sdfr	pmap_unset(RPCPROG_MNT, 3);
34325087Sdfr	if (!force_v2)
34425087Sdfr		if (!svc_register(udptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_UDP) ||
34525087Sdfr		    !svc_register(tcptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_TCP)) {
34625087Sdfr			syslog(LOG_ERR, "Can't register mount");
34725087Sdfr			exit(1);
34825087Sdfr		}
3499336Sdfr	if (!svc_register(udptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_UDP) ||
35025087Sdfr	    !svc_register(tcptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_TCP)) {
3511558Srgrimes		syslog(LOG_ERR, "Can't register mount");
3521558Srgrimes		exit(1);
3531558Srgrimes	}
3541558Srgrimes	svc_run();
3551558Srgrimes	syslog(LOG_ERR, "Mountd died");
3561558Srgrimes	exit(1);
3571558Srgrimes}
3581558Srgrimes
3591558Srgrimes/*
3601558Srgrimes * The mount rpc service
3611558Srgrimes */
3621558Srgrimesvoid
3631558Srgrimesmntsrv(rqstp, transp)
3641558Srgrimes	struct svc_req *rqstp;
3651558Srgrimes	SVCXPRT *transp;
3661558Srgrimes{
3671558Srgrimes	struct exportlist *ep;
3681558Srgrimes	struct dirlist *dp;
3699336Sdfr	struct fhreturn fhr;
3701558Srgrimes	struct stat stb;
3711558Srgrimes	struct statfs fsb;
3721558Srgrimes	struct hostent *hp;
37331656Sguido	struct in_addr saddrin;
3741558Srgrimes	u_long saddr;
3759336Sdfr	u_short sport;
37623681Speter	char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
37728911Sguido	int bad = 0, defset, hostset;
3789336Sdfr	sigset_t sighup_mask;
3791558Srgrimes
3809336Sdfr	sigemptyset(&sighup_mask);
3819336Sdfr	sigaddset(&sighup_mask, SIGHUP);
3821558Srgrimes	saddr = transp->xp_raddr.sin_addr.s_addr;
38331656Sguido	saddrin = transp->xp_raddr.sin_addr;
3849336Sdfr	sport = ntohs(transp->xp_raddr.sin_port);
3851558Srgrimes	hp = (struct hostent *)NULL;
3861558Srgrimes	switch (rqstp->rq_proc) {
3871558Srgrimes	case NULLPROC:
3881558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
3891558Srgrimes			syslog(LOG_ERR, "Can't send reply");
3901558Srgrimes		return;
3911558Srgrimes	case RPCMNT_MOUNT:
3929336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
39331656Sguido			syslog(LOG_NOTICE,
39431656Sguido			    "mount request from %s from unprivileged port",
39531656Sguido			    inet_ntoa(saddrin));
3961558Srgrimes			svcerr_weakauth(transp);
3971558Srgrimes			return;
3981558Srgrimes		}
3991558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
40031656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
40131656Sguido			    inet_ntoa(saddrin));
4021558Srgrimes			svcerr_decode(transp);
4031558Srgrimes			return;
4041558Srgrimes		}
4051558Srgrimes
4061558Srgrimes		/*
4071558Srgrimes		 * Get the real pathname and make sure it is a directory
4089336Sdfr		 * or a regular file if the -r option was specified
4099336Sdfr		 * and it exists.
4101558Srgrimes		 */
4111558Srgrimes		if (realpath(rpcpath, dirpath) == 0 ||
4121558Srgrimes		    stat(dirpath, &stb) < 0 ||
4139336Sdfr		    (!S_ISDIR(stb.st_mode) &&
4149336Sdfr		     (dir_only || !S_ISREG(stb.st_mode))) ||
4151558Srgrimes		    statfs(dirpath, &fsb) < 0) {
4161558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
41731656Sguido			syslog(LOG_NOTICE,
41831656Sguido			    "mount request from %s for non existant path %s",
41931656Sguido			    inet_ntoa(saddrin), dirpath);
4201558Srgrimes			if (debug)
4211558Srgrimes				fprintf(stderr, "stat failed on %s\n", dirpath);
42228911Sguido			bad = ENOENT;	/* We will send error reply later */
4231558Srgrimes		}
4241558Srgrimes
4251558Srgrimes		/* Check in the exports list */
4269336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
4271558Srgrimes		ep = ex_search(&fsb.f_fsid);
4289336Sdfr		hostset = defset = 0;
4299336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
4301558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
4319336Sdfr		     chk_host(dp, saddr, &defset, &hostset)) ||
4321558Srgrimes		     (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
4331558Srgrimes		      scan_tree(ep->ex_dirl, saddr) == 0))) {
43428911Sguido			if (bad) {
43528911Sguido				if (!svc_sendreply(transp, xdr_long,
43628911Sguido				    (caddr_t)&bad))
43728911Sguido					syslog(LOG_ERR, "Can't send reply");
43828911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
43928911Sguido				return;
44028911Sguido			}
4419336Sdfr			if (hostset & DP_HOSTSET)
4429336Sdfr				fhr.fhr_flag = hostset;
4439336Sdfr			else
4449336Sdfr				fhr.fhr_flag = defset;
4459336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
4461558Srgrimes			/* Get the file handle */
44723681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
4489336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
4491558Srgrimes				bad = errno;
4501558Srgrimes				syslog(LOG_ERR, "Can't get fh for %s", dirpath);
4511558Srgrimes				if (!svc_sendreply(transp, xdr_long,
4521558Srgrimes				    (caddr_t)&bad))
4531558Srgrimes					syslog(LOG_ERR, "Can't send reply");
4549336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
4551558Srgrimes				return;
4561558Srgrimes			}
4579336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
4581558Srgrimes				syslog(LOG_ERR, "Can't send reply");
4591558Srgrimes			if (hp == NULL)
4601558Srgrimes				hp = gethostbyaddr((caddr_t)&saddr,
4611558Srgrimes				    sizeof(saddr), AF_INET);
4621558Srgrimes			if (hp)
4631558Srgrimes				add_mlist(hp->h_name, dirpath);
4641558Srgrimes			else
46531656Sguido				add_mlist(inet_ntoa(saddrin),
4661558Srgrimes					dirpath);
4671558Srgrimes			if (debug)
4681558Srgrimes				fprintf(stderr,"Mount successfull.\n");
46931656Sguido			if (log)
47031656Sguido				syslog(LOG_NOTICE,
47131656Sguido				    "mount request succeeded from %s for %s",
47231656Sguido				    inet_ntoa(saddrin), dirpath);
47331656Sguido		} else {
4741558Srgrimes			bad = EACCES;
47531656Sguido			syslog(LOG_NOTICE,
47631656Sguido			    "mount request denied from %s for %s",
47731656Sguido			    inet_ntoa(saddrin), dirpath);
47831656Sguido		}
47928911Sguido
48028911Sguido		if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
48128911Sguido			syslog(LOG_ERR, "Can't send reply");
4829336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
4831558Srgrimes		return;
4841558Srgrimes	case RPCMNT_DUMP:
4851558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
4861558Srgrimes			syslog(LOG_ERR, "Can't send reply");
48731656Sguido		else if (log)
48831656Sguido			syslog(LOG_NOTICE,
48931656Sguido			    "dump request succeeded from %s",
49031656Sguido			    inet_ntoa(saddrin), dirpath);
4911558Srgrimes		return;
4921558Srgrimes	case RPCMNT_UMOUNT:
4939336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
49431656Sguido			syslog(LOG_NOTICE,
49531656Sguido			    "umount request from %s from unprivileged port",
49631656Sguido			    inet_ntoa(saddrin));
4971558Srgrimes			svcerr_weakauth(transp);
4981558Srgrimes			return;
4991558Srgrimes		}
5001558Srgrimes		if (!svc_getargs(transp, xdr_dir, dirpath)) {
50131656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
50231656Sguido			    inet_ntoa(saddrin));
5031558Srgrimes			svcerr_decode(transp);
5041558Srgrimes			return;
5051558Srgrimes		}
5061558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
5071558Srgrimes			syslog(LOG_ERR, "Can't send reply");
5081558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
5091558Srgrimes		if (hp)
5101558Srgrimes			del_mlist(hp->h_name, dirpath);
51131656Sguido		del_mlist(inet_ntoa(saddrin), dirpath);
51231656Sguido		if (log)
51331656Sguido			syslog(LOG_NOTICE,
51431656Sguido			    "umount request succeeded from %s for %s",
51531656Sguido			    inet_ntoa(saddrin), dirpath);
5161558Srgrimes		return;
5171558Srgrimes	case RPCMNT_UMNTALL:
5189336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
51931656Sguido			syslog(LOG_NOTICE,
52031656Sguido			    "umountall request from %s from unprivileged port",
52131656Sguido			    inet_ntoa(saddrin));
5221558Srgrimes			svcerr_weakauth(transp);
5231558Srgrimes			return;
5241558Srgrimes		}
5251558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
5261558Srgrimes			syslog(LOG_ERR, "Can't send reply");
5271558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
5281558Srgrimes		if (hp)
5291558Srgrimes			del_mlist(hp->h_name, (char *)NULL);
53031656Sguido		del_mlist(inet_ntoa(saddrin), (char *)NULL);
53131656Sguido		if (log)
53231656Sguido			syslog(LOG_NOTICE,
53331656Sguido			    "umountall request succeeded from %s",
53431656Sguido			    inet_ntoa(saddrin));
5351558Srgrimes		return;
5361558Srgrimes	case RPCMNT_EXPORT:
5371558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
5381558Srgrimes			syslog(LOG_ERR, "Can't send reply");
53931656Sguido		if (log)
54031656Sguido			syslog(LOG_NOTICE,
54131656Sguido			    "export request succeeded from %s",
54231656Sguido			    inet_ntoa(saddrin));
5431558Srgrimes		return;
5441558Srgrimes	default:
5451558Srgrimes		svcerr_noproc(transp);
5461558Srgrimes		return;
5471558Srgrimes	}
5481558Srgrimes}
5491558Srgrimes
5501558Srgrimes/*
5511558Srgrimes * Xdr conversion for a dirpath string
5521558Srgrimes */
5531558Srgrimesint
5541558Srgrimesxdr_dir(xdrsp, dirp)
5551558Srgrimes	XDR *xdrsp;
5561558Srgrimes	char *dirp;
5571558Srgrimes{
5581558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
5591558Srgrimes}
5601558Srgrimes
5611558Srgrimes/*
5629336Sdfr * Xdr routine to generate file handle reply
5631558Srgrimes */
5641558Srgrimesint
5659336Sdfrxdr_fhs(xdrsp, cp)
5661558Srgrimes	XDR *xdrsp;
5679336Sdfr	caddr_t cp;
5681558Srgrimes{
5699336Sdfr	register struct fhreturn *fhrp = (struct fhreturn *)cp;
5709336Sdfr	u_long ok = 0, len, auth;
5711558Srgrimes
5721558Srgrimes	if (!xdr_long(xdrsp, &ok))
5731558Srgrimes		return (0);
5749336Sdfr	switch (fhrp->fhr_vers) {
5759336Sdfr	case 1:
5769336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
5779336Sdfr	case 3:
5789336Sdfr		len = NFSX_V3FH;
5799336Sdfr		if (!xdr_long(xdrsp, &len))
5809336Sdfr			return (0);
5819336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
5829336Sdfr			return (0);
5839336Sdfr		if (fhrp->fhr_flag & DP_KERB)
5849336Sdfr			auth = RPCAUTH_KERB4;
5859336Sdfr		else
5869336Sdfr			auth = RPCAUTH_UNIX;
5879336Sdfr		len = 1;
5889336Sdfr		if (!xdr_long(xdrsp, &len))
5899336Sdfr			return (0);
5909336Sdfr		return (xdr_long(xdrsp, &auth));
5919336Sdfr	};
5929336Sdfr	return (0);
5931558Srgrimes}
5941558Srgrimes
5951558Srgrimesint
5961558Srgrimesxdr_mlist(xdrsp, cp)
5971558Srgrimes	XDR *xdrsp;
5981558Srgrimes	caddr_t cp;
5991558Srgrimes{
6001558Srgrimes	struct mountlist *mlp;
6011558Srgrimes	int true = 1;
6021558Srgrimes	int false = 0;
6031558Srgrimes	char *strp;
6041558Srgrimes
6051558Srgrimes	mlp = mlhead;
6061558Srgrimes	while (mlp) {
6071558Srgrimes		if (!xdr_bool(xdrsp, &true))
6081558Srgrimes			return (0);
6091558Srgrimes		strp = &mlp->ml_host[0];
6101558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
6111558Srgrimes			return (0);
6121558Srgrimes		strp = &mlp->ml_dirp[0];
6131558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6141558Srgrimes			return (0);
6151558Srgrimes		mlp = mlp->ml_next;
6161558Srgrimes	}
6171558Srgrimes	if (!xdr_bool(xdrsp, &false))
6181558Srgrimes		return (0);
6191558Srgrimes	return (1);
6201558Srgrimes}
6211558Srgrimes
6221558Srgrimes/*
6231558Srgrimes * Xdr conversion for export list
6241558Srgrimes */
6251558Srgrimesint
6261558Srgrimesxdr_explist(xdrsp, cp)
6271558Srgrimes	XDR *xdrsp;
6281558Srgrimes	caddr_t cp;
6291558Srgrimes{
6301558Srgrimes	struct exportlist *ep;
6311558Srgrimes	int false = 0;
6329336Sdfr	int putdef;
6339336Sdfr	sigset_t sighup_mask;
6341558Srgrimes
6359336Sdfr	sigemptyset(&sighup_mask);
6369336Sdfr	sigaddset(&sighup_mask, SIGHUP);
6379336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
6381558Srgrimes	ep = exphead;
6391558Srgrimes	while (ep) {
6401558Srgrimes		putdef = 0;
6411558Srgrimes		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
6421558Srgrimes			goto errout;
6431558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
6441558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
6451558Srgrimes			&putdef))
6461558Srgrimes			goto errout;
6471558Srgrimes		ep = ep->ex_next;
6481558Srgrimes	}
6499336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6501558Srgrimes	if (!xdr_bool(xdrsp, &false))
6511558Srgrimes		return (0);
6521558Srgrimes	return (1);
6531558Srgrimeserrout:
6549336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6551558Srgrimes	return (0);
6561558Srgrimes}
6571558Srgrimes
6581558Srgrimes/*
6591558Srgrimes * Called from xdr_explist() to traverse the tree and export the
6601558Srgrimes * directory paths.
6611558Srgrimes */
6621558Srgrimesint
6631558Srgrimesput_exlist(dp, xdrsp, adp, putdefp)
6641558Srgrimes	struct dirlist *dp;
6651558Srgrimes	XDR *xdrsp;
6661558Srgrimes	struct dirlist *adp;
6671558Srgrimes	int *putdefp;
6681558Srgrimes{
6691558Srgrimes	struct grouplist *grp;
6701558Srgrimes	struct hostlist *hp;
6711558Srgrimes	int true = 1;
6721558Srgrimes	int false = 0;
6731558Srgrimes	int gotalldir = 0;
6741558Srgrimes	char *strp;
6751558Srgrimes
6761558Srgrimes	if (dp) {
6771558Srgrimes		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
6781558Srgrimes			return (1);
6791558Srgrimes		if (!xdr_bool(xdrsp, &true))
6801558Srgrimes			return (1);
6811558Srgrimes		strp = dp->dp_dirp;
6821558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6831558Srgrimes			return (1);
6841558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
6851558Srgrimes			gotalldir = 1;
6861558Srgrimes			*putdefp = 1;
6871558Srgrimes		}
6881558Srgrimes		if ((dp->dp_flag & DP_DEFSET) == 0 &&
6891558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
6901558Srgrimes			hp = dp->dp_hosts;
6911558Srgrimes			while (hp) {
6921558Srgrimes				grp = hp->ht_grp;
6931558Srgrimes				if (grp->gr_type == GT_HOST) {
6941558Srgrimes					if (!xdr_bool(xdrsp, &true))
6951558Srgrimes						return (1);
6961558Srgrimes					strp = grp->gr_ptr.gt_hostent->h_name;
6978871Srgrimes					if (!xdr_string(xdrsp, &strp,
6981558Srgrimes					    RPCMNT_NAMELEN))
6991558Srgrimes						return (1);
7001558Srgrimes				} else if (grp->gr_type == GT_NET) {
7011558Srgrimes					if (!xdr_bool(xdrsp, &true))
7021558Srgrimes						return (1);
7031558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
7048871Srgrimes					if (!xdr_string(xdrsp, &strp,
7051558Srgrimes					    RPCMNT_NAMELEN))
7061558Srgrimes						return (1);
7071558Srgrimes				}
7081558Srgrimes				hp = hp->ht_next;
7091558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
7101558Srgrimes					hp = adp->dp_hosts;
7111558Srgrimes					gotalldir = 0;
7121558Srgrimes				}
7131558Srgrimes			}
7141558Srgrimes		}
7151558Srgrimes		if (!xdr_bool(xdrsp, &false))
7161558Srgrimes			return (1);
7171558Srgrimes		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
7181558Srgrimes			return (1);
7191558Srgrimes	}
7201558Srgrimes	return (0);
7211558Srgrimes}
7221558Srgrimes
7231558Srgrimes#define LINESIZ	10240
7241558Srgrimeschar line[LINESIZ];
7251558SrgrimesFILE *exp_file;
7261558Srgrimes
7271558Srgrimes/*
7281558Srgrimes * Get the export list
7291558Srgrimes */
7301558Srgrimesvoid
7311558Srgrimesget_exportlist()
7321558Srgrimes{
7331558Srgrimes	struct exportlist *ep, *ep2;
7341558Srgrimes	struct grouplist *grp, *tgrp;
7351558Srgrimes	struct exportlist **epp;
7361558Srgrimes	struct dirlist *dirhead;
7371558Srgrimes	struct statfs fsb, *fsp;
7381558Srgrimes	struct hostent *hpe;
7391558Srgrimes	struct ucred anon;
7401558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
7411558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
7421558Srgrimes
7431558Srgrimes	/*
7441558Srgrimes	 * First, get rid of the old list
7451558Srgrimes	 */
7461558Srgrimes	ep = exphead;
7471558Srgrimes	while (ep) {
7481558Srgrimes		ep2 = ep;
7491558Srgrimes		ep = ep->ex_next;
7501558Srgrimes		free_exp(ep2);
7511558Srgrimes	}
7521558Srgrimes	exphead = (struct exportlist *)NULL;
7531558Srgrimes
7541558Srgrimes	grp = grphead;
7551558Srgrimes	while (grp) {
7561558Srgrimes		tgrp = grp;
7571558Srgrimes		grp = grp->gr_next;
7581558Srgrimes		free_grp(tgrp);
7591558Srgrimes	}
7601558Srgrimes	grphead = (struct grouplist *)NULL;
7611558Srgrimes
7621558Srgrimes	/*
7631558Srgrimes	 * And delete exports that are in the kernel for all local
7641558Srgrimes	 * file systems.
7651558Srgrimes	 * XXX: Should know how to handle all local exportable file systems
76623681Speter	 *      instead of just "ufs".
7671558Srgrimes	 */
7681558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
7691558Srgrimes	for (i = 0; i < num; i++) {
7701558Srgrimes		union {
7711558Srgrimes			struct ufs_args ua;
7721558Srgrimes			struct iso_args ia;
7731558Srgrimes			struct mfs_args ma;
7749336Sdfr			struct msdosfs_args da;
7751558Srgrimes		} targs;
7761558Srgrimes
77723681Speter		if (!strcmp(fsp->f_fstypename, "mfs") ||
77823681Speter		    !strcmp(fsp->f_fstypename, "ufs") ||
77923681Speter		    !strcmp(fsp->f_fstypename, "msdos") ||
78023681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
7819336Sdfr			targs.ua.fspec = NULL;
7829336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
7839336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
7841558Srgrimes				  fsp->f_flags | MNT_UPDATE,
7851558Srgrimes				  (caddr_t)&targs) < 0)
7861558Srgrimes				syslog(LOG_ERR, "Can't delete exports for %s",
7871558Srgrimes				       fsp->f_mntonname);
7881558Srgrimes		}
7891558Srgrimes		fsp++;
7901558Srgrimes	}
7911558Srgrimes
7921558Srgrimes	/*
7931558Srgrimes	 * Read in the exports file and build the list, calling
7941558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
7951558Srgrimes	 */
7961558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
7971558Srgrimes		syslog(LOG_ERR, "Can't open %s", exname);
7981558Srgrimes		exit(2);
7991558Srgrimes	}
8001558Srgrimes	dirhead = (struct dirlist *)NULL;
8011558Srgrimes	while (get_line()) {
8021558Srgrimes		if (debug)
8031558Srgrimes			fprintf(stderr,"Got line %s\n",line);
8041558Srgrimes		cp = line;
8051558Srgrimes		nextfield(&cp, &endcp);
8061558Srgrimes		if (*cp == '#')
8071558Srgrimes			goto nextline;
8081558Srgrimes
8091558Srgrimes		/*
8101558Srgrimes		 * Set defaults.
8111558Srgrimes		 */
8121558Srgrimes		has_host = FALSE;
8131558Srgrimes		anon = def_anon;
8141558Srgrimes		exflags = MNT_EXPORTED;
8151558Srgrimes		got_nondir = 0;
8161558Srgrimes		opt_flags = 0;
8171558Srgrimes		ep = (struct exportlist *)NULL;
8181558Srgrimes
8191558Srgrimes		/*
8201558Srgrimes		 * Create new exports list entry
8211558Srgrimes		 */
8221558Srgrimes		len = endcp-cp;
8231558Srgrimes		tgrp = grp = get_grp();
8241558Srgrimes		while (len > 0) {
8251558Srgrimes			if (len > RPCMNT_NAMELEN) {
8261558Srgrimes			    getexp_err(ep, tgrp);
8271558Srgrimes			    goto nextline;
8281558Srgrimes			}
8291558Srgrimes			if (*cp == '-') {
8301558Srgrimes			    if (ep == (struct exportlist *)NULL) {
8311558Srgrimes				getexp_err(ep, tgrp);
8321558Srgrimes				goto nextline;
8331558Srgrimes			    }
8341558Srgrimes			    if (debug)
8351558Srgrimes				fprintf(stderr, "doing opt %s\n", cp);
8361558Srgrimes			    got_nondir = 1;
8371558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
8381558Srgrimes				&exflags, &anon)) {
8391558Srgrimes				getexp_err(ep, tgrp);
8401558Srgrimes				goto nextline;
8411558Srgrimes			    }
8421558Srgrimes			} else if (*cp == '/') {
8431558Srgrimes			    savedc = *endcp;
8441558Srgrimes			    *endcp = '\0';
8451558Srgrimes			    if (check_dirpath(cp) &&
8461558Srgrimes				statfs(cp, &fsb) >= 0) {
8471558Srgrimes				if (got_nondir) {
8481558Srgrimes				    syslog(LOG_ERR, "Dirs must be first");
8491558Srgrimes				    getexp_err(ep, tgrp);
8501558Srgrimes				    goto nextline;
8511558Srgrimes				}
8521558Srgrimes				if (ep) {
8531558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
8541558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
8551558Srgrimes					getexp_err(ep, tgrp);
8561558Srgrimes					goto nextline;
8571558Srgrimes				    }
8581558Srgrimes				} else {
8591558Srgrimes				    /*
8601558Srgrimes				     * See if this directory is already
8611558Srgrimes				     * in the list.
8621558Srgrimes				     */
8631558Srgrimes				    ep = ex_search(&fsb.f_fsid);
8641558Srgrimes				    if (ep == (struct exportlist *)NULL) {
8651558Srgrimes					ep = get_exp();
8661558Srgrimes					ep->ex_fs = fsb.f_fsid;
8671558Srgrimes					ep->ex_fsdir = (char *)
8681558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
8691558Srgrimes					if (ep->ex_fsdir)
8701558Srgrimes					    strcpy(ep->ex_fsdir,
8711558Srgrimes						fsb.f_mntonname);
8721558Srgrimes					else
8731558Srgrimes					    out_of_mem();
8741558Srgrimes					if (debug)
8751558Srgrimes					  fprintf(stderr,
8761558Srgrimes					      "Making new ep fs=0x%x,0x%x\n",
8771558Srgrimes					      fsb.f_fsid.val[0],
8781558Srgrimes					      fsb.f_fsid.val[1]);
8791558Srgrimes				    } else if (debug)
8801558Srgrimes					fprintf(stderr,
8811558Srgrimes					    "Found ep fs=0x%x,0x%x\n",
8821558Srgrimes					    fsb.f_fsid.val[0],
8831558Srgrimes					    fsb.f_fsid.val[1]);
8841558Srgrimes				}
8851558Srgrimes
8861558Srgrimes				/*
8871558Srgrimes				 * Add dirpath to export mount point.
8881558Srgrimes				 */
8891558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
8901558Srgrimes				dirplen = len;
8911558Srgrimes			    } else {
8921558Srgrimes				getexp_err(ep, tgrp);
8931558Srgrimes				goto nextline;
8941558Srgrimes			    }
8951558Srgrimes			    *endcp = savedc;
8961558Srgrimes			} else {
8971558Srgrimes			    savedc = *endcp;
8981558Srgrimes			    *endcp = '\0';
8991558Srgrimes			    got_nondir = 1;
9001558Srgrimes			    if (ep == (struct exportlist *)NULL) {
9011558Srgrimes				getexp_err(ep, tgrp);
9021558Srgrimes				goto nextline;
9031558Srgrimes			    }
9041558Srgrimes
9051558Srgrimes			    /*
9061558Srgrimes			     * Get the host or netgroup.
9071558Srgrimes			     */
9081558Srgrimes			    setnetgrent(cp);
9091558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
9101558Srgrimes			    do {
9111558Srgrimes				if (has_host) {
9121558Srgrimes				    grp->gr_next = get_grp();
9131558Srgrimes				    grp = grp->gr_next;
9141558Srgrimes				}
9151558Srgrimes				if (netgrp) {
91637003Sjoerg				    if (hst == 0) {
91737003Sjoerg					syslog(LOG_ERR, "Null hostname in netgroup %s, skipping", cp);
91837004Sjoerg					grp->gr_type = GT_IGNORE;
91937003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
92029317Sjlemon					syslog(LOG_ERR, "Bad host %s in netgroup %s, skipping", hst, cp);
92129317Sjlemon					grp->gr_type = GT_IGNORE;
9221558Srgrimes				    }
9237401Swpaul				} else if (get_host(cp, grp, tgrp)) {
92429317Sjlemon				    syslog(LOG_ERR, "Bad host %s, skipping", cp);
92529317Sjlemon				    grp->gr_type = GT_IGNORE;
9261558Srgrimes				}
9271558Srgrimes				has_host = TRUE;
9281558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
9291558Srgrimes			    endnetgrent();
9301558Srgrimes			    *endcp = savedc;
9311558Srgrimes			}
9321558Srgrimes			cp = endcp;
9331558Srgrimes			nextfield(&cp, &endcp);
9341558Srgrimes			len = endcp - cp;
9351558Srgrimes		}
9361558Srgrimes		if (check_options(dirhead)) {
9371558Srgrimes			getexp_err(ep, tgrp);
9381558Srgrimes			goto nextline;
9391558Srgrimes		}
9401558Srgrimes		if (!has_host) {
9411558Srgrimes			grp->gr_type = GT_HOST;
9421558Srgrimes			if (debug)
9431558Srgrimes				fprintf(stderr,"Adding a default entry\n");
9441558Srgrimes			/* add a default group and make the grp list NULL */
9451558Srgrimes			hpe = (struct hostent *)malloc(sizeof(struct hostent));
9461558Srgrimes			if (hpe == (struct hostent *)NULL)
9471558Srgrimes				out_of_mem();
94812348Sjoerg			hpe->h_name = strdup("Default");
9491558Srgrimes			hpe->h_addrtype = AF_INET;
9501558Srgrimes			hpe->h_length = sizeof (u_long);
9511558Srgrimes			hpe->h_addr_list = (char **)NULL;
9521558Srgrimes			grp->gr_ptr.gt_hostent = hpe;
9531558Srgrimes
9541558Srgrimes		/*
9551558Srgrimes		 * Don't allow a network export coincide with a list of
9561558Srgrimes		 * host(s) on the same line.
9571558Srgrimes		 */
9581558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
9591558Srgrimes			getexp_err(ep, tgrp);
9601558Srgrimes			goto nextline;
96129317Sjlemon
96229317Sjlemon        	/*
96329317Sjlemon	         * If an export list was specified on this line, make sure
96429317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
96529317Sjlemon		 */
96629317Sjlemon		} else {
96729317Sjlemon			grp = tgrp;
96829317Sjlemon        		while (grp && grp->gr_type == GT_IGNORE)
96929317Sjlemon				grp = grp->gr_next;
97029317Sjlemon			if (! grp) {
97129317Sjlemon			    getexp_err(ep, tgrp);
97229317Sjlemon			    goto nextline;
97329317Sjlemon			}
9741558Srgrimes		}
9751558Srgrimes
9761558Srgrimes		/*
9771558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
9781558Srgrimes		 * After loop, tgrp points to the start of the list and
9791558Srgrimes		 * grp points to the last entry in the list.
9801558Srgrimes		 */
9811558Srgrimes		grp = tgrp;
9821558Srgrimes		do {
9831558Srgrimes		    if (do_mount(ep, grp, exflags, &anon, dirp,
9841558Srgrimes			dirplen, &fsb)) {
9851558Srgrimes			getexp_err(ep, tgrp);
9861558Srgrimes			goto nextline;
9871558Srgrimes		    }
9881558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
9891558Srgrimes
9901558Srgrimes		/*
9911558Srgrimes		 * Success. Update the data structures.
9921558Srgrimes		 */
9931558Srgrimes		if (has_host) {
9949336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
9951558Srgrimes			grp->gr_next = grphead;
9961558Srgrimes			grphead = tgrp;
9971558Srgrimes		} else {
9981558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
9999336Sdfr				opt_flags);
10001558Srgrimes			free_grp(grp);
10011558Srgrimes		}
10021558Srgrimes		dirhead = (struct dirlist *)NULL;
10031558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
10041558Srgrimes			ep2 = exphead;
10051558Srgrimes			epp = &exphead;
10061558Srgrimes
10071558Srgrimes			/*
10081558Srgrimes			 * Insert in the list in alphabetical order.
10091558Srgrimes			 */
10101558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
10111558Srgrimes				epp = &ep2->ex_next;
10121558Srgrimes				ep2 = ep2->ex_next;
10131558Srgrimes			}
10141558Srgrimes			if (ep2)
10151558Srgrimes				ep->ex_next = ep2;
10161558Srgrimes			*epp = ep;
10171558Srgrimes			ep->ex_flag |= EX_LINKED;
10181558Srgrimes		}
10191558Srgrimesnextline:
10201558Srgrimes		if (dirhead) {
10211558Srgrimes			free_dir(dirhead);
10221558Srgrimes			dirhead = (struct dirlist *)NULL;
10231558Srgrimes		}
10241558Srgrimes	}
10251558Srgrimes	fclose(exp_file);
10261558Srgrimes}
10271558Srgrimes
10281558Srgrimes/*
10291558Srgrimes * Allocate an export list element
10301558Srgrimes */
10311558Srgrimesstruct exportlist *
10321558Srgrimesget_exp()
10331558Srgrimes{
10341558Srgrimes	struct exportlist *ep;
10351558Srgrimes
10361558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
10371558Srgrimes	if (ep == (struct exportlist *)NULL)
10381558Srgrimes		out_of_mem();
103923681Speter	memset(ep, 0, sizeof(struct exportlist));
10401558Srgrimes	return (ep);
10411558Srgrimes}
10421558Srgrimes
10431558Srgrimes/*
10441558Srgrimes * Allocate a group list element
10451558Srgrimes */
10461558Srgrimesstruct grouplist *
10471558Srgrimesget_grp()
10481558Srgrimes{
10491558Srgrimes	struct grouplist *gp;
10501558Srgrimes
10511558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
10521558Srgrimes	if (gp == (struct grouplist *)NULL)
10531558Srgrimes		out_of_mem();
105423681Speter	memset(gp, 0, sizeof(struct grouplist));
10551558Srgrimes	return (gp);
10561558Srgrimes}
10571558Srgrimes
10581558Srgrimes/*
10591558Srgrimes * Clean up upon an error in get_exportlist().
10601558Srgrimes */
10611558Srgrimesvoid
10621558Srgrimesgetexp_err(ep, grp)
10631558Srgrimes	struct exportlist *ep;
10641558Srgrimes	struct grouplist *grp;
10651558Srgrimes{
10661558Srgrimes	struct grouplist *tgrp;
10671558Srgrimes
10681558Srgrimes	syslog(LOG_ERR, "Bad exports list line %s", line);
10691558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
10701558Srgrimes		free_exp(ep);
10711558Srgrimes	while (grp) {
10721558Srgrimes		tgrp = grp;
10731558Srgrimes		grp = grp->gr_next;
10741558Srgrimes		free_grp(tgrp);
10751558Srgrimes	}
10761558Srgrimes}
10771558Srgrimes
10781558Srgrimes/*
10791558Srgrimes * Search the export list for a matching fs.
10801558Srgrimes */
10811558Srgrimesstruct exportlist *
10821558Srgrimesex_search(fsid)
10831558Srgrimes	fsid_t *fsid;
10841558Srgrimes{
10851558Srgrimes	struct exportlist *ep;
10861558Srgrimes
10871558Srgrimes	ep = exphead;
10881558Srgrimes	while (ep) {
10891558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
10901558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
10911558Srgrimes			return (ep);
10921558Srgrimes		ep = ep->ex_next;
10931558Srgrimes	}
10941558Srgrimes	return (ep);
10951558Srgrimes}
10961558Srgrimes
10971558Srgrimes/*
10981558Srgrimes * Add a directory path to the list.
10991558Srgrimes */
11001558Srgrimeschar *
11011558Srgrimesadd_expdir(dpp, cp, len)
11021558Srgrimes	struct dirlist **dpp;
11031558Srgrimes	char *cp;
11041558Srgrimes	int len;
11051558Srgrimes{
11061558Srgrimes	struct dirlist *dp;
11071558Srgrimes
11081558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
11091558Srgrimes	dp->dp_left = *dpp;
11101558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
11111558Srgrimes	dp->dp_flag = 0;
11121558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
11131558Srgrimes	strcpy(dp->dp_dirp, cp);
11141558Srgrimes	*dpp = dp;
11151558Srgrimes	return (dp->dp_dirp);
11161558Srgrimes}
11171558Srgrimes
11181558Srgrimes/*
11191558Srgrimes * Hang the dir list element off the dirpath binary tree as required
11201558Srgrimes * and update the entry for host.
11211558Srgrimes */
11221558Srgrimesvoid
11239336Sdfrhang_dirp(dp, grp, ep, flags)
11241558Srgrimes	struct dirlist *dp;
11251558Srgrimes	struct grouplist *grp;
11261558Srgrimes	struct exportlist *ep;
11279336Sdfr	int flags;
11281558Srgrimes{
11291558Srgrimes	struct hostlist *hp;
11301558Srgrimes	struct dirlist *dp2;
11311558Srgrimes
11329336Sdfr	if (flags & OP_ALLDIRS) {
11331558Srgrimes		if (ep->ex_defdir)
11341558Srgrimes			free((caddr_t)dp);
11351558Srgrimes		else
11361558Srgrimes			ep->ex_defdir = dp;
11379336Sdfr		if (grp == (struct grouplist *)NULL) {
11381558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
11399336Sdfr			if (flags & OP_KERB)
11409336Sdfr				ep->ex_defdir->dp_flag |= DP_KERB;
11419336Sdfr		} else while (grp) {
11421558Srgrimes			hp = get_ht();
11439336Sdfr			if (flags & OP_KERB)
11449336Sdfr				hp->ht_flag |= DP_KERB;
11451558Srgrimes			hp->ht_grp = grp;
11461558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
11471558Srgrimes			ep->ex_defdir->dp_hosts = hp;
11481558Srgrimes			grp = grp->gr_next;
11491558Srgrimes		}
11501558Srgrimes	} else {
11511558Srgrimes
11521558Srgrimes		/*
11531558Srgrimes		 * Loop throught the directories adding them to the tree.
11541558Srgrimes		 */
11551558Srgrimes		while (dp) {
11561558Srgrimes			dp2 = dp->dp_left;
11579336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
11581558Srgrimes			dp = dp2;
11591558Srgrimes		}
11601558Srgrimes	}
11611558Srgrimes}
11621558Srgrimes
11631558Srgrimes/*
11641558Srgrimes * Traverse the binary tree either updating a node that is already there
11651558Srgrimes * for the new directory or adding the new node.
11661558Srgrimes */
11671558Srgrimesvoid
11689336Sdfradd_dlist(dpp, newdp, grp, flags)
11691558Srgrimes	struct dirlist **dpp;
11701558Srgrimes	struct dirlist *newdp;
11711558Srgrimes	struct grouplist *grp;
11729336Sdfr	int flags;
11731558Srgrimes{
11741558Srgrimes	struct dirlist *dp;
11751558Srgrimes	struct hostlist *hp;
11761558Srgrimes	int cmp;
11771558Srgrimes
11781558Srgrimes	dp = *dpp;
11791558Srgrimes	if (dp) {
11801558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
11811558Srgrimes		if (cmp > 0) {
11829336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
11831558Srgrimes			return;
11841558Srgrimes		} else if (cmp < 0) {
11859336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
11861558Srgrimes			return;
11871558Srgrimes		} else
11881558Srgrimes			free((caddr_t)newdp);
11891558Srgrimes	} else {
11901558Srgrimes		dp = newdp;
11911558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
11921558Srgrimes		*dpp = dp;
11931558Srgrimes	}
11941558Srgrimes	if (grp) {
11951558Srgrimes
11961558Srgrimes		/*
11971558Srgrimes		 * Hang all of the host(s) off of the directory point.
11981558Srgrimes		 */
11991558Srgrimes		do {
12001558Srgrimes			hp = get_ht();
12019336Sdfr			if (flags & OP_KERB)
12029336Sdfr				hp->ht_flag |= DP_KERB;
12031558Srgrimes			hp->ht_grp = grp;
12041558Srgrimes			hp->ht_next = dp->dp_hosts;
12051558Srgrimes			dp->dp_hosts = hp;
12061558Srgrimes			grp = grp->gr_next;
12071558Srgrimes		} while (grp);
12089336Sdfr	} else {
12091558Srgrimes		dp->dp_flag |= DP_DEFSET;
12109336Sdfr		if (flags & OP_KERB)
12119336Sdfr			dp->dp_flag |= DP_KERB;
12129336Sdfr	}
12131558Srgrimes}
12141558Srgrimes
12151558Srgrimes/*
12161558Srgrimes * Search for a dirpath on the export point.
12171558Srgrimes */
12181558Srgrimesstruct dirlist *
12191558Srgrimesdirp_search(dp, dirpath)
12201558Srgrimes	struct dirlist *dp;
12211558Srgrimes	char *dirpath;
12221558Srgrimes{
12231558Srgrimes	int cmp;
12241558Srgrimes
12251558Srgrimes	if (dp) {
12261558Srgrimes		cmp = strcmp(dp->dp_dirp, dirpath);
12271558Srgrimes		if (cmp > 0)
12281558Srgrimes			return (dirp_search(dp->dp_left, dirpath));
12291558Srgrimes		else if (cmp < 0)
12301558Srgrimes			return (dirp_search(dp->dp_right, dirpath));
12311558Srgrimes		else
12321558Srgrimes			return (dp);
12331558Srgrimes	}
12341558Srgrimes	return (dp);
12351558Srgrimes}
12361558Srgrimes
12371558Srgrimes/*
12381558Srgrimes * Scan for a host match in a directory tree.
12391558Srgrimes */
12401558Srgrimesint
12419336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
12421558Srgrimes	struct dirlist *dp;
12431558Srgrimes	u_long saddr;
12441558Srgrimes	int *defsetp;
12459336Sdfr	int *hostsetp;
12461558Srgrimes{
12471558Srgrimes	struct hostlist *hp;
12481558Srgrimes	struct grouplist *grp;
12491558Srgrimes	u_long **addrp;
12501558Srgrimes
12511558Srgrimes	if (dp) {
12521558Srgrimes		if (dp->dp_flag & DP_DEFSET)
12539336Sdfr			*defsetp = dp->dp_flag;
12541558Srgrimes		hp = dp->dp_hosts;
12551558Srgrimes		while (hp) {
12561558Srgrimes			grp = hp->ht_grp;
12571558Srgrimes			switch (grp->gr_type) {
12581558Srgrimes			case GT_HOST:
12591558Srgrimes			    addrp = (u_long **)
12601558Srgrimes				grp->gr_ptr.gt_hostent->h_addr_list;
12611558Srgrimes			    while (*addrp) {
12629336Sdfr				if (**addrp == saddr) {
12639336Sdfr				    *hostsetp = (hp->ht_flag | DP_HOSTSET);
12641558Srgrimes				    return (1);
12659336Sdfr				}
12661558Srgrimes				addrp++;
12671558Srgrimes			    }
12681558Srgrimes			    break;
12691558Srgrimes			case GT_NET:
12701558Srgrimes			    if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
12719336Sdfr				grp->gr_ptr.gt_net.nt_net) {
12729336Sdfr				*hostsetp = (hp->ht_flag | DP_HOSTSET);
12731558Srgrimes				return (1);
12749336Sdfr			    }
12751558Srgrimes			    break;
12761558Srgrimes			};
12771558Srgrimes			hp = hp->ht_next;
12781558Srgrimes		}
12791558Srgrimes	}
12801558Srgrimes	return (0);
12811558Srgrimes}
12821558Srgrimes
12831558Srgrimes/*
12841558Srgrimes * Scan tree for a host that matches the address.
12851558Srgrimes */
12861558Srgrimesint
12871558Srgrimesscan_tree(dp, saddr)
12881558Srgrimes	struct dirlist *dp;
12891558Srgrimes	u_long saddr;
12901558Srgrimes{
12919336Sdfr	int defset, hostset;
12921558Srgrimes
12931558Srgrimes	if (dp) {
12941558Srgrimes		if (scan_tree(dp->dp_left, saddr))
12951558Srgrimes			return (1);
12969336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
12971558Srgrimes			return (1);
12981558Srgrimes		if (scan_tree(dp->dp_right, saddr))
12991558Srgrimes			return (1);
13001558Srgrimes	}
13011558Srgrimes	return (0);
13021558Srgrimes}
13031558Srgrimes
13041558Srgrimes/*
13051558Srgrimes * Traverse the dirlist tree and free it up.
13061558Srgrimes */
13071558Srgrimesvoid
13081558Srgrimesfree_dir(dp)
13091558Srgrimes	struct dirlist *dp;
13101558Srgrimes{
13111558Srgrimes
13121558Srgrimes	if (dp) {
13131558Srgrimes		free_dir(dp->dp_left);
13141558Srgrimes		free_dir(dp->dp_right);
13151558Srgrimes		free_host(dp->dp_hosts);
13161558Srgrimes		free((caddr_t)dp);
13171558Srgrimes	}
13181558Srgrimes}
13191558Srgrimes
13201558Srgrimes/*
13211558Srgrimes * Parse the option string and update fields.
13221558Srgrimes * Option arguments may either be -<option>=<value> or
13231558Srgrimes * -<option> <value>
13241558Srgrimes */
13251558Srgrimesint
13261558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
13271558Srgrimes	char **cpp, **endcpp;
13281558Srgrimes	struct exportlist *ep;
13291558Srgrimes	struct grouplist *grp;
13301558Srgrimes	int *has_hostp;
13311558Srgrimes	int *exflagsp;
13321558Srgrimes	struct ucred *cr;
13331558Srgrimes{
13341558Srgrimes	char *cpoptarg, *cpoptend;
13351558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
13361558Srgrimes	int allflag, usedarg;
13371558Srgrimes
13381558Srgrimes	cpopt = *cpp;
13391558Srgrimes	cpopt++;
13401558Srgrimes	cp = *endcpp;
13411558Srgrimes	savedc = *cp;
13421558Srgrimes	*cp = '\0';
13431558Srgrimes	while (cpopt && *cpopt) {
13441558Srgrimes		allflag = 1;
13451558Srgrimes		usedarg = -2;
134623681Speter		if (cpoptend = strchr(cpopt, ',')) {
13471558Srgrimes			*cpoptend++ = '\0';
134823681Speter			if (cpoptarg = strchr(cpopt, '='))
13491558Srgrimes				*cpoptarg++ = '\0';
13501558Srgrimes		} else {
135123681Speter			if (cpoptarg = strchr(cpopt, '='))
13521558Srgrimes				*cpoptarg++ = '\0';
13531558Srgrimes			else {
13541558Srgrimes				*cp = savedc;
13551558Srgrimes				nextfield(&cp, &endcp);
13561558Srgrimes				**endcpp = '\0';
13571558Srgrimes				if (endcp > cp && *cp != '-') {
13581558Srgrimes					cpoptarg = cp;
13591558Srgrimes					savedc2 = *endcp;
13601558Srgrimes					*endcp = '\0';
13611558Srgrimes					usedarg = 0;
13621558Srgrimes				}
13631558Srgrimes			}
13641558Srgrimes		}
13651558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
13661558Srgrimes			*exflagsp |= MNT_EXRDONLY;
13671558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
13681558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
13691558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
13701558Srgrimes			usedarg++;
13711558Srgrimes			parsecred(cpoptarg, cr);
13721558Srgrimes			if (allflag == 0) {
13731558Srgrimes				*exflagsp |= MNT_EXPORTANON;
13741558Srgrimes				opt_flags |= OP_MAPALL;
13751558Srgrimes			} else
13761558Srgrimes				opt_flags |= OP_MAPROOT;
13771558Srgrimes		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
13781558Srgrimes			*exflagsp |= MNT_EXKERB;
13791558Srgrimes			opt_flags |= OP_KERB;
13801558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
13811558Srgrimes			!strcmp(cpopt, "m"))) {
13821558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
13831558Srgrimes				syslog(LOG_ERR, "Bad mask: %s", cpoptarg);
13841558Srgrimes				return (1);
13851558Srgrimes			}
13861558Srgrimes			usedarg++;
13871558Srgrimes			opt_flags |= OP_MASK;
13881558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
13891558Srgrimes			!strcmp(cpopt, "n"))) {
13901558Srgrimes			if (grp->gr_type != GT_NULL) {
13911558Srgrimes				syslog(LOG_ERR, "Network/host conflict");
13921558Srgrimes				return (1);
13931558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
13941558Srgrimes				syslog(LOG_ERR, "Bad net: %s", cpoptarg);
13951558Srgrimes				return (1);
13961558Srgrimes			}
13971558Srgrimes			grp->gr_type = GT_NET;
13981558Srgrimes			*has_hostp = 1;
13991558Srgrimes			usedarg++;
14001558Srgrimes			opt_flags |= OP_NET;
14011558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
14021558Srgrimes			opt_flags |= OP_ALLDIRS;
140327447Sdfr		} else if (!strcmp(cpopt, "public")) {
140427447Sdfr			*exflagsp |= MNT_EXPUBLIC;
140527447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
140627447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
140727447Sdfr			opt_flags |= OP_MAPALL;
140827447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
140927447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
14101558Srgrimes#ifdef ISO
14111558Srgrimes		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
14121558Srgrimes			if (get_isoaddr(cpoptarg, grp)) {
14131558Srgrimes				syslog(LOG_ERR, "Bad iso addr: %s", cpoptarg);
14141558Srgrimes				return (1);
14151558Srgrimes			}
14161558Srgrimes			*has_hostp = 1;
14171558Srgrimes			usedarg++;
14181558Srgrimes			opt_flags |= OP_ISO;
14191558Srgrimes#endif /* ISO */
14201558Srgrimes		} else {
14211558Srgrimes			syslog(LOG_ERR, "Bad opt %s", cpopt);
14221558Srgrimes			return (1);
14231558Srgrimes		}
14241558Srgrimes		if (usedarg >= 0) {
14251558Srgrimes			*endcp = savedc2;
14261558Srgrimes			**endcpp = savedc;
14271558Srgrimes			if (usedarg > 0) {
14281558Srgrimes				*cpp = cp;
14291558Srgrimes				*endcpp = endcp;
14301558Srgrimes			}
14311558Srgrimes			return (0);
14321558Srgrimes		}
14331558Srgrimes		cpopt = cpoptend;
14341558Srgrimes	}
14351558Srgrimes	**endcpp = savedc;
14361558Srgrimes	return (0);
14371558Srgrimes}
14381558Srgrimes
14391558Srgrimes/*
14401558Srgrimes * Translate a character string to the corresponding list of network
14411558Srgrimes * addresses for a hostname.
14421558Srgrimes */
14431558Srgrimesint
14447401Swpaulget_host(cp, grp, tgrp)
14451558Srgrimes	char *cp;
14461558Srgrimes	struct grouplist *grp;
14477401Swpaul	struct grouplist *tgrp;
14481558Srgrimes{
14497401Swpaul	struct grouplist *checkgrp;
14501558Srgrimes	struct hostent *hp, *nhp;
14511558Srgrimes	char **addrp, **naddrp;
14521558Srgrimes	struct hostent t_host;
14531558Srgrimes	int i;
14541558Srgrimes	u_long saddr;
14551558Srgrimes	char *aptr[2];
14561558Srgrimes
14571558Srgrimes	if (grp->gr_type != GT_NULL)
14581558Srgrimes		return (1);
14591558Srgrimes	if ((hp = gethostbyname(cp)) == NULL) {
14601558Srgrimes		if (isdigit(*cp)) {
14611558Srgrimes			saddr = inet_addr(cp);
14621558Srgrimes			if (saddr == -1) {
14639336Sdfr 				syslog(LOG_ERR, "Inet_addr failed for %s", cp);
14641558Srgrimes				return (1);
14651558Srgrimes			}
14661558Srgrimes			if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
14671558Srgrimes				AF_INET)) == NULL) {
14681558Srgrimes				hp = &t_host;
14691558Srgrimes				hp->h_name = cp;
14701558Srgrimes				hp->h_addrtype = AF_INET;
14711558Srgrimes				hp->h_length = sizeof (u_long);
14721558Srgrimes				hp->h_addr_list = aptr;
14731558Srgrimes				aptr[0] = (char *)&saddr;
14741558Srgrimes				aptr[1] = (char *)NULL;
14751558Srgrimes			}
14761558Srgrimes		} else {
14779336Sdfr 			syslog(LOG_ERR, "Gethostbyname failed for %s", cp);
14781558Srgrimes			return (1);
14791558Srgrimes		}
14801558Srgrimes	}
14817401Swpaul        /*
14827401Swpaul         * Sanity check: make sure we don't already have an entry
14837401Swpaul         * for this host in the grouplist.
14847401Swpaul         */
14857401Swpaul        checkgrp = tgrp;
14867401Swpaul        while (checkgrp) {
148717887Swpaul		if (checkgrp->gr_type == GT_HOST &&
148817887Swpaul                    checkgrp->gr_ptr.gt_hostent != NULL &&
14897401Swpaul                    !strcmp(checkgrp->gr_ptr.gt_hostent->h_name, hp->h_name)) {
14907401Swpaul                        grp->gr_type = GT_IGNORE;
14917401Swpaul			return(0);
14927401Swpaul		}
14937401Swpaul                checkgrp = checkgrp->gr_next;
14947401Swpaul        }
14957401Swpaul
14961558Srgrimes	grp->gr_type = GT_HOST;
14971558Srgrimes	nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
14981558Srgrimes		malloc(sizeof(struct hostent));
14991558Srgrimes	if (nhp == (struct hostent *)NULL)
15001558Srgrimes		out_of_mem();
150123681Speter	memmove(nhp, hp, sizeof(struct hostent));
15021558Srgrimes	i = strlen(hp->h_name)+1;
15031558Srgrimes	nhp->h_name = (char *)malloc(i);
15041558Srgrimes	if (nhp->h_name == (char *)NULL)
15051558Srgrimes		out_of_mem();
150623681Speter	memmove(nhp->h_name, hp->h_name, i);
15071558Srgrimes	addrp = hp->h_addr_list;
15081558Srgrimes	i = 1;
15091558Srgrimes	while (*addrp++)
15101558Srgrimes		i++;
15111558Srgrimes	naddrp = nhp->h_addr_list = (char **)
15121558Srgrimes		malloc(i*sizeof(char *));
15131558Srgrimes	if (naddrp == (char **)NULL)
15141558Srgrimes		out_of_mem();
15151558Srgrimes	addrp = hp->h_addr_list;
15161558Srgrimes	while (*addrp) {
15171558Srgrimes		*naddrp = (char *)
15181558Srgrimes		    malloc(hp->h_length);
15191558Srgrimes		if (*naddrp == (char *)NULL)
15201558Srgrimes		    out_of_mem();
152123681Speter		memmove(*naddrp, *addrp, hp->h_length);
15221558Srgrimes		addrp++;
15231558Srgrimes		naddrp++;
15241558Srgrimes	}
15251558Srgrimes	*naddrp = (char *)NULL;
15261558Srgrimes	if (debug)
15271558Srgrimes		fprintf(stderr, "got host %s\n", hp->h_name);
15281558Srgrimes	return (0);
15291558Srgrimes}
15301558Srgrimes
15311558Srgrimes/*
15321558Srgrimes * Free up an exports list component
15331558Srgrimes */
15341558Srgrimesvoid
15351558Srgrimesfree_exp(ep)
15361558Srgrimes	struct exportlist *ep;
15371558Srgrimes{
15381558Srgrimes
15391558Srgrimes	if (ep->ex_defdir) {
15401558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
15411558Srgrimes		free((caddr_t)ep->ex_defdir);
15421558Srgrimes	}
15431558Srgrimes	if (ep->ex_fsdir)
15441558Srgrimes		free(ep->ex_fsdir);
154527447Sdfr	if (ep->ex_indexfile)
154627447Sdfr		free(ep->ex_indexfile);
15471558Srgrimes	free_dir(ep->ex_dirl);
15481558Srgrimes	free((caddr_t)ep);
15491558Srgrimes}
15501558Srgrimes
15511558Srgrimes/*
15521558Srgrimes * Free hosts.
15531558Srgrimes */
15541558Srgrimesvoid
15551558Srgrimesfree_host(hp)
15561558Srgrimes	struct hostlist *hp;
15571558Srgrimes{
15581558Srgrimes	struct hostlist *hp2;
15591558Srgrimes
15601558Srgrimes	while (hp) {
15611558Srgrimes		hp2 = hp;
15621558Srgrimes		hp = hp->ht_next;
15631558Srgrimes		free((caddr_t)hp2);
15641558Srgrimes	}
15651558Srgrimes}
15661558Srgrimes
15671558Srgrimesstruct hostlist *
15681558Srgrimesget_ht()
15691558Srgrimes{
15701558Srgrimes	struct hostlist *hp;
15711558Srgrimes
15721558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
15731558Srgrimes	if (hp == (struct hostlist *)NULL)
15741558Srgrimes		out_of_mem();
15751558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
15769336Sdfr	hp->ht_flag = 0;
15771558Srgrimes	return (hp);
15781558Srgrimes}
15791558Srgrimes
15801558Srgrimes#ifdef ISO
15811558Srgrimes/*
15821558Srgrimes * Translate an iso address.
15831558Srgrimes */
15841558Srgrimesget_isoaddr(cp, grp)
15851558Srgrimes	char *cp;
15861558Srgrimes	struct grouplist *grp;
15871558Srgrimes{
15881558Srgrimes	struct iso_addr *isop;
15891558Srgrimes	struct sockaddr_iso *isoaddr;
15901558Srgrimes
15911558Srgrimes	if (grp->gr_type != GT_NULL)
15921558Srgrimes		return (1);
15931558Srgrimes	if ((isop = iso_addr(cp)) == NULL) {
15941558Srgrimes		syslog(LOG_ERR,
15951558Srgrimes		    "iso_addr failed, ignored");
15961558Srgrimes		return (1);
15971558Srgrimes	}
15981558Srgrimes	isoaddr = (struct sockaddr_iso *)
15991558Srgrimes	    malloc(sizeof (struct sockaddr_iso));
16001558Srgrimes	if (isoaddr == (struct sockaddr_iso *)NULL)
16011558Srgrimes		out_of_mem();
160223681Speter	memset(isoaddr, 0, sizeof(struct sockaddr_iso));
160323681Speter	memmove(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
160423681Speter	isoaddr->siso_len = sizeof(struct sockaddr_iso);
16051558Srgrimes	isoaddr->siso_family = AF_ISO;
16061558Srgrimes	grp->gr_type = GT_ISO;
16071558Srgrimes	grp->gr_ptr.gt_isoaddr = isoaddr;
16081558Srgrimes	return (0);
16091558Srgrimes}
16101558Srgrimes#endif	/* ISO */
16111558Srgrimes
16121558Srgrimes/*
16131558Srgrimes * Out of memory, fatal
16141558Srgrimes */
16151558Srgrimesvoid
16161558Srgrimesout_of_mem()
16171558Srgrimes{
16181558Srgrimes
16191558Srgrimes	syslog(LOG_ERR, "Out of memory");
16201558Srgrimes	exit(2);
16211558Srgrimes}
16221558Srgrimes
16231558Srgrimes/*
16241558Srgrimes * Do the mount syscall with the update flag to push the export info into
16251558Srgrimes * the kernel.
16261558Srgrimes */
16271558Srgrimesint
16281558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
16291558Srgrimes	struct exportlist *ep;
16301558Srgrimes	struct grouplist *grp;
16311558Srgrimes	int exflags;
16321558Srgrimes	struct ucred *anoncrp;
16331558Srgrimes	char *dirp;
16341558Srgrimes	int dirplen;
16351558Srgrimes	struct statfs *fsb;
16361558Srgrimes{
16371558Srgrimes	char *cp = (char *)NULL;
16381558Srgrimes	u_long **addrp;
16391558Srgrimes	int done;
16401558Srgrimes	char savedc = '\0';
16411558Srgrimes	struct sockaddr_in sin, imask;
16421558Srgrimes	union {
16431558Srgrimes		struct ufs_args ua;
16441558Srgrimes		struct iso_args ia;
16451558Srgrimes		struct mfs_args ma;
16469336Sdfr#ifdef __NetBSD__
16479336Sdfr		struct msdosfs_args da;
16489336Sdfr#endif
16491558Srgrimes	} args;
16501558Srgrimes	u_long net;
16511558Srgrimes
16521558Srgrimes	args.ua.fspec = 0;
16531558Srgrimes	args.ua.export.ex_flags = exflags;
16541558Srgrimes	args.ua.export.ex_anon = *anoncrp;
165527447Sdfr	args.ua.export.ex_indexfile = ep->ex_indexfile;
165623681Speter	memset(&sin, 0, sizeof(sin));
165723681Speter	memset(&imask, 0, sizeof(imask));
16581558Srgrimes	sin.sin_family = AF_INET;
16591558Srgrimes	sin.sin_len = sizeof(sin);
16601558Srgrimes	imask.sin_family = AF_INET;
16611558Srgrimes	imask.sin_len = sizeof(sin);
16621558Srgrimes	if (grp->gr_type == GT_HOST)
16631558Srgrimes		addrp = (u_long **)grp->gr_ptr.gt_hostent->h_addr_list;
16641558Srgrimes	else
16651558Srgrimes		addrp = (u_long **)NULL;
16661558Srgrimes	done = FALSE;
16671558Srgrimes	while (!done) {
16681558Srgrimes		switch (grp->gr_type) {
16691558Srgrimes		case GT_HOST:
16701558Srgrimes			if (addrp) {
16711558Srgrimes				sin.sin_addr.s_addr = **addrp;
16721558Srgrimes				args.ua.export.ex_addrlen = sizeof(sin);
16731558Srgrimes			} else
16741558Srgrimes				args.ua.export.ex_addrlen = 0;
16751558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16761558Srgrimes			args.ua.export.ex_masklen = 0;
16771558Srgrimes			break;
16781558Srgrimes		case GT_NET:
16791558Srgrimes			if (grp->gr_ptr.gt_net.nt_mask)
16801558Srgrimes			    imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
16811558Srgrimes			else {
16821558Srgrimes			    net = ntohl(grp->gr_ptr.gt_net.nt_net);
16831558Srgrimes			    if (IN_CLASSA(net))
16841558Srgrimes				imask.sin_addr.s_addr = inet_addr("255.0.0.0");
16851558Srgrimes			    else if (IN_CLASSB(net))
16861558Srgrimes				imask.sin_addr.s_addr =
16871558Srgrimes				    inet_addr("255.255.0.0");
16881558Srgrimes			    else
16891558Srgrimes				imask.sin_addr.s_addr =
16901558Srgrimes				    inet_addr("255.255.255.0");
16911558Srgrimes			    grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
16921558Srgrimes			}
16931558Srgrimes			sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
16941558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16951558Srgrimes			args.ua.export.ex_addrlen = sizeof (sin);
16961558Srgrimes			args.ua.export.ex_mask = (struct sockaddr *)&imask;
16971558Srgrimes			args.ua.export.ex_masklen = sizeof (imask);
16981558Srgrimes			break;
16991558Srgrimes#ifdef ISO
17001558Srgrimes		case GT_ISO:
17011558Srgrimes			args.ua.export.ex_addr =
17021558Srgrimes				(struct sockaddr *)grp->gr_ptr.gt_isoaddr;
17031558Srgrimes			args.ua.export.ex_addrlen =
17041558Srgrimes				sizeof(struct sockaddr_iso);
17051558Srgrimes			args.ua.export.ex_masklen = 0;
17061558Srgrimes			break;
17071558Srgrimes#endif	/* ISO */
17087401Swpaul		case GT_IGNORE:
17097401Swpaul			return(0);
17107401Swpaul			break;
17111558Srgrimes		default:
17121558Srgrimes			syslog(LOG_ERR, "Bad grouptype");
17131558Srgrimes			if (cp)
17141558Srgrimes				*cp = savedc;
17151558Srgrimes			return (1);
17161558Srgrimes		};
17171558Srgrimes
17181558Srgrimes		/*
17191558Srgrimes		 * XXX:
17201558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
17211558Srgrimes		 * of looping back up the dirp to the mount point??
17221558Srgrimes		 * Also, needs to know how to export all types of local
172323681Speter		 * exportable file systems and not just "ufs".
17241558Srgrimes		 */
17259336Sdfr		while (mount(fsb->f_fstypename, dirp,
17261558Srgrimes		       fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
17271558Srgrimes			if (cp)
17281558Srgrimes				*cp-- = savedc;
17291558Srgrimes			else
17301558Srgrimes				cp = dirp + dirplen - 1;
17311558Srgrimes			if (errno == EPERM) {
17321558Srgrimes				syslog(LOG_ERR,
17331558Srgrimes				   "Can't change attributes for %s.\n", dirp);
17341558Srgrimes				return (1);
17351558Srgrimes			}
17361558Srgrimes			if (opt_flags & OP_ALLDIRS) {
17374895Swollman				syslog(LOG_ERR, "Could not remount %s: %m",
17384895Swollman					dirp);
17391558Srgrimes				return (1);
17401558Srgrimes			}
17411558Srgrimes			/* back up over the last component */
17421558Srgrimes			while (*cp == '/' && cp > dirp)
17431558Srgrimes				cp--;
17441558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
17451558Srgrimes				cp--;
17461558Srgrimes			if (cp == dirp) {
17471558Srgrimes				if (debug)
17481558Srgrimes					fprintf(stderr,"mnt unsucc\n");
17491558Srgrimes				syslog(LOG_ERR, "Can't export %s", dirp);
17501558Srgrimes				return (1);
17511558Srgrimes			}
17521558Srgrimes			savedc = *cp;
17531558Srgrimes			*cp = '\0';
17541558Srgrimes		}
17551558Srgrimes		if (addrp) {
17561558Srgrimes			++addrp;
17571558Srgrimes			if (*addrp == (u_long *)NULL)
17581558Srgrimes				done = TRUE;
17591558Srgrimes		} else
17601558Srgrimes			done = TRUE;
17611558Srgrimes	}
17621558Srgrimes	if (cp)
17631558Srgrimes		*cp = savedc;
17641558Srgrimes	return (0);
17651558Srgrimes}
17661558Srgrimes
17671558Srgrimes/*
17681558Srgrimes * Translate a net address.
17691558Srgrimes */
17701558Srgrimesint
17711558Srgrimesget_net(cp, net, maskflg)
17721558Srgrimes	char *cp;
17731558Srgrimes	struct netmsk *net;
17741558Srgrimes	int maskflg;
17751558Srgrimes{
17761558Srgrimes	struct netent *np;
17771558Srgrimes	long netaddr;
17781558Srgrimes	struct in_addr inetaddr, inetaddr2;
17791558Srgrimes	char *name;
17801558Srgrimes
178125318Spst	if (isdigit(*cp) && ((netaddr = inet_network(cp)) != -1)) {
17821558Srgrimes		inetaddr = inet_makeaddr(netaddr, 0);
17831558Srgrimes		/*
17841558Srgrimes		 * Due to arbritrary subnet masks, you don't know how many
17851558Srgrimes		 * bits to shift the address to make it into a network,
17861558Srgrimes		 * however you do know how to make a network address into
17871558Srgrimes		 * a host with host == 0 and then compare them.
17881558Srgrimes		 * (What a pest)
17891558Srgrimes		 */
17901558Srgrimes		if (!maskflg) {
17911558Srgrimes			setnetent(0);
17921558Srgrimes			while (np = getnetent()) {
17931558Srgrimes				inetaddr2 = inet_makeaddr(np->n_net, 0);
17941558Srgrimes				if (inetaddr2.s_addr == inetaddr.s_addr)
17951558Srgrimes					break;
17961558Srgrimes			}
17971558Srgrimes			endnetent();
17981558Srgrimes		}
179925318Spst	} else if ((np = getnetbyname(cp)) != NULL) {
180025318Spst		inetaddr = inet_makeaddr(np->n_net, 0);
18011558Srgrimes	} else
18021558Srgrimes		return (1);
180325318Spst
18041558Srgrimes	if (maskflg)
18051558Srgrimes		net->nt_mask = inetaddr.s_addr;
18061558Srgrimes	else {
18071558Srgrimes		if (np)
18081558Srgrimes			name = np->n_name;
18091558Srgrimes		else
18101558Srgrimes			name = inet_ntoa(inetaddr);
18111558Srgrimes		net->nt_name = (char *)malloc(strlen(name) + 1);
18121558Srgrimes		if (net->nt_name == (char *)NULL)
18131558Srgrimes			out_of_mem();
18141558Srgrimes		strcpy(net->nt_name, name);
18151558Srgrimes		net->nt_net = inetaddr.s_addr;
18161558Srgrimes	}
18171558Srgrimes	return (0);
18181558Srgrimes}
18191558Srgrimes
18201558Srgrimes/*
18211558Srgrimes * Parse out the next white space separated field
18221558Srgrimes */
18231558Srgrimesvoid
18241558Srgrimesnextfield(cp, endcp)
18251558Srgrimes	char **cp;
18261558Srgrimes	char **endcp;
18271558Srgrimes{
18281558Srgrimes	char *p;
18291558Srgrimes
18301558Srgrimes	p = *cp;
18311558Srgrimes	while (*p == ' ' || *p == '\t')
18321558Srgrimes		p++;
18331558Srgrimes	if (*p == '\n' || *p == '\0')
18341558Srgrimes		*cp = *endcp = p;
18351558Srgrimes	else {
18361558Srgrimes		*cp = p++;
18371558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
18381558Srgrimes			p++;
18391558Srgrimes		*endcp = p;
18401558Srgrimes	}
18411558Srgrimes}
18421558Srgrimes
18431558Srgrimes/*
18441558Srgrimes * Get an exports file line. Skip over blank lines and handle line
18451558Srgrimes * continuations.
18461558Srgrimes */
18471558Srgrimesint
18481558Srgrimesget_line()
18491558Srgrimes{
18501558Srgrimes	char *p, *cp;
18511558Srgrimes	int len;
18521558Srgrimes	int totlen, cont_line;
18531558Srgrimes
18541558Srgrimes	/*
18551558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
18561558Srgrimes	 */
18571558Srgrimes	p = line;
18581558Srgrimes	totlen = 0;
18591558Srgrimes	do {
18601558Srgrimes		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
18611558Srgrimes			return (0);
18621558Srgrimes		len = strlen(p);
18631558Srgrimes		cp = p + len - 1;
18641558Srgrimes		cont_line = 0;
18651558Srgrimes		while (cp >= p &&
18661558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
18671558Srgrimes			if (*cp == '\\')
18681558Srgrimes				cont_line = 1;
18691558Srgrimes			cp--;
18701558Srgrimes			len--;
18711558Srgrimes		}
18721558Srgrimes		*++cp = '\0';
18731558Srgrimes		if (len > 0) {
18741558Srgrimes			totlen += len;
18751558Srgrimes			if (totlen >= LINESIZ) {
18761558Srgrimes				syslog(LOG_ERR, "Exports line too long");
18771558Srgrimes				exit(2);
18781558Srgrimes			}
18791558Srgrimes			p = cp;
18801558Srgrimes		}
18811558Srgrimes	} while (totlen == 0 || cont_line);
18821558Srgrimes	return (1);
18831558Srgrimes}
18841558Srgrimes
18851558Srgrimes/*
18861558Srgrimes * Parse a description of a credential.
18871558Srgrimes */
18881558Srgrimesvoid
18891558Srgrimesparsecred(namelist, cr)
18901558Srgrimes	char *namelist;
18911558Srgrimes	struct ucred *cr;
18921558Srgrimes{
18931558Srgrimes	char *name;
18941558Srgrimes	int cnt;
18951558Srgrimes	char *names;
18961558Srgrimes	struct passwd *pw;
18971558Srgrimes	struct group *gr;
18981558Srgrimes	int ngroups, groups[NGROUPS + 1];
18991558Srgrimes
19001558Srgrimes	/*
19011558Srgrimes	 * Set up the unpriviledged user.
19021558Srgrimes	 */
19031558Srgrimes	cr->cr_ref = 1;
19041558Srgrimes	cr->cr_uid = -2;
19051558Srgrimes	cr->cr_groups[0] = -2;
19061558Srgrimes	cr->cr_ngroups = 1;
19071558Srgrimes	/*
19081558Srgrimes	 * Get the user's password table entry.
19091558Srgrimes	 */
19101558Srgrimes	names = strsep(&namelist, " \t\n");
19111558Srgrimes	name = strsep(&names, ":");
19121558Srgrimes	if (isdigit(*name) || *name == '-')
19131558Srgrimes		pw = getpwuid(atoi(name));
19141558Srgrimes	else
19151558Srgrimes		pw = getpwnam(name);
19161558Srgrimes	/*
19171558Srgrimes	 * Credentials specified as those of a user.
19181558Srgrimes	 */
19191558Srgrimes	if (names == NULL) {
19201558Srgrimes		if (pw == NULL) {
19211558Srgrimes			syslog(LOG_ERR, "Unknown user: %s", name);
19221558Srgrimes			return;
19231558Srgrimes		}
19241558Srgrimes		cr->cr_uid = pw->pw_uid;
19251558Srgrimes		ngroups = NGROUPS + 1;
19261558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
19271558Srgrimes			syslog(LOG_ERR, "Too many groups");
19281558Srgrimes		/*
19291558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
19301558Srgrimes		 */
19311558Srgrimes		cr->cr_ngroups = ngroups - 1;
19321558Srgrimes		cr->cr_groups[0] = groups[0];
19331558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
19341558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
19351558Srgrimes		return;
19361558Srgrimes	}
19371558Srgrimes	/*
19381558Srgrimes	 * Explicit credential specified as a colon separated list:
19391558Srgrimes	 *	uid:gid:gid:...
19401558Srgrimes	 */
19411558Srgrimes	if (pw != NULL)
19421558Srgrimes		cr->cr_uid = pw->pw_uid;
19431558Srgrimes	else if (isdigit(*name) || *name == '-')
19441558Srgrimes		cr->cr_uid = atoi(name);
19451558Srgrimes	else {
19461558Srgrimes		syslog(LOG_ERR, "Unknown user: %s", name);
19471558Srgrimes		return;
19481558Srgrimes	}
19491558Srgrimes	cr->cr_ngroups = 0;
19501558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
19511558Srgrimes		name = strsep(&names, ":");
19521558Srgrimes		if (isdigit(*name) || *name == '-') {
19531558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
19541558Srgrimes		} else {
19551558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
19561558Srgrimes				syslog(LOG_ERR, "Unknown group: %s", name);
19571558Srgrimes				continue;
19581558Srgrimes			}
19591558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
19601558Srgrimes		}
19611558Srgrimes	}
19621558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
19631558Srgrimes		syslog(LOG_ERR, "Too many groups");
19641558Srgrimes}
19651558Srgrimes
19661558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
19671558Srgrimes/*
19681558Srgrimes * Routines that maintain the remote mounttab
19691558Srgrimes */
19701558Srgrimesvoid
19711558Srgrimesget_mountlist()
19721558Srgrimes{
19731558Srgrimes	struct mountlist *mlp, **mlpp;
197423681Speter	char *host, *dirp, *cp;
19751558Srgrimes	int len;
19761558Srgrimes	char str[STRSIZ];
19771558Srgrimes	FILE *mlfile;
19781558Srgrimes
19791558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
19801558Srgrimes		syslog(LOG_ERR, "Can't open %s", _PATH_RMOUNTLIST);
19811558Srgrimes		return;
19821558Srgrimes	}
19831558Srgrimes	mlpp = &mlhead;
19841558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
198523681Speter		cp = str;
198623681Speter		host = strsep(&cp, " \t\n");
198723681Speter		dirp = strsep(&cp, " \t\n");
198823681Speter		if (host == NULL || dirp == NULL)
19891558Srgrimes			continue;
19901558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
199123681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
199223681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
199323681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
199423681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
19951558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
19961558Srgrimes		*mlpp = mlp;
19971558Srgrimes		mlpp = &mlp->ml_next;
19981558Srgrimes	}
19991558Srgrimes	fclose(mlfile);
20001558Srgrimes}
20011558Srgrimes
20021558Srgrimesvoid
20031558Srgrimesdel_mlist(hostp, dirp)
20041558Srgrimes	char *hostp, *dirp;
20051558Srgrimes{
20061558Srgrimes	struct mountlist *mlp, **mlpp;
20071558Srgrimes	struct mountlist *mlp2;
20081558Srgrimes	FILE *mlfile;
20091558Srgrimes	int fnd = 0;
20101558Srgrimes
20111558Srgrimes	mlpp = &mlhead;
20121558Srgrimes	mlp = mlhead;
20131558Srgrimes	while (mlp) {
20141558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
20151558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
20161558Srgrimes			fnd = 1;
20171558Srgrimes			mlp2 = mlp;
20181558Srgrimes			*mlpp = mlp = mlp->ml_next;
20191558Srgrimes			free((caddr_t)mlp2);
20201558Srgrimes		} else {
20211558Srgrimes			mlpp = &mlp->ml_next;
20221558Srgrimes			mlp = mlp->ml_next;
20231558Srgrimes		}
20241558Srgrimes	}
20251558Srgrimes	if (fnd) {
20261558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
20271558Srgrimes			syslog(LOG_ERR,"Can't update %s", _PATH_RMOUNTLIST);
20281558Srgrimes			return;
20291558Srgrimes		}
20301558Srgrimes		mlp = mlhead;
20311558Srgrimes		while (mlp) {
20321558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20331558Srgrimes			mlp = mlp->ml_next;
20341558Srgrimes		}
20351558Srgrimes		fclose(mlfile);
20361558Srgrimes	}
20371558Srgrimes}
20381558Srgrimes
20391558Srgrimesvoid
20401558Srgrimesadd_mlist(hostp, dirp)
20411558Srgrimes	char *hostp, *dirp;
20421558Srgrimes{
20431558Srgrimes	struct mountlist *mlp, **mlpp;
20441558Srgrimes	FILE *mlfile;
20451558Srgrimes
20461558Srgrimes	mlpp = &mlhead;
20471558Srgrimes	mlp = mlhead;
20481558Srgrimes	while (mlp) {
20491558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
20501558Srgrimes			return;
20511558Srgrimes		mlpp = &mlp->ml_next;
20521558Srgrimes		mlp = mlp->ml_next;
20531558Srgrimes	}
20541558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
20551558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
20561558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
20571558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
20581558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
20591558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
20601558Srgrimes	*mlpp = mlp;
20611558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
20621558Srgrimes		syslog(LOG_ERR, "Can't update %s", _PATH_RMOUNTLIST);
20631558Srgrimes		return;
20641558Srgrimes	}
20651558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20661558Srgrimes	fclose(mlfile);
20671558Srgrimes}
20681558Srgrimes
20691558Srgrimes/*
20701558Srgrimes * This function is called via. SIGTERM when the system is going down.
20711558Srgrimes * It sends a broadcast RPCMNT_UMNTALL.
20721558Srgrimes */
20731558Srgrimesvoid
20741558Srgrimessend_umntall()
20751558Srgrimes{
20761558Srgrimes	(void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
20771558Srgrimes		xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
20781558Srgrimes	exit(0);
20791558Srgrimes}
20801558Srgrimes
20811558Srgrimesint
20821558Srgrimesumntall_each(resultsp, raddr)
20831558Srgrimes	caddr_t resultsp;
20841558Srgrimes	struct sockaddr_in *raddr;
20851558Srgrimes{
20861558Srgrimes	return (1);
20871558Srgrimes}
20881558Srgrimes
20891558Srgrimes/*
20901558Srgrimes * Free up a group list.
20911558Srgrimes */
20921558Srgrimesvoid
20931558Srgrimesfree_grp(grp)
20941558Srgrimes	struct grouplist *grp;
20951558Srgrimes{
20961558Srgrimes	char **addrp;
20971558Srgrimes
20981558Srgrimes	if (grp->gr_type == GT_HOST) {
20991558Srgrimes		if (grp->gr_ptr.gt_hostent->h_name) {
21001558Srgrimes			addrp = grp->gr_ptr.gt_hostent->h_addr_list;
21011558Srgrimes			while (addrp && *addrp)
21021558Srgrimes				free(*addrp++);
21031558Srgrimes			free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
21041558Srgrimes			free(grp->gr_ptr.gt_hostent->h_name);
21051558Srgrimes		}
21061558Srgrimes		free((caddr_t)grp->gr_ptr.gt_hostent);
21071558Srgrimes	} else if (grp->gr_type == GT_NET) {
21081558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
21091558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
21101558Srgrimes	}
21111558Srgrimes#ifdef ISO
21121558Srgrimes	else if (grp->gr_type == GT_ISO)
21131558Srgrimes		free((caddr_t)grp->gr_ptr.gt_isoaddr);
21141558Srgrimes#endif
21151558Srgrimes	free((caddr_t)grp);
21161558Srgrimes}
21171558Srgrimes
21181558Srgrimes#ifdef DEBUG
21191558Srgrimesvoid
21201558SrgrimesSYSLOG(int pri, const char *fmt, ...)
21211558Srgrimes{
21221558Srgrimes	va_list ap;
21231558Srgrimes
21241558Srgrimes	va_start(ap, fmt);
21251558Srgrimes	vfprintf(stderr, fmt, ap);
21261558Srgrimes	va_end(ap);
21271558Srgrimes}
21281558Srgrimes#endif /* DEBUG */
21291558Srgrimes
21301558Srgrimes/*
21311558Srgrimes * Check options for consistency.
21321558Srgrimes */
21331558Srgrimesint
21341558Srgrimescheck_options(dp)
21351558Srgrimes	struct dirlist *dp;
21361558Srgrimes{
21371558Srgrimes
21381558Srgrimes	if (dp == (struct dirlist *)NULL)
21391558Srgrimes	    return (1);
21401558Srgrimes	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
21411558Srgrimes	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
21421558Srgrimes	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
21431558Srgrimes	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
21441558Srgrimes	    return (1);
21451558Srgrimes	}
21461558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
21471558Srgrimes	    syslog(LOG_ERR, "-mask requires -net");
21481558Srgrimes	    return (1);
21491558Srgrimes	}
21501558Srgrimes	if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
21511558Srgrimes	    syslog(LOG_ERR, "-net and -iso mutually exclusive");
21521558Srgrimes	    return (1);
21531558Srgrimes	}
21541558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
21551558Srgrimes	    syslog(LOG_ERR, "-alldir has multiple directories");
21561558Srgrimes	    return (1);
21571558Srgrimes	}
21581558Srgrimes	return (0);
21591558Srgrimes}
21601558Srgrimes
21611558Srgrimes/*
21621558Srgrimes * Check an absolute directory path for any symbolic links. Return true
21631558Srgrimes * if no symbolic links are found.
21641558Srgrimes */
21651558Srgrimesint
21661558Srgrimescheck_dirpath(dirp)
21671558Srgrimes	char *dirp;
21681558Srgrimes{
21691558Srgrimes	char *cp;
21701558Srgrimes	int ret = 1;
21711558Srgrimes	struct stat sb;
21721558Srgrimes
21731558Srgrimes	cp = dirp + 1;
21741558Srgrimes	while (*cp && ret) {
21751558Srgrimes		if (*cp == '/') {
21761558Srgrimes			*cp = '\0';
21779336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21781558Srgrimes				ret = 0;
21791558Srgrimes			*cp = '/';
21801558Srgrimes		}
21811558Srgrimes		cp++;
21821558Srgrimes	}
21839336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21841558Srgrimes		ret = 0;
21851558Srgrimes	return (ret);
21861558Srgrimes}
21879336Sdfr
21889336Sdfr/*
21899336Sdfr * Just translate an ascii string to an integer.
21909336Sdfr */
21919336Sdfrint
21929336Sdfrget_num(cp)
21939336Sdfr	register char *cp;
21949336Sdfr{
21959336Sdfr	register int res = 0;
21969336Sdfr
21979336Sdfr	while (*cp) {
21989336Sdfr		if (*cp < '0' || *cp > '9')
21999336Sdfr			return (-1);
22009336Sdfr		res = res * 10 + (*cp++ - '0');
22019336Sdfr	}
22029336Sdfr	return (res);
22039336Sdfr}
2204