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