mountd.c revision 42144
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[] =
4842144Sdfr	"$Id: mountd.c,v 1.33 1998/08/02 16:06:34 bde Exp $";
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		 */
4161558Srgrimes		if (realpath(rpcpath, dirpath) == 0 ||
4171558Srgrimes		    stat(dirpath, &stb) < 0 ||
4189336Sdfr		    (!S_ISDIR(stb.st_mode) &&
4199336Sdfr		     (dir_only || !S_ISREG(stb.st_mode))) ||
4201558Srgrimes		    statfs(dirpath, &fsb) < 0) {
4211558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
42231656Sguido			syslog(LOG_NOTICE,
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		}
5051558Srgrimes		if (!svc_getargs(transp, xdr_dir, dirpath)) {
50631656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
50731656Sguido			    inet_ntoa(saddrin));
5081558Srgrimes			svcerr_decode(transp);
5091558Srgrimes			return;
5101558Srgrimes		}
5111558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
51237663Scharnier			syslog(LOG_ERR, "can't send reply");
5131558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
5141558Srgrimes		if (hp)
5151558Srgrimes			del_mlist(hp->h_name, dirpath);
51631656Sguido		del_mlist(inet_ntoa(saddrin), dirpath);
51731656Sguido		if (log)
51831656Sguido			syslog(LOG_NOTICE,
51931656Sguido			    "umount request succeeded from %s for %s",
52031656Sguido			    inet_ntoa(saddrin), dirpath);
5211558Srgrimes		return;
5221558Srgrimes	case RPCMNT_UMNTALL:
5239336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
52431656Sguido			syslog(LOG_NOTICE,
52531656Sguido			    "umountall request from %s from unprivileged port",
52631656Sguido			    inet_ntoa(saddrin));
5271558Srgrimes			svcerr_weakauth(transp);
5281558Srgrimes			return;
5291558Srgrimes		}
5301558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
53137663Scharnier			syslog(LOG_ERR, "can't send reply");
5321558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
5331558Srgrimes		if (hp)
5341558Srgrimes			del_mlist(hp->h_name, (char *)NULL);
53531656Sguido		del_mlist(inet_ntoa(saddrin), (char *)NULL);
53631656Sguido		if (log)
53731656Sguido			syslog(LOG_NOTICE,
53831656Sguido			    "umountall request succeeded from %s",
53931656Sguido			    inet_ntoa(saddrin));
5401558Srgrimes		return;
5411558Srgrimes	case RPCMNT_EXPORT:
5421558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
54337663Scharnier			syslog(LOG_ERR, "can't send reply");
54431656Sguido		if (log)
54531656Sguido			syslog(LOG_NOTICE,
54631656Sguido			    "export request succeeded from %s",
54731656Sguido			    inet_ntoa(saddrin));
5481558Srgrimes		return;
5491558Srgrimes	default:
5501558Srgrimes		svcerr_noproc(transp);
5511558Srgrimes		return;
5521558Srgrimes	}
5531558Srgrimes}
5541558Srgrimes
5551558Srgrimes/*
5561558Srgrimes * Xdr conversion for a dirpath string
5571558Srgrimes */
5581558Srgrimesint
5591558Srgrimesxdr_dir(xdrsp, dirp)
5601558Srgrimes	XDR *xdrsp;
5611558Srgrimes	char *dirp;
5621558Srgrimes{
5631558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
5641558Srgrimes}
5651558Srgrimes
5661558Srgrimes/*
5679336Sdfr * Xdr routine to generate file handle reply
5681558Srgrimes */
5691558Srgrimesint
5709336Sdfrxdr_fhs(xdrsp, cp)
5711558Srgrimes	XDR *xdrsp;
5729336Sdfr	caddr_t cp;
5731558Srgrimes{
5749336Sdfr	register struct fhreturn *fhrp = (struct fhreturn *)cp;
5759336Sdfr	u_long ok = 0, len, auth;
5761558Srgrimes
5771558Srgrimes	if (!xdr_long(xdrsp, &ok))
5781558Srgrimes		return (0);
5799336Sdfr	switch (fhrp->fhr_vers) {
5809336Sdfr	case 1:
5819336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
5829336Sdfr	case 3:
5839336Sdfr		len = NFSX_V3FH;
5849336Sdfr		if (!xdr_long(xdrsp, &len))
5859336Sdfr			return (0);
5869336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
5879336Sdfr			return (0);
5889336Sdfr		if (fhrp->fhr_flag & DP_KERB)
5899336Sdfr			auth = RPCAUTH_KERB4;
5909336Sdfr		else
5919336Sdfr			auth = RPCAUTH_UNIX;
5929336Sdfr		len = 1;
5939336Sdfr		if (!xdr_long(xdrsp, &len))
5949336Sdfr			return (0);
5959336Sdfr		return (xdr_long(xdrsp, &auth));
5969336Sdfr	};
5979336Sdfr	return (0);
5981558Srgrimes}
5991558Srgrimes
6001558Srgrimesint
6011558Srgrimesxdr_mlist(xdrsp, cp)
6021558Srgrimes	XDR *xdrsp;
6031558Srgrimes	caddr_t cp;
6041558Srgrimes{
6051558Srgrimes	struct mountlist *mlp;
6061558Srgrimes	int true = 1;
6071558Srgrimes	int false = 0;
6081558Srgrimes	char *strp;
6091558Srgrimes
6101558Srgrimes	mlp = mlhead;
6111558Srgrimes	while (mlp) {
6121558Srgrimes		if (!xdr_bool(xdrsp, &true))
6131558Srgrimes			return (0);
6141558Srgrimes		strp = &mlp->ml_host[0];
6151558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
6161558Srgrimes			return (0);
6171558Srgrimes		strp = &mlp->ml_dirp[0];
6181558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6191558Srgrimes			return (0);
6201558Srgrimes		mlp = mlp->ml_next;
6211558Srgrimes	}
6221558Srgrimes	if (!xdr_bool(xdrsp, &false))
6231558Srgrimes		return (0);
6241558Srgrimes	return (1);
6251558Srgrimes}
6261558Srgrimes
6271558Srgrimes/*
6281558Srgrimes * Xdr conversion for export list
6291558Srgrimes */
6301558Srgrimesint
6311558Srgrimesxdr_explist(xdrsp, cp)
6321558Srgrimes	XDR *xdrsp;
6331558Srgrimes	caddr_t cp;
6341558Srgrimes{
6351558Srgrimes	struct exportlist *ep;
6361558Srgrimes	int false = 0;
6379336Sdfr	int putdef;
6389336Sdfr	sigset_t sighup_mask;
6391558Srgrimes
6409336Sdfr	sigemptyset(&sighup_mask);
6419336Sdfr	sigaddset(&sighup_mask, SIGHUP);
6429336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
6431558Srgrimes	ep = exphead;
6441558Srgrimes	while (ep) {
6451558Srgrimes		putdef = 0;
6461558Srgrimes		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
6471558Srgrimes			goto errout;
6481558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
6491558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
6501558Srgrimes			&putdef))
6511558Srgrimes			goto errout;
6521558Srgrimes		ep = ep->ex_next;
6531558Srgrimes	}
6549336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6551558Srgrimes	if (!xdr_bool(xdrsp, &false))
6561558Srgrimes		return (0);
6571558Srgrimes	return (1);
6581558Srgrimeserrout:
6599336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6601558Srgrimes	return (0);
6611558Srgrimes}
6621558Srgrimes
6631558Srgrimes/*
6641558Srgrimes * Called from xdr_explist() to traverse the tree and export the
6651558Srgrimes * directory paths.
6661558Srgrimes */
6671558Srgrimesint
6681558Srgrimesput_exlist(dp, xdrsp, adp, putdefp)
6691558Srgrimes	struct dirlist *dp;
6701558Srgrimes	XDR *xdrsp;
6711558Srgrimes	struct dirlist *adp;
6721558Srgrimes	int *putdefp;
6731558Srgrimes{
6741558Srgrimes	struct grouplist *grp;
6751558Srgrimes	struct hostlist *hp;
6761558Srgrimes	int true = 1;
6771558Srgrimes	int false = 0;
6781558Srgrimes	int gotalldir = 0;
6791558Srgrimes	char *strp;
6801558Srgrimes
6811558Srgrimes	if (dp) {
6821558Srgrimes		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
6831558Srgrimes			return (1);
6841558Srgrimes		if (!xdr_bool(xdrsp, &true))
6851558Srgrimes			return (1);
6861558Srgrimes		strp = dp->dp_dirp;
6871558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6881558Srgrimes			return (1);
6891558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
6901558Srgrimes			gotalldir = 1;
6911558Srgrimes			*putdefp = 1;
6921558Srgrimes		}
6931558Srgrimes		if ((dp->dp_flag & DP_DEFSET) == 0 &&
6941558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
6951558Srgrimes			hp = dp->dp_hosts;
6961558Srgrimes			while (hp) {
6971558Srgrimes				grp = hp->ht_grp;
6981558Srgrimes				if (grp->gr_type == GT_HOST) {
6991558Srgrimes					if (!xdr_bool(xdrsp, &true))
7001558Srgrimes						return (1);
7011558Srgrimes					strp = grp->gr_ptr.gt_hostent->h_name;
7028871Srgrimes					if (!xdr_string(xdrsp, &strp,
7031558Srgrimes					    RPCMNT_NAMELEN))
7041558Srgrimes						return (1);
7051558Srgrimes				} else if (grp->gr_type == GT_NET) {
7061558Srgrimes					if (!xdr_bool(xdrsp, &true))
7071558Srgrimes						return (1);
7081558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
7098871Srgrimes					if (!xdr_string(xdrsp, &strp,
7101558Srgrimes					    RPCMNT_NAMELEN))
7111558Srgrimes						return (1);
7121558Srgrimes				}
7131558Srgrimes				hp = hp->ht_next;
7141558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
7151558Srgrimes					hp = adp->dp_hosts;
7161558Srgrimes					gotalldir = 0;
7171558Srgrimes				}
7181558Srgrimes			}
7191558Srgrimes		}
7201558Srgrimes		if (!xdr_bool(xdrsp, &false))
7211558Srgrimes			return (1);
7221558Srgrimes		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
7231558Srgrimes			return (1);
7241558Srgrimes	}
7251558Srgrimes	return (0);
7261558Srgrimes}
7271558Srgrimes
7281558Srgrimes#define LINESIZ	10240
7291558Srgrimeschar line[LINESIZ];
7301558SrgrimesFILE *exp_file;
7311558Srgrimes
7321558Srgrimes/*
7331558Srgrimes * Get the export list
7341558Srgrimes */
7351558Srgrimesvoid
7361558Srgrimesget_exportlist()
7371558Srgrimes{
7381558Srgrimes	struct exportlist *ep, *ep2;
7391558Srgrimes	struct grouplist *grp, *tgrp;
7401558Srgrimes	struct exportlist **epp;
7411558Srgrimes	struct dirlist *dirhead;
7421558Srgrimes	struct statfs fsb, *fsp;
7431558Srgrimes	struct hostent *hpe;
7441558Srgrimes	struct ucred anon;
7451558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
7461558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
7471558Srgrimes
7481558Srgrimes	/*
7491558Srgrimes	 * First, get rid of the old list
7501558Srgrimes	 */
7511558Srgrimes	ep = exphead;
7521558Srgrimes	while (ep) {
7531558Srgrimes		ep2 = ep;
7541558Srgrimes		ep = ep->ex_next;
7551558Srgrimes		free_exp(ep2);
7561558Srgrimes	}
7571558Srgrimes	exphead = (struct exportlist *)NULL;
7581558Srgrimes
7591558Srgrimes	grp = grphead;
7601558Srgrimes	while (grp) {
7611558Srgrimes		tgrp = grp;
7621558Srgrimes		grp = grp->gr_next;
7631558Srgrimes		free_grp(tgrp);
7641558Srgrimes	}
7651558Srgrimes	grphead = (struct grouplist *)NULL;
7661558Srgrimes
7671558Srgrimes	/*
7681558Srgrimes	 * And delete exports that are in the kernel for all local
7691558Srgrimes	 * file systems.
7701558Srgrimes	 * XXX: Should know how to handle all local exportable file systems
77123681Speter	 *      instead of just "ufs".
7721558Srgrimes	 */
7731558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
7741558Srgrimes	for (i = 0; i < num; i++) {
7751558Srgrimes		union {
7761558Srgrimes			struct ufs_args ua;
7771558Srgrimes			struct iso_args ia;
7781558Srgrimes			struct mfs_args ma;
7799336Sdfr			struct msdosfs_args da;
7801558Srgrimes		} targs;
7811558Srgrimes
78223681Speter		if (!strcmp(fsp->f_fstypename, "mfs") ||
78323681Speter		    !strcmp(fsp->f_fstypename, "ufs") ||
78423681Speter		    !strcmp(fsp->f_fstypename, "msdos") ||
78523681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
7869336Sdfr			targs.ua.fspec = NULL;
7879336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
7889336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
7891558Srgrimes				  fsp->f_flags | MNT_UPDATE,
7901558Srgrimes				  (caddr_t)&targs) < 0)
79137663Scharnier				syslog(LOG_ERR, "can't delete exports for %s",
7921558Srgrimes				       fsp->f_mntonname);
7931558Srgrimes		}
7941558Srgrimes		fsp++;
7951558Srgrimes	}
7961558Srgrimes
7971558Srgrimes	/*
7981558Srgrimes	 * Read in the exports file and build the list, calling
7991558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
8001558Srgrimes	 */
8011558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
80237663Scharnier		syslog(LOG_ERR, "can't open %s", exname);
8031558Srgrimes		exit(2);
8041558Srgrimes	}
8051558Srgrimes	dirhead = (struct dirlist *)NULL;
8061558Srgrimes	while (get_line()) {
8071558Srgrimes		if (debug)
80837663Scharnier			warnx("got line %s", line);
8091558Srgrimes		cp = line;
8101558Srgrimes		nextfield(&cp, &endcp);
8111558Srgrimes		if (*cp == '#')
8121558Srgrimes			goto nextline;
8131558Srgrimes
8141558Srgrimes		/*
8151558Srgrimes		 * Set defaults.
8161558Srgrimes		 */
8171558Srgrimes		has_host = FALSE;
8181558Srgrimes		anon = def_anon;
8191558Srgrimes		exflags = MNT_EXPORTED;
8201558Srgrimes		got_nondir = 0;
8211558Srgrimes		opt_flags = 0;
8221558Srgrimes		ep = (struct exportlist *)NULL;
8231558Srgrimes
8241558Srgrimes		/*
8251558Srgrimes		 * Create new exports list entry
8261558Srgrimes		 */
8271558Srgrimes		len = endcp-cp;
8281558Srgrimes		tgrp = grp = get_grp();
8291558Srgrimes		while (len > 0) {
8301558Srgrimes			if (len > RPCMNT_NAMELEN) {
8311558Srgrimes			    getexp_err(ep, tgrp);
8321558Srgrimes			    goto nextline;
8331558Srgrimes			}
8341558Srgrimes			if (*cp == '-') {
8351558Srgrimes			    if (ep == (struct exportlist *)NULL) {
8361558Srgrimes				getexp_err(ep, tgrp);
8371558Srgrimes				goto nextline;
8381558Srgrimes			    }
8391558Srgrimes			    if (debug)
84037663Scharnier				warnx("doing opt %s", cp);
8411558Srgrimes			    got_nondir = 1;
8421558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
8431558Srgrimes				&exflags, &anon)) {
8441558Srgrimes				getexp_err(ep, tgrp);
8451558Srgrimes				goto nextline;
8461558Srgrimes			    }
8471558Srgrimes			} else if (*cp == '/') {
8481558Srgrimes			    savedc = *endcp;
8491558Srgrimes			    *endcp = '\0';
8501558Srgrimes			    if (check_dirpath(cp) &&
8511558Srgrimes				statfs(cp, &fsb) >= 0) {
8521558Srgrimes				if (got_nondir) {
85337663Scharnier				    syslog(LOG_ERR, "dirs must be first");
8541558Srgrimes				    getexp_err(ep, tgrp);
8551558Srgrimes				    goto nextline;
8561558Srgrimes				}
8571558Srgrimes				if (ep) {
8581558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
8591558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
8601558Srgrimes					getexp_err(ep, tgrp);
8611558Srgrimes					goto nextline;
8621558Srgrimes				    }
8631558Srgrimes				} else {
8641558Srgrimes				    /*
8651558Srgrimes				     * See if this directory is already
8661558Srgrimes				     * in the list.
8671558Srgrimes				     */
8681558Srgrimes				    ep = ex_search(&fsb.f_fsid);
8691558Srgrimes				    if (ep == (struct exportlist *)NULL) {
8701558Srgrimes					ep = get_exp();
8711558Srgrimes					ep->ex_fs = fsb.f_fsid;
8721558Srgrimes					ep->ex_fsdir = (char *)
8731558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
8741558Srgrimes					if (ep->ex_fsdir)
8751558Srgrimes					    strcpy(ep->ex_fsdir,
8761558Srgrimes						fsb.f_mntonname);
8771558Srgrimes					else
8781558Srgrimes					    out_of_mem();
8791558Srgrimes					if (debug)
88037663Scharnier					  warnx("making new ep fs=0x%x,0x%x",
8811558Srgrimes					      fsb.f_fsid.val[0],
8821558Srgrimes					      fsb.f_fsid.val[1]);
8831558Srgrimes				    } else if (debug)
88437663Scharnier					warnx("found ep fs=0x%x,0x%x",
8851558Srgrimes					    fsb.f_fsid.val[0],
8861558Srgrimes					    fsb.f_fsid.val[1]);
8871558Srgrimes				}
8881558Srgrimes
8891558Srgrimes				/*
8901558Srgrimes				 * Add dirpath to export mount point.
8911558Srgrimes				 */
8921558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
8931558Srgrimes				dirplen = len;
8941558Srgrimes			    } else {
8951558Srgrimes				getexp_err(ep, tgrp);
8961558Srgrimes				goto nextline;
8971558Srgrimes			    }
8981558Srgrimes			    *endcp = savedc;
8991558Srgrimes			} else {
9001558Srgrimes			    savedc = *endcp;
9011558Srgrimes			    *endcp = '\0';
9021558Srgrimes			    got_nondir = 1;
9031558Srgrimes			    if (ep == (struct exportlist *)NULL) {
9041558Srgrimes				getexp_err(ep, tgrp);
9051558Srgrimes				goto nextline;
9061558Srgrimes			    }
9071558Srgrimes
9081558Srgrimes			    /*
9091558Srgrimes			     * Get the host or netgroup.
9101558Srgrimes			     */
9111558Srgrimes			    setnetgrent(cp);
9121558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
9131558Srgrimes			    do {
9141558Srgrimes				if (has_host) {
9151558Srgrimes				    grp->gr_next = get_grp();
9161558Srgrimes				    grp = grp->gr_next;
9171558Srgrimes				}
9181558Srgrimes				if (netgrp) {
91937003Sjoerg				    if (hst == 0) {
92037663Scharnier					syslog(LOG_ERR,
92137663Scharnier				"null hostname in netgroup %s, skipping", cp);
92237004Sjoerg					grp->gr_type = GT_IGNORE;
92337003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
92437663Scharnier					syslog(LOG_ERR,
92537663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
92629317Sjlemon					grp->gr_type = GT_IGNORE;
9271558Srgrimes				    }
9287401Swpaul				} else if (get_host(cp, grp, tgrp)) {
92937663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
93029317Sjlemon				    grp->gr_type = GT_IGNORE;
9311558Srgrimes				}
9321558Srgrimes				has_host = TRUE;
9331558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
9341558Srgrimes			    endnetgrent();
9351558Srgrimes			    *endcp = savedc;
9361558Srgrimes			}
9371558Srgrimes			cp = endcp;
9381558Srgrimes			nextfield(&cp, &endcp);
9391558Srgrimes			len = endcp - cp;
9401558Srgrimes		}
9411558Srgrimes		if (check_options(dirhead)) {
9421558Srgrimes			getexp_err(ep, tgrp);
9431558Srgrimes			goto nextline;
9441558Srgrimes		}
9451558Srgrimes		if (!has_host) {
9461558Srgrimes			grp->gr_type = GT_HOST;
9471558Srgrimes			if (debug)
94837663Scharnier				warnx("adding a default entry");
9491558Srgrimes			/* add a default group and make the grp list NULL */
9501558Srgrimes			hpe = (struct hostent *)malloc(sizeof(struct hostent));
9511558Srgrimes			if (hpe == (struct hostent *)NULL)
9521558Srgrimes				out_of_mem();
95312348Sjoerg			hpe->h_name = strdup("Default");
9541558Srgrimes			hpe->h_addrtype = AF_INET;
95542144Sdfr			hpe->h_length = sizeof (u_int32_t);
9561558Srgrimes			hpe->h_addr_list = (char **)NULL;
9571558Srgrimes			grp->gr_ptr.gt_hostent = hpe;
9581558Srgrimes
9591558Srgrimes		/*
9601558Srgrimes		 * Don't allow a network export coincide with a list of
9611558Srgrimes		 * host(s) on the same line.
9621558Srgrimes		 */
9631558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
9641558Srgrimes			getexp_err(ep, tgrp);
9651558Srgrimes			goto nextline;
96629317Sjlemon
96729317Sjlemon        	/*
96829317Sjlemon	         * If an export list was specified on this line, make sure
96929317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
97029317Sjlemon		 */
97129317Sjlemon		} else {
97229317Sjlemon			grp = tgrp;
97329317Sjlemon        		while (grp && grp->gr_type == GT_IGNORE)
97429317Sjlemon				grp = grp->gr_next;
97529317Sjlemon			if (! grp) {
97629317Sjlemon			    getexp_err(ep, tgrp);
97729317Sjlemon			    goto nextline;
97829317Sjlemon			}
9791558Srgrimes		}
9801558Srgrimes
9811558Srgrimes		/*
9821558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
9831558Srgrimes		 * After loop, tgrp points to the start of the list and
9841558Srgrimes		 * grp points to the last entry in the list.
9851558Srgrimes		 */
9861558Srgrimes		grp = tgrp;
9871558Srgrimes		do {
9881558Srgrimes		    if (do_mount(ep, grp, exflags, &anon, dirp,
9891558Srgrimes			dirplen, &fsb)) {
9901558Srgrimes			getexp_err(ep, tgrp);
9911558Srgrimes			goto nextline;
9921558Srgrimes		    }
9931558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
9941558Srgrimes
9951558Srgrimes		/*
9961558Srgrimes		 * Success. Update the data structures.
9971558Srgrimes		 */
9981558Srgrimes		if (has_host) {
9999336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
10001558Srgrimes			grp->gr_next = grphead;
10011558Srgrimes			grphead = tgrp;
10021558Srgrimes		} else {
10031558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
10049336Sdfr				opt_flags);
10051558Srgrimes			free_grp(grp);
10061558Srgrimes		}
10071558Srgrimes		dirhead = (struct dirlist *)NULL;
10081558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
10091558Srgrimes			ep2 = exphead;
10101558Srgrimes			epp = &exphead;
10111558Srgrimes
10121558Srgrimes			/*
10131558Srgrimes			 * Insert in the list in alphabetical order.
10141558Srgrimes			 */
10151558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
10161558Srgrimes				epp = &ep2->ex_next;
10171558Srgrimes				ep2 = ep2->ex_next;
10181558Srgrimes			}
10191558Srgrimes			if (ep2)
10201558Srgrimes				ep->ex_next = ep2;
10211558Srgrimes			*epp = ep;
10221558Srgrimes			ep->ex_flag |= EX_LINKED;
10231558Srgrimes		}
10241558Srgrimesnextline:
10251558Srgrimes		if (dirhead) {
10261558Srgrimes			free_dir(dirhead);
10271558Srgrimes			dirhead = (struct dirlist *)NULL;
10281558Srgrimes		}
10291558Srgrimes	}
10301558Srgrimes	fclose(exp_file);
10311558Srgrimes}
10321558Srgrimes
10331558Srgrimes/*
10341558Srgrimes * Allocate an export list element
10351558Srgrimes */
10361558Srgrimesstruct exportlist *
10371558Srgrimesget_exp()
10381558Srgrimes{
10391558Srgrimes	struct exportlist *ep;
10401558Srgrimes
10411558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
10421558Srgrimes	if (ep == (struct exportlist *)NULL)
10431558Srgrimes		out_of_mem();
104423681Speter	memset(ep, 0, sizeof(struct exportlist));
10451558Srgrimes	return (ep);
10461558Srgrimes}
10471558Srgrimes
10481558Srgrimes/*
10491558Srgrimes * Allocate a group list element
10501558Srgrimes */
10511558Srgrimesstruct grouplist *
10521558Srgrimesget_grp()
10531558Srgrimes{
10541558Srgrimes	struct grouplist *gp;
10551558Srgrimes
10561558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
10571558Srgrimes	if (gp == (struct grouplist *)NULL)
10581558Srgrimes		out_of_mem();
105923681Speter	memset(gp, 0, sizeof(struct grouplist));
10601558Srgrimes	return (gp);
10611558Srgrimes}
10621558Srgrimes
10631558Srgrimes/*
10641558Srgrimes * Clean up upon an error in get_exportlist().
10651558Srgrimes */
10661558Srgrimesvoid
10671558Srgrimesgetexp_err(ep, grp)
10681558Srgrimes	struct exportlist *ep;
10691558Srgrimes	struct grouplist *grp;
10701558Srgrimes{
10711558Srgrimes	struct grouplist *tgrp;
10721558Srgrimes
107337663Scharnier	syslog(LOG_ERR, "bad exports list line %s", line);
10741558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
10751558Srgrimes		free_exp(ep);
10761558Srgrimes	while (grp) {
10771558Srgrimes		tgrp = grp;
10781558Srgrimes		grp = grp->gr_next;
10791558Srgrimes		free_grp(tgrp);
10801558Srgrimes	}
10811558Srgrimes}
10821558Srgrimes
10831558Srgrimes/*
10841558Srgrimes * Search the export list for a matching fs.
10851558Srgrimes */
10861558Srgrimesstruct exportlist *
10871558Srgrimesex_search(fsid)
10881558Srgrimes	fsid_t *fsid;
10891558Srgrimes{
10901558Srgrimes	struct exportlist *ep;
10911558Srgrimes
10921558Srgrimes	ep = exphead;
10931558Srgrimes	while (ep) {
10941558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
10951558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
10961558Srgrimes			return (ep);
10971558Srgrimes		ep = ep->ex_next;
10981558Srgrimes	}
10991558Srgrimes	return (ep);
11001558Srgrimes}
11011558Srgrimes
11021558Srgrimes/*
11031558Srgrimes * Add a directory path to the list.
11041558Srgrimes */
11051558Srgrimeschar *
11061558Srgrimesadd_expdir(dpp, cp, len)
11071558Srgrimes	struct dirlist **dpp;
11081558Srgrimes	char *cp;
11091558Srgrimes	int len;
11101558Srgrimes{
11111558Srgrimes	struct dirlist *dp;
11121558Srgrimes
11131558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
111437663Scharnier	if (dp == (struct dirlist *)NULL)
111537663Scharnier		out_of_mem();
11161558Srgrimes	dp->dp_left = *dpp;
11171558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
11181558Srgrimes	dp->dp_flag = 0;
11191558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
11201558Srgrimes	strcpy(dp->dp_dirp, cp);
11211558Srgrimes	*dpp = dp;
11221558Srgrimes	return (dp->dp_dirp);
11231558Srgrimes}
11241558Srgrimes
11251558Srgrimes/*
11261558Srgrimes * Hang the dir list element off the dirpath binary tree as required
11271558Srgrimes * and update the entry for host.
11281558Srgrimes */
11291558Srgrimesvoid
11309336Sdfrhang_dirp(dp, grp, ep, flags)
11311558Srgrimes	struct dirlist *dp;
11321558Srgrimes	struct grouplist *grp;
11331558Srgrimes	struct exportlist *ep;
11349336Sdfr	int flags;
11351558Srgrimes{
11361558Srgrimes	struct hostlist *hp;
11371558Srgrimes	struct dirlist *dp2;
11381558Srgrimes
11399336Sdfr	if (flags & OP_ALLDIRS) {
11401558Srgrimes		if (ep->ex_defdir)
11411558Srgrimes			free((caddr_t)dp);
11421558Srgrimes		else
11431558Srgrimes			ep->ex_defdir = dp;
11449336Sdfr		if (grp == (struct grouplist *)NULL) {
11451558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
11469336Sdfr			if (flags & OP_KERB)
11479336Sdfr				ep->ex_defdir->dp_flag |= DP_KERB;
11489336Sdfr		} else while (grp) {
11491558Srgrimes			hp = get_ht();
11509336Sdfr			if (flags & OP_KERB)
11519336Sdfr				hp->ht_flag |= DP_KERB;
11521558Srgrimes			hp->ht_grp = grp;
11531558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
11541558Srgrimes			ep->ex_defdir->dp_hosts = hp;
11551558Srgrimes			grp = grp->gr_next;
11561558Srgrimes		}
11571558Srgrimes	} else {
11581558Srgrimes
11591558Srgrimes		/*
116037663Scharnier		 * Loop through the directories adding them to the tree.
11611558Srgrimes		 */
11621558Srgrimes		while (dp) {
11631558Srgrimes			dp2 = dp->dp_left;
11649336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
11651558Srgrimes			dp = dp2;
11661558Srgrimes		}
11671558Srgrimes	}
11681558Srgrimes}
11691558Srgrimes
11701558Srgrimes/*
11711558Srgrimes * Traverse the binary tree either updating a node that is already there
11721558Srgrimes * for the new directory or adding the new node.
11731558Srgrimes */
11741558Srgrimesvoid
11759336Sdfradd_dlist(dpp, newdp, grp, flags)
11761558Srgrimes	struct dirlist **dpp;
11771558Srgrimes	struct dirlist *newdp;
11781558Srgrimes	struct grouplist *grp;
11799336Sdfr	int flags;
11801558Srgrimes{
11811558Srgrimes	struct dirlist *dp;
11821558Srgrimes	struct hostlist *hp;
11831558Srgrimes	int cmp;
11841558Srgrimes
11851558Srgrimes	dp = *dpp;
11861558Srgrimes	if (dp) {
11871558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
11881558Srgrimes		if (cmp > 0) {
11899336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
11901558Srgrimes			return;
11911558Srgrimes		} else if (cmp < 0) {
11929336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
11931558Srgrimes			return;
11941558Srgrimes		} else
11951558Srgrimes			free((caddr_t)newdp);
11961558Srgrimes	} else {
11971558Srgrimes		dp = newdp;
11981558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
11991558Srgrimes		*dpp = dp;
12001558Srgrimes	}
12011558Srgrimes	if (grp) {
12021558Srgrimes
12031558Srgrimes		/*
12041558Srgrimes		 * Hang all of the host(s) off of the directory point.
12051558Srgrimes		 */
12061558Srgrimes		do {
12071558Srgrimes			hp = get_ht();
12089336Sdfr			if (flags & OP_KERB)
12099336Sdfr				hp->ht_flag |= DP_KERB;
12101558Srgrimes			hp->ht_grp = grp;
12111558Srgrimes			hp->ht_next = dp->dp_hosts;
12121558Srgrimes			dp->dp_hosts = hp;
12131558Srgrimes			grp = grp->gr_next;
12141558Srgrimes		} while (grp);
12159336Sdfr	} else {
12161558Srgrimes		dp->dp_flag |= DP_DEFSET;
12179336Sdfr		if (flags & OP_KERB)
12189336Sdfr			dp->dp_flag |= DP_KERB;
12199336Sdfr	}
12201558Srgrimes}
12211558Srgrimes
12221558Srgrimes/*
12231558Srgrimes * Search for a dirpath on the export point.
12241558Srgrimes */
12251558Srgrimesstruct dirlist *
12261558Srgrimesdirp_search(dp, dirpath)
12271558Srgrimes	struct dirlist *dp;
12281558Srgrimes	char *dirpath;
12291558Srgrimes{
12301558Srgrimes	int cmp;
12311558Srgrimes
12321558Srgrimes	if (dp) {
12331558Srgrimes		cmp = strcmp(dp->dp_dirp, dirpath);
12341558Srgrimes		if (cmp > 0)
12351558Srgrimes			return (dirp_search(dp->dp_left, dirpath));
12361558Srgrimes		else if (cmp < 0)
12371558Srgrimes			return (dirp_search(dp->dp_right, dirpath));
12381558Srgrimes		else
12391558Srgrimes			return (dp);
12401558Srgrimes	}
12411558Srgrimes	return (dp);
12421558Srgrimes}
12431558Srgrimes
12441558Srgrimes/*
12451558Srgrimes * Scan for a host match in a directory tree.
12461558Srgrimes */
12471558Srgrimesint
12489336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
12491558Srgrimes	struct dirlist *dp;
125042144Sdfr	u_int32_t saddr;
12511558Srgrimes	int *defsetp;
12529336Sdfr	int *hostsetp;
12531558Srgrimes{
12541558Srgrimes	struct hostlist *hp;
12551558Srgrimes	struct grouplist *grp;
125642144Sdfr	u_int32_t **addrp;
12571558Srgrimes
12581558Srgrimes	if (dp) {
12591558Srgrimes		if (dp->dp_flag & DP_DEFSET)
12609336Sdfr			*defsetp = dp->dp_flag;
12611558Srgrimes		hp = dp->dp_hosts;
12621558Srgrimes		while (hp) {
12631558Srgrimes			grp = hp->ht_grp;
12641558Srgrimes			switch (grp->gr_type) {
12651558Srgrimes			case GT_HOST:
126642144Sdfr			    addrp = (u_int32_t **)
12671558Srgrimes				grp->gr_ptr.gt_hostent->h_addr_list;
12681558Srgrimes			    while (*addrp) {
12699336Sdfr				if (**addrp == saddr) {
12709336Sdfr				    *hostsetp = (hp->ht_flag | DP_HOSTSET);
12711558Srgrimes				    return (1);
12729336Sdfr				}
12731558Srgrimes				addrp++;
12741558Srgrimes			    }
12751558Srgrimes			    break;
12761558Srgrimes			case GT_NET:
12771558Srgrimes			    if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
12789336Sdfr				grp->gr_ptr.gt_net.nt_net) {
12799336Sdfr				*hostsetp = (hp->ht_flag | DP_HOSTSET);
12801558Srgrimes				return (1);
12819336Sdfr			    }
12821558Srgrimes			    break;
12831558Srgrimes			};
12841558Srgrimes			hp = hp->ht_next;
12851558Srgrimes		}
12861558Srgrimes	}
12871558Srgrimes	return (0);
12881558Srgrimes}
12891558Srgrimes
12901558Srgrimes/*
12911558Srgrimes * Scan tree for a host that matches the address.
12921558Srgrimes */
12931558Srgrimesint
12941558Srgrimesscan_tree(dp, saddr)
12951558Srgrimes	struct dirlist *dp;
129642144Sdfr	u_int32_t saddr;
12971558Srgrimes{
12989336Sdfr	int defset, hostset;
12991558Srgrimes
13001558Srgrimes	if (dp) {
13011558Srgrimes		if (scan_tree(dp->dp_left, saddr))
13021558Srgrimes			return (1);
13039336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
13041558Srgrimes			return (1);
13051558Srgrimes		if (scan_tree(dp->dp_right, saddr))
13061558Srgrimes			return (1);
13071558Srgrimes	}
13081558Srgrimes	return (0);
13091558Srgrimes}
13101558Srgrimes
13111558Srgrimes/*
13121558Srgrimes * Traverse the dirlist tree and free it up.
13131558Srgrimes */
13141558Srgrimesvoid
13151558Srgrimesfree_dir(dp)
13161558Srgrimes	struct dirlist *dp;
13171558Srgrimes{
13181558Srgrimes
13191558Srgrimes	if (dp) {
13201558Srgrimes		free_dir(dp->dp_left);
13211558Srgrimes		free_dir(dp->dp_right);
13221558Srgrimes		free_host(dp->dp_hosts);
13231558Srgrimes		free((caddr_t)dp);
13241558Srgrimes	}
13251558Srgrimes}
13261558Srgrimes
13271558Srgrimes/*
13281558Srgrimes * Parse the option string and update fields.
13291558Srgrimes * Option arguments may either be -<option>=<value> or
13301558Srgrimes * -<option> <value>
13311558Srgrimes */
13321558Srgrimesint
13331558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
13341558Srgrimes	char **cpp, **endcpp;
13351558Srgrimes	struct exportlist *ep;
13361558Srgrimes	struct grouplist *grp;
13371558Srgrimes	int *has_hostp;
13381558Srgrimes	int *exflagsp;
13391558Srgrimes	struct ucred *cr;
13401558Srgrimes{
13411558Srgrimes	char *cpoptarg, *cpoptend;
13421558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
13431558Srgrimes	int allflag, usedarg;
13441558Srgrimes
13451558Srgrimes	cpopt = *cpp;
13461558Srgrimes	cpopt++;
13471558Srgrimes	cp = *endcpp;
13481558Srgrimes	savedc = *cp;
13491558Srgrimes	*cp = '\0';
13501558Srgrimes	while (cpopt && *cpopt) {
13511558Srgrimes		allflag = 1;
13521558Srgrimes		usedarg = -2;
135337663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
13541558Srgrimes			*cpoptend++ = '\0';
135537663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
13561558Srgrimes				*cpoptarg++ = '\0';
13571558Srgrimes		} else {
135837663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
13591558Srgrimes				*cpoptarg++ = '\0';
13601558Srgrimes			else {
13611558Srgrimes				*cp = savedc;
13621558Srgrimes				nextfield(&cp, &endcp);
13631558Srgrimes				**endcpp = '\0';
13641558Srgrimes				if (endcp > cp && *cp != '-') {
13651558Srgrimes					cpoptarg = cp;
13661558Srgrimes					savedc2 = *endcp;
13671558Srgrimes					*endcp = '\0';
13681558Srgrimes					usedarg = 0;
13691558Srgrimes				}
13701558Srgrimes			}
13711558Srgrimes		}
13721558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
13731558Srgrimes			*exflagsp |= MNT_EXRDONLY;
13741558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
13751558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
13761558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
13771558Srgrimes			usedarg++;
13781558Srgrimes			parsecred(cpoptarg, cr);
13791558Srgrimes			if (allflag == 0) {
13801558Srgrimes				*exflagsp |= MNT_EXPORTANON;
13811558Srgrimes				opt_flags |= OP_MAPALL;
13821558Srgrimes			} else
13831558Srgrimes				opt_flags |= OP_MAPROOT;
13841558Srgrimes		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
13851558Srgrimes			*exflagsp |= MNT_EXKERB;
13861558Srgrimes			opt_flags |= OP_KERB;
13871558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
13881558Srgrimes			!strcmp(cpopt, "m"))) {
13891558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
139037663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
13911558Srgrimes				return (1);
13921558Srgrimes			}
13931558Srgrimes			usedarg++;
13941558Srgrimes			opt_flags |= OP_MASK;
13951558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
13961558Srgrimes			!strcmp(cpopt, "n"))) {
13971558Srgrimes			if (grp->gr_type != GT_NULL) {
139837663Scharnier				syslog(LOG_ERR, "network/host conflict");
13991558Srgrimes				return (1);
14001558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
140137663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
14021558Srgrimes				return (1);
14031558Srgrimes			}
14041558Srgrimes			grp->gr_type = GT_NET;
14051558Srgrimes			*has_hostp = 1;
14061558Srgrimes			usedarg++;
14071558Srgrimes			opt_flags |= OP_NET;
14081558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
14091558Srgrimes			opt_flags |= OP_ALLDIRS;
141027447Sdfr		} else if (!strcmp(cpopt, "public")) {
141127447Sdfr			*exflagsp |= MNT_EXPUBLIC;
141227447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
141327447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
141427447Sdfr			opt_flags |= OP_MAPALL;
141527447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
141627447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
14171558Srgrimes#ifdef ISO
14181558Srgrimes		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
14191558Srgrimes			if (get_isoaddr(cpoptarg, grp)) {
142037663Scharnier				syslog(LOG_ERR, "bad iso addr: %s", cpoptarg);
14211558Srgrimes				return (1);
14221558Srgrimes			}
14231558Srgrimes			*has_hostp = 1;
14241558Srgrimes			usedarg++;
14251558Srgrimes			opt_flags |= OP_ISO;
14261558Srgrimes#endif /* ISO */
14271558Srgrimes		} else {
142837663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
14291558Srgrimes			return (1);
14301558Srgrimes		}
14311558Srgrimes		if (usedarg >= 0) {
14321558Srgrimes			*endcp = savedc2;
14331558Srgrimes			**endcpp = savedc;
14341558Srgrimes			if (usedarg > 0) {
14351558Srgrimes				*cpp = cp;
14361558Srgrimes				*endcpp = endcp;
14371558Srgrimes			}
14381558Srgrimes			return (0);
14391558Srgrimes		}
14401558Srgrimes		cpopt = cpoptend;
14411558Srgrimes	}
14421558Srgrimes	**endcpp = savedc;
14431558Srgrimes	return (0);
14441558Srgrimes}
14451558Srgrimes
14461558Srgrimes/*
14471558Srgrimes * Translate a character string to the corresponding list of network
14481558Srgrimes * addresses for a hostname.
14491558Srgrimes */
14501558Srgrimesint
14517401Swpaulget_host(cp, grp, tgrp)
14521558Srgrimes	char *cp;
14531558Srgrimes	struct grouplist *grp;
14547401Swpaul	struct grouplist *tgrp;
14551558Srgrimes{
14567401Swpaul	struct grouplist *checkgrp;
14571558Srgrimes	struct hostent *hp, *nhp;
14581558Srgrimes	char **addrp, **naddrp;
14591558Srgrimes	struct hostent t_host;
14601558Srgrimes	int i;
146142144Sdfr	u_int32_t saddr;
14621558Srgrimes	char *aptr[2];
14631558Srgrimes
14641558Srgrimes	if (grp->gr_type != GT_NULL)
14651558Srgrimes		return (1);
14661558Srgrimes	if ((hp = gethostbyname(cp)) == NULL) {
14671558Srgrimes		if (isdigit(*cp)) {
14681558Srgrimes			saddr = inet_addr(cp);
14691558Srgrimes			if (saddr == -1) {
147037663Scharnier 				syslog(LOG_ERR, "inet_addr failed for %s", cp);
14711558Srgrimes				return (1);
14721558Srgrimes			}
14731558Srgrimes			if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
14741558Srgrimes				AF_INET)) == NULL) {
14751558Srgrimes				hp = &t_host;
14761558Srgrimes				hp->h_name = cp;
14771558Srgrimes				hp->h_addrtype = AF_INET;
147842144Sdfr				hp->h_length = sizeof (u_int32_t);
14791558Srgrimes				hp->h_addr_list = aptr;
14801558Srgrimes				aptr[0] = (char *)&saddr;
14811558Srgrimes				aptr[1] = (char *)NULL;
14821558Srgrimes			}
14831558Srgrimes		} else {
148437663Scharnier 			syslog(LOG_ERR, "gethostbyname failed for %s", cp);
14851558Srgrimes			return (1);
14861558Srgrimes		}
14871558Srgrimes	}
14887401Swpaul        /*
14897401Swpaul         * Sanity check: make sure we don't already have an entry
14907401Swpaul         * for this host in the grouplist.
14917401Swpaul         */
14927401Swpaul        checkgrp = tgrp;
149337157Swpaul        while (checkgrp != NULL) {
149417887Swpaul		if (checkgrp->gr_type == GT_HOST &&
149517887Swpaul                    checkgrp->gr_ptr.gt_hostent != NULL &&
149637157Swpaul                    (!strcmp(checkgrp->gr_ptr.gt_hostent->h_name, hp->h_name)
149742144Sdfr		|| *(u_int32_t *)checkgrp->gr_ptr.gt_hostent->h_addr ==
149842144Sdfr			*(u_int32_t *)hp->h_addr)) {
14997401Swpaul                        grp->gr_type = GT_IGNORE;
15007401Swpaul			return(0);
15017401Swpaul		}
15027401Swpaul                checkgrp = checkgrp->gr_next;
15037401Swpaul        }
15047401Swpaul
15051558Srgrimes	grp->gr_type = GT_HOST;
15061558Srgrimes	nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
15071558Srgrimes		malloc(sizeof(struct hostent));
15081558Srgrimes	if (nhp == (struct hostent *)NULL)
15091558Srgrimes		out_of_mem();
151023681Speter	memmove(nhp, hp, sizeof(struct hostent));
15111558Srgrimes	i = strlen(hp->h_name)+1;
15121558Srgrimes	nhp->h_name = (char *)malloc(i);
15131558Srgrimes	if (nhp->h_name == (char *)NULL)
15141558Srgrimes		out_of_mem();
151523681Speter	memmove(nhp->h_name, hp->h_name, i);
15161558Srgrimes	addrp = hp->h_addr_list;
15171558Srgrimes	i = 1;
15181558Srgrimes	while (*addrp++)
15191558Srgrimes		i++;
152037663Scharnier	naddrp = nhp->h_addr_list = (char **)malloc(i*sizeof(char *));
15211558Srgrimes	if (naddrp == (char **)NULL)
15221558Srgrimes		out_of_mem();
15231558Srgrimes	addrp = hp->h_addr_list;
15241558Srgrimes	while (*addrp) {
152537663Scharnier		*naddrp = (char *)malloc(hp->h_length);
15261558Srgrimes		if (*naddrp == (char *)NULL)
15271558Srgrimes		    out_of_mem();
152823681Speter		memmove(*naddrp, *addrp, hp->h_length);
15291558Srgrimes		addrp++;
15301558Srgrimes		naddrp++;
15311558Srgrimes	}
15321558Srgrimes	*naddrp = (char *)NULL;
15331558Srgrimes	if (debug)
153437663Scharnier		warnx("got host %s", hp->h_name);
15351558Srgrimes	return (0);
15361558Srgrimes}
15371558Srgrimes
15381558Srgrimes/*
15391558Srgrimes * Free up an exports list component
15401558Srgrimes */
15411558Srgrimesvoid
15421558Srgrimesfree_exp(ep)
15431558Srgrimes	struct exportlist *ep;
15441558Srgrimes{
15451558Srgrimes
15461558Srgrimes	if (ep->ex_defdir) {
15471558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
15481558Srgrimes		free((caddr_t)ep->ex_defdir);
15491558Srgrimes	}
15501558Srgrimes	if (ep->ex_fsdir)
15511558Srgrimes		free(ep->ex_fsdir);
155227447Sdfr	if (ep->ex_indexfile)
155327447Sdfr		free(ep->ex_indexfile);
15541558Srgrimes	free_dir(ep->ex_dirl);
15551558Srgrimes	free((caddr_t)ep);
15561558Srgrimes}
15571558Srgrimes
15581558Srgrimes/*
15591558Srgrimes * Free hosts.
15601558Srgrimes */
15611558Srgrimesvoid
15621558Srgrimesfree_host(hp)
15631558Srgrimes	struct hostlist *hp;
15641558Srgrimes{
15651558Srgrimes	struct hostlist *hp2;
15661558Srgrimes
15671558Srgrimes	while (hp) {
15681558Srgrimes		hp2 = hp;
15691558Srgrimes		hp = hp->ht_next;
15701558Srgrimes		free((caddr_t)hp2);
15711558Srgrimes	}
15721558Srgrimes}
15731558Srgrimes
15741558Srgrimesstruct hostlist *
15751558Srgrimesget_ht()
15761558Srgrimes{
15771558Srgrimes	struct hostlist *hp;
15781558Srgrimes
15791558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
15801558Srgrimes	if (hp == (struct hostlist *)NULL)
15811558Srgrimes		out_of_mem();
15821558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
15839336Sdfr	hp->ht_flag = 0;
15841558Srgrimes	return (hp);
15851558Srgrimes}
15861558Srgrimes
15871558Srgrimes#ifdef ISO
15881558Srgrimes/*
15891558Srgrimes * Translate an iso address.
15901558Srgrimes */
15911558Srgrimesget_isoaddr(cp, grp)
15921558Srgrimes	char *cp;
15931558Srgrimes	struct grouplist *grp;
15941558Srgrimes{
15951558Srgrimes	struct iso_addr *isop;
15961558Srgrimes	struct sockaddr_iso *isoaddr;
15971558Srgrimes
15981558Srgrimes	if (grp->gr_type != GT_NULL)
15991558Srgrimes		return (1);
16001558Srgrimes	if ((isop = iso_addr(cp)) == NULL) {
160137663Scharnier		syslog(LOG_ERR, "iso_addr failed, ignored");
16021558Srgrimes		return (1);
16031558Srgrimes	}
160437663Scharnier	isoaddr = (struct sockaddr_iso *)malloc(sizeof (struct sockaddr_iso));
16051558Srgrimes	if (isoaddr == (struct sockaddr_iso *)NULL)
16061558Srgrimes		out_of_mem();
160723681Speter	memset(isoaddr, 0, sizeof(struct sockaddr_iso));
160823681Speter	memmove(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
160923681Speter	isoaddr->siso_len = sizeof(struct sockaddr_iso);
16101558Srgrimes	isoaddr->siso_family = AF_ISO;
16111558Srgrimes	grp->gr_type = GT_ISO;
16121558Srgrimes	grp->gr_ptr.gt_isoaddr = isoaddr;
16131558Srgrimes	return (0);
16141558Srgrimes}
16151558Srgrimes#endif	/* ISO */
16161558Srgrimes
16171558Srgrimes/*
16181558Srgrimes * Out of memory, fatal
16191558Srgrimes */
16201558Srgrimesvoid
16211558Srgrimesout_of_mem()
16221558Srgrimes{
16231558Srgrimes
162437663Scharnier	syslog(LOG_ERR, "out of memory");
16251558Srgrimes	exit(2);
16261558Srgrimes}
16271558Srgrimes
16281558Srgrimes/*
16291558Srgrimes * Do the mount syscall with the update flag to push the export info into
16301558Srgrimes * the kernel.
16311558Srgrimes */
16321558Srgrimesint
16331558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
16341558Srgrimes	struct exportlist *ep;
16351558Srgrimes	struct grouplist *grp;
16361558Srgrimes	int exflags;
16371558Srgrimes	struct ucred *anoncrp;
16381558Srgrimes	char *dirp;
16391558Srgrimes	int dirplen;
16401558Srgrimes	struct statfs *fsb;
16411558Srgrimes{
16421558Srgrimes	char *cp = (char *)NULL;
164342144Sdfr	u_int32_t **addrp;
16441558Srgrimes	int done;
16451558Srgrimes	char savedc = '\0';
16461558Srgrimes	struct sockaddr_in sin, imask;
16471558Srgrimes	union {
16481558Srgrimes		struct ufs_args ua;
16491558Srgrimes		struct iso_args ia;
16501558Srgrimes		struct mfs_args ma;
16519336Sdfr#ifdef __NetBSD__
16529336Sdfr		struct msdosfs_args da;
16539336Sdfr#endif
16541558Srgrimes	} args;
165542144Sdfr	u_int32_t net;
16561558Srgrimes
16571558Srgrimes	args.ua.fspec = 0;
16581558Srgrimes	args.ua.export.ex_flags = exflags;
16591558Srgrimes	args.ua.export.ex_anon = *anoncrp;
166027447Sdfr	args.ua.export.ex_indexfile = ep->ex_indexfile;
166123681Speter	memset(&sin, 0, sizeof(sin));
166223681Speter	memset(&imask, 0, sizeof(imask));
16631558Srgrimes	sin.sin_family = AF_INET;
16641558Srgrimes	sin.sin_len = sizeof(sin);
16651558Srgrimes	imask.sin_family = AF_INET;
16661558Srgrimes	imask.sin_len = sizeof(sin);
16671558Srgrimes	if (grp->gr_type == GT_HOST)
166842144Sdfr		addrp = (u_int32_t **)grp->gr_ptr.gt_hostent->h_addr_list;
16691558Srgrimes	else
167042144Sdfr		addrp = (u_int32_t **)NULL;
16711558Srgrimes	done = FALSE;
16721558Srgrimes	while (!done) {
16731558Srgrimes		switch (grp->gr_type) {
16741558Srgrimes		case GT_HOST:
16751558Srgrimes			if (addrp) {
16761558Srgrimes				sin.sin_addr.s_addr = **addrp;
16771558Srgrimes				args.ua.export.ex_addrlen = sizeof(sin);
16781558Srgrimes			} else
16791558Srgrimes				args.ua.export.ex_addrlen = 0;
16801558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16811558Srgrimes			args.ua.export.ex_masklen = 0;
16821558Srgrimes			break;
16831558Srgrimes		case GT_NET:
16841558Srgrimes			if (grp->gr_ptr.gt_net.nt_mask)
16851558Srgrimes			    imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
16861558Srgrimes			else {
16871558Srgrimes			    net = ntohl(grp->gr_ptr.gt_net.nt_net);
16881558Srgrimes			    if (IN_CLASSA(net))
16891558Srgrimes				imask.sin_addr.s_addr = inet_addr("255.0.0.0");
16901558Srgrimes			    else if (IN_CLASSB(net))
16911558Srgrimes				imask.sin_addr.s_addr =
16921558Srgrimes				    inet_addr("255.255.0.0");
16931558Srgrimes			    else
16941558Srgrimes				imask.sin_addr.s_addr =
16951558Srgrimes				    inet_addr("255.255.255.0");
16961558Srgrimes			    grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
16971558Srgrimes			}
16981558Srgrimes			sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
16991558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
17001558Srgrimes			args.ua.export.ex_addrlen = sizeof (sin);
17011558Srgrimes			args.ua.export.ex_mask = (struct sockaddr *)&imask;
17021558Srgrimes			args.ua.export.ex_masklen = sizeof (imask);
17031558Srgrimes			break;
17041558Srgrimes#ifdef ISO
17051558Srgrimes		case GT_ISO:
17061558Srgrimes			args.ua.export.ex_addr =
17071558Srgrimes				(struct sockaddr *)grp->gr_ptr.gt_isoaddr;
17081558Srgrimes			args.ua.export.ex_addrlen =
17091558Srgrimes				sizeof(struct sockaddr_iso);
17101558Srgrimes			args.ua.export.ex_masklen = 0;
17111558Srgrimes			break;
17121558Srgrimes#endif	/* ISO */
17137401Swpaul		case GT_IGNORE:
17147401Swpaul			return(0);
17157401Swpaul			break;
17161558Srgrimes		default:
171737663Scharnier			syslog(LOG_ERR, "bad grouptype");
17181558Srgrimes			if (cp)
17191558Srgrimes				*cp = savedc;
17201558Srgrimes			return (1);
17211558Srgrimes		};
17221558Srgrimes
17231558Srgrimes		/*
17241558Srgrimes		 * XXX:
17251558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
17261558Srgrimes		 * of looping back up the dirp to the mount point??
17271558Srgrimes		 * Also, needs to know how to export all types of local
172823681Speter		 * exportable file systems and not just "ufs".
17291558Srgrimes		 */
17309336Sdfr		while (mount(fsb->f_fstypename, dirp,
17311558Srgrimes		       fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
17321558Srgrimes			if (cp)
17331558Srgrimes				*cp-- = savedc;
17341558Srgrimes			else
17351558Srgrimes				cp = dirp + dirplen - 1;
17361558Srgrimes			if (errno == EPERM) {
17371558Srgrimes				syslog(LOG_ERR,
173837663Scharnier				   "can't change attributes for %s", dirp);
17391558Srgrimes				return (1);
17401558Srgrimes			}
17411558Srgrimes			if (opt_flags & OP_ALLDIRS) {
174237663Scharnier				syslog(LOG_ERR, "could not remount %s: %m",
17434895Swollman					dirp);
17441558Srgrimes				return (1);
17451558Srgrimes			}
17461558Srgrimes			/* back up over the last component */
17471558Srgrimes			while (*cp == '/' && cp > dirp)
17481558Srgrimes				cp--;
17491558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
17501558Srgrimes				cp--;
17511558Srgrimes			if (cp == dirp) {
17521558Srgrimes				if (debug)
175337663Scharnier					warnx("mnt unsucc");
175437663Scharnier				syslog(LOG_ERR, "can't export %s", dirp);
17551558Srgrimes				return (1);
17561558Srgrimes			}
17571558Srgrimes			savedc = *cp;
17581558Srgrimes			*cp = '\0';
17591558Srgrimes		}
17601558Srgrimes		if (addrp) {
17611558Srgrimes			++addrp;
176242144Sdfr			if (*addrp == (u_int32_t *)NULL)
17631558Srgrimes				done = TRUE;
17641558Srgrimes		} else
17651558Srgrimes			done = TRUE;
17661558Srgrimes	}
17671558Srgrimes	if (cp)
17681558Srgrimes		*cp = savedc;
17691558Srgrimes	return (0);
17701558Srgrimes}
17711558Srgrimes
17721558Srgrimes/*
17731558Srgrimes * Translate a net address.
17741558Srgrimes */
17751558Srgrimesint
17761558Srgrimesget_net(cp, net, maskflg)
17771558Srgrimes	char *cp;
17781558Srgrimes	struct netmsk *net;
17791558Srgrimes	int maskflg;
17801558Srgrimes{
17811558Srgrimes	struct netent *np;
17821558Srgrimes	long netaddr;
17831558Srgrimes	struct in_addr inetaddr, inetaddr2;
17841558Srgrimes	char *name;
17851558Srgrimes
178625318Spst	if (isdigit(*cp) && ((netaddr = inet_network(cp)) != -1)) {
17871558Srgrimes		inetaddr = inet_makeaddr(netaddr, 0);
17881558Srgrimes		/*
178937663Scharnier		 * Due to arbitrary subnet masks, you don't know how many
17901558Srgrimes		 * bits to shift the address to make it into a network,
17911558Srgrimes		 * however you do know how to make a network address into
17921558Srgrimes		 * a host with host == 0 and then compare them.
17931558Srgrimes		 * (What a pest)
17941558Srgrimes		 */
17951558Srgrimes		if (!maskflg) {
17961558Srgrimes			setnetent(0);
179737663Scharnier			while ((np = getnetent())) {
17981558Srgrimes				inetaddr2 = inet_makeaddr(np->n_net, 0);
17991558Srgrimes				if (inetaddr2.s_addr == inetaddr.s_addr)
18001558Srgrimes					break;
18011558Srgrimes			}
18021558Srgrimes			endnetent();
18031558Srgrimes		}
180425318Spst	} else if ((np = getnetbyname(cp)) != NULL) {
180525318Spst		inetaddr = inet_makeaddr(np->n_net, 0);
18061558Srgrimes	} else
18071558Srgrimes		return (1);
180825318Spst
18091558Srgrimes	if (maskflg)
18101558Srgrimes		net->nt_mask = inetaddr.s_addr;
18111558Srgrimes	else {
18121558Srgrimes		if (np)
18131558Srgrimes			name = np->n_name;
18141558Srgrimes		else
18151558Srgrimes			name = inet_ntoa(inetaddr);
18161558Srgrimes		net->nt_name = (char *)malloc(strlen(name) + 1);
18171558Srgrimes		if (net->nt_name == (char *)NULL)
18181558Srgrimes			out_of_mem();
18191558Srgrimes		strcpy(net->nt_name, name);
18201558Srgrimes		net->nt_net = inetaddr.s_addr;
18211558Srgrimes	}
18221558Srgrimes	return (0);
18231558Srgrimes}
18241558Srgrimes
18251558Srgrimes/*
18261558Srgrimes * Parse out the next white space separated field
18271558Srgrimes */
18281558Srgrimesvoid
18291558Srgrimesnextfield(cp, endcp)
18301558Srgrimes	char **cp;
18311558Srgrimes	char **endcp;
18321558Srgrimes{
18331558Srgrimes	char *p;
18341558Srgrimes
18351558Srgrimes	p = *cp;
18361558Srgrimes	while (*p == ' ' || *p == '\t')
18371558Srgrimes		p++;
18381558Srgrimes	if (*p == '\n' || *p == '\0')
18391558Srgrimes		*cp = *endcp = p;
18401558Srgrimes	else {
18411558Srgrimes		*cp = p++;
18421558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
18431558Srgrimes			p++;
18441558Srgrimes		*endcp = p;
18451558Srgrimes	}
18461558Srgrimes}
18471558Srgrimes
18481558Srgrimes/*
18491558Srgrimes * Get an exports file line. Skip over blank lines and handle line
18501558Srgrimes * continuations.
18511558Srgrimes */
18521558Srgrimesint
18531558Srgrimesget_line()
18541558Srgrimes{
18551558Srgrimes	char *p, *cp;
18561558Srgrimes	int len;
18571558Srgrimes	int totlen, cont_line;
18581558Srgrimes
18591558Srgrimes	/*
18601558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
18611558Srgrimes	 */
18621558Srgrimes	p = line;
18631558Srgrimes	totlen = 0;
18641558Srgrimes	do {
18651558Srgrimes		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
18661558Srgrimes			return (0);
18671558Srgrimes		len = strlen(p);
18681558Srgrimes		cp = p + len - 1;
18691558Srgrimes		cont_line = 0;
18701558Srgrimes		while (cp >= p &&
18711558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
18721558Srgrimes			if (*cp == '\\')
18731558Srgrimes				cont_line = 1;
18741558Srgrimes			cp--;
18751558Srgrimes			len--;
18761558Srgrimes		}
18771558Srgrimes		*++cp = '\0';
18781558Srgrimes		if (len > 0) {
18791558Srgrimes			totlen += len;
18801558Srgrimes			if (totlen >= LINESIZ) {
188137663Scharnier				syslog(LOG_ERR, "exports line too long");
18821558Srgrimes				exit(2);
18831558Srgrimes			}
18841558Srgrimes			p = cp;
18851558Srgrimes		}
18861558Srgrimes	} while (totlen == 0 || cont_line);
18871558Srgrimes	return (1);
18881558Srgrimes}
18891558Srgrimes
18901558Srgrimes/*
18911558Srgrimes * Parse a description of a credential.
18921558Srgrimes */
18931558Srgrimesvoid
18941558Srgrimesparsecred(namelist, cr)
18951558Srgrimes	char *namelist;
18961558Srgrimes	struct ucred *cr;
18971558Srgrimes{
18981558Srgrimes	char *name;
18991558Srgrimes	int cnt;
19001558Srgrimes	char *names;
19011558Srgrimes	struct passwd *pw;
19021558Srgrimes	struct group *gr;
19031558Srgrimes	int ngroups, groups[NGROUPS + 1];
19041558Srgrimes
19051558Srgrimes	/*
190637663Scharnier	 * Set up the unprivileged user.
19071558Srgrimes	 */
19081558Srgrimes	cr->cr_ref = 1;
19091558Srgrimes	cr->cr_uid = -2;
19101558Srgrimes	cr->cr_groups[0] = -2;
19111558Srgrimes	cr->cr_ngroups = 1;
19121558Srgrimes	/*
19131558Srgrimes	 * Get the user's password table entry.
19141558Srgrimes	 */
19151558Srgrimes	names = strsep(&namelist, " \t\n");
19161558Srgrimes	name = strsep(&names, ":");
19171558Srgrimes	if (isdigit(*name) || *name == '-')
19181558Srgrimes		pw = getpwuid(atoi(name));
19191558Srgrimes	else
19201558Srgrimes		pw = getpwnam(name);
19211558Srgrimes	/*
19221558Srgrimes	 * Credentials specified as those of a user.
19231558Srgrimes	 */
19241558Srgrimes	if (names == NULL) {
19251558Srgrimes		if (pw == NULL) {
192637663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
19271558Srgrimes			return;
19281558Srgrimes		}
19291558Srgrimes		cr->cr_uid = pw->pw_uid;
19301558Srgrimes		ngroups = NGROUPS + 1;
19311558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
193237663Scharnier			syslog(LOG_ERR, "too many groups");
19331558Srgrimes		/*
19341558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
19351558Srgrimes		 */
19361558Srgrimes		cr->cr_ngroups = ngroups - 1;
19371558Srgrimes		cr->cr_groups[0] = groups[0];
19381558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
19391558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
19401558Srgrimes		return;
19411558Srgrimes	}
19421558Srgrimes	/*
19431558Srgrimes	 * Explicit credential specified as a colon separated list:
19441558Srgrimes	 *	uid:gid:gid:...
19451558Srgrimes	 */
19461558Srgrimes	if (pw != NULL)
19471558Srgrimes		cr->cr_uid = pw->pw_uid;
19481558Srgrimes	else if (isdigit(*name) || *name == '-')
19491558Srgrimes		cr->cr_uid = atoi(name);
19501558Srgrimes	else {
195137663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
19521558Srgrimes		return;
19531558Srgrimes	}
19541558Srgrimes	cr->cr_ngroups = 0;
19551558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
19561558Srgrimes		name = strsep(&names, ":");
19571558Srgrimes		if (isdigit(*name) || *name == '-') {
19581558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
19591558Srgrimes		} else {
19601558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
196137663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
19621558Srgrimes				continue;
19631558Srgrimes			}
19641558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
19651558Srgrimes		}
19661558Srgrimes	}
19671558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
196837663Scharnier		syslog(LOG_ERR, "too many groups");
19691558Srgrimes}
19701558Srgrimes
19711558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
19721558Srgrimes/*
19731558Srgrimes * Routines that maintain the remote mounttab
19741558Srgrimes */
19751558Srgrimesvoid
19761558Srgrimesget_mountlist()
19771558Srgrimes{
19781558Srgrimes	struct mountlist *mlp, **mlpp;
197923681Speter	char *host, *dirp, *cp;
19801558Srgrimes	char str[STRSIZ];
19811558Srgrimes	FILE *mlfile;
19821558Srgrimes
19831558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
198437663Scharnier		syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
19851558Srgrimes		return;
19861558Srgrimes	}
19871558Srgrimes	mlpp = &mlhead;
19881558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
198923681Speter		cp = str;
199023681Speter		host = strsep(&cp, " \t\n");
199123681Speter		dirp = strsep(&cp, " \t\n");
199223681Speter		if (host == NULL || dirp == NULL)
19931558Srgrimes			continue;
19941558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
199537663Scharnier		if (mlp == (struct mountlist *)NULL)
199637663Scharnier			out_of_mem();
199723681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
199823681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
199923681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
200023681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
20011558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
20021558Srgrimes		*mlpp = mlp;
20031558Srgrimes		mlpp = &mlp->ml_next;
20041558Srgrimes	}
20051558Srgrimes	fclose(mlfile);
20061558Srgrimes}
20071558Srgrimes
20081558Srgrimesvoid
20091558Srgrimesdel_mlist(hostp, dirp)
20101558Srgrimes	char *hostp, *dirp;
20111558Srgrimes{
20121558Srgrimes	struct mountlist *mlp, **mlpp;
20131558Srgrimes	struct mountlist *mlp2;
20141558Srgrimes	FILE *mlfile;
20151558Srgrimes	int fnd = 0;
20161558Srgrimes
20171558Srgrimes	mlpp = &mlhead;
20181558Srgrimes	mlp = mlhead;
20191558Srgrimes	while (mlp) {
20201558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
20211558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
20221558Srgrimes			fnd = 1;
20231558Srgrimes			mlp2 = mlp;
20241558Srgrimes			*mlpp = mlp = mlp->ml_next;
20251558Srgrimes			free((caddr_t)mlp2);
20261558Srgrimes		} else {
20271558Srgrimes			mlpp = &mlp->ml_next;
20281558Srgrimes			mlp = mlp->ml_next;
20291558Srgrimes		}
20301558Srgrimes	}
20311558Srgrimes	if (fnd) {
20321558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
203337663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
20341558Srgrimes			return;
20351558Srgrimes		}
20361558Srgrimes		mlp = mlhead;
20371558Srgrimes		while (mlp) {
20381558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20391558Srgrimes			mlp = mlp->ml_next;
20401558Srgrimes		}
20411558Srgrimes		fclose(mlfile);
20421558Srgrimes	}
20431558Srgrimes}
20441558Srgrimes
20451558Srgrimesvoid
20461558Srgrimesadd_mlist(hostp, dirp)
20471558Srgrimes	char *hostp, *dirp;
20481558Srgrimes{
20491558Srgrimes	struct mountlist *mlp, **mlpp;
20501558Srgrimes	FILE *mlfile;
20511558Srgrimes
20521558Srgrimes	mlpp = &mlhead;
20531558Srgrimes	mlp = mlhead;
20541558Srgrimes	while (mlp) {
20551558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
20561558Srgrimes			return;
20571558Srgrimes		mlpp = &mlp->ml_next;
20581558Srgrimes		mlp = mlp->ml_next;
20591558Srgrimes	}
20601558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
206137663Scharnier	if (mlp == (struct mountlist *)NULL)
206237663Scharnier		out_of_mem();
20631558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
20641558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
20651558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
20661558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
20671558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
20681558Srgrimes	*mlpp = mlp;
20691558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
207037663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
20711558Srgrimes		return;
20721558Srgrimes	}
20731558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20741558Srgrimes	fclose(mlfile);
20751558Srgrimes}
20761558Srgrimes
20771558Srgrimes/*
20781558Srgrimes * This function is called via. SIGTERM when the system is going down.
20791558Srgrimes * It sends a broadcast RPCMNT_UMNTALL.
20801558Srgrimes */
20811558Srgrimesvoid
20821558Srgrimessend_umntall()
20831558Srgrimes{
20841558Srgrimes	(void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
20851558Srgrimes		xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
20861558Srgrimes	exit(0);
20871558Srgrimes}
20881558Srgrimes
20891558Srgrimesint
20901558Srgrimesumntall_each(resultsp, raddr)
20911558Srgrimes	caddr_t resultsp;
20921558Srgrimes	struct sockaddr_in *raddr;
20931558Srgrimes{
20941558Srgrimes	return (1);
20951558Srgrimes}
20961558Srgrimes
20971558Srgrimes/*
20981558Srgrimes * Free up a group list.
20991558Srgrimes */
21001558Srgrimesvoid
21011558Srgrimesfree_grp(grp)
21021558Srgrimes	struct grouplist *grp;
21031558Srgrimes{
21041558Srgrimes	char **addrp;
21051558Srgrimes
21061558Srgrimes	if (grp->gr_type == GT_HOST) {
21071558Srgrimes		if (grp->gr_ptr.gt_hostent->h_name) {
21081558Srgrimes			addrp = grp->gr_ptr.gt_hostent->h_addr_list;
21091558Srgrimes			while (addrp && *addrp)
21101558Srgrimes				free(*addrp++);
21111558Srgrimes			free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
21121558Srgrimes			free(grp->gr_ptr.gt_hostent->h_name);
21131558Srgrimes		}
21141558Srgrimes		free((caddr_t)grp->gr_ptr.gt_hostent);
21151558Srgrimes	} else if (grp->gr_type == GT_NET) {
21161558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
21171558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
21181558Srgrimes	}
21191558Srgrimes#ifdef ISO
21201558Srgrimes	else if (grp->gr_type == GT_ISO)
21211558Srgrimes		free((caddr_t)grp->gr_ptr.gt_isoaddr);
21221558Srgrimes#endif
21231558Srgrimes	free((caddr_t)grp);
21241558Srgrimes}
21251558Srgrimes
21261558Srgrimes#ifdef DEBUG
21271558Srgrimesvoid
21281558SrgrimesSYSLOG(int pri, const char *fmt, ...)
21291558Srgrimes{
21301558Srgrimes	va_list ap;
21311558Srgrimes
21321558Srgrimes	va_start(ap, fmt);
21331558Srgrimes	vfprintf(stderr, fmt, ap);
21341558Srgrimes	va_end(ap);
21351558Srgrimes}
21361558Srgrimes#endif /* DEBUG */
21371558Srgrimes
21381558Srgrimes/*
21391558Srgrimes * Check options for consistency.
21401558Srgrimes */
21411558Srgrimesint
21421558Srgrimescheck_options(dp)
21431558Srgrimes	struct dirlist *dp;
21441558Srgrimes{
21451558Srgrimes
21461558Srgrimes	if (dp == (struct dirlist *)NULL)
21471558Srgrimes	    return (1);
21481558Srgrimes	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
21491558Srgrimes	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
21501558Srgrimes	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
21511558Srgrimes	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
21521558Srgrimes	    return (1);
21531558Srgrimes	}
21541558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
21551558Srgrimes	    syslog(LOG_ERR, "-mask requires -net");
21561558Srgrimes	    return (1);
21571558Srgrimes	}
21581558Srgrimes	if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
21591558Srgrimes	    syslog(LOG_ERR, "-net and -iso mutually exclusive");
21601558Srgrimes	    return (1);
21611558Srgrimes	}
21621558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
21631558Srgrimes	    syslog(LOG_ERR, "-alldir has multiple directories");
21641558Srgrimes	    return (1);
21651558Srgrimes	}
21661558Srgrimes	return (0);
21671558Srgrimes}
21681558Srgrimes
21691558Srgrimes/*
21701558Srgrimes * Check an absolute directory path for any symbolic links. Return true
21711558Srgrimes * if no symbolic links are found.
21721558Srgrimes */
21731558Srgrimesint
21741558Srgrimescheck_dirpath(dirp)
21751558Srgrimes	char *dirp;
21761558Srgrimes{
21771558Srgrimes	char *cp;
21781558Srgrimes	int ret = 1;
21791558Srgrimes	struct stat sb;
21801558Srgrimes
21811558Srgrimes	cp = dirp + 1;
21821558Srgrimes	while (*cp && ret) {
21831558Srgrimes		if (*cp == '/') {
21841558Srgrimes			*cp = '\0';
21859336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21861558Srgrimes				ret = 0;
21871558Srgrimes			*cp = '/';
21881558Srgrimes		}
21891558Srgrimes		cp++;
21901558Srgrimes	}
21919336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21921558Srgrimes		ret = 0;
21931558Srgrimes	return (ret);
21941558Srgrimes}
21959336Sdfr
21969336Sdfr/*
21979336Sdfr * Just translate an ascii string to an integer.
21989336Sdfr */
21999336Sdfrint
22009336Sdfrget_num(cp)
22019336Sdfr	register char *cp;
22029336Sdfr{
22039336Sdfr	register int res = 0;
22049336Sdfr
22059336Sdfr	while (*cp) {
22069336Sdfr		if (*cp < '0' || *cp > '9')
22079336Sdfr			return (-1);
22089336Sdfr		res = res * 10 + (*cp++ - '0');
22099336Sdfr	}
22109336Sdfr	return (res);
22119336Sdfr}
2212