Home | History | Annotate | Line # | Download | only in mountd
mountd.c revision 1.93
      1 /* 	$NetBSD: mountd.c,v 1.93 2005/03/15 04:54:59 briggs 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.93 2005/03/15 04:54:59 briggs 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_MSDOS, MFSNAMELEN) ||
   1022 		    !strncmp(fsp->f_fstypename, MOUNT_ADOSFS, MFSNAMELEN) ||
   1023 		    !strncmp(fsp->f_fstypename, MOUNT_NULL, MFSNAMELEN) ||
   1024 		    !strncmp(fsp->f_fstypename, MOUNT_UMAP, MFSNAMELEN) ||
   1025 		    !strncmp(fsp->f_fstypename, MOUNT_UNION, MFSNAMELEN) ||
   1026 		    !strncmp(fsp->f_fstypename, MOUNT_CD9660, MFSNAMELEN) ||
   1027 		    !strncmp(fsp->f_fstypename, MOUNT_NTFS, MFSNAMELEN)) {
   1028 			bzero((char *) &targs, sizeof(targs));
   1029 			targs.ua.fspec = NULL;
   1030 			targs.ua.export.ex_flags = MNT_DELEXPORT;
   1031 			if (mount(fsp->f_fstypename, fsp->f_mntonname,
   1032 			    fsp->f_flag | MNT_UPDATE, &targs) == -1)
   1033 				syslog(LOG_ERR, "Can't delete exports for %s",
   1034 				    fsp->f_mntonname);
   1035 		}
   1036 		fsp++;
   1037 	}
   1038 
   1039 	/*
   1040 	 * Read in the exports file and build the list, calling
   1041 	 * mount() as we go along to push the export rules into the kernel.
   1042 	 */
   1043 	if ((exp_file = fopen(exname, "r")) == NULL) {
   1044 		/*
   1045 		 * Don't exit here; we can still reload the config
   1046 		 * after a SIGHUP.
   1047 		 */
   1048 		if (debug)
   1049 			(void)fprintf(stderr, "Can't open %s: %s\n", exname,
   1050 			    strerror(errno));
   1051 		return;
   1052 	}
   1053 	dirhead = NULL;
   1054 	while ((line = fparseln(exp_file, &len, &lineno, NULL, 0)) != NULL) {
   1055 		if (debug)
   1056 			(void)fprintf(stderr, "Got line %s\n", line);
   1057 		cp = line;
   1058 		nextfield(&cp, &endcp);
   1059 		if (cp == endcp)
   1060 			goto nextline;	/* skip empty line */
   1061 		/*
   1062 		 * Set defaults.
   1063 		 */
   1064 		has_host = FALSE;
   1065 		anon = def_anon;
   1066 		exflags = MNT_EXPORTED;
   1067 		got_nondir = 0;
   1068 		opt_flags = 0;
   1069 		ep = NULL;
   1070 
   1071 		if (noprivports) {
   1072 			opt_flags |= OP_NORESMNT | OP_NORESPORT;
   1073 			exflags |= MNT_EXNORESPORT;
   1074 		}
   1075 
   1076 		/*
   1077 		 * Create new exports list entry
   1078 		 */
   1079 		len = endcp - cp;
   1080 		tgrp = grp = get_grp();
   1081 		while (len > 0) {
   1082 			if (len > RPCMNT_NAMELEN) {
   1083 				*endcp = '\0';
   1084 				syslog(LOG_ERR,
   1085 				    "\"%s\", line %ld: name `%s' is too long",
   1086 				    line, (unsigned long)lineno, cp);
   1087 				goto badline;
   1088 			}
   1089 			switch (*cp) {
   1090 			case '-':
   1091 				/*
   1092 				 * Option
   1093 				 */
   1094 				if (ep == NULL) {
   1095 					syslog(LOG_ERR,
   1096 				"\"%s\", line %ld: No current export list",
   1097 					    line, (unsigned long)lineno);
   1098 					goto badline;
   1099 				}
   1100 				if (debug)
   1101 					(void)fprintf(stderr, "doing opt %s\n",
   1102 					    cp);
   1103 				got_nondir = 1;
   1104 				if (do_opt(line, lineno, &cp, &endcp, ep, grp,
   1105 				    &has_host, &exflags, &anon))
   1106 					goto badline;
   1107 				break;
   1108 
   1109 			case '/':
   1110 				/*
   1111 				 * Directory
   1112 				 */
   1113 				savedc = *endcp;
   1114 				*endcp = '\0';
   1115 
   1116 				if (!parse_directory(line, lineno, tgrp,
   1117 				    got_nondir, cp, &ep, &fsb))
   1118 					goto badline;
   1119 				/*
   1120 				 * Add dirpath to export mount point.
   1121 				 */
   1122 				dirp = add_expdir(&dirhead, cp, len);
   1123 				dirplen = len;
   1124 
   1125 				*endcp = savedc;
   1126 				break;
   1127 
   1128 			default:
   1129 				/*
   1130 				 * Host or netgroup.
   1131 				 */
   1132 				savedc = *endcp;
   1133 				*endcp = '\0';
   1134 
   1135 				if (!parse_host_netgroup(line, lineno, ep,
   1136 				    tgrp, cp, &has_host, &grp))
   1137 					goto badline;
   1138 
   1139 				got_nondir = 1;
   1140 
   1141 				*endcp = savedc;
   1142 				break;
   1143 			}
   1144 
   1145 			cp = endcp;
   1146 			nextfield(&cp, &endcp);
   1147 			len = endcp - cp;
   1148 		}
   1149 		if (check_options(line, lineno, dirhead))
   1150 			goto badline;
   1151 
   1152 		if (!has_host) {
   1153 			grp->gr_type = GT_HOST;
   1154 			if (debug)
   1155 				(void)fprintf(stderr,
   1156 				    "Adding a default entry\n");
   1157 			/* add a default group and make the grp list NULL */
   1158 			ai = emalloc(sizeof(struct addrinfo));
   1159 			ai->ai_flags = 0;
   1160 			ai->ai_family = AF_INET;	/* XXXX */
   1161 			ai->ai_socktype = SOCK_DGRAM;
   1162 			/* setting the length to 0 will match anything */
   1163 			ai->ai_addrlen = 0;
   1164 			ai->ai_flags = AI_CANONNAME;
   1165 			ai->ai_canonname = estrdup("Default");
   1166 			ai->ai_addr = NULL;
   1167 			ai->ai_next = NULL;
   1168 			grp->gr_ptr.gt_addrinfo = ai;
   1169 
   1170 		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
   1171 			/*
   1172 			 * Don't allow a network export coincide with a list of
   1173 			 * host(s) on the same line.
   1174 			 */
   1175 			syslog(LOG_ERR,
   1176 			    "\"%s\", line %ld: Mixed exporting of networks and hosts is disallowed",
   1177 			    line, (unsigned long)lineno);
   1178 			goto badline;
   1179 		}
   1180 		/*
   1181 		 * Loop through hosts, pushing the exports into the kernel.
   1182 		 * After loop, tgrp points to the start of the list and
   1183 		 * grp points to the last entry in the list.
   1184 		 */
   1185 		grp = tgrp;
   1186 		do {
   1187 			if (do_mount(line, lineno, ep, grp, exflags, &anon,
   1188 			    dirp, dirplen, &fsb))
   1189 				goto badline;
   1190 		} while (grp->gr_next && (grp = grp->gr_next));
   1191 
   1192 		/*
   1193 		 * Success. Update the data structures.
   1194 		 */
   1195 		if (has_host) {
   1196 			hang_dirp(dirhead, tgrp, ep, opt_flags);
   1197 			grp->gr_next = grphead;
   1198 			grphead = tgrp;
   1199 		} else {
   1200 			hang_dirp(dirhead, NULL, ep, opt_flags);
   1201 			free_grp(grp);
   1202 		}
   1203 		dirhead = NULL;
   1204 		if ((ep->ex_flag & EX_LINKED) == 0) {
   1205 			ep2 = exphead;
   1206 			epp = &exphead;
   1207 
   1208 			/*
   1209 			 * Insert in the list in alphabetical order.
   1210 			 */
   1211 			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
   1212 				epp = &ep2->ex_next;
   1213 				ep2 = ep2->ex_next;
   1214 			}
   1215 			if (ep2)
   1216 				ep->ex_next = ep2;
   1217 			*epp = ep;
   1218 			ep->ex_flag |= EX_LINKED;
   1219 		}
   1220 		goto nextline;
   1221 badline:
   1222 		free_exp_grp(ep, grp);
   1223 nextline:
   1224 		if (dirhead) {
   1225 			free_dir(dirhead);
   1226 			dirhead = NULL;
   1227 		}
   1228 		free(line);
   1229 	}
   1230 	(void)fclose(exp_file);
   1231 }
   1232 
   1233 /*
   1234  * Allocate an export list element
   1235  */
   1236 static struct exportlist *
   1237 get_exp()
   1238 {
   1239 	struct exportlist *ep;
   1240 
   1241 	ep = emalloc(sizeof(struct exportlist));
   1242 	(void)memset(ep, 0, sizeof(struct exportlist));
   1243 	return (ep);
   1244 }
   1245 
   1246 /*
   1247  * Allocate a group list element
   1248  */
   1249 static struct grouplist *
   1250 get_grp()
   1251 {
   1252 	struct grouplist *gp;
   1253 
   1254 	gp = emalloc(sizeof(struct grouplist));
   1255 	(void)memset(gp, 0, sizeof(struct grouplist));
   1256 	return (gp);
   1257 }
   1258 
   1259 /*
   1260  * Clean up upon an error in get_exportlist().
   1261  */
   1262 static void
   1263 free_exp_grp(ep, grp)
   1264 	struct exportlist *ep;
   1265 	struct grouplist *grp;
   1266 {
   1267 	struct grouplist *tgrp;
   1268 
   1269 	if (ep && (ep->ex_flag & EX_LINKED) == 0)
   1270 		free_exp(ep);
   1271 	while (grp) {
   1272 		tgrp = grp;
   1273 		grp = grp->gr_next;
   1274 		free_grp(tgrp);
   1275 	}
   1276 }
   1277 
   1278 /*
   1279  * Search the export list for a matching fs.
   1280  */
   1281 static struct exportlist *
   1282 ex_search(fsid)
   1283 	fsid_t *fsid;
   1284 {
   1285 	struct exportlist *ep;
   1286 
   1287 	ep = exphead;
   1288 	while (ep) {
   1289 		if (ep->ex_fs.__fsid_val[0] == fsid->__fsid_val[0] &&
   1290 		    ep->ex_fs.__fsid_val[1] == fsid->__fsid_val[1])
   1291 			return (ep);
   1292 		ep = ep->ex_next;
   1293 	}
   1294 	return (ep);
   1295 }
   1296 
   1297 /*
   1298  * Add a directory path to the list.
   1299  */
   1300 static char *
   1301 add_expdir(dpp, cp, len)
   1302 	struct dirlist **dpp;
   1303 	char *cp;
   1304 	int len;
   1305 {
   1306 	struct dirlist *dp;
   1307 
   1308 	dp = emalloc(sizeof(struct dirlist) + len);
   1309 	dp->dp_left = *dpp;
   1310 	dp->dp_right = NULL;
   1311 	dp->dp_flag = 0;
   1312 	dp->dp_hosts = NULL;
   1313 	(void)strcpy(dp->dp_dirp, cp);
   1314 	*dpp = dp;
   1315 	return (dp->dp_dirp);
   1316 }
   1317 
   1318 /*
   1319  * Hang the dir list element off the dirpath binary tree as required
   1320  * and update the entry for host.
   1321  */
   1322 void
   1323 hang_dirp(dp, grp, ep, flags)
   1324 	struct dirlist *dp;
   1325 	struct grouplist *grp;
   1326 	struct exportlist *ep;
   1327 	int flags;
   1328 {
   1329 	struct hostlist *hp;
   1330 	struct dirlist *dp2;
   1331 
   1332 	if (flags & OP_ALLDIRS) {
   1333 		if (ep->ex_defdir)
   1334 			free(dp);
   1335 		else
   1336 			ep->ex_defdir = dp;
   1337 		if (grp == NULL) {
   1338 			ep->ex_defdir->dp_flag |= DP_DEFSET;
   1339 			if (flags & OP_KERB)
   1340 				ep->ex_defdir->dp_flag |= DP_KERB;
   1341 			if (flags & OP_NORESMNT)
   1342 				ep->ex_defdir->dp_flag |= DP_NORESMNT;
   1343 		} else
   1344 			while (grp) {
   1345 				hp = get_ht();
   1346 				if (flags & OP_KERB)
   1347 					hp->ht_flag |= DP_KERB;
   1348 				if (flags & OP_NORESMNT)
   1349 					hp->ht_flag |= DP_NORESMNT;
   1350 				hp->ht_grp = grp;
   1351 				hp->ht_next = ep->ex_defdir->dp_hosts;
   1352 				ep->ex_defdir->dp_hosts = hp;
   1353 				grp = grp->gr_next;
   1354 			}
   1355 	} else {
   1356 
   1357 		/*
   1358 		 * Loop through the directories adding them to the tree.
   1359 		 */
   1360 		while (dp) {
   1361 			dp2 = dp->dp_left;
   1362 			add_dlist(&ep->ex_dirl, dp, grp, flags);
   1363 			dp = dp2;
   1364 		}
   1365 	}
   1366 }
   1367 
   1368 /*
   1369  * Traverse the binary tree either updating a node that is already there
   1370  * for the new directory or adding the new node.
   1371  */
   1372 static void
   1373 add_dlist(dpp, newdp, grp, flags)
   1374 	struct dirlist **dpp;
   1375 	struct dirlist *newdp;
   1376 	struct grouplist *grp;
   1377 	int flags;
   1378 {
   1379 	struct dirlist *dp;
   1380 	struct hostlist *hp;
   1381 	int cmp;
   1382 
   1383 	dp = *dpp;
   1384 	if (dp) {
   1385 		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
   1386 		if (cmp > 0) {
   1387 			add_dlist(&dp->dp_left, newdp, grp, flags);
   1388 			return;
   1389 		} else if (cmp < 0) {
   1390 			add_dlist(&dp->dp_right, newdp, grp, flags);
   1391 			return;
   1392 		} else
   1393 			free(newdp);
   1394 	} else {
   1395 		dp = newdp;
   1396 		dp->dp_left = NULL;
   1397 		*dpp = dp;
   1398 	}
   1399 	if (grp) {
   1400 
   1401 		/*
   1402 		 * Hang all of the host(s) off of the directory point.
   1403 		 */
   1404 		do {
   1405 			hp = get_ht();
   1406 			if (flags & OP_KERB)
   1407 				hp->ht_flag |= DP_KERB;
   1408 			if (flags & OP_NORESMNT)
   1409 				hp->ht_flag |= DP_NORESMNT;
   1410 			hp->ht_grp = grp;
   1411 			hp->ht_next = dp->dp_hosts;
   1412 			dp->dp_hosts = hp;
   1413 			grp = grp->gr_next;
   1414 		} while (grp);
   1415 	} else {
   1416 		dp->dp_flag |= DP_DEFSET;
   1417 		if (flags & OP_KERB)
   1418 			dp->dp_flag |= DP_KERB;
   1419 		if (flags & OP_NORESMNT)
   1420 			dp->dp_flag |= DP_NORESMNT;
   1421 	}
   1422 }
   1423 
   1424 /*
   1425  * Search for a dirpath on the export point.
   1426  */
   1427 static struct dirlist *
   1428 dirp_search(dp, dirp)
   1429 	struct dirlist *dp;
   1430 	char *dirp;
   1431 {
   1432 	int cmp;
   1433 
   1434 	if (dp) {
   1435 		cmp = strcmp(dp->dp_dirp, dirp);
   1436 		if (cmp > 0)
   1437 			return (dirp_search(dp->dp_left, dirp));
   1438 		else if (cmp < 0)
   1439 			return (dirp_search(dp->dp_right, dirp));
   1440 		else
   1441 			return (dp);
   1442 	}
   1443 	return (dp);
   1444 }
   1445 
   1446 /*
   1447  * Some helper functions for netmasks. They all assume masks in network
   1448  * order (big endian).
   1449  */
   1450 static int
   1451 bitcmp(void *dst, void *src, int bitlen)
   1452 {
   1453 	int i;
   1454 	u_int8_t *p1 = dst, *p2 = src;
   1455 	u_int8_t bitmask;
   1456 	int bytelen, bitsleft;
   1457 
   1458 	bytelen = bitlen / 8;
   1459 	bitsleft = bitlen % 8;
   1460 
   1461 	if (debug) {
   1462 		printf("comparing:\n");
   1463 		for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
   1464 			printf("%02x", p1[i]);
   1465 		printf("\n");
   1466 		for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
   1467 			printf("%02x", p2[i]);
   1468 		printf("\n");
   1469 	}
   1470 
   1471 	for (i = 0; i < bytelen; i++) {
   1472 		if (*p1 != *p2)
   1473 			return 1;
   1474 		p1++;
   1475 		p2++;
   1476 	}
   1477 
   1478 	for (i = 0; i < bitsleft; i++) {
   1479 		bitmask = 1 << (7 - i);
   1480 		if ((*p1 & bitmask) != (*p2 & bitmask))
   1481 			return 1;
   1482 	}
   1483 
   1484 	return 0;
   1485 }
   1486 
   1487 static int
   1488 netpartcmp(struct sockaddr *s1, struct sockaddr *s2, int bitlen)
   1489 {
   1490 	void *src, *dst;
   1491 
   1492 	if (s1->sa_family != s2->sa_family)
   1493 		return 1;
   1494 
   1495 	switch (s1->sa_family) {
   1496 	case AF_INET:
   1497 		src = &((struct sockaddr_in *)s1)->sin_addr;
   1498 		dst = &((struct sockaddr_in *)s2)->sin_addr;
   1499 		if (bitlen > sizeof(((struct sockaddr_in *)s1)->sin_addr) * 8)
   1500 			return 1;
   1501 		break;
   1502 	case AF_INET6:
   1503 		src = &((struct sockaddr_in6 *)s1)->sin6_addr;
   1504 		dst = &((struct sockaddr_in6 *)s2)->sin6_addr;
   1505 		if (((struct sockaddr_in6 *)s1)->sin6_scope_id !=
   1506 		    ((struct sockaddr_in6 *)s2)->sin6_scope_id)
   1507 			return 1;
   1508 		if (bitlen > sizeof(((struct sockaddr_in6 *)s1)->sin6_addr) * 8)
   1509 			return 1;
   1510 		break;
   1511 	default:
   1512 		return 1;
   1513 	}
   1514 
   1515 	return bitcmp(src, dst, bitlen);
   1516 }
   1517 
   1518 static int
   1519 allones(struct sockaddr_storage *ssp, int bitlen)
   1520 {
   1521 	u_int8_t *p;
   1522 	int bytelen, bitsleft, i;
   1523 	int zerolen;
   1524 
   1525 	switch (ssp->ss_family) {
   1526 	case AF_INET:
   1527 		p = (u_int8_t *)&((struct sockaddr_in *)ssp)->sin_addr;
   1528 		zerolen = sizeof (((struct sockaddr_in *)ssp)->sin_addr);
   1529 		break;
   1530 	case AF_INET6:
   1531 		p = (u_int8_t *)&((struct sockaddr_in6 *)ssp)->sin6_addr;
   1532 		zerolen = sizeof (((struct sockaddr_in6 *)ssp)->sin6_addr);
   1533 		break;
   1534 	default:
   1535 		return -1;
   1536 	}
   1537 
   1538 	memset(p, 0, zerolen);
   1539 
   1540 	bytelen = bitlen / 8;
   1541 	bitsleft = bitlen % 8;
   1542 
   1543 	if (bytelen > zerolen)
   1544 		return -1;
   1545 
   1546 	for (i = 0; i < bytelen; i++)
   1547 		*p++ = 0xff;
   1548 
   1549 	for (i = 0; i < bitsleft; i++)
   1550 		*p |= 1 << (7 - i);
   1551 
   1552 	return 0;
   1553 }
   1554 
   1555 static int
   1556 countones(struct sockaddr *sa)
   1557 {
   1558 	void *mask;
   1559 	int i, bits = 0, bytelen;
   1560 	u_int8_t *p;
   1561 
   1562 	switch (sa->sa_family) {
   1563 	case AF_INET:
   1564 		mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr;
   1565 		bytelen = 4;
   1566 		break;
   1567 	case AF_INET6:
   1568 		mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr;
   1569 		bytelen = 16;
   1570 		break;
   1571 	default:
   1572 		return 0;
   1573 	}
   1574 
   1575 	p = mask;
   1576 
   1577 	for (i = 0; i < bytelen; i++, p++) {
   1578 		if (*p != 0xff) {
   1579 			for (bits = 0; bits < 8; bits++) {
   1580 				if (!(*p & (1 << (7 - bits))))
   1581 					break;
   1582 			}
   1583 			break;
   1584 		}
   1585 	}
   1586 
   1587 	return (i * 8 + bits);
   1588 }
   1589 
   1590 static int
   1591 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
   1592 {
   1593 	void *p1, *p2;
   1594 	int len;
   1595 
   1596 	if (sa1->sa_family != sa2->sa_family)
   1597 		return 1;
   1598 
   1599 	switch (sa1->sa_family) {
   1600 	case AF_INET:
   1601 		p1 = &((struct sockaddr_in *)sa1)->sin_addr;
   1602 		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
   1603 		len = 4;
   1604 		break;
   1605 	case AF_INET6:
   1606 		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
   1607 		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
   1608 		len = 16;
   1609 		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
   1610 		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
   1611 			return 1;
   1612 		break;
   1613 	default:
   1614 		return 1;
   1615 	}
   1616 
   1617 	return memcmp(p1, p2, len);
   1618 }
   1619 
   1620 /*
   1621  * Scan for a host match in a directory tree.
   1622  */
   1623 static int
   1624 chk_host(dp, saddr, defsetp, hostsetp)
   1625 	struct dirlist *dp;
   1626 	struct sockaddr *saddr;
   1627 	int *defsetp;
   1628 	int *hostsetp;
   1629 {
   1630 	struct hostlist *hp;
   1631 	struct grouplist *grp;
   1632 	struct addrinfo *ai;
   1633 
   1634 	if (dp) {
   1635 		if (dp->dp_flag & DP_DEFSET)
   1636 			*defsetp = dp->dp_flag;
   1637 		hp = dp->dp_hosts;
   1638 		while (hp) {
   1639 			grp = hp->ht_grp;
   1640 			switch (grp->gr_type) {
   1641 			case GT_HOST:
   1642 				ai = grp->gr_ptr.gt_addrinfo;
   1643 				for (; ai; ai = ai->ai_next) {
   1644 					if (!sacmp(ai->ai_addr, saddr)) {
   1645 						*hostsetp =
   1646 						    (hp->ht_flag | DP_HOSTSET);
   1647 						return (1);
   1648 					}
   1649 				}
   1650 				break;
   1651 			case GT_NET:
   1652 				if (!netpartcmp(saddr,
   1653 				    (struct sockaddr *)
   1654 					&grp->gr_ptr.gt_net.nt_net,
   1655 				    grp->gr_ptr.gt_net.nt_len)) {
   1656 					*hostsetp = (hp->ht_flag | DP_HOSTSET);
   1657 					return (1);
   1658 				}
   1659 				break;
   1660 			};
   1661 			hp = hp->ht_next;
   1662 		}
   1663 	}
   1664 	return (0);
   1665 }
   1666 
   1667 /*
   1668  * Scan tree for a host that matches the address.
   1669  */
   1670 static int
   1671 scan_tree(dp, saddr)
   1672 	struct dirlist *dp;
   1673 	struct sockaddr *saddr;
   1674 {
   1675 	int defset, hostset;
   1676 
   1677 	if (dp) {
   1678 		if (scan_tree(dp->dp_left, saddr))
   1679 			return (1);
   1680 		if (chk_host(dp, saddr, &defset, &hostset))
   1681 			return (1);
   1682 		if (scan_tree(dp->dp_right, saddr))
   1683 			return (1);
   1684 	}
   1685 	return (0);
   1686 }
   1687 
   1688 /*
   1689  * Traverse the dirlist tree and free it up.
   1690  */
   1691 static void
   1692 free_dir(dp)
   1693 	struct dirlist *dp;
   1694 {
   1695 
   1696 	if (dp) {
   1697 		free_dir(dp->dp_left);
   1698 		free_dir(dp->dp_right);
   1699 		free_host(dp->dp_hosts);
   1700 		free(dp);
   1701 	}
   1702 }
   1703 
   1704 /*
   1705  * Parse the option string and update fields.
   1706  * Option arguments may either be -<option>=<value> or
   1707  * -<option> <value>
   1708  */
   1709 static int
   1710 do_opt(line, lineno, cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
   1711 	const char *line;
   1712 	size_t lineno;
   1713 	char **cpp, **endcpp;
   1714 	struct exportlist *ep;
   1715 	struct grouplist *grp;
   1716 	int *has_hostp;
   1717 	int *exflagsp;
   1718 	struct uucred *cr;
   1719 {
   1720 	char *cpoptarg, *cpoptend;
   1721 	char *cp, *endcp, *cpopt, savedc, savedc2;
   1722 	int allflag, usedarg;
   1723 
   1724 	cpopt = *cpp;
   1725 	cpopt++;
   1726 	cp = *endcpp;
   1727 	savedc = *cp;
   1728 	*cp = '\0';
   1729 	while (cpopt && *cpopt) {
   1730 		allflag = 1;
   1731 		usedarg = -2;
   1732 		savedc2 = '\0';
   1733 		if ((cpoptend = strchr(cpopt, ',')) != NULL) {
   1734 			*cpoptend++ = '\0';
   1735 			if ((cpoptarg = strchr(cpopt, '=')) != NULL)
   1736 				*cpoptarg++ = '\0';
   1737 		} else {
   1738 			if ((cpoptarg = strchr(cpopt, '=')) != NULL)
   1739 				*cpoptarg++ = '\0';
   1740 			else {
   1741 				*cp = savedc;
   1742 				nextfield(&cp, &endcp);
   1743 				**endcpp = '\0';
   1744 				if (endcp > cp && *cp != '-') {
   1745 					cpoptarg = cp;
   1746 					savedc2 = *endcp;
   1747 					*endcp = '\0';
   1748 					usedarg = 0;
   1749 				}
   1750 			}
   1751 		}
   1752 		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
   1753 			*exflagsp |= MNT_EXRDONLY;
   1754 		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
   1755 		    !(allflag = strcmp(cpopt, "mapall")) ||
   1756 		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
   1757 			usedarg++;
   1758 			parsecred(cpoptarg, cr);
   1759 			if (allflag == 0) {
   1760 				*exflagsp |= MNT_EXPORTANON;
   1761 				opt_flags |= OP_MAPALL;
   1762 			} else
   1763 				opt_flags |= OP_MAPROOT;
   1764 		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
   1765 			*exflagsp |= MNT_EXKERB;
   1766 			opt_flags |= OP_KERB;
   1767 		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
   1768 		    !strcmp(cpopt, "m"))) {
   1769 			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
   1770 				syslog(LOG_ERR,
   1771 				    "\"%s\", line %ld: Bad mask: %s",
   1772 				    line, (unsigned long)lineno, cpoptarg);
   1773 				return (1);
   1774 			}
   1775 			usedarg++;
   1776 			opt_flags |= OP_MASK;
   1777 		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
   1778 		    !strcmp(cpopt, "n"))) {
   1779 			if (strchr(cpoptarg, '/') != NULL) {
   1780 				if (debug)
   1781 					fprintf(stderr, "setting OP_MASKLEN\n");
   1782 				opt_flags |= OP_MASKLEN;
   1783 			}
   1784 			if (grp->gr_type != GT_NULL) {
   1785 				syslog(LOG_ERR,
   1786 				    "\"%s\", line %ld: Network/host conflict",
   1787 				    line, (unsigned long)lineno);
   1788 				return (1);
   1789 			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
   1790 				syslog(LOG_ERR,
   1791 				    "\"%s\", line %ld: Bad net: %s",
   1792 				    line, (unsigned long)lineno, cpoptarg);
   1793 				return (1);
   1794 			}
   1795 			grp->gr_type = GT_NET;
   1796 			*has_hostp = 1;
   1797 			usedarg++;
   1798 			opt_flags |= OP_NET;
   1799 		} else if (!strcmp(cpopt, "alldirs")) {
   1800 			opt_flags |= OP_ALLDIRS;
   1801 		} else if (!strcmp(cpopt, "noresvmnt")) {
   1802 			opt_flags |= OP_NORESMNT;
   1803 		} else if (!strcmp(cpopt, "noresvport")) {
   1804 			opt_flags |= OP_NORESPORT;
   1805 			*exflagsp |= MNT_EXNORESPORT;
   1806 		} else if (!strcmp(cpopt, "public")) {
   1807 			*exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC);
   1808 			opt_flags |= OP_NORESPORT;
   1809 		} else if (!strcmp(cpopt, "webnfs")) {
   1810 			*exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC |
   1811 			    MNT_EXRDONLY | MNT_EXPORTANON);
   1812 			opt_flags |= (OP_MAPALL | OP_NORESPORT);
   1813 		} else if (cpoptarg && !strcmp(cpopt, "index")) {
   1814 			ep->ex_indexfile = strdup(cpoptarg);
   1815 #ifdef ISO
   1816 		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
   1817 			if (get_isoaddr(line, lineno, cpoptarg, grp))
   1818 				return (1);
   1819 			*has_hostp = 1;
   1820 			usedarg++;
   1821 			opt_flags |= OP_ISO;
   1822 #endif /* ISO */
   1823 		} else {
   1824 			syslog(LOG_ERR,
   1825 			    "\"%s\", line %ld: Bad opt %s",
   1826 			    line, (unsigned long)lineno, cpopt);
   1827 			return (1);
   1828 		}
   1829 		if (usedarg >= 0) {
   1830 			*endcp = savedc2;
   1831 			**endcpp = savedc;
   1832 			if (usedarg > 0) {
   1833 				*cpp = cp;
   1834 				*endcpp = endcp;
   1835 			}
   1836 			return (0);
   1837 		}
   1838 		cpopt = cpoptend;
   1839 	}
   1840 	**endcpp = savedc;
   1841 	return (0);
   1842 }
   1843 
   1844 /*
   1845  * Translate a character string to the corresponding list of network
   1846  * addresses for a hostname.
   1847  */
   1848 static int
   1849 get_host(line, lineno, cp, grp)
   1850 	const char *line;
   1851 	size_t lineno;
   1852 	const char *cp;
   1853 	struct grouplist *grp;
   1854 {
   1855 	struct addrinfo *ai, hints;
   1856 	int ecode;
   1857 	char host[NI_MAXHOST];
   1858 
   1859 	if (grp->gr_type != GT_NULL) {
   1860 		syslog(LOG_ERR,
   1861 		    "\"%s\", line %ld: Bad netgroup type for ip host %s",
   1862 		    line, (unsigned long)lineno, cp);
   1863 		return (1);
   1864 	}
   1865 	memset(&hints, 0, sizeof hints);
   1866 	hints.ai_flags = AI_CANONNAME;
   1867 	hints.ai_protocol = IPPROTO_UDP;
   1868 	ecode = getaddrinfo(cp, NULL, &hints, &ai);
   1869 	if (ecode != 0) {
   1870 		syslog(LOG_ERR, "\"%s\", line %ld: can't get address info for "
   1871 				"host %s",
   1872 		    line, (long)lineno, cp);
   1873 		return 1;
   1874 	}
   1875 	grp->gr_type = GT_HOST;
   1876 	grp->gr_ptr.gt_addrinfo = ai;
   1877 	while (ai != NULL) {
   1878 		if (ai->ai_canonname == NULL) {
   1879 			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
   1880 			    sizeof host, NULL, 0, ninumeric) != 0)
   1881 				strlcpy(host, "?", sizeof(host));
   1882 			ai->ai_canonname = estrdup(host);
   1883 			ai->ai_flags |= AI_CANONNAME;
   1884 		} else
   1885 			ai->ai_flags &= ~AI_CANONNAME;
   1886 		if (debug)
   1887 			(void)fprintf(stderr, "got host %s\n", ai->ai_canonname);
   1888 		ai = ai->ai_next;
   1889 	}
   1890 	return (0);
   1891 }
   1892 
   1893 /*
   1894  * Free up an exports list component
   1895  */
   1896 static void
   1897 free_exp(ep)
   1898 	struct exportlist *ep;
   1899 {
   1900 
   1901 	if (ep->ex_defdir) {
   1902 		free_host(ep->ex_defdir->dp_hosts);
   1903 		free(ep->ex_defdir);
   1904 	}
   1905 	if (ep->ex_fsdir)
   1906 		free(ep->ex_fsdir);
   1907 	if (ep->ex_indexfile)
   1908 		free(ep->ex_indexfile);
   1909 	free_dir(ep->ex_dirl);
   1910 	free(ep);
   1911 }
   1912 
   1913 /*
   1914  * Free hosts.
   1915  */
   1916 static void
   1917 free_host(hp)
   1918 	struct hostlist *hp;
   1919 {
   1920 	struct hostlist *hp2;
   1921 
   1922 	while (hp) {
   1923 		hp2 = hp;
   1924 		hp = hp->ht_next;
   1925 		free(hp2);
   1926 	}
   1927 }
   1928 
   1929 static struct hostlist *
   1930 get_ht()
   1931 {
   1932 	struct hostlist *hp;
   1933 
   1934 	hp = emalloc(sizeof(struct hostlist));
   1935 	hp->ht_next = NULL;
   1936 	hp->ht_flag = 0;
   1937 	return (hp);
   1938 }
   1939 
   1940 #ifdef ISO
   1941 /*
   1942  * Translate an iso address.
   1943  */
   1944 static int
   1945 get_isoaddr(line, lineno, cp, grp)
   1946 	const char *line;
   1947 	size_t lineno;
   1948 	char *cp;
   1949 	struct grouplist *grp;
   1950 {
   1951 	struct iso_addr *isop;
   1952 	struct sockaddr_iso *isoaddr;
   1953 
   1954 	if (grp->gr_type != GT_NULL) {
   1955 		syslog(LOG_ERR,
   1956 		    "\"%s\", line %ld: Bad netgroup type for iso addr %s",
   1957 		    line, (unsigned long)lineno, cp);
   1958 		return (1);
   1959 	}
   1960 	if ((isop = iso_addr(cp)) == NULL) {
   1961 		syslog(LOG_ERR,
   1962 		    "\"%s\", line %ld: Bad iso addr %s",
   1963 		    line, (unsigned long)lineno, cp);
   1964 		return (1);
   1965 	}
   1966 	isoaddr = emalloc(sizeof(struct sockaddr_iso));
   1967 	(void)memset(isoaddr, 0, sizeof(struct sockaddr_iso));
   1968 	(void)memcpy(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
   1969 	isoaddr->siso_len = sizeof(struct sockaddr_iso);
   1970 	isoaddr->siso_family = AF_ISO;
   1971 	grp->gr_type = GT_ISO;
   1972 	grp->gr_ptr.gt_isoaddr = isoaddr;
   1973 	return (0);
   1974 }
   1975 #endif				/* ISO */
   1976 
   1977 /*
   1978  * error checked malloc and strdup
   1979  */
   1980 static void *
   1981 emalloc(n)
   1982 	size_t n;
   1983 {
   1984 	void *ptr = malloc(n);
   1985 
   1986 	if (ptr == NULL) {
   1987 		syslog(LOG_ERR, "%m");
   1988 		exit(2);
   1989 	}
   1990 	return ptr;
   1991 }
   1992 
   1993 static char *
   1994 estrdup(s)
   1995 	const char *s;
   1996 {
   1997 	char *n = strdup(s);
   1998 
   1999 	if (n == NULL) {
   2000 		syslog(LOG_ERR, "%m");
   2001 		exit(2);
   2002 	}
   2003 	return n;
   2004 }
   2005 
   2006 /*
   2007  * Do the mount syscall with the update flag to push the export info into
   2008  * the kernel.
   2009  */
   2010 static int
   2011 do_mount(line, lineno, ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
   2012 	const char *line;
   2013 	size_t lineno;
   2014 	struct exportlist *ep;
   2015 	struct grouplist *grp;
   2016 	int exflags;
   2017 	struct uucred *anoncrp;
   2018 	char *dirp;
   2019 	int dirplen;
   2020 	struct statvfs *fsb;
   2021 {
   2022 	struct sockaddr *addrp;
   2023 	struct sockaddr_storage ss;
   2024 	struct addrinfo *ai;
   2025 	int addrlen;
   2026 	char *cp = NULL;
   2027 	int done;
   2028 	char savedc = '\0';
   2029 	union {
   2030 		struct ufs_args ua;
   2031 		struct iso_args ia;
   2032 		struct mfs_args ma;
   2033 		struct msdosfs_args da;
   2034 		struct adosfs_args aa;
   2035 	} args;
   2036 
   2037 	args.ua.fspec = 0;
   2038 	args.ua.export.ex_flags = exflags;
   2039 	args.ua.export.ex_anon = *anoncrp;
   2040 	args.ua.export.ex_indexfile = ep->ex_indexfile;
   2041 	if (grp->gr_type == GT_HOST) {
   2042 		ai = grp->gr_ptr.gt_addrinfo;
   2043 		addrp = ai->ai_addr;
   2044 		addrlen = ai->ai_addrlen;
   2045 	} else
   2046 		addrp = NULL;
   2047 	done = FALSE;
   2048 	while (!done) {
   2049 		switch (grp->gr_type) {
   2050 		case GT_HOST:
   2051 			if (addrp != NULL && addrp->sa_family == AF_INET6 &&
   2052 			    have_v6 == 0)
   2053 				goto skip;
   2054 			args.ua.export.ex_addr = addrp;
   2055 			args.ua.export.ex_addrlen = addrlen;
   2056 			args.ua.export.ex_masklen = 0;
   2057 			break;
   2058 		case GT_NET:
   2059 			args.ua.export.ex_addr = (struct sockaddr *)
   2060 			    &grp->gr_ptr.gt_net.nt_net;
   2061 			if (args.ua.export.ex_addr->sa_family == AF_INET6 &&
   2062 			    have_v6 == 0)
   2063 				goto skip;
   2064 			args.ua.export.ex_addrlen =
   2065 			    args.ua.export.ex_addr->sa_len;
   2066 			memset(&ss, 0, sizeof ss);
   2067 			ss.ss_family = args.ua.export.ex_addr->sa_family;
   2068 			ss.ss_len = args.ua.export.ex_addr->sa_len;
   2069 			if (allones(&ss, grp->gr_ptr.gt_net.nt_len) != 0) {
   2070 				syslog(LOG_ERR,
   2071 				    "\"%s\", line %ld: Bad network flag",
   2072 				    line, (unsigned long)lineno);
   2073 				if (cp)
   2074 					*cp = savedc;
   2075 				return (1);
   2076 			}
   2077 			args.ua.export.ex_mask = (struct sockaddr *)&ss;
   2078 			args.ua.export.ex_masklen = ss.ss_len;
   2079 			break;
   2080 #ifdef ISO
   2081 		case GT_ISO:
   2082 			args.ua.export.ex_addr =
   2083 			    (struct sockaddr *) grp->gr_ptr.gt_isoaddr;
   2084 			args.ua.export.ex_addrlen =
   2085 			    sizeof(struct sockaddr_iso);
   2086 			args.ua.export.ex_masklen = 0;
   2087 			break;
   2088 #endif				/* ISO */
   2089 		default:
   2090 			syslog(LOG_ERR, "\"%s\", line %ld: Bad netgroup type",
   2091 			    line, (unsigned long)lineno);
   2092 			if (cp)
   2093 				*cp = savedc;
   2094 			return (1);
   2095 		};
   2096 
   2097 		/*
   2098 		 * XXX:
   2099 		 * Maybe I should just use the fsb->f_mntonname path instead
   2100 		 * of looping back up the dirp to the mount point??
   2101 		 * Also, needs to know how to export all types of local
   2102 		 * exportable file systems and not just MOUNT_FFS.
   2103 		 */
   2104 		while (mount(fsb->f_fstypename, dirp,
   2105 		    fsb->f_flag | MNT_UPDATE, &args) == -1) {
   2106 			if (cp)
   2107 				*cp-- = savedc;
   2108 			else
   2109 				cp = dirp + dirplen - 1;
   2110 			if (errno == EPERM) {
   2111 				syslog(LOG_ERR,
   2112 		    "\"%s\", line %ld: Can't change attributes for %s to %s: %m",
   2113 				    line, (unsigned long)lineno,
   2114 				    dirp, (grp->gr_type == GT_HOST) ?
   2115 				    grp->gr_ptr.gt_addrinfo->ai_canonname :
   2116 				    (grp->gr_type == GT_NET) ?
   2117 				    grp->gr_ptr.gt_net.nt_name :
   2118 				    "Unknown");
   2119 				return (1);
   2120 			}
   2121 			if (opt_flags & OP_ALLDIRS) {
   2122 				syslog(LOG_ERR,
   2123 				"\"%s\", line %ld: Could not remount %s: %m",
   2124 				    line, (unsigned long)lineno,
   2125 				    dirp);
   2126 				return (1);
   2127 			}
   2128 			/* back up over the last component */
   2129 			while (*cp == '/' && cp > dirp)
   2130 				cp--;
   2131 			while (*(cp - 1) != '/' && cp > dirp)
   2132 				cp--;
   2133 			if (cp == dirp) {
   2134 				if (debug)
   2135 					(void)fprintf(stderr, "mnt unsucc\n");
   2136 				syslog(LOG_ERR,
   2137 				    "\"%s\", line %ld: Can't export %s: %m",
   2138 				    line, (unsigned long)lineno, dirp);
   2139 				return (1);
   2140 			}
   2141 			savedc = *cp;
   2142 			*cp = '\0';
   2143 		}
   2144 skip:
   2145 		if (addrp) {
   2146 			ai = ai->ai_next;
   2147 			if (ai == NULL)
   2148 				done = TRUE;
   2149 			else {
   2150 				addrp = ai->ai_addr;
   2151 				addrlen = ai->ai_addrlen;
   2152 			}
   2153 		} else
   2154 			done = TRUE;
   2155 	}
   2156 	if (cp)
   2157 		*cp = savedc;
   2158 	return (0);
   2159 }
   2160 
   2161 /*
   2162  * Translate a net address.
   2163  */
   2164 static int
   2165 get_net(cp, net, maskflg)
   2166 	char *cp;
   2167 	struct netmsk *net;
   2168 	int maskflg;
   2169 {
   2170 	struct netent *np;
   2171 	char *name, *p, *prefp;
   2172 	struct sockaddr_in sin, *sinp;
   2173 	struct sockaddr *sa;
   2174 	struct addrinfo hints, *ai = NULL;
   2175 	char netname[NI_MAXHOST];
   2176 	long preflen;
   2177 	int ecode;
   2178 
   2179 	(void)memset(&sin, 0, sizeof(sin));
   2180 	if ((opt_flags & OP_MASKLEN) && !maskflg) {
   2181 		p = strchr(cp, '/');
   2182 		*p = '\0';
   2183 		prefp = p + 1;
   2184 	}
   2185 
   2186 	if ((np = getnetbyname(cp)) != NULL) {
   2187 		sin.sin_family = AF_INET;
   2188 		sin.sin_len = sizeof sin;
   2189 		sin.sin_addr = inet_makeaddr(np->n_net, 0);
   2190 		sa = (struct sockaddr *)&sin;
   2191 	} else if (isdigit((unsigned char)*cp)) {
   2192 		memset(&hints, 0, sizeof hints);
   2193 		hints.ai_family = AF_UNSPEC;
   2194 		hints.ai_flags = AI_NUMERICHOST;
   2195 		if (getaddrinfo(cp, NULL, &hints, &ai) != 0) {
   2196 			/*
   2197 			 * If getaddrinfo() failed, try the inet4 network
   2198 			 * notation with less than 3 dots.
   2199 			 */
   2200 			sin.sin_family = AF_INET;
   2201 			sin.sin_len = sizeof sin;
   2202 			sin.sin_addr = inet_makeaddr(inet_network(cp),0);
   2203 			if (debug)
   2204 				fprintf(stderr, "get_net: v4 addr %x\n",
   2205 				    sin.sin_addr.s_addr);
   2206 			sa = (struct sockaddr *)&sin;
   2207 		} else
   2208 			sa = ai->ai_addr;
   2209 	} else if (isxdigit((unsigned char)*cp) || *cp == ':') {
   2210 		memset(&hints, 0, sizeof hints);
   2211 		hints.ai_family = AF_UNSPEC;
   2212 		hints.ai_flags = AI_NUMERICHOST;
   2213 		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
   2214 			sa = ai->ai_addr;
   2215 		else
   2216 			goto fail;
   2217 	} else
   2218 		goto fail;
   2219 
   2220 	/*
   2221 	 * Only allow /pref notation for v6 addresses.
   2222 	 */
   2223 	if (sa->sa_family == AF_INET6 && (!(opt_flags & OP_MASKLEN) || maskflg))
   2224 		return 1;
   2225 
   2226 	ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname,
   2227 	    NULL, 0, ninumeric);
   2228 	if (ecode != 0)
   2229 		goto fail;
   2230 
   2231 	if (maskflg)
   2232 		net->nt_len = countones(sa);
   2233 	else {
   2234 		if (opt_flags & OP_MASKLEN) {
   2235 			errno = 0;
   2236 			preflen = strtol(prefp, NULL, 10);
   2237 			if (preflen == LONG_MIN && errno == ERANGE)
   2238 				goto fail;
   2239 			net->nt_len = (int)preflen;
   2240 			*p = '/';
   2241 		}
   2242 
   2243 		if (np)
   2244 			name = np->n_name;
   2245 		else {
   2246 			if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
   2247 			    NULL, 0, ninumeric) != 0)
   2248 				strlcpy(netname, "?", sizeof(netname));
   2249 			name = netname;
   2250 		}
   2251 		net->nt_name = estrdup(name);
   2252 		memcpy(&net->nt_net, sa, sa->sa_len);
   2253 	}
   2254 
   2255 	if (!maskflg && sa->sa_family == AF_INET &&
   2256 	    !(opt_flags & (OP_MASK|OP_MASKLEN))) {
   2257 		sinp = (struct sockaddr_in *)sa;
   2258 		if (IN_CLASSA(sinp->sin_addr.s_addr))
   2259 			net->nt_len = 8;
   2260 		else if (IN_CLASSB(sinp->sin_addr.s_addr))
   2261 			net->nt_len = 16;
   2262 		else if (IN_CLASSC(sinp->sin_addr.s_addr))
   2263 			net->nt_len = 24;
   2264 		else if (IN_CLASSD(sinp->sin_addr.s_addr))
   2265 			net->nt_len = 28;
   2266 		else
   2267 			net->nt_len = 32;	/* XXX */
   2268 	}
   2269 
   2270 	if (ai)
   2271 		freeaddrinfo(ai);
   2272 	return 0;
   2273 
   2274 fail:
   2275 	if (ai)
   2276 		freeaddrinfo(ai);
   2277 	return 1;
   2278 }
   2279 
   2280 /*
   2281  * Parse out the next white space separated field
   2282  */
   2283 static void
   2284 nextfield(cp, endcp)
   2285 	char **cp;
   2286 	char **endcp;
   2287 {
   2288 	char *p;
   2289 
   2290 	p = *cp;
   2291 	while (*p == ' ' || *p == '\t')
   2292 		p++;
   2293 	if (*p == '\n' || *p == '\0')
   2294 		*cp = *endcp = p;
   2295 	else {
   2296 		*cp = p++;
   2297 		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
   2298 			p++;
   2299 		*endcp = p;
   2300 	}
   2301 }
   2302 
   2303 /*
   2304  * Parse a description of a credential.
   2305  */
   2306 static void
   2307 parsecred(namelist, cr)
   2308 	char *namelist;
   2309 	struct uucred *cr;
   2310 {
   2311 	char *name;
   2312 	int cnt;
   2313 	char *names;
   2314 	struct passwd *pw;
   2315 	struct group *gr;
   2316 	int ngroups, groups[NGROUPS + 1];
   2317 
   2318 	/*
   2319 	 * Set up the unprivileged user.
   2320 	 */
   2321 	*cr = def_anon;
   2322 	/*
   2323 	 * Get the user's password table entry.
   2324 	 */
   2325 	names = strsep(&namelist, " \t\n");
   2326 	name = strsep(&names, ":");
   2327 	if (isdigit((unsigned char)*name) || *name == '-')
   2328 		pw = getpwuid(atoi(name));
   2329 	else
   2330 		pw = getpwnam(name);
   2331 	/*
   2332 	 * Credentials specified as those of a user.
   2333 	 */
   2334 	if (names == NULL) {
   2335 		if (pw == NULL) {
   2336 			syslog(LOG_ERR, "Unknown user: %s", name);
   2337 			return;
   2338 		}
   2339 		cr->cr_uid = pw->pw_uid;
   2340 		ngroups = NGROUPS + 1;
   2341 		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
   2342 			syslog(LOG_ERR, "Too many groups");
   2343 		/*
   2344 		 * Convert from int's to gid_t's and compress out duplicate
   2345 		 */
   2346 		cr->cr_ngroups = ngroups - 1;
   2347 		cr->cr_gid = groups[0];
   2348 		for (cnt = 1; cnt < ngroups; cnt++)
   2349 			cr->cr_groups[cnt - 1] = groups[cnt];
   2350 		return;
   2351 	}
   2352 	/*
   2353 	 * Explicit credential specified as a colon separated list:
   2354 	 *	uid:gid:gid:...
   2355 	 */
   2356 	if (pw != NULL)
   2357 		cr->cr_uid = pw->pw_uid;
   2358 	else if (isdigit((unsigned char)*name) || *name == '-')
   2359 		cr->cr_uid = atoi(name);
   2360 	else {
   2361 		syslog(LOG_ERR, "Unknown user: %s", name);
   2362 		return;
   2363 	}
   2364 	cr->cr_ngroups = 0;
   2365 	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
   2366 		name = strsep(&names, ":");
   2367 		if (isdigit((unsigned char)*name) || *name == '-') {
   2368 			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
   2369 		} else {
   2370 			if ((gr = getgrnam(name)) == NULL) {
   2371 				syslog(LOG_ERR, "Unknown group: %s", name);
   2372 				continue;
   2373 			}
   2374 			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
   2375 		}
   2376 	}
   2377 	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
   2378 		syslog(LOG_ERR, "Too many groups");
   2379 }
   2380 
   2381 #define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
   2382 /*
   2383  * Routines that maintain the remote mounttab
   2384  */
   2385 static void
   2386 get_mountlist()
   2387 {
   2388 	struct mountlist *mlp, **mlpp;
   2389 	char *host, *dirp, *cp;
   2390 	char str[STRSIZ];
   2391 	FILE *mlfile;
   2392 
   2393 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
   2394 		syslog(LOG_ERR, "Can't open %s: %m", _PATH_RMOUNTLIST);
   2395 		return;
   2396 	}
   2397 	mlpp = &mlhead;
   2398 	while (fgets(str, STRSIZ, mlfile) != NULL) {
   2399 		cp = str;
   2400 		host = strsep(&cp, " \t\n");
   2401 		dirp = strsep(&cp, " \t\n");
   2402 		if (host == NULL || dirp == NULL)
   2403 			continue;
   2404 		mlp = emalloc(sizeof(*mlp));
   2405 		(void)strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
   2406 		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
   2407 		(void)strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
   2408 		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
   2409 		mlp->ml_next = NULL;
   2410 		*mlpp = mlp;
   2411 		mlpp = &mlp->ml_next;
   2412 	}
   2413 	(void)fclose(mlfile);
   2414 }
   2415 
   2416 static int
   2417 del_mlist(hostp, dirp, saddr)
   2418 	char *hostp, *dirp;
   2419 	struct sockaddr *saddr;
   2420 {
   2421 	struct mountlist *mlp, **mlpp;
   2422 	struct mountlist *mlp2;
   2423 	u_short sport;
   2424 	FILE *mlfile;
   2425 	int fnd = 0, ret = 0;
   2426 	char host[NI_MAXHOST];
   2427 
   2428 	switch (saddr->sa_family) {
   2429 	case AF_INET6:
   2430 		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
   2431 		break;
   2432 	case AF_INET:
   2433 		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
   2434 		break;
   2435 	default:
   2436 		return -1;
   2437 	}
   2438 	mlpp = &mlhead;
   2439 	mlp = mlhead;
   2440 	while (mlp) {
   2441 		if (!strcmp(mlp->ml_host, hostp) &&
   2442 		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
   2443 			if (!(mlp->ml_flag & DP_NORESMNT) &&
   2444 			    sport >= IPPORT_RESERVED) {
   2445 				if (getnameinfo(saddr, saddr->sa_len, host,
   2446 				    sizeof host, NULL, 0, ninumeric) != 0)
   2447 					strlcpy(host, "?", sizeof(host));
   2448 				syslog(LOG_NOTICE,
   2449 				"Umount request for %s:%s from %s refused\n",
   2450 				    mlp->ml_host, mlp->ml_dirp, host);
   2451 				ret = -1;
   2452 				goto cont;
   2453 			}
   2454 			fnd = 1;
   2455 			mlp2 = mlp;
   2456 			*mlpp = mlp = mlp->ml_next;
   2457 			free(mlp2);
   2458 		} else {
   2459 cont:
   2460 			mlpp = &mlp->ml_next;
   2461 			mlp = mlp->ml_next;
   2462 		}
   2463 	}
   2464 	if (fnd) {
   2465 		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
   2466 			syslog(LOG_ERR, "Can't update %s: %m",
   2467 			    _PATH_RMOUNTLIST);
   2468 			return ret;
   2469 		}
   2470 		mlp = mlhead;
   2471 		while (mlp) {
   2472 			(void)fprintf(mlfile, "%s %s\n", mlp->ml_host,
   2473 		            mlp->ml_dirp);
   2474 			mlp = mlp->ml_next;
   2475 		}
   2476 		(void)fclose(mlfile);
   2477 	}
   2478 	return ret;
   2479 }
   2480 
   2481 static void
   2482 add_mlist(hostp, dirp, flags)
   2483 	char *hostp, *dirp;
   2484 	int flags;
   2485 {
   2486 	struct mountlist *mlp, **mlpp;
   2487 	FILE *mlfile;
   2488 
   2489 	mlpp = &mlhead;
   2490 	mlp = mlhead;
   2491 	while (mlp) {
   2492 		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
   2493 			return;
   2494 		mlpp = &mlp->ml_next;
   2495 		mlp = mlp->ml_next;
   2496 	}
   2497 	mlp = emalloc(sizeof(*mlp));
   2498 	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
   2499 	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
   2500 	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
   2501 	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
   2502 	mlp->ml_flag = flags;
   2503 	mlp->ml_next = NULL;
   2504 	*mlpp = mlp;
   2505 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
   2506 		syslog(LOG_ERR, "Can't update %s: %m", _PATH_RMOUNTLIST);
   2507 		return;
   2508 	}
   2509 	(void)fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
   2510 	(void)fclose(mlfile);
   2511 }
   2512 
   2513 /*
   2514  * This function is called via. SIGTERM when the system is going down.
   2515  * It sends a broadcast RPCMNT_UMNTALL.
   2516  */
   2517 /* ARGSUSED */
   2518 static void
   2519 send_umntall(n)
   2520 	int n;
   2521 {
   2522 	(void)clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
   2523 	    xdr_void, NULL, xdr_void, NULL, (resultproc_t)umntall_each);
   2524 	exit(0);
   2525 }
   2526 
   2527 static int
   2528 umntall_each(resultsp, raddr)
   2529 	caddr_t resultsp;
   2530 	struct sockaddr_in *raddr;
   2531 {
   2532 	return (1);
   2533 }
   2534 
   2535 /*
   2536  * Free up a group list.
   2537  */
   2538 static void
   2539 free_grp(grp)
   2540 	struct grouplist *grp;
   2541 {
   2542 
   2543 	if (grp->gr_type == GT_HOST) {
   2544 		if (grp->gr_ptr.gt_addrinfo != NULL)
   2545 			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
   2546 	} else if (grp->gr_type == GT_NET) {
   2547 		if (grp->gr_ptr.gt_net.nt_name)
   2548 			free(grp->gr_ptr.gt_net.nt_name);
   2549 	}
   2550 #ifdef ISO
   2551 	else if (grp->gr_type == GT_ISO)
   2552 		free(grp->gr_ptr.gt_isoaddr);
   2553 #endif
   2554 	free(grp);
   2555 }
   2556 
   2557 #if 0
   2558 static void
   2559 SYSLOG(int pri, const char *fmt,...)
   2560 {
   2561 	va_list ap;
   2562 
   2563 	va_start(ap, fmt);
   2564 
   2565 	if (debug)
   2566 		vfprintf(stderr, fmt, ap);
   2567 	else
   2568 		vsyslog(pri, fmt, ap);
   2569 
   2570 	va_end(ap);
   2571 }
   2572 #endif
   2573 
   2574 /*
   2575  * Check options for consistency.
   2576  */
   2577 static int
   2578 check_options(line, lineno, dp)
   2579 	const char *line;
   2580 	size_t lineno;
   2581 	struct dirlist *dp;
   2582 {
   2583 
   2584 	if (dp == NULL) {
   2585 		syslog(LOG_ERR,
   2586 		    "\"%s\", line %ld: missing directory list",
   2587 		    line, (unsigned long)lineno);
   2588 		return (1);
   2589 	}
   2590 	if ((opt_flags & (OP_MAPROOT|OP_MAPALL)) == (OP_MAPROOT|OP_MAPALL) ||
   2591 	    (opt_flags & (OP_MAPROOT|OP_KERB)) == (OP_MAPROOT|OP_KERB) ||
   2592 	    (opt_flags & (OP_MAPALL|OP_KERB)) == (OP_MAPALL|OP_KERB)) {
   2593 		syslog(LOG_ERR,
   2594 		    "\"%s\", line %ld: -mapall, -maproot and -kerb mutually exclusive",
   2595 		    line, (unsigned long)lineno);
   2596 		return (1);
   2597 	}
   2598 	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
   2599 		syslog(LOG_ERR, "\"%s\", line %ld: -mask requires -net",
   2600 		    line, (unsigned long)lineno);
   2601 		return (1);
   2602 	}
   2603 	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN) != 0) {
   2604 		syslog(LOG_ERR, "\"%s\", line %ld: /pref and -mask mutually"
   2605 		    " exclusive",
   2606 		    line, (unsigned long)lineno);
   2607 		return (1);
   2608 	}
   2609 	if ((opt_flags & (OP_NET|OP_ISO)) == (OP_NET|OP_ISO)) {
   2610 		syslog(LOG_ERR,
   2611 		    "\"%s\", line %ld: -net and -iso mutually exclusive",
   2612 		    line, (unsigned long)lineno);
   2613 		return (1);
   2614 	}
   2615 	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
   2616 		syslog(LOG_ERR,
   2617 		    "\"%s\", line %ld: -alldir has multiple directories",
   2618 		    line, (unsigned long)lineno);
   2619 		return (1);
   2620 	}
   2621 	return (0);
   2622 }
   2623 
   2624 /*
   2625  * Check an absolute directory path for any symbolic links. Return true
   2626  * if no symbolic links are found.
   2627  */
   2628 static int
   2629 check_dirpath(line, lineno, dirp)
   2630 	const char *line;
   2631 	size_t lineno;
   2632 	char *dirp;
   2633 {
   2634 	char *cp;
   2635 	struct stat sb;
   2636 	char *file = "";
   2637 
   2638 	for (cp = dirp + 1; *cp; cp++) {
   2639 		if (*cp == '/') {
   2640 			*cp = '\0';
   2641 			if (lstat(dirp, &sb) == -1)
   2642 				goto bad;
   2643 			if (!S_ISDIR(sb.st_mode))
   2644 				goto bad1;
   2645 			*cp = '/';
   2646 		}
   2647 	}
   2648 
   2649 	cp = NULL;
   2650 	if (lstat(dirp, &sb) == -1)
   2651 		goto bad;
   2652 
   2653 	if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
   2654 		file = " file or a";
   2655 		goto bad1;
   2656 	}
   2657 
   2658 	return 1;
   2659 
   2660 bad:
   2661 	syslog(LOG_ERR,
   2662 	    "\"%s\", line %ld: lstat for `%s' failed: %m",
   2663 	    line, (unsigned long)lineno, dirp);
   2664 	if (cp)
   2665 		*cp = '/';
   2666 	return 0;
   2667 
   2668 bad1:
   2669 	syslog(LOG_ERR,
   2670 	    "\"%s\", line %ld: `%s' is not a%s directory",
   2671 	    line, (unsigned long)lineno, dirp, file);
   2672 	if (cp)
   2673 		*cp = '/';
   2674 	return 0;
   2675 }
   2676