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