mountd.c revision 74792
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 74792 2001-03-25 19:59:07Z 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);
35574791Salfred	udpconf  = getnetconfigent("udp");
35674791Salfred	tcpconf  = getnetconfigent("tcp");
35774791Salfred	if (!have_v6)
35874791Salfred		goto skip_v6;
35974462Salfred	udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
36074462Salfred	tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
36174462Salfred	/*
36274462Salfred	 * We're doing host-based access checks here, so don't allow
36374462Salfred	 * v4-in-v6 to confuse things. The kernel will disable it
36474462Salfred	 * by default on NFS sockets too.
36574462Salfred	 */
36674462Salfred	if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
36774462Salfred		IPV6_BINDV6ONLY, &one, sizeof one) < 0){
36874462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
36974462Salfred		exit(1);
37074462Salfred	}
37174462Salfred	if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
37274462Salfred		IPV6_BINDV6ONLY, &one, sizeof one) < 0){
37374462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
37474462Salfred		exit(1);
37574462Salfred	}
37674462Salfred	udp6conf = getnetconfigent("udp6");
37774462Salfred	tcp6conf = getnetconfigent("tcp6");
37874791Salfred
37974791Salfredskip_v6:
38024759Sguido	if (!resvport_only) {
38124759Sguido		mib[0] = CTL_VFS;
38232656Sbde		mib[1] = vfc.vfc_typenum;
38324759Sguido		mib[2] = NFS_NFSPRIVPORT;
38424759Sguido		if (sysctl(mib, 3, NULL, NULL, &resvport_only,
38524759Sguido		    sizeof(resvport_only)) != 0 && errno != ENOENT) {
38624759Sguido			syslog(LOG_ERR, "sysctl: %m");
38724759Sguido			exit(1);
38824759Sguido		}
38924330Sguido	}
3909202Srgrimes	if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
3919202Srgrimes	    (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
39237663Scharnier		syslog(LOG_ERR, "can't create socket");
3931558Srgrimes		exit(1);
3941558Srgrimes	}
39574462Salfred	if (udpsock != -1 && udpconf != NULL) {
39674462Salfred		bindresvport(udpsock, NULL);
39774462Salfred		udptransp = svc_dg_create(udpsock, 0, 0);
39874462Salfred		if (udptransp != NULL) {
39974462Salfred			if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
40074462Salfred			    mntsrv, udpconf))
40174462Salfred				syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service");
40274462Salfred			else
40374462Salfred				xcreated++;
40474462Salfred			if (!force_v2) {
40574462Salfred				if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
40674462Salfred				    mntsrv, udpconf))
40774462Salfred					syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service");
40874462Salfred				else
40974462Salfred					xcreated++;
41074462Salfred			}
41174462Salfred		} else
41274462Salfred			syslog(LOG_WARNING, "can't create UDP services");
41374462Salfred
41474462Salfred	}
41574462Salfred	if (tcpsock != -1 && tcpconf != NULL) {
41674462Salfred		bindresvport(tcpsock, NULL);
41774462Salfred		listen(tcpsock, SOMAXCONN);
41874462Salfred		tcptransp = svc_vc_create(tcpsock, 0, 0);
41974462Salfred		if (tcptransp != NULL) {
42074462Salfred			if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
42174462Salfred			    mntsrv, tcpconf))
42274462Salfred				syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service");
42374462Salfred			else
42474462Salfred				xcreated++;
42574462Salfred			if (!force_v2) {
42674462Salfred				if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
42774462Salfred				    mntsrv, tcpconf))
42874462Salfred					syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service");
42974462Salfred				else
43074462Salfred					xcreated++;
43174462Salfred			}
43274462Salfred		} else
43374462Salfred			syslog(LOG_WARNING, "can't create TCP service");
43474462Salfred
43574462Salfred	}
43674791Salfred	if (have_v6 && udp6sock != -1 && udp6conf != NULL) {
43774462Salfred		bindresvport(udp6sock, NULL);
43874462Salfred		udp6transp = svc_dg_create(udp6sock, 0, 0);
43974462Salfred		if (udp6transp != NULL) {
44074462Salfred			if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
44174462Salfred			    mntsrv, udp6conf))
44274462Salfred				syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service");
44374462Salfred			else
44474462Salfred				xcreated++;
44574462Salfred			if (!force_v2) {
44674462Salfred				if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
44774462Salfred				    mntsrv, udp6conf))
44874462Salfred					syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service");
44974462Salfred				else
45074462Salfred					xcreated++;
45174462Salfred			}
45274462Salfred		} else
45374462Salfred			syslog(LOG_WARNING, "can't create UDP6 service");
45474462Salfred
45574462Salfred	}
45674791Salfred	if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) {
45774462Salfred		bindresvport(tcp6sock, NULL);
45874462Salfred		listen(tcp6sock, SOMAXCONN);
45974462Salfred		tcp6transp = svc_vc_create(tcp6sock, 0, 0);
46074462Salfred		if (tcp6transp != NULL) {
46174462Salfred			if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
46274462Salfred			    mntsrv, tcp6conf))
46374462Salfred				syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service");
46474462Salfred			else
46574462Salfred				xcreated++;
46674462Salfred			if (!force_v2) {
46774462Salfred				if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
46874462Salfred				    mntsrv, tcp6conf))
46974462Salfred					syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service");
47074462Salfred					else
47174462Salfred						xcreated++;
47274462Salfred				}
47374462Salfred		} else
47474462Salfred			syslog(LOG_WARNING, "can't create TCP6 service");
47574462Salfred
47674462Salfred	}
47774462Salfred	if (xcreated == 0) {
47874462Salfred		syslog(LOG_ERR, "could not create any services");
4791558Srgrimes		exit(1);
4801558Srgrimes	}
4811558Srgrimes	svc_run();
48237663Scharnier	syslog(LOG_ERR, "mountd died");
4831558Srgrimes	exit(1);
4841558Srgrimes}
4851558Srgrimes
48637663Scharnierstatic void
48737663Scharnierusage()
48837663Scharnier{
48937663Scharnier	fprintf(stderr,
49037663Scharnier		"usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
49137663Scharnier	exit(1);
49237663Scharnier}
49337663Scharnier
4941558Srgrimes/*
4951558Srgrimes * The mount rpc service
4961558Srgrimes */
4971558Srgrimesvoid
4981558Srgrimesmntsrv(rqstp, transp)
4991558Srgrimes	struct svc_req *rqstp;
5001558Srgrimes	SVCXPRT *transp;
5011558Srgrimes{
5021558Srgrimes	struct exportlist *ep;
5031558Srgrimes	struct dirlist *dp;
5049336Sdfr	struct fhreturn fhr;
5051558Srgrimes	struct stat stb;
5061558Srgrimes	struct statfs fsb;
50774462Salfred	struct addrinfo *ai;
50874462Salfred	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
50974462Salfred	int lookup_failed = 1;
51074462Salfred	struct sockaddr *saddr;
5119336Sdfr	u_short sport;
51223681Speter	char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
51328911Sguido	int bad = 0, defset, hostset;
5149336Sdfr	sigset_t sighup_mask;
51574462Salfred	struct sockaddr_in6 *sin6;
51674462Salfred	struct sockaddr_in *sin;
5171558Srgrimes
5189336Sdfr	sigemptyset(&sighup_mask);
5199336Sdfr	sigaddset(&sighup_mask, SIGHUP);
52074462Salfred	saddr = svc_getrpccaller(transp)->buf;
52174462Salfred	switch (saddr->sa_family) {
52274462Salfred	case AF_INET6:
52374462Salfred		sin6 = (struct sockaddr_in6 *)saddr;
52474462Salfred		sport = ntohs(sin6->sin6_port);
52574462Salfred		break;
52674462Salfred	case AF_INET:
52774462Salfred		sin = (struct sockaddr_in *)saddr;
52874462Salfred		sport = ntohs(sin->sin_port);
52974462Salfred		break;
53074462Salfred	default:
53174462Salfred		syslog(LOG_ERR, "request from unknown address family");
53274462Salfred		return;
53374462Salfred	}
53474462Salfred	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
53574462Salfred	    NULL, 0, 0);
53674462Salfred	getnameinfo(saddr, saddr->sa_len, numerichost,
53774462Salfred	    sizeof numerichost, NULL, 0, NI_NUMERICHOST);
53874462Salfred	ai = NULL;
5391558Srgrimes	switch (rqstp->rq_proc) {
5401558Srgrimes	case NULLPROC:
5411558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
54237663Scharnier			syslog(LOG_ERR, "can't send reply");
5431558Srgrimes		return;
5441558Srgrimes	case RPCMNT_MOUNT:
5459336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
54631656Sguido			syslog(LOG_NOTICE,
54731656Sguido			    "mount request from %s from unprivileged port",
54874462Salfred			    numerichost);
5491558Srgrimes			svcerr_weakauth(transp);
5501558Srgrimes			return;
5511558Srgrimes		}
5521558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
55331656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
55474462Salfred			    numerichost);
5551558Srgrimes			svcerr_decode(transp);
5561558Srgrimes			return;
5571558Srgrimes		}
5581558Srgrimes
5591558Srgrimes		/*
5601558Srgrimes		 * Get the real pathname and make sure it is a directory
5619336Sdfr		 * or a regular file if the -r option was specified
5629336Sdfr		 * and it exists.
5631558Srgrimes		 */
56451968Salfred		if (realpath(rpcpath, dirpath) == NULL ||
5651558Srgrimes		    stat(dirpath, &stb) < 0 ||
5669336Sdfr		    (!S_ISDIR(stb.st_mode) &&
56774462Salfred		    (dir_only || !S_ISREG(stb.st_mode))) ||
5681558Srgrimes		    statfs(dirpath, &fsb) < 0) {
5691558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
57031656Sguido			syslog(LOG_NOTICE,
57137663Scharnier			    "mount request from %s for non existent path %s",
57274462Salfred			    numerichost, dirpath);
5731558Srgrimes			if (debug)
57437663Scharnier				warnx("stat failed on %s", dirpath);
57528911Sguido			bad = ENOENT;	/* We will send error reply later */
5761558Srgrimes		}
5771558Srgrimes
5781558Srgrimes		/* Check in the exports list */
5799336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
5801558Srgrimes		ep = ex_search(&fsb.f_fsid);
5819336Sdfr		hostset = defset = 0;
5829336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
5831558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
58474462Salfred		      chk_host(dp, saddr, &defset, &hostset)) ||
58574462Salfred		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
58674462Salfred		     scan_tree(ep->ex_dirl, saddr) == 0))) {
58728911Sguido			if (bad) {
58828911Sguido				if (!svc_sendreply(transp, xdr_long,
58928911Sguido				    (caddr_t)&bad))
59037663Scharnier					syslog(LOG_ERR, "can't send reply");
59128911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
59228911Sguido				return;
59328911Sguido			}
5949336Sdfr			if (hostset & DP_HOSTSET)
5959336Sdfr				fhr.fhr_flag = hostset;
5969336Sdfr			else
5979336Sdfr				fhr.fhr_flag = defset;
5989336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
5991558Srgrimes			/* Get the file handle */
60023681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
6019336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
6021558Srgrimes				bad = errno;
60337663Scharnier				syslog(LOG_ERR, "can't get fh for %s", dirpath);
6041558Srgrimes				if (!svc_sendreply(transp, xdr_long,
6051558Srgrimes				    (caddr_t)&bad))
60637663Scharnier					syslog(LOG_ERR, "can't send reply");
6079336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6081558Srgrimes				return;
6091558Srgrimes			}
6109336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
61137663Scharnier				syslog(LOG_ERR, "can't send reply");
61274462Salfred			if (!lookup_failed)
61374462Salfred				add_mlist(host, dirpath);
6141558Srgrimes			else
61574462Salfred				add_mlist(numerichost, dirpath);
6161558Srgrimes			if (debug)
61737663Scharnier				warnx("mount successful");
61831656Sguido			if (log)
61931656Sguido				syslog(LOG_NOTICE,
62031656Sguido				    "mount request succeeded from %s for %s",
62174462Salfred				    numerichost, dirpath);
62231656Sguido		} else {
6231558Srgrimes			bad = EACCES;
62431656Sguido			syslog(LOG_NOTICE,
62531656Sguido			    "mount request denied from %s for %s",
62674462Salfred			    numerichost, dirpath);
62731656Sguido		}
62828911Sguido
62928911Sguido		if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
63037663Scharnier			syslog(LOG_ERR, "can't send reply");
6319336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6321558Srgrimes		return;
6331558Srgrimes	case RPCMNT_DUMP:
6341558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
63537663Scharnier			syslog(LOG_ERR, "can't send reply");
63631656Sguido		else if (log)
63731656Sguido			syslog(LOG_NOTICE,
63831656Sguido			    "dump request succeeded from %s",
63974462Salfred			    numerichost);
6401558Srgrimes		return;
6411558Srgrimes	case RPCMNT_UMOUNT:
6429336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
64331656Sguido			syslog(LOG_NOTICE,
64431656Sguido			    "umount request from %s from unprivileged port",
64574462Salfred			    numerichost);
6461558Srgrimes			svcerr_weakauth(transp);
6471558Srgrimes			return;
6481558Srgrimes		}
64951968Salfred		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
65031656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
65174462Salfred			    numerichost);
6521558Srgrimes			svcerr_decode(transp);
6531558Srgrimes			return;
6541558Srgrimes		}
65551968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
65651968Salfred			syslog(LOG_NOTICE, "umount request from %s "
65751968Salfred			    "for non existent path %s",
65874462Salfred			    numerichost, dirpath);
65951968Salfred		}
6601558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
66137663Scharnier			syslog(LOG_ERR, "can't send reply");
66274462Salfred		if (!lookup_failed)
66374462Salfred			del_mlist(host, dirpath, saddr);
66474462Salfred		del_mlist(numerichost, dirpath, saddr);
66531656Sguido		if (log)
66631656Sguido			syslog(LOG_NOTICE,
66731656Sguido			    "umount request succeeded from %s for %s",
66874462Salfred			    numerichost, dirpath);
6691558Srgrimes		return;
6701558Srgrimes	case RPCMNT_UMNTALL:
6719336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
67231656Sguido			syslog(LOG_NOTICE,
67331656Sguido			    "umountall request from %s from unprivileged port",
67474462Salfred			    numerichost);
6751558Srgrimes			svcerr_weakauth(transp);
6761558Srgrimes			return;
6771558Srgrimes		}
6781558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
67937663Scharnier			syslog(LOG_ERR, "can't send reply");
68074462Salfred		if (!lookup_failed)
68174462Salfred			del_mlist(host, NULL, saddr);
68274462Salfred		del_mlist(numerichost, NULL, saddr);
68331656Sguido		if (log)
68431656Sguido			syslog(LOG_NOTICE,
68531656Sguido			    "umountall request succeeded from %s",
68674462Salfred			    numerichost);
6871558Srgrimes		return;
6881558Srgrimes	case RPCMNT_EXPORT:
6891558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
69037663Scharnier			syslog(LOG_ERR, "can't send reply");
69131656Sguido		if (log)
69231656Sguido			syslog(LOG_NOTICE,
69331656Sguido			    "export request succeeded from %s",
69474462Salfred			    numerichost);
6951558Srgrimes		return;
6961558Srgrimes	default:
6971558Srgrimes		svcerr_noproc(transp);
6981558Srgrimes		return;
6991558Srgrimes	}
7001558Srgrimes}
7011558Srgrimes
7021558Srgrimes/*
7031558Srgrimes * Xdr conversion for a dirpath string
7041558Srgrimes */
7051558Srgrimesint
7061558Srgrimesxdr_dir(xdrsp, dirp)
7071558Srgrimes	XDR *xdrsp;
7081558Srgrimes	char *dirp;
7091558Srgrimes{
7101558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
7111558Srgrimes}
7121558Srgrimes
7131558Srgrimes/*
7149336Sdfr * Xdr routine to generate file handle reply
7151558Srgrimes */
7161558Srgrimesint
7179336Sdfrxdr_fhs(xdrsp, cp)
7181558Srgrimes	XDR *xdrsp;
7199336Sdfr	caddr_t cp;
7201558Srgrimes{
7219336Sdfr	register struct fhreturn *fhrp = (struct fhreturn *)cp;
7229336Sdfr	u_long ok = 0, len, auth;
7231558Srgrimes
7241558Srgrimes	if (!xdr_long(xdrsp, &ok))
7251558Srgrimes		return (0);
7269336Sdfr	switch (fhrp->fhr_vers) {
7279336Sdfr	case 1:
7289336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
7299336Sdfr	case 3:
7309336Sdfr		len = NFSX_V3FH;
7319336Sdfr		if (!xdr_long(xdrsp, &len))
7329336Sdfr			return (0);
7339336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
7349336Sdfr			return (0);
7359336Sdfr		if (fhrp->fhr_flag & DP_KERB)
7369336Sdfr			auth = RPCAUTH_KERB4;
7379336Sdfr		else
7389336Sdfr			auth = RPCAUTH_UNIX;
7399336Sdfr		len = 1;
7409336Sdfr		if (!xdr_long(xdrsp, &len))
7419336Sdfr			return (0);
7429336Sdfr		return (xdr_long(xdrsp, &auth));
7439336Sdfr	};
7449336Sdfr	return (0);
7451558Srgrimes}
7461558Srgrimes
7471558Srgrimesint
7481558Srgrimesxdr_mlist(xdrsp, cp)
7491558Srgrimes	XDR *xdrsp;
7501558Srgrimes	caddr_t cp;
7511558Srgrimes{
7521558Srgrimes	struct mountlist *mlp;
7531558Srgrimes	int true = 1;
7541558Srgrimes	int false = 0;
7551558Srgrimes	char *strp;
7561558Srgrimes
7571558Srgrimes	mlp = mlhead;
7581558Srgrimes	while (mlp) {
7591558Srgrimes		if (!xdr_bool(xdrsp, &true))
7601558Srgrimes			return (0);
7611558Srgrimes		strp = &mlp->ml_host[0];
7621558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
7631558Srgrimes			return (0);
7641558Srgrimes		strp = &mlp->ml_dirp[0];
7651558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
7661558Srgrimes			return (0);
7671558Srgrimes		mlp = mlp->ml_next;
7681558Srgrimes	}
7691558Srgrimes	if (!xdr_bool(xdrsp, &false))
7701558Srgrimes		return (0);
7711558Srgrimes	return (1);
7721558Srgrimes}
7731558Srgrimes
7741558Srgrimes/*
7751558Srgrimes * Xdr conversion for export list
7761558Srgrimes */
7771558Srgrimesint
7781558Srgrimesxdr_explist(xdrsp, cp)
7791558Srgrimes	XDR *xdrsp;
7801558Srgrimes	caddr_t cp;
7811558Srgrimes{
7821558Srgrimes	struct exportlist *ep;
7831558Srgrimes	int false = 0;
7849336Sdfr	int putdef;
7859336Sdfr	sigset_t sighup_mask;
7861558Srgrimes
7879336Sdfr	sigemptyset(&sighup_mask);
7889336Sdfr	sigaddset(&sighup_mask, SIGHUP);
7899336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
7901558Srgrimes	ep = exphead;
7911558Srgrimes	while (ep) {
7921558Srgrimes		putdef = 0;
7931558Srgrimes		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
7941558Srgrimes			goto errout;
7951558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
7961558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
7971558Srgrimes			&putdef))
7981558Srgrimes			goto errout;
7991558Srgrimes		ep = ep->ex_next;
8001558Srgrimes	}
8019336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8021558Srgrimes	if (!xdr_bool(xdrsp, &false))
8031558Srgrimes		return (0);
8041558Srgrimes	return (1);
8051558Srgrimeserrout:
8069336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8071558Srgrimes	return (0);
8081558Srgrimes}
8091558Srgrimes
8101558Srgrimes/*
8111558Srgrimes * Called from xdr_explist() to traverse the tree and export the
8121558Srgrimes * directory paths.
8131558Srgrimes */
8141558Srgrimesint
8151558Srgrimesput_exlist(dp, xdrsp, adp, putdefp)
8161558Srgrimes	struct dirlist *dp;
8171558Srgrimes	XDR *xdrsp;
8181558Srgrimes	struct dirlist *adp;
8191558Srgrimes	int *putdefp;
8201558Srgrimes{
8211558Srgrimes	struct grouplist *grp;
8221558Srgrimes	struct hostlist *hp;
8231558Srgrimes	int true = 1;
8241558Srgrimes	int false = 0;
8251558Srgrimes	int gotalldir = 0;
8261558Srgrimes	char *strp;
8271558Srgrimes
8281558Srgrimes	if (dp) {
8291558Srgrimes		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
8301558Srgrimes			return (1);
8311558Srgrimes		if (!xdr_bool(xdrsp, &true))
8321558Srgrimes			return (1);
8331558Srgrimes		strp = dp->dp_dirp;
8341558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
8351558Srgrimes			return (1);
8361558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
8371558Srgrimes			gotalldir = 1;
8381558Srgrimes			*putdefp = 1;
8391558Srgrimes		}
8401558Srgrimes		if ((dp->dp_flag & DP_DEFSET) == 0 &&
8411558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
8421558Srgrimes			hp = dp->dp_hosts;
8431558Srgrimes			while (hp) {
8441558Srgrimes				grp = hp->ht_grp;
8451558Srgrimes				if (grp->gr_type == GT_HOST) {
8461558Srgrimes					if (!xdr_bool(xdrsp, &true))
8471558Srgrimes						return (1);
84874462Salfred					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
8498871Srgrimes					if (!xdr_string(xdrsp, &strp,
8501558Srgrimes					    RPCMNT_NAMELEN))
8511558Srgrimes						return (1);
8521558Srgrimes				} else if (grp->gr_type == GT_NET) {
8531558Srgrimes					if (!xdr_bool(xdrsp, &true))
8541558Srgrimes						return (1);
8551558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
8568871Srgrimes					if (!xdr_string(xdrsp, &strp,
8571558Srgrimes					    RPCMNT_NAMELEN))
8581558Srgrimes						return (1);
8591558Srgrimes				}
8601558Srgrimes				hp = hp->ht_next;
8611558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
8621558Srgrimes					hp = adp->dp_hosts;
8631558Srgrimes					gotalldir = 0;
8641558Srgrimes				}
8651558Srgrimes			}
8661558Srgrimes		}
8671558Srgrimes		if (!xdr_bool(xdrsp, &false))
8681558Srgrimes			return (1);
8691558Srgrimes		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
8701558Srgrimes			return (1);
8711558Srgrimes	}
8721558Srgrimes	return (0);
8731558Srgrimes}
8741558Srgrimes
8751558Srgrimes#define LINESIZ	10240
8761558Srgrimeschar line[LINESIZ];
8771558SrgrimesFILE *exp_file;
8781558Srgrimes
8791558Srgrimes/*
8801558Srgrimes * Get the export list
8811558Srgrimes */
8821558Srgrimesvoid
8831558Srgrimesget_exportlist()
8841558Srgrimes{
8851558Srgrimes	struct exportlist *ep, *ep2;
8861558Srgrimes	struct grouplist *grp, *tgrp;
8871558Srgrimes	struct exportlist **epp;
8881558Srgrimes	struct dirlist *dirhead;
8891558Srgrimes	struct statfs fsb, *fsp;
89074462Salfred	struct addrinfo *ai;
89172650Sgreen	struct xucred anon;
8921558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
8931558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
8941558Srgrimes
89551968Salfred	dirp = NULL;
89651968Salfred	dirplen = 0;
89751968Salfred
8981558Srgrimes	/*
8991558Srgrimes	 * First, get rid of the old list
9001558Srgrimes	 */
9011558Srgrimes	ep = exphead;
9021558Srgrimes	while (ep) {
9031558Srgrimes		ep2 = ep;
9041558Srgrimes		ep = ep->ex_next;
9051558Srgrimes		free_exp(ep2);
9061558Srgrimes	}
9071558Srgrimes	exphead = (struct exportlist *)NULL;
9081558Srgrimes
9091558Srgrimes	grp = grphead;
9101558Srgrimes	while (grp) {
9111558Srgrimes		tgrp = grp;
9121558Srgrimes		grp = grp->gr_next;
9131558Srgrimes		free_grp(tgrp);
9141558Srgrimes	}
9151558Srgrimes	grphead = (struct grouplist *)NULL;
9161558Srgrimes
9171558Srgrimes	/*
9181558Srgrimes	 * And delete exports that are in the kernel for all local
9191558Srgrimes	 * file systems.
9201558Srgrimes	 * XXX: Should know how to handle all local exportable file systems
92123681Speter	 *      instead of just "ufs".
9221558Srgrimes	 */
9231558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
9241558Srgrimes	for (i = 0; i < num; i++) {
9251558Srgrimes		union {
9261558Srgrimes			struct ufs_args ua;
9271558Srgrimes			struct iso_args ia;
9281558Srgrimes			struct mfs_args ma;
9299336Sdfr			struct msdosfs_args da;
93054093Ssemenu			struct ntfs_args na;
9311558Srgrimes		} targs;
9321558Srgrimes
93323681Speter		if (!strcmp(fsp->f_fstypename, "mfs") ||
93423681Speter		    !strcmp(fsp->f_fstypename, "ufs") ||
93523681Speter		    !strcmp(fsp->f_fstypename, "msdos") ||
93654093Ssemenu		    !strcmp(fsp->f_fstypename, "ntfs") ||
93723681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
9389336Sdfr			targs.ua.fspec = NULL;
9399336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
9409336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
9411558Srgrimes				  fsp->f_flags | MNT_UPDATE,
9421558Srgrimes				  (caddr_t)&targs) < 0)
94337663Scharnier				syslog(LOG_ERR, "can't delete exports for %s",
94474462Salfred				    fsp->f_mntonname);
9451558Srgrimes		}
9461558Srgrimes		fsp++;
9471558Srgrimes	}
9481558Srgrimes
9491558Srgrimes	/*
9501558Srgrimes	 * Read in the exports file and build the list, calling
9511558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
9521558Srgrimes	 */
9531558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
95437663Scharnier		syslog(LOG_ERR, "can't open %s", exname);
9551558Srgrimes		exit(2);
9561558Srgrimes	}
9571558Srgrimes	dirhead = (struct dirlist *)NULL;
9581558Srgrimes	while (get_line()) {
9591558Srgrimes		if (debug)
96037663Scharnier			warnx("got line %s", line);
9611558Srgrimes		cp = line;
9621558Srgrimes		nextfield(&cp, &endcp);
9631558Srgrimes		if (*cp == '#')
9641558Srgrimes			goto nextline;
9651558Srgrimes
9661558Srgrimes		/*
9671558Srgrimes		 * Set defaults.
9681558Srgrimes		 */
9691558Srgrimes		has_host = FALSE;
9701558Srgrimes		anon = def_anon;
9711558Srgrimes		exflags = MNT_EXPORTED;
9721558Srgrimes		got_nondir = 0;
9731558Srgrimes		opt_flags = 0;
9741558Srgrimes		ep = (struct exportlist *)NULL;
9751558Srgrimes
9761558Srgrimes		/*
9771558Srgrimes		 * Create new exports list entry
9781558Srgrimes		 */
9791558Srgrimes		len = endcp-cp;
9801558Srgrimes		tgrp = grp = get_grp();
9811558Srgrimes		while (len > 0) {
9821558Srgrimes			if (len > RPCMNT_NAMELEN) {
9831558Srgrimes			    getexp_err(ep, tgrp);
9841558Srgrimes			    goto nextline;
9851558Srgrimes			}
9861558Srgrimes			if (*cp == '-') {
9871558Srgrimes			    if (ep == (struct exportlist *)NULL) {
9881558Srgrimes				getexp_err(ep, tgrp);
9891558Srgrimes				goto nextline;
9901558Srgrimes			    }
9911558Srgrimes			    if (debug)
99237663Scharnier				warnx("doing opt %s", cp);
9931558Srgrimes			    got_nondir = 1;
9941558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
9951558Srgrimes				&exflags, &anon)) {
9961558Srgrimes				getexp_err(ep, tgrp);
9971558Srgrimes				goto nextline;
9981558Srgrimes			    }
9991558Srgrimes			} else if (*cp == '/') {
10001558Srgrimes			    savedc = *endcp;
10011558Srgrimes			    *endcp = '\0';
10021558Srgrimes			    if (check_dirpath(cp) &&
10031558Srgrimes				statfs(cp, &fsb) >= 0) {
10041558Srgrimes				if (got_nondir) {
100537663Scharnier				    syslog(LOG_ERR, "dirs must be first");
10061558Srgrimes				    getexp_err(ep, tgrp);
10071558Srgrimes				    goto nextline;
10081558Srgrimes				}
10091558Srgrimes				if (ep) {
10101558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
10111558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
10121558Srgrimes					getexp_err(ep, tgrp);
10131558Srgrimes					goto nextline;
10141558Srgrimes				    }
10151558Srgrimes				} else {
10161558Srgrimes				    /*
10171558Srgrimes				     * See if this directory is already
10181558Srgrimes				     * in the list.
10191558Srgrimes				     */
10201558Srgrimes				    ep = ex_search(&fsb.f_fsid);
10211558Srgrimes				    if (ep == (struct exportlist *)NULL) {
10221558Srgrimes					ep = get_exp();
10231558Srgrimes					ep->ex_fs = fsb.f_fsid;
10241558Srgrimes					ep->ex_fsdir = (char *)
10251558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
10261558Srgrimes					if (ep->ex_fsdir)
10271558Srgrimes					    strcpy(ep->ex_fsdir,
10281558Srgrimes						fsb.f_mntonname);
10291558Srgrimes					else
10301558Srgrimes					    out_of_mem();
10311558Srgrimes					if (debug)
103274462Salfred						warnx("making new ep fs=0x%x,0x%x",
103374462Salfred						    fsb.f_fsid.val[0],
103474462Salfred						    fsb.f_fsid.val[1]);
10351558Srgrimes				    } else if (debug)
103637663Scharnier					warnx("found ep fs=0x%x,0x%x",
10371558Srgrimes					    fsb.f_fsid.val[0],
10381558Srgrimes					    fsb.f_fsid.val[1]);
10391558Srgrimes				}
10401558Srgrimes
10411558Srgrimes				/*
10421558Srgrimes				 * Add dirpath to export mount point.
10431558Srgrimes				 */
10441558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
10451558Srgrimes				dirplen = len;
10461558Srgrimes			    } else {
10471558Srgrimes				getexp_err(ep, tgrp);
10481558Srgrimes				goto nextline;
10491558Srgrimes			    }
10501558Srgrimes			    *endcp = savedc;
10511558Srgrimes			} else {
10521558Srgrimes			    savedc = *endcp;
10531558Srgrimes			    *endcp = '\0';
10541558Srgrimes			    got_nondir = 1;
10551558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10561558Srgrimes				getexp_err(ep, tgrp);
10571558Srgrimes				goto nextline;
10581558Srgrimes			    }
10591558Srgrimes
10601558Srgrimes			    /*
10611558Srgrimes			     * Get the host or netgroup.
10621558Srgrimes			     */
10631558Srgrimes			    setnetgrent(cp);
10641558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
10651558Srgrimes			    do {
10661558Srgrimes				if (has_host) {
10671558Srgrimes				    grp->gr_next = get_grp();
10681558Srgrimes				    grp = grp->gr_next;
10691558Srgrimes				}
10701558Srgrimes				if (netgrp) {
107137003Sjoerg				    if (hst == 0) {
107237663Scharnier					syslog(LOG_ERR,
107337663Scharnier				"null hostname in netgroup %s, skipping", cp);
107437004Sjoerg					grp->gr_type = GT_IGNORE;
107537003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
107637663Scharnier					syslog(LOG_ERR,
107737663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
107829317Sjlemon					grp->gr_type = GT_IGNORE;
10791558Srgrimes				    }
10807401Swpaul				} else if (get_host(cp, grp, tgrp)) {
108137663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
108229317Sjlemon				    grp->gr_type = GT_IGNORE;
10831558Srgrimes				}
10841558Srgrimes				has_host = TRUE;
10851558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
10861558Srgrimes			    endnetgrent();
10871558Srgrimes			    *endcp = savedc;
10881558Srgrimes			}
10891558Srgrimes			cp = endcp;
10901558Srgrimes			nextfield(&cp, &endcp);
10911558Srgrimes			len = endcp - cp;
10921558Srgrimes		}
10931558Srgrimes		if (check_options(dirhead)) {
10941558Srgrimes			getexp_err(ep, tgrp);
10951558Srgrimes			goto nextline;
10961558Srgrimes		}
10971558Srgrimes		if (!has_host) {
10981558Srgrimes			grp->gr_type = GT_HOST;
10991558Srgrimes			if (debug)
110037663Scharnier				warnx("adding a default entry");
11011558Srgrimes			/* add a default group and make the grp list NULL */
110274462Salfred			ai = malloc(sizeof(struct addrinfo));
110374462Salfred			ai->ai_flags = 0;
110474462Salfred			ai->ai_family = AF_INET;        /* XXXX */
110574462Salfred			ai->ai_socktype = SOCK_DGRAM;
110674462Salfred			/* setting the length to 0 will match anything */
110774462Salfred			ai->ai_addrlen = 0;
110874462Salfred			ai->ai_flags = AI_CANONNAME;
110974462Salfred			ai->ai_canonname = strdup("Default");
111074462Salfred			ai->ai_addr = NULL;
111174462Salfred			ai->ai_next = NULL;
111274462Salfred			grp->gr_ptr.gt_addrinfo = ai;
11131558Srgrimes
11141558Srgrimes		/*
11151558Srgrimes		 * Don't allow a network export coincide with a list of
11161558Srgrimes		 * host(s) on the same line.
11171558Srgrimes		 */
11181558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
11191558Srgrimes			getexp_err(ep, tgrp);
11201558Srgrimes			goto nextline;
112129317Sjlemon
112274462Salfred		/*
112374462Salfred		 * If an export list was specified on this line, make sure
112429317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
112529317Sjlemon		 */
112629317Sjlemon		} else {
112729317Sjlemon			grp = tgrp;
112874462Salfred			while (grp && grp->gr_type == GT_IGNORE)
112929317Sjlemon				grp = grp->gr_next;
113029317Sjlemon			if (! grp) {
113129317Sjlemon			    getexp_err(ep, tgrp);
113229317Sjlemon			    goto nextline;
113329317Sjlemon			}
11341558Srgrimes		}
11351558Srgrimes
11361558Srgrimes		/*
11371558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
11381558Srgrimes		 * After loop, tgrp points to the start of the list and
11391558Srgrimes		 * grp points to the last entry in the list.
11401558Srgrimes		 */
11411558Srgrimes		grp = tgrp;
11421558Srgrimes		do {
11431558Srgrimes		    if (do_mount(ep, grp, exflags, &anon, dirp,
11441558Srgrimes			dirplen, &fsb)) {
11451558Srgrimes			getexp_err(ep, tgrp);
11461558Srgrimes			goto nextline;
11471558Srgrimes		    }
11481558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
11491558Srgrimes
11501558Srgrimes		/*
11511558Srgrimes		 * Success. Update the data structures.
11521558Srgrimes		 */
11531558Srgrimes		if (has_host) {
11549336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
11551558Srgrimes			grp->gr_next = grphead;
11561558Srgrimes			grphead = tgrp;
11571558Srgrimes		} else {
11581558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
11599336Sdfr				opt_flags);
11601558Srgrimes			free_grp(grp);
11611558Srgrimes		}
11621558Srgrimes		dirhead = (struct dirlist *)NULL;
11631558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
11641558Srgrimes			ep2 = exphead;
11651558Srgrimes			epp = &exphead;
11661558Srgrimes
11671558Srgrimes			/*
11681558Srgrimes			 * Insert in the list in alphabetical order.
11691558Srgrimes			 */
11701558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
11711558Srgrimes				epp = &ep2->ex_next;
11721558Srgrimes				ep2 = ep2->ex_next;
11731558Srgrimes			}
11741558Srgrimes			if (ep2)
11751558Srgrimes				ep->ex_next = ep2;
11761558Srgrimes			*epp = ep;
11771558Srgrimes			ep->ex_flag |= EX_LINKED;
11781558Srgrimes		}
11791558Srgrimesnextline:
11801558Srgrimes		if (dirhead) {
11811558Srgrimes			free_dir(dirhead);
11821558Srgrimes			dirhead = (struct dirlist *)NULL;
11831558Srgrimes		}
11841558Srgrimes	}
11851558Srgrimes	fclose(exp_file);
11861558Srgrimes}
11871558Srgrimes
11881558Srgrimes/*
11891558Srgrimes * Allocate an export list element
11901558Srgrimes */
11911558Srgrimesstruct exportlist *
11921558Srgrimesget_exp()
11931558Srgrimes{
11941558Srgrimes	struct exportlist *ep;
11951558Srgrimes
11961558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
11971558Srgrimes	if (ep == (struct exportlist *)NULL)
11981558Srgrimes		out_of_mem();
119923681Speter	memset(ep, 0, sizeof(struct exportlist));
12001558Srgrimes	return (ep);
12011558Srgrimes}
12021558Srgrimes
12031558Srgrimes/*
12041558Srgrimes * Allocate a group list element
12051558Srgrimes */
12061558Srgrimesstruct grouplist *
12071558Srgrimesget_grp()
12081558Srgrimes{
12091558Srgrimes	struct grouplist *gp;
12101558Srgrimes
12111558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
12121558Srgrimes	if (gp == (struct grouplist *)NULL)
12131558Srgrimes		out_of_mem();
121423681Speter	memset(gp, 0, sizeof(struct grouplist));
12151558Srgrimes	return (gp);
12161558Srgrimes}
12171558Srgrimes
12181558Srgrimes/*
12191558Srgrimes * Clean up upon an error in get_exportlist().
12201558Srgrimes */
12211558Srgrimesvoid
12221558Srgrimesgetexp_err(ep, grp)
12231558Srgrimes	struct exportlist *ep;
12241558Srgrimes	struct grouplist *grp;
12251558Srgrimes{
12261558Srgrimes	struct grouplist *tgrp;
12271558Srgrimes
122837663Scharnier	syslog(LOG_ERR, "bad exports list line %s", line);
12291558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
12301558Srgrimes		free_exp(ep);
12311558Srgrimes	while (grp) {
12321558Srgrimes		tgrp = grp;
12331558Srgrimes		grp = grp->gr_next;
12341558Srgrimes		free_grp(tgrp);
12351558Srgrimes	}
12361558Srgrimes}
12371558Srgrimes
12381558Srgrimes/*
12391558Srgrimes * Search the export list for a matching fs.
12401558Srgrimes */
12411558Srgrimesstruct exportlist *
12421558Srgrimesex_search(fsid)
12431558Srgrimes	fsid_t *fsid;
12441558Srgrimes{
12451558Srgrimes	struct exportlist *ep;
12461558Srgrimes
12471558Srgrimes	ep = exphead;
12481558Srgrimes	while (ep) {
12491558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
12501558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
12511558Srgrimes			return (ep);
12521558Srgrimes		ep = ep->ex_next;
12531558Srgrimes	}
12541558Srgrimes	return (ep);
12551558Srgrimes}
12561558Srgrimes
12571558Srgrimes/*
12581558Srgrimes * Add a directory path to the list.
12591558Srgrimes */
12601558Srgrimeschar *
12611558Srgrimesadd_expdir(dpp, cp, len)
12621558Srgrimes	struct dirlist **dpp;
12631558Srgrimes	char *cp;
12641558Srgrimes	int len;
12651558Srgrimes{
12661558Srgrimes	struct dirlist *dp;
12671558Srgrimes
12681558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
126937663Scharnier	if (dp == (struct dirlist *)NULL)
127037663Scharnier		out_of_mem();
12711558Srgrimes	dp->dp_left = *dpp;
12721558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
12731558Srgrimes	dp->dp_flag = 0;
12741558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
12751558Srgrimes	strcpy(dp->dp_dirp, cp);
12761558Srgrimes	*dpp = dp;
12771558Srgrimes	return (dp->dp_dirp);
12781558Srgrimes}
12791558Srgrimes
12801558Srgrimes/*
12811558Srgrimes * Hang the dir list element off the dirpath binary tree as required
12821558Srgrimes * and update the entry for host.
12831558Srgrimes */
12841558Srgrimesvoid
12859336Sdfrhang_dirp(dp, grp, ep, flags)
12861558Srgrimes	struct dirlist *dp;
12871558Srgrimes	struct grouplist *grp;
12881558Srgrimes	struct exportlist *ep;
12899336Sdfr	int flags;
12901558Srgrimes{
12911558Srgrimes	struct hostlist *hp;
12921558Srgrimes	struct dirlist *dp2;
12931558Srgrimes
12949336Sdfr	if (flags & OP_ALLDIRS) {
12951558Srgrimes		if (ep->ex_defdir)
12961558Srgrimes			free((caddr_t)dp);
12971558Srgrimes		else
12981558Srgrimes			ep->ex_defdir = dp;
12999336Sdfr		if (grp == (struct grouplist *)NULL) {
13001558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
13019336Sdfr			if (flags & OP_KERB)
13029336Sdfr				ep->ex_defdir->dp_flag |= DP_KERB;
13039336Sdfr		} else while (grp) {
13041558Srgrimes			hp = get_ht();
13059336Sdfr			if (flags & OP_KERB)
13069336Sdfr				hp->ht_flag |= DP_KERB;
13071558Srgrimes			hp->ht_grp = grp;
13081558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
13091558Srgrimes			ep->ex_defdir->dp_hosts = hp;
13101558Srgrimes			grp = grp->gr_next;
13111558Srgrimes		}
13121558Srgrimes	} else {
13131558Srgrimes
13141558Srgrimes		/*
131537663Scharnier		 * Loop through the directories adding them to the tree.
13161558Srgrimes		 */
13171558Srgrimes		while (dp) {
13181558Srgrimes			dp2 = dp->dp_left;
13199336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
13201558Srgrimes			dp = dp2;
13211558Srgrimes		}
13221558Srgrimes	}
13231558Srgrimes}
13241558Srgrimes
13251558Srgrimes/*
13261558Srgrimes * Traverse the binary tree either updating a node that is already there
13271558Srgrimes * for the new directory or adding the new node.
13281558Srgrimes */
13291558Srgrimesvoid
13309336Sdfradd_dlist(dpp, newdp, grp, flags)
13311558Srgrimes	struct dirlist **dpp;
13321558Srgrimes	struct dirlist *newdp;
13331558Srgrimes	struct grouplist *grp;
13349336Sdfr	int flags;
13351558Srgrimes{
13361558Srgrimes	struct dirlist *dp;
13371558Srgrimes	struct hostlist *hp;
13381558Srgrimes	int cmp;
13391558Srgrimes
13401558Srgrimes	dp = *dpp;
13411558Srgrimes	if (dp) {
13421558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
13431558Srgrimes		if (cmp > 0) {
13449336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
13451558Srgrimes			return;
13461558Srgrimes		} else if (cmp < 0) {
13479336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
13481558Srgrimes			return;
13491558Srgrimes		} else
13501558Srgrimes			free((caddr_t)newdp);
13511558Srgrimes	} else {
13521558Srgrimes		dp = newdp;
13531558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
13541558Srgrimes		*dpp = dp;
13551558Srgrimes	}
13561558Srgrimes	if (grp) {
13571558Srgrimes
13581558Srgrimes		/*
13591558Srgrimes		 * Hang all of the host(s) off of the directory point.
13601558Srgrimes		 */
13611558Srgrimes		do {
13621558Srgrimes			hp = get_ht();
13639336Sdfr			if (flags & OP_KERB)
13649336Sdfr				hp->ht_flag |= DP_KERB;
13651558Srgrimes			hp->ht_grp = grp;
13661558Srgrimes			hp->ht_next = dp->dp_hosts;
13671558Srgrimes			dp->dp_hosts = hp;
13681558Srgrimes			grp = grp->gr_next;
13691558Srgrimes		} while (grp);
13709336Sdfr	} else {
13711558Srgrimes		dp->dp_flag |= DP_DEFSET;
13729336Sdfr		if (flags & OP_KERB)
13739336Sdfr			dp->dp_flag |= DP_KERB;
13749336Sdfr	}
13751558Srgrimes}
13761558Srgrimes
13771558Srgrimes/*
13781558Srgrimes * Search for a dirpath on the export point.
13791558Srgrimes */
138074462Salfredvoid *
138174462Salfredtest()
138274462Salfred{
138374462Salfred}
138474462Salfred
138574462Salfred/*
138674462Salfred * Search for a dirpath on the export point.
138774462Salfred */
13881558Srgrimesstruct dirlist *
138974462Salfreddirp_search(dp, dirp)
13901558Srgrimes	struct dirlist *dp;
139174462Salfred	char *dirp;
13921558Srgrimes{
13931558Srgrimes	int cmp;
13941558Srgrimes
13951558Srgrimes	if (dp) {
139674462Salfred		cmp = strcmp(dp->dp_dirp, dirp);
13971558Srgrimes		if (cmp > 0)
139874462Salfred			return (dirp_search(dp->dp_left, dirp));
13991558Srgrimes		else if (cmp < 0)
140074462Salfred			return (dirp_search(dp->dp_right, dirp));
14011558Srgrimes		else
14021558Srgrimes			return (dp);
14031558Srgrimes	}
14041558Srgrimes	return (dp);
14051558Srgrimes}
14061558Srgrimes
14071558Srgrimes/*
140874462Salfred * Some helper functions for netmasks. They all assume masks in network
140974462Salfred * order (big endian).
141074462Salfred */
141174462Salfredstatic int
141274462Salfredbitcmp(void *dst, void *src, int bitlen)
141374462Salfred{
141474462Salfred	int i;
141574462Salfred	u_int8_t *p1 = dst, *p2 = src;
141674462Salfred	u_int8_t bitmask;
141774462Salfred	int bytelen, bitsleft;
141874462Salfred
141974462Salfred	bytelen = bitlen / 8;
142074462Salfred	bitsleft = bitlen % 8;
142174462Salfred
142274462Salfred	if (debug) {
142374462Salfred		printf("comparing:\n");
142474462Salfred		for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
142574462Salfred			printf("%02x", p1[i]);
142674462Salfred		printf("\n");
142774462Salfred		for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
142874462Salfred			printf("%02x", p2[i]);
142974462Salfred		printf("\n");
143074462Salfred	}
143174462Salfred
143274462Salfred	for (i = 0; i < bytelen; i++) {
143374462Salfred		if (*p1 != *p2)
143474462Salfred			return 1;
143574462Salfred		p1++;
143674462Salfred		p2++;
143774462Salfred	}
143874462Salfred
143974462Salfred	for (i = 0; i < bitsleft; i++) {
144074462Salfred		bitmask = 1 << (7 - i);
144174462Salfred		if ((*p1 & bitmask) != (*p2 & bitmask))
144274462Salfred			return 1;
144374462Salfred	}
144474462Salfred
144574462Salfred	return 0;
144674462Salfred}
144774462Salfred
144874462Salfred/*
14491558Srgrimes * Scan for a host match in a directory tree.
14501558Srgrimes */
14511558Srgrimesint
14529336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
14531558Srgrimes	struct dirlist *dp;
145474462Salfred	struct sockaddr *saddr;
14551558Srgrimes	int *defsetp;
14569336Sdfr	int *hostsetp;
14571558Srgrimes{
14581558Srgrimes	struct hostlist *hp;
14591558Srgrimes	struct grouplist *grp;
146074462Salfred	struct addrinfo *ai;
14611558Srgrimes
14621558Srgrimes	if (dp) {
14631558Srgrimes		if (dp->dp_flag & DP_DEFSET)
14649336Sdfr			*defsetp = dp->dp_flag;
14651558Srgrimes		hp = dp->dp_hosts;
14661558Srgrimes		while (hp) {
14671558Srgrimes			grp = hp->ht_grp;
14681558Srgrimes			switch (grp->gr_type) {
14691558Srgrimes			case GT_HOST:
147074462Salfred				ai = grp->gr_ptr.gt_addrinfo;
147174462Salfred				for (; ai; ai = ai->ai_next) {
147274462Salfred					if (!sacmp(ai->ai_addr, saddr)) {
147374462Salfred						*hostsetp =
147474462Salfred						    (hp->ht_flag | DP_HOSTSET);
147574462Salfred						return (1);
147674462Salfred					}
14779336Sdfr				}
14781558Srgrimes			    break;
14791558Srgrimes			case GT_NET:
148074462Salfred				if (!netpartcmp(saddr,
148174462Salfred				    (struct sockaddr *) &grp->gr_ptr.gt_net.nt_net,
148274462Salfred				     grp->gr_ptr.gt_net.nt_mask)) {
148374462Salfred					*hostsetp = (hp->ht_flag | DP_HOSTSET);
148474462Salfred					return (1);
148574462Salfred				}
14861558Srgrimes			    break;
14871558Srgrimes			};
14881558Srgrimes			hp = hp->ht_next;
14891558Srgrimes		}
14901558Srgrimes	}
14911558Srgrimes	return (0);
14921558Srgrimes}
14931558Srgrimes
14941558Srgrimes/*
14951558Srgrimes * Scan tree for a host that matches the address.
14961558Srgrimes */
14971558Srgrimesint
14981558Srgrimesscan_tree(dp, saddr)
14991558Srgrimes	struct dirlist *dp;
150074462Salfred	struct sockaddr *saddr;
15011558Srgrimes{
15029336Sdfr	int defset, hostset;
15031558Srgrimes
15041558Srgrimes	if (dp) {
15051558Srgrimes		if (scan_tree(dp->dp_left, saddr))
15061558Srgrimes			return (1);
15079336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
15081558Srgrimes			return (1);
15091558Srgrimes		if (scan_tree(dp->dp_right, saddr))
15101558Srgrimes			return (1);
15111558Srgrimes	}
15121558Srgrimes	return (0);
15131558Srgrimes}
15141558Srgrimes
15151558Srgrimes/*
15161558Srgrimes * Traverse the dirlist tree and free it up.
15171558Srgrimes */
15181558Srgrimesvoid
15191558Srgrimesfree_dir(dp)
15201558Srgrimes	struct dirlist *dp;
15211558Srgrimes{
15221558Srgrimes
15231558Srgrimes	if (dp) {
15241558Srgrimes		free_dir(dp->dp_left);
15251558Srgrimes		free_dir(dp->dp_right);
15261558Srgrimes		free_host(dp->dp_hosts);
15271558Srgrimes		free((caddr_t)dp);
15281558Srgrimes	}
15291558Srgrimes}
15301558Srgrimes
15311558Srgrimes/*
15321558Srgrimes * Parse the option string and update fields.
15331558Srgrimes * Option arguments may either be -<option>=<value> or
15341558Srgrimes * -<option> <value>
15351558Srgrimes */
15361558Srgrimesint
15371558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
15381558Srgrimes	char **cpp, **endcpp;
15391558Srgrimes	struct exportlist *ep;
15401558Srgrimes	struct grouplist *grp;
15411558Srgrimes	int *has_hostp;
15421558Srgrimes	int *exflagsp;
154372650Sgreen	struct xucred *cr;
15441558Srgrimes{
15451558Srgrimes	char *cpoptarg, *cpoptend;
15461558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
15471558Srgrimes	int allflag, usedarg;
15481558Srgrimes
154951968Salfred	savedc2 = '\0';
15501558Srgrimes	cpopt = *cpp;
15511558Srgrimes	cpopt++;
15521558Srgrimes	cp = *endcpp;
15531558Srgrimes	savedc = *cp;
15541558Srgrimes	*cp = '\0';
15551558Srgrimes	while (cpopt && *cpopt) {
15561558Srgrimes		allflag = 1;
15571558Srgrimes		usedarg = -2;
155837663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
15591558Srgrimes			*cpoptend++ = '\0';
156037663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15611558Srgrimes				*cpoptarg++ = '\0';
15621558Srgrimes		} else {
156337663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15641558Srgrimes				*cpoptarg++ = '\0';
15651558Srgrimes			else {
15661558Srgrimes				*cp = savedc;
15671558Srgrimes				nextfield(&cp, &endcp);
15681558Srgrimes				**endcpp = '\0';
15691558Srgrimes				if (endcp > cp && *cp != '-') {
15701558Srgrimes					cpoptarg = cp;
15711558Srgrimes					savedc2 = *endcp;
15721558Srgrimes					*endcp = '\0';
15731558Srgrimes					usedarg = 0;
15741558Srgrimes				}
15751558Srgrimes			}
15761558Srgrimes		}
15771558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
15781558Srgrimes			*exflagsp |= MNT_EXRDONLY;
15791558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
15801558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
15811558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
15821558Srgrimes			usedarg++;
15831558Srgrimes			parsecred(cpoptarg, cr);
15841558Srgrimes			if (allflag == 0) {
15851558Srgrimes				*exflagsp |= MNT_EXPORTANON;
15861558Srgrimes				opt_flags |= OP_MAPALL;
15871558Srgrimes			} else
15881558Srgrimes				opt_flags |= OP_MAPROOT;
15891558Srgrimes		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
15901558Srgrimes			*exflagsp |= MNT_EXKERB;
15911558Srgrimes			opt_flags |= OP_KERB;
15921558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
15931558Srgrimes			!strcmp(cpopt, "m"))) {
15941558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
159537663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
15961558Srgrimes				return (1);
15971558Srgrimes			}
15981558Srgrimes			usedarg++;
15991558Srgrimes			opt_flags |= OP_MASK;
16001558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
16011558Srgrimes			!strcmp(cpopt, "n"))) {
160274462Salfred			if (strchr(cpoptarg, '/') != NULL) {
160374462Salfred				if (debug)
160474462Salfred					fprintf(stderr, "setting OP_MASKLEN\n");
160574462Salfred				opt_flags |= OP_MASKLEN;
160674462Salfred			}
16071558Srgrimes			if (grp->gr_type != GT_NULL) {
160837663Scharnier				syslog(LOG_ERR, "network/host conflict");
16091558Srgrimes				return (1);
16101558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
161137663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
16121558Srgrimes				return (1);
16131558Srgrimes			}
16141558Srgrimes			grp->gr_type = GT_NET;
16151558Srgrimes			*has_hostp = 1;
16161558Srgrimes			usedarg++;
16171558Srgrimes			opt_flags |= OP_NET;
16181558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
16191558Srgrimes			opt_flags |= OP_ALLDIRS;
162027447Sdfr		} else if (!strcmp(cpopt, "public")) {
162127447Sdfr			*exflagsp |= MNT_EXPUBLIC;
162227447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
162327447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
162427447Sdfr			opt_flags |= OP_MAPALL;
162527447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
162627447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
16271558Srgrimes		} else {
162837663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
16291558Srgrimes			return (1);
16301558Srgrimes		}
16311558Srgrimes		if (usedarg >= 0) {
16321558Srgrimes			*endcp = savedc2;
16331558Srgrimes			**endcpp = savedc;
16341558Srgrimes			if (usedarg > 0) {
16351558Srgrimes				*cpp = cp;
16361558Srgrimes				*endcpp = endcp;
16371558Srgrimes			}
16381558Srgrimes			return (0);
16391558Srgrimes		}
16401558Srgrimes		cpopt = cpoptend;
16411558Srgrimes	}
16421558Srgrimes	**endcpp = savedc;
16431558Srgrimes	return (0);
16441558Srgrimes}
16451558Srgrimes
16461558Srgrimes/*
16471558Srgrimes * Translate a character string to the corresponding list of network
16481558Srgrimes * addresses for a hostname.
16491558Srgrimes */
16501558Srgrimesint
16517401Swpaulget_host(cp, grp, tgrp)
16521558Srgrimes	char *cp;
16531558Srgrimes	struct grouplist *grp;
16547401Swpaul	struct grouplist *tgrp;
16551558Srgrimes{
16567401Swpaul	struct grouplist *checkgrp;
165774462Salfred	struct addrinfo *ai, hints;
165874462Salfred	int ecode;
165974462Salfred	char host[NI_MAXHOST];
16601558Srgrimes	int i;
16611558Srgrimes	char *aptr[2];
16621558Srgrimes
166374462Salfred	if (grp->gr_type != GT_NULL) {
166474462Salfred		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
16651558Srgrimes		return (1);
16661558Srgrimes	}
166774462Salfred	memset(&hints, 0, sizeof hints);
166874462Salfred	hints.ai_flags = AI_CANONNAME;
166974462Salfred	hints.ai_protocol = IPPROTO_UDP;
167074462Salfred	ecode = getaddrinfo(cp, NULL, &hints, &ai);
167174462Salfred	if (ecode != 0) {
167274462Salfred		syslog(LOG_ERR,"can't get address info for "
167374462Salfred		    "host %s", cp);
167474462Salfred		return 1;
167574462Salfred	}
16761558Srgrimes	grp->gr_type = GT_HOST;
167774462Salfred	grp->gr_ptr.gt_addrinfo = ai;
167874462Salfred	while (ai != NULL) {
167974462Salfred		if (ai->ai_canonname == NULL) {
168074462Salfred			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
168174462Salfred			    sizeof host, NULL, 0, ninumeric) != 0)
168274462Salfred				strlcpy(host, "?", sizeof(host));
168374462Salfred			ai->ai_canonname = strdup(host);
168474462Salfred			ai->ai_flags |= AI_CANONNAME;
168574462Salfred		} else
168674462Salfred			ai->ai_flags &= ~AI_CANONNAME;
168774462Salfred		if (debug)
168874462Salfred			(void)fprintf(stderr, "got host %s\n", ai->ai_canonname);
168974462Salfred		ai = ai->ai_next;
16901558Srgrimes	}
16911558Srgrimes	return (0);
16921558Srgrimes}
16931558Srgrimes
16941558Srgrimes/*
16951558Srgrimes * Free up an exports list component
16961558Srgrimes */
16971558Srgrimesvoid
16981558Srgrimesfree_exp(ep)
16991558Srgrimes	struct exportlist *ep;
17001558Srgrimes{
17011558Srgrimes
17021558Srgrimes	if (ep->ex_defdir) {
17031558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
17041558Srgrimes		free((caddr_t)ep->ex_defdir);
17051558Srgrimes	}
17061558Srgrimes	if (ep->ex_fsdir)
17071558Srgrimes		free(ep->ex_fsdir);
170827447Sdfr	if (ep->ex_indexfile)
170927447Sdfr		free(ep->ex_indexfile);
17101558Srgrimes	free_dir(ep->ex_dirl);
17111558Srgrimes	free((caddr_t)ep);
17121558Srgrimes}
17131558Srgrimes
17141558Srgrimes/*
17151558Srgrimes * Free hosts.
17161558Srgrimes */
17171558Srgrimesvoid
17181558Srgrimesfree_host(hp)
17191558Srgrimes	struct hostlist *hp;
17201558Srgrimes{
17211558Srgrimes	struct hostlist *hp2;
17221558Srgrimes
17231558Srgrimes	while (hp) {
17241558Srgrimes		hp2 = hp;
17251558Srgrimes		hp = hp->ht_next;
17261558Srgrimes		free((caddr_t)hp2);
17271558Srgrimes	}
17281558Srgrimes}
17291558Srgrimes
17301558Srgrimesstruct hostlist *
17311558Srgrimesget_ht()
17321558Srgrimes{
17331558Srgrimes	struct hostlist *hp;
17341558Srgrimes
17351558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
17361558Srgrimes	if (hp == (struct hostlist *)NULL)
17371558Srgrimes		out_of_mem();
17381558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
17399336Sdfr	hp->ht_flag = 0;
17401558Srgrimes	return (hp);
17411558Srgrimes}
17421558Srgrimes
17431558Srgrimes/*
17441558Srgrimes * Out of memory, fatal
17451558Srgrimes */
17461558Srgrimesvoid
17471558Srgrimesout_of_mem()
17481558Srgrimes{
17491558Srgrimes
175037663Scharnier	syslog(LOG_ERR, "out of memory");
17511558Srgrimes	exit(2);
17521558Srgrimes}
17531558Srgrimes
17541558Srgrimes/*
17551558Srgrimes * Do the mount syscall with the update flag to push the export info into
17561558Srgrimes * the kernel.
17571558Srgrimes */
17581558Srgrimesint
17591558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
17601558Srgrimes	struct exportlist *ep;
17611558Srgrimes	struct grouplist *grp;
17621558Srgrimes	int exflags;
176372650Sgreen	struct xucred *anoncrp;
17641558Srgrimes	char *dirp;
17651558Srgrimes	int dirplen;
17661558Srgrimes	struct statfs *fsb;
17671558Srgrimes{
176874462Salfred	struct sockaddr *addrp;
176974462Salfred	struct sockaddr_storage ss;
177074462Salfred	struct addrinfo *ai;
177174462Salfred	int addrlen;
177274462Salfred	char *cp = NULL;
17731558Srgrimes	int done;
17741558Srgrimes	char savedc = '\0';
17751558Srgrimes	union {
17761558Srgrimes		struct ufs_args ua;
17771558Srgrimes		struct iso_args ia;
17781558Srgrimes		struct mfs_args ma;
17799336Sdfr#ifdef __NetBSD__
17809336Sdfr		struct msdosfs_args da;
178174462Salfred		struct adosfs_args aa;
17829336Sdfr#endif
178354093Ssemenu		struct ntfs_args na;
17841558Srgrimes	} args;
17851558Srgrimes
17861558Srgrimes	args.ua.fspec = 0;
17871558Srgrimes	args.ua.export.ex_flags = exflags;
17881558Srgrimes	args.ua.export.ex_anon = *anoncrp;
178927447Sdfr	args.ua.export.ex_indexfile = ep->ex_indexfile;
179074462Salfred	if (grp->gr_type == GT_HOST) {
179174462Salfred		ai = grp->gr_ptr.gt_addrinfo;
179274462Salfred		addrp = ai->ai_addr;
179374462Salfred		addrlen = ai->ai_addrlen;
179474462Salfred	} else
179574462Salfred		addrp = NULL;
17961558Srgrimes	done = FALSE;
17971558Srgrimes	while (!done) {
17981558Srgrimes		switch (grp->gr_type) {
17991558Srgrimes		case GT_HOST:
180074462Salfred			if (addrp != NULL && addrp->sa_family == AF_INET6 &&
180174462Salfred			    have_v6 == 0)
180274462Salfred				goto skip;
180374462Salfred			args.ua.export.ex_addr = addrp;
180474462Salfred			args.ua.export.ex_addrlen = addrlen;
18051558Srgrimes			args.ua.export.ex_masklen = 0;
18061558Srgrimes			break;
18071558Srgrimes		case GT_NET:
180874462Salfred			args.ua.export.ex_addr = (struct sockaddr *)
180974462Salfred			    &grp->gr_ptr.gt_net.nt_net;
181074462Salfred			if (args.ua.export.ex_addr->sa_family == AF_INET6 &&
181174462Salfred			    have_v6 == 0)
181274462Salfred				goto skip;
181374462Salfred			args.ua.export.ex_addrlen =
181474462Salfred			    args.ua.export.ex_addr->sa_len;
181574462Salfred			memset(&ss, 0, sizeof ss);
181674462Salfred			ss.ss_family = args.ua.export.ex_addr->sa_family;
181774462Salfred			ss.ss_len = args.ua.export.ex_addr->sa_len;
181874462Salfred			if (allones(&ss, grp->gr_ptr.gt_net.nt_mask) != 0) {
181974462Salfred				syslog(LOG_ERR, "Bad network flag");
182074462Salfred				if (cp)
182174462Salfred					*cp = savedc;
182274462Salfred				return (1);
18231558Srgrimes			}
182474462Salfred			args.ua.export.ex_mask = (struct sockaddr *)&ss;
182574462Salfred			args.ua.export.ex_masklen = ss.ss_len;
18261558Srgrimes			break;
18277401Swpaul		case GT_IGNORE:
18287401Swpaul			return(0);
18297401Swpaul			break;
18301558Srgrimes		default:
183137663Scharnier			syslog(LOG_ERR, "bad grouptype");
18321558Srgrimes			if (cp)
18331558Srgrimes				*cp = savedc;
18341558Srgrimes			return (1);
18351558Srgrimes		};
18361558Srgrimes
18371558Srgrimes		/*
18381558Srgrimes		 * XXX:
18391558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
18401558Srgrimes		 * of looping back up the dirp to the mount point??
18411558Srgrimes		 * Also, needs to know how to export all types of local
184223681Speter		 * exportable file systems and not just "ufs".
18431558Srgrimes		 */
18449336Sdfr		while (mount(fsb->f_fstypename, dirp,
184574462Salfred		    fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
18461558Srgrimes			if (cp)
18471558Srgrimes				*cp-- = savedc;
18481558Srgrimes			else
18491558Srgrimes				cp = dirp + dirplen - 1;
18501558Srgrimes			if (errno == EPERM) {
18511558Srgrimes				syslog(LOG_ERR,
185237663Scharnier				   "can't change attributes for %s", dirp);
18531558Srgrimes				return (1);
18541558Srgrimes			}
18551558Srgrimes			if (opt_flags & OP_ALLDIRS) {
185637663Scharnier				syslog(LOG_ERR, "could not remount %s: %m",
18574895Swollman					dirp);
18581558Srgrimes				return (1);
18591558Srgrimes			}
18601558Srgrimes			/* back up over the last component */
18611558Srgrimes			while (*cp == '/' && cp > dirp)
18621558Srgrimes				cp--;
18631558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
18641558Srgrimes				cp--;
18651558Srgrimes			if (cp == dirp) {
18661558Srgrimes				if (debug)
186737663Scharnier					warnx("mnt unsucc");
186837663Scharnier				syslog(LOG_ERR, "can't export %s", dirp);
18691558Srgrimes				return (1);
18701558Srgrimes			}
18711558Srgrimes			savedc = *cp;
18721558Srgrimes			*cp = '\0';
18731558Srgrimes		}
187474462Salfredskip:
18751558Srgrimes		if (addrp) {
187674462Salfred			ai = ai->ai_next;
187774462Salfred			if (ai == NULL)
18781558Srgrimes				done = TRUE;
187974462Salfred			else {
188074462Salfred				addrp = ai->ai_addr;
188174462Salfred				addrlen = ai->ai_addrlen;
188274462Salfred			}
18831558Srgrimes		} else
18841558Srgrimes			done = TRUE;
18851558Srgrimes	}
18861558Srgrimes	if (cp)
18871558Srgrimes		*cp = savedc;
18881558Srgrimes	return (0);
18891558Srgrimes}
18901558Srgrimes
18911558Srgrimes/*
18921558Srgrimes * Translate a net address.
18931558Srgrimes */
18941558Srgrimesint
18951558Srgrimesget_net(cp, net, maskflg)
18961558Srgrimes	char *cp;
18971558Srgrimes	struct netmsk *net;
18981558Srgrimes	int maskflg;
18991558Srgrimes{
19001558Srgrimes	struct netent *np;
190174462Salfred	char *name, *p, *prefp;
190274462Salfred	struct sockaddr_in sin, *sinp;
190374462Salfred	struct sockaddr *sa;
190474462Salfred	struct addrinfo hints, *ai = NULL;
190574462Salfred	char netname[NI_MAXHOST];
190674462Salfred	long preflen;
190774462Salfred	int ecode;
19081558Srgrimes
190974462Salfred	if ((opt_flags & OP_MASKLEN) && !maskflg) {
191074462Salfred		p = strchr(cp, '/');
191174462Salfred		*p = '\0';
191274462Salfred		prefp = p + 1;
191374462Salfred	}
191474462Salfred
191574462Salfred	if ((np = getnetbyname(cp)) != NULL) {
191674462Salfred		sin.sin_family = AF_INET;
191774462Salfred		sin.sin_len = sizeof sin;
191874462Salfred		sin.sin_addr = inet_makeaddr(np->n_net, 0);
191974462Salfred		sa = (struct sockaddr *)&sin;
192074462Salfred	} else if (isdigit(*cp)) {
192174462Salfred		memset(&hints, 0, sizeof hints);
192274462Salfred		hints.ai_family = AF_UNSPEC;
192374462Salfred		hints.ai_flags = AI_NUMERICHOST;
192474462Salfred		if (getaddrinfo(cp, NULL, &hints, &ai) != 0) {
192574462Salfred			/*
192674462Salfred			 * If getaddrinfo() failed, try the inet4 network
192774462Salfred			 * notation with less than 3 dots.
192874462Salfred			 */
192974462Salfred			sin.sin_family = AF_INET;
193074462Salfred			sin.sin_len = sizeof sin;
193174462Salfred			sin.sin_addr = inet_makeaddr(inet_network(cp),0);
193274462Salfred			if (debug)
193374462Salfred				fprintf(stderr, "get_net: v4 addr %x\n",
193474462Salfred				    sin.sin_addr.s_addr);
193574462Salfred			sa = (struct sockaddr *)&sin;
193674462Salfred		} else
193774462Salfred			sa = ai->ai_addr;
193874462Salfred	} else if (isxdigit(*cp) || *cp == ':') {
193974462Salfred		memset(&hints, 0, sizeof hints);
194074462Salfred		hints.ai_family = AF_UNSPEC;
194174462Salfred		hints.ai_flags = AI_NUMERICHOST;
194274462Salfred		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
194374462Salfred			sa = ai->ai_addr;
194474462Salfred		else
194574462Salfred			goto fail;
19461558Srgrimes	} else
194774462Salfred		goto fail;
194825318Spst
194974462Salfred	ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname,
195074462Salfred	    NULL, 0, ninumeric);
195174462Salfred	if (ecode != 0)
195274462Salfred		goto fail;
195374462Salfred
19541558Srgrimes	if (maskflg)
195574462Salfred		net->nt_mask = countones(sa);
19561558Srgrimes	else {
195774462Salfred		if (opt_flags & OP_MASKLEN) {
195874462Salfred			preflen = strtol(prefp, NULL, 10);
195974462Salfred			if (preflen == LONG_MIN && errno == ERANGE)
196074462Salfred				goto fail;
196174462Salfred			net->nt_mask = (int)preflen;
196274462Salfred			*p = '/';
196374462Salfred		}
196474462Salfred
19651558Srgrimes		if (np)
19661558Srgrimes			name = np->n_name;
196774462Salfred		else {
196874462Salfred			if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
196974462Salfred			   NULL, 0, ninumeric) != 0)
197074462Salfred				strlcpy(netname, "?", sizeof(netname));
197174462Salfred			name = netname;
197274462Salfred		}
197374462Salfred		net->nt_name = strdup(name);
197474462Salfred		memcpy(&net->nt_net, sa, sa->sa_len);
197574462Salfred	}
197674462Salfred
197774462Salfred	if (!maskflg && sa->sa_family == AF_INET &&
197874462Salfred	    !(opt_flags & (OP_MASK|OP_MASKLEN))) {
197974462Salfred		sinp = (struct sockaddr_in *)sa;
198074462Salfred		if (IN_CLASSA(sinp->sin_addr.s_addr))
198174462Salfred			net->nt_mask = 8;
198274462Salfred		else if (IN_CLASSB(sinp->sin_addr.s_addr))
198374462Salfred			net->nt_mask = 16;
198474462Salfred		else if (IN_CLASSC(sinp->sin_addr.s_addr))
198574462Salfred			net->nt_mask = 24;
198674462Salfred		else if (IN_CLASSD(sinp->sin_addr.s_addr))
198774462Salfred			net->nt_mask = 28;
19881558Srgrimes		else
198974462Salfred			net->nt_mask = 32;       /* XXX */
19901558Srgrimes	}
199174462Salfred
199274462Salfred	if (ai)
199374462Salfred		freeaddrinfo(ai);
199474462Salfred	return 0;
199574462Salfred
199674462Salfredfail:
199774462Salfred	if (ai)
199874462Salfred		freeaddrinfo(ai);
199974462Salfred	return 1;
20001558Srgrimes}
20011558Srgrimes
20021558Srgrimes/*
20031558Srgrimes * Parse out the next white space separated field
20041558Srgrimes */
20051558Srgrimesvoid
20061558Srgrimesnextfield(cp, endcp)
20071558Srgrimes	char **cp;
20081558Srgrimes	char **endcp;
20091558Srgrimes{
20101558Srgrimes	char *p;
20111558Srgrimes
20121558Srgrimes	p = *cp;
20131558Srgrimes	while (*p == ' ' || *p == '\t')
20141558Srgrimes		p++;
20151558Srgrimes	if (*p == '\n' || *p == '\0')
20161558Srgrimes		*cp = *endcp = p;
20171558Srgrimes	else {
20181558Srgrimes		*cp = p++;
20191558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
20201558Srgrimes			p++;
20211558Srgrimes		*endcp = p;
20221558Srgrimes	}
20231558Srgrimes}
20241558Srgrimes
20251558Srgrimes/*
20261558Srgrimes * Get an exports file line. Skip over blank lines and handle line
20271558Srgrimes * continuations.
20281558Srgrimes */
20291558Srgrimesint
20301558Srgrimesget_line()
20311558Srgrimes{
20321558Srgrimes	char *p, *cp;
20331558Srgrimes	int len;
20341558Srgrimes	int totlen, cont_line;
20351558Srgrimes
20361558Srgrimes	/*
20371558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
20381558Srgrimes	 */
20391558Srgrimes	p = line;
20401558Srgrimes	totlen = 0;
20411558Srgrimes	do {
20421558Srgrimes		if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
20431558Srgrimes			return (0);
20441558Srgrimes		len = strlen(p);
20451558Srgrimes		cp = p + len - 1;
20461558Srgrimes		cont_line = 0;
20471558Srgrimes		while (cp >= p &&
20481558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
20491558Srgrimes			if (*cp == '\\')
20501558Srgrimes				cont_line = 1;
20511558Srgrimes			cp--;
20521558Srgrimes			len--;
20531558Srgrimes		}
20541558Srgrimes		*++cp = '\0';
20551558Srgrimes		if (len > 0) {
20561558Srgrimes			totlen += len;
20571558Srgrimes			if (totlen >= LINESIZ) {
205837663Scharnier				syslog(LOG_ERR, "exports line too long");
20591558Srgrimes				exit(2);
20601558Srgrimes			}
20611558Srgrimes			p = cp;
20621558Srgrimes		}
20631558Srgrimes	} while (totlen == 0 || cont_line);
20641558Srgrimes	return (1);
20651558Srgrimes}
20661558Srgrimes
20671558Srgrimes/*
20681558Srgrimes * Parse a description of a credential.
20691558Srgrimes */
20701558Srgrimesvoid
20711558Srgrimesparsecred(namelist, cr)
20721558Srgrimes	char *namelist;
207372650Sgreen	struct xucred *cr;
20741558Srgrimes{
20751558Srgrimes	char *name;
20761558Srgrimes	int cnt;
20771558Srgrimes	char *names;
20781558Srgrimes	struct passwd *pw;
20791558Srgrimes	struct group *gr;
20801558Srgrimes	int ngroups, groups[NGROUPS + 1];
20811558Srgrimes
20821558Srgrimes	/*
208337663Scharnier	 * Set up the unprivileged user.
20841558Srgrimes	 */
20851558Srgrimes	cr->cr_uid = -2;
20861558Srgrimes	cr->cr_groups[0] = -2;
20871558Srgrimes	cr->cr_ngroups = 1;
20881558Srgrimes	/*
20891558Srgrimes	 * Get the user's password table entry.
20901558Srgrimes	 */
20911558Srgrimes	names = strsep(&namelist, " \t\n");
20921558Srgrimes	name = strsep(&names, ":");
20931558Srgrimes	if (isdigit(*name) || *name == '-')
20941558Srgrimes		pw = getpwuid(atoi(name));
20951558Srgrimes	else
20961558Srgrimes		pw = getpwnam(name);
20971558Srgrimes	/*
20981558Srgrimes	 * Credentials specified as those of a user.
20991558Srgrimes	 */
21001558Srgrimes	if (names == NULL) {
21011558Srgrimes		if (pw == NULL) {
210237663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
21031558Srgrimes			return;
21041558Srgrimes		}
21051558Srgrimes		cr->cr_uid = pw->pw_uid;
21061558Srgrimes		ngroups = NGROUPS + 1;
21071558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
210837663Scharnier			syslog(LOG_ERR, "too many groups");
21091558Srgrimes		/*
21101558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
21111558Srgrimes		 */
21121558Srgrimes		cr->cr_ngroups = ngroups - 1;
21131558Srgrimes		cr->cr_groups[0] = groups[0];
21141558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
21151558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
21161558Srgrimes		return;
21171558Srgrimes	}
21181558Srgrimes	/*
21191558Srgrimes	 * Explicit credential specified as a colon separated list:
21201558Srgrimes	 *	uid:gid:gid:...
21211558Srgrimes	 */
21221558Srgrimes	if (pw != NULL)
21231558Srgrimes		cr->cr_uid = pw->pw_uid;
21241558Srgrimes	else if (isdigit(*name) || *name == '-')
21251558Srgrimes		cr->cr_uid = atoi(name);
21261558Srgrimes	else {
212737663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
21281558Srgrimes		return;
21291558Srgrimes	}
21301558Srgrimes	cr->cr_ngroups = 0;
21311558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
21321558Srgrimes		name = strsep(&names, ":");
21331558Srgrimes		if (isdigit(*name) || *name == '-') {
21341558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
21351558Srgrimes		} else {
21361558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
213737663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
21381558Srgrimes				continue;
21391558Srgrimes			}
21401558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
21411558Srgrimes		}
21421558Srgrimes	}
21431558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
214437663Scharnier		syslog(LOG_ERR, "too many groups");
21451558Srgrimes}
21461558Srgrimes
21471558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
21481558Srgrimes/*
21491558Srgrimes * Routines that maintain the remote mounttab
21501558Srgrimes */
21511558Srgrimesvoid
21521558Srgrimesget_mountlist()
21531558Srgrimes{
21541558Srgrimes	struct mountlist *mlp, **mlpp;
215523681Speter	char *host, *dirp, *cp;
21561558Srgrimes	char str[STRSIZ];
21571558Srgrimes	FILE *mlfile;
21581558Srgrimes
21591558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
216053117Sbillf		if (errno == ENOENT)
216153117Sbillf			return;
216253117Sbillf		else {
216353117Sbillf			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
216453117Sbillf			return;
216553117Sbillf		}
21661558Srgrimes	}
21671558Srgrimes	mlpp = &mlhead;
21681558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
216923681Speter		cp = str;
217023681Speter		host = strsep(&cp, " \t\n");
217123681Speter		dirp = strsep(&cp, " \t\n");
217223681Speter		if (host == NULL || dirp == NULL)
21731558Srgrimes			continue;
21741558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
217537663Scharnier		if (mlp == (struct mountlist *)NULL)
217637663Scharnier			out_of_mem();
217723681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
217823681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
217923681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
218023681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
21811558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
21821558Srgrimes		*mlpp = mlp;
21831558Srgrimes		mlpp = &mlp->ml_next;
21841558Srgrimes	}
21851558Srgrimes	fclose(mlfile);
21861558Srgrimes}
21871558Srgrimes
218874462Salfredint
218974462Salfreddel_mlist(hostp, dirp, saddr)
21901558Srgrimes	char *hostp, *dirp;
219174462Salfred	struct sockaddr *saddr;
21921558Srgrimes{
21931558Srgrimes	struct mountlist *mlp, **mlpp;
21941558Srgrimes	struct mountlist *mlp2;
219574462Salfred	u_short sport;
21961558Srgrimes	FILE *mlfile;
21971558Srgrimes	int fnd = 0;
219874462Salfred	char host[NI_MAXHOST];
21991558Srgrimes
220074462Salfred	switch (saddr->sa_family) {
220174462Salfred	case AF_INET6:
220274462Salfred		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
220374462Salfred		break;
220474462Salfred	case AF_INET:
220574462Salfred		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
220674462Salfred		break;
220774462Salfred	default:
220874462Salfred		return -1;
220974462Salfred	}
22101558Srgrimes	mlpp = &mlhead;
22111558Srgrimes	mlp = mlhead;
22121558Srgrimes	while (mlp) {
22131558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
22141558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
22151558Srgrimes			fnd = 1;
22161558Srgrimes			mlp2 = mlp;
22171558Srgrimes			*mlpp = mlp = mlp->ml_next;
22181558Srgrimes			free((caddr_t)mlp2);
22191558Srgrimes		} else {
22201558Srgrimes			mlpp = &mlp->ml_next;
22211558Srgrimes			mlp = mlp->ml_next;
22221558Srgrimes		}
22231558Srgrimes	}
22241558Srgrimes	if (fnd) {
22251558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
222637663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
22271558Srgrimes			return;
22281558Srgrimes		}
22291558Srgrimes		mlp = mlhead;
22301558Srgrimes		while (mlp) {
22311558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22321558Srgrimes			mlp = mlp->ml_next;
22331558Srgrimes		}
22341558Srgrimes		fclose(mlfile);
22351558Srgrimes	}
22361558Srgrimes}
22371558Srgrimes
22381558Srgrimesvoid
22391558Srgrimesadd_mlist(hostp, dirp)
22401558Srgrimes	char *hostp, *dirp;
22411558Srgrimes{
22421558Srgrimes	struct mountlist *mlp, **mlpp;
22431558Srgrimes	FILE *mlfile;
22441558Srgrimes
22451558Srgrimes	mlpp = &mlhead;
22461558Srgrimes	mlp = mlhead;
22471558Srgrimes	while (mlp) {
22481558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
22491558Srgrimes			return;
22501558Srgrimes		mlpp = &mlp->ml_next;
22511558Srgrimes		mlp = mlp->ml_next;
22521558Srgrimes	}
22531558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
225437663Scharnier	if (mlp == (struct mountlist *)NULL)
225537663Scharnier		out_of_mem();
22561558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
22571558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
22581558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
22591558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
22601558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
22611558Srgrimes	*mlpp = mlp;
22621558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
226337663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
22641558Srgrimes		return;
22651558Srgrimes	}
22661558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22671558Srgrimes	fclose(mlfile);
22681558Srgrimes}
22691558Srgrimes
22701558Srgrimes/*
22711558Srgrimes * Free up a group list.
22721558Srgrimes */
22731558Srgrimesvoid
22741558Srgrimesfree_grp(grp)
22751558Srgrimes	struct grouplist *grp;
22761558Srgrimes{
227774462Salfred	struct addrinfo *ai;
22781558Srgrimes
22791558Srgrimes	if (grp->gr_type == GT_HOST) {
228074462Salfred		if (grp->gr_ptr.gt_addrinfo != NULL)
228174462Salfred			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
22821558Srgrimes	} else if (grp->gr_type == GT_NET) {
22831558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
22841558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
22851558Srgrimes	}
22861558Srgrimes	free((caddr_t)grp);
22871558Srgrimes}
22881558Srgrimes
22891558Srgrimes#ifdef DEBUG
22901558Srgrimesvoid
22911558SrgrimesSYSLOG(int pri, const char *fmt, ...)
22921558Srgrimes{
22931558Srgrimes	va_list ap;
22941558Srgrimes
22951558Srgrimes	va_start(ap, fmt);
22961558Srgrimes	vfprintf(stderr, fmt, ap);
22971558Srgrimes	va_end(ap);
22981558Srgrimes}
22991558Srgrimes#endif /* DEBUG */
23001558Srgrimes
23011558Srgrimes/*
23021558Srgrimes * Check options for consistency.
23031558Srgrimes */
23041558Srgrimesint
23051558Srgrimescheck_options(dp)
23061558Srgrimes	struct dirlist *dp;
23071558Srgrimes{
23081558Srgrimes
23091558Srgrimes	if (dp == (struct dirlist *)NULL)
23101558Srgrimes	    return (1);
23111558Srgrimes	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
23121558Srgrimes	    (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
23131558Srgrimes	    (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
23141558Srgrimes	    syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
23151558Srgrimes	    return (1);
23161558Srgrimes	}
23171558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
23181558Srgrimes	    syslog(LOG_ERR, "-mask requires -net");
23191558Srgrimes	    return (1);
23201558Srgrimes	}
23211558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
232245927Salex	    syslog(LOG_ERR, "-alldirs has multiple directories");
23231558Srgrimes	    return (1);
23241558Srgrimes	}
23251558Srgrimes	return (0);
23261558Srgrimes}
23271558Srgrimes
23281558Srgrimes/*
23291558Srgrimes * Check an absolute directory path for any symbolic links. Return true
23301558Srgrimes */
23311558Srgrimesint
23321558Srgrimescheck_dirpath(dirp)
23331558Srgrimes	char *dirp;
23341558Srgrimes{
23351558Srgrimes	char *cp;
23361558Srgrimes	int ret = 1;
23371558Srgrimes	struct stat sb;
23381558Srgrimes
23391558Srgrimes	cp = dirp + 1;
23401558Srgrimes	while (*cp && ret) {
23411558Srgrimes		if (*cp == '/') {
23421558Srgrimes			*cp = '\0';
23439336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23441558Srgrimes				ret = 0;
23451558Srgrimes			*cp = '/';
23461558Srgrimes		}
23471558Srgrimes		cp++;
23481558Srgrimes	}
23499336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23501558Srgrimes		ret = 0;
23511558Srgrimes	return (ret);
23521558Srgrimes}
23539336Sdfr
23549336Sdfr/*
23559336Sdfr * Just translate an ascii string to an integer.
23569336Sdfr */
23579336Sdfrint
23589336Sdfrget_num(cp)
23599336Sdfr	register char *cp;
23609336Sdfr{
23619336Sdfr	register int res = 0;
23629336Sdfr
23639336Sdfr	while (*cp) {
23649336Sdfr		if (*cp < '0' || *cp > '9')
23659336Sdfr			return (-1);
23669336Sdfr		res = res * 10 + (*cp++ - '0');
23679336Sdfr	}
23689336Sdfr	return (res);
23699336Sdfr}
237074462Salfred
237174462Salfredstatic int
237274462Salfrednetpartcmp(struct sockaddr *s1, struct sockaddr *s2, int bitlen)
237374462Salfred{
237474462Salfred	void *src, *dst;
237574462Salfred
237674462Salfred	if (s1->sa_family != s2->sa_family)
237774462Salfred		return 1;
237874462Salfred
237974462Salfred	switch (s1->sa_family) {
238074462Salfred	case AF_INET:
238174462Salfred		src = &((struct sockaddr_in *)s1)->sin_addr;
238274462Salfred		dst = &((struct sockaddr_in *)s2)->sin_addr;
238374462Salfred		if (bitlen > sizeof(((struct sockaddr_in *)s1)->sin_addr) * 8)
238474462Salfred			return 1;
238574462Salfred		break;
238674462Salfred	case AF_INET6:
238774462Salfred		src = &((struct sockaddr_in6 *)s1)->sin6_addr;
238874462Salfred		dst = &((struct sockaddr_in6 *)s2)->sin6_addr;
238974462Salfred		if (((struct sockaddr_in6 *)s1)->sin6_scope_id !=
239074462Salfred		   ((struct sockaddr_in6 *)s2)->sin6_scope_id)
239174462Salfred			return 1;
239274462Salfred		if (bitlen > sizeof(((struct sockaddr_in6 *)s1)->sin6_addr) * 8)
239374462Salfred			return 1;
239474462Salfred		break;
239574462Salfred	default:
239674462Salfred		return 1;
239774462Salfred	}
239874462Salfred
239974462Salfred	return bitcmp(src, dst, bitlen);
240074462Salfred}
240174462Salfred
240274462Salfredstatic int
240374462Salfredallones(struct sockaddr_storage *ssp, int bitlen)
240474462Salfred{
240574462Salfred	u_int8_t *p;
240674462Salfred	int bytelen, bitsleft, i;
240774462Salfred	int zerolen;
240874462Salfred
240974462Salfred	switch (ssp->ss_family) {
241074462Salfred	case AF_INET:
241174462Salfred		p = (u_int8_t *)&((struct sockaddr_in *)ssp)->sin_addr;
241274462Salfred		zerolen = sizeof (((struct sockaddr_in *)ssp)->sin_addr);
241374462Salfred		break;
241474462Salfred	case AF_INET6:
241574462Salfred		p = (u_int8_t *)&((struct sockaddr_in6 *)ssp)->sin6_addr;
241674462Salfred		zerolen = sizeof (((struct sockaddr_in6 *)ssp)->sin6_addr);
241774462Salfred	break;
241874462Salfred	default:
241974462Salfred		return -1;
242074462Salfred	}
242174462Salfred
242274462Salfred	memset(p, 0, zerolen);
242374462Salfred
242474462Salfred	bytelen = bitlen / 8;
242574462Salfred	bitsleft = bitlen % 8;
242674462Salfred
242774462Salfred	if (bytelen > zerolen)
242874462Salfred		return -1;
242974462Salfred
243074462Salfred	for (i = 0; i < bytelen; i++)
243174462Salfred		*p++ = 0xff;
243274462Salfred
243374462Salfred	for (i = 0; i < bitsleft; i++)
243474462Salfred		*p |= 1 << (7 - i);
243574462Salfred
243674462Salfred	return 0;
243774462Salfred}
243874462Salfred
243974462Salfredstatic int
244074462Salfredcountones(struct sockaddr *sa)
244174462Salfred{
244274462Salfred	void *mask;
244374462Salfred	int i, bits = 0, bytelen;
244474462Salfred	u_int8_t *p;
244574462Salfred
244674462Salfred	switch (sa->sa_family) {
244774462Salfred	case AF_INET:
244874462Salfred		mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr;
244974462Salfred		bytelen = 4;
245074462Salfred		break;
245174462Salfred	case AF_INET6:
245274462Salfred		mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr;
245374462Salfred		bytelen = 16;
245474462Salfred		break;
245574462Salfred	default:
245674462Salfred		return 0;
245774462Salfred	}
245874462Salfred
245974462Salfred	p = mask;
246074462Salfred
246174462Salfred	for (i = 0; i < bytelen; i++, p++) {
246274462Salfred		if (*p != 0xff) {
246374462Salfred			for (bits = 0; bits < 8; bits++) {
246474462Salfred				if (!(*p & (1 << (7 - bits))))
246574462Salfred					break;
246674462Salfred			}
246774462Salfred			break;
246874462Salfred		}
246974462Salfred	}
247074462Salfred
247174462Salfred	return (i * 8 + bits);
247274462Salfred}
247374462Salfred
247474462Salfredstatic int
247574462Salfredsacmp(struct sockaddr *sa1, struct sockaddr *sa2)
247674462Salfred{
247774462Salfred	void *p1, *p2;
247874462Salfred	int len;
247974462Salfred
248074462Salfred	if (sa1->sa_family != sa2->sa_family)
248174462Salfred		return 1;
248274462Salfred
248374462Salfred	switch (sa1->sa_family) {
248474462Salfred	case AF_INET:
248574462Salfred		p1 = &((struct sockaddr_in *)sa1)->sin_addr;
248674462Salfred		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
248774462Salfred		len = 4;
248874462Salfred		break;
248974462Salfred	case AF_INET6:
249074462Salfred		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
249174462Salfred		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
249274462Salfred		len = 16;
249374462Salfred		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
249474462Salfred		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
249574462Salfred			return 1;
249674462Salfred		break;
249774462Salfred	default:
249874462Salfred		return 1;
249974462Salfred	}
250074462Salfred
250174462Salfred	return memcmp(p1, p2, len);
250274462Salfred}
250374462Salfred
250474462Salfredvoid terminate(sig)
250574462Salfredint sig;
250674462Salfred{
250774462Salfred	close(mountdlockfd);
250874462Salfred	unlink(MOUNTDLOCK);
250974792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
251074792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
251174462Salfred	exit (0);
251274462Salfred}
2513