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