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