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