mountd.c revision 329392
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1989, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * This code is derived from software contributed to Berkeley by
61558Srgrimes * Herb Hasler and Rick Macklem at The University of Guelph.
71558Srgrimes *
81558Srgrimes * Redistribution and use in source and binary forms, with or without
91558Srgrimes * modification, are permitted provided that the following conditions
101558Srgrimes * are met:
111558Srgrimes * 1. Redistributions of source code must retain the above copyright
121558Srgrimes *    notice, this list of conditions and the following disclaimer.
131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer in the
151558Srgrimes *    documentation and/or other materials provided with the distribution.
161558Srgrimes * 4. Neither the name of the University nor the names of its contributors
171558Srgrimes *    may be used to endorse or promote products derived from this software
181558Srgrimes *    without specific prior written permission.
191558Srgrimes *
201558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301558Srgrimes * SUCH DAMAGE.
311558Srgrimes */
321558Srgrimes
331558Srgrimes#ifndef lint
3437663Scharnierstatic const char copyright[] =
351558Srgrimes"@(#) Copyright (c) 1989, 1993\n\
361558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
372999Swollman#endif /*not lint*/
381558Srgrimes
39105267Scharnier#if 0
401558Srgrimes#ifndef lint
4137663Scharnierstatic char sccsid[] = "@(#)mountd.c	8.15 (Berkeley) 5/1/95";
42105267Scharnier#endif /*not lint*/
4337663Scharnier#endif
441558Srgrimes
45105267Scharnier#include <sys/cdefs.h>
46105267Scharnier__FBSDID("$FreeBSD: stable/11/usr.sbin/mountd/mountd.c 329392 2018-02-16 18:49:50Z brd $");
47105267Scharnier
481558Srgrimes#include <sys/param.h>
49192934Srmacklem#include <sys/fcntl.h>
50192934Srmacklem#include <sys/linker.h>
51192934Srmacklem#include <sys/module.h>
521558Srgrimes#include <sys/mount.h>
53324955Smanu#include <sys/queue.h>
541558Srgrimes#include <sys/stat.h>
55192934Srmacklem#include <sys/sysctl.h>
561558Srgrimes#include <sys/syslog.h>
571558Srgrimes
581558Srgrimes#include <rpc/rpc.h>
59109363Smbr#include <rpc/rpc_com.h>
601558Srgrimes#include <rpc/pmap_clnt.h>
6174462Salfred#include <rpc/pmap_prot.h>
6274462Salfred#include <rpcsvc/mount.h>
639336Sdfr#include <nfs/nfsproto.h>
64192934Srmacklem#include <nfs/nfssvc.h>
6583653Speter#include <nfsserver/nfs.h>
661558Srgrimes
67192934Srmacklem#include <fs/nfs/nfsport.h>
68192934Srmacklem
691558Srgrimes#include <arpa/inet.h>
701558Srgrimes
711558Srgrimes#include <ctype.h>
7237663Scharnier#include <err.h>
731558Srgrimes#include <errno.h>
741558Srgrimes#include <grp.h>
75149433Spjd#include <libutil.h>
76103949Smike#include <limits.h>
771558Srgrimes#include <netdb.h>
781558Srgrimes#include <pwd.h>
791558Srgrimes#include <signal.h>
801558Srgrimes#include <stdio.h>
811558Srgrimes#include <stdlib.h>
821558Srgrimes#include <string.h>
831558Srgrimes#include <unistd.h>
841558Srgrimes#include "pathnames.h"
85158857Srodrigc#include "mntopts.h"
861558Srgrimes
871558Srgrimes#ifdef DEBUG
881558Srgrimes#include <stdarg.h>
891558Srgrimes#endif
901558Srgrimes
911558Srgrimes/*
921558Srgrimes * Structures for keeping the mount list and export list
931558Srgrimes */
941558Srgrimesstruct mountlist {
95194880Sdfr	char	ml_host[MNTNAMLEN+1];
96194880Sdfr	char	ml_dirp[MNTPATHLEN+1];
97324955Smanu
98324955Smanu	SLIST_ENTRY(mountlist)	next;
991558Srgrimes};
1001558Srgrimes
1011558Srgrimesstruct dirlist {
1021558Srgrimes	struct dirlist	*dp_left;
1031558Srgrimes	struct dirlist	*dp_right;
1041558Srgrimes	int		dp_flag;
1051558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
106324234Smanu	char		*dp_dirp;
1071558Srgrimes};
1081558Srgrimes/* dp_flag bits */
1091558Srgrimes#define	DP_DEFSET	0x1
1109336Sdfr#define DP_HOSTSET	0x2
1111558Srgrimes
1121558Srgrimesstruct exportlist {
1131558Srgrimes	struct dirlist	*ex_dirl;
1141558Srgrimes	struct dirlist	*ex_defdir;
1151558Srgrimes	int		ex_flag;
1161558Srgrimes	fsid_t		ex_fs;
1171558Srgrimes	char		*ex_fsdir;
11827447Sdfr	char		*ex_indexfile;
119184588Sdfr	int		ex_numsecflavors;
120184588Sdfr	int		ex_secflavors[MAXSECFLAVORS];
121240902Srmacklem	int		ex_defnumsecflavors;
122240902Srmacklem	int		ex_defsecflavors[MAXSECFLAVORS];
123324955Smanu
124324955Smanu	SLIST_ENTRY(exportlist) entries;
1251558Srgrimes};
1261558Srgrimes/* ex_flag bits */
1271558Srgrimes#define	EX_LINKED	0x1
1281558Srgrimes
1291558Srgrimesstruct netmsk {
13074462Salfred	struct sockaddr_storage nt_net;
13175801Siedowse	struct sockaddr_storage nt_mask;
13242144Sdfr	char		*nt_name;
1331558Srgrimes};
1341558Srgrimes
1351558Srgrimesunion grouptypes {
13674462Salfred	struct addrinfo *gt_addrinfo;
1371558Srgrimes	struct netmsk	gt_net;
1381558Srgrimes};
1391558Srgrimes
1401558Srgrimesstruct grouplist {
1411558Srgrimes	int gr_type;
1421558Srgrimes	union grouptypes gr_ptr;
1431558Srgrimes	struct grouplist *gr_next;
144240902Srmacklem	int gr_numsecflavors;
145240902Srmacklem	int gr_secflavors[MAXSECFLAVORS];
1461558Srgrimes};
1471558Srgrimes/* Group types */
1481558Srgrimes#define	GT_NULL		0x0
1491558Srgrimes#define	GT_HOST		0x1
1501558Srgrimes#define	GT_NET		0x2
15175641Siedowse#define	GT_DEFAULT	0x3
1527401Swpaul#define GT_IGNORE	0x5
1531558Srgrimes
1541558Srgrimesstruct hostlist {
1559336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1561558Srgrimes	struct grouplist *ht_grp;
1571558Srgrimes	struct hostlist	 *ht_next;
1581558Srgrimes};
1591558Srgrimes
1609336Sdfrstruct fhreturn {
1619336Sdfr	int	fhr_flag;
1629336Sdfr	int	fhr_vers;
1639336Sdfr	nfsfh_t	fhr_fh;
164184588Sdfr	int	fhr_numsecflavors;
165184588Sdfr	int	*fhr_secflavors;
1669336Sdfr};
1679336Sdfr
168222623Srmacklem#define	GETPORT_MAXTRY	20	/* Max tries to get a port # */
169222623Srmacklem
1701558Srgrimes/* Global defs */
171285128Straszstatic char	*add_expdir(struct dirlist **, char *, int);
172285128Straszstatic void	add_dlist(struct dirlist **, struct dirlist *,
173285128Strasz		    struct grouplist *, int, struct exportlist *);
174285128Straszstatic void	add_mlist(char *, char *);
175285128Straszstatic int	check_dirpath(char *);
176285128Straszstatic int	check_options(struct dirlist *);
177285128Straszstatic int	checkmask(struct sockaddr *sa);
178285128Straszstatic int	chk_host(struct dirlist *, struct sockaddr *, int *, int *,
179285128Strasz		    int *, int **);
180293305Sjpaetzelstatic char	*strsep_quote(char **stringp, const char *delim);
181222623Srmacklemstatic int	create_service(struct netconfig *nconf);
182222623Srmacklemstatic void	complete_service(struct netconfig *nconf, char *port_str);
183222623Srmacklemstatic void	clearout_service(void);
184285128Straszstatic void	del_mlist(char *hostp, char *dirp);
185285128Straszstatic struct dirlist	*dirp_search(struct dirlist *, char *);
186285128Straszstatic int	do_mount(struct exportlist *, struct grouplist *, int,
187285128Strasz		    struct xucred *, char *, int, struct statfs *);
188285128Straszstatic int	do_opt(char **, char **, struct exportlist *,
189285128Strasz		    struct grouplist *, int *, int *, struct xucred *);
190285128Straszstatic struct exportlist	*ex_search(fsid_t *);
191285128Straszstatic struct exportlist	*get_exp(void);
192285128Straszstatic void	free_dir(struct dirlist *);
193285128Straszstatic void	free_exp(struct exportlist *);
194285128Straszstatic void	free_grp(struct grouplist *);
195285128Straszstatic void	free_host(struct hostlist *);
196285128Straszstatic void	get_exportlist(void);
197285128Straszstatic int	get_host(char *, struct grouplist *, struct grouplist *);
198285128Straszstatic struct hostlist *get_ht(void);
199285128Straszstatic int	get_line(void);
200285128Straszstatic void	get_mountlist(void);
201285128Straszstatic int	get_net(char *, struct netmsk *, int);
202329392Sbrdstatic void	getexp_err(struct exportlist *, struct grouplist *, const char *);
203285128Straszstatic struct grouplist	*get_grp(void);
204285128Straszstatic void	hang_dirp(struct dirlist *, struct grouplist *,
20592882Simp				struct exportlist *, int);
206285128Straszstatic void	huphandler(int sig);
207285128Straszstatic int	makemask(struct sockaddr_storage *ssp, int bitlen);
208285128Straszstatic void	mntsrv(struct svc_req *, SVCXPRT *);
209285128Straszstatic void	nextfield(char **, char **);
210285128Straszstatic void	out_of_mem(void);
211285128Straszstatic void	parsecred(char *, struct xucred *);
212285128Straszstatic int	parsesec(char *, struct exportlist *);
213285128Straszstatic int	put_exlist(struct dirlist *, XDR *, struct dirlist *,
214285128Strasz		    int *, int);
215285128Straszstatic void	*sa_rawaddr(struct sockaddr *sa, int *nbytes);
216285128Straszstatic int	sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
217285128Strasz		    struct sockaddr *samask);
218285128Straszstatic int	scan_tree(struct dirlist *, struct sockaddr *);
219285128Straszstatic void	usage(void);
220285128Straszstatic int	xdr_dir(XDR *, char *);
221285128Straszstatic int	xdr_explist(XDR *, caddr_t);
222285128Straszstatic int	xdr_explist_brief(XDR *, caddr_t);
223285128Straszstatic int	xdr_explist_common(XDR *, caddr_t, int);
224285128Straszstatic int	xdr_fhs(XDR *, caddr_t);
225285128Straszstatic int	xdr_mlist(XDR *, caddr_t);
226285128Straszstatic void	terminate(int);
2271558Srgrimes
228324955Smanustatic SLIST_HEAD(, exportlist) exphead = SLIST_HEAD_INITIALIZER(exphead);
229324955Smanustatic SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(mlhead);
230285128Straszstatic struct grouplist *grphead;
231285128Straszstatic char *exnames_default[2] = { _PATH_EXPORTS, NULL };
232285128Straszstatic char **exnames;
233285128Straszstatic char **hosts = NULL;
234285128Straszstatic struct xucred def_anon = {
23591354Sdd	XUCRED_VERSION,
23672650Sgreen	(uid_t)-2,
2371558Srgrimes	1,
23872650Sgreen	{ (gid_t)-2 },
23972650Sgreen	NULL
2401558Srgrimes};
241285128Straszstatic int force_v2 = 0;
242285128Straszstatic int resvport_only = 1;
243285128Straszstatic int nhosts = 0;
244285128Straszstatic int dir_only = 1;
245285128Straszstatic int dolog = 0;
246285128Straszstatic int got_sighup = 0;
247285128Straszstatic int xcreated = 0;
24874462Salfred
249285128Straszstatic char *svcport_str = NULL;
250285128Straszstatic int mallocd_svcport = 0;
251285128Straszstatic int *sock_fd;
252285128Straszstatic int sock_fdcnt;
253285128Straszstatic int sock_fdpos;
254285128Straszstatic int suspend_nfsd = 0;
255172827Smatteo
256285128Straszstatic int opt_flags;
25774462Salfredstatic int have_v6 = 1;
25874462Salfred
259285128Straszstatic int v4root_phase = 0;
260285128Straszstatic char v4root_dirpath[PATH_MAX + 1];
261285128Straszstatic int has_publicfh = 0;
262192934Srmacklem
263285128Straszstatic struct pidfh *pfh = NULL;
26475801Siedowse/* Bits for opt_flags above */
2651558Srgrimes#define	OP_MAPROOT	0x01
2661558Srgrimes#define	OP_MAPALL	0x02
26783653Speter/* 0x4 free */
2681558Srgrimes#define	OP_MASK		0x08
2691558Srgrimes#define	OP_NET		0x10
2701558Srgrimes#define	OP_ALLDIRS	0x40
27175801Siedowse#define	OP_HAVEMASK	0x80	/* A mask was specified or inferred. */
272100336Sjoerg#define	OP_QUIET	0x100
27374462Salfred#define OP_MASKLEN	0x200
274184588Sdfr#define OP_SEC		0x400
2751558Srgrimes
2761558Srgrimes#ifdef DEBUG
277285128Straszstatic int debug = 1;
278285128Straszstatic void	SYSLOG(int, const char *, ...) __printflike(2, 3);
2791558Srgrimes#define syslog SYSLOG
2801558Srgrimes#else
281285128Straszstatic int debug = 0;
2821558Srgrimes#endif
2831558Srgrimes
2841558Srgrimes/*
285293305Sjpaetzel * Similar to strsep(), but it allows for quoted strings
286293305Sjpaetzel * and escaped characters.
287293305Sjpaetzel *
288293305Sjpaetzel * It returns the string (or NULL, if *stringp is NULL),
289293305Sjpaetzel * which is a de-quoted version of the string if necessary.
290293305Sjpaetzel *
291293305Sjpaetzel * It modifies *stringp in place.
292293305Sjpaetzel */
293293305Sjpaetzelstatic char *
294293305Sjpaetzelstrsep_quote(char **stringp, const char *delim)
295293305Sjpaetzel{
296293305Sjpaetzel	char *srcptr, *dstptr, *retval;
297293305Sjpaetzel	char quot = 0;
298293305Sjpaetzel
299293305Sjpaetzel	if (stringp == NULL || *stringp == NULL)
300293305Sjpaetzel		return (NULL);
301293305Sjpaetzel
302293305Sjpaetzel	srcptr = dstptr = retval = *stringp;
303293305Sjpaetzel
304293305Sjpaetzel	while (*srcptr) {
305293305Sjpaetzel		/*
306293305Sjpaetzel		 * We're looking for several edge cases here.
307293305Sjpaetzel		 * First:  if we're in quote state (quot != 0),
308293305Sjpaetzel		 * then we ignore the delim characters, but otherwise
309293305Sjpaetzel		 * process as normal, unless it is the quote character.
310293305Sjpaetzel		 * Second:  if the current character is a backslash,
311293305Sjpaetzel		 * we take the next character as-is, without checking
312293305Sjpaetzel		 * for delim, quote, or backslash.  Exception:  if the
313293305Sjpaetzel		 * next character is a NUL, that's the end of the string.
314293305Sjpaetzel		 * Third:  if the character is a quote character, we toggle
315293305Sjpaetzel		 * quote state.
316293305Sjpaetzel		 * Otherwise:  check the current character for NUL, or
317293305Sjpaetzel		 * being in delim, and end the string if either is true.
318293305Sjpaetzel		 */
319293305Sjpaetzel		if (*srcptr == '\\') {
320293305Sjpaetzel			srcptr++;
321293305Sjpaetzel			/*
322293305Sjpaetzel			 * The edge case here is if the next character
323293305Sjpaetzel			 * is NUL, we want to stop processing.  But if
324293305Sjpaetzel			 * it's not NUL, then we simply want to copy it.
325293305Sjpaetzel			 */
326293305Sjpaetzel			if (*srcptr) {
327293305Sjpaetzel				*dstptr++ = *srcptr++;
328293305Sjpaetzel			}
329293305Sjpaetzel			continue;
330293305Sjpaetzel		}
331293305Sjpaetzel		if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
332293305Sjpaetzel			quot = *srcptr++;
333293305Sjpaetzel			continue;
334293305Sjpaetzel		}
335293305Sjpaetzel		if (quot && *srcptr == quot) {
336293305Sjpaetzel			/* End of the quoted part */
337293305Sjpaetzel			quot = 0;
338293305Sjpaetzel			srcptr++;
339293305Sjpaetzel			continue;
340293305Sjpaetzel		}
341293305Sjpaetzel		if (!quot && strchr(delim, *srcptr))
342293305Sjpaetzel			break;
343293305Sjpaetzel		*dstptr++ = *srcptr++;
344293305Sjpaetzel	}
345293305Sjpaetzel
346293305Sjpaetzel	*dstptr = 0; /* Terminate the string */
347293305Sjpaetzel	*stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
348293305Sjpaetzel	return (retval);
349293305Sjpaetzel}
350293305Sjpaetzel
351293305Sjpaetzel/*
3521558Srgrimes * Mountd server for NFS mount protocol as described in:
3531558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
3541558Srgrimes * The optional arguments are the exports file name
3551558Srgrimes * default: _PATH_EXPORTS
3561558Srgrimes * and "-n" to allow nonroot mount.
3571558Srgrimes */
3581558Srgrimesint
359216587Scharniermain(int argc, char **argv)
3601558Srgrimes{
36175754Siedowse	fd_set readfds;
362172827Smatteo	struct netconfig *nconf;
363172827Smatteo	char *endptr, **hosts_bak;
364172827Smatteo	void *nc_handle;
365149433Spjd	pid_t otherpid;
366172827Smatteo	in_port_t svcport;
367172827Smatteo	int c, k, s;
368109363Smbr	int maxrec = RPC_MAXDATASIZE;
369222623Srmacklem	int attempt_cnt, port_len, port_pos, ret;
370222623Srmacklem	char **port_list;
3711558Srgrimes
37274462Salfred	/* Check that another mountd isn't already running. */
373150214Spjd	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
374149433Spjd	if (pfh == NULL) {
375149433Spjd		if (errno == EEXIST)
376149433Spjd			errx(1, "mountd already running, pid: %d.", otherpid);
377149433Spjd		warn("cannot open or create pidfile");
378149433Spjd	}
37974462Salfred
38074462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
38174462Salfred	if (s < 0)
38274462Salfred		have_v6 = 0;
38374462Salfred	else
38474462Salfred		close(s);
3852999Swollman
386282214Strasz	while ((c = getopt(argc, argv, "2deh:lnp:rS")) != -1)
3871558Srgrimes		switch (c) {
38825087Sdfr		case '2':
38925087Sdfr			force_v2 = 1;
39025087Sdfr			break;
391192993Srmacklem		case 'e':
392220980Srmacklem			/* now a no-op, since this is the default */
393192934Srmacklem			break;
3949336Sdfr		case 'n':
3959336Sdfr			resvport_only = 0;
3969336Sdfr			break;
3979336Sdfr		case 'r':
3989336Sdfr			dir_only = 0;
3999336Sdfr			break;
4008688Sphk		case 'd':
4018688Sphk			debug = debug ? 0 : 1;
4028688Sphk			break;
40331656Sguido		case 'l':
404121767Speter			dolog = 1;
40531656Sguido			break;
406126572Sbms		case 'p':
407126572Sbms			endptr = NULL;
408126572Sbms			svcport = (in_port_t)strtoul(optarg, &endptr, 10);
409126572Sbms			if (endptr == NULL || *endptr != '\0' ||
410126572Sbms			    svcport == 0 || svcport >= IPPORT_MAX)
411126572Sbms				usage();
412172827Smatteo			svcport_str = strdup(optarg);
413126572Sbms			break;
414172827Smatteo		case 'h':
415172827Smatteo			++nhosts;
416172827Smatteo			hosts_bak = hosts;
417172827Smatteo			hosts_bak = realloc(hosts, nhosts * sizeof(char *));
418172827Smatteo			if (hosts_bak == NULL) {
419172827Smatteo				if (hosts != NULL) {
420172827Smatteo					for (k = 0; k < nhosts; k++)
421172827Smatteo						free(hosts[k]);
422172827Smatteo					free(hosts);
423172827Smatteo					out_of_mem();
424172827Smatteo				}
425172827Smatteo			}
426172827Smatteo			hosts = hosts_bak;
427172827Smatteo			hosts[nhosts - 1] = strdup(optarg);
428172827Smatteo			if (hosts[nhosts - 1] == NULL) {
429172827Smatteo				for (k = 0; k < (nhosts - 1); k++)
430172827Smatteo					free(hosts[k]);
431172827Smatteo				free(hosts);
432172827Smatteo				out_of_mem();
433172827Smatteo			}
434172827Smatteo			break;
435241568Srmacklem		case 'S':
436241568Srmacklem			suspend_nfsd = 1;
437241568Srmacklem			break;
4381558Srgrimes		default:
43937663Scharnier			usage();
440298089Spfg		}
441192934Srmacklem
442282214Strasz	if (modfind("nfsd") < 0) {
443192934Srmacklem		/* Not present in kernel, try loading it */
444282214Strasz		if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
445192934Srmacklem			errx(1, "NFS server is not available");
446192934Srmacklem	}
447192934Srmacklem
4481558Srgrimes	argc -= optind;
4491558Srgrimes	argv += optind;
4501558Srgrimes	grphead = (struct grouplist *)NULL;
451166440Spjd	if (argc > 0)
452166440Spjd		exnames = argv;
453166440Spjd	else
454166440Spjd		exnames = exnames_default;
4551558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
4561558Srgrimes	if (debug)
45737663Scharnier		warnx("getting export list");
4581558Srgrimes	get_exportlist();
4591558Srgrimes	if (debug)
46037663Scharnier		warnx("getting mount list");
4611558Srgrimes	get_mountlist();
4621558Srgrimes	if (debug)
46337663Scharnier		warnx("here we go");
4641558Srgrimes	if (debug == 0) {
4651558Srgrimes		daemon(0, 0);
4661558Srgrimes		signal(SIGINT, SIG_IGN);
4671558Srgrimes		signal(SIGQUIT, SIG_IGN);
4681558Srgrimes	}
46975754Siedowse	signal(SIGHUP, huphandler);
47074462Salfred	signal(SIGTERM, terminate);
471164394Srodrigc	signal(SIGPIPE, SIG_IGN);
472149433Spjd
473149433Spjd	pidfile_write(pfh);
474149433Spjd
475194880Sdfr	rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
476194880Sdfr	rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
477109363Smbr	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
478109363Smbr
47924759Sguido	if (!resvport_only) {
480308449Srmacklem		if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
48183687Speter		    &resvport_only, sizeof(resvport_only)) != 0 &&
48283687Speter		    errno != ENOENT) {
48324759Sguido			syslog(LOG_ERR, "sysctl: %m");
48424759Sguido			exit(1);
48524759Sguido		}
48624330Sguido	}
487126572Sbms
488172827Smatteo	/*
489172827Smatteo	 * If no hosts were specified, add a wildcard entry to bind to
490172827Smatteo	 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
491172827Smatteo	 * list.
492172827Smatteo	 */
493172827Smatteo	if (nhosts == 0) {
494292864Suqs		hosts = malloc(sizeof(char *));
495172827Smatteo		if (hosts == NULL)
496172827Smatteo			out_of_mem();
497172827Smatteo		hosts[0] = "*";
498172827Smatteo		nhosts = 1;
499172827Smatteo	} else {
500172827Smatteo		hosts_bak = hosts;
501172827Smatteo		if (have_v6) {
502172827Smatteo			hosts_bak = realloc(hosts, (nhosts + 2) *
503172827Smatteo			    sizeof(char *));
504172827Smatteo			if (hosts_bak == NULL) {
505172827Smatteo				for (k = 0; k < nhosts; k++)
506172827Smatteo					free(hosts[k]);
507172827Smatteo		    		free(hosts);
508172827Smatteo		    		out_of_mem();
509172827Smatteo			} else
510172827Smatteo				hosts = hosts_bak;
511172827Smatteo			nhosts += 2;
512172827Smatteo			hosts[nhosts - 2] = "::1";
513172827Smatteo		} else {
514172827Smatteo			hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
515172827Smatteo			if (hosts_bak == NULL) {
516172827Smatteo				for (k = 0; k < nhosts; k++)
517172827Smatteo					free(hosts[k]);
518172827Smatteo				free(hosts);
519172827Smatteo				out_of_mem();
520172827Smatteo			} else {
521172827Smatteo				nhosts += 1;
522172827Smatteo				hosts = hosts_bak;
523126572Sbms			}
524172827Smatteo		}
52574462Salfred
526172827Smatteo		hosts[nhosts - 1] = "127.0.0.1";
52774462Salfred	}
52874462Salfred
529222623Srmacklem	attempt_cnt = 1;
530222623Srmacklem	sock_fdcnt = 0;
531222623Srmacklem	sock_fd = NULL;
532222623Srmacklem	port_list = NULL;
533222623Srmacklem	port_len = 0;
534172827Smatteo	nc_handle = setnetconfig();
535172827Smatteo	while ((nconf = getnetconfig(nc_handle))) {
536172827Smatteo		if (nconf->nc_flag & NC_VISIBLE) {
537172827Smatteo			if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
538172827Smatteo			    "inet6") == 0) {
539172827Smatteo				/* DO NOTHING */
540222623Srmacklem			} else {
541222623Srmacklem				ret = create_service(nconf);
542222623Srmacklem				if (ret == 1)
543222623Srmacklem					/* Ignore this call */
544222623Srmacklem					continue;
545222623Srmacklem				if (ret < 0) {
546222623Srmacklem					/*
547222623Srmacklem					 * Failed to bind port, so close off
548222623Srmacklem					 * all sockets created and try again
549222623Srmacklem					 * if the port# was dynamically
550222623Srmacklem					 * assigned via bind(2).
551222623Srmacklem					 */
552222623Srmacklem					clearout_service();
553222623Srmacklem					if (mallocd_svcport != 0 &&
554222623Srmacklem					    attempt_cnt < GETPORT_MAXTRY) {
555222623Srmacklem						free(svcport_str);
556222623Srmacklem						svcport_str = NULL;
557222623Srmacklem						mallocd_svcport = 0;
558222623Srmacklem					} else {
559222623Srmacklem						errno = EADDRINUSE;
560222623Srmacklem						syslog(LOG_ERR,
561222623Srmacklem						    "bindresvport_sa: %m");
562222623Srmacklem						exit(1);
563222623Srmacklem					}
564222623Srmacklem
565222623Srmacklem					/* Start over at the first service. */
566222623Srmacklem					free(sock_fd);
567222623Srmacklem					sock_fdcnt = 0;
568222623Srmacklem					sock_fd = NULL;
569222623Srmacklem					nc_handle = setnetconfig();
570222623Srmacklem					attempt_cnt++;
571222623Srmacklem				} else if (mallocd_svcport != 0 &&
572222623Srmacklem				    attempt_cnt == GETPORT_MAXTRY) {
573222623Srmacklem					/*
574222623Srmacklem					 * For the last attempt, allow
575222623Srmacklem					 * different port #s for each nconf
576222623Srmacklem					 * by saving the svcport_str and
577222623Srmacklem					 * setting it back to NULL.
578222623Srmacklem					 */
579222623Srmacklem					port_list = realloc(port_list,
580222623Srmacklem					    (port_len + 1) * sizeof(char *));
581222623Srmacklem					if (port_list == NULL)
582222623Srmacklem						out_of_mem();
583222623Srmacklem					port_list[port_len++] = svcport_str;
584222623Srmacklem					svcport_str = NULL;
585222623Srmacklem					mallocd_svcport = 0;
586222623Srmacklem				}
587222623Srmacklem			}
588222623Srmacklem		}
589222623Srmacklem	}
590222623Srmacklem
591222623Srmacklem	/*
592222623Srmacklem	 * Successfully bound the ports, so call complete_service() to
593222623Srmacklem	 * do the rest of the setup on the service(s).
594222623Srmacklem	 */
595222623Srmacklem	sock_fdpos = 0;
596222623Srmacklem	port_pos = 0;
597222623Srmacklem	nc_handle = setnetconfig();
598222623Srmacklem	while ((nconf = getnetconfig(nc_handle))) {
599222623Srmacklem		if (nconf->nc_flag & NC_VISIBLE) {
600222623Srmacklem			if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
601222623Srmacklem			    "inet6") == 0) {
602222623Srmacklem				/* DO NOTHING */
603222623Srmacklem			} else if (port_list != NULL) {
604222623Srmacklem				if (port_pos >= port_len) {
605222623Srmacklem					syslog(LOG_ERR, "too many port#s");
606222623Srmacklem					exit(1);
607222623Srmacklem				}
608222623Srmacklem				complete_service(nconf, port_list[port_pos++]);
609172827Smatteo			} else
610222623Srmacklem				complete_service(nconf, svcport_str);
611172827Smatteo		}
61274462Salfred	}
613172827Smatteo	endnetconfig(nc_handle);
614222623Srmacklem	free(sock_fd);
615222623Srmacklem	if (port_list != NULL) {
616222623Srmacklem		for (port_pos = 0; port_pos < port_len; port_pos++)
617222623Srmacklem			free(port_list[port_pos]);
618222623Srmacklem		free(port_list);
619222623Srmacklem	}
62074462Salfred
62174462Salfred	if (xcreated == 0) {
62274462Salfred		syslog(LOG_ERR, "could not create any services");
6231558Srgrimes		exit(1);
6241558Srgrimes	}
62575754Siedowse
62675754Siedowse	/* Expand svc_run() here so that we can call get_exportlist(). */
62775754Siedowse	for (;;) {
62875754Siedowse		if (got_sighup) {
62975754Siedowse			get_exportlist();
63075754Siedowse			got_sighup = 0;
63175754Siedowse		}
63275754Siedowse		readfds = svc_fdset;
63375754Siedowse		switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
63475754Siedowse		case -1:
63575754Siedowse			if (errno == EINTR)
63675754Siedowse                                continue;
63775754Siedowse			syslog(LOG_ERR, "mountd died: select: %m");
63875754Siedowse			exit(1);
63975754Siedowse		case 0:
64075754Siedowse			continue;
64175754Siedowse		default:
64275754Siedowse			svc_getreqset(&readfds);
64375754Siedowse		}
64475754Siedowse	}
645172827Smatteo}
646172827Smatteo
647172827Smatteo/*
648172827Smatteo * This routine creates and binds sockets on the appropriate
649222623Srmacklem * addresses. It gets called one time for each transport.
650222623Srmacklem * It returns 0 upon success, 1 for ingore the call and -1 to indicate
651222623Srmacklem * bind failed with EADDRINUSE.
652222623Srmacklem * Any file descriptors that have been created are stored in sock_fd and
653222623Srmacklem * the total count of them is maintained in sock_fdcnt.
654172827Smatteo */
655222623Srmacklemstatic int
656172827Smatteocreate_service(struct netconfig *nconf)
657172827Smatteo{
658172827Smatteo	struct addrinfo hints, *res = NULL;
659172827Smatteo	struct sockaddr_in *sin;
660172827Smatteo	struct sockaddr_in6 *sin6;
661172827Smatteo	struct __rpc_sockinfo si;
662172827Smatteo	int aicode;
663172827Smatteo	int fd;
664172827Smatteo	int nhostsbak;
665172827Smatteo	int one = 1;
666172827Smatteo	int r;
667172827Smatteo	u_int32_t host_addr[4];  /* IPv4 or IPv6 */
668222623Srmacklem	int mallocd_res;
669172827Smatteo
670172827Smatteo	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
671172827Smatteo	    (nconf->nc_semantics != NC_TPI_COTS) &&
672172827Smatteo	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
673222623Srmacklem		return (1);	/* not my type */
674172827Smatteo
675172827Smatteo	/*
676172827Smatteo	 * XXX - using RPC library internal functions.
677172827Smatteo	 */
678172827Smatteo	if (!__rpc_nconf2sockinfo(nconf, &si)) {
679172827Smatteo		syslog(LOG_ERR, "cannot get information for %s",
680172827Smatteo		    nconf->nc_netid);
681222623Srmacklem		return (1);
682172827Smatteo	}
683172827Smatteo
684172827Smatteo	/* Get mountd's address on this transport */
685172827Smatteo	memset(&hints, 0, sizeof hints);
686172827Smatteo	hints.ai_family = si.si_af;
687172827Smatteo	hints.ai_socktype = si.si_socktype;
688172827Smatteo	hints.ai_protocol = si.si_proto;
689172827Smatteo
690172827Smatteo	/*
691172827Smatteo	 * Bind to specific IPs if asked to
692172827Smatteo	 */
693172827Smatteo	nhostsbak = nhosts;
694172827Smatteo	while (nhostsbak > 0) {
695172827Smatteo		--nhostsbak;
696222623Srmacklem		sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
697222623Srmacklem		if (sock_fd == NULL)
698222623Srmacklem			out_of_mem();
699222623Srmacklem		sock_fd[sock_fdcnt++] = -1;	/* Set invalid for now. */
700222623Srmacklem		mallocd_res = 0;
701222623Srmacklem
702277352Srstone		hints.ai_flags = AI_PASSIVE;
703277352Srstone
704172827Smatteo		/*
705172827Smatteo		 * XXX - using RPC library internal functions.
706172827Smatteo		 */
707172827Smatteo		if ((fd = __rpc_nconf2fd(nconf)) < 0) {
708172827Smatteo			int non_fatal = 0;
709244538Skevlo	    		if (errno == EAFNOSUPPORT &&
710172827Smatteo			    nconf->nc_semantics != NC_TPI_CLTS)
711172827Smatteo				non_fatal = 1;
712172827Smatteo
713172827Smatteo			syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
714172827Smatteo			    "cannot create socket for %s", nconf->nc_netid);
715222623Srmacklem			if (non_fatal != 0)
716222623Srmacklem				continue;
717222623Srmacklem			exit(1);
718172827Smatteo		}
719172827Smatteo
720172827Smatteo		switch (hints.ai_family) {
721172827Smatteo		case AF_INET:
722172827Smatteo			if (inet_pton(AF_INET, hosts[nhostsbak],
723172827Smatteo			    host_addr) == 1) {
724222623Srmacklem				hints.ai_flags |= AI_NUMERICHOST;
725172827Smatteo			} else {
726172827Smatteo				/*
727172827Smatteo				 * Skip if we have an AF_INET6 address.
728172827Smatteo				 */
729172827Smatteo				if (inet_pton(AF_INET6, hosts[nhostsbak],
730172827Smatteo				    host_addr) == 1) {
731172827Smatteo					close(fd);
732172827Smatteo					continue;
733172827Smatteo				}
734172827Smatteo			}
735172827Smatteo			break;
736172827Smatteo		case AF_INET6:
737172827Smatteo			if (inet_pton(AF_INET6, hosts[nhostsbak],
738172827Smatteo			    host_addr) == 1) {
739222623Srmacklem				hints.ai_flags |= AI_NUMERICHOST;
740172827Smatteo			} else {
741172827Smatteo				/*
742172827Smatteo				 * Skip if we have an AF_INET address.
743172827Smatteo				 */
744172827Smatteo				if (inet_pton(AF_INET, hosts[nhostsbak],
745172827Smatteo				    host_addr) == 1) {
746172827Smatteo					close(fd);
747172827Smatteo					continue;
748172827Smatteo				}
749172827Smatteo			}
750172827Smatteo
751172827Smatteo			/*
752172827Smatteo			 * We're doing host-based access checks here, so don't
753172827Smatteo			 * allow v4-in-v6 to confuse things. The kernel will
754172827Smatteo			 * disable it by default on NFS sockets too.
755172827Smatteo			 */
756172827Smatteo			if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
757172827Smatteo			    sizeof one) < 0) {
758172827Smatteo				syslog(LOG_ERR,
759172827Smatteo				    "can't disable v4-in-v6 on IPv6 socket");
760172827Smatteo				exit(1);
761172827Smatteo			}
762172827Smatteo			break;
763172827Smatteo		default:
764172827Smatteo			break;
765172827Smatteo		}
766172827Smatteo
767172827Smatteo		/*
768172827Smatteo		 * If no hosts were specified, just bind to INADDR_ANY
769172827Smatteo		 */
770172827Smatteo		if (strcmp("*", hosts[nhostsbak]) == 0) {
771172827Smatteo			if (svcport_str == NULL) {
772172827Smatteo				res = malloc(sizeof(struct addrinfo));
773172827Smatteo				if (res == NULL)
774172827Smatteo					out_of_mem();
775222623Srmacklem				mallocd_res = 1;
776172827Smatteo				res->ai_flags = hints.ai_flags;
777172827Smatteo				res->ai_family = hints.ai_family;
778172827Smatteo				res->ai_protocol = hints.ai_protocol;
779172827Smatteo				switch (res->ai_family) {
780172827Smatteo				case AF_INET:
781172827Smatteo					sin = malloc(sizeof(struct sockaddr_in));
782172827Smatteo					if (sin == NULL)
783172827Smatteo						out_of_mem();
784172827Smatteo					sin->sin_family = AF_INET;
785172827Smatteo					sin->sin_port = htons(0);
786172827Smatteo					sin->sin_addr.s_addr = htonl(INADDR_ANY);
787172827Smatteo					res->ai_addr = (struct sockaddr*) sin;
788172827Smatteo					res->ai_addrlen = (socklen_t)
789222623Srmacklem					    sizeof(struct sockaddr_in);
790172827Smatteo					break;
791172827Smatteo				case AF_INET6:
792172827Smatteo					sin6 = malloc(sizeof(struct sockaddr_in6));
793173056Ssimon					if (sin6 == NULL)
794172827Smatteo						out_of_mem();
795172827Smatteo					sin6->sin6_family = AF_INET6;
796172827Smatteo					sin6->sin6_port = htons(0);
797172827Smatteo					sin6->sin6_addr = in6addr_any;
798172827Smatteo					res->ai_addr = (struct sockaddr*) sin6;
799172827Smatteo					res->ai_addrlen = (socklen_t)
800222623Srmacklem					    sizeof(struct sockaddr_in6);
801222623Srmacklem					break;
802172827Smatteo				default:
803222623Srmacklem					syslog(LOG_ERR, "bad addr fam %d",
804222623Srmacklem					    res->ai_family);
805222623Srmacklem					exit(1);
806172827Smatteo				}
807172827Smatteo			} else {
808172827Smatteo				if ((aicode = getaddrinfo(NULL, svcport_str,
809172827Smatteo				    &hints, &res)) != 0) {
810172827Smatteo					syslog(LOG_ERR,
811172827Smatteo					    "cannot get local address for %s: %s",
812172827Smatteo					    nconf->nc_netid,
813172827Smatteo					    gai_strerror(aicode));
814222623Srmacklem					close(fd);
815172827Smatteo					continue;
816172827Smatteo				}
817172827Smatteo			}
818172827Smatteo		} else {
819172827Smatteo			if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
820172827Smatteo			    &hints, &res)) != 0) {
821172827Smatteo				syslog(LOG_ERR,
822172827Smatteo				    "cannot get local address for %s: %s",
823172827Smatteo				    nconf->nc_netid, gai_strerror(aicode));
824222623Srmacklem				close(fd);
825172827Smatteo				continue;
826172827Smatteo			}
827172827Smatteo		}
828172827Smatteo
829222623Srmacklem		/* Store the fd. */
830222623Srmacklem		sock_fd[sock_fdcnt - 1] = fd;
831222623Srmacklem
832222623Srmacklem		/* Now, attempt the bind. */
833172827Smatteo		r = bindresvport_sa(fd, res->ai_addr);
834172827Smatteo		if (r != 0) {
835222623Srmacklem			if (errno == EADDRINUSE && mallocd_svcport != 0) {
836222623Srmacklem				if (mallocd_res != 0) {
837222623Srmacklem					free(res->ai_addr);
838222623Srmacklem					free(res);
839222623Srmacklem				} else
840222623Srmacklem					freeaddrinfo(res);
841222623Srmacklem				return (-1);
842222623Srmacklem			}
843172827Smatteo			syslog(LOG_ERR, "bindresvport_sa: %m");
844172827Smatteo			exit(1);
845172827Smatteo		}
846172827Smatteo
847222623Srmacklem		if (svcport_str == NULL) {
848222623Srmacklem			svcport_str = malloc(NI_MAXSERV * sizeof(char));
849222623Srmacklem			if (svcport_str == NULL)
850222623Srmacklem				out_of_mem();
851222623Srmacklem			mallocd_svcport = 1;
852222623Srmacklem
853222623Srmacklem			if (getnameinfo(res->ai_addr,
854222623Srmacklem			    res->ai_addr->sa_len, NULL, NI_MAXHOST,
855222623Srmacklem			    svcport_str, NI_MAXSERV * sizeof(char),
856222623Srmacklem			    NI_NUMERICHOST | NI_NUMERICSERV))
857222623Srmacklem				errx(1, "Cannot get port number");
858222623Srmacklem		}
859222623Srmacklem		if (mallocd_res != 0) {
860222623Srmacklem			free(res->ai_addr);
861222623Srmacklem			free(res);
862222623Srmacklem		} else
863222623Srmacklem			freeaddrinfo(res);
864222623Srmacklem		res = NULL;
865222623Srmacklem	}
866222623Srmacklem	return (0);
867222623Srmacklem}
868222623Srmacklem
869222623Srmacklem/*
870222623Srmacklem * Called after all the create_service() calls have succeeded, to complete
871222623Srmacklem * the setup and registration.
872222623Srmacklem */
873222623Srmacklemstatic void
874222623Srmacklemcomplete_service(struct netconfig *nconf, char *port_str)
875222623Srmacklem{
876222623Srmacklem	struct addrinfo hints, *res = NULL;
877222623Srmacklem	struct __rpc_sockinfo si;
878222623Srmacklem	struct netbuf servaddr;
879222623Srmacklem	SVCXPRT	*transp = NULL;
880222623Srmacklem	int aicode, fd, nhostsbak;
881222623Srmacklem	int registered = 0;
882222623Srmacklem
883222623Srmacklem	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
884222623Srmacklem	    (nconf->nc_semantics != NC_TPI_COTS) &&
885222623Srmacklem	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
886222623Srmacklem		return;	/* not my type */
887222623Srmacklem
888222623Srmacklem	/*
889222623Srmacklem	 * XXX - using RPC library internal functions.
890222623Srmacklem	 */
891222623Srmacklem	if (!__rpc_nconf2sockinfo(nconf, &si)) {
892222623Srmacklem		syslog(LOG_ERR, "cannot get information for %s",
893222623Srmacklem		    nconf->nc_netid);
894222623Srmacklem		return;
895222623Srmacklem	}
896222623Srmacklem
897222623Srmacklem	nhostsbak = nhosts;
898222623Srmacklem	while (nhostsbak > 0) {
899222623Srmacklem		--nhostsbak;
900222623Srmacklem		if (sock_fdpos >= sock_fdcnt) {
901222623Srmacklem			/* Should never happen. */
902222623Srmacklem			syslog(LOG_ERR, "Ran out of socket fd's");
903222623Srmacklem			return;
904222623Srmacklem		}
905222623Srmacklem		fd = sock_fd[sock_fdpos++];
906222623Srmacklem		if (fd < 0)
907222623Srmacklem			continue;
908222623Srmacklem
909172827Smatteo		if (nconf->nc_semantics != NC_TPI_CLTS)
910172827Smatteo			listen(fd, SOMAXCONN);
911172827Smatteo
912172827Smatteo		if (nconf->nc_semantics == NC_TPI_CLTS )
913172827Smatteo			transp = svc_dg_create(fd, 0, 0);
914172827Smatteo		else
915172827Smatteo			transp = svc_vc_create(fd, RPC_MAXDATASIZE,
916172827Smatteo			    RPC_MAXDATASIZE);
917172827Smatteo
918172827Smatteo		if (transp != (SVCXPRT *) NULL) {
919194880Sdfr			if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
920172827Smatteo			    NULL))
921172827Smatteo				syslog(LOG_ERR,
922194880Sdfr				    "can't register %s MOUNTVERS service",
923172827Smatteo				    nconf->nc_netid);
924172827Smatteo			if (!force_v2) {
925194880Sdfr				if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
926172827Smatteo				    mntsrv, NULL))
927172827Smatteo					syslog(LOG_ERR,
928194880Sdfr					    "can't register %s MOUNTVERS3 service",
929172827Smatteo					    nconf->nc_netid);
930172827Smatteo			}
931172827Smatteo		} else
932172827Smatteo			syslog(LOG_WARNING, "can't create %s services",
933172827Smatteo			    nconf->nc_netid);
934172827Smatteo
935172827Smatteo		if (registered == 0) {
936172827Smatteo			registered = 1;
937172827Smatteo			memset(&hints, 0, sizeof hints);
938172827Smatteo			hints.ai_flags = AI_PASSIVE;
939172827Smatteo			hints.ai_family = si.si_af;
940172827Smatteo			hints.ai_socktype = si.si_socktype;
941172827Smatteo			hints.ai_protocol = si.si_proto;
942172827Smatteo
943222623Srmacklem			if ((aicode = getaddrinfo(NULL, port_str, &hints,
944172827Smatteo			    &res)) != 0) {
945172827Smatteo				syslog(LOG_ERR, "cannot get local address: %s",
946172827Smatteo				    gai_strerror(aicode));
947172827Smatteo				exit(1);
948172827Smatteo			}
949172827Smatteo
950172827Smatteo			servaddr.buf = malloc(res->ai_addrlen);
951172827Smatteo			memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
952172827Smatteo			servaddr.len = res->ai_addrlen;
953172827Smatteo
954194880Sdfr			rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
955194880Sdfr			rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
956172827Smatteo
957172827Smatteo			xcreated++;
958172827Smatteo			freeaddrinfo(res);
959172827Smatteo		}
960172827Smatteo	} /* end while */
9611558Srgrimes}
9621558Srgrimes
963222623Srmacklem/*
964222623Srmacklem * Clear out sockets after a failure to bind one of them, so that the
965222623Srmacklem * cycle of socket creation/binding can start anew.
966222623Srmacklem */
96737663Scharnierstatic void
968222623Srmacklemclearout_service(void)
969222623Srmacklem{
970222623Srmacklem	int i;
971222623Srmacklem
972222623Srmacklem	for (i = 0; i < sock_fdcnt; i++) {
973222623Srmacklem		if (sock_fd[i] >= 0) {
974222623Srmacklem			shutdown(sock_fd[i], SHUT_RDWR);
975222623Srmacklem			close(sock_fd[i]);
976222623Srmacklem		}
977222623Srmacklem	}
978222623Srmacklem}
979222623Srmacklem
980222623Srmacklemstatic void
981216587Scharnierusage(void)
98237663Scharnier{
98337663Scharnier	fprintf(stderr,
984192993Srmacklem		"usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
985241568Srmacklem		"[-S] [-h <bindip>] [export_file ...]\n");
98637663Scharnier	exit(1);
98737663Scharnier}
98837663Scharnier
9891558Srgrimes/*
9901558Srgrimes * The mount rpc service
9911558Srgrimes */
9921558Srgrimesvoid
993216587Scharniermntsrv(struct svc_req *rqstp, SVCXPRT *transp)
9941558Srgrimes{
9951558Srgrimes	struct exportlist *ep;
9961558Srgrimes	struct dirlist *dp;
9979336Sdfr	struct fhreturn fhr;
9981558Srgrimes	struct stat stb;
9991558Srgrimes	struct statfs fsb;
100074462Salfred	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
100174462Salfred	int lookup_failed = 1;
100274462Salfred	struct sockaddr *saddr;
10039336Sdfr	u_short sport;
1004194880Sdfr	char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
100528911Sguido	int bad = 0, defset, hostset;
10069336Sdfr	sigset_t sighup_mask;
1007240902Srmacklem	int numsecflavors, *secflavorsp;
10081558Srgrimes
10099336Sdfr	sigemptyset(&sighup_mask);
10109336Sdfr	sigaddset(&sighup_mask, SIGHUP);
101174462Salfred	saddr = svc_getrpccaller(transp)->buf;
101274462Salfred	switch (saddr->sa_family) {
101374462Salfred	case AF_INET6:
101475635Siedowse		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
101574462Salfred		break;
101674462Salfred	case AF_INET:
101775635Siedowse		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
101874462Salfred		break;
101974462Salfred	default:
102074462Salfred		syslog(LOG_ERR, "request from unknown address family");
102174462Salfred		return;
102274462Salfred	}
102374462Salfred	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
102474462Salfred	    NULL, 0, 0);
102574462Salfred	getnameinfo(saddr, saddr->sa_len, numerichost,
102674462Salfred	    sizeof numerichost, NULL, 0, NI_NUMERICHOST);
10271558Srgrimes	switch (rqstp->rq_proc) {
10281558Srgrimes	case NULLPROC:
1029121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
103037663Scharnier			syslog(LOG_ERR, "can't send reply");
10311558Srgrimes		return;
1032194880Sdfr	case MOUNTPROC_MNT:
10339336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
103431656Sguido			syslog(LOG_NOTICE,
103531656Sguido			    "mount request from %s from unprivileged port",
103674462Salfred			    numerichost);
10371558Srgrimes			svcerr_weakauth(transp);
10381558Srgrimes			return;
10391558Srgrimes		}
1040121556Speter		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
104131656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
104274462Salfred			    numerichost);
10431558Srgrimes			svcerr_decode(transp);
10441558Srgrimes			return;
10451558Srgrimes		}
10461558Srgrimes
10471558Srgrimes		/*
10481558Srgrimes		 * Get the real pathname and make sure it is a directory
10499336Sdfr		 * or a regular file if the -r option was specified
10509336Sdfr		 * and it exists.
10511558Srgrimes		 */
105251968Salfred		if (realpath(rpcpath, dirpath) == NULL ||
10531558Srgrimes		    stat(dirpath, &stb) < 0 ||
10549336Sdfr		    (!S_ISDIR(stb.st_mode) &&
105574462Salfred		    (dir_only || !S_ISREG(stb.st_mode))) ||
10561558Srgrimes		    statfs(dirpath, &fsb) < 0) {
10571558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
105831656Sguido			syslog(LOG_NOTICE,
105937663Scharnier			    "mount request from %s for non existent path %s",
106074462Salfred			    numerichost, dirpath);
10611558Srgrimes			if (debug)
106237663Scharnier				warnx("stat failed on %s", dirpath);
106328911Sguido			bad = ENOENT;	/* We will send error reply later */
10641558Srgrimes		}
10651558Srgrimes
10661558Srgrimes		/* Check in the exports list */
10679336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
10681558Srgrimes		ep = ex_search(&fsb.f_fsid);
10699336Sdfr		hostset = defset = 0;
1070240902Srmacklem		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1071240902Srmacklem		    &numsecflavors, &secflavorsp) ||
10721558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1073240902Srmacklem		      chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1074240902Srmacklem		       &secflavorsp)) ||
107574462Salfred		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
107674462Salfred		     scan_tree(ep->ex_dirl, saddr) == 0))) {
107728911Sguido			if (bad) {
1078121556Speter				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
107928911Sguido				    (caddr_t)&bad))
108037663Scharnier					syslog(LOG_ERR, "can't send reply");
108128911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
108228911Sguido				return;
108328911Sguido			}
1084240902Srmacklem			if (hostset & DP_HOSTSET) {
10859336Sdfr				fhr.fhr_flag = hostset;
1086240902Srmacklem				fhr.fhr_numsecflavors = numsecflavors;
1087240902Srmacklem				fhr.fhr_secflavors = secflavorsp;
1088240902Srmacklem			} else {
10899336Sdfr				fhr.fhr_flag = defset;
1090240902Srmacklem				fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1091240902Srmacklem				fhr.fhr_secflavors = ep->ex_defsecflavors;
1092240902Srmacklem			}
10939336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
10941558Srgrimes			/* Get the file handle */
109523681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
10969336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
10971558Srgrimes				bad = errno;
109837663Scharnier				syslog(LOG_ERR, "can't get fh for %s", dirpath);
1099121556Speter				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
11001558Srgrimes				    (caddr_t)&bad))
110137663Scharnier					syslog(LOG_ERR, "can't send reply");
11029336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
11031558Srgrimes				return;
11041558Srgrimes			}
1105121556Speter			if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1106121556Speter			    (caddr_t)&fhr))
110737663Scharnier				syslog(LOG_ERR, "can't send reply");
110874462Salfred			if (!lookup_failed)
110974462Salfred				add_mlist(host, dirpath);
11101558Srgrimes			else
111174462Salfred				add_mlist(numerichost, dirpath);
11121558Srgrimes			if (debug)
111337663Scharnier				warnx("mount successful");
1114121767Speter			if (dolog)
111531656Sguido				syslog(LOG_NOTICE,
111631656Sguido				    "mount request succeeded from %s for %s",
111774462Salfred				    numerichost, dirpath);
111831656Sguido		} else {
11191558Srgrimes			bad = EACCES;
112031656Sguido			syslog(LOG_NOTICE,
112131656Sguido			    "mount request denied from %s for %s",
112274462Salfred			    numerichost, dirpath);
112331656Sguido		}
112428911Sguido
1125121556Speter		if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1126121556Speter		    (caddr_t)&bad))
112737663Scharnier			syslog(LOG_ERR, "can't send reply");
11289336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
11291558Srgrimes		return;
1130194880Sdfr	case MOUNTPROC_DUMP:
1131121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
113237663Scharnier			syslog(LOG_ERR, "can't send reply");
1133121767Speter		else if (dolog)
113431656Sguido			syslog(LOG_NOTICE,
113531656Sguido			    "dump request succeeded from %s",
113674462Salfred			    numerichost);
11371558Srgrimes		return;
1138194880Sdfr	case MOUNTPROC_UMNT:
11399336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
114031656Sguido			syslog(LOG_NOTICE,
114131656Sguido			    "umount request from %s from unprivileged port",
114274462Salfred			    numerichost);
11431558Srgrimes			svcerr_weakauth(transp);
11441558Srgrimes			return;
11451558Srgrimes		}
1146121556Speter		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
114731656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
114874462Salfred			    numerichost);
11491558Srgrimes			svcerr_decode(transp);
11501558Srgrimes			return;
11511558Srgrimes		}
115251968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
115351968Salfred			syslog(LOG_NOTICE, "umount request from %s "
115451968Salfred			    "for non existent path %s",
115574462Salfred			    numerichost, dirpath);
115651968Salfred		}
1157121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
115837663Scharnier			syslog(LOG_ERR, "can't send reply");
115974462Salfred		if (!lookup_failed)
116075635Siedowse			del_mlist(host, dirpath);
116175635Siedowse		del_mlist(numerichost, dirpath);
1162121767Speter		if (dolog)
116331656Sguido			syslog(LOG_NOTICE,
116431656Sguido			    "umount request succeeded from %s for %s",
116574462Salfred			    numerichost, dirpath);
11661558Srgrimes		return;
1167194880Sdfr	case MOUNTPROC_UMNTALL:
11689336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
116931656Sguido			syslog(LOG_NOTICE,
117031656Sguido			    "umountall request from %s from unprivileged port",
117174462Salfred			    numerichost);
11721558Srgrimes			svcerr_weakauth(transp);
11731558Srgrimes			return;
11741558Srgrimes		}
1175121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
117637663Scharnier			syslog(LOG_ERR, "can't send reply");
117774462Salfred		if (!lookup_failed)
117875635Siedowse			del_mlist(host, NULL);
117975635Siedowse		del_mlist(numerichost, NULL);
1180121767Speter		if (dolog)
118131656Sguido			syslog(LOG_NOTICE,
118231656Sguido			    "umountall request succeeded from %s",
118374462Salfred			    numerichost);
11841558Srgrimes		return;
1185194880Sdfr	case MOUNTPROC_EXPORT:
1186121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1187121556Speter			if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1188121556Speter			    (caddr_t)NULL))
1189100117Salfred				syslog(LOG_ERR, "can't send reply");
1190121767Speter		if (dolog)
119131656Sguido			syslog(LOG_NOTICE,
119231656Sguido			    "export request succeeded from %s",
119374462Salfred			    numerichost);
11941558Srgrimes		return;
11951558Srgrimes	default:
11961558Srgrimes		svcerr_noproc(transp);
11971558Srgrimes		return;
11981558Srgrimes	}
11991558Srgrimes}
12001558Srgrimes
12011558Srgrimes/*
12021558Srgrimes * Xdr conversion for a dirpath string
12031558Srgrimes */
1204285128Straszstatic int
1205216587Scharnierxdr_dir(XDR *xdrsp, char *dirp)
12061558Srgrimes{
1207194880Sdfr	return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
12081558Srgrimes}
12091558Srgrimes
12101558Srgrimes/*
12119336Sdfr * Xdr routine to generate file handle reply
12121558Srgrimes */
1213285128Straszstatic int
1214216587Scharnierxdr_fhs(XDR *xdrsp, caddr_t cp)
12151558Srgrimes{
121692806Sobrien	struct fhreturn *fhrp = (struct fhreturn *)cp;
12179336Sdfr	u_long ok = 0, len, auth;
1218184588Sdfr	int i;
12191558Srgrimes
12201558Srgrimes	if (!xdr_long(xdrsp, &ok))
12211558Srgrimes		return (0);
12229336Sdfr	switch (fhrp->fhr_vers) {
12239336Sdfr	case 1:
12249336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
12259336Sdfr	case 3:
12269336Sdfr		len = NFSX_V3FH;
12279336Sdfr		if (!xdr_long(xdrsp, &len))
12289336Sdfr			return (0);
12299336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
12309336Sdfr			return (0);
1231184588Sdfr		if (fhrp->fhr_numsecflavors) {
1232184588Sdfr			if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1233184588Sdfr				return (0);
1234184588Sdfr			for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1235184588Sdfr				if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1236184588Sdfr					return (0);
1237184588Sdfr			return (1);
1238184588Sdfr		} else {
1239184588Sdfr			auth = AUTH_SYS;
1240184588Sdfr			len = 1;
1241184588Sdfr			if (!xdr_long(xdrsp, &len))
1242184588Sdfr				return (0);
1243184588Sdfr			return (xdr_long(xdrsp, &auth));
1244184588Sdfr		}
1245298089Spfg	}
12469336Sdfr	return (0);
12471558Srgrimes}
12481558Srgrimes
1249285128Straszstatic int
1250216587Scharnierxdr_mlist(XDR *xdrsp, caddr_t cp __unused)
12511558Srgrimes{
12521558Srgrimes	struct mountlist *mlp;
12531558Srgrimes	int true = 1;
12541558Srgrimes	int false = 0;
12551558Srgrimes	char *strp;
12561558Srgrimes
1257324955Smanu	SLIST_FOREACH(mlp, &mlhead, next) {
12581558Srgrimes		if (!xdr_bool(xdrsp, &true))
12591558Srgrimes			return (0);
12601558Srgrimes		strp = &mlp->ml_host[0];
1261194880Sdfr		if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
12621558Srgrimes			return (0);
12631558Srgrimes		strp = &mlp->ml_dirp[0];
1264194880Sdfr		if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
12651558Srgrimes			return (0);
12661558Srgrimes	}
12671558Srgrimes	if (!xdr_bool(xdrsp, &false))
12681558Srgrimes		return (0);
12691558Srgrimes	return (1);
12701558Srgrimes}
12711558Srgrimes
12721558Srgrimes/*
12731558Srgrimes * Xdr conversion for export list
12741558Srgrimes */
1275285128Straszstatic int
1276216587Scharnierxdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
12771558Srgrimes{
12781558Srgrimes	struct exportlist *ep;
12791558Srgrimes	int false = 0;
12809336Sdfr	int putdef;
12819336Sdfr	sigset_t sighup_mask;
12821558Srgrimes
12839336Sdfr	sigemptyset(&sighup_mask);
12849336Sdfr	sigaddset(&sighup_mask, SIGHUP);
12859336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1286324955Smanu
1287324955Smanu	SLIST_FOREACH(ep, &exphead, entries) {
12881558Srgrimes		putdef = 0;
1289100117Salfred		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1290100117Salfred			       &putdef, brief))
12911558Srgrimes			goto errout;
12921558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
12931558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1294100117Salfred			&putdef, brief))
12951558Srgrimes			goto errout;
12961558Srgrimes	}
12979336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
12981558Srgrimes	if (!xdr_bool(xdrsp, &false))
12991558Srgrimes		return (0);
13001558Srgrimes	return (1);
13011558Srgrimeserrout:
13029336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
13031558Srgrimes	return (0);
13041558Srgrimes}
13051558Srgrimes
13061558Srgrimes/*
13071558Srgrimes * Called from xdr_explist() to traverse the tree and export the
13081558Srgrimes * directory paths.
13091558Srgrimes */
1310285128Straszstatic int
1311216587Scharnierput_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1312216587Scharnier	int brief)
13131558Srgrimes{
13141558Srgrimes	struct grouplist *grp;
13151558Srgrimes	struct hostlist *hp;
13161558Srgrimes	int true = 1;
13171558Srgrimes	int false = 0;
13181558Srgrimes	int gotalldir = 0;
13191558Srgrimes	char *strp;
13201558Srgrimes
13211558Srgrimes	if (dp) {
1322100117Salfred		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
13231558Srgrimes			return (1);
13241558Srgrimes		if (!xdr_bool(xdrsp, &true))
13251558Srgrimes			return (1);
13261558Srgrimes		strp = dp->dp_dirp;
1327194880Sdfr		if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
13281558Srgrimes			return (1);
13291558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
13301558Srgrimes			gotalldir = 1;
13311558Srgrimes			*putdefp = 1;
13321558Srgrimes		}
1333100117Salfred		if (brief) {
1334100117Salfred			if (!xdr_bool(xdrsp, &true))
1335100117Salfred				return (1);
1336100117Salfred			strp = "(...)";
1337194880Sdfr			if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1338100117Salfred				return (1);
1339100117Salfred		} else if ((dp->dp_flag & DP_DEFSET) == 0 &&
13401558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
13411558Srgrimes			hp = dp->dp_hosts;
13421558Srgrimes			while (hp) {
13431558Srgrimes				grp = hp->ht_grp;
13441558Srgrimes				if (grp->gr_type == GT_HOST) {
13451558Srgrimes					if (!xdr_bool(xdrsp, &true))
13461558Srgrimes						return (1);
134774462Salfred					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
13488871Srgrimes					if (!xdr_string(xdrsp, &strp,
1349194880Sdfr					    MNTNAMLEN))
13501558Srgrimes						return (1);
13511558Srgrimes				} else if (grp->gr_type == GT_NET) {
13521558Srgrimes					if (!xdr_bool(xdrsp, &true))
13531558Srgrimes						return (1);
13541558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
13558871Srgrimes					if (!xdr_string(xdrsp, &strp,
1356194880Sdfr					    MNTNAMLEN))
13571558Srgrimes						return (1);
13581558Srgrimes				}
13591558Srgrimes				hp = hp->ht_next;
13601558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
13611558Srgrimes					hp = adp->dp_hosts;
13621558Srgrimes					gotalldir = 0;
13631558Srgrimes				}
13641558Srgrimes			}
13651558Srgrimes		}
13661558Srgrimes		if (!xdr_bool(xdrsp, &false))
13671558Srgrimes			return (1);
1368100117Salfred		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
13691558Srgrimes			return (1);
13701558Srgrimes	}
13711558Srgrimes	return (0);
13721558Srgrimes}
13731558Srgrimes
1374285128Straszstatic int
1375216587Scharnierxdr_explist(XDR *xdrsp, caddr_t cp)
1376100117Salfred{
1377100117Salfred
1378100117Salfred	return xdr_explist_common(xdrsp, cp, 0);
1379100117Salfred}
1380100117Salfred
1381285128Straszstatic int
1382216587Scharnierxdr_explist_brief(XDR *xdrsp, caddr_t cp)
1383100117Salfred{
1384100117Salfred
1385100117Salfred	return xdr_explist_common(xdrsp, cp, 1);
1386100117Salfred}
1387100117Salfred
1388285128Straszstatic char *line;
1389285128Straszstatic size_t linesize;
1390285128Straszstatic FILE *exp_file;
13911558Srgrimes
13921558Srgrimes/*
1393166440Spjd * Get the export list from one, currently open file
13941558Srgrimes */
1395166440Spjdstatic void
1396216587Scharnierget_exportlist_one(void)
13971558Srgrimes{
1398324955Smanu	struct exportlist *ep;
13991558Srgrimes	struct grouplist *grp, *tgrp;
14001558Srgrimes	struct dirlist *dirhead;
1401166440Spjd	struct statfs fsb;
140272650Sgreen	struct xucred anon;
14031558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1404166440Spjd	int len, has_host, exflags, got_nondir, dirplen, netgrp;
14051558Srgrimes
1406192934Srmacklem	v4root_phase = 0;
14071558Srgrimes	dirhead = (struct dirlist *)NULL;
14081558Srgrimes	while (get_line()) {
14091558Srgrimes		if (debug)
141037663Scharnier			warnx("got line %s", line);
14111558Srgrimes		cp = line;
14121558Srgrimes		nextfield(&cp, &endcp);
14131558Srgrimes		if (*cp == '#')
14141558Srgrimes			goto nextline;
14151558Srgrimes
14161558Srgrimes		/*
14171558Srgrimes		 * Set defaults.
14181558Srgrimes		 */
14191558Srgrimes		has_host = FALSE;
14201558Srgrimes		anon = def_anon;
14211558Srgrimes		exflags = MNT_EXPORTED;
14221558Srgrimes		got_nondir = 0;
14231558Srgrimes		opt_flags = 0;
14241558Srgrimes		ep = (struct exportlist *)NULL;
1425192934Srmacklem		dirp = NULL;
14261558Srgrimes
14271558Srgrimes		/*
1428192934Srmacklem		 * Handle the V4 root dir.
1429192934Srmacklem		 */
1430192934Srmacklem		if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1431192934Srmacklem			/*
1432192934Srmacklem			 * V4: just indicates that it is the v4 root point,
1433192934Srmacklem			 * so skip over that and set v4root_phase.
1434192934Srmacklem			 */
1435192934Srmacklem			if (v4root_phase > 0) {
1436192934Srmacklem				syslog(LOG_ERR, "V4:duplicate line, ignored");
1437192934Srmacklem				goto nextline;
1438192934Srmacklem			}
1439192934Srmacklem			v4root_phase = 1;
1440192934Srmacklem			cp += 3;
1441192934Srmacklem			nextfield(&cp, &endcp);
1442192934Srmacklem		}
1443192934Srmacklem
1444192934Srmacklem		/*
14451558Srgrimes		 * Create new exports list entry
14461558Srgrimes		 */
14471558Srgrimes		len = endcp-cp;
14481558Srgrimes		tgrp = grp = get_grp();
14491558Srgrimes		while (len > 0) {
1450194880Sdfr			if (len > MNTNAMLEN) {
1451329392Sbrd			    getexp_err(ep, tgrp, "mountpoint too long");
14521558Srgrimes			    goto nextline;
14531558Srgrimes			}
14541558Srgrimes			if (*cp == '-') {
14551558Srgrimes			    if (ep == (struct exportlist *)NULL) {
1456329392Sbrd				getexp_err(ep, tgrp,
1457329392Sbrd				    "flag before export path definition");
14581558Srgrimes				goto nextline;
14591558Srgrimes			    }
14601558Srgrimes			    if (debug)
146137663Scharnier				warnx("doing opt %s", cp);
14621558Srgrimes			    got_nondir = 1;
14631558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
14641558Srgrimes				&exflags, &anon)) {
1465329392Sbrd				getexp_err(ep, tgrp, NULL);
14661558Srgrimes				goto nextline;
14671558Srgrimes			    }
14681558Srgrimes			} else if (*cp == '/') {
14691558Srgrimes			    savedc = *endcp;
14701558Srgrimes			    *endcp = '\0';
1471192934Srmacklem			    if (v4root_phase > 1) {
1472192934Srmacklem				    if (dirp != NULL) {
1473329392Sbrd					getexp_err(ep, tgrp, "Multiple V4 dirs");
1474192934Srmacklem					goto nextline;
1475192934Srmacklem				    }
1476192934Srmacklem			    }
14771558Srgrimes			    if (check_dirpath(cp) &&
14781558Srgrimes				statfs(cp, &fsb) >= 0) {
1479283008Srmacklem				if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1480283008Srmacklem				    syslog(LOG_ERR, "Warning: exporting of "
1481283008Srmacklem					"automounted fs %s not supported", cp);
14821558Srgrimes				if (got_nondir) {
1483329392Sbrd				    getexp_err(ep, tgrp, "dirs must be first");
14841558Srgrimes				    goto nextline;
14851558Srgrimes				}
1486192934Srmacklem				if (v4root_phase == 1) {
1487192934Srmacklem				    if (dirp != NULL) {
1488329392Sbrd					getexp_err(ep, tgrp, "Multiple V4 dirs");
14891558Srgrimes					goto nextline;
14901558Srgrimes				    }
1491192934Srmacklem				    if (strlen(v4root_dirpath) == 0) {
1492192934Srmacklem					strlcpy(v4root_dirpath, cp,
1493192934Srmacklem					    sizeof (v4root_dirpath));
1494192934Srmacklem				    } else if (strcmp(v4root_dirpath, cp)
1495192934Srmacklem					!= 0) {
1496192934Srmacklem					syslog(LOG_ERR,
1497192934Srmacklem					    "different V4 dirpath %s", cp);
1498329392Sbrd					getexp_err(ep, tgrp, NULL);
1499192934Srmacklem					goto nextline;
1500192934Srmacklem				    }
1501192934Srmacklem				    dirp = cp;
1502192934Srmacklem				    v4root_phase = 2;
1503192934Srmacklem				    got_nondir = 1;
1504192934Srmacklem				    ep = get_exp();
15051558Srgrimes				} else {
1506192934Srmacklem				    if (ep) {
1507192934Srmacklem					if (ep->ex_fs.val[0] !=
1508192934Srmacklem					    fsb.f_fsid.val[0] ||
1509192934Srmacklem					    ep->ex_fs.val[1] !=
1510192934Srmacklem					    fsb.f_fsid.val[1]) {
1511329392Sbrd						getexp_err(ep, tgrp,
1512329392Sbrd						    "fsid mismatch");
1513192934Srmacklem						goto nextline;
1514192934Srmacklem					}
1515192934Srmacklem				    } else {
1516192934Srmacklem					/*
1517192934Srmacklem					 * See if this directory is already
1518192934Srmacklem					 * in the list.
1519192934Srmacklem					 */
1520192934Srmacklem					ep = ex_search(&fsb.f_fsid);
1521192934Srmacklem					if (ep == (struct exportlist *)NULL) {
1522192934Srmacklem					    ep = get_exp();
1523192934Srmacklem					    ep->ex_fs = fsb.f_fsid;
1524324234Smanu					    ep->ex_fsdir = strdup(fsb.f_mntonname);
1525324234Smanu					    if (ep->ex_fsdir == NULL)
1526192934Srmacklem						out_of_mem();
1527192934Srmacklem					    if (debug)
1528192934Srmacklem						warnx(
1529192934Srmacklem						  "making new ep fs=0x%x,0x%x",
1530192934Srmacklem						  fsb.f_fsid.val[0],
1531192934Srmacklem						  fsb.f_fsid.val[1]);
1532192934Srmacklem					} else if (debug)
1533192934Srmacklem					    warnx("found ep fs=0x%x,0x%x",
1534192934Srmacklem						fsb.f_fsid.val[0],
1535192934Srmacklem						fsb.f_fsid.val[1]);
1536192934Srmacklem				    }
1537192934Srmacklem
15381558Srgrimes				    /*
1539192934Srmacklem				     * Add dirpath to export mount point.
15401558Srgrimes				     */
1541192934Srmacklem				    dirp = add_expdir(&dirhead, cp, len);
1542192934Srmacklem				    dirplen = len;
15431558Srgrimes				}
15441558Srgrimes			    } else {
1545329392Sbrd				getexp_err(ep, tgrp,
1546329392Sbrd				    "symbolic link in export path or statfs failed");
15471558Srgrimes				goto nextline;
15481558Srgrimes			    }
15491558Srgrimes			    *endcp = savedc;
15501558Srgrimes			} else {
15511558Srgrimes			    savedc = *endcp;
15521558Srgrimes			    *endcp = '\0';
15531558Srgrimes			    got_nondir = 1;
15541558Srgrimes			    if (ep == (struct exportlist *)NULL) {
1555329392Sbrd				getexp_err(ep, tgrp,
1556329392Sbrd				    "host(s) before export path definition");
15571558Srgrimes				goto nextline;
15581558Srgrimes			    }
15591558Srgrimes
15601558Srgrimes			    /*
15611558Srgrimes			     * Get the host or netgroup.
15621558Srgrimes			     */
15631558Srgrimes			    setnetgrent(cp);
15641558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
15651558Srgrimes			    do {
15661558Srgrimes				if (has_host) {
15671558Srgrimes				    grp->gr_next = get_grp();
15681558Srgrimes				    grp = grp->gr_next;
15691558Srgrimes				}
15701558Srgrimes				if (netgrp) {
157137003Sjoerg				    if (hst == 0) {
157237663Scharnier					syslog(LOG_ERR,
157337663Scharnier				"null hostname in netgroup %s, skipping", cp);
157437004Sjoerg					grp->gr_type = GT_IGNORE;
157537003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
157637663Scharnier					syslog(LOG_ERR,
157737663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
157829317Sjlemon					grp->gr_type = GT_IGNORE;
15791558Srgrimes				    }
15807401Swpaul				} else if (get_host(cp, grp, tgrp)) {
158137663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
158229317Sjlemon				    grp->gr_type = GT_IGNORE;
15831558Srgrimes				}
15841558Srgrimes				has_host = TRUE;
15851558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
15861558Srgrimes			    endnetgrent();
15871558Srgrimes			    *endcp = savedc;
15881558Srgrimes			}
15891558Srgrimes			cp = endcp;
15901558Srgrimes			nextfield(&cp, &endcp);
15911558Srgrimes			len = endcp - cp;
15921558Srgrimes		}
15931558Srgrimes		if (check_options(dirhead)) {
1594329392Sbrd			getexp_err(ep, tgrp, NULL);
15951558Srgrimes			goto nextline;
15961558Srgrimes		}
15971558Srgrimes		if (!has_host) {
159875641Siedowse			grp->gr_type = GT_DEFAULT;
15991558Srgrimes			if (debug)
160037663Scharnier				warnx("adding a default entry");
16011558Srgrimes
16021558Srgrimes		/*
16031558Srgrimes		 * Don't allow a network export coincide with a list of
16041558Srgrimes		 * host(s) on the same line.
16051558Srgrimes		 */
16061558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1607329392Sbrd			getexp_err(ep, tgrp, "network/host conflict");
16081558Srgrimes			goto nextline;
160929317Sjlemon
161074462Salfred		/*
161174462Salfred		 * If an export list was specified on this line, make sure
161229317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
161329317Sjlemon		 */
161429317Sjlemon		} else {
161529317Sjlemon			grp = tgrp;
161674462Salfred			while (grp && grp->gr_type == GT_IGNORE)
161729317Sjlemon				grp = grp->gr_next;
161829317Sjlemon			if (! grp) {
1619329392Sbrd			    getexp_err(ep, tgrp, "no valid entries");
162029317Sjlemon			    goto nextline;
162129317Sjlemon			}
16221558Srgrimes		}
16231558Srgrimes
1624192934Srmacklem		if (v4root_phase == 1) {
1625329392Sbrd			getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1626192934Srmacklem			goto nextline;
1627192934Srmacklem		}
1628192934Srmacklem
16291558Srgrimes		/*
16301558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
16311558Srgrimes		 * After loop, tgrp points to the start of the list and
16321558Srgrimes		 * grp points to the last entry in the list.
16331558Srgrimes		 */
16341558Srgrimes		grp = tgrp;
16351558Srgrimes		do {
163675635Siedowse			if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
163775635Siedowse			    &fsb)) {
1638329392Sbrd				getexp_err(ep, tgrp, NULL);
163975635Siedowse				goto nextline;
164075635Siedowse			}
16411558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
16421558Srgrimes
16431558Srgrimes		/*
1644192934Srmacklem		 * For V4: don't enter in mount lists.
1645192934Srmacklem		 */
1646194773Srmacklem		if (v4root_phase > 0 && v4root_phase <= 2) {
1647194773Srmacklem			/*
1648194773Srmacklem			 * Since these structures aren't used by mountd,
1649194773Srmacklem			 * free them up now.
1650194773Srmacklem			 */
1651194773Srmacklem			if (ep != NULL)
1652194773Srmacklem				free_exp(ep);
1653194773Srmacklem			while (tgrp != NULL) {
1654194773Srmacklem				grp = tgrp;
1655194773Srmacklem				tgrp = tgrp->gr_next;
1656194773Srmacklem				free_grp(grp);
1657194773Srmacklem			}
1658192934Srmacklem			goto nextline;
1659194773Srmacklem		}
1660192934Srmacklem
1661192934Srmacklem		/*
16621558Srgrimes		 * Success. Update the data structures.
16631558Srgrimes		 */
16641558Srgrimes		if (has_host) {
16659336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
16661558Srgrimes			grp->gr_next = grphead;
16671558Srgrimes			grphead = tgrp;
16681558Srgrimes		} else {
16691558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
16709336Sdfr				opt_flags);
16711558Srgrimes			free_grp(grp);
16721558Srgrimes		}
16731558Srgrimes		dirhead = (struct dirlist *)NULL;
16741558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
1675324955Smanu			SLIST_INSERT_HEAD(&exphead, ep, entries);
16761558Srgrimes
16771558Srgrimes			ep->ex_flag |= EX_LINKED;
16781558Srgrimes		}
16791558Srgrimesnextline:
1680192934Srmacklem		v4root_phase = 0;
16811558Srgrimes		if (dirhead) {
16821558Srgrimes			free_dir(dirhead);
16831558Srgrimes			dirhead = (struct dirlist *)NULL;
16841558Srgrimes		}
16851558Srgrimes	}
16861558Srgrimes}
16871558Srgrimes
16881558Srgrimes/*
1689166440Spjd * Get the export list from all specified files
1690166440Spjd */
1691285128Straszstatic void
1692216587Scharnierget_exportlist(void)
1693166440Spjd{
1694166440Spjd	struct exportlist *ep, *ep2;
1695166440Spjd	struct grouplist *grp, *tgrp;
1696166440Spjd	struct export_args export;
1697166440Spjd	struct iovec *iov;
1698166440Spjd	struct statfs *fsp, *mntbufp;
1699166440Spjd	struct xvfsconf vfc;
1700166440Spjd	char errmsg[255];
1701230352Seadler	int num, i;
1702166440Spjd	int iovlen;
1703168684Spjd	int done;
1704192934Srmacklem	struct nfsex_args eargs;
1705166440Spjd
1706241568Srmacklem	if (suspend_nfsd != 0)
1707241568Srmacklem		(void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1708192934Srmacklem	v4root_dirpath[0] = '\0';
1709166440Spjd	bzero(&export, sizeof(export));
1710166440Spjd	export.ex_flags = MNT_DELEXPORT;
1711166440Spjd	iov = NULL;
1712166440Spjd	iovlen = 0;
1713166440Spjd	bzero(errmsg, sizeof(errmsg));
1714166440Spjd
1715166440Spjd	/*
1716166440Spjd	 * First, get rid of the old list
1717166440Spjd	 */
1718324955Smanu	SLIST_FOREACH_SAFE(ep, &exphead, entries, ep2) {
1719324955Smanu		SLIST_REMOVE(&exphead, ep, exportlist, entries);
1720324955Smanu		free_exp(ep);
1721166440Spjd	}
1722166440Spjd
1723166440Spjd	grp = grphead;
1724166440Spjd	while (grp) {
1725166440Spjd		tgrp = grp;
1726166440Spjd		grp = grp->gr_next;
1727166440Spjd		free_grp(tgrp);
1728166440Spjd	}
1729166440Spjd	grphead = (struct grouplist *)NULL;
1730166440Spjd
1731166440Spjd	/*
1732192934Srmacklem	 * and the old V4 root dir.
1733192934Srmacklem	 */
1734192934Srmacklem	bzero(&eargs, sizeof (eargs));
1735192934Srmacklem	eargs.export.ex_flags = MNT_DELEXPORT;
1736282214Strasz	if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1737192934Srmacklem	    errno != ENOENT)
1738192934Srmacklem		syslog(LOG_ERR, "Can't delete exports for V4:");
1739192934Srmacklem
1740192934Srmacklem	/*
1741192934Srmacklem	 * and clear flag that notes if a public fh has been exported.
1742192934Srmacklem	 */
1743192934Srmacklem	has_publicfh = 0;
1744192934Srmacklem
1745192934Srmacklem	/*
1746166440Spjd	 * And delete exports that are in the kernel for all local
1747166440Spjd	 * filesystems.
1748166440Spjd	 * XXX: Should know how to handle all local exportable filesystems.
1749166440Spjd	 */
1750166440Spjd	num = getmntinfo(&mntbufp, MNT_NOWAIT);
1751166440Spjd
1752166440Spjd	if (num > 0) {
1753166440Spjd		build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1754166440Spjd		build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1755166440Spjd		build_iovec(&iov, &iovlen, "from", NULL, 0);
1756166440Spjd		build_iovec(&iov, &iovlen, "update", NULL, 0);
1757166440Spjd		build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1758166440Spjd		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1759166440Spjd	}
1760166440Spjd
1761166440Spjd	for (i = 0; i < num; i++) {
1762166440Spjd		fsp = &mntbufp[i];
1763166440Spjd		if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1764166440Spjd			syslog(LOG_ERR, "getvfsbyname() failed for %s",
1765166440Spjd			    fsp->f_fstypename);
1766166440Spjd			continue;
1767166440Spjd		}
1768166440Spjd
1769166440Spjd		/*
1770281699Ssjg		 * We do not need to delete "export" flag from
1771281699Ssjg		 * filesystems that do not have it set.
1772281699Ssjg		 */
1773281699Ssjg		if (!(fsp->f_flags & MNT_EXPORTED))
1774281699Ssjg		    continue;
1775281699Ssjg		/*
1776166440Spjd		 * Do not delete export for network filesystem by
1777166440Spjd		 * passing "export" arg to nmount().
1778166440Spjd		 * It only makes sense to do this for local filesystems.
1779166440Spjd		 */
1780166440Spjd		if (vfc.vfc_flags & VFCF_NETWORK)
1781166440Spjd			continue;
1782166440Spjd
1783166440Spjd		iov[1].iov_base = fsp->f_fstypename;
1784166440Spjd		iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1785166440Spjd		iov[3].iov_base = fsp->f_mntonname;
1786166440Spjd		iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1787166440Spjd		iov[5].iov_base = fsp->f_mntfromname;
1788166440Spjd		iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1789270183Sbdrewery		errmsg[0] = '\0';
1790166440Spjd
1791278523Skib		/*
1792278523Skib		 * EXDEV is returned when path exists but is not a
1793278523Skib		 * mount point.  May happens if raced with unmount.
1794278523Skib		 */
1795166440Spjd		if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1796278523Skib		    errno != ENOENT && errno != ENOTSUP && errno != EXDEV) {
1797166440Spjd			syslog(LOG_ERR,
1798166440Spjd			    "can't delete exports for %s: %m %s",
1799166440Spjd			    fsp->f_mntonname, errmsg);
1800166440Spjd		}
1801166440Spjd	}
1802166440Spjd
1803166440Spjd	if (iov != NULL) {
1804166440Spjd		/* Free strings allocated by strdup() in getmntopts.c */
1805166440Spjd		free(iov[0].iov_base); /* fstype */
1806166440Spjd		free(iov[2].iov_base); /* fspath */
1807166440Spjd		free(iov[4].iov_base); /* from */
1808166440Spjd		free(iov[6].iov_base); /* update */
1809166440Spjd		free(iov[8].iov_base); /* export */
1810166440Spjd		free(iov[10].iov_base); /* errmsg */
1811166440Spjd
1812166440Spjd		/* free iov, allocated by realloc() */
1813166440Spjd		free(iov);
1814166440Spjd		iovlen = 0;
1815166440Spjd	}
1816166440Spjd
1817166440Spjd	/*
1818166440Spjd	 * Read in the exports file and build the list, calling
1819166440Spjd	 * nmount() as we go along to push the export rules into the kernel.
1820166440Spjd	 */
1821168684Spjd	done = 0;
1822166440Spjd	for (i = 0; exnames[i] != NULL; i++) {
1823166440Spjd		if (debug)
1824166440Spjd			warnx("reading exports from %s", exnames[i]);
1825166440Spjd		if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1826168684Spjd			syslog(LOG_WARNING, "can't open %s", exnames[i]);
1827168684Spjd			continue;
1828166440Spjd		}
1829166440Spjd		get_exportlist_one();
1830166440Spjd		fclose(exp_file);
1831168684Spjd		done++;
1832166440Spjd	}
1833168684Spjd	if (done == 0) {
1834168684Spjd		syslog(LOG_ERR, "can't open any exports file");
1835168684Spjd		exit(2);
1836168684Spjd	}
1837192934Srmacklem
1838192934Srmacklem	/*
1839192934Srmacklem	 * If there was no public fh, clear any previous one set.
1840192934Srmacklem	 */
1841282214Strasz	if (has_publicfh == 0)
1842192934Srmacklem		(void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1843241568Srmacklem
1844241568Srmacklem	/* Resume the nfsd. If they weren't suspended, this is harmless. */
1845241568Srmacklem	(void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1846166440Spjd}
1847166440Spjd
1848166440Spjd/*
18491558Srgrimes * Allocate an export list element
18501558Srgrimes */
1851285128Straszstatic struct exportlist *
1852216587Scharnierget_exp(void)
18531558Srgrimes{
18541558Srgrimes	struct exportlist *ep;
18551558Srgrimes
1856224003Sdelphij	ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
18571558Srgrimes	if (ep == (struct exportlist *)NULL)
18581558Srgrimes		out_of_mem();
18591558Srgrimes	return (ep);
18601558Srgrimes}
18611558Srgrimes
18621558Srgrimes/*
18631558Srgrimes * Allocate a group list element
18641558Srgrimes */
1865285128Straszstatic struct grouplist *
1866216587Scharnierget_grp(void)
18671558Srgrimes{
18681558Srgrimes	struct grouplist *gp;
18691558Srgrimes
1870224003Sdelphij	gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
18711558Srgrimes	if (gp == (struct grouplist *)NULL)
18721558Srgrimes		out_of_mem();
18731558Srgrimes	return (gp);
18741558Srgrimes}
18751558Srgrimes
18761558Srgrimes/*
18771558Srgrimes * Clean up upon an error in get_exportlist().
18781558Srgrimes */
1879285128Straszstatic void
1880329392Sbrdgetexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
18811558Srgrimes{
18821558Srgrimes	struct grouplist *tgrp;
18831558Srgrimes
1884329392Sbrd	if (!(opt_flags & OP_QUIET)) {
1885329392Sbrd		if (reason != NULL)
1886329392Sbrd			syslog(LOG_ERR, "bad exports list line '%s': %s", line,
1887329392Sbrd			    reason);
1888329392Sbrd		else
1889329392Sbrd			syslog(LOG_ERR, "bad exports list line '%s'", line);
1890329392Sbrd	}
18911558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
18921558Srgrimes		free_exp(ep);
18931558Srgrimes	while (grp) {
18941558Srgrimes		tgrp = grp;
18951558Srgrimes		grp = grp->gr_next;
18961558Srgrimes		free_grp(tgrp);
18971558Srgrimes	}
18981558Srgrimes}
18991558Srgrimes
19001558Srgrimes/*
19011558Srgrimes * Search the export list for a matching fs.
19021558Srgrimes */
1903285128Straszstatic struct exportlist *
1904216587Scharnierex_search(fsid_t *fsid)
19051558Srgrimes{
19061558Srgrimes	struct exportlist *ep;
19071558Srgrimes
1908324955Smanu	SLIST_FOREACH(ep, &exphead, entries) {
19091558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
19101558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
19111558Srgrimes			return (ep);
19121558Srgrimes	}
1913324955Smanu
19141558Srgrimes	return (ep);
19151558Srgrimes}
19161558Srgrimes
19171558Srgrimes/*
19181558Srgrimes * Add a directory path to the list.
19191558Srgrimes */
1920285128Straszstatic char *
1921216587Scharnieradd_expdir(struct dirlist **dpp, char *cp, int len)
19221558Srgrimes{
19231558Srgrimes	struct dirlist *dp;
19241558Srgrimes
1925324234Smanu	dp = malloc(sizeof (struct dirlist));
192637663Scharnier	if (dp == (struct dirlist *)NULL)
192737663Scharnier		out_of_mem();
19281558Srgrimes	dp->dp_left = *dpp;
19291558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
19301558Srgrimes	dp->dp_flag = 0;
19311558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
1932324234Smanu	dp->dp_dirp = strndup(cp, len);
1933324234Smanu	if (dp->dp_dirp == NULL)
1934324234Smanu		out_of_mem();
19351558Srgrimes	*dpp = dp;
19361558Srgrimes	return (dp->dp_dirp);
19371558Srgrimes}
19381558Srgrimes
19391558Srgrimes/*
19401558Srgrimes * Hang the dir list element off the dirpath binary tree as required
19411558Srgrimes * and update the entry for host.
19421558Srgrimes */
1943285128Straszstatic void
1944216587Scharnierhang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1945216587Scharnier	int flags)
19461558Srgrimes{
19471558Srgrimes	struct hostlist *hp;
19481558Srgrimes	struct dirlist *dp2;
19491558Srgrimes
19509336Sdfr	if (flags & OP_ALLDIRS) {
19511558Srgrimes		if (ep->ex_defdir)
19521558Srgrimes			free((caddr_t)dp);
19531558Srgrimes		else
19541558Srgrimes			ep->ex_defdir = dp;
19559336Sdfr		if (grp == (struct grouplist *)NULL) {
19561558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
1957240902Srmacklem			/* Save the default security flavors list. */
1958240902Srmacklem			ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1959240902Srmacklem			if (ep->ex_numsecflavors > 0)
1960240902Srmacklem				memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1961240902Srmacklem				    sizeof(ep->ex_secflavors));
19629336Sdfr		} else while (grp) {
19631558Srgrimes			hp = get_ht();
19641558Srgrimes			hp->ht_grp = grp;
19651558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
19661558Srgrimes			ep->ex_defdir->dp_hosts = hp;
1967240902Srmacklem			/* Save the security flavors list for this host set. */
1968240902Srmacklem			grp->gr_numsecflavors = ep->ex_numsecflavors;
1969240902Srmacklem			if (ep->ex_numsecflavors > 0)
1970240902Srmacklem				memcpy(grp->gr_secflavors, ep->ex_secflavors,
1971240902Srmacklem				    sizeof(ep->ex_secflavors));
19721558Srgrimes			grp = grp->gr_next;
19731558Srgrimes		}
19741558Srgrimes	} else {
19751558Srgrimes
19761558Srgrimes		/*
197737663Scharnier		 * Loop through the directories adding them to the tree.
19781558Srgrimes		 */
19791558Srgrimes		while (dp) {
19801558Srgrimes			dp2 = dp->dp_left;
1981240902Srmacklem			add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
19821558Srgrimes			dp = dp2;
19831558Srgrimes		}
19841558Srgrimes	}
19851558Srgrimes}
19861558Srgrimes
19871558Srgrimes/*
19881558Srgrimes * Traverse the binary tree either updating a node that is already there
19891558Srgrimes * for the new directory or adding the new node.
19901558Srgrimes */
1991285128Straszstatic void
1992216587Scharnieradd_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
1993240902Srmacklem	int flags, struct exportlist *ep)
19941558Srgrimes{
19951558Srgrimes	struct dirlist *dp;
19961558Srgrimes	struct hostlist *hp;
19971558Srgrimes	int cmp;
19981558Srgrimes
19991558Srgrimes	dp = *dpp;
20001558Srgrimes	if (dp) {
20011558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
20021558Srgrimes		if (cmp > 0) {
2003240902Srmacklem			add_dlist(&dp->dp_left, newdp, grp, flags, ep);
20041558Srgrimes			return;
20051558Srgrimes		} else if (cmp < 0) {
2006240902Srmacklem			add_dlist(&dp->dp_right, newdp, grp, flags, ep);
20071558Srgrimes			return;
20081558Srgrimes		} else
20091558Srgrimes			free((caddr_t)newdp);
20101558Srgrimes	} else {
20111558Srgrimes		dp = newdp;
20121558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
20131558Srgrimes		*dpp = dp;
20141558Srgrimes	}
20151558Srgrimes	if (grp) {
20161558Srgrimes
20171558Srgrimes		/*
20181558Srgrimes		 * Hang all of the host(s) off of the directory point.
20191558Srgrimes		 */
20201558Srgrimes		do {
20211558Srgrimes			hp = get_ht();
20221558Srgrimes			hp->ht_grp = grp;
20231558Srgrimes			hp->ht_next = dp->dp_hosts;
20241558Srgrimes			dp->dp_hosts = hp;
2025240902Srmacklem			/* Save the security flavors list for this host set. */
2026240902Srmacklem			grp->gr_numsecflavors = ep->ex_numsecflavors;
2027240902Srmacklem			if (ep->ex_numsecflavors > 0)
2028240902Srmacklem				memcpy(grp->gr_secflavors, ep->ex_secflavors,
2029240902Srmacklem				    sizeof(ep->ex_secflavors));
20301558Srgrimes			grp = grp->gr_next;
20311558Srgrimes		} while (grp);
20329336Sdfr	} else {
20331558Srgrimes		dp->dp_flag |= DP_DEFSET;
2034240902Srmacklem		/* Save the default security flavors list. */
2035240902Srmacklem		ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2036240902Srmacklem		if (ep->ex_numsecflavors > 0)
2037240902Srmacklem			memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2038240902Srmacklem			    sizeof(ep->ex_secflavors));
20399336Sdfr	}
20401558Srgrimes}
20411558Srgrimes
20421558Srgrimes/*
20431558Srgrimes * Search for a dirpath on the export point.
20441558Srgrimes */
2045285128Straszstatic struct dirlist *
2046216587Scharnierdirp_search(struct dirlist *dp, char *dirp)
20471558Srgrimes{
20481558Srgrimes	int cmp;
20491558Srgrimes
20501558Srgrimes	if (dp) {
205174462Salfred		cmp = strcmp(dp->dp_dirp, dirp);
20521558Srgrimes		if (cmp > 0)
205374462Salfred			return (dirp_search(dp->dp_left, dirp));
20541558Srgrimes		else if (cmp < 0)
205574462Salfred			return (dirp_search(dp->dp_right, dirp));
20561558Srgrimes		else
20571558Srgrimes			return (dp);
20581558Srgrimes	}
20591558Srgrimes	return (dp);
20601558Srgrimes}
20611558Srgrimes
20621558Srgrimes/*
20631558Srgrimes * Scan for a host match in a directory tree.
20641558Srgrimes */
2065285128Straszstatic int
2066216587Scharnierchk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2067240902Srmacklem	int *hostsetp, int *numsecflavors, int **secflavorsp)
20681558Srgrimes{
20691558Srgrimes	struct hostlist *hp;
20701558Srgrimes	struct grouplist *grp;
207174462Salfred	struct addrinfo *ai;
20721558Srgrimes
20731558Srgrimes	if (dp) {
20741558Srgrimes		if (dp->dp_flag & DP_DEFSET)
20759336Sdfr			*defsetp = dp->dp_flag;
20761558Srgrimes		hp = dp->dp_hosts;
20771558Srgrimes		while (hp) {
20781558Srgrimes			grp = hp->ht_grp;
20791558Srgrimes			switch (grp->gr_type) {
20801558Srgrimes			case GT_HOST:
208174462Salfred				ai = grp->gr_ptr.gt_addrinfo;
208274462Salfred				for (; ai; ai = ai->ai_next) {
208375801Siedowse					if (!sacmp(ai->ai_addr, saddr, NULL)) {
208474462Salfred						*hostsetp =
208574462Salfred						    (hp->ht_flag | DP_HOSTSET);
2086240902Srmacklem						if (numsecflavors != NULL) {
2087240902Srmacklem							*numsecflavors =
2088240902Srmacklem							    grp->gr_numsecflavors;
2089240902Srmacklem							*secflavorsp =
2090240902Srmacklem							    grp->gr_secflavors;
2091240902Srmacklem						}
209274462Salfred						return (1);
209374462Salfred					}
20949336Sdfr				}
209575801Siedowse				break;
20961558Srgrimes			case GT_NET:
209775801Siedowse				if (!sacmp(saddr, (struct sockaddr *)
209875801Siedowse				    &grp->gr_ptr.gt_net.nt_net,
209975801Siedowse				    (struct sockaddr *)
210075801Siedowse				    &grp->gr_ptr.gt_net.nt_mask)) {
210174462Salfred					*hostsetp = (hp->ht_flag | DP_HOSTSET);
2102240902Srmacklem					if (numsecflavors != NULL) {
2103240902Srmacklem						*numsecflavors =
2104240902Srmacklem						    grp->gr_numsecflavors;
2105240902Srmacklem						*secflavorsp =
2106240902Srmacklem						    grp->gr_secflavors;
2107240902Srmacklem					}
210874462Salfred					return (1);
210974462Salfred				}
211075801Siedowse				break;
211175801Siedowse			}
21121558Srgrimes			hp = hp->ht_next;
21131558Srgrimes		}
21141558Srgrimes	}
21151558Srgrimes	return (0);
21161558Srgrimes}
21171558Srgrimes
21181558Srgrimes/*
21191558Srgrimes * Scan tree for a host that matches the address.
21201558Srgrimes */
2121285128Straszstatic int
2122216587Scharnierscan_tree(struct dirlist *dp, struct sockaddr *saddr)
21231558Srgrimes{
21249336Sdfr	int defset, hostset;
21251558Srgrimes
21261558Srgrimes	if (dp) {
21271558Srgrimes		if (scan_tree(dp->dp_left, saddr))
21281558Srgrimes			return (1);
2129240902Srmacklem		if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
21301558Srgrimes			return (1);
21311558Srgrimes		if (scan_tree(dp->dp_right, saddr))
21321558Srgrimes			return (1);
21331558Srgrimes	}
21341558Srgrimes	return (0);
21351558Srgrimes}
21361558Srgrimes
21371558Srgrimes/*
21381558Srgrimes * Traverse the dirlist tree and free it up.
21391558Srgrimes */
2140285128Straszstatic void
2141216587Scharnierfree_dir(struct dirlist *dp)
21421558Srgrimes{
21431558Srgrimes
21441558Srgrimes	if (dp) {
21451558Srgrimes		free_dir(dp->dp_left);
21461558Srgrimes		free_dir(dp->dp_right);
21471558Srgrimes		free_host(dp->dp_hosts);
2148324234Smanu		free(dp->dp_dirp);
2149324234Smanu		free(dp);
21501558Srgrimes	}
21511558Srgrimes}
21521558Srgrimes
21531558Srgrimes/*
2154184588Sdfr * Parse a colon separated list of security flavors
2155184588Sdfr */
2156285128Straszstatic int
2157216587Scharnierparsesec(char *seclist, struct exportlist *ep)
2158184588Sdfr{
2159184588Sdfr	char *cp, savedc;
2160184588Sdfr	int flavor;
2161184588Sdfr
2162184588Sdfr	ep->ex_numsecflavors = 0;
2163184588Sdfr	for (;;) {
2164184588Sdfr		cp = strchr(seclist, ':');
2165184588Sdfr		if (cp) {
2166184588Sdfr			savedc = *cp;
2167184588Sdfr			*cp = '\0';
2168184588Sdfr		}
2169184588Sdfr
2170184588Sdfr		if (!strcmp(seclist, "sys"))
2171184588Sdfr			flavor = AUTH_SYS;
2172184588Sdfr		else if (!strcmp(seclist, "krb5"))
2173184588Sdfr			flavor = RPCSEC_GSS_KRB5;
2174184588Sdfr		else if (!strcmp(seclist, "krb5i"))
2175184588Sdfr			flavor = RPCSEC_GSS_KRB5I;
2176184588Sdfr		else if (!strcmp(seclist, "krb5p"))
2177184588Sdfr			flavor = RPCSEC_GSS_KRB5P;
2178184588Sdfr		else {
2179184588Sdfr			if (cp)
2180184588Sdfr				*cp = savedc;
2181184588Sdfr			syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2182184588Sdfr			return (1);
2183184588Sdfr		}
2184184588Sdfr		if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2185184588Sdfr			if (cp)
2186184588Sdfr				*cp = savedc;
2187184588Sdfr			syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2188184588Sdfr			return (1);
2189184588Sdfr		}
2190184588Sdfr		ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2191184588Sdfr		ep->ex_numsecflavors++;
2192184588Sdfr		if (cp) {
2193184588Sdfr			*cp = savedc;
2194184588Sdfr			seclist = cp + 1;
2195184588Sdfr		} else {
2196184588Sdfr			break;
2197184588Sdfr		}
2198184588Sdfr	}
2199184588Sdfr	return (0);
2200184588Sdfr}
2201184588Sdfr
2202184588Sdfr/*
22031558Srgrimes * Parse the option string and update fields.
22041558Srgrimes * Option arguments may either be -<option>=<value> or
22051558Srgrimes * -<option> <value>
22061558Srgrimes */
2207285128Straszstatic int
2208216587Scharnierdo_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2209216587Scharnier	int *has_hostp, int *exflagsp, struct xucred *cr)
22101558Srgrimes{
22111558Srgrimes	char *cpoptarg, *cpoptend;
22121558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
22131558Srgrimes	int allflag, usedarg;
22141558Srgrimes
221551968Salfred	savedc2 = '\0';
22161558Srgrimes	cpopt = *cpp;
22171558Srgrimes	cpopt++;
22181558Srgrimes	cp = *endcpp;
22191558Srgrimes	savedc = *cp;
22201558Srgrimes	*cp = '\0';
22211558Srgrimes	while (cpopt && *cpopt) {
22221558Srgrimes		allflag = 1;
22231558Srgrimes		usedarg = -2;
222437663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
22251558Srgrimes			*cpoptend++ = '\0';
222637663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
22271558Srgrimes				*cpoptarg++ = '\0';
22281558Srgrimes		} else {
222937663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
22301558Srgrimes				*cpoptarg++ = '\0';
22311558Srgrimes			else {
22321558Srgrimes				*cp = savedc;
22331558Srgrimes				nextfield(&cp, &endcp);
22341558Srgrimes				**endcpp = '\0';
22351558Srgrimes				if (endcp > cp && *cp != '-') {
22361558Srgrimes					cpoptarg = cp;
22371558Srgrimes					savedc2 = *endcp;
22381558Srgrimes					*endcp = '\0';
22391558Srgrimes					usedarg = 0;
22401558Srgrimes				}
22411558Srgrimes			}
22421558Srgrimes		}
22431558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
22441558Srgrimes			*exflagsp |= MNT_EXRDONLY;
22451558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
22461558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
22471558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
22481558Srgrimes			usedarg++;
22491558Srgrimes			parsecred(cpoptarg, cr);
22501558Srgrimes			if (allflag == 0) {
22511558Srgrimes				*exflagsp |= MNT_EXPORTANON;
22521558Srgrimes				opt_flags |= OP_MAPALL;
22531558Srgrimes			} else
22541558Srgrimes				opt_flags |= OP_MAPROOT;
22551558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
225675801Siedowse		    !strcmp(cpopt, "m"))) {
22571558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
225837663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
22591558Srgrimes				return (1);
22601558Srgrimes			}
22611558Srgrimes			usedarg++;
22621558Srgrimes			opt_flags |= OP_MASK;
22631558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
22641558Srgrimes			!strcmp(cpopt, "n"))) {
226574462Salfred			if (strchr(cpoptarg, '/') != NULL) {
226674462Salfred				if (debug)
226774462Salfred					fprintf(stderr, "setting OP_MASKLEN\n");
226874462Salfred				opt_flags |= OP_MASKLEN;
226974462Salfred			}
22701558Srgrimes			if (grp->gr_type != GT_NULL) {
227137663Scharnier				syslog(LOG_ERR, "network/host conflict");
22721558Srgrimes				return (1);
22731558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
227437663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
22751558Srgrimes				return (1);
22761558Srgrimes			}
22771558Srgrimes			grp->gr_type = GT_NET;
22781558Srgrimes			*has_hostp = 1;
22791558Srgrimes			usedarg++;
22801558Srgrimes			opt_flags |= OP_NET;
22811558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
22821558Srgrimes			opt_flags |= OP_ALLDIRS;
228327447Sdfr		} else if (!strcmp(cpopt, "public")) {
228427447Sdfr			*exflagsp |= MNT_EXPUBLIC;
228527447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
228627447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
228727447Sdfr			opt_flags |= OP_MAPALL;
228827447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
228927447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
2290100336Sjoerg		} else if (!strcmp(cpopt, "quiet")) {
2291100336Sjoerg			opt_flags |= OP_QUIET;
2292247034Spluknet		} else if (cpoptarg && !strcmp(cpopt, "sec")) {
2293184588Sdfr			if (parsesec(cpoptarg, ep))
2294184588Sdfr				return (1);
2295184588Sdfr			opt_flags |= OP_SEC;
2296184588Sdfr			usedarg++;
22971558Srgrimes		} else {
229837663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
22991558Srgrimes			return (1);
23001558Srgrimes		}
23011558Srgrimes		if (usedarg >= 0) {
23021558Srgrimes			*endcp = savedc2;
23031558Srgrimes			**endcpp = savedc;
23041558Srgrimes			if (usedarg > 0) {
23051558Srgrimes				*cpp = cp;
23061558Srgrimes				*endcpp = endcp;
23071558Srgrimes			}
23081558Srgrimes			return (0);
23091558Srgrimes		}
23101558Srgrimes		cpopt = cpoptend;
23111558Srgrimes	}
23121558Srgrimes	**endcpp = savedc;
23131558Srgrimes	return (0);
23141558Srgrimes}
23151558Srgrimes
23161558Srgrimes/*
23171558Srgrimes * Translate a character string to the corresponding list of network
23181558Srgrimes * addresses for a hostname.
23191558Srgrimes */
2320285128Straszstatic int
2321216587Scharnierget_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
23221558Srgrimes{
23237401Swpaul	struct grouplist *checkgrp;
232475635Siedowse	struct addrinfo *ai, *tai, hints;
232574462Salfred	int ecode;
232674462Salfred	char host[NI_MAXHOST];
23271558Srgrimes
232874462Salfred	if (grp->gr_type != GT_NULL) {
232974462Salfred		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
23301558Srgrimes		return (1);
23311558Srgrimes	}
233274462Salfred	memset(&hints, 0, sizeof hints);
233374462Salfred	hints.ai_flags = AI_CANONNAME;
233474462Salfred	hints.ai_protocol = IPPROTO_UDP;
233574462Salfred	ecode = getaddrinfo(cp, NULL, &hints, &ai);
233674462Salfred	if (ecode != 0) {
233775635Siedowse		syslog(LOG_ERR,"can't get address info for host %s", cp);
233874462Salfred		return 1;
233974462Salfred	}
234074462Salfred	grp->gr_ptr.gt_addrinfo = ai;
234174462Salfred	while (ai != NULL) {
234274462Salfred		if (ai->ai_canonname == NULL) {
234374462Salfred			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2344146187Sume			    sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
234574462Salfred				strlcpy(host, "?", sizeof(host));
234674462Salfred			ai->ai_canonname = strdup(host);
234774462Salfred			ai->ai_flags |= AI_CANONNAME;
234875641Siedowse		}
234974462Salfred		if (debug)
235075635Siedowse			fprintf(stderr, "got host %s\n", ai->ai_canonname);
235175635Siedowse		/*
235275635Siedowse		 * Sanity check: make sure we don't already have an entry
235375635Siedowse		 * for this host in the grouplist.
235475635Siedowse		 */
235575635Siedowse		for (checkgrp = tgrp; checkgrp != NULL;
235675635Siedowse		    checkgrp = checkgrp->gr_next) {
235775635Siedowse			if (checkgrp->gr_type != GT_HOST)
235875635Siedowse				continue;
235975635Siedowse			for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
236075635Siedowse			    tai = tai->ai_next) {
236175801Siedowse				if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
236275635Siedowse					continue;
236375635Siedowse				if (debug)
236475635Siedowse					fprintf(stderr,
236575635Siedowse					    "ignoring duplicate host %s\n",
236675635Siedowse					    ai->ai_canonname);
236775635Siedowse				grp->gr_type = GT_IGNORE;
236875635Siedowse				return (0);
236975635Siedowse			}
237075635Siedowse		}
237174462Salfred		ai = ai->ai_next;
23721558Srgrimes	}
237375635Siedowse	grp->gr_type = GT_HOST;
23741558Srgrimes	return (0);
23751558Srgrimes}
23761558Srgrimes
23771558Srgrimes/*
23781558Srgrimes * Free up an exports list component
23791558Srgrimes */
2380285128Straszstatic void
2381216587Scharnierfree_exp(struct exportlist *ep)
23821558Srgrimes{
23831558Srgrimes
23841558Srgrimes	if (ep->ex_defdir) {
23851558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
23861558Srgrimes		free((caddr_t)ep->ex_defdir);
23871558Srgrimes	}
23881558Srgrimes	if (ep->ex_fsdir)
23891558Srgrimes		free(ep->ex_fsdir);
239027447Sdfr	if (ep->ex_indexfile)
239127447Sdfr		free(ep->ex_indexfile);
23921558Srgrimes	free_dir(ep->ex_dirl);
23931558Srgrimes	free((caddr_t)ep);
23941558Srgrimes}
23951558Srgrimes
23961558Srgrimes/*
23971558Srgrimes * Free hosts.
23981558Srgrimes */
2399285128Straszstatic void
2400216587Scharnierfree_host(struct hostlist *hp)
24011558Srgrimes{
24021558Srgrimes	struct hostlist *hp2;
24031558Srgrimes
24041558Srgrimes	while (hp) {
24051558Srgrimes		hp2 = hp;
24061558Srgrimes		hp = hp->ht_next;
24071558Srgrimes		free((caddr_t)hp2);
24081558Srgrimes	}
24091558Srgrimes}
24101558Srgrimes
2411285128Straszstatic struct hostlist *
2412216587Scharnierget_ht(void)
24131558Srgrimes{
24141558Srgrimes	struct hostlist *hp;
24151558Srgrimes
24161558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
24171558Srgrimes	if (hp == (struct hostlist *)NULL)
24181558Srgrimes		out_of_mem();
24191558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
24209336Sdfr	hp->ht_flag = 0;
24211558Srgrimes	return (hp);
24221558Srgrimes}
24231558Srgrimes
24241558Srgrimes/*
24251558Srgrimes * Out of memory, fatal
24261558Srgrimes */
2427285128Straszstatic void
2428216587Scharnierout_of_mem(void)
24291558Srgrimes{
24301558Srgrimes
243137663Scharnier	syslog(LOG_ERR, "out of memory");
24321558Srgrimes	exit(2);
24331558Srgrimes}
24341558Srgrimes
24351558Srgrimes/*
2436158857Srodrigc * Do the nmount() syscall with the update flag to push the export info into
24371558Srgrimes * the kernel.
24381558Srgrimes */
2439285128Straszstatic int
2440158857Srodrigcdo_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2441158857Srodrigc    struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
24421558Srgrimes{
244375841Siedowse	struct statfs fsb1;
244474462Salfred	struct addrinfo *ai;
2445282214Strasz	struct export_args *eap;
2446158857Srodrigc	char errmsg[255];
2447158857Srodrigc	char *cp;
24481558Srgrimes	int done;
2449158857Srodrigc	char savedc;
2450158857Srodrigc	struct iovec *iov;
2451184588Sdfr	int i, iovlen;
2452158857Srodrigc	int ret;
2453192934Srmacklem	struct nfsex_args nfsea;
24541558Srgrimes
2455282214Strasz	eap = &nfsea.export;
2456192934Srmacklem
2457158857Srodrigc	cp = NULL;
2458158857Srodrigc	savedc = '\0';
2459158857Srodrigc	iov = NULL;
2460158857Srodrigc	iovlen = 0;
2461158857Srodrigc	ret = 0;
246275801Siedowse
2463192934Srmacklem	bzero(eap, sizeof (struct export_args));
2464158857Srodrigc	bzero(errmsg, sizeof(errmsg));
2465192934Srmacklem	eap->ex_flags = exflags;
2466192934Srmacklem	eap->ex_anon = *anoncrp;
2467192934Srmacklem	eap->ex_indexfile = ep->ex_indexfile;
246875641Siedowse	if (grp->gr_type == GT_HOST)
246974462Salfred		ai = grp->gr_ptr.gt_addrinfo;
247075641Siedowse	else
247175641Siedowse		ai = NULL;
2472192934Srmacklem	eap->ex_numsecflavors = ep->ex_numsecflavors;
2473192934Srmacklem	for (i = 0; i < eap->ex_numsecflavors; i++)
2474192934Srmacklem		eap->ex_secflavors[i] = ep->ex_secflavors[i];
2475192934Srmacklem	if (eap->ex_numsecflavors == 0) {
2476192934Srmacklem		eap->ex_numsecflavors = 1;
2477192934Srmacklem		eap->ex_secflavors[0] = AUTH_SYS;
2478184588Sdfr	}
24791558Srgrimes	done = FALSE;
2480158857Srodrigc
2481192934Srmacklem	if (v4root_phase == 0) {
2482192934Srmacklem		build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2483192934Srmacklem		build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2484192934Srmacklem		build_iovec(&iov, &iovlen, "from", NULL, 0);
2485192934Srmacklem		build_iovec(&iov, &iovlen, "update", NULL, 0);
2486192934Srmacklem		build_iovec(&iov, &iovlen, "export", eap,
2487192934Srmacklem		    sizeof (struct export_args));
2488192934Srmacklem		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2489192934Srmacklem	}
2490158857Srodrigc
24911558Srgrimes	while (!done) {
24921558Srgrimes		switch (grp->gr_type) {
24931558Srgrimes		case GT_HOST:
249475641Siedowse			if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
249574462Salfred				goto skip;
2496192934Srmacklem			eap->ex_addr = ai->ai_addr;
2497192934Srmacklem			eap->ex_addrlen = ai->ai_addrlen;
2498192934Srmacklem			eap->ex_masklen = 0;
24991558Srgrimes			break;
25001558Srgrimes		case GT_NET:
250175801Siedowse			if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
250274462Salfred			    have_v6 == 0)
250374462Salfred				goto skip;
2504192934Srmacklem			eap->ex_addr =
250575801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2506192934Srmacklem			eap->ex_addrlen =
2507158857Srodrigc			    ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2508192934Srmacklem			eap->ex_mask =
250975801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2510192934Srmacklem			eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
25111558Srgrimes			break;
251275641Siedowse		case GT_DEFAULT:
2513192934Srmacklem			eap->ex_addr = NULL;
2514192934Srmacklem			eap->ex_addrlen = 0;
2515192934Srmacklem			eap->ex_mask = NULL;
2516192934Srmacklem			eap->ex_masklen = 0;
251775641Siedowse			break;
25187401Swpaul		case GT_IGNORE:
2519158857Srodrigc			ret = 0;
2520158857Srodrigc			goto error_exit;
25217401Swpaul			break;
25221558Srgrimes		default:
252337663Scharnier			syslog(LOG_ERR, "bad grouptype");
25241558Srgrimes			if (cp)
25251558Srgrimes				*cp = savedc;
2526158857Srodrigc			ret = 1;
2527158857Srodrigc			goto error_exit;
2528298089Spfg		}
25291558Srgrimes
25301558Srgrimes		/*
2531192934Srmacklem		 * For V4:, use the nfssvc() syscall, instead of mount().
25321558Srgrimes		 */
2533192934Srmacklem		if (v4root_phase == 2) {
2534192934Srmacklem			nfsea.fspec = v4root_dirpath;
2535282214Strasz			if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2536192934Srmacklem				syslog(LOG_ERR, "Exporting V4: failed");
2537192934Srmacklem				return (2);
2538158857Srodrigc			}
2539192934Srmacklem		} else {
2540192934Srmacklem			/*
2541192934Srmacklem			 * XXX:
2542192934Srmacklem			 * Maybe I should just use the fsb->f_mntonname path
2543192934Srmacklem			 * instead of looping back up the dirp to the mount
2544192934Srmacklem			 * point??
2545192934Srmacklem			 * Also, needs to know how to export all types of local
2546192934Srmacklem			 * exportable filesystems and not just "ufs".
2547192934Srmacklem			 */
2548192934Srmacklem			iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2549192934Srmacklem			iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2550192934Srmacklem			iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2551192934Srmacklem			iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2552192934Srmacklem			iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2553192934Srmacklem			iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2554270183Sbdrewery			errmsg[0] = '\0';
2555192934Srmacklem
2556192934Srmacklem			while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2557192934Srmacklem				if (cp)
2558192934Srmacklem					*cp-- = savedc;
2559192934Srmacklem				else
2560192934Srmacklem					cp = dirp + dirplen - 1;
2561192934Srmacklem				if (opt_flags & OP_QUIET) {
2562192934Srmacklem					ret = 1;
2563192934Srmacklem					goto error_exit;
2564192934Srmacklem				}
2565192934Srmacklem				if (errno == EPERM) {
2566192934Srmacklem					if (debug)
2567239744Sdelphij						warnx("can't change attributes for %s: %s",
2568239744Sdelphij						    dirp, errmsg);
2569192934Srmacklem					syslog(LOG_ERR,
2570239744Sdelphij					   "can't change attributes for %s: %s",
2571239744Sdelphij					    dirp, errmsg);
2572192934Srmacklem					ret = 1;
2573192934Srmacklem					goto error_exit;
2574192934Srmacklem				}
2575192934Srmacklem				if (opt_flags & OP_ALLDIRS) {
2576192934Srmacklem					if (errno == EINVAL)
2577192934Srmacklem						syslog(LOG_ERR,
2578100336Sjoerg		"-alldirs requested but %s is not a filesystem mountpoint",
2579192934Srmacklem						    dirp);
2580192934Srmacklem					else
2581192934Srmacklem						syslog(LOG_ERR,
2582192934Srmacklem						    "could not remount %s: %m",
2583192934Srmacklem						    dirp);
2584192934Srmacklem					ret = 1;
2585192934Srmacklem					goto error_exit;
2586192934Srmacklem				}
2587192934Srmacklem				/* back up over the last component */
2588192934Srmacklem				while (*cp == '/' && cp > dirp)
2589192934Srmacklem					cp--;
2590192934Srmacklem				while (*(cp - 1) != '/' && cp > dirp)
2591192934Srmacklem					cp--;
2592192934Srmacklem				if (cp == dirp) {
2593192934Srmacklem					if (debug)
2594192934Srmacklem						warnx("mnt unsucc");
2595192934Srmacklem					syslog(LOG_ERR, "can't export %s %s",
2596192934Srmacklem					    dirp, errmsg);
2597192934Srmacklem					ret = 1;
2598192934Srmacklem					goto error_exit;
2599192934Srmacklem				}
2600192934Srmacklem				savedc = *cp;
2601192934Srmacklem				*cp = '\0';
2602192934Srmacklem				/*
2603192934Srmacklem				 * Check that we're still on the same
2604192934Srmacklem				 * filesystem.
2605192934Srmacklem				 */
2606192934Srmacklem				if (statfs(dirp, &fsb1) != 0 ||
2607192934Srmacklem				    bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2608192934Srmacklem				    sizeof (fsb1.f_fsid)) != 0) {
2609192934Srmacklem					*cp = savedc;
2610100336Sjoerg					syslog(LOG_ERR,
2611192934Srmacklem					    "can't export %s %s", dirp,
2612192934Srmacklem					    errmsg);
2613192934Srmacklem					ret = 1;
2614192934Srmacklem					goto error_exit;
2615192934Srmacklem				}
26161558Srgrimes			}
26171558Srgrimes		}
2618192934Srmacklem
2619192934Srmacklem		/*
2620192934Srmacklem		 * For the experimental server:
2621192934Srmacklem		 * If this is the public directory, get the file handle
2622192934Srmacklem		 * and load it into the kernel via the nfssvc() syscall.
2623192934Srmacklem		 */
2624282214Strasz		if ((exflags & MNT_EXPUBLIC) != 0) {
2625192934Srmacklem			fhandle_t fh;
2626192934Srmacklem			char *public_name;
2627192934Srmacklem
2628192934Srmacklem			if (eap->ex_indexfile != NULL)
2629192934Srmacklem				public_name = eap->ex_indexfile;
2630192934Srmacklem			else
2631192934Srmacklem				public_name = dirp;
2632192934Srmacklem			if (getfh(public_name, &fh) < 0)
2633192934Srmacklem				syslog(LOG_ERR,
2634192934Srmacklem				    "Can't get public fh for %s", public_name);
2635192934Srmacklem			else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2636192934Srmacklem				syslog(LOG_ERR,
2637192934Srmacklem				    "Can't set public fh for %s", public_name);
2638192934Srmacklem			else
2639192934Srmacklem				has_publicfh = 1;
2640192934Srmacklem		}
264174462Salfredskip:
264275641Siedowse		if (ai != NULL)
264374462Salfred			ai = ai->ai_next;
264475641Siedowse		if (ai == NULL)
26451558Srgrimes			done = TRUE;
26461558Srgrimes	}
26471558Srgrimes	if (cp)
26481558Srgrimes		*cp = savedc;
2649158857Srodrigcerror_exit:
2650158857Srodrigc	/* free strings allocated by strdup() in getmntopts.c */
2651158857Srodrigc	if (iov != NULL) {
2652158857Srodrigc		free(iov[0].iov_base); /* fstype */
2653158857Srodrigc		free(iov[2].iov_base); /* fspath */
2654158857Srodrigc		free(iov[4].iov_base); /* from */
2655158857Srodrigc		free(iov[6].iov_base); /* update */
2656158857Srodrigc		free(iov[8].iov_base); /* export */
2657158857Srodrigc		free(iov[10].iov_base); /* errmsg */
2658158857Srodrigc
2659158857Srodrigc		/* free iov, allocated by realloc() */
2660158857Srodrigc		free(iov);
2661158857Srodrigc	}
2662158857Srodrigc	return (ret);
26631558Srgrimes}
26641558Srgrimes
26651558Srgrimes/*
26661558Srgrimes * Translate a net address.
266775801Siedowse *
266875801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
26691558Srgrimes */
2670285128Straszstatic int
2671216587Scharnierget_net(char *cp, struct netmsk *net, int maskflg)
26721558Srgrimes{
267375861Siedowse	struct netent *np = NULL;
267474462Salfred	char *name, *p, *prefp;
267575801Siedowse	struct sockaddr_in sin;
267675861Siedowse	struct sockaddr *sa = NULL;
267774462Salfred	struct addrinfo hints, *ai = NULL;
267874462Salfred	char netname[NI_MAXHOST];
267974462Salfred	long preflen;
26801558Srgrimes
268175635Siedowse	p = prefp = NULL;
268274462Salfred	if ((opt_flags & OP_MASKLEN) && !maskflg) {
268374462Salfred		p = strchr(cp, '/');
268474462Salfred		*p = '\0';
268574462Salfred		prefp = p + 1;
268674462Salfred	}
268774462Salfred
268875861Siedowse	/*
268975861Siedowse	 * Check for a numeric address first. We wish to avoid
269075861Siedowse	 * possible DNS lookups in getnetbyname().
269175861Siedowse	 */
269275861Siedowse	if (isxdigit(*cp) || *cp == ':') {
269374462Salfred		memset(&hints, 0, sizeof hints);
269475801Siedowse		/* Ensure the mask and the network have the same family. */
269575801Siedowse		if (maskflg && (opt_flags & OP_NET))
269675801Siedowse			hints.ai_family = net->nt_net.ss_family;
269775801Siedowse		else if (!maskflg && (opt_flags & OP_HAVEMASK))
269875801Siedowse			hints.ai_family = net->nt_mask.ss_family;
269975801Siedowse		else
270075801Siedowse			hints.ai_family = AF_UNSPEC;
270174462Salfred		hints.ai_flags = AI_NUMERICHOST;
270275861Siedowse		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
270375861Siedowse			sa = ai->ai_addr;
270475861Siedowse		if (sa != NULL && ai->ai_family == AF_INET) {
270574462Salfred			/*
270675801Siedowse			 * The address in `cp' is really a network address, so
270775801Siedowse			 * use inet_network() to re-interpret this correctly.
270875801Siedowse			 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
270974462Salfred			 */
271075801Siedowse			bzero(&sin, sizeof sin);
271174462Salfred			sin.sin_family = AF_INET;
271274462Salfred			sin.sin_len = sizeof sin;
271375801Siedowse			sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
271474462Salfred			if (debug)
271575801Siedowse				fprintf(stderr, "get_net: v4 addr %s\n",
271675801Siedowse				    inet_ntoa(sin.sin_addr));
271774462Salfred			sa = (struct sockaddr *)&sin;
271875861Siedowse		}
271975861Siedowse	}
272075861Siedowse	if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
272175861Siedowse		bzero(&sin, sizeof sin);
272275861Siedowse		sin.sin_family = AF_INET;
272375861Siedowse		sin.sin_len = sizeof sin;
272475861Siedowse		sin.sin_addr = inet_makeaddr(np->n_net, 0);
272575861Siedowse		sa = (struct sockaddr *)&sin;
272675861Siedowse	}
272775861Siedowse	if (sa == NULL)
272874462Salfred		goto fail;
272925318Spst
273075801Siedowse	if (maskflg) {
273175801Siedowse		/* The specified sockaddr is a mask. */
273275801Siedowse		if (checkmask(sa) != 0)
273375801Siedowse			goto fail;
273475801Siedowse		bcopy(sa, &net->nt_mask, sa->sa_len);
273575801Siedowse		opt_flags |= OP_HAVEMASK;
273675801Siedowse	} else {
273775801Siedowse		/* The specified sockaddr is a network address. */
273875801Siedowse		bcopy(sa, &net->nt_net, sa->sa_len);
273974462Salfred
274075801Siedowse		/* Get a network name for the export list. */
274175801Siedowse		if (np) {
274275801Siedowse			name = np->n_name;
274375801Siedowse		} else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2744146187Sume		   NULL, 0, NI_NUMERICHOST) == 0) {
274575801Siedowse			name = netname;
274675801Siedowse		} else {
274775801Siedowse			goto fail;
274875801Siedowse		}
274975801Siedowse		if ((net->nt_name = strdup(name)) == NULL)
275075801Siedowse			out_of_mem();
275175801Siedowse
275275801Siedowse		/*
275375801Siedowse		 * Extract a mask from either a "/<masklen>" suffix, or
275475801Siedowse		 * from the class of an IPv4 address.
275575801Siedowse		 */
275674462Salfred		if (opt_flags & OP_MASKLEN) {
275774462Salfred			preflen = strtol(prefp, NULL, 10);
275875801Siedowse			if (preflen < 0L || preflen == LONG_MAX)
275974462Salfred				goto fail;
276075801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
276175801Siedowse			if (makemask(&net->nt_mask, (int)preflen) != 0)
276275801Siedowse				goto fail;
276375801Siedowse			opt_flags |= OP_HAVEMASK;
276474462Salfred			*p = '/';
276575801Siedowse		} else if (sa->sa_family == AF_INET &&
276675801Siedowse		    (opt_flags & OP_MASK) == 0) {
276775801Siedowse			in_addr_t addr;
276874462Salfred
276975801Siedowse			addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
277075801Siedowse			if (IN_CLASSA(addr))
277175801Siedowse				preflen = 8;
277275801Siedowse			else if (IN_CLASSB(addr))
277375801Siedowse				preflen = 16;
277475801Siedowse			else if (IN_CLASSC(addr))
277575801Siedowse				preflen = 24;
277675801Siedowse			else if (IN_CLASSD(addr))
277775801Siedowse				preflen = 28;
277875801Siedowse			else
277975801Siedowse				preflen = 32;	/* XXX */
278075801Siedowse
278175801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
278275801Siedowse			makemask(&net->nt_mask, (int)preflen);
278375801Siedowse			opt_flags |= OP_HAVEMASK;
278474462Salfred		}
278574462Salfred	}
278674462Salfred
278774462Salfred	if (ai)
278874462Salfred		freeaddrinfo(ai);
278974462Salfred	return 0;
279074462Salfred
279174462Salfredfail:
279274462Salfred	if (ai)
279374462Salfred		freeaddrinfo(ai);
279474462Salfred	return 1;
27951558Srgrimes}
27961558Srgrimes
27971558Srgrimes/*
27981558Srgrimes * Parse out the next white space separated field
27991558Srgrimes */
2800285128Straszstatic void
2801216587Scharniernextfield(char **cp, char **endcp)
28021558Srgrimes{
28031558Srgrimes	char *p;
28041558Srgrimes
28051558Srgrimes	p = *cp;
28061558Srgrimes	while (*p == ' ' || *p == '\t')
28071558Srgrimes		p++;
28081558Srgrimes	if (*p == '\n' || *p == '\0')
28091558Srgrimes		*cp = *endcp = p;
28101558Srgrimes	else {
28111558Srgrimes		*cp = p++;
28121558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
28131558Srgrimes			p++;
28141558Srgrimes		*endcp = p;
28151558Srgrimes	}
28161558Srgrimes}
28171558Srgrimes
28181558Srgrimes/*
28191558Srgrimes * Get an exports file line. Skip over blank lines and handle line
28201558Srgrimes * continuations.
28211558Srgrimes */
2822285128Straszstatic int
2823216587Scharnierget_line(void)
28241558Srgrimes{
28251558Srgrimes	char *p, *cp;
282696622Siedowse	size_t len;
28271558Srgrimes	int totlen, cont_line;
28281558Srgrimes
28291558Srgrimes	/*
28301558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
28311558Srgrimes	 */
28321558Srgrimes	p = line;
28331558Srgrimes	totlen = 0;
28341558Srgrimes	do {
283596622Siedowse		if ((p = fgetln(exp_file, &len)) == NULL)
28361558Srgrimes			return (0);
28371558Srgrimes		cp = p + len - 1;
28381558Srgrimes		cont_line = 0;
28391558Srgrimes		while (cp >= p &&
28401558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
28411558Srgrimes			if (*cp == '\\')
28421558Srgrimes				cont_line = 1;
28431558Srgrimes			cp--;
28441558Srgrimes			len--;
28451558Srgrimes		}
284679117Sdd		if (cont_line) {
284779117Sdd			*++cp = ' ';
284879117Sdd			len++;
284979117Sdd		}
285096622Siedowse		if (linesize < len + totlen + 1) {
285196622Siedowse			linesize = len + totlen + 1;
285296622Siedowse			line = realloc(line, linesize);
285396622Siedowse			if (line == NULL)
285496622Siedowse				out_of_mem();
28551558Srgrimes		}
285696622Siedowse		memcpy(line + totlen, p, len);
285796622Siedowse		totlen += len;
285896622Siedowse		line[totlen] = '\0';
28591558Srgrimes	} while (totlen == 0 || cont_line);
28601558Srgrimes	return (1);
28611558Srgrimes}
28621558Srgrimes
28631558Srgrimes/*
28641558Srgrimes * Parse a description of a credential.
28651558Srgrimes */
2866285128Straszstatic void
2867216587Scharnierparsecred(char *namelist, struct xucred *cr)
28681558Srgrimes{
28691558Srgrimes	char *name;
28701558Srgrimes	int cnt;
28711558Srgrimes	char *names;
28721558Srgrimes	struct passwd *pw;
28731558Srgrimes	struct group *gr;
2874194498Sbrooks	gid_t groups[XU_NGROUPS + 1];
2875136051Sstefanf	int ngroups;
28761558Srgrimes
287791354Sdd	cr->cr_version = XUCRED_VERSION;
28781558Srgrimes	/*
287937663Scharnier	 * Set up the unprivileged user.
28801558Srgrimes	 */
28811558Srgrimes	cr->cr_uid = -2;
28821558Srgrimes	cr->cr_groups[0] = -2;
28831558Srgrimes	cr->cr_ngroups = 1;
28841558Srgrimes	/*
28851558Srgrimes	 * Get the user's password table entry.
28861558Srgrimes	 */
2887293305Sjpaetzel	names = strsep_quote(&namelist, " \t\n");
28881558Srgrimes	name = strsep(&names, ":");
2889293305Sjpaetzel	/* Bug?  name could be NULL here */
28901558Srgrimes	if (isdigit(*name) || *name == '-')
28911558Srgrimes		pw = getpwuid(atoi(name));
28921558Srgrimes	else
28931558Srgrimes		pw = getpwnam(name);
28941558Srgrimes	/*
28951558Srgrimes	 * Credentials specified as those of a user.
28961558Srgrimes	 */
28971558Srgrimes	if (names == NULL) {
28981558Srgrimes		if (pw == NULL) {
289937663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
29001558Srgrimes			return;
29011558Srgrimes		}
29021558Srgrimes		cr->cr_uid = pw->pw_uid;
2903194498Sbrooks		ngroups = XU_NGROUPS + 1;
29041558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
290537663Scharnier			syslog(LOG_ERR, "too many groups");
29061558Srgrimes		/*
2907136051Sstefanf		 * Compress out duplicate.
29081558Srgrimes		 */
29091558Srgrimes		cr->cr_ngroups = ngroups - 1;
29101558Srgrimes		cr->cr_groups[0] = groups[0];
29111558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
29121558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
29131558Srgrimes		return;
29141558Srgrimes	}
29151558Srgrimes	/*
29161558Srgrimes	 * Explicit credential specified as a colon separated list:
29171558Srgrimes	 *	uid:gid:gid:...
29181558Srgrimes	 */
29191558Srgrimes	if (pw != NULL)
29201558Srgrimes		cr->cr_uid = pw->pw_uid;
29211558Srgrimes	else if (isdigit(*name) || *name == '-')
29221558Srgrimes		cr->cr_uid = atoi(name);
29231558Srgrimes	else {
292437663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
29251558Srgrimes		return;
29261558Srgrimes	}
29271558Srgrimes	cr->cr_ngroups = 0;
2928194498Sbrooks	while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
29291558Srgrimes		name = strsep(&names, ":");
29301558Srgrimes		if (isdigit(*name) || *name == '-') {
29311558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
29321558Srgrimes		} else {
29331558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
293437663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
29351558Srgrimes				continue;
29361558Srgrimes			}
29371558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
29381558Srgrimes		}
29391558Srgrimes	}
2940194498Sbrooks	if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
294137663Scharnier		syslog(LOG_ERR, "too many groups");
29421558Srgrimes}
29431558Srgrimes
2944194880Sdfr#define	STRSIZ	(MNTNAMLEN+MNTPATHLEN+50)
29451558Srgrimes/*
29461558Srgrimes * Routines that maintain the remote mounttab
29471558Srgrimes */
2948285128Straszstatic void
2949216587Scharnierget_mountlist(void)
29501558Srgrimes{
2951324955Smanu	struct mountlist *mlp;
295223681Speter	char *host, *dirp, *cp;
29531558Srgrimes	char str[STRSIZ];
29541558Srgrimes	FILE *mlfile;
29551558Srgrimes
29561558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
295753117Sbillf		if (errno == ENOENT)
295853117Sbillf			return;
295953117Sbillf		else {
296053117Sbillf			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
296153117Sbillf			return;
296253117Sbillf		}
29631558Srgrimes	}
29641558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
296523681Speter		cp = str;
296623681Speter		host = strsep(&cp, " \t\n");
296723681Speter		dirp = strsep(&cp, " \t\n");
296823681Speter		if (host == NULL || dirp == NULL)
29691558Srgrimes			continue;
29701558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
297137663Scharnier		if (mlp == (struct mountlist *)NULL)
297237663Scharnier			out_of_mem();
2973194880Sdfr		strncpy(mlp->ml_host, host, MNTNAMLEN);
2974194880Sdfr		mlp->ml_host[MNTNAMLEN] = '\0';
2975194880Sdfr		strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2976194880Sdfr		mlp->ml_dirp[MNTPATHLEN] = '\0';
2977324955Smanu
2978324955Smanu		SLIST_INSERT_HEAD(&mlhead, mlp, next);
29791558Srgrimes	}
29801558Srgrimes	fclose(mlfile);
29811558Srgrimes}
29821558Srgrimes
2983285128Straszstatic void
298475635Siedowsedel_mlist(char *hostp, char *dirp)
29851558Srgrimes{
2986324955Smanu	struct mountlist *mlp, *mlp2;
29871558Srgrimes	FILE *mlfile;
29881558Srgrimes	int fnd = 0;
29891558Srgrimes
2990324955Smanu	SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
29911558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
29921558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
29931558Srgrimes			fnd = 1;
2994324955Smanu			SLIST_REMOVE(&mlhead, mlp, mountlist, next);
2995324955Smanu			free((caddr_t)mlp);
29961558Srgrimes		}
29971558Srgrimes	}
29981558Srgrimes	if (fnd) {
29991558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
300037663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
30011558Srgrimes			return;
30021558Srgrimes		}
3003324955Smanu		SLIST_FOREACH(mlp, &mlhead, next) {
30041558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
30051558Srgrimes		}
30061558Srgrimes		fclose(mlfile);
30071558Srgrimes	}
30081558Srgrimes}
30091558Srgrimes
3010285128Straszstatic void
3011216587Scharnieradd_mlist(char *hostp, char *dirp)
30121558Srgrimes{
3013324955Smanu	struct mountlist *mlp;
30141558Srgrimes	FILE *mlfile;
30151558Srgrimes
3016324955Smanu	SLIST_FOREACH(mlp, &mlhead, next) {
30171558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
30181558Srgrimes			return;
30191558Srgrimes	}
3020324955Smanu
30211558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
302237663Scharnier	if (mlp == (struct mountlist *)NULL)
302337663Scharnier		out_of_mem();
3024194880Sdfr	strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3025194880Sdfr	mlp->ml_host[MNTNAMLEN] = '\0';
3026194880Sdfr	strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3027194880Sdfr	mlp->ml_dirp[MNTPATHLEN] = '\0';
3028324955Smanu	SLIST_INSERT_HEAD(&mlhead, mlp, next);
30291558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
303037663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
30311558Srgrimes		return;
30321558Srgrimes	}
30331558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
30341558Srgrimes	fclose(mlfile);
30351558Srgrimes}
30361558Srgrimes
30371558Srgrimes/*
30381558Srgrimes * Free up a group list.
30391558Srgrimes */
3040285128Straszstatic void
3041216587Scharnierfree_grp(struct grouplist *grp)
30421558Srgrimes{
30431558Srgrimes	if (grp->gr_type == GT_HOST) {
304474462Salfred		if (grp->gr_ptr.gt_addrinfo != NULL)
304574462Salfred			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
30461558Srgrimes	} else if (grp->gr_type == GT_NET) {
30471558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
30481558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
30491558Srgrimes	}
30501558Srgrimes	free((caddr_t)grp);
30511558Srgrimes}
30521558Srgrimes
30531558Srgrimes#ifdef DEBUG
3054285128Straszstatic void
30551558SrgrimesSYSLOG(int pri, const char *fmt, ...)
30561558Srgrimes{
30571558Srgrimes	va_list ap;
30581558Srgrimes
30591558Srgrimes	va_start(ap, fmt);
30601558Srgrimes	vfprintf(stderr, fmt, ap);
30611558Srgrimes	va_end(ap);
30621558Srgrimes}
30631558Srgrimes#endif /* DEBUG */
30641558Srgrimes
30651558Srgrimes/*
30661558Srgrimes * Check options for consistency.
30671558Srgrimes */
3068285128Straszstatic int
3069216587Scharniercheck_options(struct dirlist *dp)
30701558Srgrimes{
30711558Srgrimes
3072192934Srmacklem	if (v4root_phase == 0 && dp == NULL)
30731558Srgrimes	    return (1);
307483653Speter	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
307583653Speter	    syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
30761558Srgrimes	    return (1);
30771558Srgrimes	}
30781558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
307975801Siedowse		syslog(LOG_ERR, "-mask requires -network");
308075801Siedowse		return (1);
30811558Srgrimes	}
308275801Siedowse	if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
308375801Siedowse		syslog(LOG_ERR, "-network requires mask specification");
308475801Siedowse		return (1);
308575801Siedowse	}
308675801Siedowse	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
308775801Siedowse		syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
308875801Siedowse		return (1);
308975801Siedowse	}
3090192934Srmacklem	if (v4root_phase > 0 &&
3091192934Srmacklem	    (opt_flags &
3092192934Srmacklem	     ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3093192934Srmacklem	    syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3094192934Srmacklem	    return (1);
3095192934Srmacklem	}
3096207689Srmacklem	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3097207689Srmacklem	    syslog(LOG_ERR, "-alldirs has multiple directories");
3098207689Srmacklem	    return (1);
3099207689Srmacklem	}
31001558Srgrimes	return (0);
31011558Srgrimes}
31021558Srgrimes
31031558Srgrimes/*
31041558Srgrimes * Check an absolute directory path for any symbolic links. Return true
31051558Srgrimes */
3106285128Straszstatic int
3107216587Scharniercheck_dirpath(char *dirp)
31081558Srgrimes{
31091558Srgrimes	char *cp;
31101558Srgrimes	int ret = 1;
31111558Srgrimes	struct stat sb;
31121558Srgrimes
31131558Srgrimes	cp = dirp + 1;
31141558Srgrimes	while (*cp && ret) {
31151558Srgrimes		if (*cp == '/') {
31161558Srgrimes			*cp = '\0';
31179336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
31181558Srgrimes				ret = 0;
31191558Srgrimes			*cp = '/';
31201558Srgrimes		}
31211558Srgrimes		cp++;
31221558Srgrimes	}
31239336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
31241558Srgrimes		ret = 0;
31251558Srgrimes	return (ret);
31261558Srgrimes}
31279336Sdfr
312875801Siedowse/*
312975801Siedowse * Make a netmask according to the specified prefix length. The ss_family
313075801Siedowse * and other non-address fields must be initialised before calling this.
313175801Siedowse */
3132285128Straszstatic int
313375801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen)
313474462Salfred{
313575801Siedowse	u_char *p;
313675801Siedowse	int bits, i, len;
313774462Salfred
313875801Siedowse	if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
313975801Siedowse		return (-1);
3140103949Smike	if (bitlen > len * CHAR_BIT)
314175801Siedowse		return (-1);
314274462Salfred
314375801Siedowse	for (i = 0; i < len; i++) {
3144298912Saraujo		bits = MIN(CHAR_BIT, bitlen);
3145219125Sru		*p++ = (u_char)~0 << (CHAR_BIT - bits);
314675801Siedowse		bitlen -= bits;
314774462Salfred	}
314875801Siedowse	return 0;
314974462Salfred}
315074462Salfred
315175801Siedowse/*
315275801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask
315375801Siedowse * is acceptable (i.e. of the form 1...10....0).
315475801Siedowse */
3155285128Straszstatic int
315675801Siedowsecheckmask(struct sockaddr *sa)
315774462Salfred{
315875801Siedowse	u_char *mask;
315975801Siedowse	int i, len;
316074462Salfred
316175801Siedowse	if ((mask = sa_rawaddr(sa, &len)) == NULL)
316275801Siedowse		return (-1);
316375801Siedowse
316475801Siedowse	for (i = 0; i < len; i++)
316575801Siedowse		if (mask[i] != 0xff)
316675801Siedowse			break;
316775801Siedowse	if (i < len) {
316875801Siedowse		if (~mask[i] & (u_char)(~mask[i] + 1))
316975801Siedowse			return (-1);
317075801Siedowse		i++;
317174462Salfred	}
317275801Siedowse	for (; i < len; i++)
317375801Siedowse		if (mask[i] != 0)
317475801Siedowse			return (-1);
317575801Siedowse	return (0);
317674462Salfred}
317774462Salfred
317875801Siedowse/*
317975801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if
318075801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3181228990Suqs * If samask is NULL, perform a full comparison.
318275801Siedowse */
3183285128Straszstatic int
318475801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
318574462Salfred{
318675801Siedowse	unsigned char *p1, *p2, *mask;
318775801Siedowse	int len, i;
318874462Salfred
318975801Siedowse	if (sa1->sa_family != sa2->sa_family ||
319075801Siedowse	    (p1 = sa_rawaddr(sa1, &len)) == NULL ||
319175801Siedowse	    (p2 = sa_rawaddr(sa2, NULL)) == NULL)
319275801Siedowse		return (1);
319375801Siedowse
319475801Siedowse	switch (sa1->sa_family) {
319574462Salfred	case AF_INET6:
319675801Siedowse		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
319775801Siedowse		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
319875801Siedowse			return (1);
319974462Salfred		break;
320074462Salfred	}
320174462Salfred
320275801Siedowse	/* Simple binary comparison if no mask specified. */
320375801Siedowse	if (samask == NULL)
320475801Siedowse		return (memcmp(p1, p2, len));
320574462Salfred
320675801Siedowse	/* Set up the mask, and do a mask-based comparison. */
320775801Siedowse	if (sa1->sa_family != samask->sa_family ||
320875801Siedowse	    (mask = sa_rawaddr(samask, NULL)) == NULL)
320975801Siedowse		return (1);
321074462Salfred
321175801Siedowse	for (i = 0; i < len; i++)
321275801Siedowse		if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
321375801Siedowse			return (1);
321475801Siedowse	return (0);
321574462Salfred}
321674462Salfred
321775801Siedowse/*
321875801Siedowse * Return a pointer to the part of the sockaddr that contains the
321975801Siedowse * raw address, and set *nbytes to its length in bytes. Returns
322075801Siedowse * NULL if the address family is unknown.
322175801Siedowse */
3222285128Straszstatic void *
322375801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) {
322475801Siedowse	void *p;
322574462Salfred	int len;
322674462Salfred
322775801Siedowse	switch (sa->sa_family) {
322874462Salfred	case AF_INET:
322975801Siedowse		len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
323075801Siedowse		p = &((struct sockaddr_in *)sa)->sin_addr;
323174462Salfred		break;
323274462Salfred	case AF_INET6:
323375801Siedowse		len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
323475801Siedowse		p = &((struct sockaddr_in6 *)sa)->sin6_addr;
323574462Salfred		break;
323674462Salfred	default:
323775801Siedowse		p = NULL;
323875801Siedowse		len = 0;
323974462Salfred	}
324074462Salfred
324175801Siedowse	if (nbytes != NULL)
324275801Siedowse		*nbytes = len;
324375801Siedowse	return (p);
324474462Salfred}
324574462Salfred
3246285128Straszstatic void
3247216587Scharnierhuphandler(int sig __unused)
324875754Siedowse{
3249285128Strasz
325075754Siedowse	got_sighup = 1;
325175754Siedowse}
325275754Siedowse
3253285128Straszstatic void
3254285128Straszterminate(int sig __unused)
325574462Salfred{
3256149433Spjd	pidfile_remove(pfh);
3257194880Sdfr	rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3258194880Sdfr	rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
325974462Salfred	exit (0);
326074462Salfred}
3261