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