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