libshare_smb.c revision 8e314a4400960b022e9b9135577c1c0ceebd1e36
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* SMB specific functions
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <zone.h>
#include <errno.h>
#include <locale.h>
#include <fcntl.h>
#include <syslog.h>
#include "libshare.h"
#include "libshare_impl.h"
#include <pwd.h>
#include <limits.h>
#include <libscf.h>
#include <strings.h>
#include "libshare_smb.h"
#include <rpcsvc/daemon_utils.h>
#include <smbsrv/lmshare_door.h>
/* internal functions */
static int smb_share_init(void);
static void smb_share_fini(void);
static int smb_enable_share(sa_share_t);
static int smb_share_changed(sa_share_t);
static int smb_resource_changed(sa_resource_t);
static int smb_set_proto_prop(sa_property_t);
static sa_protocol_properties_t smb_get_proto_set(void);
static char *smb_get_status(void);
static int smb_parse_optstring(sa_group_t, char *);
static char *smb_format_options(sa_group_t, int);
static int smb_enable_service(void);
static int range_check_validator(int, char *);
static int range_check_validator_zero_ok(int, char *);
static int string_length_check_validator(int, char *);
static int true_false_validator(int, char *);
static int ip_address_validator_empty_ok(int, char *);
static int ip_address_csv_list_validator_empty_ok(int, char *);
static int path_validator(int, char *);
static int smb_enable_resource(sa_resource_t);
static int smb_disable_resource(sa_resource_t);
static uint64_t smb_share_features(void);
static int smb_list_transient(sa_handle_t);
extern void lmshrd_door_close(void);
/* size of basic format allocation */
#define OPT_CHUNK 1024
/* size of string for types - big enough to hold "dependency" */
#define SCFTYPE_LEN 32
/*
* Indexes of entries in smb_proto_options table.
* Changes to smb_proto_options table may require
* an update to these values.
*/
#define PROTO_OPT_WINS1 6
#define PROTO_OPT_WINS_EXCLUDE 8
/*
* ops vector that provides the protocol specific info and operations
* for share management.
*/
struct sa_plugin_ops sa_plugin_ops = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
/*
* option definitions. Make sure to keep the #define for the option
* index just before the entry it is the index for. Changing the order
* can cause breakage.
*/
struct option_defs optdefs[] = {
};
/*
* findopt(name)
*
* Lookup option "name" in the option table and return the table
* index.
*/
static int
{
int i;
return (i);
}
}
return (-1);
}
/*
* is_a_number(number)
*
* is the string a number in one of the forms we want to use?
*/
static int
is_a_number(char *number)
{
int ret = 1;
int hex = 0;
number += 2;
hex = 1;
} else if (*number == '-') {
number++; /* skip the minus */
}
if (hex) {
} else {
}
}
return (ret);
}
/*
* validresource(name)
*
* Check that name only has valid characters in it. The current valid
* set are the printable characters but not including:
* " / \ [ ] : | < > + ; , ? * = \t
* Note that space is included and there is a maximum length.
*/
static int
validresource(const char *name)
{
const char *cp;
return (B_FALSE);
return (B_FALSE);
return (B_FALSE);
}
return (B_FALSE);
return (B_TRUE);
}
/*
* smb_isonline()
*
* Determine if the SMF service instance is in the online state or
* not. A number of operations depend on this state.
*/
static boolean_t
smb_isonline(void)
{
char *str;
}
return (ret);
}
/*
* smb_isdisabled()
*
* Determine if the SMF service instance is in the disabled state or
* not. A number of operations depend on this state.
*/
static boolean_t
smb_isdisabled(void)
{
char *str;
}
return (ret);
}
/*
* smb_isautoenable()
*
* Determine if the SMF service instance auto_enabled set or not. A
* number of operations depend on this state. The property not being
* set or being set to true means autoenable. Only being set to false
* is not autoenabled.
*/
static boolean_t
smb_isautoenable(void)
{
"application", "auto_enable");
}
return (ret);
}
/*
* smb_enable_share tells the implementation that it is to enable the share.
* This entails converting the path and options into the appropriate ioctl
* calls. It is assumed that all error checking of paths, etc. were
* done earlier.
*/
static int
{
char *path;
char *rname;
/* get the path since it is important in several places */
return (SA_NO_SUCH_PATH);
/*
* If administratively disabled, don't try to start anything.
*/
online = smb_isonline();
goto done;
if (iszfs) {
if (!online) {
"SMB: Cannot share remove "
"file system: %s\n"), path);
"SMB: Service needs to be enabled "
"by a privileged user\n"));
}
if (err) {
return (err);
}
}
}
err = smb_enable_service();
"SMB: Unable to enable service\n"));
/*
* For now, it is OK to not be able to enable
* the service.
*/
} else {
}
}
/*
* Don't bother trying to start shares if the service isn't
* running.
*/
if (!online)
goto done;
/* Each share can have multiple resources */
return (SA_NO_SUCH_RESOURCE);
}
if (!iszfs) {
} else {
&si, ZFS_SHARE_SMB);
sa_emptyshare(&sh);
}
}
if (!iszfs)
done:
}
/*
* This is the share for CIFS all shares have resource names.
* Enable tells the smb server to update its hash. If it fails
* because smb server is down, we just ignore as smb server loads
* the resources from sharemanager at startup.
*/
static int
{
char *path;
char *rname;
int err;
return (SA_NO_SUCH_PATH);
/*
* If administratively disabled, don't try to start anything.
*/
isonline = smb_isonline();
goto done;
if (!isonline)
ret = smb_enable_service();
if (!smb_isonline()) {
goto done;
}
return (SA_SYSTEM_ERR);
return (SA_NO_SUCH_RESOURCE);
}
/*
* Attempt to add the share. Any error that occurs if it was
* online is an error but don't count NERR_DuplicateName if
* service up will enable the share that was just added prior
* to the attempt to enable.
*/
else
return (SA_NOT_SHARED);
done:
return (ret);
}
/*
* Remove it from smb server hash.
*/
static int
{
char *rname;
return (SA_NO_SUCH_RESOURCE);
if (smb_isonline()) {
if (res != NERR_Success) {
return (SA_CONFIG_ERR);
}
}
}
}
/*
* Shares will be picked up when loaded.
*/
return (SA_OK);
}
/*
* smb_share_changed(sa_share_t share)
*
* The specified share has changed.
*/
static int
{
char *path;
/* get the path since it is important in several places */
return (SA_NO_SUCH_PATH);
(void) smb_resource_changed(resource);
return (SA_OK);
}
/*
* smb_resource_changed(sa_resource_t resource)
*
* The specified resource has changed.
*/
static int
{
return (SA_NO_SUCH_RESOURCE);
return (SA_CONFIG_ERR);
}
return (SA_NO_SUCH_PATH);
}
if (!smb_isonline()) {
return (SA_OK);
}
if (res != NERR_Success) {
return (SA_CONFIG_ERR);
}
/*
* Update all fields from sa_share_t
* Get derived values.
*/
return (SA_CONFIG_ERR);
return (smb_enable_service());
}
/*
* smb_disable_share(sa_share_t share, char *path)
*
* Unshare the specified share. Note that "path" is the same
* path as what is in the "share" object. It is passed in to avoid an
* additional lookup. A missing "path" value makes this a no-op
* function.
*/
static int
{
char *rname;
return (err);
/*
* If the share is in a ZFS group we need to handle it
* differently. Just being on a ZFS file system isn't
* enough since we may be in a legacy share case.
*/
if (!smb_isonline())
goto done;
continue;
}
if (!iszfs) {
switch (err) {
case NERR_NetNameNotFound:
case NERR_Success:
break;
default:
err = SA_CONFIG_ERR;
break;
}
} else {
sa_emptyshare(&sh);
}
}
done:
if (!iszfs)
return (err);
}
/*
* smb_validate_property(property, parent)
*
* Check that the property has a legitimate value for its type.
*/
static int
{
char *propname;
int optindex;
char *value;
/* need to validate value range here as well */
}
return (ret);
}
/* first basic type checking */
case OPT_TYPE_NUMBER:
/* check that the value is all digits */
if (!is_a_number(value))
ret = SA_BAD_VALUE;
break;
case OPT_TYPE_BOOLEAN:
} else {
ret = SA_BAD_VALUE;
}
break;
case OPT_TYPE_NAME:
/*
* Make sure no invalid characters
*/
ret = SA_BAD_VALUE;
break;
case OPT_TYPE_STRING:
/* whatever is here should be ok */
break;
default:
break;
}
}
/* do the property specific check */
return (ret);
}
/*
* Protocol management functions
*
* properties defined in the default files are defined in
* proto_option_defs for parsing and validation.
*/
struct smb_proto_option_defs {
int smb_index;
int (*validator)(int, char *);
} smb_proto_options[] = {
{ SMB_CI_SYS_CMNT, 0, MAX_VALUE_BUFLEN,
{ SMB_CI_NBSCOPE, 0, MAX_VALUE_BUFLEN,
{ SMB_CI_WINS_SRV1, 0, MAX_VALUE_BUFLEN,
{ SMB_CI_WINS_SRV2, 0, MAX_VALUE_BUFLEN,
{ SMB_CI_WINS_EXCL, 0, MAX_VALUE_BUFLEN,
{ SMB_CI_SIGNING_ENABLE, 0, 0, true_false_validator,
{ SMB_CI_SIGNING_REQD, 0, 0, true_false_validator,
{ SMB_CI_RESTRICT_ANON, 0, 0, true_false_validator,
{ SMB_CI_ADS_SITE, 0, MAX_VALUE_BUFLEN,
{ SMB_CI_DYNDNS_ENABLE, 0, 0, true_false_validator, 0 },
};
#define SMB_OPT_NUM \
(sizeof (smb_proto_options) / sizeof (smb_proto_options[0]))
/*
* Check the range of value as int range.
*/
static int
{
if (!is_a_number(value)) {
ret = SA_BAD_VALUE;
} else {
int val;
ret = SA_BAD_VALUE;
}
return (ret);
}
/*
* Check the range of value as int range.
*/
static int
{
if (!is_a_number(value)) {
ret = SA_BAD_VALUE;
} else {
int val;
if (val == 0)
else {
ret = SA_BAD_VALUE;
}
}
return (ret);
}
/*
* Check the length of the string
*/
static int
{
return (SA_BAD_VALUE);
ret = SA_BAD_VALUE;
return (ret);
}
/*
*/
/*ARGSUSED*/
static int
{
return (SA_BAD_VALUE);
return (SA_OK);
return (SA_BAD_VALUE);
}
/*
* Check IP address.
*/
/*ARGSUSED*/
static int
{
char sbytes[16];
int len;
return (SA_OK);
if (len == 0)
return (SA_OK);
return (SA_BAD_VALUE);
return (SA_OK);
}
/*
* Check IP address list
*/
/*ARGSUSED*/
static int
{
char sbytes[16];
return (SA_OK);
return (SA_BAD_VALUE);
return (SA_NO_MEMORY);
while (ip) {
return (SA_BAD_VALUE);
}
if (*ip != 0) {
(void *)sbytes) != 1) {
return (SA_BAD_VALUE);
}
}
}
return (SA_OK);
}
/*
* Check path
*/
/*ARGSUSED*/
static int
{
return (SA_BAD_VALUE);
if (fd < 0)
return (SA_BAD_VALUE);
if (status < 0)
return (SA_BAD_VALUE);
return (SA_OK);
return (SA_BAD_VALUE);
}
/*
* the protoset holds the defined options so we don't have to read
* them multiple times
*/
static sa_protocol_properties_t protoset;
static int
findprotoopt(char *name)
{
int i;
char *sc_name;
for (i = 0; i < SMB_OPT_NUM; i++) {
return (i);
}
return (-1);
}
/*
* smb_load_proto_properties()
*
* read the smb config values from SMF.
*/
static int
{
char value[MAX_VALUE_BUFLEN];
char *name;
int index;
int rc;
return (SA_NO_MEMORY);
if (rc != SMBD_SMF_OK)
continue;
}
return (SA_OK);
}
/*
* smb_share_init()
*
* Initialize the smb plugin.
*/
static int
smb_share_init(void)
{
return (SA_SYSTEM_ERR);
if (smb_load_proto_properties() != SA_OK)
return (SA_SYSTEM_ERR);
return (ret);
}
/*
* smb_share_fini()
*
*/
static void
smb_share_fini(void)
{
(void) lmshrd_door_close();
}
/*
* smb_get_proto_set()
*
* Return an optionset with all the protocol specific properties in
* it.
*/
static sa_protocol_properties_t
smb_get_proto_set(void)
{
return (protoset);
}
/*
* smb_enable_dependencies()
*
* SMBD_DEFAULT_INSTANCE_FMRI may have some dependencies that aren't
* enabled. This will attempt to enable all of them.
*/
static void
smb_enable_dependencies(const char *fmri)
{
char type[SCFTYPE_LEN];
char *dependency;
char *servname;
int maxlen;
/*
* Get all required handles and storage.
*/
return;
if (scf_handle_bind(handle) != 0) {
return;
}
maxlen = MAXPATHLEN;
goto done;
/*
* We passed in the FMRI for the default instance but for
* some things we need the simple form so construct it. Since
* we reuse the storage that dependency points to, we need to
* use the servname early.
*/
goto done;
*servname = '\0';
/*
* Setup to iterate over the service property groups, only
* looking at those that are "dependency" types. The "entity"
* property will have the FMRI of the service we are dependent
* on.
*/
goto done;
goto done;
goto done;
char *services[2];
/*
* Have a property group for the service. See if it is
* a dependency pg and only do operations on those.
*/
continue;
continue;
/*
* Have a dependency. Attempt to enable it.
*/
continue;
continue;
services[0] = dependency;
}
}
done:
if (dependency != NULL)
(void) scf_handle_unbind(handle);
}
/*
* How long to wait for service to come online
*/
#define WAIT_FOR_SERVICE 15
/*
* smb_enable_service()
*
*/
static int
smb_enable_service(void)
{
int i;
if (!smb_isonline()) {
/*
* Attempt to start the idmap, and other dependent
* services, first. If it fails, the SMB service will
* ultimately fail so we use that as the error. If we
* don't try to enable idmap, smb won't start the
* first time unless the admin has done it
* manually. The service could be administratively
* disabled so we won't always get started.
*/
/* Wait for service to come online */
for (i = 0; i < WAIT_FOR_SERVICE; i++) {
if (smb_isonline()) {
break;
} else {
(void) sleep(1);
}
}
}
return (ret);
}
/*
* smb_validate_proto_prop(index, name, value)
*
* Verify that the property specified by name can take the new
* value. This is a sanity check to prevent bad values getting into
* the default files.
*/
static int
{
return (SA_BAD_VALUE);
return (SA_OK);
return (SA_OK);
return (SA_BAD_VALUE);
}
/*
* smb_set_proto_prop(prop)
*
* check that prop is valid.
*/
/*ARGSUSED*/
static int
{
char *name;
char *value;
int index = -1;
struct smb_proto_option_defs *opt;
if (index >= 0) {
/* should test for valid value */
/* Save to SMF */
/*
* Specialized refresh mechanisms can
* be flagged in the proto_options and
* processed here.
*/
(void) smb_config_refresh();
(void) smf_restart_instance(
}
}
}
return (ret);
}
/*
* smb_get_status()
*
* What is the current status of the smbd? We use the SMF state here.
* Caller must free the returned value.
*/
static char *
smb_get_status(void)
{
}
/*
* This protocol plugin require resource names
*/
static uint64_t
smb_share_features(void)
{
return (SA_FEATURE_RESOURCE | SA_FEATURE_ALLOWSUBDIRS |
}
/*
* This should be used to convert lmshare_info to sa_resource_t
* supplied to sharemanager to display temp shares.
*/
static int
{
int err;
return (SA_INVALID_NAME);
/*
* First determine if the "share path" is already shared
* somewhere. If it is, we have to use it as the authority on
* where the transient share lives so will use it's parent
* group. If it doesn't exist, it needs to land in "smb".
*/
} else {
return (SA_NO_SUCH_GROUP);
return (SA_NO_SUCH_PATH);
}
}
/*
* Now handle the resource. Make sure that the resource is
* transient and added to the share.
*/
return (SA_NO_SUCH_RESOURCE);
}
/* set resource attributes now */
return (SA_OK);
}
/*
* Return smb transient shares. Note that we really want to look at
* all current shares from SMB in order to determine this. Transient
* shares should be those that don't appear in either the SMF or ZFS
* configurations. Those that are in the repositories will be
* filtered out by smb_build_tmp_sa_resource.
*/
static int
{
int res;
num = lmshrd_num_shares();
if (num <= 0)
return (SA_OK);
offset = 0;
break;
return (res);
}
}
return (SA_OK);
}
/*
* fix_resource_name(share, name, prefix)
*
* Construct a name where the ZFS dataset has the prefix replaced with "name".
*/
static char *
{
/* need string plus ',' and NULL */
}
return (newname);
}
}
}
/*
* smb_parse_optstring(group, options)
*
* parse a compact option string into individual options. This allows
* ZFS sharesmb and sharemgr "share" command to work. group can be a
* group, a share or a resource.
*/
static int
{
char *dup;
char *base;
char *lasts;
char *token;
int iszfs = 0;
int persist = 0;
int need_optionset = 0;
/*
* In order to not attempt to change ZFS properties unless
* absolutely necessary, we never do it in the legacy parsing
* so we need to keep track of this.
*/
if (sa_is_share(group)) {
char *zfs;
iszfs = 1;
}
}
} else {
/*
* If a ZFS group, then we need to see if a resource
* name is being set. If so, bail with
* SA_PROP_SHARE_ONLY, so we come back in with a share
* instead of a group.
*/
return (SA_PROP_SHARE_ONLY);
}
}
/* do we have an existing optionset? */
/* didn't find existing optionset so create one */
return (SA_NO_MEMORY);
} else {
/*
* If an optionset already exists, we've come through
* twice so ignore the second time.
*/
return (ret);
}
/* We need a copy of options for the next part. */
return (SA_NO_MEMORY);
/*
* SMB properties are straightforward and are strings,
* integers or booleans. Properties are separated by
* commas. It will be necessary to parse quotes due to some
* strings not having a restricted characters set.
*
* Note that names will create a resource. For now, if there
* is a set of properties "before" the first name="", those
* properties will be placed on the group.
*/
char *value;
/*
* All SMB properties have values so there
* MUST be an '=' character. If it doesn't,
* it is a syntax error.
*/
*value++ = '\0';
} else {
ret = SA_SYNTAX_ERR;
break;
}
/*
* We may need to handle a "name" property
* that is a ZFS imposed resource name. Each
* name would trigger getting a new "resource"
* to put properties on. For now, assume no
* "name" property for special handling.
*/
char *prefix;
/*
* We have a name, so now work on the
* resource level. We have a "share"
* in "group" due to the caller having
* added it. If we are called with a
* at the beginning of this function
* will bail out the parse if there is a
* "name" but no share.
*/
if (!iszfs) {
ret = SA_SYNTAX_ERR;
break;
}
/*
* Make sure the parent group has the
* "prefix" property since we will
* need to use this for constructing
* inherited name= values.
*/
"name");
(void) sa_set_group_attr(parent,
"prefix", prefix);
}
}
} else {
ret = SA_NO_MEMORY;
}
/* A resource level optionset is needed */
need_optionset = 1;
ret = SA_NO_MEMORY;
break;
}
continue;
}
if (need_optionset) {
"smb");
need_optionset = 0;
}
ret = SA_NO_MEMORY;
else
break;
if (!iszfs)
}
}
return (ret);
}
/*
* smb_sprint_option(rbuff, rbuffsize, incr, prop, sep)
*
* provides a mechanism to format SMB properties into legacy output
* format. If the buffer would overflow, it is reallocated and grown
* as appropriate. Special cases of converting internal form of values
* to those used by "share" are done. this function does one property
* at a time.
*/
static void
{
char *name;
char *value;
int curlen;
else
curlen = 0;
int len;
/*
* A future RFE would be to replace this with more
* generic code and to possibly handle more types.
*
* For now, everything else is treated as a string. If
* we get any properties that aren't exactly
*/
/* need more room */
/* realloc failed so free everything */
goto err;
}
}
goto err;
}
err:
}
/*
* smb_format_resource_options(resource, hier)
*
* format all the options on the group into a flattened option
* string. If hier is non-zero, walk up the tree to get inherited
* options.
*/
static char *
{
int sep = 0;
char *buff;
return (NULL);
buff[0] = '\0';
/*
* We may have a an optionset relative to this item. format
* these if we find them and then add any security definitions.
*/
/*
* do the default set first but skip any option that is also
* in the protocol specific optionset.
*/
/*
* use this one since we skipped any
* of these that were also in
* optdefault
*/
/*
* buff could become NULL if there
* isn't enough memory for
* smb_sprint_option to realloc()
* as necessary. We can't really
* do anything about it at this
* point so we return NULL. The
* caller should handle the
* failure.
*/
options);
return (buff);
}
sep = 1;
}
}
return (buff);
}
/*
* smb_rename_resource(resource, newname)
*
* Change the current exported name of the resource to newname.
*/
/*ARGSUSED*/
int
{
int err;
char *oldname;
return (SA_NO_SUCH_RESOURCE);
/* improve error values somewhat */
switch (err) {
case NERR_Success:
break;
case NERR_InternalError:
ret = SA_SYSTEM_ERR;
break;
case NERR_DuplicateShare:
break;
default:
ret = SA_CONFIG_ERR;
break;
}
return (ret);
}