mountd.c revision 333197
1331722Seadler/*
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 333197 2018-05-03 07:28:04Z avg $");
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 ||
10541558Srgrimes		    statfs(dirpath, &fsb) < 0) {
10551558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
105631656Sguido			syslog(LOG_NOTICE,
105737663Scharnier			    "mount request from %s for non existent path %s",
105874462Salfred			    numerichost, dirpath);
10591558Srgrimes			if (debug)
106037663Scharnier				warnx("stat failed on %s", dirpath);
106128911Sguido			bad = ENOENT;	/* We will send error reply later */
10621558Srgrimes		}
1063330092Srpokala		if (!bad &&
1064330092Srpokala		    !S_ISDIR(stb.st_mode) &&
1065330092Srpokala		    (dir_only || !S_ISREG(stb.st_mode))) {
1066330092Srpokala			syslog(LOG_NOTICE,
1067330092Srpokala			    "mount request from %s for non-directory path %s",
1068330092Srpokala			    numerichost, dirpath);
1069330092Srpokala			if (debug)
1070330092Srpokala				warnx("mounting non-directory %s", dirpath);
1071330092Srpokala			bad = ENOTDIR;	/* We will send error reply later */
1072330092Srpokala		}
10731558Srgrimes
10741558Srgrimes		/* Check in the exports list */
10759336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1076330092Srpokala		if (bad)
1077330092Srpokala			ep = NULL;
1078330092Srpokala		else
1079330092Srpokala			ep = ex_search(&fsb.f_fsid);
10809336Sdfr		hostset = defset = 0;
1081240902Srmacklem		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1082240902Srmacklem		    &numsecflavors, &secflavorsp) ||
10831558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1084240902Srmacklem		      chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1085240902Srmacklem		       &secflavorsp)) ||
108674462Salfred		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
108774462Salfred		     scan_tree(ep->ex_dirl, saddr) == 0))) {
108828911Sguido			if (bad) {
1089121556Speter				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
109028911Sguido				    (caddr_t)&bad))
109137663Scharnier					syslog(LOG_ERR, "can't send reply");
109228911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
109328911Sguido				return;
109428911Sguido			}
1095240902Srmacklem			if (hostset & DP_HOSTSET) {
10969336Sdfr				fhr.fhr_flag = hostset;
1097240902Srmacklem				fhr.fhr_numsecflavors = numsecflavors;
1098240902Srmacklem				fhr.fhr_secflavors = secflavorsp;
1099240902Srmacklem			} else {
11009336Sdfr				fhr.fhr_flag = defset;
1101240902Srmacklem				fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1102240902Srmacklem				fhr.fhr_secflavors = ep->ex_defsecflavors;
1103240902Srmacklem			}
11049336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
11051558Srgrimes			/* Get the file handle */
110623681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
11079336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
11081558Srgrimes				bad = errno;
110937663Scharnier				syslog(LOG_ERR, "can't get fh for %s", dirpath);
1110121556Speter				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
11111558Srgrimes				    (caddr_t)&bad))
111237663Scharnier					syslog(LOG_ERR, "can't send reply");
11139336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
11141558Srgrimes				return;
11151558Srgrimes			}
1116121556Speter			if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1117121556Speter			    (caddr_t)&fhr))
111837663Scharnier				syslog(LOG_ERR, "can't send reply");
111974462Salfred			if (!lookup_failed)
112074462Salfred				add_mlist(host, dirpath);
11211558Srgrimes			else
112274462Salfred				add_mlist(numerichost, dirpath);
11231558Srgrimes			if (debug)
112437663Scharnier				warnx("mount successful");
1125121767Speter			if (dolog)
112631656Sguido				syslog(LOG_NOTICE,
112731656Sguido				    "mount request succeeded from %s for %s",
112874462Salfred				    numerichost, dirpath);
112931656Sguido		} else {
1130330092Srpokala			if (!bad)
1131330092Srpokala				bad = EACCES;
113231656Sguido			syslog(LOG_NOTICE,
113331656Sguido			    "mount request denied from %s for %s",
113474462Salfred			    numerichost, dirpath);
113531656Sguido		}
113628911Sguido
1137121556Speter		if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1138121556Speter		    (caddr_t)&bad))
113937663Scharnier			syslog(LOG_ERR, "can't send reply");
11409336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
11411558Srgrimes		return;
1142194880Sdfr	case MOUNTPROC_DUMP:
1143121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
114437663Scharnier			syslog(LOG_ERR, "can't send reply");
1145121767Speter		else if (dolog)
114631656Sguido			syslog(LOG_NOTICE,
114731656Sguido			    "dump request succeeded from %s",
114874462Salfred			    numerichost);
11491558Srgrimes		return;
1150194880Sdfr	case MOUNTPROC_UMNT:
11519336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
115231656Sguido			syslog(LOG_NOTICE,
115331656Sguido			    "umount request from %s from unprivileged port",
115474462Salfred			    numerichost);
11551558Srgrimes			svcerr_weakauth(transp);
11561558Srgrimes			return;
11571558Srgrimes		}
1158121556Speter		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
115931656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
116074462Salfred			    numerichost);
11611558Srgrimes			svcerr_decode(transp);
11621558Srgrimes			return;
11631558Srgrimes		}
116451968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
116551968Salfred			syslog(LOG_NOTICE, "umount request from %s "
116651968Salfred			    "for non existent path %s",
116774462Salfred			    numerichost, dirpath);
116851968Salfred		}
1169121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
117037663Scharnier			syslog(LOG_ERR, "can't send reply");
117174462Salfred		if (!lookup_failed)
117275635Siedowse			del_mlist(host, dirpath);
117375635Siedowse		del_mlist(numerichost, dirpath);
1174121767Speter		if (dolog)
117531656Sguido			syslog(LOG_NOTICE,
117631656Sguido			    "umount request succeeded from %s for %s",
117774462Salfred			    numerichost, dirpath);
11781558Srgrimes		return;
1179194880Sdfr	case MOUNTPROC_UMNTALL:
11809336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
118131656Sguido			syslog(LOG_NOTICE,
118231656Sguido			    "umountall request from %s from unprivileged port",
118374462Salfred			    numerichost);
11841558Srgrimes			svcerr_weakauth(transp);
11851558Srgrimes			return;
11861558Srgrimes		}
1187121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
118837663Scharnier			syslog(LOG_ERR, "can't send reply");
118974462Salfred		if (!lookup_failed)
119075635Siedowse			del_mlist(host, NULL);
119175635Siedowse		del_mlist(numerichost, NULL);
1192121767Speter		if (dolog)
119331656Sguido			syslog(LOG_NOTICE,
119431656Sguido			    "umountall request succeeded from %s",
119574462Salfred			    numerichost);
11961558Srgrimes		return;
1197194880Sdfr	case MOUNTPROC_EXPORT:
1198121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1199121556Speter			if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1200121556Speter			    (caddr_t)NULL))
1201100117Salfred				syslog(LOG_ERR, "can't send reply");
1202121767Speter		if (dolog)
120331656Sguido			syslog(LOG_NOTICE,
120431656Sguido			    "export request succeeded from %s",
120574462Salfred			    numerichost);
12061558Srgrimes		return;
12071558Srgrimes	default:
12081558Srgrimes		svcerr_noproc(transp);
12091558Srgrimes		return;
12101558Srgrimes	}
12111558Srgrimes}
12121558Srgrimes
12131558Srgrimes/*
12141558Srgrimes * Xdr conversion for a dirpath string
12151558Srgrimes */
1216285128Straszstatic int
1217216587Scharnierxdr_dir(XDR *xdrsp, char *dirp)
12181558Srgrimes{
1219194880Sdfr	return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
12201558Srgrimes}
12211558Srgrimes
12221558Srgrimes/*
12239336Sdfr * Xdr routine to generate file handle reply
12241558Srgrimes */
1225285128Straszstatic int
1226216587Scharnierxdr_fhs(XDR *xdrsp, caddr_t cp)
12271558Srgrimes{
122892806Sobrien	struct fhreturn *fhrp = (struct fhreturn *)cp;
12299336Sdfr	u_long ok = 0, len, auth;
1230184588Sdfr	int i;
12311558Srgrimes
12321558Srgrimes	if (!xdr_long(xdrsp, &ok))
12331558Srgrimes		return (0);
12349336Sdfr	switch (fhrp->fhr_vers) {
12359336Sdfr	case 1:
12369336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
12379336Sdfr	case 3:
12389336Sdfr		len = NFSX_V3FH;
12399336Sdfr		if (!xdr_long(xdrsp, &len))
12409336Sdfr			return (0);
12419336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
12429336Sdfr			return (0);
1243184588Sdfr		if (fhrp->fhr_numsecflavors) {
1244184588Sdfr			if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1245184588Sdfr				return (0);
1246184588Sdfr			for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1247184588Sdfr				if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1248184588Sdfr					return (0);
1249184588Sdfr			return (1);
1250184588Sdfr		} else {
1251184588Sdfr			auth = AUTH_SYS;
1252184588Sdfr			len = 1;
1253184588Sdfr			if (!xdr_long(xdrsp, &len))
1254184588Sdfr				return (0);
1255184588Sdfr			return (xdr_long(xdrsp, &auth));
1256184588Sdfr		}
1257298089Spfg	}
12589336Sdfr	return (0);
12591558Srgrimes}
12601558Srgrimes
1261285128Straszstatic int
1262216587Scharnierxdr_mlist(XDR *xdrsp, caddr_t cp __unused)
12631558Srgrimes{
12641558Srgrimes	struct mountlist *mlp;
12651558Srgrimes	int true = 1;
12661558Srgrimes	int false = 0;
12671558Srgrimes	char *strp;
12681558Srgrimes
1269324955Smanu	SLIST_FOREACH(mlp, &mlhead, next) {
12701558Srgrimes		if (!xdr_bool(xdrsp, &true))
12711558Srgrimes			return (0);
12721558Srgrimes		strp = &mlp->ml_host[0];
1273194880Sdfr		if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
12741558Srgrimes			return (0);
12751558Srgrimes		strp = &mlp->ml_dirp[0];
1276194880Sdfr		if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
12771558Srgrimes			return (0);
12781558Srgrimes	}
12791558Srgrimes	if (!xdr_bool(xdrsp, &false))
12801558Srgrimes		return (0);
12811558Srgrimes	return (1);
12821558Srgrimes}
12831558Srgrimes
12841558Srgrimes/*
12851558Srgrimes * Xdr conversion for export list
12861558Srgrimes */
1287285128Straszstatic int
1288216587Scharnierxdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
12891558Srgrimes{
12901558Srgrimes	struct exportlist *ep;
12911558Srgrimes	int false = 0;
12929336Sdfr	int putdef;
12939336Sdfr	sigset_t sighup_mask;
12941558Srgrimes
12959336Sdfr	sigemptyset(&sighup_mask);
12969336Sdfr	sigaddset(&sighup_mask, SIGHUP);
12979336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1298324955Smanu
1299324955Smanu	SLIST_FOREACH(ep, &exphead, entries) {
13001558Srgrimes		putdef = 0;
1301100117Salfred		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1302100117Salfred			       &putdef, brief))
13031558Srgrimes			goto errout;
13041558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
13051558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1306100117Salfred			&putdef, brief))
13071558Srgrimes			goto errout;
13081558Srgrimes	}
13099336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
13101558Srgrimes	if (!xdr_bool(xdrsp, &false))
13111558Srgrimes		return (0);
13121558Srgrimes	return (1);
13131558Srgrimeserrout:
13149336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
13151558Srgrimes	return (0);
13161558Srgrimes}
13171558Srgrimes
13181558Srgrimes/*
13191558Srgrimes * Called from xdr_explist() to traverse the tree and export the
13201558Srgrimes * directory paths.
13211558Srgrimes */
1322285128Straszstatic int
1323216587Scharnierput_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1324216587Scharnier	int brief)
13251558Srgrimes{
13261558Srgrimes	struct grouplist *grp;
13271558Srgrimes	struct hostlist *hp;
13281558Srgrimes	int true = 1;
13291558Srgrimes	int false = 0;
13301558Srgrimes	int gotalldir = 0;
13311558Srgrimes	char *strp;
13321558Srgrimes
13331558Srgrimes	if (dp) {
1334100117Salfred		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
13351558Srgrimes			return (1);
13361558Srgrimes		if (!xdr_bool(xdrsp, &true))
13371558Srgrimes			return (1);
13381558Srgrimes		strp = dp->dp_dirp;
1339194880Sdfr		if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
13401558Srgrimes			return (1);
13411558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
13421558Srgrimes			gotalldir = 1;
13431558Srgrimes			*putdefp = 1;
13441558Srgrimes		}
1345100117Salfred		if (brief) {
1346100117Salfred			if (!xdr_bool(xdrsp, &true))
1347100117Salfred				return (1);
1348100117Salfred			strp = "(...)";
1349194880Sdfr			if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1350100117Salfred				return (1);
1351100117Salfred		} else if ((dp->dp_flag & DP_DEFSET) == 0 &&
13521558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
13531558Srgrimes			hp = dp->dp_hosts;
13541558Srgrimes			while (hp) {
13551558Srgrimes				grp = hp->ht_grp;
13561558Srgrimes				if (grp->gr_type == GT_HOST) {
13571558Srgrimes					if (!xdr_bool(xdrsp, &true))
13581558Srgrimes						return (1);
135974462Salfred					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
13608871Srgrimes					if (!xdr_string(xdrsp, &strp,
1361194880Sdfr					    MNTNAMLEN))
13621558Srgrimes						return (1);
13631558Srgrimes				} else if (grp->gr_type == GT_NET) {
13641558Srgrimes					if (!xdr_bool(xdrsp, &true))
13651558Srgrimes						return (1);
13661558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
13678871Srgrimes					if (!xdr_string(xdrsp, &strp,
1368194880Sdfr					    MNTNAMLEN))
13691558Srgrimes						return (1);
13701558Srgrimes				}
13711558Srgrimes				hp = hp->ht_next;
13721558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
13731558Srgrimes					hp = adp->dp_hosts;
13741558Srgrimes					gotalldir = 0;
13751558Srgrimes				}
13761558Srgrimes			}
13771558Srgrimes		}
13781558Srgrimes		if (!xdr_bool(xdrsp, &false))
13791558Srgrimes			return (1);
1380100117Salfred		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
13811558Srgrimes			return (1);
13821558Srgrimes	}
13831558Srgrimes	return (0);
13841558Srgrimes}
13851558Srgrimes
1386285128Straszstatic int
1387216587Scharnierxdr_explist(XDR *xdrsp, caddr_t cp)
1388100117Salfred{
1389100117Salfred
1390100117Salfred	return xdr_explist_common(xdrsp, cp, 0);
1391100117Salfred}
1392100117Salfred
1393285128Straszstatic int
1394216587Scharnierxdr_explist_brief(XDR *xdrsp, caddr_t cp)
1395100117Salfred{
1396100117Salfred
1397100117Salfred	return xdr_explist_common(xdrsp, cp, 1);
1398100117Salfred}
1399100117Salfred
1400285128Straszstatic char *line;
1401285128Straszstatic size_t linesize;
1402285128Straszstatic FILE *exp_file;
14031558Srgrimes
14041558Srgrimes/*
1405166440Spjd * Get the export list from one, currently open file
14061558Srgrimes */
1407166440Spjdstatic void
1408216587Scharnierget_exportlist_one(void)
14091558Srgrimes{
1410324955Smanu	struct exportlist *ep;
14111558Srgrimes	struct grouplist *grp, *tgrp;
14121558Srgrimes	struct dirlist *dirhead;
1413166440Spjd	struct statfs fsb;
141472650Sgreen	struct xucred anon;
14151558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1416166440Spjd	int len, has_host, exflags, got_nondir, dirplen, netgrp;
14171558Srgrimes
1418192934Srmacklem	v4root_phase = 0;
14191558Srgrimes	dirhead = (struct dirlist *)NULL;
14201558Srgrimes	while (get_line()) {
14211558Srgrimes		if (debug)
142237663Scharnier			warnx("got line %s", line);
14231558Srgrimes		cp = line;
14241558Srgrimes		nextfield(&cp, &endcp);
14251558Srgrimes		if (*cp == '#')
14261558Srgrimes			goto nextline;
14271558Srgrimes
14281558Srgrimes		/*
14291558Srgrimes		 * Set defaults.
14301558Srgrimes		 */
14311558Srgrimes		has_host = FALSE;
14321558Srgrimes		anon = def_anon;
14331558Srgrimes		exflags = MNT_EXPORTED;
14341558Srgrimes		got_nondir = 0;
14351558Srgrimes		opt_flags = 0;
14361558Srgrimes		ep = (struct exportlist *)NULL;
1437192934Srmacklem		dirp = NULL;
14381558Srgrimes
14391558Srgrimes		/*
1440192934Srmacklem		 * Handle the V4 root dir.
1441192934Srmacklem		 */
1442192934Srmacklem		if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1443192934Srmacklem			/*
1444192934Srmacklem			 * V4: just indicates that it is the v4 root point,
1445192934Srmacklem			 * so skip over that and set v4root_phase.
1446192934Srmacklem			 */
1447192934Srmacklem			if (v4root_phase > 0) {
1448192934Srmacklem				syslog(LOG_ERR, "V4:duplicate line, ignored");
1449192934Srmacklem				goto nextline;
1450192934Srmacklem			}
1451192934Srmacklem			v4root_phase = 1;
1452192934Srmacklem			cp += 3;
1453192934Srmacklem			nextfield(&cp, &endcp);
1454192934Srmacklem		}
1455192934Srmacklem
1456192934Srmacklem		/*
14571558Srgrimes		 * Create new exports list entry
14581558Srgrimes		 */
14591558Srgrimes		len = endcp-cp;
14601558Srgrimes		tgrp = grp = get_grp();
14611558Srgrimes		while (len > 0) {
1462194880Sdfr			if (len > MNTNAMLEN) {
1463329392Sbrd			    getexp_err(ep, tgrp, "mountpoint too long");
14641558Srgrimes			    goto nextline;
14651558Srgrimes			}
14661558Srgrimes			if (*cp == '-') {
14671558Srgrimes			    if (ep == (struct exportlist *)NULL) {
1468329392Sbrd				getexp_err(ep, tgrp,
1469329392Sbrd				    "flag before export path definition");
14701558Srgrimes				goto nextline;
14711558Srgrimes			    }
14721558Srgrimes			    if (debug)
147337663Scharnier				warnx("doing opt %s", cp);
14741558Srgrimes			    got_nondir = 1;
14751558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
14761558Srgrimes				&exflags, &anon)) {
1477329392Sbrd				getexp_err(ep, tgrp, NULL);
14781558Srgrimes				goto nextline;
14791558Srgrimes			    }
14801558Srgrimes			} else if (*cp == '/') {
14811558Srgrimes			    savedc = *endcp;
14821558Srgrimes			    *endcp = '\0';
1483192934Srmacklem			    if (v4root_phase > 1) {
1484192934Srmacklem				    if (dirp != NULL) {
1485329392Sbrd					getexp_err(ep, tgrp, "Multiple V4 dirs");
1486192934Srmacklem					goto nextline;
1487192934Srmacklem				    }
1488192934Srmacklem			    }
14891558Srgrimes			    if (check_dirpath(cp) &&
14901558Srgrimes				statfs(cp, &fsb) >= 0) {
1491283008Srmacklem				if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1492283008Srmacklem				    syslog(LOG_ERR, "Warning: exporting of "
1493283008Srmacklem					"automounted fs %s not supported", cp);
14941558Srgrimes				if (got_nondir) {
1495329392Sbrd				    getexp_err(ep, tgrp, "dirs must be first");
14961558Srgrimes				    goto nextline;
14971558Srgrimes				}
1498192934Srmacklem				if (v4root_phase == 1) {
1499192934Srmacklem				    if (dirp != NULL) {
1500329392Sbrd					getexp_err(ep, tgrp, "Multiple V4 dirs");
15011558Srgrimes					goto nextline;
15021558Srgrimes				    }
1503192934Srmacklem				    if (strlen(v4root_dirpath) == 0) {
1504192934Srmacklem					strlcpy(v4root_dirpath, cp,
1505192934Srmacklem					    sizeof (v4root_dirpath));
1506192934Srmacklem				    } else if (strcmp(v4root_dirpath, cp)
1507192934Srmacklem					!= 0) {
1508192934Srmacklem					syslog(LOG_ERR,
1509192934Srmacklem					    "different V4 dirpath %s", cp);
1510329392Sbrd					getexp_err(ep, tgrp, NULL);
1511192934Srmacklem					goto nextline;
1512192934Srmacklem				    }
1513192934Srmacklem				    dirp = cp;
1514192934Srmacklem				    v4root_phase = 2;
1515192934Srmacklem				    got_nondir = 1;
1516192934Srmacklem				    ep = get_exp();
15171558Srgrimes				} else {
1518192934Srmacklem				    if (ep) {
1519192934Srmacklem					if (ep->ex_fs.val[0] !=
1520192934Srmacklem					    fsb.f_fsid.val[0] ||
1521192934Srmacklem					    ep->ex_fs.val[1] !=
1522192934Srmacklem					    fsb.f_fsid.val[1]) {
1523329392Sbrd						getexp_err(ep, tgrp,
1524329392Sbrd						    "fsid mismatch");
1525192934Srmacklem						goto nextline;
1526192934Srmacklem					}
1527192934Srmacklem				    } else {
1528192934Srmacklem					/*
1529192934Srmacklem					 * See if this directory is already
1530192934Srmacklem					 * in the list.
1531192934Srmacklem					 */
1532192934Srmacklem					ep = ex_search(&fsb.f_fsid);
1533192934Srmacklem					if (ep == (struct exportlist *)NULL) {
1534192934Srmacklem					    ep = get_exp();
1535192934Srmacklem					    ep->ex_fs = fsb.f_fsid;
1536324234Smanu					    ep->ex_fsdir = strdup(fsb.f_mntonname);
1537324234Smanu					    if (ep->ex_fsdir == NULL)
1538192934Srmacklem						out_of_mem();
1539192934Srmacklem					    if (debug)
1540192934Srmacklem						warnx(
1541192934Srmacklem						  "making new ep fs=0x%x,0x%x",
1542192934Srmacklem						  fsb.f_fsid.val[0],
1543192934Srmacklem						  fsb.f_fsid.val[1]);
1544192934Srmacklem					} else if (debug)
1545192934Srmacklem					    warnx("found ep fs=0x%x,0x%x",
1546192934Srmacklem						fsb.f_fsid.val[0],
1547192934Srmacklem						fsb.f_fsid.val[1]);
1548192934Srmacklem				    }
1549192934Srmacklem
15501558Srgrimes				    /*
1551192934Srmacklem				     * Add dirpath to export mount point.
15521558Srgrimes				     */
1553192934Srmacklem				    dirp = add_expdir(&dirhead, cp, len);
1554192934Srmacklem				    dirplen = len;
15551558Srgrimes				}
15561558Srgrimes			    } else {
1557329392Sbrd				getexp_err(ep, tgrp,
1558329392Sbrd				    "symbolic link in export path or statfs failed");
15591558Srgrimes				goto nextline;
15601558Srgrimes			    }
15611558Srgrimes			    *endcp = savedc;
15621558Srgrimes			} else {
15631558Srgrimes			    savedc = *endcp;
15641558Srgrimes			    *endcp = '\0';
15651558Srgrimes			    got_nondir = 1;
15661558Srgrimes			    if (ep == (struct exportlist *)NULL) {
1567329392Sbrd				getexp_err(ep, tgrp,
1568329392Sbrd				    "host(s) before export path definition");
15691558Srgrimes				goto nextline;
15701558Srgrimes			    }
15711558Srgrimes
15721558Srgrimes			    /*
15731558Srgrimes			     * Get the host or netgroup.
15741558Srgrimes			     */
15751558Srgrimes			    setnetgrent(cp);
15761558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
15771558Srgrimes			    do {
15781558Srgrimes				if (has_host) {
15791558Srgrimes				    grp->gr_next = get_grp();
15801558Srgrimes				    grp = grp->gr_next;
15811558Srgrimes				}
15821558Srgrimes				if (netgrp) {
158337003Sjoerg				    if (hst == 0) {
158437663Scharnier					syslog(LOG_ERR,
158537663Scharnier				"null hostname in netgroup %s, skipping", cp);
158637004Sjoerg					grp->gr_type = GT_IGNORE;
158737003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
158837663Scharnier					syslog(LOG_ERR,
158937663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
159029317Sjlemon					grp->gr_type = GT_IGNORE;
15911558Srgrimes				    }
15927401Swpaul				} else if (get_host(cp, grp, tgrp)) {
159337663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
159429317Sjlemon				    grp->gr_type = GT_IGNORE;
15951558Srgrimes				}
15961558Srgrimes				has_host = TRUE;
15971558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
15981558Srgrimes			    endnetgrent();
15991558Srgrimes			    *endcp = savedc;
16001558Srgrimes			}
16011558Srgrimes			cp = endcp;
16021558Srgrimes			nextfield(&cp, &endcp);
16031558Srgrimes			len = endcp - cp;
16041558Srgrimes		}
16051558Srgrimes		if (check_options(dirhead)) {
1606329392Sbrd			getexp_err(ep, tgrp, NULL);
16071558Srgrimes			goto nextline;
16081558Srgrimes		}
16091558Srgrimes		if (!has_host) {
161075641Siedowse			grp->gr_type = GT_DEFAULT;
16111558Srgrimes			if (debug)
161237663Scharnier				warnx("adding a default entry");
16131558Srgrimes
16141558Srgrimes		/*
16151558Srgrimes		 * Don't allow a network export coincide with a list of
16161558Srgrimes		 * host(s) on the same line.
16171558Srgrimes		 */
16181558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1619329392Sbrd			getexp_err(ep, tgrp, "network/host conflict");
16201558Srgrimes			goto nextline;
162129317Sjlemon
162274462Salfred		/*
162374462Salfred		 * If an export list was specified on this line, make sure
162429317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
162529317Sjlemon		 */
162629317Sjlemon		} else {
162729317Sjlemon			grp = tgrp;
162874462Salfred			while (grp && grp->gr_type == GT_IGNORE)
162929317Sjlemon				grp = grp->gr_next;
163029317Sjlemon			if (! grp) {
1631329392Sbrd			    getexp_err(ep, tgrp, "no valid entries");
163229317Sjlemon			    goto nextline;
163329317Sjlemon			}
16341558Srgrimes		}
16351558Srgrimes
1636192934Srmacklem		if (v4root_phase == 1) {
1637329392Sbrd			getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1638192934Srmacklem			goto nextline;
1639192934Srmacklem		}
1640192934Srmacklem
16411558Srgrimes		/*
16421558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
16431558Srgrimes		 * After loop, tgrp points to the start of the list and
16441558Srgrimes		 * grp points to the last entry in the list.
16451558Srgrimes		 */
16461558Srgrimes		grp = tgrp;
16471558Srgrimes		do {
164875635Siedowse			if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
164975635Siedowse			    &fsb)) {
1650329392Sbrd				getexp_err(ep, tgrp, NULL);
165175635Siedowse				goto nextline;
165275635Siedowse			}
16531558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
16541558Srgrimes
16551558Srgrimes		/*
1656192934Srmacklem		 * For V4: don't enter in mount lists.
1657192934Srmacklem		 */
1658194773Srmacklem		if (v4root_phase > 0 && v4root_phase <= 2) {
1659194773Srmacklem			/*
1660194773Srmacklem			 * Since these structures aren't used by mountd,
1661194773Srmacklem			 * free them up now.
1662194773Srmacklem			 */
1663194773Srmacklem			if (ep != NULL)
1664194773Srmacklem				free_exp(ep);
1665194773Srmacklem			while (tgrp != NULL) {
1666194773Srmacklem				grp = tgrp;
1667194773Srmacklem				tgrp = tgrp->gr_next;
1668194773Srmacklem				free_grp(grp);
1669194773Srmacklem			}
1670192934Srmacklem			goto nextline;
1671194773Srmacklem		}
1672192934Srmacklem
1673192934Srmacklem		/*
16741558Srgrimes		 * Success. Update the data structures.
16751558Srgrimes		 */
16761558Srgrimes		if (has_host) {
16779336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
16781558Srgrimes			grp->gr_next = grphead;
16791558Srgrimes			grphead = tgrp;
16801558Srgrimes		} else {
16811558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
16829336Sdfr				opt_flags);
16831558Srgrimes			free_grp(grp);
16841558Srgrimes		}
16851558Srgrimes		dirhead = (struct dirlist *)NULL;
16861558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
1687324955Smanu			SLIST_INSERT_HEAD(&exphead, ep, entries);
16881558Srgrimes
16891558Srgrimes			ep->ex_flag |= EX_LINKED;
16901558Srgrimes		}
16911558Srgrimesnextline:
1692192934Srmacklem		v4root_phase = 0;
16931558Srgrimes		if (dirhead) {
16941558Srgrimes			free_dir(dirhead);
16951558Srgrimes			dirhead = (struct dirlist *)NULL;
16961558Srgrimes		}
16971558Srgrimes	}
16981558Srgrimes}
16991558Srgrimes
17001558Srgrimes/*
1701166440Spjd * Get the export list from all specified files
1702166440Spjd */
1703285128Straszstatic void
1704216587Scharnierget_exportlist(void)
1705166440Spjd{
1706166440Spjd	struct exportlist *ep, *ep2;
1707166440Spjd	struct grouplist *grp, *tgrp;
1708166440Spjd	struct export_args export;
1709166440Spjd	struct iovec *iov;
1710166440Spjd	struct statfs *fsp, *mntbufp;
1711166440Spjd	struct xvfsconf vfc;
1712166440Spjd	char errmsg[255];
1713230352Seadler	int num, i;
1714166440Spjd	int iovlen;
1715168684Spjd	int done;
1716192934Srmacklem	struct nfsex_args eargs;
1717166440Spjd
1718241568Srmacklem	if (suspend_nfsd != 0)
1719241568Srmacklem		(void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1720192934Srmacklem	v4root_dirpath[0] = '\0';
1721166440Spjd	bzero(&export, sizeof(export));
1722166440Spjd	export.ex_flags = MNT_DELEXPORT;
1723166440Spjd	iov = NULL;
1724166440Spjd	iovlen = 0;
1725166440Spjd	bzero(errmsg, sizeof(errmsg));
1726166440Spjd
1727166440Spjd	/*
1728166440Spjd	 * First, get rid of the old list
1729166440Spjd	 */
1730324955Smanu	SLIST_FOREACH_SAFE(ep, &exphead, entries, ep2) {
1731324955Smanu		SLIST_REMOVE(&exphead, ep, exportlist, entries);
1732324955Smanu		free_exp(ep);
1733166440Spjd	}
1734166440Spjd
1735166440Spjd	grp = grphead;
1736166440Spjd	while (grp) {
1737166440Spjd		tgrp = grp;
1738166440Spjd		grp = grp->gr_next;
1739166440Spjd		free_grp(tgrp);
1740166440Spjd	}
1741166440Spjd	grphead = (struct grouplist *)NULL;
1742166440Spjd
1743166440Spjd	/*
1744192934Srmacklem	 * and the old V4 root dir.
1745192934Srmacklem	 */
1746192934Srmacklem	bzero(&eargs, sizeof (eargs));
1747192934Srmacklem	eargs.export.ex_flags = MNT_DELEXPORT;
1748282214Strasz	if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1749192934Srmacklem	    errno != ENOENT)
1750192934Srmacklem		syslog(LOG_ERR, "Can't delete exports for V4:");
1751192934Srmacklem
1752192934Srmacklem	/*
1753192934Srmacklem	 * and clear flag that notes if a public fh has been exported.
1754192934Srmacklem	 */
1755192934Srmacklem	has_publicfh = 0;
1756192934Srmacklem
1757192934Srmacklem	/*
1758166440Spjd	 * And delete exports that are in the kernel for all local
1759166440Spjd	 * filesystems.
1760166440Spjd	 * XXX: Should know how to handle all local exportable filesystems.
1761166440Spjd	 */
1762166440Spjd	num = getmntinfo(&mntbufp, MNT_NOWAIT);
1763166440Spjd
1764166440Spjd	if (num > 0) {
1765166440Spjd		build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1766166440Spjd		build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1767166440Spjd		build_iovec(&iov, &iovlen, "from", NULL, 0);
1768166440Spjd		build_iovec(&iov, &iovlen, "update", NULL, 0);
1769166440Spjd		build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1770166440Spjd		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1771166440Spjd	}
1772166440Spjd
1773166440Spjd	for (i = 0; i < num; i++) {
1774166440Spjd		fsp = &mntbufp[i];
1775166440Spjd		if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1776166440Spjd			syslog(LOG_ERR, "getvfsbyname() failed for %s",
1777166440Spjd			    fsp->f_fstypename);
1778166440Spjd			continue;
1779166440Spjd		}
1780166440Spjd
1781166440Spjd		/*
1782281699Ssjg		 * We do not need to delete "export" flag from
1783281699Ssjg		 * filesystems that do not have it set.
1784281699Ssjg		 */
1785281699Ssjg		if (!(fsp->f_flags & MNT_EXPORTED))
1786281699Ssjg		    continue;
1787281699Ssjg		/*
1788166440Spjd		 * Do not delete export for network filesystem by
1789166440Spjd		 * passing "export" arg to nmount().
1790166440Spjd		 * It only makes sense to do this for local filesystems.
1791166440Spjd		 */
1792166440Spjd		if (vfc.vfc_flags & VFCF_NETWORK)
1793166440Spjd			continue;
1794166440Spjd
1795166440Spjd		iov[1].iov_base = fsp->f_fstypename;
1796166440Spjd		iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1797166440Spjd		iov[3].iov_base = fsp->f_mntonname;
1798166440Spjd		iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1799166440Spjd		iov[5].iov_base = fsp->f_mntfromname;
1800166440Spjd		iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1801270183Sbdrewery		errmsg[0] = '\0';
1802166440Spjd
1803278523Skib		/*
1804278523Skib		 * EXDEV is returned when path exists but is not a
1805278523Skib		 * mount point.  May happens if raced with unmount.
1806278523Skib		 */
1807166440Spjd		if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1808278523Skib		    errno != ENOENT && errno != ENOTSUP && errno != EXDEV) {
1809166440Spjd			syslog(LOG_ERR,
1810166440Spjd			    "can't delete exports for %s: %m %s",
1811166440Spjd			    fsp->f_mntonname, errmsg);
1812166440Spjd		}
1813166440Spjd	}
1814166440Spjd
1815166440Spjd	if (iov != NULL) {
1816166440Spjd		/* Free strings allocated by strdup() in getmntopts.c */
1817166440Spjd		free(iov[0].iov_base); /* fstype */
1818166440Spjd		free(iov[2].iov_base); /* fspath */
1819166440Spjd		free(iov[4].iov_base); /* from */
1820166440Spjd		free(iov[6].iov_base); /* update */
1821166440Spjd		free(iov[8].iov_base); /* export */
1822166440Spjd		free(iov[10].iov_base); /* errmsg */
1823166440Spjd
1824166440Spjd		/* free iov, allocated by realloc() */
1825166440Spjd		free(iov);
1826166440Spjd		iovlen = 0;
1827166440Spjd	}
1828166440Spjd
1829166440Spjd	/*
1830166440Spjd	 * Read in the exports file and build the list, calling
1831166440Spjd	 * nmount() as we go along to push the export rules into the kernel.
1832166440Spjd	 */
1833168684Spjd	done = 0;
1834166440Spjd	for (i = 0; exnames[i] != NULL; i++) {
1835166440Spjd		if (debug)
1836166440Spjd			warnx("reading exports from %s", exnames[i]);
1837166440Spjd		if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1838168684Spjd			syslog(LOG_WARNING, "can't open %s", exnames[i]);
1839168684Spjd			continue;
1840166440Spjd		}
1841166440Spjd		get_exportlist_one();
1842166440Spjd		fclose(exp_file);
1843168684Spjd		done++;
1844166440Spjd	}
1845168684Spjd	if (done == 0) {
1846168684Spjd		syslog(LOG_ERR, "can't open any exports file");
1847168684Spjd		exit(2);
1848168684Spjd	}
1849192934Srmacklem
1850192934Srmacklem	/*
1851192934Srmacklem	 * If there was no public fh, clear any previous one set.
1852192934Srmacklem	 */
1853282214Strasz	if (has_publicfh == 0)
1854192934Srmacklem		(void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1855241568Srmacklem
1856241568Srmacklem	/* Resume the nfsd. If they weren't suspended, this is harmless. */
1857241568Srmacklem	(void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1858166440Spjd}
1859166440Spjd
1860166440Spjd/*
18611558Srgrimes * Allocate an export list element
18621558Srgrimes */
1863285128Straszstatic struct exportlist *
1864216587Scharnierget_exp(void)
18651558Srgrimes{
18661558Srgrimes	struct exportlist *ep;
18671558Srgrimes
1868224003Sdelphij	ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
18691558Srgrimes	if (ep == (struct exportlist *)NULL)
18701558Srgrimes		out_of_mem();
18711558Srgrimes	return (ep);
18721558Srgrimes}
18731558Srgrimes
18741558Srgrimes/*
18751558Srgrimes * Allocate a group list element
18761558Srgrimes */
1877285128Straszstatic struct grouplist *
1878216587Scharnierget_grp(void)
18791558Srgrimes{
18801558Srgrimes	struct grouplist *gp;
18811558Srgrimes
1882224003Sdelphij	gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
18831558Srgrimes	if (gp == (struct grouplist *)NULL)
18841558Srgrimes		out_of_mem();
18851558Srgrimes	return (gp);
18861558Srgrimes}
18871558Srgrimes
18881558Srgrimes/*
18891558Srgrimes * Clean up upon an error in get_exportlist().
18901558Srgrimes */
1891285128Straszstatic void
1892329392Sbrdgetexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
18931558Srgrimes{
18941558Srgrimes	struct grouplist *tgrp;
18951558Srgrimes
1896329392Sbrd	if (!(opt_flags & OP_QUIET)) {
1897329392Sbrd		if (reason != NULL)
1898329392Sbrd			syslog(LOG_ERR, "bad exports list line '%s': %s", line,
1899329392Sbrd			    reason);
1900329392Sbrd		else
1901329392Sbrd			syslog(LOG_ERR, "bad exports list line '%s'", line);
1902329392Sbrd	}
19031558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
19041558Srgrimes		free_exp(ep);
19051558Srgrimes	while (grp) {
19061558Srgrimes		tgrp = grp;
19071558Srgrimes		grp = grp->gr_next;
19081558Srgrimes		free_grp(tgrp);
19091558Srgrimes	}
19101558Srgrimes}
19111558Srgrimes
19121558Srgrimes/*
19131558Srgrimes * Search the export list for a matching fs.
19141558Srgrimes */
1915285128Straszstatic struct exportlist *
1916216587Scharnierex_search(fsid_t *fsid)
19171558Srgrimes{
19181558Srgrimes	struct exportlist *ep;
19191558Srgrimes
1920324955Smanu	SLIST_FOREACH(ep, &exphead, entries) {
19211558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
19221558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
19231558Srgrimes			return (ep);
19241558Srgrimes	}
1925324955Smanu
19261558Srgrimes	return (ep);
19271558Srgrimes}
19281558Srgrimes
19291558Srgrimes/*
19301558Srgrimes * Add a directory path to the list.
19311558Srgrimes */
1932285128Straszstatic char *
1933216587Scharnieradd_expdir(struct dirlist **dpp, char *cp, int len)
19341558Srgrimes{
19351558Srgrimes	struct dirlist *dp;
19361558Srgrimes
1937324234Smanu	dp = malloc(sizeof (struct dirlist));
193837663Scharnier	if (dp == (struct dirlist *)NULL)
193937663Scharnier		out_of_mem();
19401558Srgrimes	dp->dp_left = *dpp;
19411558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
19421558Srgrimes	dp->dp_flag = 0;
19431558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
1944324234Smanu	dp->dp_dirp = strndup(cp, len);
1945324234Smanu	if (dp->dp_dirp == NULL)
1946324234Smanu		out_of_mem();
19471558Srgrimes	*dpp = dp;
19481558Srgrimes	return (dp->dp_dirp);
19491558Srgrimes}
19501558Srgrimes
19511558Srgrimes/*
19521558Srgrimes * Hang the dir list element off the dirpath binary tree as required
19531558Srgrimes * and update the entry for host.
19541558Srgrimes */
1955285128Straszstatic void
1956216587Scharnierhang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1957216587Scharnier	int flags)
19581558Srgrimes{
19591558Srgrimes	struct hostlist *hp;
19601558Srgrimes	struct dirlist *dp2;
19611558Srgrimes
19629336Sdfr	if (flags & OP_ALLDIRS) {
19631558Srgrimes		if (ep->ex_defdir)
19641558Srgrimes			free((caddr_t)dp);
19651558Srgrimes		else
19661558Srgrimes			ep->ex_defdir = dp;
19679336Sdfr		if (grp == (struct grouplist *)NULL) {
19681558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
1969240902Srmacklem			/* Save the default security flavors list. */
1970240902Srmacklem			ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1971240902Srmacklem			if (ep->ex_numsecflavors > 0)
1972240902Srmacklem				memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1973240902Srmacklem				    sizeof(ep->ex_secflavors));
19749336Sdfr		} else while (grp) {
19751558Srgrimes			hp = get_ht();
19761558Srgrimes			hp->ht_grp = grp;
19771558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
19781558Srgrimes			ep->ex_defdir->dp_hosts = hp;
1979240902Srmacklem			/* Save the security flavors list for this host set. */
1980240902Srmacklem			grp->gr_numsecflavors = ep->ex_numsecflavors;
1981240902Srmacklem			if (ep->ex_numsecflavors > 0)
1982240902Srmacklem				memcpy(grp->gr_secflavors, ep->ex_secflavors,
1983240902Srmacklem				    sizeof(ep->ex_secflavors));
19841558Srgrimes			grp = grp->gr_next;
19851558Srgrimes		}
19861558Srgrimes	} else {
19871558Srgrimes
19881558Srgrimes		/*
198937663Scharnier		 * Loop through the directories adding them to the tree.
19901558Srgrimes		 */
19911558Srgrimes		while (dp) {
19921558Srgrimes			dp2 = dp->dp_left;
1993240902Srmacklem			add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
19941558Srgrimes			dp = dp2;
19951558Srgrimes		}
19961558Srgrimes	}
19971558Srgrimes}
19981558Srgrimes
19991558Srgrimes/*
20001558Srgrimes * Traverse the binary tree either updating a node that is already there
20011558Srgrimes * for the new directory or adding the new node.
20021558Srgrimes */
2003285128Straszstatic void
2004216587Scharnieradd_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2005240902Srmacklem	int flags, struct exportlist *ep)
20061558Srgrimes{
20071558Srgrimes	struct dirlist *dp;
20081558Srgrimes	struct hostlist *hp;
20091558Srgrimes	int cmp;
20101558Srgrimes
20111558Srgrimes	dp = *dpp;
20121558Srgrimes	if (dp) {
20131558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
20141558Srgrimes		if (cmp > 0) {
2015240902Srmacklem			add_dlist(&dp->dp_left, newdp, grp, flags, ep);
20161558Srgrimes			return;
20171558Srgrimes		} else if (cmp < 0) {
2018240902Srmacklem			add_dlist(&dp->dp_right, newdp, grp, flags, ep);
20191558Srgrimes			return;
20201558Srgrimes		} else
20211558Srgrimes			free((caddr_t)newdp);
20221558Srgrimes	} else {
20231558Srgrimes		dp = newdp;
20241558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
20251558Srgrimes		*dpp = dp;
20261558Srgrimes	}
20271558Srgrimes	if (grp) {
20281558Srgrimes
20291558Srgrimes		/*
20301558Srgrimes		 * Hang all of the host(s) off of the directory point.
20311558Srgrimes		 */
20321558Srgrimes		do {
20331558Srgrimes			hp = get_ht();
20341558Srgrimes			hp->ht_grp = grp;
20351558Srgrimes			hp->ht_next = dp->dp_hosts;
20361558Srgrimes			dp->dp_hosts = hp;
2037240902Srmacklem			/* Save the security flavors list for this host set. */
2038240902Srmacklem			grp->gr_numsecflavors = ep->ex_numsecflavors;
2039240902Srmacklem			if (ep->ex_numsecflavors > 0)
2040240902Srmacklem				memcpy(grp->gr_secflavors, ep->ex_secflavors,
2041240902Srmacklem				    sizeof(ep->ex_secflavors));
20421558Srgrimes			grp = grp->gr_next;
20431558Srgrimes		} while (grp);
20449336Sdfr	} else {
20451558Srgrimes		dp->dp_flag |= DP_DEFSET;
2046240902Srmacklem		/* Save the default security flavors list. */
2047240902Srmacklem		ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2048240902Srmacklem		if (ep->ex_numsecflavors > 0)
2049240902Srmacklem			memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2050240902Srmacklem			    sizeof(ep->ex_secflavors));
20519336Sdfr	}
20521558Srgrimes}
20531558Srgrimes
20541558Srgrimes/*
20551558Srgrimes * Search for a dirpath on the export point.
20561558Srgrimes */
2057285128Straszstatic struct dirlist *
2058216587Scharnierdirp_search(struct dirlist *dp, char *dirp)
20591558Srgrimes{
20601558Srgrimes	int cmp;
20611558Srgrimes
20621558Srgrimes	if (dp) {
206374462Salfred		cmp = strcmp(dp->dp_dirp, dirp);
20641558Srgrimes		if (cmp > 0)
206574462Salfred			return (dirp_search(dp->dp_left, dirp));
20661558Srgrimes		else if (cmp < 0)
206774462Salfred			return (dirp_search(dp->dp_right, dirp));
20681558Srgrimes		else
20691558Srgrimes			return (dp);
20701558Srgrimes	}
20711558Srgrimes	return (dp);
20721558Srgrimes}
20731558Srgrimes
20741558Srgrimes/*
20751558Srgrimes * Scan for a host match in a directory tree.
20761558Srgrimes */
2077285128Straszstatic int
2078216587Scharnierchk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2079240902Srmacklem	int *hostsetp, int *numsecflavors, int **secflavorsp)
20801558Srgrimes{
20811558Srgrimes	struct hostlist *hp;
20821558Srgrimes	struct grouplist *grp;
208374462Salfred	struct addrinfo *ai;
20841558Srgrimes
20851558Srgrimes	if (dp) {
20861558Srgrimes		if (dp->dp_flag & DP_DEFSET)
20879336Sdfr			*defsetp = dp->dp_flag;
20881558Srgrimes		hp = dp->dp_hosts;
20891558Srgrimes		while (hp) {
20901558Srgrimes			grp = hp->ht_grp;
20911558Srgrimes			switch (grp->gr_type) {
20921558Srgrimes			case GT_HOST:
209374462Salfred				ai = grp->gr_ptr.gt_addrinfo;
209474462Salfred				for (; ai; ai = ai->ai_next) {
209575801Siedowse					if (!sacmp(ai->ai_addr, saddr, NULL)) {
209674462Salfred						*hostsetp =
209774462Salfred						    (hp->ht_flag | DP_HOSTSET);
2098240902Srmacklem						if (numsecflavors != NULL) {
2099240902Srmacklem							*numsecflavors =
2100240902Srmacklem							    grp->gr_numsecflavors;
2101240902Srmacklem							*secflavorsp =
2102240902Srmacklem							    grp->gr_secflavors;
2103240902Srmacklem						}
210474462Salfred						return (1);
210574462Salfred					}
21069336Sdfr				}
210775801Siedowse				break;
21081558Srgrimes			case GT_NET:
210975801Siedowse				if (!sacmp(saddr, (struct sockaddr *)
211075801Siedowse				    &grp->gr_ptr.gt_net.nt_net,
211175801Siedowse				    (struct sockaddr *)
211275801Siedowse				    &grp->gr_ptr.gt_net.nt_mask)) {
211374462Salfred					*hostsetp = (hp->ht_flag | DP_HOSTSET);
2114240902Srmacklem					if (numsecflavors != NULL) {
2115240902Srmacklem						*numsecflavors =
2116240902Srmacklem						    grp->gr_numsecflavors;
2117240902Srmacklem						*secflavorsp =
2118240902Srmacklem						    grp->gr_secflavors;
2119240902Srmacklem					}
212074462Salfred					return (1);
212174462Salfred				}
212275801Siedowse				break;
212375801Siedowse			}
21241558Srgrimes			hp = hp->ht_next;
21251558Srgrimes		}
21261558Srgrimes	}
21271558Srgrimes	return (0);
21281558Srgrimes}
21291558Srgrimes
21301558Srgrimes/*
21311558Srgrimes * Scan tree for a host that matches the address.
21321558Srgrimes */
2133285128Straszstatic int
2134216587Scharnierscan_tree(struct dirlist *dp, struct sockaddr *saddr)
21351558Srgrimes{
21369336Sdfr	int defset, hostset;
21371558Srgrimes
21381558Srgrimes	if (dp) {
21391558Srgrimes		if (scan_tree(dp->dp_left, saddr))
21401558Srgrimes			return (1);
2141240902Srmacklem		if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
21421558Srgrimes			return (1);
21431558Srgrimes		if (scan_tree(dp->dp_right, saddr))
21441558Srgrimes			return (1);
21451558Srgrimes	}
21461558Srgrimes	return (0);
21471558Srgrimes}
21481558Srgrimes
21491558Srgrimes/*
21501558Srgrimes * Traverse the dirlist tree and free it up.
21511558Srgrimes */
2152285128Straszstatic void
2153216587Scharnierfree_dir(struct dirlist *dp)
21541558Srgrimes{
21551558Srgrimes
21561558Srgrimes	if (dp) {
21571558Srgrimes		free_dir(dp->dp_left);
21581558Srgrimes		free_dir(dp->dp_right);
21591558Srgrimes		free_host(dp->dp_hosts);
2160324234Smanu		free(dp->dp_dirp);
2161324234Smanu		free(dp);
21621558Srgrimes	}
21631558Srgrimes}
21641558Srgrimes
21651558Srgrimes/*
2166184588Sdfr * Parse a colon separated list of security flavors
2167184588Sdfr */
2168285128Straszstatic int
2169216587Scharnierparsesec(char *seclist, struct exportlist *ep)
2170184588Sdfr{
2171184588Sdfr	char *cp, savedc;
2172184588Sdfr	int flavor;
2173184588Sdfr
2174184588Sdfr	ep->ex_numsecflavors = 0;
2175184588Sdfr	for (;;) {
2176184588Sdfr		cp = strchr(seclist, ':');
2177184588Sdfr		if (cp) {
2178184588Sdfr			savedc = *cp;
2179184588Sdfr			*cp = '\0';
2180184588Sdfr		}
2181184588Sdfr
2182184588Sdfr		if (!strcmp(seclist, "sys"))
2183184588Sdfr			flavor = AUTH_SYS;
2184184588Sdfr		else if (!strcmp(seclist, "krb5"))
2185184588Sdfr			flavor = RPCSEC_GSS_KRB5;
2186184588Sdfr		else if (!strcmp(seclist, "krb5i"))
2187184588Sdfr			flavor = RPCSEC_GSS_KRB5I;
2188184588Sdfr		else if (!strcmp(seclist, "krb5p"))
2189184588Sdfr			flavor = RPCSEC_GSS_KRB5P;
2190184588Sdfr		else {
2191184588Sdfr			if (cp)
2192184588Sdfr				*cp = savedc;
2193184588Sdfr			syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2194184588Sdfr			return (1);
2195184588Sdfr		}
2196184588Sdfr		if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2197184588Sdfr			if (cp)
2198184588Sdfr				*cp = savedc;
2199184588Sdfr			syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2200184588Sdfr			return (1);
2201184588Sdfr		}
2202184588Sdfr		ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2203184588Sdfr		ep->ex_numsecflavors++;
2204184588Sdfr		if (cp) {
2205184588Sdfr			*cp = savedc;
2206184588Sdfr			seclist = cp + 1;
2207184588Sdfr		} else {
2208184588Sdfr			break;
2209184588Sdfr		}
2210184588Sdfr	}
2211184588Sdfr	return (0);
2212184588Sdfr}
2213184588Sdfr
2214184588Sdfr/*
22151558Srgrimes * Parse the option string and update fields.
22161558Srgrimes * Option arguments may either be -<option>=<value> or
22171558Srgrimes * -<option> <value>
22181558Srgrimes */
2219285128Straszstatic int
2220216587Scharnierdo_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2221216587Scharnier	int *has_hostp, int *exflagsp, struct xucred *cr)
22221558Srgrimes{
22231558Srgrimes	char *cpoptarg, *cpoptend;
22241558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
22251558Srgrimes	int allflag, usedarg;
22261558Srgrimes
222751968Salfred	savedc2 = '\0';
22281558Srgrimes	cpopt = *cpp;
22291558Srgrimes	cpopt++;
22301558Srgrimes	cp = *endcpp;
22311558Srgrimes	savedc = *cp;
22321558Srgrimes	*cp = '\0';
22331558Srgrimes	while (cpopt && *cpopt) {
22341558Srgrimes		allflag = 1;
22351558Srgrimes		usedarg = -2;
223637663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
22371558Srgrimes			*cpoptend++ = '\0';
223837663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
22391558Srgrimes				*cpoptarg++ = '\0';
22401558Srgrimes		} else {
224137663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
22421558Srgrimes				*cpoptarg++ = '\0';
22431558Srgrimes			else {
22441558Srgrimes				*cp = savedc;
22451558Srgrimes				nextfield(&cp, &endcp);
22461558Srgrimes				**endcpp = '\0';
22471558Srgrimes				if (endcp > cp && *cp != '-') {
22481558Srgrimes					cpoptarg = cp;
22491558Srgrimes					savedc2 = *endcp;
22501558Srgrimes					*endcp = '\0';
22511558Srgrimes					usedarg = 0;
22521558Srgrimes				}
22531558Srgrimes			}
22541558Srgrimes		}
22551558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
22561558Srgrimes			*exflagsp |= MNT_EXRDONLY;
22571558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
22581558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
22591558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
22601558Srgrimes			usedarg++;
22611558Srgrimes			parsecred(cpoptarg, cr);
22621558Srgrimes			if (allflag == 0) {
22631558Srgrimes				*exflagsp |= MNT_EXPORTANON;
22641558Srgrimes				opt_flags |= OP_MAPALL;
22651558Srgrimes			} else
22661558Srgrimes				opt_flags |= OP_MAPROOT;
22671558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
226875801Siedowse		    !strcmp(cpopt, "m"))) {
22691558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
227037663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
22711558Srgrimes				return (1);
22721558Srgrimes			}
22731558Srgrimes			usedarg++;
22741558Srgrimes			opt_flags |= OP_MASK;
22751558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
22761558Srgrimes			!strcmp(cpopt, "n"))) {
227774462Salfred			if (strchr(cpoptarg, '/') != NULL) {
227874462Salfred				if (debug)
227974462Salfred					fprintf(stderr, "setting OP_MASKLEN\n");
228074462Salfred				opt_flags |= OP_MASKLEN;
228174462Salfred			}
22821558Srgrimes			if (grp->gr_type != GT_NULL) {
228337663Scharnier				syslog(LOG_ERR, "network/host conflict");
22841558Srgrimes				return (1);
22851558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
228637663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
22871558Srgrimes				return (1);
22881558Srgrimes			}
22891558Srgrimes			grp->gr_type = GT_NET;
22901558Srgrimes			*has_hostp = 1;
22911558Srgrimes			usedarg++;
22921558Srgrimes			opt_flags |= OP_NET;
22931558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
22941558Srgrimes			opt_flags |= OP_ALLDIRS;
229527447Sdfr		} else if (!strcmp(cpopt, "public")) {
229627447Sdfr			*exflagsp |= MNT_EXPUBLIC;
229727447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
229827447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
229927447Sdfr			opt_flags |= OP_MAPALL;
230027447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
230127447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
2302100336Sjoerg		} else if (!strcmp(cpopt, "quiet")) {
2303100336Sjoerg			opt_flags |= OP_QUIET;
2304247034Spluknet		} else if (cpoptarg && !strcmp(cpopt, "sec")) {
2305184588Sdfr			if (parsesec(cpoptarg, ep))
2306184588Sdfr				return (1);
2307184588Sdfr			opt_flags |= OP_SEC;
2308184588Sdfr			usedarg++;
23091558Srgrimes		} else {
231037663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
23111558Srgrimes			return (1);
23121558Srgrimes		}
23131558Srgrimes		if (usedarg >= 0) {
23141558Srgrimes			*endcp = savedc2;
23151558Srgrimes			**endcpp = savedc;
23161558Srgrimes			if (usedarg > 0) {
23171558Srgrimes				*cpp = cp;
23181558Srgrimes				*endcpp = endcp;
23191558Srgrimes			}
23201558Srgrimes			return (0);
23211558Srgrimes		}
23221558Srgrimes		cpopt = cpoptend;
23231558Srgrimes	}
23241558Srgrimes	**endcpp = savedc;
23251558Srgrimes	return (0);
23261558Srgrimes}
23271558Srgrimes
23281558Srgrimes/*
23291558Srgrimes * Translate a character string to the corresponding list of network
23301558Srgrimes * addresses for a hostname.
23311558Srgrimes */
2332285128Straszstatic int
2333216587Scharnierget_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
23341558Srgrimes{
23357401Swpaul	struct grouplist *checkgrp;
233675635Siedowse	struct addrinfo *ai, *tai, hints;
233774462Salfred	int ecode;
233874462Salfred	char host[NI_MAXHOST];
23391558Srgrimes
234074462Salfred	if (grp->gr_type != GT_NULL) {
234174462Salfred		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
23421558Srgrimes		return (1);
23431558Srgrimes	}
234474462Salfred	memset(&hints, 0, sizeof hints);
234574462Salfred	hints.ai_flags = AI_CANONNAME;
234674462Salfred	hints.ai_protocol = IPPROTO_UDP;
234774462Salfred	ecode = getaddrinfo(cp, NULL, &hints, &ai);
234874462Salfred	if (ecode != 0) {
234975635Siedowse		syslog(LOG_ERR,"can't get address info for host %s", cp);
235074462Salfred		return 1;
235174462Salfred	}
235274462Salfred	grp->gr_ptr.gt_addrinfo = ai;
235374462Salfred	while (ai != NULL) {
235474462Salfred		if (ai->ai_canonname == NULL) {
235574462Salfred			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2356146187Sume			    sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
235774462Salfred				strlcpy(host, "?", sizeof(host));
235874462Salfred			ai->ai_canonname = strdup(host);
235974462Salfred			ai->ai_flags |= AI_CANONNAME;
236075641Siedowse		}
236174462Salfred		if (debug)
236275635Siedowse			fprintf(stderr, "got host %s\n", ai->ai_canonname);
236375635Siedowse		/*
236475635Siedowse		 * Sanity check: make sure we don't already have an entry
236575635Siedowse		 * for this host in the grouplist.
236675635Siedowse		 */
236775635Siedowse		for (checkgrp = tgrp; checkgrp != NULL;
236875635Siedowse		    checkgrp = checkgrp->gr_next) {
236975635Siedowse			if (checkgrp->gr_type != GT_HOST)
237075635Siedowse				continue;
237175635Siedowse			for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
237275635Siedowse			    tai = tai->ai_next) {
237375801Siedowse				if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
237475635Siedowse					continue;
237575635Siedowse				if (debug)
237675635Siedowse					fprintf(stderr,
237775635Siedowse					    "ignoring duplicate host %s\n",
237875635Siedowse					    ai->ai_canonname);
237975635Siedowse				grp->gr_type = GT_IGNORE;
238075635Siedowse				return (0);
238175635Siedowse			}
238275635Siedowse		}
238374462Salfred		ai = ai->ai_next;
23841558Srgrimes	}
238575635Siedowse	grp->gr_type = GT_HOST;
23861558Srgrimes	return (0);
23871558Srgrimes}
23881558Srgrimes
23891558Srgrimes/*
23901558Srgrimes * Free up an exports list component
23911558Srgrimes */
2392285128Straszstatic void
2393216587Scharnierfree_exp(struct exportlist *ep)
23941558Srgrimes{
23951558Srgrimes
23961558Srgrimes	if (ep->ex_defdir) {
23971558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
23981558Srgrimes		free((caddr_t)ep->ex_defdir);
23991558Srgrimes	}
24001558Srgrimes	if (ep->ex_fsdir)
24011558Srgrimes		free(ep->ex_fsdir);
240227447Sdfr	if (ep->ex_indexfile)
240327447Sdfr		free(ep->ex_indexfile);
24041558Srgrimes	free_dir(ep->ex_dirl);
24051558Srgrimes	free((caddr_t)ep);
24061558Srgrimes}
24071558Srgrimes
24081558Srgrimes/*
24091558Srgrimes * Free hosts.
24101558Srgrimes */
2411285128Straszstatic void
2412216587Scharnierfree_host(struct hostlist *hp)
24131558Srgrimes{
24141558Srgrimes	struct hostlist *hp2;
24151558Srgrimes
24161558Srgrimes	while (hp) {
24171558Srgrimes		hp2 = hp;
24181558Srgrimes		hp = hp->ht_next;
24191558Srgrimes		free((caddr_t)hp2);
24201558Srgrimes	}
24211558Srgrimes}
24221558Srgrimes
2423285128Straszstatic struct hostlist *
2424216587Scharnierget_ht(void)
24251558Srgrimes{
24261558Srgrimes	struct hostlist *hp;
24271558Srgrimes
24281558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
24291558Srgrimes	if (hp == (struct hostlist *)NULL)
24301558Srgrimes		out_of_mem();
24311558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
24329336Sdfr	hp->ht_flag = 0;
24331558Srgrimes	return (hp);
24341558Srgrimes}
24351558Srgrimes
24361558Srgrimes/*
24371558Srgrimes * Out of memory, fatal
24381558Srgrimes */
2439285128Straszstatic void
2440216587Scharnierout_of_mem(void)
24411558Srgrimes{
24421558Srgrimes
244337663Scharnier	syslog(LOG_ERR, "out of memory");
24441558Srgrimes	exit(2);
24451558Srgrimes}
24461558Srgrimes
24471558Srgrimes/*
2448158857Srodrigc * Do the nmount() syscall with the update flag to push the export info into
24491558Srgrimes * the kernel.
24501558Srgrimes */
2451285128Straszstatic int
2452158857Srodrigcdo_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2453158857Srodrigc    struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
24541558Srgrimes{
245575841Siedowse	struct statfs fsb1;
245674462Salfred	struct addrinfo *ai;
2457282214Strasz	struct export_args *eap;
2458158857Srodrigc	char errmsg[255];
2459158857Srodrigc	char *cp;
24601558Srgrimes	int done;
2461158857Srodrigc	char savedc;
2462158857Srodrigc	struct iovec *iov;
2463184588Sdfr	int i, iovlen;
2464158857Srodrigc	int ret;
2465192934Srmacklem	struct nfsex_args nfsea;
24661558Srgrimes
2467282214Strasz	eap = &nfsea.export;
2468192934Srmacklem
2469158857Srodrigc	cp = NULL;
2470158857Srodrigc	savedc = '\0';
2471158857Srodrigc	iov = NULL;
2472158857Srodrigc	iovlen = 0;
2473158857Srodrigc	ret = 0;
247475801Siedowse
2475192934Srmacklem	bzero(eap, sizeof (struct export_args));
2476158857Srodrigc	bzero(errmsg, sizeof(errmsg));
2477192934Srmacklem	eap->ex_flags = exflags;
2478192934Srmacklem	eap->ex_anon = *anoncrp;
2479192934Srmacklem	eap->ex_indexfile = ep->ex_indexfile;
248075641Siedowse	if (grp->gr_type == GT_HOST)
248174462Salfred		ai = grp->gr_ptr.gt_addrinfo;
248275641Siedowse	else
248375641Siedowse		ai = NULL;
2484192934Srmacklem	eap->ex_numsecflavors = ep->ex_numsecflavors;
2485192934Srmacklem	for (i = 0; i < eap->ex_numsecflavors; i++)
2486192934Srmacklem		eap->ex_secflavors[i] = ep->ex_secflavors[i];
2487192934Srmacklem	if (eap->ex_numsecflavors == 0) {
2488192934Srmacklem		eap->ex_numsecflavors = 1;
2489192934Srmacklem		eap->ex_secflavors[0] = AUTH_SYS;
2490184588Sdfr	}
24911558Srgrimes	done = FALSE;
2492158857Srodrigc
2493192934Srmacklem	if (v4root_phase == 0) {
2494192934Srmacklem		build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2495192934Srmacklem		build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2496192934Srmacklem		build_iovec(&iov, &iovlen, "from", NULL, 0);
2497192934Srmacklem		build_iovec(&iov, &iovlen, "update", NULL, 0);
2498192934Srmacklem		build_iovec(&iov, &iovlen, "export", eap,
2499192934Srmacklem		    sizeof (struct export_args));
2500192934Srmacklem		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2501192934Srmacklem	}
2502158857Srodrigc
25031558Srgrimes	while (!done) {
25041558Srgrimes		switch (grp->gr_type) {
25051558Srgrimes		case GT_HOST:
250675641Siedowse			if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
250774462Salfred				goto skip;
2508192934Srmacklem			eap->ex_addr = ai->ai_addr;
2509192934Srmacklem			eap->ex_addrlen = ai->ai_addrlen;
2510192934Srmacklem			eap->ex_masklen = 0;
25111558Srgrimes			break;
25121558Srgrimes		case GT_NET:
251375801Siedowse			if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
251474462Salfred			    have_v6 == 0)
251574462Salfred				goto skip;
2516192934Srmacklem			eap->ex_addr =
251775801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2518192934Srmacklem			eap->ex_addrlen =
2519158857Srodrigc			    ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2520192934Srmacklem			eap->ex_mask =
252175801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2522192934Srmacklem			eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
25231558Srgrimes			break;
252475641Siedowse		case GT_DEFAULT:
2525192934Srmacklem			eap->ex_addr = NULL;
2526192934Srmacklem			eap->ex_addrlen = 0;
2527192934Srmacklem			eap->ex_mask = NULL;
2528192934Srmacklem			eap->ex_masklen = 0;
252975641Siedowse			break;
25307401Swpaul		case GT_IGNORE:
2531158857Srodrigc			ret = 0;
2532158857Srodrigc			goto error_exit;
25337401Swpaul			break;
25341558Srgrimes		default:
253537663Scharnier			syslog(LOG_ERR, "bad grouptype");
25361558Srgrimes			if (cp)
25371558Srgrimes				*cp = savedc;
2538158857Srodrigc			ret = 1;
2539158857Srodrigc			goto error_exit;
2540298089Spfg		}
25411558Srgrimes
25421558Srgrimes		/*
2543192934Srmacklem		 * For V4:, use the nfssvc() syscall, instead of mount().
25441558Srgrimes		 */
2545192934Srmacklem		if (v4root_phase == 2) {
2546192934Srmacklem			nfsea.fspec = v4root_dirpath;
2547282214Strasz			if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2548192934Srmacklem				syslog(LOG_ERR, "Exporting V4: failed");
2549192934Srmacklem				return (2);
2550158857Srodrigc			}
2551192934Srmacklem		} else {
2552192934Srmacklem			/*
2553192934Srmacklem			 * XXX:
2554192934Srmacklem			 * Maybe I should just use the fsb->f_mntonname path
2555192934Srmacklem			 * instead of looping back up the dirp to the mount
2556192934Srmacklem			 * point??
2557192934Srmacklem			 * Also, needs to know how to export all types of local
2558192934Srmacklem			 * exportable filesystems and not just "ufs".
2559192934Srmacklem			 */
2560192934Srmacklem			iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2561192934Srmacklem			iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2562192934Srmacklem			iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2563192934Srmacklem			iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2564192934Srmacklem			iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2565192934Srmacklem			iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2566270183Sbdrewery			errmsg[0] = '\0';
2567192934Srmacklem
2568192934Srmacklem			while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2569192934Srmacklem				if (cp)
2570192934Srmacklem					*cp-- = savedc;
2571192934Srmacklem				else
2572192934Srmacklem					cp = dirp + dirplen - 1;
2573192934Srmacklem				if (opt_flags & OP_QUIET) {
2574192934Srmacklem					ret = 1;
2575192934Srmacklem					goto error_exit;
2576192934Srmacklem				}
2577192934Srmacklem				if (errno == EPERM) {
2578192934Srmacklem					if (debug)
2579239744Sdelphij						warnx("can't change attributes for %s: %s",
2580239744Sdelphij						    dirp, errmsg);
2581192934Srmacklem					syslog(LOG_ERR,
2582239744Sdelphij					   "can't change attributes for %s: %s",
2583239744Sdelphij					    dirp, errmsg);
2584192934Srmacklem					ret = 1;
2585192934Srmacklem					goto error_exit;
2586192934Srmacklem				}
2587192934Srmacklem				if (opt_flags & OP_ALLDIRS) {
2588192934Srmacklem					if (errno == EINVAL)
2589192934Srmacklem						syslog(LOG_ERR,
2590100336Sjoerg		"-alldirs requested but %s is not a filesystem mountpoint",
2591192934Srmacklem						    dirp);
2592192934Srmacklem					else
2593192934Srmacklem						syslog(LOG_ERR,
2594192934Srmacklem						    "could not remount %s: %m",
2595192934Srmacklem						    dirp);
2596192934Srmacklem					ret = 1;
2597192934Srmacklem					goto error_exit;
2598192934Srmacklem				}
2599192934Srmacklem				/* back up over the last component */
2600192934Srmacklem				while (*cp == '/' && cp > dirp)
2601192934Srmacklem					cp--;
2602192934Srmacklem				while (*(cp - 1) != '/' && cp > dirp)
2603192934Srmacklem					cp--;
2604192934Srmacklem				if (cp == dirp) {
2605192934Srmacklem					if (debug)
2606192934Srmacklem						warnx("mnt unsucc");
2607192934Srmacklem					syslog(LOG_ERR, "can't export %s %s",
2608192934Srmacklem					    dirp, errmsg);
2609192934Srmacklem					ret = 1;
2610192934Srmacklem					goto error_exit;
2611192934Srmacklem				}
2612192934Srmacklem				savedc = *cp;
2613192934Srmacklem				*cp = '\0';
2614192934Srmacklem				/*
2615192934Srmacklem				 * Check that we're still on the same
2616192934Srmacklem				 * filesystem.
2617192934Srmacklem				 */
2618192934Srmacklem				if (statfs(dirp, &fsb1) != 0 ||
2619192934Srmacklem				    bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2620192934Srmacklem				    sizeof (fsb1.f_fsid)) != 0) {
2621192934Srmacklem					*cp = savedc;
2622100336Sjoerg					syslog(LOG_ERR,
2623192934Srmacklem					    "can't export %s %s", dirp,
2624192934Srmacklem					    errmsg);
2625192934Srmacklem					ret = 1;
2626192934Srmacklem					goto error_exit;
2627192934Srmacklem				}
26281558Srgrimes			}
26291558Srgrimes		}
2630192934Srmacklem
2631192934Srmacklem		/*
2632192934Srmacklem		 * For the experimental server:
2633192934Srmacklem		 * If this is the public directory, get the file handle
2634192934Srmacklem		 * and load it into the kernel via the nfssvc() syscall.
2635192934Srmacklem		 */
2636282214Strasz		if ((exflags & MNT_EXPUBLIC) != 0) {
2637192934Srmacklem			fhandle_t fh;
2638192934Srmacklem			char *public_name;
2639192934Srmacklem
2640192934Srmacklem			if (eap->ex_indexfile != NULL)
2641192934Srmacklem				public_name = eap->ex_indexfile;
2642192934Srmacklem			else
2643192934Srmacklem				public_name = dirp;
2644192934Srmacklem			if (getfh(public_name, &fh) < 0)
2645192934Srmacklem				syslog(LOG_ERR,
2646192934Srmacklem				    "Can't get public fh for %s", public_name);
2647192934Srmacklem			else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2648192934Srmacklem				syslog(LOG_ERR,
2649192934Srmacklem				    "Can't set public fh for %s", public_name);
2650192934Srmacklem			else
2651192934Srmacklem				has_publicfh = 1;
2652192934Srmacklem		}
265374462Salfredskip:
265475641Siedowse		if (ai != NULL)
265574462Salfred			ai = ai->ai_next;
265675641Siedowse		if (ai == NULL)
26571558Srgrimes			done = TRUE;
26581558Srgrimes	}
26591558Srgrimes	if (cp)
26601558Srgrimes		*cp = savedc;
2661158857Srodrigcerror_exit:
2662158857Srodrigc	/* free strings allocated by strdup() in getmntopts.c */
2663158857Srodrigc	if (iov != NULL) {
2664158857Srodrigc		free(iov[0].iov_base); /* fstype */
2665158857Srodrigc		free(iov[2].iov_base); /* fspath */
2666158857Srodrigc		free(iov[4].iov_base); /* from */
2667158857Srodrigc		free(iov[6].iov_base); /* update */
2668158857Srodrigc		free(iov[8].iov_base); /* export */
2669158857Srodrigc		free(iov[10].iov_base); /* errmsg */
2670158857Srodrigc
2671158857Srodrigc		/* free iov, allocated by realloc() */
2672158857Srodrigc		free(iov);
2673158857Srodrigc	}
2674158857Srodrigc	return (ret);
26751558Srgrimes}
26761558Srgrimes
26771558Srgrimes/*
26781558Srgrimes * Translate a net address.
267975801Siedowse *
268075801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
26811558Srgrimes */
2682285128Straszstatic int
2683216587Scharnierget_net(char *cp, struct netmsk *net, int maskflg)
26841558Srgrimes{
268575861Siedowse	struct netent *np = NULL;
268674462Salfred	char *name, *p, *prefp;
268775801Siedowse	struct sockaddr_in sin;
268875861Siedowse	struct sockaddr *sa = NULL;
268974462Salfred	struct addrinfo hints, *ai = NULL;
269074462Salfred	char netname[NI_MAXHOST];
269174462Salfred	long preflen;
26921558Srgrimes
269375635Siedowse	p = prefp = NULL;
269474462Salfred	if ((opt_flags & OP_MASKLEN) && !maskflg) {
269574462Salfred		p = strchr(cp, '/');
269674462Salfred		*p = '\0';
269774462Salfred		prefp = p + 1;
269874462Salfred	}
269974462Salfred
270075861Siedowse	/*
270175861Siedowse	 * Check for a numeric address first. We wish to avoid
270275861Siedowse	 * possible DNS lookups in getnetbyname().
270375861Siedowse	 */
270475861Siedowse	if (isxdigit(*cp) || *cp == ':') {
270574462Salfred		memset(&hints, 0, sizeof hints);
270675801Siedowse		/* Ensure the mask and the network have the same family. */
270775801Siedowse		if (maskflg && (opt_flags & OP_NET))
270875801Siedowse			hints.ai_family = net->nt_net.ss_family;
270975801Siedowse		else if (!maskflg && (opt_flags & OP_HAVEMASK))
271075801Siedowse			hints.ai_family = net->nt_mask.ss_family;
271175801Siedowse		else
271275801Siedowse			hints.ai_family = AF_UNSPEC;
271374462Salfred		hints.ai_flags = AI_NUMERICHOST;
271475861Siedowse		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
271575861Siedowse			sa = ai->ai_addr;
271675861Siedowse		if (sa != NULL && ai->ai_family == AF_INET) {
271774462Salfred			/*
271875801Siedowse			 * The address in `cp' is really a network address, so
271975801Siedowse			 * use inet_network() to re-interpret this correctly.
272075801Siedowse			 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
272174462Salfred			 */
272275801Siedowse			bzero(&sin, sizeof sin);
272374462Salfred			sin.sin_family = AF_INET;
272474462Salfred			sin.sin_len = sizeof sin;
272575801Siedowse			sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
272674462Salfred			if (debug)
272775801Siedowse				fprintf(stderr, "get_net: v4 addr %s\n",
272875801Siedowse				    inet_ntoa(sin.sin_addr));
272974462Salfred			sa = (struct sockaddr *)&sin;
273075861Siedowse		}
273175861Siedowse	}
273275861Siedowse	if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
273375861Siedowse		bzero(&sin, sizeof sin);
273475861Siedowse		sin.sin_family = AF_INET;
273575861Siedowse		sin.sin_len = sizeof sin;
273675861Siedowse		sin.sin_addr = inet_makeaddr(np->n_net, 0);
273775861Siedowse		sa = (struct sockaddr *)&sin;
273875861Siedowse	}
273975861Siedowse	if (sa == NULL)
274074462Salfred		goto fail;
274125318Spst
274275801Siedowse	if (maskflg) {
274375801Siedowse		/* The specified sockaddr is a mask. */
274475801Siedowse		if (checkmask(sa) != 0)
274575801Siedowse			goto fail;
274675801Siedowse		bcopy(sa, &net->nt_mask, sa->sa_len);
274775801Siedowse		opt_flags |= OP_HAVEMASK;
274875801Siedowse	} else {
274975801Siedowse		/* The specified sockaddr is a network address. */
275075801Siedowse		bcopy(sa, &net->nt_net, sa->sa_len);
275174462Salfred
275275801Siedowse		/* Get a network name for the export list. */
275375801Siedowse		if (np) {
275475801Siedowse			name = np->n_name;
275575801Siedowse		} else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2756146187Sume		   NULL, 0, NI_NUMERICHOST) == 0) {
275775801Siedowse			name = netname;
275875801Siedowse		} else {
275975801Siedowse			goto fail;
276075801Siedowse		}
276175801Siedowse		if ((net->nt_name = strdup(name)) == NULL)
276275801Siedowse			out_of_mem();
276375801Siedowse
276475801Siedowse		/*
276575801Siedowse		 * Extract a mask from either a "/<masklen>" suffix, or
276675801Siedowse		 * from the class of an IPv4 address.
276775801Siedowse		 */
276874462Salfred		if (opt_flags & OP_MASKLEN) {
276974462Salfred			preflen = strtol(prefp, NULL, 10);
277075801Siedowse			if (preflen < 0L || preflen == LONG_MAX)
277174462Salfred				goto fail;
277275801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
277375801Siedowse			if (makemask(&net->nt_mask, (int)preflen) != 0)
277475801Siedowse				goto fail;
277575801Siedowse			opt_flags |= OP_HAVEMASK;
277674462Salfred			*p = '/';
277775801Siedowse		} else if (sa->sa_family == AF_INET &&
277875801Siedowse		    (opt_flags & OP_MASK) == 0) {
277975801Siedowse			in_addr_t addr;
278074462Salfred
278175801Siedowse			addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
278275801Siedowse			if (IN_CLASSA(addr))
278375801Siedowse				preflen = 8;
278475801Siedowse			else if (IN_CLASSB(addr))
278575801Siedowse				preflen = 16;
278675801Siedowse			else if (IN_CLASSC(addr))
278775801Siedowse				preflen = 24;
278875801Siedowse			else if (IN_CLASSD(addr))
278975801Siedowse				preflen = 28;
279075801Siedowse			else
279175801Siedowse				preflen = 32;	/* XXX */
279275801Siedowse
279375801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
279475801Siedowse			makemask(&net->nt_mask, (int)preflen);
279575801Siedowse			opt_flags |= OP_HAVEMASK;
279674462Salfred		}
279774462Salfred	}
279874462Salfred
279974462Salfred	if (ai)
280074462Salfred		freeaddrinfo(ai);
280174462Salfred	return 0;
280274462Salfred
280374462Salfredfail:
280474462Salfred	if (ai)
280574462Salfred		freeaddrinfo(ai);
280674462Salfred	return 1;
28071558Srgrimes}
28081558Srgrimes
28091558Srgrimes/*
28101558Srgrimes * Parse out the next white space separated field
28111558Srgrimes */
2812285128Straszstatic void
2813216587Scharniernextfield(char **cp, char **endcp)
28141558Srgrimes{
28151558Srgrimes	char *p;
28161558Srgrimes
28171558Srgrimes	p = *cp;
28181558Srgrimes	while (*p == ' ' || *p == '\t')
28191558Srgrimes		p++;
28201558Srgrimes	if (*p == '\n' || *p == '\0')
28211558Srgrimes		*cp = *endcp = p;
28221558Srgrimes	else {
28231558Srgrimes		*cp = p++;
28241558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
28251558Srgrimes			p++;
28261558Srgrimes		*endcp = p;
28271558Srgrimes	}
28281558Srgrimes}
28291558Srgrimes
28301558Srgrimes/*
28311558Srgrimes * Get an exports file line. Skip over blank lines and handle line
28321558Srgrimes * continuations.
28331558Srgrimes */
2834285128Straszstatic int
2835216587Scharnierget_line(void)
28361558Srgrimes{
28371558Srgrimes	char *p, *cp;
283896622Siedowse	size_t len;
28391558Srgrimes	int totlen, cont_line;
28401558Srgrimes
28411558Srgrimes	/*
28421558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
28431558Srgrimes	 */
28441558Srgrimes	p = line;
28451558Srgrimes	totlen = 0;
28461558Srgrimes	do {
284796622Siedowse		if ((p = fgetln(exp_file, &len)) == NULL)
28481558Srgrimes			return (0);
28491558Srgrimes		cp = p + len - 1;
28501558Srgrimes		cont_line = 0;
28511558Srgrimes		while (cp >= p &&
28521558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
28531558Srgrimes			if (*cp == '\\')
28541558Srgrimes				cont_line = 1;
28551558Srgrimes			cp--;
28561558Srgrimes			len--;
28571558Srgrimes		}
285879117Sdd		if (cont_line) {
285979117Sdd			*++cp = ' ';
286079117Sdd			len++;
286179117Sdd		}
286296622Siedowse		if (linesize < len + totlen + 1) {
286396622Siedowse			linesize = len + totlen + 1;
286496622Siedowse			line = realloc(line, linesize);
286596622Siedowse			if (line == NULL)
286696622Siedowse				out_of_mem();
28671558Srgrimes		}
286896622Siedowse		memcpy(line + totlen, p, len);
286996622Siedowse		totlen += len;
287096622Siedowse		line[totlen] = '\0';
28711558Srgrimes	} while (totlen == 0 || cont_line);
28721558Srgrimes	return (1);
28731558Srgrimes}
28741558Srgrimes
28751558Srgrimes/*
28761558Srgrimes * Parse a description of a credential.
28771558Srgrimes */
2878285128Straszstatic void
2879216587Scharnierparsecred(char *namelist, struct xucred *cr)
28801558Srgrimes{
28811558Srgrimes	char *name;
28821558Srgrimes	int cnt;
28831558Srgrimes	char *names;
28841558Srgrimes	struct passwd *pw;
28851558Srgrimes	struct group *gr;
2886194498Sbrooks	gid_t groups[XU_NGROUPS + 1];
2887136051Sstefanf	int ngroups;
28881558Srgrimes
288991354Sdd	cr->cr_version = XUCRED_VERSION;
28901558Srgrimes	/*
289137663Scharnier	 * Set up the unprivileged user.
28921558Srgrimes	 */
28931558Srgrimes	cr->cr_uid = -2;
28941558Srgrimes	cr->cr_groups[0] = -2;
28951558Srgrimes	cr->cr_ngroups = 1;
28961558Srgrimes	/*
28971558Srgrimes	 * Get the user's password table entry.
28981558Srgrimes	 */
2899293305Sjpaetzel	names = strsep_quote(&namelist, " \t\n");
29001558Srgrimes	name = strsep(&names, ":");
2901293305Sjpaetzel	/* Bug?  name could be NULL here */
29021558Srgrimes	if (isdigit(*name) || *name == '-')
29031558Srgrimes		pw = getpwuid(atoi(name));
29041558Srgrimes	else
29051558Srgrimes		pw = getpwnam(name);
29061558Srgrimes	/*
29071558Srgrimes	 * Credentials specified as those of a user.
29081558Srgrimes	 */
29091558Srgrimes	if (names == NULL) {
29101558Srgrimes		if (pw == NULL) {
291137663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
29121558Srgrimes			return;
29131558Srgrimes		}
29141558Srgrimes		cr->cr_uid = pw->pw_uid;
2915194498Sbrooks		ngroups = XU_NGROUPS + 1;
2916333197Savg		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) {
291737663Scharnier			syslog(LOG_ERR, "too many groups");
2918333197Savg			ngroups = XU_NGROUPS + 1;
2919333197Savg		}
2920333197Savg
29211558Srgrimes		/*
2922136051Sstefanf		 * Compress out duplicate.
29231558Srgrimes		 */
29241558Srgrimes		cr->cr_ngroups = ngroups - 1;
29251558Srgrimes		cr->cr_groups[0] = groups[0];
29261558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
29271558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
29281558Srgrimes		return;
29291558Srgrimes	}
29301558Srgrimes	/*
29311558Srgrimes	 * Explicit credential specified as a colon separated list:
29321558Srgrimes	 *	uid:gid:gid:...
29331558Srgrimes	 */
29341558Srgrimes	if (pw != NULL)
29351558Srgrimes		cr->cr_uid = pw->pw_uid;
29361558Srgrimes	else if (isdigit(*name) || *name == '-')
29371558Srgrimes		cr->cr_uid = atoi(name);
29381558Srgrimes	else {
293937663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
29401558Srgrimes		return;
29411558Srgrimes	}
29421558Srgrimes	cr->cr_ngroups = 0;
2943194498Sbrooks	while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
29441558Srgrimes		name = strsep(&names, ":");
29451558Srgrimes		if (isdigit(*name) || *name == '-') {
29461558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
29471558Srgrimes		} else {
29481558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
294937663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
29501558Srgrimes				continue;
29511558Srgrimes			}
29521558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
29531558Srgrimes		}
29541558Srgrimes	}
2955194498Sbrooks	if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
295637663Scharnier		syslog(LOG_ERR, "too many groups");
29571558Srgrimes}
29581558Srgrimes
2959194880Sdfr#define	STRSIZ	(MNTNAMLEN+MNTPATHLEN+50)
29601558Srgrimes/*
29611558Srgrimes * Routines that maintain the remote mounttab
29621558Srgrimes */
2963285128Straszstatic void
2964216587Scharnierget_mountlist(void)
29651558Srgrimes{
2966324955Smanu	struct mountlist *mlp;
296723681Speter	char *host, *dirp, *cp;
29681558Srgrimes	char str[STRSIZ];
29691558Srgrimes	FILE *mlfile;
29701558Srgrimes
29711558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
297253117Sbillf		if (errno == ENOENT)
297353117Sbillf			return;
297453117Sbillf		else {
297553117Sbillf			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
297653117Sbillf			return;
297753117Sbillf		}
29781558Srgrimes	}
29791558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
298023681Speter		cp = str;
298123681Speter		host = strsep(&cp, " \t\n");
298223681Speter		dirp = strsep(&cp, " \t\n");
298323681Speter		if (host == NULL || dirp == NULL)
29841558Srgrimes			continue;
29851558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
298637663Scharnier		if (mlp == (struct mountlist *)NULL)
298737663Scharnier			out_of_mem();
2988194880Sdfr		strncpy(mlp->ml_host, host, MNTNAMLEN);
2989194880Sdfr		mlp->ml_host[MNTNAMLEN] = '\0';
2990194880Sdfr		strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2991194880Sdfr		mlp->ml_dirp[MNTPATHLEN] = '\0';
2992324955Smanu
2993324955Smanu		SLIST_INSERT_HEAD(&mlhead, mlp, next);
29941558Srgrimes	}
29951558Srgrimes	fclose(mlfile);
29961558Srgrimes}
29971558Srgrimes
2998285128Straszstatic void
299975635Siedowsedel_mlist(char *hostp, char *dirp)
30001558Srgrimes{
3001324955Smanu	struct mountlist *mlp, *mlp2;
30021558Srgrimes	FILE *mlfile;
30031558Srgrimes	int fnd = 0;
30041558Srgrimes
3005324955Smanu	SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
30061558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
30071558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
30081558Srgrimes			fnd = 1;
3009324955Smanu			SLIST_REMOVE(&mlhead, mlp, mountlist, next);
3010324955Smanu			free((caddr_t)mlp);
30111558Srgrimes		}
30121558Srgrimes	}
30131558Srgrimes	if (fnd) {
30141558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
301537663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
30161558Srgrimes			return;
30171558Srgrimes		}
3018324955Smanu		SLIST_FOREACH(mlp, &mlhead, next) {
30191558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
30201558Srgrimes		}
30211558Srgrimes		fclose(mlfile);
30221558Srgrimes	}
30231558Srgrimes}
30241558Srgrimes
3025285128Straszstatic void
3026216587Scharnieradd_mlist(char *hostp, char *dirp)
30271558Srgrimes{
3028324955Smanu	struct mountlist *mlp;
30291558Srgrimes	FILE *mlfile;
30301558Srgrimes
3031324955Smanu	SLIST_FOREACH(mlp, &mlhead, next) {
30321558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
30331558Srgrimes			return;
30341558Srgrimes	}
3035324955Smanu
30361558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
303737663Scharnier	if (mlp == (struct mountlist *)NULL)
303837663Scharnier		out_of_mem();
3039194880Sdfr	strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3040194880Sdfr	mlp->ml_host[MNTNAMLEN] = '\0';
3041194880Sdfr	strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3042194880Sdfr	mlp->ml_dirp[MNTPATHLEN] = '\0';
3043324955Smanu	SLIST_INSERT_HEAD(&mlhead, mlp, next);
30441558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
304537663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
30461558Srgrimes		return;
30471558Srgrimes	}
30481558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
30491558Srgrimes	fclose(mlfile);
30501558Srgrimes}
30511558Srgrimes
30521558Srgrimes/*
30531558Srgrimes * Free up a group list.
30541558Srgrimes */
3055285128Straszstatic void
3056216587Scharnierfree_grp(struct grouplist *grp)
30571558Srgrimes{
30581558Srgrimes	if (grp->gr_type == GT_HOST) {
305974462Salfred		if (grp->gr_ptr.gt_addrinfo != NULL)
306074462Salfred			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
30611558Srgrimes	} else if (grp->gr_type == GT_NET) {
30621558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
30631558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
30641558Srgrimes	}
30651558Srgrimes	free((caddr_t)grp);
30661558Srgrimes}
30671558Srgrimes
30681558Srgrimes#ifdef DEBUG
3069285128Straszstatic void
30701558SrgrimesSYSLOG(int pri, const char *fmt, ...)
30711558Srgrimes{
30721558Srgrimes	va_list ap;
30731558Srgrimes
30741558Srgrimes	va_start(ap, fmt);
30751558Srgrimes	vfprintf(stderr, fmt, ap);
30761558Srgrimes	va_end(ap);
30771558Srgrimes}
30781558Srgrimes#endif /* DEBUG */
30791558Srgrimes
30801558Srgrimes/*
30811558Srgrimes * Check options for consistency.
30821558Srgrimes */
3083285128Straszstatic int
3084216587Scharniercheck_options(struct dirlist *dp)
30851558Srgrimes{
30861558Srgrimes
3087192934Srmacklem	if (v4root_phase == 0 && dp == NULL)
30881558Srgrimes	    return (1);
308983653Speter	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
309083653Speter	    syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
30911558Srgrimes	    return (1);
30921558Srgrimes	}
30931558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
309475801Siedowse		syslog(LOG_ERR, "-mask requires -network");
309575801Siedowse		return (1);
30961558Srgrimes	}
309775801Siedowse	if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
309875801Siedowse		syslog(LOG_ERR, "-network requires mask specification");
309975801Siedowse		return (1);
310075801Siedowse	}
310175801Siedowse	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
310275801Siedowse		syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
310375801Siedowse		return (1);
310475801Siedowse	}
3105192934Srmacklem	if (v4root_phase > 0 &&
3106192934Srmacklem	    (opt_flags &
3107192934Srmacklem	     ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3108192934Srmacklem	    syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3109192934Srmacklem	    return (1);
3110192934Srmacklem	}
3111207689Srmacklem	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3112207689Srmacklem	    syslog(LOG_ERR, "-alldirs has multiple directories");
3113207689Srmacklem	    return (1);
3114207689Srmacklem	}
31151558Srgrimes	return (0);
31161558Srgrimes}
31171558Srgrimes
31181558Srgrimes/*
31191558Srgrimes * Check an absolute directory path for any symbolic links. Return true
31201558Srgrimes */
3121285128Straszstatic int
3122216587Scharniercheck_dirpath(char *dirp)
31231558Srgrimes{
31241558Srgrimes	char *cp;
31251558Srgrimes	int ret = 1;
31261558Srgrimes	struct stat sb;
31271558Srgrimes
31281558Srgrimes	cp = dirp + 1;
31291558Srgrimes	while (*cp && ret) {
31301558Srgrimes		if (*cp == '/') {
31311558Srgrimes			*cp = '\0';
31329336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
31331558Srgrimes				ret = 0;
31341558Srgrimes			*cp = '/';
31351558Srgrimes		}
31361558Srgrimes		cp++;
31371558Srgrimes	}
31389336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
31391558Srgrimes		ret = 0;
31401558Srgrimes	return (ret);
31411558Srgrimes}
31429336Sdfr
314375801Siedowse/*
314475801Siedowse * Make a netmask according to the specified prefix length. The ss_family
314575801Siedowse * and other non-address fields must be initialised before calling this.
314675801Siedowse */
3147285128Straszstatic int
314875801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen)
314974462Salfred{
315075801Siedowse	u_char *p;
315175801Siedowse	int bits, i, len;
315274462Salfred
315375801Siedowse	if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
315475801Siedowse		return (-1);
3155103949Smike	if (bitlen > len * CHAR_BIT)
315675801Siedowse		return (-1);
315774462Salfred
315875801Siedowse	for (i = 0; i < len; i++) {
3159298912Saraujo		bits = MIN(CHAR_BIT, bitlen);
3160219125Sru		*p++ = (u_char)~0 << (CHAR_BIT - bits);
316175801Siedowse		bitlen -= bits;
316274462Salfred	}
316375801Siedowse	return 0;
316474462Salfred}
316574462Salfred
316675801Siedowse/*
316775801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask
316875801Siedowse * is acceptable (i.e. of the form 1...10....0).
316975801Siedowse */
3170285128Straszstatic int
317175801Siedowsecheckmask(struct sockaddr *sa)
317274462Salfred{
317375801Siedowse	u_char *mask;
317475801Siedowse	int i, len;
317574462Salfred
317675801Siedowse	if ((mask = sa_rawaddr(sa, &len)) == NULL)
317775801Siedowse		return (-1);
317875801Siedowse
317975801Siedowse	for (i = 0; i < len; i++)
318075801Siedowse		if (mask[i] != 0xff)
318175801Siedowse			break;
318275801Siedowse	if (i < len) {
318375801Siedowse		if (~mask[i] & (u_char)(~mask[i] + 1))
318475801Siedowse			return (-1);
318575801Siedowse		i++;
318674462Salfred	}
318775801Siedowse	for (; i < len; i++)
318875801Siedowse		if (mask[i] != 0)
318975801Siedowse			return (-1);
319075801Siedowse	return (0);
319174462Salfred}
319274462Salfred
319375801Siedowse/*
319475801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if
319575801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3196228990Suqs * If samask is NULL, perform a full comparison.
319775801Siedowse */
3198285128Straszstatic int
319975801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
320074462Salfred{
320175801Siedowse	unsigned char *p1, *p2, *mask;
320275801Siedowse	int len, i;
320374462Salfred
320475801Siedowse	if (sa1->sa_family != sa2->sa_family ||
320575801Siedowse	    (p1 = sa_rawaddr(sa1, &len)) == NULL ||
320675801Siedowse	    (p2 = sa_rawaddr(sa2, NULL)) == NULL)
320775801Siedowse		return (1);
320875801Siedowse
320975801Siedowse	switch (sa1->sa_family) {
321074462Salfred	case AF_INET6:
321175801Siedowse		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
321275801Siedowse		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
321375801Siedowse			return (1);
321474462Salfred		break;
321574462Salfred	}
321674462Salfred
321775801Siedowse	/* Simple binary comparison if no mask specified. */
321875801Siedowse	if (samask == NULL)
321975801Siedowse		return (memcmp(p1, p2, len));
322074462Salfred
322175801Siedowse	/* Set up the mask, and do a mask-based comparison. */
322275801Siedowse	if (sa1->sa_family != samask->sa_family ||
322375801Siedowse	    (mask = sa_rawaddr(samask, NULL)) == NULL)
322475801Siedowse		return (1);
322574462Salfred
322675801Siedowse	for (i = 0; i < len; i++)
322775801Siedowse		if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
322875801Siedowse			return (1);
322975801Siedowse	return (0);
323074462Salfred}
323174462Salfred
323275801Siedowse/*
323375801Siedowse * Return a pointer to the part of the sockaddr that contains the
323475801Siedowse * raw address, and set *nbytes to its length in bytes. Returns
323575801Siedowse * NULL if the address family is unknown.
323675801Siedowse */
3237285128Straszstatic void *
323875801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) {
323975801Siedowse	void *p;
324074462Salfred	int len;
324174462Salfred
324275801Siedowse	switch (sa->sa_family) {
324374462Salfred	case AF_INET:
324475801Siedowse		len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
324575801Siedowse		p = &((struct sockaddr_in *)sa)->sin_addr;
324674462Salfred		break;
324774462Salfred	case AF_INET6:
324875801Siedowse		len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
324975801Siedowse		p = &((struct sockaddr_in6 *)sa)->sin6_addr;
325074462Salfred		break;
325174462Salfred	default:
325275801Siedowse		p = NULL;
325375801Siedowse		len = 0;
325474462Salfred	}
325574462Salfred
325675801Siedowse	if (nbytes != NULL)
325775801Siedowse		*nbytes = len;
325875801Siedowse	return (p);
325974462Salfred}
326074462Salfred
3261285128Straszstatic void
3262216587Scharnierhuphandler(int sig __unused)
326375754Siedowse{
3264285128Strasz
326575754Siedowse	got_sighup = 1;
326675754Siedowse}
326775754Siedowse
3268285128Straszstatic void
3269285128Straszterminate(int sig __unused)
327074462Salfred{
3271149433Spjd	pidfile_remove(pfh);
3272194880Sdfr	rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3273194880Sdfr	rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
327474462Salfred	exit (0);
327574462Salfred}
3276