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