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
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "../libcli/security/security.h"
25 #include "../librpc/gen_ndr/ndr_security.h"
26 #include "../lib/util/bitmap.h"
27 #include "passdb/lookup_sid.h"
29 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
32 uint8_t hash[XATTR_SD_HASH_SIZE]);
34 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
35 vfs_handle_struct *handle,
37 const struct smb_filename *smb_fname,
40 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
44 #define HASH_SECURITY_INFO (SECINFO_OWNER | \
49 /*******************************************************************
50 Hash a security descriptor.
51 *******************************************************************/
53 static NTSTATUS hash_blob_sha256(DATA_BLOB blob,
58 memset(hash, '\0', XATTR_SD_HASH_SIZE);
60 samba_SHA256_Init(&tctx);
61 samba_SHA256_Update(&tctx, blob.data, blob.length);
62 samba_SHA256_Final(hash, &tctx);
67 /*******************************************************************
68 Hash a security descriptor.
69 *******************************************************************/
71 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
77 memset(hash, '\0', XATTR_SD_HASH_SIZE);
78 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
79 if (!NT_STATUS_IS_OK(status)) {
82 return hash_blob_sha256(blob, hash);
85 /*******************************************************************
86 Parse out a struct security_descriptor from a DATA_BLOB.
87 *******************************************************************/
89 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
91 struct security_descriptor **ppdesc,
92 uint16_t *p_hash_type,
94 uint8_t hash[XATTR_SD_HASH_SIZE],
95 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
97 struct xattr_NTACL xacl;
98 enum ndr_err_code ndr_err;
100 TALLOC_CTX *frame = talloc_stackframe();
102 ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
103 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
105 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
106 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
107 ndr_errstr(ndr_err)));
109 return ndr_map_error2ntstatus(ndr_err);
112 *p_version = xacl.version;
114 switch (xacl.version) {
116 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
117 xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
118 xacl.info.sd->owner_sid,
119 xacl.info.sd->group_sid,
123 /* No hash - null out. */
124 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
125 memset(hash, '\0', XATTR_SD_HASH_SIZE);
128 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
129 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
130 xacl.info.sd_hs2->sd->owner_sid,
131 xacl.info.sd_hs2->sd->group_sid,
132 xacl.info.sd_hs2->sd->sacl,
133 xacl.info.sd_hs2->sd->dacl,
135 /* No hash - null out. */
136 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
137 memset(hash, '\0', XATTR_SD_HASH_SIZE);
140 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
141 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
142 xacl.info.sd_hs3->sd->owner_sid,
143 xacl.info.sd_hs3->sd->group_sid,
144 xacl.info.sd_hs3->sd->sacl,
145 xacl.info.sd_hs3->sd->dacl,
147 *p_hash_type = xacl.info.sd_hs3->hash_type;
148 /* Current version 3 (if no sys acl hash available). */
149 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
152 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
153 xacl.info.sd_hs4->sd->type | SEC_DESC_SELF_RELATIVE,
154 xacl.info.sd_hs4->sd->owner_sid,
155 xacl.info.sd_hs4->sd->group_sid,
156 xacl.info.sd_hs4->sd->sacl,
157 xacl.info.sd_hs4->sd->dacl,
159 *p_hash_type = xacl.info.sd_hs4->hash_type;
160 /* Current version 4. */
161 memcpy(hash, xacl.info.sd_hs4->hash, XATTR_SD_HASH_SIZE);
162 memcpy(sys_acl_hash, xacl.info.sd_hs4->sys_acl_hash, XATTR_SD_HASH_SIZE);
166 return NT_STATUS_REVISION_MISMATCH;
171 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
174 /*******************************************************************
175 Create a DATA_BLOB from a hash of the security descriptor storead at
176 the system layer and the NT ACL we wish to preserve
177 *******************************************************************/
179 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
182 uint8_t hash[XATTR_SD_HASH_SIZE])
184 struct xattr_NTACL xacl;
185 struct security_descriptor_hash_v3 sd_hs3;
186 enum ndr_err_code ndr_err;
187 TALLOC_CTX *ctx = talloc_tos();
193 xacl.info.sd_hs3 = &sd_hs3;
194 xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
195 xacl.info.sd_hs3->hash_type = hash_type;
196 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
198 ndr_err = ndr_push_struct_blob(
200 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
202 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
203 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
204 ndr_errstr(ndr_err)));
205 return ndr_map_error2ntstatus(ndr_err);
211 /*******************************************************************
212 Create a DATA_BLOB from a hash of the security descriptors
213 (system and NT) stored at the system layer and the NT ACL we wish
215 *******************************************************************/
217 static NTSTATUS create_sys_acl_blob(const struct security_descriptor *psd,
220 uint8_t hash[XATTR_SD_HASH_SIZE],
221 const char *description,
222 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
224 struct xattr_NTACL xacl;
225 struct security_descriptor_hash_v4 sd_hs4;
226 enum ndr_err_code ndr_err;
227 TALLOC_CTX *ctx = talloc_tos();
229 struct timeval now = timeval_current();
230 nttime_now = timeval_to_nttime(&now);
236 xacl.info.sd_hs4 = &sd_hs4;
237 xacl.info.sd_hs4->sd = discard_const_p(struct security_descriptor, psd);
238 xacl.info.sd_hs4->hash_type = hash_type;
239 memcpy(&xacl.info.sd_hs4->hash[0], hash, XATTR_SD_HASH_SIZE);
240 xacl.info.sd_hs4->description = description;
241 xacl.info.sd_hs4->time = nttime_now;
242 memcpy(&xacl.info.sd_hs4->sys_acl_hash[0], sys_acl_hash, XATTR_SD_HASH_SIZE);
244 ndr_err = ndr_push_struct_blob(
246 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
248 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
249 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
250 ndr_errstr(ndr_err)));
251 return ndr_map_error2ntstatus(ndr_err);
257 /*******************************************************************
258 Add in 3 inheritable components for a non-inheritable directory ACL.
259 CREATOR_OWNER/CREATOR_GROUP/WORLD.
260 *******************************************************************/
262 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
264 SMB_STRUCT_STAT *psbuf,
265 struct security_descriptor *psd)
267 struct connection_struct *conn = handle->conn;
268 int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
269 struct smb_filename smb_fname;
270 enum security_ace_type acltype;
271 uint32_t access_mask;
275 struct security_ace *new_ace_list;
278 new_ace_list = talloc_zero_array(psd->dacl,
283 * make_sec_acl() at the bottom of this function
284 * dupliates new_ace_list
286 new_ace_list = talloc_zero_array(talloc_tos(),
291 if (new_ace_list == NULL) {
292 return NT_STATUS_NO_MEMORY;
295 /* Fake a quick smb_filename. */
296 ZERO_STRUCT(smb_fname);
297 smb_fname.st = *psbuf;
298 smb_fname.base_name = discard_const_p(char, name);
300 dir_mode = unix_mode(conn,
301 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
302 file_mode = unix_mode(conn,
303 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
305 mode = dir_mode | file_mode;
307 DEBUG(10, ("add_directory_inheritable_components: directory %s, "
310 (unsigned int)mode ));
313 memcpy(new_ace_list, psd->dacl->aces,
314 num_aces * sizeof(struct security_ace));
316 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
319 init_sec_ace(&new_ace_list[num_aces],
320 &global_sid_Creator_Owner,
323 SEC_ACE_FLAG_CONTAINER_INHERIT|
324 SEC_ACE_FLAG_OBJECT_INHERIT|
325 SEC_ACE_FLAG_INHERIT_ONLY);
326 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
327 (mode << 3) & 0700, false);
328 init_sec_ace(&new_ace_list[num_aces+1],
329 &global_sid_Creator_Group,
332 SEC_ACE_FLAG_CONTAINER_INHERIT|
333 SEC_ACE_FLAG_OBJECT_INHERIT|
334 SEC_ACE_FLAG_INHERIT_ONLY);
335 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
336 (mode << 6) & 0700, false);
337 init_sec_ace(&new_ace_list[num_aces+2],
341 SEC_ACE_FLAG_CONTAINER_INHERIT|
342 SEC_ACE_FLAG_OBJECT_INHERIT|
343 SEC_ACE_FLAG_INHERIT_ONLY);
345 psd->dacl->aces = new_ace_list;
346 psd->dacl->num_aces += 3;
347 psd->dacl->size += new_ace_list[num_aces].size +
348 new_ace_list[num_aces+1].size +
349 new_ace_list[num_aces+2].size;
351 psd->dacl = make_sec_acl(psd,
355 if (psd->dacl == NULL) {
356 return NT_STATUS_NO_MEMORY;
362 static NTSTATUS make_default_filesystem_acl(TALLOC_CTX *ctx,
364 SMB_STRUCT_STAT *psbuf,
365 struct security_descriptor **ppdesc)
367 struct dom_sid owner_sid, group_sid;
369 struct security_ace aces[4];
370 uint32_t access_mask = 0;
371 mode_t mode = psbuf->st_ex_mode;
372 struct security_acl *new_dacl = NULL;
375 DEBUG(10,("make_default_filesystem_acl: file %s mode = 0%o\n",
378 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
379 gid_to_sid(&group_sid, psbuf->st_ex_gid);
382 We provide up to 4 ACEs
389 if (mode & S_IRUSR) {
390 if (mode & S_IWUSR) {
391 access_mask |= SEC_RIGHTS_FILE_ALL;
393 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
396 if (mode & S_IWUSR) {
397 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
400 init_sec_ace(&aces[idx],
402 SEC_ACE_TYPE_ACCESS_ALLOWED,
408 if (mode & S_IRGRP) {
409 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
411 if (mode & S_IWGRP) {
412 /* note that delete is not granted - this matches posix behaviour */
413 access_mask |= SEC_RIGHTS_FILE_WRITE;
416 init_sec_ace(&aces[idx],
418 SEC_ACE_TYPE_ACCESS_ALLOWED,
425 if (mode & S_IROTH) {
426 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
428 if (mode & S_IWOTH) {
429 access_mask |= SEC_RIGHTS_FILE_WRITE;
432 init_sec_ace(&aces[idx],
434 SEC_ACE_TYPE_ACCESS_ALLOWED,
440 init_sec_ace(&aces[idx],
442 SEC_ACE_TYPE_ACCESS_ALLOWED,
447 new_dacl = make_sec_acl(ctx,
453 return NT_STATUS_NO_MEMORY;
456 *ppdesc = make_sec_desc(ctx,
457 SECURITY_DESCRIPTOR_REVISION_1,
458 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
465 return NT_STATUS_NO_MEMORY;
470 /*******************************************************************
471 Pull a DATA_BLOB from an xattr given a pathname.
472 If the hash doesn't match, or doesn't exist - return the underlying
474 *******************************************************************/
476 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
478 const struct smb_filename *smb_fname_in,
479 uint32_t security_info,
481 struct security_descriptor **ppdesc)
483 DATA_BLOB blob = data_blob_null;
485 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
486 uint16_t xattr_version = 0;
487 uint8_t hash[XATTR_SD_HASH_SIZE];
488 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
489 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
490 uint8_t sys_acl_hash_tmp[XATTR_SD_HASH_SIZE];
491 struct security_descriptor *psd_blob = NULL;
492 struct security_descriptor *psd_fs = NULL;
493 const struct smb_filename *smb_fname = NULL;
494 bool ignore_file_system_acl = lp_parm_bool(SNUM(handle->conn),
496 "ignore system acls",
498 TALLOC_CTX *frame = talloc_stackframe();
500 if (fsp && smb_fname_in == NULL) {
501 smb_fname = fsp->fsp_name;
503 smb_fname = smb_fname_in;
506 DEBUG(10, ("get_nt_acl_internal: name=%s\n", smb_fname->base_name));
508 status = get_acl_blob(frame, handle, fsp, smb_fname, &blob);
509 if (!NT_STATUS_IS_OK(status)) {
510 DEBUG(10, ("get_nt_acl_internal: get_acl_blob returned %s\n",
515 status = parse_acl_blob(&blob, mem_ctx, &psd_blob,
516 &hash_type, &xattr_version, &hash[0], &sys_acl_hash[0]);
517 if (!NT_STATUS_IS_OK(status)) {
518 DEBUG(10, ("parse_acl_blob returned %s\n",
525 /* Ensure we don't leak psd_blob if we don't choose it.
527 * We don't allocate it onto frame as it is preferred not to
528 * steal from a talloc pool.
530 talloc_steal(frame, psd_blob);
532 /* determine which type of xattr we got */
533 switch (xattr_version) {
536 /* These xattr types are unilatteral, they do not
537 * require confirmation of the hash. In particular,
538 * the NTVFS file server uses version 1, but
539 * 'samba-tool ntacl' can set these as well */
543 if (ignore_file_system_acl) {
549 DEBUG(10, ("get_nt_acl_internal: ACL blob revision "
550 "mismatch (%u) for file %s\n",
551 (unsigned int)hash_type,
552 smb_fname->base_name));
553 TALLOC_FREE(psd_blob);
558 /* determine which type of xattr we got */
559 if (hash_type != XATTR_SD_HASH_TYPE_SHA256) {
560 DEBUG(10, ("get_nt_acl_internal: ACL blob hash type "
561 "(%u) unexpected for file %s\n",
562 (unsigned int)hash_type,
563 smb_fname->base_name));
564 TALLOC_FREE(psd_blob);
569 /* determine which type of xattr we got */
570 switch (xattr_version) {
574 char *sys_acl_blob_description;
575 DATA_BLOB sys_acl_blob;
577 /* Get the full underlying sd, then hash. */
578 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
581 &sys_acl_blob_description,
584 /* Get the full underlying sd, then hash. */
585 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle,
586 smb_fname->base_name,
588 &sys_acl_blob_description,
592 /* If we fail to get the ACL blob (for some reason) then this
593 * is not fatal, we just work based on the NT ACL only */
595 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash_tmp);
596 if (!NT_STATUS_IS_OK(status)) {
601 if (memcmp(&sys_acl_hash[0], &sys_acl_hash_tmp[0],
602 XATTR_SD_HASH_SIZE) == 0) {
603 /* Hash matches, return blob sd. */
604 DEBUG(10, ("get_nt_acl_internal: blob hash "
605 "matches for file %s\n",
606 smb_fname->base_name ));
611 /* Otherwise, fall though and see if the NT ACL hash matches */
614 /* Get the full underlying sd for the hash
615 or to return as backup. */
617 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
623 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
630 if (!NT_STATUS_IS_OK(status)) {
631 DEBUG(10, ("get_nt_acl_internal: get_next_acl for file %s "
633 smb_fname->base_name,
639 /* Ensure we don't leak psd_fs if we don't choose it.
641 * We don't allocate it onto frame as it is preferred not to
642 * steal from a talloc pool.
644 talloc_steal(frame, psd_fs);
646 status = hash_sd_sha256(psd_fs, hash_tmp);
647 if (!NT_STATUS_IS_OK(status)) {
648 TALLOC_FREE(psd_blob);
653 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
654 /* Hash matches, return blob sd. */
655 DEBUG(10, ("get_nt_acl_internal: blob hash "
656 "matches for file %s\n",
657 smb_fname->base_name ));
661 /* Hash doesn't match, return underlying sd. */
662 DEBUG(10, ("get_nt_acl_internal: blob hash "
663 "does not match for file %s - returning "
664 "file system SD mapping.\n",
665 smb_fname->base_name ));
667 if (DEBUGLEVEL >= 10) {
668 DEBUG(10,("get_nt_acl_internal: acl for blob hash for %s is:\n",
669 smb_fname->base_name ));
670 NDR_PRINT_DEBUG(security_descriptor, psd_fs);
673 TALLOC_FREE(psd_blob);
678 if (psd_blob == NULL) {
679 /* Get the full underlying sd, as we failed to get the
680 * blob for the hash, or the revision/hash type wasn't
683 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
689 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
696 if (!NT_STATUS_IS_OK(status)) {
697 DEBUG(10, ("get_nt_acl_internal: get_next_acl for file %s "
699 smb_fname->base_name,
705 /* Ensure we don't leak psd_fs if we don't choose it.
707 * We don't allocate it onto frame as it is preferred not to
708 * steal from a talloc pool.
710 talloc_steal(frame, psd_fs);
714 if (psd_blob != psd_fs) {
715 /* We're returning the blob, throw
716 * away the filesystem SD. */
719 SMB_STRUCT_STAT sbuf;
720 SMB_STRUCT_STAT *psbuf = &sbuf;
721 bool is_directory = false;
723 * We're returning the underlying ACL from the
724 * filesystem. If it's a directory, and has no
725 * inheritable ACE entries we have to fake them.
728 status = vfs_stat_fsp(fsp);
729 if (!NT_STATUS_IS_OK(status)) {
733 psbuf = &fsp->fsp_name->st;
736 * https://bugzilla.samba.org/show_bug.cgi?id=11249
738 * We are currently guaranteed that 'name' here is
739 * a smb_fname->base_name, which *cannot* contain
740 * a stream name (':'). vfs_stat_smb_fname() splits
741 * a name into a base name + stream name, which
742 * when we get here we know we've already done.
743 * So we have to call the stat or lstat VFS
744 * calls directly here. Else, a base_name that
745 * contains a ':' (from a demangled name) will
749 * This uglyness will go away once smb_fname
750 * is fully plumbed through the VFS.
752 int ret = vfs_stat_smb_basename(handle->conn,
757 return map_nt_error_from_unix(errno);
760 is_directory = S_ISDIR(psbuf->st_ex_mode);
762 if (ignore_file_system_acl) {
764 status = make_default_filesystem_acl(mem_ctx,
765 smb_fname->base_name,
768 if (!NT_STATUS_IS_OK(status)) {
774 !sd_has_inheritable_components(psd_blob,
776 status = add_directory_inheritable_components(
778 smb_fname->base_name,
781 if (!NT_STATUS_IS_OK(status)) {
786 /* The underlying POSIX module always sets
787 the ~SEC_DESC_DACL_PROTECTED bit, as ACLs
788 can't be inherited in this way under POSIX.
789 Remove it for Windows-style ACLs. */
790 psd_blob->type &= ~SEC_DESC_DACL_PROTECTED;
794 if (!(security_info & SECINFO_OWNER)) {
795 psd_blob->owner_sid = NULL;
797 if (!(security_info & SECINFO_GROUP)) {
798 psd_blob->group_sid = NULL;
800 if (!(security_info & SECINFO_DACL)) {
801 psd_blob->type &= ~SEC_DESC_DACL_PRESENT;
802 psd_blob->dacl = NULL;
804 if (!(security_info & SECINFO_SACL)) {
805 psd_blob->type &= ~SEC_DESC_SACL_PRESENT;
806 psd_blob->sacl = NULL;
809 TALLOC_FREE(blob.data);
811 if (DEBUGLEVEL >= 10) {
812 DEBUG(10,("get_nt_acl_internal: returning acl for %s is:\n",
813 smb_fname->base_name ));
814 NDR_PRINT_DEBUG(security_descriptor, psd_blob);
817 /* The VFS API is that the ACL is expected to be on mem_ctx */
818 *ppdesc = talloc_move(mem_ctx, &psd_blob);
824 /*********************************************************************
825 Fetch a security descriptor given an fsp.
826 *********************************************************************/
828 static NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle,
830 uint32_t security_info,
832 struct security_descriptor **ppdesc)
834 return get_nt_acl_internal(handle, fsp,
835 NULL, security_info, mem_ctx, ppdesc);
838 /*********************************************************************
839 Fetch a security descriptor given a pathname.
840 *********************************************************************/
842 static NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
843 const struct smb_filename *smb_fname,
844 uint32_t security_info,
846 struct security_descriptor **ppdesc)
848 return get_nt_acl_internal(handle,
856 /*********************************************************************
857 Set the underlying ACL (e.g. POSIX ACLS, POSIX owner, etc)
858 *********************************************************************/
859 static NTSTATUS set_underlying_acl(vfs_handle_struct *handle, files_struct *fsp,
860 struct security_descriptor *psd,
861 uint32_t security_info_sent,
865 SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
866 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
870 /* We got access denied here. If we're already root,
871 or we didn't need to do a chown, or the fsp isn't
872 open with WRITE_OWNER access, just return. */
873 if (get_current_uid(handle->conn) == 0 || chown_needed == false ||
874 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
875 return NT_STATUS_ACCESS_DENIED;
878 DEBUG(10, ("fset_nt_acl_common: overriding chown on file %s "
880 fsp_str_dbg(fsp), sid_string_tos(psd->owner_sid)));
882 /* Ok, we failed to chown and we have
883 SEC_STD_WRITE_OWNER access - override. */
885 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
891 /*********************************************************************
892 Store a v3 security descriptor
893 *********************************************************************/
894 static NTSTATUS store_v3_blob(vfs_handle_struct *handle, files_struct *fsp,
895 struct security_descriptor *psd,
896 struct security_descriptor *pdesc_next,
897 uint8_t hash[XATTR_SD_HASH_SIZE])
902 if (DEBUGLEVEL >= 10) {
903 DEBUG(10, ("fset_nt_acl_xattr: storing xattr sd for file %s\n",
907 discard_const_p(struct security_descriptor, psd));
909 if (pdesc_next != NULL) {
910 DEBUG(10, ("fset_nt_acl_xattr: storing has in xattr sd "
914 discard_const_p(struct security_descriptor,
918 ("fset_nt_acl_xattr: ignoring underlying sd\n"));
921 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
922 if (!NT_STATUS_IS_OK(status)) {
923 DEBUG(10, ("fset_nt_acl_xattr: create_acl_blob failed\n"));
927 status = store_acl_blob_fsp(handle, fsp, &blob);
931 /*********************************************************************
932 Store a security descriptor given an fsp.
933 *********************************************************************/
935 static NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
936 uint32_t security_info_sent, const struct security_descriptor *orig_psd)
940 DATA_BLOB blob, sys_acl_blob;
941 struct security_descriptor *pdesc_next = NULL;
942 struct security_descriptor *psd = NULL;
943 uint8_t hash[XATTR_SD_HASH_SIZE];
944 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
945 bool chown_needed = false;
946 char *sys_acl_description;
947 TALLOC_CTX *frame = talloc_stackframe();
948 bool ignore_file_system_acl = lp_parm_bool(
949 SNUM(handle->conn), ACL_MODULE_NAME, "ignore system acls", false);
951 if (DEBUGLEVEL >= 10) {
952 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
954 NDR_PRINT_DEBUG(security_descriptor,
955 discard_const_p(struct security_descriptor, orig_psd));
958 status = get_nt_acl_internal(handle, fsp,
960 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
964 if (!NT_STATUS_IS_OK(status)) {
969 psd->revision = orig_psd->revision;
970 /* All our SD's are self relative. */
971 psd->type = orig_psd->type | SEC_DESC_SELF_RELATIVE;
973 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
974 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
975 /* We're changing the owner. */
978 psd->owner_sid = orig_psd->owner_sid;
980 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
981 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
982 /* We're changing the group. */
985 psd->group_sid = orig_psd->group_sid;
987 if (security_info_sent & SECINFO_DACL) {
988 if (security_descriptor_with_ms_nfs(orig_psd)) {
990 * If the sd contains a MS NFS SID, do
991 * nothing, it's a chmod() request from OS X
997 psd->dacl = orig_psd->dacl;
998 psd->type |= SEC_DESC_DACL_PRESENT;
1000 if (security_info_sent & SECINFO_SACL) {
1001 psd->sacl = orig_psd->sacl;
1002 psd->type |= SEC_DESC_SACL_PRESENT;
1005 if (ignore_file_system_acl) {
1007 /* send only ownership stuff to lower layer */
1008 security_info_sent &= (SECINFO_OWNER | SECINFO_GROUP);
1009 status = set_underlying_acl(handle, fsp, psd,
1010 security_info_sent, true);
1011 if (!NT_STATUS_IS_OK(status)) {
1017 status = store_v3_blob(handle, fsp, psd, NULL, hash);
1023 status = set_underlying_acl(handle, fsp, psd, security_info_sent,
1025 if (!NT_STATUS_IS_OK(status)) {
1030 /* Get the full underlying sd, then hash. */
1031 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
1037 if (!NT_STATUS_IS_OK(status)) {
1042 status = hash_sd_sha256(pdesc_next, hash);
1043 if (!NT_STATUS_IS_OK(status)) {
1048 /* Get the full underlying sd, then hash. */
1049 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
1052 &sys_acl_description,
1055 /* If we fail to get the ACL blob (for some reason) then this
1056 * is not fatal, we just work based on the NT ACL only */
1058 status = store_v3_blob(handle, fsp, psd, pdesc_next, hash);
1064 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash);
1065 if (!NT_STATUS_IS_OK(status)) {
1070 if (DEBUGLEVEL >= 10) {
1071 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s based on system ACL\n",
1073 NDR_PRINT_DEBUG(security_descriptor,
1074 discard_const_p(struct security_descriptor, psd));
1076 DEBUG(10,("fset_nt_acl_xattr: storing hash in xattr sd based on system ACL and:\n"));
1077 NDR_PRINT_DEBUG(security_descriptor,
1078 discard_const_p(struct security_descriptor, pdesc_next));
1081 /* We store hashes of both the sys ACL blob and the NT
1082 * security desciptor mapped from that ACL so as to improve
1083 * our chances against some inadvertant change breaking the
1085 status = create_sys_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash,
1086 sys_acl_description, sys_acl_hash);
1087 if (!NT_STATUS_IS_OK(status)) {
1088 DEBUG(10, ("fset_nt_acl_xattr: create_sys_acl_blob failed\n"));
1093 status = store_acl_blob_fsp(handle, fsp, &blob);
1099 static int acl_common_remove_object(vfs_handle_struct *handle,
1103 connection_struct *conn = handle->conn;
1105 files_struct *fsp = NULL;
1107 char *parent_dir = NULL;
1108 const char *final_component = NULL;
1109 struct smb_filename local_fname;
1110 int saved_errno = 0;
1111 char *saved_dir = NULL;
1113 saved_dir = vfs_GetWd(talloc_tos(),conn);
1115 saved_errno = errno;
1119 if (!parent_dirname(talloc_tos(), path,
1120 &parent_dir, &final_component)) {
1121 saved_errno = ENOMEM;
1125 DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
1126 is_directory ? "directory" : "file",
1127 parent_dir, final_component ));
1129 /* cd into the parent dir to pin it. */
1130 ret = vfs_ChDir(conn, parent_dir);
1132 saved_errno = errno;
1136 ZERO_STRUCT(local_fname);
1137 local_fname.base_name = discard_const_p(char, final_component);
1139 /* Must use lstat here. */
1140 ret = SMB_VFS_LSTAT(conn, &local_fname);
1142 saved_errno = errno;
1146 /* Ensure we have this file open with DELETE access. */
1147 id = vfs_file_id_from_sbuf(conn, &local_fname.st);
1148 for (fsp = file_find_di_first(conn->sconn, id); fsp;
1149 fsp = file_find_di_next(fsp)) {
1150 if (fsp->access_mask & DELETE_ACCESS &&
1151 fsp->delete_on_close) {
1152 /* We did open this for delete,
1153 * allow the delete as root.
1160 DEBUG(10,("acl_common_remove_object: %s %s/%s "
1161 "not an open file\n",
1162 is_directory ? "directory" : "file",
1163 parent_dir, final_component ));
1164 saved_errno = EACCES;
1170 ret = SMB_VFS_NEXT_RMDIR(handle, &local_fname);
1172 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
1177 saved_errno = errno;
1182 TALLOC_FREE(parent_dir);
1185 vfs_ChDir(conn, saved_dir);
1188 errno = saved_errno;
1193 static int rmdir_acl_common(struct vfs_handle_struct *handle,
1194 const struct smb_filename *smb_fname)
1198 /* Try the normal rmdir first. */
1199 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1203 if (errno == EACCES || errno == EPERM) {
1204 /* Failed due to access denied,
1205 see if we need to root override. */
1206 return acl_common_remove_object(handle,
1207 smb_fname->base_name,
1211 DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
1212 smb_fname->base_name,
1217 static int unlink_acl_common(struct vfs_handle_struct *handle,
1218 const struct smb_filename *smb_fname)
1222 /* Try the normal unlink first. */
1223 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1227 if (errno == EACCES || errno == EPERM) {
1228 /* Failed due to access denied,
1229 see if we need to root override. */
1231 /* Don't do anything fancy for streams. */
1232 if (smb_fname->stream_name) {
1235 return acl_common_remove_object(handle,
1236 smb_fname->base_name,
1240 DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
1241 smb_fname->base_name,
1246 static int chmod_acl_module_common(struct vfs_handle_struct *handle,
1247 const struct smb_filename *smb_fname,
1250 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1251 /* Only allow this on POSIX pathnames. */
1252 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1257 static int fchmod_acl_module_common(struct vfs_handle_struct *handle,
1258 struct files_struct *fsp, mode_t mode)
1260 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1261 /* Only allow this on POSIX opens. */
1262 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1267 static int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1268 const struct smb_filename *smb_fname,
1271 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1272 /* Only allow this on POSIX pathnames. */
1273 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
1278 static int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1279 struct files_struct *fsp, mode_t mode)
1281 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1282 /* Only allow this on POSIX opens. */
1283 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);