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