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