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