mountd.c revision 51968
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
3837663Scharnierstatic const 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
4437663Scharnier#if 0
4537663Scharnierstatic char sccsid[] = "@(#)mountd.c	8.15 (Berkeley) 5/1/95";
4637663Scharnier#endif
472999Swollmanstatic const char rcsid[] =
4850476Speter  "$FreeBSD: head/usr.sbin/mountd/mountd.c 51968 1999-10-06 18:20:44Z alfred $";
492999Swollman#endif /*not lint*/
501558Srgrimes
511558Srgrimes#include <sys/param.h>
521558Srgrimes#include <sys/mount.h>
531558Srgrimes#include <sys/stat.h>
541558Srgrimes#include <sys/syslog.h>
5524330Sguido#include <sys/sysctl.h>
561558Srgrimes
571558Srgrimes#include <rpc/rpc.h>
581558Srgrimes#include <rpc/pmap_clnt.h>
591558Srgrimes#ifdef ISO
601558Srgrimes#include <netiso/iso.h>
611558Srgrimes#endif
621558Srgrimes#include <nfs/rpcv2.h>
639336Sdfr#include <nfs/nfsproto.h>
6424330Sguido#include <nfs/nfs.h>
6523681Speter#include <ufs/ufs/ufsmount.h>
6623681Speter#include <msdosfs/msdosfsmount.h>
6723681Speter#include <isofs/cd9660/cd9660_mount.h>	/* XXX need isofs in include */
681558Srgrimes
691558Srgrimes#include <arpa/inet.h>
701558Srgrimes
711558Srgrimes#include <ctype.h>
7237663Scharnier#include <err.h>
731558Srgrimes#include <errno.h>
741558Srgrimes#include <grp.h>
751558Srgrimes#include <netdb.h>
761558Srgrimes#include <pwd.h>
771558Srgrimes#include <signal.h>
781558Srgrimes#include <stdio.h>
791558Srgrimes#include <stdlib.h>
801558Srgrimes#include <string.h>
811558Srgrimes#include <unistd.h>
821558Srgrimes#include "pathnames.h"
831558Srgrimes
841558Srgrimes#ifdef DEBUG
851558Srgrimes#include <stdarg.h>
861558Srgrimes#endif
871558Srgrimes
881558Srgrimes/*
891558Srgrimes * Structures for keeping the mount list and export list
901558Srgrimes */
911558Srgrimesstruct mountlist {
921558Srgrimes	struct mountlist *ml_next;
931558Srgrimes	char	ml_host[RPCMNT_NAMELEN+1];
941558Srgrimes	char	ml_dirp[RPCMNT_PATHLEN+1];
951558Srgrimes};
961558Srgrimes
971558Srgrimesstruct dirlist {
981558Srgrimes	struct dirlist	*dp_left;
991558Srgrimes	struct dirlist	*dp_right;
1001558Srgrimes	int		dp_flag;
1011558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
1021558Srgrimes	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1031558Srgrimes};
1041558Srgrimes/* dp_flag bits */
1051558Srgrimes#define	DP_DEFSET	0x1
1069336Sdfr#define DP_HOSTSET	0x2
1079336Sdfr#define DP_KERB		0x4
1081558Srgrimes
1091558Srgrimesstruct exportlist {
1101558Srgrimes	struct exportlist *ex_next;
1111558Srgrimes	struct dirlist	*ex_dirl;
1121558Srgrimes	struct dirlist	*ex_defdir;
1131558Srgrimes	int		ex_flag;
1141558Srgrimes	fsid_t		ex_fs;
1151558Srgrimes	char		*ex_fsdir;
11627447Sdfr	char		*ex_indexfile;
1171558Srgrimes};
1181558Srgrimes/* ex_flag bits */
1191558Srgrimes#define	EX_LINKED	0x1
1201558Srgrimes
1211558Srgrimesstruct netmsk {
12242144Sdfr	u_int32_t	nt_net;
12342144Sdfr	u_int32_t	nt_mask;
12442144Sdfr	char		*nt_name;
1251558Srgrimes};
1261558Srgrimes
1271558Srgrimesunion grouptypes {
1281558Srgrimes	struct hostent *gt_hostent;
1291558Srgrimes	struct netmsk	gt_net;
1301558Srgrimes#ifdef ISO
1311558Srgrimes	struct sockaddr_iso *gt_isoaddr;
1321558Srgrimes#endif
1331558Srgrimes};
1341558Srgrimes
1351558Srgrimesstruct grouplist {
1361558Srgrimes	int gr_type;
1371558Srgrimes	union grouptypes gr_ptr;
1381558Srgrimes	struct grouplist *gr_next;
1391558Srgrimes};
1401558Srgrimes/* Group types */
1411558Srgrimes#define	GT_NULL		0x0
1421558Srgrimes#define	GT_HOST		0x1
1431558Srgrimes#define	GT_NET		0x2
1441558Srgrimes#define	GT_ISO		0x4
1457401Swpaul#define GT_IGNORE	0x5
1461558Srgrimes
1471558Srgrimesstruct hostlist {
1489336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1491558Srgrimes	struct grouplist *ht_grp;
1501558Srgrimes	struct hostlist	 *ht_next;
1511558Srgrimes};
1521558Srgrimes
1539336Sdfrstruct fhreturn {
1549336Sdfr	int	fhr_flag;
1559336Sdfr	int	fhr_vers;
1569336Sdfr	nfsfh_t	fhr_fh;
1579336Sdfr};
1589336Sdfr
1591558Srgrimes/* Global defs */
1601558Srgrimeschar	*add_expdir __P((struct dirlist **, char *, int));
1611558Srgrimesvoid	add_dlist __P((struct dirlist **, struct dirlist *,
1629336Sdfr				struct grouplist *, int));
1631558Srgrimesvoid	add_mlist __P((char *, char *));
1641558Srgrimesint	check_dirpath __P((char *));
1651558Srgrimesint	check_options __P((struct dirlist *));
16642144Sdfrint	chk_host __P((struct dirlist *, u_int32_t, int *, int *));
1671558Srgrimesvoid	del_mlist __P((char *, char *));
1681558Srgrimesstruct dirlist *dirp_search __P((struct dirlist *, char *));
1691558Srgrimesint	do_mount __P((struct exportlist *, struct grouplist *, int,
1709336Sdfr		struct ucred *, char *, int, struct statfs *));
1711558Srgrimesint	do_opt __P((char **, char **, struct exportlist *, struct grouplist *,
1721558Srgrimes				int *, int *, struct ucred *));
1731558Srgrimesstruct	exportlist *ex_search __P((fsid_t *));
1741558Srgrimesstruct	exportlist *get_exp __P((void));
1751558Srgrimesvoid	free_dir __P((struct dirlist *));
1761558Srgrimesvoid	free_exp __P((struct exportlist *));
1771558Srgrimesvoid	free_grp __P((struct grouplist *));
1781558Srgrimesvoid	free_host __P((struct hostlist *));
1791558Srgrimesvoid	get_exportlist __P((void));
1807401Swpaulint	get_host __P((char *, struct grouplist *, struct grouplist *));
1819336Sdfrint	get_num __P((char *));
1821558Srgrimesstruct hostlist *get_ht __P((void));
1831558Srgrimesint	get_line __P((void));
1841558Srgrimesvoid	get_mountlist __P((void));
1851558Srgrimesint	get_net __P((char *, struct netmsk *, int));
1861558Srgrimesvoid	getexp_err __P((struct exportlist *, struct grouplist *));
1871558Srgrimesstruct grouplist *get_grp __P((void));
1881558Srgrimesvoid	hang_dirp __P((struct dirlist *, struct grouplist *,
1891558Srgrimes				struct exportlist *, int));
1901558Srgrimesvoid	mntsrv __P((struct svc_req *, SVCXPRT *));
1911558Srgrimesvoid	nextfield __P((char **, char **));
1921558Srgrimesvoid	out_of_mem __P((void));
1931558Srgrimesvoid	parsecred __P((char *, struct ucred *));
1941558Srgrimesint	put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
19542144Sdfrint	scan_tree __P((struct dirlist *, u_int32_t));
1961558Srgrimesvoid	send_umntall __P((void));
1971558Srgrimesint	umntall_each __P((caddr_t, struct sockaddr_in *));
19837663Scharnierstatic void usage __P((void));
1991558Srgrimesint	xdr_dir __P((XDR *, char *));
2001558Srgrimesint	xdr_explist __P((XDR *, caddr_t));
2019336Sdfrint	xdr_fhs __P((XDR *, caddr_t));
2021558Srgrimesint	xdr_mlist __P((XDR *, caddr_t));
2031558Srgrimes
2041558Srgrimes/* C library */
2051558Srgrimesint	getnetgrent();
2061558Srgrimesvoid	endnetgrent();
2071558Srgrimesvoid	setnetgrent();
2081558Srgrimes
2091558Srgrimes#ifdef ISO
2101558Srgrimesstruct iso_addr *iso_addr();
2111558Srgrimes#endif
2121558Srgrimes
2131558Srgrimesstruct exportlist *exphead;
2141558Srgrimesstruct mountlist *mlhead;
2151558Srgrimesstruct grouplist *grphead;
2161558Srgrimeschar exname[MAXPATHLEN];
2171558Srgrimesstruct ucred def_anon = {
2181558Srgrimes	1,
2191558Srgrimes	(uid_t) -2,
2201558Srgrimes	1,
2211558Srgrimes	{ (gid_t) -2 }
2221558Srgrimes};
22325087Sdfrint force_v2 = 0;
2249336Sdfrint resvport_only = 1;
2259336Sdfrint dir_only = 1;
22631705Sguidoint log = 0;
2271558Srgrimesint opt_flags;
2281558Srgrimes/* Bits for above */
2291558Srgrimes#define	OP_MAPROOT	0x01
2301558Srgrimes#define	OP_MAPALL	0x02
2311558Srgrimes#define	OP_KERB		0x04
2321558Srgrimes#define	OP_MASK		0x08
2331558Srgrimes#define	OP_NET		0x10
2341558Srgrimes#define	OP_ISO		0x20
2351558Srgrimes#define	OP_ALLDIRS	0x40
2361558Srgrimes
2371558Srgrimes#ifdef DEBUG
2381558Srgrimesint debug = 1;
2391558Srgrimesvoid	SYSLOG __P((int, const char *, ...));
2401558Srgrimes#define syslog SYSLOG
2411558Srgrimes#else
2421558Srgrimesint debug = 0;
2431558Srgrimes#endif
2441558Srgrimes
2451558Srgrimes/*
2461558Srgrimes * Mountd server for NFS mount protocol as described in:
2471558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2481558Srgrimes * The optional arguments are the exports file name
2491558Srgrimes * default: _PATH_EXPORTS
2501558Srgrimes * and "-n" to allow nonroot mount.
2511558Srgrimes */
2521558Srgrimesint
2531558Srgrimesmain(argc, argv)
2541558Srgrimes	int argc;
2551558Srgrimes	char **argv;
2561558Srgrimes{
2579202Srgrimes	SVCXPRT *udptransp, *tcptransp;
25832656Sbde	int c, error, mib[3];
25923681Speter	struct vfsconf vfc;
2601558Srgrimes
26123681Speter	error = getvfsbyname("nfs", &vfc);
26223681Speter	if (error && vfsisloadable("nfs")) {
2632999Swollman		if(vfsload("nfs"))
2642999Swollman			err(1, "vfsload(nfs)");
2652999Swollman		endvfsent();	/* flush cache */
26623681Speter		error = getvfsbyname("nfs", &vfc);
2672999Swollman	}
26823681Speter	if (error)
2692999Swollman		errx(1, "NFS support is not available in the running kernel");
2702999Swollman
27131665Sguido	while ((c = getopt(argc, argv, "2dlnr")) != -1)
2721558Srgrimes		switch (c) {
27325087Sdfr		case '2':
27425087Sdfr			force_v2 = 1;
27525087Sdfr			break;
2769336Sdfr		case 'n':
2779336Sdfr			resvport_only = 0;
2789336Sdfr			break;
2799336Sdfr		case 'r':
2809336Sdfr			dir_only = 0;
2819336Sdfr			break;
2828688Sphk		case 'd':
2838688Sphk			debug = debug ? 0 : 1;
2848688Sphk			break;
28531656Sguido		case 'l':
28631656Sguido			log = 1;
28731656Sguido			break;
2881558Srgrimes		default:
28937663Scharnier			usage();
2901558Srgrimes		};
2911558Srgrimes	argc -= optind;
2921558Srgrimes	argv += optind;
2931558Srgrimes	grphead = (struct grouplist *)NULL;
2941558Srgrimes	exphead = (struct exportlist *)NULL;
2951558Srgrimes	mlhead = (struct mountlist *)NULL;
2961558Srgrimes	if (argc == 1) {
2971558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
2981558Srgrimes		exname[MAXPATHLEN-1] = '\0';
2991558Srgrimes	} else
3001558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3011558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3021558Srgrimes	if (debug)
30337663Scharnier		warnx("getting export list");
3041558Srgrimes	get_exportlist();
3051558Srgrimes	if (debug)
30637663Scharnier		warnx("getting mount list");
3071558Srgrimes	get_mountlist();
3081558Srgrimes	if (debug)
30937663Scharnier		warnx("here we go");
3101558Srgrimes	if (debug == 0) {
3111558Srgrimes		daemon(0, 0);
3121558Srgrimes		signal(SIGINT, SIG_IGN);
3131558Srgrimes		signal(SIGQUIT, SIG_IGN);
3141558Srgrimes	}
3151558Srgrimes	signal(SIGHUP, (void (*) __P((int))) get_exportlist);
3161558Srgrimes	signal(SIGTERM, (void (*) __P((int))) send_umntall);
3171558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3181558Srgrimes	  if (pidfile != NULL) {
3191558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3201558Srgrimes		fclose(pidfile);
3211558Srgrimes	  }
3221558Srgrimes	}
32324759Sguido	if (!resvport_only) {
32424759Sguido		mib[0] = CTL_VFS;
32532656Sbde		mib[1] = vfc.vfc_typenum;
32624759Sguido		mib[2] = NFS_NFSPRIVPORT;
32724759Sguido		if (sysctl(mib, 3, NULL, NULL, &resvport_only,
32824759Sguido		    sizeof(resvport_only)) != 0 && errno != ENOENT) {
32924759Sguido			syslog(LOG_ERR, "sysctl: %m");
33024759Sguido			exit(1);
33124759Sguido		}
33224330Sguido	}
3339202Srgrimes	if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
3349202Srgrimes	    (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
33537663Scharnier		syslog(LOG_ERR, "can't create socket");
3361558Srgrimes		exit(1);
3371558Srgrimes	}
3389336Sdfr	pmap_unset(RPCPROG_MNT, 1);
3399336Sdfr	pmap_unset(RPCPROG_MNT, 3);
34025087Sdfr	if (!force_v2)
34125087Sdfr		if (!svc_register(udptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_UDP) ||
34225087Sdfr		    !svc_register(tcptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_TCP)) {
34337663Scharnier			syslog(LOG_ERR, "can't register mount");
34425087Sdfr			exit(1);
34525087Sdfr		}
3469336Sdfr	if (!svc_register(udptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_UDP) ||
34725087Sdfr	    !svc_register(tcptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_TCP)) {
34837663Scharnier		syslog(LOG_ERR, "can't register mount");
3491558Srgrimes		exit(1);
3501558Srgrimes	}
3511558Srgrimes	svc_run();
35237663Scharnier	syslog(LOG_ERR, "mountd died");
3531558Srgrimes	exit(1);
3541558Srgrimes}
3551558Srgrimes
35637663Scharnierstatic void
35737663Scharnierusage()
35837663Scharnier{
35937663Scharnier	fprintf(stderr,
36037663Scharnier		"usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
36137663Scharnier	exit(1);
36237663Scharnier}
36337663Scharnier
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;
37942144Sdfr	u_int32_t 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))
39437663Scharnier			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		 */
41651968Salfred		if (realpath(rpcpath, dirpath) == NULL ||
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,
42337663Scharnier			    "mount request from %s for non existent path %s",
42431656Sguido			    inet_ntoa(saddrin), dirpath);
4251558Srgrimes			if (debug)
42637663Scharnier				warnx("stat failed on %s", 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))
44237663Scharnier					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;
45537663Scharnier				syslog(LOG_ERR, "can't get fh for %s", dirpath);
4561558Srgrimes				if (!svc_sendreply(transp, xdr_long,
4571558Srgrimes				    (caddr_t)&bad))
45837663Scharnier					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))
46337663Scharnier				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)
47337663Scharnier				warnx("mount successful");
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))
48637663Scharnier			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))
49137663Scharnier			syslog(LOG_ERR, "can't send reply");
49231656Sguido		else if (log)
49331656Sguido			syslog(LOG_NOTICE,
49431656Sguido			    "dump request succeeded from %s",
49538023Sbde			    inet_ntoa(saddrin));
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		}
50551968Salfred		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
50631656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
50731656Sguido			    inet_ntoa(saddrin));
5081558Srgrimes			svcerr_decode(transp);
5091558Srgrimes			return;
5101558Srgrimes		}
51151968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
51251968Salfred			syslog(LOG_NOTICE, "umount request from %s "
51351968Salfred			    "for non existent path %s",
51451968Salfred			    inet_ntoa(saddrin), dirpath);
51551968Salfred		}
5161558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
51737663Scharnier			syslog(LOG_ERR, "can't send reply");
5181558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
5191558Srgrimes		if (hp)
5201558Srgrimes			del_mlist(hp->h_name, dirpath);
52131656Sguido		del_mlist(inet_ntoa(saddrin), dirpath);
52231656Sguido		if (log)
52331656Sguido			syslog(LOG_NOTICE,
52431656Sguido			    "umount request succeeded from %s for %s",
52531656Sguido			    inet_ntoa(saddrin), dirpath);
5261558Srgrimes		return;
5271558Srgrimes	case RPCMNT_UMNTALL:
5289336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
52931656Sguido			syslog(LOG_NOTICE,
53031656Sguido			    "umountall request from %s from unprivileged port",
53131656Sguido			    inet_ntoa(saddrin));
5321558Srgrimes			svcerr_weakauth(transp);
5331558Srgrimes			return;
5341558Srgrimes		}
5351558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
53637663Scharnier			syslog(LOG_ERR, "can't send reply");
5371558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
5381558Srgrimes		if (hp)
5391558Srgrimes			del_mlist(hp->h_name, (char *)NULL);
54031656Sguido		del_mlist(inet_ntoa(saddrin), (char *)NULL);
54131656Sguido		if (log)
54231656Sguido			syslog(LOG_NOTICE,
54331656Sguido			    "umountall request succeeded from %s",
54431656Sguido			    inet_ntoa(saddrin));
5451558Srgrimes		return;
5461558Srgrimes	case RPCMNT_EXPORT:
5471558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
54837663Scharnier			syslog(LOG_ERR, "can't send reply");
54931656Sguido		if (log)
55031656Sguido			syslog(LOG_NOTICE,
55131656Sguido			    "export request succeeded from %s",
55231656Sguido			    inet_ntoa(saddrin));
5531558Srgrimes		return;
5541558Srgrimes	default:
5551558Srgrimes		svcerr_noproc(transp);
5561558Srgrimes		return;
5571558Srgrimes	}
5581558Srgrimes}
5591558Srgrimes
5601558Srgrimes/*
5611558Srgrimes * Xdr conversion for a dirpath string
5621558Srgrimes */
5631558Srgrimesint
5641558Srgrimesxdr_dir(xdrsp, dirp)
5651558Srgrimes	XDR *xdrsp;
5661558Srgrimes	char *dirp;
5671558Srgrimes{
5681558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
5691558Srgrimes}
5701558Srgrimes
5711558Srgrimes/*
5729336Sdfr * Xdr routine to generate file handle reply
5731558Srgrimes */
5741558Srgrimesint
5759336Sdfrxdr_fhs(xdrsp, cp)
5761558Srgrimes	XDR *xdrsp;
5779336Sdfr	caddr_t cp;
5781558Srgrimes{
5799336Sdfr	register struct fhreturn *fhrp = (struct fhreturn *)cp;
5809336Sdfr	u_long ok = 0, len, auth;
5811558Srgrimes
5821558Srgrimes	if (!xdr_long(xdrsp, &ok))
5831558Srgrimes		return (0);
5849336Sdfr	switch (fhrp->fhr_vers) {
5859336Sdfr	case 1:
5869336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
5879336Sdfr	case 3:
5889336Sdfr		len = NFSX_V3FH;
5899336Sdfr		if (!xdr_long(xdrsp, &len))
5909336Sdfr			return (0);
5919336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
5929336Sdfr			return (0);
5939336Sdfr		if (fhrp->fhr_flag & DP_KERB)
5949336Sdfr			auth = RPCAUTH_KERB4;
5959336Sdfr		else
5969336Sdfr			auth = RPCAUTH_UNIX;
5979336Sdfr		len = 1;
5989336Sdfr		if (!xdr_long(xdrsp, &len))
5999336Sdfr			return (0);
6009336Sdfr		return (xdr_long(xdrsp, &auth));
6019336Sdfr	};
6029336Sdfr	return (0);
6031558Srgrimes}
6041558Srgrimes
6051558Srgrimesint
6061558Srgrimesxdr_mlist(xdrsp, cp)
6071558Srgrimes	XDR *xdrsp;
6081558Srgrimes	caddr_t cp;
6091558Srgrimes{
6101558Srgrimes	struct mountlist *mlp;
6111558Srgrimes	int true = 1;
6121558Srgrimes	int false = 0;
6131558Srgrimes	char *strp;
6141558Srgrimes
6151558Srgrimes	mlp = mlhead;
6161558Srgrimes	while (mlp) {
6171558Srgrimes		if (!xdr_bool(xdrsp, &true))
6181558Srgrimes			return (0);
6191558Srgrimes		strp = &mlp->ml_host[0];
6201558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
6211558Srgrimes			return (0);
6221558Srgrimes		strp = &mlp->ml_dirp[0];
6231558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6241558Srgrimes			return (0);
6251558Srgrimes		mlp = mlp->ml_next;
6261558Srgrimes	}
6271558Srgrimes	if (!xdr_bool(xdrsp, &false))
6281558Srgrimes		return (0);
6291558Srgrimes	return (1);
6301558Srgrimes}
6311558Srgrimes
6321558Srgrimes/*
6331558Srgrimes * Xdr conversion for export list
6341558Srgrimes */
6351558Srgrimesint
6361558Srgrimesxdr_explist(xdrsp, cp)
6371558Srgrimes	XDR *xdrsp;
6381558Srgrimes	caddr_t cp;
6391558Srgrimes{
6401558Srgrimes	struct exportlist *ep;
6411558Srgrimes	int false = 0;
6429336Sdfr	int putdef;
6439336Sdfr	sigset_t sighup_mask;
6441558Srgrimes
6459336Sdfr	sigemptyset(&sighup_mask);
6469336Sdfr	sigaddset(&sighup_mask, SIGHUP);
6479336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
6481558Srgrimes	ep = exphead;
6491558Srgrimes	while (ep) {
6501558Srgrimes		putdef = 0;
6511558Srgrimes		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
6521558Srgrimes			goto errout;
6531558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
6541558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
6551558Srgrimes			&putdef))
6561558Srgrimes			goto errout;
6571558Srgrimes		ep = ep->ex_next;
6581558Srgrimes	}
6599336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6601558Srgrimes	if (!xdr_bool(xdrsp, &false))
6611558Srgrimes		return (0);
6621558Srgrimes	return (1);
6631558Srgrimeserrout:
6649336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6651558Srgrimes	return (0);
6661558Srgrimes}
6671558Srgrimes
6681558Srgrimes/*
6691558Srgrimes * Called from xdr_explist() to traverse the tree and export the
6701558Srgrimes * directory paths.
6711558Srgrimes */
6721558Srgrimesint
6731558Srgrimesput_exlist(dp, xdrsp, adp, putdefp)
6741558Srgrimes	struct dirlist *dp;
6751558Srgrimes	XDR *xdrsp;
6761558Srgrimes	struct dirlist *adp;
6771558Srgrimes	int *putdefp;
6781558Srgrimes{
6791558Srgrimes	struct grouplist *grp;
6801558Srgrimes	struct hostlist *hp;
6811558Srgrimes	int true = 1;
6821558Srgrimes	int false = 0;
6831558Srgrimes	int gotalldir = 0;
6841558Srgrimes	char *strp;
6851558Srgrimes
6861558Srgrimes	if (dp) {
6871558Srgrimes		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
6881558Srgrimes			return (1);
6891558Srgrimes		if (!xdr_bool(xdrsp, &true))
6901558Srgrimes			return (1);
6911558Srgrimes		strp = dp->dp_dirp;
6921558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6931558Srgrimes			return (1);
6941558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
6951558Srgrimes			gotalldir = 1;
6961558Srgrimes			*putdefp = 1;
6971558Srgrimes		}
6981558Srgrimes		if ((dp->dp_flag & DP_DEFSET) == 0 &&
6991558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
7001558Srgrimes			hp = dp->dp_hosts;
7011558Srgrimes			while (hp) {
7021558Srgrimes				grp = hp->ht_grp;
7031558Srgrimes				if (grp->gr_type == GT_HOST) {
7041558Srgrimes					if (!xdr_bool(xdrsp, &true))
7051558Srgrimes						return (1);
7061558Srgrimes					strp = grp->gr_ptr.gt_hostent->h_name;
7078871Srgrimes					if (!xdr_string(xdrsp, &strp,
7081558Srgrimes					    RPCMNT_NAMELEN))
7091558Srgrimes						return (1);
7101558Srgrimes				} else if (grp->gr_type == GT_NET) {
7111558Srgrimes					if (!xdr_bool(xdrsp, &true))
7121558Srgrimes						return (1);
7131558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
7148871Srgrimes					if (!xdr_string(xdrsp, &strp,
7151558Srgrimes					    RPCMNT_NAMELEN))
7161558Srgrimes						return (1);
7171558Srgrimes				}
7181558Srgrimes				hp = hp->ht_next;
7191558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
7201558Srgrimes					hp = adp->dp_hosts;
7211558Srgrimes					gotalldir = 0;
7221558Srgrimes				}
7231558Srgrimes			}
7241558Srgrimes		}
7251558Srgrimes		if (!xdr_bool(xdrsp, &false))
7261558Srgrimes			return (1);
7271558Srgrimes		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
7281558Srgrimes			return (1);
7291558Srgrimes	}
7301558Srgrimes	return (0);
7311558Srgrimes}
7321558Srgrimes
7331558Srgrimes#define LINESIZ	10240
7341558Srgrimeschar line[LINESIZ];
7351558SrgrimesFILE *exp_file;
7361558Srgrimes
7371558Srgrimes/*
7381558Srgrimes * Get the export list
7391558Srgrimes */
7401558Srgrimesvoid
7411558Srgrimesget_exportlist()
7421558Srgrimes{
7431558Srgrimes	struct exportlist *ep, *ep2;
7441558Srgrimes	struct grouplist *grp, *tgrp;
7451558Srgrimes	struct exportlist **epp;
7461558Srgrimes	struct dirlist *dirhead;
7471558Srgrimes	struct statfs fsb, *fsp;
7481558Srgrimes	struct hostent *hpe;
7491558Srgrimes	struct ucred anon;
7501558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
7511558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
7521558Srgrimes
75351968Salfred	dirp = NULL;
75451968Salfred	dirplen = 0;
75551968Salfred
7561558Srgrimes	/*
7571558Srgrimes	 * First, get rid of the old list
7581558Srgrimes	 */
7591558Srgrimes	ep = exphead;
7601558Srgrimes	while (ep) {
7611558Srgrimes		ep2 = ep;
7621558Srgrimes		ep = ep->ex_next;
7631558Srgrimes		free_exp(ep2);
7641558Srgrimes	}
7651558Srgrimes	exphead = (struct exportlist *)NULL;
7661558Srgrimes
7671558Srgrimes	grp = grphead;
7681558Srgrimes	while (grp) {
7691558Srgrimes		tgrp = grp;
7701558Srgrimes		grp = grp->gr_next;
7711558Srgrimes		free_grp(tgrp);
7721558Srgrimes	}
7731558Srgrimes	grphead = (struct grouplist *)NULL;
7741558Srgrimes
7751558Srgrimes	/*
7761558Srgrimes	 * And delete exports that are in the kernel for all local
7771558Srgrimes	 * file systems.
7781558Srgrimes	 * XXX: Should know how to handle all local exportable file systems
77923681Speter	 *      instead of just "ufs".
7801558Srgrimes	 */
7811558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
7821558Srgrimes	for (i = 0; i < num; i++) {
7831558Srgrimes		union {
7841558Srgrimes			struct ufs_args ua;
7851558Srgrimes			struct iso_args ia;
7861558Srgrimes			struct mfs_args ma;
7879336Sdfr			struct msdosfs_args da;
7881558Srgrimes		} targs;
7891558Srgrimes
79023681Speter		if (!strcmp(fsp->f_fstypename, "mfs") ||
79123681Speter		    !strcmp(fsp->f_fstypename, "ufs") ||
79223681Speter		    !strcmp(fsp->f_fstypename, "msdos") ||
79323681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
7949336Sdfr			targs.ua.fspec = NULL;
7959336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
7969336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
7971558Srgrimes				  fsp->f_flags | MNT_UPDATE,
7981558Srgrimes				  (caddr_t)&targs) < 0)
79937663Scharnier				syslog(LOG_ERR, "can't delete exports for %s",
8001558Srgrimes				       fsp->f_mntonname);
8011558Srgrimes		}
8021558Srgrimes		fsp++;
8031558Srgrimes	}
8041558Srgrimes
8051558Srgrimes	/*
8061558Srgrimes	 * Read in the exports file and build the list, calling
8071558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
8081558Srgrimes	 */
8091558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
81037663Scharnier		syslog(LOG_ERR, "can't open %s", exname);
8111558Srgrimes		exit(2);
8121558Srgrimes	}
8131558Srgrimes	dirhead = (struct dirlist *)NULL;
8141558Srgrimes	while (get_line()) {
8151558Srgrimes		if (debug)
81637663Scharnier			warnx("got line %s", line);
8171558Srgrimes		cp = line;
8181558Srgrimes		nextfield(&cp, &endcp);
8191558Srgrimes		if (*cp == '#')
8201558Srgrimes			goto nextline;
8211558Srgrimes
8221558Srgrimes		/*
8231558Srgrimes		 * Set defaults.
8241558Srgrimes		 */
8251558Srgrimes		has_host = FALSE;
8261558Srgrimes		anon = def_anon;
8271558Srgrimes		exflags = MNT_EXPORTED;
8281558Srgrimes		got_nondir = 0;
8291558Srgrimes		opt_flags = 0;
8301558Srgrimes		ep = (struct exportlist *)NULL;
8311558Srgrimes
8321558Srgrimes		/*
8331558Srgrimes		 * Create new exports list entry
8341558Srgrimes		 */
8351558Srgrimes		len = endcp-cp;
8361558Srgrimes		tgrp = grp = get_grp();
8371558Srgrimes		while (len > 0) {
8381558Srgrimes			if (len > RPCMNT_NAMELEN) {
8391558Srgrimes			    getexp_err(ep, tgrp);
8401558Srgrimes			    goto nextline;
8411558Srgrimes			}
8421558Srgrimes			if (*cp == '-') {
8431558Srgrimes			    if (ep == (struct exportlist *)NULL) {
8441558Srgrimes				getexp_err(ep, tgrp);
8451558Srgrimes				goto nextline;
8461558Srgrimes			    }
8471558Srgrimes			    if (debug)
84837663Scharnier				warnx("doing opt %s", cp);
8491558Srgrimes			    got_nondir = 1;
8501558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
8511558Srgrimes				&exflags, &anon)) {
8521558Srgrimes				getexp_err(ep, tgrp);
8531558Srgrimes				goto nextline;
8541558Srgrimes			    }
8551558Srgrimes			} else if (*cp == '/') {
8561558Srgrimes			    savedc = *endcp;
8571558Srgrimes			    *endcp = '\0';
8581558Srgrimes			    if (check_dirpath(cp) &&
8591558Srgrimes				statfs(cp, &fsb) >= 0) {
8601558Srgrimes				if (got_nondir) {
86137663Scharnier				    syslog(LOG_ERR, "dirs must be first");
8621558Srgrimes				    getexp_err(ep, tgrp);
8631558Srgrimes				    goto nextline;
8641558Srgrimes				}
8651558Srgrimes				if (ep) {
8661558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
8671558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
8681558Srgrimes					getexp_err(ep, tgrp);
8691558Srgrimes					goto nextline;
8701558Srgrimes				    }
8711558Srgrimes				} else {
8721558Srgrimes				    /*
8731558Srgrimes				     * See if this directory is already
8741558Srgrimes				     * in the list.
8751558Srgrimes				     */
8761558Srgrimes				    ep = ex_search(&fsb.f_fsid);
8771558Srgrimes				    if (ep == (struct exportlist *)NULL) {
8781558Srgrimes					ep = get_exp();
8791558Srgrimes					ep->ex_fs = fsb.f_fsid;
8801558Srgrimes					ep->ex_fsdir = (char *)
8811558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
8821558Srgrimes					if (ep->ex_fsdir)
8831558Srgrimes					    strcpy(ep->ex_fsdir,
8841558Srgrimes						fsb.f_mntonname);
8851558Srgrimes					else
8861558Srgrimes					    out_of_mem();
8871558Srgrimes					if (debug)
88837663Scharnier					  warnx("making new ep fs=0x%x,0x%x",
8891558Srgrimes					      fsb.f_fsid.val[0],
8901558Srgrimes					      fsb.f_fsid.val[1]);
8911558Srgrimes				    } else if (debug)
89237663Scharnier					warnx("found ep fs=0x%x,0x%x",
8931558Srgrimes					    fsb.f_fsid.val[0],
8941558Srgrimes					    fsb.f_fsid.val[1]);
8951558Srgrimes				}
8961558Srgrimes
8971558Srgrimes				/*
8981558Srgrimes				 * Add dirpath to export mount point.
8991558Srgrimes				 */
9001558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
9011558Srgrimes				dirplen = len;
9021558Srgrimes			    } else {
9031558Srgrimes				getexp_err(ep, tgrp);
9041558Srgrimes				goto nextline;
9051558Srgrimes			    }
9061558Srgrimes			    *endcp = savedc;
9071558Srgrimes			} else {
9081558Srgrimes			    savedc = *endcp;
9091558Srgrimes			    *endcp = '\0';
9101558Srgrimes			    got_nondir = 1;
9111558Srgrimes			    if (ep == (struct exportlist *)NULL) {
9121558Srgrimes				getexp_err(ep, tgrp);
9131558Srgrimes				goto nextline;
9141558Srgrimes			    }
9151558Srgrimes
9161558Srgrimes			    /*
9171558Srgrimes			     * Get the host or netgroup.
9181558Srgrimes			     */
9191558Srgrimes			    setnetgrent(cp);
9201558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
9211558Srgrimes			    do {
9221558Srgrimes				if (has_host) {
9231558Srgrimes				    grp->gr_next = get_grp();
9241558Srgrimes				    grp = grp->gr_next;
9251558Srgrimes				}
9261558Srgrimes				if (netgrp) {
92737003Sjoerg				    if (hst == 0) {
92837663Scharnier					syslog(LOG_ERR,
92937663Scharnier				"null hostname in netgroup %s, skipping", cp);
93037004Sjoerg					grp->gr_type = GT_IGNORE;
93137003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
93237663Scharnier					syslog(LOG_ERR,
93337663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
93429317Sjlemon					grp->gr_type = GT_IGNORE;
9351558Srgrimes				    }
9367401Swpaul				} else if (get_host(cp, grp, tgrp)) {
93737663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
93829317Sjlemon				    grp->gr_type = GT_IGNORE;
9391558Srgrimes				}
9401558Srgrimes				has_host = TRUE;
9411558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
9421558Srgrimes			    endnetgrent();
9431558Srgrimes			    *endcp = savedc;
9441558Srgrimes			}
9451558Srgrimes			cp = endcp;
9461558Srgrimes			nextfield(&cp, &endcp);
9471558Srgrimes			len = endcp - cp;
9481558Srgrimes		}
9491558Srgrimes		if (check_options(dirhead)) {
9501558Srgrimes			getexp_err(ep, tgrp);
9511558Srgrimes			goto nextline;
9521558Srgrimes		}
9531558Srgrimes		if (!has_host) {
9541558Srgrimes			grp->gr_type = GT_HOST;
9551558Srgrimes			if (debug)
95637663Scharnier				warnx("adding a default entry");
9571558Srgrimes			/* add a default group and make the grp list NULL */
9581558Srgrimes			hpe = (struct hostent *)malloc(sizeof(struct hostent));
9591558Srgrimes			if (hpe == (struct hostent *)NULL)
9601558Srgrimes				out_of_mem();
96112348Sjoerg			hpe->h_name = strdup("Default");
9621558Srgrimes			hpe->h_addrtype = AF_INET;
96342144Sdfr			hpe->h_length = sizeof (u_int32_t);
9641558Srgrimes			hpe->h_addr_list = (char **)NULL;
9651558Srgrimes			grp->gr_ptr.gt_hostent = hpe;
9661558Srgrimes
9671558Srgrimes		/*
9681558Srgrimes		 * Don't allow a network export coincide with a list of
9691558Srgrimes		 * host(s) on the same line.
9701558Srgrimes		 */
9711558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
9721558Srgrimes			getexp_err(ep, tgrp);
9731558Srgrimes			goto nextline;
97429317Sjlemon
97529317Sjlemon        	/*
97629317Sjlemon	         * If an export list was specified on this line, make sure
97729317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
97829317Sjlemon		 */
97929317Sjlemon		} else {
98029317Sjlemon			grp = tgrp;
98129317Sjlemon        		while (grp && grp->gr_type == GT_IGNORE)
98229317Sjlemon				grp = grp->gr_next;
98329317Sjlemon			if (! grp) {
98429317Sjlemon			    getexp_err(ep, tgrp);
98529317Sjlemon			    goto nextline;
98629317Sjlemon			}
9871558Srgrimes		}
9881558Srgrimes
9891558Srgrimes		/*
9901558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
9911558Srgrimes		 * After loop, tgrp points to the start of the list and
9921558Srgrimes		 * grp points to the last entry in the list.
9931558Srgrimes		 */
9941558Srgrimes		grp = tgrp;
9951558Srgrimes		do {
9961558Srgrimes		    if (do_mount(ep, grp, exflags, &anon, dirp,
9971558Srgrimes			dirplen, &fsb)) {
9981558Srgrimes			getexp_err(ep, tgrp);
9991558Srgrimes			goto nextline;
10001558Srgrimes		    }
10011558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
10021558Srgrimes
10031558Srgrimes		/*
10041558Srgrimes		 * Success. Update the data structures.
10051558Srgrimes		 */
10061558Srgrimes		if (has_host) {
10079336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
10081558Srgrimes			grp->gr_next = grphead;
10091558Srgrimes			grphead = tgrp;
10101558Srgrimes		} else {
10111558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
10129336Sdfr				opt_flags);
10131558Srgrimes			free_grp(grp);
10141558Srgrimes		}
10151558Srgrimes		dirhead = (struct dirlist *)NULL;
10161558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
10171558Srgrimes			ep2 = exphead;
10181558Srgrimes			epp = &exphead;
10191558Srgrimes
10201558Srgrimes			/*
10211558Srgrimes			 * Insert in the list in alphabetical order.
10221558Srgrimes			 */
10231558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
10241558Srgrimes				epp = &ep2->ex_next;
10251558Srgrimes				ep2 = ep2->ex_next;
10261558Srgrimes			}
10271558Srgrimes			if (ep2)
10281558Srgrimes				ep->ex_next = ep2;
10291558Srgrimes			*epp = ep;
10301558Srgrimes			ep->ex_flag |= EX_LINKED;
10311558Srgrimes		}
10321558Srgrimesnextline:
10331558Srgrimes		if (dirhead) {
10341558Srgrimes			free_dir(dirhead);
10351558Srgrimes			dirhead = (struct dirlist *)NULL;
10361558Srgrimes		}
10371558Srgrimes	}
10381558Srgrimes	fclose(exp_file);
10391558Srgrimes}
10401558Srgrimes
10411558Srgrimes/*
10421558Srgrimes * Allocate an export list element
10431558Srgrimes */
10441558Srgrimesstruct exportlist *
10451558Srgrimesget_exp()
10461558Srgrimes{
10471558Srgrimes	struct exportlist *ep;
10481558Srgrimes
10491558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
10501558Srgrimes	if (ep == (struct exportlist *)NULL)
10511558Srgrimes		out_of_mem();
105223681Speter	memset(ep, 0, sizeof(struct exportlist));
10531558Srgrimes	return (ep);
10541558Srgrimes}
10551558Srgrimes
10561558Srgrimes/*
10571558Srgrimes * Allocate a group list element
10581558Srgrimes */
10591558Srgrimesstruct grouplist *
10601558Srgrimesget_grp()
10611558Srgrimes{
10621558Srgrimes	struct grouplist *gp;
10631558Srgrimes
10641558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
10651558Srgrimes	if (gp == (struct grouplist *)NULL)
10661558Srgrimes		out_of_mem();
106723681Speter	memset(gp, 0, sizeof(struct grouplist));
10681558Srgrimes	return (gp);
10691558Srgrimes}
10701558Srgrimes
10711558Srgrimes/*
10721558Srgrimes * Clean up upon an error in get_exportlist().
10731558Srgrimes */
10741558Srgrimesvoid
10751558Srgrimesgetexp_err(ep, grp)
10761558Srgrimes	struct exportlist *ep;
10771558Srgrimes	struct grouplist *grp;
10781558Srgrimes{
10791558Srgrimes	struct grouplist *tgrp;
10801558Srgrimes
108137663Scharnier	syslog(LOG_ERR, "bad exports list line %s", line);
10821558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
10831558Srgrimes		free_exp(ep);
10841558Srgrimes	while (grp) {
10851558Srgrimes		tgrp = grp;
10861558Srgrimes		grp = grp->gr_next;
10871558Srgrimes		free_grp(tgrp);
10881558Srgrimes	}
10891558Srgrimes}
10901558Srgrimes
10911558Srgrimes/*
10921558Srgrimes * Search the export list for a matching fs.
10931558Srgrimes */
10941558Srgrimesstruct exportlist *
10951558Srgrimesex_search(fsid)
10961558Srgrimes	fsid_t *fsid;
10971558Srgrimes{
10981558Srgrimes	struct exportlist *ep;
10991558Srgrimes
11001558Srgrimes	ep = exphead;
11011558Srgrimes	while (ep) {
11021558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
11031558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
11041558Srgrimes			return (ep);
11051558Srgrimes		ep = ep->ex_next;
11061558Srgrimes	}
11071558Srgrimes	return (ep);
11081558Srgrimes}
11091558Srgrimes
11101558Srgrimes/*
11111558Srgrimes * Add a directory path to the list.
11121558Srgrimes */
11131558Srgrimeschar *
11141558Srgrimesadd_expdir(dpp, cp, len)
11151558Srgrimes	struct dirlist **dpp;
11161558Srgrimes	char *cp;
11171558Srgrimes	int len;
11181558Srgrimes{
11191558Srgrimes	struct dirlist *dp;
11201558Srgrimes
11211558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
112237663Scharnier	if (dp == (struct dirlist *)NULL)
112337663Scharnier		out_of_mem();
11241558Srgrimes	dp->dp_left = *dpp;
11251558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
11261558Srgrimes	dp->dp_flag = 0;
11271558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
11281558Srgrimes	strcpy(dp->dp_dirp, cp);
11291558Srgrimes	*dpp = dp;
11301558Srgrimes	return (dp->dp_dirp);
11311558Srgrimes}
11321558Srgrimes
11331558Srgrimes/*
11341558Srgrimes * Hang the dir list element off the dirpath binary tree as required
11351558Srgrimes * and update the entry for host.
11361558Srgrimes */
11371558Srgrimesvoid
11389336Sdfrhang_dirp(dp, grp, ep, flags)
11391558Srgrimes	struct dirlist *dp;
11401558Srgrimes	struct grouplist *grp;
11411558Srgrimes	struct exportlist *ep;
11429336Sdfr	int flags;
11431558Srgrimes{
11441558Srgrimes	struct hostlist *hp;
11451558Srgrimes	struct dirlist *dp2;
11461558Srgrimes
11479336Sdfr	if (flags & OP_ALLDIRS) {
11481558Srgrimes		if (ep->ex_defdir)
11491558Srgrimes			free((caddr_t)dp);
11501558Srgrimes		else
11511558Srgrimes			ep->ex_defdir = dp;
11529336Sdfr		if (grp == (struct grouplist *)NULL) {
11531558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
11549336Sdfr			if (flags & OP_KERB)
11559336Sdfr				ep->ex_defdir->dp_flag |= DP_KERB;
11569336Sdfr		} else while (grp) {
11571558Srgrimes			hp = get_ht();
11589336Sdfr			if (flags & OP_KERB)
11599336Sdfr				hp->ht_flag |= DP_KERB;
11601558Srgrimes			hp->ht_grp = grp;
11611558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
11621558Srgrimes			ep->ex_defdir->dp_hosts = hp;
11631558Srgrimes			grp = grp->gr_next;
11641558Srgrimes		}
11651558Srgrimes	} else {
11661558Srgrimes
11671558Srgrimes		/*
116837663Scharnier		 * Loop through the directories adding them to the tree.
11691558Srgrimes		 */
11701558Srgrimes		while (dp) {
11711558Srgrimes			dp2 = dp->dp_left;
11729336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
11731558Srgrimes			dp = dp2;
11741558Srgrimes		}
11751558Srgrimes	}
11761558Srgrimes}
11771558Srgrimes
11781558Srgrimes/*
11791558Srgrimes * Traverse the binary tree either updating a node that is already there
11801558Srgrimes * for the new directory or adding the new node.
11811558Srgrimes */
11821558Srgrimesvoid
11839336Sdfradd_dlist(dpp, newdp, grp, flags)
11841558Srgrimes	struct dirlist **dpp;
11851558Srgrimes	struct dirlist *newdp;
11861558Srgrimes	struct grouplist *grp;
11879336Sdfr	int flags;
11881558Srgrimes{
11891558Srgrimes	struct dirlist *dp;
11901558Srgrimes	struct hostlist *hp;
11911558Srgrimes	int cmp;
11921558Srgrimes
11931558Srgrimes	dp = *dpp;
11941558Srgrimes	if (dp) {
11951558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
11961558Srgrimes		if (cmp > 0) {
11979336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
11981558Srgrimes			return;
11991558Srgrimes		} else if (cmp < 0) {
12009336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
12011558Srgrimes			return;
12021558Srgrimes		} else
12031558Srgrimes			free((caddr_t)newdp);
12041558Srgrimes	} else {
12051558Srgrimes		dp = newdp;
12061558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
12071558Srgrimes		*dpp = dp;
12081558Srgrimes	}
12091558Srgrimes	if (grp) {
12101558Srgrimes
12111558Srgrimes		/*
12121558Srgrimes		 * Hang all of the host(s) off of the directory point.
12131558Srgrimes		 */
12141558Srgrimes		do {
12151558Srgrimes			hp = get_ht();
12169336Sdfr			if (flags & OP_KERB)
12179336Sdfr				hp->ht_flag |= DP_KERB;
12181558Srgrimes			hp->ht_grp = grp;
12191558Srgrimes			hp->ht_next = dp->dp_hosts;
12201558Srgrimes			dp->dp_hosts = hp;
12211558Srgrimes			grp = grp->gr_next;
12221558Srgrimes		} while (grp);
12239336Sdfr	} else {
12241558Srgrimes		dp->dp_flag |= DP_DEFSET;
12259336Sdfr		if (flags & OP_KERB)
12269336Sdfr			dp->dp_flag |= DP_KERB;
12279336Sdfr	}
12281558Srgrimes}
12291558Srgrimes
12301558Srgrimes/*
12311558Srgrimes * Search for a dirpath on the export point.
12321558Srgrimes */
12331558Srgrimesstruct dirlist *
12341558Srgrimesdirp_search(dp, dirpath)
12351558Srgrimes	struct dirlist *dp;
12361558Srgrimes	char *dirpath;
12371558Srgrimes{
12381558Srgrimes	int cmp;
12391558Srgrimes
12401558Srgrimes	if (dp) {
12411558Srgrimes		cmp = strcmp(dp->dp_dirp, dirpath);
12421558Srgrimes		if (cmp > 0)
12431558Srgrimes			return (dirp_search(dp->dp_left, dirpath));
12441558Srgrimes		else if (cmp < 0)
12451558Srgrimes			return (dirp_search(dp->dp_right, dirpath));
12461558Srgrimes		else
12471558Srgrimes			return (dp);
12481558Srgrimes	}
12491558Srgrimes	return (dp);
12501558Srgrimes}
12511558Srgrimes
12521558Srgrimes/*
12531558Srgrimes * Scan for a host match in a directory tree.
12541558Srgrimes */
12551558Srgrimesint
12569336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
12571558Srgrimes	struct dirlist *dp;
125842144Sdfr	u_int32_t saddr;
12591558Srgrimes	int *defsetp;
12609336Sdfr	int *hostsetp;
12611558Srgrimes{
12621558Srgrimes	struct hostlist *hp;
12631558Srgrimes	struct grouplist *grp;
126442144Sdfr	u_int32_t **addrp;
12651558Srgrimes
12661558Srgrimes	if (dp) {
12671558Srgrimes		if (dp->dp_flag & DP_DEFSET)
12689336Sdfr			*defsetp = dp->dp_flag;
12691558Srgrimes		hp = dp->dp_hosts;
12701558Srgrimes		while (hp) {
12711558Srgrimes			grp = hp->ht_grp;
12721558Srgrimes			switch (grp->gr_type) {
12731558Srgrimes			case GT_HOST:
127442144Sdfr			    addrp = (u_int32_t **)
12751558Srgrimes				grp->gr_ptr.gt_hostent->h_addr_list;
12761558Srgrimes			    while (*addrp) {
12779336Sdfr				if (**addrp == saddr) {
12789336Sdfr				    *hostsetp = (hp->ht_flag | DP_HOSTSET);
12791558Srgrimes				    return (1);
12809336Sdfr				}
12811558Srgrimes				addrp++;
12821558Srgrimes			    }
12831558Srgrimes			    break;
12841558Srgrimes			case GT_NET:
12851558Srgrimes			    if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
12869336Sdfr				grp->gr_ptr.gt_net.nt_net) {
12879336Sdfr				*hostsetp = (hp->ht_flag | DP_HOSTSET);
12881558Srgrimes				return (1);
12899336Sdfr			    }
12901558Srgrimes			    break;
12911558Srgrimes			};
12921558Srgrimes			hp = hp->ht_next;
12931558Srgrimes		}
12941558Srgrimes	}
12951558Srgrimes	return (0);
12961558Srgrimes}
12971558Srgrimes
12981558Srgrimes/*
12991558Srgrimes * Scan tree for a host that matches the address.
13001558Srgrimes */
13011558Srgrimesint
13021558Srgrimesscan_tree(dp, saddr)
13031558Srgrimes	struct dirlist *dp;
130442144Sdfr	u_int32_t saddr;
13051558Srgrimes{
13069336Sdfr	int defset, hostset;
13071558Srgrimes
13081558Srgrimes	if (dp) {
13091558Srgrimes		if (scan_tree(dp->dp_left, saddr))
13101558Srgrimes			return (1);
13119336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
13121558Srgrimes			return (1);
13131558Srgrimes		if (scan_tree(dp->dp_right, saddr))
13141558Srgrimes			return (1);
13151558Srgrimes	}
13161558Srgrimes	return (0);
13171558Srgrimes}
13181558Srgrimes
13191558Srgrimes/*
13201558Srgrimes * Traverse the dirlist tree and free it up.
13211558Srgrimes */
13221558Srgrimesvoid
13231558Srgrimesfree_dir(dp)
13241558Srgrimes	struct dirlist *dp;
13251558Srgrimes{
13261558Srgrimes
13271558Srgrimes	if (dp) {
13281558Srgrimes		free_dir(dp->dp_left);
13291558Srgrimes		free_dir(dp->dp_right);
13301558Srgrimes		free_host(dp->dp_hosts);
13311558Srgrimes		free((caddr_t)dp);
13321558Srgrimes	}
13331558Srgrimes}
13341558Srgrimes
13351558Srgrimes/*
13361558Srgrimes * Parse the option string and update fields.
13371558Srgrimes * Option arguments may either be -<option>=<value> or
13381558Srgrimes * -<option> <value>
13391558Srgrimes */
13401558Srgrimesint
13411558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
13421558Srgrimes	char **cpp, **endcpp;
13431558Srgrimes	struct exportlist *ep;
13441558Srgrimes	struct grouplist *grp;
13451558Srgrimes	int *has_hostp;
13461558Srgrimes	int *exflagsp;
13471558Srgrimes	struct ucred *cr;
13481558Srgrimes{
13491558Srgrimes	char *cpoptarg, *cpoptend;
13501558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
13511558Srgrimes	int allflag, usedarg;
13521558Srgrimes
135351968Salfred	savedc2 = '\0';
13541558Srgrimes	cpopt = *cpp;
13551558Srgrimes	cpopt++;
13561558Srgrimes	cp = *endcpp;
13571558Srgrimes	savedc = *cp;
13581558Srgrimes	*cp = '\0';
13591558Srgrimes	while (cpopt && *cpopt) {
13601558Srgrimes		allflag = 1;
13611558Srgrimes		usedarg = -2;
136237663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
13631558Srgrimes			*cpoptend++ = '\0';
136437663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
13651558Srgrimes				*cpoptarg++ = '\0';
13661558Srgrimes		} else {
136737663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
13681558Srgrimes				*cpoptarg++ = '\0';
13691558Srgrimes			else {
13701558Srgrimes				*cp = savedc;
13711558Srgrimes				nextfield(&cp, &endcp);
13721558Srgrimes				**endcpp = '\0';
13731558Srgrimes				if (endcp > cp && *cp != '-') {
13741558Srgrimes					cpoptarg = cp;
13751558Srgrimes					savedc2 = *endcp;
13761558Srgrimes					*endcp = '\0';
13771558Srgrimes					usedarg = 0;
13781558Srgrimes				}
13791558Srgrimes			}
13801558Srgrimes		}
13811558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
13821558Srgrimes			*exflagsp |= MNT_EXRDONLY;
13831558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
13841558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
13851558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
13861558Srgrimes			usedarg++;
13871558Srgrimes			parsecred(cpoptarg, cr);
13881558Srgrimes			if (allflag == 0) {
13891558Srgrimes				*exflagsp |= MNT_EXPORTANON;
13901558Srgrimes				opt_flags |= OP_MAPALL;
13911558Srgrimes			} else
13921558Srgrimes				opt_flags |= OP_MAPROOT;
13931558Srgrimes		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
13941558Srgrimes			*exflagsp |= MNT_EXKERB;
13951558Srgrimes			opt_flags |= OP_KERB;
13961558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
13971558Srgrimes			!strcmp(cpopt, "m"))) {
13981558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
139937663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
14001558Srgrimes				return (1);
14011558Srgrimes			}
14021558Srgrimes			usedarg++;
14031558Srgrimes			opt_flags |= OP_MASK;
14041558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
14051558Srgrimes			!strcmp(cpopt, "n"))) {
14061558Srgrimes			if (grp->gr_type != GT_NULL) {
140737663Scharnier				syslog(LOG_ERR, "network/host conflict");
14081558Srgrimes				return (1);
14091558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
141037663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
14111558Srgrimes				return (1);
14121558Srgrimes			}
14131558Srgrimes			grp->gr_type = GT_NET;
14141558Srgrimes			*has_hostp = 1;
14151558Srgrimes			usedarg++;
14161558Srgrimes			opt_flags |= OP_NET;
14171558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
14181558Srgrimes			opt_flags |= OP_ALLDIRS;
141927447Sdfr		} else if (!strcmp(cpopt, "public")) {
142027447Sdfr			*exflagsp |= MNT_EXPUBLIC;
142127447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
142227447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
142327447Sdfr			opt_flags |= OP_MAPALL;
142427447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
142527447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
14261558Srgrimes#ifdef ISO
14271558Srgrimes		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
14281558Srgrimes			if (get_isoaddr(cpoptarg, grp)) {
142937663Scharnier				syslog(LOG_ERR, "bad iso addr: %s", cpoptarg);
14301558Srgrimes				return (1);
14311558Srgrimes			}
14321558Srgrimes			*has_hostp = 1;
14331558Srgrimes			usedarg++;
14341558Srgrimes			opt_flags |= OP_ISO;
14351558Srgrimes#endif /* ISO */
14361558Srgrimes		} else {
143737663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
14381558Srgrimes			return (1);
14391558Srgrimes		}
14401558Srgrimes		if (usedarg >= 0) {
14411558Srgrimes			*endcp = savedc2;
14421558Srgrimes			**endcpp = savedc;
14431558Srgrimes			if (usedarg > 0) {
14441558Srgrimes				*cpp = cp;
14451558Srgrimes				*endcpp = endcp;
14461558Srgrimes			}
14471558Srgrimes			return (0);
14481558Srgrimes		}
14491558Srgrimes		cpopt = cpoptend;
14501558Srgrimes	}
14511558Srgrimes	**endcpp = savedc;
14521558Srgrimes	return (0);
14531558Srgrimes}
14541558Srgrimes
14551558Srgrimes/*
14561558Srgrimes * Translate a character string to the corresponding list of network
14571558Srgrimes * addresses for a hostname.
14581558Srgrimes */
14591558Srgrimesint
14607401Swpaulget_host(cp, grp, tgrp)
14611558Srgrimes	char *cp;
14621558Srgrimes	struct grouplist *grp;
14637401Swpaul	struct grouplist *tgrp;
14641558Srgrimes{
14657401Swpaul	struct grouplist *checkgrp;
14661558Srgrimes	struct hostent *hp, *nhp;
14671558Srgrimes	char **addrp, **naddrp;
14681558Srgrimes	struct hostent t_host;
14691558Srgrimes	int i;
147042144Sdfr	u_int32_t saddr;
14711558Srgrimes	char *aptr[2];
14721558Srgrimes
14731558Srgrimes	if (grp->gr_type != GT_NULL)
14741558Srgrimes		return (1);
14751558Srgrimes	if ((hp = gethostbyname(cp)) == NULL) {
14761558Srgrimes		if (isdigit(*cp)) {
14771558Srgrimes			saddr = inet_addr(cp);
14781558Srgrimes			if (saddr == -1) {
147937663Scharnier 				syslog(LOG_ERR, "inet_addr failed for %s", cp);
14801558Srgrimes				return (1);
14811558Srgrimes			}
14821558Srgrimes			if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
14831558Srgrimes				AF_INET)) == NULL) {
14841558Srgrimes				hp = &t_host;
14851558Srgrimes				hp->h_name = cp;
14861558Srgrimes				hp->h_addrtype = AF_INET;
148742144Sdfr				hp->h_length = sizeof (u_int32_t);
14881558Srgrimes				hp->h_addr_list = aptr;
14891558Srgrimes				aptr[0] = (char *)&saddr;
14901558Srgrimes				aptr[1] = (char *)NULL;
14911558Srgrimes			}
14921558Srgrimes		} else {
149337663Scharnier 			syslog(LOG_ERR, "gethostbyname failed for %s", cp);
14941558Srgrimes			return (1);
14951558Srgrimes		}
14961558Srgrimes	}
14977401Swpaul        /*
14987401Swpaul         * Sanity check: make sure we don't already have an entry
14997401Swpaul         * for this host in the grouplist.
15007401Swpaul         */
15017401Swpaul        checkgrp = tgrp;
150237157Swpaul        while (checkgrp != NULL) {
150317887Swpaul		if (checkgrp->gr_type == GT_HOST &&
150417887Swpaul                    checkgrp->gr_ptr.gt_hostent != NULL &&
150537157Swpaul                    (!strcmp(checkgrp->gr_ptr.gt_hostent->h_name, hp->h_name)
150642144Sdfr		|| *(u_int32_t *)checkgrp->gr_ptr.gt_hostent->h_addr ==
150742144Sdfr			*(u_int32_t *)hp->h_addr)) {
15087401Swpaul                        grp->gr_type = GT_IGNORE;
15097401Swpaul			return(0);
15107401Swpaul		}
15117401Swpaul                checkgrp = checkgrp->gr_next;
15127401Swpaul        }
15137401Swpaul
15141558Srgrimes	grp->gr_type = GT_HOST;
15151558Srgrimes	nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
15161558Srgrimes		malloc(sizeof(struct hostent));
15171558Srgrimes	if (nhp == (struct hostent *)NULL)
15181558Srgrimes		out_of_mem();
151923681Speter	memmove(nhp, hp, sizeof(struct hostent));
15201558Srgrimes	i = strlen(hp->h_name)+1;
15211558Srgrimes	nhp->h_name = (char *)malloc(i);
15221558Srgrimes	if (nhp->h_name == (char *)NULL)
15231558Srgrimes		out_of_mem();
152423681Speter	memmove(nhp->h_name, hp->h_name, i);
15251558Srgrimes	addrp = hp->h_addr_list;
15261558Srgrimes	i = 1;
15271558Srgrimes	while (*addrp++)
15281558Srgrimes		i++;
152937663Scharnier	naddrp = nhp->h_addr_list = (char **)malloc(i*sizeof(char *));
15301558Srgrimes	if (naddrp == (char **)NULL)
15311558Srgrimes		out_of_mem();
15321558Srgrimes	addrp = hp->h_addr_list;
15331558Srgrimes	while (*addrp) {
153437663Scharnier		*naddrp = (char *)malloc(hp->h_length);
15351558Srgrimes		if (*naddrp == (char *)NULL)
15361558Srgrimes		    out_of_mem();
153723681Speter		memmove(*naddrp, *addrp, hp->h_length);
15381558Srgrimes		addrp++;
15391558Srgrimes		naddrp++;
15401558Srgrimes	}
15411558Srgrimes	*naddrp = (char *)NULL;
15421558Srgrimes	if (debug)
154337663Scharnier		warnx("got host %s", hp->h_name);
15441558Srgrimes	return (0);
15451558Srgrimes}
15461558Srgrimes
15471558Srgrimes/*
15481558Srgrimes * Free up an exports list component
15491558Srgrimes */
15501558Srgrimesvoid
15511558Srgrimesfree_exp(ep)
15521558Srgrimes	struct exportlist *ep;
15531558Srgrimes{
15541558Srgrimes
15551558Srgrimes	if (ep->ex_defdir) {
15561558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
15571558Srgrimes		free((caddr_t)ep->ex_defdir);
15581558Srgrimes	}
15591558Srgrimes	if (ep->ex_fsdir)
15601558Srgrimes		free(ep->ex_fsdir);
156127447Sdfr	if (ep->ex_indexfile)
156227447Sdfr		free(ep->ex_indexfile);
15631558Srgrimes	free_dir(ep->ex_dirl);
15641558Srgrimes	free((caddr_t)ep);
15651558Srgrimes}
15661558Srgrimes
15671558Srgrimes/*
15681558Srgrimes * Free hosts.
15691558Srgrimes */
15701558Srgrimesvoid
15711558Srgrimesfree_host(hp)
15721558Srgrimes	struct hostlist *hp;
15731558Srgrimes{
15741558Srgrimes	struct hostlist *hp2;
15751558Srgrimes
15761558Srgrimes	while (hp) {
15771558Srgrimes		hp2 = hp;
15781558Srgrimes		hp = hp->ht_next;
15791558Srgrimes		free((caddr_t)hp2);
15801558Srgrimes	}
15811558Srgrimes}
15821558Srgrimes
15831558Srgrimesstruct hostlist *
15841558Srgrimesget_ht()
15851558Srgrimes{
15861558Srgrimes	struct hostlist *hp;
15871558Srgrimes
15881558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
15891558Srgrimes	if (hp == (struct hostlist *)NULL)
15901558Srgrimes		out_of_mem();
15911558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
15929336Sdfr	hp->ht_flag = 0;
15931558Srgrimes	return (hp);
15941558Srgrimes}
15951558Srgrimes
15961558Srgrimes#ifdef ISO
15971558Srgrimes/*
15981558Srgrimes * Translate an iso address.
15991558Srgrimes */
16001558Srgrimesget_isoaddr(cp, grp)
16011558Srgrimes	char *cp;
16021558Srgrimes	struct grouplist *grp;
16031558Srgrimes{
16041558Srgrimes	struct iso_addr *isop;
16051558Srgrimes	struct sockaddr_iso *isoaddr;
16061558Srgrimes
16071558Srgrimes	if (grp->gr_type != GT_NULL)
16081558Srgrimes		return (1);
16091558Srgrimes	if ((isop = iso_addr(cp)) == NULL) {
161037663Scharnier		syslog(LOG_ERR, "iso_addr failed, ignored");
16111558Srgrimes		return (1);
16121558Srgrimes	}
161337663Scharnier	isoaddr = (struct sockaddr_iso *)malloc(sizeof (struct sockaddr_iso));
16141558Srgrimes	if (isoaddr == (struct sockaddr_iso *)NULL)
16151558Srgrimes		out_of_mem();
161623681Speter	memset(isoaddr, 0, sizeof(struct sockaddr_iso));
161723681Speter	memmove(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
161823681Speter	isoaddr->siso_len = sizeof(struct sockaddr_iso);
16191558Srgrimes	isoaddr->siso_family = AF_ISO;
16201558Srgrimes	grp->gr_type = GT_ISO;
16211558Srgrimes	grp->gr_ptr.gt_isoaddr = isoaddr;
16221558Srgrimes	return (0);
16231558Srgrimes}
16241558Srgrimes#endif	/* ISO */
16251558Srgrimes
16261558Srgrimes/*
16271558Srgrimes * Out of memory, fatal
16281558Srgrimes */
16291558Srgrimesvoid
16301558Srgrimesout_of_mem()
16311558Srgrimes{
16321558Srgrimes
163337663Scharnier	syslog(LOG_ERR, "out of memory");
16341558Srgrimes	exit(2);
16351558Srgrimes}
16361558Srgrimes
16371558Srgrimes/*
16381558Srgrimes * Do the mount syscall with the update flag to push the export info into
16391558Srgrimes * the kernel.
16401558Srgrimes */
16411558Srgrimesint
16421558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
16431558Srgrimes	struct exportlist *ep;
16441558Srgrimes	struct grouplist *grp;
16451558Srgrimes	int exflags;
16461558Srgrimes	struct ucred *anoncrp;
16471558Srgrimes	char *dirp;
16481558Srgrimes	int dirplen;
16491558Srgrimes	struct statfs *fsb;
16501558Srgrimes{
16511558Srgrimes	char *cp = (char *)NULL;
165242144Sdfr	u_int32_t **addrp;
16531558Srgrimes	int done;
16541558Srgrimes	char savedc = '\0';
16551558Srgrimes	struct sockaddr_in sin, imask;
16561558Srgrimes	union {
16571558Srgrimes		struct ufs_args ua;
16581558Srgrimes		struct iso_args ia;
16591558Srgrimes		struct mfs_args ma;
16609336Sdfr#ifdef __NetBSD__
16619336Sdfr		struct msdosfs_args da;
16629336Sdfr#endif
16631558Srgrimes	} args;
166442144Sdfr	u_int32_t net;
16651558Srgrimes
16661558Srgrimes	args.ua.fspec = 0;
16671558Srgrimes	args.ua.export.ex_flags = exflags;
16681558Srgrimes	args.ua.export.ex_anon = *anoncrp;
166927447Sdfr	args.ua.export.ex_indexfile = ep->ex_indexfile;
167023681Speter	memset(&sin, 0, sizeof(sin));
167123681Speter	memset(&imask, 0, sizeof(imask));
16721558Srgrimes	sin.sin_family = AF_INET;
16731558Srgrimes	sin.sin_len = sizeof(sin);
16741558Srgrimes	imask.sin_family = AF_INET;
16751558Srgrimes	imask.sin_len = sizeof(sin);
16761558Srgrimes	if (grp->gr_type == GT_HOST)
167742144Sdfr		addrp = (u_int32_t **)grp->gr_ptr.gt_hostent->h_addr_list;
16781558Srgrimes	else
167942144Sdfr		addrp = (u_int32_t **)NULL;
16801558Srgrimes	done = FALSE;
16811558Srgrimes	while (!done) {
16821558Srgrimes		switch (grp->gr_type) {
16831558Srgrimes		case GT_HOST:
16841558Srgrimes			if (addrp) {
16851558Srgrimes				sin.sin_addr.s_addr = **addrp;
16861558Srgrimes				args.ua.export.ex_addrlen = sizeof(sin);
16871558Srgrimes			} else
16881558Srgrimes				args.ua.export.ex_addrlen = 0;
16891558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16901558Srgrimes			args.ua.export.ex_masklen = 0;
16911558Srgrimes			break;
16921558Srgrimes		case GT_NET:
16931558Srgrimes			if (grp->gr_ptr.gt_net.nt_mask)
16941558Srgrimes			    imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
16951558Srgrimes			else {
16961558Srgrimes			    net = ntohl(grp->gr_ptr.gt_net.nt_net);
16971558Srgrimes			    if (IN_CLASSA(net))
16981558Srgrimes				imask.sin_addr.s_addr = inet_addr("255.0.0.0");
16991558Srgrimes			    else if (IN_CLASSB(net))
17001558Srgrimes				imask.sin_addr.s_addr =
17011558Srgrimes				    inet_addr("255.255.0.0");
17021558Srgrimes			    else
17031558Srgrimes				imask.sin_addr.s_addr =
17041558Srgrimes				    inet_addr("255.255.255.0");
17051558Srgrimes			    grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
17061558Srgrimes			}
17071558Srgrimes			sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
17081558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
17091558Srgrimes			args.ua.export.ex_addrlen = sizeof (sin);
17101558Srgrimes			args.ua.export.ex_mask = (struct sockaddr *)&imask;
17111558Srgrimes			args.ua.export.ex_masklen = sizeof (imask);
17121558Srgrimes			break;
17131558Srgrimes#ifdef ISO
17141558Srgrimes		case GT_ISO:
17151558Srgrimes			args.ua.export.ex_addr =
17161558Srgrimes				(struct sockaddr *)grp->gr_ptr.gt_isoaddr;
17171558Srgrimes			args.ua.export.ex_addrlen =
17181558Srgrimes				sizeof(struct sockaddr_iso);
17191558Srgrimes			args.ua.export.ex_masklen = 0;
17201558Srgrimes			break;
17211558Srgrimes#endif	/* ISO */
17227401Swpaul		case GT_IGNORE:
17237401Swpaul			return(0);
17247401Swpaul			break;
17251558Srgrimes		default:
172637663Scharnier			syslog(LOG_ERR, "bad grouptype");
17271558Srgrimes			if (cp)
17281558Srgrimes				*cp = savedc;
17291558Srgrimes			return (1);
17301558Srgrimes		};
17311558Srgrimes
17321558Srgrimes		/*
17331558Srgrimes		 * XXX:
17341558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
17351558Srgrimes		 * of looping back up the dirp to the mount point??
17361558Srgrimes		 * Also, needs to know how to export all types of local
173723681Speter		 * exportable file systems and not just "ufs".
17381558Srgrimes		 */
17399336Sdfr		while (mount(fsb->f_fstypename, dirp,
17401558Srgrimes		       fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
17411558Srgrimes			if (cp)
17421558Srgrimes				*cp-- = savedc;
17431558Srgrimes			else
17441558Srgrimes				cp = dirp + dirplen - 1;
17451558Srgrimes			if (errno == EPERM) {
17461558Srgrimes				syslog(LOG_ERR,
174737663Scharnier				   "can't change attributes for %s", dirp);
17481558Srgrimes				return (1);
17491558Srgrimes			}
17501558Srgrimes			if (opt_flags & OP_ALLDIRS) {
175137663Scharnier				syslog(LOG_ERR, "could not remount %s: %m",
17524895Swollman					dirp);
17531558Srgrimes				return (1);
17541558Srgrimes			}
17551558Srgrimes			/* back up over the last component */
17561558Srgrimes			while (*cp == '/' && cp > dirp)
17571558Srgrimes				cp--;
17581558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
17591558Srgrimes				cp--;
17601558Srgrimes			if (cp == dirp) {
17611558Srgrimes				if (debug)
176237663Scharnier					warnx("mnt unsucc");
176337663Scharnier				syslog(LOG_ERR, "can't export %s", dirp);
17641558Srgrimes				return (1);
17651558Srgrimes			}
17661558Srgrimes			savedc = *cp;
17671558Srgrimes			*cp = '\0';
17681558Srgrimes		}
17691558Srgrimes		if (addrp) {
17701558Srgrimes			++addrp;
177142144Sdfr			if (*addrp == (u_int32_t *)NULL)
17721558Srgrimes				done = TRUE;
17731558Srgrimes		} else
17741558Srgrimes			done = TRUE;
17751558Srgrimes	}
17761558Srgrimes	if (cp)
17771558Srgrimes		*cp = savedc;
17781558Srgrimes	return (0);
17791558Srgrimes}
17801558Srgrimes
17811558Srgrimes/*
17821558Srgrimes * Translate a net address.
17831558Srgrimes */
17841558Srgrimesint
17851558Srgrimesget_net(cp, net, maskflg)
17861558Srgrimes	char *cp;
17871558Srgrimes	struct netmsk *net;
17881558Srgrimes	int maskflg;
17891558Srgrimes{
17901558Srgrimes	struct netent *np;
17911558Srgrimes	long netaddr;
17921558Srgrimes	struct in_addr inetaddr, inetaddr2;
17931558Srgrimes	char *name;
17941558Srgrimes
179525318Spst	if (isdigit(*cp) && ((netaddr = inet_network(cp)) != -1)) {
17961558Srgrimes		inetaddr = inet_makeaddr(netaddr, 0);
17971558Srgrimes		/*
179837663Scharnier		 * Due to arbitrary subnet masks, you don't know how many
17991558Srgrimes		 * bits to shift the address to make it into a network,
18001558Srgrimes		 * however you do know how to make a network address into
18011558Srgrimes		 * a host with host == 0 and then compare them.
18021558Srgrimes		 * (What a pest)
18031558Srgrimes		 */
18041558Srgrimes		if (!maskflg) {
18051558Srgrimes			setnetent(0);
180637663Scharnier			while ((np = getnetent())) {
18071558Srgrimes				inetaddr2 = inet_makeaddr(np->n_net, 0);
18081558Srgrimes				if (inetaddr2.s_addr == inetaddr.s_addr)
18091558Srgrimes					break;
18101558Srgrimes			}
18111558Srgrimes			endnetent();
18121558Srgrimes		}
181325318Spst	} else if ((np = getnetbyname(cp)) != NULL) {
181425318Spst		inetaddr = inet_makeaddr(np->n_net, 0);
18151558Srgrimes	} else
18161558Srgrimes		return (1);
181725318Spst
18181558Srgrimes	if (maskflg)
18191558Srgrimes		net->nt_mask = inetaddr.s_addr;
18201558Srgrimes	else {
18211558Srgrimes		if (np)
18221558Srgrimes			name = np->n_name;
18231558Srgrimes		else
18241558Srgrimes			name = inet_ntoa(inetaddr);
18251558Srgrimes		net->nt_name = (char *)malloc(strlen(name) + 1);
18261558Srgrimes		if (net->nt_name == (char *)NULL)
18271558Srgrimes			out_of_mem();
18281558Srgrimes		strcpy(net->nt_name, name);
18291558Srgrimes		net->nt_net = inetaddr.s_addr;
18301558Srgrimes	}
18311558Srgrimes	return (0);
18321558Srgrimes}
18331558Srgrimes
18341558Srgrimes/*
18351558Srgrimes * Parse out the next white space separated field
18361558Srgrimes */
18371558Srgrimesvoid
18381558Srgrimesnextfield(cp, endcp)
18391558Srgrimes	char **cp;
18401558Srgrimes	char **endcp;
18411558Srgrimes{
18421558Srgrimes	char *p;
18431558Srgrimes
18441558Srgrimes	p = *cp;
18451558Srgrimes	while (*p == ' ' || *p == '\t')
18461558Srgrimes		p++;
18471558Srgrimes	if (*p == '\n' || *p == '\0')
18481558Srgrimes		*cp = *endcp = p;
18491558Srgrimes	else {
18501558Srgrimes		*cp = p++;
18511558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
18521558Srgrimes			p++;
18531558Srgrimes		*endcp = p;
18541558Srgrimes	}
18551558Srgrimes}
18561558Srgrimes
18571558Srgrimes/*
18581558Srgrimes * Get an exports file line. Skip over blank lines and handle line
18591558Srgrimes * continuations.
18601558Srgrimes */
18611558Srgrimesint
18621558Srgrimesget_line()
18631558Srgrimes{
18641558Srgrimes	char *p, *cp;
18651558Srgrimes	int len;
18661558Srgrimes	int totlen, cont_line;
18671558Srgrimes
18681558Srgrimes	/*
18691558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
18701558Srgrimes	 */
18711558Srgrimes	p = line;
18721558Srgrimes	totlen = 0;
18731558Srgrimes	do {
18741558Srgrimes		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
18751558Srgrimes			return (0);
18761558Srgrimes		len = strlen(p);
18771558Srgrimes		cp = p + len - 1;
18781558Srgrimes		cont_line = 0;
18791558Srgrimes		while (cp >= p &&
18801558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
18811558Srgrimes			if (*cp == '\\')
18821558Srgrimes				cont_line = 1;
18831558Srgrimes			cp--;
18841558Srgrimes			len--;
18851558Srgrimes		}
18861558Srgrimes		*++cp = '\0';
18871558Srgrimes		if (len > 0) {
18881558Srgrimes			totlen += len;
18891558Srgrimes			if (totlen >= LINESIZ) {
189037663Scharnier				syslog(LOG_ERR, "exports line too long");
18911558Srgrimes				exit(2);
18921558Srgrimes			}
18931558Srgrimes			p = cp;
18941558Srgrimes		}
18951558Srgrimes	} while (totlen == 0 || cont_line);
18961558Srgrimes	return (1);
18971558Srgrimes}
18981558Srgrimes
18991558Srgrimes/*
19001558Srgrimes * Parse a description of a credential.
19011558Srgrimes */
19021558Srgrimesvoid
19031558Srgrimesparsecred(namelist, cr)
19041558Srgrimes	char *namelist;
19051558Srgrimes	struct ucred *cr;
19061558Srgrimes{
19071558Srgrimes	char *name;
19081558Srgrimes	int cnt;
19091558Srgrimes	char *names;
19101558Srgrimes	struct passwd *pw;
19111558Srgrimes	struct group *gr;
19121558Srgrimes	int ngroups, groups[NGROUPS + 1];
19131558Srgrimes
19141558Srgrimes	/*
191537663Scharnier	 * Set up the unprivileged user.
19161558Srgrimes	 */
19171558Srgrimes	cr->cr_ref = 1;
19181558Srgrimes	cr->cr_uid = -2;
19191558Srgrimes	cr->cr_groups[0] = -2;
19201558Srgrimes	cr->cr_ngroups = 1;
19211558Srgrimes	/*
19221558Srgrimes	 * Get the user's password table entry.
19231558Srgrimes	 */
19241558Srgrimes	names = strsep(&namelist, " \t\n");
19251558Srgrimes	name = strsep(&names, ":");
19261558Srgrimes	if (isdigit(*name) || *name == '-')
19271558Srgrimes		pw = getpwuid(atoi(name));
19281558Srgrimes	else
19291558Srgrimes		pw = getpwnam(name);
19301558Srgrimes	/*
19311558Srgrimes	 * Credentials specified as those of a user.
19321558Srgrimes	 */
19331558Srgrimes	if (names == NULL) {
19341558Srgrimes		if (pw == NULL) {
193537663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
19361558Srgrimes			return;
19371558Srgrimes		}
19381558Srgrimes		cr->cr_uid = pw->pw_uid;
19391558Srgrimes		ngroups = NGROUPS + 1;
19401558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
194137663Scharnier			syslog(LOG_ERR, "too many groups");
19421558Srgrimes		/*
19431558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
19441558Srgrimes		 */
19451558Srgrimes		cr->cr_ngroups = ngroups - 1;
19461558Srgrimes		cr->cr_groups[0] = groups[0];
19471558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
19481558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
19491558Srgrimes		return;
19501558Srgrimes	}
19511558Srgrimes	/*
19521558Srgrimes	 * Explicit credential specified as a colon separated list:
19531558Srgrimes	 *	uid:gid:gid:...
19541558Srgrimes	 */
19551558Srgrimes	if (pw != NULL)
19561558Srgrimes		cr->cr_uid = pw->pw_uid;
19571558Srgrimes	else if (isdigit(*name) || *name == '-')
19581558Srgrimes		cr->cr_uid = atoi(name);
19591558Srgrimes	else {
196037663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
19611558Srgrimes		return;
19621558Srgrimes	}
19631558Srgrimes	cr->cr_ngroups = 0;
19641558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
19651558Srgrimes		name = strsep(&names, ":");
19661558Srgrimes		if (isdigit(*name) || *name == '-') {
19671558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
19681558Srgrimes		} else {
19691558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
197037663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
19711558Srgrimes				continue;
19721558Srgrimes			}
19731558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
19741558Srgrimes		}
19751558Srgrimes	}
19761558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
197737663Scharnier		syslog(LOG_ERR, "too many groups");
19781558Srgrimes}
19791558Srgrimes
19801558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
19811558Srgrimes/*
19821558Srgrimes * Routines that maintain the remote mounttab
19831558Srgrimes */
19841558Srgrimesvoid
19851558Srgrimesget_mountlist()
19861558Srgrimes{
19871558Srgrimes	struct mountlist *mlp, **mlpp;
198823681Speter	char *host, *dirp, *cp;
19891558Srgrimes	char str[STRSIZ];
19901558Srgrimes	FILE *mlfile;
19911558Srgrimes
19921558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
199337663Scharnier		syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
19941558Srgrimes		return;
19951558Srgrimes	}
19961558Srgrimes	mlpp = &mlhead;
19971558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
199823681Speter		cp = str;
199923681Speter		host = strsep(&cp, " \t\n");
200023681Speter		dirp = strsep(&cp, " \t\n");
200123681Speter		if (host == NULL || dirp == NULL)
20021558Srgrimes			continue;
20031558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
200437663Scharnier		if (mlp == (struct mountlist *)NULL)
200537663Scharnier			out_of_mem();
200623681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
200723681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
200823681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
200923681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
20101558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
20111558Srgrimes		*mlpp = mlp;
20121558Srgrimes		mlpp = &mlp->ml_next;
20131558Srgrimes	}
20141558Srgrimes	fclose(mlfile);
20151558Srgrimes}
20161558Srgrimes
20171558Srgrimesvoid
20181558Srgrimesdel_mlist(hostp, dirp)
20191558Srgrimes	char *hostp, *dirp;
20201558Srgrimes{
20211558Srgrimes	struct mountlist *mlp, **mlpp;
20221558Srgrimes	struct mountlist *mlp2;
20231558Srgrimes	FILE *mlfile;
20241558Srgrimes	int fnd = 0;
20251558Srgrimes
20261558Srgrimes	mlpp = &mlhead;
20271558Srgrimes	mlp = mlhead;
20281558Srgrimes	while (mlp) {
20291558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
20301558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
20311558Srgrimes			fnd = 1;
20321558Srgrimes			mlp2 = mlp;
20331558Srgrimes			*mlpp = mlp = mlp->ml_next;
20341558Srgrimes			free((caddr_t)mlp2);
20351558Srgrimes		} else {
20361558Srgrimes			mlpp = &mlp->ml_next;
20371558Srgrimes			mlp = mlp->ml_next;
20381558Srgrimes		}
20391558Srgrimes	}
20401558Srgrimes	if (fnd) {
20411558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
204237663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
20431558Srgrimes			return;
20441558Srgrimes		}
20451558Srgrimes		mlp = mlhead;
20461558Srgrimes		while (mlp) {
20471558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20481558Srgrimes			mlp = mlp->ml_next;
20491558Srgrimes		}
20501558Srgrimes		fclose(mlfile);
20511558Srgrimes	}
20521558Srgrimes}
20531558Srgrimes
20541558Srgrimesvoid
20551558Srgrimesadd_mlist(hostp, dirp)
20561558Srgrimes	char *hostp, *dirp;
20571558Srgrimes{
20581558Srgrimes	struct mountlist *mlp, **mlpp;
20591558Srgrimes	FILE *mlfile;
20601558Srgrimes
20611558Srgrimes	mlpp = &mlhead;
20621558Srgrimes	mlp = mlhead;
20631558Srgrimes	while (mlp) {
20641558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
20651558Srgrimes			return;
20661558Srgrimes		mlpp = &mlp->ml_next;
20671558Srgrimes		mlp = mlp->ml_next;
20681558Srgrimes	}
20691558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
207037663Scharnier	if (mlp == (struct mountlist *)NULL)
207137663Scharnier		out_of_mem();
20721558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
20731558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
20741558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
20751558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
20761558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
20771558Srgrimes	*mlpp = mlp;
20781558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
207937663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
20801558Srgrimes		return;
20811558Srgrimes	}
20821558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20831558Srgrimes	fclose(mlfile);
20841558Srgrimes}
20851558Srgrimes
20861558Srgrimes/*
20871558Srgrimes * This function is called via. SIGTERM when the system is going down.
20881558Srgrimes * It sends a broadcast RPCMNT_UMNTALL.
20891558Srgrimes */
20901558Srgrimesvoid
20911558Srgrimessend_umntall()
20921558Srgrimes{
20931558Srgrimes	(void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
20941558Srgrimes		xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
20951558Srgrimes	exit(0);
20961558Srgrimes}
20971558Srgrimes
20981558Srgrimesint
20991558Srgrimesumntall_each(resultsp, raddr)
21001558Srgrimes	caddr_t resultsp;
21011558Srgrimes	struct sockaddr_in *raddr;
21021558Srgrimes{
21031558Srgrimes	return (1);
21041558Srgrimes}
21051558Srgrimes
21061558Srgrimes/*
21071558Srgrimes * Free up a group list.
21081558Srgrimes */
21091558Srgrimesvoid
21101558Srgrimesfree_grp(grp)
21111558Srgrimes	struct grouplist *grp;
21121558Srgrimes{
21131558Srgrimes	char **addrp;
21141558Srgrimes
21151558Srgrimes	if (grp->gr_type == GT_HOST) {
21161558Srgrimes		if (grp->gr_ptr.gt_hostent->h_name) {
21171558Srgrimes			addrp = grp->gr_ptr.gt_hostent->h_addr_list;
21181558Srgrimes			while (addrp && *addrp)
21191558Srgrimes				free(*addrp++);
21201558Srgrimes			free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
21211558Srgrimes			free(grp->gr_ptr.gt_hostent->h_name);
21221558Srgrimes		}
21231558Srgrimes		free((caddr_t)grp->gr_ptr.gt_hostent);
21241558Srgrimes	} else if (grp->gr_type == GT_NET) {
21251558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
21261558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
21271558Srgrimes	}
21281558Srgrimes#ifdef ISO
21291558Srgrimes	else if (grp->gr_type == GT_ISO)
21301558Srgrimes		free((caddr_t)grp->gr_ptr.gt_isoaddr);
21311558Srgrimes#endif
21321558Srgrimes	free((caddr_t)grp);
21331558Srgrimes}
21341558Srgrimes
21351558Srgrimes#ifdef DEBUG
21361558Srgrimesvoid
21371558SrgrimesSYSLOG(int pri, const char *fmt, ...)
21381558Srgrimes{
21391558Srgrimes	va_list ap;
21401558Srgrimes
21411558Srgrimes	va_start(ap, fmt);
21421558Srgrimes	vfprintf(stderr, fmt, ap);
21431558Srgrimes	va_end(ap);
21441558Srgrimes}
21451558Srgrimes#endif /* DEBUG */
21461558Srgrimes
21471558Srgrimes/*
21481558Srgrimes * Check options for consistency.
21491558Srgrimes */
21501558Srgrimesint
21511558Srgrimescheck_options(dp)
21521558Srgrimes	struct dirlist *dp;
21531558Srgrimes{
21541558Srgrimes
21551558Srgrimes	if (dp == (struct dirlist *)NULL)
21561558Srgrimes	    return (1);
21571558Srgrimes	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
21581558Srgrimes	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
21591558Srgrimes	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
21601558Srgrimes	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
21611558Srgrimes	    return (1);
21621558Srgrimes	}
21631558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
21641558Srgrimes	    syslog(LOG_ERR, "-mask requires -net");
21651558Srgrimes	    return (1);
21661558Srgrimes	}
21671558Srgrimes	if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
21681558Srgrimes	    syslog(LOG_ERR, "-net and -iso mutually exclusive");
21691558Srgrimes	    return (1);
21701558Srgrimes	}
21711558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
217245927Salex	    syslog(LOG_ERR, "-alldirs has multiple directories");
21731558Srgrimes	    return (1);
21741558Srgrimes	}
21751558Srgrimes	return (0);
21761558Srgrimes}
21771558Srgrimes
21781558Srgrimes/*
21791558Srgrimes * Check an absolute directory path for any symbolic links. Return true
21801558Srgrimes * if no symbolic links are found.
21811558Srgrimes */
21821558Srgrimesint
21831558Srgrimescheck_dirpath(dirp)
21841558Srgrimes	char *dirp;
21851558Srgrimes{
21861558Srgrimes	char *cp;
21871558Srgrimes	int ret = 1;
21881558Srgrimes	struct stat sb;
21891558Srgrimes
21901558Srgrimes	cp = dirp + 1;
21911558Srgrimes	while (*cp && ret) {
21921558Srgrimes		if (*cp == '/') {
21931558Srgrimes			*cp = '\0';
21949336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21951558Srgrimes				ret = 0;
21961558Srgrimes			*cp = '/';
21971558Srgrimes		}
21981558Srgrimes		cp++;
21991558Srgrimes	}
22009336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
22011558Srgrimes		ret = 0;
22021558Srgrimes	return (ret);
22031558Srgrimes}
22049336Sdfr
22059336Sdfr/*
22069336Sdfr * Just translate an ascii string to an integer.
22079336Sdfr */
22089336Sdfrint
22099336Sdfrget_num(cp)
22109336Sdfr	register char *cp;
22119336Sdfr{
22129336Sdfr	register int res = 0;
22139336Sdfr
22149336Sdfr	while (*cp) {
22159336Sdfr		if (*cp < '0' || *cp > '9')
22169336Sdfr			return (-1);
22179336Sdfr		res = res * 10 + (*cp++ - '0');
22189336Sdfr	}
22199336Sdfr	return (res);
22209336Sdfr}
2221