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 * SMB 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 <fcntl.h> 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <syslog.h> 44 #include "libshare.h" 45 #include "libshare_impl.h" 46 #include <pwd.h> 47 #include <limits.h> 48 #include <libscf.h> 49 #include <strings.h> 50 #include "libshare_smb.h" 51 #include <rpcsvc/daemon_utils.h> 52 #include <smbsrv/lmshare.h> 53 #include <smbsrv/lmshare_door.h> 54 #include <smbsrv/smbinfo.h> 55 #include <smbsrv/libsmb.h> 56 57 /* internal functions */ 58 static int smb_share_init(void); 59 static void smb_share_fini(void); 60 static int smb_enable_share(sa_share_t); 61 static int smb_share_changed(sa_share_t); 62 static int smb_resource_changed(sa_resource_t); 63 static int smb_rename_resource(sa_handle_t, sa_resource_t, char *); 64 static int smb_disable_share(sa_share_t share, char *); 65 static int smb_validate_property(sa_handle_t, sa_property_t, sa_optionset_t); 66 static int smb_set_proto_prop(sa_property_t); 67 static sa_protocol_properties_t smb_get_proto_set(void); 68 static char *smb_get_status(void); 69 static int smb_parse_optstring(sa_group_t, char *); 70 static char *smb_format_options(sa_group_t, int); 71 72 static int smb_enable_service(void); 73 74 static int range_check_validator(int, char *); 75 static int range_check_validator_zero_ok(int, char *); 76 static int string_length_check_validator(int, char *); 77 static int true_false_validator(int, char *); 78 static int ip_address_validator_empty_ok(int, char *); 79 static int ip_address_csv_list_validator_empty_ok(int, char *); 80 static int path_validator(int, char *); 81 82 static int smb_enable_resource(sa_resource_t); 83 static int smb_disable_resource(sa_resource_t); 84 static uint64_t smb_share_features(void); 85 static int smb_list_transient(sa_handle_t); 86 87 extern void lmshrd_door_close(void); 88 89 /* size of basic format allocation */ 90 #define OPT_CHUNK 1024 91 92 /* size of string for types - big enough to hold "dependency" */ 93 #define SCFTYPE_LEN 32 94 95 /* 96 * Indexes of entries in smb_proto_options table. 97 * Changes to smb_proto_options table may require 98 * an update to these values. 99 */ 100 #define PROTO_OPT_WINS1 6 101 #define PROTO_OPT_WINS_EXCLUDE 8 102 103 104 /* 105 * ops vector that provides the protocol specific info and operations 106 * for share management. 107 */ 108 109 struct sa_plugin_ops sa_plugin_ops = { 110 SA_PLUGIN_VERSION, 111 SMB_PROTOCOL_NAME, 112 smb_share_init, 113 smb_share_fini, 114 smb_enable_share, 115 smb_disable_share, 116 smb_validate_property, 117 NULL, /* valid_space */ 118 NULL, /* security_prop */ 119 smb_parse_optstring, 120 smb_format_options, 121 smb_set_proto_prop, 122 smb_get_proto_set, 123 smb_get_status, 124 NULL, /* space_alias */ 125 NULL, /* update_legacy */ 126 NULL, /* delete_legacy */ 127 smb_share_changed, 128 smb_enable_resource, 129 smb_disable_resource, 130 smb_share_features, 131 smb_list_transient, 132 smb_resource_changed, 133 smb_rename_resource, 134 NULL, /* run_command */ 135 NULL, /* command_help */ 136 NULL /* delete_proto_section */ 137 }; 138 139 /* 140 * option definitions. Make sure to keep the #define for the option 141 * index just before the entry it is the index for. Changing the order 142 * can cause breakage. 143 */ 144 145 struct option_defs optdefs[] = { 146 {SHOPT_AD_CONTAINER, OPT_TYPE_STRING}, 147 {SHOPT_NAME, OPT_TYPE_NAME}, 148 {NULL, NULL}, 149 }; 150 151 /* 152 * findopt(name) 153 * 154 * Lookup option "name" in the option table and return the table 155 * index. 156 */ 157 158 static int 159 findopt(char *name) 160 { 161 int i; 162 if (name != NULL) { 163 for (i = 0; optdefs[i].tag != NULL; i++) { 164 if (strcmp(optdefs[i].tag, name) == 0) 165 return (i); 166 } 167 } 168 return (-1); 169 } 170 171 /* 172 * is_a_number(number) 173 * 174 * is the string a number in one of the forms we want to use? 175 */ 176 177 static int 178 is_a_number(char *number) 179 { 180 int ret = 1; 181 int hex = 0; 182 183 if (strncmp(number, "0x", 2) == 0) { 184 number += 2; 185 hex = 1; 186 } else if (*number == '-') { 187 number++; /* skip the minus */ 188 } 189 190 while (ret == 1 && *number != '\0') { 191 if (hex) { 192 ret = isxdigit(*number++); 193 } else { 194 ret = isdigit(*number++); 195 } 196 } 197 return (ret); 198 } 199 200 /* 201 * validresource(name) 202 * 203 * Check that name only has valid characters in it. The current valid 204 * set are the printable characters but not including: 205 * " / \ [ ] : | < > + ; , ? * = \t 206 * Note that space is included and there is a maximum length. 207 */ 208 static int 209 validresource(const char *name) 210 { 211 const char *cp; 212 size_t len; 213 214 if (name == NULL) 215 return (B_FALSE); 216 217 len = strlen(name); 218 if (len == 0 || len > SA_MAX_RESOURCE_NAME) 219 return (B_FALSE); 220 221 if (strpbrk(name, "\"/\\[]:|<>+;,?*=\t") != NULL) { 222 return (B_FALSE); 223 } 224 225 for (cp = name; *cp != '\0'; cp++) 226 if (iscntrl(*cp)) 227 return (B_FALSE); 228 229 return (B_TRUE); 230 } 231 232 /* 233 * smb_isonline() 234 * 235 * Determine if the SMF service instance is in the online state or 236 * not. A number of operations depend on this state. 237 */ 238 static boolean_t 239 smb_isonline(void) 240 { 241 char *str; 242 boolean_t ret = B_FALSE; 243 244 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) { 245 ret = (strcmp(str, SCF_STATE_STRING_ONLINE) == 0); 246 free(str); 247 } 248 return (ret); 249 } 250 251 /* 252 * smb_isdisabled() 253 * 254 * Determine if the SMF service instance is in the disabled state or 255 * not. A number of operations depend on this state. 256 */ 257 static boolean_t 258 smb_isdisabled(void) 259 { 260 char *str; 261 boolean_t ret = B_FALSE; 262 263 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) { 264 ret = (strcmp(str, SCF_STATE_STRING_DISABLED) == 0); 265 free(str); 266 } 267 return (ret); 268 } 269 270 /* 271 * smb_isautoenable() 272 * 273 * Determine if the SMF service instance auto_enabled set or not. A 274 * number of operations depend on this state. The property not being 275 * set or being set to true means autoenable. Only being set to false 276 * is not autoenabled. 277 */ 278 static boolean_t 279 smb_isautoenable(void) 280 { 281 boolean_t ret = B_TRUE; 282 scf_simple_prop_t *prop; 283 uint8_t *retstr; 284 285 prop = scf_simple_prop_get(NULL, SMBD_DEFAULT_INSTANCE_FMRI, 286 "application", "auto_enable"); 287 if (prop != NULL) { 288 retstr = scf_simple_prop_next_boolean(prop); 289 ret = *retstr != 0; 290 scf_simple_prop_free(prop); 291 } 292 return (ret); 293 } 294 295 /* 296 * smb_ismaint() 297 * 298 * Determine if the SMF service instance is in the disabled state or 299 * not. A number of operations depend on this state. 300 */ 301 static boolean_t 302 smb_ismaint(void) 303 { 304 char *str; 305 boolean_t ret = B_FALSE; 306 307 if ((str = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI)) != NULL) { 308 ret = (strcmp(str, SCF_STATE_STRING_MAINT) == 0); 309 free(str); 310 } 311 return (ret); 312 } 313 314 /* 315 * smb_enable_share tells the implementation that it is to enable the share. 316 * This entails converting the path and options into the appropriate ioctl 317 * calls. It is assumed that all error checking of paths, etc. were 318 * done earlier. 319 */ 320 static int 321 smb_enable_share(sa_share_t share) 322 { 323 char *path; 324 char *rname; 325 lmshare_info_t si; 326 sa_resource_t resource; 327 boolean_t iszfs; 328 boolean_t privileged; 329 int err = SA_OK; 330 priv_set_t *priv_effective; 331 boolean_t online; 332 333 /* 334 * We only start in the global zone and only run if we aren't 335 * running Trusted Extensions. 336 */ 337 if (getzoneid() != GLOBAL_ZONEID) { 338 (void) printf(dgettext(TEXT_DOMAIN, 339 "SMB: service not supported in local zone\n")); 340 return (SA_NOT_SUPPORTED); 341 } 342 if (is_system_labeled()) { 343 (void) printf(dgettext(TEXT_DOMAIN, 344 "SMB: service not supported with Trusted Extensions\n")); 345 return (SA_NOT_SUPPORTED); 346 } 347 348 priv_effective = priv_allocset(); 349 (void) getppriv(PRIV_EFFECTIVE, priv_effective); 350 privileged = (priv_isfullset(priv_effective) == B_TRUE); 351 priv_freeset(priv_effective); 352 353 /* get the path since it is important in several places */ 354 path = sa_get_share_attr(share, "path"); 355 if (path == NULL) 356 return (SA_NO_SUCH_PATH); 357 358 /* 359 * If administratively disabled, don't try to start anything. 360 */ 361 online = smb_isonline(); 362 if (!online && !smb_isautoenable() && smb_isdisabled()) 363 goto done; 364 365 iszfs = sa_path_is_zfs(path); 366 367 if (iszfs) { 368 369 if (privileged == B_FALSE && !online) { 370 371 if (!online) { 372 (void) printf(dgettext(TEXT_DOMAIN, 373 "SMB: Cannot share remove " 374 "file system: %s\n"), path); 375 (void) printf(dgettext(TEXT_DOMAIN, 376 "SMB: Service needs to be enabled " 377 "by a privileged user\n")); 378 err = SA_NO_PERMISSION; 379 errno = EPERM; 380 } 381 if (err) { 382 sa_free_attr_string(path); 383 return (err); 384 } 385 386 } 387 } 388 389 if (privileged == B_TRUE && !online) { 390 err = smb_enable_service(); 391 if (err != SA_OK) { 392 (void) printf(dgettext(TEXT_DOMAIN, 393 "SMB: Unable to enable service\n")); 394 /* 395 * For now, it is OK to not be able to enable 396 * the service. 397 */ 398 if (err == SA_BUSY || err == SA_SYSTEM_ERR) 399 err = SA_OK; 400 } else { 401 online = B_TRUE; 402 } 403 } 404 405 /* 406 * Don't bother trying to start shares if the service isn't 407 * running. 408 */ 409 if (!online) 410 goto done; 411 412 /* Each share can have multiple resources */ 413 for (resource = sa_get_share_resource(share, NULL); 414 resource != NULL; 415 resource = sa_get_next_resource(resource)) { 416 bzero(&si, sizeof (lmshare_info_t)); 417 rname = sa_get_resource_attr(resource, "name"); 418 if (rname == NULL) { 419 sa_free_attr_string(path); 420 return (SA_NO_SUCH_RESOURCE); 421 } 422 423 smb_build_lmshare_info(rname, path, resource, &si); 424 sa_free_attr_string(rname); 425 426 if (!iszfs) { 427 err = lmshrd_add(&si); 428 } else { 429 share_t sh; 430 431 sa_sharetab_fill_zfs(share, &sh, "smb"); 432 err = sa_share_zfs(share, (char *)path, &sh, 433 &si, ZFS_SHARE_SMB); 434 435 sa_emptyshare(&sh); 436 } 437 } 438 if (!iszfs) 439 (void) sa_update_sharetab(share, "smb"); 440 done: 441 sa_free_attr_string(path); 442 443 return (err == NERR_DuplicateShare ? 0 : err); 444 } 445 446 /* 447 * This is the share for CIFS all shares have resource names. 448 * Enable tells the smb server to update its hash. If it fails 449 * because smb server is down, we just ignore as smb server loads 450 * the resources from sharemanager at startup. 451 */ 452 453 static int 454 smb_enable_resource(sa_resource_t resource) 455 { 456 char *path; 457 char *rname; 458 sa_share_t share; 459 lmshare_info_t si; 460 int ret = SA_OK; 461 int err; 462 boolean_t isonline; 463 464 share = sa_get_resource_parent(resource); 465 if (share == NULL) 466 return (SA_NO_SUCH_PATH); 467 468 /* 469 * If administratively disabled, don't try to start anything. 470 */ 471 isonline = smb_isonline(); 472 if (!isonline && !smb_isautoenable() && smb_isdisabled()) 473 goto done; 474 475 if (!isonline) 476 ret = smb_enable_service(); 477 if (!smb_isonline()) { 478 ret = SA_OK; 479 goto done; 480 } 481 482 path = sa_get_share_attr(share, "path"); 483 if (path == NULL) 484 return (SA_SYSTEM_ERR); 485 rname = sa_get_resource_attr(resource, "name"); 486 if (rname == NULL) { 487 sa_free_attr_string(path); 488 return (SA_NO_SUCH_RESOURCE); 489 } 490 491 smb_build_lmshare_info(rname, path, resource, &si); 492 sa_free_attr_string(path); 493 sa_free_attr_string(rname); 494 495 /* 496 * Attempt to add the share. Any error that occurs if it was 497 * online is an error but don't count NERR_DuplicateName if 498 * smb/server had to be brought online since bringing the 499 * service up will enable the share that was just added prior 500 * to the attempt to enable. 501 */ 502 503 err = lmshrd_add(&si); 504 if (err == NERR_Success || !(!isonline && err == NERR_DuplicateName)) 505 (void) sa_update_sharetab(share, "smb"); 506 else 507 return (SA_NOT_SHARED); 508 509 done: 510 return (ret); 511 } 512 513 /* 514 * Remove it from smb server hash. 515 */ 516 static int 517 smb_disable_resource(sa_resource_t resource) 518 { 519 char *rname; 520 DWORD res; 521 sa_share_t share; 522 523 rname = sa_get_resource_attr(resource, "name"); 524 if (rname == NULL) 525 return (SA_NO_SUCH_RESOURCE); 526 527 if (smb_isonline()) { 528 res = lmshrd_delete(rname); 529 if (res != NERR_Success) { 530 sa_free_attr_string(rname); 531 return (SA_CONFIG_ERR); 532 } 533 } 534 535 sa_free_attr_string(rname); 536 537 share = sa_get_resource_parent(resource); 538 if (share != NULL) { 539 rname = sa_get_share_attr(share, "path"); 540 if (rname != NULL) { 541 sa_handle_t handle; 542 543 handle = sa_find_group_handle((sa_group_t)resource); 544 (void) sa_delete_sharetab(handle, rname, "smb"); 545 sa_free_attr_string(rname); 546 } 547 } 548 /* 549 * Always return OK as smb/server may be down and 550 * Shares will be picked up when loaded. 551 */ 552 return (SA_OK); 553 } 554 555 /* 556 * smb_share_changed(sa_share_t share) 557 * 558 * The specified share has changed. 559 */ 560 static int 561 smb_share_changed(sa_share_t share) 562 { 563 char *path; 564 sa_resource_t resource; 565 566 /* get the path since it is important in several places */ 567 path = sa_get_share_attr(share, "path"); 568 if (path == NULL) 569 return (SA_NO_SUCH_PATH); 570 for (resource = sa_get_share_resource(share, NULL); 571 resource != NULL; 572 resource = sa_get_next_resource(resource)) 573 (void) smb_resource_changed(resource); 574 575 sa_free_attr_string(path); 576 577 return (SA_OK); 578 } 579 580 /* 581 * smb_resource_changed(sa_resource_t resource) 582 * 583 * The specified resource has changed. 584 */ 585 static int 586 smb_resource_changed(sa_resource_t resource) 587 { 588 DWORD res; 589 lmshare_info_t si; 590 lmshare_info_t new_si; 591 char *rname, *path; 592 sa_share_t share; 593 594 rname = sa_get_resource_attr(resource, "name"); 595 if (rname == NULL) 596 return (SA_NO_SUCH_RESOURCE); 597 598 share = sa_get_resource_parent(resource); 599 if (share == NULL) { 600 sa_free_attr_string(rname); 601 return (SA_CONFIG_ERR); 602 } 603 604 path = sa_get_share_attr(share, "path"); 605 if (path == NULL) { 606 sa_free_attr_string(rname); 607 return (SA_NO_SUCH_PATH); 608 } 609 610 if (!smb_isonline()) { 611 sa_free_attr_string(rname); 612 return (SA_OK); 613 } 614 615 /* Update the share cache in smb/server */ 616 res = lmshrd_getinfo(rname, &si); 617 if (res != NERR_Success) { 618 sa_free_attr_string(path); 619 sa_free_attr_string(rname); 620 return (SA_CONFIG_ERR); 621 } 622 623 smb_build_lmshare_info(rname, path, resource, &new_si); 624 sa_free_attr_string(path); 625 sa_free_attr_string(rname); 626 627 /* 628 * Update all fields from sa_share_t 629 * Get derived values. 630 */ 631 if (lmshrd_setinfo(&new_si) != LMSHR_DOOR_SRV_SUCCESS) 632 return (SA_CONFIG_ERR); 633 return (smb_enable_service()); 634 } 635 636 /* 637 * smb_disable_share(sa_share_t share, char *path) 638 * 639 * Unshare the specified share. Note that "path" is the same 640 * path as what is in the "share" object. It is passed in to avoid an 641 * additional lookup. A missing "path" value makes this a no-op 642 * function. 643 */ 644 static int 645 smb_disable_share(sa_share_t share, char *path) 646 { 647 char *rname; 648 sa_resource_t resource; 649 sa_group_t parent; 650 boolean_t iszfs; 651 int err = SA_OK; 652 sa_handle_t handle; 653 654 if (path == NULL) 655 return (err); 656 657 /* 658 * If the share is in a ZFS group we need to handle it 659 * differently. Just being on a ZFS file system isn't 660 * enough since we may be in a legacy share case. 661 */ 662 parent = sa_get_parent_group(share); 663 iszfs = sa_group_is_zfs(parent); 664 665 if (!smb_isonline()) 666 goto done; 667 668 for (resource = sa_get_share_resource(share, NULL); 669 resource != NULL; 670 resource = sa_get_next_resource(resource)) { 671 rname = sa_get_resource_attr(resource, "name"); 672 if (rname == NULL) { 673 continue; 674 } 675 if (!iszfs) { 676 err = lmshrd_delete(rname); 677 switch (err) { 678 case NERR_NetNameNotFound: 679 case NERR_Success: 680 err = SA_OK; 681 break; 682 default: 683 err = SA_CONFIG_ERR; 684 break; 685 } 686 } else { 687 share_t sh; 688 689 sa_sharetab_fill_zfs(share, &sh, "smb"); 690 err = sa_share_zfs(share, (char *)path, &sh, 691 rname, ZFS_UNSHARE_SMB); 692 sa_emptyshare(&sh); 693 } 694 sa_free_attr_string(rname); 695 } 696 done: 697 if (!iszfs) { 698 handle = sa_find_group_handle((sa_group_t)share); 699 if (handle != NULL) 700 (void) sa_delete_sharetab(handle, path, "smb"); 701 else 702 err = SA_SYSTEM_ERR; 703 } 704 return (err); 705 } 706 707 /* 708 * smb_validate_property(handle, property, parent) 709 * 710 * Check that the property has a legitimate value for its type. 711 * Handle isn't currently used but may need to be in the future. 712 */ 713 714 /*ARGSUSED*/ 715 static int 716 smb_validate_property(sa_handle_t handle, sa_property_t property, 717 sa_optionset_t parent) 718 { 719 int ret = SA_OK; 720 char *propname; 721 int optindex; 722 sa_group_t parent_group; 723 char *value; 724 725 propname = sa_get_property_attr(property, "type"); 726 727 if ((optindex = findopt(propname)) < 0) 728 ret = SA_NO_SUCH_PROP; 729 730 /* need to validate value range here as well */ 731 if (ret == SA_OK) { 732 parent_group = sa_get_parent_group((sa_share_t)parent); 733 if (optdefs[optindex].share && !sa_is_share(parent_group)) 734 ret = SA_PROP_SHARE_ONLY; 735 } 736 if (ret != SA_OK) { 737 if (propname != NULL) 738 sa_free_attr_string(propname); 739 return (ret); 740 } 741 742 value = sa_get_property_attr(property, "value"); 743 if (value != NULL) { 744 /* first basic type checking */ 745 switch (optdefs[optindex].type) { 746 case OPT_TYPE_NUMBER: 747 /* check that the value is all digits */ 748 if (!is_a_number(value)) 749 ret = SA_BAD_VALUE; 750 break; 751 case OPT_TYPE_BOOLEAN: 752 if (strlen(value) == 0 || 753 strcasecmp(value, "true") == 0 || 754 strcmp(value, "1") == 0 || 755 strcasecmp(value, "false") == 0 || 756 strcmp(value, "0") == 0) { 757 ret = SA_OK; 758 } else { 759 ret = SA_BAD_VALUE; 760 } 761 break; 762 case OPT_TYPE_NAME: 763 /* 764 * Make sure no invalid characters 765 */ 766 if (validresource(value) == B_FALSE) 767 ret = SA_BAD_VALUE; 768 break; 769 case OPT_TYPE_STRING: 770 /* whatever is here should be ok */ 771 break; 772 default: 773 break; 774 } 775 } 776 777 if (value != NULL) 778 sa_free_attr_string(value); 779 if (ret == SA_OK && optdefs[optindex].check != NULL) 780 /* do the property specific check */ 781 ret = optdefs[optindex].check(property); 782 783 if (propname != NULL) 784 sa_free_attr_string(propname); 785 return (ret); 786 } 787 788 /* 789 * Protocol management functions 790 * 791 * properties defined in the default files are defined in 792 * proto_option_defs for parsing and validation. 793 */ 794 795 struct smb_proto_option_defs { 796 int smb_index; 797 int32_t minval; 798 int32_t maxval; /* In case of length of string this should be max */ 799 int (*validator)(int, char *); 800 int32_t refresh; 801 } smb_proto_options[] = { 802 { SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN, 803 string_length_check_validator, SMB_REFRESH_REFRESH }, 804 { SMB_CI_MAX_WORKERS, 64, 1024, range_check_validator, 805 SMB_REFRESH_REFRESH }, 806 { SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN, 807 string_length_check_validator, 0 }, 808 { SMB_CI_LM_LEVEL, 2, 5, range_check_validator, 0 }, 809 { SMB_CI_KEEPALIVE, 20, 5400, range_check_validator_zero_ok, 810 SMB_REFRESH_REFRESH }, 811 { SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN, 812 ip_address_validator_empty_ok, SMB_REFRESH_REFRESH }, 813 { SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN, 814 ip_address_validator_empty_ok, SMB_REFRESH_REFRESH }, 815 { SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN, 816 ip_address_csv_list_validator_empty_ok, SMB_REFRESH_REFRESH }, 817 { SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator, 818 SMB_REFRESH_REFRESH }, 819 { SMB_CI_SIGNING_REQD, 0, 0, true_false_validator, 820 SMB_REFRESH_REFRESH }, 821 { SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator, 822 SMB_REFRESH_REFRESH }, 823 { SMB_CI_DOMAIN_SRV, 0, MAX_VALUE_BUFLEN, 824 ip_address_validator_empty_ok, 0 }, 825 { SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN, 826 string_length_check_validator, SMB_REFRESH_REFRESH }, 827 { SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 }, 828 { SMB_CI_AUTOHOME_MAP, 0, MAX_VALUE_BUFLEN, path_validator, 0 }, 829 }; 830 831 #define SMB_OPT_NUM \ 832 (sizeof (smb_proto_options) / sizeof (smb_proto_options[0])) 833 834 /* 835 * Check the range of value as int range. 836 */ 837 static int 838 range_check_validator(int index, char *value) 839 { 840 int ret = SA_OK; 841 842 if (!is_a_number(value)) { 843 ret = SA_BAD_VALUE; 844 } else { 845 int val; 846 val = strtoul(value, NULL, 0); 847 if (val < smb_proto_options[index].minval || 848 val > smb_proto_options[index].maxval) 849 ret = SA_BAD_VALUE; 850 } 851 return (ret); 852 } 853 854 /* 855 * Check the range of value as int range. 856 */ 857 static int 858 range_check_validator_zero_ok(int index, char *value) 859 { 860 int ret = SA_OK; 861 862 if (!is_a_number(value)) { 863 ret = SA_BAD_VALUE; 864 } else { 865 int val; 866 val = strtoul(value, NULL, 0); 867 if (val == 0) 868 ret = SA_OK; 869 else { 870 if (val < smb_proto_options[index].minval || 871 val > smb_proto_options[index].maxval) 872 ret = SA_BAD_VALUE; 873 } 874 } 875 return (ret); 876 } 877 878 /* 879 * Check the length of the string 880 */ 881 static int 882 string_length_check_validator(int index, char *value) 883 { 884 int ret = SA_OK; 885 886 if (value == NULL) 887 return (SA_BAD_VALUE); 888 if (strlen(value) > smb_proto_options[index].maxval) 889 ret = SA_BAD_VALUE; 890 return (ret); 891 } 892 893 /* 894 * Check yes/no 895 */ 896 /*ARGSUSED*/ 897 static int 898 true_false_validator(int index, char *value) 899 { 900 if (value == NULL) 901 return (SA_BAD_VALUE); 902 if ((strcasecmp(value, "true") == 0) || 903 (strcasecmp(value, "false") == 0)) 904 return (SA_OK); 905 return (SA_BAD_VALUE); 906 } 907 908 /* 909 * Check IP address. 910 */ 911 /*ARGSUSED*/ 912 static int 913 ip_address_validator_empty_ok(int index, char *value) 914 { 915 char sbytes[16]; 916 int len; 917 918 if (value == NULL) 919 return (SA_OK); 920 len = strlen(value); 921 if (len == 0) 922 return (SA_OK); 923 if (inet_pton(AF_INET, value, (void *)sbytes) != 1) 924 return (SA_BAD_VALUE); 925 926 return (SA_OK); 927 } 928 929 /* 930 * Check IP address list 931 */ 932 /*ARGSUSED*/ 933 static int 934 ip_address_csv_list_validator_empty_ok(int index, char *value) 935 { 936 char sbytes[16]; 937 char *ip, *tmp, *ctx; 938 939 if (value == NULL || *value == '\0') 940 return (SA_OK); 941 942 if (strlen(value) > MAX_VALUE_BUFLEN) 943 return (SA_BAD_VALUE); 944 945 if ((tmp = strdup(value)) == NULL) 946 return (SA_NO_MEMORY); 947 948 ip = strtok_r(tmp, ",", &ctx); 949 while (ip) { 950 if (strlen(ip) == 0) { 951 free(tmp); 952 return (SA_BAD_VALUE); 953 } 954 if (*ip != 0) { 955 if (inet_pton(AF_INET, ip, 956 (void *)sbytes) != 1) { 957 free(tmp); 958 return (SA_BAD_VALUE); 959 } 960 } 961 ip = strtok_r(0, ",", &ctx); 962 } 963 964 free(tmp); 965 return (SA_OK); 966 } 967 968 /* 969 * Check path 970 */ 971 /*ARGSUSED*/ 972 static int 973 path_validator(int index, char *value) 974 { 975 struct stat buffer; 976 int fd, status; 977 978 if (value == NULL) 979 return (SA_BAD_VALUE); 980 981 fd = open(value, O_RDONLY); 982 if (fd < 0) 983 return (SA_BAD_VALUE); 984 985 status = fstat(fd, &buffer); 986 (void) close(fd); 987 988 if (status < 0) 989 return (SA_BAD_VALUE); 990 991 if (buffer.st_mode & S_IFDIR) 992 return (SA_OK); 993 return (SA_BAD_VALUE); 994 } 995 996 /* 997 * the protoset holds the defined options so we don't have to read 998 * them multiple times 999 */ 1000 static sa_protocol_properties_t protoset; 1001 1002 static int 1003 findprotoopt(char *name) 1004 { 1005 int i; 1006 char *sc_name; 1007 1008 for (i = 0; i < SMB_OPT_NUM; i++) { 1009 sc_name = smb_config_getname(smb_proto_options[i].smb_index); 1010 if (strcasecmp(sc_name, name) == 0) 1011 return (i); 1012 } 1013 1014 return (-1); 1015 } 1016 1017 /* 1018 * smb_load_proto_properties() 1019 * 1020 * read the smb config values from SMF. 1021 */ 1022 1023 static int 1024 smb_load_proto_properties() 1025 { 1026 sa_property_t prop; 1027 char value[MAX_VALUE_BUFLEN]; 1028 char *name; 1029 int index; 1030 int ret = SA_OK; 1031 int rc; 1032 1033 protoset = sa_create_protocol_properties(SMB_PROTOCOL_NAME); 1034 if (protoset == NULL) 1035 return (SA_NO_MEMORY); 1036 1037 for (index = 0; index < SMB_OPT_NUM && ret == SA_OK; index++) { 1038 rc = smb_config_get(smb_proto_options[index].smb_index, 1039 value, sizeof (value)); 1040 if (rc != SMBD_SMF_OK) 1041 continue; 1042 name = smb_config_getname(smb_proto_options[index].smb_index); 1043 prop = sa_create_property(name, value); 1044 if (prop != NULL) 1045 ret = sa_add_protocol_property(protoset, prop); 1046 else 1047 ret = SA_NO_MEMORY; 1048 } 1049 return (ret); 1050 } 1051 1052 /* 1053 * smb_share_init() 1054 * 1055 * Initialize the smb plugin. 1056 */ 1057 1058 static int 1059 smb_share_init(void) 1060 { 1061 int ret = SA_OK; 1062 1063 if (sa_plugin_ops.sa_init != smb_share_init) 1064 return (SA_SYSTEM_ERR); 1065 1066 ret = smb_load_proto_properties(); 1067 1068 return (ret); 1069 } 1070 1071 /* 1072 * smb_share_fini() 1073 * 1074 */ 1075 static void 1076 smb_share_fini(void) 1077 { 1078 xmlFreeNode(protoset); 1079 protoset = NULL; 1080 1081 (void) lmshrd_door_close(); 1082 } 1083 1084 /* 1085 * smb_get_proto_set() 1086 * 1087 * Return an optionset with all the protocol specific properties in 1088 * it. 1089 */ 1090 static sa_protocol_properties_t 1091 smb_get_proto_set(void) 1092 { 1093 return (protoset); 1094 } 1095 1096 /* 1097 * smb_enable_dependencies() 1098 * 1099 * SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't 1100 * enabled. This will attempt to enable all of them. 1101 */ 1102 static void 1103 smb_enable_dependencies(const char *fmri) 1104 { 1105 scf_handle_t *handle; 1106 scf_service_t *service; 1107 scf_instance_t *inst = NULL; 1108 scf_iter_t *iter; 1109 scf_property_t *prop; 1110 scf_value_t *value; 1111 scf_propertygroup_t *pg; 1112 scf_scope_t *scope; 1113 char type[SCFTYPE_LEN]; 1114 char *dependency; 1115 char *servname; 1116 int maxlen; 1117 1118 /* 1119 * Get all required handles and storage. 1120 */ 1121 handle = scf_handle_create(SCF_VERSION); 1122 if (handle == NULL) 1123 return; 1124 1125 if (scf_handle_bind(handle) != 0) { 1126 scf_handle_destroy(handle); 1127 return; 1128 } 1129 1130 maxlen = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH); 1131 if (maxlen == (ssize_t)-1) 1132 maxlen = MAXPATHLEN; 1133 1134 dependency = malloc(maxlen); 1135 1136 service = scf_service_create(handle); 1137 1138 iter = scf_iter_create(handle); 1139 1140 pg = scf_pg_create(handle); 1141 1142 prop = scf_property_create(handle); 1143 1144 value = scf_value_create(handle); 1145 1146 scope = scf_scope_create(handle); 1147 1148 if (service == NULL || iter == NULL || pg == NULL || prop == NULL || 1149 value == NULL || scope == NULL || dependency == NULL) 1150 goto done; 1151 1152 /* 1153 * We passed in the FMRI for the default instance but for 1154 * some things we need the simple form so construct it. Since 1155 * we reuse the storage that dependency points to, we need to 1156 * use the servname early. 1157 */ 1158 (void) snprintf(dependency, maxlen, "%s", fmri + sizeof ("svc:")); 1159 servname = strrchr(dependency, ':'); 1160 if (servname == NULL) 1161 goto done; 1162 *servname = '\0'; 1163 servname = dependency; 1164 1165 /* 1166 * Setup to iterate over the service property groups, only 1167 * looking at those that are "dependency" types. The "entity" 1168 * property will have the FMRI of the service we are dependent 1169 * on. 1170 */ 1171 if (scf_handle_get_scope(handle, SCF_SCOPE_LOCAL, scope) != 0) 1172 goto done; 1173 1174 if (scf_scope_get_service(scope, servname, service) != 0) 1175 goto done; 1176 1177 if (scf_iter_service_pgs(iter, service) != 0) 1178 goto done; 1179 1180 while (scf_iter_next_pg(iter, pg) > 0) { 1181 char *services[2]; 1182 /* 1183 * Have a property group for the service. See if it is 1184 * a dependency pg and only do operations on those. 1185 */ 1186 if (scf_pg_get_type(pg, type, SCFTYPE_LEN) <= 0) 1187 continue; 1188 1189 if (strncmp(type, SCF_GROUP_DEPENDENCY, SCFTYPE_LEN) != 0) 1190 continue; 1191 /* 1192 * Have a dependency. Attempt to enable it. 1193 */ 1194 if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) 1195 continue; 1196 1197 if (scf_property_get_value(prop, value) != 0) 1198 continue; 1199 1200 services[1] = NULL; 1201 1202 if (scf_value_get_as_string(value, dependency, maxlen) > 0) { 1203 services[0] = dependency; 1204 _check_services(services); 1205 } 1206 } 1207 1208 done: 1209 if (dependency != NULL) 1210 free(dependency); 1211 if (value != NULL) 1212 scf_value_destroy(value); 1213 if (prop != NULL) 1214 scf_property_destroy(prop); 1215 if (pg != NULL) 1216 scf_pg_destroy(pg); 1217 if (iter != NULL) 1218 scf_iter_destroy(iter); 1219 if (scope != NULL) 1220 scf_scope_destroy(scope); 1221 if (inst != NULL) 1222 scf_instance_destroy(inst); 1223 if (service != NULL) 1224 scf_service_destroy(service); 1225 1226 (void) scf_handle_unbind(handle); 1227 scf_handle_destroy(handle); 1228 } 1229 1230 /* 1231 * How long to wait for service to come online 1232 */ 1233 #define WAIT_FOR_SERVICE 15 1234 1235 /* 1236 * smb_enable_service() 1237 * 1238 */ 1239 static int 1240 smb_enable_service(void) 1241 { 1242 int i; 1243 int ret = SA_OK; 1244 char *service[] = { SMBD_DEFAULT_INSTANCE_FMRI, NULL }; 1245 1246 if (!smb_isonline()) { 1247 /* 1248 * Attempt to start the idmap, and other dependent 1249 * services, first. If it fails, the SMB service will 1250 * ultimately fail so we use that as the error. If we 1251 * don't try to enable idmap, smb won't start the 1252 * first time unless the admin has done it 1253 * manually. The service could be administratively 1254 * disabled so we won't always get started. 1255 */ 1256 smb_enable_dependencies(SMBD_DEFAULT_INSTANCE_FMRI); 1257 _check_services(service); 1258 1259 /* Wait for service to come online */ 1260 for (i = 0; i < WAIT_FOR_SERVICE; i++) { 1261 if (smb_isonline()) { 1262 ret = SA_OK; 1263 break; 1264 } else if (smb_ismaint()) { 1265 /* maintenance requires help */ 1266 ret = SA_SYSTEM_ERR; 1267 break; 1268 } else if (smb_isdisabled()) { 1269 /* disabled is ok */ 1270 ret = SA_OK; 1271 break; 1272 } else { 1273 /* try another time */ 1274 ret = SA_BUSY; 1275 (void) sleep(1); 1276 } 1277 } 1278 } 1279 return (ret); 1280 } 1281 1282 /* 1283 * smb_validate_proto_prop(index, name, value) 1284 * 1285 * Verify that the property specified by name can take the new 1286 * value. This is a sanity check to prevent bad values getting into 1287 * the default files. 1288 */ 1289 static int 1290 smb_validate_proto_prop(int index, char *name, char *value) 1291 { 1292 if ((name == NULL) || (index < 0)) 1293 return (SA_BAD_VALUE); 1294 1295 if (smb_proto_options[index].validator == NULL) 1296 return (SA_OK); 1297 1298 if (smb_proto_options[index].validator(index, value) == SA_OK) 1299 return (SA_OK); 1300 return (SA_BAD_VALUE); 1301 } 1302 1303 /* 1304 * smb_set_proto_prop(prop) 1305 * 1306 * check that prop is valid. 1307 */ 1308 /*ARGSUSED*/ 1309 static int 1310 smb_set_proto_prop(sa_property_t prop) 1311 { 1312 int ret = SA_OK; 1313 char *name; 1314 char *value; 1315 int index = -1; 1316 struct smb_proto_option_defs *opt; 1317 1318 name = sa_get_property_attr(prop, "type"); 1319 value = sa_get_property_attr(prop, "value"); 1320 if (name != NULL && value != NULL) { 1321 index = findprotoopt(name); 1322 if (index >= 0) { 1323 /* should test for valid value */ 1324 ret = smb_validate_proto_prop(index, name, value); 1325 if (ret == SA_OK) { 1326 opt = &smb_proto_options[index]; 1327 1328 /* Save to SMF */ 1329 (void) smb_config_set(opt->smb_index, value); 1330 /* 1331 * Specialized refresh mechanisms can 1332 * be flagged in the proto_options and 1333 * processed here. 1334 */ 1335 if (opt->refresh & SMB_REFRESH_REFRESH) 1336 (void) smf_refresh_instance( 1337 SMBD_DEFAULT_INSTANCE_FMRI); 1338 else if (opt->refresh & SMB_REFRESH_RESTART) 1339 (void) smf_restart_instance( 1340 SMBD_DEFAULT_INSTANCE_FMRI); 1341 } 1342 } 1343 } 1344 1345 if (name != NULL) 1346 sa_free_attr_string(name); 1347 if (value != NULL) 1348 sa_free_attr_string(value); 1349 1350 return (ret); 1351 } 1352 1353 /* 1354 * smb_get_status() 1355 * 1356 * What is the current status of the smbd? We use the SMF state here. 1357 * Caller must free the returned value. 1358 */ 1359 1360 static char * 1361 smb_get_status(void) 1362 { 1363 char *state = NULL; 1364 state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI); 1365 return (state != NULL ? state : "-"); 1366 } 1367 1368 /* 1369 * This protocol plugin require resource names 1370 */ 1371 static uint64_t 1372 smb_share_features(void) 1373 { 1374 return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS | 1375 SA_FEATURE_ALLOWPARDIRS | SA_FEATURE_SERVER); 1376 } 1377 1378 /* 1379 * This should be used to convert lmshare_info to sa_resource_t 1380 * Should only be needed to build temp shares/resources to be 1381 * supplied to sharemanager to display temp shares. 1382 */ 1383 static int 1384 smb_build_tmp_sa_resource(sa_handle_t handle, lmshare_info_t *si) 1385 { 1386 int err; 1387 sa_share_t share; 1388 sa_group_t group; 1389 sa_resource_t resource; 1390 1391 if (si == NULL) 1392 return (SA_INVALID_NAME); 1393 1394 /* 1395 * First determine if the "share path" is already shared 1396 * somewhere. If it is, we have to use it as the authority on 1397 * where the transient share lives so will use it's parent 1398 * group. If it doesn't exist, it needs to land in "smb". 1399 */ 1400 1401 share = sa_find_share(handle, si->directory); 1402 if (share != NULL) { 1403 group = sa_get_parent_group(share); 1404 } else { 1405 group = smb_get_smb_share_group(handle); 1406 if (group == NULL) 1407 return (SA_NO_SUCH_GROUP); 1408 share = sa_get_share(group, si->directory); 1409 if (share == NULL) { 1410 share = sa_add_share(group, si->directory, 1411 SA_SHARE_TRANSIENT, &err); 1412 if (share == NULL) 1413 return (SA_NO_SUCH_PATH); 1414 } 1415 } 1416 1417 /* 1418 * Now handle the resource. Make sure that the resource is 1419 * transient and added to the share. 1420 */ 1421 resource = sa_get_share_resource(share, si->share_name); 1422 if (resource == NULL) { 1423 resource = sa_add_resource(share, 1424 si->share_name, SA_SHARE_TRANSIENT, &err); 1425 if (resource == NULL) 1426 return (SA_NO_SUCH_RESOURCE); 1427 } 1428 1429 /* set resource attributes now */ 1430 (void) sa_set_resource_attr(resource, "description", si->comment); 1431 (void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER, 1432 si->container); 1433 1434 return (SA_OK); 1435 } 1436 1437 /* 1438 * Return smb transient shares. Note that we really want to look at 1439 * all current shares from SMB in order to determine this. Transient 1440 * shares should be those that don't appear in either the SMF or ZFS 1441 * configurations. Those that are in the repositories will be 1442 * filtered out by smb_build_tmp_sa_resource. 1443 */ 1444 static int 1445 smb_list_transient(sa_handle_t handle) 1446 { 1447 int i, offset, num; 1448 lmshare_list_t list; 1449 int res; 1450 1451 num = lmshrd_num_shares(); 1452 if (num <= 0) 1453 return (SA_OK); 1454 offset = 0; 1455 while (lmshrd_list(offset, &list) != NERR_InternalError) { 1456 if (list.no == 0) 1457 break; 1458 for (i = 0; i < list.no; i++) { 1459 res = smb_build_tmp_sa_resource(handle, 1460 &(list.smbshr[i])); 1461 if (res != SA_OK) 1462 return (res); 1463 } 1464 offset += list.no; 1465 } 1466 1467 return (SA_OK); 1468 } 1469 1470 /* 1471 * fix_resource_name(share, name, prefix) 1472 * 1473 * Construct a name where the ZFS dataset has the prefix replaced with "name". 1474 */ 1475 static char * 1476 fix_resource_name(sa_share_t share, char *name, char *prefix) 1477 { 1478 char *dataset = NULL; 1479 char *newname = NULL; 1480 size_t psize; 1481 size_t nsize; 1482 1483 dataset = sa_get_share_attr(share, "dataset"); 1484 1485 if (dataset != NULL && strcmp(dataset, prefix) != 0) { 1486 psize = strlen(prefix); 1487 if (strncmp(dataset, prefix, psize) == 0) { 1488 /* need string plus ',' and NULL */ 1489 nsize = (strlen(dataset) - psize) + strlen(name) + 2; 1490 newname = calloc(nsize, 1); 1491 if (newname != NULL) { 1492 (void) snprintf(newname, nsize, "%s%s", name, 1493 dataset + psize); 1494 sa_fix_resource_name(newname); 1495 } 1496 sa_free_attr_string(dataset); 1497 return (newname); 1498 } 1499 } 1500 if (dataset != NULL) 1501 sa_free_attr_string(dataset); 1502 return (strdup(name)); 1503 } 1504 1505 /* 1506 * smb_parse_optstring(group, options) 1507 * 1508 * parse a compact option string into individual options. This allows 1509 * ZFS sharesmb and sharemgr "share" command to work. group can be a 1510 * group, a share or a resource. 1511 */ 1512 static int 1513 smb_parse_optstring(sa_group_t group, char *options) 1514 { 1515 char *dup; 1516 char *base; 1517 char *lasts; 1518 char *token; 1519 sa_optionset_t optionset; 1520 sa_group_t parent = NULL; 1521 sa_resource_t resource = NULL; 1522 int iszfs = 0; 1523 int persist = 0; 1524 int need_optionset = 0; 1525 int ret = SA_OK; 1526 sa_property_t prop; 1527 1528 /* 1529 * In order to not attempt to change ZFS properties unless 1530 * absolutely necessary, we never do it in the legacy parsing 1531 * so we need to keep track of this. 1532 */ 1533 if (sa_is_share(group)) { 1534 char *zfs; 1535 1536 parent = sa_get_parent_group(group); 1537 if (parent != NULL) { 1538 zfs = sa_get_group_attr(parent, "zfs"); 1539 if (zfs != NULL) { 1540 sa_free_attr_string(zfs); 1541 iszfs = 1; 1542 } 1543 } 1544 } else { 1545 iszfs = sa_group_is_zfs(group); 1546 /* 1547 * If a ZFS group, then we need to see if a resource 1548 * name is being set. If so, bail with 1549 * SA_PROP_SHARE_ONLY, so we come back in with a share 1550 * instead of a group. 1551 */ 1552 if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 || 1553 strstr(options, ",name=") != NULL) { 1554 return (SA_PROP_SHARE_ONLY); 1555 } 1556 } 1557 1558 /* do we have an existing optionset? */ 1559 optionset = sa_get_optionset(group, "smb"); 1560 if (optionset == NULL) { 1561 /* didn't find existing optionset so create one */ 1562 optionset = sa_create_optionset(group, "smb"); 1563 if (optionset == NULL) 1564 return (SA_NO_MEMORY); 1565 } else { 1566 /* 1567 * If an optionset already exists, we've come through 1568 * twice so ignore the second time. 1569 */ 1570 return (ret); 1571 } 1572 1573 /* We need a copy of options for the next part. */ 1574 dup = strdup(options); 1575 if (dup == NULL) 1576 return (SA_NO_MEMORY); 1577 1578 /* 1579 * SMB properties are straightforward and are strings, 1580 * integers or booleans. Properties are separated by 1581 * commas. It will be necessary to parse quotes due to some 1582 * strings not having a restricted characters set. 1583 * 1584 * Note that names will create a resource. For now, if there 1585 * is a set of properties "before" the first name="", those 1586 * properties will be placed on the group. 1587 */ 1588 persist = sa_is_persistent(group); 1589 base = dup; 1590 token = dup; 1591 lasts = NULL; 1592 while (token != NULL && ret == SA_OK) { 1593 ret = SA_OK; 1594 token = strtok_r(base, ",", &lasts); 1595 base = NULL; 1596 if (token != NULL) { 1597 char *value; 1598 /* 1599 * All SMB properties have values so there 1600 * MUST be an '=' character. If it doesn't, 1601 * it is a syntax error. 1602 */ 1603 value = strchr(token, '='); 1604 if (value != NULL) { 1605 *value++ = '\0'; 1606 } else { 1607 ret = SA_SYNTAX_ERR; 1608 break; 1609 } 1610 /* 1611 * We may need to handle a "name" property 1612 * that is a ZFS imposed resource name. Each 1613 * name would trigger getting a new "resource" 1614 * to put properties on. For now, assume no 1615 * "name" property for special handling. 1616 */ 1617 1618 if (strcmp(token, "name") == 0) { 1619 char *prefix; 1620 char *name = NULL; 1621 /* 1622 * We have a name, so now work on the 1623 * resource level. We have a "share" 1624 * in "group" due to the caller having 1625 * added it. If we are called with a 1626 * group, the check for group/share 1627 * at the beginning of this function 1628 * will bail out the parse if there is a 1629 * "name" but no share. 1630 */ 1631 if (!iszfs) { 1632 ret = SA_SYNTAX_ERR; 1633 break; 1634 } 1635 /* 1636 * Make sure the parent group has the 1637 * "prefix" property since we will 1638 * need to use this for constructing 1639 * inherited name= values. 1640 */ 1641 prefix = sa_get_group_attr(parent, "prefix"); 1642 if (prefix == NULL) { 1643 prefix = sa_get_group_attr(parent, 1644 "name"); 1645 if (prefix != NULL) { 1646 (void) sa_set_group_attr(parent, 1647 "prefix", prefix); 1648 } 1649 } 1650 name = fix_resource_name((sa_share_t)group, 1651 value, prefix); 1652 if (name != NULL) { 1653 resource = sa_add_resource( 1654 (sa_share_t)group, name, 1655 SA_SHARE_TRANSIENT, &ret); 1656 sa_free_attr_string(name); 1657 } else { 1658 ret = SA_NO_MEMORY; 1659 } 1660 if (prefix != NULL) 1661 sa_free_attr_string(prefix); 1662 1663 /* A resource level optionset is needed */ 1664 1665 need_optionset = 1; 1666 if (resource == NULL) { 1667 ret = SA_NO_MEMORY; 1668 break; 1669 } 1670 continue; 1671 } 1672 1673 if (need_optionset) { 1674 optionset = sa_create_optionset(resource, 1675 "smb"); 1676 need_optionset = 0; 1677 } 1678 1679 prop = sa_create_property(token, value); 1680 if (prop == NULL) 1681 ret = SA_NO_MEMORY; 1682 else 1683 ret = sa_add_property(optionset, prop); 1684 if (ret != SA_OK) 1685 break; 1686 if (!iszfs) 1687 ret = sa_commit_properties(optionset, !persist); 1688 } 1689 } 1690 free(dup); 1691 return (ret); 1692 } 1693 1694 /* 1695 * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep) 1696 * 1697 * provides a mechanism to format SMB properties into legacy output 1698 * format. If the buffer would overflow, it is reallocated and grown 1699 * as appropriate. Special cases of converting internal form of values 1700 * to those used by "share" are done. this function does one property 1701 * at a time. 1702 */ 1703 1704 static void 1705 smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr, 1706 sa_property_t prop, int sep) 1707 { 1708 char *name; 1709 char *value; 1710 int curlen; 1711 char *buff = *rbuff; 1712 size_t buffsize = *rbuffsize; 1713 1714 name = sa_get_property_attr(prop, "type"); 1715 value = sa_get_property_attr(prop, "value"); 1716 if (buff != NULL) 1717 curlen = strlen(buff); 1718 else 1719 curlen = 0; 1720 if (name != NULL) { 1721 int len; 1722 len = strlen(name) + sep; 1723 1724 /* 1725 * A future RFE would be to replace this with more 1726 * generic code and to possibly handle more types. 1727 * 1728 * For now, everything else is treated as a string. If 1729 * we get any properties that aren't exactly 1730 * name/value pairs, we may need to 1731 * interpret/transform. 1732 */ 1733 if (value != NULL) 1734 len += 1 + strlen(value); 1735 1736 while (buffsize <= (curlen + len)) { 1737 /* need more room */ 1738 buffsize += incr; 1739 buff = realloc(buff, buffsize); 1740 *rbuff = buff; 1741 *rbuffsize = buffsize; 1742 if (buff == NULL) { 1743 /* realloc failed so free everything */ 1744 if (*rbuff != NULL) 1745 free(*rbuff); 1746 goto err; 1747 } 1748 } 1749 if (buff == NULL) 1750 goto err; 1751 (void) snprintf(buff + curlen, buffsize - curlen, 1752 "%s%s=%s", sep ? "," : "", 1753 name, value != NULL ? value : "\"\""); 1754 1755 } 1756 err: 1757 if (name != NULL) 1758 sa_free_attr_string(name); 1759 if (value != NULL) 1760 sa_free_attr_string(value); 1761 } 1762 1763 /* 1764 * smb_format_resource_options(resource, hier) 1765 * 1766 * format all the options on the group into a flattened option 1767 * string. If hier is non-zero, walk up the tree to get inherited 1768 * options. 1769 */ 1770 1771 static char * 1772 smb_format_options(sa_group_t group, int hier) 1773 { 1774 sa_optionset_t options = NULL; 1775 sa_property_t prop; 1776 int sep = 0; 1777 char *buff; 1778 size_t buffsize; 1779 1780 1781 buff = malloc(OPT_CHUNK); 1782 if (buff == NULL) 1783 return (NULL); 1784 1785 buff[0] = '\0'; 1786 buffsize = OPT_CHUNK; 1787 1788 /* 1789 * We may have a an optionset relative to this item. format 1790 * these if we find them and then add any security definitions. 1791 */ 1792 1793 options = sa_get_derived_optionset(group, "smb", hier); 1794 1795 /* 1796 * do the default set first but skip any option that is also 1797 * in the protocol specific optionset. 1798 */ 1799 if (options != NULL) { 1800 for (prop = sa_get_property(options, NULL); 1801 prop != NULL; prop = sa_get_next_property(prop)) { 1802 /* 1803 * use this one since we skipped any 1804 * of these that were also in 1805 * optdefault 1806 */ 1807 smb_sprint_option(&buff, &buffsize, OPT_CHUNK, 1808 prop, sep); 1809 if (buff == NULL) { 1810 /* 1811 * buff could become NULL if there 1812 * isn't enough memory for 1813 * smb_sprint_option to realloc() 1814 * as necessary. We can't really 1815 * do anything about it at this 1816 * point so we return NULL. The 1817 * caller should handle the 1818 * failure. 1819 */ 1820 if (options != NULL) 1821 sa_free_derived_optionset( 1822 options); 1823 return (buff); 1824 } 1825 sep = 1; 1826 } 1827 } 1828 1829 if (options != NULL) 1830 sa_free_derived_optionset(options); 1831 return (buff); 1832 } 1833 1834 /* 1835 * smb_rename_resource(resource, newname) 1836 * 1837 * Change the current exported name of the resource to newname. 1838 */ 1839 /*ARGSUSED*/ 1840 int 1841 smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname) 1842 { 1843 int ret = SA_OK; 1844 int err; 1845 char *oldname; 1846 1847 oldname = sa_get_resource_attr(resource, "name"); 1848 if (oldname == NULL) 1849 return (SA_NO_SUCH_RESOURCE); 1850 1851 err = lmshrd_rename(oldname, newname); 1852 1853 /* improve error values somewhat */ 1854 switch (err) { 1855 case NERR_Success: 1856 break; 1857 case NERR_InternalError: 1858 ret = SA_SYSTEM_ERR; 1859 break; 1860 case NERR_DuplicateShare: 1861 ret = SA_DUPLICATE_NAME; 1862 break; 1863 default: 1864 ret = SA_CONFIG_ERR; 1865 break; 1866 } 1867 1868 return (ret); 1869 } 1870