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