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