mountd.c revision 28911
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[] =
4628911Sguido	"$Id: mountd.c,v 1.22 1997/07/16 09:27:53 dfr 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;
2271558Srgrimesint opt_flags;
2281558Srgrimes/* Bits for above */
2291558Srgrimes#define	OP_MAPROOT	0x01
2301558Srgrimes#define	OP_MAPALL	0x02
2311558Srgrimes#define	OP_KERB		0x04
2321558Srgrimes#define	OP_MASK		0x08
2331558Srgrimes#define	OP_NET		0x10
2341558Srgrimes#define	OP_ISO		0x20
2351558Srgrimes#define	OP_ALLDIRS	0x40
2361558Srgrimes
2371558Srgrimes#ifdef DEBUG
2381558Srgrimesint debug = 1;
2391558Srgrimesvoid	SYSLOG __P((int, const char *, ...));
2401558Srgrimes#define syslog SYSLOG
2411558Srgrimes#else
2421558Srgrimesint debug = 0;
2431558Srgrimes#endif
2441558Srgrimes
2451558Srgrimes/*
2461558Srgrimes * Mountd server for NFS mount protocol as described in:
2471558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2481558Srgrimes * The optional arguments are the exports file name
2491558Srgrimes * default: _PATH_EXPORTS
2501558Srgrimes * and "-n" to allow nonroot mount.
2511558Srgrimes */
2521558Srgrimesint
2531558Srgrimesmain(argc, argv)
2541558Srgrimes	int argc;
2551558Srgrimes	char **argv;
2561558Srgrimes{
2579202Srgrimes	SVCXPRT *udptransp, *tcptransp;
2581558Srgrimes	int c;
25924489Sbde	int mib[3];
2609336Sdfr#ifdef __FreeBSD__
26123681Speter	struct vfsconf vfc;
26223681Speter	int error;
2631558Srgrimes
26423681Speter	error = getvfsbyname("nfs", &vfc);
26523681Speter	if (error && vfsisloadable("nfs")) {
2662999Swollman		if(vfsload("nfs"))
2672999Swollman			err(1, "vfsload(nfs)");
2682999Swollman		endvfsent();	/* flush cache */
26923681Speter		error = getvfsbyname("nfs", &vfc);
2702999Swollman	}
27123681Speter	if (error)
2722999Swollman		errx(1, "NFS support is not available in the running kernel");
2739336Sdfr#endif	/* __FreeBSD__ */
2742999Swollman
27525087Sdfr	while ((c = getopt(argc, argv, "2dnr")) != -1)
2761558Srgrimes		switch (c) {
27725087Sdfr		case '2':
27825087Sdfr			force_v2 = 1;
27925087Sdfr			break;
2809336Sdfr		case 'n':
2819336Sdfr			resvport_only = 0;
2829336Sdfr			break;
2839336Sdfr		case 'r':
2849336Sdfr			dir_only = 0;
2859336Sdfr			break;
2868688Sphk		case 'd':
2878688Sphk			debug = debug ? 0 : 1;
2888688Sphk			break;
2891558Srgrimes		default:
29023681Speter			fprintf(stderr, "Usage: mountd [-d] [-r] [-n] [export_file]\n");
2911558Srgrimes			exit(1);
2921558Srgrimes		};
2931558Srgrimes	argc -= optind;
2941558Srgrimes	argv += optind;
2951558Srgrimes	grphead = (struct grouplist *)NULL;
2961558Srgrimes	exphead = (struct exportlist *)NULL;
2971558Srgrimes	mlhead = (struct mountlist *)NULL;
2981558Srgrimes	if (argc == 1) {
2991558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
3001558Srgrimes		exname[MAXPATHLEN-1] = '\0';
3011558Srgrimes	} else
3021558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3031558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3041558Srgrimes	if (debug)
3051558Srgrimes		fprintf(stderr,"Getting export list.\n");
3061558Srgrimes	get_exportlist();
3071558Srgrimes	if (debug)
3081558Srgrimes		fprintf(stderr,"Getting mount list.\n");
3091558Srgrimes	get_mountlist();
3101558Srgrimes	if (debug)
3111558Srgrimes		fprintf(stderr,"Here we go.\n");
3121558Srgrimes	if (debug == 0) {
3131558Srgrimes		daemon(0, 0);
3141558Srgrimes		signal(SIGINT, SIG_IGN);
3151558Srgrimes		signal(SIGQUIT, SIG_IGN);
3161558Srgrimes	}
3171558Srgrimes	signal(SIGHUP, (void (*) __P((int))) get_exportlist);
3181558Srgrimes	signal(SIGTERM, (void (*) __P((int))) send_umntall);
3191558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3201558Srgrimes	  if (pidfile != NULL) {
3211558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3221558Srgrimes		fclose(pidfile);
3231558Srgrimes	  }
3241558Srgrimes	}
32524330Sguido
32624759Sguido	if (!resvport_only) {
32724759Sguido		mib[0] = CTL_VFS;
32824759Sguido		mib[1] = MOUNT_NFS;
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	}
33624330Sguido
3379202Srgrimes	if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
3389202Srgrimes	    (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
3391558Srgrimes		syslog(LOG_ERR, "Can't create socket");
3401558Srgrimes		exit(1);
3411558Srgrimes	}
3429336Sdfr	pmap_unset(RPCPROG_MNT, 1);
3439336Sdfr	pmap_unset(RPCPROG_MNT, 3);
34425087Sdfr	if (!force_v2)
34525087Sdfr		if (!svc_register(udptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_UDP) ||
34625087Sdfr		    !svc_register(tcptransp, RPCPROG_MNT, 3, mntsrv, IPPROTO_TCP)) {
34725087Sdfr			syslog(LOG_ERR, "Can't register mount");
34825087Sdfr			exit(1);
34925087Sdfr		}
3509336Sdfr	if (!svc_register(udptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_UDP) ||
35125087Sdfr	    !svc_register(tcptransp, RPCPROG_MNT, 1, mntsrv, IPPROTO_TCP)) {
3521558Srgrimes		syslog(LOG_ERR, "Can't register mount");
3531558Srgrimes		exit(1);
3541558Srgrimes	}
3551558Srgrimes	svc_run();
3561558Srgrimes	syslog(LOG_ERR, "Mountd died");
3571558Srgrimes	exit(1);
3581558Srgrimes}
3591558Srgrimes
3601558Srgrimes/*
3611558Srgrimes * The mount rpc service
3621558Srgrimes */
3631558Srgrimesvoid
3641558Srgrimesmntsrv(rqstp, transp)
3651558Srgrimes	struct svc_req *rqstp;
3661558Srgrimes	SVCXPRT *transp;
3671558Srgrimes{
3681558Srgrimes	struct exportlist *ep;
3691558Srgrimes	struct dirlist *dp;
3709336Sdfr	struct fhreturn fhr;
3711558Srgrimes	struct stat stb;
3721558Srgrimes	struct statfs fsb;
3731558Srgrimes	struct hostent *hp;
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;
3839336Sdfr	sport = ntohs(transp->xp_raddr.sin_port);
3841558Srgrimes	hp = (struct hostent *)NULL;
3851558Srgrimes	switch (rqstp->rq_proc) {
3861558Srgrimes	case NULLPROC:
3871558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
3881558Srgrimes			syslog(LOG_ERR, "Can't send reply");
3891558Srgrimes		return;
3901558Srgrimes	case RPCMNT_MOUNT:
3919336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
3921558Srgrimes			svcerr_weakauth(transp);
3931558Srgrimes			return;
3941558Srgrimes		}
3951558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
3961558Srgrimes			svcerr_decode(transp);
3971558Srgrimes			return;
3981558Srgrimes		}
3991558Srgrimes
4001558Srgrimes		/*
4011558Srgrimes		 * Get the real pathname and make sure it is a directory
4029336Sdfr		 * or a regular file if the -r option was specified
4039336Sdfr		 * and it exists.
4041558Srgrimes		 */
4051558Srgrimes		if (realpath(rpcpath, dirpath) == 0 ||
4061558Srgrimes		    stat(dirpath, &stb) < 0 ||
4079336Sdfr		    (!S_ISDIR(stb.st_mode) &&
4089336Sdfr		     (dir_only || !S_ISREG(stb.st_mode))) ||
4091558Srgrimes		    statfs(dirpath, &fsb) < 0) {
4101558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
4111558Srgrimes			if (debug)
4121558Srgrimes				fprintf(stderr, "stat failed on %s\n", dirpath);
41328911Sguido			bad = ENOENT;	/* We will send error reply later */
4141558Srgrimes		}
4151558Srgrimes
4161558Srgrimes		/* Check in the exports list */
4179336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
4181558Srgrimes		ep = ex_search(&fsb.f_fsid);
4199336Sdfr		hostset = defset = 0;
4209336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
4211558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
4229336Sdfr		     chk_host(dp, saddr, &defset, &hostset)) ||
4231558Srgrimes		     (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
4241558Srgrimes		      scan_tree(ep->ex_dirl, saddr) == 0))) {
42528911Sguido			if (bad) {
42628911Sguido				if (!svc_sendreply(transp, xdr_long,
42728911Sguido				    (caddr_t)&bad))
42828911Sguido					syslog(LOG_ERR, "Can't send reply");
42928911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
43028911Sguido				return;
43128911Sguido			}
4329336Sdfr			if (hostset & DP_HOSTSET)
4339336Sdfr				fhr.fhr_flag = hostset;
4349336Sdfr			else
4359336Sdfr				fhr.fhr_flag = defset;
4369336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
4371558Srgrimes			/* Get the file handle */
43823681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
4399336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
4401558Srgrimes				bad = errno;
4411558Srgrimes				syslog(LOG_ERR, "Can't get fh for %s", dirpath);
4421558Srgrimes				if (!svc_sendreply(transp, xdr_long,
4431558Srgrimes				    (caddr_t)&bad))
4441558Srgrimes					syslog(LOG_ERR, "Can't send reply");
4459336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
4461558Srgrimes				return;
4471558Srgrimes			}
4489336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
4491558Srgrimes				syslog(LOG_ERR, "Can't send reply");
4501558Srgrimes			if (hp == NULL)
4511558Srgrimes				hp = gethostbyaddr((caddr_t)&saddr,
4521558Srgrimes				    sizeof(saddr), AF_INET);
4531558Srgrimes			if (hp)
4541558Srgrimes				add_mlist(hp->h_name, dirpath);
4551558Srgrimes			else
4561558Srgrimes				add_mlist(inet_ntoa(transp->xp_raddr.sin_addr),
4571558Srgrimes					dirpath);
4581558Srgrimes			if (debug)
4591558Srgrimes				fprintf(stderr,"Mount successfull.\n");
46028911Sguido		} else
4611558Srgrimes			bad = EACCES;
46228911Sguido
46328911Sguido		if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
46428911Sguido			syslog(LOG_ERR, "Can't send reply");
4659336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
4661558Srgrimes		return;
4671558Srgrimes	case RPCMNT_DUMP:
4681558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
4691558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4701558Srgrimes		return;
4711558Srgrimes	case RPCMNT_UMOUNT:
4729336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
4731558Srgrimes			svcerr_weakauth(transp);
4741558Srgrimes			return;
4751558Srgrimes		}
4761558Srgrimes		if (!svc_getargs(transp, xdr_dir, dirpath)) {
4771558Srgrimes			svcerr_decode(transp);
4781558Srgrimes			return;
4791558Srgrimes		}
4801558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
4811558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4821558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
4831558Srgrimes		if (hp)
4841558Srgrimes			del_mlist(hp->h_name, dirpath);
4851558Srgrimes		del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), dirpath);
4861558Srgrimes		return;
4871558Srgrimes	case RPCMNT_UMNTALL:
4889336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
4891558Srgrimes			svcerr_weakauth(transp);
4901558Srgrimes			return;
4911558Srgrimes		}
4921558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
4931558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4941558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
4951558Srgrimes		if (hp)
4961558Srgrimes			del_mlist(hp->h_name, (char *)NULL);
4971558Srgrimes		del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), (char *)NULL);
4981558Srgrimes		return;
4991558Srgrimes	case RPCMNT_EXPORT:
5001558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
5011558Srgrimes			syslog(LOG_ERR, "Can't send reply");
5021558Srgrimes		return;
5031558Srgrimes	default:
5041558Srgrimes		svcerr_noproc(transp);
5051558Srgrimes		return;
5061558Srgrimes	}
5071558Srgrimes}
5081558Srgrimes
5091558Srgrimes/*
5101558Srgrimes * Xdr conversion for a dirpath string
5111558Srgrimes */
5121558Srgrimesint
5131558Srgrimesxdr_dir(xdrsp, dirp)
5141558Srgrimes	XDR *xdrsp;
5151558Srgrimes	char *dirp;
5161558Srgrimes{
5171558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
5181558Srgrimes}
5191558Srgrimes
5201558Srgrimes/*
5219336Sdfr * Xdr routine to generate file handle reply
5221558Srgrimes */
5231558Srgrimesint
5249336Sdfrxdr_fhs(xdrsp, cp)
5251558Srgrimes	XDR *xdrsp;
5269336Sdfr	caddr_t cp;
5271558Srgrimes{
5289336Sdfr	register struct fhreturn *fhrp = (struct fhreturn *)cp;
5299336Sdfr	u_long ok = 0, len, auth;
5301558Srgrimes
5311558Srgrimes	if (!xdr_long(xdrsp, &ok))
5321558Srgrimes		return (0);
5339336Sdfr	switch (fhrp->fhr_vers) {
5349336Sdfr	case 1:
5359336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
5369336Sdfr	case 3:
5379336Sdfr		len = NFSX_V3FH;
5389336Sdfr		if (!xdr_long(xdrsp, &len))
5399336Sdfr			return (0);
5409336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
5419336Sdfr			return (0);
5429336Sdfr		if (fhrp->fhr_flag & DP_KERB)
5439336Sdfr			auth = RPCAUTH_KERB4;
5449336Sdfr		else
5459336Sdfr			auth = RPCAUTH_UNIX;
5469336Sdfr		len = 1;
5479336Sdfr		if (!xdr_long(xdrsp, &len))
5489336Sdfr			return (0);
5499336Sdfr		return (xdr_long(xdrsp, &auth));
5509336Sdfr	};
5519336Sdfr	return (0);
5521558Srgrimes}
5531558Srgrimes
5541558Srgrimesint
5551558Srgrimesxdr_mlist(xdrsp, cp)
5561558Srgrimes	XDR *xdrsp;
5571558Srgrimes	caddr_t cp;
5581558Srgrimes{
5591558Srgrimes	struct mountlist *mlp;
5601558Srgrimes	int true = 1;
5611558Srgrimes	int false = 0;
5621558Srgrimes	char *strp;
5631558Srgrimes
5641558Srgrimes	mlp = mlhead;
5651558Srgrimes	while (mlp) {
5661558Srgrimes		if (!xdr_bool(xdrsp, &true))
5671558Srgrimes			return (0);
5681558Srgrimes		strp = &mlp->ml_host[0];
5691558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
5701558Srgrimes			return (0);
5711558Srgrimes		strp = &mlp->ml_dirp[0];
5721558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
5731558Srgrimes			return (0);
5741558Srgrimes		mlp = mlp->ml_next;
5751558Srgrimes	}
5761558Srgrimes	if (!xdr_bool(xdrsp, &false))
5771558Srgrimes		return (0);
5781558Srgrimes	return (1);
5791558Srgrimes}
5801558Srgrimes
5811558Srgrimes/*
5821558Srgrimes * Xdr conversion for export list
5831558Srgrimes */
5841558Srgrimesint
5851558Srgrimesxdr_explist(xdrsp, cp)
5861558Srgrimes	XDR *xdrsp;
5871558Srgrimes	caddr_t cp;
5881558Srgrimes{
5891558Srgrimes	struct exportlist *ep;
5901558Srgrimes	int false = 0;
5919336Sdfr	int putdef;
5929336Sdfr	sigset_t sighup_mask;
5931558Srgrimes
5949336Sdfr	sigemptyset(&sighup_mask);
5959336Sdfr	sigaddset(&sighup_mask, SIGHUP);
5969336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
5971558Srgrimes	ep = exphead;
5981558Srgrimes	while (ep) {
5991558Srgrimes		putdef = 0;
6001558Srgrimes		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
6011558Srgrimes			goto errout;
6021558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
6031558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
6041558Srgrimes			&putdef))
6051558Srgrimes			goto errout;
6061558Srgrimes		ep = ep->ex_next;
6071558Srgrimes	}
6089336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6091558Srgrimes	if (!xdr_bool(xdrsp, &false))
6101558Srgrimes		return (0);
6111558Srgrimes	return (1);
6121558Srgrimeserrout:
6139336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6141558Srgrimes	return (0);
6151558Srgrimes}
6161558Srgrimes
6171558Srgrimes/*
6181558Srgrimes * Called from xdr_explist() to traverse the tree and export the
6191558Srgrimes * directory paths.
6201558Srgrimes */
6211558Srgrimesint
6221558Srgrimesput_exlist(dp, xdrsp, adp, putdefp)
6231558Srgrimes	struct dirlist *dp;
6241558Srgrimes	XDR *xdrsp;
6251558Srgrimes	struct dirlist *adp;
6261558Srgrimes	int *putdefp;
6271558Srgrimes{
6281558Srgrimes	struct grouplist *grp;
6291558Srgrimes	struct hostlist *hp;
6301558Srgrimes	int true = 1;
6311558Srgrimes	int false = 0;
6321558Srgrimes	int gotalldir = 0;
6331558Srgrimes	char *strp;
6341558Srgrimes
6351558Srgrimes	if (dp) {
6361558Srgrimes		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
6371558Srgrimes			return (1);
6381558Srgrimes		if (!xdr_bool(xdrsp, &true))
6391558Srgrimes			return (1);
6401558Srgrimes		strp = dp->dp_dirp;
6411558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
6421558Srgrimes			return (1);
6431558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
6441558Srgrimes			gotalldir = 1;
6451558Srgrimes			*putdefp = 1;
6461558Srgrimes		}
6471558Srgrimes		if ((dp->dp_flag & DP_DEFSET) == 0 &&
6481558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
6491558Srgrimes			hp = dp->dp_hosts;
6501558Srgrimes			while (hp) {
6511558Srgrimes				grp = hp->ht_grp;
6521558Srgrimes				if (grp->gr_type == GT_HOST) {
6531558Srgrimes					if (!xdr_bool(xdrsp, &true))
6541558Srgrimes						return (1);
6551558Srgrimes					strp = grp->gr_ptr.gt_hostent->h_name;
6568871Srgrimes					if (!xdr_string(xdrsp, &strp,
6571558Srgrimes					    RPCMNT_NAMELEN))
6581558Srgrimes						return (1);
6591558Srgrimes				} else if (grp->gr_type == GT_NET) {
6601558Srgrimes					if (!xdr_bool(xdrsp, &true))
6611558Srgrimes						return (1);
6621558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
6638871Srgrimes					if (!xdr_string(xdrsp, &strp,
6641558Srgrimes					    RPCMNT_NAMELEN))
6651558Srgrimes						return (1);
6661558Srgrimes				}
6671558Srgrimes				hp = hp->ht_next;
6681558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
6691558Srgrimes					hp = adp->dp_hosts;
6701558Srgrimes					gotalldir = 0;
6711558Srgrimes				}
6721558Srgrimes			}
6731558Srgrimes		}
6741558Srgrimes		if (!xdr_bool(xdrsp, &false))
6751558Srgrimes			return (1);
6761558Srgrimes		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
6771558Srgrimes			return (1);
6781558Srgrimes	}
6791558Srgrimes	return (0);
6801558Srgrimes}
6811558Srgrimes
6821558Srgrimes#define LINESIZ	10240
6831558Srgrimeschar line[LINESIZ];
6841558SrgrimesFILE *exp_file;
6851558Srgrimes
6861558Srgrimes/*
6871558Srgrimes * Get the export list
6881558Srgrimes */
6891558Srgrimesvoid
6901558Srgrimesget_exportlist()
6911558Srgrimes{
6921558Srgrimes	struct exportlist *ep, *ep2;
6931558Srgrimes	struct grouplist *grp, *tgrp;
6941558Srgrimes	struct exportlist **epp;
6951558Srgrimes	struct dirlist *dirhead;
6961558Srgrimes	struct statfs fsb, *fsp;
6971558Srgrimes	struct hostent *hpe;
6981558Srgrimes	struct ucred anon;
6991558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
7001558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
7011558Srgrimes
7021558Srgrimes	/*
7031558Srgrimes	 * First, get rid of the old list
7041558Srgrimes	 */
7051558Srgrimes	ep = exphead;
7061558Srgrimes	while (ep) {
7071558Srgrimes		ep2 = ep;
7081558Srgrimes		ep = ep->ex_next;
7091558Srgrimes		free_exp(ep2);
7101558Srgrimes	}
7111558Srgrimes	exphead = (struct exportlist *)NULL;
7121558Srgrimes
7131558Srgrimes	grp = grphead;
7141558Srgrimes	while (grp) {
7151558Srgrimes		tgrp = grp;
7161558Srgrimes		grp = grp->gr_next;
7171558Srgrimes		free_grp(tgrp);
7181558Srgrimes	}
7191558Srgrimes	grphead = (struct grouplist *)NULL;
7201558Srgrimes
7211558Srgrimes	/*
7221558Srgrimes	 * And delete exports that are in the kernel for all local
7231558Srgrimes	 * file systems.
7241558Srgrimes	 * XXX: Should know how to handle all local exportable file systems
72523681Speter	 *      instead of just "ufs".
7261558Srgrimes	 */
7271558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
7281558Srgrimes	for (i = 0; i < num; i++) {
7291558Srgrimes		union {
7301558Srgrimes			struct ufs_args ua;
7311558Srgrimes			struct iso_args ia;
7321558Srgrimes			struct mfs_args ma;
7339336Sdfr			struct msdosfs_args da;
7341558Srgrimes		} targs;
7351558Srgrimes
73623681Speter		if (!strcmp(fsp->f_fstypename, "mfs") ||
73723681Speter		    !strcmp(fsp->f_fstypename, "ufs") ||
73823681Speter		    !strcmp(fsp->f_fstypename, "msdos") ||
73923681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
7409336Sdfr			targs.ua.fspec = NULL;
7419336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
7429336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
7431558Srgrimes				  fsp->f_flags | MNT_UPDATE,
7441558Srgrimes				  (caddr_t)&targs) < 0)
7451558Srgrimes				syslog(LOG_ERR, "Can't delete exports for %s",
7461558Srgrimes				       fsp->f_mntonname);
7471558Srgrimes		}
7481558Srgrimes		fsp++;
7491558Srgrimes	}
7501558Srgrimes
7511558Srgrimes	/*
7521558Srgrimes	 * Read in the exports file and build the list, calling
7531558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
7541558Srgrimes	 */
7551558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
7561558Srgrimes		syslog(LOG_ERR, "Can't open %s", exname);
7571558Srgrimes		exit(2);
7581558Srgrimes	}
7591558Srgrimes	dirhead = (struct dirlist *)NULL;
7601558Srgrimes	while (get_line()) {
7611558Srgrimes		if (debug)
7621558Srgrimes			fprintf(stderr,"Got line %s\n",line);
7631558Srgrimes		cp = line;
7641558Srgrimes		nextfield(&cp, &endcp);
7651558Srgrimes		if (*cp == '#')
7661558Srgrimes			goto nextline;
7671558Srgrimes
7681558Srgrimes		/*
7691558Srgrimes		 * Set defaults.
7701558Srgrimes		 */
7711558Srgrimes		has_host = FALSE;
7721558Srgrimes		anon = def_anon;
7731558Srgrimes		exflags = MNT_EXPORTED;
7741558Srgrimes		got_nondir = 0;
7751558Srgrimes		opt_flags = 0;
7761558Srgrimes		ep = (struct exportlist *)NULL;
7771558Srgrimes
7781558Srgrimes		/*
7791558Srgrimes		 * Create new exports list entry
7801558Srgrimes		 */
7811558Srgrimes		len = endcp-cp;
7821558Srgrimes		tgrp = grp = get_grp();
7831558Srgrimes		while (len > 0) {
7841558Srgrimes			if (len > RPCMNT_NAMELEN) {
7851558Srgrimes			    getexp_err(ep, tgrp);
7861558Srgrimes			    goto nextline;
7871558Srgrimes			}
7881558Srgrimes			if (*cp == '-') {
7891558Srgrimes			    if (ep == (struct exportlist *)NULL) {
7901558Srgrimes				getexp_err(ep, tgrp);
7911558Srgrimes				goto nextline;
7921558Srgrimes			    }
7931558Srgrimes			    if (debug)
7941558Srgrimes				fprintf(stderr, "doing opt %s\n", cp);
7951558Srgrimes			    got_nondir = 1;
7961558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
7971558Srgrimes				&exflags, &anon)) {
7981558Srgrimes				getexp_err(ep, tgrp);
7991558Srgrimes				goto nextline;
8001558Srgrimes			    }
8011558Srgrimes			} else if (*cp == '/') {
8021558Srgrimes			    savedc = *endcp;
8031558Srgrimes			    *endcp = '\0';
8041558Srgrimes			    if (check_dirpath(cp) &&
8051558Srgrimes				statfs(cp, &fsb) >= 0) {
8061558Srgrimes				if (got_nondir) {
8071558Srgrimes				    syslog(LOG_ERR, "Dirs must be first");
8081558Srgrimes				    getexp_err(ep, tgrp);
8091558Srgrimes				    goto nextline;
8101558Srgrimes				}
8111558Srgrimes				if (ep) {
8121558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
8131558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
8141558Srgrimes					getexp_err(ep, tgrp);
8151558Srgrimes					goto nextline;
8161558Srgrimes				    }
8171558Srgrimes				} else {
8181558Srgrimes				    /*
8191558Srgrimes				     * See if this directory is already
8201558Srgrimes				     * in the list.
8211558Srgrimes				     */
8221558Srgrimes				    ep = ex_search(&fsb.f_fsid);
8231558Srgrimes				    if (ep == (struct exportlist *)NULL) {
8241558Srgrimes					ep = get_exp();
8251558Srgrimes					ep->ex_fs = fsb.f_fsid;
8261558Srgrimes					ep->ex_fsdir = (char *)
8271558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
8281558Srgrimes					if (ep->ex_fsdir)
8291558Srgrimes					    strcpy(ep->ex_fsdir,
8301558Srgrimes						fsb.f_mntonname);
8311558Srgrimes					else
8321558Srgrimes					    out_of_mem();
8331558Srgrimes					if (debug)
8341558Srgrimes					  fprintf(stderr,
8351558Srgrimes					      "Making new ep fs=0x%x,0x%x\n",
8361558Srgrimes					      fsb.f_fsid.val[0],
8371558Srgrimes					      fsb.f_fsid.val[1]);
8381558Srgrimes				    } else if (debug)
8391558Srgrimes					fprintf(stderr,
8401558Srgrimes					    "Found ep fs=0x%x,0x%x\n",
8411558Srgrimes					    fsb.f_fsid.val[0],
8421558Srgrimes					    fsb.f_fsid.val[1]);
8431558Srgrimes				}
8441558Srgrimes
8451558Srgrimes				/*
8461558Srgrimes				 * Add dirpath to export mount point.
8471558Srgrimes				 */
8481558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
8491558Srgrimes				dirplen = len;
8501558Srgrimes			    } else {
8511558Srgrimes				getexp_err(ep, tgrp);
8521558Srgrimes				goto nextline;
8531558Srgrimes			    }
8541558Srgrimes			    *endcp = savedc;
8551558Srgrimes			} else {
8561558Srgrimes			    savedc = *endcp;
8571558Srgrimes			    *endcp = '\0';
8581558Srgrimes			    got_nondir = 1;
8591558Srgrimes			    if (ep == (struct exportlist *)NULL) {
8601558Srgrimes				getexp_err(ep, tgrp);
8611558Srgrimes				goto nextline;
8621558Srgrimes			    }
8631558Srgrimes
8641558Srgrimes			    /*
8651558Srgrimes			     * Get the host or netgroup.
8661558Srgrimes			     */
8671558Srgrimes			    setnetgrent(cp);
8681558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
8691558Srgrimes			    do {
8701558Srgrimes				if (has_host) {
8711558Srgrimes				    grp->gr_next = get_grp();
8721558Srgrimes				    grp = grp->gr_next;
8731558Srgrimes				}
8741558Srgrimes				if (netgrp) {
8757401Swpaul				    if (get_host(hst, grp, tgrp)) {
8761558Srgrimes					syslog(LOG_ERR, "Bad netgroup %s", cp);
8771558Srgrimes					getexp_err(ep, tgrp);
8789336Sdfr					endnetgrent();
8791558Srgrimes					goto nextline;
8801558Srgrimes				    }
8817401Swpaul				} else if (get_host(cp, grp, tgrp)) {
8821558Srgrimes				    getexp_err(ep, tgrp);
8831558Srgrimes				    goto nextline;
8841558Srgrimes				}
8851558Srgrimes				has_host = TRUE;
8861558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
8871558Srgrimes			    endnetgrent();
8881558Srgrimes			    *endcp = savedc;
8891558Srgrimes			}
8901558Srgrimes			cp = endcp;
8911558Srgrimes			nextfield(&cp, &endcp);
8921558Srgrimes			len = endcp - cp;
8931558Srgrimes		}
8941558Srgrimes		if (check_options(dirhead)) {
8951558Srgrimes			getexp_err(ep, tgrp);
8961558Srgrimes			goto nextline;
8971558Srgrimes		}
8981558Srgrimes		if (!has_host) {
8991558Srgrimes			grp->gr_type = GT_HOST;
9001558Srgrimes			if (debug)
9011558Srgrimes				fprintf(stderr,"Adding a default entry\n");
9021558Srgrimes			/* add a default group and make the grp list NULL */
9031558Srgrimes			hpe = (struct hostent *)malloc(sizeof(struct hostent));
9041558Srgrimes			if (hpe == (struct hostent *)NULL)
9051558Srgrimes				out_of_mem();
90612348Sjoerg			hpe->h_name = strdup("Default");
9071558Srgrimes			hpe->h_addrtype = AF_INET;
9081558Srgrimes			hpe->h_length = sizeof (u_long);
9091558Srgrimes			hpe->h_addr_list = (char **)NULL;
9101558Srgrimes			grp->gr_ptr.gt_hostent = hpe;
9111558Srgrimes
9121558Srgrimes		/*
9131558Srgrimes		 * Don't allow a network export coincide with a list of
9141558Srgrimes		 * host(s) on the same line.
9151558Srgrimes		 */
9161558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
9171558Srgrimes			getexp_err(ep, tgrp);
9181558Srgrimes			goto nextline;
9191558Srgrimes		}
9201558Srgrimes
9211558Srgrimes		/*
9221558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
9231558Srgrimes		 * After loop, tgrp points to the start of the list and
9241558Srgrimes		 * grp points to the last entry in the list.
9251558Srgrimes		 */
9261558Srgrimes		grp = tgrp;
9271558Srgrimes		do {
9281558Srgrimes		    if (do_mount(ep, grp, exflags, &anon, dirp,
9291558Srgrimes			dirplen, &fsb)) {
9301558Srgrimes			getexp_err(ep, tgrp);
9311558Srgrimes			goto nextline;
9321558Srgrimes		    }
9331558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
9341558Srgrimes
9351558Srgrimes		/*
9361558Srgrimes		 * Success. Update the data structures.
9371558Srgrimes		 */
9381558Srgrimes		if (has_host) {
9399336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
9401558Srgrimes			grp->gr_next = grphead;
9411558Srgrimes			grphead = tgrp;
9421558Srgrimes		} else {
9431558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
9449336Sdfr				opt_flags);
9451558Srgrimes			free_grp(grp);
9461558Srgrimes		}
9471558Srgrimes		dirhead = (struct dirlist *)NULL;
9481558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
9491558Srgrimes			ep2 = exphead;
9501558Srgrimes			epp = &exphead;
9511558Srgrimes
9521558Srgrimes			/*
9531558Srgrimes			 * Insert in the list in alphabetical order.
9541558Srgrimes			 */
9551558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
9561558Srgrimes				epp = &ep2->ex_next;
9571558Srgrimes				ep2 = ep2->ex_next;
9581558Srgrimes			}
9591558Srgrimes			if (ep2)
9601558Srgrimes				ep->ex_next = ep2;
9611558Srgrimes			*epp = ep;
9621558Srgrimes			ep->ex_flag |= EX_LINKED;
9631558Srgrimes		}
9641558Srgrimesnextline:
9651558Srgrimes		if (dirhead) {
9661558Srgrimes			free_dir(dirhead);
9671558Srgrimes			dirhead = (struct dirlist *)NULL;
9681558Srgrimes		}
9691558Srgrimes	}
9701558Srgrimes	fclose(exp_file);
9711558Srgrimes}
9721558Srgrimes
9731558Srgrimes/*
9741558Srgrimes * Allocate an export list element
9751558Srgrimes */
9761558Srgrimesstruct exportlist *
9771558Srgrimesget_exp()
9781558Srgrimes{
9791558Srgrimes	struct exportlist *ep;
9801558Srgrimes
9811558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
9821558Srgrimes	if (ep == (struct exportlist *)NULL)
9831558Srgrimes		out_of_mem();
98423681Speter	memset(ep, 0, sizeof(struct exportlist));
9851558Srgrimes	return (ep);
9861558Srgrimes}
9871558Srgrimes
9881558Srgrimes/*
9891558Srgrimes * Allocate a group list element
9901558Srgrimes */
9911558Srgrimesstruct grouplist *
9921558Srgrimesget_grp()
9931558Srgrimes{
9941558Srgrimes	struct grouplist *gp;
9951558Srgrimes
9961558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
9971558Srgrimes	if (gp == (struct grouplist *)NULL)
9981558Srgrimes		out_of_mem();
99923681Speter	memset(gp, 0, sizeof(struct grouplist));
10001558Srgrimes	return (gp);
10011558Srgrimes}
10021558Srgrimes
10031558Srgrimes/*
10041558Srgrimes * Clean up upon an error in get_exportlist().
10051558Srgrimes */
10061558Srgrimesvoid
10071558Srgrimesgetexp_err(ep, grp)
10081558Srgrimes	struct exportlist *ep;
10091558Srgrimes	struct grouplist *grp;
10101558Srgrimes{
10111558Srgrimes	struct grouplist *tgrp;
10121558Srgrimes
10131558Srgrimes	syslog(LOG_ERR, "Bad exports list line %s", line);
10141558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
10151558Srgrimes		free_exp(ep);
10161558Srgrimes	while (grp) {
10171558Srgrimes		tgrp = grp;
10181558Srgrimes		grp = grp->gr_next;
10191558Srgrimes		free_grp(tgrp);
10201558Srgrimes	}
10211558Srgrimes}
10221558Srgrimes
10231558Srgrimes/*
10241558Srgrimes * Search the export list for a matching fs.
10251558Srgrimes */
10261558Srgrimesstruct exportlist *
10271558Srgrimesex_search(fsid)
10281558Srgrimes	fsid_t *fsid;
10291558Srgrimes{
10301558Srgrimes	struct exportlist *ep;
10311558Srgrimes
10321558Srgrimes	ep = exphead;
10331558Srgrimes	while (ep) {
10341558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
10351558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
10361558Srgrimes			return (ep);
10371558Srgrimes		ep = ep->ex_next;
10381558Srgrimes	}
10391558Srgrimes	return (ep);
10401558Srgrimes}
10411558Srgrimes
10421558Srgrimes/*
10431558Srgrimes * Add a directory path to the list.
10441558Srgrimes */
10451558Srgrimeschar *
10461558Srgrimesadd_expdir(dpp, cp, len)
10471558Srgrimes	struct dirlist **dpp;
10481558Srgrimes	char *cp;
10491558Srgrimes	int len;
10501558Srgrimes{
10511558Srgrimes	struct dirlist *dp;
10521558Srgrimes
10531558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
10541558Srgrimes	dp->dp_left = *dpp;
10551558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
10561558Srgrimes	dp->dp_flag = 0;
10571558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
10581558Srgrimes	strcpy(dp->dp_dirp, cp);
10591558Srgrimes	*dpp = dp;
10601558Srgrimes	return (dp->dp_dirp);
10611558Srgrimes}
10621558Srgrimes
10631558Srgrimes/*
10641558Srgrimes * Hang the dir list element off the dirpath binary tree as required
10651558Srgrimes * and update the entry for host.
10661558Srgrimes */
10671558Srgrimesvoid
10689336Sdfrhang_dirp(dp, grp, ep, flags)
10691558Srgrimes	struct dirlist *dp;
10701558Srgrimes	struct grouplist *grp;
10711558Srgrimes	struct exportlist *ep;
10729336Sdfr	int flags;
10731558Srgrimes{
10741558Srgrimes	struct hostlist *hp;
10751558Srgrimes	struct dirlist *dp2;
10761558Srgrimes
10779336Sdfr	if (flags & OP_ALLDIRS) {
10781558Srgrimes		if (ep->ex_defdir)
10791558Srgrimes			free((caddr_t)dp);
10801558Srgrimes		else
10811558Srgrimes			ep->ex_defdir = dp;
10829336Sdfr		if (grp == (struct grouplist *)NULL) {
10831558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
10849336Sdfr			if (flags & OP_KERB)
10859336Sdfr				ep->ex_defdir->dp_flag |= DP_KERB;
10869336Sdfr		} else while (grp) {
10871558Srgrimes			hp = get_ht();
10889336Sdfr			if (flags & OP_KERB)
10899336Sdfr				hp->ht_flag |= DP_KERB;
10901558Srgrimes			hp->ht_grp = grp;
10911558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
10921558Srgrimes			ep->ex_defdir->dp_hosts = hp;
10931558Srgrimes			grp = grp->gr_next;
10941558Srgrimes		}
10951558Srgrimes	} else {
10961558Srgrimes
10971558Srgrimes		/*
10981558Srgrimes		 * Loop throught the directories adding them to the tree.
10991558Srgrimes		 */
11001558Srgrimes		while (dp) {
11011558Srgrimes			dp2 = dp->dp_left;
11029336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
11031558Srgrimes			dp = dp2;
11041558Srgrimes		}
11051558Srgrimes	}
11061558Srgrimes}
11071558Srgrimes
11081558Srgrimes/*
11091558Srgrimes * Traverse the binary tree either updating a node that is already there
11101558Srgrimes * for the new directory or adding the new node.
11111558Srgrimes */
11121558Srgrimesvoid
11139336Sdfradd_dlist(dpp, newdp, grp, flags)
11141558Srgrimes	struct dirlist **dpp;
11151558Srgrimes	struct dirlist *newdp;
11161558Srgrimes	struct grouplist *grp;
11179336Sdfr	int flags;
11181558Srgrimes{
11191558Srgrimes	struct dirlist *dp;
11201558Srgrimes	struct hostlist *hp;
11211558Srgrimes	int cmp;
11221558Srgrimes
11231558Srgrimes	dp = *dpp;
11241558Srgrimes	if (dp) {
11251558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
11261558Srgrimes		if (cmp > 0) {
11279336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
11281558Srgrimes			return;
11291558Srgrimes		} else if (cmp < 0) {
11309336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
11311558Srgrimes			return;
11321558Srgrimes		} else
11331558Srgrimes			free((caddr_t)newdp);
11341558Srgrimes	} else {
11351558Srgrimes		dp = newdp;
11361558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
11371558Srgrimes		*dpp = dp;
11381558Srgrimes	}
11391558Srgrimes	if (grp) {
11401558Srgrimes
11411558Srgrimes		/*
11421558Srgrimes		 * Hang all of the host(s) off of the directory point.
11431558Srgrimes		 */
11441558Srgrimes		do {
11451558Srgrimes			hp = get_ht();
11469336Sdfr			if (flags & OP_KERB)
11479336Sdfr				hp->ht_flag |= DP_KERB;
11481558Srgrimes			hp->ht_grp = grp;
11491558Srgrimes			hp->ht_next = dp->dp_hosts;
11501558Srgrimes			dp->dp_hosts = hp;
11511558Srgrimes			grp = grp->gr_next;
11521558Srgrimes		} while (grp);
11539336Sdfr	} else {
11541558Srgrimes		dp->dp_flag |= DP_DEFSET;
11559336Sdfr		if (flags & OP_KERB)
11569336Sdfr			dp->dp_flag |= DP_KERB;
11579336Sdfr	}
11581558Srgrimes}
11591558Srgrimes
11601558Srgrimes/*
11611558Srgrimes * Search for a dirpath on the export point.
11621558Srgrimes */
11631558Srgrimesstruct dirlist *
11641558Srgrimesdirp_search(dp, dirpath)
11651558Srgrimes	struct dirlist *dp;
11661558Srgrimes	char *dirpath;
11671558Srgrimes{
11681558Srgrimes	int cmp;
11691558Srgrimes
11701558Srgrimes	if (dp) {
11711558Srgrimes		cmp = strcmp(dp->dp_dirp, dirpath);
11721558Srgrimes		if (cmp > 0)
11731558Srgrimes			return (dirp_search(dp->dp_left, dirpath));
11741558Srgrimes		else if (cmp < 0)
11751558Srgrimes			return (dirp_search(dp->dp_right, dirpath));
11761558Srgrimes		else
11771558Srgrimes			return (dp);
11781558Srgrimes	}
11791558Srgrimes	return (dp);
11801558Srgrimes}
11811558Srgrimes
11821558Srgrimes/*
11831558Srgrimes * Scan for a host match in a directory tree.
11841558Srgrimes */
11851558Srgrimesint
11869336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
11871558Srgrimes	struct dirlist *dp;
11881558Srgrimes	u_long saddr;
11891558Srgrimes	int *defsetp;
11909336Sdfr	int *hostsetp;
11911558Srgrimes{
11921558Srgrimes	struct hostlist *hp;
11931558Srgrimes	struct grouplist *grp;
11941558Srgrimes	u_long **addrp;
11951558Srgrimes
11961558Srgrimes	if (dp) {
11971558Srgrimes		if (dp->dp_flag & DP_DEFSET)
11989336Sdfr			*defsetp = dp->dp_flag;
11991558Srgrimes		hp = dp->dp_hosts;
12001558Srgrimes		while (hp) {
12011558Srgrimes			grp = hp->ht_grp;
12021558Srgrimes			switch (grp->gr_type) {
12031558Srgrimes			case GT_HOST:
12041558Srgrimes			    addrp = (u_long **)
12051558Srgrimes				grp->gr_ptr.gt_hostent->h_addr_list;
12061558Srgrimes			    while (*addrp) {
12079336Sdfr				if (**addrp == saddr) {
12089336Sdfr				    *hostsetp = (hp->ht_flag | DP_HOSTSET);
12091558Srgrimes				    return (1);
12109336Sdfr				}
12111558Srgrimes				addrp++;
12121558Srgrimes			    }
12131558Srgrimes			    break;
12141558Srgrimes			case GT_NET:
12151558Srgrimes			    if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
12169336Sdfr				grp->gr_ptr.gt_net.nt_net) {
12179336Sdfr				*hostsetp = (hp->ht_flag | DP_HOSTSET);
12181558Srgrimes				return (1);
12199336Sdfr			    }
12201558Srgrimes			    break;
12211558Srgrimes			};
12221558Srgrimes			hp = hp->ht_next;
12231558Srgrimes		}
12241558Srgrimes	}
12251558Srgrimes	return (0);
12261558Srgrimes}
12271558Srgrimes
12281558Srgrimes/*
12291558Srgrimes * Scan tree for a host that matches the address.
12301558Srgrimes */
12311558Srgrimesint
12321558Srgrimesscan_tree(dp, saddr)
12331558Srgrimes	struct dirlist *dp;
12341558Srgrimes	u_long saddr;
12351558Srgrimes{
12369336Sdfr	int defset, hostset;
12371558Srgrimes
12381558Srgrimes	if (dp) {
12391558Srgrimes		if (scan_tree(dp->dp_left, saddr))
12401558Srgrimes			return (1);
12419336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
12421558Srgrimes			return (1);
12431558Srgrimes		if (scan_tree(dp->dp_right, saddr))
12441558Srgrimes			return (1);
12451558Srgrimes	}
12461558Srgrimes	return (0);
12471558Srgrimes}
12481558Srgrimes
12491558Srgrimes/*
12501558Srgrimes * Traverse the dirlist tree and free it up.
12511558Srgrimes */
12521558Srgrimesvoid
12531558Srgrimesfree_dir(dp)
12541558Srgrimes	struct dirlist *dp;
12551558Srgrimes{
12561558Srgrimes
12571558Srgrimes	if (dp) {
12581558Srgrimes		free_dir(dp->dp_left);
12591558Srgrimes		free_dir(dp->dp_right);
12601558Srgrimes		free_host(dp->dp_hosts);
12611558Srgrimes		free((caddr_t)dp);
12621558Srgrimes	}
12631558Srgrimes}
12641558Srgrimes
12651558Srgrimes/*
12661558Srgrimes * Parse the option string and update fields.
12671558Srgrimes * Option arguments may either be -<option>=<value> or
12681558Srgrimes * -<option> <value>
12691558Srgrimes */
12701558Srgrimesint
12711558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
12721558Srgrimes	char **cpp, **endcpp;
12731558Srgrimes	struct exportlist *ep;
12741558Srgrimes	struct grouplist *grp;
12751558Srgrimes	int *has_hostp;
12761558Srgrimes	int *exflagsp;
12771558Srgrimes	struct ucred *cr;
12781558Srgrimes{
12791558Srgrimes	char *cpoptarg, *cpoptend;
12801558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
12811558Srgrimes	int allflag, usedarg;
12821558Srgrimes
12831558Srgrimes	cpopt = *cpp;
12841558Srgrimes	cpopt++;
12851558Srgrimes	cp = *endcpp;
12861558Srgrimes	savedc = *cp;
12871558Srgrimes	*cp = '\0';
12881558Srgrimes	while (cpopt && *cpopt) {
12891558Srgrimes		allflag = 1;
12901558Srgrimes		usedarg = -2;
129123681Speter		if (cpoptend = strchr(cpopt, ',')) {
12921558Srgrimes			*cpoptend++ = '\0';
129323681Speter			if (cpoptarg = strchr(cpopt, '='))
12941558Srgrimes				*cpoptarg++ = '\0';
12951558Srgrimes		} else {
129623681Speter			if (cpoptarg = strchr(cpopt, '='))
12971558Srgrimes				*cpoptarg++ = '\0';
12981558Srgrimes			else {
12991558Srgrimes				*cp = savedc;
13001558Srgrimes				nextfield(&cp, &endcp);
13011558Srgrimes				**endcpp = '\0';
13021558Srgrimes				if (endcp > cp && *cp != '-') {
13031558Srgrimes					cpoptarg = cp;
13041558Srgrimes					savedc2 = *endcp;
13051558Srgrimes					*endcp = '\0';
13061558Srgrimes					usedarg = 0;
13071558Srgrimes				}
13081558Srgrimes			}
13091558Srgrimes		}
13101558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
13111558Srgrimes			*exflagsp |= MNT_EXRDONLY;
13121558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
13131558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
13141558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
13151558Srgrimes			usedarg++;
13161558Srgrimes			parsecred(cpoptarg, cr);
13171558Srgrimes			if (allflag == 0) {
13181558Srgrimes				*exflagsp |= MNT_EXPORTANON;
13191558Srgrimes				opt_flags |= OP_MAPALL;
13201558Srgrimes			} else
13211558Srgrimes				opt_flags |= OP_MAPROOT;
13221558Srgrimes		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
13231558Srgrimes			*exflagsp |= MNT_EXKERB;
13241558Srgrimes			opt_flags |= OP_KERB;
13251558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
13261558Srgrimes			!strcmp(cpopt, "m"))) {
13271558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
13281558Srgrimes				syslog(LOG_ERR, "Bad mask: %s", cpoptarg);
13291558Srgrimes				return (1);
13301558Srgrimes			}
13311558Srgrimes			usedarg++;
13321558Srgrimes			opt_flags |= OP_MASK;
13331558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
13341558Srgrimes			!strcmp(cpopt, "n"))) {
13351558Srgrimes			if (grp->gr_type != GT_NULL) {
13361558Srgrimes				syslog(LOG_ERR, "Network/host conflict");
13371558Srgrimes				return (1);
13381558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
13391558Srgrimes				syslog(LOG_ERR, "Bad net: %s", cpoptarg);
13401558Srgrimes				return (1);
13411558Srgrimes			}
13421558Srgrimes			grp->gr_type = GT_NET;
13431558Srgrimes			*has_hostp = 1;
13441558Srgrimes			usedarg++;
13451558Srgrimes			opt_flags |= OP_NET;
13461558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
13471558Srgrimes			opt_flags |= OP_ALLDIRS;
134827447Sdfr		} else if (!strcmp(cpopt, "public")) {
134927447Sdfr			*exflagsp |= MNT_EXPUBLIC;
135027447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
135127447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
135227447Sdfr			opt_flags |= OP_MAPALL;
135327447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
135427447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
13551558Srgrimes#ifdef ISO
13561558Srgrimes		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
13571558Srgrimes			if (get_isoaddr(cpoptarg, grp)) {
13581558Srgrimes				syslog(LOG_ERR, "Bad iso addr: %s", cpoptarg);
13591558Srgrimes				return (1);
13601558Srgrimes			}
13611558Srgrimes			*has_hostp = 1;
13621558Srgrimes			usedarg++;
13631558Srgrimes			opt_flags |= OP_ISO;
13641558Srgrimes#endif /* ISO */
13651558Srgrimes		} else {
13661558Srgrimes			syslog(LOG_ERR, "Bad opt %s", cpopt);
13671558Srgrimes			return (1);
13681558Srgrimes		}
13691558Srgrimes		if (usedarg >= 0) {
13701558Srgrimes			*endcp = savedc2;
13711558Srgrimes			**endcpp = savedc;
13721558Srgrimes			if (usedarg > 0) {
13731558Srgrimes				*cpp = cp;
13741558Srgrimes				*endcpp = endcp;
13751558Srgrimes			}
13761558Srgrimes			return (0);
13771558Srgrimes		}
13781558Srgrimes		cpopt = cpoptend;
13791558Srgrimes	}
13801558Srgrimes	**endcpp = savedc;
13811558Srgrimes	return (0);
13821558Srgrimes}
13831558Srgrimes
13841558Srgrimes/*
13851558Srgrimes * Translate a character string to the corresponding list of network
13861558Srgrimes * addresses for a hostname.
13871558Srgrimes */
13881558Srgrimesint
13897401Swpaulget_host(cp, grp, tgrp)
13901558Srgrimes	char *cp;
13911558Srgrimes	struct grouplist *grp;
13927401Swpaul	struct grouplist *tgrp;
13931558Srgrimes{
13947401Swpaul	struct grouplist *checkgrp;
13951558Srgrimes	struct hostent *hp, *nhp;
13961558Srgrimes	char **addrp, **naddrp;
13971558Srgrimes	struct hostent t_host;
13981558Srgrimes	int i;
13991558Srgrimes	u_long saddr;
14001558Srgrimes	char *aptr[2];
14011558Srgrimes
14021558Srgrimes	if (grp->gr_type != GT_NULL)
14031558Srgrimes		return (1);
14041558Srgrimes	if ((hp = gethostbyname(cp)) == NULL) {
14051558Srgrimes		if (isdigit(*cp)) {
14061558Srgrimes			saddr = inet_addr(cp);
14071558Srgrimes			if (saddr == -1) {
14089336Sdfr 				syslog(LOG_ERR, "Inet_addr failed for %s", cp);
14091558Srgrimes				return (1);
14101558Srgrimes			}
14111558Srgrimes			if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
14121558Srgrimes				AF_INET)) == NULL) {
14131558Srgrimes				hp = &t_host;
14141558Srgrimes				hp->h_name = cp;
14151558Srgrimes				hp->h_addrtype = AF_INET;
14161558Srgrimes				hp->h_length = sizeof (u_long);
14171558Srgrimes				hp->h_addr_list = aptr;
14181558Srgrimes				aptr[0] = (char *)&saddr;
14191558Srgrimes				aptr[1] = (char *)NULL;
14201558Srgrimes			}
14211558Srgrimes		} else {
14229336Sdfr 			syslog(LOG_ERR, "Gethostbyname failed for %s", cp);
14231558Srgrimes			return (1);
14241558Srgrimes		}
14251558Srgrimes	}
14267401Swpaul        /*
14277401Swpaul         * Sanity check: make sure we don't already have an entry
14287401Swpaul         * for this host in the grouplist.
14297401Swpaul         */
14307401Swpaul        checkgrp = tgrp;
14317401Swpaul        while (checkgrp) {
143217887Swpaul		if (checkgrp->gr_type == GT_HOST &&
143317887Swpaul                    checkgrp->gr_ptr.gt_hostent != NULL &&
14347401Swpaul                    !strcmp(checkgrp->gr_ptr.gt_hostent->h_name, hp->h_name)) {
14357401Swpaul                        grp->gr_type = GT_IGNORE;
14367401Swpaul			return(0);
14377401Swpaul		}
14387401Swpaul                checkgrp = checkgrp->gr_next;
14397401Swpaul        }
14407401Swpaul
14411558Srgrimes	grp->gr_type = GT_HOST;
14421558Srgrimes	nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
14431558Srgrimes		malloc(sizeof(struct hostent));
14441558Srgrimes	if (nhp == (struct hostent *)NULL)
14451558Srgrimes		out_of_mem();
144623681Speter	memmove(nhp, hp, sizeof(struct hostent));
14471558Srgrimes	i = strlen(hp->h_name)+1;
14481558Srgrimes	nhp->h_name = (char *)malloc(i);
14491558Srgrimes	if (nhp->h_name == (char *)NULL)
14501558Srgrimes		out_of_mem();
145123681Speter	memmove(nhp->h_name, hp->h_name, i);
14521558Srgrimes	addrp = hp->h_addr_list;
14531558Srgrimes	i = 1;
14541558Srgrimes	while (*addrp++)
14551558Srgrimes		i++;
14561558Srgrimes	naddrp = nhp->h_addr_list = (char **)
14571558Srgrimes		malloc(i*sizeof(char *));
14581558Srgrimes	if (naddrp == (char **)NULL)
14591558Srgrimes		out_of_mem();
14601558Srgrimes	addrp = hp->h_addr_list;
14611558Srgrimes	while (*addrp) {
14621558Srgrimes		*naddrp = (char *)
14631558Srgrimes		    malloc(hp->h_length);
14641558Srgrimes		if (*naddrp == (char *)NULL)
14651558Srgrimes		    out_of_mem();
146623681Speter		memmove(*naddrp, *addrp, hp->h_length);
14671558Srgrimes		addrp++;
14681558Srgrimes		naddrp++;
14691558Srgrimes	}
14701558Srgrimes	*naddrp = (char *)NULL;
14711558Srgrimes	if (debug)
14721558Srgrimes		fprintf(stderr, "got host %s\n", hp->h_name);
14731558Srgrimes	return (0);
14741558Srgrimes}
14751558Srgrimes
14761558Srgrimes/*
14771558Srgrimes * Free up an exports list component
14781558Srgrimes */
14791558Srgrimesvoid
14801558Srgrimesfree_exp(ep)
14811558Srgrimes	struct exportlist *ep;
14821558Srgrimes{
14831558Srgrimes
14841558Srgrimes	if (ep->ex_defdir) {
14851558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
14861558Srgrimes		free((caddr_t)ep->ex_defdir);
14871558Srgrimes	}
14881558Srgrimes	if (ep->ex_fsdir)
14891558Srgrimes		free(ep->ex_fsdir);
149027447Sdfr	if (ep->ex_indexfile)
149127447Sdfr		free(ep->ex_indexfile);
14921558Srgrimes	free_dir(ep->ex_dirl);
14931558Srgrimes	free((caddr_t)ep);
14941558Srgrimes}
14951558Srgrimes
14961558Srgrimes/*
14971558Srgrimes * Free hosts.
14981558Srgrimes */
14991558Srgrimesvoid
15001558Srgrimesfree_host(hp)
15011558Srgrimes	struct hostlist *hp;
15021558Srgrimes{
15031558Srgrimes	struct hostlist *hp2;
15041558Srgrimes
15051558Srgrimes	while (hp) {
15061558Srgrimes		hp2 = hp;
15071558Srgrimes		hp = hp->ht_next;
15081558Srgrimes		free((caddr_t)hp2);
15091558Srgrimes	}
15101558Srgrimes}
15111558Srgrimes
15121558Srgrimesstruct hostlist *
15131558Srgrimesget_ht()
15141558Srgrimes{
15151558Srgrimes	struct hostlist *hp;
15161558Srgrimes
15171558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
15181558Srgrimes	if (hp == (struct hostlist *)NULL)
15191558Srgrimes		out_of_mem();
15201558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
15219336Sdfr	hp->ht_flag = 0;
15221558Srgrimes	return (hp);
15231558Srgrimes}
15241558Srgrimes
15251558Srgrimes#ifdef ISO
15261558Srgrimes/*
15271558Srgrimes * Translate an iso address.
15281558Srgrimes */
15291558Srgrimesget_isoaddr(cp, grp)
15301558Srgrimes	char *cp;
15311558Srgrimes	struct grouplist *grp;
15321558Srgrimes{
15331558Srgrimes	struct iso_addr *isop;
15341558Srgrimes	struct sockaddr_iso *isoaddr;
15351558Srgrimes
15361558Srgrimes	if (grp->gr_type != GT_NULL)
15371558Srgrimes		return (1);
15381558Srgrimes	if ((isop = iso_addr(cp)) == NULL) {
15391558Srgrimes		syslog(LOG_ERR,
15401558Srgrimes		    "iso_addr failed, ignored");
15411558Srgrimes		return (1);
15421558Srgrimes	}
15431558Srgrimes	isoaddr = (struct sockaddr_iso *)
15441558Srgrimes	    malloc(sizeof (struct sockaddr_iso));
15451558Srgrimes	if (isoaddr == (struct sockaddr_iso *)NULL)
15461558Srgrimes		out_of_mem();
154723681Speter	memset(isoaddr, 0, sizeof(struct sockaddr_iso));
154823681Speter	memmove(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
154923681Speter	isoaddr->siso_len = sizeof(struct sockaddr_iso);
15501558Srgrimes	isoaddr->siso_family = AF_ISO;
15511558Srgrimes	grp->gr_type = GT_ISO;
15521558Srgrimes	grp->gr_ptr.gt_isoaddr = isoaddr;
15531558Srgrimes	return (0);
15541558Srgrimes}
15551558Srgrimes#endif	/* ISO */
15561558Srgrimes
15571558Srgrimes/*
15581558Srgrimes * Out of memory, fatal
15591558Srgrimes */
15601558Srgrimesvoid
15611558Srgrimesout_of_mem()
15621558Srgrimes{
15631558Srgrimes
15641558Srgrimes	syslog(LOG_ERR, "Out of memory");
15651558Srgrimes	exit(2);
15661558Srgrimes}
15671558Srgrimes
15681558Srgrimes/*
15691558Srgrimes * Do the mount syscall with the update flag to push the export info into
15701558Srgrimes * the kernel.
15711558Srgrimes */
15721558Srgrimesint
15731558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
15741558Srgrimes	struct exportlist *ep;
15751558Srgrimes	struct grouplist *grp;
15761558Srgrimes	int exflags;
15771558Srgrimes	struct ucred *anoncrp;
15781558Srgrimes	char *dirp;
15791558Srgrimes	int dirplen;
15801558Srgrimes	struct statfs *fsb;
15811558Srgrimes{
15821558Srgrimes	char *cp = (char *)NULL;
15831558Srgrimes	u_long **addrp;
15841558Srgrimes	int done;
15851558Srgrimes	char savedc = '\0';
15861558Srgrimes	struct sockaddr_in sin, imask;
15871558Srgrimes	union {
15881558Srgrimes		struct ufs_args ua;
15891558Srgrimes		struct iso_args ia;
15901558Srgrimes		struct mfs_args ma;
15919336Sdfr#ifdef __NetBSD__
15929336Sdfr		struct msdosfs_args da;
15939336Sdfr#endif
15941558Srgrimes	} args;
15951558Srgrimes	u_long net;
15961558Srgrimes
15971558Srgrimes	args.ua.fspec = 0;
15981558Srgrimes	args.ua.export.ex_flags = exflags;
15991558Srgrimes	args.ua.export.ex_anon = *anoncrp;
160027447Sdfr	args.ua.export.ex_indexfile = ep->ex_indexfile;
160123681Speter	memset(&sin, 0, sizeof(sin));
160223681Speter	memset(&imask, 0, sizeof(imask));
16031558Srgrimes	sin.sin_family = AF_INET;
16041558Srgrimes	sin.sin_len = sizeof(sin);
16051558Srgrimes	imask.sin_family = AF_INET;
16061558Srgrimes	imask.sin_len = sizeof(sin);
16071558Srgrimes	if (grp->gr_type == GT_HOST)
16081558Srgrimes		addrp = (u_long **)grp->gr_ptr.gt_hostent->h_addr_list;
16091558Srgrimes	else
16101558Srgrimes		addrp = (u_long **)NULL;
16111558Srgrimes	done = FALSE;
16121558Srgrimes	while (!done) {
16131558Srgrimes		switch (grp->gr_type) {
16141558Srgrimes		case GT_HOST:
16151558Srgrimes			if (addrp) {
16161558Srgrimes				sin.sin_addr.s_addr = **addrp;
16171558Srgrimes				args.ua.export.ex_addrlen = sizeof(sin);
16181558Srgrimes			} else
16191558Srgrimes				args.ua.export.ex_addrlen = 0;
16201558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16211558Srgrimes			args.ua.export.ex_masklen = 0;
16221558Srgrimes			break;
16231558Srgrimes		case GT_NET:
16241558Srgrimes			if (grp->gr_ptr.gt_net.nt_mask)
16251558Srgrimes			    imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
16261558Srgrimes			else {
16271558Srgrimes			    net = ntohl(grp->gr_ptr.gt_net.nt_net);
16281558Srgrimes			    if (IN_CLASSA(net))
16291558Srgrimes				imask.sin_addr.s_addr = inet_addr("255.0.0.0");
16301558Srgrimes			    else if (IN_CLASSB(net))
16311558Srgrimes				imask.sin_addr.s_addr =
16321558Srgrimes				    inet_addr("255.255.0.0");
16331558Srgrimes			    else
16341558Srgrimes				imask.sin_addr.s_addr =
16351558Srgrimes				    inet_addr("255.255.255.0");
16361558Srgrimes			    grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
16371558Srgrimes			}
16381558Srgrimes			sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
16391558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
16401558Srgrimes			args.ua.export.ex_addrlen = sizeof (sin);
16411558Srgrimes			args.ua.export.ex_mask = (struct sockaddr *)&imask;
16421558Srgrimes			args.ua.export.ex_masklen = sizeof (imask);
16431558Srgrimes			break;
16441558Srgrimes#ifdef ISO
16451558Srgrimes		case GT_ISO:
16461558Srgrimes			args.ua.export.ex_addr =
16471558Srgrimes				(struct sockaddr *)grp->gr_ptr.gt_isoaddr;
16481558Srgrimes			args.ua.export.ex_addrlen =
16491558Srgrimes				sizeof(struct sockaddr_iso);
16501558Srgrimes			args.ua.export.ex_masklen = 0;
16511558Srgrimes			break;
16521558Srgrimes#endif	/* ISO */
16537401Swpaul		case GT_IGNORE:
16547401Swpaul			return(0);
16557401Swpaul			break;
16561558Srgrimes		default:
16571558Srgrimes			syslog(LOG_ERR, "Bad grouptype");
16581558Srgrimes			if (cp)
16591558Srgrimes				*cp = savedc;
16601558Srgrimes			return (1);
16611558Srgrimes		};
16621558Srgrimes
16631558Srgrimes		/*
16641558Srgrimes		 * XXX:
16651558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
16661558Srgrimes		 * of looping back up the dirp to the mount point??
16671558Srgrimes		 * Also, needs to know how to export all types of local
166823681Speter		 * exportable file systems and not just "ufs".
16691558Srgrimes		 */
16709336Sdfr		while (mount(fsb->f_fstypename, dirp,
16711558Srgrimes		       fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
16721558Srgrimes			if (cp)
16731558Srgrimes				*cp-- = savedc;
16741558Srgrimes			else
16751558Srgrimes				cp = dirp + dirplen - 1;
16761558Srgrimes			if (errno == EPERM) {
16771558Srgrimes				syslog(LOG_ERR,
16781558Srgrimes				   "Can't change attributes for %s.\n", dirp);
16791558Srgrimes				return (1);
16801558Srgrimes			}
16811558Srgrimes			if (opt_flags & OP_ALLDIRS) {
16824895Swollman				syslog(LOG_ERR, "Could not remount %s: %m",
16834895Swollman					dirp);
16841558Srgrimes				return (1);
16851558Srgrimes			}
16861558Srgrimes			/* back up over the last component */
16871558Srgrimes			while (*cp == '/' && cp > dirp)
16881558Srgrimes				cp--;
16891558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
16901558Srgrimes				cp--;
16911558Srgrimes			if (cp == dirp) {
16921558Srgrimes				if (debug)
16931558Srgrimes					fprintf(stderr,"mnt unsucc\n");
16941558Srgrimes				syslog(LOG_ERR, "Can't export %s", dirp);
16951558Srgrimes				return (1);
16961558Srgrimes			}
16971558Srgrimes			savedc = *cp;
16981558Srgrimes			*cp = '\0';
16991558Srgrimes		}
17001558Srgrimes		if (addrp) {
17011558Srgrimes			++addrp;
17021558Srgrimes			if (*addrp == (u_long *)NULL)
17031558Srgrimes				done = TRUE;
17041558Srgrimes		} else
17051558Srgrimes			done = TRUE;
17061558Srgrimes	}
17071558Srgrimes	if (cp)
17081558Srgrimes		*cp = savedc;
17091558Srgrimes	return (0);
17101558Srgrimes}
17111558Srgrimes
17121558Srgrimes/*
17131558Srgrimes * Translate a net address.
17141558Srgrimes */
17151558Srgrimesint
17161558Srgrimesget_net(cp, net, maskflg)
17171558Srgrimes	char *cp;
17181558Srgrimes	struct netmsk *net;
17191558Srgrimes	int maskflg;
17201558Srgrimes{
17211558Srgrimes	struct netent *np;
17221558Srgrimes	long netaddr;
17231558Srgrimes	struct in_addr inetaddr, inetaddr2;
17241558Srgrimes	char *name;
17251558Srgrimes
172625318Spst	if (isdigit(*cp) && ((netaddr = inet_network(cp)) != -1)) {
17271558Srgrimes		inetaddr = inet_makeaddr(netaddr, 0);
17281558Srgrimes		/*
17291558Srgrimes		 * Due to arbritrary subnet masks, you don't know how many
17301558Srgrimes		 * bits to shift the address to make it into a network,
17311558Srgrimes		 * however you do know how to make a network address into
17321558Srgrimes		 * a host with host == 0 and then compare them.
17331558Srgrimes		 * (What a pest)
17341558Srgrimes		 */
17351558Srgrimes		if (!maskflg) {
17361558Srgrimes			setnetent(0);
17371558Srgrimes			while (np = getnetent()) {
17381558Srgrimes				inetaddr2 = inet_makeaddr(np->n_net, 0);
17391558Srgrimes				if (inetaddr2.s_addr == inetaddr.s_addr)
17401558Srgrimes					break;
17411558Srgrimes			}
17421558Srgrimes			endnetent();
17431558Srgrimes		}
174425318Spst	} else if ((np = getnetbyname(cp)) != NULL) {
174525318Spst		inetaddr = inet_makeaddr(np->n_net, 0);
17461558Srgrimes	} else
17471558Srgrimes		return (1);
174825318Spst
17491558Srgrimes	if (maskflg)
17501558Srgrimes		net->nt_mask = inetaddr.s_addr;
17511558Srgrimes	else {
17521558Srgrimes		if (np)
17531558Srgrimes			name = np->n_name;
17541558Srgrimes		else
17551558Srgrimes			name = inet_ntoa(inetaddr);
17561558Srgrimes		net->nt_name = (char *)malloc(strlen(name) + 1);
17571558Srgrimes		if (net->nt_name == (char *)NULL)
17581558Srgrimes			out_of_mem();
17591558Srgrimes		strcpy(net->nt_name, name);
17601558Srgrimes		net->nt_net = inetaddr.s_addr;
17611558Srgrimes	}
17621558Srgrimes	return (0);
17631558Srgrimes}
17641558Srgrimes
17651558Srgrimes/*
17661558Srgrimes * Parse out the next white space separated field
17671558Srgrimes */
17681558Srgrimesvoid
17691558Srgrimesnextfield(cp, endcp)
17701558Srgrimes	char **cp;
17711558Srgrimes	char **endcp;
17721558Srgrimes{
17731558Srgrimes	char *p;
17741558Srgrimes
17751558Srgrimes	p = *cp;
17761558Srgrimes	while (*p == ' ' || *p == '\t')
17771558Srgrimes		p++;
17781558Srgrimes	if (*p == '\n' || *p == '\0')
17791558Srgrimes		*cp = *endcp = p;
17801558Srgrimes	else {
17811558Srgrimes		*cp = p++;
17821558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
17831558Srgrimes			p++;
17841558Srgrimes		*endcp = p;
17851558Srgrimes	}
17861558Srgrimes}
17871558Srgrimes
17881558Srgrimes/*
17891558Srgrimes * Get an exports file line. Skip over blank lines and handle line
17901558Srgrimes * continuations.
17911558Srgrimes */
17921558Srgrimesint
17931558Srgrimesget_line()
17941558Srgrimes{
17951558Srgrimes	char *p, *cp;
17961558Srgrimes	int len;
17971558Srgrimes	int totlen, cont_line;
17981558Srgrimes
17991558Srgrimes	/*
18001558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
18011558Srgrimes	 */
18021558Srgrimes	p = line;
18031558Srgrimes	totlen = 0;
18041558Srgrimes	do {
18051558Srgrimes		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
18061558Srgrimes			return (0);
18071558Srgrimes		len = strlen(p);
18081558Srgrimes		cp = p + len - 1;
18091558Srgrimes		cont_line = 0;
18101558Srgrimes		while (cp >= p &&
18111558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
18121558Srgrimes			if (*cp == '\\')
18131558Srgrimes				cont_line = 1;
18141558Srgrimes			cp--;
18151558Srgrimes			len--;
18161558Srgrimes		}
18171558Srgrimes		*++cp = '\0';
18181558Srgrimes		if (len > 0) {
18191558Srgrimes			totlen += len;
18201558Srgrimes			if (totlen >= LINESIZ) {
18211558Srgrimes				syslog(LOG_ERR, "Exports line too long");
18221558Srgrimes				exit(2);
18231558Srgrimes			}
18241558Srgrimes			p = cp;
18251558Srgrimes		}
18261558Srgrimes	} while (totlen == 0 || cont_line);
18271558Srgrimes	return (1);
18281558Srgrimes}
18291558Srgrimes
18301558Srgrimes/*
18311558Srgrimes * Parse a description of a credential.
18321558Srgrimes */
18331558Srgrimesvoid
18341558Srgrimesparsecred(namelist, cr)
18351558Srgrimes	char *namelist;
18361558Srgrimes	struct ucred *cr;
18371558Srgrimes{
18381558Srgrimes	char *name;
18391558Srgrimes	int cnt;
18401558Srgrimes	char *names;
18411558Srgrimes	struct passwd *pw;
18421558Srgrimes	struct group *gr;
18431558Srgrimes	int ngroups, groups[NGROUPS + 1];
18441558Srgrimes
18451558Srgrimes	/*
18461558Srgrimes	 * Set up the unpriviledged user.
18471558Srgrimes	 */
18481558Srgrimes	cr->cr_ref = 1;
18491558Srgrimes	cr->cr_uid = -2;
18501558Srgrimes	cr->cr_groups[0] = -2;
18511558Srgrimes	cr->cr_ngroups = 1;
18521558Srgrimes	/*
18531558Srgrimes	 * Get the user's password table entry.
18541558Srgrimes	 */
18551558Srgrimes	names = strsep(&namelist, " \t\n");
18561558Srgrimes	name = strsep(&names, ":");
18571558Srgrimes	if (isdigit(*name) || *name == '-')
18581558Srgrimes		pw = getpwuid(atoi(name));
18591558Srgrimes	else
18601558Srgrimes		pw = getpwnam(name);
18611558Srgrimes	/*
18621558Srgrimes	 * Credentials specified as those of a user.
18631558Srgrimes	 */
18641558Srgrimes	if (names == NULL) {
18651558Srgrimes		if (pw == NULL) {
18661558Srgrimes			syslog(LOG_ERR, "Unknown user: %s", name);
18671558Srgrimes			return;
18681558Srgrimes		}
18691558Srgrimes		cr->cr_uid = pw->pw_uid;
18701558Srgrimes		ngroups = NGROUPS + 1;
18711558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
18721558Srgrimes			syslog(LOG_ERR, "Too many groups");
18731558Srgrimes		/*
18741558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
18751558Srgrimes		 */
18761558Srgrimes		cr->cr_ngroups = ngroups - 1;
18771558Srgrimes		cr->cr_groups[0] = groups[0];
18781558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
18791558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
18801558Srgrimes		return;
18811558Srgrimes	}
18821558Srgrimes	/*
18831558Srgrimes	 * Explicit credential specified as a colon separated list:
18841558Srgrimes	 *	uid:gid:gid:...
18851558Srgrimes	 */
18861558Srgrimes	if (pw != NULL)
18871558Srgrimes		cr->cr_uid = pw->pw_uid;
18881558Srgrimes	else if (isdigit(*name) || *name == '-')
18891558Srgrimes		cr->cr_uid = atoi(name);
18901558Srgrimes	else {
18911558Srgrimes		syslog(LOG_ERR, "Unknown user: %s", name);
18921558Srgrimes		return;
18931558Srgrimes	}
18941558Srgrimes	cr->cr_ngroups = 0;
18951558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
18961558Srgrimes		name = strsep(&names, ":");
18971558Srgrimes		if (isdigit(*name) || *name == '-') {
18981558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
18991558Srgrimes		} else {
19001558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
19011558Srgrimes				syslog(LOG_ERR, "Unknown group: %s", name);
19021558Srgrimes				continue;
19031558Srgrimes			}
19041558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
19051558Srgrimes		}
19061558Srgrimes	}
19071558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
19081558Srgrimes		syslog(LOG_ERR, "Too many groups");
19091558Srgrimes}
19101558Srgrimes
19111558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
19121558Srgrimes/*
19131558Srgrimes * Routines that maintain the remote mounttab
19141558Srgrimes */
19151558Srgrimesvoid
19161558Srgrimesget_mountlist()
19171558Srgrimes{
19181558Srgrimes	struct mountlist *mlp, **mlpp;
191923681Speter	char *host, *dirp, *cp;
19201558Srgrimes	int len;
19211558Srgrimes	char str[STRSIZ];
19221558Srgrimes	FILE *mlfile;
19231558Srgrimes
19241558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
19251558Srgrimes		syslog(LOG_ERR, "Can't open %s", _PATH_RMOUNTLIST);
19261558Srgrimes		return;
19271558Srgrimes	}
19281558Srgrimes	mlpp = &mlhead;
19291558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
193023681Speter		cp = str;
193123681Speter		host = strsep(&cp, " \t\n");
193223681Speter		dirp = strsep(&cp, " \t\n");
193323681Speter		if (host == NULL || dirp == NULL)
19341558Srgrimes			continue;
19351558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
193623681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
193723681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
193823681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
193923681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
19401558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
19411558Srgrimes		*mlpp = mlp;
19421558Srgrimes		mlpp = &mlp->ml_next;
19431558Srgrimes	}
19441558Srgrimes	fclose(mlfile);
19451558Srgrimes}
19461558Srgrimes
19471558Srgrimesvoid
19481558Srgrimesdel_mlist(hostp, dirp)
19491558Srgrimes	char *hostp, *dirp;
19501558Srgrimes{
19511558Srgrimes	struct mountlist *mlp, **mlpp;
19521558Srgrimes	struct mountlist *mlp2;
19531558Srgrimes	FILE *mlfile;
19541558Srgrimes	int fnd = 0;
19551558Srgrimes
19561558Srgrimes	mlpp = &mlhead;
19571558Srgrimes	mlp = mlhead;
19581558Srgrimes	while (mlp) {
19591558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
19601558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
19611558Srgrimes			fnd = 1;
19621558Srgrimes			mlp2 = mlp;
19631558Srgrimes			*mlpp = mlp = mlp->ml_next;
19641558Srgrimes			free((caddr_t)mlp2);
19651558Srgrimes		} else {
19661558Srgrimes			mlpp = &mlp->ml_next;
19671558Srgrimes			mlp = mlp->ml_next;
19681558Srgrimes		}
19691558Srgrimes	}
19701558Srgrimes	if (fnd) {
19711558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
19721558Srgrimes			syslog(LOG_ERR,"Can't update %s", _PATH_RMOUNTLIST);
19731558Srgrimes			return;
19741558Srgrimes		}
19751558Srgrimes		mlp = mlhead;
19761558Srgrimes		while (mlp) {
19771558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
19781558Srgrimes			mlp = mlp->ml_next;
19791558Srgrimes		}
19801558Srgrimes		fclose(mlfile);
19811558Srgrimes	}
19821558Srgrimes}
19831558Srgrimes
19841558Srgrimesvoid
19851558Srgrimesadd_mlist(hostp, dirp)
19861558Srgrimes	char *hostp, *dirp;
19871558Srgrimes{
19881558Srgrimes	struct mountlist *mlp, **mlpp;
19891558Srgrimes	FILE *mlfile;
19901558Srgrimes
19911558Srgrimes	mlpp = &mlhead;
19921558Srgrimes	mlp = mlhead;
19931558Srgrimes	while (mlp) {
19941558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
19951558Srgrimes			return;
19961558Srgrimes		mlpp = &mlp->ml_next;
19971558Srgrimes		mlp = mlp->ml_next;
19981558Srgrimes	}
19991558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
20001558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
20011558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
20021558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
20031558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
20041558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
20051558Srgrimes	*mlpp = mlp;
20061558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
20071558Srgrimes		syslog(LOG_ERR, "Can't update %s", _PATH_RMOUNTLIST);
20081558Srgrimes		return;
20091558Srgrimes	}
20101558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
20111558Srgrimes	fclose(mlfile);
20121558Srgrimes}
20131558Srgrimes
20141558Srgrimes/*
20151558Srgrimes * This function is called via. SIGTERM when the system is going down.
20161558Srgrimes * It sends a broadcast RPCMNT_UMNTALL.
20171558Srgrimes */
20181558Srgrimesvoid
20191558Srgrimessend_umntall()
20201558Srgrimes{
20211558Srgrimes	(void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
20221558Srgrimes		xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
20231558Srgrimes	exit(0);
20241558Srgrimes}
20251558Srgrimes
20261558Srgrimesint
20271558Srgrimesumntall_each(resultsp, raddr)
20281558Srgrimes	caddr_t resultsp;
20291558Srgrimes	struct sockaddr_in *raddr;
20301558Srgrimes{
20311558Srgrimes	return (1);
20321558Srgrimes}
20331558Srgrimes
20341558Srgrimes/*
20351558Srgrimes * Free up a group list.
20361558Srgrimes */
20371558Srgrimesvoid
20381558Srgrimesfree_grp(grp)
20391558Srgrimes	struct grouplist *grp;
20401558Srgrimes{
20411558Srgrimes	char **addrp;
20421558Srgrimes
20431558Srgrimes	if (grp->gr_type == GT_HOST) {
20441558Srgrimes		if (grp->gr_ptr.gt_hostent->h_name) {
20451558Srgrimes			addrp = grp->gr_ptr.gt_hostent->h_addr_list;
20461558Srgrimes			while (addrp && *addrp)
20471558Srgrimes				free(*addrp++);
20481558Srgrimes			free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
20491558Srgrimes			free(grp->gr_ptr.gt_hostent->h_name);
20501558Srgrimes		}
20511558Srgrimes		free((caddr_t)grp->gr_ptr.gt_hostent);
20521558Srgrimes	} else if (grp->gr_type == GT_NET) {
20531558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
20541558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
20551558Srgrimes	}
20561558Srgrimes#ifdef ISO
20571558Srgrimes	else if (grp->gr_type == GT_ISO)
20581558Srgrimes		free((caddr_t)grp->gr_ptr.gt_isoaddr);
20591558Srgrimes#endif
20601558Srgrimes	free((caddr_t)grp);
20611558Srgrimes}
20621558Srgrimes
20631558Srgrimes#ifdef DEBUG
20641558Srgrimesvoid
20651558SrgrimesSYSLOG(int pri, const char *fmt, ...)
20661558Srgrimes{
20671558Srgrimes	va_list ap;
20681558Srgrimes
20691558Srgrimes	va_start(ap, fmt);
20701558Srgrimes	vfprintf(stderr, fmt, ap);
20711558Srgrimes	va_end(ap);
20721558Srgrimes}
20731558Srgrimes#endif /* DEBUG */
20741558Srgrimes
20751558Srgrimes/*
20761558Srgrimes * Check options for consistency.
20771558Srgrimes */
20781558Srgrimesint
20791558Srgrimescheck_options(dp)
20801558Srgrimes	struct dirlist *dp;
20811558Srgrimes{
20821558Srgrimes
20831558Srgrimes	if (dp == (struct dirlist *)NULL)
20841558Srgrimes	    return (1);
20851558Srgrimes	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
20861558Srgrimes	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
20871558Srgrimes	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
20881558Srgrimes	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
20891558Srgrimes	    return (1);
20901558Srgrimes	}
20911558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
20921558Srgrimes	    syslog(LOG_ERR, "-mask requires -net");
20931558Srgrimes	    return (1);
20941558Srgrimes	}
20951558Srgrimes	if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
20961558Srgrimes	    syslog(LOG_ERR, "-net and -iso mutually exclusive");
20971558Srgrimes	    return (1);
20981558Srgrimes	}
20991558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
21001558Srgrimes	    syslog(LOG_ERR, "-alldir has multiple directories");
21011558Srgrimes	    return (1);
21021558Srgrimes	}
21031558Srgrimes	return (0);
21041558Srgrimes}
21051558Srgrimes
21061558Srgrimes/*
21071558Srgrimes * Check an absolute directory path for any symbolic links. Return true
21081558Srgrimes * if no symbolic links are found.
21091558Srgrimes */
21101558Srgrimesint
21111558Srgrimescheck_dirpath(dirp)
21121558Srgrimes	char *dirp;
21131558Srgrimes{
21141558Srgrimes	char *cp;
21151558Srgrimes	int ret = 1;
21161558Srgrimes	struct stat sb;
21171558Srgrimes
21181558Srgrimes	cp = dirp + 1;
21191558Srgrimes	while (*cp && ret) {
21201558Srgrimes		if (*cp == '/') {
21211558Srgrimes			*cp = '\0';
21229336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21231558Srgrimes				ret = 0;
21241558Srgrimes			*cp = '/';
21251558Srgrimes		}
21261558Srgrimes		cp++;
21271558Srgrimes	}
21289336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
21291558Srgrimes		ret = 0;
21301558Srgrimes	return (ret);
21311558Srgrimes}
21329336Sdfr
21339336Sdfr/*
21349336Sdfr * Just translate an ascii string to an integer.
21359336Sdfr */
21369336Sdfrint
21379336Sdfrget_num(cp)
21389336Sdfr	register char *cp;
21399336Sdfr{
21409336Sdfr	register int res = 0;
21419336Sdfr
21429336Sdfr	while (*cp) {
21439336Sdfr		if (*cp < '0' || *cp > '9')
21449336Sdfr			return (-1);
21459336Sdfr		res = res * 10 + (*cp++ - '0');
21469336Sdfr	}
21479336Sdfr	return (res);
21489336Sdfr}
2149