mountd.c revision 109363
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 109363 2003-01-16 07:27:30Z mbr $");
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,
372100505Sume		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,
377100505Sume		IPV6_V6ONLY, &one, sizeof one) < 0){
37874462Salfred		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP 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:
552100117Salfred		if (!svc_sendreply(transp, 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		}
5631558Srgrimes		if (!svc_getargs(transp, 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) {
59928911Sguido				if (!svc_sendreply(transp, 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);
6151558Srgrimes				if (!svc_sendreply(transp, xdr_long,
6161558Srgrimes				    (caddr_t)&bad))
61737663Scharnier					syslog(LOG_ERR, "can't send reply");
6189336Sdfr				sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6191558Srgrimes				return;
6201558Srgrimes			}
6219336Sdfr			if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
62237663Scharnier				syslog(LOG_ERR, "can't send reply");
62374462Salfred			if (!lookup_failed)
62474462Salfred				add_mlist(host, dirpath);
6251558Srgrimes			else
62674462Salfred				add_mlist(numerichost, dirpath);
6271558Srgrimes			if (debug)
62837663Scharnier				warnx("mount successful");
62931656Sguido			if (log)
63031656Sguido				syslog(LOG_NOTICE,
63131656Sguido				    "mount request succeeded from %s for %s",
63274462Salfred				    numerichost, dirpath);
63331656Sguido		} else {
6341558Srgrimes			bad = EACCES;
63531656Sguido			syslog(LOG_NOTICE,
63631656Sguido			    "mount request denied from %s for %s",
63774462Salfred			    numerichost, dirpath);
63831656Sguido		}
63928911Sguido
64028911Sguido		if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
64137663Scharnier			syslog(LOG_ERR, "can't send reply");
6429336Sdfr		sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
6431558Srgrimes		return;
6441558Srgrimes	case RPCMNT_DUMP:
6451558Srgrimes		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
64637663Scharnier			syslog(LOG_ERR, "can't send reply");
64731656Sguido		else if (log)
64831656Sguido			syslog(LOG_NOTICE,
64931656Sguido			    "dump request succeeded from %s",
65074462Salfred			    numerichost);
6511558Srgrimes		return;
6521558Srgrimes	case RPCMNT_UMOUNT:
6539336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
65431656Sguido			syslog(LOG_NOTICE,
65531656Sguido			    "umount request from %s from unprivileged port",
65674462Salfred			    numerichost);
6571558Srgrimes			svcerr_weakauth(transp);
6581558Srgrimes			return;
6591558Srgrimes		}
66051968Salfred		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
66131656Sguido			syslog(LOG_NOTICE, "undecodable umount request from %s",
66274462Salfred			    numerichost);
6631558Srgrimes			svcerr_decode(transp);
6641558Srgrimes			return;
6651558Srgrimes		}
66651968Salfred		if (realpath(rpcpath, dirpath) == NULL) {
66751968Salfred			syslog(LOG_NOTICE, "umount request from %s "
66851968Salfred			    "for non existent path %s",
66974462Salfred			    numerichost, dirpath);
67051968Salfred		}
6711558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
67237663Scharnier			syslog(LOG_ERR, "can't send reply");
67374462Salfred		if (!lookup_failed)
67475635Siedowse			del_mlist(host, dirpath);
67575635Siedowse		del_mlist(numerichost, dirpath);
67631656Sguido		if (log)
67731656Sguido			syslog(LOG_NOTICE,
67831656Sguido			    "umount request succeeded from %s for %s",
67974462Salfred			    numerichost, dirpath);
6801558Srgrimes		return;
6811558Srgrimes	case RPCMNT_UMNTALL:
6829336Sdfr		if (sport >= IPPORT_RESERVED && resvport_only) {
68331656Sguido			syslog(LOG_NOTICE,
68431656Sguido			    "umountall request from %s from unprivileged port",
68574462Salfred			    numerichost);
6861558Srgrimes			svcerr_weakauth(transp);
6871558Srgrimes			return;
6881558Srgrimes		}
6891558Srgrimes		if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
69037663Scharnier			syslog(LOG_ERR, "can't send reply");
69174462Salfred		if (!lookup_failed)
69275635Siedowse			del_mlist(host, NULL);
69375635Siedowse		del_mlist(numerichost, NULL);
69431656Sguido		if (log)
69531656Sguido			syslog(LOG_NOTICE,
69631656Sguido			    "umountall request succeeded from %s",
69774462Salfred			    numerichost);
6981558Srgrimes		return;
6991558Srgrimes	case RPCMNT_EXPORT:
7001558Srgrimes		if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
701100117Salfred			if (!svc_sendreply(transp, xdr_explist_brief, (caddr_t)NULL))
702100117Salfred				syslog(LOG_ERR, "can't send reply");
70331656Sguido		if (log)
70431656Sguido			syslog(LOG_NOTICE,
70531656Sguido			    "export request succeeded from %s",
70674462Salfred			    numerichost);
7071558Srgrimes		return;
7081558Srgrimes	default:
7091558Srgrimes		svcerr_noproc(transp);
7101558Srgrimes		return;
7111558Srgrimes	}
7121558Srgrimes}
7131558Srgrimes
7141558Srgrimes/*
7151558Srgrimes * Xdr conversion for a dirpath string
7161558Srgrimes */
7171558Srgrimesint
7181558Srgrimesxdr_dir(xdrsp, dirp)
7191558Srgrimes	XDR *xdrsp;
7201558Srgrimes	char *dirp;
7211558Srgrimes{
7221558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
7231558Srgrimes}
7241558Srgrimes
7251558Srgrimes/*
7269336Sdfr * Xdr routine to generate file handle reply
7271558Srgrimes */
7281558Srgrimesint
7299336Sdfrxdr_fhs(xdrsp, cp)
7301558Srgrimes	XDR *xdrsp;
7319336Sdfr	caddr_t cp;
7321558Srgrimes{
73392806Sobrien	struct fhreturn *fhrp = (struct fhreturn *)cp;
7349336Sdfr	u_long ok = 0, len, auth;
7351558Srgrimes
7361558Srgrimes	if (!xdr_long(xdrsp, &ok))
7371558Srgrimes		return (0);
7389336Sdfr	switch (fhrp->fhr_vers) {
7399336Sdfr	case 1:
7409336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
7419336Sdfr	case 3:
7429336Sdfr		len = NFSX_V3FH;
7439336Sdfr		if (!xdr_long(xdrsp, &len))
7449336Sdfr			return (0);
7459336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
7469336Sdfr			return (0);
74783653Speter		auth = RPCAUTH_UNIX;
7489336Sdfr		len = 1;
7499336Sdfr		if (!xdr_long(xdrsp, &len))
7509336Sdfr			return (0);
7519336Sdfr		return (xdr_long(xdrsp, &auth));
7529336Sdfr	};
7539336Sdfr	return (0);
7541558Srgrimes}
7551558Srgrimes
7561558Srgrimesint
7571558Srgrimesxdr_mlist(xdrsp, cp)
7581558Srgrimes	XDR *xdrsp;
7591558Srgrimes	caddr_t cp;
7601558Srgrimes{
7611558Srgrimes	struct mountlist *mlp;
7621558Srgrimes	int true = 1;
7631558Srgrimes	int false = 0;
7641558Srgrimes	char *strp;
7651558Srgrimes
7661558Srgrimes	mlp = mlhead;
7671558Srgrimes	while (mlp) {
7681558Srgrimes		if (!xdr_bool(xdrsp, &true))
7691558Srgrimes			return (0);
7701558Srgrimes		strp = &mlp->ml_host[0];
7711558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
7721558Srgrimes			return (0);
7731558Srgrimes		strp = &mlp->ml_dirp[0];
7741558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
7751558Srgrimes			return (0);
7761558Srgrimes		mlp = mlp->ml_next;
7771558Srgrimes	}
7781558Srgrimes	if (!xdr_bool(xdrsp, &false))
7791558Srgrimes		return (0);
7801558Srgrimes	return (1);
7811558Srgrimes}
7821558Srgrimes
7831558Srgrimes/*
7841558Srgrimes * Xdr conversion for export list
7851558Srgrimes */
7861558Srgrimesint
787100117Salfredxdr_explist_common(xdrsp, cp, brief)
7881558Srgrimes	XDR *xdrsp;
7891558Srgrimes	caddr_t cp;
790100117Salfred	int brief;
7911558Srgrimes{
7921558Srgrimes	struct exportlist *ep;
7931558Srgrimes	int false = 0;
7949336Sdfr	int putdef;
7959336Sdfr	sigset_t sighup_mask;
7961558Srgrimes
7979336Sdfr	sigemptyset(&sighup_mask);
7989336Sdfr	sigaddset(&sighup_mask, SIGHUP);
7999336Sdfr	sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
8001558Srgrimes	ep = exphead;
8011558Srgrimes	while (ep) {
8021558Srgrimes		putdef = 0;
803100117Salfred		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
804100117Salfred			       &putdef, brief))
8051558Srgrimes			goto errout;
8061558Srgrimes		if (ep->ex_defdir && putdef == 0 &&
8071558Srgrimes			put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
808100117Salfred			&putdef, brief))
8091558Srgrimes			goto errout;
8101558Srgrimes		ep = ep->ex_next;
8111558Srgrimes	}
8129336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8131558Srgrimes	if (!xdr_bool(xdrsp, &false))
8141558Srgrimes		return (0);
8151558Srgrimes	return (1);
8161558Srgrimeserrout:
8179336Sdfr	sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
8181558Srgrimes	return (0);
8191558Srgrimes}
8201558Srgrimes
8211558Srgrimes/*
8221558Srgrimes * Called from xdr_explist() to traverse the tree and export the
8231558Srgrimes * directory paths.
8241558Srgrimes */
8251558Srgrimesint
826100117Salfredput_exlist(dp, xdrsp, adp, putdefp, brief)
8271558Srgrimes	struct dirlist *dp;
8281558Srgrimes	XDR *xdrsp;
8291558Srgrimes	struct dirlist *adp;
8301558Srgrimes	int *putdefp;
831100117Salfred	int brief;
8321558Srgrimes{
8331558Srgrimes	struct grouplist *grp;
8341558Srgrimes	struct hostlist *hp;
8351558Srgrimes	int true = 1;
8361558Srgrimes	int false = 0;
8371558Srgrimes	int gotalldir = 0;
8381558Srgrimes	char *strp;
8391558Srgrimes
8401558Srgrimes	if (dp) {
841100117Salfred		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
8421558Srgrimes			return (1);
8431558Srgrimes		if (!xdr_bool(xdrsp, &true))
8441558Srgrimes			return (1);
8451558Srgrimes		strp = dp->dp_dirp;
8461558Srgrimes		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
8471558Srgrimes			return (1);
8481558Srgrimes		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
8491558Srgrimes			gotalldir = 1;
8501558Srgrimes			*putdefp = 1;
8511558Srgrimes		}
852100117Salfred		if (brief) {
853100117Salfred			if (!xdr_bool(xdrsp, &true))
854100117Salfred				return (1);
855100117Salfred			strp = "(...)";
856100117Salfred			if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
857100117Salfred				return (1);
858100117Salfred		} else if ((dp->dp_flag & DP_DEFSET) == 0 &&
8591558Srgrimes		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
8601558Srgrimes			hp = dp->dp_hosts;
8611558Srgrimes			while (hp) {
8621558Srgrimes				grp = hp->ht_grp;
8631558Srgrimes				if (grp->gr_type == GT_HOST) {
8641558Srgrimes					if (!xdr_bool(xdrsp, &true))
8651558Srgrimes						return (1);
86674462Salfred					strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
8678871Srgrimes					if (!xdr_string(xdrsp, &strp,
8681558Srgrimes					    RPCMNT_NAMELEN))
8691558Srgrimes						return (1);
8701558Srgrimes				} else if (grp->gr_type == GT_NET) {
8711558Srgrimes					if (!xdr_bool(xdrsp, &true))
8721558Srgrimes						return (1);
8731558Srgrimes					strp = grp->gr_ptr.gt_net.nt_name;
8748871Srgrimes					if (!xdr_string(xdrsp, &strp,
8751558Srgrimes					    RPCMNT_NAMELEN))
8761558Srgrimes						return (1);
8771558Srgrimes				}
8781558Srgrimes				hp = hp->ht_next;
8791558Srgrimes				if (gotalldir && hp == (struct hostlist *)NULL) {
8801558Srgrimes					hp = adp->dp_hosts;
8811558Srgrimes					gotalldir = 0;
8821558Srgrimes				}
8831558Srgrimes			}
8841558Srgrimes		}
8851558Srgrimes		if (!xdr_bool(xdrsp, &false))
8861558Srgrimes			return (1);
887100117Salfred		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
8881558Srgrimes			return (1);
8891558Srgrimes	}
8901558Srgrimes	return (0);
8911558Srgrimes}
8921558Srgrimes
893100117Salfredint
894100117Salfredxdr_explist(xdrsp, cp)
895100117Salfred	XDR *xdrsp;
896100117Salfred	caddr_t cp;
897100117Salfred{
898100117Salfred
899100117Salfred	return xdr_explist_common(xdrsp, cp, 0);
900100117Salfred}
901100117Salfred
902100117Salfredint
903100117Salfredxdr_explist_brief(xdrsp, cp)
904100117Salfred	XDR *xdrsp;
905100117Salfred	caddr_t cp;
906100117Salfred{
907100117Salfred
908100117Salfred	return xdr_explist_common(xdrsp, cp, 1);
909100117Salfred}
910100117Salfred
91196622Siedowsechar *line;
91296622Siedowseint linesize;
9131558SrgrimesFILE *exp_file;
9141558Srgrimes
9151558Srgrimes/*
9161558Srgrimes * Get the export list
9171558Srgrimes */
9181558Srgrimesvoid
9191558Srgrimesget_exportlist()
9201558Srgrimes{
9211558Srgrimes	struct exportlist *ep, *ep2;
9221558Srgrimes	struct grouplist *grp, *tgrp;
9231558Srgrimes	struct exportlist **epp;
9241558Srgrimes	struct dirlist *dirhead;
9251558Srgrimes	struct statfs fsb, *fsp;
92672650Sgreen	struct xucred anon;
9271558Srgrimes	char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
9281558Srgrimes	int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
9291558Srgrimes
93051968Salfred	dirp = NULL;
93151968Salfred	dirplen = 0;
93251968Salfred
9331558Srgrimes	/*
9341558Srgrimes	 * First, get rid of the old list
9351558Srgrimes	 */
9361558Srgrimes	ep = exphead;
9371558Srgrimes	while (ep) {
9381558Srgrimes		ep2 = ep;
9391558Srgrimes		ep = ep->ex_next;
9401558Srgrimes		free_exp(ep2);
9411558Srgrimes	}
9421558Srgrimes	exphead = (struct exportlist *)NULL;
9431558Srgrimes
9441558Srgrimes	grp = grphead;
9451558Srgrimes	while (grp) {
9461558Srgrimes		tgrp = grp;
9471558Srgrimes		grp = grp->gr_next;
9481558Srgrimes		free_grp(tgrp);
9491558Srgrimes	}
9501558Srgrimes	grphead = (struct grouplist *)NULL;
9511558Srgrimes
9521558Srgrimes	/*
9531558Srgrimes	 * And delete exports that are in the kernel for all local
95496707Strhodes	 * filesystems.
95596707Strhodes	 * XXX: Should know how to handle all local exportable filesystems
95623681Speter	 *      instead of just "ufs".
9571558Srgrimes	 */
9581558Srgrimes	num = getmntinfo(&fsp, MNT_NOWAIT);
9591558Srgrimes	for (i = 0; i < num; i++) {
9601558Srgrimes		union {
9611558Srgrimes			struct ufs_args ua;
9621558Srgrimes			struct iso_args ia;
9639336Sdfr			struct msdosfs_args da;
96454093Ssemenu			struct ntfs_args na;
9651558Srgrimes		} targs;
9661558Srgrimes
96777435Sphk		if (!strcmp(fsp->f_fstypename, "ufs") ||
96877577Sru		    !strcmp(fsp->f_fstypename, "msdosfs") ||
96954093Ssemenu		    !strcmp(fsp->f_fstypename, "ntfs") ||
97023681Speter		    !strcmp(fsp->f_fstypename, "cd9660")) {
9719336Sdfr			targs.ua.fspec = NULL;
9729336Sdfr			targs.ua.export.ex_flags = MNT_DELEXPORT;
9739336Sdfr			if (mount(fsp->f_fstypename, fsp->f_mntonname,
97477405Siedowse			    fsp->f_flags | MNT_UPDATE, (caddr_t)&targs) < 0 &&
97577405Siedowse			    errno != ENOENT)
97677405Siedowse				syslog(LOG_ERR,
97777405Siedowse				    "can't delete exports for %s: %m",
97874462Salfred				    fsp->f_mntonname);
9791558Srgrimes		}
9801558Srgrimes		fsp++;
9811558Srgrimes	}
9821558Srgrimes
9831558Srgrimes	/*
9841558Srgrimes	 * Read in the exports file and build the list, calling
9851558Srgrimes	 * mount() as we go along to push the export rules into the kernel.
9861558Srgrimes	 */
9871558Srgrimes	if ((exp_file = fopen(exname, "r")) == NULL) {
98837663Scharnier		syslog(LOG_ERR, "can't open %s", exname);
9891558Srgrimes		exit(2);
9901558Srgrimes	}
9911558Srgrimes	dirhead = (struct dirlist *)NULL;
9921558Srgrimes	while (get_line()) {
9931558Srgrimes		if (debug)
99437663Scharnier			warnx("got line %s", line);
9951558Srgrimes		cp = line;
9961558Srgrimes		nextfield(&cp, &endcp);
9971558Srgrimes		if (*cp == '#')
9981558Srgrimes			goto nextline;
9991558Srgrimes
10001558Srgrimes		/*
10011558Srgrimes		 * Set defaults.
10021558Srgrimes		 */
10031558Srgrimes		has_host = FALSE;
10041558Srgrimes		anon = def_anon;
10051558Srgrimes		exflags = MNT_EXPORTED;
10061558Srgrimes		got_nondir = 0;
10071558Srgrimes		opt_flags = 0;
10081558Srgrimes		ep = (struct exportlist *)NULL;
10091558Srgrimes
10101558Srgrimes		/*
10111558Srgrimes		 * Create new exports list entry
10121558Srgrimes		 */
10131558Srgrimes		len = endcp-cp;
10141558Srgrimes		tgrp = grp = get_grp();
10151558Srgrimes		while (len > 0) {
10161558Srgrimes			if (len > RPCMNT_NAMELEN) {
10171558Srgrimes			    getexp_err(ep, tgrp);
10181558Srgrimes			    goto nextline;
10191558Srgrimes			}
10201558Srgrimes			if (*cp == '-') {
10211558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10221558Srgrimes				getexp_err(ep, tgrp);
10231558Srgrimes				goto nextline;
10241558Srgrimes			    }
10251558Srgrimes			    if (debug)
102637663Scharnier				warnx("doing opt %s", cp);
10271558Srgrimes			    got_nondir = 1;
10281558Srgrimes			    if (do_opt(&cp, &endcp, ep, grp, &has_host,
10291558Srgrimes				&exflags, &anon)) {
10301558Srgrimes				getexp_err(ep, tgrp);
10311558Srgrimes				goto nextline;
10321558Srgrimes			    }
10331558Srgrimes			} else if (*cp == '/') {
10341558Srgrimes			    savedc = *endcp;
10351558Srgrimes			    *endcp = '\0';
10361558Srgrimes			    if (check_dirpath(cp) &&
10371558Srgrimes				statfs(cp, &fsb) >= 0) {
10381558Srgrimes				if (got_nondir) {
103937663Scharnier				    syslog(LOG_ERR, "dirs must be first");
10401558Srgrimes				    getexp_err(ep, tgrp);
10411558Srgrimes				    goto nextline;
10421558Srgrimes				}
10431558Srgrimes				if (ep) {
10441558Srgrimes				    if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
10451558Srgrimes					ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
10461558Srgrimes					getexp_err(ep, tgrp);
10471558Srgrimes					goto nextline;
10481558Srgrimes				    }
10491558Srgrimes				} else {
10501558Srgrimes				    /*
10511558Srgrimes				     * See if this directory is already
10521558Srgrimes				     * in the list.
10531558Srgrimes				     */
10541558Srgrimes				    ep = ex_search(&fsb.f_fsid);
10551558Srgrimes				    if (ep == (struct exportlist *)NULL) {
10561558Srgrimes					ep = get_exp();
10571558Srgrimes					ep->ex_fs = fsb.f_fsid;
10581558Srgrimes					ep->ex_fsdir = (char *)
10591558Srgrimes					    malloc(strlen(fsb.f_mntonname) + 1);
10601558Srgrimes					if (ep->ex_fsdir)
10611558Srgrimes					    strcpy(ep->ex_fsdir,
10621558Srgrimes						fsb.f_mntonname);
10631558Srgrimes					else
10641558Srgrimes					    out_of_mem();
10651558Srgrimes					if (debug)
106674462Salfred						warnx("making new ep fs=0x%x,0x%x",
106774462Salfred						    fsb.f_fsid.val[0],
106874462Salfred						    fsb.f_fsid.val[1]);
10691558Srgrimes				    } else if (debug)
107037663Scharnier					warnx("found ep fs=0x%x,0x%x",
10711558Srgrimes					    fsb.f_fsid.val[0],
10721558Srgrimes					    fsb.f_fsid.val[1]);
10731558Srgrimes				}
10741558Srgrimes
10751558Srgrimes				/*
10761558Srgrimes				 * Add dirpath to export mount point.
10771558Srgrimes				 */
10781558Srgrimes				dirp = add_expdir(&dirhead, cp, len);
10791558Srgrimes				dirplen = len;
10801558Srgrimes			    } else {
10811558Srgrimes				getexp_err(ep, tgrp);
10821558Srgrimes				goto nextline;
10831558Srgrimes			    }
10841558Srgrimes			    *endcp = savedc;
10851558Srgrimes			} else {
10861558Srgrimes			    savedc = *endcp;
10871558Srgrimes			    *endcp = '\0';
10881558Srgrimes			    got_nondir = 1;
10891558Srgrimes			    if (ep == (struct exportlist *)NULL) {
10901558Srgrimes				getexp_err(ep, tgrp);
10911558Srgrimes				goto nextline;
10921558Srgrimes			    }
10931558Srgrimes
10941558Srgrimes			    /*
10951558Srgrimes			     * Get the host or netgroup.
10961558Srgrimes			     */
10971558Srgrimes			    setnetgrent(cp);
10981558Srgrimes			    netgrp = getnetgrent(&hst, &usr, &dom);
10991558Srgrimes			    do {
11001558Srgrimes				if (has_host) {
11011558Srgrimes				    grp->gr_next = get_grp();
11021558Srgrimes				    grp = grp->gr_next;
11031558Srgrimes				}
11041558Srgrimes				if (netgrp) {
110537003Sjoerg				    if (hst == 0) {
110637663Scharnier					syslog(LOG_ERR,
110737663Scharnier				"null hostname in netgroup %s, skipping", cp);
110837004Sjoerg					grp->gr_type = GT_IGNORE;
110937003Sjoerg				    } else if (get_host(hst, grp, tgrp)) {
111037663Scharnier					syslog(LOG_ERR,
111137663Scharnier			"bad host %s in netgroup %s, skipping", hst, cp);
111229317Sjlemon					grp->gr_type = GT_IGNORE;
11131558Srgrimes				    }
11147401Swpaul				} else if (get_host(cp, grp, tgrp)) {
111537663Scharnier				    syslog(LOG_ERR, "bad host %s, skipping", cp);
111629317Sjlemon				    grp->gr_type = GT_IGNORE;
11171558Srgrimes				}
11181558Srgrimes				has_host = TRUE;
11191558Srgrimes			    } while (netgrp && getnetgrent(&hst, &usr, &dom));
11201558Srgrimes			    endnetgrent();
11211558Srgrimes			    *endcp = savedc;
11221558Srgrimes			}
11231558Srgrimes			cp = endcp;
11241558Srgrimes			nextfield(&cp, &endcp);
11251558Srgrimes			len = endcp - cp;
11261558Srgrimes		}
11271558Srgrimes		if (check_options(dirhead)) {
11281558Srgrimes			getexp_err(ep, tgrp);
11291558Srgrimes			goto nextline;
11301558Srgrimes		}
11311558Srgrimes		if (!has_host) {
113275641Siedowse			grp->gr_type = GT_DEFAULT;
11331558Srgrimes			if (debug)
113437663Scharnier				warnx("adding a default entry");
11351558Srgrimes
11361558Srgrimes		/*
11371558Srgrimes		 * Don't allow a network export coincide with a list of
11381558Srgrimes		 * host(s) on the same line.
11391558Srgrimes		 */
11401558Srgrimes		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
114175801Siedowse			syslog(LOG_ERR, "network/host conflict");
11421558Srgrimes			getexp_err(ep, tgrp);
11431558Srgrimes			goto nextline;
114429317Sjlemon
114574462Salfred		/*
114674462Salfred		 * If an export list was specified on this line, make sure
114729317Sjlemon		 * that we have at least one valid entry, otherwise skip it.
114829317Sjlemon		 */
114929317Sjlemon		} else {
115029317Sjlemon			grp = tgrp;
115174462Salfred			while (grp && grp->gr_type == GT_IGNORE)
115229317Sjlemon				grp = grp->gr_next;
115329317Sjlemon			if (! grp) {
115429317Sjlemon			    getexp_err(ep, tgrp);
115529317Sjlemon			    goto nextline;
115629317Sjlemon			}
11571558Srgrimes		}
11581558Srgrimes
11591558Srgrimes		/*
11601558Srgrimes		 * Loop through hosts, pushing the exports into the kernel.
11611558Srgrimes		 * After loop, tgrp points to the start of the list and
11621558Srgrimes		 * grp points to the last entry in the list.
11631558Srgrimes		 */
11641558Srgrimes		grp = tgrp;
11651558Srgrimes		do {
116675635Siedowse			if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
116775635Siedowse			    &fsb)) {
116875635Siedowse				getexp_err(ep, tgrp);
116975635Siedowse				goto nextline;
117075635Siedowse			}
11711558Srgrimes		} while (grp->gr_next && (grp = grp->gr_next));
11721558Srgrimes
11731558Srgrimes		/*
11741558Srgrimes		 * Success. Update the data structures.
11751558Srgrimes		 */
11761558Srgrimes		if (has_host) {
11779336Sdfr			hang_dirp(dirhead, tgrp, ep, opt_flags);
11781558Srgrimes			grp->gr_next = grphead;
11791558Srgrimes			grphead = tgrp;
11801558Srgrimes		} else {
11811558Srgrimes			hang_dirp(dirhead, (struct grouplist *)NULL, ep,
11829336Sdfr				opt_flags);
11831558Srgrimes			free_grp(grp);
11841558Srgrimes		}
11851558Srgrimes		dirhead = (struct dirlist *)NULL;
11861558Srgrimes		if ((ep->ex_flag & EX_LINKED) == 0) {
11871558Srgrimes			ep2 = exphead;
11881558Srgrimes			epp = &exphead;
11891558Srgrimes
11901558Srgrimes			/*
11911558Srgrimes			 * Insert in the list in alphabetical order.
11921558Srgrimes			 */
11931558Srgrimes			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
11941558Srgrimes				epp = &ep2->ex_next;
11951558Srgrimes				ep2 = ep2->ex_next;
11961558Srgrimes			}
11971558Srgrimes			if (ep2)
11981558Srgrimes				ep->ex_next = ep2;
11991558Srgrimes			*epp = ep;
12001558Srgrimes			ep->ex_flag |= EX_LINKED;
12011558Srgrimes		}
12021558Srgrimesnextline:
12031558Srgrimes		if (dirhead) {
12041558Srgrimes			free_dir(dirhead);
12051558Srgrimes			dirhead = (struct dirlist *)NULL;
12061558Srgrimes		}
12071558Srgrimes	}
12081558Srgrimes	fclose(exp_file);
12091558Srgrimes}
12101558Srgrimes
12111558Srgrimes/*
12121558Srgrimes * Allocate an export list element
12131558Srgrimes */
12141558Srgrimesstruct exportlist *
12151558Srgrimesget_exp()
12161558Srgrimes{
12171558Srgrimes	struct exportlist *ep;
12181558Srgrimes
12191558Srgrimes	ep = (struct exportlist *)malloc(sizeof (struct exportlist));
12201558Srgrimes	if (ep == (struct exportlist *)NULL)
12211558Srgrimes		out_of_mem();
122223681Speter	memset(ep, 0, sizeof(struct exportlist));
12231558Srgrimes	return (ep);
12241558Srgrimes}
12251558Srgrimes
12261558Srgrimes/*
12271558Srgrimes * Allocate a group list element
12281558Srgrimes */
12291558Srgrimesstruct grouplist *
12301558Srgrimesget_grp()
12311558Srgrimes{
12321558Srgrimes	struct grouplist *gp;
12331558Srgrimes
12341558Srgrimes	gp = (struct grouplist *)malloc(sizeof (struct grouplist));
12351558Srgrimes	if (gp == (struct grouplist *)NULL)
12361558Srgrimes		out_of_mem();
123723681Speter	memset(gp, 0, sizeof(struct grouplist));
12381558Srgrimes	return (gp);
12391558Srgrimes}
12401558Srgrimes
12411558Srgrimes/*
12421558Srgrimes * Clean up upon an error in get_exportlist().
12431558Srgrimes */
12441558Srgrimesvoid
12451558Srgrimesgetexp_err(ep, grp)
12461558Srgrimes	struct exportlist *ep;
12471558Srgrimes	struct grouplist *grp;
12481558Srgrimes{
12491558Srgrimes	struct grouplist *tgrp;
12501558Srgrimes
1251100336Sjoerg	if (!(opt_flags & OP_QUIET))
1252100336Sjoerg		syslog(LOG_ERR, "bad exports list line %s", line);
12531558Srgrimes	if (ep && (ep->ex_flag & EX_LINKED) == 0)
12541558Srgrimes		free_exp(ep);
12551558Srgrimes	while (grp) {
12561558Srgrimes		tgrp = grp;
12571558Srgrimes		grp = grp->gr_next;
12581558Srgrimes		free_grp(tgrp);
12591558Srgrimes	}
12601558Srgrimes}
12611558Srgrimes
12621558Srgrimes/*
12631558Srgrimes * Search the export list for a matching fs.
12641558Srgrimes */
12651558Srgrimesstruct exportlist *
12661558Srgrimesex_search(fsid)
12671558Srgrimes	fsid_t *fsid;
12681558Srgrimes{
12691558Srgrimes	struct exportlist *ep;
12701558Srgrimes
12711558Srgrimes	ep = exphead;
12721558Srgrimes	while (ep) {
12731558Srgrimes		if (ep->ex_fs.val[0] == fsid->val[0] &&
12741558Srgrimes		    ep->ex_fs.val[1] == fsid->val[1])
12751558Srgrimes			return (ep);
12761558Srgrimes		ep = ep->ex_next;
12771558Srgrimes	}
12781558Srgrimes	return (ep);
12791558Srgrimes}
12801558Srgrimes
12811558Srgrimes/*
12821558Srgrimes * Add a directory path to the list.
12831558Srgrimes */
12841558Srgrimeschar *
12851558Srgrimesadd_expdir(dpp, cp, len)
12861558Srgrimes	struct dirlist **dpp;
12871558Srgrimes	char *cp;
12881558Srgrimes	int len;
12891558Srgrimes{
12901558Srgrimes	struct dirlist *dp;
12911558Srgrimes
12921558Srgrimes	dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
129337663Scharnier	if (dp == (struct dirlist *)NULL)
129437663Scharnier		out_of_mem();
12951558Srgrimes	dp->dp_left = *dpp;
12961558Srgrimes	dp->dp_right = (struct dirlist *)NULL;
12971558Srgrimes	dp->dp_flag = 0;
12981558Srgrimes	dp->dp_hosts = (struct hostlist *)NULL;
12991558Srgrimes	strcpy(dp->dp_dirp, cp);
13001558Srgrimes	*dpp = dp;
13011558Srgrimes	return (dp->dp_dirp);
13021558Srgrimes}
13031558Srgrimes
13041558Srgrimes/*
13051558Srgrimes * Hang the dir list element off the dirpath binary tree as required
13061558Srgrimes * and update the entry for host.
13071558Srgrimes */
13081558Srgrimesvoid
13099336Sdfrhang_dirp(dp, grp, ep, flags)
13101558Srgrimes	struct dirlist *dp;
13111558Srgrimes	struct grouplist *grp;
13121558Srgrimes	struct exportlist *ep;
13139336Sdfr	int flags;
13141558Srgrimes{
13151558Srgrimes	struct hostlist *hp;
13161558Srgrimes	struct dirlist *dp2;
13171558Srgrimes
13189336Sdfr	if (flags & OP_ALLDIRS) {
13191558Srgrimes		if (ep->ex_defdir)
13201558Srgrimes			free((caddr_t)dp);
13211558Srgrimes		else
13221558Srgrimes			ep->ex_defdir = dp;
13239336Sdfr		if (grp == (struct grouplist *)NULL) {
13241558Srgrimes			ep->ex_defdir->dp_flag |= DP_DEFSET;
13259336Sdfr		} else while (grp) {
13261558Srgrimes			hp = get_ht();
13271558Srgrimes			hp->ht_grp = grp;
13281558Srgrimes			hp->ht_next = ep->ex_defdir->dp_hosts;
13291558Srgrimes			ep->ex_defdir->dp_hosts = hp;
13301558Srgrimes			grp = grp->gr_next;
13311558Srgrimes		}
13321558Srgrimes	} else {
13331558Srgrimes
13341558Srgrimes		/*
133537663Scharnier		 * Loop through the directories adding them to the tree.
13361558Srgrimes		 */
13371558Srgrimes		while (dp) {
13381558Srgrimes			dp2 = dp->dp_left;
13399336Sdfr			add_dlist(&ep->ex_dirl, dp, grp, flags);
13401558Srgrimes			dp = dp2;
13411558Srgrimes		}
13421558Srgrimes	}
13431558Srgrimes}
13441558Srgrimes
13451558Srgrimes/*
13461558Srgrimes * Traverse the binary tree either updating a node that is already there
13471558Srgrimes * for the new directory or adding the new node.
13481558Srgrimes */
13491558Srgrimesvoid
13509336Sdfradd_dlist(dpp, newdp, grp, flags)
13511558Srgrimes	struct dirlist **dpp;
13521558Srgrimes	struct dirlist *newdp;
13531558Srgrimes	struct grouplist *grp;
13549336Sdfr	int flags;
13551558Srgrimes{
13561558Srgrimes	struct dirlist *dp;
13571558Srgrimes	struct hostlist *hp;
13581558Srgrimes	int cmp;
13591558Srgrimes
13601558Srgrimes	dp = *dpp;
13611558Srgrimes	if (dp) {
13621558Srgrimes		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
13631558Srgrimes		if (cmp > 0) {
13649336Sdfr			add_dlist(&dp->dp_left, newdp, grp, flags);
13651558Srgrimes			return;
13661558Srgrimes		} else if (cmp < 0) {
13679336Sdfr			add_dlist(&dp->dp_right, newdp, grp, flags);
13681558Srgrimes			return;
13691558Srgrimes		} else
13701558Srgrimes			free((caddr_t)newdp);
13711558Srgrimes	} else {
13721558Srgrimes		dp = newdp;
13731558Srgrimes		dp->dp_left = (struct dirlist *)NULL;
13741558Srgrimes		*dpp = dp;
13751558Srgrimes	}
13761558Srgrimes	if (grp) {
13771558Srgrimes
13781558Srgrimes		/*
13791558Srgrimes		 * Hang all of the host(s) off of the directory point.
13801558Srgrimes		 */
13811558Srgrimes		do {
13821558Srgrimes			hp = get_ht();
13831558Srgrimes			hp->ht_grp = grp;
13841558Srgrimes			hp->ht_next = dp->dp_hosts;
13851558Srgrimes			dp->dp_hosts = hp;
13861558Srgrimes			grp = grp->gr_next;
13871558Srgrimes		} while (grp);
13889336Sdfr	} else {
13891558Srgrimes		dp->dp_flag |= DP_DEFSET;
13909336Sdfr	}
13911558Srgrimes}
13921558Srgrimes
13931558Srgrimes/*
13941558Srgrimes * Search for a dirpath on the export point.
13951558Srgrimes */
13961558Srgrimesstruct dirlist *
139774462Salfreddirp_search(dp, dirp)
13981558Srgrimes	struct dirlist *dp;
139974462Salfred	char *dirp;
14001558Srgrimes{
14011558Srgrimes	int cmp;
14021558Srgrimes
14031558Srgrimes	if (dp) {
140474462Salfred		cmp = strcmp(dp->dp_dirp, dirp);
14051558Srgrimes		if (cmp > 0)
140674462Salfred			return (dirp_search(dp->dp_left, dirp));
14071558Srgrimes		else if (cmp < 0)
140874462Salfred			return (dirp_search(dp->dp_right, dirp));
14091558Srgrimes		else
14101558Srgrimes			return (dp);
14111558Srgrimes	}
14121558Srgrimes	return (dp);
14131558Srgrimes}
14141558Srgrimes
14151558Srgrimes/*
14161558Srgrimes * Scan for a host match in a directory tree.
14171558Srgrimes */
14181558Srgrimesint
14199336Sdfrchk_host(dp, saddr, defsetp, hostsetp)
14201558Srgrimes	struct dirlist *dp;
142174462Salfred	struct sockaddr *saddr;
14221558Srgrimes	int *defsetp;
14239336Sdfr	int *hostsetp;
14241558Srgrimes{
14251558Srgrimes	struct hostlist *hp;
14261558Srgrimes	struct grouplist *grp;
142774462Salfred	struct addrinfo *ai;
14281558Srgrimes
14291558Srgrimes	if (dp) {
14301558Srgrimes		if (dp->dp_flag & DP_DEFSET)
14319336Sdfr			*defsetp = dp->dp_flag;
14321558Srgrimes		hp = dp->dp_hosts;
14331558Srgrimes		while (hp) {
14341558Srgrimes			grp = hp->ht_grp;
14351558Srgrimes			switch (grp->gr_type) {
14361558Srgrimes			case GT_HOST:
143774462Salfred				ai = grp->gr_ptr.gt_addrinfo;
143874462Salfred				for (; ai; ai = ai->ai_next) {
143975801Siedowse					if (!sacmp(ai->ai_addr, saddr, NULL)) {
144074462Salfred						*hostsetp =
144174462Salfred						    (hp->ht_flag | DP_HOSTSET);
144274462Salfred						return (1);
144374462Salfred					}
14449336Sdfr				}
144575801Siedowse				break;
14461558Srgrimes			case GT_NET:
144775801Siedowse				if (!sacmp(saddr, (struct sockaddr *)
144875801Siedowse				    &grp->gr_ptr.gt_net.nt_net,
144975801Siedowse				    (struct sockaddr *)
145075801Siedowse				    &grp->gr_ptr.gt_net.nt_mask)) {
145174462Salfred					*hostsetp = (hp->ht_flag | DP_HOSTSET);
145274462Salfred					return (1);
145374462Salfred				}
145475801Siedowse				break;
145575801Siedowse			}
14561558Srgrimes			hp = hp->ht_next;
14571558Srgrimes		}
14581558Srgrimes	}
14591558Srgrimes	return (0);
14601558Srgrimes}
14611558Srgrimes
14621558Srgrimes/*
14631558Srgrimes * Scan tree for a host that matches the address.
14641558Srgrimes */
14651558Srgrimesint
14661558Srgrimesscan_tree(dp, saddr)
14671558Srgrimes	struct dirlist *dp;
146874462Salfred	struct sockaddr *saddr;
14691558Srgrimes{
14709336Sdfr	int defset, hostset;
14711558Srgrimes
14721558Srgrimes	if (dp) {
14731558Srgrimes		if (scan_tree(dp->dp_left, saddr))
14741558Srgrimes			return (1);
14759336Sdfr		if (chk_host(dp, saddr, &defset, &hostset))
14761558Srgrimes			return (1);
14771558Srgrimes		if (scan_tree(dp->dp_right, saddr))
14781558Srgrimes			return (1);
14791558Srgrimes	}
14801558Srgrimes	return (0);
14811558Srgrimes}
14821558Srgrimes
14831558Srgrimes/*
14841558Srgrimes * Traverse the dirlist tree and free it up.
14851558Srgrimes */
14861558Srgrimesvoid
14871558Srgrimesfree_dir(dp)
14881558Srgrimes	struct dirlist *dp;
14891558Srgrimes{
14901558Srgrimes
14911558Srgrimes	if (dp) {
14921558Srgrimes		free_dir(dp->dp_left);
14931558Srgrimes		free_dir(dp->dp_right);
14941558Srgrimes		free_host(dp->dp_hosts);
14951558Srgrimes		free((caddr_t)dp);
14961558Srgrimes	}
14971558Srgrimes}
14981558Srgrimes
14991558Srgrimes/*
15001558Srgrimes * Parse the option string and update fields.
15011558Srgrimes * Option arguments may either be -<option>=<value> or
15021558Srgrimes * -<option> <value>
15031558Srgrimes */
15041558Srgrimesint
15051558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
15061558Srgrimes	char **cpp, **endcpp;
15071558Srgrimes	struct exportlist *ep;
15081558Srgrimes	struct grouplist *grp;
15091558Srgrimes	int *has_hostp;
15101558Srgrimes	int *exflagsp;
151172650Sgreen	struct xucred *cr;
15121558Srgrimes{
15131558Srgrimes	char *cpoptarg, *cpoptend;
15141558Srgrimes	char *cp, *endcp, *cpopt, savedc, savedc2;
15151558Srgrimes	int allflag, usedarg;
15161558Srgrimes
151751968Salfred	savedc2 = '\0';
15181558Srgrimes	cpopt = *cpp;
15191558Srgrimes	cpopt++;
15201558Srgrimes	cp = *endcpp;
15211558Srgrimes	savedc = *cp;
15221558Srgrimes	*cp = '\0';
15231558Srgrimes	while (cpopt && *cpopt) {
15241558Srgrimes		allflag = 1;
15251558Srgrimes		usedarg = -2;
152637663Scharnier		if ((cpoptend = strchr(cpopt, ','))) {
15271558Srgrimes			*cpoptend++ = '\0';
152837663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15291558Srgrimes				*cpoptarg++ = '\0';
15301558Srgrimes		} else {
153137663Scharnier			if ((cpoptarg = strchr(cpopt, '=')))
15321558Srgrimes				*cpoptarg++ = '\0';
15331558Srgrimes			else {
15341558Srgrimes				*cp = savedc;
15351558Srgrimes				nextfield(&cp, &endcp);
15361558Srgrimes				**endcpp = '\0';
15371558Srgrimes				if (endcp > cp && *cp != '-') {
15381558Srgrimes					cpoptarg = cp;
15391558Srgrimes					savedc2 = *endcp;
15401558Srgrimes					*endcp = '\0';
15411558Srgrimes					usedarg = 0;
15421558Srgrimes				}
15431558Srgrimes			}
15441558Srgrimes		}
15451558Srgrimes		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
15461558Srgrimes			*exflagsp |= MNT_EXRDONLY;
15471558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
15481558Srgrimes		    !(allflag = strcmp(cpopt, "mapall")) ||
15491558Srgrimes		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
15501558Srgrimes			usedarg++;
15511558Srgrimes			parsecred(cpoptarg, cr);
15521558Srgrimes			if (allflag == 0) {
15531558Srgrimes				*exflagsp |= MNT_EXPORTANON;
15541558Srgrimes				opt_flags |= OP_MAPALL;
15551558Srgrimes			} else
15561558Srgrimes				opt_flags |= OP_MAPROOT;
15571558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
155875801Siedowse		    !strcmp(cpopt, "m"))) {
15591558Srgrimes			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
156037663Scharnier				syslog(LOG_ERR, "bad mask: %s", cpoptarg);
15611558Srgrimes				return (1);
15621558Srgrimes			}
15631558Srgrimes			usedarg++;
15641558Srgrimes			opt_flags |= OP_MASK;
15651558Srgrimes		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
15661558Srgrimes			!strcmp(cpopt, "n"))) {
156774462Salfred			if (strchr(cpoptarg, '/') != NULL) {
156874462Salfred				if (debug)
156974462Salfred					fprintf(stderr, "setting OP_MASKLEN\n");
157074462Salfred				opt_flags |= OP_MASKLEN;
157174462Salfred			}
15721558Srgrimes			if (grp->gr_type != GT_NULL) {
157337663Scharnier				syslog(LOG_ERR, "network/host conflict");
15741558Srgrimes				return (1);
15751558Srgrimes			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
157637663Scharnier				syslog(LOG_ERR, "bad net: %s", cpoptarg);
15771558Srgrimes				return (1);
15781558Srgrimes			}
15791558Srgrimes			grp->gr_type = GT_NET;
15801558Srgrimes			*has_hostp = 1;
15811558Srgrimes			usedarg++;
15821558Srgrimes			opt_flags |= OP_NET;
15831558Srgrimes		} else if (!strcmp(cpopt, "alldirs")) {
15841558Srgrimes			opt_flags |= OP_ALLDIRS;
158527447Sdfr		} else if (!strcmp(cpopt, "public")) {
158627447Sdfr			*exflagsp |= MNT_EXPUBLIC;
158727447Sdfr		} else if (!strcmp(cpopt, "webnfs")) {
158827447Sdfr			*exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
158927447Sdfr			opt_flags |= OP_MAPALL;
159027447Sdfr		} else if (cpoptarg && !strcmp(cpopt, "index")) {
159127447Sdfr			ep->ex_indexfile = strdup(cpoptarg);
1592100336Sjoerg		} else if (!strcmp(cpopt, "quiet")) {
1593100336Sjoerg			opt_flags |= OP_QUIET;
15941558Srgrimes		} else {
159537663Scharnier			syslog(LOG_ERR, "bad opt %s", cpopt);
15961558Srgrimes			return (1);
15971558Srgrimes		}
15981558Srgrimes		if (usedarg >= 0) {
15991558Srgrimes			*endcp = savedc2;
16001558Srgrimes			**endcpp = savedc;
16011558Srgrimes			if (usedarg > 0) {
16021558Srgrimes				*cpp = cp;
16031558Srgrimes				*endcpp = endcp;
16041558Srgrimes			}
16051558Srgrimes			return (0);
16061558Srgrimes		}
16071558Srgrimes		cpopt = cpoptend;
16081558Srgrimes	}
16091558Srgrimes	**endcpp = savedc;
16101558Srgrimes	return (0);
16111558Srgrimes}
16121558Srgrimes
16131558Srgrimes/*
16141558Srgrimes * Translate a character string to the corresponding list of network
16151558Srgrimes * addresses for a hostname.
16161558Srgrimes */
16171558Srgrimesint
16187401Swpaulget_host(cp, grp, tgrp)
16191558Srgrimes	char *cp;
16201558Srgrimes	struct grouplist *grp;
16217401Swpaul	struct grouplist *tgrp;
16221558Srgrimes{
16237401Swpaul	struct grouplist *checkgrp;
162475635Siedowse	struct addrinfo *ai, *tai, hints;
162574462Salfred	int ecode;
162674462Salfred	char host[NI_MAXHOST];
16271558Srgrimes
162874462Salfred	if (grp->gr_type != GT_NULL) {
162974462Salfred		syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
16301558Srgrimes		return (1);
16311558Srgrimes	}
163274462Salfred	memset(&hints, 0, sizeof hints);
163374462Salfred	hints.ai_flags = AI_CANONNAME;
163474462Salfred	hints.ai_protocol = IPPROTO_UDP;
163574462Salfred	ecode = getaddrinfo(cp, NULL, &hints, &ai);
163674462Salfred	if (ecode != 0) {
163775635Siedowse		syslog(LOG_ERR,"can't get address info for host %s", cp);
163874462Salfred		return 1;
163974462Salfred	}
164074462Salfred	grp->gr_ptr.gt_addrinfo = ai;
164174462Salfred	while (ai != NULL) {
164274462Salfred		if (ai->ai_canonname == NULL) {
164374462Salfred			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
164474462Salfred			    sizeof host, NULL, 0, ninumeric) != 0)
164574462Salfred				strlcpy(host, "?", sizeof(host));
164674462Salfred			ai->ai_canonname = strdup(host);
164774462Salfred			ai->ai_flags |= AI_CANONNAME;
164875641Siedowse		}
164974462Salfred		if (debug)
165075635Siedowse			fprintf(stderr, "got host %s\n", ai->ai_canonname);
165175635Siedowse		/*
165275635Siedowse		 * Sanity check: make sure we don't already have an entry
165375635Siedowse		 * for this host in the grouplist.
165475635Siedowse		 */
165575635Siedowse		for (checkgrp = tgrp; checkgrp != NULL;
165675635Siedowse		    checkgrp = checkgrp->gr_next) {
165775635Siedowse			if (checkgrp->gr_type != GT_HOST)
165875635Siedowse				continue;
165975635Siedowse			for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
166075635Siedowse			    tai = tai->ai_next) {
166175801Siedowse				if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
166275635Siedowse					continue;
166375635Siedowse				if (debug)
166475635Siedowse					fprintf(stderr,
166575635Siedowse					    "ignoring duplicate host %s\n",
166675635Siedowse					    ai->ai_canonname);
166775635Siedowse				grp->gr_type = GT_IGNORE;
166875635Siedowse				return (0);
166975635Siedowse			}
167075635Siedowse		}
167174462Salfred		ai = ai->ai_next;
16721558Srgrimes	}
167375635Siedowse	grp->gr_type = GT_HOST;
16741558Srgrimes	return (0);
16751558Srgrimes}
16761558Srgrimes
16771558Srgrimes/*
16781558Srgrimes * Free up an exports list component
16791558Srgrimes */
16801558Srgrimesvoid
16811558Srgrimesfree_exp(ep)
16821558Srgrimes	struct exportlist *ep;
16831558Srgrimes{
16841558Srgrimes
16851558Srgrimes	if (ep->ex_defdir) {
16861558Srgrimes		free_host(ep->ex_defdir->dp_hosts);
16871558Srgrimes		free((caddr_t)ep->ex_defdir);
16881558Srgrimes	}
16891558Srgrimes	if (ep->ex_fsdir)
16901558Srgrimes		free(ep->ex_fsdir);
169127447Sdfr	if (ep->ex_indexfile)
169227447Sdfr		free(ep->ex_indexfile);
16931558Srgrimes	free_dir(ep->ex_dirl);
16941558Srgrimes	free((caddr_t)ep);
16951558Srgrimes}
16961558Srgrimes
16971558Srgrimes/*
16981558Srgrimes * Free hosts.
16991558Srgrimes */
17001558Srgrimesvoid
17011558Srgrimesfree_host(hp)
17021558Srgrimes	struct hostlist *hp;
17031558Srgrimes{
17041558Srgrimes	struct hostlist *hp2;
17051558Srgrimes
17061558Srgrimes	while (hp) {
17071558Srgrimes		hp2 = hp;
17081558Srgrimes		hp = hp->ht_next;
17091558Srgrimes		free((caddr_t)hp2);
17101558Srgrimes	}
17111558Srgrimes}
17121558Srgrimes
17131558Srgrimesstruct hostlist *
17141558Srgrimesget_ht()
17151558Srgrimes{
17161558Srgrimes	struct hostlist *hp;
17171558Srgrimes
17181558Srgrimes	hp = (struct hostlist *)malloc(sizeof (struct hostlist));
17191558Srgrimes	if (hp == (struct hostlist *)NULL)
17201558Srgrimes		out_of_mem();
17211558Srgrimes	hp->ht_next = (struct hostlist *)NULL;
17229336Sdfr	hp->ht_flag = 0;
17231558Srgrimes	return (hp);
17241558Srgrimes}
17251558Srgrimes
17261558Srgrimes/*
17271558Srgrimes * Out of memory, fatal
17281558Srgrimes */
17291558Srgrimesvoid
17301558Srgrimesout_of_mem()
17311558Srgrimes{
17321558Srgrimes
173337663Scharnier	syslog(LOG_ERR, "out of memory");
17341558Srgrimes	exit(2);
17351558Srgrimes}
17361558Srgrimes
17371558Srgrimes/*
17381558Srgrimes * Do the mount syscall with the update flag to push the export info into
17391558Srgrimes * the kernel.
17401558Srgrimes */
17411558Srgrimesint
17421558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
17431558Srgrimes	struct exportlist *ep;
17441558Srgrimes	struct grouplist *grp;
17451558Srgrimes	int exflags;
174672650Sgreen	struct xucred *anoncrp;
17471558Srgrimes	char *dirp;
17481558Srgrimes	int dirplen;
17491558Srgrimes	struct statfs *fsb;
17501558Srgrimes{
175175841Siedowse	struct statfs fsb1;
175274462Salfred	struct addrinfo *ai;
175375801Siedowse	struct export_args *eap;
175474462Salfred	char *cp = NULL;
17551558Srgrimes	int done;
17561558Srgrimes	char savedc = '\0';
17571558Srgrimes	union {
17581558Srgrimes		struct ufs_args ua;
17591558Srgrimes		struct iso_args ia;
17609336Sdfr		struct msdosfs_args da;
176154093Ssemenu		struct ntfs_args na;
17621558Srgrimes	} args;
17631558Srgrimes
176475801Siedowse	bzero(&args, sizeof args);
176575801Siedowse	/* XXX, we assume that all xx_args look like ufs_args. */
17661558Srgrimes	args.ua.fspec = 0;
176775801Siedowse	eap = &args.ua.export;
176875801Siedowse
176975801Siedowse	eap->ex_flags = exflags;
177075801Siedowse	eap->ex_anon = *anoncrp;
177175801Siedowse	eap->ex_indexfile = ep->ex_indexfile;
177275641Siedowse	if (grp->gr_type == GT_HOST)
177374462Salfred		ai = grp->gr_ptr.gt_addrinfo;
177475641Siedowse	else
177575641Siedowse		ai = NULL;
17761558Srgrimes	done = FALSE;
17771558Srgrimes	while (!done) {
17781558Srgrimes		switch (grp->gr_type) {
17791558Srgrimes		case GT_HOST:
178075641Siedowse			if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
178174462Salfred				goto skip;
178275801Siedowse			eap->ex_addr = ai->ai_addr;
178375801Siedowse			eap->ex_addrlen = ai->ai_addrlen;
178475801Siedowse			eap->ex_masklen = 0;
17851558Srgrimes			break;
17861558Srgrimes		case GT_NET:
178775801Siedowse			if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
178874462Salfred			    have_v6 == 0)
178974462Salfred				goto skip;
179075801Siedowse			eap->ex_addr =
179175801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
179275801Siedowse			eap->ex_addrlen = args.ua.export.ex_addr->sa_len;
179375801Siedowse			eap->ex_mask =
179475801Siedowse			    (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
179575801Siedowse			eap->ex_masklen = args.ua.export.ex_mask->sa_len;
17961558Srgrimes			break;
179775641Siedowse		case GT_DEFAULT:
179875801Siedowse			eap->ex_addr = NULL;
179975801Siedowse			eap->ex_addrlen = 0;
180075801Siedowse			eap->ex_mask = NULL;
180175801Siedowse			eap->ex_masklen = 0;
180275641Siedowse			break;
18037401Swpaul		case GT_IGNORE:
18047401Swpaul			return(0);
18057401Swpaul			break;
18061558Srgrimes		default:
180737663Scharnier			syslog(LOG_ERR, "bad grouptype");
18081558Srgrimes			if (cp)
18091558Srgrimes				*cp = savedc;
18101558Srgrimes			return (1);
18111558Srgrimes		};
18121558Srgrimes
18131558Srgrimes		/*
18141558Srgrimes		 * XXX:
18151558Srgrimes		 * Maybe I should just use the fsb->f_mntonname path instead
18161558Srgrimes		 * of looping back up the dirp to the mount point??
18171558Srgrimes		 * Also, needs to know how to export all types of local
181896707Strhodes		 * exportable filesystems and not just "ufs".
18191558Srgrimes		 */
18209336Sdfr		while (mount(fsb->f_fstypename, dirp,
182174462Salfred		    fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
18221558Srgrimes			if (cp)
18231558Srgrimes				*cp-- = savedc;
18241558Srgrimes			else
18251558Srgrimes				cp = dirp + dirplen - 1;
1826100336Sjoerg			if (opt_flags & OP_QUIET)
1827100336Sjoerg				return (1);
18281558Srgrimes			if (errno == EPERM) {
182975635Siedowse				if (debug)
183075635Siedowse					warnx("can't change attributes for %s",
183175635Siedowse					    dirp);
18321558Srgrimes				syslog(LOG_ERR,
183337663Scharnier				   "can't change attributes for %s", dirp);
18341558Srgrimes				return (1);
18351558Srgrimes			}
18361558Srgrimes			if (opt_flags & OP_ALLDIRS) {
1837100336Sjoerg				if (errno == EINVAL)
1838100336Sjoerg					syslog(LOG_ERR,
1839100336Sjoerg		"-alldirs requested but %s is not a filesystem mountpoint",
1840100336Sjoerg						dirp);
1841100336Sjoerg				else
1842100336Sjoerg					syslog(LOG_ERR,
1843100336Sjoerg						"could not remount %s: %m",
1844100336Sjoerg						dirp);
18451558Srgrimes				return (1);
18461558Srgrimes			}
18471558Srgrimes			/* back up over the last component */
18481558Srgrimes			while (*cp == '/' && cp > dirp)
18491558Srgrimes				cp--;
18501558Srgrimes			while (*(cp - 1) != '/' && cp > dirp)
18511558Srgrimes				cp--;
18521558Srgrimes			if (cp == dirp) {
18531558Srgrimes				if (debug)
185437663Scharnier					warnx("mnt unsucc");
185537663Scharnier				syslog(LOG_ERR, "can't export %s", dirp);
18561558Srgrimes				return (1);
18571558Srgrimes			}
18581558Srgrimes			savedc = *cp;
18591558Srgrimes			*cp = '\0';
186075841Siedowse			/* Check that we're still on the same filesystem. */
186175841Siedowse			if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
186275841Siedowse			    &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
186375841Siedowse				*cp = savedc;
186475841Siedowse				syslog(LOG_ERR, "can't export %s", dirp);
186575841Siedowse				return (1);
186675841Siedowse			}
18671558Srgrimes		}
186874462Salfredskip:
186975641Siedowse		if (ai != NULL)
187074462Salfred			ai = ai->ai_next;
187175641Siedowse		if (ai == NULL)
18721558Srgrimes			done = TRUE;
18731558Srgrimes	}
18741558Srgrimes	if (cp)
18751558Srgrimes		*cp = savedc;
18761558Srgrimes	return (0);
18771558Srgrimes}
18781558Srgrimes
18791558Srgrimes/*
18801558Srgrimes * Translate a net address.
188175801Siedowse *
188275801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
18831558Srgrimes */
18841558Srgrimesint
18851558Srgrimesget_net(cp, net, maskflg)
18861558Srgrimes	char *cp;
18871558Srgrimes	struct netmsk *net;
18881558Srgrimes	int maskflg;
18891558Srgrimes{
189075861Siedowse	struct netent *np = NULL;
189174462Salfred	char *name, *p, *prefp;
189275801Siedowse	struct sockaddr_in sin;
189375861Siedowse	struct sockaddr *sa = NULL;
189474462Salfred	struct addrinfo hints, *ai = NULL;
189574462Salfred	char netname[NI_MAXHOST];
189674462Salfred	long preflen;
18971558Srgrimes
189875635Siedowse	p = prefp = NULL;
189974462Salfred	if ((opt_flags & OP_MASKLEN) && !maskflg) {
190074462Salfred		p = strchr(cp, '/');
190174462Salfred		*p = '\0';
190274462Salfred		prefp = p + 1;
190374462Salfred	}
190474462Salfred
190575861Siedowse	/*
190675861Siedowse	 * Check for a numeric address first. We wish to avoid
190775861Siedowse	 * possible DNS lookups in getnetbyname().
190875861Siedowse	 */
190975861Siedowse	if (isxdigit(*cp) || *cp == ':') {
191074462Salfred		memset(&hints, 0, sizeof hints);
191175801Siedowse		/* Ensure the mask and the network have the same family. */
191275801Siedowse		if (maskflg && (opt_flags & OP_NET))
191375801Siedowse			hints.ai_family = net->nt_net.ss_family;
191475801Siedowse		else if (!maskflg && (opt_flags & OP_HAVEMASK))
191575801Siedowse			hints.ai_family = net->nt_mask.ss_family;
191675801Siedowse		else
191775801Siedowse			hints.ai_family = AF_UNSPEC;
191874462Salfred		hints.ai_flags = AI_NUMERICHOST;
191975861Siedowse		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
192075861Siedowse			sa = ai->ai_addr;
192175861Siedowse		if (sa != NULL && ai->ai_family == AF_INET) {
192274462Salfred			/*
192375801Siedowse			 * The address in `cp' is really a network address, so
192475801Siedowse			 * use inet_network() to re-interpret this correctly.
192575801Siedowse			 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
192674462Salfred			 */
192775801Siedowse			bzero(&sin, sizeof sin);
192874462Salfred			sin.sin_family = AF_INET;
192974462Salfred			sin.sin_len = sizeof sin;
193075801Siedowse			sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
193174462Salfred			if (debug)
193275801Siedowse				fprintf(stderr, "get_net: v4 addr %s\n",
193375801Siedowse				    inet_ntoa(sin.sin_addr));
193474462Salfred			sa = (struct sockaddr *)&sin;
193575861Siedowse		}
193675861Siedowse	}
193775861Siedowse	if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
193875861Siedowse		bzero(&sin, sizeof sin);
193975861Siedowse		sin.sin_family = AF_INET;
194075861Siedowse		sin.sin_len = sizeof sin;
194175861Siedowse		sin.sin_addr = inet_makeaddr(np->n_net, 0);
194275861Siedowse		sa = (struct sockaddr *)&sin;
194375861Siedowse	}
194475861Siedowse	if (sa == NULL)
194574462Salfred		goto fail;
194625318Spst
194775801Siedowse	if (maskflg) {
194875801Siedowse		/* The specified sockaddr is a mask. */
194975801Siedowse		if (checkmask(sa) != 0)
195075801Siedowse			goto fail;
195175801Siedowse		bcopy(sa, &net->nt_mask, sa->sa_len);
195275801Siedowse		opt_flags |= OP_HAVEMASK;
195375801Siedowse	} else {
195475801Siedowse		/* The specified sockaddr is a network address. */
195575801Siedowse		bcopy(sa, &net->nt_net, sa->sa_len);
195674462Salfred
195775801Siedowse		/* Get a network name for the export list. */
195875801Siedowse		if (np) {
195975801Siedowse			name = np->n_name;
196075801Siedowse		} else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
196175801Siedowse		   NULL, 0, ninumeric) == 0) {
196275801Siedowse			name = netname;
196375801Siedowse		} else {
196475801Siedowse			goto fail;
196575801Siedowse		}
196675801Siedowse		if ((net->nt_name = strdup(name)) == NULL)
196775801Siedowse			out_of_mem();
196875801Siedowse
196975801Siedowse		/*
197075801Siedowse		 * Extract a mask from either a "/<masklen>" suffix, or
197175801Siedowse		 * from the class of an IPv4 address.
197275801Siedowse		 */
197374462Salfred		if (opt_flags & OP_MASKLEN) {
197474462Salfred			preflen = strtol(prefp, NULL, 10);
197575801Siedowse			if (preflen < 0L || preflen == LONG_MAX)
197674462Salfred				goto fail;
197775801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
197875801Siedowse			if (makemask(&net->nt_mask, (int)preflen) != 0)
197975801Siedowse				goto fail;
198075801Siedowse			opt_flags |= OP_HAVEMASK;
198174462Salfred			*p = '/';
198275801Siedowse		} else if (sa->sa_family == AF_INET &&
198375801Siedowse		    (opt_flags & OP_MASK) == 0) {
198475801Siedowse			in_addr_t addr;
198574462Salfred
198675801Siedowse			addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
198775801Siedowse			if (IN_CLASSA(addr))
198875801Siedowse				preflen = 8;
198975801Siedowse			else if (IN_CLASSB(addr))
199075801Siedowse				preflen = 16;
199175801Siedowse			else if (IN_CLASSC(addr))
199275801Siedowse				preflen = 24;
199375801Siedowse			else if (IN_CLASSD(addr))
199475801Siedowse				preflen = 28;
199575801Siedowse			else
199675801Siedowse				preflen = 32;	/* XXX */
199775801Siedowse
199875801Siedowse			bcopy(sa, &net->nt_mask, sa->sa_len);
199975801Siedowse			makemask(&net->nt_mask, (int)preflen);
200075801Siedowse			opt_flags |= OP_HAVEMASK;
200174462Salfred		}
200274462Salfred	}
200374462Salfred
200474462Salfred	if (ai)
200574462Salfred		freeaddrinfo(ai);
200674462Salfred	return 0;
200774462Salfred
200874462Salfredfail:
200974462Salfred	if (ai)
201074462Salfred		freeaddrinfo(ai);
201174462Salfred	return 1;
20121558Srgrimes}
20131558Srgrimes
20141558Srgrimes/*
20151558Srgrimes * Parse out the next white space separated field
20161558Srgrimes */
20171558Srgrimesvoid
20181558Srgrimesnextfield(cp, endcp)
20191558Srgrimes	char **cp;
20201558Srgrimes	char **endcp;
20211558Srgrimes{
20221558Srgrimes	char *p;
20231558Srgrimes
20241558Srgrimes	p = *cp;
20251558Srgrimes	while (*p == ' ' || *p == '\t')
20261558Srgrimes		p++;
20271558Srgrimes	if (*p == '\n' || *p == '\0')
20281558Srgrimes		*cp = *endcp = p;
20291558Srgrimes	else {
20301558Srgrimes		*cp = p++;
20311558Srgrimes		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
20321558Srgrimes			p++;
20331558Srgrimes		*endcp = p;
20341558Srgrimes	}
20351558Srgrimes}
20361558Srgrimes
20371558Srgrimes/*
20381558Srgrimes * Get an exports file line. Skip over blank lines and handle line
20391558Srgrimes * continuations.
20401558Srgrimes */
20411558Srgrimesint
20421558Srgrimesget_line()
20431558Srgrimes{
20441558Srgrimes	char *p, *cp;
204596622Siedowse	size_t len;
20461558Srgrimes	int totlen, cont_line;
20471558Srgrimes
20481558Srgrimes	/*
20491558Srgrimes	 * Loop around ignoring blank lines and getting all continuation lines.
20501558Srgrimes	 */
20511558Srgrimes	p = line;
20521558Srgrimes	totlen = 0;
20531558Srgrimes	do {
205496622Siedowse		if ((p = fgetln(exp_file, &len)) == NULL)
20551558Srgrimes			return (0);
20561558Srgrimes		cp = p + len - 1;
20571558Srgrimes		cont_line = 0;
20581558Srgrimes		while (cp >= p &&
20591558Srgrimes		    (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
20601558Srgrimes			if (*cp == '\\')
20611558Srgrimes				cont_line = 1;
20621558Srgrimes			cp--;
20631558Srgrimes			len--;
20641558Srgrimes		}
206579117Sdd		if (cont_line) {
206679117Sdd			*++cp = ' ';
206779117Sdd			len++;
206879117Sdd		}
206996622Siedowse		if (linesize < len + totlen + 1) {
207096622Siedowse			linesize = len + totlen + 1;
207196622Siedowse			line = realloc(line, linesize);
207296622Siedowse			if (line == NULL)
207396622Siedowse				out_of_mem();
20741558Srgrimes		}
207596622Siedowse		memcpy(line + totlen, p, len);
207696622Siedowse		totlen += len;
207796622Siedowse		line[totlen] = '\0';
20781558Srgrimes	} while (totlen == 0 || cont_line);
20791558Srgrimes	return (1);
20801558Srgrimes}
20811558Srgrimes
20821558Srgrimes/*
20831558Srgrimes * Parse a description of a credential.
20841558Srgrimes */
20851558Srgrimesvoid
20861558Srgrimesparsecred(namelist, cr)
20871558Srgrimes	char *namelist;
208872650Sgreen	struct xucred *cr;
20891558Srgrimes{
20901558Srgrimes	char *name;
20911558Srgrimes	int cnt;
20921558Srgrimes	char *names;
20931558Srgrimes	struct passwd *pw;
20941558Srgrimes	struct group *gr;
20951558Srgrimes	int ngroups, groups[NGROUPS + 1];
20961558Srgrimes
209791354Sdd	cr->cr_version = XUCRED_VERSION;
20981558Srgrimes	/*
209937663Scharnier	 * Set up the unprivileged user.
21001558Srgrimes	 */
21011558Srgrimes	cr->cr_uid = -2;
21021558Srgrimes	cr->cr_groups[0] = -2;
21031558Srgrimes	cr->cr_ngroups = 1;
21041558Srgrimes	/*
21051558Srgrimes	 * Get the user's password table entry.
21061558Srgrimes	 */
21071558Srgrimes	names = strsep(&namelist, " \t\n");
21081558Srgrimes	name = strsep(&names, ":");
21091558Srgrimes	if (isdigit(*name) || *name == '-')
21101558Srgrimes		pw = getpwuid(atoi(name));
21111558Srgrimes	else
21121558Srgrimes		pw = getpwnam(name);
21131558Srgrimes	/*
21141558Srgrimes	 * Credentials specified as those of a user.
21151558Srgrimes	 */
21161558Srgrimes	if (names == NULL) {
21171558Srgrimes		if (pw == NULL) {
211837663Scharnier			syslog(LOG_ERR, "unknown user: %s", name);
21191558Srgrimes			return;
21201558Srgrimes		}
21211558Srgrimes		cr->cr_uid = pw->pw_uid;
21221558Srgrimes		ngroups = NGROUPS + 1;
21231558Srgrimes		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
212437663Scharnier			syslog(LOG_ERR, "too many groups");
21251558Srgrimes		/*
21261558Srgrimes		 * Convert from int's to gid_t's and compress out duplicate
21271558Srgrimes		 */
21281558Srgrimes		cr->cr_ngroups = ngroups - 1;
21291558Srgrimes		cr->cr_groups[0] = groups[0];
21301558Srgrimes		for (cnt = 2; cnt < ngroups; cnt++)
21311558Srgrimes			cr->cr_groups[cnt - 1] = groups[cnt];
21321558Srgrimes		return;
21331558Srgrimes	}
21341558Srgrimes	/*
21351558Srgrimes	 * Explicit credential specified as a colon separated list:
21361558Srgrimes	 *	uid:gid:gid:...
21371558Srgrimes	 */
21381558Srgrimes	if (pw != NULL)
21391558Srgrimes		cr->cr_uid = pw->pw_uid;
21401558Srgrimes	else if (isdigit(*name) || *name == '-')
21411558Srgrimes		cr->cr_uid = atoi(name);
21421558Srgrimes	else {
214337663Scharnier		syslog(LOG_ERR, "unknown user: %s", name);
21441558Srgrimes		return;
21451558Srgrimes	}
21461558Srgrimes	cr->cr_ngroups = 0;
21471558Srgrimes	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
21481558Srgrimes		name = strsep(&names, ":");
21491558Srgrimes		if (isdigit(*name) || *name == '-') {
21501558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
21511558Srgrimes		} else {
21521558Srgrimes			if ((gr = getgrnam(name)) == NULL) {
215337663Scharnier				syslog(LOG_ERR, "unknown group: %s", name);
21541558Srgrimes				continue;
21551558Srgrimes			}
21561558Srgrimes			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
21571558Srgrimes		}
21581558Srgrimes	}
21591558Srgrimes	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
216037663Scharnier		syslog(LOG_ERR, "too many groups");
21611558Srgrimes}
21621558Srgrimes
21631558Srgrimes#define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
21641558Srgrimes/*
21651558Srgrimes * Routines that maintain the remote mounttab
21661558Srgrimes */
21671558Srgrimesvoid
21681558Srgrimesget_mountlist()
21691558Srgrimes{
21701558Srgrimes	struct mountlist *mlp, **mlpp;
217123681Speter	char *host, *dirp, *cp;
21721558Srgrimes	char str[STRSIZ];
21731558Srgrimes	FILE *mlfile;
21741558Srgrimes
21751558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
217653117Sbillf		if (errno == ENOENT)
217753117Sbillf			return;
217853117Sbillf		else {
217953117Sbillf			syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
218053117Sbillf			return;
218153117Sbillf		}
21821558Srgrimes	}
21831558Srgrimes	mlpp = &mlhead;
21841558Srgrimes	while (fgets(str, STRSIZ, mlfile) != NULL) {
218523681Speter		cp = str;
218623681Speter		host = strsep(&cp, " \t\n");
218723681Speter		dirp = strsep(&cp, " \t\n");
218823681Speter		if (host == NULL || dirp == NULL)
21891558Srgrimes			continue;
21901558Srgrimes		mlp = (struct mountlist *)malloc(sizeof (*mlp));
219137663Scharnier		if (mlp == (struct mountlist *)NULL)
219237663Scharnier			out_of_mem();
219323681Speter		strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
219423681Speter		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
219523681Speter		strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
219623681Speter		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
21971558Srgrimes		mlp->ml_next = (struct mountlist *)NULL;
21981558Srgrimes		*mlpp = mlp;
21991558Srgrimes		mlpp = &mlp->ml_next;
22001558Srgrimes	}
22011558Srgrimes	fclose(mlfile);
22021558Srgrimes}
22031558Srgrimes
220475635Siedowsevoid
220575635Siedowsedel_mlist(char *hostp, char *dirp)
22061558Srgrimes{
22071558Srgrimes	struct mountlist *mlp, **mlpp;
22081558Srgrimes	struct mountlist *mlp2;
22091558Srgrimes	FILE *mlfile;
22101558Srgrimes	int fnd = 0;
22111558Srgrimes
22121558Srgrimes	mlpp = &mlhead;
22131558Srgrimes	mlp = mlhead;
22141558Srgrimes	while (mlp) {
22151558Srgrimes		if (!strcmp(mlp->ml_host, hostp) &&
22161558Srgrimes		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
22171558Srgrimes			fnd = 1;
22181558Srgrimes			mlp2 = mlp;
22191558Srgrimes			*mlpp = mlp = mlp->ml_next;
22201558Srgrimes			free((caddr_t)mlp2);
22211558Srgrimes		} else {
22221558Srgrimes			mlpp = &mlp->ml_next;
22231558Srgrimes			mlp = mlp->ml_next;
22241558Srgrimes		}
22251558Srgrimes	}
22261558Srgrimes	if (fnd) {
22271558Srgrimes		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
222837663Scharnier			syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
22291558Srgrimes			return;
22301558Srgrimes		}
22311558Srgrimes		mlp = mlhead;
22321558Srgrimes		while (mlp) {
22331558Srgrimes			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22341558Srgrimes			mlp = mlp->ml_next;
22351558Srgrimes		}
22361558Srgrimes		fclose(mlfile);
22371558Srgrimes	}
22381558Srgrimes}
22391558Srgrimes
22401558Srgrimesvoid
22411558Srgrimesadd_mlist(hostp, dirp)
22421558Srgrimes	char *hostp, *dirp;
22431558Srgrimes{
22441558Srgrimes	struct mountlist *mlp, **mlpp;
22451558Srgrimes	FILE *mlfile;
22461558Srgrimes
22471558Srgrimes	mlpp = &mlhead;
22481558Srgrimes	mlp = mlhead;
22491558Srgrimes	while (mlp) {
22501558Srgrimes		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
22511558Srgrimes			return;
22521558Srgrimes		mlpp = &mlp->ml_next;
22531558Srgrimes		mlp = mlp->ml_next;
22541558Srgrimes	}
22551558Srgrimes	mlp = (struct mountlist *)malloc(sizeof (*mlp));
225637663Scharnier	if (mlp == (struct mountlist *)NULL)
225737663Scharnier		out_of_mem();
22581558Srgrimes	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
22591558Srgrimes	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
22601558Srgrimes	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
22611558Srgrimes	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
22621558Srgrimes	mlp->ml_next = (struct mountlist *)NULL;
22631558Srgrimes	*mlpp = mlp;
22641558Srgrimes	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
226537663Scharnier		syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
22661558Srgrimes		return;
22671558Srgrimes	}
22681558Srgrimes	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
22691558Srgrimes	fclose(mlfile);
22701558Srgrimes}
22711558Srgrimes
22721558Srgrimes/*
22731558Srgrimes * Free up a group list.
22741558Srgrimes */
22751558Srgrimesvoid
22761558Srgrimesfree_grp(grp)
22771558Srgrimes	struct grouplist *grp;
22781558Srgrimes{
22791558Srgrimes	if (grp->gr_type == GT_HOST) {
228074462Salfred		if (grp->gr_ptr.gt_addrinfo != NULL)
228174462Salfred			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
22821558Srgrimes	} else if (grp->gr_type == GT_NET) {
22831558Srgrimes		if (grp->gr_ptr.gt_net.nt_name)
22841558Srgrimes			free(grp->gr_ptr.gt_net.nt_name);
22851558Srgrimes	}
22861558Srgrimes	free((caddr_t)grp);
22871558Srgrimes}
22881558Srgrimes
22891558Srgrimes#ifdef DEBUG
22901558Srgrimesvoid
22911558SrgrimesSYSLOG(int pri, const char *fmt, ...)
22921558Srgrimes{
22931558Srgrimes	va_list ap;
22941558Srgrimes
22951558Srgrimes	va_start(ap, fmt);
22961558Srgrimes	vfprintf(stderr, fmt, ap);
22971558Srgrimes	va_end(ap);
22981558Srgrimes}
22991558Srgrimes#endif /* DEBUG */
23001558Srgrimes
23011558Srgrimes/*
23021558Srgrimes * Check options for consistency.
23031558Srgrimes */
23041558Srgrimesint
23051558Srgrimescheck_options(dp)
23061558Srgrimes	struct dirlist *dp;
23071558Srgrimes{
23081558Srgrimes
23091558Srgrimes	if (dp == (struct dirlist *)NULL)
23101558Srgrimes	    return (1);
231183653Speter	if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
231283653Speter	    syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
23131558Srgrimes	    return (1);
23141558Srgrimes	}
23151558Srgrimes	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
231675801Siedowse		syslog(LOG_ERR, "-mask requires -network");
231775801Siedowse		return (1);
23181558Srgrimes	}
231975801Siedowse	if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
232075801Siedowse		syslog(LOG_ERR, "-network requires mask specification");
232175801Siedowse		return (1);
232275801Siedowse	}
232375801Siedowse	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
232475801Siedowse		syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
232575801Siedowse		return (1);
232675801Siedowse	}
23271558Srgrimes	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
232845927Salex	    syslog(LOG_ERR, "-alldirs has multiple directories");
23291558Srgrimes	    return (1);
23301558Srgrimes	}
23311558Srgrimes	return (0);
23321558Srgrimes}
23331558Srgrimes
23341558Srgrimes/*
23351558Srgrimes * Check an absolute directory path for any symbolic links. Return true
23361558Srgrimes */
23371558Srgrimesint
23381558Srgrimescheck_dirpath(dirp)
23391558Srgrimes	char *dirp;
23401558Srgrimes{
23411558Srgrimes	char *cp;
23421558Srgrimes	int ret = 1;
23431558Srgrimes	struct stat sb;
23441558Srgrimes
23451558Srgrimes	cp = dirp + 1;
23461558Srgrimes	while (*cp && ret) {
23471558Srgrimes		if (*cp == '/') {
23481558Srgrimes			*cp = '\0';
23499336Sdfr			if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23501558Srgrimes				ret = 0;
23511558Srgrimes			*cp = '/';
23521558Srgrimes		}
23531558Srgrimes		cp++;
23541558Srgrimes	}
23559336Sdfr	if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
23561558Srgrimes		ret = 0;
23571558Srgrimes	return (ret);
23581558Srgrimes}
23599336Sdfr
236075801Siedowse/*
236175801Siedowse * Make a netmask according to the specified prefix length. The ss_family
236275801Siedowse * and other non-address fields must be initialised before calling this.
236375801Siedowse */
236475801Siedowseint
236575801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen)
236674462Salfred{
236775801Siedowse	u_char *p;
236875801Siedowse	int bits, i, len;
236974462Salfred
237075801Siedowse	if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
237175801Siedowse		return (-1);
2372103949Smike	if (bitlen > len * CHAR_BIT)
237375801Siedowse		return (-1);
237474462Salfred
237575801Siedowse	for (i = 0; i < len; i++) {
2376103949Smike		bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
237775801Siedowse		*p++ = (1 << bits) - 1;
237875801Siedowse		bitlen -= bits;
237974462Salfred	}
238075801Siedowse	return 0;
238174462Salfred}
238274462Salfred
238375801Siedowse/*
238475801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask
238575801Siedowse * is acceptable (i.e. of the form 1...10....0).
238675801Siedowse */
238775801Siedowseint
238875801Siedowsecheckmask(struct sockaddr *sa)
238974462Salfred{
239075801Siedowse	u_char *mask;
239175801Siedowse	int i, len;
239274462Salfred
239375801Siedowse	if ((mask = sa_rawaddr(sa, &len)) == NULL)
239475801Siedowse		return (-1);
239575801Siedowse
239675801Siedowse	for (i = 0; i < len; i++)
239775801Siedowse		if (mask[i] != 0xff)
239875801Siedowse			break;
239975801Siedowse	if (i < len) {
240075801Siedowse		if (~mask[i] & (u_char)(~mask[i] + 1))
240175801Siedowse			return (-1);
240275801Siedowse		i++;
240374462Salfred	}
240475801Siedowse	for (; i < len; i++)
240575801Siedowse		if (mask[i] != 0)
240675801Siedowse			return (-1);
240775801Siedowse	return (0);
240874462Salfred}
240974462Salfred
241075801Siedowse/*
241175801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if
241275801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'.
241375801Siedowse * If samask is NULL, perform a full comparision.
241475801Siedowse */
241575801Siedowseint
241675801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
241774462Salfred{
241875801Siedowse	unsigned char *p1, *p2, *mask;
241975801Siedowse	int len, i;
242074462Salfred
242175801Siedowse	if (sa1->sa_family != sa2->sa_family ||
242275801Siedowse	    (p1 = sa_rawaddr(sa1, &len)) == NULL ||
242375801Siedowse	    (p2 = sa_rawaddr(sa2, NULL)) == NULL)
242475801Siedowse		return (1);
242575801Siedowse
242675801Siedowse	switch (sa1->sa_family) {
242774462Salfred	case AF_INET6:
242875801Siedowse		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
242975801Siedowse		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
243075801Siedowse			return (1);
243174462Salfred		break;
243274462Salfred	}
243374462Salfred
243475801Siedowse	/* Simple binary comparison if no mask specified. */
243575801Siedowse	if (samask == NULL)
243675801Siedowse		return (memcmp(p1, p2, len));
243774462Salfred
243875801Siedowse	/* Set up the mask, and do a mask-based comparison. */
243975801Siedowse	if (sa1->sa_family != samask->sa_family ||
244075801Siedowse	    (mask = sa_rawaddr(samask, NULL)) == NULL)
244175801Siedowse		return (1);
244274462Salfred
244375801Siedowse	for (i = 0; i < len; i++)
244475801Siedowse		if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
244575801Siedowse			return (1);
244675801Siedowse	return (0);
244774462Salfred}
244874462Salfred
244975801Siedowse/*
245075801Siedowse * Return a pointer to the part of the sockaddr that contains the
245175801Siedowse * raw address, and set *nbytes to its length in bytes. Returns
245275801Siedowse * NULL if the address family is unknown.
245375801Siedowse */
245475801Siedowsevoid *
245575801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) {
245675801Siedowse	void *p;
245774462Salfred	int len;
245874462Salfred
245975801Siedowse	switch (sa->sa_family) {
246074462Salfred	case AF_INET:
246175801Siedowse		len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
246275801Siedowse		p = &((struct sockaddr_in *)sa)->sin_addr;
246374462Salfred		break;
246474462Salfred	case AF_INET6:
246575801Siedowse		len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
246675801Siedowse		p = &((struct sockaddr_in6 *)sa)->sin6_addr;
246774462Salfred		break;
246874462Salfred	default:
246975801Siedowse		p = NULL;
247075801Siedowse		len = 0;
247174462Salfred	}
247274462Salfred
247375801Siedowse	if (nbytes != NULL)
247475801Siedowse		*nbytes = len;
247575801Siedowse	return (p);
247674462Salfred}
247774462Salfred
247875754Siedowsevoid
247975754Siedowsehuphandler(int sig)
248075754Siedowse{
248175754Siedowse	got_sighup = 1;
248275754Siedowse}
248375754Siedowse
248474462Salfredvoid terminate(sig)
248574462Salfredint sig;
248674462Salfred{
248774462Salfred	close(mountdlockfd);
248874462Salfred	unlink(MOUNTDLOCK);
248974792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
249074792Salfred	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
249174462Salfred	exit (0);
249274462Salfred}
2493