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 static char *add_expdir(struct dirlist **, char *, int);
189 static void add_dlist(struct dirlist **, struct dirlist *,
190 struct grouplist *, int, struct exportlist *,
191 struct xucred *, int);
192 static void add_mlist(char *, char *);
193 static int check_dirpath(char *);
194 static int check_options(struct dirlist *);
195 static int checkmask(struct sockaddr *sa);
196 static int chk_host(struct dirlist *, struct sockaddr *, int *, int *,
198 static char *strsep_quote(char **stringp, const char *delim);
199 static int create_service(struct netconfig *nconf);
200 static void complete_service(struct netconfig *nconf, char *port_str);
201 static void clearout_service(void);
202 static void del_mlist(char *hostp, char *dirp);
203 static struct dirlist *dirp_search(struct dirlist *, char *);
204 static int do_export_mount(struct exportlist *, struct statfs *);
205 static int do_mount(struct exportlist *, struct grouplist *, int,
206 struct xucred *, char *, int, struct statfs *, int, int *);
207 static int do_opt(char **, char **, struct exportlist *,
208 struct grouplist *, int *, int *, struct xucred *);
209 static struct exportlist *ex_search(fsid_t *, struct exportlisthead *);
210 static struct exportlist *get_exp(void);
211 static void free_dir(struct dirlist *);
212 static void free_exp(struct exportlist *);
213 static void free_grp(struct grouplist *);
214 static void free_host(struct hostlist *);
215 static void free_v4rootexp(void);
216 static void get_exportlist_one(int);
217 static void get_exportlist(int);
218 static void insert_exports(struct exportlist *, struct exportlisthead *);
219 static void free_exports(struct exportlisthead *);
220 static void read_exportfile(int);
221 static int compare_nmount_exportlist(struct iovec *, int, char *);
222 static int compare_export(struct exportlist *, struct exportlist *);
223 static int compare_cred(struct xucred *, struct xucred *);
224 static int compare_secflavor(int *, int *, int);
225 static void delete_export(struct iovec *, int, struct statfs *, char *);
226 static int get_host(char *, struct grouplist *, struct grouplist *);
227 static struct hostlist *get_ht(void);
228 static int get_line(void);
229 static void get_mountlist(void);
230 static int get_net(char *, struct netmsk *, int);
231 static void getexp_err(struct exportlist *, struct grouplist *, const char *);
232 static struct grouplist *get_grp(void);
233 static void hang_dirp(struct dirlist *, struct grouplist *,
234 struct exportlist *, int, struct xucred *, int);
235 static void huphandler(int sig);
236 static int makemask(struct sockaddr_storage *ssp, int bitlen);
237 static void mntsrv(struct svc_req *, SVCXPRT *);
238 static void nextfield(char **, char **);
239 static void out_of_mem(void);
240 static void parsecred(char *, struct xucred *);
241 static int parsesec(char *, struct exportlist *);
242 static int put_exlist(struct dirlist *, XDR *, struct dirlist *,
244 static void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
245 static int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
246 struct sockaddr *samask);
247 static int scan_tree(struct dirlist *, struct sockaddr *);
248 static void usage(void);
249 static int xdr_dir(XDR *, char *);
250 static int xdr_explist(XDR *, caddr_t);
251 static int xdr_explist_brief(XDR *, caddr_t);
252 static int xdr_explist_common(XDR *, caddr_t, int);
253 static int xdr_fhs(XDR *, caddr_t);
254 static int xdr_mlist(XDR *, caddr_t);
255 static void terminate(int);
257 #define EXPHASH(f) (fnv_32_buf((f), sizeof(fsid_t), 0) % exphashsize)
258 static struct exportlisthead *exphead = NULL;
259 static struct exportlisthead *oldexphead = NULL;
260 static int exphashsize = 0;
261 static SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(&mlhead);
262 static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
263 static char **exnames;
264 static char **hosts = NULL;
265 static struct xucred def_anon = {
272 static int force_v2 = 0;
273 static int resvport_only = 1;
274 static int nhosts = 0;
275 static int dir_only = 1;
276 static int dolog = 0;
277 static int got_sighup = 0;
278 static int xcreated = 0;
280 static char *svcport_str = NULL;
281 static int mallocd_svcport = 0;
283 static int sock_fdcnt;
284 static int sock_fdpos;
285 static int suspend_nfsd = 0;
287 static int opt_flags;
288 static int have_v6 = 1;
290 static int v4root_phase = 0;
291 static char v4root_dirpath[PATH_MAX + 1];
292 static struct exportlist *v4root_ep = NULL;
293 static int has_publicfh = 0;
294 static int has_set_publicfh = 0;
296 static struct pidfh *pfh = NULL;
297 /* Bits for opt_flags above */
298 #define OP_MAPROOT 0x01
299 #define OP_MAPALL 0x02
303 #define OP_ALLDIRS 0x40
304 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
305 #define OP_QUIET 0x100
306 #define OP_MASKLEN 0x200
310 static int debug = 1;
311 static void SYSLOG(int, const char *, ...) __printflike(2, 3);
312 #define syslog SYSLOG
314 static int debug = 0;
318 * The LOGDEBUG() syslog() calls are always compiled into the daemon.
319 * To enable them, create a file at _PATH_MOUNTDDEBUG. This file can be empty.
320 * To disable the logging, just delete the file at _PATH_MOUNTDDEBUG.
322 static int logdebug = 0;
323 #define LOGDEBUG(format, ...) \
324 (logdebug ? syslog(LOG_DEBUG, format, ## __VA_ARGS__) : 0)
327 * Similar to strsep(), but it allows for quoted strings
328 * and escaped characters.
330 * It returns the string (or NULL, if *stringp is NULL),
331 * which is a de-quoted version of the string if necessary.
333 * It modifies *stringp in place.
336 strsep_quote(char **stringp, const char *delim)
338 char *srcptr, *dstptr, *retval;
341 if (stringp == NULL || *stringp == NULL)
344 srcptr = dstptr = retval = *stringp;
348 * We're looking for several edge cases here.
349 * First: if we're in quote state (quot != 0),
350 * then we ignore the delim characters, but otherwise
351 * process as normal, unless it is the quote character.
352 * Second: if the current character is a backslash,
353 * we take the next character as-is, without checking
354 * for delim, quote, or backslash. Exception: if the
355 * next character is a NUL, that's the end of the string.
356 * Third: if the character is a quote character, we toggle
358 * Otherwise: check the current character for NUL, or
359 * being in delim, and end the string if either is true.
361 if (*srcptr == '\\') {
364 * The edge case here is if the next character
365 * is NUL, we want to stop processing. But if
366 * it's not NUL, then we simply want to copy it.
369 *dstptr++ = *srcptr++;
373 if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
377 if (quot && *srcptr == quot) {
378 /* End of the quoted part */
383 if (!quot && strchr(delim, *srcptr))
385 *dstptr++ = *srcptr++;
388 *stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
389 *dstptr = 0; /* Terminate the string */
394 * Mountd server for NFS mount protocol as described in:
395 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
396 * The optional arguments are the exports file name
397 * default: _PATH_EXPORTS
398 * and "-n" to allow nonroot mount.
401 main(int argc, char **argv)
404 struct netconfig *nconf;
405 char *endptr, **hosts_bak;
410 int maxrec = RPC_MAXDATASIZE;
411 int attempt_cnt, port_len, port_pos, ret;
414 /* Check that another mountd isn't already running. */
415 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
418 errx(1, "mountd already running, pid: %d.", otherpid);
419 warn("cannot open or create pidfile");
422 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
428 while ((c = getopt(argc, argv, "2deh:lnp:rS")) != -1)
434 /* now a no-op, since this is the default */
443 debug = debug ? 0 : 1;
450 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
451 if (endptr == NULL || *endptr != '\0' ||
452 svcport == 0 || svcport >= IPPORT_MAX)
454 svcport_str = strdup(optarg);
459 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
460 if (hosts_bak == NULL) {
462 for (k = 0; k < nhosts; k++)
469 hosts[nhosts - 1] = strdup(optarg);
470 if (hosts[nhosts - 1] == NULL) {
471 for (k = 0; k < (nhosts - 1); k++)
484 if (modfind("nfsd") < 0) {
485 /* Not present in kernel, try loading it */
486 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
487 errx(1, "NFS server is not available");
495 exnames = exnames_default;
496 openlog("mountd", LOG_PID, LOG_DAEMON);
498 warnx("getting export list");
501 warnx("getting mount list");
507 signal(SIGINT, SIG_IGN);
508 signal(SIGQUIT, SIG_IGN);
510 signal(SIGHUP, huphandler);
511 signal(SIGTERM, terminate);
512 signal(SIGPIPE, SIG_IGN);
516 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
517 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
518 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
520 if (!resvport_only) {
521 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
522 &resvport_only, sizeof(resvport_only)) != 0 &&
524 syslog(LOG_ERR, "sysctl: %m");
530 * If no hosts were specified, add a wildcard entry to bind to
531 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
535 hosts = malloc(sizeof(char *));
543 hosts_bak = realloc(hosts, (nhosts + 2) *
545 if (hosts_bak == NULL) {
546 for (k = 0; k < nhosts; k++)
553 hosts[nhosts - 2] = "::1";
555 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
556 if (hosts_bak == NULL) {
557 for (k = 0; k < nhosts; k++)
567 hosts[nhosts - 1] = "127.0.0.1";
575 nc_handle = setnetconfig();
576 while ((nconf = getnetconfig(nc_handle))) {
577 if (nconf->nc_flag & NC_VISIBLE) {
578 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
582 ret = create_service(nconf);
584 /* Ignore this call */
588 * Failed to bind port, so close off
589 * all sockets created and try again
590 * if the port# was dynamically
591 * assigned via bind(2).
594 if (mallocd_svcport != 0 &&
595 attempt_cnt < GETPORT_MAXTRY) {
602 "bindresvport_sa: %m");
606 /* Start over at the first service. */
610 nc_handle = setnetconfig();
612 } else if (mallocd_svcport != 0 &&
613 attempt_cnt == GETPORT_MAXTRY) {
615 * For the last attempt, allow
616 * different port #s for each nconf
617 * by saving the svcport_str and
618 * setting it back to NULL.
620 port_list = realloc(port_list,
621 (port_len + 1) * sizeof(char *));
622 if (port_list == NULL)
624 port_list[port_len++] = svcport_str;
633 * Successfully bound the ports, so call complete_service() to
634 * do the rest of the setup on the service(s).
638 nc_handle = setnetconfig();
639 while ((nconf = getnetconfig(nc_handle))) {
640 if (nconf->nc_flag & NC_VISIBLE) {
641 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
644 } else if (port_list != NULL) {
645 if (port_pos >= port_len) {
646 syslog(LOG_ERR, "too many port#s");
649 complete_service(nconf, port_list[port_pos++]);
651 complete_service(nconf, svcport_str);
654 endnetconfig(nc_handle);
656 if (port_list != NULL) {
657 for (port_pos = 0; port_pos < port_len; port_pos++)
658 free(port_list[port_pos]);
663 syslog(LOG_ERR, "could not create any services");
667 /* Expand svc_run() here so that we can call get_exportlist(). */
674 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
678 syslog(LOG_ERR, "mountd died: select: %m");
683 svc_getreqset(&readfds);
689 * This routine creates and binds sockets on the appropriate
690 * addresses. It gets called one time for each transport.
691 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
692 * bind failed with EADDRINUSE.
693 * Any file descriptors that have been created are stored in sock_fd and
694 * the total count of them is maintained in sock_fdcnt.
697 create_service(struct netconfig *nconf)
699 struct addrinfo hints, *res = NULL;
700 struct sockaddr_in *sin;
701 struct sockaddr_in6 *sin6;
702 struct __rpc_sockinfo si;
708 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
711 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
712 (nconf->nc_semantics != NC_TPI_COTS) &&
713 (nconf->nc_semantics != NC_TPI_COTS_ORD))
714 return (1); /* not my type */
717 * XXX - using RPC library internal functions.
719 if (!__rpc_nconf2sockinfo(nconf, &si)) {
720 syslog(LOG_ERR, "cannot get information for %s",
725 /* Get mountd's address on this transport */
726 memset(&hints, 0, sizeof hints);
727 hints.ai_family = si.si_af;
728 hints.ai_socktype = si.si_socktype;
729 hints.ai_protocol = si.si_proto;
732 * Bind to specific IPs if asked to
735 while (nhostsbak > 0) {
737 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
740 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
743 hints.ai_flags = AI_PASSIVE;
746 * XXX - using RPC library internal functions.
748 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
750 if (errno == EAFNOSUPPORT &&
751 nconf->nc_semantics != NC_TPI_CLTS)
754 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
755 "cannot create socket for %s", nconf->nc_netid);
761 switch (hints.ai_family) {
763 if (inet_pton(AF_INET, hosts[nhostsbak],
765 hints.ai_flags |= AI_NUMERICHOST;
768 * Skip if we have an AF_INET6 address.
770 if (inet_pton(AF_INET6, hosts[nhostsbak],
778 if (inet_pton(AF_INET6, hosts[nhostsbak],
780 hints.ai_flags |= AI_NUMERICHOST;
783 * Skip if we have an AF_INET address.
785 if (inet_pton(AF_INET, hosts[nhostsbak],
793 * We're doing host-based access checks here, so don't
794 * allow v4-in-v6 to confuse things. The kernel will
795 * disable it by default on NFS sockets too.
797 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
800 "can't disable v4-in-v6 on IPv6 socket");
809 * If no hosts were specified, just bind to INADDR_ANY
811 if (strcmp("*", hosts[nhostsbak]) == 0) {
812 if (svcport_str == NULL) {
813 res = malloc(sizeof(struct addrinfo));
817 res->ai_flags = hints.ai_flags;
818 res->ai_family = hints.ai_family;
819 res->ai_protocol = hints.ai_protocol;
820 switch (res->ai_family) {
822 sin = malloc(sizeof(struct sockaddr_in));
825 sin->sin_family = AF_INET;
826 sin->sin_port = htons(0);
827 sin->sin_addr.s_addr = htonl(INADDR_ANY);
828 res->ai_addr = (struct sockaddr*) sin;
829 res->ai_addrlen = (socklen_t)
830 sizeof(struct sockaddr_in);
833 sin6 = malloc(sizeof(struct sockaddr_in6));
836 sin6->sin6_family = AF_INET6;
837 sin6->sin6_port = htons(0);
838 sin6->sin6_addr = in6addr_any;
839 res->ai_addr = (struct sockaddr*) sin6;
840 res->ai_addrlen = (socklen_t)
841 sizeof(struct sockaddr_in6);
844 syslog(LOG_ERR, "bad addr fam %d",
849 if ((aicode = getaddrinfo(NULL, svcport_str,
850 &hints, &res)) != 0) {
852 "cannot get local address for %s: %s",
854 gai_strerror(aicode));
860 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
861 &hints, &res)) != 0) {
863 "cannot get local address for %s: %s",
864 nconf->nc_netid, gai_strerror(aicode));
871 sock_fd[sock_fdcnt - 1] = fd;
873 /* Now, attempt the bind. */
874 r = bindresvport_sa(fd, res->ai_addr);
876 if (errno == EADDRINUSE && mallocd_svcport != 0) {
877 if (mallocd_res != 0) {
884 syslog(LOG_ERR, "bindresvport_sa: %m");
888 if (svcport_str == NULL) {
889 svcport_str = malloc(NI_MAXSERV * sizeof(char));
890 if (svcport_str == NULL)
894 if (getnameinfo(res->ai_addr,
895 res->ai_addr->sa_len, NULL, NI_MAXHOST,
896 svcport_str, NI_MAXSERV * sizeof(char),
897 NI_NUMERICHOST | NI_NUMERICSERV))
898 errx(1, "Cannot get port number");
900 if (mallocd_res != 0) {
911 * Called after all the create_service() calls have succeeded, to complete
912 * the setup and registration.
915 complete_service(struct netconfig *nconf, char *port_str)
917 struct addrinfo hints, *res = NULL;
918 struct __rpc_sockinfo si;
919 struct netbuf servaddr;
920 SVCXPRT *transp = NULL;
921 int aicode, fd, nhostsbak;
924 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
925 (nconf->nc_semantics != NC_TPI_COTS) &&
926 (nconf->nc_semantics != NC_TPI_COTS_ORD))
927 return; /* not my type */
930 * XXX - using RPC library internal functions.
932 if (!__rpc_nconf2sockinfo(nconf, &si)) {
933 syslog(LOG_ERR, "cannot get information for %s",
939 while (nhostsbak > 0) {
941 if (sock_fdpos >= sock_fdcnt) {
942 /* Should never happen. */
943 syslog(LOG_ERR, "Ran out of socket fd's");
946 fd = sock_fd[sock_fdpos++];
951 * Using -1 tells listen(2) to use
952 * kern.ipc.soacceptqueue for the backlog.
954 if (nconf->nc_semantics != NC_TPI_CLTS)
957 if (nconf->nc_semantics == NC_TPI_CLTS )
958 transp = svc_dg_create(fd, 0, 0);
960 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
963 if (transp != (SVCXPRT *) NULL) {
964 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
967 "can't register %s MOUNTVERS service",
970 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
973 "can't register %s MOUNTVERS3 service",
977 syslog(LOG_WARNING, "can't create %s services",
980 if (registered == 0) {
982 memset(&hints, 0, sizeof hints);
983 hints.ai_flags = AI_PASSIVE;
984 hints.ai_family = si.si_af;
985 hints.ai_socktype = si.si_socktype;
986 hints.ai_protocol = si.si_proto;
988 if ((aicode = getaddrinfo(NULL, port_str, &hints,
990 syslog(LOG_ERR, "cannot get local address: %s",
991 gai_strerror(aicode));
995 servaddr.buf = malloc(res->ai_addrlen);
996 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
997 servaddr.len = res->ai_addrlen;
999 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
1000 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
1009 * Clear out sockets after a failure to bind one of them, so that the
1010 * cycle of socket creation/binding can start anew.
1013 clearout_service(void)
1017 for (i = 0; i < sock_fdcnt; i++) {
1018 if (sock_fd[i] >= 0) {
1019 shutdown(sock_fd[i], SHUT_RDWR);
1029 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
1030 "[-S] [-h <bindip>] [export_file ...]\n");
1035 * The mount rpc service
1038 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
1040 struct exportlist *ep;
1042 struct fhreturn fhr;
1045 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1046 int lookup_failed = 1;
1047 struct sockaddr *saddr;
1049 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1050 int bad = 0, defset, hostset;
1051 sigset_t sighup_mask;
1052 int numsecflavors, *secflavorsp;
1054 sigemptyset(&sighup_mask);
1055 sigaddset(&sighup_mask, SIGHUP);
1056 saddr = svc_getrpccaller(transp)->buf;
1057 switch (saddr->sa_family) {
1059 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1062 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1065 syslog(LOG_ERR, "request from unknown address family");
1068 switch (rqstp->rq_proc) {
1070 case MOUNTPROC_UMNT:
1071 case MOUNTPROC_UMNTALL:
1072 lookup_failed = getnameinfo(saddr, saddr->sa_len, host,
1073 sizeof host, NULL, 0, 0);
1075 getnameinfo(saddr, saddr->sa_len, numerichost,
1076 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1077 switch (rqstp->rq_proc) {
1079 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1080 syslog(LOG_ERR, "can't send reply");
1083 if (sport >= IPPORT_RESERVED && resvport_only) {
1085 "mount request from %s from unprivileged port",
1087 svcerr_weakauth(transp);
1090 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1091 syslog(LOG_NOTICE, "undecodable mount request from %s",
1093 svcerr_decode(transp);
1098 * Get the real pathname and make sure it is a directory
1099 * or a regular file if the -r option was specified
1102 if (realpath(rpcpath, dirpath) == NULL ||
1103 stat(dirpath, &stb) < 0 ||
1104 statfs(dirpath, &fsb) < 0) {
1105 chdir("/"); /* Just in case realpath doesn't */
1107 "mount request from %s for non existent path %s",
1108 numerichost, dirpath);
1110 warnx("stat failed on %s", dirpath);
1111 bad = ENOENT; /* We will send error reply later */
1114 !S_ISDIR(stb.st_mode) &&
1115 (dir_only || !S_ISREG(stb.st_mode))) {
1117 "mount request from %s for non-directory path %s",
1118 numerichost, dirpath);
1120 warnx("mounting non-directory %s", dirpath);
1121 bad = ENOTDIR; /* We will send error reply later */
1124 /* Check in the exports list */
1125 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1129 ep = ex_search(&fsb.f_fsid, exphead);
1130 hostset = defset = 0;
1131 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1132 &numsecflavors, &secflavorsp) ||
1133 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1134 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1136 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1137 scan_tree(ep->ex_dirl, saddr) == 0))) {
1139 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1141 syslog(LOG_ERR, "can't send reply");
1142 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1145 if (hostset & DP_HOSTSET) {
1146 fhr.fhr_flag = hostset;
1147 fhr.fhr_numsecflavors = numsecflavors;
1148 fhr.fhr_secflavors = secflavorsp;
1150 fhr.fhr_flag = defset;
1151 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1152 fhr.fhr_secflavors = ep->ex_defsecflavors;
1154 fhr.fhr_vers = rqstp->rq_vers;
1155 /* Get the file handle */
1156 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1157 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1159 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1160 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1162 syslog(LOG_ERR, "can't send reply");
1163 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1166 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1168 syslog(LOG_ERR, "can't send reply");
1170 add_mlist(host, dirpath);
1172 add_mlist(numerichost, dirpath);
1174 warnx("mount successful");
1177 "mount request succeeded from %s for %s",
1178 numerichost, dirpath);
1183 "mount request denied from %s for %s",
1184 numerichost, dirpath);
1187 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1189 syslog(LOG_ERR, "can't send reply");
1190 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1192 case MOUNTPROC_DUMP:
1193 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1194 syslog(LOG_ERR, "can't send reply");
1197 "dump request succeeded from %s",
1200 case MOUNTPROC_UMNT:
1201 if (sport >= IPPORT_RESERVED && resvport_only) {
1203 "umount request from %s from unprivileged port",
1205 svcerr_weakauth(transp);
1208 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1209 syslog(LOG_NOTICE, "undecodable umount request from %s",
1211 svcerr_decode(transp);
1214 if (realpath(rpcpath, dirpath) == NULL) {
1215 syslog(LOG_NOTICE, "umount request from %s "
1216 "for non existent path %s",
1217 numerichost, dirpath);
1219 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1220 syslog(LOG_ERR, "can't send reply");
1222 del_mlist(host, dirpath);
1223 del_mlist(numerichost, dirpath);
1226 "umount request succeeded from %s for %s",
1227 numerichost, dirpath);
1229 case MOUNTPROC_UMNTALL:
1230 if (sport >= IPPORT_RESERVED && resvport_only) {
1232 "umountall request from %s from unprivileged port",
1234 svcerr_weakauth(transp);
1237 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1238 syslog(LOG_ERR, "can't send reply");
1240 del_mlist(host, NULL);
1241 del_mlist(numerichost, NULL);
1244 "umountall request succeeded from %s",
1247 case MOUNTPROC_EXPORT:
1248 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1249 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1251 syslog(LOG_ERR, "can't send reply");
1254 "export request succeeded from %s",
1258 svcerr_noproc(transp);
1264 * Xdr conversion for a dirpath string
1267 xdr_dir(XDR *xdrsp, char *dirp)
1269 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1273 * Xdr routine to generate file handle reply
1276 xdr_fhs(XDR *xdrsp, caddr_t cp)
1278 struct fhreturn *fhrp = (struct fhreturn *)cp;
1279 u_long ok = 0, len, auth;
1282 if (!xdr_long(xdrsp, &ok))
1284 switch (fhrp->fhr_vers) {
1286 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1289 if (!xdr_long(xdrsp, &len))
1291 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1293 if (fhrp->fhr_numsecflavors) {
1294 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1296 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1297 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1303 if (!xdr_long(xdrsp, &len))
1305 return (xdr_long(xdrsp, &auth));
1312 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1314 struct mountlist *mlp;
1319 SLIST_FOREACH(mlp, &mlhead, next) {
1320 if (!xdr_bool(xdrsp, &true))
1322 strp = &mlp->ml_host[0];
1323 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1325 strp = &mlp->ml_dirp[0];
1326 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1329 if (!xdr_bool(xdrsp, &false))
1335 * Xdr conversion for export list
1338 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1340 struct exportlist *ep;
1343 sigset_t sighup_mask;
1346 sigemptyset(&sighup_mask);
1347 sigaddset(&sighup_mask, SIGHUP);
1348 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1350 for (i = 0; i < exphashsize; i++)
1351 SLIST_FOREACH(ep, &exphead[i], entries) {
1353 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1356 if (ep->ex_defdir && putdef == 0 &&
1357 put_exlist(ep->ex_defdir, xdrsp, NULL,
1361 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1362 if (!xdr_bool(xdrsp, &false))
1366 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1371 * Called from xdr_explist() to traverse the tree and export the
1375 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1378 struct grouplist *grp;
1379 struct hostlist *hp;
1386 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1388 if (!xdr_bool(xdrsp, &true))
1391 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1393 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1398 if (!xdr_bool(xdrsp, &true))
1401 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1403 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1404 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1408 if (grp->gr_type == GT_HOST) {
1409 if (!xdr_bool(xdrsp, &true))
1411 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1412 if (!xdr_string(xdrsp, &strp,
1415 } else if (grp->gr_type == GT_NET) {
1416 if (!xdr_bool(xdrsp, &true))
1418 strp = grp->gr_ptr.gt_net.nt_name;
1419 if (!xdr_string(xdrsp, &strp,
1424 if (gotalldir && hp == (struct hostlist *)NULL) {
1430 if (!xdr_bool(xdrsp, &false))
1432 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1439 xdr_explist(XDR *xdrsp, caddr_t cp)
1442 return xdr_explist_common(xdrsp, cp, 0);
1446 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1449 return xdr_explist_common(xdrsp, cp, 1);
1453 static size_t linesize;
1454 static FILE *exp_file;
1457 * Get the export list from one, currently open file
1460 get_exportlist_one(int passno)
1462 struct exportlist *ep;
1463 struct grouplist *grp, *tgrp, *savgrp;
1464 struct dirlist *dirhead;
1467 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1468 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1471 dirhead = (struct dirlist *)NULL;
1472 while (get_line()) {
1474 warnx("got line %s", line);
1476 nextfield(&cp, &endcp);
1485 exflags = MNT_EXPORTED;
1488 ep = (struct exportlist *)NULL;
1492 * Handle the V4 root dir.
1494 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1496 * V4: just indicates that it is the v4 root point,
1497 * so skip over that and set v4root_phase.
1499 if (v4root_phase > 0) {
1500 syslog(LOG_ERR, "V4:duplicate line, ignored");
1505 nextfield(&cp, &endcp);
1509 * Create new exports list entry
1512 tgrp = grp = get_grp();
1514 if (len > MNTNAMLEN) {
1515 getexp_err(ep, tgrp, "mountpoint too long");
1519 if (ep == (struct exportlist *)NULL) {
1520 getexp_err(ep, tgrp,
1521 "flag before export path definition");
1525 warnx("doing opt %s", cp);
1527 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1529 getexp_err(ep, tgrp, NULL);
1532 } else if (*cp == '/') {
1535 if (v4root_phase > 1) {
1537 getexp_err(ep, tgrp, "Multiple V4 dirs");
1541 if (check_dirpath(cp) &&
1542 statfs(cp, &fsb) >= 0) {
1543 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1544 syslog(LOG_ERR, "Warning: exporting of "
1545 "automounted fs %s not supported", cp);
1547 getexp_err(ep, tgrp, "dirs must be first");
1550 if (v4root_phase == 1) {
1552 getexp_err(ep, tgrp, "Multiple V4 dirs");
1555 if (strlen(v4root_dirpath) == 0) {
1556 strlcpy(v4root_dirpath, cp,
1557 sizeof (v4root_dirpath));
1558 } else if (strcmp(v4root_dirpath, cp)
1561 "different V4 dirpath %s", cp);
1562 getexp_err(ep, tgrp, NULL);
1571 if (fsidcmp(&ep->ex_fs, &fsb.f_fsid)
1573 getexp_err(ep, tgrp,
1579 * See if this directory is already
1582 ep = ex_search(&fsb.f_fsid, exphead);
1583 if (ep == (struct exportlist *)NULL) {
1585 ep->ex_fs = fsb.f_fsid;
1586 ep->ex_fsdir = strdup(fsb.f_mntonname);
1587 if (ep->ex_fsdir == NULL)
1591 "making new ep fs=0x%x,0x%x",
1595 warnx("found ep fs=0x%x,0x%x",
1601 * Add dirpath to export mount point.
1603 dirp = add_expdir(&dirhead, cp, len);
1607 getexp_err(ep, tgrp,
1608 "symbolic link in export path or statfs failed");
1616 if (ep == (struct exportlist *)NULL) {
1617 getexp_err(ep, tgrp,
1618 "host(s) before export path definition");
1623 * Get the host or netgroup.
1626 netgrp = getnetgrent(&hst, &usr, &dom);
1629 grp->gr_next = get_grp();
1635 "null hostname in netgroup %s, skipping", cp);
1636 grp->gr_type = GT_IGNORE;
1637 } else if (get_host(hst, grp, tgrp)) {
1639 "bad host %s in netgroup %s, skipping", hst, cp);
1640 grp->gr_type = GT_IGNORE;
1642 } else if (get_host(cp, grp, tgrp)) {
1643 syslog(LOG_ERR, "bad host %s, skipping", cp);
1644 grp->gr_type = GT_IGNORE;
1647 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1652 nextfield(&cp, &endcp);
1655 if (check_options(dirhead)) {
1656 getexp_err(ep, tgrp, NULL);
1660 grp->gr_type = GT_DEFAULT;
1662 warnx("adding a default entry");
1665 * Don't allow a network export coincide with a list of
1666 * host(s) on the same line.
1668 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1669 getexp_err(ep, tgrp, "network/host conflict");
1673 * If an export list was specified on this line, make sure
1674 * that we have at least one valid entry, otherwise skip it.
1678 while (grp && grp->gr_type == GT_IGNORE)
1681 getexp_err(ep, tgrp, "no valid entries");
1686 if (v4root_phase == 1) {
1687 getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1692 * Loop through hosts, pushing the exports into the kernel.
1693 * After loop, tgrp points to the start of the list and
1694 * grp points to the last entry in the list.
1695 * Do not do the do_mount() for passno == 1, since the
1696 * second pass will do it, as required.
1700 grp->gr_exflags = exflags;
1701 grp->gr_anon = anon;
1702 if (v4root_phase == 2 && passno == 0)
1703 LOGDEBUG("do_mount v4root");
1704 if (passno == 0 && do_mount(ep, grp, exflags, &anon,
1705 dirp, dirplen, &fsb, ep->ex_numsecflavors,
1706 ep->ex_secflavors)) {
1707 getexp_err(ep, tgrp, NULL);
1710 } while (grp->gr_next && (grp = grp->gr_next));
1713 * For V4: don't enter in mount lists.
1715 if (v4root_phase > 0 && v4root_phase <= 2) {
1717 * These structures are used for the reload,
1718 * so save them for that case. Otherwise, just
1721 if (passno == 1 && ep != NULL) {
1723 while (tgrp != NULL) {
1725 * Save the security flavors and exflags
1726 * for this host set in the groups.
1728 tgrp->gr_numsecflavors =
1729 ep->ex_numsecflavors;
1730 if (ep->ex_numsecflavors > 0)
1731 memcpy(tgrp->gr_secflavors,
1733 sizeof(ep->ex_secflavors));
1734 tgrp = tgrp->gr_next;
1736 if (v4root_ep == NULL) {
1738 ep = NULL; /* Don't free below. */
1740 grp->gr_next = v4root_ep->ex_grphead;
1741 v4root_ep->ex_grphead = savgrp;
1745 while (tgrp != NULL) {
1747 tgrp = tgrp->gr_next;
1754 * Success. Update the data structures.
1757 hang_dirp(dirhead, tgrp, ep, opt_flags, &anon, exflags);
1758 grp->gr_next = ep->ex_grphead;
1759 ep->ex_grphead = tgrp;
1761 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1762 opt_flags, &anon, exflags);
1765 dirhead = (struct dirlist *)NULL;
1766 if ((ep->ex_flag & EX_LINKED) == 0) {
1767 insert_exports(ep, exphead);
1769 ep->ex_flag |= EX_LINKED;
1775 dirhead = (struct dirlist *)NULL;
1781 * Get the export list from all specified files
1784 get_exportlist(int passno)
1786 struct export_args export;
1788 struct statfs *mntbufp;
1792 struct nfsex_args eargs;
1795 if ((debug_file = fopen(_PATH_MOUNTDDEBUG, "r")) != NULL) {
1800 LOGDEBUG("passno=%d", passno);
1801 v4root_dirpath[0] = '\0';
1805 * Save the current lists as old ones, so that the new lists
1806 * can be compared with the old ones in the 2nd pass.
1808 for (i = 0; i < exphashsize; i++) {
1809 SLIST_FIRST(&oldexphead[i]) = SLIST_FIRST(&exphead[i]);
1810 SLIST_INIT(&exphead[i]);
1813 /* Note that the public fh has not yet been set. */
1814 has_set_publicfh = 0;
1816 /* Read the export file(s) and process them */
1817 read_exportfile(passno);
1820 * Just make the old lists empty.
1821 * exphashsize == 0 for the first call, before oldexphead
1822 * has been initialized-->loop won't be executed.
1824 for (i = 0; i < exphashsize; i++)
1825 SLIST_INIT(&oldexphead[i]);
1828 bzero(&export, sizeof(export));
1829 export.ex_flags = MNT_DELEXPORT;
1832 bzero(errmsg, sizeof(errmsg));
1834 if (suspend_nfsd != 0)
1835 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1837 * Delete the old V4 root dir.
1839 bzero(&eargs, sizeof (eargs));
1840 eargs.export.ex_flags = MNT_DELEXPORT;
1841 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1843 syslog(LOG_ERR, "Can't delete exports for V4:");
1845 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1846 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1847 build_iovec(&iov, &iovlen, "from", NULL, 0);
1848 build_iovec(&iov, &iovlen, "update", NULL, 0);
1849 build_iovec(&iov, &iovlen, "export", &export,
1851 build_iovec(&iov, &iovlen, "errmsg", errmsg,
1855 * For passno == 1, compare the old and new lists updating the kernel
1856 * exports for any cases that have changed.
1857 * This call is doing the second pass through the lists.
1858 * If it fails, fall back on the bulk reload.
1860 if (passno == 1 && compare_nmount_exportlist(iov, iovlen, errmsg) ==
1862 LOGDEBUG("compareok");
1863 /* Free up the old lists. */
1864 free_exports(oldexphead);
1866 LOGDEBUG("doing passno=0");
1868 * Clear flag that notes if a public fh has been exported.
1869 * It is set by do_mount() if MNT_EXPUBLIC is set for the entry.
1873 /* exphead == NULL if not yet allocated (first call). */
1874 if (exphead != NULL) {
1876 * First, get rid of the old lists.
1878 free_exports(exphead);
1879 free_exports(oldexphead);
1883 * And delete exports that are in the kernel for all local
1885 * XXX: Should know how to handle all local exportable
1888 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1890 /* Allocate hash tables, for first call. */
1891 if (exphead == NULL) {
1892 /* Target an average linked list length of 10. */
1893 exphashsize = num / 10;
1894 if (exphashsize < 1)
1896 else if (exphashsize > 100000)
1897 exphashsize = 100000;
1898 exphead = malloc(exphashsize * sizeof(*exphead));
1899 oldexphead = malloc(exphashsize * sizeof(*oldexphead));
1900 if (exphead == NULL || oldexphead == NULL)
1901 errx(1, "Can't malloc hash tables");
1903 for (i = 0; i < exphashsize; i++) {
1904 SLIST_INIT(&exphead[i]);
1905 SLIST_INIT(&oldexphead[i]);
1909 for (i = 0; i < num; i++)
1910 delete_export(iov, iovlen, &mntbufp[i], errmsg);
1913 /* Read the export file(s) and process them */
1918 /* Free strings allocated by strdup() in getmntopts.c */
1919 free(iov[0].iov_base); /* fstype */
1920 free(iov[2].iov_base); /* fspath */
1921 free(iov[4].iov_base); /* from */
1922 free(iov[6].iov_base); /* update */
1923 free(iov[8].iov_base); /* export */
1924 free(iov[10].iov_base); /* errmsg */
1926 /* free iov, allocated by realloc() */
1932 * If there was no public fh, clear any previous one set.
1934 if (has_publicfh == 0) {
1935 LOGDEBUG("clear public fh");
1936 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1939 /* Resume the nfsd. If they weren't suspended, this is harmless. */
1940 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1941 LOGDEBUG("eo get_exportlist");
1945 * Insert an export entry in the appropriate list.
1948 insert_exports(struct exportlist *ep, struct exportlisthead *exhp)
1952 i = EXPHASH(&ep->ex_fs);
1953 LOGDEBUG("fs=%s hash=%i", ep->ex_fsdir, i);
1954 SLIST_INSERT_HEAD(&exhp[i], ep, entries);
1958 * Free up the exports lists passed in as arguments.
1961 free_exports(struct exportlisthead *exhp)
1963 struct exportlist *ep, *ep2;
1966 for (i = 0; i < exphashsize; i++) {
1967 SLIST_FOREACH_SAFE(ep, &exhp[i], entries, ep2) {
1968 SLIST_REMOVE(&exhp[i], ep, exportlist, entries);
1971 SLIST_INIT(&exhp[i]);
1976 * Read the exports file(s) and call get_exportlist_one() for each line.
1979 read_exportfile(int passno)
1984 * Read in the exports file and build the list, calling
1985 * nmount() as we go along to push the export rules into the kernel.
1988 for (i = 0; exnames[i] != NULL; i++) {
1990 warnx("reading exports from %s", exnames[i]);
1991 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1992 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1995 get_exportlist_one(passno);
2000 syslog(LOG_ERR, "can't open any exports file");
2006 * Compare the export lists against the old ones and do nmount() operations
2007 * for any cases that have changed. This avoids doing nmount() for entries
2008 * that have not changed.
2009 * Return 0 upon success, 1 otherwise.
2012 compare_nmount_exportlist(struct iovec *iov, int iovlen, char *errmsg)
2014 struct exportlist *ep, *oep;
2015 struct grouplist *grp;
2016 struct statfs fs, ofs;
2020 * Loop through the current list and look for an entry in the old
2022 * If found, check to see if it the same.
2023 * If it is not the same, delete and re-export.
2024 * Then mark it done on the old list.
2027 * Any entries left in the old list after processing must have their
2030 for (i = 0; i < exphashsize; i++)
2031 SLIST_FOREACH(ep, &exphead[i], entries) {
2032 LOGDEBUG("foreach ep=%s", ep->ex_fsdir);
2033 oep = ex_search(&ep->ex_fs, oldexphead);
2036 * Check the mount paths are the same.
2037 * If not, return 1 so that the reload of the
2038 * exports will be done in bulk, the
2041 LOGDEBUG("found old exp");
2042 if (strcmp(ep->ex_fsdir, oep->ex_fsdir) != 0)
2044 LOGDEBUG("same fsdir");
2046 * Test to see if the entry is the same.
2047 * If not the same delete exports and
2050 if (compare_export(ep, oep) != 0) {
2052 * Clear has_publicfh if if was set
2053 * in the old exports, but only if it
2054 * has not been set during processing of
2055 * the exports for this pass, as
2056 * indicated by has_set_publicfh.
2058 if (has_set_publicfh == 0 &&
2059 (oep->ex_flag & EX_PUBLICFH) != 0)
2062 /* Delete and re-export. */
2063 if (statfs(ep->ex_fsdir, &fs) < 0)
2065 delete_export(iov, iovlen, &fs, errmsg);
2066 ret = do_export_mount(ep, &fs);
2070 oep->ex_flag |= EX_DONE;
2073 LOGDEBUG("not found so export");
2074 /* Not found, so do export. */
2075 if (statfs(ep->ex_fsdir, &fs) < 0)
2077 ret = do_export_mount(ep, &fs);
2083 /* Delete exports not done. */
2084 for (i = 0; i < exphashsize; i++)
2085 SLIST_FOREACH(oep, &oldexphead[i], entries) {
2086 if ((oep->ex_flag & EX_DONE) == 0) {
2087 LOGDEBUG("not done delete=%s", oep->ex_fsdir);
2088 if (statfs(oep->ex_fsdir, &ofs) >= 0 &&
2089 fsidcmp(&oep->ex_fs, &ofs.f_fsid) == 0) {
2090 LOGDEBUG("do delete");
2092 * Clear has_publicfh if if was set
2093 * in the old exports, but only if it
2094 * has not been set during processing of
2095 * the exports for this pass, as
2096 * indicated by has_set_publicfh.
2098 if (has_set_publicfh == 0 &&
2099 (oep->ex_flag & EX_PUBLICFH) != 0)
2102 delete_export(iov, iovlen, &ofs,
2108 /* Do the V4 root exports, as required. */
2110 if (v4root_ep != NULL)
2111 grp = v4root_ep->ex_grphead;
2113 while (v4root_ep != NULL && grp != NULL) {
2114 LOGDEBUG("v4root expath=%s", v4root_dirpath);
2115 ret = do_mount(v4root_ep, grp, grp->gr_exflags, &grp->gr_anon,
2116 v4root_dirpath, strlen(v4root_dirpath), &fs,
2117 grp->gr_numsecflavors, grp->gr_secflavors);
2130 * Compare old and current exportlist entries for the fsid and return 0
2131 * if they are the same, 1 otherwise.
2134 compare_export(struct exportlist *ep, struct exportlist *oep)
2136 struct grouplist *grp, *ogrp;
2138 if (strcmp(ep->ex_fsdir, oep->ex_fsdir) != 0)
2140 if ((ep->ex_flag & EX_DEFSET) != (oep->ex_flag & EX_DEFSET))
2142 if ((ep->ex_defdir != NULL && oep->ex_defdir == NULL) ||
2143 (ep->ex_defdir == NULL && oep->ex_defdir != NULL))
2145 if (ep->ex_defdir != NULL && (ep->ex_defdir->dp_flag & DP_DEFSET) !=
2146 (oep->ex_defdir->dp_flag & DP_DEFSET))
2148 if ((ep->ex_flag & EX_DEFSET) != 0 && (ep->ex_defnumsecflavors !=
2149 oep->ex_defnumsecflavors || ep->ex_defexflags !=
2150 oep->ex_defexflags || compare_cred(&ep->ex_defanon,
2151 &oep->ex_defanon) != 0 || compare_secflavor(ep->ex_defsecflavors,
2152 oep->ex_defsecflavors, ep->ex_defnumsecflavors) != 0))
2155 /* Now, check all the groups. */
2156 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp = ogrp->gr_next)
2158 for (grp = ep->ex_grphead; grp != NULL; grp = grp->gr_next) {
2159 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp =
2161 if ((ogrp->gr_flag & GR_FND) == 0 &&
2162 grp->gr_numsecflavors == ogrp->gr_numsecflavors &&
2163 grp->gr_exflags == ogrp->gr_exflags &&
2164 compare_cred(&grp->gr_anon, &ogrp->gr_anon) == 0 &&
2165 compare_secflavor(grp->gr_secflavors,
2166 ogrp->gr_secflavors, grp->gr_numsecflavors) == 0)
2169 ogrp->gr_flag |= GR_FND;
2173 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp = ogrp->gr_next)
2174 if ((ogrp->gr_flag & GR_FND) == 0)
2180 * This algorithm compares two arrays of "n" items. It returns 0 if they are
2181 * the "same" and 1 otherwise. Although suboptimal, it is always safe to
2182 * return 1, which makes compare_nmount_export() reload the exports entry.
2183 * "same" refers to having the same set of values in the two arrays.
2184 * The arrays are in no particular order and duplicates (multiple entries
2185 * in an array with the same value) is allowed.
2186 * The algorithm is inefficient, but the common case of indentical arrays is
2187 * handled first and "n" is normally fairly small.
2188 * Since the two functions need the same algorithm but for arrays of
2189 * different types (gid_t vs int), this is done as a macro.
2191 #define COMPARE_ARRAYS(a1, a2, n) \
2193 int fnd, fndarray[(n)], i, j; \
2194 /* Handle common case of identical arrays. */ \
2195 for (i = 0; i < (n); i++) \
2196 if ((a1)[i] != (a2)[i]) \
2200 for (i = 0; i < (n); i++) \
2202 for (i = 0; i < (n); i++) { \
2204 for (j = 0; j < (n); j++) { \
2205 if ((a1)[i] == (a2)[j]) { \
2213 for (i = 0; i < (n); i++) \
2214 if (fndarray[i] == 0) \
2220 * Compare to struct xucred's. Return 0 if the same and 1 otherwise.
2223 compare_cred(struct xucred *cr0, struct xucred *cr1)
2226 if (cr0->cr_uid != cr1->cr_uid || cr0->cr_ngroups != cr1->cr_ngroups)
2229 COMPARE_ARRAYS(cr0->cr_groups, cr1->cr_groups, cr0->cr_ngroups);
2233 * Compare two lists of security flavors. Return 0 if the same and 1 otherwise.
2236 compare_secflavor(int *sec1, int *sec2, int nsec)
2239 COMPARE_ARRAYS(sec1, sec2, nsec);
2243 * Delete an exports entry.
2246 delete_export(struct iovec *iov, int iovlen, struct statfs *fsp, char *errmsg)
2248 struct xvfsconf vfc;
2250 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
2251 syslog(LOG_ERR, "getvfsbyname() failed for %s",
2257 * We do not need to delete "export" flag from
2258 * filesystems that do not have it set.
2260 if (!(fsp->f_flags & MNT_EXPORTED))
2263 * Do not delete export for network filesystem by
2264 * passing "export" arg to nmount().
2265 * It only makes sense to do this for local filesystems.
2267 if (vfc.vfc_flags & VFCF_NETWORK)
2270 iov[1].iov_base = fsp->f_fstypename;
2271 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
2272 iov[3].iov_base = fsp->f_mntonname;
2273 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
2274 iov[5].iov_base = fsp->f_mntfromname;
2275 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
2279 * EXDEV is returned when path exists but is not a
2280 * mount point. May happens if raced with unmount.
2282 if (nmount(iov, iovlen, fsp->f_flags) < 0 && errno != ENOENT &&
2283 errno != ENOTSUP && errno != EXDEV) {
2285 "can't delete exports for %s: %m %s",
2286 fsp->f_mntonname, errmsg);
2291 * Allocate an export list element
2293 static struct exportlist *
2296 struct exportlist *ep;
2298 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
2299 if (ep == (struct exportlist *)NULL)
2305 * Allocate a group list element
2307 static struct grouplist *
2310 struct grouplist *gp;
2312 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
2313 if (gp == (struct grouplist *)NULL)
2319 * Clean up upon an error in get_exportlist().
2322 getexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
2324 struct grouplist *tgrp;
2326 if (!(opt_flags & OP_QUIET)) {
2328 syslog(LOG_ERR, "bad exports list line '%s': %s", line,
2331 syslog(LOG_ERR, "bad exports list line '%s'", line);
2333 if (ep && (ep->ex_flag & EX_LINKED) == 0)
2343 * Search the export list for a matching fs.
2345 static struct exportlist *
2346 ex_search(fsid_t *fsid, struct exportlisthead *exhp)
2348 struct exportlist *ep;
2352 SLIST_FOREACH(ep, &exhp[i], entries) {
2353 if (fsidcmp(&ep->ex_fs, fsid) == 0)
2361 * Add a directory path to the list.
2364 add_expdir(struct dirlist **dpp, char *cp, int len)
2368 dp = malloc(sizeof (struct dirlist));
2369 if (dp == (struct dirlist *)NULL)
2372 dp->dp_right = (struct dirlist *)NULL;
2374 dp->dp_hosts = (struct hostlist *)NULL;
2375 dp->dp_dirp = strndup(cp, len);
2376 if (dp->dp_dirp == NULL)
2379 return (dp->dp_dirp);
2383 * Hang the dir list element off the dirpath binary tree as required
2384 * and update the entry for host.
2387 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
2388 int flags, struct xucred *anoncrp, int exflags)
2390 struct hostlist *hp;
2391 struct dirlist *dp2;
2393 if (flags & OP_ALLDIRS) {
2398 if (grp == (struct grouplist *)NULL) {
2399 ep->ex_flag |= EX_DEFSET;
2400 ep->ex_defdir->dp_flag |= DP_DEFSET;
2401 /* Save the default security flavors list. */
2402 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2403 if (ep->ex_numsecflavors > 0)
2404 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2405 sizeof(ep->ex_secflavors));
2406 ep->ex_defanon = *anoncrp;
2407 ep->ex_defexflags = exflags;
2408 } else while (grp) {
2411 hp->ht_next = ep->ex_defdir->dp_hosts;
2412 ep->ex_defdir->dp_hosts = hp;
2413 /* Save the security flavors list for this host set. */
2414 grp->gr_numsecflavors = ep->ex_numsecflavors;
2415 if (ep->ex_numsecflavors > 0)
2416 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2417 sizeof(ep->ex_secflavors));
2423 * Loop through the directories adding them to the tree.
2427 add_dlist(&ep->ex_dirl, dp, grp, flags, ep, anoncrp,
2435 * Traverse the binary tree either updating a node that is already there
2436 * for the new directory or adding the new node.
2439 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2440 int flags, struct exportlist *ep, struct xucred *anoncrp, int exflags)
2443 struct hostlist *hp;
2448 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2450 add_dlist(&dp->dp_left, newdp, grp, flags, ep, anoncrp,
2453 } else if (cmp < 0) {
2454 add_dlist(&dp->dp_right, newdp, grp, flags, ep, anoncrp,
2458 free((caddr_t)newdp);
2461 dp->dp_left = (struct dirlist *)NULL;
2467 * Hang all of the host(s) off of the directory point.
2472 hp->ht_next = dp->dp_hosts;
2474 /* Save the security flavors list for this host set. */
2475 grp->gr_numsecflavors = ep->ex_numsecflavors;
2476 if (ep->ex_numsecflavors > 0)
2477 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2478 sizeof(ep->ex_secflavors));
2482 ep->ex_flag |= EX_DEFSET;
2483 dp->dp_flag |= DP_DEFSET;
2484 /* Save the default security flavors list. */
2485 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2486 if (ep->ex_numsecflavors > 0)
2487 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2488 sizeof(ep->ex_secflavors));
2489 ep->ex_defanon = *anoncrp;
2490 ep->ex_defexflags = exflags;
2495 * Search for a dirpath on the export point.
2497 static struct dirlist *
2498 dirp_search(struct dirlist *dp, char *dirp)
2503 cmp = strcmp(dp->dp_dirp, dirp);
2505 return (dirp_search(dp->dp_left, dirp));
2507 return (dirp_search(dp->dp_right, dirp));
2515 * Scan for a host match in a directory tree.
2518 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2519 int *hostsetp, int *numsecflavors, int **secflavorsp)
2521 struct hostlist *hp;
2522 struct grouplist *grp;
2523 struct addrinfo *ai;
2526 if (dp->dp_flag & DP_DEFSET)
2527 *defsetp = dp->dp_flag;
2531 switch (grp->gr_type) {
2533 ai = grp->gr_ptr.gt_addrinfo;
2534 for (; ai; ai = ai->ai_next) {
2535 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2537 (hp->ht_flag | DP_HOSTSET);
2538 if (numsecflavors != NULL) {
2540 grp->gr_numsecflavors;
2549 if (!sacmp(saddr, (struct sockaddr *)
2550 &grp->gr_ptr.gt_net.nt_net,
2552 &grp->gr_ptr.gt_net.nt_mask)) {
2553 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2554 if (numsecflavors != NULL) {
2556 grp->gr_numsecflavors;
2571 * Scan tree for a host that matches the address.
2574 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2576 int defset, hostset;
2579 if (scan_tree(dp->dp_left, saddr))
2581 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2583 if (scan_tree(dp->dp_right, saddr))
2590 * Traverse the dirlist tree and free it up.
2593 free_dir(struct dirlist *dp)
2597 free_dir(dp->dp_left);
2598 free_dir(dp->dp_right);
2599 free_host(dp->dp_hosts);
2606 * Parse a colon separated list of security flavors
2609 parsesec(char *seclist, struct exportlist *ep)
2614 ep->ex_numsecflavors = 0;
2616 cp = strchr(seclist, ':');
2622 if (!strcmp(seclist, "sys"))
2624 else if (!strcmp(seclist, "krb5"))
2625 flavor = RPCSEC_GSS_KRB5;
2626 else if (!strcmp(seclist, "krb5i"))
2627 flavor = RPCSEC_GSS_KRB5I;
2628 else if (!strcmp(seclist, "krb5p"))
2629 flavor = RPCSEC_GSS_KRB5P;
2633 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2636 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2639 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2642 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2643 ep->ex_numsecflavors++;
2655 * Parse the option string and update fields.
2656 * Option arguments may either be -<option>=<value> or
2660 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2661 int *has_hostp, int *exflagsp, struct xucred *cr)
2663 char *cpoptarg, *cpoptend;
2664 char *cp, *endcp, *cpopt, savedc, savedc2;
2665 int allflag, usedarg;
2673 while (cpopt && *cpopt) {
2676 if ((cpoptend = strchr(cpopt, ','))) {
2678 if ((cpoptarg = strchr(cpopt, '=')))
2681 if ((cpoptarg = strchr(cpopt, '=')))
2685 nextfield(&cp, &endcp);
2687 if (endcp > cp && *cp != '-') {
2695 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2696 *exflagsp |= MNT_EXRDONLY;
2697 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2698 !(allflag = strcmp(cpopt, "mapall")) ||
2699 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2701 parsecred(cpoptarg, cr);
2703 *exflagsp |= MNT_EXPORTANON;
2704 opt_flags |= OP_MAPALL;
2706 opt_flags |= OP_MAPROOT;
2707 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2708 !strcmp(cpopt, "m"))) {
2709 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2710 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2714 opt_flags |= OP_MASK;
2715 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2716 !strcmp(cpopt, "n"))) {
2717 if (strchr(cpoptarg, '/') != NULL) {
2719 fprintf(stderr, "setting OP_MASKLEN\n");
2720 opt_flags |= OP_MASKLEN;
2722 if (grp->gr_type != GT_NULL) {
2723 syslog(LOG_ERR, "network/host conflict");
2725 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2726 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2729 grp->gr_type = GT_NET;
2732 opt_flags |= OP_NET;
2733 } else if (!strcmp(cpopt, "alldirs")) {
2734 opt_flags |= OP_ALLDIRS;
2735 } else if (!strcmp(cpopt, "public")) {
2736 *exflagsp |= MNT_EXPUBLIC;
2737 } else if (!strcmp(cpopt, "webnfs")) {
2738 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2739 opt_flags |= OP_MAPALL;
2740 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2741 ep->ex_indexfile = strdup(cpoptarg);
2742 } else if (!strcmp(cpopt, "quiet")) {
2743 opt_flags |= OP_QUIET;
2744 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2745 if (parsesec(cpoptarg, ep))
2747 opt_flags |= OP_SEC;
2750 syslog(LOG_ERR, "bad opt %s", cpopt);
2769 * Translate a character string to the corresponding list of network
2770 * addresses for a hostname.
2773 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2775 struct grouplist *checkgrp;
2776 struct addrinfo *ai, *tai, hints;
2778 char host[NI_MAXHOST];
2780 if (grp->gr_type != GT_NULL) {
2781 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2784 memset(&hints, 0, sizeof hints);
2785 hints.ai_flags = AI_CANONNAME;
2786 hints.ai_protocol = IPPROTO_UDP;
2787 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2789 syslog(LOG_ERR,"can't get address info for host %s", cp);
2792 grp->gr_ptr.gt_addrinfo = ai;
2793 while (ai != NULL) {
2794 if (ai->ai_canonname == NULL) {
2795 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2796 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2797 strlcpy(host, "?", sizeof(host));
2798 ai->ai_canonname = strdup(host);
2799 ai->ai_flags |= AI_CANONNAME;
2802 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2804 * Sanity check: make sure we don't already have an entry
2805 * for this host in the grouplist.
2807 for (checkgrp = tgrp; checkgrp != NULL;
2808 checkgrp = checkgrp->gr_next) {
2809 if (checkgrp->gr_type != GT_HOST)
2811 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2812 tai = tai->ai_next) {
2813 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2817 "ignoring duplicate host %s\n",
2819 grp->gr_type = GT_IGNORE;
2825 grp->gr_type = GT_HOST;
2830 * Free up an exports list component
2833 free_exp(struct exportlist *ep)
2835 struct grouplist *grp, *tgrp;
2837 if (ep->ex_defdir) {
2838 free_host(ep->ex_defdir->dp_hosts);
2839 free((caddr_t)ep->ex_defdir);
2843 if (ep->ex_indexfile)
2844 free(ep->ex_indexfile);
2845 free_dir(ep->ex_dirl);
2846 grp = ep->ex_grphead;
2856 * Free up the v4root exports.
2859 free_v4rootexp(void)
2862 if (v4root_ep != NULL) {
2863 free_exp(v4root_ep);
2872 free_host(struct hostlist *hp)
2874 struct hostlist *hp2;
2883 static struct hostlist *
2886 struct hostlist *hp;
2888 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2889 if (hp == (struct hostlist *)NULL)
2891 hp->ht_next = (struct hostlist *)NULL;
2897 * Out of memory, fatal
2903 syslog(LOG_ERR, "out of memory");
2908 * Call do_mount() from the struct exportlist, for each case needed.
2911 do_export_mount(struct exportlist *ep, struct statfs *fsp)
2913 struct grouplist *grp, defgrp;
2917 LOGDEBUG("do_export_mount=%s", ep->ex_fsdir);
2918 dirlen = strlen(ep->ex_fsdir);
2919 if ((ep->ex_flag & EX_DEFSET) != 0) {
2920 defgrp.gr_type = GT_DEFAULT;
2921 defgrp.gr_next = NULL;
2922 /* We have an entry for all other hosts/nets. */
2923 LOGDEBUG("ex_defexflags=0x%x", ep->ex_defexflags);
2924 ret = do_mount(ep, &defgrp, ep->ex_defexflags, &ep->ex_defanon,
2925 ep->ex_fsdir, dirlen, fsp, ep->ex_defnumsecflavors,
2926 ep->ex_defsecflavors);
2931 /* Do a mount for each group. */
2932 grp = ep->ex_grphead;
2933 while (grp != NULL) {
2934 LOGDEBUG("do mount gr_type=0x%x gr_exflags=0x%x",
2935 grp->gr_type, grp->gr_exflags);
2936 ret = do_mount(ep, grp, grp->gr_exflags, &grp->gr_anon,
2937 ep->ex_fsdir, dirlen, fsp, grp->gr_numsecflavors,
2938 grp->gr_secflavors);
2947 * Do the nmount() syscall with the update flag to push the export info into
2951 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2952 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb,
2953 int numsecflavors, int *secflavors)
2956 struct addrinfo *ai;
2957 struct export_args *eap;
2965 struct nfsex_args nfsea;
2967 eap = &nfsea.export;
2975 bzero(eap, sizeof (struct export_args));
2976 bzero(errmsg, sizeof(errmsg));
2977 eap->ex_flags = exflags;
2978 eap->ex_anon = *anoncrp;
2979 LOGDEBUG("do_mount exflags=0x%x", exflags);
2980 eap->ex_indexfile = ep->ex_indexfile;
2981 if (grp->gr_type == GT_HOST)
2982 ai = grp->gr_ptr.gt_addrinfo;
2985 eap->ex_numsecflavors = numsecflavors;
2986 LOGDEBUG("do_mount numsec=%d", numsecflavors);
2987 for (i = 0; i < eap->ex_numsecflavors; i++)
2988 eap->ex_secflavors[i] = secflavors[i];
2989 if (eap->ex_numsecflavors == 0) {
2990 eap->ex_numsecflavors = 1;
2991 eap->ex_secflavors[0] = AUTH_SYS;
2995 if (v4root_phase == 0) {
2996 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2997 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2998 build_iovec(&iov, &iovlen, "from", NULL, 0);
2999 build_iovec(&iov, &iovlen, "update", NULL, 0);
3000 build_iovec(&iov, &iovlen, "export", eap,
3001 sizeof (struct export_args));
3002 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
3006 switch (grp->gr_type) {
3008 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
3010 eap->ex_addr = ai->ai_addr;
3011 eap->ex_addrlen = ai->ai_addrlen;
3012 eap->ex_masklen = 0;
3015 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
3019 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
3021 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
3023 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
3024 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
3027 eap->ex_addr = NULL;
3028 eap->ex_addrlen = 0;
3029 eap->ex_mask = NULL;
3030 eap->ex_masklen = 0;
3037 syslog(LOG_ERR, "bad grouptype");
3045 * For V4:, use the nfssvc() syscall, instead of mount().
3047 if (v4root_phase == 2) {
3048 nfsea.fspec = v4root_dirpath;
3049 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
3050 syslog(LOG_ERR, "Exporting V4: failed");
3056 * Maybe I should just use the fsb->f_mntonname path
3057 * instead of looping back up the dirp to the mount
3059 * Also, needs to know how to export all types of local
3060 * exportable filesystems and not just "ufs".
3062 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
3063 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
3064 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
3065 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
3066 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
3067 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
3070 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
3074 cp = dirp + dirplen - 1;
3075 if (opt_flags & OP_QUIET) {
3079 if (errno == EPERM) {
3081 warnx("can't change attributes for %s: %s",
3084 "can't change attributes for %s: %s",
3089 if (opt_flags & OP_ALLDIRS) {
3090 if (errno == EINVAL)
3092 "-alldirs requested but %s is not a filesystem mountpoint",
3096 "could not remount %s: %m",
3101 /* back up over the last component */
3102 while (*cp == '/' && cp > dirp)
3104 while (*(cp - 1) != '/' && cp > dirp)
3108 warnx("mnt unsucc");
3109 syslog(LOG_ERR, "can't export %s %s",
3117 * Check that we're still on the same
3120 if (statfs(dirp, &fsb1) != 0 ||
3121 fsidcmp(&fsb1.f_fsid, &fsb->f_fsid) != 0) {
3124 "can't export %s %s", dirp,
3133 * For the experimental server:
3134 * If this is the public directory, get the file handle
3135 * and load it into the kernel via the nfssvc() syscall.
3137 if ((exflags & MNT_EXPUBLIC) != 0) {
3141 if (eap->ex_indexfile != NULL)
3142 public_name = eap->ex_indexfile;
3145 if (getfh(public_name, &fh) < 0)
3147 "Can't get public fh for %s", public_name);
3148 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
3150 "Can't set public fh for %s", public_name);
3153 has_set_publicfh = 1;
3154 ep->ex_flag |= EX_PUBLICFH;
3166 /* free strings allocated by strdup() in getmntopts.c */
3168 free(iov[0].iov_base); /* fstype */
3169 free(iov[2].iov_base); /* fspath */
3170 free(iov[4].iov_base); /* from */
3171 free(iov[6].iov_base); /* update */
3172 free(iov[8].iov_base); /* export */
3173 free(iov[10].iov_base); /* errmsg */
3175 /* free iov, allocated by realloc() */
3182 * Translate a net address.
3184 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
3187 get_net(char *cp, struct netmsk *net, int maskflg)
3189 struct netent *np = NULL;
3190 char *name, *p, *prefp;
3191 struct sockaddr_in sin;
3192 struct sockaddr *sa = NULL;
3193 struct addrinfo hints, *ai = NULL;
3194 char netname[NI_MAXHOST];
3198 if ((opt_flags & OP_MASKLEN) && !maskflg) {
3199 p = strchr(cp, '/');
3205 * Check for a numeric address first. We wish to avoid
3206 * possible DNS lookups in getnetbyname().
3208 if (isxdigit(*cp) || *cp == ':') {
3209 memset(&hints, 0, sizeof hints);
3210 /* Ensure the mask and the network have the same family. */
3211 if (maskflg && (opt_flags & OP_NET))
3212 hints.ai_family = net->nt_net.ss_family;
3213 else if (!maskflg && (opt_flags & OP_HAVEMASK))
3214 hints.ai_family = net->nt_mask.ss_family;
3216 hints.ai_family = AF_UNSPEC;
3217 hints.ai_flags = AI_NUMERICHOST;
3218 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
3220 if (sa != NULL && ai->ai_family == AF_INET) {
3222 * The address in `cp' is really a network address, so
3223 * use inet_network() to re-interpret this correctly.
3224 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
3226 bzero(&sin, sizeof sin);
3227 sin.sin_family = AF_INET;
3228 sin.sin_len = sizeof sin;
3229 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
3231 fprintf(stderr, "get_net: v4 addr %s\n",
3232 inet_ntoa(sin.sin_addr));
3233 sa = (struct sockaddr *)&sin;
3236 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
3237 bzero(&sin, sizeof sin);
3238 sin.sin_family = AF_INET;
3239 sin.sin_len = sizeof sin;
3240 sin.sin_addr = inet_makeaddr(np->n_net, 0);
3241 sa = (struct sockaddr *)&sin;
3247 /* The specified sockaddr is a mask. */
3248 if (checkmask(sa) != 0)
3250 bcopy(sa, &net->nt_mask, sa->sa_len);
3251 opt_flags |= OP_HAVEMASK;
3253 /* The specified sockaddr is a network address. */
3254 bcopy(sa, &net->nt_net, sa->sa_len);
3256 /* Get a network name for the export list. */
3259 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
3260 NULL, 0, NI_NUMERICHOST) == 0) {
3265 if ((net->nt_name = strdup(name)) == NULL)
3269 * Extract a mask from either a "/<masklen>" suffix, or
3270 * from the class of an IPv4 address.
3272 if (opt_flags & OP_MASKLEN) {
3273 preflen = strtol(prefp, NULL, 10);
3274 if (preflen < 0L || preflen == LONG_MAX)
3276 bcopy(sa, &net->nt_mask, sa->sa_len);
3277 if (makemask(&net->nt_mask, (int)preflen) != 0)
3279 opt_flags |= OP_HAVEMASK;
3281 } else if (sa->sa_family == AF_INET &&
3282 (opt_flags & OP_MASK) == 0) {
3285 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
3286 if (IN_CLASSA(addr))
3288 else if (IN_CLASSB(addr))
3290 else if (IN_CLASSC(addr))
3292 else if (IN_CLASSD(addr))
3295 preflen = 32; /* XXX */
3297 bcopy(sa, &net->nt_mask, sa->sa_len);
3298 makemask(&net->nt_mask, (int)preflen);
3299 opt_flags |= OP_HAVEMASK;
3314 * Parse out the next white space separated field
3317 nextfield(char **cp, char **endcp)
3323 while (*p == ' ' || *p == '\t')
3326 while (*p != '\0') {
3331 if (*p == '\\' && *(p + 1) != '\0')
3333 else if (*p == '\'' || *p == '"')
3335 else if (*p == ' ' || *p == '\t')
3344 * Get an exports file line. Skip over blank lines and handle line
3352 int totlen, cont_line;
3355 * Loop around ignoring blank lines and getting all continuation lines.
3360 if ((p = fgetln(exp_file, &len)) == NULL)
3365 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
3375 if (linesize < len + totlen + 1) {
3376 linesize = len + totlen + 1;
3377 line = realloc(line, linesize);
3381 memcpy(line + totlen, p, len);
3383 line[totlen] = '\0';
3384 } while (totlen == 0 || cont_line);
3389 * Parse a description of a credential.
3392 parsecred(char *namelist, struct xucred *cr)
3399 gid_t groups[XU_NGROUPS + 1];
3402 cr->cr_version = XUCRED_VERSION;
3404 * Set up the unprivileged user.
3407 cr->cr_groups[0] = 65533;
3410 * Get the user's password table entry.
3413 name = strsep_quote(&names, ":");
3414 /* Bug? name could be NULL here */
3415 if (isdigit(*name) || *name == '-')
3416 pw = getpwuid(atoi(name));
3418 pw = getpwnam(name);
3420 * Credentials specified as those of a user.
3422 if (names == NULL) {
3424 syslog(LOG_ERR, "unknown user: %s", name);
3427 cr->cr_uid = pw->pw_uid;
3428 ngroups = XU_NGROUPS + 1;
3429 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) {
3430 syslog(LOG_ERR, "too many groups");
3431 ngroups = XU_NGROUPS + 1;
3435 * Compress out duplicate.
3437 cr->cr_ngroups = ngroups - 1;
3438 cr->cr_groups[0] = groups[0];
3439 for (cnt = 2; cnt < ngroups; cnt++)
3440 cr->cr_groups[cnt - 1] = groups[cnt];
3444 * Explicit credential specified as a colon separated list:
3448 cr->cr_uid = pw->pw_uid;
3449 else if (isdigit(*name) || *name == '-')
3450 cr->cr_uid = atoi(name);
3452 syslog(LOG_ERR, "unknown user: %s", name);
3456 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
3457 name = strsep_quote(&names, ":");
3458 if (isdigit(*name) || *name == '-') {
3459 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
3461 if ((gr = getgrnam(name)) == NULL) {
3462 syslog(LOG_ERR, "unknown group: %s", name);
3465 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
3468 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
3469 syslog(LOG_ERR, "too many groups");
3472 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
3474 * Routines that maintain the remote mounttab
3479 struct mountlist *mlp;
3480 char *host, *dirp, *cp;
3484 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
3485 if (errno == ENOENT)
3488 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
3492 while (fgets(str, STRSIZ, mlfile) != NULL) {
3494 host = strsep(&cp, " \t\n");
3495 dirp = strsep(&cp, " \t\n");
3496 if (host == NULL || dirp == NULL)
3498 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3499 if (mlp == (struct mountlist *)NULL)
3501 strncpy(mlp->ml_host, host, MNTNAMLEN);
3502 mlp->ml_host[MNTNAMLEN] = '\0';
3503 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3504 mlp->ml_dirp[MNTPATHLEN] = '\0';
3506 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3512 del_mlist(char *hostp, char *dirp)
3514 struct mountlist *mlp, *mlp2;
3518 SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
3519 if (!strcmp(mlp->ml_host, hostp) &&
3520 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3522 SLIST_REMOVE(&mlhead, mlp, mountlist, next);
3527 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3528 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3531 SLIST_FOREACH(mlp, &mlhead, next) {
3532 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3539 add_mlist(char *hostp, char *dirp)
3541 struct mountlist *mlp;
3544 SLIST_FOREACH(mlp, &mlhead, next) {
3545 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3549 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3550 if (mlp == (struct mountlist *)NULL)
3552 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3553 mlp->ml_host[MNTNAMLEN] = '\0';
3554 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3555 mlp->ml_dirp[MNTPATHLEN] = '\0';
3556 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3557 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3558 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3561 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3566 * Free up a group list.
3569 free_grp(struct grouplist *grp)
3571 if (grp->gr_type == GT_HOST) {
3572 if (grp->gr_ptr.gt_addrinfo != NULL)
3573 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3574 } else if (grp->gr_type == GT_NET) {
3575 if (grp->gr_ptr.gt_net.nt_name)
3576 free(grp->gr_ptr.gt_net.nt_name);
3583 SYSLOG(int pri, const char *fmt, ...)
3588 vfprintf(stderr, fmt, ap);
3594 * Check options for consistency.
3597 check_options(struct dirlist *dp)
3600 if (v4root_phase == 0 && dp == NULL)
3602 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3603 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3606 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3607 syslog(LOG_ERR, "-mask requires -network");
3610 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3611 syslog(LOG_ERR, "-network requires mask specification");
3614 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3615 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3618 if (v4root_phase > 0 &&
3620 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3621 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3624 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3625 syslog(LOG_ERR, "-alldirs has multiple directories");
3632 * Check an absolute directory path for any symbolic links. Return true
3635 check_dirpath(char *dirp)
3642 while (*cp && ret) {
3645 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3651 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3657 * Make a netmask according to the specified prefix length. The ss_family
3658 * and other non-address fields must be initialised before calling this.
3661 makemask(struct sockaddr_storage *ssp, int bitlen)
3666 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3668 if (bitlen > len * CHAR_BIT)
3671 for (i = 0; i < len; i++) {
3672 bits = MIN(CHAR_BIT, bitlen);
3673 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3680 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3681 * is acceptable (i.e. of the form 1...10....0).
3684 checkmask(struct sockaddr *sa)
3689 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3692 for (i = 0; i < len; i++)
3693 if (mask[i] != 0xff)
3696 if (~mask[i] & (u_char)(~mask[i] + 1))
3700 for (; i < len; i++)
3707 * Compare two sockaddrs according to a specified mask. Return zero if
3708 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3709 * If samask is NULL, perform a full comparison.
3712 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3714 unsigned char *p1, *p2, *mask;
3717 if (sa1->sa_family != sa2->sa_family ||
3718 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3719 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3722 switch (sa1->sa_family) {
3724 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3725 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3730 /* Simple binary comparison if no mask specified. */
3732 return (memcmp(p1, p2, len));
3734 /* Set up the mask, and do a mask-based comparison. */
3735 if (sa1->sa_family != samask->sa_family ||
3736 (mask = sa_rawaddr(samask, NULL)) == NULL)
3739 for (i = 0; i < len; i++)
3740 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3746 * Return a pointer to the part of the sockaddr that contains the
3747 * raw address, and set *nbytes to its length in bytes. Returns
3748 * NULL if the address family is unknown.
3751 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3755 switch (sa->sa_family) {
3757 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3758 p = &((struct sockaddr_in *)sa)->sin_addr;
3761 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3762 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3775 huphandler(int sig __unused)
3782 terminate(int sig __unused)
3784 pidfile_remove(pfh);
3785 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3786 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);