xref: /freebsd/usr.sbin/mountd/mountd.c (revision 89b859e39add7404330a64dc26019791985af9cd)
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.
16fbbd9655SWarner Losh  * 3. 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 */
104380a3fcdSEmmanuel Vadot 	char		*dp_dirp;
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 **);
177b875c2e9SJosh Paetzel static char	*strsep_quote(char **stringp, const char *delim);
1788fb6ad5dSRick Macklem static int	create_service(struct netconfig *nconf);
1798fb6ad5dSRick Macklem static void	complete_service(struct netconfig *nconf, char *port_str);
1808fb6ad5dSRick Macklem static void	clearout_service(void);
18119c46d8cSEdward Tomasz Napierala static void	del_mlist(char *hostp, char *dirp);
18219c46d8cSEdward Tomasz Napierala static struct dirlist	*dirp_search(struct dirlist *, char *);
18319c46d8cSEdward Tomasz Napierala static int	do_mount(struct exportlist *, struct grouplist *, int,
18485429990SWarner Losh 		    struct xucred *, char *, int, struct statfs *);
18519c46d8cSEdward Tomasz Napierala static int	do_opt(char **, char **, struct exportlist *,
18619c46d8cSEdward Tomasz Napierala 		    struct grouplist *, int *, int *, struct xucred *);
18719c46d8cSEdward Tomasz Napierala static struct exportlist	*ex_search(fsid_t *);
18819c46d8cSEdward Tomasz Napierala static struct exportlist	*get_exp(void);
18919c46d8cSEdward Tomasz Napierala static void	free_dir(struct dirlist *);
19019c46d8cSEdward Tomasz Napierala static void	free_exp(struct exportlist *);
19119c46d8cSEdward Tomasz Napierala static void	free_grp(struct grouplist *);
19219c46d8cSEdward Tomasz Napierala static void	free_host(struct hostlist *);
19319c46d8cSEdward Tomasz Napierala static void	get_exportlist(void);
19419c46d8cSEdward Tomasz Napierala static int	get_host(char *, struct grouplist *, struct grouplist *);
19519c46d8cSEdward Tomasz Napierala static struct hostlist *get_ht(void);
19619c46d8cSEdward Tomasz Napierala static int	get_line(void);
19719c46d8cSEdward Tomasz Napierala static void	get_mountlist(void);
19819c46d8cSEdward Tomasz Napierala static int	get_net(char *, struct netmsk *, int);
19919c46d8cSEdward Tomasz Napierala static void	getexp_err(struct exportlist *, struct grouplist *);
20019c46d8cSEdward Tomasz Napierala static struct grouplist	*get_grp(void);
20119c46d8cSEdward Tomasz Napierala static void	hang_dirp(struct dirlist *, struct grouplist *,
20285429990SWarner Losh 				struct exportlist *, int);
20319c46d8cSEdward Tomasz Napierala static void	huphandler(int sig);
20419c46d8cSEdward Tomasz Napierala static int	makemask(struct sockaddr_storage *ssp, int bitlen);
20519c46d8cSEdward Tomasz Napierala static void	mntsrv(struct svc_req *, SVCXPRT *);
20619c46d8cSEdward Tomasz Napierala static void	nextfield(char **, char **);
20719c46d8cSEdward Tomasz Napierala static void	out_of_mem(void);
20819c46d8cSEdward Tomasz Napierala static void	parsecred(char *, struct xucred *);
20919c46d8cSEdward Tomasz Napierala static int	parsesec(char *, struct exportlist *);
21019c46d8cSEdward Tomasz Napierala static int	put_exlist(struct dirlist *, XDR *, struct dirlist *,
21119c46d8cSEdward Tomasz Napierala 		    int *, int);
21219c46d8cSEdward Tomasz Napierala static void	*sa_rawaddr(struct sockaddr *sa, int *nbytes);
21319c46d8cSEdward Tomasz Napierala static int	sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
21460caaee2SIan Dowse 		    struct sockaddr *samask);
21519c46d8cSEdward Tomasz Napierala static int	scan_tree(struct dirlist *, struct sockaddr *);
21685429990SWarner Losh static void	usage(void);
21719c46d8cSEdward Tomasz Napierala static int	xdr_dir(XDR *, char *);
21819c46d8cSEdward Tomasz Napierala static int	xdr_explist(XDR *, caddr_t);
21919c46d8cSEdward Tomasz Napierala static int	xdr_explist_brief(XDR *, caddr_t);
22019c46d8cSEdward Tomasz Napierala static int	xdr_explist_common(XDR *, caddr_t, int);
22119c46d8cSEdward Tomasz Napierala static int	xdr_fhs(XDR *, caddr_t);
22219c46d8cSEdward Tomasz Napierala static int	xdr_mlist(XDR *, caddr_t);
22319c46d8cSEdward Tomasz Napierala static void	terminate(int);
2248fae3551SRodney W. Grimes 
22519c46d8cSEdward Tomasz Napierala static struct exportlist *exphead;
22619c46d8cSEdward Tomasz Napierala static struct mountlist *mlhead;
22719c46d8cSEdward Tomasz Napierala static struct grouplist *grphead;
22819c46d8cSEdward Tomasz Napierala static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
22919c46d8cSEdward Tomasz Napierala static char **exnames;
23019c46d8cSEdward Tomasz Napierala static char **hosts = NULL;
23119c46d8cSEdward Tomasz Napierala static struct xucred def_anon = {
23276183f34SDima Dorfman 	XUCRED_VERSION,
233947572b4SRick Macklem 	(uid_t)65534,
2348fae3551SRodney W. Grimes 	1,
235947572b4SRick Macklem 	{ (gid_t)65533 },
236c0511d3bSBrian Feldman 	NULL
2378fae3551SRodney W. Grimes };
23819c46d8cSEdward Tomasz Napierala static int force_v2 = 0;
23919c46d8cSEdward Tomasz Napierala static int resvport_only = 1;
24019c46d8cSEdward Tomasz Napierala static int nhosts = 0;
24119c46d8cSEdward Tomasz Napierala static int dir_only = 1;
24219c46d8cSEdward Tomasz Napierala static int dolog = 0;
24319c46d8cSEdward Tomasz Napierala static int got_sighup = 0;
24419c46d8cSEdward Tomasz Napierala static int xcreated = 0;
245d11e3645SMatteo Riondato 
24619c46d8cSEdward Tomasz Napierala static char *svcport_str = NULL;
2478fb6ad5dSRick Macklem static int mallocd_svcport = 0;
2488fb6ad5dSRick Macklem static int *sock_fd;
2498fb6ad5dSRick Macklem static int sock_fdcnt;
2508fb6ad5dSRick Macklem static int sock_fdpos;
251c548eb5cSRick Macklem static int suspend_nfsd = 0;
2528360efbdSAlfred Perlstein 
25319c46d8cSEdward Tomasz Napierala static int opt_flags;
2548360efbdSAlfred Perlstein static int have_v6 = 1;
2558360efbdSAlfred Perlstein 
25619c46d8cSEdward Tomasz Napierala static int v4root_phase = 0;
25719c46d8cSEdward Tomasz Napierala static char v4root_dirpath[PATH_MAX + 1];
25819c46d8cSEdward Tomasz Napierala static int has_publicfh = 0;
259bcc1d071SRick Macklem 
26019c46d8cSEdward Tomasz Napierala static struct pidfh *pfh = NULL;
26160caaee2SIan Dowse /* Bits for opt_flags above */
2628fae3551SRodney W. Grimes #define	OP_MAPROOT	0x01
2638fae3551SRodney W. Grimes #define	OP_MAPALL	0x02
26491196234SPeter Wemm /* 0x4 free */
2658fae3551SRodney W. Grimes #define	OP_MASK		0x08
2668fae3551SRodney W. Grimes #define	OP_NET		0x10
2678fae3551SRodney W. Grimes #define	OP_ALLDIRS	0x40
26860caaee2SIan Dowse #define	OP_HAVEMASK	0x80	/* A mask was specified or inferred. */
269288fa14aSJoerg Wunsch #define	OP_QUIET	0x100
2708360efbdSAlfred Perlstein #define OP_MASKLEN	0x200
271a9148abdSDoug Rabson #define OP_SEC		0x400
2728fae3551SRodney W. Grimes 
2738fae3551SRodney W. Grimes #ifdef DEBUG
27419c46d8cSEdward Tomasz Napierala static int debug = 1;
27519c46d8cSEdward Tomasz Napierala static void	SYSLOG(int, const char *, ...) __printflike(2, 3);
2768fae3551SRodney W. Grimes #define syslog SYSLOG
2778fae3551SRodney W. Grimes #else
27819c46d8cSEdward Tomasz Napierala static int debug = 0;
2798fae3551SRodney W. Grimes #endif
2808fae3551SRodney W. Grimes 
2818fae3551SRodney W. Grimes /*
282b875c2e9SJosh Paetzel  * Similar to strsep(), but it allows for quoted strings
283b875c2e9SJosh Paetzel  * and escaped characters.
284b875c2e9SJosh Paetzel  *
285b875c2e9SJosh Paetzel  * It returns the string (or NULL, if *stringp is NULL),
286b875c2e9SJosh Paetzel  * which is a de-quoted version of the string if necessary.
287b875c2e9SJosh Paetzel  *
288b875c2e9SJosh Paetzel  * It modifies *stringp in place.
289b875c2e9SJosh Paetzel  */
290b875c2e9SJosh Paetzel static char *
291b875c2e9SJosh Paetzel strsep_quote(char **stringp, const char *delim)
292b875c2e9SJosh Paetzel {
293b875c2e9SJosh Paetzel 	char *srcptr, *dstptr, *retval;
294b875c2e9SJosh Paetzel 	char quot = 0;
295b875c2e9SJosh Paetzel 
296b875c2e9SJosh Paetzel 	if (stringp == NULL || *stringp == NULL)
297b875c2e9SJosh Paetzel 		return (NULL);
298b875c2e9SJosh Paetzel 
299b875c2e9SJosh Paetzel 	srcptr = dstptr = retval = *stringp;
300b875c2e9SJosh Paetzel 
301b875c2e9SJosh Paetzel 	while (*srcptr) {
302b875c2e9SJosh Paetzel 		/*
303b875c2e9SJosh Paetzel 		 * We're looking for several edge cases here.
304b875c2e9SJosh Paetzel 		 * First:  if we're in quote state (quot != 0),
305b875c2e9SJosh Paetzel 		 * then we ignore the delim characters, but otherwise
306b875c2e9SJosh Paetzel 		 * process as normal, unless it is the quote character.
307b875c2e9SJosh Paetzel 		 * Second:  if the current character is a backslash,
308b875c2e9SJosh Paetzel 		 * we take the next character as-is, without checking
309b875c2e9SJosh Paetzel 		 * for delim, quote, or backslash.  Exception:  if the
310b875c2e9SJosh Paetzel 		 * next character is a NUL, that's the end of the string.
311b875c2e9SJosh Paetzel 		 * Third:  if the character is a quote character, we toggle
312b875c2e9SJosh Paetzel 		 * quote state.
313b875c2e9SJosh Paetzel 		 * Otherwise:  check the current character for NUL, or
314b875c2e9SJosh Paetzel 		 * being in delim, and end the string if either is true.
315b875c2e9SJosh Paetzel 		 */
316b875c2e9SJosh Paetzel 		if (*srcptr == '\\') {
317b875c2e9SJosh Paetzel 			srcptr++;
318b875c2e9SJosh Paetzel 			/*
319b875c2e9SJosh Paetzel 			 * The edge case here is if the next character
320b875c2e9SJosh Paetzel 			 * is NUL, we want to stop processing.  But if
321b875c2e9SJosh Paetzel 			 * it's not NUL, then we simply want to copy it.
322b875c2e9SJosh Paetzel 			 */
323b875c2e9SJosh Paetzel 			if (*srcptr) {
324b875c2e9SJosh Paetzel 				*dstptr++ = *srcptr++;
325b875c2e9SJosh Paetzel 			}
326b875c2e9SJosh Paetzel 			continue;
327b875c2e9SJosh Paetzel 		}
328b875c2e9SJosh Paetzel 		if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
329b875c2e9SJosh Paetzel 			quot = *srcptr++;
330b875c2e9SJosh Paetzel 			continue;
331b875c2e9SJosh Paetzel 		}
332b875c2e9SJosh Paetzel 		if (quot && *srcptr == quot) {
333b875c2e9SJosh Paetzel 			/* End of the quoted part */
334b875c2e9SJosh Paetzel 			quot = 0;
335b875c2e9SJosh Paetzel 			srcptr++;
336b875c2e9SJosh Paetzel 			continue;
337b875c2e9SJosh Paetzel 		}
338b875c2e9SJosh Paetzel 		if (!quot && strchr(delim, *srcptr))
339b875c2e9SJosh Paetzel 			break;
340b875c2e9SJosh Paetzel 		*dstptr++ = *srcptr++;
341b875c2e9SJosh Paetzel 	}
342b875c2e9SJosh Paetzel 
343b875c2e9SJosh Paetzel 	*dstptr = 0; /* Terminate the string */
344b875c2e9SJosh Paetzel 	*stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
345b875c2e9SJosh Paetzel 	return (retval);
346b875c2e9SJosh Paetzel }
347b875c2e9SJosh Paetzel 
348b875c2e9SJosh Paetzel /*
3498fae3551SRodney W. Grimes  * Mountd server for NFS mount protocol as described in:
3508fae3551SRodney W. Grimes  * NFS: Network File System Protocol Specification, RFC1094, Appendix A
3518fae3551SRodney W. Grimes  * The optional arguments are the exports file name
3528fae3551SRodney W. Grimes  * default: _PATH_EXPORTS
3538fae3551SRodney W. Grimes  * and "-n" to allow nonroot mount.
3548fae3551SRodney W. Grimes  */
3558fae3551SRodney W. Grimes int
356a7a7d96cSPhilippe Charnier main(int argc, char **argv)
3578fae3551SRodney W. Grimes {
35869d65572SIan Dowse 	fd_set readfds;
359d11e3645SMatteo Riondato 	struct netconfig *nconf;
360d11e3645SMatteo Riondato 	char *endptr, **hosts_bak;
361d11e3645SMatteo Riondato 	void *nc_handle;
362a032b226SPawel Jakub Dawidek 	pid_t otherpid;
363d11e3645SMatteo Riondato 	in_port_t svcport;
364d11e3645SMatteo Riondato 	int c, k, s;
365bcb53b16SMartin Blapp 	int maxrec = RPC_MAXDATASIZE;
3668fb6ad5dSRick Macklem 	int attempt_cnt, port_len, port_pos, ret;
3678fb6ad5dSRick Macklem 	char **port_list;
3688360efbdSAlfred Perlstein 
36901709abfSIan Dowse 	/* Check that another mountd isn't already running. */
3708b28aef2SPawel Jakub Dawidek 	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
371a032b226SPawel Jakub Dawidek 	if (pfh == NULL) {
372a032b226SPawel Jakub Dawidek 		if (errno == EEXIST)
373a032b226SPawel Jakub Dawidek 			errx(1, "mountd already running, pid: %d.", otherpid);
374a032b226SPawel Jakub Dawidek 		warn("cannot open or create pidfile");
375a032b226SPawel Jakub Dawidek 	}
3768360efbdSAlfred Perlstein 
3778360efbdSAlfred Perlstein 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
3788360efbdSAlfred Perlstein 	if (s < 0)
3798360efbdSAlfred Perlstein 		have_v6 = 0;
3808360efbdSAlfred Perlstein 	else
3818360efbdSAlfred Perlstein 		close(s);
3828fae3551SRodney W. Grimes 
38379b86807SEdward Tomasz Napierala 	while ((c = getopt(argc, argv, "2deh:lnp:rS")) != -1)
3848fae3551SRodney W. Grimes 		switch (c) {
3852a66cfc5SDoug Rabson 		case '2':
3862a66cfc5SDoug Rabson 			force_v2 = 1;
3872a66cfc5SDoug Rabson 			break;
3882179ae1eSRick Macklem 		case 'e':
3892a85df8cSRick Macklem 			/* now a no-op, since this is the default */
390bcc1d071SRick Macklem 			break;
391a62dc406SDoug Rabson 		case 'n':
392a62dc406SDoug Rabson 			resvport_only = 0;
393a62dc406SDoug Rabson 			break;
394a62dc406SDoug Rabson 		case 'r':
395a62dc406SDoug Rabson 			dir_only = 0;
396a62dc406SDoug Rabson 			break;
3976444ef3bSPoul-Henning Kamp 		case 'd':
3986444ef3bSPoul-Henning Kamp 			debug = debug ? 0 : 1;
3996444ef3bSPoul-Henning Kamp 			break;
400f51631d7SGuido van Rooij 		case 'l':
401c903443aSPeter Wemm 			dolog = 1;
402f51631d7SGuido van Rooij 			break;
403c203da27SBruce M Simpson 		case 'p':
404c203da27SBruce M Simpson 			endptr = NULL;
405c203da27SBruce M Simpson 			svcport = (in_port_t)strtoul(optarg, &endptr, 10);
406c203da27SBruce M Simpson 			if (endptr == NULL || *endptr != '\0' ||
407c203da27SBruce M Simpson 			    svcport == 0 || svcport >= IPPORT_MAX)
408c203da27SBruce M Simpson 				usage();
409d11e3645SMatteo Riondato 			svcport_str = strdup(optarg);
410d11e3645SMatteo Riondato 			break;
411d11e3645SMatteo Riondato 		case 'h':
412d11e3645SMatteo Riondato 			++nhosts;
413d11e3645SMatteo Riondato 			hosts_bak = hosts;
414d11e3645SMatteo Riondato 			hosts_bak = realloc(hosts, nhosts * sizeof(char *));
415d11e3645SMatteo Riondato 			if (hosts_bak == NULL) {
416d11e3645SMatteo Riondato 				if (hosts != NULL) {
417d11e3645SMatteo Riondato 					for (k = 0; k < nhosts; k++)
418d11e3645SMatteo Riondato 						free(hosts[k]);
419d11e3645SMatteo Riondato 					free(hosts);
420d11e3645SMatteo Riondato 					out_of_mem();
421d11e3645SMatteo Riondato 				}
422d11e3645SMatteo Riondato 			}
423d11e3645SMatteo Riondato 			hosts = hosts_bak;
424d11e3645SMatteo Riondato 			hosts[nhosts - 1] = strdup(optarg);
425d11e3645SMatteo Riondato 			if (hosts[nhosts - 1] == NULL) {
426d11e3645SMatteo Riondato 				for (k = 0; k < (nhosts - 1); k++)
427d11e3645SMatteo Riondato 					free(hosts[k]);
428d11e3645SMatteo Riondato 				free(hosts);
429d11e3645SMatteo Riondato 				out_of_mem();
430d11e3645SMatteo Riondato 			}
431c203da27SBruce M Simpson 			break;
432c548eb5cSRick Macklem 		case 'S':
433c548eb5cSRick Macklem 			suspend_nfsd = 1;
434c548eb5cSRick Macklem 			break;
4358fae3551SRodney W. Grimes 		default:
43674853402SPhilippe Charnier 			usage();
43780c7cc1cSPedro F. Giffuni 		}
438bcc1d071SRick Macklem 
439bcc1d071SRick Macklem 	if (modfind("nfsd") < 0) {
440bcc1d071SRick Macklem 		/* Not present in kernel, try loading it */
441bcc1d071SRick Macklem 		if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
442bcc1d071SRick Macklem 			errx(1, "NFS server is not available");
443bcc1d071SRick Macklem 	}
444bcc1d071SRick Macklem 
4458fae3551SRodney W. Grimes 	argc -= optind;
4468fae3551SRodney W. Grimes 	argv += optind;
4478fae3551SRodney W. Grimes 	grphead = (struct grouplist *)NULL;
4488fae3551SRodney W. Grimes 	exphead = (struct exportlist *)NULL;
4498fae3551SRodney W. Grimes 	mlhead = (struct mountlist *)NULL;
45096968c22SPawel Jakub Dawidek 	if (argc > 0)
45196968c22SPawel Jakub Dawidek 		exnames = argv;
45296968c22SPawel Jakub Dawidek 	else
45396968c22SPawel Jakub Dawidek 		exnames = exnames_default;
4548fae3551SRodney W. Grimes 	openlog("mountd", LOG_PID, LOG_DAEMON);
4558fae3551SRodney W. Grimes 	if (debug)
45674853402SPhilippe Charnier 		warnx("getting export list");
4578fae3551SRodney W. Grimes 	get_exportlist();
4588fae3551SRodney W. Grimes 	if (debug)
45974853402SPhilippe Charnier 		warnx("getting mount list");
4608fae3551SRodney W. Grimes 	get_mountlist();
4618fae3551SRodney W. Grimes 	if (debug)
46274853402SPhilippe Charnier 		warnx("here we go");
4638fae3551SRodney W. Grimes 	if (debug == 0) {
4648fae3551SRodney W. Grimes 		daemon(0, 0);
4658fae3551SRodney W. Grimes 		signal(SIGINT, SIG_IGN);
4668fae3551SRodney W. Grimes 		signal(SIGQUIT, SIG_IGN);
4678fae3551SRodney W. Grimes 	}
46869d65572SIan Dowse 	signal(SIGHUP, huphandler);
4698360efbdSAlfred Perlstein 	signal(SIGTERM, terminate);
47009fc9dc6SCraig Rodrigues 	signal(SIGPIPE, SIG_IGN);
471a032b226SPawel Jakub Dawidek 
472a032b226SPawel Jakub Dawidek 	pidfile_write(pfh);
473a032b226SPawel Jakub Dawidek 
4740775314bSDoug Rabson 	rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
4750775314bSDoug Rabson 	rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
476bcb53b16SMartin Blapp 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
477bcb53b16SMartin Blapp 
478c6e5e158SGuido van Rooij 	if (!resvport_only) {
4795ebee88dSRick Macklem 		if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
4804a0785aaSPeter Wemm 		    &resvport_only, sizeof(resvport_only)) != 0 &&
4814a0785aaSPeter Wemm 		    errno != ENOENT) {
482394da4c1SGuido van Rooij 			syslog(LOG_ERR, "sysctl: %m");
483394da4c1SGuido van Rooij 			exit(1);
484394da4c1SGuido van Rooij 		}
485c6e5e158SGuido van Rooij 	}
486c203da27SBruce M Simpson 
487d11e3645SMatteo Riondato 	/*
488d11e3645SMatteo Riondato 	 * If no hosts were specified, add a wildcard entry to bind to
489d11e3645SMatteo Riondato 	 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
490d11e3645SMatteo Riondato 	 * list.
491d11e3645SMatteo Riondato 	 */
492d11e3645SMatteo Riondato 	if (nhosts == 0) {
493c9e1c304SUlrich Spörlein 		hosts = malloc(sizeof(char *));
494d11e3645SMatteo Riondato 		if (hosts == NULL)
495d11e3645SMatteo Riondato 			out_of_mem();
496d11e3645SMatteo Riondato 		hosts[0] = "*";
497d11e3645SMatteo Riondato 		nhosts = 1;
498d11e3645SMatteo Riondato 	} else {
499d11e3645SMatteo Riondato 		hosts_bak = hosts;
500d11e3645SMatteo Riondato 		if (have_v6) {
501d11e3645SMatteo Riondato 			hosts_bak = realloc(hosts, (nhosts + 2) *
502d11e3645SMatteo Riondato 			    sizeof(char *));
503d11e3645SMatteo Riondato 			if (hosts_bak == NULL) {
504d11e3645SMatteo Riondato 				for (k = 0; k < nhosts; k++)
505d11e3645SMatteo Riondato 					free(hosts[k]);
506d11e3645SMatteo Riondato 		    		free(hosts);
507d11e3645SMatteo Riondato 		    		out_of_mem();
508c203da27SBruce M Simpson 			} else
509d11e3645SMatteo Riondato 				hosts = hosts_bak;
510d11e3645SMatteo Riondato 			nhosts += 2;
511d11e3645SMatteo Riondato 			hosts[nhosts - 2] = "::1";
512d11e3645SMatteo Riondato 		} else {
513d11e3645SMatteo Riondato 			hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
514d11e3645SMatteo Riondato 			if (hosts_bak == NULL) {
515d11e3645SMatteo Riondato 				for (k = 0; k < nhosts; k++)
516d11e3645SMatteo Riondato 					free(hosts[k]);
517d11e3645SMatteo Riondato 				free(hosts);
518d11e3645SMatteo Riondato 				out_of_mem();
519d11e3645SMatteo Riondato 			} else {
520d11e3645SMatteo Riondato 				nhosts += 1;
521d11e3645SMatteo Riondato 				hosts = hosts_bak;
5228fae3551SRodney W. Grimes 			}
523d11e3645SMatteo Riondato 		}
5248360efbdSAlfred Perlstein 
525d11e3645SMatteo Riondato 		hosts[nhosts - 1] = "127.0.0.1";
5268360efbdSAlfred Perlstein 	}
5278360efbdSAlfred Perlstein 
5288fb6ad5dSRick Macklem 	attempt_cnt = 1;
5298fb6ad5dSRick Macklem 	sock_fdcnt = 0;
5308fb6ad5dSRick Macklem 	sock_fd = NULL;
5318fb6ad5dSRick Macklem 	port_list = NULL;
5328fb6ad5dSRick Macklem 	port_len = 0;
533d11e3645SMatteo Riondato 	nc_handle = setnetconfig();
534d11e3645SMatteo Riondato 	while ((nconf = getnetconfig(nc_handle))) {
535d11e3645SMatteo Riondato 		if (nconf->nc_flag & NC_VISIBLE) {
536d11e3645SMatteo Riondato 			if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
537d11e3645SMatteo Riondato 			    "inet6") == 0) {
538d11e3645SMatteo Riondato 				/* DO NOTHING */
5398fb6ad5dSRick Macklem 			} else {
5408fb6ad5dSRick Macklem 				ret = create_service(nconf);
5418fb6ad5dSRick Macklem 				if (ret == 1)
5428fb6ad5dSRick Macklem 					/* Ignore this call */
5438fb6ad5dSRick Macklem 					continue;
5448fb6ad5dSRick Macklem 				if (ret < 0) {
5458fb6ad5dSRick Macklem 					/*
5468fb6ad5dSRick Macklem 					 * Failed to bind port, so close off
5478fb6ad5dSRick Macklem 					 * all sockets created and try again
5488fb6ad5dSRick Macklem 					 * if the port# was dynamically
5498fb6ad5dSRick Macklem 					 * assigned via bind(2).
5508fb6ad5dSRick Macklem 					 */
5518fb6ad5dSRick Macklem 					clearout_service();
5528fb6ad5dSRick Macklem 					if (mallocd_svcport != 0 &&
5538fb6ad5dSRick Macklem 					    attempt_cnt < GETPORT_MAXTRY) {
5548fb6ad5dSRick Macklem 						free(svcport_str);
5558fb6ad5dSRick Macklem 						svcport_str = NULL;
5568fb6ad5dSRick Macklem 						mallocd_svcport = 0;
5578fb6ad5dSRick Macklem 					} else {
5588fb6ad5dSRick Macklem 						errno = EADDRINUSE;
5598fb6ad5dSRick Macklem 						syslog(LOG_ERR,
5608fb6ad5dSRick Macklem 						    "bindresvport_sa: %m");
5618fb6ad5dSRick Macklem 						exit(1);
5628fb6ad5dSRick Macklem 					}
5638fb6ad5dSRick Macklem 
5648fb6ad5dSRick Macklem 					/* Start over at the first service. */
5658fb6ad5dSRick Macklem 					free(sock_fd);
5668fb6ad5dSRick Macklem 					sock_fdcnt = 0;
5678fb6ad5dSRick Macklem 					sock_fd = NULL;
5688fb6ad5dSRick Macklem 					nc_handle = setnetconfig();
5698fb6ad5dSRick Macklem 					attempt_cnt++;
5708fb6ad5dSRick Macklem 				} else if (mallocd_svcport != 0 &&
5718fb6ad5dSRick Macklem 				    attempt_cnt == GETPORT_MAXTRY) {
5728fb6ad5dSRick Macklem 					/*
5738fb6ad5dSRick Macklem 					 * For the last attempt, allow
5748fb6ad5dSRick Macklem 					 * different port #s for each nconf
5758fb6ad5dSRick Macklem 					 * by saving the svcport_str and
5768fb6ad5dSRick Macklem 					 * setting it back to NULL.
5778fb6ad5dSRick Macklem 					 */
5788fb6ad5dSRick Macklem 					port_list = realloc(port_list,
5798fb6ad5dSRick Macklem 					    (port_len + 1) * sizeof(char *));
5808fb6ad5dSRick Macklem 					if (port_list == NULL)
5818fb6ad5dSRick Macklem 						out_of_mem();
5828fb6ad5dSRick Macklem 					port_list[port_len++] = svcport_str;
5838fb6ad5dSRick Macklem 					svcport_str = NULL;
5848fb6ad5dSRick Macklem 					mallocd_svcport = 0;
5858fb6ad5dSRick Macklem 				}
5868fb6ad5dSRick Macklem 			}
5878fb6ad5dSRick Macklem 		}
5888fb6ad5dSRick Macklem 	}
5898fb6ad5dSRick Macklem 
5908fb6ad5dSRick Macklem 	/*
5918fb6ad5dSRick Macklem 	 * Successfully bound the ports, so call complete_service() to
5928fb6ad5dSRick Macklem 	 * do the rest of the setup on the service(s).
5938fb6ad5dSRick Macklem 	 */
5948fb6ad5dSRick Macklem 	sock_fdpos = 0;
5958fb6ad5dSRick Macklem 	port_pos = 0;
5968fb6ad5dSRick Macklem 	nc_handle = setnetconfig();
5978fb6ad5dSRick Macklem 	while ((nconf = getnetconfig(nc_handle))) {
5988fb6ad5dSRick Macklem 		if (nconf->nc_flag & NC_VISIBLE) {
5998fb6ad5dSRick Macklem 			if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
6008fb6ad5dSRick Macklem 			    "inet6") == 0) {
6018fb6ad5dSRick Macklem 				/* DO NOTHING */
6028fb6ad5dSRick Macklem 			} else if (port_list != NULL) {
6038fb6ad5dSRick Macklem 				if (port_pos >= port_len) {
6048fb6ad5dSRick Macklem 					syslog(LOG_ERR, "too many port#s");
6058fb6ad5dSRick Macklem 					exit(1);
6068fb6ad5dSRick Macklem 				}
6078fb6ad5dSRick Macklem 				complete_service(nconf, port_list[port_pos++]);
608c203da27SBruce M Simpson 			} else
6098fb6ad5dSRick Macklem 				complete_service(nconf, svcport_str);
6108360efbdSAlfred Perlstein 		}
611d11e3645SMatteo Riondato 	}
612d11e3645SMatteo Riondato 	endnetconfig(nc_handle);
6138fb6ad5dSRick Macklem 	free(sock_fd);
6148fb6ad5dSRick Macklem 	if (port_list != NULL) {
6158fb6ad5dSRick Macklem 		for (port_pos = 0; port_pos < port_len; port_pos++)
6168fb6ad5dSRick Macklem 			free(port_list[port_pos]);
6178fb6ad5dSRick Macklem 		free(port_list);
6188fb6ad5dSRick Macklem 	}
6198360efbdSAlfred Perlstein 
6208360efbdSAlfred Perlstein 	if (xcreated == 0) {
6218360efbdSAlfred Perlstein 		syslog(LOG_ERR, "could not create any services");
6222a66cfc5SDoug Rabson 		exit(1);
6232a66cfc5SDoug Rabson 	}
62469d65572SIan Dowse 
62569d65572SIan Dowse 	/* Expand svc_run() here so that we can call get_exportlist(). */
62669d65572SIan Dowse 	for (;;) {
62769d65572SIan Dowse 		if (got_sighup) {
62869d65572SIan Dowse 			get_exportlist();
62969d65572SIan Dowse 			got_sighup = 0;
63069d65572SIan Dowse 		}
63169d65572SIan Dowse 		readfds = svc_fdset;
63269d65572SIan Dowse 		switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
63369d65572SIan Dowse 		case -1:
63469d65572SIan Dowse 			if (errno == EINTR)
63569d65572SIan Dowse                                 continue;
63669d65572SIan Dowse 			syslog(LOG_ERR, "mountd died: select: %m");
63774853402SPhilippe Charnier 			exit(1);
63869d65572SIan Dowse 		case 0:
63969d65572SIan Dowse 			continue;
64069d65572SIan Dowse 		default:
64169d65572SIan Dowse 			svc_getreqset(&readfds);
64269d65572SIan Dowse 		}
64369d65572SIan Dowse 	}
64474853402SPhilippe Charnier }
64574853402SPhilippe Charnier 
646d11e3645SMatteo Riondato /*
647d11e3645SMatteo Riondato  * This routine creates and binds sockets on the appropriate
6488fb6ad5dSRick Macklem  * addresses. It gets called one time for each transport.
6498fb6ad5dSRick Macklem  * It returns 0 upon success, 1 for ingore the call and -1 to indicate
6508fb6ad5dSRick Macklem  * bind failed with EADDRINUSE.
6518fb6ad5dSRick Macklem  * Any file descriptors that have been created are stored in sock_fd and
6528fb6ad5dSRick Macklem  * the total count of them is maintained in sock_fdcnt.
653d11e3645SMatteo Riondato  */
6548fb6ad5dSRick Macklem static int
655d11e3645SMatteo Riondato create_service(struct netconfig *nconf)
656d11e3645SMatteo Riondato {
657d11e3645SMatteo Riondato 	struct addrinfo hints, *res = NULL;
658d11e3645SMatteo Riondato 	struct sockaddr_in *sin;
659d11e3645SMatteo Riondato 	struct sockaddr_in6 *sin6;
660d11e3645SMatteo Riondato 	struct __rpc_sockinfo si;
661d11e3645SMatteo Riondato 	int aicode;
662d11e3645SMatteo Riondato 	int fd;
663d11e3645SMatteo Riondato 	int nhostsbak;
664d11e3645SMatteo Riondato 	int one = 1;
665d11e3645SMatteo Riondato 	int r;
666d11e3645SMatteo Riondato 	u_int32_t host_addr[4];  /* IPv4 or IPv6 */
6678fb6ad5dSRick Macklem 	int mallocd_res;
668d11e3645SMatteo Riondato 
669d11e3645SMatteo Riondato 	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
670d11e3645SMatteo Riondato 	    (nconf->nc_semantics != NC_TPI_COTS) &&
671d11e3645SMatteo Riondato 	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
6728fb6ad5dSRick Macklem 		return (1);	/* not my type */
673d11e3645SMatteo Riondato 
674d11e3645SMatteo Riondato 	/*
675d11e3645SMatteo Riondato 	 * XXX - using RPC library internal functions.
676d11e3645SMatteo Riondato 	 */
677d11e3645SMatteo Riondato 	if (!__rpc_nconf2sockinfo(nconf, &si)) {
678d11e3645SMatteo Riondato 		syslog(LOG_ERR, "cannot get information for %s",
679d11e3645SMatteo Riondato 		    nconf->nc_netid);
6808fb6ad5dSRick Macklem 		return (1);
681d11e3645SMatteo Riondato 	}
682d11e3645SMatteo Riondato 
683d11e3645SMatteo Riondato 	/* Get mountd's address on this transport */
684d11e3645SMatteo Riondato 	memset(&hints, 0, sizeof hints);
685d11e3645SMatteo Riondato 	hints.ai_family = si.si_af;
686d11e3645SMatteo Riondato 	hints.ai_socktype = si.si_socktype;
687d11e3645SMatteo Riondato 	hints.ai_protocol = si.si_proto;
688d11e3645SMatteo Riondato 
689d11e3645SMatteo Riondato 	/*
690d11e3645SMatteo Riondato 	 * Bind to specific IPs if asked to
691d11e3645SMatteo Riondato 	 */
692d11e3645SMatteo Riondato 	nhostsbak = nhosts;
693d11e3645SMatteo Riondato 	while (nhostsbak > 0) {
694d11e3645SMatteo Riondato 		--nhostsbak;
6958fb6ad5dSRick Macklem 		sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
6968fb6ad5dSRick Macklem 		if (sock_fd == NULL)
6978fb6ad5dSRick Macklem 			out_of_mem();
6988fb6ad5dSRick Macklem 		sock_fd[sock_fdcnt++] = -1;	/* Set invalid for now. */
6998fb6ad5dSRick Macklem 		mallocd_res = 0;
7008fb6ad5dSRick Macklem 
7019745de4cSRyan Stone 		hints.ai_flags = AI_PASSIVE;
7029745de4cSRyan Stone 
703d11e3645SMatteo Riondato 		/*
704d11e3645SMatteo Riondato 		 * XXX - using RPC library internal functions.
705d11e3645SMatteo Riondato 		 */
706d11e3645SMatteo Riondato 		if ((fd = __rpc_nconf2fd(nconf)) < 0) {
707d11e3645SMatteo Riondato 			int non_fatal = 0;
708a5752d55SKevin Lo 	    		if (errno == EAFNOSUPPORT &&
709d11e3645SMatteo Riondato 			    nconf->nc_semantics != NC_TPI_CLTS)
710d11e3645SMatteo Riondato 				non_fatal = 1;
711d11e3645SMatteo Riondato 
712d11e3645SMatteo Riondato 			syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
713d11e3645SMatteo Riondato 			    "cannot create socket for %s", nconf->nc_netid);
7148fb6ad5dSRick Macklem 			if (non_fatal != 0)
7158fb6ad5dSRick Macklem 				continue;
7168fb6ad5dSRick Macklem 			exit(1);
717d11e3645SMatteo Riondato 		}
718d11e3645SMatteo Riondato 
719d11e3645SMatteo Riondato 		switch (hints.ai_family) {
720d11e3645SMatteo Riondato 		case AF_INET:
721d11e3645SMatteo Riondato 			if (inet_pton(AF_INET, hosts[nhostsbak],
722d11e3645SMatteo Riondato 			    host_addr) == 1) {
7238fb6ad5dSRick Macklem 				hints.ai_flags |= AI_NUMERICHOST;
724d11e3645SMatteo Riondato 			} else {
725d11e3645SMatteo Riondato 				/*
726d11e3645SMatteo Riondato 				 * Skip if we have an AF_INET6 address.
727d11e3645SMatteo Riondato 				 */
728d11e3645SMatteo Riondato 				if (inet_pton(AF_INET6, hosts[nhostsbak],
729d11e3645SMatteo Riondato 				    host_addr) == 1) {
730d11e3645SMatteo Riondato 					close(fd);
731d11e3645SMatteo Riondato 					continue;
732d11e3645SMatteo Riondato 				}
733d11e3645SMatteo Riondato 			}
734d11e3645SMatteo Riondato 			break;
735d11e3645SMatteo Riondato 		case AF_INET6:
736d11e3645SMatteo Riondato 			if (inet_pton(AF_INET6, hosts[nhostsbak],
737d11e3645SMatteo Riondato 			    host_addr) == 1) {
7388fb6ad5dSRick Macklem 				hints.ai_flags |= AI_NUMERICHOST;
739d11e3645SMatteo Riondato 			} else {
740d11e3645SMatteo Riondato 				/*
741d11e3645SMatteo Riondato 				 * Skip if we have an AF_INET address.
742d11e3645SMatteo Riondato 				 */
743d11e3645SMatteo Riondato 				if (inet_pton(AF_INET, hosts[nhostsbak],
744d11e3645SMatteo Riondato 				    host_addr) == 1) {
745d11e3645SMatteo Riondato 					close(fd);
746d11e3645SMatteo Riondato 					continue;
747d11e3645SMatteo Riondato 				}
748d11e3645SMatteo Riondato 			}
749d11e3645SMatteo Riondato 
750d11e3645SMatteo Riondato 			/*
751d11e3645SMatteo Riondato 			 * We're doing host-based access checks here, so don't
752d11e3645SMatteo Riondato 			 * allow v4-in-v6 to confuse things. The kernel will
753d11e3645SMatteo Riondato 			 * disable it by default on NFS sockets too.
754d11e3645SMatteo Riondato 			 */
755d11e3645SMatteo Riondato 			if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
756d11e3645SMatteo Riondato 			    sizeof one) < 0) {
757d11e3645SMatteo Riondato 				syslog(LOG_ERR,
758d11e3645SMatteo Riondato 				    "can't disable v4-in-v6 on IPv6 socket");
759d11e3645SMatteo Riondato 				exit(1);
760d11e3645SMatteo Riondato 			}
761d11e3645SMatteo Riondato 			break;
762d11e3645SMatteo Riondato 		default:
763d11e3645SMatteo Riondato 			break;
764d11e3645SMatteo Riondato 		}
765d11e3645SMatteo Riondato 
766d11e3645SMatteo Riondato 		/*
767d11e3645SMatteo Riondato 		 * If no hosts were specified, just bind to INADDR_ANY
768d11e3645SMatteo Riondato 		 */
769d11e3645SMatteo Riondato 		if (strcmp("*", hosts[nhostsbak]) == 0) {
770d11e3645SMatteo Riondato 			if (svcport_str == NULL) {
771d11e3645SMatteo Riondato 				res = malloc(sizeof(struct addrinfo));
772d11e3645SMatteo Riondato 				if (res == NULL)
773d11e3645SMatteo Riondato 					out_of_mem();
7748fb6ad5dSRick Macklem 				mallocd_res = 1;
775d11e3645SMatteo Riondato 				res->ai_flags = hints.ai_flags;
776d11e3645SMatteo Riondato 				res->ai_family = hints.ai_family;
777d11e3645SMatteo Riondato 				res->ai_protocol = hints.ai_protocol;
778d11e3645SMatteo Riondato 				switch (res->ai_family) {
779d11e3645SMatteo Riondato 				case AF_INET:
780d11e3645SMatteo Riondato 					sin = malloc(sizeof(struct sockaddr_in));
781d11e3645SMatteo Riondato 					if (sin == NULL)
782d11e3645SMatteo Riondato 						out_of_mem();
783d11e3645SMatteo Riondato 					sin->sin_family = AF_INET;
784d11e3645SMatteo Riondato 					sin->sin_port = htons(0);
785d11e3645SMatteo Riondato 					sin->sin_addr.s_addr = htonl(INADDR_ANY);
786d11e3645SMatteo Riondato 					res->ai_addr = (struct sockaddr*) sin;
787d11e3645SMatteo Riondato 					res->ai_addrlen = (socklen_t)
7888fb6ad5dSRick Macklem 					    sizeof(struct sockaddr_in);
789d11e3645SMatteo Riondato 					break;
790d11e3645SMatteo Riondato 				case AF_INET6:
791d11e3645SMatteo Riondato 					sin6 = malloc(sizeof(struct sockaddr_in6));
79289ca9145SSimon L. B. Nielsen 					if (sin6 == NULL)
793d11e3645SMatteo Riondato 						out_of_mem();
794d11e3645SMatteo Riondato 					sin6->sin6_family = AF_INET6;
795d11e3645SMatteo Riondato 					sin6->sin6_port = htons(0);
796d11e3645SMatteo Riondato 					sin6->sin6_addr = in6addr_any;
797d11e3645SMatteo Riondato 					res->ai_addr = (struct sockaddr*) sin6;
798d11e3645SMatteo Riondato 					res->ai_addrlen = (socklen_t)
7998fb6ad5dSRick Macklem 					    sizeof(struct sockaddr_in6);
800d11e3645SMatteo Riondato 					break;
801d11e3645SMatteo Riondato 				default:
8028fb6ad5dSRick Macklem 					syslog(LOG_ERR, "bad addr fam %d",
8038fb6ad5dSRick Macklem 					    res->ai_family);
8048fb6ad5dSRick Macklem 					exit(1);
805d11e3645SMatteo Riondato 				}
806d11e3645SMatteo Riondato 			} else {
807d11e3645SMatteo Riondato 				if ((aicode = getaddrinfo(NULL, svcport_str,
808d11e3645SMatteo Riondato 				    &hints, &res)) != 0) {
809d11e3645SMatteo Riondato 					syslog(LOG_ERR,
810d11e3645SMatteo Riondato 					    "cannot get local address for %s: %s",
811d11e3645SMatteo Riondato 					    nconf->nc_netid,
812d11e3645SMatteo Riondato 					    gai_strerror(aicode));
8138fb6ad5dSRick Macklem 					close(fd);
814d11e3645SMatteo Riondato 					continue;
815d11e3645SMatteo Riondato 				}
816d11e3645SMatteo Riondato 			}
817d11e3645SMatteo Riondato 		} else {
818d11e3645SMatteo Riondato 			if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
819d11e3645SMatteo Riondato 			    &hints, &res)) != 0) {
820d11e3645SMatteo Riondato 				syslog(LOG_ERR,
821d11e3645SMatteo Riondato 				    "cannot get local address for %s: %s",
822d11e3645SMatteo Riondato 				    nconf->nc_netid, gai_strerror(aicode));
8238fb6ad5dSRick Macklem 				close(fd);
824d11e3645SMatteo Riondato 				continue;
825d11e3645SMatteo Riondato 			}
826d11e3645SMatteo Riondato 		}
827d11e3645SMatteo Riondato 
8288fb6ad5dSRick Macklem 		/* Store the fd. */
8298fb6ad5dSRick Macklem 		sock_fd[sock_fdcnt - 1] = fd;
8308fb6ad5dSRick Macklem 
8318fb6ad5dSRick Macklem 		/* Now, attempt the bind. */
832d11e3645SMatteo Riondato 		r = bindresvport_sa(fd, res->ai_addr);
833d11e3645SMatteo Riondato 		if (r != 0) {
8348fb6ad5dSRick Macklem 			if (errno == EADDRINUSE && mallocd_svcport != 0) {
8358fb6ad5dSRick Macklem 				if (mallocd_res != 0) {
8368fb6ad5dSRick Macklem 					free(res->ai_addr);
8378fb6ad5dSRick Macklem 					free(res);
8388fb6ad5dSRick Macklem 				} else
8398fb6ad5dSRick Macklem 					freeaddrinfo(res);
8408fb6ad5dSRick Macklem 				return (-1);
8418fb6ad5dSRick Macklem 			}
842d11e3645SMatteo Riondato 			syslog(LOG_ERR, "bindresvport_sa: %m");
843d11e3645SMatteo Riondato 			exit(1);
844d11e3645SMatteo Riondato 		}
845d11e3645SMatteo Riondato 
8468fb6ad5dSRick Macklem 		if (svcport_str == NULL) {
8478fb6ad5dSRick Macklem 			svcport_str = malloc(NI_MAXSERV * sizeof(char));
8488fb6ad5dSRick Macklem 			if (svcport_str == NULL)
8498fb6ad5dSRick Macklem 				out_of_mem();
8508fb6ad5dSRick Macklem 			mallocd_svcport = 1;
8518fb6ad5dSRick Macklem 
8528fb6ad5dSRick Macklem 			if (getnameinfo(res->ai_addr,
8538fb6ad5dSRick Macklem 			    res->ai_addr->sa_len, NULL, NI_MAXHOST,
8548fb6ad5dSRick Macklem 			    svcport_str, NI_MAXSERV * sizeof(char),
8558fb6ad5dSRick Macklem 			    NI_NUMERICHOST | NI_NUMERICSERV))
8568fb6ad5dSRick Macklem 				errx(1, "Cannot get port number");
8578fb6ad5dSRick Macklem 		}
8588fb6ad5dSRick Macklem 		if (mallocd_res != 0) {
8598fb6ad5dSRick Macklem 			free(res->ai_addr);
8608fb6ad5dSRick Macklem 			free(res);
8618fb6ad5dSRick Macklem 		} else
8628fb6ad5dSRick Macklem 			freeaddrinfo(res);
8638fb6ad5dSRick Macklem 		res = NULL;
8648fb6ad5dSRick Macklem 	}
8658fb6ad5dSRick Macklem 	return (0);
8668fb6ad5dSRick Macklem }
8678fb6ad5dSRick Macklem 
8688fb6ad5dSRick Macklem /*
8698fb6ad5dSRick Macklem  * Called after all the create_service() calls have succeeded, to complete
8708fb6ad5dSRick Macklem  * the setup and registration.
8718fb6ad5dSRick Macklem  */
8728fb6ad5dSRick Macklem static void
8738fb6ad5dSRick Macklem complete_service(struct netconfig *nconf, char *port_str)
8748fb6ad5dSRick Macklem {
8758fb6ad5dSRick Macklem 	struct addrinfo hints, *res = NULL;
8768fb6ad5dSRick Macklem 	struct __rpc_sockinfo si;
8778fb6ad5dSRick Macklem 	struct netbuf servaddr;
8788fb6ad5dSRick Macklem 	SVCXPRT	*transp = NULL;
8798fb6ad5dSRick Macklem 	int aicode, fd, nhostsbak;
8808fb6ad5dSRick Macklem 	int registered = 0;
8818fb6ad5dSRick Macklem 
8828fb6ad5dSRick Macklem 	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
8838fb6ad5dSRick Macklem 	    (nconf->nc_semantics != NC_TPI_COTS) &&
8848fb6ad5dSRick Macklem 	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
8858fb6ad5dSRick Macklem 		return;	/* not my type */
8868fb6ad5dSRick Macklem 
8878fb6ad5dSRick Macklem 	/*
8888fb6ad5dSRick Macklem 	 * XXX - using RPC library internal functions.
8898fb6ad5dSRick Macklem 	 */
8908fb6ad5dSRick Macklem 	if (!__rpc_nconf2sockinfo(nconf, &si)) {
8918fb6ad5dSRick Macklem 		syslog(LOG_ERR, "cannot get information for %s",
8928fb6ad5dSRick Macklem 		    nconf->nc_netid);
8938fb6ad5dSRick Macklem 		return;
8948fb6ad5dSRick Macklem 	}
8958fb6ad5dSRick Macklem 
8968fb6ad5dSRick Macklem 	nhostsbak = nhosts;
8978fb6ad5dSRick Macklem 	while (nhostsbak > 0) {
8988fb6ad5dSRick Macklem 		--nhostsbak;
8998fb6ad5dSRick Macklem 		if (sock_fdpos >= sock_fdcnt) {
9008fb6ad5dSRick Macklem 			/* Should never happen. */
9018fb6ad5dSRick Macklem 			syslog(LOG_ERR, "Ran out of socket fd's");
9028fb6ad5dSRick Macklem 			return;
9038fb6ad5dSRick Macklem 		}
9048fb6ad5dSRick Macklem 		fd = sock_fd[sock_fdpos++];
9058fb6ad5dSRick Macklem 		if (fd < 0)
9068fb6ad5dSRick Macklem 			continue;
9078fb6ad5dSRick Macklem 
908d11e3645SMatteo Riondato 		if (nconf->nc_semantics != NC_TPI_CLTS)
909d11e3645SMatteo Riondato 			listen(fd, SOMAXCONN);
910d11e3645SMatteo Riondato 
911d11e3645SMatteo Riondato 		if (nconf->nc_semantics == NC_TPI_CLTS )
912d11e3645SMatteo Riondato 			transp = svc_dg_create(fd, 0, 0);
913d11e3645SMatteo Riondato 		else
914d11e3645SMatteo Riondato 			transp = svc_vc_create(fd, RPC_MAXDATASIZE,
915d11e3645SMatteo Riondato 			    RPC_MAXDATASIZE);
916d11e3645SMatteo Riondato 
917d11e3645SMatteo Riondato 		if (transp != (SVCXPRT *) NULL) {
9180775314bSDoug Rabson 			if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
919d11e3645SMatteo Riondato 			    NULL))
920d11e3645SMatteo Riondato 				syslog(LOG_ERR,
9210775314bSDoug Rabson 				    "can't register %s MOUNTVERS service",
922d11e3645SMatteo Riondato 				    nconf->nc_netid);
923d11e3645SMatteo Riondato 			if (!force_v2) {
9240775314bSDoug Rabson 				if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
925d11e3645SMatteo Riondato 				    mntsrv, NULL))
926d11e3645SMatteo Riondato 					syslog(LOG_ERR,
9270775314bSDoug Rabson 					    "can't register %s MOUNTVERS3 service",
928d11e3645SMatteo Riondato 					    nconf->nc_netid);
929d11e3645SMatteo Riondato 			}
930d11e3645SMatteo Riondato 		} else
931d11e3645SMatteo Riondato 			syslog(LOG_WARNING, "can't create %s services",
932d11e3645SMatteo Riondato 			    nconf->nc_netid);
933d11e3645SMatteo Riondato 
934d11e3645SMatteo Riondato 		if (registered == 0) {
935d11e3645SMatteo Riondato 			registered = 1;
936d11e3645SMatteo Riondato 			memset(&hints, 0, sizeof hints);
937d11e3645SMatteo Riondato 			hints.ai_flags = AI_PASSIVE;
938d11e3645SMatteo Riondato 			hints.ai_family = si.si_af;
939d11e3645SMatteo Riondato 			hints.ai_socktype = si.si_socktype;
940d11e3645SMatteo Riondato 			hints.ai_protocol = si.si_proto;
941d11e3645SMatteo Riondato 
9428fb6ad5dSRick Macklem 			if ((aicode = getaddrinfo(NULL, port_str, &hints,
943d11e3645SMatteo Riondato 			    &res)) != 0) {
944d11e3645SMatteo Riondato 				syslog(LOG_ERR, "cannot get local address: %s",
945d11e3645SMatteo Riondato 				    gai_strerror(aicode));
946d11e3645SMatteo Riondato 				exit(1);
947d11e3645SMatteo Riondato 			}
948d11e3645SMatteo Riondato 
949d11e3645SMatteo Riondato 			servaddr.buf = malloc(res->ai_addrlen);
950d11e3645SMatteo Riondato 			memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
951d11e3645SMatteo Riondato 			servaddr.len = res->ai_addrlen;
952d11e3645SMatteo Riondato 
9530775314bSDoug Rabson 			rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
9540775314bSDoug Rabson 			rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
955d11e3645SMatteo Riondato 
956d11e3645SMatteo Riondato 			xcreated++;
957d11e3645SMatteo Riondato 			freeaddrinfo(res);
958d11e3645SMatteo Riondato 		}
959d11e3645SMatteo Riondato 	} /* end while */
960d11e3645SMatteo Riondato }
961d11e3645SMatteo Riondato 
9628fb6ad5dSRick Macklem /*
9638fb6ad5dSRick Macklem  * Clear out sockets after a failure to bind one of them, so that the
9648fb6ad5dSRick Macklem  * cycle of socket creation/binding can start anew.
9658fb6ad5dSRick Macklem  */
9668fb6ad5dSRick Macklem static void
9678fb6ad5dSRick Macklem clearout_service(void)
9688fb6ad5dSRick Macklem {
9698fb6ad5dSRick Macklem 	int i;
9708fb6ad5dSRick Macklem 
9718fb6ad5dSRick Macklem 	for (i = 0; i < sock_fdcnt; i++) {
9728fb6ad5dSRick Macklem 		if (sock_fd[i] >= 0) {
9738fb6ad5dSRick Macklem 			shutdown(sock_fd[i], SHUT_RDWR);
9748fb6ad5dSRick Macklem 			close(sock_fd[i]);
9758fb6ad5dSRick Macklem 		}
9768fb6ad5dSRick Macklem 	}
9778fb6ad5dSRick Macklem }
9788fb6ad5dSRick Macklem 
97974853402SPhilippe Charnier static void
980a7a7d96cSPhilippe Charnier usage(void)
98174853402SPhilippe Charnier {
98274853402SPhilippe Charnier 	fprintf(stderr,
9832179ae1eSRick Macklem 		"usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
984c548eb5cSRick Macklem 		"[-S] [-h <bindip>] [export_file ...]\n");
9858fae3551SRodney W. Grimes 	exit(1);
9868fae3551SRodney W. Grimes }
9878fae3551SRodney W. Grimes 
9888fae3551SRodney W. Grimes /*
9898fae3551SRodney W. Grimes  * The mount rpc service
9908fae3551SRodney W. Grimes  */
9918fae3551SRodney W. Grimes void
992a7a7d96cSPhilippe Charnier mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
9938fae3551SRodney W. Grimes {
9948fae3551SRodney W. Grimes 	struct exportlist *ep;
9958fae3551SRodney W. Grimes 	struct dirlist *dp;
996a62dc406SDoug Rabson 	struct fhreturn fhr;
9978fae3551SRodney W. Grimes 	struct stat stb;
9988fae3551SRodney W. Grimes 	struct statfs fsb;
9998360efbdSAlfred Perlstein 	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
10008360efbdSAlfred Perlstein 	int lookup_failed = 1;
10018360efbdSAlfred Perlstein 	struct sockaddr *saddr;
1002a62dc406SDoug Rabson 	u_short sport;
10030775314bSDoug Rabson 	char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1004e90cdb54SGuido van Rooij 	int bad = 0, defset, hostset;
1005a62dc406SDoug Rabson 	sigset_t sighup_mask;
1006c3f86a25SRick Macklem 	int numsecflavors, *secflavorsp;
10078fae3551SRodney W. Grimes 
1008a62dc406SDoug Rabson 	sigemptyset(&sighup_mask);
1009a62dc406SDoug Rabson 	sigaddset(&sighup_mask, SIGHUP);
10108360efbdSAlfred Perlstein 	saddr = svc_getrpccaller(transp)->buf;
10118360efbdSAlfred Perlstein 	switch (saddr->sa_family) {
10128360efbdSAlfred Perlstein 	case AF_INET6:
101301709abfSIan Dowse 		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
10148360efbdSAlfred Perlstein 		break;
10158360efbdSAlfred Perlstein 	case AF_INET:
101601709abfSIan Dowse 		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
10178360efbdSAlfred Perlstein 		break;
10188360efbdSAlfred Perlstein 	default:
10198360efbdSAlfred Perlstein 		syslog(LOG_ERR, "request from unknown address family");
10208360efbdSAlfred Perlstein 		return;
10218360efbdSAlfred Perlstein 	}
10228360efbdSAlfred Perlstein 	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
10238360efbdSAlfred Perlstein 	    NULL, 0, 0);
10248360efbdSAlfred Perlstein 	getnameinfo(saddr, saddr->sa_len, numerichost,
10258360efbdSAlfred Perlstein 	    sizeof numerichost, NULL, 0, NI_NUMERICHOST);
10268fae3551SRodney W. Grimes 	switch (rqstp->rq_proc) {
10278fae3551SRodney W. Grimes 	case NULLPROC:
1028389b8446SPeter Wemm 		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
102974853402SPhilippe Charnier 			syslog(LOG_ERR, "can't send reply");
10308fae3551SRodney W. Grimes 		return;
10310775314bSDoug Rabson 	case MOUNTPROC_MNT:
1032a62dc406SDoug Rabson 		if (sport >= IPPORT_RESERVED && resvport_only) {
1033f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1034f51631d7SGuido van Rooij 			    "mount request from %s from unprivileged port",
10358360efbdSAlfred Perlstein 			    numerichost);
10368fae3551SRodney W. Grimes 			svcerr_weakauth(transp);
10378fae3551SRodney W. Grimes 			return;
10388fae3551SRodney W. Grimes 		}
1039389b8446SPeter Wemm 		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1040f51631d7SGuido van Rooij 			syslog(LOG_NOTICE, "undecodable mount request from %s",
10418360efbdSAlfred Perlstein 			    numerichost);
10428fae3551SRodney W. Grimes 			svcerr_decode(transp);
10438fae3551SRodney W. Grimes 			return;
10448fae3551SRodney W. Grimes 		}
10458fae3551SRodney W. Grimes 
10468fae3551SRodney W. Grimes 		/*
10478fae3551SRodney W. Grimes 		 * Get the real pathname and make sure it is a directory
1048a62dc406SDoug Rabson 		 * or a regular file if the -r option was specified
1049a62dc406SDoug Rabson 		 * and it exists.
10508fae3551SRodney W. Grimes 		 */
1051cb479b11SAlfred Perlstein 		if (realpath(rpcpath, dirpath) == NULL ||
10528fae3551SRodney W. Grimes 		    stat(dirpath, &stb) < 0 ||
1053a62dc406SDoug Rabson 		    (!S_ISDIR(stb.st_mode) &&
1054a62dc406SDoug Rabson 		    (dir_only || !S_ISREG(stb.st_mode))) ||
10558fae3551SRodney W. Grimes 		    statfs(dirpath, &fsb) < 0) {
10568fae3551SRodney W. Grimes 			chdir("/");	/* Just in case realpath doesn't */
1057f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
105874853402SPhilippe Charnier 			    "mount request from %s for non existent path %s",
10598360efbdSAlfred Perlstein 			    numerichost, dirpath);
10608fae3551SRodney W. Grimes 			if (debug)
106174853402SPhilippe Charnier 				warnx("stat failed on %s", dirpath);
1062e90cdb54SGuido van Rooij 			bad = ENOENT;	/* We will send error reply later */
10638fae3551SRodney W. Grimes 		}
10648fae3551SRodney W. Grimes 
10658fae3551SRodney W. Grimes 		/* Check in the exports list */
1066a62dc406SDoug Rabson 		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
10678fae3551SRodney W. Grimes 		ep = ex_search(&fsb.f_fsid);
1068a62dc406SDoug Rabson 		hostset = defset = 0;
1069c3f86a25SRick Macklem 		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1070c3f86a25SRick Macklem 		    &numsecflavors, &secflavorsp) ||
10718fae3551SRodney W. Grimes 		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1072c3f86a25SRick Macklem 		      chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1073c3f86a25SRick Macklem 		       &secflavorsp)) ||
10748fae3551SRodney W. Grimes 		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
10758fae3551SRodney W. Grimes 		     scan_tree(ep->ex_dirl, saddr) == 0))) {
1076e90cdb54SGuido van Rooij 			if (bad) {
1077389b8446SPeter Wemm 				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1078e90cdb54SGuido van Rooij 				    (caddr_t)&bad))
107974853402SPhilippe Charnier 					syslog(LOG_ERR, "can't send reply");
1080e90cdb54SGuido van Rooij 				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1081e90cdb54SGuido van Rooij 				return;
1082e90cdb54SGuido van Rooij 			}
1083c3f86a25SRick Macklem 			if (hostset & DP_HOSTSET) {
1084a62dc406SDoug Rabson 				fhr.fhr_flag = hostset;
1085c3f86a25SRick Macklem 				fhr.fhr_numsecflavors = numsecflavors;
1086c3f86a25SRick Macklem 				fhr.fhr_secflavors = secflavorsp;
1087c3f86a25SRick Macklem 			} else {
1088a62dc406SDoug Rabson 				fhr.fhr_flag = defset;
1089c3f86a25SRick Macklem 				fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1090c3f86a25SRick Macklem 				fhr.fhr_secflavors = ep->ex_defsecflavors;
1091c3f86a25SRick Macklem 			}
1092a62dc406SDoug Rabson 			fhr.fhr_vers = rqstp->rq_vers;
10938fae3551SRodney W. Grimes 			/* Get the file handle */
109487564113SPeter Wemm 			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1095a62dc406SDoug Rabson 			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
10968fae3551SRodney W. Grimes 				bad = errno;
109774853402SPhilippe Charnier 				syslog(LOG_ERR, "can't get fh for %s", dirpath);
1098389b8446SPeter Wemm 				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
10998fae3551SRodney W. Grimes 				    (caddr_t)&bad))
110074853402SPhilippe Charnier 					syslog(LOG_ERR, "can't send reply");
1101a62dc406SDoug Rabson 				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
11028fae3551SRodney W. Grimes 				return;
11038fae3551SRodney W. Grimes 			}
1104389b8446SPeter Wemm 			if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1105389b8446SPeter Wemm 			    (caddr_t)&fhr))
110674853402SPhilippe Charnier 				syslog(LOG_ERR, "can't send reply");
11078360efbdSAlfred Perlstein 			if (!lookup_failed)
11088360efbdSAlfred Perlstein 				add_mlist(host, dirpath);
11098fae3551SRodney W. Grimes 			else
11108360efbdSAlfred Perlstein 				add_mlist(numerichost, dirpath);
11118fae3551SRodney W. Grimes 			if (debug)
111274853402SPhilippe Charnier 				warnx("mount successful");
1113c903443aSPeter Wemm 			if (dolog)
1114f51631d7SGuido van Rooij 				syslog(LOG_NOTICE,
1115f51631d7SGuido van Rooij 				    "mount request succeeded from %s for %s",
11168360efbdSAlfred Perlstein 				    numerichost, dirpath);
1117f51631d7SGuido van Rooij 		} else {
11188fae3551SRodney W. Grimes 			bad = EACCES;
1119f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1120f51631d7SGuido van Rooij 			    "mount request denied from %s for %s",
11218360efbdSAlfred Perlstein 			    numerichost, dirpath);
1122f51631d7SGuido van Rooij 		}
1123e90cdb54SGuido van Rooij 
1124389b8446SPeter Wemm 		if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1125389b8446SPeter Wemm 		    (caddr_t)&bad))
112674853402SPhilippe Charnier 			syslog(LOG_ERR, "can't send reply");
1127a62dc406SDoug Rabson 		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
11288fae3551SRodney W. Grimes 		return;
11290775314bSDoug Rabson 	case MOUNTPROC_DUMP:
1130389b8446SPeter Wemm 		if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
113174853402SPhilippe Charnier 			syslog(LOG_ERR, "can't send reply");
1132c903443aSPeter Wemm 		else if (dolog)
1133f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1134f51631d7SGuido van Rooij 			    "dump request succeeded from %s",
11358360efbdSAlfred Perlstein 			    numerichost);
11368fae3551SRodney W. Grimes 		return;
11370775314bSDoug Rabson 	case MOUNTPROC_UMNT:
1138a62dc406SDoug Rabson 		if (sport >= IPPORT_RESERVED && resvport_only) {
1139f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1140f51631d7SGuido van Rooij 			    "umount request from %s from unprivileged port",
11418360efbdSAlfred Perlstein 			    numerichost);
11428fae3551SRodney W. Grimes 			svcerr_weakauth(transp);
11438fae3551SRodney W. Grimes 			return;
11448fae3551SRodney W. Grimes 		}
1145389b8446SPeter Wemm 		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1146f51631d7SGuido van Rooij 			syslog(LOG_NOTICE, "undecodable umount request from %s",
11478360efbdSAlfred Perlstein 			    numerichost);
11488fae3551SRodney W. Grimes 			svcerr_decode(transp);
11498fae3551SRodney W. Grimes 			return;
11508fae3551SRodney W. Grimes 		}
1151cb479b11SAlfred Perlstein 		if (realpath(rpcpath, dirpath) == NULL) {
1152cb479b11SAlfred Perlstein 			syslog(LOG_NOTICE, "umount request from %s "
1153cb479b11SAlfred Perlstein 			    "for non existent path %s",
11548360efbdSAlfred Perlstein 			    numerichost, dirpath);
1155cb479b11SAlfred Perlstein 		}
1156389b8446SPeter Wemm 		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
115774853402SPhilippe Charnier 			syslog(LOG_ERR, "can't send reply");
11588360efbdSAlfred Perlstein 		if (!lookup_failed)
115901709abfSIan Dowse 			del_mlist(host, dirpath);
116001709abfSIan Dowse 		del_mlist(numerichost, dirpath);
1161c903443aSPeter Wemm 		if (dolog)
1162f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1163f51631d7SGuido van Rooij 			    "umount request succeeded from %s for %s",
11648360efbdSAlfred Perlstein 			    numerichost, dirpath);
11658fae3551SRodney W. Grimes 		return;
11660775314bSDoug Rabson 	case MOUNTPROC_UMNTALL:
1167a62dc406SDoug Rabson 		if (sport >= IPPORT_RESERVED && resvport_only) {
1168f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1169f51631d7SGuido van Rooij 			    "umountall request from %s from unprivileged port",
11708360efbdSAlfred Perlstein 			    numerichost);
11718fae3551SRodney W. Grimes 			svcerr_weakauth(transp);
11728fae3551SRodney W. Grimes 			return;
11738fae3551SRodney W. Grimes 		}
1174389b8446SPeter Wemm 		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
117574853402SPhilippe Charnier 			syslog(LOG_ERR, "can't send reply");
11768360efbdSAlfred Perlstein 		if (!lookup_failed)
117701709abfSIan Dowse 			del_mlist(host, NULL);
117801709abfSIan Dowse 		del_mlist(numerichost, NULL);
1179c903443aSPeter Wemm 		if (dolog)
1180f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1181f51631d7SGuido van Rooij 			    "umountall request succeeded from %s",
11828360efbdSAlfred Perlstein 			    numerichost);
11838fae3551SRodney W. Grimes 		return;
11840775314bSDoug Rabson 	case MOUNTPROC_EXPORT:
1185389b8446SPeter Wemm 		if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1186389b8446SPeter Wemm 			if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1187389b8446SPeter Wemm 			    (caddr_t)NULL))
118874853402SPhilippe Charnier 				syslog(LOG_ERR, "can't send reply");
1189c903443aSPeter Wemm 		if (dolog)
1190f51631d7SGuido van Rooij 			syslog(LOG_NOTICE,
1191f51631d7SGuido van Rooij 			    "export request succeeded from %s",
11928360efbdSAlfred Perlstein 			    numerichost);
11938fae3551SRodney W. Grimes 		return;
11948fae3551SRodney W. Grimes 	default:
11958fae3551SRodney W. Grimes 		svcerr_noproc(transp);
11968fae3551SRodney W. Grimes 		return;
11978fae3551SRodney W. Grimes 	}
11988fae3551SRodney W. Grimes }
11998fae3551SRodney W. Grimes 
12008fae3551SRodney W. Grimes /*
12018fae3551SRodney W. Grimes  * Xdr conversion for a dirpath string
12028fae3551SRodney W. Grimes  */
120319c46d8cSEdward Tomasz Napierala static int
1204a7a7d96cSPhilippe Charnier xdr_dir(XDR *xdrsp, char *dirp)
12058fae3551SRodney W. Grimes {
12060775314bSDoug Rabson 	return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
12078fae3551SRodney W. Grimes }
12088fae3551SRodney W. Grimes 
12098fae3551SRodney W. Grimes /*
1210a62dc406SDoug Rabson  * Xdr routine to generate file handle reply
12118fae3551SRodney W. Grimes  */
121219c46d8cSEdward Tomasz Napierala static int
1213a7a7d96cSPhilippe Charnier xdr_fhs(XDR *xdrsp, caddr_t cp)
12148fae3551SRodney W. Grimes {
12153d438ad6SDavid E. O'Brien 	struct fhreturn *fhrp = (struct fhreturn *)cp;
1216a62dc406SDoug Rabson 	u_long ok = 0, len, auth;
1217a9148abdSDoug Rabson 	int i;
12188fae3551SRodney W. Grimes 
12198fae3551SRodney W. Grimes 	if (!xdr_long(xdrsp, &ok))
12208fae3551SRodney W. Grimes 		return (0);
1221a62dc406SDoug Rabson 	switch (fhrp->fhr_vers) {
1222a62dc406SDoug Rabson 	case 1:
1223a62dc406SDoug Rabson 		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1224a62dc406SDoug Rabson 	case 3:
1225a62dc406SDoug Rabson 		len = NFSX_V3FH;
1226a62dc406SDoug Rabson 		if (!xdr_long(xdrsp, &len))
1227a62dc406SDoug Rabson 			return (0);
1228a62dc406SDoug Rabson 		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1229a62dc406SDoug Rabson 			return (0);
1230a9148abdSDoug Rabson 		if (fhrp->fhr_numsecflavors) {
1231a9148abdSDoug Rabson 			if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1232a9148abdSDoug Rabson 				return (0);
1233a9148abdSDoug Rabson 			for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1234a9148abdSDoug Rabson 				if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1235a9148abdSDoug Rabson 					return (0);
1236a9148abdSDoug Rabson 			return (1);
1237a9148abdSDoug Rabson 		} else {
1238a9148abdSDoug Rabson 			auth = AUTH_SYS;
1239a62dc406SDoug Rabson 			len = 1;
1240a62dc406SDoug Rabson 			if (!xdr_long(xdrsp, &len))
1241a62dc406SDoug Rabson 				return (0);
1242a62dc406SDoug Rabson 			return (xdr_long(xdrsp, &auth));
1243a9148abdSDoug Rabson 		}
124480c7cc1cSPedro F. Giffuni 	}
1245a62dc406SDoug Rabson 	return (0);
12468fae3551SRodney W. Grimes }
12478fae3551SRodney W. Grimes 
124819c46d8cSEdward Tomasz Napierala static int
1249a7a7d96cSPhilippe Charnier xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
12508fae3551SRodney W. Grimes {
12518fae3551SRodney W. Grimes 	struct mountlist *mlp;
12528fae3551SRodney W. Grimes 	int true = 1;
12538fae3551SRodney W. Grimes 	int false = 0;
12548fae3551SRodney W. Grimes 	char *strp;
12558fae3551SRodney W. Grimes 
12568fae3551SRodney W. Grimes 	mlp = mlhead;
12578fae3551SRodney W. Grimes 	while (mlp) {
12588fae3551SRodney W. Grimes 		if (!xdr_bool(xdrsp, &true))
12598fae3551SRodney W. Grimes 			return (0);
12608fae3551SRodney W. Grimes 		strp = &mlp->ml_host[0];
12610775314bSDoug Rabson 		if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
12628fae3551SRodney W. Grimes 			return (0);
12638fae3551SRodney W. Grimes 		strp = &mlp->ml_dirp[0];
12640775314bSDoug Rabson 		if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
12658fae3551SRodney W. Grimes 			return (0);
12668fae3551SRodney W. Grimes 		mlp = mlp->ml_next;
12678fae3551SRodney W. Grimes 	}
12688fae3551SRodney W. Grimes 	if (!xdr_bool(xdrsp, &false))
12698fae3551SRodney W. Grimes 		return (0);
12708fae3551SRodney W. Grimes 	return (1);
12718fae3551SRodney W. Grimes }
12728fae3551SRodney W. Grimes 
12738fae3551SRodney W. Grimes /*
12748fae3551SRodney W. Grimes  * Xdr conversion for export list
12758fae3551SRodney W. Grimes  */
127619c46d8cSEdward Tomasz Napierala static int
1277a7a7d96cSPhilippe Charnier xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
12788fae3551SRodney W. Grimes {
12798fae3551SRodney W. Grimes 	struct exportlist *ep;
12808fae3551SRodney W. Grimes 	int false = 0;
1281a62dc406SDoug Rabson 	int putdef;
1282a62dc406SDoug Rabson 	sigset_t sighup_mask;
12838fae3551SRodney W. Grimes 
1284a62dc406SDoug Rabson 	sigemptyset(&sighup_mask);
1285a62dc406SDoug Rabson 	sigaddset(&sighup_mask, SIGHUP);
1286a62dc406SDoug Rabson 	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
12878fae3551SRodney W. Grimes 	ep = exphead;
12888fae3551SRodney W. Grimes 	while (ep) {
12898fae3551SRodney W. Grimes 		putdef = 0;
129091acb349SAlfred Perlstein 		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
129191acb349SAlfred Perlstein 			       &putdef, brief))
12928fae3551SRodney W. Grimes 			goto errout;
12938fae3551SRodney W. Grimes 		if (ep->ex_defdir && putdef == 0 &&
12948fae3551SRodney W. Grimes 			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
129591acb349SAlfred Perlstein 			&putdef, brief))
12968fae3551SRodney W. Grimes 			goto errout;
12978fae3551SRodney W. Grimes 		ep = ep->ex_next;
12988fae3551SRodney W. Grimes 	}
1299a62dc406SDoug Rabson 	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
13008fae3551SRodney W. Grimes 	if (!xdr_bool(xdrsp, &false))
13018fae3551SRodney W. Grimes 		return (0);
13028fae3551SRodney W. Grimes 	return (1);
13038fae3551SRodney W. Grimes errout:
1304a62dc406SDoug Rabson 	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
13058fae3551SRodney W. Grimes 	return (0);
13068fae3551SRodney W. Grimes }
13078fae3551SRodney W. Grimes 
13088fae3551SRodney W. Grimes /*
13098fae3551SRodney W. Grimes  * Called from xdr_explist() to traverse the tree and export the
13108fae3551SRodney W. Grimes  * directory paths.
13118fae3551SRodney W. Grimes  */
131219c46d8cSEdward Tomasz Napierala static int
1313a7a7d96cSPhilippe Charnier put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1314a7a7d96cSPhilippe Charnier 	int brief)
13158fae3551SRodney W. Grimes {
13168fae3551SRodney W. Grimes 	struct grouplist *grp;
13178fae3551SRodney W. Grimes 	struct hostlist *hp;
13188fae3551SRodney W. Grimes 	int true = 1;
13198fae3551SRodney W. Grimes 	int false = 0;
13208fae3551SRodney W. Grimes 	int gotalldir = 0;
13218fae3551SRodney W. Grimes 	char *strp;
13228fae3551SRodney W. Grimes 
13238fae3551SRodney W. Grimes 	if (dp) {
132491acb349SAlfred Perlstein 		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
13258fae3551SRodney W. Grimes 			return (1);
13268fae3551SRodney W. Grimes 		if (!xdr_bool(xdrsp, &true))
13278fae3551SRodney W. Grimes 			return (1);
13288fae3551SRodney W. Grimes 		strp = dp->dp_dirp;
13290775314bSDoug Rabson 		if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
13308fae3551SRodney W. Grimes 			return (1);
13318fae3551SRodney W. Grimes 		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
13328fae3551SRodney W. Grimes 			gotalldir = 1;
13338fae3551SRodney W. Grimes 			*putdefp = 1;
13348fae3551SRodney W. Grimes 		}
133591acb349SAlfred Perlstein 		if (brief) {
133691acb349SAlfred Perlstein 			if (!xdr_bool(xdrsp, &true))
133791acb349SAlfred Perlstein 				return (1);
133891acb349SAlfred Perlstein 			strp = "(...)";
13390775314bSDoug Rabson 			if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
134091acb349SAlfred Perlstein 				return (1);
134191acb349SAlfred Perlstein 		} else if ((dp->dp_flag & DP_DEFSET) == 0 &&
13428fae3551SRodney W. Grimes 		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
13438fae3551SRodney W. Grimes 			hp = dp->dp_hosts;
13448fae3551SRodney W. Grimes 			while (hp) {
13458fae3551SRodney W. Grimes 				grp = hp->ht_grp;
13468fae3551SRodney W. Grimes 				if (grp->gr_type == GT_HOST) {
13478fae3551SRodney W. Grimes 					if (!xdr_bool(xdrsp, &true))
13488fae3551SRodney W. Grimes 						return (1);
13498360efbdSAlfred Perlstein 					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
13508fae3551SRodney W. Grimes 					if (!xdr_string(xdrsp, &strp,
13510775314bSDoug Rabson 					    MNTNAMLEN))
13528fae3551SRodney W. Grimes 						return (1);
13538fae3551SRodney W. Grimes 				} else if (grp->gr_type == GT_NET) {
13548fae3551SRodney W. Grimes 					if (!xdr_bool(xdrsp, &true))
13558fae3551SRodney W. Grimes 						return (1);
13568fae3551SRodney W. Grimes 					strp = grp->gr_ptr.gt_net.nt_name;
13578fae3551SRodney W. Grimes 					if (!xdr_string(xdrsp, &strp,
13580775314bSDoug Rabson 					    MNTNAMLEN))
13598fae3551SRodney W. Grimes 						return (1);
13608fae3551SRodney W. Grimes 				}
13618fae3551SRodney W. Grimes 				hp = hp->ht_next;
13628fae3551SRodney W. Grimes 				if (gotalldir && hp == (struct hostlist *)NULL) {
13638fae3551SRodney W. Grimes 					hp = adp->dp_hosts;
13648fae3551SRodney W. Grimes 					gotalldir = 0;
13658fae3551SRodney W. Grimes 				}
13668fae3551SRodney W. Grimes 			}
13678fae3551SRodney W. Grimes 		}
13688fae3551SRodney W. Grimes 		if (!xdr_bool(xdrsp, &false))
13698fae3551SRodney W. Grimes 			return (1);
137091acb349SAlfred Perlstein 		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
13718fae3551SRodney W. Grimes 			return (1);
13728fae3551SRodney W. Grimes 	}
13738fae3551SRodney W. Grimes 	return (0);
13748fae3551SRodney W. Grimes }
13758fae3551SRodney W. Grimes 
137619c46d8cSEdward Tomasz Napierala static int
1377a7a7d96cSPhilippe Charnier xdr_explist(XDR *xdrsp, caddr_t cp)
137891acb349SAlfred Perlstein {
137991acb349SAlfred Perlstein 
138091acb349SAlfred Perlstein 	return xdr_explist_common(xdrsp, cp, 0);
138191acb349SAlfred Perlstein }
138291acb349SAlfred Perlstein 
138319c46d8cSEdward Tomasz Napierala static int
1384a7a7d96cSPhilippe Charnier xdr_explist_brief(XDR *xdrsp, caddr_t cp)
138591acb349SAlfred Perlstein {
138691acb349SAlfred Perlstein 
138791acb349SAlfred Perlstein 	return xdr_explist_common(xdrsp, cp, 1);
138891acb349SAlfred Perlstein }
138991acb349SAlfred Perlstein 
139019c46d8cSEdward Tomasz Napierala static char *line;
139119c46d8cSEdward Tomasz Napierala static size_t linesize;
139219c46d8cSEdward Tomasz Napierala static FILE *exp_file;
13938fae3551SRodney W. Grimes 
13948fae3551SRodney W. Grimes /*
139596968c22SPawel Jakub Dawidek  * Get the export list from one, currently open file
13968fae3551SRodney W. Grimes  */
139796968c22SPawel Jakub Dawidek static void
1398a7a7d96cSPhilippe Charnier get_exportlist_one(void)
13998fae3551SRodney W. Grimes {
14008fae3551SRodney W. Grimes 	struct exportlist *ep, *ep2;
14018fae3551SRodney W. Grimes 	struct grouplist *grp, *tgrp;
14028fae3551SRodney W. Grimes 	struct exportlist **epp;
14038fae3551SRodney W. Grimes 	struct dirlist *dirhead;
140496968c22SPawel Jakub Dawidek 	struct statfs fsb;
1405c0511d3bSBrian Feldman 	struct xucred anon;
14068fae3551SRodney W. Grimes 	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
140796968c22SPawel Jakub Dawidek 	int len, has_host, exflags, got_nondir, dirplen, netgrp;
14088fae3551SRodney W. Grimes 
1409bcc1d071SRick Macklem 	v4root_phase = 0;
14108fae3551SRodney W. Grimes 	dirhead = (struct dirlist *)NULL;
14118fae3551SRodney W. Grimes 	while (get_line()) {
14128fae3551SRodney W. Grimes 		if (debug)
141374853402SPhilippe Charnier 			warnx("got line %s", line);
14148fae3551SRodney W. Grimes 		cp = line;
14158fae3551SRodney W. Grimes 		nextfield(&cp, &endcp);
14168fae3551SRodney W. Grimes 		if (*cp == '#')
14178fae3551SRodney W. Grimes 			goto nextline;
14188fae3551SRodney W. Grimes 
14198fae3551SRodney W. Grimes 		/*
14208fae3551SRodney W. Grimes 		 * Set defaults.
14218fae3551SRodney W. Grimes 		 */
14228fae3551SRodney W. Grimes 		has_host = FALSE;
14238fae3551SRodney W. Grimes 		anon = def_anon;
14248fae3551SRodney W. Grimes 		exflags = MNT_EXPORTED;
14258fae3551SRodney W. Grimes 		got_nondir = 0;
14268fae3551SRodney W. Grimes 		opt_flags = 0;
14278fae3551SRodney W. Grimes 		ep = (struct exportlist *)NULL;
1428bcc1d071SRick Macklem 		dirp = NULL;
1429bcc1d071SRick Macklem 
1430bcc1d071SRick Macklem 		/*
1431bcc1d071SRick Macklem 		 * Handle the V4 root dir.
1432bcc1d071SRick Macklem 		 */
1433bcc1d071SRick Macklem 		if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1434bcc1d071SRick Macklem 			/*
1435bcc1d071SRick Macklem 			 * V4: just indicates that it is the v4 root point,
1436bcc1d071SRick Macklem 			 * so skip over that and set v4root_phase.
1437bcc1d071SRick Macklem 			 */
1438bcc1d071SRick Macklem 			if (v4root_phase > 0) {
1439bcc1d071SRick Macklem 				syslog(LOG_ERR, "V4:duplicate line, ignored");
1440bcc1d071SRick Macklem 				goto nextline;
1441bcc1d071SRick Macklem 			}
1442bcc1d071SRick Macklem 			v4root_phase = 1;
1443bcc1d071SRick Macklem 			cp += 3;
1444bcc1d071SRick Macklem 			nextfield(&cp, &endcp);
1445bcc1d071SRick Macklem 		}
14468fae3551SRodney W. Grimes 
14478fae3551SRodney W. Grimes 		/*
14488fae3551SRodney W. Grimes 		 * Create new exports list entry
14498fae3551SRodney W. Grimes 		 */
14508fae3551SRodney W. Grimes 		len = endcp-cp;
14518fae3551SRodney W. Grimes 		tgrp = grp = get_grp();
14528fae3551SRodney W. Grimes 		while (len > 0) {
14530775314bSDoug Rabson 			if (len > MNTNAMLEN) {
14548fae3551SRodney W. Grimes 			    getexp_err(ep, tgrp);
14558fae3551SRodney W. Grimes 			    goto nextline;
14568fae3551SRodney W. Grimes 			}
14578fae3551SRodney W. Grimes 			if (*cp == '-') {
14588fae3551SRodney W. Grimes 			    if (ep == (struct exportlist *)NULL) {
14598fae3551SRodney W. Grimes 				getexp_err(ep, tgrp);
14608fae3551SRodney W. Grimes 				goto nextline;
14618fae3551SRodney W. Grimes 			    }
14628fae3551SRodney W. Grimes 			    if (debug)
146374853402SPhilippe Charnier 				warnx("doing opt %s", cp);
14648fae3551SRodney W. Grimes 			    got_nondir = 1;
14658fae3551SRodney W. Grimes 			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
14668fae3551SRodney W. Grimes 				&exflags, &anon)) {
14678fae3551SRodney W. Grimes 				getexp_err(ep, tgrp);
14688fae3551SRodney W. Grimes 				goto nextline;
14698fae3551SRodney W. Grimes 			    }
14708fae3551SRodney W. Grimes 			} else if (*cp == '/') {
14718fae3551SRodney W. Grimes 			    savedc = *endcp;
14728fae3551SRodney W. Grimes 			    *endcp = '\0';
1473bcc1d071SRick Macklem 			    if (v4root_phase > 1) {
1474bcc1d071SRick Macklem 				    if (dirp != NULL) {
1475bcc1d071SRick Macklem 					syslog(LOG_ERR, "Multiple V4 dirs");
1476bcc1d071SRick Macklem 					getexp_err(ep, tgrp);
1477bcc1d071SRick Macklem 					goto nextline;
1478bcc1d071SRick Macklem 				    }
1479bcc1d071SRick Macklem 			    }
14808fae3551SRodney W. Grimes 			    if (check_dirpath(cp) &&
14818fae3551SRodney W. Grimes 				statfs(cp, &fsb) >= 0) {
14829896584aSRick Macklem 				if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
14839896584aSRick Macklem 				    syslog(LOG_ERR, "Warning: exporting of "
14849896584aSRick Macklem 					"automounted fs %s not supported", cp);
14858fae3551SRodney W. Grimes 				if (got_nondir) {
148674853402SPhilippe Charnier 				    syslog(LOG_ERR, "dirs must be first");
14878fae3551SRodney W. Grimes 				    getexp_err(ep, tgrp);
14888fae3551SRodney W. Grimes 				    goto nextline;
14898fae3551SRodney W. Grimes 				}
1490bcc1d071SRick Macklem 				if (v4root_phase == 1) {
1491bcc1d071SRick Macklem 				    if (dirp != NULL) {
1492bcc1d071SRick Macklem 					syslog(LOG_ERR, "Multiple V4 dirs");
1493bcc1d071SRick Macklem 					getexp_err(ep, tgrp);
1494bcc1d071SRick Macklem 					goto nextline;
1495bcc1d071SRick Macklem 				    }
1496bcc1d071SRick Macklem 				    if (strlen(v4root_dirpath) == 0) {
1497bcc1d071SRick Macklem 					strlcpy(v4root_dirpath, cp,
1498bcc1d071SRick Macklem 					    sizeof (v4root_dirpath));
1499bcc1d071SRick Macklem 				    } else if (strcmp(v4root_dirpath, cp)
1500bcc1d071SRick Macklem 					!= 0) {
1501bcc1d071SRick Macklem 					syslog(LOG_ERR,
1502bcc1d071SRick Macklem 					    "different V4 dirpath %s", cp);
1503bcc1d071SRick Macklem 					getexp_err(ep, tgrp);
1504bcc1d071SRick Macklem 					goto nextline;
1505bcc1d071SRick Macklem 				    }
1506bcc1d071SRick Macklem 				    dirp = cp;
1507bcc1d071SRick Macklem 				    v4root_phase = 2;
1508bcc1d071SRick Macklem 				    got_nondir = 1;
1509bcc1d071SRick Macklem 				    ep = get_exp();
1510bcc1d071SRick Macklem 				} else {
15118fae3551SRodney W. Grimes 				    if (ep) {
1512bcc1d071SRick Macklem 					if (ep->ex_fs.val[0] !=
1513bcc1d071SRick Macklem 					    fsb.f_fsid.val[0] ||
1514bcc1d071SRick Macklem 					    ep->ex_fs.val[1] !=
1515bcc1d071SRick Macklem 					    fsb.f_fsid.val[1]) {
15168fae3551SRodney W. Grimes 						getexp_err(ep, tgrp);
15178fae3551SRodney W. Grimes 						goto nextline;
15188fae3551SRodney W. Grimes 					}
15198fae3551SRodney W. Grimes 				    } else {
15208fae3551SRodney W. Grimes 					/*
15218fae3551SRodney W. Grimes 					 * See if this directory is already
15228fae3551SRodney W. Grimes 					 * in the list.
15238fae3551SRodney W. Grimes 					 */
15248fae3551SRodney W. Grimes 					ep = ex_search(&fsb.f_fsid);
15258fae3551SRodney W. Grimes 					if (ep == (struct exportlist *)NULL) {
15268fae3551SRodney W. Grimes 					    ep = get_exp();
15278fae3551SRodney W. Grimes 					    ep->ex_fs = fsb.f_fsid;
1528380a3fcdSEmmanuel Vadot 					    ep->ex_fsdir = strdup(fsb.f_mntonname);
1529380a3fcdSEmmanuel Vadot 					    if (ep->ex_fsdir == NULL)
15308fae3551SRodney W. Grimes 						out_of_mem();
15318fae3551SRodney W. Grimes 					    if (debug)
1532bcc1d071SRick Macklem 						warnx(
1533bcc1d071SRick Macklem 						  "making new ep fs=0x%x,0x%x",
15348fae3551SRodney W. Grimes 						  fsb.f_fsid.val[0],
15358fae3551SRodney W. Grimes 						  fsb.f_fsid.val[1]);
15368fae3551SRodney W. Grimes 					} else if (debug)
153774853402SPhilippe Charnier 					    warnx("found ep fs=0x%x,0x%x",
15388fae3551SRodney W. Grimes 						fsb.f_fsid.val[0],
15398fae3551SRodney W. Grimes 						fsb.f_fsid.val[1]);
15408fae3551SRodney W. Grimes 				    }
15418fae3551SRodney W. Grimes 
15428fae3551SRodney W. Grimes 				    /*
15438fae3551SRodney W. Grimes 				     * Add dirpath to export mount point.
15448fae3551SRodney W. Grimes 				     */
15458fae3551SRodney W. Grimes 				    dirp = add_expdir(&dirhead, cp, len);
15468fae3551SRodney W. Grimes 				    dirplen = len;
1547bcc1d071SRick Macklem 				}
15488fae3551SRodney W. Grimes 			    } else {
15498fae3551SRodney W. Grimes 				getexp_err(ep, tgrp);
15508fae3551SRodney W. Grimes 				goto nextline;
15518fae3551SRodney W. Grimes 			    }
15528fae3551SRodney W. Grimes 			    *endcp = savedc;
15538fae3551SRodney W. Grimes 			} else {
15548fae3551SRodney W. Grimes 			    savedc = *endcp;
15558fae3551SRodney W. Grimes 			    *endcp = '\0';
15568fae3551SRodney W. Grimes 			    got_nondir = 1;
15578fae3551SRodney W. Grimes 			    if (ep == (struct exportlist *)NULL) {
15588fae3551SRodney W. Grimes 				getexp_err(ep, tgrp);
15598fae3551SRodney W. Grimes 				goto nextline;
15608fae3551SRodney W. Grimes 			    }
15618fae3551SRodney W. Grimes 
15628fae3551SRodney W. Grimes 			    /*
15638fae3551SRodney W. Grimes 			     * Get the host or netgroup.
15648fae3551SRodney W. Grimes 			     */
15658fae3551SRodney W. Grimes 			    setnetgrent(cp);
15668fae3551SRodney W. Grimes 			    netgrp = getnetgrent(&hst, &usr, &dom);
15678fae3551SRodney W. Grimes 			    do {
15688fae3551SRodney W. Grimes 				if (has_host) {
15698fae3551SRodney W. Grimes 				    grp->gr_next = get_grp();
15708fae3551SRodney W. Grimes 				    grp = grp->gr_next;
15718fae3551SRodney W. Grimes 				}
15728fae3551SRodney W. Grimes 				if (netgrp) {
15739d70a156SJoerg Wunsch 				    if (hst == 0) {
157474853402SPhilippe Charnier 					syslog(LOG_ERR,
157574853402SPhilippe Charnier 				"null hostname in netgroup %s, skipping", cp);
157601d48801SJoerg Wunsch 					grp->gr_type = GT_IGNORE;
15779d70a156SJoerg Wunsch 				    } else if (get_host(hst, grp, tgrp)) {
157874853402SPhilippe Charnier 					syslog(LOG_ERR,
157974853402SPhilippe Charnier 			"bad host %s in netgroup %s, skipping", hst, cp);
1580a968cfd8SJonathan Lemon 					grp->gr_type = GT_IGNORE;
15818fae3551SRodney W. Grimes 				    }
15828b5a6d67SBill Paul 				} else if (get_host(cp, grp, tgrp)) {
158374853402SPhilippe Charnier 				    syslog(LOG_ERR, "bad host %s, skipping", cp);
1584a968cfd8SJonathan Lemon 				    grp->gr_type = GT_IGNORE;
15858fae3551SRodney W. Grimes 				}
15868fae3551SRodney W. Grimes 				has_host = TRUE;
15878fae3551SRodney W. Grimes 			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
15888fae3551SRodney W. Grimes 			    endnetgrent();
15898fae3551SRodney W. Grimes 			    *endcp = savedc;
15908fae3551SRodney W. Grimes 			}
15918fae3551SRodney W. Grimes 			cp = endcp;
15928fae3551SRodney W. Grimes 			nextfield(&cp, &endcp);
15938fae3551SRodney W. Grimes 			len = endcp - cp;
15948fae3551SRodney W. Grimes 		}
15958fae3551SRodney W. Grimes 		if (check_options(dirhead)) {
15968fae3551SRodney W. Grimes 			getexp_err(ep, tgrp);
15978fae3551SRodney W. Grimes 			goto nextline;
15988fae3551SRodney W. Grimes 		}
15998fae3551SRodney W. Grimes 		if (!has_host) {
16006d359f31SIan Dowse 			grp->gr_type = GT_DEFAULT;
16018fae3551SRodney W. Grimes 			if (debug)
160274853402SPhilippe Charnier 				warnx("adding a default entry");
16038fae3551SRodney W. Grimes 
16048fae3551SRodney W. Grimes 		/*
16058fae3551SRodney W. Grimes 		 * Don't allow a network export coincide with a list of
16068fae3551SRodney W. Grimes 		 * host(s) on the same line.
16078fae3551SRodney W. Grimes 		 */
16088fae3551SRodney W. Grimes 		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
160960caaee2SIan Dowse 			syslog(LOG_ERR, "network/host conflict");
16108fae3551SRodney W. Grimes 			getexp_err(ep, tgrp);
16118fae3551SRodney W. Grimes 			goto nextline;
1612a968cfd8SJonathan Lemon 
1613a968cfd8SJonathan Lemon 		/*
1614a968cfd8SJonathan Lemon 		 * If an export list was specified on this line, make sure
1615a968cfd8SJonathan Lemon 		 * that we have at least one valid entry, otherwise skip it.
1616a968cfd8SJonathan Lemon 		 */
1617a968cfd8SJonathan Lemon 		} else {
1618a968cfd8SJonathan Lemon 			grp = tgrp;
1619a968cfd8SJonathan Lemon 			while (grp && grp->gr_type == GT_IGNORE)
1620a968cfd8SJonathan Lemon 				grp = grp->gr_next;
1621a968cfd8SJonathan Lemon 			if (! grp) {
1622a968cfd8SJonathan Lemon 			    getexp_err(ep, tgrp);
1623a968cfd8SJonathan Lemon 			    goto nextline;
1624a968cfd8SJonathan Lemon 			}
16258fae3551SRodney W. Grimes 		}
16268fae3551SRodney W. Grimes 
1627bcc1d071SRick Macklem 		if (v4root_phase == 1) {
1628bcc1d071SRick Macklem 			syslog(LOG_ERR, "V4:root, no dirp, ignored");
1629bcc1d071SRick Macklem 			getexp_err(ep, tgrp);
1630bcc1d071SRick Macklem 			goto nextline;
1631bcc1d071SRick Macklem 		}
1632bcc1d071SRick Macklem 
16338fae3551SRodney W. Grimes 		/*
16348fae3551SRodney W. Grimes 		 * Loop through hosts, pushing the exports into the kernel.
16358fae3551SRodney W. Grimes 		 * After loop, tgrp points to the start of the list and
16368fae3551SRodney W. Grimes 		 * grp points to the last entry in the list.
16378fae3551SRodney W. Grimes 		 */
16388fae3551SRodney W. Grimes 		grp = tgrp;
16398fae3551SRodney W. Grimes 		do {
164001709abfSIan Dowse 			if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
164101709abfSIan Dowse 			    &fsb)) {
16428fae3551SRodney W. Grimes 				getexp_err(ep, tgrp);
16438fae3551SRodney W. Grimes 				goto nextline;
16448fae3551SRodney W. Grimes 			}
16458fae3551SRodney W. Grimes 		} while (grp->gr_next && (grp = grp->gr_next));
16468fae3551SRodney W. Grimes 
16478fae3551SRodney W. Grimes 		/*
1648bcc1d071SRick Macklem 		 * For V4: don't enter in mount lists.
1649bcc1d071SRick Macklem 		 */
165073f4ccbdSRick Macklem 		if (v4root_phase > 0 && v4root_phase <= 2) {
165173f4ccbdSRick Macklem 			/*
165273f4ccbdSRick Macklem 			 * Since these structures aren't used by mountd,
165373f4ccbdSRick Macklem 			 * free them up now.
165473f4ccbdSRick Macklem 			 */
165573f4ccbdSRick Macklem 			if (ep != NULL)
165673f4ccbdSRick Macklem 				free_exp(ep);
165773f4ccbdSRick Macklem 			while (tgrp != NULL) {
165873f4ccbdSRick Macklem 				grp = tgrp;
165973f4ccbdSRick Macklem 				tgrp = tgrp->gr_next;
166073f4ccbdSRick Macklem 				free_grp(grp);
166173f4ccbdSRick Macklem 			}
1662bcc1d071SRick Macklem 			goto nextline;
166373f4ccbdSRick Macklem 		}
1664bcc1d071SRick Macklem 
1665bcc1d071SRick Macklem 		/*
16668fae3551SRodney W. Grimes 		 * Success. Update the data structures.
16678fae3551SRodney W. Grimes 		 */
16688fae3551SRodney W. Grimes 		if (has_host) {
1669a62dc406SDoug Rabson 			hang_dirp(dirhead, tgrp, ep, opt_flags);
16708fae3551SRodney W. Grimes 			grp->gr_next = grphead;
16718fae3551SRodney W. Grimes 			grphead = tgrp;
16728fae3551SRodney W. Grimes 		} else {
16738fae3551SRodney W. Grimes 			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1674a62dc406SDoug Rabson 				opt_flags);
16758fae3551SRodney W. Grimes 			free_grp(grp);
16768fae3551SRodney W. Grimes 		}
16778fae3551SRodney W. Grimes 		dirhead = (struct dirlist *)NULL;
16788fae3551SRodney W. Grimes 		if ((ep->ex_flag & EX_LINKED) == 0) {
16798fae3551SRodney W. Grimes 			ep2 = exphead;
16808fae3551SRodney W. Grimes 			epp = &exphead;
16818fae3551SRodney W. Grimes 
16828fae3551SRodney W. Grimes 			/*
16838fae3551SRodney W. Grimes 			 * Insert in the list in alphabetical order.
16848fae3551SRodney W. Grimes 			 */
16858fae3551SRodney W. Grimes 			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
16868fae3551SRodney W. Grimes 				epp = &ep2->ex_next;
16878fae3551SRodney W. Grimes 				ep2 = ep2->ex_next;
16888fae3551SRodney W. Grimes 			}
16898fae3551SRodney W. Grimes 			if (ep2)
16908fae3551SRodney W. Grimes 				ep->ex_next = ep2;
16918fae3551SRodney W. Grimes 			*epp = ep;
16928fae3551SRodney W. Grimes 			ep->ex_flag |= EX_LINKED;
16938fae3551SRodney W. Grimes 		}
16948fae3551SRodney W. Grimes nextline:
1695bcc1d071SRick Macklem 		v4root_phase = 0;
16968fae3551SRodney W. Grimes 		if (dirhead) {
16978fae3551SRodney W. Grimes 			free_dir(dirhead);
16988fae3551SRodney W. Grimes 			dirhead = (struct dirlist *)NULL;
16998fae3551SRodney W. Grimes 		}
17008fae3551SRodney W. Grimes 	}
170196968c22SPawel Jakub Dawidek }
170296968c22SPawel Jakub Dawidek 
170396968c22SPawel Jakub Dawidek /*
170496968c22SPawel Jakub Dawidek  * Get the export list from all specified files
170596968c22SPawel Jakub Dawidek  */
170619c46d8cSEdward Tomasz Napierala static void
1707a7a7d96cSPhilippe Charnier get_exportlist(void)
170896968c22SPawel Jakub Dawidek {
170996968c22SPawel Jakub Dawidek 	struct exportlist *ep, *ep2;
171096968c22SPawel Jakub Dawidek 	struct grouplist *grp, *tgrp;
171196968c22SPawel Jakub Dawidek 	struct export_args export;
171296968c22SPawel Jakub Dawidek 	struct iovec *iov;
171396968c22SPawel Jakub Dawidek 	struct statfs *fsp, *mntbufp;
171496968c22SPawel Jakub Dawidek 	struct xvfsconf vfc;
171596968c22SPawel Jakub Dawidek 	char errmsg[255];
1716e0bcf086SEitan Adler 	int num, i;
171796968c22SPawel Jakub Dawidek 	int iovlen;
17186c90092bSPawel Jakub Dawidek 	int done;
1719bcc1d071SRick Macklem 	struct nfsex_args eargs;
172096968c22SPawel Jakub Dawidek 
1721c548eb5cSRick Macklem 	if (suspend_nfsd != 0)
1722c548eb5cSRick Macklem 		(void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1723bcc1d071SRick Macklem 	v4root_dirpath[0] = '\0';
172496968c22SPawel Jakub Dawidek 	bzero(&export, sizeof(export));
172596968c22SPawel Jakub Dawidek 	export.ex_flags = MNT_DELEXPORT;
172696968c22SPawel Jakub Dawidek 	iov = NULL;
172796968c22SPawel Jakub Dawidek 	iovlen = 0;
172896968c22SPawel Jakub Dawidek 	bzero(errmsg, sizeof(errmsg));
172996968c22SPawel Jakub Dawidek 
173096968c22SPawel Jakub Dawidek 	/*
173196968c22SPawel Jakub Dawidek 	 * First, get rid of the old list
173296968c22SPawel Jakub Dawidek 	 */
173396968c22SPawel Jakub Dawidek 	ep = exphead;
173496968c22SPawel Jakub Dawidek 	while (ep) {
173596968c22SPawel Jakub Dawidek 		ep2 = ep;
173696968c22SPawel Jakub Dawidek 		ep = ep->ex_next;
173796968c22SPawel Jakub Dawidek 		free_exp(ep2);
173896968c22SPawel Jakub Dawidek 	}
173996968c22SPawel Jakub Dawidek 	exphead = (struct exportlist *)NULL;
174096968c22SPawel Jakub Dawidek 
174196968c22SPawel Jakub Dawidek 	grp = grphead;
174296968c22SPawel Jakub Dawidek 	while (grp) {
174396968c22SPawel Jakub Dawidek 		tgrp = grp;
174496968c22SPawel Jakub Dawidek 		grp = grp->gr_next;
174596968c22SPawel Jakub Dawidek 		free_grp(tgrp);
174696968c22SPawel Jakub Dawidek 	}
174796968c22SPawel Jakub Dawidek 	grphead = (struct grouplist *)NULL;
174896968c22SPawel Jakub Dawidek 
174996968c22SPawel Jakub Dawidek 	/*
1750bcc1d071SRick Macklem 	 * and the old V4 root dir.
1751bcc1d071SRick Macklem 	 */
1752bcc1d071SRick Macklem 	bzero(&eargs, sizeof (eargs));
1753bcc1d071SRick Macklem 	eargs.export.ex_flags = MNT_DELEXPORT;
175479b86807SEdward Tomasz Napierala 	if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1755bcc1d071SRick Macklem 	    errno != ENOENT)
1756bcc1d071SRick Macklem 		syslog(LOG_ERR, "Can't delete exports for V4:");
1757bcc1d071SRick Macklem 
1758bcc1d071SRick Macklem 	/*
1759bcc1d071SRick Macklem 	 * and clear flag that notes if a public fh has been exported.
1760bcc1d071SRick Macklem 	 */
1761bcc1d071SRick Macklem 	has_publicfh = 0;
1762bcc1d071SRick Macklem 
1763bcc1d071SRick Macklem 	/*
176496968c22SPawel Jakub Dawidek 	 * And delete exports that are in the kernel for all local
176596968c22SPawel Jakub Dawidek 	 * filesystems.
176696968c22SPawel Jakub Dawidek 	 * XXX: Should know how to handle all local exportable filesystems.
176796968c22SPawel Jakub Dawidek 	 */
176896968c22SPawel Jakub Dawidek 	num = getmntinfo(&mntbufp, MNT_NOWAIT);
176996968c22SPawel Jakub Dawidek 
177096968c22SPawel Jakub Dawidek 	if (num > 0) {
177196968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "fstype", NULL, 0);
177296968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "fspath", NULL, 0);
177396968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "from", NULL, 0);
177496968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "update", NULL, 0);
177596968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
177696968c22SPawel Jakub Dawidek 		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
177796968c22SPawel Jakub Dawidek 	}
177896968c22SPawel Jakub Dawidek 
177996968c22SPawel Jakub Dawidek 	for (i = 0; i < num; i++) {
178096968c22SPawel Jakub Dawidek 		fsp = &mntbufp[i];
178196968c22SPawel Jakub Dawidek 		if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
178296968c22SPawel Jakub Dawidek 			syslog(LOG_ERR, "getvfsbyname() failed for %s",
178396968c22SPawel Jakub Dawidek 			    fsp->f_fstypename);
178496968c22SPawel Jakub Dawidek 			continue;
178596968c22SPawel Jakub Dawidek 		}
178696968c22SPawel Jakub Dawidek 
178796968c22SPawel Jakub Dawidek 		/*
17882ef26470SSimon J. Gerraty 		 * We do not need to delete "export" flag from
17892ef26470SSimon J. Gerraty 		 * filesystems that do not have it set.
17902ef26470SSimon J. Gerraty 		 */
17912ef26470SSimon J. Gerraty 		if (!(fsp->f_flags & MNT_EXPORTED))
17922ef26470SSimon J. Gerraty 		    continue;
17932ef26470SSimon J. Gerraty 		/*
179496968c22SPawel Jakub Dawidek 		 * Do not delete export for network filesystem by
179596968c22SPawel Jakub Dawidek 		 * passing "export" arg to nmount().
179696968c22SPawel Jakub Dawidek 		 * It only makes sense to do this for local filesystems.
179796968c22SPawel Jakub Dawidek 		 */
179896968c22SPawel Jakub Dawidek 		if (vfc.vfc_flags & VFCF_NETWORK)
179996968c22SPawel Jakub Dawidek 			continue;
180096968c22SPawel Jakub Dawidek 
180196968c22SPawel Jakub Dawidek 		iov[1].iov_base = fsp->f_fstypename;
180296968c22SPawel Jakub Dawidek 		iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
180396968c22SPawel Jakub Dawidek 		iov[3].iov_base = fsp->f_mntonname;
180496968c22SPawel Jakub Dawidek 		iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
180596968c22SPawel Jakub Dawidek 		iov[5].iov_base = fsp->f_mntfromname;
180696968c22SPawel Jakub Dawidek 		iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
18074a185fa6SBryan Drewery 		errmsg[0] = '\0';
180896968c22SPawel Jakub Dawidek 
18095d6f5b24SKonstantin Belousov 		/*
18105d6f5b24SKonstantin Belousov 		 * EXDEV is returned when path exists but is not a
18115d6f5b24SKonstantin Belousov 		 * mount point.  May happens if raced with unmount.
18125d6f5b24SKonstantin Belousov 		 */
181396968c22SPawel Jakub Dawidek 		if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
18145d6f5b24SKonstantin Belousov 		    errno != ENOENT && errno != ENOTSUP && errno != EXDEV) {
181596968c22SPawel Jakub Dawidek 			syslog(LOG_ERR,
181696968c22SPawel Jakub Dawidek 			    "can't delete exports for %s: %m %s",
181796968c22SPawel Jakub Dawidek 			    fsp->f_mntonname, errmsg);
181896968c22SPawel Jakub Dawidek 		}
181996968c22SPawel Jakub Dawidek 	}
182096968c22SPawel Jakub Dawidek 
182196968c22SPawel Jakub Dawidek 	if (iov != NULL) {
182296968c22SPawel Jakub Dawidek 		/* Free strings allocated by strdup() in getmntopts.c */
182396968c22SPawel Jakub Dawidek 		free(iov[0].iov_base); /* fstype */
182496968c22SPawel Jakub Dawidek 		free(iov[2].iov_base); /* fspath */
182596968c22SPawel Jakub Dawidek 		free(iov[4].iov_base); /* from */
182696968c22SPawel Jakub Dawidek 		free(iov[6].iov_base); /* update */
182796968c22SPawel Jakub Dawidek 		free(iov[8].iov_base); /* export */
182896968c22SPawel Jakub Dawidek 		free(iov[10].iov_base); /* errmsg */
182996968c22SPawel Jakub Dawidek 
183096968c22SPawel Jakub Dawidek 		/* free iov, allocated by realloc() */
183196968c22SPawel Jakub Dawidek 		free(iov);
183296968c22SPawel Jakub Dawidek 		iovlen = 0;
183396968c22SPawel Jakub Dawidek 	}
183496968c22SPawel Jakub Dawidek 
183596968c22SPawel Jakub Dawidek 	/*
183696968c22SPawel Jakub Dawidek 	 * Read in the exports file and build the list, calling
183796968c22SPawel Jakub Dawidek 	 * nmount() as we go along to push the export rules into the kernel.
183896968c22SPawel Jakub Dawidek 	 */
18396c90092bSPawel Jakub Dawidek 	done = 0;
184096968c22SPawel Jakub Dawidek 	for (i = 0; exnames[i] != NULL; i++) {
184196968c22SPawel Jakub Dawidek 		if (debug)
184296968c22SPawel Jakub Dawidek 			warnx("reading exports from %s", exnames[i]);
184396968c22SPawel Jakub Dawidek 		if ((exp_file = fopen(exnames[i], "r")) == NULL) {
18446c90092bSPawel Jakub Dawidek 			syslog(LOG_WARNING, "can't open %s", exnames[i]);
18456c90092bSPawel Jakub Dawidek 			continue;
184696968c22SPawel Jakub Dawidek 		}
184796968c22SPawel Jakub Dawidek 		get_exportlist_one();
18488fae3551SRodney W. Grimes 		fclose(exp_file);
18496c90092bSPawel Jakub Dawidek 		done++;
18506c90092bSPawel Jakub Dawidek 	}
18516c90092bSPawel Jakub Dawidek 	if (done == 0) {
18526c90092bSPawel Jakub Dawidek 		syslog(LOG_ERR, "can't open any exports file");
18536c90092bSPawel Jakub Dawidek 		exit(2);
18548fae3551SRodney W. Grimes 	}
1855bcc1d071SRick Macklem 
1856bcc1d071SRick Macklem 	/*
1857bcc1d071SRick Macklem 	 * If there was no public fh, clear any previous one set.
1858bcc1d071SRick Macklem 	 */
185979b86807SEdward Tomasz Napierala 	if (has_publicfh == 0)
1860bcc1d071SRick Macklem 		(void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1861c548eb5cSRick Macklem 
1862c548eb5cSRick Macklem 	/* Resume the nfsd. If they weren't suspended, this is harmless. */
1863c548eb5cSRick Macklem 	(void)nfssvc(NFSSVC_RESUMENFSD, NULL);
186496968c22SPawel Jakub Dawidek }
18658fae3551SRodney W. Grimes 
18668fae3551SRodney W. Grimes /*
18678fae3551SRodney W. Grimes  * Allocate an export list element
18688fae3551SRodney W. Grimes  */
186919c46d8cSEdward Tomasz Napierala static struct exportlist *
1870a7a7d96cSPhilippe Charnier get_exp(void)
18718fae3551SRodney W. Grimes {
18728fae3551SRodney W. Grimes 	struct exportlist *ep;
18738fae3551SRodney W. Grimes 
187453750151SXin LI 	ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
18758fae3551SRodney W. Grimes 	if (ep == (struct exportlist *)NULL)
18768fae3551SRodney W. Grimes 		out_of_mem();
18778fae3551SRodney W. Grimes 	return (ep);
18788fae3551SRodney W. Grimes }
18798fae3551SRodney W. Grimes 
18808fae3551SRodney W. Grimes /*
18818fae3551SRodney W. Grimes  * Allocate a group list element
18828fae3551SRodney W. Grimes  */
188319c46d8cSEdward Tomasz Napierala static struct grouplist *
1884a7a7d96cSPhilippe Charnier get_grp(void)
18858fae3551SRodney W. Grimes {
18868fae3551SRodney W. Grimes 	struct grouplist *gp;
18878fae3551SRodney W. Grimes 
188853750151SXin LI 	gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
18898fae3551SRodney W. Grimes 	if (gp == (struct grouplist *)NULL)
18908fae3551SRodney W. Grimes 		out_of_mem();
18918fae3551SRodney W. Grimes 	return (gp);
18928fae3551SRodney W. Grimes }
18938fae3551SRodney W. Grimes 
18948fae3551SRodney W. Grimes /*
18958fae3551SRodney W. Grimes  * Clean up upon an error in get_exportlist().
18968fae3551SRodney W. Grimes  */
189719c46d8cSEdward Tomasz Napierala static void
1898a7a7d96cSPhilippe Charnier getexp_err(struct exportlist *ep, struct grouplist *grp)
18998fae3551SRodney W. Grimes {
19008fae3551SRodney W. Grimes 	struct grouplist *tgrp;
19018fae3551SRodney W. Grimes 
1902288fa14aSJoerg Wunsch 	if (!(opt_flags & OP_QUIET))
190374853402SPhilippe Charnier 		syslog(LOG_ERR, "bad exports list line %s", line);
19048fae3551SRodney W. Grimes 	if (ep && (ep->ex_flag & EX_LINKED) == 0)
19058fae3551SRodney W. Grimes 		free_exp(ep);
19068fae3551SRodney W. Grimes 	while (grp) {
19078fae3551SRodney W. Grimes 		tgrp = grp;
19088fae3551SRodney W. Grimes 		grp = grp->gr_next;
19098fae3551SRodney W. Grimes 		free_grp(tgrp);
19108fae3551SRodney W. Grimes 	}
19118fae3551SRodney W. Grimes }
19128fae3551SRodney W. Grimes 
19138fae3551SRodney W. Grimes /*
19148fae3551SRodney W. Grimes  * Search the export list for a matching fs.
19158fae3551SRodney W. Grimes  */
191619c46d8cSEdward Tomasz Napierala static struct exportlist *
1917a7a7d96cSPhilippe Charnier ex_search(fsid_t *fsid)
19188fae3551SRodney W. Grimes {
19198fae3551SRodney W. Grimes 	struct exportlist *ep;
19208fae3551SRodney W. Grimes 
19218fae3551SRodney W. Grimes 	ep = exphead;
19228fae3551SRodney W. Grimes 	while (ep) {
19238fae3551SRodney W. Grimes 		if (ep->ex_fs.val[0] == fsid->val[0] &&
19248fae3551SRodney W. Grimes 		    ep->ex_fs.val[1] == fsid->val[1])
19258fae3551SRodney W. Grimes 			return (ep);
19268fae3551SRodney W. Grimes 		ep = ep->ex_next;
19278fae3551SRodney W. Grimes 	}
19288fae3551SRodney W. Grimes 	return (ep);
19298fae3551SRodney W. Grimes }
19308fae3551SRodney W. Grimes 
19318fae3551SRodney W. Grimes /*
19328fae3551SRodney W. Grimes  * Add a directory path to the list.
19338fae3551SRodney W. Grimes  */
193419c46d8cSEdward Tomasz Napierala static char *
1935a7a7d96cSPhilippe Charnier add_expdir(struct dirlist **dpp, char *cp, int len)
19368fae3551SRodney W. Grimes {
19378fae3551SRodney W. Grimes 	struct dirlist *dp;
19388fae3551SRodney W. Grimes 
1939*89b859e3SEmmanuel Vadot 	dp = malloc(sizeof (struct dirlist));
194074853402SPhilippe Charnier 	if (dp == (struct dirlist *)NULL)
194174853402SPhilippe Charnier 		out_of_mem();
19428fae3551SRodney W. Grimes 	dp->dp_left = *dpp;
19438fae3551SRodney W. Grimes 	dp->dp_right = (struct dirlist *)NULL;
19448fae3551SRodney W. Grimes 	dp->dp_flag = 0;
19458fae3551SRodney W. Grimes 	dp->dp_hosts = (struct hostlist *)NULL;
1946380a3fcdSEmmanuel Vadot 	dp->dp_dirp = strndup(cp, len);
1947380a3fcdSEmmanuel Vadot 	if (dp->dp_dirp == NULL)
1948380a3fcdSEmmanuel Vadot 		out_of_mem();
19498fae3551SRodney W. Grimes 	*dpp = dp;
19508fae3551SRodney W. Grimes 	return (dp->dp_dirp);
19518fae3551SRodney W. Grimes }
19528fae3551SRodney W. Grimes 
19538fae3551SRodney W. Grimes /*
19548fae3551SRodney W. Grimes  * Hang the dir list element off the dirpath binary tree as required
19558fae3551SRodney W. Grimes  * and update the entry for host.
19568fae3551SRodney W. Grimes  */
195719c46d8cSEdward Tomasz Napierala static void
1958a7a7d96cSPhilippe Charnier hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1959a7a7d96cSPhilippe Charnier 	int flags)
19608fae3551SRodney W. Grimes {
19618fae3551SRodney W. Grimes 	struct hostlist *hp;
19628fae3551SRodney W. Grimes 	struct dirlist *dp2;
19638fae3551SRodney W. Grimes 
1964a62dc406SDoug Rabson 	if (flags & OP_ALLDIRS) {
19658fae3551SRodney W. Grimes 		if (ep->ex_defdir)
19668fae3551SRodney W. Grimes 			free((caddr_t)dp);
19678fae3551SRodney W. Grimes 		else
19688fae3551SRodney W. Grimes 			ep->ex_defdir = dp;
1969a62dc406SDoug Rabson 		if (grp == (struct grouplist *)NULL) {
19708fae3551SRodney W. Grimes 			ep->ex_defdir->dp_flag |= DP_DEFSET;
1971c3f86a25SRick Macklem 			/* Save the default security flavors list. */
1972c3f86a25SRick Macklem 			ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1973c3f86a25SRick Macklem 			if (ep->ex_numsecflavors > 0)
1974c3f86a25SRick Macklem 				memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1975c3f86a25SRick Macklem 				    sizeof(ep->ex_secflavors));
1976a62dc406SDoug Rabson 		} else while (grp) {
19778fae3551SRodney W. Grimes 			hp = get_ht();
19788fae3551SRodney W. Grimes 			hp->ht_grp = grp;
19798fae3551SRodney W. Grimes 			hp->ht_next = ep->ex_defdir->dp_hosts;
19808fae3551SRodney W. Grimes 			ep->ex_defdir->dp_hosts = hp;
1981c3f86a25SRick Macklem 			/* Save the security flavors list for this host set. */
1982c3f86a25SRick Macklem 			grp->gr_numsecflavors = ep->ex_numsecflavors;
1983c3f86a25SRick Macklem 			if (ep->ex_numsecflavors > 0)
1984c3f86a25SRick Macklem 				memcpy(grp->gr_secflavors, ep->ex_secflavors,
1985c3f86a25SRick Macklem 				    sizeof(ep->ex_secflavors));
19868fae3551SRodney W. Grimes 			grp = grp->gr_next;
19878fae3551SRodney W. Grimes 		}
19888fae3551SRodney W. Grimes 	} else {
19898fae3551SRodney W. Grimes 
19908fae3551SRodney W. Grimes 		/*
199174853402SPhilippe Charnier 		 * Loop through the directories adding them to the tree.
19928fae3551SRodney W. Grimes 		 */
19938fae3551SRodney W. Grimes 		while (dp) {
19948fae3551SRodney W. Grimes 			dp2 = dp->dp_left;
1995c3f86a25SRick Macklem 			add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
19968fae3551SRodney W. Grimes 			dp = dp2;
19978fae3551SRodney W. Grimes 		}
19988fae3551SRodney W. Grimes 	}
19998fae3551SRodney W. Grimes }
20008fae3551SRodney W. Grimes 
20018fae3551SRodney W. Grimes /*
20028fae3551SRodney W. Grimes  * Traverse the binary tree either updating a node that is already there
20038fae3551SRodney W. Grimes  * for the new directory or adding the new node.
20048fae3551SRodney W. Grimes  */
200519c46d8cSEdward Tomasz Napierala static void
2006a7a7d96cSPhilippe Charnier add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2007c3f86a25SRick Macklem 	int flags, struct exportlist *ep)
20088fae3551SRodney W. Grimes {
20098fae3551SRodney W. Grimes 	struct dirlist *dp;
20108fae3551SRodney W. Grimes 	struct hostlist *hp;
20118fae3551SRodney W. Grimes 	int cmp;
20128fae3551SRodney W. Grimes 
20138fae3551SRodney W. Grimes 	dp = *dpp;
20148fae3551SRodney W. Grimes 	if (dp) {
20158fae3551SRodney W. Grimes 		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
20168fae3551SRodney W. Grimes 		if (cmp > 0) {
2017c3f86a25SRick Macklem 			add_dlist(&dp->dp_left, newdp, grp, flags, ep);
20188fae3551SRodney W. Grimes 			return;
20198fae3551SRodney W. Grimes 		} else if (cmp < 0) {
2020c3f86a25SRick Macklem 			add_dlist(&dp->dp_right, newdp, grp, flags, ep);
20218fae3551SRodney W. Grimes 			return;
20228fae3551SRodney W. Grimes 		} else
20238fae3551SRodney W. Grimes 			free((caddr_t)newdp);
20248fae3551SRodney W. Grimes 	} else {
20258fae3551SRodney W. Grimes 		dp = newdp;
20268fae3551SRodney W. Grimes 		dp->dp_left = (struct dirlist *)NULL;
20278fae3551SRodney W. Grimes 		*dpp = dp;
20288fae3551SRodney W. Grimes 	}
20298fae3551SRodney W. Grimes 	if (grp) {
20308fae3551SRodney W. Grimes 
20318fae3551SRodney W. Grimes 		/*
20328fae3551SRodney W. Grimes 		 * Hang all of the host(s) off of the directory point.
20338fae3551SRodney W. Grimes 		 */
20348fae3551SRodney W. Grimes 		do {
20358fae3551SRodney W. Grimes 			hp = get_ht();
20368fae3551SRodney W. Grimes 			hp->ht_grp = grp;
20378fae3551SRodney W. Grimes 			hp->ht_next = dp->dp_hosts;
20388fae3551SRodney W. Grimes 			dp->dp_hosts = hp;
2039c3f86a25SRick Macklem 			/* Save the security flavors list for this host set. */
2040c3f86a25SRick Macklem 			grp->gr_numsecflavors = ep->ex_numsecflavors;
2041c3f86a25SRick Macklem 			if (ep->ex_numsecflavors > 0)
2042c3f86a25SRick Macklem 				memcpy(grp->gr_secflavors, ep->ex_secflavors,
2043c3f86a25SRick Macklem 				    sizeof(ep->ex_secflavors));
20448fae3551SRodney W. Grimes 			grp = grp->gr_next;
20458fae3551SRodney W. Grimes 		} while (grp);
2046a62dc406SDoug Rabson 	} else {
20478fae3551SRodney W. Grimes 		dp->dp_flag |= DP_DEFSET;
2048c3f86a25SRick Macklem 		/* Save the default security flavors list. */
2049c3f86a25SRick Macklem 		ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2050c3f86a25SRick Macklem 		if (ep->ex_numsecflavors > 0)
2051c3f86a25SRick Macklem 			memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2052c3f86a25SRick Macklem 			    sizeof(ep->ex_secflavors));
2053a62dc406SDoug Rabson 	}
20548fae3551SRodney W. Grimes }
20558fae3551SRodney W. Grimes 
20568fae3551SRodney W. Grimes /*
20578fae3551SRodney W. Grimes  * Search for a dirpath on the export point.
20588fae3551SRodney W. Grimes  */
205919c46d8cSEdward Tomasz Napierala static struct dirlist *
2060a7a7d96cSPhilippe Charnier dirp_search(struct dirlist *dp, char *dirp)
20618fae3551SRodney W. Grimes {
20628fae3551SRodney W. Grimes 	int cmp;
20638fae3551SRodney W. Grimes 
20648fae3551SRodney W. Grimes 	if (dp) {
20658360efbdSAlfred Perlstein 		cmp = strcmp(dp->dp_dirp, dirp);
20668fae3551SRodney W. Grimes 		if (cmp > 0)
20678360efbdSAlfred Perlstein 			return (dirp_search(dp->dp_left, dirp));
20688fae3551SRodney W. Grimes 		else if (cmp < 0)
20698360efbdSAlfred Perlstein 			return (dirp_search(dp->dp_right, dirp));
20708fae3551SRodney W. Grimes 		else
20718fae3551SRodney W. Grimes 			return (dp);
20728fae3551SRodney W. Grimes 	}
20738fae3551SRodney W. Grimes 	return (dp);
20748fae3551SRodney W. Grimes }
20758fae3551SRodney W. Grimes 
20768fae3551SRodney W. Grimes /*
20778fae3551SRodney W. Grimes  * Scan for a host match in a directory tree.
20788fae3551SRodney W. Grimes  */
207919c46d8cSEdward Tomasz Napierala static int
2080a7a7d96cSPhilippe Charnier chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2081c3f86a25SRick Macklem 	int *hostsetp, int *numsecflavors, int **secflavorsp)
20828fae3551SRodney W. Grimes {
20838fae3551SRodney W. Grimes 	struct hostlist *hp;
20848fae3551SRodney W. Grimes 	struct grouplist *grp;
20858360efbdSAlfred Perlstein 	struct addrinfo *ai;
20868fae3551SRodney W. Grimes 
20878fae3551SRodney W. Grimes 	if (dp) {
20888fae3551SRodney W. Grimes 		if (dp->dp_flag & DP_DEFSET)
2089a62dc406SDoug Rabson 			*defsetp = dp->dp_flag;
20908fae3551SRodney W. Grimes 		hp = dp->dp_hosts;
20918fae3551SRodney W. Grimes 		while (hp) {
20928fae3551SRodney W. Grimes 			grp = hp->ht_grp;
20938fae3551SRodney W. Grimes 			switch (grp->gr_type) {
20948fae3551SRodney W. Grimes 			case GT_HOST:
20958360efbdSAlfred Perlstein 				ai = grp->gr_ptr.gt_addrinfo;
20968360efbdSAlfred Perlstein 				for (; ai; ai = ai->ai_next) {
209760caaee2SIan Dowse 					if (!sacmp(ai->ai_addr, saddr, NULL)) {
20988360efbdSAlfred Perlstein 						*hostsetp =
20998360efbdSAlfred Perlstein 						    (hp->ht_flag | DP_HOSTSET);
2100c3f86a25SRick Macklem 						if (numsecflavors != NULL) {
2101c3f86a25SRick Macklem 							*numsecflavors =
2102c3f86a25SRick Macklem 							    grp->gr_numsecflavors;
2103c3f86a25SRick Macklem 							*secflavorsp =
2104c3f86a25SRick Macklem 							    grp->gr_secflavors;
2105c3f86a25SRick Macklem 						}
21068fae3551SRodney W. Grimes 						return (1);
2107a62dc406SDoug Rabson 					}
21088fae3551SRodney W. Grimes 				}
21098fae3551SRodney W. Grimes 				break;
21108fae3551SRodney W. Grimes 			case GT_NET:
211160caaee2SIan Dowse 				if (!sacmp(saddr, (struct sockaddr *)
211260caaee2SIan Dowse 				    &grp->gr_ptr.gt_net.nt_net,
211360caaee2SIan Dowse 				    (struct sockaddr *)
211460caaee2SIan Dowse 				    &grp->gr_ptr.gt_net.nt_mask)) {
2115a62dc406SDoug Rabson 					*hostsetp = (hp->ht_flag | DP_HOSTSET);
2116c3f86a25SRick Macklem 					if (numsecflavors != NULL) {
2117c3f86a25SRick Macklem 						*numsecflavors =
2118c3f86a25SRick Macklem 						    grp->gr_numsecflavors;
2119c3f86a25SRick Macklem 						*secflavorsp =
2120c3f86a25SRick Macklem 						    grp->gr_secflavors;
2121c3f86a25SRick Macklem 					}
21228fae3551SRodney W. Grimes 					return (1);
2123a62dc406SDoug Rabson 				}
21248fae3551SRodney W. Grimes 				break;
212560caaee2SIan Dowse 			}
21268fae3551SRodney W. Grimes 			hp = hp->ht_next;
21278fae3551SRodney W. Grimes 		}
21288fae3551SRodney W. Grimes 	}
21298fae3551SRodney W. Grimes 	return (0);
21308fae3551SRodney W. Grimes }
21318fae3551SRodney W. Grimes 
21328fae3551SRodney W. Grimes /*
21338fae3551SRodney W. Grimes  * Scan tree for a host that matches the address.
21348fae3551SRodney W. Grimes  */
213519c46d8cSEdward Tomasz Napierala static int
2136a7a7d96cSPhilippe Charnier scan_tree(struct dirlist *dp, struct sockaddr *saddr)
21378fae3551SRodney W. Grimes {
2138a62dc406SDoug Rabson 	int defset, hostset;
21398fae3551SRodney W. Grimes 
21408fae3551SRodney W. Grimes 	if (dp) {
21418fae3551SRodney W. Grimes 		if (scan_tree(dp->dp_left, saddr))
21428fae3551SRodney W. Grimes 			return (1);
2143c3f86a25SRick Macklem 		if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
21448fae3551SRodney W. Grimes 			return (1);
21458fae3551SRodney W. Grimes 		if (scan_tree(dp->dp_right, saddr))
21468fae3551SRodney W. Grimes 			return (1);
21478fae3551SRodney W. Grimes 	}
21488fae3551SRodney W. Grimes 	return (0);
21498fae3551SRodney W. Grimes }
21508fae3551SRodney W. Grimes 
21518fae3551SRodney W. Grimes /*
21528fae3551SRodney W. Grimes  * Traverse the dirlist tree and free it up.
21538fae3551SRodney W. Grimes  */
215419c46d8cSEdward Tomasz Napierala static void
2155a7a7d96cSPhilippe Charnier free_dir(struct dirlist *dp)
21568fae3551SRodney W. Grimes {
21578fae3551SRodney W. Grimes 
21588fae3551SRodney W. Grimes 	if (dp) {
21598fae3551SRodney W. Grimes 		free_dir(dp->dp_left);
21608fae3551SRodney W. Grimes 		free_dir(dp->dp_right);
21618fae3551SRodney W. Grimes 		free_host(dp->dp_hosts);
21628fae3551SRodney W. Grimes 		free((caddr_t)dp);
21638fae3551SRodney W. Grimes 	}
21648fae3551SRodney W. Grimes }
21658fae3551SRodney W. Grimes 
21668fae3551SRodney W. Grimes /*
2167a9148abdSDoug Rabson  * Parse a colon separated list of security flavors
2168a9148abdSDoug Rabson  */
216919c46d8cSEdward Tomasz Napierala static int
2170a7a7d96cSPhilippe Charnier parsesec(char *seclist, struct exportlist *ep)
2171a9148abdSDoug Rabson {
2172a9148abdSDoug Rabson 	char *cp, savedc;
2173a9148abdSDoug Rabson 	int flavor;
2174a9148abdSDoug Rabson 
2175a9148abdSDoug Rabson 	ep->ex_numsecflavors = 0;
2176a9148abdSDoug Rabson 	for (;;) {
2177a9148abdSDoug Rabson 		cp = strchr(seclist, ':');
2178a9148abdSDoug Rabson 		if (cp) {
2179a9148abdSDoug Rabson 			savedc = *cp;
2180a9148abdSDoug Rabson 			*cp = '\0';
2181a9148abdSDoug Rabson 		}
2182a9148abdSDoug Rabson 
2183a9148abdSDoug Rabson 		if (!strcmp(seclist, "sys"))
2184a9148abdSDoug Rabson 			flavor = AUTH_SYS;
2185a9148abdSDoug Rabson 		else if (!strcmp(seclist, "krb5"))
2186a9148abdSDoug Rabson 			flavor = RPCSEC_GSS_KRB5;
2187a9148abdSDoug Rabson 		else if (!strcmp(seclist, "krb5i"))
2188a9148abdSDoug Rabson 			flavor = RPCSEC_GSS_KRB5I;
2189a9148abdSDoug Rabson 		else if (!strcmp(seclist, "krb5p"))
2190a9148abdSDoug Rabson 			flavor = RPCSEC_GSS_KRB5P;
2191a9148abdSDoug Rabson 		else {
2192a9148abdSDoug Rabson 			if (cp)
2193a9148abdSDoug Rabson 				*cp = savedc;
2194a9148abdSDoug Rabson 			syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2195a9148abdSDoug Rabson 			return (1);
2196a9148abdSDoug Rabson 		}
2197a9148abdSDoug Rabson 		if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2198a9148abdSDoug Rabson 			if (cp)
2199a9148abdSDoug Rabson 				*cp = savedc;
2200a9148abdSDoug Rabson 			syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2201a9148abdSDoug Rabson 			return (1);
2202a9148abdSDoug Rabson 		}
2203a9148abdSDoug Rabson 		ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2204a9148abdSDoug Rabson 		ep->ex_numsecflavors++;
2205a9148abdSDoug Rabson 		if (cp) {
2206a9148abdSDoug Rabson 			*cp = savedc;
2207a9148abdSDoug Rabson 			seclist = cp + 1;
2208a9148abdSDoug Rabson 		} else {
2209a9148abdSDoug Rabson 			break;
2210a9148abdSDoug Rabson 		}
2211a9148abdSDoug Rabson 	}
2212a9148abdSDoug Rabson 	return (0);
2213a9148abdSDoug Rabson }
2214a9148abdSDoug Rabson 
2215a9148abdSDoug Rabson /*
22168fae3551SRodney W. Grimes  * Parse the option string and update fields.
22178fae3551SRodney W. Grimes  * Option arguments may either be -<option>=<value> or
22188fae3551SRodney W. Grimes  * -<option> <value>
22198fae3551SRodney W. Grimes  */
222019c46d8cSEdward Tomasz Napierala static int
2221a7a7d96cSPhilippe Charnier do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2222a7a7d96cSPhilippe Charnier 	int *has_hostp, int *exflagsp, struct xucred *cr)
22238fae3551SRodney W. Grimes {
22248fae3551SRodney W. Grimes 	char *cpoptarg, *cpoptend;
22258fae3551SRodney W. Grimes 	char *cp, *endcp, *cpopt, savedc, savedc2;
22268fae3551SRodney W. Grimes 	int allflag, usedarg;
22278fae3551SRodney W. Grimes 
2228cb479b11SAlfred Perlstein 	savedc2 = '\0';
22298fae3551SRodney W. Grimes 	cpopt = *cpp;
22308fae3551SRodney W. Grimes 	cpopt++;
22318fae3551SRodney W. Grimes 	cp = *endcpp;
22328fae3551SRodney W. Grimes 	savedc = *cp;
22338fae3551SRodney W. Grimes 	*cp = '\0';
22348fae3551SRodney W. Grimes 	while (cpopt && *cpopt) {
22358fae3551SRodney W. Grimes 		allflag = 1;
22368fae3551SRodney W. Grimes 		usedarg = -2;
223774853402SPhilippe Charnier 		if ((cpoptend = strchr(cpopt, ','))) {
22388fae3551SRodney W. Grimes 			*cpoptend++ = '\0';
223974853402SPhilippe Charnier 			if ((cpoptarg = strchr(cpopt, '=')))
22408fae3551SRodney W. Grimes 				*cpoptarg++ = '\0';
22418fae3551SRodney W. Grimes 		} else {
224274853402SPhilippe Charnier 			if ((cpoptarg = strchr(cpopt, '=')))
22438fae3551SRodney W. Grimes 				*cpoptarg++ = '\0';
22448fae3551SRodney W. Grimes 			else {
22458fae3551SRodney W. Grimes 				*cp = savedc;
22468fae3551SRodney W. Grimes 				nextfield(&cp, &endcp);
22478fae3551SRodney W. Grimes 				**endcpp = '\0';
22488fae3551SRodney W. Grimes 				if (endcp > cp && *cp != '-') {
22498fae3551SRodney W. Grimes 					cpoptarg = cp;
22508fae3551SRodney W. Grimes 					savedc2 = *endcp;
22518fae3551SRodney W. Grimes 					*endcp = '\0';
22528fae3551SRodney W. Grimes 					usedarg = 0;
22538fae3551SRodney W. Grimes 				}
22548fae3551SRodney W. Grimes 			}
22558fae3551SRodney W. Grimes 		}
22568fae3551SRodney W. Grimes 		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
22578fae3551SRodney W. Grimes 			*exflagsp |= MNT_EXRDONLY;
22588fae3551SRodney W. Grimes 		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
22598fae3551SRodney W. Grimes 		    !(allflag = strcmp(cpopt, "mapall")) ||
22608fae3551SRodney W. Grimes 		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
22618fae3551SRodney W. Grimes 			usedarg++;
22628fae3551SRodney W. Grimes 			parsecred(cpoptarg, cr);
22638fae3551SRodney W. Grimes 			if (allflag == 0) {
22648fae3551SRodney W. Grimes 				*exflagsp |= MNT_EXPORTANON;
22658fae3551SRodney W. Grimes 				opt_flags |= OP_MAPALL;
22668fae3551SRodney W. Grimes 			} else
22678fae3551SRodney W. Grimes 				opt_flags |= OP_MAPROOT;
22688fae3551SRodney W. Grimes 		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
22698fae3551SRodney W. Grimes 		    !strcmp(cpopt, "m"))) {
22708fae3551SRodney W. Grimes 			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
227174853402SPhilippe Charnier 				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
22728fae3551SRodney W. Grimes 				return (1);
22738fae3551SRodney W. Grimes 			}
22748fae3551SRodney W. Grimes 			usedarg++;
22758fae3551SRodney W. Grimes 			opt_flags |= OP_MASK;
22768fae3551SRodney W. Grimes 		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
22778fae3551SRodney W. Grimes 			!strcmp(cpopt, "n"))) {
22788360efbdSAlfred Perlstein 			if (strchr(cpoptarg, '/') != NULL) {
22798360efbdSAlfred Perlstein 				if (debug)
22808360efbdSAlfred Perlstein 					fprintf(stderr, "setting OP_MASKLEN\n");
22818360efbdSAlfred Perlstein 				opt_flags |= OP_MASKLEN;
22828360efbdSAlfred Perlstein 			}
22838fae3551SRodney W. Grimes 			if (grp->gr_type != GT_NULL) {
228474853402SPhilippe Charnier 				syslog(LOG_ERR, "network/host conflict");
22858fae3551SRodney W. Grimes 				return (1);
22868fae3551SRodney W. Grimes 			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
228774853402SPhilippe Charnier 				syslog(LOG_ERR, "bad net: %s", cpoptarg);
22888fae3551SRodney W. Grimes 				return (1);
22898fae3551SRodney W. Grimes 			}
22908fae3551SRodney W. Grimes 			grp->gr_type = GT_NET;
22918fae3551SRodney W. Grimes 			*has_hostp = 1;
22928fae3551SRodney W. Grimes 			usedarg++;
22938fae3551SRodney W. Grimes 			opt_flags |= OP_NET;
22948fae3551SRodney W. Grimes 		} else if (!strcmp(cpopt, "alldirs")) {
22958fae3551SRodney W. Grimes 			opt_flags |= OP_ALLDIRS;
2296cb3923e0SDoug Rabson 		} else if (!strcmp(cpopt, "public")) {
2297cb3923e0SDoug Rabson 			*exflagsp |= MNT_EXPUBLIC;
2298cb3923e0SDoug Rabson 		} else if (!strcmp(cpopt, "webnfs")) {
2299cb3923e0SDoug Rabson 			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2300cb3923e0SDoug Rabson 			opt_flags |= OP_MAPALL;
2301cb3923e0SDoug Rabson 		} else if (cpoptarg && !strcmp(cpopt, "index")) {
2302cb3923e0SDoug Rabson 			ep->ex_indexfile = strdup(cpoptarg);
2303288fa14aSJoerg Wunsch 		} else if (!strcmp(cpopt, "quiet")) {
2304288fa14aSJoerg Wunsch 			opt_flags |= OP_QUIET;
2305dcdc127bSSergey Kandaurov 		} else if (cpoptarg && !strcmp(cpopt, "sec")) {
2306a9148abdSDoug Rabson 			if (parsesec(cpoptarg, ep))
2307a9148abdSDoug Rabson 				return (1);
2308a9148abdSDoug Rabson 			opt_flags |= OP_SEC;
2309a9148abdSDoug Rabson 			usedarg++;
23108fae3551SRodney W. Grimes 		} else {
231174853402SPhilippe Charnier 			syslog(LOG_ERR, "bad opt %s", cpopt);
23128fae3551SRodney W. Grimes 			return (1);
23138fae3551SRodney W. Grimes 		}
23148fae3551SRodney W. Grimes 		if (usedarg >= 0) {
23158fae3551SRodney W. Grimes 			*endcp = savedc2;
23168fae3551SRodney W. Grimes 			**endcpp = savedc;
23178fae3551SRodney W. Grimes 			if (usedarg > 0) {
23188fae3551SRodney W. Grimes 				*cpp = cp;
23198fae3551SRodney W. Grimes 				*endcpp = endcp;
23208fae3551SRodney W. Grimes 			}
23218fae3551SRodney W. Grimes 			return (0);
23228fae3551SRodney W. Grimes 		}
23238fae3551SRodney W. Grimes 		cpopt = cpoptend;
23248fae3551SRodney W. Grimes 	}
23258fae3551SRodney W. Grimes 	**endcpp = savedc;
23268fae3551SRodney W. Grimes 	return (0);
23278fae3551SRodney W. Grimes }
23288fae3551SRodney W. Grimes 
23298fae3551SRodney W. Grimes /*
23308fae3551SRodney W. Grimes  * Translate a character string to the corresponding list of network
23318fae3551SRodney W. Grimes  * addresses for a hostname.
23328fae3551SRodney W. Grimes  */
233319c46d8cSEdward Tomasz Napierala static int
2334a7a7d96cSPhilippe Charnier get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
23358fae3551SRodney W. Grimes {
23368b5a6d67SBill Paul 	struct grouplist *checkgrp;
233701709abfSIan Dowse 	struct addrinfo *ai, *tai, hints;
23388360efbdSAlfred Perlstein 	int ecode;
23398360efbdSAlfred Perlstein 	char host[NI_MAXHOST];
23408fae3551SRodney W. Grimes 
23418360efbdSAlfred Perlstein 	if (grp->gr_type != GT_NULL) {
23428360efbdSAlfred Perlstein 		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
23438fae3551SRodney W. Grimes 		return (1);
23448fae3551SRodney W. Grimes 	}
23458360efbdSAlfred Perlstein 	memset(&hints, 0, sizeof hints);
23468360efbdSAlfred Perlstein 	hints.ai_flags = AI_CANONNAME;
23478360efbdSAlfred Perlstein 	hints.ai_protocol = IPPROTO_UDP;
23488360efbdSAlfred Perlstein 	ecode = getaddrinfo(cp, NULL, &hints, &ai);
23498360efbdSAlfred Perlstein 	if (ecode != 0) {
235001709abfSIan Dowse 		syslog(LOG_ERR,"can't get address info for host %s", cp);
23518360efbdSAlfred Perlstein 		return 1;
23528fae3551SRodney W. Grimes 	}
23538360efbdSAlfred Perlstein 	grp->gr_ptr.gt_addrinfo = ai;
23548360efbdSAlfred Perlstein 	while (ai != NULL) {
23558360efbdSAlfred Perlstein 		if (ai->ai_canonname == NULL) {
23568360efbdSAlfred Perlstein 			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
23574f101318SHajimu UMEMOTO 			    sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
23588360efbdSAlfred Perlstein 				strlcpy(host, "?", sizeof(host));
23598360efbdSAlfred Perlstein 			ai->ai_canonname = strdup(host);
23608360efbdSAlfred Perlstein 			ai->ai_flags |= AI_CANONNAME;
23616d359f31SIan Dowse 		}
23628fae3551SRodney W. Grimes 		if (debug)
236301709abfSIan Dowse 			fprintf(stderr, "got host %s\n", ai->ai_canonname);
236401709abfSIan Dowse 		/*
236501709abfSIan Dowse 		 * Sanity check: make sure we don't already have an entry
236601709abfSIan Dowse 		 * for this host in the grouplist.
236701709abfSIan Dowse 		 */
236801709abfSIan Dowse 		for (checkgrp = tgrp; checkgrp != NULL;
236901709abfSIan Dowse 		    checkgrp = checkgrp->gr_next) {
237001709abfSIan Dowse 			if (checkgrp->gr_type != GT_HOST)
237101709abfSIan Dowse 				continue;
237201709abfSIan Dowse 			for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
237301709abfSIan Dowse 			    tai = tai->ai_next) {
237460caaee2SIan Dowse 				if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
237501709abfSIan Dowse 					continue;
237601709abfSIan Dowse 				if (debug)
237701709abfSIan Dowse 					fprintf(stderr,
237801709abfSIan Dowse 					    "ignoring duplicate host %s\n",
237901709abfSIan Dowse 					    ai->ai_canonname);
238001709abfSIan Dowse 				grp->gr_type = GT_IGNORE;
238101709abfSIan Dowse 				return (0);
238201709abfSIan Dowse 			}
238301709abfSIan Dowse 		}
23848360efbdSAlfred Perlstein 		ai = ai->ai_next;
23858360efbdSAlfred Perlstein 	}
238601709abfSIan Dowse 	grp->gr_type = GT_HOST;
23878fae3551SRodney W. Grimes 	return (0);
23888fae3551SRodney W. Grimes }
23898fae3551SRodney W. Grimes 
23908fae3551SRodney W. Grimes /*
23918fae3551SRodney W. Grimes  * Free up an exports list component
23928fae3551SRodney W. Grimes  */
239319c46d8cSEdward Tomasz Napierala static void
2394a7a7d96cSPhilippe Charnier free_exp(struct exportlist *ep)
23958fae3551SRodney W. Grimes {
23968fae3551SRodney W. Grimes 
23978fae3551SRodney W. Grimes 	if (ep->ex_defdir) {
23988fae3551SRodney W. Grimes 		free_host(ep->ex_defdir->dp_hosts);
23998fae3551SRodney W. Grimes 		free((caddr_t)ep->ex_defdir);
24008fae3551SRodney W. Grimes 	}
24018fae3551SRodney W. Grimes 	if (ep->ex_fsdir)
24028fae3551SRodney W. Grimes 		free(ep->ex_fsdir);
2403cb3923e0SDoug Rabson 	if (ep->ex_indexfile)
2404cb3923e0SDoug Rabson 		free(ep->ex_indexfile);
24058fae3551SRodney W. Grimes 	free_dir(ep->ex_dirl);
24068fae3551SRodney W. Grimes 	free((caddr_t)ep);
24078fae3551SRodney W. Grimes }
24088fae3551SRodney W. Grimes 
24098fae3551SRodney W. Grimes /*
24108fae3551SRodney W. Grimes  * Free hosts.
24118fae3551SRodney W. Grimes  */
241219c46d8cSEdward Tomasz Napierala static void
2413a7a7d96cSPhilippe Charnier free_host(struct hostlist *hp)
24148fae3551SRodney W. Grimes {
24158fae3551SRodney W. Grimes 	struct hostlist *hp2;
24168fae3551SRodney W. Grimes 
24178fae3551SRodney W. Grimes 	while (hp) {
24188fae3551SRodney W. Grimes 		hp2 = hp;
24198fae3551SRodney W. Grimes 		hp = hp->ht_next;
24208fae3551SRodney W. Grimes 		free((caddr_t)hp2);
24218fae3551SRodney W. Grimes 	}
24228fae3551SRodney W. Grimes }
24238fae3551SRodney W. Grimes 
242419c46d8cSEdward Tomasz Napierala static struct hostlist *
2425a7a7d96cSPhilippe Charnier get_ht(void)
24268fae3551SRodney W. Grimes {
24278fae3551SRodney W. Grimes 	struct hostlist *hp;
24288fae3551SRodney W. Grimes 
24298fae3551SRodney W. Grimes 	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
24308fae3551SRodney W. Grimes 	if (hp == (struct hostlist *)NULL)
24318fae3551SRodney W. Grimes 		out_of_mem();
24328fae3551SRodney W. Grimes 	hp->ht_next = (struct hostlist *)NULL;
2433a62dc406SDoug Rabson 	hp->ht_flag = 0;
24348fae3551SRodney W. Grimes 	return (hp);
24358fae3551SRodney W. Grimes }
24368fae3551SRodney W. Grimes 
24378fae3551SRodney W. Grimes /*
24388fae3551SRodney W. Grimes  * Out of memory, fatal
24398fae3551SRodney W. Grimes  */
244019c46d8cSEdward Tomasz Napierala static void
2441a7a7d96cSPhilippe Charnier out_of_mem(void)
24428fae3551SRodney W. Grimes {
24438fae3551SRodney W. Grimes 
244474853402SPhilippe Charnier 	syslog(LOG_ERR, "out of memory");
24458fae3551SRodney W. Grimes 	exit(2);
24468fae3551SRodney W. Grimes }
24478fae3551SRodney W. Grimes 
24488fae3551SRodney W. Grimes /*
24496a09faf2SCraig Rodrigues  * Do the nmount() syscall with the update flag to push the export info into
24508fae3551SRodney W. Grimes  * the kernel.
24518fae3551SRodney W. Grimes  */
245219c46d8cSEdward Tomasz Napierala static int
24536a09faf2SCraig Rodrigues do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
24546a09faf2SCraig Rodrigues     struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
24558fae3551SRodney W. Grimes {
2456f93caef2SIan Dowse 	struct statfs fsb1;
24578360efbdSAlfred Perlstein 	struct addrinfo *ai;
245879b86807SEdward Tomasz Napierala 	struct export_args *eap;
24596a09faf2SCraig Rodrigues 	char errmsg[255];
24606a09faf2SCraig Rodrigues 	char *cp;
24618fae3551SRodney W. Grimes 	int done;
24626a09faf2SCraig Rodrigues 	char savedc;
24636a09faf2SCraig Rodrigues 	struct iovec *iov;
2464a9148abdSDoug Rabson 	int i, iovlen;
24656a09faf2SCraig Rodrigues 	int ret;
2466bcc1d071SRick Macklem 	struct nfsex_args nfsea;
2467bcc1d071SRick Macklem 
2468bcc1d071SRick Macklem 	eap = &nfsea.export;
24698fae3551SRodney W. Grimes 
24706a09faf2SCraig Rodrigues 	cp = NULL;
24716a09faf2SCraig Rodrigues 	savedc = '\0';
24726a09faf2SCraig Rodrigues 	iov = NULL;
24736a09faf2SCraig Rodrigues 	iovlen = 0;
24746a09faf2SCraig Rodrigues 	ret = 0;
247560caaee2SIan Dowse 
2476bcc1d071SRick Macklem 	bzero(eap, sizeof (struct export_args));
24776a09faf2SCraig Rodrigues 	bzero(errmsg, sizeof(errmsg));
2478bcc1d071SRick Macklem 	eap->ex_flags = exflags;
2479bcc1d071SRick Macklem 	eap->ex_anon = *anoncrp;
2480bcc1d071SRick Macklem 	eap->ex_indexfile = ep->ex_indexfile;
24816d359f31SIan Dowse 	if (grp->gr_type == GT_HOST)
24828360efbdSAlfred Perlstein 		ai = grp->gr_ptr.gt_addrinfo;
24836d359f31SIan Dowse 	else
24846d359f31SIan Dowse 		ai = NULL;
2485bcc1d071SRick Macklem 	eap->ex_numsecflavors = ep->ex_numsecflavors;
2486bcc1d071SRick Macklem 	for (i = 0; i < eap->ex_numsecflavors; i++)
2487bcc1d071SRick Macklem 		eap->ex_secflavors[i] = ep->ex_secflavors[i];
2488bcc1d071SRick Macklem 	if (eap->ex_numsecflavors == 0) {
2489bcc1d071SRick Macklem 		eap->ex_numsecflavors = 1;
2490bcc1d071SRick Macklem 		eap->ex_secflavors[0] = AUTH_SYS;
2491a9148abdSDoug Rabson 	}
24928fae3551SRodney W. Grimes 	done = FALSE;
24936a09faf2SCraig Rodrigues 
2494bcc1d071SRick Macklem 	if (v4root_phase == 0) {
24956a09faf2SCraig Rodrigues 		build_iovec(&iov, &iovlen, "fstype", NULL, 0);
24966a09faf2SCraig Rodrigues 		build_iovec(&iov, &iovlen, "fspath", NULL, 0);
24976a09faf2SCraig Rodrigues 		build_iovec(&iov, &iovlen, "from", NULL, 0);
24986a09faf2SCraig Rodrigues 		build_iovec(&iov, &iovlen, "update", NULL, 0);
2499bcc1d071SRick Macklem 		build_iovec(&iov, &iovlen, "export", eap,
2500bcc1d071SRick Macklem 		    sizeof (struct export_args));
25016a09faf2SCraig Rodrigues 		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2502bcc1d071SRick Macklem 	}
25036a09faf2SCraig Rodrigues 
25048fae3551SRodney W. Grimes 	while (!done) {
25058fae3551SRodney W. Grimes 		switch (grp->gr_type) {
25068fae3551SRodney W. Grimes 		case GT_HOST:
25076d359f31SIan Dowse 			if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
25088360efbdSAlfred Perlstein 				goto skip;
2509bcc1d071SRick Macklem 			eap->ex_addr = ai->ai_addr;
2510bcc1d071SRick Macklem 			eap->ex_addrlen = ai->ai_addrlen;
2511bcc1d071SRick Macklem 			eap->ex_masklen = 0;
25128fae3551SRodney W. Grimes 			break;
25138fae3551SRodney W. Grimes 		case GT_NET:
251460caaee2SIan Dowse 			if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
25158360efbdSAlfred Perlstein 			    have_v6 == 0)
25168360efbdSAlfred Perlstein 				goto skip;
2517bcc1d071SRick Macklem 			eap->ex_addr =
251860caaee2SIan Dowse 			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2519bcc1d071SRick Macklem 			eap->ex_addrlen =
25206a09faf2SCraig Rodrigues 			    ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2521bcc1d071SRick Macklem 			eap->ex_mask =
252260caaee2SIan Dowse 			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2523bcc1d071SRick Macklem 			eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
25248fae3551SRodney W. Grimes 			break;
25256d359f31SIan Dowse 		case GT_DEFAULT:
2526bcc1d071SRick Macklem 			eap->ex_addr = NULL;
2527bcc1d071SRick Macklem 			eap->ex_addrlen = 0;
2528bcc1d071SRick Macklem 			eap->ex_mask = NULL;
2529bcc1d071SRick Macklem 			eap->ex_masklen = 0;
25306d359f31SIan Dowse 			break;
25318b5a6d67SBill Paul 		case GT_IGNORE:
25326a09faf2SCraig Rodrigues 			ret = 0;
25336a09faf2SCraig Rodrigues 			goto error_exit;
25348b5a6d67SBill Paul 			break;
25358fae3551SRodney W. Grimes 		default:
253674853402SPhilippe Charnier 			syslog(LOG_ERR, "bad grouptype");
25378fae3551SRodney W. Grimes 			if (cp)
25388fae3551SRodney W. Grimes 				*cp = savedc;
25396a09faf2SCraig Rodrigues 			ret = 1;
25406a09faf2SCraig Rodrigues 			goto error_exit;
254180c7cc1cSPedro F. Giffuni 		}
25428fae3551SRodney W. Grimes 
25438fae3551SRodney W. Grimes 		/*
2544bcc1d071SRick Macklem 		 * For V4:, use the nfssvc() syscall, instead of mount().
2545bcc1d071SRick Macklem 		 */
2546bcc1d071SRick Macklem 		if (v4root_phase == 2) {
2547bcc1d071SRick Macklem 			nfsea.fspec = v4root_dirpath;
254879b86807SEdward Tomasz Napierala 			if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2549bcc1d071SRick Macklem 				syslog(LOG_ERR, "Exporting V4: failed");
2550bcc1d071SRick Macklem 				return (2);
2551bcc1d071SRick Macklem 			}
2552bcc1d071SRick Macklem 		} else {
2553bcc1d071SRick Macklem 			/*
25548fae3551SRodney W. Grimes 			 * XXX:
2555bcc1d071SRick Macklem 			 * Maybe I should just use the fsb->f_mntonname path
2556bcc1d071SRick Macklem 			 * instead of looping back up the dirp to the mount
2557bcc1d071SRick Macklem 			 * point??
25588fae3551SRodney W. Grimes 			 * Also, needs to know how to export all types of local
255987564113SPeter Wemm 			 * exportable filesystems and not just "ufs".
25608fae3551SRodney W. Grimes 			 */
25616a09faf2SCraig Rodrigues 			iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
25626a09faf2SCraig Rodrigues 			iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
25636a09faf2SCraig Rodrigues 			iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
25646a09faf2SCraig Rodrigues 			iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
25656a09faf2SCraig Rodrigues 			iov[5].iov_base = fsb->f_mntfromname; /* "from" */
25666a09faf2SCraig Rodrigues 			iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
25674a185fa6SBryan Drewery 			errmsg[0] = '\0';
25686a09faf2SCraig Rodrigues 
256955dd1327SCraig Rodrigues 			while (nmount(iov, iovlen, fsb->f_flags) < 0) {
25708fae3551SRodney W. Grimes 				if (cp)
25718fae3551SRodney W. Grimes 					*cp-- = savedc;
25728fae3551SRodney W. Grimes 				else
25738fae3551SRodney W. Grimes 					cp = dirp + dirplen - 1;
25746a09faf2SCraig Rodrigues 				if (opt_flags & OP_QUIET) {
25756a09faf2SCraig Rodrigues 					ret = 1;
25766a09faf2SCraig Rodrigues 					goto error_exit;
25776a09faf2SCraig Rodrigues 				}
25788fae3551SRodney W. Grimes 				if (errno == EPERM) {
257901709abfSIan Dowse 					if (debug)
258077909162SXin LI 						warnx("can't change attributes for %s: %s",
258177909162SXin LI 						    dirp, errmsg);
25828fae3551SRodney W. Grimes 					syslog(LOG_ERR,
258377909162SXin LI 					   "can't change attributes for %s: %s",
258477909162SXin LI 					    dirp, errmsg);
25856a09faf2SCraig Rodrigues 					ret = 1;
25866a09faf2SCraig Rodrigues 					goto error_exit;
25878fae3551SRodney W. Grimes 				}
25888fae3551SRodney W. Grimes 				if (opt_flags & OP_ALLDIRS) {
2589288fa14aSJoerg Wunsch 					if (errno == EINVAL)
2590288fa14aSJoerg Wunsch 						syslog(LOG_ERR,
2591288fa14aSJoerg Wunsch 		"-alldirs requested but %s is not a filesystem mountpoint",
2592288fa14aSJoerg Wunsch 						    dirp);
2593288fa14aSJoerg Wunsch 					else
2594288fa14aSJoerg Wunsch 						syslog(LOG_ERR,
2595288fa14aSJoerg Wunsch 						    "could not remount %s: %m",
25963980ac4fSGarrett Wollman 						    dirp);
25976a09faf2SCraig Rodrigues 					ret = 1;
25986a09faf2SCraig Rodrigues 					goto error_exit;
25998fae3551SRodney W. Grimes 				}
26008fae3551SRodney W. Grimes 				/* back up over the last component */
26018fae3551SRodney W. Grimes 				while (*cp == '/' && cp > dirp)
26028fae3551SRodney W. Grimes 					cp--;
26038fae3551SRodney W. Grimes 				while (*(cp - 1) != '/' && cp > dirp)
26048fae3551SRodney W. Grimes 					cp--;
26058fae3551SRodney W. Grimes 				if (cp == dirp) {
26068fae3551SRodney W. Grimes 					if (debug)
260774853402SPhilippe Charnier 						warnx("mnt unsucc");
2608bcc1d071SRick Macklem 					syslog(LOG_ERR, "can't export %s %s",
2609bcc1d071SRick Macklem 					    dirp, errmsg);
26106a09faf2SCraig Rodrigues 					ret = 1;
26116a09faf2SCraig Rodrigues 					goto error_exit;
26128fae3551SRodney W. Grimes 				}
26138fae3551SRodney W. Grimes 				savedc = *cp;
26148fae3551SRodney W. Grimes 				*cp = '\0';
2615bcc1d071SRick Macklem 				/*
2616bcc1d071SRick Macklem 				 * Check that we're still on the same
2617bcc1d071SRick Macklem 				 * filesystem.
2618bcc1d071SRick Macklem 				 */
2619bcc1d071SRick Macklem 				if (statfs(dirp, &fsb1) != 0 ||
2620bcc1d071SRick Macklem 				    bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2621bcc1d071SRick Macklem 				    sizeof (fsb1.f_fsid)) != 0) {
2622f93caef2SIan Dowse 					*cp = savedc;
2623bcc1d071SRick Macklem 					syslog(LOG_ERR,
2624bcc1d071SRick Macklem 					    "can't export %s %s", dirp,
262537518a88SCraig Rodrigues 					    errmsg);
26266a09faf2SCraig Rodrigues 					ret = 1;
26276a09faf2SCraig Rodrigues 					goto error_exit;
2628f93caef2SIan Dowse 				}
26298fae3551SRodney W. Grimes 			}
2630bcc1d071SRick Macklem 		}
2631bcc1d071SRick Macklem 
2632bcc1d071SRick Macklem 		/*
2633bcc1d071SRick Macklem 		 * For the experimental server:
2634bcc1d071SRick Macklem 		 * If this is the public directory, get the file handle
2635bcc1d071SRick Macklem 		 * and load it into the kernel via the nfssvc() syscall.
2636bcc1d071SRick Macklem 		 */
263779b86807SEdward Tomasz Napierala 		if ((exflags & MNT_EXPUBLIC) != 0) {
2638bcc1d071SRick Macklem 			fhandle_t fh;
2639bcc1d071SRick Macklem 			char *public_name;
2640bcc1d071SRick Macklem 
2641bcc1d071SRick Macklem 			if (eap->ex_indexfile != NULL)
2642bcc1d071SRick Macklem 				public_name = eap->ex_indexfile;
2643bcc1d071SRick Macklem 			else
2644bcc1d071SRick Macklem 				public_name = dirp;
2645bcc1d071SRick Macklem 			if (getfh(public_name, &fh) < 0)
2646bcc1d071SRick Macklem 				syslog(LOG_ERR,
2647bcc1d071SRick Macklem 				    "Can't get public fh for %s", public_name);
2648bcc1d071SRick Macklem 			else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2649bcc1d071SRick Macklem 				syslog(LOG_ERR,
2650bcc1d071SRick Macklem 				    "Can't set public fh for %s", public_name);
2651bcc1d071SRick Macklem 			else
2652bcc1d071SRick Macklem 				has_publicfh = 1;
2653bcc1d071SRick Macklem 		}
26548360efbdSAlfred Perlstein skip:
26556d359f31SIan Dowse 		if (ai != NULL)
26568360efbdSAlfred Perlstein 			ai = ai->ai_next;
26578360efbdSAlfred Perlstein 		if (ai == NULL)
26588fae3551SRodney W. Grimes 			done = TRUE;
26598fae3551SRodney W. Grimes 	}
26608fae3551SRodney W. Grimes 	if (cp)
26618fae3551SRodney W. Grimes 		*cp = savedc;
26626a09faf2SCraig Rodrigues error_exit:
26636a09faf2SCraig Rodrigues 	/* free strings allocated by strdup() in getmntopts.c */
26646a09faf2SCraig Rodrigues 	if (iov != NULL) {
26656a09faf2SCraig Rodrigues 		free(iov[0].iov_base); /* fstype */
26666a09faf2SCraig Rodrigues 		free(iov[2].iov_base); /* fspath */
26676a09faf2SCraig Rodrigues 		free(iov[4].iov_base); /* from */
26686a09faf2SCraig Rodrigues 		free(iov[6].iov_base); /* update */
26696a09faf2SCraig Rodrigues 		free(iov[8].iov_base); /* export */
26706a09faf2SCraig Rodrigues 		free(iov[10].iov_base); /* errmsg */
26716a09faf2SCraig Rodrigues 
26726a09faf2SCraig Rodrigues 		/* free iov, allocated by realloc() */
26736a09faf2SCraig Rodrigues 		free(iov);
26746a09faf2SCraig Rodrigues 	}
26756a09faf2SCraig Rodrigues 	return (ret);
26768fae3551SRodney W. Grimes }
26778fae3551SRodney W. Grimes 
26788fae3551SRodney W. Grimes /*
26798fae3551SRodney W. Grimes  * Translate a net address.
268060caaee2SIan Dowse  *
268160caaee2SIan Dowse  * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
26828fae3551SRodney W. Grimes  */
268319c46d8cSEdward Tomasz Napierala static int
2684a7a7d96cSPhilippe Charnier get_net(char *cp, struct netmsk *net, int maskflg)
26858fae3551SRodney W. Grimes {
2686931c04f1SIan Dowse 	struct netent *np = NULL;
26878360efbdSAlfred Perlstein 	char *name, *p, *prefp;
268860caaee2SIan Dowse 	struct sockaddr_in sin;
2689931c04f1SIan Dowse 	struct sockaddr *sa = NULL;
26908360efbdSAlfred Perlstein 	struct addrinfo hints, *ai = NULL;
26918360efbdSAlfred Perlstein 	char netname[NI_MAXHOST];
26928360efbdSAlfred Perlstein 	long preflen;
26938fae3551SRodney W. Grimes 
269401709abfSIan Dowse 	p = prefp = NULL;
26958360efbdSAlfred Perlstein 	if ((opt_flags & OP_MASKLEN) && !maskflg) {
26968360efbdSAlfred Perlstein 		p = strchr(cp, '/');
26978360efbdSAlfred Perlstein 		*p = '\0';
26988360efbdSAlfred Perlstein 		prefp = p + 1;
26998360efbdSAlfred Perlstein 	}
27008360efbdSAlfred Perlstein 
2701931c04f1SIan Dowse 	/*
2702931c04f1SIan Dowse 	 * Check for a numeric address first. We wish to avoid
2703931c04f1SIan Dowse 	 * possible DNS lookups in getnetbyname().
2704931c04f1SIan Dowse 	 */
2705931c04f1SIan Dowse 	if (isxdigit(*cp) || *cp == ':') {
27068360efbdSAlfred Perlstein 		memset(&hints, 0, sizeof hints);
270760caaee2SIan Dowse 		/* Ensure the mask and the network have the same family. */
270860caaee2SIan Dowse 		if (maskflg && (opt_flags & OP_NET))
270960caaee2SIan Dowse 			hints.ai_family = net->nt_net.ss_family;
271060caaee2SIan Dowse 		else if (!maskflg && (opt_flags & OP_HAVEMASK))
271160caaee2SIan Dowse 			hints.ai_family = net->nt_mask.ss_family;
271260caaee2SIan Dowse 		else
27138360efbdSAlfred Perlstein 			hints.ai_family = AF_UNSPEC;
27148360efbdSAlfred Perlstein 		hints.ai_flags = AI_NUMERICHOST;
2715931c04f1SIan Dowse 		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2716931c04f1SIan Dowse 			sa = ai->ai_addr;
2717931c04f1SIan Dowse 		if (sa != NULL && ai->ai_family == AF_INET) {
27188fae3551SRodney W. Grimes 			/*
271960caaee2SIan Dowse 			 * The address in `cp' is really a network address, so
272060caaee2SIan Dowse 			 * use inet_network() to re-interpret this correctly.
272160caaee2SIan Dowse 			 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
27228fae3551SRodney W. Grimes 			 */
272360caaee2SIan Dowse 			bzero(&sin, sizeof sin);
27248360efbdSAlfred Perlstein 			sin.sin_family = AF_INET;
27258360efbdSAlfred Perlstein 			sin.sin_len = sizeof sin;
27268360efbdSAlfred Perlstein 			sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
27278360efbdSAlfred Perlstein 			if (debug)
272860caaee2SIan Dowse 				fprintf(stderr, "get_net: v4 addr %s\n",
272960caaee2SIan Dowse 				    inet_ntoa(sin.sin_addr));
27308360efbdSAlfred Perlstein 			sa = (struct sockaddr *)&sin;
2731931c04f1SIan Dowse 		}
2732931c04f1SIan Dowse 	}
2733931c04f1SIan Dowse 	if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2734931c04f1SIan Dowse 		bzero(&sin, sizeof sin);
2735931c04f1SIan Dowse 		sin.sin_family = AF_INET;
2736931c04f1SIan Dowse 		sin.sin_len = sizeof sin;
2737931c04f1SIan Dowse 		sin.sin_addr = inet_makeaddr(np->n_net, 0);
2738931c04f1SIan Dowse 		sa = (struct sockaddr *)&sin;
2739931c04f1SIan Dowse 	}
2740931c04f1SIan Dowse 	if (sa == NULL)
27418360efbdSAlfred Perlstein 		goto fail;
27428360efbdSAlfred Perlstein 
274360caaee2SIan Dowse 	if (maskflg) {
274460caaee2SIan Dowse 		/* The specified sockaddr is a mask. */
274560caaee2SIan Dowse 		if (checkmask(sa) != 0)
27468360efbdSAlfred Perlstein 			goto fail;
274760caaee2SIan Dowse 		bcopy(sa, &net->nt_mask, sa->sa_len);
274860caaee2SIan Dowse 		opt_flags |= OP_HAVEMASK;
274960caaee2SIan Dowse 	} else {
275060caaee2SIan Dowse 		/* The specified sockaddr is a network address. */
275160caaee2SIan Dowse 		bcopy(sa, &net->nt_net, sa->sa_len);
27520f4b7baaSPaul Traina 
275360caaee2SIan Dowse 		/* Get a network name for the export list. */
275460caaee2SIan Dowse 		if (np) {
275560caaee2SIan Dowse 			name = np->n_name;
275660caaee2SIan Dowse 		} else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
27574f101318SHajimu UMEMOTO 		   NULL, 0, NI_NUMERICHOST) == 0) {
275860caaee2SIan Dowse 			name = netname;
275960caaee2SIan Dowse 		} else {
276060caaee2SIan Dowse 			goto fail;
276160caaee2SIan Dowse 		}
276260caaee2SIan Dowse 		if ((net->nt_name = strdup(name)) == NULL)
276360caaee2SIan Dowse 			out_of_mem();
276460caaee2SIan Dowse 
276560caaee2SIan Dowse 		/*
276660caaee2SIan Dowse 		 * Extract a mask from either a "/<masklen>" suffix, or
276760caaee2SIan Dowse 		 * from the class of an IPv4 address.
276860caaee2SIan Dowse 		 */
27698360efbdSAlfred Perlstein 		if (opt_flags & OP_MASKLEN) {
27708360efbdSAlfred Perlstein 			preflen = strtol(prefp, NULL, 10);
277160caaee2SIan Dowse 			if (preflen < 0L || preflen == LONG_MAX)
27728360efbdSAlfred Perlstein 				goto fail;
277360caaee2SIan Dowse 			bcopy(sa, &net->nt_mask, sa->sa_len);
277460caaee2SIan Dowse 			if (makemask(&net->nt_mask, (int)preflen) != 0)
277560caaee2SIan Dowse 				goto fail;
277660caaee2SIan Dowse 			opt_flags |= OP_HAVEMASK;
27778360efbdSAlfred Perlstein 			*p = '/';
277860caaee2SIan Dowse 		} else if (sa->sa_family == AF_INET &&
277960caaee2SIan Dowse 		    (opt_flags & OP_MASK) == 0) {
278060caaee2SIan Dowse 			in_addr_t addr;
27818360efbdSAlfred Perlstein 
278260caaee2SIan Dowse 			addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
278360caaee2SIan Dowse 			if (IN_CLASSA(addr))
278460caaee2SIan Dowse 				preflen = 8;
278560caaee2SIan Dowse 			else if (IN_CLASSB(addr))
278660caaee2SIan Dowse 				preflen = 16;
278760caaee2SIan Dowse 			else if (IN_CLASSC(addr))
278860caaee2SIan Dowse 				preflen = 24;
278960caaee2SIan Dowse 			else if (IN_CLASSD(addr))
279060caaee2SIan Dowse 				preflen = 28;
27918360efbdSAlfred Perlstein 			else
279260caaee2SIan Dowse 				preflen = 32;	/* XXX */
279360caaee2SIan Dowse 
279460caaee2SIan Dowse 			bcopy(sa, &net->nt_mask, sa->sa_len);
279560caaee2SIan Dowse 			makemask(&net->nt_mask, (int)preflen);
279660caaee2SIan Dowse 			opt_flags |= OP_HAVEMASK;
279760caaee2SIan Dowse 		}
27988360efbdSAlfred Perlstein 	}
27998360efbdSAlfred Perlstein 
28008360efbdSAlfred Perlstein 	if (ai)
28018360efbdSAlfred Perlstein 		freeaddrinfo(ai);
28028360efbdSAlfred Perlstein 	return 0;
28038360efbdSAlfred Perlstein 
28048360efbdSAlfred Perlstein fail:
28058360efbdSAlfred Perlstein 	if (ai)
28068360efbdSAlfred Perlstein 		freeaddrinfo(ai);
28078360efbdSAlfred Perlstein 	return 1;
28088fae3551SRodney W. Grimes }
28098fae3551SRodney W. Grimes 
28108fae3551SRodney W. Grimes /*
28118fae3551SRodney W. Grimes  * Parse out the next white space separated field
28128fae3551SRodney W. Grimes  */
281319c46d8cSEdward Tomasz Napierala static void
2814a7a7d96cSPhilippe Charnier nextfield(char **cp, char **endcp)
28158fae3551SRodney W. Grimes {
28168fae3551SRodney W. Grimes 	char *p;
28178fae3551SRodney W. Grimes 
28188fae3551SRodney W. Grimes 	p = *cp;
28198fae3551SRodney W. Grimes 	while (*p == ' ' || *p == '\t')
28208fae3551SRodney W. Grimes 		p++;
28218fae3551SRodney W. Grimes 	if (*p == '\n' || *p == '\0')
28228fae3551SRodney W. Grimes 		*cp = *endcp = p;
28238fae3551SRodney W. Grimes 	else {
28248fae3551SRodney W. Grimes 		*cp = p++;
28258fae3551SRodney W. Grimes 		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
28268fae3551SRodney W. Grimes 			p++;
28278fae3551SRodney W. Grimes 		*endcp = p;
28288fae3551SRodney W. Grimes 	}
28298fae3551SRodney W. Grimes }
28308fae3551SRodney W. Grimes 
28318fae3551SRodney W. Grimes /*
28328fae3551SRodney W. Grimes  * Get an exports file line. Skip over blank lines and handle line
28338fae3551SRodney W. Grimes  * continuations.
28348fae3551SRodney W. Grimes  */
283519c46d8cSEdward Tomasz Napierala static int
2836a7a7d96cSPhilippe Charnier get_line(void)
28378fae3551SRodney W. Grimes {
28388fae3551SRodney W. Grimes 	char *p, *cp;
283991ca1a91SIan Dowse 	size_t len;
28408fae3551SRodney W. Grimes 	int totlen, cont_line;
28418fae3551SRodney W. Grimes 
28428fae3551SRodney W. Grimes 	/*
28438fae3551SRodney W. Grimes 	 * Loop around ignoring blank lines and getting all continuation lines.
28448fae3551SRodney W. Grimes 	 */
28458fae3551SRodney W. Grimes 	p = line;
28468fae3551SRodney W. Grimes 	totlen = 0;
28478fae3551SRodney W. Grimes 	do {
284891ca1a91SIan Dowse 		if ((p = fgetln(exp_file, &len)) == NULL)
28498fae3551SRodney W. Grimes 			return (0);
28508fae3551SRodney W. Grimes 		cp = p + len - 1;
28518fae3551SRodney W. Grimes 		cont_line = 0;
28528fae3551SRodney W. Grimes 		while (cp >= p &&
28538fae3551SRodney W. Grimes 		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
28548fae3551SRodney W. Grimes 			if (*cp == '\\')
28558fae3551SRodney W. Grimes 				cont_line = 1;
28568fae3551SRodney W. Grimes 			cp--;
28578fae3551SRodney W. Grimes 			len--;
28588fae3551SRodney W. Grimes 		}
2859376f8390SDima Dorfman 		if (cont_line) {
2860376f8390SDima Dorfman 			*++cp = ' ';
2861376f8390SDima Dorfman 			len++;
2862376f8390SDima Dorfman 		}
286391ca1a91SIan Dowse 		if (linesize < len + totlen + 1) {
286491ca1a91SIan Dowse 			linesize = len + totlen + 1;
286591ca1a91SIan Dowse 			line = realloc(line, linesize);
286691ca1a91SIan Dowse 			if (line == NULL)
286791ca1a91SIan Dowse 				out_of_mem();
286891ca1a91SIan Dowse 		}
286991ca1a91SIan Dowse 		memcpy(line + totlen, p, len);
28708fae3551SRodney W. Grimes 		totlen += len;
287191ca1a91SIan Dowse 		line[totlen] = '\0';
28728fae3551SRodney W. Grimes 	} while (totlen == 0 || cont_line);
28738fae3551SRodney W. Grimes 	return (1);
28748fae3551SRodney W. Grimes }
28758fae3551SRodney W. Grimes 
28768fae3551SRodney W. Grimes /*
28778fae3551SRodney W. Grimes  * Parse a description of a credential.
28788fae3551SRodney W. Grimes  */
287919c46d8cSEdward Tomasz Napierala static void
2880a7a7d96cSPhilippe Charnier parsecred(char *namelist, struct xucred *cr)
28818fae3551SRodney W. Grimes {
28828fae3551SRodney W. Grimes 	char *name;
28838fae3551SRodney W. Grimes 	int cnt;
28848fae3551SRodney W. Grimes 	char *names;
28858fae3551SRodney W. Grimes 	struct passwd *pw;
28868fae3551SRodney W. Grimes 	struct group *gr;
2887838d9858SBrooks Davis 	gid_t groups[XU_NGROUPS + 1];
2888950cc395SStefan Farfeleder 	int ngroups;
28898fae3551SRodney W. Grimes 
289076183f34SDima Dorfman 	cr->cr_version = XUCRED_VERSION;
28918fae3551SRodney W. Grimes 	/*
289274853402SPhilippe Charnier 	 * Set up the unprivileged user.
28938fae3551SRodney W. Grimes 	 */
2894947572b4SRick Macklem 	cr->cr_uid = 65534;
2895947572b4SRick Macklem 	cr->cr_groups[0] = 65533;
28968fae3551SRodney W. Grimes 	cr->cr_ngroups = 1;
28978fae3551SRodney W. Grimes 	/*
28988fae3551SRodney W. Grimes 	 * Get the user's password table entry.
28998fae3551SRodney W. Grimes 	 */
2900b875c2e9SJosh Paetzel 	names = strsep_quote(&namelist, " \t\n");
29018fae3551SRodney W. Grimes 	name = strsep(&names, ":");
2902b875c2e9SJosh Paetzel 	/* Bug?  name could be NULL here */
29038fae3551SRodney W. Grimes 	if (isdigit(*name) || *name == '-')
29048fae3551SRodney W. Grimes 		pw = getpwuid(atoi(name));
29058fae3551SRodney W. Grimes 	else
29068fae3551SRodney W. Grimes 		pw = getpwnam(name);
29078fae3551SRodney W. Grimes 	/*
29088fae3551SRodney W. Grimes 	 * Credentials specified as those of a user.
29098fae3551SRodney W. Grimes 	 */
29108fae3551SRodney W. Grimes 	if (names == NULL) {
29118fae3551SRodney W. Grimes 		if (pw == NULL) {
291274853402SPhilippe Charnier 			syslog(LOG_ERR, "unknown user: %s", name);
29138fae3551SRodney W. Grimes 			return;
29148fae3551SRodney W. Grimes 		}
29158fae3551SRodney W. Grimes 		cr->cr_uid = pw->pw_uid;
2916838d9858SBrooks Davis 		ngroups = XU_NGROUPS + 1;
29178fae3551SRodney W. Grimes 		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
291874853402SPhilippe Charnier 			syslog(LOG_ERR, "too many groups");
29198fae3551SRodney W. Grimes 		/*
2920950cc395SStefan Farfeleder 		 * Compress out duplicate.
29218fae3551SRodney W. Grimes 		 */
29228fae3551SRodney W. Grimes 		cr->cr_ngroups = ngroups - 1;
29238fae3551SRodney W. Grimes 		cr->cr_groups[0] = groups[0];
29248fae3551SRodney W. Grimes 		for (cnt = 2; cnt < ngroups; cnt++)
29258fae3551SRodney W. Grimes 			cr->cr_groups[cnt - 1] = groups[cnt];
29268fae3551SRodney W. Grimes 		return;
29278fae3551SRodney W. Grimes 	}
29288fae3551SRodney W. Grimes 	/*
29298fae3551SRodney W. Grimes 	 * Explicit credential specified as a colon separated list:
29308fae3551SRodney W. Grimes 	 *	uid:gid:gid:...
29318fae3551SRodney W. Grimes 	 */
29328fae3551SRodney W. Grimes 	if (pw != NULL)
29338fae3551SRodney W. Grimes 		cr->cr_uid = pw->pw_uid;
29348fae3551SRodney W. Grimes 	else if (isdigit(*name) || *name == '-')
29358fae3551SRodney W. Grimes 		cr->cr_uid = atoi(name);
29368fae3551SRodney W. Grimes 	else {
293774853402SPhilippe Charnier 		syslog(LOG_ERR, "unknown user: %s", name);
29388fae3551SRodney W. Grimes 		return;
29398fae3551SRodney W. Grimes 	}
29408fae3551SRodney W. Grimes 	cr->cr_ngroups = 0;
2941838d9858SBrooks Davis 	while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
29428fae3551SRodney W. Grimes 		name = strsep(&names, ":");
29438fae3551SRodney W. Grimes 		if (isdigit(*name) || *name == '-') {
29448fae3551SRodney W. Grimes 			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
29458fae3551SRodney W. Grimes 		} else {
29468fae3551SRodney W. Grimes 			if ((gr = getgrnam(name)) == NULL) {
294774853402SPhilippe Charnier 				syslog(LOG_ERR, "unknown group: %s", name);
29488fae3551SRodney W. Grimes 				continue;
29498fae3551SRodney W. Grimes 			}
29508fae3551SRodney W. Grimes 			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
29518fae3551SRodney W. Grimes 		}
29528fae3551SRodney W. Grimes 	}
2953838d9858SBrooks Davis 	if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
295474853402SPhilippe Charnier 		syslog(LOG_ERR, "too many groups");
29558fae3551SRodney W. Grimes }
29568fae3551SRodney W. Grimes 
29570775314bSDoug Rabson #define	STRSIZ	(MNTNAMLEN+MNTPATHLEN+50)
29588fae3551SRodney W. Grimes /*
29598fae3551SRodney W. Grimes  * Routines that maintain the remote mounttab
29608fae3551SRodney W. Grimes  */
296119c46d8cSEdward Tomasz Napierala static void
2962a7a7d96cSPhilippe Charnier get_mountlist(void)
29638fae3551SRodney W. Grimes {
29648fae3551SRodney W. Grimes 	struct mountlist *mlp, **mlpp;
296587564113SPeter Wemm 	char *host, *dirp, *cp;
29668fae3551SRodney W. Grimes 	char str[STRSIZ];
29678fae3551SRodney W. Grimes 	FILE *mlfile;
29688fae3551SRodney W. Grimes 
29698fae3551SRodney W. Grimes 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
297039539916SBill Fumerola 		if (errno == ENOENT)
297139539916SBill Fumerola 			return;
297239539916SBill Fumerola 		else {
297374853402SPhilippe Charnier 			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
29748fae3551SRodney W. Grimes 			return;
29758fae3551SRodney W. Grimes 		}
297639539916SBill Fumerola 	}
29778fae3551SRodney W. Grimes 	mlpp = &mlhead;
29788fae3551SRodney W. Grimes 	while (fgets(str, STRSIZ, mlfile) != NULL) {
297987564113SPeter Wemm 		cp = str;
298087564113SPeter Wemm 		host = strsep(&cp, " \t\n");
298187564113SPeter Wemm 		dirp = strsep(&cp, " \t\n");
298287564113SPeter Wemm 		if (host == NULL || dirp == NULL)
29838fae3551SRodney W. Grimes 			continue;
29848fae3551SRodney W. Grimes 		mlp = (struct mountlist *)malloc(sizeof (*mlp));
298574853402SPhilippe Charnier 		if (mlp == (struct mountlist *)NULL)
298674853402SPhilippe Charnier 			out_of_mem();
29870775314bSDoug Rabson 		strncpy(mlp->ml_host, host, MNTNAMLEN);
29880775314bSDoug Rabson 		mlp->ml_host[MNTNAMLEN] = '\0';
29890775314bSDoug Rabson 		strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
29900775314bSDoug Rabson 		mlp->ml_dirp[MNTPATHLEN] = '\0';
29918fae3551SRodney W. Grimes 		mlp->ml_next = (struct mountlist *)NULL;
29928fae3551SRodney W. Grimes 		*mlpp = mlp;
29938fae3551SRodney W. Grimes 		mlpp = &mlp->ml_next;
29948fae3551SRodney W. Grimes 	}
29958fae3551SRodney W. Grimes 	fclose(mlfile);
29968fae3551SRodney W. Grimes }
29978fae3551SRodney W. Grimes 
299819c46d8cSEdward Tomasz Napierala static void
299901709abfSIan Dowse del_mlist(char *hostp, char *dirp)
30008fae3551SRodney W. Grimes {
30018fae3551SRodney W. Grimes 	struct mountlist *mlp, **mlpp;
30028fae3551SRodney W. Grimes 	struct mountlist *mlp2;
30038fae3551SRodney W. Grimes 	FILE *mlfile;
30048fae3551SRodney W. Grimes 	int fnd = 0;
30058fae3551SRodney W. Grimes 
30068fae3551SRodney W. Grimes 	mlpp = &mlhead;
30078fae3551SRodney W. Grimes 	mlp = mlhead;
30088fae3551SRodney W. Grimes 	while (mlp) {
30098fae3551SRodney W. Grimes 		if (!strcmp(mlp->ml_host, hostp) &&
30108fae3551SRodney W. Grimes 		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
30118fae3551SRodney W. Grimes 			fnd = 1;
30128fae3551SRodney W. Grimes 			mlp2 = mlp;
30138fae3551SRodney W. Grimes 			*mlpp = mlp = mlp->ml_next;
30148fae3551SRodney W. Grimes 			free((caddr_t)mlp2);
30158fae3551SRodney W. Grimes 		} else {
30168fae3551SRodney W. Grimes 			mlpp = &mlp->ml_next;
30178fae3551SRodney W. Grimes 			mlp = mlp->ml_next;
30188fae3551SRodney W. Grimes 		}
30198fae3551SRodney W. Grimes 	}
30208fae3551SRodney W. Grimes 	if (fnd) {
30218fae3551SRodney W. Grimes 		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
302274853402SPhilippe Charnier 			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
30238fae3551SRodney W. Grimes 			return;
30248fae3551SRodney W. Grimes 		}
30258fae3551SRodney W. Grimes 		mlp = mlhead;
30268fae3551SRodney W. Grimes 		while (mlp) {
30278fae3551SRodney W. Grimes 			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
30288fae3551SRodney W. Grimes 			mlp = mlp->ml_next;
30298fae3551SRodney W. Grimes 		}
30308fae3551SRodney W. Grimes 		fclose(mlfile);
30318fae3551SRodney W. Grimes 	}
30328fae3551SRodney W. Grimes }
30338fae3551SRodney W. Grimes 
303419c46d8cSEdward Tomasz Napierala static void
3035a7a7d96cSPhilippe Charnier add_mlist(char *hostp, char *dirp)
30368fae3551SRodney W. Grimes {
30378fae3551SRodney W. Grimes 	struct mountlist *mlp, **mlpp;
30388fae3551SRodney W. Grimes 	FILE *mlfile;
30398fae3551SRodney W. Grimes 
30408fae3551SRodney W. Grimes 	mlpp = &mlhead;
30418fae3551SRodney W. Grimes 	mlp = mlhead;
30428fae3551SRodney W. Grimes 	while (mlp) {
30438fae3551SRodney W. Grimes 		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
30448fae3551SRodney W. Grimes 			return;
30458fae3551SRodney W. Grimes 		mlpp = &mlp->ml_next;
30468fae3551SRodney W. Grimes 		mlp = mlp->ml_next;
30478fae3551SRodney W. Grimes 	}
30488fae3551SRodney W. Grimes 	mlp = (struct mountlist *)malloc(sizeof (*mlp));
304974853402SPhilippe Charnier 	if (mlp == (struct mountlist *)NULL)
305074853402SPhilippe Charnier 		out_of_mem();
30510775314bSDoug Rabson 	strncpy(mlp->ml_host, hostp, MNTNAMLEN);
30520775314bSDoug Rabson 	mlp->ml_host[MNTNAMLEN] = '\0';
30530775314bSDoug Rabson 	strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
30540775314bSDoug Rabson 	mlp->ml_dirp[MNTPATHLEN] = '\0';
30558fae3551SRodney W. Grimes 	mlp->ml_next = (struct mountlist *)NULL;
30568fae3551SRodney W. Grimes 	*mlpp = mlp;
30578fae3551SRodney W. Grimes 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
305874853402SPhilippe Charnier 		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
30598fae3551SRodney W. Grimes 		return;
30608fae3551SRodney W. Grimes 	}
30618fae3551SRodney W. Grimes 	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
30628fae3551SRodney W. Grimes 	fclose(mlfile);
30638fae3551SRodney W. Grimes }
30648fae3551SRodney W. Grimes 
30658fae3551SRodney W. Grimes /*
30668fae3551SRodney W. Grimes  * Free up a group list.
30678fae3551SRodney W. Grimes  */
306819c46d8cSEdward Tomasz Napierala static void
3069a7a7d96cSPhilippe Charnier free_grp(struct grouplist *grp)
30708fae3551SRodney W. Grimes {
30718fae3551SRodney W. Grimes 	if (grp->gr_type == GT_HOST) {
30728360efbdSAlfred Perlstein 		if (grp->gr_ptr.gt_addrinfo != NULL)
30738360efbdSAlfred Perlstein 			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
30748fae3551SRodney W. Grimes 	} else if (grp->gr_type == GT_NET) {
30758fae3551SRodney W. Grimes 		if (grp->gr_ptr.gt_net.nt_name)
30768fae3551SRodney W. Grimes 			free(grp->gr_ptr.gt_net.nt_name);
30778fae3551SRodney W. Grimes 	}
30788fae3551SRodney W. Grimes 	free((caddr_t)grp);
30798fae3551SRodney W. Grimes }
30808fae3551SRodney W. Grimes 
30818fae3551SRodney W. Grimes #ifdef DEBUG
308219c46d8cSEdward Tomasz Napierala static void
30838fae3551SRodney W. Grimes SYSLOG(int pri, const char *fmt, ...)
30848fae3551SRodney W. Grimes {
30858fae3551SRodney W. Grimes 	va_list ap;
30868fae3551SRodney W. Grimes 
30878fae3551SRodney W. Grimes 	va_start(ap, fmt);
30888fae3551SRodney W. Grimes 	vfprintf(stderr, fmt, ap);
30898fae3551SRodney W. Grimes 	va_end(ap);
30908fae3551SRodney W. Grimes }
30918fae3551SRodney W. Grimes #endif /* DEBUG */
30928fae3551SRodney W. Grimes 
30938fae3551SRodney W. Grimes /*
30948fae3551SRodney W. Grimes  * Check options for consistency.
30958fae3551SRodney W. Grimes  */
309619c46d8cSEdward Tomasz Napierala static int
3097a7a7d96cSPhilippe Charnier check_options(struct dirlist *dp)
30988fae3551SRodney W. Grimes {
30998fae3551SRodney W. Grimes 
3100bcc1d071SRick Macklem 	if (v4root_phase == 0 && dp == NULL)
31018fae3551SRodney W. Grimes 	    return (1);
310291196234SPeter Wemm 	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
310391196234SPeter Wemm 	    syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
31048fae3551SRodney W. Grimes 	    return (1);
31058fae3551SRodney W. Grimes 	}
31068fae3551SRodney W. Grimes 	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
310760caaee2SIan Dowse 		syslog(LOG_ERR, "-mask requires -network");
310860caaee2SIan Dowse 		return (1);
310960caaee2SIan Dowse 	}
311060caaee2SIan Dowse 	if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
311160caaee2SIan Dowse 		syslog(LOG_ERR, "-network requires mask specification");
311260caaee2SIan Dowse 		return (1);
311360caaee2SIan Dowse 	}
311460caaee2SIan Dowse 	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
311560caaee2SIan Dowse 		syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
31168fae3551SRodney W. Grimes 		return (1);
31178fae3551SRodney W. Grimes 	}
3118bcc1d071SRick Macklem 	if (v4root_phase > 0 &&
3119bcc1d071SRick Macklem 	    (opt_flags &
3120bcc1d071SRick Macklem 	     ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3121bcc1d071SRick Macklem 	    syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3122bcc1d071SRick Macklem 	    return (1);
3123bcc1d071SRick Macklem 	}
312456cfc5edSRick Macklem 	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
312556cfc5edSRick Macklem 	    syslog(LOG_ERR, "-alldirs has multiple directories");
312656cfc5edSRick Macklem 	    return (1);
312756cfc5edSRick Macklem 	}
31288fae3551SRodney W. Grimes 	return (0);
31298fae3551SRodney W. Grimes }
31308fae3551SRodney W. Grimes 
31318fae3551SRodney W. Grimes /*
31328fae3551SRodney W. Grimes  * Check an absolute directory path for any symbolic links. Return true
31338fae3551SRodney W. Grimes  */
313419c46d8cSEdward Tomasz Napierala static int
3135a7a7d96cSPhilippe Charnier check_dirpath(char *dirp)
31368fae3551SRodney W. Grimes {
31378fae3551SRodney W. Grimes 	char *cp;
31388fae3551SRodney W. Grimes 	int ret = 1;
31398fae3551SRodney W. Grimes 	struct stat sb;
31408fae3551SRodney W. Grimes 
31418fae3551SRodney W. Grimes 	cp = dirp + 1;
31428fae3551SRodney W. Grimes 	while (*cp && ret) {
31438fae3551SRodney W. Grimes 		if (*cp == '/') {
31448fae3551SRodney W. Grimes 			*cp = '\0';
3145a62dc406SDoug Rabson 			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
31468fae3551SRodney W. Grimes 				ret = 0;
31478fae3551SRodney W. Grimes 			*cp = '/';
31488fae3551SRodney W. Grimes 		}
31498fae3551SRodney W. Grimes 		cp++;
31508fae3551SRodney W. Grimes 	}
3151a62dc406SDoug Rabson 	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
31528fae3551SRodney W. Grimes 		ret = 0;
31538fae3551SRodney W. Grimes 	return (ret);
31548fae3551SRodney W. Grimes }
3155a62dc406SDoug Rabson 
315660caaee2SIan Dowse /*
315760caaee2SIan Dowse  * Make a netmask according to the specified prefix length. The ss_family
315860caaee2SIan Dowse  * and other non-address fields must be initialised before calling this.
315960caaee2SIan Dowse  */
316019c46d8cSEdward Tomasz Napierala static int
316160caaee2SIan Dowse makemask(struct sockaddr_storage *ssp, int bitlen)
31628360efbdSAlfred Perlstein {
316360caaee2SIan Dowse 	u_char *p;
316460caaee2SIan Dowse 	int bits, i, len;
31658360efbdSAlfred Perlstein 
316660caaee2SIan Dowse 	if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
316760caaee2SIan Dowse 		return (-1);
316889fdc4e1SMike Barcroft 	if (bitlen > len * CHAR_BIT)
316960caaee2SIan Dowse 		return (-1);
31708360efbdSAlfred Perlstein 
317160caaee2SIan Dowse 	for (i = 0; i < len; i++) {
3172a175f065SMarcelo Araujo 		bits = MIN(CHAR_BIT, bitlen);
317358202d89SRuslan Ermilov 		*p++ = (u_char)~0 << (CHAR_BIT - bits);
317460caaee2SIan Dowse 		bitlen -= bits;
31758360efbdSAlfred Perlstein 	}
31768360efbdSAlfred Perlstein 	return 0;
31778360efbdSAlfred Perlstein }
31788360efbdSAlfred Perlstein 
317960caaee2SIan Dowse /*
318060caaee2SIan Dowse  * Check that the sockaddr is a valid netmask. Returns 0 if the mask
318160caaee2SIan Dowse  * is acceptable (i.e. of the form 1...10....0).
318260caaee2SIan Dowse  */
318319c46d8cSEdward Tomasz Napierala static int
318460caaee2SIan Dowse checkmask(struct sockaddr *sa)
31858360efbdSAlfred Perlstein {
318660caaee2SIan Dowse 	u_char *mask;
318760caaee2SIan Dowse 	int i, len;
318860caaee2SIan Dowse 
318960caaee2SIan Dowse 	if ((mask = sa_rawaddr(sa, &len)) == NULL)
319060caaee2SIan Dowse 		return (-1);
319160caaee2SIan Dowse 
319260caaee2SIan Dowse 	for (i = 0; i < len; i++)
319360caaee2SIan Dowse 		if (mask[i] != 0xff)
319460caaee2SIan Dowse 			break;
319560caaee2SIan Dowse 	if (i < len) {
319660caaee2SIan Dowse 		if (~mask[i] & (u_char)(~mask[i] + 1))
319760caaee2SIan Dowse 			return (-1);
319860caaee2SIan Dowse 		i++;
319960caaee2SIan Dowse 	}
320060caaee2SIan Dowse 	for (; i < len; i++)
320160caaee2SIan Dowse 		if (mask[i] != 0)
320260caaee2SIan Dowse 			return (-1);
320360caaee2SIan Dowse 	return (0);
320460caaee2SIan Dowse }
320560caaee2SIan Dowse 
320660caaee2SIan Dowse /*
320760caaee2SIan Dowse  * Compare two sockaddrs according to a specified mask. Return zero if
320860caaee2SIan Dowse  * `sa1' matches `sa2' when filtered by the netmask in `samask'.
32093df5ecacSUlrich Spörlein  * If samask is NULL, perform a full comparison.
321060caaee2SIan Dowse  */
321119c46d8cSEdward Tomasz Napierala static int
321260caaee2SIan Dowse sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
321360caaee2SIan Dowse {
321460caaee2SIan Dowse 	unsigned char *p1, *p2, *mask;
321560caaee2SIan Dowse 	int len, i;
321660caaee2SIan Dowse 
321760caaee2SIan Dowse 	if (sa1->sa_family != sa2->sa_family ||
321860caaee2SIan Dowse 	    (p1 = sa_rawaddr(sa1, &len)) == NULL ||
321960caaee2SIan Dowse 	    (p2 = sa_rawaddr(sa2, NULL)) == NULL)
322060caaee2SIan Dowse 		return (1);
322160caaee2SIan Dowse 
322260caaee2SIan Dowse 	switch (sa1->sa_family) {
322360caaee2SIan Dowse 	case AF_INET6:
322460caaee2SIan Dowse 		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
322560caaee2SIan Dowse 		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
322660caaee2SIan Dowse 			return (1);
322760caaee2SIan Dowse 		break;
322860caaee2SIan Dowse 	}
322960caaee2SIan Dowse 
323060caaee2SIan Dowse 	/* Simple binary comparison if no mask specified. */
323160caaee2SIan Dowse 	if (samask == NULL)
323260caaee2SIan Dowse 		return (memcmp(p1, p2, len));
323360caaee2SIan Dowse 
323460caaee2SIan Dowse 	/* Set up the mask, and do a mask-based comparison. */
323560caaee2SIan Dowse 	if (sa1->sa_family != samask->sa_family ||
323660caaee2SIan Dowse 	    (mask = sa_rawaddr(samask, NULL)) == NULL)
323760caaee2SIan Dowse 		return (1);
323860caaee2SIan Dowse 
323960caaee2SIan Dowse 	for (i = 0; i < len; i++)
324060caaee2SIan Dowse 		if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
324160caaee2SIan Dowse 			return (1);
324260caaee2SIan Dowse 	return (0);
324360caaee2SIan Dowse }
324460caaee2SIan Dowse 
324560caaee2SIan Dowse /*
324660caaee2SIan Dowse  * Return a pointer to the part of the sockaddr that contains the
324760caaee2SIan Dowse  * raw address, and set *nbytes to its length in bytes. Returns
324860caaee2SIan Dowse  * NULL if the address family is unknown.
324960caaee2SIan Dowse  */
325019c46d8cSEdward Tomasz Napierala static void *
325160caaee2SIan Dowse sa_rawaddr(struct sockaddr *sa, int *nbytes) {
325260caaee2SIan Dowse 	void *p;
325360caaee2SIan Dowse 	int len;
32548360efbdSAlfred Perlstein 
32558360efbdSAlfred Perlstein 	switch (sa->sa_family) {
32568360efbdSAlfred Perlstein 	case AF_INET:
325760caaee2SIan Dowse 		len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
325860caaee2SIan Dowse 		p = &((struct sockaddr_in *)sa)->sin_addr;
32598360efbdSAlfred Perlstein 		break;
32608360efbdSAlfred Perlstein 	case AF_INET6:
326160caaee2SIan Dowse 		len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
326260caaee2SIan Dowse 		p = &((struct sockaddr_in6 *)sa)->sin6_addr;
32638360efbdSAlfred Perlstein 		break;
32648360efbdSAlfred Perlstein 	default:
326560caaee2SIan Dowse 		p = NULL;
326660caaee2SIan Dowse 		len = 0;
32678360efbdSAlfred Perlstein 	}
32688360efbdSAlfred Perlstein 
326960caaee2SIan Dowse 	if (nbytes != NULL)
327060caaee2SIan Dowse 		*nbytes = len;
327160caaee2SIan Dowse 	return (p);
32728360efbdSAlfred Perlstein }
32738360efbdSAlfred Perlstein 
327419c46d8cSEdward Tomasz Napierala static void
3275a7a7d96cSPhilippe Charnier huphandler(int sig __unused)
327669d65572SIan Dowse {
327719c46d8cSEdward Tomasz Napierala 
327869d65572SIan Dowse 	got_sighup = 1;
327969d65572SIan Dowse }
328069d65572SIan Dowse 
328119c46d8cSEdward Tomasz Napierala static void
328219c46d8cSEdward Tomasz Napierala terminate(int sig __unused)
32838360efbdSAlfred Perlstein {
3284a032b226SPawel Jakub Dawidek 	pidfile_remove(pfh);
32850775314bSDoug Rabson 	rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
32860775314bSDoug Rabson 	rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
32878360efbdSAlfred Perlstein 	exit (0);
32888360efbdSAlfred Perlstein }
3289