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