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