xref: /freebsd/usr.sbin/mountd/mountd.c (revision c9e1c304c1f5553fb45f2851840cb6ee6e39243f)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1989, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
68fae3551SRodney W. Grimes  * Herb Hasler and Rick Macklem at The University of Guelph.
78fae3551SRodney W. Grimes  *
88fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
98fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
108fae3551SRodney W. Grimes  * are met:
118fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
128fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
138fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
148fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
158fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
168fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
178fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
188fae3551SRodney W. Grimes  *    without specific prior written permission.
198fae3551SRodney W. Grimes  *
208fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
218fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
228fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
238fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
248fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
258fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
268fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
278fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
288fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
298fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
308fae3551SRodney W. Grimes  * SUCH DAMAGE.
318fae3551SRodney W. Grimes  */
328fae3551SRodney W. Grimes 
338fae3551SRodney W. Grimes #ifndef lint
3474853402SPhilippe Charnier static const char copyright[] =
358fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
368fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
37d599144dSGarrett Wollman #endif /*not lint*/
388fae3551SRodney W. Grimes 
3974853402SPhilippe Charnier #if 0
4075201fa4SPhilippe Charnier #ifndef lint
4174853402SPhilippe Charnier static char sccsid[] = "@(#)mountd.c	8.15 (Berkeley) 5/1/95";
42d599144dSGarrett Wollman #endif /*not lint*/
4375201fa4SPhilippe Charnier #endif
4475201fa4SPhilippe Charnier 
4575201fa4SPhilippe Charnier #include <sys/cdefs.h>
4675201fa4SPhilippe Charnier __FBSDID("$FreeBSD$");
478fae3551SRodney W. Grimes 
488fae3551SRodney W. Grimes #include <sys/param.h>
498360efbdSAlfred Perlstein #include <sys/fcntl.h>
5091ca1a91SIan Dowse #include <sys/linker.h>
5191ca1a91SIan Dowse #include <sys/module.h>
52bcc1d071SRick Macklem #include <sys/mount.h>
53bcc1d071SRick Macklem #include <sys/stat.h>
54bcc1d071SRick Macklem #include <sys/sysctl.h>
55bcc1d071SRick Macklem #include <sys/syslog.h>
568fae3551SRodney W. Grimes 
578fae3551SRodney W. Grimes #include <rpc/rpc.h>
58bcb53b16SMartin Blapp #include <rpc/rpc_com.h>
598fae3551SRodney W. Grimes #include <rpc/pmap_clnt.h>
608360efbdSAlfred Perlstein #include <rpc/pmap_prot.h>
618360efbdSAlfred Perlstein #include <rpcsvc/mount.h>
62a62dc406SDoug Rabson #include <nfs/nfsproto.h>
63bcc1d071SRick Macklem #include <nfs/nfssvc.h>
6491196234SPeter Wemm #include <nfsserver/nfs.h>
658fae3551SRodney W. Grimes 
66bcc1d071SRick Macklem #include <fs/nfs/nfsport.h>
67bcc1d071SRick Macklem 
688fae3551SRodney W. Grimes #include <arpa/inet.h>
698fae3551SRodney W. Grimes 
708fae3551SRodney W. Grimes #include <ctype.h>
7174853402SPhilippe Charnier #include <err.h>
728fae3551SRodney W. Grimes #include <errno.h>
738fae3551SRodney W. Grimes #include <grp.h>
74a032b226SPawel Jakub Dawidek #include <libutil.h>
7589fdc4e1SMike Barcroft #include <limits.h>
768fae3551SRodney W. Grimes #include <netdb.h>
778fae3551SRodney W. Grimes #include <pwd.h>
788fae3551SRodney W. Grimes #include <signal.h>
798fae3551SRodney W. Grimes #include <stdio.h>
808fae3551SRodney W. Grimes #include <stdlib.h>
818fae3551SRodney W. Grimes #include <string.h>
828fae3551SRodney W. Grimes #include <unistd.h>
838fae3551SRodney W. Grimes #include "pathnames.h"
846a09faf2SCraig Rodrigues #include "mntopts.h"
858fae3551SRodney W. Grimes 
868fae3551SRodney W. Grimes #ifdef DEBUG
878fae3551SRodney W. Grimes #include <stdarg.h>
888fae3551SRodney W. Grimes #endif
898fae3551SRodney W. Grimes 
908fae3551SRodney W. Grimes /*
918fae3551SRodney W. Grimes  * Structures for keeping the mount list and export list
928fae3551SRodney W. Grimes  */
938fae3551SRodney W. Grimes struct mountlist {
948fae3551SRodney W. Grimes 	struct mountlist *ml_next;
950775314bSDoug Rabson 	char	ml_host[MNTNAMLEN+1];
960775314bSDoug Rabson 	char	ml_dirp[MNTPATHLEN+1];
978fae3551SRodney W. Grimes };
988fae3551SRodney W. Grimes 
998fae3551SRodney W. Grimes struct dirlist {
1008fae3551SRodney W. Grimes 	struct dirlist	*dp_left;
1018fae3551SRodney W. Grimes 	struct dirlist	*dp_right;
1028fae3551SRodney W. Grimes 	int		dp_flag;
1038fae3551SRodney W. Grimes 	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
1048fae3551SRodney W. Grimes 	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1058fae3551SRodney W. Grimes };
1068fae3551SRodney W. Grimes /* dp_flag bits */
1078fae3551SRodney W. Grimes #define	DP_DEFSET	0x1
108a62dc406SDoug Rabson #define DP_HOSTSET	0x2
1098fae3551SRodney W. Grimes 
1108fae3551SRodney W. Grimes struct exportlist {
1118fae3551SRodney W. Grimes 	struct exportlist *ex_next;
1128fae3551SRodney W. Grimes 	struct dirlist	*ex_dirl;
1138fae3551SRodney W. Grimes 	struct dirlist	*ex_defdir;
1148fae3551SRodney W. Grimes 	int		ex_flag;
1158fae3551SRodney W. Grimes 	fsid_t		ex_fs;
1168fae3551SRodney W. Grimes 	char		*ex_fsdir;
117cb3923e0SDoug Rabson 	char		*ex_indexfile;
118a9148abdSDoug Rabson 	int		ex_numsecflavors;
119a9148abdSDoug Rabson 	int		ex_secflavors[MAXSECFLAVORS];
120c3f86a25SRick Macklem 	int		ex_defnumsecflavors;
121c3f86a25SRick Macklem 	int		ex_defsecflavors[MAXSECFLAVORS];
1228fae3551SRodney W. Grimes };
1238fae3551SRodney W. Grimes /* ex_flag bits */
1248fae3551SRodney W. Grimes #define	EX_LINKED	0x1
1258fae3551SRodney W. Grimes 
1268fae3551SRodney W. Grimes struct netmsk {
1278360efbdSAlfred Perlstein 	struct sockaddr_storage nt_net;
12860caaee2SIan Dowse 	struct sockaddr_storage nt_mask;
1298fae3551SRodney W. Grimes 	char		*nt_name;
1308fae3551SRodney W. Grimes };
1318fae3551SRodney W. Grimes 
1328fae3551SRodney W. Grimes union grouptypes {
1338360efbdSAlfred Perlstein 	struct addrinfo *gt_addrinfo;
1348fae3551SRodney W. Grimes 	struct netmsk	gt_net;
1358fae3551SRodney W. Grimes };
1368fae3551SRodney W. Grimes 
1378fae3551SRodney W. Grimes struct grouplist {
1388fae3551SRodney W. Grimes 	int gr_type;
1398fae3551SRodney W. Grimes 	union grouptypes gr_ptr;
1408fae3551SRodney W. Grimes 	struct grouplist *gr_next;
141c3f86a25SRick Macklem 	int gr_numsecflavors;
142c3f86a25SRick Macklem 	int gr_secflavors[MAXSECFLAVORS];
1438fae3551SRodney W. Grimes };
1448fae3551SRodney W. Grimes /* Group types */
1458fae3551SRodney W. Grimes #define	GT_NULL		0x0
1468fae3551SRodney W. Grimes #define	GT_HOST		0x1
1478fae3551SRodney W. Grimes #define	GT_NET		0x2
1486d359f31SIan Dowse #define	GT_DEFAULT	0x3
1498b5a6d67SBill Paul #define GT_IGNORE	0x5
1508fae3551SRodney W. Grimes 
1518fae3551SRodney W. Grimes struct hostlist {
152a62dc406SDoug Rabson 	int		 ht_flag;	/* Uses DP_xx bits */
1538fae3551SRodney W. Grimes 	struct grouplist *ht_grp;
1548fae3551SRodney W. Grimes 	struct hostlist	 *ht_next;
1558fae3551SRodney W. Grimes };
1568fae3551SRodney W. Grimes 
157a62dc406SDoug Rabson struct fhreturn {
158a62dc406SDoug Rabson 	int	fhr_flag;
159a62dc406SDoug Rabson 	int	fhr_vers;
160a62dc406SDoug Rabson 	nfsfh_t	fhr_fh;
161a9148abdSDoug Rabson 	int	fhr_numsecflavors;
162a9148abdSDoug Rabson 	int	*fhr_secflavors;
163a62dc406SDoug Rabson };
164a62dc406SDoug Rabson 
1658fb6ad5dSRick Macklem #define	GETPORT_MAXTRY	20	/* Max tries to get a port # */
1668fb6ad5dSRick Macklem 
1678fae3551SRodney W. Grimes /* Global defs */
16819c46d8cSEdward Tomasz Napierala static char	*add_expdir(struct dirlist **, char *, int);
16919c46d8cSEdward Tomasz Napierala static void	add_dlist(struct dirlist **, struct dirlist *,
170c3f86a25SRick Macklem 		    struct grouplist *, int, struct exportlist *);
17119c46d8cSEdward Tomasz Napierala static void	add_mlist(char *, char *);
17219c46d8cSEdward Tomasz Napierala static int	check_dirpath(char *);
17319c46d8cSEdward Tomasz Napierala static int	check_options(struct dirlist *);
17419c46d8cSEdward Tomasz Napierala static int	checkmask(struct sockaddr *sa);
17519c46d8cSEdward Tomasz Napierala static int	chk_host(struct dirlist *, struct sockaddr *, int *, int *,
17619c46d8cSEdward Tomasz Napierala 		    int *, int **);
1778fb6ad5dSRick Macklem static int	create_service(struct netconfig *nconf);
1788fb6ad5dSRick Macklem static void	complete_service(struct netconfig *nconf, char *port_str);
1798fb6ad5dSRick Macklem static void	clearout_service(void);
18019c46d8cSEdward Tomasz Napierala static void	del_mlist(char *hostp, char *dirp);
18119c46d8cSEdward Tomasz Napierala static struct dirlist	*dirp_search(struct dirlist *, char *);
18219c46d8cSEdward Tomasz Napierala static int	do_mount(struct exportlist *, struct grouplist *, int,
18385429990SWarner Losh 		    struct xucred *, char *, int, struct statfs *);
18419c46d8cSEdward Tomasz Napierala static int	do_opt(char **, char **, struct exportlist *,
18519c46d8cSEdward Tomasz Napierala 		    struct grouplist *, int *, int *, struct xucred *);
18619c46d8cSEdward Tomasz Napierala static struct exportlist	*ex_search(fsid_t *);
18719c46d8cSEdward Tomasz Napierala static struct exportlist	*get_exp(void);
18819c46d8cSEdward Tomasz Napierala static void	free_dir(struct dirlist *);
18919c46d8cSEdward Tomasz Napierala static void	free_exp(struct exportlist *);
19019c46d8cSEdward Tomasz Napierala static void	free_grp(struct grouplist *);
19119c46d8cSEdward Tomasz Napierala static void	free_host(struct hostlist *);
19219c46d8cSEdward Tomasz Napierala static void	get_exportlist(void);
19319c46d8cSEdward Tomasz Napierala static int	get_host(char *, struct grouplist *, struct grouplist *);
19419c46d8cSEdward Tomasz Napierala static struct hostlist *get_ht(void);
19519c46d8cSEdward Tomasz Napierala static int	get_line(void);
19619c46d8cSEdward Tomasz Napierala static void	get_mountlist(void);
19719c46d8cSEdward Tomasz Napierala static int	get_net(char *, struct netmsk *, int);
19819c46d8cSEdward Tomasz Napierala static void	getexp_err(struct exportlist *, struct grouplist *);
19919c46d8cSEdward Tomasz Napierala static struct grouplist	*get_grp(void);
20019c46d8cSEdward Tomasz Napierala static void	hang_dirp(struct dirlist *, struct grouplist *,
20185429990SWarner Losh 				struct exportlist *, int);
20219c46d8cSEdward Tomasz Napierala static void	huphandler(int sig);
20319c46d8cSEdward Tomasz Napierala static int	makemask(struct sockaddr_storage *ssp, int bitlen);
20419c46d8cSEdward Tomasz Napierala static void	mntsrv(struct svc_req *, SVCXPRT *);
20519c46d8cSEdward Tomasz Napierala static void	nextfield(char **, char **);
20619c46d8cSEdward Tomasz Napierala static void	out_of_mem(void);
20719c46d8cSEdward Tomasz Napierala static void	parsecred(char *, struct xucred *);
20819c46d8cSEdward Tomasz Napierala static int	parsesec(char *, struct exportlist *);
20919c46d8cSEdward Tomasz Napierala static int	put_exlist(struct dirlist *, XDR *, struct dirlist *,
21019c46d8cSEdward Tomasz Napierala 		    int *, int);
21119c46d8cSEdward Tomasz Napierala static void	*sa_rawaddr(struct sockaddr *sa, int *nbytes);
21219c46d8cSEdward Tomasz Napierala static int	sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
21360caaee2SIan Dowse 		    struct sockaddr *samask);
21419c46d8cSEdward Tomasz Napierala static int	scan_tree(struct dirlist *, struct sockaddr *);
21585429990SWarner Losh static void	usage(void);
21619c46d8cSEdward Tomasz Napierala static int	xdr_dir(XDR *, char *);
21719c46d8cSEdward Tomasz Napierala static int	xdr_explist(XDR *, caddr_t);
21819c46d8cSEdward Tomasz Napierala static int	xdr_explist_brief(XDR *, caddr_t);
21919c46d8cSEdward Tomasz Napierala static int	xdr_explist_common(XDR *, caddr_t, int);
22019c46d8cSEdward Tomasz Napierala static int	xdr_fhs(XDR *, caddr_t);
22119c46d8cSEdward Tomasz Napierala static int	xdr_mlist(XDR *, caddr_t);
22219c46d8cSEdward Tomasz Napierala static void	terminate(int);
2238fae3551SRodney W. Grimes 
22419c46d8cSEdward Tomasz Napierala static struct exportlist *exphead;
22519c46d8cSEdward Tomasz Napierala static struct mountlist *mlhead;
22619c46d8cSEdward Tomasz Napierala static struct grouplist *grphead;
22719c46d8cSEdward Tomasz Napierala static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
22819c46d8cSEdward Tomasz Napierala static char **exnames;
22919c46d8cSEdward Tomasz Napierala static char **hosts = NULL;
23019c46d8cSEdward Tomasz Napierala static struct xucred def_anon = {
23176183f34SDima Dorfman 	XUCRED_VERSION,
2328fae3551SRodney W. Grimes 	(uid_t)-2,
2338fae3551SRodney W. Grimes 	1,
234c0511d3bSBrian Feldman 	{ (gid_t)-2 },
235c0511d3bSBrian Feldman 	NULL
2368fae3551SRodney W. Grimes };
23719c46d8cSEdward Tomasz Napierala static int force_v2 = 0;
23819c46d8cSEdward Tomasz Napierala static int resvport_only = 1;
23919c46d8cSEdward Tomasz Napierala static int nhosts = 0;
24019c46d8cSEdward Tomasz Napierala static int dir_only = 1;
24119c46d8cSEdward Tomasz Napierala static int dolog = 0;
24219c46d8cSEdward Tomasz Napierala static int got_sighup = 0;
24319c46d8cSEdward Tomasz Napierala static int xcreated = 0;
244d11e3645SMatteo Riondato 
24519c46d8cSEdward Tomasz Napierala static char *svcport_str = NULL;
2468fb6ad5dSRick Macklem static int mallocd_svcport = 0;
2478fb6ad5dSRick Macklem static int *sock_fd;
2488fb6ad5dSRick Macklem static int sock_fdcnt;
2498fb6ad5dSRick Macklem static int sock_fdpos;
250c548eb5cSRick Macklem static int suspend_nfsd = 0;
2518360efbdSAlfred Perlstein 
25219c46d8cSEdward Tomasz Napierala static int opt_flags;
2538360efbdSAlfred Perlstein static int have_v6 = 1;
2548360efbdSAlfred Perlstein 
25519c46d8cSEdward Tomasz Napierala static int v4root_phase = 0;
25619c46d8cSEdward Tomasz Napierala static char v4root_dirpath[PATH_MAX + 1];
25719c46d8cSEdward Tomasz Napierala static int has_publicfh = 0;
258bcc1d071SRick Macklem 
25919c46d8cSEdward Tomasz Napierala static struct pidfh *pfh = NULL;
26060caaee2SIan Dowse /* Bits for opt_flags above */
2618fae3551SRodney W. Grimes #define	OP_MAPROOT	0x01
2628fae3551SRodney W. Grimes #define	OP_MAPALL	0x02
26391196234SPeter Wemm /* 0x4 free */
2648fae3551SRodney W. Grimes #define	OP_MASK		0x08
2658fae3551SRodney W. Grimes #define	OP_NET		0x10
2668fae3551SRodney W. Grimes #define	OP_ALLDIRS	0x40
26760caaee2SIan Dowse #define	OP_HAVEMASK	0x80	/* A mask was specified or inferred. */
268288fa14aSJoerg Wunsch #define	OP_QUIET	0x100
2698360efbdSAlfred Perlstein #define OP_MASKLEN	0x200
270a9148abdSDoug Rabson #define OP_SEC		0x400
2718fae3551SRodney W. Grimes 
2728fae3551SRodney W. Grimes #ifdef DEBUG
27319c46d8cSEdward Tomasz Napierala static int debug = 1;
27419c46d8cSEdward Tomasz Napierala static void	SYSLOG(int, const char *, ...) __printflike(2, 3);
2758fae3551SRodney W. Grimes #define syslog SYSLOG
2768fae3551SRodney W. Grimes #else
27719c46d8cSEdward Tomasz Napierala static int debug = 0;
2788fae3551SRodney W. Grimes #endif
2798fae3551SRodney W. Grimes 
2808fae3551SRodney W. Grimes /*
2818fae3551SRodney W. Grimes  * Mountd server for NFS mount protocol as described in:
2828fae3551SRodney W. Grimes  * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2838fae3551SRodney W. Grimes  * The optional arguments are the exports file name
2848fae3551SRodney W. Grimes  * default: _PATH_EXPORTS
2858fae3551SRodney W. Grimes  * and "-n" to allow nonroot mount.
2868fae3551SRodney W. Grimes  */
2878fae3551SRodney W. Grimes int
288a7a7d96cSPhilippe Charnier main(int argc, char **argv)
2898fae3551SRodney W. Grimes {
29069d65572SIan Dowse 	fd_set readfds;
291d11e3645SMatteo Riondato 	struct netconfig *nconf;
292d11e3645SMatteo Riondato 	char *endptr, **hosts_bak;
293d11e3645SMatteo Riondato 	void *nc_handle;
294a032b226SPawel Jakub Dawidek 	pid_t otherpid;
295d11e3645SMatteo Riondato 	in_port_t svcport;
296d11e3645SMatteo Riondato 	int c, k, s;
297bcb53b16SMartin Blapp 	int maxrec = RPC_MAXDATASIZE;
2988fb6ad5dSRick Macklem 	int attempt_cnt, port_len, port_pos, ret;
2998fb6ad5dSRick Macklem 	char **port_list;
3008360efbdSAlfred Perlstein 
30101709abfSIan Dowse 	/* Check that another mountd isn't already running. */
3028b28aef2SPawel Jakub Dawidek 	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
303a032b226SPawel Jakub Dawidek 	if (pfh == NULL) {
304a032b226SPawel Jakub Dawidek 		if (errno == EEXIST)
305a032b226SPawel Jakub Dawidek 			errx(1, "mountd already running, pid: %d.", otherpid);
306a032b226SPawel Jakub Dawidek 		warn("cannot open or create pidfile");
307a032b226SPawel Jakub Dawidek 	}
3088360efbdSAlfred Perlstein 
3098360efbdSAlfred Perlstein 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
3108360efbdSAlfred Perlstein 	if (s < 0)
3118360efbdSAlfred Perlstein 		have_v6 = 0;
3128360efbdSAlfred Perlstein 	else
3138360efbdSAlfred Perlstein 		close(s);
3148fae3551SRodney W. Grimes 
31579b86807SEdward Tomasz Napierala 	while ((c = getopt(argc, argv, "2deh:lnp:rS")) != -1)
3168fae3551SRodney W. Grimes 		switch (c) {
3172a66cfc5SDoug Rabson 		case '2':
3182a66cfc5SDoug Rabson 			force_v2 = 1;
3192a66cfc5SDoug Rabson 			break;
3202179ae1eSRick Macklem 		case 'e':
3212a85df8cSRick Macklem 			/* now a no-op, since this is the default */
322bcc1d071SRick Macklem 			break;
323a62dc406SDoug Rabson 		case 'n':
324a62dc406SDoug Rabson 			resvport_only = 0;
325a62dc406SDoug Rabson 			break;
326a62dc406SDoug Rabson 		case 'r':
327a62dc406SDoug Rabson 			dir_only = 0;
328a62dc406SDoug Rabson 			break;
3296444ef3bSPoul-Henning Kamp 		case 'd':
3306444ef3bSPoul-Henning Kamp 			debug = debug ? 0 : 1;
3316444ef3bSPoul-Henning Kamp 			break;
332f51631d7SGuido van Rooij 		case 'l':
333c903443aSPeter Wemm 			dolog = 1;
334f51631d7SGuido van Rooij 			break;
335c203da27SBruce M Simpson 		case 'p':
336c203da27SBruce M Simpson 			endptr = NULL;
337c203da27SBruce M Simpson 			svcport = (in_port_t)strtoul(optarg, &endptr, 10);
338c203da27SBruce M Simpson 			if (endptr == NULL || *endptr != '\0' ||
339c203da27SBruce M Simpson 			    svcport == 0 || svcport >= IPPORT_MAX)
340c203da27SBruce M Simpson 				usage();
341d11e3645SMatteo Riondato 			svcport_str = strdup(optarg);
342d11e3645SMatteo Riondato 			break;
343d11e3645SMatteo Riondato 		case 'h':
344d11e3645SMatteo Riondato 			++nhosts;
345d11e3645SMatteo Riondato 			hosts_bak = hosts;
346d11e3645SMatteo Riondato 			hosts_bak = realloc(hosts, nhosts * sizeof(char *));
347d11e3645SMatteo Riondato 			if (hosts_bak == NULL) {
348d11e3645SMatteo Riondato 				if (hosts != NULL) {
349d11e3645SMatteo Riondato 					for (k = 0; k < nhosts; k++)
350d11e3645SMatteo Riondato 						free(hosts[k]);
351d11e3645SMatteo Riondato 					free(hosts);
352d11e3645SMatteo Riondato 					out_of_mem();
353d11e3645SMatteo Riondato 				}
354d11e3645SMatteo Riondato 			}
355d11e3645SMatteo Riondato 			hosts = hosts_bak;
356d11e3645SMatteo Riondato 			hosts[nhosts - 1] = strdup(optarg);
357d11e3645SMatteo Riondato 			if (hosts[nhosts - 1] == NULL) {
358d11e3645SMatteo Riondato 				for (k = 0; k < (nhosts - 1); k++)
359d11e3645SMatteo Riondato 					free(hosts[k]);
360d11e3645SMatteo Riondato 				free(hosts);
361d11e3645SMatteo Riondato 				out_of_mem();
362d11e3645SMatteo Riondato 			}
363c203da27SBruce M Simpson 			break;
364c548eb5cSRick Macklem 		case 'S':
365c548eb5cSRick Macklem 			suspend_nfsd = 1;
366c548eb5cSRick Macklem 			break;
3678fae3551SRodney W. Grimes 		default:
36874853402SPhilippe Charnier 			usage();
3698fae3551SRodney W. Grimes 		};
370bcc1d071SRick Macklem 
371bcc1d071SRick Macklem 	if (modfind("nfsd") < 0) {
372bcc1d071SRick Macklem 		/* Not present in kernel, try loading it */
373bcc1d071SRick Macklem 		if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
374bcc1d071SRick Macklem 			errx(1, "NFS server is not available");
375bcc1d071SRick Macklem 	}
376bcc1d071SRick Macklem 
3778fae3551SRodney W. Grimes 	argc -= optind;
3788fae3551SRodney W. Grimes 	argv += optind;
3798fae3551SRodney W. Grimes 	grphead = (struct grouplist *)NULL;
3808fae3551SRodney W. Grimes 	exphead = (struct exportlist *)NULL;
3818fae3551SRodney W. Grimes 	mlhead = (struct mountlist *)NULL;
38296968c22SPawel Jakub Dawidek 	if (argc > 0)
38396968c22SPawel Jakub Dawidek 		exnames = argv;
38496968c22SPawel Jakub Dawidek 	else
38596968c22SPawel Jakub Dawidek 		exnames = exnames_default;
3868fae3551SRodney W. Grimes 	openlog("mountd", LOG_PID, LOG_DAEMON);
3878fae3551SRodney W. Grimes 	if (debug)
38874853402SPhilippe Charnier 		warnx("getting export list");
3898fae3551SRodney W. Grimes 	get_exportlist();
3908fae3551SRodney W. Grimes 	if (debug)
39174853402SPhilippe Charnier 		warnx("getting mount list");
3928fae3551SRodney W. Grimes 	get_mountlist();
3938fae3551SRodney W. Grimes 	if (debug)
39474853402SPhilippe Charnier 		warnx("here we go");
3958fae3551SRodney W. Grimes 	if (debug == 0) {
3968fae3551SRodney W. Grimes 		daemon(0, 0);
3978fae3551SRodney W. Grimes 		signal(SIGINT, SIG_IGN);
3988fae3551SRodney W. Grimes 		signal(SIGQUIT, SIG_IGN);
3998fae3551SRodney W. Grimes 	}
40069d65572SIan Dowse 	signal(SIGHUP, huphandler);
4018360efbdSAlfred Perlstein 	signal(SIGTERM, terminate);
40209fc9dc6SCraig Rodrigues 	signal(SIGPIPE, SIG_IGN);
403a032b226SPawel Jakub Dawidek 
404a032b226SPawel Jakub Dawidek 	pidfile_write(pfh);
405a032b226SPawel Jakub Dawidek 
4060775314bSDoug Rabson 	rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
4070775314bSDoug Rabson 	rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
408bcb53b16SMartin Blapp 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
409bcb53b16SMartin Blapp 
410c6e5e158SGuido van Rooij 	if (!resvport_only) {
4114a0785aaSPeter Wemm 		if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
4124a0785aaSPeter Wemm 		    &resvport_only, sizeof(resvport_only)) != 0 &&
4134a0785aaSPeter Wemm 		    errno != ENOENT) {
414394da4c1SGuido van Rooij 			syslog(LOG_ERR, "sysctl: %m");
415394da4c1SGuido van Rooij 			exit(1);
416394da4c1SGuido van Rooij 		}
417c6e5e158SGuido van Rooij 	}
418c203da27SBruce M Simpson 
419d11e3645SMatteo Riondato 	/*
420d11e3645SMatteo Riondato 	 * If no hosts were specified, add a wildcard entry to bind to
421d11e3645SMatteo Riondato 	 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
422d11e3645SMatteo Riondato 	 * list.
423d11e3645SMatteo Riondato 	 */
424d11e3645SMatteo Riondato 	if (nhosts == 0) {
425*c9e1c304SUlrich Spörlein 		hosts = malloc(sizeof(char *));
426d11e3645SMatteo Riondato 		if (hosts == NULL)
427d11e3645SMatteo Riondato 			out_of_mem();
428d11e3645SMatteo Riondato 		hosts[0] = "*";
429d11e3645SMatteo Riondato 		nhosts = 1;
430d11e3645SMatteo Riondato 	} else {
431d11e3645SMatteo Riondato 		hosts_bak = hosts;
432d11e3645SMatteo Riondato 		if (have_v6) {
433d11e3645SMatteo Riondato 			hosts_bak = realloc(hosts, (nhosts + 2) *
434d11e3645SMatteo Riondato 			    sizeof(char *));
435d11e3645SMatteo Riondato 			if (hosts_bak == NULL) {
436d11e3645SMatteo Riondato 				for (k = 0; k < nhosts; k++)
437d11e3645SMatteo Riondato 					free(hosts[k]);
438d11e3645SMatteo Riondato 		    		free(hosts);
439d11e3645SMatteo Riondato 		    		out_of_mem();
440c203da27SBruce M Simpson 			} else
441d11e3645SMatteo Riondato 				hosts = hosts_bak;
442d11e3645SMatteo Riondato 			nhosts += 2;
443d11e3645SMatteo Riondato 			hosts[nhosts - 2] = "::1";
444d11e3645SMatteo Riondato 		} else {
445d11e3645SMatteo Riondato 			hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
446d11e3645SMatteo Riondato 			if (hosts_bak == NULL) {
447d11e3645SMatteo Riondato 				for (k = 0; k < nhosts; k++)
448d11e3645SMatteo Riondato 					free(hosts[k]);
449d11e3645SMatteo Riondato 				free(hosts);
450d11e3645SMatteo Riondato 				out_of_mem();
451d11e3645SMatteo Riondato 			} else {
452d11e3645SMatteo Riondato 				nhosts += 1;
453d11e3645SMatteo Riondato 				hosts = hosts_bak;
4548fae3551SRodney W. Grimes 			}
455d11e3645SMatteo Riondato 		}
4568360efbdSAlfred Perlstein 
457d11e3645SMatteo Riondato 		hosts[nhosts - 1] = "127.0.0.1";
4588360efbdSAlfred Perlstein 	}
4598360efbdSAlfred Perlstein 
4608fb6ad5dSRick Macklem 	attempt_cnt = 1;
4618fb6ad5dSRick Macklem 	sock_fdcnt = 0;
4628fb6ad5dSRick Macklem 	sock_fd = NULL;
4638fb6ad5dSRick Macklem 	port_list = NULL;
4648fb6ad5dSRick Macklem 	port_len = 0;
465d11e3645SMatteo Riondato 	nc_handle = setnetconfig();
466d11e3645SMatteo Riondato 	while ((nconf = getnetconfig(nc_handle))) {
467d11e3645SMatteo Riondato 		if (nconf->nc_flag & NC_VISIBLE) {
468d11e3645SMatteo Riondato 			if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
469d11e3645SMatteo Riondato 			    "inet6") == 0) {
470d11e3645SMatteo Riondato 				/* DO NOTHING */
4718fb6ad5dSRick Macklem 			} else {
4728fb6ad5dSRick Macklem 				ret = create_service(nconf);
4738fb6ad5dSRick Macklem 				if (ret == 1)
4748fb6ad5dSRick Macklem 					/* Ignore this call */
4758fb6ad5dSRick Macklem 					continue;
4768fb6ad5dSRick Macklem 				if (ret < 0) {
4778fb6ad5dSRick Macklem 					/*
4788fb6ad5dSRick Macklem 					 * Failed to bind port, so close off
4798fb6ad5dSRick Macklem 					 * all sockets created and try again
4808fb6ad5dSRick Macklem 					 * if the port# was dynamically
4818fb6ad5dSRick Macklem 					 * assigned via bind(2).
4828fb6ad5dSRick Macklem 					 */
4838fb6ad5dSRick Macklem 					clearout_service();
4848fb6ad5dSRick Macklem 					if (mallocd_svcport != 0 &&
4858fb6ad5dSRick Macklem 					    attempt_cnt < GETPORT_MAXTRY) {
4868fb6ad5dSRick Macklem 						free(svcport_str);
4878fb6ad5dSRick Macklem 						svcport_str = NULL;
4888fb6ad5dSRick Macklem 						mallocd_svcport = 0;
4898fb6ad5dSRick Macklem 					} else {
4908fb6ad5dSRick Macklem 						errno = EADDRINUSE;
4918fb6ad5dSRick Macklem 						syslog(LOG_ERR,
4928fb6ad5dSRick Macklem 						    "bindresvport_sa: %m");
4938fb6ad5dSRick Macklem 						exit(1);
4948fb6ad5dSRick Macklem 					}
4958fb6ad5dSRick Macklem 
4968fb6ad5dSRick Macklem 					/* Start over at the first service. */
4978fb6ad5dSRick Macklem 					free(sock_fd);
4988fb6ad5dSRick Macklem 					sock_fdcnt = 0;
4998fb6ad5dSRick Macklem 					sock_fd = NULL;
5008fb6ad5dSRick Macklem 					nc_handle = setnetconfig();
5018fb6ad5dSRick Macklem 					attempt_cnt++;
5028fb6ad5dSRick Macklem 				} else if (mallocd_svcport != 0 &&
5038fb6ad5dSRick Macklem 				    attempt_cnt == GETPORT_MAXTRY) {
5048fb6ad5dSRick Macklem 					/*
5058fb6ad5dSRick Macklem 					 * For the last attempt, allow
5068fb6ad5dSRick Macklem 					 * different port #s for each nconf
5078fb6ad5dSRick Macklem 					 * by saving the svcport_str and
5088fb6ad5dSRick Macklem 					 * setting it back to NULL.
5098fb6ad5dSRick Macklem 					 */
5108fb6ad5dSRick Macklem 					port_list = realloc(port_list,
5118fb6ad5dSRick Macklem 					    (port_len + 1) * sizeof(char *));
5128fb6ad5dSRick Macklem 					if (port_list == NULL)
5138fb6ad5dSRick Macklem 						out_of_mem();
5148fb6ad5dSRick Macklem 					port_list[port_len++] = svcport_str;
5158fb6ad5dSRick Macklem 					svcport_str = NULL;
5168fb6ad5dSRick Macklem 					mallocd_svcport = 0;
5178fb6ad5dSRick Macklem 				}
5188fb6ad5dSRick Macklem 			}
5198fb6ad5dSRick Macklem 		}
5208fb6ad5dSRick Macklem 	}
5218fb6ad5dSRick Macklem 
5228fb6ad5dSRick Macklem 	/*
5238fb6ad5dSRick Macklem 	 * Successfully bound the ports, so call complete_service() to
5248fb6ad5dSRick Macklem 	 * do the rest of the setup on the service(s).
5258fb6ad5dSRick Macklem 	 */
5268fb6ad5dSRick Macklem 	sock_fdpos = 0;
5278fb6ad5dSRick Macklem 	port_pos = 0;
5288fb6ad5dSRick Macklem 	nc_handle = setnetconfig();
5298fb6ad5dSRick Macklem 	while ((nconf = getnetconfig(nc_handle))) {
5308fb6ad5dSRick Macklem 		if (nconf->nc_flag & NC_VISIBLE) {
5318fb6ad5dSRick Macklem 			if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
5328fb6ad5dSRick Macklem 			    "inet6") == 0) {
5338fb6ad5dSRick Macklem 				/* DO NOTHING */
5348fb6ad5dSRick Macklem 			} else if (port_list != NULL) {
5358fb6ad5dSRick Macklem 				if (port_pos >= port_len) {
5368fb6ad5dSRick Macklem 					syslog(LOG_ERR, "too many port#s");
5378fb6ad5dSRick Macklem 					exit(1);
5388fb6ad5dSRick Macklem 				}
5398fb6ad5dSRick Macklem 				complete_service(nconf, port_list[port_pos++]);
540c203da27SBruce M Simpson 			} else
5418fb6ad5dSRick Macklem 				complete_service(nconf, svcport_str);
5428360efbdSAlfred Perlstein 		}
543d11e3645SMatteo Riondato 	}
544d11e3645SMatteo Riondato 	endnetconfig(nc_handle);
5458fb6ad5dSRick Macklem 	free(sock_fd);
5468fb6ad5dSRick Macklem 	if (port_list != NULL) {
5478fb6ad5dSRick Macklem 		for (port_pos = 0; port_pos < port_len; port_pos++)
5488fb6ad5dSRick Macklem 			free(port_list[port_pos]);
5498fb6ad5dSRick Macklem 		free(port_list);
5508fb6ad5dSRick Macklem 	}
5518360efbdSAlfred Perlstein 
5528360efbdSAlfred Perlstein 	if (xcreated == 0) {
5538360efbdSAlfred Perlstein 		syslog(LOG_ERR, "could not create any services");
5542a66cfc5SDoug Rabson 		exit(1);
5552a66cfc5SDoug Rabson 	}
55669d65572SIan Dowse 
55769d65572SIan Dowse 	/* Expand svc_run() here so that we can call get_exportlist(). */
55869d65572SIan Dowse 	for (;;) {
55969d65572SIan Dowse 		if (got_sighup) {
56069d65572SIan Dowse 			get_exportlist();
56169d65572SIan Dowse 			got_sighup = 0;
56269d65572SIan Dowse 		}
56369d65572SIan Dowse 		readfds = svc_fdset;
56469d65572SIan Dowse 		switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
56569d65572SIan Dowse 		case -1:
56669d65572SIan Dowse 			if (errno == EINTR)
56769d65572SIan Dowse                                 continue;
56869d65572SIan Dowse 			syslog(LOG_ERR, "mountd died: select: %m");
56974853402SPhilippe Charnier 			exit(1);
57069d65572SIan Dowse 		case 0:
57169d65572SIan Dowse 			continue;
57269d65572SIan Dowse 		default:
57369d65572SIan Dowse 			svc_getreqset(&readfds);
57469d65572SIan Dowse 		}
57569d65572SIan Dowse 	}
57674853402SPhilippe Charnier }
57774853402SPhilippe Charnier 
578d11e3645SMatteo Riondato /*
579d11e3645SMatteo Riondato  * This routine creates and binds sockets on the appropriate
5808fb6ad5dSRick Macklem  * addresses. It gets called one time for each transport.
5818fb6ad5dSRick Macklem  * It returns 0 upon success, 1 for ingore the call and -1 to indicate
5828fb6ad5dSRick Macklem  * bind failed with EADDRINUSE.
5838fb6ad5dSRick Macklem  * Any file descriptors that have been created are stored in sock_fd and
5848fb6ad5dSRick Macklem  * the total count of them is maintained in sock_fdcnt.
585d11e3645SMatteo Riondato  */
5868fb6ad5dSRick Macklem static int
587d11e3645SMatteo Riondato create_service(struct netconfig *nconf)
588d11e3645SMatteo Riondato {
589d11e3645SMatteo Riondato 	struct addrinfo hints, *res = NULL;
590d11e3645SMatteo Riondato 	struct sockaddr_in *sin;
591d11e3645SMatteo Riondato 	struct sockaddr_in6 *sin6;
592d11e3645SMatteo Riondato 	struct __rpc_sockinfo si;
593d11e3645SMatteo Riondato 	int aicode;
594d11e3645SMatteo Riondato 	int fd;
595d11e3645SMatteo Riondato 	int nhostsbak;
596d11e3645SMatteo Riondato 	int one = 1;
597d11e3645SMatteo Riondato 	int r;
598d11e3645SMatteo Riondato 	u_int32_t host_addr[4];  /* IPv4 or IPv6 */
5998fb6ad5dSRick Macklem 	int mallocd_res;
600d11e3645SMatteo Riondato 
601d11e3645SMatteo Riondato 	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
602d11e3645SMatteo Riondato 	    (nconf->nc_semantics != NC_TPI_COTS) &&
603d11e3645SMatteo Riondato 	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
6048fb6ad5dSRick Macklem 		return (1);	/* not my type */
605d11e3645SMatteo Riondato 
606d11e3645SMatteo Riondato 	/*
607d11e3645SMatteo Riondato 	 * XXX - using RPC library internal functions.
608d11e3645SMatteo Riondato 	 */
609d11e3645SMatteo Riondato 	if (!__rpc_nconf2sockinfo(nconf, &si)) {
610d11e3645SMatteo Riondato 		syslog(LOG_ERR, "cannot get information for %s",
611d11e3645SMatteo Riondato 		    nconf->nc_netid);
6128fb6ad5dSRick Macklem 		return (1);
613d11e3645SMatteo Riondato 	}
614d11e3645SMatteo Riondato 
615d11e3645SMatteo Riondato 	/* Get mountd's address on this transport */
616d11e3645SMatteo Riondato 	memset(&hints, 0, sizeof hints);
617d11e3645SMatteo Riondato 	hints.ai_family = si.si_af;
618d11e3645SMatteo Riondato 	hints.ai_socktype = si.si_socktype;
619d11e3645SMatteo Riondato 	hints.ai_protocol = si.si_proto;
620d11e3645SMatteo Riondato 
621d11e3645SMatteo Riondato 	/*
622d11e3645SMatteo Riondato 	 * Bind to specific IPs if asked to
623d11e3645SMatteo Riondato 	 */
624d11e3645SMatteo Riondato 	nhostsbak = nhosts;
625d11e3645SMatteo Riondato 	while (nhostsbak > 0) {
626d11e3645SMatteo Riondato 		--nhostsbak;
6278fb6ad5dSRick Macklem 		sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
6288fb6ad5dSRick Macklem 		if (sock_fd == NULL)
6298fb6ad5dSRick Macklem 			out_of_mem();
6308fb6ad5dSRick Macklem 		sock_fd[sock_fdcnt++] = -1;	/* Set invalid for now. */
6318fb6ad5dSRick Macklem 		mallocd_res = 0;
6328fb6ad5dSRick Macklem 
6339745de4cSRyan Stone 		hints.ai_flags = AI_PASSIVE;
6349745de4cSRyan Stone 
635d11e3645SMatteo Riondato 		/*
636d11e3645SMatteo Riondato 		 * XXX - using RPC library internal functions.
637d11e3645SMatteo Riondato 		 */
638d11e3645SMatteo Riondato 		if ((fd = __rpc_nconf2fd(nconf)) < 0) {
639d11e3645SMatteo Riondato 			int non_fatal = 0;
640a5752d55SKevin Lo 	    		if (errno == EAFNOSUPPORT &&
641d11e3645SMatteo Riondato 			    nconf->nc_semantics != NC_TPI_CLTS)
642d11e3645SMatteo Riondato 				non_fatal = 1;
643d11e3645SMatteo Riondato 
644d11e3645SMatteo Riondato 			syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
645d11e3645SMatteo Riondato 			    "cannot create socket for %s", nconf->nc_netid);
6468fb6ad5dSRick Macklem 			if (non_fatal != 0)
6478fb6ad5dSRick Macklem 				continue;
6488fb6ad5dSRick Macklem 			exit(1);
649d11e3645SMatteo Riondato 		}
650d11e3645SMatteo Riondato 
651d11e3645SMatteo Riondato 		switch (hints.ai_family) {
652d11e3645SMatteo Riondato 		case AF_INET:
653d11e3645SMatteo Riondato 			if (inet_pton(AF_INET, hosts[nhostsbak],
654d11e3645SMatteo Riondato 			    host_addr) == 1) {
6558fb6ad5dSRick Macklem 				hints.ai_flags |= AI_NUMERICHOST;
656d11e3645SMatteo Riondato 			} else {
657d11e3645SMatteo Riondato 				/*
658d11e3645SMatteo Riondato 				 * Skip if we have an AF_INET6 address.
659d11e3645SMatteo Riondato 				 */
660d11e3645SMatteo Riondato 				if (inet_pton(AF_INET6, hosts[nhostsbak],
661d11e3645SMatteo Riondato 				    host_addr) == 1) {
662d11e3645SMatteo Riondato 					close(fd);
663d11e3645SMatteo Riondato 					continue;
664d11e3645SMatteo Riondato 				}
665d11e3645SMatteo Riondato 			}
666d11e3645SMatteo Riondato 			break;
667d11e3645SMatteo Riondato 		case AF_INET6:
668d11e3645SMatteo Riondato 			if (inet_pton(AF_INET6, hosts[nhostsbak],
669d11e3645SMatteo Riondato 			    host_addr) == 1) {
6708fb6ad5dSRick Macklem 				hints.ai_flags |= AI_NUMERICHOST;
671d11e3645SMatteo Riondato 			} else {
672d11e3645SMatteo Riondato 				/*
673d11e3645SMatteo Riondato 				 * Skip if we have an AF_INET address.
674d11e3645SMatteo Riondato 				 */
675d11e3645SMatteo Riondato 				if (inet_pton(AF_INET, hosts[nhostsbak],
676d11e3645SMatteo Riondato 				    host_addr) == 1) {
677d11e3645SMatteo Riondato 					close(fd);
678d11e3645SMatteo Riondato 					continue;
679d11e3645SMatteo Riondato 				}
680d11e3645SMatteo Riondato 			}
681d11e3645SMatteo Riondato 
682d11e3645SMatteo Riondato 			/*
683d11e3645SMatteo Riondato 			 * We're doing host-based access checks here, so don't
684d11e3645SMatteo Riondato 			 * allow v4-in-v6 to confuse things. The kernel will
685d11e3645SMatteo Riondato 			 * disable it by default on NFS sockets too.
686d11e3645SMatteo Riondato 			 */
687d11e3645SMatteo Riondato 			if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
688d11e3645SMatteo Riondato 			    sizeof one) < 0) {
689d11e3645SMatteo Riondato 				syslog(LOG_ERR,
690d11e3645SMatteo Riondato 				    "can't disable v4-in-v6 on IPv6 socket");
691d11e3645SMatteo Riondato 				exit(1);
692d11e3645SMatteo Riondato 			}
693d11e3645SMatteo Riondato 			break;
694d11e3645SMatteo Riondato 		default:
695d11e3645SMatteo Riondato 			break;
696d11e3645SMatteo Riondato 		}
697d11e3645SMatteo Riondato 
698d11e3645SMatteo Riondato 		/*
699d11e3645SMatteo Riondato 		 * If no hosts were specified, just bind to INADDR_ANY
700d11e3645SMatteo Riondato 		 */
701d11e3645SMatteo Riondato 		if (strcmp("*", hosts[nhostsbak]) == 0) {
702d11e3645SMatteo Riondato 			if (svcport_str == NULL) {
703d11e3645SMatteo Riondato 				res = malloc(sizeof(struct addrinfo));
704d11e3645SMatteo Riondato 				if (res == NULL)
705d11e3645SMatteo Riondato 					out_of_mem();
7068fb6ad5dSRick Macklem 				mallocd_res = 1;
707d11e3645SMatteo Riondato 				res->ai_flags = hints.ai_flags;
708d11e3645SMatteo Riondato 				res->ai_family = hints.ai_family;
709d11e3645SMatteo Riondato 				res->ai_protocol = hints.ai_protocol;
710d11e3645SMatteo Riondato 				switch (res->ai_family) {
711d11e3645SMatteo Riondato 				case AF_INET:
712d11e3645SMatteo Riondato 					sin = malloc(sizeof(struct sockaddr_in));
713d11e3645SMatteo Riondato 					if (sin == NULL)
714d11e3645SMatteo Riondato 						out_of_mem();
715d11e3645SMatteo Riondato 					sin->sin_family = AF_INET;
716d11e3645SMatteo Riondato 					sin->sin_port = htons(0);
717d11e3645SMatteo Riondato 					sin->sin_addr.s_addr = htonl(INADDR_ANY);
718d11e3645SMatteo Riondato 					res->ai_addr = (struct sockaddr*) sin;
719d11e3645SMatteo Riondato 					res->ai_addrlen = (socklen_t)
7208fb6ad5dSRick Macklem 					    sizeof(struct sockaddr_in);
721d11e3645SMatteo Riondato 					break;
722d11e3645SMatteo Riondato 				case AF_INET6:
723d11e3645SMatteo Riondato 					sin6 = malloc(sizeof(struct sockaddr_in6));
72489ca9145SSimon L. B. Nielsen 					if (sin6 == NULL)
725d11e3645SMatteo Riondato 						out_of_mem();
726d11e3645SMatteo Riondato 					sin6->sin6_family = AF_INET6;
727d11e3645SMatteo Riondato 					sin6->sin6_port = htons(0);
728d11e3645SMatteo Riondato 					sin6->sin6_addr = in6addr_any;
729d11e3645SMatteo Riondato 					res->ai_addr = (struct sockaddr*) sin6;
730d11e3645SMatteo Riondato 					res->ai_addrlen = (socklen_t)
7318fb6ad5dSRick Macklem 					    sizeof(struct sockaddr_in6);
732d11e3645SMatteo Riondato 					break;
733d11e3645SMatteo Riondato 				default:
7348fb6ad5dSRick Macklem 					syslog(LOG_ERR, "bad addr fam %d",
7358fb6ad5dSRick Macklem 					    res->ai_family);
7368fb6ad5dSRick Macklem 					exit(1);
737d11e3645SMatteo Riondato 				}
738d11e3645SMatteo Riondato 			} else {
739d11e3645SMatteo Riondato 				if ((aicode = getaddrinfo(NULL, svcport_str,
740d11e3645SMatteo Riondato 				    &hints, &res)) != 0) {
741d11e3645SMatteo Riondato 					syslog(LOG_ERR,
742d11e3645SMatteo Riondato 					    "cannot get local address for %s: %s",
743d11e3645SMatteo Riondato 					    nconf->nc_netid,
744d11e3645SMatteo Riondato 					    gai_strerror(aicode));
7458fb6ad5dSRick Macklem 					close(fd);
746d11e3645SMatteo Riondato 					continue;
747d11e3645SMatteo Riondato 				}
748d11e3645SMatteo Riondato 			}
749d11e3645SMatteo Riondato 		} else {
750d11e3645SMatteo Riondato 			if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
751d11e3645SMatteo Riondato 			    &hints, &res)) != 0) {
752d11e3645SMatteo Riondato 				syslog(LOG_ERR,
753d11e3645SMatteo Riondato 				    "cannot get local address for %s: %s",
754d11e3645SMatteo Riondato 				    nconf->nc_netid, gai_strerror(aicode));
7558fb6ad5dSRick Macklem 				close(fd);
756d11e3645SMatteo Riondato 				continue;
757d11e3645SMatteo Riondato 			}
758d11e3645SMatteo Riondato 		}
759d11e3645SMatteo Riondato 
7608fb6ad5dSRick Macklem 		/* Store the fd. */
7618fb6ad5dSRick Macklem 		sock_fd[sock_fdcnt - 1] = fd;
7628fb6ad5dSRick Macklem 
7638fb6ad5dSRick Macklem 		/* Now, attempt the bind. */
764d11e3645SMatteo Riondato 		r = bindresvport_sa(fd, res->ai_addr);
765d11e3645SMatteo Riondato 		if (r != 0) {
7668fb6ad5dSRick Macklem 			if (errno == EADDRINUSE && mallocd_svcport != 0) {
7678fb6ad5dSRick Macklem 				if (mallocd_res != 0) {
7688fb6ad5dSRick Macklem 					free(res->ai_addr);
7698fb6ad5dSRick Macklem 					free(res);
7708fb6ad5dSRick Macklem 				} else
7718fb6ad5dSRick Macklem 					freeaddrinfo(res);
7728fb6ad5dSRick Macklem 				return (-1);
7738fb6ad5dSRick Macklem 			}
774d11e3645SMatteo Riondato 			syslog(LOG_ERR, "bindresvport_sa: %m");
775d11e3645SMatteo Riondato 			exit(1);
776d11e3645SMatteo Riondato 		}
777d11e3645SMatteo Riondato 
7788fb6ad5dSRick Macklem 		if (svcport_str == NULL) {
7798fb6ad5dSRick Macklem 			svcport_str = malloc(NI_MAXSERV * sizeof(char));
7808fb6ad5dSRick Macklem 			if (svcport_str == NULL)
7818fb6ad5dSRick Macklem 				out_of_mem();
7828fb6ad5dSRick Macklem 			mallocd_svcport = 1;
7838fb6ad5dSRick Macklem 
7848fb6ad5dSRick Macklem 			if (getnameinfo(res->ai_addr,
7858fb6ad5dSRick Macklem 			    res->ai_addr->sa_len, NULL, NI_MAXHOST,
7868fb6ad5dSRick Macklem 			    svcport_str, NI_MAXSERV * sizeof(char),
7878fb6ad5dSRick Macklem 			    NI_NUMERICHOST | NI_NUMERICSERV))
7888fb6ad5dSRick Macklem 				errx(1, "Cannot get port number");
7898fb6ad5dSRick Macklem 		}
7908fb6ad5dSRick Macklem 		if (mallocd_res != 0) {
7918fb6ad5dSRick Macklem 			free(res->ai_addr);
7928fb6ad5dSRick Macklem 			free(res);
7938fb6ad5dSRick Macklem 		} else
7948fb6ad5dSRick Macklem 			freeaddrinfo(res);
7958fb6ad5dSRick Macklem 		res = NULL;
7968fb6ad5dSRick Macklem 	}
7978fb6ad5dSRick Macklem 	return (0);
7988fb6ad5dSRick Macklem }
7998fb6ad5dSRick Macklem 
8008fb6ad5dSRick Macklem /*
8018fb6ad5dSRick Macklem  * Called after all the create_service() calls have succeeded, to complete
8028fb6ad5dSRick Macklem  * the setup and registration.
8038fb6ad5dSRick Macklem  */
8048fb6ad5dSRick Macklem static void
8058fb6ad5dSRick Macklem complete_service(struct netconfig *nconf, char *port_str)
8068fb6ad5dSRick Macklem {
8078fb6ad5dSRick Macklem 	struct addrinfo hints, *res = NULL;
8088fb6ad5dSRick Macklem 	struct __rpc_sockinfo si;
8098fb6ad5dSRick Macklem 	struct netbuf servaddr;
8108fb6ad5dSRick Macklem 	SVCXPRT	*transp = NULL;
8118fb6ad5dSRick Macklem 	int aicode, fd, nhostsbak;
8128fb6ad5dSRick Macklem 	int registered = 0;
8138fb6ad5dSRick Macklem 
8148fb6ad5dSRick Macklem 	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
8158fb6ad5dSRick Macklem 	    (nconf->nc_semantics != NC_TPI_COTS) &&
8168fb6ad5dSRick Macklem 	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
8178fb6ad5dSRick Macklem 		return;	/* not my type */
8188fb6ad5dSRick Macklem 
8198fb6ad5dSRick Macklem 	/*
8208fb6ad5dSRick Macklem 	 * XXX - using RPC library internal functions.
8218fb6ad5dSRick Macklem 	 */
8228fb6ad5dSRick Macklem 	if (!__rpc_nconf2sockinfo(nconf, &si)) {
8238fb6ad5dSRick Macklem 		syslog(LOG_ERR, "cannot get information for %s",
8248fb6ad5dSRick Macklem 		    nconf->nc_netid);
8258fb6ad5dSRick Macklem 		return;
8268fb6ad5dSRick Macklem 	}
8278fb6ad5dSRick Macklem 
8288fb6ad5dSRick Macklem 	nhostsbak = nhosts;
8298fb6ad5dSRick Macklem 	while (nhostsbak > 0) {
8308fb6ad5dSRick Macklem 		--nhostsbak;
8318fb6ad5dSRick Macklem 		if (sock_fdpos >= sock_fdcnt) {
8328fb6ad5dSRick Macklem 			/* Should never happen. */
8338fb6ad5dSRick Macklem 			syslog(LOG_ERR, "Ran out of socket fd's");
8348fb6ad5dSRick Macklem 			return;
8358fb6ad5dSRick Macklem 		}
8368fb6ad5dSRick Macklem 		fd = sock_fd[sock_fdpos++];
8378fb6ad5dSRick Macklem 		if (fd < 0)
8388fb6ad5dSRick Macklem 			continue;
8398fb6ad5dSRick Macklem 
840d11e3645SMatteo Riondato 		if (nconf->nc_semantics != NC_TPI_CLTS)
841d11e3645SMatteo Riondato 			listen(fd, SOMAXCONN);
842d11e3645SMatteo Riondato 
843d11e3645SMatteo Riondato 		if (nconf->nc_semantics == NC_TPI_CLTS )
844d11e3645SMatteo Riondato 			transp = svc_dg_create(fd, 0, 0);
845d11e3645SMatteo Riondato 		else
846d11e3645SMatteo Riondato 			transp = svc_vc_create(fd, RPC_MAXDATASIZE,
847d11e3645SMatteo Riondato 			    RPC_MAXDATASIZE);
848d11e3645SMatteo Riondato 
849d11e3645SMatteo Riondato 		if (transp != (SVCXPRT *) NULL) {
8500775314bSDoug Rabson 			if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
851d11e3645SMatteo Riondato 			    NULL))
852d11e3645SMatteo Riondato 				syslog(LOG_ERR,
8530775314bSDoug Rabson 				    "can't register %s MOUNTVERS service",
854d11e3645SMatteo Riondato 				    nconf->nc_netid);
855d11e3645SMatteo Riondato 			if (!force_v2) {
8560775314bSDoug Rabson 				if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
857d11e3645SMatteo Riondato 				    mntsrv, NULL))
858d11e3645SMatteo Riondato 					syslog(LOG_ERR,
8590775314bSDoug Rabson 					    "can't register %s MOUNTVERS3 service",
860d11e3645SMatteo Riondato 					    nconf->nc_netid);
861d11e3645SMatteo Riondato 			}
862d11e3645SMatteo Riondato 		} else
863d11e3645SMatteo Riondato 			syslog(LOG_WARNING, "can't create %s services",
864d11e3645SMatteo Riondato 			    nconf->nc_netid);
865d11e3645SMatteo Riondato 
866d11e3645SMatteo Riondato 		if (registered == 0) {
867d11e3645SMatteo Riondato 			registered = 1;
868d11e3645SMatteo Riondato 			memset(&hints, 0, sizeof hints);
869d11e3645SMatteo Riondato 			hints.ai_flags = AI_PASSIVE;
870d11e3645SMatteo Riondato 			hints.ai_family = si.si_af;
871d11e3645SMatteo Riondato 			hints.ai_socktype = si.si_socktype;
872d11e3645SMatteo Riondato 			hints.ai_protocol = si.si_proto;
873d11e3645SMatteo Riondato 
8748fb6ad5dSRick Macklem 			if ((aicode = getaddrinfo(NULL, port_str, &hints,
875d11e3645SMatteo Riondato 			    &res)) != 0) {
876d11e3645SMatteo Riondato 				syslog(LOG_ERR, "cannot get local address: %s",
877d11e3645SMatteo Riondato 				    gai_strerror(aicode));
878d11e3645SMatteo Riondato 				exit(1);
879d11e3645SMatteo Riondato 			}
880d11e3645SMatteo Riondato 
881d11e3645SMatteo Riondato 			servaddr.buf = malloc(res->ai_addrlen);
882d11e3645SMatteo Riondato 			memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
883d11e3645SMatteo Riondato 			servaddr.len = res->ai_addrlen;
884d11e3645SMatteo Riondato 
8850775314bSDoug Rabson 			rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
8860775314bSDoug Rabson 			rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
887d11e3645SMatteo Riondato 
888d11e3645SMatteo Riondato 			xcreated++;
889d11e3645SMatteo Riondato 			freeaddrinfo(res);
890d11e3645SMatteo Riondato 		}
891d11e3645SMatteo Riondato 	} /* end while */
892d11e3645SMatteo Riondato }
893d11e3645SMatteo Riondato 
8948fb6ad5dSRick Macklem /*
8958fb6ad5dSRick Macklem  * Clear out sockets after a failure to bind one of them, so that the
8968fb6ad5dSRick Macklem  * cycle of socket creation/binding can start anew.
8978fb6ad5dSRick Macklem  */
8988fb6ad5dSRick Macklem static void
8998fb6ad5dSRick Macklem clearout_service(void)
9008fb6ad5dSRick Macklem {
9018fb6ad5dSRick Macklem 	int i;
9028fb6ad5dSRick Macklem 
9038fb6ad5dSRick Macklem 	for (i = 0; i < sock_fdcnt; i++) {
9048fb6ad5dSRick Macklem 		if (sock_fd[i] >= 0) {
9058fb6ad5dSRick Macklem 			shutdown(sock_fd[i], SHUT_RDWR);
9068fb6ad5dSRick Macklem 			close(sock_fd[i]);
9078fb6ad5dSRick Macklem 		}
9088fb6ad5dSRick Macklem 	}
9098fb6ad5dSRick Macklem }
9108fb6ad5dSRick Macklem 
91174853402SPhilippe Charnier static void
912a7a7d96cSPhilippe Charnier usage(void)
91374853402SPhilippe Charnier {
91474853402SPhilippe Charnier 	fprintf(stderr,
9152179ae1eSRick Macklem 		"usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
916c548eb5cSRick Macklem 		"[-S] [-h <bindip>] [export_file ...]\n");
9178fae3551SRodney W. Grimes 	exit(1);
9188fae3551SRodney W. Grimes }
9198fae3551SRodney W. Grimes 
9208fae3551SRodney W. Grimes /*
9218fae3551SRodney W. Grimes  * The mount rpc service
9228fae3551SRodney W. Grimes  */
9238fae3551SRodney W. Grimes void
924a7a7d96cSPhilippe Charnier mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
9258fae3551SRodney W. Grimes {
9268fae3551SRodney W. Grimes 	struct exportlist *ep;
9278fae3551SRodney W. Grimes 	struct dirlist *dp;
928a62dc406SDoug Rabson 	struct fhreturn fhr;
9298fae3551SRodney W. Grimes 	struct stat stb;
9308fae3551SRodney W. Grimes 	struct statfs fsb;
9318360efbdSAlfred Perlstein 	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
9328360efbdSAlfred Perlstein 	int lookup_failed = 1;
9338360efbdSAlfred Perlstein 	struct sockaddr *saddr;
934a62dc406SDoug Rabson 	u_short sport;
9350775314bSDoug Rabson 	char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
936e90cdb54SGuido van Rooij 	int bad = 0, defset, hostset;
937a62dc406SDoug Rabson 	sigset_t sighup_mask;
938c3f86a25SRick Macklem 	int numsecflavors, *secflavorsp;
9398fae3551SRodney W. Grimes 
940a62dc406SDoug Rabson 	sigemptyset(&sighup_mask);
941a62dc406SDoug Rabson 	sigaddset(&sighup_mask, SIGHUP);
9428360efbdSAlfred Perlstein 	saddr = svc_getrpccaller(transp)->buf;
9438360efbdSAlfred Perlstein 	switch (saddr->sa_family) {
9448360efbdSAlfred Perlstein 	case AF_INET6:
94501709abfSIan Dowse 		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
9468360efbdSAlfred Perlstein 		break;
9478360efbdSAlfred Perlstein 	case AF_INET:
94801709abfSIan Dowse 		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
9498360efbdSAlfred Perlstein 		break;
9508360efbdSAlfred Perlstein 	default:
9518360efbdSAlfred Perlstein 		syslog(LOG_ERR, "request from unknown address family");
9528360efbdSAlfred Perlstein 		return;
9538360efbdSAlfred Perlstein 	}
9548360efbdSAlfred Perlstein 	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
9558360efbdSAlfred Perlstein 	    NULL, 0, 0);
9568360efbdSAlfred Perlstein 	getnameinfo(saddr, saddr->sa_len, numerichost,
9578360efbdSAlfred Perlstein 	    sizeof numerichost, NULL, 0, NI_NUMERICHOST);
9588fae3551SRodney W. Grimes 	switch (rqstp->rq_proc) {
9598fae3551SRodney W. Grimes 	case NULLPROC:
960389b8446SPeter Wemm 		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
96174853402SPhilippe Charnier 			syslog(LOG_ERR, "can't send reply");
9628fae3551SRodney W. Grimes 		return;
9630775314bSDoug Rabson 	case MOUNTPROC_MNT:
964a62dc406SDoug Rabson 		if (sport >= IPPORT_RESERVED && resvport_only) {
965f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
966f51631d7SGuido van Rooij 			    "mount request from %s from unprivileged port",
9678360efbdSAlfred Perlstein 			    numerichost);
9688fae3551SRodney W. Grimes 			svcerr_weakauth(transp);
9698fae3551SRodney W. Grimes 			return;
9708fae3551SRodney W. Grimes 		}
971389b8446SPeter Wemm 		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
972f51631d7SGuido van Rooij 			syslog(LOG_NOTICE, "undecodable mount request from %s",
9738360efbdSAlfred Perlstein 			    numerichost);
9748fae3551SRodney W. Grimes 			svcerr_decode(transp);
9758fae3551SRodney W. Grimes 			return;
9768fae3551SRodney W. Grimes 		}
9778fae3551SRodney W. Grimes 
9788fae3551SRodney W. Grimes 		/*
9798fae3551SRodney W. Grimes 		 * Get the real pathname and make sure it is a directory
980a62dc406SDoug Rabson 		 * or a regular file if the -r option was specified
981a62dc406SDoug Rabson 		 * and it exists.
9828fae3551SRodney W. Grimes 		 */
983cb479b11SAlfred Perlstein 		if (realpath(rpcpath, dirpath) == NULL ||
9848fae3551SRodney W. Grimes 		    stat(dirpath, &stb) < 0 ||
985a62dc406SDoug Rabson 		    (!S_ISDIR(stb.st_mode) &&
986a62dc406SDoug Rabson 		    (dir_only || !S_ISREG(stb.st_mode))) ||
9878fae3551SRodney W. Grimes 		    statfs(dirpath, &fsb) < 0) {
9888fae3551SRodney W. Grimes 			chdir("/");	/* Just in case realpath doesn't */
989f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
99074853402SPhilippe Charnier 			    "mount request from %s for non existent path %s",
9918360efbdSAlfred Perlstein 			    numerichost, dirpath);
9928fae3551SRodney W. Grimes 			if (debug)
99374853402SPhilippe Charnier 				warnx("stat failed on %s", dirpath);
994e90cdb54SGuido van Rooij 			bad = ENOENT;	/* We will send error reply later */
9958fae3551SRodney W. Grimes 		}
9968fae3551SRodney W. Grimes 
9978fae3551SRodney W. Grimes 		/* Check in the exports list */
998a62dc406SDoug Rabson 		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
9998fae3551SRodney W. Grimes 		ep = ex_search(&fsb.f_fsid);
1000a62dc406SDoug Rabson 		hostset = defset = 0;
1001c3f86a25SRick Macklem 		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1002c3f86a25SRick Macklem 		    &numsecflavors, &secflavorsp) ||
10038fae3551SRodney W. Grimes 		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1004c3f86a25SRick Macklem 		      chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1005c3f86a25SRick Macklem 		       &secflavorsp)) ||
10068fae3551SRodney W. Grimes 		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
10078fae3551SRodney W. Grimes 		     scan_tree(ep->ex_dirl, saddr) == 0))) {
1008e90cdb54SGuido van Rooij 			if (bad) {
1009389b8446SPeter Wemm 				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1010e90cdb54SGuido van Rooij 				    (caddr_t)&bad))
101174853402SPhilippe Charnier 					syslog(LOG_ERR, "can't send reply");
1012e90cdb54SGuido van Rooij 				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1013e90cdb54SGuido van Rooij 				return;
1014e90cdb54SGuido van Rooij 			}
1015c3f86a25SRick Macklem 			if (hostset & DP_HOSTSET) {
1016a62dc406SDoug Rabson 				fhr.fhr_flag = hostset;
1017c3f86a25SRick Macklem 				fhr.fhr_numsecflavors = numsecflavors;
1018c3f86a25SRick Macklem 				fhr.fhr_secflavors = secflavorsp;
1019c3f86a25SRick Macklem 			} else {
1020a62dc406SDoug Rabson 				fhr.fhr_flag = defset;
1021c3f86a25SRick Macklem 				fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1022c3f86a25SRick Macklem 				fhr.fhr_secflavors = ep->ex_defsecflavors;
1023c3f86a25SRick Macklem 			}
1024a62dc406SDoug Rabson 			fhr.fhr_vers = rqstp->rq_vers;
10258fae3551SRodney W. Grimes 			/* Get the file handle */
102687564113SPeter Wemm 			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1027a62dc406SDoug Rabson 			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
10288fae3551SRodney W. Grimes 				bad = errno;
102974853402SPhilippe Charnier 				syslog(LOG_ERR, "can't get fh for %s", dirpath);
1030389b8446SPeter Wemm 				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
10318fae3551SRodney W. Grimes 				    (caddr_t)&bad))
103274853402SPhilippe Charnier 					syslog(LOG_ERR, "can't send reply");
1033a62dc406SDoug Rabson 				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
10348fae3551SRodney W. Grimes 				return;
10358fae3551SRodney W. Grimes 			}
1036389b8446SPeter Wemm 			if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1037389b8446SPeter Wemm 			    (caddr_t)&fhr))
103874853402SPhilippe Charnier 				syslog(LOG_ERR, "can't send reply");
10398360efbdSAlfred Perlstein 			if (!lookup_failed)
10408360efbdSAlfred Perlstein 				add_mlist(host, dirpath);
10418fae3551SRodney W. Grimes 			else
10428360efbdSAlfred Perlstein 				add_mlist(numerichost, dirpath);
10438fae3551SRodney W. Grimes 			if (debug)
104474853402SPhilippe Charnier 				warnx("mount successful");
1045c903443aSPeter Wemm 			if (dolog)
1046f51631d7SGuido van Rooij 				syslog(LOG_NOTICE,
1047f51631d7SGuido van Rooij 				    "mount request succeeded from %s for %s",
10488360efbdSAlfred Perlstein 				    numerichost, dirpath);
1049f51631d7SGuido van Rooij 		} else {
10508fae3551SRodney W. Grimes 			bad = EACCES;
1051f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1052f51631d7SGuido van Rooij 			    "mount request denied from %s for %s",
10538360efbdSAlfred Perlstein 			    numerichost, dirpath);
1054f51631d7SGuido van Rooij 		}
1055e90cdb54SGuido van Rooij 
1056389b8446SPeter Wemm 		if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1057389b8446SPeter Wemm 		    (caddr_t)&bad))
105874853402SPhilippe Charnier 			syslog(LOG_ERR, "can't send reply");
1059a62dc406SDoug Rabson 		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
10608fae3551SRodney W. Grimes 		return;
10610775314bSDoug Rabson 	case MOUNTPROC_DUMP:
1062389b8446SPeter Wemm 		if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
106374853402SPhilippe Charnier 			syslog(LOG_ERR, "can't send reply");
1064c903443aSPeter Wemm 		else if (dolog)
1065f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1066f51631d7SGuido van Rooij 			    "dump request succeeded from %s",
10678360efbdSAlfred Perlstein 			    numerichost);
10688fae3551SRodney W. Grimes 		return;
10690775314bSDoug Rabson 	case MOUNTPROC_UMNT:
1070a62dc406SDoug Rabson 		if (sport >= IPPORT_RESERVED && resvport_only) {
1071f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1072f51631d7SGuido van Rooij 			    "umount request from %s from unprivileged port",
10738360efbdSAlfred Perlstein 			    numerichost);
10748fae3551SRodney W. Grimes 			svcerr_weakauth(transp);
10758fae3551SRodney W. Grimes 			return;
10768fae3551SRodney W. Grimes 		}
1077389b8446SPeter Wemm 		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1078f51631d7SGuido van Rooij 			syslog(LOG_NOTICE, "undecodable umount request from %s",
10798360efbdSAlfred Perlstein 			    numerichost);
10808fae3551SRodney W. Grimes 			svcerr_decode(transp);
10818fae3551SRodney W. Grimes 			return;
10828fae3551SRodney W. Grimes 		}
1083cb479b11SAlfred Perlstein 		if (realpath(rpcpath, dirpath) == NULL) {
1084cb479b11SAlfred Perlstein 			syslog(LOG_NOTICE, "umount request from %s "
1085cb479b11SAlfred Perlstein 			    "for non existent path %s",
10868360efbdSAlfred Perlstein 			    numerichost, dirpath);
1087cb479b11SAlfred Perlstein 		}
1088389b8446SPeter Wemm 		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
108974853402SPhilippe Charnier 			syslog(LOG_ERR, "can't send reply");
10908360efbdSAlfred Perlstein 		if (!lookup_failed)
109101709abfSIan Dowse 			del_mlist(host, dirpath);
109201709abfSIan Dowse 		del_mlist(numerichost, dirpath);
1093c903443aSPeter Wemm 		if (dolog)
1094f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1095f51631d7SGuido van Rooij 			    "umount request succeeded from %s for %s",
10968360efbdSAlfred Perlstein 			    numerichost, dirpath);
10978fae3551SRodney W. Grimes 		return;
10980775314bSDoug Rabson 	case MOUNTPROC_UMNTALL:
1099a62dc406SDoug Rabson 		if (sport >= IPPORT_RESERVED && resvport_only) {
1100f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1101f51631d7SGuido van Rooij 			    "umountall request from %s from unprivileged port",
11028360efbdSAlfred Perlstein 			    numerichost);
11038fae3551SRodney W. Grimes 			svcerr_weakauth(transp);
11048fae3551SRodney W. Grimes 			return;
11058fae3551SRodney W. Grimes 		}
1106389b8446SPeter Wemm 		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
110774853402SPhilippe Charnier 			syslog(LOG_ERR, "can't send reply");
11088360efbdSAlfred Perlstein 		if (!lookup_failed)
110901709abfSIan Dowse 			del_mlist(host, NULL);
111001709abfSIan Dowse 		del_mlist(numerichost, NULL);
1111c903443aSPeter Wemm 		if (dolog)
1112f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1113f51631d7SGuido van Rooij 			    "umountall request succeeded from %s",
11148360efbdSAlfred Perlstein 			    numerichost);
11158fae3551SRodney W. Grimes 		return;
11160775314bSDoug Rabson 	case MOUNTPROC_EXPORT:
1117389b8446SPeter Wemm 		if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1118389b8446SPeter Wemm 			if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1119389b8446SPeter Wemm 			    (caddr_t)NULL))
112074853402SPhilippe Charnier 				syslog(LOG_ERR, "can't send reply");
1121c903443aSPeter Wemm 		if (dolog)
1122f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1123f51631d7SGuido van Rooij 			    "export request succeeded from %s",
11248360efbdSAlfred Perlstein 			    numerichost);
11258fae3551SRodney W. Grimes 		return;
11268fae3551SRodney W. Grimes 	default:
11278fae3551SRodney W. Grimes 		svcerr_noproc(transp);
11288fae3551SRodney W. Grimes 		return;
11298fae3551SRodney W. Grimes 	}
11308fae3551SRodney W. Grimes }
11318fae3551SRodney W. Grimes 
11328fae3551SRodney W. Grimes /*
11338fae3551SRodney W. Grimes  * Xdr conversion for a dirpath string
11348fae3551SRodney W. Grimes  */
113519c46d8cSEdward Tomasz Napierala static int
1136a7a7d96cSPhilippe Charnier xdr_dir(XDR *xdrsp, char *dirp)
11378fae3551SRodney W. Grimes {
11380775314bSDoug Rabson 	return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
11398fae3551SRodney W. Grimes }
11408fae3551SRodney W. Grimes 
11418fae3551SRodney W. Grimes /*
1142a62dc406SDoug Rabson  * Xdr routine to generate file handle reply
11438fae3551SRodney W. Grimes  */
114419c46d8cSEdward Tomasz Napierala static int
1145a7a7d96cSPhilippe Charnier xdr_fhs(XDR *xdrsp, caddr_t cp)
11468fae3551SRodney W. Grimes {
11473d438ad6SDavid E. O'Brien 	struct fhreturn *fhrp = (struct fhreturn *)cp;
1148a62dc406SDoug Rabson 	u_long ok = 0, len, auth;
1149a9148abdSDoug Rabson 	int i;
11508fae3551SRodney W. Grimes 
11518fae3551SRodney W. Grimes 	if (!xdr_long(xdrsp, &ok))
11528fae3551SRodney W. Grimes 		return (0);
1153a62dc406SDoug Rabson 	switch (fhrp->fhr_vers) {
1154a62dc406SDoug Rabson 	case 1:
1155a62dc406SDoug Rabson 		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1156a62dc406SDoug Rabson 	case 3:
1157a62dc406SDoug Rabson 		len = NFSX_V3FH;
1158a62dc406SDoug Rabson 		if (!xdr_long(xdrsp, &len))
1159a62dc406SDoug Rabson 			return (0);
1160a62dc406SDoug Rabson 		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1161a62dc406SDoug Rabson 			return (0);
1162a9148abdSDoug Rabson 		if (fhrp->fhr_numsecflavors) {
1163a9148abdSDoug Rabson 			if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1164a9148abdSDoug Rabson 				return (0);
1165a9148abdSDoug Rabson 			for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1166a9148abdSDoug Rabson 				if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1167a9148abdSDoug Rabson 					return (0);
1168a9148abdSDoug Rabson 			return (1);
1169a9148abdSDoug Rabson 		} else {
1170a9148abdSDoug Rabson 			auth = AUTH_SYS;
1171a62dc406SDoug Rabson 			len = 1;
1172a62dc406SDoug Rabson 			if (!xdr_long(xdrsp, &len))
1173a62dc406SDoug Rabson 				return (0);
1174a62dc406SDoug Rabson 			return (xdr_long(xdrsp, &auth));
1175a9148abdSDoug Rabson 		}
1176a62dc406SDoug Rabson 	};
1177a62dc406SDoug Rabson 	return (0);
11788fae3551SRodney W. Grimes }
11798fae3551SRodney W. Grimes 
118019c46d8cSEdward Tomasz Napierala static int
1181a7a7d96cSPhilippe Charnier xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
11828fae3551SRodney W. Grimes {
11838fae3551SRodney W. Grimes 	struct mountlist *mlp;
11848fae3551SRodney W. Grimes 	int true = 1;
11858fae3551SRodney W. Grimes 	int false = 0;
11868fae3551SRodney W. Grimes 	char *strp;
11878fae3551SRodney W. Grimes 
11888fae3551SRodney W. Grimes 	mlp = mlhead;
11898fae3551SRodney W. Grimes 	while (mlp) {
11908fae3551SRodney W. Grimes 		if (!xdr_bool(xdrsp, &true))
11918fae3551SRodney W. Grimes 			return (0);
11928fae3551SRodney W. Grimes 		strp = &mlp->ml_host[0];
11930775314bSDoug Rabson 		if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
11948fae3551SRodney W. Grimes 			return (0);
11958fae3551SRodney W. Grimes 		strp = &mlp->ml_dirp[0];
11960775314bSDoug Rabson 		if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
11978fae3551SRodney W. Grimes 			return (0);
11988fae3551SRodney W. Grimes 		mlp = mlp->ml_next;
11998fae3551SRodney W. Grimes 	}
12008fae3551SRodney W. Grimes 	if (!xdr_bool(xdrsp, &false))
12018fae3551SRodney W. Grimes 		return (0);
12028fae3551SRodney W. Grimes 	return (1);
12038fae3551SRodney W. Grimes }
12048fae3551SRodney W. Grimes 
12058fae3551SRodney W. Grimes /*
12068fae3551SRodney W. Grimes  * Xdr conversion for export list
12078fae3551SRodney W. Grimes  */
120819c46d8cSEdward Tomasz Napierala static int
1209a7a7d96cSPhilippe Charnier xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
12108fae3551SRodney W. Grimes {
12118fae3551SRodney W. Grimes 	struct exportlist *ep;
12128fae3551SRodney W. Grimes 	int false = 0;
1213a62dc406SDoug Rabson 	int putdef;
1214a62dc406SDoug Rabson 	sigset_t sighup_mask;
12158fae3551SRodney W. Grimes 
1216a62dc406SDoug Rabson 	sigemptyset(&sighup_mask);
1217a62dc406SDoug Rabson 	sigaddset(&sighup_mask, SIGHUP);
1218a62dc406SDoug Rabson 	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
12198fae3551SRodney W. Grimes 	ep = exphead;
12208fae3551SRodney W. Grimes 	while (ep) {
12218fae3551SRodney W. Grimes 		putdef = 0;
122291acb349SAlfred Perlstein 		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
122391acb349SAlfred Perlstein 			       &putdef, brief))
12248fae3551SRodney W. Grimes 			goto errout;
12258fae3551SRodney W. Grimes 		if (ep->ex_defdir && putdef == 0 &&
12268fae3551SRodney W. Grimes 			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
122791acb349SAlfred Perlstein 			&putdef, brief))
12288fae3551SRodney W. Grimes 			goto errout;
12298fae3551SRodney W. Grimes 		ep = ep->ex_next;
12308fae3551SRodney W. Grimes 	}
1231a62dc406SDoug Rabson 	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
12328fae3551SRodney W. Grimes 	if (!xdr_bool(xdrsp, &false))
12338fae3551SRodney W. Grimes 		return (0);
12348fae3551SRodney W. Grimes 	return (1);
12358fae3551SRodney W. Grimes errout:
1236a62dc406SDoug Rabson 	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
12378fae3551SRodney W. Grimes 	return (0);
12388fae3551SRodney W. Grimes }
12398fae3551SRodney W. Grimes 
12408fae3551SRodney W. Grimes /*
12418fae3551SRodney W. Grimes  * Called from xdr_explist() to traverse the tree and export the
12428fae3551SRodney W. Grimes  * directory paths.
12438fae3551SRodney W. Grimes  */
124419c46d8cSEdward Tomasz Napierala static int
1245a7a7d96cSPhilippe Charnier put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1246a7a7d96cSPhilippe Charnier 	int brief)
12478fae3551SRodney W. Grimes {
12488fae3551SRodney W. Grimes 	struct grouplist *grp;
12498fae3551SRodney W. Grimes 	struct hostlist *hp;
12508fae3551SRodney W. Grimes 	int true = 1;
12518fae3551SRodney W. Grimes 	int false = 0;
12528fae3551SRodney W. Grimes 	int gotalldir = 0;
12538fae3551SRodney W. Grimes 	char *strp;
12548fae3551SRodney W. Grimes 
12558fae3551SRodney W. Grimes 	if (dp) {
125691acb349SAlfred Perlstein 		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
12578fae3551SRodney W. Grimes 			return (1);
12588fae3551SRodney W. Grimes 		if (!xdr_bool(xdrsp, &true))
12598fae3551SRodney W. Grimes 			return (1);
12608fae3551SRodney W. Grimes 		strp = dp->dp_dirp;
12610775314bSDoug Rabson 		if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
12628fae3551SRodney W. Grimes 			return (1);
12638fae3551SRodney W. Grimes 		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
12648fae3551SRodney W. Grimes 			gotalldir = 1;
12658fae3551SRodney W. Grimes 			*putdefp = 1;
12668fae3551SRodney W. Grimes 		}
126791acb349SAlfred Perlstein 		if (brief) {
126891acb349SAlfred Perlstein 			if (!xdr_bool(xdrsp, &true))
126991acb349SAlfred Perlstein 				return (1);
127091acb349SAlfred Perlstein 			strp = "(...)";
12710775314bSDoug Rabson 			if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
127291acb349SAlfred Perlstein 				return (1);
127391acb349SAlfred Perlstein 		} else if ((dp->dp_flag & DP_DEFSET) == 0 &&
12748fae3551SRodney W. Grimes 		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
12758fae3551SRodney W. Grimes 			hp = dp->dp_hosts;
12768fae3551SRodney W. Grimes 			while (hp) {
12778fae3551SRodney W. Grimes 				grp = hp->ht_grp;
12788fae3551SRodney W. Grimes 				if (grp->gr_type == GT_HOST) {
12798fae3551SRodney W. Grimes 					if (!xdr_bool(xdrsp, &true))
12808fae3551SRodney W. Grimes 						return (1);
12818360efbdSAlfred Perlstein 					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
12828fae3551SRodney W. Grimes 					if (!xdr_string(xdrsp, &strp,
12830775314bSDoug Rabson 					    MNTNAMLEN))
12848fae3551SRodney W. Grimes 						return (1);
12858fae3551SRodney W. Grimes 				} else if (grp->gr_type == GT_NET) {
12868fae3551SRodney W. Grimes 					if (!xdr_bool(xdrsp, &true))
12878fae3551SRodney W. Grimes 						return (1);
12888fae3551SRodney W. Grimes 					strp = grp->gr_ptr.gt_net.nt_name;
12898fae3551SRodney W. Grimes 					if (!xdr_string(xdrsp, &strp,
12900775314bSDoug Rabson 					    MNTNAMLEN))
12918fae3551SRodney W. Grimes 						return (1);
12928fae3551SRodney W. Grimes 				}
12938fae3551SRodney W. Grimes 				hp = hp->ht_next;
12948fae3551SRodney W. Grimes 				if (gotalldir && hp == (struct hostlist *)NULL) {
12958fae3551SRodney W. Grimes 					hp = adp->dp_hosts;
12968fae3551SRodney W. Grimes 					gotalldir = 0;
12978fae3551SRodney W. Grimes 				}
12988fae3551SRodney W. Grimes 			}
12998fae3551SRodney W. Grimes 		}
13008fae3551SRodney W. Grimes 		if (!xdr_bool(xdrsp, &false))
13018fae3551SRodney W. Grimes 			return (1);
130291acb349SAlfred Perlstein 		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
13038fae3551SRodney W. Grimes 			return (1);
13048fae3551SRodney W. Grimes 	}
13058fae3551SRodney W. Grimes 	return (0);
13068fae3551SRodney W. Grimes }
13078fae3551SRodney W. Grimes 
130819c46d8cSEdward Tomasz Napierala static int
1309a7a7d96cSPhilippe Charnier xdr_explist(XDR *xdrsp, caddr_t cp)
131091acb349SAlfred Perlstein {
131191acb349SAlfred Perlstein 
131291acb349SAlfred Perlstein 	return xdr_explist_common(xdrsp, cp, 0);
131391acb349SAlfred Perlstein }
131491acb349SAlfred Perlstein 
131519c46d8cSEdward Tomasz Napierala static int
1316a7a7d96cSPhilippe Charnier xdr_explist_brief(XDR *xdrsp, caddr_t cp)
131791acb349SAlfred Perlstein {
131891acb349SAlfred Perlstein 
131991acb349SAlfred Perlstein 	return xdr_explist_common(xdrsp, cp, 1);
132091acb349SAlfred Perlstein }
132191acb349SAlfred Perlstein 
132219c46d8cSEdward Tomasz Napierala static char *line;
132319c46d8cSEdward Tomasz Napierala static size_t linesize;
132419c46d8cSEdward Tomasz Napierala static FILE *exp_file;
13258fae3551SRodney W. Grimes 
13268fae3551SRodney W. Grimes /*
132796968c22SPawel Jakub Dawidek  * Get the export list from one, currently open file
13288fae3551SRodney W. Grimes  */
132996968c22SPawel Jakub Dawidek static void
1330a7a7d96cSPhilippe Charnier get_exportlist_one(void)
13318fae3551SRodney W. Grimes {
13328fae3551SRodney W. Grimes 	struct exportlist *ep, *ep2;
13338fae3551SRodney W. Grimes 	struct grouplist *grp, *tgrp;
13348fae3551SRodney W. Grimes 	struct exportlist **epp;
13358fae3551SRodney W. Grimes 	struct dirlist *dirhead;
133696968c22SPawel Jakub Dawidek 	struct statfs fsb;
1337c0511d3bSBrian Feldman 	struct xucred anon;
13388fae3551SRodney W. Grimes 	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
133996968c22SPawel Jakub Dawidek 	int len, has_host, exflags, got_nondir, dirplen, netgrp;
13408fae3551SRodney W. Grimes 
1341bcc1d071SRick Macklem 	v4root_phase = 0;
13428fae3551SRodney W. Grimes 	dirhead = (struct dirlist *)NULL;
13438fae3551SRodney W. Grimes 	while (get_line()) {
13448fae3551SRodney W. Grimes 		if (debug)
134574853402SPhilippe Charnier 			warnx("got line %s", line);
13468fae3551SRodney W. Grimes 		cp = line;
13478fae3551SRodney W. Grimes 		nextfield(&cp, &endcp);
13488fae3551SRodney W. Grimes 		if (*cp == '#')
13498fae3551SRodney W. Grimes 			goto nextline;
13508fae3551SRodney W. Grimes 
13518fae3551SRodney W. Grimes 		/*
13528fae3551SRodney W. Grimes 		 * Set defaults.
13538fae3551SRodney W. Grimes 		 */
13548fae3551SRodney W. Grimes 		has_host = FALSE;
13558fae3551SRodney W. Grimes 		anon = def_anon;
13568fae3551SRodney W. Grimes 		exflags = MNT_EXPORTED;
13578fae3551SRodney W. Grimes 		got_nondir = 0;
13588fae3551SRodney W. Grimes 		opt_flags = 0;
13598fae3551SRodney W. Grimes 		ep = (struct exportlist *)NULL;
1360bcc1d071SRick Macklem 		dirp = NULL;
1361bcc1d071SRick Macklem 
1362bcc1d071SRick Macklem 		/*
1363bcc1d071SRick Macklem 		 * Handle the V4 root dir.
1364bcc1d071SRick Macklem 		 */
1365bcc1d071SRick Macklem 		if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1366bcc1d071SRick Macklem 			/*
1367bcc1d071SRick Macklem 			 * V4: just indicates that it is the v4 root point,
1368bcc1d071SRick Macklem 			 * so skip over that and set v4root_phase.
1369bcc1d071SRick Macklem 			 */
1370bcc1d071SRick Macklem 			if (v4root_phase > 0) {
1371bcc1d071SRick Macklem 				syslog(LOG_ERR, "V4:duplicate line, ignored");
1372bcc1d071SRick Macklem 				goto nextline;
1373bcc1d071SRick Macklem 			}
1374bcc1d071SRick Macklem 			v4root_phase = 1;
1375bcc1d071SRick Macklem 			cp += 3;
1376bcc1d071SRick Macklem 			nextfield(&cp, &endcp);
1377bcc1d071SRick Macklem 		}
13788fae3551SRodney W. Grimes 
13798fae3551SRodney W. Grimes 		/*
13808fae3551SRodney W. Grimes 		 * Create new exports list entry
13818fae3551SRodney W. Grimes 		 */
13828fae3551SRodney W. Grimes 		len = endcp-cp;
13838fae3551SRodney W. Grimes 		tgrp = grp = get_grp();
13848fae3551SRodney W. Grimes 		while (len > 0) {
13850775314bSDoug Rabson 			if (len > MNTNAMLEN) {
13868fae3551SRodney W. Grimes 			    getexp_err(ep, tgrp);
13878fae3551SRodney W. Grimes 			    goto nextline;
13888fae3551SRodney W. Grimes 			}
13898fae3551SRodney W. Grimes 			if (*cp == '-') {
13908fae3551SRodney W. Grimes 			    if (ep == (struct exportlist *)NULL) {
13918fae3551SRodney W. Grimes 				getexp_err(ep, tgrp);
13928fae3551SRodney W. Grimes 				goto nextline;
13938fae3551SRodney W. Grimes 			    }
13948fae3551SRodney W. Grimes 			    if (debug)
139574853402SPhilippe Charnier 				warnx("doing opt %s", cp);
13968fae3551SRodney W. Grimes 			    got_nondir = 1;
13978fae3551SRodney W. Grimes 			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
13988fae3551SRodney W. Grimes 				&exflags, &anon)) {
13998fae3551SRodney W. Grimes 				getexp_err(ep, tgrp);
14008fae3551SRodney W. Grimes 				goto nextline;
14018fae3551SRodney W. Grimes 			    }
14028fae3551SRodney W. Grimes 			} else if (*cp == '/') {
14038fae3551SRodney W. Grimes 			    savedc = *endcp;
14048fae3551SRodney W. Grimes 			    *endcp = '\0';
1405bcc1d071SRick Macklem 			    if (v4root_phase > 1) {
1406bcc1d071SRick Macklem 				    if (dirp != NULL) {
1407bcc1d071SRick Macklem 					syslog(LOG_ERR, "Multiple V4 dirs");
1408bcc1d071SRick Macklem 					getexp_err(ep, tgrp);
1409bcc1d071SRick Macklem 					goto nextline;
1410bcc1d071SRick Macklem 				    }
1411bcc1d071SRick Macklem 			    }
14128fae3551SRodney W. Grimes 			    if (check_dirpath(cp) &&
14138fae3551SRodney W. Grimes 				statfs(cp, &fsb) >= 0) {
14149896584aSRick Macklem 				if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
14159896584aSRick Macklem 				    syslog(LOG_ERR, "Warning: exporting of "
14169896584aSRick Macklem 					"automounted fs %s not supported", cp);
14178fae3551SRodney W. Grimes 				if (got_nondir) {
141874853402SPhilippe Charnier 				    syslog(LOG_ERR, "dirs must be first");
14198fae3551SRodney W. Grimes 				    getexp_err(ep, tgrp);
14208fae3551SRodney W. Grimes 				    goto nextline;
14218fae3551SRodney W. Grimes 				}
1422bcc1d071SRick Macklem 				if (v4root_phase == 1) {
1423bcc1d071SRick Macklem 				    if (dirp != NULL) {
1424bcc1d071SRick Macklem 					syslog(LOG_ERR, "Multiple V4 dirs");
1425bcc1d071SRick Macklem 					getexp_err(ep, tgrp);
1426bcc1d071SRick Macklem 					goto nextline;
1427bcc1d071SRick Macklem 				    }
1428bcc1d071SRick Macklem 				    if (strlen(v4root_dirpath) == 0) {
1429bcc1d071SRick Macklem 					strlcpy(v4root_dirpath, cp,
1430bcc1d071SRick Macklem 					    sizeof (v4root_dirpath));
1431bcc1d071SRick Macklem 				    } else if (strcmp(v4root_dirpath, cp)
1432bcc1d071SRick Macklem 					!= 0) {
1433bcc1d071SRick Macklem 					syslog(LOG_ERR,
1434bcc1d071SRick Macklem 					    "different V4 dirpath %s", cp);
1435bcc1d071SRick Macklem 					getexp_err(ep, tgrp);
1436bcc1d071SRick Macklem 					goto nextline;
1437bcc1d071SRick Macklem 				    }
1438bcc1d071SRick Macklem 				    dirp = cp;
1439bcc1d071SRick Macklem 				    v4root_phase = 2;
1440bcc1d071SRick Macklem 				    got_nondir = 1;
1441bcc1d071SRick Macklem 				    ep = get_exp();
1442bcc1d071SRick Macklem 				} else {
14438fae3551SRodney W. Grimes 				    if (ep) {
1444bcc1d071SRick Macklem 					if (ep->ex_fs.val[0] !=
1445bcc1d071SRick Macklem 					    fsb.f_fsid.val[0] ||
1446bcc1d071SRick Macklem 					    ep->ex_fs.val[1] !=
1447bcc1d071SRick Macklem 					    fsb.f_fsid.val[1]) {
14488fae3551SRodney W. Grimes 						getexp_err(ep, tgrp);
14498fae3551SRodney W. Grimes 						goto nextline;
14508fae3551SRodney W. Grimes 					}
14518fae3551SRodney W. Grimes 				    } else {
14528fae3551SRodney W. Grimes 					/*
14538fae3551SRodney W. Grimes 					 * See if this directory is already
14548fae3551SRodney W. Grimes 					 * in the list.
14558fae3551SRodney W. Grimes 					 */
14568fae3551SRodney W. Grimes 					ep = ex_search(&fsb.f_fsid);
14578fae3551SRodney W. Grimes 					if (ep == (struct exportlist *)NULL) {
14588fae3551SRodney W. Grimes 					    ep = get_exp();
14598fae3551SRodney W. Grimes 					    ep->ex_fs = fsb.f_fsid;
1460bcc1d071SRick Macklem 					    ep->ex_fsdir = (char *)malloc
1461bcc1d071SRick Macklem 					        (strlen(fsb.f_mntonname) + 1);
14628fae3551SRodney W. Grimes 					    if (ep->ex_fsdir)
14638fae3551SRodney W. Grimes 						strcpy(ep->ex_fsdir,
14648fae3551SRodney W. Grimes 						    fsb.f_mntonname);
14658fae3551SRodney W. Grimes 					    else
14668fae3551SRodney W. Grimes 						out_of_mem();
14678fae3551SRodney W. Grimes 					    if (debug)
1468bcc1d071SRick Macklem 						warnx(
1469bcc1d071SRick Macklem 						  "making new ep fs=0x%x,0x%x",
14708fae3551SRodney W. Grimes 						  fsb.f_fsid.val[0],
14718fae3551SRodney W. Grimes 						  fsb.f_fsid.val[1]);
14728fae3551SRodney W. Grimes 					} else if (debug)
147374853402SPhilippe Charnier 					    warnx("found ep fs=0x%x,0x%x",
14748fae3551SRodney W. Grimes 						fsb.f_fsid.val[0],
14758fae3551SRodney W. Grimes 						fsb.f_fsid.val[1]);
14768fae3551SRodney W. Grimes 				    }
14778fae3551SRodney W. Grimes 
14788fae3551SRodney W. Grimes 				    /*
14798fae3551SRodney W. Grimes 				     * Add dirpath to export mount point.
14808fae3551SRodney W. Grimes 				     */
14818fae3551SRodney W. Grimes 				    dirp = add_expdir(&dirhead, cp, len);
14828fae3551SRodney W. Grimes 				    dirplen = len;
1483bcc1d071SRick Macklem 				}
14848fae3551SRodney W. Grimes 			    } else {
14858fae3551SRodney W. Grimes 				getexp_err(ep, tgrp);
14868fae3551SRodney W. Grimes 				goto nextline;
14878fae3551SRodney W. Grimes 			    }
14888fae3551SRodney W. Grimes 			    *endcp = savedc;
14898fae3551SRodney W. Grimes 			} else {
14908fae3551SRodney W. Grimes 			    savedc = *endcp;
14918fae3551SRodney W. Grimes 			    *endcp = '\0';
14928fae3551SRodney W. Grimes 			    got_nondir = 1;
14938fae3551SRodney W. Grimes 			    if (ep == (struct exportlist *)NULL) {
14948fae3551SRodney W. Grimes 				getexp_err(ep, tgrp);
14958fae3551SRodney W. Grimes 				goto nextline;
14968fae3551SRodney W. Grimes 			    }
14978fae3551SRodney W. Grimes 
14988fae3551SRodney W. Grimes 			    /*
14998fae3551SRodney W. Grimes 			     * Get the host or netgroup.
15008fae3551SRodney W. Grimes 			     */
15018fae3551SRodney W. Grimes 			    setnetgrent(cp);
15028fae3551SRodney W. Grimes 			    netgrp = getnetgrent(&hst, &usr, &dom);
15038fae3551SRodney W. Grimes 			    do {
15048fae3551SRodney W. Grimes 				if (has_host) {
15058fae3551SRodney W. Grimes 				    grp->gr_next = get_grp();
15068fae3551SRodney W. Grimes 				    grp = grp->gr_next;
15078fae3551SRodney W. Grimes 				}
15088fae3551SRodney W. Grimes 				if (netgrp) {
15099d70a156SJoerg Wunsch 				    if (hst == 0) {
151074853402SPhilippe Charnier 					syslog(LOG_ERR,
151174853402SPhilippe Charnier 				"null hostname in netgroup %s, skipping", cp);
151201d48801SJoerg Wunsch 					grp->gr_type = GT_IGNORE;
15139d70a156SJoerg Wunsch 				    } else if (get_host(hst, grp, tgrp)) {
151474853402SPhilippe Charnier 					syslog(LOG_ERR,
151574853402SPhilippe Charnier 			"bad host %s in netgroup %s, skipping", hst, cp);
1516a968cfd8SJonathan Lemon 					grp->gr_type = GT_IGNORE;
15178fae3551SRodney W. Grimes 				    }
15188b5a6d67SBill Paul 				} else if (get_host(cp, grp, tgrp)) {
151974853402SPhilippe Charnier 				    syslog(LOG_ERR, "bad host %s, skipping", cp);
1520a968cfd8SJonathan Lemon 				    grp->gr_type = GT_IGNORE;
15218fae3551SRodney W. Grimes 				}
15228fae3551SRodney W. Grimes 				has_host = TRUE;
15238fae3551SRodney W. Grimes 			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
15248fae3551SRodney W. Grimes 			    endnetgrent();
15258fae3551SRodney W. Grimes 			    *endcp = savedc;
15268fae3551SRodney W. Grimes 			}
15278fae3551SRodney W. Grimes 			cp = endcp;
15288fae3551SRodney W. Grimes 			nextfield(&cp, &endcp);
15298fae3551SRodney W. Grimes 			len = endcp - cp;
15308fae3551SRodney W. Grimes 		}
15318fae3551SRodney W. Grimes 		if (check_options(dirhead)) {
15328fae3551SRodney W. Grimes 			getexp_err(ep, tgrp);
15338fae3551SRodney W. Grimes 			goto nextline;
15348fae3551SRodney W. Grimes 		}
15358fae3551SRodney W. Grimes 		if (!has_host) {
15366d359f31SIan Dowse 			grp->gr_type = GT_DEFAULT;
15378fae3551SRodney W. Grimes 			if (debug)
153874853402SPhilippe Charnier 				warnx("adding a default entry");
15398fae3551SRodney W. Grimes 
15408fae3551SRodney W. Grimes 		/*
15418fae3551SRodney W. Grimes 		 * Don't allow a network export coincide with a list of
15428fae3551SRodney W. Grimes 		 * host(s) on the same line.
15438fae3551SRodney W. Grimes 		 */
15448fae3551SRodney W. Grimes 		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
154560caaee2SIan Dowse 			syslog(LOG_ERR, "network/host conflict");
15468fae3551SRodney W. Grimes 			getexp_err(ep, tgrp);
15478fae3551SRodney W. Grimes 			goto nextline;
1548a968cfd8SJonathan Lemon 
1549a968cfd8SJonathan Lemon 		/*
1550a968cfd8SJonathan Lemon 		 * If an export list was specified on this line, make sure
1551a968cfd8SJonathan Lemon 		 * that we have at least one valid entry, otherwise skip it.
1552a968cfd8SJonathan Lemon 		 */
1553a968cfd8SJonathan Lemon 		} else {
1554a968cfd8SJonathan Lemon 			grp = tgrp;
1555a968cfd8SJonathan Lemon 			while (grp && grp->gr_type == GT_IGNORE)
1556a968cfd8SJonathan Lemon 				grp = grp->gr_next;
1557a968cfd8SJonathan Lemon 			if (! grp) {
1558a968cfd8SJonathan Lemon 			    getexp_err(ep, tgrp);
1559a968cfd8SJonathan Lemon 			    goto nextline;
1560a968cfd8SJonathan Lemon 			}
15618fae3551SRodney W. Grimes 		}
15628fae3551SRodney W. Grimes 
1563bcc1d071SRick Macklem 		if (v4root_phase == 1) {
1564bcc1d071SRick Macklem 			syslog(LOG_ERR, "V4:root, no dirp, ignored");
1565bcc1d071SRick Macklem 			getexp_err(ep, tgrp);
1566bcc1d071SRick Macklem 			goto nextline;
1567bcc1d071SRick Macklem 		}
1568bcc1d071SRick Macklem 
15698fae3551SRodney W. Grimes 		/*
15708fae3551SRodney W. Grimes 		 * Loop through hosts, pushing the exports into the kernel.
15718fae3551SRodney W. Grimes 		 * After loop, tgrp points to the start of the list and
15728fae3551SRodney W. Grimes 		 * grp points to the last entry in the list.
15738fae3551SRodney W. Grimes 		 */
15748fae3551SRodney W. Grimes 		grp = tgrp;
15758fae3551SRodney W. Grimes 		do {
157601709abfSIan Dowse 			if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
157701709abfSIan Dowse 			    &fsb)) {
15788fae3551SRodney W. Grimes 				getexp_err(ep, tgrp);
15798fae3551SRodney W. Grimes 				goto nextline;
15808fae3551SRodney W. Grimes 			}
15818fae3551SRodney W. Grimes 		} while (grp->gr_next && (grp = grp->gr_next));
15828fae3551SRodney W. Grimes 
15838fae3551SRodney W. Grimes 		/*
1584bcc1d071SRick Macklem 		 * For V4: don't enter in mount lists.
1585bcc1d071SRick Macklem 		 */
158673f4ccbdSRick Macklem 		if (v4root_phase > 0 && v4root_phase <= 2) {
158773f4ccbdSRick Macklem 			/*
158873f4ccbdSRick Macklem 			 * Since these structures aren't used by mountd,
158973f4ccbdSRick Macklem 			 * free them up now.
159073f4ccbdSRick Macklem 			 */
159173f4ccbdSRick Macklem 			if (ep != NULL)
159273f4ccbdSRick Macklem 				free_exp(ep);
159373f4ccbdSRick Macklem 			while (tgrp != NULL) {
159473f4ccbdSRick Macklem 				grp = tgrp;
159573f4ccbdSRick Macklem 				tgrp = tgrp->gr_next;
159673f4ccbdSRick Macklem 				free_grp(grp);
159773f4ccbdSRick Macklem 			}
1598bcc1d071SRick Macklem 			goto nextline;
159973f4ccbdSRick Macklem 		}
1600bcc1d071SRick Macklem 
1601bcc1d071SRick Macklem 		/*
16028fae3551SRodney W. Grimes 		 * Success. Update the data structures.
16038fae3551SRodney W. Grimes 		 */
16048fae3551SRodney W. Grimes 		if (has_host) {
1605a62dc406SDoug Rabson 			hang_dirp(dirhead, tgrp, ep, opt_flags);
16068fae3551SRodney W. Grimes 			grp->gr_next = grphead;
16078fae3551SRodney W. Grimes 			grphead = tgrp;
16088fae3551SRodney W. Grimes 		} else {
16098fae3551SRodney W. Grimes 			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1610a62dc406SDoug Rabson 				opt_flags);
16118fae3551SRodney W. Grimes 			free_grp(grp);
16128fae3551SRodney W. Grimes 		}
16138fae3551SRodney W. Grimes 		dirhead = (struct dirlist *)NULL;
16148fae3551SRodney W. Grimes 		if ((ep->ex_flag & EX_LINKED) == 0) {
16158fae3551SRodney W. Grimes 			ep2 = exphead;
16168fae3551SRodney W. Grimes 			epp = &exphead;
16178fae3551SRodney W. Grimes 
16188fae3551SRodney W. Grimes 			/*
16198fae3551SRodney W. Grimes 			 * Insert in the list in alphabetical order.
16208fae3551SRodney W. Grimes 			 */
16218fae3551SRodney W. Grimes 			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
16228fae3551SRodney W. Grimes 				epp = &ep2->ex_next;
16238fae3551SRodney W. Grimes 				ep2 = ep2->ex_next;
16248fae3551SRodney W. Grimes 			}
16258fae3551SRodney W. Grimes 			if (ep2)
16268fae3551SRodney W. Grimes 				ep->ex_next = ep2;
16278fae3551SRodney W. Grimes 			*epp = ep;
16288fae3551SRodney W. Grimes 			ep->ex_flag |= EX_LINKED;
16298fae3551SRodney W. Grimes 		}
16308fae3551SRodney W. Grimes nextline:
1631bcc1d071SRick Macklem 		v4root_phase = 0;
16328fae3551SRodney W. Grimes 		if (dirhead) {
16338fae3551SRodney W. Grimes 			free_dir(dirhead);
16348fae3551SRodney W. Grimes 			dirhead = (struct dirlist *)NULL;
16358fae3551SRodney W. Grimes 		}
16368fae3551SRodney W. Grimes 	}
163796968c22SPawel Jakub Dawidek }
163896968c22SPawel Jakub Dawidek 
163996968c22SPawel Jakub Dawidek /*
164096968c22SPawel Jakub Dawidek  * Get the export list from all specified files
164196968c22SPawel Jakub Dawidek  */
164219c46d8cSEdward Tomasz Napierala static void
1643a7a7d96cSPhilippe Charnier get_exportlist(void)
164496968c22SPawel Jakub Dawidek {
164596968c22SPawel Jakub Dawidek 	struct exportlist *ep, *ep2;
164696968c22SPawel Jakub Dawidek 	struct grouplist *grp, *tgrp;
164796968c22SPawel Jakub Dawidek 	struct export_args export;
164896968c22SPawel Jakub Dawidek 	struct iovec *iov;
164996968c22SPawel Jakub Dawidek 	struct statfs *fsp, *mntbufp;
165096968c22SPawel Jakub Dawidek 	struct xvfsconf vfc;
165196968c22SPawel Jakub Dawidek 	char errmsg[255];
1652e0bcf086SEitan Adler 	int num, i;
165396968c22SPawel Jakub Dawidek 	int iovlen;
16546c90092bSPawel Jakub Dawidek 	int done;
1655bcc1d071SRick Macklem 	struct nfsex_args eargs;
165696968c22SPawel Jakub Dawidek 
1657c548eb5cSRick Macklem 	if (suspend_nfsd != 0)
1658c548eb5cSRick Macklem 		(void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1659bcc1d071SRick Macklem 	v4root_dirpath[0] = '\0';
166096968c22SPawel Jakub Dawidek 	bzero(&export, sizeof(export));
166196968c22SPawel Jakub Dawidek 	export.ex_flags = MNT_DELEXPORT;
166296968c22SPawel Jakub Dawidek 	iov = NULL;
166396968c22SPawel Jakub Dawidek 	iovlen = 0;
166496968c22SPawel Jakub Dawidek 	bzero(errmsg, sizeof(errmsg));
166596968c22SPawel Jakub Dawidek 
166696968c22SPawel Jakub Dawidek 	/*
166796968c22SPawel Jakub Dawidek 	 * First, get rid of the old list
166896968c22SPawel Jakub Dawidek 	 */
166996968c22SPawel Jakub Dawidek 	ep = exphead;
167096968c22SPawel Jakub Dawidek 	while (ep) {
167196968c22SPawel Jakub Dawidek 		ep2 = ep;
167296968c22SPawel Jakub Dawidek 		ep = ep->ex_next;
167396968c22SPawel Jakub Dawidek 		free_exp(ep2);
167496968c22SPawel Jakub Dawidek 	}
167596968c22SPawel Jakub Dawidek 	exphead = (struct exportlist *)NULL;
167696968c22SPawel Jakub Dawidek 
167796968c22SPawel Jakub Dawidek 	grp = grphead;
167896968c22SPawel Jakub Dawidek 	while (grp) {
167996968c22SPawel Jakub Dawidek 		tgrp = grp;
168096968c22SPawel Jakub Dawidek 		grp = grp->gr_next;
168196968c22SPawel Jakub Dawidek 		free_grp(tgrp);
168296968c22SPawel Jakub Dawidek 	}
168396968c22SPawel Jakub Dawidek 	grphead = (struct grouplist *)NULL;
168496968c22SPawel Jakub Dawidek 
168596968c22SPawel Jakub Dawidek 	/*
1686bcc1d071SRick Macklem 	 * and the old V4 root dir.
1687bcc1d071SRick Macklem 	 */
1688bcc1d071SRick Macklem 	bzero(&eargs, sizeof (eargs));
1689bcc1d071SRick Macklem 	eargs.export.ex_flags = MNT_DELEXPORT;
169079b86807SEdward Tomasz Napierala 	if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1691bcc1d071SRick Macklem 	    errno != ENOENT)
1692bcc1d071SRick Macklem 		syslog(LOG_ERR, "Can't delete exports for V4:");
1693bcc1d071SRick Macklem 
1694bcc1d071SRick Macklem 	/*
1695bcc1d071SRick Macklem 	 * and clear flag that notes if a public fh has been exported.
1696bcc1d071SRick Macklem 	 */
1697bcc1d071SRick Macklem 	has_publicfh = 0;
1698bcc1d071SRick Macklem 
1699bcc1d071SRick Macklem 	/*
170096968c22SPawel Jakub Dawidek 	 * And delete exports that are in the kernel for all local
170196968c22SPawel Jakub Dawidek 	 * filesystems.
170296968c22SPawel Jakub Dawidek 	 * XXX: Should know how to handle all local exportable filesystems.
170396968c22SPawel Jakub Dawidek 	 */
170496968c22SPawel Jakub Dawidek 	num = getmntinfo(&mntbufp, MNT_NOWAIT);
170596968c22SPawel Jakub Dawidek 
170696968c22SPawel Jakub Dawidek 	if (num > 0) {
170796968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "fstype", NULL, 0);
170896968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "fspath", NULL, 0);
170996968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "from", NULL, 0);
171096968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "update", NULL, 0);
171196968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
171296968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
171396968c22SPawel Jakub Dawidek 	}
171496968c22SPawel Jakub Dawidek 
171596968c22SPawel Jakub Dawidek 	for (i = 0; i < num; i++) {
171696968c22SPawel Jakub Dawidek 		fsp = &mntbufp[i];
171796968c22SPawel Jakub Dawidek 		if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
171896968c22SPawel Jakub Dawidek 			syslog(LOG_ERR, "getvfsbyname() failed for %s",
171996968c22SPawel Jakub Dawidek 			    fsp->f_fstypename);
172096968c22SPawel Jakub Dawidek 			continue;
172196968c22SPawel Jakub Dawidek 		}
172296968c22SPawel Jakub Dawidek 
172396968c22SPawel Jakub Dawidek 		/*
17242ef26470SSimon J. Gerraty 		 * We do not need to delete "export" flag from
17252ef26470SSimon J. Gerraty 		 * filesystems that do not have it set.
17262ef26470SSimon J. Gerraty 		 */
17272ef26470SSimon J. Gerraty 		if (!(fsp->f_flags & MNT_EXPORTED))
17282ef26470SSimon J. Gerraty 		    continue;
17292ef26470SSimon J. Gerraty 		/*
173096968c22SPawel Jakub Dawidek 		 * Do not delete export for network filesystem by
173196968c22SPawel Jakub Dawidek 		 * passing "export" arg to nmount().
173296968c22SPawel Jakub Dawidek 		 * It only makes sense to do this for local filesystems.
173396968c22SPawel Jakub Dawidek 		 */
173496968c22SPawel Jakub Dawidek 		if (vfc.vfc_flags & VFCF_NETWORK)
173596968c22SPawel Jakub Dawidek 			continue;
173696968c22SPawel Jakub Dawidek 
173796968c22SPawel Jakub Dawidek 		iov[1].iov_base = fsp->f_fstypename;
173896968c22SPawel Jakub Dawidek 		iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
173996968c22SPawel Jakub Dawidek 		iov[3].iov_base = fsp->f_mntonname;
174096968c22SPawel Jakub Dawidek 		iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
174196968c22SPawel Jakub Dawidek 		iov[5].iov_base = fsp->f_mntfromname;
174296968c22SPawel Jakub Dawidek 		iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
17434a185fa6SBryan Drewery 		errmsg[0] = '\0';
174496968c22SPawel Jakub Dawidek 
17455d6f5b24SKonstantin Belousov 		/*
17465d6f5b24SKonstantin Belousov 		 * EXDEV is returned when path exists but is not a
17475d6f5b24SKonstantin Belousov 		 * mount point.  May happens if raced with unmount.
17485d6f5b24SKonstantin Belousov 		 */
174996968c22SPawel Jakub Dawidek 		if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
17505d6f5b24SKonstantin Belousov 		    errno != ENOENT && errno != ENOTSUP && errno != EXDEV) {
175196968c22SPawel Jakub Dawidek 			syslog(LOG_ERR,
175296968c22SPawel Jakub Dawidek 			    "can't delete exports for %s: %m %s",
175396968c22SPawel Jakub Dawidek 			    fsp->f_mntonname, errmsg);
175496968c22SPawel Jakub Dawidek 		}
175596968c22SPawel Jakub Dawidek 	}
175696968c22SPawel Jakub Dawidek 
175796968c22SPawel Jakub Dawidek 	if (iov != NULL) {
175896968c22SPawel Jakub Dawidek 		/* Free strings allocated by strdup() in getmntopts.c */
175996968c22SPawel Jakub Dawidek 		free(iov[0].iov_base); /* fstype */
176096968c22SPawel Jakub Dawidek 		free(iov[2].iov_base); /* fspath */
176196968c22SPawel Jakub Dawidek 		free(iov[4].iov_base); /* from */
176296968c22SPawel Jakub Dawidek 		free(iov[6].iov_base); /* update */
176396968c22SPawel Jakub Dawidek 		free(iov[8].iov_base); /* export */
176496968c22SPawel Jakub Dawidek 		free(iov[10].iov_base); /* errmsg */
176596968c22SPawel Jakub Dawidek 
176696968c22SPawel Jakub Dawidek 		/* free iov, allocated by realloc() */
176796968c22SPawel Jakub Dawidek 		free(iov);
176896968c22SPawel Jakub Dawidek 		iovlen = 0;
176996968c22SPawel Jakub Dawidek 	}
177096968c22SPawel Jakub Dawidek 
177196968c22SPawel Jakub Dawidek 	/*
177296968c22SPawel Jakub Dawidek 	 * Read in the exports file and build the list, calling
177396968c22SPawel Jakub Dawidek 	 * nmount() as we go along to push the export rules into the kernel.
177496968c22SPawel Jakub Dawidek 	 */
17756c90092bSPawel Jakub Dawidek 	done = 0;
177696968c22SPawel Jakub Dawidek 	for (i = 0; exnames[i] != NULL; i++) {
177796968c22SPawel Jakub Dawidek 		if (debug)
177896968c22SPawel Jakub Dawidek 			warnx("reading exports from %s", exnames[i]);
177996968c22SPawel Jakub Dawidek 		if ((exp_file = fopen(exnames[i], "r")) == NULL) {
17806c90092bSPawel Jakub Dawidek 			syslog(LOG_WARNING, "can't open %s", exnames[i]);
17816c90092bSPawel Jakub Dawidek 			continue;
178296968c22SPawel Jakub Dawidek 		}
178396968c22SPawel Jakub Dawidek 		get_exportlist_one();
17848fae3551SRodney W. Grimes 		fclose(exp_file);
17856c90092bSPawel Jakub Dawidek 		done++;
17866c90092bSPawel Jakub Dawidek 	}
17876c90092bSPawel Jakub Dawidek 	if (done == 0) {
17886c90092bSPawel Jakub Dawidek 		syslog(LOG_ERR, "can't open any exports file");
17896c90092bSPawel Jakub Dawidek 		exit(2);
17908fae3551SRodney W. Grimes 	}
1791bcc1d071SRick Macklem 
1792bcc1d071SRick Macklem 	/*
1793bcc1d071SRick Macklem 	 * If there was no public fh, clear any previous one set.
1794bcc1d071SRick Macklem 	 */
179579b86807SEdward Tomasz Napierala 	if (has_publicfh == 0)
1796bcc1d071SRick Macklem 		(void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1797c548eb5cSRick Macklem 
1798c548eb5cSRick Macklem 	/* Resume the nfsd. If they weren't suspended, this is harmless. */
1799c548eb5cSRick Macklem 	(void)nfssvc(NFSSVC_RESUMENFSD, NULL);
180096968c22SPawel Jakub Dawidek }
18018fae3551SRodney W. Grimes 
18028fae3551SRodney W. Grimes /*
18038fae3551SRodney W. Grimes  * Allocate an export list element
18048fae3551SRodney W. Grimes  */
180519c46d8cSEdward Tomasz Napierala static struct exportlist *
1806a7a7d96cSPhilippe Charnier get_exp(void)
18078fae3551SRodney W. Grimes {
18088fae3551SRodney W. Grimes 	struct exportlist *ep;
18098fae3551SRodney W. Grimes 
181053750151SXin LI 	ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
18118fae3551SRodney W. Grimes 	if (ep == (struct exportlist *)NULL)
18128fae3551SRodney W. Grimes 		out_of_mem();
18138fae3551SRodney W. Grimes 	return (ep);
18148fae3551SRodney W. Grimes }
18158fae3551SRodney W. Grimes 
18168fae3551SRodney W. Grimes /*
18178fae3551SRodney W. Grimes  * Allocate a group list element
18188fae3551SRodney W. Grimes  */
181919c46d8cSEdward Tomasz Napierala static struct grouplist *
1820a7a7d96cSPhilippe Charnier get_grp(void)
18218fae3551SRodney W. Grimes {
18228fae3551SRodney W. Grimes 	struct grouplist *gp;
18238fae3551SRodney W. Grimes 
182453750151SXin LI 	gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
18258fae3551SRodney W. Grimes 	if (gp == (struct grouplist *)NULL)
18268fae3551SRodney W. Grimes 		out_of_mem();
18278fae3551SRodney W. Grimes 	return (gp);
18288fae3551SRodney W. Grimes }
18298fae3551SRodney W. Grimes 
18308fae3551SRodney W. Grimes /*
18318fae3551SRodney W. Grimes  * Clean up upon an error in get_exportlist().
18328fae3551SRodney W. Grimes  */
183319c46d8cSEdward Tomasz Napierala static void
1834a7a7d96cSPhilippe Charnier getexp_err(struct exportlist *ep, struct grouplist *grp)
18358fae3551SRodney W. Grimes {
18368fae3551SRodney W. Grimes 	struct grouplist *tgrp;
18378fae3551SRodney W. Grimes 
1838288fa14aSJoerg Wunsch 	if (!(opt_flags & OP_QUIET))
183974853402SPhilippe Charnier 		syslog(LOG_ERR, "bad exports list line %s", line);
18408fae3551SRodney W. Grimes 	if (ep && (ep->ex_flag & EX_LINKED) == 0)
18418fae3551SRodney W. Grimes 		free_exp(ep);
18428fae3551SRodney W. Grimes 	while (grp) {
18438fae3551SRodney W. Grimes 		tgrp = grp;
18448fae3551SRodney W. Grimes 		grp = grp->gr_next;
18458fae3551SRodney W. Grimes 		free_grp(tgrp);
18468fae3551SRodney W. Grimes 	}
18478fae3551SRodney W. Grimes }
18488fae3551SRodney W. Grimes 
18498fae3551SRodney W. Grimes /*
18508fae3551SRodney W. Grimes  * Search the export list for a matching fs.
18518fae3551SRodney W. Grimes  */
185219c46d8cSEdward Tomasz Napierala static struct exportlist *
1853a7a7d96cSPhilippe Charnier ex_search(fsid_t *fsid)
18548fae3551SRodney W. Grimes {
18558fae3551SRodney W. Grimes 	struct exportlist *ep;
18568fae3551SRodney W. Grimes 
18578fae3551SRodney W. Grimes 	ep = exphead;
18588fae3551SRodney W. Grimes 	while (ep) {
18598fae3551SRodney W. Grimes 		if (ep->ex_fs.val[0] == fsid->val[0] &&
18608fae3551SRodney W. Grimes 		    ep->ex_fs.val[1] == fsid->val[1])
18618fae3551SRodney W. Grimes 			return (ep);
18628fae3551SRodney W. Grimes 		ep = ep->ex_next;
18638fae3551SRodney W. Grimes 	}
18648fae3551SRodney W. Grimes 	return (ep);
18658fae3551SRodney W. Grimes }
18668fae3551SRodney W. Grimes 
18678fae3551SRodney W. Grimes /*
18688fae3551SRodney W. Grimes  * Add a directory path to the list.
18698fae3551SRodney W. Grimes  */
187019c46d8cSEdward Tomasz Napierala static char *
1871a7a7d96cSPhilippe Charnier add_expdir(struct dirlist **dpp, char *cp, int len)
18728fae3551SRodney W. Grimes {
18738fae3551SRodney W. Grimes 	struct dirlist *dp;
18748fae3551SRodney W. Grimes 
18758fae3551SRodney W. Grimes 	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
187674853402SPhilippe Charnier 	if (dp == (struct dirlist *)NULL)
187774853402SPhilippe Charnier 		out_of_mem();
18788fae3551SRodney W. Grimes 	dp->dp_left = *dpp;
18798fae3551SRodney W. Grimes 	dp->dp_right = (struct dirlist *)NULL;
18808fae3551SRodney W. Grimes 	dp->dp_flag = 0;
18818fae3551SRodney W. Grimes 	dp->dp_hosts = (struct hostlist *)NULL;
18828fae3551SRodney W. Grimes 	strcpy(dp->dp_dirp, cp);
18838fae3551SRodney W. Grimes 	*dpp = dp;
18848fae3551SRodney W. Grimes 	return (dp->dp_dirp);
18858fae3551SRodney W. Grimes }
18868fae3551SRodney W. Grimes 
18878fae3551SRodney W. Grimes /*
18888fae3551SRodney W. Grimes  * Hang the dir list element off the dirpath binary tree as required
18898fae3551SRodney W. Grimes  * and update the entry for host.
18908fae3551SRodney W. Grimes  */
189119c46d8cSEdward Tomasz Napierala static void
1892a7a7d96cSPhilippe Charnier hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1893a7a7d96cSPhilippe Charnier 	int flags)
18948fae3551SRodney W. Grimes {
18958fae3551SRodney W. Grimes 	struct hostlist *hp;
18968fae3551SRodney W. Grimes 	struct dirlist *dp2;
18978fae3551SRodney W. Grimes 
1898a62dc406SDoug Rabson 	if (flags & OP_ALLDIRS) {
18998fae3551SRodney W. Grimes 		if (ep->ex_defdir)
19008fae3551SRodney W. Grimes 			free((caddr_t)dp);
19018fae3551SRodney W. Grimes 		else
19028fae3551SRodney W. Grimes 			ep->ex_defdir = dp;
1903a62dc406SDoug Rabson 		if (grp == (struct grouplist *)NULL) {
19048fae3551SRodney W. Grimes 			ep->ex_defdir->dp_flag |= DP_DEFSET;
1905c3f86a25SRick Macklem 			/* Save the default security flavors list. */
1906c3f86a25SRick Macklem 			ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1907c3f86a25SRick Macklem 			if (ep->ex_numsecflavors > 0)
1908c3f86a25SRick Macklem 				memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1909c3f86a25SRick Macklem 				    sizeof(ep->ex_secflavors));
1910a62dc406SDoug Rabson 		} else while (grp) {
19118fae3551SRodney W. Grimes 			hp = get_ht();
19128fae3551SRodney W. Grimes 			hp->ht_grp = grp;
19138fae3551SRodney W. Grimes 			hp->ht_next = ep->ex_defdir->dp_hosts;
19148fae3551SRodney W. Grimes 			ep->ex_defdir->dp_hosts = hp;
1915c3f86a25SRick Macklem 			/* Save the security flavors list for this host set. */
1916c3f86a25SRick Macklem 			grp->gr_numsecflavors = ep->ex_numsecflavors;
1917c3f86a25SRick Macklem 			if (ep->ex_numsecflavors > 0)
1918c3f86a25SRick Macklem 				memcpy(grp->gr_secflavors, ep->ex_secflavors,
1919c3f86a25SRick Macklem 				    sizeof(ep->ex_secflavors));
19208fae3551SRodney W. Grimes 			grp = grp->gr_next;
19218fae3551SRodney W. Grimes 		}
19228fae3551SRodney W. Grimes 	} else {
19238fae3551SRodney W. Grimes 
19248fae3551SRodney W. Grimes 		/*
192574853402SPhilippe Charnier 		 * Loop through the directories adding them to the tree.
19268fae3551SRodney W. Grimes 		 */
19278fae3551SRodney W. Grimes 		while (dp) {
19288fae3551SRodney W. Grimes 			dp2 = dp->dp_left;
1929c3f86a25SRick Macklem 			add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
19308fae3551SRodney W. Grimes 			dp = dp2;
19318fae3551SRodney W. Grimes 		}
19328fae3551SRodney W. Grimes 	}
19338fae3551SRodney W. Grimes }
19348fae3551SRodney W. Grimes 
19358fae3551SRodney W. Grimes /*
19368fae3551SRodney W. Grimes  * Traverse the binary tree either updating a node that is already there
19378fae3551SRodney W. Grimes  * for the new directory or adding the new node.
19388fae3551SRodney W. Grimes  */
193919c46d8cSEdward Tomasz Napierala static void
1940a7a7d96cSPhilippe Charnier add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
1941c3f86a25SRick Macklem 	int flags, struct exportlist *ep)
19428fae3551SRodney W. Grimes {
19438fae3551SRodney W. Grimes 	struct dirlist *dp;
19448fae3551SRodney W. Grimes 	struct hostlist *hp;
19458fae3551SRodney W. Grimes 	int cmp;
19468fae3551SRodney W. Grimes 
19478fae3551SRodney W. Grimes 	dp = *dpp;
19488fae3551SRodney W. Grimes 	if (dp) {
19498fae3551SRodney W. Grimes 		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
19508fae3551SRodney W. Grimes 		if (cmp > 0) {
1951c3f86a25SRick Macklem 			add_dlist(&dp->dp_left, newdp, grp, flags, ep);
19528fae3551SRodney W. Grimes 			return;
19538fae3551SRodney W. Grimes 		} else if (cmp < 0) {
1954c3f86a25SRick Macklem 			add_dlist(&dp->dp_right, newdp, grp, flags, ep);
19558fae3551SRodney W. Grimes 			return;
19568fae3551SRodney W. Grimes 		} else
19578fae3551SRodney W. Grimes 			free((caddr_t)newdp);
19588fae3551SRodney W. Grimes 	} else {
19598fae3551SRodney W. Grimes 		dp = newdp;
19608fae3551SRodney W. Grimes 		dp->dp_left = (struct dirlist *)NULL;
19618fae3551SRodney W. Grimes 		*dpp = dp;
19628fae3551SRodney W. Grimes 	}
19638fae3551SRodney W. Grimes 	if (grp) {
19648fae3551SRodney W. Grimes 
19658fae3551SRodney W. Grimes 		/*
19668fae3551SRodney W. Grimes 		 * Hang all of the host(s) off of the directory point.
19678fae3551SRodney W. Grimes 		 */
19688fae3551SRodney W. Grimes 		do {
19698fae3551SRodney W. Grimes 			hp = get_ht();
19708fae3551SRodney W. Grimes 			hp->ht_grp = grp;
19718fae3551SRodney W. Grimes 			hp->ht_next = dp->dp_hosts;
19728fae3551SRodney W. Grimes 			dp->dp_hosts = hp;
1973c3f86a25SRick Macklem 			/* Save the security flavors list for this host set. */
1974c3f86a25SRick Macklem 			grp->gr_numsecflavors = ep->ex_numsecflavors;
1975c3f86a25SRick Macklem 			if (ep->ex_numsecflavors > 0)
1976c3f86a25SRick Macklem 				memcpy(grp->gr_secflavors, ep->ex_secflavors,
1977c3f86a25SRick Macklem 				    sizeof(ep->ex_secflavors));
19788fae3551SRodney W. Grimes 			grp = grp->gr_next;
19798fae3551SRodney W. Grimes 		} while (grp);
1980a62dc406SDoug Rabson 	} else {
19818fae3551SRodney W. Grimes 		dp->dp_flag |= DP_DEFSET;
1982c3f86a25SRick Macklem 		/* Save the default security flavors list. */
1983c3f86a25SRick Macklem 		ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1984c3f86a25SRick Macklem 		if (ep->ex_numsecflavors > 0)
1985c3f86a25SRick Macklem 			memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1986c3f86a25SRick Macklem 			    sizeof(ep->ex_secflavors));
1987a62dc406SDoug Rabson 	}
19888fae3551SRodney W. Grimes }
19898fae3551SRodney W. Grimes 
19908fae3551SRodney W. Grimes /*
19918fae3551SRodney W. Grimes  * Search for a dirpath on the export point.
19928fae3551SRodney W. Grimes  */
199319c46d8cSEdward Tomasz Napierala static struct dirlist *
1994a7a7d96cSPhilippe Charnier dirp_search(struct dirlist *dp, char *dirp)
19958fae3551SRodney W. Grimes {
19968fae3551SRodney W. Grimes 	int cmp;
19978fae3551SRodney W. Grimes 
19988fae3551SRodney W. Grimes 	if (dp) {
19998360efbdSAlfred Perlstein 		cmp = strcmp(dp->dp_dirp, dirp);
20008fae3551SRodney W. Grimes 		if (cmp > 0)
20018360efbdSAlfred Perlstein 			return (dirp_search(dp->dp_left, dirp));
20028fae3551SRodney W. Grimes 		else if (cmp < 0)
20038360efbdSAlfred Perlstein 			return (dirp_search(dp->dp_right, dirp));
20048fae3551SRodney W. Grimes 		else
20058fae3551SRodney W. Grimes 			return (dp);
20068fae3551SRodney W. Grimes 	}
20078fae3551SRodney W. Grimes 	return (dp);
20088fae3551SRodney W. Grimes }
20098fae3551SRodney W. Grimes 
20108fae3551SRodney W. Grimes /*
20118fae3551SRodney W. Grimes  * Scan for a host match in a directory tree.
20128fae3551SRodney W. Grimes  */
201319c46d8cSEdward Tomasz Napierala static int
2014a7a7d96cSPhilippe Charnier chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2015c3f86a25SRick Macklem 	int *hostsetp, int *numsecflavors, int **secflavorsp)
20168fae3551SRodney W. Grimes {
20178fae3551SRodney W. Grimes 	struct hostlist *hp;
20188fae3551SRodney W. Grimes 	struct grouplist *grp;
20198360efbdSAlfred Perlstein 	struct addrinfo *ai;
20208fae3551SRodney W. Grimes 
20218fae3551SRodney W. Grimes 	if (dp) {
20228fae3551SRodney W. Grimes 		if (dp->dp_flag & DP_DEFSET)
2023a62dc406SDoug Rabson 			*defsetp = dp->dp_flag;
20248fae3551SRodney W. Grimes 		hp = dp->dp_hosts;
20258fae3551SRodney W. Grimes 		while (hp) {
20268fae3551SRodney W. Grimes 			grp = hp->ht_grp;
20278fae3551SRodney W. Grimes 			switch (grp->gr_type) {
20288fae3551SRodney W. Grimes 			case GT_HOST:
20298360efbdSAlfred Perlstein 				ai = grp->gr_ptr.gt_addrinfo;
20308360efbdSAlfred Perlstein 				for (; ai; ai = ai->ai_next) {
203160caaee2SIan Dowse 					if (!sacmp(ai->ai_addr, saddr, NULL)) {
20328360efbdSAlfred Perlstein 						*hostsetp =
20338360efbdSAlfred Perlstein 						    (hp->ht_flag | DP_HOSTSET);
2034c3f86a25SRick Macklem 						if (numsecflavors != NULL) {
2035c3f86a25SRick Macklem 							*numsecflavors =
2036c3f86a25SRick Macklem 							    grp->gr_numsecflavors;
2037c3f86a25SRick Macklem 							*secflavorsp =
2038c3f86a25SRick Macklem 							    grp->gr_secflavors;
2039c3f86a25SRick Macklem 						}
20408fae3551SRodney W. Grimes 						return (1);
2041a62dc406SDoug Rabson 					}
20428fae3551SRodney W. Grimes 				}
20438fae3551SRodney W. Grimes 				break;
20448fae3551SRodney W. Grimes 			case GT_NET:
204560caaee2SIan Dowse 				if (!sacmp(saddr, (struct sockaddr *)
204660caaee2SIan Dowse 				    &grp->gr_ptr.gt_net.nt_net,
204760caaee2SIan Dowse 				    (struct sockaddr *)
204860caaee2SIan Dowse 				    &grp->gr_ptr.gt_net.nt_mask)) {
2049a62dc406SDoug Rabson 					*hostsetp = (hp->ht_flag | DP_HOSTSET);
2050c3f86a25SRick Macklem 					if (numsecflavors != NULL) {
2051c3f86a25SRick Macklem 						*numsecflavors =
2052c3f86a25SRick Macklem 						    grp->gr_numsecflavors;
2053c3f86a25SRick Macklem 						*secflavorsp =
2054c3f86a25SRick Macklem 						    grp->gr_secflavors;
2055c3f86a25SRick Macklem 					}
20568fae3551SRodney W. Grimes 					return (1);
2057a62dc406SDoug Rabson 				}
20588fae3551SRodney W. Grimes 				break;
205960caaee2SIan Dowse 			}
20608fae3551SRodney W. Grimes 			hp = hp->ht_next;
20618fae3551SRodney W. Grimes 		}
20628fae3551SRodney W. Grimes 	}
20638fae3551SRodney W. Grimes 	return (0);
20648fae3551SRodney W. Grimes }
20658fae3551SRodney W. Grimes 
20668fae3551SRodney W. Grimes /*
20678fae3551SRodney W. Grimes  * Scan tree for a host that matches the address.
20688fae3551SRodney W. Grimes  */
206919c46d8cSEdward Tomasz Napierala static int
2070a7a7d96cSPhilippe Charnier scan_tree(struct dirlist *dp, struct sockaddr *saddr)
20718fae3551SRodney W. Grimes {
2072a62dc406SDoug Rabson 	int defset, hostset;
20738fae3551SRodney W. Grimes 
20748fae3551SRodney W. Grimes 	if (dp) {
20758fae3551SRodney W. Grimes 		if (scan_tree(dp->dp_left, saddr))
20768fae3551SRodney W. Grimes 			return (1);
2077c3f86a25SRick Macklem 		if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
20788fae3551SRodney W. Grimes 			return (1);
20798fae3551SRodney W. Grimes 		if (scan_tree(dp->dp_right, saddr))
20808fae3551SRodney W. Grimes 			return (1);
20818fae3551SRodney W. Grimes 	}
20828fae3551SRodney W. Grimes 	return (0);
20838fae3551SRodney W. Grimes }
20848fae3551SRodney W. Grimes 
20858fae3551SRodney W. Grimes /*
20868fae3551SRodney W. Grimes  * Traverse the dirlist tree and free it up.
20878fae3551SRodney W. Grimes  */
208819c46d8cSEdward Tomasz Napierala static void
2089a7a7d96cSPhilippe Charnier free_dir(struct dirlist *dp)
20908fae3551SRodney W. Grimes {
20918fae3551SRodney W. Grimes 
20928fae3551SRodney W. Grimes 	if (dp) {
20938fae3551SRodney W. Grimes 		free_dir(dp->dp_left);
20948fae3551SRodney W. Grimes 		free_dir(dp->dp_right);
20958fae3551SRodney W. Grimes 		free_host(dp->dp_hosts);
20968fae3551SRodney W. Grimes 		free((caddr_t)dp);
20978fae3551SRodney W. Grimes 	}
20988fae3551SRodney W. Grimes }
20998fae3551SRodney W. Grimes 
21008fae3551SRodney W. Grimes /*
2101a9148abdSDoug Rabson  * Parse a colon separated list of security flavors
2102a9148abdSDoug Rabson  */
210319c46d8cSEdward Tomasz Napierala static int
2104a7a7d96cSPhilippe Charnier parsesec(char *seclist, struct exportlist *ep)
2105a9148abdSDoug Rabson {
2106a9148abdSDoug Rabson 	char *cp, savedc;
2107a9148abdSDoug Rabson 	int flavor;
2108a9148abdSDoug Rabson 
2109a9148abdSDoug Rabson 	ep->ex_numsecflavors = 0;
2110a9148abdSDoug Rabson 	for (;;) {
2111a9148abdSDoug Rabson 		cp = strchr(seclist, ':');
2112a9148abdSDoug Rabson 		if (cp) {
2113a9148abdSDoug Rabson 			savedc = *cp;
2114a9148abdSDoug Rabson 			*cp = '\0';
2115a9148abdSDoug Rabson 		}
2116a9148abdSDoug Rabson 
2117a9148abdSDoug Rabson 		if (!strcmp(seclist, "sys"))
2118a9148abdSDoug Rabson 			flavor = AUTH_SYS;
2119a9148abdSDoug Rabson 		else if (!strcmp(seclist, "krb5"))
2120a9148abdSDoug Rabson 			flavor = RPCSEC_GSS_KRB5;
2121a9148abdSDoug Rabson 		else if (!strcmp(seclist, "krb5i"))
2122a9148abdSDoug Rabson 			flavor = RPCSEC_GSS_KRB5I;
2123a9148abdSDoug Rabson 		else if (!strcmp(seclist, "krb5p"))
2124a9148abdSDoug Rabson 			flavor = RPCSEC_GSS_KRB5P;
2125a9148abdSDoug Rabson 		else {
2126a9148abdSDoug Rabson 			if (cp)
2127a9148abdSDoug Rabson 				*cp = savedc;
2128a9148abdSDoug Rabson 			syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2129a9148abdSDoug Rabson 			return (1);
2130a9148abdSDoug Rabson 		}
2131a9148abdSDoug Rabson 		if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2132a9148abdSDoug Rabson 			if (cp)
2133a9148abdSDoug Rabson 				*cp = savedc;
2134a9148abdSDoug Rabson 			syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2135a9148abdSDoug Rabson 			return (1);
2136a9148abdSDoug Rabson 		}
2137a9148abdSDoug Rabson 		ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2138a9148abdSDoug Rabson 		ep->ex_numsecflavors++;
2139a9148abdSDoug Rabson 		if (cp) {
2140a9148abdSDoug Rabson 			*cp = savedc;
2141a9148abdSDoug Rabson 			seclist = cp + 1;
2142a9148abdSDoug Rabson 		} else {
2143a9148abdSDoug Rabson 			break;
2144a9148abdSDoug Rabson 		}
2145a9148abdSDoug Rabson 	}
2146a9148abdSDoug Rabson 	return (0);
2147a9148abdSDoug Rabson }
2148a9148abdSDoug Rabson 
2149a9148abdSDoug Rabson /*
21508fae3551SRodney W. Grimes  * Parse the option string and update fields.
21518fae3551SRodney W. Grimes  * Option arguments may either be -<option>=<value> or
21528fae3551SRodney W. Grimes  * -<option> <value>
21538fae3551SRodney W. Grimes  */
215419c46d8cSEdward Tomasz Napierala static int
2155a7a7d96cSPhilippe Charnier do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2156a7a7d96cSPhilippe Charnier 	int *has_hostp, int *exflagsp, struct xucred *cr)
21578fae3551SRodney W. Grimes {
21588fae3551SRodney W. Grimes 	char *cpoptarg, *cpoptend;
21598fae3551SRodney W. Grimes 	char *cp, *endcp, *cpopt, savedc, savedc2;
21608fae3551SRodney W. Grimes 	int allflag, usedarg;
21618fae3551SRodney W. Grimes 
2162cb479b11SAlfred Perlstein 	savedc2 = '\0';
21638fae3551SRodney W. Grimes 	cpopt = *cpp;
21648fae3551SRodney W. Grimes 	cpopt++;
21658fae3551SRodney W. Grimes 	cp = *endcpp;
21668fae3551SRodney W. Grimes 	savedc = *cp;
21678fae3551SRodney W. Grimes 	*cp = '\0';
21688fae3551SRodney W. Grimes 	while (cpopt && *cpopt) {
21698fae3551SRodney W. Grimes 		allflag = 1;
21708fae3551SRodney W. Grimes 		usedarg = -2;
217174853402SPhilippe Charnier 		if ((cpoptend = strchr(cpopt, ','))) {
21728fae3551SRodney W. Grimes 			*cpoptend++ = '\0';
217374853402SPhilippe Charnier 			if ((cpoptarg = strchr(cpopt, '=')))
21748fae3551SRodney W. Grimes 				*cpoptarg++ = '\0';
21758fae3551SRodney W. Grimes 		} else {
217674853402SPhilippe Charnier 			if ((cpoptarg = strchr(cpopt, '=')))
21778fae3551SRodney W. Grimes 				*cpoptarg++ = '\0';
21788fae3551SRodney W. Grimes 			else {
21798fae3551SRodney W. Grimes 				*cp = savedc;
21808fae3551SRodney W. Grimes 				nextfield(&cp, &endcp);
21818fae3551SRodney W. Grimes 				**endcpp = '\0';
21828fae3551SRodney W. Grimes 				if (endcp > cp && *cp != '-') {
21838fae3551SRodney W. Grimes 					cpoptarg = cp;
21848fae3551SRodney W. Grimes 					savedc2 = *endcp;
21858fae3551SRodney W. Grimes 					*endcp = '\0';
21868fae3551SRodney W. Grimes 					usedarg = 0;
21878fae3551SRodney W. Grimes 				}
21888fae3551SRodney W. Grimes 			}
21898fae3551SRodney W. Grimes 		}
21908fae3551SRodney W. Grimes 		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
21918fae3551SRodney W. Grimes 			*exflagsp |= MNT_EXRDONLY;
21928fae3551SRodney W. Grimes 		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
21938fae3551SRodney W. Grimes 		    !(allflag = strcmp(cpopt, "mapall")) ||
21948fae3551SRodney W. Grimes 		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
21958fae3551SRodney W. Grimes 			usedarg++;
21968fae3551SRodney W. Grimes 			parsecred(cpoptarg, cr);
21978fae3551SRodney W. Grimes 			if (allflag == 0) {
21988fae3551SRodney W. Grimes 				*exflagsp |= MNT_EXPORTANON;
21998fae3551SRodney W. Grimes 				opt_flags |= OP_MAPALL;
22008fae3551SRodney W. Grimes 			} else
22018fae3551SRodney W. Grimes 				opt_flags |= OP_MAPROOT;
22028fae3551SRodney W. Grimes 		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
22038fae3551SRodney W. Grimes 		    !strcmp(cpopt, "m"))) {
22048fae3551SRodney W. Grimes 			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
220574853402SPhilippe Charnier 				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
22068fae3551SRodney W. Grimes 				return (1);
22078fae3551SRodney W. Grimes 			}
22088fae3551SRodney W. Grimes 			usedarg++;
22098fae3551SRodney W. Grimes 			opt_flags |= OP_MASK;
22108fae3551SRodney W. Grimes 		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
22118fae3551SRodney W. Grimes 			!strcmp(cpopt, "n"))) {
22128360efbdSAlfred Perlstein 			if (strchr(cpoptarg, '/') != NULL) {
22138360efbdSAlfred Perlstein 				if (debug)
22148360efbdSAlfred Perlstein 					fprintf(stderr, "setting OP_MASKLEN\n");
22158360efbdSAlfred Perlstein 				opt_flags |= OP_MASKLEN;
22168360efbdSAlfred Perlstein 			}
22178fae3551SRodney W. Grimes 			if (grp->gr_type != GT_NULL) {
221874853402SPhilippe Charnier 				syslog(LOG_ERR, "network/host conflict");
22198fae3551SRodney W. Grimes 				return (1);
22208fae3551SRodney W. Grimes 			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
222174853402SPhilippe Charnier 				syslog(LOG_ERR, "bad net: %s", cpoptarg);
22228fae3551SRodney W. Grimes 				return (1);
22238fae3551SRodney W. Grimes 			}
22248fae3551SRodney W. Grimes 			grp->gr_type = GT_NET;
22258fae3551SRodney W. Grimes 			*has_hostp = 1;
22268fae3551SRodney W. Grimes 			usedarg++;
22278fae3551SRodney W. Grimes 			opt_flags |= OP_NET;
22288fae3551SRodney W. Grimes 		} else if (!strcmp(cpopt, "alldirs")) {
22298fae3551SRodney W. Grimes 			opt_flags |= OP_ALLDIRS;
2230cb3923e0SDoug Rabson 		} else if (!strcmp(cpopt, "public")) {
2231cb3923e0SDoug Rabson 			*exflagsp |= MNT_EXPUBLIC;
2232cb3923e0SDoug Rabson 		} else if (!strcmp(cpopt, "webnfs")) {
2233cb3923e0SDoug Rabson 			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2234cb3923e0SDoug Rabson 			opt_flags |= OP_MAPALL;
2235cb3923e0SDoug Rabson 		} else if (cpoptarg && !strcmp(cpopt, "index")) {
2236cb3923e0SDoug Rabson 			ep->ex_indexfile = strdup(cpoptarg);
2237288fa14aSJoerg Wunsch 		} else if (!strcmp(cpopt, "quiet")) {
2238288fa14aSJoerg Wunsch 			opt_flags |= OP_QUIET;
2239dcdc127bSSergey Kandaurov 		} else if (cpoptarg && !strcmp(cpopt, "sec")) {
2240a9148abdSDoug Rabson 			if (parsesec(cpoptarg, ep))
2241a9148abdSDoug Rabson 				return (1);
2242a9148abdSDoug Rabson 			opt_flags |= OP_SEC;
2243a9148abdSDoug Rabson 			usedarg++;
22448fae3551SRodney W. Grimes 		} else {
224574853402SPhilippe Charnier 			syslog(LOG_ERR, "bad opt %s", cpopt);
22468fae3551SRodney W. Grimes 			return (1);
22478fae3551SRodney W. Grimes 		}
22488fae3551SRodney W. Grimes 		if (usedarg >= 0) {
22498fae3551SRodney W. Grimes 			*endcp = savedc2;
22508fae3551SRodney W. Grimes 			**endcpp = savedc;
22518fae3551SRodney W. Grimes 			if (usedarg > 0) {
22528fae3551SRodney W. Grimes 				*cpp = cp;
22538fae3551SRodney W. Grimes 				*endcpp = endcp;
22548fae3551SRodney W. Grimes 			}
22558fae3551SRodney W. Grimes 			return (0);
22568fae3551SRodney W. Grimes 		}
22578fae3551SRodney W. Grimes 		cpopt = cpoptend;
22588fae3551SRodney W. Grimes 	}
22598fae3551SRodney W. Grimes 	**endcpp = savedc;
22608fae3551SRodney W. Grimes 	return (0);
22618fae3551SRodney W. Grimes }
22628fae3551SRodney W. Grimes 
22638fae3551SRodney W. Grimes /*
22648fae3551SRodney W. Grimes  * Translate a character string to the corresponding list of network
22658fae3551SRodney W. Grimes  * addresses for a hostname.
22668fae3551SRodney W. Grimes  */
226719c46d8cSEdward Tomasz Napierala static int
2268a7a7d96cSPhilippe Charnier get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
22698fae3551SRodney W. Grimes {
22708b5a6d67SBill Paul 	struct grouplist *checkgrp;
227101709abfSIan Dowse 	struct addrinfo *ai, *tai, hints;
22728360efbdSAlfred Perlstein 	int ecode;
22738360efbdSAlfred Perlstein 	char host[NI_MAXHOST];
22748fae3551SRodney W. Grimes 
22758360efbdSAlfred Perlstein 	if (grp->gr_type != GT_NULL) {
22768360efbdSAlfred Perlstein 		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
22778fae3551SRodney W. Grimes 		return (1);
22788fae3551SRodney W. Grimes 	}
22798360efbdSAlfred Perlstein 	memset(&hints, 0, sizeof hints);
22808360efbdSAlfred Perlstein 	hints.ai_flags = AI_CANONNAME;
22818360efbdSAlfred Perlstein 	hints.ai_protocol = IPPROTO_UDP;
22828360efbdSAlfred Perlstein 	ecode = getaddrinfo(cp, NULL, &hints, &ai);
22838360efbdSAlfred Perlstein 	if (ecode != 0) {
228401709abfSIan Dowse 		syslog(LOG_ERR,"can't get address info for host %s", cp);
22858360efbdSAlfred Perlstein 		return 1;
22868fae3551SRodney W. Grimes 	}
22878360efbdSAlfred Perlstein 	grp->gr_ptr.gt_addrinfo = ai;
22888360efbdSAlfred Perlstein 	while (ai != NULL) {
22898360efbdSAlfred Perlstein 		if (ai->ai_canonname == NULL) {
22908360efbdSAlfred Perlstein 			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
22914f101318SHajimu UMEMOTO 			    sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
22928360efbdSAlfred Perlstein 				strlcpy(host, "?", sizeof(host));
22938360efbdSAlfred Perlstein 			ai->ai_canonname = strdup(host);
22948360efbdSAlfred Perlstein 			ai->ai_flags |= AI_CANONNAME;
22956d359f31SIan Dowse 		}
22968fae3551SRodney W. Grimes 		if (debug)
229701709abfSIan Dowse 			fprintf(stderr, "got host %s\n", ai->ai_canonname);
229801709abfSIan Dowse 		/*
229901709abfSIan Dowse 		 * Sanity check: make sure we don't already have an entry
230001709abfSIan Dowse 		 * for this host in the grouplist.
230101709abfSIan Dowse 		 */
230201709abfSIan Dowse 		for (checkgrp = tgrp; checkgrp != NULL;
230301709abfSIan Dowse 		    checkgrp = checkgrp->gr_next) {
230401709abfSIan Dowse 			if (checkgrp->gr_type != GT_HOST)
230501709abfSIan Dowse 				continue;
230601709abfSIan Dowse 			for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
230701709abfSIan Dowse 			    tai = tai->ai_next) {
230860caaee2SIan Dowse 				if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
230901709abfSIan Dowse 					continue;
231001709abfSIan Dowse 				if (debug)
231101709abfSIan Dowse 					fprintf(stderr,
231201709abfSIan Dowse 					    "ignoring duplicate host %s\n",
231301709abfSIan Dowse 					    ai->ai_canonname);
231401709abfSIan Dowse 				grp->gr_type = GT_IGNORE;
231501709abfSIan Dowse 				return (0);
231601709abfSIan Dowse 			}
231701709abfSIan Dowse 		}
23188360efbdSAlfred Perlstein 		ai = ai->ai_next;
23198360efbdSAlfred Perlstein 	}
232001709abfSIan Dowse 	grp->gr_type = GT_HOST;
23218fae3551SRodney W. Grimes 	return (0);
23228fae3551SRodney W. Grimes }
23238fae3551SRodney W. Grimes 
23248fae3551SRodney W. Grimes /*
23258fae3551SRodney W. Grimes  * Free up an exports list component
23268fae3551SRodney W. Grimes  */
232719c46d8cSEdward Tomasz Napierala static void
2328a7a7d96cSPhilippe Charnier free_exp(struct exportlist *ep)
23298fae3551SRodney W. Grimes {
23308fae3551SRodney W. Grimes 
23318fae3551SRodney W. Grimes 	if (ep->ex_defdir) {
23328fae3551SRodney W. Grimes 		free_host(ep->ex_defdir->dp_hosts);
23338fae3551SRodney W. Grimes 		free((caddr_t)ep->ex_defdir);
23348fae3551SRodney W. Grimes 	}
23358fae3551SRodney W. Grimes 	if (ep->ex_fsdir)
23368fae3551SRodney W. Grimes 		free(ep->ex_fsdir);
2337cb3923e0SDoug Rabson 	if (ep->ex_indexfile)
2338cb3923e0SDoug Rabson 		free(ep->ex_indexfile);
23398fae3551SRodney W. Grimes 	free_dir(ep->ex_dirl);
23408fae3551SRodney W. Grimes 	free((caddr_t)ep);
23418fae3551SRodney W. Grimes }
23428fae3551SRodney W. Grimes 
23438fae3551SRodney W. Grimes /*
23448fae3551SRodney W. Grimes  * Free hosts.
23458fae3551SRodney W. Grimes  */
234619c46d8cSEdward Tomasz Napierala static void
2347a7a7d96cSPhilippe Charnier free_host(struct hostlist *hp)
23488fae3551SRodney W. Grimes {
23498fae3551SRodney W. Grimes 	struct hostlist *hp2;
23508fae3551SRodney W. Grimes 
23518fae3551SRodney W. Grimes 	while (hp) {
23528fae3551SRodney W. Grimes 		hp2 = hp;
23538fae3551SRodney W. Grimes 		hp = hp->ht_next;
23548fae3551SRodney W. Grimes 		free((caddr_t)hp2);
23558fae3551SRodney W. Grimes 	}
23568fae3551SRodney W. Grimes }
23578fae3551SRodney W. Grimes 
235819c46d8cSEdward Tomasz Napierala static struct hostlist *
2359a7a7d96cSPhilippe Charnier get_ht(void)
23608fae3551SRodney W. Grimes {
23618fae3551SRodney W. Grimes 	struct hostlist *hp;
23628fae3551SRodney W. Grimes 
23638fae3551SRodney W. Grimes 	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
23648fae3551SRodney W. Grimes 	if (hp == (struct hostlist *)NULL)
23658fae3551SRodney W. Grimes 		out_of_mem();
23668fae3551SRodney W. Grimes 	hp->ht_next = (struct hostlist *)NULL;
2367a62dc406SDoug Rabson 	hp->ht_flag = 0;
23688fae3551SRodney W. Grimes 	return (hp);
23698fae3551SRodney W. Grimes }
23708fae3551SRodney W. Grimes 
23718fae3551SRodney W. Grimes /*
23728fae3551SRodney W. Grimes  * Out of memory, fatal
23738fae3551SRodney W. Grimes  */
237419c46d8cSEdward Tomasz Napierala static void
2375a7a7d96cSPhilippe Charnier out_of_mem(void)
23768fae3551SRodney W. Grimes {
23778fae3551SRodney W. Grimes 
237874853402SPhilippe Charnier 	syslog(LOG_ERR, "out of memory");
23798fae3551SRodney W. Grimes 	exit(2);
23808fae3551SRodney W. Grimes }
23818fae3551SRodney W. Grimes 
23828fae3551SRodney W. Grimes /*
23836a09faf2SCraig Rodrigues  * Do the nmount() syscall with the update flag to push the export info into
23848fae3551SRodney W. Grimes  * the kernel.
23858fae3551SRodney W. Grimes  */
238619c46d8cSEdward Tomasz Napierala static int
23876a09faf2SCraig Rodrigues do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
23886a09faf2SCraig Rodrigues     struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
23898fae3551SRodney W. Grimes {
2390f93caef2SIan Dowse 	struct statfs fsb1;
23918360efbdSAlfred Perlstein 	struct addrinfo *ai;
239279b86807SEdward Tomasz Napierala 	struct export_args *eap;
23936a09faf2SCraig Rodrigues 	char errmsg[255];
23946a09faf2SCraig Rodrigues 	char *cp;
23958fae3551SRodney W. Grimes 	int done;
23966a09faf2SCraig Rodrigues 	char savedc;
23976a09faf2SCraig Rodrigues 	struct iovec *iov;
2398a9148abdSDoug Rabson 	int i, iovlen;
23996a09faf2SCraig Rodrigues 	int ret;
2400bcc1d071SRick Macklem 	struct nfsex_args nfsea;
2401bcc1d071SRick Macklem 
2402bcc1d071SRick Macklem 	eap = &nfsea.export;
24038fae3551SRodney W. Grimes 
24046a09faf2SCraig Rodrigues 	cp = NULL;
24056a09faf2SCraig Rodrigues 	savedc = '\0';
24066a09faf2SCraig Rodrigues 	iov = NULL;
24076a09faf2SCraig Rodrigues 	iovlen = 0;
24086a09faf2SCraig Rodrigues 	ret = 0;
240960caaee2SIan Dowse 
2410bcc1d071SRick Macklem 	bzero(eap, sizeof (struct export_args));
24116a09faf2SCraig Rodrigues 	bzero(errmsg, sizeof(errmsg));
2412bcc1d071SRick Macklem 	eap->ex_flags = exflags;
2413bcc1d071SRick Macklem 	eap->ex_anon = *anoncrp;
2414bcc1d071SRick Macklem 	eap->ex_indexfile = ep->ex_indexfile;
24156d359f31SIan Dowse 	if (grp->gr_type == GT_HOST)
24168360efbdSAlfred Perlstein 		ai = grp->gr_ptr.gt_addrinfo;
24176d359f31SIan Dowse 	else
24186d359f31SIan Dowse 		ai = NULL;
2419bcc1d071SRick Macklem 	eap->ex_numsecflavors = ep->ex_numsecflavors;
2420bcc1d071SRick Macklem 	for (i = 0; i < eap->ex_numsecflavors; i++)
2421bcc1d071SRick Macklem 		eap->ex_secflavors[i] = ep->ex_secflavors[i];
2422bcc1d071SRick Macklem 	if (eap->ex_numsecflavors == 0) {
2423bcc1d071SRick Macklem 		eap->ex_numsecflavors = 1;
2424bcc1d071SRick Macklem 		eap->ex_secflavors[0] = AUTH_SYS;
2425a9148abdSDoug Rabson 	}
24268fae3551SRodney W. Grimes 	done = FALSE;
24276a09faf2SCraig Rodrigues 
2428bcc1d071SRick Macklem 	if (v4root_phase == 0) {
24296a09faf2SCraig Rodrigues 		build_iovec(&iov, &iovlen, "fstype", NULL, 0);
24306a09faf2SCraig Rodrigues 		build_iovec(&iov, &iovlen, "fspath", NULL, 0);
24316a09faf2SCraig Rodrigues 		build_iovec(&iov, &iovlen, "from", NULL, 0);
24326a09faf2SCraig Rodrigues 		build_iovec(&iov, &iovlen, "update", NULL, 0);
2433bcc1d071SRick Macklem 		build_iovec(&iov, &iovlen, "export", eap,
2434bcc1d071SRick Macklem 		    sizeof (struct export_args));
24356a09faf2SCraig Rodrigues 		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2436bcc1d071SRick Macklem 	}
24376a09faf2SCraig Rodrigues 
24388fae3551SRodney W. Grimes 	while (!done) {
24398fae3551SRodney W. Grimes 		switch (grp->gr_type) {
24408fae3551SRodney W. Grimes 		case GT_HOST:
24416d359f31SIan Dowse 			if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
24428360efbdSAlfred Perlstein 				goto skip;
2443bcc1d071SRick Macklem 			eap->ex_addr = ai->ai_addr;
2444bcc1d071SRick Macklem 			eap->ex_addrlen = ai->ai_addrlen;
2445bcc1d071SRick Macklem 			eap->ex_masklen = 0;
24468fae3551SRodney W. Grimes 			break;
24478fae3551SRodney W. Grimes 		case GT_NET:
244860caaee2SIan Dowse 			if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
24498360efbdSAlfred Perlstein 			    have_v6 == 0)
24508360efbdSAlfred Perlstein 				goto skip;
2451bcc1d071SRick Macklem 			eap->ex_addr =
245260caaee2SIan Dowse 			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2453bcc1d071SRick Macklem 			eap->ex_addrlen =
24546a09faf2SCraig Rodrigues 			    ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2455bcc1d071SRick Macklem 			eap->ex_mask =
245660caaee2SIan Dowse 			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2457bcc1d071SRick Macklem 			eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
24588fae3551SRodney W. Grimes 			break;
24596d359f31SIan Dowse 		case GT_DEFAULT:
2460bcc1d071SRick Macklem 			eap->ex_addr = NULL;
2461bcc1d071SRick Macklem 			eap->ex_addrlen = 0;
2462bcc1d071SRick Macklem 			eap->ex_mask = NULL;
2463bcc1d071SRick Macklem 			eap->ex_masklen = 0;
24646d359f31SIan Dowse 			break;
24658b5a6d67SBill Paul 		case GT_IGNORE:
24666a09faf2SCraig Rodrigues 			ret = 0;
24676a09faf2SCraig Rodrigues 			goto error_exit;
24688b5a6d67SBill Paul 			break;
24698fae3551SRodney W. Grimes 		default:
247074853402SPhilippe Charnier 			syslog(LOG_ERR, "bad grouptype");
24718fae3551SRodney W. Grimes 			if (cp)
24728fae3551SRodney W. Grimes 				*cp = savedc;
24736a09faf2SCraig Rodrigues 			ret = 1;
24746a09faf2SCraig Rodrigues 			goto error_exit;
24758fae3551SRodney W. Grimes 		};
24768fae3551SRodney W. Grimes 
24778fae3551SRodney W. Grimes 		/*
2478bcc1d071SRick Macklem 		 * For V4:, use the nfssvc() syscall, instead of mount().
2479bcc1d071SRick Macklem 		 */
2480bcc1d071SRick Macklem 		if (v4root_phase == 2) {
2481bcc1d071SRick Macklem 			nfsea.fspec = v4root_dirpath;
248279b86807SEdward Tomasz Napierala 			if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2483bcc1d071SRick Macklem 				syslog(LOG_ERR, "Exporting V4: failed");
2484bcc1d071SRick Macklem 				return (2);
2485bcc1d071SRick Macklem 			}
2486bcc1d071SRick Macklem 		} else {
2487bcc1d071SRick Macklem 			/*
24888fae3551SRodney W. Grimes 			 * XXX:
2489bcc1d071SRick Macklem 			 * Maybe I should just use the fsb->f_mntonname path
2490bcc1d071SRick Macklem 			 * instead of looping back up the dirp to the mount
2491bcc1d071SRick Macklem 			 * point??
24928fae3551SRodney W. Grimes 			 * Also, needs to know how to export all types of local
249387564113SPeter Wemm 			 * exportable filesystems and not just "ufs".
24948fae3551SRodney W. Grimes 			 */
24956a09faf2SCraig Rodrigues 			iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
24966a09faf2SCraig Rodrigues 			iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
24976a09faf2SCraig Rodrigues 			iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
24986a09faf2SCraig Rodrigues 			iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
24996a09faf2SCraig Rodrigues 			iov[5].iov_base = fsb->f_mntfromname; /* "from" */
25006a09faf2SCraig Rodrigues 			iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
25014a185fa6SBryan Drewery 			errmsg[0] = '\0';
25026a09faf2SCraig Rodrigues 
250355dd1327SCraig Rodrigues 			while (nmount(iov, iovlen, fsb->f_flags) < 0) {
25048fae3551SRodney W. Grimes 				if (cp)
25058fae3551SRodney W. Grimes 					*cp-- = savedc;
25068fae3551SRodney W. Grimes 				else
25078fae3551SRodney W. Grimes 					cp = dirp + dirplen - 1;
25086a09faf2SCraig Rodrigues 				if (opt_flags & OP_QUIET) {
25096a09faf2SCraig Rodrigues 					ret = 1;
25106a09faf2SCraig Rodrigues 					goto error_exit;
25116a09faf2SCraig Rodrigues 				}
25128fae3551SRodney W. Grimes 				if (errno == EPERM) {
251301709abfSIan Dowse 					if (debug)
251477909162SXin LI 						warnx("can't change attributes for %s: %s",
251577909162SXin LI 						    dirp, errmsg);
25168fae3551SRodney W. Grimes 					syslog(LOG_ERR,
251777909162SXin LI 					   "can't change attributes for %s: %s",
251877909162SXin LI 					    dirp, errmsg);
25196a09faf2SCraig Rodrigues 					ret = 1;
25206a09faf2SCraig Rodrigues 					goto error_exit;
25218fae3551SRodney W. Grimes 				}
25228fae3551SRodney W. Grimes 				if (opt_flags & OP_ALLDIRS) {
2523288fa14aSJoerg Wunsch 					if (errno == EINVAL)
2524288fa14aSJoerg Wunsch 						syslog(LOG_ERR,
2525288fa14aSJoerg Wunsch 		"-alldirs requested but %s is not a filesystem mountpoint",
2526288fa14aSJoerg Wunsch 						    dirp);
2527288fa14aSJoerg Wunsch 					else
2528288fa14aSJoerg Wunsch 						syslog(LOG_ERR,
2529288fa14aSJoerg Wunsch 						    "could not remount %s: %m",
25303980ac4fSGarrett Wollman 						    dirp);
25316a09faf2SCraig Rodrigues 					ret = 1;
25326a09faf2SCraig Rodrigues 					goto error_exit;
25338fae3551SRodney W. Grimes 				}
25348fae3551SRodney W. Grimes 				/* back up over the last component */
25358fae3551SRodney W. Grimes 				while (*cp == '/' && cp > dirp)
25368fae3551SRodney W. Grimes 					cp--;
25378fae3551SRodney W. Grimes 				while (*(cp - 1) != '/' && cp > dirp)
25388fae3551SRodney W. Grimes 					cp--;
25398fae3551SRodney W. Grimes 				if (cp == dirp) {
25408fae3551SRodney W. Grimes 					if (debug)
254174853402SPhilippe Charnier 						warnx("mnt unsucc");
2542bcc1d071SRick Macklem 					syslog(LOG_ERR, "can't export %s %s",
2543bcc1d071SRick Macklem 					    dirp, errmsg);
25446a09faf2SCraig Rodrigues 					ret = 1;
25456a09faf2SCraig Rodrigues 					goto error_exit;
25468fae3551SRodney W. Grimes 				}
25478fae3551SRodney W. Grimes 				savedc = *cp;
25488fae3551SRodney W. Grimes 				*cp = '\0';
2549bcc1d071SRick Macklem 				/*
2550bcc1d071SRick Macklem 				 * Check that we're still on the same
2551bcc1d071SRick Macklem 				 * filesystem.
2552bcc1d071SRick Macklem 				 */
2553bcc1d071SRick Macklem 				if (statfs(dirp, &fsb1) != 0 ||
2554bcc1d071SRick Macklem 				    bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2555bcc1d071SRick Macklem 				    sizeof (fsb1.f_fsid)) != 0) {
2556f93caef2SIan Dowse 					*cp = savedc;
2557bcc1d071SRick Macklem 					syslog(LOG_ERR,
2558bcc1d071SRick Macklem 					    "can't export %s %s", dirp,
255937518a88SCraig Rodrigues 					    errmsg);
25606a09faf2SCraig Rodrigues 					ret = 1;
25616a09faf2SCraig Rodrigues 					goto error_exit;
2562f93caef2SIan Dowse 				}
25638fae3551SRodney W. Grimes 			}
2564bcc1d071SRick Macklem 		}
2565bcc1d071SRick Macklem 
2566bcc1d071SRick Macklem 		/*
2567bcc1d071SRick Macklem 		 * For the experimental server:
2568bcc1d071SRick Macklem 		 * If this is the public directory, get the file handle
2569bcc1d071SRick Macklem 		 * and load it into the kernel via the nfssvc() syscall.
2570bcc1d071SRick Macklem 		 */
257179b86807SEdward Tomasz Napierala 		if ((exflags & MNT_EXPUBLIC) != 0) {
2572bcc1d071SRick Macklem 			fhandle_t fh;
2573bcc1d071SRick Macklem 			char *public_name;
2574bcc1d071SRick Macklem 
2575bcc1d071SRick Macklem 			if (eap->ex_indexfile != NULL)
2576bcc1d071SRick Macklem 				public_name = eap->ex_indexfile;
2577bcc1d071SRick Macklem 			else
2578bcc1d071SRick Macklem 				public_name = dirp;
2579bcc1d071SRick Macklem 			if (getfh(public_name, &fh) < 0)
2580bcc1d071SRick Macklem 				syslog(LOG_ERR,
2581bcc1d071SRick Macklem 				    "Can't get public fh for %s", public_name);
2582bcc1d071SRick Macklem 			else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2583bcc1d071SRick Macklem 				syslog(LOG_ERR,
2584bcc1d071SRick Macklem 				    "Can't set public fh for %s", public_name);
2585bcc1d071SRick Macklem 			else
2586bcc1d071SRick Macklem 				has_publicfh = 1;
2587bcc1d071SRick Macklem 		}
25888360efbdSAlfred Perlstein skip:
25896d359f31SIan Dowse 		if (ai != NULL)
25908360efbdSAlfred Perlstein 			ai = ai->ai_next;
25918360efbdSAlfred Perlstein 		if (ai == NULL)
25928fae3551SRodney W. Grimes 			done = TRUE;
25938fae3551SRodney W. Grimes 	}
25948fae3551SRodney W. Grimes 	if (cp)
25958fae3551SRodney W. Grimes 		*cp = savedc;
25966a09faf2SCraig Rodrigues error_exit:
25976a09faf2SCraig Rodrigues 	/* free strings allocated by strdup() in getmntopts.c */
25986a09faf2SCraig Rodrigues 	if (iov != NULL) {
25996a09faf2SCraig Rodrigues 		free(iov[0].iov_base); /* fstype */
26006a09faf2SCraig Rodrigues 		free(iov[2].iov_base); /* fspath */
26016a09faf2SCraig Rodrigues 		free(iov[4].iov_base); /* from */
26026a09faf2SCraig Rodrigues 		free(iov[6].iov_base); /* update */
26036a09faf2SCraig Rodrigues 		free(iov[8].iov_base); /* export */
26046a09faf2SCraig Rodrigues 		free(iov[10].iov_base); /* errmsg */
26056a09faf2SCraig Rodrigues 
26066a09faf2SCraig Rodrigues 		/* free iov, allocated by realloc() */
26076a09faf2SCraig Rodrigues 		free(iov);
26086a09faf2SCraig Rodrigues 	}
26096a09faf2SCraig Rodrigues 	return (ret);
26108fae3551SRodney W. Grimes }
26118fae3551SRodney W. Grimes 
26128fae3551SRodney W. Grimes /*
26138fae3551SRodney W. Grimes  * Translate a net address.
261460caaee2SIan Dowse  *
261560caaee2SIan Dowse  * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
26168fae3551SRodney W. Grimes  */
261719c46d8cSEdward Tomasz Napierala static int
2618a7a7d96cSPhilippe Charnier get_net(char *cp, struct netmsk *net, int maskflg)
26198fae3551SRodney W. Grimes {
2620931c04f1SIan Dowse 	struct netent *np = NULL;
26218360efbdSAlfred Perlstein 	char *name, *p, *prefp;
262260caaee2SIan Dowse 	struct sockaddr_in sin;
2623931c04f1SIan Dowse 	struct sockaddr *sa = NULL;
26248360efbdSAlfred Perlstein 	struct addrinfo hints, *ai = NULL;
26258360efbdSAlfred Perlstein 	char netname[NI_MAXHOST];
26268360efbdSAlfred Perlstein 	long preflen;
26278fae3551SRodney W. Grimes 
262801709abfSIan Dowse 	p = prefp = NULL;
26298360efbdSAlfred Perlstein 	if ((opt_flags & OP_MASKLEN) && !maskflg) {
26308360efbdSAlfred Perlstein 		p = strchr(cp, '/');
26318360efbdSAlfred Perlstein 		*p = '\0';
26328360efbdSAlfred Perlstein 		prefp = p + 1;
26338360efbdSAlfred Perlstein 	}
26348360efbdSAlfred Perlstein 
2635931c04f1SIan Dowse 	/*
2636931c04f1SIan Dowse 	 * Check for a numeric address first. We wish to avoid
2637931c04f1SIan Dowse 	 * possible DNS lookups in getnetbyname().
2638931c04f1SIan Dowse 	 */
2639931c04f1SIan Dowse 	if (isxdigit(*cp) || *cp == ':') {
26408360efbdSAlfred Perlstein 		memset(&hints, 0, sizeof hints);
264160caaee2SIan Dowse 		/* Ensure the mask and the network have the same family. */
264260caaee2SIan Dowse 		if (maskflg && (opt_flags & OP_NET))
264360caaee2SIan Dowse 			hints.ai_family = net->nt_net.ss_family;
264460caaee2SIan Dowse 		else if (!maskflg && (opt_flags & OP_HAVEMASK))
264560caaee2SIan Dowse 			hints.ai_family = net->nt_mask.ss_family;
264660caaee2SIan Dowse 		else
26478360efbdSAlfred Perlstein 			hints.ai_family = AF_UNSPEC;
26488360efbdSAlfred Perlstein 		hints.ai_flags = AI_NUMERICHOST;
2649931c04f1SIan Dowse 		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2650931c04f1SIan Dowse 			sa = ai->ai_addr;
2651931c04f1SIan Dowse 		if (sa != NULL && ai->ai_family == AF_INET) {
26528fae3551SRodney W. Grimes 			/*
265360caaee2SIan Dowse 			 * The address in `cp' is really a network address, so
265460caaee2SIan Dowse 			 * use inet_network() to re-interpret this correctly.
265560caaee2SIan Dowse 			 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
26568fae3551SRodney W. Grimes 			 */
265760caaee2SIan Dowse 			bzero(&sin, sizeof sin);
26588360efbdSAlfred Perlstein 			sin.sin_family = AF_INET;
26598360efbdSAlfred Perlstein 			sin.sin_len = sizeof sin;
26608360efbdSAlfred Perlstein 			sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
26618360efbdSAlfred Perlstein 			if (debug)
266260caaee2SIan Dowse 				fprintf(stderr, "get_net: v4 addr %s\n",
266360caaee2SIan Dowse 				    inet_ntoa(sin.sin_addr));
26648360efbdSAlfred Perlstein 			sa = (struct sockaddr *)&sin;
2665931c04f1SIan Dowse 		}
2666931c04f1SIan Dowse 	}
2667931c04f1SIan Dowse 	if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2668931c04f1SIan Dowse 		bzero(&sin, sizeof sin);
2669931c04f1SIan Dowse 		sin.sin_family = AF_INET;
2670931c04f1SIan Dowse 		sin.sin_len = sizeof sin;
2671931c04f1SIan Dowse 		sin.sin_addr = inet_makeaddr(np->n_net, 0);
2672931c04f1SIan Dowse 		sa = (struct sockaddr *)&sin;
2673931c04f1SIan Dowse 	}
2674931c04f1SIan Dowse 	if (sa == NULL)
26758360efbdSAlfred Perlstein 		goto fail;
26768360efbdSAlfred Perlstein 
267760caaee2SIan Dowse 	if (maskflg) {
267860caaee2SIan Dowse 		/* The specified sockaddr is a mask. */
267960caaee2SIan Dowse 		if (checkmask(sa) != 0)
26808360efbdSAlfred Perlstein 			goto fail;
268160caaee2SIan Dowse 		bcopy(sa, &net->nt_mask, sa->sa_len);
268260caaee2SIan Dowse 		opt_flags |= OP_HAVEMASK;
268360caaee2SIan Dowse 	} else {
268460caaee2SIan Dowse 		/* The specified sockaddr is a network address. */
268560caaee2SIan Dowse 		bcopy(sa, &net->nt_net, sa->sa_len);
26860f4b7baaSPaul Traina 
268760caaee2SIan Dowse 		/* Get a network name for the export list. */
268860caaee2SIan Dowse 		if (np) {
268960caaee2SIan Dowse 			name = np->n_name;
269060caaee2SIan Dowse 		} else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
26914f101318SHajimu UMEMOTO 		   NULL, 0, NI_NUMERICHOST) == 0) {
269260caaee2SIan Dowse 			name = netname;
269360caaee2SIan Dowse 		} else {
269460caaee2SIan Dowse 			goto fail;
269560caaee2SIan Dowse 		}
269660caaee2SIan Dowse 		if ((net->nt_name = strdup(name)) == NULL)
269760caaee2SIan Dowse 			out_of_mem();
269860caaee2SIan Dowse 
269960caaee2SIan Dowse 		/*
270060caaee2SIan Dowse 		 * Extract a mask from either a "/<masklen>" suffix, or
270160caaee2SIan Dowse 		 * from the class of an IPv4 address.
270260caaee2SIan Dowse 		 */
27038360efbdSAlfred Perlstein 		if (opt_flags & OP_MASKLEN) {
27048360efbdSAlfred Perlstein 			preflen = strtol(prefp, NULL, 10);
270560caaee2SIan Dowse 			if (preflen < 0L || preflen == LONG_MAX)
27068360efbdSAlfred Perlstein 				goto fail;
270760caaee2SIan Dowse 			bcopy(sa, &net->nt_mask, sa->sa_len);
270860caaee2SIan Dowse 			if (makemask(&net->nt_mask, (int)preflen) != 0)
270960caaee2SIan Dowse 				goto fail;
271060caaee2SIan Dowse 			opt_flags |= OP_HAVEMASK;
27118360efbdSAlfred Perlstein 			*p = '/';
271260caaee2SIan Dowse 		} else if (sa->sa_family == AF_INET &&
271360caaee2SIan Dowse 		    (opt_flags & OP_MASK) == 0) {
271460caaee2SIan Dowse 			in_addr_t addr;
27158360efbdSAlfred Perlstein 
271660caaee2SIan Dowse 			addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
271760caaee2SIan Dowse 			if (IN_CLASSA(addr))
271860caaee2SIan Dowse 				preflen = 8;
271960caaee2SIan Dowse 			else if (IN_CLASSB(addr))
272060caaee2SIan Dowse 				preflen = 16;
272160caaee2SIan Dowse 			else if (IN_CLASSC(addr))
272260caaee2SIan Dowse 				preflen = 24;
272360caaee2SIan Dowse 			else if (IN_CLASSD(addr))
272460caaee2SIan Dowse 				preflen = 28;
27258360efbdSAlfred Perlstein 			else
272660caaee2SIan Dowse 				preflen = 32;	/* XXX */
272760caaee2SIan Dowse 
272860caaee2SIan Dowse 			bcopy(sa, &net->nt_mask, sa->sa_len);
272960caaee2SIan Dowse 			makemask(&net->nt_mask, (int)preflen);
273060caaee2SIan Dowse 			opt_flags |= OP_HAVEMASK;
273160caaee2SIan Dowse 		}
27328360efbdSAlfred Perlstein 	}
27338360efbdSAlfred Perlstein 
27348360efbdSAlfred Perlstein 	if (ai)
27358360efbdSAlfred Perlstein 		freeaddrinfo(ai);
27368360efbdSAlfred Perlstein 	return 0;
27378360efbdSAlfred Perlstein 
27388360efbdSAlfred Perlstein fail:
27398360efbdSAlfred Perlstein 	if (ai)
27408360efbdSAlfred Perlstein 		freeaddrinfo(ai);
27418360efbdSAlfred Perlstein 	return 1;
27428fae3551SRodney W. Grimes }
27438fae3551SRodney W. Grimes 
27448fae3551SRodney W. Grimes /*
27458fae3551SRodney W. Grimes  * Parse out the next white space separated field
27468fae3551SRodney W. Grimes  */
274719c46d8cSEdward Tomasz Napierala static void
2748a7a7d96cSPhilippe Charnier nextfield(char **cp, char **endcp)
27498fae3551SRodney W. Grimes {
27508fae3551SRodney W. Grimes 	char *p;
27518fae3551SRodney W. Grimes 
27528fae3551SRodney W. Grimes 	p = *cp;
27538fae3551SRodney W. Grimes 	while (*p == ' ' || *p == '\t')
27548fae3551SRodney W. Grimes 		p++;
27558fae3551SRodney W. Grimes 	if (*p == '\n' || *p == '\0')
27568fae3551SRodney W. Grimes 		*cp = *endcp = p;
27578fae3551SRodney W. Grimes 	else {
27588fae3551SRodney W. Grimes 		*cp = p++;
27598fae3551SRodney W. Grimes 		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
27608fae3551SRodney W. Grimes 			p++;
27618fae3551SRodney W. Grimes 		*endcp = p;
27628fae3551SRodney W. Grimes 	}
27638fae3551SRodney W. Grimes }
27648fae3551SRodney W. Grimes 
27658fae3551SRodney W. Grimes /*
27668fae3551SRodney W. Grimes  * Get an exports file line. Skip over blank lines and handle line
27678fae3551SRodney W. Grimes  * continuations.
27688fae3551SRodney W. Grimes  */
276919c46d8cSEdward Tomasz Napierala static int
2770a7a7d96cSPhilippe Charnier get_line(void)
27718fae3551SRodney W. Grimes {
27728fae3551SRodney W. Grimes 	char *p, *cp;
277391ca1a91SIan Dowse 	size_t len;
27748fae3551SRodney W. Grimes 	int totlen, cont_line;
27758fae3551SRodney W. Grimes 
27768fae3551SRodney W. Grimes 	/*
27778fae3551SRodney W. Grimes 	 * Loop around ignoring blank lines and getting all continuation lines.
27788fae3551SRodney W. Grimes 	 */
27798fae3551SRodney W. Grimes 	p = line;
27808fae3551SRodney W. Grimes 	totlen = 0;
27818fae3551SRodney W. Grimes 	do {
278291ca1a91SIan Dowse 		if ((p = fgetln(exp_file, &len)) == NULL)
27838fae3551SRodney W. Grimes 			return (0);
27848fae3551SRodney W. Grimes 		cp = p + len - 1;
27858fae3551SRodney W. Grimes 		cont_line = 0;
27868fae3551SRodney W. Grimes 		while (cp >= p &&
27878fae3551SRodney W. Grimes 		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
27888fae3551SRodney W. Grimes 			if (*cp == '\\')
27898fae3551SRodney W. Grimes 				cont_line = 1;
27908fae3551SRodney W. Grimes 			cp--;
27918fae3551SRodney W. Grimes 			len--;
27928fae3551SRodney W. Grimes 		}
2793376f8390SDima Dorfman 		if (cont_line) {
2794376f8390SDima Dorfman 			*++cp = ' ';
2795376f8390SDima Dorfman 			len++;
2796376f8390SDima Dorfman 		}
279791ca1a91SIan Dowse 		if (linesize < len + totlen + 1) {
279891ca1a91SIan Dowse 			linesize = len + totlen + 1;
279991ca1a91SIan Dowse 			line = realloc(line, linesize);
280091ca1a91SIan Dowse 			if (line == NULL)
280191ca1a91SIan Dowse 				out_of_mem();
280291ca1a91SIan Dowse 		}
280391ca1a91SIan Dowse 		memcpy(line + totlen, p, len);
28048fae3551SRodney W. Grimes 		totlen += len;
280591ca1a91SIan Dowse 		line[totlen] = '\0';
28068fae3551SRodney W. Grimes 	} while (totlen == 0 || cont_line);
28078fae3551SRodney W. Grimes 	return (1);
28088fae3551SRodney W. Grimes }
28098fae3551SRodney W. Grimes 
28108fae3551SRodney W. Grimes /*
28118fae3551SRodney W. Grimes  * Parse a description of a credential.
28128fae3551SRodney W. Grimes  */
281319c46d8cSEdward Tomasz Napierala static void
2814a7a7d96cSPhilippe Charnier parsecred(char *namelist, struct xucred *cr)
28158fae3551SRodney W. Grimes {
28168fae3551SRodney W. Grimes 	char *name;
28178fae3551SRodney W. Grimes 	int cnt;
28188fae3551SRodney W. Grimes 	char *names;
28198fae3551SRodney W. Grimes 	struct passwd *pw;
28208fae3551SRodney W. Grimes 	struct group *gr;
2821838d9858SBrooks Davis 	gid_t groups[XU_NGROUPS + 1];
2822950cc395SStefan Farfeleder 	int ngroups;
28238fae3551SRodney W. Grimes 
282476183f34SDima Dorfman 	cr->cr_version = XUCRED_VERSION;
28258fae3551SRodney W. Grimes 	/*
282674853402SPhilippe Charnier 	 * Set up the unprivileged user.
28278fae3551SRodney W. Grimes 	 */
28288fae3551SRodney W. Grimes 	cr->cr_uid = -2;
28298fae3551SRodney W. Grimes 	cr->cr_groups[0] = -2;
28308fae3551SRodney W. Grimes 	cr->cr_ngroups = 1;
28318fae3551SRodney W. Grimes 	/*
28328fae3551SRodney W. Grimes 	 * Get the user's password table entry.
28338fae3551SRodney W. Grimes 	 */
28348fae3551SRodney W. Grimes 	names = strsep(&namelist, " \t\n");
28358fae3551SRodney W. Grimes 	name = strsep(&names, ":");
28368fae3551SRodney W. Grimes 	if (isdigit(*name) || *name == '-')
28378fae3551SRodney W. Grimes 		pw = getpwuid(atoi(name));
28388fae3551SRodney W. Grimes 	else
28398fae3551SRodney W. Grimes 		pw = getpwnam(name);
28408fae3551SRodney W. Grimes 	/*
28418fae3551SRodney W. Grimes 	 * Credentials specified as those of a user.
28428fae3551SRodney W. Grimes 	 */
28438fae3551SRodney W. Grimes 	if (names == NULL) {
28448fae3551SRodney W. Grimes 		if (pw == NULL) {
284574853402SPhilippe Charnier 			syslog(LOG_ERR, "unknown user: %s", name);
28468fae3551SRodney W. Grimes 			return;
28478fae3551SRodney W. Grimes 		}
28488fae3551SRodney W. Grimes 		cr->cr_uid = pw->pw_uid;
2849838d9858SBrooks Davis 		ngroups = XU_NGROUPS + 1;
28508fae3551SRodney W. Grimes 		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
285174853402SPhilippe Charnier 			syslog(LOG_ERR, "too many groups");
28528fae3551SRodney W. Grimes 		/*
2853950cc395SStefan Farfeleder 		 * Compress out duplicate.
28548fae3551SRodney W. Grimes 		 */
28558fae3551SRodney W. Grimes 		cr->cr_ngroups = ngroups - 1;
28568fae3551SRodney W. Grimes 		cr->cr_groups[0] = groups[0];
28578fae3551SRodney W. Grimes 		for (cnt = 2; cnt < ngroups; cnt++)
28588fae3551SRodney W. Grimes 			cr->cr_groups[cnt - 1] = groups[cnt];
28598fae3551SRodney W. Grimes 		return;
28608fae3551SRodney W. Grimes 	}
28618fae3551SRodney W. Grimes 	/*
28628fae3551SRodney W. Grimes 	 * Explicit credential specified as a colon separated list:
28638fae3551SRodney W. Grimes 	 *	uid:gid:gid:...
28648fae3551SRodney W. Grimes 	 */
28658fae3551SRodney W. Grimes 	if (pw != NULL)
28668fae3551SRodney W. Grimes 		cr->cr_uid = pw->pw_uid;
28678fae3551SRodney W. Grimes 	else if (isdigit(*name) || *name == '-')
28688fae3551SRodney W. Grimes 		cr->cr_uid = atoi(name);
28698fae3551SRodney W. Grimes 	else {
287074853402SPhilippe Charnier 		syslog(LOG_ERR, "unknown user: %s", name);
28718fae3551SRodney W. Grimes 		return;
28728fae3551SRodney W. Grimes 	}
28738fae3551SRodney W. Grimes 	cr->cr_ngroups = 0;
2874838d9858SBrooks Davis 	while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
28758fae3551SRodney W. Grimes 		name = strsep(&names, ":");
28768fae3551SRodney W. Grimes 		if (isdigit(*name) || *name == '-') {
28778fae3551SRodney W. Grimes 			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
28788fae3551SRodney W. Grimes 		} else {
28798fae3551SRodney W. Grimes 			if ((gr = getgrnam(name)) == NULL) {
288074853402SPhilippe Charnier 				syslog(LOG_ERR, "unknown group: %s", name);
28818fae3551SRodney W. Grimes 				continue;
28828fae3551SRodney W. Grimes 			}
28838fae3551SRodney W. Grimes 			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
28848fae3551SRodney W. Grimes 		}
28858fae3551SRodney W. Grimes 	}
2886838d9858SBrooks Davis 	if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
288774853402SPhilippe Charnier 		syslog(LOG_ERR, "too many groups");
28888fae3551SRodney W. Grimes }
28898fae3551SRodney W. Grimes 
28900775314bSDoug Rabson #define	STRSIZ	(MNTNAMLEN+MNTPATHLEN+50)
28918fae3551SRodney W. Grimes /*
28928fae3551SRodney W. Grimes  * Routines that maintain the remote mounttab
28938fae3551SRodney W. Grimes  */
289419c46d8cSEdward Tomasz Napierala static void
2895a7a7d96cSPhilippe Charnier get_mountlist(void)
28968fae3551SRodney W. Grimes {
28978fae3551SRodney W. Grimes 	struct mountlist *mlp, **mlpp;
289887564113SPeter Wemm 	char *host, *dirp, *cp;
28998fae3551SRodney W. Grimes 	char str[STRSIZ];
29008fae3551SRodney W. Grimes 	FILE *mlfile;
29018fae3551SRodney W. Grimes 
29028fae3551SRodney W. Grimes 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
290339539916SBill Fumerola 		if (errno == ENOENT)
290439539916SBill Fumerola 			return;
290539539916SBill Fumerola 		else {
290674853402SPhilippe Charnier 			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
29078fae3551SRodney W. Grimes 			return;
29088fae3551SRodney W. Grimes 		}
290939539916SBill Fumerola 	}
29108fae3551SRodney W. Grimes 	mlpp = &mlhead;
29118fae3551SRodney W. Grimes 	while (fgets(str, STRSIZ, mlfile) != NULL) {
291287564113SPeter Wemm 		cp = str;
291387564113SPeter Wemm 		host = strsep(&cp, " \t\n");
291487564113SPeter Wemm 		dirp = strsep(&cp, " \t\n");
291587564113SPeter Wemm 		if (host == NULL || dirp == NULL)
29168fae3551SRodney W. Grimes 			continue;
29178fae3551SRodney W. Grimes 		mlp = (struct mountlist *)malloc(sizeof (*mlp));
291874853402SPhilippe Charnier 		if (mlp == (struct mountlist *)NULL)
291974853402SPhilippe Charnier 			out_of_mem();
29200775314bSDoug Rabson 		strncpy(mlp->ml_host, host, MNTNAMLEN);
29210775314bSDoug Rabson 		mlp->ml_host[MNTNAMLEN] = '\0';
29220775314bSDoug Rabson 		strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
29230775314bSDoug Rabson 		mlp->ml_dirp[MNTPATHLEN] = '\0';
29248fae3551SRodney W. Grimes 		mlp->ml_next = (struct mountlist *)NULL;
29258fae3551SRodney W. Grimes 		*mlpp = mlp;
29268fae3551SRodney W. Grimes 		mlpp = &mlp->ml_next;
29278fae3551SRodney W. Grimes 	}
29288fae3551SRodney W. Grimes 	fclose(mlfile);
29298fae3551SRodney W. Grimes }
29308fae3551SRodney W. Grimes 
293119c46d8cSEdward Tomasz Napierala static void
293201709abfSIan Dowse del_mlist(char *hostp, char *dirp)
29338fae3551SRodney W. Grimes {
29348fae3551SRodney W. Grimes 	struct mountlist *mlp, **mlpp;
29358fae3551SRodney W. Grimes 	struct mountlist *mlp2;
29368fae3551SRodney W. Grimes 	FILE *mlfile;
29378fae3551SRodney W. Grimes 	int fnd = 0;
29388fae3551SRodney W. Grimes 
29398fae3551SRodney W. Grimes 	mlpp = &mlhead;
29408fae3551SRodney W. Grimes 	mlp = mlhead;
29418fae3551SRodney W. Grimes 	while (mlp) {
29428fae3551SRodney W. Grimes 		if (!strcmp(mlp->ml_host, hostp) &&
29438fae3551SRodney W. Grimes 		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
29448fae3551SRodney W. Grimes 			fnd = 1;
29458fae3551SRodney W. Grimes 			mlp2 = mlp;
29468fae3551SRodney W. Grimes 			*mlpp = mlp = mlp->ml_next;
29478fae3551SRodney W. Grimes 			free((caddr_t)mlp2);
29488fae3551SRodney W. Grimes 		} else {
29498fae3551SRodney W. Grimes 			mlpp = &mlp->ml_next;
29508fae3551SRodney W. Grimes 			mlp = mlp->ml_next;
29518fae3551SRodney W. Grimes 		}
29528fae3551SRodney W. Grimes 	}
29538fae3551SRodney W. Grimes 	if (fnd) {
29548fae3551SRodney W. Grimes 		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
295574853402SPhilippe Charnier 			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
29568fae3551SRodney W. Grimes 			return;
29578fae3551SRodney W. Grimes 		}
29588fae3551SRodney W. Grimes 		mlp = mlhead;
29598fae3551SRodney W. Grimes 		while (mlp) {
29608fae3551SRodney W. Grimes 			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
29618fae3551SRodney W. Grimes 			mlp = mlp->ml_next;
29628fae3551SRodney W. Grimes 		}
29638fae3551SRodney W. Grimes 		fclose(mlfile);
29648fae3551SRodney W. Grimes 	}
29658fae3551SRodney W. Grimes }
29668fae3551SRodney W. Grimes 
296719c46d8cSEdward Tomasz Napierala static void
2968a7a7d96cSPhilippe Charnier add_mlist(char *hostp, char *dirp)
29698fae3551SRodney W. Grimes {
29708fae3551SRodney W. Grimes 	struct mountlist *mlp, **mlpp;
29718fae3551SRodney W. Grimes 	FILE *mlfile;
29728fae3551SRodney W. Grimes 
29738fae3551SRodney W. Grimes 	mlpp = &mlhead;
29748fae3551SRodney W. Grimes 	mlp = mlhead;
29758fae3551SRodney W. Grimes 	while (mlp) {
29768fae3551SRodney W. Grimes 		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
29778fae3551SRodney W. Grimes 			return;
29788fae3551SRodney W. Grimes 		mlpp = &mlp->ml_next;
29798fae3551SRodney W. Grimes 		mlp = mlp->ml_next;
29808fae3551SRodney W. Grimes 	}
29818fae3551SRodney W. Grimes 	mlp = (struct mountlist *)malloc(sizeof (*mlp));
298274853402SPhilippe Charnier 	if (mlp == (struct mountlist *)NULL)
298374853402SPhilippe Charnier 		out_of_mem();
29840775314bSDoug Rabson 	strncpy(mlp->ml_host, hostp, MNTNAMLEN);
29850775314bSDoug Rabson 	mlp->ml_host[MNTNAMLEN] = '\0';
29860775314bSDoug Rabson 	strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
29870775314bSDoug Rabson 	mlp->ml_dirp[MNTPATHLEN] = '\0';
29888fae3551SRodney W. Grimes 	mlp->ml_next = (struct mountlist *)NULL;
29898fae3551SRodney W. Grimes 	*mlpp = mlp;
29908fae3551SRodney W. Grimes 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
299174853402SPhilippe Charnier 		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
29928fae3551SRodney W. Grimes 		return;
29938fae3551SRodney W. Grimes 	}
29948fae3551SRodney W. Grimes 	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
29958fae3551SRodney W. Grimes 	fclose(mlfile);
29968fae3551SRodney W. Grimes }
29978fae3551SRodney W. Grimes 
29988fae3551SRodney W. Grimes /*
29998fae3551SRodney W. Grimes  * Free up a group list.
30008fae3551SRodney W. Grimes  */
300119c46d8cSEdward Tomasz Napierala static void
3002a7a7d96cSPhilippe Charnier free_grp(struct grouplist *grp)
30038fae3551SRodney W. Grimes {
30048fae3551SRodney W. Grimes 	if (grp->gr_type == GT_HOST) {
30058360efbdSAlfred Perlstein 		if (grp->gr_ptr.gt_addrinfo != NULL)
30068360efbdSAlfred Perlstein 			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
30078fae3551SRodney W. Grimes 	} else if (grp->gr_type == GT_NET) {
30088fae3551SRodney W. Grimes 		if (grp->gr_ptr.gt_net.nt_name)
30098fae3551SRodney W. Grimes 			free(grp->gr_ptr.gt_net.nt_name);
30108fae3551SRodney W. Grimes 	}
30118fae3551SRodney W. Grimes 	free((caddr_t)grp);
30128fae3551SRodney W. Grimes }
30138fae3551SRodney W. Grimes 
30148fae3551SRodney W. Grimes #ifdef DEBUG
301519c46d8cSEdward Tomasz Napierala static void
30168fae3551SRodney W. Grimes SYSLOG(int pri, const char *fmt, ...)
30178fae3551SRodney W. Grimes {
30188fae3551SRodney W. Grimes 	va_list ap;
30198fae3551SRodney W. Grimes 
30208fae3551SRodney W. Grimes 	va_start(ap, fmt);
30218fae3551SRodney W. Grimes 	vfprintf(stderr, fmt, ap);
30228fae3551SRodney W. Grimes 	va_end(ap);
30238fae3551SRodney W. Grimes }
30248fae3551SRodney W. Grimes #endif /* DEBUG */
30258fae3551SRodney W. Grimes 
30268fae3551SRodney W. Grimes /*
30278fae3551SRodney W. Grimes  * Check options for consistency.
30288fae3551SRodney W. Grimes  */
302919c46d8cSEdward Tomasz Napierala static int
3030a7a7d96cSPhilippe Charnier check_options(struct dirlist *dp)
30318fae3551SRodney W. Grimes {
30328fae3551SRodney W. Grimes 
3033bcc1d071SRick Macklem 	if (v4root_phase == 0 && dp == NULL)
30348fae3551SRodney W. Grimes 	    return (1);
303591196234SPeter Wemm 	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
303691196234SPeter Wemm 	    syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
30378fae3551SRodney W. Grimes 	    return (1);
30388fae3551SRodney W. Grimes 	}
30398fae3551SRodney W. Grimes 	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
304060caaee2SIan Dowse 		syslog(LOG_ERR, "-mask requires -network");
304160caaee2SIan Dowse 		return (1);
304260caaee2SIan Dowse 	}
304360caaee2SIan Dowse 	if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
304460caaee2SIan Dowse 		syslog(LOG_ERR, "-network requires mask specification");
304560caaee2SIan Dowse 		return (1);
304660caaee2SIan Dowse 	}
304760caaee2SIan Dowse 	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
304860caaee2SIan Dowse 		syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
30498fae3551SRodney W. Grimes 		return (1);
30508fae3551SRodney W. Grimes 	}
3051bcc1d071SRick Macklem 	if (v4root_phase > 0 &&
3052bcc1d071SRick Macklem 	    (opt_flags &
3053bcc1d071SRick Macklem 	     ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3054bcc1d071SRick Macklem 	    syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3055bcc1d071SRick Macklem 	    return (1);
3056bcc1d071SRick Macklem 	}
305756cfc5edSRick Macklem 	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
305856cfc5edSRick Macklem 	    syslog(LOG_ERR, "-alldirs has multiple directories");
305956cfc5edSRick Macklem 	    return (1);
306056cfc5edSRick Macklem 	}
30618fae3551SRodney W. Grimes 	return (0);
30628fae3551SRodney W. Grimes }
30638fae3551SRodney W. Grimes 
30648fae3551SRodney W. Grimes /*
30658fae3551SRodney W. Grimes  * Check an absolute directory path for any symbolic links. Return true
30668fae3551SRodney W. Grimes  */
306719c46d8cSEdward Tomasz Napierala static int
3068a7a7d96cSPhilippe Charnier check_dirpath(char *dirp)
30698fae3551SRodney W. Grimes {
30708fae3551SRodney W. Grimes 	char *cp;
30718fae3551SRodney W. Grimes 	int ret = 1;
30728fae3551SRodney W. Grimes 	struct stat sb;
30738fae3551SRodney W. Grimes 
30748fae3551SRodney W. Grimes 	cp = dirp + 1;
30758fae3551SRodney W. Grimes 	while (*cp && ret) {
30768fae3551SRodney W. Grimes 		if (*cp == '/') {
30778fae3551SRodney W. Grimes 			*cp = '\0';
3078a62dc406SDoug Rabson 			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
30798fae3551SRodney W. Grimes 				ret = 0;
30808fae3551SRodney W. Grimes 			*cp = '/';
30818fae3551SRodney W. Grimes 		}
30828fae3551SRodney W. Grimes 		cp++;
30838fae3551SRodney W. Grimes 	}
3084a62dc406SDoug Rabson 	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
30858fae3551SRodney W. Grimes 		ret = 0;
30868fae3551SRodney W. Grimes 	return (ret);
30878fae3551SRodney W. Grimes }
3088a62dc406SDoug Rabson 
308960caaee2SIan Dowse /*
309060caaee2SIan Dowse  * Make a netmask according to the specified prefix length. The ss_family
309160caaee2SIan Dowse  * and other non-address fields must be initialised before calling this.
309260caaee2SIan Dowse  */
309319c46d8cSEdward Tomasz Napierala static int
309460caaee2SIan Dowse makemask(struct sockaddr_storage *ssp, int bitlen)
30958360efbdSAlfred Perlstein {
309660caaee2SIan Dowse 	u_char *p;
309760caaee2SIan Dowse 	int bits, i, len;
30988360efbdSAlfred Perlstein 
309960caaee2SIan Dowse 	if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
310060caaee2SIan Dowse 		return (-1);
310189fdc4e1SMike Barcroft 	if (bitlen > len * CHAR_BIT)
310260caaee2SIan Dowse 		return (-1);
31038360efbdSAlfred Perlstein 
310460caaee2SIan Dowse 	for (i = 0; i < len; i++) {
310589fdc4e1SMike Barcroft 		bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
310658202d89SRuslan Ermilov 		*p++ = (u_char)~0 << (CHAR_BIT - bits);
310760caaee2SIan Dowse 		bitlen -= bits;
31088360efbdSAlfred Perlstein 	}
31098360efbdSAlfred Perlstein 	return 0;
31108360efbdSAlfred Perlstein }
31118360efbdSAlfred Perlstein 
311260caaee2SIan Dowse /*
311360caaee2SIan Dowse  * Check that the sockaddr is a valid netmask. Returns 0 if the mask
311460caaee2SIan Dowse  * is acceptable (i.e. of the form 1...10....0).
311560caaee2SIan Dowse  */
311619c46d8cSEdward Tomasz Napierala static int
311760caaee2SIan Dowse checkmask(struct sockaddr *sa)
31188360efbdSAlfred Perlstein {
311960caaee2SIan Dowse 	u_char *mask;
312060caaee2SIan Dowse 	int i, len;
312160caaee2SIan Dowse 
312260caaee2SIan Dowse 	if ((mask = sa_rawaddr(sa, &len)) == NULL)
312360caaee2SIan Dowse 		return (-1);
312460caaee2SIan Dowse 
312560caaee2SIan Dowse 	for (i = 0; i < len; i++)
312660caaee2SIan Dowse 		if (mask[i] != 0xff)
312760caaee2SIan Dowse 			break;
312860caaee2SIan Dowse 	if (i < len) {
312960caaee2SIan Dowse 		if (~mask[i] & (u_char)(~mask[i] + 1))
313060caaee2SIan Dowse 			return (-1);
313160caaee2SIan Dowse 		i++;
313260caaee2SIan Dowse 	}
313360caaee2SIan Dowse 	for (; i < len; i++)
313460caaee2SIan Dowse 		if (mask[i] != 0)
313560caaee2SIan Dowse 			return (-1);
313660caaee2SIan Dowse 	return (0);
313760caaee2SIan Dowse }
313860caaee2SIan Dowse 
313960caaee2SIan Dowse /*
314060caaee2SIan Dowse  * Compare two sockaddrs according to a specified mask. Return zero if
314160caaee2SIan Dowse  * `sa1' matches `sa2' when filtered by the netmask in `samask'.
31423df5ecacSUlrich Spörlein  * If samask is NULL, perform a full comparison.
314360caaee2SIan Dowse  */
314419c46d8cSEdward Tomasz Napierala static int
314560caaee2SIan Dowse sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
314660caaee2SIan Dowse {
314760caaee2SIan Dowse 	unsigned char *p1, *p2, *mask;
314860caaee2SIan Dowse 	int len, i;
314960caaee2SIan Dowse 
315060caaee2SIan Dowse 	if (sa1->sa_family != sa2->sa_family ||
315160caaee2SIan Dowse 	    (p1 = sa_rawaddr(sa1, &len)) == NULL ||
315260caaee2SIan Dowse 	    (p2 = sa_rawaddr(sa2, NULL)) == NULL)
315360caaee2SIan Dowse 		return (1);
315460caaee2SIan Dowse 
315560caaee2SIan Dowse 	switch (sa1->sa_family) {
315660caaee2SIan Dowse 	case AF_INET6:
315760caaee2SIan Dowse 		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
315860caaee2SIan Dowse 		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
315960caaee2SIan Dowse 			return (1);
316060caaee2SIan Dowse 		break;
316160caaee2SIan Dowse 	}
316260caaee2SIan Dowse 
316360caaee2SIan Dowse 	/* Simple binary comparison if no mask specified. */
316460caaee2SIan Dowse 	if (samask == NULL)
316560caaee2SIan Dowse 		return (memcmp(p1, p2, len));
316660caaee2SIan Dowse 
316760caaee2SIan Dowse 	/* Set up the mask, and do a mask-based comparison. */
316860caaee2SIan Dowse 	if (sa1->sa_family != samask->sa_family ||
316960caaee2SIan Dowse 	    (mask = sa_rawaddr(samask, NULL)) == NULL)
317060caaee2SIan Dowse 		return (1);
317160caaee2SIan Dowse 
317260caaee2SIan Dowse 	for (i = 0; i < len; i++)
317360caaee2SIan Dowse 		if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
317460caaee2SIan Dowse 			return (1);
317560caaee2SIan Dowse 	return (0);
317660caaee2SIan Dowse }
317760caaee2SIan Dowse 
317860caaee2SIan Dowse /*
317960caaee2SIan Dowse  * Return a pointer to the part of the sockaddr that contains the
318060caaee2SIan Dowse  * raw address, and set *nbytes to its length in bytes. Returns
318160caaee2SIan Dowse  * NULL if the address family is unknown.
318260caaee2SIan Dowse  */
318319c46d8cSEdward Tomasz Napierala static void *
318460caaee2SIan Dowse sa_rawaddr(struct sockaddr *sa, int *nbytes) {
318560caaee2SIan Dowse 	void *p;
318660caaee2SIan Dowse 	int len;
31878360efbdSAlfred Perlstein 
31888360efbdSAlfred Perlstein 	switch (sa->sa_family) {
31898360efbdSAlfred Perlstein 	case AF_INET:
319060caaee2SIan Dowse 		len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
319160caaee2SIan Dowse 		p = &((struct sockaddr_in *)sa)->sin_addr;
31928360efbdSAlfred Perlstein 		break;
31938360efbdSAlfred Perlstein 	case AF_INET6:
319460caaee2SIan Dowse 		len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
319560caaee2SIan Dowse 		p = &((struct sockaddr_in6 *)sa)->sin6_addr;
31968360efbdSAlfred Perlstein 		break;
31978360efbdSAlfred Perlstein 	default:
319860caaee2SIan Dowse 		p = NULL;
319960caaee2SIan Dowse 		len = 0;
32008360efbdSAlfred Perlstein 	}
32018360efbdSAlfred Perlstein 
320260caaee2SIan Dowse 	if (nbytes != NULL)
320360caaee2SIan Dowse 		*nbytes = len;
320460caaee2SIan Dowse 	return (p);
32058360efbdSAlfred Perlstein }
32068360efbdSAlfred Perlstein 
320719c46d8cSEdward Tomasz Napierala static void
3208a7a7d96cSPhilippe Charnier huphandler(int sig __unused)
320969d65572SIan Dowse {
321019c46d8cSEdward Tomasz Napierala 
321169d65572SIan Dowse 	got_sighup = 1;
321269d65572SIan Dowse }
321369d65572SIan Dowse 
321419c46d8cSEdward Tomasz Napierala static void
321519c46d8cSEdward Tomasz Napierala terminate(int sig __unused)
32168360efbdSAlfred Perlstein {
3217a032b226SPawel Jakub Dawidek 	pidfile_remove(pfh);
32180775314bSDoug Rabson 	rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
32190775314bSDoug Rabson 	rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
32208360efbdSAlfred Perlstein 	exit (0);
32218360efbdSAlfred Perlstein }
3222