2021-04-11 17:37:48 +00:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2021-04-11 17:42:07 +00:00
|
|
|
#include <sys/file.h>
|
2021-04-11 17:37:48 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2021-04-11 17:42:07 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <libshare.h>
|
2021-04-11 17:37:48 +00:00
|
|
|
#include "nfs.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int nfs_lock_fd = -1;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nfs_exports_[lock|unlock] are used to guard against conconcurrent
|
|
|
|
* updates to the exports file. Each protocol is responsible for
|
|
|
|
* providing the necessary locking to ensure consistency.
|
|
|
|
*/
|
2021-04-15 22:40:22 +00:00
|
|
|
static int
|
2021-04-11 17:37:48 +00:00
|
|
|
nfs_exports_lock(const char *name)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
nfs_lock_fd = open(name, O_RDWR | O_CREAT | O_CLOEXEC, 0600);
|
|
|
|
if (nfs_lock_fd == -1) {
|
|
|
|
err = errno;
|
|
|
|
fprintf(stderr, "failed to lock %s: %s\n", name, strerror(err));
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
2021-05-17 16:08:35 +00:00
|
|
|
while ((err = flock(nfs_lock_fd, LOCK_EX)) != 0 && errno == EINTR)
|
|
|
|
;
|
|
|
|
if (err != 0) {
|
2021-04-11 17:37:48 +00:00
|
|
|
err = errno;
|
|
|
|
fprintf(stderr, "failed to lock %s: %s\n", name, strerror(err));
|
|
|
|
(void) close(nfs_lock_fd);
|
|
|
|
nfs_lock_fd = -1;
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2021-04-15 22:40:22 +00:00
|
|
|
static void
|
2021-04-11 17:37:48 +00:00
|
|
|
nfs_exports_unlock(const char *name)
|
|
|
|
{
|
|
|
|
verify(nfs_lock_fd > 0);
|
|
|
|
|
|
|
|
if (flock(nfs_lock_fd, LOCK_UN) != 0) {
|
|
|
|
fprintf(stderr, "failed to unlock %s: %s\n",
|
|
|
|
name, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) close(nfs_lock_fd);
|
|
|
|
nfs_lock_fd = -1;
|
|
|
|
}
|
2021-04-11 17:42:07 +00:00
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
struct tmpfile {
|
|
|
|
/*
|
|
|
|
* This only needs to be as wide as ZFS_EXPORTS_FILE and mktemp suffix,
|
|
|
|
* 64 is more than enough.
|
|
|
|
*/
|
|
|
|
char name[64];
|
|
|
|
FILE *fp;
|
|
|
|
};
|
|
|
|
|
|
|
|
static boolean_t
|
|
|
|
nfs_init_tmpfile(const char *prefix, const char *mdir, struct tmpfile *tmpf)
|
2021-04-11 17:42:07 +00:00
|
|
|
{
|
|
|
|
if (mdir != NULL &&
|
2021-05-18 16:00:26 +00:00
|
|
|
mkdir(mdir, 0755) < 0 &&
|
|
|
|
errno != EEXIST) {
|
2021-04-11 17:42:07 +00:00
|
|
|
fprintf(stderr, "failed to create %s: %s\n",
|
|
|
|
mdir, strerror(errno));
|
2021-05-17 16:13:18 +00:00
|
|
|
return (B_FALSE);
|
2021-04-11 17:42:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
strcpy(tmpf->name, prefix);
|
|
|
|
strcat(tmpf->name, ".XXXXXXXX");
|
2021-04-11 17:42:07 +00:00
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
int fd = mkostemp(tmpf->name, O_CLOEXEC);
|
2021-04-11 17:42:07 +00:00
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Unable to create temporary file: %s",
|
|
|
|
strerror(errno));
|
2021-05-17 16:13:18 +00:00
|
|
|
return (B_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpf->fp = fdopen(fd, "w+");
|
|
|
|
if (tmpf->fp == NULL) {
|
|
|
|
fprintf(stderr, "Unable to reopen temporary file: %s",
|
|
|
|
strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return (B_FALSE);
|
2021-04-11 17:42:07 +00:00
|
|
|
}
|
2021-05-17 16:13:18 +00:00
|
|
|
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nfs_abort_tmpfile(struct tmpfile *tmpf)
|
|
|
|
{
|
|
|
|
unlink(tmpf->name);
|
|
|
|
fclose(tmpf->fp);
|
2021-04-11 17:42:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 22:40:22 +00:00
|
|
|
static int
|
2021-05-17 16:13:18 +00:00
|
|
|
nfs_fini_tmpfile(const char *exports, struct tmpfile *tmpf)
|
2021-04-11 17:42:07 +00:00
|
|
|
{
|
2021-05-17 16:13:18 +00:00
|
|
|
if (fflush(tmpf->fp) != 0) {
|
|
|
|
fprintf(stderr, "Failed to write to temporary file: %s\n",
|
2021-04-11 17:42:07 +00:00
|
|
|
strerror(errno));
|
2021-05-17 16:13:18 +00:00
|
|
|
nfs_abort_tmpfile(tmpf);
|
|
|
|
return (SA_SYSTEM_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rename(tmpf->name, exports) == -1) {
|
|
|
|
fprintf(stderr, "Unable to rename %s -> %s: %s\n",
|
|
|
|
tmpf->name, exports, strerror(errno));
|
|
|
|
nfs_abort_tmpfile(tmpf);
|
2021-04-11 17:42:07 +00:00
|
|
|
return (SA_SYSTEM_ERR);
|
|
|
|
}
|
2021-05-17 16:13:18 +00:00
|
|
|
|
2021-05-17 19:08:16 +00:00
|
|
|
(void) fchmod(fileno(tmpf->fp), 0644);
|
2021-05-17 16:13:18 +00:00
|
|
|
fclose(tmpf->fp);
|
2021-04-11 17:42:07 +00:00
|
|
|
return (SA_OK);
|
|
|
|
}
|
|
|
|
|
2021-05-17 17:56:48 +00:00
|
|
|
static int
|
2021-05-17 18:25:29 +00:00
|
|
|
nfs_process_exports(const char *exports, const char *mountpoint,
|
|
|
|
boolean_t (*cbk)(void *userdata, char *line, boolean_t found_mountpoint),
|
|
|
|
void *userdata)
|
2021-05-17 17:56:48 +00:00
|
|
|
{
|
|
|
|
int error = SA_OK;
|
2021-05-17 18:25:29 +00:00
|
|
|
boolean_t cont = B_TRUE;
|
2021-05-17 17:56:48 +00:00
|
|
|
|
|
|
|
FILE *oldfp = fopen(exports, "re");
|
|
|
|
if (oldfp != NULL) {
|
|
|
|
char *buf = NULL, *sep;
|
|
|
|
size_t buflen = 0, mplen = strlen(mountpoint);
|
|
|
|
|
2021-05-17 18:25:29 +00:00
|
|
|
while (cont && getline(&buf, &buflen, oldfp) != -1) {
|
2021-05-17 17:56:48 +00:00
|
|
|
if (buf[0] == '\n' || buf[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
2021-05-17 18:25:29 +00:00
|
|
|
cont = cbk(userdata, buf,
|
|
|
|
(sep = strpbrk(buf, "\t \n")) != NULL &&
|
2021-05-17 17:56:48 +00:00
|
|
|
sep - buf == mplen &&
|
2021-05-17 18:25:29 +00:00
|
|
|
strncmp(buf, mountpoint, mplen) == 0);
|
2021-05-17 17:56:48 +00:00
|
|
|
}
|
2021-05-17 18:25:29 +00:00
|
|
|
free(buf);
|
2021-05-17 17:56:48 +00:00
|
|
|
|
|
|
|
if (ferror(oldfp) != 0)
|
|
|
|
error = ferror(oldfp);
|
|
|
|
|
|
|
|
if (fclose(oldfp) != 0) {
|
|
|
|
fprintf(stderr, "Unable to close file %s: %s\n",
|
|
|
|
exports, strerror(errno));
|
|
|
|
error = error != SA_OK ? error : SA_SYSTEM_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 18:25:29 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean_t
|
|
|
|
nfs_copy_entries_cb(void *userdata, char *line, boolean_t found_mountpoint)
|
|
|
|
{
|
|
|
|
FILE *newfp = userdata;
|
|
|
|
if (!found_mountpoint)
|
|
|
|
fputs(line, newfp);
|
|
|
|
return (B_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy all entries from the exports file (if it exists) to newfp,
|
|
|
|
* omitting any entries for the specified mountpoint.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
nfs_copy_entries(FILE *newfp, const char *exports, const char *mountpoint)
|
|
|
|
{
|
|
|
|
fputs(FILE_HEADER, newfp);
|
|
|
|
|
|
|
|
int error = nfs_process_exports(
|
|
|
|
exports, mountpoint, nfs_copy_entries_cb, newfp);
|
|
|
|
|
2021-05-17 17:56:48 +00:00
|
|
|
if (error == SA_OK && ferror(newfp) != 0)
|
|
|
|
error = ferror(newfp);
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2021-06-03 21:50:07 +00:00
|
|
|
int
|
2021-04-15 22:40:22 +00:00
|
|
|
nfs_toggle_share(const char *lockfile, const char *exports,
|
|
|
|
const char *expdir, sa_share_impl_t impl_share,
|
2021-05-17 16:13:18 +00:00
|
|
|
int(*cbk)(sa_share_impl_t impl_share, FILE *tmpfile))
|
2021-04-11 17:42:07 +00:00
|
|
|
{
|
|
|
|
int error;
|
2021-05-17 16:13:18 +00:00
|
|
|
struct tmpfile tmpf;
|
2021-04-11 17:42:07 +00:00
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
if (!nfs_init_tmpfile(exports, expdir, &tmpf))
|
2021-04-11 17:42:07 +00:00
|
|
|
return (SA_SYSTEM_ERR);
|
|
|
|
|
|
|
|
error = nfs_exports_lock(lockfile);
|
|
|
|
if (error != 0) {
|
2021-05-17 16:13:18 +00:00
|
|
|
nfs_abort_tmpfile(&tmpf);
|
2021-04-11 17:42:07 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2021-05-17 17:56:48 +00:00
|
|
|
error = nfs_copy_entries(tmpf.fp, exports, impl_share->sa_mountpoint);
|
2021-04-15 22:40:22 +00:00
|
|
|
if (error != SA_OK)
|
|
|
|
goto fullerr;
|
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
error = cbk(impl_share, tmpf.fp);
|
2021-04-15 22:40:22 +00:00
|
|
|
if (error != SA_OK)
|
|
|
|
goto fullerr;
|
2021-04-11 17:42:07 +00:00
|
|
|
|
2021-05-17 16:13:18 +00:00
|
|
|
error = nfs_fini_tmpfile(exports, &tmpf);
|
2021-04-11 17:42:07 +00:00
|
|
|
nfs_exports_unlock(lockfile);
|
|
|
|
return (error);
|
2021-04-15 22:40:22 +00:00
|
|
|
|
|
|
|
fullerr:
|
2021-05-17 16:13:18 +00:00
|
|
|
nfs_abort_tmpfile(&tmpf);
|
2021-04-15 22:40:22 +00:00
|
|
|
nfs_exports_unlock(lockfile);
|
|
|
|
return (error);
|
2021-04-11 17:42:07 +00:00
|
|
|
}
|
2021-05-17 18:25:29 +00:00
|
|
|
|
|
|
|
static boolean_t
|
|
|
|
nfs_is_shared_cb(void *userdata, char *line, boolean_t found_mountpoint)
|
|
|
|
{
|
2021-12-12 14:26:39 +00:00
|
|
|
(void) line;
|
|
|
|
|
2021-05-17 18:25:29 +00:00
|
|
|
boolean_t *found = userdata;
|
|
|
|
*found = found_mountpoint;
|
|
|
|
return (!found_mountpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean_t
|
|
|
|
nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share)
|
|
|
|
{
|
|
|
|
boolean_t found = B_FALSE;
|
|
|
|
nfs_process_exports(exports, impl_share->sa_mountpoint,
|
|
|
|
nfs_is_shared_cb, &found);
|
|
|
|
return (found);
|
|
|
|
}
|