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