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