1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
// SPDX-License-Identifier: CDDL-1.0
/*
* 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 https://opensource.org/licenses/CDDL-1.0.
* 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
*/
/*
* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011 Gunnar Beutner
* Copyright (c) 2012 Cyril Plisko. All rights reserved.
* Copyright (c) 2019, 2022 by Delphix. All rights reserved.
*/
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <libzfs.h>
#include <libshare.h>
#include "libshare_impl.h"
#include "nfs.h"
#define ZFS_EXPORTS_DIR "/etc/exports.d"
#define ZFS_EXPORTS_FILE ZFS_EXPORTS_DIR"/zfs.exports"
#define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock"
static boolean_t nfs_available(void);
static boolean_t exports_available(void);
typedef int (*nfs_shareopt_callback_t)(const char *opt, const char *value,
void *cookie);
typedef int (*nfs_host_callback_t)(FILE *tmpfile, const char *sharepath,
const char *host, const char *security, const char *access, void *cookie);
/*
* Invokes the specified callback function for each Solaris share option
* listed in the specified string.
*/
static int
foreach_nfs_shareopt(const char *shareopts,
nfs_shareopt_callback_t callback, void *cookie)
{
char *shareopts_dup, *opt, *cur, *value;
int was_nul, error;
if (shareopts == NULL)
return (SA_OK);
if (strcmp(shareopts, "on") == 0)
shareopts = "rw,crossmnt";
shareopts_dup = strdup(shareopts);
if (shareopts_dup == NULL)
return (SA_NO_MEMORY);
opt = shareopts_dup;
was_nul = 0;
while (1) {
cur = opt;
while (*cur != ',' && *cur != '\0')
cur++;
if (*cur == '\0')
was_nul = 1;
*cur = '\0';
if (cur > opt) {
value = strchr(opt, '=');
if (value != NULL) {
*value = '\0';
value++;
}
error = callback(opt, value, cookie);
if (error != SA_OK) {
free(shareopts_dup);
return (error);
}
}
opt = cur + 1;
if (was_nul)
break;
}
free(shareopts_dup);
return (SA_OK);
}
typedef struct nfs_host_cookie_s {
nfs_host_callback_t callback;
const char *sharepath;
void *cookie;
FILE *tmpfile;
const char *security;
} nfs_host_cookie_t;
/*
* Helper function for foreach_nfs_host. This function checks whether the
* current share option is a host specification and invokes a callback
* function with information about the host.
*/
static int
foreach_nfs_host_cb(const char *opt, const char *value, void *pcookie)
{
int error;
const char *access;
char *host_dup, *host, *next, *v6Literal;
nfs_host_cookie_t *udata = (nfs_host_cookie_t *)pcookie;
int cidr_len;
#ifdef DEBUG
fprintf(stderr, "foreach_nfs_host_cb: key=%s, value=%s\n", opt, value);
#endif
if (strcmp(opt, "sec") == 0)
udata->security = value;
if (strcmp(opt, "rw") == 0 || strcmp(opt, "ro") == 0) {
if (value == NULL)
value = "*";
access = opt;
host_dup = strdup(value);
if (host_dup == NULL)
return (SA_NO_MEMORY);
host = host_dup;
do {
if (*host == '[') {
host++;
v6Literal = strchr(host, ']');
if (v6Literal == NULL) {
free(host_dup);
return (SA_SYNTAX_ERR);
}
if (v6Literal[1] == '\0') {
*v6Literal = '\0';
next = NULL;
} else if (v6Literal[1] == '/') {
next = strchr(v6Literal + 2, ':');
if (next == NULL) {
cidr_len =
strlen(v6Literal + 1);
memmove(v6Literal,
v6Literal + 1,
cidr_len);
v6Literal[cidr_len] = '\0';
} else {
cidr_len = next - v6Literal - 1;
memmove(v6Literal,
v6Literal + 1,
cidr_len);
v6Literal[cidr_len] = '\0';
next++;
}
} else if (v6Literal[1] == ':') {
*v6Literal = '\0';
next = v6Literal + 2;
} else {
free(host_dup);
return (SA_SYNTAX_ERR);
}
} else {
next = strchr(host, ':');
if (next != NULL) {
*next = '\0';
next++;
}
}
error = udata->callback(udata->tmpfile,
udata->sharepath, host, udata->security,
access, udata->cookie);
if (error != SA_OK) {
free(host_dup);
return (error);
}
host = next;
} while (host != NULL);
free(host_dup);
}
return (SA_OK);
}
/*
* Invokes a callback function for all NFS hosts that are set for a share.
*/
static int
foreach_nfs_host(sa_share_impl_t impl_share, FILE *tmpfile,
nfs_host_callback_t callback, void *cookie)
{
nfs_host_cookie_t udata;
udata.callback = callback;
udata.sharepath = impl_share->sa_mountpoint;
udata.cookie = cookie;
udata.tmpfile = tmpfile;
udata.security = "sys";
return (foreach_nfs_shareopt(impl_share->sa_shareopts,
foreach_nfs_host_cb, &udata));
}
/*
* Converts a Solaris NFS host specification to its Linux equivalent.
*/
static const char *
get_linux_hostspec(const char *solaris_hostspec)
{
/*
* For now we just support CIDR masks (e.g. @192.168.0.0/16) and host
* wildcards (e.g. *.example.org).
*/
if (solaris_hostspec[0] == '@') {
/*
* Solaris host specifier, e.g. @192.168.0.0/16; we just need
* to skip the @ in this case
*/
return (solaris_hostspec + 1);
} else {
return (solaris_hostspec);
}
}
/*
* Adds a Linux share option to an array of NFS options.
*/
static int
add_linux_shareopt(char **plinux_opts, const char *key, const char *value)
{
size_t len = 0;
char *new_linux_opts;
if (*plinux_opts != NULL)
len = strlen(*plinux_opts);
new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) +
(value ? 1 + strlen(value) : 0) + 1);
if (new_linux_opts == NULL)
return (SA_NO_MEMORY);
new_linux_opts[len] = '\0';
if (len > 0)
strcat(new_linux_opts, ",");
strcat(new_linux_opts, key);
if (value != NULL) {
strcat(new_linux_opts, "=");
strcat(new_linux_opts, value);
}
*plinux_opts = new_linux_opts;
return (SA_OK);
}
static int string_cmp(const void *lhs, const void *rhs) {
const char *const *l = lhs, *const *r = rhs;
return (strcmp(*l, *r));
}
/*
* Validates and converts a single Solaris share option to its Linux
* equivalent.
*/
static int
get_linux_shareopts_cb(const char *key, const char *value, void *cookie)
{
/* This list must remain sorted, since we bsearch() it */
static const char *const valid_keys[] = { "all_squash", "anongid",
"anonuid", "async", "auth_nlm", "crossmnt", "fsid", "fsuid", "hide",
"insecure", "insecure_locks", "mountpoint", "mp", "no_acl",
"no_all_squash", "no_auth_nlm", "no_root_squash",
"no_subtree_check", "no_wdelay", "nohide", "refer", "replicas",
"root_squash", "secure", "secure_locks", "subtree_check", "sync",
"wdelay" };
char **plinux_opts = (char **)cookie;
char *host, *val_dup, *literal, *next;
if (strcmp(key, "sec") == 0)
return (SA_OK);
if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0) {
if (value == NULL || strlen(value) == 0)
return (SA_OK);
val_dup = strdup(value);
host = val_dup;
if (host == NULL)
return (SA_NO_MEMORY);
do {
if (*host == '[') {
host++;
literal = strchr(host, ']');
if (literal == NULL) {
free(val_dup);
return (SA_SYNTAX_ERR);
}
if (literal[1] == '\0')
next = NULL;
else if (literal[1] == '/') {
next = strchr(literal + 2, ':');
if (next != NULL)
++next;
} else if (literal[1] == ':')
next = literal + 2;
else {
free(val_dup);
return (SA_SYNTAX_ERR);
}
} else {
next = strchr(host, ':');
if (next != NULL)
++next;
}
host = next;
} while (host != NULL);
free(val_dup);
return (SA_OK);
}
if (strcmp(key, "anon") == 0)
key = "anonuid";
if (strcmp(key, "root_mapping") == 0) {
(void) add_linux_shareopt(plinux_opts, "root_squash", NULL);
key = "anonuid";
}
if (strcmp(key, "nosub") == 0)
key = "subtree_check";
if (bsearch(&key, valid_keys, ARRAY_SIZE(valid_keys),
sizeof (*valid_keys), string_cmp) == NULL)
return (SA_SYNTAX_ERR);
(void) add_linux_shareopt(plinux_opts, key, value);
return (SA_OK);
}
/*
* Takes a string containing Solaris share options (e.g. "sync,no_acl") and
* converts them to a NULL-terminated array of Linux NFS options.
*/
static int
get_linux_shareopts(const char *shareopts, char **plinux_opts)
{
int error;
assert(plinux_opts != NULL);
*plinux_opts = NULL;
/* no_subtree_check - Default as of nfs-utils v1.1.0 */
(void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL);
/* mountpoint - Restrict exports to ZFS mountpoints */
(void) add_linux_shareopt(plinux_opts, "mountpoint", NULL);
error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb,
plinux_opts);
if (error != SA_OK) {
free(*plinux_opts);
*plinux_opts = NULL;
}
return (error);
}
/*
* This function populates an entry into /etc/exports.d/zfs.exports.
* This file is consumed by the linux nfs server so that zfs shares are
* automatically exported upon boot or whenever the nfs server restarts.
*/
static int
nfs_add_entry(FILE *tmpfile, const char *sharepath,
const char *host, const char *security, const char *access_opts,
void *pcookie)
{
const char *linux_opts = (const char *)pcookie;
if (linux_opts == NULL)
linux_opts = "";
boolean_t need_free;
char *mp;
int rc = nfs_escape_mountpoint(sharepath, &mp, &need_free);
if (rc != SA_OK)
return (rc);
if (fprintf(tmpfile, "%s %s(sec=%s,%s,%s)\n", mp,
get_linux_hostspec(host), security, access_opts,
linux_opts) < 0) {
fprintf(stderr, "failed to write to temporary file\n");
rc = SA_SYSTEM_ERR;
}
if (need_free)
free(mp);
return (rc);
}
/*
* Enables NFS sharing for the specified share.
*/
static int
nfs_enable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
{
char *linux_opts = NULL;
int error = get_linux_shareopts(impl_share->sa_shareopts, &linux_opts);
if (error != SA_OK)
return (error);
error = foreach_nfs_host(impl_share, tmpfile, nfs_add_entry,
linux_opts);
free(linux_opts);
return (error);
}
static int
nfs_enable_share(sa_share_impl_t impl_share)
{
if (!nfs_available())
return (SA_SYSTEM_ERR);
return (nfs_toggle_share(
ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
nfs_enable_share_impl));
}
/*
* Disables NFS sharing for the specified share.
*/
static int
nfs_disable_share_impl(sa_share_impl_t impl_share, FILE *tmpfile)
{
(void) impl_share, (void) tmpfile;
return (SA_OK);
}
static int
nfs_disable_share(sa_share_impl_t impl_share)
{
if (!nfs_available())
return (SA_OK);
return (nfs_toggle_share(
ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share,
nfs_disable_share_impl));
}
static boolean_t
nfs_is_shared(sa_share_impl_t impl_share)
{
if (!nfs_available())
return (SA_SYSTEM_ERR);
return (nfs_is_shared_impl(ZFS_EXPORTS_FILE, impl_share));
}
/*
* Checks whether the specified NFS share options are syntactically correct.
*/
static int
nfs_validate_shareopts(const char *shareopts)
{
char *linux_opts = NULL;
if (strlen(shareopts) == 0)
return (SA_SYNTAX_ERR);
int error = get_linux_shareopts(shareopts, &linux_opts);
if (error != SA_OK)
return (error);
free(linux_opts);
return (SA_OK);
}
static int
nfs_commit_shares(void)
{
if (!nfs_available())
return (SA_SYSTEM_ERR);
char *argv[] = {
(char *)"/usr/sbin/exportfs",
(char *)"-ra",
NULL
};
return (libzfs_run_process(argv[0], argv, 0));
}
static void
nfs_truncate_shares(void)
{
if (!exports_available())
return;
nfs_reset_shares(ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE);
}
const sa_fstype_t libshare_nfs_type = {
.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,
.truncate_shares = nfs_truncate_shares,
};
static boolean_t
nfs_available(void)
{
static int avail;
if (!avail) {
if (access("/usr/sbin/exportfs", F_OK) != 0)
avail = -1;
else
avail = 1;
}
return (avail == 1);
}
static boolean_t
exports_available(void)
{
static int avail;
if (!avail) {
if (access(ZFS_EXPORTS_DIR, F_OK) != 0)
avail = -1;
else
avail = 1;
}
return (avail == 1);
}
|