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