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