mountd.c revision 121556
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 * 3. All advertising materials mentioning features or use of this software
171558Srgrimes *    must display the following acknowledgement:
181558Srgrimes *	This product includes software developed by the University of
191558Srgrimes *	California, Berkeley and its contributors.
201558Srgrimes * 4. Neither the name of the University nor the names of its contributors
211558Srgrimes *    may be used to endorse or promote products derived from this software
221558Srgrimes *    without specific prior written permission.
231558Srgrimes *
241558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341558Srgrimes * SUCH DAMAGE.
351558Srgrimes */
361558Srgrimes
371558Srgrimes#ifndef lint
3837663Scharnierstatic const char copyright[] =
391558Srgrimes"@(#) Copyright (c) 1989, 1993\n\
401558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
412999Swollman#endif /*not lint*/
421558Srgrimes
43105267Scharnier#if 0
441558Srgrimes#ifndef lint
4537663Scharnierstatic char sccsid[] = "@(#)mountd.c	8.15 (Berkeley) 5/1/95";
46105267Scharnier#endif /*not lint*/
4737663Scharnier#endif
481558Srgrimes
49105267Scharnier#include <sys/cdefs.h>
50105267Scharnier__FBSDID("$FreeBSD: head/usr.sbin/mountd/mountd.c 121556 2003-10-26 05:58:21Z peter $");
51105267Scharnier
521558Srgrimes#include <sys/param.h>
531558Srgrimes#include <sys/mount.h>
5474462Salfred#include <sys/fcntl.h>
551558Srgrimes#include <sys/stat.h>
561558Srgrimes#include <sys/syslog.h>
5724330Sguido#include <sys/sysctl.h>
5896622Siedowse#include <sys/linker.h>
5996622Siedowse#include <sys/module.h>
601558Srgrimes
611558Srgrimes#include <rpc/rpc.h>
62109363Smbr#include <rpc/rpc_com.h>
631558Srgrimes#include <rpc/pmap_clnt.h>
6474462Salfred#include <rpc/pmap_prot.h>
6574462Salfred#include <rpcsvc/mount.h>
661558Srgrimes#include <nfs/rpcv2.h>
679336Sdfr#include <nfs/nfsproto.h>
6883653Speter#include <nfsserver/nfs.h>
6923681Speter#include <ufs/ufs/ufsmount.h>
7077162Sru#include <fs/msdosfs/msdosfsmount.h>
7177223Sru#include <fs/ntfs/ntfsmount.h>
7223681Speter#include <isofs/cd9660/cd9660_mount.h>	/* XXX need isofs in include */
731558Srgrimes
741558Srgrimes#include <arpa/inet.h>
751558Srgrimes
761558Srgrimes#include <ctype.h>
7737663Scharnier#include <err.h>
781558Srgrimes#include <errno.h>
791558Srgrimes#include <grp.h>
80103949Smike#include <limits.h>
811558Srgrimes#include <netdb.h>
821558Srgrimes#include <pwd.h>
831558Srgrimes#include <signal.h>
841558Srgrimes#include <stdio.h>
851558Srgrimes#include <stdlib.h>
861558Srgrimes#include <string.h>
871558Srgrimes#include <unistd.h>
881558Srgrimes#include "pathnames.h"
891558Srgrimes
901558Srgrimes#ifdef DEBUG
911558Srgrimes#include <stdarg.h>
921558Srgrimes#endif
931558Srgrimes
9474462Salfred#ifndef MOUNTDLOCK
9574462Salfred#define MOUNTDLOCK "/var/run/mountd.lock"
9674462Salfred#endif
9774462Salfred
981558Srgrimes/*
991558Srgrimes * Structures for keeping the mount list and export list
1001558Srgrimes */
1011558Srgrimesstruct mountlist {
1021558Srgrimes	struct mountlist *ml_next;
1031558Srgrimes	char	ml_host[RPCMNT_NAMELEN+1];
1041558Srgrimes	char	ml_dirp[RPCMNT_PATHLEN+1];
1051558Srgrimes};
1061558Srgrimes
1071558Srgrimesstruct dirlist {
1081558Srgrimes	struct dirlist	*dp_left;
1091558Srgrimes	struct dirlist	*dp_right;
1101558Srgrimes	int		dp_flag;
1111558Srgrimes	struct hostlist	*dp_hosts;	/* List of hosts this dir exported to */
1121558Srgrimes	char		dp_dirp[1];	/* Actually malloc'd to size of dir */
1131558Srgrimes};
1141558Srgrimes/* dp_flag bits */
1151558Srgrimes#define	DP_DEFSET	0x1
1169336Sdfr#define DP_HOSTSET	0x2
1171558Srgrimes
1181558Srgrimesstruct exportlist {
1191558Srgrimes	struct exportlist *ex_next;
1201558Srgrimes	struct dirlist	*ex_dirl;
1211558Srgrimes	struct dirlist	*ex_defdir;
1221558Srgrimes	int		ex_flag;
1231558Srgrimes	fsid_t		ex_fs;
1241558Srgrimes	char		*ex_fsdir;
12527447Sdfr	char		*ex_indexfile;
1261558Srgrimes};
1271558Srgrimes/* ex_flag bits */
1281558Srgrimes#define	EX_LINKED	0x1
1291558Srgrimes
1301558Srgrimesstruct netmsk {
13174462Salfred	struct sockaddr_storage nt_net;
13275801Siedowse	struct sockaddr_storage nt_mask;
13342144Sdfr	char		*nt_name;
1341558Srgrimes};
1351558Srgrimes
1361558Srgrimesunion grouptypes {
13774462Salfred	struct addrinfo *gt_addrinfo;
1381558Srgrimes	struct netmsk	gt_net;
1391558Srgrimes};
1401558Srgrimes
1411558Srgrimesstruct grouplist {
1421558Srgrimes	int gr_type;
1431558Srgrimes	union grouptypes gr_ptr;
1441558Srgrimes	struct grouplist *gr_next;
1451558Srgrimes};
1461558Srgrimes/* Group types */
1471558Srgrimes#define	GT_NULL		0x0
1481558Srgrimes#define	GT_HOST		0x1
1491558Srgrimes#define	GT_NET		0x2
15075641Siedowse#define	GT_DEFAULT	0x3
1517401Swpaul#define GT_IGNORE	0x5
1521558Srgrimes
1531558Srgrimesstruct hostlist {
1549336Sdfr	int		 ht_flag;	/* Uses DP_xx bits */
1551558Srgrimes	struct grouplist *ht_grp;
1561558Srgrimes	struct hostlist	 *ht_next;
1571558Srgrimes};
1581558Srgrimes
1599336Sdfrstruct fhreturn {
1609336Sdfr	int	fhr_flag;
1619336Sdfr	int	fhr_vers;
1629336Sdfr	nfsfh_t	fhr_fh;
1639336Sdfr};
1649336Sdfr
1651558Srgrimes/* Global defs */
16692882Simpchar	*add_expdir(struct dirlist **, char *, int);
16792882Simpvoid	add_dlist(struct dirlist **, struct dirlist *,
16892882Simp				struct grouplist *, int);
16992882Simpvoid	add_mlist(char *, char *);
17092882Simpint	check_dirpath(char *);
17192882Simpint	check_options(struct dirlist *);
17275801Siedowseint	checkmask(struct sockaddr *sa);
17392882Simpint	chk_host(struct dirlist *, struct sockaddr *, int *, int *);
17475635Siedowsevoid	del_mlist(char *hostp, char *dirp);
17592882Simpstruct dirlist *dirp_search(struct dirlist *, char *);
17692882Simpint	do_mount(struct exportlist *, struct grouplist *, int,
17792882Simp		struct xucred *, char *, int, struct statfs *);
17892882Simpint	do_opt(char **, char **, struct exportlist *, struct grouplist *,
17992882Simp				int *, int *, struct xucred *);
18092882Simpstruct	exportlist *ex_search(fsid_t *);
18192882Simpstruct	exportlist *get_exp(void);
18292882Simpvoid	free_dir(struct dirlist *);
18392882Simpvoid	free_exp(struct exportlist *);
18492882Simpvoid	free_grp(struct grouplist *);
18592882Simpvoid	free_host(struct hostlist *);
18692882Simpvoid	get_exportlist(void);
18792882Simpint	get_host(char *, struct grouplist *, struct grouplist *);
18892882Simpstruct hostlist *get_ht(void);
18992882Simpint	get_line(void);
19092882Simpvoid	get_mountlist(void);
19192882Simpint	get_net(char *, struct netmsk *, int);
19292882Simpvoid	getexp_err(struct exportlist *, struct grouplist *);
19392882Simpstruct grouplist *get_grp(void);
19492882Simpvoid	hang_dirp(struct dirlist *, struct grouplist *,
19592882Simp				struct exportlist *, int);
19675754Siedowsevoid	huphandler(int sig);
19775801Siedowseint	makemask(struct sockaddr_storage *ssp, int bitlen);
19892882Simpvoid	mntsrv(struct svc_req *, SVCXPRT *);
19992882Simpvoid	nextfield(char **, char **);
20092882Simpvoid	out_of_mem(void);
20192882Simpvoid	parsecred(char *, struct xucred *);
202100117Salfredint	put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
20375801Siedowsevoid	*sa_rawaddr(struct sockaddr *sa, int *nbytes);
20475801Siedowseint	sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
20575801Siedowse    struct sockaddr *samask);
20692882Simpint	scan_tree(struct dirlist *, struct sockaddr *);
20792882Simpstatic void usage(void);
20892882Simpint	xdr_dir(XDR *, char *);
20992882Simpint	xdr_explist(XDR *, caddr_t);
210100117Salfredint	xdr_explist_brief(XDR *, caddr_t);
21192882Simpint	xdr_fhs(XDR *, caddr_t);
21292882Simpint	xdr_mlist(XDR *, caddr_t);
21392882Simpvoid	terminate(int);
2141558Srgrimes
2151558Srgrimesstruct exportlist *exphead;
2161558Srgrimesstruct mountlist *mlhead;
2171558Srgrimesstruct grouplist *grphead;
2181558Srgrimeschar exname[MAXPATHLEN];
21972650Sgreenstruct xucred def_anon = {
22091354Sdd	XUCRED_VERSION,
22172650Sgreen	(uid_t)-2,
2221558Srgrimes	1,
22372650Sgreen	{ (gid_t)-2 },
22472650Sgreen	NULL
2251558Srgrimes};
22625087Sdfrint force_v2 = 0;
2279336Sdfrint resvport_only = 1;
2289336Sdfrint dir_only = 1;
22931705Sguidoint log = 0;
23075754Siedowseint got_sighup = 0;
23174462Salfred
2321558Srgrimesint opt_flags;
23374462Salfredstatic int have_v6 = 1;
23474462Salfred#ifdef NI_WITHSCOPEID
23574462Salfredstatic const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID;
23674462Salfred#else
23774462Salfredstatic const int ninumeric = NI_NUMERICHOST;
23874462Salfred#endif
23974462Salfred
24074462Salfredint mountdlockfd;
24175801Siedowse/* Bits for opt_flags above */
2421558Srgrimes#define	OP_MAPROOT	0x01
2431558Srgrimes#define	OP_MAPALL	0x02
24483653Speter/* 0x4 free */
2451558Srgrimes#define	OP_MASK		0x08
2461558Srgrimes#define	OP_NET		0x10
2471558Srgrimes#define	OP_ALLDIRS	0x40
24875801Siedowse#define	OP_HAVEMASK	0x80	/* A mask was specified or inferred. */
249100336Sjoerg#define	OP_QUIET	0x100
25074462Salfred#define OP_MASKLEN	0x200
2511558Srgrimes
2521558Srgrimes#ifdef DEBUG
2531558Srgrimesint debug = 1;
25492882Simpvoid	SYSLOG(int, const char *, ...) __printflike(2, 3);
2551558Srgrimes#define syslog SYSLOG
2561558Srgrimes#else
2571558Srgrimesint debug = 0;
2581558Srgrimes#endif
2591558Srgrimes
2601558Srgrimes/*
2611558Srgrimes * Mountd server for NFS mount protocol as described in:
2621558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A
2631558Srgrimes * The optional arguments are the exports file name
2641558Srgrimes * default: _PATH_EXPORTS
2651558Srgrimes * and "-n" to allow nonroot mount.
2661558Srgrimes */
2671558Srgrimesint
2681558Srgrimesmain(argc, argv)
2691558Srgrimes	int argc;
2701558Srgrimes	char **argv;
2711558Srgrimes{
27275754Siedowse	fd_set readfds;
27374462Salfred	SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
27474462Salfred	struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
27574462Salfred	int udpsock, tcpsock, udp6sock, tcp6sock;
27674462Salfred	int xcreated = 0, s;
277109363Smbr	int maxrec = RPC_MAXDATASIZE;
27874462Salfred	int one = 1;
27996622Siedowse	int c;
2801558Srgrimes
28175635Siedowse	udp6conf = tcp6conf = NULL;
28275635Siedowse	udp6sock = tcp6sock = NULL;
28375635Siedowse
28474462Salfred	/* Check that another mountd isn't already running. */
28574462Salfred	if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
28674462Salfred		err(1, "%s", MOUNTDLOCK);
28774462Salfred
28874462Salfred	if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
289105267Scharnier		errx(1, "another mountd is already running. Aborting");
29074462Salfred	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
29174462Salfred	if (s < 0)
29274462Salfred		have_v6 = 0;
29374462Salfred	else
29474462Salfred		close(s);
29583687Speter	if (modfind("nfsserver") < 0) {
29683687Speter		/* Not present in kernel, try loading it */
29783687Speter		if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
29883687Speter			errx(1, "NFS server is not available or loadable");
2992999Swollman	}
3002999Swollman
30131665Sguido	while ((c = getopt(argc, argv, "2dlnr")) != -1)
3021558Srgrimes		switch (c) {
30325087Sdfr		case '2':
30425087Sdfr			force_v2 = 1;
30525087Sdfr			break;
3069336Sdfr		case 'n':
3079336Sdfr			resvport_only = 0;
3089336Sdfr			break;
3099336Sdfr		case 'r':
3109336Sdfr			dir_only = 0;
3119336Sdfr			break;
3128688Sphk		case 'd':
3138688Sphk			debug = debug ? 0 : 1;
3148688Sphk			break;
31531656Sguido		case 'l':
31631656Sguido			log = 1;
31731656Sguido			break;
3181558Srgrimes		default:
31937663Scharnier			usage();
3201558Srgrimes		};
3211558Srgrimes	argc -= optind;
3221558Srgrimes	argv += optind;
3231558Srgrimes	grphead = (struct grouplist *)NULL;
3241558Srgrimes	exphead = (struct exportlist *)NULL;
3251558Srgrimes	mlhead = (struct mountlist *)NULL;
3261558Srgrimes	if (argc == 1) {
3271558Srgrimes		strncpy(exname, *argv, MAXPATHLEN-1);
3281558Srgrimes		exname[MAXPATHLEN-1] = '\0';
3291558Srgrimes	} else
3301558Srgrimes		strcpy(exname, _PATH_EXPORTS);
3311558Srgrimes	openlog("mountd", LOG_PID, LOG_DAEMON);
3321558Srgrimes	if (debug)
33337663Scharnier		warnx("getting export list");
3341558Srgrimes	get_exportlist();
3351558Srgrimes	if (debug)
33637663Scharnier		warnx("getting mount list");
3371558Srgrimes	get_mountlist();
3381558Srgrimes	if (debug)
33937663Scharnier		warnx("here we go");
3401558Srgrimes	if (debug == 0) {
3411558Srgrimes		daemon(0, 0);
3421558Srgrimes		signal(SIGINT, SIG_IGN);
3431558Srgrimes		signal(SIGQUIT, SIG_IGN);
3441558Srgrimes	}
34575754Siedowse	signal(SIGHUP, huphandler);
34674462Salfred	signal(SIGTERM, terminate);
3471558Srgrimes	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
3481558Srgrimes	  if (pidfile != NULL) {
3491558Srgrimes		fprintf(pidfile, "%d\n", getpid());
3501558Srgrimes		fclose(pidfile);
3511558Srgrimes	  }
3521558Srgrimes	}
35374462Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
35474462Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
35574462Salfred	udpsock  = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
35674462Salfred	tcpsock  = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
35774791Salfred	udpconf  = getnetconfigent("udp");
35874791Salfred	tcpconf  = getnetconfigent("tcp");
359109363Smbr
360109363Smbr	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
361109363Smbr
36274791Salfred	if (!have_v6)
36374791Salfred		goto skip_v6;
36474462Salfred	udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
36574462Salfred	tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
36674462Salfred	/*
36774462Salfred	 * We're doing host-based access checks here, so don't allow
36874462Salfred	 * v4-in-v6 to confuse things. The kernel will disable it
36974462Salfred	 * by default on NFS sockets too.
37074462Salfred	 */
37174462Salfred	if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
372117684Srwatson		IPV6_V6ONLY, &one, sizeof one) < 0) {
37374462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
37474462Salfred		exit(1);
37574462Salfred	}
37674462Salfred	if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
377117684Srwatson		IPV6_V6ONLY, &one, sizeof one) < 0) {
378117684Srwatson		syslog(LOG_ERR, "can't disable v4-in-v6 on TCP socket");
37974462Salfred		exit(1);
38074462Salfred	}
38174462Salfred	udp6conf = getnetconfigent("udp6");
38274462Salfred	tcp6conf = getnetconfigent("tcp6");
38374791Salfred
38474791Salfredskip_v6:
38524759Sguido	if (!resvport_only) {
38683687Speter		if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
38783687Speter		    &resvport_only, sizeof(resvport_only)) != 0 &&
38883687Speter		    errno != ENOENT) {
38924759Sguido			syslog(LOG_ERR, "sysctl: %m");
39024759Sguido			exit(1);
39124759Sguido		}
39224330Sguido	}
39374462Salfred	if (udpsock != -1 && udpconf != NULL) {
39474462Salfred		bindresvport(udpsock, NULL);
39574462Salfred		udptransp = svc_dg_create(udpsock, 0, 0);
39674462Salfred		if (udptransp != NULL) {
39774462Salfred			if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
39874462Salfred			    mntsrv, udpconf))
39974462Salfred				syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service");
40074462Salfred			else
40174462Salfred				xcreated++;
40274462Salfred			if (!force_v2) {
40374462Salfred				if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
40474462Salfred				    mntsrv, udpconf))
40574462Salfred					syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service");
40674462Salfred				else
40774462Salfred					xcreated++;
40874462Salfred			}
40974462Salfred		} else
41074462Salfred			syslog(LOG_WARNING, "can't create UDP services");
41174462Salfred
41274462Salfred	}
41374462Salfred	if (tcpsock != -1 && tcpconf != NULL) {
41474462Salfred		bindresvport(tcpsock, NULL);
41574462Salfred		listen(tcpsock, SOMAXCONN);
416109363Smbr		tcptransp = svc_vc_create(tcpsock, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
41774462Salfred		if (tcptransp != NULL) {
41874462Salfred			if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
41974462Salfred			    mntsrv, tcpconf))
42074462Salfred				syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service");
42174462Salfred			else
42274462Salfred				xcreated++;
42374462Salfred			if (!force_v2) {
42474462Salfred				if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
42574462Salfred				    mntsrv, tcpconf))
42674462Salfred					syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service");
42774462Salfred				else
42874462Salfred					xcreated++;
42974462Salfred			}
43074462Salfred		} else
43174462Salfred			syslog(LOG_WARNING, "can't create TCP service");
43274462Salfred
43374462Salfred	}
43474791Salfred	if (have_v6 && udp6sock != -1 && udp6conf != NULL) {
43574462Salfred		bindresvport(udp6sock, NULL);
43674462Salfred		udp6transp = svc_dg_create(udp6sock, 0, 0);
43774462Salfred		if (udp6transp != NULL) {
43874462Salfred			if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
43974462Salfred			    mntsrv, udp6conf))
44074462Salfred				syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service");
44174462Salfred			else
44274462Salfred				xcreated++;
44374462Salfred			if (!force_v2) {
44474462Salfred				if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
44574462Salfred				    mntsrv, udp6conf))
44674462Salfred					syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service");
44774462Salfred				else
44874462Salfred					xcreated++;
44974462Salfred			}
45074462Salfred		} else
45174462Salfred			syslog(LOG_WARNING, "can't create UDP6 service");
45274462Salfred
45374462Salfred	}
45474791Salfred	if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) {
45574462Salfred		bindresvport(tcp6sock, NULL);
45674462Salfred		listen(tcp6sock, SOMAXCONN);
457109363Smbr		tcp6transp = svc_vc_create(tcp6sock, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
45874462Salfred		if (tcp6transp != NULL) {
45974462Salfred			if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
46074462Salfred			    mntsrv, tcp6conf))
46174462Salfred				syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service");
46274462Salfred			else
46374462Salfred				xcreated++;
46474462Salfred			if (!force_v2) {
46574462Salfred				if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
46674462Salfred				    mntsrv, tcp6conf))
46774462Salfred					syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service");
46874462Salfred					else
46974462Salfred						xcreated++;
47074462Salfred				}
47174462Salfred		} else
47274462Salfred			syslog(LOG_WARNING, "can't create TCP6 service");
47374462Salfred
47474462Salfred	}
47574462Salfred	if (xcreated == 0) {
47674462Salfred		syslog(LOG_ERR, "could not create any services");
4771558Srgrimes		exit(1);
4781558Srgrimes	}
47975754Siedowse
48075754Siedowse	/* Expand svc_run() here so that we can call get_exportlist(). */
48175754Siedowse	for (;;) {
48275754Siedowse		if (got_sighup) {
48375754Siedowse			get_exportlist();
48475754Siedowse			got_sighup = 0;
48575754Siedowse		}
48675754Siedowse		readfds = svc_fdset;
48775754Siedowse		switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
48875754Siedowse		case -1:
48975754Siedowse			if (errno == EINTR)
49075754Siedowse                                continue;
49175754Siedowse			syslog(LOG_ERR, "mountd died: select: %m");
49275754Siedowse			exit(1);
49375754Siedowse		case 0:
49475754Siedowse			continue;
49575754Siedowse		default:
49675754Siedowse			svc_getreqset(&readfds);
49775754Siedowse		}
49875754Siedowse	}
4991558Srgrimes}
5001558Srgrimes
50137663Scharnierstatic void
50237663Scharnierusage()
50337663Scharnier{
50437663Scharnier	fprintf(stderr,
50537663Scharnier		"usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
50637663Scharnier	exit(1);
50737663Scharnier}
50837663Scharnier
5091558Srgrimes/*
5101558Srgrimes * The mount rpc service
5111558Srgrimes */
5121558Srgrimesvoid
5131558Srgrimesmntsrv(rqstp, transp)
5141558Srgrimes	struct svc_req *rqstp;
5151558Srgrimes	SVCXPRT *transp;
5161558Srgrimes{
5171558Srgrimes	struct exportlist *ep;
5181558Srgrimes	struct dirlist *dp;
5199336Sdfr	struct fhreturn fhr;
5201558Srgrimes	struct stat stb;
5211558Srgrimes	struct statfs fsb;
52274462Salfred	struct addrinfo *ai;
52374462Salfred	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
52474462Salfred	int lookup_failed = 1;
52574462Salfred	struct sockaddr *saddr;
5269336Sdfr	u_short sport;
52723681Speter	char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
52828911Sguido	int bad = 0, defset, hostset;
5299336Sdfr	sigset_t sighup_mask;
5301558Srgrimes
5319336Sdfr	sigemptyset(&sighup_mask);
5329336Sdfr	sigaddset(&sighup_mask, SIGHUP);
53374462Salfred	saddr = svc_getrpccaller(transp)->buf;
53474462Salfred	switch (saddr->sa_family) {
53574462Salfred	case AF_INET6:
53675635Siedowse		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
53774462Salfred		break;
53874462Salfred	case AF_INET:
53975635Siedowse		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
54074462Salfred		break;
54174462Salfred	default:
54274462Salfred		syslog(LOG_ERR, "request from unknown address family");
54374462Salfred		return;
54474462Salfred	}
54574462Salfred	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
54674462Salfred	    NULL, 0, 0);
54774462Salfred	getnameinfo(saddr, saddr->sa_len, numerichost,
54874462Salfred	    sizeof numerichost, NULL, 0, NI_NUMERICHOST);
54974462Salfred	ai = NULL;
5501558Srgrimes	switch (rqstp->rq_proc) {
5511558Srgrimes	case NULLPROC:
552121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
55337663Scharnier			syslog(LOG_ERR, "can't send reply");
5541558Srgrimes		return;
5551558Srgrimes	case RPCMNT_MOUNT:
5569336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
55731656Sguido			syslog(LOG_NOTICE,
55831656Sguido			    "mount request from %s from unprivileged port",
55974462Salfred			    numerichost);
5601558Srgrimes			svcerr_weakauth(transp);
5611558Srgrimes			return;
5621558Srgrimes		}
563121556Speter		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
56431656Sguido			syslog(LOG_NOTICE, "undecodable mount request from %s",
56574462Salfred			    numerichost);
5661558Srgrimes			svcerr_decode(transp);
5671558Srgrimes			return;
5681558Srgrimes		}
5691558Srgrimes
5701558Srgrimes		/*
5711558Srgrimes		 * Get the real pathname and make sure it is a directory
5729336Sdfr		 * or a regular file if the -r option was specified
5739336Sdfr		 * and it exists.
5741558Srgrimes		 */
57551968Salfred		if (realpath(rpcpath, dirpath) == NULL ||
5761558Srgrimes		    stat(dirpath, &stb) < 0 ||
5779336Sdfr		    (!S_ISDIR(stb.st_mode) &&
57874462Salfred		    (dir_only || !S_ISREG(stb.st_mode))) ||
5791558Srgrimes		    statfs(dirpath, &fsb) < 0) {
5801558Srgrimes			chdir("/");	/* Just in case realpath doesn't */
58131656Sguido			syslog(LOG_NOTICE,
58237663Scharnier			    "mount request from %s for non existent path %s",
58374462Salfred			    numerichost, dirpath);
5841558Srgrimes			if (debug)
58537663Scharnier				warnx("stat failed on %s", dirpath);
58628911Sguido			bad = ENOENT;	/* We will send error reply later */
5871558Srgrimes		}
5881558Srgrimes
5891558Srgrimes		/* Check in the exports list */
5909336Sdfr		sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
5911558Srgrimes		ep = ex_search(&fsb.f_fsid);
5929336Sdfr		hostset = defset = 0;
5939336Sdfr		if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
5941558Srgrimes		    ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
59574462Salfred		      chk_host(dp, saddr, &defset, &hostset)) ||
59674462Salfred		    (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
59774462Salfred		     scan_tree(ep->ex_dirl, saddr) == 0))) {
59828911Sguido			if (bad) {
599121556Speter				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
60028911Sguido				    (caddr_t)&bad))
60137663Scharnier					syslog(LOG_ERR, "can't send reply");
60228911Sguido				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
60328911Sguido				return;
60428911Sguido			}
6059336Sdfr			if (hostset & DP_HOSTSET)
6069336Sdfr				fhr.fhr_flag = hostset;
6079336Sdfr			else
6089336Sdfr				fhr.fhr_flag = defset;
6099336Sdfr			fhr.fhr_vers = rqstp->rq_vers;
6101558Srgrimes			/* Get the file handle */
61123681Speter			memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
6129336Sdfr			if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
6131558Srgrimes				bad = errno;
61437663Scharnier				syslog(LOG_ERR, "can't get fh for %s", dirpath);
615121556Speter				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
6161558Srgrimes				    (caddr_t)&bad))
61737663Scharnier					syslog(LOG_ERR, "can't send reply");
6189336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6191558Srgrimes				return;
6201558Srgrimes			}
621121556Speter			if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
622121556Speter			    (caddr_t)&fhr))
62337663Scharnier				syslog(LOG_ERR, "can't send reply");
62474462Salfred			if (!lookup_failed)
62574462Salfred				add_mlist(host, dirpath);
6261558Srgrimes			else
62774462Salfred				add_mlist(numerichost, dirpath);
6281558Srgrimes			if (debug)
62937663Scharnier				warnx("mount successful");
63031656Sguido			if (log)
63131656Sguido				syslog(LOG_NOTICE,
63231656Sguido				    "mount request succeeded from %s for %s",
63374462Salfred				    numerichost, dirpath);
63431656Sguido		} else {
6351558Srgrimes			bad = EACCES;
63631656Sguido			syslog(LOG_NOTICE,
63731656Sguido			    "mount request denied from %s for %s",
63874462Salfred			    numerichost, dirpath);
63931656Sguido		}
64028911Sguido
641121556Speter		if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
642121556Speter		    (caddr_t)&bad))
64337663Scharnier			syslog(LOG_ERR, "can't send reply");
6449336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6451558Srgrimes		return;
6461558Srgrimes	case RPCMNT_DUMP:
647121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
64837663Scharnier			syslog(LOG_ERR, "can't send reply");
64931656Sguido		else if (log)
65031656Sguido			syslog(LOG_NOTICE,
65131656Sguido			    "dump request succeeded from %s",
65274462Salfred			    numerichost);
6531558Srgrimes		return;
6541558Srgrimes	case RPCMNT_UMOUNT:
6559336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
65631656Sguido			syslog(LOG_NOTICE,
65731656Sguido			    "umount request from %s from unprivileged port",
65874462Salfred			    numerichost);
6591558Srgrimes			svcerr_weakauth(transp);
6601558Srgrimes			return;
6611558Srgrimes		}
662121556Speter		if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
66331656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
66474462Salfred			    numerichost);
6651558Srgrimes			svcerr_decode(transp);
6661558Srgrimes			return;
6671558Srgrimes		}
66851968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
66951968Salfred			syslog(LOG_NOTICE, "umount request from %s "
67051968Salfred			    "for non existent path %s",
67174462Salfred			    numerichost, dirpath);
67251968Salfred		}
673121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
67437663Scharnier			syslog(LOG_ERR, "can't send reply");
67574462Salfred		if (!lookup_failed)
67675635Siedowse			del_mlist(host, dirpath);
67775635Siedowse		del_mlist(numerichost, dirpath);
67831656Sguido		if (log)
67931656Sguido			syslog(LOG_NOTICE,
68031656Sguido			    "umount request succeeded from %s for %s",
68174462Salfred			    numerichost, dirpath);
6821558Srgrimes		return;
6831558Srgrimes	case RPCMNT_UMNTALL:
6849336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
68531656Sguido			syslog(LOG_NOTICE,
68631656Sguido			    "umountall request from %s from unprivileged port",
68774462Salfred			    numerichost);
6881558Srgrimes			svcerr_weakauth(transp);
6891558Srgrimes			return;
6901558Srgrimes		}
691121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
69237663Scharnier			syslog(LOG_ERR, "can't send reply");
69374462Salfred		if (!lookup_failed)
69475635Siedowse			del_mlist(host, NULL);
69575635Siedowse		del_mlist(numerichost, NULL);
69631656Sguido		if (log)
69731656Sguido			syslog(LOG_NOTICE,
69831656Sguido			    "umountall request succeeded from %s",
69974462Salfred			    numerichost);
7001558Srgrimes		return;
7011558Srgrimes	case RPCMNT_EXPORT:
702121556Speter		if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
703121556Speter			if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
704121556Speter			    (caddr_t)NULL))
705100117Salfred				syslog(LOG_ERR, "can't send reply");
70631656Sguido		if (log)
70731656Sguido			syslog(LOG_NOTICE,
70831656Sguido			    "export request succeeded from %s",
70974462Salfred			    numerichost);
7101558Srgrimes		return;
7111558Srgrimes	default:
7121558Srgrimes		svcerr_noproc(transp);
7131558Srgrimes		return;
7141558Srgrimes	}
7151558Srgrimes}
7161558Srgrimes
7171558Srgrimes/*
7181558Srgrimes * Xdr conversion for a dirpath string
7191558Srgrimes */
7201558Srgrimesint
7211558Srgrimesxdr_dir(xdrsp, dirp)
7221558Srgrimes	XDR *xdrsp;
7231558Srgrimes	char *dirp;
7241558Srgrimes{
7251558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
7261558Srgrimes}
7271558Srgrimes
7281558Srgrimes/*
7299336Sdfr * Xdr routine to generate file handle reply
7301558Srgrimes */
7311558Srgrimesint
7329336Sdfrxdr_fhs(xdrsp, cp)
7331558Srgrimes	XDR *xdrsp;
7349336Sdfr	caddr_t cp;
7351558Srgrimes{
73692806Sobrien	struct fhreturn *fhrp = (struct fhreturn *)cp;
7379336Sdfr	u_long ok = 0, len, auth;
7381558Srgrimes
7391558Srgrimes	if (!xdr_long(xdrsp, &ok))
7401558Srgrimes		return (0);
7419336Sdfr	switch (fhrp->fhr_vers) {
7429336Sdfr	case 1:
7439336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
7449336Sdfr	case 3:
7459336Sdfr		len = NFSX_V3FH;
7469336Sdfr		if (!xdr_long(xdrsp, &len))
7479336Sdfr			return (0);
7489336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
7499336Sdfr			return (0);
75083653Speter		auth = RPCAUTH_UNIX;
7519336Sdfr		len = 1;
7529336Sdfr		if (!xdr_long(xdrsp, &len))
7539336Sdfr			return (0);
7549336Sdfr		return (xdr_long(xdrsp, &auth));
7559336Sdfr	};
7569336Sdfr	return (0);
7571558Srgrimes}
7581558Srgrimes
7591558Srgrimesint
7601558Srgrimesxdr_mlist(xdrsp, cp)
7611558Srgrimes	XDR *xdrsp;
7621558Srgrimes	caddr_t cp;
7631558Srgrimes{
7641558Srgrimes	struct mountlist *mlp;
7651558Srgrimes	int true = 1;
7661558Srgrimes	int false = 0;
7671558Srgrimes	char *strp;
7681558Srgrimes
7691558Srgrimes	mlp = mlhead;
7701558Srgrimes	while (mlp) {
7711558Srgrimes		if (!xdr_bool(xdrsp, &true))
7721558Srgrimes			return (0);
7731558Srgrimes		strp = &mlp->ml_host[0];
7741558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
7751558Srgrimes			return (0);
7761558Srgrimes		strp = &mlp->ml_dirp[0];
7771558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
7781558Srgrimes			return (0);
7791558Srgrimes		mlp = mlp->ml_next;
7801558Srgrimes	}
7811558Srgrimes	if (!xdr_bool(xdrsp, &false))
7821558Srgrimes		return (0);
7831558Srgrimes	return (1);
7841558Srgrimes}
7851558Srgrimes
7861558Srgrimes/*
7871558Srgrimes * Xdr conversion for export list
7881558Srgrimes */
7891558Srgrimesint
790100117Salfredxdr_explist_common(xdrsp, cp, brief)
7911558Srgrimes	XDR *xdrsp;
7921558Srgrimes	caddr_t cp;
793100117Salfred	int brief;
7941558Srgrimes{
7951558Srgrimes	struct exportlist *ep;
7961558Srgrimes	int false = 0;
7979336Sdfr	int putdef;
7989336Sdfr	sigset_t sighup_mask;
7991558Srgrimes
8009336Sdfr	sigemptyset(&sighup_mask);
8019336Sdfr	sigaddset(&sighup_mask, SIGHUP);
8029336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
8031558Srgrimes	ep = exphead;
8041558Srgrimes	while (ep) {
8051558Srgrimes		putdef = 0;
806100117Salfred		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
807100117Salfred			       &putdef, brief))
8081558Srgrimes			goto errout;
8091558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
8101558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
811100117Salfred			&putdef, brief))
8121558Srgrimes			goto errout;
8131558Srgrimes		ep = ep->ex_next;
8141558Srgrimes	}
8159336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8161558Srgrimes	if (!xdr_bool(xdrsp, &false))
8171558Srgrimes		return (0);
8181558Srgrimes	return (1);
8191558Srgrimeserrout:
8209336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8211558Srgrimes	return (0);
8221558Srgrimes}
8231558Srgrimes
8241558Srgrimes/*
8251558Srgrimes * Called from xdr_explist() to traverse the tree and export the
8261558Srgrimes * directory paths.
8271558Srgrimes */
8281558Srgrimesint
829100117Salfredput_exlist(dp, xdrsp, adp, putdefp, brief)
8301558Srgrimes	struct dirlist *dp;
8311558Srgrimes	XDR *xdrsp;
8321558Srgrimes	struct dirlist *adp;
8331558Srgrimes	int *putdefp;
834100117Salfred	int brief;
8351558Srgrimes{
8361558Srgrimes	struct grouplist *grp;
8371558Srgrimes	struct hostlist *hp;
8381558Srgrimes	int true = 1;
8391558Srgrimes	int false = 0;
8401558Srgrimes	int gotalldir = 0;
8411558Srgrimes	char *strp;
8421558Srgrimes
8431558Srgrimes	if (dp) {
844100117Salfred		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
8451558Srgrimes			return (1);
8461558Srgrimes		if (!xdr_bool(xdrsp, &true))
8471558Srgrimes			return (1);
8481558Srgrimes		strp = dp->dp_dirp;
8491558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
8501558Srgrimes			return (1);
8511558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
8521558Srgrimes			gotalldir = 1;
8531558Srgrimes			*putdefp = 1;
8541558Srgrimes		}
855100117Salfred		if (brief) {
856100117Salfred			if (!xdr_bool(xdrsp, &true))
857100117Salfred				return (1);
858100117Salfred			strp = "(...)";
859100117Salfred			if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
860100117Salfred				return (1);
861100117Salfred		} else if ((dp->dp_flag & DP_DEFSET) == 0 &&
8621558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
8631558Srgrimes			hp = dp->dp_hosts;
8641558Srgrimes			while (hp) {
8651558Srgrimes				grp = hp->ht_grp;
8661558Srgrimes				if (grp->gr_type == GT_HOST) {
8671558Srgrimes					if (!xdr_bool(xdrsp, &true))
8681558Srgrimes						return (1);
86974462Salfred					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
8708871Srgrimes					if (!xdr_string(xdrsp, &strp,
8711558Srgrimes					    RPCMNT_NAMELEN))
8721558Srgrimes						return (1);
8731558Srgrimes				} else if (grp->gr_type == GT_NET) {
8741558Srgrimes					if (!xdr_bool(xdrsp, &true))
8751558Srgrimes						return (1);
8761558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
8778871Srgrimes					if (!xdr_string(xdrsp, &strp,
8781558Srgrimes					    RPCMNT_NAMELEN))
8791558Srgrimes						return (1);
8801558Srgrimes				}
8811558Srgrimes				hp = hp->ht_next;
8821558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
8831558Srgrimes					hp = adp->dp_hosts;
8841558Srgrimes					gotalldir = 0;
8851558Srgrimes				}
8861558Srgrimes			}
8871558Srgrimes		}
8881558Srgrimes		if (!xdr_bool(xdrsp, &false))
8891558Srgrimes			return (1);
890100117Salfred		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
8911558Srgrimes			return (1);
8921558Srgrimes	}
8931558Srgrimes	return (0);
8941558Srgrimes}
8951558Srgrimes
896100117Salfredint
897100117Salfredxdr_explist(xdrsp, cp)
898100117Salfred	XDR *xdrsp;
899100117Salfred	caddr_t cp;
900100117Salfred{
901100117Salfred
902100117Salfred	return xdr_explist_common(xdrsp, cp, 0);
903100117Salfred}
904100117Salfred
905100117Salfredint
906100117Salfredxdr_explist_brief(xdrsp, cp)
907100117Salfred	XDR *xdrsp;
908100117Salfred	caddr_t cp;
909100117Salfred{
910100117Salfred
911100117Salfred	return xdr_explist_common(xdrsp, cp, 1);
912100117Salfred}
913100117Salfred
91496622Siedowsechar *line;
91596622Siedowseint linesize;
9161558SrgrimesFILE *exp_file;
9171558Srgrimes
9181558Srgrimes/*
9191558Srgrimes * Get the export list
9201558Srgrimes */
9211558Srgrimesvoid
9221558Srgrimesget_exportlist()
9231558Srgrimes{
9241558Srgrimes	struct exportlist *ep, *ep2;
9251558Srgrimes	struct grouplist *grp, *tgrp;
9261558Srgrimes	struct exportlist **epp;
9271558Srgrimes	struct dirlist *dirhead;
9281558Srgrimes	struct statfs fsb, *fsp;
92972650Sgreen	struct xucred anon;
9301558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
9311558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
9321558Srgrimes
93351968Salfred	dirp = NULL;
93451968Salfred	dirplen = 0;
93551968Salfred
9361558Srgrimes	/*
9371558Srgrimes	 * First, get rid of the old list
9381558Srgrimes	 */
9391558Srgrimes	ep = exphead;
9401558Srgrimes	while (ep) {
9411558Srgrimes		ep2 = ep;
9421558Srgrimes		ep = ep->ex_next;
9431558Srgrimes		free_exp(ep2);
9441558Srgrimes	}
9451558Srgrimes	exphead = (struct exportlist *)NULL;
9461558Srgrimes
9471558Srgrimes	grp = grphead;
9481558Srgrimes	while (grp) {
9491558Srgrimes		tgrp = grp;
9501558Srgrimes		grp = grp->gr_next;
9511558Srgrimes		free_grp(tgrp);
9521558Srgrimes	}
9531558Srgrimes	grphead = (struct grouplist *)NULL;
9541558Srgrimes
9551558Srgrimes	/*
9561558Srgrimes	 * And delete exports that are in the kernel for all local
95796707Strhodes	 * filesystems.
95896707Strhodes	 * XXX: Should know how to handle all local exportable filesystems
95923681Speter	 *      instead of just "ufs".
9601558Srgrimes	 */
9611558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
9621558Srgrimes	for (i = 0; i < num; i++) {
9631558Srgrimes		union {
9641558Srgrimes			struct ufs_args ua;
9651558Srgrimes			struct iso_args ia;
9669336Sdfr			struct msdosfs_args da;
96754093Ssemenu			struct ntfs_args na;
9681558Srgrimes		} targs;
9691558Srgrimes
97077435Sphk		if (!strcmp(fsp->f_fstypename, "ufs") ||
97177577Sru		    !strcmp(fsp->f_fstypename, "msdosfs") ||
97254093Ssemenu		    !strcmp(fsp->f_fstypename, "ntfs") ||
97323681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
9749336Sdfr			targs.ua.fspec = NULL;
9759336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
9769336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
97777405Siedowse			    fsp->f_flags | MNT_UPDATE, (caddr_t)&targs) < 0 &&
97877405Siedowse			    errno != ENOENT)
97977405Siedowse				syslog(LOG_ERR,
98077405Siedowse				    "can't delete exports for %s: %m",
98174462Salfred				    fsp->f_mntonname);
9821558Srgrimes		}
9831558Srgrimes		fsp++;
9841558Srgrimes	}
9851558Srgrimes
9861558Srgrimes	/*
9871558Srgrimes	 * Read in the exports file and build the list, calling
9881558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
9891558Srgrimes	 */
9901558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
99137663Scharnier		syslog(LOG_ERR, "can't open %s", exname);
9921558Srgrimes		exit(2);
9931558Srgrimes	}
9941558Srgrimes	dirhead = (struct dirlist *)NULL;
9951558Srgrimes	while (get_line()) {
9961558Srgrimes		if (debug)
99737663Scharnier			warnx("got line %s", line);
9981558Srgrimes		cp = line;
9991558Srgrimes		nextfield(&cp, &endcp);
10001558Srgrimes		if (*cp == '#')
10011558Srgrimes			goto nextline;
10021558Srgrimes
10031558Srgrimes		/*
10041558Srgrimes		 * Set defaults.
10051558Srgrimes		 */
10061558Srgrimes		has_host = FALSE;
10071558Srgrimes		anon = def_anon;
10081558Srgrimes		exflags = MNT_EXPORTED;
10091558Srgrimes		got_nondir = 0;
10101558Srgrimes		opt_flags = 0;
10111558Srgrimes		ep = (struct exportlist *)NULL;
10121558Srgrimes
10131558Srgrimes		/*
10141558Srgrimes		 * Create new exports list entry
10151558Srgrimes		 */
10161558Srgrimes		len = endcp-cp;
10171558Srgrimes		tgrp = grp = get_grp();
10181558Srgrimes		while (len > 0) {
10191558Srgrimes			if (len > RPCMNT_NAMELEN) {
10201558Srgrimes			    getexp_err(ep, tgrp);
10211558Srgrimes			    goto nextline;
10221558Srgrimes			}
10231558Srgrimes			if (*cp == '-') {
10241558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10251558Srgrimes				getexp_err(ep, tgrp);
10261558Srgrimes				goto nextline;
10271558Srgrimes			    }
10281558Srgrimes			    if (debug)
102937663Scharnier				warnx("doing opt %s", cp);
10301558Srgrimes			    got_nondir = 1;
10311558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
10321558Srgrimes				&exflags, &anon)) {
10331558Srgrimes				getexp_err(ep, tgrp);
10341558Srgrimes				goto nextline;
10351558Srgrimes			    }
10361558Srgrimes			} else if (*cp == '/') {
10371558Srgrimes			    savedc = *endcp;
10381558Srgrimes			    *endcp = '\0';
10391558Srgrimes			    if (check_dirpath(cp) &&
10401558Srgrimes				statfs(cp, &fsb) >= 0) {
10411558Srgrimes				if (got_nondir) {
104237663Scharnier				    syslog(LOG_ERR, "dirs must be first");
10431558Srgrimes				    getexp_err(ep, tgrp);
10441558Srgrimes				    goto nextline;
10451558Srgrimes				}
10461558Srgrimes				if (ep) {
10471558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
10481558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
10491558Srgrimes					getexp_err(ep, tgrp);
10501558Srgrimes					goto nextline;
10511558Srgrimes				    }
10521558Srgrimes				} else {
10531558Srgrimes				    /*
10541558Srgrimes				     * See if this directory is already
10551558Srgrimes				     * in the list.
10561558Srgrimes				     */
10571558Srgrimes				    ep = ex_search(&fsb.f_fsid);
10581558Srgrimes				    if (ep == (struct exportlist *)NULL) {
10591558Srgrimes					ep = get_exp();
10601558Srgrimes					ep->ex_fs = fsb.f_fsid;
10611558Srgrimes					ep->ex_fsdir = (char *)
10621558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
10631558Srgrimes					if (ep->ex_fsdir)
10641558Srgrimes					    strcpy(ep->ex_fsdir,
10651558Srgrimes						fsb.f_mntonname);
10661558Srgrimes					else
10671558Srgrimes					    out_of_mem();
10681558Srgrimes					if (debug)
106974462Salfred						warnx("making new ep fs=0x%x,0x%x",
107074462Salfred						    fsb.f_fsid.val[0],
107174462Salfred						    fsb.f_fsid.val[1]);
10721558Srgrimes				    } else if (debug)
107337663Scharnier					warnx("found ep fs=0x%x,0x%x",
10741558Srgrimes					    fsb.f_fsid.val[0],
10751558Srgrimes					    fsb.f_fsid.val[1]);
10761558Srgrimes				}
10771558Srgrimes
10781558Srgrimes				/*
10791558Srgrimes				 * Add dirpath to export mount point.
10801558Srgrimes				 */
10811558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
10821558Srgrimes				dirplen = len;
10831558Srgrimes			    } else {
10841558Srgrimes				getexp_err(ep, tgrp);
10851558Srgrimes				goto nextline;
10861558Srgrimes			    }
10871558Srgrimes			    *endcp = savedc;
10881558Srgrimes			} else {
10891558Srgrimes			    savedc = *endcp;
10901558Srgrimes			    *endcp = '\0';
10911558Srgrimes			    got_nondir = 1;
10921558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10931558Srgrimes				getexp_err(ep, tgrp);
10941558Srgrimes				goto nextline;
10951558Srgrimes			    }
10961558Srgrimes
10971558Srgrimes			    /*
10981558Srgrimes			     * Get the host or netgroup.
10991558Srgrimes			     */
11001558Srgrimes			    setnetgrent(cp);
11011558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
11021558Srgrimes			    do {
11031558Srgrimes				if (has_host) {
11041558Srgrimes				    grp->gr_next = get_grp();
11051558Srgrimes				    grp = grp->gr_next;
11061558Srgrimes				}
11071558Srgrimes				if (netgrp) {
110837003Sjoerg				    if (hst == 0) {
110937663Scharnier					syslog(LOG_ERR,
111037663Scharnier				"null hostname in netgroup %s, skipping", cp);
111137004Sjoerg					grp->gr_type = GT_IGNORE;
111237003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
111337663Scharnier					syslog(LOG_ERR,
111437663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
111529317Sjlemon					grp->gr_type = GT_IGNORE;
11161558Srgrimes				    }
11177401Swpaul				} else if (get_host(cp, grp, tgrp)) {
111837663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
111929317Sjlemon				    grp->gr_type = GT_IGNORE;
11201558Srgrimes				}
11211558Srgrimes				has_host = TRUE;
11221558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
11231558Srgrimes			    endnetgrent();
11241558Srgrimes			    *endcp = savedc;
11251558Srgrimes			}
11261558Srgrimes			cp = endcp;
11271558Srgrimes			nextfield(&cp, &endcp);
11281558Srgrimes			len = endcp - cp;
11291558Srgrimes		}
11301558Srgrimes		if (check_options(dirhead)) {
11311558Srgrimes			getexp_err(ep, tgrp);
11321558Srgrimes			goto nextline;
11331558Srgrimes		}
11341558Srgrimes		if (!has_host) {
113575641Siedowse			grp->gr_type = GT_DEFAULT;
11361558Srgrimes			if (debug)
113737663Scharnier				warnx("adding a default entry");
11381558Srgrimes
11391558Srgrimes		/*
11401558Srgrimes		 * Don't allow a network export coincide with a list of
11411558Srgrimes		 * host(s) on the same line.
11421558Srgrimes		 */
11431558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
114475801Siedowse			syslog(LOG_ERR, "network/host conflict");
11451558Srgrimes			getexp_err(ep, tgrp);
11461558Srgrimes			goto nextline;
114729317Sjlemon
114874462Salfred		/*
114974462Salfred		 * If an export list was specified on this line, make sure
115029317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
115129317Sjlemon		 */
115229317Sjlemon		} else {
115329317Sjlemon			grp = tgrp;
115474462Salfred			while (grp && grp->gr_type == GT_IGNORE)
115529317Sjlemon				grp = grp->gr_next;
115629317Sjlemon			if (! grp) {
115729317Sjlemon			    getexp_err(ep, tgrp);
115829317Sjlemon			    goto nextline;
115929317Sjlemon			}
11601558Srgrimes		}
11611558Srgrimes
11621558Srgrimes		/*
11631558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
11641558Srgrimes		 * After loop, tgrp points to the start of the list and
11651558Srgrimes		 * grp points to the last entry in the list.
11661558Srgrimes		 */
11671558Srgrimes		grp = tgrp;
11681558Srgrimes		do {
116975635Siedowse			if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
117075635Siedowse			    &fsb)) {
117175635Siedowse				getexp_err(ep, tgrp);
117275635Siedowse				goto nextline;
117375635Siedowse			}
11741558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
11751558Srgrimes
11761558Srgrimes		/*
11771558Srgrimes		 * Success. Update the data structures.
11781558Srgrimes		 */
11791558Srgrimes		if (has_host) {
11809336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
11811558Srgrimes			grp->gr_next = grphead;
11821558Srgrimes			grphead = tgrp;
11831558Srgrimes		} else {
11841558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
11859336Sdfr				opt_flags);
11861558Srgrimes			free_grp(grp);
11871558Srgrimes		}
11881558Srgrimes		dirhead = (struct dirlist *)NULL;
11891558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
11901558Srgrimes			ep2 = exphead;
11911558Srgrimes			epp = &exphead;
11921558Srgrimes
11931558Srgrimes			/*
11941558Srgrimes			 * Insert in the list in alphabetical order.
11951558Srgrimes			 */
11961558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
11971558Srgrimes				epp = &ep2->ex_next;
11981558Srgrimes				ep2 = ep2->ex_next;
11991558Srgrimes			}
12001558Srgrimes			if (ep2)
12011558Srgrimes				ep->ex_next = ep2;
12021558Srgrimes			*epp = ep;
12031558Srgrimes			ep->ex_flag |= EX_LINKED;
12041558Srgrimes		}
12051558Srgrimesnextline:
12061558Srgrimes		if (dirhead) {
12071558Srgrimes			free_dir(dirhead);
12081558Srgrimes			dirhead = (struct dirlist *)NULL;
12091558Srgrimes		}
12101558Srgrimes	}
12111558Srgrimes	fclose(exp_file);
12121558Srgrimes}
12131558Srgrimes
12141558Srgrimes/*
12151558Srgrimes * Allocate an export list element
12161558Srgrimes */
12171558Srgrimesstruct exportlist *
12181558Srgrimesget_exp()
12191558Srgrimes{
12201558Srgrimes	struct exportlist *ep;
12211558Srgrimes
12221558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
12231558Srgrimes	if (ep == (struct exportlist *)NULL)
12241558Srgrimes		out_of_mem();
122523681Speter	memset(ep, 0, sizeof(struct exportlist));
12261558Srgrimes	return (ep);
12271558Srgrimes}
12281558Srgrimes
12291558Srgrimes/*
12301558Srgrimes * Allocate a group list element
12311558Srgrimes */
12321558Srgrimesstruct grouplist *
12331558Srgrimesget_grp()
12341558Srgrimes{
12351558Srgrimes	struct grouplist *gp;
12361558Srgrimes
12371558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
12381558Srgrimes	if (gp == (struct grouplist *)NULL)
12391558Srgrimes		out_of_mem();
124023681Speter	memset(gp, 0, sizeof(struct grouplist));
12411558Srgrimes	return (gp);
12421558Srgrimes}
12431558Srgrimes
12441558Srgrimes/*
12451558Srgrimes * Clean up upon an error in get_exportlist().
12461558Srgrimes */
12471558Srgrimesvoid
12481558Srgrimesgetexp_err(ep, grp)
12491558Srgrimes	struct exportlist *ep;
12501558Srgrimes	struct grouplist *grp;
12511558Srgrimes{
12521558Srgrimes	struct grouplist *tgrp;
12531558Srgrimes
1254100336Sjoerg	if (!(opt_flags & OP_QUIET))
1255100336Sjoerg		syslog(LOG_ERR, "bad exports list line %s", line);
12561558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
12571558Srgrimes		free_exp(ep);
12581558Srgrimes	while (grp) {
12591558Srgrimes		tgrp = grp;
12601558Srgrimes		grp = grp->gr_next;
12611558Srgrimes		free_grp(tgrp);
12621558Srgrimes	}
12631558Srgrimes}
12641558Srgrimes
12651558Srgrimes/*
12661558Srgrimes * Search the export list for a matching fs.
12671558Srgrimes */
12681558Srgrimesstruct exportlist *
12691558Srgrimesex_search(fsid)
12701558Srgrimes	fsid_t *fsid;
12711558Srgrimes{
12721558Srgrimes	struct exportlist *ep;
12731558Srgrimes
12741558Srgrimes	ep = exphead;
12751558Srgrimes	while (ep) {
12761558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
12771558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
12781558Srgrimes			return (ep);
12791558Srgrimes		ep = ep->ex_next;
12801558Srgrimes	}
12811558Srgrimes	return (ep);
12821558Srgrimes}
12831558Srgrimes
12841558Srgrimes/*
12851558Srgrimes * Add a directory path to the list.
12861558Srgrimes */
12871558Srgrimeschar *
12881558Srgrimesadd_expdir(dpp, cp, len)
12891558Srgrimes	struct dirlist **dpp;
12901558Srgrimes	char *cp;
12911558Srgrimes	int len;
12921558Srgrimes{
12931558Srgrimes	struct dirlist *dp;
12941558Srgrimes
12951558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
129637663Scharnier	if (dp == (struct dirlist *)NULL)
129737663Scharnier		out_of_mem();
12981558Srgrimes	dp->dp_left = *dpp;
12991558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
13001558Srgrimes	dp->dp_flag = 0;
13011558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
13021558Srgrimes	strcpy(dp->dp_dirp, cp);
13031558Srgrimes	*dpp = dp;
13041558Srgrimes	return (dp->dp_dirp);
13051558Srgrimes}
13061558Srgrimes
13071558Srgrimes/*
13081558Srgrimes * Hang the dir list element off the dirpath binary tree as required
13091558Srgrimes * and update the entry for host.
13101558Srgrimes */
13111558Srgrimesvoid
13129336Sdfrhang_dirp(dp, grp, ep, flags)
13131558Srgrimes	struct dirlist *dp;
13141558Srgrimes	struct grouplist *grp;
13151558Srgrimes	struct exportlist *ep;
13169336Sdfr	int flags;
13171558Srgrimes{
13181558Srgrimes	struct hostlist *hp;
13191558Srgrimes	struct dirlist *dp2;
13201558Srgrimes
13219336Sdfr	if (flags & OP_ALLDIRS) {
13221558Srgrimes		if (ep->ex_defdir)
13231558Srgrimes			free((caddr_t)dp);
13241558Srgrimes		else
13251558Srgrimes			ep->ex_defdir = dp;
13269336Sdfr		if (grp == (struct grouplist *)NULL) {
13271558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
13289336Sdfr		} else while (grp) {
13291558Srgrimes			hp = get_ht();
13301558Srgrimes			hp->ht_grp = grp;
13311558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
13321558Srgrimes			ep->ex_defdir->dp_hosts = hp;
13331558Srgrimes			grp = grp->gr_next;
13341558Srgrimes		}
13351558Srgrimes	} else {
13361558Srgrimes
13371558Srgrimes		/*
133837663Scharnier		 * Loop through the directories adding them to the tree.
13391558Srgrimes		 */
13401558Srgrimes		while (dp) {
13411558Srgrimes			dp2 = dp->dp_left;
13429336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
13431558Srgrimes			dp = dp2;
13441558Srgrimes		}
13451558Srgrimes	}
13461558Srgrimes}
13471558Srgrimes
13481558Srgrimes/*
13491558Srgrimes * Traverse the binary tree either updating a node that is already there
13501558Srgrimes * for the new directory or adding the new node.
13511558Srgrimes */
13521558Srgrimesvoid
13539336Sdfradd_dlist(dpp, newdp, grp, flags)
13541558Srgrimes	struct dirlist **dpp;
13551558Srgrimes	struct dirlist *newdp;
13561558Srgrimes	struct grouplist *grp;
13579336Sdfr	int flags;
13581558Srgrimes{
13591558Srgrimes	struct dirlist *dp;
13601558Srgrimes	struct hostlist *hp;
13611558Srgrimes	int cmp;
13621558Srgrimes
13631558Srgrimes	dp = *dpp;
13641558Srgrimes	if (dp) {
13651558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
13661558Srgrimes		if (cmp > 0) {
13679336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
13681558Srgrimes			return;
13691558Srgrimes		} else if (cmp < 0) {
13709336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
13711558Srgrimes			return;
13721558Srgrimes		} else
13731558Srgrimes			free((caddr_t)newdp);
13741558Srgrimes	} else {
13751558Srgrimes		dp = newdp;
13761558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
13771558Srgrimes		*dpp = dp;
13781558Srgrimes	}
13791558Srgrimes	if (grp) {
13801558Srgrimes
13811558Srgrimes		/*
13821558Srgrimes		 * Hang all of the host(s) off of the directory point.
13831558Srgrimes		 */
13841558Srgrimes		do {
13851558Srgrimes			hp = get_ht();
13861558Srgrimes			hp->ht_grp = grp;
13871558Srgrimes			hp->ht_next = dp->dp_hosts;
13881558Srgrimes			dp->dp_hosts = hp;
13891558Srgrimes			grp = grp->gr_next;
13901558Srgrimes		} while (grp);
13919336Sdfr	} else {
13921558Srgrimes		dp->dp_flag |= DP_DEFSET;
13939336Sdfr	}
13941558Srgrimes}
13951558Srgrimes
13961558Srgrimes/*
13971558Srgrimes * Search for a dirpath on the export point.
13981558Srgrimes */
13991558Srgrimesstruct dirlist *
140074462Salfreddirp_search(dp, dirp)
14011558Srgrimes	struct dirlist *dp;
140274462Salfred	char *dirp;
14031558Srgrimes{
14041558Srgrimes	int cmp;
14051558Srgrimes
14061558Srgrimes	if (dp) {
140774462Salfred		cmp = strcmp(dp->dp_dirp, dirp);
14081558Srgrimes		if (cmp > 0)
140974462Salfred			return (dirp_search(dp->dp_left, dirp));
14101558Srgrimes		else if (cmp < 0)
141174462Salfred			return (dirp_search(dp->dp_right, dirp));
14121558Srgrimes		else
14131558Srgrimes			return (dp);
14141558Srgrimes	}
14151558Srgrimes	return (dp);
14161558Srgrimes}
14171558Srgrimes
14181558Srgrimes/*
14191558Srgrimes * Scan for a host match in a directory tree.
14201558Srgrimes */
14211558Srgrimesint
14229336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
14231558Srgrimes	struct dirlist *dp;
142474462Salfred	struct sockaddr *saddr;
14251558Srgrimes	int *defsetp;
14269336Sdfr	int *hostsetp;
14271558Srgrimes{
14281558Srgrimes	struct hostlist *hp;
14291558Srgrimes	struct grouplist *grp;
143074462Salfred	struct addrinfo *ai;
14311558Srgrimes
14321558Srgrimes	if (dp) {
14331558Srgrimes		if (dp->dp_flag & DP_DEFSET)
14349336Sdfr			*defsetp = dp->dp_flag;
14351558Srgrimes		hp = dp->dp_hosts;
14361558Srgrimes		while (hp) {
14371558Srgrimes			grp = hp->ht_grp;
14381558Srgrimes			switch (grp->gr_type) {
14391558Srgrimes			case GT_HOST:
144074462Salfred				ai = grp->gr_ptr.gt_addrinfo;
144174462Salfred				for (; ai; ai = ai->ai_next) {
144275801Siedowse					if (!sacmp(ai->ai_addr, saddr, NULL)) {
144374462Salfred						*hostsetp =
144474462Salfred						    (hp->ht_flag | DP_HOSTSET);
144574462Salfred						return (1);
144674462Salfred					}
14479336Sdfr				}
144875801Siedowse				break;
14491558Srgrimes			case GT_NET:
145075801Siedowse				if (!sacmp(saddr, (struct sockaddr *)
145175801Siedowse				    &grp->gr_ptr.gt_net.nt_net,
145275801Siedowse				    (struct sockaddr *)
145375801Siedowse				    &grp->gr_ptr.gt_net.nt_mask)) {
145474462Salfred					*hostsetp = (hp->ht_flag | DP_HOSTSET);
145574462Salfred					return (1);
145674462Salfred				}
145775801Siedowse				break;
145875801Siedowse			}
14591558Srgrimes			hp = hp->ht_next;
14601558Srgrimes		}
14611558Srgrimes	}
14621558Srgrimes	return (0);
14631558Srgrimes}
14641558Srgrimes
14651558Srgrimes/*
14661558Srgrimes * Scan tree for a host that matches the address.
14671558Srgrimes */
14681558Srgrimesint
14691558Srgrimesscan_tree(dp, saddr)
14701558Srgrimes	struct dirlist *dp;
147174462Salfred	struct sockaddr *saddr;
14721558Srgrimes{
14739336Sdfr	int defset, hostset;
14741558Srgrimes
14751558Srgrimes	if (dp) {
14761558Srgrimes		if (scan_tree(dp->dp_left, saddr))
14771558Srgrimes			return (1);
14789336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
14791558Srgrimes			return (1);
14801558Srgrimes		if (scan_tree(dp->dp_right, saddr))
14811558Srgrimes			return (1);
14821558Srgrimes	}
14831558Srgrimes	return (0);
14841558Srgrimes}
14851558Srgrimes
14861558Srgrimes/*
14871558Srgrimes * Traverse the dirlist tree and free it up.
14881558Srgrimes */
14891558Srgrimesvoid
14901558Srgrimesfree_dir(dp)
14911558Srgrimes	struct dirlist *dp;
14921558Srgrimes{
14931558Srgrimes
14941558Srgrimes	if (dp) {
14951558Srgrimes		free_dir(dp->dp_left);
14961558Srgrimes		free_dir(dp->dp_right);
14971558Srgrimes		free_host(dp->dp_hosts);
14981558Srgrimes		free((caddr_t)dp);
14991558Srgrimes	}
15001558Srgrimes}
15011558Srgrimes
15021558Srgrimes/*
15031558Srgrimes * Parse the option string and update fields.
15041558Srgrimes * Option arguments may either be -<option>=<value> or
15051558Srgrimes * -<option> <value>
15061558Srgrimes */
15071558Srgrimesint
15081558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
15091558Srgrimes	char **cpp, **endcpp;
15101558Srgrimes	struct exportlist *ep;
15111558Srgrimes	struct grouplist *grp;
15121558Srgrimes	int *has_hostp;
15131558Srgrimes	int *exflagsp;
151472650Sgreen	struct xucred *cr;
15151558Srgrimes{
15161558Srgrimes	char *cpoptarg, *cpoptend;
15171558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
15181558Srgrimes	int allflag, usedarg;
15191558Srgrimes
152051968Salfred	savedc2 = '\0';
15211558Srgrimes	cpopt = *cpp;
15221558Srgrimes	cpopt++;
15231558Srgrimes	cp = *endcpp;
15241558Srgrimes	savedc = *cp;
15251558Srgrimes	*cp = '\0';
15261558Srgrimes	while (cpopt && *cpopt) {
15271558Srgrimes		allflag = 1;
15281558Srgrimes		usedarg = -2;
152937663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
15301558Srgrimes			*cpoptend++ = '\0';
153137663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15321558Srgrimes				*cpoptarg++ = '\0';
15331558Srgrimes		} else {
153437663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15351558Srgrimes				*cpoptarg++ = '\0';
15361558Srgrimes			else {
15371558Srgrimes				*cp = savedc;
15381558Srgrimes				nextfield(&cp, &endcp);
15391558Srgrimes				**endcpp = '\0';
15401558Srgrimes				if (endcp > cp && *cp != '-') {
15411558Srgrimes					cpoptarg = cp;
15421558Srgrimes					savedc2 = *endcp;
15431558Srgrimes					*endcp = '\0';
15441558Srgrimes					usedarg = 0;
15451558Srgrimes				}
15461558Srgrimes			}
15471558Srgrimes		}
15481558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
15491558Srgrimes			*exflagsp |= MNT_EXRDONLY;
15501558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
15511558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
15521558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
15531558Srgrimes			usedarg++;
15541558Srgrimes			parsecred(cpoptarg, cr);
15551558Srgrimes			if (allflag == 0) {
15561558Srgrimes				*exflagsp |= MNT_EXPORTANON;
15571558Srgrimes				opt_flags |= OP_MAPALL;
15581558Srgrimes			} else
15591558Srgrimes				opt_flags |= OP_MAPROOT;
15601558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
156175801Siedowse		    !strcmp(cpopt, "m"))) {
15621558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
156337663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
15641558Srgrimes				return (1);
15651558Srgrimes			}
15661558Srgrimes			usedarg++;
15671558Srgrimes			opt_flags |= OP_MASK;
15681558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
15691558Srgrimes			!strcmp(cpopt, "n"))) {
157074462Salfred			if (strchr(cpoptarg, '/') != NULL) {
157174462Salfred				if (debug)
157274462Salfred					fprintf(stderr, "setting OP_MASKLEN\n");
157374462Salfred				opt_flags |= OP_MASKLEN;
157474462Salfred			}
15751558Srgrimes			if (grp->gr_type != GT_NULL) {
157637663Scharnier				syslog(LOG_ERR, "network/host conflict");
15771558Srgrimes				return (1);
15781558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
157937663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
15801558Srgrimes				return (1);
15811558Srgrimes			}
15821558Srgrimes			grp->gr_type = GT_NET;
15831558Srgrimes			*has_hostp = 1;
15841558Srgrimes			usedarg++;
15851558Srgrimes			opt_flags |= OP_NET;
15861558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
15871558Srgrimes			opt_flags |= OP_ALLDIRS;
158827447Sdfr		} else if (!strcmp(cpopt, "public")) {
158927447Sdfr			*exflagsp |= MNT_EXPUBLIC;
159027447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
159127447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
159227447Sdfr			opt_flags |= OP_MAPALL;
159327447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
159427447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
1595100336Sjoerg		} else if (!strcmp(cpopt, "quiet")) {
1596100336Sjoerg			opt_flags |= OP_QUIET;
15971558Srgrimes		} else {
159837663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
15991558Srgrimes			return (1);
16001558Srgrimes		}
16011558Srgrimes		if (usedarg >= 0) {
16021558Srgrimes			*endcp = savedc2;
16031558Srgrimes			**endcpp = savedc;
16041558Srgrimes			if (usedarg > 0) {
16051558Srgrimes				*cpp = cp;
16061558Srgrimes				*endcpp = endcp;
16071558Srgrimes			}
16081558Srgrimes			return (0);
16091558Srgrimes		}
16101558Srgrimes		cpopt = cpoptend;
16111558Srgrimes	}
16121558Srgrimes	**endcpp = savedc;
16131558Srgrimes	return (0);
16141558Srgrimes}
16151558Srgrimes
16161558Srgrimes/*
16171558Srgrimes * Translate a character string to the corresponding list of network
16181558Srgrimes * addresses for a hostname.
16191558Srgrimes */
16201558Srgrimesint
16217401Swpaulget_host(cp, grp, tgrp)
16221558Srgrimes	char *cp;
16231558Srgrimes	struct grouplist *grp;
16247401Swpaul	struct grouplist *tgrp;
16251558Srgrimes{
16267401Swpaul	struct grouplist *checkgrp;
162775635Siedowse	struct addrinfo *ai, *tai, hints;
162874462Salfred	int ecode;
162974462Salfred	char host[NI_MAXHOST];
16301558Srgrimes
163174462Salfred	if (grp->gr_type != GT_NULL) {
163274462Salfred		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
16331558Srgrimes		return (1);
16341558Srgrimes	}
163574462Salfred	memset(&hints, 0, sizeof hints);
163674462Salfred	hints.ai_flags = AI_CANONNAME;
163774462Salfred	hints.ai_protocol = IPPROTO_UDP;
163874462Salfred	ecode = getaddrinfo(cp, NULL, &hints, &ai);
163974462Salfred	if (ecode != 0) {
164075635Siedowse		syslog(LOG_ERR,"can't get address info for host %s", cp);
164174462Salfred		return 1;
164274462Salfred	}
164374462Salfred	grp->gr_ptr.gt_addrinfo = ai;
164474462Salfred	while (ai != NULL) {
164574462Salfred		if (ai->ai_canonname == NULL) {
164674462Salfred			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
164774462Salfred			    sizeof host, NULL, 0, ninumeric) != 0)
164874462Salfred				strlcpy(host, "?", sizeof(host));
164974462Salfred			ai->ai_canonname = strdup(host);
165074462Salfred			ai->ai_flags |= AI_CANONNAME;
165175641Siedowse		}
165274462Salfred		if (debug)
165375635Siedowse			fprintf(stderr, "got host %s\n", ai->ai_canonname);
165475635Siedowse		/*
165575635Siedowse		 * Sanity check: make sure we don't already have an entry
165675635Siedowse		 * for this host in the grouplist.
165775635Siedowse		 */
165875635Siedowse		for (checkgrp = tgrp; checkgrp != NULL;
165975635Siedowse		    checkgrp = checkgrp->gr_next) {
166075635Siedowse			if (checkgrp->gr_type != GT_HOST)
166175635Siedowse				continue;
166275635Siedowse			for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
166375635Siedowse			    tai = tai->ai_next) {
166475801Siedowse				if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
166575635Siedowse					continue;
166675635Siedowse				if (debug)
166775635Siedowse					fprintf(stderr,
166875635Siedowse					    "ignoring duplicate host %s\n",
166975635Siedowse					    ai->ai_canonname);
167075635Siedowse				grp->gr_type = GT_IGNORE;
167175635Siedowse				return (0);
167275635Siedowse			}
167375635Siedowse		}
167474462Salfred		ai = ai->ai_next;
16751558Srgrimes	}
167675635Siedowse	grp->gr_type = GT_HOST;
16771558Srgrimes	return (0);
16781558Srgrimes}
16791558Srgrimes
16801558Srgrimes/*
16811558Srgrimes * Free up an exports list component
16821558Srgrimes */
16831558Srgrimesvoid
16841558Srgrimesfree_exp(ep)
16851558Srgrimes	struct exportlist *ep;
16861558Srgrimes{
16871558Srgrimes
16881558Srgrimes	if (ep->ex_defdir) {
16891558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
16901558Srgrimes		free((caddr_t)ep->ex_defdir);
16911558Srgrimes	}
16921558Srgrimes	if (ep->ex_fsdir)
16931558Srgrimes		free(ep->ex_fsdir);
169427447Sdfr	if (ep->ex_indexfile)
169527447Sdfr		free(ep->ex_indexfile);
16961558Srgrimes	free_dir(ep->ex_dirl);
16971558Srgrimes	free((caddr_t)ep);
16981558Srgrimes}
16991558Srgrimes
17001558Srgrimes/*
17011558Srgrimes * Free hosts.
17021558Srgrimes */
17031558Srgrimesvoid
17041558Srgrimesfree_host(hp)
17051558Srgrimes	struct hostlist *hp;
17061558Srgrimes{
17071558Srgrimes	struct hostlist *hp2;
17081558Srgrimes
17091558Srgrimes	while (hp) {
17101558Srgrimes		hp2 = hp;
17111558Srgrimes		hp = hp->ht_next;
17121558Srgrimes		free((caddr_t)hp2);
17131558Srgrimes	}
17141558Srgrimes}
17151558Srgrimes
17161558Srgrimesstruct hostlist *
17171558Srgrimesget_ht()
17181558Srgrimes{
17191558Srgrimes	struct hostlist *hp;
17201558Srgrimes
17211558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
17221558Srgrimes	if (hp == (struct hostlist *)NULL)
17231558Srgrimes		out_of_mem();
17241558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
17259336Sdfr	hp->ht_flag = 0;
17261558Srgrimes	return (hp);
17271558Srgrimes}
17281558Srgrimes
17291558Srgrimes/*
17301558Srgrimes * Out of memory, fatal
17311558Srgrimes */
17321558Srgrimesvoid
17331558Srgrimesout_of_mem()
17341558Srgrimes{
17351558Srgrimes
173637663Scharnier	syslog(LOG_ERR, "out of memory");
17371558Srgrimes	exit(2);
17381558Srgrimes}
17391558Srgrimes
17401558Srgrimes/*
17411558Srgrimes * Do the mount syscall with the update flag to push the export info into
17421558Srgrimes * the kernel.
17431558Srgrimes */
17441558Srgrimesint
17451558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
17461558Srgrimes	struct exportlist *ep;
17471558Srgrimes	struct grouplist *grp;
17481558Srgrimes	int exflags;
174972650Sgreen	struct xucred *anoncrp;
17501558Srgrimes	char *dirp;
17511558Srgrimes	int dirplen;
17521558Srgrimes	struct statfs *fsb;
17531558Srgrimes{
175475841Siedowse	struct statfs fsb1;
175574462Salfred	struct addrinfo *ai;
175675801Siedowse	struct export_args *eap;
175774462Salfred	char *cp = NULL;
17581558Srgrimes	int done;
17591558Srgrimes	char savedc = '\0';
17601558Srgrimes	union {
17611558Srgrimes		struct ufs_args ua;
17621558Srgrimes		struct iso_args ia;
17639336Sdfr		struct msdosfs_args da;
176454093Ssemenu		struct ntfs_args na;
17651558Srgrimes	} args;
17661558Srgrimes
176775801Siedowse	bzero(&args, sizeof args);
176875801Siedowse	/* XXX, we assume that all xx_args look like ufs_args. */
17691558Srgrimes	args.ua.fspec = 0;
177075801Siedowse	eap = &args.ua.export;
177175801Siedowse
177275801Siedowse	eap->ex_flags = exflags;
177375801Siedowse	eap->ex_anon = *anoncrp;
177475801Siedowse	eap->ex_indexfile = ep->ex_indexfile;
177575641Siedowse	if (grp->gr_type == GT_HOST)
177674462Salfred		ai = grp->gr_ptr.gt_addrinfo;
177775641Siedowse	else
177875641Siedowse		ai = NULL;
17791558Srgrimes	done = FALSE;
17801558Srgrimes	while (!done) {
17811558Srgrimes		switch (grp->gr_type) {
17821558Srgrimes		case GT_HOST:
178375641Siedowse			if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
178474462Salfred				goto skip;
178575801Siedowse			eap->ex_addr = ai->ai_addr;
178675801Siedowse			eap->ex_addrlen = ai->ai_addrlen;
178775801Siedowse			eap->ex_masklen = 0;
17881558Srgrimes			break;
17891558Srgrimes		case GT_NET:
179075801Siedowse			if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
179174462Salfred			    have_v6 == 0)
179274462Salfred				goto skip;
179375801Siedowse			eap->ex_addr =
179475801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
179575801Siedowse			eap->ex_addrlen = args.ua.export.ex_addr->sa_len;
179675801Siedowse			eap->ex_mask =
179775801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
179875801Siedowse			eap->ex_masklen = args.ua.export.ex_mask->sa_len;
17991558Srgrimes			break;
180075641Siedowse		case GT_DEFAULT:
180175801Siedowse			eap->ex_addr = NULL;
180275801Siedowse			eap->ex_addrlen = 0;
180375801Siedowse			eap->ex_mask = NULL;
180475801Siedowse			eap->ex_masklen = 0;
180575641Siedowse			break;
18067401Swpaul		case GT_IGNORE:
18077401Swpaul			return(0);
18087401Swpaul			break;
18091558Srgrimes		default:
181037663Scharnier			syslog(LOG_ERR, "bad grouptype");
18111558Srgrimes			if (cp)
18121558Srgrimes				*cp = savedc;
18131558Srgrimes			return (1);
18141558Srgrimes		};
18151558Srgrimes
18161558Srgrimes		/*
18171558Srgrimes		 * XXX:
18181558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
18191558Srgrimes		 * of looping back up the dirp to the mount point??
18201558Srgrimes		 * Also, needs to know how to export all types of local
182196707Strhodes		 * exportable filesystems and not just "ufs".
18221558Srgrimes		 */
18239336Sdfr		while (mount(fsb->f_fstypename, dirp,
182474462Salfred		    fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
18251558Srgrimes			if (cp)
18261558Srgrimes				*cp-- = savedc;
18271558Srgrimes			else
18281558Srgrimes				cp = dirp + dirplen - 1;
1829100336Sjoerg			if (opt_flags & OP_QUIET)
1830100336Sjoerg				return (1);
18311558Srgrimes			if (errno == EPERM) {
183275635Siedowse				if (debug)
183375635Siedowse					warnx("can't change attributes for %s",
183475635Siedowse					    dirp);
18351558Srgrimes				syslog(LOG_ERR,
183637663Scharnier				   "can't change attributes for %s", dirp);
18371558Srgrimes				return (1);
18381558Srgrimes			}
18391558Srgrimes			if (opt_flags & OP_ALLDIRS) {
1840100336Sjoerg				if (errno == EINVAL)
1841100336Sjoerg					syslog(LOG_ERR,
1842100336Sjoerg		"-alldirs requested but %s is not a filesystem mountpoint",
1843100336Sjoerg						dirp);
1844100336Sjoerg				else
1845100336Sjoerg					syslog(LOG_ERR,
1846100336Sjoerg						"could not remount %s: %m",
1847100336Sjoerg						dirp);
18481558Srgrimes				return (1);
18491558Srgrimes			}
18501558Srgrimes			/* back up over the last component */
18511558Srgrimes			while (*cp == '/' && cp > dirp)
18521558Srgrimes				cp--;
18531558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
18541558Srgrimes				cp--;
18551558Srgrimes			if (cp == dirp) {
18561558Srgrimes				if (debug)
185737663Scharnier					warnx("mnt unsucc");
185837663Scharnier				syslog(LOG_ERR, "can't export %s", dirp);
18591558Srgrimes				return (1);
18601558Srgrimes			}
18611558Srgrimes			savedc = *cp;
18621558Srgrimes			*cp = '\0';
186375841Siedowse			/* Check that we're still on the same filesystem. */
186475841Siedowse			if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
186575841Siedowse			    &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
186675841Siedowse				*cp = savedc;
186775841Siedowse				syslog(LOG_ERR, "can't export %s", dirp);
186875841Siedowse				return (1);
186975841Siedowse			}
18701558Srgrimes		}
187174462Salfredskip:
187275641Siedowse		if (ai != NULL)
187374462Salfred			ai = ai->ai_next;
187475641Siedowse		if (ai == NULL)
18751558Srgrimes			done = TRUE;
18761558Srgrimes	}
18771558Srgrimes	if (cp)
18781558Srgrimes		*cp = savedc;
18791558Srgrimes	return (0);
18801558Srgrimes}
18811558Srgrimes
18821558Srgrimes/*
18831558Srgrimes * Translate a net address.
188475801Siedowse *
188575801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
18861558Srgrimes */
18871558Srgrimesint
18881558Srgrimesget_net(cp, net, maskflg)
18891558Srgrimes	char *cp;
18901558Srgrimes	struct netmsk *net;
18911558Srgrimes	int maskflg;
18921558Srgrimes{
189375861Siedowse	struct netent *np = NULL;
189474462Salfred	char *name, *p, *prefp;
189575801Siedowse	struct sockaddr_in sin;
189675861Siedowse	struct sockaddr *sa = NULL;
189774462Salfred	struct addrinfo hints, *ai = NULL;
189874462Salfred	char netname[NI_MAXHOST];
189974462Salfred	long preflen;
19001558Srgrimes
190175635Siedowse	p = prefp = NULL;
190274462Salfred	if ((opt_flags & OP_MASKLEN) && !maskflg) {
190374462Salfred		p = strchr(cp, '/');
190474462Salfred		*p = '\0';
190574462Salfred		prefp = p + 1;
190674462Salfred	}
190774462Salfred
190875861Siedowse	/*
190975861Siedowse	 * Check for a numeric address first. We wish to avoid
191075861Siedowse	 * possible DNS lookups in getnetbyname().
191175861Siedowse	 */
191275861Siedowse	if (isxdigit(*cp) || *cp == ':') {
191374462Salfred		memset(&hints, 0, sizeof hints);
191475801Siedowse		/* Ensure the mask and the network have the same family. */
191575801Siedowse		if (maskflg && (opt_flags & OP_NET))
191675801Siedowse			hints.ai_family = net->nt_net.ss_family;
191775801Siedowse		else if (!maskflg && (opt_flags & OP_HAVEMASK))
191875801Siedowse			hints.ai_family = net->nt_mask.ss_family;
191975801Siedowse		else
192075801Siedowse			hints.ai_family = AF_UNSPEC;
192174462Salfred		hints.ai_flags = AI_NUMERICHOST;
192275861Siedowse		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
192375861Siedowse			sa = ai->ai_addr;
192475861Siedowse		if (sa != NULL && ai->ai_family == AF_INET) {
192574462Salfred			/*
192675801Siedowse			 * The address in `cp' is really a network address, so
192775801Siedowse			 * use inet_network() to re-interpret this correctly.
192875801Siedowse			 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
192974462Salfred			 */
193075801Siedowse			bzero(&sin, sizeof sin);
193174462Salfred			sin.sin_family = AF_INET;
193274462Salfred			sin.sin_len = sizeof sin;
193375801Siedowse			sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
193474462Salfred			if (debug)
193575801Siedowse				fprintf(stderr, "get_net: v4 addr %s\n",
193675801Siedowse				    inet_ntoa(sin.sin_addr));
193774462Salfred			sa = (struct sockaddr *)&sin;
193875861Siedowse		}
193975861Siedowse	}
194075861Siedowse	if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
194175861Siedowse		bzero(&sin, sizeof sin);
194275861Siedowse		sin.sin_family = AF_INET;
194375861Siedowse		sin.sin_len = sizeof sin;
194475861Siedowse		sin.sin_addr = inet_makeaddr(np->n_net, 0);
194575861Siedowse		sa = (struct sockaddr *)&sin;
194675861Siedowse	}
194775861Siedowse	if (sa == NULL)
194874462Salfred		goto fail;
194925318Spst
195075801Siedowse	if (maskflg) {
195175801Siedowse		/* The specified sockaddr is a mask. */
195275801Siedowse		if (checkmask(sa) != 0)
195375801Siedowse			goto fail;
195475801Siedowse		bcopy(sa, &net->nt_mask, sa->sa_len);
195575801Siedowse		opt_flags |= OP_HAVEMASK;
195675801Siedowse	} else {
195775801Siedowse		/* The specified sockaddr is a network address. */
195875801Siedowse		bcopy(sa, &net->nt_net, sa->sa_len);
195974462Salfred
196075801Siedowse		/* Get a network name for the export list. */
196175801Siedowse		if (np) {
196275801Siedowse			name = np->n_name;
196375801Siedowse		} else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
196475801Siedowse		   NULL, 0, ninumeric) == 0) {
196575801Siedowse			name = netname;
196675801Siedowse		} else {
196775801Siedowse			goto fail;
196875801Siedowse		}
196975801Siedowse		if ((net->nt_name = strdup(name)) == NULL)
197075801Siedowse			out_of_mem();
197175801Siedowse
197275801Siedowse		/*
197375801Siedowse		 * Extract a mask from either a "/<masklen>" suffix, or
197475801Siedowse		 * from the class of an IPv4 address.
197575801Siedowse		 */
197674462Salfred		if (opt_flags & OP_MASKLEN) {
197774462Salfred			preflen = strtol(prefp, NULL, 10);
197875801Siedowse			if (preflen < 0L || preflen == LONG_MAX)
197974462Salfred				goto fail;
198075801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
198175801Siedowse			if (makemask(&net->nt_mask, (int)preflen) != 0)
198275801Siedowse				goto fail;
198375801Siedowse			opt_flags |= OP_HAVEMASK;
198474462Salfred			*p = '/';
198575801Siedowse		} else if (sa->sa_family == AF_INET &&
198675801Siedowse		    (opt_flags & OP_MASK) == 0) {
198775801Siedowse			in_addr_t addr;
198874462Salfred
198975801Siedowse			addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
199075801Siedowse			if (IN_CLASSA(addr))
199175801Siedowse				preflen = 8;
199275801Siedowse			else if (IN_CLASSB(addr))
199375801Siedowse				preflen = 16;
199475801Siedowse			else if (IN_CLASSC(addr))
199575801Siedowse				preflen = 24;
199675801Siedowse			else if (IN_CLASSD(addr))
199775801Siedowse				preflen = 28;
199875801Siedowse			else
199975801Siedowse				preflen = 32;	/* XXX */
200075801Siedowse
200175801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
200275801Siedowse			makemask(&net->nt_mask, (int)preflen);
200375801Siedowse			opt_flags |= OP_HAVEMASK;
200474462Salfred		}
200574462Salfred	}
200674462Salfred
200774462Salfred	if (ai)
200874462Salfred		freeaddrinfo(ai);
200974462Salfred	return 0;
201074462Salfred
201174462Salfredfail:
201274462Salfred	if (ai)
201374462Salfred		freeaddrinfo(ai);
201474462Salfred	return 1;
20151558Srgrimes}
20161558Srgrimes
20171558Srgrimes/*
20181558Srgrimes * Parse out the next white space separated field
20191558Srgrimes */
20201558Srgrimesvoid
20211558Srgrimesnextfield(cp, endcp)
20221558Srgrimes	char **cp;
20231558Srgrimes	char **endcp;
20241558Srgrimes{
20251558Srgrimes	char *p;
20261558Srgrimes
20271558Srgrimes	p = *cp;
20281558Srgrimes	while (*p == ' ' || *p == '\t')
20291558Srgrimes		p++;
20301558Srgrimes	if (*p == '\n' || *p == '\0')
20311558Srgrimes		*cp = *endcp = p;
20321558Srgrimes	else {
20331558Srgrimes		*cp = p++;
20341558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
20351558Srgrimes			p++;
20361558Srgrimes		*endcp = p;
20371558Srgrimes	}
20381558Srgrimes}
20391558Srgrimes
20401558Srgrimes/*
20411558Srgrimes * Get an exports file line. Skip over blank lines and handle line
20421558Srgrimes * continuations.
20431558Srgrimes */
20441558Srgrimesint
20451558Srgrimesget_line()
20461558Srgrimes{
20471558Srgrimes	char *p, *cp;
204896622Siedowse	size_t len;
20491558Srgrimes	int totlen, cont_line;
20501558Srgrimes
20511558Srgrimes	/*
20521558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
20531558Srgrimes	 */
20541558Srgrimes	p = line;
20551558Srgrimes	totlen = 0;
20561558Srgrimes	do {
205796622Siedowse		if ((p = fgetln(exp_file, &len)) == NULL)
20581558Srgrimes			return (0);
20591558Srgrimes		cp = p + len - 1;
20601558Srgrimes		cont_line = 0;
20611558Srgrimes		while (cp >= p &&
20621558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
20631558Srgrimes			if (*cp == '\\')
20641558Srgrimes				cont_line = 1;
20651558Srgrimes			cp--;
20661558Srgrimes			len--;
20671558Srgrimes		}
206879117Sdd		if (cont_line) {
206979117Sdd			*++cp = ' ';
207079117Sdd			len++;
207179117Sdd		}
207296622Siedowse		if (linesize < len + totlen + 1) {
207396622Siedowse			linesize = len + totlen + 1;
207496622Siedowse			line = realloc(line, linesize);
207596622Siedowse			if (line == NULL)
207696622Siedowse				out_of_mem();
20771558Srgrimes		}
207896622Siedowse		memcpy(line + totlen, p, len);
207996622Siedowse		totlen += len;
208096622Siedowse		line[totlen] = '\0';
20811558Srgrimes	} while (totlen == 0 || cont_line);
20821558Srgrimes	return (1);
20831558Srgrimes}
20841558Srgrimes
20851558Srgrimes/*
20861558Srgrimes * Parse a description of a credential.
20871558Srgrimes */
20881558Srgrimesvoid
20891558Srgrimesparsecred(namelist, cr)
20901558Srgrimes	char *namelist;
209172650Sgreen	struct xucred *cr;
20921558Srgrimes{
20931558Srgrimes	char *name;
20941558Srgrimes	int cnt;
20951558Srgrimes	char *names;
20961558Srgrimes	struct passwd *pw;
20971558Srgrimes	struct group *gr;
20981558Srgrimes	int ngroups, groups[NGROUPS + 1];
20991558Srgrimes
210091354Sdd	cr->cr_version = XUCRED_VERSION;
21011558Srgrimes	/*
210237663Scharnier	 * Set up the unprivileged user.
21031558Srgrimes	 */
21041558Srgrimes	cr->cr_uid = -2;
21051558Srgrimes	cr->cr_groups[0] = -2;
21061558Srgrimes	cr->cr_ngroups = 1;
21071558Srgrimes	/*
21081558Srgrimes	 * Get the user's password table entry.
21091558Srgrimes	 */
21101558Srgrimes	names = strsep(&namelist, " \t\n");
21111558Srgrimes	name = strsep(&names, ":");
21121558Srgrimes	if (isdigit(*name) || *name == '-')
21131558Srgrimes		pw = getpwuid(atoi(name));
21141558Srgrimes	else
21151558Srgrimes		pw = getpwnam(name);
21161558Srgrimes	/*
21171558Srgrimes	 * Credentials specified as those of a user.
21181558Srgrimes	 */
21191558Srgrimes	if (names == NULL) {
21201558Srgrimes		if (pw == NULL) {
212137663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
21221558Srgrimes			return;
21231558Srgrimes		}
21241558Srgrimes		cr->cr_uid = pw->pw_uid;
21251558Srgrimes		ngroups = NGROUPS + 1;
21261558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
212737663Scharnier			syslog(LOG_ERR, "too many groups");
21281558Srgrimes		/*
21291558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
21301558Srgrimes		 */
21311558Srgrimes		cr->cr_ngroups = ngroups - 1;
21321558Srgrimes		cr->cr_groups[0] = groups[0];
21331558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
21341558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
21351558Srgrimes		return;
21361558Srgrimes	}
21371558Srgrimes	/*
21381558Srgrimes	 * Explicit credential specified as a colon separated list:
21391558Srgrimes	 *	uid:gid:gid:...
21401558Srgrimes	 */
21411558Srgrimes	if (pw != NULL)
21421558Srgrimes		cr->cr_uid = pw->pw_uid;
21431558Srgrimes	else if (isdigit(*name) || *name == '-')
21441558Srgrimes		cr->cr_uid = atoi(name);
21451558Srgrimes	else {
214637663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
21471558Srgrimes		return;
21481558Srgrimes	}
21491558Srgrimes	cr->cr_ngroups = 0;
21501558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
21511558Srgrimes		name = strsep(&names, ":");
21521558Srgrimes		if (isdigit(*name) || *name == '-') {
21531558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
21541558Srgrimes		} else {
21551558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
215637663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
21571558Srgrimes				continue;
21581558Srgrimes			}
21591558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
21601558Srgrimes		}
21611558Srgrimes	}
21621558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
216337663Scharnier		syslog(LOG_ERR, "too many groups");
21641558Srgrimes}
21651558Srgrimes
21661558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
21671558Srgrimes/*
21681558Srgrimes * Routines that maintain the remote mounttab
21691558Srgrimes */
21701558Srgrimesvoid
21711558Srgrimesget_mountlist()
21721558Srgrimes{
21731558Srgrimes	struct mountlist *mlp, **mlpp;
217423681Speter	char *host, *dirp, *cp;
21751558Srgrimes	char str[STRSIZ];
21761558Srgrimes	FILE *mlfile;
21771558Srgrimes
21781558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
217953117Sbillf		if (errno == ENOENT)
218053117Sbillf			return;
218153117Sbillf		else {
218253117Sbillf			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
218353117Sbillf			return;
218453117Sbillf		}
21851558Srgrimes	}
21861558Srgrimes	mlpp = &mlhead;
21871558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
218823681Speter		cp = str;
218923681Speter		host = strsep(&cp, " \t\n");
219023681Speter		dirp = strsep(&cp, " \t\n");
219123681Speter		if (host == NULL || dirp == NULL)
21921558Srgrimes			continue;
21931558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
219437663Scharnier		if (mlp == (struct mountlist *)NULL)
219537663Scharnier			out_of_mem();
219623681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
219723681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
219823681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
219923681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
22001558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
22011558Srgrimes		*mlpp = mlp;
22021558Srgrimes		mlpp = &mlp->ml_next;
22031558Srgrimes	}
22041558Srgrimes	fclose(mlfile);
22051558Srgrimes}
22061558Srgrimes
220775635Siedowsevoid
220875635Siedowsedel_mlist(char *hostp, char *dirp)
22091558Srgrimes{
22101558Srgrimes	struct mountlist *mlp, **mlpp;
22111558Srgrimes	struct mountlist *mlp2;
22121558Srgrimes	FILE *mlfile;
22131558Srgrimes	int fnd = 0;
22141558Srgrimes
22151558Srgrimes	mlpp = &mlhead;
22161558Srgrimes	mlp = mlhead;
22171558Srgrimes	while (mlp) {
22181558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
22191558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
22201558Srgrimes			fnd = 1;
22211558Srgrimes			mlp2 = mlp;
22221558Srgrimes			*mlpp = mlp = mlp->ml_next;
22231558Srgrimes			free((caddr_t)mlp2);
22241558Srgrimes		} else {
22251558Srgrimes			mlpp = &mlp->ml_next;
22261558Srgrimes			mlp = mlp->ml_next;
22271558Srgrimes		}
22281558Srgrimes	}
22291558Srgrimes	if (fnd) {
22301558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
223137663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
22321558Srgrimes			return;
22331558Srgrimes		}
22341558Srgrimes		mlp = mlhead;
22351558Srgrimes		while (mlp) {
22361558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22371558Srgrimes			mlp = mlp->ml_next;
22381558Srgrimes		}
22391558Srgrimes		fclose(mlfile);
22401558Srgrimes	}
22411558Srgrimes}
22421558Srgrimes
22431558Srgrimesvoid
22441558Srgrimesadd_mlist(hostp, dirp)
22451558Srgrimes	char *hostp, *dirp;
22461558Srgrimes{
22471558Srgrimes	struct mountlist *mlp, **mlpp;
22481558Srgrimes	FILE *mlfile;
22491558Srgrimes
22501558Srgrimes	mlpp = &mlhead;
22511558Srgrimes	mlp = mlhead;
22521558Srgrimes	while (mlp) {
22531558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
22541558Srgrimes			return;
22551558Srgrimes		mlpp = &mlp->ml_next;
22561558Srgrimes		mlp = mlp->ml_next;
22571558Srgrimes	}
22581558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
225937663Scharnier	if (mlp == (struct mountlist *)NULL)
226037663Scharnier		out_of_mem();
22611558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
22621558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
22631558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
22641558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
22651558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
22661558Srgrimes	*mlpp = mlp;
22671558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
226837663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
22691558Srgrimes		return;
22701558Srgrimes	}
22711558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22721558Srgrimes	fclose(mlfile);
22731558Srgrimes}
22741558Srgrimes
22751558Srgrimes/*
22761558Srgrimes * Free up a group list.
22771558Srgrimes */
22781558Srgrimesvoid
22791558Srgrimesfree_grp(grp)
22801558Srgrimes	struct grouplist *grp;
22811558Srgrimes{
22821558Srgrimes	if (grp->gr_type == GT_HOST) {
228374462Salfred		if (grp->gr_ptr.gt_addrinfo != NULL)
228474462Salfred			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
22851558Srgrimes	} else if (grp->gr_type == GT_NET) {
22861558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
22871558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
22881558Srgrimes	}
22891558Srgrimes	free((caddr_t)grp);
22901558Srgrimes}
22911558Srgrimes
22921558Srgrimes#ifdef DEBUG
22931558Srgrimesvoid
22941558SrgrimesSYSLOG(int pri, const char *fmt, ...)
22951558Srgrimes{
22961558Srgrimes	va_list ap;
22971558Srgrimes
22981558Srgrimes	va_start(ap, fmt);
22991558Srgrimes	vfprintf(stderr, fmt, ap);
23001558Srgrimes	va_end(ap);
23011558Srgrimes}
23021558Srgrimes#endif /* DEBUG */
23031558Srgrimes
23041558Srgrimes/*
23051558Srgrimes * Check options for consistency.
23061558Srgrimes */
23071558Srgrimesint
23081558Srgrimescheck_options(dp)
23091558Srgrimes	struct dirlist *dp;
23101558Srgrimes{
23111558Srgrimes
23121558Srgrimes	if (dp == (struct dirlist *)NULL)
23131558Srgrimes	    return (1);
231483653Speter	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
231583653Speter	    syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
23161558Srgrimes	    return (1);
23171558Srgrimes	}
23181558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
231975801Siedowse		syslog(LOG_ERR, "-mask requires -network");
232075801Siedowse		return (1);
23211558Srgrimes	}
232275801Siedowse	if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
232375801Siedowse		syslog(LOG_ERR, "-network requires mask specification");
232475801Siedowse		return (1);
232575801Siedowse	}
232675801Siedowse	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
232775801Siedowse		syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
232875801Siedowse		return (1);
232975801Siedowse	}
23301558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
233145927Salex	    syslog(LOG_ERR, "-alldirs has multiple directories");
23321558Srgrimes	    return (1);
23331558Srgrimes	}
23341558Srgrimes	return (0);
23351558Srgrimes}
23361558Srgrimes
23371558Srgrimes/*
23381558Srgrimes * Check an absolute directory path for any symbolic links. Return true
23391558Srgrimes */
23401558Srgrimesint
23411558Srgrimescheck_dirpath(dirp)
23421558Srgrimes	char *dirp;
23431558Srgrimes{
23441558Srgrimes	char *cp;
23451558Srgrimes	int ret = 1;
23461558Srgrimes	struct stat sb;
23471558Srgrimes
23481558Srgrimes	cp = dirp + 1;
23491558Srgrimes	while (*cp && ret) {
23501558Srgrimes		if (*cp == '/') {
23511558Srgrimes			*cp = '\0';
23529336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23531558Srgrimes				ret = 0;
23541558Srgrimes			*cp = '/';
23551558Srgrimes		}
23561558Srgrimes		cp++;
23571558Srgrimes	}
23589336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23591558Srgrimes		ret = 0;
23601558Srgrimes	return (ret);
23611558Srgrimes}
23629336Sdfr
236375801Siedowse/*
236475801Siedowse * Make a netmask according to the specified prefix length. The ss_family
236575801Siedowse * and other non-address fields must be initialised before calling this.
236675801Siedowse */
236775801Siedowseint
236875801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen)
236974462Salfred{
237075801Siedowse	u_char *p;
237175801Siedowse	int bits, i, len;
237274462Salfred
237375801Siedowse	if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
237475801Siedowse		return (-1);
2375103949Smike	if (bitlen > len * CHAR_BIT)
237675801Siedowse		return (-1);
237774462Salfred
237875801Siedowse	for (i = 0; i < len; i++) {
2379103949Smike		bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
238075801Siedowse		*p++ = (1 << bits) - 1;
238175801Siedowse		bitlen -= bits;
238274462Salfred	}
238375801Siedowse	return 0;
238474462Salfred}
238574462Salfred
238675801Siedowse/*
238775801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask
238875801Siedowse * is acceptable (i.e. of the form 1...10....0).
238975801Siedowse */
239075801Siedowseint
239175801Siedowsecheckmask(struct sockaddr *sa)
239274462Salfred{
239375801Siedowse	u_char *mask;
239475801Siedowse	int i, len;
239574462Salfred
239675801Siedowse	if ((mask = sa_rawaddr(sa, &len)) == NULL)
239775801Siedowse		return (-1);
239875801Siedowse
239975801Siedowse	for (i = 0; i < len; i++)
240075801Siedowse		if (mask[i] != 0xff)
240175801Siedowse			break;
240275801Siedowse	if (i < len) {
240375801Siedowse		if (~mask[i] & (u_char)(~mask[i] + 1))
240475801Siedowse			return (-1);
240575801Siedowse		i++;
240674462Salfred	}
240775801Siedowse	for (; i < len; i++)
240875801Siedowse		if (mask[i] != 0)
240975801Siedowse			return (-1);
241075801Siedowse	return (0);
241174462Salfred}
241274462Salfred
241375801Siedowse/*
241475801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if
241575801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'.
241675801Siedowse * If samask is NULL, perform a full comparision.
241775801Siedowse */
241875801Siedowseint
241975801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
242074462Salfred{
242175801Siedowse	unsigned char *p1, *p2, *mask;
242275801Siedowse	int len, i;
242374462Salfred
242475801Siedowse	if (sa1->sa_family != sa2->sa_family ||
242575801Siedowse	    (p1 = sa_rawaddr(sa1, &len)) == NULL ||
242675801Siedowse	    (p2 = sa_rawaddr(sa2, NULL)) == NULL)
242775801Siedowse		return (1);
242875801Siedowse
242975801Siedowse	switch (sa1->sa_family) {
243074462Salfred	case AF_INET6:
243175801Siedowse		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
243275801Siedowse		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
243375801Siedowse			return (1);
243474462Salfred		break;
243574462Salfred	}
243674462Salfred
243775801Siedowse	/* Simple binary comparison if no mask specified. */
243875801Siedowse	if (samask == NULL)
243975801Siedowse		return (memcmp(p1, p2, len));
244074462Salfred
244175801Siedowse	/* Set up the mask, and do a mask-based comparison. */
244275801Siedowse	if (sa1->sa_family != samask->sa_family ||
244375801Siedowse	    (mask = sa_rawaddr(samask, NULL)) == NULL)
244475801Siedowse		return (1);
244574462Salfred
244675801Siedowse	for (i = 0; i < len; i++)
244775801Siedowse		if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
244875801Siedowse			return (1);
244975801Siedowse	return (0);
245074462Salfred}
245174462Salfred
245275801Siedowse/*
245375801Siedowse * Return a pointer to the part of the sockaddr that contains the
245475801Siedowse * raw address, and set *nbytes to its length in bytes. Returns
245575801Siedowse * NULL if the address family is unknown.
245675801Siedowse */
245775801Siedowsevoid *
245875801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) {
245975801Siedowse	void *p;
246074462Salfred	int len;
246174462Salfred
246275801Siedowse	switch (sa->sa_family) {
246374462Salfred	case AF_INET:
246475801Siedowse		len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
246575801Siedowse		p = &((struct sockaddr_in *)sa)->sin_addr;
246674462Salfred		break;
246774462Salfred	case AF_INET6:
246875801Siedowse		len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
246975801Siedowse		p = &((struct sockaddr_in6 *)sa)->sin6_addr;
247074462Salfred		break;
247174462Salfred	default:
247275801Siedowse		p = NULL;
247375801Siedowse		len = 0;
247474462Salfred	}
247574462Salfred
247675801Siedowse	if (nbytes != NULL)
247775801Siedowse		*nbytes = len;
247875801Siedowse	return (p);
247974462Salfred}
248074462Salfred
248175754Siedowsevoid
248275754Siedowsehuphandler(int sig)
248375754Siedowse{
248475754Siedowse	got_sighup = 1;
248575754Siedowse}
248675754Siedowse
248774462Salfredvoid terminate(sig)
248874462Salfredint sig;
248974462Salfred{
249074462Salfred	close(mountdlockfd);
249174462Salfred	unlink(MOUNTDLOCK);
249274792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
249374792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
249474462Salfred	exit (0);
249574462Salfred}
2496