mountd.c revision 103949
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 103949 2002-09-25 04:06:37Z mike $";
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>
5796622Siedowse#include <sys/linker.h>
5896622Siedowse#include <sys/module.h>
591558Srgrimes
601558Srgrimes#include <rpc/rpc.h>
611558Srgrimes#include <rpc/pmap_clnt.h>
6274462Salfred#include <rpc/pmap_prot.h>
6374462Salfred#include <rpcsvc/mount.h>
641558Srgrimes#include <nfs/rpcv2.h>
659336Sdfr#include <nfs/nfsproto.h>
6683653Speter#include <nfsserver/nfs.h>
6723681Speter#include <ufs/ufs/ufsmount.h>
6877162Sru#include <fs/msdosfs/msdosfsmount.h>
6977223Sru#include <fs/ntfs/ntfsmount.h>
7023681Speter#include <isofs/cd9660/cd9660_mount.h>	/* XXX need isofs in include */
711558Srgrimes
721558Srgrimes#include <arpa/inet.h>
731558Srgrimes
741558Srgrimes#include <ctype.h>
7537663Scharnier#include <err.h>
761558Srgrimes#include <errno.h>
771558Srgrimes#include <grp.h>
78103949Smike#include <limits.h>
791558Srgrimes#include <netdb.h>
801558Srgrimes#include <pwd.h>
811558Srgrimes#include <signal.h>
821558Srgrimes#include <stdio.h>
831558Srgrimes#include <stdlib.h>
841558Srgrimes#include <string.h>
851558Srgrimes#include <unistd.h>
861558Srgrimes#include "pathnames.h"
871558Srgrimes
881558Srgrimes#ifdef DEBUG
891558Srgrimes#include <stdarg.h>
901558Srgrimes#endif
911558Srgrimes
9274462Salfred#ifndef MOUNTDLOCK
9374462Salfred#define MOUNTDLOCK "/var/run/mountd.lock"
9474462Salfred#endif
9574462Salfred
961558Srgrimes/*
971558Srgrimes * Structures for keeping the mount list and export list
981558Srgrimes */
991558Srgrimesstruct mountlist {
1001558Srgrimes	struct mountlist *ml_next;
1011558Srgrimes	char	ml_host[RPCMNT_NAMELEN+1];
1021558Srgrimes	char	ml_dirp[RPCMNT_PATHLEN+1];
1031558Srgrimes};
1041558Srgrimes
1051558Srgrimesstruct dirlist {
1061558Srgrimes	struct dirlist	*dp_left;
1071558Srgrimes	struct dirlist	*dp_right;
1081558Srgrimes	int		dp_flag;
1091558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
1101558Srgrimes	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1111558Srgrimes};
1121558Srgrimes/* dp_flag bits */
1131558Srgrimes#define	DP_DEFSET	0x1
1149336Sdfr#define DP_HOSTSET	0x2
1151558Srgrimes
1161558Srgrimesstruct exportlist {
1171558Srgrimes	struct exportlist *ex_next;
1181558Srgrimes	struct dirlist	*ex_dirl;
1191558Srgrimes	struct dirlist	*ex_defdir;
1201558Srgrimes	int		ex_flag;
1211558Srgrimes	fsid_t		ex_fs;
1221558Srgrimes	char		*ex_fsdir;
12327447Sdfr	char		*ex_indexfile;
1241558Srgrimes};
1251558Srgrimes/* ex_flag bits */
1261558Srgrimes#define	EX_LINKED	0x1
1271558Srgrimes
1281558Srgrimesstruct netmsk {
12974462Salfred	struct sockaddr_storage nt_net;
13075801Siedowse	struct sockaddr_storage nt_mask;
13142144Sdfr	char		*nt_name;
1321558Srgrimes};
1331558Srgrimes
1341558Srgrimesunion grouptypes {
13574462Salfred	struct addrinfo *gt_addrinfo;
1361558Srgrimes	struct netmsk	gt_net;
1371558Srgrimes};
1381558Srgrimes
1391558Srgrimesstruct grouplist {
1401558Srgrimes	int gr_type;
1411558Srgrimes	union grouptypes gr_ptr;
1421558Srgrimes	struct grouplist *gr_next;
1431558Srgrimes};
1441558Srgrimes/* Group types */
1451558Srgrimes#define	GT_NULL		0x0
1461558Srgrimes#define	GT_HOST		0x1
1471558Srgrimes#define	GT_NET		0x2
14875641Siedowse#define	GT_DEFAULT	0x3
1497401Swpaul#define GT_IGNORE	0x5
1501558Srgrimes
1511558Srgrimesstruct hostlist {
1529336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1531558Srgrimes	struct grouplist *ht_grp;
1541558Srgrimes	struct hostlist	 *ht_next;
1551558Srgrimes};
1561558Srgrimes
1579336Sdfrstruct fhreturn {
1589336Sdfr	int	fhr_flag;
1599336Sdfr	int	fhr_vers;
1609336Sdfr	nfsfh_t	fhr_fh;
1619336Sdfr};
1629336Sdfr
1631558Srgrimes/* Global defs */
16492882Simpchar	*add_expdir(struct dirlist **, char *, int);
16592882Simpvoid	add_dlist(struct dirlist **, struct dirlist *,
16692882Simp				struct grouplist *, int);
16792882Simpvoid	add_mlist(char *, char *);
16892882Simpint	check_dirpath(char *);
16992882Simpint	check_options(struct dirlist *);
17075801Siedowseint	checkmask(struct sockaddr *sa);
17192882Simpint	chk_host(struct dirlist *, struct sockaddr *, int *, int *);
17275635Siedowsevoid	del_mlist(char *hostp, char *dirp);
17392882Simpstruct dirlist *dirp_search(struct dirlist *, char *);
17492882Simpint	do_mount(struct exportlist *, struct grouplist *, int,
17592882Simp		struct xucred *, char *, int, struct statfs *);
17692882Simpint	do_opt(char **, char **, struct exportlist *, struct grouplist *,
17792882Simp				int *, int *, struct xucred *);
17892882Simpstruct	exportlist *ex_search(fsid_t *);
17992882Simpstruct	exportlist *get_exp(void);
18092882Simpvoid	free_dir(struct dirlist *);
18192882Simpvoid	free_exp(struct exportlist *);
18292882Simpvoid	free_grp(struct grouplist *);
18392882Simpvoid	free_host(struct hostlist *);
18492882Simpvoid	get_exportlist(void);
18592882Simpint	get_host(char *, struct grouplist *, struct grouplist *);
18692882Simpstruct hostlist *get_ht(void);
18792882Simpint	get_line(void);
18892882Simpvoid	get_mountlist(void);
18992882Simpint	get_net(char *, struct netmsk *, int);
19092882Simpvoid	getexp_err(struct exportlist *, struct grouplist *);
19192882Simpstruct grouplist *get_grp(void);
19292882Simpvoid	hang_dirp(struct dirlist *, struct grouplist *,
19392882Simp				struct exportlist *, int);
19475754Siedowsevoid	huphandler(int sig);
19575801Siedowseint	makemask(struct sockaddr_storage *ssp, int bitlen);
19692882Simpvoid	mntsrv(struct svc_req *, SVCXPRT *);
19792882Simpvoid	nextfield(char **, char **);
19892882Simpvoid	out_of_mem(void);
19992882Simpvoid	parsecred(char *, struct xucred *);
200100117Salfredint	put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
20175801Siedowsevoid	*sa_rawaddr(struct sockaddr *sa, int *nbytes);
20275801Siedowseint	sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
20375801Siedowse    struct sockaddr *samask);
20492882Simpint	scan_tree(struct dirlist *, struct sockaddr *);
20592882Simpstatic void usage(void);
20692882Simpint	xdr_dir(XDR *, char *);
20792882Simpint	xdr_explist(XDR *, caddr_t);
208100117Salfredint	xdr_explist_brief(XDR *, caddr_t);
20992882Simpint	xdr_fhs(XDR *, caddr_t);
21092882Simpint	xdr_mlist(XDR *, caddr_t);
21192882Simpvoid	terminate(int);
2121558Srgrimes
2131558Srgrimesstruct exportlist *exphead;
2141558Srgrimesstruct mountlist *mlhead;
2151558Srgrimesstruct grouplist *grphead;
2161558Srgrimeschar exname[MAXPATHLEN];
21772650Sgreenstruct xucred def_anon = {
21891354Sdd	XUCRED_VERSION,
21972650Sgreen	(uid_t)-2,
2201558Srgrimes	1,
22172650Sgreen	{ (gid_t)-2 },
22272650Sgreen	NULL
2231558Srgrimes};
22425087Sdfrint force_v2 = 0;
2259336Sdfrint resvport_only = 1;
2269336Sdfrint dir_only = 1;
22731705Sguidoint log = 0;
22875754Siedowseint got_sighup = 0;
22974462Salfred
2301558Srgrimesint opt_flags;
23174462Salfredstatic int have_v6 = 1;
23274462Salfred#ifdef NI_WITHSCOPEID
23374462Salfredstatic const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID;
23474462Salfred#else
23574462Salfredstatic const int ninumeric = NI_NUMERICHOST;
23674462Salfred#endif
23774462Salfred
23874462Salfredint mountdlockfd;
23975801Siedowse/* Bits for opt_flags above */
2401558Srgrimes#define	OP_MAPROOT	0x01
2411558Srgrimes#define	OP_MAPALL	0x02
24283653Speter/* 0x4 free */
2431558Srgrimes#define	OP_MASK		0x08
2441558Srgrimes#define	OP_NET		0x10
2451558Srgrimes#define	OP_ALLDIRS	0x40
24675801Siedowse#define	OP_HAVEMASK	0x80	/* A mask was specified or inferred. */
247100336Sjoerg#define	OP_QUIET	0x100
24874462Salfred#define OP_MASKLEN	0x200
2491558Srgrimes
2501558Srgrimes#ifdef DEBUG
2511558Srgrimesint debug = 1;
25292882Simpvoid	SYSLOG(int, const char *, ...) __printflike(2, 3);
2531558Srgrimes#define syslog SYSLOG
2541558Srgrimes#else
2551558Srgrimesint debug = 0;
2561558Srgrimes#endif
2571558Srgrimes
2581558Srgrimes/*
2591558Srgrimes * Mountd server for NFS mount protocol as described in:
2601558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2611558Srgrimes * The optional arguments are the exports file name
2621558Srgrimes * default: _PATH_EXPORTS
2631558Srgrimes * and "-n" to allow nonroot mount.
2641558Srgrimes */
2651558Srgrimesint
2661558Srgrimesmain(argc, argv)
2671558Srgrimes	int argc;
2681558Srgrimes	char **argv;
2691558Srgrimes{
27075754Siedowse	fd_set readfds;
27174462Salfred	SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
27274462Salfred	struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
27374462Salfred	int udpsock, tcpsock, udp6sock, tcp6sock;
27474462Salfred	int xcreated = 0, s;
27574462Salfred	int one = 1;
27696622Siedowse	int c;
2771558Srgrimes
27875635Siedowse	udp6conf = tcp6conf = NULL;
27975635Siedowse	udp6sock = tcp6sock = NULL;
28075635Siedowse
28174462Salfred	/* Check that another mountd isn't already running. */
28274462Salfred	if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
28374462Salfred		err(1, "%s", MOUNTDLOCK);
28474462Salfred
28574462Salfred	if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
28674462Salfred		errx(1, "another rpc.mountd is already running. Aborting");
28774462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
28874462Salfred	if (s < 0)
28974462Salfred		have_v6 = 0;
29074462Salfred	else
29174462Salfred		close(s);
29283687Speter	if (modfind("nfsserver") < 0) {
29383687Speter		/* Not present in kernel, try loading it */
29483687Speter		if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
29583687Speter			errx(1, "NFS server is not available or loadable");
2962999Swollman	}
2972999Swollman
29831665Sguido	while ((c = getopt(argc, argv, "2dlnr")) != -1)
2991558Srgrimes		switch (c) {
30025087Sdfr		case '2':
30125087Sdfr			force_v2 = 1;
30225087Sdfr			break;
3039336Sdfr		case 'n':
3049336Sdfr			resvport_only = 0;
3059336Sdfr			break;
3069336Sdfr		case 'r':
3079336Sdfr			dir_only = 0;
3089336Sdfr			break;
3098688Sphk		case 'd':
3108688Sphk			debug = debug ? 0 : 1;
3118688Sphk			break;
31231656Sguido		case 'l':
31331656Sguido			log = 1;
31431656Sguido			break;
3151558Srgrimes		default:
31637663Scharnier			usage();
3171558Srgrimes		};
3181558Srgrimes	argc -= optind;
3191558Srgrimes	argv += optind;
3201558Srgrimes	grphead = (struct grouplist *)NULL;
3211558Srgrimes	exphead = (struct exportlist *)NULL;
3221558Srgrimes	mlhead = (struct mountlist *)NULL;
3231558Srgrimes	if (argc == 1) {
3241558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
3251558Srgrimes		exname[MAXPATHLEN-1] = '\0';
3261558Srgrimes	} else
3271558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3281558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3291558Srgrimes	if (debug)
33037663Scharnier		warnx("getting export list");
3311558Srgrimes	get_exportlist();
3321558Srgrimes	if (debug)
33337663Scharnier		warnx("getting mount list");
3341558Srgrimes	get_mountlist();
3351558Srgrimes	if (debug)
33637663Scharnier		warnx("here we go");
3371558Srgrimes	if (debug == 0) {
3381558Srgrimes		daemon(0, 0);
3391558Srgrimes		signal(SIGINT, SIG_IGN);
3401558Srgrimes		signal(SIGQUIT, SIG_IGN);
3411558Srgrimes	}
34275754Siedowse	signal(SIGHUP, huphandler);
34374462Salfred	signal(SIGTERM, terminate);
3441558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3451558Srgrimes	  if (pidfile != NULL) {
3461558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3471558Srgrimes		fclose(pidfile);
3481558Srgrimes	  }
3491558Srgrimes	}
35074462Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
35174462Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
35274462Salfred	udpsock  = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
35374462Salfred	tcpsock  = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
35474791Salfred	udpconf  = getnetconfigent("udp");
35574791Salfred	tcpconf  = getnetconfigent("tcp");
35674791Salfred	if (!have_v6)
35774791Salfred		goto skip_v6;
35874462Salfred	udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
35974462Salfred	tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
36074462Salfred	/*
36174462Salfred	 * We're doing host-based access checks here, so don't allow
36274462Salfred	 * v4-in-v6 to confuse things. The kernel will disable it
36374462Salfred	 * by default on NFS sockets too.
36474462Salfred	 */
36574462Salfred	if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
366100505Sume		IPV6_V6ONLY, &one, sizeof one) < 0){
36774462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
36874462Salfred		exit(1);
36974462Salfred	}
37074462Salfred	if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
371100505Sume		IPV6_V6ONLY, &one, sizeof one) < 0){
37274462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
37374462Salfred		exit(1);
37474462Salfred	}
37574462Salfred	udp6conf = getnetconfigent("udp6");
37674462Salfred	tcp6conf = getnetconfigent("tcp6");
37774791Salfred
37874791Salfredskip_v6:
37924759Sguido	if (!resvport_only) {
38083687Speter		if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
38183687Speter		    &resvport_only, sizeof(resvport_only)) != 0 &&
38283687Speter		    errno != ENOENT) {
38324759Sguido			syslog(LOG_ERR, "sysctl: %m");
38424759Sguido			exit(1);
38524759Sguido		}
38624330Sguido	}
38774462Salfred	if (udpsock != -1 && udpconf != NULL) {
38874462Salfred		bindresvport(udpsock, NULL);
38974462Salfred		udptransp = svc_dg_create(udpsock, 0, 0);
39074462Salfred		if (udptransp != NULL) {
39174462Salfred			if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
39274462Salfred			    mntsrv, udpconf))
39374462Salfred				syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service");
39474462Salfred			else
39574462Salfred				xcreated++;
39674462Salfred			if (!force_v2) {
39774462Salfred				if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
39874462Salfred				    mntsrv, udpconf))
39974462Salfred					syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service");
40074462Salfred				else
40174462Salfred					xcreated++;
40274462Salfred			}
40374462Salfred		} else
40474462Salfred			syslog(LOG_WARNING, "can't create UDP services");
40574462Salfred
40674462Salfred	}
40774462Salfred	if (tcpsock != -1 && tcpconf != NULL) {
40874462Salfred		bindresvport(tcpsock, NULL);
40974462Salfred		listen(tcpsock, SOMAXCONN);
41074462Salfred		tcptransp = svc_vc_create(tcpsock, 0, 0);
41174462Salfred		if (tcptransp != NULL) {
41274462Salfred			if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
41374462Salfred			    mntsrv, tcpconf))
41474462Salfred				syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service");
41574462Salfred			else
41674462Salfred				xcreated++;
41774462Salfred			if (!force_v2) {
41874462Salfred				if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
41974462Salfred				    mntsrv, tcpconf))
42074462Salfred					syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service");
42174462Salfred				else
42274462Salfred					xcreated++;
42374462Salfred			}
42474462Salfred		} else
42574462Salfred			syslog(LOG_WARNING, "can't create TCP service");
42674462Salfred
42774462Salfred	}
42874791Salfred	if (have_v6 && udp6sock != -1 && udp6conf != NULL) {
42974462Salfred		bindresvport(udp6sock, NULL);
43074462Salfred		udp6transp = svc_dg_create(udp6sock, 0, 0);
43174462Salfred		if (udp6transp != NULL) {
43274462Salfred			if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
43374462Salfred			    mntsrv, udp6conf))
43474462Salfred				syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service");
43574462Salfred			else
43674462Salfred				xcreated++;
43774462Salfred			if (!force_v2) {
43874462Salfred				if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
43974462Salfred				    mntsrv, udp6conf))
44074462Salfred					syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service");
44174462Salfred				else
44274462Salfred					xcreated++;
44374462Salfred			}
44474462Salfred		} else
44574462Salfred			syslog(LOG_WARNING, "can't create UDP6 service");
44674462Salfred
44774462Salfred	}
44874791Salfred	if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) {
44974462Salfred		bindresvport(tcp6sock, NULL);
45074462Salfred		listen(tcp6sock, SOMAXCONN);
45174462Salfred		tcp6transp = svc_vc_create(tcp6sock, 0, 0);
45274462Salfred		if (tcp6transp != NULL) {
45374462Salfred			if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
45474462Salfred			    mntsrv, tcp6conf))
45574462Salfred				syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service");
45674462Salfred			else
45774462Salfred				xcreated++;
45874462Salfred			if (!force_v2) {
45974462Salfred				if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
46074462Salfred				    mntsrv, tcp6conf))
46174462Salfred					syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service");
46274462Salfred					else
46374462Salfred						xcreated++;
46474462Salfred				}
46574462Salfred		} else
46674462Salfred			syslog(LOG_WARNING, "can't create TCP6 service");
46774462Salfred
46874462Salfred	}
46974462Salfred	if (xcreated == 0) {
47074462Salfred		syslog(LOG_ERR, "could not create any services");
4711558Srgrimes		exit(1);
4721558Srgrimes	}
47375754Siedowse
47475754Siedowse	/* Expand svc_run() here so that we can call get_exportlist(). */
47575754Siedowse	for (;;) {
47675754Siedowse		if (got_sighup) {
47775754Siedowse			get_exportlist();
47875754Siedowse			got_sighup = 0;
47975754Siedowse		}
48075754Siedowse		readfds = svc_fdset;
48175754Siedowse		switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
48275754Siedowse		case -1:
48375754Siedowse			if (errno == EINTR)
48475754Siedowse                                continue;
48575754Siedowse			syslog(LOG_ERR, "mountd died: select: %m");
48675754Siedowse			exit(1);
48775754Siedowse		case 0:
48875754Siedowse			continue;
48975754Siedowse		default:
49075754Siedowse			svc_getreqset(&readfds);
49175754Siedowse		}
49275754Siedowse	}
4931558Srgrimes}
4941558Srgrimes
49537663Scharnierstatic void
49637663Scharnierusage()
49737663Scharnier{
49837663Scharnier	fprintf(stderr,
49937663Scharnier		"usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
50037663Scharnier	exit(1);
50137663Scharnier}
50237663Scharnier
5031558Srgrimes/*
5041558Srgrimes * The mount rpc service
5051558Srgrimes */
5061558Srgrimesvoid
5071558Srgrimesmntsrv(rqstp, transp)
5081558Srgrimes	struct svc_req *rqstp;
5091558Srgrimes	SVCXPRT *transp;
5101558Srgrimes{
5111558Srgrimes	struct exportlist *ep;
5121558Srgrimes	struct dirlist *dp;
5139336Sdfr	struct fhreturn fhr;
5141558Srgrimes	struct stat stb;
5151558Srgrimes	struct statfs fsb;
51674462Salfred	struct addrinfo *ai;
51774462Salfred	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
51874462Salfred	int lookup_failed = 1;
51974462Salfred	struct sockaddr *saddr;
5209336Sdfr	u_short sport;
52123681Speter	char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
52228911Sguido	int bad = 0, defset, hostset;
5239336Sdfr	sigset_t sighup_mask;
5241558Srgrimes
5259336Sdfr	sigemptyset(&sighup_mask);
5269336Sdfr	sigaddset(&sighup_mask, SIGHUP);
52774462Salfred	saddr = svc_getrpccaller(transp)->buf;
52874462Salfred	switch (saddr->sa_family) {
52974462Salfred	case AF_INET6:
53075635Siedowse		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
53174462Salfred		break;
53274462Salfred	case AF_INET:
53375635Siedowse		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
53474462Salfred		break;
53574462Salfred	default:
53674462Salfred		syslog(LOG_ERR, "request from unknown address family");
53774462Salfred		return;
53874462Salfred	}
53974462Salfred	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
54074462Salfred	    NULL, 0, 0);
54174462Salfred	getnameinfo(saddr, saddr->sa_len, numerichost,
54274462Salfred	    sizeof numerichost, NULL, 0, NI_NUMERICHOST);
54374462Salfred	ai = NULL;
5441558Srgrimes	switch (rqstp->rq_proc) {
5451558Srgrimes	case NULLPROC:
546100117Salfred		if (!svc_sendreply(transp, xdr_void, NULL))
54737663Scharnier			syslog(LOG_ERR, "can't send reply");
5481558Srgrimes		return;
5491558Srgrimes	case RPCMNT_MOUNT:
5509336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
55131656Sguido			syslog(LOG_NOTICE,
55231656Sguido			    "mount request from %s from unprivileged port",
55374462Salfred			    numerichost);
5541558Srgrimes			svcerr_weakauth(transp);
5551558Srgrimes			return;
5561558Srgrimes		}
5571558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
55831656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
55974462Salfred			    numerichost);
5601558Srgrimes			svcerr_decode(transp);
5611558Srgrimes			return;
5621558Srgrimes		}
5631558Srgrimes
5641558Srgrimes		/*
5651558Srgrimes		 * Get the real pathname and make sure it is a directory
5669336Sdfr		 * or a regular file if the -r option was specified
5679336Sdfr		 * and it exists.
5681558Srgrimes		 */
56951968Salfred		if (realpath(rpcpath, dirpath) == NULL ||
5701558Srgrimes		    stat(dirpath, &stb) < 0 ||
5719336Sdfr		    (!S_ISDIR(stb.st_mode) &&
57274462Salfred		    (dir_only || !S_ISREG(stb.st_mode))) ||
5731558Srgrimes		    statfs(dirpath, &fsb) < 0) {
5741558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
57531656Sguido			syslog(LOG_NOTICE,
57637663Scharnier			    "mount request from %s for non existent path %s",
57774462Salfred			    numerichost, dirpath);
5781558Srgrimes			if (debug)
57937663Scharnier				warnx("stat failed on %s", dirpath);
58028911Sguido			bad = ENOENT;	/* We will send error reply later */
5811558Srgrimes		}
5821558Srgrimes
5831558Srgrimes		/* Check in the exports list */
5849336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
5851558Srgrimes		ep = ex_search(&fsb.f_fsid);
5869336Sdfr		hostset = defset = 0;
5879336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
5881558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
58974462Salfred		      chk_host(dp, saddr, &defset, &hostset)) ||
59074462Salfred		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
59174462Salfred		     scan_tree(ep->ex_dirl, saddr) == 0))) {
59228911Sguido			if (bad) {
59328911Sguido				if (!svc_sendreply(transp, xdr_long,
59428911Sguido				    (caddr_t)&bad))
59537663Scharnier					syslog(LOG_ERR, "can't send reply");
59628911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
59728911Sguido				return;
59828911Sguido			}
5999336Sdfr			if (hostset & DP_HOSTSET)
6009336Sdfr				fhr.fhr_flag = hostset;
6019336Sdfr			else
6029336Sdfr				fhr.fhr_flag = defset;
6039336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
6041558Srgrimes			/* Get the file handle */
60523681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
6069336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
6071558Srgrimes				bad = errno;
60837663Scharnier				syslog(LOG_ERR, "can't get fh for %s", dirpath);
6091558Srgrimes				if (!svc_sendreply(transp, xdr_long,
6101558Srgrimes				    (caddr_t)&bad))
61137663Scharnier					syslog(LOG_ERR, "can't send reply");
6129336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6131558Srgrimes				return;
6141558Srgrimes			}
6159336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
61637663Scharnier				syslog(LOG_ERR, "can't send reply");
61774462Salfred			if (!lookup_failed)
61874462Salfred				add_mlist(host, dirpath);
6191558Srgrimes			else
62074462Salfred				add_mlist(numerichost, dirpath);
6211558Srgrimes			if (debug)
62237663Scharnier				warnx("mount successful");
62331656Sguido			if (log)
62431656Sguido				syslog(LOG_NOTICE,
62531656Sguido				    "mount request succeeded from %s for %s",
62674462Salfred				    numerichost, dirpath);
62731656Sguido		} else {
6281558Srgrimes			bad = EACCES;
62931656Sguido			syslog(LOG_NOTICE,
63031656Sguido			    "mount request denied from %s for %s",
63174462Salfred			    numerichost, dirpath);
63231656Sguido		}
63328911Sguido
63428911Sguido		if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
63537663Scharnier			syslog(LOG_ERR, "can't send reply");
6369336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6371558Srgrimes		return;
6381558Srgrimes	case RPCMNT_DUMP:
6391558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
64037663Scharnier			syslog(LOG_ERR, "can't send reply");
64131656Sguido		else if (log)
64231656Sguido			syslog(LOG_NOTICE,
64331656Sguido			    "dump request succeeded from %s",
64474462Salfred			    numerichost);
6451558Srgrimes		return;
6461558Srgrimes	case RPCMNT_UMOUNT:
6479336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
64831656Sguido			syslog(LOG_NOTICE,
64931656Sguido			    "umount request from %s from unprivileged port",
65074462Salfred			    numerichost);
6511558Srgrimes			svcerr_weakauth(transp);
6521558Srgrimes			return;
6531558Srgrimes		}
65451968Salfred		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
65531656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
65674462Salfred			    numerichost);
6571558Srgrimes			svcerr_decode(transp);
6581558Srgrimes			return;
6591558Srgrimes		}
66051968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
66151968Salfred			syslog(LOG_NOTICE, "umount request from %s "
66251968Salfred			    "for non existent path %s",
66374462Salfred			    numerichost, dirpath);
66451968Salfred		}
6651558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
66637663Scharnier			syslog(LOG_ERR, "can't send reply");
66774462Salfred		if (!lookup_failed)
66875635Siedowse			del_mlist(host, dirpath);
66975635Siedowse		del_mlist(numerichost, dirpath);
67031656Sguido		if (log)
67131656Sguido			syslog(LOG_NOTICE,
67231656Sguido			    "umount request succeeded from %s for %s",
67374462Salfred			    numerichost, dirpath);
6741558Srgrimes		return;
6751558Srgrimes	case RPCMNT_UMNTALL:
6769336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
67731656Sguido			syslog(LOG_NOTICE,
67831656Sguido			    "umountall request from %s from unprivileged port",
67974462Salfred			    numerichost);
6801558Srgrimes			svcerr_weakauth(transp);
6811558Srgrimes			return;
6821558Srgrimes		}
6831558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
68437663Scharnier			syslog(LOG_ERR, "can't send reply");
68574462Salfred		if (!lookup_failed)
68675635Siedowse			del_mlist(host, NULL);
68775635Siedowse		del_mlist(numerichost, NULL);
68831656Sguido		if (log)
68931656Sguido			syslog(LOG_NOTICE,
69031656Sguido			    "umountall request succeeded from %s",
69174462Salfred			    numerichost);
6921558Srgrimes		return;
6931558Srgrimes	case RPCMNT_EXPORT:
6941558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
695100117Salfred			if (!svc_sendreply(transp, xdr_explist_brief, (caddr_t)NULL))
696100117Salfred				syslog(LOG_ERR, "can't send reply");
69731656Sguido		if (log)
69831656Sguido			syslog(LOG_NOTICE,
69931656Sguido			    "export request succeeded from %s",
70074462Salfred			    numerichost);
7011558Srgrimes		return;
7021558Srgrimes	default:
7031558Srgrimes		svcerr_noproc(transp);
7041558Srgrimes		return;
7051558Srgrimes	}
7061558Srgrimes}
7071558Srgrimes
7081558Srgrimes/*
7091558Srgrimes * Xdr conversion for a dirpath string
7101558Srgrimes */
7111558Srgrimesint
7121558Srgrimesxdr_dir(xdrsp, dirp)
7131558Srgrimes	XDR *xdrsp;
7141558Srgrimes	char *dirp;
7151558Srgrimes{
7161558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
7171558Srgrimes}
7181558Srgrimes
7191558Srgrimes/*
7209336Sdfr * Xdr routine to generate file handle reply
7211558Srgrimes */
7221558Srgrimesint
7239336Sdfrxdr_fhs(xdrsp, cp)
7241558Srgrimes	XDR *xdrsp;
7259336Sdfr	caddr_t cp;
7261558Srgrimes{
72792806Sobrien	struct fhreturn *fhrp = (struct fhreturn *)cp;
7289336Sdfr	u_long ok = 0, len, auth;
7291558Srgrimes
7301558Srgrimes	if (!xdr_long(xdrsp, &ok))
7311558Srgrimes		return (0);
7329336Sdfr	switch (fhrp->fhr_vers) {
7339336Sdfr	case 1:
7349336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
7359336Sdfr	case 3:
7369336Sdfr		len = NFSX_V3FH;
7379336Sdfr		if (!xdr_long(xdrsp, &len))
7389336Sdfr			return (0);
7399336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
7409336Sdfr			return (0);
74183653Speter		auth = RPCAUTH_UNIX;
7429336Sdfr		len = 1;
7439336Sdfr		if (!xdr_long(xdrsp, &len))
7449336Sdfr			return (0);
7459336Sdfr		return (xdr_long(xdrsp, &auth));
7469336Sdfr	};
7479336Sdfr	return (0);
7481558Srgrimes}
7491558Srgrimes
7501558Srgrimesint
7511558Srgrimesxdr_mlist(xdrsp, cp)
7521558Srgrimes	XDR *xdrsp;
7531558Srgrimes	caddr_t cp;
7541558Srgrimes{
7551558Srgrimes	struct mountlist *mlp;
7561558Srgrimes	int true = 1;
7571558Srgrimes	int false = 0;
7581558Srgrimes	char *strp;
7591558Srgrimes
7601558Srgrimes	mlp = mlhead;
7611558Srgrimes	while (mlp) {
7621558Srgrimes		if (!xdr_bool(xdrsp, &true))
7631558Srgrimes			return (0);
7641558Srgrimes		strp = &mlp->ml_host[0];
7651558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
7661558Srgrimes			return (0);
7671558Srgrimes		strp = &mlp->ml_dirp[0];
7681558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
7691558Srgrimes			return (0);
7701558Srgrimes		mlp = mlp->ml_next;
7711558Srgrimes	}
7721558Srgrimes	if (!xdr_bool(xdrsp, &false))
7731558Srgrimes		return (0);
7741558Srgrimes	return (1);
7751558Srgrimes}
7761558Srgrimes
7771558Srgrimes/*
7781558Srgrimes * Xdr conversion for export list
7791558Srgrimes */
7801558Srgrimesint
781100117Salfredxdr_explist_common(xdrsp, cp, brief)
7821558Srgrimes	XDR *xdrsp;
7831558Srgrimes	caddr_t cp;
784100117Salfred	int brief;
7851558Srgrimes{
7861558Srgrimes	struct exportlist *ep;
7871558Srgrimes	int false = 0;
7889336Sdfr	int putdef;
7899336Sdfr	sigset_t sighup_mask;
7901558Srgrimes
7919336Sdfr	sigemptyset(&sighup_mask);
7929336Sdfr	sigaddset(&sighup_mask, SIGHUP);
7939336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
7941558Srgrimes	ep = exphead;
7951558Srgrimes	while (ep) {
7961558Srgrimes		putdef = 0;
797100117Salfred		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
798100117Salfred			       &putdef, brief))
7991558Srgrimes			goto errout;
8001558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
8011558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
802100117Salfred			&putdef, brief))
8031558Srgrimes			goto errout;
8041558Srgrimes		ep = ep->ex_next;
8051558Srgrimes	}
8069336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8071558Srgrimes	if (!xdr_bool(xdrsp, &false))
8081558Srgrimes		return (0);
8091558Srgrimes	return (1);
8101558Srgrimeserrout:
8119336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8121558Srgrimes	return (0);
8131558Srgrimes}
8141558Srgrimes
8151558Srgrimes/*
8161558Srgrimes * Called from xdr_explist() to traverse the tree and export the
8171558Srgrimes * directory paths.
8181558Srgrimes */
8191558Srgrimesint
820100117Salfredput_exlist(dp, xdrsp, adp, putdefp, brief)
8211558Srgrimes	struct dirlist *dp;
8221558Srgrimes	XDR *xdrsp;
8231558Srgrimes	struct dirlist *adp;
8241558Srgrimes	int *putdefp;
825100117Salfred	int brief;
8261558Srgrimes{
8271558Srgrimes	struct grouplist *grp;
8281558Srgrimes	struct hostlist *hp;
8291558Srgrimes	int true = 1;
8301558Srgrimes	int false = 0;
8311558Srgrimes	int gotalldir = 0;
8321558Srgrimes	char *strp;
8331558Srgrimes
8341558Srgrimes	if (dp) {
835100117Salfred		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
8361558Srgrimes			return (1);
8371558Srgrimes		if (!xdr_bool(xdrsp, &true))
8381558Srgrimes			return (1);
8391558Srgrimes		strp = dp->dp_dirp;
8401558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
8411558Srgrimes			return (1);
8421558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
8431558Srgrimes			gotalldir = 1;
8441558Srgrimes			*putdefp = 1;
8451558Srgrimes		}
846100117Salfred		if (brief) {
847100117Salfred			if (!xdr_bool(xdrsp, &true))
848100117Salfred				return (1);
849100117Salfred			strp = "(...)";
850100117Salfred			if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
851100117Salfred				return (1);
852100117Salfred		} else if ((dp->dp_flag & DP_DEFSET) == 0 &&
8531558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
8541558Srgrimes			hp = dp->dp_hosts;
8551558Srgrimes			while (hp) {
8561558Srgrimes				grp = hp->ht_grp;
8571558Srgrimes				if (grp->gr_type == GT_HOST) {
8581558Srgrimes					if (!xdr_bool(xdrsp, &true))
8591558Srgrimes						return (1);
86074462Salfred					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
8618871Srgrimes					if (!xdr_string(xdrsp, &strp,
8621558Srgrimes					    RPCMNT_NAMELEN))
8631558Srgrimes						return (1);
8641558Srgrimes				} else if (grp->gr_type == GT_NET) {
8651558Srgrimes					if (!xdr_bool(xdrsp, &true))
8661558Srgrimes						return (1);
8671558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
8688871Srgrimes					if (!xdr_string(xdrsp, &strp,
8691558Srgrimes					    RPCMNT_NAMELEN))
8701558Srgrimes						return (1);
8711558Srgrimes				}
8721558Srgrimes				hp = hp->ht_next;
8731558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
8741558Srgrimes					hp = adp->dp_hosts;
8751558Srgrimes					gotalldir = 0;
8761558Srgrimes				}
8771558Srgrimes			}
8781558Srgrimes		}
8791558Srgrimes		if (!xdr_bool(xdrsp, &false))
8801558Srgrimes			return (1);
881100117Salfred		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
8821558Srgrimes			return (1);
8831558Srgrimes	}
8841558Srgrimes	return (0);
8851558Srgrimes}
8861558Srgrimes
887100117Salfredint
888100117Salfredxdr_explist(xdrsp, cp)
889100117Salfred	XDR *xdrsp;
890100117Salfred	caddr_t cp;
891100117Salfred{
892100117Salfred
893100117Salfred	return xdr_explist_common(xdrsp, cp, 0);
894100117Salfred}
895100117Salfred
896100117Salfredint
897100117Salfredxdr_explist_brief(xdrsp, cp)
898100117Salfred	XDR *xdrsp;
899100117Salfred	caddr_t cp;
900100117Salfred{
901100117Salfred
902100117Salfred	return xdr_explist_common(xdrsp, cp, 1);
903100117Salfred}
904100117Salfred
90596622Siedowsechar *line;
90696622Siedowseint linesize;
9071558SrgrimesFILE *exp_file;
9081558Srgrimes
9091558Srgrimes/*
9101558Srgrimes * Get the export list
9111558Srgrimes */
9121558Srgrimesvoid
9131558Srgrimesget_exportlist()
9141558Srgrimes{
9151558Srgrimes	struct exportlist *ep, *ep2;
9161558Srgrimes	struct grouplist *grp, *tgrp;
9171558Srgrimes	struct exportlist **epp;
9181558Srgrimes	struct dirlist *dirhead;
9191558Srgrimes	struct statfs fsb, *fsp;
92072650Sgreen	struct xucred anon;
9211558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
9221558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
9231558Srgrimes
92451968Salfred	dirp = NULL;
92551968Salfred	dirplen = 0;
92651968Salfred
9271558Srgrimes	/*
9281558Srgrimes	 * First, get rid of the old list
9291558Srgrimes	 */
9301558Srgrimes	ep = exphead;
9311558Srgrimes	while (ep) {
9321558Srgrimes		ep2 = ep;
9331558Srgrimes		ep = ep->ex_next;
9341558Srgrimes		free_exp(ep2);
9351558Srgrimes	}
9361558Srgrimes	exphead = (struct exportlist *)NULL;
9371558Srgrimes
9381558Srgrimes	grp = grphead;
9391558Srgrimes	while (grp) {
9401558Srgrimes		tgrp = grp;
9411558Srgrimes		grp = grp->gr_next;
9421558Srgrimes		free_grp(tgrp);
9431558Srgrimes	}
9441558Srgrimes	grphead = (struct grouplist *)NULL;
9451558Srgrimes
9461558Srgrimes	/*
9471558Srgrimes	 * And delete exports that are in the kernel for all local
94896707Strhodes	 * filesystems.
94996707Strhodes	 * XXX: Should know how to handle all local exportable filesystems
95023681Speter	 *      instead of just "ufs".
9511558Srgrimes	 */
9521558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
9531558Srgrimes	for (i = 0; i < num; i++) {
9541558Srgrimes		union {
9551558Srgrimes			struct ufs_args ua;
9561558Srgrimes			struct iso_args ia;
9579336Sdfr			struct msdosfs_args da;
95854093Ssemenu			struct ntfs_args na;
9591558Srgrimes		} targs;
9601558Srgrimes
96177435Sphk		if (!strcmp(fsp->f_fstypename, "ufs") ||
96277577Sru		    !strcmp(fsp->f_fstypename, "msdosfs") ||
96354093Ssemenu		    !strcmp(fsp->f_fstypename, "ntfs") ||
96423681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
9659336Sdfr			targs.ua.fspec = NULL;
9669336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
9679336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
96877405Siedowse			    fsp->f_flags | MNT_UPDATE, (caddr_t)&targs) < 0 &&
96977405Siedowse			    errno != ENOENT)
97077405Siedowse				syslog(LOG_ERR,
97177405Siedowse				    "can't delete exports for %s: %m",
97274462Salfred				    fsp->f_mntonname);
9731558Srgrimes		}
9741558Srgrimes		fsp++;
9751558Srgrimes	}
9761558Srgrimes
9771558Srgrimes	/*
9781558Srgrimes	 * Read in the exports file and build the list, calling
9791558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
9801558Srgrimes	 */
9811558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
98237663Scharnier		syslog(LOG_ERR, "can't open %s", exname);
9831558Srgrimes		exit(2);
9841558Srgrimes	}
9851558Srgrimes	dirhead = (struct dirlist *)NULL;
9861558Srgrimes	while (get_line()) {
9871558Srgrimes		if (debug)
98837663Scharnier			warnx("got line %s", line);
9891558Srgrimes		cp = line;
9901558Srgrimes		nextfield(&cp, &endcp);
9911558Srgrimes		if (*cp == '#')
9921558Srgrimes			goto nextline;
9931558Srgrimes
9941558Srgrimes		/*
9951558Srgrimes		 * Set defaults.
9961558Srgrimes		 */
9971558Srgrimes		has_host = FALSE;
9981558Srgrimes		anon = def_anon;
9991558Srgrimes		exflags = MNT_EXPORTED;
10001558Srgrimes		got_nondir = 0;
10011558Srgrimes		opt_flags = 0;
10021558Srgrimes		ep = (struct exportlist *)NULL;
10031558Srgrimes
10041558Srgrimes		/*
10051558Srgrimes		 * Create new exports list entry
10061558Srgrimes		 */
10071558Srgrimes		len = endcp-cp;
10081558Srgrimes		tgrp = grp = get_grp();
10091558Srgrimes		while (len > 0) {
10101558Srgrimes			if (len > RPCMNT_NAMELEN) {
10111558Srgrimes			    getexp_err(ep, tgrp);
10121558Srgrimes			    goto nextline;
10131558Srgrimes			}
10141558Srgrimes			if (*cp == '-') {
10151558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10161558Srgrimes				getexp_err(ep, tgrp);
10171558Srgrimes				goto nextline;
10181558Srgrimes			    }
10191558Srgrimes			    if (debug)
102037663Scharnier				warnx("doing opt %s", cp);
10211558Srgrimes			    got_nondir = 1;
10221558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
10231558Srgrimes				&exflags, &anon)) {
10241558Srgrimes				getexp_err(ep, tgrp);
10251558Srgrimes				goto nextline;
10261558Srgrimes			    }
10271558Srgrimes			} else if (*cp == '/') {
10281558Srgrimes			    savedc = *endcp;
10291558Srgrimes			    *endcp = '\0';
10301558Srgrimes			    if (check_dirpath(cp) &&
10311558Srgrimes				statfs(cp, &fsb) >= 0) {
10321558Srgrimes				if (got_nondir) {
103337663Scharnier				    syslog(LOG_ERR, "dirs must be first");
10341558Srgrimes				    getexp_err(ep, tgrp);
10351558Srgrimes				    goto nextline;
10361558Srgrimes				}
10371558Srgrimes				if (ep) {
10381558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
10391558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
10401558Srgrimes					getexp_err(ep, tgrp);
10411558Srgrimes					goto nextline;
10421558Srgrimes				    }
10431558Srgrimes				} else {
10441558Srgrimes				    /*
10451558Srgrimes				     * See if this directory is already
10461558Srgrimes				     * in the list.
10471558Srgrimes				     */
10481558Srgrimes				    ep = ex_search(&fsb.f_fsid);
10491558Srgrimes				    if (ep == (struct exportlist *)NULL) {
10501558Srgrimes					ep = get_exp();
10511558Srgrimes					ep->ex_fs = fsb.f_fsid;
10521558Srgrimes					ep->ex_fsdir = (char *)
10531558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
10541558Srgrimes					if (ep->ex_fsdir)
10551558Srgrimes					    strcpy(ep->ex_fsdir,
10561558Srgrimes						fsb.f_mntonname);
10571558Srgrimes					else
10581558Srgrimes					    out_of_mem();
10591558Srgrimes					if (debug)
106074462Salfred						warnx("making new ep fs=0x%x,0x%x",
106174462Salfred						    fsb.f_fsid.val[0],
106274462Salfred						    fsb.f_fsid.val[1]);
10631558Srgrimes				    } else if (debug)
106437663Scharnier					warnx("found ep fs=0x%x,0x%x",
10651558Srgrimes					    fsb.f_fsid.val[0],
10661558Srgrimes					    fsb.f_fsid.val[1]);
10671558Srgrimes				}
10681558Srgrimes
10691558Srgrimes				/*
10701558Srgrimes				 * Add dirpath to export mount point.
10711558Srgrimes				 */
10721558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
10731558Srgrimes				dirplen = len;
10741558Srgrimes			    } else {
10751558Srgrimes				getexp_err(ep, tgrp);
10761558Srgrimes				goto nextline;
10771558Srgrimes			    }
10781558Srgrimes			    *endcp = savedc;
10791558Srgrimes			} else {
10801558Srgrimes			    savedc = *endcp;
10811558Srgrimes			    *endcp = '\0';
10821558Srgrimes			    got_nondir = 1;
10831558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10841558Srgrimes				getexp_err(ep, tgrp);
10851558Srgrimes				goto nextline;
10861558Srgrimes			    }
10871558Srgrimes
10881558Srgrimes			    /*
10891558Srgrimes			     * Get the host or netgroup.
10901558Srgrimes			     */
10911558Srgrimes			    setnetgrent(cp);
10921558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
10931558Srgrimes			    do {
10941558Srgrimes				if (has_host) {
10951558Srgrimes				    grp->gr_next = get_grp();
10961558Srgrimes				    grp = grp->gr_next;
10971558Srgrimes				}
10981558Srgrimes				if (netgrp) {
109937003Sjoerg				    if (hst == 0) {
110037663Scharnier					syslog(LOG_ERR,
110137663Scharnier				"null hostname in netgroup %s, skipping", cp);
110237004Sjoerg					grp->gr_type = GT_IGNORE;
110337003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
110437663Scharnier					syslog(LOG_ERR,
110537663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
110629317Sjlemon					grp->gr_type = GT_IGNORE;
11071558Srgrimes				    }
11087401Swpaul				} else if (get_host(cp, grp, tgrp)) {
110937663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
111029317Sjlemon				    grp->gr_type = GT_IGNORE;
11111558Srgrimes				}
11121558Srgrimes				has_host = TRUE;
11131558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
11141558Srgrimes			    endnetgrent();
11151558Srgrimes			    *endcp = savedc;
11161558Srgrimes			}
11171558Srgrimes			cp = endcp;
11181558Srgrimes			nextfield(&cp, &endcp);
11191558Srgrimes			len = endcp - cp;
11201558Srgrimes		}
11211558Srgrimes		if (check_options(dirhead)) {
11221558Srgrimes			getexp_err(ep, tgrp);
11231558Srgrimes			goto nextline;
11241558Srgrimes		}
11251558Srgrimes		if (!has_host) {
112675641Siedowse			grp->gr_type = GT_DEFAULT;
11271558Srgrimes			if (debug)
112837663Scharnier				warnx("adding a default entry");
11291558Srgrimes
11301558Srgrimes		/*
11311558Srgrimes		 * Don't allow a network export coincide with a list of
11321558Srgrimes		 * host(s) on the same line.
11331558Srgrimes		 */
11341558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
113575801Siedowse			syslog(LOG_ERR, "network/host conflict");
11361558Srgrimes			getexp_err(ep, tgrp);
11371558Srgrimes			goto nextline;
113829317Sjlemon
113974462Salfred		/*
114074462Salfred		 * If an export list was specified on this line, make sure
114129317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
114229317Sjlemon		 */
114329317Sjlemon		} else {
114429317Sjlemon			grp = tgrp;
114574462Salfred			while (grp && grp->gr_type == GT_IGNORE)
114629317Sjlemon				grp = grp->gr_next;
114729317Sjlemon			if (! grp) {
114829317Sjlemon			    getexp_err(ep, tgrp);
114929317Sjlemon			    goto nextline;
115029317Sjlemon			}
11511558Srgrimes		}
11521558Srgrimes
11531558Srgrimes		/*
11541558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
11551558Srgrimes		 * After loop, tgrp points to the start of the list and
11561558Srgrimes		 * grp points to the last entry in the list.
11571558Srgrimes		 */
11581558Srgrimes		grp = tgrp;
11591558Srgrimes		do {
116075635Siedowse			if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
116175635Siedowse			    &fsb)) {
116275635Siedowse				getexp_err(ep, tgrp);
116375635Siedowse				goto nextline;
116475635Siedowse			}
11651558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
11661558Srgrimes
11671558Srgrimes		/*
11681558Srgrimes		 * Success. Update the data structures.
11691558Srgrimes		 */
11701558Srgrimes		if (has_host) {
11719336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
11721558Srgrimes			grp->gr_next = grphead;
11731558Srgrimes			grphead = tgrp;
11741558Srgrimes		} else {
11751558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
11769336Sdfr				opt_flags);
11771558Srgrimes			free_grp(grp);
11781558Srgrimes		}
11791558Srgrimes		dirhead = (struct dirlist *)NULL;
11801558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
11811558Srgrimes			ep2 = exphead;
11821558Srgrimes			epp = &exphead;
11831558Srgrimes
11841558Srgrimes			/*
11851558Srgrimes			 * Insert in the list in alphabetical order.
11861558Srgrimes			 */
11871558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
11881558Srgrimes				epp = &ep2->ex_next;
11891558Srgrimes				ep2 = ep2->ex_next;
11901558Srgrimes			}
11911558Srgrimes			if (ep2)
11921558Srgrimes				ep->ex_next = ep2;
11931558Srgrimes			*epp = ep;
11941558Srgrimes			ep->ex_flag |= EX_LINKED;
11951558Srgrimes		}
11961558Srgrimesnextline:
11971558Srgrimes		if (dirhead) {
11981558Srgrimes			free_dir(dirhead);
11991558Srgrimes			dirhead = (struct dirlist *)NULL;
12001558Srgrimes		}
12011558Srgrimes	}
12021558Srgrimes	fclose(exp_file);
12031558Srgrimes}
12041558Srgrimes
12051558Srgrimes/*
12061558Srgrimes * Allocate an export list element
12071558Srgrimes */
12081558Srgrimesstruct exportlist *
12091558Srgrimesget_exp()
12101558Srgrimes{
12111558Srgrimes	struct exportlist *ep;
12121558Srgrimes
12131558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
12141558Srgrimes	if (ep == (struct exportlist *)NULL)
12151558Srgrimes		out_of_mem();
121623681Speter	memset(ep, 0, sizeof(struct exportlist));
12171558Srgrimes	return (ep);
12181558Srgrimes}
12191558Srgrimes
12201558Srgrimes/*
12211558Srgrimes * Allocate a group list element
12221558Srgrimes */
12231558Srgrimesstruct grouplist *
12241558Srgrimesget_grp()
12251558Srgrimes{
12261558Srgrimes	struct grouplist *gp;
12271558Srgrimes
12281558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
12291558Srgrimes	if (gp == (struct grouplist *)NULL)
12301558Srgrimes		out_of_mem();
123123681Speter	memset(gp, 0, sizeof(struct grouplist));
12321558Srgrimes	return (gp);
12331558Srgrimes}
12341558Srgrimes
12351558Srgrimes/*
12361558Srgrimes * Clean up upon an error in get_exportlist().
12371558Srgrimes */
12381558Srgrimesvoid
12391558Srgrimesgetexp_err(ep, grp)
12401558Srgrimes	struct exportlist *ep;
12411558Srgrimes	struct grouplist *grp;
12421558Srgrimes{
12431558Srgrimes	struct grouplist *tgrp;
12441558Srgrimes
1245100336Sjoerg	if (!(opt_flags & OP_QUIET))
1246100336Sjoerg		syslog(LOG_ERR, "bad exports list line %s", line);
12471558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
12481558Srgrimes		free_exp(ep);
12491558Srgrimes	while (grp) {
12501558Srgrimes		tgrp = grp;
12511558Srgrimes		grp = grp->gr_next;
12521558Srgrimes		free_grp(tgrp);
12531558Srgrimes	}
12541558Srgrimes}
12551558Srgrimes
12561558Srgrimes/*
12571558Srgrimes * Search the export list for a matching fs.
12581558Srgrimes */
12591558Srgrimesstruct exportlist *
12601558Srgrimesex_search(fsid)
12611558Srgrimes	fsid_t *fsid;
12621558Srgrimes{
12631558Srgrimes	struct exportlist *ep;
12641558Srgrimes
12651558Srgrimes	ep = exphead;
12661558Srgrimes	while (ep) {
12671558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
12681558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
12691558Srgrimes			return (ep);
12701558Srgrimes		ep = ep->ex_next;
12711558Srgrimes	}
12721558Srgrimes	return (ep);
12731558Srgrimes}
12741558Srgrimes
12751558Srgrimes/*
12761558Srgrimes * Add a directory path to the list.
12771558Srgrimes */
12781558Srgrimeschar *
12791558Srgrimesadd_expdir(dpp, cp, len)
12801558Srgrimes	struct dirlist **dpp;
12811558Srgrimes	char *cp;
12821558Srgrimes	int len;
12831558Srgrimes{
12841558Srgrimes	struct dirlist *dp;
12851558Srgrimes
12861558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
128737663Scharnier	if (dp == (struct dirlist *)NULL)
128837663Scharnier		out_of_mem();
12891558Srgrimes	dp->dp_left = *dpp;
12901558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
12911558Srgrimes	dp->dp_flag = 0;
12921558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
12931558Srgrimes	strcpy(dp->dp_dirp, cp);
12941558Srgrimes	*dpp = dp;
12951558Srgrimes	return (dp->dp_dirp);
12961558Srgrimes}
12971558Srgrimes
12981558Srgrimes/*
12991558Srgrimes * Hang the dir list element off the dirpath binary tree as required
13001558Srgrimes * and update the entry for host.
13011558Srgrimes */
13021558Srgrimesvoid
13039336Sdfrhang_dirp(dp, grp, ep, flags)
13041558Srgrimes	struct dirlist *dp;
13051558Srgrimes	struct grouplist *grp;
13061558Srgrimes	struct exportlist *ep;
13079336Sdfr	int flags;
13081558Srgrimes{
13091558Srgrimes	struct hostlist *hp;
13101558Srgrimes	struct dirlist *dp2;
13111558Srgrimes
13129336Sdfr	if (flags & OP_ALLDIRS) {
13131558Srgrimes		if (ep->ex_defdir)
13141558Srgrimes			free((caddr_t)dp);
13151558Srgrimes		else
13161558Srgrimes			ep->ex_defdir = dp;
13179336Sdfr		if (grp == (struct grouplist *)NULL) {
13181558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
13199336Sdfr		} else while (grp) {
13201558Srgrimes			hp = get_ht();
13211558Srgrimes			hp->ht_grp = grp;
13221558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
13231558Srgrimes			ep->ex_defdir->dp_hosts = hp;
13241558Srgrimes			grp = grp->gr_next;
13251558Srgrimes		}
13261558Srgrimes	} else {
13271558Srgrimes
13281558Srgrimes		/*
132937663Scharnier		 * Loop through the directories adding them to the tree.
13301558Srgrimes		 */
13311558Srgrimes		while (dp) {
13321558Srgrimes			dp2 = dp->dp_left;
13339336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
13341558Srgrimes			dp = dp2;
13351558Srgrimes		}
13361558Srgrimes	}
13371558Srgrimes}
13381558Srgrimes
13391558Srgrimes/*
13401558Srgrimes * Traverse the binary tree either updating a node that is already there
13411558Srgrimes * for the new directory or adding the new node.
13421558Srgrimes */
13431558Srgrimesvoid
13449336Sdfradd_dlist(dpp, newdp, grp, flags)
13451558Srgrimes	struct dirlist **dpp;
13461558Srgrimes	struct dirlist *newdp;
13471558Srgrimes	struct grouplist *grp;
13489336Sdfr	int flags;
13491558Srgrimes{
13501558Srgrimes	struct dirlist *dp;
13511558Srgrimes	struct hostlist *hp;
13521558Srgrimes	int cmp;
13531558Srgrimes
13541558Srgrimes	dp = *dpp;
13551558Srgrimes	if (dp) {
13561558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
13571558Srgrimes		if (cmp > 0) {
13589336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
13591558Srgrimes			return;
13601558Srgrimes		} else if (cmp < 0) {
13619336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
13621558Srgrimes			return;
13631558Srgrimes		} else
13641558Srgrimes			free((caddr_t)newdp);
13651558Srgrimes	} else {
13661558Srgrimes		dp = newdp;
13671558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
13681558Srgrimes		*dpp = dp;
13691558Srgrimes	}
13701558Srgrimes	if (grp) {
13711558Srgrimes
13721558Srgrimes		/*
13731558Srgrimes		 * Hang all of the host(s) off of the directory point.
13741558Srgrimes		 */
13751558Srgrimes		do {
13761558Srgrimes			hp = get_ht();
13771558Srgrimes			hp->ht_grp = grp;
13781558Srgrimes			hp->ht_next = dp->dp_hosts;
13791558Srgrimes			dp->dp_hosts = hp;
13801558Srgrimes			grp = grp->gr_next;
13811558Srgrimes		} while (grp);
13829336Sdfr	} else {
13831558Srgrimes		dp->dp_flag |= DP_DEFSET;
13849336Sdfr	}
13851558Srgrimes}
13861558Srgrimes
13871558Srgrimes/*
13881558Srgrimes * Search for a dirpath on the export point.
13891558Srgrimes */
13901558Srgrimesstruct dirlist *
139174462Salfreddirp_search(dp, dirp)
13921558Srgrimes	struct dirlist *dp;
139374462Salfred	char *dirp;
13941558Srgrimes{
13951558Srgrimes	int cmp;
13961558Srgrimes
13971558Srgrimes	if (dp) {
139874462Salfred		cmp = strcmp(dp->dp_dirp, dirp);
13991558Srgrimes		if (cmp > 0)
140074462Salfred			return (dirp_search(dp->dp_left, dirp));
14011558Srgrimes		else if (cmp < 0)
140274462Salfred			return (dirp_search(dp->dp_right, dirp));
14031558Srgrimes		else
14041558Srgrimes			return (dp);
14051558Srgrimes	}
14061558Srgrimes	return (dp);
14071558Srgrimes}
14081558Srgrimes
14091558Srgrimes/*
14101558Srgrimes * Scan for a host match in a directory tree.
14111558Srgrimes */
14121558Srgrimesint
14139336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
14141558Srgrimes	struct dirlist *dp;
141574462Salfred	struct sockaddr *saddr;
14161558Srgrimes	int *defsetp;
14179336Sdfr	int *hostsetp;
14181558Srgrimes{
14191558Srgrimes	struct hostlist *hp;
14201558Srgrimes	struct grouplist *grp;
142174462Salfred	struct addrinfo *ai;
14221558Srgrimes
14231558Srgrimes	if (dp) {
14241558Srgrimes		if (dp->dp_flag & DP_DEFSET)
14259336Sdfr			*defsetp = dp->dp_flag;
14261558Srgrimes		hp = dp->dp_hosts;
14271558Srgrimes		while (hp) {
14281558Srgrimes			grp = hp->ht_grp;
14291558Srgrimes			switch (grp->gr_type) {
14301558Srgrimes			case GT_HOST:
143174462Salfred				ai = grp->gr_ptr.gt_addrinfo;
143274462Salfred				for (; ai; ai = ai->ai_next) {
143375801Siedowse					if (!sacmp(ai->ai_addr, saddr, NULL)) {
143474462Salfred						*hostsetp =
143574462Salfred						    (hp->ht_flag | DP_HOSTSET);
143674462Salfred						return (1);
143774462Salfred					}
14389336Sdfr				}
143975801Siedowse				break;
14401558Srgrimes			case GT_NET:
144175801Siedowse				if (!sacmp(saddr, (struct sockaddr *)
144275801Siedowse				    &grp->gr_ptr.gt_net.nt_net,
144375801Siedowse				    (struct sockaddr *)
144475801Siedowse				    &grp->gr_ptr.gt_net.nt_mask)) {
144574462Salfred					*hostsetp = (hp->ht_flag | DP_HOSTSET);
144674462Salfred					return (1);
144774462Salfred				}
144875801Siedowse				break;
144975801Siedowse			}
14501558Srgrimes			hp = hp->ht_next;
14511558Srgrimes		}
14521558Srgrimes	}
14531558Srgrimes	return (0);
14541558Srgrimes}
14551558Srgrimes
14561558Srgrimes/*
14571558Srgrimes * Scan tree for a host that matches the address.
14581558Srgrimes */
14591558Srgrimesint
14601558Srgrimesscan_tree(dp, saddr)
14611558Srgrimes	struct dirlist *dp;
146274462Salfred	struct sockaddr *saddr;
14631558Srgrimes{
14649336Sdfr	int defset, hostset;
14651558Srgrimes
14661558Srgrimes	if (dp) {
14671558Srgrimes		if (scan_tree(dp->dp_left, saddr))
14681558Srgrimes			return (1);
14699336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
14701558Srgrimes			return (1);
14711558Srgrimes		if (scan_tree(dp->dp_right, saddr))
14721558Srgrimes			return (1);
14731558Srgrimes	}
14741558Srgrimes	return (0);
14751558Srgrimes}
14761558Srgrimes
14771558Srgrimes/*
14781558Srgrimes * Traverse the dirlist tree and free it up.
14791558Srgrimes */
14801558Srgrimesvoid
14811558Srgrimesfree_dir(dp)
14821558Srgrimes	struct dirlist *dp;
14831558Srgrimes{
14841558Srgrimes
14851558Srgrimes	if (dp) {
14861558Srgrimes		free_dir(dp->dp_left);
14871558Srgrimes		free_dir(dp->dp_right);
14881558Srgrimes		free_host(dp->dp_hosts);
14891558Srgrimes		free((caddr_t)dp);
14901558Srgrimes	}
14911558Srgrimes}
14921558Srgrimes
14931558Srgrimes/*
14941558Srgrimes * Parse the option string and update fields.
14951558Srgrimes * Option arguments may either be -<option>=<value> or
14961558Srgrimes * -<option> <value>
14971558Srgrimes */
14981558Srgrimesint
14991558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
15001558Srgrimes	char **cpp, **endcpp;
15011558Srgrimes	struct exportlist *ep;
15021558Srgrimes	struct grouplist *grp;
15031558Srgrimes	int *has_hostp;
15041558Srgrimes	int *exflagsp;
150572650Sgreen	struct xucred *cr;
15061558Srgrimes{
15071558Srgrimes	char *cpoptarg, *cpoptend;
15081558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
15091558Srgrimes	int allflag, usedarg;
15101558Srgrimes
151151968Salfred	savedc2 = '\0';
15121558Srgrimes	cpopt = *cpp;
15131558Srgrimes	cpopt++;
15141558Srgrimes	cp = *endcpp;
15151558Srgrimes	savedc = *cp;
15161558Srgrimes	*cp = '\0';
15171558Srgrimes	while (cpopt && *cpopt) {
15181558Srgrimes		allflag = 1;
15191558Srgrimes		usedarg = -2;
152037663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
15211558Srgrimes			*cpoptend++ = '\0';
152237663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15231558Srgrimes				*cpoptarg++ = '\0';
15241558Srgrimes		} else {
152537663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15261558Srgrimes				*cpoptarg++ = '\0';
15271558Srgrimes			else {
15281558Srgrimes				*cp = savedc;
15291558Srgrimes				nextfield(&cp, &endcp);
15301558Srgrimes				**endcpp = '\0';
15311558Srgrimes				if (endcp > cp && *cp != '-') {
15321558Srgrimes					cpoptarg = cp;
15331558Srgrimes					savedc2 = *endcp;
15341558Srgrimes					*endcp = '\0';
15351558Srgrimes					usedarg = 0;
15361558Srgrimes				}
15371558Srgrimes			}
15381558Srgrimes		}
15391558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
15401558Srgrimes			*exflagsp |= MNT_EXRDONLY;
15411558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
15421558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
15431558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
15441558Srgrimes			usedarg++;
15451558Srgrimes			parsecred(cpoptarg, cr);
15461558Srgrimes			if (allflag == 0) {
15471558Srgrimes				*exflagsp |= MNT_EXPORTANON;
15481558Srgrimes				opt_flags |= OP_MAPALL;
15491558Srgrimes			} else
15501558Srgrimes				opt_flags |= OP_MAPROOT;
15511558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
155275801Siedowse		    !strcmp(cpopt, "m"))) {
15531558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
155437663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
15551558Srgrimes				return (1);
15561558Srgrimes			}
15571558Srgrimes			usedarg++;
15581558Srgrimes			opt_flags |= OP_MASK;
15591558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
15601558Srgrimes			!strcmp(cpopt, "n"))) {
156174462Salfred			if (strchr(cpoptarg, '/') != NULL) {
156274462Salfred				if (debug)
156374462Salfred					fprintf(stderr, "setting OP_MASKLEN\n");
156474462Salfred				opt_flags |= OP_MASKLEN;
156574462Salfred			}
15661558Srgrimes			if (grp->gr_type != GT_NULL) {
156737663Scharnier				syslog(LOG_ERR, "network/host conflict");
15681558Srgrimes				return (1);
15691558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
157037663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
15711558Srgrimes				return (1);
15721558Srgrimes			}
15731558Srgrimes			grp->gr_type = GT_NET;
15741558Srgrimes			*has_hostp = 1;
15751558Srgrimes			usedarg++;
15761558Srgrimes			opt_flags |= OP_NET;
15771558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
15781558Srgrimes			opt_flags |= OP_ALLDIRS;
157927447Sdfr		} else if (!strcmp(cpopt, "public")) {
158027447Sdfr			*exflagsp |= MNT_EXPUBLIC;
158127447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
158227447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
158327447Sdfr			opt_flags |= OP_MAPALL;
158427447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
158527447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
1586100336Sjoerg		} else if (!strcmp(cpopt, "quiet")) {
1587100336Sjoerg			opt_flags |= OP_QUIET;
15881558Srgrimes		} else {
158937663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
15901558Srgrimes			return (1);
15911558Srgrimes		}
15921558Srgrimes		if (usedarg >= 0) {
15931558Srgrimes			*endcp = savedc2;
15941558Srgrimes			**endcpp = savedc;
15951558Srgrimes			if (usedarg > 0) {
15961558Srgrimes				*cpp = cp;
15971558Srgrimes				*endcpp = endcp;
15981558Srgrimes			}
15991558Srgrimes			return (0);
16001558Srgrimes		}
16011558Srgrimes		cpopt = cpoptend;
16021558Srgrimes	}
16031558Srgrimes	**endcpp = savedc;
16041558Srgrimes	return (0);
16051558Srgrimes}
16061558Srgrimes
16071558Srgrimes/*
16081558Srgrimes * Translate a character string to the corresponding list of network
16091558Srgrimes * addresses for a hostname.
16101558Srgrimes */
16111558Srgrimesint
16127401Swpaulget_host(cp, grp, tgrp)
16131558Srgrimes	char *cp;
16141558Srgrimes	struct grouplist *grp;
16157401Swpaul	struct grouplist *tgrp;
16161558Srgrimes{
16177401Swpaul	struct grouplist *checkgrp;
161875635Siedowse	struct addrinfo *ai, *tai, hints;
161974462Salfred	int ecode;
162074462Salfred	char host[NI_MAXHOST];
16211558Srgrimes
162274462Salfred	if (grp->gr_type != GT_NULL) {
162374462Salfred		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
16241558Srgrimes		return (1);
16251558Srgrimes	}
162674462Salfred	memset(&hints, 0, sizeof hints);
162774462Salfred	hints.ai_flags = AI_CANONNAME;
162874462Salfred	hints.ai_protocol = IPPROTO_UDP;
162974462Salfred	ecode = getaddrinfo(cp, NULL, &hints, &ai);
163074462Salfred	if (ecode != 0) {
163175635Siedowse		syslog(LOG_ERR,"can't get address info for host %s", cp);
163274462Salfred		return 1;
163374462Salfred	}
163474462Salfred	grp->gr_ptr.gt_addrinfo = ai;
163574462Salfred	while (ai != NULL) {
163674462Salfred		if (ai->ai_canonname == NULL) {
163774462Salfred			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
163874462Salfred			    sizeof host, NULL, 0, ninumeric) != 0)
163974462Salfred				strlcpy(host, "?", sizeof(host));
164074462Salfred			ai->ai_canonname = strdup(host);
164174462Salfred			ai->ai_flags |= AI_CANONNAME;
164275641Siedowse		}
164374462Salfred		if (debug)
164475635Siedowse			fprintf(stderr, "got host %s\n", ai->ai_canonname);
164575635Siedowse		/*
164675635Siedowse		 * Sanity check: make sure we don't already have an entry
164775635Siedowse		 * for this host in the grouplist.
164875635Siedowse		 */
164975635Siedowse		for (checkgrp = tgrp; checkgrp != NULL;
165075635Siedowse		    checkgrp = checkgrp->gr_next) {
165175635Siedowse			if (checkgrp->gr_type != GT_HOST)
165275635Siedowse				continue;
165375635Siedowse			for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
165475635Siedowse			    tai = tai->ai_next) {
165575801Siedowse				if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
165675635Siedowse					continue;
165775635Siedowse				if (debug)
165875635Siedowse					fprintf(stderr,
165975635Siedowse					    "ignoring duplicate host %s\n",
166075635Siedowse					    ai->ai_canonname);
166175635Siedowse				grp->gr_type = GT_IGNORE;
166275635Siedowse				return (0);
166375635Siedowse			}
166475635Siedowse		}
166574462Salfred		ai = ai->ai_next;
16661558Srgrimes	}
166775635Siedowse	grp->gr_type = GT_HOST;
16681558Srgrimes	return (0);
16691558Srgrimes}
16701558Srgrimes
16711558Srgrimes/*
16721558Srgrimes * Free up an exports list component
16731558Srgrimes */
16741558Srgrimesvoid
16751558Srgrimesfree_exp(ep)
16761558Srgrimes	struct exportlist *ep;
16771558Srgrimes{
16781558Srgrimes
16791558Srgrimes	if (ep->ex_defdir) {
16801558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
16811558Srgrimes		free((caddr_t)ep->ex_defdir);
16821558Srgrimes	}
16831558Srgrimes	if (ep->ex_fsdir)
16841558Srgrimes		free(ep->ex_fsdir);
168527447Sdfr	if (ep->ex_indexfile)
168627447Sdfr		free(ep->ex_indexfile);
16871558Srgrimes	free_dir(ep->ex_dirl);
16881558Srgrimes	free((caddr_t)ep);
16891558Srgrimes}
16901558Srgrimes
16911558Srgrimes/*
16921558Srgrimes * Free hosts.
16931558Srgrimes */
16941558Srgrimesvoid
16951558Srgrimesfree_host(hp)
16961558Srgrimes	struct hostlist *hp;
16971558Srgrimes{
16981558Srgrimes	struct hostlist *hp2;
16991558Srgrimes
17001558Srgrimes	while (hp) {
17011558Srgrimes		hp2 = hp;
17021558Srgrimes		hp = hp->ht_next;
17031558Srgrimes		free((caddr_t)hp2);
17041558Srgrimes	}
17051558Srgrimes}
17061558Srgrimes
17071558Srgrimesstruct hostlist *
17081558Srgrimesget_ht()
17091558Srgrimes{
17101558Srgrimes	struct hostlist *hp;
17111558Srgrimes
17121558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
17131558Srgrimes	if (hp == (struct hostlist *)NULL)
17141558Srgrimes		out_of_mem();
17151558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
17169336Sdfr	hp->ht_flag = 0;
17171558Srgrimes	return (hp);
17181558Srgrimes}
17191558Srgrimes
17201558Srgrimes/*
17211558Srgrimes * Out of memory, fatal
17221558Srgrimes */
17231558Srgrimesvoid
17241558Srgrimesout_of_mem()
17251558Srgrimes{
17261558Srgrimes
172737663Scharnier	syslog(LOG_ERR, "out of memory");
17281558Srgrimes	exit(2);
17291558Srgrimes}
17301558Srgrimes
17311558Srgrimes/*
17321558Srgrimes * Do the mount syscall with the update flag to push the export info into
17331558Srgrimes * the kernel.
17341558Srgrimes */
17351558Srgrimesint
17361558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
17371558Srgrimes	struct exportlist *ep;
17381558Srgrimes	struct grouplist *grp;
17391558Srgrimes	int exflags;
174072650Sgreen	struct xucred *anoncrp;
17411558Srgrimes	char *dirp;
17421558Srgrimes	int dirplen;
17431558Srgrimes	struct statfs *fsb;
17441558Srgrimes{
174575841Siedowse	struct statfs fsb1;
174674462Salfred	struct addrinfo *ai;
174775801Siedowse	struct export_args *eap;
174874462Salfred	char *cp = NULL;
17491558Srgrimes	int done;
17501558Srgrimes	char savedc = '\0';
17511558Srgrimes	union {
17521558Srgrimes		struct ufs_args ua;
17531558Srgrimes		struct iso_args ia;
17549336Sdfr		struct msdosfs_args da;
175554093Ssemenu		struct ntfs_args na;
17561558Srgrimes	} args;
17571558Srgrimes
175875801Siedowse	bzero(&args, sizeof args);
175975801Siedowse	/* XXX, we assume that all xx_args look like ufs_args. */
17601558Srgrimes	args.ua.fspec = 0;
176175801Siedowse	eap = &args.ua.export;
176275801Siedowse
176375801Siedowse	eap->ex_flags = exflags;
176475801Siedowse	eap->ex_anon = *anoncrp;
176575801Siedowse	eap->ex_indexfile = ep->ex_indexfile;
176675641Siedowse	if (grp->gr_type == GT_HOST)
176774462Salfred		ai = grp->gr_ptr.gt_addrinfo;
176875641Siedowse	else
176975641Siedowse		ai = NULL;
17701558Srgrimes	done = FALSE;
17711558Srgrimes	while (!done) {
17721558Srgrimes		switch (grp->gr_type) {
17731558Srgrimes		case GT_HOST:
177475641Siedowse			if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
177574462Salfred				goto skip;
177675801Siedowse			eap->ex_addr = ai->ai_addr;
177775801Siedowse			eap->ex_addrlen = ai->ai_addrlen;
177875801Siedowse			eap->ex_masklen = 0;
17791558Srgrimes			break;
17801558Srgrimes		case GT_NET:
178175801Siedowse			if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
178274462Salfred			    have_v6 == 0)
178374462Salfred				goto skip;
178475801Siedowse			eap->ex_addr =
178575801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
178675801Siedowse			eap->ex_addrlen = args.ua.export.ex_addr->sa_len;
178775801Siedowse			eap->ex_mask =
178875801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
178975801Siedowse			eap->ex_masklen = args.ua.export.ex_mask->sa_len;
17901558Srgrimes			break;
179175641Siedowse		case GT_DEFAULT:
179275801Siedowse			eap->ex_addr = NULL;
179375801Siedowse			eap->ex_addrlen = 0;
179475801Siedowse			eap->ex_mask = NULL;
179575801Siedowse			eap->ex_masklen = 0;
179675641Siedowse			break;
17977401Swpaul		case GT_IGNORE:
17987401Swpaul			return(0);
17997401Swpaul			break;
18001558Srgrimes		default:
180137663Scharnier			syslog(LOG_ERR, "bad grouptype");
18021558Srgrimes			if (cp)
18031558Srgrimes				*cp = savedc;
18041558Srgrimes			return (1);
18051558Srgrimes		};
18061558Srgrimes
18071558Srgrimes		/*
18081558Srgrimes		 * XXX:
18091558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
18101558Srgrimes		 * of looping back up the dirp to the mount point??
18111558Srgrimes		 * Also, needs to know how to export all types of local
181296707Strhodes		 * exportable filesystems and not just "ufs".
18131558Srgrimes		 */
18149336Sdfr		while (mount(fsb->f_fstypename, dirp,
181574462Salfred		    fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
18161558Srgrimes			if (cp)
18171558Srgrimes				*cp-- = savedc;
18181558Srgrimes			else
18191558Srgrimes				cp = dirp + dirplen - 1;
1820100336Sjoerg			if (opt_flags & OP_QUIET)
1821100336Sjoerg				return (1);
18221558Srgrimes			if (errno == EPERM) {
182375635Siedowse				if (debug)
182475635Siedowse					warnx("can't change attributes for %s",
182575635Siedowse					    dirp);
18261558Srgrimes				syslog(LOG_ERR,
182737663Scharnier				   "can't change attributes for %s", dirp);
18281558Srgrimes				return (1);
18291558Srgrimes			}
18301558Srgrimes			if (opt_flags & OP_ALLDIRS) {
1831100336Sjoerg				if (errno == EINVAL)
1832100336Sjoerg					syslog(LOG_ERR,
1833100336Sjoerg		"-alldirs requested but %s is not a filesystem mountpoint",
1834100336Sjoerg						dirp);
1835100336Sjoerg				else
1836100336Sjoerg					syslog(LOG_ERR,
1837100336Sjoerg						"could not remount %s: %m",
1838100336Sjoerg						dirp);
18391558Srgrimes				return (1);
18401558Srgrimes			}
18411558Srgrimes			/* back up over the last component */
18421558Srgrimes			while (*cp == '/' && cp > dirp)
18431558Srgrimes				cp--;
18441558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
18451558Srgrimes				cp--;
18461558Srgrimes			if (cp == dirp) {
18471558Srgrimes				if (debug)
184837663Scharnier					warnx("mnt unsucc");
184937663Scharnier				syslog(LOG_ERR, "can't export %s", dirp);
18501558Srgrimes				return (1);
18511558Srgrimes			}
18521558Srgrimes			savedc = *cp;
18531558Srgrimes			*cp = '\0';
185475841Siedowse			/* Check that we're still on the same filesystem. */
185575841Siedowse			if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
185675841Siedowse			    &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
185775841Siedowse				*cp = savedc;
185875841Siedowse				syslog(LOG_ERR, "can't export %s", dirp);
185975841Siedowse				return (1);
186075841Siedowse			}
18611558Srgrimes		}
186274462Salfredskip:
186375641Siedowse		if (ai != NULL)
186474462Salfred			ai = ai->ai_next;
186575641Siedowse		if (ai == NULL)
18661558Srgrimes			done = TRUE;
18671558Srgrimes	}
18681558Srgrimes	if (cp)
18691558Srgrimes		*cp = savedc;
18701558Srgrimes	return (0);
18711558Srgrimes}
18721558Srgrimes
18731558Srgrimes/*
18741558Srgrimes * Translate a net address.
187575801Siedowse *
187675801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
18771558Srgrimes */
18781558Srgrimesint
18791558Srgrimesget_net(cp, net, maskflg)
18801558Srgrimes	char *cp;
18811558Srgrimes	struct netmsk *net;
18821558Srgrimes	int maskflg;
18831558Srgrimes{
188475861Siedowse	struct netent *np = NULL;
188574462Salfred	char *name, *p, *prefp;
188675801Siedowse	struct sockaddr_in sin;
188775861Siedowse	struct sockaddr *sa = NULL;
188874462Salfred	struct addrinfo hints, *ai = NULL;
188974462Salfred	char netname[NI_MAXHOST];
189074462Salfred	long preflen;
18911558Srgrimes
189275635Siedowse	p = prefp = NULL;
189374462Salfred	if ((opt_flags & OP_MASKLEN) && !maskflg) {
189474462Salfred		p = strchr(cp, '/');
189574462Salfred		*p = '\0';
189674462Salfred		prefp = p + 1;
189774462Salfred	}
189874462Salfred
189975861Siedowse	/*
190075861Siedowse	 * Check for a numeric address first. We wish to avoid
190175861Siedowse	 * possible DNS lookups in getnetbyname().
190275861Siedowse	 */
190375861Siedowse	if (isxdigit(*cp) || *cp == ':') {
190474462Salfred		memset(&hints, 0, sizeof hints);
190575801Siedowse		/* Ensure the mask and the network have the same family. */
190675801Siedowse		if (maskflg && (opt_flags & OP_NET))
190775801Siedowse			hints.ai_family = net->nt_net.ss_family;
190875801Siedowse		else if (!maskflg && (opt_flags & OP_HAVEMASK))
190975801Siedowse			hints.ai_family = net->nt_mask.ss_family;
191075801Siedowse		else
191175801Siedowse			hints.ai_family = AF_UNSPEC;
191274462Salfred		hints.ai_flags = AI_NUMERICHOST;
191375861Siedowse		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
191475861Siedowse			sa = ai->ai_addr;
191575861Siedowse		if (sa != NULL && ai->ai_family == AF_INET) {
191674462Salfred			/*
191775801Siedowse			 * The address in `cp' is really a network address, so
191875801Siedowse			 * use inet_network() to re-interpret this correctly.
191975801Siedowse			 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
192074462Salfred			 */
192175801Siedowse			bzero(&sin, sizeof sin);
192274462Salfred			sin.sin_family = AF_INET;
192374462Salfred			sin.sin_len = sizeof sin;
192475801Siedowse			sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
192574462Salfred			if (debug)
192675801Siedowse				fprintf(stderr, "get_net: v4 addr %s\n",
192775801Siedowse				    inet_ntoa(sin.sin_addr));
192874462Salfred			sa = (struct sockaddr *)&sin;
192975861Siedowse		}
193075861Siedowse	}
193175861Siedowse	if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
193275861Siedowse		bzero(&sin, sizeof sin);
193375861Siedowse		sin.sin_family = AF_INET;
193475861Siedowse		sin.sin_len = sizeof sin;
193575861Siedowse		sin.sin_addr = inet_makeaddr(np->n_net, 0);
193675861Siedowse		sa = (struct sockaddr *)&sin;
193775861Siedowse	}
193875861Siedowse	if (sa == NULL)
193974462Salfred		goto fail;
194025318Spst
194175801Siedowse	if (maskflg) {
194275801Siedowse		/* The specified sockaddr is a mask. */
194375801Siedowse		if (checkmask(sa) != 0)
194475801Siedowse			goto fail;
194575801Siedowse		bcopy(sa, &net->nt_mask, sa->sa_len);
194675801Siedowse		opt_flags |= OP_HAVEMASK;
194775801Siedowse	} else {
194875801Siedowse		/* The specified sockaddr is a network address. */
194975801Siedowse		bcopy(sa, &net->nt_net, sa->sa_len);
195074462Salfred
195175801Siedowse		/* Get a network name for the export list. */
195275801Siedowse		if (np) {
195375801Siedowse			name = np->n_name;
195475801Siedowse		} else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
195575801Siedowse		   NULL, 0, ninumeric) == 0) {
195675801Siedowse			name = netname;
195775801Siedowse		} else {
195875801Siedowse			goto fail;
195975801Siedowse		}
196075801Siedowse		if ((net->nt_name = strdup(name)) == NULL)
196175801Siedowse			out_of_mem();
196275801Siedowse
196375801Siedowse		/*
196475801Siedowse		 * Extract a mask from either a "/<masklen>" suffix, or
196575801Siedowse		 * from the class of an IPv4 address.
196675801Siedowse		 */
196774462Salfred		if (opt_flags & OP_MASKLEN) {
196874462Salfred			preflen = strtol(prefp, NULL, 10);
196975801Siedowse			if (preflen < 0L || preflen == LONG_MAX)
197074462Salfred				goto fail;
197175801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
197275801Siedowse			if (makemask(&net->nt_mask, (int)preflen) != 0)
197375801Siedowse				goto fail;
197475801Siedowse			opt_flags |= OP_HAVEMASK;
197574462Salfred			*p = '/';
197675801Siedowse		} else if (sa->sa_family == AF_INET &&
197775801Siedowse		    (opt_flags & OP_MASK) == 0) {
197875801Siedowse			in_addr_t addr;
197974462Salfred
198075801Siedowse			addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
198175801Siedowse			if (IN_CLASSA(addr))
198275801Siedowse				preflen = 8;
198375801Siedowse			else if (IN_CLASSB(addr))
198475801Siedowse				preflen = 16;
198575801Siedowse			else if (IN_CLASSC(addr))
198675801Siedowse				preflen = 24;
198775801Siedowse			else if (IN_CLASSD(addr))
198875801Siedowse				preflen = 28;
198975801Siedowse			else
199075801Siedowse				preflen = 32;	/* XXX */
199175801Siedowse
199275801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
199375801Siedowse			makemask(&net->nt_mask, (int)preflen);
199475801Siedowse			opt_flags |= OP_HAVEMASK;
199574462Salfred		}
199674462Salfred	}
199774462Salfred
199874462Salfred	if (ai)
199974462Salfred		freeaddrinfo(ai);
200074462Salfred	return 0;
200174462Salfred
200274462Salfredfail:
200374462Salfred	if (ai)
200474462Salfred		freeaddrinfo(ai);
200574462Salfred	return 1;
20061558Srgrimes}
20071558Srgrimes
20081558Srgrimes/*
20091558Srgrimes * Parse out the next white space separated field
20101558Srgrimes */
20111558Srgrimesvoid
20121558Srgrimesnextfield(cp, endcp)
20131558Srgrimes	char **cp;
20141558Srgrimes	char **endcp;
20151558Srgrimes{
20161558Srgrimes	char *p;
20171558Srgrimes
20181558Srgrimes	p = *cp;
20191558Srgrimes	while (*p == ' ' || *p == '\t')
20201558Srgrimes		p++;
20211558Srgrimes	if (*p == '\n' || *p == '\0')
20221558Srgrimes		*cp = *endcp = p;
20231558Srgrimes	else {
20241558Srgrimes		*cp = p++;
20251558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
20261558Srgrimes			p++;
20271558Srgrimes		*endcp = p;
20281558Srgrimes	}
20291558Srgrimes}
20301558Srgrimes
20311558Srgrimes/*
20321558Srgrimes * Get an exports file line. Skip over blank lines and handle line
20331558Srgrimes * continuations.
20341558Srgrimes */
20351558Srgrimesint
20361558Srgrimesget_line()
20371558Srgrimes{
20381558Srgrimes	char *p, *cp;
203996622Siedowse	size_t len;
20401558Srgrimes	int totlen, cont_line;
20411558Srgrimes
20421558Srgrimes	/*
20431558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
20441558Srgrimes	 */
20451558Srgrimes	p = line;
20461558Srgrimes	totlen = 0;
20471558Srgrimes	do {
204896622Siedowse		if ((p = fgetln(exp_file, &len)) == NULL)
20491558Srgrimes			return (0);
20501558Srgrimes		cp = p + len - 1;
20511558Srgrimes		cont_line = 0;
20521558Srgrimes		while (cp >= p &&
20531558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
20541558Srgrimes			if (*cp == '\\')
20551558Srgrimes				cont_line = 1;
20561558Srgrimes			cp--;
20571558Srgrimes			len--;
20581558Srgrimes		}
205979117Sdd		if (cont_line) {
206079117Sdd			*++cp = ' ';
206179117Sdd			len++;
206279117Sdd		}
206396622Siedowse		if (linesize < len + totlen + 1) {
206496622Siedowse			linesize = len + totlen + 1;
206596622Siedowse			line = realloc(line, linesize);
206696622Siedowse			if (line == NULL)
206796622Siedowse				out_of_mem();
20681558Srgrimes		}
206996622Siedowse		memcpy(line + totlen, p, len);
207096622Siedowse		totlen += len;
207196622Siedowse		line[totlen] = '\0';
20721558Srgrimes	} while (totlen == 0 || cont_line);
20731558Srgrimes	return (1);
20741558Srgrimes}
20751558Srgrimes
20761558Srgrimes/*
20771558Srgrimes * Parse a description of a credential.
20781558Srgrimes */
20791558Srgrimesvoid
20801558Srgrimesparsecred(namelist, cr)
20811558Srgrimes	char *namelist;
208272650Sgreen	struct xucred *cr;
20831558Srgrimes{
20841558Srgrimes	char *name;
20851558Srgrimes	int cnt;
20861558Srgrimes	char *names;
20871558Srgrimes	struct passwd *pw;
20881558Srgrimes	struct group *gr;
20891558Srgrimes	int ngroups, groups[NGROUPS + 1];
20901558Srgrimes
209191354Sdd	cr->cr_version = XUCRED_VERSION;
20921558Srgrimes	/*
209337663Scharnier	 * Set up the unprivileged user.
20941558Srgrimes	 */
20951558Srgrimes	cr->cr_uid = -2;
20961558Srgrimes	cr->cr_groups[0] = -2;
20971558Srgrimes	cr->cr_ngroups = 1;
20981558Srgrimes	/*
20991558Srgrimes	 * Get the user's password table entry.
21001558Srgrimes	 */
21011558Srgrimes	names = strsep(&namelist, " \t\n");
21021558Srgrimes	name = strsep(&names, ":");
21031558Srgrimes	if (isdigit(*name) || *name == '-')
21041558Srgrimes		pw = getpwuid(atoi(name));
21051558Srgrimes	else
21061558Srgrimes		pw = getpwnam(name);
21071558Srgrimes	/*
21081558Srgrimes	 * Credentials specified as those of a user.
21091558Srgrimes	 */
21101558Srgrimes	if (names == NULL) {
21111558Srgrimes		if (pw == NULL) {
211237663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
21131558Srgrimes			return;
21141558Srgrimes		}
21151558Srgrimes		cr->cr_uid = pw->pw_uid;
21161558Srgrimes		ngroups = NGROUPS + 1;
21171558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
211837663Scharnier			syslog(LOG_ERR, "too many groups");
21191558Srgrimes		/*
21201558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
21211558Srgrimes		 */
21221558Srgrimes		cr->cr_ngroups = ngroups - 1;
21231558Srgrimes		cr->cr_groups[0] = groups[0];
21241558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
21251558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
21261558Srgrimes		return;
21271558Srgrimes	}
21281558Srgrimes	/*
21291558Srgrimes	 * Explicit credential specified as a colon separated list:
21301558Srgrimes	 *	uid:gid:gid:...
21311558Srgrimes	 */
21321558Srgrimes	if (pw != NULL)
21331558Srgrimes		cr->cr_uid = pw->pw_uid;
21341558Srgrimes	else if (isdigit(*name) || *name == '-')
21351558Srgrimes		cr->cr_uid = atoi(name);
21361558Srgrimes	else {
213737663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
21381558Srgrimes		return;
21391558Srgrimes	}
21401558Srgrimes	cr->cr_ngroups = 0;
21411558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
21421558Srgrimes		name = strsep(&names, ":");
21431558Srgrimes		if (isdigit(*name) || *name == '-') {
21441558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
21451558Srgrimes		} else {
21461558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
214737663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
21481558Srgrimes				continue;
21491558Srgrimes			}
21501558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
21511558Srgrimes		}
21521558Srgrimes	}
21531558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
215437663Scharnier		syslog(LOG_ERR, "too many groups");
21551558Srgrimes}
21561558Srgrimes
21571558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
21581558Srgrimes/*
21591558Srgrimes * Routines that maintain the remote mounttab
21601558Srgrimes */
21611558Srgrimesvoid
21621558Srgrimesget_mountlist()
21631558Srgrimes{
21641558Srgrimes	struct mountlist *mlp, **mlpp;
216523681Speter	char *host, *dirp, *cp;
21661558Srgrimes	char str[STRSIZ];
21671558Srgrimes	FILE *mlfile;
21681558Srgrimes
21691558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
217053117Sbillf		if (errno == ENOENT)
217153117Sbillf			return;
217253117Sbillf		else {
217353117Sbillf			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
217453117Sbillf			return;
217553117Sbillf		}
21761558Srgrimes	}
21771558Srgrimes	mlpp = &mlhead;
21781558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
217923681Speter		cp = str;
218023681Speter		host = strsep(&cp, " \t\n");
218123681Speter		dirp = strsep(&cp, " \t\n");
218223681Speter		if (host == NULL || dirp == NULL)
21831558Srgrimes			continue;
21841558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
218537663Scharnier		if (mlp == (struct mountlist *)NULL)
218637663Scharnier			out_of_mem();
218723681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
218823681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
218923681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
219023681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
21911558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
21921558Srgrimes		*mlpp = mlp;
21931558Srgrimes		mlpp = &mlp->ml_next;
21941558Srgrimes	}
21951558Srgrimes	fclose(mlfile);
21961558Srgrimes}
21971558Srgrimes
219875635Siedowsevoid
219975635Siedowsedel_mlist(char *hostp, char *dirp)
22001558Srgrimes{
22011558Srgrimes	struct mountlist *mlp, **mlpp;
22021558Srgrimes	struct mountlist *mlp2;
22031558Srgrimes	FILE *mlfile;
22041558Srgrimes	int fnd = 0;
22051558Srgrimes
22061558Srgrimes	mlpp = &mlhead;
22071558Srgrimes	mlp = mlhead;
22081558Srgrimes	while (mlp) {
22091558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
22101558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
22111558Srgrimes			fnd = 1;
22121558Srgrimes			mlp2 = mlp;
22131558Srgrimes			*mlpp = mlp = mlp->ml_next;
22141558Srgrimes			free((caddr_t)mlp2);
22151558Srgrimes		} else {
22161558Srgrimes			mlpp = &mlp->ml_next;
22171558Srgrimes			mlp = mlp->ml_next;
22181558Srgrimes		}
22191558Srgrimes	}
22201558Srgrimes	if (fnd) {
22211558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
222237663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
22231558Srgrimes			return;
22241558Srgrimes		}
22251558Srgrimes		mlp = mlhead;
22261558Srgrimes		while (mlp) {
22271558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22281558Srgrimes			mlp = mlp->ml_next;
22291558Srgrimes		}
22301558Srgrimes		fclose(mlfile);
22311558Srgrimes	}
22321558Srgrimes}
22331558Srgrimes
22341558Srgrimesvoid
22351558Srgrimesadd_mlist(hostp, dirp)
22361558Srgrimes	char *hostp, *dirp;
22371558Srgrimes{
22381558Srgrimes	struct mountlist *mlp, **mlpp;
22391558Srgrimes	FILE *mlfile;
22401558Srgrimes
22411558Srgrimes	mlpp = &mlhead;
22421558Srgrimes	mlp = mlhead;
22431558Srgrimes	while (mlp) {
22441558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
22451558Srgrimes			return;
22461558Srgrimes		mlpp = &mlp->ml_next;
22471558Srgrimes		mlp = mlp->ml_next;
22481558Srgrimes	}
22491558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
225037663Scharnier	if (mlp == (struct mountlist *)NULL)
225137663Scharnier		out_of_mem();
22521558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
22531558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
22541558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
22551558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
22561558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
22571558Srgrimes	*mlpp = mlp;
22581558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
225937663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
22601558Srgrimes		return;
22611558Srgrimes	}
22621558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22631558Srgrimes	fclose(mlfile);
22641558Srgrimes}
22651558Srgrimes
22661558Srgrimes/*
22671558Srgrimes * Free up a group list.
22681558Srgrimes */
22691558Srgrimesvoid
22701558Srgrimesfree_grp(grp)
22711558Srgrimes	struct grouplist *grp;
22721558Srgrimes{
22731558Srgrimes	if (grp->gr_type == GT_HOST) {
227474462Salfred		if (grp->gr_ptr.gt_addrinfo != NULL)
227574462Salfred			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
22761558Srgrimes	} else if (grp->gr_type == GT_NET) {
22771558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
22781558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
22791558Srgrimes	}
22801558Srgrimes	free((caddr_t)grp);
22811558Srgrimes}
22821558Srgrimes
22831558Srgrimes#ifdef DEBUG
22841558Srgrimesvoid
22851558SrgrimesSYSLOG(int pri, const char *fmt, ...)
22861558Srgrimes{
22871558Srgrimes	va_list ap;
22881558Srgrimes
22891558Srgrimes	va_start(ap, fmt);
22901558Srgrimes	vfprintf(stderr, fmt, ap);
22911558Srgrimes	va_end(ap);
22921558Srgrimes}
22931558Srgrimes#endif /* DEBUG */
22941558Srgrimes
22951558Srgrimes/*
22961558Srgrimes * Check options for consistency.
22971558Srgrimes */
22981558Srgrimesint
22991558Srgrimescheck_options(dp)
23001558Srgrimes	struct dirlist *dp;
23011558Srgrimes{
23021558Srgrimes
23031558Srgrimes	if (dp == (struct dirlist *)NULL)
23041558Srgrimes	    return (1);
230583653Speter	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
230683653Speter	    syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
23071558Srgrimes	    return (1);
23081558Srgrimes	}
23091558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
231075801Siedowse		syslog(LOG_ERR, "-mask requires -network");
231175801Siedowse		return (1);
23121558Srgrimes	}
231375801Siedowse	if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
231475801Siedowse		syslog(LOG_ERR, "-network requires mask specification");
231575801Siedowse		return (1);
231675801Siedowse	}
231775801Siedowse	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
231875801Siedowse		syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
231975801Siedowse		return (1);
232075801Siedowse	}
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
235475801Siedowse/*
235575801Siedowse * Make a netmask according to the specified prefix length. The ss_family
235675801Siedowse * and other non-address fields must be initialised before calling this.
235775801Siedowse */
235875801Siedowseint
235975801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen)
236074462Salfred{
236175801Siedowse	u_char *p;
236275801Siedowse	int bits, i, len;
236374462Salfred
236475801Siedowse	if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
236575801Siedowse		return (-1);
2366103949Smike	if (bitlen > len * CHAR_BIT)
236775801Siedowse		return (-1);
236874462Salfred
236975801Siedowse	for (i = 0; i < len; i++) {
2370103949Smike		bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
237175801Siedowse		*p++ = (1 << bits) - 1;
237275801Siedowse		bitlen -= bits;
237374462Salfred	}
237475801Siedowse	return 0;
237574462Salfred}
237674462Salfred
237775801Siedowse/*
237875801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask
237975801Siedowse * is acceptable (i.e. of the form 1...10....0).
238075801Siedowse */
238175801Siedowseint
238275801Siedowsecheckmask(struct sockaddr *sa)
238374462Salfred{
238475801Siedowse	u_char *mask;
238575801Siedowse	int i, len;
238674462Salfred
238775801Siedowse	if ((mask = sa_rawaddr(sa, &len)) == NULL)
238875801Siedowse		return (-1);
238975801Siedowse
239075801Siedowse	for (i = 0; i < len; i++)
239175801Siedowse		if (mask[i] != 0xff)
239275801Siedowse			break;
239375801Siedowse	if (i < len) {
239475801Siedowse		if (~mask[i] & (u_char)(~mask[i] + 1))
239575801Siedowse			return (-1);
239675801Siedowse		i++;
239774462Salfred	}
239875801Siedowse	for (; i < len; i++)
239975801Siedowse		if (mask[i] != 0)
240075801Siedowse			return (-1);
240175801Siedowse	return (0);
240274462Salfred}
240374462Salfred
240475801Siedowse/*
240575801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if
240675801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'.
240775801Siedowse * If samask is NULL, perform a full comparision.
240875801Siedowse */
240975801Siedowseint
241075801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
241174462Salfred{
241275801Siedowse	unsigned char *p1, *p2, *mask;
241375801Siedowse	int len, i;
241474462Salfred
241575801Siedowse	if (sa1->sa_family != sa2->sa_family ||
241675801Siedowse	    (p1 = sa_rawaddr(sa1, &len)) == NULL ||
241775801Siedowse	    (p2 = sa_rawaddr(sa2, NULL)) == NULL)
241875801Siedowse		return (1);
241975801Siedowse
242075801Siedowse	switch (sa1->sa_family) {
242174462Salfred	case AF_INET6:
242275801Siedowse		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
242375801Siedowse		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
242475801Siedowse			return (1);
242574462Salfred		break;
242674462Salfred	}
242774462Salfred
242875801Siedowse	/* Simple binary comparison if no mask specified. */
242975801Siedowse	if (samask == NULL)
243075801Siedowse		return (memcmp(p1, p2, len));
243174462Salfred
243275801Siedowse	/* Set up the mask, and do a mask-based comparison. */
243375801Siedowse	if (sa1->sa_family != samask->sa_family ||
243475801Siedowse	    (mask = sa_rawaddr(samask, NULL)) == NULL)
243575801Siedowse		return (1);
243674462Salfred
243775801Siedowse	for (i = 0; i < len; i++)
243875801Siedowse		if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
243975801Siedowse			return (1);
244075801Siedowse	return (0);
244174462Salfred}
244274462Salfred
244375801Siedowse/*
244475801Siedowse * Return a pointer to the part of the sockaddr that contains the
244575801Siedowse * raw address, and set *nbytes to its length in bytes. Returns
244675801Siedowse * NULL if the address family is unknown.
244775801Siedowse */
244875801Siedowsevoid *
244975801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) {
245075801Siedowse	void *p;
245174462Salfred	int len;
245274462Salfred
245375801Siedowse	switch (sa->sa_family) {
245474462Salfred	case AF_INET:
245575801Siedowse		len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
245675801Siedowse		p = &((struct sockaddr_in *)sa)->sin_addr;
245774462Salfred		break;
245874462Salfred	case AF_INET6:
245975801Siedowse		len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
246075801Siedowse		p = &((struct sockaddr_in6 *)sa)->sin6_addr;
246174462Salfred		break;
246274462Salfred	default:
246375801Siedowse		p = NULL;
246475801Siedowse		len = 0;
246574462Salfred	}
246674462Salfred
246775801Siedowse	if (nbytes != NULL)
246875801Siedowse		*nbytes = len;
246975801Siedowse	return (p);
247074462Salfred}
247174462Salfred
247275754Siedowsevoid
247375754Siedowsehuphandler(int sig)
247475754Siedowse{
247575754Siedowse	got_sighup = 1;
247675754Siedowse}
247775754Siedowse
247874462Salfredvoid terminate(sig)
247974462Salfredint sig;
248074462Salfred{
248174462Salfred	close(mountdlockfd);
248274462Salfred	unlink(MOUNTDLOCK);
248374792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
248474792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
248574462Salfred	exit (0);
248674462Salfred}
2487