2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Herb Hasler and Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/fcntl.h>
50 #include <sys/linker.h>
51 #include <sys/module.h>
52 #include <sys/mount.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
58 #include <rpc/rpc_com.h>
59 #include <rpc/pmap_clnt.h>
60 #include <rpc/pmap_prot.h>
61 #include <rpcsvc/mount.h>
62 #include <nfs/nfsproto.h>
63 #include <nfs/nfssvc.h>
64 #include <nfsserver/nfs.h>
66 #include <fs/nfs/nfsport.h>
68 #include <arpa/inet.h>
83 #include "pathnames.h"
91 * Structures for keeping the mount list and export list
94 struct mountlist *ml_next;
95 char ml_host[MNTNAMLEN+1];
96 char ml_dirp[MNTPATHLEN+1];
100 struct dirlist *dp_left;
101 struct dirlist *dp_right;
103 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
107 #define DP_DEFSET 0x1
108 #define DP_HOSTSET 0x2
111 struct exportlist *ex_next;
112 struct dirlist *ex_dirl;
113 struct dirlist *ex_defdir;
118 int ex_numsecflavors;
119 int ex_secflavors[MAXSECFLAVORS];
120 int ex_defnumsecflavors;
121 int ex_defsecflavors[MAXSECFLAVORS];
124 #define EX_LINKED 0x1
127 struct sockaddr_storage nt_net;
128 struct sockaddr_storage nt_mask;
133 struct addrinfo *gt_addrinfo;
134 struct netmsk gt_net;
139 union grouptypes gr_ptr;
140 struct grouplist *gr_next;
141 int gr_numsecflavors;
142 int gr_secflavors[MAXSECFLAVORS];
148 #define GT_DEFAULT 0x3
149 #define GT_IGNORE 0x5
152 int ht_flag; /* Uses DP_xx bits */
153 struct grouplist *ht_grp;
154 struct hostlist *ht_next;
161 int fhr_numsecflavors;
165 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
168 static char *add_expdir(struct dirlist **, char *, int);
169 static void add_dlist(struct dirlist **, struct dirlist *,
170 struct grouplist *, int, struct exportlist *);
171 static void add_mlist(char *, char *);
172 static int check_dirpath(char *);
173 static int check_options(struct dirlist *);
174 static int checkmask(struct sockaddr *sa);
175 static int chk_host(struct dirlist *, struct sockaddr *, int *, int *,
177 static char *strsep_quote(char **stringp, const char *delim);
178 static int create_service(struct netconfig *nconf);
179 static void complete_service(struct netconfig *nconf, char *port_str);
180 static void clearout_service(void);
181 static void del_mlist(char *hostp, char *dirp);
182 static struct dirlist *dirp_search(struct dirlist *, char *);
183 static int do_mount(struct exportlist *, struct grouplist *, int,
184 struct xucred *, char *, int, struct statfs *);
185 static int do_opt(char **, char **, struct exportlist *,
186 struct grouplist *, int *, int *, struct xucred *);
187 static struct exportlist *ex_search(fsid_t *);
188 static struct exportlist *get_exp(void);
189 static void free_dir(struct dirlist *);
190 static void free_exp(struct exportlist *);
191 static void free_grp(struct grouplist *);
192 static void free_host(struct hostlist *);
193 static void get_exportlist(void);
194 static int get_host(char *, struct grouplist *, struct grouplist *);
195 static struct hostlist *get_ht(void);
196 static int get_line(void);
197 static void get_mountlist(void);
198 static int get_net(char *, struct netmsk *, int);
199 static void getexp_err(struct exportlist *, struct grouplist *);
200 static struct grouplist *get_grp(void);
201 static void hang_dirp(struct dirlist *, struct grouplist *,
202 struct exportlist *, int);
203 static void huphandler(int sig);
204 static int makemask(struct sockaddr_storage *ssp, int bitlen);
205 static void mntsrv(struct svc_req *, SVCXPRT *);
206 static void nextfield(char **, char **);
207 static void out_of_mem(void);
208 static void parsecred(char *, struct xucred *);
209 static int parsesec(char *, struct exportlist *);
210 static int put_exlist(struct dirlist *, XDR *, struct dirlist *,
212 static void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
213 static int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
214 struct sockaddr *samask);
215 static int scan_tree(struct dirlist *, struct sockaddr *);
216 static void usage(void);
217 static int xdr_dir(XDR *, char *);
218 static int xdr_explist(XDR *, caddr_t);
219 static int xdr_explist_brief(XDR *, caddr_t);
220 static int xdr_explist_common(XDR *, caddr_t, int);
221 static int xdr_fhs(XDR *, caddr_t);
222 static int xdr_mlist(XDR *, caddr_t);
223 static void terminate(int);
225 static struct exportlist *exphead;
226 static struct mountlist *mlhead;
227 static struct grouplist *grphead;
228 static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
229 static char **exnames;
230 static char **hosts = NULL;
231 static struct xucred def_anon = {
238 static int force_v2 = 0;
239 static int resvport_only = 1;
240 static int nhosts = 0;
241 static int dir_only = 1;
242 static int dolog = 0;
243 static int got_sighup = 0;
244 static int xcreated = 0;
246 static char *svcport_str = NULL;
247 static int mallocd_svcport = 0;
249 static int sock_fdcnt;
250 static int sock_fdpos;
251 static int suspend_nfsd = 0;
253 static int opt_flags;
254 static int have_v6 = 1;
256 static int v4root_phase = 0;
257 static char v4root_dirpath[PATH_MAX + 1];
258 static int has_publicfh = 0;
260 static struct pidfh *pfh = NULL;
261 /* Bits for opt_flags above */
262 #define OP_MAPROOT 0x01
263 #define OP_MAPALL 0x02
267 #define OP_ALLDIRS 0x40
268 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
269 #define OP_QUIET 0x100
270 #define OP_MASKLEN 0x200
274 static int debug = 1;
275 static void SYSLOG(int, const char *, ...) __printflike(2, 3);
276 #define syslog SYSLOG
278 static int debug = 0;
282 * Similar to strsep(), but it allows for quoted strings
283 * and escaped characters.
285 * It returns the string (or NULL, if *stringp is NULL),
286 * which is a de-quoted version of the string if necessary.
288 * It modifies *stringp in place.
291 strsep_quote(char **stringp, const char *delim)
293 char *srcptr, *dstptr, *retval;
296 if (stringp == NULL || *stringp == NULL)
299 srcptr = dstptr = retval = *stringp;
303 * We're looking for several edge cases here.
304 * First: if we're in quote state (quot != 0),
305 * then we ignore the delim characters, but otherwise
306 * process as normal, unless it is the quote character.
307 * Second: if the current character is a backslash,
308 * we take the next character as-is, without checking
309 * for delim, quote, or backslash. Exception: if the
310 * next character is a NUL, that's the end of the string.
311 * Third: if the character is a quote character, we toggle
313 * Otherwise: check the current character for NUL, or
314 * being in delim, and end the string if either is true.
316 if (*srcptr == '\\') {
319 * The edge case here is if the next character
320 * is NUL, we want to stop processing. But if
321 * it's not NUL, then we simply want to copy it.
324 *dstptr++ = *srcptr++;
328 if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
332 if (quot && *srcptr == quot) {
333 /* End of the quoted part */
338 if (!quot && strchr(delim, *srcptr))
340 *dstptr++ = *srcptr++;
343 *dstptr = 0; /* Terminate the string */
344 *stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
349 * Mountd server for NFS mount protocol as described in:
350 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
351 * The optional arguments are the exports file name
352 * default: _PATH_EXPORTS
353 * and "-n" to allow nonroot mount.
356 main(int argc, char **argv)
359 struct netconfig *nconf;
360 char *endptr, **hosts_bak;
365 int maxrec = RPC_MAXDATASIZE;
366 int attempt_cnt, port_len, port_pos, ret;
369 /* Check that another mountd isn't already running. */
370 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
373 errx(1, "mountd already running, pid: %d.", otherpid);
374 warn("cannot open or create pidfile");
377 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
383 while ((c = getopt(argc, argv, "2deh:lnp:rS")) != -1)
389 /* now a no-op, since this is the default */
398 debug = debug ? 0 : 1;
405 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
406 if (endptr == NULL || *endptr != '\0' ||
407 svcport == 0 || svcport >= IPPORT_MAX)
409 svcport_str = strdup(optarg);
414 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
415 if (hosts_bak == NULL) {
417 for (k = 0; k < nhosts; k++)
424 hosts[nhosts - 1] = strdup(optarg);
425 if (hosts[nhosts - 1] == NULL) {
426 for (k = 0; k < (nhosts - 1); k++)
439 if (modfind("nfsd") < 0) {
440 /* Not present in kernel, try loading it */
441 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
442 errx(1, "NFS server is not available");
447 grphead = (struct grouplist *)NULL;
448 exphead = (struct exportlist *)NULL;
449 mlhead = (struct mountlist *)NULL;
453 exnames = exnames_default;
454 openlog("mountd", LOG_PID, LOG_DAEMON);
456 warnx("getting export list");
459 warnx("getting mount list");
465 signal(SIGINT, SIG_IGN);
466 signal(SIGQUIT, SIG_IGN);
468 signal(SIGHUP, huphandler);
469 signal(SIGTERM, terminate);
470 signal(SIGPIPE, SIG_IGN);
474 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
475 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
476 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
478 if (!resvport_only) {
479 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
480 &resvport_only, sizeof(resvport_only)) != 0 &&
482 syslog(LOG_ERR, "sysctl: %m");
488 * If no hosts were specified, add a wildcard entry to bind to
489 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
493 hosts = malloc(sizeof(char *));
501 hosts_bak = realloc(hosts, (nhosts + 2) *
503 if (hosts_bak == NULL) {
504 for (k = 0; k < nhosts; k++)
511 hosts[nhosts - 2] = "::1";
513 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
514 if (hosts_bak == NULL) {
515 for (k = 0; k < nhosts; k++)
525 hosts[nhosts - 1] = "127.0.0.1";
533 nc_handle = setnetconfig();
534 while ((nconf = getnetconfig(nc_handle))) {
535 if (nconf->nc_flag & NC_VISIBLE) {
536 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
540 ret = create_service(nconf);
542 /* Ignore this call */
546 * Failed to bind port, so close off
547 * all sockets created and try again
548 * if the port# was dynamically
549 * assigned via bind(2).
552 if (mallocd_svcport != 0 &&
553 attempt_cnt < GETPORT_MAXTRY) {
560 "bindresvport_sa: %m");
564 /* Start over at the first service. */
568 nc_handle = setnetconfig();
570 } else if (mallocd_svcport != 0 &&
571 attempt_cnt == GETPORT_MAXTRY) {
573 * For the last attempt, allow
574 * different port #s for each nconf
575 * by saving the svcport_str and
576 * setting it back to NULL.
578 port_list = realloc(port_list,
579 (port_len + 1) * sizeof(char *));
580 if (port_list == NULL)
582 port_list[port_len++] = svcport_str;
591 * Successfully bound the ports, so call complete_service() to
592 * do the rest of the setup on the service(s).
596 nc_handle = setnetconfig();
597 while ((nconf = getnetconfig(nc_handle))) {
598 if (nconf->nc_flag & NC_VISIBLE) {
599 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
602 } else if (port_list != NULL) {
603 if (port_pos >= port_len) {
604 syslog(LOG_ERR, "too many port#s");
607 complete_service(nconf, port_list[port_pos++]);
609 complete_service(nconf, svcport_str);
612 endnetconfig(nc_handle);
614 if (port_list != NULL) {
615 for (port_pos = 0; port_pos < port_len; port_pos++)
616 free(port_list[port_pos]);
621 syslog(LOG_ERR, "could not create any services");
625 /* Expand svc_run() here so that we can call get_exportlist(). */
632 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
636 syslog(LOG_ERR, "mountd died: select: %m");
641 svc_getreqset(&readfds);
647 * This routine creates and binds sockets on the appropriate
648 * addresses. It gets called one time for each transport.
649 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
650 * bind failed with EADDRINUSE.
651 * Any file descriptors that have been created are stored in sock_fd and
652 * the total count of them is maintained in sock_fdcnt.
655 create_service(struct netconfig *nconf)
657 struct addrinfo hints, *res = NULL;
658 struct sockaddr_in *sin;
659 struct sockaddr_in6 *sin6;
660 struct __rpc_sockinfo si;
666 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
669 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
670 (nconf->nc_semantics != NC_TPI_COTS) &&
671 (nconf->nc_semantics != NC_TPI_COTS_ORD))
672 return (1); /* not my type */
675 * XXX - using RPC library internal functions.
677 if (!__rpc_nconf2sockinfo(nconf, &si)) {
678 syslog(LOG_ERR, "cannot get information for %s",
683 /* Get mountd's address on this transport */
684 memset(&hints, 0, sizeof hints);
685 hints.ai_family = si.si_af;
686 hints.ai_socktype = si.si_socktype;
687 hints.ai_protocol = si.si_proto;
690 * Bind to specific IPs if asked to
693 while (nhostsbak > 0) {
695 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
698 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
701 hints.ai_flags = AI_PASSIVE;
704 * XXX - using RPC library internal functions.
706 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
708 if (errno == EAFNOSUPPORT &&
709 nconf->nc_semantics != NC_TPI_CLTS)
712 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
713 "cannot create socket for %s", nconf->nc_netid);
719 switch (hints.ai_family) {
721 if (inet_pton(AF_INET, hosts[nhostsbak],
723 hints.ai_flags |= AI_NUMERICHOST;
726 * Skip if we have an AF_INET6 address.
728 if (inet_pton(AF_INET6, hosts[nhostsbak],
736 if (inet_pton(AF_INET6, hosts[nhostsbak],
738 hints.ai_flags |= AI_NUMERICHOST;
741 * Skip if we have an AF_INET address.
743 if (inet_pton(AF_INET, hosts[nhostsbak],
751 * We're doing host-based access checks here, so don't
752 * allow v4-in-v6 to confuse things. The kernel will
753 * disable it by default on NFS sockets too.
755 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
758 "can't disable v4-in-v6 on IPv6 socket");
767 * If no hosts were specified, just bind to INADDR_ANY
769 if (strcmp("*", hosts[nhostsbak]) == 0) {
770 if (svcport_str == NULL) {
771 res = malloc(sizeof(struct addrinfo));
775 res->ai_flags = hints.ai_flags;
776 res->ai_family = hints.ai_family;
777 res->ai_protocol = hints.ai_protocol;
778 switch (res->ai_family) {
780 sin = malloc(sizeof(struct sockaddr_in));
783 sin->sin_family = AF_INET;
784 sin->sin_port = htons(0);
785 sin->sin_addr.s_addr = htonl(INADDR_ANY);
786 res->ai_addr = (struct sockaddr*) sin;
787 res->ai_addrlen = (socklen_t)
788 sizeof(struct sockaddr_in);
791 sin6 = malloc(sizeof(struct sockaddr_in6));
794 sin6->sin6_family = AF_INET6;
795 sin6->sin6_port = htons(0);
796 sin6->sin6_addr = in6addr_any;
797 res->ai_addr = (struct sockaddr*) sin6;
798 res->ai_addrlen = (socklen_t)
799 sizeof(struct sockaddr_in6);
802 syslog(LOG_ERR, "bad addr fam %d",
807 if ((aicode = getaddrinfo(NULL, svcport_str,
808 &hints, &res)) != 0) {
810 "cannot get local address for %s: %s",
812 gai_strerror(aicode));
818 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
819 &hints, &res)) != 0) {
821 "cannot get local address for %s: %s",
822 nconf->nc_netid, gai_strerror(aicode));
829 sock_fd[sock_fdcnt - 1] = fd;
831 /* Now, attempt the bind. */
832 r = bindresvport_sa(fd, res->ai_addr);
834 if (errno == EADDRINUSE && mallocd_svcport != 0) {
835 if (mallocd_res != 0) {
842 syslog(LOG_ERR, "bindresvport_sa: %m");
846 if (svcport_str == NULL) {
847 svcport_str = malloc(NI_MAXSERV * sizeof(char));
848 if (svcport_str == NULL)
852 if (getnameinfo(res->ai_addr,
853 res->ai_addr->sa_len, NULL, NI_MAXHOST,
854 svcport_str, NI_MAXSERV * sizeof(char),
855 NI_NUMERICHOST | NI_NUMERICSERV))
856 errx(1, "Cannot get port number");
858 if (mallocd_res != 0) {
869 * Called after all the create_service() calls have succeeded, to complete
870 * the setup and registration.
873 complete_service(struct netconfig *nconf, char *port_str)
875 struct addrinfo hints, *res = NULL;
876 struct __rpc_sockinfo si;
877 struct netbuf servaddr;
878 SVCXPRT *transp = NULL;
879 int aicode, fd, nhostsbak;
882 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
883 (nconf->nc_semantics != NC_TPI_COTS) &&
884 (nconf->nc_semantics != NC_TPI_COTS_ORD))
885 return; /* not my type */
888 * XXX - using RPC library internal functions.
890 if (!__rpc_nconf2sockinfo(nconf, &si)) {
891 syslog(LOG_ERR, "cannot get information for %s",
897 while (nhostsbak > 0) {
899 if (sock_fdpos >= sock_fdcnt) {
900 /* Should never happen. */
901 syslog(LOG_ERR, "Ran out of socket fd's");
904 fd = sock_fd[sock_fdpos++];
908 if (nconf->nc_semantics != NC_TPI_CLTS)
909 listen(fd, SOMAXCONN);
911 if (nconf->nc_semantics == NC_TPI_CLTS )
912 transp = svc_dg_create(fd, 0, 0);
914 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
917 if (transp != (SVCXPRT *) NULL) {
918 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
921 "can't register %s MOUNTVERS service",
924 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
927 "can't register %s MOUNTVERS3 service",
931 syslog(LOG_WARNING, "can't create %s services",
934 if (registered == 0) {
936 memset(&hints, 0, sizeof hints);
937 hints.ai_flags = AI_PASSIVE;
938 hints.ai_family = si.si_af;
939 hints.ai_socktype = si.si_socktype;
940 hints.ai_protocol = si.si_proto;
942 if ((aicode = getaddrinfo(NULL, port_str, &hints,
944 syslog(LOG_ERR, "cannot get local address: %s",
945 gai_strerror(aicode));
949 servaddr.buf = malloc(res->ai_addrlen);
950 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
951 servaddr.len = res->ai_addrlen;
953 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
954 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
963 * Clear out sockets after a failure to bind one of them, so that the
964 * cycle of socket creation/binding can start anew.
967 clearout_service(void)
971 for (i = 0; i < sock_fdcnt; i++) {
972 if (sock_fd[i] >= 0) {
973 shutdown(sock_fd[i], SHUT_RDWR);
983 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
984 "[-S] [-h <bindip>] [export_file ...]\n");
989 * The mount rpc service
992 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
994 struct exportlist *ep;
999 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1000 int lookup_failed = 1;
1001 struct sockaddr *saddr;
1003 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1004 int bad = 0, defset, hostset;
1005 sigset_t sighup_mask;
1006 int numsecflavors, *secflavorsp;
1008 sigemptyset(&sighup_mask);
1009 sigaddset(&sighup_mask, SIGHUP);
1010 saddr = svc_getrpccaller(transp)->buf;
1011 switch (saddr->sa_family) {
1013 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1016 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1019 syslog(LOG_ERR, "request from unknown address family");
1022 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
1024 getnameinfo(saddr, saddr->sa_len, numerichost,
1025 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1026 switch (rqstp->rq_proc) {
1028 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1029 syslog(LOG_ERR, "can't send reply");
1032 if (sport >= IPPORT_RESERVED && resvport_only) {
1034 "mount request from %s from unprivileged port",
1036 svcerr_weakauth(transp);
1039 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1040 syslog(LOG_NOTICE, "undecodable mount request from %s",
1042 svcerr_decode(transp);
1047 * Get the real pathname and make sure it is a directory
1048 * or a regular file if the -r option was specified
1051 if (realpath(rpcpath, dirpath) == NULL ||
1052 stat(dirpath, &stb) < 0 ||
1053 (!S_ISDIR(stb.st_mode) &&
1054 (dir_only || !S_ISREG(stb.st_mode))) ||
1055 statfs(dirpath, &fsb) < 0) {
1056 chdir("/"); /* Just in case realpath doesn't */
1058 "mount request from %s for non existent path %s",
1059 numerichost, dirpath);
1061 warnx("stat failed on %s", dirpath);
1062 bad = ENOENT; /* We will send error reply later */
1065 /* Check in the exports list */
1066 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1067 ep = ex_search(&fsb.f_fsid);
1068 hostset = defset = 0;
1069 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1070 &numsecflavors, &secflavorsp) ||
1071 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1072 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1074 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1075 scan_tree(ep->ex_dirl, saddr) == 0))) {
1077 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1079 syslog(LOG_ERR, "can't send reply");
1080 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1083 if (hostset & DP_HOSTSET) {
1084 fhr.fhr_flag = hostset;
1085 fhr.fhr_numsecflavors = numsecflavors;
1086 fhr.fhr_secflavors = secflavorsp;
1088 fhr.fhr_flag = defset;
1089 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1090 fhr.fhr_secflavors = ep->ex_defsecflavors;
1092 fhr.fhr_vers = rqstp->rq_vers;
1093 /* Get the file handle */
1094 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1095 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1097 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1098 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1100 syslog(LOG_ERR, "can't send reply");
1101 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1104 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1106 syslog(LOG_ERR, "can't send reply");
1108 add_mlist(host, dirpath);
1110 add_mlist(numerichost, dirpath);
1112 warnx("mount successful");
1115 "mount request succeeded from %s for %s",
1116 numerichost, dirpath);
1120 "mount request denied from %s for %s",
1121 numerichost, dirpath);
1124 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1126 syslog(LOG_ERR, "can't send reply");
1127 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1129 case MOUNTPROC_DUMP:
1130 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1131 syslog(LOG_ERR, "can't send reply");
1134 "dump request succeeded from %s",
1137 case MOUNTPROC_UMNT:
1138 if (sport >= IPPORT_RESERVED && resvport_only) {
1140 "umount request from %s from unprivileged port",
1142 svcerr_weakauth(transp);
1145 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1146 syslog(LOG_NOTICE, "undecodable umount request from %s",
1148 svcerr_decode(transp);
1151 if (realpath(rpcpath, dirpath) == NULL) {
1152 syslog(LOG_NOTICE, "umount request from %s "
1153 "for non existent path %s",
1154 numerichost, dirpath);
1156 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1157 syslog(LOG_ERR, "can't send reply");
1159 del_mlist(host, dirpath);
1160 del_mlist(numerichost, dirpath);
1163 "umount request succeeded from %s for %s",
1164 numerichost, dirpath);
1166 case MOUNTPROC_UMNTALL:
1167 if (sport >= IPPORT_RESERVED && resvport_only) {
1169 "umountall request from %s from unprivileged port",
1171 svcerr_weakauth(transp);
1174 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1175 syslog(LOG_ERR, "can't send reply");
1177 del_mlist(host, NULL);
1178 del_mlist(numerichost, NULL);
1181 "umountall request succeeded from %s",
1184 case MOUNTPROC_EXPORT:
1185 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1186 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1188 syslog(LOG_ERR, "can't send reply");
1191 "export request succeeded from %s",
1195 svcerr_noproc(transp);
1201 * Xdr conversion for a dirpath string
1204 xdr_dir(XDR *xdrsp, char *dirp)
1206 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1210 * Xdr routine to generate file handle reply
1213 xdr_fhs(XDR *xdrsp, caddr_t cp)
1215 struct fhreturn *fhrp = (struct fhreturn *)cp;
1216 u_long ok = 0, len, auth;
1219 if (!xdr_long(xdrsp, &ok))
1221 switch (fhrp->fhr_vers) {
1223 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1226 if (!xdr_long(xdrsp, &len))
1228 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1230 if (fhrp->fhr_numsecflavors) {
1231 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1233 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1234 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1240 if (!xdr_long(xdrsp, &len))
1242 return (xdr_long(xdrsp, &auth));
1249 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1251 struct mountlist *mlp;
1258 if (!xdr_bool(xdrsp, &true))
1260 strp = &mlp->ml_host[0];
1261 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1263 strp = &mlp->ml_dirp[0];
1264 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1268 if (!xdr_bool(xdrsp, &false))
1274 * Xdr conversion for export list
1277 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1279 struct exportlist *ep;
1282 sigset_t sighup_mask;
1284 sigemptyset(&sighup_mask);
1285 sigaddset(&sighup_mask, SIGHUP);
1286 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1290 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1293 if (ep->ex_defdir && putdef == 0 &&
1294 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1299 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1300 if (!xdr_bool(xdrsp, &false))
1304 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1309 * Called from xdr_explist() to traverse the tree and export the
1313 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1316 struct grouplist *grp;
1317 struct hostlist *hp;
1324 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1326 if (!xdr_bool(xdrsp, &true))
1329 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1331 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1336 if (!xdr_bool(xdrsp, &true))
1339 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1341 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1342 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1346 if (grp->gr_type == GT_HOST) {
1347 if (!xdr_bool(xdrsp, &true))
1349 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1350 if (!xdr_string(xdrsp, &strp,
1353 } else if (grp->gr_type == GT_NET) {
1354 if (!xdr_bool(xdrsp, &true))
1356 strp = grp->gr_ptr.gt_net.nt_name;
1357 if (!xdr_string(xdrsp, &strp,
1362 if (gotalldir && hp == (struct hostlist *)NULL) {
1368 if (!xdr_bool(xdrsp, &false))
1370 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1377 xdr_explist(XDR *xdrsp, caddr_t cp)
1380 return xdr_explist_common(xdrsp, cp, 0);
1384 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1387 return xdr_explist_common(xdrsp, cp, 1);
1391 static size_t linesize;
1392 static FILE *exp_file;
1395 * Get the export list from one, currently open file
1398 get_exportlist_one(void)
1400 struct exportlist *ep, *ep2;
1401 struct grouplist *grp, *tgrp;
1402 struct exportlist **epp;
1403 struct dirlist *dirhead;
1406 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1407 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1410 dirhead = (struct dirlist *)NULL;
1411 while (get_line()) {
1413 warnx("got line %s", line);
1415 nextfield(&cp, &endcp);
1424 exflags = MNT_EXPORTED;
1427 ep = (struct exportlist *)NULL;
1431 * Handle the V4 root dir.
1433 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1435 * V4: just indicates that it is the v4 root point,
1436 * so skip over that and set v4root_phase.
1438 if (v4root_phase > 0) {
1439 syslog(LOG_ERR, "V4:duplicate line, ignored");
1444 nextfield(&cp, &endcp);
1448 * Create new exports list entry
1451 tgrp = grp = get_grp();
1453 if (len > MNTNAMLEN) {
1454 getexp_err(ep, tgrp);
1458 if (ep == (struct exportlist *)NULL) {
1459 getexp_err(ep, tgrp);
1463 warnx("doing opt %s", cp);
1465 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1467 getexp_err(ep, tgrp);
1470 } else if (*cp == '/') {
1473 if (v4root_phase > 1) {
1475 syslog(LOG_ERR, "Multiple V4 dirs");
1476 getexp_err(ep, tgrp);
1480 if (check_dirpath(cp) &&
1481 statfs(cp, &fsb) >= 0) {
1482 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1483 syslog(LOG_ERR, "Warning: exporting of "
1484 "automounted fs %s not supported", cp);
1486 syslog(LOG_ERR, "dirs must be first");
1487 getexp_err(ep, tgrp);
1490 if (v4root_phase == 1) {
1492 syslog(LOG_ERR, "Multiple V4 dirs");
1493 getexp_err(ep, tgrp);
1496 if (strlen(v4root_dirpath) == 0) {
1497 strlcpy(v4root_dirpath, cp,
1498 sizeof (v4root_dirpath));
1499 } else if (strcmp(v4root_dirpath, cp)
1502 "different V4 dirpath %s", cp);
1503 getexp_err(ep, tgrp);
1512 if (ep->ex_fs.val[0] !=
1513 fsb.f_fsid.val[0] ||
1515 fsb.f_fsid.val[1]) {
1516 getexp_err(ep, tgrp);
1521 * See if this directory is already
1524 ep = ex_search(&fsb.f_fsid);
1525 if (ep == (struct exportlist *)NULL) {
1527 ep->ex_fs = fsb.f_fsid;
1528 ep->ex_fsdir = strdup(fsb.f_mntonname);
1529 if (ep->ex_fsdir == NULL)
1533 "making new ep fs=0x%x,0x%x",
1537 warnx("found ep fs=0x%x,0x%x",
1543 * Add dirpath to export mount point.
1545 dirp = add_expdir(&dirhead, cp, len);
1549 getexp_err(ep, tgrp);
1557 if (ep == (struct exportlist *)NULL) {
1558 getexp_err(ep, tgrp);
1563 * Get the host or netgroup.
1566 netgrp = getnetgrent(&hst, &usr, &dom);
1569 grp->gr_next = get_grp();
1575 "null hostname in netgroup %s, skipping", cp);
1576 grp->gr_type = GT_IGNORE;
1577 } else if (get_host(hst, grp, tgrp)) {
1579 "bad host %s in netgroup %s, skipping", hst, cp);
1580 grp->gr_type = GT_IGNORE;
1582 } else if (get_host(cp, grp, tgrp)) {
1583 syslog(LOG_ERR, "bad host %s, skipping", cp);
1584 grp->gr_type = GT_IGNORE;
1587 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1592 nextfield(&cp, &endcp);
1595 if (check_options(dirhead)) {
1596 getexp_err(ep, tgrp);
1600 grp->gr_type = GT_DEFAULT;
1602 warnx("adding a default entry");
1605 * Don't allow a network export coincide with a list of
1606 * host(s) on the same line.
1608 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1609 syslog(LOG_ERR, "network/host conflict");
1610 getexp_err(ep, tgrp);
1614 * If an export list was specified on this line, make sure
1615 * that we have at least one valid entry, otherwise skip it.
1619 while (grp && grp->gr_type == GT_IGNORE)
1622 getexp_err(ep, tgrp);
1627 if (v4root_phase == 1) {
1628 syslog(LOG_ERR, "V4:root, no dirp, ignored");
1629 getexp_err(ep, tgrp);
1634 * Loop through hosts, pushing the exports into the kernel.
1635 * After loop, tgrp points to the start of the list and
1636 * grp points to the last entry in the list.
1640 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1642 getexp_err(ep, tgrp);
1645 } while (grp->gr_next && (grp = grp->gr_next));
1648 * For V4: don't enter in mount lists.
1650 if (v4root_phase > 0 && v4root_phase <= 2) {
1652 * Since these structures aren't used by mountd,
1657 while (tgrp != NULL) {
1659 tgrp = tgrp->gr_next;
1666 * Success. Update the data structures.
1669 hang_dirp(dirhead, tgrp, ep, opt_flags);
1670 grp->gr_next = grphead;
1673 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1677 dirhead = (struct dirlist *)NULL;
1678 if ((ep->ex_flag & EX_LINKED) == 0) {
1683 * Insert in the list in alphabetical order.
1685 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1686 epp = &ep2->ex_next;
1692 ep->ex_flag |= EX_LINKED;
1698 dirhead = (struct dirlist *)NULL;
1704 * Get the export list from all specified files
1707 get_exportlist(void)
1709 struct exportlist *ep, *ep2;
1710 struct grouplist *grp, *tgrp;
1711 struct export_args export;
1713 struct statfs *fsp, *mntbufp;
1714 struct xvfsconf vfc;
1719 struct nfsex_args eargs;
1721 if (suspend_nfsd != 0)
1722 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1723 v4root_dirpath[0] = '\0';
1724 bzero(&export, sizeof(export));
1725 export.ex_flags = MNT_DELEXPORT;
1728 bzero(errmsg, sizeof(errmsg));
1731 * First, get rid of the old list
1739 exphead = (struct exportlist *)NULL;
1747 grphead = (struct grouplist *)NULL;
1750 * and the old V4 root dir.
1752 bzero(&eargs, sizeof (eargs));
1753 eargs.export.ex_flags = MNT_DELEXPORT;
1754 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1756 syslog(LOG_ERR, "Can't delete exports for V4:");
1759 * and clear flag that notes if a public fh has been exported.
1764 * And delete exports that are in the kernel for all local
1766 * XXX: Should know how to handle all local exportable filesystems.
1768 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1771 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1772 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1773 build_iovec(&iov, &iovlen, "from", NULL, 0);
1774 build_iovec(&iov, &iovlen, "update", NULL, 0);
1775 build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1776 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1779 for (i = 0; i < num; i++) {
1781 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1782 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1788 * We do not need to delete "export" flag from
1789 * filesystems that do not have it set.
1791 if (!(fsp->f_flags & MNT_EXPORTED))
1794 * Do not delete export for network filesystem by
1795 * passing "export" arg to nmount().
1796 * It only makes sense to do this for local filesystems.
1798 if (vfc.vfc_flags & VFCF_NETWORK)
1801 iov[1].iov_base = fsp->f_fstypename;
1802 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1803 iov[3].iov_base = fsp->f_mntonname;
1804 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1805 iov[5].iov_base = fsp->f_mntfromname;
1806 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1810 * EXDEV is returned when path exists but is not a
1811 * mount point. May happens if raced with unmount.
1813 if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1814 errno != ENOENT && errno != ENOTSUP && errno != EXDEV) {
1816 "can't delete exports for %s: %m %s",
1817 fsp->f_mntonname, errmsg);
1822 /* Free strings allocated by strdup() in getmntopts.c */
1823 free(iov[0].iov_base); /* fstype */
1824 free(iov[2].iov_base); /* fspath */
1825 free(iov[4].iov_base); /* from */
1826 free(iov[6].iov_base); /* update */
1827 free(iov[8].iov_base); /* export */
1828 free(iov[10].iov_base); /* errmsg */
1830 /* free iov, allocated by realloc() */
1836 * Read in the exports file and build the list, calling
1837 * nmount() as we go along to push the export rules into the kernel.
1840 for (i = 0; exnames[i] != NULL; i++) {
1842 warnx("reading exports from %s", exnames[i]);
1843 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1844 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1847 get_exportlist_one();
1852 syslog(LOG_ERR, "can't open any exports file");
1857 * If there was no public fh, clear any previous one set.
1859 if (has_publicfh == 0)
1860 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1862 /* Resume the nfsd. If they weren't suspended, this is harmless. */
1863 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1867 * Allocate an export list element
1869 static struct exportlist *
1872 struct exportlist *ep;
1874 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
1875 if (ep == (struct exportlist *)NULL)
1881 * Allocate a group list element
1883 static struct grouplist *
1886 struct grouplist *gp;
1888 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
1889 if (gp == (struct grouplist *)NULL)
1895 * Clean up upon an error in get_exportlist().
1898 getexp_err(struct exportlist *ep, struct grouplist *grp)
1900 struct grouplist *tgrp;
1902 if (!(opt_flags & OP_QUIET))
1903 syslog(LOG_ERR, "bad exports list line %s", line);
1904 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1914 * Search the export list for a matching fs.
1916 static struct exportlist *
1917 ex_search(fsid_t *fsid)
1919 struct exportlist *ep;
1923 if (ep->ex_fs.val[0] == fsid->val[0] &&
1924 ep->ex_fs.val[1] == fsid->val[1])
1932 * Add a directory path to the list.
1935 add_expdir(struct dirlist **dpp, char *cp, int len)
1939 dp = malloc(sizeof (struct dirlist));
1940 if (dp == (struct dirlist *)NULL)
1943 dp->dp_right = (struct dirlist *)NULL;
1945 dp->dp_hosts = (struct hostlist *)NULL;
1946 dp->dp_dirp = strndup(cp, len);
1947 if (dp->dp_dirp == NULL)
1950 return (dp->dp_dirp);
1954 * Hang the dir list element off the dirpath binary tree as required
1955 * and update the entry for host.
1958 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1961 struct hostlist *hp;
1962 struct dirlist *dp2;
1964 if (flags & OP_ALLDIRS) {
1969 if (grp == (struct grouplist *)NULL) {
1970 ep->ex_defdir->dp_flag |= DP_DEFSET;
1971 /* Save the default security flavors list. */
1972 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1973 if (ep->ex_numsecflavors > 0)
1974 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1975 sizeof(ep->ex_secflavors));
1976 } else while (grp) {
1979 hp->ht_next = ep->ex_defdir->dp_hosts;
1980 ep->ex_defdir->dp_hosts = hp;
1981 /* Save the security flavors list for this host set. */
1982 grp->gr_numsecflavors = ep->ex_numsecflavors;
1983 if (ep->ex_numsecflavors > 0)
1984 memcpy(grp->gr_secflavors, ep->ex_secflavors,
1985 sizeof(ep->ex_secflavors));
1991 * Loop through the directories adding them to the tree.
1995 add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
2002 * Traverse the binary tree either updating a node that is already there
2003 * for the new directory or adding the new node.
2006 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2007 int flags, struct exportlist *ep)
2010 struct hostlist *hp;
2015 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2017 add_dlist(&dp->dp_left, newdp, grp, flags, ep);
2019 } else if (cmp < 0) {
2020 add_dlist(&dp->dp_right, newdp, grp, flags, ep);
2023 free((caddr_t)newdp);
2026 dp->dp_left = (struct dirlist *)NULL;
2032 * Hang all of the host(s) off of the directory point.
2037 hp->ht_next = dp->dp_hosts;
2039 /* Save the security flavors list for this host set. */
2040 grp->gr_numsecflavors = ep->ex_numsecflavors;
2041 if (ep->ex_numsecflavors > 0)
2042 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2043 sizeof(ep->ex_secflavors));
2047 dp->dp_flag |= DP_DEFSET;
2048 /* Save the default security flavors list. */
2049 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2050 if (ep->ex_numsecflavors > 0)
2051 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2052 sizeof(ep->ex_secflavors));
2057 * Search for a dirpath on the export point.
2059 static struct dirlist *
2060 dirp_search(struct dirlist *dp, char *dirp)
2065 cmp = strcmp(dp->dp_dirp, dirp);
2067 return (dirp_search(dp->dp_left, dirp));
2069 return (dirp_search(dp->dp_right, dirp));
2077 * Scan for a host match in a directory tree.
2080 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2081 int *hostsetp, int *numsecflavors, int **secflavorsp)
2083 struct hostlist *hp;
2084 struct grouplist *grp;
2085 struct addrinfo *ai;
2088 if (dp->dp_flag & DP_DEFSET)
2089 *defsetp = dp->dp_flag;
2093 switch (grp->gr_type) {
2095 ai = grp->gr_ptr.gt_addrinfo;
2096 for (; ai; ai = ai->ai_next) {
2097 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2099 (hp->ht_flag | DP_HOSTSET);
2100 if (numsecflavors != NULL) {
2102 grp->gr_numsecflavors;
2111 if (!sacmp(saddr, (struct sockaddr *)
2112 &grp->gr_ptr.gt_net.nt_net,
2114 &grp->gr_ptr.gt_net.nt_mask)) {
2115 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2116 if (numsecflavors != NULL) {
2118 grp->gr_numsecflavors;
2133 * Scan tree for a host that matches the address.
2136 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2138 int defset, hostset;
2141 if (scan_tree(dp->dp_left, saddr))
2143 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2145 if (scan_tree(dp->dp_right, saddr))
2152 * Traverse the dirlist tree and free it up.
2155 free_dir(struct dirlist *dp)
2159 free_dir(dp->dp_left);
2160 free_dir(dp->dp_right);
2161 free_host(dp->dp_hosts);
2168 * Parse a colon separated list of security flavors
2171 parsesec(char *seclist, struct exportlist *ep)
2176 ep->ex_numsecflavors = 0;
2178 cp = strchr(seclist, ':');
2184 if (!strcmp(seclist, "sys"))
2186 else if (!strcmp(seclist, "krb5"))
2187 flavor = RPCSEC_GSS_KRB5;
2188 else if (!strcmp(seclist, "krb5i"))
2189 flavor = RPCSEC_GSS_KRB5I;
2190 else if (!strcmp(seclist, "krb5p"))
2191 flavor = RPCSEC_GSS_KRB5P;
2195 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2198 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2201 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2204 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2205 ep->ex_numsecflavors++;
2217 * Parse the option string and update fields.
2218 * Option arguments may either be -<option>=<value> or
2222 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2223 int *has_hostp, int *exflagsp, struct xucred *cr)
2225 char *cpoptarg, *cpoptend;
2226 char *cp, *endcp, *cpopt, savedc, savedc2;
2227 int allflag, usedarg;
2235 while (cpopt && *cpopt) {
2238 if ((cpoptend = strchr(cpopt, ','))) {
2240 if ((cpoptarg = strchr(cpopt, '=')))
2243 if ((cpoptarg = strchr(cpopt, '=')))
2247 nextfield(&cp, &endcp);
2249 if (endcp > cp && *cp != '-') {
2257 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2258 *exflagsp |= MNT_EXRDONLY;
2259 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2260 !(allflag = strcmp(cpopt, "mapall")) ||
2261 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2263 parsecred(cpoptarg, cr);
2265 *exflagsp |= MNT_EXPORTANON;
2266 opt_flags |= OP_MAPALL;
2268 opt_flags |= OP_MAPROOT;
2269 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2270 !strcmp(cpopt, "m"))) {
2271 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2272 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2276 opt_flags |= OP_MASK;
2277 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2278 !strcmp(cpopt, "n"))) {
2279 if (strchr(cpoptarg, '/') != NULL) {
2281 fprintf(stderr, "setting OP_MASKLEN\n");
2282 opt_flags |= OP_MASKLEN;
2284 if (grp->gr_type != GT_NULL) {
2285 syslog(LOG_ERR, "network/host conflict");
2287 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2288 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2291 grp->gr_type = GT_NET;
2294 opt_flags |= OP_NET;
2295 } else if (!strcmp(cpopt, "alldirs")) {
2296 opt_flags |= OP_ALLDIRS;
2297 } else if (!strcmp(cpopt, "public")) {
2298 *exflagsp |= MNT_EXPUBLIC;
2299 } else if (!strcmp(cpopt, "webnfs")) {
2300 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2301 opt_flags |= OP_MAPALL;
2302 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2303 ep->ex_indexfile = strdup(cpoptarg);
2304 } else if (!strcmp(cpopt, "quiet")) {
2305 opt_flags |= OP_QUIET;
2306 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2307 if (parsesec(cpoptarg, ep))
2309 opt_flags |= OP_SEC;
2312 syslog(LOG_ERR, "bad opt %s", cpopt);
2331 * Translate a character string to the corresponding list of network
2332 * addresses for a hostname.
2335 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2337 struct grouplist *checkgrp;
2338 struct addrinfo *ai, *tai, hints;
2340 char host[NI_MAXHOST];
2342 if (grp->gr_type != GT_NULL) {
2343 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2346 memset(&hints, 0, sizeof hints);
2347 hints.ai_flags = AI_CANONNAME;
2348 hints.ai_protocol = IPPROTO_UDP;
2349 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2351 syslog(LOG_ERR,"can't get address info for host %s", cp);
2354 grp->gr_ptr.gt_addrinfo = ai;
2355 while (ai != NULL) {
2356 if (ai->ai_canonname == NULL) {
2357 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2358 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2359 strlcpy(host, "?", sizeof(host));
2360 ai->ai_canonname = strdup(host);
2361 ai->ai_flags |= AI_CANONNAME;
2364 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2366 * Sanity check: make sure we don't already have an entry
2367 * for this host in the grouplist.
2369 for (checkgrp = tgrp; checkgrp != NULL;
2370 checkgrp = checkgrp->gr_next) {
2371 if (checkgrp->gr_type != GT_HOST)
2373 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2374 tai = tai->ai_next) {
2375 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2379 "ignoring duplicate host %s\n",
2381 grp->gr_type = GT_IGNORE;
2387 grp->gr_type = GT_HOST;
2392 * Free up an exports list component
2395 free_exp(struct exportlist *ep)
2398 if (ep->ex_defdir) {
2399 free_host(ep->ex_defdir->dp_hosts);
2400 free((caddr_t)ep->ex_defdir);
2404 if (ep->ex_indexfile)
2405 free(ep->ex_indexfile);
2406 free_dir(ep->ex_dirl);
2414 free_host(struct hostlist *hp)
2416 struct hostlist *hp2;
2425 static struct hostlist *
2428 struct hostlist *hp;
2430 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2431 if (hp == (struct hostlist *)NULL)
2433 hp->ht_next = (struct hostlist *)NULL;
2439 * Out of memory, fatal
2445 syslog(LOG_ERR, "out of memory");
2450 * Do the nmount() syscall with the update flag to push the export info into
2454 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2455 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
2458 struct addrinfo *ai;
2459 struct export_args *eap;
2467 struct nfsex_args nfsea;
2469 eap = &nfsea.export;
2477 bzero(eap, sizeof (struct export_args));
2478 bzero(errmsg, sizeof(errmsg));
2479 eap->ex_flags = exflags;
2480 eap->ex_anon = *anoncrp;
2481 eap->ex_indexfile = ep->ex_indexfile;
2482 if (grp->gr_type == GT_HOST)
2483 ai = grp->gr_ptr.gt_addrinfo;
2486 eap->ex_numsecflavors = ep->ex_numsecflavors;
2487 for (i = 0; i < eap->ex_numsecflavors; i++)
2488 eap->ex_secflavors[i] = ep->ex_secflavors[i];
2489 if (eap->ex_numsecflavors == 0) {
2490 eap->ex_numsecflavors = 1;
2491 eap->ex_secflavors[0] = AUTH_SYS;
2495 if (v4root_phase == 0) {
2496 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2497 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2498 build_iovec(&iov, &iovlen, "from", NULL, 0);
2499 build_iovec(&iov, &iovlen, "update", NULL, 0);
2500 build_iovec(&iov, &iovlen, "export", eap,
2501 sizeof (struct export_args));
2502 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2506 switch (grp->gr_type) {
2508 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
2510 eap->ex_addr = ai->ai_addr;
2511 eap->ex_addrlen = ai->ai_addrlen;
2512 eap->ex_masklen = 0;
2515 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
2519 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2521 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2523 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2524 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
2527 eap->ex_addr = NULL;
2528 eap->ex_addrlen = 0;
2529 eap->ex_mask = NULL;
2530 eap->ex_masklen = 0;
2537 syslog(LOG_ERR, "bad grouptype");
2545 * For V4:, use the nfssvc() syscall, instead of mount().
2547 if (v4root_phase == 2) {
2548 nfsea.fspec = v4root_dirpath;
2549 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2550 syslog(LOG_ERR, "Exporting V4: failed");
2556 * Maybe I should just use the fsb->f_mntonname path
2557 * instead of looping back up the dirp to the mount
2559 * Also, needs to know how to export all types of local
2560 * exportable filesystems and not just "ufs".
2562 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2563 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2564 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2565 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2566 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2567 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2570 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2574 cp = dirp + dirplen - 1;
2575 if (opt_flags & OP_QUIET) {
2579 if (errno == EPERM) {
2581 warnx("can't change attributes for %s: %s",
2584 "can't change attributes for %s: %s",
2589 if (opt_flags & OP_ALLDIRS) {
2590 if (errno == EINVAL)
2592 "-alldirs requested but %s is not a filesystem mountpoint",
2596 "could not remount %s: %m",
2601 /* back up over the last component */
2602 while (*cp == '/' && cp > dirp)
2604 while (*(cp - 1) != '/' && cp > dirp)
2608 warnx("mnt unsucc");
2609 syslog(LOG_ERR, "can't export %s %s",
2617 * Check that we're still on the same
2620 if (statfs(dirp, &fsb1) != 0 ||
2621 bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2622 sizeof (fsb1.f_fsid)) != 0) {
2625 "can't export %s %s", dirp,
2634 * For the experimental server:
2635 * If this is the public directory, get the file handle
2636 * and load it into the kernel via the nfssvc() syscall.
2638 if ((exflags & MNT_EXPUBLIC) != 0) {
2642 if (eap->ex_indexfile != NULL)
2643 public_name = eap->ex_indexfile;
2646 if (getfh(public_name, &fh) < 0)
2648 "Can't get public fh for %s", public_name);
2649 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2651 "Can't set public fh for %s", public_name);
2664 /* free strings allocated by strdup() in getmntopts.c */
2666 free(iov[0].iov_base); /* fstype */
2667 free(iov[2].iov_base); /* fspath */
2668 free(iov[4].iov_base); /* from */
2669 free(iov[6].iov_base); /* update */
2670 free(iov[8].iov_base); /* export */
2671 free(iov[10].iov_base); /* errmsg */
2673 /* free iov, allocated by realloc() */
2680 * Translate a net address.
2682 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2685 get_net(char *cp, struct netmsk *net, int maskflg)
2687 struct netent *np = NULL;
2688 char *name, *p, *prefp;
2689 struct sockaddr_in sin;
2690 struct sockaddr *sa = NULL;
2691 struct addrinfo hints, *ai = NULL;
2692 char netname[NI_MAXHOST];
2696 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2697 p = strchr(cp, '/');
2703 * Check for a numeric address first. We wish to avoid
2704 * possible DNS lookups in getnetbyname().
2706 if (isxdigit(*cp) || *cp == ':') {
2707 memset(&hints, 0, sizeof hints);
2708 /* Ensure the mask and the network have the same family. */
2709 if (maskflg && (opt_flags & OP_NET))
2710 hints.ai_family = net->nt_net.ss_family;
2711 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2712 hints.ai_family = net->nt_mask.ss_family;
2714 hints.ai_family = AF_UNSPEC;
2715 hints.ai_flags = AI_NUMERICHOST;
2716 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2718 if (sa != NULL && ai->ai_family == AF_INET) {
2720 * The address in `cp' is really a network address, so
2721 * use inet_network() to re-interpret this correctly.
2722 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2724 bzero(&sin, sizeof sin);
2725 sin.sin_family = AF_INET;
2726 sin.sin_len = sizeof sin;
2727 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2729 fprintf(stderr, "get_net: v4 addr %s\n",
2730 inet_ntoa(sin.sin_addr));
2731 sa = (struct sockaddr *)&sin;
2734 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2735 bzero(&sin, sizeof sin);
2736 sin.sin_family = AF_INET;
2737 sin.sin_len = sizeof sin;
2738 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2739 sa = (struct sockaddr *)&sin;
2745 /* The specified sockaddr is a mask. */
2746 if (checkmask(sa) != 0)
2748 bcopy(sa, &net->nt_mask, sa->sa_len);
2749 opt_flags |= OP_HAVEMASK;
2751 /* The specified sockaddr is a network address. */
2752 bcopy(sa, &net->nt_net, sa->sa_len);
2754 /* Get a network name for the export list. */
2757 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2758 NULL, 0, NI_NUMERICHOST) == 0) {
2763 if ((net->nt_name = strdup(name)) == NULL)
2767 * Extract a mask from either a "/<masklen>" suffix, or
2768 * from the class of an IPv4 address.
2770 if (opt_flags & OP_MASKLEN) {
2771 preflen = strtol(prefp, NULL, 10);
2772 if (preflen < 0L || preflen == LONG_MAX)
2774 bcopy(sa, &net->nt_mask, sa->sa_len);
2775 if (makemask(&net->nt_mask, (int)preflen) != 0)
2777 opt_flags |= OP_HAVEMASK;
2779 } else if (sa->sa_family == AF_INET &&
2780 (opt_flags & OP_MASK) == 0) {
2783 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2784 if (IN_CLASSA(addr))
2786 else if (IN_CLASSB(addr))
2788 else if (IN_CLASSC(addr))
2790 else if (IN_CLASSD(addr))
2793 preflen = 32; /* XXX */
2795 bcopy(sa, &net->nt_mask, sa->sa_len);
2796 makemask(&net->nt_mask, (int)preflen);
2797 opt_flags |= OP_HAVEMASK;
2812 * Parse out the next white space separated field
2815 nextfield(char **cp, char **endcp)
2820 while (*p == ' ' || *p == '\t')
2822 if (*p == '\n' || *p == '\0')
2826 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2833 * Get an exports file line. Skip over blank lines and handle line
2841 int totlen, cont_line;
2844 * Loop around ignoring blank lines and getting all continuation lines.
2849 if ((p = fgetln(exp_file, &len)) == NULL)
2854 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2864 if (linesize < len + totlen + 1) {
2865 linesize = len + totlen + 1;
2866 line = realloc(line, linesize);
2870 memcpy(line + totlen, p, len);
2872 line[totlen] = '\0';
2873 } while (totlen == 0 || cont_line);
2878 * Parse a description of a credential.
2881 parsecred(char *namelist, struct xucred *cr)
2888 gid_t groups[XU_NGROUPS + 1];
2891 cr->cr_version = XUCRED_VERSION;
2893 * Set up the unprivileged user.
2896 cr->cr_groups[0] = 65533;
2899 * Get the user's password table entry.
2901 names = strsep_quote(&namelist, " \t\n");
2902 name = strsep(&names, ":");
2903 /* Bug? name could be NULL here */
2904 if (isdigit(*name) || *name == '-')
2905 pw = getpwuid(atoi(name));
2907 pw = getpwnam(name);
2909 * Credentials specified as those of a user.
2911 if (names == NULL) {
2913 syslog(LOG_ERR, "unknown user: %s", name);
2916 cr->cr_uid = pw->pw_uid;
2917 ngroups = XU_NGROUPS + 1;
2918 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2919 syslog(LOG_ERR, "too many groups");
2921 * Compress out duplicate.
2923 cr->cr_ngroups = ngroups - 1;
2924 cr->cr_groups[0] = groups[0];
2925 for (cnt = 2; cnt < ngroups; cnt++)
2926 cr->cr_groups[cnt - 1] = groups[cnt];
2930 * Explicit credential specified as a colon separated list:
2934 cr->cr_uid = pw->pw_uid;
2935 else if (isdigit(*name) || *name == '-')
2936 cr->cr_uid = atoi(name);
2938 syslog(LOG_ERR, "unknown user: %s", name);
2942 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
2943 name = strsep(&names, ":");
2944 if (isdigit(*name) || *name == '-') {
2945 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2947 if ((gr = getgrnam(name)) == NULL) {
2948 syslog(LOG_ERR, "unknown group: %s", name);
2951 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2954 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
2955 syslog(LOG_ERR, "too many groups");
2958 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
2960 * Routines that maintain the remote mounttab
2965 struct mountlist *mlp, **mlpp;
2966 char *host, *dirp, *cp;
2970 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2971 if (errno == ENOENT)
2974 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2979 while (fgets(str, STRSIZ, mlfile) != NULL) {
2981 host = strsep(&cp, " \t\n");
2982 dirp = strsep(&cp, " \t\n");
2983 if (host == NULL || dirp == NULL)
2985 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2986 if (mlp == (struct mountlist *)NULL)
2988 strncpy(mlp->ml_host, host, MNTNAMLEN);
2989 mlp->ml_host[MNTNAMLEN] = '\0';
2990 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2991 mlp->ml_dirp[MNTPATHLEN] = '\0';
2992 mlp->ml_next = (struct mountlist *)NULL;
2994 mlpp = &mlp->ml_next;
3000 del_mlist(char *hostp, char *dirp)
3002 struct mountlist *mlp, **mlpp;
3003 struct mountlist *mlp2;
3010 if (!strcmp(mlp->ml_host, hostp) &&
3011 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3014 *mlpp = mlp = mlp->ml_next;
3015 free((caddr_t)mlp2);
3017 mlpp = &mlp->ml_next;
3022 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3023 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3028 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3036 add_mlist(char *hostp, char *dirp)
3038 struct mountlist *mlp, **mlpp;
3044 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3046 mlpp = &mlp->ml_next;
3049 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3050 if (mlp == (struct mountlist *)NULL)
3052 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3053 mlp->ml_host[MNTNAMLEN] = '\0';
3054 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3055 mlp->ml_dirp[MNTPATHLEN] = '\0';
3056 mlp->ml_next = (struct mountlist *)NULL;
3058 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3059 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3062 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3067 * Free up a group list.
3070 free_grp(struct grouplist *grp)
3072 if (grp->gr_type == GT_HOST) {
3073 if (grp->gr_ptr.gt_addrinfo != NULL)
3074 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3075 } else if (grp->gr_type == GT_NET) {
3076 if (grp->gr_ptr.gt_net.nt_name)
3077 free(grp->gr_ptr.gt_net.nt_name);
3084 SYSLOG(int pri, const char *fmt, ...)
3089 vfprintf(stderr, fmt, ap);
3095 * Check options for consistency.
3098 check_options(struct dirlist *dp)
3101 if (v4root_phase == 0 && dp == NULL)
3103 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3104 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3107 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3108 syslog(LOG_ERR, "-mask requires -network");
3111 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3112 syslog(LOG_ERR, "-network requires mask specification");
3115 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3116 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3119 if (v4root_phase > 0 &&
3121 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3122 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3125 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3126 syslog(LOG_ERR, "-alldirs has multiple directories");
3133 * Check an absolute directory path for any symbolic links. Return true
3136 check_dirpath(char *dirp)
3143 while (*cp && ret) {
3146 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3152 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3158 * Make a netmask according to the specified prefix length. The ss_family
3159 * and other non-address fields must be initialised before calling this.
3162 makemask(struct sockaddr_storage *ssp, int bitlen)
3167 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3169 if (bitlen > len * CHAR_BIT)
3172 for (i = 0; i < len; i++) {
3173 bits = MIN(CHAR_BIT, bitlen);
3174 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3181 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3182 * is acceptable (i.e. of the form 1...10....0).
3185 checkmask(struct sockaddr *sa)
3190 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3193 for (i = 0; i < len; i++)
3194 if (mask[i] != 0xff)
3197 if (~mask[i] & (u_char)(~mask[i] + 1))
3201 for (; i < len; i++)
3208 * Compare two sockaddrs according to a specified mask. Return zero if
3209 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3210 * If samask is NULL, perform a full comparison.
3213 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3215 unsigned char *p1, *p2, *mask;
3218 if (sa1->sa_family != sa2->sa_family ||
3219 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3220 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3223 switch (sa1->sa_family) {
3225 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3226 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3231 /* Simple binary comparison if no mask specified. */
3233 return (memcmp(p1, p2, len));
3235 /* Set up the mask, and do a mask-based comparison. */
3236 if (sa1->sa_family != samask->sa_family ||
3237 (mask = sa_rawaddr(samask, NULL)) == NULL)
3240 for (i = 0; i < len; i++)
3241 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3247 * Return a pointer to the part of the sockaddr that contains the
3248 * raw address, and set *nbytes to its length in bytes. Returns
3249 * NULL if the address family is unknown.
3252 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3256 switch (sa->sa_family) {
3258 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3259 p = &((struct sockaddr_in *)sa)->sin_addr;
3262 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3263 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3276 huphandler(int sig __unused)
3283 terminate(int sig __unused)
3285 pidfile_remove(pfh);
3286 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3287 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);