Home | History | Annotate | Line # | Download | only in mountd
mountd.c revision 1.58
      1 /* $NetBSD: mountd.c,v 1.58 1999/11/14 14:37:16 sommerfeld 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.58 1999/11/14 14:37:16 sommerfeld 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 	 * save off the current export lists for later
    827 	 */
    828 	num = getmntinfo(&fsp, MNT_NOWAIT);
    829 
    830 	/*
    831 	 * Read in the exports file and build the list, calling
    832 	 * mount() as we go along to push the export rules into the kernel.
    833 	 */
    834 	if ((exp_file = fopen(exname, "r")) == NULL) {
    835 		syslog(LOG_ERR, "Can't open %s: %m", exname);
    836 		exit(2);
    837 	}
    838 	dirhead = NULL;
    839 	while ((line = fparseln(exp_file, &len, &lineno, NULL, 0)) != NULL) {
    840 		if (debug)
    841 			(void)fprintf(stderr, "Got line %s\n", line);
    842 		cp = line;
    843 		nextfield(&cp, &endcp);
    844 		if (cp == endcp)
    845 			continue;	/* skip empty line */
    846 		/*
    847 		 * Set defaults.
    848 		 */
    849 		has_host = FALSE;
    850 		anon = def_anon;
    851 		exflags = MNT_EXPORTED;
    852 		got_nondir = 0;
    853 		opt_flags = 0;
    854 		ep = NULL;
    855 
    856 		/*
    857 		 * Create new exports list entry
    858 		 */
    859 		len = endcp - cp;
    860 		tgrp = grp = get_grp();
    861 		while (len > 0) {
    862 			if (len > RPCMNT_NAMELEN) {
    863 				*endcp = '\0';
    864 				syslog(LOG_ERR,
    865 				    "\"%s\", line %ld: name `%s' is too long",
    866 				    line, (unsigned long)lineno, cp);
    867 				goto badline;
    868 			}
    869 			switch (*cp) {
    870 			case '-':
    871 				/*
    872 				 * Option
    873 				 */
    874 				if (ep == NULL) {
    875 					syslog(LOG_ERR,
    876 				"\"%s\", line %ld: No current export list",
    877 					    line, (unsigned long)lineno);
    878 					goto badline;
    879 				}
    880 				if (debug)
    881 					(void)fprintf(stderr, "doing opt %s\n",
    882 					    cp);
    883 				got_nondir = 1;
    884 				if (do_opt(line, lineno, &cp, &endcp, ep, grp,
    885 				    &has_host, &exflags, &anon))
    886 					goto badline;
    887 				break;
    888 
    889 			case '/':
    890 				/*
    891 				 * Directory
    892 				 */
    893 				savedc = *endcp;
    894 				*endcp = '\0';
    895 
    896 				if (!parse_directory(line, lineno, tgrp,
    897 				    got_nondir, cp, &ep, &fsb))
    898 					goto badline;
    899 				/*
    900 				 * Add dirpath to export mount point.
    901 				 */
    902 				dirp = add_expdir(&dirhead, cp, len);
    903 				dirplen = len;
    904 
    905 				*endcp = savedc;
    906 				break;
    907 
    908 			default:
    909 				/*
    910 				 * Host or netgroup.
    911 				 */
    912 				savedc = *endcp;
    913 				*endcp = '\0';
    914 
    915 				if (!parse_host_netgroup(line, lineno, ep,
    916 				    tgrp, cp, &has_host, &grp))
    917 					goto badline;
    918 
    919 				got_nondir = 1;
    920 
    921 				*endcp = savedc;
    922 				break;
    923 			}
    924 
    925 			cp = endcp;
    926 			nextfield(&cp, &endcp);
    927 			len = endcp - cp;
    928 		}
    929 		if (check_options(line, lineno, dirhead))
    930 			goto badline;
    931 
    932 		if (!has_host) {
    933 			grp->gr_type = GT_HOST;
    934 			if (debug)
    935 				(void)fprintf(stderr,
    936 				    "Adding a default entry\n");
    937 			/* add a default group and make the grp list NULL */
    938 			hpe = emalloc(sizeof(struct hostent));
    939 			hpe->h_name = estrdup("Default");
    940 			hpe->h_addrtype = AF_INET;
    941 			hpe->h_length = sizeof(u_int32_t);
    942 			hpe->h_addr_list = NULL;
    943 			grp->gr_ptr.gt_hostent = hpe;
    944 
    945 		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
    946 			/*
    947 			 * Don't allow a network export coincide with a list of
    948 			 * host(s) on the same line.
    949 			 */
    950 			syslog(LOG_ERR,
    951 			    "\"%s\", line %ld: Mixed exporting of networks and hosts is disallowed",
    952 			    line, (unsigned long)lineno);
    953 			goto badline;
    954 		}
    955 		/*
    956 		 * Loop through hosts, pushing the exports into the kernel.
    957 		 * After loop, tgrp points to the start of the list and
    958 		 * grp points to the last entry in the list.
    959 		 */
    960 		grp = tgrp;
    961 		do {
    962 			if (do_mount(line, lineno, ep, grp, exflags, &anon,
    963 			    dirp, dirplen, &fsb))
    964 				goto badline;
    965 		} while (grp->gr_next && (grp = grp->gr_next));
    966 
    967 		/*
    968 		 * Success. Update the data structures.
    969 		 */
    970 		if (has_host) {
    971 			hang_dirp(dirhead, tgrp, ep, opt_flags);
    972 			grp->gr_next = grphead;
    973 			grphead = tgrp;
    974 		} else {
    975 			hang_dirp(dirhead, NULL, ep, opt_flags);
    976 			free_grp(grp);
    977 		}
    978 		dirhead = NULL;
    979 		if ((ep->ex_flag & EX_LINKED) == 0) {
    980 			ep2 = exphead;
    981 			epp = &exphead;
    982 
    983 			/*
    984 			 * Insert in the list in alphabetical order.
    985 			 */
    986 			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
    987 				epp = &ep2->ex_next;
    988 				ep2 = ep2->ex_next;
    989 			}
    990 			if (ep2)
    991 				ep->ex_next = ep2;
    992 			*epp = ep;
    993 			ep->ex_flag |= EX_LINKED;
    994 		}
    995 		goto nextline;
    996 badline:
    997 		free_exp_grp(ep, grp);
    998 nextline:
    999 		if (dirhead) {
   1000 			free_dir(dirhead);
   1001 			dirhead = NULL;
   1002 		}
   1003 		free(line);
   1004 	}
   1005 	(void)fclose(exp_file);
   1006 
   1007 	/*
   1008 	 * Ok, remove any filesystems that existed before that should
   1009 	 * not be there anymore.
   1010 	 */
   1011 	/*
   1012 	 * XXX: Should know how to handle all local exportable file systems
   1013 	 *      instead of just MOUNT_FFS.
   1014 	 */
   1015 	for (i = 0; i < num; i++, fsp++) {
   1016 		union {
   1017 			struct ufs_args ua;
   1018 			struct iso_args ia;
   1019 			struct mfs_args ma;
   1020 			struct msdosfs_args da;
   1021 			struct adosfs_args aa;
   1022 		} targs;
   1023 
   1024 		if (debug)
   1025 			(void)fprintf(stderr,
   1026 			    "seeing if we want to delete %s.\n",
   1027 			    fsp->f_mntonname);
   1028 		/*
   1029 		 * If we can find it in our internal list, we do not
   1030 		 * want to delete it from the kernel.
   1031 		 */
   1032 		if (ex_search(&fsp->f_fsid))
   1033 			continue;
   1034 
   1035 		if (!strncmp(fsp->f_fstypename, MOUNT_MFS, MFSNAMELEN) ||
   1036 		    !strncmp(fsp->f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
   1037 		    !strncmp(fsp->f_fstypename, MOUNT_EXT2FS, MFSNAMELEN) ||
   1038 		    !strncmp(fsp->f_fstypename, MOUNT_MSDOS, MFSNAMELEN) ||
   1039 		    !strncmp(fsp->f_fstypename, MOUNT_ADOSFS, MFSNAMELEN) ||
   1040 		    !strncmp(fsp->f_fstypename, MOUNT_NULL, MFSNAMELEN) ||
   1041 		    !strncmp(fsp->f_fstypename, MOUNT_UMAP, MFSNAMELEN) ||
   1042 		    !strncmp(fsp->f_fstypename, MOUNT_UNION, MFSNAMELEN) ||
   1043 		    !strncmp(fsp->f_fstypename, MOUNT_CD9660, MFSNAMELEN) ||
   1044 		    !strncmp(fsp->f_fstypename, MOUNT_NTFS, MFSNAMELEN)) {
   1045 			if (debug)
   1046 				(void)fprintf(stderr,
   1047 				    "Deleting export for mount %s.\n",
   1048 				    fsp->f_mntonname);
   1049 
   1050 			bzero((char *) &targs, sizeof(targs));
   1051 			targs.ua.fspec = NULL;
   1052 			targs.ua.export.ex_flags = MNT_DELEXPORT;
   1053 			if (mount(fsp->f_fstypename, fsp->f_mntonname,
   1054 			    fsp->f_flags | MNT_UPDATE, &targs) == -1)
   1055 				syslog(LOG_ERR, "Can't delete exports for %s",
   1056 				    fsp->f_mntonname);
   1057 		}
   1058 	}
   1059 }
   1060 
   1061 /*
   1062  * Allocate an export list element
   1063  */
   1064 static struct exportlist *
   1065 get_exp()
   1066 {
   1067 	struct exportlist *ep;
   1068 
   1069 	ep = emalloc(sizeof(struct exportlist));
   1070 	(void)memset(ep, 0, sizeof(struct exportlist));
   1071 	return (ep);
   1072 }
   1073 
   1074 /*
   1075  * Allocate a group list element
   1076  */
   1077 static struct grouplist *
   1078 get_grp()
   1079 {
   1080 	struct grouplist *gp;
   1081 
   1082 	gp = emalloc(sizeof(struct grouplist));
   1083 	(void)memset(gp, 0, sizeof(struct grouplist));
   1084 	return (gp);
   1085 }
   1086 
   1087 /*
   1088  * Clean up upon an error in get_exportlist().
   1089  */
   1090 static void
   1091 free_exp_grp(ep, grp)
   1092 	struct exportlist *ep;
   1093 	struct grouplist *grp;
   1094 {
   1095 	struct grouplist *tgrp;
   1096 
   1097 	if (ep && (ep->ex_flag & EX_LINKED) == 0)
   1098 		free_exp(ep);
   1099 	while (grp) {
   1100 		tgrp = grp;
   1101 		grp = grp->gr_next;
   1102 		free_grp(tgrp);
   1103 	}
   1104 }
   1105 
   1106 /*
   1107  * Search the export list for a matching fs.
   1108  */
   1109 static struct exportlist *
   1110 ex_search(fsid)
   1111 	fsid_t *fsid;
   1112 {
   1113 	struct exportlist *ep;
   1114 
   1115 	ep = exphead;
   1116 	while (ep) {
   1117 		if (ep->ex_fs.val[0] == fsid->val[0] &&
   1118 		    ep->ex_fs.val[1] == fsid->val[1])
   1119 			return (ep);
   1120 		ep = ep->ex_next;
   1121 	}
   1122 	return (ep);
   1123 }
   1124 
   1125 /*
   1126  * Add a directory path to the list.
   1127  */
   1128 static char *
   1129 add_expdir(dpp, cp, len)
   1130 	struct dirlist **dpp;
   1131 	char *cp;
   1132 	int len;
   1133 {
   1134 	struct dirlist *dp;
   1135 
   1136 	dp = emalloc(sizeof(struct dirlist) + len);
   1137 	dp->dp_left = *dpp;
   1138 	dp->dp_right = NULL;
   1139 	dp->dp_flag = 0;
   1140 	dp->dp_hosts = NULL;
   1141 	(void)strcpy(dp->dp_dirp, cp);
   1142 	*dpp = dp;
   1143 	return (dp->dp_dirp);
   1144 }
   1145 
   1146 /*
   1147  * Hang the dir list element off the dirpath binary tree as required
   1148  * and update the entry for host.
   1149  */
   1150 void
   1151 hang_dirp(dp, grp, ep, flags)
   1152 	struct dirlist *dp;
   1153 	struct grouplist *grp;
   1154 	struct exportlist *ep;
   1155 	int flags;
   1156 {
   1157 	struct hostlist *hp;
   1158 	struct dirlist *dp2;
   1159 
   1160 	if (flags & OP_ALLDIRS) {
   1161 		if (ep->ex_defdir)
   1162 			free(dp);
   1163 		else
   1164 			ep->ex_defdir = dp;
   1165 		if (grp == NULL) {
   1166 			ep->ex_defdir->dp_flag |= DP_DEFSET;
   1167 			if (flags & OP_KERB)
   1168 				ep->ex_defdir->dp_flag |= DP_KERB;
   1169 			if (flags & OP_NORESMNT)
   1170 				ep->ex_defdir->dp_flag |= DP_NORESMNT;
   1171 		} else
   1172 			while (grp) {
   1173 				hp = get_ht();
   1174 				if (flags & OP_KERB)
   1175 					hp->ht_flag |= DP_KERB;
   1176 				if (flags & OP_NORESMNT)
   1177 					hp->ht_flag |= DP_NORESMNT;
   1178 				hp->ht_grp = grp;
   1179 				hp->ht_next = ep->ex_defdir->dp_hosts;
   1180 				ep->ex_defdir->dp_hosts = hp;
   1181 				grp = grp->gr_next;
   1182 			}
   1183 	} else {
   1184 
   1185 		/*
   1186 		 * Loop throught the directories adding them to the tree.
   1187 		 */
   1188 		while (dp) {
   1189 			dp2 = dp->dp_left;
   1190 			add_dlist(&ep->ex_dirl, dp, grp, flags);
   1191 			dp = dp2;
   1192 		}
   1193 	}
   1194 }
   1195 
   1196 /*
   1197  * Traverse the binary tree either updating a node that is already there
   1198  * for the new directory or adding the new node.
   1199  */
   1200 static void
   1201 add_dlist(dpp, newdp, grp, flags)
   1202 	struct dirlist **dpp;
   1203 	struct dirlist *newdp;
   1204 	struct grouplist *grp;
   1205 	int flags;
   1206 {
   1207 	struct dirlist *dp;
   1208 	struct hostlist *hp;
   1209 	int cmp;
   1210 
   1211 	dp = *dpp;
   1212 	if (dp) {
   1213 		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
   1214 		if (cmp > 0) {
   1215 			add_dlist(&dp->dp_left, newdp, grp, flags);
   1216 			return;
   1217 		} else if (cmp < 0) {
   1218 			add_dlist(&dp->dp_right, newdp, grp, flags);
   1219 			return;
   1220 		} else
   1221 			free(newdp);
   1222 	} else {
   1223 		dp = newdp;
   1224 		dp->dp_left = NULL;
   1225 		*dpp = dp;
   1226 	}
   1227 	if (grp) {
   1228 
   1229 		/*
   1230 		 * Hang all of the host(s) off of the directory point.
   1231 		 */
   1232 		do {
   1233 			hp = get_ht();
   1234 			if (flags & OP_KERB)
   1235 				hp->ht_flag |= DP_KERB;
   1236 			if (flags & OP_NORESMNT)
   1237 				hp->ht_flag |= DP_NORESMNT;
   1238 			hp->ht_grp = grp;
   1239 			hp->ht_next = dp->dp_hosts;
   1240 			dp->dp_hosts = hp;
   1241 			grp = grp->gr_next;
   1242 		} while (grp);
   1243 	} else {
   1244 		dp->dp_flag |= DP_DEFSET;
   1245 		if (flags & OP_KERB)
   1246 			dp->dp_flag |= DP_KERB;
   1247 		if (flags & OP_NORESMNT)
   1248 			dp->dp_flag |= DP_NORESMNT;
   1249 	}
   1250 }
   1251 
   1252 /*
   1253  * Search for a dirpath on the export point.
   1254  */
   1255 static struct dirlist *
   1256 dirp_search(dp, dirp)
   1257 	struct dirlist *dp;
   1258 	char *dirp;
   1259 {
   1260 	int cmp;
   1261 
   1262 	if (dp) {
   1263 		cmp = strcmp(dp->dp_dirp, dirp);
   1264 		if (cmp > 0)
   1265 			return (dirp_search(dp->dp_left, dirp));
   1266 		else if (cmp < 0)
   1267 			return (dirp_search(dp->dp_right, dirp));
   1268 		else
   1269 			return (dp);
   1270 	}
   1271 	return (dp);
   1272 }
   1273 
   1274 /*
   1275  * Scan for a host match in a directory tree.
   1276  */
   1277 static int
   1278 chk_host(dp, saddr, defsetp, hostsetp)
   1279 	struct dirlist *dp;
   1280 	u_int32_t saddr;
   1281 	int *defsetp;
   1282 	int *hostsetp;
   1283 {
   1284 	struct hostlist *hp;
   1285 	struct grouplist *grp;
   1286 	u_int32_t **addrp;
   1287 
   1288 	if (dp) {
   1289 		if (dp->dp_flag & DP_DEFSET)
   1290 			*defsetp = dp->dp_flag;
   1291 		hp = dp->dp_hosts;
   1292 		while (hp) {
   1293 			grp = hp->ht_grp;
   1294 			switch (grp->gr_type) {
   1295 			case GT_HOST:
   1296 				addrp = (u_int32_t **)
   1297 				    grp->gr_ptr.gt_hostent->h_addr_list;
   1298 				for (; *addrp; addrp++) {
   1299 					if (**addrp != saddr)
   1300 						continue;
   1301 					*hostsetp = (hp->ht_flag | DP_HOSTSET);
   1302 					return (1);
   1303 				}
   1304 				break;
   1305 			case GT_NET:
   1306 				if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
   1307 				    grp->gr_ptr.gt_net.nt_net) {
   1308 					*hostsetp = (hp->ht_flag | DP_HOSTSET);
   1309 					return (1);
   1310 				}
   1311 				break;
   1312 			};
   1313 			hp = hp->ht_next;
   1314 		}
   1315 	}
   1316 	return (0);
   1317 }
   1318 
   1319 /*
   1320  * Scan tree for a host that matches the address.
   1321  */
   1322 static int
   1323 scan_tree(dp, saddr)
   1324 	struct dirlist *dp;
   1325 	u_int32_t saddr;
   1326 {
   1327 	int defset, hostset;
   1328 
   1329 	if (dp) {
   1330 		if (scan_tree(dp->dp_left, saddr))
   1331 			return (1);
   1332 		if (chk_host(dp, saddr, &defset, &hostset))
   1333 			return (1);
   1334 		if (scan_tree(dp->dp_right, saddr))
   1335 			return (1);
   1336 	}
   1337 	return (0);
   1338 }
   1339 
   1340 /*
   1341  * Traverse the dirlist tree and free it up.
   1342  */
   1343 static void
   1344 free_dir(dp)
   1345 	struct dirlist *dp;
   1346 {
   1347 
   1348 	if (dp) {
   1349 		free_dir(dp->dp_left);
   1350 		free_dir(dp->dp_right);
   1351 		free_host(dp->dp_hosts);
   1352 		free(dp);
   1353 	}
   1354 }
   1355 
   1356 /*
   1357  * Parse the option string and update fields.
   1358  * Option arguments may either be -<option>=<value> or
   1359  * -<option> <value>
   1360  */
   1361 static int
   1362 do_opt(line, lineno, cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
   1363 	const char *line;
   1364 	size_t lineno;
   1365 	char **cpp, **endcpp;
   1366 	struct exportlist *ep;
   1367 	struct grouplist *grp;
   1368 	int *has_hostp;
   1369 	int *exflagsp;
   1370 	struct ucred *cr;
   1371 {
   1372 	char *cpoptarg, *cpoptend;
   1373 	char *cp, *endcp, *cpopt, savedc, savedc2;
   1374 	int allflag, usedarg;
   1375 
   1376 	cpopt = *cpp;
   1377 	cpopt++;
   1378 	cp = *endcpp;
   1379 	savedc = *cp;
   1380 	*cp = '\0';
   1381 	while (cpopt && *cpopt) {
   1382 		allflag = 1;
   1383 		usedarg = -2;
   1384 		savedc2 = '\0';
   1385 		if ((cpoptend = strchr(cpopt, ',')) != NULL) {
   1386 			*cpoptend++ = '\0';
   1387 			if ((cpoptarg = strchr(cpopt, '=')) != NULL)
   1388 				*cpoptarg++ = '\0';
   1389 		} else {
   1390 			if ((cpoptarg = strchr(cpopt, '=')) != NULL)
   1391 				*cpoptarg++ = '\0';
   1392 			else {
   1393 				*cp = savedc;
   1394 				nextfield(&cp, &endcp);
   1395 				**endcpp = '\0';
   1396 				if (endcp > cp && *cp != '-') {
   1397 					cpoptarg = cp;
   1398 					savedc2 = *endcp;
   1399 					*endcp = '\0';
   1400 					usedarg = 0;
   1401 				}
   1402 			}
   1403 		}
   1404 		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
   1405 			*exflagsp |= MNT_EXRDONLY;
   1406 		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
   1407 		    !(allflag = strcmp(cpopt, "mapall")) ||
   1408 		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
   1409 			usedarg++;
   1410 			parsecred(cpoptarg, cr);
   1411 			if (allflag == 0) {
   1412 				*exflagsp |= MNT_EXPORTANON;
   1413 				opt_flags |= OP_MAPALL;
   1414 			} else
   1415 				opt_flags |= OP_MAPROOT;
   1416 		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
   1417 			*exflagsp |= MNT_EXKERB;
   1418 			opt_flags |= OP_KERB;
   1419 		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
   1420 		    !strcmp(cpopt, "m"))) {
   1421 			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
   1422 				syslog(LOG_ERR,
   1423 				    "\"%s\", line %ld: Bad mask: %s",
   1424 				    line, (unsigned long)lineno, cpoptarg);
   1425 				return (1);
   1426 			}
   1427 			usedarg++;
   1428 			opt_flags |= OP_MASK;
   1429 		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
   1430 		    !strcmp(cpopt, "n"))) {
   1431 			if (grp->gr_type != GT_NULL) {
   1432 				syslog(LOG_ERR,
   1433 				    "\"%s\", line %ld: Network/host conflict",
   1434 				    line, (unsigned long)lineno);
   1435 				return (1);
   1436 			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
   1437 				syslog(LOG_ERR,
   1438 				    "\"%s\", line %ld: Bad net: %s",
   1439 				    line, (unsigned long)lineno, cpoptarg);
   1440 				return (1);
   1441 			}
   1442 			grp->gr_type = GT_NET;
   1443 			*has_hostp = 1;
   1444 			usedarg++;
   1445 			opt_flags |= OP_NET;
   1446 		} else if (!strcmp(cpopt, "alldirs")) {
   1447 			opt_flags |= OP_ALLDIRS;
   1448 		} else if (!strcmp(cpopt, "noresvmnt")) {
   1449 			opt_flags |= OP_NORESMNT;
   1450 		} else if (!strcmp(cpopt, "noresvport")) {
   1451 			opt_flags |= OP_NORESPORT;
   1452 			*exflagsp |= MNT_EXNORESPORT;
   1453 		} else if (!strcmp(cpopt, "public")) {
   1454 			*exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC);
   1455 			opt_flags |= OP_NORESPORT;
   1456 		} else if (!strcmp(cpopt, "webnfs")) {
   1457 			*exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC |
   1458 			    MNT_EXRDONLY | MNT_EXPORTANON);
   1459 			opt_flags |= (OP_MAPALL | OP_NORESPORT);
   1460 		} else if (cpoptarg && !strcmp(cpopt, "index")) {
   1461 			ep->ex_indexfile = strdup(cpoptarg);
   1462 #ifdef ISO
   1463 		} else if (cpoptarg && !strcmp(cpopt, "iso")) {
   1464 			if (get_isoaddr(line, lineno, cpoptarg, grp))
   1465 				return (1);
   1466 			*has_hostp = 1;
   1467 			usedarg++;
   1468 			opt_flags |= OP_ISO;
   1469 #endif /* ISO */
   1470 		} else {
   1471 			syslog(LOG_ERR,
   1472 			    "\"%s\", line %ld: Bad opt %s",
   1473 			    line, (unsigned long)lineno, cpopt);
   1474 			return (1);
   1475 		}
   1476 		if (usedarg >= 0) {
   1477 			*endcp = savedc2;
   1478 			**endcpp = savedc;
   1479 			if (usedarg > 0) {
   1480 				*cpp = cp;
   1481 				*endcpp = endcp;
   1482 			}
   1483 			return (0);
   1484 		}
   1485 		cpopt = cpoptend;
   1486 	}
   1487 	**endcpp = savedc;
   1488 	return (0);
   1489 }
   1490 
   1491 /*
   1492  * Translate a character string to the corresponding list of network
   1493  * addresses for a hostname.
   1494  */
   1495 static int
   1496 get_host(line, lineno, cp, grp)
   1497 	const char *line;
   1498 	size_t lineno;
   1499 	const char *cp;
   1500 	struct grouplist *grp;
   1501 {
   1502 	struct hostent *hp, *nhp;
   1503 	char **addrp, **naddrp;
   1504 	struct hostent  t_host;
   1505 	int i;
   1506 	u_int32_t saddr;
   1507 	char *aptr[2];
   1508 
   1509 	if (grp->gr_type != GT_NULL) {
   1510 		syslog(LOG_ERR,
   1511 		    "\"%s\", line %ld: Bad netgroup type for ip host %s",
   1512 		    line, (unsigned long)lineno, cp);
   1513 		return (1);
   1514 	}
   1515 	if ((hp = gethostbyname(cp)) == NULL) {
   1516 		if (isdigit(*cp)) {
   1517 			saddr = inet_addr(cp);
   1518 			if (saddr == -1) {
   1519 				syslog(LOG_ERR,
   1520 				    "\"%s\", line %ld: inet_addr failed for %s",
   1521 				    line, (unsigned long) lineno, cp);
   1522 				return (1);
   1523 			}
   1524 			if ((hp = gethostbyaddr((const char *) &saddr,
   1525 			    sizeof(saddr), AF_INET)) == NULL) {
   1526 				hp = &t_host;
   1527 				hp->h_name = (char *) cp;
   1528 				hp->h_addrtype = AF_INET;
   1529 				hp->h_length = sizeof(u_int32_t);
   1530 				hp->h_addr_list = aptr;
   1531 				aptr[0] = (char *) &saddr;
   1532 				aptr[1] = NULL;
   1533 			}
   1534 		} else {
   1535 			syslog(LOG_ERR,
   1536 			    "\"%s\", line %ld: gethostbyname failed for %s: %s",
   1537 			    line, (unsigned long) lineno, cp,
   1538 			    hstrerror(h_errno));
   1539 			return (1);
   1540 		}
   1541 	}
   1542 	grp->gr_type = GT_HOST;
   1543 	nhp = grp->gr_ptr.gt_hostent = emalloc(sizeof(struct hostent));
   1544 	(void)memcpy(nhp, hp, sizeof(struct hostent));
   1545 	nhp->h_name = estrdup(hp->h_name);
   1546 	addrp = hp->h_addr_list;
   1547 	i = 1;
   1548 	while (*addrp++)
   1549 		i++;
   1550 	naddrp = nhp->h_addr_list = emalloc(i * sizeof(char *));
   1551 	addrp = hp->h_addr_list;
   1552 	while (*addrp) {
   1553 		*naddrp = emalloc(hp->h_length);
   1554 		(void)memcpy(*naddrp, *addrp, hp->h_length);
   1555 		addrp++;
   1556 		naddrp++;
   1557 	}
   1558 	*naddrp = NULL;
   1559 	if (debug)
   1560 		(void)fprintf(stderr, "got host %s\n", hp->h_name);
   1561 	return (0);
   1562 }
   1563 
   1564 /*
   1565  * Free up an exports list component
   1566  */
   1567 static void
   1568 free_exp(ep)
   1569 	struct exportlist *ep;
   1570 {
   1571 
   1572 	if (ep->ex_defdir) {
   1573 		free_host(ep->ex_defdir->dp_hosts);
   1574 		free(ep->ex_defdir);
   1575 	}
   1576 	if (ep->ex_fsdir)
   1577 		free(ep->ex_fsdir);
   1578 	if (ep->ex_indexfile)
   1579 		free(ep->ex_indexfile);
   1580 	free_dir(ep->ex_dirl);
   1581 	free(ep);
   1582 }
   1583 
   1584 /*
   1585  * Free hosts.
   1586  */
   1587 static void
   1588 free_host(hp)
   1589 	struct hostlist *hp;
   1590 {
   1591 	struct hostlist *hp2;
   1592 
   1593 	while (hp) {
   1594 		hp2 = hp;
   1595 		hp = hp->ht_next;
   1596 		free(hp2);
   1597 	}
   1598 }
   1599 
   1600 static struct hostlist *
   1601 get_ht()
   1602 {
   1603 	struct hostlist *hp;
   1604 
   1605 	hp = emalloc(sizeof(struct hostlist));
   1606 	hp->ht_next = NULL;
   1607 	hp->ht_flag = 0;
   1608 	return (hp);
   1609 }
   1610 
   1611 #ifdef ISO
   1612 /*
   1613  * Translate an iso address.
   1614  */
   1615 static int
   1616 get_isoaddr(line, lineno, cp, grp)
   1617 	const char *line;
   1618 	size_t lineno;
   1619 	char *cp;
   1620 	struct grouplist *grp;
   1621 {
   1622 	struct iso_addr *isop;
   1623 	struct sockaddr_iso *isoaddr;
   1624 
   1625 	if (grp->gr_type != GT_NULL) {
   1626 		syslog(LOG_ERR,
   1627 		    "\"%s\", line %ld: Bad netgroup type for iso addr %s",
   1628 		    line, (unsigned long)lineno, cp);
   1629 		return (1);
   1630 	}
   1631 	if ((isop = iso_addr(cp)) == NULL) {
   1632 		syslog(LOG_ERR,
   1633 		    "\"%s\", line %ld: Bad iso addr %s",
   1634 		    line, (unsigned long)lineno, cp);
   1635 		return (1);
   1636 	}
   1637 	isoaddr = emalloc(sizeof(struct sockaddr_iso));
   1638 	(void)memset(isoaddr, 0, sizeof(struct sockaddr_iso));
   1639 	(void)memcpy(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
   1640 	isoaddr->siso_len = sizeof(struct sockaddr_iso);
   1641 	isoaddr->siso_family = AF_ISO;
   1642 	grp->gr_type = GT_ISO;
   1643 	grp->gr_ptr.gt_isoaddr = isoaddr;
   1644 	return (0);
   1645 }
   1646 #endif				/* ISO */
   1647 
   1648 /*
   1649  * error checked malloc and strdup
   1650  */
   1651 static void *
   1652 emalloc(n)
   1653 	size_t n;
   1654 {
   1655 	void *ptr = malloc(n);
   1656 
   1657 	if (ptr == NULL) {
   1658 		syslog(LOG_ERR, "%m");
   1659 		exit(2);
   1660 	}
   1661 	return ptr;
   1662 }
   1663 
   1664 static char *
   1665 estrdup(s)
   1666 	const char *s;
   1667 {
   1668 	char *n = strdup(s);
   1669 
   1670 	if (n == NULL) {
   1671 		syslog(LOG_ERR, "%m");
   1672 		exit(2);
   1673 	}
   1674 	return n;
   1675 }
   1676 
   1677 /*
   1678  * Do the mount syscall with the update flag to push the export info into
   1679  * the kernel.
   1680  */
   1681 static int
   1682 do_mount(line, lineno, ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
   1683 	const char *line;
   1684 	size_t lineno;
   1685 	struct exportlist *ep;
   1686 	struct grouplist *grp;
   1687 	int exflags;
   1688 	struct ucred *anoncrp;
   1689 	char *dirp;
   1690 	int dirplen;
   1691 	struct statfs *fsb;
   1692 {
   1693 	char *cp = NULL;
   1694 	u_int32_t **addrp;
   1695 	int done;
   1696 	char savedc = '\0';
   1697 	struct sockaddr_in sin, imask;
   1698 	union {
   1699 		struct ufs_args ua;
   1700 		struct iso_args ia;
   1701 		struct mfs_args ma;
   1702 		struct msdosfs_args da;
   1703 		struct adosfs_args aa;
   1704 	} args;
   1705 	u_int32_t net;
   1706 
   1707 	args.ua.fspec = 0;
   1708 	args.ua.export.ex_flags = exflags;
   1709 	args.ua.export.ex_anon = *anoncrp;
   1710 	args.ua.export.ex_indexfile = ep->ex_indexfile;
   1711 	(void)memset(&sin, 0, sizeof(sin));
   1712 	(void)memset(&imask, 0, sizeof(imask));
   1713 	sin.sin_family = AF_INET;
   1714 	sin.sin_len = sizeof(sin);
   1715 	imask.sin_family = AF_INET;
   1716 	imask.sin_len = sizeof(sin);
   1717 	if (grp->gr_type == GT_HOST)
   1718 		addrp = (u_int32_t **) grp->gr_ptr.gt_hostent->h_addr_list;
   1719 	else
   1720 		addrp = NULL;
   1721 	done = FALSE;
   1722 	while (!done) {
   1723 		switch (grp->gr_type) {
   1724 		case GT_HOST:
   1725 			if (addrp) {
   1726 				sin.sin_addr.s_addr = **addrp;
   1727 				args.ua.export.ex_addrlen = sizeof(sin);
   1728 			} else
   1729 				args.ua.export.ex_addrlen = 0;
   1730 			args.ua.export.ex_addr = (struct sockaddr *)&sin;
   1731 			args.ua.export.ex_masklen = 0;
   1732 			break;
   1733 		case GT_NET:
   1734 			if (grp->gr_ptr.gt_net.nt_mask)
   1735 				imask.sin_addr.s_addr =
   1736 				    grp->gr_ptr.gt_net.nt_mask;
   1737 			else {
   1738 				net = ntohl(grp->gr_ptr.gt_net.nt_net);
   1739 				if (IN_CLASSA(net))
   1740 					imask.sin_addr.s_addr =
   1741 					    inet_addr("255.0.0.0");
   1742 				else if (IN_CLASSB(net))
   1743 					imask.sin_addr.s_addr =
   1744 					    inet_addr("255.255.0.0");
   1745 				else
   1746 					imask.sin_addr.s_addr =
   1747 					    inet_addr("255.255.255.0");
   1748 				grp->gr_ptr.gt_net.nt_mask =
   1749 				    imask.sin_addr.s_addr;
   1750 			}
   1751 			sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
   1752 			args.ua.export.ex_addr = (struct sockaddr *) &sin;
   1753 			args.ua.export.ex_addrlen = sizeof(sin);
   1754 			args.ua.export.ex_mask = (struct sockaddr *) &imask;
   1755 			args.ua.export.ex_masklen = sizeof(imask);
   1756 			break;
   1757 #ifdef ISO
   1758 		case GT_ISO:
   1759 			args.ua.export.ex_addr =
   1760 			    (struct sockaddr *) grp->gr_ptr.gt_isoaddr;
   1761 			args.ua.export.ex_addrlen =
   1762 			    sizeof(struct sockaddr_iso);
   1763 			args.ua.export.ex_masklen = 0;
   1764 			break;
   1765 #endif				/* ISO */
   1766 		default:
   1767 			syslog(LOG_ERR, "\"%s\", line %ld: Bad netgroup type",
   1768 			    line, (unsigned long)lineno);
   1769 			if (cp)
   1770 				*cp = savedc;
   1771 			return (1);
   1772 		};
   1773 
   1774 		/*
   1775 		 * XXX:
   1776 		 * Maybe I should just use the fsb->f_mntonname path instead
   1777 		 * of looping back up the dirp to the mount point??
   1778 		 * Also, needs to know how to export all types of local
   1779 		 * exportable file systems and not just MOUNT_FFS.
   1780 		 */
   1781 		while (mount(fsb->f_fstypename, dirp,
   1782 		    fsb->f_flags | MNT_UPDATE, &args) == -1) {
   1783 			if (cp)
   1784 				*cp-- = savedc;
   1785 			else
   1786 				cp = dirp + dirplen - 1;
   1787 			if (errno == EPERM) {
   1788 				syslog(LOG_ERR,
   1789 		    "\"%s\", line %ld: Can't change attributes for %s to %s",
   1790 				    line, (unsigned long)lineno,
   1791 				    dirp, (grp->gr_type == GT_HOST) ?
   1792 				    grp->gr_ptr.gt_hostent->h_name :
   1793 				    (grp->gr_type == GT_NET) ?
   1794 				    grp->gr_ptr.gt_net.nt_name :
   1795 				    "Unknown");
   1796 				return (1);
   1797 			}
   1798 			if (opt_flags & OP_ALLDIRS) {
   1799 				syslog(LOG_ERR,
   1800 				"\"%s\", line %ld: Could not remount %s: %m",
   1801 				    line, (unsigned long)lineno,
   1802 				    dirp);
   1803 				return (1);
   1804 			}
   1805 			/* back up over the last component */
   1806 			while (*cp == '/' && cp > dirp)
   1807 				cp--;
   1808 			while (*(cp - 1) != '/' && cp > dirp)
   1809 				cp--;
   1810 			if (cp == dirp) {
   1811 				if (debug)
   1812 					(void)fprintf(stderr, "mnt unsucc\n");
   1813 				syslog(LOG_ERR,
   1814 				    "\"%s\", line %ld: Can't export %s",
   1815 				    line, (unsigned long)lineno, dirp);
   1816 				return (1);
   1817 			}
   1818 			savedc = *cp;
   1819 			*cp = '\0';
   1820 		}
   1821 		if (addrp) {
   1822 			++addrp;
   1823 			if (*addrp == NULL)
   1824 				done = TRUE;
   1825 		} else
   1826 			done = TRUE;
   1827 	}
   1828 	if (cp)
   1829 		*cp = savedc;
   1830 	return (0);
   1831 }
   1832 
   1833 /*
   1834  * Translate a net address.
   1835  */
   1836 static int
   1837 get_net(cp, net, maskflg)
   1838 	char *cp;
   1839 	struct netmsk *net;
   1840 	int maskflg;
   1841 {
   1842 	struct netent *np;
   1843 	long netaddr;
   1844 	struct in_addr inetaddr, inetaddr2;
   1845 	char *name;
   1846 
   1847 	if ((np = getnetbyname(cp)) != NULL)
   1848 		inetaddr = inet_makeaddr(np->n_net, 0);
   1849 	else if (isdigit(*cp)) {
   1850 		if ((netaddr = inet_network(cp)) == -1)
   1851 			return (1);
   1852 		inetaddr = inet_makeaddr(netaddr, 0);
   1853 		/*
   1854 		 * Due to arbritrary subnet masks, you don't know how many
   1855 		 * bits to shift the address to make it into a network,
   1856 		 * however you do know how to make a network address into
   1857 		 * a host with host == 0 and then compare them.
   1858 		 * (What a pest)
   1859 		 */
   1860 		if (!maskflg) {
   1861 			setnetent(0);
   1862 			while ((np = getnetent()) != NULL) {
   1863 				inetaddr2 = inet_makeaddr(np->n_net, 0);
   1864 				if (inetaddr2.s_addr == inetaddr.s_addr)
   1865 					break;
   1866 			}
   1867 			endnetent();
   1868 		}
   1869 	} else
   1870 		return (1);
   1871 	if (maskflg)
   1872 		net->nt_mask = inetaddr.s_addr;
   1873 	else {
   1874 		if (np)
   1875 			name = np->n_name;
   1876 		else
   1877 			name = inet_ntoa(inetaddr);
   1878 		net->nt_name = estrdup(name);
   1879 		net->nt_net = inetaddr.s_addr;
   1880 	}
   1881 	return (0);
   1882 }
   1883 
   1884 /*
   1885  * Parse out the next white space separated field
   1886  */
   1887 static void
   1888 nextfield(cp, endcp)
   1889 	char **cp;
   1890 	char **endcp;
   1891 {
   1892 	char *p;
   1893 
   1894 	p = *cp;
   1895 	while (*p == ' ' || *p == '\t')
   1896 		p++;
   1897 	if (*p == '\n' || *p == '\0')
   1898 		*cp = *endcp = p;
   1899 	else {
   1900 		*cp = p++;
   1901 		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
   1902 			p++;
   1903 		*endcp = p;
   1904 	}
   1905 }
   1906 
   1907 /*
   1908  * Parse a description of a credential.
   1909  */
   1910 static void
   1911 parsecred(namelist, cr)
   1912 	char *namelist;
   1913 	struct ucred *cr;
   1914 {
   1915 	char *name;
   1916 	int cnt;
   1917 	char *names;
   1918 	struct passwd *pw;
   1919 	struct group *gr;
   1920 	int ngroups, groups[NGROUPS + 1];
   1921 
   1922 	/*
   1923 	 * Set up the unprivileged user.
   1924 	 */
   1925 	cr->cr_ref = 1;
   1926 	cr->cr_uid = -2;
   1927 	cr->cr_gid = -2;
   1928 	cr->cr_ngroups = 0;
   1929 	/*
   1930 	 * Get the user's password table entry.
   1931 	 */
   1932 	names = strsep(&namelist, " \t\n");
   1933 	name = strsep(&names, ":");
   1934 	if (isdigit(*name) || *name == '-')
   1935 		pw = getpwuid(atoi(name));
   1936 	else
   1937 		pw = getpwnam(name);
   1938 	/*
   1939 	 * Credentials specified as those of a user.
   1940 	 */
   1941 	if (names == NULL) {
   1942 		if (pw == NULL) {
   1943 			syslog(LOG_ERR, "Unknown user: %s", name);
   1944 			return;
   1945 		}
   1946 		cr->cr_uid = pw->pw_uid;
   1947 		ngroups = NGROUPS + 1;
   1948 		if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
   1949 			syslog(LOG_ERR, "Too many groups");
   1950 		/*
   1951 		 * Convert from int's to gid_t's and compress out duplicate
   1952 		 */
   1953 		cr->cr_ngroups = ngroups - 1;
   1954 		cr->cr_gid = groups[0];
   1955 		for (cnt = 1; cnt < ngroups; cnt++)
   1956 			cr->cr_groups[cnt - 1] = groups[cnt];
   1957 		return;
   1958 	}
   1959 	/*
   1960 	 * Explicit credential specified as a colon separated list:
   1961 	 *	uid:gid:gid:...
   1962 	 */
   1963 	if (pw != NULL)
   1964 		cr->cr_uid = pw->pw_uid;
   1965 	else if (isdigit(*name) || *name == '-')
   1966 		cr->cr_uid = atoi(name);
   1967 	else {
   1968 		syslog(LOG_ERR, "Unknown user: %s", name);
   1969 		return;
   1970 	}
   1971 	cr->cr_ngroups = 0;
   1972 	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
   1973 		name = strsep(&names, ":");
   1974 		if (isdigit(*name) || *name == '-') {
   1975 			cr->cr_groups[cr->cr_ngroups++] = atoi(name);
   1976 		} else {
   1977 			if ((gr = getgrnam(name)) == NULL) {
   1978 				syslog(LOG_ERR, "Unknown group: %s", name);
   1979 				continue;
   1980 			}
   1981 			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
   1982 		}
   1983 	}
   1984 	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
   1985 		syslog(LOG_ERR, "Too many groups");
   1986 }
   1987 
   1988 #define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
   1989 /*
   1990  * Routines that maintain the remote mounttab
   1991  */
   1992 static void
   1993 get_mountlist()
   1994 {
   1995 	struct mountlist *mlp, **mlpp;
   1996 	char *host, *dirp, *cp;
   1997 	char str[STRSIZ];
   1998 	FILE *mlfile;
   1999 
   2000 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
   2001 		syslog(LOG_ERR, "Can't open %s: %m", _PATH_RMOUNTLIST);
   2002 		return;
   2003 	}
   2004 	mlpp = &mlhead;
   2005 	while (fgets(str, STRSIZ, mlfile) != NULL) {
   2006 		cp = str;
   2007 		host = strsep(&cp, " \t\n");
   2008 		dirp = strsep(&cp, " \t\n");
   2009 		if (host == NULL || dirp == NULL)
   2010 			continue;
   2011 		mlp = emalloc(sizeof(*mlp));
   2012 		(void)strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
   2013 		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
   2014 		(void)strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
   2015 		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
   2016 		mlp->ml_next = NULL;
   2017 		*mlpp = mlp;
   2018 		mlpp = &mlp->ml_next;
   2019 	}
   2020 	(void)fclose(mlfile);
   2021 }
   2022 
   2023 static int
   2024 del_mlist(hostp, dirp, saddr)
   2025 	char *hostp, *dirp;
   2026 	struct sockaddr *saddr;
   2027 {
   2028 	struct mountlist *mlp, **mlpp;
   2029 	struct mountlist *mlp2;
   2030 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
   2031 	FILE *mlfile;
   2032 	int fnd = 0, ret = 0;
   2033 
   2034 	mlpp = &mlhead;
   2035 	mlp = mlhead;
   2036 	while (mlp) {
   2037 		if (!strcmp(mlp->ml_host, hostp) &&
   2038 		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
   2039 			if (!(mlp->ml_flag & DP_NORESMNT) &&
   2040 			    ntohs(sin->sin_port) >= IPPORT_RESERVED) {
   2041 				syslog(LOG_NOTICE,
   2042 				"Umount request for %s:%s from %s refused\n",
   2043 				    mlp->ml_host, mlp->ml_dirp,
   2044 				    inet_ntoa(sin->sin_addr));
   2045 				ret = -1;
   2046 				goto cont;
   2047 			}
   2048 			fnd = 1;
   2049 			mlp2 = mlp;
   2050 			*mlpp = mlp = mlp->ml_next;
   2051 			free(mlp2);
   2052 		} else {
   2053 cont:
   2054 			mlpp = &mlp->ml_next;
   2055 			mlp = mlp->ml_next;
   2056 		}
   2057 	}
   2058 	if (fnd) {
   2059 		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
   2060 			syslog(LOG_ERR, "Can't update %s: %m",
   2061 			    _PATH_RMOUNTLIST);
   2062 			return ret;
   2063 		}
   2064 		mlp = mlhead;
   2065 		while (mlp) {
   2066 			(void)fprintf(mlfile, "%s %s\n", mlp->ml_host,
   2067 		            mlp->ml_dirp);
   2068 			mlp = mlp->ml_next;
   2069 		}
   2070 		(void)fclose(mlfile);
   2071 	}
   2072 	return ret;
   2073 }
   2074 
   2075 static void
   2076 add_mlist(hostp, dirp, flags)
   2077 	char *hostp, *dirp;
   2078 	int flags;
   2079 {
   2080 	struct mountlist *mlp, **mlpp;
   2081 	FILE *mlfile;
   2082 
   2083 	mlpp = &mlhead;
   2084 	mlp = mlhead;
   2085 	while (mlp) {
   2086 		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
   2087 			return;
   2088 		mlpp = &mlp->ml_next;
   2089 		mlp = mlp->ml_next;
   2090 	}
   2091 	mlp = emalloc(sizeof(*mlp));
   2092 	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
   2093 	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
   2094 	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
   2095 	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
   2096 	mlp->ml_flag = flags;
   2097 	mlp->ml_next = NULL;
   2098 	*mlpp = mlp;
   2099 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
   2100 		syslog(LOG_ERR, "Can't update %s: %m", _PATH_RMOUNTLIST);
   2101 		return;
   2102 	}
   2103 	(void)fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
   2104 	(void)fclose(mlfile);
   2105 }
   2106 
   2107 /*
   2108  * This function is called via. SIGTERM when the system is going down.
   2109  * It sends a broadcast RPCMNT_UMNTALL.
   2110  */
   2111 /* ARGSUSED */
   2112 static void
   2113 send_umntall(n)
   2114 	int n;
   2115 {
   2116 	(void)clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
   2117 	    xdr_void, NULL, xdr_void, NULL, umntall_each);
   2118 	exit(0);
   2119 }
   2120 
   2121 static int
   2122 umntall_each(resultsp, raddr)
   2123 	caddr_t resultsp;
   2124 	struct sockaddr_in *raddr;
   2125 {
   2126 	return (1);
   2127 }
   2128 
   2129 /*
   2130  * Free up a group list.
   2131  */
   2132 static void
   2133 free_grp(grp)
   2134 	struct grouplist *grp;
   2135 {
   2136 	char **addrp;
   2137 
   2138 	if (grp->gr_type == GT_HOST) {
   2139 		if (grp->gr_ptr.gt_hostent->h_name) {
   2140 			addrp = grp->gr_ptr.gt_hostent->h_addr_list;
   2141 			if (addrp) {
   2142 				while (*addrp)
   2143 					free(*addrp++);
   2144 				free(grp->gr_ptr.gt_hostent->h_addr_list);
   2145 			}
   2146 			free(grp->gr_ptr.gt_hostent->h_name);
   2147 		}
   2148 		free(grp->gr_ptr.gt_hostent);
   2149 	} else if (grp->gr_type == GT_NET) {
   2150 		if (grp->gr_ptr.gt_net.nt_name)
   2151 			free(grp->gr_ptr.gt_net.nt_name);
   2152 	}
   2153 #ifdef ISO
   2154 	else if (grp->gr_type == GT_ISO)
   2155 		free(grp->gr_ptr.gt_isoaddr);
   2156 #endif
   2157 	free(grp);
   2158 }
   2159 
   2160 #if 0
   2161 static void
   2162 SYSLOG(int pri, const char *fmt,...)
   2163 {
   2164 	va_list ap;
   2165 
   2166 	va_start(ap, fmt);
   2167 
   2168 	if (debug)
   2169 		vfprintf(stderr, fmt, ap);
   2170 	else
   2171 		vsyslog(pri, fmt, ap);
   2172 
   2173 	va_end(ap);
   2174 }
   2175 #endif
   2176 
   2177 /*
   2178  * Check options for consistency.
   2179  */
   2180 static int
   2181 check_options(line, lineno, dp)
   2182 	const char *line;
   2183 	size_t lineno;
   2184 	struct dirlist *dp;
   2185 {
   2186 
   2187 	if (dp == NULL) {
   2188 		syslog(LOG_ERR,
   2189 		    "\"%s\", line %ld: missing directory list",
   2190 		    line, (unsigned long)lineno);
   2191 		return (1);
   2192 	}
   2193 	if ((opt_flags & (OP_MAPROOT|OP_MAPALL)) == (OP_MAPROOT|OP_MAPALL) ||
   2194 	    (opt_flags & (OP_MAPROOT|OP_KERB)) == (OP_MAPROOT|OP_KERB) ||
   2195 	    (opt_flags & (OP_MAPALL|OP_KERB)) == (OP_MAPALL|OP_KERB)) {
   2196 		syslog(LOG_ERR,
   2197 		    "\"%s\", line %ld: -mapall, -maproot and -kerb mutually exclusive",
   2198 		    line, (unsigned long)lineno);
   2199 		return (1);
   2200 	}
   2201 	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
   2202 		syslog(LOG_ERR, "\"%s\", line %ld: -mask requires -net",
   2203 		    line, (unsigned long)lineno);
   2204 		return (1);
   2205 	}
   2206 	if ((opt_flags & (OP_NET|OP_ISO)) == (OP_NET|OP_ISO)) {
   2207 		syslog(LOG_ERR,
   2208 		    "\"%s\", line %ld: -net and -iso mutually exclusive",
   2209 		    line, (unsigned long)lineno);
   2210 		return (1);
   2211 	}
   2212 	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
   2213 		syslog(LOG_ERR,
   2214 		    "\"%s\", line %ld: -alldir has multiple directories",
   2215 		    line, (unsigned long)lineno);
   2216 		return (1);
   2217 	}
   2218 	return (0);
   2219 }
   2220 
   2221 /*
   2222  * Check an absolute directory path for any symbolic links. Return true
   2223  * if no symbolic links are found.
   2224  */
   2225 static int
   2226 check_dirpath(line, lineno, dirp)
   2227 	const char *line;
   2228 	size_t lineno;
   2229 	char *dirp;
   2230 {
   2231 	char *cp;
   2232 	struct stat sb;
   2233 	char *file = "";
   2234 
   2235 	for (cp = dirp + 1; *cp; cp++) {
   2236 		if (*cp == '/') {
   2237 			*cp = '\0';
   2238 			if (lstat(dirp, &sb) == -1)
   2239 				goto bad;
   2240 			if (!S_ISDIR(sb.st_mode))
   2241 				goto bad1;
   2242 			*cp = '/';
   2243 		}
   2244 	}
   2245 
   2246 	cp = NULL;
   2247 	if (lstat(dirp, &sb) == -1)
   2248 		goto bad;
   2249 
   2250 	if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
   2251 		file = " file or a";
   2252 		goto bad1;
   2253 	}
   2254 
   2255 	return 1;
   2256 
   2257 bad:
   2258 	syslog(LOG_ERR,
   2259 	    "\"%s\", line %ld: lstat for `%s' failed: %m",
   2260 	    line, (unsigned long)lineno, dirp);
   2261 	if (cp)
   2262 		*cp = '/';
   2263 	return 0;
   2264 
   2265 bad1:
   2266 	syslog(LOG_ERR,
   2267 	    "\"%s\", line %ld: `%s' is not a%s directory",
   2268 	    line, (unsigned long)lineno, dirp, file);
   2269 	if (cp)
   2270 		*cp = '/';
   2271 	return 0;
   2272 }
   2273