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 int create_service(struct netconfig *nconf);
178 static void complete_service(struct netconfig *nconf, char *port_str);
179 static void clearout_service(void);
180 void del_mlist(char *hostp, char *dirp);
181 struct dirlist *dirp_search(struct dirlist *, char *);
182 int do_mount(struct exportlist *, struct grouplist *, int,
183 struct xucred *, char *, int, struct statfs *);
184 int do_opt(char **, char **, struct exportlist *, struct grouplist *,
185 int *, int *, struct xucred *);
186 struct exportlist *ex_search(fsid_t *);
187 struct exportlist *get_exp(void);
188 void free_dir(struct dirlist *);
189 void free_exp(struct exportlist *);
190 void free_grp(struct grouplist *);
191 void free_host(struct hostlist *);
192 void get_exportlist(void);
193 int get_host(char *, struct grouplist *, struct grouplist *);
194 struct hostlist *get_ht(void);
196 void get_mountlist(void);
197 int get_net(char *, struct netmsk *, int);
198 void getexp_err(struct exportlist *, struct grouplist *);
199 struct grouplist *get_grp(void);
200 void hang_dirp(struct dirlist *, struct grouplist *,
201 struct exportlist *, int);
202 void huphandler(int sig);
203 int makemask(struct sockaddr_storage *ssp, int bitlen);
204 void mntsrv(struct svc_req *, SVCXPRT *);
205 void nextfield(char **, char **);
206 void out_of_mem(void);
207 void parsecred(char *, struct xucred *);
208 int parsesec(char *, struct exportlist *);
209 int put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
210 void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
211 int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
212 struct sockaddr *samask);
213 int scan_tree(struct dirlist *, struct sockaddr *);
214 static void usage(void);
215 int xdr_dir(XDR *, char *);
216 int xdr_explist(XDR *, caddr_t);
217 int xdr_explist_brief(XDR *, caddr_t);
218 int xdr_explist_common(XDR *, caddr_t, int);
219 int xdr_fhs(XDR *, caddr_t);
220 int xdr_mlist(XDR *, caddr_t);
223 struct exportlist *exphead;
224 struct mountlist *mlhead;
225 struct grouplist *grphead;
226 char *exnames_default[2] = { _PATH_EXPORTS, NULL };
229 struct xucred def_anon = {
237 int resvport_only = 1;
244 char *svcport_str = NULL;
245 static int mallocd_svcport = 0;
247 static int sock_fdcnt;
248 static int sock_fdpos;
249 static int suspend_nfsd = 0;
252 static int have_v6 = 1;
254 int v4root_phase = 0;
255 char v4root_dirpath[PATH_MAX + 1];
256 int run_v4server = 1;
257 int has_publicfh = 0;
259 struct pidfh *pfh = NULL;
260 /* Bits for opt_flags above */
261 #define OP_MAPROOT 0x01
262 #define OP_MAPALL 0x02
266 #define OP_ALLDIRS 0x40
267 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
268 #define OP_QUIET 0x100
269 #define OP_MASKLEN 0x200
274 void SYSLOG(int, const char *, ...) __printflike(2, 3);
275 #define syslog SYSLOG
281 * Mountd server for NFS mount protocol as described in:
282 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
283 * The optional arguments are the exports file name
284 * default: _PATH_EXPORTS
285 * and "-n" to allow nonroot mount.
288 main(int argc, char **argv)
291 struct netconfig *nconf;
292 char *endptr, **hosts_bak;
297 int maxrec = RPC_MAXDATASIZE;
298 int attempt_cnt, port_len, port_pos, ret;
301 /* Check that another mountd isn't already running. */
302 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
305 errx(1, "mountd already running, pid: %d.", otherpid);
306 warn("cannot open or create pidfile");
309 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
315 while ((c = getopt(argc, argv, "2deh:lnop:rS")) != -1)
321 /* now a no-op, since this is the default */
330 debug = debug ? 0 : 1;
340 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
341 if (endptr == NULL || *endptr != '\0' ||
342 svcport == 0 || svcport >= IPPORT_MAX)
344 svcport_str = strdup(optarg);
349 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
350 if (hosts_bak == NULL) {
352 for (k = 0; k < nhosts; k++)
359 hosts[nhosts - 1] = strdup(optarg);
360 if (hosts[nhosts - 1] == NULL) {
361 for (k = 0; k < (nhosts - 1); k++)
375 * Unless the "-o" option was specified, try and run "nfsd".
376 * If "-o" was specified, try and run "nfsserver".
378 if (run_v4server > 0) {
379 if (modfind("nfsd") < 0) {
380 /* Not present in kernel, try loading it */
381 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
382 errx(1, "NFS server is not available");
384 } else if (modfind("nfsserver") < 0) {
385 /* Not present in kernel, try loading it */
386 if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
387 errx(1, "NFS server is not available");
392 grphead = (struct grouplist *)NULL;
393 exphead = (struct exportlist *)NULL;
394 mlhead = (struct mountlist *)NULL;
398 exnames = exnames_default;
399 openlog("mountd", LOG_PID, LOG_DAEMON);
401 warnx("getting export list");
404 warnx("getting mount list");
410 signal(SIGINT, SIG_IGN);
411 signal(SIGQUIT, SIG_IGN);
413 signal(SIGHUP, huphandler);
414 signal(SIGTERM, terminate);
415 signal(SIGPIPE, SIG_IGN);
419 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
420 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
421 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
423 if (!resvport_only) {
424 if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
425 &resvport_only, sizeof(resvport_only)) != 0 &&
427 syslog(LOG_ERR, "sysctl: %m");
433 * If no hosts were specified, add a wildcard entry to bind to
434 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
438 hosts = malloc(sizeof(char**));
446 hosts_bak = realloc(hosts, (nhosts + 2) *
448 if (hosts_bak == NULL) {
449 for (k = 0; k < nhosts; k++)
456 hosts[nhosts - 2] = "::1";
458 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
459 if (hosts_bak == NULL) {
460 for (k = 0; k < nhosts; k++)
470 hosts[nhosts - 1] = "127.0.0.1";
478 nc_handle = setnetconfig();
479 while ((nconf = getnetconfig(nc_handle))) {
480 if (nconf->nc_flag & NC_VISIBLE) {
481 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
485 ret = create_service(nconf);
487 /* Ignore this call */
491 * Failed to bind port, so close off
492 * all sockets created and try again
493 * if the port# was dynamically
494 * assigned via bind(2).
497 if (mallocd_svcport != 0 &&
498 attempt_cnt < GETPORT_MAXTRY) {
505 "bindresvport_sa: %m");
509 /* Start over at the first service. */
513 nc_handle = setnetconfig();
515 } else if (mallocd_svcport != 0 &&
516 attempt_cnt == GETPORT_MAXTRY) {
518 * For the last attempt, allow
519 * different port #s for each nconf
520 * by saving the svcport_str and
521 * setting it back to NULL.
523 port_list = realloc(port_list,
524 (port_len + 1) * sizeof(char *));
525 if (port_list == NULL)
527 port_list[port_len++] = svcport_str;
536 * Successfully bound the ports, so call complete_service() to
537 * do the rest of the setup on the service(s).
541 nc_handle = setnetconfig();
542 while ((nconf = getnetconfig(nc_handle))) {
543 if (nconf->nc_flag & NC_VISIBLE) {
544 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
547 } else if (port_list != NULL) {
548 if (port_pos >= port_len) {
549 syslog(LOG_ERR, "too many port#s");
552 complete_service(nconf, port_list[port_pos++]);
554 complete_service(nconf, svcport_str);
557 endnetconfig(nc_handle);
559 if (port_list != NULL) {
560 for (port_pos = 0; port_pos < port_len; port_pos++)
561 free(port_list[port_pos]);
566 syslog(LOG_ERR, "could not create any services");
570 /* Expand svc_run() here so that we can call get_exportlist(). */
577 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
581 syslog(LOG_ERR, "mountd died: select: %m");
586 svc_getreqset(&readfds);
592 * This routine creates and binds sockets on the appropriate
593 * addresses. It gets called one time for each transport.
594 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
595 * bind failed with EADDRINUSE.
596 * Any file descriptors that have been created are stored in sock_fd and
597 * the total count of them is maintained in sock_fdcnt.
600 create_service(struct netconfig *nconf)
602 struct addrinfo hints, *res = NULL;
603 struct sockaddr_in *sin;
604 struct sockaddr_in6 *sin6;
605 struct __rpc_sockinfo si;
611 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
614 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
615 (nconf->nc_semantics != NC_TPI_COTS) &&
616 (nconf->nc_semantics != NC_TPI_COTS_ORD))
617 return (1); /* not my type */
620 * XXX - using RPC library internal functions.
622 if (!__rpc_nconf2sockinfo(nconf, &si)) {
623 syslog(LOG_ERR, "cannot get information for %s",
628 /* Get mountd's address on this transport */
629 memset(&hints, 0, sizeof hints);
630 hints.ai_flags = AI_PASSIVE;
631 hints.ai_family = si.si_af;
632 hints.ai_socktype = si.si_socktype;
633 hints.ai_protocol = si.si_proto;
636 * Bind to specific IPs if asked to
639 while (nhostsbak > 0) {
641 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
644 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
648 * XXX - using RPC library internal functions.
650 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
652 if (errno == EAFNOSUPPORT &&
653 nconf->nc_semantics != NC_TPI_CLTS)
656 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
657 "cannot create socket for %s", nconf->nc_netid);
663 switch (hints.ai_family) {
665 if (inet_pton(AF_INET, hosts[nhostsbak],
667 hints.ai_flags |= AI_NUMERICHOST;
670 * Skip if we have an AF_INET6 address.
672 if (inet_pton(AF_INET6, hosts[nhostsbak],
680 if (inet_pton(AF_INET6, hosts[nhostsbak],
682 hints.ai_flags |= AI_NUMERICHOST;
685 * Skip if we have an AF_INET address.
687 if (inet_pton(AF_INET, hosts[nhostsbak],
695 * We're doing host-based access checks here, so don't
696 * allow v4-in-v6 to confuse things. The kernel will
697 * disable it by default on NFS sockets too.
699 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
702 "can't disable v4-in-v6 on IPv6 socket");
711 * If no hosts were specified, just bind to INADDR_ANY
713 if (strcmp("*", hosts[nhostsbak]) == 0) {
714 if (svcport_str == NULL) {
715 res = malloc(sizeof(struct addrinfo));
719 res->ai_flags = hints.ai_flags;
720 res->ai_family = hints.ai_family;
721 res->ai_protocol = hints.ai_protocol;
722 switch (res->ai_family) {
724 sin = malloc(sizeof(struct sockaddr_in));
727 sin->sin_family = AF_INET;
728 sin->sin_port = htons(0);
729 sin->sin_addr.s_addr = htonl(INADDR_ANY);
730 res->ai_addr = (struct sockaddr*) sin;
731 res->ai_addrlen = (socklen_t)
732 sizeof(struct sockaddr_in);
735 sin6 = malloc(sizeof(struct sockaddr_in6));
738 sin6->sin6_family = AF_INET6;
739 sin6->sin6_port = htons(0);
740 sin6->sin6_addr = in6addr_any;
741 res->ai_addr = (struct sockaddr*) sin6;
742 res->ai_addrlen = (socklen_t)
743 sizeof(struct sockaddr_in6);
746 syslog(LOG_ERR, "bad addr fam %d",
751 if ((aicode = getaddrinfo(NULL, svcport_str,
752 &hints, &res)) != 0) {
754 "cannot get local address for %s: %s",
756 gai_strerror(aicode));
762 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
763 &hints, &res)) != 0) {
765 "cannot get local address for %s: %s",
766 nconf->nc_netid, gai_strerror(aicode));
773 sock_fd[sock_fdcnt - 1] = fd;
775 /* Now, attempt the bind. */
776 r = bindresvport_sa(fd, res->ai_addr);
778 if (errno == EADDRINUSE && mallocd_svcport != 0) {
779 if (mallocd_res != 0) {
786 syslog(LOG_ERR, "bindresvport_sa: %m");
790 if (svcport_str == NULL) {
791 svcport_str = malloc(NI_MAXSERV * sizeof(char));
792 if (svcport_str == NULL)
796 if (getnameinfo(res->ai_addr,
797 res->ai_addr->sa_len, NULL, NI_MAXHOST,
798 svcport_str, NI_MAXSERV * sizeof(char),
799 NI_NUMERICHOST | NI_NUMERICSERV))
800 errx(1, "Cannot get port number");
802 if (mallocd_res != 0) {
813 * Called after all the create_service() calls have succeeded, to complete
814 * the setup and registration.
817 complete_service(struct netconfig *nconf, char *port_str)
819 struct addrinfo hints, *res = NULL;
820 struct __rpc_sockinfo si;
821 struct netbuf servaddr;
822 SVCXPRT *transp = NULL;
823 int aicode, fd, nhostsbak;
826 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
827 (nconf->nc_semantics != NC_TPI_COTS) &&
828 (nconf->nc_semantics != NC_TPI_COTS_ORD))
829 return; /* not my type */
832 * XXX - using RPC library internal functions.
834 if (!__rpc_nconf2sockinfo(nconf, &si)) {
835 syslog(LOG_ERR, "cannot get information for %s",
841 while (nhostsbak > 0) {
843 if (sock_fdpos >= sock_fdcnt) {
844 /* Should never happen. */
845 syslog(LOG_ERR, "Ran out of socket fd's");
848 fd = sock_fd[sock_fdpos++];
852 if (nconf->nc_semantics != NC_TPI_CLTS)
853 listen(fd, SOMAXCONN);
855 if (nconf->nc_semantics == NC_TPI_CLTS )
856 transp = svc_dg_create(fd, 0, 0);
858 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
861 if (transp != (SVCXPRT *) NULL) {
862 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
865 "can't register %s MOUNTVERS service",
868 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
871 "can't register %s MOUNTVERS3 service",
875 syslog(LOG_WARNING, "can't create %s services",
878 if (registered == 0) {
880 memset(&hints, 0, sizeof hints);
881 hints.ai_flags = AI_PASSIVE;
882 hints.ai_family = si.si_af;
883 hints.ai_socktype = si.si_socktype;
884 hints.ai_protocol = si.si_proto;
886 if ((aicode = getaddrinfo(NULL, port_str, &hints,
888 syslog(LOG_ERR, "cannot get local address: %s",
889 gai_strerror(aicode));
893 servaddr.buf = malloc(res->ai_addrlen);
894 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
895 servaddr.len = res->ai_addrlen;
897 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
898 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
907 * Clear out sockets after a failure to bind one of them, so that the
908 * cycle of socket creation/binding can start anew.
911 clearout_service(void)
915 for (i = 0; i < sock_fdcnt; i++) {
916 if (sock_fd[i] >= 0) {
917 shutdown(sock_fd[i], SHUT_RDWR);
927 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
928 "[-S] [-h <bindip>] [export_file ...]\n");
933 * The mount rpc service
936 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
938 struct exportlist *ep;
943 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
944 int lookup_failed = 1;
945 struct sockaddr *saddr;
947 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
948 int bad = 0, defset, hostset;
949 sigset_t sighup_mask;
950 int numsecflavors, *secflavorsp;
952 sigemptyset(&sighup_mask);
953 sigaddset(&sighup_mask, SIGHUP);
954 saddr = svc_getrpccaller(transp)->buf;
955 switch (saddr->sa_family) {
957 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
960 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
963 syslog(LOG_ERR, "request from unknown address family");
966 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
968 getnameinfo(saddr, saddr->sa_len, numerichost,
969 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
970 switch (rqstp->rq_proc) {
972 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
973 syslog(LOG_ERR, "can't send reply");
976 if (sport >= IPPORT_RESERVED && resvport_only) {
978 "mount request from %s from unprivileged port",
980 svcerr_weakauth(transp);
983 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
984 syslog(LOG_NOTICE, "undecodable mount request from %s",
986 svcerr_decode(transp);
991 * Get the real pathname and make sure it is a directory
992 * or a regular file if the -r option was specified
995 if (realpath(rpcpath, dirpath) == NULL ||
996 stat(dirpath, &stb) < 0 ||
997 (!S_ISDIR(stb.st_mode) &&
998 (dir_only || !S_ISREG(stb.st_mode))) ||
999 statfs(dirpath, &fsb) < 0) {
1000 chdir("/"); /* Just in case realpath doesn't */
1002 "mount request from %s for non existent path %s",
1003 numerichost, dirpath);
1005 warnx("stat failed on %s", dirpath);
1006 bad = ENOENT; /* We will send error reply later */
1009 /* Check in the exports list */
1010 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1011 ep = ex_search(&fsb.f_fsid);
1012 hostset = defset = 0;
1013 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1014 &numsecflavors, &secflavorsp) ||
1015 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1016 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1018 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1019 scan_tree(ep->ex_dirl, saddr) == 0))) {
1021 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1023 syslog(LOG_ERR, "can't send reply");
1024 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1027 if (hostset & DP_HOSTSET) {
1028 fhr.fhr_flag = hostset;
1029 fhr.fhr_numsecflavors = numsecflavors;
1030 fhr.fhr_secflavors = secflavorsp;
1032 fhr.fhr_flag = defset;
1033 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1034 fhr.fhr_secflavors = ep->ex_defsecflavors;
1036 fhr.fhr_vers = rqstp->rq_vers;
1037 /* Get the file handle */
1038 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1039 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1041 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1042 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1044 syslog(LOG_ERR, "can't send reply");
1045 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1048 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1050 syslog(LOG_ERR, "can't send reply");
1052 add_mlist(host, dirpath);
1054 add_mlist(numerichost, dirpath);
1056 warnx("mount successful");
1059 "mount request succeeded from %s for %s",
1060 numerichost, dirpath);
1064 "mount request denied from %s for %s",
1065 numerichost, dirpath);
1068 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1070 syslog(LOG_ERR, "can't send reply");
1071 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1073 case MOUNTPROC_DUMP:
1074 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1075 syslog(LOG_ERR, "can't send reply");
1078 "dump request succeeded from %s",
1081 case MOUNTPROC_UMNT:
1082 if (sport >= IPPORT_RESERVED && resvport_only) {
1084 "umount request from %s from unprivileged port",
1086 svcerr_weakauth(transp);
1089 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1090 syslog(LOG_NOTICE, "undecodable umount request from %s",
1092 svcerr_decode(transp);
1095 if (realpath(rpcpath, dirpath) == NULL) {
1096 syslog(LOG_NOTICE, "umount request from %s "
1097 "for non existent path %s",
1098 numerichost, dirpath);
1100 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1101 syslog(LOG_ERR, "can't send reply");
1103 del_mlist(host, dirpath);
1104 del_mlist(numerichost, dirpath);
1107 "umount request succeeded from %s for %s",
1108 numerichost, dirpath);
1110 case MOUNTPROC_UMNTALL:
1111 if (sport >= IPPORT_RESERVED && resvport_only) {
1113 "umountall request from %s from unprivileged port",
1115 svcerr_weakauth(transp);
1118 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1119 syslog(LOG_ERR, "can't send reply");
1121 del_mlist(host, NULL);
1122 del_mlist(numerichost, NULL);
1125 "umountall request succeeded from %s",
1128 case MOUNTPROC_EXPORT:
1129 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1130 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1132 syslog(LOG_ERR, "can't send reply");
1135 "export request succeeded from %s",
1139 svcerr_noproc(transp);
1145 * Xdr conversion for a dirpath string
1148 xdr_dir(XDR *xdrsp, char *dirp)
1150 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1154 * Xdr routine to generate file handle reply
1157 xdr_fhs(XDR *xdrsp, caddr_t cp)
1159 struct fhreturn *fhrp = (struct fhreturn *)cp;
1160 u_long ok = 0, len, auth;
1163 if (!xdr_long(xdrsp, &ok))
1165 switch (fhrp->fhr_vers) {
1167 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1170 if (!xdr_long(xdrsp, &len))
1172 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1174 if (fhrp->fhr_numsecflavors) {
1175 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1177 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1178 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1184 if (!xdr_long(xdrsp, &len))
1186 return (xdr_long(xdrsp, &auth));
1193 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1195 struct mountlist *mlp;
1202 if (!xdr_bool(xdrsp, &true))
1204 strp = &mlp->ml_host[0];
1205 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1207 strp = &mlp->ml_dirp[0];
1208 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1212 if (!xdr_bool(xdrsp, &false))
1218 * Xdr conversion for export list
1221 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1223 struct exportlist *ep;
1226 sigset_t sighup_mask;
1228 sigemptyset(&sighup_mask);
1229 sigaddset(&sighup_mask, SIGHUP);
1230 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1234 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1237 if (ep->ex_defdir && putdef == 0 &&
1238 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1243 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1244 if (!xdr_bool(xdrsp, &false))
1248 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1253 * Called from xdr_explist() to traverse the tree and export the
1257 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1260 struct grouplist *grp;
1261 struct hostlist *hp;
1268 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1270 if (!xdr_bool(xdrsp, &true))
1273 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1275 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1280 if (!xdr_bool(xdrsp, &true))
1283 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1285 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1286 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1290 if (grp->gr_type == GT_HOST) {
1291 if (!xdr_bool(xdrsp, &true))
1293 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1294 if (!xdr_string(xdrsp, &strp,
1297 } else if (grp->gr_type == GT_NET) {
1298 if (!xdr_bool(xdrsp, &true))
1300 strp = grp->gr_ptr.gt_net.nt_name;
1301 if (!xdr_string(xdrsp, &strp,
1306 if (gotalldir && hp == (struct hostlist *)NULL) {
1312 if (!xdr_bool(xdrsp, &false))
1314 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1321 xdr_explist(XDR *xdrsp, caddr_t cp)
1324 return xdr_explist_common(xdrsp, cp, 0);
1328 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1331 return xdr_explist_common(xdrsp, cp, 1);
1339 * Get the export list from one, currently open file
1342 get_exportlist_one(void)
1344 struct exportlist *ep, *ep2;
1345 struct grouplist *grp, *tgrp;
1346 struct exportlist **epp;
1347 struct dirlist *dirhead;
1350 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1351 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1354 dirhead = (struct dirlist *)NULL;
1355 while (get_line()) {
1357 warnx("got line %s", line);
1359 nextfield(&cp, &endcp);
1368 exflags = MNT_EXPORTED;
1371 ep = (struct exportlist *)NULL;
1375 * Handle the V4 root dir.
1377 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1379 * V4: just indicates that it is the v4 root point,
1380 * so skip over that and set v4root_phase.
1382 if (v4root_phase > 0) {
1383 syslog(LOG_ERR, "V4:duplicate line, ignored");
1388 nextfield(&cp, &endcp);
1392 * Create new exports list entry
1395 tgrp = grp = get_grp();
1397 if (len > MNTNAMLEN) {
1398 getexp_err(ep, tgrp);
1402 if (ep == (struct exportlist *)NULL) {
1403 getexp_err(ep, tgrp);
1407 warnx("doing opt %s", cp);
1409 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1411 getexp_err(ep, tgrp);
1414 } else if (*cp == '/') {
1417 if (v4root_phase > 1) {
1419 syslog(LOG_ERR, "Multiple V4 dirs");
1420 getexp_err(ep, tgrp);
1424 if (check_dirpath(cp) &&
1425 statfs(cp, &fsb) >= 0) {
1427 syslog(LOG_ERR, "dirs must be first");
1428 getexp_err(ep, tgrp);
1431 if (v4root_phase == 1) {
1433 syslog(LOG_ERR, "Multiple V4 dirs");
1434 getexp_err(ep, tgrp);
1437 if (strlen(v4root_dirpath) == 0) {
1438 strlcpy(v4root_dirpath, cp,
1439 sizeof (v4root_dirpath));
1440 } else if (strcmp(v4root_dirpath, cp)
1443 "different V4 dirpath %s", cp);
1444 getexp_err(ep, tgrp);
1453 if (ep->ex_fs.val[0] !=
1454 fsb.f_fsid.val[0] ||
1456 fsb.f_fsid.val[1]) {
1457 getexp_err(ep, tgrp);
1462 * See if this directory is already
1465 ep = ex_search(&fsb.f_fsid);
1466 if (ep == (struct exportlist *)NULL) {
1468 ep->ex_fs = fsb.f_fsid;
1469 ep->ex_fsdir = (char *)malloc
1470 (strlen(fsb.f_mntonname) + 1);
1472 strcpy(ep->ex_fsdir,
1478 "making new ep fs=0x%x,0x%x",
1482 warnx("found ep fs=0x%x,0x%x",
1488 * Add dirpath to export mount point.
1490 dirp = add_expdir(&dirhead, cp, len);
1494 getexp_err(ep, tgrp);
1502 if (ep == (struct exportlist *)NULL) {
1503 getexp_err(ep, tgrp);
1508 * Get the host or netgroup.
1511 netgrp = getnetgrent(&hst, &usr, &dom);
1514 grp->gr_next = get_grp();
1520 "null hostname in netgroup %s, skipping", cp);
1521 grp->gr_type = GT_IGNORE;
1522 } else if (get_host(hst, grp, tgrp)) {
1524 "bad host %s in netgroup %s, skipping", hst, cp);
1525 grp->gr_type = GT_IGNORE;
1527 } else if (get_host(cp, grp, tgrp)) {
1528 syslog(LOG_ERR, "bad host %s, skipping", cp);
1529 grp->gr_type = GT_IGNORE;
1532 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1537 nextfield(&cp, &endcp);
1540 if (check_options(dirhead)) {
1541 getexp_err(ep, tgrp);
1545 grp->gr_type = GT_DEFAULT;
1547 warnx("adding a default entry");
1550 * Don't allow a network export coincide with a list of
1551 * host(s) on the same line.
1553 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1554 syslog(LOG_ERR, "network/host conflict");
1555 getexp_err(ep, tgrp);
1559 * If an export list was specified on this line, make sure
1560 * that we have at least one valid entry, otherwise skip it.
1564 while (grp && grp->gr_type == GT_IGNORE)
1567 getexp_err(ep, tgrp);
1572 if (v4root_phase == 1) {
1573 syslog(LOG_ERR, "V4:root, no dirp, ignored");
1574 getexp_err(ep, tgrp);
1579 * Loop through hosts, pushing the exports into the kernel.
1580 * After loop, tgrp points to the start of the list and
1581 * grp points to the last entry in the list.
1585 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1587 getexp_err(ep, tgrp);
1590 } while (grp->gr_next && (grp = grp->gr_next));
1593 * For V4: don't enter in mount lists.
1595 if (v4root_phase > 0 && v4root_phase <= 2) {
1597 * Since these structures aren't used by mountd,
1602 while (tgrp != NULL) {
1604 tgrp = tgrp->gr_next;
1611 * Success. Update the data structures.
1614 hang_dirp(dirhead, tgrp, ep, opt_flags);
1615 grp->gr_next = grphead;
1618 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1622 dirhead = (struct dirlist *)NULL;
1623 if ((ep->ex_flag & EX_LINKED) == 0) {
1628 * Insert in the list in alphabetical order.
1630 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1631 epp = &ep2->ex_next;
1637 ep->ex_flag |= EX_LINKED;
1643 dirhead = (struct dirlist *)NULL;
1649 * Get the export list from all specified files
1652 get_exportlist(void)
1654 struct exportlist *ep, *ep2;
1655 struct grouplist *grp, *tgrp;
1656 struct export_args export;
1658 struct statfs *fsp, *mntbufp;
1659 struct xvfsconf vfc;
1664 struct nfsex_args eargs;
1666 if (suspend_nfsd != 0)
1667 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1668 v4root_dirpath[0] = '\0';
1669 bzero(&export, sizeof(export));
1670 export.ex_flags = MNT_DELEXPORT;
1673 bzero(errmsg, sizeof(errmsg));
1676 * First, get rid of the old list
1684 exphead = (struct exportlist *)NULL;
1692 grphead = (struct grouplist *)NULL;
1695 * and the old V4 root dir.
1697 bzero(&eargs, sizeof (eargs));
1698 eargs.export.ex_flags = MNT_DELEXPORT;
1699 if (run_v4server > 0 &&
1700 nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1702 syslog(LOG_ERR, "Can't delete exports for V4:");
1705 * and clear flag that notes if a public fh has been exported.
1710 * And delete exports that are in the kernel for all local
1712 * XXX: Should know how to handle all local exportable filesystems.
1714 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1717 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1718 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1719 build_iovec(&iov, &iovlen, "from", NULL, 0);
1720 build_iovec(&iov, &iovlen, "update", NULL, 0);
1721 build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1722 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1725 for (i = 0; i < num; i++) {
1727 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1728 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1734 * Do not delete export for network filesystem by
1735 * passing "export" arg to nmount().
1736 * It only makes sense to do this for local filesystems.
1738 if (vfc.vfc_flags & VFCF_NETWORK)
1741 iov[1].iov_base = fsp->f_fstypename;
1742 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1743 iov[3].iov_base = fsp->f_mntonname;
1744 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1745 iov[5].iov_base = fsp->f_mntfromname;
1746 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1749 if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1750 errno != ENOENT && errno != ENOTSUP) {
1752 "can't delete exports for %s: %m %s",
1753 fsp->f_mntonname, errmsg);
1758 /* Free strings allocated by strdup() in getmntopts.c */
1759 free(iov[0].iov_base); /* fstype */
1760 free(iov[2].iov_base); /* fspath */
1761 free(iov[4].iov_base); /* from */
1762 free(iov[6].iov_base); /* update */
1763 free(iov[8].iov_base); /* export */
1764 free(iov[10].iov_base); /* errmsg */
1766 /* free iov, allocated by realloc() */
1772 * Read in the exports file and build the list, calling
1773 * nmount() as we go along to push the export rules into the kernel.
1776 for (i = 0; exnames[i] != NULL; i++) {
1778 warnx("reading exports from %s", exnames[i]);
1779 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1780 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1783 get_exportlist_one();
1788 syslog(LOG_ERR, "can't open any exports file");
1793 * If there was no public fh, clear any previous one set.
1795 if (run_v4server > 0 && has_publicfh == 0)
1796 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1798 /* Resume the nfsd. If they weren't suspended, this is harmless. */
1799 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
1803 * Allocate an export list element
1808 struct exportlist *ep;
1810 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
1811 if (ep == (struct exportlist *)NULL)
1817 * Allocate a group list element
1822 struct grouplist *gp;
1824 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
1825 if (gp == (struct grouplist *)NULL)
1831 * Clean up upon an error in get_exportlist().
1834 getexp_err(struct exportlist *ep, struct grouplist *grp)
1836 struct grouplist *tgrp;
1838 if (!(opt_flags & OP_QUIET))
1839 syslog(LOG_ERR, "bad exports list line %s", line);
1840 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1850 * Search the export list for a matching fs.
1853 ex_search(fsid_t *fsid)
1855 struct exportlist *ep;
1859 if (ep->ex_fs.val[0] == fsid->val[0] &&
1860 ep->ex_fs.val[1] == fsid->val[1])
1868 * Add a directory path to the list.
1871 add_expdir(struct dirlist **dpp, char *cp, int len)
1875 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
1876 if (dp == (struct dirlist *)NULL)
1879 dp->dp_right = (struct dirlist *)NULL;
1881 dp->dp_hosts = (struct hostlist *)NULL;
1882 strcpy(dp->dp_dirp, cp);
1884 return (dp->dp_dirp);
1888 * Hang the dir list element off the dirpath binary tree as required
1889 * and update the entry for host.
1892 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1895 struct hostlist *hp;
1896 struct dirlist *dp2;
1898 if (flags & OP_ALLDIRS) {
1903 if (grp == (struct grouplist *)NULL) {
1904 ep->ex_defdir->dp_flag |= DP_DEFSET;
1905 /* Save the default security flavors list. */
1906 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1907 if (ep->ex_numsecflavors > 0)
1908 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1909 sizeof(ep->ex_secflavors));
1910 } else while (grp) {
1913 hp->ht_next = ep->ex_defdir->dp_hosts;
1914 ep->ex_defdir->dp_hosts = hp;
1915 /* Save the security flavors list for this host set. */
1916 grp->gr_numsecflavors = ep->ex_numsecflavors;
1917 if (ep->ex_numsecflavors > 0)
1918 memcpy(grp->gr_secflavors, ep->ex_secflavors,
1919 sizeof(ep->ex_secflavors));
1925 * Loop through the directories adding them to the tree.
1929 add_dlist(&ep->ex_dirl, dp, grp, flags, ep);
1936 * Traverse the binary tree either updating a node that is already there
1937 * for the new directory or adding the new node.
1940 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
1941 int flags, struct exportlist *ep)
1944 struct hostlist *hp;
1949 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1951 add_dlist(&dp->dp_left, newdp, grp, flags, ep);
1953 } else if (cmp < 0) {
1954 add_dlist(&dp->dp_right, newdp, grp, flags, ep);
1957 free((caddr_t)newdp);
1960 dp->dp_left = (struct dirlist *)NULL;
1966 * Hang all of the host(s) off of the directory point.
1971 hp->ht_next = dp->dp_hosts;
1973 /* Save the security flavors list for this host set. */
1974 grp->gr_numsecflavors = ep->ex_numsecflavors;
1975 if (ep->ex_numsecflavors > 0)
1976 memcpy(grp->gr_secflavors, ep->ex_secflavors,
1977 sizeof(ep->ex_secflavors));
1981 dp->dp_flag |= DP_DEFSET;
1982 /* Save the default security flavors list. */
1983 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
1984 if (ep->ex_numsecflavors > 0)
1985 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
1986 sizeof(ep->ex_secflavors));
1991 * Search for a dirpath on the export point.
1994 dirp_search(struct dirlist *dp, char *dirp)
1999 cmp = strcmp(dp->dp_dirp, dirp);
2001 return (dirp_search(dp->dp_left, dirp));
2003 return (dirp_search(dp->dp_right, dirp));
2011 * Scan for a host match in a directory tree.
2014 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2015 int *hostsetp, int *numsecflavors, int **secflavorsp)
2017 struct hostlist *hp;
2018 struct grouplist *grp;
2019 struct addrinfo *ai;
2022 if (dp->dp_flag & DP_DEFSET)
2023 *defsetp = dp->dp_flag;
2027 switch (grp->gr_type) {
2029 ai = grp->gr_ptr.gt_addrinfo;
2030 for (; ai; ai = ai->ai_next) {
2031 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2033 (hp->ht_flag | DP_HOSTSET);
2034 if (numsecflavors != NULL) {
2036 grp->gr_numsecflavors;
2045 if (!sacmp(saddr, (struct sockaddr *)
2046 &grp->gr_ptr.gt_net.nt_net,
2048 &grp->gr_ptr.gt_net.nt_mask)) {
2049 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2050 if (numsecflavors != NULL) {
2052 grp->gr_numsecflavors;
2067 * Scan tree for a host that matches the address.
2070 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2072 int defset, hostset;
2075 if (scan_tree(dp->dp_left, saddr))
2077 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2079 if (scan_tree(dp->dp_right, saddr))
2086 * Traverse the dirlist tree and free it up.
2089 free_dir(struct dirlist *dp)
2093 free_dir(dp->dp_left);
2094 free_dir(dp->dp_right);
2095 free_host(dp->dp_hosts);
2101 * Parse a colon separated list of security flavors
2104 parsesec(char *seclist, struct exportlist *ep)
2109 ep->ex_numsecflavors = 0;
2111 cp = strchr(seclist, ':');
2117 if (!strcmp(seclist, "sys"))
2119 else if (!strcmp(seclist, "krb5"))
2120 flavor = RPCSEC_GSS_KRB5;
2121 else if (!strcmp(seclist, "krb5i"))
2122 flavor = RPCSEC_GSS_KRB5I;
2123 else if (!strcmp(seclist, "krb5p"))
2124 flavor = RPCSEC_GSS_KRB5P;
2128 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2131 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2134 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2137 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2138 ep->ex_numsecflavors++;
2150 * Parse the option string and update fields.
2151 * Option arguments may either be -<option>=<value> or
2155 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2156 int *has_hostp, int *exflagsp, struct xucred *cr)
2158 char *cpoptarg, *cpoptend;
2159 char *cp, *endcp, *cpopt, savedc, savedc2;
2160 int allflag, usedarg;
2168 while (cpopt && *cpopt) {
2171 if ((cpoptend = strchr(cpopt, ','))) {
2173 if ((cpoptarg = strchr(cpopt, '=')))
2176 if ((cpoptarg = strchr(cpopt, '=')))
2180 nextfield(&cp, &endcp);
2182 if (endcp > cp && *cp != '-') {
2190 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2191 *exflagsp |= MNT_EXRDONLY;
2192 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2193 !(allflag = strcmp(cpopt, "mapall")) ||
2194 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2196 parsecred(cpoptarg, cr);
2198 *exflagsp |= MNT_EXPORTANON;
2199 opt_flags |= OP_MAPALL;
2201 opt_flags |= OP_MAPROOT;
2202 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2203 !strcmp(cpopt, "m"))) {
2204 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2205 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2209 opt_flags |= OP_MASK;
2210 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2211 !strcmp(cpopt, "n"))) {
2212 if (strchr(cpoptarg, '/') != NULL) {
2214 fprintf(stderr, "setting OP_MASKLEN\n");
2215 opt_flags |= OP_MASKLEN;
2217 if (grp->gr_type != GT_NULL) {
2218 syslog(LOG_ERR, "network/host conflict");
2220 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2221 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2224 grp->gr_type = GT_NET;
2227 opt_flags |= OP_NET;
2228 } else if (!strcmp(cpopt, "alldirs")) {
2229 opt_flags |= OP_ALLDIRS;
2230 } else if (!strcmp(cpopt, "public")) {
2231 *exflagsp |= MNT_EXPUBLIC;
2232 } else if (!strcmp(cpopt, "webnfs")) {
2233 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2234 opt_flags |= OP_MAPALL;
2235 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2236 ep->ex_indexfile = strdup(cpoptarg);
2237 } else if (!strcmp(cpopt, "quiet")) {
2238 opt_flags |= OP_QUIET;
2239 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2240 if (parsesec(cpoptarg, ep))
2242 opt_flags |= OP_SEC;
2245 syslog(LOG_ERR, "bad opt %s", cpopt);
2264 * Translate a character string to the corresponding list of network
2265 * addresses for a hostname.
2268 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2270 struct grouplist *checkgrp;
2271 struct addrinfo *ai, *tai, hints;
2273 char host[NI_MAXHOST];
2275 if (grp->gr_type != GT_NULL) {
2276 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2279 memset(&hints, 0, sizeof hints);
2280 hints.ai_flags = AI_CANONNAME;
2281 hints.ai_protocol = IPPROTO_UDP;
2282 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2284 syslog(LOG_ERR,"can't get address info for host %s", cp);
2287 grp->gr_ptr.gt_addrinfo = ai;
2288 while (ai != NULL) {
2289 if (ai->ai_canonname == NULL) {
2290 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2291 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2292 strlcpy(host, "?", sizeof(host));
2293 ai->ai_canonname = strdup(host);
2294 ai->ai_flags |= AI_CANONNAME;
2297 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2299 * Sanity check: make sure we don't already have an entry
2300 * for this host in the grouplist.
2302 for (checkgrp = tgrp; checkgrp != NULL;
2303 checkgrp = checkgrp->gr_next) {
2304 if (checkgrp->gr_type != GT_HOST)
2306 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2307 tai = tai->ai_next) {
2308 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2312 "ignoring duplicate host %s\n",
2314 grp->gr_type = GT_IGNORE;
2320 grp->gr_type = GT_HOST;
2325 * Free up an exports list component
2328 free_exp(struct exportlist *ep)
2331 if (ep->ex_defdir) {
2332 free_host(ep->ex_defdir->dp_hosts);
2333 free((caddr_t)ep->ex_defdir);
2337 if (ep->ex_indexfile)
2338 free(ep->ex_indexfile);
2339 free_dir(ep->ex_dirl);
2347 free_host(struct hostlist *hp)
2349 struct hostlist *hp2;
2361 struct hostlist *hp;
2363 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2364 if (hp == (struct hostlist *)NULL)
2366 hp->ht_next = (struct hostlist *)NULL;
2372 * Out of memory, fatal
2378 syslog(LOG_ERR, "out of memory");
2383 * Do the nmount() syscall with the update flag to push the export info into
2387 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2388 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
2391 struct addrinfo *ai;
2392 struct export_args ea, *eap;
2400 struct nfsex_args nfsea;
2402 if (run_v4server > 0)
2403 eap = &nfsea.export;
2413 bzero(eap, sizeof (struct export_args));
2414 bzero(errmsg, sizeof(errmsg));
2415 eap->ex_flags = exflags;
2416 eap->ex_anon = *anoncrp;
2417 eap->ex_indexfile = ep->ex_indexfile;
2418 if (grp->gr_type == GT_HOST)
2419 ai = grp->gr_ptr.gt_addrinfo;
2422 eap->ex_numsecflavors = ep->ex_numsecflavors;
2423 for (i = 0; i < eap->ex_numsecflavors; i++)
2424 eap->ex_secflavors[i] = ep->ex_secflavors[i];
2425 if (eap->ex_numsecflavors == 0) {
2426 eap->ex_numsecflavors = 1;
2427 eap->ex_secflavors[0] = AUTH_SYS;
2431 if (v4root_phase == 0) {
2432 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2433 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2434 build_iovec(&iov, &iovlen, "from", NULL, 0);
2435 build_iovec(&iov, &iovlen, "update", NULL, 0);
2436 build_iovec(&iov, &iovlen, "export", eap,
2437 sizeof (struct export_args));
2438 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2442 switch (grp->gr_type) {
2444 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
2446 eap->ex_addr = ai->ai_addr;
2447 eap->ex_addrlen = ai->ai_addrlen;
2448 eap->ex_masklen = 0;
2451 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
2455 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2457 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2459 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2460 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
2463 eap->ex_addr = NULL;
2464 eap->ex_addrlen = 0;
2465 eap->ex_mask = NULL;
2466 eap->ex_masklen = 0;
2473 syslog(LOG_ERR, "bad grouptype");
2481 * For V4:, use the nfssvc() syscall, instead of mount().
2483 if (v4root_phase == 2) {
2484 nfsea.fspec = v4root_dirpath;
2485 if (run_v4server > 0 &&
2486 nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2487 syslog(LOG_ERR, "Exporting V4: failed");
2493 * Maybe I should just use the fsb->f_mntonname path
2494 * instead of looping back up the dirp to the mount
2496 * Also, needs to know how to export all types of local
2497 * exportable filesystems and not just "ufs".
2499 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2500 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2501 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2502 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2503 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2504 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2507 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2511 cp = dirp + dirplen - 1;
2512 if (opt_flags & OP_QUIET) {
2516 if (errno == EPERM) {
2518 warnx("can't change attributes for %s: %s",
2521 "can't change attributes for %s: %s",
2526 if (opt_flags & OP_ALLDIRS) {
2527 if (errno == EINVAL)
2529 "-alldirs requested but %s is not a filesystem mountpoint",
2533 "could not remount %s: %m",
2538 /* back up over the last component */
2539 while (*cp == '/' && cp > dirp)
2541 while (*(cp - 1) != '/' && cp > dirp)
2545 warnx("mnt unsucc");
2546 syslog(LOG_ERR, "can't export %s %s",
2554 * Check that we're still on the same
2557 if (statfs(dirp, &fsb1) != 0 ||
2558 bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2559 sizeof (fsb1.f_fsid)) != 0) {
2562 "can't export %s %s", dirp,
2571 * For the experimental server:
2572 * If this is the public directory, get the file handle
2573 * and load it into the kernel via the nfssvc() syscall.
2575 if (run_v4server > 0 && (exflags & MNT_EXPUBLIC) != 0) {
2579 if (eap->ex_indexfile != NULL)
2580 public_name = eap->ex_indexfile;
2583 if (getfh(public_name, &fh) < 0)
2585 "Can't get public fh for %s", public_name);
2586 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2588 "Can't set public fh for %s", public_name);
2601 /* free strings allocated by strdup() in getmntopts.c */
2603 free(iov[0].iov_base); /* fstype */
2604 free(iov[2].iov_base); /* fspath */
2605 free(iov[4].iov_base); /* from */
2606 free(iov[6].iov_base); /* update */
2607 free(iov[8].iov_base); /* export */
2608 free(iov[10].iov_base); /* errmsg */
2610 /* free iov, allocated by realloc() */
2617 * Translate a net address.
2619 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2622 get_net(char *cp, struct netmsk *net, int maskflg)
2624 struct netent *np = NULL;
2625 char *name, *p, *prefp;
2626 struct sockaddr_in sin;
2627 struct sockaddr *sa = NULL;
2628 struct addrinfo hints, *ai = NULL;
2629 char netname[NI_MAXHOST];
2633 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2634 p = strchr(cp, '/');
2640 * Check for a numeric address first. We wish to avoid
2641 * possible DNS lookups in getnetbyname().
2643 if (isxdigit(*cp) || *cp == ':') {
2644 memset(&hints, 0, sizeof hints);
2645 /* Ensure the mask and the network have the same family. */
2646 if (maskflg && (opt_flags & OP_NET))
2647 hints.ai_family = net->nt_net.ss_family;
2648 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2649 hints.ai_family = net->nt_mask.ss_family;
2651 hints.ai_family = AF_UNSPEC;
2652 hints.ai_flags = AI_NUMERICHOST;
2653 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2655 if (sa != NULL && ai->ai_family == AF_INET) {
2657 * The address in `cp' is really a network address, so
2658 * use inet_network() to re-interpret this correctly.
2659 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2661 bzero(&sin, sizeof sin);
2662 sin.sin_family = AF_INET;
2663 sin.sin_len = sizeof sin;
2664 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2666 fprintf(stderr, "get_net: v4 addr %s\n",
2667 inet_ntoa(sin.sin_addr));
2668 sa = (struct sockaddr *)&sin;
2671 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2672 bzero(&sin, sizeof sin);
2673 sin.sin_family = AF_INET;
2674 sin.sin_len = sizeof sin;
2675 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2676 sa = (struct sockaddr *)&sin;
2682 /* The specified sockaddr is a mask. */
2683 if (checkmask(sa) != 0)
2685 bcopy(sa, &net->nt_mask, sa->sa_len);
2686 opt_flags |= OP_HAVEMASK;
2688 /* The specified sockaddr is a network address. */
2689 bcopy(sa, &net->nt_net, sa->sa_len);
2691 /* Get a network name for the export list. */
2694 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2695 NULL, 0, NI_NUMERICHOST) == 0) {
2700 if ((net->nt_name = strdup(name)) == NULL)
2704 * Extract a mask from either a "/<masklen>" suffix, or
2705 * from the class of an IPv4 address.
2707 if (opt_flags & OP_MASKLEN) {
2708 preflen = strtol(prefp, NULL, 10);
2709 if (preflen < 0L || preflen == LONG_MAX)
2711 bcopy(sa, &net->nt_mask, sa->sa_len);
2712 if (makemask(&net->nt_mask, (int)preflen) != 0)
2714 opt_flags |= OP_HAVEMASK;
2716 } else if (sa->sa_family == AF_INET &&
2717 (opt_flags & OP_MASK) == 0) {
2720 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2721 if (IN_CLASSA(addr))
2723 else if (IN_CLASSB(addr))
2725 else if (IN_CLASSC(addr))
2727 else if (IN_CLASSD(addr))
2730 preflen = 32; /* XXX */
2732 bcopy(sa, &net->nt_mask, sa->sa_len);
2733 makemask(&net->nt_mask, (int)preflen);
2734 opt_flags |= OP_HAVEMASK;
2749 * Parse out the next white space separated field
2752 nextfield(char **cp, char **endcp)
2757 while (*p == ' ' || *p == '\t')
2759 if (*p == '\n' || *p == '\0')
2763 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2770 * Get an exports file line. Skip over blank lines and handle line
2778 int totlen, cont_line;
2781 * Loop around ignoring blank lines and getting all continuation lines.
2786 if ((p = fgetln(exp_file, &len)) == NULL)
2791 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2801 if (linesize < len + totlen + 1) {
2802 linesize = len + totlen + 1;
2803 line = realloc(line, linesize);
2807 memcpy(line + totlen, p, len);
2809 line[totlen] = '\0';
2810 } while (totlen == 0 || cont_line);
2815 * Parse a description of a credential.
2818 parsecred(char *namelist, struct xucred *cr)
2825 gid_t groups[XU_NGROUPS + 1];
2828 cr->cr_version = XUCRED_VERSION;
2830 * Set up the unprivileged user.
2833 cr->cr_groups[0] = -2;
2836 * Get the user's password table entry.
2838 names = strsep(&namelist, " \t\n");
2839 name = strsep(&names, ":");
2840 if (isdigit(*name) || *name == '-')
2841 pw = getpwuid(atoi(name));
2843 pw = getpwnam(name);
2845 * Credentials specified as those of a user.
2847 if (names == NULL) {
2849 syslog(LOG_ERR, "unknown user: %s", name);
2852 cr->cr_uid = pw->pw_uid;
2853 ngroups = XU_NGROUPS + 1;
2854 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2855 syslog(LOG_ERR, "too many groups");
2857 * Compress out duplicate.
2859 cr->cr_ngroups = ngroups - 1;
2860 cr->cr_groups[0] = groups[0];
2861 for (cnt = 2; cnt < ngroups; cnt++)
2862 cr->cr_groups[cnt - 1] = groups[cnt];
2866 * Explicit credential specified as a colon separated list:
2870 cr->cr_uid = pw->pw_uid;
2871 else if (isdigit(*name) || *name == '-')
2872 cr->cr_uid = atoi(name);
2874 syslog(LOG_ERR, "unknown user: %s", name);
2878 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
2879 name = strsep(&names, ":");
2880 if (isdigit(*name) || *name == '-') {
2881 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2883 if ((gr = getgrnam(name)) == NULL) {
2884 syslog(LOG_ERR, "unknown group: %s", name);
2887 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2890 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
2891 syslog(LOG_ERR, "too many groups");
2894 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
2896 * Routines that maintain the remote mounttab
2901 struct mountlist *mlp, **mlpp;
2902 char *host, *dirp, *cp;
2906 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2907 if (errno == ENOENT)
2910 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2915 while (fgets(str, STRSIZ, mlfile) != NULL) {
2917 host = strsep(&cp, " \t\n");
2918 dirp = strsep(&cp, " \t\n");
2919 if (host == NULL || dirp == NULL)
2921 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2922 if (mlp == (struct mountlist *)NULL)
2924 strncpy(mlp->ml_host, host, MNTNAMLEN);
2925 mlp->ml_host[MNTNAMLEN] = '\0';
2926 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2927 mlp->ml_dirp[MNTPATHLEN] = '\0';
2928 mlp->ml_next = (struct mountlist *)NULL;
2930 mlpp = &mlp->ml_next;
2936 del_mlist(char *hostp, char *dirp)
2938 struct mountlist *mlp, **mlpp;
2939 struct mountlist *mlp2;
2946 if (!strcmp(mlp->ml_host, hostp) &&
2947 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2950 *mlpp = mlp = mlp->ml_next;
2951 free((caddr_t)mlp2);
2953 mlpp = &mlp->ml_next;
2958 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2959 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
2964 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2972 add_mlist(char *hostp, char *dirp)
2974 struct mountlist *mlp, **mlpp;
2980 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2982 mlpp = &mlp->ml_next;
2985 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2986 if (mlp == (struct mountlist *)NULL)
2988 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
2989 mlp->ml_host[MNTNAMLEN] = '\0';
2990 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2991 mlp->ml_dirp[MNTPATHLEN] = '\0';
2992 mlp->ml_next = (struct mountlist *)NULL;
2994 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2995 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
2998 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3003 * Free up a group list.
3006 free_grp(struct grouplist *grp)
3008 if (grp->gr_type == GT_HOST) {
3009 if (grp->gr_ptr.gt_addrinfo != NULL)
3010 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3011 } else if (grp->gr_type == GT_NET) {
3012 if (grp->gr_ptr.gt_net.nt_name)
3013 free(grp->gr_ptr.gt_net.nt_name);
3020 SYSLOG(int pri, const char *fmt, ...)
3025 vfprintf(stderr, fmt, ap);
3031 * Check options for consistency.
3034 check_options(struct dirlist *dp)
3037 if (v4root_phase == 0 && dp == NULL)
3039 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3040 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3043 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3044 syslog(LOG_ERR, "-mask requires -network");
3047 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3048 syslog(LOG_ERR, "-network requires mask specification");
3051 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3052 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3055 if (v4root_phase > 0 &&
3057 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3058 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3061 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3062 syslog(LOG_ERR, "-alldirs has multiple directories");
3069 * Check an absolute directory path for any symbolic links. Return true
3072 check_dirpath(char *dirp)
3079 while (*cp && ret) {
3082 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3088 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3094 * Make a netmask according to the specified prefix length. The ss_family
3095 * and other non-address fields must be initialised before calling this.
3098 makemask(struct sockaddr_storage *ssp, int bitlen)
3103 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3105 if (bitlen > len * CHAR_BIT)
3108 for (i = 0; i < len; i++) {
3109 bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
3110 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3117 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3118 * is acceptable (i.e. of the form 1...10....0).
3121 checkmask(struct sockaddr *sa)
3126 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3129 for (i = 0; i < len; i++)
3130 if (mask[i] != 0xff)
3133 if (~mask[i] & (u_char)(~mask[i] + 1))
3137 for (; i < len; i++)
3144 * Compare two sockaddrs according to a specified mask. Return zero if
3145 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3146 * If samask is NULL, perform a full comparison.
3149 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3151 unsigned char *p1, *p2, *mask;
3154 if (sa1->sa_family != sa2->sa_family ||
3155 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3156 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3159 switch (sa1->sa_family) {
3161 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3162 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3167 /* Simple binary comparison if no mask specified. */
3169 return (memcmp(p1, p2, len));
3171 /* Set up the mask, and do a mask-based comparison. */
3172 if (sa1->sa_family != samask->sa_family ||
3173 (mask = sa_rawaddr(samask, NULL)) == NULL)
3176 for (i = 0; i < len; i++)
3177 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3183 * Return a pointer to the part of the sockaddr that contains the
3184 * raw address, and set *nbytes to its length in bytes. Returns
3185 * NULL if the address family is unknown.
3188 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3192 switch (sa->sa_family) {
3194 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3195 p = &((struct sockaddr_in *)sa)->sin_addr;
3198 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3199 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3212 huphandler(int sig __unused)
3217 void terminate(int sig __unused)
3219 pidfile_remove(pfh);
3220 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3221 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);