2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Herb Hasler and Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
51 #include <sys/fcntl.h>
52 #include <sys/fnv_hash.h>
53 #include <sys/linker.h>
54 #include <sys/module.h>
55 #include <sys/mount.h>
56 #include <sys/queue.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
62 #include <rpc/rpc_com.h>
63 #include <rpc/pmap_clnt.h>
64 #include <rpc/pmap_prot.h>
65 #include <rpcsvc/mount.h>
66 #include <nfs/nfsproto.h>
67 #include <nfs/nfssvc.h>
68 #include <nfsserver/nfs.h>
70 #include <fs/nfs/nfsport.h>
72 #include <arpa/inet.h>
87 #include "pathnames.h"
95 * Structures for keeping the mount list and export list
98 char ml_host[MNTNAMLEN+1];
99 char ml_dirp[MNTPATHLEN+1];
101 SLIST_ENTRY(mountlist) next;
105 struct dirlist *dp_left;
106 struct dirlist *dp_right;
108 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
112 #define DP_DEFSET 0x1
113 #define DP_HOSTSET 0x2
116 struct dirlist *ex_dirl;
117 struct dirlist *ex_defdir;
118 struct grouplist *ex_grphead;
123 struct xucred ex_defanon;
125 int ex_numsecflavors;
126 int ex_secflavors[MAXSECFLAVORS];
127 int ex_defnumsecflavors;
128 int ex_defsecflavors[MAXSECFLAVORS];
130 SLIST_ENTRY(exportlist) entries;
133 #define EX_LINKED 0x1
135 #define EX_DEFSET 0x4
136 #define EX_PUBLICFH 0x8
138 SLIST_HEAD(exportlisthead, exportlist);
141 struct sockaddr_storage nt_net;
142 struct sockaddr_storage nt_mask;
147 struct addrinfo *gt_addrinfo;
148 struct netmsk gt_net;
153 union grouptypes gr_ptr;
154 struct grouplist *gr_next;
155 struct xucred gr_anon;
158 int gr_numsecflavors;
159 int gr_secflavors[MAXSECFLAVORS];
165 #define GT_DEFAULT 0x3
166 #define GT_IGNORE 0x5
172 int ht_flag; /* Uses DP_xx bits */
173 struct grouplist *ht_grp;
174 struct hostlist *ht_next;
181 int fhr_numsecflavors;
185 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
188 * How long to delay a reload of exports when there are RPC request(s)
189 * to process, in usec. Must be less than 1second.
191 #define RELOADDELAY 250000
194 static char *add_expdir(struct dirlist **, char *, int);
195 static void add_dlist(struct dirlist **, struct dirlist *,
196 struct grouplist *, int, struct exportlist *,
197 struct xucred *, int);
198 static void add_mlist(char *, char *);
199 static int check_dirpath(char *);
200 static int check_options(struct dirlist *);
201 static int checkmask(struct sockaddr *sa);
202 static int chk_host(struct dirlist *, struct sockaddr *, int *, int *,
204 static char *strsep_quote(char **stringp, const char *delim);
205 static int create_service(struct netconfig *nconf);
206 static void complete_service(struct netconfig *nconf, char *port_str);
207 static void clearout_service(void);
208 static void del_mlist(char *hostp, char *dirp);
209 static struct dirlist *dirp_search(struct dirlist *, char *);
210 static int do_export_mount(struct exportlist *, struct statfs *);
211 static int do_mount(struct exportlist *, struct grouplist *, int,
212 struct xucred *, char *, int, struct statfs *, int, int *);
213 static int do_opt(char **, char **, struct exportlist *,
214 struct grouplist *, int *, int *, struct xucred *);
215 static struct exportlist *ex_search(fsid_t *, struct exportlisthead *);
216 static struct exportlist *get_exp(void);
217 static void free_dir(struct dirlist *);
218 static void free_exp(struct exportlist *);
219 static void free_grp(struct grouplist *);
220 static void free_host(struct hostlist *);
221 static void free_v4rootexp(void);
222 static void get_exportlist_one(int);
223 static void get_exportlist(int);
224 static void insert_exports(struct exportlist *, struct exportlisthead *);
225 static void free_exports(struct exportlisthead *);
226 static void read_exportfile(int);
227 static int compare_nmount_exportlist(struct iovec *, int, char *);
228 static int compare_export(struct exportlist *, struct exportlist *);
229 static int compare_cred(struct xucred *, struct xucred *);
230 static int compare_secflavor(int *, int *, int);
231 static void delete_export(struct iovec *, int, struct statfs *, char *);
232 static int get_host(char *, struct grouplist *, struct grouplist *);
233 static struct hostlist *get_ht(void);
234 static int get_line(void);
235 static void get_mountlist(void);
236 static int get_net(char *, struct netmsk *, int);
237 static void getexp_err(struct exportlist *, struct grouplist *, const char *);
238 static struct grouplist *get_grp(void);
239 static void hang_dirp(struct dirlist *, struct grouplist *,
240 struct exportlist *, int, struct xucred *, int);
241 static void huphandler(int sig);
242 static int makemask(struct sockaddr_storage *ssp, int bitlen);
243 static void mntsrv(struct svc_req *, SVCXPRT *);
244 static void nextfield(char **, char **);
245 static void out_of_mem(void);
246 static void parsecred(char *, struct xucred *);
247 static int parsesec(char *, struct exportlist *);
248 static int put_exlist(struct dirlist *, XDR *, struct dirlist *,
250 static void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
251 static int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
252 struct sockaddr *samask);
253 static int scan_tree(struct dirlist *, struct sockaddr *);
254 static void usage(void);
255 static int xdr_dir(XDR *, char *);
256 static int xdr_explist(XDR *, caddr_t);
257 static int xdr_explist_brief(XDR *, caddr_t);
258 static int xdr_explist_common(XDR *, caddr_t, int);
259 static int xdr_fhs(XDR *, caddr_t);
260 static int xdr_mlist(XDR *, caddr_t);
261 static void terminate(int);
263 #define EXPHASH(f) (fnv_32_buf((f), sizeof(fsid_t), 0) % exphashsize)
264 static struct exportlisthead *exphead = NULL;
265 static struct exportlisthead *oldexphead = NULL;
266 static int exphashsize = 0;
267 static SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(&mlhead);
268 static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
269 static char **exnames;
270 static char **hosts = NULL;
271 static struct xucred def_anon = {
278 static int force_v2 = 0;
279 static int resvport_only = 1;
280 static int nhosts = 0;
281 static int dir_only = 1;
282 static int dolog = 0;
283 static int got_sighup = 0;
284 static int xcreated = 0;
286 static char *svcport_str = NULL;
287 static int mallocd_svcport = 0;
289 static int sock_fdcnt;
290 static int sock_fdpos;
291 static int suspend_nfsd = 0;
293 static int opt_flags;
294 static int have_v6 = 1;
296 static int v4root_phase = 0;
297 static char v4root_dirpath[PATH_MAX + 1];
298 static struct exportlist *v4root_ep = NULL;
299 static int has_publicfh = 0;
300 static int has_set_publicfh = 0;
302 static struct pidfh *pfh = NULL;
303 /* Bits for opt_flags above */
304 #define OP_MAPROOT 0x01
305 #define OP_MAPALL 0x02
309 #define OP_ALLDIRS 0x40
310 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
311 #define OP_QUIET 0x100
312 #define OP_MASKLEN 0x200
316 static int debug = 1;
317 static void SYSLOG(int, const char *, ...) __printflike(2, 3);
318 #define syslog SYSLOG
320 static int debug = 0;
324 * The LOGDEBUG() syslog() calls are always compiled into the daemon.
325 * To enable them, create a file at _PATH_MOUNTDDEBUG. This file can be empty.
326 * To disable the logging, just delete the file at _PATH_MOUNTDDEBUG.
328 static int logdebug = 0;
329 #define LOGDEBUG(format, ...) \
330 (logdebug ? syslog(LOG_DEBUG, format, ## __VA_ARGS__) : 0)
333 * Similar to strsep(), but it allows for quoted strings
334 * and escaped characters.
336 * It returns the string (or NULL, if *stringp is NULL),
337 * which is a de-quoted version of the string if necessary.
339 * It modifies *stringp in place.
342 strsep_quote(char **stringp, const char *delim)
344 char *srcptr, *dstptr, *retval;
347 if (stringp == NULL || *stringp == NULL)
350 srcptr = dstptr = retval = *stringp;
354 * We're looking for several edge cases here.
355 * First: if we're in quote state (quot != 0),
356 * then we ignore the delim characters, but otherwise
357 * process as normal, unless it is the quote character.
358 * Second: if the current character is a backslash,
359 * we take the next character as-is, without checking
360 * for delim, quote, or backslash. Exception: if the
361 * next character is a NUL, that's the end of the string.
362 * Third: if the character is a quote character, we toggle
364 * Otherwise: check the current character for NUL, or
365 * being in delim, and end the string if either is true.
367 if (*srcptr == '\\') {
370 * The edge case here is if the next character
371 * is NUL, we want to stop processing. But if
372 * it's not NUL, then we simply want to copy it.
375 *dstptr++ = *srcptr++;
379 if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
383 if (quot && *srcptr == quot) {
384 /* End of the quoted part */
389 if (!quot && strchr(delim, *srcptr))
391 *dstptr++ = *srcptr++;
394 *stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
395 *dstptr = 0; /* Terminate the string */
400 * Mountd server for NFS mount protocol as described in:
401 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
402 * The optional arguments are the exports file name
403 * default: _PATH_EXPORTS
404 * and "-n" to allow nonroot mount.
407 main(int argc, char **argv)
410 struct netconfig *nconf;
411 char *endptr, **hosts_bak;
416 int maxrec = RPC_MAXDATASIZE;
417 int attempt_cnt, port_len, port_pos, ret;
419 uint64_t curtime, nexttime;
422 sigset_t sig_mask, sighup_mask;
426 /* Check that another mountd isn't already running. */
427 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
430 errx(1, "mountd already running, pid: %d.", otherpid);
431 warn("cannot open or create pidfile");
434 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
440 while ((c = getopt(argc, argv, "2deh:lnp:RrS")) != -1)
446 /* now a no-op, since this is the default */
452 /* Do not support Mount protocol */
459 debug = debug ? 0 : 1;
466 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
467 if (endptr == NULL || *endptr != '\0' ||
468 svcport == 0 || svcport >= IPPORT_MAX)
470 svcport_str = strdup(optarg);
475 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
476 if (hosts_bak == NULL) {
478 for (k = 0; k < nhosts; k++)
485 hosts[nhosts - 1] = strdup(optarg);
486 if (hosts[nhosts - 1] == NULL) {
487 for (k = 0; k < (nhosts - 1); k++)
499 if (enable_rpcbind == 0) {
500 if (svcport_str != NULL) {
501 warnx("-p option not compatible with -R, ignored");
506 warnx("-h option not compatible with -R, ignored");
507 for (k = 0; k < nhosts; k++)
515 if (modfind("nfsd") < 0) {
516 /* Not present in kernel, try loading it */
517 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
518 errx(1, "NFS server is not available");
526 exnames = exnames_default;
527 openlog("mountd", LOG_PID, LOG_DAEMON);
529 warnx("getting export list");
532 warnx("getting mount list");
538 signal(SIGINT, SIG_IGN);
539 signal(SIGQUIT, SIG_IGN);
541 signal(SIGHUP, huphandler);
542 signal(SIGTERM, terminate);
543 signal(SIGPIPE, SIG_IGN);
547 if (enable_rpcbind != 0) {
548 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
549 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
550 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
552 if (!resvport_only) {
553 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
554 &resvport_only, sizeof(resvport_only)) != 0 &&
556 syslog(LOG_ERR, "sysctl: %m");
562 * If no hosts were specified, add a wildcard entry to bind to
563 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added
567 hosts = malloc(sizeof(char *));
575 hosts_bak = realloc(hosts, (nhosts + 2) *
577 if (hosts_bak == NULL) {
578 for (k = 0; k < nhosts; k++)
585 hosts[nhosts - 2] = "::1";
587 hosts_bak = realloc(hosts, (nhosts + 1) *
589 if (hosts_bak == NULL) {
590 for (k = 0; k < nhosts; k++)
600 hosts[nhosts - 1] = "127.0.0.1";
609 if (enable_rpcbind != 0) {
610 nc_handle = setnetconfig();
611 while ((nconf = getnetconfig(nc_handle))) {
612 if (nconf->nc_flag & NC_VISIBLE) {
613 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
617 ret = create_service(nconf);
619 /* Ignore this call */
623 * Failed to bind port, so close
624 * off all sockets created and
625 * try again if the port# was
626 * dynamically assigned via
630 if (mallocd_svcport != 0 &&
645 * Start over at the first
651 nc_handle = setnetconfig();
653 } else if (mallocd_svcport != 0 &&
654 attempt_cnt == GETPORT_MAXTRY) {
656 * For the last attempt, allow
657 * different port #s for each
658 * nconf by saving the
659 * svcport_str setting it back
662 port_list = realloc(port_list,
665 if (port_list == NULL)
667 port_list[port_len++] =
677 * Successfully bound the ports, so call complete_service() to
678 * do the rest of the setup on the service(s).
682 nc_handle = setnetconfig();
683 while ((nconf = getnetconfig(nc_handle))) {
684 if (nconf->nc_flag & NC_VISIBLE) {
685 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
688 } else if (port_list != NULL) {
689 if (port_pos >= port_len) {
690 syslog(LOG_ERR, "too many"
694 complete_service(nconf,
695 port_list[port_pos++]);
697 complete_service(nconf, svcport_str);
700 endnetconfig(nc_handle);
702 if (port_list != NULL) {
703 for (port_pos = 0; port_pos < port_len; port_pos++)
704 free(port_list[port_pos]);
709 syslog(LOG_ERR, "could not create any services");
714 /* Expand svc_run() here so that we can call get_exportlist(). */
715 curtime = nexttime = 0;
716 sigemptyset(&sighup_mask);
717 sigaddset(&sighup_mask, SIGHUP);
719 clock_gettime(CLOCK_MONOTONIC, &tp);
721 curtime = curtime * 1000000 + tp.tv_nsec / 1000;
722 sigprocmask(SIG_BLOCK, &sighup_mask, &sig_mask);
723 if (got_sighup && curtime >= nexttime) {
725 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
727 clock_gettime(CLOCK_MONOTONIC, &tp);
728 nexttime = tp.tv_sec;
729 nexttime = nexttime * 1000000 + tp.tv_nsec / 1000 +
732 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
735 * If a reload is pending, poll for received request(s),
736 * otherwise set a RELOADDELAY timeout, since a SIGHUP
737 * could be processed between the got_sighup test and
738 * the select() system call.
744 tv.tv_usec = RELOADDELAY;
745 if (enable_rpcbind != 0) {
747 switch (select(svc_maxfd + 1, &readfds, NULL, NULL,
750 if (errno == EINTR) {
751 /* Allow a reload now. */
755 syslog(LOG_ERR, "mountd died: select: %m");
758 /* Allow a reload now. */
762 svc_getreqset(&readfds);
765 /* Simply wait for a signal. */
766 sigsuspend(&sig_mask);
772 * This routine creates and binds sockets on the appropriate
773 * addresses. It gets called one time for each transport.
774 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
775 * bind failed with EADDRINUSE.
776 * Any file descriptors that have been created are stored in sock_fd and
777 * the total count of them is maintained in sock_fdcnt.
780 create_service(struct netconfig *nconf)
782 struct addrinfo hints, *res = NULL;
783 struct sockaddr_in *sin;
784 struct sockaddr_in6 *sin6;
785 struct __rpc_sockinfo si;
791 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
794 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
795 (nconf->nc_semantics != NC_TPI_COTS) &&
796 (nconf->nc_semantics != NC_TPI_COTS_ORD))
797 return (1); /* not my type */
800 * XXX - using RPC library internal functions.
802 if (!__rpc_nconf2sockinfo(nconf, &si)) {
803 syslog(LOG_ERR, "cannot get information for %s",
808 /* Get mountd's address on this transport */
809 memset(&hints, 0, sizeof hints);
810 hints.ai_family = si.si_af;
811 hints.ai_socktype = si.si_socktype;
812 hints.ai_protocol = si.si_proto;
815 * Bind to specific IPs if asked to
818 while (nhostsbak > 0) {
820 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
823 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
826 hints.ai_flags = AI_PASSIVE;
829 * XXX - using RPC library internal functions.
831 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
833 if (errno == EAFNOSUPPORT &&
834 nconf->nc_semantics != NC_TPI_CLTS)
837 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
838 "cannot create socket for %s", nconf->nc_netid);
844 switch (hints.ai_family) {
846 if (inet_pton(AF_INET, hosts[nhostsbak],
848 hints.ai_flags |= AI_NUMERICHOST;
851 * Skip if we have an AF_INET6 address.
853 if (inet_pton(AF_INET6, hosts[nhostsbak],
861 if (inet_pton(AF_INET6, hosts[nhostsbak],
863 hints.ai_flags |= AI_NUMERICHOST;
866 * Skip if we have an AF_INET address.
868 if (inet_pton(AF_INET, hosts[nhostsbak],
876 * We're doing host-based access checks here, so don't
877 * allow v4-in-v6 to confuse things. The kernel will
878 * disable it by default on NFS sockets too.
880 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
883 "can't disable v4-in-v6 on IPv6 socket");
892 * If no hosts were specified, just bind to INADDR_ANY
894 if (strcmp("*", hosts[nhostsbak]) == 0) {
895 if (svcport_str == NULL) {
896 res = malloc(sizeof(struct addrinfo));
900 res->ai_flags = hints.ai_flags;
901 res->ai_family = hints.ai_family;
902 res->ai_protocol = hints.ai_protocol;
903 switch (res->ai_family) {
905 sin = malloc(sizeof(struct sockaddr_in));
908 sin->sin_family = AF_INET;
909 sin->sin_port = htons(0);
910 sin->sin_addr.s_addr = htonl(INADDR_ANY);
911 res->ai_addr = (struct sockaddr*) sin;
912 res->ai_addrlen = (socklen_t)
913 sizeof(struct sockaddr_in);
916 sin6 = malloc(sizeof(struct sockaddr_in6));
919 sin6->sin6_family = AF_INET6;
920 sin6->sin6_port = htons(0);
921 sin6->sin6_addr = in6addr_any;
922 res->ai_addr = (struct sockaddr*) sin6;
923 res->ai_addrlen = (socklen_t)
924 sizeof(struct sockaddr_in6);
927 syslog(LOG_ERR, "bad addr fam %d",
932 if ((aicode = getaddrinfo(NULL, svcport_str,
933 &hints, &res)) != 0) {
935 "cannot get local address for %s: %s",
937 gai_strerror(aicode));
943 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
944 &hints, &res)) != 0) {
946 "cannot get local address for %s: %s",
947 nconf->nc_netid, gai_strerror(aicode));
954 sock_fd[sock_fdcnt - 1] = fd;
956 /* Now, attempt the bind. */
957 r = bindresvport_sa(fd, res->ai_addr);
959 if (errno == EADDRINUSE && mallocd_svcport != 0) {
960 if (mallocd_res != 0) {
967 syslog(LOG_ERR, "bindresvport_sa: %m");
971 if (svcport_str == NULL) {
972 svcport_str = malloc(NI_MAXSERV * sizeof(char));
973 if (svcport_str == NULL)
977 if (getnameinfo(res->ai_addr,
978 res->ai_addr->sa_len, NULL, NI_MAXHOST,
979 svcport_str, NI_MAXSERV * sizeof(char),
980 NI_NUMERICHOST | NI_NUMERICSERV))
981 errx(1, "Cannot get port number");
983 if (mallocd_res != 0) {
994 * Called after all the create_service() calls have succeeded, to complete
995 * the setup and registration.
998 complete_service(struct netconfig *nconf, char *port_str)
1000 struct addrinfo hints, *res = NULL;
1001 struct __rpc_sockinfo si;
1002 struct netbuf servaddr;
1003 SVCXPRT *transp = NULL;
1004 int aicode, fd, nhostsbak;
1007 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
1008 (nconf->nc_semantics != NC_TPI_COTS) &&
1009 (nconf->nc_semantics != NC_TPI_COTS_ORD))
1010 return; /* not my type */
1013 * XXX - using RPC library internal functions.
1015 if (!__rpc_nconf2sockinfo(nconf, &si)) {
1016 syslog(LOG_ERR, "cannot get information for %s",
1022 while (nhostsbak > 0) {
1024 if (sock_fdpos >= sock_fdcnt) {
1025 /* Should never happen. */
1026 syslog(LOG_ERR, "Ran out of socket fd's");
1029 fd = sock_fd[sock_fdpos++];
1034 * Using -1 tells listen(2) to use
1035 * kern.ipc.soacceptqueue for the backlog.
1037 if (nconf->nc_semantics != NC_TPI_CLTS)
1040 if (nconf->nc_semantics == NC_TPI_CLTS )
1041 transp = svc_dg_create(fd, 0, 0);
1043 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
1046 if (transp != (SVCXPRT *) NULL) {
1047 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
1050 "can't register %s MOUNTVERS service",
1053 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
1056 "can't register %s MOUNTVERS3 service",
1060 syslog(LOG_WARNING, "can't create %s services",
1063 if (registered == 0) {
1065 memset(&hints, 0, sizeof hints);
1066 hints.ai_flags = AI_PASSIVE;
1067 hints.ai_family = si.si_af;
1068 hints.ai_socktype = si.si_socktype;
1069 hints.ai_protocol = si.si_proto;
1071 if ((aicode = getaddrinfo(NULL, port_str, &hints,
1073 syslog(LOG_ERR, "cannot get local address: %s",
1074 gai_strerror(aicode));
1078 servaddr.buf = malloc(res->ai_addrlen);
1079 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
1080 servaddr.len = res->ai_addrlen;
1082 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
1083 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
1092 * Clear out sockets after a failure to bind one of them, so that the
1093 * cycle of socket creation/binding can start anew.
1096 clearout_service(void)
1100 for (i = 0; i < sock_fdcnt; i++) {
1101 if (sock_fd[i] >= 0) {
1102 shutdown(sock_fd[i], SHUT_RDWR);
1112 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
1113 "[-S] [-h <bindip>] [export_file ...]\n");
1118 * The mount rpc service
1121 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
1123 struct exportlist *ep;
1125 struct fhreturn fhr;
1128 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1129 int lookup_failed = 1;
1130 struct sockaddr *saddr;
1132 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1133 int defset, hostset;
1135 sigset_t sighup_mask;
1136 int numsecflavors, *secflavorsp;
1138 sigemptyset(&sighup_mask);
1139 sigaddset(&sighup_mask, SIGHUP);
1140 saddr = svc_getrpccaller(transp)->buf;
1141 switch (saddr->sa_family) {
1143 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1146 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1149 syslog(LOG_ERR, "request from unknown address family");
1152 switch (rqstp->rq_proc) {
1154 case MOUNTPROC_UMNT:
1155 case MOUNTPROC_UMNTALL:
1156 lookup_failed = getnameinfo(saddr, saddr->sa_len, host,
1157 sizeof host, NULL, 0, 0);
1159 getnameinfo(saddr, saddr->sa_len, numerichost,
1160 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1161 switch (rqstp->rq_proc) {
1163 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1164 syslog(LOG_ERR, "can't send reply");
1167 if (sport >= IPPORT_RESERVED && resvport_only) {
1169 "mount request from %s from unprivileged port",
1171 svcerr_weakauth(transp);
1174 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1175 syslog(LOG_NOTICE, "undecodable mount request from %s",
1177 svcerr_decode(transp);
1182 * Get the real pathname and make sure it is a directory
1183 * or a regular file if the -r option was specified
1186 if (realpath(rpcpath, dirpath) == NULL ||
1187 stat(dirpath, &stb) < 0 ||
1188 statfs(dirpath, &fsb) < 0) {
1189 chdir("/"); /* Just in case realpath doesn't */
1191 "mount request from %s for non existent path %s",
1192 numerichost, dirpath);
1194 warnx("stat failed on %s", dirpath);
1195 bad = ENOENT; /* We will send error reply later */
1198 !S_ISDIR(stb.st_mode) &&
1199 (dir_only || !S_ISREG(stb.st_mode))) {
1201 "mount request from %s for non-directory path %s",
1202 numerichost, dirpath);
1204 warnx("mounting non-directory %s", dirpath);
1205 bad = ENOTDIR; /* We will send error reply later */
1208 /* Check in the exports list */
1209 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1213 ep = ex_search(&fsb.f_fsid, exphead);
1214 hostset = defset = 0;
1215 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1216 &numsecflavors, &secflavorsp) ||
1217 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1218 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1220 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1221 scan_tree(ep->ex_dirl, saddr) == 0))) {
1223 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1225 syslog(LOG_ERR, "can't send reply");
1226 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1229 if (hostset & DP_HOSTSET) {
1230 fhr.fhr_flag = hostset;
1231 fhr.fhr_numsecflavors = numsecflavors;
1232 fhr.fhr_secflavors = secflavorsp;
1234 fhr.fhr_flag = defset;
1235 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1236 fhr.fhr_secflavors = ep->ex_defsecflavors;
1238 fhr.fhr_vers = rqstp->rq_vers;
1239 /* Get the file handle */
1240 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1241 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1243 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1244 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1246 syslog(LOG_ERR, "can't send reply");
1247 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1250 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1252 syslog(LOG_ERR, "can't send reply");
1254 add_mlist(host, dirpath);
1256 add_mlist(numerichost, dirpath);
1258 warnx("mount successful");
1261 "mount request succeeded from %s for %s",
1262 numerichost, dirpath);
1267 "mount request denied from %s for %s",
1268 numerichost, dirpath);
1271 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1273 syslog(LOG_ERR, "can't send reply");
1274 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1276 case MOUNTPROC_DUMP:
1277 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1278 syslog(LOG_ERR, "can't send reply");
1281 "dump request succeeded from %s",
1284 case MOUNTPROC_UMNT:
1285 if (sport >= IPPORT_RESERVED && resvport_only) {
1287 "umount request from %s from unprivileged port",
1289 svcerr_weakauth(transp);
1292 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1293 syslog(LOG_NOTICE, "undecodable umount request from %s",
1295 svcerr_decode(transp);
1298 if (realpath(rpcpath, dirpath) == NULL) {
1299 syslog(LOG_NOTICE, "umount request from %s "
1300 "for non existent path %s",
1301 numerichost, dirpath);
1303 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1304 syslog(LOG_ERR, "can't send reply");
1306 del_mlist(host, dirpath);
1307 del_mlist(numerichost, dirpath);
1310 "umount request succeeded from %s for %s",
1311 numerichost, dirpath);
1313 case MOUNTPROC_UMNTALL:
1314 if (sport >= IPPORT_RESERVED && resvport_only) {
1316 "umountall request from %s from unprivileged port",
1318 svcerr_weakauth(transp);
1321 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1322 syslog(LOG_ERR, "can't send reply");
1324 del_mlist(host, NULL);
1325 del_mlist(numerichost, NULL);
1328 "umountall request succeeded from %s",
1331 case MOUNTPROC_EXPORT:
1332 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1333 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1335 syslog(LOG_ERR, "can't send reply");
1338 "export request succeeded from %s",
1342 svcerr_noproc(transp);
1348 * Xdr conversion for a dirpath string
1351 xdr_dir(XDR *xdrsp, char *dirp)
1353 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1357 * Xdr routine to generate file handle reply
1360 xdr_fhs(XDR *xdrsp, caddr_t cp)
1362 struct fhreturn *fhrp = (struct fhreturn *)cp;
1363 u_long ok = 0, len, auth;
1366 if (!xdr_long(xdrsp, &ok))
1368 switch (fhrp->fhr_vers) {
1370 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1373 if (!xdr_long(xdrsp, &len))
1375 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1377 if (fhrp->fhr_numsecflavors) {
1378 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1380 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1381 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1387 if (!xdr_long(xdrsp, &len))
1389 return (xdr_long(xdrsp, &auth));
1396 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1398 struct mountlist *mlp;
1403 SLIST_FOREACH(mlp, &mlhead, next) {
1404 if (!xdr_bool(xdrsp, &true))
1406 strp = &mlp->ml_host[0];
1407 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1409 strp = &mlp->ml_dirp[0];
1410 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1413 if (!xdr_bool(xdrsp, &false))
1419 * Xdr conversion for export list
1422 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1424 struct exportlist *ep;
1427 sigset_t sighup_mask;
1430 sigemptyset(&sighup_mask);
1431 sigaddset(&sighup_mask, SIGHUP);
1432 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1434 for (i = 0; i < exphashsize; i++)
1435 SLIST_FOREACH(ep, &exphead[i], entries) {
1437 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1440 if (ep->ex_defdir && putdef == 0 &&
1441 put_exlist(ep->ex_defdir, xdrsp, NULL,
1445 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1446 if (!xdr_bool(xdrsp, &false))
1450 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1455 * Called from xdr_explist() to traverse the tree and export the
1459 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1462 struct grouplist *grp;
1463 struct hostlist *hp;
1470 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1472 if (!xdr_bool(xdrsp, &true))
1475 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1477 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1482 if (!xdr_bool(xdrsp, &true))
1485 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1487 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1488 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1492 if (grp->gr_type == GT_HOST) {
1493 if (!xdr_bool(xdrsp, &true))
1495 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1496 if (!xdr_string(xdrsp, &strp,
1499 } else if (grp->gr_type == GT_NET) {
1500 if (!xdr_bool(xdrsp, &true))
1502 strp = grp->gr_ptr.gt_net.nt_name;
1503 if (!xdr_string(xdrsp, &strp,
1508 if (gotalldir && hp == (struct hostlist *)NULL) {
1514 if (!xdr_bool(xdrsp, &false))
1516 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1523 xdr_explist(XDR *xdrsp, caddr_t cp)
1526 return xdr_explist_common(xdrsp, cp, 0);
1530 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1533 return xdr_explist_common(xdrsp, cp, 1);
1537 static size_t linesize;
1538 static FILE *exp_file;
1541 * Get the export list from one, currently open file
1544 get_exportlist_one(int passno)
1546 struct exportlist *ep;
1547 struct grouplist *grp, *tgrp, *savgrp;
1548 struct dirlist *dirhead;
1551 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1552 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1555 dirhead = (struct dirlist *)NULL;
1556 while (get_line()) {
1558 warnx("got line %s", line);
1560 nextfield(&cp, &endcp);
1569 exflags = MNT_EXPORTED;
1572 ep = (struct exportlist *)NULL;
1576 * Handle the V4 root dir.
1578 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1580 * V4: just indicates that it is the v4 root point,
1581 * so skip over that and set v4root_phase.
1583 if (v4root_phase > 0) {
1584 syslog(LOG_ERR, "V4:duplicate line, ignored");
1589 nextfield(&cp, &endcp);
1593 * Create new exports list entry
1596 tgrp = grp = get_grp();
1598 if (len > MNTNAMLEN) {
1599 getexp_err(ep, tgrp, "mountpoint too long");
1603 if (ep == (struct exportlist *)NULL) {
1604 getexp_err(ep, tgrp,
1605 "flag before export path definition");
1609 warnx("doing opt %s", cp);
1611 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1613 getexp_err(ep, tgrp, NULL);
1616 } else if (*cp == '/') {
1619 if (v4root_phase > 1) {
1621 getexp_err(ep, tgrp, "Multiple V4 dirs");
1625 if (check_dirpath(cp) &&
1626 statfs(cp, &fsb) >= 0) {
1627 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1628 syslog(LOG_ERR, "Warning: exporting of "
1629 "automounted fs %s not supported", cp);
1631 getexp_err(ep, tgrp, "dirs must be first");
1634 if (v4root_phase == 1) {
1636 getexp_err(ep, tgrp, "Multiple V4 dirs");
1639 if (strlen(v4root_dirpath) == 0) {
1640 strlcpy(v4root_dirpath, cp,
1641 sizeof (v4root_dirpath));
1642 } else if (strcmp(v4root_dirpath, cp)
1645 "different V4 dirpath %s", cp);
1646 getexp_err(ep, tgrp, NULL);
1655 if (fsidcmp(&ep->ex_fs, &fsb.f_fsid)
1657 getexp_err(ep, tgrp,
1663 * See if this directory is already
1666 ep = ex_search(&fsb.f_fsid, exphead);
1667 if (ep == (struct exportlist *)NULL) {
1669 ep->ex_fs = fsb.f_fsid;
1670 ep->ex_fsdir = strdup(fsb.f_mntonname);
1671 if (ep->ex_fsdir == NULL)
1675 "making new ep fs=0x%x,0x%x",
1679 warnx("found ep fs=0x%x,0x%x",
1685 * Add dirpath to export mount point.
1687 dirp = add_expdir(&dirhead, cp, len);
1691 getexp_err(ep, tgrp,
1692 "symbolic link in export path or statfs failed");
1700 if (ep == (struct exportlist *)NULL) {
1701 getexp_err(ep, tgrp,
1702 "host(s) before export path definition");
1707 * Get the host or netgroup.
1710 netgrp = getnetgrent(&hst, &usr, &dom);
1713 grp->gr_next = get_grp();
1719 "null hostname in netgroup %s, skipping", cp);
1720 grp->gr_type = GT_IGNORE;
1721 } else if (get_host(hst, grp, tgrp)) {
1723 "bad host %s in netgroup %s, skipping", hst, cp);
1724 grp->gr_type = GT_IGNORE;
1726 } else if (get_host(cp, grp, tgrp)) {
1727 syslog(LOG_ERR, "bad host %s, skipping", cp);
1728 grp->gr_type = GT_IGNORE;
1731 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1736 nextfield(&cp, &endcp);
1739 if (check_options(dirhead)) {
1740 getexp_err(ep, tgrp, NULL);
1744 grp->gr_type = GT_DEFAULT;
1746 warnx("adding a default entry");
1749 * Don't allow a network export coincide with a list of
1750 * host(s) on the same line.
1752 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1753 getexp_err(ep, tgrp, "network/host conflict");
1757 * If an export list was specified on this line, make sure
1758 * that we have at least one valid entry, otherwise skip it.
1762 while (grp && grp->gr_type == GT_IGNORE)
1765 getexp_err(ep, tgrp, "no valid entries");
1770 if (v4root_phase == 1) {
1771 getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1776 * Loop through hosts, pushing the exports into the kernel.
1777 * After loop, tgrp points to the start of the list and
1778 * grp points to the last entry in the list.
1779 * Do not do the do_mount() for passno == 1, since the
1780 * second pass will do it, as required.
1784 grp->gr_exflags = exflags;
1785 grp->gr_anon = anon;
1786 if (v4root_phase == 2 && passno == 0)
1787 LOGDEBUG("do_mount v4root");
1788 if (passno == 0 && do_mount(ep, grp, exflags, &anon,
1789 dirp, dirplen, &fsb, ep->ex_numsecflavors,
1790 ep->ex_secflavors)) {
1791 getexp_err(ep, tgrp, NULL);
1794 } while (grp->gr_next && (grp = grp->gr_next));
1797 * For V4: don't enter in mount lists.
1799 if (v4root_phase > 0 && v4root_phase <= 2) {
1801 * These structures are used for the reload,
1802 * so save them for that case. Otherwise, just
1805 if (passno == 1 && ep != NULL) {
1807 while (tgrp != NULL) {
1809 * Save the security flavors and exflags
1810 * for this host set in the groups.
1812 tgrp->gr_numsecflavors =
1813 ep->ex_numsecflavors;
1814 if (ep->ex_numsecflavors > 0)
1815 memcpy(tgrp->gr_secflavors,
1817 sizeof(ep->ex_secflavors));
1818 tgrp = tgrp->gr_next;
1820 if (v4root_ep == NULL) {
1822 ep = NULL; /* Don't free below. */
1824 grp->gr_next = v4root_ep->ex_grphead;
1825 v4root_ep->ex_grphead = savgrp;
1829 while (tgrp != NULL) {
1831 tgrp = tgrp->gr_next;
1838 * Success. Update the data structures.
1841 hang_dirp(dirhead, tgrp, ep, opt_flags, &anon, exflags);
1842 grp->gr_next = ep->ex_grphead;
1843 ep->ex_grphead = tgrp;
1845 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1846 opt_flags, &anon, exflags);
1849 dirhead = (struct dirlist *)NULL;
1850 if ((ep->ex_flag & EX_LINKED) == 0) {
1851 insert_exports(ep, exphead);
1853 ep->ex_flag |= EX_LINKED;
1859 dirhead = (struct dirlist *)NULL;
1865 * Get the export list from all specified files
1868 get_exportlist(int passno)
1870 struct export_args export;
1872 struct statfs *mntbufp;
1876 struct nfsex_args eargs;
1879 if ((debug_file = fopen(_PATH_MOUNTDDEBUG, "r")) != NULL) {
1884 LOGDEBUG("passno=%d", passno);
1885 v4root_dirpath[0] = '\0';
1889 * Save the current lists as old ones, so that the new lists
1890 * can be compared with the old ones in the 2nd pass.
1892 for (i = 0; i < exphashsize; i++) {
1893 SLIST_FIRST(&oldexphead[i]) = SLIST_FIRST(&exphead[i]);
1894 SLIST_INIT(&exphead[i]);
1897 /* Note that the public fh has not yet been set. */
1898 has_set_publicfh = 0;
1900 /* Read the export file(s) and process them */
1901 read_exportfile(passno);
1904 * Just make the old lists empty.
1905 * exphashsize == 0 for the first call, before oldexphead
1906 * has been initialized-->loop won't be executed.
1908 for (i = 0; i < exphashsize; i++)
1909 SLIST_INIT(&oldexphead[i]);
1912 bzero(&export, sizeof(export));
1913 export.ex_flags = MNT_DELEXPORT;
1916 bzero(errmsg, sizeof(errmsg));
1918 if (suspend_nfsd != 0)
1919 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1921 * Delete the old V4 root dir.
1923 bzero(&eargs, sizeof (eargs));
1924 eargs.export.ex_flags = MNT_DELEXPORT;
1925 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1927 syslog(LOG_ERR, "Can't delete exports for V4:");
1929 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1930 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1931 build_iovec(&iov, &iovlen, "from", NULL, 0);
1932 build_iovec(&iov, &iovlen, "update", NULL, 0);
1933 build_iovec(&iov, &iovlen, "export", &export,
1935 build_iovec(&iov, &iovlen, "errmsg", errmsg,
1939 * For passno == 1, compare the old and new lists updating the kernel
1940 * exports for any cases that have changed.
1941 * This call is doing the second pass through the lists.
1942 * If it fails, fall back on the bulk reload.
1944 if (passno == 1 && compare_nmount_exportlist(iov, iovlen, errmsg) ==
1946 LOGDEBUG("compareok");
1947 /* Free up the old lists. */
1948 free_exports(oldexphead);
1950 LOGDEBUG("doing passno=0");
1952 * Clear flag that notes if a public fh has been exported.
1953 * It is set by do_mount() if MNT_EXPUBLIC is set for the entry.
1957 /* exphead == NULL if not yet allocated (first call). */
1958 if (exphead != NULL) {
1960 * First, get rid of the old lists.
1962 free_exports(exphead);
1963 free_exports(oldexphead);
1967 * And delete exports that are in the kernel for all local
1969 * XXX: Should know how to handle all local exportable
1972 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1974 /* Allocate hash tables, for first call. */
1975 if (exphead == NULL) {
1976 /* Target an average linked list length of 10. */
1977 exphashsize = num / 10;
1978 if (exphashsize < 1)
1980 else if (exphashsize > 100000)
1981 exphashsize = 100000;
1982 exphead = malloc(exphashsize * sizeof(*exphead));
1983 oldexphead = malloc(exphashsize * sizeof(*oldexphead));
1984 if (exphead == NULL || oldexphead == NULL)
1985 errx(1, "Can't malloc hash tables");
1987 for (i = 0; i < exphashsize; i++) {
1988 SLIST_INIT(&exphead[i]);
1989 SLIST_INIT(&oldexphead[i]);
1993 for (i = 0; i < num; i++)
1994 delete_export(iov, iovlen, &mntbufp[i], errmsg);
1997 /* Read the export file(s) and process them */
2002 /* Free strings allocated by strdup() in getmntopts.c */
2003 free(iov[0].iov_base); /* fstype */
2004 free(iov[2].iov_base); /* fspath */
2005 free(iov[4].iov_base); /* from */
2006 free(iov[6].iov_base); /* update */
2007 free(iov[8].iov_base); /* export */
2008 free(iov[10].iov_base); /* errmsg */
2010 /* free iov, allocated by realloc() */
2016 * If there was no public fh, clear any previous one set.
2018 if (has_publicfh == 0) {
2019 LOGDEBUG("clear public fh");
2020 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
2023 /* Resume the nfsd. If they weren't suspended, this is harmless. */
2024 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
2025 LOGDEBUG("eo get_exportlist");
2029 * Insert an export entry in the appropriate list.
2032 insert_exports(struct exportlist *ep, struct exportlisthead *exhp)
2036 i = EXPHASH(&ep->ex_fs);
2037 LOGDEBUG("fs=%s hash=%i", ep->ex_fsdir, i);
2038 SLIST_INSERT_HEAD(&exhp[i], ep, entries);
2042 * Free up the exports lists passed in as arguments.
2045 free_exports(struct exportlisthead *exhp)
2047 struct exportlist *ep, *ep2;
2050 for (i = 0; i < exphashsize; i++) {
2051 SLIST_FOREACH_SAFE(ep, &exhp[i], entries, ep2) {
2052 SLIST_REMOVE(&exhp[i], ep, exportlist, entries);
2055 SLIST_INIT(&exhp[i]);
2060 * Read the exports file(s) and call get_exportlist_one() for each line.
2063 read_exportfile(int passno)
2068 * Read in the exports file and build the list, calling
2069 * nmount() as we go along to push the export rules into the kernel.
2072 for (i = 0; exnames[i] != NULL; i++) {
2074 warnx("reading exports from %s", exnames[i]);
2075 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
2076 syslog(LOG_WARNING, "can't open %s", exnames[i]);
2079 get_exportlist_one(passno);
2084 syslog(LOG_ERR, "can't open any exports file");
2090 * Compare the export lists against the old ones and do nmount() operations
2091 * for any cases that have changed. This avoids doing nmount() for entries
2092 * that have not changed.
2093 * Return 0 upon success, 1 otherwise.
2096 compare_nmount_exportlist(struct iovec *iov, int iovlen, char *errmsg)
2098 struct exportlist *ep, *oep;
2099 struct grouplist *grp;
2100 struct statfs fs, ofs;
2104 * Loop through the current list and look for an entry in the old
2106 * If found, check to see if it the same.
2107 * If it is not the same, delete and re-export.
2108 * Then mark it done on the old list.
2111 * Any entries left in the old list after processing must have their
2114 for (i = 0; i < exphashsize; i++)
2115 SLIST_FOREACH(ep, &exphead[i], entries) {
2116 LOGDEBUG("foreach ep=%s", ep->ex_fsdir);
2117 oep = ex_search(&ep->ex_fs, oldexphead);
2120 * Check the mount paths are the same.
2121 * If not, return 1 so that the reload of the
2122 * exports will be done in bulk, the
2125 LOGDEBUG("found old exp");
2126 if (strcmp(ep->ex_fsdir, oep->ex_fsdir) != 0)
2128 LOGDEBUG("same fsdir");
2130 * Test to see if the entry is the same.
2131 * If not the same delete exports and
2134 if (compare_export(ep, oep) != 0) {
2136 * Clear has_publicfh if if was set
2137 * in the old exports, but only if it
2138 * has not been set during processing of
2139 * the exports for this pass, as
2140 * indicated by has_set_publicfh.
2142 if (has_set_publicfh == 0 &&
2143 (oep->ex_flag & EX_PUBLICFH) != 0)
2146 /* Delete and re-export. */
2147 if (statfs(ep->ex_fsdir, &fs) < 0)
2149 delete_export(iov, iovlen, &fs, errmsg);
2150 ret = do_export_mount(ep, &fs);
2154 oep->ex_flag |= EX_DONE;
2157 LOGDEBUG("not found so export");
2158 /* Not found, so do export. */
2159 if (statfs(ep->ex_fsdir, &fs) < 0)
2161 ret = do_export_mount(ep, &fs);
2167 /* Delete exports not done. */
2168 for (i = 0; i < exphashsize; i++)
2169 SLIST_FOREACH(oep, &oldexphead[i], entries) {
2170 if ((oep->ex_flag & EX_DONE) == 0) {
2171 LOGDEBUG("not done delete=%s", oep->ex_fsdir);
2172 if (statfs(oep->ex_fsdir, &ofs) >= 0 &&
2173 fsidcmp(&oep->ex_fs, &ofs.f_fsid) == 0) {
2174 LOGDEBUG("do delete");
2176 * Clear has_publicfh if if was set
2177 * in the old exports, but only if it
2178 * has not been set during processing of
2179 * the exports for this pass, as
2180 * indicated by has_set_publicfh.
2182 if (has_set_publicfh == 0 &&
2183 (oep->ex_flag & EX_PUBLICFH) != 0)
2186 delete_export(iov, iovlen, &ofs,
2192 /* Do the V4 root exports, as required. */
2194 if (v4root_ep != NULL)
2195 grp = v4root_ep->ex_grphead;
2197 while (v4root_ep != NULL && grp != NULL) {
2198 LOGDEBUG("v4root expath=%s", v4root_dirpath);
2199 ret = do_mount(v4root_ep, grp, grp->gr_exflags, &grp->gr_anon,
2200 v4root_dirpath, strlen(v4root_dirpath), &fs,
2201 grp->gr_numsecflavors, grp->gr_secflavors);
2214 * Compare old and current exportlist entries for the fsid and return 0
2215 * if they are the same, 1 otherwise.
2218 compare_export(struct exportlist *ep, struct exportlist *oep)
2220 struct grouplist *grp, *ogrp;
2222 if (strcmp(ep->ex_fsdir, oep->ex_fsdir) != 0)
2224 if ((ep->ex_flag & EX_DEFSET) != (oep->ex_flag & EX_DEFSET))
2226 if ((ep->ex_defdir != NULL && oep->ex_defdir == NULL) ||
2227 (ep->ex_defdir == NULL && oep->ex_defdir != NULL))
2229 if (ep->ex_defdir != NULL && (ep->ex_defdir->dp_flag & DP_DEFSET) !=
2230 (oep->ex_defdir->dp_flag & DP_DEFSET))
2232 if ((ep->ex_flag & EX_DEFSET) != 0 && (ep->ex_defnumsecflavors !=
2233 oep->ex_defnumsecflavors || ep->ex_defexflags !=
2234 oep->ex_defexflags || compare_cred(&ep->ex_defanon,
2235 &oep->ex_defanon) != 0 || compare_secflavor(ep->ex_defsecflavors,
2236 oep->ex_defsecflavors, ep->ex_defnumsecflavors) != 0))
2239 /* Now, check all the groups. */
2240 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp = ogrp->gr_next)
2242 for (grp = ep->ex_grphead; grp != NULL; grp = grp->gr_next) {
2243 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp =
2245 if ((ogrp->gr_flag & GR_FND) == 0 &&
2246 grp->gr_numsecflavors == ogrp->gr_numsecflavors &&
2247 grp->gr_exflags == ogrp->gr_exflags &&
2248 compare_cred(&grp->gr_anon, &ogrp->gr_anon) == 0 &&
2249 compare_secflavor(grp->gr_secflavors,
2250 ogrp->gr_secflavors, grp->gr_numsecflavors) == 0)
2253 ogrp->gr_flag |= GR_FND;
2257 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp = ogrp->gr_next)
2258 if ((ogrp->gr_flag & GR_FND) == 0)
2264 * This algorithm compares two arrays of "n" items. It returns 0 if they are
2265 * the "same" and 1 otherwise. Although suboptimal, it is always safe to
2266 * return 1, which makes compare_nmount_export() reload the exports entry.
2267 * "same" refers to having the same set of values in the two arrays.
2268 * The arrays are in no particular order and duplicates (multiple entries
2269 * in an array with the same value) is allowed.
2270 * The algorithm is inefficient, but the common case of indentical arrays is
2271 * handled first and "n" is normally fairly small.
2272 * Since the two functions need the same algorithm but for arrays of
2273 * different types (gid_t vs int), this is done as a macro.
2275 #define COMPARE_ARRAYS(a1, a2, n) \
2277 int fnd, fndarray[(n)], i, j; \
2278 /* Handle common case of identical arrays. */ \
2279 for (i = 0; i < (n); i++) \
2280 if ((a1)[i] != (a2)[i]) \
2284 for (i = 0; i < (n); i++) \
2286 for (i = 0; i < (n); i++) { \
2288 for (j = 0; j < (n); j++) { \
2289 if ((a1)[i] == (a2)[j]) { \
2297 for (i = 0; i < (n); i++) \
2298 if (fndarray[i] == 0) \
2304 * Compare to struct xucred's. Return 0 if the same and 1 otherwise.
2307 compare_cred(struct xucred *cr0, struct xucred *cr1)
2310 if (cr0->cr_uid != cr1->cr_uid || cr0->cr_ngroups != cr1->cr_ngroups)
2313 COMPARE_ARRAYS(cr0->cr_groups, cr1->cr_groups, cr0->cr_ngroups);
2317 * Compare two lists of security flavors. Return 0 if the same and 1 otherwise.
2320 compare_secflavor(int *sec1, int *sec2, int nsec)
2323 COMPARE_ARRAYS(sec1, sec2, nsec);
2327 * Delete an exports entry.
2330 delete_export(struct iovec *iov, int iovlen, struct statfs *fsp, char *errmsg)
2332 struct xvfsconf vfc;
2334 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
2335 syslog(LOG_ERR, "getvfsbyname() failed for %s",
2341 * We do not need to delete "export" flag from
2342 * filesystems that do not have it set.
2344 if (!(fsp->f_flags & MNT_EXPORTED))
2347 * Do not delete export for network filesystem by
2348 * passing "export" arg to nmount().
2349 * It only makes sense to do this for local filesystems.
2351 if (vfc.vfc_flags & VFCF_NETWORK)
2354 iov[1].iov_base = fsp->f_fstypename;
2355 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
2356 iov[3].iov_base = fsp->f_mntonname;
2357 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
2358 iov[5].iov_base = fsp->f_mntfromname;
2359 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
2363 * EXDEV is returned when path exists but is not a
2364 * mount point. May happens if raced with unmount.
2366 if (nmount(iov, iovlen, fsp->f_flags) < 0 && errno != ENOENT &&
2367 errno != ENOTSUP && errno != EXDEV) {
2369 "can't delete exports for %s: %m %s",
2370 fsp->f_mntonname, errmsg);
2375 * Allocate an export list element
2377 static struct exportlist *
2380 struct exportlist *ep;
2382 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
2383 if (ep == (struct exportlist *)NULL)
2389 * Allocate a group list element
2391 static struct grouplist *
2394 struct grouplist *gp;
2396 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
2397 if (gp == (struct grouplist *)NULL)
2403 * Clean up upon an error in get_exportlist().
2406 getexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
2408 struct grouplist *tgrp;
2410 if (!(opt_flags & OP_QUIET)) {
2412 syslog(LOG_ERR, "bad exports list line '%s': %s", line,
2415 syslog(LOG_ERR, "bad exports list line '%s'", line);
2417 if (ep && (ep->ex_flag & EX_LINKED) == 0)
2427 * Search the export list for a matching fs.
2429 static struct exportlist *
2430 ex_search(fsid_t *fsid, struct exportlisthead *exhp)
2432 struct exportlist *ep;
2436 SLIST_FOREACH(ep, &exhp[i], entries) {
2437 if (fsidcmp(&ep->ex_fs, fsid) == 0)
2445 * Add a directory path to the list.
2448 add_expdir(struct dirlist **dpp, char *cp, int len)
2452 dp = malloc(sizeof (struct dirlist));
2453 if (dp == (struct dirlist *)NULL)
2456 dp->dp_right = (struct dirlist *)NULL;
2458 dp->dp_hosts = (struct hostlist *)NULL;
2459 dp->dp_dirp = strndup(cp, len);
2460 if (dp->dp_dirp == NULL)
2463 return (dp->dp_dirp);
2467 * Hang the dir list element off the dirpath binary tree as required
2468 * and update the entry for host.
2471 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
2472 int flags, struct xucred *anoncrp, int exflags)
2474 struct hostlist *hp;
2475 struct dirlist *dp2;
2477 if (flags & OP_ALLDIRS) {
2482 if (grp == (struct grouplist *)NULL) {
2483 ep->ex_flag |= EX_DEFSET;
2484 ep->ex_defdir->dp_flag |= DP_DEFSET;
2485 /* Save the default security flavors list. */
2486 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2487 if (ep->ex_numsecflavors > 0)
2488 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2489 sizeof(ep->ex_secflavors));
2490 ep->ex_defanon = *anoncrp;
2491 ep->ex_defexflags = exflags;
2492 } else while (grp) {
2495 hp->ht_next = ep->ex_defdir->dp_hosts;
2496 ep->ex_defdir->dp_hosts = hp;
2497 /* Save the security flavors list for this host set. */
2498 grp->gr_numsecflavors = ep->ex_numsecflavors;
2499 if (ep->ex_numsecflavors > 0)
2500 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2501 sizeof(ep->ex_secflavors));
2507 * Loop through the directories adding them to the tree.
2511 add_dlist(&ep->ex_dirl, dp, grp, flags, ep, anoncrp,
2519 * Traverse the binary tree either updating a node that is already there
2520 * for the new directory or adding the new node.
2523 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2524 int flags, struct exportlist *ep, struct xucred *anoncrp, int exflags)
2527 struct hostlist *hp;
2532 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2534 add_dlist(&dp->dp_left, newdp, grp, flags, ep, anoncrp,
2537 } else if (cmp < 0) {
2538 add_dlist(&dp->dp_right, newdp, grp, flags, ep, anoncrp,
2542 free((caddr_t)newdp);
2545 dp->dp_left = (struct dirlist *)NULL;
2551 * Hang all of the host(s) off of the directory point.
2556 hp->ht_next = dp->dp_hosts;
2558 /* Save the security flavors list for this host set. */
2559 grp->gr_numsecflavors = ep->ex_numsecflavors;
2560 if (ep->ex_numsecflavors > 0)
2561 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2562 sizeof(ep->ex_secflavors));
2566 ep->ex_flag |= EX_DEFSET;
2567 dp->dp_flag |= DP_DEFSET;
2568 /* Save the default security flavors list. */
2569 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2570 if (ep->ex_numsecflavors > 0)
2571 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2572 sizeof(ep->ex_secflavors));
2573 ep->ex_defanon = *anoncrp;
2574 ep->ex_defexflags = exflags;
2579 * Search for a dirpath on the export point.
2581 static struct dirlist *
2582 dirp_search(struct dirlist *dp, char *dirp)
2587 cmp = strcmp(dp->dp_dirp, dirp);
2589 return (dirp_search(dp->dp_left, dirp));
2591 return (dirp_search(dp->dp_right, dirp));
2599 * Scan for a host match in a directory tree.
2602 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2603 int *hostsetp, int *numsecflavors, int **secflavorsp)
2605 struct hostlist *hp;
2606 struct grouplist *grp;
2607 struct addrinfo *ai;
2610 if (dp->dp_flag & DP_DEFSET)
2611 *defsetp = dp->dp_flag;
2615 switch (grp->gr_type) {
2617 ai = grp->gr_ptr.gt_addrinfo;
2618 for (; ai; ai = ai->ai_next) {
2619 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2621 (hp->ht_flag | DP_HOSTSET);
2622 if (numsecflavors != NULL) {
2624 grp->gr_numsecflavors;
2633 if (!sacmp(saddr, (struct sockaddr *)
2634 &grp->gr_ptr.gt_net.nt_net,
2636 &grp->gr_ptr.gt_net.nt_mask)) {
2637 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2638 if (numsecflavors != NULL) {
2640 grp->gr_numsecflavors;
2655 * Scan tree for a host that matches the address.
2658 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2660 int defset, hostset;
2663 if (scan_tree(dp->dp_left, saddr))
2665 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2667 if (scan_tree(dp->dp_right, saddr))
2674 * Traverse the dirlist tree and free it up.
2677 free_dir(struct dirlist *dp)
2681 free_dir(dp->dp_left);
2682 free_dir(dp->dp_right);
2683 free_host(dp->dp_hosts);
2690 * Parse a colon separated list of security flavors
2693 parsesec(char *seclist, struct exportlist *ep)
2698 ep->ex_numsecflavors = 0;
2700 cp = strchr(seclist, ':');
2706 if (!strcmp(seclist, "sys"))
2708 else if (!strcmp(seclist, "krb5"))
2709 flavor = RPCSEC_GSS_KRB5;
2710 else if (!strcmp(seclist, "krb5i"))
2711 flavor = RPCSEC_GSS_KRB5I;
2712 else if (!strcmp(seclist, "krb5p"))
2713 flavor = RPCSEC_GSS_KRB5P;
2717 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2720 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2723 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2726 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2727 ep->ex_numsecflavors++;
2739 * Parse the option string and update fields.
2740 * Option arguments may either be -<option>=<value> or
2744 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2745 int *has_hostp, int *exflagsp, struct xucred *cr)
2747 char *cpoptarg, *cpoptend;
2748 char *cp, *endcp, *cpopt, savedc, savedc2;
2749 int allflag, usedarg;
2757 while (cpopt && *cpopt) {
2760 if ((cpoptend = strchr(cpopt, ','))) {
2762 if ((cpoptarg = strchr(cpopt, '=')))
2765 if ((cpoptarg = strchr(cpopt, '=')))
2769 nextfield(&cp, &endcp);
2771 if (endcp > cp && *cp != '-') {
2779 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2780 *exflagsp |= MNT_EXRDONLY;
2781 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2782 !(allflag = strcmp(cpopt, "mapall")) ||
2783 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2785 parsecred(cpoptarg, cr);
2787 *exflagsp |= MNT_EXPORTANON;
2788 opt_flags |= OP_MAPALL;
2790 opt_flags |= OP_MAPROOT;
2791 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2792 !strcmp(cpopt, "m"))) {
2793 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2794 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2798 opt_flags |= OP_MASK;
2799 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2800 !strcmp(cpopt, "n"))) {
2801 if (strchr(cpoptarg, '/') != NULL) {
2803 fprintf(stderr, "setting OP_MASKLEN\n");
2804 opt_flags |= OP_MASKLEN;
2806 if (grp->gr_type != GT_NULL) {
2807 syslog(LOG_ERR, "network/host conflict");
2809 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2810 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2813 grp->gr_type = GT_NET;
2816 opt_flags |= OP_NET;
2817 } else if (!strcmp(cpopt, "alldirs")) {
2818 opt_flags |= OP_ALLDIRS;
2819 } else if (!strcmp(cpopt, "public")) {
2820 *exflagsp |= MNT_EXPUBLIC;
2821 } else if (!strcmp(cpopt, "webnfs")) {
2822 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2823 opt_flags |= OP_MAPALL;
2824 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2825 ep->ex_indexfile = strdup(cpoptarg);
2826 } else if (!strcmp(cpopt, "quiet")) {
2827 opt_flags |= OP_QUIET;
2828 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2829 if (parsesec(cpoptarg, ep))
2831 opt_flags |= OP_SEC;
2834 syslog(LOG_ERR, "bad opt %s", cpopt);
2853 * Translate a character string to the corresponding list of network
2854 * addresses for a hostname.
2857 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2859 struct grouplist *checkgrp;
2860 struct addrinfo *ai, *tai, hints;
2862 char host[NI_MAXHOST];
2864 if (grp->gr_type != GT_NULL) {
2865 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2868 memset(&hints, 0, sizeof hints);
2869 hints.ai_flags = AI_CANONNAME;
2870 hints.ai_protocol = IPPROTO_UDP;
2871 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2873 syslog(LOG_ERR,"can't get address info for host %s", cp);
2876 grp->gr_ptr.gt_addrinfo = ai;
2877 while (ai != NULL) {
2878 if (ai->ai_canonname == NULL) {
2879 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2880 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2881 strlcpy(host, "?", sizeof(host));
2882 ai->ai_canonname = strdup(host);
2883 ai->ai_flags |= AI_CANONNAME;
2886 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2888 * Sanity check: make sure we don't already have an entry
2889 * for this host in the grouplist.
2891 for (checkgrp = tgrp; checkgrp != NULL;
2892 checkgrp = checkgrp->gr_next) {
2893 if (checkgrp->gr_type != GT_HOST)
2895 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2896 tai = tai->ai_next) {
2897 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2901 "ignoring duplicate host %s\n",
2903 grp->gr_type = GT_IGNORE;
2909 grp->gr_type = GT_HOST;
2914 * Free up an exports list component
2917 free_exp(struct exportlist *ep)
2919 struct grouplist *grp, *tgrp;
2921 if (ep->ex_defdir) {
2922 free_host(ep->ex_defdir->dp_hosts);
2923 free((caddr_t)ep->ex_defdir);
2927 if (ep->ex_indexfile)
2928 free(ep->ex_indexfile);
2929 free_dir(ep->ex_dirl);
2930 grp = ep->ex_grphead;
2940 * Free up the v4root exports.
2943 free_v4rootexp(void)
2946 if (v4root_ep != NULL) {
2947 free_exp(v4root_ep);
2956 free_host(struct hostlist *hp)
2958 struct hostlist *hp2;
2967 static struct hostlist *
2970 struct hostlist *hp;
2972 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2973 if (hp == (struct hostlist *)NULL)
2975 hp->ht_next = (struct hostlist *)NULL;
2981 * Out of memory, fatal
2987 syslog(LOG_ERR, "out of memory");
2992 * Call do_mount() from the struct exportlist, for each case needed.
2995 do_export_mount(struct exportlist *ep, struct statfs *fsp)
2997 struct grouplist *grp, defgrp;
3001 LOGDEBUG("do_export_mount=%s", ep->ex_fsdir);
3002 dirlen = strlen(ep->ex_fsdir);
3003 if ((ep->ex_flag & EX_DEFSET) != 0) {
3004 defgrp.gr_type = GT_DEFAULT;
3005 defgrp.gr_next = NULL;
3006 /* We have an entry for all other hosts/nets. */
3007 LOGDEBUG("ex_defexflags=0x%x", ep->ex_defexflags);
3008 ret = do_mount(ep, &defgrp, ep->ex_defexflags, &ep->ex_defanon,
3009 ep->ex_fsdir, dirlen, fsp, ep->ex_defnumsecflavors,
3010 ep->ex_defsecflavors);
3015 /* Do a mount for each group. */
3016 grp = ep->ex_grphead;
3017 while (grp != NULL) {
3018 LOGDEBUG("do mount gr_type=0x%x gr_exflags=0x%x",
3019 grp->gr_type, grp->gr_exflags);
3020 ret = do_mount(ep, grp, grp->gr_exflags, &grp->gr_anon,
3021 ep->ex_fsdir, dirlen, fsp, grp->gr_numsecflavors,
3022 grp->gr_secflavors);
3031 * Do the nmount() syscall with the update flag to push the export info into
3035 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
3036 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb,
3037 int numsecflavors, int *secflavors)
3040 struct addrinfo *ai;
3041 struct export_args *eap;
3049 struct nfsex_args nfsea;
3051 eap = &nfsea.export;
3059 bzero(eap, sizeof (struct export_args));
3060 bzero(errmsg, sizeof(errmsg));
3061 eap->ex_flags = exflags;
3062 eap->ex_anon = *anoncrp;
3063 LOGDEBUG("do_mount exflags=0x%x", exflags);
3064 eap->ex_indexfile = ep->ex_indexfile;
3065 if (grp->gr_type == GT_HOST)
3066 ai = grp->gr_ptr.gt_addrinfo;
3069 eap->ex_numsecflavors = numsecflavors;
3070 LOGDEBUG("do_mount numsec=%d", numsecflavors);
3071 for (i = 0; i < eap->ex_numsecflavors; i++)
3072 eap->ex_secflavors[i] = secflavors[i];
3073 if (eap->ex_numsecflavors == 0) {
3074 eap->ex_numsecflavors = 1;
3075 eap->ex_secflavors[0] = AUTH_SYS;
3079 if (v4root_phase == 0) {
3080 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
3081 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
3082 build_iovec(&iov, &iovlen, "from", NULL, 0);
3083 build_iovec(&iov, &iovlen, "update", NULL, 0);
3084 build_iovec(&iov, &iovlen, "export", eap,
3085 sizeof (struct export_args));
3086 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
3090 switch (grp->gr_type) {
3092 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
3094 eap->ex_addr = ai->ai_addr;
3095 eap->ex_addrlen = ai->ai_addrlen;
3096 eap->ex_masklen = 0;
3099 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
3103 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
3105 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
3107 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
3108 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
3111 eap->ex_addr = NULL;
3112 eap->ex_addrlen = 0;
3113 eap->ex_mask = NULL;
3114 eap->ex_masklen = 0;
3121 syslog(LOG_ERR, "bad grouptype");
3129 * For V4:, use the nfssvc() syscall, instead of mount().
3131 if (v4root_phase == 2) {
3132 nfsea.fspec = v4root_dirpath;
3133 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
3134 syslog(LOG_ERR, "Exporting V4: failed");
3140 * Maybe I should just use the fsb->f_mntonname path
3141 * instead of looping back up the dirp to the mount
3143 * Also, needs to know how to export all types of local
3144 * exportable filesystems and not just "ufs".
3146 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
3147 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
3148 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
3149 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
3150 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
3151 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
3154 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
3158 cp = dirp + dirplen - 1;
3159 if (opt_flags & OP_QUIET) {
3163 if (errno == EPERM) {
3165 warnx("can't change attributes for %s: %s",
3168 "can't change attributes for %s: %s",
3173 if (opt_flags & OP_ALLDIRS) {
3174 if (errno == EINVAL)
3176 "-alldirs requested but %s is not a filesystem mountpoint",
3180 "could not remount %s: %m",
3185 /* back up over the last component */
3186 while (cp > dirp && *cp == '/')
3188 while (cp > dirp && *(cp - 1) != '/')
3192 warnx("mnt unsucc");
3193 syslog(LOG_ERR, "can't export %s %s",
3201 * Check that we're still on the same
3204 if (statfs(dirp, &fsb1) != 0 ||
3205 fsidcmp(&fsb1.f_fsid, &fsb->f_fsid) != 0) {
3208 "can't export %s %s", dirp,
3217 * For the experimental server:
3218 * If this is the public directory, get the file handle
3219 * and load it into the kernel via the nfssvc() syscall.
3221 if ((exflags & MNT_EXPUBLIC) != 0) {
3225 if (eap->ex_indexfile != NULL)
3226 public_name = eap->ex_indexfile;
3229 if (getfh(public_name, &fh) < 0)
3231 "Can't get public fh for %s", public_name);
3232 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
3234 "Can't set public fh for %s", public_name);
3237 has_set_publicfh = 1;
3238 ep->ex_flag |= EX_PUBLICFH;
3250 /* free strings allocated by strdup() in getmntopts.c */
3252 free(iov[0].iov_base); /* fstype */
3253 free(iov[2].iov_base); /* fspath */
3254 free(iov[4].iov_base); /* from */
3255 free(iov[6].iov_base); /* update */
3256 free(iov[8].iov_base); /* export */
3257 free(iov[10].iov_base); /* errmsg */
3259 /* free iov, allocated by realloc() */
3266 * Translate a net address.
3268 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
3271 get_net(char *cp, struct netmsk *net, int maskflg)
3273 struct netent *np = NULL;
3274 char *name, *p, *prefp;
3275 struct sockaddr_in sin;
3276 struct sockaddr *sa = NULL;
3277 struct addrinfo hints, *ai = NULL;
3278 char netname[NI_MAXHOST];
3282 if ((opt_flags & OP_MASKLEN) && !maskflg) {
3283 p = strchr(cp, '/');
3289 * Check for a numeric address first. We wish to avoid
3290 * possible DNS lookups in getnetbyname().
3292 if (isxdigit(*cp) || *cp == ':') {
3293 memset(&hints, 0, sizeof hints);
3294 /* Ensure the mask and the network have the same family. */
3295 if (maskflg && (opt_flags & OP_NET))
3296 hints.ai_family = net->nt_net.ss_family;
3297 else if (!maskflg && (opt_flags & OP_HAVEMASK))
3298 hints.ai_family = net->nt_mask.ss_family;
3300 hints.ai_family = AF_UNSPEC;
3301 hints.ai_flags = AI_NUMERICHOST;
3302 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
3304 if (sa != NULL && ai->ai_family == AF_INET) {
3306 * The address in `cp' is really a network address, so
3307 * use inet_network() to re-interpret this correctly.
3308 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
3310 bzero(&sin, sizeof sin);
3311 sin.sin_family = AF_INET;
3312 sin.sin_len = sizeof sin;
3313 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
3315 fprintf(stderr, "get_net: v4 addr %s\n",
3316 inet_ntoa(sin.sin_addr));
3317 sa = (struct sockaddr *)&sin;
3320 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
3321 bzero(&sin, sizeof sin);
3322 sin.sin_family = AF_INET;
3323 sin.sin_len = sizeof sin;
3324 sin.sin_addr = inet_makeaddr(np->n_net, 0);
3325 sa = (struct sockaddr *)&sin;
3331 /* The specified sockaddr is a mask. */
3332 if (checkmask(sa) != 0)
3334 bcopy(sa, &net->nt_mask, sa->sa_len);
3335 opt_flags |= OP_HAVEMASK;
3337 /* The specified sockaddr is a network address. */
3338 bcopy(sa, &net->nt_net, sa->sa_len);
3340 /* Get a network name for the export list. */
3343 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
3344 NULL, 0, NI_NUMERICHOST) == 0) {
3349 if ((net->nt_name = strdup(name)) == NULL)
3353 * Extract a mask from either a "/<masklen>" suffix, or
3354 * from the class of an IPv4 address.
3356 if (opt_flags & OP_MASKLEN) {
3357 preflen = strtol(prefp, NULL, 10);
3358 if (preflen < 0L || preflen == LONG_MAX)
3360 bcopy(sa, &net->nt_mask, sa->sa_len);
3361 if (makemask(&net->nt_mask, (int)preflen) != 0)
3363 opt_flags |= OP_HAVEMASK;
3365 } else if (sa->sa_family == AF_INET &&
3366 (opt_flags & OP_MASK) == 0) {
3369 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
3370 if (IN_CLASSA(addr))
3372 else if (IN_CLASSB(addr))
3374 else if (IN_CLASSC(addr))
3376 else if (IN_CLASSD(addr))
3379 preflen = 32; /* XXX */
3381 bcopy(sa, &net->nt_mask, sa->sa_len);
3382 makemask(&net->nt_mask, (int)preflen);
3383 opt_flags |= OP_HAVEMASK;
3398 * Parse out the next white space separated field
3401 nextfield(char **cp, char **endcp)
3407 while (*p == ' ' || *p == '\t')
3410 while (*p != '\0') {
3415 if (*p == '\\' && *(p + 1) != '\0')
3417 else if (*p == '\'' || *p == '"')
3419 else if (*p == ' ' || *p == '\t')
3428 * Get an exports file line. Skip over blank lines and handle line
3436 int totlen, cont_line;
3439 * Loop around ignoring blank lines and getting all continuation lines.
3444 if ((p = fgetln(exp_file, &len)) == NULL)
3449 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
3459 if (linesize < len + totlen + 1) {
3460 linesize = len + totlen + 1;
3461 line = realloc(line, linesize);
3465 memcpy(line + totlen, p, len);
3467 line[totlen] = '\0';
3468 } while (totlen == 0 || cont_line);
3473 * Parse a description of a credential.
3476 parsecred(char *namelist, struct xucred *cr)
3483 gid_t groups[XU_NGROUPS + 1];
3486 cr->cr_version = XUCRED_VERSION;
3488 * Set up the unprivileged user.
3491 cr->cr_groups[0] = 65533;
3494 * Get the user's password table entry.
3497 name = strsep_quote(&names, ":");
3498 /* Bug? name could be NULL here */
3499 if (isdigit(*name) || *name == '-')
3500 pw = getpwuid(atoi(name));
3502 pw = getpwnam(name);
3504 * Credentials specified as those of a user.
3506 if (names == NULL) {
3508 syslog(LOG_ERR, "unknown user: %s", name);
3511 cr->cr_uid = pw->pw_uid;
3512 ngroups = XU_NGROUPS + 1;
3513 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) {
3514 syslog(LOG_ERR, "too many groups");
3515 ngroups = XU_NGROUPS + 1;
3519 * Compress out duplicate.
3521 cr->cr_groups[0] = groups[0];
3522 if (ngroups > 1 && groups[0] == groups[1]) {
3523 cr->cr_ngroups = ngroups - 1;
3524 for (cnt = 2; cnt < ngroups; cnt++)
3525 cr->cr_groups[cnt - 1] = groups[cnt];
3527 cr->cr_ngroups = ngroups;
3528 if (cr->cr_ngroups > XU_NGROUPS)
3529 cr->cr_ngroups = XU_NGROUPS;
3530 for (cnt = 1; cnt < cr->cr_ngroups; cnt++)
3531 cr->cr_groups[cnt] = groups[cnt];
3536 * Explicit credential specified as a colon separated list:
3540 cr->cr_uid = pw->pw_uid;
3541 else if (isdigit(*name) || *name == '-')
3542 cr->cr_uid = atoi(name);
3544 syslog(LOG_ERR, "unknown user: %s", name);
3548 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
3549 name = strsep_quote(&names, ":");
3550 if (isdigit(*name) || *name == '-') {
3551 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
3553 if ((gr = getgrnam(name)) == NULL) {
3554 syslog(LOG_ERR, "unknown group: %s", name);
3557 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
3560 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
3561 syslog(LOG_ERR, "too many groups");
3564 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
3566 * Routines that maintain the remote mounttab
3571 struct mountlist *mlp;
3572 char *host, *dirp, *cp;
3576 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
3577 if (errno == ENOENT)
3580 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
3584 while (fgets(str, STRSIZ, mlfile) != NULL) {
3586 host = strsep(&cp, " \t\n");
3587 dirp = strsep(&cp, " \t\n");
3588 if (host == NULL || dirp == NULL)
3590 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3591 if (mlp == (struct mountlist *)NULL)
3593 strncpy(mlp->ml_host, host, MNTNAMLEN);
3594 mlp->ml_host[MNTNAMLEN] = '\0';
3595 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3596 mlp->ml_dirp[MNTPATHLEN] = '\0';
3598 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3604 del_mlist(char *hostp, char *dirp)
3606 struct mountlist *mlp, *mlp2;
3610 SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
3611 if (!strcmp(mlp->ml_host, hostp) &&
3612 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3614 SLIST_REMOVE(&mlhead, mlp, mountlist, next);
3619 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3620 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3623 SLIST_FOREACH(mlp, &mlhead, next) {
3624 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3631 add_mlist(char *hostp, char *dirp)
3633 struct mountlist *mlp;
3636 SLIST_FOREACH(mlp, &mlhead, next) {
3637 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3641 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3642 if (mlp == (struct mountlist *)NULL)
3644 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3645 mlp->ml_host[MNTNAMLEN] = '\0';
3646 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3647 mlp->ml_dirp[MNTPATHLEN] = '\0';
3648 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3649 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3650 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3653 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3658 * Free up a group list.
3661 free_grp(struct grouplist *grp)
3663 if (grp->gr_type == GT_HOST) {
3664 if (grp->gr_ptr.gt_addrinfo != NULL)
3665 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3666 } else if (grp->gr_type == GT_NET) {
3667 if (grp->gr_ptr.gt_net.nt_name)
3668 free(grp->gr_ptr.gt_net.nt_name);
3675 SYSLOG(int pri, const char *fmt, ...)
3680 vfprintf(stderr, fmt, ap);
3686 * Check options for consistency.
3689 check_options(struct dirlist *dp)
3692 if (v4root_phase == 0 && dp == NULL)
3694 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3695 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3698 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3699 syslog(LOG_ERR, "-mask requires -network");
3702 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3703 syslog(LOG_ERR, "-network requires mask specification");
3706 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3707 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3710 if (v4root_phase > 0 &&
3712 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3713 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3716 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3717 syslog(LOG_ERR, "-alldirs has multiple directories");
3724 * Check an absolute directory path for any symbolic links. Return true
3727 check_dirpath(char *dirp)
3734 while (*cp && ret) {
3737 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3743 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3749 * Make a netmask according to the specified prefix length. The ss_family
3750 * and other non-address fields must be initialised before calling this.
3753 makemask(struct sockaddr_storage *ssp, int bitlen)
3758 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3760 if (bitlen > len * CHAR_BIT)
3763 for (i = 0; i < len; i++) {
3764 bits = MIN(CHAR_BIT, bitlen);
3765 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3772 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3773 * is acceptable (i.e. of the form 1...10....0).
3776 checkmask(struct sockaddr *sa)
3781 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3784 for (i = 0; i < len; i++)
3785 if (mask[i] != 0xff)
3788 if (~mask[i] & (u_char)(~mask[i] + 1))
3792 for (; i < len; i++)
3799 * Compare two sockaddrs according to a specified mask. Return zero if
3800 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3801 * If samask is NULL, perform a full comparison.
3804 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3806 unsigned char *p1, *p2, *mask;
3809 if (sa1->sa_family != sa2->sa_family ||
3810 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3811 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3814 switch (sa1->sa_family) {
3816 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3817 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3822 /* Simple binary comparison if no mask specified. */
3824 return (memcmp(p1, p2, len));
3826 /* Set up the mask, and do a mask-based comparison. */
3827 if (sa1->sa_family != samask->sa_family ||
3828 (mask = sa_rawaddr(samask, NULL)) == NULL)
3831 for (i = 0; i < len; i++)
3832 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3838 * Return a pointer to the part of the sockaddr that contains the
3839 * raw address, and set *nbytes to its length in bytes. Returns
3840 * NULL if the address family is unknown.
3843 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3847 switch (sa->sa_family) {
3849 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3850 p = &((struct sockaddr_in *)sa)->sin_addr;
3853 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3854 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3867 huphandler(int sig __unused)
3874 terminate(int sig __unused)
3876 pidfile_remove(pfh);
3877 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3878 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);