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