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 http://www.opensolaris.org/os/licensing. 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, 2020 by Delphix. All rights reserved. 27 */ 28 29 #include <dirent.h> 30 #include <stdio.h> 31 #include <string.h> 32 #include <strings.h> 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <sys/file.h> 36 #include <sys/stat.h> 37 #include <sys/types.h> 38 #include <sys/wait.h> 39 #include <unistd.h> 40 #include <libzfs.h> 41 #include <libshare.h> 42 #include "libshare_impl.h" 43 #include "nfs.h" 44 45 #define ZFS_EXPORTS_DIR "/etc/exports.d" 46 #define ZFS_EXPORTS_FILE ZFS_EXPORTS_DIR"/zfs.exports" 47 #define ZFS_EXPORTS_LOCK ZFS_EXPORTS_FILE".lock" 48 49 static sa_fstype_t *nfs_fstype; 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)(const char *sharepath, const char *filename, 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 const char *filename; 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->filename, 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, char *filename, 230 nfs_host_callback_t callback, void *cookie) 231 { 232 nfs_host_cookie_t udata; 233 char *shareopts; 234 235 udata.callback = callback; 236 udata.sharepath = impl_share->sa_mountpoint; 237 udata.cookie = cookie; 238 udata.filename = filename; 239 udata.security = "sys"; 240 241 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; 242 243 return (foreach_nfs_shareopt(shareopts, foreach_nfs_host_cb, 244 &udata)); 245 } 246 247 /* 248 * Converts a Solaris NFS host specification to its Linux equivalent. 249 */ 250 static int 251 get_linux_hostspec(const char *solaris_hostspec, char **plinux_hostspec) 252 { 253 /* 254 * For now we just support CIDR masks (e.g. @192.168.0.0/16) and host 255 * wildcards (e.g. *.example.org). 256 */ 257 if (solaris_hostspec[0] == '@') { 258 /* 259 * Solaris host specifier, e.g. @192.168.0.0/16; we just need 260 * to skip the @ in this case 261 */ 262 *plinux_hostspec = strdup(solaris_hostspec + 1); 263 } else { 264 *plinux_hostspec = strdup(solaris_hostspec); 265 } 266 267 if (*plinux_hostspec == NULL) { 268 return (SA_NO_MEMORY); 269 } 270 271 return (SA_OK); 272 } 273 274 /* 275 * Adds a Linux share option to an array of NFS options. 276 */ 277 static int 278 add_linux_shareopt(char **plinux_opts, const char *key, const char *value) 279 { 280 size_t len = 0; 281 char *new_linux_opts; 282 283 if (*plinux_opts != NULL) 284 len = strlen(*plinux_opts); 285 286 new_linux_opts = realloc(*plinux_opts, len + 1 + strlen(key) + 287 (value ? 1 + strlen(value) : 0) + 1); 288 289 if (new_linux_opts == NULL) 290 return (SA_NO_MEMORY); 291 292 new_linux_opts[len] = '\0'; 293 294 if (len > 0) 295 strcat(new_linux_opts, ","); 296 297 strcat(new_linux_opts, key); 298 299 if (value != NULL) { 300 strcat(new_linux_opts, "="); 301 strcat(new_linux_opts, value); 302 } 303 304 *plinux_opts = new_linux_opts; 305 306 return (SA_OK); 307 } 308 309 /* 310 * Validates and converts a single Solaris share option to its Linux 311 * equivalent. 312 */ 313 static int 314 get_linux_shareopts_cb(const char *key, const char *value, void *cookie) 315 { 316 char **plinux_opts = (char **)cookie; 317 318 /* host-specific options, these are taken care of elsewhere */ 319 if (strcmp(key, "ro") == 0 || strcmp(key, "rw") == 0 || 320 strcmp(key, "sec") == 0) 321 return (SA_OK); 322 323 if (strcmp(key, "anon") == 0) 324 key = "anonuid"; 325 326 if (strcmp(key, "root_mapping") == 0) { 327 (void) add_linux_shareopt(plinux_opts, "root_squash", NULL); 328 key = "anonuid"; 329 } 330 331 if (strcmp(key, "nosub") == 0) 332 key = "subtree_check"; 333 334 if (strcmp(key, "insecure") != 0 && strcmp(key, "secure") != 0 && 335 strcmp(key, "async") != 0 && strcmp(key, "sync") != 0 && 336 strcmp(key, "no_wdelay") != 0 && strcmp(key, "wdelay") != 0 && 337 strcmp(key, "nohide") != 0 && strcmp(key, "hide") != 0 && 338 strcmp(key, "crossmnt") != 0 && 339 strcmp(key, "no_subtree_check") != 0 && 340 strcmp(key, "subtree_check") != 0 && 341 strcmp(key, "insecure_locks") != 0 && 342 strcmp(key, "secure_locks") != 0 && 343 strcmp(key, "no_auth_nlm") != 0 && strcmp(key, "auth_nlm") != 0 && 344 strcmp(key, "no_acl") != 0 && strcmp(key, "mountpoint") != 0 && 345 strcmp(key, "mp") != 0 && strcmp(key, "fsuid") != 0 && 346 strcmp(key, "refer") != 0 && strcmp(key, "replicas") != 0 && 347 strcmp(key, "root_squash") != 0 && 348 strcmp(key, "no_root_squash") != 0 && 349 strcmp(key, "all_squash") != 0 && 350 strcmp(key, "no_all_squash") != 0 && strcmp(key, "fsid") != 0 && 351 strcmp(key, "anonuid") != 0 && strcmp(key, "anongid") != 0) { 352 return (SA_SYNTAX_ERR); 353 } 354 355 (void) add_linux_shareopt(plinux_opts, key, value); 356 357 return (SA_OK); 358 } 359 360 /* 361 * Takes a string containing Solaris share options (e.g. "sync,no_acl") and 362 * converts them to a NULL-terminated array of Linux NFS options. 363 */ 364 static int 365 get_linux_shareopts(const char *shareopts, char **plinux_opts) 366 { 367 int error; 368 369 assert(plinux_opts != NULL); 370 371 *plinux_opts = NULL; 372 373 /* no_subtree_check - Default as of nfs-utils v1.1.0 */ 374 (void) add_linux_shareopt(plinux_opts, "no_subtree_check", NULL); 375 376 /* mountpoint - Restrict exports to ZFS mountpoints */ 377 (void) add_linux_shareopt(plinux_opts, "mountpoint", NULL); 378 379 error = foreach_nfs_shareopt(shareopts, get_linux_shareopts_cb, 380 plinux_opts); 381 382 if (error != SA_OK) { 383 free(*plinux_opts); 384 *plinux_opts = NULL; 385 } 386 387 return (error); 388 } 389 390 /* 391 * This function populates an entry into /etc/exports.d/zfs.exports. 392 * This file is consumed by the linux nfs server so that zfs shares are 393 * automatically exported upon boot or whenever the nfs server restarts. 394 */ 395 static int 396 nfs_add_entry(const char *filename, const char *sharepath, 397 const char *host, const char *security, const char *access_opts, 398 void *pcookie) 399 { 400 int error; 401 char *linuxhost; 402 const char *linux_opts = (const char *)pcookie; 403 404 error = get_linux_hostspec(host, &linuxhost); 405 if (error != SA_OK) 406 return (error); 407 408 if (linux_opts == NULL) 409 linux_opts = ""; 410 411 FILE *fp = fopen(filename, "a+e"); 412 if (fp == NULL) { 413 fprintf(stderr, "failed to open %s file: %s", filename, 414 strerror(errno)); 415 free(linuxhost); 416 return (SA_SYSTEM_ERR); 417 } 418 419 if (fprintf(fp, "%s %s(sec=%s,%s,%s)\n", sharepath, linuxhost, 420 security, access_opts, linux_opts) < 0) { 421 fprintf(stderr, "failed to write to %s\n", filename); 422 free(linuxhost); 423 fclose(fp); 424 return (SA_SYSTEM_ERR); 425 } 426 427 free(linuxhost); 428 if (fclose(fp) != 0) { 429 fprintf(stderr, "Unable to close file %s: %s\n", 430 filename, strerror(errno)); 431 return (SA_SYSTEM_ERR); 432 } 433 return (SA_OK); 434 } 435 436 /* 437 * This function copies all entries from the exports file to "filename", 438 * omitting any entries for the specified mountpoint. 439 */ 440 int 441 nfs_copy_entries(char *filename, const char *mountpoint) 442 { 443 char *buf = NULL; 444 size_t buflen = 0; 445 int error = SA_OK; 446 447 FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "re"); 448 FILE *newfp = fopen(filename, "w+e"); 449 if (newfp == NULL) { 450 fprintf(stderr, "failed to open %s file: %s", filename, 451 strerror(errno)); 452 fclose(oldfp); 453 return (SA_SYSTEM_ERR); 454 } 455 fputs(FILE_HEADER, newfp); 456 457 /* 458 * The ZFS_EXPORTS_FILE may not exist yet. If that's the 459 * case then just write out the new file. 460 */ 461 if (oldfp != NULL) { 462 while (getline(&buf, &buflen, oldfp) != -1) { 463 char *space = NULL; 464 465 if (buf[0] == '\n' || buf[0] == '#') 466 continue; 467 468 if ((space = strchr(buf, ' ')) != NULL) { 469 int mountpoint_len = strlen(mountpoint); 470 471 if (space - buf == mountpoint_len && 472 strncmp(mountpoint, buf, 473 mountpoint_len) == 0) { 474 continue; 475 } 476 } 477 fputs(buf, newfp); 478 } 479 480 if (ferror(oldfp) != 0) { 481 error = ferror(oldfp); 482 } 483 if (fclose(oldfp) != 0) { 484 fprintf(stderr, "Unable to close file %s: %s\n", 485 filename, strerror(errno)); 486 error = error != 0 ? error : SA_SYSTEM_ERR; 487 } 488 } 489 490 if (error == 0 && ferror(newfp) != 0) { 491 error = ferror(newfp); 492 } 493 494 free(buf); 495 if (fclose(newfp) != 0) { 496 fprintf(stderr, "Unable to close file %s: %s\n", 497 filename, strerror(errno)); 498 error = error != 0 ? error : SA_SYSTEM_ERR; 499 } 500 return (error); 501 } 502 503 /* 504 * Enables NFS sharing for the specified share. 505 */ 506 static int 507 nfs_enable_share_impl(sa_share_impl_t impl_share, char *filename) 508 { 509 char *shareopts, *linux_opts; 510 int error; 511 512 shareopts = FSINFO(impl_share, nfs_fstype)->shareopts; 513 error = get_linux_shareopts(shareopts, &linux_opts); 514 if (error != SA_OK) 515 return (error); 516 517 error = foreach_nfs_host(impl_share, filename, nfs_add_entry, 518 linux_opts); 519 free(linux_opts); 520 return (error); 521 } 522 523 static int 524 nfs_enable_share(sa_share_impl_t impl_share) 525 { 526 return (nfs_toggle_share( 527 ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share, 528 nfs_enable_share_impl)); 529 } 530 531 /* 532 * Disables NFS sharing for the specified share. 533 */ 534 static int 535 nfs_disable_share_impl(sa_share_impl_t impl_share, char *filename) 536 { 537 return (SA_OK); 538 } 539 540 static int 541 nfs_disable_share(sa_share_impl_t impl_share) 542 { 543 return (nfs_toggle_share( 544 ZFS_EXPORTS_LOCK, ZFS_EXPORTS_FILE, ZFS_EXPORTS_DIR, impl_share, 545 nfs_disable_share_impl)); 546 } 547 548 static boolean_t 549 nfs_is_shared(sa_share_impl_t impl_share) 550 { 551 size_t buflen = 0; 552 char *buf = NULL; 553 554 FILE *fp = fopen(ZFS_EXPORTS_FILE, "re"); 555 if (fp == NULL) { 556 return (B_FALSE); 557 } 558 while ((getline(&buf, &buflen, fp)) != -1) { 559 char *space = NULL; 560 561 if ((space = strchr(buf, ' ')) != NULL) { 562 int mountpoint_len = strlen(impl_share->sa_mountpoint); 563 564 if (space - buf == mountpoint_len && 565 strncmp(impl_share->sa_mountpoint, buf, 566 mountpoint_len) == 0) { 567 fclose(fp); 568 free(buf); 569 return (B_TRUE); 570 } 571 } 572 } 573 free(buf); 574 fclose(fp); 575 return (B_FALSE); 576 } 577 578 /* 579 * Checks whether the specified NFS share options are syntactically correct. 580 */ 581 static int 582 nfs_validate_shareopts(const char *shareopts) 583 { 584 char *linux_opts; 585 int error; 586 587 error = get_linux_shareopts(shareopts, &linux_opts); 588 589 if (error != SA_OK) 590 return (error); 591 592 free(linux_opts); 593 return (SA_OK); 594 } 595 596 static int 597 nfs_update_shareopts(sa_share_impl_t impl_share, const char *shareopts) 598 { 599 FSINFO(impl_share, nfs_fstype)->shareopts = (char *)shareopts; 600 return (SA_OK); 601 } 602 603 /* 604 * Clears a share's NFS options. Used by libshare to 605 * clean up shares that are about to be free()'d. 606 */ 607 static void 608 nfs_clear_shareopts(sa_share_impl_t impl_share) 609 { 610 FSINFO(impl_share, nfs_fstype)->shareopts = NULL; 611 } 612 613 static int 614 nfs_commit_shares(void) 615 { 616 char *argv[] = { 617 "/usr/sbin/exportfs", 618 "-ra", 619 NULL 620 }; 621 622 return (libzfs_run_process(argv[0], argv, 0)); 623 } 624 625 static const sa_share_ops_t nfs_shareops = { 626 .enable_share = nfs_enable_share, 627 .disable_share = nfs_disable_share, 628 .is_shared = nfs_is_shared, 629 630 .validate_shareopts = nfs_validate_shareopts, 631 .update_shareopts = nfs_update_shareopts, 632 .clear_shareopts = nfs_clear_shareopts, 633 .commit_shares = nfs_commit_shares, 634 }; 635 636 /* 637 * Initializes the NFS functionality of libshare. 638 */ 639 void 640 libshare_nfs_init(void) 641 { 642 nfs_fstype = register_fstype("nfs", &nfs_shareops); 643 } 644