2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Herb Hasler and Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/fcntl.h>
50 #include <sys/linker.h>
51 #include <sys/module.h>
52 #include <sys/mount.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
58 #include <rpc/rpc_com.h>
59 #include <rpc/pmap_clnt.h>
60 #include <rpc/pmap_prot.h>
61 #include <rpcsvc/mount.h>
62 #include <nfs/nfsproto.h>
63 #include <nfs/nfssvc.h>
64 #include <nfsserver/nfs.h>
66 #include <fs/nfs/nfsport.h>
68 #include <arpa/inet.h>
83 #include "pathnames.h"
91 * Structures for keeping the mount list and export list
94 struct mountlist *ml_next;
95 char ml_host[MNTNAMLEN+1];
96 char ml_dirp[MNTPATHLEN+1];
100 struct dirlist *dp_left;
101 struct dirlist *dp_right;
103 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
104 char dp_dirp[1]; /* Actually malloc'd to size of dir */
107 #define DP_DEFSET 0x1
108 #define DP_HOSTSET 0x2
111 struct exportlist *ex_next;
112 struct dirlist *ex_dirl;
113 struct dirlist *ex_defdir;
118 int ex_numsecflavors;
119 int ex_secflavors[MAXSECFLAVORS];
122 #define EX_LINKED 0x1
125 struct sockaddr_storage nt_net;
126 struct sockaddr_storage nt_mask;
131 struct addrinfo *gt_addrinfo;
132 struct netmsk gt_net;
137 union grouptypes gr_ptr;
138 struct grouplist *gr_next;
144 #define GT_DEFAULT 0x3
145 #define GT_IGNORE 0x5
148 int ht_flag; /* Uses DP_xx bits */
149 struct grouplist *ht_grp;
150 struct hostlist *ht_next;
157 int fhr_numsecflavors;
161 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
164 char *add_expdir(struct dirlist **, char *, int);
165 void add_dlist(struct dirlist **, struct dirlist *,
166 struct grouplist *, int);
167 void add_mlist(char *, char *);
168 int check_dirpath(char *);
169 int check_options(struct dirlist *);
170 int checkmask(struct sockaddr *sa);
171 int chk_host(struct dirlist *, struct sockaddr *, int *, int *);
172 static int create_service(struct netconfig *nconf);
173 static void complete_service(struct netconfig *nconf, char *port_str);
174 static void clearout_service(void);
175 void del_mlist(char *hostp, char *dirp);
176 struct dirlist *dirp_search(struct dirlist *, char *);
177 int do_mount(struct exportlist *, struct grouplist *, int,
178 struct xucred *, char *, int, struct statfs *);
179 int do_opt(char **, char **, struct exportlist *, struct grouplist *,
180 int *, int *, struct xucred *);
181 struct exportlist *ex_search(fsid_t *);
182 struct exportlist *get_exp(void);
183 void free_dir(struct dirlist *);
184 void free_exp(struct exportlist *);
185 void free_grp(struct grouplist *);
186 void free_host(struct hostlist *);
187 void get_exportlist(void);
188 int get_host(char *, struct grouplist *, struct grouplist *);
189 struct hostlist *get_ht(void);
191 void get_mountlist(void);
192 int get_net(char *, struct netmsk *, int);
193 void getexp_err(struct exportlist *, struct grouplist *);
194 struct grouplist *get_grp(void);
195 void hang_dirp(struct dirlist *, struct grouplist *,
196 struct exportlist *, int);
197 void huphandler(int sig);
198 int makemask(struct sockaddr_storage *ssp, int bitlen);
199 void mntsrv(struct svc_req *, SVCXPRT *);
200 void nextfield(char **, char **);
201 void out_of_mem(void);
202 void parsecred(char *, struct xucred *);
203 int parsesec(char *, struct exportlist *);
204 int put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int);
205 void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
206 int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
207 struct sockaddr *samask);
208 int scan_tree(struct dirlist *, struct sockaddr *);
209 static void usage(void);
210 int xdr_dir(XDR *, char *);
211 int xdr_explist(XDR *, caddr_t);
212 int xdr_explist_brief(XDR *, caddr_t);
213 int xdr_explist_common(XDR *, caddr_t, int);
214 int xdr_fhs(XDR *, caddr_t);
215 int xdr_mlist(XDR *, caddr_t);
218 struct exportlist *exphead;
219 struct mountlist *mlhead;
220 struct grouplist *grphead;
221 char *exnames_default[2] = { _PATH_EXPORTS, NULL };
224 struct xucred def_anon = {
232 int resvport_only = 1;
239 char *svcport_str = NULL;
240 static int mallocd_svcport = 0;
242 static int sock_fdcnt;
243 static int sock_fdpos;
246 static int have_v6 = 1;
248 int v4root_phase = 0;
249 char v4root_dirpath[PATH_MAX + 1];
250 int run_v4server = 0;
251 int has_publicfh = 0;
253 struct pidfh *pfh = NULL;
254 /* Bits for opt_flags above */
255 #define OP_MAPROOT 0x01
256 #define OP_MAPALL 0x02
260 #define OP_ALLDIRS 0x40
261 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
262 #define OP_QUIET 0x100
263 #define OP_MASKLEN 0x200
268 void SYSLOG(int, const char *, ...) __printflike(2, 3);
269 #define syslog SYSLOG
275 * Mountd server for NFS mount protocol as described in:
276 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
277 * The optional arguments are the exports file name
278 * default: _PATH_EXPORTS
279 * and "-n" to allow nonroot mount.
282 main(int argc, char **argv)
285 struct netconfig *nconf;
286 char *endptr, **hosts_bak;
291 int maxrec = RPC_MAXDATASIZE;
292 int attempt_cnt, port_len, port_pos, ret;
295 /* Check that another mountd isn't already running. */
296 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
299 errx(1, "mountd already running, pid: %d.", otherpid);
300 warn("cannot open or create pidfile");
303 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
309 while ((c = getopt(argc, argv, "2deh:lnp:r")) != -1)
324 debug = debug ? 0 : 1;
331 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
332 if (endptr == NULL || *endptr != '\0' ||
333 svcport == 0 || svcport >= IPPORT_MAX)
335 svcport_str = strdup(optarg);
340 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
341 if (hosts_bak == NULL) {
343 for (k = 0; k < nhosts; k++)
350 hosts[nhosts - 1] = strdup(optarg);
351 if (hosts[nhosts - 1] == NULL) {
352 for (k = 0; k < (nhosts - 1); k++)
363 * If the "-e" option was specified OR only the nfsd module is
364 * found in the server, run "nfsd".
365 * Otherwise, try and run "nfsserver".
367 if (run_v4server > 0) {
368 if (modfind("nfsd") < 0) {
369 /* Not present in kernel, try loading it */
370 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
371 errx(1, "NFS server is not available");
373 } else if (modfind("nfsserver") < 0 && modfind("nfsd") >= 0) {
375 } else if (modfind("nfsserver") < 0) {
376 /* Not present in kernel, try loading it */
377 if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
378 errx(1, "NFS server is not available");
383 grphead = (struct grouplist *)NULL;
384 exphead = (struct exportlist *)NULL;
385 mlhead = (struct mountlist *)NULL;
389 exnames = exnames_default;
390 openlog("mountd", LOG_PID, LOG_DAEMON);
392 warnx("getting export list");
395 warnx("getting mount list");
401 signal(SIGINT, SIG_IGN);
402 signal(SIGQUIT, SIG_IGN);
404 signal(SIGHUP, huphandler);
405 signal(SIGTERM, terminate);
406 signal(SIGPIPE, SIG_IGN);
410 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
411 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
412 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
414 if (!resvport_only) {
415 if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
416 &resvport_only, sizeof(resvport_only)) != 0 &&
418 syslog(LOG_ERR, "sysctl: %m");
424 * If no hosts were specified, add a wildcard entry to bind to
425 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the
429 hosts = malloc(sizeof(char**));
437 hosts_bak = realloc(hosts, (nhosts + 2) *
439 if (hosts_bak == NULL) {
440 for (k = 0; k < nhosts; k++)
447 hosts[nhosts - 2] = "::1";
449 hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *));
450 if (hosts_bak == NULL) {
451 for (k = 0; k < nhosts; k++)
461 hosts[nhosts - 1] = "127.0.0.1";
469 nc_handle = setnetconfig();
470 while ((nconf = getnetconfig(nc_handle))) {
471 if (nconf->nc_flag & NC_VISIBLE) {
472 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
476 ret = create_service(nconf);
478 /* Ignore this call */
482 * Failed to bind port, so close off
483 * all sockets created and try again
484 * if the port# was dynamically
485 * assigned via bind(2).
488 if (mallocd_svcport != 0 &&
489 attempt_cnt < GETPORT_MAXTRY) {
496 "bindresvport_sa: %m");
500 /* Start over at the first service. */
504 nc_handle = setnetconfig();
506 } else if (mallocd_svcport != 0 &&
507 attempt_cnt == GETPORT_MAXTRY) {
509 * For the last attempt, allow
510 * different port #s for each nconf
511 * by saving the svcport_str and
512 * setting it back to NULL.
514 port_list = realloc(port_list,
515 (port_len + 1) * sizeof(char *));
516 if (port_list == NULL)
518 port_list[port_len++] = svcport_str;
527 * Successfully bound the ports, so call complete_service() to
528 * do the rest of the setup on the service(s).
532 nc_handle = setnetconfig();
533 while ((nconf = getnetconfig(nc_handle))) {
534 if (nconf->nc_flag & NC_VISIBLE) {
535 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
538 } else if (port_list != NULL) {
539 if (port_pos >= port_len) {
540 syslog(LOG_ERR, "too many port#s");
543 complete_service(nconf, port_list[port_pos++]);
545 complete_service(nconf, svcport_str);
548 endnetconfig(nc_handle);
550 if (port_list != NULL) {
551 for (port_pos = 0; port_pos < port_len; port_pos++)
552 free(port_list[port_pos]);
557 syslog(LOG_ERR, "could not create any services");
561 /* Expand svc_run() here so that we can call get_exportlist(). */
568 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
572 syslog(LOG_ERR, "mountd died: select: %m");
577 svc_getreqset(&readfds);
583 * This routine creates and binds sockets on the appropriate
584 * addresses. It gets called one time for each transport.
585 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
586 * bind failed with EADDRINUSE.
587 * Any file descriptors that have been created are stored in sock_fd and
588 * the total count of them is maintained in sock_fdcnt.
591 create_service(struct netconfig *nconf)
593 struct addrinfo hints, *res = NULL;
594 struct sockaddr_in *sin;
595 struct sockaddr_in6 *sin6;
596 struct __rpc_sockinfo si;
602 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
605 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
606 (nconf->nc_semantics != NC_TPI_COTS) &&
607 (nconf->nc_semantics != NC_TPI_COTS_ORD))
608 return (1); /* not my type */
611 * XXX - using RPC library internal functions.
613 if (!__rpc_nconf2sockinfo(nconf, &si)) {
614 syslog(LOG_ERR, "cannot get information for %s",
619 /* Get mountd's address on this transport */
620 memset(&hints, 0, sizeof hints);
621 hints.ai_flags = AI_PASSIVE;
622 hints.ai_family = si.si_af;
623 hints.ai_socktype = si.si_socktype;
624 hints.ai_protocol = si.si_proto;
627 * Bind to specific IPs if asked to
630 while (nhostsbak > 0) {
632 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
635 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
639 * XXX - using RPC library internal functions.
641 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
643 if (errno == EPROTONOSUPPORT &&
644 nconf->nc_semantics != NC_TPI_CLTS)
647 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
648 "cannot create socket for %s", nconf->nc_netid);
654 switch (hints.ai_family) {
656 if (inet_pton(AF_INET, hosts[nhostsbak],
658 hints.ai_flags |= AI_NUMERICHOST;
661 * Skip if we have an AF_INET6 address.
663 if (inet_pton(AF_INET6, hosts[nhostsbak],
671 if (inet_pton(AF_INET6, hosts[nhostsbak],
673 hints.ai_flags |= AI_NUMERICHOST;
676 * Skip if we have an AF_INET address.
678 if (inet_pton(AF_INET, hosts[nhostsbak],
686 * We're doing host-based access checks here, so don't
687 * allow v4-in-v6 to confuse things. The kernel will
688 * disable it by default on NFS sockets too.
690 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
693 "can't disable v4-in-v6 on IPv6 socket");
702 * If no hosts were specified, just bind to INADDR_ANY
704 if (strcmp("*", hosts[nhostsbak]) == 0) {
705 if (svcport_str == NULL) {
706 res = malloc(sizeof(struct addrinfo));
710 res->ai_flags = hints.ai_flags;
711 res->ai_family = hints.ai_family;
712 res->ai_protocol = hints.ai_protocol;
713 switch (res->ai_family) {
715 sin = malloc(sizeof(struct sockaddr_in));
718 sin->sin_family = AF_INET;
719 sin->sin_port = htons(0);
720 sin->sin_addr.s_addr = htonl(INADDR_ANY);
721 res->ai_addr = (struct sockaddr*) sin;
722 res->ai_addrlen = (socklen_t)
723 sizeof(struct sockaddr_in);
726 sin6 = malloc(sizeof(struct sockaddr_in6));
729 sin6->sin6_family = AF_INET6;
730 sin6->sin6_port = htons(0);
731 sin6->sin6_addr = in6addr_any;
732 res->ai_addr = (struct sockaddr*) sin6;
733 res->ai_addrlen = (socklen_t)
734 sizeof(struct sockaddr_in6);
737 syslog(LOG_ERR, "bad addr fam %d",
742 if ((aicode = getaddrinfo(NULL, svcport_str,
743 &hints, &res)) != 0) {
745 "cannot get local address for %s: %s",
747 gai_strerror(aicode));
753 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
754 &hints, &res)) != 0) {
756 "cannot get local address for %s: %s",
757 nconf->nc_netid, gai_strerror(aicode));
764 sock_fd[sock_fdcnt - 1] = fd;
766 /* Now, attempt the bind. */
767 r = bindresvport_sa(fd, res->ai_addr);
769 if (errno == EADDRINUSE && mallocd_svcport != 0) {
770 if (mallocd_res != 0) {
777 syslog(LOG_ERR, "bindresvport_sa: %m");
781 if (svcport_str == NULL) {
782 svcport_str = malloc(NI_MAXSERV * sizeof(char));
783 if (svcport_str == NULL)
787 if (getnameinfo(res->ai_addr,
788 res->ai_addr->sa_len, NULL, NI_MAXHOST,
789 svcport_str, NI_MAXSERV * sizeof(char),
790 NI_NUMERICHOST | NI_NUMERICSERV))
791 errx(1, "Cannot get port number");
793 if (mallocd_res != 0) {
804 * Called after all the create_service() calls have succeeded, to complete
805 * the setup and registration.
808 complete_service(struct netconfig *nconf, char *port_str)
810 struct addrinfo hints, *res = NULL;
811 struct __rpc_sockinfo si;
812 struct netbuf servaddr;
813 SVCXPRT *transp = NULL;
814 int aicode, fd, nhostsbak;
817 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
818 (nconf->nc_semantics != NC_TPI_COTS) &&
819 (nconf->nc_semantics != NC_TPI_COTS_ORD))
820 return; /* not my type */
823 * XXX - using RPC library internal functions.
825 if (!__rpc_nconf2sockinfo(nconf, &si)) {
826 syslog(LOG_ERR, "cannot get information for %s",
832 while (nhostsbak > 0) {
834 if (sock_fdpos >= sock_fdcnt) {
835 /* Should never happen. */
836 syslog(LOG_ERR, "Ran out of socket fd's");
839 fd = sock_fd[sock_fdpos++];
843 if (nconf->nc_semantics != NC_TPI_CLTS)
844 listen(fd, SOMAXCONN);
846 if (nconf->nc_semantics == NC_TPI_CLTS )
847 transp = svc_dg_create(fd, 0, 0);
849 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
852 if (transp != (SVCXPRT *) NULL) {
853 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
856 "can't register %s MOUNTVERS service",
859 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
862 "can't register %s MOUNTVERS3 service",
866 syslog(LOG_WARNING, "can't create %s services",
869 if (registered == 0) {
871 memset(&hints, 0, sizeof hints);
872 hints.ai_flags = AI_PASSIVE;
873 hints.ai_family = si.si_af;
874 hints.ai_socktype = si.si_socktype;
875 hints.ai_protocol = si.si_proto;
877 if ((aicode = getaddrinfo(NULL, port_str, &hints,
879 syslog(LOG_ERR, "cannot get local address: %s",
880 gai_strerror(aicode));
884 servaddr.buf = malloc(res->ai_addrlen);
885 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
886 servaddr.len = res->ai_addrlen;
888 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
889 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
898 * Clear out sockets after a failure to bind one of them, so that the
899 * cycle of socket creation/binding can start anew.
902 clearout_service(void)
906 for (i = 0; i < sock_fdcnt; i++) {
907 if (sock_fd[i] >= 0) {
908 shutdown(sock_fd[i], SHUT_RDWR);
918 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
919 "[-h <bindip>] [export_file ...]\n");
924 * The mount rpc service
927 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
929 struct exportlist *ep;
934 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
935 int lookup_failed = 1;
936 struct sockaddr *saddr;
938 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
939 int bad = 0, defset, hostset;
940 sigset_t sighup_mask;
942 sigemptyset(&sighup_mask);
943 sigaddset(&sighup_mask, SIGHUP);
944 saddr = svc_getrpccaller(transp)->buf;
945 switch (saddr->sa_family) {
947 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
950 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
953 syslog(LOG_ERR, "request from unknown address family");
956 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
958 getnameinfo(saddr, saddr->sa_len, numerichost,
959 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
960 switch (rqstp->rq_proc) {
962 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
963 syslog(LOG_ERR, "can't send reply");
966 if (sport >= IPPORT_RESERVED && resvport_only) {
968 "mount request from %s from unprivileged port",
970 svcerr_weakauth(transp);
973 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
974 syslog(LOG_NOTICE, "undecodable mount request from %s",
976 svcerr_decode(transp);
981 * Get the real pathname and make sure it is a directory
982 * or a regular file if the -r option was specified
985 if (realpath(rpcpath, dirpath) == NULL ||
986 stat(dirpath, &stb) < 0 ||
987 (!S_ISDIR(stb.st_mode) &&
988 (dir_only || !S_ISREG(stb.st_mode))) ||
989 statfs(dirpath, &fsb) < 0) {
990 chdir("/"); /* Just in case realpath doesn't */
992 "mount request from %s for non existent path %s",
993 numerichost, dirpath);
995 warnx("stat failed on %s", dirpath);
996 bad = ENOENT; /* We will send error reply later */
999 /* Check in the exports list */
1000 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1001 ep = ex_search(&fsb.f_fsid);
1002 hostset = defset = 0;
1003 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
1004 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1005 chk_host(dp, saddr, &defset, &hostset)) ||
1006 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1007 scan_tree(ep->ex_dirl, saddr) == 0))) {
1009 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1011 syslog(LOG_ERR, "can't send reply");
1012 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1015 if (hostset & DP_HOSTSET)
1016 fhr.fhr_flag = hostset;
1018 fhr.fhr_flag = defset;
1019 fhr.fhr_vers = rqstp->rq_vers;
1020 /* Get the file handle */
1021 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1022 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1024 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1025 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1027 syslog(LOG_ERR, "can't send reply");
1028 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1031 fhr.fhr_numsecflavors = ep->ex_numsecflavors;
1032 fhr.fhr_secflavors = ep->ex_secflavors;
1033 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1035 syslog(LOG_ERR, "can't send reply");
1037 add_mlist(host, dirpath);
1039 add_mlist(numerichost, dirpath);
1041 warnx("mount successful");
1044 "mount request succeeded from %s for %s",
1045 numerichost, dirpath);
1049 "mount request denied from %s for %s",
1050 numerichost, dirpath);
1053 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1055 syslog(LOG_ERR, "can't send reply");
1056 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1058 case MOUNTPROC_DUMP:
1059 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1060 syslog(LOG_ERR, "can't send reply");
1063 "dump request succeeded from %s",
1066 case MOUNTPROC_UMNT:
1067 if (sport >= IPPORT_RESERVED && resvport_only) {
1069 "umount request from %s from unprivileged port",
1071 svcerr_weakauth(transp);
1074 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1075 syslog(LOG_NOTICE, "undecodable umount request from %s",
1077 svcerr_decode(transp);
1080 if (realpath(rpcpath, dirpath) == NULL) {
1081 syslog(LOG_NOTICE, "umount request from %s "
1082 "for non existent path %s",
1083 numerichost, dirpath);
1085 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1086 syslog(LOG_ERR, "can't send reply");
1088 del_mlist(host, dirpath);
1089 del_mlist(numerichost, dirpath);
1092 "umount request succeeded from %s for %s",
1093 numerichost, dirpath);
1095 case MOUNTPROC_UMNTALL:
1096 if (sport >= IPPORT_RESERVED && resvport_only) {
1098 "umountall request from %s from unprivileged port",
1100 svcerr_weakauth(transp);
1103 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1104 syslog(LOG_ERR, "can't send reply");
1106 del_mlist(host, NULL);
1107 del_mlist(numerichost, NULL);
1110 "umountall request succeeded from %s",
1113 case MOUNTPROC_EXPORT:
1114 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1115 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1117 syslog(LOG_ERR, "can't send reply");
1120 "export request succeeded from %s",
1124 svcerr_noproc(transp);
1130 * Xdr conversion for a dirpath string
1133 xdr_dir(XDR *xdrsp, char *dirp)
1135 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1139 * Xdr routine to generate file handle reply
1142 xdr_fhs(XDR *xdrsp, caddr_t cp)
1144 struct fhreturn *fhrp = (struct fhreturn *)cp;
1145 u_long ok = 0, len, auth;
1148 if (!xdr_long(xdrsp, &ok))
1150 switch (fhrp->fhr_vers) {
1152 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1155 if (!xdr_long(xdrsp, &len))
1157 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1159 if (fhrp->fhr_numsecflavors) {
1160 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1162 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1163 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1169 if (!xdr_long(xdrsp, &len))
1171 return (xdr_long(xdrsp, &auth));
1178 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1180 struct mountlist *mlp;
1187 if (!xdr_bool(xdrsp, &true))
1189 strp = &mlp->ml_host[0];
1190 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1192 strp = &mlp->ml_dirp[0];
1193 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1197 if (!xdr_bool(xdrsp, &false))
1203 * Xdr conversion for export list
1206 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1208 struct exportlist *ep;
1211 sigset_t sighup_mask;
1213 sigemptyset(&sighup_mask);
1214 sigaddset(&sighup_mask, SIGHUP);
1215 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1219 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1222 if (ep->ex_defdir && putdef == 0 &&
1223 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
1228 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1229 if (!xdr_bool(xdrsp, &false))
1233 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1238 * Called from xdr_explist() to traverse the tree and export the
1242 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1245 struct grouplist *grp;
1246 struct hostlist *hp;
1253 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1255 if (!xdr_bool(xdrsp, &true))
1258 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1260 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1265 if (!xdr_bool(xdrsp, &true))
1268 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1270 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1271 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1275 if (grp->gr_type == GT_HOST) {
1276 if (!xdr_bool(xdrsp, &true))
1278 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1279 if (!xdr_string(xdrsp, &strp,
1282 } else if (grp->gr_type == GT_NET) {
1283 if (!xdr_bool(xdrsp, &true))
1285 strp = grp->gr_ptr.gt_net.nt_name;
1286 if (!xdr_string(xdrsp, &strp,
1291 if (gotalldir && hp == (struct hostlist *)NULL) {
1297 if (!xdr_bool(xdrsp, &false))
1299 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1306 xdr_explist(XDR *xdrsp, caddr_t cp)
1309 return xdr_explist_common(xdrsp, cp, 0);
1313 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1316 return xdr_explist_common(xdrsp, cp, 1);
1324 * Get the export list from one, currently open file
1327 get_exportlist_one(void)
1329 struct exportlist *ep, *ep2;
1330 struct grouplist *grp, *tgrp;
1331 struct exportlist **epp;
1332 struct dirlist *dirhead;
1335 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1336 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1339 dirhead = (struct dirlist *)NULL;
1340 while (get_line()) {
1342 warnx("got line %s", line);
1344 nextfield(&cp, &endcp);
1353 exflags = MNT_EXPORTED;
1356 ep = (struct exportlist *)NULL;
1360 * Handle the V4 root dir.
1362 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1364 * V4: just indicates that it is the v4 root point,
1365 * so skip over that and set v4root_phase.
1367 if (v4root_phase > 0) {
1368 syslog(LOG_ERR, "V4:duplicate line, ignored");
1373 nextfield(&cp, &endcp);
1377 * Create new exports list entry
1380 tgrp = grp = get_grp();
1382 if (len > MNTNAMLEN) {
1383 getexp_err(ep, tgrp);
1387 if (ep == (struct exportlist *)NULL) {
1388 getexp_err(ep, tgrp);
1392 warnx("doing opt %s", cp);
1394 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1396 getexp_err(ep, tgrp);
1399 } else if (*cp == '/') {
1402 if (v4root_phase > 1) {
1404 syslog(LOG_ERR, "Multiple V4 dirs");
1405 getexp_err(ep, tgrp);
1409 if (check_dirpath(cp) &&
1410 statfs(cp, &fsb) >= 0) {
1412 syslog(LOG_ERR, "dirs must be first");
1413 getexp_err(ep, tgrp);
1416 if (v4root_phase == 1) {
1418 syslog(LOG_ERR, "Multiple V4 dirs");
1419 getexp_err(ep, tgrp);
1422 if (strlen(v4root_dirpath) == 0) {
1423 strlcpy(v4root_dirpath, cp,
1424 sizeof (v4root_dirpath));
1425 } else if (strcmp(v4root_dirpath, cp)
1428 "different V4 dirpath %s", cp);
1429 getexp_err(ep, tgrp);
1438 if (ep->ex_fs.val[0] !=
1439 fsb.f_fsid.val[0] ||
1441 fsb.f_fsid.val[1]) {
1442 getexp_err(ep, tgrp);
1447 * See if this directory is already
1450 ep = ex_search(&fsb.f_fsid);
1451 if (ep == (struct exportlist *)NULL) {
1453 ep->ex_fs = fsb.f_fsid;
1454 ep->ex_fsdir = (char *)malloc
1455 (strlen(fsb.f_mntonname) + 1);
1457 strcpy(ep->ex_fsdir,
1463 "making new ep fs=0x%x,0x%x",
1467 warnx("found ep fs=0x%x,0x%x",
1473 * Add dirpath to export mount point.
1475 dirp = add_expdir(&dirhead, cp, len);
1479 getexp_err(ep, tgrp);
1487 if (ep == (struct exportlist *)NULL) {
1488 getexp_err(ep, tgrp);
1493 * Get the host or netgroup.
1496 netgrp = getnetgrent(&hst, &usr, &dom);
1499 grp->gr_next = get_grp();
1505 "null hostname in netgroup %s, skipping", cp);
1506 grp->gr_type = GT_IGNORE;
1507 } else if (get_host(hst, grp, tgrp)) {
1509 "bad host %s in netgroup %s, skipping", hst, cp);
1510 grp->gr_type = GT_IGNORE;
1512 } else if (get_host(cp, grp, tgrp)) {
1513 syslog(LOG_ERR, "bad host %s, skipping", cp);
1514 grp->gr_type = GT_IGNORE;
1517 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1522 nextfield(&cp, &endcp);
1525 if (check_options(dirhead)) {
1526 getexp_err(ep, tgrp);
1530 grp->gr_type = GT_DEFAULT;
1532 warnx("adding a default entry");
1535 * Don't allow a network export coincide with a list of
1536 * host(s) on the same line.
1538 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1539 syslog(LOG_ERR, "network/host conflict");
1540 getexp_err(ep, tgrp);
1544 * If an export list was specified on this line, make sure
1545 * that we have at least one valid entry, otherwise skip it.
1549 while (grp && grp->gr_type == GT_IGNORE)
1552 getexp_err(ep, tgrp);
1557 if (v4root_phase == 1) {
1558 syslog(LOG_ERR, "V4:root, no dirp, ignored");
1559 getexp_err(ep, tgrp);
1564 * Loop through hosts, pushing the exports into the kernel.
1565 * After loop, tgrp points to the start of the list and
1566 * grp points to the last entry in the list.
1570 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1572 getexp_err(ep, tgrp);
1575 } while (grp->gr_next && (grp = grp->gr_next));
1578 * For V4: don't enter in mount lists.
1580 if (v4root_phase > 0 && v4root_phase <= 2) {
1582 * Since these structures aren't used by mountd,
1587 while (tgrp != NULL) {
1589 tgrp = tgrp->gr_next;
1596 * Success. Update the data structures.
1599 hang_dirp(dirhead, tgrp, ep, opt_flags);
1600 grp->gr_next = grphead;
1603 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1607 dirhead = (struct dirlist *)NULL;
1608 if ((ep->ex_flag & EX_LINKED) == 0) {
1613 * Insert in the list in alphabetical order.
1615 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1616 epp = &ep2->ex_next;
1622 ep->ex_flag |= EX_LINKED;
1628 dirhead = (struct dirlist *)NULL;
1634 * Get the export list from all specified files
1637 get_exportlist(void)
1639 struct exportlist *ep, *ep2;
1640 struct grouplist *grp, *tgrp;
1641 struct export_args export;
1643 struct statfs *fsp, *mntbufp;
1644 struct xvfsconf vfc;
1647 int dirplen, num, i;
1650 struct nfsex_args eargs;
1652 v4root_dirpath[0] = '\0';
1653 bzero(&export, sizeof(export));
1654 export.ex_flags = MNT_DELEXPORT;
1659 bzero(errmsg, sizeof(errmsg));
1662 * First, get rid of the old list
1670 exphead = (struct exportlist *)NULL;
1678 grphead = (struct grouplist *)NULL;
1681 * and the old V4 root dir.
1683 bzero(&eargs, sizeof (eargs));
1684 eargs.export.ex_flags = MNT_DELEXPORT;
1685 if (run_v4server > 0 &&
1686 nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1688 syslog(LOG_ERR, "Can't delete exports for V4:");
1691 * and clear flag that notes if a public fh has been exported.
1696 * And delete exports that are in the kernel for all local
1698 * XXX: Should know how to handle all local exportable filesystems.
1700 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1703 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1704 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1705 build_iovec(&iov, &iovlen, "from", NULL, 0);
1706 build_iovec(&iov, &iovlen, "update", NULL, 0);
1707 build_iovec(&iov, &iovlen, "export", &export, sizeof(export));
1708 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
1711 for (i = 0; i < num; i++) {
1713 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
1714 syslog(LOG_ERR, "getvfsbyname() failed for %s",
1720 * Do not delete export for network filesystem by
1721 * passing "export" arg to nmount().
1722 * It only makes sense to do this for local filesystems.
1724 if (vfc.vfc_flags & VFCF_NETWORK)
1727 iov[1].iov_base = fsp->f_fstypename;
1728 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
1729 iov[3].iov_base = fsp->f_mntonname;
1730 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
1731 iov[5].iov_base = fsp->f_mntfromname;
1732 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
1734 if (nmount(iov, iovlen, fsp->f_flags) < 0 &&
1735 errno != ENOENT && errno != ENOTSUP) {
1737 "can't delete exports for %s: %m %s",
1738 fsp->f_mntonname, errmsg);
1743 /* Free strings allocated by strdup() in getmntopts.c */
1744 free(iov[0].iov_base); /* fstype */
1745 free(iov[2].iov_base); /* fspath */
1746 free(iov[4].iov_base); /* from */
1747 free(iov[6].iov_base); /* update */
1748 free(iov[8].iov_base); /* export */
1749 free(iov[10].iov_base); /* errmsg */
1751 /* free iov, allocated by realloc() */
1757 * Read in the exports file and build the list, calling
1758 * nmount() as we go along to push the export rules into the kernel.
1761 for (i = 0; exnames[i] != NULL; i++) {
1763 warnx("reading exports from %s", exnames[i]);
1764 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
1765 syslog(LOG_WARNING, "can't open %s", exnames[i]);
1768 get_exportlist_one();
1773 syslog(LOG_ERR, "can't open any exports file");
1778 * If there was no public fh, clear any previous one set.
1780 if (run_v4server > 0 && has_publicfh == 0)
1781 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
1785 * Allocate an export list element
1790 struct exportlist *ep;
1792 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
1793 if (ep == (struct exportlist *)NULL)
1799 * Allocate a group list element
1804 struct grouplist *gp;
1806 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
1807 if (gp == (struct grouplist *)NULL)
1813 * Clean up upon an error in get_exportlist().
1816 getexp_err(struct exportlist *ep, struct grouplist *grp)
1818 struct grouplist *tgrp;
1820 if (!(opt_flags & OP_QUIET))
1821 syslog(LOG_ERR, "bad exports list line %s", line);
1822 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1832 * Search the export list for a matching fs.
1835 ex_search(fsid_t *fsid)
1837 struct exportlist *ep;
1841 if (ep->ex_fs.val[0] == fsid->val[0] &&
1842 ep->ex_fs.val[1] == fsid->val[1])
1850 * Add a directory path to the list.
1853 add_expdir(struct dirlist **dpp, char *cp, int len)
1857 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
1858 if (dp == (struct dirlist *)NULL)
1861 dp->dp_right = (struct dirlist *)NULL;
1863 dp->dp_hosts = (struct hostlist *)NULL;
1864 strcpy(dp->dp_dirp, cp);
1866 return (dp->dp_dirp);
1870 * Hang the dir list element off the dirpath binary tree as required
1871 * and update the entry for host.
1874 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
1877 struct hostlist *hp;
1878 struct dirlist *dp2;
1880 if (flags & OP_ALLDIRS) {
1885 if (grp == (struct grouplist *)NULL) {
1886 ep->ex_defdir->dp_flag |= DP_DEFSET;
1887 } else while (grp) {
1890 hp->ht_next = ep->ex_defdir->dp_hosts;
1891 ep->ex_defdir->dp_hosts = hp;
1897 * Loop through the directories adding them to the tree.
1901 add_dlist(&ep->ex_dirl, dp, grp, flags);
1908 * Traverse the binary tree either updating a node that is already there
1909 * for the new directory or adding the new node.
1912 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
1916 struct hostlist *hp;
1921 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1923 add_dlist(&dp->dp_left, newdp, grp, flags);
1925 } else if (cmp < 0) {
1926 add_dlist(&dp->dp_right, newdp, grp, flags);
1929 free((caddr_t)newdp);
1932 dp->dp_left = (struct dirlist *)NULL;
1938 * Hang all of the host(s) off of the directory point.
1943 hp->ht_next = dp->dp_hosts;
1948 dp->dp_flag |= DP_DEFSET;
1953 * Search for a dirpath on the export point.
1956 dirp_search(struct dirlist *dp, char *dirp)
1961 cmp = strcmp(dp->dp_dirp, dirp);
1963 return (dirp_search(dp->dp_left, dirp));
1965 return (dirp_search(dp->dp_right, dirp));
1973 * Scan for a host match in a directory tree.
1976 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
1979 struct hostlist *hp;
1980 struct grouplist *grp;
1981 struct addrinfo *ai;
1984 if (dp->dp_flag & DP_DEFSET)
1985 *defsetp = dp->dp_flag;
1989 switch (grp->gr_type) {
1991 ai = grp->gr_ptr.gt_addrinfo;
1992 for (; ai; ai = ai->ai_next) {
1993 if (!sacmp(ai->ai_addr, saddr, NULL)) {
1995 (hp->ht_flag | DP_HOSTSET);
2001 if (!sacmp(saddr, (struct sockaddr *)
2002 &grp->gr_ptr.gt_net.nt_net,
2004 &grp->gr_ptr.gt_net.nt_mask)) {
2005 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2017 * Scan tree for a host that matches the address.
2020 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2022 int defset, hostset;
2025 if (scan_tree(dp->dp_left, saddr))
2027 if (chk_host(dp, saddr, &defset, &hostset))
2029 if (scan_tree(dp->dp_right, saddr))
2036 * Traverse the dirlist tree and free it up.
2039 free_dir(struct dirlist *dp)
2043 free_dir(dp->dp_left);
2044 free_dir(dp->dp_right);
2045 free_host(dp->dp_hosts);
2051 * Parse a colon separated list of security flavors
2054 parsesec(char *seclist, struct exportlist *ep)
2059 ep->ex_numsecflavors = 0;
2061 cp = strchr(seclist, ':');
2067 if (!strcmp(seclist, "sys"))
2069 else if (!strcmp(seclist, "krb5"))
2070 flavor = RPCSEC_GSS_KRB5;
2071 else if (!strcmp(seclist, "krb5i"))
2072 flavor = RPCSEC_GSS_KRB5I;
2073 else if (!strcmp(seclist, "krb5p"))
2074 flavor = RPCSEC_GSS_KRB5P;
2078 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2081 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2084 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2087 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2088 ep->ex_numsecflavors++;
2100 * Parse the option string and update fields.
2101 * Option arguments may either be -<option>=<value> or
2105 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2106 int *has_hostp, int *exflagsp, struct xucred *cr)
2108 char *cpoptarg, *cpoptend;
2109 char *cp, *endcp, *cpopt, savedc, savedc2;
2110 int allflag, usedarg;
2118 while (cpopt && *cpopt) {
2121 if ((cpoptend = strchr(cpopt, ','))) {
2123 if ((cpoptarg = strchr(cpopt, '=')))
2126 if ((cpoptarg = strchr(cpopt, '=')))
2130 nextfield(&cp, &endcp);
2132 if (endcp > cp && *cp != '-') {
2140 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2141 *exflagsp |= MNT_EXRDONLY;
2142 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2143 !(allflag = strcmp(cpopt, "mapall")) ||
2144 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2146 parsecred(cpoptarg, cr);
2148 *exflagsp |= MNT_EXPORTANON;
2149 opt_flags |= OP_MAPALL;
2151 opt_flags |= OP_MAPROOT;
2152 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2153 !strcmp(cpopt, "m"))) {
2154 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2155 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2159 opt_flags |= OP_MASK;
2160 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2161 !strcmp(cpopt, "n"))) {
2162 if (strchr(cpoptarg, '/') != NULL) {
2164 fprintf(stderr, "setting OP_MASKLEN\n");
2165 opt_flags |= OP_MASKLEN;
2167 if (grp->gr_type != GT_NULL) {
2168 syslog(LOG_ERR, "network/host conflict");
2170 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2171 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2174 grp->gr_type = GT_NET;
2177 opt_flags |= OP_NET;
2178 } else if (!strcmp(cpopt, "alldirs")) {
2179 opt_flags |= OP_ALLDIRS;
2180 } else if (!strcmp(cpopt, "public")) {
2181 *exflagsp |= MNT_EXPUBLIC;
2182 } else if (!strcmp(cpopt, "webnfs")) {
2183 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2184 opt_flags |= OP_MAPALL;
2185 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2186 ep->ex_indexfile = strdup(cpoptarg);
2187 } else if (!strcmp(cpopt, "quiet")) {
2188 opt_flags |= OP_QUIET;
2189 } else if (!strcmp(cpopt, "sec")) {
2190 if (parsesec(cpoptarg, ep))
2192 opt_flags |= OP_SEC;
2195 syslog(LOG_ERR, "bad opt %s", cpopt);
2214 * Translate a character string to the corresponding list of network
2215 * addresses for a hostname.
2218 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2220 struct grouplist *checkgrp;
2221 struct addrinfo *ai, *tai, hints;
2223 char host[NI_MAXHOST];
2225 if (grp->gr_type != GT_NULL) {
2226 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2229 memset(&hints, 0, sizeof hints);
2230 hints.ai_flags = AI_CANONNAME;
2231 hints.ai_protocol = IPPROTO_UDP;
2232 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2234 syslog(LOG_ERR,"can't get address info for host %s", cp);
2237 grp->gr_ptr.gt_addrinfo = ai;
2238 while (ai != NULL) {
2239 if (ai->ai_canonname == NULL) {
2240 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2241 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2242 strlcpy(host, "?", sizeof(host));
2243 ai->ai_canonname = strdup(host);
2244 ai->ai_flags |= AI_CANONNAME;
2247 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2249 * Sanity check: make sure we don't already have an entry
2250 * for this host in the grouplist.
2252 for (checkgrp = tgrp; checkgrp != NULL;
2253 checkgrp = checkgrp->gr_next) {
2254 if (checkgrp->gr_type != GT_HOST)
2256 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2257 tai = tai->ai_next) {
2258 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2262 "ignoring duplicate host %s\n",
2264 grp->gr_type = GT_IGNORE;
2270 grp->gr_type = GT_HOST;
2275 * Free up an exports list component
2278 free_exp(struct exportlist *ep)
2281 if (ep->ex_defdir) {
2282 free_host(ep->ex_defdir->dp_hosts);
2283 free((caddr_t)ep->ex_defdir);
2287 if (ep->ex_indexfile)
2288 free(ep->ex_indexfile);
2289 free_dir(ep->ex_dirl);
2297 free_host(struct hostlist *hp)
2299 struct hostlist *hp2;
2311 struct hostlist *hp;
2313 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2314 if (hp == (struct hostlist *)NULL)
2316 hp->ht_next = (struct hostlist *)NULL;
2322 * Out of memory, fatal
2328 syslog(LOG_ERR, "out of memory");
2333 * Do the nmount() syscall with the update flag to push the export info into
2337 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
2338 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb)
2341 struct addrinfo *ai;
2342 struct export_args ea, *eap;
2350 struct nfsex_args nfsea;
2352 if (run_v4server > 0)
2353 eap = &nfsea.export;
2363 bzero(eap, sizeof (struct export_args));
2364 bzero(errmsg, sizeof(errmsg));
2365 eap->ex_flags = exflags;
2366 eap->ex_anon = *anoncrp;
2367 eap->ex_indexfile = ep->ex_indexfile;
2368 if (grp->gr_type == GT_HOST)
2369 ai = grp->gr_ptr.gt_addrinfo;
2372 eap->ex_numsecflavors = ep->ex_numsecflavors;
2373 for (i = 0; i < eap->ex_numsecflavors; i++)
2374 eap->ex_secflavors[i] = ep->ex_secflavors[i];
2375 if (eap->ex_numsecflavors == 0) {
2376 eap->ex_numsecflavors = 1;
2377 eap->ex_secflavors[0] = AUTH_SYS;
2381 if (v4root_phase == 0) {
2382 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
2383 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
2384 build_iovec(&iov, &iovlen, "from", NULL, 0);
2385 build_iovec(&iov, &iovlen, "update", NULL, 0);
2386 build_iovec(&iov, &iovlen, "export", eap,
2387 sizeof (struct export_args));
2388 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
2392 switch (grp->gr_type) {
2394 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
2396 eap->ex_addr = ai->ai_addr;
2397 eap->ex_addrlen = ai->ai_addrlen;
2398 eap->ex_masklen = 0;
2401 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
2405 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
2407 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
2409 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
2410 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
2413 eap->ex_addr = NULL;
2414 eap->ex_addrlen = 0;
2415 eap->ex_mask = NULL;
2416 eap->ex_masklen = 0;
2423 syslog(LOG_ERR, "bad grouptype");
2431 * For V4:, use the nfssvc() syscall, instead of mount().
2433 if (v4root_phase == 2) {
2434 nfsea.fspec = v4root_dirpath;
2435 if (run_v4server > 0 &&
2436 nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
2437 syslog(LOG_ERR, "Exporting V4: failed");
2443 * Maybe I should just use the fsb->f_mntonname path
2444 * instead of looping back up the dirp to the mount
2446 * Also, needs to know how to export all types of local
2447 * exportable filesystems and not just "ufs".
2449 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
2450 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
2451 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
2452 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
2453 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
2454 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
2456 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
2460 cp = dirp + dirplen - 1;
2461 if (opt_flags & OP_QUIET) {
2465 if (errno == EPERM) {
2467 warnx("can't change attributes for %s",
2470 "can't change attributes for %s",
2475 if (opt_flags & OP_ALLDIRS) {
2476 if (errno == EINVAL)
2478 "-alldirs requested but %s is not a filesystem mountpoint",
2482 "could not remount %s: %m",
2487 /* back up over the last component */
2488 while (*cp == '/' && cp > dirp)
2490 while (*(cp - 1) != '/' && cp > dirp)
2494 warnx("mnt unsucc");
2495 syslog(LOG_ERR, "can't export %s %s",
2503 * Check that we're still on the same
2506 if (statfs(dirp, &fsb1) != 0 ||
2507 bcmp(&fsb1.f_fsid, &fsb->f_fsid,
2508 sizeof (fsb1.f_fsid)) != 0) {
2511 "can't export %s %s", dirp,
2520 * For the experimental server:
2521 * If this is the public directory, get the file handle
2522 * and load it into the kernel via the nfssvc() syscall.
2524 if (run_v4server > 0 && (exflags & MNT_EXPUBLIC) != 0) {
2528 if (eap->ex_indexfile != NULL)
2529 public_name = eap->ex_indexfile;
2532 if (getfh(public_name, &fh) < 0)
2534 "Can't get public fh for %s", public_name);
2535 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
2537 "Can't set public fh for %s", public_name);
2550 /* free strings allocated by strdup() in getmntopts.c */
2552 free(iov[0].iov_base); /* fstype */
2553 free(iov[2].iov_base); /* fspath */
2554 free(iov[4].iov_base); /* from */
2555 free(iov[6].iov_base); /* update */
2556 free(iov[8].iov_base); /* export */
2557 free(iov[10].iov_base); /* errmsg */
2559 /* free iov, allocated by realloc() */
2566 * Translate a net address.
2568 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
2571 get_net(char *cp, struct netmsk *net, int maskflg)
2573 struct netent *np = NULL;
2574 char *name, *p, *prefp;
2575 struct sockaddr_in sin;
2576 struct sockaddr *sa = NULL;
2577 struct addrinfo hints, *ai = NULL;
2578 char netname[NI_MAXHOST];
2582 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2583 p = strchr(cp, '/');
2589 * Check for a numeric address first. We wish to avoid
2590 * possible DNS lookups in getnetbyname().
2592 if (isxdigit(*cp) || *cp == ':') {
2593 memset(&hints, 0, sizeof hints);
2594 /* Ensure the mask and the network have the same family. */
2595 if (maskflg && (opt_flags & OP_NET))
2596 hints.ai_family = net->nt_net.ss_family;
2597 else if (!maskflg && (opt_flags & OP_HAVEMASK))
2598 hints.ai_family = net->nt_mask.ss_family;
2600 hints.ai_family = AF_UNSPEC;
2601 hints.ai_flags = AI_NUMERICHOST;
2602 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2604 if (sa != NULL && ai->ai_family == AF_INET) {
2606 * The address in `cp' is really a network address, so
2607 * use inet_network() to re-interpret this correctly.
2608 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
2610 bzero(&sin, sizeof sin);
2611 sin.sin_family = AF_INET;
2612 sin.sin_len = sizeof sin;
2613 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
2615 fprintf(stderr, "get_net: v4 addr %s\n",
2616 inet_ntoa(sin.sin_addr));
2617 sa = (struct sockaddr *)&sin;
2620 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
2621 bzero(&sin, sizeof sin);
2622 sin.sin_family = AF_INET;
2623 sin.sin_len = sizeof sin;
2624 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2625 sa = (struct sockaddr *)&sin;
2631 /* The specified sockaddr is a mask. */
2632 if (checkmask(sa) != 0)
2634 bcopy(sa, &net->nt_mask, sa->sa_len);
2635 opt_flags |= OP_HAVEMASK;
2637 /* The specified sockaddr is a network address. */
2638 bcopy(sa, &net->nt_net, sa->sa_len);
2640 /* Get a network name for the export list. */
2643 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2644 NULL, 0, NI_NUMERICHOST) == 0) {
2649 if ((net->nt_name = strdup(name)) == NULL)
2653 * Extract a mask from either a "/<masklen>" suffix, or
2654 * from the class of an IPv4 address.
2656 if (opt_flags & OP_MASKLEN) {
2657 preflen = strtol(prefp, NULL, 10);
2658 if (preflen < 0L || preflen == LONG_MAX)
2660 bcopy(sa, &net->nt_mask, sa->sa_len);
2661 if (makemask(&net->nt_mask, (int)preflen) != 0)
2663 opt_flags |= OP_HAVEMASK;
2665 } else if (sa->sa_family == AF_INET &&
2666 (opt_flags & OP_MASK) == 0) {
2669 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
2670 if (IN_CLASSA(addr))
2672 else if (IN_CLASSB(addr))
2674 else if (IN_CLASSC(addr))
2676 else if (IN_CLASSD(addr))
2679 preflen = 32; /* XXX */
2681 bcopy(sa, &net->nt_mask, sa->sa_len);
2682 makemask(&net->nt_mask, (int)preflen);
2683 opt_flags |= OP_HAVEMASK;
2698 * Parse out the next white space separated field
2701 nextfield(char **cp, char **endcp)
2706 while (*p == ' ' || *p == '\t')
2708 if (*p == '\n' || *p == '\0')
2712 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2719 * Get an exports file line. Skip over blank lines and handle line
2727 int totlen, cont_line;
2730 * Loop around ignoring blank lines and getting all continuation lines.
2735 if ((p = fgetln(exp_file, &len)) == NULL)
2740 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2750 if (linesize < len + totlen + 1) {
2751 linesize = len + totlen + 1;
2752 line = realloc(line, linesize);
2756 memcpy(line + totlen, p, len);
2758 line[totlen] = '\0';
2759 } while (totlen == 0 || cont_line);
2764 * Parse a description of a credential.
2767 parsecred(char *namelist, struct xucred *cr)
2774 gid_t groups[XU_NGROUPS + 1];
2777 cr->cr_version = XUCRED_VERSION;
2779 * Set up the unprivileged user.
2782 cr->cr_groups[0] = -2;
2785 * Get the user's password table entry.
2787 names = strsep(&namelist, " \t\n");
2788 name = strsep(&names, ":");
2789 if (isdigit(*name) || *name == '-')
2790 pw = getpwuid(atoi(name));
2792 pw = getpwnam(name);
2794 * Credentials specified as those of a user.
2796 if (names == NULL) {
2798 syslog(LOG_ERR, "unknown user: %s", name);
2801 cr->cr_uid = pw->pw_uid;
2802 ngroups = XU_NGROUPS + 1;
2803 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2804 syslog(LOG_ERR, "too many groups");
2806 * Compress out duplicate.
2808 cr->cr_ngroups = ngroups - 1;
2809 cr->cr_groups[0] = groups[0];
2810 for (cnt = 2; cnt < ngroups; cnt++)
2811 cr->cr_groups[cnt - 1] = groups[cnt];
2815 * Explicit credential specified as a colon separated list:
2819 cr->cr_uid = pw->pw_uid;
2820 else if (isdigit(*name) || *name == '-')
2821 cr->cr_uid = atoi(name);
2823 syslog(LOG_ERR, "unknown user: %s", name);
2827 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
2828 name = strsep(&names, ":");
2829 if (isdigit(*name) || *name == '-') {
2830 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2832 if ((gr = getgrnam(name)) == NULL) {
2833 syslog(LOG_ERR, "unknown group: %s", name);
2836 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2839 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
2840 syslog(LOG_ERR, "too many groups");
2843 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
2845 * Routines that maintain the remote mounttab
2850 struct mountlist *mlp, **mlpp;
2851 char *host, *dirp, *cp;
2855 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2856 if (errno == ENOENT)
2859 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2864 while (fgets(str, STRSIZ, mlfile) != NULL) {
2866 host = strsep(&cp, " \t\n");
2867 dirp = strsep(&cp, " \t\n");
2868 if (host == NULL || dirp == NULL)
2870 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2871 if (mlp == (struct mountlist *)NULL)
2873 strncpy(mlp->ml_host, host, MNTNAMLEN);
2874 mlp->ml_host[MNTNAMLEN] = '\0';
2875 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2876 mlp->ml_dirp[MNTPATHLEN] = '\0';
2877 mlp->ml_next = (struct mountlist *)NULL;
2879 mlpp = &mlp->ml_next;
2885 del_mlist(char *hostp, char *dirp)
2887 struct mountlist *mlp, **mlpp;
2888 struct mountlist *mlp2;
2895 if (!strcmp(mlp->ml_host, hostp) &&
2896 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2899 *mlpp = mlp = mlp->ml_next;
2900 free((caddr_t)mlp2);
2902 mlpp = &mlp->ml_next;
2907 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2908 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
2913 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2921 add_mlist(char *hostp, char *dirp)
2923 struct mountlist *mlp, **mlpp;
2929 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2931 mlpp = &mlp->ml_next;
2934 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2935 if (mlp == (struct mountlist *)NULL)
2937 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
2938 mlp->ml_host[MNTNAMLEN] = '\0';
2939 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
2940 mlp->ml_dirp[MNTPATHLEN] = '\0';
2941 mlp->ml_next = (struct mountlist *)NULL;
2943 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2944 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
2947 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2952 * Free up a group list.
2955 free_grp(struct grouplist *grp)
2957 if (grp->gr_type == GT_HOST) {
2958 if (grp->gr_ptr.gt_addrinfo != NULL)
2959 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
2960 } else if (grp->gr_type == GT_NET) {
2961 if (grp->gr_ptr.gt_net.nt_name)
2962 free(grp->gr_ptr.gt_net.nt_name);
2969 SYSLOG(int pri, const char *fmt, ...)
2974 vfprintf(stderr, fmt, ap);
2980 * Check options for consistency.
2983 check_options(struct dirlist *dp)
2986 if (v4root_phase == 0 && dp == NULL)
2988 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
2989 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
2992 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2993 syslog(LOG_ERR, "-mask requires -network");
2996 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
2997 syslog(LOG_ERR, "-network requires mask specification");
3000 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3001 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3004 if (v4root_phase > 0 &&
3006 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3007 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3010 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3011 syslog(LOG_ERR, "-alldirs has multiple directories");
3018 * Check an absolute directory path for any symbolic links. Return true
3021 check_dirpath(char *dirp)
3028 while (*cp && ret) {
3031 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3037 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3043 * Make a netmask according to the specified prefix length. The ss_family
3044 * and other non-address fields must be initialised before calling this.
3047 makemask(struct sockaddr_storage *ssp, int bitlen)
3052 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3054 if (bitlen > len * CHAR_BIT)
3057 for (i = 0; i < len; i++) {
3058 bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen;
3059 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3066 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3067 * is acceptable (i.e. of the form 1...10....0).
3070 checkmask(struct sockaddr *sa)
3075 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3078 for (i = 0; i < len; i++)
3079 if (mask[i] != 0xff)
3082 if (~mask[i] & (u_char)(~mask[i] + 1))
3086 for (; i < len; i++)
3093 * Compare two sockaddrs according to a specified mask. Return zero if
3094 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3095 * If samask is NULL, perform a full comparision.
3098 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3100 unsigned char *p1, *p2, *mask;
3103 if (sa1->sa_family != sa2->sa_family ||
3104 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3105 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3108 switch (sa1->sa_family) {
3110 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3111 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3116 /* Simple binary comparison if no mask specified. */
3118 return (memcmp(p1, p2, len));
3120 /* Set up the mask, and do a mask-based comparison. */
3121 if (sa1->sa_family != samask->sa_family ||
3122 (mask = sa_rawaddr(samask, NULL)) == NULL)
3125 for (i = 0; i < len; i++)
3126 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3132 * Return a pointer to the part of the sockaddr that contains the
3133 * raw address, and set *nbytes to its length in bytes. Returns
3134 * NULL if the address family is unknown.
3137 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3141 switch (sa->sa_family) {
3143 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3144 p = &((struct sockaddr_in *)sa)->sin_addr;
3147 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3148 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3161 huphandler(int sig __unused)
3166 void terminate(int sig __unused)
3168 pidfile_remove(pfh);
3169 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3170 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);