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/linker.h>
53 #include <sys/module.h>
54 #include <sys/mount.h>
55 #include <sys/queue.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
61 #include <rpc/rpc_com.h>
62 #include <rpc/pmap_clnt.h>
63 #include <rpc/pmap_prot.h>
64 #include <rpcsvc/mount.h>
65 #include <nfs/nfsproto.h>
66 #include <nfs/nfssvc.h>
67 #include <nfsserver/nfs.h>
69 #include <fs/nfs/nfsport.h>
71 #include <arpa/inet.h>
86 #include "pathnames.h"
94 * Structures for keeping the mount list and export list
97 char ml_host[MNTNAMLEN+1];
98 char ml_dirp[MNTPATHLEN+1];
100 SLIST_ENTRY(mountlist) next;
104 struct dirlist *dp_left;
105 struct dirlist *dp_right;
107 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
111 #define DP_DEFSET 0x1
112 #define DP_HOSTSET 0x2
115 struct dirlist *ex_dirl;
116 struct dirlist *ex_defdir;
117 struct grouplist *ex_grphead;
122 int ex_numsecflavors;
123 int ex_secflavors[MAXSECFLAVORS];
124 int ex_defnumsecflavors;
125 int ex_defsecflavors[MAXSECFLAVORS];
127 SLIST_ENTRY(exportlist) entries;
130 #define EX_LINKED 0x1
132 SLIST_HEAD(exportlisthead, exportlist);
135 struct sockaddr_storage nt_net;
136 struct sockaddr_storage nt_mask;
141 struct addrinfo *gt_addrinfo;
142 struct netmsk gt_net;
147 union grouptypes gr_ptr;
148 struct grouplist *gr_next;
149 int gr_numsecflavors;
150 int gr_secflavors[MAXSECFLAVORS];
156 #define GT_DEFAULT 0x3
157 #define GT_IGNORE 0x5
160 int ht_flag; /* Uses DP_xx bits */
161 struct grouplist *ht_grp;
162 struct hostlist *ht_next;
169 int fhr_numsecflavors;
173 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
176 static char *add_expdir(struct dirlist **, char *, int);
177 static void add_dlist(struct dirlist **, struct dirlist *,
178 struct grouplist *, int, struct exportlist *);
179 static void add_mlist(char *, char *);
180 static int check_dirpath(char *);
181 static int check_options(struct dirlist *);
182 static int checkmask(struct sockaddr *sa);
183 static int chk_host(struct dirlist *, struct sockaddr *, int *, int *,
185 static char *strsep_quote(char **stringp, const char *delim);
186 static int create_service(struct netconfig *nconf);
187 static void complete_service(struct netconfig *nconf, char *port_str);
188 static void clearout_service(void);
189 static void del_mlist(char *hostp, char *dirp);
190 static struct dirlist *dirp_search(struct dirlist *, char *);
191 static int do_mount(struct exportlist *, struct grouplist *, int,
192 struct xucred *, char *, int, struct statfs *);
193 static int do_opt(char **, char **, struct exportlist *,
194 struct grouplist *, int *, int *, struct xucred *);
195 static struct exportlist *ex_search(fsid_t *, struct exportlisthead *);
196 static struct exportlist *get_exp(void);
197 static void free_dir(struct dirlist *);
198 static void free_exp(struct exportlist *);
199 static void free_grp(struct grouplist *);
200 static void free_host(struct hostlist *);
201 static void get_exportlist(void);
202 static void insert_exports(struct exportlist *, struct exportlisthead *);
203 static void free_exports(struct exportlisthead *);
204 static void read_exportfile(void);
205 static void delete_export(struct iovec *, int, struct statfs *, char *);
206 static int get_host(char *, struct grouplist *, struct grouplist *);
207 static struct hostlist *get_ht(void);
208 static int get_line(void);
209 static void get_mountlist(void);
210 static int get_net(char *, struct netmsk *, int);
211 static void getexp_err(struct exportlist *, struct grouplist *, const char *);
212 static struct grouplist *get_grp(void);
213 static void hang_dirp(struct dirlist *, struct grouplist *,
214 struct exportlist *, int);
215 static void huphandler(int sig);
216 static int makemask(struct sockaddr_storage *ssp, int bitlen);
217 static void mntsrv(struct svc_req *, SVCXPRT *);
218 static void nextfield(char **, char **);
219 static void out_of_mem(void);
220 static void parsecred(char *, struct xucred *);
221 static int parsesec(char *, struct exportlist *);
222 static int put_exlist(struct dirlist *, XDR *, struct dirlist *,
224 static void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
225 static int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
226 struct sockaddr *samask);
227 static int scan_tree(struct dirlist *, struct sockaddr *);
228 static void usage(void);
229 static int xdr_dir(XDR *, char *);
230 static int xdr_explist(XDR *, caddr_t);
231 static int xdr_explist_brief(XDR *, caddr_t);
232 static int xdr_explist_common(XDR *, caddr_t, int);
233 static int xdr_fhs(XDR *, caddr_t);
234 static int xdr_mlist(XDR *, caddr_t);
235 static void terminate(int);
237 static struct exportlisthead exphead = SLIST_HEAD_INITIALIZER(&exphead);
238 static SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(&mlhead);
239 static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
240 static char **exnames;
241 static char **hosts = NULL;
242 static struct xucred def_anon = {
249 static int force_v2 = 0;
250 static int resvport_only = 1;
251 static int nhosts = 0;
252 static int dir_only = 1;
253 static int dolog = 0;
254 static int got_sighup = 0;
255 static int xcreated = 0;
257 static char *svcport_str = NULL;
258 static int mallocd_svcport = 0;
260 static int sock_fdcnt;
261 static int sock_fdpos;
262 static int suspend_nfsd = 0;
264 static int opt_flags;
265 static int have_v6 = 1;
267 static int v4root_phase = 0;
268 static char v4root_dirpath[PATH_MAX + 1];
269 static int has_publicfh = 0;
271 static struct pidfh *pfh = NULL;
272 /* Bits for opt_flags above */
273 #define OP_MAPROOT 0x01
274 #define OP_MAPALL 0x02
278 #define OP_ALLDIRS 0x40
279 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
280 #define OP_QUIET 0x100
281 #define OP_MASKLEN 0x200
285 static int debug = 1;
286 static void SYSLOG(int, const char *, ...) __printflike(2, 3);
287 #define syslog SYSLOG
289 static int debug = 0;
293 * Similar to strsep(), but it allows for quoted strings
294 * and escaped characters.
296 * It returns the string (or NULL, if *stringp is NULL),
297 * which is a de-quoted version of the string if necessary.
299 * It modifies *stringp in place.
302 strsep_quote(char **stringp, const char *delim)
304 char *srcptr, *dstptr, *retval;
307 if (stringp == NULL || *stringp == NULL)
310 srcptr = dstptr = retval = *stringp;
314 * We're looking for several edge cases here.
315 * First: if we're in quote state (quot != 0),
316 * then we ignore the delim characters, but otherwise
317 * process as normal, unless it is the quote character.
318 * Second: if the current character is a backslash,
319 * we take the next character as-is, without checking
320 * for delim, quote, or backslash. Exception: if the
321 * next character is a NUL, that's the end of the string.
322 * Third: if the character is a quote character, we toggle
324 * Otherwise: check the current character for NUL, or
325 * being in delim, and end the string if either is true.
327 if (*srcptr == '\\') {
330 * The edge case here is if the next character
331 * is NUL, we want to stop processing. But if
332 * it's not NUL, then we simply want to copy it.
335 *dstptr++ = *srcptr++;
339 if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
343 if (quot && *srcptr == quot) {
344 /* End of the quoted part */
349 if (!quot && strchr(delim, *srcptr))
351 *dstptr++ = *srcptr++;
354 *dstptr = 0; /* Terminate the string */
355 *stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
360 * Mountd server for NFS mount protocol as described in:
361 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
362 * The optional arguments are the exports file name
363 * default: _PATH_EXPORTS
364 * and "-n" to allow nonroot mount.
367 main(int argc, char **argv)
370 struct netconfig *nconf;
371 char *endptr, **hosts_bak;
376 int maxrec = RPC_MAXDATASIZE;
377 int attempt_cnt, port_len, port_pos, ret;
380 /* Check that another mountd isn't already running. */
381 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
384 errx(1, "mountd already running, pid: %d.", otherpid);
385 warn("cannot open or create pidfile");
388 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
394 while ((c = getopt(argc, argv, "2deh:lnp:rS")) != -1)
400 /* now a no-op, since this is the default */
409 debug = debug ? 0 : 1;
416 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
417 if (endptr == NULL || *endptr != '\0' ||
418 svcport == 0 || svcport >= IPPORT_MAX)
420 svcport_str = strdup(optarg);
425 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
426 if (hosts_bak == NULL) {
428 for (k = 0; k < nhosts; k++)
435 hosts[nhosts - 1] = strdup(optarg);
436 if (hosts[nhosts - 1] == NULL) {
437 for (k = 0; k < (nhosts - 1); k++)
450 if (modfind("nfsd") < 0) {
451 /* Not present in kernel, try loading it */
452 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
453 errx(1, "NFS server is not available");
461 exnames = exnames_default;
462 openlog("mountd", LOG_PID, LOG_DAEMON);
464 warnx("getting export list");
467 warnx("getting mount list");
473 signal(SIGINT, SIG_IGN);
474 signal(SIGQUIT, SIG_IGN);
476 signal(SIGHUP, huphandler);
477 signal(SIGTERM, terminate);
478 signal(SIGPIPE, SIG_IGN);
482 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
483 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
484 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
486 if (!resvport_only) {
487 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
488 &resvport_only, sizeof(resvport_only)) != 0 &&
490 syslog(LOG_ERR, "sysctl: %m");
496 * If no hosts were specified, add a wildcard entry to bind to
497 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
501 hosts = malloc(sizeof(char *));
509 hosts_bak = realloc(hosts, (nhosts + 2) *
511 if (hosts_bak == NULL) {
512 for (k = 0; k < nhosts; k++)
519 hosts[nhosts - 2] = "::1";
521 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
522 if (hosts_bak == NULL) {
523 for (k = 0; k < nhosts; k++)
533 hosts[nhosts - 1] = "127.0.0.1";
541 nc_handle = setnetconfig();
542 while ((nconf = getnetconfig(nc_handle))) {
543 if (nconf->nc_flag & NC_VISIBLE) {
544 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
548 ret = create_service(nconf);
550 /* Ignore this call */
554 * Failed to bind port, so close off
555 * all sockets created and try again
556 * if the port# was dynamically
557 * assigned via bind(2).
560 if (mallocd_svcport != 0 &&
561 attempt_cnt < GETPORT_MAXTRY) {
568 "bindresvport_sa: %m");
572 /* Start over at the first service. */
576 nc_handle = setnetconfig();
578 } else if (mallocd_svcport != 0 &&
579 attempt_cnt == GETPORT_MAXTRY) {
581 * For the last attempt, allow
582 * different port #s for each nconf
583 * by saving the svcport_str and
584 * setting it back to NULL.
586 port_list = realloc(port_list,
587 (port_len + 1) * sizeof(char *));
588 if (port_list == NULL)
590 port_list[port_len++] = svcport_str;
599 * Successfully bound the ports, so call complete_service() to
600 * do the rest of the setup on the service(s).
604 nc_handle = setnetconfig();
605 while ((nconf = getnetconfig(nc_handle))) {
606 if (nconf->nc_flag & NC_VISIBLE) {
607 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
610 } else if (port_list != NULL) {
611 if (port_pos >= port_len) {
612 syslog(LOG_ERR, "too many port#s");
615 complete_service(nconf, port_list[port_pos++]);
617 complete_service(nconf, svcport_str);
620 endnetconfig(nc_handle);
622 if (port_list != NULL) {
623 for (port_pos = 0; port_pos < port_len; port_pos++)
624 free(port_list[port_pos]);
629 syslog(LOG_ERR, "could not create any services");
633 /* Expand svc_run() here so that we can call get_exportlist(). */
640 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
644 syslog(LOG_ERR, "mountd died: select: %m");
649 svc_getreqset(&readfds);
655 * This routine creates and binds sockets on the appropriate
656 * addresses. It gets called one time for each transport.
657 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
658 * bind failed with EADDRINUSE.
659 * Any file descriptors that have been created are stored in sock_fd and
660 * the total count of them is maintained in sock_fdcnt.
663 create_service(struct netconfig *nconf)
665 struct addrinfo hints, *res = NULL;
666 struct sockaddr_in *sin;
667 struct sockaddr_in6 *sin6;
668 struct __rpc_sockinfo si;
674 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
677 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
678 (nconf->nc_semantics != NC_TPI_COTS) &&
679 (nconf->nc_semantics != NC_TPI_COTS_ORD))
680 return (1); /* not my type */
683 * XXX - using RPC library internal functions.
685 if (!__rpc_nconf2sockinfo(nconf, &si)) {
686 syslog(LOG_ERR, "cannot get information for %s",
691 /* Get mountd's address on this transport */
692 memset(&hints, 0, sizeof hints);
693 hints.ai_family = si.si_af;
694 hints.ai_socktype = si.si_socktype;
695 hints.ai_protocol = si.si_proto;
698 * Bind to specific IPs if asked to
701 while (nhostsbak > 0) {
703 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
706 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
709 hints.ai_flags = AI_PASSIVE;
712 * XXX - using RPC library internal functions.
714 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
716 if (errno == EAFNOSUPPORT &&
717 nconf->nc_semantics != NC_TPI_CLTS)
720 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
721 "cannot create socket for %s", nconf->nc_netid);
727 switch (hints.ai_family) {
729 if (inet_pton(AF_INET, hosts[nhostsbak],
731 hints.ai_flags |= AI_NUMERICHOST;
734 * Skip if we have an AF_INET6 address.
736 if (inet_pton(AF_INET6, hosts[nhostsbak],
744 if (inet_pton(AF_INET6, hosts[nhostsbak],
746 hints.ai_flags |= AI_NUMERICHOST;
749 * Skip if we have an AF_INET address.
751 if (inet_pton(AF_INET, hosts[nhostsbak],
759 * We're doing host-based access checks here, so don't
760 * allow v4-in-v6 to confuse things. The kernel will
761 * disable it by default on NFS sockets too.
763 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
766 "can't disable v4-in-v6 on IPv6 socket");
775 * If no hosts were specified, just bind to INADDR_ANY
777 if (strcmp("*", hosts[nhostsbak]) == 0) {
778 if (svcport_str == NULL) {
779 res = malloc(sizeof(struct addrinfo));
783 res->ai_flags = hints.ai_flags;
784 res->ai_family = hints.ai_family;
785 res->ai_protocol = hints.ai_protocol;
786 switch (res->ai_family) {
788 sin = malloc(sizeof(struct sockaddr_in));
791 sin->sin_family = AF_INET;
792 sin->sin_port = htons(0);
793 sin->sin_addr.s_addr = htonl(INADDR_ANY);
794 res->ai_addr = (struct sockaddr*) sin;
795 res->ai_addrlen = (socklen_t)
796 sizeof(struct sockaddr_in);
799 sin6 = malloc(sizeof(struct sockaddr_in6));
802 sin6->sin6_family = AF_INET6;
803 sin6->sin6_port = htons(0);
804 sin6->sin6_addr = in6addr_any;
805 res->ai_addr = (struct sockaddr*) sin6;
806 res->ai_addrlen = (socklen_t)
807 sizeof(struct sockaddr_in6);
810 syslog(LOG_ERR, "bad addr fam %d",
815 if ((aicode = getaddrinfo(NULL, svcport_str,
816 &hints, &res)) != 0) {
818 "cannot get local address for %s: %s",
820 gai_strerror(aicode));
826 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
827 &hints, &res)) != 0) {
829 "cannot get local address for %s: %s",
830 nconf->nc_netid, gai_strerror(aicode));
837 sock_fd[sock_fdcnt - 1] = fd;
839 /* Now, attempt the bind. */
840 r = bindresvport_sa(fd, res->ai_addr);
842 if (errno == EADDRINUSE && mallocd_svcport != 0) {
843 if (mallocd_res != 0) {
850 syslog(LOG_ERR, "bindresvport_sa: %m");
854 if (svcport_str == NULL) {
855 svcport_str = malloc(NI_MAXSERV * sizeof(char));
856 if (svcport_str == NULL)
860 if (getnameinfo(res->ai_addr,
861 res->ai_addr->sa_len, NULL, NI_MAXHOST,
862 svcport_str, NI_MAXSERV * sizeof(char),
863 NI_NUMERICHOST | NI_NUMERICSERV))
864 errx(1, "Cannot get port number");
866 if (mallocd_res != 0) {
877 * Called after all the create_service() calls have succeeded, to complete
878 * the setup and registration.
881 complete_service(struct netconfig *nconf, char *port_str)
883 struct addrinfo hints, *res = NULL;
884 struct __rpc_sockinfo si;
885 struct netbuf servaddr;
886 SVCXPRT *transp = NULL;
887 int aicode, fd, nhostsbak;
890 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
891 (nconf->nc_semantics != NC_TPI_COTS) &&
892 (nconf->nc_semantics != NC_TPI_COTS_ORD))
893 return; /* not my type */
896 * XXX - using RPC library internal functions.
898 if (!__rpc_nconf2sockinfo(nconf, &si)) {
899 syslog(LOG_ERR, "cannot get information for %s",
905 while (nhostsbak > 0) {
907 if (sock_fdpos >= sock_fdcnt) {
908 /* Should never happen. */
909 syslog(LOG_ERR, "Ran out of socket fd's");
912 fd = sock_fd[sock_fdpos++];
917 * Using -1 tells listen(2) to use
918 * kern.ipc.soacceptqueue for the backlog.
920 if (nconf->nc_semantics != NC_TPI_CLTS)
923 if (nconf->nc_semantics == NC_TPI_CLTS )
924 transp = svc_dg_create(fd, 0, 0);
926 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
929 if (transp != (SVCXPRT *) NULL) {
930 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
933 "can't register %s MOUNTVERS service",
936 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
939 "can't register %s MOUNTVERS3 service",
943 syslog(LOG_WARNING, "can't create %s services",
946 if (registered == 0) {
948 memset(&hints, 0, sizeof hints);
949 hints.ai_flags = AI_PASSIVE;
950 hints.ai_family = si.si_af;
951 hints.ai_socktype = si.si_socktype;
952 hints.ai_protocol = si.si_proto;
954 if ((aicode = getaddrinfo(NULL, port_str, &hints,
956 syslog(LOG_ERR, "cannot get local address: %s",
957 gai_strerror(aicode));
961 servaddr.buf = malloc(res->ai_addrlen);
962 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
963 servaddr.len = res->ai_addrlen;
965 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
966 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
975 * Clear out sockets after a failure to bind one of them, so that the
976 * cycle of socket creation/binding can start anew.
979 clearout_service(void)
983 for (i = 0; i < sock_fdcnt; i++) {
984 if (sock_fd[i] >= 0) {
985 shutdown(sock_fd[i], SHUT_RDWR);
995 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
996 "[-S] [-h <bindip>] [export_file ...]\n");
1001 * The mount rpc service
1004 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
1006 struct exportlist *ep;
1008 struct fhreturn fhr;
1011 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1012 int lookup_failed = 1;
1013 struct sockaddr *saddr;
1015 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1016 int bad = 0, defset, hostset;
1017 sigset_t sighup_mask;
1018 int numsecflavors, *secflavorsp;
1020 sigemptyset(&sighup_mask);
1021 sigaddset(&sighup_mask, SIGHUP);
1022 saddr = svc_getrpccaller(transp)->buf;
1023 switch (saddr->sa_family) {
1025 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1028 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1031 syslog(LOG_ERR, "request from unknown address family");
1034 switch (rqstp->rq_proc) {
1036 case MOUNTPROC_UMNT:
1037 case MOUNTPROC_UMNTALL:
1038 lookup_failed = getnameinfo(saddr, saddr->sa_len, host,
1039 sizeof host, NULL, 0, 0);
1041 getnameinfo(saddr, saddr->sa_len, numerichost,
1042 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1043 switch (rqstp->rq_proc) {
1045 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1046 syslog(LOG_ERR, "can't send reply");
1049 if (sport >= IPPORT_RESERVED && resvport_only) {
1051 "mount request from %s from unprivileged port",
1053 svcerr_weakauth(transp);
1056 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1057 syslog(LOG_NOTICE, "undecodable mount request from %s",
1059 svcerr_decode(transp);
1064 * Get the real pathname and make sure it is a directory
1065 * or a regular file if the -r option was specified
1068 if (realpath(rpcpath, dirpath) == NULL ||
1069 stat(dirpath, &stb) < 0 ||
1070 statfs(dirpath, &fsb) < 0) {
1071 chdir("/"); /* Just in case realpath doesn't */
1073 "mount request from %s for non existent path %s",
1074 numerichost, dirpath);
1076 warnx("stat failed on %s", dirpath);
1077 bad = ENOENT; /* We will send error reply later */
1080 !S_ISDIR(stb.st_mode) &&
1081 (dir_only || !S_ISREG(stb.st_mode))) {
1083 "mount request from %s for non-directory path %s",
1084 numerichost, dirpath);
1086 warnx("mounting non-directory %s", dirpath);
1087 bad = ENOTDIR; /* We will send error reply later */
1090 /* Check in the exports list */
1091 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1095 ep = ex_search(&fsb.f_fsid, &exphead);
1096 hostset = defset = 0;
1097 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1098 &numsecflavors, &secflavorsp) ||
1099 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1100 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1102 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1103 scan_tree(ep->ex_dirl, saddr) == 0))) {
1105 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1107 syslog(LOG_ERR, "can't send reply");
1108 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1111 if (hostset & DP_HOSTSET) {
1112 fhr.fhr_flag = hostset;
1113 fhr.fhr_numsecflavors = numsecflavors;
1114 fhr.fhr_secflavors = secflavorsp;
1116 fhr.fhr_flag = defset;
1117 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1118 fhr.fhr_secflavors = ep->ex_defsecflavors;
1120 fhr.fhr_vers = rqstp->rq_vers;
1121 /* Get the file handle */
1122 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1123 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1125 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1126 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1128 syslog(LOG_ERR, "can't send reply");
1129 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1132 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1134 syslog(LOG_ERR, "can't send reply");
1136 add_mlist(host, dirpath);
1138 add_mlist(numerichost, dirpath);
1140 warnx("mount successful");
1143 "mount request succeeded from %s for %s",
1144 numerichost, dirpath);
1149 "mount request denied from %s for %s",
1150 numerichost, dirpath);
1153 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1155 syslog(LOG_ERR, "can't send reply");
1156 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1158 case MOUNTPROC_DUMP:
1159 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1160 syslog(LOG_ERR, "can't send reply");
1163 "dump request succeeded from %s",
1166 case MOUNTPROC_UMNT:
1167 if (sport >= IPPORT_RESERVED && resvport_only) {
1169 "umount 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 umount request from %s",
1177 svcerr_decode(transp);
1180 if (realpath(rpcpath, dirpath) == NULL) {
1181 syslog(LOG_NOTICE, "umount request from %s "
1182 "for non existent path %s",
1183 numerichost, dirpath);
1185 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1186 syslog(LOG_ERR, "can't send reply");
1188 del_mlist(host, dirpath);
1189 del_mlist(numerichost, dirpath);
1192 "umount request succeeded from %s for %s",
1193 numerichost, dirpath);
1195 case MOUNTPROC_UMNTALL:
1196 if (sport >= IPPORT_RESERVED && resvport_only) {
1198 "umountall request from %s from unprivileged port",
1200 svcerr_weakauth(transp);
1203 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1204 syslog(LOG_ERR, "can't send reply");
1206 del_mlist(host, NULL);
1207 del_mlist(numerichost, NULL);
1210 "umountall request succeeded from %s",
1213 case MOUNTPROC_EXPORT:
1214 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1215 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1217 syslog(LOG_ERR, "can't send reply");
1220 "export request succeeded from %s",
1224 svcerr_noproc(transp);
1230 * Xdr conversion for a dirpath string
1233 xdr_dir(XDR *xdrsp, char *dirp)
1235 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1239 * Xdr routine to generate file handle reply
1242 xdr_fhs(XDR *xdrsp, caddr_t cp)
1244 struct fhreturn *fhrp = (struct fhreturn *)cp;
1245 u_long ok = 0, len, auth;
1248 if (!xdr_long(xdrsp, &ok))
1250 switch (fhrp->fhr_vers) {
1252 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1255 if (!xdr_long(xdrsp, &len))
1257 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1259 if (fhrp->fhr_numsecflavors) {
1260 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1262 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1263 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1269 if (!xdr_long(xdrsp, &len))
1271 return (xdr_long(xdrsp, &auth));
1278 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1280 struct mountlist *mlp;
1285 SLIST_FOREACH(mlp, &mlhead, next) {
1286 if (!xdr_bool(xdrsp, &true))
1288 strp = &mlp->ml_host[0];
1289 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1291 strp = &mlp->ml_dirp[0];
1292 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1295 if (!xdr_bool(xdrsp, &false))
1301 * Xdr conversion for export list
1304 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1306 struct exportlist *ep;
1309 sigset_t sighup_mask;
1311 sigemptyset(&sighup_mask);
1312 sigaddset(&sighup_mask, SIGHUP);
1313 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1315 SLIST_FOREACH(ep, &exphead, entries) {
1317 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1320 if (ep->ex_defdir && putdef == 0 &&
1321 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1325 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1326 if (!xdr_bool(xdrsp, &false))
1330 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1335 * Called from xdr_explist() to traverse the tree and export the
1339 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1342 struct grouplist *grp;
1343 struct hostlist *hp;
1350 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1352 if (!xdr_bool(xdrsp, &true))
1355 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1357 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1362 if (!xdr_bool(xdrsp, &true))
1365 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1367 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1368 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1372 if (grp->gr_type == GT_HOST) {
1373 if (!xdr_bool(xdrsp, &true))
1375 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1376 if (!xdr_string(xdrsp, &strp,
1379 } else if (grp->gr_type == GT_NET) {
1380 if (!xdr_bool(xdrsp, &true))
1382 strp = grp->gr_ptr.gt_net.nt_name;
1383 if (!xdr_string(xdrsp, &strp,
1388 if (gotalldir && hp == (struct hostlist *)NULL) {
1394 if (!xdr_bool(xdrsp, &false))
1396 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1403 xdr_explist(XDR *xdrsp, caddr_t cp)
1406 return xdr_explist_common(xdrsp, cp, 0);
1410 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1413 return xdr_explist_common(xdrsp, cp, 1);
1417 static size_t linesize;
1418 static FILE *exp_file;
1421 * Get the export list from one, currently open file
1424 get_exportlist_one(void)
1426 struct exportlist *ep;
1427 struct grouplist *grp, *tgrp;
1428 struct dirlist *dirhead;
1431 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1432 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1435 dirhead = (struct dirlist *)NULL;
1436 while (get_line()) {
1438 warnx("got line %s", line);
1440 nextfield(&cp, &endcp);
1449 exflags = MNT_EXPORTED;
1452 ep = (struct exportlist *)NULL;
1456 * Handle the V4 root dir.
1458 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1460 * V4: just indicates that it is the v4 root point,
1461 * so skip over that and set v4root_phase.
1463 if (v4root_phase > 0) {
1464 syslog(LOG_ERR, "V4:duplicate line, ignored");
1469 nextfield(&cp, &endcp);
1473 * Create new exports list entry
1476 tgrp = grp = get_grp();
1478 if (len > MNTNAMLEN) {
1479 getexp_err(ep, tgrp, "mountpoint too long");
1483 if (ep == (struct exportlist *)NULL) {
1484 getexp_err(ep, tgrp,
1485 "flag before export path definition");
1489 warnx("doing opt %s", cp);
1491 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1493 getexp_err(ep, tgrp, NULL);
1496 } else if (*cp == '/') {
1499 if (v4root_phase > 1) {
1501 getexp_err(ep, tgrp, "Multiple V4 dirs");
1505 if (check_dirpath(cp) &&
1506 statfs(cp, &fsb) >= 0) {
1507 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1508 syslog(LOG_ERR, "Warning: exporting of "
1509 "automounted fs %s not supported", cp);
1511 getexp_err(ep, tgrp, "dirs must be first");
1514 if (v4root_phase == 1) {
1516 getexp_err(ep, tgrp, "Multiple V4 dirs");
1519 if (strlen(v4root_dirpath) == 0) {
1520 strlcpy(v4root_dirpath, cp,
1521 sizeof (v4root_dirpath));
1522 } else if (strcmp(v4root_dirpath, cp)
1525 "different V4 dirpath %s", cp);
1526 getexp_err(ep, tgrp, NULL);
1535 if (ep->ex_fs.val[0] !=
1536 fsb.f_fsid.val[0] ||
1538 fsb.f_fsid.val[1]) {
1539 getexp_err(ep, tgrp,
1545 * See if this directory is already
1548 ep = ex_search(&fsb.f_fsid, &exphead);
1549 if (ep == (struct exportlist *)NULL) {
1551 ep->ex_fs = fsb.f_fsid;
1552 ep->ex_fsdir = strdup(fsb.f_mntonname);
1553 if (ep->ex_fsdir == NULL)
1557 "making new ep fs=0x%x,0x%x",
1561 warnx("found ep fs=0x%x,0x%x",
1567 * Add dirpath to export mount point.
1569 dirp = add_expdir(&dirhead, cp, len);
1573 getexp_err(ep, tgrp,
1574 "symbolic link in export path or statfs failed");
1582 if (ep == (struct exportlist *)NULL) {
1583 getexp_err(ep, tgrp,
1584 "host(s) before export path definition");
1589 * Get the host or netgroup.
1592 netgrp = getnetgrent(&hst, &usr, &dom);
1595 grp->gr_next = get_grp();
1601 "null hostname in netgroup %s, skipping", cp);
1602 grp->gr_type = GT_IGNORE;
1603 } else if (get_host(hst, grp, tgrp)) {
1605 "bad host %s in netgroup %s, skipping", hst, cp);
1606 grp->gr_type = GT_IGNORE;
1608 } else if (get_host(cp, grp, tgrp)) {
1609 syslog(LOG_ERR, "bad host %s, skipping", cp);
1610 grp->gr_type = GT_IGNORE;
1613 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1618 nextfield(&cp, &endcp);
1621 if (check_options(dirhead)) {
1622 getexp_err(ep, tgrp, NULL);
1626 grp->gr_type = GT_DEFAULT;
1628 warnx("adding a default entry");
1631 * Don't allow a network export coincide with a list of
1632 * host(s) on the same line.
1634 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1635 getexp_err(ep, tgrp, "network/host conflict");
1639 * If an export list was specified on this line, make sure
1640 * that we have at least one valid entry, otherwise skip it.
1644 while (grp && grp->gr_type == GT_IGNORE)
1647 getexp_err(ep, tgrp, "no valid entries");
1652 if (v4root_phase == 1) {
1653 getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1658 * Loop through hosts, pushing the exports into the kernel.
1659 * After loop, tgrp points to the start of the list and
1660 * grp points to the last entry in the list.
1664 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1666 getexp_err(ep, tgrp, NULL);
1669 } while (grp->gr_next && (grp = grp->gr_next));
1672 * For V4: don't enter in mount lists.
1674 if (v4root_phase > 0 && v4root_phase <= 2) {
1676 * Since these structures aren't used by mountd,
1681 while (tgrp != NULL) {
1683 tgrp = tgrp->gr_next;
1690 * Success. Update the data structures.
1693 hang_dirp(dirhead, tgrp, ep, opt_flags);
1694 grp->gr_next = ep->ex_grphead;
1695 ep->ex_grphead = tgrp;
1697 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1701 dirhead = (struct dirlist *)NULL;
1702 if ((ep->ex_flag & EX_LINKED) == 0) {
1703 insert_exports(ep, &exphead);
1705 ep->ex_flag |= EX_LINKED;
1711 dirhead = (struct dirlist *)NULL;
1717 * Get the export list from all specified files
1720 get_exportlist(void)
1722 struct export_args export;
1724 struct statfs *mntbufp;
1728 struct nfsex_args eargs;
1730 if (suspend_nfsd != 0)
1731 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1732 v4root_dirpath[0] = '\0';
1733 bzero(&export, sizeof(export));
1734 export.ex_flags = MNT_DELEXPORT;
1737 bzero(errmsg, sizeof(errmsg));
1740 * First, get rid of the old list
1742 free_exports(&exphead);
1745 * and the old V4 root dir.
1747 bzero(&eargs, sizeof (eargs));
1748 eargs.export.ex_flags = MNT_DELEXPORT;
1749 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1751 syslog(LOG_ERR, "Can't delete exports for V4:");
1754 * and clear flag that notes if a public fh has been exported.
1759 * And delete exports that are in the kernel for all local
1761 * XXX: Should know how to handle all local exportable filesystems.
1763 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1766 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1767 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1768 build_iovec(&iov, &iovlen, "from", NULL, 0);
1769 build_iovec(&iov, &iovlen, "update", NULL, 0);
1770 build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1771 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1774 for (i = 0; i < num; i++)
1775 delete_export(iov, iovlen, &mntbufp[i], errmsg);
1778 /* Free strings allocated by strdup() in getmntopts.c */
1779 free(iov[0].iov_base); /* fstype */
1780 free(iov[2].iov_base); /* fspath */
1781 free(iov[4].iov_base); /* from */
1782 free(iov[6].iov_base); /* update */
1783 free(iov[8].iov_base); /* export */
1784 free(iov[10].iov_base); /* errmsg */
1786 /* free iov, allocated by realloc() */
1794 * If there was no public fh, clear any previous one set.
1796 if (has_publicfh == 0)
1797 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1799 /* Resume the nfsd. If they weren't suspended, this is harmless. */
1800 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1804 * Insert an export entry in the appropriate list.
1807 insert_exports(struct exportlist *ep, struct exportlisthead *exhp)
1810 SLIST_INSERT_HEAD(exhp, ep, entries);
1814 * Free up the exports lists passed in as arguments.
1817 free_exports(struct exportlisthead *exhp)
1819 struct exportlist *ep, *ep2;
1821 SLIST_FOREACH_SAFE(ep, exhp, entries, ep2) {
1822 SLIST_REMOVE(exhp, ep, exportlist, entries);
1829 * Read the exports file(s) and call get_exportlist_one() for each line.
1832 read_exportfile(void)
1837 * Read in the exports file and build the list, calling
1838 * nmount() as we go along to push the export rules into the kernel.
1841 for (i = 0; exnames[i] != NULL; i++) {
1843 warnx("reading exports from %s", exnames[i]);
1844 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1845 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1848 get_exportlist_one();
1853 syslog(LOG_ERR, "can't open any exports file");
1859 * Delete an exports entry.
1862 delete_export(struct iovec *iov, int iovlen, struct statfs *fsp, char *errmsg)
1864 struct xvfsconf vfc;
1866 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1867 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1873 * We do not need to delete "export" flag from
1874 * filesystems that do not have it set.
1876 if (!(fsp->f_flags & MNT_EXPORTED))
1879 * Do not delete export for network filesystem by
1880 * passing "export" arg to nmount().
1881 * It only makes sense to do this for local filesystems.
1883 if (vfc.vfc_flags & VFCF_NETWORK)
1886 iov[1].iov_base = fsp->f_fstypename;
1887 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1888 iov[3].iov_base = fsp->f_mntonname;
1889 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1890 iov[5].iov_base = fsp->f_mntfromname;
1891 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1895 * EXDEV is returned when path exists but is not a
1896 * mount point. May happens if raced with unmount.
1898 if (nmount(iov, iovlen, fsp->f_flags) < 0 && errno != ENOENT &&
1899 errno != ENOTSUP && errno != EXDEV) {
1901 "can't delete exports for %s: %m %s",
1902 fsp->f_mntonname, errmsg);
1907 * Allocate an export list element
1909 static struct exportlist *
1912 struct exportlist *ep;
1914 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
1915 if (ep == (struct exportlist *)NULL)
1921 * Allocate a group list element
1923 static struct grouplist *
1926 struct grouplist *gp;
1928 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
1929 if (gp == (struct grouplist *)NULL)
1935 * Clean up upon an error in get_exportlist().
1938 getexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
1940 struct grouplist *tgrp;
1942 if (!(opt_flags & OP_QUIET)) {
1944 syslog(LOG_ERR, "bad exports list line '%s': %s", line,
1947 syslog(LOG_ERR, "bad exports list line '%s'", line);
1949 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1959 * Search the export list for a matching fs.
1961 static struct exportlist *
1962 ex_search(fsid_t *fsid, struct exportlisthead *exhp)
1964 struct exportlist *ep;
1966 SLIST_FOREACH(ep, exhp, entries) {
1967 if (ep->ex_fs.val[0] == fsid->val[0] &&
1968 ep->ex_fs.val[1] == fsid->val[1])
1976 * Add a directory path to the list.
1979 add_expdir(struct dirlist **dpp, char *cp, int len)
1983 dp = malloc(sizeof (struct dirlist));
1984 if (dp == (struct dirlist *)NULL)
1987 dp->dp_right = (struct dirlist *)NULL;
1989 dp->dp_hosts = (struct hostlist *)NULL;
1990 dp->dp_dirp = strndup(cp, len);
1991 if (dp->dp_dirp == NULL)
1994 return (dp->dp_dirp);
1998 * Hang the dir list element off the dirpath binary tree as required
1999 * and update the entry for host.
2002 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
2005 struct hostlist *hp;
2006 struct dirlist *dp2;
2008 if (flags & OP_ALLDIRS) {
2013 if (grp == (struct grouplist *)NULL) {
2014 ep->ex_defdir->dp_flag |= DP_DEFSET;
2015 /* Save the default security flavors list. */
2016 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2017 if (ep->ex_numsecflavors > 0)
2018 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2019 sizeof(ep->ex_secflavors));
2020 } else while (grp) {
2023 hp->ht_next = ep->ex_defdir->dp_hosts;
2024 ep->ex_defdir->dp_hosts = hp;
2025 /* Save the security flavors list for this host set. */
2026 grp->gr_numsecflavors = ep->ex_numsecflavors;
2027 if (ep->ex_numsecflavors > 0)
2028 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2029 sizeof(ep->ex_secflavors));
2035 * Loop through the directories adding them to the tree.
2039 add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
2046 * Traverse the binary tree either updating a node that is already there
2047 * for the new directory or adding the new node.
2050 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2051 int flags, struct exportlist *ep)
2054 struct hostlist *hp;
2059 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2061 add_dlist(&dp->dp_left, newdp, grp, flags, ep);
2063 } else if (cmp < 0) {
2064 add_dlist(&dp->dp_right, newdp, grp, flags, ep);
2067 free((caddr_t)newdp);
2070 dp->dp_left = (struct dirlist *)NULL;
2076 * Hang all of the host(s) off of the directory point.
2081 hp->ht_next = dp->dp_hosts;
2083 /* Save the security flavors list for this host set. */
2084 grp->gr_numsecflavors = ep->ex_numsecflavors;
2085 if (ep->ex_numsecflavors > 0)
2086 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2087 sizeof(ep->ex_secflavors));
2091 dp->dp_flag |= DP_DEFSET;
2092 /* Save the default security flavors list. */
2093 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2094 if (ep->ex_numsecflavors > 0)
2095 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2096 sizeof(ep->ex_secflavors));
2101 * Search for a dirpath on the export point.
2103 static struct dirlist *
2104 dirp_search(struct dirlist *dp, char *dirp)
2109 cmp = strcmp(dp->dp_dirp, dirp);
2111 return (dirp_search(dp->dp_left, dirp));
2113 return (dirp_search(dp->dp_right, dirp));
2121 * Scan for a host match in a directory tree.
2124 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2125 int *hostsetp, int *numsecflavors, int **secflavorsp)
2127 struct hostlist *hp;
2128 struct grouplist *grp;
2129 struct addrinfo *ai;
2132 if (dp->dp_flag & DP_DEFSET)
2133 *defsetp = dp->dp_flag;
2137 switch (grp->gr_type) {
2139 ai = grp->gr_ptr.gt_addrinfo;
2140 for (; ai; ai = ai->ai_next) {
2141 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2143 (hp->ht_flag | DP_HOSTSET);
2144 if (numsecflavors != NULL) {
2146 grp->gr_numsecflavors;
2155 if (!sacmp(saddr, (struct sockaddr *)
2156 &grp->gr_ptr.gt_net.nt_net,
2158 &grp->gr_ptr.gt_net.nt_mask)) {
2159 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2160 if (numsecflavors != NULL) {
2162 grp->gr_numsecflavors;
2177 * Scan tree for a host that matches the address.
2180 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2182 int defset, hostset;
2185 if (scan_tree(dp->dp_left, saddr))
2187 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2189 if (scan_tree(dp->dp_right, saddr))
2196 * Traverse the dirlist tree and free it up.
2199 free_dir(struct dirlist *dp)
2203 free_dir(dp->dp_left);
2204 free_dir(dp->dp_right);
2205 free_host(dp->dp_hosts);
2212 * Parse a colon separated list of security flavors
2215 parsesec(char *seclist, struct exportlist *ep)
2220 ep->ex_numsecflavors = 0;
2222 cp = strchr(seclist, ':');
2228 if (!strcmp(seclist, "sys"))
2230 else if (!strcmp(seclist, "krb5"))
2231 flavor = RPCSEC_GSS_KRB5;
2232 else if (!strcmp(seclist, "krb5i"))
2233 flavor = RPCSEC_GSS_KRB5I;
2234 else if (!strcmp(seclist, "krb5p"))
2235 flavor = RPCSEC_GSS_KRB5P;
2239 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2242 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2245 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2248 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2249 ep->ex_numsecflavors++;
2261 * Parse the option string and update fields.
2262 * Option arguments may either be -<option>=<value> or
2266 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2267 int *has_hostp, int *exflagsp, struct xucred *cr)
2269 char *cpoptarg, *cpoptend;
2270 char *cp, *endcp, *cpopt, savedc, savedc2;
2271 int allflag, usedarg;
2279 while (cpopt && *cpopt) {
2282 if ((cpoptend = strchr(cpopt, ','))) {
2284 if ((cpoptarg = strchr(cpopt, '=')))
2287 if ((cpoptarg = strchr(cpopt, '=')))
2291 nextfield(&cp, &endcp);
2293 if (endcp > cp && *cp != '-') {
2301 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2302 *exflagsp |= MNT_EXRDONLY;
2303 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2304 !(allflag = strcmp(cpopt, "mapall")) ||
2305 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2307 parsecred(cpoptarg, cr);
2309 *exflagsp |= MNT_EXPORTANON;
2310 opt_flags |= OP_MAPALL;
2312 opt_flags |= OP_MAPROOT;
2313 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2314 !strcmp(cpopt, "m"))) {
2315 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2316 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2320 opt_flags |= OP_MASK;
2321 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2322 !strcmp(cpopt, "n"))) {
2323 if (strchr(cpoptarg, '/') != NULL) {
2325 fprintf(stderr, "setting OP_MASKLEN\n");
2326 opt_flags |= OP_MASKLEN;
2328 if (grp->gr_type != GT_NULL) {
2329 syslog(LOG_ERR, "network/host conflict");
2331 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2332 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2335 grp->gr_type = GT_NET;
2338 opt_flags |= OP_NET;
2339 } else if (!strcmp(cpopt, "alldirs")) {
2340 opt_flags |= OP_ALLDIRS;
2341 } else if (!strcmp(cpopt, "public")) {
2342 *exflagsp |= MNT_EXPUBLIC;
2343 } else if (!strcmp(cpopt, "webnfs")) {
2344 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2345 opt_flags |= OP_MAPALL;
2346 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2347 ep->ex_indexfile = strdup(cpoptarg);
2348 } else if (!strcmp(cpopt, "quiet")) {
2349 opt_flags |= OP_QUIET;
2350 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2351 if (parsesec(cpoptarg, ep))
2353 opt_flags |= OP_SEC;
2356 syslog(LOG_ERR, "bad opt %s", cpopt);
2375 * Translate a character string to the corresponding list of network
2376 * addresses for a hostname.
2379 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2381 struct grouplist *checkgrp;
2382 struct addrinfo *ai, *tai, hints;
2384 char host[NI_MAXHOST];
2386 if (grp->gr_type != GT_NULL) {
2387 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2390 memset(&hints, 0, sizeof hints);
2391 hints.ai_flags = AI_CANONNAME;
2392 hints.ai_protocol = IPPROTO_UDP;
2393 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2395 syslog(LOG_ERR,"can't get address info for host %s", cp);
2398 grp->gr_ptr.gt_addrinfo = ai;
2399 while (ai != NULL) {
2400 if (ai->ai_canonname == NULL) {
2401 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2402 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2403 strlcpy(host, "?", sizeof(host));
2404 ai->ai_canonname = strdup(host);
2405 ai->ai_flags |= AI_CANONNAME;
2408 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2410 * Sanity check: make sure we don't already have an entry
2411 * for this host in the grouplist.
2413 for (checkgrp = tgrp; checkgrp != NULL;
2414 checkgrp = checkgrp->gr_next) {
2415 if (checkgrp->gr_type != GT_HOST)
2417 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2418 tai = tai->ai_next) {
2419 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2423 "ignoring duplicate host %s\n",
2425 grp->gr_type = GT_IGNORE;
2431 grp->gr_type = GT_HOST;
2436 * Free up an exports list component
2439 free_exp(struct exportlist *ep)
2441 struct grouplist *grp, *tgrp;
2443 if (ep->ex_defdir) {
2444 free_host(ep->ex_defdir->dp_hosts);
2445 free((caddr_t)ep->ex_defdir);
2449 if (ep->ex_indexfile)
2450 free(ep->ex_indexfile);
2451 free_dir(ep->ex_dirl);
2452 grp = ep->ex_grphead;
2465 free_host(struct hostlist *hp)
2467 struct hostlist *hp2;
2476 static struct hostlist *
2479 struct hostlist *hp;
2481 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2482 if (hp == (struct hostlist *)NULL)
2484 hp->ht_next = (struct hostlist *)NULL;
2490 * Out of memory, fatal
2496 syslog(LOG_ERR, "out of memory");
2501 * Do the nmount() syscall with the update flag to push the export info into
2505 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2506 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
2509 struct addrinfo *ai;
2510 struct export_args *eap;
2518 struct nfsex_args nfsea;
2520 eap = &nfsea.export;
2528 bzero(eap, sizeof (struct export_args));
2529 bzero(errmsg, sizeof(errmsg));
2530 eap->ex_flags = exflags;
2531 eap->ex_anon = *anoncrp;
2532 eap->ex_indexfile = ep->ex_indexfile;
2533 if (grp->gr_type == GT_HOST)
2534 ai = grp->gr_ptr.gt_addrinfo;
2537 eap->ex_numsecflavors = ep->ex_numsecflavors;
2538 for (i = 0; i < eap->ex_numsecflavors; i++)
2539 eap->ex_secflavors[i] = ep->ex_secflavors[i];
2540 if (eap->ex_numsecflavors == 0) {
2541 eap->ex_numsecflavors = 1;
2542 eap->ex_secflavors[0] = AUTH_SYS;
2546 if (v4root_phase == 0) {
2547 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2548 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2549 build_iovec(&iov, &iovlen, "from", NULL, 0);
2550 build_iovec(&iov, &iovlen, "update", NULL, 0);
2551 build_iovec(&iov, &iovlen, "export", eap,
2552 sizeof (struct export_args));
2553 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2557 switch (grp->gr_type) {
2559 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
2561 eap->ex_addr = ai->ai_addr;
2562 eap->ex_addrlen = ai->ai_addrlen;
2563 eap->ex_masklen = 0;
2566 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
2570 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2572 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2574 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2575 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
2578 eap->ex_addr = NULL;
2579 eap->ex_addrlen = 0;
2580 eap->ex_mask = NULL;
2581 eap->ex_masklen = 0;
2588 syslog(LOG_ERR, "bad grouptype");
2596 * For V4:, use the nfssvc() syscall, instead of mount().
2598 if (v4root_phase == 2) {
2599 nfsea.fspec = v4root_dirpath;
2600 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2601 syslog(LOG_ERR, "Exporting V4: failed");
2607 * Maybe I should just use the fsb->f_mntonname path
2608 * instead of looping back up the dirp to the mount
2610 * Also, needs to know how to export all types of local
2611 * exportable filesystems and not just "ufs".
2613 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2614 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2615 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2616 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2617 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2618 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2621 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2625 cp = dirp + dirplen - 1;
2626 if (opt_flags & OP_QUIET) {
2630 if (errno == EPERM) {
2632 warnx("can't change attributes for %s: %s",
2635 "can't change attributes for %s: %s",
2640 if (opt_flags & OP_ALLDIRS) {
2641 if (errno == EINVAL)
2643 "-alldirs requested but %s is not a filesystem mountpoint",
2647 "could not remount %s: %m",
2652 /* back up over the last component */
2653 while (*cp == '/' && cp > dirp)
2655 while (*(cp - 1) != '/' && cp > dirp)
2659 warnx("mnt unsucc");
2660 syslog(LOG_ERR, "can't export %s %s",
2668 * Check that we're still on the same
2671 if (statfs(dirp, &fsb1) != 0 ||
2672 bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2673 sizeof (fsb1.f_fsid)) != 0) {
2676 "can't export %s %s", dirp,
2685 * For the experimental server:
2686 * If this is the public directory, get the file handle
2687 * and load it into the kernel via the nfssvc() syscall.
2689 if ((exflags & MNT_EXPUBLIC) != 0) {
2693 if (eap->ex_indexfile != NULL)
2694 public_name = eap->ex_indexfile;
2697 if (getfh(public_name, &fh) < 0)
2699 "Can't get public fh for %s", public_name);
2700 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2702 "Can't set public fh for %s", public_name);
2715 /* free strings allocated by strdup() in getmntopts.c */
2717 free(iov[0].iov_base); /* fstype */
2718 free(iov[2].iov_base); /* fspath */
2719 free(iov[4].iov_base); /* from */
2720 free(iov[6].iov_base); /* update */
2721 free(iov[8].iov_base); /* export */
2722 free(iov[10].iov_base); /* errmsg */
2724 /* free iov, allocated by realloc() */
2731 * Translate a net address.
2733 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2736 get_net(char *cp, struct netmsk *net, int maskflg)
2738 struct netent *np = NULL;
2739 char *name, *p, *prefp;
2740 struct sockaddr_in sin;
2741 struct sockaddr *sa = NULL;
2742 struct addrinfo hints, *ai = NULL;
2743 char netname[NI_MAXHOST];
2747 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2748 p = strchr(cp, '/');
2754 * Check for a numeric address first. We wish to avoid
2755 * possible DNS lookups in getnetbyname().
2757 if (isxdigit(*cp) || *cp == ':') {
2758 memset(&hints, 0, sizeof hints);
2759 /* Ensure the mask and the network have the same family. */
2760 if (maskflg && (opt_flags & OP_NET))
2761 hints.ai_family = net->nt_net.ss_family;
2762 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2763 hints.ai_family = net->nt_mask.ss_family;
2765 hints.ai_family = AF_UNSPEC;
2766 hints.ai_flags = AI_NUMERICHOST;
2767 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2769 if (sa != NULL && ai->ai_family == AF_INET) {
2771 * The address in `cp' is really a network address, so
2772 * use inet_network() to re-interpret this correctly.
2773 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2775 bzero(&sin, sizeof sin);
2776 sin.sin_family = AF_INET;
2777 sin.sin_len = sizeof sin;
2778 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2780 fprintf(stderr, "get_net: v4 addr %s\n",
2781 inet_ntoa(sin.sin_addr));
2782 sa = (struct sockaddr *)&sin;
2785 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2786 bzero(&sin, sizeof sin);
2787 sin.sin_family = AF_INET;
2788 sin.sin_len = sizeof sin;
2789 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2790 sa = (struct sockaddr *)&sin;
2796 /* The specified sockaddr is a mask. */
2797 if (checkmask(sa) != 0)
2799 bcopy(sa, &net->nt_mask, sa->sa_len);
2800 opt_flags |= OP_HAVEMASK;
2802 /* The specified sockaddr is a network address. */
2803 bcopy(sa, &net->nt_net, sa->sa_len);
2805 /* Get a network name for the export list. */
2808 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2809 NULL, 0, NI_NUMERICHOST) == 0) {
2814 if ((net->nt_name = strdup(name)) == NULL)
2818 * Extract a mask from either a "/<masklen>" suffix, or
2819 * from the class of an IPv4 address.
2821 if (opt_flags & OP_MASKLEN) {
2822 preflen = strtol(prefp, NULL, 10);
2823 if (preflen < 0L || preflen == LONG_MAX)
2825 bcopy(sa, &net->nt_mask, sa->sa_len);
2826 if (makemask(&net->nt_mask, (int)preflen) != 0)
2828 opt_flags |= OP_HAVEMASK;
2830 } else if (sa->sa_family == AF_INET &&
2831 (opt_flags & OP_MASK) == 0) {
2834 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2835 if (IN_CLASSA(addr))
2837 else if (IN_CLASSB(addr))
2839 else if (IN_CLASSC(addr))
2841 else if (IN_CLASSD(addr))
2844 preflen = 32; /* XXX */
2846 bcopy(sa, &net->nt_mask, sa->sa_len);
2847 makemask(&net->nt_mask, (int)preflen);
2848 opt_flags |= OP_HAVEMASK;
2863 * Parse out the next white space separated field
2866 nextfield(char **cp, char **endcp)
2872 while (*p == ' ' || *p == '\t')
2875 while (*p != '\0') {
2880 if (*p == '\\' && *(p + 1) != '\0')
2882 else if (*p == '\'' || *p == '"')
2884 else if (*p == ' ' || *p == '\t')
2893 * Get an exports file line. Skip over blank lines and handle line
2901 int totlen, cont_line;
2904 * Loop around ignoring blank lines and getting all continuation lines.
2909 if ((p = fgetln(exp_file, &len)) == NULL)
2914 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2924 if (linesize < len + totlen + 1) {
2925 linesize = len + totlen + 1;
2926 line = realloc(line, linesize);
2930 memcpy(line + totlen, p, len);
2932 line[totlen] = '\0';
2933 } while (totlen == 0 || cont_line);
2938 * Parse a description of a credential.
2941 parsecred(char *namelist, struct xucred *cr)
2948 gid_t groups[XU_NGROUPS + 1];
2951 cr->cr_version = XUCRED_VERSION;
2953 * Set up the unprivileged user.
2956 cr->cr_groups[0] = 65533;
2959 * Get the user's password table entry.
2962 name = strsep_quote(&names, ":");
2963 /* Bug? name could be NULL here */
2964 if (isdigit(*name) || *name == '-')
2965 pw = getpwuid(atoi(name));
2967 pw = getpwnam(name);
2969 * Credentials specified as those of a user.
2971 if (names == NULL) {
2973 syslog(LOG_ERR, "unknown user: %s", name);
2976 cr->cr_uid = pw->pw_uid;
2977 ngroups = XU_NGROUPS + 1;
2978 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) {
2979 syslog(LOG_ERR, "too many groups");
2980 ngroups = XU_NGROUPS + 1;
2984 * Compress out duplicate.
2986 cr->cr_ngroups = ngroups - 1;
2987 cr->cr_groups[0] = groups[0];
2988 for (cnt = 2; cnt < ngroups; cnt++)
2989 cr->cr_groups[cnt - 1] = groups[cnt];
2993 * Explicit credential specified as a colon separated list:
2997 cr->cr_uid = pw->pw_uid;
2998 else if (isdigit(*name) || *name == '-')
2999 cr->cr_uid = atoi(name);
3001 syslog(LOG_ERR, "unknown user: %s", name);
3005 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
3006 name = strsep_quote(&names, ":");
3007 if (isdigit(*name) || *name == '-') {
3008 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
3010 if ((gr = getgrnam(name)) == NULL) {
3011 syslog(LOG_ERR, "unknown group: %s", name);
3014 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
3017 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
3018 syslog(LOG_ERR, "too many groups");
3021 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
3023 * Routines that maintain the remote mounttab
3028 struct mountlist *mlp;
3029 char *host, *dirp, *cp;
3033 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
3034 if (errno == ENOENT)
3037 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
3041 while (fgets(str, STRSIZ, mlfile) != NULL) {
3043 host = strsep(&cp, " \t\n");
3044 dirp = strsep(&cp, " \t\n");
3045 if (host == NULL || dirp == NULL)
3047 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3048 if (mlp == (struct mountlist *)NULL)
3050 strncpy(mlp->ml_host, host, MNTNAMLEN);
3051 mlp->ml_host[MNTNAMLEN] = '\0';
3052 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3053 mlp->ml_dirp[MNTPATHLEN] = '\0';
3055 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3061 del_mlist(char *hostp, char *dirp)
3063 struct mountlist *mlp, *mlp2;
3067 SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
3068 if (!strcmp(mlp->ml_host, hostp) &&
3069 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3071 SLIST_REMOVE(&mlhead, mlp, mountlist, next);
3076 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3077 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3080 SLIST_FOREACH(mlp, &mlhead, next) {
3081 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3088 add_mlist(char *hostp, char *dirp)
3090 struct mountlist *mlp;
3093 SLIST_FOREACH(mlp, &mlhead, next) {
3094 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3098 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3099 if (mlp == (struct mountlist *)NULL)
3101 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3102 mlp->ml_host[MNTNAMLEN] = '\0';
3103 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3104 mlp->ml_dirp[MNTPATHLEN] = '\0';
3105 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3106 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3107 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3110 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3115 * Free up a group list.
3118 free_grp(struct grouplist *grp)
3120 if (grp->gr_type == GT_HOST) {
3121 if (grp->gr_ptr.gt_addrinfo != NULL)
3122 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3123 } else if (grp->gr_type == GT_NET) {
3124 if (grp->gr_ptr.gt_net.nt_name)
3125 free(grp->gr_ptr.gt_net.nt_name);
3132 SYSLOG(int pri, const char *fmt, ...)
3137 vfprintf(stderr, fmt, ap);
3143 * Check options for consistency.
3146 check_options(struct dirlist *dp)
3149 if (v4root_phase == 0 && dp == NULL)
3151 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3152 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3155 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3156 syslog(LOG_ERR, "-mask requires -network");
3159 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3160 syslog(LOG_ERR, "-network requires mask specification");
3163 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3164 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3167 if (v4root_phase > 0 &&
3169 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3170 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3173 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3174 syslog(LOG_ERR, "-alldirs has multiple directories");
3181 * Check an absolute directory path for any symbolic links. Return true
3184 check_dirpath(char *dirp)
3191 while (*cp && ret) {
3194 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3200 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3206 * Make a netmask according to the specified prefix length. The ss_family
3207 * and other non-address fields must be initialised before calling this.
3210 makemask(struct sockaddr_storage *ssp, int bitlen)
3215 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3217 if (bitlen > len * CHAR_BIT)
3220 for (i = 0; i < len; i++) {
3221 bits = MIN(CHAR_BIT, bitlen);
3222 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3229 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3230 * is acceptable (i.e. of the form 1...10....0).
3233 checkmask(struct sockaddr *sa)
3238 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3241 for (i = 0; i < len; i++)
3242 if (mask[i] != 0xff)
3245 if (~mask[i] & (u_char)(~mask[i] + 1))
3249 for (; i < len; i++)
3256 * Compare two sockaddrs according to a specified mask. Return zero if
3257 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3258 * If samask is NULL, perform a full comparison.
3261 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3263 unsigned char *p1, *p2, *mask;
3266 if (sa1->sa_family != sa2->sa_family ||
3267 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3268 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3271 switch (sa1->sa_family) {
3273 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3274 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3279 /* Simple binary comparison if no mask specified. */
3281 return (memcmp(p1, p2, len));
3283 /* Set up the mask, and do a mask-based comparison. */
3284 if (sa1->sa_family != samask->sa_family ||
3285 (mask = sa_rawaddr(samask, NULL)) == NULL)
3288 for (i = 0; i < len; i++)
3289 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3295 * Return a pointer to the part of the sockaddr that contains the
3296 * raw address, and set *nbytes to its length in bytes. Returns
3297 * NULL if the address family is unknown.
3300 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3304 switch (sa->sa_family) {
3306 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3307 p = &((struct sockaddr_in *)sa)->sin_addr;
3310 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3311 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3324 huphandler(int sig __unused)
3331 terminate(int sig __unused)
3333 pidfile_remove(pfh);
3334 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3335 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);