mountd.c revision 25087
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[] =
4625087Sdfr	"$Id: mountd.c,v 1.18 1997/04/09 20:17:15 guido 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;
1181558Srgrimes};
1191558Srgrimes/* ex_flag bits */
1201558Srgrimes#define	EX_LINKED	0x1
1211558Srgrimes
1221558Srgrimesstruct netmsk {
1231558Srgrimes	u_long	nt_net;
1241558Srgrimes	u_long	nt_mask;
1251558Srgrimes	char *nt_name;
1261558Srgrimes};
1271558Srgrimes
1281558Srgrimesunion grouptypes {
1291558Srgrimes	struct hostent *gt_hostent;
1301558Srgrimes	struct netmsk	gt_net;
1311558Srgrimes#ifdef ISO
1321558Srgrimes	struct sockaddr_iso *gt_isoaddr;
1331558Srgrimes#endif
1341558Srgrimes};
1351558Srgrimes
1361558Srgrimesstruct grouplist {
1371558Srgrimes	int gr_type;
1381558Srgrimes	union grouptypes gr_ptr;
1391558Srgrimes	struct grouplist *gr_next;
1401558Srgrimes};
1411558Srgrimes/* Group types */
1421558Srgrimes#define	GT_NULL		0x0
1431558Srgrimes#define	GT_HOST		0x1
1441558Srgrimes#define	GT_NET		0x2
1451558Srgrimes#define	GT_ISO		0x4
1467401Swpaul#define GT_IGNORE	0x5
1471558Srgrimes
1481558Srgrimesstruct hostlist {
1499336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1501558Srgrimes	struct grouplist *ht_grp;
1511558Srgrimes	struct hostlist	 *ht_next;
1521558Srgrimes};
1531558Srgrimes
1549336Sdfrstruct fhreturn {
1559336Sdfr	int	fhr_flag;
1569336Sdfr	int	fhr_vers;
1579336Sdfr	nfsfh_t	fhr_fh;
1589336Sdfr};
1599336Sdfr
1601558Srgrimes/* Global defs */
1611558Srgrimeschar	*add_expdir __P((struct dirlist **, char *, int));
1621558Srgrimesvoid	add_dlist __P((struct dirlist **, struct dirlist *,
1639336Sdfr				struct grouplist *, int));
1641558Srgrimesvoid	add_mlist __P((char *, char *));
1651558Srgrimesint	check_dirpath __P((char *));
1661558Srgrimesint	check_options __P((struct dirlist *));
1679336Sdfrint	chk_host __P((struct dirlist *, u_long, int *, int *));
1681558Srgrimesvoid	del_mlist __P((char *, char *));
1691558Srgrimesstruct dirlist *dirp_search __P((struct dirlist *, char *));
1701558Srgrimesint	do_mount __P((struct exportlist *, struct grouplist *, int,
1719336Sdfr		struct ucred *, char *, int, struct statfs *));
1721558Srgrimesint	do_opt __P((char **, char **, struct exportlist *, struct grouplist *,
1731558Srgrimes				int *, int *, struct ucred *));
1741558Srgrimesstruct	exportlist *ex_search __P((fsid_t *));
1751558Srgrimesstruct	exportlist *get_exp __P((void));
1761558Srgrimesvoid	free_dir __P((struct dirlist *));
1771558Srgrimesvoid	free_exp __P((struct exportlist *));
1781558Srgrimesvoid	free_grp __P((struct grouplist *));
1791558Srgrimesvoid	free_host __P((struct hostlist *));
1801558Srgrimesvoid	get_exportlist __P((void));
1817401Swpaulint	get_host __P((char *, struct grouplist *, struct grouplist *));
1829336Sdfrint	get_num __P((char *));
1831558Srgrimesstruct hostlist *get_ht __P((void));
1841558Srgrimesint	get_line __P((void));
1851558Srgrimesvoid	get_mountlist __P((void));
1861558Srgrimesint	get_net __P((char *, struct netmsk *, int));
1871558Srgrimesvoid	getexp_err __P((struct exportlist *, struct grouplist *));
1881558Srgrimesstruct grouplist *get_grp __P((void));
1891558Srgrimesvoid	hang_dirp __P((struct dirlist *, struct grouplist *,
1901558Srgrimes				struct exportlist *, int));
1911558Srgrimesvoid	mntsrv __P((struct svc_req *, SVCXPRT *));
1921558Srgrimesvoid	nextfield __P((char **, char **));
1931558Srgrimesvoid	out_of_mem __P((void));
1941558Srgrimesvoid	parsecred __P((char *, struct ucred *));
1951558Srgrimesint	put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
1961558Srgrimesint	scan_tree __P((struct dirlist *, u_long));
1971558Srgrimesvoid	send_umntall __P((void));
1981558Srgrimesint	umntall_each __P((caddr_t, struct sockaddr_in *));
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;
2261558Srgrimesint opt_flags;
2271558Srgrimes/* Bits for above */
2281558Srgrimes#define	OP_MAPROOT	0x01
2291558Srgrimes#define	OP_MAPALL	0x02
2301558Srgrimes#define	OP_KERB		0x04
2311558Srgrimes#define	OP_MASK		0x08
2321558Srgrimes#define	OP_NET		0x10
2331558Srgrimes#define	OP_ISO		0x20
2341558Srgrimes#define	OP_ALLDIRS	0x40
2351558Srgrimes
2361558Srgrimes#ifdef DEBUG
2371558Srgrimesint debug = 1;
2381558Srgrimesvoid	SYSLOG __P((int, const char *, ...));
2391558Srgrimes#define syslog SYSLOG
2401558Srgrimes#else
2411558Srgrimesint debug = 0;
2421558Srgrimes#endif
2431558Srgrimes
2441558Srgrimes/*
2451558Srgrimes * Mountd server for NFS mount protocol as described in:
2461558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2471558Srgrimes * The optional arguments are the exports file name
2481558Srgrimes * default: _PATH_EXPORTS
2491558Srgrimes * and "-n" to allow nonroot mount.
2501558Srgrimes */
2511558Srgrimesint
2521558Srgrimesmain(argc, argv)
2531558Srgrimes	int argc;
2541558Srgrimes	char **argv;
2551558Srgrimes{
2569202Srgrimes	SVCXPRT *udptransp, *tcptransp;
2571558Srgrimes	int c;
25824489Sbde	int mib[3];
2599336Sdfr#ifdef __FreeBSD__
26023681Speter	struct vfsconf vfc;
26123681Speter	int error;
2621558Srgrimes
26323681Speter	error = getvfsbyname("nfs", &vfc);
26423681Speter	if (error && vfsisloadable("nfs")) {
2652999Swollman		if(vfsload("nfs"))
2662999Swollman			err(1, "vfsload(nfs)");
2672999Swollman		endvfsent();	/* flush cache */
26823681Speter		error = getvfsbyname("nfs", &vfc);
2692999Swollman	}
27023681Speter	if (error)
2712999Swollman		errx(1, "NFS support is not available in the running kernel");
2729336Sdfr#endif	/* __FreeBSD__ */
2732999Swollman
27425087Sdfr	while ((c = getopt(argc, argv, "2dnr")) != -1)
2751558Srgrimes		switch (c) {
27625087Sdfr		case '2':
27725087Sdfr			force_v2 = 1;
27825087Sdfr			break;
2799336Sdfr		case 'n':
2809336Sdfr			resvport_only = 0;
2819336Sdfr			break;
2829336Sdfr		case 'r':
2839336Sdfr			dir_only = 0;
2849336Sdfr			break;
2858688Sphk		case 'd':
2868688Sphk			debug = debug ? 0 : 1;
2878688Sphk			break;
2881558Srgrimes		default:
28923681Speter			fprintf(stderr, "Usage: mountd [-d] [-r] [-n] [export_file]\n");
2901558Srgrimes			exit(1);
2911558Srgrimes		};
2921558Srgrimes	argc -= optind;
2931558Srgrimes	argv += optind;
2941558Srgrimes	grphead = (struct grouplist *)NULL;
2951558Srgrimes	exphead = (struct exportlist *)NULL;
2961558Srgrimes	mlhead = (struct mountlist *)NULL;
2971558Srgrimes	if (argc == 1) {
2981558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
2991558Srgrimes		exname[MAXPATHLEN-1] = '\0';
3001558Srgrimes	} else
3011558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3021558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3031558Srgrimes	if (debug)
3041558Srgrimes		fprintf(stderr,"Getting export list.\n");
3051558Srgrimes	get_exportlist();
3061558Srgrimes	if (debug)
3071558Srgrimes		fprintf(stderr,"Getting mount list.\n");
3081558Srgrimes	get_mountlist();
3091558Srgrimes	if (debug)
3101558Srgrimes		fprintf(stderr,"Here we go.\n");
3111558Srgrimes	if (debug == 0) {
3121558Srgrimes		daemon(0, 0);
3131558Srgrimes		signal(SIGINT, SIG_IGN);
3141558Srgrimes		signal(SIGQUIT, SIG_IGN);
3151558Srgrimes	}
3161558Srgrimes	signal(SIGHUP, (void (*) __P((int))) get_exportlist);
3171558Srgrimes	signal(SIGTERM, (void (*) __P((int))) send_umntall);
3181558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3191558Srgrimes	  if (pidfile != NULL) {
3201558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3211558Srgrimes		fclose(pidfile);
3221558Srgrimes	  }
3231558Srgrimes	}
32424330Sguido
32524759Sguido	if (!resvport_only) {
32624759Sguido		mib[0] = CTL_VFS;
32724759Sguido		mib[1] = MOUNT_NFS;
32824759Sguido		mib[2] = NFS_NFSPRIVPORT;
32924759Sguido		if (sysctl(mib, 3, NULL, NULL, &resvport_only,
33024759Sguido		    sizeof(resvport_only)) != 0 && errno != ENOENT) {
33124759Sguido			syslog(LOG_ERR, "sysctl: %m");
33224759Sguido			exit(1);
33324759Sguido		}
33424330Sguido	}
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;
3731558Srgrimes	u_long saddr;
3749336Sdfr	u_short sport;
37523681Speter	char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
3769336Sdfr	int bad = ENOENT, defset, hostset;
3779336Sdfr	sigset_t sighup_mask;
3781558Srgrimes
3799336Sdfr	sigemptyset(&sighup_mask);
3809336Sdfr	sigaddset(&sighup_mask, SIGHUP);
3811558Srgrimes	saddr = transp->xp_raddr.sin_addr.s_addr;
3829336Sdfr	sport = ntohs(transp->xp_raddr.sin_port);
3831558Srgrimes	hp = (struct hostent *)NULL;
3841558Srgrimes	switch (rqstp->rq_proc) {
3851558Srgrimes	case NULLPROC:
3861558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
3871558Srgrimes			syslog(LOG_ERR, "Can't send reply");
3881558Srgrimes		return;
3891558Srgrimes	case RPCMNT_MOUNT:
3909336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
3911558Srgrimes			svcerr_weakauth(transp);
3921558Srgrimes			return;
3931558Srgrimes		}
3941558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
3951558Srgrimes			svcerr_decode(transp);
3961558Srgrimes			return;
3971558Srgrimes		}
3981558Srgrimes
3991558Srgrimes		/*
4001558Srgrimes		 * Get the real pathname and make sure it is a directory
4019336Sdfr		 * or a regular file if the -r option was specified
4029336Sdfr		 * and it exists.
4031558Srgrimes		 */
4041558Srgrimes		if (realpath(rpcpath, dirpath) == 0 ||
4051558Srgrimes		    stat(dirpath, &stb) < 0 ||
4069336Sdfr		    (!S_ISDIR(stb.st_mode) &&
4079336Sdfr		     (dir_only || !S_ISREG(stb.st_mode))) ||
4081558Srgrimes		    statfs(dirpath, &fsb) < 0) {
4091558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
4101558Srgrimes			if (debug)
4111558Srgrimes				fprintf(stderr, "stat failed on %s\n", dirpath);
4121558Srgrimes			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
4131558Srgrimes				syslog(LOG_ERR, "Can't send reply");
4141558Srgrimes			return;
4151558Srgrimes		}
4161558Srgrimes
4171558Srgrimes		/* Check in the exports list */
4189336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
4191558Srgrimes		ep = ex_search(&fsb.f_fsid);
4209336Sdfr		hostset = defset = 0;
4219336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
4221558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
4239336Sdfr		     chk_host(dp, saddr, &defset, &hostset)) ||
4241558Srgrimes		     (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
4251558Srgrimes		      scan_tree(ep->ex_dirl, saddr) == 0))) {
4269336Sdfr			if (hostset & DP_HOSTSET)
4279336Sdfr				fhr.fhr_flag = hostset;
4289336Sdfr			else
4299336Sdfr				fhr.fhr_flag = defset;
4309336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
4311558Srgrimes			/* Get the file handle */
43223681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
4339336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
4341558Srgrimes				bad = errno;
4351558Srgrimes				syslog(LOG_ERR, "Can't get fh for %s", dirpath);
4361558Srgrimes				if (!svc_sendreply(transp, xdr_long,
4371558Srgrimes				    (caddr_t)&bad))
4381558Srgrimes					syslog(LOG_ERR, "Can't send reply");
4399336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
4401558Srgrimes				return;
4411558Srgrimes			}
4429336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
4431558Srgrimes				syslog(LOG_ERR, "Can't send reply");
4441558Srgrimes			if (hp == NULL)
4451558Srgrimes				hp = gethostbyaddr((caddr_t)&saddr,
4461558Srgrimes				    sizeof(saddr), AF_INET);
4471558Srgrimes			if (hp)
4481558Srgrimes				add_mlist(hp->h_name, dirpath);
4491558Srgrimes			else
4501558Srgrimes				add_mlist(inet_ntoa(transp->xp_raddr.sin_addr),
4511558Srgrimes					dirpath);
4521558Srgrimes			if (debug)
4531558Srgrimes				fprintf(stderr,"Mount successfull.\n");
4541558Srgrimes		} else {
4551558Srgrimes			bad = EACCES;
4561558Srgrimes			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
4571558Srgrimes				syslog(LOG_ERR, "Can't send reply");
4581558Srgrimes		}
4599336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
4601558Srgrimes		return;
4611558Srgrimes	case RPCMNT_DUMP:
4621558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
4631558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4641558Srgrimes		return;
4651558Srgrimes	case RPCMNT_UMOUNT:
4669336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
4671558Srgrimes			svcerr_weakauth(transp);
4681558Srgrimes			return;
4691558Srgrimes		}
4701558Srgrimes		if (!svc_getargs(transp, xdr_dir, dirpath)) {
4711558Srgrimes			svcerr_decode(transp);
4721558Srgrimes			return;
4731558Srgrimes		}
4741558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
4751558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4761558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
4771558Srgrimes		if (hp)
4781558Srgrimes			del_mlist(hp->h_name, dirpath);
4791558Srgrimes		del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), dirpath);
4801558Srgrimes		return;
4811558Srgrimes	case RPCMNT_UMNTALL:
4829336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
4831558Srgrimes			svcerr_weakauth(transp);
4841558Srgrimes			return;
4851558Srgrimes		}
4861558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
4871558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4881558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
4891558Srgrimes		if (hp)
4901558Srgrimes			del_mlist(hp->h_name, (char *)NULL);
4911558Srgrimes		del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), (char *)NULL);
4921558Srgrimes		return;
4931558Srgrimes	case RPCMNT_EXPORT:
4941558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
4951558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4961558Srgrimes		return;
4971558Srgrimes	default:
4981558Srgrimes		svcerr_noproc(transp);
4991558Srgrimes		return;
5001558Srgrimes	}
5011558Srgrimes}
5021558Srgrimes
5031558Srgrimes/*
5041558Srgrimes * Xdr conversion for a dirpath string
5051558Srgrimes */
5061558Srgrimesint
5071558Srgrimesxdr_dir(xdrsp, dirp)
5081558Srgrimes	XDR *xdrsp;
5091558Srgrimes	char *dirp;
5101558Srgrimes{
5111558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
5121558Srgrimes}
5131558Srgrimes
5141558Srgrimes/*
5159336Sdfr * Xdr routine to generate file handle reply
5161558Srgrimes */
5171558Srgrimesint
5189336Sdfrxdr_fhs(xdrsp, cp)
5191558Srgrimes	XDR *xdrsp;
5209336Sdfr	caddr_t cp;
5211558Srgrimes{
5229336Sdfr	register struct fhreturn *fhrp = (struct fhreturn *)cp;
5239336Sdfr	u_long ok = 0, len, auth;
5241558Srgrimes
5251558Srgrimes	if (!xdr_long(xdrsp, &ok))
5261558Srgrimes		return (0);
5279336Sdfr	switch (fhrp->fhr_vers) {
5289336Sdfr	case 1:
5299336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
5309336Sdfr	case 3:
5319336Sdfr		len = NFSX_V3FH;
5329336Sdfr		if (!xdr_long(xdrsp, &len))
5339336Sdfr			return (0);
5349336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
5359336Sdfr			return (0);
5369336Sdfr		if (fhrp->fhr_flag & DP_KERB)
5379336Sdfr			auth = RPCAUTH_KERB4;
5389336Sdfr		else
5399336Sdfr			auth = RPCAUTH_UNIX;
5409336Sdfr		len = 1;
5419336Sdfr		if (!xdr_long(xdrsp, &len))
5429336Sdfr			return (0);
5439336Sdfr		return (xdr_long(xdrsp, &auth));
5449336Sdfr	};
5459336Sdfr	return (0);
5461558Srgrimes}
5471558Srgrimes
5481558Srgrimesint
5491558Srgrimesxdr_mlist(xdrsp, cp)
5501558Srgrimes	XDR *xdrsp;
5511558Srgrimes	caddr_t cp;
5521558Srgrimes{
5531558Srgrimes	struct mountlist *mlp;
5541558Srgrimes	int true = 1;
5551558Srgrimes	int false = 0;
5561558Srgrimes	char *strp;
5571558Srgrimes
5581558Srgrimes	mlp = mlhead;
5591558Srgrimes	while (mlp) {
5601558Srgrimes		if (!xdr_bool(xdrsp, &true))
5611558Srgrimes			return (0);
5621558Srgrimes		strp = &mlp->ml_host[0];
5631558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
5641558Srgrimes			return (0);
5651558Srgrimes		strp = &mlp->ml_dirp[0];
5661558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
5671558Srgrimes			return (0);
5681558Srgrimes		mlp = mlp->ml_next;
5691558Srgrimes	}
5701558Srgrimes	if (!xdr_bool(xdrsp, &false))
5711558Srgrimes		return (0);
5721558Srgrimes	return (1);
5731558Srgrimes}
5741558Srgrimes
5751558Srgrimes/*
5761558Srgrimes * Xdr conversion for export list
5771558Srgrimes */
5781558Srgrimesint
5791558Srgrimesxdr_explist(xdrsp, cp)
5801558Srgrimes	XDR *xdrsp;
5811558Srgrimes	caddr_t cp;
5821558Srgrimes{
5831558Srgrimes	struct exportlist *ep;
5841558Srgrimes	int false = 0;
5859336Sdfr	int putdef;
5869336Sdfr	sigset_t sighup_mask;
5871558Srgrimes
5889336Sdfr	sigemptyset(&sighup_mask);
5899336Sdfr	sigaddset(&sighup_mask, SIGHUP);
5909336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
5911558Srgrimes	ep = exphead;
5921558Srgrimes	while (ep) {
5931558Srgrimes		putdef = 0;
5941558Srgrimes		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
5951558Srgrimes			goto errout;
5961558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
5971558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
5981558Srgrimes			&putdef))
5991558Srgrimes			goto errout;
6001558Srgrimes		ep = ep->ex_next;
6011558Srgrimes	}
6029336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6031558Srgrimes	if (!xdr_bool(xdrsp, &false))
6041558Srgrimes		return (0);
6051558Srgrimes	return (1);
6061558Srgrimeserrout:
6079336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6081558Srgrimes	return (0);
6091558Srgrimes}
6101558Srgrimes
6111558Srgrimes/*
6121558Srgrimes * Called from xdr_explist() to traverse the tree and export the
6131558Srgrimes * directory paths.
6141558Srgrimes */
6151558Srgrimesint
6161558Srgrimesput_exlist(dp, xdrsp, adp, putdefp)
6171558Srgrimes	struct dirlist *dp;
6181558Srgrimes	XDR *xdrsp;
6191558Srgrimes	struct dirlist *adp;
6201558Srgrimes	int *putdefp;
6211558Srgrimes{
6221558Srgrimes	struct grouplist *grp;
6231558Srgrimes	struct hostlist *hp;
6241558Srgrimes	int true = 1;
6251558Srgrimes	int false = 0;
6261558Srgrimes	int gotalldir = 0;
6271558Srgrimes	char *strp;
6281558Srgrimes
6291558Srgrimes	if (dp) {
6301558Srgrimes		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
6311558Srgrimes			return (1);
6321558Srgrimes		if (!xdr_bool(xdrsp, &true))
6331558Srgrimes			return (1);
6341558Srgrimes		strp = dp->dp_dirp;
6351558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6361558Srgrimes			return (1);
6371558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
6381558Srgrimes			gotalldir = 1;
6391558Srgrimes			*putdefp = 1;
6401558Srgrimes		}
6411558Srgrimes		if ((dp->dp_flag & DP_DEFSET) == 0 &&
6421558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
6431558Srgrimes			hp = dp->dp_hosts;
6441558Srgrimes			while (hp) {
6451558Srgrimes				grp = hp->ht_grp;
6461558Srgrimes				if (grp->gr_type == GT_HOST) {
6471558Srgrimes					if (!xdr_bool(xdrsp, &true))
6481558Srgrimes						return (1);
6491558Srgrimes					strp = grp->gr_ptr.gt_hostent->h_name;
6508871Srgrimes					if (!xdr_string(xdrsp, &strp,
6511558Srgrimes					    RPCMNT_NAMELEN))
6521558Srgrimes						return (1);
6531558Srgrimes				} else if (grp->gr_type == GT_NET) {
6541558Srgrimes					if (!xdr_bool(xdrsp, &true))
6551558Srgrimes						return (1);
6561558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
6578871Srgrimes					if (!xdr_string(xdrsp, &strp,
6581558Srgrimes					    RPCMNT_NAMELEN))
6591558Srgrimes						return (1);
6601558Srgrimes				}
6611558Srgrimes				hp = hp->ht_next;
6621558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
6631558Srgrimes					hp = adp->dp_hosts;
6641558Srgrimes					gotalldir = 0;
6651558Srgrimes				}
6661558Srgrimes			}
6671558Srgrimes		}
6681558Srgrimes		if (!xdr_bool(xdrsp, &false))
6691558Srgrimes			return (1);
6701558Srgrimes		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
6711558Srgrimes			return (1);
6721558Srgrimes	}
6731558Srgrimes	return (0);
6741558Srgrimes}
6751558Srgrimes
6761558Srgrimes#define LINESIZ	10240
6771558Srgrimeschar line[LINESIZ];
6781558SrgrimesFILE *exp_file;
6791558Srgrimes
6801558Srgrimes/*
6811558Srgrimes * Get the export list
6821558Srgrimes */
6831558Srgrimesvoid
6841558Srgrimesget_exportlist()
6851558Srgrimes{
6861558Srgrimes	struct exportlist *ep, *ep2;
6871558Srgrimes	struct grouplist *grp, *tgrp;
6881558Srgrimes	struct exportlist **epp;
6891558Srgrimes	struct dirlist *dirhead;
6901558Srgrimes	struct statfs fsb, *fsp;
6911558Srgrimes	struct hostent *hpe;
6921558Srgrimes	struct ucred anon;
6931558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
6941558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
6951558Srgrimes
6961558Srgrimes	/*
6971558Srgrimes	 * First, get rid of the old list
6981558Srgrimes	 */
6991558Srgrimes	ep = exphead;
7001558Srgrimes	while (ep) {
7011558Srgrimes		ep2 = ep;
7021558Srgrimes		ep = ep->ex_next;
7031558Srgrimes		free_exp(ep2);
7041558Srgrimes	}
7051558Srgrimes	exphead = (struct exportlist *)NULL;
7061558Srgrimes
7071558Srgrimes	grp = grphead;
7081558Srgrimes	while (grp) {
7091558Srgrimes		tgrp = grp;
7101558Srgrimes		grp = grp->gr_next;
7111558Srgrimes		free_grp(tgrp);
7121558Srgrimes	}
7131558Srgrimes	grphead = (struct grouplist *)NULL;
7141558Srgrimes
7151558Srgrimes	/*
7161558Srgrimes	 * And delete exports that are in the kernel for all local
7171558Srgrimes	 * file systems.
7181558Srgrimes	 * XXX: Should know how to handle all local exportable file systems
71923681Speter	 *      instead of just "ufs".
7201558Srgrimes	 */
7211558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
7221558Srgrimes	for (i = 0; i < num; i++) {
7231558Srgrimes		union {
7241558Srgrimes			struct ufs_args ua;
7251558Srgrimes			struct iso_args ia;
7261558Srgrimes			struct mfs_args ma;
7279336Sdfr			struct msdosfs_args da;
7281558Srgrimes		} targs;
7291558Srgrimes
73023681Speter		if (!strcmp(fsp->f_fstypename, "mfs") ||
73123681Speter		    !strcmp(fsp->f_fstypename, "ufs") ||
73223681Speter		    !strcmp(fsp->f_fstypename, "msdos") ||
73323681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
7349336Sdfr			targs.ua.fspec = NULL;
7359336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
7369336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
7371558Srgrimes				  fsp->f_flags | MNT_UPDATE,
7381558Srgrimes				  (caddr_t)&targs) < 0)
7391558Srgrimes				syslog(LOG_ERR, "Can't delete exports for %s",
7401558Srgrimes				       fsp->f_mntonname);
7411558Srgrimes		}
7421558Srgrimes		fsp++;
7431558Srgrimes	}
7441558Srgrimes
7451558Srgrimes	/*
7461558Srgrimes	 * Read in the exports file and build the list, calling
7471558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
7481558Srgrimes	 */
7491558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
7501558Srgrimes		syslog(LOG_ERR, "Can't open %s", exname);
7511558Srgrimes		exit(2);
7521558Srgrimes	}
7531558Srgrimes	dirhead = (struct dirlist *)NULL;
7541558Srgrimes	while (get_line()) {
7551558Srgrimes		if (debug)
7561558Srgrimes			fprintf(stderr,"Got line %s\n",line);
7571558Srgrimes		cp = line;
7581558Srgrimes		nextfield(&cp, &endcp);
7591558Srgrimes		if (*cp == '#')
7601558Srgrimes			goto nextline;
7611558Srgrimes
7621558Srgrimes		/*
7631558Srgrimes		 * Set defaults.
7641558Srgrimes		 */
7651558Srgrimes		has_host = FALSE;
7661558Srgrimes		anon = def_anon;
7671558Srgrimes		exflags = MNT_EXPORTED;
7681558Srgrimes		got_nondir = 0;
7691558Srgrimes		opt_flags = 0;
7701558Srgrimes		ep = (struct exportlist *)NULL;
7711558Srgrimes
7721558Srgrimes		/*
7731558Srgrimes		 * Create new exports list entry
7741558Srgrimes		 */
7751558Srgrimes		len = endcp-cp;
7761558Srgrimes		tgrp = grp = get_grp();
7771558Srgrimes		while (len > 0) {
7781558Srgrimes			if (len > RPCMNT_NAMELEN) {
7791558Srgrimes			    getexp_err(ep, tgrp);
7801558Srgrimes			    goto nextline;
7811558Srgrimes			}
7821558Srgrimes			if (*cp == '-') {
7831558Srgrimes			    if (ep == (struct exportlist *)NULL) {
7841558Srgrimes				getexp_err(ep, tgrp);
7851558Srgrimes				goto nextline;
7861558Srgrimes			    }
7871558Srgrimes			    if (debug)
7881558Srgrimes				fprintf(stderr, "doing opt %s\n", cp);
7891558Srgrimes			    got_nondir = 1;
7901558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
7911558Srgrimes				&exflags, &anon)) {
7921558Srgrimes				getexp_err(ep, tgrp);
7931558Srgrimes				goto nextline;
7941558Srgrimes			    }
7951558Srgrimes			} else if (*cp == '/') {
7961558Srgrimes			    savedc = *endcp;
7971558Srgrimes			    *endcp = '\0';
7981558Srgrimes			    if (check_dirpath(cp) &&
7991558Srgrimes				statfs(cp, &fsb) >= 0) {
8001558Srgrimes				if (got_nondir) {
8011558Srgrimes				    syslog(LOG_ERR, "Dirs must be first");
8021558Srgrimes				    getexp_err(ep, tgrp);
8031558Srgrimes				    goto nextline;
8041558Srgrimes				}
8051558Srgrimes				if (ep) {
8061558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
8071558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
8081558Srgrimes					getexp_err(ep, tgrp);
8091558Srgrimes					goto nextline;
8101558Srgrimes				    }
8111558Srgrimes				} else {
8121558Srgrimes				    /*
8131558Srgrimes				     * See if this directory is already
8141558Srgrimes				     * in the list.
8151558Srgrimes				     */
8161558Srgrimes				    ep = ex_search(&fsb.f_fsid);
8171558Srgrimes				    if (ep == (struct exportlist *)NULL) {
8181558Srgrimes					ep = get_exp();
8191558Srgrimes					ep->ex_fs = fsb.f_fsid;
8201558Srgrimes					ep->ex_fsdir = (char *)
8211558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
8221558Srgrimes					if (ep->ex_fsdir)
8231558Srgrimes					    strcpy(ep->ex_fsdir,
8241558Srgrimes						fsb.f_mntonname);
8251558Srgrimes					else
8261558Srgrimes					    out_of_mem();
8271558Srgrimes					if (debug)
8281558Srgrimes					  fprintf(stderr,
8291558Srgrimes					      "Making new ep fs=0x%x,0x%x\n",
8301558Srgrimes					      fsb.f_fsid.val[0],
8311558Srgrimes					      fsb.f_fsid.val[1]);
8321558Srgrimes				    } else if (debug)
8331558Srgrimes					fprintf(stderr,
8341558Srgrimes					    "Found ep fs=0x%x,0x%x\n",
8351558Srgrimes					    fsb.f_fsid.val[0],
8361558Srgrimes					    fsb.f_fsid.val[1]);
8371558Srgrimes				}
8381558Srgrimes
8391558Srgrimes				/*
8401558Srgrimes				 * Add dirpath to export mount point.
8411558Srgrimes				 */
8421558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
8431558Srgrimes				dirplen = len;
8441558Srgrimes			    } else {
8451558Srgrimes				getexp_err(ep, tgrp);
8461558Srgrimes				goto nextline;
8471558Srgrimes			    }
8481558Srgrimes			    *endcp = savedc;
8491558Srgrimes			} else {
8501558Srgrimes			    savedc = *endcp;
8511558Srgrimes			    *endcp = '\0';
8521558Srgrimes			    got_nondir = 1;
8531558Srgrimes			    if (ep == (struct exportlist *)NULL) {
8541558Srgrimes				getexp_err(ep, tgrp);
8551558Srgrimes				goto nextline;
8561558Srgrimes			    }
8571558Srgrimes
8581558Srgrimes			    /*
8591558Srgrimes			     * Get the host or netgroup.
8601558Srgrimes			     */
8611558Srgrimes			    setnetgrent(cp);
8621558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
8631558Srgrimes			    do {
8641558Srgrimes				if (has_host) {
8651558Srgrimes				    grp->gr_next = get_grp();
8661558Srgrimes				    grp = grp->gr_next;
8671558Srgrimes				}
8681558Srgrimes				if (netgrp) {
8697401Swpaul				    if (get_host(hst, grp, tgrp)) {
8701558Srgrimes					syslog(LOG_ERR, "Bad netgroup %s", cp);
8711558Srgrimes					getexp_err(ep, tgrp);
8729336Sdfr					endnetgrent();
8731558Srgrimes					goto nextline;
8741558Srgrimes				    }
8757401Swpaul				} else if (get_host(cp, grp, tgrp)) {
8761558Srgrimes				    getexp_err(ep, tgrp);
8771558Srgrimes				    goto nextline;
8781558Srgrimes				}
8791558Srgrimes				has_host = TRUE;
8801558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
8811558Srgrimes			    endnetgrent();
8821558Srgrimes			    *endcp = savedc;
8831558Srgrimes			}
8841558Srgrimes			cp = endcp;
8851558Srgrimes			nextfield(&cp, &endcp);
8861558Srgrimes			len = endcp - cp;
8871558Srgrimes		}
8881558Srgrimes		if (check_options(dirhead)) {
8891558Srgrimes			getexp_err(ep, tgrp);
8901558Srgrimes			goto nextline;
8911558Srgrimes		}
8921558Srgrimes		if (!has_host) {
8931558Srgrimes			grp->gr_type = GT_HOST;
8941558Srgrimes			if (debug)
8951558Srgrimes				fprintf(stderr,"Adding a default entry\n");
8961558Srgrimes			/* add a default group and make the grp list NULL */
8971558Srgrimes			hpe = (struct hostent *)malloc(sizeof(struct hostent));
8981558Srgrimes			if (hpe == (struct hostent *)NULL)
8991558Srgrimes				out_of_mem();
90012348Sjoerg			hpe->h_name = strdup("Default");
9011558Srgrimes			hpe->h_addrtype = AF_INET;
9021558Srgrimes			hpe->h_length = sizeof (u_long);
9031558Srgrimes			hpe->h_addr_list = (char **)NULL;
9041558Srgrimes			grp->gr_ptr.gt_hostent = hpe;
9051558Srgrimes
9061558Srgrimes		/*
9071558Srgrimes		 * Don't allow a network export coincide with a list of
9081558Srgrimes		 * host(s) on the same line.
9091558Srgrimes		 */
9101558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
9111558Srgrimes			getexp_err(ep, tgrp);
9121558Srgrimes			goto nextline;
9131558Srgrimes		}
9141558Srgrimes
9151558Srgrimes		/*
9161558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
9171558Srgrimes		 * After loop, tgrp points to the start of the list and
9181558Srgrimes		 * grp points to the last entry in the list.
9191558Srgrimes		 */
9201558Srgrimes		grp = tgrp;
9211558Srgrimes		do {
9221558Srgrimes		    if (do_mount(ep, grp, exflags, &anon, dirp,
9231558Srgrimes			dirplen, &fsb)) {
9241558Srgrimes			getexp_err(ep, tgrp);
9251558Srgrimes			goto nextline;
9261558Srgrimes		    }
9271558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
9281558Srgrimes
9291558Srgrimes		/*
9301558Srgrimes		 * Success. Update the data structures.
9311558Srgrimes		 */
9321558Srgrimes		if (has_host) {
9339336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
9341558Srgrimes			grp->gr_next = grphead;
9351558Srgrimes			grphead = tgrp;
9361558Srgrimes		} else {
9371558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
9389336Sdfr				opt_flags);
9391558Srgrimes			free_grp(grp);
9401558Srgrimes		}
9411558Srgrimes		dirhead = (struct dirlist *)NULL;
9421558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
9431558Srgrimes			ep2 = exphead;
9441558Srgrimes			epp = &exphead;
9451558Srgrimes
9461558Srgrimes			/*
9471558Srgrimes			 * Insert in the list in alphabetical order.
9481558Srgrimes			 */
9491558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
9501558Srgrimes				epp = &ep2->ex_next;
9511558Srgrimes				ep2 = ep2->ex_next;
9521558Srgrimes			}
9531558Srgrimes			if (ep2)
9541558Srgrimes				ep->ex_next = ep2;
9551558Srgrimes			*epp = ep;
9561558Srgrimes			ep->ex_flag |= EX_LINKED;
9571558Srgrimes		}
9581558Srgrimesnextline:
9591558Srgrimes		if (dirhead) {
9601558Srgrimes			free_dir(dirhead);
9611558Srgrimes			dirhead = (struct dirlist *)NULL;
9621558Srgrimes		}
9631558Srgrimes	}
9641558Srgrimes	fclose(exp_file);
9651558Srgrimes}
9661558Srgrimes
9671558Srgrimes/*
9681558Srgrimes * Allocate an export list element
9691558Srgrimes */
9701558Srgrimesstruct exportlist *
9711558Srgrimesget_exp()
9721558Srgrimes{
9731558Srgrimes	struct exportlist *ep;
9741558Srgrimes
9751558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
9761558Srgrimes	if (ep == (struct exportlist *)NULL)
9771558Srgrimes		out_of_mem();
97823681Speter	memset(ep, 0, sizeof(struct exportlist));
9791558Srgrimes	return (ep);
9801558Srgrimes}
9811558Srgrimes
9821558Srgrimes/*
9831558Srgrimes * Allocate a group list element
9841558Srgrimes */
9851558Srgrimesstruct grouplist *
9861558Srgrimesget_grp()
9871558Srgrimes{
9881558Srgrimes	struct grouplist *gp;
9891558Srgrimes
9901558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
9911558Srgrimes	if (gp == (struct grouplist *)NULL)
9921558Srgrimes		out_of_mem();
99323681Speter	memset(gp, 0, sizeof(struct grouplist));
9941558Srgrimes	return (gp);
9951558Srgrimes}
9961558Srgrimes
9971558Srgrimes/*
9981558Srgrimes * Clean up upon an error in get_exportlist().
9991558Srgrimes */
10001558Srgrimesvoid
10011558Srgrimesgetexp_err(ep, grp)
10021558Srgrimes	struct exportlist *ep;
10031558Srgrimes	struct grouplist *grp;
10041558Srgrimes{
10051558Srgrimes	struct grouplist *tgrp;
10061558Srgrimes
10071558Srgrimes	syslog(LOG_ERR, "Bad exports list line %s", line);
10081558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
10091558Srgrimes		free_exp(ep);
10101558Srgrimes	while (grp) {
10111558Srgrimes		tgrp = grp;
10121558Srgrimes		grp = grp->gr_next;
10131558Srgrimes		free_grp(tgrp);
10141558Srgrimes	}
10151558Srgrimes}
10161558Srgrimes
10171558Srgrimes/*
10181558Srgrimes * Search the export list for a matching fs.
10191558Srgrimes */
10201558Srgrimesstruct exportlist *
10211558Srgrimesex_search(fsid)
10221558Srgrimes	fsid_t *fsid;
10231558Srgrimes{
10241558Srgrimes	struct exportlist *ep;
10251558Srgrimes
10261558Srgrimes	ep = exphead;
10271558Srgrimes	while (ep) {
10281558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
10291558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
10301558Srgrimes			return (ep);
10311558Srgrimes		ep = ep->ex_next;
10321558Srgrimes	}
10331558Srgrimes	return (ep);
10341558Srgrimes}
10351558Srgrimes
10361558Srgrimes/*
10371558Srgrimes * Add a directory path to the list.
10381558Srgrimes */
10391558Srgrimeschar *
10401558Srgrimesadd_expdir(dpp, cp, len)
10411558Srgrimes	struct dirlist **dpp;
10421558Srgrimes	char *cp;
10431558Srgrimes	int len;
10441558Srgrimes{
10451558Srgrimes	struct dirlist *dp;
10461558Srgrimes
10471558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
10481558Srgrimes	dp->dp_left = *dpp;
10491558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
10501558Srgrimes	dp->dp_flag = 0;
10511558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
10521558Srgrimes	strcpy(dp->dp_dirp, cp);
10531558Srgrimes	*dpp = dp;
10541558Srgrimes	return (dp->dp_dirp);
10551558Srgrimes}
10561558Srgrimes
10571558Srgrimes/*
10581558Srgrimes * Hang the dir list element off the dirpath binary tree as required
10591558Srgrimes * and update the entry for host.
10601558Srgrimes */
10611558Srgrimesvoid
10629336Sdfrhang_dirp(dp, grp, ep, flags)
10631558Srgrimes	struct dirlist *dp;
10641558Srgrimes	struct grouplist *grp;
10651558Srgrimes	struct exportlist *ep;
10669336Sdfr	int flags;
10671558Srgrimes{
10681558Srgrimes	struct hostlist *hp;
10691558Srgrimes	struct dirlist *dp2;
10701558Srgrimes
10719336Sdfr	if (flags & OP_ALLDIRS) {
10721558Srgrimes		if (ep->ex_defdir)
10731558Srgrimes			free((caddr_t)dp);
10741558Srgrimes		else
10751558Srgrimes			ep->ex_defdir = dp;
10769336Sdfr		if (grp == (struct grouplist *)NULL) {
10771558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
10789336Sdfr			if (flags & OP_KERB)
10799336Sdfr				ep->ex_defdir->dp_flag |= DP_KERB;
10809336Sdfr		} else while (grp) {
10811558Srgrimes			hp = get_ht();
10829336Sdfr			if (flags & OP_KERB)
10839336Sdfr				hp->ht_flag |= DP_KERB;
10841558Srgrimes			hp->ht_grp = grp;
10851558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
10861558Srgrimes			ep->ex_defdir->dp_hosts = hp;
10871558Srgrimes			grp = grp->gr_next;
10881558Srgrimes		}
10891558Srgrimes	} else {
10901558Srgrimes
10911558Srgrimes		/*
10921558Srgrimes		 * Loop throught the directories adding them to the tree.
10931558Srgrimes		 */
10941558Srgrimes		while (dp) {
10951558Srgrimes			dp2 = dp->dp_left;
10969336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
10971558Srgrimes			dp = dp2;
10981558Srgrimes		}
10991558Srgrimes	}
11001558Srgrimes}
11011558Srgrimes
11021558Srgrimes/*
11031558Srgrimes * Traverse the binary tree either updating a node that is already there
11041558Srgrimes * for the new directory or adding the new node.
11051558Srgrimes */
11061558Srgrimesvoid
11079336Sdfradd_dlist(dpp, newdp, grp, flags)
11081558Srgrimes	struct dirlist **dpp;
11091558Srgrimes	struct dirlist *newdp;
11101558Srgrimes	struct grouplist *grp;
11119336Sdfr	int flags;
11121558Srgrimes{
11131558Srgrimes	struct dirlist *dp;
11141558Srgrimes	struct hostlist *hp;
11151558Srgrimes	int cmp;
11161558Srgrimes
11171558Srgrimes	dp = *dpp;
11181558Srgrimes	if (dp) {
11191558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
11201558Srgrimes		if (cmp > 0) {
11219336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
11221558Srgrimes			return;
11231558Srgrimes		} else if (cmp < 0) {
11249336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
11251558Srgrimes			return;
11261558Srgrimes		} else
11271558Srgrimes			free((caddr_t)newdp);
11281558Srgrimes	} else {
11291558Srgrimes		dp = newdp;
11301558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
11311558Srgrimes		*dpp = dp;
11321558Srgrimes	}
11331558Srgrimes	if (grp) {
11341558Srgrimes
11351558Srgrimes		/*
11361558Srgrimes		 * Hang all of the host(s) off of the directory point.
11371558Srgrimes		 */
11381558Srgrimes		do {
11391558Srgrimes			hp = get_ht();
11409336Sdfr			if (flags & OP_KERB)
11419336Sdfr				hp->ht_flag |= DP_KERB;
11421558Srgrimes			hp->ht_grp = grp;
11431558Srgrimes			hp->ht_next = dp->dp_hosts;
11441558Srgrimes			dp->dp_hosts = hp;
11451558Srgrimes			grp = grp->gr_next;
11461558Srgrimes		} while (grp);
11479336Sdfr	} else {
11481558Srgrimes		dp->dp_flag |= DP_DEFSET;
11499336Sdfr		if (flags & OP_KERB)
11509336Sdfr			dp->dp_flag |= DP_KERB;
11519336Sdfr	}
11521558Srgrimes}
11531558Srgrimes
11541558Srgrimes/*
11551558Srgrimes * Search for a dirpath on the export point.
11561558Srgrimes */
11571558Srgrimesstruct dirlist *
11581558Srgrimesdirp_search(dp, dirpath)
11591558Srgrimes	struct dirlist *dp;
11601558Srgrimes	char *dirpath;
11611558Srgrimes{
11621558Srgrimes	int cmp;
11631558Srgrimes
11641558Srgrimes	if (dp) {
11651558Srgrimes		cmp = strcmp(dp->dp_dirp, dirpath);
11661558Srgrimes		if (cmp > 0)
11671558Srgrimes			return (dirp_search(dp->dp_left, dirpath));
11681558Srgrimes		else if (cmp < 0)
11691558Srgrimes			return (dirp_search(dp->dp_right, dirpath));
11701558Srgrimes		else
11711558Srgrimes			return (dp);
11721558Srgrimes	}
11731558Srgrimes	return (dp);
11741558Srgrimes}
11751558Srgrimes
11761558Srgrimes/*
11771558Srgrimes * Scan for a host match in a directory tree.
11781558Srgrimes */
11791558Srgrimesint
11809336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
11811558Srgrimes	struct dirlist *dp;
11821558Srgrimes	u_long saddr;
11831558Srgrimes	int *defsetp;
11849336Sdfr	int *hostsetp;
11851558Srgrimes{
11861558Srgrimes	struct hostlist *hp;
11871558Srgrimes	struct grouplist *grp;
11881558Srgrimes	u_long **addrp;
11891558Srgrimes
11901558Srgrimes	if (dp) {
11911558Srgrimes		if (dp->dp_flag & DP_DEFSET)
11929336Sdfr			*defsetp = dp->dp_flag;
11931558Srgrimes		hp = dp->dp_hosts;
11941558Srgrimes		while (hp) {
11951558Srgrimes			grp = hp->ht_grp;
11961558Srgrimes			switch (grp->gr_type) {
11971558Srgrimes			case GT_HOST:
11981558Srgrimes			    addrp = (u_long **)
11991558Srgrimes				grp->gr_ptr.gt_hostent->h_addr_list;
12001558Srgrimes			    while (*addrp) {
12019336Sdfr				if (**addrp == saddr) {
12029336Sdfr				    *hostsetp = (hp->ht_flag | DP_HOSTSET);
12031558Srgrimes				    return (1);
12049336Sdfr				}
12051558Srgrimes				addrp++;
12061558Srgrimes			    }
12071558Srgrimes			    break;
12081558Srgrimes			case GT_NET:
12091558Srgrimes			    if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
12109336Sdfr				grp->gr_ptr.gt_net.nt_net) {
12119336Sdfr				*hostsetp = (hp->ht_flag | DP_HOSTSET);
12121558Srgrimes				return (1);
12139336Sdfr			    }
12141558Srgrimes			    break;
12151558Srgrimes			};
12161558Srgrimes			hp = hp->ht_next;
12171558Srgrimes		}
12181558Srgrimes	}
12191558Srgrimes	return (0);
12201558Srgrimes}
12211558Srgrimes
12221558Srgrimes/*
12231558Srgrimes * Scan tree for a host that matches the address.
12241558Srgrimes */
12251558Srgrimesint
12261558Srgrimesscan_tree(dp, saddr)
12271558Srgrimes	struct dirlist *dp;
12281558Srgrimes	u_long saddr;
12291558Srgrimes{
12309336Sdfr	int defset, hostset;
12311558Srgrimes
12321558Srgrimes	if (dp) {
12331558Srgrimes		if (scan_tree(dp->dp_left, saddr))
12341558Srgrimes			return (1);
12359336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
12361558Srgrimes			return (1);
12371558Srgrimes		if (scan_tree(dp->dp_right, saddr))
12381558Srgrimes			return (1);
12391558Srgrimes	}
12401558Srgrimes	return (0);
12411558Srgrimes}
12421558Srgrimes
12431558Srgrimes/*
12441558Srgrimes * Traverse the dirlist tree and free it up.
12451558Srgrimes */
12461558Srgrimesvoid
12471558Srgrimesfree_dir(dp)
12481558Srgrimes	struct dirlist *dp;
12491558Srgrimes{
12501558Srgrimes
12511558Srgrimes	if (dp) {
12521558Srgrimes		free_dir(dp->dp_left);
12531558Srgrimes		free_dir(dp->dp_right);
12541558Srgrimes		free_host(dp->dp_hosts);
12551558Srgrimes		free((caddr_t)dp);
12561558Srgrimes	}
12571558Srgrimes}
12581558Srgrimes
12591558Srgrimes/*
12601558Srgrimes * Parse the option string and update fields.
12611558Srgrimes * Option arguments may either be -<option>=<value> or
12621558Srgrimes * -<option> <value>
12631558Srgrimes */
12641558Srgrimesint
12651558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
12661558Srgrimes	char **cpp, **endcpp;
12671558Srgrimes	struct exportlist *ep;
12681558Srgrimes	struct grouplist *grp;
12691558Srgrimes	int *has_hostp;
12701558Srgrimes	int *exflagsp;
12711558Srgrimes	struct ucred *cr;
12721558Srgrimes{
12731558Srgrimes	char *cpoptarg, *cpoptend;
12741558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
12751558Srgrimes	int allflag, usedarg;
12761558Srgrimes
12771558Srgrimes	cpopt = *cpp;
12781558Srgrimes	cpopt++;
12791558Srgrimes	cp = *endcpp;
12801558Srgrimes	savedc = *cp;
12811558Srgrimes	*cp = '\0';
12821558Srgrimes	while (cpopt && *cpopt) {
12831558Srgrimes		allflag = 1;
12841558Srgrimes		usedarg = -2;
128523681Speter		if (cpoptend = strchr(cpopt, ',')) {
12861558Srgrimes			*cpoptend++ = '\0';
128723681Speter			if (cpoptarg = strchr(cpopt, '='))
12881558Srgrimes				*cpoptarg++ = '\0';
12891558Srgrimes		} else {
129023681Speter			if (cpoptarg = strchr(cpopt, '='))
12911558Srgrimes				*cpoptarg++ = '\0';
12921558Srgrimes			else {
12931558Srgrimes				*cp = savedc;
12941558Srgrimes				nextfield(&cp, &endcp);
12951558Srgrimes				**endcpp = '\0';
12961558Srgrimes				if (endcp > cp && *cp != '-') {
12971558Srgrimes					cpoptarg = cp;
12981558Srgrimes					savedc2 = *endcp;
12991558Srgrimes					*endcp = '\0';
13001558Srgrimes					usedarg = 0;
13011558Srgrimes				}
13021558Srgrimes			}
13031558Srgrimes		}
13041558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
13051558Srgrimes			*exflagsp |= MNT_EXRDONLY;
13061558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
13071558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
13081558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
13091558Srgrimes			usedarg++;
13101558Srgrimes			parsecred(cpoptarg, cr);
13111558Srgrimes			if (allflag == 0) {
13121558Srgrimes				*exflagsp |= MNT_EXPORTANON;
13131558Srgrimes				opt_flags |= OP_MAPALL;
13141558Srgrimes			} else
13151558Srgrimes				opt_flags |= OP_MAPROOT;
13161558Srgrimes		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
13171558Srgrimes			*exflagsp |= MNT_EXKERB;
13181558Srgrimes			opt_flags |= OP_KERB;
13191558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
13201558Srgrimes			!strcmp(cpopt, "m"))) {
13211558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
13221558Srgrimes				syslog(LOG_ERR, "Bad mask: %s", cpoptarg);
13231558Srgrimes				return (1);
13241558Srgrimes			}
13251558Srgrimes			usedarg++;
13261558Srgrimes			opt_flags |= OP_MASK;
13271558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
13281558Srgrimes			!strcmp(cpopt, "n"))) {
13291558Srgrimes			if (grp->gr_type != GT_NULL) {
13301558Srgrimes				syslog(LOG_ERR, "Network/host conflict");
13311558Srgrimes				return (1);
13321558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
13331558Srgrimes				syslog(LOG_ERR, "Bad net: %s", cpoptarg);
13341558Srgrimes				return (1);
13351558Srgrimes			}
13361558Srgrimes			grp->gr_type = GT_NET;
13371558Srgrimes			*has_hostp = 1;
13381558Srgrimes			usedarg++;
13391558Srgrimes			opt_flags |= OP_NET;
13401558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
13411558Srgrimes			opt_flags |= OP_ALLDIRS;
13421558Srgrimes#ifdef ISO
13431558Srgrimes		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
13441558Srgrimes			if (get_isoaddr(cpoptarg, grp)) {
13451558Srgrimes				syslog(LOG_ERR, "Bad iso addr: %s", cpoptarg);
13461558Srgrimes				return (1);
13471558Srgrimes			}
13481558Srgrimes			*has_hostp = 1;
13491558Srgrimes			usedarg++;
13501558Srgrimes			opt_flags |= OP_ISO;
13511558Srgrimes#endif /* ISO */
13521558Srgrimes		} else {
13531558Srgrimes			syslog(LOG_ERR, "Bad opt %s", cpopt);
13541558Srgrimes			return (1);
13551558Srgrimes		}
13561558Srgrimes		if (usedarg >= 0) {
13571558Srgrimes			*endcp = savedc2;
13581558Srgrimes			**endcpp = savedc;
13591558Srgrimes			if (usedarg > 0) {
13601558Srgrimes				*cpp = cp;
13611558Srgrimes				*endcpp = endcp;
13621558Srgrimes			}
13631558Srgrimes			return (0);
13641558Srgrimes		}
13651558Srgrimes		cpopt = cpoptend;
13661558Srgrimes	}
13671558Srgrimes	**endcpp = savedc;
13681558Srgrimes	return (0);
13691558Srgrimes}
13701558Srgrimes
13711558Srgrimes/*
13721558Srgrimes * Translate a character string to the corresponding list of network
13731558Srgrimes * addresses for a hostname.
13741558Srgrimes */
13751558Srgrimesint
13767401Swpaulget_host(cp, grp, tgrp)
13771558Srgrimes	char *cp;
13781558Srgrimes	struct grouplist *grp;
13797401Swpaul	struct grouplist *tgrp;
13801558Srgrimes{
13817401Swpaul	struct grouplist *checkgrp;
13821558Srgrimes	struct hostent *hp, *nhp;
13831558Srgrimes	char **addrp, **naddrp;
13841558Srgrimes	struct hostent t_host;
13851558Srgrimes	int i;
13861558Srgrimes	u_long saddr;
13871558Srgrimes	char *aptr[2];
13881558Srgrimes
13891558Srgrimes	if (grp->gr_type != GT_NULL)
13901558Srgrimes		return (1);
13911558Srgrimes	if ((hp = gethostbyname(cp)) == NULL) {
13921558Srgrimes		if (isdigit(*cp)) {
13931558Srgrimes			saddr = inet_addr(cp);
13941558Srgrimes			if (saddr == -1) {
13959336Sdfr 				syslog(LOG_ERR, "Inet_addr failed for %s", cp);
13961558Srgrimes				return (1);
13971558Srgrimes			}
13981558Srgrimes			if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
13991558Srgrimes				AF_INET)) == NULL) {
14001558Srgrimes				hp = &t_host;
14011558Srgrimes				hp->h_name = cp;
14021558Srgrimes				hp->h_addrtype = AF_INET;
14031558Srgrimes				hp->h_length = sizeof (u_long);
14041558Srgrimes				hp->h_addr_list = aptr;
14051558Srgrimes				aptr[0] = (char *)&saddr;
14061558Srgrimes				aptr[1] = (char *)NULL;
14071558Srgrimes			}
14081558Srgrimes		} else {
14099336Sdfr 			syslog(LOG_ERR, "Gethostbyname failed for %s", cp);
14101558Srgrimes			return (1);
14111558Srgrimes		}
14121558Srgrimes	}
14137401Swpaul        /*
14147401Swpaul         * Sanity check: make sure we don't already have an entry
14157401Swpaul         * for this host in the grouplist.
14167401Swpaul         */
14177401Swpaul        checkgrp = tgrp;
14187401Swpaul        while (checkgrp) {
141917887Swpaul		if (checkgrp->gr_type == GT_HOST &&
142017887Swpaul                    checkgrp->gr_ptr.gt_hostent != NULL &&
14217401Swpaul                    !strcmp(checkgrp->gr_ptr.gt_hostent->h_name, hp->h_name)) {
14227401Swpaul                        grp->gr_type = GT_IGNORE;
14237401Swpaul			return(0);
14247401Swpaul		}
14257401Swpaul                checkgrp = checkgrp->gr_next;
14267401Swpaul        }
14277401Swpaul
14281558Srgrimes	grp->gr_type = GT_HOST;
14291558Srgrimes	nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
14301558Srgrimes		malloc(sizeof(struct hostent));
14311558Srgrimes	if (nhp == (struct hostent *)NULL)
14321558Srgrimes		out_of_mem();
143323681Speter	memmove(nhp, hp, sizeof(struct hostent));
14341558Srgrimes	i = strlen(hp->h_name)+1;
14351558Srgrimes	nhp->h_name = (char *)malloc(i);
14361558Srgrimes	if (nhp->h_name == (char *)NULL)
14371558Srgrimes		out_of_mem();
143823681Speter	memmove(nhp->h_name, hp->h_name, i);
14391558Srgrimes	addrp = hp->h_addr_list;
14401558Srgrimes	i = 1;
14411558Srgrimes	while (*addrp++)
14421558Srgrimes		i++;
14431558Srgrimes	naddrp = nhp->h_addr_list = (char **)
14441558Srgrimes		malloc(i*sizeof(char *));
14451558Srgrimes	if (naddrp == (char **)NULL)
14461558Srgrimes		out_of_mem();
14471558Srgrimes	addrp = hp->h_addr_list;
14481558Srgrimes	while (*addrp) {
14491558Srgrimes		*naddrp = (char *)
14501558Srgrimes		    malloc(hp->h_length);
14511558Srgrimes		if (*naddrp == (char *)NULL)
14521558Srgrimes		    out_of_mem();
145323681Speter		memmove(*naddrp, *addrp, hp->h_length);
14541558Srgrimes		addrp++;
14551558Srgrimes		naddrp++;
14561558Srgrimes	}
14571558Srgrimes	*naddrp = (char *)NULL;
14581558Srgrimes	if (debug)
14591558Srgrimes		fprintf(stderr, "got host %s\n", hp->h_name);
14601558Srgrimes	return (0);
14611558Srgrimes}
14621558Srgrimes
14631558Srgrimes/*
14641558Srgrimes * Free up an exports list component
14651558Srgrimes */
14661558Srgrimesvoid
14671558Srgrimesfree_exp(ep)
14681558Srgrimes	struct exportlist *ep;
14691558Srgrimes{
14701558Srgrimes
14711558Srgrimes	if (ep->ex_defdir) {
14721558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
14731558Srgrimes		free((caddr_t)ep->ex_defdir);
14741558Srgrimes	}
14751558Srgrimes	if (ep->ex_fsdir)
14761558Srgrimes		free(ep->ex_fsdir);
14771558Srgrimes	free_dir(ep->ex_dirl);
14781558Srgrimes	free((caddr_t)ep);
14791558Srgrimes}
14801558Srgrimes
14811558Srgrimes/*
14821558Srgrimes * Free hosts.
14831558Srgrimes */
14841558Srgrimesvoid
14851558Srgrimesfree_host(hp)
14861558Srgrimes	struct hostlist *hp;
14871558Srgrimes{
14881558Srgrimes	struct hostlist *hp2;
14891558Srgrimes
14901558Srgrimes	while (hp) {
14911558Srgrimes		hp2 = hp;
14921558Srgrimes		hp = hp->ht_next;
14931558Srgrimes		free((caddr_t)hp2);
14941558Srgrimes	}
14951558Srgrimes}
14961558Srgrimes
14971558Srgrimesstruct hostlist *
14981558Srgrimesget_ht()
14991558Srgrimes{
15001558Srgrimes	struct hostlist *hp;
15011558Srgrimes
15021558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
15031558Srgrimes	if (hp == (struct hostlist *)NULL)
15041558Srgrimes		out_of_mem();
15051558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
15069336Sdfr	hp->ht_flag = 0;
15071558Srgrimes	return (hp);
15081558Srgrimes}
15091558Srgrimes
15101558Srgrimes#ifdef ISO
15111558Srgrimes/*
15121558Srgrimes * Translate an iso address.
15131558Srgrimes */
15141558Srgrimesget_isoaddr(cp, grp)
15151558Srgrimes	char *cp;
15161558Srgrimes	struct grouplist *grp;
15171558Srgrimes{
15181558Srgrimes	struct iso_addr *isop;
15191558Srgrimes	struct sockaddr_iso *isoaddr;
15201558Srgrimes
15211558Srgrimes	if (grp->gr_type != GT_NULL)
15221558Srgrimes		return (1);
15231558Srgrimes	if ((isop = iso_addr(cp)) == NULL) {
15241558Srgrimes		syslog(LOG_ERR,
15251558Srgrimes		    "iso_addr failed, ignored");
15261558Srgrimes		return (1);
15271558Srgrimes	}
15281558Srgrimes	isoaddr = (struct sockaddr_iso *)
15291558Srgrimes	    malloc(sizeof (struct sockaddr_iso));
15301558Srgrimes	if (isoaddr == (struct sockaddr_iso *)NULL)
15311558Srgrimes		out_of_mem();
153223681Speter	memset(isoaddr, 0, sizeof(struct sockaddr_iso));
153323681Speter	memmove(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
153423681Speter	isoaddr->siso_len = sizeof(struct sockaddr_iso);
15351558Srgrimes	isoaddr->siso_family = AF_ISO;
15361558Srgrimes	grp->gr_type = GT_ISO;
15371558Srgrimes	grp->gr_ptr.gt_isoaddr = isoaddr;
15381558Srgrimes	return (0);
15391558Srgrimes}
15401558Srgrimes#endif	/* ISO */
15411558Srgrimes
15421558Srgrimes/*
15431558Srgrimes * Out of memory, fatal
15441558Srgrimes */
15451558Srgrimesvoid
15461558Srgrimesout_of_mem()
15471558Srgrimes{
15481558Srgrimes
15491558Srgrimes	syslog(LOG_ERR, "Out of memory");
15501558Srgrimes	exit(2);
15511558Srgrimes}
15521558Srgrimes
15531558Srgrimes/*
15541558Srgrimes * Do the mount syscall with the update flag to push the export info into
15551558Srgrimes * the kernel.
15561558Srgrimes */
15571558Srgrimesint
15581558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
15591558Srgrimes	struct exportlist *ep;
15601558Srgrimes	struct grouplist *grp;
15611558Srgrimes	int exflags;
15621558Srgrimes	struct ucred *anoncrp;
15631558Srgrimes	char *dirp;
15641558Srgrimes	int dirplen;
15651558Srgrimes	struct statfs *fsb;
15661558Srgrimes{
15671558Srgrimes	char *cp = (char *)NULL;
15681558Srgrimes	u_long **addrp;
15691558Srgrimes	int done;
15701558Srgrimes	char savedc = '\0';
15711558Srgrimes	struct sockaddr_in sin, imask;
15721558Srgrimes	union {
15731558Srgrimes		struct ufs_args ua;
15741558Srgrimes		struct iso_args ia;
15751558Srgrimes		struct mfs_args ma;
15769336Sdfr#ifdef __NetBSD__
15779336Sdfr		struct msdosfs_args da;
15789336Sdfr#endif
15791558Srgrimes	} args;
15801558Srgrimes	u_long net;
15811558Srgrimes
15821558Srgrimes	args.ua.fspec = 0;
15831558Srgrimes	args.ua.export.ex_flags = exflags;
15841558Srgrimes	args.ua.export.ex_anon = *anoncrp;
158523681Speter	memset(&sin, 0, sizeof(sin));
158623681Speter	memset(&imask, 0, sizeof(imask));
15871558Srgrimes	sin.sin_family = AF_INET;
15881558Srgrimes	sin.sin_len = sizeof(sin);
15891558Srgrimes	imask.sin_family = AF_INET;
15901558Srgrimes	imask.sin_len = sizeof(sin);
15911558Srgrimes	if (grp->gr_type == GT_HOST)
15921558Srgrimes		addrp = (u_long **)grp->gr_ptr.gt_hostent->h_addr_list;
15931558Srgrimes	else
15941558Srgrimes		addrp = (u_long **)NULL;
15951558Srgrimes	done = FALSE;
15961558Srgrimes	while (!done) {
15971558Srgrimes		switch (grp->gr_type) {
15981558Srgrimes		case GT_HOST:
15991558Srgrimes			if (addrp) {
16001558Srgrimes				sin.sin_addr.s_addr = **addrp;
16011558Srgrimes				args.ua.export.ex_addrlen = sizeof(sin);
16021558Srgrimes			} else
16031558Srgrimes				args.ua.export.ex_addrlen = 0;
16041558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16051558Srgrimes			args.ua.export.ex_masklen = 0;
16061558Srgrimes			break;
16071558Srgrimes		case GT_NET:
16081558Srgrimes			if (grp->gr_ptr.gt_net.nt_mask)
16091558Srgrimes			    imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
16101558Srgrimes			else {
16111558Srgrimes			    net = ntohl(grp->gr_ptr.gt_net.nt_net);
16121558Srgrimes			    if (IN_CLASSA(net))
16131558Srgrimes				imask.sin_addr.s_addr = inet_addr("255.0.0.0");
16141558Srgrimes			    else if (IN_CLASSB(net))
16151558Srgrimes				imask.sin_addr.s_addr =
16161558Srgrimes				    inet_addr("255.255.0.0");
16171558Srgrimes			    else
16181558Srgrimes				imask.sin_addr.s_addr =
16191558Srgrimes				    inet_addr("255.255.255.0");
16201558Srgrimes			    grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
16211558Srgrimes			}
16221558Srgrimes			sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
16231558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16241558Srgrimes			args.ua.export.ex_addrlen = sizeof (sin);
16251558Srgrimes			args.ua.export.ex_mask = (struct sockaddr *)&imask;
16261558Srgrimes			args.ua.export.ex_masklen = sizeof (imask);
16271558Srgrimes			break;
16281558Srgrimes#ifdef ISO
16291558Srgrimes		case GT_ISO:
16301558Srgrimes			args.ua.export.ex_addr =
16311558Srgrimes				(struct sockaddr *)grp->gr_ptr.gt_isoaddr;
16321558Srgrimes			args.ua.export.ex_addrlen =
16331558Srgrimes				sizeof(struct sockaddr_iso);
16341558Srgrimes			args.ua.export.ex_masklen = 0;
16351558Srgrimes			break;
16361558Srgrimes#endif	/* ISO */
16377401Swpaul		case GT_IGNORE:
16387401Swpaul			return(0);
16397401Swpaul			break;
16401558Srgrimes		default:
16411558Srgrimes			syslog(LOG_ERR, "Bad grouptype");
16421558Srgrimes			if (cp)
16431558Srgrimes				*cp = savedc;
16441558Srgrimes			return (1);
16451558Srgrimes		};
16461558Srgrimes
16471558Srgrimes		/*
16481558Srgrimes		 * XXX:
16491558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
16501558Srgrimes		 * of looping back up the dirp to the mount point??
16511558Srgrimes		 * Also, needs to know how to export all types of local
165223681Speter		 * exportable file systems and not just "ufs".
16531558Srgrimes		 */
16549336Sdfr		while (mount(fsb->f_fstypename, dirp,
16551558Srgrimes		       fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
16561558Srgrimes			if (cp)
16571558Srgrimes				*cp-- = savedc;
16581558Srgrimes			else
16591558Srgrimes				cp = dirp + dirplen - 1;
16601558Srgrimes			if (errno == EPERM) {
16611558Srgrimes				syslog(LOG_ERR,
16621558Srgrimes				   "Can't change attributes for %s.\n", dirp);
16631558Srgrimes				return (1);
16641558Srgrimes			}
16651558Srgrimes			if (opt_flags & OP_ALLDIRS) {
16664895Swollman				syslog(LOG_ERR, "Could not remount %s: %m",
16674895Swollman					dirp);
16681558Srgrimes				return (1);
16691558Srgrimes			}
16701558Srgrimes			/* back up over the last component */
16711558Srgrimes			while (*cp == '/' && cp > dirp)
16721558Srgrimes				cp--;
16731558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
16741558Srgrimes				cp--;
16751558Srgrimes			if (cp == dirp) {
16761558Srgrimes				if (debug)
16771558Srgrimes					fprintf(stderr,"mnt unsucc\n");
16781558Srgrimes				syslog(LOG_ERR, "Can't export %s", dirp);
16791558Srgrimes				return (1);
16801558Srgrimes			}
16811558Srgrimes			savedc = *cp;
16821558Srgrimes			*cp = '\0';
16831558Srgrimes		}
16841558Srgrimes		if (addrp) {
16851558Srgrimes			++addrp;
16861558Srgrimes			if (*addrp == (u_long *)NULL)
16871558Srgrimes				done = TRUE;
16881558Srgrimes		} else
16891558Srgrimes			done = TRUE;
16901558Srgrimes	}
16911558Srgrimes	if (cp)
16921558Srgrimes		*cp = savedc;
16931558Srgrimes	return (0);
16941558Srgrimes}
16951558Srgrimes
16961558Srgrimes/*
16971558Srgrimes * Translate a net address.
16981558Srgrimes */
16991558Srgrimesint
17001558Srgrimesget_net(cp, net, maskflg)
17011558Srgrimes	char *cp;
17021558Srgrimes	struct netmsk *net;
17031558Srgrimes	int maskflg;
17041558Srgrimes{
17051558Srgrimes	struct netent *np;
17061558Srgrimes	long netaddr;
17071558Srgrimes	struct in_addr inetaddr, inetaddr2;
17081558Srgrimes	char *name;
17091558Srgrimes
17101558Srgrimes	if (np = getnetbyname(cp))
17111558Srgrimes		inetaddr = inet_makeaddr(np->n_net, 0);
17121558Srgrimes	else if (isdigit(*cp)) {
17131558Srgrimes		if ((netaddr = inet_network(cp)) == -1)
17141558Srgrimes			return (1);
17151558Srgrimes		inetaddr = inet_makeaddr(netaddr, 0);
17161558Srgrimes		/*
17171558Srgrimes		 * Due to arbritrary subnet masks, you don't know how many
17181558Srgrimes		 * bits to shift the address to make it into a network,
17191558Srgrimes		 * however you do know how to make a network address into
17201558Srgrimes		 * a host with host == 0 and then compare them.
17211558Srgrimes		 * (What a pest)
17221558Srgrimes		 */
17231558Srgrimes		if (!maskflg) {
17241558Srgrimes			setnetent(0);
17251558Srgrimes			while (np = getnetent()) {
17261558Srgrimes				inetaddr2 = inet_makeaddr(np->n_net, 0);
17271558Srgrimes				if (inetaddr2.s_addr == inetaddr.s_addr)
17281558Srgrimes					break;
17291558Srgrimes			}
17301558Srgrimes			endnetent();
17311558Srgrimes		}
17321558Srgrimes	} else
17331558Srgrimes		return (1);
17341558Srgrimes	if (maskflg)
17351558Srgrimes		net->nt_mask = inetaddr.s_addr;
17361558Srgrimes	else {
17371558Srgrimes		if (np)
17381558Srgrimes			name = np->n_name;
17391558Srgrimes		else
17401558Srgrimes			name = inet_ntoa(inetaddr);
17411558Srgrimes		net->nt_name = (char *)malloc(strlen(name) + 1);
17421558Srgrimes		if (net->nt_name == (char *)NULL)
17431558Srgrimes			out_of_mem();
17441558Srgrimes		strcpy(net->nt_name, name);
17451558Srgrimes		net->nt_net = inetaddr.s_addr;
17461558Srgrimes	}
17471558Srgrimes	return (0);
17481558Srgrimes}
17491558Srgrimes
17501558Srgrimes/*
17511558Srgrimes * Parse out the next white space separated field
17521558Srgrimes */
17531558Srgrimesvoid
17541558Srgrimesnextfield(cp, endcp)
17551558Srgrimes	char **cp;
17561558Srgrimes	char **endcp;
17571558Srgrimes{
17581558Srgrimes	char *p;
17591558Srgrimes
17601558Srgrimes	p = *cp;
17611558Srgrimes	while (*p == ' ' || *p == '\t')
17621558Srgrimes		p++;
17631558Srgrimes	if (*p == '\n' || *p == '\0')
17641558Srgrimes		*cp = *endcp = p;
17651558Srgrimes	else {
17661558Srgrimes		*cp = p++;
17671558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
17681558Srgrimes			p++;
17691558Srgrimes		*endcp = p;
17701558Srgrimes	}
17711558Srgrimes}
17721558Srgrimes
17731558Srgrimes/*
17741558Srgrimes * Get an exports file line. Skip over blank lines and handle line
17751558Srgrimes * continuations.
17761558Srgrimes */
17771558Srgrimesint
17781558Srgrimesget_line()
17791558Srgrimes{
17801558Srgrimes	char *p, *cp;
17811558Srgrimes	int len;
17821558Srgrimes	int totlen, cont_line;
17831558Srgrimes
17841558Srgrimes	/*
17851558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
17861558Srgrimes	 */
17871558Srgrimes	p = line;
17881558Srgrimes	totlen = 0;
17891558Srgrimes	do {
17901558Srgrimes		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
17911558Srgrimes			return (0);
17921558Srgrimes		len = strlen(p);
17931558Srgrimes		cp = p + len - 1;
17941558Srgrimes		cont_line = 0;
17951558Srgrimes		while (cp >= p &&
17961558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
17971558Srgrimes			if (*cp == '\\')
17981558Srgrimes				cont_line = 1;
17991558Srgrimes			cp--;
18001558Srgrimes			len--;
18011558Srgrimes		}
18021558Srgrimes		*++cp = '\0';
18031558Srgrimes		if (len > 0) {
18041558Srgrimes			totlen += len;
18051558Srgrimes			if (totlen >= LINESIZ) {
18061558Srgrimes				syslog(LOG_ERR, "Exports line too long");
18071558Srgrimes				exit(2);
18081558Srgrimes			}
18091558Srgrimes			p = cp;
18101558Srgrimes		}
18111558Srgrimes	} while (totlen == 0 || cont_line);
18121558Srgrimes	return (1);
18131558Srgrimes}
18141558Srgrimes
18151558Srgrimes/*
18161558Srgrimes * Parse a description of a credential.
18171558Srgrimes */
18181558Srgrimesvoid
18191558Srgrimesparsecred(namelist, cr)
18201558Srgrimes	char *namelist;
18211558Srgrimes	struct ucred *cr;
18221558Srgrimes{
18231558Srgrimes	char *name;
18241558Srgrimes	int cnt;
18251558Srgrimes	char *names;
18261558Srgrimes	struct passwd *pw;
18271558Srgrimes	struct group *gr;
18281558Srgrimes	int ngroups, groups[NGROUPS + 1];
18291558Srgrimes
18301558Srgrimes	/*
18311558Srgrimes	 * Set up the unpriviledged user.
18321558Srgrimes	 */
18331558Srgrimes	cr->cr_ref = 1;
18341558Srgrimes	cr->cr_uid = -2;
18351558Srgrimes	cr->cr_groups[0] = -2;
18361558Srgrimes	cr->cr_ngroups = 1;
18371558Srgrimes	/*
18381558Srgrimes	 * Get the user's password table entry.
18391558Srgrimes	 */
18401558Srgrimes	names = strsep(&namelist, " \t\n");
18411558Srgrimes	name = strsep(&names, ":");
18421558Srgrimes	if (isdigit(*name) || *name == '-')
18431558Srgrimes		pw = getpwuid(atoi(name));
18441558Srgrimes	else
18451558Srgrimes		pw = getpwnam(name);
18461558Srgrimes	/*
18471558Srgrimes	 * Credentials specified as those of a user.
18481558Srgrimes	 */
18491558Srgrimes	if (names == NULL) {
18501558Srgrimes		if (pw == NULL) {
18511558Srgrimes			syslog(LOG_ERR, "Unknown user: %s", name);
18521558Srgrimes			return;
18531558Srgrimes		}
18541558Srgrimes		cr->cr_uid = pw->pw_uid;
18551558Srgrimes		ngroups = NGROUPS + 1;
18561558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
18571558Srgrimes			syslog(LOG_ERR, "Too many groups");
18581558Srgrimes		/*
18591558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
18601558Srgrimes		 */
18611558Srgrimes		cr->cr_ngroups = ngroups - 1;
18621558Srgrimes		cr->cr_groups[0] = groups[0];
18631558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
18641558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
18651558Srgrimes		return;
18661558Srgrimes	}
18671558Srgrimes	/*
18681558Srgrimes	 * Explicit credential specified as a colon separated list:
18691558Srgrimes	 *	uid:gid:gid:...
18701558Srgrimes	 */
18711558Srgrimes	if (pw != NULL)
18721558Srgrimes		cr->cr_uid = pw->pw_uid;
18731558Srgrimes	else if (isdigit(*name) || *name == '-')
18741558Srgrimes		cr->cr_uid = atoi(name);
18751558Srgrimes	else {
18761558Srgrimes		syslog(LOG_ERR, "Unknown user: %s", name);
18771558Srgrimes		return;
18781558Srgrimes	}
18791558Srgrimes	cr->cr_ngroups = 0;
18801558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
18811558Srgrimes		name = strsep(&names, ":");
18821558Srgrimes		if (isdigit(*name) || *name == '-') {
18831558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
18841558Srgrimes		} else {
18851558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
18861558Srgrimes				syslog(LOG_ERR, "Unknown group: %s", name);
18871558Srgrimes				continue;
18881558Srgrimes			}
18891558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
18901558Srgrimes		}
18911558Srgrimes	}
18921558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
18931558Srgrimes		syslog(LOG_ERR, "Too many groups");
18941558Srgrimes}
18951558Srgrimes
18961558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
18971558Srgrimes/*
18981558Srgrimes * Routines that maintain the remote mounttab
18991558Srgrimes */
19001558Srgrimesvoid
19011558Srgrimesget_mountlist()
19021558Srgrimes{
19031558Srgrimes	struct mountlist *mlp, **mlpp;
190423681Speter	char *host, *dirp, *cp;
19051558Srgrimes	int len;
19061558Srgrimes	char str[STRSIZ];
19071558Srgrimes	FILE *mlfile;
19081558Srgrimes
19091558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
19101558Srgrimes		syslog(LOG_ERR, "Can't open %s", _PATH_RMOUNTLIST);
19111558Srgrimes		return;
19121558Srgrimes	}
19131558Srgrimes	mlpp = &mlhead;
19141558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
191523681Speter		cp = str;
191623681Speter		host = strsep(&cp, " \t\n");
191723681Speter		dirp = strsep(&cp, " \t\n");
191823681Speter		if (host == NULL || dirp == NULL)
19191558Srgrimes			continue;
19201558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
192123681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
192223681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
192323681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
192423681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
19251558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
19261558Srgrimes		*mlpp = mlp;
19271558Srgrimes		mlpp = &mlp->ml_next;
19281558Srgrimes	}
19291558Srgrimes	fclose(mlfile);
19301558Srgrimes}
19311558Srgrimes
19321558Srgrimesvoid
19331558Srgrimesdel_mlist(hostp, dirp)
19341558Srgrimes	char *hostp, *dirp;
19351558Srgrimes{
19361558Srgrimes	struct mountlist *mlp, **mlpp;
19371558Srgrimes	struct mountlist *mlp2;
19381558Srgrimes	FILE *mlfile;
19391558Srgrimes	int fnd = 0;
19401558Srgrimes
19411558Srgrimes	mlpp = &mlhead;
19421558Srgrimes	mlp = mlhead;
19431558Srgrimes	while (mlp) {
19441558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
19451558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
19461558Srgrimes			fnd = 1;
19471558Srgrimes			mlp2 = mlp;
19481558Srgrimes			*mlpp = mlp = mlp->ml_next;
19491558Srgrimes			free((caddr_t)mlp2);
19501558Srgrimes		} else {
19511558Srgrimes			mlpp = &mlp->ml_next;
19521558Srgrimes			mlp = mlp->ml_next;
19531558Srgrimes		}
19541558Srgrimes	}
19551558Srgrimes	if (fnd) {
19561558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
19571558Srgrimes			syslog(LOG_ERR,"Can't update %s", _PATH_RMOUNTLIST);
19581558Srgrimes			return;
19591558Srgrimes		}
19601558Srgrimes		mlp = mlhead;
19611558Srgrimes		while (mlp) {
19621558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
19631558Srgrimes			mlp = mlp->ml_next;
19641558Srgrimes		}
19651558Srgrimes		fclose(mlfile);
19661558Srgrimes	}
19671558Srgrimes}
19681558Srgrimes
19691558Srgrimesvoid
19701558Srgrimesadd_mlist(hostp, dirp)
19711558Srgrimes	char *hostp, *dirp;
19721558Srgrimes{
19731558Srgrimes	struct mountlist *mlp, **mlpp;
19741558Srgrimes	FILE *mlfile;
19751558Srgrimes
19761558Srgrimes	mlpp = &mlhead;
19771558Srgrimes	mlp = mlhead;
19781558Srgrimes	while (mlp) {
19791558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
19801558Srgrimes			return;
19811558Srgrimes		mlpp = &mlp->ml_next;
19821558Srgrimes		mlp = mlp->ml_next;
19831558Srgrimes	}
19841558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
19851558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
19861558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
19871558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
19881558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
19891558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
19901558Srgrimes	*mlpp = mlp;
19911558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
19921558Srgrimes		syslog(LOG_ERR, "Can't update %s", _PATH_RMOUNTLIST);
19931558Srgrimes		return;
19941558Srgrimes	}
19951558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
19961558Srgrimes	fclose(mlfile);
19971558Srgrimes}
19981558Srgrimes
19991558Srgrimes/*
20001558Srgrimes * This function is called via. SIGTERM when the system is going down.
20011558Srgrimes * It sends a broadcast RPCMNT_UMNTALL.
20021558Srgrimes */
20031558Srgrimesvoid
20041558Srgrimessend_umntall()
20051558Srgrimes{
20061558Srgrimes	(void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
20071558Srgrimes		xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
20081558Srgrimes	exit(0);
20091558Srgrimes}
20101558Srgrimes
20111558Srgrimesint
20121558Srgrimesumntall_each(resultsp, raddr)
20131558Srgrimes	caddr_t resultsp;
20141558Srgrimes	struct sockaddr_in *raddr;
20151558Srgrimes{
20161558Srgrimes	return (1);
20171558Srgrimes}
20181558Srgrimes
20191558Srgrimes/*
20201558Srgrimes * Free up a group list.
20211558Srgrimes */
20221558Srgrimesvoid
20231558Srgrimesfree_grp(grp)
20241558Srgrimes	struct grouplist *grp;
20251558Srgrimes{
20261558Srgrimes	char **addrp;
20271558Srgrimes
20281558Srgrimes	if (grp->gr_type == GT_HOST) {
20291558Srgrimes		if (grp->gr_ptr.gt_hostent->h_name) {
20301558Srgrimes			addrp = grp->gr_ptr.gt_hostent->h_addr_list;
20311558Srgrimes			while (addrp && *addrp)
20321558Srgrimes				free(*addrp++);
20331558Srgrimes			free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
20341558Srgrimes			free(grp->gr_ptr.gt_hostent->h_name);
20351558Srgrimes		}
20361558Srgrimes		free((caddr_t)grp->gr_ptr.gt_hostent);
20371558Srgrimes	} else if (grp->gr_type == GT_NET) {
20381558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
20391558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
20401558Srgrimes	}
20411558Srgrimes#ifdef ISO
20421558Srgrimes	else if (grp->gr_type == GT_ISO)
20431558Srgrimes		free((caddr_t)grp->gr_ptr.gt_isoaddr);
20441558Srgrimes#endif
20451558Srgrimes	free((caddr_t)grp);
20461558Srgrimes}
20471558Srgrimes
20481558Srgrimes#ifdef DEBUG
20491558Srgrimesvoid
20501558SrgrimesSYSLOG(int pri, const char *fmt, ...)
20511558Srgrimes{
20521558Srgrimes	va_list ap;
20531558Srgrimes
20541558Srgrimes	va_start(ap, fmt);
20551558Srgrimes	vfprintf(stderr, fmt, ap);
20561558Srgrimes	va_end(ap);
20571558Srgrimes}
20581558Srgrimes#endif /* DEBUG */
20591558Srgrimes
20601558Srgrimes/*
20611558Srgrimes * Check options for consistency.
20621558Srgrimes */
20631558Srgrimesint
20641558Srgrimescheck_options(dp)
20651558Srgrimes	struct dirlist *dp;
20661558Srgrimes{
20671558Srgrimes
20681558Srgrimes	if (dp == (struct dirlist *)NULL)
20691558Srgrimes	    return (1);
20701558Srgrimes	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
20711558Srgrimes	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
20721558Srgrimes	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
20731558Srgrimes	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
20741558Srgrimes	    return (1);
20751558Srgrimes	}
20761558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
20771558Srgrimes	    syslog(LOG_ERR, "-mask requires -net");
20781558Srgrimes	    return (1);
20791558Srgrimes	}
20801558Srgrimes	if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
20811558Srgrimes	    syslog(LOG_ERR, "-net and -iso mutually exclusive");
20821558Srgrimes	    return (1);
20831558Srgrimes	}
20841558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
20851558Srgrimes	    syslog(LOG_ERR, "-alldir has multiple directories");
20861558Srgrimes	    return (1);
20871558Srgrimes	}
20881558Srgrimes	return (0);
20891558Srgrimes}
20901558Srgrimes
20911558Srgrimes/*
20921558Srgrimes * Check an absolute directory path for any symbolic links. Return true
20931558Srgrimes * if no symbolic links are found.
20941558Srgrimes */
20951558Srgrimesint
20961558Srgrimescheck_dirpath(dirp)
20971558Srgrimes	char *dirp;
20981558Srgrimes{
20991558Srgrimes	char *cp;
21001558Srgrimes	int ret = 1;
21011558Srgrimes	struct stat sb;
21021558Srgrimes
21031558Srgrimes	cp = dirp + 1;
21041558Srgrimes	while (*cp && ret) {
21051558Srgrimes		if (*cp == '/') {
21061558Srgrimes			*cp = '\0';
21079336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21081558Srgrimes				ret = 0;
21091558Srgrimes			*cp = '/';
21101558Srgrimes		}
21111558Srgrimes		cp++;
21121558Srgrimes	}
21139336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21141558Srgrimes		ret = 0;
21151558Srgrimes	return (ret);
21161558Srgrimes}
21179336Sdfr
21189336Sdfr/*
21199336Sdfr * Just translate an ascii string to an integer.
21209336Sdfr */
21219336Sdfrint
21229336Sdfrget_num(cp)
21239336Sdfr	register char *cp;
21249336Sdfr{
21259336Sdfr	register int res = 0;
21269336Sdfr
21279336Sdfr	while (*cp) {
21289336Sdfr		if (*cp < '0' || *cp > '9')
21299336Sdfr			return (-1);
21309336Sdfr		res = res * 10 + (*cp++ - '0');
21319336Sdfr	}
21329336Sdfr	return (res);
21339336Sdfr}
2134