mountd.c revision 100336
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 100336 2002-07-18 20:52:17Z joerg $";
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>
781558Srgrimes#include <netdb.h>
791558Srgrimes#include <pwd.h>
801558Srgrimes#include <signal.h>
811558Srgrimes#include <stdio.h>
821558Srgrimes#include <stdlib.h>
831558Srgrimes#include <string.h>
841558Srgrimes#include <unistd.h>
851558Srgrimes#include "pathnames.h"
861558Srgrimes
871558Srgrimes#ifdef DEBUG
881558Srgrimes#include <stdarg.h>
891558Srgrimes#endif
901558Srgrimes
9174462Salfred#ifndef MOUNTDLOCK
9274462Salfred#define MOUNTDLOCK "/var/run/mountd.lock"
9374462Salfred#endif
9474462Salfred
951558Srgrimes/*
961558Srgrimes * Structures for keeping the mount list and export list
971558Srgrimes */
981558Srgrimesstruct mountlist {
991558Srgrimes	struct mountlist *ml_next;
1001558Srgrimes	char	ml_host[RPCMNT_NAMELEN+1];
1011558Srgrimes	char	ml_dirp[RPCMNT_PATHLEN+1];
1021558Srgrimes};
1031558Srgrimes
1041558Srgrimesstruct dirlist {
1051558Srgrimes	struct dirlist	*dp_left;
1061558Srgrimes	struct dirlist	*dp_right;
1071558Srgrimes	int		dp_flag;
1081558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
1091558Srgrimes	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1101558Srgrimes};
1111558Srgrimes/* dp_flag bits */
1121558Srgrimes#define	DP_DEFSET	0x1
1139336Sdfr#define DP_HOSTSET	0x2
1141558Srgrimes
1151558Srgrimesstruct exportlist {
1161558Srgrimes	struct exportlist *ex_next;
1171558Srgrimes	struct dirlist	*ex_dirl;
1181558Srgrimes	struct dirlist	*ex_defdir;
1191558Srgrimes	int		ex_flag;
1201558Srgrimes	fsid_t		ex_fs;
1211558Srgrimes	char		*ex_fsdir;
12227447Sdfr	char		*ex_indexfile;
1231558Srgrimes};
1241558Srgrimes/* ex_flag bits */
1251558Srgrimes#define	EX_LINKED	0x1
1261558Srgrimes
1271558Srgrimesstruct netmsk {
12874462Salfred	struct sockaddr_storage nt_net;
12975801Siedowse	struct sockaddr_storage nt_mask;
13042144Sdfr	char		*nt_name;
1311558Srgrimes};
1321558Srgrimes
1331558Srgrimesunion grouptypes {
13474462Salfred	struct addrinfo *gt_addrinfo;
1351558Srgrimes	struct netmsk	gt_net;
1361558Srgrimes};
1371558Srgrimes
1381558Srgrimesstruct grouplist {
1391558Srgrimes	int gr_type;
1401558Srgrimes	union grouptypes gr_ptr;
1411558Srgrimes	struct grouplist *gr_next;
1421558Srgrimes};
1431558Srgrimes/* Group types */
1441558Srgrimes#define	GT_NULL		0x0
1451558Srgrimes#define	GT_HOST		0x1
1461558Srgrimes#define	GT_NET		0x2
14775641Siedowse#define	GT_DEFAULT	0x3
1487401Swpaul#define GT_IGNORE	0x5
1491558Srgrimes
1501558Srgrimesstruct hostlist {
1519336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1521558Srgrimes	struct grouplist *ht_grp;
1531558Srgrimes	struct hostlist	 *ht_next;
1541558Srgrimes};
1551558Srgrimes
1569336Sdfrstruct fhreturn {
1579336Sdfr	int	fhr_flag;
1589336Sdfr	int	fhr_vers;
1599336Sdfr	nfsfh_t	fhr_fh;
1609336Sdfr};
1619336Sdfr
1621558Srgrimes/* Global defs */
16392882Simpchar	*add_expdir(struct dirlist **, char *, int);
16492882Simpvoid	add_dlist(struct dirlist **, struct dirlist *,
16592882Simp				struct grouplist *, int);
16692882Simpvoid	add_mlist(char *, char *);
16792882Simpint	check_dirpath(char *);
16892882Simpint	check_options(struct dirlist *);
16975801Siedowseint	checkmask(struct sockaddr *sa);
17092882Simpint	chk_host(struct dirlist *, struct sockaddr *, int *, int *);
17175635Siedowsevoid	del_mlist(char *hostp, char *dirp);
17292882Simpstruct dirlist *dirp_search(struct dirlist *, char *);
17392882Simpint	do_mount(struct exportlist *, struct grouplist *, int,
17492882Simp		struct xucred *, char *, int, struct statfs *);
17592882Simpint	do_opt(char **, char **, struct exportlist *, struct grouplist *,
17692882Simp				int *, int *, struct xucred *);
17792882Simpstruct	exportlist *ex_search(fsid_t *);
17892882Simpstruct	exportlist *get_exp(void);
17992882Simpvoid	free_dir(struct dirlist *);
18092882Simpvoid	free_exp(struct exportlist *);
18192882Simpvoid	free_grp(struct grouplist *);
18292882Simpvoid	free_host(struct hostlist *);
18392882Simpvoid	get_exportlist(void);
18492882Simpint	get_host(char *, struct grouplist *, struct grouplist *);
18592882Simpstruct hostlist *get_ht(void);
18692882Simpint	get_line(void);
18792882Simpvoid	get_mountlist(void);
18892882Simpint	get_net(char *, struct netmsk *, int);
18992882Simpvoid	getexp_err(struct exportlist *, struct grouplist *);
19092882Simpstruct grouplist *get_grp(void);
19192882Simpvoid	hang_dirp(struct dirlist *, struct grouplist *,
19292882Simp				struct exportlist *, int);
19375754Siedowsevoid	huphandler(int sig);
19475801Siedowseint	makemask(struct sockaddr_storage *ssp, int bitlen);
19592882Simpvoid	mntsrv(struct svc_req *, SVCXPRT *);
19692882Simpvoid	nextfield(char **, char **);
19792882Simpvoid	out_of_mem(void);
19892882Simpvoid	parsecred(char *, struct xucred *);
199100117Salfredint	put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
20075801Siedowsevoid	*sa_rawaddr(struct sockaddr *sa, int *nbytes);
20175801Siedowseint	sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
20275801Siedowse    struct sockaddr *samask);
20392882Simpint	scan_tree(struct dirlist *, struct sockaddr *);
20492882Simpstatic void usage(void);
20592882Simpint	xdr_dir(XDR *, char *);
20692882Simpint	xdr_explist(XDR *, caddr_t);
207100117Salfredint	xdr_explist_brief(XDR *, caddr_t);
20892882Simpint	xdr_fhs(XDR *, caddr_t);
20992882Simpint	xdr_mlist(XDR *, caddr_t);
21092882Simpvoid	terminate(int);
2111558Srgrimes
2121558Srgrimesstruct exportlist *exphead;
2131558Srgrimesstruct mountlist *mlhead;
2141558Srgrimesstruct grouplist *grphead;
2151558Srgrimeschar exname[MAXPATHLEN];
21672650Sgreenstruct xucred def_anon = {
21791354Sdd	XUCRED_VERSION,
21872650Sgreen	(uid_t)-2,
2191558Srgrimes	1,
22072650Sgreen	{ (gid_t)-2 },
22172650Sgreen	NULL
2221558Srgrimes};
22325087Sdfrint force_v2 = 0;
2249336Sdfrint resvport_only = 1;
2259336Sdfrint dir_only = 1;
22631705Sguidoint log = 0;
22775754Siedowseint got_sighup = 0;
22874462Salfred
2291558Srgrimesint opt_flags;
23074462Salfredstatic int have_v6 = 1;
23174462Salfred#ifdef NI_WITHSCOPEID
23274462Salfredstatic const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID;
23374462Salfred#else
23474462Salfredstatic const int ninumeric = NI_NUMERICHOST;
23574462Salfred#endif
23674462Salfred
23774462Salfredint mountdlockfd;
23875801Siedowse/* Bits for opt_flags above */
2391558Srgrimes#define	OP_MAPROOT	0x01
2401558Srgrimes#define	OP_MAPALL	0x02
24183653Speter/* 0x4 free */
2421558Srgrimes#define	OP_MASK		0x08
2431558Srgrimes#define	OP_NET		0x10
2441558Srgrimes#define	OP_ALLDIRS	0x40
24575801Siedowse#define	OP_HAVEMASK	0x80	/* A mask was specified or inferred. */
246100336Sjoerg#define	OP_QUIET	0x100
24774462Salfred#define OP_MASKLEN	0x200
2481558Srgrimes
2491558Srgrimes#ifdef DEBUG
2501558Srgrimesint debug = 1;
25192882Simpvoid	SYSLOG(int, const char *, ...) __printflike(2, 3);
2521558Srgrimes#define syslog SYSLOG
2531558Srgrimes#else
2541558Srgrimesint debug = 0;
2551558Srgrimes#endif
2561558Srgrimes
2571558Srgrimes/*
2581558Srgrimes * Mountd server for NFS mount protocol as described in:
2591558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2601558Srgrimes * The optional arguments are the exports file name
2611558Srgrimes * default: _PATH_EXPORTS
2621558Srgrimes * and "-n" to allow nonroot mount.
2631558Srgrimes */
2641558Srgrimesint
2651558Srgrimesmain(argc, argv)
2661558Srgrimes	int argc;
2671558Srgrimes	char **argv;
2681558Srgrimes{
26975754Siedowse	fd_set readfds;
27074462Salfred	SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
27174462Salfred	struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
27274462Salfred	int udpsock, tcpsock, udp6sock, tcp6sock;
27374462Salfred	int xcreated = 0, s;
27474462Salfred	int one = 1;
27596622Siedowse	int c;
2761558Srgrimes
27775635Siedowse	udp6conf = tcp6conf = NULL;
27875635Siedowse	udp6sock = tcp6sock = NULL;
27975635Siedowse
28074462Salfred	/* Check that another mountd isn't already running. */
28174462Salfred	if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
28274462Salfred		err(1, "%s", MOUNTDLOCK);
28374462Salfred
28474462Salfred	if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
28574462Salfred		errx(1, "another rpc.mountd is already running. Aborting");
28674462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
28774462Salfred	if (s < 0)
28874462Salfred		have_v6 = 0;
28974462Salfred	else
29074462Salfred		close(s);
29183687Speter	if (modfind("nfsserver") < 0) {
29283687Speter		/* Not present in kernel, try loading it */
29383687Speter		if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
29483687Speter			errx(1, "NFS server is not available or loadable");
2952999Swollman	}
2962999Swollman
29731665Sguido	while ((c = getopt(argc, argv, "2dlnr")) != -1)
2981558Srgrimes		switch (c) {
29925087Sdfr		case '2':
30025087Sdfr			force_v2 = 1;
30125087Sdfr			break;
3029336Sdfr		case 'n':
3039336Sdfr			resvport_only = 0;
3049336Sdfr			break;
3059336Sdfr		case 'r':
3069336Sdfr			dir_only = 0;
3079336Sdfr			break;
3088688Sphk		case 'd':
3098688Sphk			debug = debug ? 0 : 1;
3108688Sphk			break;
31131656Sguido		case 'l':
31231656Sguido			log = 1;
31331656Sguido			break;
3141558Srgrimes		default:
31537663Scharnier			usage();
3161558Srgrimes		};
3171558Srgrimes	argc -= optind;
3181558Srgrimes	argv += optind;
3191558Srgrimes	grphead = (struct grouplist *)NULL;
3201558Srgrimes	exphead = (struct exportlist *)NULL;
3211558Srgrimes	mlhead = (struct mountlist *)NULL;
3221558Srgrimes	if (argc == 1) {
3231558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
3241558Srgrimes		exname[MAXPATHLEN-1] = '\0';
3251558Srgrimes	} else
3261558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3271558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3281558Srgrimes	if (debug)
32937663Scharnier		warnx("getting export list");
3301558Srgrimes	get_exportlist();
3311558Srgrimes	if (debug)
33237663Scharnier		warnx("getting mount list");
3331558Srgrimes	get_mountlist();
3341558Srgrimes	if (debug)
33537663Scharnier		warnx("here we go");
3361558Srgrimes	if (debug == 0) {
3371558Srgrimes		daemon(0, 0);
3381558Srgrimes		signal(SIGINT, SIG_IGN);
3391558Srgrimes		signal(SIGQUIT, SIG_IGN);
3401558Srgrimes	}
34175754Siedowse	signal(SIGHUP, huphandler);
34274462Salfred	signal(SIGTERM, terminate);
3431558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3441558Srgrimes	  if (pidfile != NULL) {
3451558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3461558Srgrimes		fclose(pidfile);
3471558Srgrimes	  }
3481558Srgrimes	}
34974462Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
35074462Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
35174462Salfred	udpsock  = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
35274462Salfred	tcpsock  = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
35374791Salfred	udpconf  = getnetconfigent("udp");
35474791Salfred	tcpconf  = getnetconfigent("tcp");
35574791Salfred	if (!have_v6)
35674791Salfred		goto skip_v6;
35774462Salfred	udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
35874462Salfred	tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
35974462Salfred	/*
36074462Salfred	 * We're doing host-based access checks here, so don't allow
36174462Salfred	 * v4-in-v6 to confuse things. The kernel will disable it
36274462Salfred	 * by default on NFS sockets too.
36374462Salfred	 */
36474462Salfred	if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
36574462Salfred		IPV6_BINDV6ONLY, &one, sizeof one) < 0){
36674462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
36774462Salfred		exit(1);
36874462Salfred	}
36974462Salfred	if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
37074462Salfred		IPV6_BINDV6ONLY, &one, sizeof one) < 0){
37174462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
37274462Salfred		exit(1);
37374462Salfred	}
37474462Salfred	udp6conf = getnetconfigent("udp6");
37574462Salfred	tcp6conf = getnetconfigent("tcp6");
37674791Salfred
37774791Salfredskip_v6:
37824759Sguido	if (!resvport_only) {
37983687Speter		if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
38083687Speter		    &resvport_only, sizeof(resvport_only)) != 0 &&
38183687Speter		    errno != ENOENT) {
38224759Sguido			syslog(LOG_ERR, "sysctl: %m");
38324759Sguido			exit(1);
38424759Sguido		}
38524330Sguido	}
38674462Salfred	if (udpsock != -1 && udpconf != NULL) {
38774462Salfred		bindresvport(udpsock, NULL);
38874462Salfred		udptransp = svc_dg_create(udpsock, 0, 0);
38974462Salfred		if (udptransp != NULL) {
39074462Salfred			if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
39174462Salfred			    mntsrv, udpconf))
39274462Salfred				syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service");
39374462Salfred			else
39474462Salfred				xcreated++;
39574462Salfred			if (!force_v2) {
39674462Salfred				if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
39774462Salfred				    mntsrv, udpconf))
39874462Salfred					syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service");
39974462Salfred				else
40074462Salfred					xcreated++;
40174462Salfred			}
40274462Salfred		} else
40374462Salfred			syslog(LOG_WARNING, "can't create UDP services");
40474462Salfred
40574462Salfred	}
40674462Salfred	if (tcpsock != -1 && tcpconf != NULL) {
40774462Salfred		bindresvport(tcpsock, NULL);
40874462Salfred		listen(tcpsock, SOMAXCONN);
40974462Salfred		tcptransp = svc_vc_create(tcpsock, 0, 0);
41074462Salfred		if (tcptransp != NULL) {
41174462Salfred			if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
41274462Salfred			    mntsrv, tcpconf))
41374462Salfred				syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service");
41474462Salfred			else
41574462Salfred				xcreated++;
41674462Salfred			if (!force_v2) {
41774462Salfred				if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
41874462Salfred				    mntsrv, tcpconf))
41974462Salfred					syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service");
42074462Salfred				else
42174462Salfred					xcreated++;
42274462Salfred			}
42374462Salfred		} else
42474462Salfred			syslog(LOG_WARNING, "can't create TCP service");
42574462Salfred
42674462Salfred	}
42774791Salfred	if (have_v6 && udp6sock != -1 && udp6conf != NULL) {
42874462Salfred		bindresvport(udp6sock, NULL);
42974462Salfred		udp6transp = svc_dg_create(udp6sock, 0, 0);
43074462Salfred		if (udp6transp != NULL) {
43174462Salfred			if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
43274462Salfred			    mntsrv, udp6conf))
43374462Salfred				syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service");
43474462Salfred			else
43574462Salfred				xcreated++;
43674462Salfred			if (!force_v2) {
43774462Salfred				if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
43874462Salfred				    mntsrv, udp6conf))
43974462Salfred					syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service");
44074462Salfred				else
44174462Salfred					xcreated++;
44274462Salfred			}
44374462Salfred		} else
44474462Salfred			syslog(LOG_WARNING, "can't create UDP6 service");
44574462Salfred
44674462Salfred	}
44774791Salfred	if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) {
44874462Salfred		bindresvport(tcp6sock, NULL);
44974462Salfred		listen(tcp6sock, SOMAXCONN);
45074462Salfred		tcp6transp = svc_vc_create(tcp6sock, 0, 0);
45174462Salfred		if (tcp6transp != NULL) {
45274462Salfred			if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
45374462Salfred			    mntsrv, tcp6conf))
45474462Salfred				syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service");
45574462Salfred			else
45674462Salfred				xcreated++;
45774462Salfred			if (!force_v2) {
45874462Salfred				if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
45974462Salfred				    mntsrv, tcp6conf))
46074462Salfred					syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service");
46174462Salfred					else
46274462Salfred						xcreated++;
46374462Salfred				}
46474462Salfred		} else
46574462Salfred			syslog(LOG_WARNING, "can't create TCP6 service");
46674462Salfred
46774462Salfred	}
46874462Salfred	if (xcreated == 0) {
46974462Salfred		syslog(LOG_ERR, "could not create any services");
4701558Srgrimes		exit(1);
4711558Srgrimes	}
47275754Siedowse
47375754Siedowse	/* Expand svc_run() here so that we can call get_exportlist(). */
47475754Siedowse	for (;;) {
47575754Siedowse		if (got_sighup) {
47675754Siedowse			get_exportlist();
47775754Siedowse			got_sighup = 0;
47875754Siedowse		}
47975754Siedowse		readfds = svc_fdset;
48075754Siedowse		switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
48175754Siedowse		case -1:
48275754Siedowse			if (errno == EINTR)
48375754Siedowse                                continue;
48475754Siedowse			syslog(LOG_ERR, "mountd died: select: %m");
48575754Siedowse			exit(1);
48675754Siedowse		case 0:
48775754Siedowse			continue;
48875754Siedowse		default:
48975754Siedowse			svc_getreqset(&readfds);
49075754Siedowse		}
49175754Siedowse	}
4921558Srgrimes}
4931558Srgrimes
49437663Scharnierstatic void
49537663Scharnierusage()
49637663Scharnier{
49737663Scharnier	fprintf(stderr,
49837663Scharnier		"usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
49937663Scharnier	exit(1);
50037663Scharnier}
50137663Scharnier
5021558Srgrimes/*
5031558Srgrimes * The mount rpc service
5041558Srgrimes */
5051558Srgrimesvoid
5061558Srgrimesmntsrv(rqstp, transp)
5071558Srgrimes	struct svc_req *rqstp;
5081558Srgrimes	SVCXPRT *transp;
5091558Srgrimes{
5101558Srgrimes	struct exportlist *ep;
5111558Srgrimes	struct dirlist *dp;
5129336Sdfr	struct fhreturn fhr;
5131558Srgrimes	struct stat stb;
5141558Srgrimes	struct statfs fsb;
51574462Salfred	struct addrinfo *ai;
51674462Salfred	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
51774462Salfred	int lookup_failed = 1;
51874462Salfred	struct sockaddr *saddr;
5199336Sdfr	u_short sport;
52023681Speter	char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
52128911Sguido	int bad = 0, defset, hostset;
5229336Sdfr	sigset_t sighup_mask;
5231558Srgrimes
5249336Sdfr	sigemptyset(&sighup_mask);
5259336Sdfr	sigaddset(&sighup_mask, SIGHUP);
52674462Salfred	saddr = svc_getrpccaller(transp)->buf;
52774462Salfred	switch (saddr->sa_family) {
52874462Salfred	case AF_INET6:
52975635Siedowse		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
53074462Salfred		break;
53174462Salfred	case AF_INET:
53275635Siedowse		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
53374462Salfred		break;
53474462Salfred	default:
53574462Salfred		syslog(LOG_ERR, "request from unknown address family");
53674462Salfred		return;
53774462Salfred	}
53874462Salfred	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
53974462Salfred	    NULL, 0, 0);
54074462Salfred	getnameinfo(saddr, saddr->sa_len, numerichost,
54174462Salfred	    sizeof numerichost, NULL, 0, NI_NUMERICHOST);
54274462Salfred	ai = NULL;
5431558Srgrimes	switch (rqstp->rq_proc) {
5441558Srgrimes	case NULLPROC:
545100117Salfred		if (!svc_sendreply(transp, xdr_void, NULL))
54637663Scharnier			syslog(LOG_ERR, "can't send reply");
5471558Srgrimes		return;
5481558Srgrimes	case RPCMNT_MOUNT:
5499336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
55031656Sguido			syslog(LOG_NOTICE,
55131656Sguido			    "mount request from %s from unprivileged port",
55274462Salfred			    numerichost);
5531558Srgrimes			svcerr_weakauth(transp);
5541558Srgrimes			return;
5551558Srgrimes		}
5561558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
55731656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
55874462Salfred			    numerichost);
5591558Srgrimes			svcerr_decode(transp);
5601558Srgrimes			return;
5611558Srgrimes		}
5621558Srgrimes
5631558Srgrimes		/*
5641558Srgrimes		 * Get the real pathname and make sure it is a directory
5659336Sdfr		 * or a regular file if the -r option was specified
5669336Sdfr		 * and it exists.
5671558Srgrimes		 */
56851968Salfred		if (realpath(rpcpath, dirpath) == NULL ||
5691558Srgrimes		    stat(dirpath, &stb) < 0 ||
5709336Sdfr		    (!S_ISDIR(stb.st_mode) &&
57174462Salfred		    (dir_only || !S_ISREG(stb.st_mode))) ||
5721558Srgrimes		    statfs(dirpath, &fsb) < 0) {
5731558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
57431656Sguido			syslog(LOG_NOTICE,
57537663Scharnier			    "mount request from %s for non existent path %s",
57674462Salfred			    numerichost, dirpath);
5771558Srgrimes			if (debug)
57837663Scharnier				warnx("stat failed on %s", dirpath);
57928911Sguido			bad = ENOENT;	/* We will send error reply later */
5801558Srgrimes		}
5811558Srgrimes
5821558Srgrimes		/* Check in the exports list */
5839336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
5841558Srgrimes		ep = ex_search(&fsb.f_fsid);
5859336Sdfr		hostset = defset = 0;
5869336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
5871558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
58874462Salfred		      chk_host(dp, saddr, &defset, &hostset)) ||
58974462Salfred		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
59074462Salfred		     scan_tree(ep->ex_dirl, saddr) == 0))) {
59128911Sguido			if (bad) {
59228911Sguido				if (!svc_sendreply(transp, xdr_long,
59328911Sguido				    (caddr_t)&bad))
59437663Scharnier					syslog(LOG_ERR, "can't send reply");
59528911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
59628911Sguido				return;
59728911Sguido			}
5989336Sdfr			if (hostset & DP_HOSTSET)
5999336Sdfr				fhr.fhr_flag = hostset;
6009336Sdfr			else
6019336Sdfr				fhr.fhr_flag = defset;
6029336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
6031558Srgrimes			/* Get the file handle */
60423681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
6059336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
6061558Srgrimes				bad = errno;
60737663Scharnier				syslog(LOG_ERR, "can't get fh for %s", dirpath);
6081558Srgrimes				if (!svc_sendreply(transp, xdr_long,
6091558Srgrimes				    (caddr_t)&bad))
61037663Scharnier					syslog(LOG_ERR, "can't send reply");
6119336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6121558Srgrimes				return;
6131558Srgrimes			}
6149336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
61537663Scharnier				syslog(LOG_ERR, "can't send reply");
61674462Salfred			if (!lookup_failed)
61774462Salfred				add_mlist(host, dirpath);
6181558Srgrimes			else
61974462Salfred				add_mlist(numerichost, dirpath);
6201558Srgrimes			if (debug)
62137663Scharnier				warnx("mount successful");
62231656Sguido			if (log)
62331656Sguido				syslog(LOG_NOTICE,
62431656Sguido				    "mount request succeeded from %s for %s",
62574462Salfred				    numerichost, dirpath);
62631656Sguido		} else {
6271558Srgrimes			bad = EACCES;
62831656Sguido			syslog(LOG_NOTICE,
62931656Sguido			    "mount request denied from %s for %s",
63074462Salfred			    numerichost, dirpath);
63131656Sguido		}
63228911Sguido
63328911Sguido		if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
63437663Scharnier			syslog(LOG_ERR, "can't send reply");
6359336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6361558Srgrimes		return;
6371558Srgrimes	case RPCMNT_DUMP:
6381558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
63937663Scharnier			syslog(LOG_ERR, "can't send reply");
64031656Sguido		else if (log)
64131656Sguido			syslog(LOG_NOTICE,
64231656Sguido			    "dump request succeeded from %s",
64374462Salfred			    numerichost);
6441558Srgrimes		return;
6451558Srgrimes	case RPCMNT_UMOUNT:
6469336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
64731656Sguido			syslog(LOG_NOTICE,
64831656Sguido			    "umount request from %s from unprivileged port",
64974462Salfred			    numerichost);
6501558Srgrimes			svcerr_weakauth(transp);
6511558Srgrimes			return;
6521558Srgrimes		}
65351968Salfred		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
65431656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
65574462Salfred			    numerichost);
6561558Srgrimes			svcerr_decode(transp);
6571558Srgrimes			return;
6581558Srgrimes		}
65951968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
66051968Salfred			syslog(LOG_NOTICE, "umount request from %s "
66151968Salfred			    "for non existent path %s",
66274462Salfred			    numerichost, dirpath);
66351968Salfred		}
6641558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
66537663Scharnier			syslog(LOG_ERR, "can't send reply");
66674462Salfred		if (!lookup_failed)
66775635Siedowse			del_mlist(host, dirpath);
66875635Siedowse		del_mlist(numerichost, dirpath);
66931656Sguido		if (log)
67031656Sguido			syslog(LOG_NOTICE,
67131656Sguido			    "umount request succeeded from %s for %s",
67274462Salfred			    numerichost, dirpath);
6731558Srgrimes		return;
6741558Srgrimes	case RPCMNT_UMNTALL:
6759336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
67631656Sguido			syslog(LOG_NOTICE,
67731656Sguido			    "umountall request from %s from unprivileged port",
67874462Salfred			    numerichost);
6791558Srgrimes			svcerr_weakauth(transp);
6801558Srgrimes			return;
6811558Srgrimes		}
6821558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
68337663Scharnier			syslog(LOG_ERR, "can't send reply");
68474462Salfred		if (!lookup_failed)
68575635Siedowse			del_mlist(host, NULL);
68675635Siedowse		del_mlist(numerichost, NULL);
68731656Sguido		if (log)
68831656Sguido			syslog(LOG_NOTICE,
68931656Sguido			    "umountall request succeeded from %s",
69074462Salfred			    numerichost);
6911558Srgrimes		return;
6921558Srgrimes	case RPCMNT_EXPORT:
6931558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
694100117Salfred			if (!svc_sendreply(transp, xdr_explist_brief, (caddr_t)NULL))
695100117Salfred				syslog(LOG_ERR, "can't send reply");
69631656Sguido		if (log)
69731656Sguido			syslog(LOG_NOTICE,
69831656Sguido			    "export request succeeded from %s",
69974462Salfred			    numerichost);
7001558Srgrimes		return;
7011558Srgrimes	default:
7021558Srgrimes		svcerr_noproc(transp);
7031558Srgrimes		return;
7041558Srgrimes	}
7051558Srgrimes}
7061558Srgrimes
7071558Srgrimes/*
7081558Srgrimes * Xdr conversion for a dirpath string
7091558Srgrimes */
7101558Srgrimesint
7111558Srgrimesxdr_dir(xdrsp, dirp)
7121558Srgrimes	XDR *xdrsp;
7131558Srgrimes	char *dirp;
7141558Srgrimes{
7151558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
7161558Srgrimes}
7171558Srgrimes
7181558Srgrimes/*
7199336Sdfr * Xdr routine to generate file handle reply
7201558Srgrimes */
7211558Srgrimesint
7229336Sdfrxdr_fhs(xdrsp, cp)
7231558Srgrimes	XDR *xdrsp;
7249336Sdfr	caddr_t cp;
7251558Srgrimes{
72692806Sobrien	struct fhreturn *fhrp = (struct fhreturn *)cp;
7279336Sdfr	u_long ok = 0, len, auth;
7281558Srgrimes
7291558Srgrimes	if (!xdr_long(xdrsp, &ok))
7301558Srgrimes		return (0);
7319336Sdfr	switch (fhrp->fhr_vers) {
7329336Sdfr	case 1:
7339336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
7349336Sdfr	case 3:
7359336Sdfr		len = NFSX_V3FH;
7369336Sdfr		if (!xdr_long(xdrsp, &len))
7379336Sdfr			return (0);
7389336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
7399336Sdfr			return (0);
74083653Speter		auth = RPCAUTH_UNIX;
7419336Sdfr		len = 1;
7429336Sdfr		if (!xdr_long(xdrsp, &len))
7439336Sdfr			return (0);
7449336Sdfr		return (xdr_long(xdrsp, &auth));
7459336Sdfr	};
7469336Sdfr	return (0);
7471558Srgrimes}
7481558Srgrimes
7491558Srgrimesint
7501558Srgrimesxdr_mlist(xdrsp, cp)
7511558Srgrimes	XDR *xdrsp;
7521558Srgrimes	caddr_t cp;
7531558Srgrimes{
7541558Srgrimes	struct mountlist *mlp;
7551558Srgrimes	int true = 1;
7561558Srgrimes	int false = 0;
7571558Srgrimes	char *strp;
7581558Srgrimes
7591558Srgrimes	mlp = mlhead;
7601558Srgrimes	while (mlp) {
7611558Srgrimes		if (!xdr_bool(xdrsp, &true))
7621558Srgrimes			return (0);
7631558Srgrimes		strp = &mlp->ml_host[0];
7641558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
7651558Srgrimes			return (0);
7661558Srgrimes		strp = &mlp->ml_dirp[0];
7671558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
7681558Srgrimes			return (0);
7691558Srgrimes		mlp = mlp->ml_next;
7701558Srgrimes	}
7711558Srgrimes	if (!xdr_bool(xdrsp, &false))
7721558Srgrimes		return (0);
7731558Srgrimes	return (1);
7741558Srgrimes}
7751558Srgrimes
7761558Srgrimes/*
7771558Srgrimes * Xdr conversion for export list
7781558Srgrimes */
7791558Srgrimesint
780100117Salfredxdr_explist_common(xdrsp, cp, brief)
7811558Srgrimes	XDR *xdrsp;
7821558Srgrimes	caddr_t cp;
783100117Salfred	int brief;
7841558Srgrimes{
7851558Srgrimes	struct exportlist *ep;
7861558Srgrimes	int false = 0;
7879336Sdfr	int putdef;
7889336Sdfr	sigset_t sighup_mask;
7891558Srgrimes
7909336Sdfr	sigemptyset(&sighup_mask);
7919336Sdfr	sigaddset(&sighup_mask, SIGHUP);
7929336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
7931558Srgrimes	ep = exphead;
7941558Srgrimes	while (ep) {
7951558Srgrimes		putdef = 0;
796100117Salfred		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
797100117Salfred			       &putdef, brief))
7981558Srgrimes			goto errout;
7991558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
8001558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
801100117Salfred			&putdef, brief))
8021558Srgrimes			goto errout;
8031558Srgrimes		ep = ep->ex_next;
8041558Srgrimes	}
8059336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8061558Srgrimes	if (!xdr_bool(xdrsp, &false))
8071558Srgrimes		return (0);
8081558Srgrimes	return (1);
8091558Srgrimeserrout:
8109336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8111558Srgrimes	return (0);
8121558Srgrimes}
8131558Srgrimes
8141558Srgrimes/*
8151558Srgrimes * Called from xdr_explist() to traverse the tree and export the
8161558Srgrimes * directory paths.
8171558Srgrimes */
8181558Srgrimesint
819100117Salfredput_exlist(dp, xdrsp, adp, putdefp, brief)
8201558Srgrimes	struct dirlist *dp;
8211558Srgrimes	XDR *xdrsp;
8221558Srgrimes	struct dirlist *adp;
8231558Srgrimes	int *putdefp;
824100117Salfred	int brief;
8251558Srgrimes{
8261558Srgrimes	struct grouplist *grp;
8271558Srgrimes	struct hostlist *hp;
8281558Srgrimes	int true = 1;
8291558Srgrimes	int false = 0;
8301558Srgrimes	int gotalldir = 0;
8311558Srgrimes	char *strp;
8321558Srgrimes
8331558Srgrimes	if (dp) {
834100117Salfred		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
8351558Srgrimes			return (1);
8361558Srgrimes		if (!xdr_bool(xdrsp, &true))
8371558Srgrimes			return (1);
8381558Srgrimes		strp = dp->dp_dirp;
8391558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
8401558Srgrimes			return (1);
8411558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
8421558Srgrimes			gotalldir = 1;
8431558Srgrimes			*putdefp = 1;
8441558Srgrimes		}
845100117Salfred		if (brief) {
846100117Salfred			if (!xdr_bool(xdrsp, &true))
847100117Salfred				return (1);
848100117Salfred			strp = "(...)";
849100117Salfred			if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
850100117Salfred				return (1);
851100117Salfred		} else if ((dp->dp_flag & DP_DEFSET) == 0 &&
8521558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
8531558Srgrimes			hp = dp->dp_hosts;
8541558Srgrimes			while (hp) {
8551558Srgrimes				grp = hp->ht_grp;
8561558Srgrimes				if (grp->gr_type == GT_HOST) {
8571558Srgrimes					if (!xdr_bool(xdrsp, &true))
8581558Srgrimes						return (1);
85974462Salfred					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
8608871Srgrimes					if (!xdr_string(xdrsp, &strp,
8611558Srgrimes					    RPCMNT_NAMELEN))
8621558Srgrimes						return (1);
8631558Srgrimes				} else if (grp->gr_type == GT_NET) {
8641558Srgrimes					if (!xdr_bool(xdrsp, &true))
8651558Srgrimes						return (1);
8661558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
8678871Srgrimes					if (!xdr_string(xdrsp, &strp,
8681558Srgrimes					    RPCMNT_NAMELEN))
8691558Srgrimes						return (1);
8701558Srgrimes				}
8711558Srgrimes				hp = hp->ht_next;
8721558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
8731558Srgrimes					hp = adp->dp_hosts;
8741558Srgrimes					gotalldir = 0;
8751558Srgrimes				}
8761558Srgrimes			}
8771558Srgrimes		}
8781558Srgrimes		if (!xdr_bool(xdrsp, &false))
8791558Srgrimes			return (1);
880100117Salfred		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
8811558Srgrimes			return (1);
8821558Srgrimes	}
8831558Srgrimes	return (0);
8841558Srgrimes}
8851558Srgrimes
886100117Salfredint
887100117Salfredxdr_explist(xdrsp, cp)
888100117Salfred	XDR *xdrsp;
889100117Salfred	caddr_t cp;
890100117Salfred{
891100117Salfred
892100117Salfred	return xdr_explist_common(xdrsp, cp, 0);
893100117Salfred}
894100117Salfred
895100117Salfredint
896100117Salfredxdr_explist_brief(xdrsp, cp)
897100117Salfred	XDR *xdrsp;
898100117Salfred	caddr_t cp;
899100117Salfred{
900100117Salfred
901100117Salfred	return xdr_explist_common(xdrsp, cp, 1);
902100117Salfred}
903100117Salfred
90496622Siedowsechar *line;
90596622Siedowseint linesize;
9061558SrgrimesFILE *exp_file;
9071558Srgrimes
9081558Srgrimes/*
9091558Srgrimes * Get the export list
9101558Srgrimes */
9111558Srgrimesvoid
9121558Srgrimesget_exportlist()
9131558Srgrimes{
9141558Srgrimes	struct exportlist *ep, *ep2;
9151558Srgrimes	struct grouplist *grp, *tgrp;
9161558Srgrimes	struct exportlist **epp;
9171558Srgrimes	struct dirlist *dirhead;
9181558Srgrimes	struct statfs fsb, *fsp;
91972650Sgreen	struct xucred anon;
9201558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
9211558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
9221558Srgrimes
92351968Salfred	dirp = NULL;
92451968Salfred	dirplen = 0;
92551968Salfred
9261558Srgrimes	/*
9271558Srgrimes	 * First, get rid of the old list
9281558Srgrimes	 */
9291558Srgrimes	ep = exphead;
9301558Srgrimes	while (ep) {
9311558Srgrimes		ep2 = ep;
9321558Srgrimes		ep = ep->ex_next;
9331558Srgrimes		free_exp(ep2);
9341558Srgrimes	}
9351558Srgrimes	exphead = (struct exportlist *)NULL;
9361558Srgrimes
9371558Srgrimes	grp = grphead;
9381558Srgrimes	while (grp) {
9391558Srgrimes		tgrp = grp;
9401558Srgrimes		grp = grp->gr_next;
9411558Srgrimes		free_grp(tgrp);
9421558Srgrimes	}
9431558Srgrimes	grphead = (struct grouplist *)NULL;
9441558Srgrimes
9451558Srgrimes	/*
9461558Srgrimes	 * And delete exports that are in the kernel for all local
94796707Strhodes	 * filesystems.
94896707Strhodes	 * XXX: Should know how to handle all local exportable filesystems
94923681Speter	 *      instead of just "ufs".
9501558Srgrimes	 */
9511558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
9521558Srgrimes	for (i = 0; i < num; i++) {
9531558Srgrimes		union {
9541558Srgrimes			struct ufs_args ua;
9551558Srgrimes			struct iso_args ia;
9569336Sdfr			struct msdosfs_args da;
95754093Ssemenu			struct ntfs_args na;
9581558Srgrimes		} targs;
9591558Srgrimes
96077435Sphk		if (!strcmp(fsp->f_fstypename, "ufs") ||
96177577Sru		    !strcmp(fsp->f_fstypename, "msdosfs") ||
96254093Ssemenu		    !strcmp(fsp->f_fstypename, "ntfs") ||
96323681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
9649336Sdfr			targs.ua.fspec = NULL;
9659336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
9669336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
96777405Siedowse			    fsp->f_flags | MNT_UPDATE, (caddr_t)&targs) < 0 &&
96877405Siedowse			    errno != ENOENT)
96977405Siedowse				syslog(LOG_ERR,
97077405Siedowse				    "can't delete exports for %s: %m",
97174462Salfred				    fsp->f_mntonname);
9721558Srgrimes		}
9731558Srgrimes		fsp++;
9741558Srgrimes	}
9751558Srgrimes
9761558Srgrimes	/*
9771558Srgrimes	 * Read in the exports file and build the list, calling
9781558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
9791558Srgrimes	 */
9801558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
98137663Scharnier		syslog(LOG_ERR, "can't open %s", exname);
9821558Srgrimes		exit(2);
9831558Srgrimes	}
9841558Srgrimes	dirhead = (struct dirlist *)NULL;
9851558Srgrimes	while (get_line()) {
9861558Srgrimes		if (debug)
98737663Scharnier			warnx("got line %s", line);
9881558Srgrimes		cp = line;
9891558Srgrimes		nextfield(&cp, &endcp);
9901558Srgrimes		if (*cp == '#')
9911558Srgrimes			goto nextline;
9921558Srgrimes
9931558Srgrimes		/*
9941558Srgrimes		 * Set defaults.
9951558Srgrimes		 */
9961558Srgrimes		has_host = FALSE;
9971558Srgrimes		anon = def_anon;
9981558Srgrimes		exflags = MNT_EXPORTED;
9991558Srgrimes		got_nondir = 0;
10001558Srgrimes		opt_flags = 0;
10011558Srgrimes		ep = (struct exportlist *)NULL;
10021558Srgrimes
10031558Srgrimes		/*
10041558Srgrimes		 * Create new exports list entry
10051558Srgrimes		 */
10061558Srgrimes		len = endcp-cp;
10071558Srgrimes		tgrp = grp = get_grp();
10081558Srgrimes		while (len > 0) {
10091558Srgrimes			if (len > RPCMNT_NAMELEN) {
10101558Srgrimes			    getexp_err(ep, tgrp);
10111558Srgrimes			    goto nextline;
10121558Srgrimes			}
10131558Srgrimes			if (*cp == '-') {
10141558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10151558Srgrimes				getexp_err(ep, tgrp);
10161558Srgrimes				goto nextline;
10171558Srgrimes			    }
10181558Srgrimes			    if (debug)
101937663Scharnier				warnx("doing opt %s", cp);
10201558Srgrimes			    got_nondir = 1;
10211558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
10221558Srgrimes				&exflags, &anon)) {
10231558Srgrimes				getexp_err(ep, tgrp);
10241558Srgrimes				goto nextline;
10251558Srgrimes			    }
10261558Srgrimes			} else if (*cp == '/') {
10271558Srgrimes			    savedc = *endcp;
10281558Srgrimes			    *endcp = '\0';
10291558Srgrimes			    if (check_dirpath(cp) &&
10301558Srgrimes				statfs(cp, &fsb) >= 0) {
10311558Srgrimes				if (got_nondir) {
103237663Scharnier				    syslog(LOG_ERR, "dirs must be first");
10331558Srgrimes				    getexp_err(ep, tgrp);
10341558Srgrimes				    goto nextline;
10351558Srgrimes				}
10361558Srgrimes				if (ep) {
10371558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
10381558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
10391558Srgrimes					getexp_err(ep, tgrp);
10401558Srgrimes					goto nextline;
10411558Srgrimes				    }
10421558Srgrimes				} else {
10431558Srgrimes				    /*
10441558Srgrimes				     * See if this directory is already
10451558Srgrimes				     * in the list.
10461558Srgrimes				     */
10471558Srgrimes				    ep = ex_search(&fsb.f_fsid);
10481558Srgrimes				    if (ep == (struct exportlist *)NULL) {
10491558Srgrimes					ep = get_exp();
10501558Srgrimes					ep->ex_fs = fsb.f_fsid;
10511558Srgrimes					ep->ex_fsdir = (char *)
10521558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
10531558Srgrimes					if (ep->ex_fsdir)
10541558Srgrimes					    strcpy(ep->ex_fsdir,
10551558Srgrimes						fsb.f_mntonname);
10561558Srgrimes					else
10571558Srgrimes					    out_of_mem();
10581558Srgrimes					if (debug)
105974462Salfred						warnx("making new ep fs=0x%x,0x%x",
106074462Salfred						    fsb.f_fsid.val[0],
106174462Salfred						    fsb.f_fsid.val[1]);
10621558Srgrimes				    } else if (debug)
106337663Scharnier					warnx("found ep fs=0x%x,0x%x",
10641558Srgrimes					    fsb.f_fsid.val[0],
10651558Srgrimes					    fsb.f_fsid.val[1]);
10661558Srgrimes				}
10671558Srgrimes
10681558Srgrimes				/*
10691558Srgrimes				 * Add dirpath to export mount point.
10701558Srgrimes				 */
10711558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
10721558Srgrimes				dirplen = len;
10731558Srgrimes			    } else {
10741558Srgrimes				getexp_err(ep, tgrp);
10751558Srgrimes				goto nextline;
10761558Srgrimes			    }
10771558Srgrimes			    *endcp = savedc;
10781558Srgrimes			} else {
10791558Srgrimes			    savedc = *endcp;
10801558Srgrimes			    *endcp = '\0';
10811558Srgrimes			    got_nondir = 1;
10821558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10831558Srgrimes				getexp_err(ep, tgrp);
10841558Srgrimes				goto nextline;
10851558Srgrimes			    }
10861558Srgrimes
10871558Srgrimes			    /*
10881558Srgrimes			     * Get the host or netgroup.
10891558Srgrimes			     */
10901558Srgrimes			    setnetgrent(cp);
10911558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
10921558Srgrimes			    do {
10931558Srgrimes				if (has_host) {
10941558Srgrimes				    grp->gr_next = get_grp();
10951558Srgrimes				    grp = grp->gr_next;
10961558Srgrimes				}
10971558Srgrimes				if (netgrp) {
109837003Sjoerg				    if (hst == 0) {
109937663Scharnier					syslog(LOG_ERR,
110037663Scharnier				"null hostname in netgroup %s, skipping", cp);
110137004Sjoerg					grp->gr_type = GT_IGNORE;
110237003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
110337663Scharnier					syslog(LOG_ERR,
110437663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
110529317Sjlemon					grp->gr_type = GT_IGNORE;
11061558Srgrimes				    }
11077401Swpaul				} else if (get_host(cp, grp, tgrp)) {
110837663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
110929317Sjlemon				    grp->gr_type = GT_IGNORE;
11101558Srgrimes				}
11111558Srgrimes				has_host = TRUE;
11121558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
11131558Srgrimes			    endnetgrent();
11141558Srgrimes			    *endcp = savedc;
11151558Srgrimes			}
11161558Srgrimes			cp = endcp;
11171558Srgrimes			nextfield(&cp, &endcp);
11181558Srgrimes			len = endcp - cp;
11191558Srgrimes		}
11201558Srgrimes		if (check_options(dirhead)) {
11211558Srgrimes			getexp_err(ep, tgrp);
11221558Srgrimes			goto nextline;
11231558Srgrimes		}
11241558Srgrimes		if (!has_host) {
112575641Siedowse			grp->gr_type = GT_DEFAULT;
11261558Srgrimes			if (debug)
112737663Scharnier				warnx("adding a default entry");
11281558Srgrimes
11291558Srgrimes		/*
11301558Srgrimes		 * Don't allow a network export coincide with a list of
11311558Srgrimes		 * host(s) on the same line.
11321558Srgrimes		 */
11331558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
113475801Siedowse			syslog(LOG_ERR, "network/host conflict");
11351558Srgrimes			getexp_err(ep, tgrp);
11361558Srgrimes			goto nextline;
113729317Sjlemon
113874462Salfred		/*
113974462Salfred		 * If an export list was specified on this line, make sure
114029317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
114129317Sjlemon		 */
114229317Sjlemon		} else {
114329317Sjlemon			grp = tgrp;
114474462Salfred			while (grp && grp->gr_type == GT_IGNORE)
114529317Sjlemon				grp = grp->gr_next;
114629317Sjlemon			if (! grp) {
114729317Sjlemon			    getexp_err(ep, tgrp);
114829317Sjlemon			    goto nextline;
114929317Sjlemon			}
11501558Srgrimes		}
11511558Srgrimes
11521558Srgrimes		/*
11531558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
11541558Srgrimes		 * After loop, tgrp points to the start of the list and
11551558Srgrimes		 * grp points to the last entry in the list.
11561558Srgrimes		 */
11571558Srgrimes		grp = tgrp;
11581558Srgrimes		do {
115975635Siedowse			if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
116075635Siedowse			    &fsb)) {
116175635Siedowse				getexp_err(ep, tgrp);
116275635Siedowse				goto nextline;
116375635Siedowse			}
11641558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
11651558Srgrimes
11661558Srgrimes		/*
11671558Srgrimes		 * Success. Update the data structures.
11681558Srgrimes		 */
11691558Srgrimes		if (has_host) {
11709336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
11711558Srgrimes			grp->gr_next = grphead;
11721558Srgrimes			grphead = tgrp;
11731558Srgrimes		} else {
11741558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
11759336Sdfr				opt_flags);
11761558Srgrimes			free_grp(grp);
11771558Srgrimes		}
11781558Srgrimes		dirhead = (struct dirlist *)NULL;
11791558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
11801558Srgrimes			ep2 = exphead;
11811558Srgrimes			epp = &exphead;
11821558Srgrimes
11831558Srgrimes			/*
11841558Srgrimes			 * Insert in the list in alphabetical order.
11851558Srgrimes			 */
11861558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
11871558Srgrimes				epp = &ep2->ex_next;
11881558Srgrimes				ep2 = ep2->ex_next;
11891558Srgrimes			}
11901558Srgrimes			if (ep2)
11911558Srgrimes				ep->ex_next = ep2;
11921558Srgrimes			*epp = ep;
11931558Srgrimes			ep->ex_flag |= EX_LINKED;
11941558Srgrimes		}
11951558Srgrimesnextline:
11961558Srgrimes		if (dirhead) {
11971558Srgrimes			free_dir(dirhead);
11981558Srgrimes			dirhead = (struct dirlist *)NULL;
11991558Srgrimes		}
12001558Srgrimes	}
12011558Srgrimes	fclose(exp_file);
12021558Srgrimes}
12031558Srgrimes
12041558Srgrimes/*
12051558Srgrimes * Allocate an export list element
12061558Srgrimes */
12071558Srgrimesstruct exportlist *
12081558Srgrimesget_exp()
12091558Srgrimes{
12101558Srgrimes	struct exportlist *ep;
12111558Srgrimes
12121558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
12131558Srgrimes	if (ep == (struct exportlist *)NULL)
12141558Srgrimes		out_of_mem();
121523681Speter	memset(ep, 0, sizeof(struct exportlist));
12161558Srgrimes	return (ep);
12171558Srgrimes}
12181558Srgrimes
12191558Srgrimes/*
12201558Srgrimes * Allocate a group list element
12211558Srgrimes */
12221558Srgrimesstruct grouplist *
12231558Srgrimesget_grp()
12241558Srgrimes{
12251558Srgrimes	struct grouplist *gp;
12261558Srgrimes
12271558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
12281558Srgrimes	if (gp == (struct grouplist *)NULL)
12291558Srgrimes		out_of_mem();
123023681Speter	memset(gp, 0, sizeof(struct grouplist));
12311558Srgrimes	return (gp);
12321558Srgrimes}
12331558Srgrimes
12341558Srgrimes/*
12351558Srgrimes * Clean up upon an error in get_exportlist().
12361558Srgrimes */
12371558Srgrimesvoid
12381558Srgrimesgetexp_err(ep, grp)
12391558Srgrimes	struct exportlist *ep;
12401558Srgrimes	struct grouplist *grp;
12411558Srgrimes{
12421558Srgrimes	struct grouplist *tgrp;
12431558Srgrimes
1244100336Sjoerg	if (!(opt_flags & OP_QUIET))
1245100336Sjoerg		syslog(LOG_ERR, "bad exports list line %s", line);
12461558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
12471558Srgrimes		free_exp(ep);
12481558Srgrimes	while (grp) {
12491558Srgrimes		tgrp = grp;
12501558Srgrimes		grp = grp->gr_next;
12511558Srgrimes		free_grp(tgrp);
12521558Srgrimes	}
12531558Srgrimes}
12541558Srgrimes
12551558Srgrimes/*
12561558Srgrimes * Search the export list for a matching fs.
12571558Srgrimes */
12581558Srgrimesstruct exportlist *
12591558Srgrimesex_search(fsid)
12601558Srgrimes	fsid_t *fsid;
12611558Srgrimes{
12621558Srgrimes	struct exportlist *ep;
12631558Srgrimes
12641558Srgrimes	ep = exphead;
12651558Srgrimes	while (ep) {
12661558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
12671558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
12681558Srgrimes			return (ep);
12691558Srgrimes		ep = ep->ex_next;
12701558Srgrimes	}
12711558Srgrimes	return (ep);
12721558Srgrimes}
12731558Srgrimes
12741558Srgrimes/*
12751558Srgrimes * Add a directory path to the list.
12761558Srgrimes */
12771558Srgrimeschar *
12781558Srgrimesadd_expdir(dpp, cp, len)
12791558Srgrimes	struct dirlist **dpp;
12801558Srgrimes	char *cp;
12811558Srgrimes	int len;
12821558Srgrimes{
12831558Srgrimes	struct dirlist *dp;
12841558Srgrimes
12851558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
128637663Scharnier	if (dp == (struct dirlist *)NULL)
128737663Scharnier		out_of_mem();
12881558Srgrimes	dp->dp_left = *dpp;
12891558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
12901558Srgrimes	dp->dp_flag = 0;
12911558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
12921558Srgrimes	strcpy(dp->dp_dirp, cp);
12931558Srgrimes	*dpp = dp;
12941558Srgrimes	return (dp->dp_dirp);
12951558Srgrimes}
12961558Srgrimes
12971558Srgrimes/*
12981558Srgrimes * Hang the dir list element off the dirpath binary tree as required
12991558Srgrimes * and update the entry for host.
13001558Srgrimes */
13011558Srgrimesvoid
13029336Sdfrhang_dirp(dp, grp, ep, flags)
13031558Srgrimes	struct dirlist *dp;
13041558Srgrimes	struct grouplist *grp;
13051558Srgrimes	struct exportlist *ep;
13069336Sdfr	int flags;
13071558Srgrimes{
13081558Srgrimes	struct hostlist *hp;
13091558Srgrimes	struct dirlist *dp2;
13101558Srgrimes
13119336Sdfr	if (flags & OP_ALLDIRS) {
13121558Srgrimes		if (ep->ex_defdir)
13131558Srgrimes			free((caddr_t)dp);
13141558Srgrimes		else
13151558Srgrimes			ep->ex_defdir = dp;
13169336Sdfr		if (grp == (struct grouplist *)NULL) {
13171558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
13189336Sdfr		} else while (grp) {
13191558Srgrimes			hp = get_ht();
13201558Srgrimes			hp->ht_grp = grp;
13211558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
13221558Srgrimes			ep->ex_defdir->dp_hosts = hp;
13231558Srgrimes			grp = grp->gr_next;
13241558Srgrimes		}
13251558Srgrimes	} else {
13261558Srgrimes
13271558Srgrimes		/*
132837663Scharnier		 * Loop through the directories adding them to the tree.
13291558Srgrimes		 */
13301558Srgrimes		while (dp) {
13311558Srgrimes			dp2 = dp->dp_left;
13329336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
13331558Srgrimes			dp = dp2;
13341558Srgrimes		}
13351558Srgrimes	}
13361558Srgrimes}
13371558Srgrimes
13381558Srgrimes/*
13391558Srgrimes * Traverse the binary tree either updating a node that is already there
13401558Srgrimes * for the new directory or adding the new node.
13411558Srgrimes */
13421558Srgrimesvoid
13439336Sdfradd_dlist(dpp, newdp, grp, flags)
13441558Srgrimes	struct dirlist **dpp;
13451558Srgrimes	struct dirlist *newdp;
13461558Srgrimes	struct grouplist *grp;
13479336Sdfr	int flags;
13481558Srgrimes{
13491558Srgrimes	struct dirlist *dp;
13501558Srgrimes	struct hostlist *hp;
13511558Srgrimes	int cmp;
13521558Srgrimes
13531558Srgrimes	dp = *dpp;
13541558Srgrimes	if (dp) {
13551558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
13561558Srgrimes		if (cmp > 0) {
13579336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
13581558Srgrimes			return;
13591558Srgrimes		} else if (cmp < 0) {
13609336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
13611558Srgrimes			return;
13621558Srgrimes		} else
13631558Srgrimes			free((caddr_t)newdp);
13641558Srgrimes	} else {
13651558Srgrimes		dp = newdp;
13661558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
13671558Srgrimes		*dpp = dp;
13681558Srgrimes	}
13691558Srgrimes	if (grp) {
13701558Srgrimes
13711558Srgrimes		/*
13721558Srgrimes		 * Hang all of the host(s) off of the directory point.
13731558Srgrimes		 */
13741558Srgrimes		do {
13751558Srgrimes			hp = get_ht();
13761558Srgrimes			hp->ht_grp = grp;
13771558Srgrimes			hp->ht_next = dp->dp_hosts;
13781558Srgrimes			dp->dp_hosts = hp;
13791558Srgrimes			grp = grp->gr_next;
13801558Srgrimes		} while (grp);
13819336Sdfr	} else {
13821558Srgrimes		dp->dp_flag |= DP_DEFSET;
13839336Sdfr	}
13841558Srgrimes}
13851558Srgrimes
13861558Srgrimes/*
13871558Srgrimes * Search for a dirpath on the export point.
13881558Srgrimes */
13891558Srgrimesstruct dirlist *
139074462Salfreddirp_search(dp, dirp)
13911558Srgrimes	struct dirlist *dp;
139274462Salfred	char *dirp;
13931558Srgrimes{
13941558Srgrimes	int cmp;
13951558Srgrimes
13961558Srgrimes	if (dp) {
139774462Salfred		cmp = strcmp(dp->dp_dirp, dirp);
13981558Srgrimes		if (cmp > 0)
139974462Salfred			return (dirp_search(dp->dp_left, dirp));
14001558Srgrimes		else if (cmp < 0)
140174462Salfred			return (dirp_search(dp->dp_right, dirp));
14021558Srgrimes		else
14031558Srgrimes			return (dp);
14041558Srgrimes	}
14051558Srgrimes	return (dp);
14061558Srgrimes}
14071558Srgrimes
14081558Srgrimes/*
14091558Srgrimes * Scan for a host match in a directory tree.
14101558Srgrimes */
14111558Srgrimesint
14129336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
14131558Srgrimes	struct dirlist *dp;
141474462Salfred	struct sockaddr *saddr;
14151558Srgrimes	int *defsetp;
14169336Sdfr	int *hostsetp;
14171558Srgrimes{
14181558Srgrimes	struct hostlist *hp;
14191558Srgrimes	struct grouplist *grp;
142074462Salfred	struct addrinfo *ai;
14211558Srgrimes
14221558Srgrimes	if (dp) {
14231558Srgrimes		if (dp->dp_flag & DP_DEFSET)
14249336Sdfr			*defsetp = dp->dp_flag;
14251558Srgrimes		hp = dp->dp_hosts;
14261558Srgrimes		while (hp) {
14271558Srgrimes			grp = hp->ht_grp;
14281558Srgrimes			switch (grp->gr_type) {
14291558Srgrimes			case GT_HOST:
143074462Salfred				ai = grp->gr_ptr.gt_addrinfo;
143174462Salfred				for (; ai; ai = ai->ai_next) {
143275801Siedowse					if (!sacmp(ai->ai_addr, saddr, NULL)) {
143374462Salfred						*hostsetp =
143474462Salfred						    (hp->ht_flag | DP_HOSTSET);
143574462Salfred						return (1);
143674462Salfred					}
14379336Sdfr				}
143875801Siedowse				break;
14391558Srgrimes			case GT_NET:
144075801Siedowse				if (!sacmp(saddr, (struct sockaddr *)
144175801Siedowse				    &grp->gr_ptr.gt_net.nt_net,
144275801Siedowse				    (struct sockaddr *)
144375801Siedowse				    &grp->gr_ptr.gt_net.nt_mask)) {
144474462Salfred					*hostsetp = (hp->ht_flag | DP_HOSTSET);
144574462Salfred					return (1);
144674462Salfred				}
144775801Siedowse				break;
144875801Siedowse			}
14491558Srgrimes			hp = hp->ht_next;
14501558Srgrimes		}
14511558Srgrimes	}
14521558Srgrimes	return (0);
14531558Srgrimes}
14541558Srgrimes
14551558Srgrimes/*
14561558Srgrimes * Scan tree for a host that matches the address.
14571558Srgrimes */
14581558Srgrimesint
14591558Srgrimesscan_tree(dp, saddr)
14601558Srgrimes	struct dirlist *dp;
146174462Salfred	struct sockaddr *saddr;
14621558Srgrimes{
14639336Sdfr	int defset, hostset;
14641558Srgrimes
14651558Srgrimes	if (dp) {
14661558Srgrimes		if (scan_tree(dp->dp_left, saddr))
14671558Srgrimes			return (1);
14689336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
14691558Srgrimes			return (1);
14701558Srgrimes		if (scan_tree(dp->dp_right, saddr))
14711558Srgrimes			return (1);
14721558Srgrimes	}
14731558Srgrimes	return (0);
14741558Srgrimes}
14751558Srgrimes
14761558Srgrimes/*
14771558Srgrimes * Traverse the dirlist tree and free it up.
14781558Srgrimes */
14791558Srgrimesvoid
14801558Srgrimesfree_dir(dp)
14811558Srgrimes	struct dirlist *dp;
14821558Srgrimes{
14831558Srgrimes
14841558Srgrimes	if (dp) {
14851558Srgrimes		free_dir(dp->dp_left);
14861558Srgrimes		free_dir(dp->dp_right);
14871558Srgrimes		free_host(dp->dp_hosts);
14881558Srgrimes		free((caddr_t)dp);
14891558Srgrimes	}
14901558Srgrimes}
14911558Srgrimes
14921558Srgrimes/*
14931558Srgrimes * Parse the option string and update fields.
14941558Srgrimes * Option arguments may either be -<option>=<value> or
14951558Srgrimes * -<option> <value>
14961558Srgrimes */
14971558Srgrimesint
14981558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
14991558Srgrimes	char **cpp, **endcpp;
15001558Srgrimes	struct exportlist *ep;
15011558Srgrimes	struct grouplist *grp;
15021558Srgrimes	int *has_hostp;
15031558Srgrimes	int *exflagsp;
150472650Sgreen	struct xucred *cr;
15051558Srgrimes{
15061558Srgrimes	char *cpoptarg, *cpoptend;
15071558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
15081558Srgrimes	int allflag, usedarg;
15091558Srgrimes
151051968Salfred	savedc2 = '\0';
15111558Srgrimes	cpopt = *cpp;
15121558Srgrimes	cpopt++;
15131558Srgrimes	cp = *endcpp;
15141558Srgrimes	savedc = *cp;
15151558Srgrimes	*cp = '\0';
15161558Srgrimes	while (cpopt && *cpopt) {
15171558Srgrimes		allflag = 1;
15181558Srgrimes		usedarg = -2;
151937663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
15201558Srgrimes			*cpoptend++ = '\0';
152137663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15221558Srgrimes				*cpoptarg++ = '\0';
15231558Srgrimes		} else {
152437663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15251558Srgrimes				*cpoptarg++ = '\0';
15261558Srgrimes			else {
15271558Srgrimes				*cp = savedc;
15281558Srgrimes				nextfield(&cp, &endcp);
15291558Srgrimes				**endcpp = '\0';
15301558Srgrimes				if (endcp > cp && *cp != '-') {
15311558Srgrimes					cpoptarg = cp;
15321558Srgrimes					savedc2 = *endcp;
15331558Srgrimes					*endcp = '\0';
15341558Srgrimes					usedarg = 0;
15351558Srgrimes				}
15361558Srgrimes			}
15371558Srgrimes		}
15381558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
15391558Srgrimes			*exflagsp |= MNT_EXRDONLY;
15401558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
15411558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
15421558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
15431558Srgrimes			usedarg++;
15441558Srgrimes			parsecred(cpoptarg, cr);
15451558Srgrimes			if (allflag == 0) {
15461558Srgrimes				*exflagsp |= MNT_EXPORTANON;
15471558Srgrimes				opt_flags |= OP_MAPALL;
15481558Srgrimes			} else
15491558Srgrimes				opt_flags |= OP_MAPROOT;
15501558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
155175801Siedowse		    !strcmp(cpopt, "m"))) {
15521558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
155337663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
15541558Srgrimes				return (1);
15551558Srgrimes			}
15561558Srgrimes			usedarg++;
15571558Srgrimes			opt_flags |= OP_MASK;
15581558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
15591558Srgrimes			!strcmp(cpopt, "n"))) {
156074462Salfred			if (strchr(cpoptarg, '/') != NULL) {
156174462Salfred				if (debug)
156274462Salfred					fprintf(stderr, "setting OP_MASKLEN\n");
156374462Salfred				opt_flags |= OP_MASKLEN;
156474462Salfred			}
15651558Srgrimes			if (grp->gr_type != GT_NULL) {
156637663Scharnier				syslog(LOG_ERR, "network/host conflict");
15671558Srgrimes				return (1);
15681558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
156937663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
15701558Srgrimes				return (1);
15711558Srgrimes			}
15721558Srgrimes			grp->gr_type = GT_NET;
15731558Srgrimes			*has_hostp = 1;
15741558Srgrimes			usedarg++;
15751558Srgrimes			opt_flags |= OP_NET;
15761558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
15771558Srgrimes			opt_flags |= OP_ALLDIRS;
157827447Sdfr		} else if (!strcmp(cpopt, "public")) {
157927447Sdfr			*exflagsp |= MNT_EXPUBLIC;
158027447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
158127447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
158227447Sdfr			opt_flags |= OP_MAPALL;
158327447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
158427447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
1585100336Sjoerg		} else if (!strcmp(cpopt, "quiet")) {
1586100336Sjoerg			opt_flags |= OP_QUIET;
15871558Srgrimes		} else {
158837663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
15891558Srgrimes			return (1);
15901558Srgrimes		}
15911558Srgrimes		if (usedarg >= 0) {
15921558Srgrimes			*endcp = savedc2;
15931558Srgrimes			**endcpp = savedc;
15941558Srgrimes			if (usedarg > 0) {
15951558Srgrimes				*cpp = cp;
15961558Srgrimes				*endcpp = endcp;
15971558Srgrimes			}
15981558Srgrimes			return (0);
15991558Srgrimes		}
16001558Srgrimes		cpopt = cpoptend;
16011558Srgrimes	}
16021558Srgrimes	**endcpp = savedc;
16031558Srgrimes	return (0);
16041558Srgrimes}
16051558Srgrimes
16061558Srgrimes/*
16071558Srgrimes * Translate a character string to the corresponding list of network
16081558Srgrimes * addresses for a hostname.
16091558Srgrimes */
16101558Srgrimesint
16117401Swpaulget_host(cp, grp, tgrp)
16121558Srgrimes	char *cp;
16131558Srgrimes	struct grouplist *grp;
16147401Swpaul	struct grouplist *tgrp;
16151558Srgrimes{
16167401Swpaul	struct grouplist *checkgrp;
161775635Siedowse	struct addrinfo *ai, *tai, hints;
161874462Salfred	int ecode;
161974462Salfred	char host[NI_MAXHOST];
16201558Srgrimes
162174462Salfred	if (grp->gr_type != GT_NULL) {
162274462Salfred		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
16231558Srgrimes		return (1);
16241558Srgrimes	}
162574462Salfred	memset(&hints, 0, sizeof hints);
162674462Salfred	hints.ai_flags = AI_CANONNAME;
162774462Salfred	hints.ai_protocol = IPPROTO_UDP;
162874462Salfred	ecode = getaddrinfo(cp, NULL, &hints, &ai);
162974462Salfred	if (ecode != 0) {
163075635Siedowse		syslog(LOG_ERR,"can't get address info for host %s", cp);
163174462Salfred		return 1;
163274462Salfred	}
163374462Salfred	grp->gr_ptr.gt_addrinfo = ai;
163474462Salfred	while (ai != NULL) {
163574462Salfred		if (ai->ai_canonname == NULL) {
163674462Salfred			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
163774462Salfred			    sizeof host, NULL, 0, ninumeric) != 0)
163874462Salfred				strlcpy(host, "?", sizeof(host));
163974462Salfred			ai->ai_canonname = strdup(host);
164074462Salfred			ai->ai_flags |= AI_CANONNAME;
164175641Siedowse		}
164274462Salfred		if (debug)
164375635Siedowse			fprintf(stderr, "got host %s\n", ai->ai_canonname);
164475635Siedowse		/*
164575635Siedowse		 * Sanity check: make sure we don't already have an entry
164675635Siedowse		 * for this host in the grouplist.
164775635Siedowse		 */
164875635Siedowse		for (checkgrp = tgrp; checkgrp != NULL;
164975635Siedowse		    checkgrp = checkgrp->gr_next) {
165075635Siedowse			if (checkgrp->gr_type != GT_HOST)
165175635Siedowse				continue;
165275635Siedowse			for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
165375635Siedowse			    tai = tai->ai_next) {
165475801Siedowse				if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
165575635Siedowse					continue;
165675635Siedowse				if (debug)
165775635Siedowse					fprintf(stderr,
165875635Siedowse					    "ignoring duplicate host %s\n",
165975635Siedowse					    ai->ai_canonname);
166075635Siedowse				grp->gr_type = GT_IGNORE;
166175635Siedowse				return (0);
166275635Siedowse			}
166375635Siedowse		}
166474462Salfred		ai = ai->ai_next;
16651558Srgrimes	}
166675635Siedowse	grp->gr_type = GT_HOST;
16671558Srgrimes	return (0);
16681558Srgrimes}
16691558Srgrimes
16701558Srgrimes/*
16711558Srgrimes * Free up an exports list component
16721558Srgrimes */
16731558Srgrimesvoid
16741558Srgrimesfree_exp(ep)
16751558Srgrimes	struct exportlist *ep;
16761558Srgrimes{
16771558Srgrimes
16781558Srgrimes	if (ep->ex_defdir) {
16791558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
16801558Srgrimes		free((caddr_t)ep->ex_defdir);
16811558Srgrimes	}
16821558Srgrimes	if (ep->ex_fsdir)
16831558Srgrimes		free(ep->ex_fsdir);
168427447Sdfr	if (ep->ex_indexfile)
168527447Sdfr		free(ep->ex_indexfile);
16861558Srgrimes	free_dir(ep->ex_dirl);
16871558Srgrimes	free((caddr_t)ep);
16881558Srgrimes}
16891558Srgrimes
16901558Srgrimes/*
16911558Srgrimes * Free hosts.
16921558Srgrimes */
16931558Srgrimesvoid
16941558Srgrimesfree_host(hp)
16951558Srgrimes	struct hostlist *hp;
16961558Srgrimes{
16971558Srgrimes	struct hostlist *hp2;
16981558Srgrimes
16991558Srgrimes	while (hp) {
17001558Srgrimes		hp2 = hp;
17011558Srgrimes		hp = hp->ht_next;
17021558Srgrimes		free((caddr_t)hp2);
17031558Srgrimes	}
17041558Srgrimes}
17051558Srgrimes
17061558Srgrimesstruct hostlist *
17071558Srgrimesget_ht()
17081558Srgrimes{
17091558Srgrimes	struct hostlist *hp;
17101558Srgrimes
17111558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
17121558Srgrimes	if (hp == (struct hostlist *)NULL)
17131558Srgrimes		out_of_mem();
17141558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
17159336Sdfr	hp->ht_flag = 0;
17161558Srgrimes	return (hp);
17171558Srgrimes}
17181558Srgrimes
17191558Srgrimes/*
17201558Srgrimes * Out of memory, fatal
17211558Srgrimes */
17221558Srgrimesvoid
17231558Srgrimesout_of_mem()
17241558Srgrimes{
17251558Srgrimes
172637663Scharnier	syslog(LOG_ERR, "out of memory");
17271558Srgrimes	exit(2);
17281558Srgrimes}
17291558Srgrimes
17301558Srgrimes/*
17311558Srgrimes * Do the mount syscall with the update flag to push the export info into
17321558Srgrimes * the kernel.
17331558Srgrimes */
17341558Srgrimesint
17351558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
17361558Srgrimes	struct exportlist *ep;
17371558Srgrimes	struct grouplist *grp;
17381558Srgrimes	int exflags;
173972650Sgreen	struct xucred *anoncrp;
17401558Srgrimes	char *dirp;
17411558Srgrimes	int dirplen;
17421558Srgrimes	struct statfs *fsb;
17431558Srgrimes{
174475841Siedowse	struct statfs fsb1;
174574462Salfred	struct addrinfo *ai;
174675801Siedowse	struct export_args *eap;
174774462Salfred	char *cp = NULL;
17481558Srgrimes	int done;
17491558Srgrimes	char savedc = '\0';
17501558Srgrimes	union {
17511558Srgrimes		struct ufs_args ua;
17521558Srgrimes		struct iso_args ia;
17539336Sdfr		struct msdosfs_args da;
175454093Ssemenu		struct ntfs_args na;
17551558Srgrimes	} args;
17561558Srgrimes
175775801Siedowse	bzero(&args, sizeof args);
175875801Siedowse	/* XXX, we assume that all xx_args look like ufs_args. */
17591558Srgrimes	args.ua.fspec = 0;
176075801Siedowse	eap = &args.ua.export;
176175801Siedowse
176275801Siedowse	eap->ex_flags = exflags;
176375801Siedowse	eap->ex_anon = *anoncrp;
176475801Siedowse	eap->ex_indexfile = ep->ex_indexfile;
176575641Siedowse	if (grp->gr_type == GT_HOST)
176674462Salfred		ai = grp->gr_ptr.gt_addrinfo;
176775641Siedowse	else
176875641Siedowse		ai = NULL;
17691558Srgrimes	done = FALSE;
17701558Srgrimes	while (!done) {
17711558Srgrimes		switch (grp->gr_type) {
17721558Srgrimes		case GT_HOST:
177375641Siedowse			if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
177474462Salfred				goto skip;
177575801Siedowse			eap->ex_addr = ai->ai_addr;
177675801Siedowse			eap->ex_addrlen = ai->ai_addrlen;
177775801Siedowse			eap->ex_masklen = 0;
17781558Srgrimes			break;
17791558Srgrimes		case GT_NET:
178075801Siedowse			if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
178174462Salfred			    have_v6 == 0)
178274462Salfred				goto skip;
178375801Siedowse			eap->ex_addr =
178475801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
178575801Siedowse			eap->ex_addrlen = args.ua.export.ex_addr->sa_len;
178675801Siedowse			eap->ex_mask =
178775801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
178875801Siedowse			eap->ex_masklen = args.ua.export.ex_mask->sa_len;
17891558Srgrimes			break;
179075641Siedowse		case GT_DEFAULT:
179175801Siedowse			eap->ex_addr = NULL;
179275801Siedowse			eap->ex_addrlen = 0;
179375801Siedowse			eap->ex_mask = NULL;
179475801Siedowse			eap->ex_masklen = 0;
179575641Siedowse			break;
17967401Swpaul		case GT_IGNORE:
17977401Swpaul			return(0);
17987401Swpaul			break;
17991558Srgrimes		default:
180037663Scharnier			syslog(LOG_ERR, "bad grouptype");
18011558Srgrimes			if (cp)
18021558Srgrimes				*cp = savedc;
18031558Srgrimes			return (1);
18041558Srgrimes		};
18051558Srgrimes
18061558Srgrimes		/*
18071558Srgrimes		 * XXX:
18081558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
18091558Srgrimes		 * of looping back up the dirp to the mount point??
18101558Srgrimes		 * Also, needs to know how to export all types of local
181196707Strhodes		 * exportable filesystems and not just "ufs".
18121558Srgrimes		 */
18139336Sdfr		while (mount(fsb->f_fstypename, dirp,
181474462Salfred		    fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
18151558Srgrimes			if (cp)
18161558Srgrimes				*cp-- = savedc;
18171558Srgrimes			else
18181558Srgrimes				cp = dirp + dirplen - 1;
1819100336Sjoerg			if (opt_flags & OP_QUIET)
1820100336Sjoerg				return (1);
18211558Srgrimes			if (errno == EPERM) {
182275635Siedowse				if (debug)
182375635Siedowse					warnx("can't change attributes for %s",
182475635Siedowse					    dirp);
18251558Srgrimes				syslog(LOG_ERR,
182637663Scharnier				   "can't change attributes for %s", dirp);
18271558Srgrimes				return (1);
18281558Srgrimes			}
18291558Srgrimes			if (opt_flags & OP_ALLDIRS) {
1830100336Sjoerg				if (errno == EINVAL)
1831100336Sjoerg					syslog(LOG_ERR,
1832100336Sjoerg		"-alldirs requested but %s is not a filesystem mountpoint",
1833100336Sjoerg						dirp);
1834100336Sjoerg				else
1835100336Sjoerg					syslog(LOG_ERR,
1836100336Sjoerg						"could not remount %s: %m",
1837100336Sjoerg						dirp);
18381558Srgrimes				return (1);
18391558Srgrimes			}
18401558Srgrimes			/* back up over the last component */
18411558Srgrimes			while (*cp == '/' && cp > dirp)
18421558Srgrimes				cp--;
18431558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
18441558Srgrimes				cp--;
18451558Srgrimes			if (cp == dirp) {
18461558Srgrimes				if (debug)
184737663Scharnier					warnx("mnt unsucc");
184837663Scharnier				syslog(LOG_ERR, "can't export %s", dirp);
18491558Srgrimes				return (1);
18501558Srgrimes			}
18511558Srgrimes			savedc = *cp;
18521558Srgrimes			*cp = '\0';
185375841Siedowse			/* Check that we're still on the same filesystem. */
185475841Siedowse			if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
185575841Siedowse			    &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
185675841Siedowse				*cp = savedc;
185775841Siedowse				syslog(LOG_ERR, "can't export %s", dirp);
185875841Siedowse				return (1);
185975841Siedowse			}
18601558Srgrimes		}
186174462Salfredskip:
186275641Siedowse		if (ai != NULL)
186374462Salfred			ai = ai->ai_next;
186475641Siedowse		if (ai == NULL)
18651558Srgrimes			done = TRUE;
18661558Srgrimes	}
18671558Srgrimes	if (cp)
18681558Srgrimes		*cp = savedc;
18691558Srgrimes	return (0);
18701558Srgrimes}
18711558Srgrimes
18721558Srgrimes/*
18731558Srgrimes * Translate a net address.
187475801Siedowse *
187575801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
18761558Srgrimes */
18771558Srgrimesint
18781558Srgrimesget_net(cp, net, maskflg)
18791558Srgrimes	char *cp;
18801558Srgrimes	struct netmsk *net;
18811558Srgrimes	int maskflg;
18821558Srgrimes{
188375861Siedowse	struct netent *np = NULL;
188474462Salfred	char *name, *p, *prefp;
188575801Siedowse	struct sockaddr_in sin;
188675861Siedowse	struct sockaddr *sa = NULL;
188774462Salfred	struct addrinfo hints, *ai = NULL;
188874462Salfred	char netname[NI_MAXHOST];
188974462Salfred	long preflen;
18901558Srgrimes
189175635Siedowse	p = prefp = NULL;
189274462Salfred	if ((opt_flags & OP_MASKLEN) && !maskflg) {
189374462Salfred		p = strchr(cp, '/');
189474462Salfred		*p = '\0';
189574462Salfred		prefp = p + 1;
189674462Salfred	}
189774462Salfred
189875861Siedowse	/*
189975861Siedowse	 * Check for a numeric address first. We wish to avoid
190075861Siedowse	 * possible DNS lookups in getnetbyname().
190175861Siedowse	 */
190275861Siedowse	if (isxdigit(*cp) || *cp == ':') {
190374462Salfred		memset(&hints, 0, sizeof hints);
190475801Siedowse		/* Ensure the mask and the network have the same family. */
190575801Siedowse		if (maskflg && (opt_flags & OP_NET))
190675801Siedowse			hints.ai_family = net->nt_net.ss_family;
190775801Siedowse		else if (!maskflg && (opt_flags & OP_HAVEMASK))
190875801Siedowse			hints.ai_family = net->nt_mask.ss_family;
190975801Siedowse		else
191075801Siedowse			hints.ai_family = AF_UNSPEC;
191174462Salfred		hints.ai_flags = AI_NUMERICHOST;
191275861Siedowse		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
191375861Siedowse			sa = ai->ai_addr;
191475861Siedowse		if (sa != NULL && ai->ai_family == AF_INET) {
191574462Salfred			/*
191675801Siedowse			 * The address in `cp' is really a network address, so
191775801Siedowse			 * use inet_network() to re-interpret this correctly.
191875801Siedowse			 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
191974462Salfred			 */
192075801Siedowse			bzero(&sin, sizeof sin);
192174462Salfred			sin.sin_family = AF_INET;
192274462Salfred			sin.sin_len = sizeof sin;
192375801Siedowse			sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
192474462Salfred			if (debug)
192575801Siedowse				fprintf(stderr, "get_net: v4 addr %s\n",
192675801Siedowse				    inet_ntoa(sin.sin_addr));
192774462Salfred			sa = (struct sockaddr *)&sin;
192875861Siedowse		}
192975861Siedowse	}
193075861Siedowse	if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
193175861Siedowse		bzero(&sin, sizeof sin);
193275861Siedowse		sin.sin_family = AF_INET;
193375861Siedowse		sin.sin_len = sizeof sin;
193475861Siedowse		sin.sin_addr = inet_makeaddr(np->n_net, 0);
193575861Siedowse		sa = (struct sockaddr *)&sin;
193675861Siedowse	}
193775861Siedowse	if (sa == NULL)
193874462Salfred		goto fail;
193925318Spst
194075801Siedowse	if (maskflg) {
194175801Siedowse		/* The specified sockaddr is a mask. */
194275801Siedowse		if (checkmask(sa) != 0)
194375801Siedowse			goto fail;
194475801Siedowse		bcopy(sa, &net->nt_mask, sa->sa_len);
194575801Siedowse		opt_flags |= OP_HAVEMASK;
194675801Siedowse	} else {
194775801Siedowse		/* The specified sockaddr is a network address. */
194875801Siedowse		bcopy(sa, &net->nt_net, sa->sa_len);
194974462Salfred
195075801Siedowse		/* Get a network name for the export list. */
195175801Siedowse		if (np) {
195275801Siedowse			name = np->n_name;
195375801Siedowse		} else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
195475801Siedowse		   NULL, 0, ninumeric) == 0) {
195575801Siedowse			name = netname;
195675801Siedowse		} else {
195775801Siedowse			goto fail;
195875801Siedowse		}
195975801Siedowse		if ((net->nt_name = strdup(name)) == NULL)
196075801Siedowse			out_of_mem();
196175801Siedowse
196275801Siedowse		/*
196375801Siedowse		 * Extract a mask from either a "/<masklen>" suffix, or
196475801Siedowse		 * from the class of an IPv4 address.
196575801Siedowse		 */
196674462Salfred		if (opt_flags & OP_MASKLEN) {
196774462Salfred			preflen = strtol(prefp, NULL, 10);
196875801Siedowse			if (preflen < 0L || preflen == LONG_MAX)
196974462Salfred				goto fail;
197075801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
197175801Siedowse			if (makemask(&net->nt_mask, (int)preflen) != 0)
197275801Siedowse				goto fail;
197375801Siedowse			opt_flags |= OP_HAVEMASK;
197474462Salfred			*p = '/';
197575801Siedowse		} else if (sa->sa_family == AF_INET &&
197675801Siedowse		    (opt_flags & OP_MASK) == 0) {
197775801Siedowse			in_addr_t addr;
197874462Salfred
197975801Siedowse			addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
198075801Siedowse			if (IN_CLASSA(addr))
198175801Siedowse				preflen = 8;
198275801Siedowse			else if (IN_CLASSB(addr))
198375801Siedowse				preflen = 16;
198475801Siedowse			else if (IN_CLASSC(addr))
198575801Siedowse				preflen = 24;
198675801Siedowse			else if (IN_CLASSD(addr))
198775801Siedowse				preflen = 28;
198875801Siedowse			else
198975801Siedowse				preflen = 32;	/* XXX */
199075801Siedowse
199175801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
199275801Siedowse			makemask(&net->nt_mask, (int)preflen);
199375801Siedowse			opt_flags |= OP_HAVEMASK;
199474462Salfred		}
199574462Salfred	}
199674462Salfred
199774462Salfred	if (ai)
199874462Salfred		freeaddrinfo(ai);
199974462Salfred	return 0;
200074462Salfred
200174462Salfredfail:
200274462Salfred	if (ai)
200374462Salfred		freeaddrinfo(ai);
200474462Salfred	return 1;
20051558Srgrimes}
20061558Srgrimes
20071558Srgrimes/*
20081558Srgrimes * Parse out the next white space separated field
20091558Srgrimes */
20101558Srgrimesvoid
20111558Srgrimesnextfield(cp, endcp)
20121558Srgrimes	char **cp;
20131558Srgrimes	char **endcp;
20141558Srgrimes{
20151558Srgrimes	char *p;
20161558Srgrimes
20171558Srgrimes	p = *cp;
20181558Srgrimes	while (*p == ' ' || *p == '\t')
20191558Srgrimes		p++;
20201558Srgrimes	if (*p == '\n' || *p == '\0')
20211558Srgrimes		*cp = *endcp = p;
20221558Srgrimes	else {
20231558Srgrimes		*cp = p++;
20241558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
20251558Srgrimes			p++;
20261558Srgrimes		*endcp = p;
20271558Srgrimes	}
20281558Srgrimes}
20291558Srgrimes
20301558Srgrimes/*
20311558Srgrimes * Get an exports file line. Skip over blank lines and handle line
20321558Srgrimes * continuations.
20331558Srgrimes */
20341558Srgrimesint
20351558Srgrimesget_line()
20361558Srgrimes{
20371558Srgrimes	char *p, *cp;
203896622Siedowse	size_t len;
20391558Srgrimes	int totlen, cont_line;
20401558Srgrimes
20411558Srgrimes	/*
20421558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
20431558Srgrimes	 */
20441558Srgrimes	p = line;
20451558Srgrimes	totlen = 0;
20461558Srgrimes	do {
204796622Siedowse		if ((p = fgetln(exp_file, &len)) == NULL)
20481558Srgrimes			return (0);
20491558Srgrimes		cp = p + len - 1;
20501558Srgrimes		cont_line = 0;
20511558Srgrimes		while (cp >= p &&
20521558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
20531558Srgrimes			if (*cp == '\\')
20541558Srgrimes				cont_line = 1;
20551558Srgrimes			cp--;
20561558Srgrimes			len--;
20571558Srgrimes		}
205879117Sdd		if (cont_line) {
205979117Sdd			*++cp = ' ';
206079117Sdd			len++;
206179117Sdd		}
206296622Siedowse		if (linesize < len + totlen + 1) {
206396622Siedowse			linesize = len + totlen + 1;
206496622Siedowse			line = realloc(line, linesize);
206596622Siedowse			if (line == NULL)
206696622Siedowse				out_of_mem();
20671558Srgrimes		}
206896622Siedowse		memcpy(line + totlen, p, len);
206996622Siedowse		totlen += len;
207096622Siedowse		line[totlen] = '\0';
20711558Srgrimes	} while (totlen == 0 || cont_line);
20721558Srgrimes	return (1);
20731558Srgrimes}
20741558Srgrimes
20751558Srgrimes/*
20761558Srgrimes * Parse a description of a credential.
20771558Srgrimes */
20781558Srgrimesvoid
20791558Srgrimesparsecred(namelist, cr)
20801558Srgrimes	char *namelist;
208172650Sgreen	struct xucred *cr;
20821558Srgrimes{
20831558Srgrimes	char *name;
20841558Srgrimes	int cnt;
20851558Srgrimes	char *names;
20861558Srgrimes	struct passwd *pw;
20871558Srgrimes	struct group *gr;
20881558Srgrimes	int ngroups, groups[NGROUPS + 1];
20891558Srgrimes
209091354Sdd	cr->cr_version = XUCRED_VERSION;
20911558Srgrimes	/*
209237663Scharnier	 * Set up the unprivileged user.
20931558Srgrimes	 */
20941558Srgrimes	cr->cr_uid = -2;
20951558Srgrimes	cr->cr_groups[0] = -2;
20961558Srgrimes	cr->cr_ngroups = 1;
20971558Srgrimes	/*
20981558Srgrimes	 * Get the user's password table entry.
20991558Srgrimes	 */
21001558Srgrimes	names = strsep(&namelist, " \t\n");
21011558Srgrimes	name = strsep(&names, ":");
21021558Srgrimes	if (isdigit(*name) || *name == '-')
21031558Srgrimes		pw = getpwuid(atoi(name));
21041558Srgrimes	else
21051558Srgrimes		pw = getpwnam(name);
21061558Srgrimes	/*
21071558Srgrimes	 * Credentials specified as those of a user.
21081558Srgrimes	 */
21091558Srgrimes	if (names == NULL) {
21101558Srgrimes		if (pw == NULL) {
211137663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
21121558Srgrimes			return;
21131558Srgrimes		}
21141558Srgrimes		cr->cr_uid = pw->pw_uid;
21151558Srgrimes		ngroups = NGROUPS + 1;
21161558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
211737663Scharnier			syslog(LOG_ERR, "too many groups");
21181558Srgrimes		/*
21191558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
21201558Srgrimes		 */
21211558Srgrimes		cr->cr_ngroups = ngroups - 1;
21221558Srgrimes		cr->cr_groups[0] = groups[0];
21231558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
21241558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
21251558Srgrimes		return;
21261558Srgrimes	}
21271558Srgrimes	/*
21281558Srgrimes	 * Explicit credential specified as a colon separated list:
21291558Srgrimes	 *	uid:gid:gid:...
21301558Srgrimes	 */
21311558Srgrimes	if (pw != NULL)
21321558Srgrimes		cr->cr_uid = pw->pw_uid;
21331558Srgrimes	else if (isdigit(*name) || *name == '-')
21341558Srgrimes		cr->cr_uid = atoi(name);
21351558Srgrimes	else {
213637663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
21371558Srgrimes		return;
21381558Srgrimes	}
21391558Srgrimes	cr->cr_ngroups = 0;
21401558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
21411558Srgrimes		name = strsep(&names, ":");
21421558Srgrimes		if (isdigit(*name) || *name == '-') {
21431558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
21441558Srgrimes		} else {
21451558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
214637663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
21471558Srgrimes				continue;
21481558Srgrimes			}
21491558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
21501558Srgrimes		}
21511558Srgrimes	}
21521558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
215337663Scharnier		syslog(LOG_ERR, "too many groups");
21541558Srgrimes}
21551558Srgrimes
21561558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
21571558Srgrimes/*
21581558Srgrimes * Routines that maintain the remote mounttab
21591558Srgrimes */
21601558Srgrimesvoid
21611558Srgrimesget_mountlist()
21621558Srgrimes{
21631558Srgrimes	struct mountlist *mlp, **mlpp;
216423681Speter	char *host, *dirp, *cp;
21651558Srgrimes	char str[STRSIZ];
21661558Srgrimes	FILE *mlfile;
21671558Srgrimes
21681558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
216953117Sbillf		if (errno == ENOENT)
217053117Sbillf			return;
217153117Sbillf		else {
217253117Sbillf			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
217353117Sbillf			return;
217453117Sbillf		}
21751558Srgrimes	}
21761558Srgrimes	mlpp = &mlhead;
21771558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
217823681Speter		cp = str;
217923681Speter		host = strsep(&cp, " \t\n");
218023681Speter		dirp = strsep(&cp, " \t\n");
218123681Speter		if (host == NULL || dirp == NULL)
21821558Srgrimes			continue;
21831558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
218437663Scharnier		if (mlp == (struct mountlist *)NULL)
218537663Scharnier			out_of_mem();
218623681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
218723681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
218823681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
218923681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
21901558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
21911558Srgrimes		*mlpp = mlp;
21921558Srgrimes		mlpp = &mlp->ml_next;
21931558Srgrimes	}
21941558Srgrimes	fclose(mlfile);
21951558Srgrimes}
21961558Srgrimes
219775635Siedowsevoid
219875635Siedowsedel_mlist(char *hostp, char *dirp)
21991558Srgrimes{
22001558Srgrimes	struct mountlist *mlp, **mlpp;
22011558Srgrimes	struct mountlist *mlp2;
22021558Srgrimes	FILE *mlfile;
22031558Srgrimes	int fnd = 0;
22041558Srgrimes
22051558Srgrimes	mlpp = &mlhead;
22061558Srgrimes	mlp = mlhead;
22071558Srgrimes	while (mlp) {
22081558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
22091558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
22101558Srgrimes			fnd = 1;
22111558Srgrimes			mlp2 = mlp;
22121558Srgrimes			*mlpp = mlp = mlp->ml_next;
22131558Srgrimes			free((caddr_t)mlp2);
22141558Srgrimes		} else {
22151558Srgrimes			mlpp = &mlp->ml_next;
22161558Srgrimes			mlp = mlp->ml_next;
22171558Srgrimes		}
22181558Srgrimes	}
22191558Srgrimes	if (fnd) {
22201558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
222137663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
22221558Srgrimes			return;
22231558Srgrimes		}
22241558Srgrimes		mlp = mlhead;
22251558Srgrimes		while (mlp) {
22261558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22271558Srgrimes			mlp = mlp->ml_next;
22281558Srgrimes		}
22291558Srgrimes		fclose(mlfile);
22301558Srgrimes	}
22311558Srgrimes}
22321558Srgrimes
22331558Srgrimesvoid
22341558Srgrimesadd_mlist(hostp, dirp)
22351558Srgrimes	char *hostp, *dirp;
22361558Srgrimes{
22371558Srgrimes	struct mountlist *mlp, **mlpp;
22381558Srgrimes	FILE *mlfile;
22391558Srgrimes
22401558Srgrimes	mlpp = &mlhead;
22411558Srgrimes	mlp = mlhead;
22421558Srgrimes	while (mlp) {
22431558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
22441558Srgrimes			return;
22451558Srgrimes		mlpp = &mlp->ml_next;
22461558Srgrimes		mlp = mlp->ml_next;
22471558Srgrimes	}
22481558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
224937663Scharnier	if (mlp == (struct mountlist *)NULL)
225037663Scharnier		out_of_mem();
22511558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
22521558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
22531558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
22541558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
22551558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
22561558Srgrimes	*mlpp = mlp;
22571558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
225837663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
22591558Srgrimes		return;
22601558Srgrimes	}
22611558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22621558Srgrimes	fclose(mlfile);
22631558Srgrimes}
22641558Srgrimes
22651558Srgrimes/*
22661558Srgrimes * Free up a group list.
22671558Srgrimes */
22681558Srgrimesvoid
22691558Srgrimesfree_grp(grp)
22701558Srgrimes	struct grouplist *grp;
22711558Srgrimes{
22721558Srgrimes	if (grp->gr_type == GT_HOST) {
227374462Salfred		if (grp->gr_ptr.gt_addrinfo != NULL)
227474462Salfred			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
22751558Srgrimes	} else if (grp->gr_type == GT_NET) {
22761558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
22771558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
22781558Srgrimes	}
22791558Srgrimes	free((caddr_t)grp);
22801558Srgrimes}
22811558Srgrimes
22821558Srgrimes#ifdef DEBUG
22831558Srgrimesvoid
22841558SrgrimesSYSLOG(int pri, const char *fmt, ...)
22851558Srgrimes{
22861558Srgrimes	va_list ap;
22871558Srgrimes
22881558Srgrimes	va_start(ap, fmt);
22891558Srgrimes	vfprintf(stderr, fmt, ap);
22901558Srgrimes	va_end(ap);
22911558Srgrimes}
22921558Srgrimes#endif /* DEBUG */
22931558Srgrimes
22941558Srgrimes/*
22951558Srgrimes * Check options for consistency.
22961558Srgrimes */
22971558Srgrimesint
22981558Srgrimescheck_options(dp)
22991558Srgrimes	struct dirlist *dp;
23001558Srgrimes{
23011558Srgrimes
23021558Srgrimes	if (dp == (struct dirlist *)NULL)
23031558Srgrimes	    return (1);
230483653Speter	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
230583653Speter	    syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
23061558Srgrimes	    return (1);
23071558Srgrimes	}
23081558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
230975801Siedowse		syslog(LOG_ERR, "-mask requires -network");
231075801Siedowse		return (1);
23111558Srgrimes	}
231275801Siedowse	if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
231375801Siedowse		syslog(LOG_ERR, "-network requires mask specification");
231475801Siedowse		return (1);
231575801Siedowse	}
231675801Siedowse	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
231775801Siedowse		syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
231875801Siedowse		return (1);
231975801Siedowse	}
23201558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
232145927Salex	    syslog(LOG_ERR, "-alldirs has multiple directories");
23221558Srgrimes	    return (1);
23231558Srgrimes	}
23241558Srgrimes	return (0);
23251558Srgrimes}
23261558Srgrimes
23271558Srgrimes/*
23281558Srgrimes * Check an absolute directory path for any symbolic links. Return true
23291558Srgrimes */
23301558Srgrimesint
23311558Srgrimescheck_dirpath(dirp)
23321558Srgrimes	char *dirp;
23331558Srgrimes{
23341558Srgrimes	char *cp;
23351558Srgrimes	int ret = 1;
23361558Srgrimes	struct stat sb;
23371558Srgrimes
23381558Srgrimes	cp = dirp + 1;
23391558Srgrimes	while (*cp && ret) {
23401558Srgrimes		if (*cp == '/') {
23411558Srgrimes			*cp = '\0';
23429336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23431558Srgrimes				ret = 0;
23441558Srgrimes			*cp = '/';
23451558Srgrimes		}
23461558Srgrimes		cp++;
23471558Srgrimes	}
23489336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23491558Srgrimes		ret = 0;
23501558Srgrimes	return (ret);
23511558Srgrimes}
23529336Sdfr
235375801Siedowse/*
235475801Siedowse * Make a netmask according to the specified prefix length. The ss_family
235575801Siedowse * and other non-address fields must be initialised before calling this.
235675801Siedowse */
235775801Siedowseint
235875801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen)
235974462Salfred{
236075801Siedowse	u_char *p;
236175801Siedowse	int bits, i, len;
236274462Salfred
236375801Siedowse	if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
236475801Siedowse		return (-1);
236575801Siedowse	if (bitlen > len * NBBY)
236675801Siedowse		return (-1);
236774462Salfred
236875801Siedowse	for (i = 0; i < len; i++) {
236975801Siedowse		bits = (bitlen > NBBY) ? NBBY : bitlen;
237075801Siedowse		*p++ = (1 << bits) - 1;
237175801Siedowse		bitlen -= bits;
237274462Salfred	}
237375801Siedowse	return 0;
237474462Salfred}
237574462Salfred
237675801Siedowse/*
237775801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask
237875801Siedowse * is acceptable (i.e. of the form 1...10....0).
237975801Siedowse */
238075801Siedowseint
238175801Siedowsecheckmask(struct sockaddr *sa)
238274462Salfred{
238375801Siedowse	u_char *mask;
238475801Siedowse	int i, len;
238574462Salfred
238675801Siedowse	if ((mask = sa_rawaddr(sa, &len)) == NULL)
238775801Siedowse		return (-1);
238875801Siedowse
238975801Siedowse	for (i = 0; i < len; i++)
239075801Siedowse		if (mask[i] != 0xff)
239175801Siedowse			break;
239275801Siedowse	if (i < len) {
239375801Siedowse		if (~mask[i] & (u_char)(~mask[i] + 1))
239475801Siedowse			return (-1);
239575801Siedowse		i++;
239674462Salfred	}
239775801Siedowse	for (; i < len; i++)
239875801Siedowse		if (mask[i] != 0)
239975801Siedowse			return (-1);
240075801Siedowse	return (0);
240174462Salfred}
240274462Salfred
240375801Siedowse/*
240475801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if
240575801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'.
240675801Siedowse * If samask is NULL, perform a full comparision.
240775801Siedowse */
240875801Siedowseint
240975801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
241074462Salfred{
241175801Siedowse	unsigned char *p1, *p2, *mask;
241275801Siedowse	int len, i;
241374462Salfred
241475801Siedowse	if (sa1->sa_family != sa2->sa_family ||
241575801Siedowse	    (p1 = sa_rawaddr(sa1, &len)) == NULL ||
241675801Siedowse	    (p2 = sa_rawaddr(sa2, NULL)) == NULL)
241775801Siedowse		return (1);
241875801Siedowse
241975801Siedowse	switch (sa1->sa_family) {
242074462Salfred	case AF_INET6:
242175801Siedowse		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
242275801Siedowse		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
242375801Siedowse			return (1);
242474462Salfred		break;
242574462Salfred	}
242674462Salfred
242775801Siedowse	/* Simple binary comparison if no mask specified. */
242875801Siedowse	if (samask == NULL)
242975801Siedowse		return (memcmp(p1, p2, len));
243074462Salfred
243175801Siedowse	/* Set up the mask, and do a mask-based comparison. */
243275801Siedowse	if (sa1->sa_family != samask->sa_family ||
243375801Siedowse	    (mask = sa_rawaddr(samask, NULL)) == NULL)
243475801Siedowse		return (1);
243574462Salfred
243675801Siedowse	for (i = 0; i < len; i++)
243775801Siedowse		if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
243875801Siedowse			return (1);
243975801Siedowse	return (0);
244074462Salfred}
244174462Salfred
244275801Siedowse/*
244375801Siedowse * Return a pointer to the part of the sockaddr that contains the
244475801Siedowse * raw address, and set *nbytes to its length in bytes. Returns
244575801Siedowse * NULL if the address family is unknown.
244675801Siedowse */
244775801Siedowsevoid *
244875801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) {
244975801Siedowse	void *p;
245074462Salfred	int len;
245174462Salfred
245275801Siedowse	switch (sa->sa_family) {
245374462Salfred	case AF_INET:
245475801Siedowse		len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
245575801Siedowse		p = &((struct sockaddr_in *)sa)->sin_addr;
245674462Salfred		break;
245774462Salfred	case AF_INET6:
245875801Siedowse		len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
245975801Siedowse		p = &((struct sockaddr_in6 *)sa)->sin6_addr;
246074462Salfred		break;
246174462Salfred	default:
246275801Siedowse		p = NULL;
246375801Siedowse		len = 0;
246474462Salfred	}
246574462Salfred
246675801Siedowse	if (nbytes != NULL)
246775801Siedowse		*nbytes = len;
246875801Siedowse	return (p);
246974462Salfred}
247074462Salfred
247175754Siedowsevoid
247275754Siedowsehuphandler(int sig)
247375754Siedowse{
247475754Siedowse	got_sighup = 1;
247575754Siedowse}
247675754Siedowse
247774462Salfredvoid terminate(sig)
247874462Salfredint sig;
247974462Salfred{
248074462Salfred	close(mountdlockfd);
248174462Salfred	unlink(MOUNTDLOCK);
248274792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
248374792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
248474462Salfred	exit (0);
248574462Salfred}
2486