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