mountd.c revision 37003
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1989, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * This code is derived from software contributed to Berkeley by
61558Srgrimes * Herb Hasler and Rick Macklem at The University of Guelph.
71558Srgrimes *
81558Srgrimes * Redistribution and use in source and binary forms, with or without
91558Srgrimes * modification, are permitted provided that the following conditions
101558Srgrimes * are met:
111558Srgrimes * 1. Redistributions of source code must retain the above copyright
121558Srgrimes *    notice, this list of conditions and the following disclaimer.
131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer in the
151558Srgrimes *    documentation and/or other materials provided with the distribution.
161558Srgrimes * 3. All advertising materials mentioning features or use of this software
171558Srgrimes *    must display the following acknowledgement:
181558Srgrimes *	This product includes software developed by the University of
191558Srgrimes *	California, Berkeley and its contributors.
201558Srgrimes * 4. Neither the name of the University nor the names of its contributors
211558Srgrimes *    may be used to endorse or promote products derived from this software
221558Srgrimes *    without specific prior written permission.
231558Srgrimes *
241558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341558Srgrimes * SUCH DAMAGE.
351558Srgrimes */
361558Srgrimes
371558Srgrimes#ifndef lint
381558Srgrimesstatic char copyright[] =
391558Srgrimes"@(#) Copyright (c) 1989, 1993\n\
401558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
412999Swollman#endif /*not lint*/
421558Srgrimes
431558Srgrimes#ifndef lint
4423681Speter/*static char sccsid[] = "@(#)mountd.c	8.15 (Berkeley) 5/1/95"; */
452999Swollmanstatic const char rcsid[] =
4637003Sjoerg	"$Id: mountd.c,v 1.28 1998/01/20 15:22:27 bde Exp $";
472999Swollman#endif /*not lint*/
481558Srgrimes
491558Srgrimes#include <sys/param.h>
501558Srgrimes#include <sys/file.h>
511558Srgrimes#include <sys/ioctl.h>
521558Srgrimes#include <sys/mount.h>
531558Srgrimes#include <sys/socket.h>
541558Srgrimes#include <sys/stat.h>
551558Srgrimes#include <sys/syslog.h>
561558Srgrimes#include <sys/ucred.h>
5724330Sguido#include <sys/sysctl.h>
581558Srgrimes
591558Srgrimes#include <rpc/rpc.h>
601558Srgrimes#include <rpc/pmap_clnt.h>
611558Srgrimes#include <rpc/pmap_prot.h>
621558Srgrimes#ifdef ISO
631558Srgrimes#include <netiso/iso.h>
641558Srgrimes#endif
651558Srgrimes#include <nfs/rpcv2.h>
669336Sdfr#include <nfs/nfsproto.h>
6724330Sguido#include <nfs/nfs.h>
6823681Speter#include <ufs/ufs/ufsmount.h>
6923681Speter#include <msdosfs/msdosfsmount.h>
7023681Speter#include <isofs/cd9660/cd9660_mount.h>	/* XXX need isofs in include */
711558Srgrimes
721558Srgrimes#include <arpa/inet.h>
731558Srgrimes
741558Srgrimes#include <ctype.h>
751558Srgrimes#include <errno.h>
761558Srgrimes#include <grp.h>
771558Srgrimes#include <netdb.h>
781558Srgrimes#include <pwd.h>
791558Srgrimes#include <signal.h>
801558Srgrimes#include <stdio.h>
811558Srgrimes#include <stdlib.h>
821558Srgrimes#include <string.h>
831558Srgrimes#include <unistd.h>
841558Srgrimes#include "pathnames.h"
851558Srgrimes
861558Srgrimes#ifdef DEBUG
871558Srgrimes#include <stdarg.h>
881558Srgrimes#endif
891558Srgrimes
901558Srgrimes/*
911558Srgrimes * Structures for keeping the mount list and export list
921558Srgrimes */
931558Srgrimesstruct mountlist {
941558Srgrimes	struct mountlist *ml_next;
951558Srgrimes	char	ml_host[RPCMNT_NAMELEN+1];
961558Srgrimes	char	ml_dirp[RPCMNT_PATHLEN+1];
971558Srgrimes};
981558Srgrimes
991558Srgrimesstruct dirlist {
1001558Srgrimes	struct dirlist	*dp_left;
1011558Srgrimes	struct dirlist	*dp_right;
1021558Srgrimes	int		dp_flag;
1031558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
1041558Srgrimes	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1051558Srgrimes};
1061558Srgrimes/* dp_flag bits */
1071558Srgrimes#define	DP_DEFSET	0x1
1089336Sdfr#define DP_HOSTSET	0x2
1099336Sdfr#define DP_KERB		0x4
1101558Srgrimes
1111558Srgrimesstruct exportlist {
1121558Srgrimes	struct exportlist *ex_next;
1131558Srgrimes	struct dirlist	*ex_dirl;
1141558Srgrimes	struct dirlist	*ex_defdir;
1151558Srgrimes	int		ex_flag;
1161558Srgrimes	fsid_t		ex_fs;
1171558Srgrimes	char		*ex_fsdir;
11827447Sdfr	char		*ex_indexfile;
1191558Srgrimes};
1201558Srgrimes/* ex_flag bits */
1211558Srgrimes#define	EX_LINKED	0x1
1221558Srgrimes
1231558Srgrimesstruct netmsk {
1241558Srgrimes	u_long	nt_net;
1251558Srgrimes	u_long	nt_mask;
1261558Srgrimes	char *nt_name;
1271558Srgrimes};
1281558Srgrimes
1291558Srgrimesunion grouptypes {
1301558Srgrimes	struct hostent *gt_hostent;
1311558Srgrimes	struct netmsk	gt_net;
1321558Srgrimes#ifdef ISO
1331558Srgrimes	struct sockaddr_iso *gt_isoaddr;
1341558Srgrimes#endif
1351558Srgrimes};
1361558Srgrimes
1371558Srgrimesstruct grouplist {
1381558Srgrimes	int gr_type;
1391558Srgrimes	union grouptypes gr_ptr;
1401558Srgrimes	struct grouplist *gr_next;
1411558Srgrimes};
1421558Srgrimes/* Group types */
1431558Srgrimes#define	GT_NULL		0x0
1441558Srgrimes#define	GT_HOST		0x1
1451558Srgrimes#define	GT_NET		0x2
1461558Srgrimes#define	GT_ISO		0x4
1477401Swpaul#define GT_IGNORE	0x5
1481558Srgrimes
1491558Srgrimesstruct hostlist {
1509336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1511558Srgrimes	struct grouplist *ht_grp;
1521558Srgrimes	struct hostlist	 *ht_next;
1531558Srgrimes};
1541558Srgrimes
1559336Sdfrstruct fhreturn {
1569336Sdfr	int	fhr_flag;
1579336Sdfr	int	fhr_vers;
1589336Sdfr	nfsfh_t	fhr_fh;
1599336Sdfr};
1609336Sdfr
1611558Srgrimes/* Global defs */
1621558Srgrimeschar	*add_expdir __P((struct dirlist **, char *, int));
1631558Srgrimesvoid	add_dlist __P((struct dirlist **, struct dirlist *,
1649336Sdfr				struct grouplist *, int));
1651558Srgrimesvoid	add_mlist __P((char *, char *));
1661558Srgrimesint	check_dirpath __P((char *));
1671558Srgrimesint	check_options __P((struct dirlist *));
1689336Sdfrint	chk_host __P((struct dirlist *, u_long, int *, int *));
1691558Srgrimesvoid	del_mlist __P((char *, char *));
1701558Srgrimesstruct dirlist *dirp_search __P((struct dirlist *, char *));
1711558Srgrimesint	do_mount __P((struct exportlist *, struct grouplist *, int,
1729336Sdfr		struct ucred *, char *, int, struct statfs *));
1731558Srgrimesint	do_opt __P((char **, char **, struct exportlist *, struct grouplist *,
1741558Srgrimes				int *, int *, struct ucred *));
1751558Srgrimesstruct	exportlist *ex_search __P((fsid_t *));
1761558Srgrimesstruct	exportlist *get_exp __P((void));
1771558Srgrimesvoid	free_dir __P((struct dirlist *));
1781558Srgrimesvoid	free_exp __P((struct exportlist *));
1791558Srgrimesvoid	free_grp __P((struct grouplist *));
1801558Srgrimesvoid	free_host __P((struct hostlist *));
1811558Srgrimesvoid	get_exportlist __P((void));
1827401Swpaulint	get_host __P((char *, struct grouplist *, struct grouplist *));
1839336Sdfrint	get_num __P((char *));
1841558Srgrimesstruct hostlist *get_ht __P((void));
1851558Srgrimesint	get_line __P((void));
1861558Srgrimesvoid	get_mountlist __P((void));
1871558Srgrimesint	get_net __P((char *, struct netmsk *, int));
1881558Srgrimesvoid	getexp_err __P((struct exportlist *, struct grouplist *));
1891558Srgrimesstruct grouplist *get_grp __P((void));
1901558Srgrimesvoid	hang_dirp __P((struct dirlist *, struct grouplist *,
1911558Srgrimes				struct exportlist *, int));
1921558Srgrimesvoid	mntsrv __P((struct svc_req *, SVCXPRT *));
1931558Srgrimesvoid	nextfield __P((char **, char **));
1941558Srgrimesvoid	out_of_mem __P((void));
1951558Srgrimesvoid	parsecred __P((char *, struct ucred *));
1961558Srgrimesint	put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
1971558Srgrimesint	scan_tree __P((struct dirlist *, u_long));
1981558Srgrimesvoid	send_umntall __P((void));
1991558Srgrimesint	umntall_each __P((caddr_t, struct sockaddr_in *));
2001558Srgrimesint	xdr_dir __P((XDR *, char *));
2011558Srgrimesint	xdr_explist __P((XDR *, caddr_t));
2029336Sdfrint	xdr_fhs __P((XDR *, caddr_t));
2031558Srgrimesint	xdr_mlist __P((XDR *, caddr_t));
2041558Srgrimes
2051558Srgrimes/* C library */
2061558Srgrimesint	getnetgrent();
2071558Srgrimesvoid	endnetgrent();
2081558Srgrimesvoid	setnetgrent();
2091558Srgrimes
2101558Srgrimes#ifdef ISO
2111558Srgrimesstruct iso_addr *iso_addr();
2121558Srgrimes#endif
2131558Srgrimes
2141558Srgrimesstruct exportlist *exphead;
2151558Srgrimesstruct mountlist *mlhead;
2161558Srgrimesstruct grouplist *grphead;
2171558Srgrimeschar exname[MAXPATHLEN];
2181558Srgrimesstruct ucred def_anon = {
2191558Srgrimes	1,
2201558Srgrimes	(uid_t) -2,
2211558Srgrimes	1,
2221558Srgrimes	{ (gid_t) -2 }
2231558Srgrimes};
22425087Sdfrint force_v2 = 0;
2259336Sdfrint resvport_only = 1;
2269336Sdfrint dir_only = 1;
22731705Sguidoint log = 0;
2281558Srgrimesint opt_flags;
2291558Srgrimes/* Bits for above */
2301558Srgrimes#define	OP_MAPROOT	0x01
2311558Srgrimes#define	OP_MAPALL	0x02
2321558Srgrimes#define	OP_KERB		0x04
2331558Srgrimes#define	OP_MASK		0x08
2341558Srgrimes#define	OP_NET		0x10
2351558Srgrimes#define	OP_ISO		0x20
2361558Srgrimes#define	OP_ALLDIRS	0x40
2371558Srgrimes
2381558Srgrimes#ifdef DEBUG
2391558Srgrimesint debug = 1;
2401558Srgrimesvoid	SYSLOG __P((int, const char *, ...));
2411558Srgrimes#define syslog SYSLOG
2421558Srgrimes#else
2431558Srgrimesint debug = 0;
2441558Srgrimes#endif
2451558Srgrimes
2461558Srgrimes/*
2471558Srgrimes * Mountd server for NFS mount protocol as described in:
2481558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2491558Srgrimes * The optional arguments are the exports file name
2501558Srgrimes * default: _PATH_EXPORTS
2511558Srgrimes * and "-n" to allow nonroot mount.
2521558Srgrimes */
2531558Srgrimesint
2541558Srgrimesmain(argc, argv)
2551558Srgrimes	int argc;
2561558Srgrimes	char **argv;
2571558Srgrimes{
2589202Srgrimes	SVCXPRT *udptransp, *tcptransp;
25932656Sbde	int c, error, mib[3];
26023681Speter	struct vfsconf vfc;
2611558Srgrimes
26223681Speter	error = getvfsbyname("nfs", &vfc);
26323681Speter	if (error && vfsisloadable("nfs")) {
2642999Swollman		if(vfsload("nfs"))
2652999Swollman			err(1, "vfsload(nfs)");
2662999Swollman		endvfsent();	/* flush cache */
26723681Speter		error = getvfsbyname("nfs", &vfc);
2682999Swollman	}
26923681Speter	if (error)
2702999Swollman		errx(1, "NFS support is not available in the running kernel");
2712999Swollman
27231665Sguido	while ((c = getopt(argc, argv, "2dlnr")) != -1)
2731558Srgrimes		switch (c) {
27425087Sdfr		case '2':
27525087Sdfr			force_v2 = 1;
27625087Sdfr			break;
2779336Sdfr		case 'n':
2789336Sdfr			resvport_only = 0;
2799336Sdfr			break;
2809336Sdfr		case 'r':
2819336Sdfr			dir_only = 0;
2829336Sdfr			break;
2838688Sphk		case 'd':
2848688Sphk			debug = debug ? 0 : 1;
2858688Sphk			break;
28631656Sguido		case 'l':
28731656Sguido			log = 1;
28831656Sguido			break;
2891558Srgrimes		default:
29031665Sguido			fprintf(stderr,
29131665Sguido"Usage: mountd [-d] [-l] [-r] [-n] [export_file]\n");
2921558Srgrimes			exit(1);
2931558Srgrimes		};
2941558Srgrimes	argc -= optind;
2951558Srgrimes	argv += optind;
2961558Srgrimes	grphead = (struct grouplist *)NULL;
2971558Srgrimes	exphead = (struct exportlist *)NULL;
2981558Srgrimes	mlhead = (struct mountlist *)NULL;
2991558Srgrimes	if (argc == 1) {
3001558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
3011558Srgrimes		exname[MAXPATHLEN-1] = '\0';
3021558Srgrimes	} else
3031558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3041558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3051558Srgrimes	if (debug)
3061558Srgrimes		fprintf(stderr,"Getting export list.\n");
3071558Srgrimes	get_exportlist();
3081558Srgrimes	if (debug)
3091558Srgrimes		fprintf(stderr,"Getting mount list.\n");
3101558Srgrimes	get_mountlist();
3111558Srgrimes	if (debug)
3121558Srgrimes		fprintf(stderr,"Here we go.\n");
3131558Srgrimes	if (debug == 0) {
3141558Srgrimes		daemon(0, 0);
3151558Srgrimes		signal(SIGINT, SIG_IGN);
3161558Srgrimes		signal(SIGQUIT, SIG_IGN);
3171558Srgrimes	}
3181558Srgrimes	signal(SIGHUP, (void (*) __P((int))) get_exportlist);
3191558Srgrimes	signal(SIGTERM, (void (*) __P((int))) send_umntall);
3201558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3211558Srgrimes	  if (pidfile != NULL) {
3221558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3231558Srgrimes		fclose(pidfile);
3241558Srgrimes	  }
3251558Srgrimes	}
32624759Sguido	if (!resvport_only) {
32724759Sguido		mib[0] = CTL_VFS;
32832656Sbde		mib[1] = vfc.vfc_typenum;
32924759Sguido		mib[2] = NFS_NFSPRIVPORT;
33024759Sguido		if (sysctl(mib, 3, NULL, NULL, &resvport_only,
33124759Sguido		    sizeof(resvport_only)) != 0 && errno != ENOENT) {
33224759Sguido			syslog(LOG_ERR, "sysctl: %m");
33324759Sguido			exit(1);
33424759Sguido		}
33524330Sguido	}
3369202Srgrimes	if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
3379202Srgrimes	    (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
3381558Srgrimes		syslog(LOG_ERR, "Can't create socket");
3391558Srgrimes		exit(1);
3401558Srgrimes	}
3419336Sdfr	pmap_unset(RPCPROG_MNT, 1);
3429336Sdfr	pmap_unset(RPCPROG_MNT, 3);
34325087Sdfr	if (!force_v2)
34425087Sdfr		if (!svc_register(udptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_UDP) ||
34525087Sdfr		    !svc_register(tcptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_TCP)) {
34625087Sdfr			syslog(LOG_ERR, "Can't register mount");
34725087Sdfr			exit(1);
34825087Sdfr		}
3499336Sdfr	if (!svc_register(udptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_UDP) ||
35025087Sdfr	    !svc_register(tcptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_TCP)) {
3511558Srgrimes		syslog(LOG_ERR, "Can't register mount");
3521558Srgrimes		exit(1);
3531558Srgrimes	}
3541558Srgrimes	svc_run();
3551558Srgrimes	syslog(LOG_ERR, "Mountd died");
3561558Srgrimes	exit(1);
3571558Srgrimes}
3581558Srgrimes
3591558Srgrimes/*
3601558Srgrimes * The mount rpc service
3611558Srgrimes */
3621558Srgrimesvoid
3631558Srgrimesmntsrv(rqstp, transp)
3641558Srgrimes	struct svc_req *rqstp;
3651558Srgrimes	SVCXPRT *transp;
3661558Srgrimes{
3671558Srgrimes	struct exportlist *ep;
3681558Srgrimes	struct dirlist *dp;
3699336Sdfr	struct fhreturn fhr;
3701558Srgrimes	struct stat stb;
3711558Srgrimes	struct statfs fsb;
3721558Srgrimes	struct hostent *hp;
37331656Sguido	struct in_addr saddrin;
3741558Srgrimes	u_long saddr;
3759336Sdfr	u_short sport;
37623681Speter	char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
37728911Sguido	int bad = 0, defset, hostset;
3789336Sdfr	sigset_t sighup_mask;
3791558Srgrimes
3809336Sdfr	sigemptyset(&sighup_mask);
3819336Sdfr	sigaddset(&sighup_mask, SIGHUP);
3821558Srgrimes	saddr = transp->xp_raddr.sin_addr.s_addr;
38331656Sguido	saddrin = transp->xp_raddr.sin_addr;
3849336Sdfr	sport = ntohs(transp->xp_raddr.sin_port);
3851558Srgrimes	hp = (struct hostent *)NULL;
3861558Srgrimes	switch (rqstp->rq_proc) {
3871558Srgrimes	case NULLPROC:
3881558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
3891558Srgrimes			syslog(LOG_ERR, "Can't send reply");
3901558Srgrimes		return;
3911558Srgrimes	case RPCMNT_MOUNT:
3929336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
39331656Sguido			syslog(LOG_NOTICE,
39431656Sguido			    "mount request from %s from unprivileged port",
39531656Sguido			    inet_ntoa(saddrin));
3961558Srgrimes			svcerr_weakauth(transp);
3971558Srgrimes			return;
3981558Srgrimes		}
3991558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
40031656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
40131656Sguido			    inet_ntoa(saddrin));
4021558Srgrimes			svcerr_decode(transp);
4031558Srgrimes			return;
4041558Srgrimes		}
4051558Srgrimes
4061558Srgrimes		/*
4071558Srgrimes		 * Get the real pathname and make sure it is a directory
4089336Sdfr		 * or a regular file if the -r option was specified
4099336Sdfr		 * and it exists.
4101558Srgrimes		 */
4111558Srgrimes		if (realpath(rpcpath, dirpath) == 0 ||
4121558Srgrimes		    stat(dirpath, &stb) < 0 ||
4139336Sdfr		    (!S_ISDIR(stb.st_mode) &&
4149336Sdfr		     (dir_only || !S_ISREG(stb.st_mode))) ||
4151558Srgrimes		    statfs(dirpath, &fsb) < 0) {
4161558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
41731656Sguido			syslog(LOG_NOTICE,
41831656Sguido			    "mount request from %s for non existant path %s",
41931656Sguido			    inet_ntoa(saddrin), dirpath);
4201558Srgrimes			if (debug)
4211558Srgrimes				fprintf(stderr, "stat failed on %s\n", dirpath);
42228911Sguido			bad = ENOENT;	/* We will send error reply later */
4231558Srgrimes		}
4241558Srgrimes
4251558Srgrimes		/* Check in the exports list */
4269336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
4271558Srgrimes		ep = ex_search(&fsb.f_fsid);
4289336Sdfr		hostset = defset = 0;
4299336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
4301558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
4319336Sdfr		     chk_host(dp, saddr, &defset, &hostset)) ||
4321558Srgrimes		     (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
4331558Srgrimes		      scan_tree(ep->ex_dirl, saddr) == 0))) {
43428911Sguido			if (bad) {
43528911Sguido				if (!svc_sendreply(transp, xdr_long,
43628911Sguido				    (caddr_t)&bad))
43728911Sguido					syslog(LOG_ERR, "Can't send reply");
43828911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
43928911Sguido				return;
44028911Sguido			}
4419336Sdfr			if (hostset & DP_HOSTSET)
4429336Sdfr				fhr.fhr_flag = hostset;
4439336Sdfr			else
4449336Sdfr				fhr.fhr_flag = defset;
4459336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
4461558Srgrimes			/* Get the file handle */
44723681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
4489336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
4491558Srgrimes				bad = errno;
4501558Srgrimes				syslog(LOG_ERR, "Can't get fh for %s", dirpath);
4511558Srgrimes				if (!svc_sendreply(transp, xdr_long,
4521558Srgrimes				    (caddr_t)&bad))
4531558Srgrimes					syslog(LOG_ERR, "Can't send reply");
4549336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
4551558Srgrimes				return;
4561558Srgrimes			}
4579336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
4581558Srgrimes				syslog(LOG_ERR, "Can't send reply");
4591558Srgrimes			if (hp == NULL)
4601558Srgrimes				hp = gethostbyaddr((caddr_t)&saddr,
4611558Srgrimes				    sizeof(saddr), AF_INET);
4621558Srgrimes			if (hp)
4631558Srgrimes				add_mlist(hp->h_name, dirpath);
4641558Srgrimes			else
46531656Sguido				add_mlist(inet_ntoa(saddrin),
4661558Srgrimes					dirpath);
4671558Srgrimes			if (debug)
4681558Srgrimes				fprintf(stderr,"Mount successfull.\n");
46931656Sguido			if (log)
47031656Sguido				syslog(LOG_NOTICE,
47131656Sguido				    "mount request succeeded from %s for %s",
47231656Sguido				    inet_ntoa(saddrin), dirpath);
47331656Sguido		} else {
4741558Srgrimes			bad = EACCES;
47531656Sguido			syslog(LOG_NOTICE,
47631656Sguido			    "mount request denied from %s for %s",
47731656Sguido			    inet_ntoa(saddrin), dirpath);
47831656Sguido		}
47928911Sguido
48028911Sguido		if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
48128911Sguido			syslog(LOG_ERR, "Can't send reply");
4829336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
4831558Srgrimes		return;
4841558Srgrimes	case RPCMNT_DUMP:
4851558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
4861558Srgrimes			syslog(LOG_ERR, "Can't send reply");
48731656Sguido		else if (log)
48831656Sguido			syslog(LOG_NOTICE,
48931656Sguido			    "dump request succeeded from %s",
49031656Sguido			    inet_ntoa(saddrin), dirpath);
4911558Srgrimes		return;
4921558Srgrimes	case RPCMNT_UMOUNT:
4939336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
49431656Sguido			syslog(LOG_NOTICE,
49531656Sguido			    "umount request from %s from unprivileged port",
49631656Sguido			    inet_ntoa(saddrin));
4971558Srgrimes			svcerr_weakauth(transp);
4981558Srgrimes			return;
4991558Srgrimes		}
5001558Srgrimes		if (!svc_getargs(transp, xdr_dir, dirpath)) {
50131656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
50231656Sguido			    inet_ntoa(saddrin));
5031558Srgrimes			svcerr_decode(transp);
5041558Srgrimes			return;
5051558Srgrimes		}
5061558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
5071558Srgrimes			syslog(LOG_ERR, "Can't send reply");
5081558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
5091558Srgrimes		if (hp)
5101558Srgrimes			del_mlist(hp->h_name, dirpath);
51131656Sguido		del_mlist(inet_ntoa(saddrin), dirpath);
51231656Sguido		if (log)
51331656Sguido			syslog(LOG_NOTICE,
51431656Sguido			    "umount request succeeded from %s for %s",
51531656Sguido			    inet_ntoa(saddrin), dirpath);
5161558Srgrimes		return;
5171558Srgrimes	case RPCMNT_UMNTALL:
5189336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
51931656Sguido			syslog(LOG_NOTICE,
52031656Sguido			    "umountall request from %s from unprivileged port",
52131656Sguido			    inet_ntoa(saddrin));
5221558Srgrimes			svcerr_weakauth(transp);
5231558Srgrimes			return;
5241558Srgrimes		}
5251558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
5261558Srgrimes			syslog(LOG_ERR, "Can't send reply");
5271558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
5281558Srgrimes		if (hp)
5291558Srgrimes			del_mlist(hp->h_name, (char *)NULL);
53031656Sguido		del_mlist(inet_ntoa(saddrin), (char *)NULL);
53131656Sguido		if (log)
53231656Sguido			syslog(LOG_NOTICE,
53331656Sguido			    "umountall request succeeded from %s",
53431656Sguido			    inet_ntoa(saddrin));
5351558Srgrimes		return;
5361558Srgrimes	case RPCMNT_EXPORT:
5371558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
5381558Srgrimes			syslog(LOG_ERR, "Can't send reply");
53931656Sguido		if (log)
54031656Sguido			syslog(LOG_NOTICE,
54131656Sguido			    "export request succeeded from %s",
54231656Sguido			    inet_ntoa(saddrin));
5431558Srgrimes		return;
5441558Srgrimes	default:
5451558Srgrimes		svcerr_noproc(transp);
5461558Srgrimes		return;
5471558Srgrimes	}
5481558Srgrimes}
5491558Srgrimes
5501558Srgrimes/*
5511558Srgrimes * Xdr conversion for a dirpath string
5521558Srgrimes */
5531558Srgrimesint
5541558Srgrimesxdr_dir(xdrsp, dirp)
5551558Srgrimes	XDR *xdrsp;
5561558Srgrimes	char *dirp;
5571558Srgrimes{
5581558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
5591558Srgrimes}
5601558Srgrimes
5611558Srgrimes/*
5629336Sdfr * Xdr routine to generate file handle reply
5631558Srgrimes */
5641558Srgrimesint
5659336Sdfrxdr_fhs(xdrsp, cp)
5661558Srgrimes	XDR *xdrsp;
5679336Sdfr	caddr_t cp;
5681558Srgrimes{
5699336Sdfr	register struct fhreturn *fhrp = (struct fhreturn *)cp;
5709336Sdfr	u_long ok = 0, len, auth;
5711558Srgrimes
5721558Srgrimes	if (!xdr_long(xdrsp, &ok))
5731558Srgrimes		return (0);
5749336Sdfr	switch (fhrp->fhr_vers) {
5759336Sdfr	case 1:
5769336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
5779336Sdfr	case 3:
5789336Sdfr		len = NFSX_V3FH;
5799336Sdfr		if (!xdr_long(xdrsp, &len))
5809336Sdfr			return (0);
5819336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
5829336Sdfr			return (0);
5839336Sdfr		if (fhrp->fhr_flag & DP_KERB)
5849336Sdfr			auth = RPCAUTH_KERB4;
5859336Sdfr		else
5869336Sdfr			auth = RPCAUTH_UNIX;
5879336Sdfr		len = 1;
5889336Sdfr		if (!xdr_long(xdrsp, &len))
5899336Sdfr			return (0);
5909336Sdfr		return (xdr_long(xdrsp, &auth));
5919336Sdfr	};
5929336Sdfr	return (0);
5931558Srgrimes}
5941558Srgrimes
5951558Srgrimesint
5961558Srgrimesxdr_mlist(xdrsp, cp)
5971558Srgrimes	XDR *xdrsp;
5981558Srgrimes	caddr_t cp;
5991558Srgrimes{
6001558Srgrimes	struct mountlist *mlp;
6011558Srgrimes	int true = 1;
6021558Srgrimes	int false = 0;
6031558Srgrimes	char *strp;
6041558Srgrimes
6051558Srgrimes	mlp = mlhead;
6061558Srgrimes	while (mlp) {
6071558Srgrimes		if (!xdr_bool(xdrsp, &true))
6081558Srgrimes			return (0);
6091558Srgrimes		strp = &mlp->ml_host[0];
6101558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
6111558Srgrimes			return (0);
6121558Srgrimes		strp = &mlp->ml_dirp[0];
6131558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6141558Srgrimes			return (0);
6151558Srgrimes		mlp = mlp->ml_next;
6161558Srgrimes	}
6171558Srgrimes	if (!xdr_bool(xdrsp, &false))
6181558Srgrimes		return (0);
6191558Srgrimes	return (1);
6201558Srgrimes}
6211558Srgrimes
6221558Srgrimes/*
6231558Srgrimes * Xdr conversion for export list
6241558Srgrimes */
6251558Srgrimesint
6261558Srgrimesxdr_explist(xdrsp, cp)
6271558Srgrimes	XDR *xdrsp;
6281558Srgrimes	caddr_t cp;
6291558Srgrimes{
6301558Srgrimes	struct exportlist *ep;
6311558Srgrimes	int false = 0;
6329336Sdfr	int putdef;
6339336Sdfr	sigset_t sighup_mask;
6341558Srgrimes
6359336Sdfr	sigemptyset(&sighup_mask);
6369336Sdfr	sigaddset(&sighup_mask, SIGHUP);
6379336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
6381558Srgrimes	ep = exphead;
6391558Srgrimes	while (ep) {
6401558Srgrimes		putdef = 0;
6411558Srgrimes		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
6421558Srgrimes			goto errout;
6431558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
6441558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
6451558Srgrimes			&putdef))
6461558Srgrimes			goto errout;
6471558Srgrimes		ep = ep->ex_next;
6481558Srgrimes	}
6499336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6501558Srgrimes	if (!xdr_bool(xdrsp, &false))
6511558Srgrimes		return (0);
6521558Srgrimes	return (1);
6531558Srgrimeserrout:
6549336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6551558Srgrimes	return (0);
6561558Srgrimes}
6571558Srgrimes
6581558Srgrimes/*
6591558Srgrimes * Called from xdr_explist() to traverse the tree and export the
6601558Srgrimes * directory paths.
6611558Srgrimes */
6621558Srgrimesint
6631558Srgrimesput_exlist(dp, xdrsp, adp, putdefp)
6641558Srgrimes	struct dirlist *dp;
6651558Srgrimes	XDR *xdrsp;
6661558Srgrimes	struct dirlist *adp;
6671558Srgrimes	int *putdefp;
6681558Srgrimes{
6691558Srgrimes	struct grouplist *grp;
6701558Srgrimes	struct hostlist *hp;
6711558Srgrimes	int true = 1;
6721558Srgrimes	int false = 0;
6731558Srgrimes	int gotalldir = 0;
6741558Srgrimes	char *strp;
6751558Srgrimes
6761558Srgrimes	if (dp) {
6771558Srgrimes		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
6781558Srgrimes			return (1);
6791558Srgrimes		if (!xdr_bool(xdrsp, &true))
6801558Srgrimes			return (1);
6811558Srgrimes		strp = dp->dp_dirp;
6821558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6831558Srgrimes			return (1);
6841558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
6851558Srgrimes			gotalldir = 1;
6861558Srgrimes			*putdefp = 1;
6871558Srgrimes		}
6881558Srgrimes		if ((dp->dp_flag & DP_DEFSET) == 0 &&
6891558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
6901558Srgrimes			hp = dp->dp_hosts;
6911558Srgrimes			while (hp) {
6921558Srgrimes				grp = hp->ht_grp;
6931558Srgrimes				if (grp->gr_type == GT_HOST) {
6941558Srgrimes					if (!xdr_bool(xdrsp, &true))
6951558Srgrimes						return (1);
6961558Srgrimes					strp = grp->gr_ptr.gt_hostent->h_name;
6978871Srgrimes					if (!xdr_string(xdrsp, &strp,
6981558Srgrimes					    RPCMNT_NAMELEN))
6991558Srgrimes						return (1);
7001558Srgrimes				} else if (grp->gr_type == GT_NET) {
7011558Srgrimes					if (!xdr_bool(xdrsp, &true))
7021558Srgrimes						return (1);
7031558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
7048871Srgrimes					if (!xdr_string(xdrsp, &strp,
7051558Srgrimes					    RPCMNT_NAMELEN))
7061558Srgrimes						return (1);
7071558Srgrimes				}
7081558Srgrimes				hp = hp->ht_next;
7091558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
7101558Srgrimes					hp = adp->dp_hosts;
7111558Srgrimes					gotalldir = 0;
7121558Srgrimes				}
7131558Srgrimes			}
7141558Srgrimes		}
7151558Srgrimes		if (!xdr_bool(xdrsp, &false))
7161558Srgrimes			return (1);
7171558Srgrimes		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
7181558Srgrimes			return (1);
7191558Srgrimes	}
7201558Srgrimes	return (0);
7211558Srgrimes}
7221558Srgrimes
7231558Srgrimes#define LINESIZ	10240
7241558Srgrimeschar line[LINESIZ];
7251558SrgrimesFILE *exp_file;
7261558Srgrimes
7271558Srgrimes/*
7281558Srgrimes * Get the export list
7291558Srgrimes */
7301558Srgrimesvoid
7311558Srgrimesget_exportlist()
7321558Srgrimes{
7331558Srgrimes	struct exportlist *ep, *ep2;
7341558Srgrimes	struct grouplist *grp, *tgrp;
7351558Srgrimes	struct exportlist **epp;
7361558Srgrimes	struct dirlist *dirhead;
7371558Srgrimes	struct statfs fsb, *fsp;
7381558Srgrimes	struct hostent *hpe;
7391558Srgrimes	struct ucred anon;
7401558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
7411558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
7421558Srgrimes
7431558Srgrimes	/*
7441558Srgrimes	 * First, get rid of the old list
7451558Srgrimes	 */
7461558Srgrimes	ep = exphead;
7471558Srgrimes	while (ep) {
7481558Srgrimes		ep2 = ep;
7491558Srgrimes		ep = ep->ex_next;
7501558Srgrimes		free_exp(ep2);
7511558Srgrimes	}
7521558Srgrimes	exphead = (struct exportlist *)NULL;
7531558Srgrimes
7541558Srgrimes	grp = grphead;
7551558Srgrimes	while (grp) {
7561558Srgrimes		tgrp = grp;
7571558Srgrimes		grp = grp->gr_next;
7581558Srgrimes		free_grp(tgrp);
7591558Srgrimes	}
7601558Srgrimes	grphead = (struct grouplist *)NULL;
7611558Srgrimes
7621558Srgrimes	/*
7631558Srgrimes	 * And delete exports that are in the kernel for all local
7641558Srgrimes	 * file systems.
7651558Srgrimes	 * XXX: Should know how to handle all local exportable file systems
76623681Speter	 *      instead of just "ufs".
7671558Srgrimes	 */
7681558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
7691558Srgrimes	for (i = 0; i < num; i++) {
7701558Srgrimes		union {
7711558Srgrimes			struct ufs_args ua;
7721558Srgrimes			struct iso_args ia;
7731558Srgrimes			struct mfs_args ma;
7749336Sdfr			struct msdosfs_args da;
7751558Srgrimes		} targs;
7761558Srgrimes
77723681Speter		if (!strcmp(fsp->f_fstypename, "mfs") ||
77823681Speter		    !strcmp(fsp->f_fstypename, "ufs") ||
77923681Speter		    !strcmp(fsp->f_fstypename, "msdos") ||
78023681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
7819336Sdfr			targs.ua.fspec = NULL;
7829336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
7839336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
7841558Srgrimes				  fsp->f_flags | MNT_UPDATE,
7851558Srgrimes				  (caddr_t)&targs) < 0)
7861558Srgrimes				syslog(LOG_ERR, "Can't delete exports for %s",
7871558Srgrimes				       fsp->f_mntonname);
7881558Srgrimes		}
7891558Srgrimes		fsp++;
7901558Srgrimes	}
7911558Srgrimes
7921558Srgrimes	/*
7931558Srgrimes	 * Read in the exports file and build the list, calling
7941558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
7951558Srgrimes	 */
7961558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
7971558Srgrimes		syslog(LOG_ERR, "Can't open %s", exname);
7981558Srgrimes		exit(2);
7991558Srgrimes	}
8001558Srgrimes	dirhead = (struct dirlist *)NULL;
8011558Srgrimes	while (get_line()) {
8021558Srgrimes		if (debug)
8031558Srgrimes			fprintf(stderr,"Got line %s\n",line);
8041558Srgrimes		cp = line;
8051558Srgrimes		nextfield(&cp, &endcp);
8061558Srgrimes		if (*cp == '#')
8071558Srgrimes			goto nextline;
8081558Srgrimes
8091558Srgrimes		/*
8101558Srgrimes		 * Set defaults.
8111558Srgrimes		 */
8121558Srgrimes		has_host = FALSE;
8131558Srgrimes		anon = def_anon;
8141558Srgrimes		exflags = MNT_EXPORTED;
8151558Srgrimes		got_nondir = 0;
8161558Srgrimes		opt_flags = 0;
8171558Srgrimes		ep = (struct exportlist *)NULL;
8181558Srgrimes
8191558Srgrimes		/*
8201558Srgrimes		 * Create new exports list entry
8211558Srgrimes		 */
8221558Srgrimes		len = endcp-cp;
8231558Srgrimes		tgrp = grp = get_grp();
8241558Srgrimes		while (len > 0) {
8251558Srgrimes			if (len > RPCMNT_NAMELEN) {
8261558Srgrimes			    getexp_err(ep, tgrp);
8271558Srgrimes			    goto nextline;
8281558Srgrimes			}
8291558Srgrimes			if (*cp == '-') {
8301558Srgrimes			    if (ep == (struct exportlist *)NULL) {
8311558Srgrimes				getexp_err(ep, tgrp);
8321558Srgrimes				goto nextline;
8331558Srgrimes			    }
8341558Srgrimes			    if (debug)
8351558Srgrimes				fprintf(stderr, "doing opt %s\n", cp);
8361558Srgrimes			    got_nondir = 1;
8371558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
8381558Srgrimes				&exflags, &anon)) {
8391558Srgrimes				getexp_err(ep, tgrp);
8401558Srgrimes				goto nextline;
8411558Srgrimes			    }
8421558Srgrimes			} else if (*cp == '/') {
8431558Srgrimes			    savedc = *endcp;
8441558Srgrimes			    *endcp = '\0';
8451558Srgrimes			    if (check_dirpath(cp) &&
8461558Srgrimes				statfs(cp, &fsb) >= 0) {
8471558Srgrimes				if (got_nondir) {
8481558Srgrimes				    syslog(LOG_ERR, "Dirs must be first");
8491558Srgrimes				    getexp_err(ep, tgrp);
8501558Srgrimes				    goto nextline;
8511558Srgrimes				}
8521558Srgrimes				if (ep) {
8531558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
8541558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
8551558Srgrimes					getexp_err(ep, tgrp);
8561558Srgrimes					goto nextline;
8571558Srgrimes				    }
8581558Srgrimes				} else {
8591558Srgrimes				    /*
8601558Srgrimes				     * See if this directory is already
8611558Srgrimes				     * in the list.
8621558Srgrimes				     */
8631558Srgrimes				    ep = ex_search(&fsb.f_fsid);
8641558Srgrimes				    if (ep == (struct exportlist *)NULL) {
8651558Srgrimes					ep = get_exp();
8661558Srgrimes					ep->ex_fs = fsb.f_fsid;
8671558Srgrimes					ep->ex_fsdir = (char *)
8681558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
8691558Srgrimes					if (ep->ex_fsdir)
8701558Srgrimes					    strcpy(ep->ex_fsdir,
8711558Srgrimes						fsb.f_mntonname);
8721558Srgrimes					else
8731558Srgrimes					    out_of_mem();
8741558Srgrimes					if (debug)
8751558Srgrimes					  fprintf(stderr,
8761558Srgrimes					      "Making new ep fs=0x%x,0x%x\n",
8771558Srgrimes					      fsb.f_fsid.val[0],
8781558Srgrimes					      fsb.f_fsid.val[1]);
8791558Srgrimes				    } else if (debug)
8801558Srgrimes					fprintf(stderr,
8811558Srgrimes					    "Found ep fs=0x%x,0x%x\n",
8821558Srgrimes					    fsb.f_fsid.val[0],
8831558Srgrimes					    fsb.f_fsid.val[1]);
8841558Srgrimes				}
8851558Srgrimes
8861558Srgrimes				/*
8871558Srgrimes				 * Add dirpath to export mount point.
8881558Srgrimes				 */
8891558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
8901558Srgrimes				dirplen = len;
8911558Srgrimes			    } else {
8921558Srgrimes				getexp_err(ep, tgrp);
8931558Srgrimes				goto nextline;
8941558Srgrimes			    }
8951558Srgrimes			    *endcp = savedc;
8961558Srgrimes			} else {
8971558Srgrimes			    savedc = *endcp;
8981558Srgrimes			    *endcp = '\0';
8991558Srgrimes			    got_nondir = 1;
9001558Srgrimes			    if (ep == (struct exportlist *)NULL) {
9011558Srgrimes				getexp_err(ep, tgrp);
9021558Srgrimes				goto nextline;
9031558Srgrimes			    }
9041558Srgrimes
9051558Srgrimes			    /*
9061558Srgrimes			     * Get the host or netgroup.
9071558Srgrimes			     */
9081558Srgrimes			    setnetgrent(cp);
9091558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
9101558Srgrimes			    do {
9111558Srgrimes				if (has_host) {
9121558Srgrimes				    grp->gr_next = get_grp();
9131558Srgrimes				    grp = grp->gr_next;
9141558Srgrimes				}
9151558Srgrimes				if (netgrp) {
91637003Sjoerg				    if (hst == 0) {
91737003Sjoerg					syslog(LOG_ERR, "Null hostname in netgroup %s, skipping", cp);
91837003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
91929317Sjlemon					syslog(LOG_ERR, "Bad host %s in netgroup %s, skipping", hst, cp);
92029317Sjlemon					grp->gr_type = GT_IGNORE;
9211558Srgrimes				    }
9227401Swpaul				} else if (get_host(cp, grp, tgrp)) {
92329317Sjlemon				    syslog(LOG_ERR, "Bad host %s, skipping", cp);
92429317Sjlemon				    grp->gr_type = GT_IGNORE;
9251558Srgrimes				}
9261558Srgrimes				has_host = TRUE;
9271558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
9281558Srgrimes			    endnetgrent();
9291558Srgrimes			    *endcp = savedc;
9301558Srgrimes			}
9311558Srgrimes			cp = endcp;
9321558Srgrimes			nextfield(&cp, &endcp);
9331558Srgrimes			len = endcp - cp;
9341558Srgrimes		}
9351558Srgrimes		if (check_options(dirhead)) {
9361558Srgrimes			getexp_err(ep, tgrp);
9371558Srgrimes			goto nextline;
9381558Srgrimes		}
9391558Srgrimes		if (!has_host) {
9401558Srgrimes			grp->gr_type = GT_HOST;
9411558Srgrimes			if (debug)
9421558Srgrimes				fprintf(stderr,"Adding a default entry\n");
9431558Srgrimes			/* add a default group and make the grp list NULL */
9441558Srgrimes			hpe = (struct hostent *)malloc(sizeof(struct hostent));
9451558Srgrimes			if (hpe == (struct hostent *)NULL)
9461558Srgrimes				out_of_mem();
94712348Sjoerg			hpe->h_name = strdup("Default");
9481558Srgrimes			hpe->h_addrtype = AF_INET;
9491558Srgrimes			hpe->h_length = sizeof (u_long);
9501558Srgrimes			hpe->h_addr_list = (char **)NULL;
9511558Srgrimes			grp->gr_ptr.gt_hostent = hpe;
9521558Srgrimes
9531558Srgrimes		/*
9541558Srgrimes		 * Don't allow a network export coincide with a list of
9551558Srgrimes		 * host(s) on the same line.
9561558Srgrimes		 */
9571558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
9581558Srgrimes			getexp_err(ep, tgrp);
9591558Srgrimes			goto nextline;
96029317Sjlemon
96129317Sjlemon        	/*
96229317Sjlemon	         * If an export list was specified on this line, make sure
96329317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
96429317Sjlemon		 */
96529317Sjlemon		} else {
96629317Sjlemon			grp = tgrp;
96729317Sjlemon        		while (grp && grp->gr_type == GT_IGNORE)
96829317Sjlemon				grp = grp->gr_next;
96929317Sjlemon			if (! grp) {
97029317Sjlemon			    getexp_err(ep, tgrp);
97129317Sjlemon			    goto nextline;
97229317Sjlemon			}
9731558Srgrimes		}
9741558Srgrimes
9751558Srgrimes		/*
9761558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
9771558Srgrimes		 * After loop, tgrp points to the start of the list and
9781558Srgrimes		 * grp points to the last entry in the list.
9791558Srgrimes		 */
9801558Srgrimes		grp = tgrp;
9811558Srgrimes		do {
9821558Srgrimes		    if (do_mount(ep, grp, exflags, &anon, dirp,
9831558Srgrimes			dirplen, &fsb)) {
9841558Srgrimes			getexp_err(ep, tgrp);
9851558Srgrimes			goto nextline;
9861558Srgrimes		    }
9871558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
9881558Srgrimes
9891558Srgrimes		/*
9901558Srgrimes		 * Success. Update the data structures.
9911558Srgrimes		 */
9921558Srgrimes		if (has_host) {
9939336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
9941558Srgrimes			grp->gr_next = grphead;
9951558Srgrimes			grphead = tgrp;
9961558Srgrimes		} else {
9971558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
9989336Sdfr				opt_flags);
9991558Srgrimes			free_grp(grp);
10001558Srgrimes		}
10011558Srgrimes		dirhead = (struct dirlist *)NULL;
10021558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
10031558Srgrimes			ep2 = exphead;
10041558Srgrimes			epp = &exphead;
10051558Srgrimes
10061558Srgrimes			/*
10071558Srgrimes			 * Insert in the list in alphabetical order.
10081558Srgrimes			 */
10091558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
10101558Srgrimes				epp = &ep2->ex_next;
10111558Srgrimes				ep2 = ep2->ex_next;
10121558Srgrimes			}
10131558Srgrimes			if (ep2)
10141558Srgrimes				ep->ex_next = ep2;
10151558Srgrimes			*epp = ep;
10161558Srgrimes			ep->ex_flag |= EX_LINKED;
10171558Srgrimes		}
10181558Srgrimesnextline:
10191558Srgrimes		if (dirhead) {
10201558Srgrimes			free_dir(dirhead);
10211558Srgrimes			dirhead = (struct dirlist *)NULL;
10221558Srgrimes		}
10231558Srgrimes	}
10241558Srgrimes	fclose(exp_file);
10251558Srgrimes}
10261558Srgrimes
10271558Srgrimes/*
10281558Srgrimes * Allocate an export list element
10291558Srgrimes */
10301558Srgrimesstruct exportlist *
10311558Srgrimesget_exp()
10321558Srgrimes{
10331558Srgrimes	struct exportlist *ep;
10341558Srgrimes
10351558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
10361558Srgrimes	if (ep == (struct exportlist *)NULL)
10371558Srgrimes		out_of_mem();
103823681Speter	memset(ep, 0, sizeof(struct exportlist));
10391558Srgrimes	return (ep);
10401558Srgrimes}
10411558Srgrimes
10421558Srgrimes/*
10431558Srgrimes * Allocate a group list element
10441558Srgrimes */
10451558Srgrimesstruct grouplist *
10461558Srgrimesget_grp()
10471558Srgrimes{
10481558Srgrimes	struct grouplist *gp;
10491558Srgrimes
10501558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
10511558Srgrimes	if (gp == (struct grouplist *)NULL)
10521558Srgrimes		out_of_mem();
105323681Speter	memset(gp, 0, sizeof(struct grouplist));
10541558Srgrimes	return (gp);
10551558Srgrimes}
10561558Srgrimes
10571558Srgrimes/*
10581558Srgrimes * Clean up upon an error in get_exportlist().
10591558Srgrimes */
10601558Srgrimesvoid
10611558Srgrimesgetexp_err(ep, grp)
10621558Srgrimes	struct exportlist *ep;
10631558Srgrimes	struct grouplist *grp;
10641558Srgrimes{
10651558Srgrimes	struct grouplist *tgrp;
10661558Srgrimes
10671558Srgrimes	syslog(LOG_ERR, "Bad exports list line %s", line);
10681558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
10691558Srgrimes		free_exp(ep);
10701558Srgrimes	while (grp) {
10711558Srgrimes		tgrp = grp;
10721558Srgrimes		grp = grp->gr_next;
10731558Srgrimes		free_grp(tgrp);
10741558Srgrimes	}
10751558Srgrimes}
10761558Srgrimes
10771558Srgrimes/*
10781558Srgrimes * Search the export list for a matching fs.
10791558Srgrimes */
10801558Srgrimesstruct exportlist *
10811558Srgrimesex_search(fsid)
10821558Srgrimes	fsid_t *fsid;
10831558Srgrimes{
10841558Srgrimes	struct exportlist *ep;
10851558Srgrimes
10861558Srgrimes	ep = exphead;
10871558Srgrimes	while (ep) {
10881558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
10891558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
10901558Srgrimes			return (ep);
10911558Srgrimes		ep = ep->ex_next;
10921558Srgrimes	}
10931558Srgrimes	return (ep);
10941558Srgrimes}
10951558Srgrimes
10961558Srgrimes/*
10971558Srgrimes * Add a directory path to the list.
10981558Srgrimes */
10991558Srgrimeschar *
11001558Srgrimesadd_expdir(dpp, cp, len)
11011558Srgrimes	struct dirlist **dpp;
11021558Srgrimes	char *cp;
11031558Srgrimes	int len;
11041558Srgrimes{
11051558Srgrimes	struct dirlist *dp;
11061558Srgrimes
11071558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
11081558Srgrimes	dp->dp_left = *dpp;
11091558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
11101558Srgrimes	dp->dp_flag = 0;
11111558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
11121558Srgrimes	strcpy(dp->dp_dirp, cp);
11131558Srgrimes	*dpp = dp;
11141558Srgrimes	return (dp->dp_dirp);
11151558Srgrimes}
11161558Srgrimes
11171558Srgrimes/*
11181558Srgrimes * Hang the dir list element off the dirpath binary tree as required
11191558Srgrimes * and update the entry for host.
11201558Srgrimes */
11211558Srgrimesvoid
11229336Sdfrhang_dirp(dp, grp, ep, flags)
11231558Srgrimes	struct dirlist *dp;
11241558Srgrimes	struct grouplist *grp;
11251558Srgrimes	struct exportlist *ep;
11269336Sdfr	int flags;
11271558Srgrimes{
11281558Srgrimes	struct hostlist *hp;
11291558Srgrimes	struct dirlist *dp2;
11301558Srgrimes
11319336Sdfr	if (flags & OP_ALLDIRS) {
11321558Srgrimes		if (ep->ex_defdir)
11331558Srgrimes			free((caddr_t)dp);
11341558Srgrimes		else
11351558Srgrimes			ep->ex_defdir = dp;
11369336Sdfr		if (grp == (struct grouplist *)NULL) {
11371558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
11389336Sdfr			if (flags & OP_KERB)
11399336Sdfr				ep->ex_defdir->dp_flag |= DP_KERB;
11409336Sdfr		} else while (grp) {
11411558Srgrimes			hp = get_ht();
11429336Sdfr			if (flags & OP_KERB)
11439336Sdfr				hp->ht_flag |= DP_KERB;
11441558Srgrimes			hp->ht_grp = grp;
11451558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
11461558Srgrimes			ep->ex_defdir->dp_hosts = hp;
11471558Srgrimes			grp = grp->gr_next;
11481558Srgrimes		}
11491558Srgrimes	} else {
11501558Srgrimes
11511558Srgrimes		/*
11521558Srgrimes		 * Loop throught the directories adding them to the tree.
11531558Srgrimes		 */
11541558Srgrimes		while (dp) {
11551558Srgrimes			dp2 = dp->dp_left;
11569336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
11571558Srgrimes			dp = dp2;
11581558Srgrimes		}
11591558Srgrimes	}
11601558Srgrimes}
11611558Srgrimes
11621558Srgrimes/*
11631558Srgrimes * Traverse the binary tree either updating a node that is already there
11641558Srgrimes * for the new directory or adding the new node.
11651558Srgrimes */
11661558Srgrimesvoid
11679336Sdfradd_dlist(dpp, newdp, grp, flags)
11681558Srgrimes	struct dirlist **dpp;
11691558Srgrimes	struct dirlist *newdp;
11701558Srgrimes	struct grouplist *grp;
11719336Sdfr	int flags;
11721558Srgrimes{
11731558Srgrimes	struct dirlist *dp;
11741558Srgrimes	struct hostlist *hp;
11751558Srgrimes	int cmp;
11761558Srgrimes
11771558Srgrimes	dp = *dpp;
11781558Srgrimes	if (dp) {
11791558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
11801558Srgrimes		if (cmp > 0) {
11819336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
11821558Srgrimes			return;
11831558Srgrimes		} else if (cmp < 0) {
11849336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
11851558Srgrimes			return;
11861558Srgrimes		} else
11871558Srgrimes			free((caddr_t)newdp);
11881558Srgrimes	} else {
11891558Srgrimes		dp = newdp;
11901558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
11911558Srgrimes		*dpp = dp;
11921558Srgrimes	}
11931558Srgrimes	if (grp) {
11941558Srgrimes
11951558Srgrimes		/*
11961558Srgrimes		 * Hang all of the host(s) off of the directory point.
11971558Srgrimes		 */
11981558Srgrimes		do {
11991558Srgrimes			hp = get_ht();
12009336Sdfr			if (flags & OP_KERB)
12019336Sdfr				hp->ht_flag |= DP_KERB;
12021558Srgrimes			hp->ht_grp = grp;
12031558Srgrimes			hp->ht_next = dp->dp_hosts;
12041558Srgrimes			dp->dp_hosts = hp;
12051558Srgrimes			grp = grp->gr_next;
12061558Srgrimes		} while (grp);
12079336Sdfr	} else {
12081558Srgrimes		dp->dp_flag |= DP_DEFSET;
12099336Sdfr		if (flags & OP_KERB)
12109336Sdfr			dp->dp_flag |= DP_KERB;
12119336Sdfr	}
12121558Srgrimes}
12131558Srgrimes
12141558Srgrimes/*
12151558Srgrimes * Search for a dirpath on the export point.
12161558Srgrimes */
12171558Srgrimesstruct dirlist *
12181558Srgrimesdirp_search(dp, dirpath)
12191558Srgrimes	struct dirlist *dp;
12201558Srgrimes	char *dirpath;
12211558Srgrimes{
12221558Srgrimes	int cmp;
12231558Srgrimes
12241558Srgrimes	if (dp) {
12251558Srgrimes		cmp = strcmp(dp->dp_dirp, dirpath);
12261558Srgrimes		if (cmp > 0)
12271558Srgrimes			return (dirp_search(dp->dp_left, dirpath));
12281558Srgrimes		else if (cmp < 0)
12291558Srgrimes			return (dirp_search(dp->dp_right, dirpath));
12301558Srgrimes		else
12311558Srgrimes			return (dp);
12321558Srgrimes	}
12331558Srgrimes	return (dp);
12341558Srgrimes}
12351558Srgrimes
12361558Srgrimes/*
12371558Srgrimes * Scan for a host match in a directory tree.
12381558Srgrimes */
12391558Srgrimesint
12409336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
12411558Srgrimes	struct dirlist *dp;
12421558Srgrimes	u_long saddr;
12431558Srgrimes	int *defsetp;
12449336Sdfr	int *hostsetp;
12451558Srgrimes{
12461558Srgrimes	struct hostlist *hp;
12471558Srgrimes	struct grouplist *grp;
12481558Srgrimes	u_long **addrp;
12491558Srgrimes
12501558Srgrimes	if (dp) {
12511558Srgrimes		if (dp->dp_flag & DP_DEFSET)
12529336Sdfr			*defsetp = dp->dp_flag;
12531558Srgrimes		hp = dp->dp_hosts;
12541558Srgrimes		while (hp) {
12551558Srgrimes			grp = hp->ht_grp;
12561558Srgrimes			switch (grp->gr_type) {
12571558Srgrimes			case GT_HOST:
12581558Srgrimes			    addrp = (u_long **)
12591558Srgrimes				grp->gr_ptr.gt_hostent->h_addr_list;
12601558Srgrimes			    while (*addrp) {
12619336Sdfr				if (**addrp == saddr) {
12629336Sdfr				    *hostsetp = (hp->ht_flag | DP_HOSTSET);
12631558Srgrimes				    return (1);
12649336Sdfr				}
12651558Srgrimes				addrp++;
12661558Srgrimes			    }
12671558Srgrimes			    break;
12681558Srgrimes			case GT_NET:
12691558Srgrimes			    if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
12709336Sdfr				grp->gr_ptr.gt_net.nt_net) {
12719336Sdfr				*hostsetp = (hp->ht_flag | DP_HOSTSET);
12721558Srgrimes				return (1);
12739336Sdfr			    }
12741558Srgrimes			    break;
12751558Srgrimes			};
12761558Srgrimes			hp = hp->ht_next;
12771558Srgrimes		}
12781558Srgrimes	}
12791558Srgrimes	return (0);
12801558Srgrimes}
12811558Srgrimes
12821558Srgrimes/*
12831558Srgrimes * Scan tree for a host that matches the address.
12841558Srgrimes */
12851558Srgrimesint
12861558Srgrimesscan_tree(dp, saddr)
12871558Srgrimes	struct dirlist *dp;
12881558Srgrimes	u_long saddr;
12891558Srgrimes{
12909336Sdfr	int defset, hostset;
12911558Srgrimes
12921558Srgrimes	if (dp) {
12931558Srgrimes		if (scan_tree(dp->dp_left, saddr))
12941558Srgrimes			return (1);
12959336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
12961558Srgrimes			return (1);
12971558Srgrimes		if (scan_tree(dp->dp_right, saddr))
12981558Srgrimes			return (1);
12991558Srgrimes	}
13001558Srgrimes	return (0);
13011558Srgrimes}
13021558Srgrimes
13031558Srgrimes/*
13041558Srgrimes * Traverse the dirlist tree and free it up.
13051558Srgrimes */
13061558Srgrimesvoid
13071558Srgrimesfree_dir(dp)
13081558Srgrimes	struct dirlist *dp;
13091558Srgrimes{
13101558Srgrimes
13111558Srgrimes	if (dp) {
13121558Srgrimes		free_dir(dp->dp_left);
13131558Srgrimes		free_dir(dp->dp_right);
13141558Srgrimes		free_host(dp->dp_hosts);
13151558Srgrimes		free((caddr_t)dp);
13161558Srgrimes	}
13171558Srgrimes}
13181558Srgrimes
13191558Srgrimes/*
13201558Srgrimes * Parse the option string and update fields.
13211558Srgrimes * Option arguments may either be -<option>=<value> or
13221558Srgrimes * -<option> <value>
13231558Srgrimes */
13241558Srgrimesint
13251558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
13261558Srgrimes	char **cpp, **endcpp;
13271558Srgrimes	struct exportlist *ep;
13281558Srgrimes	struct grouplist *grp;
13291558Srgrimes	int *has_hostp;
13301558Srgrimes	int *exflagsp;
13311558Srgrimes	struct ucred *cr;
13321558Srgrimes{
13331558Srgrimes	char *cpoptarg, *cpoptend;
13341558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
13351558Srgrimes	int allflag, usedarg;
13361558Srgrimes
13371558Srgrimes	cpopt = *cpp;
13381558Srgrimes	cpopt++;
13391558Srgrimes	cp = *endcpp;
13401558Srgrimes	savedc = *cp;
13411558Srgrimes	*cp = '\0';
13421558Srgrimes	while (cpopt && *cpopt) {
13431558Srgrimes		allflag = 1;
13441558Srgrimes		usedarg = -2;
134523681Speter		if (cpoptend = strchr(cpopt, ',')) {
13461558Srgrimes			*cpoptend++ = '\0';
134723681Speter			if (cpoptarg = strchr(cpopt, '='))
13481558Srgrimes				*cpoptarg++ = '\0';
13491558Srgrimes		} else {
135023681Speter			if (cpoptarg = strchr(cpopt, '='))
13511558Srgrimes				*cpoptarg++ = '\0';
13521558Srgrimes			else {
13531558Srgrimes				*cp = savedc;
13541558Srgrimes				nextfield(&cp, &endcp);
13551558Srgrimes				**endcpp = '\0';
13561558Srgrimes				if (endcp > cp && *cp != '-') {
13571558Srgrimes					cpoptarg = cp;
13581558Srgrimes					savedc2 = *endcp;
13591558Srgrimes					*endcp = '\0';
13601558Srgrimes					usedarg = 0;
13611558Srgrimes				}
13621558Srgrimes			}
13631558Srgrimes		}
13641558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
13651558Srgrimes			*exflagsp |= MNT_EXRDONLY;
13661558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
13671558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
13681558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
13691558Srgrimes			usedarg++;
13701558Srgrimes			parsecred(cpoptarg, cr);
13711558Srgrimes			if (allflag == 0) {
13721558Srgrimes				*exflagsp |= MNT_EXPORTANON;
13731558Srgrimes				opt_flags |= OP_MAPALL;
13741558Srgrimes			} else
13751558Srgrimes				opt_flags |= OP_MAPROOT;
13761558Srgrimes		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
13771558Srgrimes			*exflagsp |= MNT_EXKERB;
13781558Srgrimes			opt_flags |= OP_KERB;
13791558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
13801558Srgrimes			!strcmp(cpopt, "m"))) {
13811558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
13821558Srgrimes				syslog(LOG_ERR, "Bad mask: %s", cpoptarg);
13831558Srgrimes				return (1);
13841558Srgrimes			}
13851558Srgrimes			usedarg++;
13861558Srgrimes			opt_flags |= OP_MASK;
13871558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
13881558Srgrimes			!strcmp(cpopt, "n"))) {
13891558Srgrimes			if (grp->gr_type != GT_NULL) {
13901558Srgrimes				syslog(LOG_ERR, "Network/host conflict");
13911558Srgrimes				return (1);
13921558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
13931558Srgrimes				syslog(LOG_ERR, "Bad net: %s", cpoptarg);
13941558Srgrimes				return (1);
13951558Srgrimes			}
13961558Srgrimes			grp->gr_type = GT_NET;
13971558Srgrimes			*has_hostp = 1;
13981558Srgrimes			usedarg++;
13991558Srgrimes			opt_flags |= OP_NET;
14001558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
14011558Srgrimes			opt_flags |= OP_ALLDIRS;
140227447Sdfr		} else if (!strcmp(cpopt, "public")) {
140327447Sdfr			*exflagsp |= MNT_EXPUBLIC;
140427447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
140527447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
140627447Sdfr			opt_flags |= OP_MAPALL;
140727447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
140827447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
14091558Srgrimes#ifdef ISO
14101558Srgrimes		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
14111558Srgrimes			if (get_isoaddr(cpoptarg, grp)) {
14121558Srgrimes				syslog(LOG_ERR, "Bad iso addr: %s", cpoptarg);
14131558Srgrimes				return (1);
14141558Srgrimes			}
14151558Srgrimes			*has_hostp = 1;
14161558Srgrimes			usedarg++;
14171558Srgrimes			opt_flags |= OP_ISO;
14181558Srgrimes#endif /* ISO */
14191558Srgrimes		} else {
14201558Srgrimes			syslog(LOG_ERR, "Bad opt %s", cpopt);
14211558Srgrimes			return (1);
14221558Srgrimes		}
14231558Srgrimes		if (usedarg >= 0) {
14241558Srgrimes			*endcp = savedc2;
14251558Srgrimes			**endcpp = savedc;
14261558Srgrimes			if (usedarg > 0) {
14271558Srgrimes				*cpp = cp;
14281558Srgrimes				*endcpp = endcp;
14291558Srgrimes			}
14301558Srgrimes			return (0);
14311558Srgrimes		}
14321558Srgrimes		cpopt = cpoptend;
14331558Srgrimes	}
14341558Srgrimes	**endcpp = savedc;
14351558Srgrimes	return (0);
14361558Srgrimes}
14371558Srgrimes
14381558Srgrimes/*
14391558Srgrimes * Translate a character string to the corresponding list of network
14401558Srgrimes * addresses for a hostname.
14411558Srgrimes */
14421558Srgrimesint
14437401Swpaulget_host(cp, grp, tgrp)
14441558Srgrimes	char *cp;
14451558Srgrimes	struct grouplist *grp;
14467401Swpaul	struct grouplist *tgrp;
14471558Srgrimes{
14487401Swpaul	struct grouplist *checkgrp;
14491558Srgrimes	struct hostent *hp, *nhp;
14501558Srgrimes	char **addrp, **naddrp;
14511558Srgrimes	struct hostent t_host;
14521558Srgrimes	int i;
14531558Srgrimes	u_long saddr;
14541558Srgrimes	char *aptr[2];
14551558Srgrimes
14561558Srgrimes	if (grp->gr_type != GT_NULL)
14571558Srgrimes		return (1);
14581558Srgrimes	if ((hp = gethostbyname(cp)) == NULL) {
14591558Srgrimes		if (isdigit(*cp)) {
14601558Srgrimes			saddr = inet_addr(cp);
14611558Srgrimes			if (saddr == -1) {
14629336Sdfr 				syslog(LOG_ERR, "Inet_addr failed for %s", cp);
14631558Srgrimes				return (1);
14641558Srgrimes			}
14651558Srgrimes			if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
14661558Srgrimes				AF_INET)) == NULL) {
14671558Srgrimes				hp = &t_host;
14681558Srgrimes				hp->h_name = cp;
14691558Srgrimes				hp->h_addrtype = AF_INET;
14701558Srgrimes				hp->h_length = sizeof (u_long);
14711558Srgrimes				hp->h_addr_list = aptr;
14721558Srgrimes				aptr[0] = (char *)&saddr;
14731558Srgrimes				aptr[1] = (char *)NULL;
14741558Srgrimes			}
14751558Srgrimes		} else {
14769336Sdfr 			syslog(LOG_ERR, "Gethostbyname failed for %s", cp);
14771558Srgrimes			return (1);
14781558Srgrimes		}
14791558Srgrimes	}
14807401Swpaul        /*
14817401Swpaul         * Sanity check: make sure we don't already have an entry
14827401Swpaul         * for this host in the grouplist.
14837401Swpaul         */
14847401Swpaul        checkgrp = tgrp;
14857401Swpaul        while (checkgrp) {
148617887Swpaul		if (checkgrp->gr_type == GT_HOST &&
148717887Swpaul                    checkgrp->gr_ptr.gt_hostent != NULL &&
14887401Swpaul                    !strcmp(checkgrp->gr_ptr.gt_hostent->h_name, hp->h_name)) {
14897401Swpaul                        grp->gr_type = GT_IGNORE;
14907401Swpaul			return(0);
14917401Swpaul		}
14927401Swpaul                checkgrp = checkgrp->gr_next;
14937401Swpaul        }
14947401Swpaul
14951558Srgrimes	grp->gr_type = GT_HOST;
14961558Srgrimes	nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
14971558Srgrimes		malloc(sizeof(struct hostent));
14981558Srgrimes	if (nhp == (struct hostent *)NULL)
14991558Srgrimes		out_of_mem();
150023681Speter	memmove(nhp, hp, sizeof(struct hostent));
15011558Srgrimes	i = strlen(hp->h_name)+1;
15021558Srgrimes	nhp->h_name = (char *)malloc(i);
15031558Srgrimes	if (nhp->h_name == (char *)NULL)
15041558Srgrimes		out_of_mem();
150523681Speter	memmove(nhp->h_name, hp->h_name, i);
15061558Srgrimes	addrp = hp->h_addr_list;
15071558Srgrimes	i = 1;
15081558Srgrimes	while (*addrp++)
15091558Srgrimes		i++;
15101558Srgrimes	naddrp = nhp->h_addr_list = (char **)
15111558Srgrimes		malloc(i*sizeof(char *));
15121558Srgrimes	if (naddrp == (char **)NULL)
15131558Srgrimes		out_of_mem();
15141558Srgrimes	addrp = hp->h_addr_list;
15151558Srgrimes	while (*addrp) {
15161558Srgrimes		*naddrp = (char *)
15171558Srgrimes		    malloc(hp->h_length);
15181558Srgrimes		if (*naddrp == (char *)NULL)
15191558Srgrimes		    out_of_mem();
152023681Speter		memmove(*naddrp, *addrp, hp->h_length);
15211558Srgrimes		addrp++;
15221558Srgrimes		naddrp++;
15231558Srgrimes	}
15241558Srgrimes	*naddrp = (char *)NULL;
15251558Srgrimes	if (debug)
15261558Srgrimes		fprintf(stderr, "got host %s\n", hp->h_name);
15271558Srgrimes	return (0);
15281558Srgrimes}
15291558Srgrimes
15301558Srgrimes/*
15311558Srgrimes * Free up an exports list component
15321558Srgrimes */
15331558Srgrimesvoid
15341558Srgrimesfree_exp(ep)
15351558Srgrimes	struct exportlist *ep;
15361558Srgrimes{
15371558Srgrimes
15381558Srgrimes	if (ep->ex_defdir) {
15391558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
15401558Srgrimes		free((caddr_t)ep->ex_defdir);
15411558Srgrimes	}
15421558Srgrimes	if (ep->ex_fsdir)
15431558Srgrimes		free(ep->ex_fsdir);
154427447Sdfr	if (ep->ex_indexfile)
154527447Sdfr		free(ep->ex_indexfile);
15461558Srgrimes	free_dir(ep->ex_dirl);
15471558Srgrimes	free((caddr_t)ep);
15481558Srgrimes}
15491558Srgrimes
15501558Srgrimes/*
15511558Srgrimes * Free hosts.
15521558Srgrimes */
15531558Srgrimesvoid
15541558Srgrimesfree_host(hp)
15551558Srgrimes	struct hostlist *hp;
15561558Srgrimes{
15571558Srgrimes	struct hostlist *hp2;
15581558Srgrimes
15591558Srgrimes	while (hp) {
15601558Srgrimes		hp2 = hp;
15611558Srgrimes		hp = hp->ht_next;
15621558Srgrimes		free((caddr_t)hp2);
15631558Srgrimes	}
15641558Srgrimes}
15651558Srgrimes
15661558Srgrimesstruct hostlist *
15671558Srgrimesget_ht()
15681558Srgrimes{
15691558Srgrimes	struct hostlist *hp;
15701558Srgrimes
15711558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
15721558Srgrimes	if (hp == (struct hostlist *)NULL)
15731558Srgrimes		out_of_mem();
15741558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
15759336Sdfr	hp->ht_flag = 0;
15761558Srgrimes	return (hp);
15771558Srgrimes}
15781558Srgrimes
15791558Srgrimes#ifdef ISO
15801558Srgrimes/*
15811558Srgrimes * Translate an iso address.
15821558Srgrimes */
15831558Srgrimesget_isoaddr(cp, grp)
15841558Srgrimes	char *cp;
15851558Srgrimes	struct grouplist *grp;
15861558Srgrimes{
15871558Srgrimes	struct iso_addr *isop;
15881558Srgrimes	struct sockaddr_iso *isoaddr;
15891558Srgrimes
15901558Srgrimes	if (grp->gr_type != GT_NULL)
15911558Srgrimes		return (1);
15921558Srgrimes	if ((isop = iso_addr(cp)) == NULL) {
15931558Srgrimes		syslog(LOG_ERR,
15941558Srgrimes		    "iso_addr failed, ignored");
15951558Srgrimes		return (1);
15961558Srgrimes	}
15971558Srgrimes	isoaddr = (struct sockaddr_iso *)
15981558Srgrimes	    malloc(sizeof (struct sockaddr_iso));
15991558Srgrimes	if (isoaddr == (struct sockaddr_iso *)NULL)
16001558Srgrimes		out_of_mem();
160123681Speter	memset(isoaddr, 0, sizeof(struct sockaddr_iso));
160223681Speter	memmove(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
160323681Speter	isoaddr->siso_len = sizeof(struct sockaddr_iso);
16041558Srgrimes	isoaddr->siso_family = AF_ISO;
16051558Srgrimes	grp->gr_type = GT_ISO;
16061558Srgrimes	grp->gr_ptr.gt_isoaddr = isoaddr;
16071558Srgrimes	return (0);
16081558Srgrimes}
16091558Srgrimes#endif	/* ISO */
16101558Srgrimes
16111558Srgrimes/*
16121558Srgrimes * Out of memory, fatal
16131558Srgrimes */
16141558Srgrimesvoid
16151558Srgrimesout_of_mem()
16161558Srgrimes{
16171558Srgrimes
16181558Srgrimes	syslog(LOG_ERR, "Out of memory");
16191558Srgrimes	exit(2);
16201558Srgrimes}
16211558Srgrimes
16221558Srgrimes/*
16231558Srgrimes * Do the mount syscall with the update flag to push the export info into
16241558Srgrimes * the kernel.
16251558Srgrimes */
16261558Srgrimesint
16271558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
16281558Srgrimes	struct exportlist *ep;
16291558Srgrimes	struct grouplist *grp;
16301558Srgrimes	int exflags;
16311558Srgrimes	struct ucred *anoncrp;
16321558Srgrimes	char *dirp;
16331558Srgrimes	int dirplen;
16341558Srgrimes	struct statfs *fsb;
16351558Srgrimes{
16361558Srgrimes	char *cp = (char *)NULL;
16371558Srgrimes	u_long **addrp;
16381558Srgrimes	int done;
16391558Srgrimes	char savedc = '\0';
16401558Srgrimes	struct sockaddr_in sin, imask;
16411558Srgrimes	union {
16421558Srgrimes		struct ufs_args ua;
16431558Srgrimes		struct iso_args ia;
16441558Srgrimes		struct mfs_args ma;
16459336Sdfr#ifdef __NetBSD__
16469336Sdfr		struct msdosfs_args da;
16479336Sdfr#endif
16481558Srgrimes	} args;
16491558Srgrimes	u_long net;
16501558Srgrimes
16511558Srgrimes	args.ua.fspec = 0;
16521558Srgrimes	args.ua.export.ex_flags = exflags;
16531558Srgrimes	args.ua.export.ex_anon = *anoncrp;
165427447Sdfr	args.ua.export.ex_indexfile = ep->ex_indexfile;
165523681Speter	memset(&sin, 0, sizeof(sin));
165623681Speter	memset(&imask, 0, sizeof(imask));
16571558Srgrimes	sin.sin_family = AF_INET;
16581558Srgrimes	sin.sin_len = sizeof(sin);
16591558Srgrimes	imask.sin_family = AF_INET;
16601558Srgrimes	imask.sin_len = sizeof(sin);
16611558Srgrimes	if (grp->gr_type == GT_HOST)
16621558Srgrimes		addrp = (u_long **)grp->gr_ptr.gt_hostent->h_addr_list;
16631558Srgrimes	else
16641558Srgrimes		addrp = (u_long **)NULL;
16651558Srgrimes	done = FALSE;
16661558Srgrimes	while (!done) {
16671558Srgrimes		switch (grp->gr_type) {
16681558Srgrimes		case GT_HOST:
16691558Srgrimes			if (addrp) {
16701558Srgrimes				sin.sin_addr.s_addr = **addrp;
16711558Srgrimes				args.ua.export.ex_addrlen = sizeof(sin);
16721558Srgrimes			} else
16731558Srgrimes				args.ua.export.ex_addrlen = 0;
16741558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16751558Srgrimes			args.ua.export.ex_masklen = 0;
16761558Srgrimes			break;
16771558Srgrimes		case GT_NET:
16781558Srgrimes			if (grp->gr_ptr.gt_net.nt_mask)
16791558Srgrimes			    imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
16801558Srgrimes			else {
16811558Srgrimes			    net = ntohl(grp->gr_ptr.gt_net.nt_net);
16821558Srgrimes			    if (IN_CLASSA(net))
16831558Srgrimes				imask.sin_addr.s_addr = inet_addr("255.0.0.0");
16841558Srgrimes			    else if (IN_CLASSB(net))
16851558Srgrimes				imask.sin_addr.s_addr =
16861558Srgrimes				    inet_addr("255.255.0.0");
16871558Srgrimes			    else
16881558Srgrimes				imask.sin_addr.s_addr =
16891558Srgrimes				    inet_addr("255.255.255.0");
16901558Srgrimes			    grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
16911558Srgrimes			}
16921558Srgrimes			sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
16931558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16941558Srgrimes			args.ua.export.ex_addrlen = sizeof (sin);
16951558Srgrimes			args.ua.export.ex_mask = (struct sockaddr *)&imask;
16961558Srgrimes			args.ua.export.ex_masklen = sizeof (imask);
16971558Srgrimes			break;
16981558Srgrimes#ifdef ISO
16991558Srgrimes		case GT_ISO:
17001558Srgrimes			args.ua.export.ex_addr =
17011558Srgrimes				(struct sockaddr *)grp->gr_ptr.gt_isoaddr;
17021558Srgrimes			args.ua.export.ex_addrlen =
17031558Srgrimes				sizeof(struct sockaddr_iso);
17041558Srgrimes			args.ua.export.ex_masklen = 0;
17051558Srgrimes			break;
17061558Srgrimes#endif	/* ISO */
17077401Swpaul		case GT_IGNORE:
17087401Swpaul			return(0);
17097401Swpaul			break;
17101558Srgrimes		default:
17111558Srgrimes			syslog(LOG_ERR, "Bad grouptype");
17121558Srgrimes			if (cp)
17131558Srgrimes				*cp = savedc;
17141558Srgrimes			return (1);
17151558Srgrimes		};
17161558Srgrimes
17171558Srgrimes		/*
17181558Srgrimes		 * XXX:
17191558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
17201558Srgrimes		 * of looping back up the dirp to the mount point??
17211558Srgrimes		 * Also, needs to know how to export all types of local
172223681Speter		 * exportable file systems and not just "ufs".
17231558Srgrimes		 */
17249336Sdfr		while (mount(fsb->f_fstypename, dirp,
17251558Srgrimes		       fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
17261558Srgrimes			if (cp)
17271558Srgrimes				*cp-- = savedc;
17281558Srgrimes			else
17291558Srgrimes				cp = dirp + dirplen - 1;
17301558Srgrimes			if (errno == EPERM) {
17311558Srgrimes				syslog(LOG_ERR,
17321558Srgrimes				   "Can't change attributes for %s.\n", dirp);
17331558Srgrimes				return (1);
17341558Srgrimes			}
17351558Srgrimes			if (opt_flags & OP_ALLDIRS) {
17364895Swollman				syslog(LOG_ERR, "Could not remount %s: %m",
17374895Swollman					dirp);
17381558Srgrimes				return (1);
17391558Srgrimes			}
17401558Srgrimes			/* back up over the last component */
17411558Srgrimes			while (*cp == '/' && cp > dirp)
17421558Srgrimes				cp--;
17431558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
17441558Srgrimes				cp--;
17451558Srgrimes			if (cp == dirp) {
17461558Srgrimes				if (debug)
17471558Srgrimes					fprintf(stderr,"mnt unsucc\n");
17481558Srgrimes				syslog(LOG_ERR, "Can't export %s", dirp);
17491558Srgrimes				return (1);
17501558Srgrimes			}
17511558Srgrimes			savedc = *cp;
17521558Srgrimes			*cp = '\0';
17531558Srgrimes		}
17541558Srgrimes		if (addrp) {
17551558Srgrimes			++addrp;
17561558Srgrimes			if (*addrp == (u_long *)NULL)
17571558Srgrimes				done = TRUE;
17581558Srgrimes		} else
17591558Srgrimes			done = TRUE;
17601558Srgrimes	}
17611558Srgrimes	if (cp)
17621558Srgrimes		*cp = savedc;
17631558Srgrimes	return (0);
17641558Srgrimes}
17651558Srgrimes
17661558Srgrimes/*
17671558Srgrimes * Translate a net address.
17681558Srgrimes */
17691558Srgrimesint
17701558Srgrimesget_net(cp, net, maskflg)
17711558Srgrimes	char *cp;
17721558Srgrimes	struct netmsk *net;
17731558Srgrimes	int maskflg;
17741558Srgrimes{
17751558Srgrimes	struct netent *np;
17761558Srgrimes	long netaddr;
17771558Srgrimes	struct in_addr inetaddr, inetaddr2;
17781558Srgrimes	char *name;
17791558Srgrimes
178025318Spst	if (isdigit(*cp) && ((netaddr = inet_network(cp)) != -1)) {
17811558Srgrimes		inetaddr = inet_makeaddr(netaddr, 0);
17821558Srgrimes		/*
17831558Srgrimes		 * Due to arbritrary subnet masks, you don't know how many
17841558Srgrimes		 * bits to shift the address to make it into a network,
17851558Srgrimes		 * however you do know how to make a network address into
17861558Srgrimes		 * a host with host == 0 and then compare them.
17871558Srgrimes		 * (What a pest)
17881558Srgrimes		 */
17891558Srgrimes		if (!maskflg) {
17901558Srgrimes			setnetent(0);
17911558Srgrimes			while (np = getnetent()) {
17921558Srgrimes				inetaddr2 = inet_makeaddr(np->n_net, 0);
17931558Srgrimes				if (inetaddr2.s_addr == inetaddr.s_addr)
17941558Srgrimes					break;
17951558Srgrimes			}
17961558Srgrimes			endnetent();
17971558Srgrimes		}
179825318Spst	} else if ((np = getnetbyname(cp)) != NULL) {
179925318Spst		inetaddr = inet_makeaddr(np->n_net, 0);
18001558Srgrimes	} else
18011558Srgrimes		return (1);
180225318Spst
18031558Srgrimes	if (maskflg)
18041558Srgrimes		net->nt_mask = inetaddr.s_addr;
18051558Srgrimes	else {
18061558Srgrimes		if (np)
18071558Srgrimes			name = np->n_name;
18081558Srgrimes		else
18091558Srgrimes			name = inet_ntoa(inetaddr);
18101558Srgrimes		net->nt_name = (char *)malloc(strlen(name) + 1);
18111558Srgrimes		if (net->nt_name == (char *)NULL)
18121558Srgrimes			out_of_mem();
18131558Srgrimes		strcpy(net->nt_name, name);
18141558Srgrimes		net->nt_net = inetaddr.s_addr;
18151558Srgrimes	}
18161558Srgrimes	return (0);
18171558Srgrimes}
18181558Srgrimes
18191558Srgrimes/*
18201558Srgrimes * Parse out the next white space separated field
18211558Srgrimes */
18221558Srgrimesvoid
18231558Srgrimesnextfield(cp, endcp)
18241558Srgrimes	char **cp;
18251558Srgrimes	char **endcp;
18261558Srgrimes{
18271558Srgrimes	char *p;
18281558Srgrimes
18291558Srgrimes	p = *cp;
18301558Srgrimes	while (*p == ' ' || *p == '\t')
18311558Srgrimes		p++;
18321558Srgrimes	if (*p == '\n' || *p == '\0')
18331558Srgrimes		*cp = *endcp = p;
18341558Srgrimes	else {
18351558Srgrimes		*cp = p++;
18361558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
18371558Srgrimes			p++;
18381558Srgrimes		*endcp = p;
18391558Srgrimes	}
18401558Srgrimes}
18411558Srgrimes
18421558Srgrimes/*
18431558Srgrimes * Get an exports file line. Skip over blank lines and handle line
18441558Srgrimes * continuations.
18451558Srgrimes */
18461558Srgrimesint
18471558Srgrimesget_line()
18481558Srgrimes{
18491558Srgrimes	char *p, *cp;
18501558Srgrimes	int len;
18511558Srgrimes	int totlen, cont_line;
18521558Srgrimes
18531558Srgrimes	/*
18541558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
18551558Srgrimes	 */
18561558Srgrimes	p = line;
18571558Srgrimes	totlen = 0;
18581558Srgrimes	do {
18591558Srgrimes		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
18601558Srgrimes			return (0);
18611558Srgrimes		len = strlen(p);
18621558Srgrimes		cp = p + len - 1;
18631558Srgrimes		cont_line = 0;
18641558Srgrimes		while (cp >= p &&
18651558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
18661558Srgrimes			if (*cp == '\\')
18671558Srgrimes				cont_line = 1;
18681558Srgrimes			cp--;
18691558Srgrimes			len--;
18701558Srgrimes		}
18711558Srgrimes		*++cp = '\0';
18721558Srgrimes		if (len > 0) {
18731558Srgrimes			totlen += len;
18741558Srgrimes			if (totlen >= LINESIZ) {
18751558Srgrimes				syslog(LOG_ERR, "Exports line too long");
18761558Srgrimes				exit(2);
18771558Srgrimes			}
18781558Srgrimes			p = cp;
18791558Srgrimes		}
18801558Srgrimes	} while (totlen == 0 || cont_line);
18811558Srgrimes	return (1);
18821558Srgrimes}
18831558Srgrimes
18841558Srgrimes/*
18851558Srgrimes * Parse a description of a credential.
18861558Srgrimes */
18871558Srgrimesvoid
18881558Srgrimesparsecred(namelist, cr)
18891558Srgrimes	char *namelist;
18901558Srgrimes	struct ucred *cr;
18911558Srgrimes{
18921558Srgrimes	char *name;
18931558Srgrimes	int cnt;
18941558Srgrimes	char *names;
18951558Srgrimes	struct passwd *pw;
18961558Srgrimes	struct group *gr;
18971558Srgrimes	int ngroups, groups[NGROUPS + 1];
18981558Srgrimes
18991558Srgrimes	/*
19001558Srgrimes	 * Set up the unpriviledged user.
19011558Srgrimes	 */
19021558Srgrimes	cr->cr_ref = 1;
19031558Srgrimes	cr->cr_uid = -2;
19041558Srgrimes	cr->cr_groups[0] = -2;
19051558Srgrimes	cr->cr_ngroups = 1;
19061558Srgrimes	/*
19071558Srgrimes	 * Get the user's password table entry.
19081558Srgrimes	 */
19091558Srgrimes	names = strsep(&namelist, " \t\n");
19101558Srgrimes	name = strsep(&names, ":");
19111558Srgrimes	if (isdigit(*name) || *name == '-')
19121558Srgrimes		pw = getpwuid(atoi(name));
19131558Srgrimes	else
19141558Srgrimes		pw = getpwnam(name);
19151558Srgrimes	/*
19161558Srgrimes	 * Credentials specified as those of a user.
19171558Srgrimes	 */
19181558Srgrimes	if (names == NULL) {
19191558Srgrimes		if (pw == NULL) {
19201558Srgrimes			syslog(LOG_ERR, "Unknown user: %s", name);
19211558Srgrimes			return;
19221558Srgrimes		}
19231558Srgrimes		cr->cr_uid = pw->pw_uid;
19241558Srgrimes		ngroups = NGROUPS + 1;
19251558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
19261558Srgrimes			syslog(LOG_ERR, "Too many groups");
19271558Srgrimes		/*
19281558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
19291558Srgrimes		 */
19301558Srgrimes		cr->cr_ngroups = ngroups - 1;
19311558Srgrimes		cr->cr_groups[0] = groups[0];
19321558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
19331558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
19341558Srgrimes		return;
19351558Srgrimes	}
19361558Srgrimes	/*
19371558Srgrimes	 * Explicit credential specified as a colon separated list:
19381558Srgrimes	 *	uid:gid:gid:...
19391558Srgrimes	 */
19401558Srgrimes	if (pw != NULL)
19411558Srgrimes		cr->cr_uid = pw->pw_uid;
19421558Srgrimes	else if (isdigit(*name) || *name == '-')
19431558Srgrimes		cr->cr_uid = atoi(name);
19441558Srgrimes	else {
19451558Srgrimes		syslog(LOG_ERR, "Unknown user: %s", name);
19461558Srgrimes		return;
19471558Srgrimes	}
19481558Srgrimes	cr->cr_ngroups = 0;
19491558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
19501558Srgrimes		name = strsep(&names, ":");
19511558Srgrimes		if (isdigit(*name) || *name == '-') {
19521558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
19531558Srgrimes		} else {
19541558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
19551558Srgrimes				syslog(LOG_ERR, "Unknown group: %s", name);
19561558Srgrimes				continue;
19571558Srgrimes			}
19581558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
19591558Srgrimes		}
19601558Srgrimes	}
19611558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
19621558Srgrimes		syslog(LOG_ERR, "Too many groups");
19631558Srgrimes}
19641558Srgrimes
19651558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
19661558Srgrimes/*
19671558Srgrimes * Routines that maintain the remote mounttab
19681558Srgrimes */
19691558Srgrimesvoid
19701558Srgrimesget_mountlist()
19711558Srgrimes{
19721558Srgrimes	struct mountlist *mlp, **mlpp;
197323681Speter	char *host, *dirp, *cp;
19741558Srgrimes	int len;
19751558Srgrimes	char str[STRSIZ];
19761558Srgrimes	FILE *mlfile;
19771558Srgrimes
19781558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
19791558Srgrimes		syslog(LOG_ERR, "Can't open %s", _PATH_RMOUNTLIST);
19801558Srgrimes		return;
19811558Srgrimes	}
19821558Srgrimes	mlpp = &mlhead;
19831558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
198423681Speter		cp = str;
198523681Speter		host = strsep(&cp, " \t\n");
198623681Speter		dirp = strsep(&cp, " \t\n");
198723681Speter		if (host == NULL || dirp == NULL)
19881558Srgrimes			continue;
19891558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
199023681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
199123681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
199223681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
199323681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
19941558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
19951558Srgrimes		*mlpp = mlp;
19961558Srgrimes		mlpp = &mlp->ml_next;
19971558Srgrimes	}
19981558Srgrimes	fclose(mlfile);
19991558Srgrimes}
20001558Srgrimes
20011558Srgrimesvoid
20021558Srgrimesdel_mlist(hostp, dirp)
20031558Srgrimes	char *hostp, *dirp;
20041558Srgrimes{
20051558Srgrimes	struct mountlist *mlp, **mlpp;
20061558Srgrimes	struct mountlist *mlp2;
20071558Srgrimes	FILE *mlfile;
20081558Srgrimes	int fnd = 0;
20091558Srgrimes
20101558Srgrimes	mlpp = &mlhead;
20111558Srgrimes	mlp = mlhead;
20121558Srgrimes	while (mlp) {
20131558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
20141558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
20151558Srgrimes			fnd = 1;
20161558Srgrimes			mlp2 = mlp;
20171558Srgrimes			*mlpp = mlp = mlp->ml_next;
20181558Srgrimes			free((caddr_t)mlp2);
20191558Srgrimes		} else {
20201558Srgrimes			mlpp = &mlp->ml_next;
20211558Srgrimes			mlp = mlp->ml_next;
20221558Srgrimes		}
20231558Srgrimes	}
20241558Srgrimes	if (fnd) {
20251558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
20261558Srgrimes			syslog(LOG_ERR,"Can't update %s", _PATH_RMOUNTLIST);
20271558Srgrimes			return;
20281558Srgrimes		}
20291558Srgrimes		mlp = mlhead;
20301558Srgrimes		while (mlp) {
20311558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20321558Srgrimes			mlp = mlp->ml_next;
20331558Srgrimes		}
20341558Srgrimes		fclose(mlfile);
20351558Srgrimes	}
20361558Srgrimes}
20371558Srgrimes
20381558Srgrimesvoid
20391558Srgrimesadd_mlist(hostp, dirp)
20401558Srgrimes	char *hostp, *dirp;
20411558Srgrimes{
20421558Srgrimes	struct mountlist *mlp, **mlpp;
20431558Srgrimes	FILE *mlfile;
20441558Srgrimes
20451558Srgrimes	mlpp = &mlhead;
20461558Srgrimes	mlp = mlhead;
20471558Srgrimes	while (mlp) {
20481558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
20491558Srgrimes			return;
20501558Srgrimes		mlpp = &mlp->ml_next;
20511558Srgrimes		mlp = mlp->ml_next;
20521558Srgrimes	}
20531558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
20541558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
20551558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
20561558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
20571558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
20581558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
20591558Srgrimes	*mlpp = mlp;
20601558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
20611558Srgrimes		syslog(LOG_ERR, "Can't update %s", _PATH_RMOUNTLIST);
20621558Srgrimes		return;
20631558Srgrimes	}
20641558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20651558Srgrimes	fclose(mlfile);
20661558Srgrimes}
20671558Srgrimes
20681558Srgrimes/*
20691558Srgrimes * This function is called via. SIGTERM when the system is going down.
20701558Srgrimes * It sends a broadcast RPCMNT_UMNTALL.
20711558Srgrimes */
20721558Srgrimesvoid
20731558Srgrimessend_umntall()
20741558Srgrimes{
20751558Srgrimes	(void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
20761558Srgrimes		xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
20771558Srgrimes	exit(0);
20781558Srgrimes}
20791558Srgrimes
20801558Srgrimesint
20811558Srgrimesumntall_each(resultsp, raddr)
20821558Srgrimes	caddr_t resultsp;
20831558Srgrimes	struct sockaddr_in *raddr;
20841558Srgrimes{
20851558Srgrimes	return (1);
20861558Srgrimes}
20871558Srgrimes
20881558Srgrimes/*
20891558Srgrimes * Free up a group list.
20901558Srgrimes */
20911558Srgrimesvoid
20921558Srgrimesfree_grp(grp)
20931558Srgrimes	struct grouplist *grp;
20941558Srgrimes{
20951558Srgrimes	char **addrp;
20961558Srgrimes
20971558Srgrimes	if (grp->gr_type == GT_HOST) {
20981558Srgrimes		if (grp->gr_ptr.gt_hostent->h_name) {
20991558Srgrimes			addrp = grp->gr_ptr.gt_hostent->h_addr_list;
21001558Srgrimes			while (addrp && *addrp)
21011558Srgrimes				free(*addrp++);
21021558Srgrimes			free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
21031558Srgrimes			free(grp->gr_ptr.gt_hostent->h_name);
21041558Srgrimes		}
21051558Srgrimes		free((caddr_t)grp->gr_ptr.gt_hostent);
21061558Srgrimes	} else if (grp->gr_type == GT_NET) {
21071558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
21081558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
21091558Srgrimes	}
21101558Srgrimes#ifdef ISO
21111558Srgrimes	else if (grp->gr_type == GT_ISO)
21121558Srgrimes		free((caddr_t)grp->gr_ptr.gt_isoaddr);
21131558Srgrimes#endif
21141558Srgrimes	free((caddr_t)grp);
21151558Srgrimes}
21161558Srgrimes
21171558Srgrimes#ifdef DEBUG
21181558Srgrimesvoid
21191558SrgrimesSYSLOG(int pri, const char *fmt, ...)
21201558Srgrimes{
21211558Srgrimes	va_list ap;
21221558Srgrimes
21231558Srgrimes	va_start(ap, fmt);
21241558Srgrimes	vfprintf(stderr, fmt, ap);
21251558Srgrimes	va_end(ap);
21261558Srgrimes}
21271558Srgrimes#endif /* DEBUG */
21281558Srgrimes
21291558Srgrimes/*
21301558Srgrimes * Check options for consistency.
21311558Srgrimes */
21321558Srgrimesint
21331558Srgrimescheck_options(dp)
21341558Srgrimes	struct dirlist *dp;
21351558Srgrimes{
21361558Srgrimes
21371558Srgrimes	if (dp == (struct dirlist *)NULL)
21381558Srgrimes	    return (1);
21391558Srgrimes	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
21401558Srgrimes	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
21411558Srgrimes	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
21421558Srgrimes	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
21431558Srgrimes	    return (1);
21441558Srgrimes	}
21451558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
21461558Srgrimes	    syslog(LOG_ERR, "-mask requires -net");
21471558Srgrimes	    return (1);
21481558Srgrimes	}
21491558Srgrimes	if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
21501558Srgrimes	    syslog(LOG_ERR, "-net and -iso mutually exclusive");
21511558Srgrimes	    return (1);
21521558Srgrimes	}
21531558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
21541558Srgrimes	    syslog(LOG_ERR, "-alldir has multiple directories");
21551558Srgrimes	    return (1);
21561558Srgrimes	}
21571558Srgrimes	return (0);
21581558Srgrimes}
21591558Srgrimes
21601558Srgrimes/*
21611558Srgrimes * Check an absolute directory path for any symbolic links. Return true
21621558Srgrimes * if no symbolic links are found.
21631558Srgrimes */
21641558Srgrimesint
21651558Srgrimescheck_dirpath(dirp)
21661558Srgrimes	char *dirp;
21671558Srgrimes{
21681558Srgrimes	char *cp;
21691558Srgrimes	int ret = 1;
21701558Srgrimes	struct stat sb;
21711558Srgrimes
21721558Srgrimes	cp = dirp + 1;
21731558Srgrimes	while (*cp && ret) {
21741558Srgrimes		if (*cp == '/') {
21751558Srgrimes			*cp = '\0';
21769336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21771558Srgrimes				ret = 0;
21781558Srgrimes			*cp = '/';
21791558Srgrimes		}
21801558Srgrimes		cp++;
21811558Srgrimes	}
21829336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21831558Srgrimes		ret = 0;
21841558Srgrimes	return (ret);
21851558Srgrimes}
21869336Sdfr
21879336Sdfr/*
21889336Sdfr * Just translate an ascii string to an integer.
21899336Sdfr */
21909336Sdfrint
21919336Sdfrget_num(cp)
21929336Sdfr	register char *cp;
21939336Sdfr{
21949336Sdfr	register int res = 0;
21959336Sdfr
21969336Sdfr	while (*cp) {
21979336Sdfr		if (*cp < '0' || *cp > '9')
21989336Sdfr			return (-1);
21999336Sdfr		res = res * 10 + (*cp++ - '0');
22009336Sdfr	}
22019336Sdfr	return (res);
22029336Sdfr}
2203