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