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