Home | History | Annotate | Line # | Download | only in mountd
mountd.c revision 1.64
      1 /* 	$NetBSD: mountd.c,v 1.64 2000/06/09 00:03:32 fvdl 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.64 2000/06/09 00:03:32 fvdl 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 		break;
   1420 	case AF_INET6:
   1421 		src = &((struct sockaddr_in6 *)s1)->sin6_addr;
   1422 		dst = &((struct sockaddr_in6 *)s2)->sin6_addr;
   1423 		break;
   1424 	default:
   1425 		return 1;
   1426 	}
   1427 
   1428 	return bitcmp(src, dst, bitlen);
   1429 }
   1430 
   1431 static int
   1432 allones(struct sockaddr_storage *ssp, int bitlen)
   1433 {
   1434 	u_int8_t *p;
   1435 	int bytelen, bitsleft, i;
   1436 	int zerolen;
   1437 
   1438 	switch (ssp->ss_family) {
   1439 	case AF_INET:
   1440 		p = (u_int8_t *)&((struct sockaddr_in *)ssp)->sin_addr;
   1441 		zerolen = sizeof (((struct sockaddr_in *)ssp)->sin_addr);
   1442 		break;
   1443 	case AF_INET6:
   1444 		p = (u_int8_t *)&((struct sockaddr_in6 *)ssp)->sin6_addr;
   1445 		zerolen = sizeof (((struct sockaddr_in6 *)ssp)->sin6_addr);
   1446 		break;
   1447 	default:
   1448 		return -1;
   1449 	}
   1450 
   1451 	memset(p, 0, zerolen);
   1452 
   1453 	bytelen = bitlen / 8;
   1454 	bitsleft = bitlen % 8;
   1455 
   1456 	for (i = 0; i < bytelen; i++)
   1457 		*p++ = 0xff;
   1458 
   1459 	for (i = 0; i < bitsleft; i++)
   1460 		*p |= 1 << (7 - i);
   1461 
   1462 	return 0;
   1463 }
   1464 
   1465 static int
   1466 countones(struct sockaddr *sa)
   1467 {
   1468 	void *mask;
   1469 	int i, bits = 0, bytelen;
   1470 	u_int8_t *p;
   1471 
   1472 	switch (sa->sa_family) {
   1473 	case AF_INET:
   1474 		mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr;
   1475 		bytelen = 4;
   1476 		break;
   1477 	case AF_INET6:
   1478 		mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr;
   1479 		bytelen = 16;
   1480 		break;
   1481 	default:
   1482 		return 0;
   1483 	}
   1484 
   1485 	p = mask;
   1486 
   1487 	for (i = 0; i < bytelen; i++, p++) {
   1488 		if (*p != 0xff) {
   1489 			for (bits = 0; bits < 8; bits++) {
   1490 				if (!(*p & (1 << (7 - bits))))
   1491 					break;
   1492 			}
   1493 			break;
   1494 		}
   1495 	}
   1496 
   1497 	return (i * 8 + bits);
   1498 }
   1499 
   1500 static int
   1501 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
   1502 {
   1503 	void *p1, *p2;
   1504 	int len;
   1505 
   1506 	if (sa1->sa_family != sa2->sa_family)
   1507 		return 1;
   1508 
   1509 	switch (sa1->sa_family) {
   1510 	case AF_INET:
   1511 		p1 = &((struct sockaddr_in *)sa1)->sin_addr;
   1512 		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
   1513 		len = 4;
   1514 		break;
   1515 	case AF_INET6:
   1516 		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
   1517 		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
   1518 		len = 16;
   1519 		break;
   1520 	default:
   1521 		return 1;
   1522 	}
   1523 
   1524 	return memcmp(p1, p2, len);
   1525 }
   1526 
   1527 /*
   1528  * Scan for a host match in a directory tree.
   1529  */
   1530 static int
   1531 chk_host(dp, saddr, defsetp, hostsetp)
   1532 	struct dirlist *dp;
   1533 	struct sockaddr *saddr;
   1534 	int *defsetp;
   1535 	int *hostsetp;
   1536 {
   1537 	struct hostlist *hp;
   1538 	struct grouplist *grp;
   1539 	struct addrinfo *ai;
   1540 
   1541 	if (dp) {
   1542 		if (dp->dp_flag & DP_DEFSET)
   1543 			*defsetp = dp->dp_flag;
   1544 		hp = dp->dp_hosts;
   1545 		while (hp) {
   1546 			grp = hp->ht_grp;
   1547 			switch (grp->gr_type) {
   1548 			case GT_HOST:
   1549 				ai = grp->gr_ptr.gt_addrinfo;
   1550 				for (; ai; ai = ai->ai_next) {
   1551 					if (!sacmp(ai->ai_addr, saddr)) {
   1552 						*hostsetp =
   1553 						    (hp->ht_flag | DP_HOSTSET);
   1554 						return (1);
   1555 					}
   1556 				}
   1557 				break;
   1558 			case GT_NET:
   1559 				if (!netpartcmp(saddr,
   1560 				    (struct sockaddr *)
   1561 					&grp->gr_ptr.gt_net.nt_net,
   1562 				    grp->gr_ptr.gt_net.nt_len)) {
   1563 					*hostsetp = (hp->ht_flag | DP_HOSTSET);
   1564 					return (1);
   1565 				}
   1566 				break;
   1567 			};
   1568 			hp = hp->ht_next;
   1569 		}
   1570 	}
   1571 	return (0);
   1572 }
   1573 
   1574 /*
   1575  * Scan tree for a host that matches the address.
   1576  */
   1577 static int
   1578 scan_tree(dp, saddr)
   1579 	struct dirlist *dp;
   1580 	struct sockaddr *saddr;
   1581 {
   1582 	int defset, hostset;
   1583 
   1584 	if (dp) {
   1585 		if (scan_tree(dp->dp_left, saddr))
   1586 			return (1);
   1587 		if (chk_host(dp, saddr, &defset, &hostset))
   1588 			return (1);
   1589 		if (scan_tree(dp->dp_right, saddr))
   1590 			return (1);
   1591 	}
   1592 	return (0);
   1593 }
   1594 
   1595 /*
   1596  * Traverse the dirlist tree and free it up.
   1597  */
   1598 static void
   1599 free_dir(dp)
   1600 	struct dirlist *dp;
   1601 {
   1602 
   1603 	if (dp) {
   1604 		free_dir(dp->dp_left);
   1605 		free_dir(dp->dp_right);
   1606 		free_host(dp->dp_hosts);
   1607 		free(dp);
   1608 	}
   1609 }
   1610 
   1611 /*
   1612  * Parse the option string and update fields.
   1613  * Option arguments may either be -<option>=<value> or
   1614  * -<option> <value>
   1615  */
   1616 static int
   1617 do_opt(line, lineno, cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
   1618 	const char *line;
   1619 	size_t lineno;
   1620 	char **cpp, **endcpp;
   1621 	struct exportlist *ep;
   1622 	struct grouplist *grp;
   1623 	int *has_hostp;
   1624 	int *exflagsp;
   1625 	struct ucred *cr;
   1626 {
   1627 	char *cpoptarg, *cpoptend;
   1628 	char *cp, *endcp, *cpopt, savedc, savedc2;
   1629 	int allflag, usedarg;
   1630 
   1631 	cpopt = *cpp;
   1632 	cpopt++;
   1633 	cp = *endcpp;
   1634 	savedc = *cp;
   1635 	*cp = '\0';
   1636 	while (cpopt && *cpopt) {
   1637 		allflag = 1;
   1638 		usedarg = -2;
   1639 		savedc2 = '\0';
   1640 		if ((cpoptend = strchr(cpopt, ',')) != NULL) {
   1641 			*cpoptend++ = '\0';
   1642 			if ((cpoptarg = strchr(cpopt, '=')) != NULL)
   1643 				*cpoptarg++ = '\0';
   1644 		} else {
   1645 			if ((cpoptarg = strchr(cpopt, '=')) != NULL)
   1646 				*cpoptarg++ = '\0';
   1647 			else {
   1648 				*cp = savedc;
   1649 				nextfield(&cp, &endcp);
   1650 				**endcpp = '\0';
   1651 				if (endcp > cp && *cp != '-') {
   1652 					cpoptarg = cp;
   1653 					savedc2 = *endcp;
   1654 					*endcp = '\0';
   1655 					usedarg = 0;
   1656 				}
   1657 			}
   1658 		}
   1659 		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
   1660 			*exflagsp |= MNT_EXRDONLY;
   1661 		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
   1662 		    !(allflag = strcmp(cpopt, "mapall")) ||
   1663 		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
   1664 			usedarg++;
   1665 			parsecred(cpoptarg, cr);
   1666 			if (allflag == 0) {
   1667 				*exflagsp |= MNT_EXPORTANON;
   1668 				opt_flags |= OP_MAPALL;
   1669 			} else
   1670 				opt_flags |= OP_MAPROOT;
   1671 		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
   1672 			*exflagsp |= MNT_EXKERB;
   1673 			opt_flags |= OP_KERB;
   1674 		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
   1675 		    !strcmp(cpopt, "m"))) {
   1676 			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
   1677 				syslog(LOG_ERR,
   1678 				    "\"%s\", line %ld: Bad mask: %s",
   1679 				    line, (unsigned long)lineno, cpoptarg);
   1680 				return (1);
   1681 			}
   1682 			usedarg++;
   1683 			opt_flags |= OP_MASK;
   1684 		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
   1685 		    !strcmp(cpopt, "n"))) {
   1686 			if (strchr(cpoptarg, '/') != NULL) {
   1687 				if (debug)
   1688 					fprintf(stderr, "setting OP_MASKLEN\n");
   1689 				opt_flags |= OP_MASKLEN;
   1690 			}
   1691 			if (grp->gr_type != GT_NULL) {
   1692 				syslog(LOG_ERR,
   1693 				    "\"%s\", line %ld: Network/host conflict",
   1694 				    line, (unsigned long)lineno);
   1695 				return (1);
   1696 			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
   1697 				syslog(LOG_ERR,
   1698 				    "\"%s\", line %ld: Bad net: %s",
   1699 				    line, (unsigned long)lineno, cpoptarg);
   1700 				return (1);
   1701 			}
   1702 			grp->gr_type = GT_NET;
   1703 			*has_hostp = 1;
   1704 			usedarg++;
   1705 			opt_flags |= OP_NET;
   1706 		} else if (!strcmp(cpopt, "alldirs")) {
   1707 			opt_flags |= OP_ALLDIRS;
   1708 		} else if (!strcmp(cpopt, "noresvmnt")) {
   1709 			opt_flags |= OP_NORESMNT;
   1710 		} else if (!strcmp(cpopt, "noresvport")) {
   1711 			opt_flags |= OP_NORESPORT;
   1712 			*exflagsp |= MNT_EXNORESPORT;
   1713 		} else if (!strcmp(cpopt, "public")) {
   1714 			*exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC);
   1715 			opt_flags |= OP_NORESPORT;
   1716 		} else if (!strcmp(cpopt, "webnfs")) {
   1717 			*exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC |
   1718 			    MNT_EXRDONLY | MNT_EXPORTANON);
   1719 			opt_flags |= (OP_MAPALL | OP_NORESPORT);
   1720 		} else if (cpoptarg && !strcmp(cpopt, "index")) {
   1721 			ep->ex_indexfile = strdup(cpoptarg);
   1722 #ifdef ISO
   1723 		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
   1724 			if (get_isoaddr(line, lineno, cpoptarg, grp))
   1725 				return (1);
   1726 			*has_hostp = 1;
   1727 			usedarg++;
   1728 			opt_flags |= OP_ISO;
   1729 #endif /* ISO */
   1730 		} else {
   1731 			syslog(LOG_ERR,
   1732 			    "\"%s\", line %ld: Bad opt %s",
   1733 			    line, (unsigned long)lineno, cpopt);
   1734 			return (1);
   1735 		}
   1736 		if (usedarg >= 0) {
   1737 			*endcp = savedc2;
   1738 			**endcpp = savedc;
   1739 			if (usedarg > 0) {
   1740 				*cpp = cp;
   1741 				*endcpp = endcp;
   1742 			}
   1743 			return (0);
   1744 		}
   1745 		cpopt = cpoptend;
   1746 	}
   1747 	**endcpp = savedc;
   1748 	return (0);
   1749 }
   1750 
   1751 /*
   1752  * Translate a character string to the corresponding list of network
   1753  * addresses for a hostname.
   1754  */
   1755 static int
   1756 get_host(line, lineno, cp, grp)
   1757 	const char *line;
   1758 	size_t lineno;
   1759 	const char *cp;
   1760 	struct grouplist *grp;
   1761 {
   1762 	struct addrinfo *ai, hints;
   1763 	int ecode;
   1764 	char host[NI_MAXHOST];
   1765 
   1766 	if (grp->gr_type != GT_NULL) {
   1767 		syslog(LOG_ERR,
   1768 		    "\"%s\", line %ld: Bad netgroup type for ip host %s",
   1769 		    line, (unsigned long)lineno, cp);
   1770 		return (1);
   1771 	}
   1772 	memset(&hints, 0, sizeof hints);
   1773 	hints.ai_flags = AI_CANONNAME;
   1774 	hints.ai_protocol = IPPROTO_UDP;
   1775 	ecode = getaddrinfo(cp, NULL, &hints, &ai);
   1776 	if (ecode != 0) {
   1777 		syslog(LOG_ERR, "\"%s\", line %ld: can't get address info for "
   1778 				"host %s",
   1779 		    line, (long)lineno, cp);
   1780 		return 1;
   1781 	}
   1782 	grp->gr_type = GT_HOST;
   1783 	grp->gr_ptr.gt_addrinfo = ai;
   1784 	while (ai != NULL) {
   1785 		if (ai->ai_canonname == NULL) {
   1786 			getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
   1787 			    sizeof host, NULL, 0, NI_NUMERICHOST);
   1788 			ai->ai_canonname = estrdup(host);
   1789 			ai->ai_flags |= AI_CANONNAME;
   1790 		} else
   1791 			ai->ai_flags &= ~AI_CANONNAME;
   1792 		if (debug)
   1793 			(void)fprintf(stderr, "got host %s\n", ai->ai_canonname);
   1794 		ai = ai->ai_next;
   1795 	}
   1796 	return (0);
   1797 }
   1798 
   1799 /*
   1800  * Free up an exports list component
   1801  */
   1802 static void
   1803 free_exp(ep)
   1804 	struct exportlist *ep;
   1805 {
   1806 
   1807 	if (ep->ex_defdir) {
   1808 		free_host(ep->ex_defdir->dp_hosts);
   1809 		free(ep->ex_defdir);
   1810 	}
   1811 	if (ep->ex_fsdir)
   1812 		free(ep->ex_fsdir);
   1813 	if (ep->ex_indexfile)
   1814 		free(ep->ex_indexfile);
   1815 	free_dir(ep->ex_dirl);
   1816 	free(ep);
   1817 }
   1818 
   1819 /*
   1820  * Free hosts.
   1821  */
   1822 static void
   1823 free_host(hp)
   1824 	struct hostlist *hp;
   1825 {
   1826 	struct hostlist *hp2;
   1827 
   1828 	while (hp) {
   1829 		hp2 = hp;
   1830 		hp = hp->ht_next;
   1831 		free(hp2);
   1832 	}
   1833 }
   1834 
   1835 static struct hostlist *
   1836 get_ht()
   1837 {
   1838 	struct hostlist *hp;
   1839 
   1840 	hp = emalloc(sizeof(struct hostlist));
   1841 	hp->ht_next = NULL;
   1842 	hp->ht_flag = 0;
   1843 	return (hp);
   1844 }
   1845 
   1846 #ifdef ISO
   1847 /*
   1848  * Translate an iso address.
   1849  */
   1850 static int
   1851 get_isoaddr(line, lineno, cp, grp)
   1852 	const char *line;
   1853 	size_t lineno;
   1854 	char *cp;
   1855 	struct grouplist *grp;
   1856 {
   1857 	struct iso_addr *isop;
   1858 	struct sockaddr_iso *isoaddr;
   1859 
   1860 	if (grp->gr_type != GT_NULL) {
   1861 		syslog(LOG_ERR,
   1862 		    "\"%s\", line %ld: Bad netgroup type for iso addr %s",
   1863 		    line, (unsigned long)lineno, cp);
   1864 		return (1);
   1865 	}
   1866 	if ((isop = iso_addr(cp)) == NULL) {
   1867 		syslog(LOG_ERR,
   1868 		    "\"%s\", line %ld: Bad iso addr %s",
   1869 		    line, (unsigned long)lineno, cp);
   1870 		return (1);
   1871 	}
   1872 	isoaddr = emalloc(sizeof(struct sockaddr_iso));
   1873 	(void)memset(isoaddr, 0, sizeof(struct sockaddr_iso));
   1874 	(void)memcpy(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
   1875 	isoaddr->siso_len = sizeof(struct sockaddr_iso);
   1876 	isoaddr->siso_family = AF_ISO;
   1877 	grp->gr_type = GT_ISO;
   1878 	grp->gr_ptr.gt_isoaddr = isoaddr;
   1879 	return (0);
   1880 }
   1881 #endif				/* ISO */
   1882 
   1883 /*
   1884  * error checked malloc and strdup
   1885  */
   1886 static void *
   1887 emalloc(n)
   1888 	size_t n;
   1889 {
   1890 	void *ptr = malloc(n);
   1891 
   1892 	if (ptr == NULL) {
   1893 		syslog(LOG_ERR, "%m");
   1894 		exit(2);
   1895 	}
   1896 	return ptr;
   1897 }
   1898 
   1899 static char *
   1900 estrdup(s)
   1901 	const char *s;
   1902 {
   1903 	char *n = strdup(s);
   1904 
   1905 	if (n == NULL) {
   1906 		syslog(LOG_ERR, "%m");
   1907 		exit(2);
   1908 	}
   1909 	return n;
   1910 }
   1911 
   1912 /*
   1913  * Do the mount syscall with the update flag to push the export info into
   1914  * the kernel.
   1915  */
   1916 static int
   1917 do_mount(line, lineno, ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
   1918 	const char *line;
   1919 	size_t lineno;
   1920 	struct exportlist *ep;
   1921 	struct grouplist *grp;
   1922 	int exflags;
   1923 	struct ucred *anoncrp;
   1924 	char *dirp;
   1925 	int dirplen;
   1926 	struct statfs *fsb;
   1927 {
   1928 	struct sockaddr *addrp;
   1929 	struct sockaddr_storage ss;
   1930 	struct addrinfo *ai;
   1931 	int addrlen;
   1932 	char *cp = NULL;
   1933 	int done;
   1934 	char savedc = '\0';
   1935 	union {
   1936 		struct ufs_args ua;
   1937 		struct iso_args ia;
   1938 		struct mfs_args ma;
   1939 		struct msdosfs_args da;
   1940 		struct adosfs_args aa;
   1941 	} args;
   1942 
   1943 	args.ua.fspec = 0;
   1944 	args.ua.export.ex_flags = exflags;
   1945 	args.ua.export.ex_anon = *anoncrp;
   1946 	args.ua.export.ex_indexfile = ep->ex_indexfile;
   1947 	if (grp->gr_type == GT_HOST) {
   1948 		ai = grp->gr_ptr.gt_addrinfo;
   1949 		addrp = ai->ai_addr;
   1950 		addrlen = ai->ai_addrlen;
   1951 	} else
   1952 		addrp = NULL;
   1953 	done = FALSE;
   1954 	while (!done) {
   1955 		switch (grp->gr_type) {
   1956 		case GT_HOST:
   1957 			args.ua.export.ex_addr = addrp;
   1958 			args.ua.export.ex_addrlen = addrlen;
   1959 			args.ua.export.ex_masklen = 0;
   1960 			break;
   1961 		case GT_NET:
   1962 			args.ua.export.ex_addr = (struct sockaddr *)
   1963 			    &grp->gr_ptr.gt_net.nt_net;
   1964 			args.ua.export.ex_addrlen =
   1965 			    args.ua.export.ex_addr->sa_len;
   1966 			memset(&ss, 0, sizeof ss);
   1967 			ss.ss_family = args.ua.export.ex_addr->sa_family;
   1968 			ss.ss_len = args.ua.export.ex_addr->sa_len;
   1969 			allones(&ss, grp->gr_ptr.gt_net.nt_len);
   1970 			args.ua.export.ex_mask = (struct sockaddr *)&ss;
   1971 			args.ua.export.ex_masklen = ss.ss_len;
   1972 			break;
   1973 #ifdef ISO
   1974 		case GT_ISO:
   1975 			args.ua.export.ex_addr =
   1976 			    (struct sockaddr *) grp->gr_ptr.gt_isoaddr;
   1977 			args.ua.export.ex_addrlen =
   1978 			    sizeof(struct sockaddr_iso);
   1979 			args.ua.export.ex_masklen = 0;
   1980 			break;
   1981 #endif				/* ISO */
   1982 		default:
   1983 			syslog(LOG_ERR, "\"%s\", line %ld: Bad netgroup type",
   1984 			    line, (unsigned long)lineno);
   1985 			if (cp)
   1986 				*cp = savedc;
   1987 			return (1);
   1988 		};
   1989 
   1990 		/*
   1991 		 * XXX:
   1992 		 * Maybe I should just use the fsb->f_mntonname path instead
   1993 		 * of looping back up the dirp to the mount point??
   1994 		 * Also, needs to know how to export all types of local
   1995 		 * exportable file systems and not just MOUNT_FFS.
   1996 		 */
   1997 		while (mount(fsb->f_fstypename, dirp,
   1998 		    fsb->f_flags | MNT_UPDATE, &args) == -1) {
   1999 			if (cp)
   2000 				*cp-- = savedc;
   2001 			else
   2002 				cp = dirp + dirplen - 1;
   2003 			if (errno == EPERM) {
   2004 				syslog(LOG_ERR,
   2005 		    "\"%s\", line %ld: Can't change attributes for %s to %s",
   2006 				    line, (unsigned long)lineno,
   2007 				    dirp, (grp->gr_type == GT_HOST) ?
   2008 				    grp->gr_ptr.gt_addrinfo->ai_canonname :
   2009 				    (grp->gr_type == GT_NET) ?
   2010 				    grp->gr_ptr.gt_net.nt_name :
   2011 				    "Unknown");
   2012 				return (1);
   2013 			}
   2014 			if (opt_flags & OP_ALLDIRS) {
   2015 				syslog(LOG_ERR,
   2016 				"\"%s\", line %ld: Could not remount %s: %m",
   2017 				    line, (unsigned long)lineno,
   2018 				    dirp);
   2019 				return (1);
   2020 			}
   2021 			/* back up over the last component */
   2022 			while (*cp == '/' && cp > dirp)
   2023 				cp--;
   2024 			while (*(cp - 1) != '/' && cp > dirp)
   2025 				cp--;
   2026 			if (cp == dirp) {
   2027 				if (debug)
   2028 					(void)fprintf(stderr, "mnt unsucc\n");
   2029 				syslog(LOG_ERR,
   2030 				    "\"%s\", line %ld: Can't export %s",
   2031 				    line, (unsigned long)lineno, dirp);
   2032 				return (1);
   2033 			}
   2034 			savedc = *cp;
   2035 			*cp = '\0';
   2036 		}
   2037 		if (addrp) {
   2038 			ai = ai->ai_next;
   2039 			if (ai == NULL)
   2040 				done = TRUE;
   2041 			else {
   2042 				addrp = ai->ai_addr;
   2043 				addrlen = ai->ai_addrlen;
   2044 			}
   2045 		} else
   2046 			done = TRUE;
   2047 	}
   2048 	if (cp)
   2049 		*cp = savedc;
   2050 	return (0);
   2051 }
   2052 
   2053 /*
   2054  * Translate a net address.
   2055  */
   2056 static int
   2057 get_net(cp, net, maskflg)
   2058 	char *cp;
   2059 	struct netmsk *net;
   2060 	int maskflg;
   2061 {
   2062 	struct netent *np;
   2063 	char *name, *p, *prefp;
   2064 	struct sockaddr_in sin, *sinp;
   2065 	struct sockaddr *sa;
   2066 	struct addrinfo hints, *ai = NULL;
   2067 	char netname[NI_MAXHOST];
   2068 	long preflen;
   2069 	int ecode;
   2070 
   2071 	if ((opt_flags & OP_MASKLEN) && !maskflg) {
   2072 		p = strchr(cp, '/');
   2073 		*p = '\0';
   2074 		prefp = p + 1;
   2075 	}
   2076 
   2077 	if ((np = getnetbyname(cp)) != NULL) {
   2078 		sin.sin_family = AF_INET;
   2079 		sin.sin_len = sizeof sin;
   2080 		sin.sin_addr = inet_makeaddr(np->n_net, 0);
   2081 		sa = (struct sockaddr *)&sin;
   2082 	} else if (isdigit(*cp)) {
   2083 		memset(&hints, 0, sizeof hints);
   2084 		hints.ai_family = AF_UNSPEC;
   2085 		hints.ai_flags = AI_NUMERICHOST;
   2086 		if (getaddrinfo(cp, NULL, &hints, &ai) != 0) {
   2087 			/*
   2088 			 * If getaddrinfo() failed, try the inet4 network
   2089 			 * notation with less than 3 dots.
   2090 			 */
   2091 			sin.sin_family = AF_INET;
   2092 			sin.sin_len = sizeof sin;
   2093 			sin.sin_addr = inet_makeaddr(inet_network(cp),0);
   2094 			if (debug)
   2095 				fprintf(stderr, "get_net: v4 addr %x\n",
   2096 				    sin.sin_addr.s_addr);
   2097 			sa = (struct sockaddr *)&sin;
   2098 		} else
   2099 			sa = ai->ai_addr;
   2100 	} else
   2101 		return 1;
   2102 
   2103 	ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname,
   2104 	    NULL, 0, NI_NUMERICHOST);
   2105 	if (ecode != 0)
   2106 		return 1;
   2107 
   2108 	if (maskflg)
   2109 		net->nt_len = countones(sa);
   2110 	else {
   2111 		if (opt_flags & OP_MASKLEN) {
   2112 			preflen = strtol(prefp, NULL, 10);
   2113 			if (preflen == LONG_MIN && errno == ERANGE)
   2114 				return 1;
   2115 			net->nt_len = (int)preflen;
   2116 			*p = '/';
   2117 		}
   2118 
   2119 		if (np)
   2120 			name = np->n_name;
   2121 		else {
   2122 			getnameinfo(sa, sa->sa_len, netname, sizeof netname,
   2123 			    NULL, 0, NI_NUMERICHOST);
   2124 			name = netname;
   2125 		}
   2126 		net->nt_name = estrdup(name);
   2127 		memcpy(&net->nt_net, sa, sa->sa_len);
   2128 	}
   2129 
   2130 	if (!maskflg && sa->sa_family == AF_INET &&
   2131 	    !(opt_flags & (OP_MASK|OP_MASKLEN))) {
   2132 		sinp = (struct sockaddr_in *)sa;
   2133 		if (IN_CLASSA(sinp->sin_addr.s_addr))
   2134 			net->nt_len = 8;
   2135 		else if (IN_CLASSB(sinp->sin_addr.s_addr))
   2136 			net->nt_len = 16;
   2137 		else if (IN_CLASSC(sinp->sin_addr.s_addr))
   2138 			net->nt_len = 24;
   2139 		else if (IN_CLASSD(sinp->sin_addr.s_addr))
   2140 			net->nt_len = 28;
   2141 		else
   2142 			net->nt_len = 32;	/* XXX */
   2143 	}
   2144 
   2145 	if (ai)
   2146 		freeaddrinfo(ai);
   2147 	return 0;
   2148 }
   2149 
   2150 /*
   2151  * Parse out the next white space separated field
   2152  */
   2153 static void
   2154 nextfield(cp, endcp)
   2155 	char **cp;
   2156 	char **endcp;
   2157 {
   2158 	char *p;
   2159 
   2160 	p = *cp;
   2161 	while (*p == ' ' || *p == '\t')
   2162 		p++;
   2163 	if (*p == '\n' || *p == '\0')
   2164 		*cp = *endcp = p;
   2165 	else {
   2166 		*cp = p++;
   2167 		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
   2168 			p++;
   2169 		*endcp = p;
   2170 	}
   2171 }
   2172 
   2173 /*
   2174  * Parse a description of a credential.
   2175  */
   2176 static void
   2177 parsecred(namelist, cr)
   2178 	char *namelist;
   2179 	struct ucred *cr;
   2180 {
   2181 	char *name;
   2182 	int cnt;
   2183 	char *names;
   2184 	struct passwd *pw;
   2185 	struct group *gr;
   2186 	int ngroups, groups[NGROUPS + 1];
   2187 
   2188 	/*
   2189 	 * Set up the unprivileged user.
   2190 	 */
   2191 	cr->cr_ref = 1;
   2192 	cr->cr_uid = -2;
   2193 	cr->cr_gid = -2;
   2194 	cr->cr_ngroups = 0;
   2195 	/*
   2196 	 * Get the user's password table entry.
   2197 	 */
   2198 	names = strsep(&namelist, " \t\n");
   2199 	name = strsep(&names, ":");
   2200 	if (isdigit(*name) || *name == '-')
   2201 		pw = getpwuid(atoi(name));
   2202 	else
   2203 		pw = getpwnam(name);
   2204 	/*
   2205 	 * Credentials specified as those of a user.
   2206 	 */
   2207 	if (names == NULL) {
   2208 		if (pw == NULL) {
   2209 			syslog(LOG_ERR, "Unknown user: %s", name);
   2210 			return;
   2211 		}
   2212 		cr->cr_uid = pw->pw_uid;
   2213 		ngroups = NGROUPS + 1;
   2214 		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
   2215 			syslog(LOG_ERR, "Too many groups");
   2216 		/*
   2217 		 * Convert from int's to gid_t's and compress out duplicate
   2218 		 */
   2219 		cr->cr_ngroups = ngroups - 1;
   2220 		cr->cr_gid = groups[0];
   2221 		for (cnt = 1; cnt < ngroups; cnt++)
   2222 			cr->cr_groups[cnt - 1] = groups[cnt];
   2223 		return;
   2224 	}
   2225 	/*
   2226 	 * Explicit credential specified as a colon separated list:
   2227 	 *	uid:gid:gid:...
   2228 	 */
   2229 	if (pw != NULL)
   2230 		cr->cr_uid = pw->pw_uid;
   2231 	else if (isdigit(*name) || *name == '-')
   2232 		cr->cr_uid = atoi(name);
   2233 	else {
   2234 		syslog(LOG_ERR, "Unknown user: %s", name);
   2235 		return;
   2236 	}
   2237 	cr->cr_ngroups = 0;
   2238 	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
   2239 		name = strsep(&names, ":");
   2240 		if (isdigit(*name) || *name == '-') {
   2241 			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
   2242 		} else {
   2243 			if ((gr = getgrnam(name)) == NULL) {
   2244 				syslog(LOG_ERR, "Unknown group: %s", name);
   2245 				continue;
   2246 			}
   2247 			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
   2248 		}
   2249 	}
   2250 	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
   2251 		syslog(LOG_ERR, "Too many groups");
   2252 }
   2253 
   2254 #define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
   2255 /*
   2256  * Routines that maintain the remote mounttab
   2257  */
   2258 static void
   2259 get_mountlist()
   2260 {
   2261 	struct mountlist *mlp, **mlpp;
   2262 	char *host, *dirp, *cp;
   2263 	char str[STRSIZ];
   2264 	FILE *mlfile;
   2265 
   2266 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
   2267 		syslog(LOG_ERR, "Can't open %s: %m", _PATH_RMOUNTLIST);
   2268 		return;
   2269 	}
   2270 	mlpp = &mlhead;
   2271 	while (fgets(str, STRSIZ, mlfile) != NULL) {
   2272 		cp = str;
   2273 		host = strsep(&cp, " \t\n");
   2274 		dirp = strsep(&cp, " \t\n");
   2275 		if (host == NULL || dirp == NULL)
   2276 			continue;
   2277 		mlp = emalloc(sizeof(*mlp));
   2278 		(void)strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
   2279 		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
   2280 		(void)strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
   2281 		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
   2282 		mlp->ml_next = NULL;
   2283 		*mlpp = mlp;
   2284 		mlpp = &mlp->ml_next;
   2285 	}
   2286 	(void)fclose(mlfile);
   2287 }
   2288 
   2289 static int
   2290 del_mlist(hostp, dirp, saddr)
   2291 	char *hostp, *dirp;
   2292 	struct sockaddr *saddr;
   2293 {
   2294 	struct mountlist *mlp, **mlpp;
   2295 	struct mountlist *mlp2;
   2296 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
   2297 	FILE *mlfile;
   2298 	int fnd = 0, ret = 0;
   2299 
   2300 	mlpp = &mlhead;
   2301 	mlp = mlhead;
   2302 	while (mlp) {
   2303 		if (!strcmp(mlp->ml_host, hostp) &&
   2304 		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
   2305 			if (!(mlp->ml_flag & DP_NORESMNT) &&
   2306 			    ntohs(sin->sin_port) >= IPPORT_RESERVED) {
   2307 				syslog(LOG_NOTICE,
   2308 				"Umount request for %s:%s from %s refused\n",
   2309 				    mlp->ml_host, mlp->ml_dirp,
   2310 				    inet_ntoa(sin->sin_addr));
   2311 				ret = -1;
   2312 				goto cont;
   2313 			}
   2314 			fnd = 1;
   2315 			mlp2 = mlp;
   2316 			*mlpp = mlp = mlp->ml_next;
   2317 			free(mlp2);
   2318 		} else {
   2319 cont:
   2320 			mlpp = &mlp->ml_next;
   2321 			mlp = mlp->ml_next;
   2322 		}
   2323 	}
   2324 	if (fnd) {
   2325 		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
   2326 			syslog(LOG_ERR, "Can't update %s: %m",
   2327 			    _PATH_RMOUNTLIST);
   2328 			return ret;
   2329 		}
   2330 		mlp = mlhead;
   2331 		while (mlp) {
   2332 			(void)fprintf(mlfile, "%s %s\n", mlp->ml_host,
   2333 		            mlp->ml_dirp);
   2334 			mlp = mlp->ml_next;
   2335 		}
   2336 		(void)fclose(mlfile);
   2337 	}
   2338 	return ret;
   2339 }
   2340 
   2341 static void
   2342 add_mlist(hostp, dirp, flags)
   2343 	char *hostp, *dirp;
   2344 	int flags;
   2345 {
   2346 	struct mountlist *mlp, **mlpp;
   2347 	FILE *mlfile;
   2348 
   2349 	mlpp = &mlhead;
   2350 	mlp = mlhead;
   2351 	while (mlp) {
   2352 		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
   2353 			return;
   2354 		mlpp = &mlp->ml_next;
   2355 		mlp = mlp->ml_next;
   2356 	}
   2357 	mlp = emalloc(sizeof(*mlp));
   2358 	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
   2359 	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
   2360 	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
   2361 	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
   2362 	mlp->ml_flag = flags;
   2363 	mlp->ml_next = NULL;
   2364 	*mlpp = mlp;
   2365 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
   2366 		syslog(LOG_ERR, "Can't update %s: %m", _PATH_RMOUNTLIST);
   2367 		return;
   2368 	}
   2369 	(void)fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
   2370 	(void)fclose(mlfile);
   2371 }
   2372 
   2373 /*
   2374  * This function is called via. SIGTERM when the system is going down.
   2375  * It sends a broadcast RPCMNT_UMNTALL.
   2376  */
   2377 /* ARGSUSED */
   2378 static void
   2379 send_umntall(n)
   2380 	int n;
   2381 {
   2382 	(void)clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
   2383 	    xdr_void, NULL, xdr_void, NULL, (resultproc_t)umntall_each);
   2384 	exit(0);
   2385 }
   2386 
   2387 static int
   2388 umntall_each(resultsp, raddr)
   2389 	caddr_t resultsp;
   2390 	struct sockaddr_in *raddr;
   2391 {
   2392 	return (1);
   2393 }
   2394 
   2395 /*
   2396  * Free up a group list.
   2397  */
   2398 static void
   2399 free_grp(grp)
   2400 	struct grouplist *grp;
   2401 {
   2402 	struct addrinfo *ai;
   2403 
   2404 	if (grp->gr_type == GT_HOST) {
   2405 		if (grp->gr_ptr.gt_addrinfo != NULL) {
   2406 			ai = grp->gr_ptr.gt_addrinfo;
   2407 			do {
   2408 				if (ai->ai_flags & AI_CANONNAME)
   2409 					free(ai->ai_canonname);
   2410 				ai = ai->ai_next;
   2411 			} while (ai != NULL);
   2412 			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
   2413 		}
   2414 	} else if (grp->gr_type == GT_NET) {
   2415 		if (grp->gr_ptr.gt_net.nt_name)
   2416 			free(grp->gr_ptr.gt_net.nt_name);
   2417 	}
   2418 #ifdef ISO
   2419 	else if (grp->gr_type == GT_ISO)
   2420 		free(grp->gr_ptr.gt_isoaddr);
   2421 #endif
   2422 	free(grp);
   2423 }
   2424 
   2425 #if 0
   2426 static void
   2427 SYSLOG(int pri, const char *fmt,...)
   2428 {
   2429 	va_list ap;
   2430 
   2431 	va_start(ap, fmt);
   2432 
   2433 	if (debug)
   2434 		vfprintf(stderr, fmt, ap);
   2435 	else
   2436 		vsyslog(pri, fmt, ap);
   2437 
   2438 	va_end(ap);
   2439 }
   2440 #endif
   2441 
   2442 /*
   2443  * Check options for consistency.
   2444  */
   2445 static int
   2446 check_options(line, lineno, dp)
   2447 	const char *line;
   2448 	size_t lineno;
   2449 	struct dirlist *dp;
   2450 {
   2451 
   2452 	if (dp == NULL) {
   2453 		syslog(LOG_ERR,
   2454 		    "\"%s\", line %ld: missing directory list",
   2455 		    line, (unsigned long)lineno);
   2456 		return (1);
   2457 	}
   2458 	if ((opt_flags & (OP_MAPROOT|OP_MAPALL)) == (OP_MAPROOT|OP_MAPALL) ||
   2459 	    (opt_flags & (OP_MAPROOT|OP_KERB)) == (OP_MAPROOT|OP_KERB) ||
   2460 	    (opt_flags & (OP_MAPALL|OP_KERB)) == (OP_MAPALL|OP_KERB)) {
   2461 		syslog(LOG_ERR,
   2462 		    "\"%s\", line %ld: -mapall, -maproot and -kerb mutually exclusive",
   2463 		    line, (unsigned long)lineno);
   2464 		return (1);
   2465 	}
   2466 	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
   2467 		syslog(LOG_ERR, "\"%s\", line %ld: -mask requires -net",
   2468 		    line, (unsigned long)lineno);
   2469 		return (1);
   2470 	}
   2471 	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN) != 0) {
   2472 		syslog(LOG_ERR, "\"%s\", line %ld: /pref and -net mutually"
   2473 		    "exclusive",
   2474 		    line, (unsigned long)lineno);
   2475 		return (1);
   2476 	}
   2477 	if ((opt_flags & (OP_NET|OP_ISO)) == (OP_NET|OP_ISO)) {
   2478 		syslog(LOG_ERR,
   2479 		    "\"%s\", line %ld: -net and -iso mutually exclusive",
   2480 		    line, (unsigned long)lineno);
   2481 		return (1);
   2482 	}
   2483 	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
   2484 		syslog(LOG_ERR,
   2485 		    "\"%s\", line %ld: -alldir has multiple directories",
   2486 		    line, (unsigned long)lineno);
   2487 		return (1);
   2488 	}
   2489 	return (0);
   2490 }
   2491 
   2492 /*
   2493  * Check an absolute directory path for any symbolic links. Return true
   2494  * if no symbolic links are found.
   2495  */
   2496 static int
   2497 check_dirpath(line, lineno, dirp)
   2498 	const char *line;
   2499 	size_t lineno;
   2500 	char *dirp;
   2501 {
   2502 	char *cp;
   2503 	struct stat sb;
   2504 	char *file = "";
   2505 
   2506 	for (cp = dirp + 1; *cp; cp++) {
   2507 		if (*cp == '/') {
   2508 			*cp = '\0';
   2509 			if (lstat(dirp, &sb) == -1)
   2510 				goto bad;
   2511 			if (!S_ISDIR(sb.st_mode))
   2512 				goto bad1;
   2513 			*cp = '/';
   2514 		}
   2515 	}
   2516 
   2517 	cp = NULL;
   2518 	if (lstat(dirp, &sb) == -1)
   2519 		goto bad;
   2520 
   2521 	if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
   2522 		file = " file or a";
   2523 		goto bad1;
   2524 	}
   2525 
   2526 	return 1;
   2527 
   2528 bad:
   2529 	syslog(LOG_ERR,
   2530 	    "\"%s\", line %ld: lstat for `%s' failed: %m",
   2531 	    line, (unsigned long)lineno, dirp);
   2532 	if (cp)
   2533 		*cp = '/';
   2534 	return 0;
   2535 
   2536 bad1:
   2537 	syslog(LOG_ERR,
   2538 	    "\"%s\", line %ld: `%s' is not a%s directory",
   2539 	    line, (unsigned long)lineno, dirp, file);
   2540 	if (cp)
   2541 		*cp = '/';
   2542 	return 0;
   2543 }
   2544