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 "lib/crypto/sha256.h"
32 #include "passdb/lookup_sid.h"
34 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
37 uint8_t hash[XATTR_SD_HASH_SIZE]);
39 #define HASH_SECURITY_INFO (SECINFO_OWNER | \
44 static const struct enum_list default_acl_style[] = {
45 {DEFAULT_ACL_POSIX, "posix"},
46 {DEFAULT_ACL_WINDOWS, "windows"}
49 bool init_acl_common_config(vfs_handle_struct *handle,
50 const char *module_name)
52 struct acl_common_config *config = NULL;
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),
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 memset(hash, '\0', XATTR_SD_HASH_SIZE);
90 samba_SHA256_Init(&tctx);
91 samba_SHA256_Update(&tctx, blob.data, blob.length);
92 samba_SHA256_Final(hash, &tctx);
97 /*******************************************************************
98 Hash a security descriptor.
99 *******************************************************************/
101 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
107 memset(hash, '\0', XATTR_SD_HASH_SIZE);
108 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
109 if (!NT_STATUS_IS_OK(status)) {
112 return hash_blob_sha256(blob, hash);
115 /*******************************************************************
116 Parse out a struct security_descriptor from a DATA_BLOB.
117 *******************************************************************/
119 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
121 struct security_descriptor **ppdesc,
122 uint16_t *p_hash_type,
124 uint8_t hash[XATTR_SD_HASH_SIZE],
125 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
127 struct xattr_NTACL xacl;
128 enum ndr_err_code ndr_err;
130 TALLOC_CTX *frame = talloc_stackframe();
132 ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
133 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
135 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
136 DBG_INFO("ndr_pull_xattr_NTACL failed: %s\n",
137 ndr_errstr(ndr_err));
139 return ndr_map_error2ntstatus(ndr_err);
142 *p_version = xacl.version;
144 switch (xacl.version) {
146 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
147 xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
148 xacl.info.sd->owner_sid,
149 xacl.info.sd->group_sid,
153 /* No hash - null out. */
154 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
155 memset(hash, '\0', XATTR_SD_HASH_SIZE);
158 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
159 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
160 xacl.info.sd_hs2->sd->owner_sid,
161 xacl.info.sd_hs2->sd->group_sid,
162 xacl.info.sd_hs2->sd->sacl,
163 xacl.info.sd_hs2->sd->dacl,
165 /* No hash - null out. */
166 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
167 memset(hash, '\0', XATTR_SD_HASH_SIZE);
170 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
171 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
172 xacl.info.sd_hs3->sd->owner_sid,
173 xacl.info.sd_hs3->sd->group_sid,
174 xacl.info.sd_hs3->sd->sacl,
175 xacl.info.sd_hs3->sd->dacl,
177 *p_hash_type = xacl.info.sd_hs3->hash_type;
178 /* Current version 3 (if no sys acl hash available). */
179 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
182 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
183 xacl.info.sd_hs4->sd->type | SEC_DESC_SELF_RELATIVE,
184 xacl.info.sd_hs4->sd->owner_sid,
185 xacl.info.sd_hs4->sd->group_sid,
186 xacl.info.sd_hs4->sd->sacl,
187 xacl.info.sd_hs4->sd->dacl,
189 *p_hash_type = xacl.info.sd_hs4->hash_type;
190 /* Current version 4. */
191 memcpy(hash, xacl.info.sd_hs4->hash, XATTR_SD_HASH_SIZE);
192 memcpy(sys_acl_hash, xacl.info.sd_hs4->sys_acl_hash, XATTR_SD_HASH_SIZE);
196 return NT_STATUS_REVISION_MISMATCH;
201 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
204 /*******************************************************************
205 Create a DATA_BLOB from a hash of the security descriptor storead at
206 the system layer and the NT ACL we wish to preserve
207 *******************************************************************/
209 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
212 uint8_t hash[XATTR_SD_HASH_SIZE])
214 struct xattr_NTACL xacl;
215 struct security_descriptor_hash_v3 sd_hs3;
216 enum ndr_err_code ndr_err;
217 TALLOC_CTX *ctx = talloc_tos();
223 xacl.info.sd_hs3 = &sd_hs3;
224 xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
225 xacl.info.sd_hs3->hash_type = hash_type;
226 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
228 ndr_err = ndr_push_struct_blob(
230 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
232 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
233 DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
234 ndr_errstr(ndr_err));
235 return ndr_map_error2ntstatus(ndr_err);
241 /*******************************************************************
242 Create a DATA_BLOB from a hash of the security descriptors
243 (system and NT) stored at the system layer and the NT ACL we wish
245 *******************************************************************/
247 static NTSTATUS create_sys_acl_blob(const struct security_descriptor *psd,
250 uint8_t hash[XATTR_SD_HASH_SIZE],
251 const char *description,
252 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
254 struct xattr_NTACL xacl;
255 struct security_descriptor_hash_v4 sd_hs4;
256 enum ndr_err_code ndr_err;
257 TALLOC_CTX *ctx = talloc_tos();
259 struct timeval now = timeval_current();
260 nttime_now = timeval_to_nttime(&now);
266 xacl.info.sd_hs4 = &sd_hs4;
267 xacl.info.sd_hs4->sd = discard_const_p(struct security_descriptor, psd);
268 xacl.info.sd_hs4->hash_type = hash_type;
269 memcpy(&xacl.info.sd_hs4->hash[0], hash, XATTR_SD_HASH_SIZE);
270 xacl.info.sd_hs4->description = description;
271 xacl.info.sd_hs4->time = nttime_now;
272 memcpy(&xacl.info.sd_hs4->sys_acl_hash[0], sys_acl_hash, XATTR_SD_HASH_SIZE);
274 ndr_err = ndr_push_struct_blob(
276 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
278 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
279 DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
280 ndr_errstr(ndr_err));
281 return ndr_map_error2ntstatus(ndr_err);
287 /*******************************************************************
288 Add in 3 inheritable components for a non-inheritable directory ACL.
289 CREATOR_OWNER/CREATOR_GROUP/WORLD.
290 *******************************************************************/
292 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
294 SMB_STRUCT_STAT *psbuf,
295 struct security_descriptor *psd)
297 struct connection_struct *conn = handle->conn;
298 int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
299 struct smb_filename smb_fname;
300 enum security_ace_type acltype;
301 uint32_t access_mask;
305 struct security_ace *new_ace_list;
308 new_ace_list = talloc_zero_array(psd->dacl,
313 * make_sec_acl() at the bottom of this function
314 * dupliates new_ace_list
316 new_ace_list = talloc_zero_array(talloc_tos(),
321 if (new_ace_list == NULL) {
322 return NT_STATUS_NO_MEMORY;
325 /* Fake a quick smb_filename. */
326 ZERO_STRUCT(smb_fname);
327 smb_fname.st = *psbuf;
328 smb_fname.base_name = discard_const_p(char, name);
330 dir_mode = unix_mode(conn,
331 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
332 file_mode = unix_mode(conn,
333 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
335 mode = dir_mode | file_mode;
337 DBG_DEBUG("directory %s, mode = 0%o\n", name, (unsigned int)mode);
340 memcpy(new_ace_list, psd->dacl->aces,
341 num_aces * sizeof(struct security_ace));
343 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
346 init_sec_ace(&new_ace_list[num_aces],
347 &global_sid_Creator_Owner,
350 SEC_ACE_FLAG_CONTAINER_INHERIT|
351 SEC_ACE_FLAG_OBJECT_INHERIT|
352 SEC_ACE_FLAG_INHERIT_ONLY);
353 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
354 (mode << 3) & 0700, false);
355 init_sec_ace(&new_ace_list[num_aces+1],
356 &global_sid_Creator_Group,
359 SEC_ACE_FLAG_CONTAINER_INHERIT|
360 SEC_ACE_FLAG_OBJECT_INHERIT|
361 SEC_ACE_FLAG_INHERIT_ONLY);
362 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
363 (mode << 6) & 0700, false);
364 init_sec_ace(&new_ace_list[num_aces+2],
368 SEC_ACE_FLAG_CONTAINER_INHERIT|
369 SEC_ACE_FLAG_OBJECT_INHERIT|
370 SEC_ACE_FLAG_INHERIT_ONLY);
372 psd->dacl->aces = new_ace_list;
373 psd->dacl->num_aces += 3;
374 psd->dacl->size += new_ace_list[num_aces].size +
375 new_ace_list[num_aces+1].size +
376 new_ace_list[num_aces+2].size;
378 psd->dacl = make_sec_acl(psd,
382 if (psd->dacl == NULL) {
383 return NT_STATUS_NO_MEMORY;
389 static NTSTATUS make_default_acl_posix(TALLOC_CTX *ctx,
391 SMB_STRUCT_STAT *psbuf,
392 struct security_descriptor **ppdesc)
394 struct dom_sid owner_sid, group_sid;
396 struct security_ace aces[4];
397 uint32_t access_mask = 0;
398 mode_t mode = psbuf->st_ex_mode;
399 struct security_acl *new_dacl = NULL;
402 DBG_DEBUG("file %s mode = 0%o\n",name, (int)mode);
404 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
405 gid_to_sid(&group_sid, psbuf->st_ex_gid);
408 We provide up to 4 ACEs
415 if (mode & S_IRUSR) {
416 if (mode & S_IWUSR) {
417 access_mask |= SEC_RIGHTS_FILE_ALL;
419 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
422 if (mode & S_IWUSR) {
423 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
426 init_sec_ace(&aces[idx],
428 SEC_ACE_TYPE_ACCESS_ALLOWED,
434 if (mode & S_IRGRP) {
435 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
437 if (mode & S_IWGRP) {
438 /* note that delete is not granted - this matches posix behaviour */
439 access_mask |= SEC_RIGHTS_FILE_WRITE;
442 init_sec_ace(&aces[idx],
444 SEC_ACE_TYPE_ACCESS_ALLOWED,
451 if (mode & S_IROTH) {
452 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
454 if (mode & S_IWOTH) {
455 access_mask |= SEC_RIGHTS_FILE_WRITE;
458 init_sec_ace(&aces[idx],
460 SEC_ACE_TYPE_ACCESS_ALLOWED,
466 init_sec_ace(&aces[idx],
468 SEC_ACE_TYPE_ACCESS_ALLOWED,
473 new_dacl = make_sec_acl(ctx,
479 return NT_STATUS_NO_MEMORY;
482 *ppdesc = make_sec_desc(ctx,
483 SECURITY_DESCRIPTOR_REVISION_1,
484 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
491 return NT_STATUS_NO_MEMORY;
496 static NTSTATUS make_default_acl_windows(TALLOC_CTX *ctx,
498 SMB_STRUCT_STAT *psbuf,
499 struct security_descriptor **ppdesc)
501 struct dom_sid owner_sid, group_sid;
503 struct security_ace aces[4];
504 uint32_t access_mask = 0;
505 mode_t mode = psbuf->st_ex_mode;
506 struct security_acl *new_dacl = NULL;
509 DBG_DEBUG("file [%s] mode [0%o]\n", name, (int)mode);
511 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
512 gid_to_sid(&group_sid, psbuf->st_ex_gid);
520 if (mode & S_IRUSR) {
521 if (mode & S_IWUSR) {
522 access_mask |= SEC_RIGHTS_FILE_ALL;
524 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
527 if (mode & S_IWUSR) {
528 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
531 init_sec_ace(&aces[idx],
533 SEC_ACE_TYPE_ACCESS_ALLOWED,
538 init_sec_ace(&aces[idx],
540 SEC_ACE_TYPE_ACCESS_ALLOWED,
545 new_dacl = make_sec_acl(ctx,
551 return NT_STATUS_NO_MEMORY;
554 *ppdesc = make_sec_desc(ctx,
555 SECURITY_DESCRIPTOR_REVISION_1,
556 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
563 return NT_STATUS_NO_MEMORY;
568 static NTSTATUS make_default_filesystem_acl(
570 enum default_acl_style acl_style,
572 SMB_STRUCT_STAT *psbuf,
573 struct security_descriptor **ppdesc)
578 case DEFAULT_ACL_POSIX:
579 status = make_default_acl_posix(ctx, name, psbuf, ppdesc);
582 case DEFAULT_ACL_WINDOWS:
583 status = make_default_acl_windows(ctx, name, psbuf, ppdesc);
587 DBG_ERR("unknown acl style %d", acl_style);
588 status = NT_STATUS_INTERNAL_ERROR;
596 * Validate an ACL blob
598 * This validates an ACL blob against the underlying filesystem ACL. If this
599 * function returns NT_STATUS_OK ppsd can be
601 * 1. the ACL from the blob (psd_from_fs=false), or
602 * 2. the ACL from the fs (psd_from_fs=true), or
605 * If the return value is anything else then NT_STATUS_OK, ppsd is set to NULL
606 * and psd_from_fs set to false.
608 * Returning the underlying filesystem ACL in case no. 2 is really just an
609 * optimisation, because some validations have to fetch the filesytem ACL as
610 * part of the validation, so we already have it available and callers might
613 static NTSTATUS validate_nt_acl_blob(TALLOC_CTX *mem_ctx,
614 vfs_handle_struct *handle,
616 const struct smb_filename *smb_fname,
617 const DATA_BLOB *blob,
618 struct security_descriptor **ppsd,
619 bool *psd_is_from_fs)
622 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
623 uint16_t xattr_version = 0;
624 uint8_t hash[XATTR_SD_HASH_SIZE];
625 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
626 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
627 uint8_t sys_acl_hash_tmp[XATTR_SD_HASH_SIZE];
628 struct security_descriptor *psd = NULL;
629 struct security_descriptor *psd_blob = NULL;
630 struct security_descriptor *psd_fs = NULL;
631 char *sys_acl_blob_description = NULL;
632 DATA_BLOB sys_acl_blob = { 0 };
633 struct acl_common_config *config = NULL;
636 *psd_is_from_fs = false;
638 SMB_VFS_HANDLE_GET_DATA(handle, config,
639 struct acl_common_config,
640 return NT_STATUS_UNSUCCESSFUL);
642 status = parse_acl_blob(blob,
649 if (!NT_STATUS_IS_OK(status)) {
650 DBG_DEBUG("parse_acl_blob returned %s\n", nt_errstr(status));
654 /* determine which type of xattr we got */
655 switch (xattr_version) {
658 /* These xattr types are unilatteral, they do not
659 * require confirmation of the hash. In particular,
660 * the NTVFS file server uses version 1, but
661 * 'samba-tool ntacl' can set these as well */
666 if (config->ignore_system_acls) {
673 DBG_DEBUG("ACL blob revision mismatch (%u) for file %s\n",
674 (unsigned int)hash_type, smb_fname->base_name);
675 TALLOC_FREE(psd_blob);
679 /* determine which type of xattr we got */
680 if (hash_type != XATTR_SD_HASH_TYPE_SHA256) {
681 DBG_DEBUG("ACL blob hash type (%u) unexpected for file %s\n",
682 (unsigned int)hash_type, smb_fname->base_name);
683 TALLOC_FREE(psd_blob);
687 /* determine which type of xattr we got */
688 switch (xattr_version) {
693 /* Get the full underlying sd, then hash. */
694 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
697 &sys_acl_blob_description,
700 /* Get the full underlying sd, then hash. */
701 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle,
704 &sys_acl_blob_description,
708 /* If we fail to get the ACL blob (for some reason) then this
709 * is not fatal, we just work based on the NT ACL only */
711 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash_tmp);
712 if (!NT_STATUS_IS_OK(status)) {
716 TALLOC_FREE(sys_acl_blob_description);
717 TALLOC_FREE(sys_acl_blob.data);
719 if (memcmp(&sys_acl_hash[0], &sys_acl_hash_tmp[0],
720 XATTR_SD_HASH_SIZE) == 0) {
721 /* Hash matches, return blob sd. */
722 DBG_DEBUG("blob hash matches for file %s\n",
723 smb_fname->base_name);
729 /* Otherwise, fall though and see if the NT ACL hash matches */
732 /* Get the full underlying sd for the hash
733 or to return as backup. */
735 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
741 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
748 if (!NT_STATUS_IS_OK(status)) {
749 DBG_DEBUG("get_next_acl for file %s returned %s\n",
750 smb_fname->base_name, nt_errstr(status));
754 status = hash_sd_sha256(psd_fs, hash_tmp);
755 if (!NT_STATUS_IS_OK(status)) {
756 TALLOC_FREE(psd_blob);
758 *psd_is_from_fs = true;
762 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
763 /* Hash matches, return blob sd. */
764 DBG_DEBUG("blob hash matches for file %s\n",
765 smb_fname->base_name);
770 /* Hash doesn't match, return underlying sd. */
771 DBG_DEBUG("blob hash does not match for file %s - returning "
772 "file system SD mapping.\n",
773 smb_fname->base_name);
775 if (DEBUGLEVEL >= 10) {
776 DBG_DEBUG("acl for blob hash for %s is:\n",
777 smb_fname->base_name);
778 NDR_PRINT_DEBUG(security_descriptor, psd_fs);
781 TALLOC_FREE(psd_blob);
783 *psd_is_from_fs = true;
790 TALLOC_FREE(psd_blob);
792 TALLOC_FREE(sys_acl_blob_description);
793 TALLOC_FREE(sys_acl_blob.data);
797 static NTSTATUS stat_fsp_or_smb_fname(vfs_handle_struct *handle,
799 const struct smb_filename *smb_fname,
800 SMB_STRUCT_STAT *sbuf,
801 SMB_STRUCT_STAT **psbuf)
807 status = vfs_stat_fsp(fsp);
808 if (!NT_STATUS_IS_OK(status)) {
811 *psbuf = &fsp->fsp_name->st;
814 * https://bugzilla.samba.org/show_bug.cgi?id=11249
816 * We are currently guaranteed that 'name' here is a
817 * smb_fname->base_name, which *cannot* contain a stream name
818 * (':'). vfs_stat_smb_fname() splits a name into a base name +
819 * stream name, which when we get here we know we've already
820 * done. So we have to call the stat or lstat VFS calls
821 * directly here. Else, a base_name that contains a ':' (from a
822 * demangled name) will get split again.
825 * This uglyness will go away once smb_fname is fully plumbed
828 ret = vfs_stat_smb_basename(handle->conn,
832 return map_nt_error_from_unix(errno);
839 /*******************************************************************
840 Pull a DATA_BLOB from an xattr given a pathname.
841 If the hash doesn't match, or doesn't exist - return the underlying
843 *******************************************************************/
845 NTSTATUS get_nt_acl_common(
846 NTSTATUS (*get_acl_blob_fn)(TALLOC_CTX *ctx,
847 vfs_handle_struct *handle,
849 const struct smb_filename *smb_fname,
851 vfs_handle_struct *handle,
853 const struct smb_filename *smb_fname_in,
854 uint32_t security_info,
856 struct security_descriptor **ppdesc)
858 DATA_BLOB blob = data_blob_null;
860 struct security_descriptor *psd = NULL;
861 const struct smb_filename *smb_fname = NULL;
862 bool psd_is_from_fs = false;
863 struct acl_common_config *config = NULL;
865 SMB_VFS_HANDLE_GET_DATA(handle, config,
866 struct acl_common_config,
867 return NT_STATUS_UNSUCCESSFUL);
869 if (fsp && smb_fname_in == NULL) {
870 smb_fname = fsp->fsp_name;
872 smb_fname = smb_fname_in;
875 DBG_DEBUG("name=%s\n", smb_fname->base_name);
877 status = get_acl_blob_fn(mem_ctx, handle, fsp, smb_fname, &blob);
878 if (NT_STATUS_IS_OK(status)) {
879 status = validate_nt_acl_blob(mem_ctx,
886 TALLOC_FREE(blob.data);
887 if (!NT_STATUS_IS_OK(status)) {
888 DBG_DEBUG("ACL validation for [%s] failed\n",
889 smb_fname->base_name);
895 /* Get the full underlying sd, as we failed to get the
896 * blob for the hash, or the revision/hash type wasn't
899 if (config->ignore_system_acls) {
900 SMB_STRUCT_STAT sbuf;
901 SMB_STRUCT_STAT *psbuf = &sbuf;
903 status = stat_fsp_or_smb_fname(handle, fsp, smb_fname,
905 if (!NT_STATUS_IS_OK(status)) {
909 status = make_default_filesystem_acl(
911 config->default_acl_style,
912 smb_fname->base_name,
915 if (!NT_STATUS_IS_OK(status)) {
920 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
926 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
933 if (!NT_STATUS_IS_OK(status)) {
934 DBG_DEBUG("get_next_acl for file %s "
936 smb_fname->base_name,
941 psd_is_from_fs = true;
945 if (psd_is_from_fs) {
946 SMB_STRUCT_STAT sbuf;
947 SMB_STRUCT_STAT *psbuf = &sbuf;
948 bool is_directory = false;
951 * We're returning the underlying ACL from the
952 * filesystem. If it's a directory, and has no
953 * inheritable ACE entries we have to fake them.
956 status = stat_fsp_or_smb_fname(handle, fsp, smb_fname,
958 if (!NT_STATUS_IS_OK(status)) {
962 is_directory = S_ISDIR(psbuf->st_ex_mode);
964 if (is_directory && !sd_has_inheritable_components(psd, true)) {
965 status = add_directory_inheritable_components(
967 smb_fname->base_name,
970 if (!NT_STATUS_IS_OK(status)) {
976 * The underlying POSIX module always sets the
977 * ~SEC_DESC_DACL_PROTECTED bit, as ACLs can't be inherited in
978 * this way under POSIX. Remove it for Windows-style ACLs.
980 psd->type &= ~SEC_DESC_DACL_PROTECTED;
983 if (!(security_info & SECINFO_OWNER)) {
984 psd->owner_sid = NULL;
986 if (!(security_info & SECINFO_GROUP)) {
987 psd->group_sid = NULL;
989 if (!(security_info & SECINFO_DACL)) {
990 psd->type &= ~SEC_DESC_DACL_PRESENT;
993 if (!(security_info & SECINFO_SACL)) {
994 psd->type &= ~SEC_DESC_SACL_PRESENT;
998 if (DEBUGLEVEL >= 10) {
999 DBG_DEBUG("returning acl for %s is:\n",
1000 smb_fname->base_name);
1001 NDR_PRINT_DEBUG(security_descriptor, psd);
1006 return NT_STATUS_OK;
1013 /*********************************************************************
1014 Set the underlying ACL (e.g. POSIX ACLS, POSIX owner, etc)
1015 *********************************************************************/
1016 static NTSTATUS set_underlying_acl(vfs_handle_struct *handle, files_struct *fsp,
1017 struct security_descriptor *psd,
1018 uint32_t security_info_sent,
1022 const struct security_token *token = NULL;
1024 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
1025 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1029 /* We got access denied here. If we're already root,
1030 or we didn't need to do a chown, or the fsp isn't
1031 open with WRITE_OWNER access, just return. */
1032 if (get_current_uid(handle->conn) == 0 || chown_needed == false ||
1033 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
1034 return NT_STATUS_ACCESS_DENIED;
1038 * Only allow take-ownership, not give-ownership. That's the way Windows
1039 * implements SEC_STD_WRITE_OWNER. MS-FSA 2.1.5.16 just states: If
1040 * InputBuffer.OwnerSid is not a valid owner SID for a file in the
1041 * objectstore, as determined in an implementation specific manner, the
1042 * object store MUST return STATUS_INVALID_OWNER.
1044 token = get_current_nttok(fsp->conn);
1045 if (!security_token_is_sid(token, psd->owner_sid)) {
1046 return NT_STATUS_INVALID_OWNER;
1049 DBG_DEBUG("overriding chown on file %s for sid %s\n",
1050 fsp_str_dbg(fsp), sid_string_tos(psd->owner_sid));
1052 /* Ok, we failed to chown and we have
1053 SEC_STD_WRITE_OWNER access - override. */
1055 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
1061 /*********************************************************************
1062 Store a v3 security descriptor
1063 *********************************************************************/
1064 static NTSTATUS store_v3_blob(
1065 NTSTATUS (*store_acl_blob_fsp_fn)(vfs_handle_struct *handle,
1068 vfs_handle_struct *handle, files_struct *fsp,
1069 struct security_descriptor *psd,
1070 struct security_descriptor *pdesc_next,
1071 uint8_t hash[XATTR_SD_HASH_SIZE])
1076 if (DEBUGLEVEL >= 10) {
1077 DBG_DEBUG("storing xattr sd for file %s\n",
1080 security_descriptor,
1081 discard_const_p(struct security_descriptor, psd));
1083 if (pdesc_next != NULL) {
1084 DBG_DEBUG("storing xattr sd based on \n");
1086 security_descriptor,
1087 discard_const_p(struct security_descriptor,
1090 DBG_DEBUG("ignoring underlying sd\n");
1093 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
1094 if (!NT_STATUS_IS_OK(status)) {
1095 DBG_DEBUG("create_acl_blob failed\n");
1099 status = store_acl_blob_fsp_fn(handle, fsp, &blob);
1103 /*********************************************************************
1104 Store a security descriptor given an fsp.
1105 *********************************************************************/
1107 NTSTATUS fset_nt_acl_common(
1108 NTSTATUS (*get_acl_blob_fn)(TALLOC_CTX *ctx,
1109 vfs_handle_struct *handle,
1111 const struct smb_filename *smb_fname,
1113 NTSTATUS (*store_acl_blob_fsp_fn)(vfs_handle_struct *handle,
1116 const char *module_name,
1117 vfs_handle_struct *handle, files_struct *fsp,
1118 uint32_t security_info_sent,
1119 const struct security_descriptor *orig_psd)
1123 DATA_BLOB blob, sys_acl_blob;
1124 struct security_descriptor *pdesc_next = NULL;
1125 struct security_descriptor *psd = NULL;
1126 uint8_t hash[XATTR_SD_HASH_SIZE];
1127 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
1128 bool chown_needed = false;
1129 char *sys_acl_description;
1130 TALLOC_CTX *frame = talloc_stackframe();
1131 bool ignore_file_system_acl = lp_parm_bool(
1132 SNUM(handle->conn), module_name, "ignore system acls", false);
1134 if (DEBUGLEVEL >= 10) {
1135 DBG_DEBUG("incoming sd for file %s\n", fsp_str_dbg(fsp));
1136 NDR_PRINT_DEBUG(security_descriptor,
1137 discard_const_p(struct security_descriptor, orig_psd));
1140 status = get_nt_acl_common(get_acl_blob_fn, handle, fsp,
1142 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
1146 if (!NT_STATUS_IS_OK(status)) {
1151 psd->revision = orig_psd->revision;
1152 /* All our SD's are self relative. */
1153 psd->type = orig_psd->type | SEC_DESC_SELF_RELATIVE;
1155 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
1156 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
1157 /* We're changing the owner. */
1158 chown_needed = true;
1160 psd->owner_sid = orig_psd->owner_sid;
1162 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
1163 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
1164 /* We're changing the group. */
1165 chown_needed = true;
1167 psd->group_sid = orig_psd->group_sid;
1169 if (security_info_sent & SECINFO_DACL) {
1170 if (security_descriptor_with_ms_nfs(orig_psd)) {
1172 * If the sd contains a MS NFS SID, do
1173 * nothing, it's a chmod() request from OS X
1174 * with AAPL context.
1177 return NT_STATUS_OK;
1179 psd->dacl = orig_psd->dacl;
1180 psd->type |= SEC_DESC_DACL_PRESENT;
1182 if (security_info_sent & SECINFO_SACL) {
1183 psd->sacl = orig_psd->sacl;
1184 psd->type |= SEC_DESC_SACL_PRESENT;
1187 if (ignore_file_system_acl) {
1189 /* send only ownership stuff to lower layer */
1190 security_info_sent &= (SECINFO_OWNER | SECINFO_GROUP);
1191 status = set_underlying_acl(handle, fsp, psd,
1192 security_info_sent, true);
1193 if (!NT_STATUS_IS_OK(status)) {
1199 status = store_v3_blob(store_acl_blob_fsp_fn, handle, fsp, psd,
1206 status = set_underlying_acl(handle, fsp, psd, security_info_sent,
1208 if (!NT_STATUS_IS_OK(status)) {
1213 /* Get the full underlying sd, then hash. */
1214 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
1220 if (!NT_STATUS_IS_OK(status)) {
1225 status = hash_sd_sha256(pdesc_next, hash);
1226 if (!NT_STATUS_IS_OK(status)) {
1231 /* Get the full underlying sd, then hash. */
1232 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
1235 &sys_acl_description,
1238 /* If we fail to get the ACL blob (for some reason) then this
1239 * is not fatal, we just work based on the NT ACL only */
1241 status = store_v3_blob(store_acl_blob_fsp_fn, handle, fsp, psd,
1248 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash);
1249 if (!NT_STATUS_IS_OK(status)) {
1254 if (DEBUGLEVEL >= 10) {
1255 DBG_DEBUG("storing xattr sd for file %s based on system ACL\n",
1257 NDR_PRINT_DEBUG(security_descriptor,
1258 discard_const_p(struct security_descriptor, psd));
1260 DBG_DEBUG("storing hash in xattr sd based on system ACL and:\n");
1261 NDR_PRINT_DEBUG(security_descriptor,
1262 discard_const_p(struct security_descriptor, pdesc_next));
1265 /* We store hashes of both the sys ACL blob and the NT
1266 * security desciptor mapped from that ACL so as to improve
1267 * our chances against some inadvertant change breaking the
1269 status = create_sys_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash,
1270 sys_acl_description, sys_acl_hash);
1271 if (!NT_STATUS_IS_OK(status)) {
1272 DBG_DEBUG("create_sys_acl_blob failed\n");
1277 status = store_acl_blob_fsp_fn(handle, fsp, &blob);
1283 static int acl_common_remove_object(vfs_handle_struct *handle,
1284 const struct smb_filename *smb_fname,
1287 connection_struct *conn = handle->conn;
1289 files_struct *fsp = NULL;
1291 char *parent_dir = NULL;
1292 const char *final_component = NULL;
1293 struct smb_filename local_fname = {0};
1294 struct smb_filename parent_dir_fname = {0};
1295 int saved_errno = 0;
1296 struct smb_filename *saved_dir_fname = NULL;
1298 saved_dir_fname = vfs_GetWd(talloc_tos(),conn);
1299 if (saved_dir_fname == NULL) {
1300 saved_errno = errno;
1304 if (!parent_dirname(talloc_tos(), smb_fname->base_name,
1305 &parent_dir, &final_component)) {
1306 saved_errno = ENOMEM;
1310 DBG_DEBUG("removing %s %s/%s\n", is_directory ? "directory" : "file",
1311 parent_dir, final_component);
1313 parent_dir_fname = (struct smb_filename) { .base_name = parent_dir };
1315 /* cd into the parent dir to pin it. */
1316 ret = vfs_ChDir(conn, &parent_dir_fname);
1318 saved_errno = errno;
1322 local_fname.base_name = discard_const_p(char, final_component);
1324 /* Must use lstat here. */
1325 ret = SMB_VFS_LSTAT(conn, &local_fname);
1327 saved_errno = errno;
1331 /* Ensure we have this file open with DELETE access. */
1332 id = vfs_file_id_from_sbuf(conn, &local_fname.st);
1333 for (fsp = file_find_di_first(conn->sconn, id); fsp;
1334 fsp = file_find_di_next(fsp)) {
1335 if (fsp->access_mask & DELETE_ACCESS &&
1336 fsp->delete_on_close) {
1337 /* We did open this for delete,
1338 * allow the delete as root.
1345 DBG_DEBUG("%s %s/%s not an open file\n",
1346 is_directory ? "directory" : "file",
1347 parent_dir, final_component);
1348 saved_errno = EACCES;
1354 ret = SMB_VFS_NEXT_RMDIR(handle, &local_fname);
1356 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
1361 saved_errno = errno;
1366 TALLOC_FREE(parent_dir);
1368 if (saved_dir_fname) {
1369 vfs_ChDir(conn, saved_dir_fname);
1370 TALLOC_FREE(saved_dir_fname);
1373 errno = saved_errno;
1378 int rmdir_acl_common(struct vfs_handle_struct *handle,
1379 const struct smb_filename *smb_fname)
1383 /* Try the normal rmdir first. */
1384 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1388 if (errno == EACCES || errno == EPERM) {
1389 /* Failed due to access denied,
1390 see if we need to root override. */
1391 return acl_common_remove_object(handle,
1396 DBG_DEBUG("unlink of %s failed %s\n",
1397 smb_fname->base_name,
1402 int unlink_acl_common(struct vfs_handle_struct *handle,
1403 const struct smb_filename *smb_fname)
1407 /* Try the normal unlink first. */
1408 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1412 if (errno == EACCES || errno == EPERM) {
1413 /* Failed due to access denied,
1414 see if we need to root override. */
1416 /* Don't do anything fancy for streams. */
1417 if (smb_fname->stream_name) {
1420 return acl_common_remove_object(handle,
1425 DBG_DEBUG("unlink of %s failed %s\n",
1426 smb_fname->base_name,
1431 int chmod_acl_module_common(struct vfs_handle_struct *handle,
1432 const struct smb_filename *smb_fname,
1435 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1436 /* Only allow this on POSIX pathnames. */
1437 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1442 int fchmod_acl_module_common(struct vfs_handle_struct *handle,
1443 struct files_struct *fsp, mode_t mode)
1445 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1446 /* Only allow this on POSIX opens. */
1447 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1452 int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1453 const struct smb_filename *smb_fname,
1456 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1457 /* Only allow this on POSIX pathnames. */
1458 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
1463 int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1464 struct files_struct *fsp, mode_t mode)
1466 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1467 /* Only allow this on POSIX opens. */
1468 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);