mountd.c revision 8871
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
442999Swollman/*static char sccsid[] = "From: @(#)mountd.c	8.8 (Berkeley) 2/20/94";*/
452999Swollmanstatic const char rcsid[] =
468871Srgrimes	"$Id: mountd.c,v 1.6 1995/05/21 19:31:09 phk 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>
571558Srgrimes
581558Srgrimes#include <rpc/rpc.h>
591558Srgrimes#include <rpc/pmap_clnt.h>
601558Srgrimes#include <rpc/pmap_prot.h>
611558Srgrimes#ifdef ISO
621558Srgrimes#include <netiso/iso.h>
631558Srgrimes#endif
641558Srgrimes#include <nfs/rpcv2.h>
651558Srgrimes#include <nfs/nfsv2.h>
661558Srgrimes
671558Srgrimes#include <arpa/inet.h>
681558Srgrimes
691558Srgrimes#include <ctype.h>
701558Srgrimes#include <errno.h>
711558Srgrimes#include <grp.h>
721558Srgrimes#include <netdb.h>
731558Srgrimes#include <pwd.h>
741558Srgrimes#include <signal.h>
751558Srgrimes#include <stdio.h>
761558Srgrimes#include <stdlib.h>
771558Srgrimes#include <string.h>
781558Srgrimes#include <unistd.h>
791558Srgrimes#include "pathnames.h"
801558Srgrimes
811558Srgrimes#ifdef DEBUG
821558Srgrimes#include <stdarg.h>
831558Srgrimes#endif
841558Srgrimes
851558Srgrimes/*
861558Srgrimes * Structures for keeping the mount list and export list
871558Srgrimes */
881558Srgrimesstruct mountlist {
891558Srgrimes	struct mountlist *ml_next;
901558Srgrimes	char	ml_host[RPCMNT_NAMELEN+1];
911558Srgrimes	char	ml_dirp[RPCMNT_PATHLEN+1];
921558Srgrimes};
931558Srgrimes
941558Srgrimesstruct dirlist {
951558Srgrimes	struct dirlist	*dp_left;
961558Srgrimes	struct dirlist	*dp_right;
971558Srgrimes	int		dp_flag;
981558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
991558Srgrimes	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1001558Srgrimes};
1011558Srgrimes/* dp_flag bits */
1021558Srgrimes#define	DP_DEFSET	0x1
1031558Srgrimes
1041558Srgrimesstruct exportlist {
1051558Srgrimes	struct exportlist *ex_next;
1061558Srgrimes	struct dirlist	*ex_dirl;
1071558Srgrimes	struct dirlist	*ex_defdir;
1081558Srgrimes	int		ex_flag;
1091558Srgrimes	fsid_t		ex_fs;
1101558Srgrimes	char		*ex_fsdir;
1111558Srgrimes};
1121558Srgrimes/* ex_flag bits */
1131558Srgrimes#define	EX_LINKED	0x1
1141558Srgrimes
1151558Srgrimesstruct netmsk {
1161558Srgrimes	u_long	nt_net;
1171558Srgrimes	u_long	nt_mask;
1181558Srgrimes	char *nt_name;
1191558Srgrimes};
1201558Srgrimes
1211558Srgrimesunion grouptypes {
1221558Srgrimes	struct hostent *gt_hostent;
1231558Srgrimes	struct netmsk	gt_net;
1241558Srgrimes#ifdef ISO
1251558Srgrimes	struct sockaddr_iso *gt_isoaddr;
1261558Srgrimes#endif
1271558Srgrimes};
1281558Srgrimes
1291558Srgrimesstruct grouplist {
1301558Srgrimes	int gr_type;
1311558Srgrimes	union grouptypes gr_ptr;
1321558Srgrimes	struct grouplist *gr_next;
1331558Srgrimes};
1341558Srgrimes/* Group types */
1351558Srgrimes#define	GT_NULL		0x0
1361558Srgrimes#define	GT_HOST		0x1
1371558Srgrimes#define	GT_NET		0x2
1381558Srgrimes#define	GT_ISO		0x4
1397401Swpaul#define GT_IGNORE	0x5
1401558Srgrimes
1411558Srgrimesstruct hostlist {
1421558Srgrimes	struct grouplist *ht_grp;
1431558Srgrimes	struct hostlist	 *ht_next;
1441558Srgrimes};
1451558Srgrimes
1461558Srgrimes/* Global defs */
1471558Srgrimeschar	*add_expdir __P((struct dirlist **, char *, int));
1481558Srgrimesvoid	add_dlist __P((struct dirlist **, struct dirlist *,
1491558Srgrimes				struct grouplist *));
1501558Srgrimesvoid	add_mlist __P((char *, char *));
1511558Srgrimesint	check_dirpath __P((char *));
1521558Srgrimesint	check_options __P((struct dirlist *));
1531558Srgrimesint	chk_host __P((struct dirlist *, u_long, int *));
1541558Srgrimesvoid	del_mlist __P((char *, char *));
1551558Srgrimesstruct dirlist *dirp_search __P((struct dirlist *, char *));
1561558Srgrimesint	do_mount __P((struct exportlist *, struct grouplist *, int,
1571558Srgrimes				struct ucred *, char *, int, struct statfs *));
1581558Srgrimesint	do_opt __P((char **, char **, struct exportlist *, struct grouplist *,
1591558Srgrimes				int *, int *, struct ucred *));
1601558Srgrimesstruct	exportlist *ex_search __P((fsid_t *));
1611558Srgrimesstruct	exportlist *get_exp __P((void));
1621558Srgrimesvoid	free_dir __P((struct dirlist *));
1631558Srgrimesvoid	free_exp __P((struct exportlist *));
1641558Srgrimesvoid	free_grp __P((struct grouplist *));
1651558Srgrimesvoid	free_host __P((struct hostlist *));
1661558Srgrimesvoid	get_exportlist __P((void));
1677401Swpaulint	get_host __P((char *, struct grouplist *, struct grouplist *));
1681558Srgrimesstruct hostlist *get_ht __P((void));
1691558Srgrimesint	get_line __P((void));
1701558Srgrimesvoid	get_mountlist __P((void));
1711558Srgrimesint	get_net __P((char *, struct netmsk *, int));
1721558Srgrimesvoid	getexp_err __P((struct exportlist *, struct grouplist *));
1731558Srgrimesstruct grouplist *get_grp __P((void));
1741558Srgrimesvoid	hang_dirp __P((struct dirlist *, struct grouplist *,
1751558Srgrimes				struct exportlist *, int));
1761558Srgrimesvoid	mntsrv __P((struct svc_req *, SVCXPRT *));
1771558Srgrimesvoid	nextfield __P((char **, char **));
1781558Srgrimesvoid	out_of_mem __P((void));
1791558Srgrimesvoid	parsecred __P((char *, struct ucred *));
1801558Srgrimesint	put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
1811558Srgrimesint	scan_tree __P((struct dirlist *, u_long));
1821558Srgrimesvoid	send_umntall __P((void));
1831558Srgrimesint	umntall_each __P((caddr_t, struct sockaddr_in *));
1841558Srgrimesint	xdr_dir __P((XDR *, char *));
1851558Srgrimesint	xdr_explist __P((XDR *, caddr_t));
1861558Srgrimesint	xdr_fhs __P((XDR *, nfsv2fh_t *));
1871558Srgrimesint	xdr_mlist __P((XDR *, caddr_t));
1881558Srgrimes
1891558Srgrimes/* C library */
1901558Srgrimesint	getnetgrent();
1911558Srgrimesvoid	endnetgrent();
1921558Srgrimesvoid	setnetgrent();
1931558Srgrimes
1941558Srgrimes#ifdef ISO
1951558Srgrimesstruct iso_addr *iso_addr();
1961558Srgrimes#endif
1971558Srgrimes
1981558Srgrimesstruct exportlist *exphead;
1991558Srgrimesstruct mountlist *mlhead;
2001558Srgrimesstruct grouplist *grphead;
2011558Srgrimeschar exname[MAXPATHLEN];
2021558Srgrimesstruct ucred def_anon = {
2031558Srgrimes	1,
2041558Srgrimes	(uid_t) -2,
2051558Srgrimes	1,
2061558Srgrimes	{ (gid_t) -2 }
2071558Srgrimes};
2081558Srgrimesint root_only = 1;
2091558Srgrimesint opt_flags;
2101558Srgrimes/* Bits for above */
2111558Srgrimes#define	OP_MAPROOT	0x01
2121558Srgrimes#define	OP_MAPALL	0x02
2131558Srgrimes#define	OP_KERB		0x04
2141558Srgrimes#define	OP_MASK		0x08
2151558Srgrimes#define	OP_NET		0x10
2161558Srgrimes#define	OP_ISO		0x20
2171558Srgrimes#define	OP_ALLDIRS	0x40
2181558Srgrimes
2191558Srgrimes#ifdef DEBUG
2201558Srgrimesint debug = 1;
2211558Srgrimesvoid	SYSLOG __P((int, const char *, ...));
2221558Srgrimes#define syslog SYSLOG
2231558Srgrimes#else
2241558Srgrimesint debug = 0;
2251558Srgrimes#endif
2261558Srgrimes
2271558Srgrimes/*
2281558Srgrimes * Mountd server for NFS mount protocol as described in:
2291558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2301558Srgrimes * The optional arguments are the exports file name
2311558Srgrimes * default: _PATH_EXPORTS
2321558Srgrimes * and "-n" to allow nonroot mount.
2331558Srgrimes */
2341558Srgrimesint
2351558Srgrimesmain(argc, argv)
2361558Srgrimes	int argc;
2371558Srgrimes	char **argv;
2381558Srgrimes{
2391558Srgrimes	SVCXPRT *transp;
2401558Srgrimes	int c;
2412999Swollman	struct vfsconf *vfc;
2421558Srgrimes
2432999Swollman	vfc = getvfsbyname("nfs");
2442999Swollman	if(!vfc && vfsisloadable("nfs")) {
2452999Swollman		if(vfsload("nfs"))
2462999Swollman			err(1, "vfsload(nfs)");
2472999Swollman		endvfsent();	/* flush cache */
2482999Swollman		vfc = getvfsbyname("nfs");
2492999Swollman	}
2502999Swollman	if(!vfc) {
2512999Swollman		errx(1, "NFS support is not available in the running kernel");
2522999Swollman	}
2532999Swollman
2548688Sphk	while ((c = getopt(argc, argv, "dn")) != EOF)
2551558Srgrimes		switch (c) {
2568688Sphk		case 'd':
2578688Sphk			debug = debug ? 0 : 1;
2588688Sphk			break;
2591558Srgrimes		case 'n':
2601558Srgrimes			root_only = 0;
2611558Srgrimes			break;
2621558Srgrimes		default:
2631558Srgrimes			fprintf(stderr, "Usage: mountd [-n] [export_file]\n");
2641558Srgrimes			exit(1);
2651558Srgrimes		};
2661558Srgrimes	argc -= optind;
2671558Srgrimes	argv += optind;
2681558Srgrimes	grphead = (struct grouplist *)NULL;
2691558Srgrimes	exphead = (struct exportlist *)NULL;
2701558Srgrimes	mlhead = (struct mountlist *)NULL;
2711558Srgrimes	if (argc == 1) {
2721558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
2731558Srgrimes		exname[MAXPATHLEN-1] = '\0';
2741558Srgrimes	} else
2751558Srgrimes		strcpy(exname, _PATH_EXPORTS);
2761558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
2771558Srgrimes	if (debug)
2781558Srgrimes		fprintf(stderr,"Getting export list.\n");
2791558Srgrimes	get_exportlist();
2801558Srgrimes	if (debug)
2811558Srgrimes		fprintf(stderr,"Getting mount list.\n");
2821558Srgrimes	get_mountlist();
2831558Srgrimes	if (debug)
2841558Srgrimes		fprintf(stderr,"Here we go.\n");
2851558Srgrimes	if (debug == 0) {
2861558Srgrimes		daemon(0, 0);
2871558Srgrimes		signal(SIGINT, SIG_IGN);
2881558Srgrimes		signal(SIGQUIT, SIG_IGN);
2891558Srgrimes	}
2901558Srgrimes	signal(SIGHUP, (void (*) __P((int))) get_exportlist);
2911558Srgrimes	signal(SIGTERM, (void (*) __P((int))) send_umntall);
2921558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
2931558Srgrimes	  if (pidfile != NULL) {
2941558Srgrimes		fprintf(pidfile, "%d\n", getpid());
2951558Srgrimes		fclose(pidfile);
2961558Srgrimes	  }
2971558Srgrimes	}
2981558Srgrimes	if ((transp = svcudp_create(RPC_ANYSOCK)) == NULL) {
2991558Srgrimes		syslog(LOG_ERR, "Can't create socket");
3001558Srgrimes		exit(1);
3011558Srgrimes	}
3021558Srgrimes	pmap_unset(RPCPROG_MNT, RPCMNT_VER1);
3031558Srgrimes	if (!svc_register(transp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
3041558Srgrimes	    IPPROTO_UDP)) {
3051558Srgrimes		syslog(LOG_ERR, "Can't register mount");
3061558Srgrimes		exit(1);
3071558Srgrimes	}
3081558Srgrimes	svc_run();
3091558Srgrimes	syslog(LOG_ERR, "Mountd died");
3101558Srgrimes	exit(1);
3111558Srgrimes}
3121558Srgrimes
3131558Srgrimes/*
3141558Srgrimes * The mount rpc service
3151558Srgrimes */
3161558Srgrimesvoid
3171558Srgrimesmntsrv(rqstp, transp)
3181558Srgrimes	struct svc_req *rqstp;
3191558Srgrimes	SVCXPRT *transp;
3201558Srgrimes{
3211558Srgrimes	struct exportlist *ep;
3221558Srgrimes	struct dirlist *dp;
3231558Srgrimes	nfsv2fh_t nfh;
3241558Srgrimes	struct authunix_parms *ucr;
3251558Srgrimes	struct stat stb;
3261558Srgrimes	struct statfs fsb;
3271558Srgrimes	struct hostent *hp;
3281558Srgrimes	u_long saddr;
3291558Srgrimes	char rpcpath[RPCMNT_PATHLEN+1], dirpath[MAXPATHLEN];
3301558Srgrimes	int bad = ENOENT, omask, defset;
3311558Srgrimes	uid_t uid = -2;
3321558Srgrimes
3331558Srgrimes	/* Get authorization */
3341558Srgrimes	switch (rqstp->rq_cred.oa_flavor) {
3351558Srgrimes	case AUTH_UNIX:
3361558Srgrimes		ucr = (struct authunix_parms *)rqstp->rq_clntcred;
3371558Srgrimes		uid = ucr->aup_uid;
3381558Srgrimes		break;
3391558Srgrimes	case AUTH_NULL:
3401558Srgrimes	default:
3411558Srgrimes		break;
3421558Srgrimes	}
3431558Srgrimes
3441558Srgrimes	saddr = transp->xp_raddr.sin_addr.s_addr;
3451558Srgrimes	hp = (struct hostent *)NULL;
3461558Srgrimes	switch (rqstp->rq_proc) {
3471558Srgrimes	case NULLPROC:
3481558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
3491558Srgrimes			syslog(LOG_ERR, "Can't send reply");
3501558Srgrimes		return;
3511558Srgrimes	case RPCMNT_MOUNT:
3521558Srgrimes		if ((uid != 0 && root_only) || uid == -2) {
3531558Srgrimes			svcerr_weakauth(transp);
3541558Srgrimes			return;
3551558Srgrimes		}
3561558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
3571558Srgrimes			svcerr_decode(transp);
3581558Srgrimes			return;
3591558Srgrimes		}
3601558Srgrimes
3611558Srgrimes		/*
3621558Srgrimes		 * Get the real pathname and make sure it is a directory
3631558Srgrimes		 * that exists.
3641558Srgrimes		 */
3651558Srgrimes		if (realpath(rpcpath, dirpath) == 0 ||
3661558Srgrimes		    stat(dirpath, &stb) < 0 ||
3671558Srgrimes		    (stb.st_mode & S_IFMT) != S_IFDIR ||
3681558Srgrimes		    statfs(dirpath, &fsb) < 0) {
3691558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
3701558Srgrimes			if (debug)
3711558Srgrimes				fprintf(stderr, "stat failed on %s\n", dirpath);
3721558Srgrimes			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
3731558Srgrimes				syslog(LOG_ERR, "Can't send reply");
3741558Srgrimes			return;
3751558Srgrimes		}
3761558Srgrimes
3771558Srgrimes		/* Check in the exports list */
3781558Srgrimes		omask = sigblock(sigmask(SIGHUP));
3791558Srgrimes		ep = ex_search(&fsb.f_fsid);
3801558Srgrimes		defset = 0;
3811558Srgrimes		if (ep && (chk_host(ep->ex_defdir, saddr, &defset) ||
3821558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
3831558Srgrimes		     chk_host(dp, saddr, &defset)) ||
3841558Srgrimes		     (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
3851558Srgrimes		      scan_tree(ep->ex_dirl, saddr) == 0))) {
3861558Srgrimes			/* Get the file handle */
3871558Srgrimes			bzero((caddr_t)&nfh, sizeof(nfh));
3881558Srgrimes			if (getfh(dirpath, (fhandle_t *)&nfh) < 0) {
3891558Srgrimes				bad = errno;
3901558Srgrimes				syslog(LOG_ERR, "Can't get fh for %s", dirpath);
3911558Srgrimes				if (!svc_sendreply(transp, xdr_long,
3921558Srgrimes				    (caddr_t)&bad))
3931558Srgrimes					syslog(LOG_ERR, "Can't send reply");
3941558Srgrimes				sigsetmask(omask);
3951558Srgrimes				return;
3961558Srgrimes			}
3971558Srgrimes			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&nfh))
3981558Srgrimes				syslog(LOG_ERR, "Can't send reply");
3991558Srgrimes			if (hp == NULL)
4001558Srgrimes				hp = gethostbyaddr((caddr_t)&saddr,
4011558Srgrimes				    sizeof(saddr), AF_INET);
4021558Srgrimes			if (hp)
4031558Srgrimes				add_mlist(hp->h_name, dirpath);
4041558Srgrimes			else
4051558Srgrimes				add_mlist(inet_ntoa(transp->xp_raddr.sin_addr),
4061558Srgrimes					dirpath);
4071558Srgrimes			if (debug)
4081558Srgrimes				fprintf(stderr,"Mount successfull.\n");
4091558Srgrimes		} else {
4101558Srgrimes			bad = EACCES;
4111558Srgrimes			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
4121558Srgrimes				syslog(LOG_ERR, "Can't send reply");
4131558Srgrimes		}
4141558Srgrimes		sigsetmask(omask);
4151558Srgrimes		return;
4161558Srgrimes	case RPCMNT_DUMP:
4171558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
4181558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4191558Srgrimes		return;
4201558Srgrimes	case RPCMNT_UMOUNT:
4211558Srgrimes		if ((uid != 0 && root_only) || uid == -2) {
4221558Srgrimes			svcerr_weakauth(transp);
4231558Srgrimes			return;
4241558Srgrimes		}
4251558Srgrimes		if (!svc_getargs(transp, xdr_dir, dirpath)) {
4261558Srgrimes			svcerr_decode(transp);
4271558Srgrimes			return;
4281558Srgrimes		}
4291558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
4301558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4311558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
4321558Srgrimes		if (hp)
4331558Srgrimes			del_mlist(hp->h_name, dirpath);
4341558Srgrimes		del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), dirpath);
4351558Srgrimes		return;
4361558Srgrimes	case RPCMNT_UMNTALL:
4371558Srgrimes		if ((uid != 0 && root_only) || uid == -2) {
4381558Srgrimes			svcerr_weakauth(transp);
4391558Srgrimes			return;
4401558Srgrimes		}
4411558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
4421558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4431558Srgrimes		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
4441558Srgrimes		if (hp)
4451558Srgrimes			del_mlist(hp->h_name, (char *)NULL);
4461558Srgrimes		del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), (char *)NULL);
4471558Srgrimes		return;
4481558Srgrimes	case RPCMNT_EXPORT:
4491558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
4501558Srgrimes			syslog(LOG_ERR, "Can't send reply");
4511558Srgrimes		return;
4521558Srgrimes	default:
4531558Srgrimes		svcerr_noproc(transp);
4541558Srgrimes		return;
4551558Srgrimes	}
4561558Srgrimes}
4571558Srgrimes
4581558Srgrimes/*
4591558Srgrimes * Xdr conversion for a dirpath string
4601558Srgrimes */
4611558Srgrimesint
4621558Srgrimesxdr_dir(xdrsp, dirp)
4631558Srgrimes	XDR *xdrsp;
4641558Srgrimes	char *dirp;
4651558Srgrimes{
4661558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
4671558Srgrimes}
4681558Srgrimes
4691558Srgrimes/*
4701558Srgrimes * Xdr routine to generate fhstatus
4711558Srgrimes */
4721558Srgrimesint
4731558Srgrimesxdr_fhs(xdrsp, nfh)
4741558Srgrimes	XDR *xdrsp;
4751558Srgrimes	nfsv2fh_t *nfh;
4761558Srgrimes{
4777401Swpaul	u_long ok = 0;
4781558Srgrimes
4791558Srgrimes	if (!xdr_long(xdrsp, &ok))
4801558Srgrimes		return (0);
4811558Srgrimes	return (xdr_opaque(xdrsp, (caddr_t)nfh, NFSX_FH));
4821558Srgrimes}
4831558Srgrimes
4841558Srgrimesint
4851558Srgrimesxdr_mlist(xdrsp, cp)
4861558Srgrimes	XDR *xdrsp;
4871558Srgrimes	caddr_t cp;
4881558Srgrimes{
4891558Srgrimes	struct mountlist *mlp;
4901558Srgrimes	int true = 1;
4911558Srgrimes	int false = 0;
4921558Srgrimes	char *strp;
4931558Srgrimes
4941558Srgrimes	mlp = mlhead;
4951558Srgrimes	while (mlp) {
4961558Srgrimes		if (!xdr_bool(xdrsp, &true))
4971558Srgrimes			return (0);
4981558Srgrimes		strp = &mlp->ml_host[0];
4991558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
5001558Srgrimes			return (0);
5011558Srgrimes		strp = &mlp->ml_dirp[0];
5021558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
5031558Srgrimes			return (0);
5041558Srgrimes		mlp = mlp->ml_next;
5051558Srgrimes	}
5061558Srgrimes	if (!xdr_bool(xdrsp, &false))
5071558Srgrimes		return (0);
5081558Srgrimes	return (1);
5091558Srgrimes}
5101558Srgrimes
5111558Srgrimes/*
5121558Srgrimes * Xdr conversion for export list
5131558Srgrimes */
5141558Srgrimesint
5151558Srgrimesxdr_explist(xdrsp, cp)
5161558Srgrimes	XDR *xdrsp;
5171558Srgrimes	caddr_t cp;
5181558Srgrimes{
5191558Srgrimes	struct exportlist *ep;
5201558Srgrimes	int false = 0;
5211558Srgrimes	int omask, putdef;
5221558Srgrimes
5231558Srgrimes	omask = sigblock(sigmask(SIGHUP));
5241558Srgrimes	ep = exphead;
5251558Srgrimes	while (ep) {
5261558Srgrimes		putdef = 0;
5271558Srgrimes		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
5281558Srgrimes			goto errout;
5291558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
5301558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
5311558Srgrimes			&putdef))
5321558Srgrimes			goto errout;
5331558Srgrimes		ep = ep->ex_next;
5341558Srgrimes	}
5351558Srgrimes	sigsetmask(omask);
5361558Srgrimes	if (!xdr_bool(xdrsp, &false))
5371558Srgrimes		return (0);
5381558Srgrimes	return (1);
5391558Srgrimeserrout:
5401558Srgrimes	sigsetmask(omask);
5411558Srgrimes	return (0);
5421558Srgrimes}
5431558Srgrimes
5441558Srgrimes/*
5451558Srgrimes * Called from xdr_explist() to traverse the tree and export the
5461558Srgrimes * directory paths.
5471558Srgrimes */
5481558Srgrimesint
5491558Srgrimesput_exlist(dp, xdrsp, adp, putdefp)
5501558Srgrimes	struct dirlist *dp;
5511558Srgrimes	XDR *xdrsp;
5521558Srgrimes	struct dirlist *adp;
5531558Srgrimes	int *putdefp;
5541558Srgrimes{
5551558Srgrimes	struct grouplist *grp;
5561558Srgrimes	struct hostlist *hp;
5571558Srgrimes	int true = 1;
5581558Srgrimes	int false = 0;
5591558Srgrimes	int gotalldir = 0;
5601558Srgrimes	char *strp;
5611558Srgrimes
5621558Srgrimes	if (dp) {
5631558Srgrimes		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
5641558Srgrimes			return (1);
5651558Srgrimes		if (!xdr_bool(xdrsp, &true))
5661558Srgrimes			return (1);
5671558Srgrimes		strp = dp->dp_dirp;
5681558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
5691558Srgrimes			return (1);
5701558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
5711558Srgrimes			gotalldir = 1;
5721558Srgrimes			*putdefp = 1;
5731558Srgrimes		}
5741558Srgrimes		if ((dp->dp_flag & DP_DEFSET) == 0 &&
5751558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
5761558Srgrimes			hp = dp->dp_hosts;
5771558Srgrimes			while (hp) {
5781558Srgrimes				grp = hp->ht_grp;
5791558Srgrimes				if (grp->gr_type == GT_HOST) {
5801558Srgrimes					if (!xdr_bool(xdrsp, &true))
5811558Srgrimes						return (1);
5821558Srgrimes					strp = grp->gr_ptr.gt_hostent->h_name;
5838871Srgrimes					if (!xdr_string(xdrsp, &strp,
5841558Srgrimes					    RPCMNT_NAMELEN))
5851558Srgrimes						return (1);
5861558Srgrimes				} else if (grp->gr_type == GT_NET) {
5871558Srgrimes					if (!xdr_bool(xdrsp, &true))
5881558Srgrimes						return (1);
5891558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
5908871Srgrimes					if (!xdr_string(xdrsp, &strp,
5911558Srgrimes					    RPCMNT_NAMELEN))
5921558Srgrimes						return (1);
5931558Srgrimes				}
5941558Srgrimes				hp = hp->ht_next;
5951558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
5961558Srgrimes					hp = adp->dp_hosts;
5971558Srgrimes					gotalldir = 0;
5981558Srgrimes				}
5991558Srgrimes			}
6001558Srgrimes		}
6011558Srgrimes		if (!xdr_bool(xdrsp, &false))
6021558Srgrimes			return (1);
6031558Srgrimes		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
6041558Srgrimes			return (1);
6051558Srgrimes	}
6061558Srgrimes	return (0);
6071558Srgrimes}
6081558Srgrimes
6091558Srgrimes#define LINESIZ	10240
6101558Srgrimeschar line[LINESIZ];
6111558SrgrimesFILE *exp_file;
6121558Srgrimes
6131558Srgrimes/*
6141558Srgrimes * Get the export list
6151558Srgrimes */
6161558Srgrimesvoid
6171558Srgrimesget_exportlist()
6181558Srgrimes{
6191558Srgrimes	struct exportlist *ep, *ep2;
6201558Srgrimes	struct grouplist *grp, *tgrp;
6211558Srgrimes	struct exportlist **epp;
6221558Srgrimes	struct dirlist *dirhead;
6231558Srgrimes	struct statfs fsb, *fsp;
6241558Srgrimes	struct hostent *hpe;
6251558Srgrimes	struct ucred anon;
6261558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
6271558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
6281558Srgrimes
6291558Srgrimes	/*
6301558Srgrimes	 * First, get rid of the old list
6311558Srgrimes	 */
6321558Srgrimes	ep = exphead;
6331558Srgrimes	while (ep) {
6341558Srgrimes		ep2 = ep;
6351558Srgrimes		ep = ep->ex_next;
6361558Srgrimes		free_exp(ep2);
6371558Srgrimes	}
6381558Srgrimes	exphead = (struct exportlist *)NULL;
6391558Srgrimes
6401558Srgrimes	grp = grphead;
6411558Srgrimes	while (grp) {
6421558Srgrimes		tgrp = grp;
6431558Srgrimes		grp = grp->gr_next;
6441558Srgrimes		free_grp(tgrp);
6451558Srgrimes	}
6461558Srgrimes	grphead = (struct grouplist *)NULL;
6471558Srgrimes
6481558Srgrimes	/*
6491558Srgrimes	 * And delete exports that are in the kernel for all local
6501558Srgrimes	 * file systems.
6511558Srgrimes	 * XXX: Should know how to handle all local exportable file systems
6521558Srgrimes	 *      instead of just MOUNT_UFS.
6531558Srgrimes	 */
6541558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
6551558Srgrimes	for (i = 0; i < num; i++) {
6561558Srgrimes		union {
6571558Srgrimes			struct ufs_args ua;
6581558Srgrimes			struct iso_args ia;
6591558Srgrimes			struct mfs_args ma;
6601558Srgrimes		} targs;
6611558Srgrimes
6621558Srgrimes		switch (fsp->f_type) {
6631558Srgrimes		case MOUNT_MFS:
6641558Srgrimes		case MOUNT_UFS:
6651558Srgrimes		case MOUNT_CD9660:
6662898Sdfr		case MOUNT_MSDOS:
6671558Srgrimes			targs.ua.fspec = NULL;
6681558Srgrimes			targs.ua.export.ex_flags = MNT_DELEXPORT;
6691558Srgrimes			if (mount(fsp->f_type, fsp->f_mntonname,
6701558Srgrimes				  fsp->f_flags | MNT_UPDATE,
6711558Srgrimes				  (caddr_t)&targs) < 0)
6721558Srgrimes				syslog(LOG_ERR, "Can't delete exports for %s",
6731558Srgrimes				       fsp->f_mntonname);
6741558Srgrimes		}
6751558Srgrimes		fsp++;
6761558Srgrimes	}
6771558Srgrimes
6781558Srgrimes	/*
6791558Srgrimes	 * Read in the exports file and build the list, calling
6801558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
6811558Srgrimes	 */
6821558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
6831558Srgrimes		syslog(LOG_ERR, "Can't open %s", exname);
6841558Srgrimes		exit(2);
6851558Srgrimes	}
6861558Srgrimes	dirhead = (struct dirlist *)NULL;
6871558Srgrimes	while (get_line()) {
6881558Srgrimes		if (debug)
6891558Srgrimes			fprintf(stderr,"Got line %s\n",line);
6901558Srgrimes		cp = line;
6911558Srgrimes		nextfield(&cp, &endcp);
6921558Srgrimes		if (*cp == '#')
6931558Srgrimes			goto nextline;
6941558Srgrimes
6951558Srgrimes		/*
6961558Srgrimes		 * Set defaults.
6971558Srgrimes		 */
6981558Srgrimes		has_host = FALSE;
6991558Srgrimes		anon = def_anon;
7001558Srgrimes		exflags = MNT_EXPORTED;
7011558Srgrimes		got_nondir = 0;
7021558Srgrimes		opt_flags = 0;
7031558Srgrimes		ep = (struct exportlist *)NULL;
7041558Srgrimes
7051558Srgrimes		/*
7061558Srgrimes		 * Create new exports list entry
7071558Srgrimes		 */
7081558Srgrimes		len = endcp-cp;
7091558Srgrimes		tgrp = grp = get_grp();
7101558Srgrimes		while (len > 0) {
7111558Srgrimes			if (len > RPCMNT_NAMELEN) {
7121558Srgrimes			    getexp_err(ep, tgrp);
7131558Srgrimes			    goto nextline;
7141558Srgrimes			}
7151558Srgrimes			if (*cp == '-') {
7161558Srgrimes			    if (ep == (struct exportlist *)NULL) {
7171558Srgrimes				getexp_err(ep, tgrp);
7181558Srgrimes				goto nextline;
7191558Srgrimes			    }
7201558Srgrimes			    if (debug)
7211558Srgrimes				fprintf(stderr, "doing opt %s\n", cp);
7221558Srgrimes			    got_nondir = 1;
7231558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
7241558Srgrimes				&exflags, &anon)) {
7251558Srgrimes				getexp_err(ep, tgrp);
7261558Srgrimes				goto nextline;
7271558Srgrimes			    }
7281558Srgrimes			} else if (*cp == '/') {
7291558Srgrimes			    savedc = *endcp;
7301558Srgrimes			    *endcp = '\0';
7311558Srgrimes			    if (check_dirpath(cp) &&
7321558Srgrimes				statfs(cp, &fsb) >= 0) {
7331558Srgrimes				if (got_nondir) {
7341558Srgrimes				    syslog(LOG_ERR, "Dirs must be first");
7351558Srgrimes				    getexp_err(ep, tgrp);
7361558Srgrimes				    goto nextline;
7371558Srgrimes				}
7381558Srgrimes				if (ep) {
7391558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
7401558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
7411558Srgrimes					getexp_err(ep, tgrp);
7421558Srgrimes					goto nextline;
7431558Srgrimes				    }
7441558Srgrimes				} else {
7451558Srgrimes				    /*
7461558Srgrimes				     * See if this directory is already
7471558Srgrimes				     * in the list.
7481558Srgrimes				     */
7491558Srgrimes				    ep = ex_search(&fsb.f_fsid);
7501558Srgrimes				    if (ep == (struct exportlist *)NULL) {
7511558Srgrimes					ep = get_exp();
7521558Srgrimes					ep->ex_fs = fsb.f_fsid;
7531558Srgrimes					ep->ex_fsdir = (char *)
7541558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
7551558Srgrimes					if (ep->ex_fsdir)
7561558Srgrimes					    strcpy(ep->ex_fsdir,
7571558Srgrimes						fsb.f_mntonname);
7581558Srgrimes					else
7591558Srgrimes					    out_of_mem();
7601558Srgrimes					if (debug)
7611558Srgrimes					  fprintf(stderr,
7621558Srgrimes					      "Making new ep fs=0x%x,0x%x\n",
7631558Srgrimes					      fsb.f_fsid.val[0],
7641558Srgrimes					      fsb.f_fsid.val[1]);
7651558Srgrimes				    } else if (debug)
7661558Srgrimes					fprintf(stderr,
7671558Srgrimes					    "Found ep fs=0x%x,0x%x\n",
7681558Srgrimes					    fsb.f_fsid.val[0],
7691558Srgrimes					    fsb.f_fsid.val[1]);
7701558Srgrimes				}
7711558Srgrimes
7721558Srgrimes				/*
7731558Srgrimes				 * Add dirpath to export mount point.
7741558Srgrimes				 */
7751558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
7761558Srgrimes				dirplen = len;
7771558Srgrimes			    } else {
7781558Srgrimes				getexp_err(ep, tgrp);
7791558Srgrimes				goto nextline;
7801558Srgrimes			    }
7811558Srgrimes			    *endcp = savedc;
7821558Srgrimes			} else {
7831558Srgrimes			    savedc = *endcp;
7841558Srgrimes			    *endcp = '\0';
7851558Srgrimes			    got_nondir = 1;
7861558Srgrimes			    if (ep == (struct exportlist *)NULL) {
7871558Srgrimes				getexp_err(ep, tgrp);
7881558Srgrimes				goto nextline;
7891558Srgrimes			    }
7901558Srgrimes
7911558Srgrimes			    /*
7921558Srgrimes			     * Get the host or netgroup.
7931558Srgrimes			     */
7941558Srgrimes			    setnetgrent(cp);
7951558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
7961558Srgrimes			    do {
7971558Srgrimes				if (has_host) {
7981558Srgrimes				    grp->gr_next = get_grp();
7991558Srgrimes				    grp = grp->gr_next;
8001558Srgrimes				}
8011558Srgrimes				if (netgrp) {
8027401Swpaul				    if (get_host(hst, grp, tgrp)) {
8031558Srgrimes					syslog(LOG_ERR, "Bad netgroup %s", cp);
8041558Srgrimes					getexp_err(ep, tgrp);
8051558Srgrimes					goto nextline;
8061558Srgrimes				    }
8077401Swpaul				} else if (get_host(cp, grp, tgrp)) {
8081558Srgrimes				    getexp_err(ep, tgrp);
8091558Srgrimes				    goto nextline;
8101558Srgrimes				}
8111558Srgrimes				has_host = TRUE;
8121558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
8131558Srgrimes			    endnetgrent();
8141558Srgrimes			    *endcp = savedc;
8151558Srgrimes			}
8161558Srgrimes			cp = endcp;
8171558Srgrimes			nextfield(&cp, &endcp);
8181558Srgrimes			len = endcp - cp;
8191558Srgrimes		}
8201558Srgrimes		if (check_options(dirhead)) {
8211558Srgrimes			getexp_err(ep, tgrp);
8221558Srgrimes			goto nextline;
8231558Srgrimes		}
8241558Srgrimes		if (!has_host) {
8251558Srgrimes			grp->gr_type = GT_HOST;
8261558Srgrimes			if (debug)
8271558Srgrimes				fprintf(stderr,"Adding a default entry\n");
8281558Srgrimes			/* add a default group and make the grp list NULL */
8291558Srgrimes			hpe = (struct hostent *)malloc(sizeof(struct hostent));
8301558Srgrimes			if (hpe == (struct hostent *)NULL)
8311558Srgrimes				out_of_mem();
8321558Srgrimes			hpe->h_name = "Default";
8331558Srgrimes			hpe->h_addrtype = AF_INET;
8341558Srgrimes			hpe->h_length = sizeof (u_long);
8351558Srgrimes			hpe->h_addr_list = (char **)NULL;
8361558Srgrimes			grp->gr_ptr.gt_hostent = hpe;
8371558Srgrimes
8381558Srgrimes		/*
8391558Srgrimes		 * Don't allow a network export coincide with a list of
8401558Srgrimes		 * host(s) on the same line.
8411558Srgrimes		 */
8421558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
8431558Srgrimes			getexp_err(ep, tgrp);
8441558Srgrimes			goto nextline;
8451558Srgrimes		}
8461558Srgrimes
8471558Srgrimes		/*
8481558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
8491558Srgrimes		 * After loop, tgrp points to the start of the list and
8501558Srgrimes		 * grp points to the last entry in the list.
8511558Srgrimes		 */
8521558Srgrimes		grp = tgrp;
8531558Srgrimes		do {
8541558Srgrimes		    if (do_mount(ep, grp, exflags, &anon, dirp,
8551558Srgrimes			dirplen, &fsb)) {
8561558Srgrimes			getexp_err(ep, tgrp);
8571558Srgrimes			goto nextline;
8581558Srgrimes		    }
8591558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
8601558Srgrimes
8611558Srgrimes		/*
8621558Srgrimes		 * Success. Update the data structures.
8631558Srgrimes		 */
8641558Srgrimes		if (has_host) {
8651558Srgrimes			hang_dirp(dirhead, tgrp, ep, (opt_flags & OP_ALLDIRS));
8661558Srgrimes			grp->gr_next = grphead;
8671558Srgrimes			grphead = tgrp;
8681558Srgrimes		} else {
8691558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
8701558Srgrimes			(opt_flags & OP_ALLDIRS));
8711558Srgrimes			free_grp(grp);
8721558Srgrimes		}
8731558Srgrimes		dirhead = (struct dirlist *)NULL;
8741558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
8751558Srgrimes			ep2 = exphead;
8761558Srgrimes			epp = &exphead;
8771558Srgrimes
8781558Srgrimes			/*
8791558Srgrimes			 * Insert in the list in alphabetical order.
8801558Srgrimes			 */
8811558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
8821558Srgrimes				epp = &ep2->ex_next;
8831558Srgrimes				ep2 = ep2->ex_next;
8841558Srgrimes			}
8851558Srgrimes			if (ep2)
8861558Srgrimes				ep->ex_next = ep2;
8871558Srgrimes			*epp = ep;
8881558Srgrimes			ep->ex_flag |= EX_LINKED;
8891558Srgrimes		}
8901558Srgrimesnextline:
8911558Srgrimes		if (dirhead) {
8921558Srgrimes			free_dir(dirhead);
8931558Srgrimes			dirhead = (struct dirlist *)NULL;
8941558Srgrimes		}
8951558Srgrimes	}
8961558Srgrimes	fclose(exp_file);
8971558Srgrimes}
8981558Srgrimes
8991558Srgrimes/*
9001558Srgrimes * Allocate an export list element
9011558Srgrimes */
9021558Srgrimesstruct exportlist *
9031558Srgrimesget_exp()
9041558Srgrimes{
9051558Srgrimes	struct exportlist *ep;
9061558Srgrimes
9071558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
9081558Srgrimes	if (ep == (struct exportlist *)NULL)
9091558Srgrimes		out_of_mem();
9101558Srgrimes	bzero((caddr_t)ep, sizeof (struct exportlist));
9111558Srgrimes	return (ep);
9121558Srgrimes}
9131558Srgrimes
9141558Srgrimes/*
9151558Srgrimes * Allocate a group list element
9161558Srgrimes */
9171558Srgrimesstruct grouplist *
9181558Srgrimesget_grp()
9191558Srgrimes{
9201558Srgrimes	struct grouplist *gp;
9211558Srgrimes
9221558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
9231558Srgrimes	if (gp == (struct grouplist *)NULL)
9241558Srgrimes		out_of_mem();
9251558Srgrimes	bzero((caddr_t)gp, sizeof (struct grouplist));
9261558Srgrimes	return (gp);
9271558Srgrimes}
9281558Srgrimes
9291558Srgrimes/*
9301558Srgrimes * Clean up upon an error in get_exportlist().
9311558Srgrimes */
9321558Srgrimesvoid
9331558Srgrimesgetexp_err(ep, grp)
9341558Srgrimes	struct exportlist *ep;
9351558Srgrimes	struct grouplist *grp;
9361558Srgrimes{
9371558Srgrimes	struct grouplist *tgrp;
9381558Srgrimes
9391558Srgrimes	syslog(LOG_ERR, "Bad exports list line %s", line);
9401558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
9411558Srgrimes		free_exp(ep);
9421558Srgrimes	while (grp) {
9431558Srgrimes		tgrp = grp;
9441558Srgrimes		grp = grp->gr_next;
9451558Srgrimes		free_grp(tgrp);
9461558Srgrimes	}
9471558Srgrimes}
9481558Srgrimes
9491558Srgrimes/*
9501558Srgrimes * Search the export list for a matching fs.
9511558Srgrimes */
9521558Srgrimesstruct exportlist *
9531558Srgrimesex_search(fsid)
9541558Srgrimes	fsid_t *fsid;
9551558Srgrimes{
9561558Srgrimes	struct exportlist *ep;
9571558Srgrimes
9581558Srgrimes	ep = exphead;
9591558Srgrimes	while (ep) {
9601558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
9611558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
9621558Srgrimes			return (ep);
9631558Srgrimes		ep = ep->ex_next;
9641558Srgrimes	}
9651558Srgrimes	return (ep);
9661558Srgrimes}
9671558Srgrimes
9681558Srgrimes/*
9691558Srgrimes * Add a directory path to the list.
9701558Srgrimes */
9711558Srgrimeschar *
9721558Srgrimesadd_expdir(dpp, cp, len)
9731558Srgrimes	struct dirlist **dpp;
9741558Srgrimes	char *cp;
9751558Srgrimes	int len;
9761558Srgrimes{
9771558Srgrimes	struct dirlist *dp;
9781558Srgrimes
9791558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
9801558Srgrimes	dp->dp_left = *dpp;
9811558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
9821558Srgrimes	dp->dp_flag = 0;
9831558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
9841558Srgrimes	strcpy(dp->dp_dirp, cp);
9851558Srgrimes	*dpp = dp;
9861558Srgrimes	return (dp->dp_dirp);
9871558Srgrimes}
9881558Srgrimes
9891558Srgrimes/*
9901558Srgrimes * Hang the dir list element off the dirpath binary tree as required
9911558Srgrimes * and update the entry for host.
9921558Srgrimes */
9931558Srgrimesvoid
9941558Srgrimeshang_dirp(dp, grp, ep, alldirs)
9951558Srgrimes	struct dirlist *dp;
9961558Srgrimes	struct grouplist *grp;
9971558Srgrimes	struct exportlist *ep;
9981558Srgrimes	int alldirs;
9991558Srgrimes{
10001558Srgrimes	struct hostlist *hp;
10011558Srgrimes	struct dirlist *dp2;
10021558Srgrimes
10031558Srgrimes	if (alldirs) {
10041558Srgrimes		if (ep->ex_defdir)
10051558Srgrimes			free((caddr_t)dp);
10061558Srgrimes		else
10071558Srgrimes			ep->ex_defdir = dp;
10081558Srgrimes		if (grp == (struct grouplist *)NULL)
10091558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
10101558Srgrimes		else while (grp) {
10111558Srgrimes			hp = get_ht();
10121558Srgrimes			hp->ht_grp = grp;
10131558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
10141558Srgrimes			ep->ex_defdir->dp_hosts = hp;
10151558Srgrimes			grp = grp->gr_next;
10161558Srgrimes		}
10171558Srgrimes	} else {
10181558Srgrimes
10191558Srgrimes		/*
10201558Srgrimes		 * Loop throught the directories adding them to the tree.
10211558Srgrimes		 */
10221558Srgrimes		while (dp) {
10231558Srgrimes			dp2 = dp->dp_left;
10241558Srgrimes			add_dlist(&ep->ex_dirl, dp, grp);
10251558Srgrimes			dp = dp2;
10261558Srgrimes		}
10271558Srgrimes	}
10281558Srgrimes}
10291558Srgrimes
10301558Srgrimes/*
10311558Srgrimes * Traverse the binary tree either updating a node that is already there
10321558Srgrimes * for the new directory or adding the new node.
10331558Srgrimes */
10341558Srgrimesvoid
10351558Srgrimesadd_dlist(dpp, newdp, grp)
10361558Srgrimes	struct dirlist **dpp;
10371558Srgrimes	struct dirlist *newdp;
10381558Srgrimes	struct grouplist *grp;
10391558Srgrimes{
10401558Srgrimes	struct dirlist *dp;
10411558Srgrimes	struct hostlist *hp;
10421558Srgrimes	int cmp;
10431558Srgrimes
10441558Srgrimes	dp = *dpp;
10451558Srgrimes	if (dp) {
10461558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
10471558Srgrimes		if (cmp > 0) {
10481558Srgrimes			add_dlist(&dp->dp_left, newdp, grp);
10491558Srgrimes			return;
10501558Srgrimes		} else if (cmp < 0) {
10511558Srgrimes			add_dlist(&dp->dp_right, newdp, grp);
10521558Srgrimes			return;
10531558Srgrimes		} else
10541558Srgrimes			free((caddr_t)newdp);
10551558Srgrimes	} else {
10561558Srgrimes		dp = newdp;
10571558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
10581558Srgrimes		*dpp = dp;
10591558Srgrimes	}
10601558Srgrimes	if (grp) {
10611558Srgrimes
10621558Srgrimes		/*
10631558Srgrimes		 * Hang all of the host(s) off of the directory point.
10641558Srgrimes		 */
10651558Srgrimes		do {
10661558Srgrimes			hp = get_ht();
10671558Srgrimes			hp->ht_grp = grp;
10681558Srgrimes			hp->ht_next = dp->dp_hosts;
10691558Srgrimes			dp->dp_hosts = hp;
10701558Srgrimes			grp = grp->gr_next;
10711558Srgrimes		} while (grp);
10721558Srgrimes	} else
10731558Srgrimes		dp->dp_flag |= DP_DEFSET;
10741558Srgrimes}
10751558Srgrimes
10761558Srgrimes/*
10771558Srgrimes * Search for a dirpath on the export point.
10781558Srgrimes */
10791558Srgrimesstruct dirlist *
10801558Srgrimesdirp_search(dp, dirpath)
10811558Srgrimes	struct dirlist *dp;
10821558Srgrimes	char *dirpath;
10831558Srgrimes{
10841558Srgrimes	int cmp;
10851558Srgrimes
10861558Srgrimes	if (dp) {
10871558Srgrimes		cmp = strcmp(dp->dp_dirp, dirpath);
10881558Srgrimes		if (cmp > 0)
10891558Srgrimes			return (dirp_search(dp->dp_left, dirpath));
10901558Srgrimes		else if (cmp < 0)
10911558Srgrimes			return (dirp_search(dp->dp_right, dirpath));
10921558Srgrimes		else
10931558Srgrimes			return (dp);
10941558Srgrimes	}
10951558Srgrimes	return (dp);
10961558Srgrimes}
10971558Srgrimes
10981558Srgrimes/*
10991558Srgrimes * Scan for a host match in a directory tree.
11001558Srgrimes */
11011558Srgrimesint
11021558Srgrimeschk_host(dp, saddr, defsetp)
11031558Srgrimes	struct dirlist *dp;
11041558Srgrimes	u_long saddr;
11051558Srgrimes	int *defsetp;
11061558Srgrimes{
11071558Srgrimes	struct hostlist *hp;
11081558Srgrimes	struct grouplist *grp;
11091558Srgrimes	u_long **addrp;
11101558Srgrimes
11111558Srgrimes	if (dp) {
11121558Srgrimes		if (dp->dp_flag & DP_DEFSET)
11131558Srgrimes			*defsetp = 1;
11141558Srgrimes		hp = dp->dp_hosts;
11151558Srgrimes		while (hp) {
11161558Srgrimes			grp = hp->ht_grp;
11171558Srgrimes			switch (grp->gr_type) {
11181558Srgrimes			case GT_HOST:
11191558Srgrimes			    addrp = (u_long **)
11201558Srgrimes				grp->gr_ptr.gt_hostent->h_addr_list;
11211558Srgrimes			    while (*addrp) {
11221558Srgrimes				if (**addrp == saddr)
11231558Srgrimes				    return (1);
11241558Srgrimes				addrp++;
11251558Srgrimes			    }
11261558Srgrimes			    break;
11271558Srgrimes			case GT_NET:
11281558Srgrimes			    if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
11291558Srgrimes				grp->gr_ptr.gt_net.nt_net)
11301558Srgrimes				return (1);
11311558Srgrimes			    break;
11321558Srgrimes			};
11331558Srgrimes			hp = hp->ht_next;
11341558Srgrimes		}
11351558Srgrimes	}
11361558Srgrimes	return (0);
11371558Srgrimes}
11381558Srgrimes
11391558Srgrimes/*
11401558Srgrimes * Scan tree for a host that matches the address.
11411558Srgrimes */
11421558Srgrimesint
11431558Srgrimesscan_tree(dp, saddr)
11441558Srgrimes	struct dirlist *dp;
11451558Srgrimes	u_long saddr;
11461558Srgrimes{
11471558Srgrimes	int defset;
11481558Srgrimes
11491558Srgrimes	if (dp) {
11501558Srgrimes		if (scan_tree(dp->dp_left, saddr))
11511558Srgrimes			return (1);
11521558Srgrimes		if (chk_host(dp, saddr, &defset))
11531558Srgrimes			return (1);
11541558Srgrimes		if (scan_tree(dp->dp_right, saddr))
11551558Srgrimes			return (1);
11561558Srgrimes	}
11571558Srgrimes	return (0);
11581558Srgrimes}
11591558Srgrimes
11601558Srgrimes/*
11611558Srgrimes * Traverse the dirlist tree and free it up.
11621558Srgrimes */
11631558Srgrimesvoid
11641558Srgrimesfree_dir(dp)
11651558Srgrimes	struct dirlist *dp;
11661558Srgrimes{
11671558Srgrimes
11681558Srgrimes	if (dp) {
11691558Srgrimes		free_dir(dp->dp_left);
11701558Srgrimes		free_dir(dp->dp_right);
11711558Srgrimes		free_host(dp->dp_hosts);
11721558Srgrimes		free((caddr_t)dp);
11731558Srgrimes	}
11741558Srgrimes}
11751558Srgrimes
11761558Srgrimes/*
11771558Srgrimes * Parse the option string and update fields.
11781558Srgrimes * Option arguments may either be -<option>=<value> or
11791558Srgrimes * -<option> <value>
11801558Srgrimes */
11811558Srgrimesint
11821558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
11831558Srgrimes	char **cpp, **endcpp;
11841558Srgrimes	struct exportlist *ep;
11851558Srgrimes	struct grouplist *grp;
11861558Srgrimes	int *has_hostp;
11871558Srgrimes	int *exflagsp;
11881558Srgrimes	struct ucred *cr;
11891558Srgrimes{
11901558Srgrimes	char *cpoptarg, *cpoptend;
11911558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
11921558Srgrimes	int allflag, usedarg;
11931558Srgrimes
11941558Srgrimes	cpopt = *cpp;
11951558Srgrimes	cpopt++;
11961558Srgrimes	cp = *endcpp;
11971558Srgrimes	savedc = *cp;
11981558Srgrimes	*cp = '\0';
11991558Srgrimes	while (cpopt && *cpopt) {
12001558Srgrimes		allflag = 1;
12011558Srgrimes		usedarg = -2;
12021558Srgrimes		if (cpoptend = index(cpopt, ',')) {
12031558Srgrimes			*cpoptend++ = '\0';
12041558Srgrimes			if (cpoptarg = index(cpopt, '='))
12051558Srgrimes				*cpoptarg++ = '\0';
12061558Srgrimes		} else {
12071558Srgrimes			if (cpoptarg = index(cpopt, '='))
12081558Srgrimes				*cpoptarg++ = '\0';
12091558Srgrimes			else {
12101558Srgrimes				*cp = savedc;
12111558Srgrimes				nextfield(&cp, &endcp);
12121558Srgrimes				**endcpp = '\0';
12131558Srgrimes				if (endcp > cp && *cp != '-') {
12141558Srgrimes					cpoptarg = cp;
12151558Srgrimes					savedc2 = *endcp;
12161558Srgrimes					*endcp = '\0';
12171558Srgrimes					usedarg = 0;
12181558Srgrimes				}
12191558Srgrimes			}
12201558Srgrimes		}
12211558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
12221558Srgrimes			*exflagsp |= MNT_EXRDONLY;
12231558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
12241558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
12251558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
12261558Srgrimes			usedarg++;
12271558Srgrimes			parsecred(cpoptarg, cr);
12281558Srgrimes			if (allflag == 0) {
12291558Srgrimes				*exflagsp |= MNT_EXPORTANON;
12301558Srgrimes				opt_flags |= OP_MAPALL;
12311558Srgrimes			} else
12321558Srgrimes				opt_flags |= OP_MAPROOT;
12331558Srgrimes		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
12341558Srgrimes			*exflagsp |= MNT_EXKERB;
12351558Srgrimes			opt_flags |= OP_KERB;
12361558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
12371558Srgrimes			!strcmp(cpopt, "m"))) {
12381558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
12391558Srgrimes				syslog(LOG_ERR, "Bad mask: %s", cpoptarg);
12401558Srgrimes				return (1);
12411558Srgrimes			}
12421558Srgrimes			usedarg++;
12431558Srgrimes			opt_flags |= OP_MASK;
12441558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
12451558Srgrimes			!strcmp(cpopt, "n"))) {
12461558Srgrimes			if (grp->gr_type != GT_NULL) {
12471558Srgrimes				syslog(LOG_ERR, "Network/host conflict");
12481558Srgrimes				return (1);
12491558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
12501558Srgrimes				syslog(LOG_ERR, "Bad net: %s", cpoptarg);
12511558Srgrimes				return (1);
12521558Srgrimes			}
12531558Srgrimes			grp->gr_type = GT_NET;
12541558Srgrimes			*has_hostp = 1;
12551558Srgrimes			usedarg++;
12561558Srgrimes			opt_flags |= OP_NET;
12571558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
12581558Srgrimes			opt_flags |= OP_ALLDIRS;
12591558Srgrimes#ifdef ISO
12601558Srgrimes		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
12611558Srgrimes			if (get_isoaddr(cpoptarg, grp)) {
12621558Srgrimes				syslog(LOG_ERR, "Bad iso addr: %s", cpoptarg);
12631558Srgrimes				return (1);
12641558Srgrimes			}
12651558Srgrimes			*has_hostp = 1;
12661558Srgrimes			usedarg++;
12671558Srgrimes			opt_flags |= OP_ISO;
12681558Srgrimes#endif /* ISO */
12691558Srgrimes		} else {
12701558Srgrimes			syslog(LOG_ERR, "Bad opt %s", cpopt);
12711558Srgrimes			return (1);
12721558Srgrimes		}
12731558Srgrimes		if (usedarg >= 0) {
12741558Srgrimes			*endcp = savedc2;
12751558Srgrimes			**endcpp = savedc;
12761558Srgrimes			if (usedarg > 0) {
12771558Srgrimes				*cpp = cp;
12781558Srgrimes				*endcpp = endcp;
12791558Srgrimes			}
12801558Srgrimes			return (0);
12811558Srgrimes		}
12821558Srgrimes		cpopt = cpoptend;
12831558Srgrimes	}
12841558Srgrimes	**endcpp = savedc;
12851558Srgrimes	return (0);
12861558Srgrimes}
12871558Srgrimes
12881558Srgrimes/*
12891558Srgrimes * Translate a character string to the corresponding list of network
12901558Srgrimes * addresses for a hostname.
12911558Srgrimes */
12921558Srgrimesint
12937401Swpaulget_host(cp, grp, tgrp)
12941558Srgrimes	char *cp;
12951558Srgrimes	struct grouplist *grp;
12967401Swpaul	struct grouplist *tgrp;
12971558Srgrimes{
12987401Swpaul	struct grouplist *checkgrp;
12991558Srgrimes	struct hostent *hp, *nhp;
13001558Srgrimes	char **addrp, **naddrp;
13011558Srgrimes	struct hostent t_host;
13021558Srgrimes	int i;
13031558Srgrimes	u_long saddr;
13041558Srgrimes	char *aptr[2];
13051558Srgrimes
13061558Srgrimes	if (grp->gr_type != GT_NULL)
13071558Srgrimes		return (1);
13081558Srgrimes	if ((hp = gethostbyname(cp)) == NULL) {
13091558Srgrimes		if (isdigit(*cp)) {
13101558Srgrimes			saddr = inet_addr(cp);
13111558Srgrimes			if (saddr == -1) {
13121558Srgrimes				syslog(LOG_ERR, "Inet_addr failed");
13131558Srgrimes				return (1);
13141558Srgrimes			}
13151558Srgrimes			if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
13161558Srgrimes				AF_INET)) == NULL) {
13171558Srgrimes				hp = &t_host;
13181558Srgrimes				hp->h_name = cp;
13191558Srgrimes				hp->h_addrtype = AF_INET;
13201558Srgrimes				hp->h_length = sizeof (u_long);
13211558Srgrimes				hp->h_addr_list = aptr;
13221558Srgrimes				aptr[0] = (char *)&saddr;
13231558Srgrimes				aptr[1] = (char *)NULL;
13241558Srgrimes			}
13251558Srgrimes		} else {
13261558Srgrimes			syslog(LOG_ERR, "Gethostbyname failed");
13271558Srgrimes			return (1);
13281558Srgrimes		}
13291558Srgrimes	}
13307401Swpaul        /*
13317401Swpaul         * Sanity check: make sure we don't already have an entry
13327401Swpaul         * for this host in the grouplist.
13337401Swpaul         */
13347401Swpaul        checkgrp = tgrp;
13357401Swpaul        while (checkgrp) {
13367401Swpaul                if (checkgrp->gr_ptr.gt_hostent != NULL &&
13377401Swpaul                    !strcmp(checkgrp->gr_ptr.gt_hostent->h_name, hp->h_name)) {
13387401Swpaul                        grp->gr_type = GT_IGNORE;
13397401Swpaul			return(0);
13407401Swpaul		}
13417401Swpaul                checkgrp = checkgrp->gr_next;
13427401Swpaul        }
13437401Swpaul
13441558Srgrimes	grp->gr_type = GT_HOST;
13451558Srgrimes	nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
13461558Srgrimes		malloc(sizeof(struct hostent));
13471558Srgrimes	if (nhp == (struct hostent *)NULL)
13481558Srgrimes		out_of_mem();
13491558Srgrimes	bcopy((caddr_t)hp, (caddr_t)nhp,
13501558Srgrimes		sizeof(struct hostent));
13511558Srgrimes	i = strlen(hp->h_name)+1;
13521558Srgrimes	nhp->h_name = (char *)malloc(i);
13531558Srgrimes	if (nhp->h_name == (char *)NULL)
13541558Srgrimes		out_of_mem();
13551558Srgrimes	bcopy(hp->h_name, nhp->h_name, i);
13561558Srgrimes	addrp = hp->h_addr_list;
13571558Srgrimes	i = 1;
13581558Srgrimes	while (*addrp++)
13591558Srgrimes		i++;
13601558Srgrimes	naddrp = nhp->h_addr_list = (char **)
13611558Srgrimes		malloc(i*sizeof(char *));
13621558Srgrimes	if (naddrp == (char **)NULL)
13631558Srgrimes		out_of_mem();
13641558Srgrimes	addrp = hp->h_addr_list;
13651558Srgrimes	while (*addrp) {
13661558Srgrimes		*naddrp = (char *)
13671558Srgrimes		    malloc(hp->h_length);
13681558Srgrimes		if (*naddrp == (char *)NULL)
13691558Srgrimes		    out_of_mem();
13701558Srgrimes		bcopy(*addrp, *naddrp,
13711558Srgrimes			hp->h_length);
13721558Srgrimes		addrp++;
13731558Srgrimes		naddrp++;
13741558Srgrimes	}
13751558Srgrimes	*naddrp = (char *)NULL;
13761558Srgrimes	if (debug)
13771558Srgrimes		fprintf(stderr, "got host %s\n", hp->h_name);
13781558Srgrimes	return (0);
13791558Srgrimes}
13801558Srgrimes
13811558Srgrimes/*
13821558Srgrimes * Free up an exports list component
13831558Srgrimes */
13841558Srgrimesvoid
13851558Srgrimesfree_exp(ep)
13861558Srgrimes	struct exportlist *ep;
13871558Srgrimes{
13881558Srgrimes
13891558Srgrimes	if (ep->ex_defdir) {
13901558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
13911558Srgrimes		free((caddr_t)ep->ex_defdir);
13921558Srgrimes	}
13931558Srgrimes	if (ep->ex_fsdir)
13941558Srgrimes		free(ep->ex_fsdir);
13951558Srgrimes	free_dir(ep->ex_dirl);
13961558Srgrimes	free((caddr_t)ep);
13971558Srgrimes}
13981558Srgrimes
13991558Srgrimes/*
14001558Srgrimes * Free hosts.
14011558Srgrimes */
14021558Srgrimesvoid
14031558Srgrimesfree_host(hp)
14041558Srgrimes	struct hostlist *hp;
14051558Srgrimes{
14061558Srgrimes	struct hostlist *hp2;
14071558Srgrimes
14081558Srgrimes	while (hp) {
14091558Srgrimes		hp2 = hp;
14101558Srgrimes		hp = hp->ht_next;
14111558Srgrimes		free((caddr_t)hp2);
14121558Srgrimes	}
14131558Srgrimes}
14141558Srgrimes
14151558Srgrimesstruct hostlist *
14161558Srgrimesget_ht()
14171558Srgrimes{
14181558Srgrimes	struct hostlist *hp;
14191558Srgrimes
14201558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
14211558Srgrimes	if (hp == (struct hostlist *)NULL)
14221558Srgrimes		out_of_mem();
14231558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
14241558Srgrimes	return (hp);
14251558Srgrimes}
14261558Srgrimes
14271558Srgrimes#ifdef ISO
14281558Srgrimes/*
14291558Srgrimes * Translate an iso address.
14301558Srgrimes */
14311558Srgrimesget_isoaddr(cp, grp)
14321558Srgrimes	char *cp;
14331558Srgrimes	struct grouplist *grp;
14341558Srgrimes{
14351558Srgrimes	struct iso_addr *isop;
14361558Srgrimes	struct sockaddr_iso *isoaddr;
14371558Srgrimes
14381558Srgrimes	if (grp->gr_type != GT_NULL)
14391558Srgrimes		return (1);
14401558Srgrimes	if ((isop = iso_addr(cp)) == NULL) {
14411558Srgrimes		syslog(LOG_ERR,
14421558Srgrimes		    "iso_addr failed, ignored");
14431558Srgrimes		return (1);
14441558Srgrimes	}
14451558Srgrimes	isoaddr = (struct sockaddr_iso *)
14461558Srgrimes	    malloc(sizeof (struct sockaddr_iso));
14471558Srgrimes	if (isoaddr == (struct sockaddr_iso *)NULL)
14481558Srgrimes		out_of_mem();
14491558Srgrimes	bzero((caddr_t)isoaddr, sizeof (struct sockaddr_iso));
14501558Srgrimes	bcopy((caddr_t)isop, (caddr_t)&isoaddr->siso_addr,
14511558Srgrimes		sizeof (struct iso_addr));
14521558Srgrimes	isoaddr->siso_len = sizeof (struct sockaddr_iso);
14531558Srgrimes	isoaddr->siso_family = AF_ISO;
14541558Srgrimes	grp->gr_type = GT_ISO;
14551558Srgrimes	grp->gr_ptr.gt_isoaddr = isoaddr;
14561558Srgrimes	return (0);
14571558Srgrimes}
14581558Srgrimes#endif	/* ISO */
14591558Srgrimes
14601558Srgrimes/*
14611558Srgrimes * Out of memory, fatal
14621558Srgrimes */
14631558Srgrimesvoid
14641558Srgrimesout_of_mem()
14651558Srgrimes{
14661558Srgrimes
14671558Srgrimes	syslog(LOG_ERR, "Out of memory");
14681558Srgrimes	exit(2);
14691558Srgrimes}
14701558Srgrimes
14711558Srgrimes/*
14721558Srgrimes * Do the mount syscall with the update flag to push the export info into
14731558Srgrimes * the kernel.
14741558Srgrimes */
14751558Srgrimesint
14761558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
14771558Srgrimes	struct exportlist *ep;
14781558Srgrimes	struct grouplist *grp;
14791558Srgrimes	int exflags;
14801558Srgrimes	struct ucred *anoncrp;
14811558Srgrimes	char *dirp;
14821558Srgrimes	int dirplen;
14831558Srgrimes	struct statfs *fsb;
14841558Srgrimes{
14851558Srgrimes	char *cp = (char *)NULL;
14861558Srgrimes	u_long **addrp;
14871558Srgrimes	int done;
14881558Srgrimes	char savedc = '\0';
14891558Srgrimes	struct sockaddr_in sin, imask;
14901558Srgrimes	union {
14911558Srgrimes		struct ufs_args ua;
14921558Srgrimes		struct iso_args ia;
14931558Srgrimes		struct mfs_args ma;
14941558Srgrimes	} args;
14951558Srgrimes	u_long net;
14961558Srgrimes
14971558Srgrimes	args.ua.fspec = 0;
14981558Srgrimes	args.ua.export.ex_flags = exflags;
14991558Srgrimes	args.ua.export.ex_anon = *anoncrp;
15001558Srgrimes	bzero((char *)&sin, sizeof(sin));
15011558Srgrimes	bzero((char *)&imask, sizeof(imask));
15021558Srgrimes	sin.sin_family = AF_INET;
15031558Srgrimes	sin.sin_len = sizeof(sin);
15041558Srgrimes	imask.sin_family = AF_INET;
15051558Srgrimes	imask.sin_len = sizeof(sin);
15061558Srgrimes	if (grp->gr_type == GT_HOST)
15071558Srgrimes		addrp = (u_long **)grp->gr_ptr.gt_hostent->h_addr_list;
15081558Srgrimes	else
15091558Srgrimes		addrp = (u_long **)NULL;
15101558Srgrimes	done = FALSE;
15111558Srgrimes	while (!done) {
15121558Srgrimes		switch (grp->gr_type) {
15131558Srgrimes		case GT_HOST:
15141558Srgrimes			if (addrp) {
15151558Srgrimes				sin.sin_addr.s_addr = **addrp;
15161558Srgrimes				args.ua.export.ex_addrlen = sizeof(sin);
15171558Srgrimes			} else
15181558Srgrimes				args.ua.export.ex_addrlen = 0;
15191558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
15201558Srgrimes			args.ua.export.ex_masklen = 0;
15211558Srgrimes			break;
15221558Srgrimes		case GT_NET:
15231558Srgrimes			if (grp->gr_ptr.gt_net.nt_mask)
15241558Srgrimes			    imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
15251558Srgrimes			else {
15261558Srgrimes			    net = ntohl(grp->gr_ptr.gt_net.nt_net);
15271558Srgrimes			    if (IN_CLASSA(net))
15281558Srgrimes				imask.sin_addr.s_addr = inet_addr("255.0.0.0");
15291558Srgrimes			    else if (IN_CLASSB(net))
15301558Srgrimes				imask.sin_addr.s_addr =
15311558Srgrimes				    inet_addr("255.255.0.0");
15321558Srgrimes			    else
15331558Srgrimes				imask.sin_addr.s_addr =
15341558Srgrimes				    inet_addr("255.255.255.0");
15351558Srgrimes			    grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
15361558Srgrimes			}
15371558Srgrimes			sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
15381558Srgrimes			args.ua.export.ex_addr = (struct sockaddr *)&sin;
15391558Srgrimes			args.ua.export.ex_addrlen = sizeof (sin);
15401558Srgrimes			args.ua.export.ex_mask = (struct sockaddr *)&imask;
15411558Srgrimes			args.ua.export.ex_masklen = sizeof (imask);
15421558Srgrimes			break;
15431558Srgrimes#ifdef ISO
15441558Srgrimes		case GT_ISO:
15451558Srgrimes			args.ua.export.ex_addr =
15461558Srgrimes				(struct sockaddr *)grp->gr_ptr.gt_isoaddr;
15471558Srgrimes			args.ua.export.ex_addrlen =
15481558Srgrimes				sizeof(struct sockaddr_iso);
15491558Srgrimes			args.ua.export.ex_masklen = 0;
15501558Srgrimes			break;
15511558Srgrimes#endif	/* ISO */
15527401Swpaul		case GT_IGNORE:
15537401Swpaul			return(0);
15547401Swpaul			break;
15551558Srgrimes		default:
15561558Srgrimes			syslog(LOG_ERR, "Bad grouptype");
15571558Srgrimes			if (cp)
15581558Srgrimes				*cp = savedc;
15591558Srgrimes			return (1);
15601558Srgrimes		};
15611558Srgrimes
15621558Srgrimes		/*
15631558Srgrimes		 * XXX:
15641558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
15651558Srgrimes		 * of looping back up the dirp to the mount point??
15661558Srgrimes		 * Also, needs to know how to export all types of local
15671558Srgrimes		 * exportable file systems and not just MOUNT_UFS.
15681558Srgrimes		 */
15691558Srgrimes		while (mount(fsb->f_type, dirp,
15701558Srgrimes		       fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
15711558Srgrimes			if (cp)
15721558Srgrimes				*cp-- = savedc;
15731558Srgrimes			else
15741558Srgrimes				cp = dirp + dirplen - 1;
15751558Srgrimes			if (errno == EPERM) {
15761558Srgrimes				syslog(LOG_ERR,
15771558Srgrimes				   "Can't change attributes for %s.\n", dirp);
15781558Srgrimes				return (1);
15791558Srgrimes			}
15801558Srgrimes			if (opt_flags & OP_ALLDIRS) {
15814895Swollman				syslog(LOG_ERR, "Could not remount %s: %m",
15824895Swollman					dirp);
15831558Srgrimes				return (1);
15841558Srgrimes			}
15851558Srgrimes			/* back up over the last component */
15861558Srgrimes			while (*cp == '/' && cp > dirp)
15871558Srgrimes				cp--;
15881558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
15891558Srgrimes				cp--;
15901558Srgrimes			if (cp == dirp) {
15911558Srgrimes				if (debug)
15921558Srgrimes					fprintf(stderr,"mnt unsucc\n");
15931558Srgrimes				syslog(LOG_ERR, "Can't export %s", dirp);
15941558Srgrimes				return (1);
15951558Srgrimes			}
15961558Srgrimes			savedc = *cp;
15971558Srgrimes			*cp = '\0';
15981558Srgrimes		}
15991558Srgrimes		if (addrp) {
16001558Srgrimes			++addrp;
16011558Srgrimes			if (*addrp == (u_long *)NULL)
16021558Srgrimes				done = TRUE;
16031558Srgrimes		} else
16041558Srgrimes			done = TRUE;
16051558Srgrimes	}
16061558Srgrimes	if (cp)
16071558Srgrimes		*cp = savedc;
16081558Srgrimes	return (0);
16091558Srgrimes}
16101558Srgrimes
16111558Srgrimes/*
16121558Srgrimes * Translate a net address.
16131558Srgrimes */
16141558Srgrimesint
16151558Srgrimesget_net(cp, net, maskflg)
16161558Srgrimes	char *cp;
16171558Srgrimes	struct netmsk *net;
16181558Srgrimes	int maskflg;
16191558Srgrimes{
16201558Srgrimes	struct netent *np;
16211558Srgrimes	long netaddr;
16221558Srgrimes	struct in_addr inetaddr, inetaddr2;
16231558Srgrimes	char *name;
16241558Srgrimes
16251558Srgrimes	if (np = getnetbyname(cp))
16261558Srgrimes		inetaddr = inet_makeaddr(np->n_net, 0);
16271558Srgrimes	else if (isdigit(*cp)) {
16281558Srgrimes		if ((netaddr = inet_network(cp)) == -1)
16291558Srgrimes			return (1);
16301558Srgrimes		inetaddr = inet_makeaddr(netaddr, 0);
16311558Srgrimes		/*
16321558Srgrimes		 * Due to arbritrary subnet masks, you don't know how many
16331558Srgrimes		 * bits to shift the address to make it into a network,
16341558Srgrimes		 * however you do know how to make a network address into
16351558Srgrimes		 * a host with host == 0 and then compare them.
16361558Srgrimes		 * (What a pest)
16371558Srgrimes		 */
16381558Srgrimes		if (!maskflg) {
16391558Srgrimes			setnetent(0);
16401558Srgrimes			while (np = getnetent()) {
16411558Srgrimes				inetaddr2 = inet_makeaddr(np->n_net, 0);
16421558Srgrimes				if (inetaddr2.s_addr == inetaddr.s_addr)
16431558Srgrimes					break;
16441558Srgrimes			}
16451558Srgrimes			endnetent();
16461558Srgrimes		}
16471558Srgrimes	} else
16481558Srgrimes		return (1);
16491558Srgrimes	if (maskflg)
16501558Srgrimes		net->nt_mask = inetaddr.s_addr;
16511558Srgrimes	else {
16521558Srgrimes		if (np)
16531558Srgrimes			name = np->n_name;
16541558Srgrimes		else
16551558Srgrimes			name = inet_ntoa(inetaddr);
16561558Srgrimes		net->nt_name = (char *)malloc(strlen(name) + 1);
16571558Srgrimes		if (net->nt_name == (char *)NULL)
16581558Srgrimes			out_of_mem();
16591558Srgrimes		strcpy(net->nt_name, name);
16601558Srgrimes		net->nt_net = inetaddr.s_addr;
16611558Srgrimes	}
16621558Srgrimes	return (0);
16631558Srgrimes}
16641558Srgrimes
16651558Srgrimes/*
16661558Srgrimes * Parse out the next white space separated field
16671558Srgrimes */
16681558Srgrimesvoid
16691558Srgrimesnextfield(cp, endcp)
16701558Srgrimes	char **cp;
16711558Srgrimes	char **endcp;
16721558Srgrimes{
16731558Srgrimes	char *p;
16741558Srgrimes
16751558Srgrimes	p = *cp;
16761558Srgrimes	while (*p == ' ' || *p == '\t')
16771558Srgrimes		p++;
16781558Srgrimes	if (*p == '\n' || *p == '\0')
16791558Srgrimes		*cp = *endcp = p;
16801558Srgrimes	else {
16811558Srgrimes		*cp = p++;
16821558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
16831558Srgrimes			p++;
16841558Srgrimes		*endcp = p;
16851558Srgrimes	}
16861558Srgrimes}
16871558Srgrimes
16881558Srgrimes/*
16891558Srgrimes * Get an exports file line. Skip over blank lines and handle line
16901558Srgrimes * continuations.
16911558Srgrimes */
16921558Srgrimesint
16931558Srgrimesget_line()
16941558Srgrimes{
16951558Srgrimes	char *p, *cp;
16961558Srgrimes	int len;
16971558Srgrimes	int totlen, cont_line;
16981558Srgrimes
16991558Srgrimes	/*
17001558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
17011558Srgrimes	 */
17021558Srgrimes	p = line;
17031558Srgrimes	totlen = 0;
17041558Srgrimes	do {
17051558Srgrimes		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
17061558Srgrimes			return (0);
17071558Srgrimes		len = strlen(p);
17081558Srgrimes		cp = p + len - 1;
17091558Srgrimes		cont_line = 0;
17101558Srgrimes		while (cp >= p &&
17111558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
17121558Srgrimes			if (*cp == '\\')
17131558Srgrimes				cont_line = 1;
17141558Srgrimes			cp--;
17151558Srgrimes			len--;
17161558Srgrimes		}
17171558Srgrimes		*++cp = '\0';
17181558Srgrimes		if (len > 0) {
17191558Srgrimes			totlen += len;
17201558Srgrimes			if (totlen >= LINESIZ) {
17211558Srgrimes				syslog(LOG_ERR, "Exports line too long");
17221558Srgrimes				exit(2);
17231558Srgrimes			}
17241558Srgrimes			p = cp;
17251558Srgrimes		}
17261558Srgrimes	} while (totlen == 0 || cont_line);
17271558Srgrimes	return (1);
17281558Srgrimes}
17291558Srgrimes
17301558Srgrimes/*
17311558Srgrimes * Parse a description of a credential.
17321558Srgrimes */
17331558Srgrimesvoid
17341558Srgrimesparsecred(namelist, cr)
17351558Srgrimes	char *namelist;
17361558Srgrimes	struct ucred *cr;
17371558Srgrimes{
17381558Srgrimes	char *name;
17391558Srgrimes	int cnt;
17401558Srgrimes	char *names;
17411558Srgrimes	struct passwd *pw;
17421558Srgrimes	struct group *gr;
17431558Srgrimes	int ngroups, groups[NGROUPS + 1];
17441558Srgrimes
17451558Srgrimes	/*
17461558Srgrimes	 * Set up the unpriviledged user.
17471558Srgrimes	 */
17481558Srgrimes	cr->cr_ref = 1;
17491558Srgrimes	cr->cr_uid = -2;
17501558Srgrimes	cr->cr_groups[0] = -2;
17511558Srgrimes	cr->cr_ngroups = 1;
17521558Srgrimes	/*
17531558Srgrimes	 * Get the user's password table entry.
17541558Srgrimes	 */
17551558Srgrimes	names = strsep(&namelist, " \t\n");
17561558Srgrimes	name = strsep(&names, ":");
17571558Srgrimes	if (isdigit(*name) || *name == '-')
17581558Srgrimes		pw = getpwuid(atoi(name));
17591558Srgrimes	else
17601558Srgrimes		pw = getpwnam(name);
17611558Srgrimes	/*
17621558Srgrimes	 * Credentials specified as those of a user.
17631558Srgrimes	 */
17641558Srgrimes	if (names == NULL) {
17651558Srgrimes		if (pw == NULL) {
17661558Srgrimes			syslog(LOG_ERR, "Unknown user: %s", name);
17671558Srgrimes			return;
17681558Srgrimes		}
17691558Srgrimes		cr->cr_uid = pw->pw_uid;
17701558Srgrimes		ngroups = NGROUPS + 1;
17711558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
17721558Srgrimes			syslog(LOG_ERR, "Too many groups");
17731558Srgrimes		/*
17741558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
17751558Srgrimes		 */
17761558Srgrimes		cr->cr_ngroups = ngroups - 1;
17771558Srgrimes		cr->cr_groups[0] = groups[0];
17781558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
17791558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
17801558Srgrimes		return;
17811558Srgrimes	}
17821558Srgrimes	/*
17831558Srgrimes	 * Explicit credential specified as a colon separated list:
17841558Srgrimes	 *	uid:gid:gid:...
17851558Srgrimes	 */
17861558Srgrimes	if (pw != NULL)
17871558Srgrimes		cr->cr_uid = pw->pw_uid;
17881558Srgrimes	else if (isdigit(*name) || *name == '-')
17891558Srgrimes		cr->cr_uid = atoi(name);
17901558Srgrimes	else {
17911558Srgrimes		syslog(LOG_ERR, "Unknown user: %s", name);
17921558Srgrimes		return;
17931558Srgrimes	}
17941558Srgrimes	cr->cr_ngroups = 0;
17951558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
17961558Srgrimes		name = strsep(&names, ":");
17971558Srgrimes		if (isdigit(*name) || *name == '-') {
17981558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
17991558Srgrimes		} else {
18001558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
18011558Srgrimes				syslog(LOG_ERR, "Unknown group: %s", name);
18021558Srgrimes				continue;
18031558Srgrimes			}
18041558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
18051558Srgrimes		}
18061558Srgrimes	}
18071558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
18081558Srgrimes		syslog(LOG_ERR, "Too many groups");
18091558Srgrimes}
18101558Srgrimes
18111558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
18121558Srgrimes/*
18131558Srgrimes * Routines that maintain the remote mounttab
18141558Srgrimes */
18151558Srgrimesvoid
18161558Srgrimesget_mountlist()
18171558Srgrimes{
18181558Srgrimes	struct mountlist *mlp, **mlpp;
18191558Srgrimes	char *eos, *dirp;
18201558Srgrimes	int len;
18211558Srgrimes	char str[STRSIZ];
18221558Srgrimes	FILE *mlfile;
18231558Srgrimes
18241558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
18251558Srgrimes		syslog(LOG_ERR, "Can't open %s", _PATH_RMOUNTLIST);
18261558Srgrimes		return;
18271558Srgrimes	}
18281558Srgrimes	mlpp = &mlhead;
18291558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
18301558Srgrimes		if ((dirp = index(str, '\t')) == NULL &&
18311558Srgrimes		    (dirp = index(str, ' ')) == NULL)
18321558Srgrimes			continue;
18331558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
18341558Srgrimes		len = dirp-str;
18351558Srgrimes		if (len > RPCMNT_NAMELEN)
18361558Srgrimes			len = RPCMNT_NAMELEN;
18371558Srgrimes		bcopy(str, mlp->ml_host, len);
18381558Srgrimes		mlp->ml_host[len] = '\0';
18391558Srgrimes		while (*dirp == '\t' || *dirp == ' ')
18401558Srgrimes			dirp++;
18411558Srgrimes		if ((eos = index(dirp, '\t')) == NULL &&
18421558Srgrimes		    (eos = index(dirp, ' ')) == NULL &&
18431558Srgrimes		    (eos = index(dirp, '\n')) == NULL)
18441558Srgrimes			len = strlen(dirp);
18451558Srgrimes		else
18461558Srgrimes			len = eos-dirp;
18471558Srgrimes		if (len > RPCMNT_PATHLEN)
18481558Srgrimes			len = RPCMNT_PATHLEN;
18491558Srgrimes		bcopy(dirp, mlp->ml_dirp, len);
18501558Srgrimes		mlp->ml_dirp[len] = '\0';
18511558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
18521558Srgrimes		*mlpp = mlp;
18531558Srgrimes		mlpp = &mlp->ml_next;
18541558Srgrimes	}
18551558Srgrimes	fclose(mlfile);
18561558Srgrimes}
18571558Srgrimes
18581558Srgrimesvoid
18591558Srgrimesdel_mlist(hostp, dirp)
18601558Srgrimes	char *hostp, *dirp;
18611558Srgrimes{
18621558Srgrimes	struct mountlist *mlp, **mlpp;
18631558Srgrimes	struct mountlist *mlp2;
18641558Srgrimes	FILE *mlfile;
18651558Srgrimes	int fnd = 0;
18661558Srgrimes
18671558Srgrimes	mlpp = &mlhead;
18681558Srgrimes	mlp = mlhead;
18691558Srgrimes	while (mlp) {
18701558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
18711558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
18721558Srgrimes			fnd = 1;
18731558Srgrimes			mlp2 = mlp;
18741558Srgrimes			*mlpp = mlp = mlp->ml_next;
18751558Srgrimes			free((caddr_t)mlp2);
18761558Srgrimes		} else {
18771558Srgrimes			mlpp = &mlp->ml_next;
18781558Srgrimes			mlp = mlp->ml_next;
18791558Srgrimes		}
18801558Srgrimes	}
18811558Srgrimes	if (fnd) {
18821558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
18831558Srgrimes			syslog(LOG_ERR,"Can't update %s", _PATH_RMOUNTLIST);
18841558Srgrimes			return;
18851558Srgrimes		}
18861558Srgrimes		mlp = mlhead;
18871558Srgrimes		while (mlp) {
18881558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
18891558Srgrimes			mlp = mlp->ml_next;
18901558Srgrimes		}
18911558Srgrimes		fclose(mlfile);
18921558Srgrimes	}
18931558Srgrimes}
18941558Srgrimes
18951558Srgrimesvoid
18961558Srgrimesadd_mlist(hostp, dirp)
18971558Srgrimes	char *hostp, *dirp;
18981558Srgrimes{
18991558Srgrimes	struct mountlist *mlp, **mlpp;
19001558Srgrimes	FILE *mlfile;
19011558Srgrimes
19021558Srgrimes	mlpp = &mlhead;
19031558Srgrimes	mlp = mlhead;
19041558Srgrimes	while (mlp) {
19051558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
19061558Srgrimes			return;
19071558Srgrimes		mlpp = &mlp->ml_next;
19081558Srgrimes		mlp = mlp->ml_next;
19091558Srgrimes	}
19101558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
19111558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
19121558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
19131558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
19141558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
19151558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
19161558Srgrimes	*mlpp = mlp;
19171558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
19181558Srgrimes		syslog(LOG_ERR, "Can't update %s", _PATH_RMOUNTLIST);
19191558Srgrimes		return;
19201558Srgrimes	}
19211558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
19221558Srgrimes	fclose(mlfile);
19231558Srgrimes}
19241558Srgrimes
19251558Srgrimes/*
19261558Srgrimes * This function is called via. SIGTERM when the system is going down.
19271558Srgrimes * It sends a broadcast RPCMNT_UMNTALL.
19281558Srgrimes */
19291558Srgrimesvoid
19301558Srgrimessend_umntall()
19311558Srgrimes{
19321558Srgrimes	(void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
19331558Srgrimes		xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
19341558Srgrimes	exit(0);
19351558Srgrimes}
19361558Srgrimes
19371558Srgrimesint
19381558Srgrimesumntall_each(resultsp, raddr)
19391558Srgrimes	caddr_t resultsp;
19401558Srgrimes	struct sockaddr_in *raddr;
19411558Srgrimes{
19421558Srgrimes	return (1);
19431558Srgrimes}
19441558Srgrimes
19451558Srgrimes/*
19461558Srgrimes * Free up a group list.
19471558Srgrimes */
19481558Srgrimesvoid
19491558Srgrimesfree_grp(grp)
19501558Srgrimes	struct grouplist *grp;
19511558Srgrimes{
19521558Srgrimes	char **addrp;
19531558Srgrimes
19541558Srgrimes	if (grp->gr_type == GT_HOST) {
19551558Srgrimes		if (grp->gr_ptr.gt_hostent->h_name) {
19561558Srgrimes			addrp = grp->gr_ptr.gt_hostent->h_addr_list;
19571558Srgrimes			while (addrp && *addrp)
19581558Srgrimes				free(*addrp++);
19591558Srgrimes			free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
19601558Srgrimes			free(grp->gr_ptr.gt_hostent->h_name);
19611558Srgrimes		}
19621558Srgrimes		free((caddr_t)grp->gr_ptr.gt_hostent);
19631558Srgrimes	} else if (grp->gr_type == GT_NET) {
19641558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
19651558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
19661558Srgrimes	}
19671558Srgrimes#ifdef ISO
19681558Srgrimes	else if (grp->gr_type == GT_ISO)
19691558Srgrimes		free((caddr_t)grp->gr_ptr.gt_isoaddr);
19701558Srgrimes#endif
19711558Srgrimes	free((caddr_t)grp);
19721558Srgrimes}
19731558Srgrimes
19741558Srgrimes#ifdef DEBUG
19751558Srgrimesvoid
19761558SrgrimesSYSLOG(int pri, const char *fmt, ...)
19771558Srgrimes{
19781558Srgrimes	va_list ap;
19791558Srgrimes
19801558Srgrimes	va_start(ap, fmt);
19811558Srgrimes	vfprintf(stderr, fmt, ap);
19821558Srgrimes	va_end(ap);
19831558Srgrimes}
19841558Srgrimes#endif /* DEBUG */
19851558Srgrimes
19861558Srgrimes/*
19871558Srgrimes * Check options for consistency.
19881558Srgrimes */
19891558Srgrimesint
19901558Srgrimescheck_options(dp)
19911558Srgrimes	struct dirlist *dp;
19921558Srgrimes{
19931558Srgrimes
19941558Srgrimes	if (dp == (struct dirlist *)NULL)
19951558Srgrimes	    return (1);
19961558Srgrimes	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
19971558Srgrimes	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
19981558Srgrimes	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
19991558Srgrimes	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
20001558Srgrimes	    return (1);
20011558Srgrimes	}
20021558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
20031558Srgrimes	    syslog(LOG_ERR, "-mask requires -net");
20041558Srgrimes	    return (1);
20051558Srgrimes	}
20061558Srgrimes	if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
20071558Srgrimes	    syslog(LOG_ERR, "-net and -iso mutually exclusive");
20081558Srgrimes	    return (1);
20091558Srgrimes	}
20101558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
20111558Srgrimes	    syslog(LOG_ERR, "-alldir has multiple directories");
20121558Srgrimes	    return (1);
20131558Srgrimes	}
20141558Srgrimes	return (0);
20151558Srgrimes}
20161558Srgrimes
20171558Srgrimes/*
20181558Srgrimes * Check an absolute directory path for any symbolic links. Return true
20191558Srgrimes * if no symbolic links are found.
20201558Srgrimes */
20211558Srgrimesint
20221558Srgrimescheck_dirpath(dirp)
20231558Srgrimes	char *dirp;
20241558Srgrimes{
20251558Srgrimes	char *cp;
20261558Srgrimes	int ret = 1;
20271558Srgrimes	struct stat sb;
20281558Srgrimes
20291558Srgrimes	cp = dirp + 1;
20301558Srgrimes	while (*cp && ret) {
20311558Srgrimes		if (*cp == '/') {
20321558Srgrimes			*cp = '\0';
20331558Srgrimes			if (lstat(dirp, &sb) < 0 ||
20341558Srgrimes				(sb.st_mode & S_IFMT) != S_IFDIR)
20351558Srgrimes				ret = 0;
20361558Srgrimes			*cp = '/';
20371558Srgrimes		}
20381558Srgrimes		cp++;
20391558Srgrimes	}
20401558Srgrimes	if (lstat(dirp, &sb) < 0 ||
20411558Srgrimes		(sb.st_mode & S_IFMT) != S_IFDIR)
20421558Srgrimes		ret = 0;
20431558Srgrimes	return (ret);
20441558Srgrimes}
2045