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