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 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.nfsrv.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 = (char *)malloc
1529 (strlen(fsb.f_mntonname) + 1);
1531 strcpy(ep->ex_fsdir,
1537 "making new ep fs=0x%x,0x%x",
1541 warnx("found ep fs=0x%x,0x%x",
1547 * Add dirpath to export mount point.
1549 dirp = add_expdir(&dirhead, cp, len);
1553 getexp_err(ep, tgrp);
1561 if (ep == (struct exportlist *)NULL) {
1562 getexp_err(ep, tgrp);
1567 * Get the host or netgroup.
1570 netgrp = getnetgrent(&hst, &usr, &dom);
1573 grp->gr_next = get_grp();
1579 "null hostname in netgroup %s, skipping", cp);
1580 grp->gr_type = GT_IGNORE;
1581 } else if (get_host(hst, grp, tgrp)) {
1583 "bad host %s in netgroup %s, skipping", hst, cp);
1584 grp->gr_type = GT_IGNORE;
1586 } else if (get_host(cp, grp, tgrp)) {
1587 syslog(LOG_ERR, "bad host %s, skipping", cp);
1588 grp->gr_type = GT_IGNORE;
1591 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1596 nextfield(&cp, &endcp);
1599 if (check_options(dirhead)) {
1600 getexp_err(ep, tgrp);
1604 grp->gr_type = GT_DEFAULT;
1606 warnx("adding a default entry");
1609 * Don't allow a network export coincide with a list of
1610 * host(s) on the same line.
1612 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1613 syslog(LOG_ERR, "network/host conflict");
1614 getexp_err(ep, tgrp);
1618 * If an export list was specified on this line, make sure
1619 * that we have at least one valid entry, otherwise skip it.
1623 while (grp && grp->gr_type == GT_IGNORE)
1626 getexp_err(ep, tgrp);
1631 if (v4root_phase == 1) {
1632 syslog(LOG_ERR, "V4:root, no dirp, ignored");
1633 getexp_err(ep, tgrp);
1638 * Loop through hosts, pushing the exports into the kernel.
1639 * After loop, tgrp points to the start of the list and
1640 * grp points to the last entry in the list.
1644 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1646 getexp_err(ep, tgrp);
1649 } while (grp->gr_next && (grp = grp->gr_next));
1652 * For V4: don't enter in mount lists.
1654 if (v4root_phase > 0 && v4root_phase <= 2) {
1656 * Since these structures aren't used by mountd,
1661 while (tgrp != NULL) {
1663 tgrp = tgrp->gr_next;
1670 * Success. Update the data structures.
1673 hang_dirp(dirhead, tgrp, ep, opt_flags);
1674 grp->gr_next = grphead;
1677 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1681 dirhead = (struct dirlist *)NULL;
1682 if ((ep->ex_flag & EX_LINKED) == 0) {
1687 * Insert in the list in alphabetical order.
1689 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1690 epp = &ep2->ex_next;
1696 ep->ex_flag |= EX_LINKED;
1702 dirhead = (struct dirlist *)NULL;
1708 * Get the export list from all specified files
1711 get_exportlist(void)
1713 struct exportlist *ep, *ep2;
1714 struct grouplist *grp, *tgrp;
1715 struct export_args export;
1717 struct statfs *fsp, *mntbufp;
1718 struct xvfsconf vfc;
1723 struct nfsex_args eargs;
1725 if (suspend_nfsd != 0)
1726 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1727 v4root_dirpath[0] = '\0';
1728 bzero(&export, sizeof(export));
1729 export.ex_flags = MNT_DELEXPORT;
1732 bzero(errmsg, sizeof(errmsg));
1735 * First, get rid of the old list
1743 exphead = (struct exportlist *)NULL;
1751 grphead = (struct grouplist *)NULL;
1754 * and the old V4 root dir.
1756 bzero(&eargs, sizeof (eargs));
1757 eargs.export.ex_flags = MNT_DELEXPORT;
1758 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1760 syslog(LOG_ERR, "Can't delete exports for V4:");
1763 * and clear flag that notes if a public fh has been exported.
1768 * And delete exports that are in the kernel for all local
1770 * XXX: Should know how to handle all local exportable filesystems.
1772 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1775 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1776 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1777 build_iovec(&iov, &iovlen, "from", NULL, 0);
1778 build_iovec(&iov, &iovlen, "update", NULL, 0);
1779 build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1780 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1783 for (i = 0; i < num; i++) {
1785 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1786 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1792 * We do not need to delete "export" flag from
1793 * filesystems that do not have it set.
1795 if (!(fsp->f_flags & MNT_EXPORTED))
1798 * Do not delete export for network filesystem by
1799 * passing "export" arg to nmount().
1800 * It only makes sense to do this for local filesystems.
1802 if (vfc.vfc_flags & VFCF_NETWORK)
1805 iov[1].iov_base = fsp->f_fstypename;
1806 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1807 iov[3].iov_base = fsp->f_mntonname;
1808 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1809 iov[5].iov_base = fsp->f_mntfromname;
1810 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1814 * EXDEV is returned when path exists but is not a
1815 * mount point. May happens if raced with unmount.
1817 if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1818 errno != ENOENT && errno != ENOTSUP && errno != EXDEV) {
1820 "can't delete exports for %s: %m %s",
1821 fsp->f_mntonname, errmsg);
1826 /* Free strings allocated by strdup() in getmntopts.c */
1827 free(iov[0].iov_base); /* fstype */
1828 free(iov[2].iov_base); /* fspath */
1829 free(iov[4].iov_base); /* from */
1830 free(iov[6].iov_base); /* update */
1831 free(iov[8].iov_base); /* export */
1832 free(iov[10].iov_base); /* errmsg */
1834 /* free iov, allocated by realloc() */
1840 * Read in the exports file and build the list, calling
1841 * nmount() as we go along to push the export rules into the kernel.
1844 for (i = 0; exnames[i] != NULL; i++) {
1846 warnx("reading exports from %s", exnames[i]);
1847 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1848 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1851 get_exportlist_one();
1856 syslog(LOG_ERR, "can't open any exports file");
1861 * If there was no public fh, clear any previous one set.
1863 if (has_publicfh == 0)
1864 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1866 /* Resume the nfsd. If they weren't suspended, this is harmless. */
1867 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1871 * Allocate an export list element
1873 static struct exportlist *
1876 struct exportlist *ep;
1878 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
1879 if (ep == (struct exportlist *)NULL)
1885 * Allocate a group list element
1887 static struct grouplist *
1890 struct grouplist *gp;
1892 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
1893 if (gp == (struct grouplist *)NULL)
1899 * Clean up upon an error in get_exportlist().
1902 getexp_err(struct exportlist *ep, struct grouplist *grp)
1904 struct grouplist *tgrp;
1906 if (!(opt_flags & OP_QUIET))
1907 syslog(LOG_ERR, "bad exports list line %s", line);
1908 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1918 * Search the export list for a matching fs.
1920 static struct exportlist *
1921 ex_search(fsid_t *fsid)
1923 struct exportlist *ep;
1927 if (ep->ex_fs.val[0] == fsid->val[0] &&
1928 ep->ex_fs.val[1] == fsid->val[1])
1936 * Add a directory path to the list.
1939 add_expdir(struct dirlist **dpp, char *cp, int len)
1943 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
1944 if (dp == (struct dirlist *)NULL)
1947 dp->dp_right = (struct dirlist *)NULL;
1949 dp->dp_hosts = (struct hostlist *)NULL;
1950 strcpy(dp->dp_dirp, cp);
1952 return (dp->dp_dirp);
1956 * Hang the dir list element off the dirpath binary tree as required
1957 * and update the entry for host.
1960 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1963 struct hostlist *hp;
1964 struct dirlist *dp2;
1966 if (flags & OP_ALLDIRS) {
1971 if (grp == (struct grouplist *)NULL) {
1972 ep->ex_defdir->dp_flag |= DP_DEFSET;
1973 /* Save the default security flavors list. */
1974 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1975 if (ep->ex_numsecflavors > 0)
1976 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1977 sizeof(ep->ex_secflavors));
1978 } else while (grp) {
1981 hp->ht_next = ep->ex_defdir->dp_hosts;
1982 ep->ex_defdir->dp_hosts = hp;
1983 /* Save the security flavors list for this host set. */
1984 grp->gr_numsecflavors = ep->ex_numsecflavors;
1985 if (ep->ex_numsecflavors > 0)
1986 memcpy(grp->gr_secflavors, ep->ex_secflavors,
1987 sizeof(ep->ex_secflavors));
1993 * Loop through the directories adding them to the tree.
1997 add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
2004 * Traverse the binary tree either updating a node that is already there
2005 * for the new directory or adding the new node.
2008 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2009 int flags, struct exportlist *ep)
2012 struct hostlist *hp;
2017 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2019 add_dlist(&dp->dp_left, newdp, grp, flags, ep);
2021 } else if (cmp < 0) {
2022 add_dlist(&dp->dp_right, newdp, grp, flags, ep);
2025 free((caddr_t)newdp);
2028 dp->dp_left = (struct dirlist *)NULL;
2034 * Hang all of the host(s) off of the directory point.
2039 hp->ht_next = dp->dp_hosts;
2041 /* Save the security flavors list for this host set. */
2042 grp->gr_numsecflavors = ep->ex_numsecflavors;
2043 if (ep->ex_numsecflavors > 0)
2044 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2045 sizeof(ep->ex_secflavors));
2049 dp->dp_flag |= DP_DEFSET;
2050 /* Save the default security flavors list. */
2051 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2052 if (ep->ex_numsecflavors > 0)
2053 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2054 sizeof(ep->ex_secflavors));
2059 * Search for a dirpath on the export point.
2061 static struct dirlist *
2062 dirp_search(struct dirlist *dp, char *dirp)
2067 cmp = strcmp(dp->dp_dirp, dirp);
2069 return (dirp_search(dp->dp_left, dirp));
2071 return (dirp_search(dp->dp_right, dirp));
2079 * Scan for a host match in a directory tree.
2082 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2083 int *hostsetp, int *numsecflavors, int **secflavorsp)
2085 struct hostlist *hp;
2086 struct grouplist *grp;
2087 struct addrinfo *ai;
2090 if (dp->dp_flag & DP_DEFSET)
2091 *defsetp = dp->dp_flag;
2095 switch (grp->gr_type) {
2097 ai = grp->gr_ptr.gt_addrinfo;
2098 for (; ai; ai = ai->ai_next) {
2099 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2101 (hp->ht_flag | DP_HOSTSET);
2102 if (numsecflavors != NULL) {
2104 grp->gr_numsecflavors;
2113 if (!sacmp(saddr, (struct sockaddr *)
2114 &grp->gr_ptr.gt_net.nt_net,
2116 &grp->gr_ptr.gt_net.nt_mask)) {
2117 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2118 if (numsecflavors != NULL) {
2120 grp->gr_numsecflavors;
2135 * Scan tree for a host that matches the address.
2138 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2140 int defset, hostset;
2143 if (scan_tree(dp->dp_left, saddr))
2145 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2147 if (scan_tree(dp->dp_right, saddr))
2154 * Traverse the dirlist tree and free it up.
2157 free_dir(struct dirlist *dp)
2161 free_dir(dp->dp_left);
2162 free_dir(dp->dp_right);
2163 free_host(dp->dp_hosts);
2169 * Parse a colon separated list of security flavors
2172 parsesec(char *seclist, struct exportlist *ep)
2177 ep->ex_numsecflavors = 0;
2179 cp = strchr(seclist, ':');
2185 if (!strcmp(seclist, "sys"))
2187 else if (!strcmp(seclist, "krb5"))
2188 flavor = RPCSEC_GSS_KRB5;
2189 else if (!strcmp(seclist, "krb5i"))
2190 flavor = RPCSEC_GSS_KRB5I;
2191 else if (!strcmp(seclist, "krb5p"))
2192 flavor = RPCSEC_GSS_KRB5P;
2196 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2199 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2202 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2205 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2206 ep->ex_numsecflavors++;
2218 * Parse the option string and update fields.
2219 * Option arguments may either be -<option>=<value> or
2223 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2224 int *has_hostp, int *exflagsp, struct xucred *cr)
2226 char *cpoptarg, *cpoptend;
2227 char *cp, *endcp, *cpopt, savedc, savedc2;
2228 int allflag, usedarg;
2236 while (cpopt && *cpopt) {
2239 if ((cpoptend = strchr(cpopt, ','))) {
2241 if ((cpoptarg = strchr(cpopt, '=')))
2244 if ((cpoptarg = strchr(cpopt, '=')))
2248 nextfield(&cp, &endcp);
2250 if (endcp > cp && *cp != '-') {
2258 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2259 *exflagsp |= MNT_EXRDONLY;
2260 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2261 !(allflag = strcmp(cpopt, "mapall")) ||
2262 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2264 parsecred(cpoptarg, cr);
2266 *exflagsp |= MNT_EXPORTANON;
2267 opt_flags |= OP_MAPALL;
2269 opt_flags |= OP_MAPROOT;
2270 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2271 !strcmp(cpopt, "m"))) {
2272 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2273 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2277 opt_flags |= OP_MASK;
2278 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2279 !strcmp(cpopt, "n"))) {
2280 if (strchr(cpoptarg, '/') != NULL) {
2282 fprintf(stderr, "setting OP_MASKLEN\n");
2283 opt_flags |= OP_MASKLEN;
2285 if (grp->gr_type != GT_NULL) {
2286 syslog(LOG_ERR, "network/host conflict");
2288 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2289 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2292 grp->gr_type = GT_NET;
2295 opt_flags |= OP_NET;
2296 } else if (!strcmp(cpopt, "alldirs")) {
2297 opt_flags |= OP_ALLDIRS;
2298 } else if (!strcmp(cpopt, "public")) {
2299 *exflagsp |= MNT_EXPUBLIC;
2300 } else if (!strcmp(cpopt, "webnfs")) {
2301 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2302 opt_flags |= OP_MAPALL;
2303 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2304 ep->ex_indexfile = strdup(cpoptarg);
2305 } else if (!strcmp(cpopt, "quiet")) {
2306 opt_flags |= OP_QUIET;
2307 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2308 if (parsesec(cpoptarg, ep))
2310 opt_flags |= OP_SEC;
2313 syslog(LOG_ERR, "bad opt %s", cpopt);
2332 * Translate a character string to the corresponding list of network
2333 * addresses for a hostname.
2336 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2338 struct grouplist *checkgrp;
2339 struct addrinfo *ai, *tai, hints;
2341 char host[NI_MAXHOST];
2343 if (grp->gr_type != GT_NULL) {
2344 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2347 memset(&hints, 0, sizeof hints);
2348 hints.ai_flags = AI_CANONNAME;
2349 hints.ai_protocol = IPPROTO_UDP;
2350 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2352 syslog(LOG_ERR,"can't get address info for host %s", cp);
2355 grp->gr_ptr.gt_addrinfo = ai;
2356 while (ai != NULL) {
2357 if (ai->ai_canonname == NULL) {
2358 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2359 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2360 strlcpy(host, "?", sizeof(host));
2361 ai->ai_canonname = strdup(host);
2362 ai->ai_flags |= AI_CANONNAME;
2365 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2367 * Sanity check: make sure we don't already have an entry
2368 * for this host in the grouplist.
2370 for (checkgrp = tgrp; checkgrp != NULL;
2371 checkgrp = checkgrp->gr_next) {
2372 if (checkgrp->gr_type != GT_HOST)
2374 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2375 tai = tai->ai_next) {
2376 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2380 "ignoring duplicate host %s\n",
2382 grp->gr_type = GT_IGNORE;
2388 grp->gr_type = GT_HOST;
2393 * Free up an exports list component
2396 free_exp(struct exportlist *ep)
2399 if (ep->ex_defdir) {
2400 free_host(ep->ex_defdir->dp_hosts);
2401 free((caddr_t)ep->ex_defdir);
2405 if (ep->ex_indexfile)
2406 free(ep->ex_indexfile);
2407 free_dir(ep->ex_dirl);
2415 free_host(struct hostlist *hp)
2417 struct hostlist *hp2;
2426 static struct hostlist *
2429 struct hostlist *hp;
2431 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2432 if (hp == (struct hostlist *)NULL)
2434 hp->ht_next = (struct hostlist *)NULL;
2440 * Out of memory, fatal
2446 syslog(LOG_ERR, "out of memory");
2451 * Do the nmount() syscall with the update flag to push the export info into
2455 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2456 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
2459 struct addrinfo *ai;
2460 struct export_args *eap;
2468 struct nfsex_args nfsea;
2470 eap = &nfsea.export;
2478 bzero(eap, sizeof (struct export_args));
2479 bzero(errmsg, sizeof(errmsg));
2480 eap->ex_flags = exflags;
2481 eap->ex_anon = *anoncrp;
2482 eap->ex_indexfile = ep->ex_indexfile;
2483 if (grp->gr_type == GT_HOST)
2484 ai = grp->gr_ptr.gt_addrinfo;
2487 eap->ex_numsecflavors = ep->ex_numsecflavors;
2488 for (i = 0; i < eap->ex_numsecflavors; i++)
2489 eap->ex_secflavors[i] = ep->ex_secflavors[i];
2490 if (eap->ex_numsecflavors == 0) {
2491 eap->ex_numsecflavors = 1;
2492 eap->ex_secflavors[0] = AUTH_SYS;
2496 if (v4root_phase == 0) {
2497 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2498 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2499 build_iovec(&iov, &iovlen, "from", NULL, 0);
2500 build_iovec(&iov, &iovlen, "update", NULL, 0);
2501 build_iovec(&iov, &iovlen, "export", eap,
2502 sizeof (struct export_args));
2503 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2507 switch (grp->gr_type) {
2509 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
2511 eap->ex_addr = ai->ai_addr;
2512 eap->ex_addrlen = ai->ai_addrlen;
2513 eap->ex_masklen = 0;
2516 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
2520 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2522 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2524 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2525 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
2528 eap->ex_addr = NULL;
2529 eap->ex_addrlen = 0;
2530 eap->ex_mask = NULL;
2531 eap->ex_masklen = 0;
2538 syslog(LOG_ERR, "bad grouptype");
2546 * For V4:, use the nfssvc() syscall, instead of mount().
2548 if (v4root_phase == 2) {
2549 nfsea.fspec = v4root_dirpath;
2550 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2551 syslog(LOG_ERR, "Exporting V4: failed");
2557 * Maybe I should just use the fsb->f_mntonname path
2558 * instead of looping back up the dirp to the mount
2560 * Also, needs to know how to export all types of local
2561 * exportable filesystems and not just "ufs".
2563 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2564 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2565 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2566 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2567 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2568 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2571 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2575 cp = dirp + dirplen - 1;
2576 if (opt_flags & OP_QUIET) {
2580 if (errno == EPERM) {
2582 warnx("can't change attributes for %s: %s",
2585 "can't change attributes for %s: %s",
2590 if (opt_flags & OP_ALLDIRS) {
2591 if (errno == EINVAL)
2593 "-alldirs requested but %s is not a filesystem mountpoint",
2597 "could not remount %s: %m",
2602 /* back up over the last component */
2603 while (*cp == '/' && cp > dirp)
2605 while (*(cp - 1) != '/' && cp > dirp)
2609 warnx("mnt unsucc");
2610 syslog(LOG_ERR, "can't export %s %s",
2618 * Check that we're still on the same
2621 if (statfs(dirp, &fsb1) != 0 ||
2622 bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2623 sizeof (fsb1.f_fsid)) != 0) {
2626 "can't export %s %s", dirp,
2635 * For the experimental server:
2636 * If this is the public directory, get the file handle
2637 * and load it into the kernel via the nfssvc() syscall.
2639 if ((exflags & MNT_EXPUBLIC) != 0) {
2643 if (eap->ex_indexfile != NULL)
2644 public_name = eap->ex_indexfile;
2647 if (getfh(public_name, &fh) < 0)
2649 "Can't get public fh for %s", public_name);
2650 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2652 "Can't set public fh for %s", public_name);
2665 /* free strings allocated by strdup() in getmntopts.c */
2667 free(iov[0].iov_base); /* fstype */
2668 free(iov[2].iov_base); /* fspath */
2669 free(iov[4].iov_base); /* from */
2670 free(iov[6].iov_base); /* update */
2671 free(iov[8].iov_base); /* export */
2672 free(iov[10].iov_base); /* errmsg */
2674 /* free iov, allocated by realloc() */
2681 * Translate a net address.
2683 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2686 get_net(char *cp, struct netmsk *net, int maskflg)
2688 struct netent *np = NULL;
2689 char *name, *p, *prefp;
2690 struct sockaddr_in sin;
2691 struct sockaddr *sa = NULL;
2692 struct addrinfo hints, *ai = NULL;
2693 char netname[NI_MAXHOST];
2697 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2698 p = strchr(cp, '/');
2704 * Check for a numeric address first. We wish to avoid
2705 * possible DNS lookups in getnetbyname().
2707 if (isxdigit(*cp) || *cp == ':') {
2708 memset(&hints, 0, sizeof hints);
2709 /* Ensure the mask and the network have the same family. */
2710 if (maskflg && (opt_flags & OP_NET))
2711 hints.ai_family = net->nt_net.ss_family;
2712 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2713 hints.ai_family = net->nt_mask.ss_family;
2715 hints.ai_family = AF_UNSPEC;
2716 hints.ai_flags = AI_NUMERICHOST;
2717 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2719 if (sa != NULL && ai->ai_family == AF_INET) {
2721 * The address in `cp' is really a network address, so
2722 * use inet_network() to re-interpret this correctly.
2723 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2725 bzero(&sin, sizeof sin);
2726 sin.sin_family = AF_INET;
2727 sin.sin_len = sizeof sin;
2728 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2730 fprintf(stderr, "get_net: v4 addr %s\n",
2731 inet_ntoa(sin.sin_addr));
2732 sa = (struct sockaddr *)&sin;
2735 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2736 bzero(&sin, sizeof sin);
2737 sin.sin_family = AF_INET;
2738 sin.sin_len = sizeof sin;
2739 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2740 sa = (struct sockaddr *)&sin;
2746 /* The specified sockaddr is a mask. */
2747 if (checkmask(sa) != 0)
2749 bcopy(sa, &net->nt_mask, sa->sa_len);
2750 opt_flags |= OP_HAVEMASK;
2752 /* The specified sockaddr is a network address. */
2753 bcopy(sa, &net->nt_net, sa->sa_len);
2755 /* Get a network name for the export list. */
2758 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2759 NULL, 0, NI_NUMERICHOST) == 0) {
2764 if ((net->nt_name = strdup(name)) == NULL)
2768 * Extract a mask from either a "/<masklen>" suffix, or
2769 * from the class of an IPv4 address.
2771 if (opt_flags & OP_MASKLEN) {
2772 preflen = strtol(prefp, NULL, 10);
2773 if (preflen < 0L || preflen == LONG_MAX)
2775 bcopy(sa, &net->nt_mask, sa->sa_len);
2776 if (makemask(&net->nt_mask, (int)preflen) != 0)
2778 opt_flags |= OP_HAVEMASK;
2780 } else if (sa->sa_family == AF_INET &&
2781 (opt_flags & OP_MASK) == 0) {
2784 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2785 if (IN_CLASSA(addr))
2787 else if (IN_CLASSB(addr))
2789 else if (IN_CLASSC(addr))
2791 else if (IN_CLASSD(addr))
2794 preflen = 32; /* XXX */
2796 bcopy(sa, &net->nt_mask, sa->sa_len);
2797 makemask(&net->nt_mask, (int)preflen);
2798 opt_flags |= OP_HAVEMASK;
2813 * Parse out the next white space separated field
2816 nextfield(char **cp, char **endcp)
2821 while (*p == ' ' || *p == '\t')
2823 if (*p == '\n' || *p == '\0')
2827 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2834 * Get an exports file line. Skip over blank lines and handle line
2842 int totlen, cont_line;
2845 * Loop around ignoring blank lines and getting all continuation lines.
2850 if ((p = fgetln(exp_file, &len)) == NULL)
2855 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2865 if (linesize < len + totlen + 1) {
2866 linesize = len + totlen + 1;
2867 line = realloc(line, linesize);
2871 memcpy(line + totlen, p, len);
2873 line[totlen] = '\0';
2874 } while (totlen == 0 || cont_line);
2879 * Parse a description of a credential.
2882 parsecred(char *namelist, struct xucred *cr)
2889 gid_t groups[XU_NGROUPS + 1];
2892 cr->cr_version = XUCRED_VERSION;
2894 * Set up the unprivileged user.
2897 cr->cr_groups[0] = -2;
2900 * Get the user's password table entry.
2902 names = strsep_quote(&namelist, " \t\n");
2903 name = strsep(&names, ":");
2904 /* Bug? name could be NULL here */
2905 if (isdigit(*name) || *name == '-')
2906 pw = getpwuid(atoi(name));
2908 pw = getpwnam(name);
2910 * Credentials specified as those of a user.
2912 if (names == NULL) {
2914 syslog(LOG_ERR, "unknown user: %s", name);
2917 cr->cr_uid = pw->pw_uid;
2918 ngroups = XU_NGROUPS + 1;
2919 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2920 syslog(LOG_ERR, "too many groups");
2922 * Compress out duplicate.
2924 cr->cr_ngroups = ngroups - 1;
2925 cr->cr_groups[0] = groups[0];
2926 for (cnt = 2; cnt < ngroups; cnt++)
2927 cr->cr_groups[cnt - 1] = groups[cnt];
2931 * Explicit credential specified as a colon separated list:
2935 cr->cr_uid = pw->pw_uid;
2936 else if (isdigit(*name) || *name == '-')
2937 cr->cr_uid = atoi(name);
2939 syslog(LOG_ERR, "unknown user: %s", name);
2943 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
2944 name = strsep(&names, ":");
2945 if (isdigit(*name) || *name == '-') {
2946 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2948 if ((gr = getgrnam(name)) == NULL) {
2949 syslog(LOG_ERR, "unknown group: %s", name);
2952 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2955 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
2956 syslog(LOG_ERR, "too many groups");
2959 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
2961 * Routines that maintain the remote mounttab
2966 struct mountlist *mlp, **mlpp;
2967 char *host, *dirp, *cp;
2971 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2972 if (errno == ENOENT)
2975 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2980 while (fgets(str, STRSIZ, mlfile) != NULL) {
2982 host = strsep(&cp, " \t\n");
2983 dirp = strsep(&cp, " \t\n");
2984 if (host == NULL || dirp == NULL)
2986 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2987 if (mlp == (struct mountlist *)NULL)
2989 strncpy(mlp->ml_host, host, MNTNAMLEN);
2990 mlp->ml_host[MNTNAMLEN] = '\0';
2991 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2992 mlp->ml_dirp[MNTPATHLEN] = '\0';
2993 mlp->ml_next = (struct mountlist *)NULL;
2995 mlpp = &mlp->ml_next;
3001 del_mlist(char *hostp, char *dirp)
3003 struct mountlist *mlp, **mlpp;
3004 struct mountlist *mlp2;
3011 if (!strcmp(mlp->ml_host, hostp) &&
3012 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3015 *mlpp = mlp = mlp->ml_next;
3016 free((caddr_t)mlp2);
3018 mlpp = &mlp->ml_next;
3023 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3024 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3029 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3037 add_mlist(char *hostp, char *dirp)
3039 struct mountlist *mlp, **mlpp;
3045 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3047 mlpp = &mlp->ml_next;
3050 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3051 if (mlp == (struct mountlist *)NULL)
3053 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3054 mlp->ml_host[MNTNAMLEN] = '\0';
3055 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3056 mlp->ml_dirp[MNTPATHLEN] = '\0';
3057 mlp->ml_next = (struct mountlist *)NULL;
3059 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3060 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3063 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3068 * Free up a group list.
3071 free_grp(struct grouplist *grp)
3073 if (grp->gr_type == GT_HOST) {
3074 if (grp->gr_ptr.gt_addrinfo != NULL)
3075 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3076 } else if (grp->gr_type == GT_NET) {
3077 if (grp->gr_ptr.gt_net.nt_name)
3078 free(grp->gr_ptr.gt_net.nt_name);
3085 SYSLOG(int pri, const char *fmt, ...)
3090 vfprintf(stderr, fmt, ap);
3096 * Check options for consistency.
3099 check_options(struct dirlist *dp)
3102 if (v4root_phase == 0 && dp == NULL)
3104 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3105 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3108 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3109 syslog(LOG_ERR, "-mask requires -network");
3112 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3113 syslog(LOG_ERR, "-network requires mask specification");
3116 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3117 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3120 if (v4root_phase > 0 &&
3122 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3123 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3126 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3127 syslog(LOG_ERR, "-alldirs has multiple directories");
3134 * Check an absolute directory path for any symbolic links. Return true
3137 check_dirpath(char *dirp)
3144 while (*cp && ret) {
3147 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3153 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3159 * Make a netmask according to the specified prefix length. The ss_family
3160 * and other non-address fields must be initialised before calling this.
3163 makemask(struct sockaddr_storage *ssp, int bitlen)
3168 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3170 if (bitlen > len * CHAR_BIT)
3173 for (i = 0; i < len; i++) {
3174 bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
3175 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3182 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3183 * is acceptable (i.e. of the form 1...10....0).
3186 checkmask(struct sockaddr *sa)
3191 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3194 for (i = 0; i < len; i++)
3195 if (mask[i] != 0xff)
3198 if (~mask[i] & (u_char)(~mask[i] + 1))
3202 for (; i < len; i++)
3209 * Compare two sockaddrs according to a specified mask. Return zero if
3210 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3211 * If samask is NULL, perform a full comparison.
3214 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3216 unsigned char *p1, *p2, *mask;
3219 if (sa1->sa_family != sa2->sa_family ||
3220 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3221 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3224 switch (sa1->sa_family) {
3226 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3227 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3232 /* Simple binary comparison if no mask specified. */
3234 return (memcmp(p1, p2, len));
3236 /* Set up the mask, and do a mask-based comparison. */
3237 if (sa1->sa_family != samask->sa_family ||
3238 (mask = sa_rawaddr(samask, NULL)) == NULL)
3241 for (i = 0; i < len; i++)
3242 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3248 * Return a pointer to the part of the sockaddr that contains the
3249 * raw address, and set *nbytes to its length in bytes. Returns
3250 * NULL if the address family is unknown.
3253 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3257 switch (sa->sa_family) {
3259 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3260 p = &((struct sockaddr_in *)sa)->sin_addr;
3263 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3264 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3277 huphandler(int sig __unused)
3284 terminate(int sig __unused)
3286 pidfile_remove(pfh);
3287 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3288 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);