2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Herb Hasler and Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
52 #include <sys/fcntl.h>
53 #include <sys/fnv_hash.h>
54 #include <sys/linker.h>
55 #include <sys/module.h>
56 #include <sys/mount.h>
57 #include <sys/queue.h>
59 #include <sys/sysctl.h>
60 #include <sys/syslog.h>
63 #include <rpc/rpc_com.h>
64 #include <rpc/pmap_clnt.h>
65 #include <rpc/pmap_prot.h>
66 #include <rpcsvc/mount.h>
67 #include <nfs/nfsproto.h>
68 #include <nfs/nfssvc.h>
69 #include <nfsserver/nfs.h>
71 #include <fs/nfs/nfsport.h>
73 #include <arpa/inet.h>
88 #include "pathnames.h"
96 * Structures for keeping the mount list and export list
99 char ml_host[MNTNAMLEN+1];
100 char ml_dirp[MNTPATHLEN+1];
102 SLIST_ENTRY(mountlist) next;
106 struct dirlist *dp_left;
107 struct dirlist *dp_right;
109 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
113 #define DP_DEFSET 0x1
114 #define DP_HOSTSET 0x2
117 * maproot/mapall credentials.
118 * cr_smallgrps can be used for a group list up to SMALLNGROUPS in size.
119 * Larger group lists are malloc'd/free'd.
121 #define SMALLNGROUPS 32
125 gid_t cr_smallgrps[SMALLNGROUPS];
130 struct dirlist *ex_dirl;
131 struct dirlist *ex_defdir;
132 struct grouplist *ex_grphead;
137 struct expcred ex_defanon;
138 uint64_t ex_defexflags;
139 int ex_numsecflavors;
140 int ex_secflavors[MAXSECFLAVORS];
141 int ex_defnumsecflavors;
142 int ex_defsecflavors[MAXSECFLAVORS];
144 SLIST_ENTRY(exportlist) entries;
147 #define EX_LINKED 0x1
149 #define EX_DEFSET 0x4
150 #define EX_PUBLICFH 0x8
152 SLIST_HEAD(exportlisthead, exportlist);
155 struct sockaddr_storage nt_net;
156 struct sockaddr_storage nt_mask;
161 struct addrinfo *gt_addrinfo;
162 struct netmsk gt_net;
167 union grouptypes gr_ptr;
168 struct grouplist *gr_next;
169 struct expcred gr_anon;
172 int gr_numsecflavors;
173 int gr_secflavors[MAXSECFLAVORS];
179 #define GT_DEFAULT 0x3
180 #define GT_IGNORE 0x5
186 int ht_flag; /* Uses DP_xx bits */
187 struct grouplist *ht_grp;
188 struct hostlist *ht_next;
195 int fhr_numsecflavors;
199 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
202 * How long to delay a reload of exports when there are RPC request(s)
203 * to process, in usec. Must be less than 1second.
205 #define RELOADDELAY 250000
208 static char *add_expdir(struct dirlist **, char *, int);
209 static void add_dlist(struct dirlist **, struct dirlist *,
210 struct grouplist *, int, struct exportlist *,
211 struct expcred *, uint64_t);
212 static void add_mlist(char *, char *);
213 static int check_dirpath(char *);
214 static int check_options(struct dirlist *);
215 static int checkmask(struct sockaddr *sa);
216 static int chk_host(struct dirlist *, struct sockaddr *, int *, int *,
218 static char *strsep_quote(char **stringp, const char *delim);
219 static int create_service(struct netconfig *nconf);
220 static void complete_service(struct netconfig *nconf, char *port_str);
221 static void clearout_service(void);
222 static void del_mlist(char *hostp, char *dirp);
223 static struct dirlist *dirp_search(struct dirlist *, char *);
224 static int do_export_mount(struct exportlist *, struct statfs *);
225 static int do_mount(struct exportlist *, struct grouplist *, uint64_t,
226 struct expcred *, char *, int, struct statfs *, int, int *);
227 static int do_opt(char **, char **, struct exportlist *,
228 struct grouplist *, int *, uint64_t *, struct expcred *);
229 static struct exportlist *ex_search(fsid_t *, struct exportlisthead *);
230 static struct exportlist *get_exp(void);
231 static void free_dir(struct dirlist *);
232 static void free_exp(struct exportlist *);
233 static void free_grp(struct grouplist *);
234 static void free_host(struct hostlist *);
235 static void free_v4rootexp(void);
236 static void get_exportlist_one(int);
237 static void get_exportlist(int);
238 static void insert_exports(struct exportlist *, struct exportlisthead *);
239 static void free_exports(struct exportlisthead *);
240 static void read_exportfile(int);
241 static int compare_nmount_exportlist(struct iovec *, int, char *);
242 static int compare_export(struct exportlist *, struct exportlist *);
243 static int compare_cred(struct expcred *, struct expcred *);
244 static int compare_secflavor(int *, int *, int);
245 static void delete_export(struct iovec *, int, struct statfs *, char *);
246 static int get_host(char *, struct grouplist *, struct grouplist *);
247 static struct hostlist *get_ht(void);
248 static int get_line(void);
249 static void get_mountlist(void);
250 static int get_net(char *, struct netmsk *, int);
251 static void getexp_err(struct exportlist *, struct grouplist *, const char *);
252 static struct grouplist *get_grp(void);
253 static void hang_dirp(struct dirlist *, struct grouplist *,
254 struct exportlist *, int, struct expcred *, uint64_t);
255 static void huphandler(int sig);
256 static int makemask(struct sockaddr_storage *ssp, int bitlen);
257 static void mntsrv(struct svc_req *, SVCXPRT *);
258 static void nextfield(char **, char **);
259 static void out_of_mem(void);
260 static void parsecred(char *, struct expcred *);
261 static int parsesec(char *, struct exportlist *);
262 static int put_exlist(struct dirlist *, XDR *, struct dirlist *,
264 static void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
265 static int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
266 struct sockaddr *samask);
267 static int scan_tree(struct dirlist *, struct sockaddr *);
268 static void usage(void);
269 static int xdr_dir(XDR *, char *);
270 static int xdr_explist(XDR *, caddr_t);
271 static int xdr_explist_brief(XDR *, caddr_t);
272 static int xdr_explist_common(XDR *, caddr_t, int);
273 static int xdr_fhs(XDR *, caddr_t);
274 static int xdr_mlist(XDR *, caddr_t);
275 static void terminate(int);
276 static void cp_cred(struct expcred *, struct expcred *);
278 #define EXPHASH(f) (fnv_32_buf((f), sizeof(fsid_t), 0) % exphashsize)
279 static struct exportlisthead *exphead = NULL;
280 static struct exportlisthead *oldexphead = NULL;
281 static int exphashsize = 0;
282 static SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(&mlhead);
283 static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
284 static char **exnames;
285 static char **hosts = NULL;
286 static int force_v2 = 0;
287 static int resvport_only = 1;
288 static int nhosts = 0;
289 static int dir_only = 1;
290 static int dolog = 0;
291 static int got_sighup = 0;
292 static int xcreated = 0;
294 static char *svcport_str = NULL;
295 static int mallocd_svcport = 0;
297 static int sock_fdcnt;
298 static int sock_fdpos;
299 static int suspend_nfsd = 0;
301 static int opt_flags;
302 static int have_v6 = 1;
304 static int v4root_phase = 0;
305 static char v4root_dirpath[PATH_MAX + 1];
306 static struct exportlist *v4root_ep = NULL;
307 static int has_publicfh = 0;
308 static int has_set_publicfh = 0;
310 static struct pidfh *pfh = NULL;
311 /* Bits for opt_flags above */
312 #define OP_MAPROOT 0x01
313 #define OP_MAPALL 0x02
317 #define OP_ALLDIRS 0x40
318 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
319 #define OP_QUIET 0x100
320 #define OP_MASKLEN 0x200
324 static int debug = 1;
325 static void SYSLOG(int, const char *, ...) __printflike(2, 3);
326 #define syslog SYSLOG
328 static int debug = 0;
332 * The LOGDEBUG() syslog() calls are always compiled into the daemon.
333 * To enable them, create a file at _PATH_MOUNTDDEBUG. This file can be empty.
334 * To disable the logging, just delete the file at _PATH_MOUNTDDEBUG.
336 static int logdebug = 0;
337 #define LOGDEBUG(format, ...) \
338 (logdebug ? syslog(LOG_DEBUG, format, ## __VA_ARGS__) : 0)
341 * Similar to strsep(), but it allows for quoted strings
342 * and escaped characters.
344 * It returns the string (or NULL, if *stringp is NULL),
345 * which is a de-quoted version of the string if necessary.
347 * It modifies *stringp in place.
350 strsep_quote(char **stringp, const char *delim)
352 char *srcptr, *dstptr, *retval;
355 if (stringp == NULL || *stringp == NULL)
358 srcptr = dstptr = retval = *stringp;
362 * We're looking for several edge cases here.
363 * First: if we're in quote state (quot != 0),
364 * then we ignore the delim characters, but otherwise
365 * process as normal, unless it is the quote character.
366 * Second: if the current character is a backslash,
367 * we take the next character as-is, without checking
368 * for delim, quote, or backslash. Exception: if the
369 * next character is a NUL, that's the end of the string.
370 * Third: if the character is a quote character, we toggle
372 * Otherwise: check the current character for NUL, or
373 * being in delim, and end the string if either is true.
375 if (*srcptr == '\\') {
378 * The edge case here is if the next character
379 * is NUL, we want to stop processing. But if
380 * it's not NUL, then we simply want to copy it.
383 *dstptr++ = *srcptr++;
387 if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
391 if (quot && *srcptr == quot) {
392 /* End of the quoted part */
397 if (!quot && strchr(delim, *srcptr))
399 *dstptr++ = *srcptr++;
402 *stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
403 *dstptr = 0; /* Terminate the string */
408 * Mountd server for NFS mount protocol as described in:
409 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
410 * The optional arguments are the exports file name
411 * default: _PATH_EXPORTS
412 * and "-n" to allow nonroot mount.
415 main(int argc, char **argv)
418 struct netconfig *nconf;
419 char *endptr, **hosts_bak;
424 int maxrec = RPC_MAXDATASIZE;
425 int attempt_cnt, port_len, port_pos, ret;
427 uint64_t curtime, nexttime;
430 sigset_t sig_mask, sighup_mask;
434 /* Check that another mountd isn't already running. */
435 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
438 errx(1, "mountd already running, pid: %d.", otherpid);
439 warn("cannot open or create pidfile");
442 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
448 while ((c = getopt(argc, argv, "2deh:lnp:RrS")) != -1)
454 /* now a no-op, since this is the default */
460 /* Do not support Mount protocol */
467 debug = debug ? 0 : 1;
474 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
475 if (endptr == NULL || *endptr != '\0' ||
476 svcport == 0 || svcport >= IPPORT_MAX)
478 svcport_str = strdup(optarg);
483 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
484 if (hosts_bak == NULL) {
486 for (k = 0; k < nhosts; k++)
493 hosts[nhosts - 1] = strdup(optarg);
494 if (hosts[nhosts - 1] == NULL) {
495 for (k = 0; k < (nhosts - 1); k++)
507 if (enable_rpcbind == 0) {
508 if (svcport_str != NULL) {
509 warnx("-p option not compatible with -R, ignored");
514 warnx("-h option not compatible with -R, ignored");
515 for (k = 0; k < nhosts; k++)
523 if (modfind("nfsd") < 0) {
524 /* Not present in kernel, try loading it */
525 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
526 errx(1, "NFS server is not available");
534 exnames = exnames_default;
535 openlog("mountd", LOG_PID, LOG_DAEMON);
537 warnx("getting export list");
540 warnx("getting mount list");
546 signal(SIGINT, SIG_IGN);
547 signal(SIGQUIT, SIG_IGN);
549 signal(SIGHUP, huphandler);
550 signal(SIGTERM, terminate);
551 signal(SIGPIPE, SIG_IGN);
555 if (enable_rpcbind != 0) {
556 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
557 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
558 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
560 if (!resvport_only) {
561 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
562 &resvport_only, sizeof(resvport_only)) != 0 &&
564 syslog(LOG_ERR, "sysctl: %m");
570 * If no hosts were specified, add a wildcard entry to bind to
571 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added
575 hosts = malloc(sizeof(char *));
583 hosts_bak = realloc(hosts, (nhosts + 2) *
585 if (hosts_bak == NULL) {
586 for (k = 0; k < nhosts; k++)
593 hosts[nhosts - 2] = "::1";
595 hosts_bak = realloc(hosts, (nhosts + 1) *
597 if (hosts_bak == NULL) {
598 for (k = 0; k < nhosts; k++)
608 hosts[nhosts - 1] = "127.0.0.1";
617 if (enable_rpcbind != 0) {
618 nc_handle = setnetconfig();
619 while ((nconf = getnetconfig(nc_handle))) {
620 if (nconf->nc_flag & NC_VISIBLE) {
621 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
625 ret = create_service(nconf);
627 /* Ignore this call */
631 * Failed to bind port, so close
632 * off all sockets created and
633 * try again if the port# was
634 * dynamically assigned via
638 if (mallocd_svcport != 0 &&
653 * Start over at the first
659 nc_handle = setnetconfig();
661 } else if (mallocd_svcport != 0 &&
662 attempt_cnt == GETPORT_MAXTRY) {
664 * For the last attempt, allow
665 * different port #s for each
666 * nconf by saving the
667 * svcport_str setting it back
670 port_list = realloc(port_list,
673 if (port_list == NULL)
675 port_list[port_len++] =
685 * Successfully bound the ports, so call complete_service() to
686 * do the rest of the setup on the service(s).
690 nc_handle = setnetconfig();
691 while ((nconf = getnetconfig(nc_handle))) {
692 if (nconf->nc_flag & NC_VISIBLE) {
693 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
696 } else if (port_list != NULL) {
697 if (port_pos >= port_len) {
698 syslog(LOG_ERR, "too many"
702 complete_service(nconf,
703 port_list[port_pos++]);
705 complete_service(nconf, svcport_str);
708 endnetconfig(nc_handle);
710 if (port_list != NULL) {
711 for (port_pos = 0; port_pos < port_len; port_pos++)
712 free(port_list[port_pos]);
717 syslog(LOG_ERR, "could not create any services");
722 /* Expand svc_run() here so that we can call get_exportlist(). */
723 curtime = nexttime = 0;
724 sigemptyset(&sighup_mask);
725 sigaddset(&sighup_mask, SIGHUP);
727 clock_gettime(CLOCK_MONOTONIC, &tp);
729 curtime = curtime * 1000000 + tp.tv_nsec / 1000;
730 sigprocmask(SIG_BLOCK, &sighup_mask, &sig_mask);
731 if (got_sighup && curtime >= nexttime) {
733 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
735 clock_gettime(CLOCK_MONOTONIC, &tp);
736 nexttime = tp.tv_sec;
737 nexttime = nexttime * 1000000 + tp.tv_nsec / 1000 +
740 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
743 * If a reload is pending, poll for received request(s),
744 * otherwise set a RELOADDELAY timeout, since a SIGHUP
745 * could be processed between the got_sighup test and
746 * the select() system call.
752 tv.tv_usec = RELOADDELAY;
753 if (enable_rpcbind != 0) {
755 switch (select(svc_maxfd + 1, &readfds, NULL, NULL,
758 if (errno == EINTR) {
759 /* Allow a reload now. */
763 syslog(LOG_ERR, "mountd died: select: %m");
766 /* Allow a reload now. */
770 svc_getreqset(&readfds);
773 /* Simply wait for a signal. */
774 sigsuspend(&sig_mask);
780 * This routine creates and binds sockets on the appropriate
781 * addresses. It gets called one time for each transport.
782 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
783 * bind failed with EADDRINUSE.
784 * Any file descriptors that have been created are stored in sock_fd and
785 * the total count of them is maintained in sock_fdcnt.
788 create_service(struct netconfig *nconf)
790 struct addrinfo hints, *res = NULL;
791 struct sockaddr_in *sin;
792 struct sockaddr_in6 *sin6;
793 struct __rpc_sockinfo si;
799 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
802 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
803 (nconf->nc_semantics != NC_TPI_COTS) &&
804 (nconf->nc_semantics != NC_TPI_COTS_ORD))
805 return (1); /* not my type */
808 * XXX - using RPC library internal functions.
810 if (!__rpc_nconf2sockinfo(nconf, &si)) {
811 syslog(LOG_ERR, "cannot get information for %s",
816 /* Get mountd's address on this transport */
817 memset(&hints, 0, sizeof hints);
818 hints.ai_family = si.si_af;
819 hints.ai_socktype = si.si_socktype;
820 hints.ai_protocol = si.si_proto;
823 * Bind to specific IPs if asked to
826 while (nhostsbak > 0) {
828 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
831 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
834 hints.ai_flags = AI_PASSIVE;
837 * XXX - using RPC library internal functions.
839 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
841 if (errno == EAFNOSUPPORT &&
842 nconf->nc_semantics != NC_TPI_CLTS)
845 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
846 "cannot create socket for %s", nconf->nc_netid);
852 switch (hints.ai_family) {
854 if (inet_pton(AF_INET, hosts[nhostsbak],
856 hints.ai_flags |= AI_NUMERICHOST;
859 * Skip if we have an AF_INET6 address.
861 if (inet_pton(AF_INET6, hosts[nhostsbak],
869 if (inet_pton(AF_INET6, hosts[nhostsbak],
871 hints.ai_flags |= AI_NUMERICHOST;
874 * Skip if we have an AF_INET address.
876 if (inet_pton(AF_INET, hosts[nhostsbak],
884 * We're doing host-based access checks here, so don't
885 * allow v4-in-v6 to confuse things. The kernel will
886 * disable it by default on NFS sockets too.
888 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
891 "can't disable v4-in-v6 on IPv6 socket");
900 * If no hosts were specified, just bind to INADDR_ANY
902 if (strcmp("*", hosts[nhostsbak]) == 0) {
903 if (svcport_str == NULL) {
904 res = malloc(sizeof(struct addrinfo));
908 res->ai_flags = hints.ai_flags;
909 res->ai_family = hints.ai_family;
910 res->ai_protocol = hints.ai_protocol;
911 switch (res->ai_family) {
913 sin = malloc(sizeof(struct sockaddr_in));
916 sin->sin_family = AF_INET;
917 sin->sin_port = htons(0);
918 sin->sin_addr.s_addr = htonl(INADDR_ANY);
919 res->ai_addr = (struct sockaddr*) sin;
920 res->ai_addrlen = (socklen_t)
921 sizeof(struct sockaddr_in);
924 sin6 = malloc(sizeof(struct sockaddr_in6));
927 sin6->sin6_family = AF_INET6;
928 sin6->sin6_port = htons(0);
929 sin6->sin6_addr = in6addr_any;
930 res->ai_addr = (struct sockaddr*) sin6;
931 res->ai_addrlen = (socklen_t)
932 sizeof(struct sockaddr_in6);
935 syslog(LOG_ERR, "bad addr fam %d",
940 if ((aicode = getaddrinfo(NULL, svcport_str,
941 &hints, &res)) != 0) {
943 "cannot get local address for %s: %s",
945 gai_strerror(aicode));
951 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
952 &hints, &res)) != 0) {
954 "cannot get local address for %s: %s",
955 nconf->nc_netid, gai_strerror(aicode));
962 sock_fd[sock_fdcnt - 1] = fd;
964 /* Now, attempt the bind. */
965 r = bindresvport_sa(fd, res->ai_addr);
967 if (errno == EADDRINUSE && mallocd_svcport != 0) {
968 if (mallocd_res != 0) {
975 syslog(LOG_ERR, "bindresvport_sa: %m");
979 if (svcport_str == NULL) {
980 svcport_str = malloc(NI_MAXSERV * sizeof(char));
981 if (svcport_str == NULL)
985 if (getnameinfo(res->ai_addr,
986 res->ai_addr->sa_len, NULL, NI_MAXHOST,
987 svcport_str, NI_MAXSERV * sizeof(char),
988 NI_NUMERICHOST | NI_NUMERICSERV))
989 errx(1, "Cannot get port number");
991 if (mallocd_res != 0) {
1002 * Called after all the create_service() calls have succeeded, to complete
1003 * the setup and registration.
1006 complete_service(struct netconfig *nconf, char *port_str)
1008 struct addrinfo hints, *res = NULL;
1009 struct __rpc_sockinfo si;
1010 struct netbuf servaddr;
1011 SVCXPRT *transp = NULL;
1012 int aicode, fd, nhostsbak;
1015 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
1016 (nconf->nc_semantics != NC_TPI_COTS) &&
1017 (nconf->nc_semantics != NC_TPI_COTS_ORD))
1018 return; /* not my type */
1021 * XXX - using RPC library internal functions.
1023 if (!__rpc_nconf2sockinfo(nconf, &si)) {
1024 syslog(LOG_ERR, "cannot get information for %s",
1030 while (nhostsbak > 0) {
1032 if (sock_fdpos >= sock_fdcnt) {
1033 /* Should never happen. */
1034 syslog(LOG_ERR, "Ran out of socket fd's");
1037 fd = sock_fd[sock_fdpos++];
1042 * Using -1 tells listen(2) to use
1043 * kern.ipc.soacceptqueue for the backlog.
1045 if (nconf->nc_semantics != NC_TPI_CLTS)
1048 if (nconf->nc_semantics == NC_TPI_CLTS )
1049 transp = svc_dg_create(fd, 0, 0);
1051 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
1054 if (transp != (SVCXPRT *) NULL) {
1055 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
1058 "can't register %s MOUNTVERS service",
1061 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
1064 "can't register %s MOUNTVERS3 service",
1068 syslog(LOG_WARNING, "can't create %s services",
1071 if (registered == 0) {
1073 memset(&hints, 0, sizeof hints);
1074 hints.ai_flags = AI_PASSIVE;
1075 hints.ai_family = si.si_af;
1076 hints.ai_socktype = si.si_socktype;
1077 hints.ai_protocol = si.si_proto;
1079 if ((aicode = getaddrinfo(NULL, port_str, &hints,
1081 syslog(LOG_ERR, "cannot get local address: %s",
1082 gai_strerror(aicode));
1086 servaddr.buf = malloc(res->ai_addrlen);
1087 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
1088 servaddr.len = res->ai_addrlen;
1090 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
1091 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
1100 * Clear out sockets after a failure to bind one of them, so that the
1101 * cycle of socket creation/binding can start anew.
1104 clearout_service(void)
1108 for (i = 0; i < sock_fdcnt; i++) {
1109 if (sock_fd[i] >= 0) {
1110 shutdown(sock_fd[i], SHUT_RDWR);
1120 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
1121 "[-S] [-h <bindip>] [export_file ...]\n");
1126 * The mount rpc service
1129 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
1131 struct exportlist *ep;
1133 struct fhreturn fhr;
1136 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1137 int lookup_failed = 1;
1138 struct sockaddr *saddr;
1140 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1141 int defset, hostset;
1143 sigset_t sighup_mask;
1144 int numsecflavors, *secflavorsp;
1146 sigemptyset(&sighup_mask);
1147 sigaddset(&sighup_mask, SIGHUP);
1148 saddr = svc_getrpccaller(transp)->buf;
1149 switch (saddr->sa_family) {
1151 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1154 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1157 syslog(LOG_ERR, "request from unknown address family");
1160 switch (rqstp->rq_proc) {
1162 case MOUNTPROC_UMNT:
1163 case MOUNTPROC_UMNTALL:
1164 lookup_failed = getnameinfo(saddr, saddr->sa_len, host,
1165 sizeof host, NULL, 0, 0);
1167 getnameinfo(saddr, saddr->sa_len, numerichost,
1168 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1169 switch (rqstp->rq_proc) {
1171 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1172 syslog(LOG_ERR, "can't send reply");
1175 if (sport >= IPPORT_RESERVED && resvport_only) {
1177 "mount request from %s from unprivileged port",
1179 svcerr_weakauth(transp);
1182 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1183 syslog(LOG_NOTICE, "undecodable mount request from %s",
1185 svcerr_decode(transp);
1190 * Get the real pathname and make sure it is a directory
1191 * or a regular file if the -r option was specified
1194 if (realpath(rpcpath, dirpath) == NULL ||
1195 stat(dirpath, &stb) < 0 ||
1196 statfs(dirpath, &fsb) < 0) {
1197 chdir("/"); /* Just in case realpath doesn't */
1199 "mount request from %s for non existent path %s",
1200 numerichost, dirpath);
1202 warnx("stat failed on %s", dirpath);
1203 bad = ENOENT; /* We will send error reply later */
1206 !S_ISDIR(stb.st_mode) &&
1207 (dir_only || !S_ISREG(stb.st_mode))) {
1209 "mount request from %s for non-directory path %s",
1210 numerichost, dirpath);
1212 warnx("mounting non-directory %s", dirpath);
1213 bad = ENOTDIR; /* We will send error reply later */
1216 /* Check in the exports list */
1217 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1221 ep = ex_search(&fsb.f_fsid, exphead);
1222 hostset = defset = 0;
1223 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1224 &numsecflavors, &secflavorsp) ||
1225 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1226 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1228 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1229 scan_tree(ep->ex_dirl, saddr) == 0))) {
1231 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1233 syslog(LOG_ERR, "can't send reply");
1234 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1237 if (hostset & DP_HOSTSET) {
1238 fhr.fhr_flag = hostset;
1239 fhr.fhr_numsecflavors = numsecflavors;
1240 fhr.fhr_secflavors = secflavorsp;
1242 fhr.fhr_flag = defset;
1243 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1244 fhr.fhr_secflavors = ep->ex_defsecflavors;
1246 fhr.fhr_vers = rqstp->rq_vers;
1247 /* Get the file handle */
1248 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1249 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1251 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1252 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1254 syslog(LOG_ERR, "can't send reply");
1255 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1258 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1260 syslog(LOG_ERR, "can't send reply");
1262 add_mlist(host, dirpath);
1264 add_mlist(numerichost, dirpath);
1266 warnx("mount successful");
1269 "mount request succeeded from %s for %s",
1270 numerichost, dirpath);
1275 "mount request denied from %s for %s",
1276 numerichost, dirpath);
1279 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1281 syslog(LOG_ERR, "can't send reply");
1282 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1284 case MOUNTPROC_DUMP:
1285 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1286 syslog(LOG_ERR, "can't send reply");
1289 "dump request succeeded from %s",
1292 case MOUNTPROC_UMNT:
1293 if (sport >= IPPORT_RESERVED && resvport_only) {
1295 "umount request from %s from unprivileged port",
1297 svcerr_weakauth(transp);
1300 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1301 syslog(LOG_NOTICE, "undecodable umount request from %s",
1303 svcerr_decode(transp);
1306 if (realpath(rpcpath, dirpath) == NULL) {
1307 syslog(LOG_NOTICE, "umount request from %s "
1308 "for non existent path %s",
1309 numerichost, dirpath);
1311 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1312 syslog(LOG_ERR, "can't send reply");
1314 del_mlist(host, dirpath);
1315 del_mlist(numerichost, dirpath);
1318 "umount request succeeded from %s for %s",
1319 numerichost, dirpath);
1321 case MOUNTPROC_UMNTALL:
1322 if (sport >= IPPORT_RESERVED && resvport_only) {
1324 "umountall request from %s from unprivileged port",
1326 svcerr_weakauth(transp);
1329 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1330 syslog(LOG_ERR, "can't send reply");
1332 del_mlist(host, NULL);
1333 del_mlist(numerichost, NULL);
1336 "umountall request succeeded from %s",
1339 case MOUNTPROC_EXPORT:
1340 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1341 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1343 syslog(LOG_ERR, "can't send reply");
1346 "export request succeeded from %s",
1350 svcerr_noproc(transp);
1356 * Xdr conversion for a dirpath string
1359 xdr_dir(XDR *xdrsp, char *dirp)
1361 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1365 * Xdr routine to generate file handle reply
1368 xdr_fhs(XDR *xdrsp, caddr_t cp)
1370 struct fhreturn *fhrp = (struct fhreturn *)cp;
1371 u_long ok = 0, len, auth;
1374 if (!xdr_long(xdrsp, &ok))
1376 switch (fhrp->fhr_vers) {
1378 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1381 if (!xdr_long(xdrsp, &len))
1383 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1385 if (fhrp->fhr_numsecflavors) {
1386 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1388 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1389 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1395 if (!xdr_long(xdrsp, &len))
1397 return (xdr_long(xdrsp, &auth));
1404 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1406 struct mountlist *mlp;
1411 SLIST_FOREACH(mlp, &mlhead, next) {
1412 if (!xdr_bool(xdrsp, &true))
1414 strp = &mlp->ml_host[0];
1415 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1417 strp = &mlp->ml_dirp[0];
1418 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1421 if (!xdr_bool(xdrsp, &false))
1427 * Xdr conversion for export list
1430 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1432 struct exportlist *ep;
1435 sigset_t sighup_mask;
1438 sigemptyset(&sighup_mask);
1439 sigaddset(&sighup_mask, SIGHUP);
1440 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1442 for (i = 0; i < exphashsize; i++)
1443 SLIST_FOREACH(ep, &exphead[i], entries) {
1445 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1448 if (ep->ex_defdir && putdef == 0 &&
1449 put_exlist(ep->ex_defdir, xdrsp, NULL,
1453 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1454 if (!xdr_bool(xdrsp, &false))
1458 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1463 * Called from xdr_explist() to traverse the tree and export the
1467 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1470 struct grouplist *grp;
1471 struct hostlist *hp;
1478 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1480 if (!xdr_bool(xdrsp, &true))
1483 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1485 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1490 if (!xdr_bool(xdrsp, &true))
1493 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1495 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1496 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1500 if (grp->gr_type == GT_HOST) {
1501 if (!xdr_bool(xdrsp, &true))
1503 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1504 if (!xdr_string(xdrsp, &strp,
1507 } else if (grp->gr_type == GT_NET) {
1508 if (!xdr_bool(xdrsp, &true))
1510 strp = grp->gr_ptr.gt_net.nt_name;
1511 if (!xdr_string(xdrsp, &strp,
1516 if (gotalldir && hp == (struct hostlist *)NULL) {
1522 if (!xdr_bool(xdrsp, &false))
1524 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1531 xdr_explist(XDR *xdrsp, caddr_t cp)
1534 return xdr_explist_common(xdrsp, cp, 0);
1538 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1541 return xdr_explist_common(xdrsp, cp, 1);
1545 static size_t linesize;
1546 static FILE *exp_file;
1549 * Get the export list from one, currently open file
1552 get_exportlist_one(int passno)
1554 struct exportlist *ep;
1555 struct grouplist *grp, *tgrp, *savgrp;
1556 struct dirlist *dirhead;
1558 struct expcred anon;
1559 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1560 int len, has_host, got_nondir, dirplen, netgrp;
1564 anon.cr_groups = NULL;
1565 dirhead = (struct dirlist *)NULL;
1566 while (get_line()) {
1568 warnx("got line %s", line);
1570 nextfield(&cp, &endcp);
1578 anon.cr_groups = anon.cr_smallgrps;
1579 anon.cr_uid = UID_NOBODY;
1580 anon.cr_ngroups = 1;
1581 anon.cr_groups[0] = GID_NOGROUP;
1582 exflags = MNT_EXPORTED;
1585 ep = (struct exportlist *)NULL;
1589 * Handle the V4 root dir.
1591 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1593 * V4: just indicates that it is the v4 root point,
1594 * so skip over that and set v4root_phase.
1596 if (v4root_phase > 0) {
1597 syslog(LOG_ERR, "V4:duplicate line, ignored");
1602 nextfield(&cp, &endcp);
1606 * Create new exports list entry
1609 tgrp = grp = get_grp();
1611 if (len > MNTNAMLEN) {
1612 getexp_err(ep, tgrp, "mountpoint too long");
1616 if (ep == (struct exportlist *)NULL) {
1617 getexp_err(ep, tgrp,
1618 "flag before export path definition");
1622 warnx("doing opt %s", cp);
1624 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1626 getexp_err(ep, tgrp, NULL);
1629 } else if (*cp == '/') {
1632 if (v4root_phase > 1) {
1634 getexp_err(ep, tgrp, "Multiple V4 dirs");
1638 if (check_dirpath(cp) &&
1639 statfs(cp, &fsb) >= 0) {
1640 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1641 syslog(LOG_ERR, "Warning: exporting of "
1642 "automounted fs %s not supported", cp);
1644 getexp_err(ep, tgrp, "dirs must be first");
1647 if (v4root_phase == 1) {
1649 getexp_err(ep, tgrp, "Multiple V4 dirs");
1652 if (strlen(v4root_dirpath) == 0) {
1653 strlcpy(v4root_dirpath, cp,
1654 sizeof (v4root_dirpath));
1655 } else if (strcmp(v4root_dirpath, cp)
1658 "different V4 dirpath %s", cp);
1659 getexp_err(ep, tgrp, NULL);
1668 if (fsidcmp(&ep->ex_fs, &fsb.f_fsid)
1670 getexp_err(ep, tgrp,
1676 * See if this directory is already
1679 ep = ex_search(&fsb.f_fsid, exphead);
1680 if (ep == (struct exportlist *)NULL) {
1682 ep->ex_fs = fsb.f_fsid;
1683 ep->ex_fsdir = strdup(fsb.f_mntonname);
1684 if (ep->ex_fsdir == NULL)
1688 "making new ep fs=0x%x,0x%x",
1692 warnx("found ep fs=0x%x,0x%x",
1698 * Add dirpath to export mount point.
1700 dirp = add_expdir(&dirhead, cp, len);
1704 getexp_err(ep, tgrp,
1705 "symbolic link in export path or statfs failed");
1713 if (ep == (struct exportlist *)NULL) {
1714 getexp_err(ep, tgrp,
1715 "host(s) before export path definition");
1720 * Get the host or netgroup.
1723 netgrp = getnetgrent(&hst, &usr, &dom);
1726 grp->gr_next = get_grp();
1732 "null hostname in netgroup %s, skipping", cp);
1733 grp->gr_type = GT_IGNORE;
1734 } else if (get_host(hst, grp, tgrp)) {
1736 "bad host %s in netgroup %s, skipping", hst, cp);
1737 grp->gr_type = GT_IGNORE;
1739 } else if (get_host(cp, grp, tgrp)) {
1740 syslog(LOG_ERR, "bad host %s, skipping", cp);
1741 grp->gr_type = GT_IGNORE;
1744 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1749 nextfield(&cp, &endcp);
1752 if (check_options(dirhead)) {
1753 getexp_err(ep, tgrp, NULL);
1757 grp->gr_type = GT_DEFAULT;
1759 warnx("adding a default entry");
1762 * Don't allow a network export coincide with a list of
1763 * host(s) on the same line.
1765 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1766 getexp_err(ep, tgrp, "network/host conflict");
1770 * If an export list was specified on this line, make sure
1771 * that we have at least one valid entry, otherwise skip it.
1775 while (grp && grp->gr_type == GT_IGNORE)
1778 getexp_err(ep, tgrp, "no valid entries");
1783 if (v4root_phase == 1) {
1784 getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1789 * Loop through hosts, pushing the exports into the kernel.
1790 * After loop, tgrp points to the start of the list and
1791 * grp points to the last entry in the list.
1792 * Do not do the do_mount() for passno == 1, since the
1793 * second pass will do it, as required.
1797 grp->gr_exflags = exflags;
1798 cp_cred(&grp->gr_anon, &anon);
1799 if (v4root_phase == 2 && passno == 0)
1800 LOGDEBUG("do_mount v4root");
1801 if (passno == 0 && do_mount(ep, grp, exflags, &anon,
1802 dirp, dirplen, &fsb, ep->ex_numsecflavors,
1803 ep->ex_secflavors)) {
1804 getexp_err(ep, tgrp, NULL);
1807 } while (grp->gr_next && (grp = grp->gr_next));
1810 * For V4: don't enter in mount lists.
1812 if (v4root_phase > 0 && v4root_phase <= 2) {
1814 * These structures are used for the reload,
1815 * so save them for that case. Otherwise, just
1818 if (passno == 1 && ep != NULL) {
1820 while (tgrp != NULL) {
1822 * Save the security flavors and exflags
1823 * for this host set in the groups.
1825 tgrp->gr_numsecflavors =
1826 ep->ex_numsecflavors;
1827 if (ep->ex_numsecflavors > 0)
1828 memcpy(tgrp->gr_secflavors,
1830 sizeof(ep->ex_secflavors));
1831 tgrp = tgrp->gr_next;
1833 if (v4root_ep == NULL) {
1835 ep = NULL; /* Don't free below. */
1837 grp->gr_next = v4root_ep->ex_grphead;
1838 v4root_ep->ex_grphead = savgrp;
1842 while (tgrp != NULL) {
1844 tgrp = tgrp->gr_next;
1851 * Success. Update the data structures.
1854 hang_dirp(dirhead, tgrp, ep, opt_flags, &anon, exflags);
1855 grp->gr_next = ep->ex_grphead;
1856 ep->ex_grphead = tgrp;
1858 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1859 opt_flags, &anon, exflags);
1862 dirhead = (struct dirlist *)NULL;
1863 if ((ep->ex_flag & EX_LINKED) == 0) {
1864 insert_exports(ep, exphead);
1866 ep->ex_flag |= EX_LINKED;
1872 dirhead = (struct dirlist *)NULL;
1874 if (anon.cr_groups != anon.cr_smallgrps) {
1875 free(anon.cr_groups);
1876 anon.cr_groups = NULL;
1882 * Get the export list from all specified files
1885 get_exportlist(int passno)
1887 struct export_args export;
1889 struct statfs *mntbufp;
1893 struct nfsex_args eargs;
1896 if ((debug_file = fopen(_PATH_MOUNTDDEBUG, "r")) != NULL) {
1901 LOGDEBUG("passno=%d", passno);
1902 v4root_dirpath[0] = '\0';
1906 * Save the current lists as old ones, so that the new lists
1907 * can be compared with the old ones in the 2nd pass.
1909 for (i = 0; i < exphashsize; i++) {
1910 SLIST_FIRST(&oldexphead[i]) = SLIST_FIRST(&exphead[i]);
1911 SLIST_INIT(&exphead[i]);
1914 /* Note that the public fh has not yet been set. */
1915 has_set_publicfh = 0;
1917 /* Read the export file(s) and process them */
1918 read_exportfile(passno);
1921 * Just make the old lists empty.
1922 * exphashsize == 0 for the first call, before oldexphead
1923 * has been initialized-->loop won't be executed.
1925 for (i = 0; i < exphashsize; i++)
1926 SLIST_INIT(&oldexphead[i]);
1929 bzero(&export, sizeof(export));
1930 export.ex_flags = MNT_DELEXPORT;
1933 bzero(errmsg, sizeof(errmsg));
1935 if (suspend_nfsd != 0)
1936 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1938 * Delete the old V4 root dir.
1940 bzero(&eargs, sizeof (eargs));
1941 eargs.export.ex_flags = MNT_DELEXPORT;
1942 if (nfssvc(NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT, (caddr_t)&eargs) < 0 &&
1944 syslog(LOG_ERR, "Can't delete exports for V4:");
1946 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1947 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1948 build_iovec(&iov, &iovlen, "from", NULL, 0);
1949 build_iovec(&iov, &iovlen, "update", NULL, 0);
1950 build_iovec(&iov, &iovlen, "export", &export,
1952 build_iovec(&iov, &iovlen, "errmsg", errmsg,
1956 * For passno == 1, compare the old and new lists updating the kernel
1957 * exports for any cases that have changed.
1958 * This call is doing the second pass through the lists.
1959 * If it fails, fall back on the bulk reload.
1961 if (passno == 1 && compare_nmount_exportlist(iov, iovlen, errmsg) ==
1963 LOGDEBUG("compareok");
1964 /* Free up the old lists. */
1965 free_exports(oldexphead);
1967 LOGDEBUG("doing passno=0");
1969 * Clear flag that notes if a public fh has been exported.
1970 * It is set by do_mount() if MNT_EXPUBLIC is set for the entry.
1974 /* exphead == NULL if not yet allocated (first call). */
1975 if (exphead != NULL) {
1977 * First, get rid of the old lists.
1979 free_exports(exphead);
1980 free_exports(oldexphead);
1984 * And delete exports that are in the kernel for all local
1986 * XXX: Should know how to handle all local exportable
1989 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1991 /* Allocate hash tables, for first call. */
1992 if (exphead == NULL) {
1993 /* Target an average linked list length of 10. */
1994 exphashsize = num / 10;
1995 if (exphashsize < 1)
1997 else if (exphashsize > 100000)
1998 exphashsize = 100000;
1999 exphead = malloc(exphashsize * sizeof(*exphead));
2000 oldexphead = malloc(exphashsize * sizeof(*oldexphead));
2001 if (exphead == NULL || oldexphead == NULL)
2002 errx(1, "Can't malloc hash tables");
2004 for (i = 0; i < exphashsize; i++) {
2005 SLIST_INIT(&exphead[i]);
2006 SLIST_INIT(&oldexphead[i]);
2010 for (i = 0; i < num; i++)
2011 delete_export(iov, iovlen, &mntbufp[i], errmsg);
2014 /* Read the export file(s) and process them */
2019 /* Free strings allocated by strdup() in getmntopts.c */
2020 free(iov[0].iov_base); /* fstype */
2021 free(iov[2].iov_base); /* fspath */
2022 free(iov[4].iov_base); /* from */
2023 free(iov[6].iov_base); /* update */
2024 free(iov[8].iov_base); /* export */
2025 free(iov[10].iov_base); /* errmsg */
2027 /* free iov, allocated by realloc() */
2033 * If there was no public fh, clear any previous one set.
2035 if (has_publicfh == 0) {
2036 LOGDEBUG("clear public fh");
2037 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
2040 /* Resume the nfsd. If they weren't suspended, this is harmless. */
2041 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
2042 LOGDEBUG("eo get_exportlist");
2046 * Insert an export entry in the appropriate list.
2049 insert_exports(struct exportlist *ep, struct exportlisthead *exhp)
2053 i = EXPHASH(&ep->ex_fs);
2054 LOGDEBUG("fs=%s hash=%i", ep->ex_fsdir, i);
2055 SLIST_INSERT_HEAD(&exhp[i], ep, entries);
2059 * Free up the exports lists passed in as arguments.
2062 free_exports(struct exportlisthead *exhp)
2064 struct exportlist *ep, *ep2;
2067 for (i = 0; i < exphashsize; i++) {
2068 SLIST_FOREACH_SAFE(ep, &exhp[i], entries, ep2) {
2069 SLIST_REMOVE(&exhp[i], ep, exportlist, entries);
2072 SLIST_INIT(&exhp[i]);
2077 * Read the exports file(s) and call get_exportlist_one() for each line.
2080 read_exportfile(int passno)
2085 * Read in the exports file and build the list, calling
2086 * nmount() as we go along to push the export rules into the kernel.
2089 for (i = 0; exnames[i] != NULL; i++) {
2091 warnx("reading exports from %s", exnames[i]);
2092 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
2093 syslog(LOG_WARNING, "can't open %s", exnames[i]);
2096 get_exportlist_one(passno);
2101 syslog(LOG_ERR, "can't open any exports file");
2107 * Compare the export lists against the old ones and do nmount() operations
2108 * for any cases that have changed. This avoids doing nmount() for entries
2109 * that have not changed.
2110 * Return 0 upon success, 1 otherwise.
2113 compare_nmount_exportlist(struct iovec *iov, int iovlen, char *errmsg)
2115 struct exportlist *ep, *oep;
2116 struct grouplist *grp;
2117 struct statfs fs, ofs;
2121 * Loop through the current list and look for an entry in the old
2123 * If found, check to see if it the same.
2124 * If it is not the same, delete and re-export.
2125 * Then mark it done on the old list.
2128 * Any entries left in the old list after processing must have their
2131 for (i = 0; i < exphashsize; i++)
2132 SLIST_FOREACH(ep, &exphead[i], entries) {
2133 LOGDEBUG("foreach ep=%s", ep->ex_fsdir);
2134 oep = ex_search(&ep->ex_fs, oldexphead);
2137 * Check the mount paths are the same.
2138 * If not, return 1 so that the reload of the
2139 * exports will be done in bulk, the
2142 LOGDEBUG("found old exp");
2143 if (strcmp(ep->ex_fsdir, oep->ex_fsdir) != 0)
2145 LOGDEBUG("same fsdir");
2147 * Test to see if the entry is the same.
2148 * If not the same delete exports and
2151 if (compare_export(ep, oep) != 0) {
2153 * Clear has_publicfh if if was set
2154 * in the old exports, but only if it
2155 * has not been set during processing of
2156 * the exports for this pass, as
2157 * indicated by has_set_publicfh.
2159 if (has_set_publicfh == 0 &&
2160 (oep->ex_flag & EX_PUBLICFH) != 0)
2163 /* Delete and re-export. */
2164 if (statfs(ep->ex_fsdir, &fs) < 0)
2166 delete_export(iov, iovlen, &fs, errmsg);
2167 ret = do_export_mount(ep, &fs);
2171 oep->ex_flag |= EX_DONE;
2174 LOGDEBUG("not found so export");
2175 /* Not found, so do export. */
2176 if (statfs(ep->ex_fsdir, &fs) < 0)
2178 ret = do_export_mount(ep, &fs);
2184 /* Delete exports not done. */
2185 for (i = 0; i < exphashsize; i++)
2186 SLIST_FOREACH(oep, &oldexphead[i], entries) {
2187 if ((oep->ex_flag & EX_DONE) == 0) {
2188 LOGDEBUG("not done delete=%s", oep->ex_fsdir);
2189 if (statfs(oep->ex_fsdir, &ofs) >= 0 &&
2190 fsidcmp(&oep->ex_fs, &ofs.f_fsid) == 0) {
2191 LOGDEBUG("do delete");
2193 * Clear has_publicfh if if was set
2194 * in the old exports, but only if it
2195 * has not been set during processing of
2196 * the exports for this pass, as
2197 * indicated by has_set_publicfh.
2199 if (has_set_publicfh == 0 &&
2200 (oep->ex_flag & EX_PUBLICFH) != 0)
2203 delete_export(iov, iovlen, &ofs,
2209 /* Do the V4 root exports, as required. */
2211 if (v4root_ep != NULL)
2212 grp = v4root_ep->ex_grphead;
2214 while (v4root_ep != NULL && grp != NULL) {
2215 LOGDEBUG("v4root expath=%s", v4root_dirpath);
2216 ret = do_mount(v4root_ep, grp, grp->gr_exflags, &grp->gr_anon,
2217 v4root_dirpath, strlen(v4root_dirpath), &fs,
2218 grp->gr_numsecflavors, grp->gr_secflavors);
2231 * Compare old and current exportlist entries for the fsid and return 0
2232 * if they are the same, 1 otherwise.
2235 compare_export(struct exportlist *ep, struct exportlist *oep)
2237 struct grouplist *grp, *ogrp;
2239 if (strcmp(ep->ex_fsdir, oep->ex_fsdir) != 0)
2241 if ((ep->ex_flag & EX_DEFSET) != (oep->ex_flag & EX_DEFSET))
2243 if ((ep->ex_defdir != NULL && oep->ex_defdir == NULL) ||
2244 (ep->ex_defdir == NULL && oep->ex_defdir != NULL))
2246 if (ep->ex_defdir != NULL && (ep->ex_defdir->dp_flag & DP_DEFSET) !=
2247 (oep->ex_defdir->dp_flag & DP_DEFSET))
2249 if ((ep->ex_flag & EX_DEFSET) != 0 && (ep->ex_defnumsecflavors !=
2250 oep->ex_defnumsecflavors || ep->ex_defexflags !=
2251 oep->ex_defexflags || compare_cred(&ep->ex_defanon,
2252 &oep->ex_defanon) != 0 || compare_secflavor(ep->ex_defsecflavors,
2253 oep->ex_defsecflavors, ep->ex_defnumsecflavors) != 0))
2256 /* Now, check all the groups. */
2257 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp = ogrp->gr_next)
2259 for (grp = ep->ex_grphead; grp != NULL; grp = grp->gr_next) {
2260 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp =
2262 if ((ogrp->gr_flag & GR_FND) == 0 &&
2263 grp->gr_numsecflavors == ogrp->gr_numsecflavors &&
2264 grp->gr_exflags == ogrp->gr_exflags &&
2265 compare_cred(&grp->gr_anon, &ogrp->gr_anon) == 0 &&
2266 compare_secflavor(grp->gr_secflavors,
2267 ogrp->gr_secflavors, grp->gr_numsecflavors) == 0)
2270 ogrp->gr_flag |= GR_FND;
2274 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp = ogrp->gr_next)
2275 if ((ogrp->gr_flag & GR_FND) == 0)
2281 * This algorithm compares two arrays of "n" items. It returns 0 if they are
2282 * the "same" and 1 otherwise. Although suboptimal, it is always safe to
2283 * return 1, which makes compare_nmount_export() reload the exports entry.
2284 * "same" refers to having the same set of values in the two arrays.
2285 * The arrays are in no particular order and duplicates (multiple entries
2286 * in an array with the same value) is allowed.
2287 * The algorithm is inefficient, but the common case of indentical arrays is
2288 * handled first and "n" is normally fairly small.
2289 * Since the two functions need the same algorithm but for arrays of
2290 * different types (gid_t vs int), this is done as a macro.
2292 #define COMPARE_ARRAYS(a1, a2, n) \
2294 int fnd, fndarray[(n)], i, j; \
2295 /* Handle common case of identical arrays. */ \
2296 for (i = 0; i < (n); i++) \
2297 if ((a1)[i] != (a2)[i]) \
2301 for (i = 0; i < (n); i++) \
2303 for (i = 0; i < (n); i++) { \
2305 for (j = 0; j < (n); j++) { \
2306 if ((a1)[i] == (a2)[j]) { \
2314 for (i = 0; i < (n); i++) \
2315 if (fndarray[i] == 0) \
2321 * Compare two struct expcred's. Return 0 if the same and 1 otherwise.
2324 compare_cred(struct expcred *cr0, struct expcred *cr1)
2327 if (cr0->cr_uid != cr1->cr_uid || cr0->cr_ngroups != cr1->cr_ngroups)
2330 COMPARE_ARRAYS(cr0->cr_groups, cr1->cr_groups, cr0->cr_ngroups);
2334 * Compare two lists of security flavors. Return 0 if the same and 1 otherwise.
2337 compare_secflavor(int *sec1, int *sec2, int nsec)
2340 COMPARE_ARRAYS(sec1, sec2, nsec);
2344 * Delete an exports entry.
2347 delete_export(struct iovec *iov, int iovlen, struct statfs *fsp, char *errmsg)
2349 struct xvfsconf vfc;
2351 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
2352 syslog(LOG_ERR, "getvfsbyname() failed for %s",
2358 * We do not need to delete "export" flag from
2359 * filesystems that do not have it set.
2361 if (!(fsp->f_flags & MNT_EXPORTED))
2364 * Do not delete export for network filesystem by
2365 * passing "export" arg to nmount().
2366 * It only makes sense to do this for local filesystems.
2368 if (vfc.vfc_flags & VFCF_NETWORK)
2371 iov[1].iov_base = fsp->f_fstypename;
2372 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
2373 iov[3].iov_base = fsp->f_mntonname;
2374 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
2375 iov[5].iov_base = fsp->f_mntfromname;
2376 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
2380 * EXDEV is returned when path exists but is not a
2381 * mount point. May happens if raced with unmount.
2383 if (nmount(iov, iovlen, fsp->f_flags) < 0 && errno != ENOENT &&
2384 errno != ENOTSUP && errno != EXDEV) {
2386 "can't delete exports for %s: %m %s",
2387 fsp->f_mntonname, errmsg);
2392 * Allocate an export list element
2394 static struct exportlist *
2397 struct exportlist *ep;
2399 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
2400 if (ep == (struct exportlist *)NULL)
2406 * Allocate a group list element
2408 static struct grouplist *
2411 struct grouplist *gp;
2413 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
2414 if (gp == (struct grouplist *)NULL)
2420 * Clean up upon an error in get_exportlist().
2423 getexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
2425 struct grouplist *tgrp;
2427 if (!(opt_flags & OP_QUIET)) {
2429 syslog(LOG_ERR, "bad exports list line '%s': %s", line,
2432 syslog(LOG_ERR, "bad exports list line '%s'", line);
2434 if (ep && (ep->ex_flag & EX_LINKED) == 0)
2444 * Search the export list for a matching fs.
2446 static struct exportlist *
2447 ex_search(fsid_t *fsid, struct exportlisthead *exhp)
2449 struct exportlist *ep;
2453 SLIST_FOREACH(ep, &exhp[i], entries) {
2454 if (fsidcmp(&ep->ex_fs, fsid) == 0)
2462 * Add a directory path to the list.
2465 add_expdir(struct dirlist **dpp, char *cp, int len)
2469 dp = malloc(sizeof (struct dirlist));
2470 if (dp == (struct dirlist *)NULL)
2473 dp->dp_right = (struct dirlist *)NULL;
2475 dp->dp_hosts = (struct hostlist *)NULL;
2476 dp->dp_dirp = strndup(cp, len);
2477 if (dp->dp_dirp == NULL)
2480 return (dp->dp_dirp);
2484 * Hang the dir list element off the dirpath binary tree as required
2485 * and update the entry for host.
2488 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
2489 int flags, struct expcred *anoncrp, uint64_t exflags)
2491 struct hostlist *hp;
2492 struct dirlist *dp2;
2494 if (flags & OP_ALLDIRS) {
2499 if (grp == (struct grouplist *)NULL) {
2500 ep->ex_flag |= EX_DEFSET;
2501 ep->ex_defdir->dp_flag |= DP_DEFSET;
2502 /* Save the default security flavors list. */
2503 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2504 if (ep->ex_numsecflavors > 0)
2505 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2506 sizeof(ep->ex_secflavors));
2507 cp_cred(&ep->ex_defanon, anoncrp);
2508 ep->ex_defexflags = exflags;
2509 } else while (grp) {
2512 hp->ht_next = ep->ex_defdir->dp_hosts;
2513 ep->ex_defdir->dp_hosts = hp;
2514 /* Save the security flavors list for this host set. */
2515 grp->gr_numsecflavors = ep->ex_numsecflavors;
2516 if (ep->ex_numsecflavors > 0)
2517 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2518 sizeof(ep->ex_secflavors));
2524 * Loop through the directories adding them to the tree.
2528 add_dlist(&ep->ex_dirl, dp, grp, flags, ep, anoncrp,
2536 * Traverse the binary tree either updating a node that is already there
2537 * for the new directory or adding the new node.
2540 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2541 int flags, struct exportlist *ep, struct expcred *anoncrp,
2545 struct hostlist *hp;
2550 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2552 add_dlist(&dp->dp_left, newdp, grp, flags, ep, anoncrp,
2555 } else if (cmp < 0) {
2556 add_dlist(&dp->dp_right, newdp, grp, flags, ep, anoncrp,
2560 free((caddr_t)newdp);
2563 dp->dp_left = (struct dirlist *)NULL;
2569 * Hang all of the host(s) off of the directory point.
2574 hp->ht_next = dp->dp_hosts;
2576 /* Save the security flavors list for this host set. */
2577 grp->gr_numsecflavors = ep->ex_numsecflavors;
2578 if (ep->ex_numsecflavors > 0)
2579 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2580 sizeof(ep->ex_secflavors));
2584 ep->ex_flag |= EX_DEFSET;
2585 dp->dp_flag |= DP_DEFSET;
2586 /* Save the default security flavors list. */
2587 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2588 if (ep->ex_numsecflavors > 0)
2589 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2590 sizeof(ep->ex_secflavors));
2591 cp_cred(&ep->ex_defanon, anoncrp);
2592 ep->ex_defexflags = exflags;
2597 * Search for a dirpath on the export point.
2599 static struct dirlist *
2600 dirp_search(struct dirlist *dp, char *dirp)
2605 cmp = strcmp(dp->dp_dirp, dirp);
2607 return (dirp_search(dp->dp_left, dirp));
2609 return (dirp_search(dp->dp_right, dirp));
2617 * Scan for a host match in a directory tree.
2620 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2621 int *hostsetp, int *numsecflavors, int **secflavorsp)
2623 struct hostlist *hp;
2624 struct grouplist *grp;
2625 struct addrinfo *ai;
2628 if (dp->dp_flag & DP_DEFSET)
2629 *defsetp = dp->dp_flag;
2633 switch (grp->gr_type) {
2635 ai = grp->gr_ptr.gt_addrinfo;
2636 for (; ai; ai = ai->ai_next) {
2637 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2639 (hp->ht_flag | DP_HOSTSET);
2640 if (numsecflavors != NULL) {
2642 grp->gr_numsecflavors;
2651 if (!sacmp(saddr, (struct sockaddr *)
2652 &grp->gr_ptr.gt_net.nt_net,
2654 &grp->gr_ptr.gt_net.nt_mask)) {
2655 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2656 if (numsecflavors != NULL) {
2658 grp->gr_numsecflavors;
2673 * Scan tree for a host that matches the address.
2676 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2678 int defset, hostset;
2681 if (scan_tree(dp->dp_left, saddr))
2683 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2685 if (scan_tree(dp->dp_right, saddr))
2692 * Traverse the dirlist tree and free it up.
2695 free_dir(struct dirlist *dp)
2699 free_dir(dp->dp_left);
2700 free_dir(dp->dp_right);
2701 free_host(dp->dp_hosts);
2708 * Parse a colon separated list of security flavors
2711 parsesec(char *seclist, struct exportlist *ep)
2716 ep->ex_numsecflavors = 0;
2718 cp = strchr(seclist, ':');
2724 if (!strcmp(seclist, "sys"))
2726 else if (!strcmp(seclist, "krb5"))
2727 flavor = RPCSEC_GSS_KRB5;
2728 else if (!strcmp(seclist, "krb5i"))
2729 flavor = RPCSEC_GSS_KRB5I;
2730 else if (!strcmp(seclist, "krb5p"))
2731 flavor = RPCSEC_GSS_KRB5P;
2735 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2738 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2741 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2744 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2745 ep->ex_numsecflavors++;
2757 * Parse the option string and update fields.
2758 * Option arguments may either be -<option>=<value> or
2762 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2763 int *has_hostp, uint64_t *exflagsp, struct expcred *cr)
2765 char *cpoptarg, *cpoptend;
2766 char *cp, *endcp, *cpopt, savedc, savedc2;
2767 int allflag, usedarg;
2775 while (cpopt && *cpopt) {
2778 if ((cpoptend = strchr(cpopt, ','))) {
2780 if ((cpoptarg = strchr(cpopt, '=')))
2783 if ((cpoptarg = strchr(cpopt, '=')))
2787 nextfield(&cp, &endcp);
2789 if (endcp > cp && *cp != '-') {
2797 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2798 *exflagsp |= MNT_EXRDONLY;
2799 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2800 !(allflag = strcmp(cpopt, "mapall")) ||
2801 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2803 parsecred(cpoptarg, cr);
2805 *exflagsp |= MNT_EXPORTANON;
2806 opt_flags |= OP_MAPALL;
2808 opt_flags |= OP_MAPROOT;
2809 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2810 !strcmp(cpopt, "m"))) {
2811 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2812 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2816 opt_flags |= OP_MASK;
2817 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2818 !strcmp(cpopt, "n"))) {
2819 if (strchr(cpoptarg, '/') != NULL) {
2821 fprintf(stderr, "setting OP_MASKLEN\n");
2822 opt_flags |= OP_MASKLEN;
2824 if (grp->gr_type != GT_NULL) {
2825 syslog(LOG_ERR, "network/host conflict");
2827 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2828 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2831 grp->gr_type = GT_NET;
2834 opt_flags |= OP_NET;
2835 } else if (!strcmp(cpopt, "alldirs")) {
2836 opt_flags |= OP_ALLDIRS;
2837 } else if (!strcmp(cpopt, "public")) {
2838 *exflagsp |= MNT_EXPUBLIC;
2839 } else if (!strcmp(cpopt, "webnfs")) {
2840 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2841 opt_flags |= OP_MAPALL;
2842 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2843 ep->ex_indexfile = strdup(cpoptarg);
2844 } else if (!strcmp(cpopt, "quiet")) {
2845 opt_flags |= OP_QUIET;
2846 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2847 if (parsesec(cpoptarg, ep))
2849 opt_flags |= OP_SEC;
2851 } else if (!strcmp(cpopt, "tls")) {
2852 *exflagsp |= MNT_EXTLS;
2853 } else if (!strcmp(cpopt, "tlscert")) {
2854 *exflagsp |= (MNT_EXTLS | MNT_EXTLSCERT);
2855 } else if (!strcmp(cpopt, "tlscertuser")) {
2856 *exflagsp |= (MNT_EXTLS | MNT_EXTLSCERT |
2859 syslog(LOG_ERR, "bad opt %s", cpopt);
2878 * Translate a character string to the corresponding list of network
2879 * addresses for a hostname.
2882 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2884 struct grouplist *checkgrp;
2885 struct addrinfo *ai, *tai, hints;
2887 char host[NI_MAXHOST];
2889 if (grp->gr_type != GT_NULL) {
2890 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2893 memset(&hints, 0, sizeof hints);
2894 hints.ai_flags = AI_CANONNAME;
2895 hints.ai_protocol = IPPROTO_UDP;
2896 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2898 syslog(LOG_ERR,"can't get address info for host %s", cp);
2901 grp->gr_ptr.gt_addrinfo = ai;
2902 while (ai != NULL) {
2903 if (ai->ai_canonname == NULL) {
2904 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2905 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2906 strlcpy(host, "?", sizeof(host));
2907 ai->ai_canonname = strdup(host);
2908 ai->ai_flags |= AI_CANONNAME;
2911 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2913 * Sanity check: make sure we don't already have an entry
2914 * for this host in the grouplist.
2916 for (checkgrp = tgrp; checkgrp != NULL;
2917 checkgrp = checkgrp->gr_next) {
2918 if (checkgrp->gr_type != GT_HOST)
2920 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2921 tai = tai->ai_next) {
2922 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2926 "ignoring duplicate host %s\n",
2928 grp->gr_type = GT_IGNORE;
2934 grp->gr_type = GT_HOST;
2939 * Free up an exports list component
2942 free_exp(struct exportlist *ep)
2944 struct grouplist *grp, *tgrp;
2946 if (ep->ex_defdir) {
2947 free_host(ep->ex_defdir->dp_hosts);
2948 free((caddr_t)ep->ex_defdir);
2952 if (ep->ex_indexfile)
2953 free(ep->ex_indexfile);
2954 free_dir(ep->ex_dirl);
2955 grp = ep->ex_grphead;
2961 if (ep->ex_defanon.cr_groups != ep->ex_defanon.cr_smallgrps)
2962 free(ep->ex_defanon.cr_groups);
2967 * Free up the v4root exports.
2970 free_v4rootexp(void)
2973 if (v4root_ep != NULL) {
2974 free_exp(v4root_ep);
2983 free_host(struct hostlist *hp)
2985 struct hostlist *hp2;
2994 static struct hostlist *
2997 struct hostlist *hp;
2999 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
3000 if (hp == (struct hostlist *)NULL)
3002 hp->ht_next = (struct hostlist *)NULL;
3008 * Out of memory, fatal
3014 syslog(LOG_ERR, "out of memory");
3019 * Call do_mount() from the struct exportlist, for each case needed.
3022 do_export_mount(struct exportlist *ep, struct statfs *fsp)
3024 struct grouplist *grp, defgrp;
3028 LOGDEBUG("do_export_mount=%s", ep->ex_fsdir);
3029 dirlen = strlen(ep->ex_fsdir);
3030 if ((ep->ex_flag & EX_DEFSET) != 0) {
3031 defgrp.gr_type = GT_DEFAULT;
3032 defgrp.gr_next = NULL;
3033 /* We have an entry for all other hosts/nets. */
3034 LOGDEBUG("ex_defexflags=0x%jx", (uintmax_t)ep->ex_defexflags);
3035 ret = do_mount(ep, &defgrp, ep->ex_defexflags, &ep->ex_defanon,
3036 ep->ex_fsdir, dirlen, fsp, ep->ex_defnumsecflavors,
3037 ep->ex_defsecflavors);
3042 /* Do a mount for each group. */
3043 grp = ep->ex_grphead;
3044 while (grp != NULL) {
3045 LOGDEBUG("do mount gr_type=0x%x gr_exflags=0x%jx",
3046 grp->gr_type, (uintmax_t)grp->gr_exflags);
3047 ret = do_mount(ep, grp, grp->gr_exflags, &grp->gr_anon,
3048 ep->ex_fsdir, dirlen, fsp, grp->gr_numsecflavors,
3049 grp->gr_secflavors);
3058 * Do the nmount() syscall with the update flag to push the export info into
3062 do_mount(struct exportlist *ep, struct grouplist *grp, uint64_t exflags,
3063 struct expcred *anoncrp, char *dirp, int dirplen, struct statfs *fsb,
3064 int numsecflavors, int *secflavors)
3067 struct addrinfo *ai;
3068 struct export_args *eap;
3076 struct nfsex_args nfsea;
3078 eap = &nfsea.export;
3086 bzero(eap, sizeof (struct export_args));
3087 bzero(errmsg, sizeof(errmsg));
3088 eap->ex_flags = exflags;
3089 eap->ex_uid = anoncrp->cr_uid;
3090 eap->ex_ngroups = anoncrp->cr_ngroups;
3091 if (eap->ex_ngroups > 0) {
3092 eap->ex_groups = malloc(eap->ex_ngroups * sizeof(gid_t));
3093 memcpy(eap->ex_groups, anoncrp->cr_groups, eap->ex_ngroups *
3096 LOGDEBUG("do_mount exflags=0x%jx", (uintmax_t)exflags);
3097 eap->ex_indexfile = ep->ex_indexfile;
3098 if (grp->gr_type == GT_HOST)
3099 ai = grp->gr_ptr.gt_addrinfo;
3102 eap->ex_numsecflavors = numsecflavors;
3103 LOGDEBUG("do_mount numsec=%d", numsecflavors);
3104 for (i = 0; i < eap->ex_numsecflavors; i++)
3105 eap->ex_secflavors[i] = secflavors[i];
3106 if (eap->ex_numsecflavors == 0) {
3107 eap->ex_numsecflavors = 1;
3108 eap->ex_secflavors[0] = AUTH_SYS;
3112 if (v4root_phase == 0) {
3113 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
3114 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
3115 build_iovec(&iov, &iovlen, "from", NULL, 0);
3116 build_iovec(&iov, &iovlen, "update", NULL, 0);
3117 build_iovec(&iov, &iovlen, "export", eap,
3118 sizeof (struct export_args));
3119 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
3123 switch (grp->gr_type) {
3125 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
3127 eap->ex_addr = ai->ai_addr;
3128 eap->ex_addrlen = ai->ai_addrlen;
3129 eap->ex_masklen = 0;
3132 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
3136 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
3138 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
3140 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
3141 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
3144 eap->ex_addr = NULL;
3145 eap->ex_addrlen = 0;
3146 eap->ex_mask = NULL;
3147 eap->ex_masklen = 0;
3154 syslog(LOG_ERR, "bad grouptype");
3162 * For V4:, use the nfssvc() syscall, instead of mount().
3164 if (v4root_phase == 2) {
3165 nfsea.fspec = v4root_dirpath;
3166 if (nfssvc(NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT,
3167 (caddr_t)&nfsea) < 0) {
3168 syslog(LOG_ERR, "Exporting V4: failed");
3175 * Maybe I should just use the fsb->f_mntonname path
3176 * instead of looping back up the dirp to the mount
3178 * Also, needs to know how to export all types of local
3179 * exportable filesystems and not just "ufs".
3181 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
3182 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
3183 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
3184 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
3185 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
3186 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
3189 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
3193 cp = dirp + dirplen - 1;
3194 if (opt_flags & OP_QUIET) {
3198 if (errno == EPERM) {
3200 warnx("can't change attributes for %s: %s",
3203 "can't change attributes for %s: %s",
3208 if (opt_flags & OP_ALLDIRS) {
3209 if (errno == EINVAL)
3211 "-alldirs requested but %s is not a filesystem mountpoint",
3215 "could not remount %s: %m",
3220 /* back up over the last component */
3221 while (cp > dirp && *cp == '/')
3223 while (cp > dirp && *(cp - 1) != '/')
3227 warnx("mnt unsucc");
3228 syslog(LOG_ERR, "can't export %s %s",
3236 * Check that we're still on the same
3239 if (statfs(dirp, &fsb1) != 0 ||
3240 fsidcmp(&fsb1.f_fsid, &fsb->f_fsid) != 0) {
3243 "can't export %s %s", dirp,
3252 * For the experimental server:
3253 * If this is the public directory, get the file handle
3254 * and load it into the kernel via the nfssvc() syscall.
3256 if ((exflags & MNT_EXPUBLIC) != 0) {
3260 if (eap->ex_indexfile != NULL)
3261 public_name = eap->ex_indexfile;
3264 if (getfh(public_name, &fh) < 0)
3266 "Can't get public fh for %s", public_name);
3267 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
3269 "Can't set public fh for %s", public_name);
3272 has_set_publicfh = 1;
3273 ep->ex_flag |= EX_PUBLICFH;
3285 free(eap->ex_groups);
3286 /* free strings allocated by strdup() in getmntopts.c */
3288 free(iov[0].iov_base); /* fstype */
3289 free(iov[2].iov_base); /* fspath */
3290 free(iov[4].iov_base); /* from */
3291 free(iov[6].iov_base); /* update */
3292 free(iov[8].iov_base); /* export */
3293 free(iov[10].iov_base); /* errmsg */
3295 /* free iov, allocated by realloc() */
3302 * Translate a net address.
3304 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
3307 get_net(char *cp, struct netmsk *net, int maskflg)
3309 struct netent *np = NULL;
3310 char *name, *p, *prefp;
3311 struct sockaddr_in sin;
3312 struct sockaddr *sa = NULL;
3313 struct addrinfo hints, *ai = NULL;
3314 char netname[NI_MAXHOST];
3318 if ((opt_flags & OP_MASKLEN) && !maskflg) {
3319 p = strchr(cp, '/');
3325 * Check for a numeric address first. We wish to avoid
3326 * possible DNS lookups in getnetbyname().
3328 if (isxdigit(*cp) || *cp == ':') {
3329 memset(&hints, 0, sizeof hints);
3330 /* Ensure the mask and the network have the same family. */
3331 if (maskflg && (opt_flags & OP_NET))
3332 hints.ai_family = net->nt_net.ss_family;
3333 else if (!maskflg && (opt_flags & OP_HAVEMASK))
3334 hints.ai_family = net->nt_mask.ss_family;
3336 hints.ai_family = AF_UNSPEC;
3337 hints.ai_flags = AI_NUMERICHOST;
3338 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
3340 if (sa != NULL && ai->ai_family == AF_INET) {
3342 * The address in `cp' is really a network address, so
3343 * use inet_network() to re-interpret this correctly.
3344 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
3346 bzero(&sin, sizeof sin);
3347 sin.sin_family = AF_INET;
3348 sin.sin_len = sizeof sin;
3349 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
3351 fprintf(stderr, "get_net: v4 addr %s\n",
3352 inet_ntoa(sin.sin_addr));
3353 sa = (struct sockaddr *)&sin;
3356 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
3357 bzero(&sin, sizeof sin);
3358 sin.sin_family = AF_INET;
3359 sin.sin_len = sizeof sin;
3360 sin.sin_addr = inet_makeaddr(np->n_net, 0);
3361 sa = (struct sockaddr *)&sin;
3367 /* The specified sockaddr is a mask. */
3368 if (checkmask(sa) != 0)
3370 bcopy(sa, &net->nt_mask, sa->sa_len);
3371 opt_flags |= OP_HAVEMASK;
3373 /* The specified sockaddr is a network address. */
3374 bcopy(sa, &net->nt_net, sa->sa_len);
3376 /* Get a network name for the export list. */
3379 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
3380 NULL, 0, NI_NUMERICHOST) == 0) {
3385 if ((net->nt_name = strdup(name)) == NULL)
3389 * Extract a mask from either a "/<masklen>" suffix, or
3390 * from the class of an IPv4 address.
3392 if (opt_flags & OP_MASKLEN) {
3393 preflen = strtol(prefp, NULL, 10);
3394 if (preflen < 0L || preflen == LONG_MAX)
3396 bcopy(sa, &net->nt_mask, sa->sa_len);
3397 if (makemask(&net->nt_mask, (int)preflen) != 0)
3399 opt_flags |= OP_HAVEMASK;
3401 } else if (sa->sa_family == AF_INET &&
3402 (opt_flags & OP_MASK) == 0) {
3405 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
3406 if (IN_CLASSA(addr))
3408 else if (IN_CLASSB(addr))
3410 else if (IN_CLASSC(addr))
3412 else if (IN_CLASSD(addr))
3415 preflen = 32; /* XXX */
3417 bcopy(sa, &net->nt_mask, sa->sa_len);
3418 makemask(&net->nt_mask, (int)preflen);
3419 opt_flags |= OP_HAVEMASK;
3434 * Parse out the next white space separated field
3437 nextfield(char **cp, char **endcp)
3443 while (*p == ' ' || *p == '\t')
3446 while (*p != '\0') {
3451 if (*p == '\\' && *(p + 1) != '\0')
3453 else if (*p == '\'' || *p == '"')
3455 else if (*p == ' ' || *p == '\t')
3464 * Get an exports file line. Skip over blank lines and handle line
3472 int totlen, cont_line;
3475 * Loop around ignoring blank lines and getting all continuation lines.
3480 if ((p = fgetln(exp_file, &len)) == NULL)
3485 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
3495 if (linesize < len + totlen + 1) {
3496 linesize = len + totlen + 1;
3497 line = realloc(line, linesize);
3501 memcpy(line + totlen, p, len);
3503 line[totlen] = '\0';
3504 } while (totlen == 0 || cont_line);
3509 * Parse a description of a credential.
3512 parsecred(char *namelist, struct expcred *cr)
3519 gid_t groups[NGROUPS_MAX + 1];
3523 * Set up the unprivileged user.
3525 cr->cr_groups = cr->cr_smallgrps;
3526 cr->cr_uid = UID_NOBODY;
3527 cr->cr_groups[0] = GID_NOGROUP;
3530 * Get the user's password table entry.
3533 name = strsep_quote(&names, ":");
3534 /* Bug? name could be NULL here */
3535 if (isdigit(*name) || *name == '-')
3536 pw = getpwuid(atoi(name));
3538 pw = getpwnam(name);
3540 * Credentials specified as those of a user.
3542 if (names == NULL) {
3544 syslog(LOG_ERR, "unknown user: %s", name);
3547 cr->cr_uid = pw->pw_uid;
3548 ngroups = NGROUPS_MAX + 1;
3549 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) {
3550 syslog(LOG_ERR, "too many groups");
3551 ngroups = NGROUPS_MAX + 1;
3555 * Compress out duplicate.
3557 if (ngroups > 1 && groups[0] == groups[1]) {
3562 if (ngroups > NGROUPS_MAX)
3563 ngroups = NGROUPS_MAX;
3564 if (ngroups > SMALLNGROUPS)
3565 cr->cr_groups = malloc(ngroups * sizeof(gid_t));
3566 cr->cr_ngroups = ngroups;
3567 cr->cr_groups[0] = groups[0];
3568 memcpy(&cr->cr_groups[1], &groups[inpos], (ngroups - 1) *
3573 * Explicit credential specified as a colon separated list:
3577 cr->cr_uid = pw->pw_uid;
3578 else if (isdigit(*name) || *name == '-')
3579 cr->cr_uid = atoi(name);
3581 syslog(LOG_ERR, "unknown user: %s", name);
3585 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS_MAX) {
3586 name = strsep_quote(&names, ":");
3587 if (isdigit(*name) || *name == '-') {
3588 groups[cr->cr_ngroups++] = atoi(name);
3590 if ((gr = getgrnam(name)) == NULL) {
3591 syslog(LOG_ERR, "unknown group: %s", name);
3594 groups[cr->cr_ngroups++] = gr->gr_gid;
3597 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS_MAX)
3598 syslog(LOG_ERR, "too many groups");
3599 if (cr->cr_ngroups > SMALLNGROUPS)
3600 cr->cr_groups = malloc(cr->cr_ngroups * sizeof(gid_t));
3601 memcpy(cr->cr_groups, groups, cr->cr_ngroups * sizeof(gid_t));
3604 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
3606 * Routines that maintain the remote mounttab
3611 struct mountlist *mlp;
3612 char *host, *dirp, *cp;
3616 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
3617 if (errno == ENOENT)
3620 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
3624 while (fgets(str, STRSIZ, mlfile) != NULL) {
3626 host = strsep(&cp, " \t\n");
3627 dirp = strsep(&cp, " \t\n");
3628 if (host == NULL || dirp == NULL)
3630 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3631 if (mlp == (struct mountlist *)NULL)
3633 strncpy(mlp->ml_host, host, MNTNAMLEN);
3634 mlp->ml_host[MNTNAMLEN] = '\0';
3635 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3636 mlp->ml_dirp[MNTPATHLEN] = '\0';
3638 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3644 del_mlist(char *hostp, char *dirp)
3646 struct mountlist *mlp, *mlp2;
3650 SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
3651 if (!strcmp(mlp->ml_host, hostp) &&
3652 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3654 SLIST_REMOVE(&mlhead, mlp, mountlist, next);
3659 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3660 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3663 SLIST_FOREACH(mlp, &mlhead, next) {
3664 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3671 add_mlist(char *hostp, char *dirp)
3673 struct mountlist *mlp;
3676 SLIST_FOREACH(mlp, &mlhead, next) {
3677 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3681 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3682 if (mlp == (struct mountlist *)NULL)
3684 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3685 mlp->ml_host[MNTNAMLEN] = '\0';
3686 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3687 mlp->ml_dirp[MNTPATHLEN] = '\0';
3688 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3689 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3690 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3693 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3698 * Free up a group list.
3701 free_grp(struct grouplist *grp)
3703 if (grp->gr_type == GT_HOST) {
3704 if (grp->gr_ptr.gt_addrinfo != NULL)
3705 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3706 } else if (grp->gr_type == GT_NET) {
3707 if (grp->gr_ptr.gt_net.nt_name)
3708 free(grp->gr_ptr.gt_net.nt_name);
3710 if (grp->gr_anon.cr_groups != grp->gr_anon.cr_smallgrps)
3711 free(grp->gr_anon.cr_groups);
3717 SYSLOG(int pri, const char *fmt, ...)
3722 vfprintf(stderr, fmt, ap);
3728 * Check options for consistency.
3731 check_options(struct dirlist *dp)
3734 if (v4root_phase == 0 && dp == NULL)
3736 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3737 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3740 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3741 syslog(LOG_ERR, "-mask requires -network");
3744 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3745 syslog(LOG_ERR, "-network requires mask specification");
3748 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3749 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3752 if (v4root_phase > 0 &&
3754 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3755 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3758 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3759 syslog(LOG_ERR, "-alldirs has multiple directories");
3766 * Check an absolute directory path for any symbolic links. Return true
3769 check_dirpath(char *dirp)
3776 while (*cp && ret) {
3779 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3785 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3791 * Make a netmask according to the specified prefix length. The ss_family
3792 * and other non-address fields must be initialised before calling this.
3795 makemask(struct sockaddr_storage *ssp, int bitlen)
3800 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3802 if (bitlen > len * CHAR_BIT)
3805 for (i = 0; i < len; i++) {
3806 bits = MIN(CHAR_BIT, bitlen);
3807 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3814 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3815 * is acceptable (i.e. of the form 1...10....0).
3818 checkmask(struct sockaddr *sa)
3823 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3826 for (i = 0; i < len; i++)
3827 if (mask[i] != 0xff)
3830 if (~mask[i] & (u_char)(~mask[i] + 1))
3834 for (; i < len; i++)
3841 * Compare two sockaddrs according to a specified mask. Return zero if
3842 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3843 * If samask is NULL, perform a full comparison.
3846 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3848 unsigned char *p1, *p2, *mask;
3851 if (sa1->sa_family != sa2->sa_family ||
3852 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3853 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3856 switch (sa1->sa_family) {
3858 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3859 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3864 /* Simple binary comparison if no mask specified. */
3866 return (memcmp(p1, p2, len));
3868 /* Set up the mask, and do a mask-based comparison. */
3869 if (sa1->sa_family != samask->sa_family ||
3870 (mask = sa_rawaddr(samask, NULL)) == NULL)
3873 for (i = 0; i < len; i++)
3874 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3880 * Return a pointer to the part of the sockaddr that contains the
3881 * raw address, and set *nbytes to its length in bytes. Returns
3882 * NULL if the address family is unknown.
3885 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3889 switch (sa->sa_family) {
3891 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3892 p = &((struct sockaddr_in *)sa)->sin_addr;
3895 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3896 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3909 huphandler(int sig __unused)
3916 terminate(int sig __unused)
3918 pidfile_remove(pfh);
3919 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3920 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
3925 cp_cred(struct expcred *outcr, struct expcred *incr)
3928 outcr->cr_uid = incr->cr_uid;
3929 outcr->cr_ngroups = incr->cr_ngroups;
3930 if (outcr->cr_ngroups > SMALLNGROUPS)
3931 outcr->cr_groups = malloc(outcr->cr_ngroups * sizeof(gid_t));
3933 outcr->cr_groups = outcr->cr_smallgrps;
3934 memcpy(outcr->cr_groups, incr->cr_groups, incr->cr_ngroups *