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