2020-07-13 19:19:18 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2022-09-09 20:54:16 +03:00
|
|
|
* Copyright (c) 2020, 2022 by Delphix. All rights reserved.
|
2020-07-13 19:19:18 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libutil.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <libintl.h>
|
|
|
|
|
2021-05-15 14:00:05 +03:00
|
|
|
#include <libshare.h>
|
2020-07-13 19:19:18 +03:00
|
|
|
#include "libshare_impl.h"
|
|
|
|
#include "nfs.h"
|
|
|
|
|
|
|
|
#define _PATH_MOUNTDPID "/var/run/mountd.pid"
|
|
|
|
#define OPTSSIZE 1024
|
|
|
|
#define MAXLINESIZE (PATH_MAX + OPTSSIZE)
|
|
|
|
#define ZFS_EXPORTS_FILE "/etc/zfs/exports"
|
|
|
|
#define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock"
|
|
|
|
|
|
|
|
/*
|
2022-02-28 14:55:07 +03:00
|
|
|
* This function translates options to a format acceptable by exports(5), eg.
|
2020-07-13 19:19:18 +03:00
|
|
|
*
|
|
|
|
* -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
|
|
|
|
* zfs.freebsd.org 69.147.83.54
|
|
|
|
*
|
|
|
|
* Accepted input formats:
|
|
|
|
*
|
|
|
|
* ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,zfs.freebsd.org
|
|
|
|
* ro network=192.168.0.0 mask=255.255.255.0 maproot=0 zfs.freebsd.org
|
|
|
|
* -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,zfs.freebsd.org
|
|
|
|
* -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 \
|
|
|
|
* zfs.freebsd.org
|
|
|
|
*
|
|
|
|
* Recognized keywords:
|
|
|
|
*
|
|
|
|
* ro, maproot, mapall, mask, network, sec, alldirs, public, webnfs,
|
|
|
|
* index, quiet
|
|
|
|
*/
|
2022-02-28 14:55:07 +03:00
|
|
|
static int
|
|
|
|
translate_opts(const char *shareopts, FILE *out)
|
2020-07-13 19:19:18 +03:00
|
|
|
{
|
2022-02-28 14:40:14 +03:00
|
|
|
static const char *const known_opts[] = { "ro", "maproot", "mapall",
|
|
|
|
"mask", "network", "sec", "alldirs", "public", "webnfs", "index",
|
|
|
|
"quiet" };
|
2022-02-28 14:55:07 +03:00
|
|
|
char oldopts[OPTSSIZE], newopts[OPTSSIZE];
|
2020-07-13 19:19:18 +03:00
|
|
|
char *o, *s = NULL;
|
|
|
|
unsigned int i;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
strlcpy(oldopts, shareopts, sizeof (oldopts));
|
|
|
|
newopts[0] = '\0';
|
|
|
|
s = oldopts;
|
|
|
|
while ((o = strsep(&s, "-, ")) != NULL) {
|
|
|
|
if (o[0] == '\0')
|
|
|
|
continue;
|
2022-02-28 14:40:14 +03:00
|
|
|
for (i = 0; i < ARRAY_SIZE(known_opts); ++i) {
|
2020-07-13 19:19:18 +03:00
|
|
|
len = strlen(known_opts[i]);
|
|
|
|
if (strncmp(known_opts[i], o, len) == 0 &&
|
|
|
|
(o[len] == '\0' || o[len] == '=')) {
|
|
|
|
strlcat(newopts, "-", sizeof (newopts));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strlcat(newopts, o, sizeof (newopts));
|
|
|
|
strlcat(newopts, " ", sizeof (newopts));
|
|
|
|
}
|
2022-02-28 14:55:07 +03:00
|
|
|
return (fputs(newopts, out));
|
2020-07-13 19:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-05-17 19:13:18 +03:00
|
|
|
nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
|
2020-07-13 19:19:18 +03:00
|
|
|
{
|
2022-02-28 16:50:28 +03:00
|
|
|
const char *shareopts = impl_share->sa_shareopts;
|
2020-07-13 19:19:18 +03:00
|
|
|
if (strcmp(shareopts, "on") == 0)
|
|
|
|
shareopts = "";
|
|
|
|
|
2022-02-28 22:42:22 +03:00
|
|
|
boolean_t need_free;
|
|
|
|
char *mp;
|
|
|
|
int rc = nfs_escape_mountpoint(impl_share->sa_mountpoint, &mp,
|
|
|
|
&need_free);
|
|
|
|
if (rc != SA_OK)
|
|
|
|
return (rc);
|
|
|
|
|
|
|
|
if (fputs(mp, tmpfile) == EOF ||
|
2022-02-28 14:55:07 +03:00
|
|
|
fputc('\t', tmpfile) == EOF ||
|
|
|
|
translate_opts(shareopts, tmpfile) == EOF ||
|
|
|
|
fputc('\n', tmpfile) == EOF) {
|
2021-05-17 19:13:18 +03:00
|
|
|
fprintf(stderr, "failed to write to temporary file\n");
|
2022-02-28 22:42:22 +03:00
|
|
|
rc = SA_SYSTEM_ERR;
|
2020-07-13 19:19:18 +03:00
|
|
|
}
|
2021-04-16 01:40:22 +03:00
|
|
|
|
2022-02-28 22:42:22 +03:00
|
|
|
if (need_free)
|
|
|
|
free(mp);
|
|
|
|
return (rc);
|
2021-04-16 01:40:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfs_enable_share(sa_share_impl_t impl_share)
|
|
|
|
{
|
|
|
|
return (nfs_toggle_share(
|
|
|
|
ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share,
|
|
|
|
nfs_enable_share_impl));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-05-17 19:13:18 +03:00
|
|
|
nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
|
2021-04-16 01:40:22 +03:00
|
|
|
{
|
2021-12-12 17:26:39 +03:00
|
|
|
(void) impl_share, (void) tmpfile;
|
2021-04-16 01:40:22 +03:00
|
|
|
return (SA_OK);
|
2020-07-13 19:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfs_disable_share(sa_share_impl_t impl_share)
|
|
|
|
{
|
2021-04-16 01:40:22 +03:00
|
|
|
return (nfs_toggle_share(
|
|
|
|
ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, NULL, impl_share,
|
|
|
|
nfs_disable_share_impl));
|
2020-07-13 19:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static boolean_t
|
|
|
|
nfs_is_shared(sa_share_impl_t impl_share)
|
|
|
|
{
|
2021-05-17 21:25:29 +03:00
|
|
|
return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share));
|
2020-07-13 19:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfs_validate_shareopts(const char *shareopts)
|
|
|
|
{
|
2022-02-16 15:09:27 +03:00
|
|
|
(void) shareopts;
|
2020-07-13 19:19:18 +03:00
|
|
|
return (SA_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Commit the shares by restarting mountd.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
nfs_commit_shares(void)
|
|
|
|
{
|
|
|
|
struct pidfh *pfh;
|
|
|
|
pid_t mountdpid;
|
|
|
|
|
2021-05-17 19:03:26 +03:00
|
|
|
start:
|
2020-07-13 19:19:18 +03:00
|
|
|
pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
|
|
|
|
if (pfh != NULL) {
|
2021-05-17 19:03:26 +03:00
|
|
|
/* mountd(8) is not running. */
|
2020-07-13 19:19:18 +03:00
|
|
|
pidfile_remove(pfh);
|
|
|
|
return (SA_OK);
|
|
|
|
}
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
/* Cannot open pidfile for some reason. */
|
|
|
|
return (SA_SYSTEM_ERR);
|
|
|
|
}
|
2021-05-17 19:03:26 +03:00
|
|
|
if (mountdpid == -1) {
|
|
|
|
/* mountd(8) exists, but didn't write the PID yet */
|
|
|
|
usleep(500);
|
|
|
|
goto start;
|
|
|
|
}
|
2020-07-13 19:19:18 +03:00
|
|
|
/* We have mountd(8) PID in mountdpid variable. */
|
|
|
|
kill(mountdpid, SIGHUP);
|
|
|
|
return (SA_OK);
|
|
|
|
}
|
|
|
|
|
2022-09-09 20:54:16 +03:00
|
|
|
static void
|
|
|
|
nfs_truncate_shares(void)
|
|
|
|
{
|
|
|
|
nfs_reset_shares(ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE);
|
|
|
|
}
|
|
|
|
|
2022-02-28 17:46:25 +03:00
|
|
|
const sa_fstype_t libshare_nfs_type = {
|
2020-07-13 19:19:18 +03:00
|
|
|
.enable_share = nfs_enable_share,
|
|
|
|
.disable_share = nfs_disable_share,
|
|
|
|
.is_shared = nfs_is_shared,
|
|
|
|
|
|
|
|
.validate_shareopts = nfs_validate_shareopts,
|
|
|
|
.commit_shares = nfs_commit_shares,
|
2022-09-09 20:54:16 +03:00
|
|
|
.truncate_shares = nfs_truncate_shares,
|
2020-07-13 19:19:18 +03:00
|
|
|
};
|