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/>.
23 #include "smbd/smbd.h"
24 #include "system/filesys.h"
25 #include "../libcli/security/security.h"
26 #include "../librpc/gen_ndr/ndr_security.h"
27 #include "../lib/util/bitmap.h"
28 #include "passdb/lookup_sid.h"
30 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
33 uint8_t hash[XATTR_SD_HASH_SIZE]);
35 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
36 vfs_handle_struct *handle,
38 const struct smb_filename *smb_fname,
41 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
45 #define HASH_SECURITY_INFO (SECINFO_OWNER | \
50 enum default_acl_style {DEFAULT_ACL_POSIX, DEFAULT_ACL_WINDOWS};
52 static const struct enum_list default_acl_style[] = {
53 {DEFAULT_ACL_POSIX, "posix"},
54 {DEFAULT_ACL_WINDOWS, "windows"}
57 struct acl_common_config {
58 bool ignore_system_acls;
59 enum default_acl_style default_acl_style;
62 static bool init_acl_common_config(vfs_handle_struct *handle)
64 struct acl_common_config *config = NULL;
66 config = talloc_zero(handle->conn, struct acl_common_config);
68 DBG_ERR("talloc_zero() failed\n");
73 config->ignore_system_acls = lp_parm_bool(SNUM(handle->conn),
77 config->default_acl_style = lp_parm_enum(SNUM(handle->conn),
83 SMB_VFS_HANDLE_SET_DATA(handle, config, NULL,
84 struct acl_common_config,
91 /*******************************************************************
92 Hash a security descriptor.
93 *******************************************************************/
95 static NTSTATUS hash_blob_sha256(DATA_BLOB blob,
100 memset(hash, '\0', XATTR_SD_HASH_SIZE);
102 samba_SHA256_Init(&tctx);
103 samba_SHA256_Update(&tctx, blob.data, blob.length);
104 samba_SHA256_Final(hash, &tctx);
109 /*******************************************************************
110 Hash a security descriptor.
111 *******************************************************************/
113 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
119 memset(hash, '\0', XATTR_SD_HASH_SIZE);
120 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
121 if (!NT_STATUS_IS_OK(status)) {
124 return hash_blob_sha256(blob, hash);
127 /*******************************************************************
128 Parse out a struct security_descriptor from a DATA_BLOB.
129 *******************************************************************/
131 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
133 struct security_descriptor **ppdesc,
134 uint16_t *p_hash_type,
136 uint8_t hash[XATTR_SD_HASH_SIZE],
137 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
139 struct xattr_NTACL xacl;
140 enum ndr_err_code ndr_err;
142 TALLOC_CTX *frame = talloc_stackframe();
144 ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
145 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
147 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
148 DBG_INFO("ndr_pull_xattr_NTACL failed: %s\n",
149 ndr_errstr(ndr_err));
151 return ndr_map_error2ntstatus(ndr_err);
154 *p_version = xacl.version;
156 switch (xacl.version) {
158 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
159 xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
160 xacl.info.sd->owner_sid,
161 xacl.info.sd->group_sid,
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_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
172 xacl.info.sd_hs2->sd->owner_sid,
173 xacl.info.sd_hs2->sd->group_sid,
174 xacl.info.sd_hs2->sd->sacl,
175 xacl.info.sd_hs2->sd->dacl,
177 /* No hash - null out. */
178 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
179 memset(hash, '\0', XATTR_SD_HASH_SIZE);
182 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
183 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
184 xacl.info.sd_hs3->sd->owner_sid,
185 xacl.info.sd_hs3->sd->group_sid,
186 xacl.info.sd_hs3->sd->sacl,
187 xacl.info.sd_hs3->sd->dacl,
189 *p_hash_type = xacl.info.sd_hs3->hash_type;
190 /* Current version 3 (if no sys acl hash available). */
191 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
194 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
195 xacl.info.sd_hs4->sd->type | SEC_DESC_SELF_RELATIVE,
196 xacl.info.sd_hs4->sd->owner_sid,
197 xacl.info.sd_hs4->sd->group_sid,
198 xacl.info.sd_hs4->sd->sacl,
199 xacl.info.sd_hs4->sd->dacl,
201 *p_hash_type = xacl.info.sd_hs4->hash_type;
202 /* Current version 4. */
203 memcpy(hash, xacl.info.sd_hs4->hash, XATTR_SD_HASH_SIZE);
204 memcpy(sys_acl_hash, xacl.info.sd_hs4->sys_acl_hash, XATTR_SD_HASH_SIZE);
208 return NT_STATUS_REVISION_MISMATCH;
213 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
216 /*******************************************************************
217 Create a DATA_BLOB from a hash of the security descriptor storead at
218 the system layer and the NT ACL we wish to preserve
219 *******************************************************************/
221 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
224 uint8_t hash[XATTR_SD_HASH_SIZE])
226 struct xattr_NTACL xacl;
227 struct security_descriptor_hash_v3 sd_hs3;
228 enum ndr_err_code ndr_err;
229 TALLOC_CTX *ctx = talloc_tos();
235 xacl.info.sd_hs3 = &sd_hs3;
236 xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
237 xacl.info.sd_hs3->hash_type = hash_type;
238 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
240 ndr_err = ndr_push_struct_blob(
242 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
244 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
245 DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
246 ndr_errstr(ndr_err));
247 return ndr_map_error2ntstatus(ndr_err);
253 /*******************************************************************
254 Create a DATA_BLOB from a hash of the security descriptors
255 (system and NT) stored at the system layer and the NT ACL we wish
257 *******************************************************************/
259 static NTSTATUS create_sys_acl_blob(const struct security_descriptor *psd,
262 uint8_t hash[XATTR_SD_HASH_SIZE],
263 const char *description,
264 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
266 struct xattr_NTACL xacl;
267 struct security_descriptor_hash_v4 sd_hs4;
268 enum ndr_err_code ndr_err;
269 TALLOC_CTX *ctx = talloc_tos();
271 struct timeval now = timeval_current();
272 nttime_now = timeval_to_nttime(&now);
278 xacl.info.sd_hs4 = &sd_hs4;
279 xacl.info.sd_hs4->sd = discard_const_p(struct security_descriptor, psd);
280 xacl.info.sd_hs4->hash_type = hash_type;
281 memcpy(&xacl.info.sd_hs4->hash[0], hash, XATTR_SD_HASH_SIZE);
282 xacl.info.sd_hs4->description = description;
283 xacl.info.sd_hs4->time = nttime_now;
284 memcpy(&xacl.info.sd_hs4->sys_acl_hash[0], sys_acl_hash, XATTR_SD_HASH_SIZE);
286 ndr_err = ndr_push_struct_blob(
288 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
290 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
291 DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
292 ndr_errstr(ndr_err));
293 return ndr_map_error2ntstatus(ndr_err);
299 /*******************************************************************
300 Add in 3 inheritable components for a non-inheritable directory ACL.
301 CREATOR_OWNER/CREATOR_GROUP/WORLD.
302 *******************************************************************/
304 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
306 SMB_STRUCT_STAT *psbuf,
307 struct security_descriptor *psd)
309 struct connection_struct *conn = handle->conn;
310 int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
311 struct smb_filename smb_fname;
312 enum security_ace_type acltype;
313 uint32_t access_mask;
317 struct security_ace *new_ace_list;
320 new_ace_list = talloc_zero_array(psd->dacl,
325 * make_sec_acl() at the bottom of this function
326 * dupliates new_ace_list
328 new_ace_list = talloc_zero_array(talloc_tos(),
333 if (new_ace_list == NULL) {
334 return NT_STATUS_NO_MEMORY;
337 /* Fake a quick smb_filename. */
338 ZERO_STRUCT(smb_fname);
339 smb_fname.st = *psbuf;
340 smb_fname.base_name = discard_const_p(char, name);
342 dir_mode = unix_mode(conn,
343 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
344 file_mode = unix_mode(conn,
345 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
347 mode = dir_mode | file_mode;
349 DBG_DEBUG("directory %s, mode = 0%o\n", name, (unsigned int)mode);
352 memcpy(new_ace_list, psd->dacl->aces,
353 num_aces * sizeof(struct security_ace));
355 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
358 init_sec_ace(&new_ace_list[num_aces],
359 &global_sid_Creator_Owner,
362 SEC_ACE_FLAG_CONTAINER_INHERIT|
363 SEC_ACE_FLAG_OBJECT_INHERIT|
364 SEC_ACE_FLAG_INHERIT_ONLY);
365 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
366 (mode << 3) & 0700, false);
367 init_sec_ace(&new_ace_list[num_aces+1],
368 &global_sid_Creator_Group,
371 SEC_ACE_FLAG_CONTAINER_INHERIT|
372 SEC_ACE_FLAG_OBJECT_INHERIT|
373 SEC_ACE_FLAG_INHERIT_ONLY);
374 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
375 (mode << 6) & 0700, false);
376 init_sec_ace(&new_ace_list[num_aces+2],
380 SEC_ACE_FLAG_CONTAINER_INHERIT|
381 SEC_ACE_FLAG_OBJECT_INHERIT|
382 SEC_ACE_FLAG_INHERIT_ONLY);
384 psd->dacl->aces = new_ace_list;
385 psd->dacl->num_aces += 3;
386 psd->dacl->size += new_ace_list[num_aces].size +
387 new_ace_list[num_aces+1].size +
388 new_ace_list[num_aces+2].size;
390 psd->dacl = make_sec_acl(psd,
394 if (psd->dacl == NULL) {
395 return NT_STATUS_NO_MEMORY;
401 static NTSTATUS make_default_acl_posix(TALLOC_CTX *ctx,
403 SMB_STRUCT_STAT *psbuf,
404 struct security_descriptor **ppdesc)
406 struct dom_sid owner_sid, group_sid;
408 struct security_ace aces[4];
409 uint32_t access_mask = 0;
410 mode_t mode = psbuf->st_ex_mode;
411 struct security_acl *new_dacl = NULL;
414 DBG_DEBUG("file %s mode = 0%o\n",name, (int)mode);
416 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
417 gid_to_sid(&group_sid, psbuf->st_ex_gid);
420 We provide up to 4 ACEs
427 if (mode & S_IRUSR) {
428 if (mode & S_IWUSR) {
429 access_mask |= SEC_RIGHTS_FILE_ALL;
431 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
434 if (mode & S_IWUSR) {
435 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
438 init_sec_ace(&aces[idx],
440 SEC_ACE_TYPE_ACCESS_ALLOWED,
446 if (mode & S_IRGRP) {
447 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
449 if (mode & S_IWGRP) {
450 /* note that delete is not granted - this matches posix behaviour */
451 access_mask |= SEC_RIGHTS_FILE_WRITE;
454 init_sec_ace(&aces[idx],
456 SEC_ACE_TYPE_ACCESS_ALLOWED,
463 if (mode & S_IROTH) {
464 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
466 if (mode & S_IWOTH) {
467 access_mask |= SEC_RIGHTS_FILE_WRITE;
470 init_sec_ace(&aces[idx],
472 SEC_ACE_TYPE_ACCESS_ALLOWED,
478 init_sec_ace(&aces[idx],
480 SEC_ACE_TYPE_ACCESS_ALLOWED,
485 new_dacl = make_sec_acl(ctx,
491 return NT_STATUS_NO_MEMORY;
494 *ppdesc = make_sec_desc(ctx,
495 SECURITY_DESCRIPTOR_REVISION_1,
496 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
503 return NT_STATUS_NO_MEMORY;
508 static NTSTATUS make_default_acl_windows(TALLOC_CTX *ctx,
510 SMB_STRUCT_STAT *psbuf,
511 struct security_descriptor **ppdesc)
513 struct dom_sid owner_sid, group_sid;
515 struct security_ace aces[4];
516 uint32_t access_mask = 0;
517 mode_t mode = psbuf->st_ex_mode;
518 struct security_acl *new_dacl = NULL;
521 DBG_DEBUG("file [%s] mode [0%o]\n", name, (int)mode);
523 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
524 gid_to_sid(&group_sid, psbuf->st_ex_gid);
532 if (mode & S_IRUSR) {
533 if (mode & S_IWUSR) {
534 access_mask |= SEC_RIGHTS_FILE_ALL;
536 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
539 if (mode & S_IWUSR) {
540 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
543 init_sec_ace(&aces[idx],
545 SEC_ACE_TYPE_ACCESS_ALLOWED,
550 init_sec_ace(&aces[idx],
552 SEC_ACE_TYPE_ACCESS_ALLOWED,
557 new_dacl = make_sec_acl(ctx,
563 return NT_STATUS_NO_MEMORY;
566 *ppdesc = make_sec_desc(ctx,
567 SECURITY_DESCRIPTOR_REVISION_1,
568 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
575 return NT_STATUS_NO_MEMORY;
580 static NTSTATUS make_default_filesystem_acl(TALLOC_CTX *ctx,
581 struct acl_common_config *config,
583 SMB_STRUCT_STAT *psbuf,
584 struct security_descriptor **ppdesc)
588 switch (config->default_acl_style) {
590 case DEFAULT_ACL_POSIX:
591 status = make_default_acl_posix(ctx, name, psbuf, ppdesc);
594 case DEFAULT_ACL_WINDOWS:
595 status = make_default_acl_windows(ctx, name, psbuf, ppdesc);
599 DBG_ERR("unknown acl style %d", config->default_acl_style);
600 status = NT_STATUS_INTERNAL_ERROR;
608 * Validate an ACL blob
610 * This validates an ACL blob against the underlying filesystem ACL. If this
611 * function returns NT_STATUS_OK ppsd can be
613 * 1. the ACL from the blob (psd_from_fs=false), or
614 * 2. the ACL from the fs (psd_from_fs=true), or
617 * If the return value is anything else then NT_STATUS_OK, ppsd is set to NULL
618 * and psd_from_fs set to false.
620 * Returning the underlying filesystem ACL in case no. 2 is really just an
621 * optimisation, because some validations have to fetch the filesytem ACL as
622 * part of the validation, so we already have it available and callers might
625 static NTSTATUS validate_nt_acl_blob(TALLOC_CTX *mem_ctx,
626 vfs_handle_struct *handle,
628 const struct smb_filename *smb_fname,
629 const DATA_BLOB *blob,
630 struct security_descriptor **ppsd,
631 bool *psd_is_from_fs)
634 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
635 uint16_t xattr_version = 0;
636 uint8_t hash[XATTR_SD_HASH_SIZE];
637 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
638 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
639 uint8_t sys_acl_hash_tmp[XATTR_SD_HASH_SIZE];
640 struct security_descriptor *psd = NULL;
641 struct security_descriptor *psd_blob = NULL;
642 struct security_descriptor *psd_fs = NULL;
643 char *sys_acl_blob_description = NULL;
644 DATA_BLOB sys_acl_blob = { 0 };
645 struct acl_common_config *config = NULL;
648 *psd_is_from_fs = false;
650 SMB_VFS_HANDLE_GET_DATA(handle, config,
651 struct acl_common_config,
652 return NT_STATUS_UNSUCCESSFUL);
654 status = parse_acl_blob(blob,
661 if (!NT_STATUS_IS_OK(status)) {
662 DBG_DEBUG("parse_acl_blob returned %s\n", nt_errstr(status));
666 /* determine which type of xattr we got */
667 switch (xattr_version) {
670 /* These xattr types are unilatteral, they do not
671 * require confirmation of the hash. In particular,
672 * the NTVFS file server uses version 1, but
673 * 'samba-tool ntacl' can set these as well */
678 if (config->ignore_system_acls) {
685 DBG_DEBUG("ACL blob revision mismatch (%u) for file %s\n",
686 (unsigned int)hash_type, smb_fname->base_name);
687 TALLOC_FREE(psd_blob);
691 /* determine which type of xattr we got */
692 if (hash_type != XATTR_SD_HASH_TYPE_SHA256) {
693 DBG_DEBUG("ACL blob hash type (%u) unexpected for file %s\n",
694 (unsigned int)hash_type, smb_fname->base_name);
695 TALLOC_FREE(psd_blob);
699 /* determine which type of xattr we got */
700 switch (xattr_version) {
705 /* Get the full underlying sd, then hash. */
706 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
709 &sys_acl_blob_description,
712 /* Get the full underlying sd, then hash. */
713 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle,
716 &sys_acl_blob_description,
720 /* If we fail to get the ACL blob (for some reason) then this
721 * is not fatal, we just work based on the NT ACL only */
723 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash_tmp);
724 if (!NT_STATUS_IS_OK(status)) {
728 TALLOC_FREE(sys_acl_blob_description);
729 TALLOC_FREE(sys_acl_blob.data);
731 if (memcmp(&sys_acl_hash[0], &sys_acl_hash_tmp[0],
732 XATTR_SD_HASH_SIZE) == 0) {
733 /* Hash matches, return blob sd. */
734 DBG_DEBUG("blob hash matches for file %s\n",
735 smb_fname->base_name);
741 /* Otherwise, fall though and see if the NT ACL hash matches */
744 /* Get the full underlying sd for the hash
745 or to return as backup. */
747 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
753 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
760 if (!NT_STATUS_IS_OK(status)) {
761 DBG_DEBUG("get_next_acl for file %s returned %s\n",
762 smb_fname->base_name, nt_errstr(status));
766 status = hash_sd_sha256(psd_fs, hash_tmp);
767 if (!NT_STATUS_IS_OK(status)) {
768 TALLOC_FREE(psd_blob);
770 *psd_is_from_fs = true;
774 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
775 /* Hash matches, return blob sd. */
776 DBG_DEBUG("blob hash matches for file %s\n",
777 smb_fname->base_name);
782 /* Hash doesn't match, return underlying sd. */
783 DBG_DEBUG("blob hash does not match for file %s - returning "
784 "file system SD mapping.\n",
785 smb_fname->base_name);
787 if (DEBUGLEVEL >= 10) {
788 DBG_DEBUG("acl for blob hash for %s is:\n",
789 smb_fname->base_name);
790 NDR_PRINT_DEBUG(security_descriptor, psd_fs);
793 TALLOC_FREE(psd_blob);
795 *psd_is_from_fs = true;
802 TALLOC_FREE(psd_blob);
804 TALLOC_FREE(sys_acl_blob_description);
805 TALLOC_FREE(sys_acl_blob.data);
809 static NTSTATUS stat_fsp_or_smb_fname(vfs_handle_struct *handle,
811 const struct smb_filename *smb_fname,
812 SMB_STRUCT_STAT *sbuf,
813 SMB_STRUCT_STAT **psbuf)
819 status = vfs_stat_fsp(fsp);
820 if (!NT_STATUS_IS_OK(status)) {
823 *psbuf = &fsp->fsp_name->st;
826 * https://bugzilla.samba.org/show_bug.cgi?id=11249
828 * We are currently guaranteed that 'name' here is a
829 * smb_fname->base_name, which *cannot* contain a stream name
830 * (':'). vfs_stat_smb_fname() splits a name into a base name +
831 * stream name, which when we get here we know we've already
832 * done. So we have to call the stat or lstat VFS calls
833 * directly here. Else, a base_name that contains a ':' (from a
834 * demangled name) will get split again.
837 * This uglyness will go away once smb_fname is fully plumbed
840 ret = vfs_stat_smb_basename(handle->conn,
844 return map_nt_error_from_unix(errno);
851 /*******************************************************************
852 Pull a DATA_BLOB from an xattr given a pathname.
853 If the hash doesn't match, or doesn't exist - return the underlying
855 *******************************************************************/
857 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
859 const struct smb_filename *smb_fname_in,
860 uint32_t security_info,
862 struct security_descriptor **ppdesc)
864 DATA_BLOB blob = data_blob_null;
866 struct security_descriptor *psd = NULL;
867 const struct smb_filename *smb_fname = NULL;
868 bool psd_is_from_fs = false;
869 struct acl_common_config *config = NULL;
871 SMB_VFS_HANDLE_GET_DATA(handle, config,
872 struct acl_common_config,
873 return NT_STATUS_UNSUCCESSFUL);
875 if (fsp && smb_fname_in == NULL) {
876 smb_fname = fsp->fsp_name;
878 smb_fname = smb_fname_in;
881 DBG_DEBUG("name=%s\n", smb_fname->base_name);
883 status = get_acl_blob(mem_ctx, handle, fsp, smb_fname, &blob);
884 if (NT_STATUS_IS_OK(status)) {
885 status = validate_nt_acl_blob(mem_ctx,
892 TALLOC_FREE(blob.data);
893 if (!NT_STATUS_IS_OK(status)) {
894 DBG_DEBUG("ACL validation for [%s] failed\n",
895 smb_fname->base_name);
901 /* Get the full underlying sd, as we failed to get the
902 * blob for the hash, or the revision/hash type wasn't
905 if (config->ignore_system_acls) {
906 SMB_STRUCT_STAT sbuf;
907 SMB_STRUCT_STAT *psbuf = &sbuf;
909 status = stat_fsp_or_smb_fname(handle, fsp, smb_fname,
911 if (!NT_STATUS_IS_OK(status)) {
915 status = make_default_filesystem_acl(
918 smb_fname->base_name,
921 if (!NT_STATUS_IS_OK(status)) {
926 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
932 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
939 if (!NT_STATUS_IS_OK(status)) {
940 DBG_DEBUG("get_next_acl for file %s "
942 smb_fname->base_name,
947 psd_is_from_fs = true;
951 if (psd_is_from_fs) {
952 SMB_STRUCT_STAT sbuf;
953 SMB_STRUCT_STAT *psbuf = &sbuf;
954 bool is_directory = false;
957 * We're returning the underlying ACL from the
958 * filesystem. If it's a directory, and has no
959 * inheritable ACE entries we have to fake them.
962 status = stat_fsp_or_smb_fname(handle, fsp, smb_fname,
964 if (!NT_STATUS_IS_OK(status)) {
968 is_directory = S_ISDIR(psbuf->st_ex_mode);
970 if (is_directory && !sd_has_inheritable_components(psd, true)) {
971 status = add_directory_inheritable_components(
973 smb_fname->base_name,
976 if (!NT_STATUS_IS_OK(status)) {
982 * The underlying POSIX module always sets the
983 * ~SEC_DESC_DACL_PROTECTED bit, as ACLs can't be inherited in
984 * this way under POSIX. Remove it for Windows-style ACLs.
986 psd->type &= ~SEC_DESC_DACL_PROTECTED;
989 if (!(security_info & SECINFO_OWNER)) {
990 psd->owner_sid = NULL;
992 if (!(security_info & SECINFO_GROUP)) {
993 psd->group_sid = NULL;
995 if (!(security_info & SECINFO_DACL)) {
996 psd->type &= ~SEC_DESC_DACL_PRESENT;
999 if (!(security_info & SECINFO_SACL)) {
1000 psd->type &= ~SEC_DESC_SACL_PRESENT;
1004 if (DEBUGLEVEL >= 10) {
1005 DBG_DEBUG("returning acl for %s is:\n",
1006 smb_fname->base_name);
1007 NDR_PRINT_DEBUG(security_descriptor, psd);
1012 return NT_STATUS_OK;
1019 /*********************************************************************
1020 Fetch a security descriptor given an fsp.
1021 *********************************************************************/
1023 static NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle,
1025 uint32_t security_info,
1026 TALLOC_CTX *mem_ctx,
1027 struct security_descriptor **ppdesc)
1029 return get_nt_acl_internal(handle, fsp,
1030 NULL, security_info, mem_ctx, ppdesc);
1033 /*********************************************************************
1034 Fetch a security descriptor given a pathname.
1035 *********************************************************************/
1037 static NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
1038 const struct smb_filename *smb_fname,
1039 uint32_t security_info,
1040 TALLOC_CTX *mem_ctx,
1041 struct security_descriptor **ppdesc)
1043 return get_nt_acl_internal(handle,
1051 /*********************************************************************
1052 Set the underlying ACL (e.g. POSIX ACLS, POSIX owner, etc)
1053 *********************************************************************/
1054 static NTSTATUS set_underlying_acl(vfs_handle_struct *handle, files_struct *fsp,
1055 struct security_descriptor *psd,
1056 uint32_t security_info_sent,
1060 SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
1061 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1065 /* We got access denied here. If we're already root,
1066 or we didn't need to do a chown, or the fsp isn't
1067 open with WRITE_OWNER access, just return. */
1068 if (get_current_uid(handle->conn) == 0 || chown_needed == false ||
1069 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
1070 return NT_STATUS_ACCESS_DENIED;
1073 DBG_DEBUG("overriding chown on file %s for sid %s\n",
1074 fsp_str_dbg(fsp), sid_string_tos(psd->owner_sid));
1076 /* Ok, we failed to chown and we have
1077 SEC_STD_WRITE_OWNER access - override. */
1079 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
1085 /*********************************************************************
1086 Store a v3 security descriptor
1087 *********************************************************************/
1088 static NTSTATUS store_v3_blob(vfs_handle_struct *handle, files_struct *fsp,
1089 struct security_descriptor *psd,
1090 struct security_descriptor *pdesc_next,
1091 uint8_t hash[XATTR_SD_HASH_SIZE])
1096 if (DEBUGLEVEL >= 10) {
1097 DBG_DEBUG("storing xattr sd for file %s\n",
1100 security_descriptor,
1101 discard_const_p(struct security_descriptor, psd));
1103 if (pdesc_next != NULL) {
1104 DBG_DEBUG("storing xattr sd based on \n");
1106 security_descriptor,
1107 discard_const_p(struct security_descriptor,
1110 DBG_DEBUG("ignoring underlying sd\n");
1113 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
1114 if (!NT_STATUS_IS_OK(status)) {
1115 DBG_DEBUG("create_acl_blob failed\n");
1119 status = store_acl_blob_fsp(handle, fsp, &blob);
1123 /*********************************************************************
1124 Store a security descriptor given an fsp.
1125 *********************************************************************/
1127 static NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
1128 uint32_t security_info_sent, const struct security_descriptor *orig_psd)
1132 DATA_BLOB blob, sys_acl_blob;
1133 struct security_descriptor *pdesc_next = NULL;
1134 struct security_descriptor *psd = NULL;
1135 uint8_t hash[XATTR_SD_HASH_SIZE];
1136 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
1137 bool chown_needed = false;
1138 char *sys_acl_description;
1139 TALLOC_CTX *frame = talloc_stackframe();
1140 bool ignore_file_system_acl = lp_parm_bool(
1141 SNUM(handle->conn), ACL_MODULE_NAME, "ignore system acls", false);
1143 if (DEBUGLEVEL >= 10) {
1144 DBG_DEBUG("incoming sd for file %s\n", fsp_str_dbg(fsp));
1145 NDR_PRINT_DEBUG(security_descriptor,
1146 discard_const_p(struct security_descriptor, orig_psd));
1149 status = get_nt_acl_internal(handle, fsp,
1151 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
1155 if (!NT_STATUS_IS_OK(status)) {
1160 psd->revision = orig_psd->revision;
1161 /* All our SD's are self relative. */
1162 psd->type = orig_psd->type | SEC_DESC_SELF_RELATIVE;
1164 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
1165 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
1166 /* We're changing the owner. */
1167 chown_needed = true;
1169 psd->owner_sid = orig_psd->owner_sid;
1171 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
1172 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
1173 /* We're changing the group. */
1174 chown_needed = true;
1176 psd->group_sid = orig_psd->group_sid;
1178 if (security_info_sent & SECINFO_DACL) {
1179 if (security_descriptor_with_ms_nfs(orig_psd)) {
1181 * If the sd contains a MS NFS SID, do
1182 * nothing, it's a chmod() request from OS X
1183 * with AAPL context.
1186 return NT_STATUS_OK;
1188 psd->dacl = orig_psd->dacl;
1189 psd->type |= SEC_DESC_DACL_PRESENT;
1191 if (security_info_sent & SECINFO_SACL) {
1192 psd->sacl = orig_psd->sacl;
1193 psd->type |= SEC_DESC_SACL_PRESENT;
1196 if (ignore_file_system_acl) {
1198 /* send only ownership stuff to lower layer */
1199 security_info_sent &= (SECINFO_OWNER | SECINFO_GROUP);
1200 status = set_underlying_acl(handle, fsp, psd,
1201 security_info_sent, true);
1202 if (!NT_STATUS_IS_OK(status)) {
1208 status = store_v3_blob(handle, fsp, psd, NULL, hash);
1214 status = set_underlying_acl(handle, fsp, psd, security_info_sent,
1216 if (!NT_STATUS_IS_OK(status)) {
1221 /* Get the full underlying sd, then hash. */
1222 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
1228 if (!NT_STATUS_IS_OK(status)) {
1233 status = hash_sd_sha256(pdesc_next, hash);
1234 if (!NT_STATUS_IS_OK(status)) {
1239 /* Get the full underlying sd, then hash. */
1240 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
1243 &sys_acl_description,
1246 /* If we fail to get the ACL blob (for some reason) then this
1247 * is not fatal, we just work based on the NT ACL only */
1249 status = store_v3_blob(handle, fsp, psd, pdesc_next, hash);
1255 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash);
1256 if (!NT_STATUS_IS_OK(status)) {
1261 if (DEBUGLEVEL >= 10) {
1262 DBG_DEBUG("storing xattr sd for file %s based on system ACL\n",
1264 NDR_PRINT_DEBUG(security_descriptor,
1265 discard_const_p(struct security_descriptor, psd));
1267 DBG_DEBUG("storing hash in xattr sd based on system ACL and:\n");
1268 NDR_PRINT_DEBUG(security_descriptor,
1269 discard_const_p(struct security_descriptor, pdesc_next));
1272 /* We store hashes of both the sys ACL blob and the NT
1273 * security desciptor mapped from that ACL so as to improve
1274 * our chances against some inadvertant change breaking the
1276 status = create_sys_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash,
1277 sys_acl_description, sys_acl_hash);
1278 if (!NT_STATUS_IS_OK(status)) {
1279 DBG_DEBUG("create_sys_acl_blob failed\n");
1284 status = store_acl_blob_fsp(handle, fsp, &blob);
1290 static int acl_common_remove_object(vfs_handle_struct *handle,
1291 const struct smb_filename *smb_fname,
1294 connection_struct *conn = handle->conn;
1296 files_struct *fsp = NULL;
1298 char *parent_dir = NULL;
1299 const char *final_component = NULL;
1300 struct smb_filename local_fname = {0};
1301 struct smb_filename parent_dir_fname = {0};
1302 int saved_errno = 0;
1303 char *saved_dir = NULL;
1304 struct smb_filename saved_dir_fname = {0};
1306 saved_dir = vfs_GetWd(talloc_tos(),conn);
1308 saved_errno = errno;
1311 saved_dir_fname = (struct smb_filename) { .base_name = saved_dir };
1313 if (!parent_dirname(talloc_tos(), smb_fname->base_name,
1314 &parent_dir, &final_component)) {
1315 saved_errno = ENOMEM;
1319 DBG_DEBUG("removing %s %s/%s\n", is_directory ? "directory" : "file",
1320 parent_dir, final_component);
1322 parent_dir_fname = (struct smb_filename) { .base_name = parent_dir };
1324 /* cd into the parent dir to pin it. */
1325 ret = vfs_ChDir(conn, &parent_dir_fname);
1327 saved_errno = errno;
1331 local_fname.base_name = discard_const_p(char, final_component);
1333 /* Must use lstat here. */
1334 ret = SMB_VFS_LSTAT(conn, &local_fname);
1336 saved_errno = errno;
1340 /* Ensure we have this file open with DELETE access. */
1341 id = vfs_file_id_from_sbuf(conn, &local_fname.st);
1342 for (fsp = file_find_di_first(conn->sconn, id); fsp;
1343 fsp = file_find_di_next(fsp)) {
1344 if (fsp->access_mask & DELETE_ACCESS &&
1345 fsp->delete_on_close) {
1346 /* We did open this for delete,
1347 * allow the delete as root.
1354 DBG_DEBUG("%s %s/%s not an open file\n",
1355 is_directory ? "directory" : "file",
1356 parent_dir, final_component);
1357 saved_errno = EACCES;
1363 ret = SMB_VFS_NEXT_RMDIR(handle, &local_fname);
1365 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
1370 saved_errno = errno;
1375 TALLOC_FREE(parent_dir);
1378 vfs_ChDir(conn, &saved_dir_fname);
1381 errno = saved_errno;
1386 static int rmdir_acl_common(struct vfs_handle_struct *handle,
1387 const struct smb_filename *smb_fname)
1391 /* Try the normal rmdir first. */
1392 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1396 if (errno == EACCES || errno == EPERM) {
1397 /* Failed due to access denied,
1398 see if we need to root override. */
1399 return acl_common_remove_object(handle,
1404 DBG_DEBUG("unlink of %s failed %s\n",
1405 smb_fname->base_name,
1410 static int unlink_acl_common(struct vfs_handle_struct *handle,
1411 const struct smb_filename *smb_fname)
1415 /* Try the normal unlink first. */
1416 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1420 if (errno == EACCES || errno == EPERM) {
1421 /* Failed due to access denied,
1422 see if we need to root override. */
1424 /* Don't do anything fancy for streams. */
1425 if (smb_fname->stream_name) {
1428 return acl_common_remove_object(handle,
1433 DBG_DEBUG("unlink of %s failed %s\n",
1434 smb_fname->base_name,
1439 static int chmod_acl_module_common(struct vfs_handle_struct *handle,
1440 const struct smb_filename *smb_fname,
1443 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1444 /* Only allow this on POSIX pathnames. */
1445 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1450 static int fchmod_acl_module_common(struct vfs_handle_struct *handle,
1451 struct files_struct *fsp, mode_t mode)
1453 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1454 /* Only allow this on POSIX opens. */
1455 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1460 static int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1461 const struct smb_filename *smb_fname,
1464 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1465 /* Only allow this on POSIX pathnames. */
1466 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
1471 static int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1472 struct files_struct *fsp, mode_t mode)
1474 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1475 /* Only allow this on POSIX opens. */
1476 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);