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