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