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