xref: /minix3/tests/fs/nfs/nfsservice/mountd.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /* 	$NetBSD: mountd.c,v 1.9 2015/08/21 14:19:10 christos Exp $	 */
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Herb Hasler and Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38  The Regents of the University of California.  All rights reserved.");
39 #endif				/* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char     sccsid[] = "@(#)mountd.c  8.15 (Berkeley) 5/1/95";
44 #else
45 __RCSID("$NetBSD: mountd.c,v 1.9 2015/08/21 14:19:10 christos Exp $");
46 #endif
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 <syslog.h>
56 #include <sys/ucred.h>
57 
58 #include <rpc/rpc.h>
59 #include <rpc/pmap_clnt.h>
60 #include <rpc/pmap_prot.h>
61 #include <rpcsvc/mount.h>
62 #include <nfs/rpcv2.h>
63 #include <nfs/nfsproto.h>
64 #include <nfs/nfs.h>
65 #include <nfs/nfsmount.h>
66 
67 #include <arpa/inet.h>
68 
69 #include <rump/rump.h>
70 #include <rump/rump_syscalls.h>
71 
72 #include <ctype.h>
73 #include <errno.h>
74 #include <grp.h>
75 #include <netdb.h>
76 #include <pwd.h>
77 #include <netgroup.h>
78 #include <pthread.h>
79 #include <semaphore.h>
80 #include <signal.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <unistd.h>
85 #include <err.h>
86 #include <util.h>
87 #include "pathnames.h"
88 
89 #ifdef IPSEC
90 #include <netinet6/ipsec.h>
91 #ifndef IPSEC_POLICY_IPSEC	/* no ipsec support on old ipsec */
92 #undef IPSEC
93 #endif
94 #include "ipsec.h"
95 #endif
96 
97 #include "svc_fdset.h"
98 
99 #include <stdarg.h>
100 
101 /*
102  * Structures for keeping the mount list and export list
103  */
104 struct mountlist {
105 	struct mountlist *ml_next;
106 	char ml_host[RPCMNT_NAMELEN + 1];
107 	char ml_dirp[RPCMNT_PATHLEN + 1];
108 	int ml_flag;/* XXX more flags (same as dp_flag) */
109 };
110 
111 struct dirlist {
112 	struct dirlist *dp_left;
113 	struct dirlist *dp_right;
114 	int dp_flag;
115 	struct hostlist *dp_hosts;	/* List of hosts this dir exported to */
116 	char dp_dirp[1];		/* Actually malloc'd to size of dir */
117 };
118 /* dp_flag bits */
119 #define	DP_DEFSET	0x1
120 #define DP_HOSTSET	0x2
121 #define DP_KERB		0x4
122 #define DP_NORESMNT	0x8
123 
124 struct exportlist {
125 	struct exportlist *ex_next;
126 	struct dirlist *ex_dirl;
127 	struct dirlist *ex_defdir;
128 	int             ex_flag;
129 	fsid_t          ex_fs;
130 	char           *ex_fsdir;
131 	char           *ex_indexfile;
132 };
133 /* ex_flag bits */
134 #define	EX_LINKED	0x1
135 
136 struct netmsk {
137 	struct sockaddr_storage nt_net;
138 	int		nt_len;
139 	char           *nt_name;
140 };
141 
142 union grouptypes {
143 	struct addrinfo *gt_addrinfo;
144 	struct netmsk   gt_net;
145 };
146 
147 struct grouplist {
148 	int             gr_type;
149 	union grouptypes gr_ptr;
150 	struct grouplist *gr_next;
151 };
152 /* Group types */
153 #define	GT_NULL		0x0
154 #define	GT_HOST		0x1
155 #define	GT_NET		0x2
156 
157 struct hostlist {
158 	int             ht_flag;/* Uses DP_xx bits */
159 	struct grouplist *ht_grp;
160 	struct hostlist *ht_next;
161 };
162 
163 struct fhreturn {
164 	int             fhr_flag;
165 	int             fhr_vers;
166 	size_t		fhr_fhsize;
167 	union {
168 		uint8_t v2[NFSX_V2FH];
169 		uint8_t v3[NFSX_V3FHMAX];
170 	} fhr_fh;
171 };
172 
173 /* Global defs */
174 static char    *add_expdir __P((struct dirlist **, char *, int));
175 static void add_dlist __P((struct dirlist **, struct dirlist *,
176     struct grouplist *, int));
177 static void add_mlist __P((char *, char *, int));
178 static int check_dirpath __P((const char *, size_t, char *));
179 static int check_options __P((const char *, size_t, struct dirlist *));
180 static int chk_host __P((struct dirlist *, struct sockaddr *, int *, int *));
181 static int del_mlist __P((char *, char *, struct sockaddr *));
182 static struct dirlist *dirp_search __P((struct dirlist *, char *));
183 static int do_nfssvc __P((const char *, size_t, struct exportlist *,
184     struct grouplist *, int, struct uucred *, char *, int, struct statvfs *));
185 static int do_opt __P((const char *, size_t, char **, char **,
186     struct exportlist *, struct grouplist *, int *, int *, struct uucred *));
187 static struct exportlist *ex_search __P((fsid_t *));
188 static int parse_directory __P((const char *, size_t, struct grouplist *,
189     int, char *, struct exportlist **, struct statvfs *));
190 static int parse_host_netgroup __P((const char *, size_t, struct exportlist *,
191     struct grouplist *, char *, int *, struct grouplist **));
192 static struct exportlist *get_exp __P((void));
193 static void free_dir __P((struct dirlist *));
194 static void free_exp __P((struct exportlist *));
195 static void free_grp __P((struct grouplist *));
196 static void free_host __P((struct hostlist *));
197 void get_exportlist __P((int));
198 static int get_host __P((const char *, size_t, const char *,
199     struct grouplist *));
200 static struct hostlist *get_ht __P((void));
201 static void get_mountlist __P((void));
202 static int get_net __P((char *, struct netmsk *, int));
203 static void free_exp_grp __P((struct exportlist *, struct grouplist *));
204 static struct grouplist *get_grp __P((void));
205 static void hang_dirp __P((struct dirlist *, struct grouplist *,
206     struct exportlist *, int));
207 static void mntsrv __P((struct svc_req *, SVCXPRT *));
208 static void nextfield __P((char **, char **));
209 static void parsecred __P((char *, struct uucred *));
210 static int put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
211 static int scan_tree __P((struct dirlist *, struct sockaddr *));
212 static void send_umntall __P((int));
213 #if 0
214 static int umntall_each __P((caddr_t, struct sockaddr_in *));
215 #endif
216 static int xdr_dir __P((XDR *, char *));
217 static int xdr_explist __P((XDR *, caddr_t));
218 static int xdr_fhs __P((XDR *, caddr_t));
219 static int xdr_mlist __P((XDR *, caddr_t));
220 static int bitcmp __P((void *, void *, int));
221 static int netpartcmp __P((struct sockaddr *, struct sockaddr *, int));
222 static int sacmp __P((struct sockaddr *, struct sockaddr *));
223 static int allones __P((struct sockaddr_storage *, int));
224 static int countones __P((struct sockaddr *));
225 static void bind_resv_port __P((int, sa_family_t, in_port_t));
226 static void no_nfs(int);
227 static struct exportlist *exphead;
228 static struct mountlist *mlhead;
229 static struct grouplist *grphead;
230 static char    *exname;
231 static struct uucred def_anon = {
232 	1,
233 	(uid_t) -2,
234 	(gid_t) -2,
235 	0,
236 	{ 0 }
237 };
238 
239 static int      opt_flags;
240 static int	have_v6 = 1;
241 static const int ninumeric = NI_NUMERICHOST;
242 
243 /* Bits for above */
244 #define	OP_MAPROOT	0x001
245 #define	OP_MAPALL	0x002
246 #define	OP_KERB		0x004
247 #define	OP_MASK		0x008
248 #define	OP_NET		0x010
249 #define	OP_ALLDIRS	0x040
250 #define OP_NORESPORT	0x080
251 #define OP_NORESMNT	0x100
252 #define OP_MASKLEN	0x200
253 
254 static int      debug = 1;
255 #if 0
256 static void SYSLOG __P((int, const char *,...));
257 #endif
258 int main __P((int, char *[]));
259 
260 /*
261  * If this is non-zero, -noresvport and -noresvmnt are implied for
262  * each export.
263  */
264 static int noprivports;
265 
266 #define C2FD(_c_) ((int)(uintptr_t)(_c_))
267 static int
rumpread(void * cookie,char * buf,int count)268 rumpread(void *cookie, char *buf, int count)
269 {
270 
271 	return rump_sys_read(C2FD(cookie), buf, count);
272 }
273 
274 static int
rumpwrite(void * cookie,const char * buf,int count)275 rumpwrite(void *cookie, const char *buf, int count)
276 {
277 
278 	return rump_sys_write(C2FD(cookie), buf, count);
279 }
280 
281 static off_t
rumpseek(void * cookie,off_t off,int whence)282 rumpseek(void *cookie, off_t off, int whence)
283 {
284 
285 	return rump_sys_lseek(C2FD(cookie), off, whence);
286 }
287 
288 static int
rumpclose(void * cookie)289 rumpclose(void *cookie)
290 {
291 
292 	return rump_sys_close(C2FD(cookie));
293 }
294 
295 int __sflags(const char *, int *); /* XXX */
296 static FILE *
rumpfopen(const char * path,const char * opts)297 rumpfopen(const char *path, const char *opts)
298 {
299 	int fd, oflags;
300 
301 	__sflags(opts, &oflags);
302 	fd = rump_sys_open(path, oflags, 0777);
303 	if (fd == -1)
304 		return NULL;
305 
306 	return funopen((void *)(uintptr_t)fd,
307 	    rumpread, rumpwrite, rumpseek, rumpclose);
308 }
309 
310 /*
311  * Make sure mountd signal handler is executed from a thread context
312  * instead of the signal handler.  This avoids the signal handler
313  * ruining our kernel context.
314  */
315 static sem_t exportsem;
316 static void
signal_get_exportlist(int sig)317 signal_get_exportlist(int sig)
318 {
319 
320 	sem_post(&exportsem);
321 }
322 
323 static void *
exportlist_thread(void * arg)324 exportlist_thread(void *arg)
325 {
326 
327 	for (;;) {
328 		sem_wait(&exportsem);
329 		get_exportlist(0);
330 	}
331 
332 	return NULL;
333 }
334 
335 /*
336  * Mountd server for NFS mount protocol as described in:
337  * NFS: Network File System Protocol Specification, RFC1094, Appendix A
338  * The optional arguments are the exports file name
339  * default: _PATH_EXPORTS
340  * "-d" to enable debugging
341  * and "-n" to allow nonroot mount.
342  */
343 void *mountd_main(void *);
344 void *
mountd_main(void * arg)345 mountd_main(void *arg)
346 {
347 	SVCXPRT *udptransp, *tcptransp;
348 	struct netconfig *udpconf, *tcpconf;
349 	int udpsock, tcpsock;
350 	int xcreated = 0;
351 	int maxrec = RPC_MAXDATASIZE;
352 	in_port_t forcedport = 0;
353 	extern sem_t gensem;
354 	pthread_t ptdummy;
355 
356 	alloc_fdset();
357 
358 #if 0
359 	while ((c = getopt(argc, argv, "dNnrp:" ADDOPTS)) != -1)
360 		switch (c) {
361 #ifdef IPSEC
362 		case 'P':
363 			if (ipsecsetup_test(policy = optarg))
364 				errx(1, "Invalid ipsec policy `%s'", policy);
365 			break;
366 #endif
367 		case 'p':
368 			/* A forced port "0" will dynamically allocate a port */
369 			forcedport = atoi(optarg);
370 			break;
371 		case 'd':
372 			debug = 1;
373 			break;
374 		case 'N':
375 			noprivports = 1;
376 			break;
377 			/* Compatibility */
378 		case 'n':
379 		case 'r':
380 			break;
381 		default:
382 			fprintf(stderr, "usage: %s [-dNn]"
383 #ifdef IPSEC
384 			    " [-P policy]"
385 #endif
386 			    " [-p port] [exportsfile]\n", getprogname());
387 			exit(1);
388 		};
389 	argc -= optind;
390 	argv += optind;
391 #endif
392 
393 	sem_init(&exportsem, 0, 0);
394 	pthread_create(&ptdummy, NULL, exportlist_thread, NULL);
395 
396 	grphead = NULL;
397 	exphead = NULL;
398 	mlhead = NULL;
399 	exname = _PATH_EXPORTS;
400 	openlog("mountd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
401 	(void)signal(SIGSYS, no_nfs);
402 
403 	if (debug)
404 		(void)fprintf(stderr, "Getting export list.\n");
405 	get_exportlist(0);
406 	if (debug)
407 		(void)fprintf(stderr, "Getting mount list.\n");
408 	get_mountlist();
409 	if (debug)
410 		(void)fprintf(stderr, "Here we go.\n");
411 	if (debug == 0) {
412 		daemon(0, 0);
413 		(void)signal(SIGINT, SIG_IGN);
414 		(void)signal(SIGQUIT, SIG_IGN);
415 	}
416 	(void)signal(SIGHUP, signal_get_exportlist);
417 	(void)signal(SIGTERM, send_umntall);
418 	pidfile(NULL);
419 
420 	rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
421 	rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
422 
423 	udpsock  = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
424 	tcpsock  = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
425 
426 	udpconf  = getnetconfigent("udp");
427 	tcpconf  = getnetconfigent("tcp");
428 
429 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
430 
431 	if (udpsock != -1 && udpconf != NULL) {
432 		bind_resv_port(udpsock, AF_INET, forcedport);
433 #ifdef IPSEC
434 		if (policy)
435 			ipsecsetup(AF_INET, udpsock, policy);
436 #endif
437 		udptransp = svc_dg_create(udpsock, 0, 0);
438 		if (udptransp != NULL) {
439 			if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
440 				mntsrv, udpconf) ||
441 			    !svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
442 				mntsrv, udpconf)) {
443 				syslog(LOG_WARNING, "can't register UDP service");
444 			}
445 			else {
446 				xcreated++;
447 			}
448 		} else {
449 			syslog(LOG_WARNING, "can't create UDP service");
450 		}
451 
452 	}
453 
454 	if (tcpsock != -1 && tcpconf != NULL) {
455 		bind_resv_port(tcpsock, AF_INET, forcedport);
456 #ifdef IPSEC
457 		if (policy)
458 			ipsecsetup(AF_INET, tcpsock, policy);
459 #endif
460 		listen(tcpsock, SOMAXCONN);
461 		tcptransp = svc_vc_create(tcpsock, RPC_MAXDATASIZE,
462 		    RPC_MAXDATASIZE);
463 		if (tcptransp != NULL) {
464 			if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
465 				mntsrv, tcpconf) ||
466 			    !svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
467 				mntsrv, tcpconf))
468 				syslog(LOG_WARNING, "can't register TCP service");
469 			else
470 				xcreated++;
471 		} else
472 			syslog(LOG_WARNING, "can't create TCP service");
473 
474 	}
475 
476 	if (xcreated == 0) {
477 		syslog(LOG_ERR, "could not create any services");
478 		exit(1);
479 	}
480 
481 	sem_post(&gensem);
482 	svc_run();
483 	syslog(LOG_ERR, "Mountd died");
484 	exit(1);
485 }
486 
487 /*
488  * The mount rpc service
489  */
490 void
mntsrv(rqstp,transp)491 mntsrv(rqstp, transp)
492 	struct svc_req *rqstp;
493 	SVCXPRT *transp;
494 {
495 	struct exportlist *ep;
496 	struct dirlist *dp;
497 	struct fhreturn fhr;
498 	struct stat     stb;
499 	struct statvfs   fsb;
500 	char host[NI_MAXHOST], numerichost[NI_MAXHOST];
501 	int lookup_failed = 1;
502 	struct sockaddr *saddr;
503 	u_short         sport;
504 	char            rpcpath[RPCMNT_PATHLEN + 1], dpath[MAXPATHLEN];
505 	long            bad = EACCES;
506 	int             defset, hostset, ret;
507 	sigset_t        sighup_mask;
508 	struct sockaddr_in6 *sin6;
509 	struct sockaddr_in *sin;
510 	size_t fh_size;
511 	int error = 0;
512 
513 	(void)sigemptyset(&sighup_mask);
514 	(void)sigaddset(&sighup_mask, SIGHUP);
515 	saddr = svc_getrpccaller(transp)->buf;
516 	switch (saddr->sa_family) {
517 	case AF_INET6:
518 		sin6 = (struct sockaddr_in6 *)saddr;
519 		sport = ntohs(sin6->sin6_port);
520 		break;
521 	case AF_INET:
522 		sin = (struct sockaddr_in *)saddr;
523 		sport = ntohs(sin->sin_port);
524 		break;
525 	default:
526 		syslog(LOG_ERR, "request from unknown address family");
527 		return;
528 	}
529 	lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
530 	    NULL, 0, 0);
531 	if (getnameinfo(saddr, saddr->sa_len, numerichost,
532 	    sizeof numerichost, NULL, 0, ninumeric) != 0)
533 		strlcpy(numerichost, "?", sizeof(numerichost));
534 	ret = 0;
535 	switch (rqstp->rq_proc) {
536 	case NULLPROC:
537 		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
538 			syslog(LOG_ERR, "Can't send reply");
539 		return;
540 	case MOUNTPROC_MNT:
541 		if (debug)
542 			fprintf(stderr,
543 			    "got mount request from %s\n", numerichost);
544 		if (!svc_getargs(transp, xdr_dir, rpcpath)) {
545 			if (debug)
546 				fprintf(stderr, "-> garbage args\n");
547 			svcerr_decode(transp);
548 			return;
549 		}
550 		if (debug)
551 			fprintf(stderr,
552 			    "-> rpcpath: %s\n", rpcpath);
553 		/*
554 		 * Get the real pathname and make sure it is a file or
555 		 * directory that exists.
556 		 */
557 #if 0
558 		if (realpath(rpcpath, dpath) == 0 ||
559 #endif
560 		strcpy(dpath, rpcpath);
561 		if (rump_sys_stat(dpath, &stb) < 0 ||
562 		    (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) ||
563 		    rump_sys_statvfs1(dpath, &fsb, ST_WAIT) < 0) {
564 			(void)chdir("/"); /* Just in case realpath doesn't */
565 			if (debug)
566 				(void)fprintf(stderr, "-> stat failed on %s\n",
567 				    dpath);
568 			if (!svc_sendreply(transp, (xdrproc_t)xdr_long, (caddr_t) &bad))
569 				syslog(LOG_ERR, "Can't send reply");
570 			return;
571 		}
572 		if (debug)
573 			fprintf(stderr,
574 			    "-> dpath: %s\n", dpath);
575 		/* Check in the exports list */
576 		(void)sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
577 		ep = ex_search(&fsb.f_fsidx);
578 		hostset = defset = 0;
579 		if (ep && (chk_host(ep->ex_defdir, saddr, &defset,
580 		   &hostset) || ((dp = dirp_search(ep->ex_dirl, dpath)) &&
581 		   chk_host(dp, saddr, &defset, &hostset)) ||
582 		   (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
583 		   scan_tree(ep->ex_dirl, saddr) == 0))) {
584 			if ((hostset & DP_HOSTSET) == 0) {
585 				hostset = defset;
586 			}
587 			if (sport >= IPPORT_RESERVED &&
588 			    !(hostset & DP_NORESMNT)) {
589 				syslog(LOG_NOTICE,
590 				    "Refused mount RPC from host %s port %d",
591 				    numerichost, sport);
592 				svcerr_weakauth(transp);
593 				goto out;
594 			}
595 			fhr.fhr_flag = hostset;
596 			fhr.fhr_vers = rqstp->rq_vers;
597 			/* Get the file handle */
598 			memset(&fhr.fhr_fh, 0, sizeof(fhr.fhr_fh)); /* for v2 */
599 			fh_size = sizeof(fhr.fhr_fh);
600 			error = 0;
601 			if (rump_sys_getfh(dpath, &fhr.fhr_fh, &fh_size) < 0) {
602 				bad = error;
603 				//syslog(LOG_ERR, "Can't get fh for %s %d %d", dpath, error, fh_size);
604 				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
605 				    (char *)&bad))
606 					syslog(LOG_ERR, "Can't send reply");
607 				goto out;
608 			}
609 			if ((fhr.fhr_vers == 1 && fh_size > NFSX_V2FH) ||
610 			    fh_size > NFSX_V3FHMAX) {
611 				bad = EINVAL; /* XXX */
612 				if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
613 				    (char *)&bad))
614 					syslog(LOG_ERR, "Can't send reply");
615 				goto out;
616 			}
617 			fhr.fhr_fhsize = fh_size;
618 			if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, (char *) &fhr))
619 				syslog(LOG_ERR, "Can't send reply");
620 			if (!lookup_failed)
621 				add_mlist(host, dpath, hostset);
622 			else
623 				add_mlist(numerichost, dpath, hostset);
624 			if (debug)
625 				(void)fprintf(stderr, "Mount successful.\n");
626 		} else {
627 			if (!svc_sendreply(transp, (xdrproc_t)xdr_long, (caddr_t) &bad))
628 				syslog(LOG_ERR, "Can't send reply");
629 		}
630 out:
631 		(void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
632 		return;
633 	case MOUNTPROC_DUMP:
634 		if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, NULL))
635 			syslog(LOG_ERR, "Can't send reply");
636 		return;
637 	case MOUNTPROC_UMNT:
638 		if (!svc_getargs(transp, xdr_dir, dpath)) {
639 			svcerr_decode(transp);
640 			return;
641 		}
642 		if (!lookup_failed)
643 			ret = del_mlist(host, dpath, saddr);
644 		ret |= del_mlist(numerichost, dpath, saddr);
645 		if (ret) {
646 			svcerr_weakauth(transp);
647 			return;
648 		}
649 		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
650 			syslog(LOG_ERR, "Can't send reply");
651 		return;
652 	case MOUNTPROC_UMNTALL:
653 		if (!lookup_failed)
654 			ret = del_mlist(host, NULL, saddr);
655 		ret |= del_mlist(numerichost, NULL, saddr);
656 		if (ret) {
657 			svcerr_weakauth(transp);
658 			return;
659 		}
660 		if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
661 			syslog(LOG_ERR, "Can't send reply");
662 		return;
663 	case MOUNTPROC_EXPORT:
664 	case MOUNTPROC_EXPORTALL:
665 		if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, NULL))
666 			syslog(LOG_ERR, "Can't send reply");
667 		return;
668 
669 
670 	default:
671 		svcerr_noproc(transp);
672 		return;
673 	}
674 }
675 
676 /*
677  * Xdr conversion for a dpath string
678  */
679 static int
xdr_dir(xdrsp,dirp)680 xdr_dir(xdrsp, dirp)
681 	XDR *xdrsp;
682 	char *dirp;
683 {
684 
685 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
686 }
687 
688 /*
689  * Xdr routine to generate file handle reply
690  */
691 static int
xdr_fhs(xdrsp,cp)692 xdr_fhs(xdrsp, cp)
693 	XDR *xdrsp;
694 	caddr_t cp;
695 {
696 	struct fhreturn *fhrp = (struct fhreturn *) cp;
697 	long ok = 0, len, auth;
698 
699 	if (!xdr_long(xdrsp, &ok))
700 		return (0);
701 	switch (fhrp->fhr_vers) {
702 	case 1:
703 		return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
704 	case 3:
705 		len = fhrp->fhr_fhsize;
706 		if (!xdr_long(xdrsp, &len))
707 			return (0);
708 		if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
709 			return (0);
710 		if (fhrp->fhr_flag & DP_KERB)
711 			auth = RPCAUTH_KERB4;
712 		else
713 			auth = RPCAUTH_UNIX;
714 		len = 1;
715 		if (!xdr_long(xdrsp, &len))
716 			return (0);
717 		return (xdr_long(xdrsp, &auth));
718 	};
719 	return (0);
720 }
721 
722 int
xdr_mlist(xdrsp,cp)723 xdr_mlist(xdrsp, cp)
724 	XDR *xdrsp;
725 	caddr_t cp;
726 {
727 	struct mountlist *mlp;
728 	int trueval = 1;
729 	int falseval = 0;
730 	char *strp;
731 
732 	mlp = mlhead;
733 	while (mlp) {
734 		if (!xdr_bool(xdrsp, &trueval))
735 			return (0);
736 		strp = &mlp->ml_host[0];
737 		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
738 			return (0);
739 		strp = &mlp->ml_dirp[0];
740 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
741 			return (0);
742 		mlp = mlp->ml_next;
743 	}
744 	if (!xdr_bool(xdrsp, &falseval))
745 		return (0);
746 	return (1);
747 }
748 
749 /*
750  * Xdr conversion for export list
751  */
752 int
xdr_explist(xdrsp,cp)753 xdr_explist(xdrsp, cp)
754 	XDR *xdrsp;
755 	caddr_t cp;
756 {
757 	struct exportlist *ep;
758 	int falseval = 0;
759 	int putdef;
760 	sigset_t sighup_mask;
761 
762 	(void)sigemptyset(&sighup_mask);
763 	(void)sigaddset(&sighup_mask, SIGHUP);
764 	(void)sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
765 	ep = exphead;
766 	while (ep) {
767 		putdef = 0;
768 		if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
769 			goto errout;
770 		if (ep->ex_defdir && putdef == 0 &&
771 		    put_exlist(ep->ex_defdir, xdrsp, NULL, &putdef))
772 			goto errout;
773 		ep = ep->ex_next;
774 	}
775 	(void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
776 	if (!xdr_bool(xdrsp, &falseval))
777 		return (0);
778 	return (1);
779 errout:
780 	(void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
781 	return (0);
782 }
783 
784 /*
785  * Called from xdr_explist() to traverse the tree and export the
786  * directory paths.  Assumes SIGHUP has already been masked.
787  */
788 int
put_exlist(dp,xdrsp,adp,putdefp)789 put_exlist(dp, xdrsp, adp, putdefp)
790 	struct dirlist *dp;
791 	XDR *xdrsp;
792 	struct dirlist *adp;
793 	int *putdefp;
794 {
795 	struct grouplist *grp;
796 	struct hostlist *hp;
797 	int trueval = 1;
798 	int falseval = 0;
799 	int gotalldir = 0;
800 	char *strp;
801 
802 	if (dp) {
803 		if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
804 			return (1);
805 		if (!xdr_bool(xdrsp, &trueval))
806 			return (1);
807 		strp = dp->dp_dirp;
808 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
809 			return (1);
810 		if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
811 			gotalldir = 1;
812 			*putdefp = 1;
813 		}
814 		if ((dp->dp_flag & DP_DEFSET) == 0 &&
815 		    (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
816 			hp = dp->dp_hosts;
817 			while (hp) {
818 				grp = hp->ht_grp;
819 				if (grp->gr_type == GT_HOST) {
820 					if (!xdr_bool(xdrsp, &trueval))
821 						return (1);
822 					strp =
823 					  grp->gr_ptr.gt_addrinfo->ai_canonname;
824 					if (!xdr_string(xdrsp, &strp,
825 							RPCMNT_NAMELEN))
826 						return (1);
827 				} else if (grp->gr_type == GT_NET) {
828 					if (!xdr_bool(xdrsp, &trueval))
829 						return (1);
830 					strp = grp->gr_ptr.gt_net.nt_name;
831 					if (!xdr_string(xdrsp, &strp,
832 							RPCMNT_NAMELEN))
833 						return (1);
834 				}
835 				hp = hp->ht_next;
836 				if (gotalldir && hp == NULL) {
837 					hp = adp->dp_hosts;
838 					gotalldir = 0;
839 				}
840 			}
841 		}
842 		if (!xdr_bool(xdrsp, &falseval))
843 			return (1);
844 		if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
845 			return (1);
846 	}
847 	return (0);
848 }
849 
850 static int
parse_host_netgroup(line,lineno,ep,tgrp,cp,has_host,grp)851 parse_host_netgroup(line, lineno, ep, tgrp, cp, has_host, grp)
852 	const char *line;
853 	size_t lineno;
854 	struct exportlist *ep;
855 	struct grouplist *tgrp;
856 	char *cp;
857 	int *has_host;
858 	struct grouplist **grp;
859 {
860 	const char *hst, *usr, *dom;
861 	int netgrp;
862 
863 	if (ep == NULL) {
864 		syslog(LOG_ERR, "\"%s\", line %ld: No current export",
865 		    line, (unsigned long)lineno);
866 		return 0;
867 	}
868 	setnetgrent(cp);
869 	netgrp = getnetgrent(&hst, &usr, &dom);
870 	do {
871 		if (*has_host) {
872 			(*grp)->gr_next = get_grp();
873 			*grp = (*grp)->gr_next;
874 		}
875 		if (netgrp) {
876 			if (hst == NULL) {
877 				syslog(LOG_ERR,
878 				    "\"%s\", line %ld: No host in netgroup %s",
879 				    line, (unsigned long)lineno, cp);
880 				goto bad;
881 			}
882 			if (get_host(line, lineno, hst, *grp))
883 				goto bad;
884 		} else if (get_host(line, lineno, cp, *grp))
885 			goto bad;
886 		*has_host = TRUE;
887 	} while (netgrp && getnetgrent(&hst, &usr, &dom));
888 
889 	endnetgrent();
890 	return 1;
891 bad:
892 	endnetgrent();
893 	return 0;
894 
895 }
896 
897 static int
parse_directory(line,lineno,tgrp,got_nondir,cp,ep,fsp)898 parse_directory(line, lineno, tgrp, got_nondir, cp, ep, fsp)
899 	const char *line;
900 	size_t lineno;
901 	struct grouplist *tgrp;
902 	int got_nondir;
903 	char *cp;
904 	struct exportlist **ep;
905 	struct statvfs *fsp;
906 {
907 	if (!check_dirpath(line, lineno, cp))
908 		return 0;
909 
910 	if (rump_sys_statvfs1(cp, fsp, ST_WAIT) == -1) {
911 		syslog(LOG_ERR, "\"%s\", line %ld: statvfs for `%s' failed (%s)",
912 		    line, (unsigned long)lineno, cp, strerror(errno));
913 		return 0;
914 	}
915 
916 	if (got_nondir) {
917 		syslog(LOG_ERR,
918 		    "\"%s\", line %ld: Directories must precede files",
919 		    line, (unsigned long)lineno);
920 		return 0;
921 	}
922 	if (*ep) {
923 		if ((*ep)->ex_fs.__fsid_val[0] != fsp->f_fsidx.__fsid_val[0] ||
924 		    (*ep)->ex_fs.__fsid_val[1] != fsp->f_fsidx.__fsid_val[1]) {
925 			syslog(LOG_ERR,
926 			    "\"%s\", line %ld: filesystem ids disagree",
927 			    line, (unsigned long)lineno);
928 			return 0;
929 		}
930 	} else {
931 		/*
932 		 * See if this directory is already
933 		 * in the list.
934 		 */
935 		*ep = ex_search(&fsp->f_fsidx);
936 		if (*ep == NULL) {
937 			*ep = get_exp();
938 			(*ep)->ex_fs = fsp->f_fsidx;
939 			(*ep)->ex_fsdir = estrdup(fsp->f_mntonname);
940 			if (debug)
941 				(void)fprintf(stderr,
942 				    "Making new ep fs=0x%x,0x%x\n",
943 				    fsp->f_fsidx.__fsid_val[0], fsp->f_fsidx.__fsid_val[1]);
944 		} else {
945 			if (debug)
946 				(void)fprintf(stderr,
947 				    "Found ep fs=0x%x,0x%x\n",
948 				    fsp->f_fsidx.__fsid_val[0], fsp->f_fsidx.__fsid_val[1]);
949 		}
950 	}
951 
952 	return 1;
953 }
954 
955 
956 /*
957  * Get the export list
958  */
959 /* ARGSUSED */
960 void
get_exportlist(n)961 get_exportlist(n)
962 	int n;
963 {
964 	struct exportlist *ep, *ep2;
965 	struct grouplist *grp, *tgrp;
966 	struct exportlist **epp;
967 	struct dirlist *dirhead;
968 	struct statvfs fsb, *fsp;
969 	struct addrinfo *ai;
970 	struct uucred anon;
971 	char *cp, *endcp, *dirp, savedc;
972 	int has_host, exflags, got_nondir, dirplen, num, i;
973 	FILE *exp_file;
974 	char *line;
975 	size_t lineno = 0, len;
976 
977 
978 	/*
979 	 * First, get rid of the old list
980 	 */
981 	ep = exphead;
982 	while (ep) {
983 		ep2 = ep;
984 		ep = ep->ex_next;
985 		free_exp(ep2);
986 	}
987 	exphead = NULL;
988 
989 	dirp = NULL;
990 	dirplen = 0;
991 	grp = grphead;
992 	while (grp) {
993 		tgrp = grp;
994 		grp = grp->gr_next;
995 		free_grp(tgrp);
996 	}
997 	grphead = NULL;
998 
999 	/*
1000 	 * And delete exports that are in the kernel for all local
1001 	 * file systems.
1002 	 */
1003 	num = getmntinfo(&fsp, MNT_NOWAIT);
1004 	for (i = 0; i < num; i++) {
1005 		struct mountd_exports_list mel;
1006 
1007 		/* Delete all entries from the export list. */
1008 		mel.mel_path = fsp->f_mntonname;
1009 		mel.mel_nexports = 0;
1010 		if (rump_sys_nfssvc(NFSSVC_SETEXPORTSLIST, &mel) == -1 &&
1011 		    errno != EOPNOTSUPP)
1012 			syslog(LOG_ERR, "Can't delete exports for %s (%s)",
1013 			    fsp->f_mntonname, strerror(errno));
1014 
1015 		fsp++;
1016 	}
1017 
1018 	/*
1019 	 * Read in the exports file and build the list, calling
1020 	 * mount() as we go along to push the export rules into the kernel.
1021 	 */
1022 	exname = _PATH_EXPORTS;
1023 	if ((exp_file = rumpfopen(exname, "r")) == NULL) {
1024 		/*
1025 		 * Don't exit here; we can still reload the config
1026 		 * after a SIGHUP.
1027 		 */
1028 		if (debug)
1029 			(void)fprintf(stderr, "Can't open %s: %s\n", exname,
1030 			    strerror(errno));
1031 		return;
1032 	}
1033 	dirhead = NULL;
1034 	while ((line = fparseln(exp_file, &len, &lineno, NULL, 0)) != NULL) {
1035 		if (debug)
1036 			(void)fprintf(stderr, "Got line %s\n", line);
1037 		cp = line;
1038 		nextfield(&cp, &endcp);
1039 		if (cp == endcp)
1040 			goto nextline;	/* skip empty line */
1041 		/*
1042 		 * Set defaults.
1043 		 */
1044 		has_host = FALSE;
1045 		anon = def_anon;
1046 		exflags = MNT_EXPORTED;
1047 		got_nondir = 0;
1048 		opt_flags = 0;
1049 		ep = NULL;
1050 
1051 		if (noprivports) {
1052 			opt_flags |= OP_NORESMNT | OP_NORESPORT;
1053 			exflags |= MNT_EXNORESPORT;
1054 		}
1055 
1056 		/*
1057 		 * Create new exports list entry
1058 		 */
1059 		len = endcp - cp;
1060 		tgrp = grp = get_grp();
1061 		while (len > 0) {
1062 			if (len > RPCMNT_NAMELEN) {
1063 				*endcp = '\0';
1064 				syslog(LOG_ERR,
1065 				    "\"%s\", line %ld: name `%s' is too long",
1066 				    line, (unsigned long)lineno, cp);
1067 				goto badline;
1068 			}
1069 			switch (*cp) {
1070 			case '-':
1071 				/*
1072 				 * Option
1073 				 */
1074 				if (ep == NULL) {
1075 					syslog(LOG_ERR,
1076 				"\"%s\", line %ld: No current export list",
1077 					    line, (unsigned long)lineno);
1078 					goto badline;
1079 				}
1080 				if (debug)
1081 					(void)fprintf(stderr, "doing opt %s\n",
1082 					    cp);
1083 				got_nondir = 1;
1084 				if (do_opt(line, lineno, &cp, &endcp, ep, grp,
1085 				    &has_host, &exflags, &anon))
1086 					goto badline;
1087 				break;
1088 
1089 			case '/':
1090 				/*
1091 				 * Directory
1092 				 */
1093 				savedc = *endcp;
1094 				*endcp = '\0';
1095 
1096 				if (!parse_directory(line, lineno, tgrp,
1097 				    got_nondir, cp, &ep, &fsb))
1098 					goto badline;
1099 				/*
1100 				 * Add dirpath to export mount point.
1101 				 */
1102 				dirp = add_expdir(&dirhead, cp, len);
1103 				dirplen = len;
1104 
1105 				*endcp = savedc;
1106 				break;
1107 
1108 			default:
1109 				/*
1110 				 * Host or netgroup.
1111 				 */
1112 				savedc = *endcp;
1113 				*endcp = '\0';
1114 
1115 				if (!parse_host_netgroup(line, lineno, ep,
1116 				    tgrp, cp, &has_host, &grp))
1117 					goto badline;
1118 
1119 				got_nondir = 1;
1120 
1121 				*endcp = savedc;
1122 				break;
1123 			}
1124 
1125 			cp = endcp;
1126 			nextfield(&cp, &endcp);
1127 			len = endcp - cp;
1128 		}
1129 		if (check_options(line, lineno, dirhead))
1130 			goto badline;
1131 
1132 		if (!has_host) {
1133 			grp->gr_type = GT_HOST;
1134 			if (debug)
1135 				(void)fprintf(stderr,
1136 				    "Adding a default entry\n");
1137 			/* add a default group and make the grp list NULL */
1138 			ai = emalloc(sizeof(struct addrinfo));
1139 			ai->ai_flags = 0;
1140 			ai->ai_family = AF_INET;	/* XXXX */
1141 			ai->ai_socktype = SOCK_DGRAM;
1142 			/* setting the length to 0 will match anything */
1143 			ai->ai_addrlen = 0;
1144 			ai->ai_flags = AI_CANONNAME;
1145 			ai->ai_canonname = estrdup("Default");
1146 			ai->ai_addr = NULL;
1147 			ai->ai_next = NULL;
1148 			grp->gr_ptr.gt_addrinfo = ai;
1149 
1150 		} else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1151 			/*
1152 			 * Don't allow a network export coincide with a list of
1153 			 * host(s) on the same line.
1154 			 */
1155 			syslog(LOG_ERR,
1156 			    "\"%s\", line %ld: Mixed exporting of networks and hosts is disallowed",
1157 			    line, (unsigned long)lineno);
1158 			goto badline;
1159 		}
1160 		/*
1161 		 * Loop through hosts, pushing the exports into the kernel.
1162 		 * After loop, tgrp points to the start of the list and
1163 		 * grp points to the last entry in the list.
1164 		 */
1165 		grp = tgrp;
1166 		do {
1167 			if (do_nfssvc(line, lineno, ep, grp, exflags, &anon,
1168 			    dirp, dirplen, &fsb))
1169 				goto badline;
1170 		} while (grp->gr_next && (grp = grp->gr_next));
1171 
1172 		/*
1173 		 * Success. Update the data structures.
1174 		 */
1175 		if (has_host) {
1176 			hang_dirp(dirhead, tgrp, ep, opt_flags);
1177 			grp->gr_next = grphead;
1178 			grphead = tgrp;
1179 		} else {
1180 			hang_dirp(dirhead, NULL, ep, opt_flags);
1181 			free_grp(tgrp);
1182 		}
1183 		tgrp = NULL;
1184 		dirhead = NULL;
1185 		if ((ep->ex_flag & EX_LINKED) == 0) {
1186 			ep2 = exphead;
1187 			epp = &exphead;
1188 
1189 			/*
1190 			 * Insert in the list in alphabetical order.
1191 			 */
1192 			while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1193 				epp = &ep2->ex_next;
1194 				ep2 = ep2->ex_next;
1195 			}
1196 			if (ep2)
1197 				ep->ex_next = ep2;
1198 			*epp = ep;
1199 			ep->ex_flag |= EX_LINKED;
1200 		}
1201 		goto nextline;
1202 badline:
1203 		free_exp_grp(ep, grp);
1204 nextline:
1205 		if (dirhead) {
1206 			free_dir(dirhead);
1207 			dirhead = NULL;
1208 		}
1209 		free(line);
1210 	}
1211 	(void)fclose(exp_file);
1212 }
1213 
1214 /*
1215  * Allocate an export list element
1216  */
1217 static struct exportlist *
get_exp()1218 get_exp()
1219 {
1220 	struct exportlist *ep;
1221 
1222 	ep = emalloc(sizeof(struct exportlist));
1223 	(void)memset(ep, 0, sizeof(struct exportlist));
1224 	return (ep);
1225 }
1226 
1227 /*
1228  * Allocate a group list element
1229  */
1230 static struct grouplist *
get_grp()1231 get_grp()
1232 {
1233 	struct grouplist *gp;
1234 
1235 	gp = emalloc(sizeof(struct grouplist));
1236 	(void)memset(gp, 0, sizeof(struct grouplist));
1237 	return (gp);
1238 }
1239 
1240 /*
1241  * Clean up upon an error in get_exportlist().
1242  */
1243 static void
free_exp_grp(ep,grp)1244 free_exp_grp(ep, grp)
1245 	struct exportlist *ep;
1246 	struct grouplist *grp;
1247 {
1248 	struct grouplist *tgrp;
1249 
1250 	if (ep && (ep->ex_flag & EX_LINKED) == 0)
1251 		free_exp(ep);
1252 	while (grp) {
1253 		tgrp = grp;
1254 		grp = grp->gr_next;
1255 		free_grp(tgrp);
1256 	}
1257 }
1258 
1259 /*
1260  * Search the export list for a matching fs.
1261  */
1262 static struct exportlist *
ex_search(fsid)1263 ex_search(fsid)
1264 	fsid_t *fsid;
1265 {
1266 	struct exportlist *ep;
1267 
1268 	ep = exphead;
1269 	return ep;
1270 	while (ep) {
1271 		if (ep->ex_fs.__fsid_val[0] == fsid->__fsid_val[0] &&
1272 		    ep->ex_fs.__fsid_val[1] == fsid->__fsid_val[1])
1273 			return (ep);
1274 		ep = ep->ex_next;
1275 	}
1276 	return (ep);
1277 }
1278 
1279 /*
1280  * Add a directory path to the list.
1281  */
1282 static char *
add_expdir(dpp,cp,len)1283 add_expdir(dpp, cp, len)
1284 	struct dirlist **dpp;
1285 	char *cp;
1286 	int len;
1287 {
1288 	struct dirlist *dp;
1289 
1290 	dp = emalloc(sizeof(struct dirlist) + len);
1291 	dp->dp_left = *dpp;
1292 	dp->dp_right = NULL;
1293 	dp->dp_flag = 0;
1294 	dp->dp_hosts = NULL;
1295 	(void)strcpy(dp->dp_dirp, cp);
1296 	*dpp = dp;
1297 	return (dp->dp_dirp);
1298 }
1299 
1300 /*
1301  * Hang the dir list element off the dirpath binary tree as required
1302  * and update the entry for host.
1303  */
1304 void
hang_dirp(dp,grp,ep,flags)1305 hang_dirp(dp, grp, ep, flags)
1306 	struct dirlist *dp;
1307 	struct grouplist *grp;
1308 	struct exportlist *ep;
1309 	int flags;
1310 {
1311 	struct hostlist *hp;
1312 	struct dirlist *dp2;
1313 
1314 	if (flags & OP_ALLDIRS) {
1315 		if (ep->ex_defdir)
1316 			free(dp);
1317 		else
1318 			ep->ex_defdir = dp;
1319 		if (grp == NULL) {
1320 			ep->ex_defdir->dp_flag |= DP_DEFSET;
1321 			if (flags & OP_KERB)
1322 				ep->ex_defdir->dp_flag |= DP_KERB;
1323 			if (flags & OP_NORESMNT)
1324 				ep->ex_defdir->dp_flag |= DP_NORESMNT;
1325 		} else
1326 			while (grp) {
1327 				hp = get_ht();
1328 				if (flags & OP_KERB)
1329 					hp->ht_flag |= DP_KERB;
1330 				if (flags & OP_NORESMNT)
1331 					hp->ht_flag |= DP_NORESMNT;
1332 				hp->ht_grp = grp;
1333 				hp->ht_next = ep->ex_defdir->dp_hosts;
1334 				ep->ex_defdir->dp_hosts = hp;
1335 				grp = grp->gr_next;
1336 			}
1337 	} else {
1338 
1339 		/*
1340 		 * Loop through the directories adding them to the tree.
1341 		 */
1342 		while (dp) {
1343 			dp2 = dp->dp_left;
1344 			add_dlist(&ep->ex_dirl, dp, grp, flags);
1345 			dp = dp2;
1346 		}
1347 	}
1348 }
1349 
1350 /*
1351  * Traverse the binary tree either updating a node that is already there
1352  * for the new directory or adding the new node.
1353  */
1354 static void
add_dlist(dpp,newdp,grp,flags)1355 add_dlist(dpp, newdp, grp, flags)
1356 	struct dirlist **dpp;
1357 	struct dirlist *newdp;
1358 	struct grouplist *grp;
1359 	int flags;
1360 {
1361 	struct dirlist *dp;
1362 	struct hostlist *hp;
1363 	int cmp;
1364 
1365 	dp = *dpp;
1366 	if (dp) {
1367 		cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1368 		if (cmp > 0) {
1369 			add_dlist(&dp->dp_left, newdp, grp, flags);
1370 			return;
1371 		} else if (cmp < 0) {
1372 			add_dlist(&dp->dp_right, newdp, grp, flags);
1373 			return;
1374 		} else
1375 			free(newdp);
1376 	} else {
1377 		dp = newdp;
1378 		dp->dp_left = NULL;
1379 		*dpp = dp;
1380 	}
1381 	if (grp) {
1382 
1383 		/*
1384 		 * Hang all of the host(s) off of the directory point.
1385 		 */
1386 		do {
1387 			hp = get_ht();
1388 			if (flags & OP_KERB)
1389 				hp->ht_flag |= DP_KERB;
1390 			if (flags & OP_NORESMNT)
1391 				hp->ht_flag |= DP_NORESMNT;
1392 			hp->ht_grp = grp;
1393 			hp->ht_next = dp->dp_hosts;
1394 			dp->dp_hosts = hp;
1395 			grp = grp->gr_next;
1396 		} while (grp);
1397 	} else {
1398 		dp->dp_flag |= DP_DEFSET;
1399 		if (flags & OP_KERB)
1400 			dp->dp_flag |= DP_KERB;
1401 		if (flags & OP_NORESMNT)
1402 			dp->dp_flag |= DP_NORESMNT;
1403 	}
1404 }
1405 
1406 /*
1407  * Search for a dirpath on the export point.
1408  */
1409 static struct dirlist *
dirp_search(dp,dirp)1410 dirp_search(dp, dirp)
1411 	struct dirlist *dp;
1412 	char *dirp;
1413 {
1414 	int cmp;
1415 
1416 	if (dp) {
1417 		cmp = strcmp(dp->dp_dirp, dirp);
1418 		if (cmp > 0)
1419 			return (dirp_search(dp->dp_left, dirp));
1420 		else if (cmp < 0)
1421 			return (dirp_search(dp->dp_right, dirp));
1422 		else
1423 			return (dp);
1424 	}
1425 	return (dp);
1426 }
1427 
1428 /*
1429  * Some helper functions for netmasks. They all assume masks in network
1430  * order (big endian).
1431  */
1432 static int
bitcmp(void * dst,void * src,int bitlen)1433 bitcmp(void *dst, void *src, int bitlen)
1434 {
1435 	int i;
1436 	u_int8_t *p1 = dst, *p2 = src;
1437 	u_int8_t bitmask;
1438 	int bytelen, bitsleft;
1439 
1440 	bytelen = bitlen / 8;
1441 	bitsleft = bitlen % 8;
1442 
1443 	if (debug) {
1444 		printf("comparing:\n");
1445 		for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
1446 			printf("%02x", p1[i]);
1447 		printf("\n");
1448 		for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
1449 			printf("%02x", p2[i]);
1450 		printf("\n");
1451 	}
1452 
1453 	for (i = 0; i < bytelen; i++) {
1454 		if (*p1 != *p2)
1455 			return 1;
1456 		p1++;
1457 		p2++;
1458 	}
1459 
1460 	for (i = 0; i < bitsleft; i++) {
1461 		bitmask = 1 << (7 - i);
1462 		if ((*p1 & bitmask) != (*p2 & bitmask))
1463 			return 1;
1464 	}
1465 
1466 	return 0;
1467 }
1468 
1469 static int
netpartcmp(struct sockaddr * s1,struct sockaddr * s2,int bitlen)1470 netpartcmp(struct sockaddr *s1, struct sockaddr *s2, int bitlen)
1471 {
1472 	void *src, *dst;
1473 
1474 	if (s1->sa_family != s2->sa_family)
1475 		return 1;
1476 
1477 	switch (s1->sa_family) {
1478 	case AF_INET:
1479 		src = &((struct sockaddr_in *)s1)->sin_addr;
1480 		dst = &((struct sockaddr_in *)s2)->sin_addr;
1481 		if (bitlen > sizeof(((struct sockaddr_in *)s1)->sin_addr) * 8)
1482 			return 1;
1483 		break;
1484 	case AF_INET6:
1485 		src = &((struct sockaddr_in6 *)s1)->sin6_addr;
1486 		dst = &((struct sockaddr_in6 *)s2)->sin6_addr;
1487 		if (((struct sockaddr_in6 *)s1)->sin6_scope_id !=
1488 		    ((struct sockaddr_in6 *)s2)->sin6_scope_id)
1489 			return 1;
1490 		if (bitlen > sizeof(((struct sockaddr_in6 *)s1)->sin6_addr) * 8)
1491 			return 1;
1492 		break;
1493 	default:
1494 		return 1;
1495 	}
1496 
1497 	return bitcmp(src, dst, bitlen);
1498 }
1499 
1500 static int
allones(struct sockaddr_storage * ssp,int bitlen)1501 allones(struct sockaddr_storage *ssp, int bitlen)
1502 {
1503 	u_int8_t *p;
1504 	int bytelen, bitsleft, i;
1505 	int zerolen;
1506 
1507 	switch (ssp->ss_family) {
1508 	case AF_INET:
1509 		p = (u_int8_t *)&((struct sockaddr_in *)ssp)->sin_addr;
1510 		zerolen = sizeof (((struct sockaddr_in *)ssp)->sin_addr);
1511 		break;
1512 	case AF_INET6:
1513 		p = (u_int8_t *)&((struct sockaddr_in6 *)ssp)->sin6_addr;
1514 		zerolen = sizeof (((struct sockaddr_in6 *)ssp)->sin6_addr);
1515 		break;
1516 	default:
1517 		return -1;
1518 	}
1519 
1520 	memset(p, 0, zerolen);
1521 
1522 	bytelen = bitlen / 8;
1523 	bitsleft = bitlen % 8;
1524 
1525 	if (bytelen > zerolen)
1526 		return -1;
1527 
1528 	for (i = 0; i < bytelen; i++)
1529 		*p++ = 0xff;
1530 
1531 	for (i = 0; i < bitsleft; i++)
1532 		*p |= 1 << (7 - i);
1533 
1534 	return 0;
1535 }
1536 
1537 static int
countones(struct sockaddr * sa)1538 countones(struct sockaddr *sa)
1539 {
1540 	void *mask;
1541 	int i, bits = 0, bytelen;
1542 	u_int8_t *p;
1543 
1544 	switch (sa->sa_family) {
1545 	case AF_INET:
1546 		mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr;
1547 		bytelen = 4;
1548 		break;
1549 	case AF_INET6:
1550 		mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr;
1551 		bytelen = 16;
1552 		break;
1553 	default:
1554 		return 0;
1555 	}
1556 
1557 	p = mask;
1558 
1559 	for (i = 0; i < bytelen; i++, p++) {
1560 		if (*p != 0xff) {
1561 			for (bits = 0; bits < 8; bits++) {
1562 				if (!(*p & (1 << (7 - bits))))
1563 					break;
1564 			}
1565 			break;
1566 		}
1567 	}
1568 
1569 	return (i * 8 + bits);
1570 }
1571 
1572 static int
sacmp(struct sockaddr * sa1,struct sockaddr * sa2)1573 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
1574 {
1575 	void *p1, *p2;
1576 	int len;
1577 
1578 	if (sa1->sa_family != sa2->sa_family)
1579 		return 1;
1580 
1581 	switch (sa1->sa_family) {
1582 	case AF_INET:
1583 		p1 = &((struct sockaddr_in *)sa1)->sin_addr;
1584 		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
1585 		len = 4;
1586 		break;
1587 	case AF_INET6:
1588 		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
1589 		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
1590 		len = 16;
1591 		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
1592 		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
1593 			return 1;
1594 		break;
1595 	default:
1596 		return 1;
1597 	}
1598 
1599 	return memcmp(p1, p2, len);
1600 }
1601 
1602 /*
1603  * Scan for a host match in a directory tree.
1604  */
1605 static int
chk_host(dp,saddr,defsetp,hostsetp)1606 chk_host(dp, saddr, defsetp, hostsetp)
1607 	struct dirlist *dp;
1608 	struct sockaddr *saddr;
1609 	int *defsetp;
1610 	int *hostsetp;
1611 {
1612 	struct hostlist *hp;
1613 	struct grouplist *grp;
1614 	struct addrinfo *ai;
1615 
1616 	if (dp) {
1617 		if (dp->dp_flag & DP_DEFSET)
1618 			*defsetp = dp->dp_flag;
1619 		hp = dp->dp_hosts;
1620 		while (hp) {
1621 			grp = hp->ht_grp;
1622 			switch (grp->gr_type) {
1623 			case GT_HOST:
1624 				ai = grp->gr_ptr.gt_addrinfo;
1625 				for (; ai; ai = ai->ai_next) {
1626 					if (!sacmp(ai->ai_addr, saddr)) {
1627 						*hostsetp =
1628 						    (hp->ht_flag | DP_HOSTSET);
1629 						return (1);
1630 					}
1631 				}
1632 				break;
1633 			case GT_NET:
1634 				if (!netpartcmp(saddr,
1635 				    (struct sockaddr *)
1636 					&grp->gr_ptr.gt_net.nt_net,
1637 				    grp->gr_ptr.gt_net.nt_len)) {
1638 					*hostsetp = (hp->ht_flag | DP_HOSTSET);
1639 					return (1);
1640 				}
1641 				break;
1642 			};
1643 			hp = hp->ht_next;
1644 		}
1645 	}
1646 	return (0);
1647 }
1648 
1649 /*
1650  * Scan tree for a host that matches the address.
1651  */
1652 static int
scan_tree(dp,saddr)1653 scan_tree(dp, saddr)
1654 	struct dirlist *dp;
1655 	struct sockaddr *saddr;
1656 {
1657 	int defset, hostset;
1658 
1659 	if (dp) {
1660 		if (scan_tree(dp->dp_left, saddr))
1661 			return (1);
1662 		if (chk_host(dp, saddr, &defset, &hostset))
1663 			return (1);
1664 		if (scan_tree(dp->dp_right, saddr))
1665 			return (1);
1666 	}
1667 	return (0);
1668 }
1669 
1670 /*
1671  * Traverse the dirlist tree and free it up.
1672  */
1673 static void
free_dir(dp)1674 free_dir(dp)
1675 	struct dirlist *dp;
1676 {
1677 
1678 	if (dp) {
1679 		free_dir(dp->dp_left);
1680 		free_dir(dp->dp_right);
1681 		free_host(dp->dp_hosts);
1682 		free(dp);
1683 	}
1684 }
1685 
1686 /*
1687  * Parse the option string and update fields.
1688  * Option arguments may either be -<option>=<value> or
1689  * -<option> <value>
1690  */
1691 static int
do_opt(line,lineno,cpp,endcpp,ep,grp,has_hostp,exflagsp,cr)1692 do_opt(line, lineno, cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
1693 	const char *line;
1694 	size_t lineno;
1695 	char **cpp, **endcpp;
1696 	struct exportlist *ep;
1697 	struct grouplist *grp;
1698 	int *has_hostp;
1699 	int *exflagsp;
1700 	struct uucred *cr;
1701 {
1702 	char *cpoptarg, *cpoptend;
1703 	char *cp, *cpopt, savedc, savedc2;
1704 	char *endcp = NULL;	/* XXX: GCC */
1705 	int allflag, usedarg;
1706 
1707 	cpopt = *cpp;
1708 	cpopt++;
1709 	cp = *endcpp;
1710 	savedc = *cp;
1711 	*cp = '\0';
1712 	while (cpopt && *cpopt) {
1713 		allflag = 1;
1714 		usedarg = -2;
1715 		savedc2 = '\0';
1716 		if ((cpoptend = strchr(cpopt, ',')) != NULL) {
1717 			*cpoptend++ = '\0';
1718 			if ((cpoptarg = strchr(cpopt, '=')) != NULL)
1719 				*cpoptarg++ = '\0';
1720 		} else {
1721 			if ((cpoptarg = strchr(cpopt, '=')) != NULL)
1722 				*cpoptarg++ = '\0';
1723 			else {
1724 				*cp = savedc;
1725 				nextfield(&cp, &endcp);
1726 				**endcpp = '\0';
1727 				if (endcp > cp && *cp != '-') {
1728 					cpoptarg = cp;
1729 					savedc2 = *endcp;
1730 					*endcp = '\0';
1731 					usedarg = 0;
1732 				}
1733 			}
1734 		}
1735 		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1736 			*exflagsp |= MNT_EXRDONLY;
1737 		} else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1738 		    !(allflag = strcmp(cpopt, "mapall")) ||
1739 		    !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1740 			usedarg++;
1741 			parsecred(cpoptarg, cr);
1742 			if (allflag == 0) {
1743 				*exflagsp |= MNT_EXPORTANON;
1744 				opt_flags |= OP_MAPALL;
1745 			} else
1746 				opt_flags |= OP_MAPROOT;
1747 		} else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
1748 			*exflagsp |= MNT_EXKERB;
1749 			opt_flags |= OP_KERB;
1750 		} else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1751 		    !strcmp(cpopt, "m"))) {
1752 			if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1753 				syslog(LOG_ERR,
1754 				    "\"%s\", line %ld: Bad mask: %s",
1755 				    line, (unsigned long)lineno, cpoptarg);
1756 				return (1);
1757 			}
1758 			usedarg++;
1759 			opt_flags |= OP_MASK;
1760 		} else if (cpoptarg && (!strcmp(cpopt, "network") ||
1761 		    !strcmp(cpopt, "n"))) {
1762 			if (strchr(cpoptarg, '/') != NULL) {
1763 				if (debug)
1764 					fprintf(stderr, "setting OP_MASKLEN\n");
1765 				opt_flags |= OP_MASKLEN;
1766 			}
1767 			if (grp->gr_type != GT_NULL) {
1768 				syslog(LOG_ERR,
1769 				    "\"%s\", line %ld: Network/host conflict",
1770 				    line, (unsigned long)lineno);
1771 				return (1);
1772 			} else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1773 				syslog(LOG_ERR,
1774 				    "\"%s\", line %ld: Bad net: %s",
1775 				    line, (unsigned long)lineno, cpoptarg);
1776 				return (1);
1777 			}
1778 			grp->gr_type = GT_NET;
1779 			*has_hostp = 1;
1780 			usedarg++;
1781 			opt_flags |= OP_NET;
1782 		} else if (!strcmp(cpopt, "alldirs")) {
1783 			opt_flags |= OP_ALLDIRS;
1784 		} else if (!strcmp(cpopt, "noresvmnt")) {
1785 			opt_flags |= OP_NORESMNT;
1786 		} else if (!strcmp(cpopt, "noresvport")) {
1787 			opt_flags |= OP_NORESPORT;
1788 			*exflagsp |= MNT_EXNORESPORT;
1789 		} else if (!strcmp(cpopt, "public")) {
1790 			*exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC);
1791 			opt_flags |= OP_NORESPORT;
1792 		} else if (!strcmp(cpopt, "webnfs")) {
1793 			*exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC |
1794 			    MNT_EXRDONLY | MNT_EXPORTANON);
1795 			opt_flags |= (OP_MAPALL | OP_NORESPORT);
1796 		} else if (cpoptarg && !strcmp(cpopt, "index")) {
1797 			ep->ex_indexfile = strdup(cpoptarg);
1798 		} else {
1799 			syslog(LOG_ERR,
1800 			    "\"%s\", line %ld: Bad opt %s",
1801 			    line, (unsigned long)lineno, cpopt);
1802 			return (1);
1803 		}
1804 		if (usedarg >= 0) {
1805 			*endcp = savedc2;
1806 			**endcpp = savedc;
1807 			if (usedarg > 0) {
1808 				*cpp = cp;
1809 				*endcpp = endcp;
1810 			}
1811 			return (0);
1812 		}
1813 		cpopt = cpoptend;
1814 	}
1815 	**endcpp = savedc;
1816 	return (0);
1817 }
1818 
1819 /*
1820  * Translate a character string to the corresponding list of network
1821  * addresses for a hostname.
1822  */
1823 static int
get_host(line,lineno,cp,grp)1824 get_host(line, lineno, cp, grp)
1825 	const char *line;
1826 	size_t lineno;
1827 	const char *cp;
1828 	struct grouplist *grp;
1829 {
1830 	struct addrinfo *ai, hints;
1831 	int ecode;
1832 	char host[NI_MAXHOST];
1833 
1834 	if (grp->gr_type != GT_NULL) {
1835 		syslog(LOG_ERR,
1836 		    "\"%s\", line %ld: Bad netgroup type for ip host %s",
1837 		    line, (unsigned long)lineno, cp);
1838 		return (1);
1839 	}
1840 	memset(&hints, 0, sizeof hints);
1841 	hints.ai_flags = AI_CANONNAME;
1842 	hints.ai_protocol = IPPROTO_UDP;
1843 	ecode = getaddrinfo(cp, NULL, &hints, &ai);
1844 	if (ecode != 0) {
1845 		syslog(LOG_ERR, "\"%s\", line %ld: can't get address info for "
1846 				"host %s",
1847 		    line, (long)lineno, cp);
1848 		return 1;
1849 	}
1850 	grp->gr_type = GT_HOST;
1851 	grp->gr_ptr.gt_addrinfo = ai;
1852 	while (ai != NULL) {
1853 		if (ai->ai_canonname == NULL) {
1854 			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
1855 			    sizeof host, NULL, 0, ninumeric) != 0)
1856 				strlcpy(host, "?", sizeof(host));
1857 			ai->ai_canonname = estrdup(host);
1858 			ai->ai_flags |= AI_CANONNAME;
1859 		} else
1860 			ai->ai_flags &= ~AI_CANONNAME;
1861 		if (debug)
1862 			(void)fprintf(stderr, "got host %s\n", ai->ai_canonname);
1863 		ai = ai->ai_next;
1864 	}
1865 	return (0);
1866 }
1867 
1868 /*
1869  * Free up an exports list component
1870  */
1871 static void
free_exp(ep)1872 free_exp(ep)
1873 	struct exportlist *ep;
1874 {
1875 
1876 	if (ep->ex_defdir) {
1877 		free_host(ep->ex_defdir->dp_hosts);
1878 		free(ep->ex_defdir);
1879 	}
1880 	if (ep->ex_fsdir)
1881 		free(ep->ex_fsdir);
1882 	if (ep->ex_indexfile)
1883 		free(ep->ex_indexfile);
1884 	free_dir(ep->ex_dirl);
1885 	free(ep);
1886 }
1887 
1888 /*
1889  * Free hosts.
1890  */
1891 static void
free_host(hp)1892 free_host(hp)
1893 	struct hostlist *hp;
1894 {
1895 	struct hostlist *hp2;
1896 
1897 	while (hp) {
1898 		hp2 = hp;
1899 		hp = hp->ht_next;
1900 		free(hp2);
1901 	}
1902 }
1903 
1904 static struct hostlist *
get_ht()1905 get_ht()
1906 {
1907 	struct hostlist *hp;
1908 
1909 	hp = emalloc(sizeof(struct hostlist));
1910 	hp->ht_next = NULL;
1911 	hp->ht_flag = 0;
1912 	return (hp);
1913 }
1914 
1915 /*
1916  * Do the nfssvc syscall to push the export info into the kernel.
1917  */
1918 static int
do_nfssvc(line,lineno,ep,grp,exflags,anoncrp,dirp,dirplen,fsb)1919 do_nfssvc(line, lineno, ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
1920 	const char *line;
1921 	size_t lineno;
1922 	struct exportlist *ep;
1923 	struct grouplist *grp;
1924 	int exflags;
1925 	struct uucred *anoncrp;
1926 	char *dirp;
1927 	int dirplen;
1928 	struct statvfs *fsb;
1929 {
1930 	struct sockaddr *addrp;
1931 	struct sockaddr_storage ss;
1932 	struct addrinfo *ai;
1933 	int addrlen;
1934 	int done;
1935 	struct export_args export;
1936 
1937 	export.ex_flags = exflags;
1938 	export.ex_anon = *anoncrp;
1939 	export.ex_indexfile = ep->ex_indexfile;
1940 	if (grp->gr_type == GT_HOST) {
1941 		ai = grp->gr_ptr.gt_addrinfo;
1942 		addrp = ai->ai_addr;
1943 		addrlen = ai->ai_addrlen;
1944 	} else {
1945 		addrp = NULL;
1946 		ai = NULL;	/* XXXGCC -Wuninitialized */
1947 		addrlen = 0;	/* XXXGCC -Wuninitialized */
1948 	}
1949 	done = FALSE;
1950 	while (!done) {
1951 		struct mountd_exports_list mel;
1952 
1953 		switch (grp->gr_type) {
1954 		case GT_HOST:
1955 			if (addrp != NULL && addrp->sa_family == AF_INET6 &&
1956 			    have_v6 == 0)
1957 				goto skip;
1958 			export.ex_addr = addrp;
1959 			export.ex_addrlen = addrlen;
1960 			export.ex_masklen = 0;
1961 			break;
1962 		case GT_NET:
1963 			export.ex_addr = (struct sockaddr *)
1964 			    &grp->gr_ptr.gt_net.nt_net;
1965 			if (export.ex_addr->sa_family == AF_INET6 &&
1966 			    have_v6 == 0)
1967 				goto skip;
1968 			export.ex_addrlen = export.ex_addr->sa_len;
1969 			memset(&ss, 0, sizeof ss);
1970 			ss.ss_family = export.ex_addr->sa_family;
1971 			ss.ss_len = export.ex_addr->sa_len;
1972 			if (allones(&ss, grp->gr_ptr.gt_net.nt_len) != 0) {
1973 				syslog(LOG_ERR,
1974 				    "\"%s\", line %ld: Bad network flag",
1975 				    line, (unsigned long)lineno);
1976 				return (1);
1977 			}
1978 			export.ex_mask = (struct sockaddr *)&ss;
1979 			export.ex_masklen = ss.ss_len;
1980 			break;
1981 		default:
1982 			syslog(LOG_ERR, "\"%s\", line %ld: Bad netgroup type",
1983 			    line, (unsigned long)lineno);
1984 			return (1);
1985 		};
1986 
1987 		/*
1988 		 * XXX:
1989 		 * Maybe I should just use the fsb->f_mntonname path?
1990 		 */
1991 
1992 		mel.mel_path = dirp;
1993 		mel.mel_nexports = 1;
1994 		mel.mel_exports = &export;
1995 
1996 		if (rump_sys_nfssvc(NFSSVC_SETEXPORTSLIST, &mel) != 0) {
1997 			syslog(LOG_ERR,
1998 	    "\"%s\", line %ld: Can't change attributes for %s to %s (%s)",
1999 			    line, (unsigned long)lineno,
2000 			    dirp, (grp->gr_type == GT_HOST) ?
2001 			    grp->gr_ptr.gt_addrinfo->ai_canonname :
2002 			    (grp->gr_type == GT_NET) ?
2003 			    grp->gr_ptr.gt_net.nt_name :
2004 			    "Unknown", strerror(errno));
2005 			return (1);
2006 		}
2007 skip:
2008 		if (addrp) {
2009 			ai = ai->ai_next;
2010 			if (ai == NULL)
2011 				done = TRUE;
2012 			else {
2013 				addrp = ai->ai_addr;
2014 				addrlen = ai->ai_addrlen;
2015 			}
2016 		} else
2017 			done = TRUE;
2018 	}
2019 	return (0);
2020 }
2021 
2022 /*
2023  * Translate a net address.
2024  */
2025 static int
get_net(cp,net,maskflg)2026 get_net(cp, net, maskflg)
2027 	char *cp;
2028 	struct netmsk *net;
2029 	int maskflg;
2030 {
2031 	struct netent *np;
2032 	char *thename, *p, *prefp;
2033 	struct sockaddr_in sin, *sinp;
2034 	struct sockaddr *sa;
2035 	struct addrinfo hints, *ai = NULL;
2036 	char netname[NI_MAXHOST];
2037 	long preflen;
2038 	int ecode;
2039 
2040 	(void)memset(&sin, 0, sizeof(sin));
2041 	if ((opt_flags & OP_MASKLEN) && !maskflg) {
2042 		p = strchr(cp, '/');
2043 		*p = '\0';
2044 		prefp = p + 1;
2045 	} else {
2046 		p = NULL;	/* XXXGCC -Wuninitialized */
2047 		prefp = NULL;	/* XXXGCC -Wuninitialized */
2048 	}
2049 
2050 	if ((np = getnetbyname(cp)) != NULL) {
2051 		sin.sin_family = AF_INET;
2052 		sin.sin_len = sizeof sin;
2053 		sin.sin_addr = inet_makeaddr(np->n_net, 0);
2054 		sa = (struct sockaddr *)&sin;
2055 	} else if (isdigit((unsigned char)*cp)) {
2056 		memset(&hints, 0, sizeof hints);
2057 		hints.ai_family = AF_UNSPEC;
2058 		hints.ai_flags = AI_NUMERICHOST;
2059 		if (getaddrinfo(cp, NULL, &hints, &ai) != 0) {
2060 			/*
2061 			 * If getaddrinfo() failed, try the inet4 network
2062 			 * notation with less than 3 dots.
2063 			 */
2064 			sin.sin_family = AF_INET;
2065 			sin.sin_len = sizeof sin;
2066 			sin.sin_addr = inet_makeaddr(inet_network(cp),0);
2067 			if (debug)
2068 				fprintf(stderr, "get_net: v4 addr %x\n",
2069 				    sin.sin_addr.s_addr);
2070 			sa = (struct sockaddr *)&sin;
2071 		} else
2072 			sa = ai->ai_addr;
2073 	} else if (isxdigit((unsigned char)*cp) || *cp == ':') {
2074 		memset(&hints, 0, sizeof hints);
2075 		hints.ai_family = AF_UNSPEC;
2076 		hints.ai_flags = AI_NUMERICHOST;
2077 		if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2078 			sa = ai->ai_addr;
2079 		else
2080 			goto fail;
2081 	} else
2082 		goto fail;
2083 
2084 	/*
2085 	 * Only allow /pref notation for v6 addresses.
2086 	 */
2087 	if (sa->sa_family == AF_INET6 && (!(opt_flags & OP_MASKLEN) || maskflg))
2088 		return 1;
2089 
2090 	ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2091 	    NULL, 0, ninumeric);
2092 	if (ecode != 0)
2093 		goto fail;
2094 
2095 	if (maskflg)
2096 		net->nt_len = countones(sa);
2097 	else {
2098 		if (opt_flags & OP_MASKLEN) {
2099 			errno = 0;
2100 			preflen = strtol(prefp, NULL, 10);
2101 			if (preflen == LONG_MIN && errno == ERANGE)
2102 				goto fail;
2103 			net->nt_len = (int)preflen;
2104 			*p = '/';
2105 		}
2106 
2107 		if (np)
2108 			thename = np->n_name;
2109 		else {
2110 			if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2111 			    NULL, 0, ninumeric) != 0)
2112 				strlcpy(netname, "?", sizeof(netname));
2113 			thename = netname;
2114 		}
2115 		net->nt_name = estrdup(thename);
2116 		memcpy(&net->nt_net, sa, sa->sa_len);
2117 	}
2118 
2119 	if (!maskflg && sa->sa_family == AF_INET &&
2120 	    !(opt_flags & (OP_MASK|OP_MASKLEN))) {
2121 		sinp = (struct sockaddr_in *)sa;
2122 		if (IN_CLASSA(sinp->sin_addr.s_addr))
2123 			net->nt_len = 8;
2124 		else if (IN_CLASSB(sinp->sin_addr.s_addr))
2125 			net->nt_len = 16;
2126 		else if (IN_CLASSC(sinp->sin_addr.s_addr))
2127 			net->nt_len = 24;
2128 		else if (IN_CLASSD(sinp->sin_addr.s_addr))
2129 			net->nt_len = 28;
2130 		else
2131 			net->nt_len = 32;	/* XXX */
2132 	}
2133 
2134 	if (ai)
2135 		freeaddrinfo(ai);
2136 	return 0;
2137 
2138 fail:
2139 	if (ai)
2140 		freeaddrinfo(ai);
2141 	return 1;
2142 }
2143 
2144 /*
2145  * Parse out the next white space separated field
2146  */
2147 static void
nextfield(cp,endcp)2148 nextfield(cp, endcp)
2149 	char **cp;
2150 	char **endcp;
2151 {
2152 	char *p;
2153 
2154 	p = *cp;
2155 	while (*p == ' ' || *p == '\t')
2156 		p++;
2157 	if (*p == '\n' || *p == '\0')
2158 		*cp = *endcp = p;
2159 	else {
2160 		*cp = p++;
2161 		while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2162 			p++;
2163 		*endcp = p;
2164 	}
2165 }
2166 
2167 /*
2168  * Parse a description of a credential.
2169  */
2170 static void
parsecred(namelist,cr)2171 parsecred(namelist, cr)
2172 	char *namelist;
2173 	struct uucred *cr;
2174 {
2175 	char *thename;
2176 	int cnt;
2177 	char *names;
2178 	struct passwd *pw;
2179 	struct group *gr;
2180 	int ngroups;
2181 	gid_t grps[NGROUPS + 1];
2182 
2183 	/*
2184 	 * Set up the unprivileged user.
2185 	 */
2186 	*cr = def_anon;
2187 	/*
2188 	 * Get the user's password table entry.
2189 	 */
2190 	names = strsep(&namelist, " \t\n");
2191 	thename = strsep(&names, ":");
2192 	if (isdigit((unsigned char)*thename) || *thename == '-')
2193 		pw = getpwuid(atoi(thename));
2194 	else
2195 		pw = getpwnam(thename);
2196 	/*
2197 	 * Credentials specified as those of a user.
2198 	 */
2199 	if (names == NULL) {
2200 		if (pw == NULL) {
2201 			syslog(LOG_ERR, "Unknown user: %s", thename);
2202 			return;
2203 		}
2204 		cr->cr_uid = pw->pw_uid;
2205 		ngroups = NGROUPS + 1;
2206 		if (getgrouplist(pw->pw_name, pw->pw_gid, grps, &ngroups))
2207 			syslog(LOG_ERR, "Too many groups for user %s", thename);
2208 		/*
2209 		 * Convert from int's to gid_t's and compress out duplicate
2210 		 */
2211 		cr->cr_ngroups = ngroups - 1;
2212 		cr->cr_gid = grps[0];
2213 		for (cnt = 1; cnt < ngroups; cnt++)
2214 			cr->cr_groups[cnt - 1] = grps[cnt];
2215 		return;
2216 	}
2217 	/*
2218 	 * Explicit credential specified as a colon separated list:
2219 	 *	uid:gid:gid:...
2220 	 */
2221 	if (pw != NULL)
2222 		cr->cr_uid = pw->pw_uid;
2223 	else if (isdigit((unsigned char)*thename) || *thename == '-')
2224 		cr->cr_uid = atoi(thename);
2225 	else {
2226 		syslog(LOG_ERR, "Unknown user: %s", thename);
2227 		return;
2228 	}
2229 	cr->cr_ngroups = 0;
2230 	while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
2231 		thename = strsep(&names, ":");
2232 		if (isdigit((unsigned char)*thename) || *thename == '-') {
2233 			cr->cr_groups[cr->cr_ngroups++] = atoi(thename);
2234 		} else {
2235 			if ((gr = getgrnam(thename)) == NULL) {
2236 				syslog(LOG_ERR, "Unknown group: %s", thename);
2237 				continue;
2238 			}
2239 			cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2240 		}
2241 	}
2242 	if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
2243 		syslog(LOG_ERR, "Too many groups");
2244 }
2245 
2246 #define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
2247 /*
2248  * Routines that maintain the remote mounttab
2249  */
2250 static void
get_mountlist()2251 get_mountlist()
2252 {
2253 	struct mountlist *mlp, **mlpp;
2254 	char *host, *dirp, *cp;
2255 	char str[STRSIZ];
2256 	FILE *mlfile;
2257 
2258 	if ((mlfile = rumpfopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2259 		syslog(LOG_ERR, "Can't open %s (%s)", _PATH_RMOUNTLIST,
2260 		    strerror(errno));
2261 		return;
2262 	}
2263 	mlpp = &mlhead;
2264 	while (fgets(str, STRSIZ, mlfile) != NULL) {
2265 		cp = str;
2266 		host = strsep(&cp, " \t\n");
2267 		dirp = strsep(&cp, " \t\n");
2268 		if (host == NULL || dirp == NULL)
2269 			continue;
2270 		mlp = emalloc(sizeof(*mlp));
2271 		(void)strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
2272 		mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2273 		(void)strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2274 		mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2275 		mlp->ml_next = NULL;
2276 		*mlpp = mlp;
2277 		mlpp = &mlp->ml_next;
2278 	}
2279 	(void)fclose(mlfile);
2280 }
2281 
2282 static int
del_mlist(hostp,dirp,saddr)2283 del_mlist(hostp, dirp, saddr)
2284 	char *hostp, *dirp;
2285 	struct sockaddr *saddr;
2286 {
2287 	struct mountlist *mlp, **mlpp;
2288 	struct mountlist *mlp2;
2289 	u_short sport;
2290 	FILE *mlfile;
2291 	int fnd = 0, ret = 0;
2292 	char host[NI_MAXHOST];
2293 
2294 	switch (saddr->sa_family) {
2295 	case AF_INET6:
2296 		sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
2297 		break;
2298 	case AF_INET:
2299 		sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
2300 		break;
2301 	default:
2302 		return -1;
2303 	}
2304 	mlpp = &mlhead;
2305 	mlp = mlhead;
2306 	while (mlp) {
2307 		if (!strcmp(mlp->ml_host, hostp) &&
2308 		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2309 			if (!(mlp->ml_flag & DP_NORESMNT) &&
2310 			    sport >= IPPORT_RESERVED) {
2311 				if (getnameinfo(saddr, saddr->sa_len, host,
2312 				    sizeof host, NULL, 0, ninumeric) != 0)
2313 					strlcpy(host, "?", sizeof(host));
2314 				syslog(LOG_NOTICE,
2315 				"Umount request for %s:%s from %s refused\n",
2316 				    mlp->ml_host, mlp->ml_dirp, host);
2317 				ret = -1;
2318 				goto cont;
2319 			}
2320 			fnd = 1;
2321 			mlp2 = mlp;
2322 			*mlpp = mlp = mlp->ml_next;
2323 			free(mlp2);
2324 		} else {
2325 cont:
2326 			mlpp = &mlp->ml_next;
2327 			mlp = mlp->ml_next;
2328 		}
2329 	}
2330 	if (fnd) {
2331 		if ((mlfile = rumpfopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2332 			syslog(LOG_ERR, "Can't update %s (%s)",
2333 			    _PATH_RMOUNTLIST, strerror(errno));
2334 			return ret;
2335 		}
2336 		mlp = mlhead;
2337 		while (mlp) {
2338 			(void)fprintf(mlfile, "%s %s\n", mlp->ml_host,
2339 		            mlp->ml_dirp);
2340 			mlp = mlp->ml_next;
2341 		}
2342 		(void)fclose(mlfile);
2343 	}
2344 	return ret;
2345 }
2346 
2347 static void
add_mlist(hostp,dirp,flags)2348 add_mlist(hostp, dirp, flags)
2349 	char *hostp, *dirp;
2350 	int flags;
2351 {
2352 	struct mountlist *mlp, **mlpp;
2353 	FILE *mlfile;
2354 
2355 	mlpp = &mlhead;
2356 	mlp = mlhead;
2357 	while (mlp) {
2358 		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2359 			return;
2360 		mlpp = &mlp->ml_next;
2361 		mlp = mlp->ml_next;
2362 	}
2363 	mlp = emalloc(sizeof(*mlp));
2364 	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2365 	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2366 	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2367 	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2368 	mlp->ml_flag = flags;
2369 	mlp->ml_next = NULL;
2370 	*mlpp = mlp;
2371 	if ((mlfile = rumpfopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2372 		syslog(LOG_ERR, "Can't update %s (%s)", _PATH_RMOUNTLIST,
2373 		    strerror(errno));
2374 		return;
2375 	}
2376 	(void)fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2377 	(void)fclose(mlfile);
2378 }
2379 
2380 /*
2381  * This function is called via. SIGTERM when the system is going down.
2382  * It sends a broadcast RPCMNT_UMNTALL.
2383  */
2384 /* ARGSUSED */
2385 static void
send_umntall(n)2386 send_umntall(n)
2387 	int n;
2388 {
2389 #if 0
2390 	(void)clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
2391 	    xdr_void, NULL, xdr_void, NULL, (resultproc_t)umntall_each);
2392 #endif
2393 	exit(0);
2394 }
2395 
2396 #if 0
2397 static int
2398 umntall_each(resultsp, raddr)
2399 	caddr_t resultsp;
2400 	struct sockaddr_in *raddr;
2401 {
2402 	return (1);
2403 }
2404 #endif
2405 
2406 /*
2407  * Free up a group list.
2408  */
2409 static void
free_grp(grp)2410 free_grp(grp)
2411 	struct grouplist *grp;
2412 {
2413 
2414 	if (grp->gr_type == GT_HOST) {
2415 		if (grp->gr_ptr.gt_addrinfo != NULL)
2416 			freeaddrinfo(grp->gr_ptr.gt_addrinfo);
2417 	} else if (grp->gr_type == GT_NET) {
2418 		if (grp->gr_ptr.gt_net.nt_name)
2419 			free(grp->gr_ptr.gt_net.nt_name);
2420 	}
2421 	free(grp);
2422 }
2423 
2424 #if 0
2425 static void
2426 SYSLOG(int pri, const char *fmt,...)
2427 {
2428 	va_list ap;
2429 
2430 	va_start(ap, fmt);
2431 
2432 	if (debug)
2433 		vfprintf(stderr, fmt, ap);
2434 	else
2435 		vsyslog(pri, fmt, ap);
2436 
2437 	va_end(ap);
2438 }
2439 #endif
2440 
2441 /*
2442  * Check options for consistency.
2443  */
2444 static int
check_options(line,lineno,dp)2445 check_options(line, lineno, dp)
2446 	const char *line;
2447 	size_t lineno;
2448 	struct dirlist *dp;
2449 {
2450 
2451 	if (dp == NULL) {
2452 		syslog(LOG_ERR,
2453 		    "\"%s\", line %ld: missing directory list",
2454 		    line, (unsigned long)lineno);
2455 		return (1);
2456 	}
2457 	if ((opt_flags & (OP_MAPROOT|OP_MAPALL)) == (OP_MAPROOT|OP_MAPALL) ||
2458 	    (opt_flags & (OP_MAPROOT|OP_KERB)) == (OP_MAPROOT|OP_KERB) ||
2459 	    (opt_flags & (OP_MAPALL|OP_KERB)) == (OP_MAPALL|OP_KERB)) {
2460 		syslog(LOG_ERR,
2461 		    "\"%s\", line %ld: -mapall, -maproot and -kerb mutually exclusive",
2462 		    line, (unsigned long)lineno);
2463 		return (1);
2464 	}
2465 	if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2466 		syslog(LOG_ERR, "\"%s\", line %ld: -mask requires -net",
2467 		    line, (unsigned long)lineno);
2468 		return (1);
2469 	}
2470 	if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN) != 0) {
2471 		syslog(LOG_ERR, "\"%s\", line %ld: /pref and -mask mutually"
2472 		    " exclusive",
2473 		    line, (unsigned long)lineno);
2474 		return (1);
2475 	}
2476 	if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2477 		syslog(LOG_ERR,
2478 		    "\"%s\", line %ld: -alldirs has multiple directories",
2479 		    line, (unsigned long)lineno);
2480 		return (1);
2481 	}
2482 	return (0);
2483 }
2484 
2485 /*
2486  * Check an absolute directory path for any symbolic links. Return true
2487  * if no symbolic links are found.
2488  */
2489 static int
check_dirpath(line,lineno,dirp)2490 check_dirpath(line, lineno, dirp)
2491 	const char *line;
2492 	size_t lineno;
2493 	char *dirp;
2494 {
2495 	char *cp;
2496 	struct stat sb;
2497 	char *file = "";
2498 
2499 	for (cp = dirp + 1; *cp; cp++) {
2500 		if (*cp == '/') {
2501 			*cp = '\0';
2502 			if (rump_sys_lstat(dirp, &sb) == -1)
2503 				goto bad;
2504 			if (!S_ISDIR(sb.st_mode))
2505 				goto bad1;
2506 			*cp = '/';
2507 		}
2508 	}
2509 
2510 	cp = NULL;
2511 	if (rump_sys_lstat(dirp, &sb) == -1)
2512 		goto bad;
2513 
2514 	if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
2515 		file = " file or a";
2516 		goto bad1;
2517 	}
2518 
2519 	return 1;
2520 
2521 bad:
2522 	syslog(LOG_ERR,
2523 	    "\"%s\", line %ld: lstat for `%s' failed (%s)",
2524 	    line, (unsigned long)lineno, dirp, strerror(errno));
2525 	if (cp)
2526 		*cp = '/';
2527 	return 0;
2528 
2529 bad1:
2530 	syslog(LOG_ERR,
2531 	    "\"%s\", line %ld: `%s' is not a%s directory",
2532 	    line, (unsigned long)lineno, dirp, file);
2533 	abort();
2534 	if (cp)
2535 		*cp = '/';
2536 	return 0;
2537 }
2538 
2539 static void
bind_resv_port(int sock,sa_family_t family,in_port_t port)2540 bind_resv_port(int sock, sa_family_t family, in_port_t port)
2541 {
2542 	struct sockaddr *sa;
2543 	struct sockaddr_in sasin;
2544 	struct sockaddr_in6 sasin6;
2545 
2546 	switch (family) {
2547 	case AF_INET:
2548 		(void)memset(&sasin, 0, sizeof(sasin));
2549 		sasin.sin_len = sizeof(sasin);
2550 		sasin.sin_family = family;
2551 		sasin.sin_port = htons(port);
2552 		sa = (struct sockaddr *)(void *)&sasin;
2553 		break;
2554 	case AF_INET6:
2555 		(void)memset(&sasin6, 0, sizeof(sasin6));
2556 		sasin6.sin6_len = sizeof(sasin6);
2557 		sasin6.sin6_family = family;
2558 		sasin6.sin6_port = htons(port);
2559 		sa = (struct sockaddr *)(void *)&sasin6;
2560 		break;
2561 	default:
2562 		syslog(LOG_ERR, "Unsupported address family %d", family);
2563 		return;
2564 	}
2565 	if (bindresvport_sa(sock, sa) == -1)
2566 		syslog(LOG_ERR, "Cannot bind to reserved port %d (%s)", port,
2567 		    strerror(errno));
2568 }
2569 
2570 /* ARGSUSED */
2571 static void
no_nfs(int sig)2572 no_nfs(int sig)
2573 {
2574 	syslog(LOG_ERR, "kernel NFS support not present; exiting");
2575 	exit(1);
2576 }
2577