11558Srgrimes/*
21558Srgrimes * Copyright (c) 1989, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * This code is derived from software contributed to Berkeley by
61558Srgrimes * Herb Hasler and Rick Macklem at The University of Guelph.
71558Srgrimes *
81558Srgrimes * Redistribution and use in source and binary forms, with or without
91558Srgrimes * modification, are permitted provided that the following conditions
101558Srgrimes * are met:
111558Srgrimes * 1. Redistributions of source code must retain the above copyright
121558Srgrimes *    notice, this list of conditions and the following disclaimer.
131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer in the
151558Srgrimes *    documentation and/or other materials provided with the distribution.
161558Srgrimes * 4. Neither the name of the University nor the names of its contributors
171558Srgrimes *    may be used to endorse or promote products derived from this software
181558Srgrimes *    without specific prior written permission.
191558Srgrimes *
201558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301558Srgrimes * SUCH DAMAGE.
311558Srgrimes */
321558Srgrimes
331558Srgrimes#ifndef lint
3437663Scharnierstatic const char copyright[] =
351558Srgrimes"@(#) Copyright (c) 1989, 1993\n\
361558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
372999Swollman#endif /*not lint*/
381558Srgrimes
39105267Scharnier#if 0
401558Srgrimes#ifndef lint
4137663Scharnierstatic char sccsid[] = "@(#)mountd.c	8.15 (Berkeley) 5/1/95";
42105267Scharnier#endif /*not lint*/
4337663Scharnier#endif
441558Srgrimes
45105267Scharnier#include <sys/cdefs.h>
46105267Scharnier__FBSDID("$FreeBSD$");
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>
531558Srgrimes#include <sys/stat.h>
54192934Srmacklem#include <sys/sysctl.h>
551558Srgrimes#include <sys/syslog.h>
561558Srgrimes
571558Srgrimes#include <rpc/rpc.h>
58109363Smbr#include <rpc/rpc_com.h>
591558Srgrimes#include <rpc/pmap_clnt.h>
6074462Salfred#include <rpc/pmap_prot.h>
6174462Salfred#include <rpcsvc/mount.h>
629336Sdfr#include <nfs/nfsproto.h>
63192934Srmacklem#include <nfs/nfssvc.h>
6483653Speter#include <nfsserver/nfs.h>
651558Srgrimes
66192934Srmacklem#include <fs/nfs/nfsport.h>
67192934Srmacklem
681558Srgrimes#include <arpa/inet.h>
691558Srgrimes
701558Srgrimes#include <ctype.h>
7137663Scharnier#include <err.h>
721558Srgrimes#include <errno.h>
731558Srgrimes#include <grp.h>
74149433Spjd#include <libutil.h>
75103949Smike#include <limits.h>
761558Srgrimes#include <netdb.h>
771558Srgrimes#include <pwd.h>
781558Srgrimes#include <signal.h>
791558Srgrimes#include <stdio.h>
801558Srgrimes#include <stdlib.h>
811558Srgrimes#include <string.h>
821558Srgrimes#include <unistd.h>
831558Srgrimes#include "pathnames.h"
84158857Srodrigc#include "mntopts.h"
851558Srgrimes
861558Srgrimes#ifdef DEBUG
871558Srgrimes#include <stdarg.h>
881558Srgrimes#endif
891558Srgrimes
901558Srgrimes/*
911558Srgrimes * Structures for keeping the mount list and export list
921558Srgrimes */
931558Srgrimesstruct mountlist {
941558Srgrimes	struct mountlist *ml_next;
95194880Sdfr	char	ml_host[MNTNAMLEN+1];
96194880Sdfr	char	ml_dirp[MNTPATHLEN+1];
971558Srgrimes};
981558Srgrimes
991558Srgrimesstruct dirlist {
1001558Srgrimes	struct dirlist	*dp_left;
1011558Srgrimes	struct dirlist	*dp_right;
1021558Srgrimes	int		dp_flag;
1031558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
1041558Srgrimes	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1051558Srgrimes};
1061558Srgrimes/* dp_flag bits */
1071558Srgrimes#define	DP_DEFSET	0x1
1089336Sdfr#define DP_HOSTSET	0x2
1091558Srgrimes
1101558Srgrimesstruct exportlist {
1111558Srgrimes	struct exportlist *ex_next;
1121558Srgrimes	struct dirlist	*ex_dirl;
1131558Srgrimes	struct dirlist	*ex_defdir;
1141558Srgrimes	int		ex_flag;
1151558Srgrimes	fsid_t		ex_fs;
1161558Srgrimes	char		*ex_fsdir;
11727447Sdfr	char		*ex_indexfile;
118184588Sdfr	int		ex_numsecflavors;
119184588Sdfr	int		ex_secflavors[MAXSECFLAVORS];
120240902Srmacklem	int		ex_defnumsecflavors;
121240902Srmacklem	int		ex_defsecflavors[MAXSECFLAVORS];
1221558Srgrimes};
1231558Srgrimes/* ex_flag bits */
1241558Srgrimes#define	EX_LINKED	0x1
1251558Srgrimes
1261558Srgrimesstruct netmsk {
12774462Salfred	struct sockaddr_storage nt_net;
12875801Siedowse	struct sockaddr_storage nt_mask;
12942144Sdfr	char		*nt_name;
1301558Srgrimes};
1311558Srgrimes
1321558Srgrimesunion grouptypes {
13374462Salfred	struct addrinfo *gt_addrinfo;
1341558Srgrimes	struct netmsk	gt_net;
1351558Srgrimes};
1361558Srgrimes
1371558Srgrimesstruct grouplist {
1381558Srgrimes	int gr_type;
1391558Srgrimes	union grouptypes gr_ptr;
1401558Srgrimes	struct grouplist *gr_next;
141240902Srmacklem	int gr_numsecflavors;
142240902Srmacklem	int gr_secflavors[MAXSECFLAVORS];
1431558Srgrimes};
1441558Srgrimes/* Group types */
1451558Srgrimes#define	GT_NULL		0x0
1461558Srgrimes#define	GT_HOST		0x1
1471558Srgrimes#define	GT_NET		0x2
14875641Siedowse#define	GT_DEFAULT	0x3
1497401Swpaul#define GT_IGNORE	0x5
1501558Srgrimes
1511558Srgrimesstruct hostlist {
1529336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1531558Srgrimes	struct grouplist *ht_grp;
1541558Srgrimes	struct hostlist	 *ht_next;
1551558Srgrimes};
1561558Srgrimes
1579336Sdfrstruct fhreturn {
1589336Sdfr	int	fhr_flag;
1599336Sdfr	int	fhr_vers;
1609336Sdfr	nfsfh_t	fhr_fh;
161184588Sdfr	int	fhr_numsecflavors;
162184588Sdfr	int	*fhr_secflavors;
1639336Sdfr};
1649336Sdfr
165222623Srmacklem#define	GETPORT_MAXTRY	20	/* Max tries to get a port # */
166222623Srmacklem
1671558Srgrimes/* Global defs */
16892882Simpchar	*add_expdir(struct dirlist **, char *, int);
16992882Simpvoid	add_dlist(struct dirlist **, struct dirlist *,
170240902Srmacklem				struct grouplist *, int, struct exportlist *);
17192882Simpvoid	add_mlist(char *, char *);
17292882Simpint	check_dirpath(char *);
17392882Simpint	check_options(struct dirlist *);
17475801Siedowseint	checkmask(struct sockaddr *sa);
175240902Srmacklemint	chk_host(struct dirlist *, struct sockaddr *, int *, int *, int *,
176240902Srmacklem				 int **);
177222623Srmacklemstatic int	create_service(struct netconfig *nconf);
178222623Srmacklemstatic void	complete_service(struct netconfig *nconf, char *port_str);
179222623Srmacklemstatic void	clearout_service(void);
18075635Siedowsevoid	del_mlist(char *hostp, char *dirp);
18192882Simpstruct dirlist *dirp_search(struct dirlist *, char *);
18292882Simpint	do_mount(struct exportlist *, struct grouplist *, int,
18392882Simp		struct xucred *, char *, int, struct statfs *);
18492882Simpint	do_opt(char **, char **, struct exportlist *, struct grouplist *,
18592882Simp				int *, int *, struct xucred *);
18692882Simpstruct	exportlist *ex_search(fsid_t *);
18792882Simpstruct	exportlist *get_exp(void);
18892882Simpvoid	free_dir(struct dirlist *);
18992882Simpvoid	free_exp(struct exportlist *);
19092882Simpvoid	free_grp(struct grouplist *);
19192882Simpvoid	free_host(struct hostlist *);
19292882Simpvoid	get_exportlist(void);
19392882Simpint	get_host(char *, struct grouplist *, struct grouplist *);
19492882Simpstruct hostlist *get_ht(void);
19592882Simpint	get_line(void);
19692882Simpvoid	get_mountlist(void);
19792882Simpint	get_net(char *, struct netmsk *, int);
19892882Simpvoid	getexp_err(struct exportlist *, struct grouplist *);
19992882Simpstruct grouplist *get_grp(void);
20092882Simpvoid	hang_dirp(struct dirlist *, struct grouplist *,
20192882Simp				struct exportlist *, int);
20275754Siedowsevoid	huphandler(int sig);
20375801Siedowseint	makemask(struct sockaddr_storage *ssp, int bitlen);
20492882Simpvoid	mntsrv(struct svc_req *, SVCXPRT *);
20592882Simpvoid	nextfield(char **, char **);
20692882Simpvoid	out_of_mem(void);
20792882Simpvoid	parsecred(char *, struct xucred *);
208216587Scharnierint	parsesec(char *, struct exportlist *);
209100117Salfredint	put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
21075801Siedowsevoid	*sa_rawaddr(struct sockaddr *sa, int *nbytes);
21175801Siedowseint	sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
21275801Siedowse    struct sockaddr *samask);
21392882Simpint	scan_tree(struct dirlist *, struct sockaddr *);
21492882Simpstatic void usage(void);
21592882Simpint	xdr_dir(XDR *, char *);
21692882Simpint	xdr_explist(XDR *, caddr_t);
217100117Salfredint	xdr_explist_brief(XDR *, caddr_t);
218216587Scharnierint	xdr_explist_common(XDR *, caddr_t, int);
21992882Simpint	xdr_fhs(XDR *, caddr_t);
22092882Simpint	xdr_mlist(XDR *, caddr_t);
22192882Simpvoid	terminate(int);
2221558Srgrimes
2231558Srgrimesstruct exportlist *exphead;
2241558Srgrimesstruct mountlist *mlhead;
2251558Srgrimesstruct grouplist *grphead;
226166440Spjdchar *exnames_default[2] = { _PATH_EXPORTS, NULL };
227166440Spjdchar **exnames;
228172827Smatteochar **hosts = NULL;
22972650Sgreenstruct xucred def_anon = {
23091354Sdd	XUCRED_VERSION,
23172650Sgreen	(uid_t)-2,
2321558Srgrimes	1,
23372650Sgreen	{ (gid_t)-2 },
23472650Sgreen	NULL
2351558Srgrimes};
23625087Sdfrint force_v2 = 0;
2379336Sdfrint resvport_only = 1;
238172827Smatteoint nhosts = 0;
2399336Sdfrint dir_only = 1;
240121767Speterint dolog = 0;
24175754Siedowseint got_sighup = 0;
242172827Smatteoint xcreated = 0;
24374462Salfred
244172827Smatteochar *svcport_str = NULL;
245222623Srmacklemstatic int	mallocd_svcport = 0;
246222623Srmacklemstatic int	*sock_fd;
247222623Srmacklemstatic int	sock_fdcnt;
248222623Srmacklemstatic int	sock_fdpos;
249241568Srmacklemstatic int	suspend_nfsd = 0;
250172827Smatteo
2511558Srgrimesint opt_flags;
25274462Salfredstatic int have_v6 = 1;
25374462Salfred
254192934Srmacklemint v4root_phase = 0;
255192934Srmacklemchar v4root_dirpath[PATH_MAX + 1];
256220980Srmacklemint run_v4server = 1;
257192934Srmacklemint has_publicfh = 0;
258192934Srmacklem
259149433Spjdstruct pidfh *pfh = NULL;
26075801Siedowse/* Bits for opt_flags above */
2611558Srgrimes#define	OP_MAPROOT	0x01
2621558Srgrimes#define	OP_MAPALL	0x02
26383653Speter/* 0x4 free */
2641558Srgrimes#define	OP_MASK		0x08
2651558Srgrimes#define	OP_NET		0x10
2661558Srgrimes#define	OP_ALLDIRS	0x40
26775801Siedowse#define	OP_HAVEMASK	0x80	/* A mask was specified or inferred. */
268100336Sjoerg#define	OP_QUIET	0x100
26974462Salfred#define OP_MASKLEN	0x200
270184588Sdfr#define OP_SEC		0x400
2711558Srgrimes
2721558Srgrimes#ifdef DEBUG
2731558Srgrimesint debug = 1;
27492882Simpvoid	SYSLOG(int, const char *, ...) __printflike(2, 3);
2751558Srgrimes#define syslog SYSLOG
2761558Srgrimes#else
2771558Srgrimesint debug = 0;
2781558Srgrimes#endif
2791558Srgrimes
2801558Srgrimes/*
2811558Srgrimes * Mountd server for NFS mount protocol as described in:
2821558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2831558Srgrimes * The optional arguments are the exports file name
2841558Srgrimes * default: _PATH_EXPORTS
2851558Srgrimes * and "-n" to allow nonroot mount.
2861558Srgrimes */
2871558Srgrimesint
288216587Scharniermain(int argc, char **argv)
2891558Srgrimes{
29075754Siedowse	fd_set readfds;
291172827Smatteo	struct netconfig *nconf;
292172827Smatteo	char *endptr, **hosts_bak;
293172827Smatteo	void *nc_handle;
294149433Spjd	pid_t otherpid;
295172827Smatteo	in_port_t svcport;
296172827Smatteo	int c, k, s;
297109363Smbr	int maxrec = RPC_MAXDATASIZE;
298222623Srmacklem	int attempt_cnt, port_len, port_pos, ret;
299222623Srmacklem	char **port_list;
3001558Srgrimes
30174462Salfred	/* Check that another mountd isn't already running. */
302150214Spjd	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
303149433Spjd	if (pfh == NULL) {
304149433Spjd		if (errno == EEXIST)
305149433Spjd			errx(1, "mountd already running, pid: %d.", otherpid);
306149433Spjd		warn("cannot open or create pidfile");
307149433Spjd	}
30874462Salfred
30974462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
31074462Salfred	if (s < 0)
31174462Salfred		have_v6 = 0;
31274462Salfred	else
31374462Salfred		close(s);
3142999Swollman
315241568Srmacklem	while ((c = getopt(argc, argv, "2deh:lnop:rS")) != -1)
3161558Srgrimes		switch (c) {
31725087Sdfr		case '2':
31825087Sdfr			force_v2 = 1;
31925087Sdfr			break;
320192993Srmacklem		case 'e':
321220980Srmacklem			/* now a no-op, since this is the default */
322192934Srmacklem			break;
3239336Sdfr		case 'n':
3249336Sdfr			resvport_only = 0;
3259336Sdfr			break;
3269336Sdfr		case 'r':
3279336Sdfr			dir_only = 0;
3289336Sdfr			break;
3298688Sphk		case 'd':
3308688Sphk			debug = debug ? 0 : 1;
3318688Sphk			break;
33231656Sguido		case 'l':
333121767Speter			dolog = 1;
33431656Sguido			break;
335220980Srmacklem		case 'o':
336220980Srmacklem			run_v4server = 0;
337220980Srmacklem			break;
338126572Sbms		case 'p':
339126572Sbms			endptr = NULL;
340126572Sbms			svcport = (in_port_t)strtoul(optarg, &endptr, 10);
341126572Sbms			if (endptr == NULL || *endptr != '\0' ||
342126572Sbms			    svcport == 0 || svcport >= IPPORT_MAX)
343126572Sbms				usage();
344172827Smatteo			svcport_str = strdup(optarg);
345126572Sbms			break;
346172827Smatteo		case 'h':
347172827Smatteo			++nhosts;
348172827Smatteo			hosts_bak = hosts;
349172827Smatteo			hosts_bak = realloc(hosts, nhosts * sizeof(char *));
350172827Smatteo			if (hosts_bak == NULL) {
351172827Smatteo				if (hosts != NULL) {
352172827Smatteo					for (k = 0; k < nhosts; k++)
353172827Smatteo						free(hosts[k]);
354172827Smatteo					free(hosts);
355172827Smatteo					out_of_mem();
356172827Smatteo				}
357172827Smatteo			}
358172827Smatteo			hosts = hosts_bak;
359172827Smatteo			hosts[nhosts - 1] = strdup(optarg);
360172827Smatteo			if (hosts[nhosts - 1] == NULL) {
361172827Smatteo				for (k = 0; k < (nhosts - 1); k++)
362172827Smatteo					free(hosts[k]);
363172827Smatteo				free(hosts);
364172827Smatteo				out_of_mem();
365172827Smatteo			}
366172827Smatteo			break;
367241568Srmacklem		case 'S':
368241568Srmacklem			suspend_nfsd = 1;
369241568Srmacklem			break;
3701558Srgrimes		default:
37137663Scharnier			usage();
3721558Srgrimes		};
373192934Srmacklem
374192934Srmacklem	/*
375220980Srmacklem	 * Unless the "-o" option was specified, try and run "nfsd".
376220980Srmacklem	 * If "-o" was specified, try and run "nfsserver".
377192934Srmacklem	 */
378192934Srmacklem	if (run_v4server > 0) {
379192934Srmacklem		if (modfind("nfsd") < 0) {
380192934Srmacklem			/* Not present in kernel, try loading it */
381192934Srmacklem			if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
382192934Srmacklem				errx(1, "NFS server is not available");
383192934Srmacklem		}
384192934Srmacklem	} else if (modfind("nfsserver") < 0) {
385192934Srmacklem		/* Not present in kernel, try loading it */
386192934Srmacklem		if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
387192934Srmacklem			errx(1, "NFS server is not available");
388192934Srmacklem	}
389192934Srmacklem
3901558Srgrimes	argc -= optind;
3911558Srgrimes	argv += optind;
3921558Srgrimes	grphead = (struct grouplist *)NULL;
3931558Srgrimes	exphead = (struct exportlist *)NULL;
3941558Srgrimes	mlhead = (struct mountlist *)NULL;
395166440Spjd	if (argc > 0)
396166440Spjd		exnames = argv;
397166440Spjd	else
398166440Spjd		exnames = exnames_default;
3991558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
4001558Srgrimes	if (debug)
40137663Scharnier		warnx("getting export list");
4021558Srgrimes	get_exportlist();
4031558Srgrimes	if (debug)
40437663Scharnier		warnx("getting mount list");
4051558Srgrimes	get_mountlist();
4061558Srgrimes	if (debug)
40737663Scharnier		warnx("here we go");
4081558Srgrimes	if (debug == 0) {
4091558Srgrimes		daemon(0, 0);
4101558Srgrimes		signal(SIGINT, SIG_IGN);
4111558Srgrimes		signal(SIGQUIT, SIG_IGN);
4121558Srgrimes	}
41375754Siedowse	signal(SIGHUP, huphandler);
41474462Salfred	signal(SIGTERM, terminate);
415164394Srodrigc	signal(SIGPIPE, SIG_IGN);
416149433Spjd
417149433Spjd	pidfile_write(pfh);
418149433Spjd
419194880Sdfr	rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
420194880Sdfr	rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
421109363Smbr	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
422109363Smbr
42324759Sguido	if (!resvport_only) {
42483687Speter		if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
42583687Speter		    &resvport_only, sizeof(resvport_only)) != 0 &&
42683687Speter		    errno != ENOENT) {
42724759Sguido			syslog(LOG_ERR, "sysctl: %m");
42824759Sguido			exit(1);
42924759Sguido		}
43024330Sguido	}
431126572Sbms
432172827Smatteo	/*
433172827Smatteo	 * If no hosts were specified, add a wildcard entry to bind to
434172827Smatteo	 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
435172827Smatteo	 * list.
436172827Smatteo	 */
437172827Smatteo	if (nhosts == 0) {
438172827Smatteo		hosts = malloc(sizeof(char**));
439172827Smatteo		if (hosts == NULL)
440172827Smatteo			out_of_mem();
441172827Smatteo		hosts[0] = "*";
442172827Smatteo		nhosts = 1;
443172827Smatteo	} else {
444172827Smatteo		hosts_bak = hosts;
445172827Smatteo		if (have_v6) {
446172827Smatteo			hosts_bak = realloc(hosts, (nhosts + 2) *
447172827Smatteo			    sizeof(char *));
448172827Smatteo			if (hosts_bak == NULL) {
449172827Smatteo				for (k = 0; k < nhosts; k++)
450172827Smatteo					free(hosts[k]);
451172827Smatteo		    		free(hosts);
452172827Smatteo		    		out_of_mem();
453172827Smatteo			} else
454172827Smatteo				hosts = hosts_bak;
455172827Smatteo			nhosts += 2;
456172827Smatteo			hosts[nhosts - 2] = "::1";
457172827Smatteo		} else {
458172827Smatteo			hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
459172827Smatteo			if (hosts_bak == NULL) {
460172827Smatteo				for (k = 0; k < nhosts; k++)
461172827Smatteo					free(hosts[k]);
462172827Smatteo				free(hosts);
463172827Smatteo				out_of_mem();
464172827Smatteo			} else {
465172827Smatteo				nhosts += 1;
466172827Smatteo				hosts = hosts_bak;
467126572Sbms			}
468172827Smatteo		}
46974462Salfred
470172827Smatteo		hosts[nhosts - 1] = "127.0.0.1";
47174462Salfred	}
47274462Salfred
473222623Srmacklem	attempt_cnt = 1;
474222623Srmacklem	sock_fdcnt = 0;
475222623Srmacklem	sock_fd = NULL;
476222623Srmacklem	port_list = NULL;
477222623Srmacklem	port_len = 0;
478172827Smatteo	nc_handle = setnetconfig();
479172827Smatteo	while ((nconf = getnetconfig(nc_handle))) {
480172827Smatteo		if (nconf->nc_flag & NC_VISIBLE) {
481172827Smatteo			if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
482172827Smatteo			    "inet6") == 0) {
483172827Smatteo				/* DO NOTHING */
484222623Srmacklem			} else {
485222623Srmacklem				ret = create_service(nconf);
486222623Srmacklem				if (ret == 1)
487222623Srmacklem					/* Ignore this call */
488222623Srmacklem					continue;
489222623Srmacklem				if (ret < 0) {
490222623Srmacklem					/*
491222623Srmacklem					 * Failed to bind port, so close off
492222623Srmacklem					 * all sockets created and try again
493222623Srmacklem					 * if the port# was dynamically
494222623Srmacklem					 * assigned via bind(2).
495222623Srmacklem					 */
496222623Srmacklem					clearout_service();
497222623Srmacklem					if (mallocd_svcport != 0 &&
498222623Srmacklem					    attempt_cnt < GETPORT_MAXTRY) {
499222623Srmacklem						free(svcport_str);
500222623Srmacklem						svcport_str = NULL;
501222623Srmacklem						mallocd_svcport = 0;
502222623Srmacklem					} else {
503222623Srmacklem						errno = EADDRINUSE;
504222623Srmacklem						syslog(LOG_ERR,
505222623Srmacklem						    "bindresvport_sa: %m");
506222623Srmacklem						exit(1);
507222623Srmacklem					}
508222623Srmacklem
509222623Srmacklem					/* Start over at the first service. */
510222623Srmacklem					free(sock_fd);
511222623Srmacklem					sock_fdcnt = 0;
512222623Srmacklem					sock_fd = NULL;
513222623Srmacklem					nc_handle = setnetconfig();
514222623Srmacklem					attempt_cnt++;
515222623Srmacklem				} else if (mallocd_svcport != 0 &&
516222623Srmacklem				    attempt_cnt == GETPORT_MAXTRY) {
517222623Srmacklem					/*
518222623Srmacklem					 * For the last attempt, allow
519222623Srmacklem					 * different port #s for each nconf
520222623Srmacklem					 * by saving the svcport_str and
521222623Srmacklem					 * setting it back to NULL.
522222623Srmacklem					 */
523222623Srmacklem					port_list = realloc(port_list,
524222623Srmacklem					    (port_len + 1) * sizeof(char *));
525222623Srmacklem					if (port_list == NULL)
526222623Srmacklem						out_of_mem();
527222623Srmacklem					port_list[port_len++] = svcport_str;
528222623Srmacklem					svcport_str = NULL;
529222623Srmacklem					mallocd_svcport = 0;
530222623Srmacklem				}
531222623Srmacklem			}
532222623Srmacklem		}
533222623Srmacklem	}
534222623Srmacklem
535222623Srmacklem	/*
536222623Srmacklem	 * Successfully bound the ports, so call complete_service() to
537222623Srmacklem	 * do the rest of the setup on the service(s).
538222623Srmacklem	 */
539222623Srmacklem	sock_fdpos = 0;
540222623Srmacklem	port_pos = 0;
541222623Srmacklem	nc_handle = setnetconfig();
542222623Srmacklem	while ((nconf = getnetconfig(nc_handle))) {
543222623Srmacklem		if (nconf->nc_flag & NC_VISIBLE) {
544222623Srmacklem			if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
545222623Srmacklem			    "inet6") == 0) {
546222623Srmacklem				/* DO NOTHING */
547222623Srmacklem			} else if (port_list != NULL) {
548222623Srmacklem				if (port_pos >= port_len) {
549222623Srmacklem					syslog(LOG_ERR, "too many port#s");
550222623Srmacklem					exit(1);
551222623Srmacklem				}
552222623Srmacklem				complete_service(nconf, port_list[port_pos++]);
553172827Smatteo			} else
554222623Srmacklem				complete_service(nconf, svcport_str);
555172827Smatteo		}
55674462Salfred	}
557172827Smatteo	endnetconfig(nc_handle);
558222623Srmacklem	free(sock_fd);
559222623Srmacklem	if (port_list != NULL) {
560222623Srmacklem		for (port_pos = 0; port_pos < port_len; port_pos++)
561222623Srmacklem			free(port_list[port_pos]);
562222623Srmacklem		free(port_list);
563222623Srmacklem	}
56474462Salfred
56574462Salfred	if (xcreated == 0) {
56674462Salfred		syslog(LOG_ERR, "could not create any services");
5671558Srgrimes		exit(1);
5681558Srgrimes	}
56975754Siedowse
57075754Siedowse	/* Expand svc_run() here so that we can call get_exportlist(). */
57175754Siedowse	for (;;) {
57275754Siedowse		if (got_sighup) {
57375754Siedowse			get_exportlist();
57475754Siedowse			got_sighup = 0;
57575754Siedowse		}
57675754Siedowse		readfds = svc_fdset;
57775754Siedowse		switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
57875754Siedowse		case -1:
57975754Siedowse			if (errno == EINTR)
58075754Siedowse                                continue;
58175754Siedowse			syslog(LOG_ERR, "mountd died: select: %m");
58275754Siedowse			exit(1);
58375754Siedowse		case 0:
58475754Siedowse			continue;
58575754Siedowse		default:
58675754Siedowse			svc_getreqset(&readfds);
58775754Siedowse		}
58875754Siedowse	}
589172827Smatteo}
590172827Smatteo
591172827Smatteo/*
592172827Smatteo * This routine creates and binds sockets on the appropriate
593222623Srmacklem * addresses. It gets called one time for each transport.
594222623Srmacklem * It returns 0 upon success, 1 for ingore the call and -1 to indicate
595222623Srmacklem * bind failed with EADDRINUSE.
596222623Srmacklem * Any file descriptors that have been created are stored in sock_fd and
597222623Srmacklem * the total count of them is maintained in sock_fdcnt.
598172827Smatteo */
599222623Srmacklemstatic int
600172827Smatteocreate_service(struct netconfig *nconf)
601172827Smatteo{
602172827Smatteo	struct addrinfo hints, *res = NULL;
603172827Smatteo	struct sockaddr_in *sin;
604172827Smatteo	struct sockaddr_in6 *sin6;
605172827Smatteo	struct __rpc_sockinfo si;
606172827Smatteo	int aicode;
607172827Smatteo	int fd;
608172827Smatteo	int nhostsbak;
609172827Smatteo	int one = 1;
610172827Smatteo	int r;
611172827Smatteo	u_int32_t host_addr[4];  /* IPv4 or IPv6 */
612222623Srmacklem	int mallocd_res;
613172827Smatteo
614172827Smatteo	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
615172827Smatteo	    (nconf->nc_semantics != NC_TPI_COTS) &&
616172827Smatteo	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
617222623Srmacklem		return (1);	/* not my type */
618172827Smatteo
619172827Smatteo	/*
620172827Smatteo	 * XXX - using RPC library internal functions.
621172827Smatteo	 */
622172827Smatteo	if (!__rpc_nconf2sockinfo(nconf, &si)) {
623172827Smatteo		syslog(LOG_ERR, "cannot get information for %s",
624172827Smatteo		    nconf->nc_netid);
625222623Srmacklem		return (1);
626172827Smatteo	}
627172827Smatteo
628172827Smatteo	/* Get mountd's address on this transport */
629172827Smatteo	memset(&hints, 0, sizeof hints);
630172827Smatteo	hints.ai_flags = AI_PASSIVE;
631172827Smatteo	hints.ai_family = si.si_af;
632172827Smatteo	hints.ai_socktype = si.si_socktype;
633172827Smatteo	hints.ai_protocol = si.si_proto;
634172827Smatteo
635172827Smatteo	/*
636172827Smatteo	 * Bind to specific IPs if asked to
637172827Smatteo	 */
638172827Smatteo	nhostsbak = nhosts;
639172827Smatteo	while (nhostsbak > 0) {
640172827Smatteo		--nhostsbak;
641222623Srmacklem		sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
642222623Srmacklem		if (sock_fd == NULL)
643222623Srmacklem			out_of_mem();
644222623Srmacklem		sock_fd[sock_fdcnt++] = -1;	/* Set invalid for now. */
645222623Srmacklem		mallocd_res = 0;
646222623Srmacklem
647172827Smatteo		/*
648172827Smatteo		 * XXX - using RPC library internal functions.
649172827Smatteo		 */
650172827Smatteo		if ((fd = __rpc_nconf2fd(nconf)) < 0) {
651172827Smatteo			int non_fatal = 0;
652244538Skevlo	    		if (errno == EAFNOSUPPORT &&
653172827Smatteo			    nconf->nc_semantics != NC_TPI_CLTS)
654172827Smatteo				non_fatal = 1;
655172827Smatteo
656172827Smatteo			syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
657172827Smatteo			    "cannot create socket for %s", nconf->nc_netid);
658222623Srmacklem			if (non_fatal != 0)
659222623Srmacklem				continue;
660222623Srmacklem			exit(1);
661172827Smatteo		}
662172827Smatteo
663172827Smatteo		switch (hints.ai_family) {
664172827Smatteo		case AF_INET:
665172827Smatteo			if (inet_pton(AF_INET, hosts[nhostsbak],
666172827Smatteo			    host_addr) == 1) {
667222623Srmacklem				hints.ai_flags |= AI_NUMERICHOST;
668172827Smatteo			} else {
669172827Smatteo				/*
670172827Smatteo				 * Skip if we have an AF_INET6 address.
671172827Smatteo				 */
672172827Smatteo				if (inet_pton(AF_INET6, hosts[nhostsbak],
673172827Smatteo				    host_addr) == 1) {
674172827Smatteo					close(fd);
675172827Smatteo					continue;
676172827Smatteo				}
677172827Smatteo			}
678172827Smatteo			break;
679172827Smatteo		case AF_INET6:
680172827Smatteo			if (inet_pton(AF_INET6, hosts[nhostsbak],
681172827Smatteo			    host_addr) == 1) {
682222623Srmacklem				hints.ai_flags |= AI_NUMERICHOST;
683172827Smatteo			} else {
684172827Smatteo				/*
685172827Smatteo				 * Skip if we have an AF_INET address.
686172827Smatteo				 */
687172827Smatteo				if (inet_pton(AF_INET, hosts[nhostsbak],
688172827Smatteo				    host_addr) == 1) {
689172827Smatteo					close(fd);
690172827Smatteo					continue;
691172827Smatteo				}
692172827Smatteo			}
693172827Smatteo
694172827Smatteo			/*
695172827Smatteo			 * We're doing host-based access checks here, so don't
696172827Smatteo			 * allow v4-in-v6 to confuse things. The kernel will
697172827Smatteo			 * disable it by default on NFS sockets too.
698172827Smatteo			 */
699172827Smatteo			if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
700172827Smatteo			    sizeof one) < 0) {
701172827Smatteo				syslog(LOG_ERR,
702172827Smatteo				    "can't disable v4-in-v6 on IPv6 socket");
703172827Smatteo				exit(1);
704172827Smatteo			}
705172827Smatteo			break;
706172827Smatteo		default:
707172827Smatteo			break;
708172827Smatteo		}
709172827Smatteo
710172827Smatteo		/*
711172827Smatteo		 * If no hosts were specified, just bind to INADDR_ANY
712172827Smatteo		 */
713172827Smatteo		if (strcmp("*", hosts[nhostsbak]) == 0) {
714172827Smatteo			if (svcport_str == NULL) {
715172827Smatteo				res = malloc(sizeof(struct addrinfo));
716172827Smatteo				if (res == NULL)
717172827Smatteo					out_of_mem();
718222623Srmacklem				mallocd_res = 1;
719172827Smatteo				res->ai_flags = hints.ai_flags;
720172827Smatteo				res->ai_family = hints.ai_family;
721172827Smatteo				res->ai_protocol = hints.ai_protocol;
722172827Smatteo				switch (res->ai_family) {
723172827Smatteo				case AF_INET:
724172827Smatteo					sin = malloc(sizeof(struct sockaddr_in));
725172827Smatteo					if (sin == NULL)
726172827Smatteo						out_of_mem();
727172827Smatteo					sin->sin_family = AF_INET;
728172827Smatteo					sin->sin_port = htons(0);
729172827Smatteo					sin->sin_addr.s_addr = htonl(INADDR_ANY);
730172827Smatteo					res->ai_addr = (struct sockaddr*) sin;
731172827Smatteo					res->ai_addrlen = (socklen_t)
732222623Srmacklem					    sizeof(struct sockaddr_in);
733172827Smatteo					break;
734172827Smatteo				case AF_INET6:
735172827Smatteo					sin6 = malloc(sizeof(struct sockaddr_in6));
736173056Ssimon					if (sin6 == NULL)
737172827Smatteo						out_of_mem();
738172827Smatteo					sin6->sin6_family = AF_INET6;
739172827Smatteo					sin6->sin6_port = htons(0);
740172827Smatteo					sin6->sin6_addr = in6addr_any;
741172827Smatteo					res->ai_addr = (struct sockaddr*) sin6;
742172827Smatteo					res->ai_addrlen = (socklen_t)
743222623Srmacklem					    sizeof(struct sockaddr_in6);
744222623Srmacklem					break;
745172827Smatteo				default:
746222623Srmacklem					syslog(LOG_ERR, "bad addr fam %d",
747222623Srmacklem					    res->ai_family);
748222623Srmacklem					exit(1);
749172827Smatteo				}
750172827Smatteo			} else {
751172827Smatteo				if ((aicode = getaddrinfo(NULL, svcport_str,
752172827Smatteo				    &hints, &res)) != 0) {
753172827Smatteo					syslog(LOG_ERR,
754172827Smatteo					    "cannot get local address for %s: %s",
755172827Smatteo					    nconf->nc_netid,
756172827Smatteo					    gai_strerror(aicode));
757222623Srmacklem					close(fd);
758172827Smatteo					continue;
759172827Smatteo				}
760172827Smatteo			}
761172827Smatteo		} else {
762172827Smatteo			if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
763172827Smatteo			    &hints, &res)) != 0) {
764172827Smatteo				syslog(LOG_ERR,
765172827Smatteo				    "cannot get local address for %s: %s",
766172827Smatteo				    nconf->nc_netid, gai_strerror(aicode));
767222623Srmacklem				close(fd);
768172827Smatteo				continue;
769172827Smatteo			}
770172827Smatteo		}
771172827Smatteo
772222623Srmacklem		/* Store the fd. */
773222623Srmacklem		sock_fd[sock_fdcnt - 1] = fd;
774222623Srmacklem
775222623Srmacklem		/* Now, attempt the bind. */
776172827Smatteo		r = bindresvport_sa(fd, res->ai_addr);
777172827Smatteo		if (r != 0) {
778222623Srmacklem			if (errno == EADDRINUSE && mallocd_svcport != 0) {
779222623Srmacklem				if (mallocd_res != 0) {
780222623Srmacklem					free(res->ai_addr);
781222623Srmacklem					free(res);
782222623Srmacklem				} else
783222623Srmacklem					freeaddrinfo(res);
784222623Srmacklem				return (-1);
785222623Srmacklem			}
786172827Smatteo			syslog(LOG_ERR, "bindresvport_sa: %m");
787172827Smatteo			exit(1);
788172827Smatteo		}
789172827Smatteo
790222623Srmacklem		if (svcport_str == NULL) {
791222623Srmacklem			svcport_str = malloc(NI_MAXSERV * sizeof(char));
792222623Srmacklem			if (svcport_str == NULL)
793222623Srmacklem				out_of_mem();
794222623Srmacklem			mallocd_svcport = 1;
795222623Srmacklem
796222623Srmacklem			if (getnameinfo(res->ai_addr,
797222623Srmacklem			    res->ai_addr->sa_len, NULL, NI_MAXHOST,
798222623Srmacklem			    svcport_str, NI_MAXSERV * sizeof(char),
799222623Srmacklem			    NI_NUMERICHOST | NI_NUMERICSERV))
800222623Srmacklem				errx(1, "Cannot get port number");
801222623Srmacklem		}
802222623Srmacklem		if (mallocd_res != 0) {
803222623Srmacklem			free(res->ai_addr);
804222623Srmacklem			free(res);
805222623Srmacklem		} else
806222623Srmacklem			freeaddrinfo(res);
807222623Srmacklem		res = NULL;
808222623Srmacklem	}
809222623Srmacklem	return (0);
810222623Srmacklem}
811222623Srmacklem
812222623Srmacklem/*
813222623Srmacklem * Called after all the create_service() calls have succeeded, to complete
814222623Srmacklem * the setup and registration.
815222623Srmacklem */
816222623Srmacklemstatic void
817222623Srmacklemcomplete_service(struct netconfig *nconf, char *port_str)
818222623Srmacklem{
819222623Srmacklem	struct addrinfo hints, *res = NULL;
820222623Srmacklem	struct __rpc_sockinfo si;
821222623Srmacklem	struct netbuf servaddr;
822222623Srmacklem	SVCXPRT	*transp = NULL;
823222623Srmacklem	int aicode, fd, nhostsbak;
824222623Srmacklem	int registered = 0;
825222623Srmacklem
826222623Srmacklem	if ((nconf->nc_semantics != NC_TPI_CLTS) &&
827222623Srmacklem	    (nconf->nc_semantics != NC_TPI_COTS) &&
828222623Srmacklem	    (nconf->nc_semantics != NC_TPI_COTS_ORD))
829222623Srmacklem		return;	/* not my type */
830222623Srmacklem
831222623Srmacklem	/*
832222623Srmacklem	 * XXX - using RPC library internal functions.
833222623Srmacklem	 */
834222623Srmacklem	if (!__rpc_nconf2sockinfo(nconf, &si)) {
835222623Srmacklem		syslog(LOG_ERR, "cannot get information for %s",
836222623Srmacklem		    nconf->nc_netid);
837222623Srmacklem		return;
838222623Srmacklem	}
839222623Srmacklem
840222623Srmacklem	nhostsbak = nhosts;
841222623Srmacklem	while (nhostsbak > 0) {
842222623Srmacklem		--nhostsbak;
843222623Srmacklem		if (sock_fdpos >= sock_fdcnt) {
844222623Srmacklem			/* Should never happen. */
845222623Srmacklem			syslog(LOG_ERR, "Ran out of socket fd's");
846222623Srmacklem			return;
847222623Srmacklem		}
848222623Srmacklem		fd = sock_fd[sock_fdpos++];
849222623Srmacklem		if (fd < 0)
850222623Srmacklem			continue;
851222623Srmacklem
852172827Smatteo		if (nconf->nc_semantics != NC_TPI_CLTS)
853172827Smatteo			listen(fd, SOMAXCONN);
854172827Smatteo
855172827Smatteo		if (nconf->nc_semantics == NC_TPI_CLTS )
856172827Smatteo			transp = svc_dg_create(fd, 0, 0);
857172827Smatteo		else
858172827Smatteo			transp = svc_vc_create(fd, RPC_MAXDATASIZE,
859172827Smatteo			    RPC_MAXDATASIZE);
860172827Smatteo
861172827Smatteo		if (transp != (SVCXPRT *) NULL) {
862194880Sdfr			if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
863172827Smatteo			    NULL))
864172827Smatteo				syslog(LOG_ERR,
865194880Sdfr				    "can't register %s MOUNTVERS service",
866172827Smatteo				    nconf->nc_netid);
867172827Smatteo			if (!force_v2) {
868194880Sdfr				if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
869172827Smatteo				    mntsrv, NULL))
870172827Smatteo					syslog(LOG_ERR,
871194880Sdfr					    "can't register %s MOUNTVERS3 service",
872172827Smatteo					    nconf->nc_netid);
873172827Smatteo			}
874172827Smatteo		} else
875172827Smatteo			syslog(LOG_WARNING, "can't create %s services",
876172827Smatteo			    nconf->nc_netid);
877172827Smatteo
878172827Smatteo		if (registered == 0) {
879172827Smatteo			registered = 1;
880172827Smatteo			memset(&hints, 0, sizeof hints);
881172827Smatteo			hints.ai_flags = AI_PASSIVE;
882172827Smatteo			hints.ai_family = si.si_af;
883172827Smatteo			hints.ai_socktype = si.si_socktype;
884172827Smatteo			hints.ai_protocol = si.si_proto;
885172827Smatteo
886222623Srmacklem			if ((aicode = getaddrinfo(NULL, port_str, &hints,
887172827Smatteo			    &res)) != 0) {
888172827Smatteo				syslog(LOG_ERR, "cannot get local address: %s",
889172827Smatteo				    gai_strerror(aicode));
890172827Smatteo				exit(1);
891172827Smatteo			}
892172827Smatteo
893172827Smatteo			servaddr.buf = malloc(res->ai_addrlen);
894172827Smatteo			memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
895172827Smatteo			servaddr.len = res->ai_addrlen;
896172827Smatteo
897194880Sdfr			rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
898194880Sdfr			rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
899172827Smatteo
900172827Smatteo			xcreated++;
901172827Smatteo			freeaddrinfo(res);
902172827Smatteo		}
903172827Smatteo	} /* end while */
9041558Srgrimes}
9051558Srgrimes
906222623Srmacklem/*
907222623Srmacklem * Clear out sockets after a failure to bind one of them, so that the
908222623Srmacklem * cycle of socket creation/binding can start anew.
909222623Srmacklem */
91037663Scharnierstatic void
911222623Srmacklemclearout_service(void)
912222623Srmacklem{
913222623Srmacklem	int i;
914222623Srmacklem
915222623Srmacklem	for (i = 0; i < sock_fdcnt; i++) {
916222623Srmacklem		if (sock_fd[i] >= 0) {
917222623Srmacklem			shutdown(sock_fd[i], SHUT_RDWR);
918222623Srmacklem			close(sock_fd[i]);
919222623Srmacklem		}
920222623Srmacklem	}
921222623Srmacklem}
922222623Srmacklem
923222623Srmacklemstatic void
924216587Scharnierusage(void)
92537663Scharnier{
92637663Scharnier	fprintf(stderr,
927192993Srmacklem		"usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
928241568Srmacklem		"[-S] [-h <bindip>] [export_file ...]\n");
92937663Scharnier	exit(1);
93037663Scharnier}
93137663Scharnier
9321558Srgrimes/*
9331558Srgrimes * The mount rpc service
9341558Srgrimes */
9351558Srgrimesvoid
936216587Scharniermntsrv(struct svc_req *rqstp, SVCXPRT *transp)
9371558Srgrimes{
9381558Srgrimes	struct exportlist *ep;
9391558Srgrimes	struct dirlist *dp;
9409336Sdfr	struct fhreturn fhr;
9411558Srgrimes	struct stat stb;
9421558Srgrimes	struct statfs fsb;
94374462Salfred	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
94474462Salfred	int lookup_failed = 1;
94574462Salfred	struct sockaddr *saddr;
9469336Sdfr	u_short sport;
947194880Sdfr	char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
94828911Sguido	int bad = 0, defset, hostset;
9499336Sdfr	sigset_t sighup_mask;
950240902Srmacklem	int numsecflavors, *secflavorsp;
9511558Srgrimes
9529336Sdfr	sigemptyset(&sighup_mask);
9539336Sdfr	sigaddset(&sighup_mask, SIGHUP);
95474462Salfred	saddr = svc_getrpccaller(transp)->buf;
95574462Salfred	switch (saddr->sa_family) {
95674462Salfred	case AF_INET6:
95775635Siedowse		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
95874462Salfred		break;
95974462Salfred	case AF_INET:
96075635Siedowse		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
96174462Salfred		break;
96274462Salfred	default:
96374462Salfred		syslog(LOG_ERR, "request from unknown address family");
96474462Salfred		return;
96574462Salfred	}
96674462Salfred	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
96774462Salfred	    NULL, 0, 0);
96874462Salfred	getnameinfo(saddr, saddr->sa_len, numerichost,
96974462Salfred	    sizeof numerichost, NULL, 0, NI_NUMERICHOST);
9701558Srgrimes	switch (rqstp->rq_proc) {
9711558Srgrimes	case NULLPROC:
972121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
97337663Scharnier			syslog(LOG_ERR, "can't send reply");
9741558Srgrimes		return;
975194880Sdfr	case MOUNTPROC_MNT:
9769336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
97731656Sguido			syslog(LOG_NOTICE,
97831656Sguido			    "mount request from %s from unprivileged port",
97974462Salfred			    numerichost);
9801558Srgrimes			svcerr_weakauth(transp);
9811558Srgrimes			return;
9821558Srgrimes		}
983121556Speter		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
98431656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
98574462Salfred			    numerichost);
9861558Srgrimes			svcerr_decode(transp);
9871558Srgrimes			return;
9881558Srgrimes		}
9891558Srgrimes
9901558Srgrimes		/*
9911558Srgrimes		 * Get the real pathname and make sure it is a directory
9929336Sdfr		 * or a regular file if the -r option was specified
9939336Sdfr		 * and it exists.
9941558Srgrimes		 */
99551968Salfred		if (realpath(rpcpath, dirpath) == NULL ||
9961558Srgrimes		    stat(dirpath, &stb) < 0 ||
9979336Sdfr		    (!S_ISDIR(stb.st_mode) &&
99874462Salfred		    (dir_only || !S_ISREG(stb.st_mode))) ||
9991558Srgrimes		    statfs(dirpath, &fsb) < 0) {
10001558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
100131656Sguido			syslog(LOG_NOTICE,
100237663Scharnier			    "mount request from %s for non existent path %s",
100374462Salfred			    numerichost, dirpath);
10041558Srgrimes			if (debug)
100537663Scharnier				warnx("stat failed on %s", dirpath);
100628911Sguido			bad = ENOENT;	/* We will send error reply later */
10071558Srgrimes		}
10081558Srgrimes
10091558Srgrimes		/* Check in the exports list */
10109336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
10111558Srgrimes		ep = ex_search(&fsb.f_fsid);
10129336Sdfr		hostset = defset = 0;
1013240902Srmacklem		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1014240902Srmacklem		    &numsecflavors, &secflavorsp) ||
10151558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1016240902Srmacklem		      chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1017240902Srmacklem		       &secflavorsp)) ||
101874462Salfred		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
101974462Salfred		     scan_tree(ep->ex_dirl, saddr) == 0))) {
102028911Sguido			if (bad) {
1021121556Speter				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
102228911Sguido				    (caddr_t)&bad))
102337663Scharnier					syslog(LOG_ERR, "can't send reply");
102428911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
102528911Sguido				return;
102628911Sguido			}
1027240902Srmacklem			if (hostset & DP_HOSTSET) {
10289336Sdfr				fhr.fhr_flag = hostset;
1029240902Srmacklem				fhr.fhr_numsecflavors = numsecflavors;
1030240902Srmacklem				fhr.fhr_secflavors = secflavorsp;
1031240902Srmacklem			} else {
10329336Sdfr				fhr.fhr_flag = defset;
1033240902Srmacklem				fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1034240902Srmacklem				fhr.fhr_secflavors = ep->ex_defsecflavors;
1035240902Srmacklem			}
10369336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
10371558Srgrimes			/* Get the file handle */
103823681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
10399336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
10401558Srgrimes				bad = errno;
104137663Scharnier				syslog(LOG_ERR, "can't get fh for %s", dirpath);
1042121556Speter				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
10431558Srgrimes				    (caddr_t)&bad))
104437663Scharnier					syslog(LOG_ERR, "can't send reply");
10459336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
10461558Srgrimes				return;
10471558Srgrimes			}
1048121556Speter			if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1049121556Speter			    (caddr_t)&fhr))
105037663Scharnier				syslog(LOG_ERR, "can't send reply");
105174462Salfred			if (!lookup_failed)
105274462Salfred				add_mlist(host, dirpath);
10531558Srgrimes			else
105474462Salfred				add_mlist(numerichost, dirpath);
10551558Srgrimes			if (debug)
105637663Scharnier				warnx("mount successful");
1057121767Speter			if (dolog)
105831656Sguido				syslog(LOG_NOTICE,
105931656Sguido				    "mount request succeeded from %s for %s",
106074462Salfred				    numerichost, dirpath);
106131656Sguido		} else {
10621558Srgrimes			bad = EACCES;
106331656Sguido			syslog(LOG_NOTICE,
106431656Sguido			    "mount request denied from %s for %s",
106574462Salfred			    numerichost, dirpath);
106631656Sguido		}
106728911Sguido
1068121556Speter		if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1069121556Speter		    (caddr_t)&bad))
107037663Scharnier			syslog(LOG_ERR, "can't send reply");
10719336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
10721558Srgrimes		return;
1073194880Sdfr	case MOUNTPROC_DUMP:
1074121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
107537663Scharnier			syslog(LOG_ERR, "can't send reply");
1076121767Speter		else if (dolog)
107731656Sguido			syslog(LOG_NOTICE,
107831656Sguido			    "dump request succeeded from %s",
107974462Salfred			    numerichost);
10801558Srgrimes		return;
1081194880Sdfr	case MOUNTPROC_UMNT:
10829336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
108331656Sguido			syslog(LOG_NOTICE,
108431656Sguido			    "umount request from %s from unprivileged port",
108574462Salfred			    numerichost);
10861558Srgrimes			svcerr_weakauth(transp);
10871558Srgrimes			return;
10881558Srgrimes		}
1089121556Speter		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
109031656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
109174462Salfred			    numerichost);
10921558Srgrimes			svcerr_decode(transp);
10931558Srgrimes			return;
10941558Srgrimes		}
109551968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
109651968Salfred			syslog(LOG_NOTICE, "umount request from %s "
109751968Salfred			    "for non existent path %s",
109874462Salfred			    numerichost, dirpath);
109951968Salfred		}
1100121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
110137663Scharnier			syslog(LOG_ERR, "can't send reply");
110274462Salfred		if (!lookup_failed)
110375635Siedowse			del_mlist(host, dirpath);
110475635Siedowse		del_mlist(numerichost, dirpath);
1105121767Speter		if (dolog)
110631656Sguido			syslog(LOG_NOTICE,
110731656Sguido			    "umount request succeeded from %s for %s",
110874462Salfred			    numerichost, dirpath);
11091558Srgrimes		return;
1110194880Sdfr	case MOUNTPROC_UMNTALL:
11119336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
111231656Sguido			syslog(LOG_NOTICE,
111331656Sguido			    "umountall request from %s from unprivileged port",
111474462Salfred			    numerichost);
11151558Srgrimes			svcerr_weakauth(transp);
11161558Srgrimes			return;
11171558Srgrimes		}
1118121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
111937663Scharnier			syslog(LOG_ERR, "can't send reply");
112074462Salfred		if (!lookup_failed)
112175635Siedowse			del_mlist(host, NULL);
112275635Siedowse		del_mlist(numerichost, NULL);
1123121767Speter		if (dolog)
112431656Sguido			syslog(LOG_NOTICE,
112531656Sguido			    "umountall request succeeded from %s",
112674462Salfred			    numerichost);
11271558Srgrimes		return;
1128194880Sdfr	case MOUNTPROC_EXPORT:
1129121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1130121556Speter			if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1131121556Speter			    (caddr_t)NULL))
1132100117Salfred				syslog(LOG_ERR, "can't send reply");
1133121767Speter		if (dolog)
113431656Sguido			syslog(LOG_NOTICE,
113531656Sguido			    "export request succeeded from %s",
113674462Salfred			    numerichost);
11371558Srgrimes		return;
11381558Srgrimes	default:
11391558Srgrimes		svcerr_noproc(transp);
11401558Srgrimes		return;
11411558Srgrimes	}
11421558Srgrimes}
11431558Srgrimes
11441558Srgrimes/*
11451558Srgrimes * Xdr conversion for a dirpath string
11461558Srgrimes */
11471558Srgrimesint
1148216587Scharnierxdr_dir(XDR *xdrsp, char *dirp)
11491558Srgrimes{
1150194880Sdfr	return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
11511558Srgrimes}
11521558Srgrimes
11531558Srgrimes/*
11549336Sdfr * Xdr routine to generate file handle reply
11551558Srgrimes */
11561558Srgrimesint
1157216587Scharnierxdr_fhs(XDR *xdrsp, caddr_t cp)
11581558Srgrimes{
115992806Sobrien	struct fhreturn *fhrp = (struct fhreturn *)cp;
11609336Sdfr	u_long ok = 0, len, auth;
1161184588Sdfr	int i;
11621558Srgrimes
11631558Srgrimes	if (!xdr_long(xdrsp, &ok))
11641558Srgrimes		return (0);
11659336Sdfr	switch (fhrp->fhr_vers) {
11669336Sdfr	case 1:
11679336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
11689336Sdfr	case 3:
11699336Sdfr		len = NFSX_V3FH;
11709336Sdfr		if (!xdr_long(xdrsp, &len))
11719336Sdfr			return (0);
11729336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
11739336Sdfr			return (0);
1174184588Sdfr		if (fhrp->fhr_numsecflavors) {
1175184588Sdfr			if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1176184588Sdfr				return (0);
1177184588Sdfr			for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1178184588Sdfr				if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1179184588Sdfr					return (0);
1180184588Sdfr			return (1);
1181184588Sdfr		} else {
1182184588Sdfr			auth = AUTH_SYS;
1183184588Sdfr			len = 1;
1184184588Sdfr			if (!xdr_long(xdrsp, &len))
1185184588Sdfr				return (0);
1186184588Sdfr			return (xdr_long(xdrsp, &auth));
1187184588Sdfr		}
11889336Sdfr	};
11899336Sdfr	return (0);
11901558Srgrimes}
11911558Srgrimes
11921558Srgrimesint
1193216587Scharnierxdr_mlist(XDR *xdrsp, caddr_t cp __unused)
11941558Srgrimes{
11951558Srgrimes	struct mountlist *mlp;
11961558Srgrimes	int true = 1;
11971558Srgrimes	int false = 0;
11981558Srgrimes	char *strp;
11991558Srgrimes
12001558Srgrimes	mlp = mlhead;
12011558Srgrimes	while (mlp) {
12021558Srgrimes		if (!xdr_bool(xdrsp, &true))
12031558Srgrimes			return (0);
12041558Srgrimes		strp = &mlp->ml_host[0];
1205194880Sdfr		if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
12061558Srgrimes			return (0);
12071558Srgrimes		strp = &mlp->ml_dirp[0];
1208194880Sdfr		if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
12091558Srgrimes			return (0);
12101558Srgrimes		mlp = mlp->ml_next;
12111558Srgrimes	}
12121558Srgrimes	if (!xdr_bool(xdrsp, &false))
12131558Srgrimes		return (0);
12141558Srgrimes	return (1);
12151558Srgrimes}
12161558Srgrimes
12171558Srgrimes/*
12181558Srgrimes * Xdr conversion for export list
12191558Srgrimes */
12201558Srgrimesint
1221216587Scharnierxdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
12221558Srgrimes{
12231558Srgrimes	struct exportlist *ep;
12241558Srgrimes	int false = 0;
12259336Sdfr	int putdef;
12269336Sdfr	sigset_t sighup_mask;
12271558Srgrimes
12289336Sdfr	sigemptyset(&sighup_mask);
12299336Sdfr	sigaddset(&sighup_mask, SIGHUP);
12309336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
12311558Srgrimes	ep = exphead;
12321558Srgrimes	while (ep) {
12331558Srgrimes		putdef = 0;
1234100117Salfred		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1235100117Salfred			       &putdef, brief))
12361558Srgrimes			goto errout;
12371558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
12381558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1239100117Salfred			&putdef, brief))
12401558Srgrimes			goto errout;
12411558Srgrimes		ep = ep->ex_next;
12421558Srgrimes	}
12439336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
12441558Srgrimes	if (!xdr_bool(xdrsp, &false))
12451558Srgrimes		return (0);
12461558Srgrimes	return (1);
12471558Srgrimeserrout:
12489336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
12491558Srgrimes	return (0);
12501558Srgrimes}
12511558Srgrimes
12521558Srgrimes/*
12531558Srgrimes * Called from xdr_explist() to traverse the tree and export the
12541558Srgrimes * directory paths.
12551558Srgrimes */
12561558Srgrimesint
1257216587Scharnierput_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1258216587Scharnier	int brief)
12591558Srgrimes{
12601558Srgrimes	struct grouplist *grp;
12611558Srgrimes	struct hostlist *hp;
12621558Srgrimes	int true = 1;
12631558Srgrimes	int false = 0;
12641558Srgrimes	int gotalldir = 0;
12651558Srgrimes	char *strp;
12661558Srgrimes
12671558Srgrimes	if (dp) {
1268100117Salfred		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
12691558Srgrimes			return (1);
12701558Srgrimes		if (!xdr_bool(xdrsp, &true))
12711558Srgrimes			return (1);
12721558Srgrimes		strp = dp->dp_dirp;
1273194880Sdfr		if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
12741558Srgrimes			return (1);
12751558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
12761558Srgrimes			gotalldir = 1;
12771558Srgrimes			*putdefp = 1;
12781558Srgrimes		}
1279100117Salfred		if (brief) {
1280100117Salfred			if (!xdr_bool(xdrsp, &true))
1281100117Salfred				return (1);
1282100117Salfred			strp = "(...)";
1283194880Sdfr			if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1284100117Salfred				return (1);
1285100117Salfred		} else if ((dp->dp_flag & DP_DEFSET) == 0 &&
12861558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
12871558Srgrimes			hp = dp->dp_hosts;
12881558Srgrimes			while (hp) {
12891558Srgrimes				grp = hp->ht_grp;
12901558Srgrimes				if (grp->gr_type == GT_HOST) {
12911558Srgrimes					if (!xdr_bool(xdrsp, &true))
12921558Srgrimes						return (1);
129374462Salfred					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
12948871Srgrimes					if (!xdr_string(xdrsp, &strp,
1295194880Sdfr					    MNTNAMLEN))
12961558Srgrimes						return (1);
12971558Srgrimes				} else if (grp->gr_type == GT_NET) {
12981558Srgrimes					if (!xdr_bool(xdrsp, &true))
12991558Srgrimes						return (1);
13001558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
13018871Srgrimes					if (!xdr_string(xdrsp, &strp,
1302194880Sdfr					    MNTNAMLEN))
13031558Srgrimes						return (1);
13041558Srgrimes				}
13051558Srgrimes				hp = hp->ht_next;
13061558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
13071558Srgrimes					hp = adp->dp_hosts;
13081558Srgrimes					gotalldir = 0;
13091558Srgrimes				}
13101558Srgrimes			}
13111558Srgrimes		}
13121558Srgrimes		if (!xdr_bool(xdrsp, &false))
13131558Srgrimes			return (1);
1314100117Salfred		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
13151558Srgrimes			return (1);
13161558Srgrimes	}
13171558Srgrimes	return (0);
13181558Srgrimes}
13191558Srgrimes
1320100117Salfredint
1321216587Scharnierxdr_explist(XDR *xdrsp, caddr_t cp)
1322100117Salfred{
1323100117Salfred
1324100117Salfred	return xdr_explist_common(xdrsp, cp, 0);
1325100117Salfred}
1326100117Salfred
1327100117Salfredint
1328216587Scharnierxdr_explist_brief(XDR *xdrsp, caddr_t cp)
1329100117Salfred{
1330100117Salfred
1331100117Salfred	return xdr_explist_common(xdrsp, cp, 1);
1332100117Salfred}
1333100117Salfred
133496622Siedowsechar *line;
133596622Siedowseint linesize;
13361558SrgrimesFILE *exp_file;
13371558Srgrimes
13381558Srgrimes/*
1339166440Spjd * Get the export list from one, currently open file
13401558Srgrimes */
1341166440Spjdstatic void
1342216587Scharnierget_exportlist_one(void)
13431558Srgrimes{
13441558Srgrimes	struct exportlist *ep, *ep2;
13451558Srgrimes	struct grouplist *grp, *tgrp;
13461558Srgrimes	struct exportlist **epp;
13471558Srgrimes	struct dirlist *dirhead;
1348166440Spjd	struct statfs fsb;
134972650Sgreen	struct xucred anon;
13501558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1351166440Spjd	int len, has_host, exflags, got_nondir, dirplen, netgrp;
13521558Srgrimes
1353192934Srmacklem	v4root_phase = 0;
13541558Srgrimes	dirhead = (struct dirlist *)NULL;
13551558Srgrimes	while (get_line()) {
13561558Srgrimes		if (debug)
135737663Scharnier			warnx("got line %s", line);
13581558Srgrimes		cp = line;
13591558Srgrimes		nextfield(&cp, &endcp);
13601558Srgrimes		if (*cp == '#')
13611558Srgrimes			goto nextline;
13621558Srgrimes
13631558Srgrimes		/*
13641558Srgrimes		 * Set defaults.
13651558Srgrimes		 */
13661558Srgrimes		has_host = FALSE;
13671558Srgrimes		anon = def_anon;
13681558Srgrimes		exflags = MNT_EXPORTED;
13691558Srgrimes		got_nondir = 0;
13701558Srgrimes		opt_flags = 0;
13711558Srgrimes		ep = (struct exportlist *)NULL;
1372192934Srmacklem		dirp = NULL;
13731558Srgrimes
13741558Srgrimes		/*
1375192934Srmacklem		 * Handle the V4 root dir.
1376192934Srmacklem		 */
1377192934Srmacklem		if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1378192934Srmacklem			/*
1379192934Srmacklem			 * V4: just indicates that it is the v4 root point,
1380192934Srmacklem			 * so skip over that and set v4root_phase.
1381192934Srmacklem			 */
1382192934Srmacklem			if (v4root_phase > 0) {
1383192934Srmacklem				syslog(LOG_ERR, "V4:duplicate line, ignored");
1384192934Srmacklem				goto nextline;
1385192934Srmacklem			}
1386192934Srmacklem			v4root_phase = 1;
1387192934Srmacklem			cp += 3;
1388192934Srmacklem			nextfield(&cp, &endcp);
1389192934Srmacklem		}
1390192934Srmacklem
1391192934Srmacklem		/*
13921558Srgrimes		 * Create new exports list entry
13931558Srgrimes		 */
13941558Srgrimes		len = endcp-cp;
13951558Srgrimes		tgrp = grp = get_grp();
13961558Srgrimes		while (len > 0) {
1397194880Sdfr			if (len > MNTNAMLEN) {
13981558Srgrimes			    getexp_err(ep, tgrp);
13991558Srgrimes			    goto nextline;
14001558Srgrimes			}
14011558Srgrimes			if (*cp == '-') {
14021558Srgrimes			    if (ep == (struct exportlist *)NULL) {
14031558Srgrimes				getexp_err(ep, tgrp);
14041558Srgrimes				goto nextline;
14051558Srgrimes			    }
14061558Srgrimes			    if (debug)
140737663Scharnier				warnx("doing opt %s", cp);
14081558Srgrimes			    got_nondir = 1;
14091558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
14101558Srgrimes				&exflags, &anon)) {
14111558Srgrimes				getexp_err(ep, tgrp);
14121558Srgrimes				goto nextline;
14131558Srgrimes			    }
14141558Srgrimes			} else if (*cp == '/') {
14151558Srgrimes			    savedc = *endcp;
14161558Srgrimes			    *endcp = '\0';
1417192934Srmacklem			    if (v4root_phase > 1) {
1418192934Srmacklem				    if (dirp != NULL) {
1419192934Srmacklem					syslog(LOG_ERR, "Multiple V4 dirs");
1420192934Srmacklem					getexp_err(ep, tgrp);
1421192934Srmacklem					goto nextline;
1422192934Srmacklem				    }
1423192934Srmacklem			    }
14241558Srgrimes			    if (check_dirpath(cp) &&
14251558Srgrimes				statfs(cp, &fsb) >= 0) {
14261558Srgrimes				if (got_nondir) {
142737663Scharnier				    syslog(LOG_ERR, "dirs must be first");
14281558Srgrimes				    getexp_err(ep, tgrp);
14291558Srgrimes				    goto nextline;
14301558Srgrimes				}
1431192934Srmacklem				if (v4root_phase == 1) {
1432192934Srmacklem				    if (dirp != NULL) {
1433192934Srmacklem					syslog(LOG_ERR, "Multiple V4 dirs");
14341558Srgrimes					getexp_err(ep, tgrp);
14351558Srgrimes					goto nextline;
14361558Srgrimes				    }
1437192934Srmacklem				    if (strlen(v4root_dirpath) == 0) {
1438192934Srmacklem					strlcpy(v4root_dirpath, cp,
1439192934Srmacklem					    sizeof (v4root_dirpath));
1440192934Srmacklem				    } else if (strcmp(v4root_dirpath, cp)
1441192934Srmacklem					!= 0) {
1442192934Srmacklem					syslog(LOG_ERR,
1443192934Srmacklem					    "different V4 dirpath %s", cp);
1444192934Srmacklem					getexp_err(ep, tgrp);
1445192934Srmacklem					goto nextline;
1446192934Srmacklem				    }
1447192934Srmacklem				    dirp = cp;
1448192934Srmacklem				    v4root_phase = 2;
1449192934Srmacklem				    got_nondir = 1;
1450192934Srmacklem				    ep = get_exp();
14511558Srgrimes				} else {
1452192934Srmacklem				    if (ep) {
1453192934Srmacklem					if (ep->ex_fs.val[0] !=
1454192934Srmacklem					    fsb.f_fsid.val[0] ||
1455192934Srmacklem					    ep->ex_fs.val[1] !=
1456192934Srmacklem					    fsb.f_fsid.val[1]) {
1457192934Srmacklem						getexp_err(ep, tgrp);
1458192934Srmacklem						goto nextline;
1459192934Srmacklem					}
1460192934Srmacklem				    } else {
1461192934Srmacklem					/*
1462192934Srmacklem					 * See if this directory is already
1463192934Srmacklem					 * in the list.
1464192934Srmacklem					 */
1465192934Srmacklem					ep = ex_search(&fsb.f_fsid);
1466192934Srmacklem					if (ep == (struct exportlist *)NULL) {
1467192934Srmacklem					    ep = get_exp();
1468192934Srmacklem					    ep->ex_fs = fsb.f_fsid;
1469192934Srmacklem					    ep->ex_fsdir = (char *)malloc
1470192934Srmacklem					        (strlen(fsb.f_mntonname) + 1);
1471192934Srmacklem					    if (ep->ex_fsdir)
1472192934Srmacklem						strcpy(ep->ex_fsdir,
1473192934Srmacklem						    fsb.f_mntonname);
1474192934Srmacklem					    else
1475192934Srmacklem						out_of_mem();
1476192934Srmacklem					    if (debug)
1477192934Srmacklem						warnx(
1478192934Srmacklem						  "making new ep fs=0x%x,0x%x",
1479192934Srmacklem						  fsb.f_fsid.val[0],
1480192934Srmacklem						  fsb.f_fsid.val[1]);
1481192934Srmacklem					} else if (debug)
1482192934Srmacklem					    warnx("found ep fs=0x%x,0x%x",
1483192934Srmacklem						fsb.f_fsid.val[0],
1484192934Srmacklem						fsb.f_fsid.val[1]);
1485192934Srmacklem				    }
1486192934Srmacklem
14871558Srgrimes				    /*
1488192934Srmacklem				     * Add dirpath to export mount point.
14891558Srgrimes				     */
1490192934Srmacklem				    dirp = add_expdir(&dirhead, cp, len);
1491192934Srmacklem				    dirplen = len;
14921558Srgrimes				}
14931558Srgrimes			    } else {
14941558Srgrimes				getexp_err(ep, tgrp);
14951558Srgrimes				goto nextline;
14961558Srgrimes			    }
14971558Srgrimes			    *endcp = savedc;
14981558Srgrimes			} else {
14991558Srgrimes			    savedc = *endcp;
15001558Srgrimes			    *endcp = '\0';
15011558Srgrimes			    got_nondir = 1;
15021558Srgrimes			    if (ep == (struct exportlist *)NULL) {
15031558Srgrimes				getexp_err(ep, tgrp);
15041558Srgrimes				goto nextline;
15051558Srgrimes			    }
15061558Srgrimes
15071558Srgrimes			    /*
15081558Srgrimes			     * Get the host or netgroup.
15091558Srgrimes			     */
15101558Srgrimes			    setnetgrent(cp);
15111558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
15121558Srgrimes			    do {
15131558Srgrimes				if (has_host) {
15141558Srgrimes				    grp->gr_next = get_grp();
15151558Srgrimes				    grp = grp->gr_next;
15161558Srgrimes				}
15171558Srgrimes				if (netgrp) {
151837003Sjoerg				    if (hst == 0) {
151937663Scharnier					syslog(LOG_ERR,
152037663Scharnier				"null hostname in netgroup %s, skipping", cp);
152137004Sjoerg					grp->gr_type = GT_IGNORE;
152237003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
152337663Scharnier					syslog(LOG_ERR,
152437663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
152529317Sjlemon					grp->gr_type = GT_IGNORE;
15261558Srgrimes				    }
15277401Swpaul				} else if (get_host(cp, grp, tgrp)) {
152837663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
152929317Sjlemon				    grp->gr_type = GT_IGNORE;
15301558Srgrimes				}
15311558Srgrimes				has_host = TRUE;
15321558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
15331558Srgrimes			    endnetgrent();
15341558Srgrimes			    *endcp = savedc;
15351558Srgrimes			}
15361558Srgrimes			cp = endcp;
15371558Srgrimes			nextfield(&cp, &endcp);
15381558Srgrimes			len = endcp - cp;
15391558Srgrimes		}
15401558Srgrimes		if (check_options(dirhead)) {
15411558Srgrimes			getexp_err(ep, tgrp);
15421558Srgrimes			goto nextline;
15431558Srgrimes		}
15441558Srgrimes		if (!has_host) {
154575641Siedowse			grp->gr_type = GT_DEFAULT;
15461558Srgrimes			if (debug)
154737663Scharnier				warnx("adding a default entry");
15481558Srgrimes
15491558Srgrimes		/*
15501558Srgrimes		 * Don't allow a network export coincide with a list of
15511558Srgrimes		 * host(s) on the same line.
15521558Srgrimes		 */
15531558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
155475801Siedowse			syslog(LOG_ERR, "network/host conflict");
15551558Srgrimes			getexp_err(ep, tgrp);
15561558Srgrimes			goto nextline;
155729317Sjlemon
155874462Salfred		/*
155974462Salfred		 * If an export list was specified on this line, make sure
156029317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
156129317Sjlemon		 */
156229317Sjlemon		} else {
156329317Sjlemon			grp = tgrp;
156474462Salfred			while (grp && grp->gr_type == GT_IGNORE)
156529317Sjlemon				grp = grp->gr_next;
156629317Sjlemon			if (! grp) {
156729317Sjlemon			    getexp_err(ep, tgrp);
156829317Sjlemon			    goto nextline;
156929317Sjlemon			}
15701558Srgrimes		}
15711558Srgrimes
1572192934Srmacklem		if (v4root_phase == 1) {
1573192934Srmacklem			syslog(LOG_ERR, "V4:root, no dirp, ignored");
1574192934Srmacklem			getexp_err(ep, tgrp);
1575192934Srmacklem			goto nextline;
1576192934Srmacklem		}
1577192934Srmacklem
15781558Srgrimes		/*
15791558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
15801558Srgrimes		 * After loop, tgrp points to the start of the list and
15811558Srgrimes		 * grp points to the last entry in the list.
15821558Srgrimes		 */
15831558Srgrimes		grp = tgrp;
15841558Srgrimes		do {
158575635Siedowse			if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
158675635Siedowse			    &fsb)) {
158775635Siedowse				getexp_err(ep, tgrp);
158875635Siedowse				goto nextline;
158975635Siedowse			}
15901558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
15911558Srgrimes
15921558Srgrimes		/*
1593192934Srmacklem		 * For V4: don't enter in mount lists.
1594192934Srmacklem		 */
1595194773Srmacklem		if (v4root_phase > 0 && v4root_phase <= 2) {
1596194773Srmacklem			/*
1597194773Srmacklem			 * Since these structures aren't used by mountd,
1598194773Srmacklem			 * free them up now.
1599194773Srmacklem			 */
1600194773Srmacklem			if (ep != NULL)
1601194773Srmacklem				free_exp(ep);
1602194773Srmacklem			while (tgrp != NULL) {
1603194773Srmacklem				grp = tgrp;
1604194773Srmacklem				tgrp = tgrp->gr_next;
1605194773Srmacklem				free_grp(grp);
1606194773Srmacklem			}
1607192934Srmacklem			goto nextline;
1608194773Srmacklem		}
1609192934Srmacklem
1610192934Srmacklem		/*
16111558Srgrimes		 * Success. Update the data structures.
16121558Srgrimes		 */
16131558Srgrimes		if (has_host) {
16149336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
16151558Srgrimes			grp->gr_next = grphead;
16161558Srgrimes			grphead = tgrp;
16171558Srgrimes		} else {
16181558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
16199336Sdfr				opt_flags);
16201558Srgrimes			free_grp(grp);
16211558Srgrimes		}
16221558Srgrimes		dirhead = (struct dirlist *)NULL;
16231558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
16241558Srgrimes			ep2 = exphead;
16251558Srgrimes			epp = &exphead;
16261558Srgrimes
16271558Srgrimes			/*
16281558Srgrimes			 * Insert in the list in alphabetical order.
16291558Srgrimes			 */
16301558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
16311558Srgrimes				epp = &ep2->ex_next;
16321558Srgrimes				ep2 = ep2->ex_next;
16331558Srgrimes			}
16341558Srgrimes			if (ep2)
16351558Srgrimes				ep->ex_next = ep2;
16361558Srgrimes			*epp = ep;
16371558Srgrimes			ep->ex_flag |= EX_LINKED;
16381558Srgrimes		}
16391558Srgrimesnextline:
1640192934Srmacklem		v4root_phase = 0;
16411558Srgrimes		if (dirhead) {
16421558Srgrimes			free_dir(dirhead);
16431558Srgrimes			dirhead = (struct dirlist *)NULL;
16441558Srgrimes		}
16451558Srgrimes	}
16461558Srgrimes}
16471558Srgrimes
16481558Srgrimes/*
1649166440Spjd * Get the export list from all specified files
1650166440Spjd */
1651166440Spjdvoid
1652216587Scharnierget_exportlist(void)
1653166440Spjd{
1654166440Spjd	struct exportlist *ep, *ep2;
1655166440Spjd	struct grouplist *grp, *tgrp;
1656166440Spjd	struct export_args export;
1657166440Spjd	struct iovec *iov;
1658166440Spjd	struct statfs *fsp, *mntbufp;
1659166440Spjd	struct xvfsconf vfc;
1660166440Spjd	char errmsg[255];
1661230352Seadler	int num, i;
1662166440Spjd	int iovlen;
1663168684Spjd	int done;
1664192934Srmacklem	struct nfsex_args eargs;
1665166440Spjd
1666241568Srmacklem	if (suspend_nfsd != 0)
1667241568Srmacklem		(void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1668192934Srmacklem	v4root_dirpath[0] = '\0';
1669166440Spjd	bzero(&export, sizeof(export));
1670166440Spjd	export.ex_flags = MNT_DELEXPORT;
1671166440Spjd	iov = NULL;
1672166440Spjd	iovlen = 0;
1673166440Spjd	bzero(errmsg, sizeof(errmsg));
1674166440Spjd
1675166440Spjd	/*
1676166440Spjd	 * First, get rid of the old list
1677166440Spjd	 */
1678166440Spjd	ep = exphead;
1679166440Spjd	while (ep) {
1680166440Spjd		ep2 = ep;
1681166440Spjd		ep = ep->ex_next;
1682166440Spjd		free_exp(ep2);
1683166440Spjd	}
1684166440Spjd	exphead = (struct exportlist *)NULL;
1685166440Spjd
1686166440Spjd	grp = grphead;
1687166440Spjd	while (grp) {
1688166440Spjd		tgrp = grp;
1689166440Spjd		grp = grp->gr_next;
1690166440Spjd		free_grp(tgrp);
1691166440Spjd	}
1692166440Spjd	grphead = (struct grouplist *)NULL;
1693166440Spjd
1694166440Spjd	/*
1695192934Srmacklem	 * and the old V4 root dir.
1696192934Srmacklem	 */
1697192934Srmacklem	bzero(&eargs, sizeof (eargs));
1698192934Srmacklem	eargs.export.ex_flags = MNT_DELEXPORT;
1699192934Srmacklem	if (run_v4server > 0 &&
1700192934Srmacklem	    nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1701192934Srmacklem	    errno != ENOENT)
1702192934Srmacklem		syslog(LOG_ERR, "Can't delete exports for V4:");
1703192934Srmacklem
1704192934Srmacklem	/*
1705192934Srmacklem	 * and clear flag that notes if a public fh has been exported.
1706192934Srmacklem	 */
1707192934Srmacklem	has_publicfh = 0;
1708192934Srmacklem
1709192934Srmacklem	/*
1710166440Spjd	 * And delete exports that are in the kernel for all local
1711166440Spjd	 * filesystems.
1712166440Spjd	 * XXX: Should know how to handle all local exportable filesystems.
1713166440Spjd	 */
1714166440Spjd	num = getmntinfo(&mntbufp, MNT_NOWAIT);
1715166440Spjd
1716166440Spjd	if (num > 0) {
1717166440Spjd		build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1718166440Spjd		build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1719166440Spjd		build_iovec(&iov, &iovlen, "from", NULL, 0);
1720166440Spjd		build_iovec(&iov, &iovlen, "update", NULL, 0);
1721166440Spjd		build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1722166440Spjd		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1723166440Spjd	}
1724166440Spjd
1725166440Spjd	for (i = 0; i < num; i++) {
1726166440Spjd		fsp = &mntbufp[i];
1727166440Spjd		if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1728166440Spjd			syslog(LOG_ERR, "getvfsbyname() failed for %s",
1729166440Spjd			    fsp->f_fstypename);
1730166440Spjd			continue;
1731166440Spjd		}
1732166440Spjd
1733166440Spjd		/*
1734166440Spjd		 * Do not delete export for network filesystem by
1735166440Spjd		 * passing "export" arg to nmount().
1736166440Spjd		 * It only makes sense to do this for local filesystems.
1737166440Spjd		 */
1738166440Spjd		if (vfc.vfc_flags & VFCF_NETWORK)
1739166440Spjd			continue;
1740166440Spjd
1741166440Spjd		iov[1].iov_base = fsp->f_fstypename;
1742166440Spjd		iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1743166440Spjd		iov[3].iov_base = fsp->f_mntonname;
1744166440Spjd		iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1745166440Spjd		iov[5].iov_base = fsp->f_mntfromname;
1746166440Spjd		iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1747272428Sbdrewery		errmsg[0] = '\0';
1748166440Spjd
1749166440Spjd		if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1750166440Spjd		    errno != ENOENT && errno != ENOTSUP) {
1751166440Spjd			syslog(LOG_ERR,
1752166440Spjd			    "can't delete exports for %s: %m %s",
1753166440Spjd			    fsp->f_mntonname, errmsg);
1754166440Spjd		}
1755166440Spjd	}
1756166440Spjd
1757166440Spjd	if (iov != NULL) {
1758166440Spjd		/* Free strings allocated by strdup() in getmntopts.c */
1759166440Spjd		free(iov[0].iov_base); /* fstype */
1760166440Spjd		free(iov[2].iov_base); /* fspath */
1761166440Spjd		free(iov[4].iov_base); /* from */
1762166440Spjd		free(iov[6].iov_base); /* update */
1763166440Spjd		free(iov[8].iov_base); /* export */
1764166440Spjd		free(iov[10].iov_base); /* errmsg */
1765166440Spjd
1766166440Spjd		/* free iov, allocated by realloc() */
1767166440Spjd		free(iov);
1768166440Spjd		iovlen = 0;
1769166440Spjd	}
1770166440Spjd
1771166440Spjd	/*
1772166440Spjd	 * Read in the exports file and build the list, calling
1773166440Spjd	 * nmount() as we go along to push the export rules into the kernel.
1774166440Spjd	 */
1775168684Spjd	done = 0;
1776166440Spjd	for (i = 0; exnames[i] != NULL; i++) {
1777166440Spjd		if (debug)
1778166440Spjd			warnx("reading exports from %s", exnames[i]);
1779166440Spjd		if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1780168684Spjd			syslog(LOG_WARNING, "can't open %s", exnames[i]);
1781168684Spjd			continue;
1782166440Spjd		}
1783166440Spjd		get_exportlist_one();
1784166440Spjd		fclose(exp_file);
1785168684Spjd		done++;
1786166440Spjd	}
1787168684Spjd	if (done == 0) {
1788168684Spjd		syslog(LOG_ERR, "can't open any exports file");
1789168684Spjd		exit(2);
1790168684Spjd	}
1791192934Srmacklem
1792192934Srmacklem	/*
1793192934Srmacklem	 * If there was no public fh, clear any previous one set.
1794192934Srmacklem	 */
1795192934Srmacklem	if (run_v4server > 0 && has_publicfh == 0)
1796192934Srmacklem		(void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1797241568Srmacklem
1798241568Srmacklem	/* Resume the nfsd. If they weren't suspended, this is harmless. */
1799241568Srmacklem	(void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1800166440Spjd}
1801166440Spjd
1802166440Spjd/*
18031558Srgrimes * Allocate an export list element
18041558Srgrimes */
18051558Srgrimesstruct exportlist *
1806216587Scharnierget_exp(void)
18071558Srgrimes{
18081558Srgrimes	struct exportlist *ep;
18091558Srgrimes
1810224003Sdelphij	ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
18111558Srgrimes	if (ep == (struct exportlist *)NULL)
18121558Srgrimes		out_of_mem();
18131558Srgrimes	return (ep);
18141558Srgrimes}
18151558Srgrimes
18161558Srgrimes/*
18171558Srgrimes * Allocate a group list element
18181558Srgrimes */
18191558Srgrimesstruct grouplist *
1820216587Scharnierget_grp(void)
18211558Srgrimes{
18221558Srgrimes	struct grouplist *gp;
18231558Srgrimes
1824224003Sdelphij	gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
18251558Srgrimes	if (gp == (struct grouplist *)NULL)
18261558Srgrimes		out_of_mem();
18271558Srgrimes	return (gp);
18281558Srgrimes}
18291558Srgrimes
18301558Srgrimes/*
18311558Srgrimes * Clean up upon an error in get_exportlist().
18321558Srgrimes */
18331558Srgrimesvoid
1834216587Scharniergetexp_err(struct exportlist *ep, struct grouplist *grp)
18351558Srgrimes{
18361558Srgrimes	struct grouplist *tgrp;
18371558Srgrimes
1838100336Sjoerg	if (!(opt_flags & OP_QUIET))
1839100336Sjoerg		syslog(LOG_ERR, "bad exports list line %s", line);
18401558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
18411558Srgrimes		free_exp(ep);
18421558Srgrimes	while (grp) {
18431558Srgrimes		tgrp = grp;
18441558Srgrimes		grp = grp->gr_next;
18451558Srgrimes		free_grp(tgrp);
18461558Srgrimes	}
18471558Srgrimes}
18481558Srgrimes
18491558Srgrimes/*
18501558Srgrimes * Search the export list for a matching fs.
18511558Srgrimes */
18521558Srgrimesstruct exportlist *
1853216587Scharnierex_search(fsid_t *fsid)
18541558Srgrimes{
18551558Srgrimes	struct exportlist *ep;
18561558Srgrimes
18571558Srgrimes	ep = exphead;
18581558Srgrimes	while (ep) {
18591558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
18601558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
18611558Srgrimes			return (ep);
18621558Srgrimes		ep = ep->ex_next;
18631558Srgrimes	}
18641558Srgrimes	return (ep);
18651558Srgrimes}
18661558Srgrimes
18671558Srgrimes/*
18681558Srgrimes * Add a directory path to the list.
18691558Srgrimes */
18701558Srgrimeschar *
1871216587Scharnieradd_expdir(struct dirlist **dpp, char *cp, int len)
18721558Srgrimes{
18731558Srgrimes	struct dirlist *dp;
18741558Srgrimes
18751558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
187637663Scharnier	if (dp == (struct dirlist *)NULL)
187737663Scharnier		out_of_mem();
18781558Srgrimes	dp->dp_left = *dpp;
18791558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
18801558Srgrimes	dp->dp_flag = 0;
18811558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
18821558Srgrimes	strcpy(dp->dp_dirp, cp);
18831558Srgrimes	*dpp = dp;
18841558Srgrimes	return (dp->dp_dirp);
18851558Srgrimes}
18861558Srgrimes
18871558Srgrimes/*
18881558Srgrimes * Hang the dir list element off the dirpath binary tree as required
18891558Srgrimes * and update the entry for host.
18901558Srgrimes */
18911558Srgrimesvoid
1892216587Scharnierhang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1893216587Scharnier	int flags)
18941558Srgrimes{
18951558Srgrimes	struct hostlist *hp;
18961558Srgrimes	struct dirlist *dp2;
18971558Srgrimes
18989336Sdfr	if (flags & OP_ALLDIRS) {
18991558Srgrimes		if (ep->ex_defdir)
19001558Srgrimes			free((caddr_t)dp);
19011558Srgrimes		else
19021558Srgrimes			ep->ex_defdir = dp;
19039336Sdfr		if (grp == (struct grouplist *)NULL) {
19041558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
1905240902Srmacklem			/* Save the default security flavors list. */
1906240902Srmacklem			ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1907240902Srmacklem			if (ep->ex_numsecflavors > 0)
1908240902Srmacklem				memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1909240902Srmacklem				    sizeof(ep->ex_secflavors));
19109336Sdfr		} else while (grp) {
19111558Srgrimes			hp = get_ht();
19121558Srgrimes			hp->ht_grp = grp;
19131558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
19141558Srgrimes			ep->ex_defdir->dp_hosts = hp;
1915240902Srmacklem			/* Save the security flavors list for this host set. */
1916240902Srmacklem			grp->gr_numsecflavors = ep->ex_numsecflavors;
1917240902Srmacklem			if (ep->ex_numsecflavors > 0)
1918240902Srmacklem				memcpy(grp->gr_secflavors, ep->ex_secflavors,
1919240902Srmacklem				    sizeof(ep->ex_secflavors));
19201558Srgrimes			grp = grp->gr_next;
19211558Srgrimes		}
19221558Srgrimes	} else {
19231558Srgrimes
19241558Srgrimes		/*
192537663Scharnier		 * Loop through the directories adding them to the tree.
19261558Srgrimes		 */
19271558Srgrimes		while (dp) {
19281558Srgrimes			dp2 = dp->dp_left;
1929240902Srmacklem			add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
19301558Srgrimes			dp = dp2;
19311558Srgrimes		}
19321558Srgrimes	}
19331558Srgrimes}
19341558Srgrimes
19351558Srgrimes/*
19361558Srgrimes * Traverse the binary tree either updating a node that is already there
19371558Srgrimes * for the new directory or adding the new node.
19381558Srgrimes */
19391558Srgrimesvoid
1940216587Scharnieradd_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
1941240902Srmacklem	int flags, struct exportlist *ep)
19421558Srgrimes{
19431558Srgrimes	struct dirlist *dp;
19441558Srgrimes	struct hostlist *hp;
19451558Srgrimes	int cmp;
19461558Srgrimes
19471558Srgrimes	dp = *dpp;
19481558Srgrimes	if (dp) {
19491558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
19501558Srgrimes		if (cmp > 0) {
1951240902Srmacklem			add_dlist(&dp->dp_left, newdp, grp, flags, ep);
19521558Srgrimes			return;
19531558Srgrimes		} else if (cmp < 0) {
1954240902Srmacklem			add_dlist(&dp->dp_right, newdp, grp, flags, ep);
19551558Srgrimes			return;
19561558Srgrimes		} else
19571558Srgrimes			free((caddr_t)newdp);
19581558Srgrimes	} else {
19591558Srgrimes		dp = newdp;
19601558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
19611558Srgrimes		*dpp = dp;
19621558Srgrimes	}
19631558Srgrimes	if (grp) {
19641558Srgrimes
19651558Srgrimes		/*
19661558Srgrimes		 * Hang all of the host(s) off of the directory point.
19671558Srgrimes		 */
19681558Srgrimes		do {
19691558Srgrimes			hp = get_ht();
19701558Srgrimes			hp->ht_grp = grp;
19711558Srgrimes			hp->ht_next = dp->dp_hosts;
19721558Srgrimes			dp->dp_hosts = hp;
1973240902Srmacklem			/* Save the security flavors list for this host set. */
1974240902Srmacklem			grp->gr_numsecflavors = ep->ex_numsecflavors;
1975240902Srmacklem			if (ep->ex_numsecflavors > 0)
1976240902Srmacklem				memcpy(grp->gr_secflavors, ep->ex_secflavors,
1977240902Srmacklem				    sizeof(ep->ex_secflavors));
19781558Srgrimes			grp = grp->gr_next;
19791558Srgrimes		} while (grp);
19809336Sdfr	} else {
19811558Srgrimes		dp->dp_flag |= DP_DEFSET;
1982240902Srmacklem		/* Save the default security flavors list. */
1983240902Srmacklem		ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1984240902Srmacklem		if (ep->ex_numsecflavors > 0)
1985240902Srmacklem			memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1986240902Srmacklem			    sizeof(ep->ex_secflavors));
19879336Sdfr	}
19881558Srgrimes}
19891558Srgrimes
19901558Srgrimes/*
19911558Srgrimes * Search for a dirpath on the export point.
19921558Srgrimes */
19931558Srgrimesstruct dirlist *
1994216587Scharnierdirp_search(struct dirlist *dp, char *dirp)
19951558Srgrimes{
19961558Srgrimes	int cmp;
19971558Srgrimes
19981558Srgrimes	if (dp) {
199974462Salfred		cmp = strcmp(dp->dp_dirp, dirp);
20001558Srgrimes		if (cmp > 0)
200174462Salfred			return (dirp_search(dp->dp_left, dirp));
20021558Srgrimes		else if (cmp < 0)
200374462Salfred			return (dirp_search(dp->dp_right, dirp));
20041558Srgrimes		else
20051558Srgrimes			return (dp);
20061558Srgrimes	}
20071558Srgrimes	return (dp);
20081558Srgrimes}
20091558Srgrimes
20101558Srgrimes/*
20111558Srgrimes * Scan for a host match in a directory tree.
20121558Srgrimes */
20131558Srgrimesint
2014216587Scharnierchk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2015240902Srmacklem	int *hostsetp, int *numsecflavors, int **secflavorsp)
20161558Srgrimes{
20171558Srgrimes	struct hostlist *hp;
20181558Srgrimes	struct grouplist *grp;
201974462Salfred	struct addrinfo *ai;
20201558Srgrimes
20211558Srgrimes	if (dp) {
20221558Srgrimes		if (dp->dp_flag & DP_DEFSET)
20239336Sdfr			*defsetp = dp->dp_flag;
20241558Srgrimes		hp = dp->dp_hosts;
20251558Srgrimes		while (hp) {
20261558Srgrimes			grp = hp->ht_grp;
20271558Srgrimes			switch (grp->gr_type) {
20281558Srgrimes			case GT_HOST:
202974462Salfred				ai = grp->gr_ptr.gt_addrinfo;
203074462Salfred				for (; ai; ai = ai->ai_next) {
203175801Siedowse					if (!sacmp(ai->ai_addr, saddr, NULL)) {
203274462Salfred						*hostsetp =
203374462Salfred						    (hp->ht_flag | DP_HOSTSET);
2034240902Srmacklem						if (numsecflavors != NULL) {
2035240902Srmacklem							*numsecflavors =
2036240902Srmacklem							    grp->gr_numsecflavors;
2037240902Srmacklem							*secflavorsp =
2038240902Srmacklem							    grp->gr_secflavors;
2039240902Srmacklem						}
204074462Salfred						return (1);
204174462Salfred					}
20429336Sdfr				}
204375801Siedowse				break;
20441558Srgrimes			case GT_NET:
204575801Siedowse				if (!sacmp(saddr, (struct sockaddr *)
204675801Siedowse				    &grp->gr_ptr.gt_net.nt_net,
204775801Siedowse				    (struct sockaddr *)
204875801Siedowse				    &grp->gr_ptr.gt_net.nt_mask)) {
204974462Salfred					*hostsetp = (hp->ht_flag | DP_HOSTSET);
2050240902Srmacklem					if (numsecflavors != NULL) {
2051240902Srmacklem						*numsecflavors =
2052240902Srmacklem						    grp->gr_numsecflavors;
2053240902Srmacklem						*secflavorsp =
2054240902Srmacklem						    grp->gr_secflavors;
2055240902Srmacklem					}
205674462Salfred					return (1);
205774462Salfred				}
205875801Siedowse				break;
205975801Siedowse			}
20601558Srgrimes			hp = hp->ht_next;
20611558Srgrimes		}
20621558Srgrimes	}
20631558Srgrimes	return (0);
20641558Srgrimes}
20651558Srgrimes
20661558Srgrimes/*
20671558Srgrimes * Scan tree for a host that matches the address.
20681558Srgrimes */
20691558Srgrimesint
2070216587Scharnierscan_tree(struct dirlist *dp, struct sockaddr *saddr)
20711558Srgrimes{
20729336Sdfr	int defset, hostset;
20731558Srgrimes
20741558Srgrimes	if (dp) {
20751558Srgrimes		if (scan_tree(dp->dp_left, saddr))
20761558Srgrimes			return (1);
2077240902Srmacklem		if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
20781558Srgrimes			return (1);
20791558Srgrimes		if (scan_tree(dp->dp_right, saddr))
20801558Srgrimes			return (1);
20811558Srgrimes	}
20821558Srgrimes	return (0);
20831558Srgrimes}
20841558Srgrimes
20851558Srgrimes/*
20861558Srgrimes * Traverse the dirlist tree and free it up.
20871558Srgrimes */
20881558Srgrimesvoid
2089216587Scharnierfree_dir(struct dirlist *dp)
20901558Srgrimes{
20911558Srgrimes
20921558Srgrimes	if (dp) {
20931558Srgrimes		free_dir(dp->dp_left);
20941558Srgrimes		free_dir(dp->dp_right);
20951558Srgrimes		free_host(dp->dp_hosts);
20961558Srgrimes		free((caddr_t)dp);
20971558Srgrimes	}
20981558Srgrimes}
20991558Srgrimes
21001558Srgrimes/*
2101184588Sdfr * Parse a colon separated list of security flavors
2102184588Sdfr */
2103184588Sdfrint
2104216587Scharnierparsesec(char *seclist, struct exportlist *ep)
2105184588Sdfr{
2106184588Sdfr	char *cp, savedc;
2107184588Sdfr	int flavor;
2108184588Sdfr
2109184588Sdfr	ep->ex_numsecflavors = 0;
2110184588Sdfr	for (;;) {
2111184588Sdfr		cp = strchr(seclist, ':');
2112184588Sdfr		if (cp) {
2113184588Sdfr			savedc = *cp;
2114184588Sdfr			*cp = '\0';
2115184588Sdfr		}
2116184588Sdfr
2117184588Sdfr		if (!strcmp(seclist, "sys"))
2118184588Sdfr			flavor = AUTH_SYS;
2119184588Sdfr		else if (!strcmp(seclist, "krb5"))
2120184588Sdfr			flavor = RPCSEC_GSS_KRB5;
2121184588Sdfr		else if (!strcmp(seclist, "krb5i"))
2122184588Sdfr			flavor = RPCSEC_GSS_KRB5I;
2123184588Sdfr		else if (!strcmp(seclist, "krb5p"))
2124184588Sdfr			flavor = RPCSEC_GSS_KRB5P;
2125184588Sdfr		else {
2126184588Sdfr			if (cp)
2127184588Sdfr				*cp = savedc;
2128184588Sdfr			syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2129184588Sdfr			return (1);
2130184588Sdfr		}
2131184588Sdfr		if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2132184588Sdfr			if (cp)
2133184588Sdfr				*cp = savedc;
2134184588Sdfr			syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2135184588Sdfr			return (1);
2136184588Sdfr		}
2137184588Sdfr		ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2138184588Sdfr		ep->ex_numsecflavors++;
2139184588Sdfr		if (cp) {
2140184588Sdfr			*cp = savedc;
2141184588Sdfr			seclist = cp + 1;
2142184588Sdfr		} else {
2143184588Sdfr			break;
2144184588Sdfr		}
2145184588Sdfr	}
2146184588Sdfr	return (0);
2147184588Sdfr}
2148184588Sdfr
2149184588Sdfr/*
21501558Srgrimes * Parse the option string and update fields.
21511558Srgrimes * Option arguments may either be -<option>=<value> or
21521558Srgrimes * -<option> <value>
21531558Srgrimes */
21541558Srgrimesint
2155216587Scharnierdo_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2156216587Scharnier	int *has_hostp, int *exflagsp, struct xucred *cr)
21571558Srgrimes{
21581558Srgrimes	char *cpoptarg, *cpoptend;
21591558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
21601558Srgrimes	int allflag, usedarg;
21611558Srgrimes
216251968Salfred	savedc2 = '\0';
21631558Srgrimes	cpopt = *cpp;
21641558Srgrimes	cpopt++;
21651558Srgrimes	cp = *endcpp;
21661558Srgrimes	savedc = *cp;
21671558Srgrimes	*cp = '\0';
21681558Srgrimes	while (cpopt && *cpopt) {
21691558Srgrimes		allflag = 1;
21701558Srgrimes		usedarg = -2;
217137663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
21721558Srgrimes			*cpoptend++ = '\0';
217337663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
21741558Srgrimes				*cpoptarg++ = '\0';
21751558Srgrimes		} else {
217637663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
21771558Srgrimes				*cpoptarg++ = '\0';
21781558Srgrimes			else {
21791558Srgrimes				*cp = savedc;
21801558Srgrimes				nextfield(&cp, &endcp);
21811558Srgrimes				**endcpp = '\0';
21821558Srgrimes				if (endcp > cp && *cp != '-') {
21831558Srgrimes					cpoptarg = cp;
21841558Srgrimes					savedc2 = *endcp;
21851558Srgrimes					*endcp = '\0';
21861558Srgrimes					usedarg = 0;
21871558Srgrimes				}
21881558Srgrimes			}
21891558Srgrimes		}
21901558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
21911558Srgrimes			*exflagsp |= MNT_EXRDONLY;
21921558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
21931558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
21941558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
21951558Srgrimes			usedarg++;
21961558Srgrimes			parsecred(cpoptarg, cr);
21971558Srgrimes			if (allflag == 0) {
21981558Srgrimes				*exflagsp |= MNT_EXPORTANON;
21991558Srgrimes				opt_flags |= OP_MAPALL;
22001558Srgrimes			} else
22011558Srgrimes				opt_flags |= OP_MAPROOT;
22021558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
220375801Siedowse		    !strcmp(cpopt, "m"))) {
22041558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
220537663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
22061558Srgrimes				return (1);
22071558Srgrimes			}
22081558Srgrimes			usedarg++;
22091558Srgrimes			opt_flags |= OP_MASK;
22101558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
22111558Srgrimes			!strcmp(cpopt, "n"))) {
221274462Salfred			if (strchr(cpoptarg, '/') != NULL) {
221374462Salfred				if (debug)
221474462Salfred					fprintf(stderr, "setting OP_MASKLEN\n");
221574462Salfred				opt_flags |= OP_MASKLEN;
221674462Salfred			}
22171558Srgrimes			if (grp->gr_type != GT_NULL) {
221837663Scharnier				syslog(LOG_ERR, "network/host conflict");
22191558Srgrimes				return (1);
22201558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
222137663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
22221558Srgrimes				return (1);
22231558Srgrimes			}
22241558Srgrimes			grp->gr_type = GT_NET;
22251558Srgrimes			*has_hostp = 1;
22261558Srgrimes			usedarg++;
22271558Srgrimes			opt_flags |= OP_NET;
22281558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
22291558Srgrimes			opt_flags |= OP_ALLDIRS;
223027447Sdfr		} else if (!strcmp(cpopt, "public")) {
223127447Sdfr			*exflagsp |= MNT_EXPUBLIC;
223227447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
223327447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
223427447Sdfr			opt_flags |= OP_MAPALL;
223527447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
223627447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
2237100336Sjoerg		} else if (!strcmp(cpopt, "quiet")) {
2238100336Sjoerg			opt_flags |= OP_QUIET;
2239247034Spluknet		} else if (cpoptarg && !strcmp(cpopt, "sec")) {
2240184588Sdfr			if (parsesec(cpoptarg, ep))
2241184588Sdfr				return (1);
2242184588Sdfr			opt_flags |= OP_SEC;
2243184588Sdfr			usedarg++;
22441558Srgrimes		} else {
224537663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
22461558Srgrimes			return (1);
22471558Srgrimes		}
22481558Srgrimes		if (usedarg >= 0) {
22491558Srgrimes			*endcp = savedc2;
22501558Srgrimes			**endcpp = savedc;
22511558Srgrimes			if (usedarg > 0) {
22521558Srgrimes				*cpp = cp;
22531558Srgrimes				*endcpp = endcp;
22541558Srgrimes			}
22551558Srgrimes			return (0);
22561558Srgrimes		}
22571558Srgrimes		cpopt = cpoptend;
22581558Srgrimes	}
22591558Srgrimes	**endcpp = savedc;
22601558Srgrimes	return (0);
22611558Srgrimes}
22621558Srgrimes
22631558Srgrimes/*
22641558Srgrimes * Translate a character string to the corresponding list of network
22651558Srgrimes * addresses for a hostname.
22661558Srgrimes */
22671558Srgrimesint
2268216587Scharnierget_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
22691558Srgrimes{
22707401Swpaul	struct grouplist *checkgrp;
227175635Siedowse	struct addrinfo *ai, *tai, hints;
227274462Salfred	int ecode;
227374462Salfred	char host[NI_MAXHOST];
22741558Srgrimes
227574462Salfred	if (grp->gr_type != GT_NULL) {
227674462Salfred		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
22771558Srgrimes		return (1);
22781558Srgrimes	}
227974462Salfred	memset(&hints, 0, sizeof hints);
228074462Salfred	hints.ai_flags = AI_CANONNAME;
228174462Salfred	hints.ai_protocol = IPPROTO_UDP;
228274462Salfred	ecode = getaddrinfo(cp, NULL, &hints, &ai);
228374462Salfred	if (ecode != 0) {
228475635Siedowse		syslog(LOG_ERR,"can't get address info for host %s", cp);
228574462Salfred		return 1;
228674462Salfred	}
228774462Salfred	grp->gr_ptr.gt_addrinfo = ai;
228874462Salfred	while (ai != NULL) {
228974462Salfred		if (ai->ai_canonname == NULL) {
229074462Salfred			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2291146187Sume			    sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
229274462Salfred				strlcpy(host, "?", sizeof(host));
229374462Salfred			ai->ai_canonname = strdup(host);
229474462Salfred			ai->ai_flags |= AI_CANONNAME;
229575641Siedowse		}
229674462Salfred		if (debug)
229775635Siedowse			fprintf(stderr, "got host %s\n", ai->ai_canonname);
229875635Siedowse		/*
229975635Siedowse		 * Sanity check: make sure we don't already have an entry
230075635Siedowse		 * for this host in the grouplist.
230175635Siedowse		 */
230275635Siedowse		for (checkgrp = tgrp; checkgrp != NULL;
230375635Siedowse		    checkgrp = checkgrp->gr_next) {
230475635Siedowse			if (checkgrp->gr_type != GT_HOST)
230575635Siedowse				continue;
230675635Siedowse			for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
230775635Siedowse			    tai = tai->ai_next) {
230875801Siedowse				if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
230975635Siedowse					continue;
231075635Siedowse				if (debug)
231175635Siedowse					fprintf(stderr,
231275635Siedowse					    "ignoring duplicate host %s\n",
231375635Siedowse					    ai->ai_canonname);
231475635Siedowse				grp->gr_type = GT_IGNORE;
231575635Siedowse				return (0);
231675635Siedowse			}
231775635Siedowse		}
231874462Salfred		ai = ai->ai_next;
23191558Srgrimes	}
232075635Siedowse	grp->gr_type = GT_HOST;
23211558Srgrimes	return (0);
23221558Srgrimes}
23231558Srgrimes
23241558Srgrimes/*
23251558Srgrimes * Free up an exports list component
23261558Srgrimes */
23271558Srgrimesvoid
2328216587Scharnierfree_exp(struct exportlist *ep)
23291558Srgrimes{
23301558Srgrimes
23311558Srgrimes	if (ep->ex_defdir) {
23321558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
23331558Srgrimes		free((caddr_t)ep->ex_defdir);
23341558Srgrimes	}
23351558Srgrimes	if (ep->ex_fsdir)
23361558Srgrimes		free(ep->ex_fsdir);
233727447Sdfr	if (ep->ex_indexfile)
233827447Sdfr		free(ep->ex_indexfile);
23391558Srgrimes	free_dir(ep->ex_dirl);
23401558Srgrimes	free((caddr_t)ep);
23411558Srgrimes}
23421558Srgrimes
23431558Srgrimes/*
23441558Srgrimes * Free hosts.
23451558Srgrimes */
23461558Srgrimesvoid
2347216587Scharnierfree_host(struct hostlist *hp)
23481558Srgrimes{
23491558Srgrimes	struct hostlist *hp2;
23501558Srgrimes
23511558Srgrimes	while (hp) {
23521558Srgrimes		hp2 = hp;
23531558Srgrimes		hp = hp->ht_next;
23541558Srgrimes		free((caddr_t)hp2);
23551558Srgrimes	}
23561558Srgrimes}
23571558Srgrimes
23581558Srgrimesstruct hostlist *
2359216587Scharnierget_ht(void)
23601558Srgrimes{
23611558Srgrimes	struct hostlist *hp;
23621558Srgrimes
23631558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
23641558Srgrimes	if (hp == (struct hostlist *)NULL)
23651558Srgrimes		out_of_mem();
23661558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
23679336Sdfr	hp->ht_flag = 0;
23681558Srgrimes	return (hp);
23691558Srgrimes}
23701558Srgrimes
23711558Srgrimes/*
23721558Srgrimes * Out of memory, fatal
23731558Srgrimes */
23741558Srgrimesvoid
2375216587Scharnierout_of_mem(void)
23761558Srgrimes{
23771558Srgrimes
237837663Scharnier	syslog(LOG_ERR, "out of memory");
23791558Srgrimes	exit(2);
23801558Srgrimes}
23811558Srgrimes
23821558Srgrimes/*
2383158857Srodrigc * Do the nmount() syscall with the update flag to push the export info into
23841558Srgrimes * the kernel.
23851558Srgrimes */
23861558Srgrimesint
2387158857Srodrigcdo_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2388158857Srodrigc    struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
23891558Srgrimes{
239075841Siedowse	struct statfs fsb1;
239174462Salfred	struct addrinfo *ai;
2392192934Srmacklem	struct export_args ea, *eap;
2393158857Srodrigc	char errmsg[255];
2394158857Srodrigc	char *cp;
23951558Srgrimes	int done;
2396158857Srodrigc	char savedc;
2397158857Srodrigc	struct iovec *iov;
2398184588Sdfr	int i, iovlen;
2399158857Srodrigc	int ret;
2400192934Srmacklem	struct nfsex_args nfsea;
24011558Srgrimes
2402192934Srmacklem	if (run_v4server > 0)
2403192934Srmacklem		eap = &nfsea.export;
2404192934Srmacklem	else
2405192934Srmacklem		eap = &ea;
2406192934Srmacklem
2407158857Srodrigc	cp = NULL;
2408158857Srodrigc	savedc = '\0';
2409158857Srodrigc	iov = NULL;
2410158857Srodrigc	iovlen = 0;
2411158857Srodrigc	ret = 0;
241275801Siedowse
2413192934Srmacklem	bzero(eap, sizeof (struct export_args));
2414158857Srodrigc	bzero(errmsg, sizeof(errmsg));
2415192934Srmacklem	eap->ex_flags = exflags;
2416192934Srmacklem	eap->ex_anon = *anoncrp;
2417192934Srmacklem	eap->ex_indexfile = ep->ex_indexfile;
241875641Siedowse	if (grp->gr_type == GT_HOST)
241974462Salfred		ai = grp->gr_ptr.gt_addrinfo;
242075641Siedowse	else
242175641Siedowse		ai = NULL;
2422192934Srmacklem	eap->ex_numsecflavors = ep->ex_numsecflavors;
2423192934Srmacklem	for (i = 0; i < eap->ex_numsecflavors; i++)
2424192934Srmacklem		eap->ex_secflavors[i] = ep->ex_secflavors[i];
2425192934Srmacklem	if (eap->ex_numsecflavors == 0) {
2426192934Srmacklem		eap->ex_numsecflavors = 1;
2427192934Srmacklem		eap->ex_secflavors[0] = AUTH_SYS;
2428184588Sdfr	}
24291558Srgrimes	done = FALSE;
2430158857Srodrigc
2431192934Srmacklem	if (v4root_phase == 0) {
2432192934Srmacklem		build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2433192934Srmacklem		build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2434192934Srmacklem		build_iovec(&iov, &iovlen, "from", NULL, 0);
2435192934Srmacklem		build_iovec(&iov, &iovlen, "update", NULL, 0);
2436192934Srmacklem		build_iovec(&iov, &iovlen, "export", eap,
2437192934Srmacklem		    sizeof (struct export_args));
2438192934Srmacklem		build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2439192934Srmacklem	}
2440158857Srodrigc
24411558Srgrimes	while (!done) {
24421558Srgrimes		switch (grp->gr_type) {
24431558Srgrimes		case GT_HOST:
244475641Siedowse			if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
244574462Salfred				goto skip;
2446192934Srmacklem			eap->ex_addr = ai->ai_addr;
2447192934Srmacklem			eap->ex_addrlen = ai->ai_addrlen;
2448192934Srmacklem			eap->ex_masklen = 0;
24491558Srgrimes			break;
24501558Srgrimes		case GT_NET:
245175801Siedowse			if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
245274462Salfred			    have_v6 == 0)
245374462Salfred				goto skip;
2454192934Srmacklem			eap->ex_addr =
245575801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2456192934Srmacklem			eap->ex_addrlen =
2457158857Srodrigc			    ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2458192934Srmacklem			eap->ex_mask =
245975801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2460192934Srmacklem			eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
24611558Srgrimes			break;
246275641Siedowse		case GT_DEFAULT:
2463192934Srmacklem			eap->ex_addr = NULL;
2464192934Srmacklem			eap->ex_addrlen = 0;
2465192934Srmacklem			eap->ex_mask = NULL;
2466192934Srmacklem			eap->ex_masklen = 0;
246775641Siedowse			break;
24687401Swpaul		case GT_IGNORE:
2469158857Srodrigc			ret = 0;
2470158857Srodrigc			goto error_exit;
24717401Swpaul			break;
24721558Srgrimes		default:
247337663Scharnier			syslog(LOG_ERR, "bad grouptype");
24741558Srgrimes			if (cp)
24751558Srgrimes				*cp = savedc;
2476158857Srodrigc			ret = 1;
2477158857Srodrigc			goto error_exit;
24781558Srgrimes		};
24791558Srgrimes
24801558Srgrimes		/*
2481192934Srmacklem		 * For V4:, use the nfssvc() syscall, instead of mount().
24821558Srgrimes		 */
2483192934Srmacklem		if (v4root_phase == 2) {
2484192934Srmacklem			nfsea.fspec = v4root_dirpath;
2485192934Srmacklem			if (run_v4server > 0 &&
2486192934Srmacklem			    nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2487192934Srmacklem				syslog(LOG_ERR, "Exporting V4: failed");
2488192934Srmacklem				return (2);
2489158857Srodrigc			}
2490192934Srmacklem		} else {
2491192934Srmacklem			/*
2492192934Srmacklem			 * XXX:
2493192934Srmacklem			 * Maybe I should just use the fsb->f_mntonname path
2494192934Srmacklem			 * instead of looping back up the dirp to the mount
2495192934Srmacklem			 * point??
2496192934Srmacklem			 * Also, needs to know how to export all types of local
2497192934Srmacklem			 * exportable filesystems and not just "ufs".
2498192934Srmacklem			 */
2499192934Srmacklem			iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2500192934Srmacklem			iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2501192934Srmacklem			iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2502192934Srmacklem			iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2503192934Srmacklem			iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2504192934Srmacklem			iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2505272428Sbdrewery			errmsg[0] = '\0';
2506192934Srmacklem
2507192934Srmacklem			while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2508192934Srmacklem				if (cp)
2509192934Srmacklem					*cp-- = savedc;
2510192934Srmacklem				else
2511192934Srmacklem					cp = dirp + dirplen - 1;
2512192934Srmacklem				if (opt_flags & OP_QUIET) {
2513192934Srmacklem					ret = 1;
2514192934Srmacklem					goto error_exit;
2515192934Srmacklem				}
2516192934Srmacklem				if (errno == EPERM) {
2517192934Srmacklem					if (debug)
2518239744Sdelphij						warnx("can't change attributes for %s: %s",
2519239744Sdelphij						    dirp, errmsg);
2520192934Srmacklem					syslog(LOG_ERR,
2521239744Sdelphij					   "can't change attributes for %s: %s",
2522239744Sdelphij					    dirp, errmsg);
2523192934Srmacklem					ret = 1;
2524192934Srmacklem					goto error_exit;
2525192934Srmacklem				}
2526192934Srmacklem				if (opt_flags & OP_ALLDIRS) {
2527192934Srmacklem					if (errno == EINVAL)
2528192934Srmacklem						syslog(LOG_ERR,
2529100336Sjoerg		"-alldirs requested but %s is not a filesystem mountpoint",
2530192934Srmacklem						    dirp);
2531192934Srmacklem					else
2532192934Srmacklem						syslog(LOG_ERR,
2533192934Srmacklem						    "could not remount %s: %m",
2534192934Srmacklem						    dirp);
2535192934Srmacklem					ret = 1;
2536192934Srmacklem					goto error_exit;
2537192934Srmacklem				}
2538192934Srmacklem				/* back up over the last component */
2539192934Srmacklem				while (*cp == '/' && cp > dirp)
2540192934Srmacklem					cp--;
2541192934Srmacklem				while (*(cp - 1) != '/' && cp > dirp)
2542192934Srmacklem					cp--;
2543192934Srmacklem				if (cp == dirp) {
2544192934Srmacklem					if (debug)
2545192934Srmacklem						warnx("mnt unsucc");
2546192934Srmacklem					syslog(LOG_ERR, "can't export %s %s",
2547192934Srmacklem					    dirp, errmsg);
2548192934Srmacklem					ret = 1;
2549192934Srmacklem					goto error_exit;
2550192934Srmacklem				}
2551192934Srmacklem				savedc = *cp;
2552192934Srmacklem				*cp = '\0';
2553192934Srmacklem				/*
2554192934Srmacklem				 * Check that we're still on the same
2555192934Srmacklem				 * filesystem.
2556192934Srmacklem				 */
2557192934Srmacklem				if (statfs(dirp, &fsb1) != 0 ||
2558192934Srmacklem				    bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2559192934Srmacklem				    sizeof (fsb1.f_fsid)) != 0) {
2560192934Srmacklem					*cp = savedc;
2561100336Sjoerg					syslog(LOG_ERR,
2562192934Srmacklem					    "can't export %s %s", dirp,
2563192934Srmacklem					    errmsg);
2564192934Srmacklem					ret = 1;
2565192934Srmacklem					goto error_exit;
2566192934Srmacklem				}
25671558Srgrimes			}
25681558Srgrimes		}
2569192934Srmacklem
2570192934Srmacklem		/*
2571192934Srmacklem		 * For the experimental server:
2572192934Srmacklem		 * If this is the public directory, get the file handle
2573192934Srmacklem		 * and load it into the kernel via the nfssvc() syscall.
2574192934Srmacklem		 */
2575192934Srmacklem		if (run_v4server > 0 && (exflags & MNT_EXPUBLIC) != 0) {
2576192934Srmacklem			fhandle_t fh;
2577192934Srmacklem			char *public_name;
2578192934Srmacklem
2579192934Srmacklem			if (eap->ex_indexfile != NULL)
2580192934Srmacklem				public_name = eap->ex_indexfile;
2581192934Srmacklem			else
2582192934Srmacklem				public_name = dirp;
2583192934Srmacklem			if (getfh(public_name, &fh) < 0)
2584192934Srmacklem				syslog(LOG_ERR,
2585192934Srmacklem				    "Can't get public fh for %s", public_name);
2586192934Srmacklem			else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2587192934Srmacklem				syslog(LOG_ERR,
2588192934Srmacklem				    "Can't set public fh for %s", public_name);
2589192934Srmacklem			else
2590192934Srmacklem				has_publicfh = 1;
2591192934Srmacklem		}
259274462Salfredskip:
259375641Siedowse		if (ai != NULL)
259474462Salfred			ai = ai->ai_next;
259575641Siedowse		if (ai == NULL)
25961558Srgrimes			done = TRUE;
25971558Srgrimes	}
25981558Srgrimes	if (cp)
25991558Srgrimes		*cp = savedc;
2600158857Srodrigcerror_exit:
2601158857Srodrigc	/* free strings allocated by strdup() in getmntopts.c */
2602158857Srodrigc	if (iov != NULL) {
2603158857Srodrigc		free(iov[0].iov_base); /* fstype */
2604158857Srodrigc		free(iov[2].iov_base); /* fspath */
2605158857Srodrigc		free(iov[4].iov_base); /* from */
2606158857Srodrigc		free(iov[6].iov_base); /* update */
2607158857Srodrigc		free(iov[8].iov_base); /* export */
2608158857Srodrigc		free(iov[10].iov_base); /* errmsg */
2609158857Srodrigc
2610158857Srodrigc		/* free iov, allocated by realloc() */
2611158857Srodrigc		free(iov);
2612158857Srodrigc	}
2613158857Srodrigc	return (ret);
26141558Srgrimes}
26151558Srgrimes
26161558Srgrimes/*
26171558Srgrimes * Translate a net address.
261875801Siedowse *
261975801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
26201558Srgrimes */
26211558Srgrimesint
2622216587Scharnierget_net(char *cp, struct netmsk *net, int maskflg)
26231558Srgrimes{
262475861Siedowse	struct netent *np = NULL;
262574462Salfred	char *name, *p, *prefp;
262675801Siedowse	struct sockaddr_in sin;
262775861Siedowse	struct sockaddr *sa = NULL;
262874462Salfred	struct addrinfo hints, *ai = NULL;
262974462Salfred	char netname[NI_MAXHOST];
263074462Salfred	long preflen;
26311558Srgrimes
263275635Siedowse	p = prefp = NULL;
263374462Salfred	if ((opt_flags & OP_MASKLEN) && !maskflg) {
263474462Salfred		p = strchr(cp, '/');
263574462Salfred		*p = '\0';
263674462Salfred		prefp = p + 1;
263774462Salfred	}
263874462Salfred
263975861Siedowse	/*
264075861Siedowse	 * Check for a numeric address first. We wish to avoid
264175861Siedowse	 * possible DNS lookups in getnetbyname().
264275861Siedowse	 */
264375861Siedowse	if (isxdigit(*cp) || *cp == ':') {
264474462Salfred		memset(&hints, 0, sizeof hints);
264575801Siedowse		/* Ensure the mask and the network have the same family. */
264675801Siedowse		if (maskflg && (opt_flags & OP_NET))
264775801Siedowse			hints.ai_family = net->nt_net.ss_family;
264875801Siedowse		else if (!maskflg && (opt_flags & OP_HAVEMASK))
264975801Siedowse			hints.ai_family = net->nt_mask.ss_family;
265075801Siedowse		else
265175801Siedowse			hints.ai_family = AF_UNSPEC;
265274462Salfred		hints.ai_flags = AI_NUMERICHOST;
265375861Siedowse		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
265475861Siedowse			sa = ai->ai_addr;
265575861Siedowse		if (sa != NULL && ai->ai_family == AF_INET) {
265674462Salfred			/*
265775801Siedowse			 * The address in `cp' is really a network address, so
265875801Siedowse			 * use inet_network() to re-interpret this correctly.
265975801Siedowse			 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
266074462Salfred			 */
266175801Siedowse			bzero(&sin, sizeof sin);
266274462Salfred			sin.sin_family = AF_INET;
266374462Salfred			sin.sin_len = sizeof sin;
266475801Siedowse			sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
266574462Salfred			if (debug)
266675801Siedowse				fprintf(stderr, "get_net: v4 addr %s\n",
266775801Siedowse				    inet_ntoa(sin.sin_addr));
266874462Salfred			sa = (struct sockaddr *)&sin;
266975861Siedowse		}
267075861Siedowse	}
267175861Siedowse	if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
267275861Siedowse		bzero(&sin, sizeof sin);
267375861Siedowse		sin.sin_family = AF_INET;
267475861Siedowse		sin.sin_len = sizeof sin;
267575861Siedowse		sin.sin_addr = inet_makeaddr(np->n_net, 0);
267675861Siedowse		sa = (struct sockaddr *)&sin;
267775861Siedowse	}
267875861Siedowse	if (sa == NULL)
267974462Salfred		goto fail;
268025318Spst
268175801Siedowse	if (maskflg) {
268275801Siedowse		/* The specified sockaddr is a mask. */
268375801Siedowse		if (checkmask(sa) != 0)
268475801Siedowse			goto fail;
268575801Siedowse		bcopy(sa, &net->nt_mask, sa->sa_len);
268675801Siedowse		opt_flags |= OP_HAVEMASK;
268775801Siedowse	} else {
268875801Siedowse		/* The specified sockaddr is a network address. */
268975801Siedowse		bcopy(sa, &net->nt_net, sa->sa_len);
269074462Salfred
269175801Siedowse		/* Get a network name for the export list. */
269275801Siedowse		if (np) {
269375801Siedowse			name = np->n_name;
269475801Siedowse		} else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2695146187Sume		   NULL, 0, NI_NUMERICHOST) == 0) {
269675801Siedowse			name = netname;
269775801Siedowse		} else {
269875801Siedowse			goto fail;
269975801Siedowse		}
270075801Siedowse		if ((net->nt_name = strdup(name)) == NULL)
270175801Siedowse			out_of_mem();
270275801Siedowse
270375801Siedowse		/*
270475801Siedowse		 * Extract a mask from either a "/<masklen>" suffix, or
270575801Siedowse		 * from the class of an IPv4 address.
270675801Siedowse		 */
270774462Salfred		if (opt_flags & OP_MASKLEN) {
270874462Salfred			preflen = strtol(prefp, NULL, 10);
270975801Siedowse			if (preflen < 0L || preflen == LONG_MAX)
271074462Salfred				goto fail;
271175801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
271275801Siedowse			if (makemask(&net->nt_mask, (int)preflen) != 0)
271375801Siedowse				goto fail;
271475801Siedowse			opt_flags |= OP_HAVEMASK;
271574462Salfred			*p = '/';
271675801Siedowse		} else if (sa->sa_family == AF_INET &&
271775801Siedowse		    (opt_flags & OP_MASK) == 0) {
271875801Siedowse			in_addr_t addr;
271974462Salfred
272075801Siedowse			addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
272175801Siedowse			if (IN_CLASSA(addr))
272275801Siedowse				preflen = 8;
272375801Siedowse			else if (IN_CLASSB(addr))
272475801Siedowse				preflen = 16;
272575801Siedowse			else if (IN_CLASSC(addr))
272675801Siedowse				preflen = 24;
272775801Siedowse			else if (IN_CLASSD(addr))
272875801Siedowse				preflen = 28;
272975801Siedowse			else
273075801Siedowse				preflen = 32;	/* XXX */
273175801Siedowse
273275801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
273375801Siedowse			makemask(&net->nt_mask, (int)preflen);
273475801Siedowse			opt_flags |= OP_HAVEMASK;
273574462Salfred		}
273674462Salfred	}
273774462Salfred
273874462Salfred	if (ai)
273974462Salfred		freeaddrinfo(ai);
274074462Salfred	return 0;
274174462Salfred
274274462Salfredfail:
274374462Salfred	if (ai)
274474462Salfred		freeaddrinfo(ai);
274574462Salfred	return 1;
27461558Srgrimes}
27471558Srgrimes
27481558Srgrimes/*
27491558Srgrimes * Parse out the next white space separated field
27501558Srgrimes */
27511558Srgrimesvoid
2752216587Scharniernextfield(char **cp, char **endcp)
27531558Srgrimes{
27541558Srgrimes	char *p;
27551558Srgrimes
27561558Srgrimes	p = *cp;
27571558Srgrimes	while (*p == ' ' || *p == '\t')
27581558Srgrimes		p++;
27591558Srgrimes	if (*p == '\n' || *p == '\0')
27601558Srgrimes		*cp = *endcp = p;
27611558Srgrimes	else {
27621558Srgrimes		*cp = p++;
27631558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
27641558Srgrimes			p++;
27651558Srgrimes		*endcp = p;
27661558Srgrimes	}
27671558Srgrimes}
27681558Srgrimes
27691558Srgrimes/*
27701558Srgrimes * Get an exports file line. Skip over blank lines and handle line
27711558Srgrimes * continuations.
27721558Srgrimes */
27731558Srgrimesint
2774216587Scharnierget_line(void)
27751558Srgrimes{
27761558Srgrimes	char *p, *cp;
277796622Siedowse	size_t len;
27781558Srgrimes	int totlen, cont_line;
27791558Srgrimes
27801558Srgrimes	/*
27811558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
27821558Srgrimes	 */
27831558Srgrimes	p = line;
27841558Srgrimes	totlen = 0;
27851558Srgrimes	do {
278696622Siedowse		if ((p = fgetln(exp_file, &len)) == NULL)
27871558Srgrimes			return (0);
27881558Srgrimes		cp = p + len - 1;
27891558Srgrimes		cont_line = 0;
27901558Srgrimes		while (cp >= p &&
27911558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
27921558Srgrimes			if (*cp == '\\')
27931558Srgrimes				cont_line = 1;
27941558Srgrimes			cp--;
27951558Srgrimes			len--;
27961558Srgrimes		}
279779117Sdd		if (cont_line) {
279879117Sdd			*++cp = ' ';
279979117Sdd			len++;
280079117Sdd		}
280196622Siedowse		if (linesize < len + totlen + 1) {
280296622Siedowse			linesize = len + totlen + 1;
280396622Siedowse			line = realloc(line, linesize);
280496622Siedowse			if (line == NULL)
280596622Siedowse				out_of_mem();
28061558Srgrimes		}
280796622Siedowse		memcpy(line + totlen, p, len);
280896622Siedowse		totlen += len;
280996622Siedowse		line[totlen] = '\0';
28101558Srgrimes	} while (totlen == 0 || cont_line);
28111558Srgrimes	return (1);
28121558Srgrimes}
28131558Srgrimes
28141558Srgrimes/*
28151558Srgrimes * Parse a description of a credential.
28161558Srgrimes */
28171558Srgrimesvoid
2818216587Scharnierparsecred(char *namelist, struct xucred *cr)
28191558Srgrimes{
28201558Srgrimes	char *name;
28211558Srgrimes	int cnt;
28221558Srgrimes	char *names;
28231558Srgrimes	struct passwd *pw;
28241558Srgrimes	struct group *gr;
2825194498Sbrooks	gid_t groups[XU_NGROUPS + 1];
2826136051Sstefanf	int ngroups;
28271558Srgrimes
282891354Sdd	cr->cr_version = XUCRED_VERSION;
28291558Srgrimes	/*
283037663Scharnier	 * Set up the unprivileged user.
28311558Srgrimes	 */
28321558Srgrimes	cr->cr_uid = -2;
28331558Srgrimes	cr->cr_groups[0] = -2;
28341558Srgrimes	cr->cr_ngroups = 1;
28351558Srgrimes	/*
28361558Srgrimes	 * Get the user's password table entry.
28371558Srgrimes	 */
28381558Srgrimes	names = strsep(&namelist, " \t\n");
28391558Srgrimes	name = strsep(&names, ":");
28401558Srgrimes	if (isdigit(*name) || *name == '-')
28411558Srgrimes		pw = getpwuid(atoi(name));
28421558Srgrimes	else
28431558Srgrimes		pw = getpwnam(name);
28441558Srgrimes	/*
28451558Srgrimes	 * Credentials specified as those of a user.
28461558Srgrimes	 */
28471558Srgrimes	if (names == NULL) {
28481558Srgrimes		if (pw == NULL) {
284937663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
28501558Srgrimes			return;
28511558Srgrimes		}
28521558Srgrimes		cr->cr_uid = pw->pw_uid;
2853194498Sbrooks		ngroups = XU_NGROUPS + 1;
28541558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
285537663Scharnier			syslog(LOG_ERR, "too many groups");
28561558Srgrimes		/*
2857136051Sstefanf		 * Compress out duplicate.
28581558Srgrimes		 */
28591558Srgrimes		cr->cr_ngroups = ngroups - 1;
28601558Srgrimes		cr->cr_groups[0] = groups[0];
28611558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
28621558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
28631558Srgrimes		return;
28641558Srgrimes	}
28651558Srgrimes	/*
28661558Srgrimes	 * Explicit credential specified as a colon separated list:
28671558Srgrimes	 *	uid:gid:gid:...
28681558Srgrimes	 */
28691558Srgrimes	if (pw != NULL)
28701558Srgrimes		cr->cr_uid = pw->pw_uid;
28711558Srgrimes	else if (isdigit(*name) || *name == '-')
28721558Srgrimes		cr->cr_uid = atoi(name);
28731558Srgrimes	else {
287437663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
28751558Srgrimes		return;
28761558Srgrimes	}
28771558Srgrimes	cr->cr_ngroups = 0;
2878194498Sbrooks	while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
28791558Srgrimes		name = strsep(&names, ":");
28801558Srgrimes		if (isdigit(*name) || *name == '-') {
28811558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
28821558Srgrimes		} else {
28831558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
288437663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
28851558Srgrimes				continue;
28861558Srgrimes			}
28871558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
28881558Srgrimes		}
28891558Srgrimes	}
2890194498Sbrooks	if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
289137663Scharnier		syslog(LOG_ERR, "too many groups");
28921558Srgrimes}
28931558Srgrimes
2894194880Sdfr#define	STRSIZ	(MNTNAMLEN+MNTPATHLEN+50)
28951558Srgrimes/*
28961558Srgrimes * Routines that maintain the remote mounttab
28971558Srgrimes */
28981558Srgrimesvoid
2899216587Scharnierget_mountlist(void)
29001558Srgrimes{
29011558Srgrimes	struct mountlist *mlp, **mlpp;
290223681Speter	char *host, *dirp, *cp;
29031558Srgrimes	char str[STRSIZ];
29041558Srgrimes	FILE *mlfile;
29051558Srgrimes
29061558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
290753117Sbillf		if (errno == ENOENT)
290853117Sbillf			return;
290953117Sbillf		else {
291053117Sbillf			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
291153117Sbillf			return;
291253117Sbillf		}
29131558Srgrimes	}
29141558Srgrimes	mlpp = &mlhead;
29151558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
291623681Speter		cp = str;
291723681Speter		host = strsep(&cp, " \t\n");
291823681Speter		dirp = strsep(&cp, " \t\n");
291923681Speter		if (host == NULL || dirp == NULL)
29201558Srgrimes			continue;
29211558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
292237663Scharnier		if (mlp == (struct mountlist *)NULL)
292337663Scharnier			out_of_mem();
2924194880Sdfr		strncpy(mlp->ml_host, host, MNTNAMLEN);
2925194880Sdfr		mlp->ml_host[MNTNAMLEN] = '\0';
2926194880Sdfr		strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2927194880Sdfr		mlp->ml_dirp[MNTPATHLEN] = '\0';
29281558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
29291558Srgrimes		*mlpp = mlp;
29301558Srgrimes		mlpp = &mlp->ml_next;
29311558Srgrimes	}
29321558Srgrimes	fclose(mlfile);
29331558Srgrimes}
29341558Srgrimes
293575635Siedowsevoid
293675635Siedowsedel_mlist(char *hostp, char *dirp)
29371558Srgrimes{
29381558Srgrimes	struct mountlist *mlp, **mlpp;
29391558Srgrimes	struct mountlist *mlp2;
29401558Srgrimes	FILE *mlfile;
29411558Srgrimes	int fnd = 0;
29421558Srgrimes
29431558Srgrimes	mlpp = &mlhead;
29441558Srgrimes	mlp = mlhead;
29451558Srgrimes	while (mlp) {
29461558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
29471558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
29481558Srgrimes			fnd = 1;
29491558Srgrimes			mlp2 = mlp;
29501558Srgrimes			*mlpp = mlp = mlp->ml_next;
29511558Srgrimes			free((caddr_t)mlp2);
29521558Srgrimes		} else {
29531558Srgrimes			mlpp = &mlp->ml_next;
29541558Srgrimes			mlp = mlp->ml_next;
29551558Srgrimes		}
29561558Srgrimes	}
29571558Srgrimes	if (fnd) {
29581558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
295937663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
29601558Srgrimes			return;
29611558Srgrimes		}
29621558Srgrimes		mlp = mlhead;
29631558Srgrimes		while (mlp) {
29641558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
29651558Srgrimes			mlp = mlp->ml_next;
29661558Srgrimes		}
29671558Srgrimes		fclose(mlfile);
29681558Srgrimes	}
29691558Srgrimes}
29701558Srgrimes
29711558Srgrimesvoid
2972216587Scharnieradd_mlist(char *hostp, char *dirp)
29731558Srgrimes{
29741558Srgrimes	struct mountlist *mlp, **mlpp;
29751558Srgrimes	FILE *mlfile;
29761558Srgrimes
29771558Srgrimes	mlpp = &mlhead;
29781558Srgrimes	mlp = mlhead;
29791558Srgrimes	while (mlp) {
29801558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
29811558Srgrimes			return;
29821558Srgrimes		mlpp = &mlp->ml_next;
29831558Srgrimes		mlp = mlp->ml_next;
29841558Srgrimes	}
29851558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
298637663Scharnier	if (mlp == (struct mountlist *)NULL)
298737663Scharnier		out_of_mem();
2988194880Sdfr	strncpy(mlp->ml_host, hostp, MNTNAMLEN);
2989194880Sdfr	mlp->ml_host[MNTNAMLEN] = '\0';
2990194880Sdfr	strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2991194880Sdfr	mlp->ml_dirp[MNTPATHLEN] = '\0';
29921558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
29931558Srgrimes	*mlpp = mlp;
29941558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
299537663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
29961558Srgrimes		return;
29971558Srgrimes	}
29981558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
29991558Srgrimes	fclose(mlfile);
30001558Srgrimes}
30011558Srgrimes
30021558Srgrimes/*
30031558Srgrimes * Free up a group list.
30041558Srgrimes */
30051558Srgrimesvoid
3006216587Scharnierfree_grp(struct grouplist *grp)
30071558Srgrimes{
30081558Srgrimes	if (grp->gr_type == GT_HOST) {
300974462Salfred		if (grp->gr_ptr.gt_addrinfo != NULL)
301074462Salfred			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
30111558Srgrimes	} else if (grp->gr_type == GT_NET) {
30121558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
30131558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
30141558Srgrimes	}
30151558Srgrimes	free((caddr_t)grp);
30161558Srgrimes}
30171558Srgrimes
30181558Srgrimes#ifdef DEBUG
30191558Srgrimesvoid
30201558SrgrimesSYSLOG(int pri, const char *fmt, ...)
30211558Srgrimes{
30221558Srgrimes	va_list ap;
30231558Srgrimes
30241558Srgrimes	va_start(ap, fmt);
30251558Srgrimes	vfprintf(stderr, fmt, ap);
30261558Srgrimes	va_end(ap);
30271558Srgrimes}
30281558Srgrimes#endif /* DEBUG */
30291558Srgrimes
30301558Srgrimes/*
30311558Srgrimes * Check options for consistency.
30321558Srgrimes */
30331558Srgrimesint
3034216587Scharniercheck_options(struct dirlist *dp)
30351558Srgrimes{
30361558Srgrimes
3037192934Srmacklem	if (v4root_phase == 0 && dp == NULL)
30381558Srgrimes	    return (1);
303983653Speter	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
304083653Speter	    syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
30411558Srgrimes	    return (1);
30421558Srgrimes	}
30431558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
304475801Siedowse		syslog(LOG_ERR, "-mask requires -network");
304575801Siedowse		return (1);
30461558Srgrimes	}
304775801Siedowse	if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
304875801Siedowse		syslog(LOG_ERR, "-network requires mask specification");
304975801Siedowse		return (1);
305075801Siedowse	}
305175801Siedowse	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
305275801Siedowse		syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
305375801Siedowse		return (1);
305475801Siedowse	}
3055192934Srmacklem	if (v4root_phase > 0 &&
3056192934Srmacklem	    (opt_flags &
3057192934Srmacklem	     ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3058192934Srmacklem	    syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3059192934Srmacklem	    return (1);
3060192934Srmacklem	}
3061207689Srmacklem	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3062207689Srmacklem	    syslog(LOG_ERR, "-alldirs has multiple directories");
3063207689Srmacklem	    return (1);
3064207689Srmacklem	}
30651558Srgrimes	return (0);
30661558Srgrimes}
30671558Srgrimes
30681558Srgrimes/*
30691558Srgrimes * Check an absolute directory path for any symbolic links. Return true
30701558Srgrimes */
30711558Srgrimesint
3072216587Scharniercheck_dirpath(char *dirp)
30731558Srgrimes{
30741558Srgrimes	char *cp;
30751558Srgrimes	int ret = 1;
30761558Srgrimes	struct stat sb;
30771558Srgrimes
30781558Srgrimes	cp = dirp + 1;
30791558Srgrimes	while (*cp && ret) {
30801558Srgrimes		if (*cp == '/') {
30811558Srgrimes			*cp = '\0';
30829336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
30831558Srgrimes				ret = 0;
30841558Srgrimes			*cp = '/';
30851558Srgrimes		}
30861558Srgrimes		cp++;
30871558Srgrimes	}
30889336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
30891558Srgrimes		ret = 0;
30901558Srgrimes	return (ret);
30911558Srgrimes}
30929336Sdfr
309375801Siedowse/*
309475801Siedowse * Make a netmask according to the specified prefix length. The ss_family
309575801Siedowse * and other non-address fields must be initialised before calling this.
309675801Siedowse */
309775801Siedowseint
309875801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen)
309974462Salfred{
310075801Siedowse	u_char *p;
310175801Siedowse	int bits, i, len;
310274462Salfred
310375801Siedowse	if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
310475801Siedowse		return (-1);
3105103949Smike	if (bitlen > len * CHAR_BIT)
310675801Siedowse		return (-1);
310774462Salfred
310875801Siedowse	for (i = 0; i < len; i++) {
3109103949Smike		bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
3110219125Sru		*p++ = (u_char)~0 << (CHAR_BIT - bits);
311175801Siedowse		bitlen -= bits;
311274462Salfred	}
311375801Siedowse	return 0;
311474462Salfred}
311574462Salfred
311675801Siedowse/*
311775801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask
311875801Siedowse * is acceptable (i.e. of the form 1...10....0).
311975801Siedowse */
312075801Siedowseint
312175801Siedowsecheckmask(struct sockaddr *sa)
312274462Salfred{
312375801Siedowse	u_char *mask;
312475801Siedowse	int i, len;
312574462Salfred
312675801Siedowse	if ((mask = sa_rawaddr(sa, &len)) == NULL)
312775801Siedowse		return (-1);
312875801Siedowse
312975801Siedowse	for (i = 0; i < len; i++)
313075801Siedowse		if (mask[i] != 0xff)
313175801Siedowse			break;
313275801Siedowse	if (i < len) {
313375801Siedowse		if (~mask[i] & (u_char)(~mask[i] + 1))
313475801Siedowse			return (-1);
313575801Siedowse		i++;
313674462Salfred	}
313775801Siedowse	for (; i < len; i++)
313875801Siedowse		if (mask[i] != 0)
313975801Siedowse			return (-1);
314075801Siedowse	return (0);
314174462Salfred}
314274462Salfred
314375801Siedowse/*
314475801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if
314575801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3146228990Suqs * If samask is NULL, perform a full comparison.
314775801Siedowse */
314875801Siedowseint
314975801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
315074462Salfred{
315175801Siedowse	unsigned char *p1, *p2, *mask;
315275801Siedowse	int len, i;
315374462Salfred
315475801Siedowse	if (sa1->sa_family != sa2->sa_family ||
315575801Siedowse	    (p1 = sa_rawaddr(sa1, &len)) == NULL ||
315675801Siedowse	    (p2 = sa_rawaddr(sa2, NULL)) == NULL)
315775801Siedowse		return (1);
315875801Siedowse
315975801Siedowse	switch (sa1->sa_family) {
316074462Salfred	case AF_INET6:
316175801Siedowse		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
316275801Siedowse		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
316375801Siedowse			return (1);
316474462Salfred		break;
316574462Salfred	}
316674462Salfred
316775801Siedowse	/* Simple binary comparison if no mask specified. */
316875801Siedowse	if (samask == NULL)
316975801Siedowse		return (memcmp(p1, p2, len));
317074462Salfred
317175801Siedowse	/* Set up the mask, and do a mask-based comparison. */
317275801Siedowse	if (sa1->sa_family != samask->sa_family ||
317375801Siedowse	    (mask = sa_rawaddr(samask, NULL)) == NULL)
317475801Siedowse		return (1);
317574462Salfred
317675801Siedowse	for (i = 0; i < len; i++)
317775801Siedowse		if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
317875801Siedowse			return (1);
317975801Siedowse	return (0);
318074462Salfred}
318174462Salfred
318275801Siedowse/*
318375801Siedowse * Return a pointer to the part of the sockaddr that contains the
318475801Siedowse * raw address, and set *nbytes to its length in bytes. Returns
318575801Siedowse * NULL if the address family is unknown.
318675801Siedowse */
318775801Siedowsevoid *
318875801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) {
318975801Siedowse	void *p;
319074462Salfred	int len;
319174462Salfred
319275801Siedowse	switch (sa->sa_family) {
319374462Salfred	case AF_INET:
319475801Siedowse		len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
319575801Siedowse		p = &((struct sockaddr_in *)sa)->sin_addr;
319674462Salfred		break;
319774462Salfred	case AF_INET6:
319875801Siedowse		len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
319975801Siedowse		p = &((struct sockaddr_in6 *)sa)->sin6_addr;
320074462Salfred		break;
320174462Salfred	default:
320275801Siedowse		p = NULL;
320375801Siedowse		len = 0;
320474462Salfred	}
320574462Salfred
320675801Siedowse	if (nbytes != NULL)
320775801Siedowse		*nbytes = len;
320875801Siedowse	return (p);
320974462Salfred}
321074462Salfred
321175754Siedowsevoid
3212216587Scharnierhuphandler(int sig __unused)
321375754Siedowse{
321475754Siedowse	got_sighup = 1;
321575754Siedowse}
321675754Siedowse
3217216587Scharniervoid terminate(int sig __unused)
321874462Salfred{
3219149433Spjd	pidfile_remove(pfh);
3220194880Sdfr	rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3221194880Sdfr	rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
322274462Salfred	exit (0);
322374462Salfred}
3224192934Srmacklem
3225