/*
* Store Windows ACLs in data store - common functions.
* #included into modules/vfs_acl_xattr.c and modules/vfs_acl_tdb.c
*
* Copyright (C) Volker Lendecke, 2008
* Copyright (C) Jeremy Allison, 2009
* Copyright (C) Ralph Böhme, 2016
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "smbd/smbd.h"
#include "system/filesys.h"
#include "../libcli/security/security.h"
#include "../librpc/gen_ndr/ndr_security.h"
#include "../lib/util/bitmap.h"
#include "passdb/lookup_sid.h"
static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
DATA_BLOB *pblob,
uint16_t hash_type,
uint8_t hash[XATTR_SD_HASH_SIZE]);
static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
vfs_handle_struct *handle,
files_struct *fsp,
const struct smb_filename *smb_fname,
DATA_BLOB *pblob);
static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
files_struct *fsp,
DATA_BLOB *pblob);
#define HASH_SECURITY_INFO (SECINFO_OWNER | \
SECINFO_GROUP | \
SECINFO_DACL | \
SECINFO_SACL)
enum default_acl_style {DEFAULT_ACL_POSIX, DEFAULT_ACL_WINDOWS};
static const struct enum_list default_acl_style[] = {
{DEFAULT_ACL_POSIX, "posix"},
{DEFAULT_ACL_WINDOWS, "windows"}
};
struct acl_common_config {
bool ignore_system_acls;
enum default_acl_style default_acl_style;
};
static bool init_acl_common_config(vfs_handle_struct *handle)
{
struct acl_common_config *config = NULL;
config = talloc_zero(handle->conn, struct acl_common_config);
if (config == NULL) {
DBG_ERR("talloc_zero() failed\n");
errno = ENOMEM;
return false;
}
config->ignore_system_acls = lp_parm_bool(SNUM(handle->conn),
ACL_MODULE_NAME,
"ignore system acls",
false);
config->default_acl_style = lp_parm_enum(SNUM(handle->conn),
ACL_MODULE_NAME,
"default acl style",
default_acl_style,
DEFAULT_ACL_POSIX);
SMB_VFS_HANDLE_SET_DATA(handle, config, NULL,
struct acl_common_config,
return false);
return true;
}
/*******************************************************************
Hash a security descriptor.
*******************************************************************/
static NTSTATUS hash_blob_sha256(DATA_BLOB blob,
uint8_t *hash)
{
SHA256_CTX tctx;
memset(hash, '\0', XATTR_SD_HASH_SIZE);
samba_SHA256_Init(&tctx);
samba_SHA256_Update(&tctx, blob.data, blob.length);
samba_SHA256_Final(hash, &tctx);
return NT_STATUS_OK;
}
/*******************************************************************
Hash a security descriptor.
*******************************************************************/
static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
uint8_t *hash)
{
DATA_BLOB blob;
NTSTATUS status;
memset(hash, '\0', XATTR_SD_HASH_SIZE);
status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
return hash_blob_sha256(blob, hash);
}
/*******************************************************************
Parse out a struct security_descriptor from a DATA_BLOB.
*******************************************************************/
static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
TALLOC_CTX *mem_ctx,
struct security_descriptor **ppdesc,
uint16_t *p_hash_type,
uint16_t *p_version,
uint8_t hash[XATTR_SD_HASH_SIZE],
uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
{
struct xattr_NTACL xacl;
enum ndr_err_code ndr_err;
size_t sd_size;
TALLOC_CTX *frame = talloc_stackframe();
ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
(ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
DBG_INFO("ndr_pull_xattr_NTACL failed: %s\n",
ndr_errstr(ndr_err));
TALLOC_FREE(frame);
return ndr_map_error2ntstatus(ndr_err);
}
*p_version = xacl.version;
switch (xacl.version) {
case 1:
*ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
xacl.info.sd->owner_sid,
xacl.info.sd->group_sid,
xacl.info.sd->sacl,
xacl.info.sd->dacl,
&sd_size);
/* No hash - null out. */
*p_hash_type = XATTR_SD_HASH_TYPE_NONE;
memset(hash, '\0', XATTR_SD_HASH_SIZE);
break;
case 2:
*ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
xacl.info.sd_hs2->sd->owner_sid,
xacl.info.sd_hs2->sd->group_sid,
xacl.info.sd_hs2->sd->sacl,
xacl.info.sd_hs2->sd->dacl,
&sd_size);
/* No hash - null out. */
*p_hash_type = XATTR_SD_HASH_TYPE_NONE;
memset(hash, '\0', XATTR_SD_HASH_SIZE);
break;
case 3:
*ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
xacl.info.sd_hs3->sd->owner_sid,
xacl.info.sd_hs3->sd->group_sid,
xacl.info.sd_hs3->sd->sacl,
xacl.info.sd_hs3->sd->dacl,
&sd_size);
*p_hash_type = xacl.info.sd_hs3->hash_type;
/* Current version 3 (if no sys acl hash available). */
memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
break;
case 4:
*ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
xacl.info.sd_hs4->sd->type | SEC_DESC_SELF_RELATIVE,
xacl.info.sd_hs4->sd->owner_sid,
xacl.info.sd_hs4->sd->group_sid,
xacl.info.sd_hs4->sd->sacl,
xacl.info.sd_hs4->sd->dacl,
&sd_size);
*p_hash_type = xacl.info.sd_hs4->hash_type;
/* Current version 4. */
memcpy(hash, xacl.info.sd_hs4->hash, XATTR_SD_HASH_SIZE);
memcpy(sys_acl_hash, xacl.info.sd_hs4->sys_acl_hash, XATTR_SD_HASH_SIZE);
break;
default:
TALLOC_FREE(frame);
return NT_STATUS_REVISION_MISMATCH;
}
TALLOC_FREE(frame);
return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
}
/*******************************************************************
Create a DATA_BLOB from a hash of the security descriptor storead at
the system layer and the NT ACL we wish to preserve
*******************************************************************/
static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
DATA_BLOB *pblob,
uint16_t hash_type,
uint8_t hash[XATTR_SD_HASH_SIZE])
{
struct xattr_NTACL xacl;
struct security_descriptor_hash_v3 sd_hs3;
enum ndr_err_code ndr_err;
TALLOC_CTX *ctx = talloc_tos();
ZERO_STRUCT(xacl);
ZERO_STRUCT(sd_hs3);
xacl.version = 3;
xacl.info.sd_hs3 = &sd_hs3;
xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
xacl.info.sd_hs3->hash_type = hash_type;
memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
ndr_err = ndr_push_struct_blob(
pblob, ctx, &xacl,
(ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
ndr_errstr(ndr_err));
return ndr_map_error2ntstatus(ndr_err);
}
return NT_STATUS_OK;
}
/*******************************************************************
Create a DATA_BLOB from a hash of the security descriptors
(system and NT) stored at the system layer and the NT ACL we wish
to preserve.
*******************************************************************/
static NTSTATUS create_sys_acl_blob(const struct security_descriptor *psd,
DATA_BLOB *pblob,
uint16_t hash_type,
uint8_t hash[XATTR_SD
|