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 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * NFS specific functions 31 */ 32 #include <stdio.h> 33 #include <string.h> 34 #include <ctype.h> 35 #include <stdlib.h> 36 #include <unistd.h> 37 #include <zone.h> 38 #include <errno.h> 39 #include <locale.h> 40 #include <signal.h> 41 #include "libshare.h" 42 #include "libshare_impl.h" 43 #include <nfs/export.h> 44 #include <pwd.h> 45 #include <limits.h> 46 #include <libscf.h> 47 #include "nfslog_config.h" 48 #include "nfslogtab.h" 49 #include "libshare_nfs.h" 50 #include <rpcsvc/daemon_utils.h> 51 #include <nfs/nfs.h> 52 #include <nfs/nfssys.h> 53 54 /* should really be in some global place */ 55 #define DEF_WIN 30000 56 #define OPT_CHUNK 1024 57 58 int debug = 0; 59 60 #define NFS_SERVER_SVC "svc:/network/nfs/server:default" 61 62 /* internal functions */ 63 static int nfs_init(); 64 static void nfs_fini(); 65 static int nfs_enable_share(sa_share_t); 66 static int nfs_disable_share(sa_share_t, char *); 67 static int nfs_validate_property(sa_property_t, sa_optionset_t); 68 static int nfs_validate_security_mode(char *); 69 static int nfs_is_security_opt(char *); 70 static int nfs_parse_legacy_options(sa_group_t, char *); 71 static char *nfs_format_options(sa_group_t, int); 72 static int nfs_set_proto_prop(sa_property_t); 73 static sa_protocol_properties_t nfs_get_proto_set(); 74 static char *nfs_get_status(); 75 static char *nfs_space_alias(char *); 76 static uint64_t nfs_features(); 77 78 /* 79 * ops vector that provides the protocol specific info and operations 80 * for share management. 81 */ 82 83 struct sa_plugin_ops sa_plugin_ops = { 84 SA_PLUGIN_VERSION, 85 "nfs", 86 nfs_init, 87 nfs_fini, 88 nfs_enable_share, 89 nfs_disable_share, 90 nfs_validate_property, 91 nfs_validate_security_mode, 92 nfs_is_security_opt, 93 nfs_parse_legacy_options, 94 nfs_format_options, 95 nfs_set_proto_prop, 96 nfs_get_proto_set, 97 nfs_get_status, 98 nfs_space_alias, 99 NULL, /* update_legacy */ 100 NULL, /* delete_legacy */ 101 NULL, /* change_notify */ 102 NULL, /* enable_resource */ 103 NULL, /* disable_resource */ 104 nfs_features, 105 NULL, /* transient shares */ 106 NULL, /* notify resource */ 107 NULL 108 }; 109 110 /* 111 * list of support services needed 112 * defines should come from head/rpcsvc/daemon_utils.h 113 */ 114 115 static char *service_list_default[] = 116 { STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NULL }; 117 static char *service_list_logging[] = 118 { STATD, LOCKD, MOUNTD, NFSD, NFSMAPID, RQUOTAD, NFSLOGD, NULL }; 119 120 /* 121 * option definitions. Make sure to keep the #define for the option 122 * index just before the entry it is the index for. Changing the order 123 * can cause breakage. E.g OPT_RW is index 1 and must precede the 124 * line that includes the SHOPT_RW and OPT_RW entries. 125 */ 126 127 struct option_defs optdefs[] = { 128 #define OPT_RO 0 129 {SHOPT_RO, OPT_RO, OPT_TYPE_ACCLIST}, 130 #define OPT_RW 1 131 {SHOPT_RW, OPT_RW, OPT_TYPE_ACCLIST}, 132 #define OPT_ROOT 2 133 {SHOPT_ROOT, OPT_ROOT, OPT_TYPE_ACCLIST}, 134 #define OPT_SECURE 3 135 {SHOPT_SECURE, OPT_SECURE, OPT_TYPE_DEPRECATED}, 136 #define OPT_ANON 4 137 {SHOPT_ANON, OPT_ANON, OPT_TYPE_USER}, 138 #define OPT_WINDOW 5 139 {SHOPT_WINDOW, OPT_WINDOW, OPT_TYPE_NUMBER}, 140 #define OPT_NOSUID 6 141 {SHOPT_NOSUID, OPT_NOSUID, OPT_TYPE_BOOLEAN}, 142 #define OPT_ACLOK 7 143 {SHOPT_ACLOK, OPT_ACLOK, OPT_TYPE_BOOLEAN}, 144 #define OPT_NOSUB 8 145 {SHOPT_NOSUB, OPT_NOSUB, OPT_TYPE_BOOLEAN}, 146 #define OPT_SEC 9 147 {SHOPT_SEC, OPT_SEC, OPT_TYPE_SECURITY}, 148 #define OPT_PUBLIC 10 149 {SHOPT_PUBLIC, OPT_PUBLIC, OPT_TYPE_BOOLEAN, OPT_SHARE_ONLY}, 150 #define OPT_INDEX 11 151 {SHOPT_INDEX, OPT_INDEX, OPT_TYPE_FILE}, 152 #define OPT_LOG 12 153 {SHOPT_LOG, OPT_LOG, OPT_TYPE_LOGTAG}, 154 #define OPT_CKSUM 13 155 {SHOPT_CKSUM, OPT_CKSUM, OPT_TYPE_STRINGSET}, 156 #ifdef VOLATILE_FH_TEST /* XXX added for testing volatile fh's only */ 157 #define OPT_VOLFH 14 158 {SHOPT_VOLFH, OPT_VOLFH}, 159 #endif /* VOLATILE_FH_TEST */ 160 NULL 161 }; 162 163 /* 164 * list of properties that are related to security flavors. 165 */ 166 static char *seclist[] = { 167 SHOPT_RO, 168 SHOPT_RW, 169 SHOPT_ROOT, 170 SHOPT_WINDOW, 171 NULL 172 }; 173 174 /* structure for list of securities */ 175 struct securities { 176 sa_security_t security; 177 struct securities *next; 178 }; 179 180 /* 181 * findopt(name) 182 * 183 * Lookup option "name" in the option table and return the table 184 * index. 185 */ 186 187 static int 188 findopt(char *name) 189 { 190 int i; 191 if (name != NULL) { 192 for (i = 0; optdefs[i].tag != NULL; i++) { 193 if (strcmp(optdefs[i].tag, name) == 0) 194 return (i); 195 } 196 } 197 return (-1); 198 } 199 200 /* 201 * gettype(name) 202 * 203 * Return the type of option "name". 204 */ 205 206 static int 207 gettype(char *name) 208 { 209 int optdef; 210 211 optdef = findopt(name); 212 if (optdef != -1) 213 return (optdefs[optdef].type); 214 return (OPT_TYPE_ANY); 215 } 216 217 /* 218 * nfs_validate_security_mode(mode) 219 * 220 * is the specified mode string a valid one for use with NFS? 221 */ 222 223 static int 224 nfs_validate_security_mode(char *mode) 225 { 226 seconfig_t secinfo; 227 int err; 228 229 (void) memset(&secinfo, '\0', sizeof (secinfo)); 230 err = nfs_getseconfig_byname(mode, &secinfo); 231 if (err == SC_NOERROR) 232 return (1); 233 return (0); 234 } 235 236 /* 237 * nfs_is_security_opt(tok) 238 * 239 * check to see if tok represents an option that is only valid in some 240 * security flavor. 241 */ 242 243 static int 244 nfs_is_security_opt(char *tok) 245 { 246 int i; 247 248 for (i = 0; seclist[i] != NULL; i++) { 249 if (strcmp(tok, seclist[i]) == 0) 250 return (1); 251 } 252 return (0); 253 } 254 255 /* 256 * find_security(seclist, sec) 257 * 258 * Walk the current list of security flavors and return true if it is 259 * present, else return false. 260 */ 261 262 static int 263 find_security(struct securities *seclist, sa_security_t sec) 264 { 265 while (seclist != NULL) { 266 if (seclist->security == sec) 267 return (1); 268 seclist = seclist->next; 269 } 270 return (0); 271 } 272 273 /* 274 * make_security_list(group, securitymodes, proto) 275 * go through the list of securitymodes and add them to the 276 * group's list of security optionsets. We also keep a list of 277 * those optionsets so we don't have to find them later. All of 278 * these will get copies of the same properties. 279 */ 280 281 static struct securities * 282 make_security_list(sa_group_t group, char *securitymodes, char *proto) 283 { 284 char *tok, *next = NULL; 285 struct securities *curp, *headp = NULL, *prev; 286 sa_security_t check; 287 int freetok = 0; 288 289 for (tok = securitymodes; tok != NULL; tok = next) { 290 next = strchr(tok, ':'); 291 if (next != NULL) 292 *next++ = '\0'; 293 if (strcmp(tok, "default") == 0) { 294 /* resolve default into the real type */ 295 tok = nfs_space_alias(tok); 296 freetok = 1; 297 } 298 check = sa_get_security(group, tok, proto); 299 300 /* add to the security list if it isn't there already */ 301 if (check == NULL || !find_security(headp, check)) { 302 curp = (struct securities *)calloc(1, 303 sizeof (struct securities)); 304 if (curp != NULL) { 305 if (check == NULL) { 306 curp->security = sa_create_security( 307 group, tok, proto); 308 } else { 309 curp->security = check; 310 } 311 /* 312 * note that the first time through the loop, 313 * headp will be NULL and prev will be 314 * undefined. Since headp is NULL, we set 315 * both it and prev to the curp (first 316 * structure to be allocated). 317 * 318 * later passes through the loop will have 319 * headp not being NULL and prev will be used 320 * to allocate at the end of the list. 321 */ 322 if (headp == NULL) { 323 headp = curp; 324 prev = curp; 325 } else { 326 prev->next = curp; 327 prev = curp; 328 } 329 } 330 } 331 332 if (freetok) { 333 freetok = 0; 334 sa_free_attr_string(tok); 335 } 336 } 337 return (headp); 338 } 339 340 static void 341 free_security_list(struct securities *sec) 342 { 343 struct securities *next; 344 if (sec != NULL) { 345 for (next = sec->next; sec != NULL; sec = next) { 346 next = sec->next; 347 free(sec); 348 } 349 } 350 } 351 352 /* 353 * nfs_alistcat(str1, str2, sep) 354 * 355 * concatenate str1 and str2 into a new string using sep as a separate 356 * character. If memory allocation fails, return NULL; 357 */ 358 359 static char * 360 nfs_alistcat(char *str1, char *str2, char sep) 361 { 362 char *newstr; 363 size_t len; 364 365 len = strlen(str1) + strlen(str2) + 2; 366 newstr = (char *)malloc(len); 367 if (newstr != NULL) 368 (void) snprintf(newstr, len, "%s%c%s", str1, sep, str2); 369 return (newstr); 370 } 371 372 /* 373 * add_security_prop(sec, name, value, persist) 374 * 375 * Add the property to the securities structure. This accumulates 376 * properties for as part of parsing legacy options. 377 */ 378 379 static int 380 add_security_prop(struct securities *sec, char *name, char *value, 381 int persist, int iszfs) 382 { 383 sa_property_t prop; 384 int ret = SA_OK; 385 386 for (; sec != NULL; sec = sec->next) { 387 if (value == NULL) { 388 if (strcmp(name, SHOPT_RW) == 0 || 389 strcmp(name, SHOPT_RO) == 0) 390 value = "*"; 391 else 392 value = "true"; 393 } 394 395 /* 396 * Get the existing property, if it exists, so we can 397 * determine what to do with it. The ro/rw/root 398 * properties can be merged if multiple instances of 399 * these properies are given. For example, if "rw" 400 * exists with a value "host1" and a later token of 401 * rw="host2" is seen, the values are merged into a 402 * single rw="host1:host2". 403 */ 404 prop = sa_get_property(sec->security, name); 405 406 if (prop != NULL) { 407 char *oldvalue; 408 char *newvalue; 409 410 /* 411 * The security options of ro/rw/root might appear 412 * multiple times. If they do, the values need to be 413 * merged into an access list. If it was previously 414 * empty, the new value alone is added. 415 */ 416 oldvalue = sa_get_property_attr(prop, "value"); 417 if (oldvalue != NULL) { 418 /* 419 * The general case is to concatenate the new 420 * value onto the old value for multiple 421 * rw(ro/root) properties. A special case 422 * exists when either the old or new is the 423 * "all" case. In the special case, if both 424 * are "all", then it is "all", else if one is 425 * an access-list, that replaces the "all". 426 */ 427 if (strcmp(oldvalue, "*") == 0) { 428 /* Replace old value with new value. */ 429 newvalue = strdup(value); 430 } else if (strcmp(value, "*") == 0 || 431 strcmp(oldvalue, value) == 0) { 432 /* 433 * Keep old value and ignore 434 * the new value. 435 */ 436 newvalue = NULL; 437 } else { 438 /* 439 * Make a new list of old plus new 440 * access-list. 441 */ 442 newvalue = nfs_alistcat(oldvalue, 443 value, ':'); 444 } 445 446 if (newvalue != NULL) { 447 (void) sa_remove_property(prop); 448 prop = sa_create_property(name, 449 newvalue); 450 ret = sa_add_property(sec->security, 451 prop); 452 free(newvalue); 453 } 454 if (oldvalue != NULL) 455 sa_free_attr_string(oldvalue); 456 } 457 } else { 458 prop = sa_create_property(name, value); 459 ret = sa_add_property(sec->security, prop); 460 } 461 if (ret == SA_OK && !iszfs) { 462 ret = sa_commit_properties(sec->security, !persist); 463 } 464 } 465 return (ret); 466 } 467 468 /* 469 * check to see if group/share is persistent. 470 */ 471 static int 472 is_persistent(sa_group_t group) 473 { 474 char *type; 475 int persist = 1; 476 477 type = sa_get_group_attr(group, "type"); 478 if (type != NULL && strcmp(type, "persist") != 0) 479 persist = 0; 480 if (type != NULL) 481 sa_free_attr_string(type); 482 return (persist); 483 } 484 485 /* 486 * invalid_security(options) 487 * 488 * search option string for any invalid sec= type. 489 * return true (1) if any are not valid else false (0) 490 */ 491 static int 492 invalid_security(char *options) 493 { 494 char *copy, *base, *token, *value; 495 int ret = 0; 496 497 copy = strdup(options); 498 token = base = copy; 499 while (token != NULL && ret == 0) { 500 token = strtok(base, ","); 501 base = NULL; 502 if (token != NULL) { 503 value = strchr(token, '='); 504 if (value != NULL) 505 *value++ = '\0'; 506 if (strcmp(token, "sec") == 0) { 507 /* HAVE security flavors so check them */ 508 char *tok, *next; 509 for (next = NULL, tok = value; tok != NULL; 510 tok = next) { 511 next = strchr(tok, ':'); 512 if (next != NULL) 513 *next++ = '\0'; 514 ret = !nfs_validate_security_mode(tok); 515 if (ret) 516 break; 517 } 518 } 519 } 520 } 521 if (copy != NULL) 522 free(copy); 523 return (ret); 524 } 525 526 /* 527 * nfs_parse_legacy_options(group, options) 528 * 529 * Parse the old style options into internal format and store on the 530 * specified group. Group could be a share for full legacy support. 531 */ 532 533 static int 534 nfs_parse_legacy_options(sa_group_t group, char *options) 535 { 536 char *dup; 537 char *base; 538 char *token; 539 sa_optionset_t optionset; 540 struct securities *security_list = NULL; 541 sa_property_t prop; 542 int ret = SA_OK; 543 int iszfs = 0; 544 sa_group_t parent; 545 int persist = 0; 546 char *lasts; 547 548 /* do we have an existing optionset? */ 549 optionset = sa_get_optionset(group, "nfs"); 550 if (optionset == NULL) { 551 /* didn't find existing optionset so create one */ 552 optionset = sa_create_optionset(group, "nfs"); 553 } else { 554 /* 555 * Have an existing optionset . Ideally, we would need 556 * to compare options in order to detect errors. For 557 * now, we assume that the first optionset is the 558 * correct one and the others will be the same. An 559 * empty optionset is the same as no optionset so we 560 * don't want to exit in that case. Getting an empty 561 * optionset can occur with ZFS property checking. 562 */ 563 if (sa_get_property(optionset, NULL) != NULL) 564 return (ret); 565 } 566 567 if (strcmp(options, SHOPT_RW) == 0) { 568 /* 569 * there is a special case of only the option "rw" 570 * being the default option. We don't have to do 571 * anything. 572 */ 573 return (ret); 574 } 575 576 /* 577 * check if security types are present and validate them. If 578 * any are not legal, fail. 579 */ 580 581 if (invalid_security(options)) { 582 return (SA_INVALID_SECURITY); 583 } 584 585 /* 586 * in order to not attempt to change ZFS properties unless 587 * absolutely necessary, we never do it in the legacy parsing. 588 */ 589 if (sa_is_share(group)) { 590 char *zfs; 591 parent = sa_get_parent_group(group); 592 if (parent != NULL) { 593 zfs = sa_get_group_attr(parent, "zfs"); 594 if (zfs != NULL) { 595 sa_free_attr_string(zfs); 596 iszfs++; 597 } 598 } 599 } else { 600 iszfs = sa_group_is_zfs(group); 601 } 602 603 /* We need a copy of options for the next part. */ 604 dup = strdup(options); 605 if (dup == NULL) 606 return (SA_NO_MEMORY); 607 608 /* 609 * we need to step through each option in the string and then 610 * add either the option or the security option as needed. If 611 * this is not a persistent share, don't commit to the 612 * repository. If there is an error, we also want to abort the 613 * processing and report it. 614 */ 615 persist = is_persistent(group); 616 base = dup; 617 token = dup; 618 lasts = NULL; 619 while (token != NULL && ret == SA_OK) { 620 ret = SA_OK; 621 token = strtok_r(base, ",", &lasts); 622 base = NULL; 623 if (token != NULL) { 624 char *value; 625 /* 626 * if the option has a value, it will have an '=' to 627 * separate the name from the value. The following 628 * code will result in value != NULL and token 629 * pointing to just the name if there is a value. 630 */ 631 value = strchr(token, '='); 632 if (value != NULL) { 633 *value++ = '\0'; 634 } 635 if (strcmp(token, "sec") == 0 || 636 strcmp(token, "secure") == 0) { 637 /* 638 * Once in security parsing, we only 639 * do security. We do need to move 640 * between the security node and the 641 * toplevel. The security tag goes on 642 * the root while the following ones 643 * go on the security. 644 */ 645 if (security_list != NULL) { 646 /* 647 * have an old list so close it and 648 * start the new 649 */ 650 free_security_list(security_list); 651 } 652 if (strcmp(token, "secure") == 0) { 653 value = "dh"; 654 } else { 655 if (value == NULL) { 656 ret = SA_SYNTAX_ERR; 657 break; 658 } 659 } 660 security_list = make_security_list(group, 661 value, "nfs"); 662 } else { 663 /* 664 * Note that the "old" syntax allowed a 665 * default security model This must be 666 * accounted for and internally converted to 667 * "standard" security structure. 668 */ 669 if (nfs_is_security_opt(token)) { 670 if (security_list == NULL) { 671 /* 672 * need to have a 673 * security 674 * option. This will 675 * be "closed" when a 676 * defined "sec=" 677 * option is 678 * seen. This is 679 * technically an 680 * error but will be 681 * allowed with 682 * warning. 683 */ 684 security_list = 685 make_security_list(group, 686 "default", 687 "nfs"); 688 } 689 if (security_list != NULL) { 690 ret = add_security_prop( 691 security_list, token, 692 value, persist, iszfs); 693 } else { 694 ret = SA_NO_MEMORY; 695 } 696 } else { 697 /* regular options */ 698 if (value == NULL) { 699 if (strcmp(token, SHOPT_RW) == 700 0 || strcmp(token, 701 SHOPT_RO) == 0) { 702 value = "*"; 703 } else { 704 value = "global"; 705 if (strcmp(token, 706 SHOPT_LOG) != 0) { 707 value = "true"; 708 } 709 } 710 } 711 /* 712 * In all cases, create the 713 * property specified. If the 714 * value was NULL, the default 715 * value will have been 716 * substituted. 717 */ 718 prop = sa_create_property(token, value); 719 ret = sa_add_property(optionset, prop); 720 if (ret != SA_OK) 721 break; 722 723 if (!iszfs) { 724 ret = sa_commit_properties( 725 optionset, !persist); 726 } 727 } 728 } 729 } 730 } 731 if (security_list != NULL) 732 free_security_list(security_list); 733 734 free(dup); 735 return (ret); 736 } 737 738 /* 739 * is_a_number(number) 740 * 741 * is the string a number in one of the forms we want to use? 742 */ 743 744 static int 745 is_a_number(char *number) 746 { 747 int ret = 1; 748 int hex = 0; 749 750 if (strncmp(number, "0x", 2) == 0) { 751 number += 2; 752 hex = 1; 753 } else if (*number == '-') { 754 number++; /* skip the minus */ 755 } 756 while (ret == 1 && *number != '\0') { 757 if (hex) { 758 ret = isxdigit(*number++); 759 } else { 760 ret = isdigit(*number++); 761 } 762 } 763 return (ret); 764 } 765 766 /* 767 * Look for the specified tag in the configuration file. If it is found, 768 * enable logging and set the logging configuration information for exp. 769 */ 770 static void 771 configlog(struct exportdata *exp, char *tag) 772 { 773 nfsl_config_t *configlist = NULL, *configp; 774 int error = 0; 775 char globaltag[] = DEFAULTTAG; 776 777 /* 778 * Sends config errors to stderr 779 */ 780 nfsl_errs_to_syslog = B_FALSE; 781 782 /* 783 * get the list of configuration settings 784 */ 785 error = nfsl_getconfig_list(&configlist); 786 if (error) { 787 (void) fprintf(stderr, 788 dgettext(TEXT_DOMAIN, "Cannot get log configuration: %s\n"), 789 strerror(error)); 790 } 791 792 if (tag == NULL) 793 tag = globaltag; 794 if ((configp = nfsl_findconfig(configlist, tag, &error)) == NULL) { 795 nfsl_freeconfig_list(&configlist); 796 (void) fprintf(stderr, 797 dgettext(TEXT_DOMAIN, "No tags matching \"%s\"\n"), tag); 798 /* bad configuration */ 799 error = ENOENT; 800 goto err; 801 } 802 803 if ((exp->ex_tag = strdup(tag)) == NULL) { 804 error = ENOMEM; 805 goto out; 806 } 807 if ((exp->ex_log_buffer = strdup(configp->nc_bufferpath)) == NULL) { 808 error = ENOMEM; 809 goto out; 810 } 811 exp->ex_flags |= EX_LOG; 812 if (configp->nc_rpclogpath != NULL) 813 exp->ex_flags |= EX_LOG_ALLOPS; 814 out: 815 if (configlist != NULL) 816 nfsl_freeconfig_list(&configlist); 817 818 err: 819 if (error != 0) { 820 if (exp->ex_flags != NULL) 821 free(exp->ex_tag); 822 if (exp->ex_log_buffer != NULL) 823 free(exp->ex_log_buffer); 824 (void) fprintf(stderr, 825 dgettext(TEXT_DOMAIN, "Cannot set log configuration: %s\n"), 826 strerror(error)); 827 } 828 } 829 830 /* 831 * fill_export_from_optionset(export, optionset) 832 * 833 * In order to share, we need to set all the possible general options 834 * into the export structure. Share info will be filled in by the 835 * caller. Various property values get turned into structure specific 836 * values. 837 */ 838 839 static int 840 fill_export_from_optionset(struct exportdata *export, sa_optionset_t optionset) 841 { 842 sa_property_t option; 843 int ret = SA_OK; 844 845 for (option = sa_get_property(optionset, NULL); 846 option != NULL; option = sa_get_next_property(option)) { 847 char *name; 848 char *value; 849 uint32_t val; 850 851 /* 852 * since options may be set/reset multiple times, always do an 853 * explicit set or clear of the option. This allows defaults 854 * to be set and then the protocol specific to override. 855 */ 856 857 name = sa_get_property_attr(option, "type"); 858 value = sa_get_property_attr(option, "value"); 859 switch (findopt(name)) { 860 case OPT_ANON: 861 if (value != NULL && is_a_number(value)) { 862 val = strtoul(value, NULL, 0); 863 } else { 864 struct passwd *pw; 865 pw = getpwnam(value != NULL ? value : "nobody"); 866 if (pw != NULL) { 867 val = pw->pw_uid; 868 } else { 869 val = UID_NOBODY; 870 } 871 endpwent(); 872 } 873 export->ex_anon = val; 874 break; 875 case OPT_NOSUID: 876 if (value != NULL && (strcasecmp(value, "true") == 0 || 877 strcmp(value, "1") == 0)) 878 export->ex_flags |= EX_NOSUID; 879 else 880 export->ex_flags &= ~EX_NOSUID; 881 break; 882 case OPT_ACLOK: 883 if (value != NULL && (strcasecmp(value, "true") == 0 || 884 strcmp(value, "1") == 0)) 885 export->ex_flags |= EX_ACLOK; 886 else 887 export->ex_flags &= ~EX_ACLOK; 888 break; 889 case OPT_NOSUB: 890 if (value != NULL && (strcasecmp(value, "true") == 0 || 891 strcmp(value, "1") == 0)) 892 export->ex_flags |= EX_NOSUB; 893 else 894 export->ex_flags &= ~EX_NOSUB; 895 break; 896 case OPT_PUBLIC: 897 if (value != NULL && (strcasecmp(value, "true") == 0 || 898 strcmp(value, "1") == 0)) 899 export->ex_flags |= EX_PUBLIC; 900 else 901 export->ex_flags &= ~EX_PUBLIC; 902 break; 903 case OPT_INDEX: 904 if (value != NULL && (strcmp(value, "..") == 0 || 905 strchr(value, '/') != NULL)) { 906 /* this is an error */ 907 (void) printf(dgettext(TEXT_DOMAIN, 908 "NFS: index=\"%s\" not valid;" 909 "must be a filename.\n"), 910 value); 911 break; 912 } 913 if (value != NULL && *value != '\0' && 914 strcmp(value, ".") != 0) { 915 /* valid index file string */ 916 if (export->ex_index != NULL) { 917 /* left over from "default" */ 918 free(export->ex_index); 919 } 920 /* remember to free */ 921 export->ex_index = strdup(value); 922 if (export->ex_index == NULL) { 923 (void) printf(dgettext(TEXT_DOMAIN, 924 "NFS: out of memory setting " 925 "index property\n")); 926 break; 927 } 928 export->ex_flags |= EX_INDEX; 929 } 930 break; 931 case OPT_LOG: 932 if (value == NULL) 933 value = strdup("global"); 934 if (value != NULL) 935 configlog(export, 936 strlen(value) ? value : "global"); 937 break; 938 default: 939 /* have a syntactic error */ 940 (void) printf(dgettext(TEXT_DOMAIN, 941 "NFS: unrecognized option %s=%s\n"), 942 name, value != NULL ? value : ""); 943 break; 944 } 945 if (name != NULL) 946 sa_free_attr_string(name); 947 if (value != NULL) 948 sa_free_attr_string(value); 949 } 950 return (ret); 951 } 952 953 /* 954 * cleanup_export(export) 955 * 956 * Cleanup the allocated areas so we don't leak memory 957 */ 958 959 static void 960 cleanup_export(struct exportdata *export) 961 { 962 int i; 963 964 if (export->ex_index != NULL) 965 free(export->ex_index); 966 if (export->ex_secinfo != NULL) { 967 for (i = 0; i < export->ex_seccnt; i++) 968 if (export->ex_secinfo[i].s_rootnames != NULL) 969 free(export->ex_secinfo[i].s_rootnames); 970 free(export->ex_secinfo); 971 } 972 } 973 974 /* 975 * Given a seconfig entry and a colon-separated 976 * list of names, allocate an array big enough 977 * to hold the root list, then convert each name to 978 * a principal name according to the security 979 * info and assign it to an array element. 980 * Return the array and its size. 981 */ 982 static caddr_t * 983 get_rootnames(seconfig_t *sec, char *list, int *count) 984 { 985 caddr_t *a; 986 int c, i; 987 char *host, *p; 988 989 /* 990 * Count the number of strings in the list. 991 * This is the number of colon separators + 1. 992 */ 993 c = 1; 994 for (p = list; *p; p++) 995 if (*p == ':') 996 c++; 997 *count = c; 998 999 a = (caddr_t *)malloc(c * sizeof (char *)); 1000 if (a == NULL) { 1001 (void) printf(dgettext(TEXT_DOMAIN, 1002 "get_rootnames: no memory\n")); 1003 } else { 1004 for (i = 0; i < c; i++) { 1005 host = strtok(list, ":"); 1006 if (!nfs_get_root_principal(sec, host, &a[i])) { 1007 free(a); 1008 a = NULL; 1009 break; 1010 } 1011 list = NULL; 1012 } 1013 } 1014 1015 return (a); 1016 } 1017 1018 /* 1019 * fill_security_from_secopts(sp, secopts) 1020 * 1021 * Fill the secinfo structure from the secopts optionset. 1022 */ 1023 1024 static int 1025 fill_security_from_secopts(struct secinfo *sp, sa_security_t secopts) 1026 { 1027 sa_property_t prop; 1028 char *type; 1029 int longform; 1030 int err = SC_NOERROR; 1031 1032 type = sa_get_security_attr(secopts, "sectype"); 1033 if (type != NULL) { 1034 /* named security type needs secinfo to be filled in */ 1035 err = nfs_getseconfig_byname(type, &sp->s_secinfo); 1036 sa_free_attr_string(type); 1037 if (err != SC_NOERROR) 1038 return (err); 1039 } else { 1040 /* default case */ 1041 err = nfs_getseconfig_default(&sp->s_secinfo); 1042 if (err != SC_NOERROR) 1043 return (err); 1044 } 1045 1046 err = SA_OK; 1047 for (prop = sa_get_property(secopts, NULL); 1048 prop != NULL && err == SA_OK; 1049 prop = sa_get_next_property(prop)) { 1050 char *name; 1051 char *value; 1052 1053 name = sa_get_property_attr(prop, "type"); 1054 value = sa_get_property_attr(prop, "value"); 1055 1056 longform = value != NULL && strcmp(value, "*") != 0; 1057 1058 switch (findopt(name)) { 1059 case OPT_RO: 1060 sp->s_flags |= longform ? M_ROL : M_RO; 1061 break; 1062 case OPT_RW: 1063 sp->s_flags |= longform ? M_RWL : M_RW; 1064 break; 1065 case OPT_ROOT: 1066 sp->s_flags |= M_ROOT; 1067 /* 1068 * if we are using AUTH_UNIX, handle like other things 1069 * such as RO/RW 1070 */ 1071 if (sp->s_secinfo.sc_rpcnum == AUTH_UNIX) 1072 continue; 1073 /* not AUTH_UNIX */ 1074 if (value != NULL) { 1075 sp->s_rootnames = get_rootnames(&sp->s_secinfo, 1076 value, &sp->s_rootcnt); 1077 if (sp->s_rootnames == NULL) { 1078 err = SA_BAD_VALUE; 1079 (void) fprintf(stderr, 1080 dgettext(TEXT_DOMAIN, 1081 "Bad root list\n")); 1082 } 1083 } 1084 break; 1085 case OPT_WINDOW: 1086 if (value != NULL) { 1087 sp->s_window = atoi(value); 1088 /* just in case */ 1089 if (sp->s_window < 0) 1090 sp->s_window = DEF_WIN; 1091 } 1092 break; 1093 default: 1094 break; 1095 } 1096 if (name != NULL) 1097 sa_free_attr_string(name); 1098 if (value != NULL) 1099 sa_free_attr_string(value); 1100 } 1101 /* if rw/ro options not set, use default of RW */ 1102 if ((sp->s_flags & NFS_RWMODES) == 0) 1103 sp->s_flags |= M_RW; 1104 return (err); 1105 } 1106 1107 /* 1108 * This is for testing only 1109 * It displays the export structure that 1110 * goes into the kernel. 1111 */ 1112 static void 1113 printarg(char *path, struct exportdata *ep) 1114 { 1115 int i, j; 1116 struct secinfo *sp; 1117 1118 if (debug == 0) 1119 return; 1120 1121 (void) printf("%s:\n", path); 1122 (void) printf("\tex_version = %d\n", ep->ex_version); 1123 (void) printf("\tex_path = %s\n", ep->ex_path); 1124 (void) printf("\tex_pathlen = %ld\n", (ulong_t)ep->ex_pathlen); 1125 (void) printf("\tex_flags: (0x%02x) ", ep->ex_flags); 1126 if (ep->ex_flags & EX_NOSUID) 1127 (void) printf("NOSUID "); 1128 if (ep->ex_flags & EX_ACLOK) 1129 (void) printf("ACLOK "); 1130 if (ep->ex_flags & EX_PUBLIC) 1131 (void) printf("PUBLIC "); 1132 if (ep->ex_flags & EX_NOSUB) 1133 (void) printf("NOSUB "); 1134 if (ep->ex_flags & EX_LOG) 1135 (void) printf("LOG "); 1136 if (ep->ex_flags & EX_LOG_ALLOPS) 1137 (void) printf("LOG_ALLOPS "); 1138 if (ep->ex_flags == 0) 1139 (void) printf("(none)"); 1140 (void) printf("\n"); 1141 if (ep->ex_flags & EX_LOG) { 1142 (void) printf("\tex_log_buffer = %s\n", 1143 (ep->ex_log_buffer ? ep->ex_log_buffer : "(NULL)")); 1144 (void) printf("\tex_tag = %s\n", 1145 (ep->ex_tag ? ep->ex_tag : "(NULL)")); 1146 } 1147 (void) printf("\tex_anon = %d\n", ep->ex_anon); 1148 (void) printf("\tex_seccnt = %d\n", ep->ex_seccnt); 1149 (void) printf("\n"); 1150 for (i = 0; i < ep->ex_seccnt; i++) { 1151 sp = &ep->ex_secinfo[i]; 1152 (void) printf("\t\ts_secinfo = %s\n", sp->s_secinfo.sc_name); 1153 (void) printf("\t\ts_flags: (0x%02x) ", sp->s_flags); 1154 if (sp->s_flags & M_ROOT) (void) printf("M_ROOT "); 1155 if (sp->s_flags & M_RO) (void) printf("M_RO "); 1156 if (sp->s_flags & M_ROL) (void) printf("M_ROL "); 1157 if (sp->s_flags & M_RW) (void) printf("M_RW "); 1158 if (sp->s_flags & M_RWL) (void) printf("M_RWL "); 1159 if (sp->s_flags == 0) (void) printf("(none)"); 1160 (void) printf("\n"); 1161 (void) printf("\t\ts_window = %d\n", sp->s_window); 1162 (void) printf("\t\ts_rootcnt = %d ", sp->s_rootcnt); 1163 (void) fflush(stdout); 1164 for (j = 0; j < sp->s_rootcnt; j++) 1165 (void) printf("%s ", sp->s_rootnames[j] ? 1166 sp->s_rootnames[j] : "<null>"); 1167 (void) printf("\n\n"); 1168 } 1169 } 1170 1171 /* 1172 * count_security(opts) 1173 * 1174 * Count the number of security types (flavors). The optionset has 1175 * been populated with the security flavors as a holding mechanism. 1176 * We later use this number to allocate data structures. 1177 */ 1178 1179 static int 1180 count_security(sa_optionset_t opts) 1181 { 1182 int count = 0; 1183 sa_property_t prop; 1184 if (opts != NULL) { 1185 for (prop = sa_get_property(opts, NULL); prop != NULL; 1186 prop = sa_get_next_property(prop)) { 1187 count++; 1188 } 1189 } 1190 return (count); 1191 } 1192 1193 /* 1194 * nfs_sprint_option(rbuff, rbuffsize, incr, prop, sep) 1195 * 1196 * provides a mechanism to format NFS properties into legacy output 1197 * format. If the buffer would overflow, it is reallocated and grown 1198 * as appropriate. Special cases of converting internal form of values 1199 * to those used by "share" are done. this function does one property 1200 * at a time. 1201 */ 1202 1203 static int 1204 nfs_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr, 1205 sa_property_t prop, int sep) 1206 { 1207 char *name; 1208 char *value; 1209 int curlen; 1210 char *buff = *rbuff; 1211 size_t buffsize = *rbuffsize; 1212 int printed = B_FALSE; 1213 1214 name = sa_get_property_attr(prop, "type"); 1215 value = sa_get_property_attr(prop, "value"); 1216 if (buff != NULL) 1217 curlen = strlen(buff); 1218 else 1219 curlen = 0; 1220 if (name != NULL) { 1221 int len; 1222 len = strlen(name) + sep; 1223 1224 /* 1225 * A future RFE would be to replace this with more 1226 * generic code and to possibly handle more types. 1227 */ 1228 switch (gettype(name)) { 1229 case OPT_TYPE_BOOLEAN: 1230 /* 1231 * For NFS, boolean value of FALSE means it 1232 * doesn't show up in the option list at all. 1233 */ 1234 if (value != NULL && strcasecmp(value, "false") == 0) 1235 goto skip; 1236 if (value != NULL) { 1237 sa_free_attr_string(value); 1238 value = NULL; 1239 } 1240 break; 1241 case OPT_TYPE_ACCLIST: 1242 if (value != NULL && strcmp(value, "*") == 0) { 1243 sa_free_attr_string(value); 1244 value = NULL; 1245 } else { 1246 if (value != NULL) 1247 len += 1 + strlen(value); 1248 } 1249 break; 1250 case OPT_TYPE_LOGTAG: 1251 if (value != NULL && strlen(value) == 0) { 1252 sa_free_attr_string(value); 1253 value = NULL; 1254 } else { 1255 if (value != NULL) 1256 len += 1 + strlen(value); 1257 } 1258 break; 1259 default: 1260 if (value != NULL) 1261 len += 1 + strlen(value); 1262 break; 1263 } 1264 while (buffsize <= (curlen + len)) { 1265 /* need more room */ 1266 buffsize += incr; 1267 buff = realloc(buff, buffsize); 1268 if (buff == NULL) { 1269 /* realloc failed so free everything */ 1270 if (*rbuff != NULL) 1271 free(*rbuff); 1272 } 1273 *rbuff = buff; 1274 *rbuffsize = buffsize; 1275 if (buff == NULL) 1276 goto skip; 1277 1278 } 1279 1280 if (buff == NULL) 1281 goto skip; 1282 1283 if (value == NULL) { 1284 (void) snprintf(buff + curlen, buffsize - curlen, 1285 "%s%s", sep ? "," : "", 1286 name, value != NULL ? value : ""); 1287 } else { 1288 (void) snprintf(buff + curlen, buffsize - curlen, 1289 "%s%s=%s", sep ? "," : "", 1290 name, value != NULL ? value : ""); 1291 } 1292 printed = B_TRUE; 1293 } 1294 skip: 1295 if (name != NULL) 1296 sa_free_attr_string(name); 1297 if (value != NULL) 1298 sa_free_attr_string(value); 1299 return (printed); 1300 } 1301 1302 /* 1303 * nfs_format_options(group, hier) 1304 * 1305 * format all the options on the group into an old-style option 1306 * string. If hier is non-zero, walk up the tree to get inherited 1307 * options. 1308 */ 1309 1310 static char * 1311 nfs_format_options(sa_group_t group, int hier) 1312 { 1313 sa_optionset_t options = NULL; 1314 sa_optionset_t secoptions = NULL; 1315 sa_property_t prop, secprop; 1316 sa_security_t security = NULL; 1317 char *buff; 1318 size_t buffsize; 1319 char *sectype = NULL; 1320 int sep = 0; 1321 1322 1323 buff = malloc(OPT_CHUNK); 1324 if (buff == NULL) { 1325 return (NULL); 1326 } 1327 1328 buff[0] = '\0'; 1329 buffsize = OPT_CHUNK; 1330 1331 /* 1332 * We may have a an optionset relative to this item. format 1333 * these if we find them and then add any security definitions. 1334 */ 1335 1336 options = sa_get_derived_optionset(group, "nfs", hier); 1337 1338 /* 1339 * do the default set first but skip any option that is also 1340 * in the protocol specific optionset. 1341 */ 1342 if (options != NULL) { 1343 for (prop = sa_get_property(options, NULL); 1344 prop != NULL; prop = sa_get_next_property(prop)) { 1345 /* 1346 * use this one since we skipped any 1347 * of these that were also in 1348 * optdefault 1349 */ 1350 if (nfs_sprint_option(&buff, &buffsize, OPT_CHUNK, 1351 prop, sep)) 1352 sep = 1; 1353 if (buff == NULL) { 1354 /* 1355 * buff could become NULL if there 1356 * isn't enough memory for 1357 * nfs_sprint_option to realloc() 1358 * as necessary. We can't really 1359 * do anything about it at this 1360 * point so we return NULL. The 1361 * caller should handle the 1362 * failure. 1363 */ 1364 if (options != NULL) 1365 sa_free_derived_optionset( 1366 options); 1367 return (buff); 1368 } 1369 } 1370 } 1371 secoptions = (sa_optionset_t)sa_get_all_security_types(group, 1372 "nfs", hier); 1373 if (secoptions != NULL) { 1374 for (secprop = sa_get_property(secoptions, NULL); 1375 secprop != NULL; 1376 secprop = sa_get_next_property(secprop)) { 1377 sectype = sa_get_property_attr(secprop, "type"); 1378 security = 1379 (sa_security_t)sa_get_derived_security( 1380 group, sectype, "nfs", hier); 1381 if (security != NULL) { 1382 if (sectype != NULL) { 1383 prop = sa_create_property( 1384 "sec", sectype); 1385 if (prop == NULL) 1386 goto err; 1387 if (nfs_sprint_option(&buff, 1388 &buffsize, OPT_CHUNK, prop, sep)) 1389 sep = 1; 1390 (void) sa_remove_property(prop); 1391 if (buff == NULL) 1392 goto err; 1393 } 1394 for (prop = sa_get_property(security, 1395 NULL); prop != NULL; 1396 prop = sa_get_next_property(prop)) { 1397 if (nfs_sprint_option(&buff, 1398 &buffsize, OPT_CHUNK, prop, sep)) 1399 sep = 1; 1400 if (buff == NULL) 1401 goto err; 1402 } 1403 sa_free_derived_optionset(security); 1404 } 1405 if (sectype != NULL) 1406 sa_free_attr_string(sectype); 1407 } 1408 sa_free_derived_optionset(secoptions); 1409 } 1410 1411 if (options != NULL) 1412 sa_free_derived_optionset(options); 1413 return (buff); 1414 1415 err: 1416 /* 1417 * If we couldn't allocate memory for option printing, we need 1418 * to break out of the nested loops, cleanup and return NULL. 1419 */ 1420 if (secoptions != NULL) 1421 sa_free_derived_optionset(secoptions); 1422 if (security != NULL) 1423 sa_free_derived_optionset(security); 1424 if (sectype != NULL) 1425 sa_free_attr_string(sectype); 1426 if (options != NULL) 1427 sa_free_derived_optionset(options); 1428 return (buff); 1429 } 1430 1431 /* 1432 * Append an entry to the nfslogtab file 1433 */ 1434 static int 1435 nfslogtab_add(dir, buffer, tag) 1436 char *dir, *buffer, *tag; 1437 { 1438 FILE *f; 1439 struct logtab_ent lep; 1440 int error = 0; 1441 1442 /* 1443 * Open the file for update and create it if necessary. 1444 * This may leave the I/O offset at the end of the file, 1445 * so rewind back to the beginning of the file. 1446 */ 1447 f = fopen(NFSLOGTAB, "a+"); 1448 if (f == NULL) { 1449 error = errno; 1450 goto out; 1451 } 1452 rewind(f); 1453 1454 if (lockf(fileno(f), F_LOCK, 0L) < 0) { 1455 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1456 "share complete, however failed to lock %s " 1457 "for update: %s\n"), NFSLOGTAB, strerror(errno)); 1458 error = -1; 1459 goto out; 1460 } 1461 1462 if (logtab_deactivate_after_boot(f) == -1) { 1463 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1464 "share complete, however could not deactivate " 1465 "entries in %s\n"), NFSLOGTAB); 1466 error = -1; 1467 goto out; 1468 } 1469 1470 /* 1471 * Remove entries matching buffer and sharepoint since we're 1472 * going to replace it with perhaps an entry with a new tag. 1473 */ 1474 if (logtab_rement(f, buffer, dir, NULL, -1)) { 1475 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1476 "share complete, however could not remove matching " 1477 "entries in %s\n"), NFSLOGTAB); 1478 error = -1; 1479 goto out; 1480 } 1481 1482 /* 1483 * Deactivate all active entries matching this sharepoint 1484 */ 1485 if (logtab_deactivate(f, NULL, dir, NULL)) { 1486 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1487 "share complete, however could not deactivate matching " 1488 "entries in %s\n"), NFSLOGTAB); 1489 error = -1; 1490 goto out; 1491 } 1492 1493 lep.le_buffer = buffer; 1494 lep.le_path = dir; 1495 lep.le_tag = tag; 1496 lep.le_state = LES_ACTIVE; 1497 1498 /* 1499 * Add new sharepoint / buffer location to nfslogtab 1500 */ 1501 if (logtab_putent(f, &lep) < 0) { 1502 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1503 "share complete, however could not add %s to %s\n"), 1504 dir, NFSLOGTAB); 1505 error = -1; 1506 } 1507 1508 out: 1509 if (f != NULL) 1510 (void) fclose(f); 1511 return (error); 1512 } 1513 1514 /* 1515 * Deactivate an entry from the nfslogtab file 1516 */ 1517 static int 1518 nfslogtab_deactivate(path) 1519 char *path; 1520 { 1521 FILE *f; 1522 int error = 0; 1523 1524 f = fopen(NFSLOGTAB, "r+"); 1525 if (f == NULL) { 1526 error = errno; 1527 goto out; 1528 } 1529 if (lockf(fileno(f), F_LOCK, 0L) < 0) { 1530 error = errno; 1531 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1532 "share complete, however could not lock %s for " 1533 "update: %s\n"), NFSLOGTAB, strerror(error)); 1534 goto out; 1535 } 1536 if (logtab_deactivate(f, NULL, path, NULL) == -1) { 1537 error = -1; 1538 (void) fprintf(stderr, 1539 dgettext(TEXT_DOMAIN, 1540 "share complete, however could not " 1541 "deactivate %s in %s\n"), path, NFSLOGTAB); 1542 goto out; 1543 } 1544 1545 out: if (f != NULL) 1546 (void) fclose(f); 1547 1548 return (error); 1549 } 1550 1551 /* 1552 * check_public(group, skipshare) 1553 * 1554 * Check the group for any shares that have the public property 1555 * enabled. We skip "skipshare" since that is the one we are 1556 * working with. This is a separate function to make handling 1557 * subgroups simpler. Returns true if there is a share with public. 1558 */ 1559 static int 1560 check_public(sa_group_t group, sa_share_t skipshare) 1561 { 1562 int exists = B_FALSE; 1563 sa_share_t share; 1564 sa_optionset_t opt; 1565 sa_property_t prop; 1566 char *shared; 1567 1568 for (share = sa_get_share(group, NULL); share != NULL; 1569 share = sa_get_next_share(share)) { 1570 if (share == skipshare) 1571 continue; 1572 1573 opt = sa_get_optionset(share, "nfs"); 1574 if (opt == NULL) 1575 continue; 1576 prop = sa_get_property(opt, "public"); 1577 if (prop == NULL) 1578 continue; 1579 shared = sa_get_share_attr(share, "shared"); 1580 if (shared != NULL) { 1581 exists = strcmp(shared, "true") == 0; 1582 sa_free_attr_string(shared); 1583 if (exists == B_TRUE) 1584 break; 1585 } 1586 } 1587 1588 return (exists); 1589 } 1590 1591 /* 1592 * public_exists(share) 1593 * 1594 * check to see if public option is set on any other share than the 1595 * one specified. Need to check zfs sub-groups as well as the top 1596 * level groups. 1597 */ 1598 static int 1599 public_exists(sa_share_t skipshare) 1600 { 1601 sa_group_t group; 1602 sa_handle_t handle; 1603 1604 group = sa_get_parent_group(skipshare); 1605 if (group == NULL) 1606 return (SA_NO_SUCH_GROUP); 1607 1608 handle = sa_find_group_handle(group); 1609 if (handle == NULL) 1610 return (SA_SYSTEM_ERR); 1611 1612 for (group = sa_get_group(handle, NULL); group != NULL; 1613 group = sa_get_next_group(group)) { 1614 /* Walk any ZFS subgroups as well as all standard groups */ 1615 if (sa_group_is_zfs(group)) { 1616 sa_group_t subgroup; 1617 for (subgroup = sa_get_sub_group(group); 1618 subgroup != NULL; 1619 subgroup = sa_get_next_group(subgroup)) { 1620 if (check_public(subgroup, skipshare)) 1621 return (B_TRUE); 1622 } 1623 } else { 1624 if (check_public(group, skipshare)) 1625 return (B_TRUE); 1626 } 1627 } 1628 return (B_FALSE); 1629 } 1630 1631 /* 1632 * sa_enable_share at the protocol level, enable_share must tell the 1633 * implementation that it is to enable the share. This entails 1634 * converting the path and options into the appropriate ioctl 1635 * calls. It is assumed that all error checking of paths, etc. were 1636 * done earlier. 1637 */ 1638 static int 1639 nfs_enable_share(sa_share_t share) 1640 { 1641 struct exportdata export; 1642 sa_optionset_t secoptlist; 1643 struct secinfo *sp; 1644 int num_secinfo; 1645 sa_optionset_t opt; 1646 sa_security_t sec; 1647 sa_property_t prop; 1648 char *path; 1649 int err = SA_OK; 1650 int i; 1651 int iszfs; 1652 1653 /* Don't drop core if the NFS module isn't loaded. */ 1654 (void) signal(SIGSYS, SIG_IGN); 1655 1656 /* get the path since it is important in several places */ 1657 path = sa_get_share_attr(share, "path"); 1658 if (path == NULL) 1659 return (SA_NO_SUCH_PATH); 1660 1661 iszfs = sa_path_is_zfs(path); 1662 /* 1663 * find the optionsets and security sets. There may not be 1664 * any or there could be one or two for each of optionset and 1665 * security may have multiple, one per security type per 1666 * protocol type. 1667 */ 1668 opt = sa_get_derived_optionset(share, "nfs", 1); 1669 secoptlist = (sa_optionset_t)sa_get_all_security_types(share, "nfs", 1); 1670 if (secoptlist != NULL) 1671 num_secinfo = MAX(1, count_security(secoptlist)); 1672 else 1673 num_secinfo = 1; 1674 1675 /* 1676 * walk through the options and fill in the structure 1677 * appropriately. 1678 */ 1679 1680 (void) memset(&export, '\0', sizeof (export)); 1681 1682 /* 1683 * do non-security options first since there is only one after 1684 * the derived group is constructed. 1685 */ 1686 export.ex_version = EX_CURRENT_VERSION; 1687 export.ex_anon = UID_NOBODY; /* this is our default value */ 1688 export.ex_index = NULL; 1689 export.ex_path = path; 1690 export.ex_pathlen = strlen(path) + 1; 1691 1692 if (opt != NULL) 1693 err = fill_export_from_optionset(&export, opt); 1694 1695 /* 1696 * check to see if "public" is set. If it is, then make sure 1697 * no other share has it set. If it is already used, fail. 1698 */ 1699 1700 if (export.ex_flags & EX_PUBLIC && public_exists(share)) { 1701 (void) printf(dgettext(TEXT_DOMAIN, 1702 "NFS: Cannot share more than one file " 1703 "system with 'public' property\n")); 1704 err = SA_NOT_ALLOWED; 1705 goto out; 1706 } 1707 1708 sp = calloc(num_secinfo, sizeof (struct secinfo)); 1709 if (sp == NULL) { 1710 err = SA_NO_MEMORY; 1711 (void) printf(dgettext(TEXT_DOMAIN, 1712 "NFS: NFS: no memory for security\n")); 1713 goto out; 1714 } 1715 export.ex_secinfo = sp; 1716 /* get default secinfo */ 1717 export.ex_seccnt = num_secinfo; 1718 /* 1719 * since we must have one security option defined, we 1720 * init to the default and then override as we find 1721 * defined security options. This handles the case 1722 * where we have no defined options but we need to set 1723 * up one. 1724 */ 1725 sp[0].s_window = DEF_WIN; 1726 sp[0].s_rootnames = NULL; 1727 /* setup a default in case no properties defined */ 1728 if (nfs_getseconfig_default(&sp[0].s_secinfo)) { 1729 (void) printf(dgettext(TEXT_DOMAIN, 1730 "NFS: nfs_getseconfig_default: failed to " 1731 "get default security mode\n")); 1732 err = SA_CONFIG_ERR; 1733 } 1734 if (secoptlist != NULL) { 1735 for (i = 0, prop = sa_get_property(secoptlist, NULL); 1736 prop != NULL && i < num_secinfo; 1737 prop = sa_get_next_property(prop), i++) { 1738 char *sectype; 1739 sectype = sa_get_property_attr(prop, "type"); 1740 /* 1741 * if sectype is NULL, we probably 1742 * have a memory problem and can't get 1743 * the correct values. Rather than 1744 * exporting with incorrect security, 1745 * don't share it. 1746 */ 1747 if (sectype == NULL) { 1748 err = SA_NO_MEMORY; 1749 (void) printf(dgettext(TEXT_DOMAIN, 1750 "NFS: Cannot share %s: " 1751 "no memory\n"), path); 1752 goto out; 1753 } 1754 sec = (sa_security_t)sa_get_derived_security( 1755 share, sectype, "nfs", 1); 1756 sp[i].s_window = DEF_WIN; 1757 sp[i].s_rootcnt = 0; 1758 sp[i].s_rootnames = NULL; 1759 (void) fill_security_from_secopts(&sp[i], sec); 1760 if (sec != NULL) 1761 sa_free_derived_security(sec); 1762 if (sectype != NULL) 1763 sa_free_attr_string(sectype); 1764 } 1765 } 1766 /* 1767 * when we get here, we can do the exportfs system call and 1768 * initiate thinsg. We probably want to enable the nfs.server 1769 * service first if it isn't running within SMF. 1770 */ 1771 /* check nfs.server status and start if needed */ 1772 /* now add the share to the internal tables */ 1773 printarg(path, &export); 1774 /* 1775 * call the exportfs system call which is implemented 1776 * via the nfssys() call as the EXPORTFS subfunction. 1777 */ 1778 if (iszfs) { 1779 struct exportfs_args ea; 1780 share_t sh; 1781 char *str; 1782 priv_set_t *priv_effective; 1783 int privileged; 1784 1785 /* 1786 * If we aren't a privileged user 1787 * and NFS server service isn't running 1788 * then print out an error message 1789 * and return EPERM 1790 */ 1791 1792 priv_effective = priv_allocset(); 1793 (void) getppriv(PRIV_EFFECTIVE, priv_effective); 1794 1795 privileged = (priv_isfullset(priv_effective) == B_TRUE); 1796 priv_freeset(priv_effective); 1797 1798 if (!privileged && 1799 (str = smf_get_state(NFS_SERVER_SVC)) != NULL) { 1800 err = 0; 1801 if (strcmp(str, SCF_STATE_STRING_ONLINE) != 0) { 1802 (void) printf(dgettext(TEXT_DOMAIN, 1803 "NFS: Cannot share remote " 1804 "filesystem: %s\n"), path); 1805 (void) printf(dgettext(TEXT_DOMAIN, 1806 "NFS: Service needs to be enabled " 1807 "by a privileged user\n")); 1808 err = SA_SYSTEM_ERR; 1809 errno = EPERM; 1810 } 1811 free(str); 1812 } 1813 1814 if (err == 0) { 1815 ea.dname = path; 1816 ea.uex = &export; 1817 1818 sa_sharetab_fill_zfs(share, &sh, "nfs"); 1819 err = sa_share_zfs(share, path, &sh, 1820 &ea, ZFS_SHARE_NFS); 1821 sa_emptyshare(&sh); 1822 } 1823 } else { 1824 err = exportfs(path, &export); 1825 } 1826 1827 if (err < 0) { 1828 err = SA_SYSTEM_ERR; 1829 switch (errno) { 1830 case EREMOTE: 1831 (void) printf(dgettext(TEXT_DOMAIN, 1832 "NFS: Cannot share filesystems " 1833 "in non-global zones: %s\n"), path); 1834 err = SA_NOT_SUPPORTED; 1835 break; 1836 case EPERM: 1837 if (getzoneid() != GLOBAL_ZONEID) { 1838 (void) printf(dgettext(TEXT_DOMAIN, 1839 "NFS: Cannot share file systems " 1840 "in non-global zones: %s\n"), path); 1841 err = SA_NOT_SUPPORTED; 1842 break; 1843 } 1844 err = SA_NO_PERMISSION; 1845 /* FALLTHROUGH */ 1846 default: 1847 break; 1848 } 1849 } else { 1850 /* update sharetab with an add/modify */ 1851 if (!iszfs) { 1852 (void) sa_update_sharetab(share, "nfs"); 1853 } 1854 } 1855 1856 if (err == SA_OK) { 1857 /* 1858 * enable services as needed. This should probably be 1859 * done elsewhere in order to minimize the calls to 1860 * check services. 1861 */ 1862 /* 1863 * check to see if logging and other services need to 1864 * be triggered, but only if there wasn't an 1865 * error. This is probably where sharetab should be 1866 * updated with the NFS specific entry. 1867 */ 1868 if (export.ex_flags & EX_LOG) { 1869 /* enable logging */ 1870 if (nfslogtab_add(path, export.ex_log_buffer, 1871 export.ex_tag) != 0) { 1872 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1873 "Could not enable logging for %s\n"), 1874 path); 1875 } 1876 _check_services(service_list_logging); 1877 } else { 1878 /* 1879 * don't have logging so remove it from file. It might 1880 * not be thre, but that doesn't matter. 1881 */ 1882 (void) nfslogtab_deactivate(path); 1883 _check_services(service_list_default); 1884 } 1885 } 1886 1887 out: 1888 if (path != NULL) 1889 free(path); 1890 1891 cleanup_export(&export); 1892 if (opt != NULL) 1893 sa_free_derived_optionset(opt); 1894 if (secoptlist != NULL) 1895 (void) sa_destroy_optionset(secoptlist); 1896 return (err); 1897 } 1898 1899 /* 1900 * nfs_disable_share(share, path) 1901 * 1902 * Unshare the specified share. Note that "path" is the same path as 1903 * what is in the "share" object. It is passed in to avoid an 1904 * additional lookup. A missing "path" value makes this a no-op 1905 * function. 1906 */ 1907 static int 1908 nfs_disable_share(sa_share_t share, char *path) 1909 { 1910 int err; 1911 int ret = SA_OK; 1912 int iszfs; 1913 sa_group_t parent; 1914 sa_handle_t handle; 1915 1916 if (path == NULL) 1917 return (ret); 1918 1919 /* 1920 * If the share is in a ZFS group we need to handle it 1921 * differently. Just being on a ZFS file system isn't 1922 * enough since we may be in a legacy share case. 1923 */ 1924 parent = sa_get_parent_group(share); 1925 iszfs = sa_group_is_zfs(parent); 1926 if (iszfs) { 1927 struct exportfs_args ea; 1928 share_t sh = { 0 }; 1929 ea.dname = path; 1930 ea.uex = NULL; 1931 sh.sh_path = path; 1932 sh.sh_fstype = "nfs"; 1933 1934 err = sa_share_zfs(share, path, &sh, 1935 &ea, ZFS_UNSHARE_NFS); 1936 } else { 1937 err = exportfs(path, NULL); 1938 } 1939 if (err < 0) { 1940 /* 1941 * TBD: only an error in some 1942 * cases - need better analysis 1943 */ 1944 switch (errno) { 1945 case EPERM: 1946 case EACCES: 1947 ret = SA_NO_PERMISSION; 1948 if (getzoneid() != GLOBAL_ZONEID) { 1949 ret = SA_NOT_SUPPORTED; 1950 } 1951 break; 1952 case EINVAL: 1953 case ENOENT: 1954 ret = SA_NO_SUCH_PATH; 1955 break; 1956 default: 1957 ret = SA_SYSTEM_ERR; 1958 break; 1959 } 1960 } 1961 if (ret == SA_OK || ret == SA_NO_SUCH_PATH) { 1962 handle = sa_find_group_handle((sa_group_t)share); 1963 if (!iszfs) 1964 (void) sa_delete_sharetab(handle, path, "nfs"); 1965 /* just in case it was logged */ 1966 (void) nfslogtab_deactivate(path); 1967 } 1968 return (ret); 1969 } 1970 1971 /* 1972 * check ro vs rw values. Over time this may get beefed up. 1973 * for now it just does simple checks. 1974 */ 1975 1976 static int 1977 check_rorw(char *v1, char *v2) 1978 { 1979 int ret = SA_OK; 1980 if (strcmp(v1, v2) == 0) 1981 ret = SA_VALUE_CONFLICT; 1982 return (ret); 1983 } 1984 1985 /* 1986 * nfs_validate_property(property, parent) 1987 * 1988 * Check that the property has a legitimate value for its type. 1989 */ 1990 1991 static int 1992 nfs_validate_property(sa_property_t property, sa_optionset_t parent) 1993 { 1994 int ret = SA_OK; 1995 char *propname; 1996 char *other; 1997 int optindex; 1998 nfsl_config_t *configlist; 1999 sa_group_t parent_group; 2000 char *value; 2001 2002 propname = sa_get_property_attr(property, "type"); 2003 2004 if ((optindex = findopt(propname)) < 0) 2005 ret = SA_NO_SUCH_PROP; 2006 2007 /* need to validate value range here as well */ 2008 2009 if (ret == SA_OK) { 2010 parent_group = sa_get_parent_group((sa_share_t)parent); 2011 if (optdefs[optindex].share && !sa_is_share(parent_group)) 2012 ret = SA_PROP_SHARE_ONLY; 2013 } 2014 if (ret == SA_OK) { 2015 value = sa_get_property_attr(property, "value"); 2016 if (value != NULL) { 2017 /* first basic type checking */ 2018 switch (optdefs[optindex].type) { 2019 case OPT_TYPE_NUMBER: 2020 /* check that the value is all digits */ 2021 if (!is_a_number(value)) 2022 ret = SA_BAD_VALUE; 2023 break; 2024 case OPT_TYPE_BOOLEAN: 2025 if (strlen(value) == 0 || 2026 strcasecmp(value, "true") == 0 || 2027 strcmp(value, "1") == 0 || 2028 strcasecmp(value, "false") == 0 || 2029 strcmp(value, "0") == 0) { 2030 ret = SA_OK; 2031 } else { 2032 ret = SA_BAD_VALUE; 2033 } 2034 break; 2035 case OPT_TYPE_USER: 2036 if (!is_a_number(value)) { 2037 struct passwd *pw; 2038 /* 2039 * in this case it would have to be a 2040 * user name 2041 */ 2042 pw = getpwnam(value); 2043 if (pw == NULL) 2044 ret = SA_BAD_VALUE; 2045 endpwent(); 2046 } else { 2047 uint64_t intval; 2048 intval = strtoull(value, NULL, 0); 2049 if (intval > UID_MAX && intval != ~0) 2050 ret = SA_BAD_VALUE; 2051 } 2052 break; 2053 case OPT_TYPE_FILE: 2054 if (strcmp(value, "..") == 0 || 2055 strchr(value, '/') != NULL) { 2056 ret = SA_BAD_VALUE; 2057 } 2058 break; 2059 case OPT_TYPE_ACCLIST: 2060 /* 2061 * access list handling. Should eventually 2062 * validate that all the values make sense. 2063 * Also, ro and rw may have cross value 2064 * conflicts. 2065 */ 2066 if (strcmp(propname, SHOPT_RO) == 0) 2067 other = SHOPT_RW; 2068 else if (strcmp(propname, SHOPT_RW) == 0) 2069 other = SHOPT_RO; 2070 else 2071 other = NULL; 2072 2073 if (other != NULL && parent != NULL) { 2074 /* compare rw(ro) with ro(rw) */ 2075 sa_property_t oprop; 2076 oprop = sa_get_property(parent, other); 2077 if (oprop != NULL) { 2078 /* 2079 * only potential 2080 * confusion if other 2081 * exists 2082 */ 2083 char *ovalue; 2084 ovalue = sa_get_property_attr( 2085 oprop, "value"); 2086 if (ovalue != NULL) { 2087 ret = check_rorw(value, 2088 ovalue); 2089 sa_free_attr_string( 2090 ovalue); 2091 } 2092 } 2093 } 2094 break; 2095 case OPT_TYPE_LOGTAG: 2096 if (nfsl_getconfig_list(&configlist) == 0) { 2097 int error; 2098 if (value == NULL || 2099 strlen(value) == 0) { 2100 if (value != NULL) 2101 sa_free_attr_string( 2102 value); 2103 value = strdup("global"); 2104 } 2105 if (value != NULL && 2106 nfsl_findconfig(configlist, value, 2107 &error) == NULL) { 2108 ret = SA_BAD_VALUE; 2109 } 2110 /* Must always free when done */ 2111 nfsl_freeconfig_list(&configlist); 2112 } else { 2113 ret = SA_CONFIG_ERR; 2114 } 2115 break; 2116 case OPT_TYPE_STRING: 2117 /* whatever is here should be ok */ 2118 break; 2119 case OPT_TYPE_SECURITY: 2120 /* 2121 * The "sec" property isn't used in the 2122 * non-legacy parts of sharemgr. We need to 2123 * reject it here. For legacy, it is pulled 2124 * out well before we get here. 2125 */ 2126 ret = SA_NO_SUCH_PROP; 2127 break; 2128 default: 2129 break; 2130 } 2131 2132 if (value != NULL) 2133 sa_free_attr_string(value); 2134 2135 if (ret == SA_OK && optdefs[optindex].check != NULL) { 2136 /* do the property specific check */ 2137 ret = optdefs[optindex].check(property); 2138 } 2139 } 2140 } 2141 2142 if (propname != NULL) 2143 sa_free_attr_string(propname); 2144 return (ret); 2145 } 2146 2147 /* 2148 * Protocol management functions 2149 * 2150 * Properties defined in the default files are defined in 2151 * proto_option_defs for parsing and validation. If "other" and 2152 * "compare" are set, then the value for this property should be 2153 * compared against the property specified in "other" using the 2154 * "compare" check (either <= or >=) in order to ensure that the 2155 * values are in the correct range. E.g. setting server_versmin 2156 * higher than server_versmax should not be allowed. 2157 */ 2158 2159 struct proto_option_defs { 2160 char *tag; 2161 char *name; /* display name -- remove protocol identifier */ 2162 int index; 2163 int type; 2164 union { 2165 int intval; 2166 char *string; 2167 } defvalue; 2168 uint32_t svcs; 2169 int32_t minval; 2170 int32_t maxval; 2171 char *file; 2172 char *other; 2173 int compare; 2174 #define OPT_CMP_GE 0 2175 #define OPT_CMP_LE 1 2176 int (*check)(char *); 2177 } proto_options[] = { 2178 #define PROTO_OPT_NFSD_SERVERS 0 2179 {"nfsd_servers", 2180 "servers", PROTO_OPT_NFSD_SERVERS, OPT_TYPE_NUMBER, 16, SVC_NFSD, 2181 1, INT32_MAX, NFSADMIN}, 2182 #define PROTO_OPT_LOCKD_LISTEN_BACKLOG 1 2183 {"lockd_listen_backlog", 2184 "lockd_listen_backlog", PROTO_OPT_LOCKD_LISTEN_BACKLOG, 2185 OPT_TYPE_NUMBER, 32, SVC_LOCKD, 32, INT32_MAX, NFSADMIN}, 2186 #define PROTO_OPT_LOCKD_SERVERS 2 2187 {"lockd_servers", 2188 "lockd_servers", PROTO_OPT_LOCKD_SERVERS, OPT_TYPE_NUMBER, 20, 2189 SVC_LOCKD, 1, INT32_MAX, NFSADMIN}, 2190 #define PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT 3 2191 {"lockd_retransmit_timeout", 2192 "lockd_retransmit_timeout", PROTO_OPT_LOCKD_RETRANSMIT_TIMEOUT, 2193 OPT_TYPE_NUMBER, 5, SVC_LOCKD, 0, INT32_MAX, NFSADMIN}, 2194 #define PROTO_OPT_GRACE_PERIOD 4 2195 {"grace_period", 2196 "grace_period", PROTO_OPT_GRACE_PERIOD, OPT_TYPE_NUMBER, 90, 2197 SVC_LOCKD, 0, INT32_MAX, NFSADMIN}, 2198 #define PROTO_OPT_NFS_SERVER_VERSMIN 5 2199 {"nfs_server_versmin", 2200 "server_versmin", PROTO_OPT_NFS_SERVER_VERSMIN, OPT_TYPE_NUMBER, 2201 (int)NFS_VERSMIN_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN, 2202 NFS_VERSMAX, NFSADMIN, "server_versmax", OPT_CMP_LE}, 2203 #define PROTO_OPT_NFS_SERVER_VERSMAX 6 2204 {"nfs_server_versmax", 2205 "server_versmax", PROTO_OPT_NFS_SERVER_VERSMAX, OPT_TYPE_NUMBER, 2206 (int)NFS_VERSMAX_DEFAULT, SVC_NFSD|SVC_MOUNTD, NFS_VERSMIN, 2207 NFS_VERSMAX, NFSADMIN, "server_versmin", OPT_CMP_GE}, 2208 #define PROTO_OPT_NFS_CLIENT_VERSMIN 7 2209 {"nfs_client_versmin", 2210 "client_versmin", PROTO_OPT_NFS_CLIENT_VERSMIN, OPT_TYPE_NUMBER, 2211 (int)NFS_VERSMIN_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX, 2212 NFSADMIN, "client_versmax", OPT_CMP_LE}, 2213 #define PROTO_OPT_NFS_CLIENT_VERSMAX 8 2214 {"nfs_client_versmax", 2215 "client_versmax", PROTO_OPT_NFS_CLIENT_VERSMAX, OPT_TYPE_NUMBER, 2216 (int)NFS_VERSMAX_DEFAULT, NULL, NFS_VERSMIN, NFS_VERSMAX, 2217 NFSADMIN, "client_versmin", OPT_CMP_GE}, 2218 #define PROTO_OPT_NFS_SERVER_DELEGATION 9 2219 {"nfs_server_delegation", 2220 "server_delegation", PROTO_OPT_NFS_SERVER_DELEGATION, 2221 OPT_TYPE_ONOFF, NFS_SERVER_DELEGATION_DEFAULT, SVC_NFSD, 0, 0, 2222 NFSADMIN}, 2223 #define PROTO_OPT_NFSMAPID_DOMAIN 10 2224 {"nfsmapid_domain", 2225 "nfsmapid_domain", PROTO_OPT_NFSMAPID_DOMAIN, OPT_TYPE_DOMAIN, 2226 NULL, SVC_NFSMAPID, 0, 0, NFSADMIN}, 2227 #define PROTO_OPT_NFSD_MAX_CONNECTIONS 11 2228 {"nfsd_max_connections", 2229 "max_connections", PROTO_OPT_NFSD_MAX_CONNECTIONS, 2230 OPT_TYPE_NUMBER, -1, SVC_NFSD, -1, INT32_MAX, NFSADMIN}, 2231 #define PROTO_OPT_NFSD_PROTOCOL 12 2232 {"nfsd_protocol", 2233 "protocol", PROTO_OPT_NFSD_PROTOCOL, OPT_TYPE_PROTOCOL, 0, 2234 SVC_NFSD, 0, 0, NFSADMIN}, 2235 #define PROTO_OPT_NFSD_LISTEN_BACKLOG 13 2236 {"nfsd_listen_backlog", 2237 "listen_backlog", PROTO_OPT_NFSD_LISTEN_BACKLOG, 2238 OPT_TYPE_NUMBER, 0, 2239 SVC_LOCKD, 0, INT32_MAX, NFSADMIN}, 2240 {NULL} 2241 }; 2242 2243 /* 2244 * the protoset holds the defined options so we don't have to read 2245 * them multiple times 2246 */ 2247 static sa_protocol_properties_t protoset; 2248 2249 static int 2250 findprotoopt(char *name, int whichname) 2251 { 2252 int i; 2253 for (i = 0; proto_options[i].tag != NULL; i++) { 2254 if (whichname == 1) { 2255 if (strcasecmp(proto_options[i].name, name) == 0) 2256 return (i); 2257 } else { 2258 if (strcasecmp(proto_options[i].tag, name) == 0) 2259 return (i); 2260 } 2261 } 2262 return (-1); 2263 } 2264 2265 /* 2266 * fixcaselower(str) 2267 * 2268 * convert a string to lower case (inplace). 2269 */ 2270 2271 static void 2272 fixcaselower(char *str) 2273 { 2274 while (*str) { 2275 *str = tolower(*str); 2276 str++; 2277 } 2278 } 2279 2280 /* 2281 * fixcaseupper(str) 2282 * 2283 * convert a string to upper case (inplace). 2284 */ 2285 2286 static void 2287 fixcaseupper(char *str) 2288 { 2289 while (*str) { 2290 *str = toupper(*str); 2291 str++; 2292 } 2293 } 2294 2295 /* 2296 * skipwhitespace(str) 2297 * 2298 * Skip leading white space. It is assumed that it is called with a 2299 * valid pointer. 2300 */ 2301 2302 static char * 2303 skipwhitespace(char *str) 2304 { 2305 while (*str && isspace(*str)) 2306 str++; 2307 2308 return (str); 2309 } 2310 2311 /* 2312 * extractprop() 2313 * 2314 * Extract the property and value out of the line and create the 2315 * property in the optionset. 2316 */ 2317 static void 2318 extractprop(char *name, char *value) 2319 { 2320 sa_property_t prop; 2321 int index; 2322 /* 2323 * Remove any leading 2324 * white space. 2325 */ 2326 name = skipwhitespace(name); 2327 2328 index = findprotoopt(name, 0); 2329 if (index >= 0) { 2330 fixcaselower(name); 2331 prop = sa_create_property(proto_options[index].name, value); 2332 if (prop != NULL) 2333 (void) sa_add_protocol_property(protoset, prop); 2334 } 2335 } 2336 2337 /* 2338 * initprotofromdefault() 2339 * 2340 * read the default file(s) and add the defined values to the 2341 * protoset. Note that default values are known from the built in 2342 * table in case the file doesn't have a definition. 2343 */ 2344 2345 static int 2346 initprotofromdefault() 2347 { 2348 FILE *nfs; 2349 char buff[BUFSIZ]; 2350 char *name; 2351 char *value; 2352 2353 protoset = sa_create_protocol_properties("nfs"); 2354 2355 if (protoset != NULL) { 2356 nfs = fopen(NFSADMIN, "r"); 2357 if (nfs != NULL) { 2358 while (fgets(buff, sizeof (buff), nfs) != NULL) { 2359 switch (buff[0]) { 2360 case '\n': 2361 case '#': 2362 /* skip */ 2363 break; 2364 default: 2365 name = buff; 2366 buff[strlen(buff) - 1] = '\0'; 2367 value = strchr(name, '='); 2368 if (value != NULL) { 2369 *value++ = '\0'; 2370 extractprop(name, value); 2371 } 2372 } 2373 } 2374 if (nfs != NULL) 2375 (void) fclose(nfs); 2376 } 2377 } 2378 if (protoset == NULL) 2379 return (SA_NO_MEMORY); 2380 return (SA_OK); 2381 } 2382 2383 /* 2384 * add_defaults() 2385 * 2386 * Add the default values for any property not defined in the parsing 2387 * of the default files. Values are set according to their defined 2388 * types. 2389 */ 2390 2391 static void 2392 add_defaults() 2393 { 2394 int i; 2395 char number[MAXDIGITS]; 2396 2397 for (i = 0; proto_options[i].tag != NULL; i++) { 2398 sa_property_t prop; 2399 prop = sa_get_protocol_property(protoset, 2400 proto_options[i].name); 2401 if (prop == NULL) { 2402 /* add the default value */ 2403 switch (proto_options[i].type) { 2404 case OPT_TYPE_NUMBER: 2405 (void) snprintf(number, sizeof (number), "%d", 2406 proto_options[i].defvalue.intval); 2407 prop = sa_create_property(proto_options[i].name, 2408 number); 2409 break; 2410 2411 case OPT_TYPE_BOOLEAN: 2412 prop = sa_create_property(proto_options[i].name, 2413 proto_options[i].defvalue.intval ? 2414 "true" : "false"); 2415 break; 2416 2417 case OPT_TYPE_ONOFF: 2418 prop = sa_create_property(proto_options[i].name, 2419 proto_options[i].defvalue.intval ? 2420 "on" : "off"); 2421 break; 2422 2423 default: 2424 /* treat as strings of zero length */ 2425 prop = sa_create_property(proto_options[i].name, 2426 ""); 2427 break; 2428 } 2429 if (prop != NULL) 2430 (void) sa_add_protocol_property(protoset, prop); 2431 } 2432 } 2433 } 2434 2435 static void 2436 free_protoprops() 2437 { 2438 if (protoset != NULL) { 2439 xmlFreeNode(protoset); 2440 protoset = NULL; 2441 } 2442 } 2443 2444 /* 2445 * nfs_init() 2446 * 2447 * Initialize the NFS plugin. 2448 */ 2449 2450 static int 2451 nfs_init() 2452 { 2453 int ret = SA_OK; 2454 2455 if (sa_plugin_ops.sa_init != nfs_init) 2456 (void) printf(dgettext(TEXT_DOMAIN, 2457 "NFS plugin not properly initialized\n")); 2458 2459 ret = initprotofromdefault(); 2460 if (ret == SA_OK) 2461 add_defaults(); 2462 2463 return (ret); 2464 } 2465 2466 /* 2467 * nfs_fini() 2468 * 2469 * uninitialize the NFS plugin. Want to avoid memory leaks. 2470 */ 2471 2472 static void 2473 nfs_fini() 2474 { 2475 free_protoprops(); 2476 } 2477 2478 /* 2479 * nfs_get_proto_set() 2480 * 2481 * Return an optionset with all the protocol specific properties in 2482 * it. 2483 */ 2484 2485 static sa_protocol_properties_t 2486 nfs_get_proto_set() 2487 { 2488 return (protoset); 2489 } 2490 2491 struct deffile { 2492 struct deffile *next; 2493 char *line; 2494 }; 2495 2496 /* 2497 * read_default_file(fname) 2498 * 2499 * Read the specified default file. We return a list of entries. This 2500 * get used for adding or removing values. 2501 */ 2502 2503 static struct deffile * 2504 read_default_file(char *fname) 2505 { 2506 FILE *file; 2507 struct deffile *defs = NULL; 2508 struct deffile *newdef; 2509 struct deffile *prevdef = NULL; 2510 char buff[BUFSIZ * 2]; 2511 2512 file = fopen(fname, "r"); 2513 if (file != NULL) { 2514 while (fgets(buff, sizeof (buff), file) != NULL) { 2515 newdef = (struct deffile *)calloc(1, 2516 sizeof (struct deffile)); 2517 if (newdef != NULL) { 2518 /* Make sure we skip any leading whitespace. */ 2519 newdef->line = strdup(skipwhitespace(buff)); 2520 if (defs == NULL) { 2521 prevdef = defs = newdef; 2522 } else { 2523 prevdef->next = newdef; 2524 prevdef = newdef; 2525 } 2526 } 2527 } 2528 } 2529 (void) fclose(file); 2530 return (defs); 2531 } 2532 2533 static void 2534 free_default_file(struct deffile *defs) 2535 { 2536 struct deffile *curdefs = NULL; 2537 2538 while (defs != NULL) { 2539 curdefs = defs; 2540 defs = defs->next; 2541 if (curdefs->line != NULL) 2542 free(curdefs->line); 2543 free(curdefs); 2544 } 2545 } 2546 2547 /* 2548 * write_default_file(fname, defs) 2549 * 2550 * Write the default file back. 2551 */ 2552 2553 static int 2554 write_default_file(char *fname, struct deffile *defs) 2555 { 2556 FILE *file; 2557 int ret = SA_OK; 2558 sigset_t old, new; 2559 2560 file = fopen(fname, "w+"); 2561 if (file != NULL) { 2562 (void) sigprocmask(SIG_BLOCK, NULL, &new); 2563 (void) sigaddset(&new, SIGHUP); 2564 (void) sigaddset(&new, SIGINT); 2565 (void) sigaddset(&new, SIGQUIT); 2566 (void) sigaddset(&new, SIGTSTP); 2567 (void) sigprocmask(SIG_SETMASK, &new, &old); 2568 while (defs != NULL) { 2569 (void) fputs(defs->line, file); 2570 defs = defs->next; 2571 } 2572 (void) fsync(fileno(file)); 2573 (void) sigprocmask(SIG_SETMASK, &old, NULL); 2574 (void) fclose(file); 2575 } else { 2576 switch (errno) { 2577 case EPERM: 2578 case EACCES: 2579 ret = SA_NO_PERMISSION; 2580 break; 2581 default: 2582 ret = SA_SYSTEM_ERR; 2583 } 2584 } 2585 return (ret); 2586 } 2587 2588 2589 /* 2590 * set_default_file_value(tag, value) 2591 * 2592 * Set the default file value for tag to value. Then rewrite the file. 2593 * tag and value are always set. The caller must ensure this. 2594 */ 2595 2596 #define MAX_STRING_LENGTH 256 2597 static int 2598 set_default_file_value(char *tag, char *value) 2599 { 2600 int ret = SA_OK; 2601 struct deffile *root; 2602 struct deffile *defs; 2603 struct deffile *prev; 2604 char string[MAX_STRING_LENGTH]; 2605 int len; 2606 int update = 0; 2607 2608 (void) snprintf(string, MAX_STRING_LENGTH, "%s=", tag); 2609 len = strlen(string); 2610 2611 root = defs = read_default_file(NFSADMIN); 2612 if (root == NULL) { 2613 if (errno == EPERM || errno == EACCES) 2614 ret = SA_NO_PERMISSION; 2615 else 2616 ret = SA_SYSTEM_ERR; 2617 } else { 2618 while (defs != NULL) { 2619 if (defs->line != NULL && 2620 strncasecmp(defs->line, string, len) == 0) { 2621 /* replace with the new value */ 2622 free(defs->line); 2623 fixcaseupper(tag); 2624 (void) snprintf(string, sizeof (string), 2625 "%s=%s\n", tag, value); 2626 string[MAX_STRING_LENGTH - 1] = '\0'; 2627 defs->line = strdup(string); 2628 update = 1; 2629 break; 2630 } 2631 defs = defs->next; 2632 } 2633 if (!update) { 2634 defs = root; 2635 /* didn't find, so see if it is a comment */ 2636 (void) snprintf(string, MAX_STRING_LENGTH, "#%s=", tag); 2637 len = strlen(string); 2638 while (defs != NULL) { 2639 if (strncasecmp(defs->line, string, len) == 0) { 2640 /* replace with the new value */ 2641 free(defs->line); 2642 fixcaseupper(tag); 2643 (void) snprintf(string, sizeof (string), 2644 "%s=%s\n", tag, value); 2645 string[MAX_STRING_LENGTH - 1] = '\0'; 2646 defs->line = strdup(string); 2647 update = 1; 2648 break; 2649 } 2650 defs = defs->next; 2651 } 2652 } 2653 if (!update) { 2654 fixcaseupper(tag); 2655 (void) snprintf(string, sizeof (string), "%s=%s\n", 2656 tag, value); 2657 prev = root; 2658 while (prev->next != NULL) 2659 prev = prev->next; 2660 defs = malloc(sizeof (struct deffile)); 2661 prev->next = defs; 2662 if (defs != NULL) { 2663 defs->next = NULL; 2664 defs->line = strdup(string); 2665 } 2666 } 2667 if (update) { 2668 ret = write_default_file(NFSADMIN, root); 2669 } 2670 free_default_file(root); 2671 } 2672 return (ret); 2673 } 2674 2675 /* 2676 * service_in_state(service, chkstate) 2677 * 2678 * Want to know if the specified service is in the desired state 2679 * (chkstate) or not. Return true (1) if it is and false (0) if it 2680 * isn't. 2681 */ 2682 static int 2683 service_in_state(char *service, const char *chkstate) 2684 { 2685 char *state; 2686 int ret = B_FALSE; 2687 2688 state = smf_get_state(service); 2689 if (state != NULL) { 2690 /* got the state so get the equality for the return value */ 2691 ret = strcmp(state, chkstate) == 0 ? B_TRUE : B_FALSE; 2692 free(state); 2693 } 2694 return (ret); 2695 } 2696 2697 /* 2698 * restart_service(svcs) 2699 * 2700 * Walk through the bit mask of services that need to be restarted in 2701 * order to use the new property values. Some properties affect 2702 * multiple daemons. Should only restart a service if it is currently 2703 * enabled (online). 2704 */ 2705 2706 static void 2707 restart_service(uint32_t svcs) 2708 { 2709 uint32_t mask; 2710 int ret; 2711 char *service; 2712 2713 for (mask = 1; svcs != 0; mask <<= 1) { 2714 switch (svcs & mask) { 2715 case SVC_LOCKD: 2716 service = LOCKD; 2717 break; 2718 case SVC_STATD: 2719 service = STATD; 2720 break; 2721 case SVC_NFSD: 2722 service = NFSD; 2723 break; 2724 case SVC_MOUNTD: 2725 service = MOUNTD; 2726 break; 2727 case SVC_NFS4CBD: 2728 service = NFS4CBD; 2729 break; 2730 case SVC_NFSMAPID: 2731 service = NFSMAPID; 2732 break; 2733 case SVC_RQUOTAD: 2734 service = RQUOTAD; 2735 break; 2736 case SVC_NFSLOGD: 2737 service = NFSLOGD; 2738 break; 2739 default: 2740 continue; 2741 } 2742 2743 /* 2744 * Only attempt to restart the service if it is 2745 * currently running. In the future, it may be 2746 * desirable to use smf_refresh_instance if the NFS 2747 * services ever implement the refresh method. 2748 */ 2749 if (service_in_state(service, SCF_STATE_STRING_ONLINE)) { 2750 ret = smf_restart_instance(service); 2751 /* 2752 * There are only a few SMF errors at this point, but 2753 * it is also possible that a bad value may have put 2754 * the service into maintenance if there wasn't an 2755 * SMF level error. 2756 */ 2757 if (ret != 0) { 2758 (void) fprintf(stderr, 2759 dgettext(TEXT_DOMAIN, 2760 "%s failed to restart: %s\n"), 2761 scf_strerror(scf_error())); 2762 } else { 2763 /* 2764 * Check whether it has gone to "maintenance" 2765 * mode or not. Maintenance implies something 2766 * went wrong. 2767 */ 2768 if (service_in_state(service, 2769 SCF_STATE_STRING_MAINT)) { 2770 (void) fprintf(stderr, 2771 dgettext(TEXT_DOMAIN, 2772 "%s failed to restart\n"), 2773 service); 2774 } 2775 } 2776 } 2777 svcs &= ~mask; 2778 } 2779 } 2780 2781 /* 2782 * nfs_minmax_check(name, value) 2783 * 2784 * Verify that the value for the property specified by index is valid 2785 * relative to the opposite value in the case of a min/max variable. 2786 * Currently, server_minvers/server_maxvers and 2787 * client_minvers/client_maxvers are the only ones to check. 2788 */ 2789 2790 static int 2791 nfs_minmax_check(int index, int value) 2792 { 2793 int val; 2794 char *pval; 2795 sa_property_t prop; 2796 sa_optionset_t opts; 2797 int ret = B_TRUE; 2798 2799 if (proto_options[index].other != NULL) { 2800 /* have a property to compare against */ 2801 opts = nfs_get_proto_set(); 2802 prop = sa_get_property(opts, proto_options[index].other); 2803 /* 2804 * If we don't find the property, assume default 2805 * values which will work since the max will be at the 2806 * max and the min at the min. 2807 */ 2808 if (prop != NULL) { 2809 pval = sa_get_property_attr(prop, "value"); 2810 if (pval != NULL) { 2811 val = strtoul(pval, NULL, 0); 2812 if (proto_options[index].compare == 2813 OPT_CMP_LE) { 2814 ret = value <= val ? B_TRUE : B_FALSE; 2815 } else if (proto_options[index].compare == 2816 OPT_CMP_GE) { 2817 ret = value >= val ? B_TRUE : B_FALSE; 2818 } 2819 } 2820 } 2821 } 2822 return (ret); 2823 } 2824 2825 /* 2826 * nfs_validate_proto_prop(index, name, value) 2827 * 2828 * Verify that the property specified by name can take the new 2829 * value. This is a sanity check to prevent bad values getting into 2830 * the default files. All values need to be checked against what is 2831 * allowed by their defined type. If a type isn't explicitly defined 2832 * here, it is treated as a string. 2833 * 2834 * Note that OPT_TYPE_NUMBER will additionally check that the value is 2835 * within the range specified and potentially against another property 2836 * value as well as specified in the proto_options members other and 2837 * compare. 2838 */ 2839 2840 static int 2841 nfs_validate_proto_prop(int index, char *name, char *value) 2842 { 2843 int ret = SA_OK; 2844 char *cp; 2845 #ifdef lint 2846 name = name; 2847 #endif 2848 2849 switch (proto_options[index].type) { 2850 case OPT_TYPE_NUMBER: 2851 if (!is_a_number(value)) 2852 ret = SA_BAD_VALUE; 2853 else { 2854 int val; 2855 val = strtoul(value, NULL, 0); 2856 if (val < proto_options[index].minval || 2857 val > proto_options[index].maxval) 2858 ret = SA_BAD_VALUE; 2859 /* 2860 * For server_versmin/server_versmax and 2861 * client_versmin/client_versmax, the value of the 2862 * min(max) should be checked to be correct relative 2863 * to the current max(min). 2864 */ 2865 if (!nfs_minmax_check(index, val)) { 2866 ret = SA_BAD_VALUE; 2867 } 2868 } 2869 break; 2870 2871 case OPT_TYPE_DOMAIN: 2872 /* 2873 * needs to be a qualified domain so will have at 2874 * least one period and other characters on either 2875 * side of it. A zero length string is also allowed 2876 * and is the way to turn off the override. 2877 */ 2878 if (strlen(value) == 0) 2879 break; 2880 cp = strchr(value, '.'); 2881 if (cp == NULL || cp == value || strchr(value, '@') != NULL) 2882 ret = SA_BAD_VALUE; 2883 break; 2884 2885 case OPT_TYPE_BOOLEAN: 2886 if (strlen(value) == 0 || 2887 strcasecmp(value, "true") == 0 || 2888 strcmp(value, "1") == 0 || 2889 strcasecmp(value, "false") == 0 || 2890 strcmp(value, "0") == 0) { 2891 ret = SA_OK; 2892 } else { 2893 ret = SA_BAD_VALUE; 2894 } 2895 break; 2896 2897 case OPT_TYPE_ONOFF: 2898 if (strcasecmp(value, "on") != 0 && 2899 strcasecmp(value, "off") != 0) { 2900 ret = SA_BAD_VALUE; 2901 } 2902 break; 2903 2904 case OPT_TYPE_PROTOCOL: 2905 if (strcasecmp(value, "all") != 0 && 2906 strcasecmp(value, "tcp") != 0 && 2907 strcasecmp(value, "udp") != 0) 2908 ret = SA_BAD_VALUE; 2909 break; 2910 2911 default: 2912 /* treat as a string */ 2913 break; 2914 } 2915 return (ret); 2916 } 2917 2918 /* 2919 * nfs_set_proto_prop(prop) 2920 * 2921 * check that prop is valid. 2922 */ 2923 2924 static int 2925 nfs_set_proto_prop(sa_property_t prop) 2926 { 2927 int ret = SA_OK; 2928 char *name; 2929 char *value; 2930 2931 name = sa_get_property_attr(prop, "type"); 2932 value = sa_get_property_attr(prop, "value"); 2933 if (name != NULL && value != NULL) { 2934 int index = findprotoopt(name, 1); 2935 if (index >= 0) { 2936 /* should test for valid value */ 2937 ret = nfs_validate_proto_prop(index, name, value); 2938 if (ret == SA_OK) 2939 ret = set_default_file_value( 2940 proto_options[index].tag, value); 2941 if (ret == SA_OK) 2942 restart_service(proto_options[index].svcs); 2943 } 2944 } 2945 if (name != NULL) 2946 sa_free_attr_string(name); 2947 if (value != NULL) 2948 sa_free_attr_string(value); 2949 return (ret); 2950 } 2951 2952 /* 2953 * nfs_get_status() 2954 * 2955 * What is the current status of the nfsd? We use the SMF state here. 2956 * Caller must free the returned value. 2957 */ 2958 2959 static char * 2960 nfs_get_status() 2961 { 2962 char *state; 2963 state = smf_get_state(NFSD); 2964 return (state != NULL ? state : strdup("-")); 2965 } 2966 2967 /* 2968 * nfs_space_alias(alias) 2969 * 2970 * Lookup the space (security) name. If it is default, convert to the 2971 * real name. 2972 */ 2973 2974 static char * 2975 nfs_space_alias(char *space) 2976 { 2977 char *name = space; 2978 seconfig_t secconf; 2979 2980 /* 2981 * Only the space named "default" is special. If it is used, 2982 * the default needs to be looked up and the real name used. 2983 * This is normally "sys" but could be changed. We always 2984 * change defautl to the real name. 2985 */ 2986 if (strcmp(space, "default") == 0 && 2987 nfs_getseconfig_default(&secconf) == 0) { 2988 if (nfs_getseconfig_bynumber(secconf.sc_nfsnum, &secconf) == 0) 2989 name = secconf.sc_name; 2990 } 2991 return (strdup(name)); 2992 } 2993 2994 /* 2995 * nfs_features() 2996 * 2997 * Return a mask of the features required. 2998 */ 2999 3000 static uint64_t 3001 nfs_features() 3002 { 3003 return ((uint64_t)SA_FEATURE_DFSTAB); 3004 } 3005