2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Herb Hasler and Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
51 #include <sys/fcntl.h>
52 #include <sys/fnv_hash.h>
53 #include <sys/linker.h>
54 #include <sys/module.h>
55 #include <sys/mount.h>
56 #include <sys/queue.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
62 #include <rpc/rpc_com.h>
63 #include <rpc/pmap_clnt.h>
64 #include <rpc/pmap_prot.h>
65 #include <rpcsvc/mount.h>
66 #include <nfs/nfsproto.h>
67 #include <nfs/nfssvc.h>
68 #include <nfsserver/nfs.h>
70 #include <fs/nfs/nfsport.h>
72 #include <arpa/inet.h>
87 #include "pathnames.h"
95 * Structures for keeping the mount list and export list
98 char ml_host[MNTNAMLEN+1];
99 char ml_dirp[MNTPATHLEN+1];
101 SLIST_ENTRY(mountlist) next;
105 struct dirlist *dp_left;
106 struct dirlist *dp_right;
108 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
112 #define DP_DEFSET 0x1
113 #define DP_HOSTSET 0x2
116 struct dirlist *ex_dirl;
117 struct dirlist *ex_defdir;
118 struct grouplist *ex_grphead;
123 int ex_numsecflavors;
124 int ex_secflavors[MAXSECFLAVORS];
125 int ex_defnumsecflavors;
126 int ex_defsecflavors[MAXSECFLAVORS];
128 SLIST_ENTRY(exportlist) entries;
131 #define EX_LINKED 0x1
133 SLIST_HEAD(exportlisthead, exportlist);
136 struct sockaddr_storage nt_net;
137 struct sockaddr_storage nt_mask;
142 struct addrinfo *gt_addrinfo;
143 struct netmsk gt_net;
148 union grouptypes gr_ptr;
149 struct grouplist *gr_next;
150 int gr_numsecflavors;
151 int gr_secflavors[MAXSECFLAVORS];
157 #define GT_DEFAULT 0x3
158 #define GT_IGNORE 0x5
161 int ht_flag; /* Uses DP_xx bits */
162 struct grouplist *ht_grp;
163 struct hostlist *ht_next;
170 int fhr_numsecflavors;
174 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
177 static char *add_expdir(struct dirlist **, char *, int);
178 static void add_dlist(struct dirlist **, struct dirlist *,
179 struct grouplist *, int, struct exportlist *);
180 static void add_mlist(char *, char *);
181 static int check_dirpath(char *);
182 static int check_options(struct dirlist *);
183 static int checkmask(struct sockaddr *sa);
184 static int chk_host(struct dirlist *, struct sockaddr *, int *, int *,
186 static char *strsep_quote(char **stringp, const char *delim);
187 static int create_service(struct netconfig *nconf);
188 static void complete_service(struct netconfig *nconf, char *port_str);
189 static void clearout_service(void);
190 static void del_mlist(char *hostp, char *dirp);
191 static struct dirlist *dirp_search(struct dirlist *, char *);
192 static int do_mount(struct exportlist *, struct grouplist *, int,
193 struct xucred *, char *, int, struct statfs *);
194 static int do_opt(char **, char **, struct exportlist *,
195 struct grouplist *, int *, int *, struct xucred *);
196 static struct exportlist *ex_search(fsid_t *, struct exportlisthead *);
197 static struct exportlist *get_exp(void);
198 static void free_dir(struct dirlist *);
199 static void free_exp(struct exportlist *);
200 static void free_grp(struct grouplist *);
201 static void free_host(struct hostlist *);
202 static void get_exportlist(void);
203 static void insert_exports(struct exportlist *, struct exportlisthead *);
204 static void free_exports(struct exportlisthead *);
205 static void read_exportfile(void);
206 static void delete_export(struct iovec *, int, struct statfs *, char *);
207 static int get_host(char *, struct grouplist *, struct grouplist *);
208 static struct hostlist *get_ht(void);
209 static int get_line(void);
210 static void get_mountlist(void);
211 static int get_net(char *, struct netmsk *, int);
212 static void getexp_err(struct exportlist *, struct grouplist *, const char *);
213 static struct grouplist *get_grp(void);
214 static void hang_dirp(struct dirlist *, struct grouplist *,
215 struct exportlist *, int);
216 static void huphandler(int sig);
217 static int makemask(struct sockaddr_storage *ssp, int bitlen);
218 static void mntsrv(struct svc_req *, SVCXPRT *);
219 static void nextfield(char **, char **);
220 static void out_of_mem(void);
221 static void parsecred(char *, struct xucred *);
222 static int parsesec(char *, struct exportlist *);
223 static int put_exlist(struct dirlist *, XDR *, struct dirlist *,
225 static void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
226 static int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
227 struct sockaddr *samask);
228 static int scan_tree(struct dirlist *, struct sockaddr *);
229 static void usage(void);
230 static int xdr_dir(XDR *, char *);
231 static int xdr_explist(XDR *, caddr_t);
232 static int xdr_explist_brief(XDR *, caddr_t);
233 static int xdr_explist_common(XDR *, caddr_t, int);
234 static int xdr_fhs(XDR *, caddr_t);
235 static int xdr_mlist(XDR *, caddr_t);
236 static void terminate(int);
238 #define EXPHASH(f) (fnv_32_buf((f), sizeof(fsid_t), 0) % exphashsize)
239 static struct exportlisthead *exphead = NULL;
240 static int exphashsize = 0;
241 static SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(&mlhead);
242 static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
243 static char **exnames;
244 static char **hosts = NULL;
245 static struct xucred def_anon = {
252 static int force_v2 = 0;
253 static int resvport_only = 1;
254 static int nhosts = 0;
255 static int dir_only = 1;
256 static int dolog = 0;
257 static int got_sighup = 0;
258 static int xcreated = 0;
260 static char *svcport_str = NULL;
261 static int mallocd_svcport = 0;
263 static int sock_fdcnt;
264 static int sock_fdpos;
265 static int suspend_nfsd = 0;
267 static int opt_flags;
268 static int have_v6 = 1;
270 static int v4root_phase = 0;
271 static char v4root_dirpath[PATH_MAX + 1];
272 static int has_publicfh = 0;
274 static struct pidfh *pfh = NULL;
275 /* Bits for opt_flags above */
276 #define OP_MAPROOT 0x01
277 #define OP_MAPALL 0x02
281 #define OP_ALLDIRS 0x40
282 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
283 #define OP_QUIET 0x100
284 #define OP_MASKLEN 0x200
288 static int debug = 1;
289 static void SYSLOG(int, const char *, ...) __printflike(2, 3);
290 #define syslog SYSLOG
292 static int debug = 0;
296 * Similar to strsep(), but it allows for quoted strings
297 * and escaped characters.
299 * It returns the string (or NULL, if *stringp is NULL),
300 * which is a de-quoted version of the string if necessary.
302 * It modifies *stringp in place.
305 strsep_quote(char **stringp, const char *delim)
307 char *srcptr, *dstptr, *retval;
310 if (stringp == NULL || *stringp == NULL)
313 srcptr = dstptr = retval = *stringp;
317 * We're looking for several edge cases here.
318 * First: if we're in quote state (quot != 0),
319 * then we ignore the delim characters, but otherwise
320 * process as normal, unless it is the quote character.
321 * Second: if the current character is a backslash,
322 * we take the next character as-is, without checking
323 * for delim, quote, or backslash. Exception: if the
324 * next character is a NUL, that's the end of the string.
325 * Third: if the character is a quote character, we toggle
327 * Otherwise: check the current character for NUL, or
328 * being in delim, and end the string if either is true.
330 if (*srcptr == '\\') {
333 * The edge case here is if the next character
334 * is NUL, we want to stop processing. But if
335 * it's not NUL, then we simply want to copy it.
338 *dstptr++ = *srcptr++;
342 if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
346 if (quot && *srcptr == quot) {
347 /* End of the quoted part */
352 if (!quot && strchr(delim, *srcptr))
354 *dstptr++ = *srcptr++;
357 *dstptr = 0; /* Terminate the string */
358 *stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
363 * Mountd server for NFS mount protocol as described in:
364 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
365 * The optional arguments are the exports file name
366 * default: _PATH_EXPORTS
367 * and "-n" to allow nonroot mount.
370 main(int argc, char **argv)
373 struct netconfig *nconf;
374 char *endptr, **hosts_bak;
379 int maxrec = RPC_MAXDATASIZE;
380 int attempt_cnt, port_len, port_pos, ret;
383 /* Check that another mountd isn't already running. */
384 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
387 errx(1, "mountd already running, pid: %d.", otherpid);
388 warn("cannot open or create pidfile");
391 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
397 while ((c = getopt(argc, argv, "2deh:lnp:rS")) != -1)
403 /* now a no-op, since this is the default */
412 debug = debug ? 0 : 1;
419 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
420 if (endptr == NULL || *endptr != '\0' ||
421 svcport == 0 || svcport >= IPPORT_MAX)
423 svcport_str = strdup(optarg);
428 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
429 if (hosts_bak == NULL) {
431 for (k = 0; k < nhosts; k++)
438 hosts[nhosts - 1] = strdup(optarg);
439 if (hosts[nhosts - 1] == NULL) {
440 for (k = 0; k < (nhosts - 1); k++)
453 if (modfind("nfsd") < 0) {
454 /* Not present in kernel, try loading it */
455 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
456 errx(1, "NFS server is not available");
464 exnames = exnames_default;
465 openlog("mountd", LOG_PID, LOG_DAEMON);
467 warnx("getting export list");
470 warnx("getting mount list");
476 signal(SIGINT, SIG_IGN);
477 signal(SIGQUIT, SIG_IGN);
479 signal(SIGHUP, huphandler);
480 signal(SIGTERM, terminate);
481 signal(SIGPIPE, SIG_IGN);
485 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
486 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
487 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
489 if (!resvport_only) {
490 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
491 &resvport_only, sizeof(resvport_only)) != 0 &&
493 syslog(LOG_ERR, "sysctl: %m");
499 * If no hosts were specified, add a wildcard entry to bind to
500 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
504 hosts = malloc(sizeof(char *));
512 hosts_bak = realloc(hosts, (nhosts + 2) *
514 if (hosts_bak == NULL) {
515 for (k = 0; k < nhosts; k++)
522 hosts[nhosts - 2] = "::1";
524 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
525 if (hosts_bak == NULL) {
526 for (k = 0; k < nhosts; k++)
536 hosts[nhosts - 1] = "127.0.0.1";
544 nc_handle = setnetconfig();
545 while ((nconf = getnetconfig(nc_handle))) {
546 if (nconf->nc_flag & NC_VISIBLE) {
547 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
551 ret = create_service(nconf);
553 /* Ignore this call */
557 * Failed to bind port, so close off
558 * all sockets created and try again
559 * if the port# was dynamically
560 * assigned via bind(2).
563 if (mallocd_svcport != 0 &&
564 attempt_cnt < GETPORT_MAXTRY) {
571 "bindresvport_sa: %m");
575 /* Start over at the first service. */
579 nc_handle = setnetconfig();
581 } else if (mallocd_svcport != 0 &&
582 attempt_cnt == GETPORT_MAXTRY) {
584 * For the last attempt, allow
585 * different port #s for each nconf
586 * by saving the svcport_str and
587 * setting it back to NULL.
589 port_list = realloc(port_list,
590 (port_len + 1) * sizeof(char *));
591 if (port_list == NULL)
593 port_list[port_len++] = svcport_str;
602 * Successfully bound the ports, so call complete_service() to
603 * do the rest of the setup on the service(s).
607 nc_handle = setnetconfig();
608 while ((nconf = getnetconfig(nc_handle))) {
609 if (nconf->nc_flag & NC_VISIBLE) {
610 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
613 } else if (port_list != NULL) {
614 if (port_pos >= port_len) {
615 syslog(LOG_ERR, "too many port#s");
618 complete_service(nconf, port_list[port_pos++]);
620 complete_service(nconf, svcport_str);
623 endnetconfig(nc_handle);
625 if (port_list != NULL) {
626 for (port_pos = 0; port_pos < port_len; port_pos++)
627 free(port_list[port_pos]);
632 syslog(LOG_ERR, "could not create any services");
636 /* Expand svc_run() here so that we can call get_exportlist(). */
643 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
647 syslog(LOG_ERR, "mountd died: select: %m");
652 svc_getreqset(&readfds);
658 * This routine creates and binds sockets on the appropriate
659 * addresses. It gets called one time for each transport.
660 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
661 * bind failed with EADDRINUSE.
662 * Any file descriptors that have been created are stored in sock_fd and
663 * the total count of them is maintained in sock_fdcnt.
666 create_service(struct netconfig *nconf)
668 struct addrinfo hints, *res = NULL;
669 struct sockaddr_in *sin;
670 struct sockaddr_in6 *sin6;
671 struct __rpc_sockinfo si;
677 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
680 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
681 (nconf->nc_semantics != NC_TPI_COTS) &&
682 (nconf->nc_semantics != NC_TPI_COTS_ORD))
683 return (1); /* not my type */
686 * XXX - using RPC library internal functions.
688 if (!__rpc_nconf2sockinfo(nconf, &si)) {
689 syslog(LOG_ERR, "cannot get information for %s",
694 /* Get mountd's address on this transport */
695 memset(&hints, 0, sizeof hints);
696 hints.ai_family = si.si_af;
697 hints.ai_socktype = si.si_socktype;
698 hints.ai_protocol = si.si_proto;
701 * Bind to specific IPs if asked to
704 while (nhostsbak > 0) {
706 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
709 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
712 hints.ai_flags = AI_PASSIVE;
715 * XXX - using RPC library internal functions.
717 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
719 if (errno == EAFNOSUPPORT &&
720 nconf->nc_semantics != NC_TPI_CLTS)
723 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
724 "cannot create socket for %s", nconf->nc_netid);
730 switch (hints.ai_family) {
732 if (inet_pton(AF_INET, hosts[nhostsbak],
734 hints.ai_flags |= AI_NUMERICHOST;
737 * Skip if we have an AF_INET6 address.
739 if (inet_pton(AF_INET6, hosts[nhostsbak],
747 if (inet_pton(AF_INET6, hosts[nhostsbak],
749 hints.ai_flags |= AI_NUMERICHOST;
752 * Skip if we have an AF_INET address.
754 if (inet_pton(AF_INET, hosts[nhostsbak],
762 * We're doing host-based access checks here, so don't
763 * allow v4-in-v6 to confuse things. The kernel will
764 * disable it by default on NFS sockets too.
766 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
769 "can't disable v4-in-v6 on IPv6 socket");
778 * If no hosts were specified, just bind to INADDR_ANY
780 if (strcmp("*", hosts[nhostsbak]) == 0) {
781 if (svcport_str == NULL) {
782 res = malloc(sizeof(struct addrinfo));
786 res->ai_flags = hints.ai_flags;
787 res->ai_family = hints.ai_family;
788 res->ai_protocol = hints.ai_protocol;
789 switch (res->ai_family) {
791 sin = malloc(sizeof(struct sockaddr_in));
794 sin->sin_family = AF_INET;
795 sin->sin_port = htons(0);
796 sin->sin_addr.s_addr = htonl(INADDR_ANY);
797 res->ai_addr = (struct sockaddr*) sin;
798 res->ai_addrlen = (socklen_t)
799 sizeof(struct sockaddr_in);
802 sin6 = malloc(sizeof(struct sockaddr_in6));
805 sin6->sin6_family = AF_INET6;
806 sin6->sin6_port = htons(0);
807 sin6->sin6_addr = in6addr_any;
808 res->ai_addr = (struct sockaddr*) sin6;
809 res->ai_addrlen = (socklen_t)
810 sizeof(struct sockaddr_in6);
813 syslog(LOG_ERR, "bad addr fam %d",
818 if ((aicode = getaddrinfo(NULL, svcport_str,
819 &hints, &res)) != 0) {
821 "cannot get local address for %s: %s",
823 gai_strerror(aicode));
829 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
830 &hints, &res)) != 0) {
832 "cannot get local address for %s: %s",
833 nconf->nc_netid, gai_strerror(aicode));
840 sock_fd[sock_fdcnt - 1] = fd;
842 /* Now, attempt the bind. */
843 r = bindresvport_sa(fd, res->ai_addr);
845 if (errno == EADDRINUSE && mallocd_svcport != 0) {
846 if (mallocd_res != 0) {
853 syslog(LOG_ERR, "bindresvport_sa: %m");
857 if (svcport_str == NULL) {
858 svcport_str = malloc(NI_MAXSERV * sizeof(char));
859 if (svcport_str == NULL)
863 if (getnameinfo(res->ai_addr,
864 res->ai_addr->sa_len, NULL, NI_MAXHOST,
865 svcport_str, NI_MAXSERV * sizeof(char),
866 NI_NUMERICHOST | NI_NUMERICSERV))
867 errx(1, "Cannot get port number");
869 if (mallocd_res != 0) {
880 * Called after all the create_service() calls have succeeded, to complete
881 * the setup and registration.
884 complete_service(struct netconfig *nconf, char *port_str)
886 struct addrinfo hints, *res = NULL;
887 struct __rpc_sockinfo si;
888 struct netbuf servaddr;
889 SVCXPRT *transp = NULL;
890 int aicode, fd, nhostsbak;
893 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
894 (nconf->nc_semantics != NC_TPI_COTS) &&
895 (nconf->nc_semantics != NC_TPI_COTS_ORD))
896 return; /* not my type */
899 * XXX - using RPC library internal functions.
901 if (!__rpc_nconf2sockinfo(nconf, &si)) {
902 syslog(LOG_ERR, "cannot get information for %s",
908 while (nhostsbak > 0) {
910 if (sock_fdpos >= sock_fdcnt) {
911 /* Should never happen. */
912 syslog(LOG_ERR, "Ran out of socket fd's");
915 fd = sock_fd[sock_fdpos++];
920 * Using -1 tells listen(2) to use
921 * kern.ipc.soacceptqueue for the backlog.
923 if (nconf->nc_semantics != NC_TPI_CLTS)
926 if (nconf->nc_semantics == NC_TPI_CLTS )
927 transp = svc_dg_create(fd, 0, 0);
929 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
932 if (transp != (SVCXPRT *) NULL) {
933 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
936 "can't register %s MOUNTVERS service",
939 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
942 "can't register %s MOUNTVERS3 service",
946 syslog(LOG_WARNING, "can't create %s services",
949 if (registered == 0) {
951 memset(&hints, 0, sizeof hints);
952 hints.ai_flags = AI_PASSIVE;
953 hints.ai_family = si.si_af;
954 hints.ai_socktype = si.si_socktype;
955 hints.ai_protocol = si.si_proto;
957 if ((aicode = getaddrinfo(NULL, port_str, &hints,
959 syslog(LOG_ERR, "cannot get local address: %s",
960 gai_strerror(aicode));
964 servaddr.buf = malloc(res->ai_addrlen);
965 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
966 servaddr.len = res->ai_addrlen;
968 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
969 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
978 * Clear out sockets after a failure to bind one of them, so that the
979 * cycle of socket creation/binding can start anew.
982 clearout_service(void)
986 for (i = 0; i < sock_fdcnt; i++) {
987 if (sock_fd[i] >= 0) {
988 shutdown(sock_fd[i], SHUT_RDWR);
998 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
999 "[-S] [-h <bindip>] [export_file ...]\n");
1004 * The mount rpc service
1007 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
1009 struct exportlist *ep;
1011 struct fhreturn fhr;
1014 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1015 int lookup_failed = 1;
1016 struct sockaddr *saddr;
1018 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1019 int bad = 0, defset, hostset;
1020 sigset_t sighup_mask;
1021 int numsecflavors, *secflavorsp;
1023 sigemptyset(&sighup_mask);
1024 sigaddset(&sighup_mask, SIGHUP);
1025 saddr = svc_getrpccaller(transp)->buf;
1026 switch (saddr->sa_family) {
1028 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1031 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1034 syslog(LOG_ERR, "request from unknown address family");
1037 switch (rqstp->rq_proc) {
1039 case MOUNTPROC_UMNT:
1040 case MOUNTPROC_UMNTALL:
1041 lookup_failed = getnameinfo(saddr, saddr->sa_len, host,
1042 sizeof host, NULL, 0, 0);
1044 getnameinfo(saddr, saddr->sa_len, numerichost,
1045 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1046 switch (rqstp->rq_proc) {
1048 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1049 syslog(LOG_ERR, "can't send reply");
1052 if (sport >= IPPORT_RESERVED && resvport_only) {
1054 "mount request from %s from unprivileged port",
1056 svcerr_weakauth(transp);
1059 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1060 syslog(LOG_NOTICE, "undecodable mount request from %s",
1062 svcerr_decode(transp);
1067 * Get the real pathname and make sure it is a directory
1068 * or a regular file if the -r option was specified
1071 if (realpath(rpcpath, dirpath) == NULL ||
1072 stat(dirpath, &stb) < 0 ||
1073 statfs(dirpath, &fsb) < 0) {
1074 chdir("/"); /* Just in case realpath doesn't */
1076 "mount request from %s for non existent path %s",
1077 numerichost, dirpath);
1079 warnx("stat failed on %s", dirpath);
1080 bad = ENOENT; /* We will send error reply later */
1083 !S_ISDIR(stb.st_mode) &&
1084 (dir_only || !S_ISREG(stb.st_mode))) {
1086 "mount request from %s for non-directory path %s",
1087 numerichost, dirpath);
1089 warnx("mounting non-directory %s", dirpath);
1090 bad = ENOTDIR; /* We will send error reply later */
1093 /* Check in the exports list */
1094 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1098 ep = ex_search(&fsb.f_fsid, exphead);
1099 hostset = defset = 0;
1100 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1101 &numsecflavors, &secflavorsp) ||
1102 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1103 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1105 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1106 scan_tree(ep->ex_dirl, saddr) == 0))) {
1108 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1110 syslog(LOG_ERR, "can't send reply");
1111 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1114 if (hostset & DP_HOSTSET) {
1115 fhr.fhr_flag = hostset;
1116 fhr.fhr_numsecflavors = numsecflavors;
1117 fhr.fhr_secflavors = secflavorsp;
1119 fhr.fhr_flag = defset;
1120 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1121 fhr.fhr_secflavors = ep->ex_defsecflavors;
1123 fhr.fhr_vers = rqstp->rq_vers;
1124 /* Get the file handle */
1125 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1126 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1128 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1129 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1131 syslog(LOG_ERR, "can't send reply");
1132 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1135 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1137 syslog(LOG_ERR, "can't send reply");
1139 add_mlist(host, dirpath);
1141 add_mlist(numerichost, dirpath);
1143 warnx("mount successful");
1146 "mount request succeeded from %s for %s",
1147 numerichost, dirpath);
1152 "mount request denied from %s for %s",
1153 numerichost, dirpath);
1156 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1158 syslog(LOG_ERR, "can't send reply");
1159 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1161 case MOUNTPROC_DUMP:
1162 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1163 syslog(LOG_ERR, "can't send reply");
1166 "dump request succeeded from %s",
1169 case MOUNTPROC_UMNT:
1170 if (sport >= IPPORT_RESERVED && resvport_only) {
1172 "umount request from %s from unprivileged port",
1174 svcerr_weakauth(transp);
1177 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1178 syslog(LOG_NOTICE, "undecodable umount request from %s",
1180 svcerr_decode(transp);
1183 if (realpath(rpcpath, dirpath) == NULL) {
1184 syslog(LOG_NOTICE, "umount request from %s "
1185 "for non existent path %s",
1186 numerichost, dirpath);
1188 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1189 syslog(LOG_ERR, "can't send reply");
1191 del_mlist(host, dirpath);
1192 del_mlist(numerichost, dirpath);
1195 "umount request succeeded from %s for %s",
1196 numerichost, dirpath);
1198 case MOUNTPROC_UMNTALL:
1199 if (sport >= IPPORT_RESERVED && resvport_only) {
1201 "umountall request from %s from unprivileged port",
1203 svcerr_weakauth(transp);
1206 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1207 syslog(LOG_ERR, "can't send reply");
1209 del_mlist(host, NULL);
1210 del_mlist(numerichost, NULL);
1213 "umountall request succeeded from %s",
1216 case MOUNTPROC_EXPORT:
1217 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1218 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1220 syslog(LOG_ERR, "can't send reply");
1223 "export request succeeded from %s",
1227 svcerr_noproc(transp);
1233 * Xdr conversion for a dirpath string
1236 xdr_dir(XDR *xdrsp, char *dirp)
1238 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1242 * Xdr routine to generate file handle reply
1245 xdr_fhs(XDR *xdrsp, caddr_t cp)
1247 struct fhreturn *fhrp = (struct fhreturn *)cp;
1248 u_long ok = 0, len, auth;
1251 if (!xdr_long(xdrsp, &ok))
1253 switch (fhrp->fhr_vers) {
1255 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1258 if (!xdr_long(xdrsp, &len))
1260 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1262 if (fhrp->fhr_numsecflavors) {
1263 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1265 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1266 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1272 if (!xdr_long(xdrsp, &len))
1274 return (xdr_long(xdrsp, &auth));
1281 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1283 struct mountlist *mlp;
1288 SLIST_FOREACH(mlp, &mlhead, next) {
1289 if (!xdr_bool(xdrsp, &true))
1291 strp = &mlp->ml_host[0];
1292 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1294 strp = &mlp->ml_dirp[0];
1295 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1298 if (!xdr_bool(xdrsp, &false))
1304 * Xdr conversion for export list
1307 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1309 struct exportlist *ep;
1312 sigset_t sighup_mask;
1315 sigemptyset(&sighup_mask);
1316 sigaddset(&sighup_mask, SIGHUP);
1317 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1319 for (i = 0; i < exphashsize; i++)
1320 SLIST_FOREACH(ep, &exphead[i], entries) {
1322 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1325 if (ep->ex_defdir && putdef == 0 &&
1326 put_exlist(ep->ex_defdir, xdrsp, NULL,
1330 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1331 if (!xdr_bool(xdrsp, &false))
1335 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1340 * Called from xdr_explist() to traverse the tree and export the
1344 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1347 struct grouplist *grp;
1348 struct hostlist *hp;
1355 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1357 if (!xdr_bool(xdrsp, &true))
1360 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1362 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1367 if (!xdr_bool(xdrsp, &true))
1370 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1372 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1373 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1377 if (grp->gr_type == GT_HOST) {
1378 if (!xdr_bool(xdrsp, &true))
1380 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1381 if (!xdr_string(xdrsp, &strp,
1384 } else if (grp->gr_type == GT_NET) {
1385 if (!xdr_bool(xdrsp, &true))
1387 strp = grp->gr_ptr.gt_net.nt_name;
1388 if (!xdr_string(xdrsp, &strp,
1393 if (gotalldir && hp == (struct hostlist *)NULL) {
1399 if (!xdr_bool(xdrsp, &false))
1401 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1408 xdr_explist(XDR *xdrsp, caddr_t cp)
1411 return xdr_explist_common(xdrsp, cp, 0);
1415 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1418 return xdr_explist_common(xdrsp, cp, 1);
1422 static size_t linesize;
1423 static FILE *exp_file;
1426 * Get the export list from one, currently open file
1429 get_exportlist_one(void)
1431 struct exportlist *ep;
1432 struct grouplist *grp, *tgrp;
1433 struct dirlist *dirhead;
1436 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1437 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1440 dirhead = (struct dirlist *)NULL;
1441 while (get_line()) {
1443 warnx("got line %s", line);
1445 nextfield(&cp, &endcp);
1454 exflags = MNT_EXPORTED;
1457 ep = (struct exportlist *)NULL;
1461 * Handle the V4 root dir.
1463 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1465 * V4: just indicates that it is the v4 root point,
1466 * so skip over that and set v4root_phase.
1468 if (v4root_phase > 0) {
1469 syslog(LOG_ERR, "V4:duplicate line, ignored");
1474 nextfield(&cp, &endcp);
1478 * Create new exports list entry
1481 tgrp = grp = get_grp();
1483 if (len > MNTNAMLEN) {
1484 getexp_err(ep, tgrp, "mountpoint too long");
1488 if (ep == (struct exportlist *)NULL) {
1489 getexp_err(ep, tgrp,
1490 "flag before export path definition");
1494 warnx("doing opt %s", cp);
1496 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1498 getexp_err(ep, tgrp, NULL);
1501 } else if (*cp == '/') {
1504 if (v4root_phase > 1) {
1506 getexp_err(ep, tgrp, "Multiple V4 dirs");
1510 if (check_dirpath(cp) &&
1511 statfs(cp, &fsb) >= 0) {
1512 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1513 syslog(LOG_ERR, "Warning: exporting of "
1514 "automounted fs %s not supported", cp);
1516 getexp_err(ep, tgrp, "dirs must be first");
1519 if (v4root_phase == 1) {
1521 getexp_err(ep, tgrp, "Multiple V4 dirs");
1524 if (strlen(v4root_dirpath) == 0) {
1525 strlcpy(v4root_dirpath, cp,
1526 sizeof (v4root_dirpath));
1527 } else if (strcmp(v4root_dirpath, cp)
1530 "different V4 dirpath %s", cp);
1531 getexp_err(ep, tgrp, NULL);
1540 if (ep->ex_fs.val[0] !=
1541 fsb.f_fsid.val[0] ||
1543 fsb.f_fsid.val[1]) {
1544 getexp_err(ep, tgrp,
1550 * See if this directory is already
1553 ep = ex_search(&fsb.f_fsid, exphead);
1554 if (ep == (struct exportlist *)NULL) {
1556 ep->ex_fs = fsb.f_fsid;
1557 ep->ex_fsdir = strdup(fsb.f_mntonname);
1558 if (ep->ex_fsdir == NULL)
1562 "making new ep fs=0x%x,0x%x",
1566 warnx("found ep fs=0x%x,0x%x",
1572 * Add dirpath to export mount point.
1574 dirp = add_expdir(&dirhead, cp, len);
1578 getexp_err(ep, tgrp,
1579 "symbolic link in export path or statfs failed");
1587 if (ep == (struct exportlist *)NULL) {
1588 getexp_err(ep, tgrp,
1589 "host(s) before export path definition");
1594 * Get the host or netgroup.
1597 netgrp = getnetgrent(&hst, &usr, &dom);
1600 grp->gr_next = get_grp();
1606 "null hostname in netgroup %s, skipping", cp);
1607 grp->gr_type = GT_IGNORE;
1608 } else if (get_host(hst, grp, tgrp)) {
1610 "bad host %s in netgroup %s, skipping", hst, cp);
1611 grp->gr_type = GT_IGNORE;
1613 } else if (get_host(cp, grp, tgrp)) {
1614 syslog(LOG_ERR, "bad host %s, skipping", cp);
1615 grp->gr_type = GT_IGNORE;
1618 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1623 nextfield(&cp, &endcp);
1626 if (check_options(dirhead)) {
1627 getexp_err(ep, tgrp, NULL);
1631 grp->gr_type = GT_DEFAULT;
1633 warnx("adding a default entry");
1636 * Don't allow a network export coincide with a list of
1637 * host(s) on the same line.
1639 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1640 getexp_err(ep, tgrp, "network/host conflict");
1644 * If an export list was specified on this line, make sure
1645 * that we have at least one valid entry, otherwise skip it.
1649 while (grp && grp->gr_type == GT_IGNORE)
1652 getexp_err(ep, tgrp, "no valid entries");
1657 if (v4root_phase == 1) {
1658 getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1663 * Loop through hosts, pushing the exports into the kernel.
1664 * After loop, tgrp points to the start of the list and
1665 * grp points to the last entry in the list.
1669 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1671 getexp_err(ep, tgrp, NULL);
1674 } while (grp->gr_next && (grp = grp->gr_next));
1677 * For V4: don't enter in mount lists.
1679 if (v4root_phase > 0 && v4root_phase <= 2) {
1681 * Since these structures aren't used by mountd,
1686 while (tgrp != NULL) {
1688 tgrp = tgrp->gr_next;
1695 * Success. Update the data structures.
1698 hang_dirp(dirhead, tgrp, ep, opt_flags);
1699 grp->gr_next = ep->ex_grphead;
1700 ep->ex_grphead = tgrp;
1702 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1706 dirhead = (struct dirlist *)NULL;
1707 if ((ep->ex_flag & EX_LINKED) == 0) {
1708 insert_exports(ep, exphead);
1710 ep->ex_flag |= EX_LINKED;
1716 dirhead = (struct dirlist *)NULL;
1722 * Get the export list from all specified files
1725 get_exportlist(void)
1727 struct export_args export;
1729 struct statfs *mntbufp;
1733 struct nfsex_args eargs;
1735 if (suspend_nfsd != 0)
1736 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1737 v4root_dirpath[0] = '\0';
1738 bzero(&export, sizeof(export));
1739 export.ex_flags = MNT_DELEXPORT;
1742 bzero(errmsg, sizeof(errmsg));
1745 * First, get rid of the old list
1747 if (exphead != NULL)
1748 free_exports(exphead);
1751 * and the old V4 root dir.
1753 bzero(&eargs, sizeof (eargs));
1754 eargs.export.ex_flags = MNT_DELEXPORT;
1755 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1757 syslog(LOG_ERR, "Can't delete exports for V4:");
1760 * and clear flag that notes if a public fh has been exported.
1765 * And delete exports that are in the kernel for all local
1767 * XXX: Should know how to handle all local exportable filesystems.
1769 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1771 /* Allocate hash tables, for first call. */
1772 if (exphead == NULL) {
1773 /* Target an average linked list length of 10. */
1774 exphashsize = num / 10;
1775 if (exphashsize < 1)
1777 else if (exphashsize > 100000)
1778 exphashsize = 100000;
1779 exphead = malloc(exphashsize * sizeof(*exphead));
1780 if (exphead == NULL)
1781 errx(1, "Can't malloc hash table");
1783 for (i = 0; i < exphashsize; i++)
1784 SLIST_INIT(&exphead[i]);
1787 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1788 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1789 build_iovec(&iov, &iovlen, "from", NULL, 0);
1790 build_iovec(&iov, &iovlen, "update", NULL, 0);
1791 build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1792 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1795 for (i = 0; i < num; i++)
1796 delete_export(iov, iovlen, &mntbufp[i], errmsg);
1799 /* Free strings allocated by strdup() in getmntopts.c */
1800 free(iov[0].iov_base); /* fstype */
1801 free(iov[2].iov_base); /* fspath */
1802 free(iov[4].iov_base); /* from */
1803 free(iov[6].iov_base); /* update */
1804 free(iov[8].iov_base); /* export */
1805 free(iov[10].iov_base); /* errmsg */
1807 /* free iov, allocated by realloc() */
1815 * If there was no public fh, clear any previous one set.
1817 if (has_publicfh == 0)
1818 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1820 /* Resume the nfsd. If they weren't suspended, this is harmless. */
1821 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1825 * Insert an export entry in the appropriate list.
1828 insert_exports(struct exportlist *ep, struct exportlisthead *exhp)
1832 i = EXPHASH(&ep->ex_fs);
1833 SLIST_INSERT_HEAD(&exhp[i], ep, entries);
1837 * Free up the exports lists passed in as arguments.
1840 free_exports(struct exportlisthead *exhp)
1842 struct exportlist *ep, *ep2;
1845 for (i = 0; i < exphashsize; i++) {
1846 SLIST_FOREACH_SAFE(ep, &exhp[i], entries, ep2) {
1847 SLIST_REMOVE(&exhp[i], ep, exportlist, entries);
1850 SLIST_INIT(&exhp[i]);
1855 * Read the exports file(s) and call get_exportlist_one() for each line.
1858 read_exportfile(void)
1863 * Read in the exports file and build the list, calling
1864 * nmount() as we go along to push the export rules into the kernel.
1867 for (i = 0; exnames[i] != NULL; i++) {
1869 warnx("reading exports from %s", exnames[i]);
1870 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1871 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1874 get_exportlist_one();
1879 syslog(LOG_ERR, "can't open any exports file");
1885 * Delete an exports entry.
1888 delete_export(struct iovec *iov, int iovlen, struct statfs *fsp, char *errmsg)
1890 struct xvfsconf vfc;
1892 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1893 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1899 * We do not need to delete "export" flag from
1900 * filesystems that do not have it set.
1902 if (!(fsp->f_flags & MNT_EXPORTED))
1905 * Do not delete export for network filesystem by
1906 * passing "export" arg to nmount().
1907 * It only makes sense to do this for local filesystems.
1909 if (vfc.vfc_flags & VFCF_NETWORK)
1912 iov[1].iov_base = fsp->f_fstypename;
1913 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1914 iov[3].iov_base = fsp->f_mntonname;
1915 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1916 iov[5].iov_base = fsp->f_mntfromname;
1917 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1921 * EXDEV is returned when path exists but is not a
1922 * mount point. May happens if raced with unmount.
1924 if (nmount(iov, iovlen, fsp->f_flags) < 0 && errno != ENOENT &&
1925 errno != ENOTSUP && errno != EXDEV) {
1927 "can't delete exports for %s: %m %s",
1928 fsp->f_mntonname, errmsg);
1933 * Allocate an export list element
1935 static struct exportlist *
1938 struct exportlist *ep;
1940 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
1941 if (ep == (struct exportlist *)NULL)
1947 * Allocate a group list element
1949 static struct grouplist *
1952 struct grouplist *gp;
1954 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
1955 if (gp == (struct grouplist *)NULL)
1961 * Clean up upon an error in get_exportlist().
1964 getexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
1966 struct grouplist *tgrp;
1968 if (!(opt_flags & OP_QUIET)) {
1970 syslog(LOG_ERR, "bad exports list line '%s': %s", line,
1973 syslog(LOG_ERR, "bad exports list line '%s'", line);
1975 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1985 * Search the export list for a matching fs.
1987 static struct exportlist *
1988 ex_search(fsid_t *fsid, struct exportlisthead *exhp)
1990 struct exportlist *ep;
1994 SLIST_FOREACH(ep, &exhp[i], entries) {
1995 if (ep->ex_fs.val[0] == fsid->val[0] &&
1996 ep->ex_fs.val[1] == fsid->val[1])
2004 * Add a directory path to the list.
2007 add_expdir(struct dirlist **dpp, char *cp, int len)
2011 dp = malloc(sizeof (struct dirlist));
2012 if (dp == (struct dirlist *)NULL)
2015 dp->dp_right = (struct dirlist *)NULL;
2017 dp->dp_hosts = (struct hostlist *)NULL;
2018 dp->dp_dirp = strndup(cp, len);
2019 if (dp->dp_dirp == NULL)
2022 return (dp->dp_dirp);
2026 * Hang the dir list element off the dirpath binary tree as required
2027 * and update the entry for host.
2030 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
2033 struct hostlist *hp;
2034 struct dirlist *dp2;
2036 if (flags & OP_ALLDIRS) {
2041 if (grp == (struct grouplist *)NULL) {
2042 ep->ex_defdir->dp_flag |= DP_DEFSET;
2043 /* Save the default security flavors list. */
2044 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2045 if (ep->ex_numsecflavors > 0)
2046 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2047 sizeof(ep->ex_secflavors));
2048 } else while (grp) {
2051 hp->ht_next = ep->ex_defdir->dp_hosts;
2052 ep->ex_defdir->dp_hosts = hp;
2053 /* Save the security flavors list for this host set. */
2054 grp->gr_numsecflavors = ep->ex_numsecflavors;
2055 if (ep->ex_numsecflavors > 0)
2056 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2057 sizeof(ep->ex_secflavors));
2063 * Loop through the directories adding them to the tree.
2067 add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
2074 * Traverse the binary tree either updating a node that is already there
2075 * for the new directory or adding the new node.
2078 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2079 int flags, struct exportlist *ep)
2082 struct hostlist *hp;
2087 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2089 add_dlist(&dp->dp_left, newdp, grp, flags, ep);
2091 } else if (cmp < 0) {
2092 add_dlist(&dp->dp_right, newdp, grp, flags, ep);
2095 free((caddr_t)newdp);
2098 dp->dp_left = (struct dirlist *)NULL;
2104 * Hang all of the host(s) off of the directory point.
2109 hp->ht_next = dp->dp_hosts;
2111 /* Save the security flavors list for this host set. */
2112 grp->gr_numsecflavors = ep->ex_numsecflavors;
2113 if (ep->ex_numsecflavors > 0)
2114 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2115 sizeof(ep->ex_secflavors));
2119 dp->dp_flag |= DP_DEFSET;
2120 /* Save the default security flavors list. */
2121 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2122 if (ep->ex_numsecflavors > 0)
2123 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2124 sizeof(ep->ex_secflavors));
2129 * Search for a dirpath on the export point.
2131 static struct dirlist *
2132 dirp_search(struct dirlist *dp, char *dirp)
2137 cmp = strcmp(dp->dp_dirp, dirp);
2139 return (dirp_search(dp->dp_left, dirp));
2141 return (dirp_search(dp->dp_right, dirp));
2149 * Scan for a host match in a directory tree.
2152 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2153 int *hostsetp, int *numsecflavors, int **secflavorsp)
2155 struct hostlist *hp;
2156 struct grouplist *grp;
2157 struct addrinfo *ai;
2160 if (dp->dp_flag & DP_DEFSET)
2161 *defsetp = dp->dp_flag;
2165 switch (grp->gr_type) {
2167 ai = grp->gr_ptr.gt_addrinfo;
2168 for (; ai; ai = ai->ai_next) {
2169 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2171 (hp->ht_flag | DP_HOSTSET);
2172 if (numsecflavors != NULL) {
2174 grp->gr_numsecflavors;
2183 if (!sacmp(saddr, (struct sockaddr *)
2184 &grp->gr_ptr.gt_net.nt_net,
2186 &grp->gr_ptr.gt_net.nt_mask)) {
2187 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2188 if (numsecflavors != NULL) {
2190 grp->gr_numsecflavors;
2205 * Scan tree for a host that matches the address.
2208 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2210 int defset, hostset;
2213 if (scan_tree(dp->dp_left, saddr))
2215 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2217 if (scan_tree(dp->dp_right, saddr))
2224 * Traverse the dirlist tree and free it up.
2227 free_dir(struct dirlist *dp)
2231 free_dir(dp->dp_left);
2232 free_dir(dp->dp_right);
2233 free_host(dp->dp_hosts);
2240 * Parse a colon separated list of security flavors
2243 parsesec(char *seclist, struct exportlist *ep)
2248 ep->ex_numsecflavors = 0;
2250 cp = strchr(seclist, ':');
2256 if (!strcmp(seclist, "sys"))
2258 else if (!strcmp(seclist, "krb5"))
2259 flavor = RPCSEC_GSS_KRB5;
2260 else if (!strcmp(seclist, "krb5i"))
2261 flavor = RPCSEC_GSS_KRB5I;
2262 else if (!strcmp(seclist, "krb5p"))
2263 flavor = RPCSEC_GSS_KRB5P;
2267 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2270 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2273 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2276 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2277 ep->ex_numsecflavors++;
2289 * Parse the option string and update fields.
2290 * Option arguments may either be -<option>=<value> or
2294 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2295 int *has_hostp, int *exflagsp, struct xucred *cr)
2297 char *cpoptarg, *cpoptend;
2298 char *cp, *endcp, *cpopt, savedc, savedc2;
2299 int allflag, usedarg;
2307 while (cpopt && *cpopt) {
2310 if ((cpoptend = strchr(cpopt, ','))) {
2312 if ((cpoptarg = strchr(cpopt, '=')))
2315 if ((cpoptarg = strchr(cpopt, '=')))
2319 nextfield(&cp, &endcp);
2321 if (endcp > cp && *cp != '-') {
2329 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2330 *exflagsp |= MNT_EXRDONLY;
2331 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2332 !(allflag = strcmp(cpopt, "mapall")) ||
2333 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2335 parsecred(cpoptarg, cr);
2337 *exflagsp |= MNT_EXPORTANON;
2338 opt_flags |= OP_MAPALL;
2340 opt_flags |= OP_MAPROOT;
2341 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2342 !strcmp(cpopt, "m"))) {
2343 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2344 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2348 opt_flags |= OP_MASK;
2349 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2350 !strcmp(cpopt, "n"))) {
2351 if (strchr(cpoptarg, '/') != NULL) {
2353 fprintf(stderr, "setting OP_MASKLEN\n");
2354 opt_flags |= OP_MASKLEN;
2356 if (grp->gr_type != GT_NULL) {
2357 syslog(LOG_ERR, "network/host conflict");
2359 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2360 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2363 grp->gr_type = GT_NET;
2366 opt_flags |= OP_NET;
2367 } else if (!strcmp(cpopt, "alldirs")) {
2368 opt_flags |= OP_ALLDIRS;
2369 } else if (!strcmp(cpopt, "public")) {
2370 *exflagsp |= MNT_EXPUBLIC;
2371 } else if (!strcmp(cpopt, "webnfs")) {
2372 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2373 opt_flags |= OP_MAPALL;
2374 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2375 ep->ex_indexfile = strdup(cpoptarg);
2376 } else if (!strcmp(cpopt, "quiet")) {
2377 opt_flags |= OP_QUIET;
2378 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2379 if (parsesec(cpoptarg, ep))
2381 opt_flags |= OP_SEC;
2384 syslog(LOG_ERR, "bad opt %s", cpopt);
2403 * Translate a character string to the corresponding list of network
2404 * addresses for a hostname.
2407 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2409 struct grouplist *checkgrp;
2410 struct addrinfo *ai, *tai, hints;
2412 char host[NI_MAXHOST];
2414 if (grp->gr_type != GT_NULL) {
2415 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2418 memset(&hints, 0, sizeof hints);
2419 hints.ai_flags = AI_CANONNAME;
2420 hints.ai_protocol = IPPROTO_UDP;
2421 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2423 syslog(LOG_ERR,"can't get address info for host %s", cp);
2426 grp->gr_ptr.gt_addrinfo = ai;
2427 while (ai != NULL) {
2428 if (ai->ai_canonname == NULL) {
2429 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2430 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2431 strlcpy(host, "?", sizeof(host));
2432 ai->ai_canonname = strdup(host);
2433 ai->ai_flags |= AI_CANONNAME;
2436 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2438 * Sanity check: make sure we don't already have an entry
2439 * for this host in the grouplist.
2441 for (checkgrp = tgrp; checkgrp != NULL;
2442 checkgrp = checkgrp->gr_next) {
2443 if (checkgrp->gr_type != GT_HOST)
2445 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2446 tai = tai->ai_next) {
2447 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2451 "ignoring duplicate host %s\n",
2453 grp->gr_type = GT_IGNORE;
2459 grp->gr_type = GT_HOST;
2464 * Free up an exports list component
2467 free_exp(struct exportlist *ep)
2469 struct grouplist *grp, *tgrp;
2471 if (ep->ex_defdir) {
2472 free_host(ep->ex_defdir->dp_hosts);
2473 free((caddr_t)ep->ex_defdir);
2477 if (ep->ex_indexfile)
2478 free(ep->ex_indexfile);
2479 free_dir(ep->ex_dirl);
2480 grp = ep->ex_grphead;
2493 free_host(struct hostlist *hp)
2495 struct hostlist *hp2;
2504 static struct hostlist *
2507 struct hostlist *hp;
2509 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2510 if (hp == (struct hostlist *)NULL)
2512 hp->ht_next = (struct hostlist *)NULL;
2518 * Out of memory, fatal
2524 syslog(LOG_ERR, "out of memory");
2529 * Do the nmount() syscall with the update flag to push the export info into
2533 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2534 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
2537 struct addrinfo *ai;
2538 struct export_args *eap;
2546 struct nfsex_args nfsea;
2548 eap = &nfsea.export;
2556 bzero(eap, sizeof (struct export_args));
2557 bzero(errmsg, sizeof(errmsg));
2558 eap->ex_flags = exflags;
2559 eap->ex_anon = *anoncrp;
2560 eap->ex_indexfile = ep->ex_indexfile;
2561 if (grp->gr_type == GT_HOST)
2562 ai = grp->gr_ptr.gt_addrinfo;
2565 eap->ex_numsecflavors = ep->ex_numsecflavors;
2566 for (i = 0; i < eap->ex_numsecflavors; i++)
2567 eap->ex_secflavors[i] = ep->ex_secflavors[i];
2568 if (eap->ex_numsecflavors == 0) {
2569 eap->ex_numsecflavors = 1;
2570 eap->ex_secflavors[0] = AUTH_SYS;
2574 if (v4root_phase == 0) {
2575 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2576 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2577 build_iovec(&iov, &iovlen, "from", NULL, 0);
2578 build_iovec(&iov, &iovlen, "update", NULL, 0);
2579 build_iovec(&iov, &iovlen, "export", eap,
2580 sizeof (struct export_args));
2581 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2585 switch (grp->gr_type) {
2587 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
2589 eap->ex_addr = ai->ai_addr;
2590 eap->ex_addrlen = ai->ai_addrlen;
2591 eap->ex_masklen = 0;
2594 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
2598 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2600 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2602 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2603 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
2606 eap->ex_addr = NULL;
2607 eap->ex_addrlen = 0;
2608 eap->ex_mask = NULL;
2609 eap->ex_masklen = 0;
2616 syslog(LOG_ERR, "bad grouptype");
2624 * For V4:, use the nfssvc() syscall, instead of mount().
2626 if (v4root_phase == 2) {
2627 nfsea.fspec = v4root_dirpath;
2628 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2629 syslog(LOG_ERR, "Exporting V4: failed");
2635 * Maybe I should just use the fsb->f_mntonname path
2636 * instead of looping back up the dirp to the mount
2638 * Also, needs to know how to export all types of local
2639 * exportable filesystems and not just "ufs".
2641 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2642 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2643 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2644 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2645 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2646 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2649 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2653 cp = dirp + dirplen - 1;
2654 if (opt_flags & OP_QUIET) {
2658 if (errno == EPERM) {
2660 warnx("can't change attributes for %s: %s",
2663 "can't change attributes for %s: %s",
2668 if (opt_flags & OP_ALLDIRS) {
2669 if (errno == EINVAL)
2671 "-alldirs requested but %s is not a filesystem mountpoint",
2675 "could not remount %s: %m",
2680 /* back up over the last component */
2681 while (*cp == '/' && cp > dirp)
2683 while (*(cp - 1) != '/' && cp > dirp)
2687 warnx("mnt unsucc");
2688 syslog(LOG_ERR, "can't export %s %s",
2696 * Check that we're still on the same
2699 if (statfs(dirp, &fsb1) != 0 ||
2700 bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2701 sizeof (fsb1.f_fsid)) != 0) {
2704 "can't export %s %s", dirp,
2713 * For the experimental server:
2714 * If this is the public directory, get the file handle
2715 * and load it into the kernel via the nfssvc() syscall.
2717 if ((exflags & MNT_EXPUBLIC) != 0) {
2721 if (eap->ex_indexfile != NULL)
2722 public_name = eap->ex_indexfile;
2725 if (getfh(public_name, &fh) < 0)
2727 "Can't get public fh for %s", public_name);
2728 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2730 "Can't set public fh for %s", public_name);
2743 /* free strings allocated by strdup() in getmntopts.c */
2745 free(iov[0].iov_base); /* fstype */
2746 free(iov[2].iov_base); /* fspath */
2747 free(iov[4].iov_base); /* from */
2748 free(iov[6].iov_base); /* update */
2749 free(iov[8].iov_base); /* export */
2750 free(iov[10].iov_base); /* errmsg */
2752 /* free iov, allocated by realloc() */
2759 * Translate a net address.
2761 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2764 get_net(char *cp, struct netmsk *net, int maskflg)
2766 struct netent *np = NULL;
2767 char *name, *p, *prefp;
2768 struct sockaddr_in sin;
2769 struct sockaddr *sa = NULL;
2770 struct addrinfo hints, *ai = NULL;
2771 char netname[NI_MAXHOST];
2775 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2776 p = strchr(cp, '/');
2782 * Check for a numeric address first. We wish to avoid
2783 * possible DNS lookups in getnetbyname().
2785 if (isxdigit(*cp) || *cp == ':') {
2786 memset(&hints, 0, sizeof hints);
2787 /* Ensure the mask and the network have the same family. */
2788 if (maskflg && (opt_flags & OP_NET))
2789 hints.ai_family = net->nt_net.ss_family;
2790 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2791 hints.ai_family = net->nt_mask.ss_family;
2793 hints.ai_family = AF_UNSPEC;
2794 hints.ai_flags = AI_NUMERICHOST;
2795 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2797 if (sa != NULL && ai->ai_family == AF_INET) {
2799 * The address in `cp' is really a network address, so
2800 * use inet_network() to re-interpret this correctly.
2801 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2803 bzero(&sin, sizeof sin);
2804 sin.sin_family = AF_INET;
2805 sin.sin_len = sizeof sin;
2806 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2808 fprintf(stderr, "get_net: v4 addr %s\n",
2809 inet_ntoa(sin.sin_addr));
2810 sa = (struct sockaddr *)&sin;
2813 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2814 bzero(&sin, sizeof sin);
2815 sin.sin_family = AF_INET;
2816 sin.sin_len = sizeof sin;
2817 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2818 sa = (struct sockaddr *)&sin;
2824 /* The specified sockaddr is a mask. */
2825 if (checkmask(sa) != 0)
2827 bcopy(sa, &net->nt_mask, sa->sa_len);
2828 opt_flags |= OP_HAVEMASK;
2830 /* The specified sockaddr is a network address. */
2831 bcopy(sa, &net->nt_net, sa->sa_len);
2833 /* Get a network name for the export list. */
2836 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2837 NULL, 0, NI_NUMERICHOST) == 0) {
2842 if ((net->nt_name = strdup(name)) == NULL)
2846 * Extract a mask from either a "/<masklen>" suffix, or
2847 * from the class of an IPv4 address.
2849 if (opt_flags & OP_MASKLEN) {
2850 preflen = strtol(prefp, NULL, 10);
2851 if (preflen < 0L || preflen == LONG_MAX)
2853 bcopy(sa, &net->nt_mask, sa->sa_len);
2854 if (makemask(&net->nt_mask, (int)preflen) != 0)
2856 opt_flags |= OP_HAVEMASK;
2858 } else if (sa->sa_family == AF_INET &&
2859 (opt_flags & OP_MASK) == 0) {
2862 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2863 if (IN_CLASSA(addr))
2865 else if (IN_CLASSB(addr))
2867 else if (IN_CLASSC(addr))
2869 else if (IN_CLASSD(addr))
2872 preflen = 32; /* XXX */
2874 bcopy(sa, &net->nt_mask, sa->sa_len);
2875 makemask(&net->nt_mask, (int)preflen);
2876 opt_flags |= OP_HAVEMASK;
2891 * Parse out the next white space separated field
2894 nextfield(char **cp, char **endcp)
2900 while (*p == ' ' || *p == '\t')
2903 while (*p != '\0') {
2908 if (*p == '\\' && *(p + 1) != '\0')
2910 else if (*p == '\'' || *p == '"')
2912 else if (*p == ' ' || *p == '\t')
2921 * Get an exports file line. Skip over blank lines and handle line
2929 int totlen, cont_line;
2932 * Loop around ignoring blank lines and getting all continuation lines.
2937 if ((p = fgetln(exp_file, &len)) == NULL)
2942 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2952 if (linesize < len + totlen + 1) {
2953 linesize = len + totlen + 1;
2954 line = realloc(line, linesize);
2958 memcpy(line + totlen, p, len);
2960 line[totlen] = '\0';
2961 } while (totlen == 0 || cont_line);
2966 * Parse a description of a credential.
2969 parsecred(char *namelist, struct xucred *cr)
2976 gid_t groups[XU_NGROUPS + 1];
2979 cr->cr_version = XUCRED_VERSION;
2981 * Set up the unprivileged user.
2984 cr->cr_groups[0] = 65533;
2987 * Get the user's password table entry.
2990 name = strsep_quote(&names, ":");
2991 /* Bug? name could be NULL here */
2992 if (isdigit(*name) || *name == '-')
2993 pw = getpwuid(atoi(name));
2995 pw = getpwnam(name);
2997 * Credentials specified as those of a user.
2999 if (names == NULL) {
3001 syslog(LOG_ERR, "unknown user: %s", name);
3004 cr->cr_uid = pw->pw_uid;
3005 ngroups = XU_NGROUPS + 1;
3006 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) {
3007 syslog(LOG_ERR, "too many groups");
3008 ngroups = XU_NGROUPS + 1;
3012 * Compress out duplicate.
3014 cr->cr_ngroups = ngroups - 1;
3015 cr->cr_groups[0] = groups[0];
3016 for (cnt = 2; cnt < ngroups; cnt++)
3017 cr->cr_groups[cnt - 1] = groups[cnt];
3021 * Explicit credential specified as a colon separated list:
3025 cr->cr_uid = pw->pw_uid;
3026 else if (isdigit(*name) || *name == '-')
3027 cr->cr_uid = atoi(name);
3029 syslog(LOG_ERR, "unknown user: %s", name);
3033 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
3034 name = strsep_quote(&names, ":");
3035 if (isdigit(*name) || *name == '-') {
3036 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
3038 if ((gr = getgrnam(name)) == NULL) {
3039 syslog(LOG_ERR, "unknown group: %s", name);
3042 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
3045 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
3046 syslog(LOG_ERR, "too many groups");
3049 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
3051 * Routines that maintain the remote mounttab
3056 struct mountlist *mlp;
3057 char *host, *dirp, *cp;
3061 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
3062 if (errno == ENOENT)
3065 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
3069 while (fgets(str, STRSIZ, mlfile) != NULL) {
3071 host = strsep(&cp, " \t\n");
3072 dirp = strsep(&cp, " \t\n");
3073 if (host == NULL || dirp == NULL)
3075 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3076 if (mlp == (struct mountlist *)NULL)
3078 strncpy(mlp->ml_host, host, MNTNAMLEN);
3079 mlp->ml_host[MNTNAMLEN] = '\0';
3080 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3081 mlp->ml_dirp[MNTPATHLEN] = '\0';
3083 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3089 del_mlist(char *hostp, char *dirp)
3091 struct mountlist *mlp, *mlp2;
3095 SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
3096 if (!strcmp(mlp->ml_host, hostp) &&
3097 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3099 SLIST_REMOVE(&mlhead, mlp, mountlist, next);
3104 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3105 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3108 SLIST_FOREACH(mlp, &mlhead, next) {
3109 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3116 add_mlist(char *hostp, char *dirp)
3118 struct mountlist *mlp;
3121 SLIST_FOREACH(mlp, &mlhead, next) {
3122 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3126 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3127 if (mlp == (struct mountlist *)NULL)
3129 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3130 mlp->ml_host[MNTNAMLEN] = '\0';
3131 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3132 mlp->ml_dirp[MNTPATHLEN] = '\0';
3133 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3134 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3135 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3138 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3143 * Free up a group list.
3146 free_grp(struct grouplist *grp)
3148 if (grp->gr_type == GT_HOST) {
3149 if (grp->gr_ptr.gt_addrinfo != NULL)
3150 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3151 } else if (grp->gr_type == GT_NET) {
3152 if (grp->gr_ptr.gt_net.nt_name)
3153 free(grp->gr_ptr.gt_net.nt_name);
3160 SYSLOG(int pri, const char *fmt, ...)
3165 vfprintf(stderr, fmt, ap);
3171 * Check options for consistency.
3174 check_options(struct dirlist *dp)
3177 if (v4root_phase == 0 && dp == NULL)
3179 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3180 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3183 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3184 syslog(LOG_ERR, "-mask requires -network");
3187 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3188 syslog(LOG_ERR, "-network requires mask specification");
3191 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3192 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3195 if (v4root_phase > 0 &&
3197 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3198 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3201 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3202 syslog(LOG_ERR, "-alldirs has multiple directories");
3209 * Check an absolute directory path for any symbolic links. Return true
3212 check_dirpath(char *dirp)
3219 while (*cp && ret) {
3222 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3228 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3234 * Make a netmask according to the specified prefix length. The ss_family
3235 * and other non-address fields must be initialised before calling this.
3238 makemask(struct sockaddr_storage *ssp, int bitlen)
3243 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3245 if (bitlen > len * CHAR_BIT)
3248 for (i = 0; i < len; i++) {
3249 bits = MIN(CHAR_BIT, bitlen);
3250 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3257 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3258 * is acceptable (i.e. of the form 1...10....0).
3261 checkmask(struct sockaddr *sa)
3266 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3269 for (i = 0; i < len; i++)
3270 if (mask[i] != 0xff)
3273 if (~mask[i] & (u_char)(~mask[i] + 1))
3277 for (; i < len; i++)
3284 * Compare two sockaddrs according to a specified mask. Return zero if
3285 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3286 * If samask is NULL, perform a full comparison.
3289 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3291 unsigned char *p1, *p2, *mask;
3294 if (sa1->sa_family != sa2->sa_family ||
3295 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3296 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3299 switch (sa1->sa_family) {
3301 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3302 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3307 /* Simple binary comparison if no mask specified. */
3309 return (memcmp(p1, p2, len));
3311 /* Set up the mask, and do a mask-based comparison. */
3312 if (sa1->sa_family != samask->sa_family ||
3313 (mask = sa_rawaddr(samask, NULL)) == NULL)
3316 for (i = 0; i < len; i++)
3317 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3323 * Return a pointer to the part of the sockaddr that contains the
3324 * raw address, and set *nbytes to its length in bytes. Returns
3325 * NULL if the address family is unknown.
3328 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3332 switch (sa->sa_family) {
3334 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3335 p = &((struct sockaddr_in *)sa)->sin_addr;
3338 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3339 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3352 huphandler(int sig __unused)
3359 terminate(int sig __unused)
3361 pidfile_remove(pfh);
3362 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3363 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);