mountd.c revision 105267
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
43105267Scharnier#if 0
441558Srgrimes#ifndef lint
4537663Scharnierstatic char sccsid[] = "@(#)mountd.c	8.15 (Berkeley) 5/1/95";
46105267Scharnier#endif /*not lint*/
4737663Scharnier#endif
481558Srgrimes
49105267Scharnier#include <sys/cdefs.h>
50105267Scharnier__FBSDID("$FreeBSD: head/usr.sbin/mountd/mountd.c 105267 2002-10-16 16:04:50Z charnier $");
51105267Scharnier
521558Srgrimes#include <sys/param.h>
531558Srgrimes#include <sys/mount.h>
5474462Salfred#include <sys/fcntl.h>
551558Srgrimes#include <sys/stat.h>
561558Srgrimes#include <sys/syslog.h>
5724330Sguido#include <sys/sysctl.h>
5896622Siedowse#include <sys/linker.h>
5996622Siedowse#include <sys/module.h>
601558Srgrimes
611558Srgrimes#include <rpc/rpc.h>
621558Srgrimes#include <rpc/pmap_clnt.h>
6374462Salfred#include <rpc/pmap_prot.h>
6474462Salfred#include <rpcsvc/mount.h>
651558Srgrimes#include <nfs/rpcv2.h>
669336Sdfr#include <nfs/nfsproto.h>
6783653Speter#include <nfsserver/nfs.h>
6823681Speter#include <ufs/ufs/ufsmount.h>
6977162Sru#include <fs/msdosfs/msdosfsmount.h>
7077223Sru#include <fs/ntfs/ntfsmount.h>
7123681Speter#include <isofs/cd9660/cd9660_mount.h>	/* XXX need isofs in include */
721558Srgrimes
731558Srgrimes#include <arpa/inet.h>
741558Srgrimes
751558Srgrimes#include <ctype.h>
7637663Scharnier#include <err.h>
771558Srgrimes#include <errno.h>
781558Srgrimes#include <grp.h>
79103949Smike#include <limits.h>
801558Srgrimes#include <netdb.h>
811558Srgrimes#include <pwd.h>
821558Srgrimes#include <signal.h>
831558Srgrimes#include <stdio.h>
841558Srgrimes#include <stdlib.h>
851558Srgrimes#include <string.h>
861558Srgrimes#include <unistd.h>
871558Srgrimes#include "pathnames.h"
881558Srgrimes
891558Srgrimes#ifdef DEBUG
901558Srgrimes#include <stdarg.h>
911558Srgrimes#endif
921558Srgrimes
9374462Salfred#ifndef MOUNTDLOCK
9474462Salfred#define MOUNTDLOCK "/var/run/mountd.lock"
9574462Salfred#endif
9674462Salfred
971558Srgrimes/*
981558Srgrimes * Structures for keeping the mount list and export list
991558Srgrimes */
1001558Srgrimesstruct mountlist {
1011558Srgrimes	struct mountlist *ml_next;
1021558Srgrimes	char	ml_host[RPCMNT_NAMELEN+1];
1031558Srgrimes	char	ml_dirp[RPCMNT_PATHLEN+1];
1041558Srgrimes};
1051558Srgrimes
1061558Srgrimesstruct dirlist {
1071558Srgrimes	struct dirlist	*dp_left;
1081558Srgrimes	struct dirlist	*dp_right;
1091558Srgrimes	int		dp_flag;
1101558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
1111558Srgrimes	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1121558Srgrimes};
1131558Srgrimes/* dp_flag bits */
1141558Srgrimes#define	DP_DEFSET	0x1
1159336Sdfr#define DP_HOSTSET	0x2
1161558Srgrimes
1171558Srgrimesstruct exportlist {
1181558Srgrimes	struct exportlist *ex_next;
1191558Srgrimes	struct dirlist	*ex_dirl;
1201558Srgrimes	struct dirlist	*ex_defdir;
1211558Srgrimes	int		ex_flag;
1221558Srgrimes	fsid_t		ex_fs;
1231558Srgrimes	char		*ex_fsdir;
12427447Sdfr	char		*ex_indexfile;
1251558Srgrimes};
1261558Srgrimes/* ex_flag bits */
1271558Srgrimes#define	EX_LINKED	0x1
1281558Srgrimes
1291558Srgrimesstruct netmsk {
13074462Salfred	struct sockaddr_storage nt_net;
13175801Siedowse	struct sockaddr_storage nt_mask;
13242144Sdfr	char		*nt_name;
1331558Srgrimes};
1341558Srgrimes
1351558Srgrimesunion grouptypes {
13674462Salfred	struct addrinfo *gt_addrinfo;
1371558Srgrimes	struct netmsk	gt_net;
1381558Srgrimes};
1391558Srgrimes
1401558Srgrimesstruct grouplist {
1411558Srgrimes	int gr_type;
1421558Srgrimes	union grouptypes gr_ptr;
1431558Srgrimes	struct grouplist *gr_next;
1441558Srgrimes};
1451558Srgrimes/* Group types */
1461558Srgrimes#define	GT_NULL		0x0
1471558Srgrimes#define	GT_HOST		0x1
1481558Srgrimes#define	GT_NET		0x2
14975641Siedowse#define	GT_DEFAULT	0x3
1507401Swpaul#define GT_IGNORE	0x5
1511558Srgrimes
1521558Srgrimesstruct hostlist {
1539336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1541558Srgrimes	struct grouplist *ht_grp;
1551558Srgrimes	struct hostlist	 *ht_next;
1561558Srgrimes};
1571558Srgrimes
1589336Sdfrstruct fhreturn {
1599336Sdfr	int	fhr_flag;
1609336Sdfr	int	fhr_vers;
1619336Sdfr	nfsfh_t	fhr_fh;
1629336Sdfr};
1639336Sdfr
1641558Srgrimes/* Global defs */
16592882Simpchar	*add_expdir(struct dirlist **, char *, int);
16692882Simpvoid	add_dlist(struct dirlist **, struct dirlist *,
16792882Simp				struct grouplist *, int);
16892882Simpvoid	add_mlist(char *, char *);
16992882Simpint	check_dirpath(char *);
17092882Simpint	check_options(struct dirlist *);
17175801Siedowseint	checkmask(struct sockaddr *sa);
17292882Simpint	chk_host(struct dirlist *, struct sockaddr *, int *, int *);
17375635Siedowsevoid	del_mlist(char *hostp, char *dirp);
17492882Simpstruct dirlist *dirp_search(struct dirlist *, char *);
17592882Simpint	do_mount(struct exportlist *, struct grouplist *, int,
17692882Simp		struct xucred *, char *, int, struct statfs *);
17792882Simpint	do_opt(char **, char **, struct exportlist *, struct grouplist *,
17892882Simp				int *, int *, struct xucred *);
17992882Simpstruct	exportlist *ex_search(fsid_t *);
18092882Simpstruct	exportlist *get_exp(void);
18192882Simpvoid	free_dir(struct dirlist *);
18292882Simpvoid	free_exp(struct exportlist *);
18392882Simpvoid	free_grp(struct grouplist *);
18492882Simpvoid	free_host(struct hostlist *);
18592882Simpvoid	get_exportlist(void);
18692882Simpint	get_host(char *, struct grouplist *, struct grouplist *);
18792882Simpstruct hostlist *get_ht(void);
18892882Simpint	get_line(void);
18992882Simpvoid	get_mountlist(void);
19092882Simpint	get_net(char *, struct netmsk *, int);
19192882Simpvoid	getexp_err(struct exportlist *, struct grouplist *);
19292882Simpstruct grouplist *get_grp(void);
19392882Simpvoid	hang_dirp(struct dirlist *, struct grouplist *,
19492882Simp				struct exportlist *, int);
19575754Siedowsevoid	huphandler(int sig);
19675801Siedowseint	makemask(struct sockaddr_storage *ssp, int bitlen);
19792882Simpvoid	mntsrv(struct svc_req *, SVCXPRT *);
19892882Simpvoid	nextfield(char **, char **);
19992882Simpvoid	out_of_mem(void);
20092882Simpvoid	parsecred(char *, struct xucred *);
201100117Salfredint	put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
20275801Siedowsevoid	*sa_rawaddr(struct sockaddr *sa, int *nbytes);
20375801Siedowseint	sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
20475801Siedowse    struct sockaddr *samask);
20592882Simpint	scan_tree(struct dirlist *, struct sockaddr *);
20692882Simpstatic void usage(void);
20792882Simpint	xdr_dir(XDR *, char *);
20892882Simpint	xdr_explist(XDR *, caddr_t);
209100117Salfredint	xdr_explist_brief(XDR *, caddr_t);
21092882Simpint	xdr_fhs(XDR *, caddr_t);
21192882Simpint	xdr_mlist(XDR *, caddr_t);
21292882Simpvoid	terminate(int);
2131558Srgrimes
2141558Srgrimesstruct exportlist *exphead;
2151558Srgrimesstruct mountlist *mlhead;
2161558Srgrimesstruct grouplist *grphead;
2171558Srgrimeschar exname[MAXPATHLEN];
21872650Sgreenstruct xucred def_anon = {
21991354Sdd	XUCRED_VERSION,
22072650Sgreen	(uid_t)-2,
2211558Srgrimes	1,
22272650Sgreen	{ (gid_t)-2 },
22372650Sgreen	NULL
2241558Srgrimes};
22525087Sdfrint force_v2 = 0;
2269336Sdfrint resvport_only = 1;
2279336Sdfrint dir_only = 1;
22831705Sguidoint log = 0;
22975754Siedowseint got_sighup = 0;
23074462Salfred
2311558Srgrimesint opt_flags;
23274462Salfredstatic int have_v6 = 1;
23374462Salfred#ifdef NI_WITHSCOPEID
23474462Salfredstatic const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID;
23574462Salfred#else
23674462Salfredstatic const int ninumeric = NI_NUMERICHOST;
23774462Salfred#endif
23874462Salfred
23974462Salfredint mountdlockfd;
24075801Siedowse/* Bits for opt_flags above */
2411558Srgrimes#define	OP_MAPROOT	0x01
2421558Srgrimes#define	OP_MAPALL	0x02
24383653Speter/* 0x4 free */
2441558Srgrimes#define	OP_MASK		0x08
2451558Srgrimes#define	OP_NET		0x10
2461558Srgrimes#define	OP_ALLDIRS	0x40
24775801Siedowse#define	OP_HAVEMASK	0x80	/* A mask was specified or inferred. */
248100336Sjoerg#define	OP_QUIET	0x100
24974462Salfred#define OP_MASKLEN	0x200
2501558Srgrimes
2511558Srgrimes#ifdef DEBUG
2521558Srgrimesint debug = 1;
25392882Simpvoid	SYSLOG(int, const char *, ...) __printflike(2, 3);
2541558Srgrimes#define syslog SYSLOG
2551558Srgrimes#else
2561558Srgrimesint debug = 0;
2571558Srgrimes#endif
2581558Srgrimes
2591558Srgrimes/*
2601558Srgrimes * Mountd server for NFS mount protocol as described in:
2611558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2621558Srgrimes * The optional arguments are the exports file name
2631558Srgrimes * default: _PATH_EXPORTS
2641558Srgrimes * and "-n" to allow nonroot mount.
2651558Srgrimes */
2661558Srgrimesint
2671558Srgrimesmain(argc, argv)
2681558Srgrimes	int argc;
2691558Srgrimes	char **argv;
2701558Srgrimes{
27175754Siedowse	fd_set readfds;
27274462Salfred	SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
27374462Salfred	struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
27474462Salfred	int udpsock, tcpsock, udp6sock, tcp6sock;
27574462Salfred	int xcreated = 0, s;
27674462Salfred	int one = 1;
27796622Siedowse	int c;
2781558Srgrimes
27975635Siedowse	udp6conf = tcp6conf = NULL;
28075635Siedowse	udp6sock = tcp6sock = NULL;
28175635Siedowse
28274462Salfred	/* Check that another mountd isn't already running. */
28374462Salfred	if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
28474462Salfred		err(1, "%s", MOUNTDLOCK);
28574462Salfred
28674462Salfred	if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
287105267Scharnier		errx(1, "another mountd is already running. Aborting");
28874462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
28974462Salfred	if (s < 0)
29074462Salfred		have_v6 = 0;
29174462Salfred	else
29274462Salfred		close(s);
29383687Speter	if (modfind("nfsserver") < 0) {
29483687Speter		/* Not present in kernel, try loading it */
29583687Speter		if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
29683687Speter			errx(1, "NFS server is not available or loadable");
2972999Swollman	}
2982999Swollman
29931665Sguido	while ((c = getopt(argc, argv, "2dlnr")) != -1)
3001558Srgrimes		switch (c) {
30125087Sdfr		case '2':
30225087Sdfr			force_v2 = 1;
30325087Sdfr			break;
3049336Sdfr		case 'n':
3059336Sdfr			resvport_only = 0;
3069336Sdfr			break;
3079336Sdfr		case 'r':
3089336Sdfr			dir_only = 0;
3099336Sdfr			break;
3108688Sphk		case 'd':
3118688Sphk			debug = debug ? 0 : 1;
3128688Sphk			break;
31331656Sguido		case 'l':
31431656Sguido			log = 1;
31531656Sguido			break;
3161558Srgrimes		default:
31737663Scharnier			usage();
3181558Srgrimes		};
3191558Srgrimes	argc -= optind;
3201558Srgrimes	argv += optind;
3211558Srgrimes	grphead = (struct grouplist *)NULL;
3221558Srgrimes	exphead = (struct exportlist *)NULL;
3231558Srgrimes	mlhead = (struct mountlist *)NULL;
3241558Srgrimes	if (argc == 1) {
3251558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
3261558Srgrimes		exname[MAXPATHLEN-1] = '\0';
3271558Srgrimes	} else
3281558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3291558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3301558Srgrimes	if (debug)
33137663Scharnier		warnx("getting export list");
3321558Srgrimes	get_exportlist();
3331558Srgrimes	if (debug)
33437663Scharnier		warnx("getting mount list");
3351558Srgrimes	get_mountlist();
3361558Srgrimes	if (debug)
33737663Scharnier		warnx("here we go");
3381558Srgrimes	if (debug == 0) {
3391558Srgrimes		daemon(0, 0);
3401558Srgrimes		signal(SIGINT, SIG_IGN);
3411558Srgrimes		signal(SIGQUIT, SIG_IGN);
3421558Srgrimes	}
34375754Siedowse	signal(SIGHUP, huphandler);
34474462Salfred	signal(SIGTERM, terminate);
3451558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3461558Srgrimes	  if (pidfile != NULL) {
3471558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3481558Srgrimes		fclose(pidfile);
3491558Srgrimes	  }
3501558Srgrimes	}
35174462Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
35274462Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
35374462Salfred	udpsock  = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
35474462Salfred	tcpsock  = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
35574791Salfred	udpconf  = getnetconfigent("udp");
35674791Salfred	tcpconf  = getnetconfigent("tcp");
35774791Salfred	if (!have_v6)
35874791Salfred		goto skip_v6;
35974462Salfred	udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
36074462Salfred	tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
36174462Salfred	/*
36274462Salfred	 * We're doing host-based access checks here, so don't allow
36374462Salfred	 * v4-in-v6 to confuse things. The kernel will disable it
36474462Salfred	 * by default on NFS sockets too.
36574462Salfred	 */
36674462Salfred	if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
367100505Sume		IPV6_V6ONLY, &one, sizeof one) < 0){
36874462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
36974462Salfred		exit(1);
37074462Salfred	}
37174462Salfred	if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
372100505Sume		IPV6_V6ONLY, &one, sizeof one) < 0){
37374462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
37474462Salfred		exit(1);
37574462Salfred	}
37674462Salfred	udp6conf = getnetconfigent("udp6");
37774462Salfred	tcp6conf = getnetconfigent("tcp6");
37874791Salfred
37974791Salfredskip_v6:
38024759Sguido	if (!resvport_only) {
38183687Speter		if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
38283687Speter		    &resvport_only, sizeof(resvport_only)) != 0 &&
38383687Speter		    errno != ENOENT) {
38424759Sguido			syslog(LOG_ERR, "sysctl: %m");
38524759Sguido			exit(1);
38624759Sguido		}
38724330Sguido	}
38874462Salfred	if (udpsock != -1 && udpconf != NULL) {
38974462Salfred		bindresvport(udpsock, NULL);
39074462Salfred		udptransp = svc_dg_create(udpsock, 0, 0);
39174462Salfred		if (udptransp != NULL) {
39274462Salfred			if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
39374462Salfred			    mntsrv, udpconf))
39474462Salfred				syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service");
39574462Salfred			else
39674462Salfred				xcreated++;
39774462Salfred			if (!force_v2) {
39874462Salfred				if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
39974462Salfred				    mntsrv, udpconf))
40074462Salfred					syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service");
40174462Salfred				else
40274462Salfred					xcreated++;
40374462Salfred			}
40474462Salfred		} else
40574462Salfred			syslog(LOG_WARNING, "can't create UDP services");
40674462Salfred
40774462Salfred	}
40874462Salfred	if (tcpsock != -1 && tcpconf != NULL) {
40974462Salfred		bindresvport(tcpsock, NULL);
41074462Salfred		listen(tcpsock, SOMAXCONN);
41174462Salfred		tcptransp = svc_vc_create(tcpsock, 0, 0);
41274462Salfred		if (tcptransp != NULL) {
41374462Salfred			if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
41474462Salfred			    mntsrv, tcpconf))
41574462Salfred				syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service");
41674462Salfred			else
41774462Salfred				xcreated++;
41874462Salfred			if (!force_v2) {
41974462Salfred				if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
42074462Salfred				    mntsrv, tcpconf))
42174462Salfred					syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service");
42274462Salfred				else
42374462Salfred					xcreated++;
42474462Salfred			}
42574462Salfred		} else
42674462Salfred			syslog(LOG_WARNING, "can't create TCP service");
42774462Salfred
42874462Salfred	}
42974791Salfred	if (have_v6 && udp6sock != -1 && udp6conf != NULL) {
43074462Salfred		bindresvport(udp6sock, NULL);
43174462Salfred		udp6transp = svc_dg_create(udp6sock, 0, 0);
43274462Salfred		if (udp6transp != NULL) {
43374462Salfred			if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
43474462Salfred			    mntsrv, udp6conf))
43574462Salfred				syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service");
43674462Salfred			else
43774462Salfred				xcreated++;
43874462Salfred			if (!force_v2) {
43974462Salfred				if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
44074462Salfred				    mntsrv, udp6conf))
44174462Salfred					syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service");
44274462Salfred				else
44374462Salfred					xcreated++;
44474462Salfred			}
44574462Salfred		} else
44674462Salfred			syslog(LOG_WARNING, "can't create UDP6 service");
44774462Salfred
44874462Salfred	}
44974791Salfred	if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) {
45074462Salfred		bindresvport(tcp6sock, NULL);
45174462Salfred		listen(tcp6sock, SOMAXCONN);
45274462Salfred		tcp6transp = svc_vc_create(tcp6sock, 0, 0);
45374462Salfred		if (tcp6transp != NULL) {
45474462Salfred			if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
45574462Salfred			    mntsrv, tcp6conf))
45674462Salfred				syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service");
45774462Salfred			else
45874462Salfred				xcreated++;
45974462Salfred			if (!force_v2) {
46074462Salfred				if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
46174462Salfred				    mntsrv, tcp6conf))
46274462Salfred					syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service");
46374462Salfred					else
46474462Salfred						xcreated++;
46574462Salfred				}
46674462Salfred		} else
46774462Salfred			syslog(LOG_WARNING, "can't create TCP6 service");
46874462Salfred
46974462Salfred	}
47074462Salfred	if (xcreated == 0) {
47174462Salfred		syslog(LOG_ERR, "could not create any services");
4721558Srgrimes		exit(1);
4731558Srgrimes	}
47475754Siedowse
47575754Siedowse	/* Expand svc_run() here so that we can call get_exportlist(). */
47675754Siedowse	for (;;) {
47775754Siedowse		if (got_sighup) {
47875754Siedowse			get_exportlist();
47975754Siedowse			got_sighup = 0;
48075754Siedowse		}
48175754Siedowse		readfds = svc_fdset;
48275754Siedowse		switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
48375754Siedowse		case -1:
48475754Siedowse			if (errno == EINTR)
48575754Siedowse                                continue;
48675754Siedowse			syslog(LOG_ERR, "mountd died: select: %m");
48775754Siedowse			exit(1);
48875754Siedowse		case 0:
48975754Siedowse			continue;
49075754Siedowse		default:
49175754Siedowse			svc_getreqset(&readfds);
49275754Siedowse		}
49375754Siedowse	}
4941558Srgrimes}
4951558Srgrimes
49637663Scharnierstatic void
49737663Scharnierusage()
49837663Scharnier{
49937663Scharnier	fprintf(stderr,
50037663Scharnier		"usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
50137663Scharnier	exit(1);
50237663Scharnier}
50337663Scharnier
5041558Srgrimes/*
5051558Srgrimes * The mount rpc service
5061558Srgrimes */
5071558Srgrimesvoid
5081558Srgrimesmntsrv(rqstp, transp)
5091558Srgrimes	struct svc_req *rqstp;
5101558Srgrimes	SVCXPRT *transp;
5111558Srgrimes{
5121558Srgrimes	struct exportlist *ep;
5131558Srgrimes	struct dirlist *dp;
5149336Sdfr	struct fhreturn fhr;
5151558Srgrimes	struct stat stb;
5161558Srgrimes	struct statfs fsb;
51774462Salfred	struct addrinfo *ai;
51874462Salfred	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
51974462Salfred	int lookup_failed = 1;
52074462Salfred	struct sockaddr *saddr;
5219336Sdfr	u_short sport;
52223681Speter	char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
52328911Sguido	int bad = 0, defset, hostset;
5249336Sdfr	sigset_t sighup_mask;
5251558Srgrimes
5269336Sdfr	sigemptyset(&sighup_mask);
5279336Sdfr	sigaddset(&sighup_mask, SIGHUP);
52874462Salfred	saddr = svc_getrpccaller(transp)->buf;
52974462Salfred	switch (saddr->sa_family) {
53074462Salfred	case AF_INET6:
53175635Siedowse		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
53274462Salfred		break;
53374462Salfred	case AF_INET:
53475635Siedowse		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
53574462Salfred		break;
53674462Salfred	default:
53774462Salfred		syslog(LOG_ERR, "request from unknown address family");
53874462Salfred		return;
53974462Salfred	}
54074462Salfred	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
54174462Salfred	    NULL, 0, 0);
54274462Salfred	getnameinfo(saddr, saddr->sa_len, numerichost,
54374462Salfred	    sizeof numerichost, NULL, 0, NI_NUMERICHOST);
54474462Salfred	ai = NULL;
5451558Srgrimes	switch (rqstp->rq_proc) {
5461558Srgrimes	case NULLPROC:
547100117Salfred		if (!svc_sendreply(transp, xdr_void, NULL))
54837663Scharnier			syslog(LOG_ERR, "can't send reply");
5491558Srgrimes		return;
5501558Srgrimes	case RPCMNT_MOUNT:
5519336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
55231656Sguido			syslog(LOG_NOTICE,
55331656Sguido			    "mount request from %s from unprivileged port",
55474462Salfred			    numerichost);
5551558Srgrimes			svcerr_weakauth(transp);
5561558Srgrimes			return;
5571558Srgrimes		}
5581558Srgrimes		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
55931656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
56074462Salfred			    numerichost);
5611558Srgrimes			svcerr_decode(transp);
5621558Srgrimes			return;
5631558Srgrimes		}
5641558Srgrimes
5651558Srgrimes		/*
5661558Srgrimes		 * Get the real pathname and make sure it is a directory
5679336Sdfr		 * or a regular file if the -r option was specified
5689336Sdfr		 * and it exists.
5691558Srgrimes		 */
57051968Salfred		if (realpath(rpcpath, dirpath) == NULL ||
5711558Srgrimes		    stat(dirpath, &stb) < 0 ||
5729336Sdfr		    (!S_ISDIR(stb.st_mode) &&
57374462Salfred		    (dir_only || !S_ISREG(stb.st_mode))) ||
5741558Srgrimes		    statfs(dirpath, &fsb) < 0) {
5751558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
57631656Sguido			syslog(LOG_NOTICE,
57737663Scharnier			    "mount request from %s for non existent path %s",
57874462Salfred			    numerichost, dirpath);
5791558Srgrimes			if (debug)
58037663Scharnier				warnx("stat failed on %s", dirpath);
58128911Sguido			bad = ENOENT;	/* We will send error reply later */
5821558Srgrimes		}
5831558Srgrimes
5841558Srgrimes		/* Check in the exports list */
5859336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
5861558Srgrimes		ep = ex_search(&fsb.f_fsid);
5879336Sdfr		hostset = defset = 0;
5889336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
5891558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
59074462Salfred		      chk_host(dp, saddr, &defset, &hostset)) ||
59174462Salfred		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
59274462Salfred		     scan_tree(ep->ex_dirl, saddr) == 0))) {
59328911Sguido			if (bad) {
59428911Sguido				if (!svc_sendreply(transp, xdr_long,
59528911Sguido				    (caddr_t)&bad))
59637663Scharnier					syslog(LOG_ERR, "can't send reply");
59728911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
59828911Sguido				return;
59928911Sguido			}
6009336Sdfr			if (hostset & DP_HOSTSET)
6019336Sdfr				fhr.fhr_flag = hostset;
6029336Sdfr			else
6039336Sdfr				fhr.fhr_flag = defset;
6049336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
6051558Srgrimes			/* Get the file handle */
60623681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
6079336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
6081558Srgrimes				bad = errno;
60937663Scharnier				syslog(LOG_ERR, "can't get fh for %s", dirpath);
6101558Srgrimes				if (!svc_sendreply(transp, xdr_long,
6111558Srgrimes				    (caddr_t)&bad))
61237663Scharnier					syslog(LOG_ERR, "can't send reply");
6139336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6141558Srgrimes				return;
6151558Srgrimes			}
6169336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
61737663Scharnier				syslog(LOG_ERR, "can't send reply");
61874462Salfred			if (!lookup_failed)
61974462Salfred				add_mlist(host, dirpath);
6201558Srgrimes			else
62174462Salfred				add_mlist(numerichost, dirpath);
6221558Srgrimes			if (debug)
62337663Scharnier				warnx("mount successful");
62431656Sguido			if (log)
62531656Sguido				syslog(LOG_NOTICE,
62631656Sguido				    "mount request succeeded from %s for %s",
62774462Salfred				    numerichost, dirpath);
62831656Sguido		} else {
6291558Srgrimes			bad = EACCES;
63031656Sguido			syslog(LOG_NOTICE,
63131656Sguido			    "mount request denied from %s for %s",
63274462Salfred			    numerichost, dirpath);
63331656Sguido		}
63428911Sguido
63528911Sguido		if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
63637663Scharnier			syslog(LOG_ERR, "can't send reply");
6379336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6381558Srgrimes		return;
6391558Srgrimes	case RPCMNT_DUMP:
6401558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
64137663Scharnier			syslog(LOG_ERR, "can't send reply");
64231656Sguido		else if (log)
64331656Sguido			syslog(LOG_NOTICE,
64431656Sguido			    "dump request succeeded from %s",
64574462Salfred			    numerichost);
6461558Srgrimes		return;
6471558Srgrimes	case RPCMNT_UMOUNT:
6489336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
64931656Sguido			syslog(LOG_NOTICE,
65031656Sguido			    "umount request from %s from unprivileged port",
65174462Salfred			    numerichost);
6521558Srgrimes			svcerr_weakauth(transp);
6531558Srgrimes			return;
6541558Srgrimes		}
65551968Salfred		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
65631656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
65774462Salfred			    numerichost);
6581558Srgrimes			svcerr_decode(transp);
6591558Srgrimes			return;
6601558Srgrimes		}
66151968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
66251968Salfred			syslog(LOG_NOTICE, "umount request from %s "
66351968Salfred			    "for non existent path %s",
66474462Salfred			    numerichost, dirpath);
66551968Salfred		}
6661558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
66737663Scharnier			syslog(LOG_ERR, "can't send reply");
66874462Salfred		if (!lookup_failed)
66975635Siedowse			del_mlist(host, dirpath);
67075635Siedowse		del_mlist(numerichost, dirpath);
67131656Sguido		if (log)
67231656Sguido			syslog(LOG_NOTICE,
67331656Sguido			    "umount request succeeded from %s for %s",
67474462Salfred			    numerichost, dirpath);
6751558Srgrimes		return;
6761558Srgrimes	case RPCMNT_UMNTALL:
6779336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
67831656Sguido			syslog(LOG_NOTICE,
67931656Sguido			    "umountall request from %s from unprivileged port",
68074462Salfred			    numerichost);
6811558Srgrimes			svcerr_weakauth(transp);
6821558Srgrimes			return;
6831558Srgrimes		}
6841558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
68537663Scharnier			syslog(LOG_ERR, "can't send reply");
68674462Salfred		if (!lookup_failed)
68775635Siedowse			del_mlist(host, NULL);
68875635Siedowse		del_mlist(numerichost, NULL);
68931656Sguido		if (log)
69031656Sguido			syslog(LOG_NOTICE,
69131656Sguido			    "umountall request succeeded from %s",
69274462Salfred			    numerichost);
6931558Srgrimes		return;
6941558Srgrimes	case RPCMNT_EXPORT:
6951558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
696100117Salfred			if (!svc_sendreply(transp, xdr_explist_brief, (caddr_t)NULL))
697100117Salfred				syslog(LOG_ERR, "can't send reply");
69831656Sguido		if (log)
69931656Sguido			syslog(LOG_NOTICE,
70031656Sguido			    "export request succeeded from %s",
70174462Salfred			    numerichost);
7021558Srgrimes		return;
7031558Srgrimes	default:
7041558Srgrimes		svcerr_noproc(transp);
7051558Srgrimes		return;
7061558Srgrimes	}
7071558Srgrimes}
7081558Srgrimes
7091558Srgrimes/*
7101558Srgrimes * Xdr conversion for a dirpath string
7111558Srgrimes */
7121558Srgrimesint
7131558Srgrimesxdr_dir(xdrsp, dirp)
7141558Srgrimes	XDR *xdrsp;
7151558Srgrimes	char *dirp;
7161558Srgrimes{
7171558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
7181558Srgrimes}
7191558Srgrimes
7201558Srgrimes/*
7219336Sdfr * Xdr routine to generate file handle reply
7221558Srgrimes */
7231558Srgrimesint
7249336Sdfrxdr_fhs(xdrsp, cp)
7251558Srgrimes	XDR *xdrsp;
7269336Sdfr	caddr_t cp;
7271558Srgrimes{
72892806Sobrien	struct fhreturn *fhrp = (struct fhreturn *)cp;
7299336Sdfr	u_long ok = 0, len, auth;
7301558Srgrimes
7311558Srgrimes	if (!xdr_long(xdrsp, &ok))
7321558Srgrimes		return (0);
7339336Sdfr	switch (fhrp->fhr_vers) {
7349336Sdfr	case 1:
7359336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
7369336Sdfr	case 3:
7379336Sdfr		len = NFSX_V3FH;
7389336Sdfr		if (!xdr_long(xdrsp, &len))
7399336Sdfr			return (0);
7409336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
7419336Sdfr			return (0);
74283653Speter		auth = RPCAUTH_UNIX;
7439336Sdfr		len = 1;
7449336Sdfr		if (!xdr_long(xdrsp, &len))
7459336Sdfr			return (0);
7469336Sdfr		return (xdr_long(xdrsp, &auth));
7479336Sdfr	};
7489336Sdfr	return (0);
7491558Srgrimes}
7501558Srgrimes
7511558Srgrimesint
7521558Srgrimesxdr_mlist(xdrsp, cp)
7531558Srgrimes	XDR *xdrsp;
7541558Srgrimes	caddr_t cp;
7551558Srgrimes{
7561558Srgrimes	struct mountlist *mlp;
7571558Srgrimes	int true = 1;
7581558Srgrimes	int false = 0;
7591558Srgrimes	char *strp;
7601558Srgrimes
7611558Srgrimes	mlp = mlhead;
7621558Srgrimes	while (mlp) {
7631558Srgrimes		if (!xdr_bool(xdrsp, &true))
7641558Srgrimes			return (0);
7651558Srgrimes		strp = &mlp->ml_host[0];
7661558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
7671558Srgrimes			return (0);
7681558Srgrimes		strp = &mlp->ml_dirp[0];
7691558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
7701558Srgrimes			return (0);
7711558Srgrimes		mlp = mlp->ml_next;
7721558Srgrimes	}
7731558Srgrimes	if (!xdr_bool(xdrsp, &false))
7741558Srgrimes		return (0);
7751558Srgrimes	return (1);
7761558Srgrimes}
7771558Srgrimes
7781558Srgrimes/*
7791558Srgrimes * Xdr conversion for export list
7801558Srgrimes */
7811558Srgrimesint
782100117Salfredxdr_explist_common(xdrsp, cp, brief)
7831558Srgrimes	XDR *xdrsp;
7841558Srgrimes	caddr_t cp;
785100117Salfred	int brief;
7861558Srgrimes{
7871558Srgrimes	struct exportlist *ep;
7881558Srgrimes	int false = 0;
7899336Sdfr	int putdef;
7909336Sdfr	sigset_t sighup_mask;
7911558Srgrimes
7929336Sdfr	sigemptyset(&sighup_mask);
7939336Sdfr	sigaddset(&sighup_mask, SIGHUP);
7949336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
7951558Srgrimes	ep = exphead;
7961558Srgrimes	while (ep) {
7971558Srgrimes		putdef = 0;
798100117Salfred		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
799100117Salfred			       &putdef, brief))
8001558Srgrimes			goto errout;
8011558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
8021558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
803100117Salfred			&putdef, brief))
8041558Srgrimes			goto errout;
8051558Srgrimes		ep = ep->ex_next;
8061558Srgrimes	}
8079336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8081558Srgrimes	if (!xdr_bool(xdrsp, &false))
8091558Srgrimes		return (0);
8101558Srgrimes	return (1);
8111558Srgrimeserrout:
8129336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8131558Srgrimes	return (0);
8141558Srgrimes}
8151558Srgrimes
8161558Srgrimes/*
8171558Srgrimes * Called from xdr_explist() to traverse the tree and export the
8181558Srgrimes * directory paths.
8191558Srgrimes */
8201558Srgrimesint
821100117Salfredput_exlist(dp, xdrsp, adp, putdefp, brief)
8221558Srgrimes	struct dirlist *dp;
8231558Srgrimes	XDR *xdrsp;
8241558Srgrimes	struct dirlist *adp;
8251558Srgrimes	int *putdefp;
826100117Salfred	int brief;
8271558Srgrimes{
8281558Srgrimes	struct grouplist *grp;
8291558Srgrimes	struct hostlist *hp;
8301558Srgrimes	int true = 1;
8311558Srgrimes	int false = 0;
8321558Srgrimes	int gotalldir = 0;
8331558Srgrimes	char *strp;
8341558Srgrimes
8351558Srgrimes	if (dp) {
836100117Salfred		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
8371558Srgrimes			return (1);
8381558Srgrimes		if (!xdr_bool(xdrsp, &true))
8391558Srgrimes			return (1);
8401558Srgrimes		strp = dp->dp_dirp;
8411558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
8421558Srgrimes			return (1);
8431558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
8441558Srgrimes			gotalldir = 1;
8451558Srgrimes			*putdefp = 1;
8461558Srgrimes		}
847100117Salfred		if (brief) {
848100117Salfred			if (!xdr_bool(xdrsp, &true))
849100117Salfred				return (1);
850100117Salfred			strp = "(...)";
851100117Salfred			if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
852100117Salfred				return (1);
853100117Salfred		} else if ((dp->dp_flag & DP_DEFSET) == 0 &&
8541558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
8551558Srgrimes			hp = dp->dp_hosts;
8561558Srgrimes			while (hp) {
8571558Srgrimes				grp = hp->ht_grp;
8581558Srgrimes				if (grp->gr_type == GT_HOST) {
8591558Srgrimes					if (!xdr_bool(xdrsp, &true))
8601558Srgrimes						return (1);
86174462Salfred					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
8628871Srgrimes					if (!xdr_string(xdrsp, &strp,
8631558Srgrimes					    RPCMNT_NAMELEN))
8641558Srgrimes						return (1);
8651558Srgrimes				} else if (grp->gr_type == GT_NET) {
8661558Srgrimes					if (!xdr_bool(xdrsp, &true))
8671558Srgrimes						return (1);
8681558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
8698871Srgrimes					if (!xdr_string(xdrsp, &strp,
8701558Srgrimes					    RPCMNT_NAMELEN))
8711558Srgrimes						return (1);
8721558Srgrimes				}
8731558Srgrimes				hp = hp->ht_next;
8741558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
8751558Srgrimes					hp = adp->dp_hosts;
8761558Srgrimes					gotalldir = 0;
8771558Srgrimes				}
8781558Srgrimes			}
8791558Srgrimes		}
8801558Srgrimes		if (!xdr_bool(xdrsp, &false))
8811558Srgrimes			return (1);
882100117Salfred		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
8831558Srgrimes			return (1);
8841558Srgrimes	}
8851558Srgrimes	return (0);
8861558Srgrimes}
8871558Srgrimes
888100117Salfredint
889100117Salfredxdr_explist(xdrsp, cp)
890100117Salfred	XDR *xdrsp;
891100117Salfred	caddr_t cp;
892100117Salfred{
893100117Salfred
894100117Salfred	return xdr_explist_common(xdrsp, cp, 0);
895100117Salfred}
896100117Salfred
897100117Salfredint
898100117Salfredxdr_explist_brief(xdrsp, cp)
899100117Salfred	XDR *xdrsp;
900100117Salfred	caddr_t cp;
901100117Salfred{
902100117Salfred
903100117Salfred	return xdr_explist_common(xdrsp, cp, 1);
904100117Salfred}
905100117Salfred
90696622Siedowsechar *line;
90796622Siedowseint linesize;
9081558SrgrimesFILE *exp_file;
9091558Srgrimes
9101558Srgrimes/*
9111558Srgrimes * Get the export list
9121558Srgrimes */
9131558Srgrimesvoid
9141558Srgrimesget_exportlist()
9151558Srgrimes{
9161558Srgrimes	struct exportlist *ep, *ep2;
9171558Srgrimes	struct grouplist *grp, *tgrp;
9181558Srgrimes	struct exportlist **epp;
9191558Srgrimes	struct dirlist *dirhead;
9201558Srgrimes	struct statfs fsb, *fsp;
92172650Sgreen	struct xucred anon;
9221558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
9231558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
9241558Srgrimes
92551968Salfred	dirp = NULL;
92651968Salfred	dirplen = 0;
92751968Salfred
9281558Srgrimes	/*
9291558Srgrimes	 * First, get rid of the old list
9301558Srgrimes	 */
9311558Srgrimes	ep = exphead;
9321558Srgrimes	while (ep) {
9331558Srgrimes		ep2 = ep;
9341558Srgrimes		ep = ep->ex_next;
9351558Srgrimes		free_exp(ep2);
9361558Srgrimes	}
9371558Srgrimes	exphead = (struct exportlist *)NULL;
9381558Srgrimes
9391558Srgrimes	grp = grphead;
9401558Srgrimes	while (grp) {
9411558Srgrimes		tgrp = grp;
9421558Srgrimes		grp = grp->gr_next;
9431558Srgrimes		free_grp(tgrp);
9441558Srgrimes	}
9451558Srgrimes	grphead = (struct grouplist *)NULL;
9461558Srgrimes
9471558Srgrimes	/*
9481558Srgrimes	 * And delete exports that are in the kernel for all local
94996707Strhodes	 * filesystems.
95096707Strhodes	 * XXX: Should know how to handle all local exportable filesystems
95123681Speter	 *      instead of just "ufs".
9521558Srgrimes	 */
9531558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
9541558Srgrimes	for (i = 0; i < num; i++) {
9551558Srgrimes		union {
9561558Srgrimes			struct ufs_args ua;
9571558Srgrimes			struct iso_args ia;
9589336Sdfr			struct msdosfs_args da;
95954093Ssemenu			struct ntfs_args na;
9601558Srgrimes		} targs;
9611558Srgrimes
96277435Sphk		if (!strcmp(fsp->f_fstypename, "ufs") ||
96377577Sru		    !strcmp(fsp->f_fstypename, "msdosfs") ||
96454093Ssemenu		    !strcmp(fsp->f_fstypename, "ntfs") ||
96523681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
9669336Sdfr			targs.ua.fspec = NULL;
9679336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
9689336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
96977405Siedowse			    fsp->f_flags | MNT_UPDATE, (caddr_t)&targs) < 0 &&
97077405Siedowse			    errno != ENOENT)
97177405Siedowse				syslog(LOG_ERR,
97277405Siedowse				    "can't delete exports for %s: %m",
97374462Salfred				    fsp->f_mntonname);
9741558Srgrimes		}
9751558Srgrimes		fsp++;
9761558Srgrimes	}
9771558Srgrimes
9781558Srgrimes	/*
9791558Srgrimes	 * Read in the exports file and build the list, calling
9801558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
9811558Srgrimes	 */
9821558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
98337663Scharnier		syslog(LOG_ERR, "can't open %s", exname);
9841558Srgrimes		exit(2);
9851558Srgrimes	}
9861558Srgrimes	dirhead = (struct dirlist *)NULL;
9871558Srgrimes	while (get_line()) {
9881558Srgrimes		if (debug)
98937663Scharnier			warnx("got line %s", line);
9901558Srgrimes		cp = line;
9911558Srgrimes		nextfield(&cp, &endcp);
9921558Srgrimes		if (*cp == '#')
9931558Srgrimes			goto nextline;
9941558Srgrimes
9951558Srgrimes		/*
9961558Srgrimes		 * Set defaults.
9971558Srgrimes		 */
9981558Srgrimes		has_host = FALSE;
9991558Srgrimes		anon = def_anon;
10001558Srgrimes		exflags = MNT_EXPORTED;
10011558Srgrimes		got_nondir = 0;
10021558Srgrimes		opt_flags = 0;
10031558Srgrimes		ep = (struct exportlist *)NULL;
10041558Srgrimes
10051558Srgrimes		/*
10061558Srgrimes		 * Create new exports list entry
10071558Srgrimes		 */
10081558Srgrimes		len = endcp-cp;
10091558Srgrimes		tgrp = grp = get_grp();
10101558Srgrimes		while (len > 0) {
10111558Srgrimes			if (len > RPCMNT_NAMELEN) {
10121558Srgrimes			    getexp_err(ep, tgrp);
10131558Srgrimes			    goto nextline;
10141558Srgrimes			}
10151558Srgrimes			if (*cp == '-') {
10161558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10171558Srgrimes				getexp_err(ep, tgrp);
10181558Srgrimes				goto nextline;
10191558Srgrimes			    }
10201558Srgrimes			    if (debug)
102137663Scharnier				warnx("doing opt %s", cp);
10221558Srgrimes			    got_nondir = 1;
10231558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
10241558Srgrimes				&exflags, &anon)) {
10251558Srgrimes				getexp_err(ep, tgrp);
10261558Srgrimes				goto nextline;
10271558Srgrimes			    }
10281558Srgrimes			} else if (*cp == '/') {
10291558Srgrimes			    savedc = *endcp;
10301558Srgrimes			    *endcp = '\0';
10311558Srgrimes			    if (check_dirpath(cp) &&
10321558Srgrimes				statfs(cp, &fsb) >= 0) {
10331558Srgrimes				if (got_nondir) {
103437663Scharnier				    syslog(LOG_ERR, "dirs must be first");
10351558Srgrimes				    getexp_err(ep, tgrp);
10361558Srgrimes				    goto nextline;
10371558Srgrimes				}
10381558Srgrimes				if (ep) {
10391558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
10401558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
10411558Srgrimes					getexp_err(ep, tgrp);
10421558Srgrimes					goto nextline;
10431558Srgrimes				    }
10441558Srgrimes				} else {
10451558Srgrimes				    /*
10461558Srgrimes				     * See if this directory is already
10471558Srgrimes				     * in the list.
10481558Srgrimes				     */
10491558Srgrimes				    ep = ex_search(&fsb.f_fsid);
10501558Srgrimes				    if (ep == (struct exportlist *)NULL) {
10511558Srgrimes					ep = get_exp();
10521558Srgrimes					ep->ex_fs = fsb.f_fsid;
10531558Srgrimes					ep->ex_fsdir = (char *)
10541558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
10551558Srgrimes					if (ep->ex_fsdir)
10561558Srgrimes					    strcpy(ep->ex_fsdir,
10571558Srgrimes						fsb.f_mntonname);
10581558Srgrimes					else
10591558Srgrimes					    out_of_mem();
10601558Srgrimes					if (debug)
106174462Salfred						warnx("making new ep fs=0x%x,0x%x",
106274462Salfred						    fsb.f_fsid.val[0],
106374462Salfred						    fsb.f_fsid.val[1]);
10641558Srgrimes				    } else if (debug)
106537663Scharnier					warnx("found ep fs=0x%x,0x%x",
10661558Srgrimes					    fsb.f_fsid.val[0],
10671558Srgrimes					    fsb.f_fsid.val[1]);
10681558Srgrimes				}
10691558Srgrimes
10701558Srgrimes				/*
10711558Srgrimes				 * Add dirpath to export mount point.
10721558Srgrimes				 */
10731558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
10741558Srgrimes				dirplen = len;
10751558Srgrimes			    } else {
10761558Srgrimes				getexp_err(ep, tgrp);
10771558Srgrimes				goto nextline;
10781558Srgrimes			    }
10791558Srgrimes			    *endcp = savedc;
10801558Srgrimes			} else {
10811558Srgrimes			    savedc = *endcp;
10821558Srgrimes			    *endcp = '\0';
10831558Srgrimes			    got_nondir = 1;
10841558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10851558Srgrimes				getexp_err(ep, tgrp);
10861558Srgrimes				goto nextline;
10871558Srgrimes			    }
10881558Srgrimes
10891558Srgrimes			    /*
10901558Srgrimes			     * Get the host or netgroup.
10911558Srgrimes			     */
10921558Srgrimes			    setnetgrent(cp);
10931558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
10941558Srgrimes			    do {
10951558Srgrimes				if (has_host) {
10961558Srgrimes				    grp->gr_next = get_grp();
10971558Srgrimes				    grp = grp->gr_next;
10981558Srgrimes				}
10991558Srgrimes				if (netgrp) {
110037003Sjoerg				    if (hst == 0) {
110137663Scharnier					syslog(LOG_ERR,
110237663Scharnier				"null hostname in netgroup %s, skipping", cp);
110337004Sjoerg					grp->gr_type = GT_IGNORE;
110437003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
110537663Scharnier					syslog(LOG_ERR,
110637663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
110729317Sjlemon					grp->gr_type = GT_IGNORE;
11081558Srgrimes				    }
11097401Swpaul				} else if (get_host(cp, grp, tgrp)) {
111037663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
111129317Sjlemon				    grp->gr_type = GT_IGNORE;
11121558Srgrimes				}
11131558Srgrimes				has_host = TRUE;
11141558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
11151558Srgrimes			    endnetgrent();
11161558Srgrimes			    *endcp = savedc;
11171558Srgrimes			}
11181558Srgrimes			cp = endcp;
11191558Srgrimes			nextfield(&cp, &endcp);
11201558Srgrimes			len = endcp - cp;
11211558Srgrimes		}
11221558Srgrimes		if (check_options(dirhead)) {
11231558Srgrimes			getexp_err(ep, tgrp);
11241558Srgrimes			goto nextline;
11251558Srgrimes		}
11261558Srgrimes		if (!has_host) {
112775641Siedowse			grp->gr_type = GT_DEFAULT;
11281558Srgrimes			if (debug)
112937663Scharnier				warnx("adding a default entry");
11301558Srgrimes
11311558Srgrimes		/*
11321558Srgrimes		 * Don't allow a network export coincide with a list of
11331558Srgrimes		 * host(s) on the same line.
11341558Srgrimes		 */
11351558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
113675801Siedowse			syslog(LOG_ERR, "network/host conflict");
11371558Srgrimes			getexp_err(ep, tgrp);
11381558Srgrimes			goto nextline;
113929317Sjlemon
114074462Salfred		/*
114174462Salfred		 * If an export list was specified on this line, make sure
114229317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
114329317Sjlemon		 */
114429317Sjlemon		} else {
114529317Sjlemon			grp = tgrp;
114674462Salfred			while (grp && grp->gr_type == GT_IGNORE)
114729317Sjlemon				grp = grp->gr_next;
114829317Sjlemon			if (! grp) {
114929317Sjlemon			    getexp_err(ep, tgrp);
115029317Sjlemon			    goto nextline;
115129317Sjlemon			}
11521558Srgrimes		}
11531558Srgrimes
11541558Srgrimes		/*
11551558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
11561558Srgrimes		 * After loop, tgrp points to the start of the list and
11571558Srgrimes		 * grp points to the last entry in the list.
11581558Srgrimes		 */
11591558Srgrimes		grp = tgrp;
11601558Srgrimes		do {
116175635Siedowse			if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
116275635Siedowse			    &fsb)) {
116375635Siedowse				getexp_err(ep, tgrp);
116475635Siedowse				goto nextline;
116575635Siedowse			}
11661558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
11671558Srgrimes
11681558Srgrimes		/*
11691558Srgrimes		 * Success. Update the data structures.
11701558Srgrimes		 */
11711558Srgrimes		if (has_host) {
11729336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
11731558Srgrimes			grp->gr_next = grphead;
11741558Srgrimes			grphead = tgrp;
11751558Srgrimes		} else {
11761558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
11779336Sdfr				opt_flags);
11781558Srgrimes			free_grp(grp);
11791558Srgrimes		}
11801558Srgrimes		dirhead = (struct dirlist *)NULL;
11811558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
11821558Srgrimes			ep2 = exphead;
11831558Srgrimes			epp = &exphead;
11841558Srgrimes
11851558Srgrimes			/*
11861558Srgrimes			 * Insert in the list in alphabetical order.
11871558Srgrimes			 */
11881558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
11891558Srgrimes				epp = &ep2->ex_next;
11901558Srgrimes				ep2 = ep2->ex_next;
11911558Srgrimes			}
11921558Srgrimes			if (ep2)
11931558Srgrimes				ep->ex_next = ep2;
11941558Srgrimes			*epp = ep;
11951558Srgrimes			ep->ex_flag |= EX_LINKED;
11961558Srgrimes		}
11971558Srgrimesnextline:
11981558Srgrimes		if (dirhead) {
11991558Srgrimes			free_dir(dirhead);
12001558Srgrimes			dirhead = (struct dirlist *)NULL;
12011558Srgrimes		}
12021558Srgrimes	}
12031558Srgrimes	fclose(exp_file);
12041558Srgrimes}
12051558Srgrimes
12061558Srgrimes/*
12071558Srgrimes * Allocate an export list element
12081558Srgrimes */
12091558Srgrimesstruct exportlist *
12101558Srgrimesget_exp()
12111558Srgrimes{
12121558Srgrimes	struct exportlist *ep;
12131558Srgrimes
12141558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
12151558Srgrimes	if (ep == (struct exportlist *)NULL)
12161558Srgrimes		out_of_mem();
121723681Speter	memset(ep, 0, sizeof(struct exportlist));
12181558Srgrimes	return (ep);
12191558Srgrimes}
12201558Srgrimes
12211558Srgrimes/*
12221558Srgrimes * Allocate a group list element
12231558Srgrimes */
12241558Srgrimesstruct grouplist *
12251558Srgrimesget_grp()
12261558Srgrimes{
12271558Srgrimes	struct grouplist *gp;
12281558Srgrimes
12291558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
12301558Srgrimes	if (gp == (struct grouplist *)NULL)
12311558Srgrimes		out_of_mem();
123223681Speter	memset(gp, 0, sizeof(struct grouplist));
12331558Srgrimes	return (gp);
12341558Srgrimes}
12351558Srgrimes
12361558Srgrimes/*
12371558Srgrimes * Clean up upon an error in get_exportlist().
12381558Srgrimes */
12391558Srgrimesvoid
12401558Srgrimesgetexp_err(ep, grp)
12411558Srgrimes	struct exportlist *ep;
12421558Srgrimes	struct grouplist *grp;
12431558Srgrimes{
12441558Srgrimes	struct grouplist *tgrp;
12451558Srgrimes
1246100336Sjoerg	if (!(opt_flags & OP_QUIET))
1247100336Sjoerg		syslog(LOG_ERR, "bad exports list line %s", line);
12481558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
12491558Srgrimes		free_exp(ep);
12501558Srgrimes	while (grp) {
12511558Srgrimes		tgrp = grp;
12521558Srgrimes		grp = grp->gr_next;
12531558Srgrimes		free_grp(tgrp);
12541558Srgrimes	}
12551558Srgrimes}
12561558Srgrimes
12571558Srgrimes/*
12581558Srgrimes * Search the export list for a matching fs.
12591558Srgrimes */
12601558Srgrimesstruct exportlist *
12611558Srgrimesex_search(fsid)
12621558Srgrimes	fsid_t *fsid;
12631558Srgrimes{
12641558Srgrimes	struct exportlist *ep;
12651558Srgrimes
12661558Srgrimes	ep = exphead;
12671558Srgrimes	while (ep) {
12681558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
12691558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
12701558Srgrimes			return (ep);
12711558Srgrimes		ep = ep->ex_next;
12721558Srgrimes	}
12731558Srgrimes	return (ep);
12741558Srgrimes}
12751558Srgrimes
12761558Srgrimes/*
12771558Srgrimes * Add a directory path to the list.
12781558Srgrimes */
12791558Srgrimeschar *
12801558Srgrimesadd_expdir(dpp, cp, len)
12811558Srgrimes	struct dirlist **dpp;
12821558Srgrimes	char *cp;
12831558Srgrimes	int len;
12841558Srgrimes{
12851558Srgrimes	struct dirlist *dp;
12861558Srgrimes
12871558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
128837663Scharnier	if (dp == (struct dirlist *)NULL)
128937663Scharnier		out_of_mem();
12901558Srgrimes	dp->dp_left = *dpp;
12911558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
12921558Srgrimes	dp->dp_flag = 0;
12931558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
12941558Srgrimes	strcpy(dp->dp_dirp, cp);
12951558Srgrimes	*dpp = dp;
12961558Srgrimes	return (dp->dp_dirp);
12971558Srgrimes}
12981558Srgrimes
12991558Srgrimes/*
13001558Srgrimes * Hang the dir list element off the dirpath binary tree as required
13011558Srgrimes * and update the entry for host.
13021558Srgrimes */
13031558Srgrimesvoid
13049336Sdfrhang_dirp(dp, grp, ep, flags)
13051558Srgrimes	struct dirlist *dp;
13061558Srgrimes	struct grouplist *grp;
13071558Srgrimes	struct exportlist *ep;
13089336Sdfr	int flags;
13091558Srgrimes{
13101558Srgrimes	struct hostlist *hp;
13111558Srgrimes	struct dirlist *dp2;
13121558Srgrimes
13139336Sdfr	if (flags & OP_ALLDIRS) {
13141558Srgrimes		if (ep->ex_defdir)
13151558Srgrimes			free((caddr_t)dp);
13161558Srgrimes		else
13171558Srgrimes			ep->ex_defdir = dp;
13189336Sdfr		if (grp == (struct grouplist *)NULL) {
13191558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
13209336Sdfr		} else while (grp) {
13211558Srgrimes			hp = get_ht();
13221558Srgrimes			hp->ht_grp = grp;
13231558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
13241558Srgrimes			ep->ex_defdir->dp_hosts = hp;
13251558Srgrimes			grp = grp->gr_next;
13261558Srgrimes		}
13271558Srgrimes	} else {
13281558Srgrimes
13291558Srgrimes		/*
133037663Scharnier		 * Loop through the directories adding them to the tree.
13311558Srgrimes		 */
13321558Srgrimes		while (dp) {
13331558Srgrimes			dp2 = dp->dp_left;
13349336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
13351558Srgrimes			dp = dp2;
13361558Srgrimes		}
13371558Srgrimes	}
13381558Srgrimes}
13391558Srgrimes
13401558Srgrimes/*
13411558Srgrimes * Traverse the binary tree either updating a node that is already there
13421558Srgrimes * for the new directory or adding the new node.
13431558Srgrimes */
13441558Srgrimesvoid
13459336Sdfradd_dlist(dpp, newdp, grp, flags)
13461558Srgrimes	struct dirlist **dpp;
13471558Srgrimes	struct dirlist *newdp;
13481558Srgrimes	struct grouplist *grp;
13499336Sdfr	int flags;
13501558Srgrimes{
13511558Srgrimes	struct dirlist *dp;
13521558Srgrimes	struct hostlist *hp;
13531558Srgrimes	int cmp;
13541558Srgrimes
13551558Srgrimes	dp = *dpp;
13561558Srgrimes	if (dp) {
13571558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
13581558Srgrimes		if (cmp > 0) {
13599336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
13601558Srgrimes			return;
13611558Srgrimes		} else if (cmp < 0) {
13629336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
13631558Srgrimes			return;
13641558Srgrimes		} else
13651558Srgrimes			free((caddr_t)newdp);
13661558Srgrimes	} else {
13671558Srgrimes		dp = newdp;
13681558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
13691558Srgrimes		*dpp = dp;
13701558Srgrimes	}
13711558Srgrimes	if (grp) {
13721558Srgrimes
13731558Srgrimes		/*
13741558Srgrimes		 * Hang all of the host(s) off of the directory point.
13751558Srgrimes		 */
13761558Srgrimes		do {
13771558Srgrimes			hp = get_ht();
13781558Srgrimes			hp->ht_grp = grp;
13791558Srgrimes			hp->ht_next = dp->dp_hosts;
13801558Srgrimes			dp->dp_hosts = hp;
13811558Srgrimes			grp = grp->gr_next;
13821558Srgrimes		} while (grp);
13839336Sdfr	} else {
13841558Srgrimes		dp->dp_flag |= DP_DEFSET;
13859336Sdfr	}
13861558Srgrimes}
13871558Srgrimes
13881558Srgrimes/*
13891558Srgrimes * Search for a dirpath on the export point.
13901558Srgrimes */
13911558Srgrimesstruct dirlist *
139274462Salfreddirp_search(dp, dirp)
13931558Srgrimes	struct dirlist *dp;
139474462Salfred	char *dirp;
13951558Srgrimes{
13961558Srgrimes	int cmp;
13971558Srgrimes
13981558Srgrimes	if (dp) {
139974462Salfred		cmp = strcmp(dp->dp_dirp, dirp);
14001558Srgrimes		if (cmp > 0)
140174462Salfred			return (dirp_search(dp->dp_left, dirp));
14021558Srgrimes		else if (cmp < 0)
140374462Salfred			return (dirp_search(dp->dp_right, dirp));
14041558Srgrimes		else
14051558Srgrimes			return (dp);
14061558Srgrimes	}
14071558Srgrimes	return (dp);
14081558Srgrimes}
14091558Srgrimes
14101558Srgrimes/*
14111558Srgrimes * Scan for a host match in a directory tree.
14121558Srgrimes */
14131558Srgrimesint
14149336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
14151558Srgrimes	struct dirlist *dp;
141674462Salfred	struct sockaddr *saddr;
14171558Srgrimes	int *defsetp;
14189336Sdfr	int *hostsetp;
14191558Srgrimes{
14201558Srgrimes	struct hostlist *hp;
14211558Srgrimes	struct grouplist *grp;
142274462Salfred	struct addrinfo *ai;
14231558Srgrimes
14241558Srgrimes	if (dp) {
14251558Srgrimes		if (dp->dp_flag & DP_DEFSET)
14269336Sdfr			*defsetp = dp->dp_flag;
14271558Srgrimes		hp = dp->dp_hosts;
14281558Srgrimes		while (hp) {
14291558Srgrimes			grp = hp->ht_grp;
14301558Srgrimes			switch (grp->gr_type) {
14311558Srgrimes			case GT_HOST:
143274462Salfred				ai = grp->gr_ptr.gt_addrinfo;
143374462Salfred				for (; ai; ai = ai->ai_next) {
143475801Siedowse					if (!sacmp(ai->ai_addr, saddr, NULL)) {
143574462Salfred						*hostsetp =
143674462Salfred						    (hp->ht_flag | DP_HOSTSET);
143774462Salfred						return (1);
143874462Salfred					}
14399336Sdfr				}
144075801Siedowse				break;
14411558Srgrimes			case GT_NET:
144275801Siedowse				if (!sacmp(saddr, (struct sockaddr *)
144375801Siedowse				    &grp->gr_ptr.gt_net.nt_net,
144475801Siedowse				    (struct sockaddr *)
144575801Siedowse				    &grp->gr_ptr.gt_net.nt_mask)) {
144674462Salfred					*hostsetp = (hp->ht_flag | DP_HOSTSET);
144774462Salfred					return (1);
144874462Salfred				}
144975801Siedowse				break;
145075801Siedowse			}
14511558Srgrimes			hp = hp->ht_next;
14521558Srgrimes		}
14531558Srgrimes	}
14541558Srgrimes	return (0);
14551558Srgrimes}
14561558Srgrimes
14571558Srgrimes/*
14581558Srgrimes * Scan tree for a host that matches the address.
14591558Srgrimes */
14601558Srgrimesint
14611558Srgrimesscan_tree(dp, saddr)
14621558Srgrimes	struct dirlist *dp;
146374462Salfred	struct sockaddr *saddr;
14641558Srgrimes{
14659336Sdfr	int defset, hostset;
14661558Srgrimes
14671558Srgrimes	if (dp) {
14681558Srgrimes		if (scan_tree(dp->dp_left, saddr))
14691558Srgrimes			return (1);
14709336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
14711558Srgrimes			return (1);
14721558Srgrimes		if (scan_tree(dp->dp_right, saddr))
14731558Srgrimes			return (1);
14741558Srgrimes	}
14751558Srgrimes	return (0);
14761558Srgrimes}
14771558Srgrimes
14781558Srgrimes/*
14791558Srgrimes * Traverse the dirlist tree and free it up.
14801558Srgrimes */
14811558Srgrimesvoid
14821558Srgrimesfree_dir(dp)
14831558Srgrimes	struct dirlist *dp;
14841558Srgrimes{
14851558Srgrimes
14861558Srgrimes	if (dp) {
14871558Srgrimes		free_dir(dp->dp_left);
14881558Srgrimes		free_dir(dp->dp_right);
14891558Srgrimes		free_host(dp->dp_hosts);
14901558Srgrimes		free((caddr_t)dp);
14911558Srgrimes	}
14921558Srgrimes}
14931558Srgrimes
14941558Srgrimes/*
14951558Srgrimes * Parse the option string and update fields.
14961558Srgrimes * Option arguments may either be -<option>=<value> or
14971558Srgrimes * -<option> <value>
14981558Srgrimes */
14991558Srgrimesint
15001558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
15011558Srgrimes	char **cpp, **endcpp;
15021558Srgrimes	struct exportlist *ep;
15031558Srgrimes	struct grouplist *grp;
15041558Srgrimes	int *has_hostp;
15051558Srgrimes	int *exflagsp;
150672650Sgreen	struct xucred *cr;
15071558Srgrimes{
15081558Srgrimes	char *cpoptarg, *cpoptend;
15091558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
15101558Srgrimes	int allflag, usedarg;
15111558Srgrimes
151251968Salfred	savedc2 = '\0';
15131558Srgrimes	cpopt = *cpp;
15141558Srgrimes	cpopt++;
15151558Srgrimes	cp = *endcpp;
15161558Srgrimes	savedc = *cp;
15171558Srgrimes	*cp = '\0';
15181558Srgrimes	while (cpopt && *cpopt) {
15191558Srgrimes		allflag = 1;
15201558Srgrimes		usedarg = -2;
152137663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
15221558Srgrimes			*cpoptend++ = '\0';
152337663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15241558Srgrimes				*cpoptarg++ = '\0';
15251558Srgrimes		} else {
152637663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15271558Srgrimes				*cpoptarg++ = '\0';
15281558Srgrimes			else {
15291558Srgrimes				*cp = savedc;
15301558Srgrimes				nextfield(&cp, &endcp);
15311558Srgrimes				**endcpp = '\0';
15321558Srgrimes				if (endcp > cp && *cp != '-') {
15331558Srgrimes					cpoptarg = cp;
15341558Srgrimes					savedc2 = *endcp;
15351558Srgrimes					*endcp = '\0';
15361558Srgrimes					usedarg = 0;
15371558Srgrimes				}
15381558Srgrimes			}
15391558Srgrimes		}
15401558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
15411558Srgrimes			*exflagsp |= MNT_EXRDONLY;
15421558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
15431558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
15441558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
15451558Srgrimes			usedarg++;
15461558Srgrimes			parsecred(cpoptarg, cr);
15471558Srgrimes			if (allflag == 0) {
15481558Srgrimes				*exflagsp |= MNT_EXPORTANON;
15491558Srgrimes				opt_flags |= OP_MAPALL;
15501558Srgrimes			} else
15511558Srgrimes				opt_flags |= OP_MAPROOT;
15521558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
155375801Siedowse		    !strcmp(cpopt, "m"))) {
15541558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
155537663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
15561558Srgrimes				return (1);
15571558Srgrimes			}
15581558Srgrimes			usedarg++;
15591558Srgrimes			opt_flags |= OP_MASK;
15601558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
15611558Srgrimes			!strcmp(cpopt, "n"))) {
156274462Salfred			if (strchr(cpoptarg, '/') != NULL) {
156374462Salfred				if (debug)
156474462Salfred					fprintf(stderr, "setting OP_MASKLEN\n");
156574462Salfred				opt_flags |= OP_MASKLEN;
156674462Salfred			}
15671558Srgrimes			if (grp->gr_type != GT_NULL) {
156837663Scharnier				syslog(LOG_ERR, "network/host conflict");
15691558Srgrimes				return (1);
15701558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
157137663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
15721558Srgrimes				return (1);
15731558Srgrimes			}
15741558Srgrimes			grp->gr_type = GT_NET;
15751558Srgrimes			*has_hostp = 1;
15761558Srgrimes			usedarg++;
15771558Srgrimes			opt_flags |= OP_NET;
15781558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
15791558Srgrimes			opt_flags |= OP_ALLDIRS;
158027447Sdfr		} else if (!strcmp(cpopt, "public")) {
158127447Sdfr			*exflagsp |= MNT_EXPUBLIC;
158227447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
158327447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
158427447Sdfr			opt_flags |= OP_MAPALL;
158527447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
158627447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
1587100336Sjoerg		} else if (!strcmp(cpopt, "quiet")) {
1588100336Sjoerg			opt_flags |= OP_QUIET;
15891558Srgrimes		} else {
159037663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
15911558Srgrimes			return (1);
15921558Srgrimes		}
15931558Srgrimes		if (usedarg >= 0) {
15941558Srgrimes			*endcp = savedc2;
15951558Srgrimes			**endcpp = savedc;
15961558Srgrimes			if (usedarg > 0) {
15971558Srgrimes				*cpp = cp;
15981558Srgrimes				*endcpp = endcp;
15991558Srgrimes			}
16001558Srgrimes			return (0);
16011558Srgrimes		}
16021558Srgrimes		cpopt = cpoptend;
16031558Srgrimes	}
16041558Srgrimes	**endcpp = savedc;
16051558Srgrimes	return (0);
16061558Srgrimes}
16071558Srgrimes
16081558Srgrimes/*
16091558Srgrimes * Translate a character string to the corresponding list of network
16101558Srgrimes * addresses for a hostname.
16111558Srgrimes */
16121558Srgrimesint
16137401Swpaulget_host(cp, grp, tgrp)
16141558Srgrimes	char *cp;
16151558Srgrimes	struct grouplist *grp;
16167401Swpaul	struct grouplist *tgrp;
16171558Srgrimes{
16187401Swpaul	struct grouplist *checkgrp;
161975635Siedowse	struct addrinfo *ai, *tai, hints;
162074462Salfred	int ecode;
162174462Salfred	char host[NI_MAXHOST];
16221558Srgrimes
162374462Salfred	if (grp->gr_type != GT_NULL) {
162474462Salfred		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
16251558Srgrimes		return (1);
16261558Srgrimes	}
162774462Salfred	memset(&hints, 0, sizeof hints);
162874462Salfred	hints.ai_flags = AI_CANONNAME;
162974462Salfred	hints.ai_protocol = IPPROTO_UDP;
163074462Salfred	ecode = getaddrinfo(cp, NULL, &hints, &ai);
163174462Salfred	if (ecode != 0) {
163275635Siedowse		syslog(LOG_ERR,"can't get address info for host %s", cp);
163374462Salfred		return 1;
163474462Salfred	}
163574462Salfred	grp->gr_ptr.gt_addrinfo = ai;
163674462Salfred	while (ai != NULL) {
163774462Salfred		if (ai->ai_canonname == NULL) {
163874462Salfred			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
163974462Salfred			    sizeof host, NULL, 0, ninumeric) != 0)
164074462Salfred				strlcpy(host, "?", sizeof(host));
164174462Salfred			ai->ai_canonname = strdup(host);
164274462Salfred			ai->ai_flags |= AI_CANONNAME;
164375641Siedowse		}
164474462Salfred		if (debug)
164575635Siedowse			fprintf(stderr, "got host %s\n", ai->ai_canonname);
164675635Siedowse		/*
164775635Siedowse		 * Sanity check: make sure we don't already have an entry
164875635Siedowse		 * for this host in the grouplist.
164975635Siedowse		 */
165075635Siedowse		for (checkgrp = tgrp; checkgrp != NULL;
165175635Siedowse		    checkgrp = checkgrp->gr_next) {
165275635Siedowse			if (checkgrp->gr_type != GT_HOST)
165375635Siedowse				continue;
165475635Siedowse			for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
165575635Siedowse			    tai = tai->ai_next) {
165675801Siedowse				if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
165775635Siedowse					continue;
165875635Siedowse				if (debug)
165975635Siedowse					fprintf(stderr,
166075635Siedowse					    "ignoring duplicate host %s\n",
166175635Siedowse					    ai->ai_canonname);
166275635Siedowse				grp->gr_type = GT_IGNORE;
166375635Siedowse				return (0);
166475635Siedowse			}
166575635Siedowse		}
166674462Salfred		ai = ai->ai_next;
16671558Srgrimes	}
166875635Siedowse	grp->gr_type = GT_HOST;
16691558Srgrimes	return (0);
16701558Srgrimes}
16711558Srgrimes
16721558Srgrimes/*
16731558Srgrimes * Free up an exports list component
16741558Srgrimes */
16751558Srgrimesvoid
16761558Srgrimesfree_exp(ep)
16771558Srgrimes	struct exportlist *ep;
16781558Srgrimes{
16791558Srgrimes
16801558Srgrimes	if (ep->ex_defdir) {
16811558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
16821558Srgrimes		free((caddr_t)ep->ex_defdir);
16831558Srgrimes	}
16841558Srgrimes	if (ep->ex_fsdir)
16851558Srgrimes		free(ep->ex_fsdir);
168627447Sdfr	if (ep->ex_indexfile)
168727447Sdfr		free(ep->ex_indexfile);
16881558Srgrimes	free_dir(ep->ex_dirl);
16891558Srgrimes	free((caddr_t)ep);
16901558Srgrimes}
16911558Srgrimes
16921558Srgrimes/*
16931558Srgrimes * Free hosts.
16941558Srgrimes */
16951558Srgrimesvoid
16961558Srgrimesfree_host(hp)
16971558Srgrimes	struct hostlist *hp;
16981558Srgrimes{
16991558Srgrimes	struct hostlist *hp2;
17001558Srgrimes
17011558Srgrimes	while (hp) {
17021558Srgrimes		hp2 = hp;
17031558Srgrimes		hp = hp->ht_next;
17041558Srgrimes		free((caddr_t)hp2);
17051558Srgrimes	}
17061558Srgrimes}
17071558Srgrimes
17081558Srgrimesstruct hostlist *
17091558Srgrimesget_ht()
17101558Srgrimes{
17111558Srgrimes	struct hostlist *hp;
17121558Srgrimes
17131558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
17141558Srgrimes	if (hp == (struct hostlist *)NULL)
17151558Srgrimes		out_of_mem();
17161558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
17179336Sdfr	hp->ht_flag = 0;
17181558Srgrimes	return (hp);
17191558Srgrimes}
17201558Srgrimes
17211558Srgrimes/*
17221558Srgrimes * Out of memory, fatal
17231558Srgrimes */
17241558Srgrimesvoid
17251558Srgrimesout_of_mem()
17261558Srgrimes{
17271558Srgrimes
172837663Scharnier	syslog(LOG_ERR, "out of memory");
17291558Srgrimes	exit(2);
17301558Srgrimes}
17311558Srgrimes
17321558Srgrimes/*
17331558Srgrimes * Do the mount syscall with the update flag to push the export info into
17341558Srgrimes * the kernel.
17351558Srgrimes */
17361558Srgrimesint
17371558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
17381558Srgrimes	struct exportlist *ep;
17391558Srgrimes	struct grouplist *grp;
17401558Srgrimes	int exflags;
174172650Sgreen	struct xucred *anoncrp;
17421558Srgrimes	char *dirp;
17431558Srgrimes	int dirplen;
17441558Srgrimes	struct statfs *fsb;
17451558Srgrimes{
174675841Siedowse	struct statfs fsb1;
174774462Salfred	struct addrinfo *ai;
174875801Siedowse	struct export_args *eap;
174974462Salfred	char *cp = NULL;
17501558Srgrimes	int done;
17511558Srgrimes	char savedc = '\0';
17521558Srgrimes	union {
17531558Srgrimes		struct ufs_args ua;
17541558Srgrimes		struct iso_args ia;
17559336Sdfr		struct msdosfs_args da;
175654093Ssemenu		struct ntfs_args na;
17571558Srgrimes	} args;
17581558Srgrimes
175975801Siedowse	bzero(&args, sizeof args);
176075801Siedowse	/* XXX, we assume that all xx_args look like ufs_args. */
17611558Srgrimes	args.ua.fspec = 0;
176275801Siedowse	eap = &args.ua.export;
176375801Siedowse
176475801Siedowse	eap->ex_flags = exflags;
176575801Siedowse	eap->ex_anon = *anoncrp;
176675801Siedowse	eap->ex_indexfile = ep->ex_indexfile;
176775641Siedowse	if (grp->gr_type == GT_HOST)
176874462Salfred		ai = grp->gr_ptr.gt_addrinfo;
176975641Siedowse	else
177075641Siedowse		ai = NULL;
17711558Srgrimes	done = FALSE;
17721558Srgrimes	while (!done) {
17731558Srgrimes		switch (grp->gr_type) {
17741558Srgrimes		case GT_HOST:
177575641Siedowse			if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
177674462Salfred				goto skip;
177775801Siedowse			eap->ex_addr = ai->ai_addr;
177875801Siedowse			eap->ex_addrlen = ai->ai_addrlen;
177975801Siedowse			eap->ex_masklen = 0;
17801558Srgrimes			break;
17811558Srgrimes		case GT_NET:
178275801Siedowse			if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
178374462Salfred			    have_v6 == 0)
178474462Salfred				goto skip;
178575801Siedowse			eap->ex_addr =
178675801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
178775801Siedowse			eap->ex_addrlen = args.ua.export.ex_addr->sa_len;
178875801Siedowse			eap->ex_mask =
178975801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
179075801Siedowse			eap->ex_masklen = args.ua.export.ex_mask->sa_len;
17911558Srgrimes			break;
179275641Siedowse		case GT_DEFAULT:
179375801Siedowse			eap->ex_addr = NULL;
179475801Siedowse			eap->ex_addrlen = 0;
179575801Siedowse			eap->ex_mask = NULL;
179675801Siedowse			eap->ex_masklen = 0;
179775641Siedowse			break;
17987401Swpaul		case GT_IGNORE:
17997401Swpaul			return(0);
18007401Swpaul			break;
18011558Srgrimes		default:
180237663Scharnier			syslog(LOG_ERR, "bad grouptype");
18031558Srgrimes			if (cp)
18041558Srgrimes				*cp = savedc;
18051558Srgrimes			return (1);
18061558Srgrimes		};
18071558Srgrimes
18081558Srgrimes		/*
18091558Srgrimes		 * XXX:
18101558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
18111558Srgrimes		 * of looping back up the dirp to the mount point??
18121558Srgrimes		 * Also, needs to know how to export all types of local
181396707Strhodes		 * exportable filesystems and not just "ufs".
18141558Srgrimes		 */
18159336Sdfr		while (mount(fsb->f_fstypename, dirp,
181674462Salfred		    fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
18171558Srgrimes			if (cp)
18181558Srgrimes				*cp-- = savedc;
18191558Srgrimes			else
18201558Srgrimes				cp = dirp + dirplen - 1;
1821100336Sjoerg			if (opt_flags & OP_QUIET)
1822100336Sjoerg				return (1);
18231558Srgrimes			if (errno == EPERM) {
182475635Siedowse				if (debug)
182575635Siedowse					warnx("can't change attributes for %s",
182675635Siedowse					    dirp);
18271558Srgrimes				syslog(LOG_ERR,
182837663Scharnier				   "can't change attributes for %s", dirp);
18291558Srgrimes				return (1);
18301558Srgrimes			}
18311558Srgrimes			if (opt_flags & OP_ALLDIRS) {
1832100336Sjoerg				if (errno == EINVAL)
1833100336Sjoerg					syslog(LOG_ERR,
1834100336Sjoerg		"-alldirs requested but %s is not a filesystem mountpoint",
1835100336Sjoerg						dirp);
1836100336Sjoerg				else
1837100336Sjoerg					syslog(LOG_ERR,
1838100336Sjoerg						"could not remount %s: %m",
1839100336Sjoerg						dirp);
18401558Srgrimes				return (1);
18411558Srgrimes			}
18421558Srgrimes			/* back up over the last component */
18431558Srgrimes			while (*cp == '/' && cp > dirp)
18441558Srgrimes				cp--;
18451558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
18461558Srgrimes				cp--;
18471558Srgrimes			if (cp == dirp) {
18481558Srgrimes				if (debug)
184937663Scharnier					warnx("mnt unsucc");
185037663Scharnier				syslog(LOG_ERR, "can't export %s", dirp);
18511558Srgrimes				return (1);
18521558Srgrimes			}
18531558Srgrimes			savedc = *cp;
18541558Srgrimes			*cp = '\0';
185575841Siedowse			/* Check that we're still on the same filesystem. */
185675841Siedowse			if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
185775841Siedowse			    &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
185875841Siedowse				*cp = savedc;
185975841Siedowse				syslog(LOG_ERR, "can't export %s", dirp);
186075841Siedowse				return (1);
186175841Siedowse			}
18621558Srgrimes		}
186374462Salfredskip:
186475641Siedowse		if (ai != NULL)
186574462Salfred			ai = ai->ai_next;
186675641Siedowse		if (ai == NULL)
18671558Srgrimes			done = TRUE;
18681558Srgrimes	}
18691558Srgrimes	if (cp)
18701558Srgrimes		*cp = savedc;
18711558Srgrimes	return (0);
18721558Srgrimes}
18731558Srgrimes
18741558Srgrimes/*
18751558Srgrimes * Translate a net address.
187675801Siedowse *
187775801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
18781558Srgrimes */
18791558Srgrimesint
18801558Srgrimesget_net(cp, net, maskflg)
18811558Srgrimes	char *cp;
18821558Srgrimes	struct netmsk *net;
18831558Srgrimes	int maskflg;
18841558Srgrimes{
188575861Siedowse	struct netent *np = NULL;
188674462Salfred	char *name, *p, *prefp;
188775801Siedowse	struct sockaddr_in sin;
188875861Siedowse	struct sockaddr *sa = NULL;
188974462Salfred	struct addrinfo hints, *ai = NULL;
189074462Salfred	char netname[NI_MAXHOST];
189174462Salfred	long preflen;
18921558Srgrimes
189375635Siedowse	p = prefp = NULL;
189474462Salfred	if ((opt_flags & OP_MASKLEN) && !maskflg) {
189574462Salfred		p = strchr(cp, '/');
189674462Salfred		*p = '\0';
189774462Salfred		prefp = p + 1;
189874462Salfred	}
189974462Salfred
190075861Siedowse	/*
190175861Siedowse	 * Check for a numeric address first. We wish to avoid
190275861Siedowse	 * possible DNS lookups in getnetbyname().
190375861Siedowse	 */
190475861Siedowse	if (isxdigit(*cp) || *cp == ':') {
190574462Salfred		memset(&hints, 0, sizeof hints);
190675801Siedowse		/* Ensure the mask and the network have the same family. */
190775801Siedowse		if (maskflg && (opt_flags & OP_NET))
190875801Siedowse			hints.ai_family = net->nt_net.ss_family;
190975801Siedowse		else if (!maskflg && (opt_flags & OP_HAVEMASK))
191075801Siedowse			hints.ai_family = net->nt_mask.ss_family;
191175801Siedowse		else
191275801Siedowse			hints.ai_family = AF_UNSPEC;
191374462Salfred		hints.ai_flags = AI_NUMERICHOST;
191475861Siedowse		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
191575861Siedowse			sa = ai->ai_addr;
191675861Siedowse		if (sa != NULL && ai->ai_family == AF_INET) {
191774462Salfred			/*
191875801Siedowse			 * The address in `cp' is really a network address, so
191975801Siedowse			 * use inet_network() to re-interpret this correctly.
192075801Siedowse			 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
192174462Salfred			 */
192275801Siedowse			bzero(&sin, sizeof sin);
192374462Salfred			sin.sin_family = AF_INET;
192474462Salfred			sin.sin_len = sizeof sin;
192575801Siedowse			sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
192674462Salfred			if (debug)
192775801Siedowse				fprintf(stderr, "get_net: v4 addr %s\n",
192875801Siedowse				    inet_ntoa(sin.sin_addr));
192974462Salfred			sa = (struct sockaddr *)&sin;
193075861Siedowse		}
193175861Siedowse	}
193275861Siedowse	if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
193375861Siedowse		bzero(&sin, sizeof sin);
193475861Siedowse		sin.sin_family = AF_INET;
193575861Siedowse		sin.sin_len = sizeof sin;
193675861Siedowse		sin.sin_addr = inet_makeaddr(np->n_net, 0);
193775861Siedowse		sa = (struct sockaddr *)&sin;
193875861Siedowse	}
193975861Siedowse	if (sa == NULL)
194074462Salfred		goto fail;
194125318Spst
194275801Siedowse	if (maskflg) {
194375801Siedowse		/* The specified sockaddr is a mask. */
194475801Siedowse		if (checkmask(sa) != 0)
194575801Siedowse			goto fail;
194675801Siedowse		bcopy(sa, &net->nt_mask, sa->sa_len);
194775801Siedowse		opt_flags |= OP_HAVEMASK;
194875801Siedowse	} else {
194975801Siedowse		/* The specified sockaddr is a network address. */
195075801Siedowse		bcopy(sa, &net->nt_net, sa->sa_len);
195174462Salfred
195275801Siedowse		/* Get a network name for the export list. */
195375801Siedowse		if (np) {
195475801Siedowse			name = np->n_name;
195575801Siedowse		} else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
195675801Siedowse		   NULL, 0, ninumeric) == 0) {
195775801Siedowse			name = netname;
195875801Siedowse		} else {
195975801Siedowse			goto fail;
196075801Siedowse		}
196175801Siedowse		if ((net->nt_name = strdup(name)) == NULL)
196275801Siedowse			out_of_mem();
196375801Siedowse
196475801Siedowse		/*
196575801Siedowse		 * Extract a mask from either a "/<masklen>" suffix, or
196675801Siedowse		 * from the class of an IPv4 address.
196775801Siedowse		 */
196874462Salfred		if (opt_flags & OP_MASKLEN) {
196974462Salfred			preflen = strtol(prefp, NULL, 10);
197075801Siedowse			if (preflen < 0L || preflen == LONG_MAX)
197174462Salfred				goto fail;
197275801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
197375801Siedowse			if (makemask(&net->nt_mask, (int)preflen) != 0)
197475801Siedowse				goto fail;
197575801Siedowse			opt_flags |= OP_HAVEMASK;
197674462Salfred			*p = '/';
197775801Siedowse		} else if (sa->sa_family == AF_INET &&
197875801Siedowse		    (opt_flags & OP_MASK) == 0) {
197975801Siedowse			in_addr_t addr;
198074462Salfred
198175801Siedowse			addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
198275801Siedowse			if (IN_CLASSA(addr))
198375801Siedowse				preflen = 8;
198475801Siedowse			else if (IN_CLASSB(addr))
198575801Siedowse				preflen = 16;
198675801Siedowse			else if (IN_CLASSC(addr))
198775801Siedowse				preflen = 24;
198875801Siedowse			else if (IN_CLASSD(addr))
198975801Siedowse				preflen = 28;
199075801Siedowse			else
199175801Siedowse				preflen = 32;	/* XXX */
199275801Siedowse
199375801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
199475801Siedowse			makemask(&net->nt_mask, (int)preflen);
199575801Siedowse			opt_flags |= OP_HAVEMASK;
199674462Salfred		}
199774462Salfred	}
199874462Salfred
199974462Salfred	if (ai)
200074462Salfred		freeaddrinfo(ai);
200174462Salfred	return 0;
200274462Salfred
200374462Salfredfail:
200474462Salfred	if (ai)
200574462Salfred		freeaddrinfo(ai);
200674462Salfred	return 1;
20071558Srgrimes}
20081558Srgrimes
20091558Srgrimes/*
20101558Srgrimes * Parse out the next white space separated field
20111558Srgrimes */
20121558Srgrimesvoid
20131558Srgrimesnextfield(cp, endcp)
20141558Srgrimes	char **cp;
20151558Srgrimes	char **endcp;
20161558Srgrimes{
20171558Srgrimes	char *p;
20181558Srgrimes
20191558Srgrimes	p = *cp;
20201558Srgrimes	while (*p == ' ' || *p == '\t')
20211558Srgrimes		p++;
20221558Srgrimes	if (*p == '\n' || *p == '\0')
20231558Srgrimes		*cp = *endcp = p;
20241558Srgrimes	else {
20251558Srgrimes		*cp = p++;
20261558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
20271558Srgrimes			p++;
20281558Srgrimes		*endcp = p;
20291558Srgrimes	}
20301558Srgrimes}
20311558Srgrimes
20321558Srgrimes/*
20331558Srgrimes * Get an exports file line. Skip over blank lines and handle line
20341558Srgrimes * continuations.
20351558Srgrimes */
20361558Srgrimesint
20371558Srgrimesget_line()
20381558Srgrimes{
20391558Srgrimes	char *p, *cp;
204096622Siedowse	size_t len;
20411558Srgrimes	int totlen, cont_line;
20421558Srgrimes
20431558Srgrimes	/*
20441558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
20451558Srgrimes	 */
20461558Srgrimes	p = line;
20471558Srgrimes	totlen = 0;
20481558Srgrimes	do {
204996622Siedowse		if ((p = fgetln(exp_file, &len)) == NULL)
20501558Srgrimes			return (0);
20511558Srgrimes		cp = p + len - 1;
20521558Srgrimes		cont_line = 0;
20531558Srgrimes		while (cp >= p &&
20541558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
20551558Srgrimes			if (*cp == '\\')
20561558Srgrimes				cont_line = 1;
20571558Srgrimes			cp--;
20581558Srgrimes			len--;
20591558Srgrimes		}
206079117Sdd		if (cont_line) {
206179117Sdd			*++cp = ' ';
206279117Sdd			len++;
206379117Sdd		}
206496622Siedowse		if (linesize < len + totlen + 1) {
206596622Siedowse			linesize = len + totlen + 1;
206696622Siedowse			line = realloc(line, linesize);
206796622Siedowse			if (line == NULL)
206896622Siedowse				out_of_mem();
20691558Srgrimes		}
207096622Siedowse		memcpy(line + totlen, p, len);
207196622Siedowse		totlen += len;
207296622Siedowse		line[totlen] = '\0';
20731558Srgrimes	} while (totlen == 0 || cont_line);
20741558Srgrimes	return (1);
20751558Srgrimes}
20761558Srgrimes
20771558Srgrimes/*
20781558Srgrimes * Parse a description of a credential.
20791558Srgrimes */
20801558Srgrimesvoid
20811558Srgrimesparsecred(namelist, cr)
20821558Srgrimes	char *namelist;
208372650Sgreen	struct xucred *cr;
20841558Srgrimes{
20851558Srgrimes	char *name;
20861558Srgrimes	int cnt;
20871558Srgrimes	char *names;
20881558Srgrimes	struct passwd *pw;
20891558Srgrimes	struct group *gr;
20901558Srgrimes	int ngroups, groups[NGROUPS + 1];
20911558Srgrimes
209291354Sdd	cr->cr_version = XUCRED_VERSION;
20931558Srgrimes	/*
209437663Scharnier	 * Set up the unprivileged user.
20951558Srgrimes	 */
20961558Srgrimes	cr->cr_uid = -2;
20971558Srgrimes	cr->cr_groups[0] = -2;
20981558Srgrimes	cr->cr_ngroups = 1;
20991558Srgrimes	/*
21001558Srgrimes	 * Get the user's password table entry.
21011558Srgrimes	 */
21021558Srgrimes	names = strsep(&namelist, " \t\n");
21031558Srgrimes	name = strsep(&names, ":");
21041558Srgrimes	if (isdigit(*name) || *name == '-')
21051558Srgrimes		pw = getpwuid(atoi(name));
21061558Srgrimes	else
21071558Srgrimes		pw = getpwnam(name);
21081558Srgrimes	/*
21091558Srgrimes	 * Credentials specified as those of a user.
21101558Srgrimes	 */
21111558Srgrimes	if (names == NULL) {
21121558Srgrimes		if (pw == NULL) {
211337663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
21141558Srgrimes			return;
21151558Srgrimes		}
21161558Srgrimes		cr->cr_uid = pw->pw_uid;
21171558Srgrimes		ngroups = NGROUPS + 1;
21181558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
211937663Scharnier			syslog(LOG_ERR, "too many groups");
21201558Srgrimes		/*
21211558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
21221558Srgrimes		 */
21231558Srgrimes		cr->cr_ngroups = ngroups - 1;
21241558Srgrimes		cr->cr_groups[0] = groups[0];
21251558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
21261558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
21271558Srgrimes		return;
21281558Srgrimes	}
21291558Srgrimes	/*
21301558Srgrimes	 * Explicit credential specified as a colon separated list:
21311558Srgrimes	 *	uid:gid:gid:...
21321558Srgrimes	 */
21331558Srgrimes	if (pw != NULL)
21341558Srgrimes		cr->cr_uid = pw->pw_uid;
21351558Srgrimes	else if (isdigit(*name) || *name == '-')
21361558Srgrimes		cr->cr_uid = atoi(name);
21371558Srgrimes	else {
213837663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
21391558Srgrimes		return;
21401558Srgrimes	}
21411558Srgrimes	cr->cr_ngroups = 0;
21421558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
21431558Srgrimes		name = strsep(&names, ":");
21441558Srgrimes		if (isdigit(*name) || *name == '-') {
21451558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
21461558Srgrimes		} else {
21471558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
214837663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
21491558Srgrimes				continue;
21501558Srgrimes			}
21511558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
21521558Srgrimes		}
21531558Srgrimes	}
21541558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
215537663Scharnier		syslog(LOG_ERR, "too many groups");
21561558Srgrimes}
21571558Srgrimes
21581558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
21591558Srgrimes/*
21601558Srgrimes * Routines that maintain the remote mounttab
21611558Srgrimes */
21621558Srgrimesvoid
21631558Srgrimesget_mountlist()
21641558Srgrimes{
21651558Srgrimes	struct mountlist *mlp, **mlpp;
216623681Speter	char *host, *dirp, *cp;
21671558Srgrimes	char str[STRSIZ];
21681558Srgrimes	FILE *mlfile;
21691558Srgrimes
21701558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
217153117Sbillf		if (errno == ENOENT)
217253117Sbillf			return;
217353117Sbillf		else {
217453117Sbillf			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
217553117Sbillf			return;
217653117Sbillf		}
21771558Srgrimes	}
21781558Srgrimes	mlpp = &mlhead;
21791558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
218023681Speter		cp = str;
218123681Speter		host = strsep(&cp, " \t\n");
218223681Speter		dirp = strsep(&cp, " \t\n");
218323681Speter		if (host == NULL || dirp == NULL)
21841558Srgrimes			continue;
21851558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
218637663Scharnier		if (mlp == (struct mountlist *)NULL)
218737663Scharnier			out_of_mem();
218823681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
218923681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
219023681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
219123681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
21921558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
21931558Srgrimes		*mlpp = mlp;
21941558Srgrimes		mlpp = &mlp->ml_next;
21951558Srgrimes	}
21961558Srgrimes	fclose(mlfile);
21971558Srgrimes}
21981558Srgrimes
219975635Siedowsevoid
220075635Siedowsedel_mlist(char *hostp, char *dirp)
22011558Srgrimes{
22021558Srgrimes	struct mountlist *mlp, **mlpp;
22031558Srgrimes	struct mountlist *mlp2;
22041558Srgrimes	FILE *mlfile;
22051558Srgrimes	int fnd = 0;
22061558Srgrimes
22071558Srgrimes	mlpp = &mlhead;
22081558Srgrimes	mlp = mlhead;
22091558Srgrimes	while (mlp) {
22101558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
22111558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
22121558Srgrimes			fnd = 1;
22131558Srgrimes			mlp2 = mlp;
22141558Srgrimes			*mlpp = mlp = mlp->ml_next;
22151558Srgrimes			free((caddr_t)mlp2);
22161558Srgrimes		} else {
22171558Srgrimes			mlpp = &mlp->ml_next;
22181558Srgrimes			mlp = mlp->ml_next;
22191558Srgrimes		}
22201558Srgrimes	}
22211558Srgrimes	if (fnd) {
22221558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
222337663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
22241558Srgrimes			return;
22251558Srgrimes		}
22261558Srgrimes		mlp = mlhead;
22271558Srgrimes		while (mlp) {
22281558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22291558Srgrimes			mlp = mlp->ml_next;
22301558Srgrimes		}
22311558Srgrimes		fclose(mlfile);
22321558Srgrimes	}
22331558Srgrimes}
22341558Srgrimes
22351558Srgrimesvoid
22361558Srgrimesadd_mlist(hostp, dirp)
22371558Srgrimes	char *hostp, *dirp;
22381558Srgrimes{
22391558Srgrimes	struct mountlist *mlp, **mlpp;
22401558Srgrimes	FILE *mlfile;
22411558Srgrimes
22421558Srgrimes	mlpp = &mlhead;
22431558Srgrimes	mlp = mlhead;
22441558Srgrimes	while (mlp) {
22451558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
22461558Srgrimes			return;
22471558Srgrimes		mlpp = &mlp->ml_next;
22481558Srgrimes		mlp = mlp->ml_next;
22491558Srgrimes	}
22501558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
225137663Scharnier	if (mlp == (struct mountlist *)NULL)
225237663Scharnier		out_of_mem();
22531558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
22541558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
22551558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
22561558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
22571558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
22581558Srgrimes	*mlpp = mlp;
22591558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
226037663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
22611558Srgrimes		return;
22621558Srgrimes	}
22631558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22641558Srgrimes	fclose(mlfile);
22651558Srgrimes}
22661558Srgrimes
22671558Srgrimes/*
22681558Srgrimes * Free up a group list.
22691558Srgrimes */
22701558Srgrimesvoid
22711558Srgrimesfree_grp(grp)
22721558Srgrimes	struct grouplist *grp;
22731558Srgrimes{
22741558Srgrimes	if (grp->gr_type == GT_HOST) {
227574462Salfred		if (grp->gr_ptr.gt_addrinfo != NULL)
227674462Salfred			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
22771558Srgrimes	} else if (grp->gr_type == GT_NET) {
22781558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
22791558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
22801558Srgrimes	}
22811558Srgrimes	free((caddr_t)grp);
22821558Srgrimes}
22831558Srgrimes
22841558Srgrimes#ifdef DEBUG
22851558Srgrimesvoid
22861558SrgrimesSYSLOG(int pri, const char *fmt, ...)
22871558Srgrimes{
22881558Srgrimes	va_list ap;
22891558Srgrimes
22901558Srgrimes	va_start(ap, fmt);
22911558Srgrimes	vfprintf(stderr, fmt, ap);
22921558Srgrimes	va_end(ap);
22931558Srgrimes}
22941558Srgrimes#endif /* DEBUG */
22951558Srgrimes
22961558Srgrimes/*
22971558Srgrimes * Check options for consistency.
22981558Srgrimes */
22991558Srgrimesint
23001558Srgrimescheck_options(dp)
23011558Srgrimes	struct dirlist *dp;
23021558Srgrimes{
23031558Srgrimes
23041558Srgrimes	if (dp == (struct dirlist *)NULL)
23051558Srgrimes	    return (1);
230683653Speter	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
230783653Speter	    syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
23081558Srgrimes	    return (1);
23091558Srgrimes	}
23101558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
231175801Siedowse		syslog(LOG_ERR, "-mask requires -network");
231275801Siedowse		return (1);
23131558Srgrimes	}
231475801Siedowse	if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
231575801Siedowse		syslog(LOG_ERR, "-network requires mask specification");
231675801Siedowse		return (1);
231775801Siedowse	}
231875801Siedowse	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
231975801Siedowse		syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
232075801Siedowse		return (1);
232175801Siedowse	}
23221558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
232345927Salex	    syslog(LOG_ERR, "-alldirs has multiple directories");
23241558Srgrimes	    return (1);
23251558Srgrimes	}
23261558Srgrimes	return (0);
23271558Srgrimes}
23281558Srgrimes
23291558Srgrimes/*
23301558Srgrimes * Check an absolute directory path for any symbolic links. Return true
23311558Srgrimes */
23321558Srgrimesint
23331558Srgrimescheck_dirpath(dirp)
23341558Srgrimes	char *dirp;
23351558Srgrimes{
23361558Srgrimes	char *cp;
23371558Srgrimes	int ret = 1;
23381558Srgrimes	struct stat sb;
23391558Srgrimes
23401558Srgrimes	cp = dirp + 1;
23411558Srgrimes	while (*cp && ret) {
23421558Srgrimes		if (*cp == '/') {
23431558Srgrimes			*cp = '\0';
23449336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23451558Srgrimes				ret = 0;
23461558Srgrimes			*cp = '/';
23471558Srgrimes		}
23481558Srgrimes		cp++;
23491558Srgrimes	}
23509336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23511558Srgrimes		ret = 0;
23521558Srgrimes	return (ret);
23531558Srgrimes}
23549336Sdfr
235575801Siedowse/*
235675801Siedowse * Make a netmask according to the specified prefix length. The ss_family
235775801Siedowse * and other non-address fields must be initialised before calling this.
235875801Siedowse */
235975801Siedowseint
236075801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen)
236174462Salfred{
236275801Siedowse	u_char *p;
236375801Siedowse	int bits, i, len;
236474462Salfred
236575801Siedowse	if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
236675801Siedowse		return (-1);
2367103949Smike	if (bitlen > len * CHAR_BIT)
236875801Siedowse		return (-1);
236974462Salfred
237075801Siedowse	for (i = 0; i < len; i++) {
2371103949Smike		bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
237275801Siedowse		*p++ = (1 << bits) - 1;
237375801Siedowse		bitlen -= bits;
237474462Salfred	}
237575801Siedowse	return 0;
237674462Salfred}
237774462Salfred
237875801Siedowse/*
237975801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask
238075801Siedowse * is acceptable (i.e. of the form 1...10....0).
238175801Siedowse */
238275801Siedowseint
238375801Siedowsecheckmask(struct sockaddr *sa)
238474462Salfred{
238575801Siedowse	u_char *mask;
238675801Siedowse	int i, len;
238774462Salfred
238875801Siedowse	if ((mask = sa_rawaddr(sa, &len)) == NULL)
238975801Siedowse		return (-1);
239075801Siedowse
239175801Siedowse	for (i = 0; i < len; i++)
239275801Siedowse		if (mask[i] != 0xff)
239375801Siedowse			break;
239475801Siedowse	if (i < len) {
239575801Siedowse		if (~mask[i] & (u_char)(~mask[i] + 1))
239675801Siedowse			return (-1);
239775801Siedowse		i++;
239874462Salfred	}
239975801Siedowse	for (; i < len; i++)
240075801Siedowse		if (mask[i] != 0)
240175801Siedowse			return (-1);
240275801Siedowse	return (0);
240374462Salfred}
240474462Salfred
240575801Siedowse/*
240675801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if
240775801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'.
240875801Siedowse * If samask is NULL, perform a full comparision.
240975801Siedowse */
241075801Siedowseint
241175801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
241274462Salfred{
241375801Siedowse	unsigned char *p1, *p2, *mask;
241475801Siedowse	int len, i;
241574462Salfred
241675801Siedowse	if (sa1->sa_family != sa2->sa_family ||
241775801Siedowse	    (p1 = sa_rawaddr(sa1, &len)) == NULL ||
241875801Siedowse	    (p2 = sa_rawaddr(sa2, NULL)) == NULL)
241975801Siedowse		return (1);
242075801Siedowse
242175801Siedowse	switch (sa1->sa_family) {
242274462Salfred	case AF_INET6:
242375801Siedowse		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
242475801Siedowse		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
242575801Siedowse			return (1);
242674462Salfred		break;
242774462Salfred	}
242874462Salfred
242975801Siedowse	/* Simple binary comparison if no mask specified. */
243075801Siedowse	if (samask == NULL)
243175801Siedowse		return (memcmp(p1, p2, len));
243274462Salfred
243375801Siedowse	/* Set up the mask, and do a mask-based comparison. */
243475801Siedowse	if (sa1->sa_family != samask->sa_family ||
243575801Siedowse	    (mask = sa_rawaddr(samask, NULL)) == NULL)
243675801Siedowse		return (1);
243774462Salfred
243875801Siedowse	for (i = 0; i < len; i++)
243975801Siedowse		if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
244075801Siedowse			return (1);
244175801Siedowse	return (0);
244274462Salfred}
244374462Salfred
244475801Siedowse/*
244575801Siedowse * Return a pointer to the part of the sockaddr that contains the
244675801Siedowse * raw address, and set *nbytes to its length in bytes. Returns
244775801Siedowse * NULL if the address family is unknown.
244875801Siedowse */
244975801Siedowsevoid *
245075801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) {
245175801Siedowse	void *p;
245274462Salfred	int len;
245374462Salfred
245475801Siedowse	switch (sa->sa_family) {
245574462Salfred	case AF_INET:
245675801Siedowse		len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
245775801Siedowse		p = &((struct sockaddr_in *)sa)->sin_addr;
245874462Salfred		break;
245974462Salfred	case AF_INET6:
246075801Siedowse		len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
246175801Siedowse		p = &((struct sockaddr_in6 *)sa)->sin6_addr;
246274462Salfred		break;
246374462Salfred	default:
246475801Siedowse		p = NULL;
246575801Siedowse		len = 0;
246674462Salfred	}
246774462Salfred
246875801Siedowse	if (nbytes != NULL)
246975801Siedowse		*nbytes = len;
247075801Siedowse	return (p);
247174462Salfred}
247274462Salfred
247375754Siedowsevoid
247475754Siedowsehuphandler(int sig)
247575754Siedowse{
247675754Siedowse	got_sighup = 1;
247775754Siedowse}
247875754Siedowse
247974462Salfredvoid terminate(sig)
248074462Salfredint sig;
248174462Salfred{
248274462Salfred	close(mountdlockfd);
248374462Salfred	unlink(MOUNTDLOCK);
248474792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
248574792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
248674462Salfred	exit (0);
248774462Salfred}
2488