2 * Store Windows ACLs in data store - common functions.
3 * #included into modules/vfs_acl_xattr.c and modules/vfs_acl_tdb.c
5 * Copyright (C) Volker Lendecke, 2008
6 * Copyright (C) Jeremy Allison, 2009
7 * Copyright (C) Ralph Böhme, 2016
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "vfs_acl_common.h"
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "librpc/gen_ndr/ndr_xattr.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "../lib/util/bitmap.h"
31 #include "passdb/lookup_sid.h"
33 #include <gnutls/gnutls.h>
34 #include <gnutls/crypto.h>
36 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
39 uint8_t hash[XATTR_SD_HASH_SIZE]);
41 #define HASH_SECURITY_INFO (SECINFO_OWNER | \
46 bool init_acl_common_config(vfs_handle_struct *handle,
47 const char *module_name)
49 struct acl_common_config *config = NULL;
50 const struct enum_list *default_acl_style_list = NULL;
52 default_acl_style_list = get_default_acl_style_list();
54 config = talloc_zero(handle->conn, struct acl_common_config);
56 DBG_ERR("talloc_zero() failed\n");
61 config->ignore_system_acls = lp_parm_bool(SNUM(handle->conn),
65 config->default_acl_style = lp_parm_enum(SNUM(handle->conn),
68 default_acl_style_list,
71 SMB_VFS_HANDLE_SET_DATA(handle, config, NULL,
72 struct acl_common_config,
79 /*******************************************************************
80 Hash a security descriptor.
81 *******************************************************************/
83 static NTSTATUS hash_blob_sha256(DATA_BLOB blob,
88 ZERO_ARRAY_LEN(hash, XATTR_SD_HASH_SIZE);
90 rc = gnutls_hash_fast(GNUTLS_DIG_SHA256,
95 return NT_STATUS_INTERNAL_ERROR;
101 /*******************************************************************
102 Hash a security descriptor.
103 *******************************************************************/
105 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
111 memset(hash, '\0', XATTR_SD_HASH_SIZE);
112 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
113 if (!NT_STATUS_IS_OK(status)) {
116 return hash_blob_sha256(blob, hash);
119 /*******************************************************************
120 Parse out a struct security_descriptor from a DATA_BLOB.
121 *******************************************************************/
123 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
125 struct security_descriptor **ppdesc,
126 uint16_t *p_hash_type,
128 uint8_t hash[XATTR_SD_HASH_SIZE],
129 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
131 struct xattr_NTACL xacl;
132 enum ndr_err_code ndr_err;
134 TALLOC_CTX *frame = talloc_stackframe();
136 ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
137 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
139 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
140 DBG_INFO("ndr_pull_xattr_NTACL failed: %s\n",
141 ndr_errstr(ndr_err));
143 return ndr_map_error2ntstatus(ndr_err);
146 *p_version = xacl.version;
148 switch (xacl.version) {
150 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
151 xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
152 xacl.info.sd->owner_sid,
153 xacl.info.sd->group_sid,
157 /* No hash - null out. */
158 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
159 memset(hash, '\0', XATTR_SD_HASH_SIZE);
162 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
163 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
164 xacl.info.sd_hs2->sd->owner_sid,
165 xacl.info.sd_hs2->sd->group_sid,
166 xacl.info.sd_hs2->sd->sacl,
167 xacl.info.sd_hs2->sd->dacl,
169 /* No hash - null out. */
170 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
171 memset(hash, '\0', XATTR_SD_HASH_SIZE);
174 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
175 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
176 xacl.info.sd_hs3->sd->owner_sid,
177 xacl.info.sd_hs3->sd->group_sid,
178 xacl.info.sd_hs3->sd->sacl,
179 xacl.info.sd_hs3->sd->dacl,
181 *p_hash_type = xacl.info.sd_hs3->hash_type;
182 /* Current version 3 (if no sys acl hash available). */
183 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
186 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
187 xacl.info.sd_hs4->sd->type | SEC_DESC_SELF_RELATIVE,
188 xacl.info.sd_hs4->sd->owner_sid,
189 xacl.info.sd_hs4->sd->group_sid,
190 xacl.info.sd_hs4->sd->sacl,
191 xacl.info.sd_hs4->sd->dacl,
193 *p_hash_type = xacl.info.sd_hs4->hash_type;
194 /* Current version 4. */
195 memcpy(hash, xacl.info.sd_hs4->hash, XATTR_SD_HASH_SIZE);
196 memcpy(sys_acl_hash, xacl.info.sd_hs4->sys_acl_hash, XATTR_SD_HASH_SIZE);
200 return NT_STATUS_REVISION_MISMATCH;
205 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
208 /*******************************************************************
209 Create a DATA_BLOB from a hash of the security descriptor storead at
210 the system layer and the NT ACL we wish to preserve
211 *******************************************************************/
213 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
216 uint8_t hash[XATTR_SD_HASH_SIZE])
218 struct xattr_NTACL xacl;
219 struct security_descriptor_hash_v3 sd_hs3;
220 enum ndr_err_code ndr_err;
221 TALLOC_CTX *ctx = talloc_tos();
227 xacl.info.sd_hs3 = &sd_hs3;
228 xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
229 xacl.info.sd_hs3->hash_type = hash_type;
230 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
232 ndr_err = ndr_push_struct_blob(
234 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
236 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
237 DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
238 ndr_errstr(ndr_err));
239 return ndr_map_error2ntstatus(ndr_err);
245 /*******************************************************************
246 Create a DATA_BLOB from a hash of the security descriptors
247 (system and NT) stored at the system layer and the NT ACL we wish
249 *******************************************************************/
251 static NTSTATUS create_sys_acl_blob(const struct security_descriptor *psd,
254 uint8_t hash[XATTR_SD_HASH_SIZE],
255 const char *description,
256 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
258 struct xattr_NTACL xacl;
259 struct security_descriptor_hash_v4 sd_hs4;
260 enum ndr_err_code ndr_err;
261 TALLOC_CTX *ctx = talloc_tos();
263 struct timeval now = timeval_current();
264 nttime_now = timeval_to_nttime(&now);
270 xacl.info.sd_hs4 = &sd_hs4;
271 xacl.info.sd_hs4->sd = discard_const_p(struct security_descriptor, psd);
272 xacl.info.sd_hs4->hash_type = hash_type;
273 memcpy(&xacl.info.sd_hs4->hash[0], hash, XATTR_SD_HASH_SIZE);
274 xacl.info.sd_hs4->description = description;
275 xacl.info.sd_hs4->time = nttime_now;
276 memcpy(&xacl.info.sd_hs4->sys_acl_hash[0], sys_acl_hash, XATTR_SD_HASH_SIZE);
278 ndr_err = ndr_push_struct_blob(
280 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
282 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
283 DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
284 ndr_errstr(ndr_err));
285 return ndr_map_error2ntstatus(ndr_err);
291 /*******************************************************************
292 Add in 3 inheritable components for a non-inheritable directory ACL.
293 CREATOR_OWNER/CREATOR_GROUP/WORLD.
294 *******************************************************************/
296 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
298 SMB_STRUCT_STAT *psbuf,
299 struct security_descriptor *psd)
301 struct connection_struct *conn = handle->conn;
302 int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
303 struct smb_filename smb_fname;
304 enum security_ace_type acltype;
305 uint32_t access_mask;
309 struct security_ace *new_ace_list;
312 new_ace_list = talloc_zero_array(psd->dacl,
317 * make_sec_acl() at the bottom of this function
318 * dupliates new_ace_list
320 new_ace_list = talloc_zero_array(talloc_tos(),
325 if (new_ace_list == NULL) {
326 return NT_STATUS_NO_MEMORY;
329 /* Fake a quick smb_filename. */
330 ZERO_STRUCT(smb_fname);
331 smb_fname.st = *psbuf;
332 smb_fname.base_name = discard_const_p(char, name);
334 dir_mode = unix_mode(conn,
335 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
336 file_mode = unix_mode(conn,
337 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
339 mode = dir_mode | file_mode;
341 DBG_DEBUG("directory %s, mode = 0%o\n", name, (unsigned int)mode);
344 memcpy(new_ace_list, psd->dacl->aces,
345 num_aces * sizeof(struct security_ace));
347 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
350 init_sec_ace(&new_ace_list[num_aces],
351 &global_sid_Creator_Owner,
354 SEC_ACE_FLAG_CONTAINER_INHERIT|
355 SEC_ACE_FLAG_OBJECT_INHERIT|
356 SEC_ACE_FLAG_INHERIT_ONLY);
357 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
358 (mode << 3) & 0700, false);
359 init_sec_ace(&new_ace_list[num_aces+1],
360 &global_sid_Creator_Group,
363 SEC_ACE_FLAG_CONTAINER_INHERIT|
364 SEC_ACE_FLAG_OBJECT_INHERIT|
365 SEC_ACE_FLAG_INHERIT_ONLY);
366 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
367 (mode << 6) & 0700, false);
368 init_sec_ace(&new_ace_list[num_aces+2],
372 SEC_ACE_FLAG_CONTAINER_INHERIT|
373 SEC_ACE_FLAG_OBJECT_INHERIT|
374 SEC_ACE_FLAG_INHERIT_ONLY);
376 psd->dacl->aces = new_ace_list;
377 psd->dacl->num_aces += 3;
378 psd->dacl->size += new_ace_list[num_aces].size +
379 new_ace_list[num_aces+1].size +
380 new_ace_list[num_aces+2].size;
382 psd->dacl = make_sec_acl(psd,
386 if (psd->dacl == NULL) {
387 return NT_STATUS_NO_MEMORY;
394 * Validate an ACL blob
396 * This validates an ACL blob against the underlying filesystem ACL. If this
397 * function returns NT_STATUS_OK ppsd can be
399 * 1. the ACL from the blob (psd_from_fs=false), or
400 * 2. the ACL from the fs (psd_from_fs=true), or
403 * If the return value is anything else then NT_STATUS_OK, ppsd is set to NULL
404 * and psd_from_fs set to false.
406 * Returning the underlying filesystem ACL in case no. 2 is really just an
407 * optimisation, because some validations have to fetch the filesytem ACL as
408 * part of the validation, so we already have it available and callers might
411 static NTSTATUS validate_nt_acl_blob(TALLOC_CTX *mem_ctx,
412 vfs_handle_struct *handle,
414 const struct smb_filename *smb_fname,
415 const DATA_BLOB *blob,
416 struct security_descriptor **ppsd,
417 bool *psd_is_from_fs)
420 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
421 uint16_t xattr_version = 0;
422 uint8_t hash[XATTR_SD_HASH_SIZE];
423 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
424 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
425 uint8_t sys_acl_hash_tmp[XATTR_SD_HASH_SIZE];
426 struct security_descriptor *psd = NULL;
427 struct security_descriptor *psd_blob = NULL;
428 struct security_descriptor *psd_fs = NULL;
429 char *sys_acl_blob_description = NULL;
430 DATA_BLOB sys_acl_blob = { 0 };
431 struct acl_common_config *config = NULL;
434 *psd_is_from_fs = false;
436 SMB_VFS_HANDLE_GET_DATA(handle, config,
437 struct acl_common_config,
438 return NT_STATUS_UNSUCCESSFUL);
440 status = parse_acl_blob(blob,
447 if (!NT_STATUS_IS_OK(status)) {
448 DBG_DEBUG("parse_acl_blob returned %s\n", nt_errstr(status));
452 /* determine which type of xattr we got */
453 switch (xattr_version) {
456 /* These xattr types are unilatteral, they do not
457 * require confirmation of the hash. In particular,
458 * the NTVFS file server uses version 1, but
459 * 'samba-tool ntacl' can set these as well */
464 if (config->ignore_system_acls) {
471 DBG_DEBUG("ACL blob revision mismatch (%u) for file %s\n",
472 (unsigned int)hash_type, smb_fname->base_name);
473 TALLOC_FREE(psd_blob);
477 /* determine which type of xattr we got */
478 if (hash_type != XATTR_SD_HASH_TYPE_SHA256) {
479 DBG_DEBUG("ACL blob hash type (%u) unexpected for file %s\n",
480 (unsigned int)hash_type, smb_fname->base_name);
481 TALLOC_FREE(psd_blob);
485 /* determine which type of xattr we got */
486 switch (xattr_version) {
491 /* Get the full underlying sd, then hash. */
492 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
495 &sys_acl_blob_description,
498 /* Get the full underlying sd, then hash. */
499 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle,
502 &sys_acl_blob_description,
506 /* If we fail to get the ACL blob (for some reason) then this
507 * is not fatal, we just work based on the NT ACL only */
509 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash_tmp);
510 if (!NT_STATUS_IS_OK(status)) {
514 TALLOC_FREE(sys_acl_blob_description);
515 TALLOC_FREE(sys_acl_blob.data);
517 if (memcmp(&sys_acl_hash[0], &sys_acl_hash_tmp[0],
518 XATTR_SD_HASH_SIZE) == 0) {
519 /* Hash matches, return blob sd. */
520 DBG_DEBUG("blob hash matches for file %s\n",
521 smb_fname->base_name);
527 /* Otherwise, fall though and see if the NT ACL hash matches */
531 /* Get the full underlying sd for the hash
532 or to return as backup. */
534 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
540 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
547 if (!NT_STATUS_IS_OK(status)) {
548 DBG_DEBUG("get_next_acl for file %s returned %s\n",
549 smb_fname->base_name, nt_errstr(status));
553 status = hash_sd_sha256(psd_fs, hash_tmp);
554 if (!NT_STATUS_IS_OK(status)) {
555 TALLOC_FREE(psd_blob);
557 *psd_is_from_fs = true;
561 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
562 /* Hash matches, return blob sd. */
563 DBG_DEBUG("blob hash matches for file %s\n",
564 smb_fname->base_name);
569 /* Hash doesn't match, return underlying sd. */
570 DBG_DEBUG("blob hash does not match for file %s - returning "
571 "file system SD mapping.\n",
572 smb_fname->base_name);
574 if (DEBUGLEVEL >= 10) {
575 DBG_DEBUG("acl for blob hash for %s is:\n",
576 smb_fname->base_name);
577 NDR_PRINT_DEBUG(security_descriptor, psd_fs);
580 TALLOC_FREE(psd_blob);
582 *psd_is_from_fs = true;
589 TALLOC_FREE(psd_blob);
591 TALLOC_FREE(sys_acl_blob_description);
592 TALLOC_FREE(sys_acl_blob.data);
596 /*******************************************************************
597 Pull a DATA_BLOB from an xattr given an fsp.
598 If the hash doesn't match, or doesn't exist - return the underlying
600 *******************************************************************/
602 NTSTATUS fget_nt_acl_common(
603 NTSTATUS (*fget_acl_blob_fn)(TALLOC_CTX *ctx,
604 vfs_handle_struct *handle,
607 vfs_handle_struct *handle,
609 uint32_t security_info,
611 struct security_descriptor **ppdesc)
613 DATA_BLOB blob = data_blob_null;
615 struct security_descriptor *psd = NULL;
616 const struct smb_filename *smb_fname = fsp->fsp_name;
617 bool psd_is_from_fs = false;
618 struct acl_common_config *config = NULL;
620 SMB_VFS_HANDLE_GET_DATA(handle, config,
621 struct acl_common_config,
622 return NT_STATUS_UNSUCCESSFUL);
624 DBG_DEBUG("name=%s\n", smb_fname->base_name);
626 status = fget_acl_blob_fn(mem_ctx, handle, fsp, &blob);
627 if (NT_STATUS_IS_OK(status)) {
628 status = validate_nt_acl_blob(mem_ctx,
635 TALLOC_FREE(blob.data);
636 if (!NT_STATUS_IS_OK(status)) {
637 DBG_DEBUG("ACL validation for [%s] failed\n",
638 smb_fname->base_name);
644 /* Get the full underlying sd, as we failed to get the
645 * blob for the hash, or the revision/hash type wasn't
648 if (config->ignore_system_acls) {
649 status = vfs_stat_fsp(fsp);
650 if (!NT_STATUS_IS_OK(status)) {
654 status = make_default_filesystem_acl(
656 config->default_acl_style,
657 smb_fname->base_name,
660 if (!NT_STATUS_IS_OK(status)) {
664 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
670 if (!NT_STATUS_IS_OK(status)) {
671 DBG_DEBUG("get_next_acl for file %s "
673 smb_fname->base_name,
678 psd_is_from_fs = true;
682 if (psd_is_from_fs) {
683 status = vfs_stat_fsp(fsp);
684 if (!NT_STATUS_IS_OK(status)) {
689 * We're returning the underlying ACL from the
690 * filesystem. If it's a directory, and has no
691 * inheritable ACE entries we have to fake them.
694 if (fsp->fsp_flags.is_directory &&
695 !sd_has_inheritable_components(psd, true)) {
696 status = add_directory_inheritable_components(
698 smb_fname->base_name,
701 if (!NT_STATUS_IS_OK(status)) {
707 * The underlying POSIX module always sets the
708 * ~SEC_DESC_DACL_PROTECTED bit, as ACLs can't be inherited in
709 * this way under POSIX. Remove it for Windows-style ACLs.
711 psd->type &= ~SEC_DESC_DACL_PROTECTED;
714 if (!(security_info & SECINFO_OWNER)) {
715 psd->owner_sid = NULL;
717 if (!(security_info & SECINFO_GROUP)) {
718 psd->group_sid = NULL;
720 if (!(security_info & SECINFO_DACL)) {
721 psd->type &= ~SEC_DESC_DACL_PRESENT;
724 if (!(security_info & SECINFO_SACL)) {
725 psd->type &= ~SEC_DESC_SACL_PRESENT;
729 if (DEBUGLEVEL >= 10) {
730 DBG_DEBUG("returning acl for %s is:\n",
731 smb_fname->base_name);
732 NDR_PRINT_DEBUG(security_descriptor, psd);
744 /*******************************************************************
745 Pull a DATA_BLOB from an xattr given a pathname.
746 If the hash doesn't match, or doesn't exist - return the underlying
748 *******************************************************************/
750 NTSTATUS get_nt_acl_common(
751 NTSTATUS (*get_acl_blob_fn)(TALLOC_CTX *ctx,
752 vfs_handle_struct *handle,
753 const struct smb_filename *smb_fname,
755 vfs_handle_struct *handle,
756 const struct smb_filename *smb_fname_in,
757 uint32_t security_info,
759 struct security_descriptor **ppdesc)
761 DATA_BLOB blob = data_blob_null;
763 struct security_descriptor *psd = NULL;
764 bool psd_is_from_fs = false;
765 struct acl_common_config *config = NULL;
767 SMB_VFS_HANDLE_GET_DATA(handle, config,
768 struct acl_common_config,
769 return NT_STATUS_UNSUCCESSFUL);
771 DBG_DEBUG("name=%s\n", smb_fname_in->base_name);
773 status = get_acl_blob_fn(mem_ctx, handle, smb_fname_in, &blob);
774 if (NT_STATUS_IS_OK(status)) {
775 status = validate_nt_acl_blob(mem_ctx,
782 TALLOC_FREE(blob.data);
783 if (!NT_STATUS_IS_OK(status)) {
784 DBG_DEBUG("ACL validation for [%s] failed\n",
785 smb_fname_in->base_name);
791 /* Get the full underlying sd, as we failed to get the
792 * blob for the hash, or the revision/hash type wasn't
795 if (config->ignore_system_acls) {
796 SMB_STRUCT_STAT sbuf;
799 ret = vfs_stat_smb_basename(handle->conn,
803 status = map_nt_error_from_unix(errno);
807 status = make_default_filesystem_acl(
809 config->default_acl_style,
810 smb_fname_in->base_name,
813 if (!NT_STATUS_IS_OK(status)) {
817 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
823 if (!NT_STATUS_IS_OK(status)) {
824 DBG_DEBUG("get_next_acl for file %s "
826 smb_fname_in->base_name,
831 psd_is_from_fs = true;
835 if (psd_is_from_fs) {
836 SMB_STRUCT_STAT sbuf;
837 bool is_directory = false;
841 * We're returning the underlying ACL from the
842 * filesystem. If it's a directory, and has no
843 * inheritable ACE entries we have to fake them.
846 ret = vfs_stat_smb_basename(handle->conn,
850 status = map_nt_error_from_unix(errno);
854 is_directory = S_ISDIR(sbuf.st_ex_mode);
856 if (is_directory && !sd_has_inheritable_components(psd, true)) {
857 status = add_directory_inheritable_components(
859 smb_fname_in->base_name,
862 if (!NT_STATUS_IS_OK(status)) {
868 * The underlying POSIX module always sets the
869 * ~SEC_DESC_DACL_PROTECTED bit, as ACLs can't be inherited in
870 * this way under POSIX. Remove it for Windows-style ACLs.
872 psd->type &= ~SEC_DESC_DACL_PROTECTED;
875 if (!(security_info & SECINFO_OWNER)) {
876 psd->owner_sid = NULL;
878 if (!(security_info & SECINFO_GROUP)) {
879 psd->group_sid = NULL;
881 if (!(security_info & SECINFO_DACL)) {
882 psd->type &= ~SEC_DESC_DACL_PRESENT;
885 if (!(security_info & SECINFO_SACL)) {
886 psd->type &= ~SEC_DESC_SACL_PRESENT;
890 if (DEBUGLEVEL >= 10) {
891 DBG_DEBUG("returning acl for %s is:\n",
892 smb_fname_in->base_name);
893 NDR_PRINT_DEBUG(security_descriptor, psd);
905 /*********************************************************************
906 Set the underlying ACL (e.g. POSIX ACLS, POSIX owner, etc)
907 *********************************************************************/
908 static NTSTATUS set_underlying_acl(vfs_handle_struct *handle, files_struct *fsp,
909 struct security_descriptor *psd,
910 uint32_t security_info_sent,
914 const struct security_token *token = NULL;
915 struct dom_sid_buf buf;
917 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
918 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
922 /* We got access denied here. If we're already root,
923 or we didn't need to do a chown, or the fsp isn't
924 open with WRITE_OWNER access, just return. */
925 if (get_current_uid(handle->conn) == 0 || chown_needed == false ||
926 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
927 return NT_STATUS_ACCESS_DENIED;
931 * Only allow take-ownership, not give-ownership. That's the way Windows
932 * implements SEC_STD_WRITE_OWNER. MS-FSA 2.1.5.16 just states: If
933 * InputBuffer.OwnerSid is not a valid owner SID for a file in the
934 * objectstore, as determined in an implementation specific manner, the
935 * object store MUST return STATUS_INVALID_OWNER.
937 token = get_current_nttok(fsp->conn);
938 if (!security_token_is_sid(token, psd->owner_sid)) {
939 return NT_STATUS_INVALID_OWNER;
942 DBG_DEBUG("overriding chown on file %s for sid %s\n",
944 dom_sid_str_buf(psd->owner_sid, &buf));
946 /* Ok, we failed to chown and we have
947 SEC_STD_WRITE_OWNER access - override. */
949 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
955 /*********************************************************************
956 Store a v3 security descriptor
957 *********************************************************************/
958 static NTSTATUS store_v3_blob(
959 NTSTATUS (*store_acl_blob_fsp_fn)(vfs_handle_struct *handle,
962 vfs_handle_struct *handle, files_struct *fsp,
963 struct security_descriptor *psd,
964 struct security_descriptor *pdesc_next,
965 uint8_t hash[XATTR_SD_HASH_SIZE])
970 if (DEBUGLEVEL >= 10) {
971 DBG_DEBUG("storing xattr sd for file %s\n",
975 discard_const_p(struct security_descriptor, psd));
977 if (pdesc_next != NULL) {
978 DBG_DEBUG("storing xattr sd based on \n");
981 discard_const_p(struct security_descriptor,
984 DBG_DEBUG("ignoring underlying sd\n");
987 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
988 if (!NT_STATUS_IS_OK(status)) {
989 DBG_DEBUG("create_acl_blob failed\n");
993 status = store_acl_blob_fsp_fn(handle, fsp, &blob);
997 /*********************************************************************
998 Store a security descriptor given an fsp.
999 *********************************************************************/
1001 NTSTATUS fset_nt_acl_common(
1002 NTSTATUS (*fget_acl_blob_fn)(TALLOC_CTX *ctx,
1003 vfs_handle_struct *handle,
1006 NTSTATUS (*store_acl_blob_fsp_fn)(vfs_handle_struct *handle,
1009 const char *module_name,
1010 vfs_handle_struct *handle, files_struct *fsp,
1011 uint32_t security_info_sent,
1012 const struct security_descriptor *orig_psd)
1016 DATA_BLOB blob, sys_acl_blob;
1017 struct security_descriptor *pdesc_next = NULL;
1018 struct security_descriptor *psd = NULL;
1019 uint8_t hash[XATTR_SD_HASH_SIZE];
1020 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
1021 bool chown_needed = false;
1022 char *sys_acl_description;
1023 TALLOC_CTX *frame = talloc_stackframe();
1024 bool ignore_file_system_acl = lp_parm_bool(
1025 SNUM(handle->conn), module_name, "ignore system acls", false);
1027 if (DEBUGLEVEL >= 10) {
1028 DBG_DEBUG("incoming sd for file %s\n", fsp_str_dbg(fsp));
1029 NDR_PRINT_DEBUG(security_descriptor,
1030 discard_const_p(struct security_descriptor, orig_psd));
1033 status = fget_nt_acl_common(fget_acl_blob_fn, handle, fsp,
1034 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
1038 if (!NT_STATUS_IS_OK(status)) {
1043 psd->revision = orig_psd->revision;
1044 if (security_info_sent & SECINFO_DACL) {
1045 psd->type = orig_psd->type;
1046 /* All our SD's are self relative. */
1047 psd->type |= SEC_DESC_SELF_RELATIVE;
1050 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
1051 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
1052 /* We're changing the owner. */
1053 chown_needed = true;
1055 psd->owner_sid = orig_psd->owner_sid;
1057 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
1058 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
1059 /* We're changing the group. */
1060 chown_needed = true;
1062 psd->group_sid = orig_psd->group_sid;
1064 if (security_info_sent & SECINFO_DACL) {
1065 if (security_descriptor_with_ms_nfs(orig_psd)) {
1067 * If the sd contains a MS NFS SID, do
1068 * nothing, it's a chmod() request from OS X
1069 * with AAPL context.
1072 return NT_STATUS_OK;
1074 psd->dacl = orig_psd->dacl;
1075 psd->type |= SEC_DESC_DACL_PRESENT;
1077 if (security_info_sent & SECINFO_SACL) {
1078 psd->sacl = orig_psd->sacl;
1079 psd->type |= SEC_DESC_SACL_PRESENT;
1082 if (ignore_file_system_acl) {
1084 /* send only ownership stuff to lower layer */
1085 security_info_sent &= (SECINFO_OWNER | SECINFO_GROUP);
1086 status = set_underlying_acl(handle, fsp, psd,
1087 security_info_sent, true);
1088 if (!NT_STATUS_IS_OK(status)) {
1094 status = store_v3_blob(store_acl_blob_fsp_fn, handle, fsp, psd,
1101 status = set_underlying_acl(handle, fsp, psd, security_info_sent,
1103 if (!NT_STATUS_IS_OK(status)) {
1108 /* Get the full underlying sd, then hash. */
1109 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
1115 if (!NT_STATUS_IS_OK(status)) {
1120 status = hash_sd_sha256(pdesc_next, hash);
1121 if (!NT_STATUS_IS_OK(status)) {
1126 /* Get the full underlying sd, then hash. */
1127 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
1130 &sys_acl_description,
1133 /* If we fail to get the ACL blob (for some reason) then this
1134 * is not fatal, we just work based on the NT ACL only */
1136 status = store_v3_blob(store_acl_blob_fsp_fn, handle, fsp, psd,
1143 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash);
1144 if (!NT_STATUS_IS_OK(status)) {
1149 if (DEBUGLEVEL >= 10) {
1150 DBG_DEBUG("storing xattr sd for file %s based on system ACL\n",
1152 NDR_PRINT_DEBUG(security_descriptor,
1153 discard_const_p(struct security_descriptor, psd));
1155 DBG_DEBUG("storing hash in xattr sd based on system ACL and:\n");
1156 NDR_PRINT_DEBUG(security_descriptor,
1157 discard_const_p(struct security_descriptor, pdesc_next));
1160 /* We store hashes of both the sys ACL blob and the NT
1161 * security desciptor mapped from that ACL so as to improve
1162 * our chances against some inadvertant change breaking the
1164 status = create_sys_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash,
1165 sys_acl_description, sys_acl_hash);
1166 if (!NT_STATUS_IS_OK(status)) {
1167 DBG_DEBUG("create_sys_acl_blob failed\n");
1172 status = store_acl_blob_fsp_fn(handle, fsp, &blob);
1178 static int acl_common_remove_object(vfs_handle_struct *handle,
1179 const struct smb_filename *smb_fname,
1182 connection_struct *conn = handle->conn;
1184 files_struct *fsp = NULL;
1186 struct smb_filename *local_fname = NULL;
1187 struct smb_filename *parent_dir_fname = NULL;
1188 int saved_errno = 0;
1189 struct smb_filename *saved_dir_fname = NULL;
1192 saved_dir_fname = vfs_GetWd(talloc_tos(),conn);
1193 if (saved_dir_fname == NULL) {
1194 saved_errno = errno;
1198 ok = parent_smb_fname(talloc_tos(),
1203 saved_errno = ENOMEM;
1207 DBG_DEBUG("removing %s %s/%s\n", is_directory ? "directory" : "file",
1208 smb_fname_str_dbg(parent_dir_fname),
1209 smb_fname_str_dbg(local_fname));
1211 /* cd into the parent dir to pin it. */
1212 ret = vfs_ChDir(conn, parent_dir_fname);
1214 saved_errno = errno;
1218 /* Must use lstat here. */
1219 ret = SMB_VFS_LSTAT(conn, local_fname);
1221 saved_errno = errno;
1225 /* Ensure we have this file open with DELETE access. */
1226 id = vfs_file_id_from_sbuf(conn, &local_fname->st);
1227 for (fsp = file_find_di_first(conn->sconn, id); fsp;
1228 fsp = file_find_di_next(fsp)) {
1229 if (fsp->access_mask & DELETE_ACCESS &&
1230 fsp->fsp_flags.delete_on_close)
1232 /* We did open this for delete,
1233 * allow the delete as root.
1240 DBG_DEBUG("%s %s/%s not an open file\n",
1241 is_directory ? "directory" : "file",
1242 smb_fname_str_dbg(parent_dir_fname),
1243 smb_fname_str_dbg(local_fname));
1244 saved_errno = EACCES;
1250 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1255 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1263 saved_errno = errno;
1268 TALLOC_FREE(parent_dir_fname);
1270 if (saved_dir_fname) {
1271 vfs_ChDir(conn, saved_dir_fname);
1272 TALLOC_FREE(saved_dir_fname);
1275 errno = saved_errno;
1280 int rmdir_acl_common(struct vfs_handle_struct *handle,
1281 struct files_struct *dirfsp,
1282 const struct smb_filename *smb_fname)
1286 /* Try the normal rmdir first. */
1287 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1294 if (errno == EACCES || errno == EPERM) {
1295 /* Failed due to access denied,
1296 see if we need to root override. */
1297 return acl_common_remove_object(handle,
1302 DBG_DEBUG("unlink of %s failed %s\n",
1303 smb_fname->base_name,
1308 int unlink_acl_common(struct vfs_handle_struct *handle,
1309 struct files_struct *dirfsp,
1310 const struct smb_filename *smb_fname,
1315 /* Try the normal unlink first. */
1316 ret = SMB_VFS_NEXT_UNLINKAT(handle,
1323 if (errno == EACCES || errno == EPERM) {
1324 /* Failed due to access denied,
1325 see if we need to root override. */
1327 /* Don't do anything fancy for streams. */
1328 if (smb_fname->stream_name) {
1331 return acl_common_remove_object(handle,
1336 DBG_DEBUG("unlink of %s failed %s\n",
1337 smb_fname->base_name,
1342 int chmod_acl_module_common(struct vfs_handle_struct *handle,
1343 const struct smb_filename *smb_fname,
1346 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1347 /* Only allow this on POSIX pathnames. */
1348 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1353 int fchmod_acl_module_common(struct vfs_handle_struct *handle,
1354 struct files_struct *fsp, mode_t mode)
1356 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1357 /* Only allow this on POSIX opens. */
1358 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);