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;
121 int ex_numsecflavors;
122 int ex_secflavors[MAXSECFLAVORS];
123 int ex_defnumsecflavors;
124 int ex_defsecflavors[MAXSECFLAVORS];
126 SLIST_ENTRY(exportlist) entries;
129 #define EX_LINKED 0x1
132 struct sockaddr_storage nt_net;
133 struct sockaddr_storage nt_mask;
138 struct addrinfo *gt_addrinfo;
139 struct netmsk gt_net;
144 union grouptypes gr_ptr;
145 struct grouplist *gr_next;
146 int gr_numsecflavors;
147 int gr_secflavors[MAXSECFLAVORS];
153 #define GT_DEFAULT 0x3
154 #define GT_IGNORE 0x5
157 int ht_flag; /* Uses DP_xx bits */
158 struct grouplist *ht_grp;
159 struct hostlist *ht_next;
166 int fhr_numsecflavors;
170 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
173 static char *add_expdir(struct dirlist **, char *, int);
174 static void add_dlist(struct dirlist **, struct dirlist *,
175 struct grouplist *, int, struct exportlist *);
176 static void add_mlist(char *, char *);
177 static int check_dirpath(char *);
178 static int check_options(struct dirlist *);
179 static int checkmask(struct sockaddr *sa);
180 static int chk_host(struct dirlist *, struct sockaddr *, int *, int *,
182 static char *strsep_quote(char **stringp, const char *delim);
183 static int create_service(struct netconfig *nconf);
184 static void complete_service(struct netconfig *nconf, char *port_str);
185 static void clearout_service(void);
186 static void del_mlist(char *hostp, char *dirp);
187 static struct dirlist *dirp_search(struct dirlist *, char *);
188 static int do_mount(struct exportlist *, struct grouplist *, int,
189 struct xucred *, char *, int, struct statfs *);
190 static int do_opt(char **, char **, struct exportlist *,
191 struct grouplist *, int *, int *, struct xucred *);
192 static struct exportlist *ex_search(fsid_t *);
193 static struct exportlist *get_exp(void);
194 static void free_dir(struct dirlist *);
195 static void free_exp(struct exportlist *);
196 static void free_grp(struct grouplist *);
197 static void free_host(struct hostlist *);
198 static void get_exportlist(void);
199 static int get_host(char *, struct grouplist *, struct grouplist *);
200 static struct hostlist *get_ht(void);
201 static int get_line(void);
202 static void get_mountlist(void);
203 static int get_net(char *, struct netmsk *, int);
204 static void getexp_err(struct exportlist *, struct grouplist *, const char *);
205 static struct grouplist *get_grp(void);
206 static void hang_dirp(struct dirlist *, struct grouplist *,
207 struct exportlist *, int);
208 static void huphandler(int sig);
209 static int makemask(struct sockaddr_storage *ssp, int bitlen);
210 static void mntsrv(struct svc_req *, SVCXPRT *);
211 static void nextfield(char **, char **);
212 static void out_of_mem(void);
213 static void parsecred(char *, struct xucred *);
214 static int parsesec(char *, struct exportlist *);
215 static int put_exlist(struct dirlist *, XDR *, struct dirlist *,
217 static void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
218 static int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
219 struct sockaddr *samask);
220 static int scan_tree(struct dirlist *, struct sockaddr *);
221 static void usage(void);
222 static int xdr_dir(XDR *, char *);
223 static int xdr_explist(XDR *, caddr_t);
224 static int xdr_explist_brief(XDR *, caddr_t);
225 static int xdr_explist_common(XDR *, caddr_t, int);
226 static int xdr_fhs(XDR *, caddr_t);
227 static int xdr_mlist(XDR *, caddr_t);
228 static void terminate(int);
230 static SLIST_HEAD(, exportlist) exphead = SLIST_HEAD_INITIALIZER(exphead);
231 static SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(mlhead);
232 static struct grouplist *grphead;
233 static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
234 static char **exnames;
235 static char **hosts = NULL;
236 static struct xucred def_anon = {
243 static int force_v2 = 0;
244 static int resvport_only = 1;
245 static int nhosts = 0;
246 static int dir_only = 1;
247 static int dolog = 0;
248 static int got_sighup = 0;
249 static int xcreated = 0;
251 static char *svcport_str = NULL;
252 static int mallocd_svcport = 0;
254 static int sock_fdcnt;
255 static int sock_fdpos;
256 static int suspend_nfsd = 0;
258 static int opt_flags;
259 static int have_v6 = 1;
261 static int v4root_phase = 0;
262 static char v4root_dirpath[PATH_MAX + 1];
263 static int has_publicfh = 0;
265 static struct pidfh *pfh = NULL;
266 /* Bits for opt_flags above */
267 #define OP_MAPROOT 0x01
268 #define OP_MAPALL 0x02
272 #define OP_ALLDIRS 0x40
273 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
274 #define OP_QUIET 0x100
275 #define OP_MASKLEN 0x200
279 static int debug = 1;
280 static void SYSLOG(int, const char *, ...) __printflike(2, 3);
281 #define syslog SYSLOG
283 static int debug = 0;
287 * Similar to strsep(), but it allows for quoted strings
288 * and escaped characters.
290 * It returns the string (or NULL, if *stringp is NULL),
291 * which is a de-quoted version of the string if necessary.
293 * It modifies *stringp in place.
296 strsep_quote(char **stringp, const char *delim)
298 char *srcptr, *dstptr, *retval;
301 if (stringp == NULL || *stringp == NULL)
304 srcptr = dstptr = retval = *stringp;
308 * We're looking for several edge cases here.
309 * First: if we're in quote state (quot != 0),
310 * then we ignore the delim characters, but otherwise
311 * process as normal, unless it is the quote character.
312 * Second: if the current character is a backslash,
313 * we take the next character as-is, without checking
314 * for delim, quote, or backslash. Exception: if the
315 * next character is a NUL, that's the end of the string.
316 * Third: if the character is a quote character, we toggle
318 * Otherwise: check the current character for NUL, or
319 * being in delim, and end the string if either is true.
321 if (*srcptr == '\\') {
324 * The edge case here is if the next character
325 * is NUL, we want to stop processing. But if
326 * it's not NUL, then we simply want to copy it.
329 *dstptr++ = *srcptr++;
333 if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
337 if (quot && *srcptr == quot) {
338 /* End of the quoted part */
343 if (!quot && strchr(delim, *srcptr))
345 *dstptr++ = *srcptr++;
348 *dstptr = 0; /* Terminate the string */
349 *stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
354 * Mountd server for NFS mount protocol as described in:
355 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
356 * The optional arguments are the exports file name
357 * default: _PATH_EXPORTS
358 * and "-n" to allow nonroot mount.
361 main(int argc, char **argv)
364 struct netconfig *nconf;
365 char *endptr, **hosts_bak;
370 int maxrec = RPC_MAXDATASIZE;
371 int attempt_cnt, port_len, port_pos, ret;
374 /* Check that another mountd isn't already running. */
375 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
378 errx(1, "mountd already running, pid: %d.", otherpid);
379 warn("cannot open or create pidfile");
382 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
388 while ((c = getopt(argc, argv, "2deh:lnp:rS")) != -1)
394 /* now a no-op, since this is the default */
403 debug = debug ? 0 : 1;
410 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
411 if (endptr == NULL || *endptr != '\0' ||
412 svcport == 0 || svcport >= IPPORT_MAX)
414 svcport_str = strdup(optarg);
419 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
420 if (hosts_bak == NULL) {
422 for (k = 0; k < nhosts; k++)
429 hosts[nhosts - 1] = strdup(optarg);
430 if (hosts[nhosts - 1] == NULL) {
431 for (k = 0; k < (nhosts - 1); k++)
444 if (modfind("nfsd") < 0) {
445 /* Not present in kernel, try loading it */
446 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
447 errx(1, "NFS server is not available");
452 grphead = (struct grouplist *)NULL;
456 exnames = exnames_default;
457 openlog("mountd", LOG_PID, LOG_DAEMON);
459 warnx("getting export list");
462 warnx("getting mount list");
468 signal(SIGINT, SIG_IGN);
469 signal(SIGQUIT, SIG_IGN);
471 signal(SIGHUP, huphandler);
472 signal(SIGTERM, terminate);
473 signal(SIGPIPE, SIG_IGN);
477 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
478 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
479 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
481 if (!resvport_only) {
482 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
483 &resvport_only, sizeof(resvport_only)) != 0 &&
485 syslog(LOG_ERR, "sysctl: %m");
491 * If no hosts were specified, add a wildcard entry to bind to
492 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
496 hosts = malloc(sizeof(char *));
504 hosts_bak = realloc(hosts, (nhosts + 2) *
506 if (hosts_bak == NULL) {
507 for (k = 0; k < nhosts; k++)
514 hosts[nhosts - 2] = "::1";
516 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
517 if (hosts_bak == NULL) {
518 for (k = 0; k < nhosts; k++)
528 hosts[nhosts - 1] = "127.0.0.1";
536 nc_handle = setnetconfig();
537 while ((nconf = getnetconfig(nc_handle))) {
538 if (nconf->nc_flag & NC_VISIBLE) {
539 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
543 ret = create_service(nconf);
545 /* Ignore this call */
549 * Failed to bind port, so close off
550 * all sockets created and try again
551 * if the port# was dynamically
552 * assigned via bind(2).
555 if (mallocd_svcport != 0 &&
556 attempt_cnt < GETPORT_MAXTRY) {
563 "bindresvport_sa: %m");
567 /* Start over at the first service. */
571 nc_handle = setnetconfig();
573 } else if (mallocd_svcport != 0 &&
574 attempt_cnt == GETPORT_MAXTRY) {
576 * For the last attempt, allow
577 * different port #s for each nconf
578 * by saving the svcport_str and
579 * setting it back to NULL.
581 port_list = realloc(port_list,
582 (port_len + 1) * sizeof(char *));
583 if (port_list == NULL)
585 port_list[port_len++] = svcport_str;
594 * Successfully bound the ports, so call complete_service() to
595 * do the rest of the setup on the service(s).
599 nc_handle = setnetconfig();
600 while ((nconf = getnetconfig(nc_handle))) {
601 if (nconf->nc_flag & NC_VISIBLE) {
602 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
605 } else if (port_list != NULL) {
606 if (port_pos >= port_len) {
607 syslog(LOG_ERR, "too many port#s");
610 complete_service(nconf, port_list[port_pos++]);
612 complete_service(nconf, svcport_str);
615 endnetconfig(nc_handle);
617 if (port_list != NULL) {
618 for (port_pos = 0; port_pos < port_len; port_pos++)
619 free(port_list[port_pos]);
624 syslog(LOG_ERR, "could not create any services");
628 /* Expand svc_run() here so that we can call get_exportlist(). */
635 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
639 syslog(LOG_ERR, "mountd died: select: %m");
644 svc_getreqset(&readfds);
650 * This routine creates and binds sockets on the appropriate
651 * addresses. It gets called one time for each transport.
652 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
653 * bind failed with EADDRINUSE.
654 * Any file descriptors that have been created are stored in sock_fd and
655 * the total count of them is maintained in sock_fdcnt.
658 create_service(struct netconfig *nconf)
660 struct addrinfo hints, *res = NULL;
661 struct sockaddr_in *sin;
662 struct sockaddr_in6 *sin6;
663 struct __rpc_sockinfo si;
669 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
672 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
673 (nconf->nc_semantics != NC_TPI_COTS) &&
674 (nconf->nc_semantics != NC_TPI_COTS_ORD))
675 return (1); /* not my type */
678 * XXX - using RPC library internal functions.
680 if (!__rpc_nconf2sockinfo(nconf, &si)) {
681 syslog(LOG_ERR, "cannot get information for %s",
686 /* Get mountd's address on this transport */
687 memset(&hints, 0, sizeof hints);
688 hints.ai_family = si.si_af;
689 hints.ai_socktype = si.si_socktype;
690 hints.ai_protocol = si.si_proto;
693 * Bind to specific IPs if asked to
696 while (nhostsbak > 0) {
698 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
701 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
704 hints.ai_flags = AI_PASSIVE;
707 * XXX - using RPC library internal functions.
709 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
711 if (errno == EAFNOSUPPORT &&
712 nconf->nc_semantics != NC_TPI_CLTS)
715 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
716 "cannot create socket for %s", nconf->nc_netid);
722 switch (hints.ai_family) {
724 if (inet_pton(AF_INET, hosts[nhostsbak],
726 hints.ai_flags |= AI_NUMERICHOST;
729 * Skip if we have an AF_INET6 address.
731 if (inet_pton(AF_INET6, hosts[nhostsbak],
739 if (inet_pton(AF_INET6, hosts[nhostsbak],
741 hints.ai_flags |= AI_NUMERICHOST;
744 * Skip if we have an AF_INET address.
746 if (inet_pton(AF_INET, hosts[nhostsbak],
754 * We're doing host-based access checks here, so don't
755 * allow v4-in-v6 to confuse things. The kernel will
756 * disable it by default on NFS sockets too.
758 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
761 "can't disable v4-in-v6 on IPv6 socket");
770 * If no hosts were specified, just bind to INADDR_ANY
772 if (strcmp("*", hosts[nhostsbak]) == 0) {
773 if (svcport_str == NULL) {
774 res = malloc(sizeof(struct addrinfo));
778 res->ai_flags = hints.ai_flags;
779 res->ai_family = hints.ai_family;
780 res->ai_protocol = hints.ai_protocol;
781 switch (res->ai_family) {
783 sin = malloc(sizeof(struct sockaddr_in));
786 sin->sin_family = AF_INET;
787 sin->sin_port = htons(0);
788 sin->sin_addr.s_addr = htonl(INADDR_ANY);
789 res->ai_addr = (struct sockaddr*) sin;
790 res->ai_addrlen = (socklen_t)
791 sizeof(struct sockaddr_in);
794 sin6 = malloc(sizeof(struct sockaddr_in6));
797 sin6->sin6_family = AF_INET6;
798 sin6->sin6_port = htons(0);
799 sin6->sin6_addr = in6addr_any;
800 res->ai_addr = (struct sockaddr*) sin6;
801 res->ai_addrlen = (socklen_t)
802 sizeof(struct sockaddr_in6);
805 syslog(LOG_ERR, "bad addr fam %d",
810 if ((aicode = getaddrinfo(NULL, svcport_str,
811 &hints, &res)) != 0) {
813 "cannot get local address for %s: %s",
815 gai_strerror(aicode));
821 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
822 &hints, &res)) != 0) {
824 "cannot get local address for %s: %s",
825 nconf->nc_netid, gai_strerror(aicode));
832 sock_fd[sock_fdcnt - 1] = fd;
834 /* Now, attempt the bind. */
835 r = bindresvport_sa(fd, res->ai_addr);
837 if (errno == EADDRINUSE && mallocd_svcport != 0) {
838 if (mallocd_res != 0) {
845 syslog(LOG_ERR, "bindresvport_sa: %m");
849 if (svcport_str == NULL) {
850 svcport_str = malloc(NI_MAXSERV * sizeof(char));
851 if (svcport_str == NULL)
855 if (getnameinfo(res->ai_addr,
856 res->ai_addr->sa_len, NULL, NI_MAXHOST,
857 svcport_str, NI_MAXSERV * sizeof(char),
858 NI_NUMERICHOST | NI_NUMERICSERV))
859 errx(1, "Cannot get port number");
861 if (mallocd_res != 0) {
872 * Called after all the create_service() calls have succeeded, to complete
873 * the setup and registration.
876 complete_service(struct netconfig *nconf, char *port_str)
878 struct addrinfo hints, *res = NULL;
879 struct __rpc_sockinfo si;
880 struct netbuf servaddr;
881 SVCXPRT *transp = NULL;
882 int aicode, fd, nhostsbak;
885 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
886 (nconf->nc_semantics != NC_TPI_COTS) &&
887 (nconf->nc_semantics != NC_TPI_COTS_ORD))
888 return; /* not my type */
891 * XXX - using RPC library internal functions.
893 if (!__rpc_nconf2sockinfo(nconf, &si)) {
894 syslog(LOG_ERR, "cannot get information for %s",
900 while (nhostsbak > 0) {
902 if (sock_fdpos >= sock_fdcnt) {
903 /* Should never happen. */
904 syslog(LOG_ERR, "Ran out of socket fd's");
907 fd = sock_fd[sock_fdpos++];
911 if (nconf->nc_semantics != NC_TPI_CLTS)
912 listen(fd, SOMAXCONN);
914 if (nconf->nc_semantics == NC_TPI_CLTS )
915 transp = svc_dg_create(fd, 0, 0);
917 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
920 if (transp != (SVCXPRT *) NULL) {
921 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
924 "can't register %s MOUNTVERS service",
927 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
930 "can't register %s MOUNTVERS3 service",
934 syslog(LOG_WARNING, "can't create %s services",
937 if (registered == 0) {
939 memset(&hints, 0, sizeof hints);
940 hints.ai_flags = AI_PASSIVE;
941 hints.ai_family = si.si_af;
942 hints.ai_socktype = si.si_socktype;
943 hints.ai_protocol = si.si_proto;
945 if ((aicode = getaddrinfo(NULL, port_str, &hints,
947 syslog(LOG_ERR, "cannot get local address: %s",
948 gai_strerror(aicode));
952 servaddr.buf = malloc(res->ai_addrlen);
953 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
954 servaddr.len = res->ai_addrlen;
956 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
957 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
966 * Clear out sockets after a failure to bind one of them, so that the
967 * cycle of socket creation/binding can start anew.
970 clearout_service(void)
974 for (i = 0; i < sock_fdcnt; i++) {
975 if (sock_fd[i] >= 0) {
976 shutdown(sock_fd[i], SHUT_RDWR);
986 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
987 "[-S] [-h <bindip>] [export_file ...]\n");
992 * The mount rpc service
995 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
997 struct exportlist *ep;
1002 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1003 int lookup_failed = 1;
1004 struct sockaddr *saddr;
1006 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1007 int bad = 0, defset, hostset;
1008 sigset_t sighup_mask;
1009 int numsecflavors, *secflavorsp;
1011 sigemptyset(&sighup_mask);
1012 sigaddset(&sighup_mask, SIGHUP);
1013 saddr = svc_getrpccaller(transp)->buf;
1014 switch (saddr->sa_family) {
1016 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1019 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1022 syslog(LOG_ERR, "request from unknown address family");
1025 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
1027 getnameinfo(saddr, saddr->sa_len, numerichost,
1028 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1029 switch (rqstp->rq_proc) {
1031 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1032 syslog(LOG_ERR, "can't send reply");
1035 if (sport >= IPPORT_RESERVED && resvport_only) {
1037 "mount request from %s from unprivileged port",
1039 svcerr_weakauth(transp);
1042 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1043 syslog(LOG_NOTICE, "undecodable mount request from %s",
1045 svcerr_decode(transp);
1050 * Get the real pathname and make sure it is a directory
1051 * or a regular file if the -r option was specified
1054 if (realpath(rpcpath, dirpath) == NULL ||
1055 stat(dirpath, &stb) < 0 ||
1056 statfs(dirpath, &fsb) < 0) {
1057 chdir("/"); /* Just in case realpath doesn't */
1059 "mount request from %s for non existent path %s",
1060 numerichost, dirpath);
1062 warnx("stat failed on %s", dirpath);
1063 bad = ENOENT; /* We will send error reply later */
1066 !S_ISDIR(stb.st_mode) &&
1067 (dir_only || !S_ISREG(stb.st_mode))) {
1069 "mount request from %s for non-directory path %s",
1070 numerichost, dirpath);
1072 warnx("mounting non-directory %s", dirpath);
1073 bad = ENOTDIR; /* We will send error reply later */
1076 /* Check in the exports list */
1077 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1081 ep = ex_search(&fsb.f_fsid);
1082 hostset = defset = 0;
1083 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1084 &numsecflavors, &secflavorsp) ||
1085 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1086 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1088 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1089 scan_tree(ep->ex_dirl, saddr) == 0))) {
1091 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1093 syslog(LOG_ERR, "can't send reply");
1094 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1097 if (hostset & DP_HOSTSET) {
1098 fhr.fhr_flag = hostset;
1099 fhr.fhr_numsecflavors = numsecflavors;
1100 fhr.fhr_secflavors = secflavorsp;
1102 fhr.fhr_flag = defset;
1103 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1104 fhr.fhr_secflavors = ep->ex_defsecflavors;
1106 fhr.fhr_vers = rqstp->rq_vers;
1107 /* Get the file handle */
1108 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1109 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1111 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1112 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1114 syslog(LOG_ERR, "can't send reply");
1115 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1118 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1120 syslog(LOG_ERR, "can't send reply");
1122 add_mlist(host, dirpath);
1124 add_mlist(numerichost, dirpath);
1126 warnx("mount successful");
1129 "mount request succeeded from %s for %s",
1130 numerichost, dirpath);
1135 "mount request denied from %s for %s",
1136 numerichost, dirpath);
1139 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1141 syslog(LOG_ERR, "can't send reply");
1142 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1144 case MOUNTPROC_DUMP:
1145 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1146 syslog(LOG_ERR, "can't send reply");
1149 "dump request succeeded from %s",
1152 case MOUNTPROC_UMNT:
1153 if (sport >= IPPORT_RESERVED && resvport_only) {
1155 "umount request from %s from unprivileged port",
1157 svcerr_weakauth(transp);
1160 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1161 syslog(LOG_NOTICE, "undecodable umount request from %s",
1163 svcerr_decode(transp);
1166 if (realpath(rpcpath, dirpath) == NULL) {
1167 syslog(LOG_NOTICE, "umount request from %s "
1168 "for non existent path %s",
1169 numerichost, dirpath);
1171 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1172 syslog(LOG_ERR, "can't send reply");
1174 del_mlist(host, dirpath);
1175 del_mlist(numerichost, dirpath);
1178 "umount request succeeded from %s for %s",
1179 numerichost, dirpath);
1181 case MOUNTPROC_UMNTALL:
1182 if (sport >= IPPORT_RESERVED && resvport_only) {
1184 "umountall request from %s from unprivileged port",
1186 svcerr_weakauth(transp);
1189 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1190 syslog(LOG_ERR, "can't send reply");
1192 del_mlist(host, NULL);
1193 del_mlist(numerichost, NULL);
1196 "umountall request succeeded from %s",
1199 case MOUNTPROC_EXPORT:
1200 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1201 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1203 syslog(LOG_ERR, "can't send reply");
1206 "export request succeeded from %s",
1210 svcerr_noproc(transp);
1216 * Xdr conversion for a dirpath string
1219 xdr_dir(XDR *xdrsp, char *dirp)
1221 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1225 * Xdr routine to generate file handle reply
1228 xdr_fhs(XDR *xdrsp, caddr_t cp)
1230 struct fhreturn *fhrp = (struct fhreturn *)cp;
1231 u_long ok = 0, len, auth;
1234 if (!xdr_long(xdrsp, &ok))
1236 switch (fhrp->fhr_vers) {
1238 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1241 if (!xdr_long(xdrsp, &len))
1243 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1245 if (fhrp->fhr_numsecflavors) {
1246 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1248 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1249 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1255 if (!xdr_long(xdrsp, &len))
1257 return (xdr_long(xdrsp, &auth));
1264 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1266 struct mountlist *mlp;
1271 SLIST_FOREACH(mlp, &mlhead, next) {
1272 if (!xdr_bool(xdrsp, &true))
1274 strp = &mlp->ml_host[0];
1275 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1277 strp = &mlp->ml_dirp[0];
1278 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1281 if (!xdr_bool(xdrsp, &false))
1287 * Xdr conversion for export list
1290 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1292 struct exportlist *ep;
1295 sigset_t sighup_mask;
1297 sigemptyset(&sighup_mask);
1298 sigaddset(&sighup_mask, SIGHUP);
1299 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1301 SLIST_FOREACH(ep, &exphead, entries) {
1303 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1306 if (ep->ex_defdir && putdef == 0 &&
1307 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1311 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1312 if (!xdr_bool(xdrsp, &false))
1316 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1321 * Called from xdr_explist() to traverse the tree and export the
1325 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1328 struct grouplist *grp;
1329 struct hostlist *hp;
1336 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1338 if (!xdr_bool(xdrsp, &true))
1341 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1343 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1348 if (!xdr_bool(xdrsp, &true))
1351 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1353 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1354 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1358 if (grp->gr_type == GT_HOST) {
1359 if (!xdr_bool(xdrsp, &true))
1361 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1362 if (!xdr_string(xdrsp, &strp,
1365 } else if (grp->gr_type == GT_NET) {
1366 if (!xdr_bool(xdrsp, &true))
1368 strp = grp->gr_ptr.gt_net.nt_name;
1369 if (!xdr_string(xdrsp, &strp,
1374 if (gotalldir && hp == (struct hostlist *)NULL) {
1380 if (!xdr_bool(xdrsp, &false))
1382 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1389 xdr_explist(XDR *xdrsp, caddr_t cp)
1392 return xdr_explist_common(xdrsp, cp, 0);
1396 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1399 return xdr_explist_common(xdrsp, cp, 1);
1403 static size_t linesize;
1404 static FILE *exp_file;
1407 * Get the export list from one, currently open file
1410 get_exportlist_one(void)
1412 struct exportlist *ep;
1413 struct grouplist *grp, *tgrp;
1414 struct dirlist *dirhead;
1417 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1418 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1421 dirhead = (struct dirlist *)NULL;
1422 while (get_line()) {
1424 warnx("got line %s", line);
1426 nextfield(&cp, &endcp);
1435 exflags = MNT_EXPORTED;
1438 ep = (struct exportlist *)NULL;
1442 * Handle the V4 root dir.
1444 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1446 * V4: just indicates that it is the v4 root point,
1447 * so skip over that and set v4root_phase.
1449 if (v4root_phase > 0) {
1450 syslog(LOG_ERR, "V4:duplicate line, ignored");
1455 nextfield(&cp, &endcp);
1459 * Create new exports list entry
1462 tgrp = grp = get_grp();
1464 if (len > MNTNAMLEN) {
1465 getexp_err(ep, tgrp, "mountpoint too long");
1469 if (ep == (struct exportlist *)NULL) {
1470 getexp_err(ep, tgrp,
1471 "flag before export path definition");
1475 warnx("doing opt %s", cp);
1477 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1479 getexp_err(ep, tgrp, NULL);
1482 } else if (*cp == '/') {
1485 if (v4root_phase > 1) {
1487 getexp_err(ep, tgrp, "Multiple V4 dirs");
1491 if (check_dirpath(cp) &&
1492 statfs(cp, &fsb) >= 0) {
1493 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1494 syslog(LOG_ERR, "Warning: exporting of "
1495 "automounted fs %s not supported", cp);
1497 getexp_err(ep, tgrp, "dirs must be first");
1500 if (v4root_phase == 1) {
1502 getexp_err(ep, tgrp, "Multiple V4 dirs");
1505 if (strlen(v4root_dirpath) == 0) {
1506 strlcpy(v4root_dirpath, cp,
1507 sizeof (v4root_dirpath));
1508 } else if (strcmp(v4root_dirpath, cp)
1511 "different V4 dirpath %s", cp);
1512 getexp_err(ep, tgrp, NULL);
1521 if (ep->ex_fs.val[0] !=
1522 fsb.f_fsid.val[0] ||
1524 fsb.f_fsid.val[1]) {
1525 getexp_err(ep, tgrp,
1531 * See if this directory is already
1534 ep = ex_search(&fsb.f_fsid);
1535 if (ep == (struct exportlist *)NULL) {
1537 ep->ex_fs = fsb.f_fsid;
1538 ep->ex_fsdir = strdup(fsb.f_mntonname);
1539 if (ep->ex_fsdir == NULL)
1543 "making new ep fs=0x%x,0x%x",
1547 warnx("found ep fs=0x%x,0x%x",
1553 * Add dirpath to export mount point.
1555 dirp = add_expdir(&dirhead, cp, len);
1559 getexp_err(ep, tgrp,
1560 "symbolic link in export path or statfs failed");
1568 if (ep == (struct exportlist *)NULL) {
1569 getexp_err(ep, tgrp,
1570 "host(s) before export path definition");
1575 * Get the host or netgroup.
1578 netgrp = getnetgrent(&hst, &usr, &dom);
1581 grp->gr_next = get_grp();
1587 "null hostname in netgroup %s, skipping", cp);
1588 grp->gr_type = GT_IGNORE;
1589 } else if (get_host(hst, grp, tgrp)) {
1591 "bad host %s in netgroup %s, skipping", hst, cp);
1592 grp->gr_type = GT_IGNORE;
1594 } else if (get_host(cp, grp, tgrp)) {
1595 syslog(LOG_ERR, "bad host %s, skipping", cp);
1596 grp->gr_type = GT_IGNORE;
1599 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1604 nextfield(&cp, &endcp);
1607 if (check_options(dirhead)) {
1608 getexp_err(ep, tgrp, NULL);
1612 grp->gr_type = GT_DEFAULT;
1614 warnx("adding a default entry");
1617 * Don't allow a network export coincide with a list of
1618 * host(s) on the same line.
1620 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1621 getexp_err(ep, tgrp, "network/host conflict");
1625 * If an export list was specified on this line, make sure
1626 * that we have at least one valid entry, otherwise skip it.
1630 while (grp && grp->gr_type == GT_IGNORE)
1633 getexp_err(ep, tgrp, "no valid entries");
1638 if (v4root_phase == 1) {
1639 getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1644 * Loop through hosts, pushing the exports into the kernel.
1645 * After loop, tgrp points to the start of the list and
1646 * grp points to the last entry in the list.
1650 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1652 getexp_err(ep, tgrp, NULL);
1655 } while (grp->gr_next && (grp = grp->gr_next));
1658 * For V4: don't enter in mount lists.
1660 if (v4root_phase > 0 && v4root_phase <= 2) {
1662 * Since these structures aren't used by mountd,
1667 while (tgrp != NULL) {
1669 tgrp = tgrp->gr_next;
1676 * Success. Update the data structures.
1679 hang_dirp(dirhead, tgrp, ep, opt_flags);
1680 grp->gr_next = grphead;
1683 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1687 dirhead = (struct dirlist *)NULL;
1688 if ((ep->ex_flag & EX_LINKED) == 0) {
1689 SLIST_INSERT_HEAD(&exphead, ep, entries);
1691 ep->ex_flag |= EX_LINKED;
1697 dirhead = (struct dirlist *)NULL;
1703 * Get the export list from all specified files
1706 get_exportlist(void)
1708 struct exportlist *ep, *ep2;
1709 struct grouplist *grp, *tgrp;
1710 struct export_args export;
1712 struct statfs *fsp, *mntbufp;
1713 struct xvfsconf vfc;
1718 struct nfsex_args eargs;
1720 if (suspend_nfsd != 0)
1721 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1722 v4root_dirpath[0] = '\0';
1723 bzero(&export, sizeof(export));
1724 export.ex_flags = MNT_DELEXPORT;
1727 bzero(errmsg, sizeof(errmsg));
1730 * First, get rid of the old list
1732 SLIST_FOREACH_SAFE(ep, &exphead, entries, ep2) {
1733 SLIST_REMOVE(&exphead, ep, exportlist, entries);
1743 grphead = (struct grouplist *)NULL;
1746 * and the old V4 root dir.
1748 bzero(&eargs, sizeof (eargs));
1749 eargs.export.ex_flags = MNT_DELEXPORT;
1750 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1752 syslog(LOG_ERR, "Can't delete exports for V4:");
1755 * and clear flag that notes if a public fh has been exported.
1760 * And delete exports that are in the kernel for all local
1762 * XXX: Should know how to handle all local exportable filesystems.
1764 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1767 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1768 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1769 build_iovec(&iov, &iovlen, "from", NULL, 0);
1770 build_iovec(&iov, &iovlen, "update", NULL, 0);
1771 build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1772 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1775 for (i = 0; i < num; i++) {
1777 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1778 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1784 * We do not need to delete "export" flag from
1785 * filesystems that do not have it set.
1787 if (!(fsp->f_flags & MNT_EXPORTED))
1790 * Do not delete export for network filesystem by
1791 * passing "export" arg to nmount().
1792 * It only makes sense to do this for local filesystems.
1794 if (vfc.vfc_flags & VFCF_NETWORK)
1797 iov[1].iov_base = fsp->f_fstypename;
1798 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1799 iov[3].iov_base = fsp->f_mntonname;
1800 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1801 iov[5].iov_base = fsp->f_mntfromname;
1802 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1806 * EXDEV is returned when path exists but is not a
1807 * mount point. May happens if raced with unmount.
1809 if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1810 errno != ENOENT && errno != ENOTSUP && errno != EXDEV) {
1812 "can't delete exports for %s: %m %s",
1813 fsp->f_mntonname, errmsg);
1818 /* Free strings allocated by strdup() in getmntopts.c */
1819 free(iov[0].iov_base); /* fstype */
1820 free(iov[2].iov_base); /* fspath */
1821 free(iov[4].iov_base); /* from */
1822 free(iov[6].iov_base); /* update */
1823 free(iov[8].iov_base); /* export */
1824 free(iov[10].iov_base); /* errmsg */
1826 /* free iov, allocated by realloc() */
1832 * Read in the exports file and build the list, calling
1833 * nmount() as we go along to push the export rules into the kernel.
1836 for (i = 0; exnames[i] != NULL; i++) {
1838 warnx("reading exports from %s", exnames[i]);
1839 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1840 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1843 get_exportlist_one();
1848 syslog(LOG_ERR, "can't open any exports file");
1853 * If there was no public fh, clear any previous one set.
1855 if (has_publicfh == 0)
1856 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1858 /* Resume the nfsd. If they weren't suspended, this is harmless. */
1859 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1863 * Allocate an export list element
1865 static struct exportlist *
1868 struct exportlist *ep;
1870 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
1871 if (ep == (struct exportlist *)NULL)
1877 * Allocate a group list element
1879 static struct grouplist *
1882 struct grouplist *gp;
1884 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
1885 if (gp == (struct grouplist *)NULL)
1891 * Clean up upon an error in get_exportlist().
1894 getexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
1896 struct grouplist *tgrp;
1898 if (!(opt_flags & OP_QUIET)) {
1900 syslog(LOG_ERR, "bad exports list line '%s': %s", line,
1903 syslog(LOG_ERR, "bad exports list line '%s'", line);
1905 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1915 * Search the export list for a matching fs.
1917 static struct exportlist *
1918 ex_search(fsid_t *fsid)
1920 struct exportlist *ep;
1922 SLIST_FOREACH(ep, &exphead, entries) {
1923 if (ep->ex_fs.val[0] == fsid->val[0] &&
1924 ep->ex_fs.val[1] == fsid->val[1])
1932 * Add a directory path to the list.
1935 add_expdir(struct dirlist **dpp, char *cp, int len)
1939 dp = malloc(sizeof (struct dirlist));
1940 if (dp == (struct dirlist *)NULL)
1943 dp->dp_right = (struct dirlist *)NULL;
1945 dp->dp_hosts = (struct hostlist *)NULL;
1946 dp->dp_dirp = strndup(cp, len);
1947 if (dp->dp_dirp == NULL)
1950 return (dp->dp_dirp);
1954 * Hang the dir list element off the dirpath binary tree as required
1955 * and update the entry for host.
1958 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1961 struct hostlist *hp;
1962 struct dirlist *dp2;
1964 if (flags & OP_ALLDIRS) {
1969 if (grp == (struct grouplist *)NULL) {
1970 ep->ex_defdir->dp_flag |= DP_DEFSET;
1971 /* Save the default security flavors list. */
1972 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1973 if (ep->ex_numsecflavors > 0)
1974 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1975 sizeof(ep->ex_secflavors));
1976 } else while (grp) {
1979 hp->ht_next = ep->ex_defdir->dp_hosts;
1980 ep->ex_defdir->dp_hosts = hp;
1981 /* Save the security flavors list for this host set. */
1982 grp->gr_numsecflavors = ep->ex_numsecflavors;
1983 if (ep->ex_numsecflavors > 0)
1984 memcpy(grp->gr_secflavors, ep->ex_secflavors,
1985 sizeof(ep->ex_secflavors));
1991 * Loop through the directories adding them to the tree.
1995 add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
2002 * Traverse the binary tree either updating a node that is already there
2003 * for the new directory or adding the new node.
2006 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2007 int flags, struct exportlist *ep)
2010 struct hostlist *hp;
2015 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2017 add_dlist(&dp->dp_left, newdp, grp, flags, ep);
2019 } else if (cmp < 0) {
2020 add_dlist(&dp->dp_right, newdp, grp, flags, ep);
2023 free((caddr_t)newdp);
2026 dp->dp_left = (struct dirlist *)NULL;
2032 * Hang all of the host(s) off of the directory point.
2037 hp->ht_next = dp->dp_hosts;
2039 /* Save the security flavors list for this host set. */
2040 grp->gr_numsecflavors = ep->ex_numsecflavors;
2041 if (ep->ex_numsecflavors > 0)
2042 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2043 sizeof(ep->ex_secflavors));
2047 dp->dp_flag |= DP_DEFSET;
2048 /* Save the default security flavors list. */
2049 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2050 if (ep->ex_numsecflavors > 0)
2051 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2052 sizeof(ep->ex_secflavors));
2057 * Search for a dirpath on the export point.
2059 static struct dirlist *
2060 dirp_search(struct dirlist *dp, char *dirp)
2065 cmp = strcmp(dp->dp_dirp, dirp);
2067 return (dirp_search(dp->dp_left, dirp));
2069 return (dirp_search(dp->dp_right, dirp));
2077 * Scan for a host match in a directory tree.
2080 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2081 int *hostsetp, int *numsecflavors, int **secflavorsp)
2083 struct hostlist *hp;
2084 struct grouplist *grp;
2085 struct addrinfo *ai;
2088 if (dp->dp_flag & DP_DEFSET)
2089 *defsetp = dp->dp_flag;
2093 switch (grp->gr_type) {
2095 ai = grp->gr_ptr.gt_addrinfo;
2096 for (; ai; ai = ai->ai_next) {
2097 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2099 (hp->ht_flag | DP_HOSTSET);
2100 if (numsecflavors != NULL) {
2102 grp->gr_numsecflavors;
2111 if (!sacmp(saddr, (struct sockaddr *)
2112 &grp->gr_ptr.gt_net.nt_net,
2114 &grp->gr_ptr.gt_net.nt_mask)) {
2115 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2116 if (numsecflavors != NULL) {
2118 grp->gr_numsecflavors;
2133 * Scan tree for a host that matches the address.
2136 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2138 int defset, hostset;
2141 if (scan_tree(dp->dp_left, saddr))
2143 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2145 if (scan_tree(dp->dp_right, saddr))
2152 * Traverse the dirlist tree and free it up.
2155 free_dir(struct dirlist *dp)
2159 free_dir(dp->dp_left);
2160 free_dir(dp->dp_right);
2161 free_host(dp->dp_hosts);
2168 * Parse a colon separated list of security flavors
2171 parsesec(char *seclist, struct exportlist *ep)
2176 ep->ex_numsecflavors = 0;
2178 cp = strchr(seclist, ':');
2184 if (!strcmp(seclist, "sys"))
2186 else if (!strcmp(seclist, "krb5"))
2187 flavor = RPCSEC_GSS_KRB5;
2188 else if (!strcmp(seclist, "krb5i"))
2189 flavor = RPCSEC_GSS_KRB5I;
2190 else if (!strcmp(seclist, "krb5p"))
2191 flavor = RPCSEC_GSS_KRB5P;
2195 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2198 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2201 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2204 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2205 ep->ex_numsecflavors++;
2217 * Parse the option string and update fields.
2218 * Option arguments may either be -<option>=<value> or
2222 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2223 int *has_hostp, int *exflagsp, struct xucred *cr)
2225 char *cpoptarg, *cpoptend;
2226 char *cp, *endcp, *cpopt, savedc, savedc2;
2227 int allflag, usedarg;
2235 while (cpopt && *cpopt) {
2238 if ((cpoptend = strchr(cpopt, ','))) {
2240 if ((cpoptarg = strchr(cpopt, '=')))
2243 if ((cpoptarg = strchr(cpopt, '=')))
2247 nextfield(&cp, &endcp);
2249 if (endcp > cp && *cp != '-') {
2257 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2258 *exflagsp |= MNT_EXRDONLY;
2259 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2260 !(allflag = strcmp(cpopt, "mapall")) ||
2261 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2263 parsecred(cpoptarg, cr);
2265 *exflagsp |= MNT_EXPORTANON;
2266 opt_flags |= OP_MAPALL;
2268 opt_flags |= OP_MAPROOT;
2269 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2270 !strcmp(cpopt, "m"))) {
2271 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2272 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2276 opt_flags |= OP_MASK;
2277 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2278 !strcmp(cpopt, "n"))) {
2279 if (strchr(cpoptarg, '/') != NULL) {
2281 fprintf(stderr, "setting OP_MASKLEN\n");
2282 opt_flags |= OP_MASKLEN;
2284 if (grp->gr_type != GT_NULL) {
2285 syslog(LOG_ERR, "network/host conflict");
2287 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2288 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2291 grp->gr_type = GT_NET;
2294 opt_flags |= OP_NET;
2295 } else if (!strcmp(cpopt, "alldirs")) {
2296 opt_flags |= OP_ALLDIRS;
2297 } else if (!strcmp(cpopt, "public")) {
2298 *exflagsp |= MNT_EXPUBLIC;
2299 } else if (!strcmp(cpopt, "webnfs")) {
2300 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2301 opt_flags |= OP_MAPALL;
2302 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2303 ep->ex_indexfile = strdup(cpoptarg);
2304 } else if (!strcmp(cpopt, "quiet")) {
2305 opt_flags |= OP_QUIET;
2306 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2307 if (parsesec(cpoptarg, ep))
2309 opt_flags |= OP_SEC;
2312 syslog(LOG_ERR, "bad opt %s", cpopt);
2331 * Translate a character string to the corresponding list of network
2332 * addresses for a hostname.
2335 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2337 struct grouplist *checkgrp;
2338 struct addrinfo *ai, *tai, hints;
2340 char host[NI_MAXHOST];
2342 if (grp->gr_type != GT_NULL) {
2343 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2346 memset(&hints, 0, sizeof hints);
2347 hints.ai_flags = AI_CANONNAME;
2348 hints.ai_protocol = IPPROTO_UDP;
2349 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2351 syslog(LOG_ERR,"can't get address info for host %s", cp);
2354 grp->gr_ptr.gt_addrinfo = ai;
2355 while (ai != NULL) {
2356 if (ai->ai_canonname == NULL) {
2357 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2358 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2359 strlcpy(host, "?", sizeof(host));
2360 ai->ai_canonname = strdup(host);
2361 ai->ai_flags |= AI_CANONNAME;
2364 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2366 * Sanity check: make sure we don't already have an entry
2367 * for this host in the grouplist.
2369 for (checkgrp = tgrp; checkgrp != NULL;
2370 checkgrp = checkgrp->gr_next) {
2371 if (checkgrp->gr_type != GT_HOST)
2373 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2374 tai = tai->ai_next) {
2375 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2379 "ignoring duplicate host %s\n",
2381 grp->gr_type = GT_IGNORE;
2387 grp->gr_type = GT_HOST;
2392 * Free up an exports list component
2395 free_exp(struct exportlist *ep)
2398 if (ep->ex_defdir) {
2399 free_host(ep->ex_defdir->dp_hosts);
2400 free((caddr_t)ep->ex_defdir);
2404 if (ep->ex_indexfile)
2405 free(ep->ex_indexfile);
2406 free_dir(ep->ex_dirl);
2414 free_host(struct hostlist *hp)
2416 struct hostlist *hp2;
2425 static struct hostlist *
2428 struct hostlist *hp;
2430 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2431 if (hp == (struct hostlist *)NULL)
2433 hp->ht_next = (struct hostlist *)NULL;
2439 * Out of memory, fatal
2445 syslog(LOG_ERR, "out of memory");
2450 * Do the nmount() syscall with the update flag to push the export info into
2454 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2455 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
2458 struct addrinfo *ai;
2459 struct export_args *eap;
2467 struct nfsex_args nfsea;
2469 eap = &nfsea.export;
2477 bzero(eap, sizeof (struct export_args));
2478 bzero(errmsg, sizeof(errmsg));
2479 eap->ex_flags = exflags;
2480 eap->ex_anon = *anoncrp;
2481 eap->ex_indexfile = ep->ex_indexfile;
2482 if (grp->gr_type == GT_HOST)
2483 ai = grp->gr_ptr.gt_addrinfo;
2486 eap->ex_numsecflavors = ep->ex_numsecflavors;
2487 for (i = 0; i < eap->ex_numsecflavors; i++)
2488 eap->ex_secflavors[i] = ep->ex_secflavors[i];
2489 if (eap->ex_numsecflavors == 0) {
2490 eap->ex_numsecflavors = 1;
2491 eap->ex_secflavors[0] = AUTH_SYS;
2495 if (v4root_phase == 0) {
2496 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2497 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2498 build_iovec(&iov, &iovlen, "from", NULL, 0);
2499 build_iovec(&iov, &iovlen, "update", NULL, 0);
2500 build_iovec(&iov, &iovlen, "export", eap,
2501 sizeof (struct export_args));
2502 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2506 switch (grp->gr_type) {
2508 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
2510 eap->ex_addr = ai->ai_addr;
2511 eap->ex_addrlen = ai->ai_addrlen;
2512 eap->ex_masklen = 0;
2515 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
2519 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2521 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2523 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2524 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
2527 eap->ex_addr = NULL;
2528 eap->ex_addrlen = 0;
2529 eap->ex_mask = NULL;
2530 eap->ex_masklen = 0;
2537 syslog(LOG_ERR, "bad grouptype");
2545 * For V4:, use the nfssvc() syscall, instead of mount().
2547 if (v4root_phase == 2) {
2548 nfsea.fspec = v4root_dirpath;
2549 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2550 syslog(LOG_ERR, "Exporting V4: failed");
2556 * Maybe I should just use the fsb->f_mntonname path
2557 * instead of looping back up the dirp to the mount
2559 * Also, needs to know how to export all types of local
2560 * exportable filesystems and not just "ufs".
2562 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2563 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2564 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2565 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2566 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2567 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2570 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2574 cp = dirp + dirplen - 1;
2575 if (opt_flags & OP_QUIET) {
2579 if (errno == EPERM) {
2581 warnx("can't change attributes for %s: %s",
2584 "can't change attributes for %s: %s",
2589 if (opt_flags & OP_ALLDIRS) {
2590 if (errno == EINVAL)
2592 "-alldirs requested but %s is not a filesystem mountpoint",
2596 "could not remount %s: %m",
2601 /* back up over the last component */
2602 while (*cp == '/' && cp > dirp)
2604 while (*(cp - 1) != '/' && cp > dirp)
2608 warnx("mnt unsucc");
2609 syslog(LOG_ERR, "can't export %s %s",
2617 * Check that we're still on the same
2620 if (statfs(dirp, &fsb1) != 0 ||
2621 bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2622 sizeof (fsb1.f_fsid)) != 0) {
2625 "can't export %s %s", dirp,
2634 * For the experimental server:
2635 * If this is the public directory, get the file handle
2636 * and load it into the kernel via the nfssvc() syscall.
2638 if ((exflags & MNT_EXPUBLIC) != 0) {
2642 if (eap->ex_indexfile != NULL)
2643 public_name = eap->ex_indexfile;
2646 if (getfh(public_name, &fh) < 0)
2648 "Can't get public fh for %s", public_name);
2649 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2651 "Can't set public fh for %s", public_name);
2664 /* free strings allocated by strdup() in getmntopts.c */
2666 free(iov[0].iov_base); /* fstype */
2667 free(iov[2].iov_base); /* fspath */
2668 free(iov[4].iov_base); /* from */
2669 free(iov[6].iov_base); /* update */
2670 free(iov[8].iov_base); /* export */
2671 free(iov[10].iov_base); /* errmsg */
2673 /* free iov, allocated by realloc() */
2680 * Translate a net address.
2682 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2685 get_net(char *cp, struct netmsk *net, int maskflg)
2687 struct netent *np = NULL;
2688 char *name, *p, *prefp;
2689 struct sockaddr_in sin;
2690 struct sockaddr *sa = NULL;
2691 struct addrinfo hints, *ai = NULL;
2692 char netname[NI_MAXHOST];
2696 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2697 p = strchr(cp, '/');
2703 * Check for a numeric address first. We wish to avoid
2704 * possible DNS lookups in getnetbyname().
2706 if (isxdigit(*cp) || *cp == ':') {
2707 memset(&hints, 0, sizeof hints);
2708 /* Ensure the mask and the network have the same family. */
2709 if (maskflg && (opt_flags & OP_NET))
2710 hints.ai_family = net->nt_net.ss_family;
2711 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2712 hints.ai_family = net->nt_mask.ss_family;
2714 hints.ai_family = AF_UNSPEC;
2715 hints.ai_flags = AI_NUMERICHOST;
2716 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2718 if (sa != NULL && ai->ai_family == AF_INET) {
2720 * The address in `cp' is really a network address, so
2721 * use inet_network() to re-interpret this correctly.
2722 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2724 bzero(&sin, sizeof sin);
2725 sin.sin_family = AF_INET;
2726 sin.sin_len = sizeof sin;
2727 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2729 fprintf(stderr, "get_net: v4 addr %s\n",
2730 inet_ntoa(sin.sin_addr));
2731 sa = (struct sockaddr *)&sin;
2734 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2735 bzero(&sin, sizeof sin);
2736 sin.sin_family = AF_INET;
2737 sin.sin_len = sizeof sin;
2738 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2739 sa = (struct sockaddr *)&sin;
2745 /* The specified sockaddr is a mask. */
2746 if (checkmask(sa) != 0)
2748 bcopy(sa, &net->nt_mask, sa->sa_len);
2749 opt_flags |= OP_HAVEMASK;
2751 /* The specified sockaddr is a network address. */
2752 bcopy(sa, &net->nt_net, sa->sa_len);
2754 /* Get a network name for the export list. */
2757 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2758 NULL, 0, NI_NUMERICHOST) == 0) {
2763 if ((net->nt_name = strdup(name)) == NULL)
2767 * Extract a mask from either a "/<masklen>" suffix, or
2768 * from the class of an IPv4 address.
2770 if (opt_flags & OP_MASKLEN) {
2771 preflen = strtol(prefp, NULL, 10);
2772 if (preflen < 0L || preflen == LONG_MAX)
2774 bcopy(sa, &net->nt_mask, sa->sa_len);
2775 if (makemask(&net->nt_mask, (int)preflen) != 0)
2777 opt_flags |= OP_HAVEMASK;
2779 } else if (sa->sa_family == AF_INET &&
2780 (opt_flags & OP_MASK) == 0) {
2783 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2784 if (IN_CLASSA(addr))
2786 else if (IN_CLASSB(addr))
2788 else if (IN_CLASSC(addr))
2790 else if (IN_CLASSD(addr))
2793 preflen = 32; /* XXX */
2795 bcopy(sa, &net->nt_mask, sa->sa_len);
2796 makemask(&net->nt_mask, (int)preflen);
2797 opt_flags |= OP_HAVEMASK;
2812 * Parse out the next white space separated field
2815 nextfield(char **cp, char **endcp)
2820 while (*p == ' ' || *p == '\t')
2822 if (*p == '\n' || *p == '\0')
2826 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2833 * Get an exports file line. Skip over blank lines and handle line
2841 int totlen, cont_line;
2844 * Loop around ignoring blank lines and getting all continuation lines.
2849 if ((p = fgetln(exp_file, &len)) == NULL)
2854 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2864 if (linesize < len + totlen + 1) {
2865 linesize = len + totlen + 1;
2866 line = realloc(line, linesize);
2870 memcpy(line + totlen, p, len);
2872 line[totlen] = '\0';
2873 } while (totlen == 0 || cont_line);
2878 * Parse a description of a credential.
2881 parsecred(char *namelist, struct xucred *cr)
2888 gid_t groups[XU_NGROUPS + 1];
2891 cr->cr_version = XUCRED_VERSION;
2893 * Set up the unprivileged user.
2896 cr->cr_groups[0] = 65533;
2899 * Get the user's password table entry.
2901 names = strsep_quote(&namelist, " \t\n");
2902 name = strsep(&names, ":");
2903 /* Bug? name could be NULL here */
2904 if (isdigit(*name) || *name == '-')
2905 pw = getpwuid(atoi(name));
2907 pw = getpwnam(name);
2909 * Credentials specified as those of a user.
2911 if (names == NULL) {
2913 syslog(LOG_ERR, "unknown user: %s", name);
2916 cr->cr_uid = pw->pw_uid;
2917 ngroups = XU_NGROUPS + 1;
2918 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) {
2919 syslog(LOG_ERR, "too many groups");
2920 ngroups = XU_NGROUPS + 1;
2924 * Compress out duplicate.
2926 cr->cr_ngroups = ngroups - 1;
2927 cr->cr_groups[0] = groups[0];
2928 for (cnt = 2; cnt < ngroups; cnt++)
2929 cr->cr_groups[cnt - 1] = groups[cnt];
2933 * Explicit credential specified as a colon separated list:
2937 cr->cr_uid = pw->pw_uid;
2938 else if (isdigit(*name) || *name == '-')
2939 cr->cr_uid = atoi(name);
2941 syslog(LOG_ERR, "unknown user: %s", name);
2945 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
2946 name = strsep(&names, ":");
2947 if (isdigit(*name) || *name == '-') {
2948 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2950 if ((gr = getgrnam(name)) == NULL) {
2951 syslog(LOG_ERR, "unknown group: %s", name);
2954 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2957 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
2958 syslog(LOG_ERR, "too many groups");
2961 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
2963 * Routines that maintain the remote mounttab
2968 struct mountlist *mlp;
2969 char *host, *dirp, *cp;
2973 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2974 if (errno == ENOENT)
2977 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2981 while (fgets(str, STRSIZ, mlfile) != NULL) {
2983 host = strsep(&cp, " \t\n");
2984 dirp = strsep(&cp, " \t\n");
2985 if (host == NULL || dirp == NULL)
2987 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2988 if (mlp == (struct mountlist *)NULL)
2990 strncpy(mlp->ml_host, host, MNTNAMLEN);
2991 mlp->ml_host[MNTNAMLEN] = '\0';
2992 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2993 mlp->ml_dirp[MNTPATHLEN] = '\0';
2995 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3001 del_mlist(char *hostp, char *dirp)
3003 struct mountlist *mlp, *mlp2;
3007 SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
3008 if (!strcmp(mlp->ml_host, hostp) &&
3009 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3011 SLIST_REMOVE(&mlhead, mlp, mountlist, next);
3016 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3017 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3020 SLIST_FOREACH(mlp, &mlhead, next) {
3021 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3028 add_mlist(char *hostp, char *dirp)
3030 struct mountlist *mlp;
3033 SLIST_FOREACH(mlp, &mlhead, next) {
3034 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3038 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3039 if (mlp == (struct mountlist *)NULL)
3041 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3042 mlp->ml_host[MNTNAMLEN] = '\0';
3043 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3044 mlp->ml_dirp[MNTPATHLEN] = '\0';
3045 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3046 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3047 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3050 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3055 * Free up a group list.
3058 free_grp(struct grouplist *grp)
3060 if (grp->gr_type == GT_HOST) {
3061 if (grp->gr_ptr.gt_addrinfo != NULL)
3062 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3063 } else if (grp->gr_type == GT_NET) {
3064 if (grp->gr_ptr.gt_net.nt_name)
3065 free(grp->gr_ptr.gt_net.nt_name);
3072 SYSLOG(int pri, const char *fmt, ...)
3077 vfprintf(stderr, fmt, ap);
3083 * Check options for consistency.
3086 check_options(struct dirlist *dp)
3089 if (v4root_phase == 0 && dp == NULL)
3091 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3092 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3095 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3096 syslog(LOG_ERR, "-mask requires -network");
3099 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3100 syslog(LOG_ERR, "-network requires mask specification");
3103 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3104 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3107 if (v4root_phase > 0 &&
3109 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3110 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3113 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3114 syslog(LOG_ERR, "-alldirs has multiple directories");
3121 * Check an absolute directory path for any symbolic links. Return true
3124 check_dirpath(char *dirp)
3131 while (*cp && ret) {
3134 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3140 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3146 * Make a netmask according to the specified prefix length. The ss_family
3147 * and other non-address fields must be initialised before calling this.
3150 makemask(struct sockaddr_storage *ssp, int bitlen)
3155 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3157 if (bitlen > len * CHAR_BIT)
3160 for (i = 0; i < len; i++) {
3161 bits = MIN(CHAR_BIT, bitlen);
3162 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3169 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3170 * is acceptable (i.e. of the form 1...10....0).
3173 checkmask(struct sockaddr *sa)
3178 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3181 for (i = 0; i < len; i++)
3182 if (mask[i] != 0xff)
3185 if (~mask[i] & (u_char)(~mask[i] + 1))
3189 for (; i < len; i++)
3196 * Compare two sockaddrs according to a specified mask. Return zero if
3197 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3198 * If samask is NULL, perform a full comparison.
3201 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3203 unsigned char *p1, *p2, *mask;
3206 if (sa1->sa_family != sa2->sa_family ||
3207 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3208 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3211 switch (sa1->sa_family) {
3213 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3214 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3219 /* Simple binary comparison if no mask specified. */
3221 return (memcmp(p1, p2, len));
3223 /* Set up the mask, and do a mask-based comparison. */
3224 if (sa1->sa_family != samask->sa_family ||
3225 (mask = sa_rawaddr(samask, NULL)) == NULL)
3228 for (i = 0; i < len; i++)
3229 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3235 * Return a pointer to the part of the sockaddr that contains the
3236 * raw address, and set *nbytes to its length in bytes. Returns
3237 * NULL if the address family is unknown.
3240 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3244 switch (sa->sa_family) {
3246 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3247 p = &((struct sockaddr_in *)sa)->sin_addr;
3250 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3251 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3264 huphandler(int sig __unused)
3271 terminate(int sig __unused)
3273 pidfile_remove(pfh);
3274 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3275 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);