mountd - Turn on SO_REUSEADDR (2)
[dragonfly.git] / sbin / mountd / mountd.c
CommitLineData
984263bc
MD
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.
dc71b7ab 16 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
1de703da
MD
31 *
32 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)mountd.c 8.15 (Berkeley) 5/1/95
1dc7b4d6 34 * $FreeBSD: head/usr.sbin/mountd/mountd.c 173056 2007-10-27 12:24:47Z simon $
984263bc
MD
35 */
36
984263bc 37#include <sys/param.h>
ab22f470 38#include <sys/module.h>
984263bc 39#include <sys/mount.h>
1aa89f17 40#include <sys/mountctl.h>
ce0e08e2 41#include <sys/fcntl.h>
ab22f470 42#include <sys/linker.h>
984263bc
MD
43#include <sys/stat.h>
44#include <sys/syslog.h>
45#include <sys/sysctl.h>
46
47#include <rpc/rpc.h>
f26349bc 48#include <rpc/rpc_com.h>
ce0e08e2 49#include <rpcsvc/mount.h>
38a690d7
MD
50#include <vfs/nfs/rpcv2.h>
51#include <vfs/nfs/nfsproto.h>
52#include <vfs/nfs/nfs.h>
53#include <vfs/ufs/ufsmount.h>
54#include <vfs/msdosfs/msdosfsmount.h>
55#include <vfs/ntfs/ntfsmount.h>
56#include <vfs/isofs/cd9660/cd9660_mount.h> /* XXX need isofs in include */
984263bc
MD
57
58#include <arpa/inet.h>
59
60#include <ctype.h>
61#include <err.h>
62#include <errno.h>
63#include <grp.h>
e815eb09 64#include <limits.h>
fdff0afe 65#include <libutil.h>
984263bc
MD
66#include <netdb.h>
67#include <pwd.h>
68#include <signal.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <string.h>
72#include <unistd.h>
73#include "pathnames.h"
74
75#ifdef DEBUG
76#include <stdarg.h>
77#endif
78
79/*
80 * Structures for keeping the mount list and export list
81 */
82struct mountlist {
83 struct mountlist *ml_next;
84 char ml_host[RPCMNT_NAMELEN+1];
85 char ml_dirp[RPCMNT_PATHLEN+1];
86};
87
88struct dirlist {
89 struct dirlist *dp_left;
90 struct dirlist *dp_right;
91 int dp_flag;
92 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
284e3b3b 93 char *dp_dirp;
984263bc
MD
94};
95/* dp_flag bits */
96#define DP_DEFSET 0x1
97#define DP_HOSTSET 0x2
984263bc
MD
98
99struct exportlist {
100 struct exportlist *ex_next;
101 struct dirlist *ex_dirl;
102 struct dirlist *ex_defdir;
103 int ex_flag;
104 fsid_t ex_fs;
105 char *ex_fsdir;
106 char *ex_indexfile;
107};
108/* ex_flag bits */
109#define EX_LINKED 0x1
110
111struct netmsk {
ce0e08e2 112 struct sockaddr_storage nt_net;
0e0ae4e8 113 struct sockaddr_storage nt_mask;
984263bc
MD
114 char *nt_name;
115};
116
117union grouptypes {
ce0e08e2 118 struct addrinfo *gt_addrinfo;
984263bc
MD
119 struct netmsk gt_net;
120};
121
122struct grouplist {
123 int gr_type;
124 union grouptypes gr_ptr;
125 struct grouplist *gr_next;
126};
127/* Group types */
128#define GT_NULL 0x0
129#define GT_HOST 0x1
130#define GT_NET 0x2
ce0e08e2 131#define GT_DEFAULT 0x3
984263bc
MD
132#define GT_IGNORE 0x5
133
134struct hostlist {
135 int ht_flag; /* Uses DP_xx bits */
136 struct grouplist *ht_grp;
137 struct hostlist *ht_next;
138};
139
140struct fhreturn {
141 int fhr_flag;
142 int fhr_vers;
143 nfsfh_t fhr_fh;
144};
145
146/* Global defs */
9a76cd75 147char *add_expdir(struct dirlist **, char *, int);
cd5332df
EN
148void add_dlist(struct dirlist **, struct dirlist *,
149 struct grouplist *, int);
9a76cd75
DR
150void add_mlist(char *, char *);
151int check_dirpath(char *);
152int check_options(struct dirlist *);
0e0ae4e8 153int checkmask(struct sockaddr *sa);
ce0e08e2 154int chk_host(struct dirlist *, struct sockaddr *, int *, int *);
2eb4cd83 155void create_service(struct netconfig *nconf);
9a76cd75
DR
156void del_mlist(char *, char *);
157struct dirlist *dirp_search(struct dirlist *, char *);
cd5332df
EN
158int do_mount(struct exportlist *, struct grouplist *, int,
159 struct ucred *, char *, int, struct statfs *);
160int do_opt(char **, char **, struct exportlist *, struct grouplist *,
161 int *, int *, struct ucred *);
9a76cd75
DR
162struct exportlist *ex_search(fsid_t *);
163struct exportlist *get_exp(void);
164void free_dir(struct dirlist *);
165void free_exp(struct exportlist *);
166void free_grp(struct grouplist *);
167void free_host(struct hostlist *);
ce1bcb9e 168void get_exportlist(int incremental);
9a76cd75 169int get_host(char *, struct grouplist *, struct grouplist *);
9a76cd75
DR
170struct hostlist *get_ht(void);
171int get_line(void);
172void get_mountlist(void);
ce1bcb9e 173static void delete_export(struct statfs *fsp);
9a76cd75
DR
174int get_net(char *, struct netmsk *, int);
175void getexp_err(struct exportlist *, struct grouplist *);
176struct grouplist *get_grp(void);
cd5332df
EN
177void hang_dirp(struct dirlist *, struct grouplist *,
178 struct exportlist *, int);
ce1bcb9e
MD
179void huphandler1(int sig);
180void huphandler2(int sig);
0e0ae4e8 181int makemask(struct sockaddr_storage *ssp, int bitlen);
9a76cd75
DR
182void mntsrv(struct svc_req *, SVCXPRT *);
183void nextfield(char **, char **);
a92dccf2 184void out_of_mem(void) __dead2;
9a76cd75 185void parsecred(char *, struct ucred *);
58206a88 186int put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
0e0ae4e8
AHJ
187void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
188int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
189 struct sockaddr *samask);
ce0e08e2 190int scan_tree(struct dirlist *, struct sockaddr *);
9a76cd75
DR
191static void usage(void);
192int xdr_dir(XDR *, char *);
193int xdr_explist(XDR *, caddr_t);
58206a88 194int xdr_explist_brief(XDR *, caddr_t);
ddfadeae 195int xdr_explist_common(XDR *, caddr_t, int);
9a76cd75
DR
196int xdr_fhs(XDR *, caddr_t);
197int xdr_mlist(XDR *, caddr_t);
a92dccf2 198void terminate(int) __dead2;
ce0e08e2 199
984263bc
MD
200struct exportlist *exphead;
201struct mountlist *mlhead;
202struct grouplist *grphead;
356c161f
AHJ
203char *exnames_default[2] = { _PATH_EXPORTS, NULL };
204char **exnames;
2eb4cd83 205char **hosts = NULL;
1285ceee 206struct ucred def_anon;
984263bc 207int force_v2 = 0;
c89b4618 208int resvport_only = 1;
2eb4cd83 209int nhosts = 0;
984263bc 210int dir_only = 1;
8c16b86f 211int dolog = 0;
ce1bcb9e
MD
212int got_sighup1 = 0;
213int got_sighup2 = 0;
ce0e08e2 214
2eb4cd83
AHJ
215int xcreated = 0;
216char *svcport_str = NULL;
984263bc 217int opt_flags;
ce0e08e2 218static int have_v6 = 1;
ce0e08e2 219
fdff0afe 220struct pidfh *pfh = NULL;
0e0ae4e8 221/* Bits for the opt_flags above */
984263bc
MD
222#define OP_MAPROOT 0x01
223#define OP_MAPALL 0x02
ec12fea1 224/* 0x4 free */
984263bc
MD
225#define OP_MASK 0x08
226#define OP_NET 0x10
227#define OP_ALLDIRS 0x40
0e0ae4e8 228#define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
984263bc 229#define OP_QUIET 0x100
ce0e08e2 230#define OP_MASKLEN 0x200
984263bc
MD
231
232#ifdef DEBUG
233int debug = 1;
9a76cd75 234void SYSLOG(int, const char *, ...);
984263bc
MD
235#define syslog SYSLOG
236#else
237int debug = 0;
238#endif
239
240/*
241 * Mountd server for NFS mount protocol as described in:
242 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
243 * The optional arguments are the exports file name
244 * default: _PATH_EXPORTS
245 * and "-n" to allow nonroot mount.
246 */
247int
b5744197 248main(int argc, char **argv)
984263bc 249{
ce0e08e2 250 fd_set readfds;
2eb4cd83
AHJ
251 struct netconfig *nconf;
252 char *endptr, **hosts_bak;
253 void *nc_handle;
fdff0afe 254 pid_t otherpid;
2eb4cd83
AHJ
255 in_port_t svcport;
256 int c, k, s;
f26349bc 257 int maxrec = RPC_MAXDATASIZE;
ce0e08e2 258
1285ceee 259 def_anon.cr_ref = 1;
19e6154d 260 def_anon.cr_uid = (uid_t)65534;
1285ceee 261 def_anon.cr_ngroups = 1;
19e6154d 262 def_anon.cr_groups[0] = (gid_t)65533;
1285ceee 263
ce0e08e2 264 /* Check that another mountd isn't already running. */
fdff0afe
SW
265 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
266 if (pfh == NULL) {
267 if (errno == EEXIST)
268 errx(1, "mountd already running, pid: %d.", otherpid);
269 warn("cannot open or create pidfile");
270 }
ce0e08e2 271
ce0e08e2
PA
272 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
273 if (s < 0)
274 have_v6 = 0;
275 else
276 close(s);
ab22f470
AHJ
277 if (modfind("nfs") < 0) {
278 /* Not present in kernel, try loading it */
279 if (kldload("nfs") < 0 || modfind("nfs") < 0)
280 errx(1, "NFS server is not available or loadable");
984263bc 281 }
984263bc 282
2eb4cd83 283 while ((c = getopt(argc, argv, "2dh:lnp:r")) != -1) {
984263bc
MD
284 switch (c) {
285 case '2':
286 force_v2 = 1;
287 break;
288 case 'n':
289 resvport_only = 0;
290 break;
291 case 'r':
292 dir_only = 0;
293 break;
294 case 'd':
295 debug = debug ? 0 : 1;
296 break;
297 case 'l':
8c16b86f 298 dolog = 1;
984263bc 299 break;
ad944d0d
AHJ
300 case 'p':
301 endptr = NULL;
302 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
303 if (endptr == NULL || *endptr != '\0' ||
304 svcport == 0 || svcport >= IPPORT_MAX)
305 usage();
2eb4cd83
AHJ
306 svcport_str = strdup(optarg);
307 break;
308 case 'h':
309 ++nhosts;
310 hosts_bak = hosts;
311 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
312 if (hosts_bak == NULL) {
313 if (hosts != NULL) {
314 for (k = 0; k < nhosts; k++)
315 free(hosts[k]);
316 free(hosts);
317 out_of_mem();
318 }
319 }
320 hosts = hosts_bak;
321 hosts[nhosts - 1] = strdup(optarg);
322 if (hosts[nhosts - 1] == NULL) {
323 for (k = 0; k < (nhosts - 1); k++)
324 free(hosts[k]);
325 free(hosts);
326 out_of_mem();
327 }
ad944d0d 328 break;
984263bc
MD
329 default:
330 usage();
0fdb7d01 331 }
1a9480c3
MD
332 }
333 if (debug == 0) {
334 daemon(0, 0);
335 signal(SIGINT, SIG_IGN);
336 signal(SIGQUIT, SIG_IGN);
337 }
984263bc
MD
338 argc -= optind;
339 argv += optind;
2038fb68
SW
340 grphead = NULL;
341 exphead = NULL;
342 mlhead = NULL;
356c161f
AHJ
343 if (argc > 0)
344 exnames = argv;
345 else
346 exnames = exnames_default;
984263bc
MD
347 openlog("mountd", LOG_PID, LOG_DAEMON);
348 if (debug)
349 warnx("getting export list");
ce1bcb9e 350 get_exportlist(0);
984263bc
MD
351 if (debug)
352 warnx("getting mount list");
353 get_mountlist();
354 if (debug)
355 warnx("here we go");
ce1bcb9e
MD
356 signal(SIGHUP, huphandler1);
357 signal(SIGUSR1, huphandler2);
ce0e08e2 358 signal(SIGTERM, terminate);
121f4a86 359 signal(SIGPIPE, SIG_IGN);
fdff0afe
SW
360
361 pidfile_write(pfh);
362
ce0e08e2
PA
363 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
364 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
f26349bc
AHJ
365
366 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
367
984263bc 368 if (!resvport_only) {
ab22f470
AHJ
369 if (sysctlbyname("vfs.nfs.nfs_privport", NULL, NULL,
370 &resvport_only, sizeof(resvport_only)) != 0 &&
371 errno != ENOENT) {
984263bc
MD
372 syslog(LOG_ERR, "sysctl: %m");
373 exit(1);
374 }
375 }
ce0e08e2 376
2eb4cd83
AHJ
377 /*
378 * If no hosts were specified, add a wildcard entry to bind to
379 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
380 * list.
381 */
382 if (nhosts == 0) {
d495caca 383 hosts = malloc(sizeof(char *));
2eb4cd83
AHJ
384 if (hosts == NULL)
385 out_of_mem();
386 hosts[0] = "*";
387 nhosts = 1;
388 } else {
389 hosts_bak = hosts;
390 if (have_v6) {
391 hosts_bak = realloc(hosts, (nhosts + 2) *
392 sizeof(char *));
393 if (hosts_bak == NULL) {
394 for (k = 0; k < nhosts; k++)
395 free(hosts[k]);
396 free(hosts);
397 out_of_mem();
398 } else
399 hosts = hosts_bak;
400 nhosts += 2;
401 hosts[nhosts - 2] = "::1";
402 } else {
403 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
404 if (hosts_bak == NULL) {
405 for (k = 0; k < nhosts; k++)
406 free(hosts[k]);
407 free(hosts);
408 out_of_mem();
409 } else {
410 nhosts += 1;
411 hosts = hosts_bak;
ce0e08e2 412 }
2eb4cd83
AHJ
413 }
414 hosts[nhosts - 1] = "127.0.0.1";
ce0e08e2 415 }
ce0e08e2 416
2eb4cd83
AHJ
417 nc_handle = setnetconfig();
418 while ((nconf = getnetconfig(nc_handle))) {
419 if (nconf->nc_flag & NC_VISIBLE) {
420 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
421 "inet6") == 0) {
422 /* DO NOTHING */
423 } else
424 create_service(nconf);
425 }
ce0e08e2 426 }
2eb4cd83 427 endnetconfig(nc_handle);
ce0e08e2 428
ce0e08e2
PA
429 if (xcreated == 0) {
430 syslog(LOG_ERR, "could not create any services");
431 exit(1);
432 }
433
434 /* Expand svc_run() here so that we can call get_exportlist(). */
435 for (;;) {
ce1bcb9e
MD
436 if (got_sighup1) {
437 got_sighup1 = 0;
438 get_exportlist(0);
439 }
440 if (got_sighup2) {
441 got_sighup2 = 0;
442 get_exportlist(1);
ce0e08e2
PA
443 }
444 readfds = svc_fdset;
445 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
446 case -1:
447 if (errno == EINTR)
448 continue;
449 syslog(LOG_ERR, "mountd died: select: %m");
984263bc 450 exit(1);
ce0e08e2
PA
451 case 0:
452 continue;
453 default:
454 svc_getreqset(&readfds);
984263bc 455 }
984263bc 456 }
984263bc
MD
457}
458
2eb4cd83
AHJ
459/*
460 * This routine creates and binds sockets on the appropriate
461 * addresses. It gets called one time for each transport and
5abbd6f3 462 * registrates the service with rpcbind on that transport.
2eb4cd83
AHJ
463 */
464void
465create_service(struct netconfig *nconf)
466{
467 struct addrinfo hints, *res = NULL;
468 struct sockaddr_in *sin;
469 struct sockaddr_in6 *sin6;
470 struct __rpc_sockinfo si;
471 struct netbuf servaddr;
472 SVCXPRT *transp = NULL;
473 int aicode;
474 int fd;
475 int nhostsbak;
476 int one = 1;
477 int r;
478 int registered = 0;
479 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
480
481 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
482 (nconf->nc_semantics != NC_TPI_COTS) &&
483 (nconf->nc_semantics != NC_TPI_COTS_ORD))
484 return; /* not my type */
485
486 /*
487 * XXX - using RPC library internal functions.
488 */
489 if (!__rpc_nconf2sockinfo(nconf, &si)) {
490 syslog(LOG_ERR, "cannot get information for %s",
491 nconf->nc_netid);
492 return;
493 }
494
495 /* Get mountd's address on this transport */
496 memset(&hints, 0, sizeof hints);
497 hints.ai_flags = AI_PASSIVE;
498 hints.ai_family = si.si_af;
499 hints.ai_socktype = si.si_socktype;
500 hints.ai_protocol = si.si_proto;
501
502 /*
503 * Bind to specific IPs if asked to
504 */
505 nhostsbak = nhosts;
506 while (nhostsbak > 0) {
507 --nhostsbak;
508 /*
509 * XXX - using RPC library internal functions.
510 */
511 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
512 int non_fatal = 0;
513 if (errno == EPROTONOSUPPORT &&
514 nconf->nc_semantics != NC_TPI_CLTS)
515 non_fatal = 1;
516
517 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
518 "cannot create socket for %s", nconf->nc_netid);
519 return;
520 }
521
522 switch (hints.ai_family) {
523 case AF_INET:
524 if (inet_pton(AF_INET, hosts[nhostsbak],
525 host_addr) == 1) {
526 hints.ai_flags &= AI_NUMERICHOST;
527 } else {
528 /*
529 * Skip if we have an AF_INET6 address.
530 */
531 if (inet_pton(AF_INET6, hosts[nhostsbak],
532 host_addr) == 1) {
533 close(fd);
534 continue;
535 }
536 }
537 break;
538 case AF_INET6:
539 if (inet_pton(AF_INET6, hosts[nhostsbak],
540 host_addr) == 1) {
541 hints.ai_flags &= AI_NUMERICHOST;
542 } else {
543 /*
544 * Skip if we have an AF_INET address.
545 */
546 if (inet_pton(AF_INET, hosts[nhostsbak],
547 host_addr) == 1) {
548 close(fd);
549 continue;
550 }
551 }
552
553 /*
554 * We're doing host-based access checks here, so don't
555 * allow v4-in-v6 to confuse things. The kernel will
556 * disable it by default on NFS sockets too.
557 */
558 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
559 sizeof one) < 0) {
560 syslog(LOG_ERR,
561 "can't disable v4-in-v6 on IPv6 socket");
562 exit(1);
563 }
564 break;
565 default:
566 break;
567 }
568
569 /*
570 * If no hosts were specified, just bind to INADDR_ANY
571 */
572 if (strcmp("*", hosts[nhostsbak]) == 0) {
573 if (svcport_str == NULL) {
574 res = malloc(sizeof(struct addrinfo));
575 if (res == NULL)
576 out_of_mem();
577 res->ai_flags = hints.ai_flags;
578 res->ai_family = hints.ai_family;
579 res->ai_protocol = hints.ai_protocol;
5abbd6f3 580
2eb4cd83
AHJ
581 switch (res->ai_family) {
582 case AF_INET:
583 sin = malloc(sizeof(struct sockaddr_in));
584 if (sin == NULL)
585 out_of_mem();
586 sin->sin_family = AF_INET;
587 sin->sin_port = htons(0);
588 sin->sin_addr.s_addr = htonl(INADDR_ANY);
589 res->ai_addr = (struct sockaddr*) sin;
590 res->ai_addrlen = (socklen_t)
591 sizeof(res->ai_addr);
592 break;
593 case AF_INET6:
594 sin6 = malloc(sizeof(struct sockaddr_in6));
1dc7b4d6 595 if (sin6 == NULL)
2eb4cd83
AHJ
596 out_of_mem();
597 sin6->sin6_family = AF_INET6;
598 sin6->sin6_port = htons(0);
599 sin6->sin6_addr = in6addr_any;
600 res->ai_addr = (struct sockaddr*) sin6;
601 res->ai_addrlen = (socklen_t)
602 sizeof(res->ai_addr);
603 break;
604 default:
605 break;
606 }
607 } else {
608 if ((aicode = getaddrinfo(NULL, svcport_str,
609 &hints, &res)) != 0) {
610 syslog(LOG_ERR,
611 "cannot get local address for %s: %s",
612 nconf->nc_netid,
613 gai_strerror(aicode));
614 continue;
615 }
616 }
617 } else {
618 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
619 &hints, &res)) != 0) {
620 syslog(LOG_ERR,
621 "cannot get local address for %s: %s",
622 nconf->nc_netid, gai_strerror(aicode));
623 continue;
624 }
625 }
626
5abbd6f3
MD
627 /*
628 * Set SO_REUSEADDR only if binding to a specific port
629 */
630 if (svcport_str) {
ef7289bf
MD
631 int on = 1;
632
633 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
634 (char *)&on, sizeof(on));
635 }
636
2eb4cd83
AHJ
637 r = bindresvport_sa(fd, res->ai_addr);
638 if (r != 0) {
5abbd6f3
MD
639 struct sockaddr_in *sin = (void *)res->ai_addr;
640 struct sockaddr_in6 *sin6 = (void *)res->ai_addr;
641 const char *protostr;
642 int portno;
643
644 switch(sin->sin_family) {
645 case AF_INET:
646 protostr = "inet";
647 portno = ntohs(sin->sin_port);
648 break;
649 case AF_INET6:
650 protostr = "inet6";
651 portno = ntohs(sin6->sin6_port);
652 break;
653 default:
654 protostr = "unknown";
655 portno = -1;
656 break;
657 }
658
659 syslog(LOG_ERR, "bindresvport_sa: %m proto %s port %d",
660 protostr, portno);
2eb4cd83
AHJ
661 exit(1);
662 }
663
664 if (nconf->nc_semantics != NC_TPI_CLTS)
665 listen(fd, SOMAXCONN);
666
667 if (nconf->nc_semantics == NC_TPI_CLTS )
668 transp = svc_dg_create(fd, 0, 0);
669 else
670 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
671 RPC_MAXDATASIZE);
672
a82d5481 673 if (transp != NULL) {
2eb4cd83
AHJ
674 if (!svc_reg(transp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
675 NULL))
676 syslog(LOG_ERR,
677 "can't register %s RPCMNT_VER1 service",
678 nconf->nc_netid);
679 if (!force_v2) {
680 if (!svc_reg(transp, RPCPROG_MNT, RPCMNT_VER3,
681 mntsrv, NULL))
682 syslog(LOG_ERR,
683 "can't register %s RPCMNT_VER3 service",
684 nconf->nc_netid);
685 }
686 } else
687 syslog(LOG_WARNING, "can't create %s services",
688 nconf->nc_netid);
689
690 if (registered == 0) {
691 registered = 1;
692 memset(&hints, 0, sizeof hints);
693 hints.ai_flags = AI_PASSIVE;
694 hints.ai_family = si.si_af;
695 hints.ai_socktype = si.si_socktype;
696 hints.ai_protocol = si.si_proto;
697
698 if (svcport_str == NULL) {
699 svcport_str = malloc(NI_MAXSERV * sizeof(char));
700 if (svcport_str == NULL)
701 out_of_mem();
702
703 if (getnameinfo(res->ai_addr,
704 res->ai_addr->sa_len, NULL, NI_MAXHOST,
705 svcport_str, NI_MAXSERV * sizeof(char),
706 NI_NUMERICHOST | NI_NUMERICSERV))
707 errx(1, "Cannot get port number");
708 }
709
710 if((aicode = getaddrinfo(NULL, svcport_str, &hints,
711 &res)) != 0) {
712 syslog(LOG_ERR, "cannot get local address: %s",
713 gai_strerror(aicode));
714 exit(1);
715 }
716
717 servaddr.buf = malloc(res->ai_addrlen);
718 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
719 servaddr.len = res->ai_addrlen;
720
721 rpcb_set(RPCPROG_MNT, RPCMNT_VER1, nconf, &servaddr);
722 rpcb_set(RPCPROG_MNT, RPCMNT_VER3, nconf, &servaddr);
723
724 xcreated++;
725 freeaddrinfo(res);
726 }
727 } /* end while */
728}
729
984263bc 730static void
b5744197 731usage(void)
984263bc
MD
732{
733 fprintf(stderr,
ad944d0d 734 "usage: mountd [-2] [-d] [-l] [-n] [-p <port>] [-r] "
2eb4cd83 735 "[-h <bindip>] [export_file ...]\n");
984263bc
MD
736 exit(1);
737}
738
739/*
740 * The mount rpc service
741 */
742void
b5744197 743mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
984263bc
MD
744{
745 struct exportlist *ep;
746 struct dirlist *dp;
747 struct fhreturn fhr;
748 struct stat stb;
749 struct statfs fsb;
ce0e08e2
PA
750 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
751 int lookup_failed = 1;
752 struct sockaddr *saddr;
984263bc
MD
753 u_short sport;
754 char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
755 int bad = 0, defset, hostset;
756 sigset_t sighup_mask;
757
758 sigemptyset(&sighup_mask);
759 sigaddset(&sighup_mask, SIGHUP);
ce1bcb9e 760
ce0e08e2
PA
761 saddr = svc_getrpccaller(transp)->buf;
762 switch (saddr->sa_family) {
763 case AF_INET6:
764 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
765 break;
766 case AF_INET:
767 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
768 break;
769 default:
770 syslog(LOG_ERR, "request from unknown address family");
771 return;
772 }
773 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
774 NULL, 0, 0);
775 getnameinfo(saddr, saddr->sa_len, numerichost,
776 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
984263bc
MD
777 switch (rqstp->rq_proc) {
778 case NULLPROC:
1b0da553 779 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
984263bc
MD
780 syslog(LOG_ERR, "can't send reply");
781 return;
782 case RPCMNT_MOUNT:
783 if (sport >= IPPORT_RESERVED && resvport_only) {
784 syslog(LOG_NOTICE,
785 "mount request from %s from unprivileged port",
ce0e08e2 786 numerichost);
984263bc
MD
787 svcerr_weakauth(transp);
788 return;
789 }
1b0da553 790 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
984263bc 791 syslog(LOG_NOTICE, "undecodable mount request from %s",
ce0e08e2 792 numerichost);
984263bc
MD
793 svcerr_decode(transp);
794 return;
795 }
796
797 /*
798 * Get the real pathname and make sure it is a directory
799 * or a regular file if the -r option was specified
800 * and it exists.
801 */
802 if (realpath(rpcpath, dirpath) == NULL ||
803 stat(dirpath, &stb) < 0 ||
804 (!S_ISDIR(stb.st_mode) &&
ce0e08e2 805 (dir_only || !S_ISREG(stb.st_mode))) ||
984263bc
MD
806 statfs(dirpath, &fsb) < 0) {
807 chdir("/"); /* Just in case realpath doesn't */
808 syslog(LOG_NOTICE,
809 "mount request from %s for non existent path %s",
ce0e08e2 810 numerichost, dirpath);
984263bc
MD
811 if (debug)
812 warnx("stat failed on %s", dirpath);
813 bad = ENOENT; /* We will send error reply later */
814 }
815
816 /* Check in the exports list */
817 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
818 ep = ex_search(&fsb.f_fsid);
819 hostset = defset = 0;
820 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
821 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
ce0e08e2
PA
822 chk_host(dp, saddr, &defset, &hostset)) ||
823 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
824 scan_tree(ep->ex_dirl, saddr) == 0))) {
984263bc 825 if (bad) {
1b0da553
SW
826 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
827 &bad))
984263bc
MD
828 syslog(LOG_ERR, "can't send reply");
829 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
830 return;
831 }
832 if (hostset & DP_HOSTSET)
833 fhr.fhr_flag = hostset;
834 else
835 fhr.fhr_flag = defset;
836 fhr.fhr_vers = rqstp->rq_vers;
837 /* Get the file handle */
838 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
839 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
840 bad = errno;
841 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1b0da553
SW
842 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
843 &bad))
984263bc
MD
844 syslog(LOG_ERR, "can't send reply");
845 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
846 return;
847 }
1b0da553 848 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, &fhr))
984263bc 849 syslog(LOG_ERR, "can't send reply");
ce0e08e2
PA
850 if (!lookup_failed)
851 add_mlist(host, dirpath);
984263bc 852 else
ce0e08e2 853 add_mlist(numerichost, dirpath);
984263bc
MD
854 if (debug)
855 warnx("mount successful");
8c16b86f 856 if (dolog)
984263bc
MD
857 syslog(LOG_NOTICE,
858 "mount request succeeded from %s for %s",
ce0e08e2 859 numerichost, dirpath);
984263bc
MD
860 } else {
861 bad = EACCES;
862 syslog(LOG_NOTICE,
863 "mount request denied from %s for %s",
ce0e08e2 864 numerichost, dirpath);
984263bc
MD
865 }
866
1b0da553 867 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long, &bad))
984263bc
MD
868 syslog(LOG_ERR, "can't send reply");
869 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
870 return;
871 case RPCMNT_DUMP:
1b0da553 872 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, NULL))
984263bc 873 syslog(LOG_ERR, "can't send reply");
8c16b86f 874 else if (dolog)
984263bc
MD
875 syslog(LOG_NOTICE,
876 "dump request succeeded from %s",
ce0e08e2 877 numerichost);
984263bc
MD
878 return;
879 case RPCMNT_UMOUNT:
880 if (sport >= IPPORT_RESERVED && resvport_only) {
881 syslog(LOG_NOTICE,
882 "umount request from %s from unprivileged port",
ce0e08e2 883 numerichost);
984263bc
MD
884 svcerr_weakauth(transp);
885 return;
886 }
1b0da553 887 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
984263bc 888 syslog(LOG_NOTICE, "undecodable umount request from %s",
ce0e08e2 889 numerichost);
984263bc
MD
890 svcerr_decode(transp);
891 return;
892 }
893 if (realpath(rpcpath, dirpath) == NULL) {
894 syslog(LOG_NOTICE, "umount request from %s "
895 "for non existent path %s",
ce0e08e2 896 numerichost, dirpath);
984263bc 897 }
1b0da553 898 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
984263bc 899 syslog(LOG_ERR, "can't send reply");
ce0e08e2
PA
900 if (!lookup_failed)
901 del_mlist(host, dirpath);
902 del_mlist(numerichost, dirpath);
8c16b86f 903 if (dolog)
984263bc
MD
904 syslog(LOG_NOTICE,
905 "umount request succeeded from %s for %s",
ce0e08e2 906 numerichost, dirpath);
984263bc
MD
907 return;
908 case RPCMNT_UMNTALL:
909 if (sport >= IPPORT_RESERVED && resvport_only) {
910 syslog(LOG_NOTICE,
911 "umountall request from %s from unprivileged port",
ce0e08e2 912 numerichost);
984263bc
MD
913 svcerr_weakauth(transp);
914 return;
915 }
1b0da553 916 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
984263bc 917 syslog(LOG_ERR, "can't send reply");
ce0e08e2
PA
918 if (!lookup_failed)
919 del_mlist(host, NULL);
920 del_mlist(numerichost, NULL);
8c16b86f 921 if (dolog)
984263bc
MD
922 syslog(LOG_NOTICE,
923 "umountall request succeeded from %s",
ce0e08e2 924 numerichost);
984263bc
MD
925 return;
926 case RPCMNT_EXPORT:
1b0da553 927 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, NULL))
58206a88
AHJ
928 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief, NULL))
929 syslog(LOG_ERR, "can't send reply");
8c16b86f 930 if (dolog)
984263bc
MD
931 syslog(LOG_NOTICE,
932 "export request succeeded from %s",
ce0e08e2 933 numerichost);
984263bc
MD
934 return;
935 default:
936 svcerr_noproc(transp);
937 return;
938 }
939}
940
941/*
942 * Xdr conversion for a dirpath string
943 */
944int
b5744197 945xdr_dir(XDR *xdrsp, char *dirp)
984263bc
MD
946{
947 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
948}
949
950/*
951 * Xdr routine to generate file handle reply
952 */
953int
b5744197 954xdr_fhs(XDR *xdrsp, caddr_t cp)
984263bc 955{
af62a082 956 struct fhreturn *fhrp = (struct fhreturn *)cp;
d495caca 957 long ok = 0, len, auth;
984263bc
MD
958
959 if (!xdr_long(xdrsp, &ok))
960 return (0);
961 switch (fhrp->fhr_vers) {
962 case 1:
963 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
964 case 3:
965 len = NFSX_V3FH;
966 if (!xdr_long(xdrsp, &len))
967 return (0);
968 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
969 return (0);
ec12fea1 970 auth = RPCAUTH_UNIX;
984263bc
MD
971 len = 1;
972 if (!xdr_long(xdrsp, &len))
973 return (0);
974 return (xdr_long(xdrsp, &auth));
0fdb7d01 975 }
984263bc
MD
976 return (0);
977}
978
979int
ddfadeae 980xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
984263bc
MD
981{
982 struct mountlist *mlp;
983 int true = 1;
984 int false = 0;
985 char *strp;
986
987 mlp = mlhead;
988 while (mlp) {
989 if (!xdr_bool(xdrsp, &true))
990 return (0);
991 strp = &mlp->ml_host[0];
992 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
993 return (0);
994 strp = &mlp->ml_dirp[0];
995 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
996 return (0);
997 mlp = mlp->ml_next;
998 }
999 if (!xdr_bool(xdrsp, &false))
1000 return (0);
1001 return (1);
1002}
1003
1004/*
1005 * Xdr conversion for export list
1006 */
1007int
ddfadeae 1008xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
984263bc
MD
1009{
1010 struct exportlist *ep;
1011 int false = 0;
1012 int putdef;
1013 sigset_t sighup_mask;
1014
1015 sigemptyset(&sighup_mask);
1016 sigaddset(&sighup_mask, SIGHUP);
ce1bcb9e 1017 sigaddset(&sighup_mask, SIGUSR1);
984263bc
MD
1018 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1019 ep = exphead;
1020 while (ep) {
1021 putdef = 0;
58206a88
AHJ
1022 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1023 &putdef, brief))
984263bc
MD
1024 goto errout;
1025 if (ep->ex_defdir && putdef == 0 &&
2038fb68 1026 put_exlist(ep->ex_defdir, xdrsp, NULL,
58206a88 1027 &putdef, brief))
984263bc
MD
1028 goto errout;
1029 ep = ep->ex_next;
1030 }
1031 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1032 if (!xdr_bool(xdrsp, &false))
1033 return (0);
1034 return (1);
1035errout:
1036 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1037 return (0);
1038}
1039
1040/*
1041 * Called from xdr_explist() to traverse the tree and export the
1042 * directory paths.
1043 */
1044int
58206a88 1045put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp, int brief)
984263bc
MD
1046{
1047 struct grouplist *grp;
1048 struct hostlist *hp;
1049 int true = 1;
1050 int false = 0;
1051 int gotalldir = 0;
1052 char *strp;
1053
1054 if (dp) {
58206a88 1055 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
984263bc
MD
1056 return (1);
1057 if (!xdr_bool(xdrsp, &true))
1058 return (1);
1059 strp = dp->dp_dirp;
1060 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
1061 return (1);
1062 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1063 gotalldir = 1;
1064 *putdefp = 1;
1065 }
58206a88
AHJ
1066 if (brief) {
1067 if (!xdr_bool(xdrsp, &true))
1068 return (1);
1069 strp = "(...)";
1070 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
1071 return (1);
1072 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
984263bc
MD
1073 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1074 hp = dp->dp_hosts;
1075 while (hp) {
1076 grp = hp->ht_grp;
1077 if (grp->gr_type == GT_HOST) {
1078 if (!xdr_bool(xdrsp, &true))
1079 return (1);
ce0e08e2 1080 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
984263bc
MD
1081 if (!xdr_string(xdrsp, &strp,
1082 RPCMNT_NAMELEN))
1083 return (1);
1084 } else if (grp->gr_type == GT_NET) {
1085 if (!xdr_bool(xdrsp, &true))
1086 return (1);
1087 strp = grp->gr_ptr.gt_net.nt_name;
1088 if (!xdr_string(xdrsp, &strp,
1089 RPCMNT_NAMELEN))
1090 return (1);
1091 }
1092 hp = hp->ht_next;
2038fb68 1093 if (gotalldir && hp == NULL) {
984263bc
MD
1094 hp = adp->dp_hosts;
1095 gotalldir = 0;
1096 }
1097 }
1098 }
1099 if (!xdr_bool(xdrsp, &false))
1100 return (1);
58206a88 1101 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
984263bc
MD
1102 return (1);
1103 }
1104 return (0);
1105}
1106
58206a88 1107int
57e0ad13 1108xdr_explist(XDR *xdrsp, caddr_t cp)
58206a88
AHJ
1109{
1110
1111 return xdr_explist_common(xdrsp, cp, 0);
1112}
1113
1114int
57e0ad13 1115xdr_explist_brief(XDR *xdrsp, caddr_t cp)
58206a88
AHJ
1116{
1117
1118 return xdr_explist_common(xdrsp, cp, 1);
1119}
1120
c57e7ba3 1121char *line;
ddfadeae 1122size_t linesize;
984263bc
MD
1123FILE *exp_file;
1124
1125/*
356c161f 1126 * Get the export list from one, currently open file
984263bc 1127 */
356c161f
AHJ
1128static void
1129get_exportlist_one(void)
984263bc
MD
1130{
1131 struct exportlist *ep, *ep2;
1132 struct grouplist *grp, *tgrp;
1133 struct exportlist **epp;
1134 struct dirlist *dirhead;
356c161f 1135 struct statfs fsb;
984263bc
MD
1136 struct ucred anon;
1137 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
356c161f 1138 int len, has_host, exflags, got_nondir, dirplen, netgrp;
984263bc 1139
284e3b3b 1140 dirhead = (struct dirlist *)NULL;
984263bc
MD
1141 while (get_line()) {
1142 if (debug)
1143 warnx("got line %s", line);
1144 cp = line;
1145 nextfield(&cp, &endcp);
1146 if (*cp == '#')
1147 goto nextline;
1148
1149 /*
1150 * Set defaults.
1151 */
1152 has_host = FALSE;
1153 anon = def_anon;
1154 exflags = MNT_EXPORTED;
1155 got_nondir = 0;
1156 opt_flags = 0;
2038fb68 1157 ep = NULL;
984263bc
MD
1158
1159 /*
1160 * Create new exports list entry
1161 */
1162 len = endcp-cp;
1163 tgrp = grp = get_grp();
1164 while (len > 0) {
1165 if (len > RPCMNT_NAMELEN) {
1166 getexp_err(ep, tgrp);
1167 goto nextline;
1168 }
1169 if (*cp == '-') {
2038fb68 1170 if (ep == NULL) {
984263bc
MD
1171 getexp_err(ep, tgrp);
1172 goto nextline;
1173 }
1174 if (debug)
1175 warnx("doing opt %s", cp);
1176 got_nondir = 1;
1177 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1178 &exflags, &anon)) {
1179 getexp_err(ep, tgrp);
1180 goto nextline;
1181 }
1182 } else if (*cp == '/') {
1183 savedc = *endcp;
1184 *endcp = '\0';
1185 if (check_dirpath(cp) &&
1186 statfs(cp, &fsb) >= 0) {
e2950f41
TK
1187 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1188 syslog(LOG_ERR, "Warning: exporting of "
1189 "automounted fs %s not supported", cp);
984263bc
MD
1190 if (got_nondir) {
1191 syslog(LOG_ERR, "dirs must be first");
1192 getexp_err(ep, tgrp);
1193 goto nextline;
1194 }
1195 if (ep) {
1196 if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
1197 ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
1198 getexp_err(ep, tgrp);
1199 goto nextline;
1200 }
1201 } else {
1202 /*
1203 * See if this directory is already
1204 * in the list.
1205 */
1206 ep = ex_search(&fsb.f_fsid);
2038fb68 1207 if (ep == NULL) {
984263bc
MD
1208 ep = get_exp();
1209 ep->ex_fs = fsb.f_fsid;
284e3b3b
AL
1210 ep->ex_fsdir = strdup(fsb.f_mntonname);
1211 if (ep->ex_fsdir == NULL)
984263bc 1212 out_of_mem();
ce1bcb9e
MD
1213
1214 delete_export(&fsb);
1215
984263bc 1216 if (debug)
ce0e08e2
PA
1217 warnx("making new ep fs=0x%x,0x%x",
1218 fsb.f_fsid.val[0],
1219 fsb.f_fsid.val[1]);
984263bc
MD
1220 } else if (debug)
1221 warnx("found ep fs=0x%x,0x%x",
1222 fsb.f_fsid.val[0],
1223 fsb.f_fsid.val[1]);
1224 }
1225
1226 /*
1227 * Add dirpath to export mount point.
1228 */
1229 dirp = add_expdir(&dirhead, cp, len);
1230 dirplen = len;
1231 } else {
1232 getexp_err(ep, tgrp);
1233 goto nextline;
1234 }
1235 *endcp = savedc;
1236 } else {
1237 savedc = *endcp;
1238 *endcp = '\0';
1239 got_nondir = 1;
2038fb68 1240 if (ep == NULL) {
984263bc
MD
1241 getexp_err(ep, tgrp);
1242 goto nextline;
1243 }
1244
1245 /*
1246 * Get the host or netgroup.
1247 */
1248 setnetgrent(cp);
1249 netgrp = getnetgrent(&hst, &usr, &dom);
1250 do {
1251 if (has_host) {
1252 grp->gr_next = get_grp();
1253 grp = grp->gr_next;
1254 }
1255 if (netgrp) {
678e8cc6 1256 if (hst == NULL) {
984263bc
MD
1257 syslog(LOG_ERR,
1258 "null hostname in netgroup %s, skipping", cp);
1259 grp->gr_type = GT_IGNORE;
1260 } else if (get_host(hst, grp, tgrp)) {
1261 syslog(LOG_ERR,
1262 "bad host %s in netgroup %s, skipping", hst, cp);
1263 grp->gr_type = GT_IGNORE;
1264 }
1265 } else if (get_host(cp, grp, tgrp)) {
1266 syslog(LOG_ERR, "bad host %s, skipping", cp);
1267 grp->gr_type = GT_IGNORE;
1268 }
1269 has_host = TRUE;
1270 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1271 endnetgrent();
1272 *endcp = savedc;
1273 }
1274 cp = endcp;
1275 nextfield(&cp, &endcp);
1276 len = endcp - cp;
1277 }
1278 if (check_options(dirhead)) {
1279 getexp_err(ep, tgrp);
1280 goto nextline;
1281 }
1282 if (!has_host) {
ce0e08e2 1283 grp->gr_type = GT_DEFAULT;
984263bc
MD
1284 if (debug)
1285 warnx("adding a default entry");
984263bc
MD
1286
1287 /*
1288 * Don't allow a network export coincide with a list of
1289 * host(s) on the same line.
1290 */
1291 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
0e0ae4e8 1292 syslog(LOG_ERR, "network/host conflict");
984263bc
MD
1293 getexp_err(ep, tgrp);
1294 goto nextline;
1295
ce0e08e2
PA
1296 /*
1297 * If an export list was specified on this line, make sure
984263bc
MD
1298 * that we have at least one valid entry, otherwise skip it.
1299 */
1300 } else {
1301 grp = tgrp;
ce0e08e2 1302 while (grp && grp->gr_type == GT_IGNORE)
984263bc
MD
1303 grp = grp->gr_next;
1304 if (! grp) {
1305 getexp_err(ep, tgrp);
1306 goto nextline;
1307 }
1308 }
1309
1310 /*
1311 * Loop through hosts, pushing the exports into the kernel.
1312 * After loop, tgrp points to the start of the list and
1313 * grp points to the last entry in the list.
1314 */
1315 grp = tgrp;
1316 do {
ce0e08e2
PA
1317 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1318 &fsb)) {
1319 getexp_err(ep, tgrp);
1320 goto nextline;
1321 }
984263bc
MD
1322 } while (grp->gr_next && (grp = grp->gr_next));
1323
1324 /*
1325 * Success. Update the data structures.
1326 */
1327 if (has_host) {
1328 hang_dirp(dirhead, tgrp, ep, opt_flags);
1329 grp->gr_next = grphead;
1330 grphead = tgrp;
1331 } else {
2038fb68 1332 hang_dirp(dirhead, NULL, ep,
984263bc
MD
1333 opt_flags);
1334 free_grp(grp);
1335 }
2038fb68 1336 dirhead = NULL;
984263bc
MD
1337 if ((ep->ex_flag & EX_LINKED) == 0) {
1338 ep2 = exphead;
1339 epp = &exphead;
1340
1341 /*
1342 * Insert in the list in alphabetical order.
1343 */
1344 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1345 epp = &ep2->ex_next;
1346 ep2 = ep2->ex_next;
1347 }
1348 if (ep2)
1349 ep->ex_next = ep2;
1350 *epp = ep;
1351 ep->ex_flag |= EX_LINKED;
1352 }
1353nextline:
1354 if (dirhead) {
1355 free_dir(dirhead);
2038fb68 1356 dirhead = NULL;
984263bc
MD
1357 }
1358 }
1359 fclose(exp_file);
1360}
1361
356c161f 1362/*
ce1bcb9e
MD
1363 * Get the export list from all specified files.
1364 *
1365 * If incremental is non-zero we were signalled by mount and we do not
1366 * call mountctl() to blow away existing exports.
356c161f
AHJ
1367 */
1368void
ce1bcb9e 1369get_exportlist(int incremental)
356c161f
AHJ
1370{
1371 struct exportlist *ep, *ep2;
1372 struct grouplist *grp, *tgrp;
1373 struct statfs *fsp, *mntbufp;
1374 struct vfsconf vfc;
1d56e9e9 1375 int num, i;
4d7e492b 1376 int done;
356c161f 1377
356c161f
AHJ
1378 /*
1379 * First, get rid of the old list
1380 */
1381 ep = exphead;
1382 while (ep) {
1383 ep2 = ep;
1384 ep = ep->ex_next;
1385 free_exp(ep2);
1386 }
1387 exphead = NULL;
1388
1389 grp = grphead;
1390 while (grp) {
1391 tgrp = grp;
1392 grp = grp->gr_next;
1393 free_grp(tgrp);
1394 }
1395 grphead = NULL;
1396
1397 /*
1398 * And delete exports that are in the kernel for all local
1399 * filesystems.
1400 * XXX: Should know how to handle all local exportable filesystems.
1401 */
1402 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1403 for (i = 0; i < num; i++) {
356c161f
AHJ
1404 fsp = &mntbufp[i];
1405 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1406 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1407 fsp->f_fstypename);
1408 continue;
1409 }
1410
1411 /*
1412 * Do not delete export for network filesystem by
1413 * passing "export" arg to mount().
1414 * It only makes sense to do this for local filesystems.
1415 */
1416 if (vfc.vfc_flags & VFCF_NETWORK)
1417 continue;
1418
ce1bcb9e
MD
1419 if (incremental == 0)
1420 delete_export(fsp);
356c161f
AHJ
1421 }
1422
1423 /*
1424 * Read in the exports file and build the list, calling
1425 * nmount() as we go along to push the export rules into the kernel.
1426 */
4d7e492b 1427 done = 0;
356c161f
AHJ
1428 for (i = 0; exnames[i] != NULL; i++) {
1429 if (debug)
1430 warnx("reading exports from %s", exnames[i]);
1431 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
4d7e492b
AHJ
1432 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1433 continue;
356c161f
AHJ
1434 }
1435 get_exportlist_one();
1436 fclose(exp_file);
4d7e492b
AHJ
1437 done++;
1438 }
1439 if (done == 0) {
1440 syslog(LOG_ERR, "can't open any exports file");
1441 exit(2);
356c161f
AHJ
1442 }
1443}
1444
984263bc
MD
1445/*
1446 * Allocate an export list element
1447 */
1448struct exportlist *
b5744197 1449get_exp(void)
984263bc
MD
1450{
1451 struct exportlist *ep;
1452
1453 ep = (struct exportlist *)malloc(sizeof (struct exportlist));
2038fb68 1454 if (ep == NULL)
984263bc
MD
1455 out_of_mem();
1456 memset(ep, 0, sizeof(struct exportlist));
1457 return (ep);
1458}
1459
1460/*
1461 * Allocate a group list element
1462 */
1463struct grouplist *
b5744197 1464get_grp(void)
984263bc
MD
1465{
1466 struct grouplist *gp;
1467
1468 gp = (struct grouplist *)malloc(sizeof (struct grouplist));
2038fb68 1469 if (gp == NULL)
984263bc
MD
1470 out_of_mem();
1471 memset(gp, 0, sizeof(struct grouplist));
1472 return (gp);
1473}
1474
1475/*
1476 * Clean up upon an error in get_exportlist().
1477 */
1478void
b5744197 1479getexp_err(struct exportlist *ep, struct grouplist *grp)
984263bc
MD
1480{
1481 struct grouplist *tgrp;
1482
1483 if (!(opt_flags & OP_QUIET))
1484 syslog(LOG_ERR, "bad exports list line %s", line);
1485 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1486 free_exp(ep);
1487 while (grp) {
1488 tgrp = grp;
1489 grp = grp->gr_next;
1490 free_grp(tgrp);
1491 }
1492}
1493
1494/*
1495 * Search the export list for a matching fs.
1496 */
1497struct exportlist *
b5744197 1498ex_search(fsid_t *fsid)
984263bc
MD
1499{
1500 struct exportlist *ep;
1501
1502 ep = exphead;
1503 while (ep) {
1504 if (ep->ex_fs.val[0] == fsid->val[0] &&
1505 ep->ex_fs.val[1] == fsid->val[1])
1506 return (ep);
1507 ep = ep->ex_next;
1508 }
1509 return (ep);
1510}
1511
1512/*
1513 * Add a directory path to the list.
1514 */
1515char *
b5744197 1516add_expdir(struct dirlist **dpp, char *cp, int len)
984263bc
MD
1517{
1518 struct dirlist *dp;
1519
284e3b3b
AL
1520 dp = malloc(sizeof (struct dirlist));
1521 if (dp == (struct dirlist *)NULL)
984263bc
MD
1522 out_of_mem();
1523 dp->dp_left = *dpp;
284e3b3b 1524 dp->dp_right = (struct dirlist *)NULL;
984263bc 1525 dp->dp_flag = 0;
284e3b3b
AL
1526 dp->dp_hosts = (struct hostlist *)NULL;
1527 dp->dp_dirp = strndup(cp, len);
1528 if (dp->dp_dirp == NULL)
1529 out_of_mem();
984263bc
MD
1530 *dpp = dp;
1531 return (dp->dp_dirp);
1532}
1533
1534/*
1535 * Hang the dir list element off the dirpath binary tree as required
1536 * and update the entry for host.
1537 */
1538void
b5744197
HP
1539hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1540 int flags)
984263bc
MD
1541{
1542 struct hostlist *hp;
1543 struct dirlist *dp2;
1544
1545 if (flags & OP_ALLDIRS) {
1546 if (ep->ex_defdir)
1547 free((caddr_t)dp);
1548 else
1549 ep->ex_defdir = dp;
2038fb68 1550 if (grp == NULL) {
984263bc 1551 ep->ex_defdir->dp_flag |= DP_DEFSET;
984263bc
MD
1552 } else while (grp) {
1553 hp = get_ht();
984263bc
MD
1554 hp->ht_grp = grp;
1555 hp->ht_next = ep->ex_defdir->dp_hosts;
1556 ep->ex_defdir->dp_hosts = hp;
1557 grp = grp->gr_next;
1558 }
1559 } else {
1560
1561 /*
1562 * Loop through the directories adding them to the tree.
1563 */
1564 while (dp) {
1565 dp2 = dp->dp_left;
1566 add_dlist(&ep->ex_dirl, dp, grp, flags);
1567 dp = dp2;
1568 }
1569 }
1570}
1571
1572/*
1573 * Traverse the binary tree either updating a node that is already there
1574 * for the new directory or adding the new node.
1575 */
1576void
b5744197
HP
1577add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
1578 int flags)
984263bc
MD
1579{
1580 struct dirlist *dp;
1581 struct hostlist *hp;
1582 int cmp;
1583
1584 dp = *dpp;
1585 if (dp) {
1586 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1587 if (cmp > 0) {
1588 add_dlist(&dp->dp_left, newdp, grp, flags);
1589 return;
1590 } else if (cmp < 0) {
1591 add_dlist(&dp->dp_right, newdp, grp, flags);
1592 return;
1593 } else
1594 free((caddr_t)newdp);
1595 } else {
1596 dp = newdp;
2038fb68 1597 dp->dp_left = NULL;
984263bc
MD
1598 *dpp = dp;
1599 }
1600 if (grp) {
1601
1602 /*
1603 * Hang all of the host(s) off of the directory point.
1604 */
1605 do {
1606 hp = get_ht();
984263bc
MD
1607 hp->ht_grp = grp;
1608 hp->ht_next = dp->dp_hosts;
1609 dp->dp_hosts = hp;
1610 grp = grp->gr_next;
1611 } while (grp);
1612 } else {
1613 dp->dp_flag |= DP_DEFSET;
984263bc
MD
1614 }
1615}
1616
1617/*
1618 * Search for a dirpath on the export point.
1619 */
1620struct dirlist *
ce0e08e2 1621dirp_search(struct dirlist *dp, char *dirp)
984263bc
MD
1622{
1623 int cmp;
1624
1625 if (dp) {
ce0e08e2 1626 cmp = strcmp(dp->dp_dirp, dirp);
984263bc 1627 if (cmp > 0)
ce0e08e2 1628 return (dirp_search(dp->dp_left, dirp));
984263bc 1629 else if (cmp < 0)
ce0e08e2 1630 return (dirp_search(dp->dp_right, dirp));
984263bc
MD
1631 else
1632 return (dp);
1633 }
1634 return (dp);
1635}
1636
1637/*
1638 * Scan for a host match in a directory tree.
1639 */
1640int
ce0e08e2
PA
1641chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
1642 int *hostsetp)
984263bc
MD
1643{
1644 struct hostlist *hp;
1645 struct grouplist *grp;
ce0e08e2 1646 struct addrinfo *ai;
984263bc
MD
1647
1648 if (dp) {
1649 if (dp->dp_flag & DP_DEFSET)
1650 *defsetp = dp->dp_flag;
1651 hp = dp->dp_hosts;
1652 while (hp) {
1653 grp = hp->ht_grp;
1654 switch (grp->gr_type) {
1655 case GT_HOST:
ce0e08e2
PA
1656 ai = grp->gr_ptr.gt_addrinfo;
1657 for (; ai; ai = ai->ai_next) {
0e0ae4e8 1658 if (!sacmp(ai->ai_addr, saddr, NULL)) {
ce0e08e2
PA
1659 *hostsetp =
1660 (hp->ht_flag | DP_HOSTSET);
1661 return (1);
1662 }
984263bc 1663 }
0e0ae4e8 1664 break;
984263bc 1665 case GT_NET:
0e0ae4e8
AHJ
1666 if (!sacmp(saddr, (struct sockaddr *)
1667 &grp->gr_ptr.gt_net.nt_net,
1668 (struct sockaddr *)
1669 &grp->gr_ptr.gt_net.nt_mask)) {
ce0e08e2
PA
1670 *hostsetp = (hp->ht_flag | DP_HOSTSET);
1671 return (1);
1672 }
0e0ae4e8 1673 break;
0fdb7d01 1674 }
984263bc
MD
1675 hp = hp->ht_next;
1676 }
1677 }
1678 return (0);
1679}
1680
1681/*
1682 * Scan tree for a host that matches the address.
1683 */
1684int
ce0e08e2 1685scan_tree(struct dirlist *dp, struct sockaddr *saddr)
984263bc
MD
1686{
1687 int defset, hostset;
1688
1689 if (dp) {
1690 if (scan_tree(dp->dp_left, saddr))
1691 return (1);
1692 if (chk_host(dp, saddr, &defset, &hostset))
1693 return (1);
1694 if (scan_tree(dp->dp_right, saddr))
1695 return (1);
1696 }
1697 return (0);
1698}
1699
1700/*
1701 * Traverse the dirlist tree and free it up.
1702 */
1703void
b5744197 1704free_dir(struct dirlist *dp)
984263bc
MD
1705{
1706
1707 if (dp) {
1708 free_dir(dp->dp_left);
1709 free_dir(dp->dp_right);
1710 free_host(dp->dp_hosts);
284e3b3b
AL
1711 free(dp->dp_dirp);
1712 free(dp);
984263bc
MD
1713 }
1714}
1715
1716/*
1717 * Parse the option string and update fields.
1718 * Option arguments may either be -<option>=<value> or
1719 * -<option> <value>
1720 */
1721int
b5744197
HP
1722do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
1723 int *has_hostp, int *exflagsp, struct ucred *cr)
984263bc
MD
1724{
1725 char *cpoptarg, *cpoptend;
1726 char *cp, *endcp, *cpopt, savedc, savedc2;
1727 int allflag, usedarg;
1728
1729 savedc2 = '\0';
1730 cpopt = *cpp;
1731 cpopt++;
1732 cp = *endcpp;
1733 savedc = *cp;
1734 *cp = '\0';
1735 while (cpopt && *cpopt) {
1736 allflag = 1;
1737 usedarg = -2;
1738 if ((cpoptend = strchr(cpopt, ','))) {
1739 *cpoptend++ = '\0';
1740 if ((cpoptarg = strchr(cpopt, '=')))
1741 *cpoptarg++ = '\0';
1742 } else {
1743 if ((cpoptarg = strchr(cpopt, '=')))
1744 *cpoptarg++ = '\0';
1745 else {
1746 *cp = savedc;
1747 nextfield(&cp, &endcp);
1748 **endcpp = '\0';
1749 if (endcp > cp && *cp != '-') {
1750 cpoptarg = cp;
1751 savedc2 = *endcp;
1752 *endcp = '\0';
1753 usedarg = 0;
1754 }
1755 }
1756 }
1757 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1758 *exflagsp |= MNT_EXRDONLY;
1759 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1760 !(allflag = strcmp(cpopt, "mapall")) ||
1761 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1762 usedarg++;
1763 parsecred(cpoptarg, cr);
1764 if (allflag == 0) {
1765 *exflagsp |= MNT_EXPORTANON;
1766 opt_flags |= OP_MAPALL;
1767 } else
1768 opt_flags |= OP_MAPROOT;
984263bc
MD
1769 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1770 !strcmp(cpopt, "m"))) {
1771 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1772 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
1773 return (1);
1774 }
1775 usedarg++;
1776 opt_flags |= OP_MASK;
1777 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1778 !strcmp(cpopt, "n"))) {
ce0e08e2
PA
1779 if (strchr(cpoptarg, '/') != NULL) {
1780 if (debug)
1781 fprintf(stderr, "setting OP_MASKLEN\n");
1782 opt_flags |= OP_MASKLEN;
1783 }
984263bc
MD
1784 if (grp->gr_type != GT_NULL) {
1785 syslog(LOG_ERR, "network/host conflict");
1786 return (1);
1787 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1788 syslog(LOG_ERR, "bad net: %s", cpoptarg);
1789 return (1);
1790 }
1791 grp->gr_type = GT_NET;
1792 *has_hostp = 1;
1793 usedarg++;
1794 opt_flags |= OP_NET;
1795 } else if (!strcmp(cpopt, "alldirs")) {
1796 opt_flags |= OP_ALLDIRS;
1797 } else if (!strcmp(cpopt, "public")) {
1798 *exflagsp |= MNT_EXPUBLIC;
1799 } else if (!strcmp(cpopt, "webnfs")) {
1800 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
1801 opt_flags |= OP_MAPALL;
1802 } else if (cpoptarg && !strcmp(cpopt, "index")) {
1803 ep->ex_indexfile = strdup(cpoptarg);
1804 } else if (!strcmp(cpopt, "quiet")) {
1805 opt_flags |= OP_QUIET;
1806 } else {
1807 syslog(LOG_ERR, "bad opt %s", cpopt);
1808 return (1);
1809 }
1810 if (usedarg >= 0) {
1811 *endcp = savedc2;
1812 **endcpp = savedc;
1813 if (usedarg > 0) {
1814 *cpp = cp;
1815 *endcpp = endcp;
1816 }
1817 return (0);
1818 }
1819 cpopt = cpoptend;
1820 }
1821 **endcpp = savedc;
1822 return (0);
1823}
1824
1825/*
1826 * Translate a character string to the corresponding list of network
1827 * addresses for a hostname.
1828 */
1829int
b5744197 1830get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
984263bc
MD
1831{
1832 struct grouplist *checkgrp;
ce0e08e2
PA
1833 struct addrinfo *ai, *tai, hints;
1834 int ecode;
1835 char host[NI_MAXHOST];
984263bc 1836
ce0e08e2
PA
1837 if (grp->gr_type != GT_NULL) {
1838 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
984263bc 1839 return (1);
ce0e08e2
PA
1840 }
1841 memset(&hints, 0, sizeof hints);
1842 hints.ai_flags = AI_CANONNAME;
1843 hints.ai_protocol = IPPROTO_UDP;
1844 ecode = getaddrinfo(cp, NULL, &hints, &ai);
1845 if (ecode != 0) {
1846 syslog(LOG_ERR,"can't get address info for host %s", cp);
1847 return 1;
1848 }
1849 grp->gr_ptr.gt_addrinfo = ai;
1850 while (ai != NULL) {
1851 if (ai->ai_canonname == NULL) {
1852 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
77e4907f 1853 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
ce0e08e2
PA
1854 strlcpy(host, "?", sizeof(host));
1855 ai->ai_canonname = strdup(host);
1856 ai->ai_flags |= AI_CANONNAME;
1857 }
1858 if (debug)
1859 fprintf(stderr, "got host %s\n", ai->ai_canonname);
1860 /*
1861 * Sanity check: make sure we don't already have an entry
1862 * for this host in the grouplist.
1863 */
1864 for (checkgrp = tgrp; checkgrp != NULL;
1865 checkgrp = checkgrp->gr_next) {
1866 if (checkgrp->gr_type != GT_HOST)
1867 continue;
1868 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
1869 tai = tai->ai_next) {
0e0ae4e8 1870 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
ce0e08e2
PA
1871 continue;
1872 if (debug)
1873 fprintf(stderr,
1874 "ignoring duplicate host %s\n",
1875 ai->ai_canonname);
1876 grp->gr_type = GT_IGNORE;
1877 return (0);
984263bc 1878 }
984263bc 1879 }
ce0e08e2 1880 ai = ai->ai_next;
984263bc 1881 }
984263bc 1882 grp->gr_type = GT_HOST;
984263bc
MD
1883 return (0);
1884}
1885
1886/*
1887 * Free up an exports list component
1888 */
1889void
b5744197 1890free_exp(struct exportlist *ep)
984263bc
MD
1891{
1892
1893 if (ep->ex_defdir) {
1894 free_host(ep->ex_defdir->dp_hosts);
1895 free((caddr_t)ep->ex_defdir);
1896 }
1897 if (ep->ex_fsdir)
1898 free(ep->ex_fsdir);
1899 if (ep->ex_indexfile)
1900 free(ep->ex_indexfile);
1901 free_dir(ep->ex_dirl);
1902 free((caddr_t)ep);
1903}
1904
1905/*
1906 * Free hosts.
1907 */
1908void
b5744197 1909free_host(struct hostlist *hp)
984263bc
MD
1910{
1911 struct hostlist *hp2;
1912
1913 while (hp) {
1914 hp2 = hp;
1915 hp = hp->ht_next;
1916 free((caddr_t)hp2);
1917 }
1918}
1919
1920struct hostlist *
b5744197 1921get_ht(void)
984263bc
MD
1922{
1923 struct hostlist *hp;
1924
1925 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2038fb68 1926 if (hp == NULL)
984263bc 1927 out_of_mem();
2038fb68 1928 hp->ht_next = NULL;
984263bc
MD
1929 hp->ht_flag = 0;
1930 return (hp);
1931}
1932
1933/*
1934 * Out of memory, fatal
1935 */
1936void
b5744197 1937out_of_mem(void)
984263bc
MD
1938{
1939
1940 syslog(LOG_ERR, "out of memory");
1941 exit(2);
1942}
1943
1944/*
1945 * Do the mount syscall with the update flag to push the export info into
1946 * the kernel.
1947 */
1948int
b5744197
HP
1949do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
1950 struct ucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
984263bc
MD
1951{
1952 struct statfs fsb1;
ce0e08e2 1953 struct addrinfo *ai;
0e0ae4e8 1954 struct export_args *eap;
984263bc 1955 char *cp = NULL;
984263bc
MD
1956 int done;
1957 char savedc = '\0';
984263bc
MD
1958 union {
1959 struct ufs_args ua;
1960 struct iso_args ia;
1961 struct mfs_args ma;
984263bc 1962 struct msdosfs_args da;
984263bc
MD
1963 struct ntfs_args na;
1964 } args;
984263bc 1965
0e0ae4e8
AHJ
1966 bzero(&args, sizeof args);
1967 /* XXX, we assume that all xx_args look like ufs_args. */
984263bc 1968 args.ua.fspec = 0;
0e0ae4e8
AHJ
1969 eap = &args.ua.export;
1970
1971 eap->ex_flags = exflags;
1972 eap->ex_anon = *anoncrp;
1973 eap->ex_indexfile = ep->ex_indexfile;
984263bc 1974 if (grp->gr_type == GT_HOST)
ce0e08e2 1975 ai = grp->gr_ptr.gt_addrinfo;
984263bc 1976 else
ce0e08e2 1977 ai = NULL;
984263bc
MD
1978 done = FALSE;
1979 while (!done) {
1980 switch (grp->gr_type) {
1981 case GT_HOST:
ce0e08e2
PA
1982 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
1983 goto skip;
0e0ae4e8
AHJ
1984 eap->ex_addr = ai->ai_addr;
1985 eap->ex_addrlen = ai->ai_addrlen;
1986 eap->ex_masklen = 0;
984263bc
MD
1987 break;
1988 case GT_NET:
0e0ae4e8 1989 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
ce0e08e2
PA
1990 have_v6 == 0)
1991 goto skip;
0e0ae4e8
AHJ
1992 eap->ex_addr =
1993 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
1994 eap->ex_addrlen = args.ua.export.ex_addr->sa_len;
1995 eap->ex_mask =
1996 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
1997 eap->ex_masklen = args.ua.export.ex_mask->sa_len;
ce0e08e2
PA
1998 break;
1999 case GT_DEFAULT:
0e0ae4e8
AHJ
2000 eap->ex_addr = NULL;
2001 eap->ex_addrlen = 0;
2002 eap->ex_mask = NULL;
2003 eap->ex_masklen = 0;
984263bc
MD
2004 break;
2005 case GT_IGNORE:
2006 return(0);
2007 break;
2008 default:
2009 syslog(LOG_ERR, "bad grouptype");
2010 if (cp)
2011 *cp = savedc;
2012 return (1);
0fdb7d01 2013 }
984263bc
MD
2014
2015 /*
2016 * XXX:
2017 * Maybe I should just use the fsb->f_mntonname path instead
2018 * of looping back up the dirp to the mount point??
2019 * Also, needs to know how to export all types of local
b211bc37 2020 * exportable filesystems and not just "ufs".
984263bc 2021 */
1aa89f17
MD
2022 for (;;) {
2023 int r;
2024
2025 r = mountctl(fsb->f_mntonname, MOUNTCTL_SET_EXPORT,
2026 -1,
2027 &args.ua.export, sizeof(args.ua.export),
2028 NULL, 0);
2029 if (r < 0 && errno == EOPNOTSUPP) {
2030 r = mount(fsb->f_fstypename, dirp,
2031 fsb->f_flags | MNT_UPDATE,
2032 (caddr_t)&args);
2033 }
2034 if (r >= 0)
2035 break;
984263bc
MD
2036 if (cp)
2037 *cp-- = savedc;
2038 else
2039 cp = dirp + dirplen - 1;
2040 if (opt_flags & OP_QUIET)
2041 return (1);
2042 if (errno == EPERM) {
ce0e08e2
PA
2043 if (debug)
2044 warnx("can't change attributes for %s",
2045 dirp);
984263bc
MD
2046 syslog(LOG_ERR,
2047 "can't change attributes for %s", dirp);
2048 return (1);
2049 }
2050 if (opt_flags & OP_ALLDIRS) {
2051 if (errno == EINVAL)
2052 syslog(LOG_ERR,
2053 "-alldirs requested but %s is not a filesystem mountpoint",
2054 dirp);
2055 else
2056 syslog(LOG_ERR,
2057 "could not remount %s: %m",
2058 dirp);
2059 return (1);
2060 }
2061 /* back up over the last component */
2062 while (*cp == '/' && cp > dirp)
2063 cp--;
2064 while (*(cp - 1) != '/' && cp > dirp)
2065 cp--;
2066 if (cp == dirp) {
2067 if (debug)
2068 warnx("mnt unsucc");
2069 syslog(LOG_ERR, "can't export %s", dirp);
2070 return (1);
2071 }
2072 savedc = *cp;
2073 *cp = '\0';
2074 /* Check that we're still on the same filesystem. */
2075 if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
2076 &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
2077 *cp = savedc;
2078 syslog(LOG_ERR, "can't export %s", dirp);
2079 return (1);
2080 }
2081 }
ce0e08e2
PA
2082skip:
2083 if (ai != NULL)
2084 ai = ai->ai_next;
2085 if (ai == NULL)
984263bc
MD
2086 done = TRUE;
2087 }
2088 if (cp)
2089 *cp = savedc;
2090 return (0);
2091}
2092
2093/*
2094 * Translate a net address.
0e0ae4e8
AHJ
2095 *
2096 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
984263bc
MD
2097 */
2098int
b5744197 2099get_net(char *cp, struct netmsk *net, int maskflg)
984263bc 2100{
1ddefeb5 2101 struct netent *np = NULL;
ce0e08e2 2102 char *name, *p, *prefp;
0e0ae4e8 2103 struct sockaddr_in sin;
1ddefeb5 2104 struct sockaddr *sa = NULL;
ce0e08e2
PA
2105 struct addrinfo hints, *ai = NULL;
2106 char netname[NI_MAXHOST];
2107 long preflen;
ce0e08e2
PA
2108
2109 p = prefp = NULL;
2110 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2111 p = strchr(cp, '/');
2112 *p = '\0';
2113 prefp = p + 1;
2114 }
984263bc 2115
1ddefeb5
AHJ
2116 /*
2117 * Check for a numeric address first. We wish to avoid
2118 * possible DNS lookups in getnetbyname().
2119 */
2120 if (isxdigit(*cp) || *cp == ':') {
ce0e08e2 2121 memset(&hints, 0, sizeof hints);
0e0ae4e8
AHJ
2122 /* Ensure the mask and the network have the same family. */
2123 if (maskflg && (opt_flags & OP_NET))
2124 hints.ai_family = net->nt_net.ss_family;
2125 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2126 hints.ai_family = net->nt_mask.ss_family;
2127 else
2128 hints.ai_family = AF_UNSPEC;
ce0e08e2 2129 hints.ai_flags = AI_NUMERICHOST;
1ddefeb5
AHJ
2130 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2131 sa = ai->ai_addr;
2132 if (sa != NULL && ai->ai_family == AF_INET) {
ce0e08e2 2133 /*
0e0ae4e8
AHJ
2134 * The address in `cp' is really a network address, so
2135 * use inet_network() to re-interpret this correctly.
2136 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
ce0e08e2 2137 */
0e0ae4e8 2138 bzero(&sin, sizeof sin);
ce0e08e2
PA
2139 sin.sin_family = AF_INET;
2140 sin.sin_len = sizeof sin;
0e0ae4e8 2141 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
ce0e08e2 2142 if (debug)
0e0ae4e8
AHJ
2143 fprintf(stderr, "get_net: v4 addr %s\n",
2144 inet_ntoa(sin.sin_addr));
ce0e08e2 2145 sa = (struct sockaddr *)&sin;
1ddefeb5
AHJ
2146 }
2147 }
2148 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2149 bzero(&sin, sizeof sin);
2150 sin.sin_family = AF_INET;
2151 sin.sin_len = sizeof sin;
2152 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2153 sa = (struct sockaddr *)&sin;
2154 }
2155 if (sa == NULL)
ce0e08e2
PA
2156 goto fail;
2157
0e0ae4e8
AHJ
2158 if (maskflg) {
2159 /* The specified sockaddr is a mask. */
2160 if (checkmask(sa) != 0)
2161 goto fail;
2162 bcopy(sa, &net->nt_mask, sa->sa_len);
2163 opt_flags |= OP_HAVEMASK;
2164 } else {
2165 /* The specified sockaddr is a network address. */
2166 bcopy(sa, &net->nt_net, sa->sa_len);
984263bc 2167
0e0ae4e8
AHJ
2168 /* Get a network name for the export list. */
2169 if (np) {
2170 name = np->n_name;
2171 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
77e4907f 2172 NULL, 0, NI_NUMERICHOST) == 0) {
0e0ae4e8
AHJ
2173 name = netname;
2174 } else {
2175 goto fail;
2176 }
2177 if ((net->nt_name = strdup(name)) == NULL)
2178 out_of_mem();
2179
2180 /*
2181 * Extract a mask from either a "/<masklen>" suffix, or
2182 * from the class of an IPv4 address.
2183 */
ce0e08e2
PA
2184 if (opt_flags & OP_MASKLEN) {
2185 preflen = strtol(prefp, NULL, 10);
0e0ae4e8
AHJ
2186 if (preflen < 0L || preflen == LONG_MAX)
2187 goto fail;
2188 bcopy(sa, &net->nt_mask, sa->sa_len);
2189 if (makemask(&net->nt_mask, (int)preflen) != 0)
ce0e08e2 2190 goto fail;
0e0ae4e8 2191 opt_flags |= OP_HAVEMASK;
ce0e08e2 2192 *p = '/';
0e0ae4e8
AHJ
2193 } else if (sa->sa_family == AF_INET &&
2194 (opt_flags & OP_MASK) == 0) {
2195 in_addr_t addr;
2196
2197 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2198 if (IN_CLASSA(addr))
2199 preflen = 8;
2200 else if (IN_CLASSB(addr))
2201 preflen = 16;
2202 else if (IN_CLASSC(addr))
2203 preflen = 24;
2204 else if (IN_CLASSD(addr))
2205 preflen = 28;
2206 else
2207 preflen = 32; /* XXX */
ce0e08e2 2208
0e0ae4e8
AHJ
2209 bcopy(sa, &net->nt_mask, sa->sa_len);
2210 makemask(&net->nt_mask, (int)preflen);
2211 opt_flags |= OP_HAVEMASK;
ce0e08e2 2212 }
984263bc 2213 }
ce0e08e2
PA
2214
2215 if (ai)
2216 freeaddrinfo(ai);
2217 return 0;
2218
2219fail:
2220 if (ai)
2221 freeaddrinfo(ai);
2222 return 1;
984263bc
MD
2223}
2224
2225/*
2226 * Parse out the next white space separated field
2227 */
2228void
b5744197 2229nextfield(char **cp, char **endcp)
984263bc
MD
2230{
2231 char *p;
2232
2233 p = *cp;
2234 while (*p == ' ' || *p == '\t')
2235 p++;
2236 if (*p == '\n' || *p == '\0')
2237 *cp = *endcp = p;
2238 else {
2239 *cp = p++;
2240 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2241 p++;
2242 *endcp = p;
2243 }
2244}
2245
2246/*
2247 * Get an exports file line. Skip over blank lines and handle line
2248 * continuations.
2249 */
2250int
b5744197 2251get_line(void)
984263bc
MD
2252{
2253 char *p, *cp;
c57e7ba3 2254 size_t len;
984263bc
MD
2255 int totlen, cont_line;
2256
2257 /*
2258 * Loop around ignoring blank lines and getting all continuation lines.
2259 */
2260 p = line;
2261 totlen = 0;
2262 do {
c57e7ba3 2263 if ((p = fgetln(exp_file, &len)) == NULL)
984263bc 2264 return (0);
984263bc
MD
2265 cp = p + len - 1;
2266 cont_line = 0;
2267 while (cp >= p &&
2268 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2269 if (*cp == '\\')
2270 cont_line = 1;
2271 cp--;
2272 len--;
2273 }
2274 if (cont_line) {
2275 *++cp = ' ';
2276 len++;
2277 }
c57e7ba3
AHJ
2278 if (linesize < len + totlen + 1) {
2279 linesize = len + totlen + 1;
2280 line = realloc(line, linesize);
2281 if (line == NULL)
2282 out_of_mem();
984263bc 2283 }
c57e7ba3
AHJ
2284 memcpy(line + totlen, p, len);
2285 totlen += len;
2286 line[totlen] = '\0';
984263bc
MD
2287 } while (totlen == 0 || cont_line);
2288 return (1);
2289}
2290
2291/*
2292 * Parse a description of a credential.
2293 */
2294void
b5744197 2295parsecred(char *namelist, struct ucred *cr)
984263bc
MD
2296{
2297 char *name;
2298 int cnt;
2299 char *names;
2300 struct passwd *pw;
2301 struct group *gr;
d495caca
AL
2302 gid_t groups[NGROUPS + 1];
2303 int ngroups;
984263bc
MD
2304
2305 /*
2306 * Set up the unprivileged user.
2307 */
2308 cr->cr_ref = 1;
19e6154d
AL
2309 cr->cr_uid = 65534;
2310 cr->cr_groups[0] = 65533;
984263bc
MD
2311 cr->cr_ngroups = 1;
2312 /*
2313 * Get the user's password table entry.
2314 */
2315 names = strsep(&namelist, " \t\n");
2316 name = strsep(&names, ":");
2317 if (isdigit(*name) || *name == '-')
2318 pw = getpwuid(atoi(name));
2319 else
2320 pw = getpwnam(name);
2321 /*
2322 * Credentials specified as those of a user.
2323 */
2324 if (names == NULL) {
2325 if (pw == NULL) {
2326 syslog(LOG_ERR, "unknown user: %s", name);
2327 return;
2328 }
2329 cr->cr_uid = pw->pw_uid;
2330 ngroups = NGROUPS + 1;
2331 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2332 syslog(LOG_ERR, "too many groups");
2333 /*
b4f0a0eb 2334 * Compress out duplicate
984263bc
MD
2335 */
2336 cr->cr_ngroups = ngroups - 1;
2337 cr->cr_groups[0] = groups[0];
2338 for (cnt = 2; cnt < ngroups; cnt++)
2339 cr->cr_groups[cnt - 1] = groups[cnt];
2340 return;
2341 }
2342 /*
2343 * Explicit credential specified as a colon separated list:
2344 * uid:gid:gid:...
2345 */
2346 if (pw != NULL)
2347 cr->cr_uid = pw->pw_uid;
2348 else if (isdigit(*name) || *name == '-')
2349 cr->cr_uid = atoi(name);
2350 else {
2351 syslog(LOG_ERR, "unknown user: %s", name);
2352 return;
2353 }
2354 cr->cr_ngroups = 0;
2355 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
2356 name = strsep(&names, ":");
2357 if (isdigit(*name) || *name == '-') {
2358 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2359 } else {
2360 if ((gr = getgrnam(name)) == NULL) {
2361 syslog(LOG_ERR, "unknown group: %s", name);
2362 continue;
2363 }
2364 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2365 }
2366 }
2367 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
2368 syslog(LOG_ERR, "too many groups");
2369}
2370
2371#define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
2372/*
2373 * Routines that maintain the remote mounttab
2374 */
2375void
b5744197 2376get_mountlist(void)
984263bc
MD
2377{
2378 struct mountlist *mlp, **mlpp;
2379 char *host, *dirp, *cp;
2380 char str[STRSIZ];
2381 FILE *mlfile;
2382
2383 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2384 if (errno == ENOENT)
2385 return;
2386 else {
2387 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2388 return;
2389 }
2390 }
2391 mlpp = &mlhead;
2392 while (fgets(str, STRSIZ, mlfile) != NULL) {
2393 cp = str;
2394 host = strsep(&cp, " \t\n");
2395 dirp = strsep(&cp, " \t\n");
2396 if (host == NULL || dirp == NULL)
2397 continue;
2398 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2038fb68 2399 if (mlp == NULL)
984263bc
MD
2400 out_of_mem();
2401 strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
2402 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2403 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2404 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2038fb68 2405 mlp->ml_next = NULL;
984263bc
MD
2406 *mlpp = mlp;
2407 mlpp = &mlp->ml_next;
2408 }
2409 fclose(mlfile);
2410}
2411
ce1bcb9e
MD
2412static
2413void
2414delete_export(struct statfs *fsp)
2415{
2416 union {
2417 struct ufs_args ua;
2418 struct iso_args ia;
2419 struct mfs_args ma;
2420 struct msdosfs_args da;
2421 struct ntfs_args na;
2422 } targs;
2423 struct export_args export;
2424
2425 export.ex_flags = MNT_DELEXPORT;
2426 if (mountctl(fsp->f_mntonname, MOUNTCTL_SET_EXPORT, -1,
2427 &export, sizeof(export), NULL, 0) == 0) {
2428 /* ok */
2429 } else if (!strcmp(fsp->f_fstypename, "mfs") ||
2430 !strcmp(fsp->f_fstypename, "ufs") ||
2431 !strcmp(fsp->f_fstypename, "msdos") ||
2432 !strcmp(fsp->f_fstypename, "ntfs") ||
2433 !strcmp(fsp->f_fstypename, "cd9660")) {
2434 bzero(&targs, sizeof targs);
2435 targs.ua.fspec = NULL;
2436 targs.ua.export.ex_flags = MNT_DELEXPORT;
2437 if (mount(fsp->f_fstypename, fsp->f_mntonname,
2438 fsp->f_flags | MNT_UPDATE,
2439 (caddr_t)&targs) < 0)
2440 syslog(LOG_ERR, "can't delete exports for %s",
2441 fsp->f_mntonname);
2442 }
2443}
2444
2445
984263bc 2446void
b5744197 2447del_mlist(char *hostp, char *dirp)
984263bc
MD
2448{
2449 struct mountlist *mlp, **mlpp;
2450 struct mountlist *mlp2;
2451 FILE *mlfile;
2452 int fnd = 0;
2453
2454 mlpp = &mlhead;
2455 mlp = mlhead;
2456 while (mlp) {
2457 if (!strcmp(mlp->ml_host, hostp) &&
2458 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2459 fnd = 1;
2460 mlp2 = mlp;
2461 *mlpp = mlp = mlp->ml_next;
2462 free((caddr_t)mlp2);
2463 } else {
2464 mlpp = &mlp->ml_next;
2465 mlp = mlp->ml_next;
2466 }
2467 }
2468 if (fnd) {
2469 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2470 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
2471 return;
2472 }
2473 mlp = mlhead;
2474 while (mlp) {
2475 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2476 mlp = mlp->ml_next;
2477 }
2478 fclose(mlfile);
2479 }
2480}
2481
2482void
b5744197 2483add_mlist(char *hostp, char *dirp)
984263bc
MD
2484{
2485 struct mountlist *mlp, **mlpp;
2486 FILE *mlfile;
2487
2488 mlpp = &mlhead;
2489 mlp = mlhead;
2490 while (mlp) {
2491 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2492 return;
2493 mlpp = &mlp->ml_next;
2494 mlp = mlp->ml_next;
2495 }
2496 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2038fb68 2497 if (mlp == NULL)
984263bc
MD
2498 out_of_mem();
2499 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2500 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2501 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2502 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2038fb68 2503 mlp->ml_next = NULL;
984263bc
MD
2504 *mlpp = mlp;
2505 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2506 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
2507 return;
2508 }
2509 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2510 fclose(mlfile);
2511}
2512
2513/*
2514 * Free up a group list.
2515 */
2516void
b5744197 2517free_grp(struct grouplist *grp)
984263bc 2518{
984263bc 2519 if (grp->gr_type == GT_HOST) {
ce0e08e2
PA
2520 if (grp->gr_ptr.gt_addrinfo != NULL)
2521 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
984263bc
MD
2522 } else if (grp->gr_type == GT_NET) {
2523 if (grp->gr_ptr.gt_net.nt_name)
2524 free(grp->gr_ptr.gt_net.nt_name);
2525 }
2526 free((caddr_t)grp);
2527}
2528
2529#ifdef DEBUG
2530void
2531SYSLOG(int pri, const char *fmt, ...)
2532{
2533 va_list ap;
2534
2535 va_start(ap, fmt);
2536 vfprintf(stderr, fmt, ap);
2537 va_end(ap);
2538}
2539#endif /* DEBUG */
2540
2541/*
2542 * Check options for consistency.
2543 */
2544int
b5744197 2545check_options(struct dirlist *dp)
984263bc
MD
2546{
2547
2038fb68 2548 if (dp == NULL)
984263bc 2549 return (1);
ec12fea1
AHJ
2550 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
2551 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
2552 return (1);
984263bc
MD
2553 }
2554 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2555 syslog(LOG_ERR, "-mask requires -network");
2556 return (1);
2557 }
0e0ae4e8
AHJ
2558 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
2559 syslog(LOG_ERR, "-network requires mask specification");
2560 return (1);
2561 }
2562 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
2563 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
2564 return (1);
2565 }
984263bc
MD
2566 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2567 syslog(LOG_ERR, "-alldirs has multiple directories");
2568 return (1);
2569 }
2570 return (0);
2571}
2572
2573/*
2574 * Check an absolute directory path for any symbolic links. Return true
984263bc
MD
2575 */
2576int
b5744197 2577check_dirpath(char *dirp)
984263bc
MD
2578{
2579 char *cp;
2580 int ret = 1;
2581 struct stat sb;
2582
2583 cp = dirp + 1;
2584 while (*cp && ret) {
2585 if (*cp == '/') {
2586 *cp = '\0';
2587 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2588 ret = 0;
2589 *cp = '/';
2590 }
2591 cp++;
2592 }
2593 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2594 ret = 0;
2595 return (ret);
2596}
2597
0e0ae4e8
AHJ
2598/*
2599 * Make a netmask according to the specified prefix length. The ss_family
2600 * and other non-address fields must be initialised before calling this.
2601 */
2602int
2603makemask(struct sockaddr_storage *ssp, int bitlen)
984263bc 2604{
0e0ae4e8
AHJ
2605 u_char *p;
2606 int bits, i, len;
2607
2608 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
2609 return (-1);
e815eb09 2610 if (bitlen > len * CHAR_BIT)
0e0ae4e8
AHJ
2611 return (-1);
2612 for (i = 0; i < len; i++) {
e815eb09 2613 bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
611231b9 2614 *p++ = (u_char)~0 << (CHAR_BIT - bits);
0e0ae4e8 2615 bitlen -= bits;
ce0e08e2 2616 }
0e0ae4e8 2617 return 0;
ce0e08e2
PA
2618}
2619
0e0ae4e8
AHJ
2620/*
2621 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
2622 * is acceptable (i.e. of the form 1...10....0).
2623 */
2624int
2625checkmask(struct sockaddr *sa)
ce0e08e2 2626{
0e0ae4e8
AHJ
2627 u_char *mask;
2628 int i, len;
ce0e08e2 2629
0e0ae4e8
AHJ
2630 if ((mask = sa_rawaddr(sa, &len)) == NULL)
2631 return (-1);
ce0e08e2 2632
0e0ae4e8
AHJ
2633 for (i = 0; i < len; i++)
2634 if (mask[i] != 0xff)
2635 break;
2636 if (i < len) {
2637 if (~mask[i] & (u_char)(~mask[i] + 1))
2638 return (-1);
2639 i++;
2640 }
2641 for (; i < len; i++)
2642 if (mask[i] != 0)
2643 return (-1);
2644 return (0);
ce0e08e2
PA
2645}
2646
0e0ae4e8
AHJ
2647/*
2648 * Compare two sockaddrs according to a specified mask. Return zero if
2649 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
2650 * If samask is NULL, perform a full comparision.
2651 */
2652int
2653sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
ce0e08e2 2654{
0e0ae4e8
AHJ
2655 unsigned char *p1, *p2, *mask;
2656 int len, i;
ce0e08e2 2657
0e0ae4e8
AHJ
2658 if (sa1->sa_family != sa2->sa_family ||
2659 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
2660 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
2661 return (1);
2662
2663 switch (sa1->sa_family) {
ce0e08e2 2664 case AF_INET6:
0e0ae4e8
AHJ
2665 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
2666 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
2667 return (1);
ce0e08e2 2668 break;
984263bc 2669 }
ce0e08e2 2670
0e0ae4e8
AHJ
2671 /* Simple binary comparison if no mask specified. */
2672 if (samask == NULL)
2673 return (memcmp(p1, p2, len));
ce0e08e2 2674
0e0ae4e8
AHJ
2675 /* Set up the mask, and do a mask-based comparison. */
2676 if (sa1->sa_family != samask->sa_family ||
2677 (mask = sa_rawaddr(samask, NULL)) == NULL)
2678 return (1);
ce0e08e2 2679
0e0ae4e8
AHJ
2680 for (i = 0; i < len; i++)
2681 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
2682 return (1);
2683 return (0);
ce0e08e2
PA
2684}
2685
0e0ae4e8
AHJ
2686/*
2687 * Return a pointer to the part of the sockaddr that contains the
2688 * raw address, and set *nbytes to its length in bytes. Returns
2689 * NULL if the address family is unknown.
2690 */
2691void *
2692sa_rawaddr(struct sockaddr *sa, int *nbytes) {
2693 void *p;
ce0e08e2
PA
2694 int len;
2695
0e0ae4e8 2696 switch (sa->sa_family) {
ce0e08e2 2697 case AF_INET:
0e0ae4e8
AHJ
2698 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2699 p = &((struct sockaddr_in *)sa)->sin_addr;
ce0e08e2
PA
2700 break;
2701 case AF_INET6:
0e0ae4e8
AHJ
2702 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2703 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
ce0e08e2
PA
2704 break;
2705 default:
0e0ae4e8
AHJ
2706 p = NULL;
2707 len = 0;
ce0e08e2
PA
2708 }
2709
0e0ae4e8
AHJ
2710 if (nbytes != NULL)
2711 *nbytes = len;
2712 return (p);
ce0e08e2
PA
2713}
2714
2715void
ddfadeae 2716huphandler1(int sig __unused)
ce1bcb9e
MD
2717{
2718 got_sighup1 = 1;
2719}
2720
2721void
ddfadeae 2722huphandler2(int sig __unused)
ce0e08e2 2723{
ce1bcb9e 2724 got_sighup2 = 1;
ce0e08e2
PA
2725}
2726
ddfadeae 2727void terminate(int sig __unused)
ce0e08e2 2728{
fdff0afe 2729 pidfile_remove(pfh);
ce0e08e2
PA
2730 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
2731 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
2732 exit (0);
984263bc 2733}