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