2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Herb Hasler and Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)mountd.c 8.15 (Berkeley) 5/1/95
34 * $FreeBSD: head/usr.sbin/mountd/mountd.c 173056 2007-10-27 12:24:47Z simon $
37 #include <sys/param.h>
38 #include <sys/module.h>
39 #include <sys/mount.h>
40 #include <sys/mountctl.h>
41 #include <sys/fcntl.h>
42 #include <sys/linker.h>
44 #include <sys/syslog.h>
45 #include <sys/sysctl.h>
48 #include <rpc/rpc_com.h>
49 #include <rpcsvc/mount.h>
50 #include <vfs/nfs/rpcv2.h>
51 #include <vfs/nfs/nfsproto.h>
52 #include <vfs/nfs/nfs.h>
53 #include <vfs/ufs/ufsmount.h>
54 #include <vfs/msdosfs/msdosfsmount.h>
55 #include <vfs/ntfs/ntfsmount.h>
56 #include <vfs/isofs/cd9660/cd9660_mount.h> /* XXX need isofs in include */
58 #include <arpa/inet.h>
73 #include "pathnames.h"
80 * Structures for keeping the mount list and export list
83 struct mountlist *ml_next;
84 char ml_host[RPCMNT_NAMELEN+1];
85 char ml_dirp[RPCMNT_PATHLEN+1];
89 struct dirlist *dp_left;
90 struct dirlist *dp_right;
92 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
93 char dp_dirp[1]; /* Actually malloc'd to size of dir */
97 #define DP_HOSTSET 0x2
100 struct exportlist *ex_next;
101 struct dirlist *ex_dirl;
102 struct dirlist *ex_defdir;
109 #define EX_LINKED 0x1
112 struct sockaddr_storage nt_net;
113 struct sockaddr_storage nt_mask;
118 struct addrinfo *gt_addrinfo;
119 struct netmsk gt_net;
124 union grouptypes gr_ptr;
125 struct grouplist *gr_next;
131 #define GT_DEFAULT 0x3
132 #define GT_IGNORE 0x5
135 int ht_flag; /* Uses DP_xx bits */
136 struct grouplist *ht_grp;
137 struct hostlist *ht_next;
147 char *add_expdir(struct dirlist **, char *, int);
148 void add_dlist(struct dirlist **, struct dirlist *,
149 struct grouplist *, int);
150 void add_mlist(char *, char *);
151 int check_dirpath(char *);
152 int check_options(struct dirlist *);
153 int checkmask(struct sockaddr *sa);
154 int chk_host(struct dirlist *, struct sockaddr *, int *, int *);
155 void create_service(struct netconfig *nconf);
156 void del_mlist(char *, char *);
157 struct dirlist *dirp_search(struct dirlist *, char *);
158 int do_mount(struct exportlist *, struct grouplist *, int,
159 struct ucred *, char *, int, struct statfs *);
160 int do_opt(char **, char **, struct exportlist *, struct grouplist *,
161 int *, int *, struct ucred *);
162 struct exportlist *ex_search(fsid_t *);
163 struct exportlist *get_exp(void);
164 void free_dir(struct dirlist *);
165 void free_exp(struct exportlist *);
166 void free_grp(struct grouplist *);
167 void free_host(struct hostlist *);
168 void get_exportlist(int incremental);
169 int get_host(char *, struct grouplist *, struct grouplist *);
170 struct hostlist *get_ht(void);
172 void get_mountlist(void);
173 static void delete_export(struct statfs *fsp);
174 int get_net(char *, struct netmsk *, int);
175 void getexp_err(struct exportlist *, struct grouplist *);
176 struct grouplist *get_grp(void);
177 void hang_dirp(struct dirlist *, struct grouplist *,
178 struct exportlist *, int);
179 void huphandler1(int sig);
180 void huphandler2(int sig);
181 int makemask(struct sockaddr_storage *ssp, int bitlen);
182 void mntsrv(struct svc_req *, SVCXPRT *);
183 void nextfield(char **, char **);
184 void out_of_mem(void);
185 void parsecred(char *, struct ucred *);
186 int put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
187 void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
188 int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
189 struct sockaddr *samask);
190 int scan_tree(struct dirlist *, struct sockaddr *);
191 static void usage(void);
192 int xdr_dir(XDR *, char *);
193 int xdr_explist(XDR *, caddr_t);
194 int xdr_explist_brief(XDR *, caddr_t);
195 int xdr_fhs(XDR *, caddr_t);
196 int xdr_mlist(XDR *, caddr_t);
199 struct exportlist *exphead;
200 struct mountlist *mlhead;
201 struct grouplist *grphead;
202 char *exnames_default[2] = { _PATH_EXPORTS, NULL };
205 struct ucred def_anon = {
212 int resvport_only = 1;
220 char *svcport_str = NULL;
222 static int have_v6 = 1;
224 struct pidfh *pfh = NULL;
225 /* Bits for the opt_flags above */
226 #define OP_MAPROOT 0x01
227 #define OP_MAPALL 0x02
231 #define OP_ALLDIRS 0x40
232 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
233 #define OP_QUIET 0x100
234 #define OP_MASKLEN 0x200
238 void SYSLOG(int, const char *, ...);
239 #define syslog SYSLOG
245 * Mountd server for NFS mount protocol as described in:
246 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
247 * The optional arguments are the exports file name
248 * default: _PATH_EXPORTS
249 * and "-n" to allow nonroot mount.
252 main(int argc, char **argv)
255 struct netconfig *nconf;
256 char *endptr, **hosts_bak;
261 int maxrec = RPC_MAXDATASIZE;
263 /* Check that another mountd isn't already running. */
264 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
267 errx(1, "mountd already running, pid: %d.", otherpid);
268 warn("cannot open or create pidfile");
271 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
276 if (modfind("nfs") < 0) {
277 /* Not present in kernel, try loading it */
278 if (kldload("nfs") < 0 || modfind("nfs") < 0)
279 errx(1, "NFS server is not available or loadable");
282 while ((c = getopt(argc, argv, "2dh:lnp:r")) != -1) {
294 debug = debug ? 0 : 1;
301 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
302 if (endptr == NULL || *endptr != '\0' ||
303 svcport == 0 || svcport >= IPPORT_MAX)
305 svcport_str = strdup(optarg);
310 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
311 if (hosts_bak == NULL) {
313 for (k = 0; k < nhosts; k++)
320 hosts[nhosts - 1] = strdup(optarg);
321 if (hosts[nhosts - 1] == NULL) {
322 for (k = 0; k < (nhosts - 1); k++)
334 signal(SIGINT, SIG_IGN);
335 signal(SIGQUIT, SIG_IGN);
345 exnames = exnames_default;
346 openlog("mountd", LOG_PID, LOG_DAEMON);
348 warnx("getting export list");
351 warnx("getting mount list");
355 signal(SIGHUP, huphandler1);
356 signal(SIGUSR1, huphandler2);
357 signal(SIGTERM, terminate);
358 signal(SIGPIPE, SIG_IGN);
362 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
363 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
365 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
367 if (!resvport_only) {
368 if (sysctlbyname("vfs.nfs.nfs_privport", NULL, NULL,
369 &resvport_only, sizeof(resvport_only)) != 0 &&
371 syslog(LOG_ERR, "sysctl: %m");
377 * If no hosts were specified, add a wildcard entry to bind to
378 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
382 hosts = malloc(sizeof(char**));
390 hosts_bak = realloc(hosts, (nhosts + 2) *
392 if (hosts_bak == NULL) {
393 for (k = 0; k < nhosts; k++)
400 hosts[nhosts - 2] = "::1";
402 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
403 if (hosts_bak == NULL) {
404 for (k = 0; k < nhosts; k++)
413 hosts[nhosts - 1] = "127.0.0.1";
416 nc_handle = setnetconfig();
417 while ((nconf = getnetconfig(nc_handle))) {
418 if (nconf->nc_flag & NC_VISIBLE) {
419 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
423 create_service(nconf);
426 endnetconfig(nc_handle);
429 syslog(LOG_ERR, "could not create any services");
433 /* Expand svc_run() here so that we can call get_exportlist(). */
444 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
448 syslog(LOG_ERR, "mountd died: select: %m");
453 svc_getreqset(&readfds);
459 * This routine creates and binds sockets on the appropriate
460 * addresses. It gets called one time for each transport and
461 * registrates the service with rpcbind on that trasport.
464 create_service(struct netconfig *nconf)
466 struct addrinfo hints, *res = NULL;
467 struct sockaddr_in *sin;
468 struct sockaddr_in6 *sin6;
469 struct __rpc_sockinfo si;
470 struct netbuf servaddr;
471 SVCXPRT *transp = NULL;
478 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
480 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
481 (nconf->nc_semantics != NC_TPI_COTS) &&
482 (nconf->nc_semantics != NC_TPI_COTS_ORD))
483 return; /* not my type */
486 * XXX - using RPC library internal functions.
488 if (!__rpc_nconf2sockinfo(nconf, &si)) {
489 syslog(LOG_ERR, "cannot get information for %s",
494 /* Get mountd's address on this transport */
495 memset(&hints, 0, sizeof hints);
496 hints.ai_flags = AI_PASSIVE;
497 hints.ai_family = si.si_af;
498 hints.ai_socktype = si.si_socktype;
499 hints.ai_protocol = si.si_proto;
502 * Bind to specific IPs if asked to
505 while (nhostsbak > 0) {
508 * XXX - using RPC library internal functions.
510 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
512 if (errno == EPROTONOSUPPORT &&
513 nconf->nc_semantics != NC_TPI_CLTS)
516 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
517 "cannot create socket for %s", nconf->nc_netid);
521 switch (hints.ai_family) {
523 if (inet_pton(AF_INET, hosts[nhostsbak],
525 hints.ai_flags &= AI_NUMERICHOST;
528 * Skip if we have an AF_INET6 address.
530 if (inet_pton(AF_INET6, hosts[nhostsbak],
538 if (inet_pton(AF_INET6, hosts[nhostsbak],
540 hints.ai_flags &= AI_NUMERICHOST;
543 * Skip if we have an AF_INET address.
545 if (inet_pton(AF_INET, hosts[nhostsbak],
553 * We're doing host-based access checks here, so don't
554 * allow v4-in-v6 to confuse things. The kernel will
555 * disable it by default on NFS sockets too.
557 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
560 "can't disable v4-in-v6 on IPv6 socket");
569 * If no hosts were specified, just bind to INADDR_ANY
571 if (strcmp("*", hosts[nhostsbak]) == 0) {
572 if (svcport_str == NULL) {
573 res = malloc(sizeof(struct addrinfo));
576 res->ai_flags = hints.ai_flags;
577 res->ai_family = hints.ai_family;
578 res->ai_protocol = hints.ai_protocol;
579 switch (res->ai_family) {
581 sin = malloc(sizeof(struct sockaddr_in));
584 sin->sin_family = AF_INET;
585 sin->sin_port = htons(0);
586 sin->sin_addr.s_addr = htonl(INADDR_ANY);
587 res->ai_addr = (struct sockaddr*) sin;
588 res->ai_addrlen = (socklen_t)
589 sizeof(res->ai_addr);
592 sin6 = malloc(sizeof(struct sockaddr_in6));
595 sin6->sin6_family = AF_INET6;
596 sin6->sin6_port = htons(0);
597 sin6->sin6_addr = in6addr_any;
598 res->ai_addr = (struct sockaddr*) sin6;
599 res->ai_addrlen = (socklen_t)
600 sizeof(res->ai_addr);
606 if ((aicode = getaddrinfo(NULL, svcport_str,
607 &hints, &res)) != 0) {
609 "cannot get local address for %s: %s",
611 gai_strerror(aicode));
616 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
617 &hints, &res)) != 0) {
619 "cannot get local address for %s: %s",
620 nconf->nc_netid, gai_strerror(aicode));
628 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
629 (char *)&on, sizeof(on));
632 r = bindresvport_sa(fd, res->ai_addr);
634 syslog(LOG_ERR, "bindresvport_sa: %m");
638 if (nconf->nc_semantics != NC_TPI_CLTS)
639 listen(fd, SOMAXCONN);
641 if (nconf->nc_semantics == NC_TPI_CLTS )
642 transp = svc_dg_create(fd, 0, 0);
644 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
647 if (transp != NULL) {
648 if (!svc_reg(transp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
651 "can't register %s RPCMNT_VER1 service",
654 if (!svc_reg(transp, RPCPROG_MNT, RPCMNT_VER3,
657 "can't register %s RPCMNT_VER3 service",
661 syslog(LOG_WARNING, "can't create %s services",
664 if (registered == 0) {
666 memset(&hints, 0, sizeof hints);
667 hints.ai_flags = AI_PASSIVE;
668 hints.ai_family = si.si_af;
669 hints.ai_socktype = si.si_socktype;
670 hints.ai_protocol = si.si_proto;
672 if (svcport_str == NULL) {
673 svcport_str = malloc(NI_MAXSERV * sizeof(char));
674 if (svcport_str == NULL)
677 if (getnameinfo(res->ai_addr,
678 res->ai_addr->sa_len, NULL, NI_MAXHOST,
679 svcport_str, NI_MAXSERV * sizeof(char),
680 NI_NUMERICHOST | NI_NUMERICSERV))
681 errx(1, "Cannot get port number");
684 if((aicode = getaddrinfo(NULL, svcport_str, &hints,
686 syslog(LOG_ERR, "cannot get local address: %s",
687 gai_strerror(aicode));
691 servaddr.buf = malloc(res->ai_addrlen);
692 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
693 servaddr.len = res->ai_addrlen;
695 rpcb_set(RPCPROG_MNT, RPCMNT_VER1, nconf, &servaddr);
696 rpcb_set(RPCPROG_MNT, RPCMNT_VER3, nconf, &servaddr);
708 "usage: mountd [-2] [-d] [-l] [-n] [-p <port>] [-r] "
709 "[-h <bindip>] [export_file ...]\n");
714 * The mount rpc service
717 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
719 struct exportlist *ep;
724 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
725 int lookup_failed = 1;
726 struct sockaddr *saddr;
728 char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
729 int bad = 0, defset, hostset;
730 sigset_t sighup_mask;
732 sigemptyset(&sighup_mask);
733 sigaddset(&sighup_mask, SIGHUP);
735 saddr = svc_getrpccaller(transp)->buf;
736 switch (saddr->sa_family) {
738 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
741 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
744 syslog(LOG_ERR, "request from unknown address family");
747 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
749 getnameinfo(saddr, saddr->sa_len, numerichost,
750 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
751 switch (rqstp->rq_proc) {
753 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
754 syslog(LOG_ERR, "can't send reply");
757 if (sport >= IPPORT_RESERVED && resvport_only) {
759 "mount request from %s from unprivileged port",
761 svcerr_weakauth(transp);
764 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
765 syslog(LOG_NOTICE, "undecodable mount request from %s",
767 svcerr_decode(transp);
772 * Get the real pathname and make sure it is a directory
773 * or a regular file if the -r option was specified
776 if (realpath(rpcpath, dirpath) == NULL ||
777 stat(dirpath, &stb) < 0 ||
778 (!S_ISDIR(stb.st_mode) &&
779 (dir_only || !S_ISREG(stb.st_mode))) ||
780 statfs(dirpath, &fsb) < 0) {
781 chdir("/"); /* Just in case realpath doesn't */
783 "mount request from %s for non existent path %s",
784 numerichost, dirpath);
786 warnx("stat failed on %s", dirpath);
787 bad = ENOENT; /* We will send error reply later */
790 /* Check in the exports list */
791 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
792 ep = ex_search(&fsb.f_fsid);
793 hostset = defset = 0;
794 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
795 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
796 chk_host(dp, saddr, &defset, &hostset)) ||
797 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
798 scan_tree(ep->ex_dirl, saddr) == 0))) {
800 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
802 syslog(LOG_ERR, "can't send reply");
803 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
806 if (hostset & DP_HOSTSET)
807 fhr.fhr_flag = hostset;
809 fhr.fhr_flag = defset;
810 fhr.fhr_vers = rqstp->rq_vers;
811 /* Get the file handle */
812 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
813 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
815 syslog(LOG_ERR, "can't get fh for %s", dirpath);
816 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
818 syslog(LOG_ERR, "can't send reply");
819 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
822 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, &fhr))
823 syslog(LOG_ERR, "can't send reply");
825 add_mlist(host, dirpath);
827 add_mlist(numerichost, dirpath);
829 warnx("mount successful");
832 "mount request succeeded from %s for %s",
833 numerichost, dirpath);
837 "mount request denied from %s for %s",
838 numerichost, dirpath);
841 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long, &bad))
842 syslog(LOG_ERR, "can't send reply");
843 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
846 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, NULL))
847 syslog(LOG_ERR, "can't send reply");
850 "dump request succeeded from %s",
854 if (sport >= IPPORT_RESERVED && resvport_only) {
856 "umount request from %s from unprivileged port",
858 svcerr_weakauth(transp);
861 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
862 syslog(LOG_NOTICE, "undecodable umount request from %s",
864 svcerr_decode(transp);
867 if (realpath(rpcpath, dirpath) == NULL) {
868 syslog(LOG_NOTICE, "umount request from %s "
869 "for non existent path %s",
870 numerichost, dirpath);
872 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
873 syslog(LOG_ERR, "can't send reply");
875 del_mlist(host, dirpath);
876 del_mlist(numerichost, dirpath);
879 "umount request succeeded from %s for %s",
880 numerichost, dirpath);
883 if (sport >= IPPORT_RESERVED && resvport_only) {
885 "umountall request from %s from unprivileged port",
887 svcerr_weakauth(transp);
890 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
891 syslog(LOG_ERR, "can't send reply");
893 del_mlist(host, NULL);
894 del_mlist(numerichost, NULL);
897 "umountall request succeeded from %s",
901 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, NULL))
902 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief, NULL))
903 syslog(LOG_ERR, "can't send reply");
906 "export request succeeded from %s",
910 svcerr_noproc(transp);
916 * Xdr conversion for a dirpath string
919 xdr_dir(XDR *xdrsp, char *dirp)
921 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
925 * Xdr routine to generate file handle reply
928 xdr_fhs(XDR *xdrsp, caddr_t cp)
930 struct fhreturn *fhrp = (struct fhreturn *)cp;
931 u_long ok = 0, len, auth;
933 if (!xdr_long(xdrsp, &ok))
935 switch (fhrp->fhr_vers) {
937 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
940 if (!xdr_long(xdrsp, &len))
942 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
946 if (!xdr_long(xdrsp, &len))
948 return (xdr_long(xdrsp, &auth));
954 xdr_mlist(XDR *xdrsp, caddr_t cp)
956 struct mountlist *mlp;
963 if (!xdr_bool(xdrsp, &true))
965 strp = &mlp->ml_host[0];
966 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
968 strp = &mlp->ml_dirp[0];
969 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
973 if (!xdr_bool(xdrsp, &false))
979 * Xdr conversion for export list
982 xdr_explist_common(XDR *xdrsp, caddr_t cp, int brief)
984 struct exportlist *ep;
987 sigset_t sighup_mask;
989 sigemptyset(&sighup_mask);
990 sigaddset(&sighup_mask, SIGHUP);
991 sigaddset(&sighup_mask, SIGUSR1);
992 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
996 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
999 if (ep->ex_defdir && putdef == 0 &&
1000 put_exlist(ep->ex_defdir, xdrsp, NULL,
1005 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1006 if (!xdr_bool(xdrsp, &false))
1010 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1015 * Called from xdr_explist() to traverse the tree and export the
1019 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp, int brief)
1021 struct grouplist *grp;
1022 struct hostlist *hp;
1029 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1031 if (!xdr_bool(xdrsp, &true))
1034 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
1036 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1041 if (!xdr_bool(xdrsp, &true))
1044 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
1046 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1047 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1051 if (grp->gr_type == GT_HOST) {
1052 if (!xdr_bool(xdrsp, &true))
1054 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1055 if (!xdr_string(xdrsp, &strp,
1058 } else if (grp->gr_type == GT_NET) {
1059 if (!xdr_bool(xdrsp, &true))
1061 strp = grp->gr_ptr.gt_net.nt_name;
1062 if (!xdr_string(xdrsp, &strp,
1067 if (gotalldir && hp == NULL) {
1073 if (!xdr_bool(xdrsp, &false))
1075 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1082 xdr_explist(XDR *xdrsp, caddr_t cp)
1085 return xdr_explist_common(xdrsp, cp, 0);
1089 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1092 return xdr_explist_common(xdrsp, cp, 1);
1100 * Get the export list from one, currently open file
1103 get_exportlist_one(void)
1105 struct exportlist *ep, *ep2;
1106 struct grouplist *grp, *tgrp;
1107 struct exportlist **epp;
1108 struct dirlist *dirhead;
1111 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1112 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1115 while (get_line()) {
1117 warnx("got line %s", line);
1119 nextfield(&cp, &endcp);
1128 exflags = MNT_EXPORTED;
1134 * Create new exports list entry
1137 tgrp = grp = get_grp();
1139 if (len > RPCMNT_NAMELEN) {
1140 getexp_err(ep, tgrp);
1145 getexp_err(ep, tgrp);
1149 warnx("doing opt %s", cp);
1151 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1153 getexp_err(ep, tgrp);
1156 } else if (*cp == '/') {
1159 if (check_dirpath(cp) &&
1160 statfs(cp, &fsb) >= 0) {
1161 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1162 syslog(LOG_ERR, "Warning: exporting of "
1163 "automounted fs %s not supported", cp);
1165 syslog(LOG_ERR, "dirs must be first");
1166 getexp_err(ep, tgrp);
1170 if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
1171 ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
1172 getexp_err(ep, tgrp);
1177 * See if this directory is already
1180 ep = ex_search(&fsb.f_fsid);
1183 ep->ex_fs = fsb.f_fsid;
1184 ep->ex_fsdir = (char *)
1185 malloc(strlen(fsb.f_mntonname) + 1);
1187 strcpy(ep->ex_fsdir,
1192 delete_export(&fsb);
1195 warnx("making new ep fs=0x%x,0x%x",
1199 warnx("found ep fs=0x%x,0x%x",
1205 * Add dirpath to export mount point.
1207 dirp = add_expdir(&dirhead, cp, len);
1210 getexp_err(ep, tgrp);
1219 getexp_err(ep, tgrp);
1224 * Get the host or netgroup.
1227 netgrp = getnetgrent(&hst, &usr, &dom);
1230 grp->gr_next = get_grp();
1236 "null hostname in netgroup %s, skipping", cp);
1237 grp->gr_type = GT_IGNORE;
1238 } else if (get_host(hst, grp, tgrp)) {
1240 "bad host %s in netgroup %s, skipping", hst, cp);
1241 grp->gr_type = GT_IGNORE;
1243 } else if (get_host(cp, grp, tgrp)) {
1244 syslog(LOG_ERR, "bad host %s, skipping", cp);
1245 grp->gr_type = GT_IGNORE;
1248 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1253 nextfield(&cp, &endcp);
1256 if (check_options(dirhead)) {
1257 getexp_err(ep, tgrp);
1261 grp->gr_type = GT_DEFAULT;
1263 warnx("adding a default entry");
1266 * Don't allow a network export coincide with a list of
1267 * host(s) on the same line.
1269 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1270 syslog(LOG_ERR, "network/host conflict");
1271 getexp_err(ep, tgrp);
1275 * If an export list was specified on this line, make sure
1276 * that we have at least one valid entry, otherwise skip it.
1280 while (grp && grp->gr_type == GT_IGNORE)
1283 getexp_err(ep, tgrp);
1289 * Loop through hosts, pushing the exports into the kernel.
1290 * After loop, tgrp points to the start of the list and
1291 * grp points to the last entry in the list.
1295 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1297 getexp_err(ep, tgrp);
1300 } while (grp->gr_next && (grp = grp->gr_next));
1303 * Success. Update the data structures.
1306 hang_dirp(dirhead, tgrp, ep, opt_flags);
1307 grp->gr_next = grphead;
1310 hang_dirp(dirhead, NULL, ep,
1315 if ((ep->ex_flag & EX_LINKED) == 0) {
1320 * Insert in the list in alphabetical order.
1322 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1323 epp = &ep2->ex_next;
1329 ep->ex_flag |= EX_LINKED;
1341 * Get the export list from all specified files.
1343 * If incremental is non-zero we were signalled by mount and we do not
1344 * call mountctl() to blow away existing exports.
1347 get_exportlist(int incremental)
1349 struct exportlist *ep, *ep2;
1350 struct grouplist *grp, *tgrp;
1351 struct statfs *fsp, *mntbufp;
1360 * First, get rid of the old list
1379 * And delete exports that are in the kernel for all local
1381 * XXX: Should know how to handle all local exportable filesystems.
1383 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1384 for (i = 0; i < num; i++) {
1386 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1387 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1393 * Do not delete export for network filesystem by
1394 * passing "export" arg to mount().
1395 * It only makes sense to do this for local filesystems.
1397 if (vfc.vfc_flags & VFCF_NETWORK)
1400 if (incremental == 0)
1405 * Read in the exports file and build the list, calling
1406 * nmount() as we go along to push the export rules into the kernel.
1409 for (i = 0; exnames[i] != NULL; i++) {
1411 warnx("reading exports from %s", exnames[i]);
1412 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1413 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1416 get_exportlist_one();
1421 syslog(LOG_ERR, "can't open any exports file");
1427 * Allocate an export list element
1432 struct exportlist *ep;
1434 ep = (struct exportlist *)malloc(sizeof (struct exportlist));
1437 memset(ep, 0, sizeof(struct exportlist));
1442 * Allocate a group list element
1447 struct grouplist *gp;
1449 gp = (struct grouplist *)malloc(sizeof (struct grouplist));
1452 memset(gp, 0, sizeof(struct grouplist));
1457 * Clean up upon an error in get_exportlist().
1460 getexp_err(struct exportlist *ep, struct grouplist *grp)
1462 struct grouplist *tgrp;
1464 if (!(opt_flags & OP_QUIET))
1465 syslog(LOG_ERR, "bad exports list line %s", line);
1466 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1476 * Search the export list for a matching fs.
1479 ex_search(fsid_t *fsid)
1481 struct exportlist *ep;
1485 if (ep->ex_fs.val[0] == fsid->val[0] &&
1486 ep->ex_fs.val[1] == fsid->val[1])
1494 * Add a directory path to the list.
1497 add_expdir(struct dirlist **dpp, char *cp, int len)
1501 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
1505 dp->dp_right = NULL;
1507 dp->dp_hosts = NULL;
1508 strcpy(dp->dp_dirp, cp);
1510 return (dp->dp_dirp);
1514 * Hang the dir list element off the dirpath binary tree as required
1515 * and update the entry for host.
1518 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1521 struct hostlist *hp;
1522 struct dirlist *dp2;
1524 if (flags & OP_ALLDIRS) {
1530 ep->ex_defdir->dp_flag |= DP_DEFSET;
1531 } else while (grp) {
1534 hp->ht_next = ep->ex_defdir->dp_hosts;
1535 ep->ex_defdir->dp_hosts = hp;
1541 * Loop through the directories adding them to the tree.
1545 add_dlist(&ep->ex_dirl, dp, grp, flags);
1552 * Traverse the binary tree either updating a node that is already there
1553 * for the new directory or adding the new node.
1556 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
1560 struct hostlist *hp;
1565 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1567 add_dlist(&dp->dp_left, newdp, grp, flags);
1569 } else if (cmp < 0) {
1570 add_dlist(&dp->dp_right, newdp, grp, flags);
1573 free((caddr_t)newdp);
1582 * Hang all of the host(s) off of the directory point.
1587 hp->ht_next = dp->dp_hosts;
1592 dp->dp_flag |= DP_DEFSET;
1597 * Search for a dirpath on the export point.
1600 dirp_search(struct dirlist *dp, char *dirp)
1605 cmp = strcmp(dp->dp_dirp, dirp);
1607 return (dirp_search(dp->dp_left, dirp));
1609 return (dirp_search(dp->dp_right, dirp));
1617 * Scan for a host match in a directory tree.
1620 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
1623 struct hostlist *hp;
1624 struct grouplist *grp;
1625 struct addrinfo *ai;
1628 if (dp->dp_flag & DP_DEFSET)
1629 *defsetp = dp->dp_flag;
1633 switch (grp->gr_type) {
1635 ai = grp->gr_ptr.gt_addrinfo;
1636 for (; ai; ai = ai->ai_next) {
1637 if (!sacmp(ai->ai_addr, saddr, NULL)) {
1639 (hp->ht_flag | DP_HOSTSET);
1645 if (!sacmp(saddr, (struct sockaddr *)
1646 &grp->gr_ptr.gt_net.nt_net,
1648 &grp->gr_ptr.gt_net.nt_mask)) {
1649 *hostsetp = (hp->ht_flag | DP_HOSTSET);
1661 * Scan tree for a host that matches the address.
1664 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
1666 int defset, hostset;
1669 if (scan_tree(dp->dp_left, saddr))
1671 if (chk_host(dp, saddr, &defset, &hostset))
1673 if (scan_tree(dp->dp_right, saddr))
1680 * Traverse the dirlist tree and free it up.
1683 free_dir(struct dirlist *dp)
1687 free_dir(dp->dp_left);
1688 free_dir(dp->dp_right);
1689 free_host(dp->dp_hosts);
1695 * Parse the option string and update fields.
1696 * Option arguments may either be -<option>=<value> or
1700 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
1701 int *has_hostp, int *exflagsp, struct ucred *cr)
1703 char *cpoptarg, *cpoptend;
1704 char *cp, *endcp, *cpopt, savedc, savedc2;
1705 int allflag, usedarg;
1713 while (cpopt && *cpopt) {
1716 if ((cpoptend = strchr(cpopt, ','))) {
1718 if ((cpoptarg = strchr(cpopt, '=')))
1721 if ((cpoptarg = strchr(cpopt, '=')))
1725 nextfield(&cp, &endcp);
1727 if (endcp > cp && *cp != '-') {
1735 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1736 *exflagsp |= MNT_EXRDONLY;
1737 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1738 !(allflag = strcmp(cpopt, "mapall")) ||
1739 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1741 parsecred(cpoptarg, cr);
1743 *exflagsp |= MNT_EXPORTANON;
1744 opt_flags |= OP_MAPALL;
1746 opt_flags |= OP_MAPROOT;
1747 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1748 !strcmp(cpopt, "m"))) {
1749 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1750 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
1754 opt_flags |= OP_MASK;
1755 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1756 !strcmp(cpopt, "n"))) {
1757 if (strchr(cpoptarg, '/') != NULL) {
1759 fprintf(stderr, "setting OP_MASKLEN\n");
1760 opt_flags |= OP_MASKLEN;
1762 if (grp->gr_type != GT_NULL) {
1763 syslog(LOG_ERR, "network/host conflict");
1765 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1766 syslog(LOG_ERR, "bad net: %s", cpoptarg);
1769 grp->gr_type = GT_NET;
1772 opt_flags |= OP_NET;
1773 } else if (!strcmp(cpopt, "alldirs")) {
1774 opt_flags |= OP_ALLDIRS;
1775 } else if (!strcmp(cpopt, "public")) {
1776 *exflagsp |= MNT_EXPUBLIC;
1777 } else if (!strcmp(cpopt, "webnfs")) {
1778 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
1779 opt_flags |= OP_MAPALL;
1780 } else if (cpoptarg && !strcmp(cpopt, "index")) {
1781 ep->ex_indexfile = strdup(cpoptarg);
1782 } else if (!strcmp(cpopt, "quiet")) {
1783 opt_flags |= OP_QUIET;
1785 syslog(LOG_ERR, "bad opt %s", cpopt);
1804 * Translate a character string to the corresponding list of network
1805 * addresses for a hostname.
1808 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
1810 struct grouplist *checkgrp;
1811 struct addrinfo *ai, *tai, hints;
1813 char host[NI_MAXHOST];
1815 if (grp->gr_type != GT_NULL) {
1816 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
1819 memset(&hints, 0, sizeof hints);
1820 hints.ai_flags = AI_CANONNAME;
1821 hints.ai_protocol = IPPROTO_UDP;
1822 ecode = getaddrinfo(cp, NULL, &hints, &ai);
1824 syslog(LOG_ERR,"can't get address info for host %s", cp);
1827 grp->gr_ptr.gt_addrinfo = ai;
1828 while (ai != NULL) {
1829 if (ai->ai_canonname == NULL) {
1830 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
1831 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
1832 strlcpy(host, "?", sizeof(host));
1833 ai->ai_canonname = strdup(host);
1834 ai->ai_flags |= AI_CANONNAME;
1837 fprintf(stderr, "got host %s\n", ai->ai_canonname);
1839 * Sanity check: make sure we don't already have an entry
1840 * for this host in the grouplist.
1842 for (checkgrp = tgrp; checkgrp != NULL;
1843 checkgrp = checkgrp->gr_next) {
1844 if (checkgrp->gr_type != GT_HOST)
1846 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
1847 tai = tai->ai_next) {
1848 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
1852 "ignoring duplicate host %s\n",
1854 grp->gr_type = GT_IGNORE;
1860 grp->gr_type = GT_HOST;
1865 * Free up an exports list component
1868 free_exp(struct exportlist *ep)
1871 if (ep->ex_defdir) {
1872 free_host(ep->ex_defdir->dp_hosts);
1873 free((caddr_t)ep->ex_defdir);
1877 if (ep->ex_indexfile)
1878 free(ep->ex_indexfile);
1879 free_dir(ep->ex_dirl);
1887 free_host(struct hostlist *hp)
1889 struct hostlist *hp2;
1901 struct hostlist *hp;
1903 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
1912 * Out of memory, fatal
1918 syslog(LOG_ERR, "out of memory");
1923 * Do the mount syscall with the update flag to push the export info into
1927 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
1928 struct ucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
1931 struct addrinfo *ai;
1932 struct export_args *eap;
1940 struct msdosfs_args da;
1941 struct ntfs_args na;
1944 bzero(&args, sizeof args);
1945 /* XXX, we assume that all xx_args look like ufs_args. */
1947 eap = &args.ua.export;
1949 eap->ex_flags = exflags;
1950 eap->ex_anon = *anoncrp;
1951 eap->ex_indexfile = ep->ex_indexfile;
1952 if (grp->gr_type == GT_HOST)
1953 ai = grp->gr_ptr.gt_addrinfo;
1958 switch (grp->gr_type) {
1960 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
1962 eap->ex_addr = ai->ai_addr;
1963 eap->ex_addrlen = ai->ai_addrlen;
1964 eap->ex_masklen = 0;
1967 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
1971 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
1972 eap->ex_addrlen = args.ua.export.ex_addr->sa_len;
1974 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
1975 eap->ex_masklen = args.ua.export.ex_mask->sa_len;
1978 eap->ex_addr = NULL;
1979 eap->ex_addrlen = 0;
1980 eap->ex_mask = NULL;
1981 eap->ex_masklen = 0;
1987 syslog(LOG_ERR, "bad grouptype");
1995 * Maybe I should just use the fsb->f_mntonname path instead
1996 * of looping back up the dirp to the mount point??
1997 * Also, needs to know how to export all types of local
1998 * exportable filesystems and not just "ufs".
2003 r = mountctl(fsb->f_mntonname, MOUNTCTL_SET_EXPORT,
2005 &args.ua.export, sizeof(args.ua.export),
2007 if (r < 0 && errno == EOPNOTSUPP) {
2008 r = mount(fsb->f_fstypename, dirp,
2009 fsb->f_flags | MNT_UPDATE,
2017 cp = dirp + dirplen - 1;
2018 if (opt_flags & OP_QUIET)
2020 if (errno == EPERM) {
2022 warnx("can't change attributes for %s",
2025 "can't change attributes for %s", dirp);
2028 if (opt_flags & OP_ALLDIRS) {
2029 if (errno == EINVAL)
2031 "-alldirs requested but %s is not a filesystem mountpoint",
2035 "could not remount %s: %m",
2039 /* back up over the last component */
2040 while (*cp == '/' && cp > dirp)
2042 while (*(cp - 1) != '/' && cp > dirp)
2046 warnx("mnt unsucc");
2047 syslog(LOG_ERR, "can't export %s", dirp);
2052 /* Check that we're still on the same filesystem. */
2053 if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
2054 &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
2056 syslog(LOG_ERR, "can't export %s", dirp);
2072 * Translate a net address.
2074 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2077 get_net(char *cp, struct netmsk *net, int maskflg)
2079 struct netent *np = NULL;
2080 char *name, *p, *prefp;
2081 struct sockaddr_in sin;
2082 struct sockaddr *sa = NULL;
2083 struct addrinfo hints, *ai = NULL;
2084 char netname[NI_MAXHOST];
2088 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2089 p = strchr(cp, '/');
2095 * Check for a numeric address first. We wish to avoid
2096 * possible DNS lookups in getnetbyname().
2098 if (isxdigit(*cp) || *cp == ':') {
2099 memset(&hints, 0, sizeof hints);
2100 /* Ensure the mask and the network have the same family. */
2101 if (maskflg && (opt_flags & OP_NET))
2102 hints.ai_family = net->nt_net.ss_family;
2103 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2104 hints.ai_family = net->nt_mask.ss_family;
2106 hints.ai_family = AF_UNSPEC;
2107 hints.ai_flags = AI_NUMERICHOST;
2108 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2110 if (sa != NULL && ai->ai_family == AF_INET) {
2112 * The address in `cp' is really a network address, so
2113 * use inet_network() to re-interpret this correctly.
2114 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2116 bzero(&sin, sizeof sin);
2117 sin.sin_family = AF_INET;
2118 sin.sin_len = sizeof sin;
2119 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2121 fprintf(stderr, "get_net: v4 addr %s\n",
2122 inet_ntoa(sin.sin_addr));
2123 sa = (struct sockaddr *)&sin;
2126 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2127 bzero(&sin, sizeof sin);
2128 sin.sin_family = AF_INET;
2129 sin.sin_len = sizeof sin;
2130 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2131 sa = (struct sockaddr *)&sin;
2137 /* The specified sockaddr is a mask. */
2138 if (checkmask(sa) != 0)
2140 bcopy(sa, &net->nt_mask, sa->sa_len);
2141 opt_flags |= OP_HAVEMASK;
2143 /* The specified sockaddr is a network address. */
2144 bcopy(sa, &net->nt_net, sa->sa_len);
2146 /* Get a network name for the export list. */
2149 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2150 NULL, 0, NI_NUMERICHOST) == 0) {
2155 if ((net->nt_name = strdup(name)) == NULL)
2159 * Extract a mask from either a "/<masklen>" suffix, or
2160 * from the class of an IPv4 address.
2162 if (opt_flags & OP_MASKLEN) {
2163 preflen = strtol(prefp, NULL, 10);
2164 if (preflen < 0L || preflen == LONG_MAX)
2166 bcopy(sa, &net->nt_mask, sa->sa_len);
2167 if (makemask(&net->nt_mask, (int)preflen) != 0)
2169 opt_flags |= OP_HAVEMASK;
2171 } else if (sa->sa_family == AF_INET &&
2172 (opt_flags & OP_MASK) == 0) {
2175 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2176 if (IN_CLASSA(addr))
2178 else if (IN_CLASSB(addr))
2180 else if (IN_CLASSC(addr))
2182 else if (IN_CLASSD(addr))
2185 preflen = 32; /* XXX */
2187 bcopy(sa, &net->nt_mask, sa->sa_len);
2188 makemask(&net->nt_mask, (int)preflen);
2189 opt_flags |= OP_HAVEMASK;
2204 * Parse out the next white space separated field
2207 nextfield(char **cp, char **endcp)
2212 while (*p == ' ' || *p == '\t')
2214 if (*p == '\n' || *p == '\0')
2218 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2225 * Get an exports file line. Skip over blank lines and handle line
2233 int totlen, cont_line;
2236 * Loop around ignoring blank lines and getting all continuation lines.
2241 if ((p = fgetln(exp_file, &len)) == NULL)
2246 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2256 if (linesize < len + totlen + 1) {
2257 linesize = len + totlen + 1;
2258 line = realloc(line, linesize);
2262 memcpy(line + totlen, p, len);
2264 line[totlen] = '\0';
2265 } while (totlen == 0 || cont_line);
2270 * Parse a description of a credential.
2273 parsecred(char *namelist, struct ucred *cr)
2280 gid_t ngroups, groups[NGROUPS + 1];
2283 * Set up the unprivileged user.
2287 cr->cr_groups[0] = -2;
2290 * Get the user's password table entry.
2292 names = strsep(&namelist, " \t\n");
2293 name = strsep(&names, ":");
2294 if (isdigit(*name) || *name == '-')
2295 pw = getpwuid(atoi(name));
2297 pw = getpwnam(name);
2299 * Credentials specified as those of a user.
2301 if (names == NULL) {
2303 syslog(LOG_ERR, "unknown user: %s", name);
2306 cr->cr_uid = pw->pw_uid;
2307 ngroups = NGROUPS + 1;
2308 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2309 syslog(LOG_ERR, "too many groups");
2311 * Compress out duplicate
2313 cr->cr_ngroups = ngroups - 1;
2314 cr->cr_groups[0] = groups[0];
2315 for (cnt = 2; cnt < ngroups; cnt++)
2316 cr->cr_groups[cnt - 1] = groups[cnt];
2320 * Explicit credential specified as a colon separated list:
2324 cr->cr_uid = pw->pw_uid;
2325 else if (isdigit(*name) || *name == '-')
2326 cr->cr_uid = atoi(name);
2328 syslog(LOG_ERR, "unknown user: %s", name);
2332 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
2333 name = strsep(&names, ":");
2334 if (isdigit(*name) || *name == '-') {
2335 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2337 if ((gr = getgrnam(name)) == NULL) {
2338 syslog(LOG_ERR, "unknown group: %s", name);
2341 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2344 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
2345 syslog(LOG_ERR, "too many groups");
2348 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
2350 * Routines that maintain the remote mounttab
2355 struct mountlist *mlp, **mlpp;
2356 char *host, *dirp, *cp;
2360 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2361 if (errno == ENOENT)
2364 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2369 while (fgets(str, STRSIZ, mlfile) != NULL) {
2371 host = strsep(&cp, " \t\n");
2372 dirp = strsep(&cp, " \t\n");
2373 if (host == NULL || dirp == NULL)
2375 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2378 strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
2379 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2380 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2381 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2382 mlp->ml_next = NULL;
2384 mlpp = &mlp->ml_next;
2391 delete_export(struct statfs *fsp)
2397 struct msdosfs_args da;
2398 struct ntfs_args na;
2400 struct export_args export;
2402 export.ex_flags = MNT_DELEXPORT;
2403 if (mountctl(fsp->f_mntonname, MOUNTCTL_SET_EXPORT, -1,
2404 &export, sizeof(export), NULL, 0) == 0) {
2406 } else if (!strcmp(fsp->f_fstypename, "mfs") ||
2407 !strcmp(fsp->f_fstypename, "ufs") ||
2408 !strcmp(fsp->f_fstypename, "msdos") ||
2409 !strcmp(fsp->f_fstypename, "ntfs") ||
2410 !strcmp(fsp->f_fstypename, "cd9660")) {
2411 bzero(&targs, sizeof targs);
2412 targs.ua.fspec = NULL;
2413 targs.ua.export.ex_flags = MNT_DELEXPORT;
2414 if (mount(fsp->f_fstypename, fsp->f_mntonname,
2415 fsp->f_flags | MNT_UPDATE,
2416 (caddr_t)&targs) < 0)
2417 syslog(LOG_ERR, "can't delete exports for %s",
2424 del_mlist(char *hostp, char *dirp)
2426 struct mountlist *mlp, **mlpp;
2427 struct mountlist *mlp2;
2434 if (!strcmp(mlp->ml_host, hostp) &&
2435 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2438 *mlpp = mlp = mlp->ml_next;
2439 free((caddr_t)mlp2);
2441 mlpp = &mlp->ml_next;
2446 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2447 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
2452 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2460 add_mlist(char *hostp, char *dirp)
2462 struct mountlist *mlp, **mlpp;
2468 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2470 mlpp = &mlp->ml_next;
2473 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2476 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2477 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2478 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2479 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2480 mlp->ml_next = NULL;
2482 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2483 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
2486 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2491 * Free up a group list.
2494 free_grp(struct grouplist *grp)
2496 if (grp->gr_type == GT_HOST) {
2497 if (grp->gr_ptr.gt_addrinfo != NULL)
2498 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
2499 } else if (grp->gr_type == GT_NET) {
2500 if (grp->gr_ptr.gt_net.nt_name)
2501 free(grp->gr_ptr.gt_net.nt_name);
2508 SYSLOG(int pri, const char *fmt, ...)
2513 vfprintf(stderr, fmt, ap);
2519 * Check options for consistency.
2522 check_options(struct dirlist *dp)
2527 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
2528 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
2531 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2532 syslog(LOG_ERR, "-mask requires -network");
2535 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
2536 syslog(LOG_ERR, "-network requires mask specification");
2539 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
2540 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
2543 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2544 syslog(LOG_ERR, "-alldirs has multiple directories");
2551 * Check an absolute directory path for any symbolic links. Return true
2554 check_dirpath(char *dirp)
2561 while (*cp && ret) {
2564 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2570 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2576 * Make a netmask according to the specified prefix length. The ss_family
2577 * and other non-address fields must be initialised before calling this.
2580 makemask(struct sockaddr_storage *ssp, int bitlen)
2585 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
2587 if (bitlen > len * CHAR_BIT)
2589 for (i = 0; i < len; i++) {
2590 bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
2591 *p++ = (1 << bits) - 1;
2598 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
2599 * is acceptable (i.e. of the form 1...10....0).
2602 checkmask(struct sockaddr *sa)
2607 if ((mask = sa_rawaddr(sa, &len)) == NULL)
2610 for (i = 0; i < len; i++)
2611 if (mask[i] != 0xff)
2614 if (~mask[i] & (u_char)(~mask[i] + 1))
2618 for (; i < len; i++)
2625 * Compare two sockaddrs according to a specified mask. Return zero if
2626 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
2627 * If samask is NULL, perform a full comparision.
2630 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
2632 unsigned char *p1, *p2, *mask;
2635 if (sa1->sa_family != sa2->sa_family ||
2636 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
2637 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
2640 switch (sa1->sa_family) {
2642 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
2643 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
2648 /* Simple binary comparison if no mask specified. */
2650 return (memcmp(p1, p2, len));
2652 /* Set up the mask, and do a mask-based comparison. */
2653 if (sa1->sa_family != samask->sa_family ||
2654 (mask = sa_rawaddr(samask, NULL)) == NULL)
2657 for (i = 0; i < len; i++)
2658 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
2664 * Return a pointer to the part of the sockaddr that contains the
2665 * raw address, and set *nbytes to its length in bytes. Returns
2666 * NULL if the address family is unknown.
2669 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
2673 switch (sa->sa_family) {
2675 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2676 p = &((struct sockaddr_in *)sa)->sin_addr;
2679 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2680 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
2693 huphandler1(int sig)
2699 huphandler2(int sig)
2704 void terminate(int sig)
2706 pidfile_remove(pfh);
2707 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
2708 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);