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