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>
51 #include <sys/fcntl.h>
52 #include <sys/fnv_hash.h>
53 #include <sys/linker.h>
54 #include <sys/module.h>
55 #include <sys/mount.h>
56 #include <sys/queue.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
62 #include <rpc/rpc_com.h>
63 #include <rpc/pmap_clnt.h>
64 #include <rpc/pmap_prot.h>
65 #include <rpcsvc/mount.h>
66 #include <nfs/nfsproto.h>
67 #include <nfs/nfssvc.h>
68 #include <nfsserver/nfs.h>
70 #include <fs/nfs/nfsport.h>
72 #include <arpa/inet.h>
87 #include "pathnames.h"
95 * Structures for keeping the mount list and export list
98 char ml_host[MNTNAMLEN+1];
99 char ml_dirp[MNTPATHLEN+1];
101 SLIST_ENTRY(mountlist) next;
105 struct dirlist *dp_left;
106 struct dirlist *dp_right;
108 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
112 #define DP_DEFSET 0x1
113 #define DP_HOSTSET 0x2
116 struct dirlist *ex_dirl;
117 struct dirlist *ex_defdir;
118 struct grouplist *ex_grphead;
123 struct xucred ex_defanon;
125 int ex_numsecflavors;
126 int ex_secflavors[MAXSECFLAVORS];
127 int ex_defnumsecflavors;
128 int ex_defsecflavors[MAXSECFLAVORS];
130 SLIST_ENTRY(exportlist) entries;
133 #define EX_LINKED 0x1
135 #define EX_DEFSET 0x4
136 #define EX_PUBLICFH 0x8
138 SLIST_HEAD(exportlisthead, exportlist);
141 struct sockaddr_storage nt_net;
142 struct sockaddr_storage nt_mask;
147 struct addrinfo *gt_addrinfo;
148 struct netmsk gt_net;
153 union grouptypes gr_ptr;
154 struct grouplist *gr_next;
155 struct xucred gr_anon;
158 int gr_numsecflavors;
159 int gr_secflavors[MAXSECFLAVORS];
165 #define GT_DEFAULT 0x3
166 #define GT_IGNORE 0x5
172 int ht_flag; /* Uses DP_xx bits */
173 struct grouplist *ht_grp;
174 struct hostlist *ht_next;
181 int fhr_numsecflavors;
185 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
188 * How long to delay a reload of exports when there are RPC request(s)
189 * to process, in usec. Must be less than 1second.
191 #define RELOADDELAY 250000
194 static char *add_expdir(struct dirlist **, char *, int);
195 static void add_dlist(struct dirlist **, struct dirlist *,
196 struct grouplist *, int, struct exportlist *,
197 struct xucred *, int);
198 static void add_mlist(char *, char *);
199 static int check_dirpath(char *);
200 static int check_options(struct dirlist *);
201 static int checkmask(struct sockaddr *sa);
202 static int chk_host(struct dirlist *, struct sockaddr *, int *, int *,
204 static char *strsep_quote(char **stringp, const char *delim);
205 static int create_service(struct netconfig *nconf);
206 static void complete_service(struct netconfig *nconf, char *port_str);
207 static void clearout_service(void);
208 static void del_mlist(char *hostp, char *dirp);
209 static struct dirlist *dirp_search(struct dirlist *, char *);
210 static int do_export_mount(struct exportlist *, struct statfs *);
211 static int do_mount(struct exportlist *, struct grouplist *, int,
212 struct xucred *, char *, int, struct statfs *, int, int *);
213 static int do_opt(char **, char **, struct exportlist *,
214 struct grouplist *, int *, int *, struct xucred *);
215 static struct exportlist *ex_search(fsid_t *, struct exportlisthead *);
216 static struct exportlist *get_exp(void);
217 static void free_dir(struct dirlist *);
218 static void free_exp(struct exportlist *);
219 static void free_grp(struct grouplist *);
220 static void free_host(struct hostlist *);
221 static void free_v4rootexp(void);
222 static void get_exportlist_one(int);
223 static void get_exportlist(int);
224 static void insert_exports(struct exportlist *, struct exportlisthead *);
225 static void free_exports(struct exportlisthead *);
226 static void read_exportfile(int);
227 static int compare_nmount_exportlist(struct iovec *, int, char *);
228 static int compare_export(struct exportlist *, struct exportlist *);
229 static int compare_cred(struct xucred *, struct xucred *);
230 static int compare_secflavor(int *, int *, int);
231 static void delete_export(struct iovec *, int, struct statfs *, char *);
232 static int get_host(char *, struct grouplist *, struct grouplist *);
233 static struct hostlist *get_ht(void);
234 static int get_line(void);
235 static void get_mountlist(void);
236 static int get_net(char *, struct netmsk *, int);
237 static void getexp_err(struct exportlist *, struct grouplist *, const char *);
238 static struct grouplist *get_grp(void);
239 static void hang_dirp(struct dirlist *, struct grouplist *,
240 struct exportlist *, int, struct xucred *, int);
241 static void huphandler(int sig);
242 static int makemask(struct sockaddr_storage *ssp, int bitlen);
243 static void mntsrv(struct svc_req *, SVCXPRT *);
244 static void nextfield(char **, char **);
245 static void out_of_mem(void);
246 static void parsecred(char *, struct xucred *);
247 static int parsesec(char *, struct exportlist *);
248 static int put_exlist(struct dirlist *, XDR *, struct dirlist *,
250 static void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
251 static int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
252 struct sockaddr *samask);
253 static int scan_tree(struct dirlist *, struct sockaddr *);
254 static void usage(void);
255 static int xdr_dir(XDR *, char *);
256 static int xdr_explist(XDR *, caddr_t);
257 static int xdr_explist_brief(XDR *, caddr_t);
258 static int xdr_explist_common(XDR *, caddr_t, int);
259 static int xdr_fhs(XDR *, caddr_t);
260 static int xdr_mlist(XDR *, caddr_t);
261 static void terminate(int);
263 #define EXPHASH(f) (fnv_32_buf((f), sizeof(fsid_t), 0) % exphashsize)
264 static struct exportlisthead *exphead = NULL;
265 static struct exportlisthead *oldexphead = NULL;
266 static int exphashsize = 0;
267 static SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(&mlhead);
268 static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
269 static char **exnames;
270 static char **hosts = NULL;
271 static struct xucred def_anon = {
278 static int force_v2 = 0;
279 static int resvport_only = 1;
280 static int nhosts = 0;
281 static int dir_only = 1;
282 static int dolog = 0;
283 static int got_sighup = 0;
284 static int xcreated = 0;
286 static char *svcport_str = NULL;
287 static int mallocd_svcport = 0;
289 static int sock_fdcnt;
290 static int sock_fdpos;
291 static int suspend_nfsd = 0;
293 static int opt_flags;
294 static int have_v6 = 1;
296 static int v4root_phase = 0;
297 static char v4root_dirpath[PATH_MAX + 1];
298 static struct exportlist *v4root_ep = NULL;
299 static int has_publicfh = 0;
300 static int has_set_publicfh = 0;
302 static struct pidfh *pfh = NULL;
303 /* Bits for opt_flags above */
304 #define OP_MAPROOT 0x01
305 #define OP_MAPALL 0x02
309 #define OP_ALLDIRS 0x40
310 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
311 #define OP_QUIET 0x100
312 #define OP_MASKLEN 0x200
316 static int debug = 1;
317 static void SYSLOG(int, const char *, ...) __printflike(2, 3);
318 #define syslog SYSLOG
320 static int debug = 0;
324 * The LOGDEBUG() syslog() calls are always compiled into the daemon.
325 * To enable them, create a file at _PATH_MOUNTDDEBUG. This file can be empty.
326 * To disable the logging, just delete the file at _PATH_MOUNTDDEBUG.
328 static int logdebug = 0;
329 #define LOGDEBUG(format, ...) \
330 (logdebug ? syslog(LOG_DEBUG, format, ## __VA_ARGS__) : 0)
333 * Similar to strsep(), but it allows for quoted strings
334 * and escaped characters.
336 * It returns the string (or NULL, if *stringp is NULL),
337 * which is a de-quoted version of the string if necessary.
339 * It modifies *stringp in place.
342 strsep_quote(char **stringp, const char *delim)
344 char *srcptr, *dstptr, *retval;
347 if (stringp == NULL || *stringp == NULL)
350 srcptr = dstptr = retval = *stringp;
354 * We're looking for several edge cases here.
355 * First: if we're in quote state (quot != 0),
356 * then we ignore the delim characters, but otherwise
357 * process as normal, unless it is the quote character.
358 * Second: if the current character is a backslash,
359 * we take the next character as-is, without checking
360 * for delim, quote, or backslash. Exception: if the
361 * next character is a NUL, that's the end of the string.
362 * Third: if the character is a quote character, we toggle
364 * Otherwise: check the current character for NUL, or
365 * being in delim, and end the string if either is true.
367 if (*srcptr == '\\') {
370 * The edge case here is if the next character
371 * is NUL, we want to stop processing. But if
372 * it's not NUL, then we simply want to copy it.
375 *dstptr++ = *srcptr++;
379 if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
383 if (quot && *srcptr == quot) {
384 /* End of the quoted part */
389 if (!quot && strchr(delim, *srcptr))
391 *dstptr++ = *srcptr++;
394 *stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
395 *dstptr = 0; /* Terminate the string */
400 * Mountd server for NFS mount protocol as described in:
401 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
402 * The optional arguments are the exports file name
403 * default: _PATH_EXPORTS
404 * and "-n" to allow nonroot mount.
407 main(int argc, char **argv)
410 struct netconfig *nconf;
411 char *endptr, **hosts_bak;
416 int maxrec = RPC_MAXDATASIZE;
417 int attempt_cnt, port_len, port_pos, ret;
419 uint64_t curtime, nexttime;
422 sigset_t sig_mask, sighup_mask;
426 /* Check that another mountd isn't already running. */
427 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
430 errx(1, "mountd already running, pid: %d.", otherpid);
431 warn("cannot open or create pidfile");
434 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
440 while ((c = getopt(argc, argv, "2deh:lnp:RrS")) != -1)
446 /* now a no-op, since this is the default */
452 /* Do not support Mount protocol */
459 debug = debug ? 0 : 1;
466 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
467 if (endptr == NULL || *endptr != '\0' ||
468 svcport == 0 || svcport >= IPPORT_MAX)
470 svcport_str = strdup(optarg);
475 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
476 if (hosts_bak == NULL) {
478 for (k = 0; k < nhosts; k++)
485 hosts[nhosts - 1] = strdup(optarg);
486 if (hosts[nhosts - 1] == NULL) {
487 for (k = 0; k < (nhosts - 1); k++)
499 if (enable_rpcbind == 0) {
500 if (svcport_str != NULL) {
501 warnx("-p option not compatible with -R, ignored");
506 warnx("-h option not compatible with -R, ignored");
507 for (k = 0; k < nhosts; k++)
515 if (modfind("nfsd") < 0) {
516 /* Not present in kernel, try loading it */
517 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
518 errx(1, "NFS server is not available");
526 exnames = exnames_default;
527 openlog("mountd", LOG_PID, LOG_DAEMON);
529 warnx("getting export list");
532 warnx("getting mount list");
538 signal(SIGINT, SIG_IGN);
539 signal(SIGQUIT, SIG_IGN);
541 signal(SIGHUP, huphandler);
542 signal(SIGTERM, terminate);
543 signal(SIGPIPE, SIG_IGN);
547 if (enable_rpcbind != 0) {
548 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
549 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
550 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
552 if (!resvport_only) {
553 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
554 &resvport_only, sizeof(resvport_only)) != 0 &&
556 syslog(LOG_ERR, "sysctl: %m");
562 * If no hosts were specified, add a wildcard entry to bind to
563 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added
567 hosts = malloc(sizeof(char *));
575 hosts_bak = realloc(hosts, (nhosts + 2) *
577 if (hosts_bak == NULL) {
578 for (k = 0; k < nhosts; k++)
585 hosts[nhosts - 2] = "::1";
587 hosts_bak = realloc(hosts, (nhosts + 1) *
589 if (hosts_bak == NULL) {
590 for (k = 0; k < nhosts; k++)
600 hosts[nhosts - 1] = "127.0.0.1";
609 if (enable_rpcbind != 0) {
610 nc_handle = setnetconfig();
611 while ((nconf = getnetconfig(nc_handle))) {
612 if (nconf->nc_flag & NC_VISIBLE) {
613 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
617 ret = create_service(nconf);
619 /* Ignore this call */
623 * Failed to bind port, so close
624 * off all sockets created and
625 * try again if the port# was
626 * dynamically assigned via
630 if (mallocd_svcport != 0 &&
645 * Start over at the first
651 nc_handle = setnetconfig();
653 } else if (mallocd_svcport != 0 &&
654 attempt_cnt == GETPORT_MAXTRY) {
656 * For the last attempt, allow
657 * different port #s for each
658 * nconf by saving the
659 * svcport_str setting it back
662 port_list = realloc(port_list,
665 if (port_list == NULL)
667 port_list[port_len++] =
677 * Successfully bound the ports, so call complete_service() to
678 * do the rest of the setup on the service(s).
682 nc_handle = setnetconfig();
683 while ((nconf = getnetconfig(nc_handle))) {
684 if (nconf->nc_flag & NC_VISIBLE) {
685 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
688 } else if (port_list != NULL) {
689 if (port_pos >= port_len) {
690 syslog(LOG_ERR, "too many"
694 complete_service(nconf,
695 port_list[port_pos++]);
697 complete_service(nconf, svcport_str);
700 endnetconfig(nc_handle);
702 if (port_list != NULL) {
703 for (port_pos = 0; port_pos < port_len; port_pos++)
704 free(port_list[port_pos]);
709 syslog(LOG_ERR, "could not create any services");
714 /* Expand svc_run() here so that we can call get_exportlist(). */
715 curtime = nexttime = 0;
716 sigemptyset(&sighup_mask);
717 sigaddset(&sighup_mask, SIGHUP);
719 clock_gettime(CLOCK_MONOTONIC, &tp);
721 curtime = curtime * 1000000 + tp.tv_nsec / 1000;
722 sigprocmask(SIG_BLOCK, &sighup_mask, &sig_mask);
723 if (got_sighup && curtime >= nexttime) {
725 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
727 clock_gettime(CLOCK_MONOTONIC, &tp);
728 nexttime = tp.tv_sec;
729 nexttime = nexttime * 1000000 + tp.tv_nsec / 1000 +
732 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
735 * If a reload is pending, poll for received request(s),
736 * otherwise set a RELOADDELAY timeout, since a SIGHUP
737 * could be processed between the got_sighup test and
738 * the select() system call.
744 tv.tv_usec = RELOADDELAY;
745 if (enable_rpcbind != 0) {
747 switch (select(svc_maxfd + 1, &readfds, NULL, NULL,
750 if (errno == EINTR) {
751 /* Allow a reload now. */
755 syslog(LOG_ERR, "mountd died: select: %m");
758 /* Allow a reload now. */
762 svc_getreqset(&readfds);
765 /* Simply wait for a signal. */
766 sigsuspend(&sig_mask);
772 * This routine creates and binds sockets on the appropriate
773 * addresses. It gets called one time for each transport.
774 * It returns 0 upon success, 1 for ingore the call and -1 to indicate
775 * bind failed with EADDRINUSE.
776 * Any file descriptors that have been created are stored in sock_fd and
777 * the total count of them is maintained in sock_fdcnt.
780 create_service(struct netconfig *nconf)
782 struct addrinfo hints, *res = NULL;
783 struct sockaddr_in *sin;
784 struct sockaddr_in6 *sin6;
785 struct __rpc_sockinfo si;
791 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
794 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
795 (nconf->nc_semantics != NC_TPI_COTS) &&
796 (nconf->nc_semantics != NC_TPI_COTS_ORD))
797 return (1); /* not my type */
800 * XXX - using RPC library internal functions.
802 if (!__rpc_nconf2sockinfo(nconf, &si)) {
803 syslog(LOG_ERR, "cannot get information for %s",
808 /* Get mountd's address on this transport */
809 memset(&hints, 0, sizeof hints);
810 hints.ai_family = si.si_af;
811 hints.ai_socktype = si.si_socktype;
812 hints.ai_protocol = si.si_proto;
815 * Bind to specific IPs if asked to
818 while (nhostsbak > 0) {
820 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
823 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
826 hints.ai_flags = AI_PASSIVE;
829 * XXX - using RPC library internal functions.
831 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
833 if (errno == EAFNOSUPPORT &&
834 nconf->nc_semantics != NC_TPI_CLTS)
837 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
838 "cannot create socket for %s", nconf->nc_netid);
844 switch (hints.ai_family) {
846 if (inet_pton(AF_INET, hosts[nhostsbak],
848 hints.ai_flags |= AI_NUMERICHOST;
851 * Skip if we have an AF_INET6 address.
853 if (inet_pton(AF_INET6, hosts[nhostsbak],
861 if (inet_pton(AF_INET6, hosts[nhostsbak],
863 hints.ai_flags |= AI_NUMERICHOST;
866 * Skip if we have an AF_INET address.
868 if (inet_pton(AF_INET, hosts[nhostsbak],
876 * We're doing host-based access checks here, so don't
877 * allow v4-in-v6 to confuse things. The kernel will
878 * disable it by default on NFS sockets too.
880 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
883 "can't disable v4-in-v6 on IPv6 socket");
892 * If no hosts were specified, just bind to INADDR_ANY
894 if (strcmp("*", hosts[nhostsbak]) == 0) {
895 if (svcport_str == NULL) {
896 res = malloc(sizeof(struct addrinfo));
900 res->ai_flags = hints.ai_flags;
901 res->ai_family = hints.ai_family;
902 res->ai_protocol = hints.ai_protocol;
903 switch (res->ai_family) {
905 sin = malloc(sizeof(struct sockaddr_in));
908 sin->sin_family = AF_INET;
909 sin->sin_port = htons(0);
910 sin->sin_addr.s_addr = htonl(INADDR_ANY);
911 res->ai_addr = (struct sockaddr*) sin;
912 res->ai_addrlen = (socklen_t)
913 sizeof(struct sockaddr_in);
916 sin6 = malloc(sizeof(struct sockaddr_in6));
919 sin6->sin6_family = AF_INET6;
920 sin6->sin6_port = htons(0);
921 sin6->sin6_addr = in6addr_any;
922 res->ai_addr = (struct sockaddr*) sin6;
923 res->ai_addrlen = (socklen_t)
924 sizeof(struct sockaddr_in6);
927 syslog(LOG_ERR, "bad addr fam %d",
932 if ((aicode = getaddrinfo(NULL, svcport_str,
933 &hints, &res)) != 0) {
935 "cannot get local address for %s: %s",
937 gai_strerror(aicode));
943 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
944 &hints, &res)) != 0) {
946 "cannot get local address for %s: %s",
947 nconf->nc_netid, gai_strerror(aicode));
954 sock_fd[sock_fdcnt - 1] = fd;
956 /* Now, attempt the bind. */
957 r = bindresvport_sa(fd, res->ai_addr);
959 if (errno == EADDRINUSE && mallocd_svcport != 0) {
960 if (mallocd_res != 0) {
967 syslog(LOG_ERR, "bindresvport_sa: %m");
971 if (svcport_str == NULL) {
972 svcport_str = malloc(NI_MAXSERV * sizeof(char));
973 if (svcport_str == NULL)
977 if (getnameinfo(res->ai_addr,
978 res->ai_addr->sa_len, NULL, NI_MAXHOST,
979 svcport_str, NI_MAXSERV * sizeof(char),
980 NI_NUMERICHOST | NI_NUMERICSERV))
981 errx(1, "Cannot get port number");
983 if (mallocd_res != 0) {
994 * Called after all the create_service() calls have succeeded, to complete
995 * the setup and registration.
998 complete_service(struct netconfig *nconf, char *port_str)
1000 struct addrinfo hints, *res = NULL;
1001 struct __rpc_sockinfo si;
1002 struct netbuf servaddr;
1003 SVCXPRT *transp = NULL;
1004 int aicode, fd, nhostsbak;
1007 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
1008 (nconf->nc_semantics != NC_TPI_COTS) &&
1009 (nconf->nc_semantics != NC_TPI_COTS_ORD))
1010 return; /* not my type */
1013 * XXX - using RPC library internal functions.
1015 if (!__rpc_nconf2sockinfo(nconf, &si)) {
1016 syslog(LOG_ERR, "cannot get information for %s",
1022 while (nhostsbak > 0) {
1024 if (sock_fdpos >= sock_fdcnt) {
1025 /* Should never happen. */
1026 syslog(LOG_ERR, "Ran out of socket fd's");
1029 fd = sock_fd[sock_fdpos++];
1034 * Using -1 tells listen(2) to use
1035 * kern.ipc.soacceptqueue for the backlog.
1037 if (nconf->nc_semantics != NC_TPI_CLTS)
1040 if (nconf->nc_semantics == NC_TPI_CLTS )
1041 transp = svc_dg_create(fd, 0, 0);
1043 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
1046 if (transp != (SVCXPRT *) NULL) {
1047 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
1050 "can't register %s MOUNTVERS service",
1053 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
1056 "can't register %s MOUNTVERS3 service",
1060 syslog(LOG_WARNING, "can't create %s services",
1063 if (registered == 0) {
1065 memset(&hints, 0, sizeof hints);
1066 hints.ai_flags = AI_PASSIVE;
1067 hints.ai_family = si.si_af;
1068 hints.ai_socktype = si.si_socktype;
1069 hints.ai_protocol = si.si_proto;
1071 if ((aicode = getaddrinfo(NULL, port_str, &hints,
1073 syslog(LOG_ERR, "cannot get local address: %s",
1074 gai_strerror(aicode));
1078 servaddr.buf = malloc(res->ai_addrlen);
1079 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
1080 servaddr.len = res->ai_addrlen;
1082 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
1083 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
1092 * Clear out sockets after a failure to bind one of them, so that the
1093 * cycle of socket creation/binding can start anew.
1096 clearout_service(void)
1100 for (i = 0; i < sock_fdcnt; i++) {
1101 if (sock_fd[i] >= 0) {
1102 shutdown(sock_fd[i], SHUT_RDWR);
1112 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
1113 "[-S] [-h <bindip>] [export_file ...]\n");
1118 * The mount rpc service
1121 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
1123 struct exportlist *ep;
1125 struct fhreturn fhr;
1128 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1129 int lookup_failed = 1;
1130 struct sockaddr *saddr;
1132 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1133 int defset, hostset;
1135 sigset_t sighup_mask;
1136 int numsecflavors, *secflavorsp;
1138 sigemptyset(&sighup_mask);
1139 sigaddset(&sighup_mask, SIGHUP);
1140 saddr = svc_getrpccaller(transp)->buf;
1141 switch (saddr->sa_family) {
1143 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1146 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1149 syslog(LOG_ERR, "request from unknown address family");
1152 switch (rqstp->rq_proc) {
1154 case MOUNTPROC_UMNT:
1155 case MOUNTPROC_UMNTALL:
1156 lookup_failed = getnameinfo(saddr, saddr->sa_len, host,
1157 sizeof host, NULL, 0, 0);
1159 getnameinfo(saddr, saddr->sa_len, numerichost,
1160 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1161 switch (rqstp->rq_proc) {
1163 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1164 syslog(LOG_ERR, "can't send reply");
1167 if (sport >= IPPORT_RESERVED && resvport_only) {
1169 "mount request from %s from unprivileged port",
1171 svcerr_weakauth(transp);
1174 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1175 syslog(LOG_NOTICE, "undecodable mount request from %s",
1177 svcerr_decode(transp);
1182 * Get the real pathname and make sure it is a directory
1183 * or a regular file if the -r option was specified
1186 if (realpath(rpcpath, dirpath) == NULL ||
1187 stat(dirpath, &stb) < 0 ||
1188 statfs(dirpath, &fsb) < 0) {
1189 chdir("/"); /* Just in case realpath doesn't */
1191 "mount request from %s for non existent path %s",
1192 numerichost, dirpath);
1194 warnx("stat failed on %s", dirpath);
1195 bad = ENOENT; /* We will send error reply later */
1198 !S_ISDIR(stb.st_mode) &&
1199 (dir_only || !S_ISREG(stb.st_mode))) {
1201 "mount request from %s for non-directory path %s",
1202 numerichost, dirpath);
1204 warnx("mounting non-directory %s", dirpath);
1205 bad = ENOTDIR; /* We will send error reply later */
1208 /* Check in the exports list */
1209 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1213 ep = ex_search(&fsb.f_fsid, exphead);
1214 hostset = defset = 0;
1215 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1216 &numsecflavors, &secflavorsp) ||
1217 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1218 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1220 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1221 scan_tree(ep->ex_dirl, saddr) == 0))) {
1223 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1225 syslog(LOG_ERR, "can't send reply");
1226 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1229 if (hostset & DP_HOSTSET) {
1230 fhr.fhr_flag = hostset;
1231 fhr.fhr_numsecflavors = numsecflavors;
1232 fhr.fhr_secflavors = secflavorsp;
1234 fhr.fhr_flag = defset;
1235 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1236 fhr.fhr_secflavors = ep->ex_defsecflavors;
1238 fhr.fhr_vers = rqstp->rq_vers;
1239 /* Get the file handle */
1240 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1241 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1243 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1244 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1246 syslog(LOG_ERR, "can't send reply");
1247 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1250 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1252 syslog(LOG_ERR, "can't send reply");
1254 add_mlist(host, dirpath);
1256 add_mlist(numerichost, dirpath);
1258 warnx("mount successful");
1261 "mount request succeeded from %s for %s",
1262 numerichost, dirpath);
1267 "mount request denied from %s for %s",
1268 numerichost, dirpath);
1271 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1273 syslog(LOG_ERR, "can't send reply");
1274 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1276 case MOUNTPROC_DUMP:
1277 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1278 syslog(LOG_ERR, "can't send reply");
1281 "dump request succeeded from %s",
1284 case MOUNTPROC_UMNT:
1285 if (sport >= IPPORT_RESERVED && resvport_only) {
1287 "umount request from %s from unprivileged port",
1289 svcerr_weakauth(transp);
1292 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1293 syslog(LOG_NOTICE, "undecodable umount request from %s",
1295 svcerr_decode(transp);
1298 if (realpath(rpcpath, dirpath) == NULL) {
1299 syslog(LOG_NOTICE, "umount request from %s "
1300 "for non existent path %s",
1301 numerichost, dirpath);
1303 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1304 syslog(LOG_ERR, "can't send reply");
1306 del_mlist(host, dirpath);
1307 del_mlist(numerichost, dirpath);
1310 "umount request succeeded from %s for %s",
1311 numerichost, dirpath);
1313 case MOUNTPROC_UMNTALL:
1314 if (sport >= IPPORT_RESERVED && resvport_only) {
1316 "umountall request from %s from unprivileged port",
1318 svcerr_weakauth(transp);
1321 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1322 syslog(LOG_ERR, "can't send reply");
1324 del_mlist(host, NULL);
1325 del_mlist(numerichost, NULL);
1328 "umountall request succeeded from %s",
1331 case MOUNTPROC_EXPORT:
1332 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1333 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1335 syslog(LOG_ERR, "can't send reply");
1338 "export request succeeded from %s",
1342 svcerr_noproc(transp);
1348 * Xdr conversion for a dirpath string
1351 xdr_dir(XDR *xdrsp, char *dirp)
1353 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1357 * Xdr routine to generate file handle reply
1360 xdr_fhs(XDR *xdrsp, caddr_t cp)
1362 struct fhreturn *fhrp = (struct fhreturn *)cp;
1363 u_long ok = 0, len, auth;
1366 if (!xdr_long(xdrsp, &ok))
1368 switch (fhrp->fhr_vers) {
1370 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1373 if (!xdr_long(xdrsp, &len))
1375 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1377 if (fhrp->fhr_numsecflavors) {
1378 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1380 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1381 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1387 if (!xdr_long(xdrsp, &len))
1389 return (xdr_long(xdrsp, &auth));
1396 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1398 struct mountlist *mlp;
1403 SLIST_FOREACH(mlp, &mlhead, next) {
1404 if (!xdr_bool(xdrsp, &true))
1406 strp = &mlp->ml_host[0];
1407 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1409 strp = &mlp->ml_dirp[0];
1410 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1413 if (!xdr_bool(xdrsp, &false))
1419 * Xdr conversion for export list
1422 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1424 struct exportlist *ep;
1427 sigset_t sighup_mask;
1430 sigemptyset(&sighup_mask);
1431 sigaddset(&sighup_mask, SIGHUP);
1432 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1434 for (i = 0; i < exphashsize; i++)
1435 SLIST_FOREACH(ep, &exphead[i], entries) {
1437 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1440 if (ep->ex_defdir && putdef == 0 &&
1441 put_exlist(ep->ex_defdir, xdrsp, NULL,
1445 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1446 if (!xdr_bool(xdrsp, &false))
1450 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1455 * Called from xdr_explist() to traverse the tree and export the
1459 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1462 struct grouplist *grp;
1463 struct hostlist *hp;
1470 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1472 if (!xdr_bool(xdrsp, &true))
1475 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1477 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1482 if (!xdr_bool(xdrsp, &true))
1485 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1487 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1488 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1492 if (grp->gr_type == GT_HOST) {
1493 if (!xdr_bool(xdrsp, &true))
1495 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1496 if (!xdr_string(xdrsp, &strp,
1499 } else if (grp->gr_type == GT_NET) {
1500 if (!xdr_bool(xdrsp, &true))
1502 strp = grp->gr_ptr.gt_net.nt_name;
1503 if (!xdr_string(xdrsp, &strp,
1508 if (gotalldir && hp == (struct hostlist *)NULL) {
1514 if (!xdr_bool(xdrsp, &false))
1516 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1523 xdr_explist(XDR *xdrsp, caddr_t cp)
1526 return xdr_explist_common(xdrsp, cp, 0);
1530 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1533 return xdr_explist_common(xdrsp, cp, 1);
1537 static size_t linesize;
1538 static FILE *exp_file;
1541 * Get the export list from one, currently open file
1544 get_exportlist_one(int passno)
1546 struct exportlist *ep;
1547 struct grouplist *grp, *tgrp, *savgrp;
1548 struct dirlist *dirhead;
1551 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1552 int len, has_host, exflags, got_nondir, dirplen, netgrp;
1555 dirhead = (struct dirlist *)NULL;
1556 while (get_line()) {
1558 warnx("got line %s", line);
1560 nextfield(&cp, &endcp);
1569 exflags = MNT_EXPORTED;
1572 ep = (struct exportlist *)NULL;
1576 * Handle the V4 root dir.
1578 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1580 * V4: just indicates that it is the v4 root point,
1581 * so skip over that and set v4root_phase.
1583 if (v4root_phase > 0) {
1584 syslog(LOG_ERR, "V4:duplicate line, ignored");
1589 nextfield(&cp, &endcp);
1593 * Create new exports list entry
1596 tgrp = grp = get_grp();
1598 if (len > MNTNAMLEN) {
1599 getexp_err(ep, tgrp, "mountpoint too long");
1603 if (ep == (struct exportlist *)NULL) {
1604 getexp_err(ep, tgrp,
1605 "flag before export path definition");
1609 warnx("doing opt %s", cp);
1611 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1613 getexp_err(ep, tgrp, NULL);
1616 } else if (*cp == '/') {
1619 if (v4root_phase > 1) {
1621 getexp_err(ep, tgrp, "Multiple V4 dirs");
1625 if (check_dirpath(cp) &&
1626 statfs(cp, &fsb) >= 0) {
1627 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1628 syslog(LOG_ERR, "Warning: exporting of "
1629 "automounted fs %s not supported", cp);
1631 getexp_err(ep, tgrp, "dirs must be first");
1634 if (v4root_phase == 1) {
1636 getexp_err(ep, tgrp, "Multiple V4 dirs");
1639 if (strlen(v4root_dirpath) == 0) {
1640 strlcpy(v4root_dirpath, cp,
1641 sizeof (v4root_dirpath));
1642 } else if (strcmp(v4root_dirpath, cp)
1645 "different V4 dirpath %s", cp);
1646 getexp_err(ep, tgrp, NULL);
1655 if (fsidcmp(&ep->ex_fs, &fsb.f_fsid)
1657 getexp_err(ep, tgrp,
1663 * See if this directory is already
1666 ep = ex_search(&fsb.f_fsid, exphead);
1667 if (ep == (struct exportlist *)NULL) {
1669 ep->ex_fs = fsb.f_fsid;
1670 ep->ex_fsdir = strdup(fsb.f_mntonname);
1671 if (ep->ex_fsdir == NULL)
1675 "making new ep fs=0x%x,0x%x",
1679 warnx("found ep fs=0x%x,0x%x",
1685 * Add dirpath to export mount point.
1687 dirp = add_expdir(&dirhead, cp, len);
1691 getexp_err(ep, tgrp,
1692 "symbolic link in export path or statfs failed");
1700 if (ep == (struct exportlist *)NULL) {
1701 getexp_err(ep, tgrp,
1702 "host(s) before export path definition");
1707 * Get the host or netgroup.
1710 netgrp = getnetgrent(&hst, &usr, &dom);
1713 grp->gr_next = get_grp();
1719 "null hostname in netgroup %s, skipping", cp);
1720 grp->gr_type = GT_IGNORE;
1721 } else if (get_host(hst, grp, tgrp)) {
1723 "bad host %s in netgroup %s, skipping", hst, cp);
1724 grp->gr_type = GT_IGNORE;
1726 } else if (get_host(cp, grp, tgrp)) {
1727 syslog(LOG_ERR, "bad host %s, skipping", cp);
1728 grp->gr_type = GT_IGNORE;
1731 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1736 nextfield(&cp, &endcp);
1739 if (check_options(dirhead)) {
1740 getexp_err(ep, tgrp, NULL);
1744 grp->gr_type = GT_DEFAULT;
1746 warnx("adding a default entry");
1749 * Don't allow a network export coincide with a list of
1750 * host(s) on the same line.
1752 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1753 getexp_err(ep, tgrp, "network/host conflict");
1757 * If an export list was specified on this line, make sure
1758 * that we have at least one valid entry, otherwise skip it.
1762 while (grp && grp->gr_type == GT_IGNORE)
1765 getexp_err(ep, tgrp, "no valid entries");
1770 if (v4root_phase == 1) {
1771 getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1776 * Loop through hosts, pushing the exports into the kernel.
1777 * After loop, tgrp points to the start of the list and
1778 * grp points to the last entry in the list.
1779 * Do not do the do_mount() for passno == 1, since the
1780 * second pass will do it, as required.
1784 grp->gr_exflags = exflags;
1785 grp->gr_anon = anon;
1786 if (v4root_phase == 2 && passno == 0)
1787 LOGDEBUG("do_mount v4root");
1788 if (passno == 0 && do_mount(ep, grp, exflags, &anon,
1789 dirp, dirplen, &fsb, ep->ex_numsecflavors,
1790 ep->ex_secflavors)) {
1791 getexp_err(ep, tgrp, NULL);
1794 } while (grp->gr_next && (grp = grp->gr_next));
1797 * For V4: don't enter in mount lists.
1799 if (v4root_phase > 0 && v4root_phase <= 2) {
1801 * These structures are used for the reload,
1802 * so save them for that case. Otherwise, just
1805 if (passno == 1 && ep != NULL) {
1807 while (tgrp != NULL) {
1809 * Save the security flavors and exflags
1810 * for this host set in the groups.
1812 tgrp->gr_numsecflavors =
1813 ep->ex_numsecflavors;
1814 if (ep->ex_numsecflavors > 0)
1815 memcpy(tgrp->gr_secflavors,
1817 sizeof(ep->ex_secflavors));
1818 tgrp = tgrp->gr_next;
1820 if (v4root_ep == NULL) {
1822 ep = NULL; /* Don't free below. */
1824 grp->gr_next = v4root_ep->ex_grphead;
1825 v4root_ep->ex_grphead = savgrp;
1829 while (tgrp != NULL) {
1831 tgrp = tgrp->gr_next;
1838 * Success. Update the data structures.
1841 hang_dirp(dirhead, tgrp, ep, opt_flags, &anon, exflags);
1842 grp->gr_next = ep->ex_grphead;
1843 ep->ex_grphead = tgrp;
1845 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1846 opt_flags, &anon, exflags);
1849 dirhead = (struct dirlist *)NULL;
1850 if ((ep->ex_flag & EX_LINKED) == 0) {
1851 insert_exports(ep, exphead);
1853 ep->ex_flag |= EX_LINKED;
1859 dirhead = (struct dirlist *)NULL;
1865 * Get the export list from all specified files
1868 get_exportlist(int passno)
1870 struct export_args export;
1872 struct statfs *mntbufp;
1874 int error, i, nfs_maxvers, num;
1876 struct nfsex_args eargs;
1878 size_t nfs_maxvers_size;
1880 if ((debug_file = fopen(_PATH_MOUNTDDEBUG, "r")) != NULL) {
1885 LOGDEBUG("passno=%d", passno);
1886 v4root_dirpath[0] = '\0';
1890 * Save the current lists as old ones, so that the new lists
1891 * can be compared with the old ones in the 2nd pass.
1893 for (i = 0; i < exphashsize; i++) {
1894 SLIST_FIRST(&oldexphead[i]) = SLIST_FIRST(&exphead[i]);
1895 SLIST_INIT(&exphead[i]);
1898 /* Note that the public fh has not yet been set. */
1899 has_set_publicfh = 0;
1901 /* Read the export file(s) and process them */
1902 read_exportfile(passno);
1905 * Just make the old lists empty.
1906 * exphashsize == 0 for the first call, before oldexphead
1907 * has been initialized-->loop won't be executed.
1909 for (i = 0; i < exphashsize; i++)
1910 SLIST_INIT(&oldexphead[i]);
1913 bzero(&export, sizeof(export));
1914 export.ex_flags = MNT_DELEXPORT;
1917 bzero(errmsg, sizeof(errmsg));
1919 if (suspend_nfsd != 0)
1920 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1922 * Delete the old V4 root dir.
1924 bzero(&eargs, sizeof (eargs));
1925 eargs.export.ex_flags = MNT_DELEXPORT;
1926 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 &&
1928 syslog(LOG_ERR, "Can't delete exports for V4:");
1930 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1931 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1932 build_iovec(&iov, &iovlen, "from", NULL, 0);
1933 build_iovec(&iov, &iovlen, "update", NULL, 0);
1934 build_iovec(&iov, &iovlen, "export", &export,
1936 build_iovec(&iov, &iovlen, "errmsg", errmsg,
1940 * For passno == 1, compare the old and new lists updating the kernel
1941 * exports for any cases that have changed.
1942 * This call is doing the second pass through the lists.
1943 * If it fails, fall back on the bulk reload.
1945 if (passno == 1 && compare_nmount_exportlist(iov, iovlen, errmsg) ==
1947 LOGDEBUG("compareok");
1948 /* Free up the old lists. */
1949 free_exports(oldexphead);
1951 LOGDEBUG("doing passno=0");
1953 * Clear flag that notes if a public fh has been exported.
1954 * It is set by do_mount() if MNT_EXPUBLIC is set for the entry.
1958 /* exphead == NULL if not yet allocated (first call). */
1959 if (exphead != NULL) {
1961 * First, get rid of the old lists.
1963 free_exports(exphead);
1964 free_exports(oldexphead);
1968 * And delete exports that are in the kernel for all local
1970 * XXX: Should know how to handle all local exportable
1973 num = getmntinfo(&mntbufp, MNT_NOWAIT);
1975 /* Allocate hash tables, for first call. */
1976 if (exphead == NULL) {
1977 /* Target an average linked list length of 10. */
1978 exphashsize = num / 10;
1979 if (exphashsize < 1)
1981 else if (exphashsize > 100000)
1982 exphashsize = 100000;
1983 exphead = malloc(exphashsize * sizeof(*exphead));
1984 oldexphead = malloc(exphashsize * sizeof(*oldexphead));
1985 if (exphead == NULL || oldexphead == NULL)
1986 errx(1, "Can't malloc hash tables");
1988 for (i = 0; i < exphashsize; i++) {
1989 SLIST_INIT(&exphead[i]);
1990 SLIST_INIT(&oldexphead[i]);
1994 for (i = 0; i < num; i++)
1995 delete_export(iov, iovlen, &mntbufp[i], errmsg);
1998 /* Read the export file(s) and process them */
2002 if (strlen(v4root_dirpath) == 0) {
2003 /* Check to see if a V4: line is needed. */
2004 nfs_maxvers_size = sizeof(nfs_maxvers);
2005 error = sysctlbyname("vfs.nfsd.server_max_nfsvers",
2006 &nfs_maxvers, &nfs_maxvers_size, NULL, 0);
2007 if (error != 0 || nfs_maxvers < NFS_VER2 || nfs_maxvers >
2009 syslog(LOG_ERR, "sysctlbyname(vfs.nfsd."
2010 "server_max_nfsvers) failed, defaulting to NFSv3");
2011 nfs_maxvers = NFS_VER3;
2013 if (nfs_maxvers == NFS_VER4)
2014 syslog(LOG_ERR, "NFSv4 requires at least one V4: line");
2018 /* Free strings allocated by strdup() in getmntopts.c */
2019 free(iov[0].iov_base); /* fstype */
2020 free(iov[2].iov_base); /* fspath */
2021 free(iov[4].iov_base); /* from */
2022 free(iov[6].iov_base); /* update */
2023 free(iov[8].iov_base); /* export */
2024 free(iov[10].iov_base); /* errmsg */
2026 /* free iov, allocated by realloc() */
2032 * If there was no public fh, clear any previous one set.
2034 if (has_publicfh == 0) {
2035 LOGDEBUG("clear public fh");
2036 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
2039 /* Resume the nfsd. If they weren't suspended, this is harmless. */
2040 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
2041 LOGDEBUG("eo get_exportlist");
2045 * Insert an export entry in the appropriate list.
2048 insert_exports(struct exportlist *ep, struct exportlisthead *exhp)
2052 i = EXPHASH(&ep->ex_fs);
2053 LOGDEBUG("fs=%s hash=%i", ep->ex_fsdir, i);
2054 SLIST_INSERT_HEAD(&exhp[i], ep, entries);
2058 * Free up the exports lists passed in as arguments.
2061 free_exports(struct exportlisthead *exhp)
2063 struct exportlist *ep, *ep2;
2066 for (i = 0; i < exphashsize; i++) {
2067 SLIST_FOREACH_SAFE(ep, &exhp[i], entries, ep2) {
2068 SLIST_REMOVE(&exhp[i], ep, exportlist, entries);
2071 SLIST_INIT(&exhp[i]);
2076 * Read the exports file(s) and call get_exportlist_one() for each line.
2079 read_exportfile(int passno)
2084 * Read in the exports file and build the list, calling
2085 * nmount() as we go along to push the export rules into the kernel.
2088 for (i = 0; exnames[i] != NULL; i++) {
2090 warnx("reading exports from %s", exnames[i]);
2091 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
2092 syslog(LOG_WARNING, "can't open %s", exnames[i]);
2095 get_exportlist_one(passno);
2100 syslog(LOG_ERR, "can't open any exports file");
2106 * Compare the export lists against the old ones and do nmount() operations
2107 * for any cases that have changed. This avoids doing nmount() for entries
2108 * that have not changed.
2109 * Return 0 upon success, 1 otherwise.
2112 compare_nmount_exportlist(struct iovec *iov, int iovlen, char *errmsg)
2114 struct exportlist *ep, *oep;
2115 struct grouplist *grp;
2116 struct statfs fs, ofs;
2120 * Loop through the current list and look for an entry in the old
2122 * If found, check to see if it the same.
2123 * If it is not the same, delete and re-export.
2124 * Then mark it done on the old list.
2127 * Any entries left in the old list after processing must have their
2130 for (i = 0; i < exphashsize; i++)
2131 SLIST_FOREACH(ep, &exphead[i], entries) {
2132 LOGDEBUG("foreach ep=%s", ep->ex_fsdir);
2133 oep = ex_search(&ep->ex_fs, oldexphead);
2136 * Check the mount paths are the same.
2137 * If not, return 1 so that the reload of the
2138 * exports will be done in bulk, the
2141 LOGDEBUG("found old exp");
2142 if (strcmp(ep->ex_fsdir, oep->ex_fsdir) != 0)
2144 LOGDEBUG("same fsdir");
2146 * Test to see if the entry is the same.
2147 * If not the same delete exports and
2150 if (compare_export(ep, oep) != 0) {
2152 * Clear has_publicfh if if was set
2153 * in the old exports, but only if it
2154 * has not been set during processing of
2155 * the exports for this pass, as
2156 * indicated by has_set_publicfh.
2158 if (has_set_publicfh == 0 &&
2159 (oep->ex_flag & EX_PUBLICFH) != 0)
2162 /* Delete and re-export. */
2163 if (statfs(ep->ex_fsdir, &fs) < 0)
2165 delete_export(iov, iovlen, &fs, errmsg);
2166 ret = do_export_mount(ep, &fs);
2170 oep->ex_flag |= EX_DONE;
2173 LOGDEBUG("not found so export");
2174 /* Not found, so do export. */
2175 if (statfs(ep->ex_fsdir, &fs) < 0)
2177 ret = do_export_mount(ep, &fs);
2183 /* Delete exports not done. */
2184 for (i = 0; i < exphashsize; i++)
2185 SLIST_FOREACH(oep, &oldexphead[i], entries) {
2186 if ((oep->ex_flag & EX_DONE) == 0) {
2187 LOGDEBUG("not done delete=%s", oep->ex_fsdir);
2188 if (statfs(oep->ex_fsdir, &ofs) >= 0 &&
2189 fsidcmp(&oep->ex_fs, &ofs.f_fsid) == 0) {
2190 LOGDEBUG("do delete");
2192 * Clear has_publicfh if if was set
2193 * in the old exports, but only if it
2194 * has not been set during processing of
2195 * the exports for this pass, as
2196 * indicated by has_set_publicfh.
2198 if (has_set_publicfh == 0 &&
2199 (oep->ex_flag & EX_PUBLICFH) != 0)
2202 delete_export(iov, iovlen, &ofs,
2208 /* Do the V4 root exports, as required. */
2210 if (v4root_ep != NULL)
2211 grp = v4root_ep->ex_grphead;
2213 while (v4root_ep != NULL && grp != NULL) {
2214 LOGDEBUG("v4root expath=%s", v4root_dirpath);
2215 ret = do_mount(v4root_ep, grp, grp->gr_exflags, &grp->gr_anon,
2216 v4root_dirpath, strlen(v4root_dirpath), &fs,
2217 grp->gr_numsecflavors, grp->gr_secflavors);
2230 * Compare old and current exportlist entries for the fsid and return 0
2231 * if they are the same, 1 otherwise.
2234 compare_export(struct exportlist *ep, struct exportlist *oep)
2236 struct grouplist *grp, *ogrp;
2238 if (strcmp(ep->ex_fsdir, oep->ex_fsdir) != 0)
2240 if ((ep->ex_flag & EX_DEFSET) != (oep->ex_flag & EX_DEFSET))
2242 if ((ep->ex_defdir != NULL && oep->ex_defdir == NULL) ||
2243 (ep->ex_defdir == NULL && oep->ex_defdir != NULL))
2245 if (ep->ex_defdir != NULL && (ep->ex_defdir->dp_flag & DP_DEFSET) !=
2246 (oep->ex_defdir->dp_flag & DP_DEFSET))
2248 if ((ep->ex_flag & EX_DEFSET) != 0 && (ep->ex_defnumsecflavors !=
2249 oep->ex_defnumsecflavors || ep->ex_defexflags !=
2250 oep->ex_defexflags || compare_cred(&ep->ex_defanon,
2251 &oep->ex_defanon) != 0 || compare_secflavor(ep->ex_defsecflavors,
2252 oep->ex_defsecflavors, ep->ex_defnumsecflavors) != 0))
2255 /* Now, check all the groups. */
2256 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp = ogrp->gr_next)
2258 for (grp = ep->ex_grphead; grp != NULL; grp = grp->gr_next) {
2259 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp =
2261 if ((ogrp->gr_flag & GR_FND) == 0 &&
2262 grp->gr_numsecflavors == ogrp->gr_numsecflavors &&
2263 grp->gr_exflags == ogrp->gr_exflags &&
2264 compare_cred(&grp->gr_anon, &ogrp->gr_anon) == 0 &&
2265 compare_secflavor(grp->gr_secflavors,
2266 ogrp->gr_secflavors, grp->gr_numsecflavors) == 0)
2269 ogrp->gr_flag |= GR_FND;
2273 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp = ogrp->gr_next)
2274 if ((ogrp->gr_flag & GR_FND) == 0)
2280 * This algorithm compares two arrays of "n" items. It returns 0 if they are
2281 * the "same" and 1 otherwise. Although suboptimal, it is always safe to
2282 * return 1, which makes compare_nmount_export() reload the exports entry.
2283 * "same" refers to having the same set of values in the two arrays.
2284 * The arrays are in no particular order and duplicates (multiple entries
2285 * in an array with the same value) is allowed.
2286 * The algorithm is inefficient, but the common case of indentical arrays is
2287 * handled first and "n" is normally fairly small.
2288 * Since the two functions need the same algorithm but for arrays of
2289 * different types (gid_t vs int), this is done as a macro.
2291 #define COMPARE_ARRAYS(a1, a2, n) \
2293 int fnd, fndarray[(n)], i, j; \
2294 /* Handle common case of identical arrays. */ \
2295 for (i = 0; i < (n); i++) \
2296 if ((a1)[i] != (a2)[i]) \
2300 for (i = 0; i < (n); i++) \
2302 for (i = 0; i < (n); i++) { \
2304 for (j = 0; j < (n); j++) { \
2305 if ((a1)[i] == (a2)[j]) { \
2313 for (i = 0; i < (n); i++) \
2314 if (fndarray[i] == 0) \
2320 * Compare to struct xucred's. Return 0 if the same and 1 otherwise.
2323 compare_cred(struct xucred *cr0, struct xucred *cr1)
2326 if (cr0->cr_uid != cr1->cr_uid || cr0->cr_ngroups != cr1->cr_ngroups)
2329 COMPARE_ARRAYS(cr0->cr_groups, cr1->cr_groups, cr0->cr_ngroups);
2333 * Compare two lists of security flavors. Return 0 if the same and 1 otherwise.
2336 compare_secflavor(int *sec1, int *sec2, int nsec)
2339 COMPARE_ARRAYS(sec1, sec2, nsec);
2343 * Delete an exports entry.
2346 delete_export(struct iovec *iov, int iovlen, struct statfs *fsp, char *errmsg)
2348 struct xvfsconf vfc;
2350 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
2351 syslog(LOG_ERR, "getvfsbyname() failed for %s",
2357 * We do not need to delete "export" flag from
2358 * filesystems that do not have it set.
2360 if (!(fsp->f_flags & MNT_EXPORTED))
2363 * Do not delete export for network filesystem by
2364 * passing "export" arg to nmount().
2365 * It only makes sense to do this for local filesystems.
2367 if (vfc.vfc_flags & VFCF_NETWORK)
2370 iov[1].iov_base = fsp->f_fstypename;
2371 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
2372 iov[3].iov_base = fsp->f_mntonname;
2373 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
2374 iov[5].iov_base = fsp->f_mntfromname;
2375 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
2379 * EXDEV is returned when path exists but is not a
2380 * mount point. May happens if raced with unmount.
2382 if (nmount(iov, iovlen, fsp->f_flags) < 0 && errno != ENOENT &&
2383 errno != ENOTSUP && errno != EXDEV) {
2385 "can't delete exports for %s: %m %s",
2386 fsp->f_mntonname, errmsg);
2391 * Allocate an export list element
2393 static struct exportlist *
2396 struct exportlist *ep;
2398 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
2399 if (ep == (struct exportlist *)NULL)
2405 * Allocate a group list element
2407 static struct grouplist *
2410 struct grouplist *gp;
2412 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
2413 if (gp == (struct grouplist *)NULL)
2419 * Clean up upon an error in get_exportlist().
2422 getexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
2424 struct grouplist *tgrp;
2426 if (!(opt_flags & OP_QUIET)) {
2428 syslog(LOG_ERR, "bad exports list line '%s': %s", line,
2431 syslog(LOG_ERR, "bad exports list line '%s'", line);
2433 if (ep && (ep->ex_flag & EX_LINKED) == 0)
2443 * Search the export list for a matching fs.
2445 static struct exportlist *
2446 ex_search(fsid_t *fsid, struct exportlisthead *exhp)
2448 struct exportlist *ep;
2452 SLIST_FOREACH(ep, &exhp[i], entries) {
2453 if (fsidcmp(&ep->ex_fs, fsid) == 0)
2461 * Add a directory path to the list.
2464 add_expdir(struct dirlist **dpp, char *cp, int len)
2468 dp = malloc(sizeof (struct dirlist));
2469 if (dp == (struct dirlist *)NULL)
2472 dp->dp_right = (struct dirlist *)NULL;
2474 dp->dp_hosts = (struct hostlist *)NULL;
2475 dp->dp_dirp = strndup(cp, len);
2476 if (dp->dp_dirp == NULL)
2479 return (dp->dp_dirp);
2483 * Hang the dir list element off the dirpath binary tree as required
2484 * and update the entry for host.
2487 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
2488 int flags, struct xucred *anoncrp, int exflags)
2490 struct hostlist *hp;
2491 struct dirlist *dp2;
2493 if (flags & OP_ALLDIRS) {
2498 if (grp == (struct grouplist *)NULL) {
2499 ep->ex_flag |= EX_DEFSET;
2500 ep->ex_defdir->dp_flag |= DP_DEFSET;
2501 /* Save the default security flavors list. */
2502 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2503 if (ep->ex_numsecflavors > 0)
2504 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2505 sizeof(ep->ex_secflavors));
2506 ep->ex_defanon = *anoncrp;
2507 ep->ex_defexflags = exflags;
2508 } else while (grp) {
2511 hp->ht_next = ep->ex_defdir->dp_hosts;
2512 ep->ex_defdir->dp_hosts = hp;
2513 /* Save the security flavors list for this host set. */
2514 grp->gr_numsecflavors = ep->ex_numsecflavors;
2515 if (ep->ex_numsecflavors > 0)
2516 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2517 sizeof(ep->ex_secflavors));
2523 * Loop through the directories adding them to the tree.
2527 add_dlist(&ep->ex_dirl, dp, grp, flags, ep, anoncrp,
2535 * Traverse the binary tree either updating a node that is already there
2536 * for the new directory or adding the new node.
2539 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2540 int flags, struct exportlist *ep, struct xucred *anoncrp, int exflags)
2543 struct hostlist *hp;
2548 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2550 add_dlist(&dp->dp_left, newdp, grp, flags, ep, anoncrp,
2553 } else if (cmp < 0) {
2554 add_dlist(&dp->dp_right, newdp, grp, flags, ep, anoncrp,
2558 free((caddr_t)newdp);
2561 dp->dp_left = (struct dirlist *)NULL;
2567 * Hang all of the host(s) off of the directory point.
2572 hp->ht_next = dp->dp_hosts;
2574 /* Save the security flavors list for this host set. */
2575 grp->gr_numsecflavors = ep->ex_numsecflavors;
2576 if (ep->ex_numsecflavors > 0)
2577 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2578 sizeof(ep->ex_secflavors));
2582 ep->ex_flag |= EX_DEFSET;
2583 dp->dp_flag |= DP_DEFSET;
2584 /* Save the default security flavors list. */
2585 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2586 if (ep->ex_numsecflavors > 0)
2587 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2588 sizeof(ep->ex_secflavors));
2589 ep->ex_defanon = *anoncrp;
2590 ep->ex_defexflags = exflags;
2595 * Search for a dirpath on the export point.
2597 static struct dirlist *
2598 dirp_search(struct dirlist *dp, char *dirp)
2603 cmp = strcmp(dp->dp_dirp, dirp);
2605 return (dirp_search(dp->dp_left, dirp));
2607 return (dirp_search(dp->dp_right, dirp));
2615 * Scan for a host match in a directory tree.
2618 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2619 int *hostsetp, int *numsecflavors, int **secflavorsp)
2621 struct hostlist *hp;
2622 struct grouplist *grp;
2623 struct addrinfo *ai;
2626 if (dp->dp_flag & DP_DEFSET)
2627 *defsetp = dp->dp_flag;
2631 switch (grp->gr_type) {
2633 ai = grp->gr_ptr.gt_addrinfo;
2634 for (; ai; ai = ai->ai_next) {
2635 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2637 (hp->ht_flag | DP_HOSTSET);
2638 if (numsecflavors != NULL) {
2640 grp->gr_numsecflavors;
2649 if (!sacmp(saddr, (struct sockaddr *)
2650 &grp->gr_ptr.gt_net.nt_net,
2652 &grp->gr_ptr.gt_net.nt_mask)) {
2653 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2654 if (numsecflavors != NULL) {
2656 grp->gr_numsecflavors;
2671 * Scan tree for a host that matches the address.
2674 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2676 int defset, hostset;
2679 if (scan_tree(dp->dp_left, saddr))
2681 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2683 if (scan_tree(dp->dp_right, saddr))
2690 * Traverse the dirlist tree and free it up.
2693 free_dir(struct dirlist *dp)
2697 free_dir(dp->dp_left);
2698 free_dir(dp->dp_right);
2699 free_host(dp->dp_hosts);
2706 * Parse a colon separated list of security flavors
2709 parsesec(char *seclist, struct exportlist *ep)
2714 ep->ex_numsecflavors = 0;
2716 cp = strchr(seclist, ':');
2722 if (!strcmp(seclist, "sys"))
2724 else if (!strcmp(seclist, "krb5"))
2725 flavor = RPCSEC_GSS_KRB5;
2726 else if (!strcmp(seclist, "krb5i"))
2727 flavor = RPCSEC_GSS_KRB5I;
2728 else if (!strcmp(seclist, "krb5p"))
2729 flavor = RPCSEC_GSS_KRB5P;
2733 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2736 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2739 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2742 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2743 ep->ex_numsecflavors++;
2755 * Parse the option string and update fields.
2756 * Option arguments may either be -<option>=<value> or
2760 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2761 int *has_hostp, int *exflagsp, struct xucred *cr)
2763 char *cpoptarg, *cpoptend;
2764 char *cp, *endcp, *cpopt, savedc, savedc2;
2765 int allflag, usedarg;
2773 while (cpopt && *cpopt) {
2776 if ((cpoptend = strchr(cpopt, ','))) {
2778 if ((cpoptarg = strchr(cpopt, '=')))
2781 if ((cpoptarg = strchr(cpopt, '=')))
2785 nextfield(&cp, &endcp);
2787 if (endcp > cp && *cp != '-') {
2795 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2796 *exflagsp |= MNT_EXRDONLY;
2797 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2798 !(allflag = strcmp(cpopt, "mapall")) ||
2799 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2801 parsecred(cpoptarg, cr);
2803 *exflagsp |= MNT_EXPORTANON;
2804 opt_flags |= OP_MAPALL;
2806 opt_flags |= OP_MAPROOT;
2807 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2808 !strcmp(cpopt, "m"))) {
2809 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2810 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2814 opt_flags |= OP_MASK;
2815 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2816 !strcmp(cpopt, "n"))) {
2817 if (strchr(cpoptarg, '/') != NULL) {
2819 fprintf(stderr, "setting OP_MASKLEN\n");
2820 opt_flags |= OP_MASKLEN;
2822 if (grp->gr_type != GT_NULL) {
2823 syslog(LOG_ERR, "network/host conflict");
2825 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2826 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2829 grp->gr_type = GT_NET;
2832 opt_flags |= OP_NET;
2833 } else if (!strcmp(cpopt, "alldirs")) {
2834 opt_flags |= OP_ALLDIRS;
2835 } else if (!strcmp(cpopt, "public")) {
2836 *exflagsp |= MNT_EXPUBLIC;
2837 } else if (!strcmp(cpopt, "webnfs")) {
2838 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2839 opt_flags |= OP_MAPALL;
2840 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2841 ep->ex_indexfile = strdup(cpoptarg);
2842 } else if (!strcmp(cpopt, "quiet")) {
2843 opt_flags |= OP_QUIET;
2844 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2845 if (parsesec(cpoptarg, ep))
2847 opt_flags |= OP_SEC;
2850 syslog(LOG_ERR, "bad opt %s", cpopt);
2869 * Translate a character string to the corresponding list of network
2870 * addresses for a hostname.
2873 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2875 struct grouplist *checkgrp;
2876 struct addrinfo *ai, *tai, hints;
2878 char host[NI_MAXHOST];
2880 if (grp->gr_type != GT_NULL) {
2881 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
2884 memset(&hints, 0, sizeof hints);
2885 hints.ai_flags = AI_CANONNAME;
2886 hints.ai_protocol = IPPROTO_UDP;
2887 ecode = getaddrinfo(cp, NULL, &hints, &ai);
2889 syslog(LOG_ERR,"can't get address info for host %s", cp);
2892 grp->gr_ptr.gt_addrinfo = ai;
2893 while (ai != NULL) {
2894 if (ai->ai_canonname == NULL) {
2895 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
2896 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
2897 strlcpy(host, "?", sizeof(host));
2898 ai->ai_canonname = strdup(host);
2899 ai->ai_flags |= AI_CANONNAME;
2902 fprintf(stderr, "got host %s\n", ai->ai_canonname);
2904 * Sanity check: make sure we don't already have an entry
2905 * for this host in the grouplist.
2907 for (checkgrp = tgrp; checkgrp != NULL;
2908 checkgrp = checkgrp->gr_next) {
2909 if (checkgrp->gr_type != GT_HOST)
2911 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
2912 tai = tai->ai_next) {
2913 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
2917 "ignoring duplicate host %s\n",
2919 grp->gr_type = GT_IGNORE;
2925 grp->gr_type = GT_HOST;
2930 * Free up an exports list component
2933 free_exp(struct exportlist *ep)
2935 struct grouplist *grp, *tgrp;
2937 if (ep->ex_defdir) {
2938 free_host(ep->ex_defdir->dp_hosts);
2939 free((caddr_t)ep->ex_defdir);
2943 if (ep->ex_indexfile)
2944 free(ep->ex_indexfile);
2945 free_dir(ep->ex_dirl);
2946 grp = ep->ex_grphead;
2956 * Free up the v4root exports.
2959 free_v4rootexp(void)
2962 if (v4root_ep != NULL) {
2963 free_exp(v4root_ep);
2972 free_host(struct hostlist *hp)
2974 struct hostlist *hp2;
2983 static struct hostlist *
2986 struct hostlist *hp;
2988 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
2989 if (hp == (struct hostlist *)NULL)
2991 hp->ht_next = (struct hostlist *)NULL;
2997 * Out of memory, fatal
3003 syslog(LOG_ERR, "out of memory");
3008 * Call do_mount() from the struct exportlist, for each case needed.
3011 do_export_mount(struct exportlist *ep, struct statfs *fsp)
3013 struct grouplist *grp, defgrp;
3017 LOGDEBUG("do_export_mount=%s", ep->ex_fsdir);
3018 dirlen = strlen(ep->ex_fsdir);
3019 if ((ep->ex_flag & EX_DEFSET) != 0) {
3020 defgrp.gr_type = GT_DEFAULT;
3021 defgrp.gr_next = NULL;
3022 /* We have an entry for all other hosts/nets. */
3023 LOGDEBUG("ex_defexflags=0x%x", ep->ex_defexflags);
3024 ret = do_mount(ep, &defgrp, ep->ex_defexflags, &ep->ex_defanon,
3025 ep->ex_fsdir, dirlen, fsp, ep->ex_defnumsecflavors,
3026 ep->ex_defsecflavors);
3031 /* Do a mount for each group. */
3032 grp = ep->ex_grphead;
3033 while (grp != NULL) {
3034 LOGDEBUG("do mount gr_type=0x%x gr_exflags=0x%x",
3035 grp->gr_type, grp->gr_exflags);
3036 ret = do_mount(ep, grp, grp->gr_exflags, &grp->gr_anon,
3037 ep->ex_fsdir, dirlen, fsp, grp->gr_numsecflavors,
3038 grp->gr_secflavors);
3047 * Do the nmount() syscall with the update flag to push the export info into
3051 do_mount(struct exportlist *ep, struct grouplist *grp, int exflags,
3052 struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb,
3053 int numsecflavors, int *secflavors)
3056 struct addrinfo *ai;
3057 struct export_args *eap;
3065 struct nfsex_args nfsea;
3067 eap = &nfsea.export;
3075 bzero(eap, sizeof (struct export_args));
3076 bzero(errmsg, sizeof(errmsg));
3077 eap->ex_flags = exflags;
3078 eap->ex_anon = *anoncrp;
3079 LOGDEBUG("do_mount exflags=0x%x", exflags);
3080 eap->ex_indexfile = ep->ex_indexfile;
3081 if (grp->gr_type == GT_HOST)
3082 ai = grp->gr_ptr.gt_addrinfo;
3085 eap->ex_numsecflavors = numsecflavors;
3086 LOGDEBUG("do_mount numsec=%d", numsecflavors);
3087 for (i = 0; i < eap->ex_numsecflavors; i++)
3088 eap->ex_secflavors[i] = secflavors[i];
3089 if (eap->ex_numsecflavors == 0) {
3090 eap->ex_numsecflavors = 1;
3091 eap->ex_secflavors[0] = AUTH_SYS;
3095 if (v4root_phase == 0) {
3096 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
3097 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
3098 build_iovec(&iov, &iovlen, "from", NULL, 0);
3099 build_iovec(&iov, &iovlen, "update", NULL, 0);
3100 build_iovec(&iov, &iovlen, "export", eap,
3101 sizeof (struct export_args));
3102 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
3106 switch (grp->gr_type) {
3108 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
3110 eap->ex_addr = ai->ai_addr;
3111 eap->ex_addrlen = ai->ai_addrlen;
3112 eap->ex_masklen = 0;
3115 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
3119 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
3121 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
3123 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
3124 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
3127 eap->ex_addr = NULL;
3128 eap->ex_addrlen = 0;
3129 eap->ex_mask = NULL;
3130 eap->ex_masklen = 0;
3137 syslog(LOG_ERR, "bad grouptype");
3145 * For V4:, use the nfssvc() syscall, instead of mount().
3147 if (v4root_phase == 2) {
3148 nfsea.fspec = v4root_dirpath;
3149 if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) {
3150 syslog(LOG_ERR, "Exporting V4: failed");
3156 * Maybe I should just use the fsb->f_mntonname path
3157 * instead of looping back up the dirp to the mount
3159 * Also, needs to know how to export all types of local
3160 * exportable filesystems and not just "ufs".
3162 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
3163 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
3164 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
3165 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
3166 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
3167 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
3170 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
3174 cp = dirp + dirplen - 1;
3175 if (opt_flags & OP_QUIET) {
3179 if (errno == EPERM) {
3181 warnx("can't change attributes for %s: %s",
3184 "can't change attributes for %s: %s",
3189 if (opt_flags & OP_ALLDIRS) {
3190 if (errno == EINVAL)
3192 "-alldirs requested but %s is not a filesystem mountpoint",
3196 "could not remount %s: %m",
3201 /* back up over the last component */
3202 while (cp > dirp && *cp == '/')
3204 while (cp > dirp && *(cp - 1) != '/')
3208 warnx("mnt unsucc");
3209 syslog(LOG_ERR, "can't export %s %s",
3217 * Check that we're still on the same
3220 if (statfs(dirp, &fsb1) != 0 ||
3221 fsidcmp(&fsb1.f_fsid, &fsb->f_fsid) != 0) {
3224 "can't export %s %s", dirp,
3233 * For the experimental server:
3234 * If this is the public directory, get the file handle
3235 * and load it into the kernel via the nfssvc() syscall.
3237 if ((exflags & MNT_EXPUBLIC) != 0) {
3241 if (eap->ex_indexfile != NULL)
3242 public_name = eap->ex_indexfile;
3245 if (getfh(public_name, &fh) < 0)
3247 "Can't get public fh for %s", public_name);
3248 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
3250 "Can't set public fh for %s", public_name);
3253 has_set_publicfh = 1;
3254 ep->ex_flag |= EX_PUBLICFH;
3266 /* free strings allocated by strdup() in getmntopts.c */
3268 free(iov[0].iov_base); /* fstype */
3269 free(iov[2].iov_base); /* fspath */
3270 free(iov[4].iov_base); /* from */
3271 free(iov[6].iov_base); /* update */
3272 free(iov[8].iov_base); /* export */
3273 free(iov[10].iov_base); /* errmsg */
3275 /* free iov, allocated by realloc() */
3282 * Translate a net address.
3284 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
3287 get_net(char *cp, struct netmsk *net, int maskflg)
3289 struct netent *np = NULL;
3290 char *name, *p, *prefp;
3291 struct sockaddr_in sin;
3292 struct sockaddr *sa = NULL;
3293 struct addrinfo hints, *ai = NULL;
3294 char netname[NI_MAXHOST];
3298 if ((opt_flags & OP_MASKLEN) && !maskflg) {
3299 p = strchr(cp, '/');
3305 * Check for a numeric address first. We wish to avoid
3306 * possible DNS lookups in getnetbyname().
3308 if (isxdigit(*cp) || *cp == ':') {
3309 memset(&hints, 0, sizeof hints);
3310 /* Ensure the mask and the network have the same family. */
3311 if (maskflg && (opt_flags & OP_NET))
3312 hints.ai_family = net->nt_net.ss_family;
3313 else if (!maskflg && (opt_flags & OP_HAVEMASK))
3314 hints.ai_family = net->nt_mask.ss_family;
3316 hints.ai_family = AF_UNSPEC;
3317 hints.ai_flags = AI_NUMERICHOST;
3318 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
3320 if (sa != NULL && ai->ai_family == AF_INET) {
3322 * The address in `cp' is really a network address, so
3323 * use inet_network() to re-interpret this correctly.
3324 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
3326 bzero(&sin, sizeof sin);
3327 sin.sin_family = AF_INET;
3328 sin.sin_len = sizeof sin;
3329 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
3331 fprintf(stderr, "get_net: v4 addr %s\n",
3332 inet_ntoa(sin.sin_addr));
3333 sa = (struct sockaddr *)&sin;
3336 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
3337 bzero(&sin, sizeof sin);
3338 sin.sin_family = AF_INET;
3339 sin.sin_len = sizeof sin;
3340 sin.sin_addr = inet_makeaddr(np->n_net, 0);
3341 sa = (struct sockaddr *)&sin;
3347 /* The specified sockaddr is a mask. */
3348 if (checkmask(sa) != 0)
3350 bcopy(sa, &net->nt_mask, sa->sa_len);
3351 opt_flags |= OP_HAVEMASK;
3353 /* The specified sockaddr is a network address. */
3354 bcopy(sa, &net->nt_net, sa->sa_len);
3356 /* Get a network name for the export list. */
3359 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
3360 NULL, 0, NI_NUMERICHOST) == 0) {
3365 if ((net->nt_name = strdup(name)) == NULL)
3369 * Extract a mask from either a "/<masklen>" suffix, or
3370 * from the class of an IPv4 address.
3372 if (opt_flags & OP_MASKLEN) {
3373 preflen = strtol(prefp, NULL, 10);
3374 if (preflen < 0L || preflen == LONG_MAX)
3376 bcopy(sa, &net->nt_mask, sa->sa_len);
3377 if (makemask(&net->nt_mask, (int)preflen) != 0)
3379 opt_flags |= OP_HAVEMASK;
3381 } else if (sa->sa_family == AF_INET &&
3382 (opt_flags & OP_MASK) == 0) {
3385 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
3386 if (IN_CLASSA(addr))
3388 else if (IN_CLASSB(addr))
3390 else if (IN_CLASSC(addr))
3392 else if (IN_CLASSD(addr))
3395 preflen = 32; /* XXX */
3397 bcopy(sa, &net->nt_mask, sa->sa_len);
3398 makemask(&net->nt_mask, (int)preflen);
3399 opt_flags |= OP_HAVEMASK;
3414 * Parse out the next white space separated field
3417 nextfield(char **cp, char **endcp)
3423 while (*p == ' ' || *p == '\t')
3426 while (*p != '\0') {
3431 if (*p == '\\' && *(p + 1) != '\0')
3433 else if (*p == '\'' || *p == '"')
3435 else if (*p == ' ' || *p == '\t')
3444 * Get an exports file line. Skip over blank lines and handle line
3452 int totlen, cont_line;
3455 * Loop around ignoring blank lines and getting all continuation lines.
3460 if ((p = fgetln(exp_file, &len)) == NULL)
3465 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
3475 if (linesize < len + totlen + 1) {
3476 linesize = len + totlen + 1;
3477 line = realloc(line, linesize);
3481 memcpy(line + totlen, p, len);
3483 line[totlen] = '\0';
3484 } while (totlen == 0 || cont_line);
3489 * Parse a description of a credential.
3492 parsecred(char *namelist, struct xucred *cr)
3499 gid_t groups[XU_NGROUPS + 1];
3502 cr->cr_version = XUCRED_VERSION;
3504 * Set up the unprivileged user.
3507 cr->cr_groups[0] = 65533;
3510 * Get the user's password table entry.
3513 name = strsep_quote(&names, ":");
3514 /* Bug? name could be NULL here */
3515 if (isdigit(*name) || *name == '-')
3516 pw = getpwuid(atoi(name));
3518 pw = getpwnam(name);
3520 * Credentials specified as those of a user.
3522 if (names == NULL) {
3524 syslog(LOG_ERR, "unknown user: %s", name);
3527 cr->cr_uid = pw->pw_uid;
3528 ngroups = XU_NGROUPS + 1;
3529 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) {
3530 syslog(LOG_ERR, "too many groups");
3531 ngroups = XU_NGROUPS + 1;
3535 * Compress out duplicate.
3537 cr->cr_groups[0] = groups[0];
3538 if (ngroups > 1 && groups[0] == groups[1]) {
3539 cr->cr_ngroups = ngroups - 1;
3540 for (cnt = 2; cnt < ngroups; cnt++)
3541 cr->cr_groups[cnt - 1] = groups[cnt];
3543 cr->cr_ngroups = ngroups;
3544 if (cr->cr_ngroups > XU_NGROUPS)
3545 cr->cr_ngroups = XU_NGROUPS;
3546 for (cnt = 1; cnt < cr->cr_ngroups; cnt++)
3547 cr->cr_groups[cnt] = groups[cnt];
3552 * Explicit credential specified as a colon separated list:
3556 cr->cr_uid = pw->pw_uid;
3557 else if (isdigit(*name) || *name == '-')
3558 cr->cr_uid = atoi(name);
3560 syslog(LOG_ERR, "unknown user: %s", name);
3564 while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) {
3565 name = strsep_quote(&names, ":");
3566 if (isdigit(*name) || *name == '-') {
3567 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
3569 if ((gr = getgrnam(name)) == NULL) {
3570 syslog(LOG_ERR, "unknown group: %s", name);
3573 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
3576 if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS)
3577 syslog(LOG_ERR, "too many groups");
3580 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
3582 * Routines that maintain the remote mounttab
3587 struct mountlist *mlp;
3588 char *host, *dirp, *cp;
3592 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
3593 if (errno == ENOENT)
3596 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
3600 while (fgets(str, STRSIZ, mlfile) != NULL) {
3602 host = strsep(&cp, " \t\n");
3603 dirp = strsep(&cp, " \t\n");
3604 if (host == NULL || dirp == NULL)
3606 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3607 if (mlp == (struct mountlist *)NULL)
3609 strncpy(mlp->ml_host, host, MNTNAMLEN);
3610 mlp->ml_host[MNTNAMLEN] = '\0';
3611 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3612 mlp->ml_dirp[MNTPATHLEN] = '\0';
3614 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3620 del_mlist(char *hostp, char *dirp)
3622 struct mountlist *mlp, *mlp2;
3626 SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
3627 if (!strcmp(mlp->ml_host, hostp) &&
3628 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3630 SLIST_REMOVE(&mlhead, mlp, mountlist, next);
3635 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3636 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3639 SLIST_FOREACH(mlp, &mlhead, next) {
3640 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3647 add_mlist(char *hostp, char *dirp)
3649 struct mountlist *mlp;
3652 SLIST_FOREACH(mlp, &mlhead, next) {
3653 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3657 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3658 if (mlp == (struct mountlist *)NULL)
3660 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3661 mlp->ml_host[MNTNAMLEN] = '\0';
3662 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3663 mlp->ml_dirp[MNTPATHLEN] = '\0';
3664 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3665 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3666 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3669 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3674 * Free up a group list.
3677 free_grp(struct grouplist *grp)
3679 if (grp->gr_type == GT_HOST) {
3680 if (grp->gr_ptr.gt_addrinfo != NULL)
3681 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3682 } else if (grp->gr_type == GT_NET) {
3683 if (grp->gr_ptr.gt_net.nt_name)
3684 free(grp->gr_ptr.gt_net.nt_name);
3691 SYSLOG(int pri, const char *fmt, ...)
3696 vfprintf(stderr, fmt, ap);
3702 * Check options for consistency.
3705 check_options(struct dirlist *dp)
3708 if (v4root_phase == 0 && dp == NULL)
3710 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3711 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3714 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3715 syslog(LOG_ERR, "-mask requires -network");
3718 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3719 syslog(LOG_ERR, "-network requires mask specification");
3722 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3723 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3726 if (v4root_phase > 0 &&
3728 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3729 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3732 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3733 syslog(LOG_ERR, "-alldirs has multiple directories");
3740 * Check an absolute directory path for any symbolic links. Return true
3743 check_dirpath(char *dirp)
3750 while (*cp && ret) {
3753 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3759 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3765 * Make a netmask according to the specified prefix length. The ss_family
3766 * and other non-address fields must be initialised before calling this.
3769 makemask(struct sockaddr_storage *ssp, int bitlen)
3774 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3776 if (bitlen > len * CHAR_BIT)
3779 for (i = 0; i < len; i++) {
3780 bits = MIN(CHAR_BIT, bitlen);
3781 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3788 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3789 * is acceptable (i.e. of the form 1...10....0).
3792 checkmask(struct sockaddr *sa)
3797 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3800 for (i = 0; i < len; i++)
3801 if (mask[i] != 0xff)
3804 if (~mask[i] & (u_char)(~mask[i] + 1))
3808 for (; i < len; i++)
3815 * Compare two sockaddrs according to a specified mask. Return zero if
3816 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3817 * If samask is NULL, perform a full comparison.
3820 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3822 unsigned char *p1, *p2, *mask;
3825 if (sa1->sa_family != sa2->sa_family ||
3826 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3827 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3830 switch (sa1->sa_family) {
3832 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3833 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3838 /* Simple binary comparison if no mask specified. */
3840 return (memcmp(p1, p2, len));
3842 /* Set up the mask, and do a mask-based comparison. */
3843 if (sa1->sa_family != samask->sa_family ||
3844 (mask = sa_rawaddr(samask, NULL)) == NULL)
3847 for (i = 0; i < len; i++)
3848 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3854 * Return a pointer to the part of the sockaddr that contains the
3855 * raw address, and set *nbytes to its length in bytes. Returns
3856 * NULL if the address family is unknown.
3859 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3863 switch (sa->sa_family) {
3865 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3866 p = &((struct sockaddr_in *)sa)->sin_addr;
3869 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
3870 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
3883 huphandler(int sig __unused)
3890 terminate(int sig __unused)
3892 pidfile_remove(pfh);
3893 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
3894 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);