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