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