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