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