Home | History | Annotate | Line # | Download | only in mountd
mountd.c revision 1.115
      1 /* 	$NetBSD: mountd.c,v 1.115 2008/02/09 19:15:59 dholland Exp $	 */
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Herb Hasler and Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 
     36 /*
     37  * XXX The ISO support can't possibly work..
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef lint
     42 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
     43 	The Regents of the University of California.  All rights reserved.\n");
     44 #endif				/* not lint */
     45 
     46 #ifndef lint
     47 #if 0
     48 static char     sccsid[] = "@(#)mountd.c  8.15 (Berkeley) 5/1/95";
     49 #else
     50 __RCSID("$NetBSD: mountd.c,v 1.115 2008/02/09 19:15:59 dholland Exp $");
     51 #endif
     52 #endif				/* not lint */
     53 
     54 #include <sys/param.h>
     55 #include <sys/file.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/mount.h>
     58 #include <sys/socket.h>
     59 #include <sys/stat.h>
     60 #include <syslog.h>
     61 #include <sys/ucred.h>
     62 
     63 #include <rpc/rpc.h>
     64 #include <rpc/pmap_clnt.h>
     65 #include <rpc/pmap_prot.h>
     66 #include <rpcsvc/mount.h>
     67 #ifdef ISO
     68 #include <netiso/iso.h>
     69 #endif
     70 #include <nfs/rpcv2.h>
     71 #include <nfs/nfsproto.h>
     72 #include <nfs/nfs.h>
     73 #include <nfs/nfsmount.h>
     74 
     75 #include <arpa/inet.h>
     76 
     77 #include <ctype.h>
     78 #include <errno.h>
     79 #include <grp.h>
     80 #include <netdb.h>
     81 #include <pwd.h>
     82 #include <netgroup.h>
     83 #include <signal.h>
     84 #include <stdio.h>
     85 #include <stdlib.h>
     86 #include <string.h>
     87 #include <unistd.h>
     88 #include <err.h>
     89 #include <util.h>
     90 #include "pathnames.h"
     91 
     92 #ifdef IPSEC
     93 #include <netinet6/ipsec.h>
     94 #ifndef IPSEC_POLICY_IPSEC	/* no ipsec support on old ipsec */
     95 #undef IPSEC
     96 #endif
     97 #include "ipsec.h"
     98 #endif
     99 
    100 #include <stdarg.h>
    101 
    102 /*
    103  * Structures for keeping the mount list and export list
    104  */
    105 struct mountlist {
    106 	struct mountlist *ml_next;
    107 	char ml_host[RPCMNT_NAMELEN + 1];
    108 	char ml_dirp[RPCMNT_PATHLEN + 1];
    109 	int ml_flag;/* XXX more flags (same as dp_flag) */
    110 };
    111 
    112 struct dirlist {
    113 	struct dirlist *dp_left;
    114 	struct dirlist *dp_right;
    115 	int dp_flag;
    116 	struct hostlist *dp_hosts;	/* List of hosts this dir exported to */
    117 	char dp_dirp[1];		/* Actually malloc'd to size of dir */
    118 };
    119 /* dp_flag bits */
    120 #define	DP_DEFSET	0x1
    121 #define DP_HOSTSET	0x2
    122 #define DP_KERB		0x4
    123 #define DP_NORESMNT	0x8
    124 
    125 struct exportlist {
    126 	struct exportlist *ex_next;
    127 	struct dirlist *ex_dirl;
    128 	struct dirlist *ex_defdir;
    129 	int             ex_flag;
    130 	fsid_t          ex_fs;
    131 	char           *ex_fsdir;
    132 	char           *ex_indexfile;
    133 };
    134 /* ex_flag bits */
    135 #define	EX_LINKED	0x1
    136 
    137 struct netmsk {
    138 	struct sockaddr_storage nt_net;
    139 	int		nt_len;
    140 	char           *nt_name;
    141 };
    142 
    143 union grouptypes {
    144 	struct addrinfo *gt_addrinfo;
    145 	struct netmsk   gt_net;
    146 #ifdef ISO
    147 	struct sockaddr_iso *gt_isoaddr;
    148 #endif
    149 };
    150 
    151 struct grouplist {
    152 	int             gr_type;
    153 	union grouptypes gr_ptr;
    154 	struct grouplist *gr_next;
    155 };
    156 /* Group types */
    157 #define	GT_NULL		0x0
    158 #define	GT_HOST		0x1
    159 #define	GT_NET		0x2
    160 #define	GT_ISO		0x4
    161 
    162 struct hostlist {
    163 	int             ht_flag;/* Uses DP_xx bits */
    164 	struct grouplist *ht_grp;
    165 	struct hostlist *ht_next;
    166 };
    167 
    168 struct fhreturn {
    169 	int             fhr_flag;
    170 	int             fhr_vers;
    171 	size_t		fhr_fhsize;
    172 	union {
    173 		uint8_t v2[NFSX_V2FH];
    174 		uint8_t v3[NFSX_V3FHMAX];
    175 	} fhr_fh;
    176 };
    177 
    178 /* Global defs */
    179 static char    *add_expdir __P((struct dirlist **, char *, int));
    180 static void add_dlist __P((struct dirlist **, struct dirlist *,
    181     struct grouplist *, int));
    182 static void add_mlist __P((char *, char *, int));
    183 static int check_dirpath __P((const char *, size_t, char *));
    184 static int check_options __P((const char *, size_t, struct dirlist *));
    185 static int chk_host __P((struct dirlist *, struct sockaddr *, int *, int *));
    186 static int del_mlist __P((char *, char *, struct sockaddr *));
    187 static struct dirlist *dirp_search __P((struct dirlist *, char *));
    188 static int do_nfssvc __P((const char *, size_t, struct exportlist *,
    189     struct grouplist *, int, struct uucred *, char *, int, struct statvfs *));
    190 static int do_opt __P((const char *, size_t, char **, char **,
    191     struct exportlist *, struct grouplist *, int *, int *, struct uucred *));
    192 static struct exportlist *ex_search __P((fsid_t *));
    193 static int parse_directory __P((const char *, size_t, struct grouplist *,
    194     int, char *, struct exportlist **, struct statvfs *));
    195 static int parse_host_netgroup __P((const char *, size_t, struct exportlist *,
    196     struct grouplist *, char *, int *, struct grouplist **));
    197 static struct exportlist *get_exp __P((void));
    198 static void free_dir __P((struct dirlist *));
    199 static void free_exp __P((struct exportlist *));
    200 static void free_grp __P((struct grouplist *));
    201 static void free_host __P((struct hostlist *));
    202 static void get_exportlist __P((int));
    203 static int get_host __P((const char *, size_t, const char *,
    204     struct grouplist *));
    205 static struct hostlist *get_ht __P((void));
    206 static void get_mountlist __P((void));
    207 static int get_net __P((char *, struct netmsk *, int));
    208 static void free_exp_grp __P((struct exportlist *, struct grouplist *));
    209 static struct grouplist *get_grp __P((void));
    210 static void hang_dirp __P((struct dirlist *, struct grouplist *,
    211     struct exportlist *, int));
    212 static void mntsrv __P((struct svc_req *, SVCXPRT *));
    213 static void nextfield __P((char **, char **));
    214 static void parsecred __P((char *, struct uucred *));
    215 static int put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
    216 static int scan_tree __P((struct dirlist *, struct sockaddr *));
    217 static void send_umntall __P((int));
    218 static int umntall_each __P((caddr_t, struct sockaddr_in *));
    219 static int xdr_dir __P((XDR *, char *));
    220 static int xdr_explist __P((XDR *, caddr_t));
    221 static int xdr_fhs __P((XDR *, caddr_t));
    222 static int xdr_mlist __P((XDR *, caddr_t));
    223 static int bitcmp __P((void *, void *, int));
    224 static int netpartcmp __P((struct sockaddr *, struct sockaddr *, int));
    225 static int sacmp __P((struct sockaddr *, struct sockaddr *));
    226 static int allones __P((struct sockaddr_storage *, int));
    227 static int countones __P((struct sockaddr *));
    228 #ifdef ISO
    229 static int get_isoaddr __P((const char *, size_t, char *, struct grouplist *));
    230 #endif
    231 static void bind_resv_port __P((int, sa_family_t, in_port_t));
    232 static void no_nfs(int);
    233 static struct exportlist *exphead;
    234 static struct mountlist *mlhead;
    235 static struct grouplist *grphead;
    236 static char    *exname;
    237 static struct uucred def_anon = {
    238 	1,
    239 	(uid_t) -2,
    240 	(gid_t) -2,
    241 	0,
    242 	{}
    243 };
    244 
    245 static int      opt_flags;
    246 static int	have_v6 = 1;
    247 static const int ninumeric = NI_NUMERICHOST;
    248 
    249 /* Bits for above */
    250 #define	OP_MAPROOT	0x001
    251 #define	OP_MAPALL	0x002
    252 #define	OP_KERB		0x004
    253 #define	OP_MASK		0x008
    254 #define	OP_NET		0x010
    255 #define	OP_ISO		0x020
    256 #define	OP_ALLDIRS	0x040
    257 #define OP_NORESPORT	0x080
    258 #define OP_NORESMNT	0x100
    259 #define OP_MASKLEN	0x200
    260 
    261 static int      debug = 0;
    262 #if 0
    263 static void SYSLOG __P((int, const char *,...));
    264 #endif
    265 int main __P((int, char *[]));
    266 
    267 /*
    268  * If this is non-zero, -noresvport and -noresvmnt are implied for
    269  * each export.
    270  */
    271 static int noprivports;
    272 
    273 /*
    274  * Mountd server for NFS mount protocol as described in:
    275  * NFS: Network File System Protocol Specification, RFC1094, Appendix A
    276  * The optional arguments are the exports file name
    277  * default: _PATH_EXPORTS
    278  * "-d" to enable debugging
    279  * and "-n" to allow nonroot mount.
    280  */
    281 int
    282 main(argc, argv)
    283 	int argc;
    284 	char **argv;
    285 {
    286 	SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
    287 	struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
    288 	int udpsock, tcpsock, udp6sock, tcp6sock;
    289 	int xcreated = 0, s;
    290 	int c, one = 1;
    291 	int maxrec = RPC_MAXDATASIZE;
    292 	in_port_t forcedport = 0;
    293 #ifdef IPSEC
    294 	char *policy = NULL;
    295 #define ADDOPTS "P:"
    296 #else
    297 #define ADDOPTS
    298 #endif
    299 
    300 	while ((c = getopt(argc, argv, "dNnrp:" ADDOPTS)) != -1)
    301 		switch (c) {
    302 #ifdef IPSEC
    303 		case 'P':
    304 			if (ipsecsetup_test(policy = optarg))
    305 				errx(1, "Invalid ipsec policy `%s'", policy);
    306 			break;
    307 #endif
    308 		case 'p':
    309 			/* A forced port "0" will dynamically allocate a port */
    310 			forcedport = atoi(optarg);
    311 			break;
    312 		case 'd':
    313 			debug = 1;
    314 			break;
    315 		case 'N':
    316 			noprivports = 1;
    317 			break;
    318 			/* Compatibility */
    319 		case 'n':
    320 		case 'r':
    321 			break;
    322 		default:
    323 			fprintf(stderr, "usage: %s [-dNn]"
    324 #ifdef IPSEC
    325 			    " [-P policy]"
    326 #endif
    327 			    " [-p port] [exportsfile]\n", getprogname());
    328 			exit(1);
    329 		};
    330 	argc -= optind;
    331 	argv += optind;
    332 	grphead = NULL;
    333 	exphead = NULL;
    334 	mlhead = NULL;
    335 	if (argc == 1)
    336 		exname = *argv;
    337 	else
    338 		exname = _PATH_EXPORTS;
    339 	openlog("mountd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
    340 	(void)signal(SIGSYS, no_nfs);
    341 
    342 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    343 	if (s < 0)
    344 		have_v6 = 0;
    345 	else
    346 		close(s);
    347 
    348 	if (debug)
    349 		(void)fprintf(stderr, "Getting export list.\n");
    350 	get_exportlist(0);
    351 	if (debug)
    352 		(void)fprintf(stderr, "Getting mount list.\n");
    353 	get_mountlist();
    354 	if (debug)
    355 		(void)fprintf(stderr, "Here we go.\n");
    356 	if (debug == 0) {
    357 		daemon(0, 0);
    358 		(void)signal(SIGINT, SIG_IGN);
    359 		(void)signal(SIGQUIT, SIG_IGN);
    360 	}
    361 	(void)signal(SIGHUP, get_exportlist);
    362 	(void)signal(SIGTERM, send_umntall);
    363 	pidfile(NULL);
    364 
    365 	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
    366 	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
    367 
    368 	udpsock  = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    369 	tcpsock  = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    370 	udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    371 	tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
    372 
    373 	/*
    374 	 * We're doing host-based access checks here, so don't allow
    375 	 * v4-in-v6 to confuse things. The kernel will disable it
    376 	 * by default on NFS sockets too.
    377 	 */
    378 	if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
    379 	    IPV6_V6ONLY, &one, sizeof one) < 0){
    380 		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
    381 		exit(1);
    382 	}
    383 	if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
    384 	    IPV6_V6ONLY, &one, sizeof one) < 0){
    385 		syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
    386 		exit(1);
    387 	}
    388 
    389 	udpconf  = getnetconfigent("udp");
    390 	tcpconf  = getnetconfigent("tcp");
    391 	udp6conf = getnetconfigent("udp6");
    392 	tcp6conf = getnetconfigent("tcp6");
    393 
    394 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
    395 
    396 	if (udpsock != -1 && udpconf != NULL) {
    397 		bind_resv_port(udpsock, AF_INET, forcedport);
    398 #ifdef IPSEC
    399 		if (policy)
    400 			ipsecsetup(AF_INET, udpsock, policy);
    401 #endif
    402 		udptransp = svc_dg_create(udpsock, 0, 0);
    403 		if (udptransp != NULL) {
    404 			if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
    405 				mntsrv, udpconf) ||
    406 			    !svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
    407 				mntsrv, udpconf))
    408 				syslog(LOG_WARNING, "can't register UDP service");
    409 			else
    410 				xcreated++;
    411 		} else
    412 			syslog(LOG_WARNING, "can't create UDP service");
    413 
    414 	}
    415 
    416 	if (tcpsock != -1 && tcpconf != NULL) {
    417 		bind_resv_port(tcpsock, AF_INET, forcedport);
    418 #ifdef IPSEC
    419 		if (policy)
    420 			ipsecsetup(AF_INET, tcpsock, policy);
    421 #endif
    422 		listen(tcpsock, SOMAXCONN);
    423 		tcptransp = svc_vc_create(tcpsock, RPC_MAXDATASIZE,
    424 		    RPC_MAXDATASIZE);
    425 		if (tcptransp != NULL) {
    426 			if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
    427 				mntsrv, tcpconf) ||
    428 			    !svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
    429 				mntsrv, tcpconf))
    430 				syslog(LOG_WARNING, "can't register TCP service");
    431 			else
    432 				xcreated++;
    433 		} else
    434 			syslog(LOG_WARNING, "can't create TCP service");
    435 
    436 	}
    437 
    438 	if (udp6sock != -1 && udp6conf != NULL) {
    439 		bind_resv_port(udp6sock, AF_INET6, forcedport);
    440 #ifdef IPSEC
    441 		if (policy)
    442 			ipsecsetup(AF_INET6, tcpsock, policy);
    443 #endif
    444 		udp6transp = svc_dg_create(udp6sock, 0, 0);
    445 		if (udp6transp != NULL) {
    446 			if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
    447 				mntsrv, udp6conf) ||
    448 			    !svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
    449 				mntsrv, udp6conf))
    450 				syslog(LOG_WARNING, "can't register UDP6 service");
    451 			else
    452 				xcreated++;
    453 		} else
    454 			syslog(LOG_WARNING, "can't create UDP6 service");
    455 
    456 	}
    457 
    458 	if (tcp6sock != -1 && tcp6conf != NULL) {
    459 		bind_resv_port(tcp6sock, AF_INET6, forcedport);
    460 #ifdef IPSEC
    461 		if (policy)
    462 			ipsecsetup(AF_INET6, tcpsock, policy);
    463 #endif
    464 		listen(tcp6sock, SOMAXCONN);
    465 		tcp6transp = svc_vc_create(tcp6sock, RPC_MAXDATASIZE,
    466 		    RPC_MAXDATASIZE);
    467 		if (tcp6transp != NULL) {
    468 			if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
    469 				mntsrv, tcp6conf) ||
    470 			    !svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
    471 				mntsrv, tcp6conf))
    472 				syslog(LOG_WARNING, "can't register TCP6 service");
    473 			else
    474 				xcreated++;
    475 		} else
    476 			syslog(LOG_WARNING, "can't create TCP6 service");
    477 
    478 	}
    479 
    480 	if (xcreated == 0) {
    481 		syslog(LOG_ERR, "could not create any services");
    482 		exit(1);
    483 	}
    484 
    485 	svc_run();
    486 	syslog(LOG_ERR, "Mountd died");
    487 	exit(1);
    488 }
    489 
    490 /*
    491  * The mount rpc service
    492  */
    493 void
    494 mntsrv(rqstp, transp)
    495 	struct svc_req *rqstp;
    496 	SVCXPRT *transp;
    497 {
    498 	struct exportlist *ep;
    499 	struct dirlist *dp;
    500 	struct fhreturn fhr;
    501 	struct stat     stb;
    502 	struct statvfs   fsb;
    503 	struct addrinfo *ai;
    504 	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
    505 	int lookup_failed = 1;
    506 	struct sockaddr *saddr;
    507 	u_short         sport;
    508 	char            rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
    509 	long            bad = EACCES;
    510 	int             defset, hostset, ret;
    511 	sigset_t        sighup_mask;
    512 	struct sockaddr_in6 *sin6;
    513 	struct sockaddr_in *sin;
    514 	size_t fh_size;
    515 
    516 	(void)sigemptyset(&sighup_mask);
    517 	(void)sigaddset(&sighup_mask, SIGHUP);
    518 	saddr = svc_getrpccaller(transp)->buf;
    519 	switch (saddr->sa_family) {
    520 	case AF_INET6:
    521 		sin6 = (struct sockaddr_in6 *)saddr;
    522 		sport = ntohs(sin6->sin6_port);
    523 		break;
    524 	case AF_INET:
    525 		sin = (struct sockaddr_in *)saddr;
    526 		sport = ntohs(sin->sin_port);
    527 		break;
    528 	default:
    529 		syslog(LOG_ERR, "request from unknown address family");
    530 		return;
    531 	}
    532 	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
    533 	    NULL, 0, 0);
    534 	if (getnameinfo(saddr, saddr->sa_len, numerichost,
    535 	    sizeof numerichost, NULL, 0, ninumeric) != 0)
    536 		strlcpy(numerichost, "?", sizeof(numerichost));
    537 	ai = NULL;
    538 	ret = 0;
    539 	switch (rqstp->rq_proc) {
    540 	case NULLPROC:
    541 		if (!svc_sendreply(transp, xdr_void, NULL))
    542 			syslog(LOG_ERR, "Can't send reply");
    543 		return;
    544 	case MOUNTPROC_MNT:
    545 		if (debug)
    546 			fprintf(stderr,
    547 			    "got mount request from %s\n", numerichost);
    548 		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
    549 			if (debug)
    550 				fprintf(stderr, "-> garbage args\n");
    551 			svcerr_decode(transp);
    552 			return;
    553 		}
    554 		if (debug)
    555 			fprintf(stderr,
    556 			    "-> rpcpath: %s\n", rpcpath);
    557 		/*
    558 		 * Get the real pathname and make sure it is a file or
    559 		 * directory that exists.
    560 		 */
    561 		if (realpath(rpcpath, dirpath) == 0 ||
    562 		    stat(dirpath, &stb) < 0 ||
    563 		    (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) ||
    564 		    statvfs(dirpath, &fsb) < 0) {
    565 			(void)chdir("/"); /* Just in case realpath doesn't */
    566 			if (debug)
    567 				(void)fprintf(stderr, "-> stat failed on %s\n",
    568 				    dirpath);
    569 			if (!svc_sendreply(transp, xdr_long, (caddr_t) &bad))
    570 				syslog(LOG_ERR, "Can't send reply");
    571 			return;
    572 		}
    573 		if (debug)
    574 			fprintf(stderr,
    575 			    "-> dirpath: %s\n", dirpath);
    576 		/* Check in the exports list */
    577 		(void)sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
    578 		ep = ex_search(&fsb.f_fsidx);
    579 		hostset = defset = 0;
    580 		if (ep && (chk_host(ep->ex_defdir, saddr, &defset,
    581 		   &hostset) || ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
    582 		   chk_host(dp, saddr, &defset, &hostset)) ||
    583 		   (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
    584 		   scan_tree(ep->ex_dirl, saddr) == 0))) {
    585 			if ((hostset & DP_HOSTSET) == 0) {
    586 				hostset = defset;
    587 			}
    588 			if (sport >= IPPORT_RESERVED &&
    589 			    !(hostset & DP_NORESMNT)) {
    590 				syslog(LOG_NOTICE,
    591 				    "Refused mount RPC from host %s port %d",
    592 				    numerichost, sport);
    593 				svcerr_weakauth(transp);
    594 				goto out;
    595 			}
    596 			fhr.fhr_flag = hostset;
    597 			fhr.fhr_vers = rqstp->rq_vers;
    598 			/* Get the file handle */
    599 			memset(&fhr.fhr_fh, 0, sizeof(fhr.fhr_fh)); /* for v2 */
    600 			fh_size = sizeof(fhr.fhr_fh);
    601 			if (getfh(dirpath, &fhr.fhr_fh, &fh_size) < 0) {
    602 				bad = errno;
    603 				syslog(LOG_ERR, "Can't get fh for %s", dirpath);
    604 				if (!svc_sendreply(transp, xdr_long,
    605 				    (char *)&bad))
    606 					syslog(LOG_ERR, "Can't send reply");
    607 				goto out;
    608 			}
    609 			if ((fhr.fhr_vers == 1 && fh_size > NFSX_V2FH) ||
    610 			    fh_size > NFSX_V3FHMAX) {
    611 				bad = EINVAL; /* XXX */
    612 				if (!svc_sendreply(transp, xdr_long,
    613 				    (char *)&bad))
    614 					syslog(LOG_ERR, "Can't send reply");
    615 				goto out;
    616 			}
    617 			fhr.fhr_fhsize = fh_size;
    618 			if (!svc_sendreply(transp, xdr_fhs, (char *) &fhr))
    619 				syslog(LOG_ERR, "Can't send reply");
    620 			if (!lookup_failed)
    621 				add_mlist(host, dirpath, hostset);
    622 			else
    623 				add_mlist(numerichost, dirpath, hostset);
    624 			if (debug)
    625 				(void)fprintf(stderr, "Mount successful.\n");
    626 		} else {
    627 			if (!svc_sendreply(transp, xdr_long, (caddr_t) &bad))
    628 				syslog(LOG_ERR, "Can't send reply");
    629 		}
    630 out:
    631 		(void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
    632 		return;
    633 	case MOUNTPROC_DUMP:
    634 		if (!svc_sendreply(transp, xdr_mlist, NULL))
    635 			syslog(LOG_ERR, "Can't send reply");
    636 		return;
    637 	case MOUNTPROC_UMNT:
    638 		if (!svc_getargs(transp, xdr_dir, dirpath)) {
    639 			svcerr_decode(transp);
    640 			return;
    641 		}
    642 		if (!lookup_failed)
    643 			ret = del_mlist(host, dirpath, saddr);
    644 		ret |= del_mlist(numerichost, dirpath, saddr);
    645 		if (ret) {
    646 			svcerr_weakauth(transp);
    647 			return;
    648 		}
    649 		if (!svc_sendreply(transp, xdr_void, NULL))
    650 			syslog(LOG_ERR, "Can't send reply");
    651 		return;
    652 	case MOUNTPROC_UMNTALL:
    653 		if (!lookup_failed)
    654 			ret = del_mlist(host, NULL, saddr);
    655 		ret |= del_mlist(numerichost, NULL, saddr);
    656 		if (ret) {
    657 			svcerr_weakauth(transp);
    658 			return;
    659 		}
    660 		if (!svc_sendreply(transp, xdr_void, NULL))
    661 			syslog(LOG_ERR, "Can't send reply");
    662 		return;
    663 	case MOUNTPROC_EXPORT:
    664 	case MOUNTPROC_EXPORTALL:
    665 		if (!svc_sendreply(transp, xdr_explist, NULL))
    666 			syslog(LOG_ERR, "Can't send reply");
    667 		return;
    668 
    669 
    670 	default:
    671 		svcerr_noproc(transp);
    672 		return;
    673 	}
    674 }
    675 
    676 /*
    677  * Xdr conversion for a dirpath string
    678  */
    679 static int
    680 xdr_dir(xdrsp, dirp)
    681 	XDR *xdrsp;
    682 	char *dirp;
    683 {
    684 
    685 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    686 }
    687 
    688 /*
    689  * Xdr routine to generate file handle reply
    690  */
    691 static int
    692 xdr_fhs(xdrsp, cp)
    693 	XDR *xdrsp;
    694 	caddr_t cp;
    695 {
    696 	struct fhreturn *fhrp = (struct fhreturn *) cp;
    697 	long ok = 0, len, auth;
    698 
    699 	if (!xdr_long(xdrsp, &ok))
    700 		return (0);
    701 	switch (fhrp->fhr_vers) {
    702 	case 1:
    703 		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
    704 	case 3:
    705 		len = fhrp->fhr_fhsize;
    706 		if (!xdr_long(xdrsp, &len))
    707 			return (0);
    708 		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
    709 			return (0);
    710 		if (fhrp->fhr_flag & DP_KERB)
    711 			auth = RPCAUTH_KERB4;
    712 		else
    713 			auth = RPCAUTH_UNIX;
    714 		len = 1;
    715 		if (!xdr_long(xdrsp, &len))
    716 			return (0);
    717 		return (xdr_long(xdrsp, &auth));
    718 	};
    719 	return (0);
    720 }
    721 
    722 int
    723 xdr_mlist(xdrsp, cp)
    724 	XDR *xdrsp;
    725 	caddr_t cp;
    726 {
    727 	struct mountlist *mlp;
    728 	int true = 1;
    729 	int false = 0;
    730 	char *strp;
    731 
    732 	mlp = mlhead;
    733 	while (mlp) {
    734 		if (!xdr_bool(xdrsp, &true))
    735 			return (0);
    736 		strp = &mlp->ml_host[0];
    737 		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
    738 			return (0);
    739 		strp = &mlp->ml_dirp[0];
    740 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
    741 			return (0);
    742 		mlp = mlp->ml_next;
    743 	}
    744 	if (!xdr_bool(xdrsp, &false))
    745 		return (0);
    746 	return (1);
    747 }
    748 
    749 /*
    750  * Xdr conversion for export list
    751  */
    752 int
    753 xdr_explist(xdrsp, cp)
    754 	XDR *xdrsp;
    755 	caddr_t cp;
    756 {
    757 	struct exportlist *ep;
    758 	int false = 0;
    759 	int putdef;
    760 	sigset_t sighup_mask;
    761 
    762 	(void)sigemptyset(&sighup_mask);
    763 	(void)sigaddset(&sighup_mask, SIGHUP);
    764 	(void)sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
    765 	ep = exphead;
    766 	while (ep) {
    767 		putdef = 0;
    768 		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
    769 			goto errout;
    770 		if (ep->ex_defdir && putdef == 0 &&
    771 		    put_exlist(ep->ex_defdir, xdrsp, NULL, &putdef))
    772 			goto errout;
    773 		ep = ep->ex_next;
    774 	}
    775 	(void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
    776 	if (!xdr_bool(xdrsp, &false))
    777 		return (0);
    778 	return (1);
    779 errout:
    780 	(void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
    781 	return (0);
    782 }
    783 
    784 /*
    785  * Called from xdr_explist() to traverse the tree and export the
    786  * directory paths.  Assumes SIGHUP has already been masked.
    787  */
    788 int
    789 put_exlist(dp, xdrsp, adp, putdefp)
    790 	struct dirlist *dp;
    791 	XDR *xdrsp;
    792 	struct dirlist *adp;
    793 	int *putdefp;
    794 {
    795 	struct grouplist *grp;
    796 	struct hostlist *hp;
    797 	int true = 1;
    798 	int false = 0;
    799 	int gotalldir = 0;
    800 	char *strp;
    801 
    802 	if (dp) {
    803 		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
    804 			return (1);
    805 		if (!xdr_bool(xdrsp, &true))
    806 			return (1);
    807 		strp = dp->dp_dirp;
    808 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
    809 			return (1);
    810 		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
    811 			gotalldir = 1;
    812 			*putdefp = 1;
    813 		}
    814 		if ((dp->dp_flag & DP_DEFSET) == 0 &&
    815 		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
    816 			hp = dp->dp_hosts;
    817 			while (hp) {
    818 				grp = hp->ht_grp;
    819 				if (grp->gr_type == GT_HOST) {
    820 					if (!xdr_bool(xdrsp, &true))
    821 						return (1);
    822 					strp =
    823 					  grp->gr_ptr.gt_addrinfo->ai_canonname;
    824 					if (!xdr_string(xdrsp, &strp,
    825 							RPCMNT_NAMELEN))
    826 						return (1);
    827 				} else if (grp->gr_type == GT_NET) {
    828 					if (!xdr_bool(xdrsp, &true))
    829 						return (1);
    830 					strp = grp->gr_ptr.gt_net.nt_name;
    831 					if (!xdr_string(xdrsp, &strp,
    832 							RPCMNT_NAMELEN))
    833 						return (1);
    834 				}
    835 				hp = hp->ht_next;
    836 				if (gotalldir && hp == NULL) {
    837 					hp = adp->dp_hosts;
    838 					gotalldir = 0;
    839 				}
    840 			}
    841 		}
    842 		if (!xdr_bool(xdrsp, &false))
    843 			return (1);
    844 		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
    845 			return (1);
    846 	}
    847 	return (0);
    848 }
    849 
    850 static int
    851 parse_host_netgroup(line, lineno, ep, tgrp, cp, has_host, grp)
    852 	const char *line;
    853 	size_t lineno;
    854 	struct exportlist *ep;
    855 	struct grouplist *tgrp;
    856 	char *cp;
    857 	int *has_host;
    858 	struct grouplist **grp;
    859 {
    860 	const char *hst, *usr, *dom;
    861 	int netgrp;
    862 
    863 	if (ep == NULL) {
    864 		syslog(LOG_ERR, "\"%s\", line %ld: No current export",
    865 		    line, (unsigned long)lineno);
    866 		return 0;
    867 	}
    868 	setnetgrent(cp);
    869 	netgrp = getnetgrent(&hst, &usr, &dom);
    870 	do {
    871 		if (*has_host) {
    872 			(*grp)->gr_next = get_grp();
    873 			*grp = (*grp)->gr_next;
    874 		}
    875 		if (netgrp) {
    876 			if (hst == NULL) {
    877 				syslog(LOG_ERR,
    878 				    "\"%s\", line %ld: No host in netgroup %s",
    879 				    line, (unsigned long)lineno, cp);
    880 				goto bad;
    881 			}
    882 			if (get_host(line, lineno, hst, *grp))
    883 				goto bad;
    884 		} else if (get_host(line, lineno, cp, *grp))
    885 			goto bad;
    886 		*has_host = TRUE;
    887 	} while (netgrp && getnetgrent(&hst, &usr, &dom));
    888 
    889 	endnetgrent();
    890 	return 1;
    891 bad:
    892 	endnetgrent();
    893 	return 0;
    894 
    895 }
    896 
    897 static int
    898 parse_directory(line, lineno, tgrp, got_nondir, cp, ep, fsp)
    899 	const char *line;
    900 	size_t lineno;
    901 	struct grouplist *tgrp;
    902 	int got_nondir;
    903 	char *cp;
    904 	struct exportlist **ep;
    905 	struct statvfs *fsp;
    906 {
    907 	if (!check_dirpath(line, lineno, cp))
    908 		return 0;
    909 
    910 	if (statvfs(cp, fsp) == -1) {
    911 		syslog(LOG_ERR, "\"%s\", line %ld: statvfs for `%s' failed: %m",
    912 		    line, (unsigned long)lineno, cp);
    913 		return 0;
    914 	}
    915 
    916 	if (got_nondir) {
    917 		syslog(LOG_ERR,
    918 		    "\"%s\", line %ld: Directories must precede files",
    919 		    line, (unsigned long)lineno);
    920 		return 0;
    921 	}
    922 	if (*ep) {
    923 		if ((*ep)->ex_fs.__fsid_val[0] != fsp->f_fsidx.__fsid_val[0] ||
    924 		    (*ep)->ex_fs.__fsid_val[1] != fsp->f_fsidx.__fsid_val[1]) {
    925 			syslog(LOG_ERR,
    926 			    "\"%s\", line %ld: filesystem ids disagree",
    927 			    line, (unsigned long)lineno);
    928 			return 0;
    929 		}
    930 	} else {
    931 		/*
    932 		 * See if this directory is already
    933 		 * in the list.
    934 		 */
    935 		*ep = ex_search(&fsp->f_fsidx);
    936 		if (*ep == NULL) {
    937 			*ep = get_exp();
    938 			(*ep)->ex_fs = fsp->f_fsidx;
    939 			(*ep)->ex_fsdir = estrdup(fsp->f_mntonname);
    940 			if (debug)
    941 				(void)fprintf(stderr,
    942 				    "Making new ep fs=0x%x,0x%x\n",
    943 				    fsp->f_fsidx.__fsid_val[0], fsp->f_fsidx.__fsid_val[1]);
    944 		} else {
    945 			if (debug)
    946 				(void)fprintf(stderr,
    947 				    "Found ep fs=0x%x,0x%x\n",
    948 				    fsp->f_fsidx.__fsid_val[0], fsp->f_fsidx.__fsid_val[1]);
    949 		}
    950 	}
    951 
    952 	return 1;
    953 }
    954 
    955 
    956 /*
    957  * Get the export list
    958  */
    959 /* ARGSUSED */
    960 void
    961 get_exportlist(n)
    962 	int n;
    963 {
    964 	struct exportlist *ep, *ep2;
    965 	struct grouplist *grp, *tgrp;
    966 	struct exportlist **epp;
    967 	struct dirlist *dirhead;
    968 	struct statvfs fsb, *fsp;
    969 	struct addrinfo *ai;
    970 	struct uucred anon;
    971 	char *cp, *endcp, *dirp, savedc;
    972 	int has_host, exflags, got_nondir, dirplen, num, i;
    973 	FILE *exp_file;
    974 	char *line;
    975 	size_t lineno = 0, len;
    976 
    977 
    978 	/*
    979 	 * First, get rid of the old list
    980 	 */
    981 	ep = exphead;
    982 	while (ep) {
    983 		ep2 = ep;
    984 		ep = ep->ex_next;
    985 		free_exp(ep2);
    986 	}
    987 	exphead = NULL;
    988 
    989 	dirp = NULL;
    990 	dirplen = 0;
    991 	grp = grphead;
    992 	while (grp) {
    993 		tgrp = grp;
    994 		grp = grp->gr_next;
    995 		free_grp(tgrp);
    996 	}
    997 	grphead = NULL;
    998 
    999 	/*
   1000 	 * And delete exports that are in the kernel for all local
   1001 	 * file systems.
   1002 	 */
   1003 	num = getmntinfo(&fsp, MNT_NOWAIT);
   1004 	for (i = 0; i < num; i++) {
   1005 		struct mountd_exports_list mel;
   1006 
   1007 		/* Delete all entries from the export list. */
   1008 		mel.mel_path = fsp->f_mntonname;
   1009 		mel.mel_nexports = 0;
   1010 		if (nfssvc(NFSSVC_SETEXPORTSLIST, &mel) == -1 &&
   1011 		    errno != EOPNOTSUPP)
   1012 			syslog(LOG_ERR, "Can't delete exports for %s (%m)",
   1013 			    fsp->f_mntonname);
   1014 
   1015 		fsp++;
   1016 	}
   1017 
   1018 	/*
   1019 	 * Read in the exports file and build the list, calling
   1020 	 * mount() as we go along to push the export rules into the kernel.
   1021 	 */
   1022 	if ((exp_file = fopen(exname, "r")) == NULL) {
   1023 		/*
   1024 		 * Don't exit here; we can still reload the config
   1025 		 * after a SIGHUP.
   1026 		 */
   1027 		if (debug)
   1028 			(void)fprintf(stderr, "Can't open %s: %s\n", exname,
   1029 			    strerror(errno));
   1030 		return;
   1031 	}
   1032 	dirhead = NULL;
   1033 	while ((line = fparseln(exp_file, &len, &lineno, NULL, 0)) != NULL) {
   1034 		if (debug)
   1035 			(void)fprintf(stderr, "Got line %s\n", line);
   1036 		cp = line;
   1037 		nextfield(&cp, &endcp);
   1038 		if (cp == endcp)
   1039 			goto nextline;	/* skip empty line */
   1040 		/*
   1041 		 * Set defaults.
   1042 		 */
   1043 		has_host = FALSE;
   1044 		anon = def_anon;
   1045 		exflags = MNT_EXPORTED;
   1046 		got_nondir = 0;
   1047 		opt_flags = 0;
   1048 		ep = NULL;
   1049 
   1050 		if (noprivports) {
   1051 			opt_flags |= OP_NORESMNT | OP_NORESPORT;
   1052 			exflags |= MNT_EXNORESPORT;
   1053 		}
   1054 
   1055 		/*
   1056 		 * Create new exports list entry
   1057 		 */
   1058 		len = endcp - cp;
   1059 		tgrp = grp = get_grp();
   1060 		while (len > 0) {
   1061 			if (len > RPCMNT_NAMELEN) {
   1062 				*endcp = '\0';
   1063 				syslog(LOG_ERR,
   1064 				    "\"%s\", line %ld: name `%s' is too long",
   1065 				    line, (unsigned long)lineno, cp);
   1066 				goto badline;
   1067 			}
   1068 			switch (*cp) {
   1069 			case '-':
   1070 				/*
   1071 				 * Option
   1072 				 */
   1073 				if (ep == NULL) {
   1074 					syslog(LOG_ERR,
   1075 				"\"%s\", line %ld: No current export list",
   1076 					    line, (unsigned long)lineno);
   1077 					goto badline;
   1078 				}
   1079 				if (debug)
   1080 					(void)fprintf(stderr, "doing opt %s\n",
   1081 					    cp);
   1082 				got_nondir = 1;
   1083 				if (do_opt(line, lineno, &cp, &endcp, ep, grp,
   1084 				    &has_host, &exflags, &anon))
   1085 					goto badline;
   1086 				break;
   1087 
   1088 			case '/':
   1089 				/*
   1090 				 * Directory
   1091 				 */
   1092 				savedc = *endcp;
   1093 				*endcp = '\0';
   1094 
   1095 				if (!parse_directory(line, lineno, tgrp,
   1096 				    got_nondir, cp, &ep, &fsb))
   1097 					goto badline;
   1098 				/*
   1099 				 * Add dirpath to export mount point.
   1100 				 */
   1101 				dirp = add_expdir(&dirhead, cp, len);
   1102 				dirplen = len;
   1103 
   1104 				*endcp = savedc;
   1105 				break;
   1106 
   1107 			default:
   1108 				/*
   1109 				 * Host or netgroup.
   1110 				 */
   1111 				savedc = *endcp;
   1112 				*endcp = '\0';
   1113 
   1114 				if (!parse_host_netgroup(line, lineno, ep,
   1115 				    tgrp, cp, &has_host, &grp))
   1116 					goto badline;
   1117 
   1118 				got_nondir = 1;
   1119 
   1120 				*endcp = savedc;
   1121 				break;
   1122 			}
   1123 
   1124 			cp = endcp;
   1125 			nextfield(&cp, &endcp);
   1126 			len = endcp - cp;
   1127 		}
   1128 		if (check_options(line, lineno, dirhead))
   1129 			goto badline;
   1130 
   1131 		if (!has_host) {
   1132 			grp->gr_type = GT_HOST;
   1133 			if (debug)
   1134 				(void)fprintf(stderr,
   1135 				    "Adding a default entry\n");
   1136 			/* add a default group and make the grp list NULL */
   1137 			ai = emalloc(sizeof(struct addrinfo));
   1138 			ai->ai_flags = 0;
   1139 			ai->ai_family = AF_INET;	/* XXXX */
   1140 			ai->ai_socktype = SOCK_DGRAM;
   1141 			/* setting the length to 0 will match anything */
   1142 			ai->ai_addrlen = 0;
   1143 			ai->ai_flags = AI_CANONNAME;
   1144 			ai->ai_canonname = estrdup("Default");
   1145 			ai->ai_addr = NULL;
   1146 			ai->ai_next = NULL;
   1147 			grp->gr_ptr.gt_addrinfo = ai;
   1148 
   1149 		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
   1150 			/*
   1151 			 * Don't allow a network export coincide with a list of
   1152 			 * host(s) on the same line.
   1153 			 */
   1154 			syslog(LOG_ERR,
   1155 			    "\"%s\", line %ld: Mixed exporting of networks and hosts is disallowed",
   1156 			    line, (unsigned long)lineno);
   1157 			goto badline;
   1158 		}
   1159 		/*
   1160 		 * Loop through hosts, pushing the exports into the kernel.
   1161 		 * After loop, tgrp points to the start of the list and
   1162 		 * grp points to the last entry in the list.
   1163 		 */
   1164 		grp = tgrp;
   1165 		do {
   1166 			if (do_nfssvc(line, lineno, ep, grp, exflags, &anon,
   1167 			    dirp, dirplen, &fsb))
   1168 				goto badline;
   1169 		} while (grp->gr_next && (grp = grp->gr_next));
   1170 
   1171 		/*
   1172 		 * Success. Update the data structures.
   1173 		 */
   1174 		if (has_host) {
   1175 			hang_dirp(dirhead, tgrp, ep, opt_flags);
   1176 			grp->gr_next = grphead;
   1177 			grphead = tgrp;
   1178 		} else {
   1179 			hang_dirp(dirhead, NULL, ep, opt_flags);
   1180 			free_grp(tgrp);
   1181 		}
   1182 		tgrp = NULL;
   1183 		dirhead = NULL;
   1184 		if ((ep->ex_flag & EX_LINKED) == 0) {
   1185 			ep2 = exphead;
   1186 			epp = &exphead;
   1187 
   1188 			/*
   1189 			 * Insert in the list in alphabetical order.
   1190 			 */
   1191 			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
   1192 				epp = &ep2->ex_next;
   1193 				ep2 = ep2->ex_next;
   1194 			}
   1195 			if (ep2)
   1196 				ep->ex_next = ep2;
   1197 			*epp = ep;
   1198 			ep->ex_flag |= EX_LINKED;
   1199 		}
   1200 		goto nextline;
   1201 badline:
   1202 		free_exp_grp(ep, grp);
   1203 nextline:
   1204 		if (dirhead) {
   1205 			free_dir(dirhead);
   1206 			dirhead = NULL;
   1207 		}
   1208 		free(line);
   1209 	}
   1210 	(void)fclose(exp_file);
   1211 }
   1212 
   1213 /*
   1214  * Allocate an export list element
   1215  */
   1216 static struct exportlist *
   1217 get_exp()
   1218 {
   1219 	struct exportlist *ep;
   1220 
   1221 	ep = emalloc(sizeof(struct exportlist));
   1222 	(void)memset(ep, 0, sizeof(struct exportlist));
   1223 	return (ep);
   1224 }
   1225 
   1226 /*
   1227  * Allocate a group list element
   1228  */
   1229 static struct grouplist *
   1230 get_grp()
   1231 {
   1232 	struct grouplist *gp;
   1233 
   1234 	gp = emalloc(sizeof(struct grouplist));
   1235 	(void)memset(gp, 0, sizeof(struct grouplist));
   1236 	return (gp);
   1237 }
   1238 
   1239 /*
   1240  * Clean up upon an error in get_exportlist().
   1241  */
   1242 static void
   1243 free_exp_grp(ep, grp)
   1244 	struct exportlist *ep;
   1245 	struct grouplist *grp;
   1246 {
   1247 	struct grouplist *tgrp;
   1248 
   1249 	if (ep && (ep->ex_flag & EX_LINKED) == 0)
   1250 		free_exp(ep);
   1251 	while (grp) {
   1252 		tgrp = grp;
   1253 		grp = grp->gr_next;
   1254 		free_grp(tgrp);
   1255 	}
   1256 }
   1257 
   1258 /*
   1259  * Search the export list for a matching fs.
   1260  */
   1261 static struct exportlist *
   1262 ex_search(fsid)
   1263 	fsid_t *fsid;
   1264 {
   1265 	struct exportlist *ep;
   1266 
   1267 	ep = exphead;
   1268 	while (ep) {
   1269 		if (ep->ex_fs.__fsid_val[0] == fsid->__fsid_val[0] &&
   1270 		    ep->ex_fs.__fsid_val[1] == fsid->__fsid_val[1])
   1271 			return (ep);
   1272 		ep = ep->ex_next;
   1273 	}
   1274 	return (ep);
   1275 }
   1276 
   1277 /*
   1278  * Add a directory path to the list.
   1279  */
   1280 static char *
   1281 add_expdir(dpp, cp, len)
   1282 	struct dirlist **dpp;
   1283 	char *cp;
   1284 	int len;
   1285 {
   1286 	struct dirlist *dp;
   1287 
   1288 	dp = emalloc(sizeof(struct dirlist) + len);
   1289 	dp->dp_left = *dpp;
   1290 	dp->dp_right = NULL;
   1291 	dp->dp_flag = 0;
   1292 	dp->dp_hosts = NULL;
   1293 	(void)strcpy(dp->dp_dirp, cp);
   1294 	*dpp = dp;
   1295 	return (dp->dp_dirp);
   1296 }
   1297 
   1298 /*
   1299  * Hang the dir list element off the dirpath binary tree as required
   1300  * and update the entry for host.
   1301  */
   1302 void
   1303 hang_dirp(dp, grp, ep, flags)
   1304 	struct dirlist *dp;
   1305 	struct grouplist *grp;
   1306 	struct exportlist *ep;
   1307 	int flags;
   1308 {
   1309 	struct hostlist *hp;
   1310 	struct dirlist *dp2;
   1311 
   1312 	if (flags & OP_ALLDIRS) {
   1313 		if (ep->ex_defdir)
   1314 			free(dp);
   1315 		else
   1316 			ep->ex_defdir = dp;
   1317 		if (grp == NULL) {
   1318 			ep->ex_defdir->dp_flag |= DP_DEFSET;
   1319 			if (flags & OP_KERB)
   1320 				ep->ex_defdir->dp_flag |= DP_KERB;
   1321 			if (flags & OP_NORESMNT)
   1322 				ep->ex_defdir->dp_flag |= DP_NORESMNT;
   1323 		} else
   1324 			while (grp) {
   1325 				hp = get_ht();
   1326 				if (flags & OP_KERB)
   1327 					hp->ht_flag |= DP_KERB;
   1328 				if (flags & OP_NORESMNT)
   1329 					hp->ht_flag |= DP_NORESMNT;
   1330 				hp->ht_grp = grp;
   1331 				hp->ht_next = ep->ex_defdir->dp_hosts;
   1332 				ep->ex_defdir->dp_hosts = hp;
   1333 				grp = grp->gr_next;
   1334 			}
   1335 	} else {
   1336 
   1337 		/*
   1338 		 * Loop through the directories adding them to the tree.
   1339 		 */
   1340 		while (dp) {
   1341 			dp2 = dp->dp_left;
   1342 			add_dlist(&ep->ex_dirl, dp, grp, flags);
   1343 			dp = dp2;
   1344 		}
   1345 	}
   1346 }
   1347 
   1348 /*
   1349  * Traverse the binary tree either updating a node that is already there
   1350  * for the new directory or adding the new node.
   1351  */
   1352 static void
   1353 add_dlist(dpp, newdp, grp, flags)
   1354 	struct dirlist **dpp;
   1355 	struct dirlist *newdp;
   1356 	struct grouplist *grp;
   1357 	int flags;
   1358 {
   1359 	struct dirlist *dp;
   1360 	struct hostlist *hp;
   1361 	int cmp;
   1362 
   1363 	dp = *dpp;
   1364 	if (dp) {
   1365 		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
   1366 		if (cmp > 0) {
   1367 			add_dlist(&dp->dp_left, newdp, grp, flags);
   1368 			return;
   1369 		} else if (cmp < 0) {
   1370 			add_dlist(&dp->dp_right, newdp, grp, flags);
   1371 			return;
   1372 		} else
   1373 			free(newdp);
   1374 	} else {
   1375 		dp = newdp;
   1376 		dp->dp_left = NULL;
   1377 		*dpp = dp;
   1378 	}
   1379 	if (grp) {
   1380 
   1381 		/*
   1382 		 * Hang all of the host(s) off of the directory point.
   1383 		 */
   1384 		do {
   1385 			hp = get_ht();
   1386 			if (flags & OP_KERB)
   1387 				hp->ht_flag |= DP_KERB;
   1388 			if (flags & OP_NORESMNT)
   1389 				hp->ht_flag |= DP_NORESMNT;
   1390 			hp->ht_grp = grp;
   1391 			hp->ht_next = dp->dp_hosts;
   1392 			dp->dp_hosts = hp;
   1393 			grp = grp->gr_next;
   1394 		} while (grp);
   1395 	} else {
   1396 		dp->dp_flag |= DP_DEFSET;
   1397 		if (flags & OP_KERB)
   1398 			dp->dp_flag |= DP_KERB;
   1399 		if (flags & OP_NORESMNT)
   1400 			dp->dp_flag |= DP_NORESMNT;
   1401 	}
   1402 }
   1403 
   1404 /*
   1405  * Search for a dirpath on the export point.
   1406  */
   1407 static struct dirlist *
   1408 dirp_search(dp, dirp)
   1409 	struct dirlist *dp;
   1410 	char *dirp;
   1411 {
   1412 	int cmp;
   1413 
   1414 	if (dp) {
   1415 		cmp = strcmp(dp->dp_dirp, dirp);
   1416 		if (cmp > 0)
   1417 			return (dirp_search(dp->dp_left, dirp));
   1418 		else if (cmp < 0)
   1419 			return (dirp_search(dp->dp_right, dirp));
   1420 		else
   1421 			return (dp);
   1422 	}
   1423 	return (dp);
   1424 }
   1425 
   1426 /*
   1427  * Some helper functions for netmasks. They all assume masks in network
   1428  * order (big endian).
   1429  */
   1430 static int
   1431 bitcmp(void *dst, void *src, int bitlen)
   1432 {
   1433 	int i;
   1434 	u_int8_t *p1 = dst, *p2 = src;
   1435 	u_int8_t bitmask;
   1436 	int bytelen, bitsleft;
   1437 
   1438 	bytelen = bitlen / 8;
   1439 	bitsleft = bitlen % 8;
   1440 
   1441 	if (debug) {
   1442 		printf("comparing:\n");
   1443 		for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
   1444 			printf("%02x", p1[i]);
   1445 		printf("\n");
   1446 		for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
   1447 			printf("%02x", p2[i]);
   1448 		printf("\n");
   1449 	}
   1450 
   1451 	for (i = 0; i < bytelen; i++) {
   1452 		if (*p1 != *p2)
   1453 			return 1;
   1454 		p1++;
   1455 		p2++;
   1456 	}
   1457 
   1458 	for (i = 0; i < bitsleft; i++) {
   1459 		bitmask = 1 << (7 - i);
   1460 		if ((*p1 & bitmask) != (*p2 & bitmask))
   1461 			return 1;
   1462 	}
   1463 
   1464 	return 0;
   1465 }
   1466 
   1467 static int
   1468 netpartcmp(struct sockaddr *s1, struct sockaddr *s2, int bitlen)
   1469 {
   1470 	void *src, *dst;
   1471 
   1472 	if (s1->sa_family != s2->sa_family)
   1473 		return 1;
   1474 
   1475 	switch (s1->sa_family) {
   1476 	case AF_INET:
   1477 		src = &((struct sockaddr_in *)s1)->sin_addr;
   1478 		dst = &((struct sockaddr_in *)s2)->sin_addr;
   1479 		if (bitlen > sizeof(((struct sockaddr_in *)s1)->sin_addr) * 8)
   1480 			return 1;
   1481 		break;
   1482 	case AF_INET6:
   1483 		src = &((struct sockaddr_in6 *)s1)->sin6_addr;
   1484 		dst = &((struct sockaddr_in6 *)s2)->sin6_addr;
   1485 		if (((struct sockaddr_in6 *)s1)->sin6_scope_id !=
   1486 		    ((struct sockaddr_in6 *)s2)->sin6_scope_id)
   1487 			return 1;
   1488 		if (bitlen > sizeof(((struct sockaddr_in6 *)s1)->sin6_addr) * 8)
   1489 			return 1;
   1490 		break;
   1491 	default:
   1492 		return 1;
   1493 	}
   1494 
   1495 	return bitcmp(src, dst, bitlen);
   1496 }
   1497 
   1498 static int
   1499 allones(struct sockaddr_storage *ssp, int bitlen)
   1500 {
   1501 	u_int8_t *p;
   1502 	int bytelen, bitsleft, i;
   1503 	int zerolen;
   1504 
   1505 	switch (ssp->ss_family) {
   1506 	case AF_INET:
   1507 		p = (u_int8_t *)&((struct sockaddr_in *)ssp)->sin_addr;
   1508 		zerolen = sizeof (((struct sockaddr_in *)ssp)->sin_addr);
   1509 		break;
   1510 	case AF_INET6:
   1511 		p = (u_int8_t *)&((struct sockaddr_in6 *)ssp)->sin6_addr;
   1512 		zerolen = sizeof (((struct sockaddr_in6 *)ssp)->sin6_addr);
   1513 		break;
   1514 	default:
   1515 		return -1;
   1516 	}
   1517 
   1518 	memset(p, 0, zerolen);
   1519 
   1520 	bytelen = bitlen / 8;
   1521 	bitsleft = bitlen % 8;
   1522 
   1523 	if (bytelen > zerolen)
   1524 		return -1;
   1525 
   1526 	for (i = 0; i < bytelen; i++)
   1527 		*p++ = 0xff;
   1528 
   1529 	for (i = 0; i < bitsleft; i++)
   1530 		*p |= 1 << (7 - i);
   1531 
   1532 	return 0;
   1533 }
   1534 
   1535 static int
   1536 countones(struct sockaddr *sa)
   1537 {
   1538 	void *mask;
   1539 	int i, bits = 0, bytelen;
   1540 	u_int8_t *p;
   1541 
   1542 	switch (sa->sa_family) {
   1543 	case AF_INET:
   1544 		mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr;
   1545 		bytelen = 4;
   1546 		break;
   1547 	case AF_INET6:
   1548 		mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr;
   1549 		bytelen = 16;
   1550 		break;
   1551 	default:
   1552 		return 0;
   1553 	}
   1554 
   1555 	p = mask;
   1556 
   1557 	for (i = 0; i < bytelen; i++, p++) {
   1558 		if (*p != 0xff) {
   1559 			for (bits = 0; bits < 8; bits++) {
   1560 				if (!(*p & (1 << (7 - bits))))
   1561 					break;
   1562 			}
   1563 			break;
   1564 		}
   1565 	}
   1566 
   1567 	return (i * 8 + bits);
   1568 }
   1569 
   1570 static int
   1571 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
   1572 {
   1573 	void *p1, *p2;
   1574 	int len;
   1575 
   1576 	if (sa1->sa_family != sa2->sa_family)
   1577 		return 1;
   1578 
   1579 	switch (sa1->sa_family) {
   1580 	case AF_INET:
   1581 		p1 = &((struct sockaddr_in *)sa1)->sin_addr;
   1582 		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
   1583 		len = 4;
   1584 		break;
   1585 	case AF_INET6:
   1586 		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
   1587 		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
   1588 		len = 16;
   1589 		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
   1590 		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
   1591 			return 1;
   1592 		break;
   1593 	default:
   1594 		return 1;
   1595 	}
   1596 
   1597 	return memcmp(p1, p2, len);
   1598 }
   1599 
   1600 /*
   1601  * Scan for a host match in a directory tree.
   1602  */
   1603 static int
   1604 chk_host(dp, saddr, defsetp, hostsetp)
   1605 	struct dirlist *dp;
   1606 	struct sockaddr *saddr;
   1607 	int *defsetp;
   1608 	int *hostsetp;
   1609 {
   1610 	struct hostlist *hp;
   1611 	struct grouplist *grp;
   1612 	struct addrinfo *ai;
   1613 
   1614 	if (dp) {
   1615 		if (dp->dp_flag & DP_DEFSET)
   1616 			*defsetp = dp->dp_flag;
   1617 		hp = dp->dp_hosts;
   1618 		while (hp) {
   1619 			grp = hp->ht_grp;
   1620 			switch (grp->gr_type) {
   1621 			case GT_HOST:
   1622 				ai = grp->gr_ptr.gt_addrinfo;
   1623 				for (; ai; ai = ai->ai_next) {
   1624 					if (!sacmp(ai->ai_addr, saddr)) {
   1625 						*hostsetp =
   1626 						    (hp->ht_flag | DP_HOSTSET);
   1627 						return (1);
   1628 					}
   1629 				}
   1630 				break;
   1631 			case GT_NET:
   1632 				if (!netpartcmp(saddr,
   1633 				    (struct sockaddr *)
   1634 					&grp->gr_ptr.gt_net.nt_net,
   1635 				    grp->gr_ptr.gt_net.nt_len)) {
   1636 					*hostsetp = (hp->ht_flag | DP_HOSTSET);
   1637 					return (1);
   1638 				}
   1639 				break;
   1640 			};
   1641 			hp = hp->ht_next;
   1642 		}
   1643 	}
   1644 	return (0);
   1645 }
   1646 
   1647 /*
   1648  * Scan tree for a host that matches the address.
   1649  */
   1650 static int
   1651 scan_tree(dp, saddr)
   1652 	struct dirlist *dp;
   1653 	struct sockaddr *saddr;
   1654 {
   1655 	int defset, hostset;
   1656 
   1657 	if (dp) {
   1658 		if (scan_tree(dp->dp_left, saddr))
   1659 			return (1);
   1660 		if (chk_host(dp, saddr, &defset, &hostset))
   1661 			return (1);
   1662 		if (scan_tree(dp->dp_right, saddr))
   1663 			return (1);
   1664 	}
   1665 	return (0);
   1666 }
   1667 
   1668 /*
   1669  * Traverse the dirlist tree and free it up.
   1670  */
   1671 static void
   1672 free_dir(dp)
   1673 	struct dirlist *dp;
   1674 {
   1675 
   1676 	if (dp) {
   1677 		free_dir(dp->dp_left);
   1678 		free_dir(dp->dp_right);
   1679 		free_host(dp->dp_hosts);
   1680 		free(dp);
   1681 	}
   1682 }
   1683 
   1684 /*
   1685  * Parse the option string and update fields.
   1686  * Option arguments may either be -<option>=<value> or
   1687  * -<option> <value>
   1688  */
   1689 static int
   1690 do_opt(line, lineno, cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
   1691 	const char *line;
   1692 	size_t lineno;
   1693 	char **cpp, **endcpp;
   1694 	struct exportlist *ep;
   1695 	struct grouplist *grp;
   1696 	int *has_hostp;
   1697 	int *exflagsp;
   1698 	struct uucred *cr;
   1699 {
   1700 	char *cpoptarg, *cpoptend;
   1701 	char *cp, *cpopt, savedc, savedc2;
   1702 	char *endcp = NULL;	/* XXX: GCC */
   1703 	int allflag, usedarg;
   1704 
   1705 	cpopt = *cpp;
   1706 	cpopt++;
   1707 	cp = *endcpp;
   1708 	savedc = *cp;
   1709 	*cp = '\0';
   1710 	while (cpopt && *cpopt) {
   1711 		allflag = 1;
   1712 		usedarg = -2;
   1713 		savedc2 = '\0';
   1714 		if ((cpoptend = strchr(cpopt, ',')) != NULL) {
   1715 			*cpoptend++ = '\0';
   1716 			if ((cpoptarg = strchr(cpopt, '=')) != NULL)
   1717 				*cpoptarg++ = '\0';
   1718 		} else {
   1719 			if ((cpoptarg = strchr(cpopt, '=')) != NULL)
   1720 				*cpoptarg++ = '\0';
   1721 			else {
   1722 				*cp = savedc;
   1723 				nextfield(&cp, &endcp);
   1724 				**endcpp = '\0';
   1725 				if (endcp > cp && *cp != '-') {
   1726 					cpoptarg = cp;
   1727 					savedc2 = *endcp;
   1728 					*endcp = '\0';
   1729 					usedarg = 0;
   1730 				}
   1731 			}
   1732 		}
   1733 		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
   1734 			*exflagsp |= MNT_EXRDONLY;
   1735 		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
   1736 		    !(allflag = strcmp(cpopt, "mapall")) ||
   1737 		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
   1738 			usedarg++;
   1739 			parsecred(cpoptarg, cr);
   1740 			if (allflag == 0) {
   1741 				*exflagsp |= MNT_EXPORTANON;
   1742 				opt_flags |= OP_MAPALL;
   1743 			} else
   1744 				opt_flags |= OP_MAPROOT;
   1745 		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
   1746 			*exflagsp |= MNT_EXKERB;
   1747 			opt_flags |= OP_KERB;
   1748 		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
   1749 		    !strcmp(cpopt, "m"))) {
   1750 			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
   1751 				syslog(LOG_ERR,
   1752 				    "\"%s\", line %ld: Bad mask: %s",
   1753 				    line, (unsigned long)lineno, cpoptarg);
   1754 				return (1);
   1755 			}
   1756 			usedarg++;
   1757 			opt_flags |= OP_MASK;
   1758 		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
   1759 		    !strcmp(cpopt, "n"))) {
   1760 			if (strchr(cpoptarg, '/') != NULL) {
   1761 				if (debug)
   1762 					fprintf(stderr, "setting OP_MASKLEN\n");
   1763 				opt_flags |= OP_MASKLEN;
   1764 			}
   1765 			if (grp->gr_type != GT_NULL) {
   1766 				syslog(LOG_ERR,
   1767 				    "\"%s\", line %ld: Network/host conflict",
   1768 				    line, (unsigned long)lineno);
   1769 				return (1);
   1770 			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
   1771 				syslog(LOG_ERR,
   1772 				    "\"%s\", line %ld: Bad net: %s",
   1773 				    line, (unsigned long)lineno, cpoptarg);
   1774 				return (1);
   1775 			}
   1776 			grp->gr_type = GT_NET;
   1777 			*has_hostp = 1;
   1778 			usedarg++;
   1779 			opt_flags |= OP_NET;
   1780 		} else if (!strcmp(cpopt, "alldirs")) {
   1781 			opt_flags |= OP_ALLDIRS;
   1782 		} else if (!strcmp(cpopt, "noresvmnt")) {
   1783 			opt_flags |= OP_NORESMNT;
   1784 		} else if (!strcmp(cpopt, "noresvport")) {
   1785 			opt_flags |= OP_NORESPORT;
   1786 			*exflagsp |= MNT_EXNORESPORT;
   1787 		} else if (!strcmp(cpopt, "public")) {
   1788 			*exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC);
   1789 			opt_flags |= OP_NORESPORT;
   1790 		} else if (!strcmp(cpopt, "webnfs")) {
   1791 			*exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC |
   1792 			    MNT_EXRDONLY | MNT_EXPORTANON);
   1793 			opt_flags |= (OP_MAPALL | OP_NORESPORT);
   1794 		} else if (cpoptarg && !strcmp(cpopt, "index")) {
   1795 			ep->ex_indexfile = strdup(cpoptarg);
   1796 #ifdef ISO
   1797 		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
   1798 			if (get_isoaddr(line, lineno, cpoptarg, grp))
   1799 				return (1);
   1800 			*has_hostp = 1;
   1801 			usedarg++;
   1802 			opt_flags |= OP_ISO;
   1803 #endif /* ISO */
   1804 		} else {
   1805 			syslog(LOG_ERR,
   1806 			    "\"%s\", line %ld: Bad opt %s",
   1807 			    line, (unsigned long)lineno, cpopt);
   1808 			return (1);
   1809 		}
   1810 		if (usedarg >= 0) {
   1811 			*endcp = savedc2;
   1812 			**endcpp = savedc;
   1813 			if (usedarg > 0) {
   1814 				*cpp = cp;
   1815 				*endcpp = endcp;
   1816 			}
   1817 			return (0);
   1818 		}
   1819 		cpopt = cpoptend;
   1820 	}
   1821 	**endcpp = savedc;
   1822 	return (0);
   1823 }
   1824 
   1825 /*
   1826  * Translate a character string to the corresponding list of network
   1827  * addresses for a hostname.
   1828  */
   1829 static int
   1830 get_host(line, lineno, cp, grp)
   1831 	const char *line;
   1832 	size_t lineno;
   1833 	const char *cp;
   1834 	struct grouplist *grp;
   1835 {
   1836 	struct addrinfo *ai, hints;
   1837 	int ecode;
   1838 	char host[NI_MAXHOST];
   1839 
   1840 	if (grp->gr_type != GT_NULL) {
   1841 		syslog(LOG_ERR,
   1842 		    "\"%s\", line %ld: Bad netgroup type for ip host %s",
   1843 		    line, (unsigned long)lineno, cp);
   1844 		return (1);
   1845 	}
   1846 	memset(&hints, 0, sizeof hints);
   1847 	hints.ai_flags = AI_CANONNAME;
   1848 	hints.ai_protocol = IPPROTO_UDP;
   1849 	ecode = getaddrinfo(cp, NULL, &hints, &ai);
   1850 	if (ecode != 0) {
   1851 		syslog(LOG_ERR, "\"%s\", line %ld: can't get address info for "
   1852 				"host %s",
   1853 		    line, (long)lineno, cp);
   1854 		return 1;
   1855 	}
   1856 	grp->gr_type = GT_HOST;
   1857 	grp->gr_ptr.gt_addrinfo = ai;
   1858 	while (ai != NULL) {
   1859 		if (ai->ai_canonname == NULL) {
   1860 			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
   1861 			    sizeof host, NULL, 0, ninumeric) != 0)
   1862 				strlcpy(host, "?", sizeof(host));
   1863 			ai->ai_canonname = estrdup(host);
   1864 			ai->ai_flags |= AI_CANONNAME;
   1865 		} else
   1866 			ai->ai_flags &= ~AI_CANONNAME;
   1867 		if (debug)
   1868 			(void)fprintf(stderr, "got host %s\n", ai->ai_canonname);
   1869 		ai = ai->ai_next;
   1870 	}
   1871 	return (0);
   1872 }
   1873 
   1874 /*
   1875  * Free up an exports list component
   1876  */
   1877 static void
   1878 free_exp(ep)
   1879 	struct exportlist *ep;
   1880 {
   1881 
   1882 	if (ep->ex_defdir) {
   1883 		free_host(ep->ex_defdir->dp_hosts);
   1884 		free(ep->ex_defdir);
   1885 	}
   1886 	if (ep->ex_fsdir)
   1887 		free(ep->ex_fsdir);
   1888 	if (ep->ex_indexfile)
   1889 		free(ep->ex_indexfile);
   1890 	free_dir(ep->ex_dirl);
   1891 	free(ep);
   1892 }
   1893 
   1894 /*
   1895  * Free hosts.
   1896  */
   1897 static void
   1898 free_host(hp)
   1899 	struct hostlist *hp;
   1900 {
   1901 	struct hostlist *hp2;
   1902 
   1903 	while (hp) {
   1904 		hp2 = hp;
   1905 		hp = hp->ht_next;
   1906 		free(hp2);
   1907 	}
   1908 }
   1909 
   1910 static struct hostlist *
   1911 get_ht()
   1912 {
   1913 	struct hostlist *hp;
   1914 
   1915 	hp = emalloc(sizeof(struct hostlist));
   1916 	hp->ht_next = NULL;
   1917 	hp->ht_flag = 0;
   1918 	return (hp);
   1919 }
   1920 
   1921 #ifdef ISO
   1922 /*
   1923  * Translate an iso address.
   1924  */
   1925 static int
   1926 get_isoaddr(line, lineno, cp, grp)
   1927 	const char *line;
   1928 	size_t lineno;
   1929 	char *cp;
   1930 	struct grouplist *grp;
   1931 {
   1932 	struct iso_addr *isop;
   1933 	struct sockaddr_iso *isoaddr;
   1934 
   1935 	if (grp->gr_type != GT_NULL) {
   1936 		syslog(LOG_ERR,
   1937 		    "\"%s\", line %ld: Bad netgroup type for iso addr %s",
   1938 		    line, (unsigned long)lineno, cp);
   1939 		return (1);
   1940 	}
   1941 	if ((isop = iso_addr(cp)) == NULL) {
   1942 		syslog(LOG_ERR,
   1943 		    "\"%s\", line %ld: Bad iso addr %s",
   1944 		    line, (unsigned long)lineno, cp);
   1945 		return (1);
   1946 	}
   1947 	isoaddr = emalloc(sizeof(struct sockaddr_iso));
   1948 	(void)memset(isoaddr, 0, sizeof(struct sockaddr_iso));
   1949 	(void)memcpy(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
   1950 	isoaddr->siso_len = sizeof(struct sockaddr_iso);
   1951 	isoaddr->siso_family = AF_ISO;
   1952 	grp->gr_type = GT_ISO;
   1953 	grp->gr_ptr.gt_isoaddr = isoaddr;
   1954 	return (0);
   1955 }
   1956 #endif				/* ISO */
   1957 
   1958 /*
   1959  * Do the nfssvc syscall to push the export info into the kernel.
   1960  */
   1961 static int
   1962 do_nfssvc(line, lineno, ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
   1963 	const char *line;
   1964 	size_t lineno;
   1965 	struct exportlist *ep;
   1966 	struct grouplist *grp;
   1967 	int exflags;
   1968 	struct uucred *anoncrp;
   1969 	char *dirp;
   1970 	int dirplen;
   1971 	struct statvfs *fsb;
   1972 {
   1973 	struct sockaddr *addrp;
   1974 	struct sockaddr_storage ss;
   1975 	struct addrinfo *ai;
   1976 	int addrlen;
   1977 	int done;
   1978 	struct export_args export;
   1979 
   1980 	export.ex_flags = exflags;
   1981 	export.ex_anon = *anoncrp;
   1982 	export.ex_indexfile = ep->ex_indexfile;
   1983 	if (grp->gr_type == GT_HOST) {
   1984 		ai = grp->gr_ptr.gt_addrinfo;
   1985 		addrp = ai->ai_addr;
   1986 		addrlen = ai->ai_addrlen;
   1987 	} else {
   1988 		addrp = NULL;
   1989 		ai = NULL;	/* XXXGCC -Wuninitialized */
   1990 		addrlen = 0;	/* XXXGCC -Wuninitialized */
   1991 	}
   1992 	done = FALSE;
   1993 	while (!done) {
   1994 		struct mountd_exports_list mel;
   1995 
   1996 		switch (grp->gr_type) {
   1997 		case GT_HOST:
   1998 			if (addrp != NULL && addrp->sa_family == AF_INET6 &&
   1999 			    have_v6 == 0)
   2000 				goto skip;
   2001 			export.ex_addr = addrp;
   2002 			export.ex_addrlen = addrlen;
   2003 			export.ex_masklen = 0;
   2004 			break;
   2005 		case GT_NET:
   2006 			export.ex_addr = (struct sockaddr *)
   2007 			    &grp->gr_ptr.gt_net.nt_net;
   2008 			if (export.ex_addr->sa_family == AF_INET6 &&
   2009 			    have_v6 == 0)
   2010 				goto skip;
   2011 			export.ex_addrlen = export.ex_addr->sa_len;
   2012 			memset(&ss, 0, sizeof ss);
   2013 			ss.ss_family = export.ex_addr->sa_family;
   2014 			ss.ss_len = export.ex_addr->sa_len;
   2015 			if (allones(&ss, grp->gr_ptr.gt_net.nt_len) != 0) {
   2016 				syslog(LOG_ERR,
   2017 				    "\"%s\", line %ld: Bad network flag",
   2018 				    line, (unsigned long)lineno);
   2019 				return (1);
   2020 			}
   2021 			export.ex_mask = (struct sockaddr *)&ss;
   2022 			export.ex_masklen = ss.ss_len;
   2023 			break;
   2024 #ifdef ISO
   2025 		case GT_ISO:
   2026 			export.ex_addr =
   2027 			    (struct sockaddr *) grp->gr_ptr.gt_isoaddr;
   2028 			export.ex_addrlen =
   2029 			    sizeof(struct sockaddr_iso);
   2030 			export.ex_masklen = 0;
   2031 			break;
   2032 #endif				/* ISO */
   2033 		default:
   2034 			syslog(LOG_ERR, "\"%s\", line %ld: Bad netgroup type",
   2035 			    line, (unsigned long)lineno);
   2036 			return (1);
   2037 		};
   2038 
   2039 		/*
   2040 		 * XXX:
   2041 		 * Maybe I should just use the fsb->f_mntonname path?
   2042 		 */
   2043 
   2044 		mel.mel_path = dirp;
   2045 		mel.mel_nexports = 1;
   2046 		mel.mel_exports = &export;
   2047 
   2048 		if (nfssvc(NFSSVC_SETEXPORTSLIST, &mel) != 0) {
   2049 			syslog(LOG_ERR,
   2050 	    "\"%s\", line %ld: Can't change attributes for %s to %s: %m",
   2051 			    line, (unsigned long)lineno,
   2052 			    dirp, (grp->gr_type == GT_HOST) ?
   2053 			    grp->gr_ptr.gt_addrinfo->ai_canonname :
   2054 			    (grp->gr_type == GT_NET) ?
   2055 			    grp->gr_ptr.gt_net.nt_name :
   2056 			    "Unknown");
   2057 			return (1);
   2058 		}
   2059 skip:
   2060 		if (addrp) {
   2061 			ai = ai->ai_next;
   2062 			if (ai == NULL)
   2063 				done = TRUE;
   2064 			else {
   2065 				addrp = ai->ai_addr;
   2066 				addrlen = ai->ai_addrlen;
   2067 			}
   2068 		} else
   2069 			done = TRUE;
   2070 	}
   2071 	return (0);
   2072 }
   2073 
   2074 /*
   2075  * Translate a net address.
   2076  */
   2077 static int
   2078 get_net(cp, net, maskflg)
   2079 	char *cp;
   2080 	struct netmsk *net;
   2081 	int maskflg;
   2082 {
   2083 	struct netent *np;
   2084 	char *name, *p, *prefp;
   2085 	struct sockaddr_in sin, *sinp;
   2086 	struct sockaddr *sa;
   2087 	struct addrinfo hints, *ai = NULL;
   2088 	char netname[NI_MAXHOST];
   2089 	long preflen;
   2090 	int ecode;
   2091 
   2092 	(void)memset(&sin, 0, sizeof(sin));
   2093 	if ((opt_flags & OP_MASKLEN) && !maskflg) {
   2094 		p = strchr(cp, '/');
   2095 		*p = '\0';
   2096 		prefp = p + 1;
   2097 	} else {
   2098 		p = NULL;	/* XXXGCC -Wuninitialized */
   2099 		prefp = NULL;	/* XXXGCC -Wuninitialized */
   2100 	}
   2101 
   2102 	if ((np = getnetbyname(cp)) != NULL) {
   2103 		sin.sin_family = AF_INET;
   2104 		sin.sin_len = sizeof sin;
   2105 		sin.sin_addr = inet_makeaddr(np->n_net, 0);
   2106 		sa = (struct sockaddr *)&sin;
   2107 	} else if (isdigit((unsigned char)*cp)) {
   2108 		memset(&hints, 0, sizeof hints);
   2109 		hints.ai_family = AF_UNSPEC;
   2110 		hints.ai_flags = AI_NUMERICHOST;
   2111 		if (getaddrinfo(cp, NULL, &hints, &ai) != 0) {
   2112 			/*
   2113 			 * If getaddrinfo() failed, try the inet4 network
   2114 			 * notation with less than 3 dots.
   2115 			 */
   2116 			sin.sin_family = AF_INET;
   2117 			sin.sin_len = sizeof sin;
   2118 			sin.sin_addr = inet_makeaddr(inet_network(cp),0);
   2119 			if (debug)
   2120 				fprintf(stderr, "get_net: v4 addr %x\n",
   2121 				    sin.sin_addr.s_addr);
   2122 			sa = (struct sockaddr *)&sin;
   2123 		} else
   2124 			sa = ai->ai_addr;
   2125 	} else if (isxdigit((unsigned char)*cp) || *cp == ':') {
   2126 		memset(&hints, 0, sizeof hints);
   2127 		hints.ai_family = AF_UNSPEC;
   2128 		hints.ai_flags = AI_NUMERICHOST;
   2129 		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
   2130 			sa = ai->ai_addr;
   2131 		else
   2132 			goto fail;
   2133 	} else
   2134 		goto fail;
   2135 
   2136 	/*
   2137 	 * Only allow /pref notation for v6 addresses.
   2138 	 */
   2139 	if (sa->sa_family == AF_INET6 && (!(opt_flags & OP_MASKLEN) || maskflg))
   2140 		return 1;
   2141 
   2142 	ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname,
   2143 	    NULL, 0, ninumeric);
   2144 	if (ecode != 0)
   2145 		goto fail;
   2146 
   2147 	if (maskflg)
   2148 		net->nt_len = countones(sa);
   2149 	else {
   2150 		if (opt_flags & OP_MASKLEN) {
   2151 			errno = 0;
   2152 			preflen = strtol(prefp, NULL, 10);
   2153 			if (preflen == LONG_MIN && errno == ERANGE)
   2154 				goto fail;
   2155 			net->nt_len = (int)preflen;
   2156 			*p = '/';
   2157 		}
   2158 
   2159 		if (np)
   2160 			name = np->n_name;
   2161 		else {
   2162 			if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
   2163 			    NULL, 0, ninumeric) != 0)
   2164 				strlcpy(netname, "?", sizeof(netname));
   2165 			name = netname;
   2166 		}
   2167 		net->nt_name = estrdup(name);
   2168 		memcpy(&net->nt_net, sa, sa->sa_len);
   2169 	}
   2170 
   2171 	if (!maskflg && sa->sa_family == AF_INET &&
   2172 	    !(opt_flags & (OP_MASK|OP_MASKLEN))) {
   2173 		sinp = (struct sockaddr_in *)sa;
   2174 		if (IN_CLASSA(sinp->sin_addr.s_addr))
   2175 			net->nt_len = 8;
   2176 		else if (IN_CLASSB(sinp->sin_addr.s_addr))
   2177 			net->nt_len = 16;
   2178 		else if (IN_CLASSC(sinp->sin_addr.s_addr))
   2179 			net->nt_len = 24;
   2180 		else if (IN_CLASSD(sinp->sin_addr.s_addr))
   2181 			net->nt_len = 28;
   2182 		else
   2183 			net->nt_len = 32;	/* XXX */
   2184 	}
   2185 
   2186 	if (ai)
   2187 		freeaddrinfo(ai);
   2188 	return 0;
   2189 
   2190 fail:
   2191 	if (ai)
   2192 		freeaddrinfo(ai);
   2193 	return 1;
   2194 }
   2195 
   2196 /*
   2197  * Parse out the next white space separated field
   2198  */
   2199 static void
   2200 nextfield(cp, endcp)
   2201 	char **cp;
   2202 	char **endcp;
   2203 {
   2204 	char *p;
   2205 
   2206 	p = *cp;
   2207 	while (*p == ' ' || *p == '\t')
   2208 		p++;
   2209 	if (*p == '\n' || *p == '\0')
   2210 		*cp = *endcp = p;
   2211 	else {
   2212 		*cp = p++;
   2213 		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
   2214 			p++;
   2215 		*endcp = p;
   2216 	}
   2217 }
   2218 
   2219 /*
   2220  * Parse a description of a credential.
   2221  */
   2222 static void
   2223 parsecred(namelist, cr)
   2224 	char *namelist;
   2225 	struct uucred *cr;
   2226 {
   2227 	char *name;
   2228 	int cnt;
   2229 	char *names;
   2230 	struct passwd *pw;
   2231 	struct group *gr;
   2232 	int ngroups;
   2233 	gid_t groups[NGROUPS + 1];
   2234 
   2235 	/*
   2236 	 * Set up the unprivileged user.
   2237 	 */
   2238 	*cr = def_anon;
   2239 	/*
   2240 	 * Get the user's password table entry.
   2241 	 */
   2242 	names = strsep(&namelist, " \t\n");
   2243 	name = strsep(&names, ":");
   2244 	if (isdigit((unsigned char)*name) || *name == '-')
   2245 		pw = getpwuid(atoi(name));
   2246 	else
   2247 		pw = getpwnam(name);
   2248 	/*
   2249 	 * Credentials specified as those of a user.
   2250 	 */
   2251 	if (names == NULL) {
   2252 		if (pw == NULL) {
   2253 			syslog(LOG_ERR, "Unknown user: %s", name);
   2254 			return;
   2255 		}
   2256 		cr->cr_uid = pw->pw_uid;
   2257 		ngroups = NGROUPS + 1;
   2258 		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
   2259 			syslog(LOG_ERR, "Too many groups for user %s", name);
   2260 		/*
   2261 		 * Convert from int's to gid_t's and compress out duplicate
   2262 		 */
   2263 		cr->cr_ngroups = ngroups - 1;
   2264 		cr->cr_gid = groups[0];
   2265 		for (cnt = 1; cnt < ngroups; cnt++)
   2266 			cr->cr_groups[cnt - 1] = groups[cnt];
   2267 		return;
   2268 	}
   2269 	/*
   2270 	 * Explicit credential specified as a colon separated list:
   2271 	 *	uid:gid:gid:...
   2272 	 */
   2273 	if (pw != NULL)
   2274 		cr->cr_uid = pw->pw_uid;
   2275 	else if (isdigit((unsigned char)*name) || *name == '-')
   2276 		cr->cr_uid = atoi(name);
   2277 	else {
   2278 		syslog(LOG_ERR, "Unknown user: %s", name);
   2279 		return;
   2280 	}
   2281 	cr->cr_ngroups = 0;
   2282 	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
   2283 		name = strsep(&names, ":");
   2284 		if (isdigit((unsigned char)*name) || *name == '-') {
   2285 			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
   2286 		} else {
   2287 			if ((gr = getgrnam(name)) == NULL) {
   2288 				syslog(LOG_ERR, "Unknown group: %s", name);
   2289 				continue;
   2290 			}
   2291 			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
   2292 		}
   2293 	}
   2294 	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
   2295 		syslog(LOG_ERR, "Too many groups");
   2296 }
   2297 
   2298 #define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
   2299 /*
   2300  * Routines that maintain the remote mounttab
   2301  */
   2302 static void
   2303 get_mountlist()
   2304 {
   2305 	struct mountlist *mlp, **mlpp;
   2306 	char *host, *dirp, *cp;
   2307 	char str[STRSIZ];
   2308 	FILE *mlfile;
   2309 
   2310 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
   2311 		syslog(LOG_ERR, "Can't open %s: %m", _PATH_RMOUNTLIST);
   2312 		return;
   2313 	}
   2314 	mlpp = &mlhead;
   2315 	while (fgets(str, STRSIZ, mlfile) != NULL) {
   2316 		cp = str;
   2317 		host = strsep(&cp, " \t\n");
   2318 		dirp = strsep(&cp, " \t\n");
   2319 		if (host == NULL || dirp == NULL)
   2320 			continue;
   2321 		mlp = emalloc(sizeof(*mlp));
   2322 		(void)strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
   2323 		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
   2324 		(void)strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
   2325 		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
   2326 		mlp->ml_next = NULL;
   2327 		*mlpp = mlp;
   2328 		mlpp = &mlp->ml_next;
   2329 	}
   2330 	(void)fclose(mlfile);
   2331 }
   2332 
   2333 static int
   2334 del_mlist(hostp, dirp, saddr)
   2335 	char *hostp, *dirp;
   2336 	struct sockaddr *saddr;
   2337 {
   2338 	struct mountlist *mlp, **mlpp;
   2339 	struct mountlist *mlp2;
   2340 	u_short sport;
   2341 	FILE *mlfile;
   2342 	int fnd = 0, ret = 0;
   2343 	char host[NI_MAXHOST];
   2344 
   2345 	switch (saddr->sa_family) {
   2346 	case AF_INET6:
   2347 		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
   2348 		break;
   2349 	case AF_INET:
   2350 		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
   2351 		break;
   2352 	default:
   2353 		return -1;
   2354 	}
   2355 	mlpp = &mlhead;
   2356 	mlp = mlhead;
   2357 	while (mlp) {
   2358 		if (!strcmp(mlp->ml_host, hostp) &&
   2359 		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
   2360 			if (!(mlp->ml_flag & DP_NORESMNT) &&
   2361 			    sport >= IPPORT_RESERVED) {
   2362 				if (getnameinfo(saddr, saddr->sa_len, host,
   2363 				    sizeof host, NULL, 0, ninumeric) != 0)
   2364 					strlcpy(host, "?", sizeof(host));
   2365 				syslog(LOG_NOTICE,
   2366 				"Umount request for %s:%s from %s refused\n",
   2367 				    mlp->ml_host, mlp->ml_dirp, host);
   2368 				ret = -1;
   2369 				goto cont;
   2370 			}
   2371 			fnd = 1;
   2372 			mlp2 = mlp;
   2373 			*mlpp = mlp = mlp->ml_next;
   2374 			free(mlp2);
   2375 		} else {
   2376 cont:
   2377 			mlpp = &mlp->ml_next;
   2378 			mlp = mlp->ml_next;
   2379 		}
   2380 	}
   2381 	if (fnd) {
   2382 		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
   2383 			syslog(LOG_ERR, "Can't update %s: %m",
   2384 			    _PATH_RMOUNTLIST);
   2385 			return ret;
   2386 		}
   2387 		mlp = mlhead;
   2388 		while (mlp) {
   2389 			(void)fprintf(mlfile, "%s %s\n", mlp->ml_host,
   2390 		            mlp->ml_dirp);
   2391 			mlp = mlp->ml_next;
   2392 		}
   2393 		(void)fclose(mlfile);
   2394 	}
   2395 	return ret;
   2396 }
   2397 
   2398 static void
   2399 add_mlist(hostp, dirp, flags)
   2400 	char *hostp, *dirp;
   2401 	int flags;
   2402 {
   2403 	struct mountlist *mlp, **mlpp;
   2404 	FILE *mlfile;
   2405 
   2406 	mlpp = &mlhead;
   2407 	mlp = mlhead;
   2408 	while (mlp) {
   2409 		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
   2410 			return;
   2411 		mlpp = &mlp->ml_next;
   2412 		mlp = mlp->ml_next;
   2413 	}
   2414 	mlp = emalloc(sizeof(*mlp));
   2415 	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
   2416 	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
   2417 	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
   2418 	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
   2419 	mlp->ml_flag = flags;
   2420 	mlp->ml_next = NULL;
   2421 	*mlpp = mlp;
   2422 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
   2423 		syslog(LOG_ERR, "Can't update %s: %m", _PATH_RMOUNTLIST);
   2424 		return;
   2425 	}
   2426 	(void)fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
   2427 	(void)fclose(mlfile);
   2428 }
   2429 
   2430 /*
   2431  * This function is called via. SIGTERM when the system is going down.
   2432  * It sends a broadcast RPCMNT_UMNTALL.
   2433  */
   2434 /* ARGSUSED */
   2435 static void
   2436 send_umntall(n)
   2437 	int n;
   2438 {
   2439 	(void)clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
   2440 	    xdr_void, NULL, xdr_void, NULL, (resultproc_t)umntall_each);
   2441 	exit(0);
   2442 }
   2443 
   2444 static int
   2445 umntall_each(resultsp, raddr)
   2446 	caddr_t resultsp;
   2447 	struct sockaddr_in *raddr;
   2448 {
   2449 	return (1);
   2450 }
   2451 
   2452 /*
   2453  * Free up a group list.
   2454  */
   2455 static void
   2456 free_grp(grp)
   2457 	struct grouplist *grp;
   2458 {
   2459 
   2460 	if (grp->gr_type == GT_HOST) {
   2461 		if (grp->gr_ptr.gt_addrinfo != NULL)
   2462 			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
   2463 	} else if (grp->gr_type == GT_NET) {
   2464 		if (grp->gr_ptr.gt_net.nt_name)
   2465 			free(grp->gr_ptr.gt_net.nt_name);
   2466 	}
   2467 #ifdef ISO
   2468 	else if (grp->gr_type == GT_ISO)
   2469 		free(grp->gr_ptr.gt_isoaddr);
   2470 #endif
   2471 	free(grp);
   2472 }
   2473 
   2474 #if 0
   2475 static void
   2476 SYSLOG(int pri, const char *fmt,...)
   2477 {
   2478 	va_list ap;
   2479 
   2480 	va_start(ap, fmt);
   2481 
   2482 	if (debug)
   2483 		vfprintf(stderr, fmt, ap);
   2484 	else
   2485 		vsyslog(pri, fmt, ap);
   2486 
   2487 	va_end(ap);
   2488 }
   2489 #endif
   2490 
   2491 /*
   2492  * Check options for consistency.
   2493  */
   2494 static int
   2495 check_options(line, lineno, dp)
   2496 	const char *line;
   2497 	size_t lineno;
   2498 	struct dirlist *dp;
   2499 {
   2500 
   2501 	if (dp == NULL) {
   2502 		syslog(LOG_ERR,
   2503 		    "\"%s\", line %ld: missing directory list",
   2504 		    line, (unsigned long)lineno);
   2505 		return (1);
   2506 	}
   2507 	if ((opt_flags & (OP_MAPROOT|OP_MAPALL)) == (OP_MAPROOT|OP_MAPALL) ||
   2508 	    (opt_flags & (OP_MAPROOT|OP_KERB)) == (OP_MAPROOT|OP_KERB) ||
   2509 	    (opt_flags & (OP_MAPALL|OP_KERB)) == (OP_MAPALL|OP_KERB)) {
   2510 		syslog(LOG_ERR,
   2511 		    "\"%s\", line %ld: -mapall, -maproot and -kerb mutually exclusive",
   2512 		    line, (unsigned long)lineno);
   2513 		return (1);
   2514 	}
   2515 	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
   2516 		syslog(LOG_ERR, "\"%s\", line %ld: -mask requires -net",
   2517 		    line, (unsigned long)lineno);
   2518 		return (1);
   2519 	}
   2520 	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN) != 0) {
   2521 		syslog(LOG_ERR, "\"%s\", line %ld: /pref and -mask mutually"
   2522 		    " exclusive",
   2523 		    line, (unsigned long)lineno);
   2524 		return (1);
   2525 	}
   2526 	if ((opt_flags & (OP_NET|OP_ISO)) == (OP_NET|OP_ISO)) {
   2527 		syslog(LOG_ERR,
   2528 		    "\"%s\", line %ld: -net and -iso mutually exclusive",
   2529 		    line, (unsigned long)lineno);
   2530 		return (1);
   2531 	}
   2532 	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
   2533 		syslog(LOG_ERR,
   2534 		    "\"%s\", line %ld: -alldirs has multiple directories",
   2535 		    line, (unsigned long)lineno);
   2536 		return (1);
   2537 	}
   2538 	return (0);
   2539 }
   2540 
   2541 /*
   2542  * Check an absolute directory path for any symbolic links. Return true
   2543  * if no symbolic links are found.
   2544  */
   2545 static int
   2546 check_dirpath(line, lineno, dirp)
   2547 	const char *line;
   2548 	size_t lineno;
   2549 	char *dirp;
   2550 {
   2551 	char *cp;
   2552 	struct stat sb;
   2553 	char *file = "";
   2554 
   2555 	for (cp = dirp + 1; *cp; cp++) {
   2556 		if (*cp == '/') {
   2557 			*cp = '\0';
   2558 			if (lstat(dirp, &sb) == -1)
   2559 				goto bad;
   2560 			if (!S_ISDIR(sb.st_mode))
   2561 				goto bad1;
   2562 			*cp = '/';
   2563 		}
   2564 	}
   2565 
   2566 	cp = NULL;
   2567 	if (lstat(dirp, &sb) == -1)
   2568 		goto bad;
   2569 
   2570 	if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
   2571 		file = " file or a";
   2572 		goto bad1;
   2573 	}
   2574 
   2575 	return 1;
   2576 
   2577 bad:
   2578 	syslog(LOG_ERR,
   2579 	    "\"%s\", line %ld: lstat for `%s' failed: %m",
   2580 	    line, (unsigned long)lineno, dirp);
   2581 	if (cp)
   2582 		*cp = '/';
   2583 	return 0;
   2584 
   2585 bad1:
   2586 	syslog(LOG_ERR,
   2587 	    "\"%s\", line %ld: `%s' is not a%s directory",
   2588 	    line, (unsigned long)lineno, dirp, file);
   2589 	if (cp)
   2590 		*cp = '/';
   2591 	return 0;
   2592 }
   2593 
   2594 static void
   2595 bind_resv_port(int sock, sa_family_t family, in_port_t port)
   2596 {
   2597 	struct sockaddr *sa;
   2598 	struct sockaddr_in sasin;
   2599 	struct sockaddr_in6 sasin6;
   2600 
   2601 	switch (family) {
   2602 	case AF_INET:
   2603 		(void)memset(&sasin, 0, sizeof(sasin));
   2604 		sasin.sin_len = sizeof(sasin);
   2605 		sasin.sin_family = family;
   2606 		sasin.sin_port = htons(port);
   2607 		sa = (struct sockaddr *)(void *)&sasin;
   2608 		break;
   2609 	case AF_INET6:
   2610 		(void)memset(&sasin6, 0, sizeof(sasin6));
   2611 		sasin6.sin6_len = sizeof(sasin6);
   2612 		sasin6.sin6_family = family;
   2613 		sasin6.sin6_port = htons(port);
   2614 		sa = (struct sockaddr *)(void *)&sasin6;
   2615 		break;
   2616 	default:
   2617 		syslog(LOG_ERR, "Unsupported address family %d", family);
   2618 		return;
   2619 	}
   2620 	if (bindresvport_sa(sock, sa) == -1)
   2621 		syslog(LOG_ERR, "Cannot bind to reserved port %d (%m)", port);
   2622 }
   2623 
   2624 /* ARGSUSED */
   2625 static void
   2626 no_nfs(int sig)
   2627 {
   2628 	syslog(LOG_ERR, "kernel NFS support not present; exiting");
   2629 	exit(1);
   2630 }
   2631