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