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++];
912 * Using -1 tells listen(2) to use
913 * kern.ipc.soacceptqueue for the backlog.
915 if (nconf->nc_semantics != NC_TPI_CLTS)
918 if (nconf->nc_semantics == NC_TPI_CLTS )
919 transp = svc_dg_create(fd, 0, 0);
921 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
924 if (transp != (SVCXPRT *) NULL) {
925 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
928 "can't register %s MOUNTVERS service",
931 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
934 "can't register %s MOUNTVERS3 service",
938 syslog(LOG_WARNING, "can't create %s services",
941 if (registered == 0) {
943 memset(&hints, 0, sizeof hints);
944 hints.ai_flags = AI_PASSIVE;
945 hints.ai_family = si.si_af;
946 hints.ai_socktype = si.si_socktype;
947 hints.ai_protocol = si.si_proto;
949 if ((aicode = getaddrinfo(NULL, port_str, &hints,
951 syslog(LOG_ERR, "cannot get local address: %s",
952 gai_strerror(aicode));
956 servaddr.buf = malloc(res->ai_addrlen);
957 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
958 servaddr.len = res->ai_addrlen;
960 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
961 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
970 * Clear out sockets after a failure to bind one of them, so that the
971 * cycle of socket creation/binding can start anew.
974 clearout_service(void)
978 for (i = 0; i < sock_fdcnt; i++) {
979 if (sock_fd[i] >= 0) {
980 shutdown(sock_fd[i], SHUT_RDWR);
990 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
991 "[-S] [-h <bindip>] [export_file ...]\n");
996 * The mount rpc service
999 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
1001 struct exportlist *ep;
1003 struct fhreturn fhr;
1006 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1007 int lookup_failed = 1;
1008 struct sockaddr *saddr;
1010 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1011 int bad = 0, defset, hostset;
1012 sigset_t sighup_mask;
1013 int numsecflavors, *secflavorsp;
1015 sigemptyset(&sighup_mask);
1016 sigaddset(&sighup_mask, SIGHUP);
1017 saddr = svc_getrpccaller(transp)->buf;
1018 switch (saddr->sa_family) {
1020 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1023 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1026 syslog(LOG_ERR, "request from unknown address family");
1029 switch (rqstp->rq_proc) {
1031 case MOUNTPROC_UMNT:
1032 case MOUNTPROC_UMNTALL:
1033 lookup_failed = getnameinfo(saddr, saddr->sa_len, host,
1034 sizeof host, NULL, 0, 0);
1036 getnameinfo(saddr, saddr->sa_len, numerichost,
1037 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1038 switch (rqstp->rq_proc) {
1040 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1041 syslog(LOG_ERR, "can't send reply");
1044 if (sport >= IPPORT_RESERVED && resvport_only) {
1046 "mount request from %s from unprivileged port",
1048 svcerr_weakauth(transp);
1051 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1052 syslog(LOG_NOTICE, "undecodable mount request from %s",
1054 svcerr_decode(transp);
1059 * Get the real pathname and make sure it is a directory
1060 * or a regular file if the -r option was specified
1063 if (realpath(rpcpath, dirpath) == NULL ||
1064 stat(dirpath, &stb) < 0 ||
1065 statfs(dirpath, &fsb) < 0) {
1066 chdir("/"); /* Just in case realpath doesn't */
1068 "mount request from %s for non existent path %s",
1069 numerichost, dirpath);
1071 warnx("stat failed on %s", dirpath);
1072 bad = ENOENT; /* We will send error reply later */
1075 !S_ISDIR(stb.st_mode) &&
1076 (dir_only || !S_ISREG(stb.st_mode))) {
1078 "mount request from %s for non-directory path %s",
1079 numerichost, dirpath);
1081 warnx("mounting non-directory %s", dirpath);
1082 bad = ENOTDIR; /* We will send error reply later */
1085 /* Check in the exports list */
1086 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1090 ep = ex_search(&fsb.f_fsid);
1091 hostset = defset = 0;
1092 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1093 &numsecflavors, &secflavorsp) ||
1094 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1095 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1097 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1098 scan_tree(ep->ex_dirl, saddr) == 0))) {
1100 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1102 syslog(LOG_ERR, "can't send reply");
1103 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1106 if (hostset & DP_HOSTSET) {
1107 fhr.fhr_flag = hostset;
1108 fhr.fhr_numsecflavors = numsecflavors;
1109 fhr.fhr_secflavors = secflavorsp;
1111 fhr.fhr_flag = defset;
1112 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1113 fhr.fhr_secflavors = ep->ex_defsecflavors;
1115 fhr.fhr_vers = rqstp->rq_vers;
1116 /* Get the file handle */
1117 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1118 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1120 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1121 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1123 syslog(LOG_ERR, "can't send reply");
1124 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1127 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1129 syslog(LOG_ERR, "can't send reply");
1131 add_mlist(host, dirpath);
1133 add_mlist(numerichost, dirpath);
1135 warnx("mount successful");
1138 "mount request succeeded from %s for %s",
1139 numerichost, dirpath);
1144 "mount request denied from %s for %s",
1145 numerichost, dirpath);
1148 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1150 syslog(LOG_ERR, "can't send reply");
1151 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1153 case MOUNTPROC_DUMP:
1154 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1155 syslog(LOG_ERR, "can't send reply");
1158 "dump request succeeded from %s",
1161 case MOUNTPROC_UMNT:
1162 if (sport >= IPPORT_RESERVED && resvport_only) {
1164 "umount request from %s from unprivileged port",
1166 svcerr_weakauth(transp);
1169 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1170 syslog(LOG_NOTICE, "undecodable umount request from %s",
1172 svcerr_decode(transp);
1175 if (realpath(rpcpath, dirpath) == NULL) {
1176 syslog(LOG_NOTICE, "umount request from %s "
1177 "for non existent path %s",
1178 numerichost, dirpath);
1180 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1181 syslog(LOG_ERR, "can't send reply");
1183 del_mlist(host, dirpath);
1184 del_mlist(numerichost, dirpath);
1187 "umount request succeeded from %s for %s",
1188 numerichost, dirpath);
1190 case MOUNTPROC_UMNTALL:
1191 if (sport >= IPPORT_RESERVED && resvport_only) {
1193 "umountall request from %s from unprivileged port",
1195 svcerr_weakauth(transp);
1198 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1199 syslog(LOG_ERR, "can't send reply");
1201 del_mlist(host, NULL);
1202 del_mlist(numerichost, NULL);
1205 "umountall request succeeded from %s",
1208 case MOUNTPROC_EXPORT:
1209 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1210 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1212 syslog(LOG_ERR, "can't send reply");
1215 "export request succeeded from %s",
1219 svcerr_noproc(transp);
1225 * Xdr conversion for a dirpath string
1228 xdr_dir(XDR *xdrsp, char *dirp)
1230 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1234 * Xdr routine to generate file handle reply
1237 xdr_fhs(XDR *xdrsp, caddr_t cp)
1239 struct fhreturn *fhrp = (struct fhreturn *)cp;
1240 u_long ok = 0, len, auth;
1243 if (!xdr_long(xdrsp, &ok))
1245 switch (fhrp->fhr_vers) {
1247 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1250 if (!xdr_long(xdrsp, &len))
1252 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1254 if (fhrp->fhr_numsecflavors) {
1255 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1257 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1258 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1264 if (!xdr_long(xdrsp, &len))
1266 return (xdr_long(xdrsp, &auth));
1273 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1275 struct mountlist *mlp;
1280 SLIST_FOREACH(mlp, &mlhead, next) {
1281 if (!xdr_bool(xdrsp, &true))
1283 strp = &mlp->ml_host[0];
1284 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1286 strp = &mlp->ml_dirp[0];
1287 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1290 if (!xdr_bool(xdrsp, &false))
1296 * Xdr conversion for export list
1299 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1301 struct exportlist *ep;
1304 sigset_t sighup_mask;
1306 sigemptyset(&sighup_mask);
1307 sigaddset(&sighup_mask, SIGHUP);
1308 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1310 SLIST_FOREACH(ep, &exphead, entries) {
1312 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1315 if (ep->ex_defdir && putdef == 0 &&
1316 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1320 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1321 if (!xdr_bool(xdrsp, &false))
1325 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1330 * Called from xdr_explist() to traverse the tree and export the
1334 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1337 struct grouplist *grp;
1338 struct hostlist *hp;
1345 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1347 if (!xdr_bool(xdrsp, &true))
1350 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1352 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1357 if (!xdr_bool(xdrsp, &true))
1360 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1362 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1363 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1367 if (grp->gr_type == GT_HOST) {
1368 if (!xdr_bool(xdrsp, &true))
1370 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1371 if (!xdr_string(xdrsp, &strp,
1374 } else if (grp->gr_type == GT_NET) {
1375 if (!xdr_bool(xdrsp, &true))
1377 strp = grp->gr_ptr.gt_net.nt_name;
1378 if (!xdr_string(xdrsp, &strp,
1383 if (gotalldir && hp == (struct hostlist *)NULL) {
1389 if (!xdr_bool(xdrsp, &false))
1391 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1398 xdr_explist(XDR *xdrsp, caddr_t cp)
1401 return xdr_explist_common(xdrsp, cp, 0);
1405 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1408 return xdr_explist_common(xdrsp, cp, 1);
1412 static size_t linesize;
1413 static FILE *exp_file;
1416 * Get the export list from one, currently open file
1419 get_exportlist_one(void)
1421 struct exportlist *ep;
1422 struct grouplist *grp, *tgrp;
1423 struct dirlist *dirhead;
1426 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1427 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1430 dirhead = (struct dirlist *)NULL;
1431 while (get_line()) {
1433 warnx("got line %s", line);
1435 nextfield(&cp, &endcp);
1444 exflags = MNT_EXPORTED;
1447 ep = (struct exportlist *)NULL;
1451 * Handle the V4 root dir.
1453 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1455 * V4: just indicates that it is the v4 root point,
1456 * so skip over that and set v4root_phase.
1458 if (v4root_phase > 0) {
1459 syslog(LOG_ERR, "V4:duplicate line, ignored");
1464 nextfield(&cp, &endcp);
1468 * Create new exports list entry
1471 tgrp = grp = get_grp();
1473 if (len > MNTNAMLEN) {
1474 getexp_err(ep, tgrp, "mountpoint too long");
1478 if (ep == (struct exportlist *)NULL) {
1479 getexp_err(ep, tgrp,
1480 "flag before export path definition");
1484 warnx("doing opt %s", cp);
1486 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1488 getexp_err(ep, tgrp, NULL);
1491 } else if (*cp == '/') {
1494 if (v4root_phase > 1) {
1496 getexp_err(ep, tgrp, "Multiple V4 dirs");
1500 if (check_dirpath(cp) &&
1501 statfs(cp, &fsb) >= 0) {
1502 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1503 syslog(LOG_ERR, "Warning: exporting of "
1504 "automounted fs %s not supported", cp);
1506 getexp_err(ep, tgrp, "dirs must be first");
1509 if (v4root_phase == 1) {
1511 getexp_err(ep, tgrp, "Multiple V4 dirs");
1514 if (strlen(v4root_dirpath) == 0) {
1515 strlcpy(v4root_dirpath, cp,
1516 sizeof (v4root_dirpath));
1517 } else if (strcmp(v4root_dirpath, cp)
1520 "different V4 dirpath %s", cp);
1521 getexp_err(ep, tgrp, NULL);
1530 if (ep->ex_fs.val[0] !=
1531 fsb.f_fsid.val[0] ||
1533 fsb.f_fsid.val[1]) {
1534 getexp_err(ep, tgrp,
1540 * See if this directory is already
1543 ep = ex_search(&fsb.f_fsid);
1544 if (ep == (struct exportlist *)NULL) {
1546 ep->ex_fs = fsb.f_fsid;
1547 ep->ex_fsdir = strdup(fsb.f_mntonname);
1548 if (ep->ex_fsdir == NULL)
1552 "making new ep fs=0x%x,0x%x",
1556 warnx("found ep fs=0x%x,0x%x",
1562 * Add dirpath to export mount point.
1564 dirp = add_expdir(&dirhead, cp, len);
1568 getexp_err(ep, tgrp,
1569 "symbolic link in export path or statfs failed");
1577 if (ep == (struct exportlist *)NULL) {
1578 getexp_err(ep, tgrp,
1579 "host(s) before export path definition");
1584 * Get the host or netgroup.
1587 netgrp = getnetgrent(&hst, &usr, &dom);
1590 grp->gr_next = get_grp();
1596 "null hostname in netgroup %s, skipping", cp);
1597 grp->gr_type = GT_IGNORE;
1598 } else if (get_host(hst, grp, tgrp)) {
1600 "bad host %s in netgroup %s, skipping", hst, cp);
1601 grp->gr_type = GT_IGNORE;
1603 } else if (get_host(cp, grp, tgrp)) {
1604 syslog(LOG_ERR, "bad host %s, skipping", cp);
1605 grp->gr_type = GT_IGNORE;
1608 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1613 nextfield(&cp, &endcp);
1616 if (check_options(dirhead)) {
1617 getexp_err(ep, tgrp, NULL);
1621 grp->gr_type = GT_DEFAULT;
1623 warnx("adding a default entry");
1626 * Don't allow a network export coincide with a list of
1627 * host(s) on the same line.
1629 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1630 getexp_err(ep, tgrp, "network/host conflict");
1634 * If an export list was specified on this line, make sure
1635 * that we have at least one valid entry, otherwise skip it.
1639 while (grp && grp->gr_type == GT_IGNORE)
1642 getexp_err(ep, tgrp, "no valid entries");
1647 if (v4root_phase == 1) {
1648 getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1653 * Loop through hosts, pushing the exports into the kernel.
1654 * After loop, tgrp points to the start of the list and
1655 * grp points to the last entry in the list.
1659 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1661 getexp_err(ep, tgrp, NULL);
1664 } while (grp->gr_next && (grp = grp->gr_next));
1667 * For V4: don't enter in mount lists.
1669 if (v4root_phase > 0 && v4root_phase <= 2) {
1671 * Since these structures aren't used by mountd,
1676 while (tgrp != NULL) {
1678 tgrp = tgrp->gr_next;
1685 * Success. Update the data structures.
1688 hang_dirp(dirhead, tgrp, ep, opt_flags);
1689 grp->gr_next = grphead;
1692 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1696 dirhead = (struct dirlist *)NULL;
1697 if ((ep->ex_flag & EX_LINKED) == 0) {
1698 SLIST_INSERT_HEAD(&exphead, ep, entries);
1700 ep->ex_flag |= EX_LINKED;
1706 dirhead = (struct dirlist *)NULL;
1712 * Get the export list from all specified files
1715 get_exportlist(void)
1717 struct exportlist *ep, *ep2;
1718 struct grouplist *grp, *tgrp;
1719 struct export_args export;
1721 struct statfs *fsp, *mntbufp;
1722 struct xvfsconf vfc;
1727 struct nfsex_args eargs;
1729 if (suspend_nfsd != 0)
1730 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1731 v4root_dirpath[0] = '\0';
1732 bzero(&export, sizeof(export));
1733 export.ex_flags = MNT_DELEXPORT;
1736 bzero(errmsg, sizeof(errmsg));
1739 * First, get rid of the old list
1741 SLIST_FOREACH_SAFE(ep, &exphead, entries, ep2) {
1742 SLIST_REMOVE(&exphead, ep, exportlist, entries);
1752 grphead = (struct grouplist *)NULL;
1755 * and the old V4 root dir.
1757 bzero(&eargs, sizeof (eargs));
1758 eargs.export.ex_flags = MNT_DELEXPORT;
1759 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1761 syslog(LOG_ERR, "Can't delete exports for V4:");
1764 * and clear flag that notes if a public fh has been exported.
1769 * And delete exports that are in the kernel for all local
1771 * XXX: Should know how to handle all local exportable filesystems.
1773 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1776 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1777 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1778 build_iovec(&iov, &iovlen, "from", NULL, 0);
1779 build_iovec(&iov, &iovlen, "update", NULL, 0);
1780 build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1781 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1784 for (i = 0; i < num; i++) {
1786 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1787 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1793 * We do not need to delete "export" flag from
1794 * filesystems that do not have it set.
1796 if (!(fsp->f_flags & MNT_EXPORTED))
1799 * Do not delete export for network filesystem by
1800 * passing "export" arg to nmount().
1801 * It only makes sense to do this for local filesystems.
1803 if (vfc.vfc_flags & VFCF_NETWORK)
1806 iov[1].iov_base = fsp->f_fstypename;
1807 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1808 iov[3].iov_base = fsp->f_mntonname;
1809 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1810 iov[5].iov_base = fsp->f_mntfromname;
1811 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1815 * EXDEV is returned when path exists but is not a
1816 * mount point. May happens if raced with unmount.
1818 if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1819 errno != ENOENT && errno != ENOTSUP && errno != EXDEV) {
1821 "can't delete exports for %s: %m %s",
1822 fsp->f_mntonname, errmsg);
1827 /* Free strings allocated by strdup() in getmntopts.c */
1828 free(iov[0].iov_base); /* fstype */
1829 free(iov[2].iov_base); /* fspath */
1830 free(iov[4].iov_base); /* from */
1831 free(iov[6].iov_base); /* update */
1832 free(iov[8].iov_base); /* export */
1833 free(iov[10].iov_base); /* errmsg */
1835 /* free iov, allocated by realloc() */
1841 * Read in the exports file and build the list, calling
1842 * nmount() as we go along to push the export rules into the kernel.
1845 for (i = 0; exnames[i] != NULL; i++) {
1847 warnx("reading exports from %s", exnames[i]);
1848 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1849 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1852 get_exportlist_one();
1857 syslog(LOG_ERR, "can't open any exports file");
1862 * If there was no public fh, clear any previous one set.
1864 if (has_publicfh == 0)
1865 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1867 /* Resume the nfsd. If they weren't suspended, this is harmless. */
1868 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1872 * Allocate an export list element
1874 static struct exportlist *
1877 struct exportlist *ep;
1879 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
1880 if (ep == (struct exportlist *)NULL)
1886 * Allocate a group list element
1888 static struct grouplist *
1891 struct grouplist *gp;
1893 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
1894 if (gp == (struct grouplist *)NULL)
1900 * Clean up upon an error in get_exportlist().
1903 getexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
1905 struct grouplist *tgrp;
1907 if (!(opt_flags & OP_QUIET)) {
1909 syslog(LOG_ERR, "bad exports list line '%s': %s", line,
1912 syslog(LOG_ERR, "bad exports list line '%s'", line);
1914 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1924 * Search the export list for a matching fs.
1926 static struct exportlist *
1927 ex_search(fsid_t *fsid)
1929 struct exportlist *ep;
1931 SLIST_FOREACH(ep, &exphead, entries) {
1932 if (ep->ex_fs.val[0] == fsid->val[0] &&
1933 ep->ex_fs.val[1] == fsid->val[1])
1941 * Add a directory path to the list.
1944 add_expdir(struct dirlist **dpp, char *cp, int len)
1948 dp = malloc(sizeof (struct dirlist));
1949 if (dp == (struct dirlist *)NULL)
1952 dp->dp_right = (struct dirlist *)NULL;
1954 dp->dp_hosts = (struct hostlist *)NULL;
1955 dp->dp_dirp = strndup(cp, len);
1956 if (dp->dp_dirp == NULL)
1959 return (dp->dp_dirp);
1963 * Hang the dir list element off the dirpath binary tree as required
1964 * and update the entry for host.
1967 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1970 struct hostlist *hp;
1971 struct dirlist *dp2;
1973 if (flags & OP_ALLDIRS) {
1978 if (grp == (struct grouplist *)NULL) {
1979 ep->ex_defdir->dp_flag |= DP_DEFSET;
1980 /* Save the default security flavors list. */
1981 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1982 if (ep->ex_numsecflavors > 0)
1983 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1984 sizeof(ep->ex_secflavors));
1985 } else while (grp) {
1988 hp->ht_next = ep->ex_defdir->dp_hosts;
1989 ep->ex_defdir->dp_hosts = hp;
1990 /* Save the security flavors list for this host set. */
1991 grp->gr_numsecflavors = ep->ex_numsecflavors;
1992 if (ep->ex_numsecflavors > 0)
1993 memcpy(grp->gr_secflavors, ep->ex_secflavors,
1994 sizeof(ep->ex_secflavors));
2000 * Loop through the directories adding them to the tree.
2004 add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
2011 * Traverse the binary tree either updating a node that is already there
2012 * for the new directory or adding the new node.
2015 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2016 int flags, struct exportlist *ep)
2019 struct hostlist *hp;
2024 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2026 add_dlist(&dp->dp_left, newdp, grp, flags, ep);
2028 } else if (cmp < 0) {
2029 add_dlist(&dp->dp_right, newdp, grp, flags, ep);
2032 free((caddr_t)newdp);
2035 dp->dp_left = (struct dirlist *)NULL;
2041 * Hang all of the host(s) off of the directory point.
2046 hp->ht_next = dp->dp_hosts;
2048 /* Save the security flavors list for this host set. */
2049 grp->gr_numsecflavors = ep->ex_numsecflavors;
2050 if (ep->ex_numsecflavors > 0)
2051 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2052 sizeof(ep->ex_secflavors));
2056 dp->dp_flag |= DP_DEFSET;
2057 /* Save the default security flavors list. */
2058 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2059 if (ep->ex_numsecflavors > 0)
2060 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2061 sizeof(ep->ex_secflavors));
2066 * Search for a dirpath on the export point.
2068 static struct dirlist *
2069 dirp_search(struct dirlist *dp, char *dirp)
2074 cmp = strcmp(dp->dp_dirp, dirp);
2076 return (dirp_search(dp->dp_left, dirp));
2078 return (dirp_search(dp->dp_right, dirp));
2086 * Scan for a host match in a directory tree.
2089 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2090 int *hostsetp, int *numsecflavors, int **secflavorsp)
2092 struct hostlist *hp;
2093 struct grouplist *grp;
2094 struct addrinfo *ai;
2097 if (dp->dp_flag & DP_DEFSET)
2098 *defsetp = dp->dp_flag;
2102 switch (grp->gr_type) {
2104 ai = grp->gr_ptr.gt_addrinfo;
2105 for (; ai; ai = ai->ai_next) {
2106 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2108 (hp->ht_flag | DP_HOSTSET);
2109 if (numsecflavors != NULL) {
2111 grp->gr_numsecflavors;
2120 if (!sacmp(saddr, (struct sockaddr *)
2121 &grp->gr_ptr.gt_net.nt_net,
2123 &grp->gr_ptr.gt_net.nt_mask)) {
2124 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2125 if (numsecflavors != NULL) {
2127 grp->gr_numsecflavors;
2142 * Scan tree for a host that matches the address.
2145 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2147 int defset, hostset;
2150 if (scan_tree(dp->dp_left, saddr))
2152 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2154 if (scan_tree(dp->dp_right, saddr))
2161 * Traverse the dirlist tree and free it up.
2164 free_dir(struct dirlist *dp)
2168 free_dir(dp->dp_left);
2169 free_dir(dp->dp_right);
2170 free_host(dp->dp_hosts);
2177 * Parse a colon separated list of security flavors
2180 parsesec(char *seclist, struct exportlist *ep)
2185 ep->ex_numsecflavors = 0;
2187 cp = strchr(seclist, ':');
2193 if (!strcmp(seclist, "sys"))
2195 else if (!strcmp(seclist, "krb5"))
2196 flavor = RPCSEC_GSS_KRB5;
2197 else if (!strcmp(seclist, "krb5i"))
2198 flavor = RPCSEC_GSS_KRB5I;
2199 else if (!strcmp(seclist, "krb5p"))
2200 flavor = RPCSEC_GSS_KRB5P;
2204 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2207 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2210 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2213 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2214 ep->ex_numsecflavors++;
2226 * Parse the option string and update fields.
2227 * Option arguments may either be -<option>=<value> or
2231 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2232 int *has_hostp, int *exflagsp, struct xucred *cr)
2234 char *cpoptarg, *cpoptend;
2235 char *cp, *endcp, *cpopt, savedc, savedc2;
2236 int allflag, usedarg;
2244 while (cpopt && *cpopt) {
2247 if ((cpoptend = strchr(cpopt, ','))) {
2249 if ((cpoptarg = strchr(cpopt, '=')))
2252 if ((cpoptarg = strchr(cpopt, '=')))
2256 nextfield(&cp, &endcp);
2258 if (endcp > cp && *cp != '-') {
2266 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2267 *exflagsp |= MNT_EXRDONLY;
2268 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2269 !(allflag = strcmp(cpopt, "mapall")) ||
2270 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2272 parsecred(cpoptarg, cr);
2274 *exflagsp |= MNT_EXPORTANON;
2275 opt_flags |= OP_MAPALL;
2277 opt_flags |= OP_MAPROOT;
2278 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2279 !strcmp(cpopt, "m"))) {
2280 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2281 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2285 opt_flags |= OP_MASK;
2286 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2287 !strcmp(cpopt, "n"))) {
2288 if (strchr(cpoptarg, '/') != NULL) {
2290 fprintf(stderr, "setting OP_MASKLEN\n");
2291 opt_flags |= OP_MASKLEN;
2293 if (grp->gr_type != GT_NULL) {
2294 syslog(LOG_ERR, "network/host conflict");
2296 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2297 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2300 grp->gr_type = GT_NET;
2303 opt_flags |= OP_NET;
2304 } else if (!strcmp(cpopt, "alldirs")) {
2305 opt_flags |= OP_ALLDIRS;
2306 } else if (!strcmp(cpopt, "public")) {
2307 *exflagsp |= MNT_EXPUBLIC;
2308 } else if (!strcmp(cpopt, "webnfs")) {
2309 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2310 opt_flags |= OP_MAPALL;
2311 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2312 ep->ex_indexfile = strdup(cpoptarg);
2313 } else if (!strcmp(cpopt, "quiet")) {
2314 opt_flags |= OP_QUIET;
2315 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2316 if (parsesec(cpoptarg, ep))
2318 opt_flags |= OP_SEC;
2321 syslog(LOG_ERR, "bad opt %s", cpopt);
2340 * Translate a character string to the corresponding list of network
2341 * addresses for a hostname.
2344 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2346 struct grouplist *checkgrp;
2347 struct addrinfo *ai, *tai, hints;
2349 char host[NI_MAXHOST];
2351 if (grp->gr_type != GT_NULL) {
2352 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2355 memset(&hints, 0, sizeof hints);
2356 hints.ai_flags = AI_CANONNAME;
2357 hints.ai_protocol = IPPROTO_UDP;
2358 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2360 syslog(LOG_ERR,"can't get address info for host %s", cp);
2363 grp->gr_ptr.gt_addrinfo = ai;
2364 while (ai != NULL) {
2365 if (ai->ai_canonname == NULL) {
2366 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2367 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2368 strlcpy(host, "?", sizeof(host));
2369 ai->ai_canonname = strdup(host);
2370 ai->ai_flags |= AI_CANONNAME;
2373 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2375 * Sanity check: make sure we don't already have an entry
2376 * for this host in the grouplist.
2378 for (checkgrp = tgrp; checkgrp != NULL;
2379 checkgrp = checkgrp->gr_next) {
2380 if (checkgrp->gr_type != GT_HOST)
2382 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2383 tai = tai->ai_next) {
2384 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2388 "ignoring duplicate host %s\n",
2390 grp->gr_type = GT_IGNORE;
2396 grp->gr_type = GT_HOST;
2401 * Free up an exports list component
2404 free_exp(struct exportlist *ep)
2407 if (ep->ex_defdir) {
2408 free_host(ep->ex_defdir->dp_hosts);
2409 free((caddr_t)ep->ex_defdir);
2413 if (ep->ex_indexfile)
2414 free(ep->ex_indexfile);
2415 free_dir(ep->ex_dirl);
2423 free_host(struct hostlist *hp)
2425 struct hostlist *hp2;
2434 static struct hostlist *
2437 struct hostlist *hp;
2439 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2440 if (hp == (struct hostlist *)NULL)
2442 hp->ht_next = (struct hostlist *)NULL;
2448 * Out of memory, fatal
2454 syslog(LOG_ERR, "out of memory");
2459 * Do the nmount() syscall with the update flag to push the export info into
2463 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2464 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
2467 struct addrinfo *ai;
2468 struct export_args *eap;
2476 struct nfsex_args nfsea;
2478 eap = &nfsea.export;
2486 bzero(eap, sizeof (struct export_args));
2487 bzero(errmsg, sizeof(errmsg));
2488 eap->ex_flags = exflags;
2489 eap->ex_anon = *anoncrp;
2490 eap->ex_indexfile = ep->ex_indexfile;
2491 if (grp->gr_type == GT_HOST)
2492 ai = grp->gr_ptr.gt_addrinfo;
2495 eap->ex_numsecflavors = ep->ex_numsecflavors;
2496 for (i = 0; i < eap->ex_numsecflavors; i++)
2497 eap->ex_secflavors[i] = ep->ex_secflavors[i];
2498 if (eap->ex_numsecflavors == 0) {
2499 eap->ex_numsecflavors = 1;
2500 eap->ex_secflavors[0] = AUTH_SYS;
2504 if (v4root_phase == 0) {
2505 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2506 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2507 build_iovec(&iov, &iovlen, "from", NULL, 0);
2508 build_iovec(&iov, &iovlen, "update", NULL, 0);
2509 build_iovec(&iov, &iovlen, "export", eap,
2510 sizeof (struct export_args));
2511 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2515 switch (grp->gr_type) {
2517 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
2519 eap->ex_addr = ai->ai_addr;
2520 eap->ex_addrlen = ai->ai_addrlen;
2521 eap->ex_masklen = 0;
2524 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
2528 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2530 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2532 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2533 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
2536 eap->ex_addr = NULL;
2537 eap->ex_addrlen = 0;
2538 eap->ex_mask = NULL;
2539 eap->ex_masklen = 0;
2546 syslog(LOG_ERR, "bad grouptype");
2554 * For V4:, use the nfssvc() syscall, instead of mount().
2556 if (v4root_phase == 2) {
2557 nfsea.fspec = v4root_dirpath;
2558 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2559 syslog(LOG_ERR, "Exporting V4: failed");
2565 * Maybe I should just use the fsb->f_mntonname path
2566 * instead of looping back up the dirp to the mount
2568 * Also, needs to know how to export all types of local
2569 * exportable filesystems and not just "ufs".
2571 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2572 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2573 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2574 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2575 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2576 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2579 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2583 cp = dirp + dirplen - 1;
2584 if (opt_flags & OP_QUIET) {
2588 if (errno == EPERM) {
2590 warnx("can't change attributes for %s: %s",
2593 "can't change attributes for %s: %s",
2598 if (opt_flags & OP_ALLDIRS) {
2599 if (errno == EINVAL)
2601 "-alldirs requested but %s is not a filesystem mountpoint",
2605 "could not remount %s: %m",
2610 /* back up over the last component */
2611 while (*cp == '/' && cp > dirp)
2613 while (*(cp - 1) != '/' && cp > dirp)
2617 warnx("mnt unsucc");
2618 syslog(LOG_ERR, "can't export %s %s",
2626 * Check that we're still on the same
2629 if (statfs(dirp, &fsb1) != 0 ||
2630 bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2631 sizeof (fsb1.f_fsid)) != 0) {
2634 "can't export %s %s", dirp,
2643 * For the experimental server:
2644 * If this is the public directory, get the file handle
2645 * and load it into the kernel via the nfssvc() syscall.
2647 if ((exflags & MNT_EXPUBLIC) != 0) {
2651 if (eap->ex_indexfile != NULL)
2652 public_name = eap->ex_indexfile;
2655 if (getfh(public_name, &fh) < 0)
2657 "Can't get public fh for %s", public_name);
2658 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2660 "Can't set public fh for %s", public_name);
2673 /* free strings allocated by strdup() in getmntopts.c */
2675 free(iov[0].iov_base); /* fstype */
2676 free(iov[2].iov_base); /* fspath */
2677 free(iov[4].iov_base); /* from */
2678 free(iov[6].iov_base); /* update */
2679 free(iov[8].iov_base); /* export */
2680 free(iov[10].iov_base); /* errmsg */
2682 /* free iov, allocated by realloc() */
2689 * Translate a net address.
2691 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2694 get_net(char *cp, struct netmsk *net, int maskflg)
2696 struct netent *np = NULL;
2697 char *name, *p, *prefp;
2698 struct sockaddr_in sin;
2699 struct sockaddr *sa = NULL;
2700 struct addrinfo hints, *ai = NULL;
2701 char netname[NI_MAXHOST];
2705 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2706 p = strchr(cp, '/');
2712 * Check for a numeric address first. We wish to avoid
2713 * possible DNS lookups in getnetbyname().
2715 if (isxdigit(*cp) || *cp == ':') {
2716 memset(&hints, 0, sizeof hints);
2717 /* Ensure the mask and the network have the same family. */
2718 if (maskflg && (opt_flags & OP_NET))
2719 hints.ai_family = net->nt_net.ss_family;
2720 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2721 hints.ai_family = net->nt_mask.ss_family;
2723 hints.ai_family = AF_UNSPEC;
2724 hints.ai_flags = AI_NUMERICHOST;
2725 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2727 if (sa != NULL && ai->ai_family == AF_INET) {
2729 * The address in `cp' is really a network address, so
2730 * use inet_network() to re-interpret this correctly.
2731 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2733 bzero(&sin, sizeof sin);
2734 sin.sin_family = AF_INET;
2735 sin.sin_len = sizeof sin;
2736 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2738 fprintf(stderr, "get_net: v4 addr %s\n",
2739 inet_ntoa(sin.sin_addr));
2740 sa = (struct sockaddr *)&sin;
2743 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2744 bzero(&sin, sizeof sin);
2745 sin.sin_family = AF_INET;
2746 sin.sin_len = sizeof sin;
2747 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2748 sa = (struct sockaddr *)&sin;
2754 /* The specified sockaddr is a mask. */
2755 if (checkmask(sa) != 0)
2757 bcopy(sa, &net->nt_mask, sa->sa_len);
2758 opt_flags |= OP_HAVEMASK;
2760 /* The specified sockaddr is a network address. */
2761 bcopy(sa, &net->nt_net, sa->sa_len);
2763 /* Get a network name for the export list. */
2766 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2767 NULL, 0, NI_NUMERICHOST) == 0) {
2772 if ((net->nt_name = strdup(name)) == NULL)
2776 * Extract a mask from either a "/<masklen>" suffix, or
2777 * from the class of an IPv4 address.
2779 if (opt_flags & OP_MASKLEN) {
2780 preflen = strtol(prefp, NULL, 10);
2781 if (preflen < 0L || preflen == LONG_MAX)
2783 bcopy(sa, &net->nt_mask, sa->sa_len);
2784 if (makemask(&net->nt_mask, (int)preflen) != 0)
2786 opt_flags |= OP_HAVEMASK;
2788 } else if (sa->sa_family == AF_INET &&
2789 (opt_flags & OP_MASK) == 0) {
2792 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2793 if (IN_CLASSA(addr))
2795 else if (IN_CLASSB(addr))
2797 else if (IN_CLASSC(addr))
2799 else if (IN_CLASSD(addr))
2802 preflen = 32; /* XXX */
2804 bcopy(sa, &net->nt_mask, sa->sa_len);
2805 makemask(&net->nt_mask, (int)preflen);
2806 opt_flags |= OP_HAVEMASK;
2821 * Parse out the next white space separated field
2824 nextfield(char **cp, char **endcp)
2829 while (*p == ' ' || *p == '\t')
2831 if (*p == '\n' || *p == '\0')
2835 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2842 * Get an exports file line. Skip over blank lines and handle line
2850 int totlen, cont_line;
2853 * Loop around ignoring blank lines and getting all continuation lines.
2858 if ((p = fgetln(exp_file, &len)) == NULL)
2863 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2873 if (linesize < len + totlen + 1) {
2874 linesize = len + totlen + 1;
2875 line = realloc(line, linesize);
2879 memcpy(line + totlen, p, len);
2881 line[totlen] = '\0';
2882 } while (totlen == 0 || cont_line);
2887 * Parse a description of a credential.
2890 parsecred(char *namelist, struct xucred *cr)
2897 gid_t groups[XU_NGROUPS + 1];
2900 cr->cr_version = XUCRED_VERSION;
2902 * Set up the unprivileged user.
2905 cr->cr_groups[0] = 65533;
2908 * Get the user's password table entry.
2910 names = strsep_quote(&namelist, " \t\n");
2911 name = strsep(&names, ":");
2912 /* Bug? name could be NULL here */
2913 if (isdigit(*name) || *name == '-')
2914 pw = getpwuid(atoi(name));
2916 pw = getpwnam(name);
2918 * Credentials specified as those of a user.
2920 if (names == NULL) {
2922 syslog(LOG_ERR, "unknown user: %s", name);
2925 cr->cr_uid = pw->pw_uid;
2926 ngroups = XU_NGROUPS + 1;
2927 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) {
2928 syslog(LOG_ERR, "too many groups");
2929 ngroups = XU_NGROUPS + 1;
2933 * Compress out duplicate.
2935 cr->cr_ngroups = ngroups - 1;
2936 cr->cr_groups[0] = groups[0];
2937 for (cnt = 2; cnt < ngroups; cnt++)
2938 cr->cr_groups[cnt - 1] = groups[cnt];
2942 * Explicit credential specified as a colon separated list:
2946 cr->cr_uid = pw->pw_uid;
2947 else if (isdigit(*name) || *name == '-')
2948 cr->cr_uid = atoi(name);
2950 syslog(LOG_ERR, "unknown user: %s", name);
2954 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
2955 name = strsep(&names, ":");
2956 if (isdigit(*name) || *name == '-') {
2957 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2959 if ((gr = getgrnam(name)) == NULL) {
2960 syslog(LOG_ERR, "unknown group: %s", name);
2963 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2966 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
2967 syslog(LOG_ERR, "too many groups");
2970 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
2972 * Routines that maintain the remote mounttab
2977 struct mountlist *mlp;
2978 char *host, *dirp, *cp;
2982 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2983 if (errno == ENOENT)
2986 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2990 while (fgets(str, STRSIZ, mlfile) != NULL) {
2992 host = strsep(&cp, " \t\n");
2993 dirp = strsep(&cp, " \t\n");
2994 if (host == NULL || dirp == NULL)
2996 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2997 if (mlp == (struct mountlist *)NULL)
2999 strncpy(mlp->ml_host, host, MNTNAMLEN);
3000 mlp->ml_host[MNTNAMLEN] = '\0';
3001 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3002 mlp->ml_dirp[MNTPATHLEN] = '\0';
3004 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3010 del_mlist(char *hostp, char *dirp)
3012 struct mountlist *mlp, *mlp2;
3016 SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
3017 if (!strcmp(mlp->ml_host, hostp) &&
3018 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3020 SLIST_REMOVE(&mlhead, mlp, mountlist, next);
3025 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3026 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3029 SLIST_FOREACH(mlp, &mlhead, next) {
3030 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3037 add_mlist(char *hostp, char *dirp)
3039 struct mountlist *mlp;
3042 SLIST_FOREACH(mlp, &mlhead, next) {
3043 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3047 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3048 if (mlp == (struct mountlist *)NULL)
3050 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3051 mlp->ml_host[MNTNAMLEN] = '\0';
3052 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3053 mlp->ml_dirp[MNTPATHLEN] = '\0';
3054 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3055 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3056 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3059 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3064 * Free up a group list.
3067 free_grp(struct grouplist *grp)
3069 if (grp->gr_type == GT_HOST) {
3070 if (grp->gr_ptr.gt_addrinfo != NULL)
3071 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3072 } else if (grp->gr_type == GT_NET) {
3073 if (grp->gr_ptr.gt_net.nt_name)
3074 free(grp->gr_ptr.gt_net.nt_name);
3081 SYSLOG(int pri, const char *fmt, ...)
3086 vfprintf(stderr, fmt, ap);
3092 * Check options for consistency.
3095 check_options(struct dirlist *dp)
3098 if (v4root_phase == 0 && dp == NULL)
3100 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3101 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3104 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3105 syslog(LOG_ERR, "-mask requires -network");
3108 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3109 syslog(LOG_ERR, "-network requires mask specification");
3112 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3113 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3116 if (v4root_phase > 0 &&
3118 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3119 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3122 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3123 syslog(LOG_ERR, "-alldirs has multiple directories");
3130 * Check an absolute directory path for any symbolic links. Return true
3133 check_dirpath(char *dirp)
3140 while (*cp && ret) {
3143 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3149 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3155 * Make a netmask according to the specified prefix length. The ss_family
3156 * and other non-address fields must be initialised before calling this.
3159 makemask(struct sockaddr_storage *ssp, int bitlen)
3164 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3166 if (bitlen > len * CHAR_BIT)
3169 for (i = 0; i < len; i++) {
3170 bits = MIN(CHAR_BIT, bitlen);
3171 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3178 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3179 * is acceptable (i.e. of the form 1...10....0).
3182 checkmask(struct sockaddr *sa)
3187 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3190 for (i = 0; i < len; i++)
3191 if (mask[i] != 0xff)
3194 if (~mask[i] & (u_char)(~mask[i] + 1))
3198 for (; i < len; i++)
3205 * Compare two sockaddrs according to a specified mask. Return zero if
3206 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3207 * If samask is NULL, perform a full comparison.
3210 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3212 unsigned char *p1, *p2, *mask;
3215 if (sa1->sa_family != sa2->sa_family ||
3216 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3217 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3220 switch (sa1->sa_family) {
3222 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3223 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3228 /* Simple binary comparison if no mask specified. */
3230 return (memcmp(p1, p2, len));
3232 /* Set up the mask, and do a mask-based comparison. */
3233 if (sa1->sa_family != samask->sa_family ||
3234 (mask = sa_rawaddr(samask, NULL)) == NULL)
3237 for (i = 0; i < len; i++)
3238 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3244 * Return a pointer to the part of the sockaddr that contains the
3245 * raw address, and set *nbytes to its length in bytes. Returns
3246 * NULL if the address family is unknown.
3249 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3253 switch (sa->sa_family) {
3255 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3256 p = &((struct sockaddr_in *)sa)->sin_addr;
3259 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3260 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3273 huphandler(int sig __unused)
3280 terminate(int sig __unused)
3282 pidfile_remove(pfh);
3283 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3284 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);