mountd.c revision 74462
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
3837663Scharnierstatic const 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
4437663Scharnier#if 0
4537663Scharnierstatic char sccsid[] = "@(#)mountd.c	8.15 (Berkeley) 5/1/95";
4637663Scharnier#endif
472999Swollmanstatic const char rcsid[] =
4850476Speter  "$FreeBSD: head/usr.sbin/mountd/mountd.c 74462 2001-03-19 12:50:13Z alfred $";
492999Swollman#endif /*not lint*/
501558Srgrimes
511558Srgrimes#include <sys/param.h>
521558Srgrimes#include <sys/mount.h>
5374462Salfred#include <sys/fcntl.h>
541558Srgrimes#include <sys/stat.h>
551558Srgrimes#include <sys/syslog.h>
5624330Sguido#include <sys/sysctl.h>
571558Srgrimes
581558Srgrimes#include <rpc/rpc.h>
591558Srgrimes#include <rpc/pmap_clnt.h>
6074462Salfred#include <rpc/pmap_prot.h>
6174462Salfred#include <rpcsvc/mount.h>
621558Srgrimes#include <nfs/rpcv2.h>
639336Sdfr#include <nfs/nfsproto.h>
6424330Sguido#include <nfs/nfs.h>
6523681Speter#include <ufs/ufs/ufsmount.h>
6623681Speter#include <msdosfs/msdosfsmount.h>
6754093Ssemenu#include <ntfs/ntfsmount.h>
6823681Speter#include <isofs/cd9660/cd9660_mount.h>	/* XXX need isofs in include */
691558Srgrimes
701558Srgrimes#include <arpa/inet.h>
711558Srgrimes
721558Srgrimes#include <ctype.h>
7337663Scharnier#include <err.h>
741558Srgrimes#include <errno.h>
751558Srgrimes#include <grp.h>
761558Srgrimes#include <netdb.h>
771558Srgrimes#include <pwd.h>
781558Srgrimes#include <signal.h>
791558Srgrimes#include <stdio.h>
801558Srgrimes#include <stdlib.h>
811558Srgrimes#include <string.h>
821558Srgrimes#include <unistd.h>
831558Srgrimes#include "pathnames.h"
841558Srgrimes
851558Srgrimes#ifdef DEBUG
861558Srgrimes#include <stdarg.h>
871558Srgrimes#endif
881558Srgrimes
8974462Salfred#ifndef MOUNTDLOCK
9074462Salfred#define MOUNTDLOCK "/var/run/mountd.lock"
9174462Salfred#endif
9274462Salfred
931558Srgrimes/*
941558Srgrimes * Structures for keeping the mount list and export list
951558Srgrimes */
961558Srgrimesstruct mountlist {
971558Srgrimes	struct mountlist *ml_next;
981558Srgrimes	char	ml_host[RPCMNT_NAMELEN+1];
991558Srgrimes	char	ml_dirp[RPCMNT_PATHLEN+1];
1001558Srgrimes};
1011558Srgrimes
1021558Srgrimesstruct dirlist {
1031558Srgrimes	struct dirlist	*dp_left;
1041558Srgrimes	struct dirlist	*dp_right;
1051558Srgrimes	int		dp_flag;
1061558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
1071558Srgrimes	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1081558Srgrimes};
1091558Srgrimes/* dp_flag bits */
1101558Srgrimes#define	DP_DEFSET	0x1
1119336Sdfr#define DP_HOSTSET	0x2
1129336Sdfr#define DP_KERB		0x4
1131558Srgrimes
1141558Srgrimesstruct exportlist {
1151558Srgrimes	struct exportlist *ex_next;
1161558Srgrimes	struct dirlist	*ex_dirl;
1171558Srgrimes	struct dirlist	*ex_defdir;
1181558Srgrimes	int		ex_flag;
1191558Srgrimes	fsid_t		ex_fs;
1201558Srgrimes	char		*ex_fsdir;
12127447Sdfr	char		*ex_indexfile;
1221558Srgrimes};
1231558Srgrimes/* ex_flag bits */
1241558Srgrimes#define	EX_LINKED	0x1
1251558Srgrimes
1261558Srgrimesstruct netmsk {
12774462Salfred	struct sockaddr_storage nt_net;
12842144Sdfr	u_int32_t	nt_mask;
12942144Sdfr	char		*nt_name;
1301558Srgrimes};
1311558Srgrimes
1321558Srgrimesunion grouptypes {
13374462Salfred	struct addrinfo *gt_addrinfo;
1341558Srgrimes	struct netmsk	gt_net;
1351558Srgrimes};
1361558Srgrimes
1371558Srgrimesstruct grouplist {
1381558Srgrimes	int gr_type;
1391558Srgrimes	union grouptypes gr_ptr;
1401558Srgrimes	struct grouplist *gr_next;
1411558Srgrimes};
1421558Srgrimes/* Group types */
1431558Srgrimes#define	GT_NULL		0x0
1441558Srgrimes#define	GT_HOST		0x1
1451558Srgrimes#define	GT_NET		0x2
1467401Swpaul#define GT_IGNORE	0x5
1471558Srgrimes
1481558Srgrimesstruct hostlist {
1499336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1501558Srgrimes	struct grouplist *ht_grp;
1511558Srgrimes	struct hostlist	 *ht_next;
1521558Srgrimes};
1531558Srgrimes
1549336Sdfrstruct fhreturn {
1559336Sdfr	int	fhr_flag;
1569336Sdfr	int	fhr_vers;
1579336Sdfr	nfsfh_t	fhr_fh;
1589336Sdfr};
1599336Sdfr
1601558Srgrimes/* Global defs */
1611558Srgrimeschar	*add_expdir __P((struct dirlist **, char *, int));
1621558Srgrimesvoid	add_dlist __P((struct dirlist **, struct dirlist *,
1639336Sdfr				struct grouplist *, int));
1641558Srgrimesvoid	add_mlist __P((char *, char *));
1651558Srgrimesint	check_dirpath __P((char *));
1661558Srgrimesint	check_options __P((struct dirlist *));
16774462Salfredint	chk_host __P((struct dirlist *, struct sockaddr *, int *, int *));
16874462Salfredint	del_mlist __P((char *, char *, struct sockaddr *));
1691558Srgrimesstruct dirlist *dirp_search __P((struct dirlist *, char *));
1701558Srgrimesint	do_mount __P((struct exportlist *, struct grouplist *, int,
17172650Sgreen		struct xucred *, char *, int, struct statfs *));
1721558Srgrimesint	do_opt __P((char **, char **, struct exportlist *, struct grouplist *,
17372650Sgreen				int *, int *, struct xucred *));
1741558Srgrimesstruct	exportlist *ex_search __P((fsid_t *));
1751558Srgrimesstruct	exportlist *get_exp __P((void));
1761558Srgrimesvoid	free_dir __P((struct dirlist *));
1771558Srgrimesvoid	free_exp __P((struct exportlist *));
1781558Srgrimesvoid	free_grp __P((struct grouplist *));
1791558Srgrimesvoid	free_host __P((struct hostlist *));
1801558Srgrimesvoid	get_exportlist __P((void));
1817401Swpaulint	get_host __P((char *, struct grouplist *, struct grouplist *));
1829336Sdfrint	get_num __P((char *));
1831558Srgrimesstruct hostlist *get_ht __P((void));
1841558Srgrimesint	get_line __P((void));
1851558Srgrimesvoid	get_mountlist __P((void));
1861558Srgrimesint	get_net __P((char *, struct netmsk *, int));
1871558Srgrimesvoid	getexp_err __P((struct exportlist *, struct grouplist *));
1881558Srgrimesstruct grouplist *get_grp __P((void));
1891558Srgrimesvoid	hang_dirp __P((struct dirlist *, struct grouplist *,
1901558Srgrimes				struct exportlist *, int));
1911558Srgrimesvoid	mntsrv __P((struct svc_req *, SVCXPRT *));
1921558Srgrimesvoid	nextfield __P((char **, char **));
1931558Srgrimesvoid	out_of_mem __P((void));
19472650Sgreenvoid	parsecred __P((char *, struct xucred *));
1951558Srgrimesint	put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
19674462Salfredint	scan_tree __P((struct dirlist *, struct sockaddr *));
19737663Scharnierstatic void usage __P((void));
1981558Srgrimesint	xdr_dir __P((XDR *, char *));
1991558Srgrimesint	xdr_explist __P((XDR *, caddr_t));
2009336Sdfrint	xdr_fhs __P((XDR *, caddr_t));
2011558Srgrimesint	xdr_mlist __P((XDR *, caddr_t));
20274462Salfredvoid	terminate __P((int));
2031558Srgrimes
2041558Srgrimes/* C library */
2051558Srgrimesint	getnetgrent();
2061558Srgrimesvoid	endnetgrent();
2071558Srgrimesvoid	setnetgrent();
2081558Srgrimes
20974462Salfredstatic int bitcmp __P((void *, void *, int));
21074462Salfredstatic int netpartcmp __P((struct sockaddr *, struct sockaddr *, int));
21174462Salfredstatic int sacmp __P((struct sockaddr *, struct sockaddr *));
21274462Salfredstatic int allones __P((struct sockaddr_storage *, int));
21374462Salfredstatic int countones __P((struct sockaddr *));
21474462Salfred
2151558Srgrimesstruct exportlist *exphead;
2161558Srgrimesstruct mountlist *mlhead;
2171558Srgrimesstruct grouplist *grphead;
2181558Srgrimeschar exname[MAXPATHLEN];
21972650Sgreenstruct xucred def_anon = {
22072650Sgreen	0,
22172650Sgreen	(uid_t)-2,
2221558Srgrimes	1,
22372650Sgreen	{ (gid_t)-2 },
22472650Sgreen	NULL
2251558Srgrimes};
22625087Sdfrint force_v2 = 0;
2279336Sdfrint resvport_only = 1;
2289336Sdfrint dir_only = 1;
22931705Sguidoint log = 0;
23074462Salfred
2311558Srgrimesint opt_flags;
23274462Salfredstatic int have_v6 = 1;
23374462Salfred#ifdef NI_WITHSCOPEID
23474462Salfredstatic const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID;
23574462Salfred#else
23674462Salfredstatic const int ninumeric = NI_NUMERICHOST;
23774462Salfred#endif
23874462Salfred
23974462Salfredint mountdlockfd;
2401558Srgrimes/* Bits for above */
2411558Srgrimes#define	OP_MAPROOT	0x01
2421558Srgrimes#define	OP_MAPALL	0x02
2431558Srgrimes#define	OP_KERB		0x04
2441558Srgrimes#define	OP_MASK		0x08
2451558Srgrimes#define	OP_NET		0x10
2461558Srgrimes#define	OP_ALLDIRS	0x40
24774462Salfred#define OP_MASKLEN	0x200
2481558Srgrimes
2491558Srgrimes#ifdef DEBUG
2501558Srgrimesint debug = 1;
2511558Srgrimesvoid	SYSLOG __P((int, const char *, ...));
2521558Srgrimes#define syslog SYSLOG
2531558Srgrimes#else
2541558Srgrimesint debug = 0;
2551558Srgrimes#endif
2561558Srgrimes
2571558Srgrimes/*
2581558Srgrimes * Mountd server for NFS mount protocol as described in:
2591558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2601558Srgrimes * The optional arguments are the exports file name
2611558Srgrimes * default: _PATH_EXPORTS
2621558Srgrimes * and "-n" to allow nonroot mount.
2631558Srgrimes */
2641558Srgrimesint
2651558Srgrimesmain(argc, argv)
2661558Srgrimes	int argc;
2671558Srgrimes	char **argv;
2681558Srgrimes{
26974462Salfred	SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
27074462Salfred	struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
27174462Salfred	int udpsock, tcpsock, udp6sock, tcp6sock;
27274462Salfred	int xcreated = 0, s;
27374462Salfred	int one = 1;
27432656Sbde	int c, error, mib[3];
27523681Speter	struct vfsconf vfc;
2761558Srgrimes
27774462Salfred	/* Check that another mountd isn't already running. */
27874462Salfred
27974462Salfred	if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
28074462Salfred		err(1, "%s", MOUNTDLOCK);
28174462Salfred
28274462Salfred	if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
28374462Salfred		errx(1, "another rpc.mountd is already running. Aborting");
28474462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
28574462Salfred	if (s < 0)
28674462Salfred		have_v6 = 0;
28774462Salfred	else
28874462Salfred		close(s);
28923681Speter	error = getvfsbyname("nfs", &vfc);
29023681Speter	if (error && vfsisloadable("nfs")) {
2912999Swollman		if(vfsload("nfs"))
2922999Swollman			err(1, "vfsload(nfs)");
2932999Swollman		endvfsent();	/* flush cache */
29423681Speter		error = getvfsbyname("nfs", &vfc);
2952999Swollman	}
29623681Speter	if (error)
2972999Swollman		errx(1, "NFS support is not available in the running kernel");
2982999Swollman
29931665Sguido	while ((c = getopt(argc, argv, "2dlnr")) != -1)
3001558Srgrimes		switch (c) {
30125087Sdfr		case '2':
30225087Sdfr			force_v2 = 1;
30325087Sdfr			break;
3049336Sdfr		case 'n':
3059336Sdfr			resvport_only = 0;
3069336Sdfr			break;
3079336Sdfr		case 'r':
3089336Sdfr			dir_only = 0;
3099336Sdfr			break;
3108688Sphk		case 'd':
3118688Sphk			debug = debug ? 0 : 1;
3128688Sphk			break;
31331656Sguido		case 'l':
31431656Sguido			log = 1;
31531656Sguido			break;
3161558Srgrimes		default:
31737663Scharnier			usage();
3181558Srgrimes		};
3191558Srgrimes	argc -= optind;
3201558Srgrimes	argv += optind;
3211558Srgrimes	grphead = (struct grouplist *)NULL;
3221558Srgrimes	exphead = (struct exportlist *)NULL;
3231558Srgrimes	mlhead = (struct mountlist *)NULL;
3241558Srgrimes	if (argc == 1) {
3251558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
3261558Srgrimes		exname[MAXPATHLEN-1] = '\0';
3271558Srgrimes	} else
3281558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3291558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3301558Srgrimes	if (debug)
33137663Scharnier		warnx("getting export list");
3321558Srgrimes	get_exportlist();
3331558Srgrimes	if (debug)
33437663Scharnier		warnx("getting mount list");
3351558Srgrimes	get_mountlist();
3361558Srgrimes	if (debug)
33737663Scharnier		warnx("here we go");
3381558Srgrimes	if (debug == 0) {
3391558Srgrimes		daemon(0, 0);
3401558Srgrimes		signal(SIGINT, SIG_IGN);
3411558Srgrimes		signal(SIGQUIT, SIG_IGN);
3421558Srgrimes	}
3431558Srgrimes	signal(SIGHUP, (void (*) __P((int))) get_exportlist);
34474462Salfred	signal(SIGTERM, terminate);
3451558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3461558Srgrimes	  if (pidfile != NULL) {
3471558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3481558Srgrimes		fclose(pidfile);
3491558Srgrimes	  }
3501558Srgrimes	}
35174462Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
35274462Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
35374462Salfred	udpsock  = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
35474462Salfred	tcpsock  = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
35574462Salfred	udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
35674462Salfred	tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
35774462Salfred	/*
35874462Salfred	 * We're doing host-based access checks here, so don't allow
35974462Salfred	 * v4-in-v6 to confuse things. The kernel will disable it
36074462Salfred	 * by default on NFS sockets too.
36174462Salfred	 */
36274462Salfred	if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
36374462Salfred		IPV6_BINDV6ONLY, &one, sizeof one) < 0){
36474462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
36574462Salfred		exit(1);
36674462Salfred	}
36774462Salfred	if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
36874462Salfred		IPV6_BINDV6ONLY, &one, sizeof one) < 0){
36974462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
37074462Salfred		exit(1);
37174462Salfred	}
37274462Salfred	udpconf  = getnetconfigent("udp");
37374462Salfred	tcpconf  = getnetconfigent("tcp");
37474462Salfred	udp6conf = getnetconfigent("udp6");
37574462Salfred	tcp6conf = getnetconfigent("tcp6");
37624759Sguido	if (!resvport_only) {
37724759Sguido		mib[0] = CTL_VFS;
37832656Sbde		mib[1] = vfc.vfc_typenum;
37924759Sguido		mib[2] = NFS_NFSPRIVPORT;
38024759Sguido		if (sysctl(mib, 3, NULL, NULL, &resvport_only,
38124759Sguido		    sizeof(resvport_only)) != 0 && errno != ENOENT) {
38224759Sguido			syslog(LOG_ERR, "sysctl: %m");
38324759Sguido			exit(1);
38424759Sguido		}
38524330Sguido	}
3869202Srgrimes	if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
3879202Srgrimes	    (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
38837663Scharnier		syslog(LOG_ERR, "can't create socket");
3891558Srgrimes		exit(1);
3901558Srgrimes	}
39174462Salfred	if (udpsock != -1 && udpconf != NULL) {
39274462Salfred		bindresvport(udpsock, NULL);
39374462Salfred		udptransp = svc_dg_create(udpsock, 0, 0);
39474462Salfred		if (udptransp != NULL) {
39574462Salfred			if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
39674462Salfred			    mntsrv, udpconf))
39774462Salfred				syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service");
39874462Salfred			else
39974462Salfred				xcreated++;
40074462Salfred			if (!force_v2) {
40174462Salfred				if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
40274462Salfred				    mntsrv, udpconf))
40374462Salfred					syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service");
40474462Salfred				else
40574462Salfred					xcreated++;
40674462Salfred			}
40774462Salfred		} else
40874462Salfred			syslog(LOG_WARNING, "can't create UDP services");
40974462Salfred
41074462Salfred	}
41174462Salfred	if (tcpsock != -1 && tcpconf != NULL) {
41274462Salfred		bindresvport(tcpsock, NULL);
41374462Salfred		listen(tcpsock, SOMAXCONN);
41474462Salfred		tcptransp = svc_vc_create(tcpsock, 0, 0);
41574462Salfred		if (tcptransp != NULL) {
41674462Salfred			if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
41774462Salfred			    mntsrv, tcpconf))
41874462Salfred				syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service");
41974462Salfred			else
42074462Salfred				xcreated++;
42174462Salfred			if (!force_v2) {
42274462Salfred				if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
42374462Salfred				    mntsrv, tcpconf))
42474462Salfred					syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service");
42574462Salfred				else
42674462Salfred					xcreated++;
42774462Salfred			}
42874462Salfred		} else
42974462Salfred			syslog(LOG_WARNING, "can't create TCP service");
43074462Salfred
43174462Salfred	}
43274462Salfred	if (udp6sock != -1 && udp6conf != NULL) {
43374462Salfred		bindresvport(udp6sock, NULL);
43474462Salfred		udp6transp = svc_dg_create(udp6sock, 0, 0);
43574462Salfred		if (udp6transp != NULL) {
43674462Salfred			if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
43774462Salfred			    mntsrv, udp6conf))
43874462Salfred				syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service");
43974462Salfred			else
44074462Salfred				xcreated++;
44174462Salfred			if (!force_v2) {
44274462Salfred				if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
44374462Salfred				    mntsrv, udp6conf))
44474462Salfred					syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service");
44574462Salfred				else
44674462Salfred					xcreated++;
44774462Salfred			}
44874462Salfred		} else
44974462Salfred			syslog(LOG_WARNING, "can't create UDP6 service");
45074462Salfred
45174462Salfred	}
45274462Salfred	if (tcp6sock != -1 && tcp6conf != NULL) {
45374462Salfred		bindresvport(tcp6sock, NULL);
45474462Salfred		listen(tcp6sock, SOMAXCONN);
45574462Salfred		tcp6transp = svc_vc_create(tcp6sock, 0, 0);
45674462Salfred		if (tcp6transp != NULL) {
45774462Salfred			if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
45874462Salfred			    mntsrv, tcp6conf))
45974462Salfred				syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service");
46074462Salfred			else
46174462Salfred				xcreated++;
46274462Salfred			if (!force_v2) {
46374462Salfred				if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
46474462Salfred				    mntsrv, tcp6conf))
46574462Salfred					syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service");
46674462Salfred					else
46774462Salfred						xcreated++;
46874462Salfred				}
46974462Salfred		} else
47074462Salfred			syslog(LOG_WARNING, "can't create TCP6 service");
47174462Salfred
47274462Salfred	}
47374462Salfred	if (xcreated == 0) {
47474462Salfred		syslog(LOG_ERR, "could not create any services");
4751558Srgrimes		exit(1);
4761558Srgrimes	}
4771558Srgrimes	svc_run();
47837663Scharnier	syslog(LOG_ERR, "mountd died");
4791558Srgrimes	exit(1);
4801558Srgrimes}
4811558Srgrimes
48237663Scharnierstatic void
48337663Scharnierusage()
48437663Scharnier{
48537663Scharnier	fprintf(stderr,
48637663Scharnier		"usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
48737663Scharnier	exit(1);
48837663Scharnier}
48937663Scharnier
4901558Srgrimes/*
4911558Srgrimes * The mount rpc service
4921558Srgrimes */
4931558Srgrimesvoid
4941558Srgrimesmntsrv(rqstp, transp)
4951558Srgrimes	struct svc_req *rqstp;
4961558Srgrimes	SVCXPRT *transp;
4971558Srgrimes{
4981558Srgrimes	struct exportlist *ep;
4991558Srgrimes	struct dirlist *dp;
5009336Sdfr	struct fhreturn fhr;
5011558Srgrimes	struct stat stb;
5021558Srgrimes	struct statfs fsb;
50374462Salfred	struct addrinfo *ai;
50474462Salfred	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
50574462Salfred	int lookup_failed = 1;
50674462Salfred	struct sockaddr *saddr;
5079336Sdfr	u_short sport;
50823681Speter	char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
50928911Sguido	int bad = 0, defset, hostset;
5109336Sdfr	sigset_t sighup_mask;
51174462Salfred	struct sockaddr_in6 *sin6;
51274462Salfred	struct sockaddr_in *sin;
5131558Srgrimes
5149336Sdfr	sigemptyset(&sighup_mask);
5159336Sdfr	sigaddset(&sighup_mask, SIGHUP);
51674462Salfred	saddr = svc_getrpccaller(transp)->buf;
51774462Salfred	switch (saddr->sa_family) {
51874462Salfred	case AF_INET6:
51974462Salfred		sin6 = (struct sockaddr_in6 *)saddr;
52074462Salfred		sport = ntohs(sin6->sin6_port);
52174462Salfred		break;
52274462Salfred	case AF_INET:
52374462Salfred		sin = (struct sockaddr_in *)saddr;
52474462Salfred		sport = ntohs(sin->sin_port);
52574462Salfred		break;
52674462Salfred	default:
52774462Salfred		syslog(LOG_ERR, "request from unknown address family");
52874462Salfred		return;
52974462Salfred	}
53074462Salfred	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
53174462Salfred	    NULL, 0, 0);
53274462Salfred	getnameinfo(saddr, saddr->sa_len, numerichost,
53374462Salfred	    sizeof numerichost, NULL, 0, NI_NUMERICHOST);
53474462Salfred	ai = NULL;
5351558Srgrimes	switch (rqstp->rq_proc) {
5361558Srgrimes	case NULLPROC:
5371558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
53837663Scharnier			syslog(LOG_ERR, "can't send reply");
5391558Srgrimes		return;
5401558Srgrimes	case RPCMNT_MOUNT:
5419336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
54231656Sguido			syslog(LOG_NOTICE,
54331656Sguido			    "mount request from %s from unprivileged port",
54474462Salfred			    numerichost);
5451558Srgrimes			svcerr_weakauth(transp);
5461558Srgrimes			return;
5471558Srgrimes		}
5481558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
54931656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
55074462Salfred			    numerichost);
5511558Srgrimes			svcerr_decode(transp);
5521558Srgrimes			return;
5531558Srgrimes		}
5541558Srgrimes
5551558Srgrimes		/*
5561558Srgrimes		 * Get the real pathname and make sure it is a directory
5579336Sdfr		 * or a regular file if the -r option was specified
5589336Sdfr		 * and it exists.
5591558Srgrimes		 */
56051968Salfred		if (realpath(rpcpath, dirpath) == NULL ||
5611558Srgrimes		    stat(dirpath, &stb) < 0 ||
5629336Sdfr		    (!S_ISDIR(stb.st_mode) &&
56374462Salfred		    (dir_only || !S_ISREG(stb.st_mode))) ||
5641558Srgrimes		    statfs(dirpath, &fsb) < 0) {
5651558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
56631656Sguido			syslog(LOG_NOTICE,
56737663Scharnier			    "mount request from %s for non existent path %s",
56874462Salfred			    numerichost, dirpath);
5691558Srgrimes			if (debug)
57037663Scharnier				warnx("stat failed on %s", dirpath);
57128911Sguido			bad = ENOENT;	/* We will send error reply later */
5721558Srgrimes		}
5731558Srgrimes
5741558Srgrimes		/* Check in the exports list */
5759336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
5761558Srgrimes		ep = ex_search(&fsb.f_fsid);
5779336Sdfr		hostset = defset = 0;
5789336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
5791558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
58074462Salfred		      chk_host(dp, saddr, &defset, &hostset)) ||
58174462Salfred		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
58274462Salfred		     scan_tree(ep->ex_dirl, saddr) == 0))) {
58328911Sguido			if (bad) {
58428911Sguido				if (!svc_sendreply(transp, xdr_long,
58528911Sguido				    (caddr_t)&bad))
58637663Scharnier					syslog(LOG_ERR, "can't send reply");
58728911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
58828911Sguido				return;
58928911Sguido			}
5909336Sdfr			if (hostset & DP_HOSTSET)
5919336Sdfr				fhr.fhr_flag = hostset;
5929336Sdfr			else
5939336Sdfr				fhr.fhr_flag = defset;
5949336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
5951558Srgrimes			/* Get the file handle */
59623681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
5979336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
5981558Srgrimes				bad = errno;
59937663Scharnier				syslog(LOG_ERR, "can't get fh for %s", dirpath);
6001558Srgrimes				if (!svc_sendreply(transp, xdr_long,
6011558Srgrimes				    (caddr_t)&bad))
60237663Scharnier					syslog(LOG_ERR, "can't send reply");
6039336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6041558Srgrimes				return;
6051558Srgrimes			}
6069336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
60737663Scharnier				syslog(LOG_ERR, "can't send reply");
60874462Salfred			if (!lookup_failed)
60974462Salfred				add_mlist(host, dirpath);
6101558Srgrimes			else
61174462Salfred				add_mlist(numerichost, dirpath);
6121558Srgrimes			if (debug)
61337663Scharnier				warnx("mount successful");
61431656Sguido			if (log)
61531656Sguido				syslog(LOG_NOTICE,
61631656Sguido				    "mount request succeeded from %s for %s",
61774462Salfred				    numerichost, dirpath);
61831656Sguido		} else {
6191558Srgrimes			bad = EACCES;
62031656Sguido			syslog(LOG_NOTICE,
62131656Sguido			    "mount request denied from %s for %s",
62274462Salfred			    numerichost, dirpath);
62331656Sguido		}
62428911Sguido
62528911Sguido		if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
62637663Scharnier			syslog(LOG_ERR, "can't send reply");
6279336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6281558Srgrimes		return;
6291558Srgrimes	case RPCMNT_DUMP:
6301558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
63137663Scharnier			syslog(LOG_ERR, "can't send reply");
63231656Sguido		else if (log)
63331656Sguido			syslog(LOG_NOTICE,
63431656Sguido			    "dump request succeeded from %s",
63574462Salfred			    numerichost);
6361558Srgrimes		return;
6371558Srgrimes	case RPCMNT_UMOUNT:
6389336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
63931656Sguido			syslog(LOG_NOTICE,
64031656Sguido			    "umount request from %s from unprivileged port",
64174462Salfred			    numerichost);
6421558Srgrimes			svcerr_weakauth(transp);
6431558Srgrimes			return;
6441558Srgrimes		}
64551968Salfred		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
64631656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
64774462Salfred			    numerichost);
6481558Srgrimes			svcerr_decode(transp);
6491558Srgrimes			return;
6501558Srgrimes		}
65151968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
65251968Salfred			syslog(LOG_NOTICE, "umount request from %s "
65351968Salfred			    "for non existent path %s",
65474462Salfred			    numerichost, dirpath);
65551968Salfred		}
6561558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
65737663Scharnier			syslog(LOG_ERR, "can't send reply");
65874462Salfred		if (!lookup_failed)
65974462Salfred			del_mlist(host, dirpath, saddr);
66074462Salfred		del_mlist(numerichost, dirpath, saddr);
66131656Sguido		if (log)
66231656Sguido			syslog(LOG_NOTICE,
66331656Sguido			    "umount request succeeded from %s for %s",
66474462Salfred			    numerichost, dirpath);
6651558Srgrimes		return;
6661558Srgrimes	case RPCMNT_UMNTALL:
6679336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
66831656Sguido			syslog(LOG_NOTICE,
66931656Sguido			    "umountall request from %s from unprivileged port",
67074462Salfred			    numerichost);
6711558Srgrimes			svcerr_weakauth(transp);
6721558Srgrimes			return;
6731558Srgrimes		}
6741558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
67537663Scharnier			syslog(LOG_ERR, "can't send reply");
67674462Salfred		if (!lookup_failed)
67774462Salfred			del_mlist(host, NULL, saddr);
67874462Salfred		del_mlist(numerichost, NULL, saddr);
67931656Sguido		if (log)
68031656Sguido			syslog(LOG_NOTICE,
68131656Sguido			    "umountall request succeeded from %s",
68274462Salfred			    numerichost);
6831558Srgrimes		return;
6841558Srgrimes	case RPCMNT_EXPORT:
6851558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
68637663Scharnier			syslog(LOG_ERR, "can't send reply");
68731656Sguido		if (log)
68831656Sguido			syslog(LOG_NOTICE,
68931656Sguido			    "export request succeeded from %s",
69074462Salfred			    numerichost);
6911558Srgrimes		return;
6921558Srgrimes	default:
6931558Srgrimes		svcerr_noproc(transp);
6941558Srgrimes		return;
6951558Srgrimes	}
6961558Srgrimes}
6971558Srgrimes
6981558Srgrimes/*
6991558Srgrimes * Xdr conversion for a dirpath string
7001558Srgrimes */
7011558Srgrimesint
7021558Srgrimesxdr_dir(xdrsp, dirp)
7031558Srgrimes	XDR *xdrsp;
7041558Srgrimes	char *dirp;
7051558Srgrimes{
7061558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
7071558Srgrimes}
7081558Srgrimes
7091558Srgrimes/*
7109336Sdfr * Xdr routine to generate file handle reply
7111558Srgrimes */
7121558Srgrimesint
7139336Sdfrxdr_fhs(xdrsp, cp)
7141558Srgrimes	XDR *xdrsp;
7159336Sdfr	caddr_t cp;
7161558Srgrimes{
7179336Sdfr	register struct fhreturn *fhrp = (struct fhreturn *)cp;
7189336Sdfr	u_long ok = 0, len, auth;
7191558Srgrimes
7201558Srgrimes	if (!xdr_long(xdrsp, &ok))
7211558Srgrimes		return (0);
7229336Sdfr	switch (fhrp->fhr_vers) {
7239336Sdfr	case 1:
7249336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
7259336Sdfr	case 3:
7269336Sdfr		len = NFSX_V3FH;
7279336Sdfr		if (!xdr_long(xdrsp, &len))
7289336Sdfr			return (0);
7299336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
7309336Sdfr			return (0);
7319336Sdfr		if (fhrp->fhr_flag & DP_KERB)
7329336Sdfr			auth = RPCAUTH_KERB4;
7339336Sdfr		else
7349336Sdfr			auth = RPCAUTH_UNIX;
7359336Sdfr		len = 1;
7369336Sdfr		if (!xdr_long(xdrsp, &len))
7379336Sdfr			return (0);
7389336Sdfr		return (xdr_long(xdrsp, &auth));
7399336Sdfr	};
7409336Sdfr	return (0);
7411558Srgrimes}
7421558Srgrimes
7431558Srgrimesint
7441558Srgrimesxdr_mlist(xdrsp, cp)
7451558Srgrimes	XDR *xdrsp;
7461558Srgrimes	caddr_t cp;
7471558Srgrimes{
7481558Srgrimes	struct mountlist *mlp;
7491558Srgrimes	int true = 1;
7501558Srgrimes	int false = 0;
7511558Srgrimes	char *strp;
7521558Srgrimes
7531558Srgrimes	mlp = mlhead;
7541558Srgrimes	while (mlp) {
7551558Srgrimes		if (!xdr_bool(xdrsp, &true))
7561558Srgrimes			return (0);
7571558Srgrimes		strp = &mlp->ml_host[0];
7581558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
7591558Srgrimes			return (0);
7601558Srgrimes		strp = &mlp->ml_dirp[0];
7611558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
7621558Srgrimes			return (0);
7631558Srgrimes		mlp = mlp->ml_next;
7641558Srgrimes	}
7651558Srgrimes	if (!xdr_bool(xdrsp, &false))
7661558Srgrimes		return (0);
7671558Srgrimes	return (1);
7681558Srgrimes}
7691558Srgrimes
7701558Srgrimes/*
7711558Srgrimes * Xdr conversion for export list
7721558Srgrimes */
7731558Srgrimesint
7741558Srgrimesxdr_explist(xdrsp, cp)
7751558Srgrimes	XDR *xdrsp;
7761558Srgrimes	caddr_t cp;
7771558Srgrimes{
7781558Srgrimes	struct exportlist *ep;
7791558Srgrimes	int false = 0;
7809336Sdfr	int putdef;
7819336Sdfr	sigset_t sighup_mask;
7821558Srgrimes
7839336Sdfr	sigemptyset(&sighup_mask);
7849336Sdfr	sigaddset(&sighup_mask, SIGHUP);
7859336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
7861558Srgrimes	ep = exphead;
7871558Srgrimes	while (ep) {
7881558Srgrimes		putdef = 0;
7891558Srgrimes		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
7901558Srgrimes			goto errout;
7911558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
7921558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
7931558Srgrimes			&putdef))
7941558Srgrimes			goto errout;
7951558Srgrimes		ep = ep->ex_next;
7961558Srgrimes	}
7979336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
7981558Srgrimes	if (!xdr_bool(xdrsp, &false))
7991558Srgrimes		return (0);
8001558Srgrimes	return (1);
8011558Srgrimeserrout:
8029336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8031558Srgrimes	return (0);
8041558Srgrimes}
8051558Srgrimes
8061558Srgrimes/*
8071558Srgrimes * Called from xdr_explist() to traverse the tree and export the
8081558Srgrimes * directory paths.
8091558Srgrimes */
8101558Srgrimesint
8111558Srgrimesput_exlist(dp, xdrsp, adp, putdefp)
8121558Srgrimes	struct dirlist *dp;
8131558Srgrimes	XDR *xdrsp;
8141558Srgrimes	struct dirlist *adp;
8151558Srgrimes	int *putdefp;
8161558Srgrimes{
8171558Srgrimes	struct grouplist *grp;
8181558Srgrimes	struct hostlist *hp;
8191558Srgrimes	int true = 1;
8201558Srgrimes	int false = 0;
8211558Srgrimes	int gotalldir = 0;
8221558Srgrimes	char *strp;
8231558Srgrimes
8241558Srgrimes	if (dp) {
8251558Srgrimes		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
8261558Srgrimes			return (1);
8271558Srgrimes		if (!xdr_bool(xdrsp, &true))
8281558Srgrimes			return (1);
8291558Srgrimes		strp = dp->dp_dirp;
8301558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
8311558Srgrimes			return (1);
8321558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
8331558Srgrimes			gotalldir = 1;
8341558Srgrimes			*putdefp = 1;
8351558Srgrimes		}
8361558Srgrimes		if ((dp->dp_flag & DP_DEFSET) == 0 &&
8371558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
8381558Srgrimes			hp = dp->dp_hosts;
8391558Srgrimes			while (hp) {
8401558Srgrimes				grp = hp->ht_grp;
8411558Srgrimes				if (grp->gr_type == GT_HOST) {
8421558Srgrimes					if (!xdr_bool(xdrsp, &true))
8431558Srgrimes						return (1);
84474462Salfred					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
8458871Srgrimes					if (!xdr_string(xdrsp, &strp,
8461558Srgrimes					    RPCMNT_NAMELEN))
8471558Srgrimes						return (1);
8481558Srgrimes				} else if (grp->gr_type == GT_NET) {
8491558Srgrimes					if (!xdr_bool(xdrsp, &true))
8501558Srgrimes						return (1);
8511558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
8528871Srgrimes					if (!xdr_string(xdrsp, &strp,
8531558Srgrimes					    RPCMNT_NAMELEN))
8541558Srgrimes						return (1);
8551558Srgrimes				}
8561558Srgrimes				hp = hp->ht_next;
8571558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
8581558Srgrimes					hp = adp->dp_hosts;
8591558Srgrimes					gotalldir = 0;
8601558Srgrimes				}
8611558Srgrimes			}
8621558Srgrimes		}
8631558Srgrimes		if (!xdr_bool(xdrsp, &false))
8641558Srgrimes			return (1);
8651558Srgrimes		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
8661558Srgrimes			return (1);
8671558Srgrimes	}
8681558Srgrimes	return (0);
8691558Srgrimes}
8701558Srgrimes
8711558Srgrimes#define LINESIZ	10240
8721558Srgrimeschar line[LINESIZ];
8731558SrgrimesFILE *exp_file;
8741558Srgrimes
8751558Srgrimes/*
8761558Srgrimes * Get the export list
8771558Srgrimes */
8781558Srgrimesvoid
8791558Srgrimesget_exportlist()
8801558Srgrimes{
8811558Srgrimes	struct exportlist *ep, *ep2;
8821558Srgrimes	struct grouplist *grp, *tgrp;
8831558Srgrimes	struct exportlist **epp;
8841558Srgrimes	struct dirlist *dirhead;
8851558Srgrimes	struct statfs fsb, *fsp;
88674462Salfred	struct addrinfo *ai;
88772650Sgreen	struct xucred anon;
8881558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
8891558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
8901558Srgrimes
89151968Salfred	dirp = NULL;
89251968Salfred	dirplen = 0;
89351968Salfred
8941558Srgrimes	/*
8951558Srgrimes	 * First, get rid of the old list
8961558Srgrimes	 */
8971558Srgrimes	ep = exphead;
8981558Srgrimes	while (ep) {
8991558Srgrimes		ep2 = ep;
9001558Srgrimes		ep = ep->ex_next;
9011558Srgrimes		free_exp(ep2);
9021558Srgrimes	}
9031558Srgrimes	exphead = (struct exportlist *)NULL;
9041558Srgrimes
9051558Srgrimes	grp = grphead;
9061558Srgrimes	while (grp) {
9071558Srgrimes		tgrp = grp;
9081558Srgrimes		grp = grp->gr_next;
9091558Srgrimes		free_grp(tgrp);
9101558Srgrimes	}
9111558Srgrimes	grphead = (struct grouplist *)NULL;
9121558Srgrimes
9131558Srgrimes	/*
9141558Srgrimes	 * And delete exports that are in the kernel for all local
9151558Srgrimes	 * file systems.
9161558Srgrimes	 * XXX: Should know how to handle all local exportable file systems
91723681Speter	 *      instead of just "ufs".
9181558Srgrimes	 */
9191558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
9201558Srgrimes	for (i = 0; i < num; i++) {
9211558Srgrimes		union {
9221558Srgrimes			struct ufs_args ua;
9231558Srgrimes			struct iso_args ia;
9241558Srgrimes			struct mfs_args ma;
9259336Sdfr			struct msdosfs_args da;
92654093Ssemenu			struct ntfs_args na;
9271558Srgrimes		} targs;
9281558Srgrimes
92923681Speter		if (!strcmp(fsp->f_fstypename, "mfs") ||
93023681Speter		    !strcmp(fsp->f_fstypename, "ufs") ||
93123681Speter		    !strcmp(fsp->f_fstypename, "msdos") ||
93254093Ssemenu		    !strcmp(fsp->f_fstypename, "ntfs") ||
93323681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
9349336Sdfr			targs.ua.fspec = NULL;
9359336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
9369336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
9371558Srgrimes				  fsp->f_flags | MNT_UPDATE,
9381558Srgrimes				  (caddr_t)&targs) < 0)
93937663Scharnier				syslog(LOG_ERR, "can't delete exports for %s",
94074462Salfred				    fsp->f_mntonname);
9411558Srgrimes		}
9421558Srgrimes		fsp++;
9431558Srgrimes	}
9441558Srgrimes
9451558Srgrimes	/*
9461558Srgrimes	 * Read in the exports file and build the list, calling
9471558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
9481558Srgrimes	 */
9491558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
95037663Scharnier		syslog(LOG_ERR, "can't open %s", exname);
9511558Srgrimes		exit(2);
9521558Srgrimes	}
9531558Srgrimes	dirhead = (struct dirlist *)NULL;
9541558Srgrimes	while (get_line()) {
9551558Srgrimes		if (debug)
95637663Scharnier			warnx("got line %s", line);
9571558Srgrimes		cp = line;
9581558Srgrimes		nextfield(&cp, &endcp);
9591558Srgrimes		if (*cp == '#')
9601558Srgrimes			goto nextline;
9611558Srgrimes
9621558Srgrimes		/*
9631558Srgrimes		 * Set defaults.
9641558Srgrimes		 */
9651558Srgrimes		has_host = FALSE;
9661558Srgrimes		anon = def_anon;
9671558Srgrimes		exflags = MNT_EXPORTED;
9681558Srgrimes		got_nondir = 0;
9691558Srgrimes		opt_flags = 0;
9701558Srgrimes		ep = (struct exportlist *)NULL;
9711558Srgrimes
9721558Srgrimes		/*
9731558Srgrimes		 * Create new exports list entry
9741558Srgrimes		 */
9751558Srgrimes		len = endcp-cp;
9761558Srgrimes		tgrp = grp = get_grp();
9771558Srgrimes		while (len > 0) {
9781558Srgrimes			if (len > RPCMNT_NAMELEN) {
9791558Srgrimes			    getexp_err(ep, tgrp);
9801558Srgrimes			    goto nextline;
9811558Srgrimes			}
9821558Srgrimes			if (*cp == '-') {
9831558Srgrimes			    if (ep == (struct exportlist *)NULL) {
9841558Srgrimes				getexp_err(ep, tgrp);
9851558Srgrimes				goto nextline;
9861558Srgrimes			    }
9871558Srgrimes			    if (debug)
98837663Scharnier				warnx("doing opt %s", cp);
9891558Srgrimes			    got_nondir = 1;
9901558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
9911558Srgrimes				&exflags, &anon)) {
9921558Srgrimes				getexp_err(ep, tgrp);
9931558Srgrimes				goto nextline;
9941558Srgrimes			    }
9951558Srgrimes			} else if (*cp == '/') {
9961558Srgrimes			    savedc = *endcp;
9971558Srgrimes			    *endcp = '\0';
9981558Srgrimes			    if (check_dirpath(cp) &&
9991558Srgrimes				statfs(cp, &fsb) >= 0) {
10001558Srgrimes				if (got_nondir) {
100137663Scharnier				    syslog(LOG_ERR, "dirs must be first");
10021558Srgrimes				    getexp_err(ep, tgrp);
10031558Srgrimes				    goto nextline;
10041558Srgrimes				}
10051558Srgrimes				if (ep) {
10061558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
10071558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
10081558Srgrimes					getexp_err(ep, tgrp);
10091558Srgrimes					goto nextline;
10101558Srgrimes				    }
10111558Srgrimes				} else {
10121558Srgrimes				    /*
10131558Srgrimes				     * See if this directory is already
10141558Srgrimes				     * in the list.
10151558Srgrimes				     */
10161558Srgrimes				    ep = ex_search(&fsb.f_fsid);
10171558Srgrimes				    if (ep == (struct exportlist *)NULL) {
10181558Srgrimes					ep = get_exp();
10191558Srgrimes					ep->ex_fs = fsb.f_fsid;
10201558Srgrimes					ep->ex_fsdir = (char *)
10211558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
10221558Srgrimes					if (ep->ex_fsdir)
10231558Srgrimes					    strcpy(ep->ex_fsdir,
10241558Srgrimes						fsb.f_mntonname);
10251558Srgrimes					else
10261558Srgrimes					    out_of_mem();
10271558Srgrimes					if (debug)
102874462Salfred						warnx("making new ep fs=0x%x,0x%x",
102974462Salfred						    fsb.f_fsid.val[0],
103074462Salfred						    fsb.f_fsid.val[1]);
10311558Srgrimes				    } else if (debug)
103237663Scharnier					warnx("found ep fs=0x%x,0x%x",
10331558Srgrimes					    fsb.f_fsid.val[0],
10341558Srgrimes					    fsb.f_fsid.val[1]);
10351558Srgrimes				}
10361558Srgrimes
10371558Srgrimes				/*
10381558Srgrimes				 * Add dirpath to export mount point.
10391558Srgrimes				 */
10401558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
10411558Srgrimes				dirplen = len;
10421558Srgrimes			    } else {
10431558Srgrimes				getexp_err(ep, tgrp);
10441558Srgrimes				goto nextline;
10451558Srgrimes			    }
10461558Srgrimes			    *endcp = savedc;
10471558Srgrimes			} else {
10481558Srgrimes			    savedc = *endcp;
10491558Srgrimes			    *endcp = '\0';
10501558Srgrimes			    got_nondir = 1;
10511558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10521558Srgrimes				getexp_err(ep, tgrp);
10531558Srgrimes				goto nextline;
10541558Srgrimes			    }
10551558Srgrimes
10561558Srgrimes			    /*
10571558Srgrimes			     * Get the host or netgroup.
10581558Srgrimes			     */
10591558Srgrimes			    setnetgrent(cp);
10601558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
10611558Srgrimes			    do {
10621558Srgrimes				if (has_host) {
10631558Srgrimes				    grp->gr_next = get_grp();
10641558Srgrimes				    grp = grp->gr_next;
10651558Srgrimes				}
10661558Srgrimes				if (netgrp) {
106737003Sjoerg				    if (hst == 0) {
106837663Scharnier					syslog(LOG_ERR,
106937663Scharnier				"null hostname in netgroup %s, skipping", cp);
107037004Sjoerg					grp->gr_type = GT_IGNORE;
107137003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
107237663Scharnier					syslog(LOG_ERR,
107337663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
107429317Sjlemon					grp->gr_type = GT_IGNORE;
10751558Srgrimes				    }
10767401Swpaul				} else if (get_host(cp, grp, tgrp)) {
107737663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
107829317Sjlemon				    grp->gr_type = GT_IGNORE;
10791558Srgrimes				}
10801558Srgrimes				has_host = TRUE;
10811558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
10821558Srgrimes			    endnetgrent();
10831558Srgrimes			    *endcp = savedc;
10841558Srgrimes			}
10851558Srgrimes			cp = endcp;
10861558Srgrimes			nextfield(&cp, &endcp);
10871558Srgrimes			len = endcp - cp;
10881558Srgrimes		}
10891558Srgrimes		if (check_options(dirhead)) {
10901558Srgrimes			getexp_err(ep, tgrp);
10911558Srgrimes			goto nextline;
10921558Srgrimes		}
10931558Srgrimes		if (!has_host) {
10941558Srgrimes			grp->gr_type = GT_HOST;
10951558Srgrimes			if (debug)
109637663Scharnier				warnx("adding a default entry");
10971558Srgrimes			/* add a default group and make the grp list NULL */
109874462Salfred			ai = malloc(sizeof(struct addrinfo));
109974462Salfred			ai->ai_flags = 0;
110074462Salfred			ai->ai_family = AF_INET;        /* XXXX */
110174462Salfred			ai->ai_socktype = SOCK_DGRAM;
110274462Salfred			/* setting the length to 0 will match anything */
110374462Salfred			ai->ai_addrlen = 0;
110474462Salfred			ai->ai_flags = AI_CANONNAME;
110574462Salfred			ai->ai_canonname = strdup("Default");
110674462Salfred			ai->ai_addr = NULL;
110774462Salfred			ai->ai_next = NULL;
110874462Salfred			grp->gr_ptr.gt_addrinfo = ai;
11091558Srgrimes
11101558Srgrimes		/*
11111558Srgrimes		 * Don't allow a network export coincide with a list of
11121558Srgrimes		 * host(s) on the same line.
11131558Srgrimes		 */
11141558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
11151558Srgrimes			getexp_err(ep, tgrp);
11161558Srgrimes			goto nextline;
111729317Sjlemon
111874462Salfred		/*
111974462Salfred		 * If an export list was specified on this line, make sure
112029317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
112129317Sjlemon		 */
112229317Sjlemon		} else {
112329317Sjlemon			grp = tgrp;
112474462Salfred			while (grp && grp->gr_type == GT_IGNORE)
112529317Sjlemon				grp = grp->gr_next;
112629317Sjlemon			if (! grp) {
112729317Sjlemon			    getexp_err(ep, tgrp);
112829317Sjlemon			    goto nextline;
112929317Sjlemon			}
11301558Srgrimes		}
11311558Srgrimes
11321558Srgrimes		/*
11331558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
11341558Srgrimes		 * After loop, tgrp points to the start of the list and
11351558Srgrimes		 * grp points to the last entry in the list.
11361558Srgrimes		 */
11371558Srgrimes		grp = tgrp;
11381558Srgrimes		do {
11391558Srgrimes		    if (do_mount(ep, grp, exflags, &anon, dirp,
11401558Srgrimes			dirplen, &fsb)) {
11411558Srgrimes			getexp_err(ep, tgrp);
11421558Srgrimes			goto nextline;
11431558Srgrimes		    }
11441558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
11451558Srgrimes
11461558Srgrimes		/*
11471558Srgrimes		 * Success. Update the data structures.
11481558Srgrimes		 */
11491558Srgrimes		if (has_host) {
11509336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
11511558Srgrimes			grp->gr_next = grphead;
11521558Srgrimes			grphead = tgrp;
11531558Srgrimes		} else {
11541558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
11559336Sdfr				opt_flags);
11561558Srgrimes			free_grp(grp);
11571558Srgrimes		}
11581558Srgrimes		dirhead = (struct dirlist *)NULL;
11591558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
11601558Srgrimes			ep2 = exphead;
11611558Srgrimes			epp = &exphead;
11621558Srgrimes
11631558Srgrimes			/*
11641558Srgrimes			 * Insert in the list in alphabetical order.
11651558Srgrimes			 */
11661558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
11671558Srgrimes				epp = &ep2->ex_next;
11681558Srgrimes				ep2 = ep2->ex_next;
11691558Srgrimes			}
11701558Srgrimes			if (ep2)
11711558Srgrimes				ep->ex_next = ep2;
11721558Srgrimes			*epp = ep;
11731558Srgrimes			ep->ex_flag |= EX_LINKED;
11741558Srgrimes		}
11751558Srgrimesnextline:
11761558Srgrimes		if (dirhead) {
11771558Srgrimes			free_dir(dirhead);
11781558Srgrimes			dirhead = (struct dirlist *)NULL;
11791558Srgrimes		}
11801558Srgrimes	}
11811558Srgrimes	fclose(exp_file);
11821558Srgrimes}
11831558Srgrimes
11841558Srgrimes/*
11851558Srgrimes * Allocate an export list element
11861558Srgrimes */
11871558Srgrimesstruct exportlist *
11881558Srgrimesget_exp()
11891558Srgrimes{
11901558Srgrimes	struct exportlist *ep;
11911558Srgrimes
11921558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
11931558Srgrimes	if (ep == (struct exportlist *)NULL)
11941558Srgrimes		out_of_mem();
119523681Speter	memset(ep, 0, sizeof(struct exportlist));
11961558Srgrimes	return (ep);
11971558Srgrimes}
11981558Srgrimes
11991558Srgrimes/*
12001558Srgrimes * Allocate a group list element
12011558Srgrimes */
12021558Srgrimesstruct grouplist *
12031558Srgrimesget_grp()
12041558Srgrimes{
12051558Srgrimes	struct grouplist *gp;
12061558Srgrimes
12071558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
12081558Srgrimes	if (gp == (struct grouplist *)NULL)
12091558Srgrimes		out_of_mem();
121023681Speter	memset(gp, 0, sizeof(struct grouplist));
12111558Srgrimes	return (gp);
12121558Srgrimes}
12131558Srgrimes
12141558Srgrimes/*
12151558Srgrimes * Clean up upon an error in get_exportlist().
12161558Srgrimes */
12171558Srgrimesvoid
12181558Srgrimesgetexp_err(ep, grp)
12191558Srgrimes	struct exportlist *ep;
12201558Srgrimes	struct grouplist *grp;
12211558Srgrimes{
12221558Srgrimes	struct grouplist *tgrp;
12231558Srgrimes
122437663Scharnier	syslog(LOG_ERR, "bad exports list line %s", line);
12251558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
12261558Srgrimes		free_exp(ep);
12271558Srgrimes	while (grp) {
12281558Srgrimes		tgrp = grp;
12291558Srgrimes		grp = grp->gr_next;
12301558Srgrimes		free_grp(tgrp);
12311558Srgrimes	}
12321558Srgrimes}
12331558Srgrimes
12341558Srgrimes/*
12351558Srgrimes * Search the export list for a matching fs.
12361558Srgrimes */
12371558Srgrimesstruct exportlist *
12381558Srgrimesex_search(fsid)
12391558Srgrimes	fsid_t *fsid;
12401558Srgrimes{
12411558Srgrimes	struct exportlist *ep;
12421558Srgrimes
12431558Srgrimes	ep = exphead;
12441558Srgrimes	while (ep) {
12451558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
12461558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
12471558Srgrimes			return (ep);
12481558Srgrimes		ep = ep->ex_next;
12491558Srgrimes	}
12501558Srgrimes	return (ep);
12511558Srgrimes}
12521558Srgrimes
12531558Srgrimes/*
12541558Srgrimes * Add a directory path to the list.
12551558Srgrimes */
12561558Srgrimeschar *
12571558Srgrimesadd_expdir(dpp, cp, len)
12581558Srgrimes	struct dirlist **dpp;
12591558Srgrimes	char *cp;
12601558Srgrimes	int len;
12611558Srgrimes{
12621558Srgrimes	struct dirlist *dp;
12631558Srgrimes
12641558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
126537663Scharnier	if (dp == (struct dirlist *)NULL)
126637663Scharnier		out_of_mem();
12671558Srgrimes	dp->dp_left = *dpp;
12681558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
12691558Srgrimes	dp->dp_flag = 0;
12701558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
12711558Srgrimes	strcpy(dp->dp_dirp, cp);
12721558Srgrimes	*dpp = dp;
12731558Srgrimes	return (dp->dp_dirp);
12741558Srgrimes}
12751558Srgrimes
12761558Srgrimes/*
12771558Srgrimes * Hang the dir list element off the dirpath binary tree as required
12781558Srgrimes * and update the entry for host.
12791558Srgrimes */
12801558Srgrimesvoid
12819336Sdfrhang_dirp(dp, grp, ep, flags)
12821558Srgrimes	struct dirlist *dp;
12831558Srgrimes	struct grouplist *grp;
12841558Srgrimes	struct exportlist *ep;
12859336Sdfr	int flags;
12861558Srgrimes{
12871558Srgrimes	struct hostlist *hp;
12881558Srgrimes	struct dirlist *dp2;
12891558Srgrimes
12909336Sdfr	if (flags & OP_ALLDIRS) {
12911558Srgrimes		if (ep->ex_defdir)
12921558Srgrimes			free((caddr_t)dp);
12931558Srgrimes		else
12941558Srgrimes			ep->ex_defdir = dp;
12959336Sdfr		if (grp == (struct grouplist *)NULL) {
12961558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
12979336Sdfr			if (flags & OP_KERB)
12989336Sdfr				ep->ex_defdir->dp_flag |= DP_KERB;
12999336Sdfr		} else while (grp) {
13001558Srgrimes			hp = get_ht();
13019336Sdfr			if (flags & OP_KERB)
13029336Sdfr				hp->ht_flag |= DP_KERB;
13031558Srgrimes			hp->ht_grp = grp;
13041558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
13051558Srgrimes			ep->ex_defdir->dp_hosts = hp;
13061558Srgrimes			grp = grp->gr_next;
13071558Srgrimes		}
13081558Srgrimes	} else {
13091558Srgrimes
13101558Srgrimes		/*
131137663Scharnier		 * Loop through the directories adding them to the tree.
13121558Srgrimes		 */
13131558Srgrimes		while (dp) {
13141558Srgrimes			dp2 = dp->dp_left;
13159336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
13161558Srgrimes			dp = dp2;
13171558Srgrimes		}
13181558Srgrimes	}
13191558Srgrimes}
13201558Srgrimes
13211558Srgrimes/*
13221558Srgrimes * Traverse the binary tree either updating a node that is already there
13231558Srgrimes * for the new directory or adding the new node.
13241558Srgrimes */
13251558Srgrimesvoid
13269336Sdfradd_dlist(dpp, newdp, grp, flags)
13271558Srgrimes	struct dirlist **dpp;
13281558Srgrimes	struct dirlist *newdp;
13291558Srgrimes	struct grouplist *grp;
13309336Sdfr	int flags;
13311558Srgrimes{
13321558Srgrimes	struct dirlist *dp;
13331558Srgrimes	struct hostlist *hp;
13341558Srgrimes	int cmp;
13351558Srgrimes
13361558Srgrimes	dp = *dpp;
13371558Srgrimes	if (dp) {
13381558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
13391558Srgrimes		if (cmp > 0) {
13409336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
13411558Srgrimes			return;
13421558Srgrimes		} else if (cmp < 0) {
13439336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
13441558Srgrimes			return;
13451558Srgrimes		} else
13461558Srgrimes			free((caddr_t)newdp);
13471558Srgrimes	} else {
13481558Srgrimes		dp = newdp;
13491558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
13501558Srgrimes		*dpp = dp;
13511558Srgrimes	}
13521558Srgrimes	if (grp) {
13531558Srgrimes
13541558Srgrimes		/*
13551558Srgrimes		 * Hang all of the host(s) off of the directory point.
13561558Srgrimes		 */
13571558Srgrimes		do {
13581558Srgrimes			hp = get_ht();
13599336Sdfr			if (flags & OP_KERB)
13609336Sdfr				hp->ht_flag |= DP_KERB;
13611558Srgrimes			hp->ht_grp = grp;
13621558Srgrimes			hp->ht_next = dp->dp_hosts;
13631558Srgrimes			dp->dp_hosts = hp;
13641558Srgrimes			grp = grp->gr_next;
13651558Srgrimes		} while (grp);
13669336Sdfr	} else {
13671558Srgrimes		dp->dp_flag |= DP_DEFSET;
13689336Sdfr		if (flags & OP_KERB)
13699336Sdfr			dp->dp_flag |= DP_KERB;
13709336Sdfr	}
13711558Srgrimes}
13721558Srgrimes
13731558Srgrimes/*
13741558Srgrimes * Search for a dirpath on the export point.
13751558Srgrimes */
137674462Salfredvoid *
137774462Salfredtest()
137874462Salfred{
137974462Salfred}
138074462Salfred
138174462Salfred/*
138274462Salfred * Search for a dirpath on the export point.
138374462Salfred */
13841558Srgrimesstruct dirlist *
138574462Salfreddirp_search(dp, dirp)
13861558Srgrimes	struct dirlist *dp;
138774462Salfred	char *dirp;
13881558Srgrimes{
13891558Srgrimes	int cmp;
13901558Srgrimes
13911558Srgrimes	if (dp) {
139274462Salfred		cmp = strcmp(dp->dp_dirp, dirp);
13931558Srgrimes		if (cmp > 0)
139474462Salfred			return (dirp_search(dp->dp_left, dirp));
13951558Srgrimes		else if (cmp < 0)
139674462Salfred			return (dirp_search(dp->dp_right, dirp));
13971558Srgrimes		else
13981558Srgrimes			return (dp);
13991558Srgrimes	}
14001558Srgrimes	return (dp);
14011558Srgrimes}
14021558Srgrimes
14031558Srgrimes/*
140474462Salfred * Some helper functions for netmasks. They all assume masks in network
140574462Salfred * order (big endian).
140674462Salfred */
140774462Salfredstatic int
140874462Salfredbitcmp(void *dst, void *src, int bitlen)
140974462Salfred{
141074462Salfred	int i;
141174462Salfred	u_int8_t *p1 = dst, *p2 = src;
141274462Salfred	u_int8_t bitmask;
141374462Salfred	int bytelen, bitsleft;
141474462Salfred
141574462Salfred	bytelen = bitlen / 8;
141674462Salfred	bitsleft = bitlen % 8;
141774462Salfred
141874462Salfred	if (debug) {
141974462Salfred		printf("comparing:\n");
142074462Salfred		for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
142174462Salfred			printf("%02x", p1[i]);
142274462Salfred		printf("\n");
142374462Salfred		for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
142474462Salfred			printf("%02x", p2[i]);
142574462Salfred		printf("\n");
142674462Salfred	}
142774462Salfred
142874462Salfred	for (i = 0; i < bytelen; i++) {
142974462Salfred		if (*p1 != *p2)
143074462Salfred			return 1;
143174462Salfred		p1++;
143274462Salfred		p2++;
143374462Salfred	}
143474462Salfred
143574462Salfred	for (i = 0; i < bitsleft; i++) {
143674462Salfred		bitmask = 1 << (7 - i);
143774462Salfred		if ((*p1 & bitmask) != (*p2 & bitmask))
143874462Salfred			return 1;
143974462Salfred	}
144074462Salfred
144174462Salfred	return 0;
144274462Salfred}
144374462Salfred
144474462Salfred/*
14451558Srgrimes * Scan for a host match in a directory tree.
14461558Srgrimes */
14471558Srgrimesint
14489336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
14491558Srgrimes	struct dirlist *dp;
145074462Salfred	struct sockaddr *saddr;
14511558Srgrimes	int *defsetp;
14529336Sdfr	int *hostsetp;
14531558Srgrimes{
14541558Srgrimes	struct hostlist *hp;
14551558Srgrimes	struct grouplist *grp;
145674462Salfred	struct addrinfo *ai;
14571558Srgrimes
14581558Srgrimes	if (dp) {
14591558Srgrimes		if (dp->dp_flag & DP_DEFSET)
14609336Sdfr			*defsetp = dp->dp_flag;
14611558Srgrimes		hp = dp->dp_hosts;
14621558Srgrimes		while (hp) {
14631558Srgrimes			grp = hp->ht_grp;
14641558Srgrimes			switch (grp->gr_type) {
14651558Srgrimes			case GT_HOST:
146674462Salfred				ai = grp->gr_ptr.gt_addrinfo;
146774462Salfred				for (; ai; ai = ai->ai_next) {
146874462Salfred					if (!sacmp(ai->ai_addr, saddr)) {
146974462Salfred						*hostsetp =
147074462Salfred						    (hp->ht_flag | DP_HOSTSET);
147174462Salfred						return (1);
147274462Salfred					}
14739336Sdfr				}
14741558Srgrimes			    break;
14751558Srgrimes			case GT_NET:
147674462Salfred				if (!netpartcmp(saddr,
147774462Salfred				    (struct sockaddr *) &grp->gr_ptr.gt_net.nt_net,
147874462Salfred				     grp->gr_ptr.gt_net.nt_mask)) {
147974462Salfred					*hostsetp = (hp->ht_flag | DP_HOSTSET);
148074462Salfred					return (1);
148174462Salfred				}
14821558Srgrimes			    break;
14831558Srgrimes			};
14841558Srgrimes			hp = hp->ht_next;
14851558Srgrimes		}
14861558Srgrimes	}
14871558Srgrimes	return (0);
14881558Srgrimes}
14891558Srgrimes
14901558Srgrimes/*
14911558Srgrimes * Scan tree for a host that matches the address.
14921558Srgrimes */
14931558Srgrimesint
14941558Srgrimesscan_tree(dp, saddr)
14951558Srgrimes	struct dirlist *dp;
149674462Salfred	struct sockaddr *saddr;
14971558Srgrimes{
14989336Sdfr	int defset, hostset;
14991558Srgrimes
15001558Srgrimes	if (dp) {
15011558Srgrimes		if (scan_tree(dp->dp_left, saddr))
15021558Srgrimes			return (1);
15039336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
15041558Srgrimes			return (1);
15051558Srgrimes		if (scan_tree(dp->dp_right, saddr))
15061558Srgrimes			return (1);
15071558Srgrimes	}
15081558Srgrimes	return (0);
15091558Srgrimes}
15101558Srgrimes
15111558Srgrimes/*
15121558Srgrimes * Traverse the dirlist tree and free it up.
15131558Srgrimes */
15141558Srgrimesvoid
15151558Srgrimesfree_dir(dp)
15161558Srgrimes	struct dirlist *dp;
15171558Srgrimes{
15181558Srgrimes
15191558Srgrimes	if (dp) {
15201558Srgrimes		free_dir(dp->dp_left);
15211558Srgrimes		free_dir(dp->dp_right);
15221558Srgrimes		free_host(dp->dp_hosts);
15231558Srgrimes		free((caddr_t)dp);
15241558Srgrimes	}
15251558Srgrimes}
15261558Srgrimes
15271558Srgrimes/*
15281558Srgrimes * Parse the option string and update fields.
15291558Srgrimes * Option arguments may either be -<option>=<value> or
15301558Srgrimes * -<option> <value>
15311558Srgrimes */
15321558Srgrimesint
15331558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
15341558Srgrimes	char **cpp, **endcpp;
15351558Srgrimes	struct exportlist *ep;
15361558Srgrimes	struct grouplist *grp;
15371558Srgrimes	int *has_hostp;
15381558Srgrimes	int *exflagsp;
153972650Sgreen	struct xucred *cr;
15401558Srgrimes{
15411558Srgrimes	char *cpoptarg, *cpoptend;
15421558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
15431558Srgrimes	int allflag, usedarg;
15441558Srgrimes
154551968Salfred	savedc2 = '\0';
15461558Srgrimes	cpopt = *cpp;
15471558Srgrimes	cpopt++;
15481558Srgrimes	cp = *endcpp;
15491558Srgrimes	savedc = *cp;
15501558Srgrimes	*cp = '\0';
15511558Srgrimes	while (cpopt && *cpopt) {
15521558Srgrimes		allflag = 1;
15531558Srgrimes		usedarg = -2;
155437663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
15551558Srgrimes			*cpoptend++ = '\0';
155637663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15571558Srgrimes				*cpoptarg++ = '\0';
15581558Srgrimes		} else {
155937663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15601558Srgrimes				*cpoptarg++ = '\0';
15611558Srgrimes			else {
15621558Srgrimes				*cp = savedc;
15631558Srgrimes				nextfield(&cp, &endcp);
15641558Srgrimes				**endcpp = '\0';
15651558Srgrimes				if (endcp > cp && *cp != '-') {
15661558Srgrimes					cpoptarg = cp;
15671558Srgrimes					savedc2 = *endcp;
15681558Srgrimes					*endcp = '\0';
15691558Srgrimes					usedarg = 0;
15701558Srgrimes				}
15711558Srgrimes			}
15721558Srgrimes		}
15731558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
15741558Srgrimes			*exflagsp |= MNT_EXRDONLY;
15751558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
15761558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
15771558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
15781558Srgrimes			usedarg++;
15791558Srgrimes			parsecred(cpoptarg, cr);
15801558Srgrimes			if (allflag == 0) {
15811558Srgrimes				*exflagsp |= MNT_EXPORTANON;
15821558Srgrimes				opt_flags |= OP_MAPALL;
15831558Srgrimes			} else
15841558Srgrimes				opt_flags |= OP_MAPROOT;
15851558Srgrimes		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
15861558Srgrimes			*exflagsp |= MNT_EXKERB;
15871558Srgrimes			opt_flags |= OP_KERB;
15881558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
15891558Srgrimes			!strcmp(cpopt, "m"))) {
15901558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
159137663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
15921558Srgrimes				return (1);
15931558Srgrimes			}
15941558Srgrimes			usedarg++;
15951558Srgrimes			opt_flags |= OP_MASK;
15961558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
15971558Srgrimes			!strcmp(cpopt, "n"))) {
159874462Salfred			if (strchr(cpoptarg, '/') != NULL) {
159974462Salfred				if (debug)
160074462Salfred					fprintf(stderr, "setting OP_MASKLEN\n");
160174462Salfred				opt_flags |= OP_MASKLEN;
160274462Salfred			}
16031558Srgrimes			if (grp->gr_type != GT_NULL) {
160437663Scharnier				syslog(LOG_ERR, "network/host conflict");
16051558Srgrimes				return (1);
16061558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
160737663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
16081558Srgrimes				return (1);
16091558Srgrimes			}
16101558Srgrimes			grp->gr_type = GT_NET;
16111558Srgrimes			*has_hostp = 1;
16121558Srgrimes			usedarg++;
16131558Srgrimes			opt_flags |= OP_NET;
16141558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
16151558Srgrimes			opt_flags |= OP_ALLDIRS;
161627447Sdfr		} else if (!strcmp(cpopt, "public")) {
161727447Sdfr			*exflagsp |= MNT_EXPUBLIC;
161827447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
161927447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
162027447Sdfr			opt_flags |= OP_MAPALL;
162127447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
162227447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
16231558Srgrimes		} else {
162437663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
16251558Srgrimes			return (1);
16261558Srgrimes		}
16271558Srgrimes		if (usedarg >= 0) {
16281558Srgrimes			*endcp = savedc2;
16291558Srgrimes			**endcpp = savedc;
16301558Srgrimes			if (usedarg > 0) {
16311558Srgrimes				*cpp = cp;
16321558Srgrimes				*endcpp = endcp;
16331558Srgrimes			}
16341558Srgrimes			return (0);
16351558Srgrimes		}
16361558Srgrimes		cpopt = cpoptend;
16371558Srgrimes	}
16381558Srgrimes	**endcpp = savedc;
16391558Srgrimes	return (0);
16401558Srgrimes}
16411558Srgrimes
16421558Srgrimes/*
16431558Srgrimes * Translate a character string to the corresponding list of network
16441558Srgrimes * addresses for a hostname.
16451558Srgrimes */
16461558Srgrimesint
16477401Swpaulget_host(cp, grp, tgrp)
16481558Srgrimes	char *cp;
16491558Srgrimes	struct grouplist *grp;
16507401Swpaul	struct grouplist *tgrp;
16511558Srgrimes{
16527401Swpaul	struct grouplist *checkgrp;
165374462Salfred	struct addrinfo *ai, hints;
165474462Salfred	int ecode;
165574462Salfred	char host[NI_MAXHOST];
16561558Srgrimes	int i;
16571558Srgrimes	char *aptr[2];
16581558Srgrimes
165974462Salfred	if (grp->gr_type != GT_NULL) {
166074462Salfred		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
16611558Srgrimes		return (1);
16621558Srgrimes	}
166374462Salfred	memset(&hints, 0, sizeof hints);
166474462Salfred	hints.ai_flags = AI_CANONNAME;
166574462Salfred	hints.ai_protocol = IPPROTO_UDP;
166674462Salfred	ecode = getaddrinfo(cp, NULL, &hints, &ai);
166774462Salfred	if (ecode != 0) {
166874462Salfred		syslog(LOG_ERR,"can't get address info for "
166974462Salfred		    "host %s", cp);
167074462Salfred		return 1;
167174462Salfred	}
16721558Srgrimes	grp->gr_type = GT_HOST;
167374462Salfred	grp->gr_ptr.gt_addrinfo = ai;
167474462Salfred	while (ai != NULL) {
167574462Salfred		if (ai->ai_canonname == NULL) {
167674462Salfred			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
167774462Salfred			    sizeof host, NULL, 0, ninumeric) != 0)
167874462Salfred				strlcpy(host, "?", sizeof(host));
167974462Salfred			ai->ai_canonname = strdup(host);
168074462Salfred			ai->ai_flags |= AI_CANONNAME;
168174462Salfred		} else
168274462Salfred			ai->ai_flags &= ~AI_CANONNAME;
168374462Salfred		if (debug)
168474462Salfred			(void)fprintf(stderr, "got host %s\n", ai->ai_canonname);
168574462Salfred		ai = ai->ai_next;
16861558Srgrimes	}
16871558Srgrimes	return (0);
16881558Srgrimes}
16891558Srgrimes
16901558Srgrimes/*
16911558Srgrimes * Free up an exports list component
16921558Srgrimes */
16931558Srgrimesvoid
16941558Srgrimesfree_exp(ep)
16951558Srgrimes	struct exportlist *ep;
16961558Srgrimes{
16971558Srgrimes
16981558Srgrimes	if (ep->ex_defdir) {
16991558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
17001558Srgrimes		free((caddr_t)ep->ex_defdir);
17011558Srgrimes	}
17021558Srgrimes	if (ep->ex_fsdir)
17031558Srgrimes		free(ep->ex_fsdir);
170427447Sdfr	if (ep->ex_indexfile)
170527447Sdfr		free(ep->ex_indexfile);
17061558Srgrimes	free_dir(ep->ex_dirl);
17071558Srgrimes	free((caddr_t)ep);
17081558Srgrimes}
17091558Srgrimes
17101558Srgrimes/*
17111558Srgrimes * Free hosts.
17121558Srgrimes */
17131558Srgrimesvoid
17141558Srgrimesfree_host(hp)
17151558Srgrimes	struct hostlist *hp;
17161558Srgrimes{
17171558Srgrimes	struct hostlist *hp2;
17181558Srgrimes
17191558Srgrimes	while (hp) {
17201558Srgrimes		hp2 = hp;
17211558Srgrimes		hp = hp->ht_next;
17221558Srgrimes		free((caddr_t)hp2);
17231558Srgrimes	}
17241558Srgrimes}
17251558Srgrimes
17261558Srgrimesstruct hostlist *
17271558Srgrimesget_ht()
17281558Srgrimes{
17291558Srgrimes	struct hostlist *hp;
17301558Srgrimes
17311558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
17321558Srgrimes	if (hp == (struct hostlist *)NULL)
17331558Srgrimes		out_of_mem();
17341558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
17359336Sdfr	hp->ht_flag = 0;
17361558Srgrimes	return (hp);
17371558Srgrimes}
17381558Srgrimes
17391558Srgrimes/*
17401558Srgrimes * Out of memory, fatal
17411558Srgrimes */
17421558Srgrimesvoid
17431558Srgrimesout_of_mem()
17441558Srgrimes{
17451558Srgrimes
174637663Scharnier	syslog(LOG_ERR, "out of memory");
17471558Srgrimes	exit(2);
17481558Srgrimes}
17491558Srgrimes
17501558Srgrimes/*
17511558Srgrimes * Do the mount syscall with the update flag to push the export info into
17521558Srgrimes * the kernel.
17531558Srgrimes */
17541558Srgrimesint
17551558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
17561558Srgrimes	struct exportlist *ep;
17571558Srgrimes	struct grouplist *grp;
17581558Srgrimes	int exflags;
175972650Sgreen	struct xucred *anoncrp;
17601558Srgrimes	char *dirp;
17611558Srgrimes	int dirplen;
17621558Srgrimes	struct statfs *fsb;
17631558Srgrimes{
176474462Salfred	struct sockaddr *addrp;
176574462Salfred	struct sockaddr_storage ss;
176674462Salfred	struct addrinfo *ai;
176774462Salfred	int addrlen;
176874462Salfred	char *cp = NULL;
17691558Srgrimes	int done;
17701558Srgrimes	char savedc = '\0';
17711558Srgrimes	union {
17721558Srgrimes		struct ufs_args ua;
17731558Srgrimes		struct iso_args ia;
17741558Srgrimes		struct mfs_args ma;
17759336Sdfr#ifdef __NetBSD__
17769336Sdfr		struct msdosfs_args da;
177774462Salfred		struct adosfs_args aa;
17789336Sdfr#endif
177954093Ssemenu		struct ntfs_args na;
17801558Srgrimes	} args;
17811558Srgrimes
17821558Srgrimes	args.ua.fspec = 0;
17831558Srgrimes	args.ua.export.ex_flags = exflags;
17841558Srgrimes	args.ua.export.ex_anon = *anoncrp;
178527447Sdfr	args.ua.export.ex_indexfile = ep->ex_indexfile;
178674462Salfred	if (grp->gr_type == GT_HOST) {
178774462Salfred		ai = grp->gr_ptr.gt_addrinfo;
178874462Salfred		addrp = ai->ai_addr;
178974462Salfred		addrlen = ai->ai_addrlen;
179074462Salfred	} else
179174462Salfred		addrp = NULL;
17921558Srgrimes	done = FALSE;
17931558Srgrimes	while (!done) {
17941558Srgrimes		switch (grp->gr_type) {
17951558Srgrimes		case GT_HOST:
179674462Salfred			if (addrp != NULL && addrp->sa_family == AF_INET6 &&
179774462Salfred			    have_v6 == 0)
179874462Salfred				goto skip;
179974462Salfred			args.ua.export.ex_addr = addrp;
180074462Salfred			args.ua.export.ex_addrlen = addrlen;
18011558Srgrimes			args.ua.export.ex_masklen = 0;
18021558Srgrimes			break;
18031558Srgrimes		case GT_NET:
180474462Salfred			args.ua.export.ex_addr = (struct sockaddr *)
180574462Salfred			    &grp->gr_ptr.gt_net.nt_net;
180674462Salfred			if (args.ua.export.ex_addr->sa_family == AF_INET6 &&
180774462Salfred			    have_v6 == 0)
180874462Salfred				goto skip;
180974462Salfred			args.ua.export.ex_addrlen =
181074462Salfred			    args.ua.export.ex_addr->sa_len;
181174462Salfred			memset(&ss, 0, sizeof ss);
181274462Salfred			ss.ss_family = args.ua.export.ex_addr->sa_family;
181374462Salfred			ss.ss_len = args.ua.export.ex_addr->sa_len;
181474462Salfred			if (allones(&ss, grp->gr_ptr.gt_net.nt_mask) != 0) {
181574462Salfred				syslog(LOG_ERR, "Bad network flag");
181674462Salfred				if (cp)
181774462Salfred					*cp = savedc;
181874462Salfred				return (1);
18191558Srgrimes			}
182074462Salfred			args.ua.export.ex_mask = (struct sockaddr *)&ss;
182174462Salfred			args.ua.export.ex_masklen = ss.ss_len;
18221558Srgrimes			break;
18237401Swpaul		case GT_IGNORE:
18247401Swpaul			return(0);
18257401Swpaul			break;
18261558Srgrimes		default:
182737663Scharnier			syslog(LOG_ERR, "bad grouptype");
18281558Srgrimes			if (cp)
18291558Srgrimes				*cp = savedc;
18301558Srgrimes			return (1);
18311558Srgrimes		};
18321558Srgrimes
18331558Srgrimes		/*
18341558Srgrimes		 * XXX:
18351558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
18361558Srgrimes		 * of looping back up the dirp to the mount point??
18371558Srgrimes		 * Also, needs to know how to export all types of local
183823681Speter		 * exportable file systems and not just "ufs".
18391558Srgrimes		 */
18409336Sdfr		while (mount(fsb->f_fstypename, dirp,
184174462Salfred		    fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
18421558Srgrimes			if (cp)
18431558Srgrimes				*cp-- = savedc;
18441558Srgrimes			else
18451558Srgrimes				cp = dirp + dirplen - 1;
18461558Srgrimes			if (errno == EPERM) {
18471558Srgrimes				syslog(LOG_ERR,
184837663Scharnier				   "can't change attributes for %s", dirp);
18491558Srgrimes				return (1);
18501558Srgrimes			}
18511558Srgrimes			if (opt_flags & OP_ALLDIRS) {
185237663Scharnier				syslog(LOG_ERR, "could not remount %s: %m",
18534895Swollman					dirp);
18541558Srgrimes				return (1);
18551558Srgrimes			}
18561558Srgrimes			/* back up over the last component */
18571558Srgrimes			while (*cp == '/' && cp > dirp)
18581558Srgrimes				cp--;
18591558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
18601558Srgrimes				cp--;
18611558Srgrimes			if (cp == dirp) {
18621558Srgrimes				if (debug)
186337663Scharnier					warnx("mnt unsucc");
186437663Scharnier				syslog(LOG_ERR, "can't export %s", dirp);
18651558Srgrimes				return (1);
18661558Srgrimes			}
18671558Srgrimes			savedc = *cp;
18681558Srgrimes			*cp = '\0';
18691558Srgrimes		}
187074462Salfredskip:
18711558Srgrimes		if (addrp) {
187274462Salfred			ai = ai->ai_next;
187374462Salfred			if (ai == NULL)
18741558Srgrimes				done = TRUE;
187574462Salfred			else {
187674462Salfred				addrp = ai->ai_addr;
187774462Salfred				addrlen = ai->ai_addrlen;
187874462Salfred			}
18791558Srgrimes		} else
18801558Srgrimes			done = TRUE;
18811558Srgrimes	}
18821558Srgrimes	if (cp)
18831558Srgrimes		*cp = savedc;
18841558Srgrimes	return (0);
18851558Srgrimes}
18861558Srgrimes
18871558Srgrimes/*
18881558Srgrimes * Translate a net address.
18891558Srgrimes */
18901558Srgrimesint
18911558Srgrimesget_net(cp, net, maskflg)
18921558Srgrimes	char *cp;
18931558Srgrimes	struct netmsk *net;
18941558Srgrimes	int maskflg;
18951558Srgrimes{
18961558Srgrimes	struct netent *np;
189774462Salfred	char *name, *p, *prefp;
189874462Salfred	struct sockaddr_in sin, *sinp;
189974462Salfred	struct sockaddr *sa;
190074462Salfred	struct addrinfo hints, *ai = NULL;
190174462Salfred	char netname[NI_MAXHOST];
190274462Salfred	long preflen;
190374462Salfred	int ecode;
19041558Srgrimes
190574462Salfred	if ((opt_flags & OP_MASKLEN) && !maskflg) {
190674462Salfred		p = strchr(cp, '/');
190774462Salfred		*p = '\0';
190874462Salfred		prefp = p + 1;
190974462Salfred	}
191074462Salfred
191174462Salfred	if ((np = getnetbyname(cp)) != NULL) {
191274462Salfred		sin.sin_family = AF_INET;
191374462Salfred		sin.sin_len = sizeof sin;
191474462Salfred		sin.sin_addr = inet_makeaddr(np->n_net, 0);
191574462Salfred		sa = (struct sockaddr *)&sin;
191674462Salfred	} else if (isdigit(*cp)) {
191774462Salfred		memset(&hints, 0, sizeof hints);
191874462Salfred		hints.ai_family = AF_UNSPEC;
191974462Salfred		hints.ai_flags = AI_NUMERICHOST;
192074462Salfred		if (getaddrinfo(cp, NULL, &hints, &ai) != 0) {
192174462Salfred			/*
192274462Salfred			 * If getaddrinfo() failed, try the inet4 network
192374462Salfred			 * notation with less than 3 dots.
192474462Salfred			 */
192574462Salfred			sin.sin_family = AF_INET;
192674462Salfred			sin.sin_len = sizeof sin;
192774462Salfred			sin.sin_addr = inet_makeaddr(inet_network(cp),0);
192874462Salfred			if (debug)
192974462Salfred				fprintf(stderr, "get_net: v4 addr %x\n",
193074462Salfred				    sin.sin_addr.s_addr);
193174462Salfred			sa = (struct sockaddr *)&sin;
193274462Salfred		} else
193374462Salfred			sa = ai->ai_addr;
193474462Salfred	} else if (isxdigit(*cp) || *cp == ':') {
193574462Salfred		memset(&hints, 0, sizeof hints);
193674462Salfred		hints.ai_family = AF_UNSPEC;
193774462Salfred		hints.ai_flags = AI_NUMERICHOST;
193874462Salfred		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
193974462Salfred			sa = ai->ai_addr;
194074462Salfred		else
194174462Salfred			goto fail;
19421558Srgrimes	} else
194374462Salfred		goto fail;
194425318Spst
194574462Salfred	ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname,
194674462Salfred	    NULL, 0, ninumeric);
194774462Salfred	if (ecode != 0)
194874462Salfred		goto fail;
194974462Salfred
19501558Srgrimes	if (maskflg)
195174462Salfred		net->nt_mask = countones(sa);
19521558Srgrimes	else {
195374462Salfred		if (opt_flags & OP_MASKLEN) {
195474462Salfred			preflen = strtol(prefp, NULL, 10);
195574462Salfred			if (preflen == LONG_MIN && errno == ERANGE)
195674462Salfred				goto fail;
195774462Salfred			net->nt_mask = (int)preflen;
195874462Salfred			*p = '/';
195974462Salfred		}
196074462Salfred
19611558Srgrimes		if (np)
19621558Srgrimes			name = np->n_name;
196374462Salfred		else {
196474462Salfred			if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
196574462Salfred			   NULL, 0, ninumeric) != 0)
196674462Salfred				strlcpy(netname, "?", sizeof(netname));
196774462Salfred			name = netname;
196874462Salfred		}
196974462Salfred		net->nt_name = strdup(name);
197074462Salfred		memcpy(&net->nt_net, sa, sa->sa_len);
197174462Salfred	}
197274462Salfred
197374462Salfred	if (!maskflg && sa->sa_family == AF_INET &&
197474462Salfred	    !(opt_flags & (OP_MASK|OP_MASKLEN))) {
197574462Salfred		sinp = (struct sockaddr_in *)sa;
197674462Salfred		if (IN_CLASSA(sinp->sin_addr.s_addr))
197774462Salfred			net->nt_mask = 8;
197874462Salfred		else if (IN_CLASSB(sinp->sin_addr.s_addr))
197974462Salfred			net->nt_mask = 16;
198074462Salfred		else if (IN_CLASSC(sinp->sin_addr.s_addr))
198174462Salfred			net->nt_mask = 24;
198274462Salfred		else if (IN_CLASSD(sinp->sin_addr.s_addr))
198374462Salfred			net->nt_mask = 28;
19841558Srgrimes		else
198574462Salfred			net->nt_mask = 32;       /* XXX */
19861558Srgrimes	}
198774462Salfred
198874462Salfred	if (ai)
198974462Salfred		freeaddrinfo(ai);
199074462Salfred	return 0;
199174462Salfred
199274462Salfredfail:
199374462Salfred	if (ai)
199474462Salfred		freeaddrinfo(ai);
199574462Salfred	return 1;
19961558Srgrimes}
19971558Srgrimes
19981558Srgrimes/*
19991558Srgrimes * Parse out the next white space separated field
20001558Srgrimes */
20011558Srgrimesvoid
20021558Srgrimesnextfield(cp, endcp)
20031558Srgrimes	char **cp;
20041558Srgrimes	char **endcp;
20051558Srgrimes{
20061558Srgrimes	char *p;
20071558Srgrimes
20081558Srgrimes	p = *cp;
20091558Srgrimes	while (*p == ' ' || *p == '\t')
20101558Srgrimes		p++;
20111558Srgrimes	if (*p == '\n' || *p == '\0')
20121558Srgrimes		*cp = *endcp = p;
20131558Srgrimes	else {
20141558Srgrimes		*cp = p++;
20151558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
20161558Srgrimes			p++;
20171558Srgrimes		*endcp = p;
20181558Srgrimes	}
20191558Srgrimes}
20201558Srgrimes
20211558Srgrimes/*
20221558Srgrimes * Get an exports file line. Skip over blank lines and handle line
20231558Srgrimes * continuations.
20241558Srgrimes */
20251558Srgrimesint
20261558Srgrimesget_line()
20271558Srgrimes{
20281558Srgrimes	char *p, *cp;
20291558Srgrimes	int len;
20301558Srgrimes	int totlen, cont_line;
20311558Srgrimes
20321558Srgrimes	/*
20331558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
20341558Srgrimes	 */
20351558Srgrimes	p = line;
20361558Srgrimes	totlen = 0;
20371558Srgrimes	do {
20381558Srgrimes		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
20391558Srgrimes			return (0);
20401558Srgrimes		len = strlen(p);
20411558Srgrimes		cp = p + len - 1;
20421558Srgrimes		cont_line = 0;
20431558Srgrimes		while (cp >= p &&
20441558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
20451558Srgrimes			if (*cp == '\\')
20461558Srgrimes				cont_line = 1;
20471558Srgrimes			cp--;
20481558Srgrimes			len--;
20491558Srgrimes		}
20501558Srgrimes		*++cp = '\0';
20511558Srgrimes		if (len > 0) {
20521558Srgrimes			totlen += len;
20531558Srgrimes			if (totlen >= LINESIZ) {
205437663Scharnier				syslog(LOG_ERR, "exports line too long");
20551558Srgrimes				exit(2);
20561558Srgrimes			}
20571558Srgrimes			p = cp;
20581558Srgrimes		}
20591558Srgrimes	} while (totlen == 0 || cont_line);
20601558Srgrimes	return (1);
20611558Srgrimes}
20621558Srgrimes
20631558Srgrimes/*
20641558Srgrimes * Parse a description of a credential.
20651558Srgrimes */
20661558Srgrimesvoid
20671558Srgrimesparsecred(namelist, cr)
20681558Srgrimes	char *namelist;
206972650Sgreen	struct xucred *cr;
20701558Srgrimes{
20711558Srgrimes	char *name;
20721558Srgrimes	int cnt;
20731558Srgrimes	char *names;
20741558Srgrimes	struct passwd *pw;
20751558Srgrimes	struct group *gr;
20761558Srgrimes	int ngroups, groups[NGROUPS + 1];
20771558Srgrimes
20781558Srgrimes	/*
207937663Scharnier	 * Set up the unprivileged user.
20801558Srgrimes	 */
20811558Srgrimes	cr->cr_uid = -2;
20821558Srgrimes	cr->cr_groups[0] = -2;
20831558Srgrimes	cr->cr_ngroups = 1;
20841558Srgrimes	/*
20851558Srgrimes	 * Get the user's password table entry.
20861558Srgrimes	 */
20871558Srgrimes	names = strsep(&namelist, " \t\n");
20881558Srgrimes	name = strsep(&names, ":");
20891558Srgrimes	if (isdigit(*name) || *name == '-')
20901558Srgrimes		pw = getpwuid(atoi(name));
20911558Srgrimes	else
20921558Srgrimes		pw = getpwnam(name);
20931558Srgrimes	/*
20941558Srgrimes	 * Credentials specified as those of a user.
20951558Srgrimes	 */
20961558Srgrimes	if (names == NULL) {
20971558Srgrimes		if (pw == NULL) {
209837663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
20991558Srgrimes			return;
21001558Srgrimes		}
21011558Srgrimes		cr->cr_uid = pw->pw_uid;
21021558Srgrimes		ngroups = NGROUPS + 1;
21031558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
210437663Scharnier			syslog(LOG_ERR, "too many groups");
21051558Srgrimes		/*
21061558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
21071558Srgrimes		 */
21081558Srgrimes		cr->cr_ngroups = ngroups - 1;
21091558Srgrimes		cr->cr_groups[0] = groups[0];
21101558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
21111558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
21121558Srgrimes		return;
21131558Srgrimes	}
21141558Srgrimes	/*
21151558Srgrimes	 * Explicit credential specified as a colon separated list:
21161558Srgrimes	 *	uid:gid:gid:...
21171558Srgrimes	 */
21181558Srgrimes	if (pw != NULL)
21191558Srgrimes		cr->cr_uid = pw->pw_uid;
21201558Srgrimes	else if (isdigit(*name) || *name == '-')
21211558Srgrimes		cr->cr_uid = atoi(name);
21221558Srgrimes	else {
212337663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
21241558Srgrimes		return;
21251558Srgrimes	}
21261558Srgrimes	cr->cr_ngroups = 0;
21271558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
21281558Srgrimes		name = strsep(&names, ":");
21291558Srgrimes		if (isdigit(*name) || *name == '-') {
21301558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
21311558Srgrimes		} else {
21321558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
213337663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
21341558Srgrimes				continue;
21351558Srgrimes			}
21361558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
21371558Srgrimes		}
21381558Srgrimes	}
21391558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
214037663Scharnier		syslog(LOG_ERR, "too many groups");
21411558Srgrimes}
21421558Srgrimes
21431558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
21441558Srgrimes/*
21451558Srgrimes * Routines that maintain the remote mounttab
21461558Srgrimes */
21471558Srgrimesvoid
21481558Srgrimesget_mountlist()
21491558Srgrimes{
21501558Srgrimes	struct mountlist *mlp, **mlpp;
215123681Speter	char *host, *dirp, *cp;
21521558Srgrimes	char str[STRSIZ];
21531558Srgrimes	FILE *mlfile;
21541558Srgrimes
21551558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
215653117Sbillf		if (errno == ENOENT)
215753117Sbillf			return;
215853117Sbillf		else {
215953117Sbillf			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
216053117Sbillf			return;
216153117Sbillf		}
21621558Srgrimes	}
21631558Srgrimes	mlpp = &mlhead;
21641558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
216523681Speter		cp = str;
216623681Speter		host = strsep(&cp, " \t\n");
216723681Speter		dirp = strsep(&cp, " \t\n");
216823681Speter		if (host == NULL || dirp == NULL)
21691558Srgrimes			continue;
21701558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
217137663Scharnier		if (mlp == (struct mountlist *)NULL)
217237663Scharnier			out_of_mem();
217323681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
217423681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
217523681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
217623681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
21771558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
21781558Srgrimes		*mlpp = mlp;
21791558Srgrimes		mlpp = &mlp->ml_next;
21801558Srgrimes	}
21811558Srgrimes	fclose(mlfile);
21821558Srgrimes}
21831558Srgrimes
218474462Salfredint
218574462Salfreddel_mlist(hostp, dirp, saddr)
21861558Srgrimes	char *hostp, *dirp;
218774462Salfred	struct sockaddr *saddr;
21881558Srgrimes{
21891558Srgrimes	struct mountlist *mlp, **mlpp;
21901558Srgrimes	struct mountlist *mlp2;
219174462Salfred	u_short sport;
21921558Srgrimes	FILE *mlfile;
21931558Srgrimes	int fnd = 0;
219474462Salfred	char host[NI_MAXHOST];
21951558Srgrimes
219674462Salfred	switch (saddr->sa_family) {
219774462Salfred	case AF_INET6:
219874462Salfred		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
219974462Salfred		break;
220074462Salfred	case AF_INET:
220174462Salfred		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
220274462Salfred		break;
220374462Salfred	default:
220474462Salfred		return -1;
220574462Salfred	}
22061558Srgrimes	mlpp = &mlhead;
22071558Srgrimes	mlp = mlhead;
22081558Srgrimes	while (mlp) {
22091558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
22101558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
22111558Srgrimes			fnd = 1;
22121558Srgrimes			mlp2 = mlp;
22131558Srgrimes			*mlpp = mlp = mlp->ml_next;
22141558Srgrimes			free((caddr_t)mlp2);
22151558Srgrimes		} else {
22161558Srgrimes			mlpp = &mlp->ml_next;
22171558Srgrimes			mlp = mlp->ml_next;
22181558Srgrimes		}
22191558Srgrimes	}
22201558Srgrimes	if (fnd) {
22211558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
222237663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
22231558Srgrimes			return;
22241558Srgrimes		}
22251558Srgrimes		mlp = mlhead;
22261558Srgrimes		while (mlp) {
22271558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22281558Srgrimes			mlp = mlp->ml_next;
22291558Srgrimes		}
22301558Srgrimes		fclose(mlfile);
22311558Srgrimes	}
22321558Srgrimes}
22331558Srgrimes
22341558Srgrimesvoid
22351558Srgrimesadd_mlist(hostp, dirp)
22361558Srgrimes	char *hostp, *dirp;
22371558Srgrimes{
22381558Srgrimes	struct mountlist *mlp, **mlpp;
22391558Srgrimes	FILE *mlfile;
22401558Srgrimes
22411558Srgrimes	mlpp = &mlhead;
22421558Srgrimes	mlp = mlhead;
22431558Srgrimes	while (mlp) {
22441558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
22451558Srgrimes			return;
22461558Srgrimes		mlpp = &mlp->ml_next;
22471558Srgrimes		mlp = mlp->ml_next;
22481558Srgrimes	}
22491558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
225037663Scharnier	if (mlp == (struct mountlist *)NULL)
225137663Scharnier		out_of_mem();
22521558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
22531558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
22541558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
22551558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
22561558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
22571558Srgrimes	*mlpp = mlp;
22581558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
225937663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
22601558Srgrimes		return;
22611558Srgrimes	}
22621558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22631558Srgrimes	fclose(mlfile);
22641558Srgrimes}
22651558Srgrimes
22661558Srgrimes/*
22671558Srgrimes * Free up a group list.
22681558Srgrimes */
22691558Srgrimesvoid
22701558Srgrimesfree_grp(grp)
22711558Srgrimes	struct grouplist *grp;
22721558Srgrimes{
227374462Salfred	struct addrinfo *ai;
22741558Srgrimes
22751558Srgrimes	if (grp->gr_type == GT_HOST) {
227674462Salfred		if (grp->gr_ptr.gt_addrinfo != NULL)
227774462Salfred			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
22781558Srgrimes	} else if (grp->gr_type == GT_NET) {
22791558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
22801558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
22811558Srgrimes	}
22821558Srgrimes	free((caddr_t)grp);
22831558Srgrimes}
22841558Srgrimes
22851558Srgrimes#ifdef DEBUG
22861558Srgrimesvoid
22871558SrgrimesSYSLOG(int pri, const char *fmt, ...)
22881558Srgrimes{
22891558Srgrimes	va_list ap;
22901558Srgrimes
22911558Srgrimes	va_start(ap, fmt);
22921558Srgrimes	vfprintf(stderr, fmt, ap);
22931558Srgrimes	va_end(ap);
22941558Srgrimes}
22951558Srgrimes#endif /* DEBUG */
22961558Srgrimes
22971558Srgrimes/*
22981558Srgrimes * Check options for consistency.
22991558Srgrimes */
23001558Srgrimesint
23011558Srgrimescheck_options(dp)
23021558Srgrimes	struct dirlist *dp;
23031558Srgrimes{
23041558Srgrimes
23051558Srgrimes	if (dp == (struct dirlist *)NULL)
23061558Srgrimes	    return (1);
23071558Srgrimes	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
23081558Srgrimes	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
23091558Srgrimes	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
23101558Srgrimes	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
23111558Srgrimes	    return (1);
23121558Srgrimes	}
23131558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
23141558Srgrimes	    syslog(LOG_ERR, "-mask requires -net");
23151558Srgrimes	    return (1);
23161558Srgrimes	}
23171558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
231845927Salex	    syslog(LOG_ERR, "-alldirs has multiple directories");
23191558Srgrimes	    return (1);
23201558Srgrimes	}
23211558Srgrimes	return (0);
23221558Srgrimes}
23231558Srgrimes
23241558Srgrimes/*
23251558Srgrimes * Check an absolute directory path for any symbolic links. Return true
23261558Srgrimes */
23271558Srgrimesint
23281558Srgrimescheck_dirpath(dirp)
23291558Srgrimes	char *dirp;
23301558Srgrimes{
23311558Srgrimes	char *cp;
23321558Srgrimes	int ret = 1;
23331558Srgrimes	struct stat sb;
23341558Srgrimes
23351558Srgrimes	cp = dirp + 1;
23361558Srgrimes	while (*cp && ret) {
23371558Srgrimes		if (*cp == '/') {
23381558Srgrimes			*cp = '\0';
23399336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23401558Srgrimes				ret = 0;
23411558Srgrimes			*cp = '/';
23421558Srgrimes		}
23431558Srgrimes		cp++;
23441558Srgrimes	}
23459336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23461558Srgrimes		ret = 0;
23471558Srgrimes	return (ret);
23481558Srgrimes}
23499336Sdfr
23509336Sdfr/*
23519336Sdfr * Just translate an ascii string to an integer.
23529336Sdfr */
23539336Sdfrint
23549336Sdfrget_num(cp)
23559336Sdfr	register char *cp;
23569336Sdfr{
23579336Sdfr	register int res = 0;
23589336Sdfr
23599336Sdfr	while (*cp) {
23609336Sdfr		if (*cp < '0' || *cp > '9')
23619336Sdfr			return (-1);
23629336Sdfr		res = res * 10 + (*cp++ - '0');
23639336Sdfr	}
23649336Sdfr	return (res);
23659336Sdfr}
236674462Salfred
236774462Salfredstatic int
236874462Salfrednetpartcmp(struct sockaddr *s1, struct sockaddr *s2, int bitlen)
236974462Salfred{
237074462Salfred	void *src, *dst;
237174462Salfred
237274462Salfred	if (s1->sa_family != s2->sa_family)
237374462Salfred		return 1;
237474462Salfred
237574462Salfred	switch (s1->sa_family) {
237674462Salfred	case AF_INET:
237774462Salfred		src = &((struct sockaddr_in *)s1)->sin_addr;
237874462Salfred		dst = &((struct sockaddr_in *)s2)->sin_addr;
237974462Salfred		if (bitlen > sizeof(((struct sockaddr_in *)s1)->sin_addr) * 8)
238074462Salfred			return 1;
238174462Salfred		break;
238274462Salfred	case AF_INET6:
238374462Salfred		src = &((struct sockaddr_in6 *)s1)->sin6_addr;
238474462Salfred		dst = &((struct sockaddr_in6 *)s2)->sin6_addr;
238574462Salfred		if (((struct sockaddr_in6 *)s1)->sin6_scope_id !=
238674462Salfred		   ((struct sockaddr_in6 *)s2)->sin6_scope_id)
238774462Salfred			return 1;
238874462Salfred		if (bitlen > sizeof(((struct sockaddr_in6 *)s1)->sin6_addr) * 8)
238974462Salfred			return 1;
239074462Salfred		break;
239174462Salfred	default:
239274462Salfred		return 1;
239374462Salfred	}
239474462Salfred
239574462Salfred	return bitcmp(src, dst, bitlen);
239674462Salfred}
239774462Salfred
239874462Salfredstatic int
239974462Salfredallones(struct sockaddr_storage *ssp, int bitlen)
240074462Salfred{
240174462Salfred	u_int8_t *p;
240274462Salfred	int bytelen, bitsleft, i;
240374462Salfred	int zerolen;
240474462Salfred
240574462Salfred	switch (ssp->ss_family) {
240674462Salfred	case AF_INET:
240774462Salfred		p = (u_int8_t *)&((struct sockaddr_in *)ssp)->sin_addr;
240874462Salfred		zerolen = sizeof (((struct sockaddr_in *)ssp)->sin_addr);
240974462Salfred		break;
241074462Salfred	case AF_INET6:
241174462Salfred		p = (u_int8_t *)&((struct sockaddr_in6 *)ssp)->sin6_addr;
241274462Salfred		zerolen = sizeof (((struct sockaddr_in6 *)ssp)->sin6_addr);
241374462Salfred	break;
241474462Salfred	default:
241574462Salfred		return -1;
241674462Salfred	}
241774462Salfred
241874462Salfred	memset(p, 0, zerolen);
241974462Salfred
242074462Salfred	bytelen = bitlen / 8;
242174462Salfred	bitsleft = bitlen % 8;
242274462Salfred
242374462Salfred	if (bytelen > zerolen)
242474462Salfred		return -1;
242574462Salfred
242674462Salfred	for (i = 0; i < bytelen; i++)
242774462Salfred		*p++ = 0xff;
242874462Salfred
242974462Salfred	for (i = 0; i < bitsleft; i++)
243074462Salfred		*p |= 1 << (7 - i);
243174462Salfred
243274462Salfred	return 0;
243374462Salfred}
243474462Salfred
243574462Salfredstatic int
243674462Salfredcountones(struct sockaddr *sa)
243774462Salfred{
243874462Salfred	void *mask;
243974462Salfred	int i, bits = 0, bytelen;
244074462Salfred	u_int8_t *p;
244174462Salfred
244274462Salfred	switch (sa->sa_family) {
244374462Salfred	case AF_INET:
244474462Salfred		mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr;
244574462Salfred		bytelen = 4;
244674462Salfred		break;
244774462Salfred	case AF_INET6:
244874462Salfred		mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr;
244974462Salfred		bytelen = 16;
245074462Salfred		break;
245174462Salfred	default:
245274462Salfred		return 0;
245374462Salfred	}
245474462Salfred
245574462Salfred	p = mask;
245674462Salfred
245774462Salfred	for (i = 0; i < bytelen; i++, p++) {
245874462Salfred		if (*p != 0xff) {
245974462Salfred			for (bits = 0; bits < 8; bits++) {
246074462Salfred				if (!(*p & (1 << (7 - bits))))
246174462Salfred					break;
246274462Salfred			}
246374462Salfred			break;
246474462Salfred		}
246574462Salfred	}
246674462Salfred
246774462Salfred	return (i * 8 + bits);
246874462Salfred}
246974462Salfred
247074462Salfredstatic int
247174462Salfredsacmp(struct sockaddr *sa1, struct sockaddr *sa2)
247274462Salfred{
247374462Salfred	void *p1, *p2;
247474462Salfred	int len;
247574462Salfred
247674462Salfred	if (sa1->sa_family != sa2->sa_family)
247774462Salfred		return 1;
247874462Salfred
247974462Salfred	switch (sa1->sa_family) {
248074462Salfred	case AF_INET:
248174462Salfred		p1 = &((struct sockaddr_in *)sa1)->sin_addr;
248274462Salfred		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
248374462Salfred		len = 4;
248474462Salfred		break;
248574462Salfred	case AF_INET6:
248674462Salfred		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
248774462Salfred		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
248874462Salfred		len = 16;
248974462Salfred		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
249074462Salfred		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
249174462Salfred			return 1;
249274462Salfred		break;
249374462Salfred	default:
249474462Salfred		return 1;
249574462Salfred	}
249674462Salfred
249774462Salfred	return memcmp(p1, p2, len);
249874462Salfred}
249974462Salfred
250074462Salfredvoid terminate(sig)
250174462Salfredint sig;
250274462Salfred{
250374462Salfred	close(mountdlockfd);
250474462Salfred	unlink(MOUNTDLOCK);
250574462Salfred	pmap_unset(RPCPROG_MNT, 1);
250674462Salfred	pmap_unset(RPCPROG_MNT, 3);
250774462Salfred	exit (0);
250874462Salfred}
2509