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