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 * 4. 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 */
104 char dp_dirp[1]; /* Actually malloc'd to size of dir */
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 char *add_expdir(struct dirlist **, char *, int);
169 void add_dlist(struct dirlist **, struct dirlist *,
170 struct grouplist *, int, struct exportlist *);
171 void add_mlist(char *, char *);
172 int check_dirpath(char *);
173 int check_options(struct dirlist *);
174 int checkmask(struct sockaddr *sa);
175 int chk_host(struct dirlist *, struct sockaddr *, int *, 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 void del_mlist(char *hostp, char *dirp);
182 struct dirlist *dirp_search(struct dirlist *, char *);
183 int do_mount(struct exportlist *, struct grouplist *, int,
184 struct xucred *, char *, int, struct statfs *);
185 int do_opt(char **, char **, struct exportlist *, struct grouplist *,
186 int *, int *, struct xucred *);
187 struct exportlist *ex_search(fsid_t *);
188 struct exportlist *get_exp(void);
189 void free_dir(struct dirlist *);
190 void free_exp(struct exportlist *);
191 void free_grp(struct grouplist *);
192 void free_host(struct hostlist *);
193 void get_exportlist(void);
194 int get_host(char *, struct grouplist *, struct grouplist *);
195 struct hostlist *get_ht(void);
197 void get_mountlist(void);
198 int get_net(char *, struct netmsk *, int);
199 void getexp_err(struct exportlist *, struct grouplist *);
200 struct grouplist *get_grp(void);
201 void hang_dirp(struct dirlist *, struct grouplist *,
202 struct exportlist *, int);
203 void huphandler(int sig);
204 int makemask(struct sockaddr_storage *ssp, int bitlen);
205 void mntsrv(struct svc_req *, SVCXPRT *);
206 void nextfield(char **, char **);
207 void out_of_mem(void);
208 void parsecred(char *, struct xucred *);
209 int parsesec(char *, struct exportlist *);
210 int put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
211 void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
212 int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
213 struct sockaddr *samask);
214 int scan_tree(struct dirlist *, struct sockaddr *);
215 static void usage(void);
216 int xdr_dir(XDR *, char *);
217 int xdr_explist(XDR *, caddr_t);
218 int xdr_explist_brief(XDR *, caddr_t);
219 int xdr_explist_common(XDR *, caddr_t, int);
220 int xdr_fhs(XDR *, caddr_t);
221 int xdr_mlist(XDR *, caddr_t);
224 struct exportlist *exphead;
225 struct mountlist *mlhead;
226 struct grouplist *grphead;
227 char *exnames_default[2] = { _PATH_EXPORTS, NULL };
230 struct xucred def_anon = {
238 int resvport_only = 1;
245 char *svcport_str = NULL;
246 static int mallocd_svcport = 0;
248 static int sock_fdcnt;
249 static int sock_fdpos;
250 static int suspend_nfsd = 0;
253 static int have_v6 = 1;
255 int v4root_phase = 0;
256 char v4root_dirpath[PATH_MAX + 1];
257 int run_v4server = 1;
258 int has_publicfh = 0;
260 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
275 void SYSLOG(int, const char *, ...) __printflike(2, 3);
276 #define syslog SYSLOG
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:lnop:rS")) != -1)
389 /* now a no-op, since this is the default */
398 debug = debug ? 0 : 1;
408 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
409 if (endptr == NULL || *endptr != '\0' ||
410 svcport == 0 || svcport >= IPPORT_MAX)
412 svcport_str = strdup(optarg);
417 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
418 if (hosts_bak == NULL) {
420 for (k = 0; k < nhosts; k++)
427 hosts[nhosts - 1] = strdup(optarg);
428 if (hosts[nhosts - 1] == NULL) {
429 for (k = 0; k < (nhosts - 1); k++)
443 * Unless the "-o" option was specified, try and run "nfsd".
444 * If "-o" was specified, try and run "nfsserver".
446 if (run_v4server > 0) {
447 if (modfind("nfsd") < 0) {
448 /* Not present in kernel, try loading it */
449 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
450 errx(1, "NFS server is not available");
452 } else if (modfind("nfsserver") < 0) {
453 /* Not present in kernel, try loading it */
454 if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
455 errx(1, "NFS server is not available");
460 grphead = (struct grouplist *)NULL;
461 exphead = (struct exportlist *)NULL;
462 mlhead = (struct mountlist *)NULL;
466 exnames = exnames_default;
467 openlog("mountd", LOG_PID, LOG_DAEMON);
469 warnx("getting export list");
472 warnx("getting mount list");
478 signal(SIGINT, SIG_IGN);
479 signal(SIGQUIT, SIG_IGN);
481 signal(SIGHUP, huphandler);
482 signal(SIGTERM, terminate);
483 signal(SIGPIPE, SIG_IGN);
487 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
488 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
489 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
491 if (!resvport_only) {
492 if (run_v4server != 0) {
493 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
494 &resvport_only, sizeof(resvport_only)) != 0 &&
496 syslog(LOG_ERR, "sysctl: %m");
500 if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
501 &resvport_only, sizeof(resvport_only)) != 0 &&
503 syslog(LOG_ERR, "sysctl: %m");
510 * If no hosts were specified, add a wildcard entry to bind to
511 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
515 hosts = malloc(sizeof(char**));
523 hosts_bak = realloc(hosts, (nhosts + 2) *
525 if (hosts_bak == NULL) {
526 for (k = 0; k < nhosts; k++)
533 hosts[nhosts - 2] = "::1";
535 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
536 if (hosts_bak == NULL) {
537 for (k = 0; k < nhosts; k++)
547 hosts[nhosts - 1] = "127.0.0.1";
555 nc_handle = setnetconfig();
556 while ((nconf = getnetconfig(nc_handle))) {
557 if (nconf->nc_flag & NC_VISIBLE) {
558 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
562 ret = create_service(nconf);
564 /* Ignore this call */
568 * Failed to bind port, so close off
569 * all sockets created and try again
570 * if the port# was dynamically
571 * assigned via bind(2).
574 if (mallocd_svcport != 0 &&
575 attempt_cnt < GETPORT_MAXTRY) {
582 "bindresvport_sa: %m");
586 /* Start over at the first service. */
590 nc_handle = setnetconfig();
592 } else if (mallocd_svcport != 0 &&
593 attempt_cnt == GETPORT_MAXTRY) {
595 * For the last attempt, allow
596 * different port #s for each nconf
597 * by saving the svcport_str and
598 * setting it back to NULL.
600 port_list = realloc(port_list,
601 (port_len + 1) * sizeof(char *));
602 if (port_list == NULL)
604 port_list[port_len++] = svcport_str;
613 * Successfully bound the ports, so call complete_service() to
614 * do the rest of the setup on the service(s).
618 nc_handle = setnetconfig();
619 while ((nconf = getnetconfig(nc_handle))) {
620 if (nconf->nc_flag & NC_VISIBLE) {
621 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
624 } else if (port_list != NULL) {
625 if (port_pos >= port_len) {
626 syslog(LOG_ERR, "too many port#s");
629 complete_service(nconf, port_list[port_pos++]);
631 complete_service(nconf, svcport_str);
634 endnetconfig(nc_handle);
636 if (port_list != NULL) {
637 for (port_pos = 0; port_pos < port_len; port_pos++)
638 free(port_list[port_pos]);
643 syslog(LOG_ERR, "could not create any services");
647 /* Expand svc_run() here so that we can call get_exportlist(). */
654 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
658 syslog(LOG_ERR, "mountd died: select: %m");
663 svc_getreqset(&readfds);
669 * This routine creates and binds sockets on the appropriate
670 * addresses. It gets called one time for each transport.
671 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
672 * bind failed with EADDRINUSE.
673 * Any file descriptors that have been created are stored in sock_fd and
674 * the total count of them is maintained in sock_fdcnt.
677 create_service(struct netconfig *nconf)
679 struct addrinfo hints, *res = NULL;
680 struct sockaddr_in *sin;
681 struct sockaddr_in6 *sin6;
682 struct __rpc_sockinfo si;
688 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
691 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
692 (nconf->nc_semantics != NC_TPI_COTS) &&
693 (nconf->nc_semantics != NC_TPI_COTS_ORD))
694 return (1); /* not my type */
697 * XXX - using RPC library internal functions.
699 if (!__rpc_nconf2sockinfo(nconf, &si)) {
700 syslog(LOG_ERR, "cannot get information for %s",
705 /* Get mountd's address on this transport */
706 memset(&hints, 0, sizeof hints);
707 hints.ai_family = si.si_af;
708 hints.ai_socktype = si.si_socktype;
709 hints.ai_protocol = si.si_proto;
712 * Bind to specific IPs if asked to
715 while (nhostsbak > 0) {
717 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
720 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
723 hints.ai_flags = AI_PASSIVE;
726 * XXX - using RPC library internal functions.
728 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
730 if (errno == EAFNOSUPPORT &&
731 nconf->nc_semantics != NC_TPI_CLTS)
734 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
735 "cannot create socket for %s", nconf->nc_netid);
741 switch (hints.ai_family) {
743 if (inet_pton(AF_INET, hosts[nhostsbak],
745 hints.ai_flags |= AI_NUMERICHOST;
748 * Skip if we have an AF_INET6 address.
750 if (inet_pton(AF_INET6, hosts[nhostsbak],
758 if (inet_pton(AF_INET6, hosts[nhostsbak],
760 hints.ai_flags |= AI_NUMERICHOST;
763 * Skip if we have an AF_INET address.
765 if (inet_pton(AF_INET, hosts[nhostsbak],
773 * We're doing host-based access checks here, so don't
774 * allow v4-in-v6 to confuse things. The kernel will
775 * disable it by default on NFS sockets too.
777 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
780 "can't disable v4-in-v6 on IPv6 socket");
789 * If no hosts were specified, just bind to INADDR_ANY
791 if (strcmp("*", hosts[nhostsbak]) == 0) {
792 if (svcport_str == NULL) {
793 res = malloc(sizeof(struct addrinfo));
797 res->ai_flags = hints.ai_flags;
798 res->ai_family = hints.ai_family;
799 res->ai_protocol = hints.ai_protocol;
800 switch (res->ai_family) {
802 sin = malloc(sizeof(struct sockaddr_in));
805 sin->sin_family = AF_INET;
806 sin->sin_port = htons(0);
807 sin->sin_addr.s_addr = htonl(INADDR_ANY);
808 res->ai_addr = (struct sockaddr*) sin;
809 res->ai_addrlen = (socklen_t)
810 sizeof(struct sockaddr_in);
813 sin6 = malloc(sizeof(struct sockaddr_in6));
816 sin6->sin6_family = AF_INET6;
817 sin6->sin6_port = htons(0);
818 sin6->sin6_addr = in6addr_any;
819 res->ai_addr = (struct sockaddr*) sin6;
820 res->ai_addrlen = (socklen_t)
821 sizeof(struct sockaddr_in6);
824 syslog(LOG_ERR, "bad addr fam %d",
829 if ((aicode = getaddrinfo(NULL, svcport_str,
830 &hints, &res)) != 0) {
832 "cannot get local address for %s: %s",
834 gai_strerror(aicode));
840 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
841 &hints, &res)) != 0) {
843 "cannot get local address for %s: %s",
844 nconf->nc_netid, gai_strerror(aicode));
851 sock_fd[sock_fdcnt - 1] = fd;
853 /* Now, attempt the bind. */
854 r = bindresvport_sa(fd, res->ai_addr);
856 if (errno == EADDRINUSE && mallocd_svcport != 0) {
857 if (mallocd_res != 0) {
864 syslog(LOG_ERR, "bindresvport_sa: %m");
868 if (svcport_str == NULL) {
869 svcport_str = malloc(NI_MAXSERV * sizeof(char));
870 if (svcport_str == NULL)
874 if (getnameinfo(res->ai_addr,
875 res->ai_addr->sa_len, NULL, NI_MAXHOST,
876 svcport_str, NI_MAXSERV * sizeof(char),
877 NI_NUMERICHOST | NI_NUMERICSERV))
878 errx(1, "Cannot get port number");
880 if (mallocd_res != 0) {
891 * Called after all the create_service() calls have succeeded, to complete
892 * the setup and registration.
895 complete_service(struct netconfig *nconf, char *port_str)
897 struct addrinfo hints, *res = NULL;
898 struct __rpc_sockinfo si;
899 struct netbuf servaddr;
900 SVCXPRT *transp = NULL;
901 int aicode, fd, nhostsbak;
904 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
905 (nconf->nc_semantics != NC_TPI_COTS) &&
906 (nconf->nc_semantics != NC_TPI_COTS_ORD))
907 return; /* not my type */
910 * XXX - using RPC library internal functions.
912 if (!__rpc_nconf2sockinfo(nconf, &si)) {
913 syslog(LOG_ERR, "cannot get information for %s",
919 while (nhostsbak > 0) {
921 if (sock_fdpos >= sock_fdcnt) {
922 /* Should never happen. */
923 syslog(LOG_ERR, "Ran out of socket fd's");
926 fd = sock_fd[sock_fdpos++];
930 if (nconf->nc_semantics != NC_TPI_CLTS)
931 listen(fd, SOMAXCONN);
933 if (nconf->nc_semantics == NC_TPI_CLTS )
934 transp = svc_dg_create(fd, 0, 0);
936 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
939 if (transp != (SVCXPRT *) NULL) {
940 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
943 "can't register %s MOUNTVERS service",
946 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
949 "can't register %s MOUNTVERS3 service",
953 syslog(LOG_WARNING, "can't create %s services",
956 if (registered == 0) {
958 memset(&hints, 0, sizeof hints);
959 hints.ai_flags = AI_PASSIVE;
960 hints.ai_family = si.si_af;
961 hints.ai_socktype = si.si_socktype;
962 hints.ai_protocol = si.si_proto;
964 if ((aicode = getaddrinfo(NULL, port_str, &hints,
966 syslog(LOG_ERR, "cannot get local address: %s",
967 gai_strerror(aicode));
971 servaddr.buf = malloc(res->ai_addrlen);
972 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
973 servaddr.len = res->ai_addrlen;
975 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
976 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
985 * Clear out sockets after a failure to bind one of them, so that the
986 * cycle of socket creation/binding can start anew.
989 clearout_service(void)
993 for (i = 0; i < sock_fdcnt; i++) {
994 if (sock_fd[i] >= 0) {
995 shutdown(sock_fd[i], SHUT_RDWR);
1005 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
1006 "[-S] [-h <bindip>] [export_file ...]\n");
1011 * The mount rpc service
1014 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
1016 struct exportlist *ep;
1018 struct fhreturn fhr;
1021 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1022 int lookup_failed = 1;
1023 struct sockaddr *saddr;
1025 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1026 int bad = 0, defset, hostset;
1027 sigset_t sighup_mask;
1028 int numsecflavors, *secflavorsp;
1030 sigemptyset(&sighup_mask);
1031 sigaddset(&sighup_mask, SIGHUP);
1032 saddr = svc_getrpccaller(transp)->buf;
1033 switch (saddr->sa_family) {
1035 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1038 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1041 syslog(LOG_ERR, "request from unknown address family");
1044 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
1046 getnameinfo(saddr, saddr->sa_len, numerichost,
1047 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1048 switch (rqstp->rq_proc) {
1050 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1051 syslog(LOG_ERR, "can't send reply");
1054 if (sport >= IPPORT_RESERVED && resvport_only) {
1056 "mount request from %s from unprivileged port",
1058 svcerr_weakauth(transp);
1061 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1062 syslog(LOG_NOTICE, "undecodable mount request from %s",
1064 svcerr_decode(transp);
1069 * Get the real pathname and make sure it is a directory
1070 * or a regular file if the -r option was specified
1073 if (realpath(rpcpath, dirpath) == NULL ||
1074 stat(dirpath, &stb) < 0 ||
1075 statfs(dirpath, &fsb) < 0) {
1076 chdir("/"); /* Just in case realpath doesn't */
1078 "mount request from %s for non existent path %s",
1079 numerichost, dirpath);
1081 warnx("stat failed on %s", dirpath);
1082 bad = ENOENT; /* We will send error reply later */
1085 !S_ISDIR(stb.st_mode) &&
1086 (dir_only || !S_ISREG(stb.st_mode))) {
1088 "mount request from %s for non-directory path %s",
1089 numerichost, dirpath);
1091 warnx("mounting non-directory %s", dirpath);
1092 bad = ENOTDIR; /* We will send error reply later */
1095 /* Check in the exports list */
1096 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1100 ep = ex_search(&fsb.f_fsid);
1101 hostset = defset = 0;
1102 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1103 &numsecflavors, &secflavorsp) ||
1104 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1105 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1107 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1108 scan_tree(ep->ex_dirl, saddr) == 0))) {
1110 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1112 syslog(LOG_ERR, "can't send reply");
1113 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1116 if (hostset & DP_HOSTSET) {
1117 fhr.fhr_flag = hostset;
1118 fhr.fhr_numsecflavors = numsecflavors;
1119 fhr.fhr_secflavors = secflavorsp;
1121 fhr.fhr_flag = defset;
1122 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1123 fhr.fhr_secflavors = ep->ex_defsecflavors;
1125 fhr.fhr_vers = rqstp->rq_vers;
1126 /* Get the file handle */
1127 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1128 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1130 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1131 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1133 syslog(LOG_ERR, "can't send reply");
1134 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1137 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1139 syslog(LOG_ERR, "can't send reply");
1141 add_mlist(host, dirpath);
1143 add_mlist(numerichost, dirpath);
1145 warnx("mount successful");
1148 "mount request succeeded from %s for %s",
1149 numerichost, dirpath);
1154 "mount request denied from %s for %s",
1155 numerichost, dirpath);
1158 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1160 syslog(LOG_ERR, "can't send reply");
1161 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1163 case MOUNTPROC_DUMP:
1164 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1165 syslog(LOG_ERR, "can't send reply");
1168 "dump request succeeded from %s",
1171 case MOUNTPROC_UMNT:
1172 if (sport >= IPPORT_RESERVED && resvport_only) {
1174 "umount request from %s from unprivileged port",
1176 svcerr_weakauth(transp);
1179 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1180 syslog(LOG_NOTICE, "undecodable umount request from %s",
1182 svcerr_decode(transp);
1185 if (realpath(rpcpath, dirpath) == NULL) {
1186 syslog(LOG_NOTICE, "umount request from %s "
1187 "for non existent path %s",
1188 numerichost, dirpath);
1190 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1191 syslog(LOG_ERR, "can't send reply");
1193 del_mlist(host, dirpath);
1194 del_mlist(numerichost, dirpath);
1197 "umount request succeeded from %s for %s",
1198 numerichost, dirpath);
1200 case MOUNTPROC_UMNTALL:
1201 if (sport >= IPPORT_RESERVED && resvport_only) {
1203 "umountall request from %s from unprivileged port",
1205 svcerr_weakauth(transp);
1208 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1209 syslog(LOG_ERR, "can't send reply");
1211 del_mlist(host, NULL);
1212 del_mlist(numerichost, NULL);
1215 "umountall request succeeded from %s",
1218 case MOUNTPROC_EXPORT:
1219 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1220 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1222 syslog(LOG_ERR, "can't send reply");
1225 "export request succeeded from %s",
1229 svcerr_noproc(transp);
1235 * Xdr conversion for a dirpath string
1238 xdr_dir(XDR *xdrsp, char *dirp)
1240 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1244 * Xdr routine to generate file handle reply
1247 xdr_fhs(XDR *xdrsp, caddr_t cp)
1249 struct fhreturn *fhrp = (struct fhreturn *)cp;
1250 u_long ok = 0, len, auth;
1253 if (!xdr_long(xdrsp, &ok))
1255 switch (fhrp->fhr_vers) {
1257 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1260 if (!xdr_long(xdrsp, &len))
1262 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1264 if (fhrp->fhr_numsecflavors) {
1265 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1267 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1268 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1274 if (!xdr_long(xdrsp, &len))
1276 return (xdr_long(xdrsp, &auth));
1283 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1285 struct mountlist *mlp;
1292 if (!xdr_bool(xdrsp, &true))
1294 strp = &mlp->ml_host[0];
1295 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1297 strp = &mlp->ml_dirp[0];
1298 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1302 if (!xdr_bool(xdrsp, &false))
1308 * Xdr conversion for export list
1311 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1313 struct exportlist *ep;
1316 sigset_t sighup_mask;
1318 sigemptyset(&sighup_mask);
1319 sigaddset(&sighup_mask, SIGHUP);
1320 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1324 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1327 if (ep->ex_defdir && putdef == 0 &&
1328 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1333 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1334 if (!xdr_bool(xdrsp, &false))
1338 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1343 * Called from xdr_explist() to traverse the tree and export the
1347 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1350 struct grouplist *grp;
1351 struct hostlist *hp;
1358 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1360 if (!xdr_bool(xdrsp, &true))
1363 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1365 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1370 if (!xdr_bool(xdrsp, &true))
1373 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1375 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1376 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1380 if (grp->gr_type == GT_HOST) {
1381 if (!xdr_bool(xdrsp, &true))
1383 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1384 if (!xdr_string(xdrsp, &strp,
1387 } else if (grp->gr_type == GT_NET) {
1388 if (!xdr_bool(xdrsp, &true))
1390 strp = grp->gr_ptr.gt_net.nt_name;
1391 if (!xdr_string(xdrsp, &strp,
1396 if (gotalldir && hp == (struct hostlist *)NULL) {
1402 if (!xdr_bool(xdrsp, &false))
1404 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1411 xdr_explist(XDR *xdrsp, caddr_t cp)
1414 return xdr_explist_common(xdrsp, cp, 0);
1418 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1421 return xdr_explist_common(xdrsp, cp, 1);
1429 * Get the export list from one, currently open file
1432 get_exportlist_one(void)
1434 struct exportlist *ep, *ep2;
1435 struct grouplist *grp, *tgrp;
1436 struct exportlist **epp;
1437 struct dirlist *dirhead;
1440 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1441 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1444 dirhead = (struct dirlist *)NULL;
1445 while (get_line()) {
1447 warnx("got line %s", line);
1449 nextfield(&cp, &endcp);
1458 exflags = MNT_EXPORTED;
1461 ep = (struct exportlist *)NULL;
1465 * Handle the V4 root dir.
1467 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1469 * V4: just indicates that it is the v4 root point,
1470 * so skip over that and set v4root_phase.
1472 if (v4root_phase > 0) {
1473 syslog(LOG_ERR, "V4:duplicate line, ignored");
1478 nextfield(&cp, &endcp);
1482 * Create new exports list entry
1485 tgrp = grp = get_grp();
1487 if (len > MNTNAMLEN) {
1488 getexp_err(ep, tgrp);
1492 if (ep == (struct exportlist *)NULL) {
1493 getexp_err(ep, tgrp);
1497 warnx("doing opt %s", cp);
1499 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1501 getexp_err(ep, tgrp);
1504 } else if (*cp == '/') {
1507 if (v4root_phase > 1) {
1509 syslog(LOG_ERR, "Multiple V4 dirs");
1510 getexp_err(ep, tgrp);
1514 if (check_dirpath(cp) &&
1515 statfs(cp, &fsb) >= 0) {
1516 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1517 syslog(LOG_ERR, "Warning: exporting of "
1518 "automounted fs %s not supported", cp);
1520 syslog(LOG_ERR, "dirs must be first");
1521 getexp_err(ep, tgrp);
1524 if (v4root_phase == 1) {
1526 syslog(LOG_ERR, "Multiple V4 dirs");
1527 getexp_err(ep, tgrp);
1530 if (strlen(v4root_dirpath) == 0) {
1531 strlcpy(v4root_dirpath, cp,
1532 sizeof (v4root_dirpath));
1533 } else if (strcmp(v4root_dirpath, cp)
1536 "different V4 dirpath %s", cp);
1537 getexp_err(ep, tgrp);
1546 if (ep->ex_fs.val[0] !=
1547 fsb.f_fsid.val[0] ||
1549 fsb.f_fsid.val[1]) {
1550 getexp_err(ep, tgrp);
1555 * See if this directory is already
1558 ep = ex_search(&fsb.f_fsid);
1559 if (ep == (struct exportlist *)NULL) {
1561 ep->ex_fs = fsb.f_fsid;
1562 ep->ex_fsdir = (char *)malloc
1563 (strlen(fsb.f_mntonname) + 1);
1565 strcpy(ep->ex_fsdir,
1571 "making new ep fs=0x%x,0x%x",
1575 warnx("found ep fs=0x%x,0x%x",
1581 * Add dirpath to export mount point.
1583 dirp = add_expdir(&dirhead, cp, len);
1587 getexp_err(ep, tgrp);
1595 if (ep == (struct exportlist *)NULL) {
1596 getexp_err(ep, tgrp);
1601 * Get the host or netgroup.
1604 netgrp = getnetgrent(&hst, &usr, &dom);
1607 grp->gr_next = get_grp();
1613 "null hostname in netgroup %s, skipping", cp);
1614 grp->gr_type = GT_IGNORE;
1615 } else if (get_host(hst, grp, tgrp)) {
1617 "bad host %s in netgroup %s, skipping", hst, cp);
1618 grp->gr_type = GT_IGNORE;
1620 } else if (get_host(cp, grp, tgrp)) {
1621 syslog(LOG_ERR, "bad host %s, skipping", cp);
1622 grp->gr_type = GT_IGNORE;
1625 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1630 nextfield(&cp, &endcp);
1633 if (check_options(dirhead)) {
1634 getexp_err(ep, tgrp);
1638 grp->gr_type = GT_DEFAULT;
1640 warnx("adding a default entry");
1643 * Don't allow a network export coincide with a list of
1644 * host(s) on the same line.
1646 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1647 syslog(LOG_ERR, "network/host conflict");
1648 getexp_err(ep, tgrp);
1652 * If an export list was specified on this line, make sure
1653 * that we have at least one valid entry, otherwise skip it.
1657 while (grp && grp->gr_type == GT_IGNORE)
1660 getexp_err(ep, tgrp);
1665 if (v4root_phase == 1) {
1666 syslog(LOG_ERR, "V4:root, no dirp, ignored");
1667 getexp_err(ep, tgrp);
1672 * Loop through hosts, pushing the exports into the kernel.
1673 * After loop, tgrp points to the start of the list and
1674 * grp points to the last entry in the list.
1678 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1680 getexp_err(ep, tgrp);
1683 } while (grp->gr_next && (grp = grp->gr_next));
1686 * For V4: don't enter in mount lists.
1688 if (v4root_phase > 0 && v4root_phase <= 2) {
1690 * Since these structures aren't used by mountd,
1695 while (tgrp != NULL) {
1697 tgrp = tgrp->gr_next;
1704 * Success. Update the data structures.
1707 hang_dirp(dirhead, tgrp, ep, opt_flags);
1708 grp->gr_next = grphead;
1711 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1715 dirhead = (struct dirlist *)NULL;
1716 if ((ep->ex_flag & EX_LINKED) == 0) {
1721 * Insert in the list in alphabetical order.
1723 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1724 epp = &ep2->ex_next;
1730 ep->ex_flag |= EX_LINKED;
1736 dirhead = (struct dirlist *)NULL;
1742 * Get the export list from all specified files
1745 get_exportlist(void)
1747 struct exportlist *ep, *ep2;
1748 struct grouplist *grp, *tgrp;
1749 struct export_args export;
1751 struct statfs *fsp, *mntbufp;
1752 struct xvfsconf vfc;
1757 struct nfsex_args eargs;
1759 if (suspend_nfsd != 0)
1760 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1761 v4root_dirpath[0] = '\0';
1762 bzero(&export, sizeof(export));
1763 export.ex_flags = MNT_DELEXPORT;
1766 bzero(errmsg, sizeof(errmsg));
1769 * First, get rid of the old list
1777 exphead = (struct exportlist *)NULL;
1785 grphead = (struct grouplist *)NULL;
1788 * and the old V4 root dir.
1790 bzero(&eargs, sizeof (eargs));
1791 eargs.export.ex_flags = MNT_DELEXPORT;
1792 if (run_v4server > 0 &&
1793 nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1795 syslog(LOG_ERR, "Can't delete exports for V4:");
1798 * and clear flag that notes if a public fh has been exported.
1803 * And delete exports that are in the kernel for all local
1805 * XXX: Should know how to handle all local exportable filesystems.
1807 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1810 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1811 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1812 build_iovec(&iov, &iovlen, "from", NULL, 0);
1813 build_iovec(&iov, &iovlen, "update", NULL, 0);
1814 build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1815 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1818 for (i = 0; i < num; i++) {
1820 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1821 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1827 * We do not need to delete "export" flag from
1828 * filesystems that do not have it set.
1830 if (!(fsp->f_flags & MNT_EXPORTED))
1833 * Do not delete export for network filesystem by
1834 * passing "export" arg to nmount().
1835 * It only makes sense to do this for local filesystems.
1837 if (vfc.vfc_flags & VFCF_NETWORK)
1840 iov[1].iov_base = fsp->f_fstypename;
1841 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1842 iov[3].iov_base = fsp->f_mntonname;
1843 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1844 iov[5].iov_base = fsp->f_mntfromname;
1845 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1849 * EXDEV is returned when path exists but is not a
1850 * mount point. May happens if raced with unmount.
1852 if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1853 errno != ENOENT && errno != ENOTSUP && errno != EXDEV) {
1855 "can't delete exports for %s: %m %s",
1856 fsp->f_mntonname, errmsg);
1861 /* Free strings allocated by strdup() in getmntopts.c */
1862 free(iov[0].iov_base); /* fstype */
1863 free(iov[2].iov_base); /* fspath */
1864 free(iov[4].iov_base); /* from */
1865 free(iov[6].iov_base); /* update */
1866 free(iov[8].iov_base); /* export */
1867 free(iov[10].iov_base); /* errmsg */
1869 /* free iov, allocated by realloc() */
1875 * Read in the exports file and build the list, calling
1876 * nmount() as we go along to push the export rules into the kernel.
1879 for (i = 0; exnames[i] != NULL; i++) {
1881 warnx("reading exports from %s", exnames[i]);
1882 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1883 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1886 get_exportlist_one();
1891 syslog(LOG_ERR, "can't open any exports file");
1896 * If there was no public fh, clear any previous one set.
1898 if (run_v4server > 0 && has_publicfh == 0)
1899 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1901 /* Resume the nfsd. If they weren't suspended, this is harmless. */
1902 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1906 * Allocate an export list element
1911 struct exportlist *ep;
1913 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
1914 if (ep == (struct exportlist *)NULL)
1920 * Allocate a group list element
1925 struct grouplist *gp;
1927 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
1928 if (gp == (struct grouplist *)NULL)
1934 * Clean up upon an error in get_exportlist().
1937 getexp_err(struct exportlist *ep, struct grouplist *grp)
1939 struct grouplist *tgrp;
1941 if (!(opt_flags & OP_QUIET))
1942 syslog(LOG_ERR, "bad exports list line %s", line);
1943 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1953 * Search the export list for a matching fs.
1956 ex_search(fsid_t *fsid)
1958 struct exportlist *ep;
1962 if (ep->ex_fs.val[0] == fsid->val[0] &&
1963 ep->ex_fs.val[1] == fsid->val[1])
1971 * Add a directory path to the list.
1974 add_expdir(struct dirlist **dpp, char *cp, int len)
1978 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
1979 if (dp == (struct dirlist *)NULL)
1982 dp->dp_right = (struct dirlist *)NULL;
1984 dp->dp_hosts = (struct hostlist *)NULL;
1985 strcpy(dp->dp_dirp, cp);
1987 return (dp->dp_dirp);
1991 * Hang the dir list element off the dirpath binary tree as required
1992 * and update the entry for host.
1995 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1998 struct hostlist *hp;
1999 struct dirlist *dp2;
2001 if (flags & OP_ALLDIRS) {
2006 if (grp == (struct grouplist *)NULL) {
2007 ep->ex_defdir->dp_flag |= DP_DEFSET;
2008 /* Save the default security flavors list. */
2009 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2010 if (ep->ex_numsecflavors > 0)
2011 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2012 sizeof(ep->ex_secflavors));
2013 } else while (grp) {
2016 hp->ht_next = ep->ex_defdir->dp_hosts;
2017 ep->ex_defdir->dp_hosts = hp;
2018 /* Save the security flavors list for this host set. */
2019 grp->gr_numsecflavors = ep->ex_numsecflavors;
2020 if (ep->ex_numsecflavors > 0)
2021 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2022 sizeof(ep->ex_secflavors));
2028 * Loop through the directories adding them to the tree.
2032 add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
2039 * Traverse the binary tree either updating a node that is already there
2040 * for the new directory or adding the new node.
2043 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2044 int flags, struct exportlist *ep)
2047 struct hostlist *hp;
2052 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2054 add_dlist(&dp->dp_left, newdp, grp, flags, ep);
2056 } else if (cmp < 0) {
2057 add_dlist(&dp->dp_right, newdp, grp, flags, ep);
2060 free((caddr_t)newdp);
2063 dp->dp_left = (struct dirlist *)NULL;
2069 * Hang all of the host(s) off of the directory point.
2074 hp->ht_next = dp->dp_hosts;
2076 /* Save the security flavors list for this host set. */
2077 grp->gr_numsecflavors = ep->ex_numsecflavors;
2078 if (ep->ex_numsecflavors > 0)
2079 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2080 sizeof(ep->ex_secflavors));
2084 dp->dp_flag |= DP_DEFSET;
2085 /* Save the default security flavors list. */
2086 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2087 if (ep->ex_numsecflavors > 0)
2088 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2089 sizeof(ep->ex_secflavors));
2094 * Search for a dirpath on the export point.
2097 dirp_search(struct dirlist *dp, char *dirp)
2102 cmp = strcmp(dp->dp_dirp, dirp);
2104 return (dirp_search(dp->dp_left, dirp));
2106 return (dirp_search(dp->dp_right, dirp));
2114 * Scan for a host match in a directory tree.
2117 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2118 int *hostsetp, int *numsecflavors, int **secflavorsp)
2120 struct hostlist *hp;
2121 struct grouplist *grp;
2122 struct addrinfo *ai;
2125 if (dp->dp_flag & DP_DEFSET)
2126 *defsetp = dp->dp_flag;
2130 switch (grp->gr_type) {
2132 ai = grp->gr_ptr.gt_addrinfo;
2133 for (; ai; ai = ai->ai_next) {
2134 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2136 (hp->ht_flag | DP_HOSTSET);
2137 if (numsecflavors != NULL) {
2139 grp->gr_numsecflavors;
2148 if (!sacmp(saddr, (struct sockaddr *)
2149 &grp->gr_ptr.gt_net.nt_net,
2151 &grp->gr_ptr.gt_net.nt_mask)) {
2152 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2153 if (numsecflavors != NULL) {
2155 grp->gr_numsecflavors;
2170 * Scan tree for a host that matches the address.
2173 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2175 int defset, hostset;
2178 if (scan_tree(dp->dp_left, saddr))
2180 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2182 if (scan_tree(dp->dp_right, saddr))
2189 * Traverse the dirlist tree and free it up.
2192 free_dir(struct dirlist *dp)
2196 free_dir(dp->dp_left);
2197 free_dir(dp->dp_right);
2198 free_host(dp->dp_hosts);
2204 * Parse a colon separated list of security flavors
2207 parsesec(char *seclist, struct exportlist *ep)
2212 ep->ex_numsecflavors = 0;
2214 cp = strchr(seclist, ':');
2220 if (!strcmp(seclist, "sys"))
2222 else if (!strcmp(seclist, "krb5"))
2223 flavor = RPCSEC_GSS_KRB5;
2224 else if (!strcmp(seclist, "krb5i"))
2225 flavor = RPCSEC_GSS_KRB5I;
2226 else if (!strcmp(seclist, "krb5p"))
2227 flavor = RPCSEC_GSS_KRB5P;
2231 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2234 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2237 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2240 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2241 ep->ex_numsecflavors++;
2253 * Parse the option string and update fields.
2254 * Option arguments may either be -<option>=<value> or
2258 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2259 int *has_hostp, int *exflagsp, struct xucred *cr)
2261 char *cpoptarg, *cpoptend;
2262 char *cp, *endcp, *cpopt, savedc, savedc2;
2263 int allflag, usedarg;
2271 while (cpopt && *cpopt) {
2274 if ((cpoptend = strchr(cpopt, ','))) {
2276 if ((cpoptarg = strchr(cpopt, '=')))
2279 if ((cpoptarg = strchr(cpopt, '=')))
2283 nextfield(&cp, &endcp);
2285 if (endcp > cp && *cp != '-') {
2293 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2294 *exflagsp |= MNT_EXRDONLY;
2295 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2296 !(allflag = strcmp(cpopt, "mapall")) ||
2297 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2299 parsecred(cpoptarg, cr);
2301 *exflagsp |= MNT_EXPORTANON;
2302 opt_flags |= OP_MAPALL;
2304 opt_flags |= OP_MAPROOT;
2305 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2306 !strcmp(cpopt, "m"))) {
2307 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2308 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2312 opt_flags |= OP_MASK;
2313 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2314 !strcmp(cpopt, "n"))) {
2315 if (strchr(cpoptarg, '/') != NULL) {
2317 fprintf(stderr, "setting OP_MASKLEN\n");
2318 opt_flags |= OP_MASKLEN;
2320 if (grp->gr_type != GT_NULL) {
2321 syslog(LOG_ERR, "network/host conflict");
2323 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2324 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2327 grp->gr_type = GT_NET;
2330 opt_flags |= OP_NET;
2331 } else if (!strcmp(cpopt, "alldirs")) {
2332 opt_flags |= OP_ALLDIRS;
2333 } else if (!strcmp(cpopt, "public")) {
2334 *exflagsp |= MNT_EXPUBLIC;
2335 } else if (!strcmp(cpopt, "webnfs")) {
2336 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2337 opt_flags |= OP_MAPALL;
2338 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2339 ep->ex_indexfile = strdup(cpoptarg);
2340 } else if (!strcmp(cpopt, "quiet")) {
2341 opt_flags |= OP_QUIET;
2342 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2343 if (parsesec(cpoptarg, ep))
2345 opt_flags |= OP_SEC;
2348 syslog(LOG_ERR, "bad opt %s", cpopt);
2367 * Translate a character string to the corresponding list of network
2368 * addresses for a hostname.
2371 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2373 struct grouplist *checkgrp;
2374 struct addrinfo *ai, *tai, hints;
2376 char host[NI_MAXHOST];
2378 if (grp->gr_type != GT_NULL) {
2379 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2382 memset(&hints, 0, sizeof hints);
2383 hints.ai_flags = AI_CANONNAME;
2384 hints.ai_protocol = IPPROTO_UDP;
2385 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2387 syslog(LOG_ERR,"can't get address info for host %s", cp);
2390 grp->gr_ptr.gt_addrinfo = ai;
2391 while (ai != NULL) {
2392 if (ai->ai_canonname == NULL) {
2393 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2394 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2395 strlcpy(host, "?", sizeof(host));
2396 ai->ai_canonname = strdup(host);
2397 ai->ai_flags |= AI_CANONNAME;
2400 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2402 * Sanity check: make sure we don't already have an entry
2403 * for this host in the grouplist.
2405 for (checkgrp = tgrp; checkgrp != NULL;
2406 checkgrp = checkgrp->gr_next) {
2407 if (checkgrp->gr_type != GT_HOST)
2409 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2410 tai = tai->ai_next) {
2411 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2415 "ignoring duplicate host %s\n",
2417 grp->gr_type = GT_IGNORE;
2423 grp->gr_type = GT_HOST;
2428 * Free up an exports list component
2431 free_exp(struct exportlist *ep)
2434 if (ep->ex_defdir) {
2435 free_host(ep->ex_defdir->dp_hosts);
2436 free((caddr_t)ep->ex_defdir);
2440 if (ep->ex_indexfile)
2441 free(ep->ex_indexfile);
2442 free_dir(ep->ex_dirl);
2450 free_host(struct hostlist *hp)
2452 struct hostlist *hp2;
2464 struct hostlist *hp;
2466 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2467 if (hp == (struct hostlist *)NULL)
2469 hp->ht_next = (struct hostlist *)NULL;
2475 * Out of memory, fatal
2481 syslog(LOG_ERR, "out of memory");
2486 * Do the nmount() syscall with the update flag to push the export info into
2490 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2491 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
2494 struct addrinfo *ai;
2495 struct export_args ea, *eap;
2503 struct nfsex_args nfsea;
2505 if (run_v4server > 0)
2506 eap = &nfsea.export;
2516 bzero(eap, sizeof (struct export_args));
2517 bzero(errmsg, sizeof(errmsg));
2518 eap->ex_flags = exflags;
2519 eap->ex_anon = *anoncrp;
2520 eap->ex_indexfile = ep->ex_indexfile;
2521 if (grp->gr_type == GT_HOST)
2522 ai = grp->gr_ptr.gt_addrinfo;
2525 eap->ex_numsecflavors = ep->ex_numsecflavors;
2526 for (i = 0; i < eap->ex_numsecflavors; i++)
2527 eap->ex_secflavors[i] = ep->ex_secflavors[i];
2528 if (eap->ex_numsecflavors == 0) {
2529 eap->ex_numsecflavors = 1;
2530 eap->ex_secflavors[0] = AUTH_SYS;
2534 if (v4root_phase == 0) {
2535 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2536 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2537 build_iovec(&iov, &iovlen, "from", NULL, 0);
2538 build_iovec(&iov, &iovlen, "update", NULL, 0);
2539 build_iovec(&iov, &iovlen, "export", eap,
2540 sizeof (struct export_args));
2541 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2545 switch (grp->gr_type) {
2547 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
2549 eap->ex_addr = ai->ai_addr;
2550 eap->ex_addrlen = ai->ai_addrlen;
2551 eap->ex_masklen = 0;
2554 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
2558 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2560 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2562 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2563 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
2566 eap->ex_addr = NULL;
2567 eap->ex_addrlen = 0;
2568 eap->ex_mask = NULL;
2569 eap->ex_masklen = 0;
2576 syslog(LOG_ERR, "bad grouptype");
2584 * For V4:, use the nfssvc() syscall, instead of mount().
2586 if (v4root_phase == 2) {
2587 nfsea.fspec = v4root_dirpath;
2588 if (run_v4server > 0 &&
2589 nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2590 syslog(LOG_ERR, "Exporting V4: failed");
2596 * Maybe I should just use the fsb->f_mntonname path
2597 * instead of looping back up the dirp to the mount
2599 * Also, needs to know how to export all types of local
2600 * exportable filesystems and not just "ufs".
2602 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2603 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2604 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2605 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2606 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2607 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2610 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2614 cp = dirp + dirplen - 1;
2615 if (opt_flags & OP_QUIET) {
2619 if (errno == EPERM) {
2621 warnx("can't change attributes for %s: %s",
2624 "can't change attributes for %s: %s",
2629 if (opt_flags & OP_ALLDIRS) {
2630 if (errno == EINVAL)
2632 "-alldirs requested but %s is not a filesystem mountpoint",
2636 "could not remount %s: %m",
2641 /* back up over the last component */
2642 while (*cp == '/' && cp > dirp)
2644 while (*(cp - 1) != '/' && cp > dirp)
2648 warnx("mnt unsucc");
2649 syslog(LOG_ERR, "can't export %s %s",
2657 * Check that we're still on the same
2660 if (statfs(dirp, &fsb1) != 0 ||
2661 bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2662 sizeof (fsb1.f_fsid)) != 0) {
2665 "can't export %s %s", dirp,
2674 * For the experimental server:
2675 * If this is the public directory, get the file handle
2676 * and load it into the kernel via the nfssvc() syscall.
2678 if (run_v4server > 0 && (exflags & MNT_EXPUBLIC) != 0) {
2682 if (eap->ex_indexfile != NULL)
2683 public_name = eap->ex_indexfile;
2686 if (getfh(public_name, &fh) < 0)
2688 "Can't get public fh for %s", public_name);
2689 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2691 "Can't set public fh for %s", public_name);
2704 /* free strings allocated by strdup() in getmntopts.c */
2706 free(iov[0].iov_base); /* fstype */
2707 free(iov[2].iov_base); /* fspath */
2708 free(iov[4].iov_base); /* from */
2709 free(iov[6].iov_base); /* update */
2710 free(iov[8].iov_base); /* export */
2711 free(iov[10].iov_base); /* errmsg */
2713 /* free iov, allocated by realloc() */
2720 * Translate a net address.
2722 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2725 get_net(char *cp, struct netmsk *net, int maskflg)
2727 struct netent *np = NULL;
2728 char *name, *p, *prefp;
2729 struct sockaddr_in sin;
2730 struct sockaddr *sa = NULL;
2731 struct addrinfo hints, *ai = NULL;
2732 char netname[NI_MAXHOST];
2736 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2737 p = strchr(cp, '/');
2743 * Check for a numeric address first. We wish to avoid
2744 * possible DNS lookups in getnetbyname().
2746 if (isxdigit(*cp) || *cp == ':') {
2747 memset(&hints, 0, sizeof hints);
2748 /* Ensure the mask and the network have the same family. */
2749 if (maskflg && (opt_flags & OP_NET))
2750 hints.ai_family = net->nt_net.ss_family;
2751 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2752 hints.ai_family = net->nt_mask.ss_family;
2754 hints.ai_family = AF_UNSPEC;
2755 hints.ai_flags = AI_NUMERICHOST;
2756 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2758 if (sa != NULL && ai->ai_family == AF_INET) {
2760 * The address in `cp' is really a network address, so
2761 * use inet_network() to re-interpret this correctly.
2762 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2764 bzero(&sin, sizeof sin);
2765 sin.sin_family = AF_INET;
2766 sin.sin_len = sizeof sin;
2767 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2769 fprintf(stderr, "get_net: v4 addr %s\n",
2770 inet_ntoa(sin.sin_addr));
2771 sa = (struct sockaddr *)&sin;
2774 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2775 bzero(&sin, sizeof sin);
2776 sin.sin_family = AF_INET;
2777 sin.sin_len = sizeof sin;
2778 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2779 sa = (struct sockaddr *)&sin;
2785 /* The specified sockaddr is a mask. */
2786 if (checkmask(sa) != 0)
2788 bcopy(sa, &net->nt_mask, sa->sa_len);
2789 opt_flags |= OP_HAVEMASK;
2791 /* The specified sockaddr is a network address. */
2792 bcopy(sa, &net->nt_net, sa->sa_len);
2794 /* Get a network name for the export list. */
2797 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2798 NULL, 0, NI_NUMERICHOST) == 0) {
2803 if ((net->nt_name = strdup(name)) == NULL)
2807 * Extract a mask from either a "/<masklen>" suffix, or
2808 * from the class of an IPv4 address.
2810 if (opt_flags & OP_MASKLEN) {
2811 preflen = strtol(prefp, NULL, 10);
2812 if (preflen < 0L || preflen == LONG_MAX)
2814 bcopy(sa, &net->nt_mask, sa->sa_len);
2815 if (makemask(&net->nt_mask, (int)preflen) != 0)
2817 opt_flags |= OP_HAVEMASK;
2819 } else if (sa->sa_family == AF_INET &&
2820 (opt_flags & OP_MASK) == 0) {
2823 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2824 if (IN_CLASSA(addr))
2826 else if (IN_CLASSB(addr))
2828 else if (IN_CLASSC(addr))
2830 else if (IN_CLASSD(addr))
2833 preflen = 32; /* XXX */
2835 bcopy(sa, &net->nt_mask, sa->sa_len);
2836 makemask(&net->nt_mask, (int)preflen);
2837 opt_flags |= OP_HAVEMASK;
2852 * Parse out the next white space separated field
2855 nextfield(char **cp, char **endcp)
2860 while (*p == ' ' || *p == '\t')
2862 if (*p == '\n' || *p == '\0')
2866 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2873 * Get an exports file line. Skip over blank lines and handle line
2881 int totlen, cont_line;
2884 * Loop around ignoring blank lines and getting all continuation lines.
2889 if ((p = fgetln(exp_file, &len)) == NULL)
2894 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2904 if (linesize < len + totlen + 1) {
2905 linesize = len + totlen + 1;
2906 line = realloc(line, linesize);
2910 memcpy(line + totlen, p, len);
2912 line[totlen] = '\0';
2913 } while (totlen == 0 || cont_line);
2918 * Parse a description of a credential.
2921 parsecred(char *namelist, struct xucred *cr)
2928 gid_t groups[XU_NGROUPS + 1];
2931 cr->cr_version = XUCRED_VERSION;
2933 * Set up the unprivileged user.
2936 cr->cr_groups[0] = -2;
2939 * Get the user's password table entry.
2941 names = strsep_quote(&namelist, " \t\n");
2942 name = strsep(&names, ":");
2943 /* Bug? name could be NULL here */
2944 if (isdigit(*name) || *name == '-')
2945 pw = getpwuid(atoi(name));
2947 pw = getpwnam(name);
2949 * Credentials specified as those of a user.
2951 if (names == NULL) {
2953 syslog(LOG_ERR, "unknown user: %s", name);
2956 cr->cr_uid = pw->pw_uid;
2957 ngroups = XU_NGROUPS + 1;
2958 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) {
2959 syslog(LOG_ERR, "too many groups");
2960 ngroups = XU_NGROUPS + 1;
2964 * Compress out duplicate.
2966 cr->cr_ngroups = ngroups - 1;
2967 cr->cr_groups[0] = groups[0];
2968 for (cnt = 2; cnt < ngroups; cnt++)
2969 cr->cr_groups[cnt - 1] = groups[cnt];
2973 * Explicit credential specified as a colon separated list:
2977 cr->cr_uid = pw->pw_uid;
2978 else if (isdigit(*name) || *name == '-')
2979 cr->cr_uid = atoi(name);
2981 syslog(LOG_ERR, "unknown user: %s", name);
2985 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
2986 name = strsep(&names, ":");
2987 if (isdigit(*name) || *name == '-') {
2988 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2990 if ((gr = getgrnam(name)) == NULL) {
2991 syslog(LOG_ERR, "unknown group: %s", name);
2994 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2997 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
2998 syslog(LOG_ERR, "too many groups");
3001 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
3003 * Routines that maintain the remote mounttab
3008 struct mountlist *mlp, **mlpp;
3009 char *host, *dirp, *cp;
3013 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
3014 if (errno == ENOENT)
3017 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
3022 while (fgets(str, STRSIZ, mlfile) != NULL) {
3024 host = strsep(&cp, " \t\n");
3025 dirp = strsep(&cp, " \t\n");
3026 if (host == NULL || dirp == NULL)
3028 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3029 if (mlp == (struct mountlist *)NULL)
3031 strncpy(mlp->ml_host, host, MNTNAMLEN);
3032 mlp->ml_host[MNTNAMLEN] = '\0';
3033 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3034 mlp->ml_dirp[MNTPATHLEN] = '\0';
3035 mlp->ml_next = (struct mountlist *)NULL;
3037 mlpp = &mlp->ml_next;
3043 del_mlist(char *hostp, char *dirp)
3045 struct mountlist *mlp, **mlpp;
3046 struct mountlist *mlp2;
3053 if (!strcmp(mlp->ml_host, hostp) &&
3054 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3057 *mlpp = mlp = mlp->ml_next;
3058 free((caddr_t)mlp2);
3060 mlpp = &mlp->ml_next;
3065 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3066 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3071 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3079 add_mlist(char *hostp, char *dirp)
3081 struct mountlist *mlp, **mlpp;
3087 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3089 mlpp = &mlp->ml_next;
3092 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3093 if (mlp == (struct mountlist *)NULL)
3095 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3096 mlp->ml_host[MNTNAMLEN] = '\0';
3097 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3098 mlp->ml_dirp[MNTPATHLEN] = '\0';
3099 mlp->ml_next = (struct mountlist *)NULL;
3101 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3102 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3105 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3110 * Free up a group list.
3113 free_grp(struct grouplist *grp)
3115 if (grp->gr_type == GT_HOST) {
3116 if (grp->gr_ptr.gt_addrinfo != NULL)
3117 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3118 } else if (grp->gr_type == GT_NET) {
3119 if (grp->gr_ptr.gt_net.nt_name)
3120 free(grp->gr_ptr.gt_net.nt_name);
3127 SYSLOG(int pri, const char *fmt, ...)
3132 vfprintf(stderr, fmt, ap);
3138 * Check options for consistency.
3141 check_options(struct dirlist *dp)
3144 if (v4root_phase == 0 && dp == NULL)
3146 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3147 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3150 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3151 syslog(LOG_ERR, "-mask requires -network");
3154 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3155 syslog(LOG_ERR, "-network requires mask specification");
3158 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3159 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3162 if (v4root_phase > 0 &&
3164 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3165 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3168 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3169 syslog(LOG_ERR, "-alldirs has multiple directories");
3176 * Check an absolute directory path for any symbolic links. Return true
3179 check_dirpath(char *dirp)
3186 while (*cp && ret) {
3189 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3195 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3201 * Make a netmask according to the specified prefix length. The ss_family
3202 * and other non-address fields must be initialised before calling this.
3205 makemask(struct sockaddr_storage *ssp, int bitlen)
3210 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3212 if (bitlen > len * CHAR_BIT)
3215 for (i = 0; i < len; i++) {
3216 bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
3217 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3224 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3225 * is acceptable (i.e. of the form 1...10....0).
3228 checkmask(struct sockaddr *sa)
3233 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3236 for (i = 0; i < len; i++)
3237 if (mask[i] != 0xff)
3240 if (~mask[i] & (u_char)(~mask[i] + 1))
3244 for (; i < len; i++)
3251 * Compare two sockaddrs according to a specified mask. Return zero if
3252 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3253 * If samask is NULL, perform a full comparison.
3256 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3258 unsigned char *p1, *p2, *mask;
3261 if (sa1->sa_family != sa2->sa_family ||
3262 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3263 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3266 switch (sa1->sa_family) {
3268 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3269 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3274 /* Simple binary comparison if no mask specified. */
3276 return (memcmp(p1, p2, len));
3278 /* Set up the mask, and do a mask-based comparison. */
3279 if (sa1->sa_family != samask->sa_family ||
3280 (mask = sa_rawaddr(samask, NULL)) == NULL)
3283 for (i = 0; i < len; i++)
3284 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3290 * Return a pointer to the part of the sockaddr that contains the
3291 * raw address, and set *nbytes to its length in bytes. Returns
3292 * NULL if the address family is unknown.
3295 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3299 switch (sa->sa_family) {
3301 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3302 p = &((struct sockaddr_in *)sa)->sin_addr;
3305 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3306 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3319 huphandler(int sig __unused)
3324 void terminate(int sig __unused)
3326 pidfile_remove(pfh);
3327 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3328 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);