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) smb_config_refresh(); 1328 else if (opt->refresh & SMB_REFRESH_RESTART) 1329 (void) smf_restart_instance( 1330 SMBD_DEFAULT_INSTANCE_FMRI); 1331 } 1332 } 1333 } 1334 1335 if (name != NULL) 1336 sa_free_attr_string(name); 1337 if (value != NULL) 1338 sa_free_attr_string(value); 1339 1340 return (ret); 1341 } 1342 1343 /* 1344 * smb_get_status() 1345 * 1346 * What is the current status of the smbd? We use the SMF state here. 1347 * Caller must free the returned value. 1348 */ 1349 1350 static char * 1351 smb_get_status(void) 1352 { 1353 char *state = NULL; 1354 state = smf_get_state(SMBD_DEFAULT_INSTANCE_FMRI); 1355 return (state != NULL ? state : "-"); 1356 } 1357 1358 /* 1359 * This protocol plugin require resource names 1360 */ 1361 static uint64_t 1362 smb_share_features(void) 1363 { 1364 return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS | 1365 SA_FEATURE_ALLOWPARDIRS); 1366 } 1367 1368 /* 1369 * This should be used to convert lmshare_info to sa_resource_t 1370 * Should only be needed to build temp shares/resources to be 1371 * supplied to sharemanager to display temp shares. 1372 */ 1373 static int 1374 smb_build_tmp_sa_resource(sa_handle_t handle, lmshare_info_t *si) 1375 { 1376 int err; 1377 sa_share_t share; 1378 sa_group_t group; 1379 sa_resource_t resource; 1380 1381 if (si == NULL) 1382 return (SA_INVALID_NAME); 1383 1384 /* 1385 * First determine if the "share path" is already shared 1386 * somewhere. If it is, we have to use it as the authority on 1387 * where the transient share lives so will use it's parent 1388 * group. If it doesn't exist, it needs to land in "smb". 1389 */ 1390 1391 share = sa_find_share(handle, si->directory); 1392 if (share != NULL) { 1393 group = sa_get_parent_group(share); 1394 } else { 1395 group = smb_get_smb_share_group(handle); 1396 if (group == NULL) 1397 return (SA_NO_SUCH_GROUP); 1398 share = sa_get_share(group, si->directory); 1399 if (share == NULL) { 1400 share = sa_add_share(group, si->directory, 1401 SA_SHARE_TRANSIENT, &err); 1402 if (share == NULL) 1403 return (SA_NO_SUCH_PATH); 1404 } 1405 } 1406 1407 /* 1408 * Now handle the resource. Make sure that the resource is 1409 * transient and added to the share. 1410 */ 1411 resource = sa_get_share_resource(share, si->share_name); 1412 if (resource == NULL) { 1413 resource = sa_add_resource(share, 1414 si->share_name, SA_SHARE_TRANSIENT, &err); 1415 if (resource == NULL) 1416 return (SA_NO_SUCH_RESOURCE); 1417 } 1418 1419 /* set resource attributes now */ 1420 (void) sa_set_resource_attr(resource, "description", si->comment); 1421 (void) sa_set_resource_attr(resource, SHOPT_AD_CONTAINER, 1422 si->container); 1423 1424 return (SA_OK); 1425 } 1426 1427 /* 1428 * Return smb transient shares. Note that we really want to look at 1429 * all current shares from SMB in order to determine this. Transient 1430 * shares should be those that don't appear in either the SMF or ZFS 1431 * configurations. Those that are in the repositories will be 1432 * filtered out by smb_build_tmp_sa_resource. 1433 */ 1434 static int 1435 smb_list_transient(sa_handle_t handle) 1436 { 1437 int i, offset, num; 1438 lmshare_list_t list; 1439 int res; 1440 1441 num = lmshrd_num_shares(); 1442 if (num <= 0) 1443 return (SA_OK); 1444 offset = 0; 1445 while (lmshrd_list(offset, &list) != NERR_InternalError) { 1446 if (list.no == 0) 1447 break; 1448 for (i = 0; i < list.no; i++) { 1449 res = smb_build_tmp_sa_resource(handle, 1450 &(list.smbshr[i])); 1451 if (res != SA_OK) 1452 return (res); 1453 } 1454 offset += list.no; 1455 } 1456 1457 return (SA_OK); 1458 } 1459 1460 /* 1461 * fix_resource_name(share, name, prefix) 1462 * 1463 * Construct a name where the ZFS dataset has the prefix replaced with "name". 1464 */ 1465 static char * 1466 fix_resource_name(sa_share_t share, char *name, char *prefix) 1467 { 1468 char *dataset = NULL; 1469 char *newname = NULL; 1470 size_t psize; 1471 size_t nsize; 1472 1473 dataset = sa_get_share_attr(share, "dataset"); 1474 1475 if (dataset != NULL && strcmp(dataset, prefix) != 0) { 1476 psize = strlen(prefix); 1477 if (strncmp(dataset, prefix, psize) == 0) { 1478 /* need string plus ',' and NULL */ 1479 nsize = (strlen(dataset) - psize) + strlen(name) + 2; 1480 newname = calloc(nsize, 1); 1481 if (newname != NULL) { 1482 (void) snprintf(newname, nsize, "%s%s", name, 1483 dataset + psize); 1484 sa_fix_resource_name(newname); 1485 } 1486 sa_free_attr_string(dataset); 1487 return (newname); 1488 } 1489 } 1490 if (dataset != NULL) 1491 sa_free_attr_string(dataset); 1492 return (strdup(name)); 1493 } 1494 1495 /* 1496 * smb_parse_optstring(group, options) 1497 * 1498 * parse a compact option string into individual options. This allows 1499 * ZFS sharesmb and sharemgr "share" command to work. group can be a 1500 * group, a share or a resource. 1501 */ 1502 static int 1503 smb_parse_optstring(sa_group_t group, char *options) 1504 { 1505 char *dup; 1506 char *base; 1507 char *lasts; 1508 char *token; 1509 sa_optionset_t optionset; 1510 sa_group_t parent = NULL; 1511 sa_resource_t resource = NULL; 1512 int iszfs = 0; 1513 int persist = 0; 1514 int need_optionset = 0; 1515 int ret = SA_OK; 1516 sa_property_t prop; 1517 1518 /* 1519 * In order to not attempt to change ZFS properties unless 1520 * absolutely necessary, we never do it in the legacy parsing 1521 * so we need to keep track of this. 1522 */ 1523 if (sa_is_share(group)) { 1524 char *zfs; 1525 1526 parent = sa_get_parent_group(group); 1527 if (parent != NULL) { 1528 zfs = sa_get_group_attr(parent, "zfs"); 1529 if (zfs != NULL) { 1530 sa_free_attr_string(zfs); 1531 iszfs = 1; 1532 } 1533 } 1534 } else { 1535 iszfs = sa_group_is_zfs(group); 1536 /* 1537 * If a ZFS group, then we need to see if a resource 1538 * name is being set. If so, bail with 1539 * SA_PROP_SHARE_ONLY, so we come back in with a share 1540 * instead of a group. 1541 */ 1542 if (strncmp(options, "name=", sizeof ("name=") - 1) == 0 || 1543 strstr(options, ",name=") != NULL) { 1544 return (SA_PROP_SHARE_ONLY); 1545 } 1546 } 1547 1548 /* do we have an existing optionset? */ 1549 optionset = sa_get_optionset(group, "smb"); 1550 if (optionset == NULL) { 1551 /* didn't find existing optionset so create one */ 1552 optionset = sa_create_optionset(group, "smb"); 1553 if (optionset == NULL) 1554 return (SA_NO_MEMORY); 1555 } else { 1556 /* 1557 * If an optionset already exists, we've come through 1558 * twice so ignore the second time. 1559 */ 1560 return (ret); 1561 } 1562 1563 /* We need a copy of options for the next part. */ 1564 dup = strdup(options); 1565 if (dup == NULL) 1566 return (SA_NO_MEMORY); 1567 1568 /* 1569 * SMB properties are straightforward and are strings, 1570 * integers or booleans. Properties are separated by 1571 * commas. It will be necessary to parse quotes due to some 1572 * strings not having a restricted characters set. 1573 * 1574 * Note that names will create a resource. For now, if there 1575 * is a set of properties "before" the first name="", those 1576 * properties will be placed on the group. 1577 */ 1578 persist = sa_is_persistent(group); 1579 base = dup; 1580 token = dup; 1581 lasts = NULL; 1582 while (token != NULL && ret == SA_OK) { 1583 ret = SA_OK; 1584 token = strtok_r(base, ",", &lasts); 1585 base = NULL; 1586 if (token != NULL) { 1587 char *value; 1588 /* 1589 * All SMB properties have values so there 1590 * MUST be an '=' character. If it doesn't, 1591 * it is a syntax error. 1592 */ 1593 value = strchr(token, '='); 1594 if (value != NULL) { 1595 *value++ = '\0'; 1596 } else { 1597 ret = SA_SYNTAX_ERR; 1598 break; 1599 } 1600 /* 1601 * We may need to handle a "name" property 1602 * that is a ZFS imposed resource name. Each 1603 * name would trigger getting a new "resource" 1604 * to put properties on. For now, assume no 1605 * "name" property for special handling. 1606 */ 1607 1608 if (strcmp(token, "name") == 0) { 1609 char *prefix; 1610 char *name = NULL; 1611 /* 1612 * We have a name, so now work on the 1613 * resource level. We have a "share" 1614 * in "group" due to the caller having 1615 * added it. If we are called with a 1616 * group, the check for group/share 1617 * at the beginning of this function 1618 * will bail out the parse if there is a 1619 * "name" but no share. 1620 */ 1621 if (!iszfs) { 1622 ret = SA_SYNTAX_ERR; 1623 break; 1624 } 1625 /* 1626 * Make sure the parent group has the 1627 * "prefix" property since we will 1628 * need to use this for constructing 1629 * inherited name= values. 1630 */ 1631 prefix = sa_get_group_attr(parent, "prefix"); 1632 if (prefix == NULL) { 1633 prefix = sa_get_group_attr(parent, 1634 "name"); 1635 if (prefix != NULL) { 1636 (void) sa_set_group_attr(parent, 1637 "prefix", prefix); 1638 } 1639 } 1640 name = fix_resource_name((sa_share_t)group, 1641 value, prefix); 1642 if (name != NULL) { 1643 resource = sa_add_resource( 1644 (sa_share_t)group, name, 1645 SA_SHARE_TRANSIENT, &ret); 1646 sa_free_attr_string(name); 1647 } else { 1648 ret = SA_NO_MEMORY; 1649 } 1650 if (prefix != NULL) 1651 sa_free_attr_string(prefix); 1652 1653 /* A resource level optionset is needed */ 1654 1655 need_optionset = 1; 1656 if (resource == NULL) { 1657 ret = SA_NO_MEMORY; 1658 break; 1659 } 1660 continue; 1661 } 1662 1663 if (need_optionset) { 1664 optionset = sa_create_optionset(resource, 1665 "smb"); 1666 need_optionset = 0; 1667 } 1668 1669 prop = sa_create_property(token, value); 1670 if (prop == NULL) 1671 ret = SA_NO_MEMORY; 1672 else 1673 ret = sa_add_property(optionset, prop); 1674 if (ret != SA_OK) 1675 break; 1676 if (!iszfs) 1677 ret = sa_commit_properties(optionset, !persist); 1678 } 1679 } 1680 free(dup); 1681 return (ret); 1682 } 1683 1684 /* 1685 * smb_sprint_option(rbuff, rbuffsize, incr, prop, sep) 1686 * 1687 * provides a mechanism to format SMB properties into legacy output 1688 * format. If the buffer would overflow, it is reallocated and grown 1689 * as appropriate. Special cases of converting internal form of values 1690 * to those used by "share" are done. this function does one property 1691 * at a time. 1692 */ 1693 1694 static void 1695 smb_sprint_option(char **rbuff, size_t *rbuffsize, size_t incr, 1696 sa_property_t prop, int sep) 1697 { 1698 char *name; 1699 char *value; 1700 int curlen; 1701 char *buff = *rbuff; 1702 size_t buffsize = *rbuffsize; 1703 1704 name = sa_get_property_attr(prop, "type"); 1705 value = sa_get_property_attr(prop, "value"); 1706 if (buff != NULL) 1707 curlen = strlen(buff); 1708 else 1709 curlen = 0; 1710 if (name != NULL) { 1711 int len; 1712 len = strlen(name) + sep; 1713 1714 /* 1715 * A future RFE would be to replace this with more 1716 * generic code and to possibly handle more types. 1717 * 1718 * For now, everything else is treated as a string. If 1719 * we get any properties that aren't exactly 1720 * name/value pairs, we may need to 1721 * interpret/transform. 1722 */ 1723 if (value != NULL) 1724 len += 1 + strlen(value); 1725 1726 while (buffsize <= (curlen + len)) { 1727 /* need more room */ 1728 buffsize += incr; 1729 buff = realloc(buff, buffsize); 1730 *rbuff = buff; 1731 *rbuffsize = buffsize; 1732 if (buff == NULL) { 1733 /* realloc failed so free everything */ 1734 if (*rbuff != NULL) 1735 free(*rbuff); 1736 goto err; 1737 } 1738 } 1739 if (buff == NULL) 1740 goto err; 1741 (void) snprintf(buff + curlen, buffsize - curlen, 1742 "%s%s=%s", sep ? "," : "", 1743 name, value != NULL ? value : "\"\""); 1744 1745 } 1746 err: 1747 if (name != NULL) 1748 sa_free_attr_string(name); 1749 if (value != NULL) 1750 sa_free_attr_string(value); 1751 } 1752 1753 /* 1754 * smb_format_resource_options(resource, hier) 1755 * 1756 * format all the options on the group into a flattened option 1757 * string. If hier is non-zero, walk up the tree to get inherited 1758 * options. 1759 */ 1760 1761 static char * 1762 smb_format_options(sa_group_t group, int hier) 1763 { 1764 sa_optionset_t options = NULL; 1765 sa_property_t prop; 1766 int sep = 0; 1767 char *buff; 1768 size_t buffsize; 1769 1770 1771 buff = malloc(OPT_CHUNK); 1772 if (buff == NULL) 1773 return (NULL); 1774 1775 buff[0] = '\0'; 1776 buffsize = OPT_CHUNK; 1777 1778 /* 1779 * We may have a an optionset relative to this item. format 1780 * these if we find them and then add any security definitions. 1781 */ 1782 1783 options = sa_get_derived_optionset(group, "smb", hier); 1784 1785 /* 1786 * do the default set first but skip any option that is also 1787 * in the protocol specific optionset. 1788 */ 1789 if (options != NULL) { 1790 for (prop = sa_get_property(options, NULL); 1791 prop != NULL; prop = sa_get_next_property(prop)) { 1792 /* 1793 * use this one since we skipped any 1794 * of these that were also in 1795 * optdefault 1796 */ 1797 smb_sprint_option(&buff, &buffsize, OPT_CHUNK, 1798 prop, sep); 1799 if (buff == NULL) { 1800 /* 1801 * buff could become NULL if there 1802 * isn't enough memory for 1803 * smb_sprint_option to realloc() 1804 * as necessary. We can't really 1805 * do anything about it at this 1806 * point so we return NULL. The 1807 * caller should handle the 1808 * failure. 1809 */ 1810 if (options != NULL) 1811 sa_free_derived_optionset( 1812 options); 1813 return (buff); 1814 } 1815 sep = 1; 1816 } 1817 } 1818 1819 if (options != NULL) 1820 sa_free_derived_optionset(options); 1821 return (buff); 1822 } 1823 1824 /* 1825 * smb_rename_resource(resource, newname) 1826 * 1827 * Change the current exported name of the resource to newname. 1828 */ 1829 /*ARGSUSED*/ 1830 int 1831 smb_rename_resource(sa_handle_t handle, sa_resource_t resource, char *newname) 1832 { 1833 int ret = SA_OK; 1834 int err; 1835 char *oldname; 1836 1837 oldname = sa_get_resource_attr(resource, "name"); 1838 if (oldname == NULL) 1839 return (SA_NO_SUCH_RESOURCE); 1840 1841 err = lmshrd_rename(oldname, newname); 1842 1843 /* improve error values somewhat */ 1844 switch (err) { 1845 case NERR_Success: 1846 break; 1847 case NERR_InternalError: 1848 ret = SA_SYSTEM_ERR; 1849 break; 1850 case NERR_DuplicateShare: 1851 ret = SA_DUPLICATE_NAME; 1852 break; 1853 default: 1854 ret = SA_CONFIG_ERR; 1855 break; 1856 } 1857 1858 return (ret); 1859 } 1860