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