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(void);
169 int get_host(char *, struct grouplist *, struct grouplist *);
170 struct hostlist *get_ht(void);
172 void get_mountlist(void);
173 int get_net(char *, struct netmsk *, int);
174 void getexp_err(struct exportlist *, struct grouplist *);
175 struct grouplist *get_grp(void);
176 void hang_dirp(struct dirlist *, struct grouplist *,
177 struct exportlist *, int);
178 void huphandler(int sig);
179 int makemask(struct sockaddr_storage *ssp, int bitlen);
180 void mntsrv(struct svc_req *, SVCXPRT *);
181 void nextfield(char **, char **);
182 void out_of_mem(void);
183 void parsecred(char *, struct ucred *);
184 int put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
185 void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
186 int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
187 struct sockaddr *samask);
188 int scan_tree(struct dirlist *, struct sockaddr *);
189 static void usage(void);
190 int xdr_dir(XDR *, char *);
191 int xdr_explist(XDR *, caddr_t);
192 int xdr_explist_brief(XDR *, caddr_t);
193 int xdr_fhs(XDR *, caddr_t);
194 int xdr_mlist(XDR *, caddr_t);
197 struct exportlist *exphead;
198 struct mountlist *mlhead;
199 struct grouplist *grphead;
200 char *exnames_default[2] = { _PATH_EXPORTS, NULL };
203 struct ucred def_anon = {
210 int resvport_only = 1;
217 char *svcport_str = NULL;
219 static int have_v6 = 1;
221 struct pidfh *pfh = NULL;
222 /* Bits for the opt_flags above */
223 #define OP_MAPROOT 0x01
224 #define OP_MAPALL 0x02
228 #define OP_ALLDIRS 0x40
229 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
230 #define OP_QUIET 0x100
231 #define OP_MASKLEN 0x200
235 void SYSLOG(int, const char *, ...);
236 #define syslog SYSLOG
242 * Mountd server for NFS mount protocol as described in:
243 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
244 * The optional arguments are the exports file name
245 * default: _PATH_EXPORTS
246 * and "-n" to allow nonroot mount.
249 main(int argc, char **argv)
252 struct netconfig *nconf;
253 char *endptr, **hosts_bak;
258 int maxrec = RPC_MAXDATASIZE;
260 /* Check that another mountd isn't already running. */
261 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
264 errx(1, "mountd already running, pid: %d.", otherpid);
265 warn("cannot open or create pidfile");
268 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
273 if (modfind("nfs") < 0) {
274 /* Not present in kernel, try loading it */
275 if (kldload("nfs") < 0 || modfind("nfs") < 0)
276 errx(1, "NFS server is not available or loadable");
279 while ((c = getopt(argc, argv, "2dh:lnp:r")) != -1) {
291 debug = debug ? 0 : 1;
298 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
299 if (endptr == NULL || *endptr != '\0' ||
300 svcport == 0 || svcport >= IPPORT_MAX)
302 svcport_str = strdup(optarg);
307 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
308 if (hosts_bak == NULL) {
310 for (k = 0; k < nhosts; k++)
317 hosts[nhosts - 1] = strdup(optarg);
318 if (hosts[nhosts - 1] == NULL) {
319 for (k = 0; k < (nhosts - 1); k++)
331 signal(SIGINT, SIG_IGN);
332 signal(SIGQUIT, SIG_IGN);
342 exnames = exnames_default;
343 openlog("mountd", LOG_PID, LOG_DAEMON);
345 warnx("getting export list");
348 warnx("getting mount list");
352 signal(SIGHUP, huphandler);
353 signal(SIGTERM, terminate);
354 signal(SIGPIPE, SIG_IGN);
358 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
359 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
361 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
363 if (!resvport_only) {
364 if (sysctlbyname("vfs.nfs.nfs_privport", NULL, NULL,
365 &resvport_only, sizeof(resvport_only)) != 0 &&
367 syslog(LOG_ERR, "sysctl: %m");
373 * If no hosts were specified, add a wildcard entry to bind to
374 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
378 hosts = malloc(sizeof(char**));
386 hosts_bak = realloc(hosts, (nhosts + 2) *
388 if (hosts_bak == NULL) {
389 for (k = 0; k < nhosts; k++)
396 hosts[nhosts - 2] = "::1";
398 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
399 if (hosts_bak == NULL) {
400 for (k = 0; k < nhosts; k++)
409 hosts[nhosts - 1] = "127.0.0.1";
412 nc_handle = setnetconfig();
413 while ((nconf = getnetconfig(nc_handle))) {
414 if (nconf->nc_flag & NC_VISIBLE) {
415 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
419 create_service(nconf);
422 endnetconfig(nc_handle);
425 syslog(LOG_ERR, "could not create any services");
429 /* Expand svc_run() here so that we can call get_exportlist(). */
436 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
440 syslog(LOG_ERR, "mountd died: select: %m");
445 svc_getreqset(&readfds);
451 * This routine creates and binds sockets on the appropriate
452 * addresses. It gets called one time for each transport and
453 * registrates the service with rpcbind on that trasport.
456 create_service(struct netconfig *nconf)
458 struct addrinfo hints, *res = NULL;
459 struct sockaddr_in *sin;
460 struct sockaddr_in6 *sin6;
461 struct __rpc_sockinfo si;
462 struct netbuf servaddr;
463 SVCXPRT *transp = NULL;
470 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
472 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
473 (nconf->nc_semantics != NC_TPI_COTS) &&
474 (nconf->nc_semantics != NC_TPI_COTS_ORD))
475 return; /* not my type */
478 * XXX - using RPC library internal functions.
480 if (!__rpc_nconf2sockinfo(nconf, &si)) {
481 syslog(LOG_ERR, "cannot get information for %s",
486 /* Get mountd's address on this transport */
487 memset(&hints, 0, sizeof hints);
488 hints.ai_flags = AI_PASSIVE;
489 hints.ai_family = si.si_af;
490 hints.ai_socktype = si.si_socktype;
491 hints.ai_protocol = si.si_proto;
494 * Bind to specific IPs if asked to
497 while (nhostsbak > 0) {
500 * XXX - using RPC library internal functions.
502 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
504 if (errno == EPROTONOSUPPORT &&
505 nconf->nc_semantics != NC_TPI_CLTS)
508 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
509 "cannot create socket for %s", nconf->nc_netid);
513 switch (hints.ai_family) {
515 if (inet_pton(AF_INET, hosts[nhostsbak],
517 hints.ai_flags &= AI_NUMERICHOST;
520 * Skip if we have an AF_INET6 address.
522 if (inet_pton(AF_INET6, hosts[nhostsbak],
530 if (inet_pton(AF_INET6, hosts[nhostsbak],
532 hints.ai_flags &= AI_NUMERICHOST;
535 * Skip if we have an AF_INET address.
537 if (inet_pton(AF_INET, hosts[nhostsbak],
545 * We're doing host-based access checks here, so don't
546 * allow v4-in-v6 to confuse things. The kernel will
547 * disable it by default on NFS sockets too.
549 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
552 "can't disable v4-in-v6 on IPv6 socket");
561 * If no hosts were specified, just bind to INADDR_ANY
563 if (strcmp("*", hosts[nhostsbak]) == 0) {
564 if (svcport_str == NULL) {
565 res = malloc(sizeof(struct addrinfo));
568 res->ai_flags = hints.ai_flags;
569 res->ai_family = hints.ai_family;
570 res->ai_protocol = hints.ai_protocol;
571 switch (res->ai_family) {
573 sin = malloc(sizeof(struct sockaddr_in));
576 sin->sin_family = AF_INET;
577 sin->sin_port = htons(0);
578 sin->sin_addr.s_addr = htonl(INADDR_ANY);
579 res->ai_addr = (struct sockaddr*) sin;
580 res->ai_addrlen = (socklen_t)
581 sizeof(res->ai_addr);
584 sin6 = malloc(sizeof(struct sockaddr_in6));
587 sin6->sin6_family = AF_INET6;
588 sin6->sin6_port = htons(0);
589 sin6->sin6_addr = in6addr_any;
590 res->ai_addr = (struct sockaddr*) sin6;
591 res->ai_addrlen = (socklen_t)
592 sizeof(res->ai_addr);
598 if ((aicode = getaddrinfo(NULL, svcport_str,
599 &hints, &res)) != 0) {
601 "cannot get local address for %s: %s",
603 gai_strerror(aicode));
608 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
609 &hints, &res)) != 0) {
611 "cannot get local address for %s: %s",
612 nconf->nc_netid, gai_strerror(aicode));
617 r = bindresvport_sa(fd, res->ai_addr);
619 syslog(LOG_ERR, "bindresvport_sa: %m");
623 if (nconf->nc_semantics != NC_TPI_CLTS)
624 listen(fd, SOMAXCONN);
626 if (nconf->nc_semantics == NC_TPI_CLTS )
627 transp = svc_dg_create(fd, 0, 0);
629 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
632 if (transp != NULL) {
633 if (!svc_reg(transp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
636 "can't register %s RPCMNT_VER1 service",
639 if (!svc_reg(transp, RPCPROG_MNT, RPCMNT_VER3,
642 "can't register %s RPCMNT_VER3 service",
646 syslog(LOG_WARNING, "can't create %s services",
649 if (registered == 0) {
651 memset(&hints, 0, sizeof hints);
652 hints.ai_flags = AI_PASSIVE;
653 hints.ai_family = si.si_af;
654 hints.ai_socktype = si.si_socktype;
655 hints.ai_protocol = si.si_proto;
657 if (svcport_str == NULL) {
658 svcport_str = malloc(NI_MAXSERV * sizeof(char));
659 if (svcport_str == NULL)
662 if (getnameinfo(res->ai_addr,
663 res->ai_addr->sa_len, NULL, NI_MAXHOST,
664 svcport_str, NI_MAXSERV * sizeof(char),
665 NI_NUMERICHOST | NI_NUMERICSERV))
666 errx(1, "Cannot get port number");
669 if((aicode = getaddrinfo(NULL, svcport_str, &hints,
671 syslog(LOG_ERR, "cannot get local address: %s",
672 gai_strerror(aicode));
676 servaddr.buf = malloc(res->ai_addrlen);
677 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
678 servaddr.len = res->ai_addrlen;
680 rpcb_set(RPCPROG_MNT, RPCMNT_VER1, nconf, &servaddr);
681 rpcb_set(RPCPROG_MNT, RPCMNT_VER3, nconf, &servaddr);
693 "usage: mountd [-2] [-d] [-l] [-n] [-p <port>] [-r] "
694 "[-h <bindip>] [export_file ...]\n");
699 * The mount rpc service
702 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
704 struct exportlist *ep;
709 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
710 int lookup_failed = 1;
711 struct sockaddr *saddr;
713 char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
714 int bad = 0, defset, hostset;
715 sigset_t sighup_mask;
717 sigemptyset(&sighup_mask);
718 sigaddset(&sighup_mask, SIGHUP);
719 saddr = svc_getrpccaller(transp)->buf;
720 switch (saddr->sa_family) {
722 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
725 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
728 syslog(LOG_ERR, "request from unknown address family");
731 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
733 getnameinfo(saddr, saddr->sa_len, numerichost,
734 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
735 switch (rqstp->rq_proc) {
737 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
738 syslog(LOG_ERR, "can't send reply");
741 if (sport >= IPPORT_RESERVED && resvport_only) {
743 "mount request from %s from unprivileged port",
745 svcerr_weakauth(transp);
748 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
749 syslog(LOG_NOTICE, "undecodable mount request from %s",
751 svcerr_decode(transp);
756 * Get the real pathname and make sure it is a directory
757 * or a regular file if the -r option was specified
760 if (realpath(rpcpath, dirpath) == NULL ||
761 stat(dirpath, &stb) < 0 ||
762 (!S_ISDIR(stb.st_mode) &&
763 (dir_only || !S_ISREG(stb.st_mode))) ||
764 statfs(dirpath, &fsb) < 0) {
765 chdir("/"); /* Just in case realpath doesn't */
767 "mount request from %s for non existent path %s",
768 numerichost, dirpath);
770 warnx("stat failed on %s", dirpath);
771 bad = ENOENT; /* We will send error reply later */
774 /* Check in the exports list */
775 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
776 ep = ex_search(&fsb.f_fsid);
777 hostset = defset = 0;
778 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
779 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
780 chk_host(dp, saddr, &defset, &hostset)) ||
781 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
782 scan_tree(ep->ex_dirl, saddr) == 0))) {
784 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
786 syslog(LOG_ERR, "can't send reply");
787 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
790 if (hostset & DP_HOSTSET)
791 fhr.fhr_flag = hostset;
793 fhr.fhr_flag = defset;
794 fhr.fhr_vers = rqstp->rq_vers;
795 /* Get the file handle */
796 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
797 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
799 syslog(LOG_ERR, "can't get fh for %s", dirpath);
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 (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, &fhr))
807 syslog(LOG_ERR, "can't send reply");
809 add_mlist(host, dirpath);
811 add_mlist(numerichost, dirpath);
813 warnx("mount successful");
816 "mount request succeeded from %s for %s",
817 numerichost, dirpath);
821 "mount request denied from %s for %s",
822 numerichost, dirpath);
825 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long, &bad))
826 syslog(LOG_ERR, "can't send reply");
827 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
830 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, NULL))
831 syslog(LOG_ERR, "can't send reply");
834 "dump request succeeded from %s",
838 if (sport >= IPPORT_RESERVED && resvport_only) {
840 "umount request from %s from unprivileged port",
842 svcerr_weakauth(transp);
845 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
846 syslog(LOG_NOTICE, "undecodable umount request from %s",
848 svcerr_decode(transp);
851 if (realpath(rpcpath, dirpath) == NULL) {
852 syslog(LOG_NOTICE, "umount request from %s "
853 "for non existent path %s",
854 numerichost, dirpath);
856 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
857 syslog(LOG_ERR, "can't send reply");
859 del_mlist(host, dirpath);
860 del_mlist(numerichost, dirpath);
863 "umount request succeeded from %s for %s",
864 numerichost, dirpath);
867 if (sport >= IPPORT_RESERVED && resvport_only) {
869 "umountall request from %s from unprivileged port",
871 svcerr_weakauth(transp);
874 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
875 syslog(LOG_ERR, "can't send reply");
877 del_mlist(host, NULL);
878 del_mlist(numerichost, NULL);
881 "umountall request succeeded from %s",
885 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, NULL))
886 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief, NULL))
887 syslog(LOG_ERR, "can't send reply");
890 "export request succeeded from %s",
894 svcerr_noproc(transp);
900 * Xdr conversion for a dirpath string
903 xdr_dir(XDR *xdrsp, char *dirp)
905 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
909 * Xdr routine to generate file handle reply
912 xdr_fhs(XDR *xdrsp, caddr_t cp)
914 struct fhreturn *fhrp = (struct fhreturn *)cp;
915 u_long ok = 0, len, auth;
917 if (!xdr_long(xdrsp, &ok))
919 switch (fhrp->fhr_vers) {
921 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
924 if (!xdr_long(xdrsp, &len))
926 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
930 if (!xdr_long(xdrsp, &len))
932 return (xdr_long(xdrsp, &auth));
938 xdr_mlist(XDR *xdrsp, caddr_t cp)
940 struct mountlist *mlp;
947 if (!xdr_bool(xdrsp, &true))
949 strp = &mlp->ml_host[0];
950 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
952 strp = &mlp->ml_dirp[0];
953 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
957 if (!xdr_bool(xdrsp, &false))
963 * Xdr conversion for export list
966 xdr_explist_common(XDR *xdrsp, caddr_t cp, int brief)
968 struct exportlist *ep;
971 sigset_t sighup_mask;
973 sigemptyset(&sighup_mask);
974 sigaddset(&sighup_mask, SIGHUP);
975 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
979 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
982 if (ep->ex_defdir && putdef == 0 &&
983 put_exlist(ep->ex_defdir, xdrsp, NULL,
988 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
989 if (!xdr_bool(xdrsp, &false))
993 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
998 * Called from xdr_explist() to traverse the tree and export the
1002 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp, int brief)
1004 struct grouplist *grp;
1005 struct hostlist *hp;
1012 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1014 if (!xdr_bool(xdrsp, &true))
1017 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
1019 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1024 if (!xdr_bool(xdrsp, &true))
1027 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
1029 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1030 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1034 if (grp->gr_type == GT_HOST) {
1035 if (!xdr_bool(xdrsp, &true))
1037 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1038 if (!xdr_string(xdrsp, &strp,
1041 } else if (grp->gr_type == GT_NET) {
1042 if (!xdr_bool(xdrsp, &true))
1044 strp = grp->gr_ptr.gt_net.nt_name;
1045 if (!xdr_string(xdrsp, &strp,
1050 if (gotalldir && hp == NULL) {
1056 if (!xdr_bool(xdrsp, &false))
1058 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1065 xdr_explist(XDR *xdrsp, caddr_t cp)
1068 return xdr_explist_common(xdrsp, cp, 0);
1072 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1075 return xdr_explist_common(xdrsp, cp, 1);
1083 * Get the export list from one, currently open file
1086 get_exportlist_one(void)
1088 struct exportlist *ep, *ep2;
1089 struct grouplist *grp, *tgrp;
1090 struct exportlist **epp;
1091 struct dirlist *dirhead;
1094 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1095 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1098 while (get_line()) {
1100 warnx("got line %s", line);
1102 nextfield(&cp, &endcp);
1111 exflags = MNT_EXPORTED;
1117 * Create new exports list entry
1120 tgrp = grp = get_grp();
1122 if (len > RPCMNT_NAMELEN) {
1123 getexp_err(ep, tgrp);
1128 getexp_err(ep, tgrp);
1132 warnx("doing opt %s", cp);
1134 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1136 getexp_err(ep, tgrp);
1139 } else if (*cp == '/') {
1142 if (check_dirpath(cp) &&
1143 statfs(cp, &fsb) >= 0) {
1145 syslog(LOG_ERR, "dirs must be first");
1146 getexp_err(ep, tgrp);
1150 if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
1151 ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
1152 getexp_err(ep, tgrp);
1157 * See if this directory is already
1160 ep = ex_search(&fsb.f_fsid);
1163 ep->ex_fs = fsb.f_fsid;
1164 ep->ex_fsdir = (char *)
1165 malloc(strlen(fsb.f_mntonname) + 1);
1167 strcpy(ep->ex_fsdir,
1172 warnx("making new ep fs=0x%x,0x%x",
1176 warnx("found ep fs=0x%x,0x%x",
1182 * Add dirpath to export mount point.
1184 dirp = add_expdir(&dirhead, cp, len);
1187 getexp_err(ep, tgrp);
1196 getexp_err(ep, tgrp);
1201 * Get the host or netgroup.
1204 netgrp = getnetgrent(&hst, &usr, &dom);
1207 grp->gr_next = get_grp();
1213 "null hostname in netgroup %s, skipping", cp);
1214 grp->gr_type = GT_IGNORE;
1215 } else if (get_host(hst, grp, tgrp)) {
1217 "bad host %s in netgroup %s, skipping", hst, cp);
1218 grp->gr_type = GT_IGNORE;
1220 } else if (get_host(cp, grp, tgrp)) {
1221 syslog(LOG_ERR, "bad host %s, skipping", cp);
1222 grp->gr_type = GT_IGNORE;
1225 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1230 nextfield(&cp, &endcp);
1233 if (check_options(dirhead)) {
1234 getexp_err(ep, tgrp);
1238 grp->gr_type = GT_DEFAULT;
1240 warnx("adding a default entry");
1243 * Don't allow a network export coincide with a list of
1244 * host(s) on the same line.
1246 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1247 syslog(LOG_ERR, "network/host conflict");
1248 getexp_err(ep, tgrp);
1252 * If an export list was specified on this line, make sure
1253 * that we have at least one valid entry, otherwise skip it.
1257 while (grp && grp->gr_type == GT_IGNORE)
1260 getexp_err(ep, tgrp);
1266 * Loop through hosts, pushing the exports into the kernel.
1267 * After loop, tgrp points to the start of the list and
1268 * grp points to the last entry in the list.
1272 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1274 getexp_err(ep, tgrp);
1277 } while (grp->gr_next && (grp = grp->gr_next));
1280 * Success. Update the data structures.
1283 hang_dirp(dirhead, tgrp, ep, opt_flags);
1284 grp->gr_next = grphead;
1287 hang_dirp(dirhead, NULL, ep,
1292 if ((ep->ex_flag & EX_LINKED) == 0) {
1297 * Insert in the list in alphabetical order.
1299 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1300 epp = &ep2->ex_next;
1306 ep->ex_flag |= EX_LINKED;
1318 * Get the export list from all specified files
1321 get_exportlist(void)
1323 struct exportlist *ep, *ep2;
1324 struct grouplist *grp, *tgrp;
1325 struct statfs *fsp, *mntbufp;
1334 * First, get rid of the old list
1353 * And delete exports that are in the kernel for all local
1355 * XXX: Should know how to handle all local exportable filesystems.
1357 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1358 for (i = 0; i < num; i++) {
1363 struct msdosfs_args da;
1364 struct ntfs_args na;
1366 struct export_args export;
1369 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1370 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1376 * Do not delete export for network filesystem by
1377 * passing "export" arg to mount().
1378 * It only makes sense to do this for local filesystems.
1380 if (vfc.vfc_flags & VFCF_NETWORK)
1383 export.ex_flags = MNT_DELEXPORT;
1384 if (mountctl(fsp->f_mntonname, MOUNTCTL_SET_EXPORT, -1,
1385 &export, sizeof(export), NULL, 0) == 0) {
1386 } else if (!strcmp(fsp->f_fstypename, "mfs") ||
1387 !strcmp(fsp->f_fstypename, "ufs") ||
1388 !strcmp(fsp->f_fstypename, "msdos") ||
1389 !strcmp(fsp->f_fstypename, "ntfs") ||
1390 !strcmp(fsp->f_fstypename, "cd9660")) {
1391 bzero(&targs, sizeof targs);
1392 targs.ua.fspec = NULL;
1393 targs.ua.export.ex_flags = MNT_DELEXPORT;
1394 if (mount(fsp->f_fstypename, fsp->f_mntonname,
1395 fsp->f_flags | MNT_UPDATE,
1396 (caddr_t)&targs) < 0)
1397 syslog(LOG_ERR, "can't delete exports for %s",
1403 * Read in the exports file and build the list, calling
1404 * nmount() as we go along to push the export rules into the kernel.
1407 for (i = 0; exnames[i] != NULL; i++) {
1409 warnx("reading exports from %s", exnames[i]);
1410 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1411 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1414 get_exportlist_one();
1419 syslog(LOG_ERR, "can't open any exports file");
1425 * Allocate an export list element
1430 struct exportlist *ep;
1432 ep = (struct exportlist *)malloc(sizeof (struct exportlist));
1435 memset(ep, 0, sizeof(struct exportlist));
1440 * Allocate a group list element
1445 struct grouplist *gp;
1447 gp = (struct grouplist *)malloc(sizeof (struct grouplist));
1450 memset(gp, 0, sizeof(struct grouplist));
1455 * Clean up upon an error in get_exportlist().
1458 getexp_err(struct exportlist *ep, struct grouplist *grp)
1460 struct grouplist *tgrp;
1462 if (!(opt_flags & OP_QUIET))
1463 syslog(LOG_ERR, "bad exports list line %s", line);
1464 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1474 * Search the export list for a matching fs.
1477 ex_search(fsid_t *fsid)
1479 struct exportlist *ep;
1483 if (ep->ex_fs.val[0] == fsid->val[0] &&
1484 ep->ex_fs.val[1] == fsid->val[1])
1492 * Add a directory path to the list.
1495 add_expdir(struct dirlist **dpp, char *cp, int len)
1499 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
1503 dp->dp_right = NULL;
1505 dp->dp_hosts = NULL;
1506 strcpy(dp->dp_dirp, cp);
1508 return (dp->dp_dirp);
1512 * Hang the dir list element off the dirpath binary tree as required
1513 * and update the entry for host.
1516 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1519 struct hostlist *hp;
1520 struct dirlist *dp2;
1522 if (flags & OP_ALLDIRS) {
1528 ep->ex_defdir->dp_flag |= DP_DEFSET;
1529 } else while (grp) {
1532 hp->ht_next = ep->ex_defdir->dp_hosts;
1533 ep->ex_defdir->dp_hosts = hp;
1539 * Loop through the directories adding them to the tree.
1543 add_dlist(&ep->ex_dirl, dp, grp, flags);
1550 * Traverse the binary tree either updating a node that is already there
1551 * for the new directory or adding the new node.
1554 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
1558 struct hostlist *hp;
1563 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1565 add_dlist(&dp->dp_left, newdp, grp, flags);
1567 } else if (cmp < 0) {
1568 add_dlist(&dp->dp_right, newdp, grp, flags);
1571 free((caddr_t)newdp);
1580 * Hang all of the host(s) off of the directory point.
1585 hp->ht_next = dp->dp_hosts;
1590 dp->dp_flag |= DP_DEFSET;
1595 * Search for a dirpath on the export point.
1598 dirp_search(struct dirlist *dp, char *dirp)
1603 cmp = strcmp(dp->dp_dirp, dirp);
1605 return (dirp_search(dp->dp_left, dirp));
1607 return (dirp_search(dp->dp_right, dirp));
1615 * Scan for a host match in a directory tree.
1618 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
1621 struct hostlist *hp;
1622 struct grouplist *grp;
1623 struct addrinfo *ai;
1626 if (dp->dp_flag & DP_DEFSET)
1627 *defsetp = dp->dp_flag;
1631 switch (grp->gr_type) {
1633 ai = grp->gr_ptr.gt_addrinfo;
1634 for (; ai; ai = ai->ai_next) {
1635 if (!sacmp(ai->ai_addr, saddr, NULL)) {
1637 (hp->ht_flag | DP_HOSTSET);
1643 if (!sacmp(saddr, (struct sockaddr *)
1644 &grp->gr_ptr.gt_net.nt_net,
1646 &grp->gr_ptr.gt_net.nt_mask)) {
1647 *hostsetp = (hp->ht_flag | DP_HOSTSET);
1659 * Scan tree for a host that matches the address.
1662 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
1664 int defset, hostset;
1667 if (scan_tree(dp->dp_left, saddr))
1669 if (chk_host(dp, saddr, &defset, &hostset))
1671 if (scan_tree(dp->dp_right, saddr))
1678 * Traverse the dirlist tree and free it up.
1681 free_dir(struct dirlist *dp)
1685 free_dir(dp->dp_left);
1686 free_dir(dp->dp_right);
1687 free_host(dp->dp_hosts);
1693 * Parse the option string and update fields.
1694 * Option arguments may either be -<option>=<value> or
1698 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
1699 int *has_hostp, int *exflagsp, struct ucred *cr)
1701 char *cpoptarg, *cpoptend;
1702 char *cp, *endcp, *cpopt, savedc, savedc2;
1703 int allflag, usedarg;
1711 while (cpopt && *cpopt) {
1714 if ((cpoptend = strchr(cpopt, ','))) {
1716 if ((cpoptarg = strchr(cpopt, '=')))
1719 if ((cpoptarg = strchr(cpopt, '=')))
1723 nextfield(&cp, &endcp);
1725 if (endcp > cp && *cp != '-') {
1733 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1734 *exflagsp |= MNT_EXRDONLY;
1735 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1736 !(allflag = strcmp(cpopt, "mapall")) ||
1737 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1739 parsecred(cpoptarg, cr);
1741 *exflagsp |= MNT_EXPORTANON;
1742 opt_flags |= OP_MAPALL;
1744 opt_flags |= OP_MAPROOT;
1745 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1746 !strcmp(cpopt, "m"))) {
1747 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1748 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
1752 opt_flags |= OP_MASK;
1753 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1754 !strcmp(cpopt, "n"))) {
1755 if (strchr(cpoptarg, '/') != NULL) {
1757 fprintf(stderr, "setting OP_MASKLEN\n");
1758 opt_flags |= OP_MASKLEN;
1760 if (grp->gr_type != GT_NULL) {
1761 syslog(LOG_ERR, "network/host conflict");
1763 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1764 syslog(LOG_ERR, "bad net: %s", cpoptarg);
1767 grp->gr_type = GT_NET;
1770 opt_flags |= OP_NET;
1771 } else if (!strcmp(cpopt, "alldirs")) {
1772 opt_flags |= OP_ALLDIRS;
1773 } else if (!strcmp(cpopt, "public")) {
1774 *exflagsp |= MNT_EXPUBLIC;
1775 } else if (!strcmp(cpopt, "webnfs")) {
1776 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
1777 opt_flags |= OP_MAPALL;
1778 } else if (cpoptarg && !strcmp(cpopt, "index")) {
1779 ep->ex_indexfile = strdup(cpoptarg);
1780 } else if (!strcmp(cpopt, "quiet")) {
1781 opt_flags |= OP_QUIET;
1783 syslog(LOG_ERR, "bad opt %s", cpopt);
1802 * Translate a character string to the corresponding list of network
1803 * addresses for a hostname.
1806 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
1808 struct grouplist *checkgrp;
1809 struct addrinfo *ai, *tai, hints;
1811 char host[NI_MAXHOST];
1813 if (grp->gr_type != GT_NULL) {
1814 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
1817 memset(&hints, 0, sizeof hints);
1818 hints.ai_flags = AI_CANONNAME;
1819 hints.ai_protocol = IPPROTO_UDP;
1820 ecode = getaddrinfo(cp, NULL, &hints, &ai);
1822 syslog(LOG_ERR,"can't get address info for host %s", cp);
1825 grp->gr_ptr.gt_addrinfo = ai;
1826 while (ai != NULL) {
1827 if (ai->ai_canonname == NULL) {
1828 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
1829 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
1830 strlcpy(host, "?", sizeof(host));
1831 ai->ai_canonname = strdup(host);
1832 ai->ai_flags |= AI_CANONNAME;
1835 fprintf(stderr, "got host %s\n", ai->ai_canonname);
1837 * Sanity check: make sure we don't already have an entry
1838 * for this host in the grouplist.
1840 for (checkgrp = tgrp; checkgrp != NULL;
1841 checkgrp = checkgrp->gr_next) {
1842 if (checkgrp->gr_type != GT_HOST)
1844 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
1845 tai = tai->ai_next) {
1846 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
1850 "ignoring duplicate host %s\n",
1852 grp->gr_type = GT_IGNORE;
1858 grp->gr_type = GT_HOST;
1863 * Free up an exports list component
1866 free_exp(struct exportlist *ep)
1869 if (ep->ex_defdir) {
1870 free_host(ep->ex_defdir->dp_hosts);
1871 free((caddr_t)ep->ex_defdir);
1875 if (ep->ex_indexfile)
1876 free(ep->ex_indexfile);
1877 free_dir(ep->ex_dirl);
1885 free_host(struct hostlist *hp)
1887 struct hostlist *hp2;
1899 struct hostlist *hp;
1901 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
1910 * Out of memory, fatal
1916 syslog(LOG_ERR, "out of memory");
1921 * Do the mount syscall with the update flag to push the export info into
1925 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
1926 struct ucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
1929 struct addrinfo *ai;
1930 struct export_args *eap;
1938 struct msdosfs_args da;
1939 struct ntfs_args na;
1942 bzero(&args, sizeof args);
1943 /* XXX, we assume that all xx_args look like ufs_args. */
1945 eap = &args.ua.export;
1947 eap->ex_flags = exflags;
1948 eap->ex_anon = *anoncrp;
1949 eap->ex_indexfile = ep->ex_indexfile;
1950 if (grp->gr_type == GT_HOST)
1951 ai = grp->gr_ptr.gt_addrinfo;
1956 switch (grp->gr_type) {
1958 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
1960 eap->ex_addr = ai->ai_addr;
1961 eap->ex_addrlen = ai->ai_addrlen;
1962 eap->ex_masklen = 0;
1965 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
1969 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
1970 eap->ex_addrlen = args.ua.export.ex_addr->sa_len;
1972 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
1973 eap->ex_masklen = args.ua.export.ex_mask->sa_len;
1976 eap->ex_addr = NULL;
1977 eap->ex_addrlen = 0;
1978 eap->ex_mask = NULL;
1979 eap->ex_masklen = 0;
1985 syslog(LOG_ERR, "bad grouptype");
1993 * Maybe I should just use the fsb->f_mntonname path instead
1994 * of looping back up the dirp to the mount point??
1995 * Also, needs to know how to export all types of local
1996 * exportable filesystems and not just "ufs".
2001 r = mountctl(fsb->f_mntonname, MOUNTCTL_SET_EXPORT,
2003 &args.ua.export, sizeof(args.ua.export),
2005 if (r < 0 && errno == EOPNOTSUPP) {
2006 r = mount(fsb->f_fstypename, dirp,
2007 fsb->f_flags | MNT_UPDATE,
2015 cp = dirp + dirplen - 1;
2016 if (opt_flags & OP_QUIET)
2018 if (errno == EPERM) {
2020 warnx("can't change attributes for %s",
2023 "can't change attributes for %s", dirp);
2026 if (opt_flags & OP_ALLDIRS) {
2027 if (errno == EINVAL)
2029 "-alldirs requested but %s is not a filesystem mountpoint",
2033 "could not remount %s: %m",
2037 /* back up over the last component */
2038 while (*cp == '/' && cp > dirp)
2040 while (*(cp - 1) != '/' && cp > dirp)
2044 warnx("mnt unsucc");
2045 syslog(LOG_ERR, "can't export %s", dirp);
2050 /* Check that we're still on the same filesystem. */
2051 if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
2052 &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
2054 syslog(LOG_ERR, "can't export %s", dirp);
2070 * Translate a net address.
2072 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2075 get_net(char *cp, struct netmsk *net, int maskflg)
2077 struct netent *np = NULL;
2078 char *name, *p, *prefp;
2079 struct sockaddr_in sin;
2080 struct sockaddr *sa = NULL;
2081 struct addrinfo hints, *ai = NULL;
2082 char netname[NI_MAXHOST];
2086 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2087 p = strchr(cp, '/');
2093 * Check for a numeric address first. We wish to avoid
2094 * possible DNS lookups in getnetbyname().
2096 if (isxdigit(*cp) || *cp == ':') {
2097 memset(&hints, 0, sizeof hints);
2098 /* Ensure the mask and the network have the same family. */
2099 if (maskflg && (opt_flags & OP_NET))
2100 hints.ai_family = net->nt_net.ss_family;
2101 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2102 hints.ai_family = net->nt_mask.ss_family;
2104 hints.ai_family = AF_UNSPEC;
2105 hints.ai_flags = AI_NUMERICHOST;
2106 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2108 if (sa != NULL && ai->ai_family == AF_INET) {
2110 * The address in `cp' is really a network address, so
2111 * use inet_network() to re-interpret this correctly.
2112 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2114 bzero(&sin, sizeof sin);
2115 sin.sin_family = AF_INET;
2116 sin.sin_len = sizeof sin;
2117 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2119 fprintf(stderr, "get_net: v4 addr %s\n",
2120 inet_ntoa(sin.sin_addr));
2121 sa = (struct sockaddr *)&sin;
2124 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2125 bzero(&sin, sizeof sin);
2126 sin.sin_family = AF_INET;
2127 sin.sin_len = sizeof sin;
2128 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2129 sa = (struct sockaddr *)&sin;
2135 /* The specified sockaddr is a mask. */
2136 if (checkmask(sa) != 0)
2138 bcopy(sa, &net->nt_mask, sa->sa_len);
2139 opt_flags |= OP_HAVEMASK;
2141 /* The specified sockaddr is a network address. */
2142 bcopy(sa, &net->nt_net, sa->sa_len);
2144 /* Get a network name for the export list. */
2147 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2148 NULL, 0, NI_NUMERICHOST) == 0) {
2153 if ((net->nt_name = strdup(name)) == NULL)
2157 * Extract a mask from either a "/<masklen>" suffix, or
2158 * from the class of an IPv4 address.
2160 if (opt_flags & OP_MASKLEN) {
2161 preflen = strtol(prefp, NULL, 10);
2162 if (preflen < 0L || preflen == LONG_MAX)
2164 bcopy(sa, &net->nt_mask, sa->sa_len);
2165 if (makemask(&net->nt_mask, (int)preflen) != 0)
2167 opt_flags |= OP_HAVEMASK;
2169 } else if (sa->sa_family == AF_INET &&
2170 (opt_flags & OP_MASK) == 0) {
2173 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2174 if (IN_CLASSA(addr))
2176 else if (IN_CLASSB(addr))
2178 else if (IN_CLASSC(addr))
2180 else if (IN_CLASSD(addr))
2183 preflen = 32; /* XXX */
2185 bcopy(sa, &net->nt_mask, sa->sa_len);
2186 makemask(&net->nt_mask, (int)preflen);
2187 opt_flags |= OP_HAVEMASK;
2202 * Parse out the next white space separated field
2205 nextfield(char **cp, char **endcp)
2210 while (*p == ' ' || *p == '\t')
2212 if (*p == '\n' || *p == '\0')
2216 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2223 * Get an exports file line. Skip over blank lines and handle line
2231 int totlen, cont_line;
2234 * Loop around ignoring blank lines and getting all continuation lines.
2239 if ((p = fgetln(exp_file, &len)) == NULL)
2244 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2254 if (linesize < len + totlen + 1) {
2255 linesize = len + totlen + 1;
2256 line = realloc(line, linesize);
2260 memcpy(line + totlen, p, len);
2262 line[totlen] = '\0';
2263 } while (totlen == 0 || cont_line);
2268 * Parse a description of a credential.
2271 parsecred(char *namelist, struct ucred *cr)
2278 gid_t ngroups, groups[NGROUPS + 1];
2281 * Set up the unprivileged user.
2285 cr->cr_groups[0] = -2;
2288 * Get the user's password table entry.
2290 names = strsep(&namelist, " \t\n");
2291 name = strsep(&names, ":");
2292 if (isdigit(*name) || *name == '-')
2293 pw = getpwuid(atoi(name));
2295 pw = getpwnam(name);
2297 * Credentials specified as those of a user.
2299 if (names == NULL) {
2301 syslog(LOG_ERR, "unknown user: %s", name);
2304 cr->cr_uid = pw->pw_uid;
2305 ngroups = NGROUPS + 1;
2306 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2307 syslog(LOG_ERR, "too many groups");
2309 * Compress out duplicate
2311 cr->cr_ngroups = ngroups - 1;
2312 cr->cr_groups[0] = groups[0];
2313 for (cnt = 2; cnt < ngroups; cnt++)
2314 cr->cr_groups[cnt - 1] = groups[cnt];
2318 * Explicit credential specified as a colon separated list:
2322 cr->cr_uid = pw->pw_uid;
2323 else if (isdigit(*name) || *name == '-')
2324 cr->cr_uid = atoi(name);
2326 syslog(LOG_ERR, "unknown user: %s", name);
2330 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
2331 name = strsep(&names, ":");
2332 if (isdigit(*name) || *name == '-') {
2333 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2335 if ((gr = getgrnam(name)) == NULL) {
2336 syslog(LOG_ERR, "unknown group: %s", name);
2339 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2342 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
2343 syslog(LOG_ERR, "too many groups");
2346 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
2348 * Routines that maintain the remote mounttab
2353 struct mountlist *mlp, **mlpp;
2354 char *host, *dirp, *cp;
2358 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2359 if (errno == ENOENT)
2362 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2367 while (fgets(str, STRSIZ, mlfile) != NULL) {
2369 host = strsep(&cp, " \t\n");
2370 dirp = strsep(&cp, " \t\n");
2371 if (host == NULL || dirp == NULL)
2373 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2376 strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
2377 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2378 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2379 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2380 mlp->ml_next = NULL;
2382 mlpp = &mlp->ml_next;
2388 del_mlist(char *hostp, char *dirp)
2390 struct mountlist *mlp, **mlpp;
2391 struct mountlist *mlp2;
2398 if (!strcmp(mlp->ml_host, hostp) &&
2399 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2402 *mlpp = mlp = mlp->ml_next;
2403 free((caddr_t)mlp2);
2405 mlpp = &mlp->ml_next;
2410 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2411 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
2416 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2424 add_mlist(char *hostp, char *dirp)
2426 struct mountlist *mlp, **mlpp;
2432 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2434 mlpp = &mlp->ml_next;
2437 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2440 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2441 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2442 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2443 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2444 mlp->ml_next = NULL;
2446 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2447 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
2450 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2455 * Free up a group list.
2458 free_grp(struct grouplist *grp)
2460 if (grp->gr_type == GT_HOST) {
2461 if (grp->gr_ptr.gt_addrinfo != NULL)
2462 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
2463 } else if (grp->gr_type == GT_NET) {
2464 if (grp->gr_ptr.gt_net.nt_name)
2465 free(grp->gr_ptr.gt_net.nt_name);
2472 SYSLOG(int pri, const char *fmt, ...)
2477 vfprintf(stderr, fmt, ap);
2483 * Check options for consistency.
2486 check_options(struct dirlist *dp)
2491 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
2492 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
2495 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2496 syslog(LOG_ERR, "-mask requires -network");
2499 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
2500 syslog(LOG_ERR, "-network requires mask specification");
2503 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
2504 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
2507 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2508 syslog(LOG_ERR, "-alldirs has multiple directories");
2515 * Check an absolute directory path for any symbolic links. Return true
2518 check_dirpath(char *dirp)
2525 while (*cp && ret) {
2528 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2534 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2540 * Make a netmask according to the specified prefix length. The ss_family
2541 * and other non-address fields must be initialised before calling this.
2544 makemask(struct sockaddr_storage *ssp, int bitlen)
2549 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
2551 if (bitlen > len * CHAR_BIT)
2553 for (i = 0; i < len; i++) {
2554 bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
2555 *p++ = (1 << bits) - 1;
2562 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
2563 * is acceptable (i.e. of the form 1...10....0).
2566 checkmask(struct sockaddr *sa)
2571 if ((mask = sa_rawaddr(sa, &len)) == NULL)
2574 for (i = 0; i < len; i++)
2575 if (mask[i] != 0xff)
2578 if (~mask[i] & (u_char)(~mask[i] + 1))
2582 for (; i < len; i++)
2589 * Compare two sockaddrs according to a specified mask. Return zero if
2590 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
2591 * If samask is NULL, perform a full comparision.
2594 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
2596 unsigned char *p1, *p2, *mask;
2599 if (sa1->sa_family != sa2->sa_family ||
2600 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
2601 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
2604 switch (sa1->sa_family) {
2606 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
2607 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
2612 /* Simple binary comparison if no mask specified. */
2614 return (memcmp(p1, p2, len));
2616 /* Set up the mask, and do a mask-based comparison. */
2617 if (sa1->sa_family != samask->sa_family ||
2618 (mask = sa_rawaddr(samask, NULL)) == NULL)
2621 for (i = 0; i < len; i++)
2622 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
2628 * Return a pointer to the part of the sockaddr that contains the
2629 * raw address, and set *nbytes to its length in bytes. Returns
2630 * NULL if the address family is unknown.
2633 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
2637 switch (sa->sa_family) {
2639 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2640 p = &((struct sockaddr_in *)sa)->sin_addr;
2643 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2644 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
2662 void terminate(int sig)
2664 pidfile_remove(pfh);
2665 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
2666 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);