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 bool init_acl_common_config(vfs_handle_struct *handle,
45 const char *module_name)
47 struct acl_common_config *config = NULL;
48 const struct enum_list *default_acl_style_list = NULL;
50 default_acl_style_list = get_default_acl_style_list();
52 config = talloc_zero(handle->conn, struct acl_common_config);
54 DBG_ERR("talloc_zero() failed\n");
59 config->ignore_system_acls = lp_parm_bool(SNUM(handle->conn),
63 config->default_acl_style = lp_parm_enum(SNUM(handle->conn),
66 default_acl_style_list,
69 SMB_VFS_HANDLE_SET_DATA(handle, config, NULL,
70 struct acl_common_config,
77 /*******************************************************************
78 Hash a security descriptor.
79 *******************************************************************/
81 static NTSTATUS hash_blob_sha256(DATA_BLOB blob,
86 memset(hash, '\0', XATTR_SD_HASH_SIZE);
88 samba_SHA256_Init(&tctx);
89 samba_SHA256_Update(&tctx, blob.data, blob.length);
90 samba_SHA256_Final(hash, &tctx);
95 /*******************************************************************
96 Hash a security descriptor.
97 *******************************************************************/
99 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
105 memset(hash, '\0', XATTR_SD_HASH_SIZE);
106 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
107 if (!NT_STATUS_IS_OK(status)) {
110 return hash_blob_sha256(blob, hash);
113 /*******************************************************************
114 Parse out a struct security_descriptor from a DATA_BLOB.
115 *******************************************************************/
117 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
119 struct security_descriptor **ppdesc,
120 uint16_t *p_hash_type,
122 uint8_t hash[XATTR_SD_HASH_SIZE],
123 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
125 struct xattr_NTACL xacl;
126 enum ndr_err_code ndr_err;
128 TALLOC_CTX *frame = talloc_stackframe();
130 ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
131 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
133 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
134 DBG_INFO("ndr_pull_xattr_NTACL failed: %s\n",
135 ndr_errstr(ndr_err));
137 return ndr_map_error2ntstatus(ndr_err);
140 *p_version = xacl.version;
142 switch (xacl.version) {
144 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
145 xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
146 xacl.info.sd->owner_sid,
147 xacl.info.sd->group_sid,
151 /* No hash - null out. */
152 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
153 memset(hash, '\0', XATTR_SD_HASH_SIZE);
156 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
157 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
158 xacl.info.sd_hs2->sd->owner_sid,
159 xacl.info.sd_hs2->sd->group_sid,
160 xacl.info.sd_hs2->sd->sacl,
161 xacl.info.sd_hs2->sd->dacl,
163 /* No hash - null out. */
164 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
165 memset(hash, '\0', XATTR_SD_HASH_SIZE);
168 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
169 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
170 xacl.info.sd_hs3->sd->owner_sid,
171 xacl.info.sd_hs3->sd->group_sid,
172 xacl.info.sd_hs3->sd->sacl,
173 xacl.info.sd_hs3->sd->dacl,
175 *p_hash_type = xacl.info.sd_hs3->hash_type;
176 /* Current version 3 (if no sys acl hash available). */
177 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
180 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
181 xacl.info.sd_hs4->sd->type | SEC_DESC_SELF_RELATIVE,
182 xacl.info.sd_hs4->sd->owner_sid,
183 xacl.info.sd_hs4->sd->group_sid,
184 xacl.info.sd_hs4->sd->sacl,
185 xacl.info.sd_hs4->sd->dacl,
187 *p_hash_type = xacl.info.sd_hs4->hash_type;
188 /* Current version 4. */
189 memcpy(hash, xacl.info.sd_hs4->hash, XATTR_SD_HASH_SIZE);
190 memcpy(sys_acl_hash, xacl.info.sd_hs4->sys_acl_hash, XATTR_SD_HASH_SIZE);
194 return NT_STATUS_REVISION_MISMATCH;
199 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
202 /*******************************************************************
203 Create a DATA_BLOB from a hash of the security descriptor storead at
204 the system layer and the NT ACL we wish to preserve
205 *******************************************************************/
207 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
210 uint8_t hash[XATTR_SD_HASH_SIZE])
212 struct xattr_NTACL xacl;
213 struct security_descriptor_hash_v3 sd_hs3;
214 enum ndr_err_code ndr_err;
215 TALLOC_CTX *ctx = talloc_tos();
221 xacl.info.sd_hs3 = &sd_hs3;
222 xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
223 xacl.info.sd_hs3->hash_type = hash_type;
224 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
226 ndr_err = ndr_push_struct_blob(
228 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
230 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
231 DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
232 ndr_errstr(ndr_err));
233 return ndr_map_error2ntstatus(ndr_err);
239 /*******************************************************************
240 Create a DATA_BLOB from a hash of the security descriptors
241 (system and NT) stored at the system layer and the NT ACL we wish
243 *******************************************************************/
245 static NTSTATUS create_sys_acl_blob(const struct security_descriptor *psd,
248 uint8_t hash[XATTR_SD_HASH_SIZE],
249 const char *description,
250 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
252 struct xattr_NTACL xacl;
253 struct security_descriptor_hash_v4 sd_hs4;
254 enum ndr_err_code ndr_err;
255 TALLOC_CTX *ctx = talloc_tos();
257 struct timeval now = timeval_current();
258 nttime_now = timeval_to_nttime(&now);
264 xacl.info.sd_hs4 = &sd_hs4;
265 xacl.info.sd_hs4->sd = discard_const_p(struct security_descriptor, psd);
266 xacl.info.sd_hs4->hash_type = hash_type;
267 memcpy(&xacl.info.sd_hs4->hash[0], hash, XATTR_SD_HASH_SIZE);
268 xacl.info.sd_hs4->description = description;
269 xacl.info.sd_hs4->time = nttime_now;
270 memcpy(&xacl.info.sd_hs4->sys_acl_hash[0], sys_acl_hash, XATTR_SD_HASH_SIZE);
272 ndr_err = ndr_push_struct_blob(
274 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
276 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
277 DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
278 ndr_errstr(ndr_err));
279 return ndr_map_error2ntstatus(ndr_err);
285 /*******************************************************************
286 Add in 3 inheritable components for a non-inheritable directory ACL.
287 CREATOR_OWNER/CREATOR_GROUP/WORLD.
288 *******************************************************************/
290 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
292 SMB_STRUCT_STAT *psbuf,
293 struct security_descriptor *psd)
295 struct connection_struct *conn = handle->conn;
296 int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
297 struct smb_filename smb_fname;
298 enum security_ace_type acltype;
299 uint32_t access_mask;
303 struct security_ace *new_ace_list;
306 new_ace_list = talloc_zero_array(psd->dacl,
311 * make_sec_acl() at the bottom of this function
312 * dupliates new_ace_list
314 new_ace_list = talloc_zero_array(talloc_tos(),
319 if (new_ace_list == NULL) {
320 return NT_STATUS_NO_MEMORY;
323 /* Fake a quick smb_filename. */
324 ZERO_STRUCT(smb_fname);
325 smb_fname.st = *psbuf;
326 smb_fname.base_name = discard_const_p(char, name);
328 dir_mode = unix_mode(conn,
329 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
330 file_mode = unix_mode(conn,
331 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
333 mode = dir_mode | file_mode;
335 DBG_DEBUG("directory %s, mode = 0%o\n", name, (unsigned int)mode);
338 memcpy(new_ace_list, psd->dacl->aces,
339 num_aces * sizeof(struct security_ace));
341 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
344 init_sec_ace(&new_ace_list[num_aces],
345 &global_sid_Creator_Owner,
348 SEC_ACE_FLAG_CONTAINER_INHERIT|
349 SEC_ACE_FLAG_OBJECT_INHERIT|
350 SEC_ACE_FLAG_INHERIT_ONLY);
351 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
352 (mode << 3) & 0700, false);
353 init_sec_ace(&new_ace_list[num_aces+1],
354 &global_sid_Creator_Group,
357 SEC_ACE_FLAG_CONTAINER_INHERIT|
358 SEC_ACE_FLAG_OBJECT_INHERIT|
359 SEC_ACE_FLAG_INHERIT_ONLY);
360 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
361 (mode << 6) & 0700, false);
362 init_sec_ace(&new_ace_list[num_aces+2],
366 SEC_ACE_FLAG_CONTAINER_INHERIT|
367 SEC_ACE_FLAG_OBJECT_INHERIT|
368 SEC_ACE_FLAG_INHERIT_ONLY);
370 psd->dacl->aces = new_ace_list;
371 psd->dacl->num_aces += 3;
372 psd->dacl->size += new_ace_list[num_aces].size +
373 new_ace_list[num_aces+1].size +
374 new_ace_list[num_aces+2].size;
376 psd->dacl = make_sec_acl(psd,
380 if (psd->dacl == NULL) {
381 return NT_STATUS_NO_MEMORY;
388 * Validate an ACL blob
390 * This validates an ACL blob against the underlying filesystem ACL. If this
391 * function returns NT_STATUS_OK ppsd can be
393 * 1. the ACL from the blob (psd_from_fs=false), or
394 * 2. the ACL from the fs (psd_from_fs=true), or
397 * If the return value is anything else then NT_STATUS_OK, ppsd is set to NULL
398 * and psd_from_fs set to false.
400 * Returning the underlying filesystem ACL in case no. 2 is really just an
401 * optimisation, because some validations have to fetch the filesytem ACL as
402 * part of the validation, so we already have it available and callers might
405 static NTSTATUS validate_nt_acl_blob(TALLOC_CTX *mem_ctx,
406 vfs_handle_struct *handle,
408 const struct smb_filename *smb_fname,
409 const DATA_BLOB *blob,
410 struct security_descriptor **ppsd,
411 bool *psd_is_from_fs)
414 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
415 uint16_t xattr_version = 0;
416 uint8_t hash[XATTR_SD_HASH_SIZE];
417 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
418 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
419 uint8_t sys_acl_hash_tmp[XATTR_SD_HASH_SIZE];
420 struct security_descriptor *psd = NULL;
421 struct security_descriptor *psd_blob = NULL;
422 struct security_descriptor *psd_fs = NULL;
423 char *sys_acl_blob_description = NULL;
424 DATA_BLOB sys_acl_blob = { 0 };
425 struct acl_common_config *config = NULL;
428 *psd_is_from_fs = false;
430 SMB_VFS_HANDLE_GET_DATA(handle, config,
431 struct acl_common_config,
432 return NT_STATUS_UNSUCCESSFUL);
434 status = parse_acl_blob(blob,
441 if (!NT_STATUS_IS_OK(status)) {
442 DBG_DEBUG("parse_acl_blob returned %s\n", nt_errstr(status));
446 /* determine which type of xattr we got */
447 switch (xattr_version) {
450 /* These xattr types are unilatteral, they do not
451 * require confirmation of the hash. In particular,
452 * the NTVFS file server uses version 1, but
453 * 'samba-tool ntacl' can set these as well */
458 if (config->ignore_system_acls) {
465 DBG_DEBUG("ACL blob revision mismatch (%u) for file %s\n",
466 (unsigned int)hash_type, smb_fname->base_name);
467 TALLOC_FREE(psd_blob);
471 /* determine which type of xattr we got */
472 if (hash_type != XATTR_SD_HASH_TYPE_SHA256) {
473 DBG_DEBUG("ACL blob hash type (%u) unexpected for file %s\n",
474 (unsigned int)hash_type, smb_fname->base_name);
475 TALLOC_FREE(psd_blob);
479 /* determine which type of xattr we got */
480 switch (xattr_version) {
485 /* Get the full underlying sd, then hash. */
486 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
489 &sys_acl_blob_description,
492 /* Get the full underlying sd, then hash. */
493 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle,
496 &sys_acl_blob_description,
500 /* If we fail to get the ACL blob (for some reason) then this
501 * is not fatal, we just work based on the NT ACL only */
503 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash_tmp);
504 if (!NT_STATUS_IS_OK(status)) {
508 TALLOC_FREE(sys_acl_blob_description);
509 TALLOC_FREE(sys_acl_blob.data);
511 if (memcmp(&sys_acl_hash[0], &sys_acl_hash_tmp[0],
512 XATTR_SD_HASH_SIZE) == 0) {
513 /* Hash matches, return blob sd. */
514 DBG_DEBUG("blob hash matches for file %s\n",
515 smb_fname->base_name);
521 /* Otherwise, fall though and see if the NT ACL hash matches */
525 /* Get the full underlying sd for the hash
526 or to return as backup. */
528 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
534 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
541 if (!NT_STATUS_IS_OK(status)) {
542 DBG_DEBUG("get_next_acl for file %s returned %s\n",
543 smb_fname->base_name, nt_errstr(status));
547 status = hash_sd_sha256(psd_fs, hash_tmp);
548 if (!NT_STATUS_IS_OK(status)) {
549 TALLOC_FREE(psd_blob);
551 *psd_is_from_fs = true;
555 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
556 /* Hash matches, return blob sd. */
557 DBG_DEBUG("blob hash matches for file %s\n",
558 smb_fname->base_name);
563 /* Hash doesn't match, return underlying sd. */
564 DBG_DEBUG("blob hash does not match for file %s - returning "
565 "file system SD mapping.\n",
566 smb_fname->base_name);
568 if (DEBUGLEVEL >= 10) {
569 DBG_DEBUG("acl for blob hash for %s is:\n",
570 smb_fname->base_name);
571 NDR_PRINT_DEBUG(security_descriptor, psd_fs);
574 TALLOC_FREE(psd_blob);
576 *psd_is_from_fs = true;
583 TALLOC_FREE(psd_blob);
585 TALLOC_FREE(sys_acl_blob_description);
586 TALLOC_FREE(sys_acl_blob.data);
590 static NTSTATUS stat_fsp_or_smb_fname(vfs_handle_struct *handle,
592 const struct smb_filename *smb_fname,
593 SMB_STRUCT_STAT *sbuf,
594 SMB_STRUCT_STAT **psbuf)
600 status = vfs_stat_fsp(fsp);
601 if (!NT_STATUS_IS_OK(status)) {
604 *psbuf = &fsp->fsp_name->st;
607 * https://bugzilla.samba.org/show_bug.cgi?id=11249
609 * We are currently guaranteed that 'name' here is a
610 * smb_fname->base_name, which *cannot* contain a stream name
611 * (':'). vfs_stat_smb_fname() splits a name into a base name +
612 * stream name, which when we get here we know we've already
613 * done. So we have to call the stat or lstat VFS calls
614 * directly here. Else, a base_name that contains a ':' (from a
615 * demangled name) will get split again.
618 * This uglyness will go away once smb_fname is fully plumbed
621 ret = vfs_stat_smb_basename(handle->conn,
625 return map_nt_error_from_unix(errno);
632 /*******************************************************************
633 Pull a DATA_BLOB from an xattr given a pathname.
634 If the hash doesn't match, or doesn't exist - return the underlying
636 *******************************************************************/
638 NTSTATUS get_nt_acl_common(
639 NTSTATUS (*get_acl_blob_fn)(TALLOC_CTX *ctx,
640 vfs_handle_struct *handle,
642 const struct smb_filename *smb_fname,
644 vfs_handle_struct *handle,
646 const struct smb_filename *smb_fname_in,
647 uint32_t security_info,
649 struct security_descriptor **ppdesc)
651 DATA_BLOB blob = data_blob_null;
653 struct security_descriptor *psd = NULL;
654 const struct smb_filename *smb_fname = NULL;
655 bool psd_is_from_fs = false;
656 struct acl_common_config *config = NULL;
658 SMB_VFS_HANDLE_GET_DATA(handle, config,
659 struct acl_common_config,
660 return NT_STATUS_UNSUCCESSFUL);
662 if (fsp && smb_fname_in == NULL) {
663 smb_fname = fsp->fsp_name;
665 smb_fname = smb_fname_in;
668 DBG_DEBUG("name=%s\n", smb_fname->base_name);
670 status = get_acl_blob_fn(mem_ctx, handle, fsp, smb_fname, &blob);
671 if (NT_STATUS_IS_OK(status)) {
672 status = validate_nt_acl_blob(mem_ctx,
679 TALLOC_FREE(blob.data);
680 if (!NT_STATUS_IS_OK(status)) {
681 DBG_DEBUG("ACL validation for [%s] failed\n",
682 smb_fname->base_name);
688 /* Get the full underlying sd, as we failed to get the
689 * blob for the hash, or the revision/hash type wasn't
692 if (config->ignore_system_acls) {
693 SMB_STRUCT_STAT sbuf;
694 SMB_STRUCT_STAT *psbuf = &sbuf;
696 status = stat_fsp_or_smb_fname(handle, fsp, smb_fname,
698 if (!NT_STATUS_IS_OK(status)) {
702 status = make_default_filesystem_acl(
704 config->default_acl_style,
705 smb_fname->base_name,
708 if (!NT_STATUS_IS_OK(status)) {
713 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
719 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
726 if (!NT_STATUS_IS_OK(status)) {
727 DBG_DEBUG("get_next_acl for file %s "
729 smb_fname->base_name,
734 psd_is_from_fs = true;
738 if (psd_is_from_fs) {
739 SMB_STRUCT_STAT sbuf;
740 SMB_STRUCT_STAT *psbuf = &sbuf;
741 bool is_directory = false;
744 * We're returning the underlying ACL from the
745 * filesystem. If it's a directory, and has no
746 * inheritable ACE entries we have to fake them.
749 status = stat_fsp_or_smb_fname(handle, fsp, smb_fname,
751 if (!NT_STATUS_IS_OK(status)) {
755 is_directory = S_ISDIR(psbuf->st_ex_mode);
757 if (is_directory && !sd_has_inheritable_components(psd, true)) {
758 status = add_directory_inheritable_components(
760 smb_fname->base_name,
763 if (!NT_STATUS_IS_OK(status)) {
769 * The underlying POSIX module always sets the
770 * ~SEC_DESC_DACL_PROTECTED bit, as ACLs can't be inherited in
771 * this way under POSIX. Remove it for Windows-style ACLs.
773 psd->type &= ~SEC_DESC_DACL_PROTECTED;
776 if (!(security_info & SECINFO_OWNER)) {
777 psd->owner_sid = NULL;
779 if (!(security_info & SECINFO_GROUP)) {
780 psd->group_sid = NULL;
782 if (!(security_info & SECINFO_DACL)) {
783 psd->type &= ~SEC_DESC_DACL_PRESENT;
786 if (!(security_info & SECINFO_SACL)) {
787 psd->type &= ~SEC_DESC_SACL_PRESENT;
791 if (DEBUGLEVEL >= 10) {
792 DBG_DEBUG("returning acl for %s is:\n",
793 smb_fname->base_name);
794 NDR_PRINT_DEBUG(security_descriptor, psd);
806 /*********************************************************************
807 Set the underlying ACL (e.g. POSIX ACLS, POSIX owner, etc)
808 *********************************************************************/
809 static NTSTATUS set_underlying_acl(vfs_handle_struct *handle, files_struct *fsp,
810 struct security_descriptor *psd,
811 uint32_t security_info_sent,
815 const struct security_token *token = NULL;
817 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
818 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
822 /* We got access denied here. If we're already root,
823 or we didn't need to do a chown, or the fsp isn't
824 open with WRITE_OWNER access, just return. */
825 if (get_current_uid(handle->conn) == 0 || chown_needed == false ||
826 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
827 return NT_STATUS_ACCESS_DENIED;
831 * Only allow take-ownership, not give-ownership. That's the way Windows
832 * implements SEC_STD_WRITE_OWNER. MS-FSA 2.1.5.16 just states: If
833 * InputBuffer.OwnerSid is not a valid owner SID for a file in the
834 * objectstore, as determined in an implementation specific manner, the
835 * object store MUST return STATUS_INVALID_OWNER.
837 token = get_current_nttok(fsp->conn);
838 if (!security_token_is_sid(token, psd->owner_sid)) {
839 return NT_STATUS_INVALID_OWNER;
842 DBG_DEBUG("overriding chown on file %s for sid %s\n",
843 fsp_str_dbg(fsp), sid_string_tos(psd->owner_sid));
845 /* Ok, we failed to chown and we have
846 SEC_STD_WRITE_OWNER access - override. */
848 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
854 /*********************************************************************
855 Store a v3 security descriptor
856 *********************************************************************/
857 static NTSTATUS store_v3_blob(
858 NTSTATUS (*store_acl_blob_fsp_fn)(vfs_handle_struct *handle,
861 vfs_handle_struct *handle, files_struct *fsp,
862 struct security_descriptor *psd,
863 struct security_descriptor *pdesc_next,
864 uint8_t hash[XATTR_SD_HASH_SIZE])
869 if (DEBUGLEVEL >= 10) {
870 DBG_DEBUG("storing xattr sd for file %s\n",
874 discard_const_p(struct security_descriptor, psd));
876 if (pdesc_next != NULL) {
877 DBG_DEBUG("storing xattr sd based on \n");
880 discard_const_p(struct security_descriptor,
883 DBG_DEBUG("ignoring underlying sd\n");
886 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
887 if (!NT_STATUS_IS_OK(status)) {
888 DBG_DEBUG("create_acl_blob failed\n");
892 status = store_acl_blob_fsp_fn(handle, fsp, &blob);
896 /*********************************************************************
897 Store a security descriptor given an fsp.
898 *********************************************************************/
900 NTSTATUS fset_nt_acl_common(
901 NTSTATUS (*get_acl_blob_fn)(TALLOC_CTX *ctx,
902 vfs_handle_struct *handle,
904 const struct smb_filename *smb_fname,
906 NTSTATUS (*store_acl_blob_fsp_fn)(vfs_handle_struct *handle,
909 const char *module_name,
910 vfs_handle_struct *handle, files_struct *fsp,
911 uint32_t security_info_sent,
912 const struct security_descriptor *orig_psd)
916 DATA_BLOB blob, sys_acl_blob;
917 struct security_descriptor *pdesc_next = NULL;
918 struct security_descriptor *psd = NULL;
919 uint8_t hash[XATTR_SD_HASH_SIZE];
920 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
921 bool chown_needed = false;
922 char *sys_acl_description;
923 TALLOC_CTX *frame = talloc_stackframe();
924 bool ignore_file_system_acl = lp_parm_bool(
925 SNUM(handle->conn), module_name, "ignore system acls", false);
927 if (DEBUGLEVEL >= 10) {
928 DBG_DEBUG("incoming sd for file %s\n", fsp_str_dbg(fsp));
929 NDR_PRINT_DEBUG(security_descriptor,
930 discard_const_p(struct security_descriptor, orig_psd));
933 status = get_nt_acl_common(get_acl_blob_fn, handle, fsp,
935 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
939 if (!NT_STATUS_IS_OK(status)) {
944 psd->revision = orig_psd->revision;
945 if (security_info_sent & SECINFO_DACL) {
946 psd->type = orig_psd->type;
947 /* All our SD's are self relative. */
948 psd->type |= SEC_DESC_SELF_RELATIVE;
951 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
952 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
953 /* We're changing the owner. */
956 psd->owner_sid = orig_psd->owner_sid;
958 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
959 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
960 /* We're changing the group. */
963 psd->group_sid = orig_psd->group_sid;
965 if (security_info_sent & SECINFO_DACL) {
966 if (security_descriptor_with_ms_nfs(orig_psd)) {
968 * If the sd contains a MS NFS SID, do
969 * nothing, it's a chmod() request from OS X
975 psd->dacl = orig_psd->dacl;
976 psd->type |= SEC_DESC_DACL_PRESENT;
978 if (security_info_sent & SECINFO_SACL) {
979 psd->sacl = orig_psd->sacl;
980 psd->type |= SEC_DESC_SACL_PRESENT;
983 if (ignore_file_system_acl) {
985 /* send only ownership stuff to lower layer */
986 security_info_sent &= (SECINFO_OWNER | SECINFO_GROUP);
987 status = set_underlying_acl(handle, fsp, psd,
988 security_info_sent, true);
989 if (!NT_STATUS_IS_OK(status)) {
995 status = store_v3_blob(store_acl_blob_fsp_fn, handle, fsp, psd,
1002 status = set_underlying_acl(handle, fsp, psd, security_info_sent,
1004 if (!NT_STATUS_IS_OK(status)) {
1009 /* Get the full underlying sd, then hash. */
1010 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
1016 if (!NT_STATUS_IS_OK(status)) {
1021 status = hash_sd_sha256(pdesc_next, hash);
1022 if (!NT_STATUS_IS_OK(status)) {
1027 /* Get the full underlying sd, then hash. */
1028 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
1031 &sys_acl_description,
1034 /* If we fail to get the ACL blob (for some reason) then this
1035 * is not fatal, we just work based on the NT ACL only */
1037 status = store_v3_blob(store_acl_blob_fsp_fn, handle, fsp, psd,
1044 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash);
1045 if (!NT_STATUS_IS_OK(status)) {
1050 if (DEBUGLEVEL >= 10) {
1051 DBG_DEBUG("storing xattr sd for file %s based on system ACL\n",
1053 NDR_PRINT_DEBUG(security_descriptor,
1054 discard_const_p(struct security_descriptor, psd));
1056 DBG_DEBUG("storing hash in xattr sd based on system ACL and:\n");
1057 NDR_PRINT_DEBUG(security_descriptor,
1058 discard_const_p(struct security_descriptor, pdesc_next));
1061 /* We store hashes of both the sys ACL blob and the NT
1062 * security desciptor mapped from that ACL so as to improve
1063 * our chances against some inadvertant change breaking the
1065 status = create_sys_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash,
1066 sys_acl_description, sys_acl_hash);
1067 if (!NT_STATUS_IS_OK(status)) {
1068 DBG_DEBUG("create_sys_acl_blob failed\n");
1073 status = store_acl_blob_fsp_fn(handle, fsp, &blob);
1079 static int acl_common_remove_object(vfs_handle_struct *handle,
1080 const struct smb_filename *smb_fname,
1083 connection_struct *conn = handle->conn;
1085 files_struct *fsp = NULL;
1087 char *parent_dir = NULL;
1088 const char *final_component = NULL;
1089 struct smb_filename local_fname = {0};
1090 struct smb_filename parent_dir_fname = {0};
1091 int saved_errno = 0;
1092 struct smb_filename *saved_dir_fname = NULL;
1094 saved_dir_fname = vfs_GetWd(talloc_tos(),conn);
1095 if (saved_dir_fname == NULL) {
1096 saved_errno = errno;
1100 if (!parent_dirname(talloc_tos(), smb_fname->base_name,
1101 &parent_dir, &final_component)) {
1102 saved_errno = ENOMEM;
1106 DBG_DEBUG("removing %s %s/%s\n", is_directory ? "directory" : "file",
1107 parent_dir, final_component);
1109 parent_dir_fname = (struct smb_filename) { .base_name = parent_dir };
1111 /* cd into the parent dir to pin it. */
1112 ret = vfs_ChDir(conn, &parent_dir_fname);
1114 saved_errno = errno;
1118 local_fname.base_name = discard_const_p(char, final_component);
1120 /* Must use lstat here. */
1121 ret = SMB_VFS_LSTAT(conn, &local_fname);
1123 saved_errno = errno;
1127 /* Ensure we have this file open with DELETE access. */
1128 id = vfs_file_id_from_sbuf(conn, &local_fname.st);
1129 for (fsp = file_find_di_first(conn->sconn, id); fsp;
1130 fsp = file_find_di_next(fsp)) {
1131 if (fsp->access_mask & DELETE_ACCESS &&
1132 fsp->delete_on_close) {
1133 /* We did open this for delete,
1134 * allow the delete as root.
1141 DBG_DEBUG("%s %s/%s not an open file\n",
1142 is_directory ? "directory" : "file",
1143 parent_dir, final_component);
1144 saved_errno = EACCES;
1150 ret = SMB_VFS_NEXT_RMDIR(handle, &local_fname);
1152 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
1157 saved_errno = errno;
1162 TALLOC_FREE(parent_dir);
1164 if (saved_dir_fname) {
1165 vfs_ChDir(conn, saved_dir_fname);
1166 TALLOC_FREE(saved_dir_fname);
1169 errno = saved_errno;
1174 int rmdir_acl_common(struct vfs_handle_struct *handle,
1175 const struct smb_filename *smb_fname)
1179 /* Try the normal rmdir first. */
1180 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1184 if (errno == EACCES || errno == EPERM) {
1185 /* Failed due to access denied,
1186 see if we need to root override. */
1187 return acl_common_remove_object(handle,
1192 DBG_DEBUG("unlink of %s failed %s\n",
1193 smb_fname->base_name,
1198 int unlink_acl_common(struct vfs_handle_struct *handle,
1199 const struct smb_filename *smb_fname)
1203 /* Try the normal unlink first. */
1204 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1208 if (errno == EACCES || errno == EPERM) {
1209 /* Failed due to access denied,
1210 see if we need to root override. */
1212 /* Don't do anything fancy for streams. */
1213 if (smb_fname->stream_name) {
1216 return acl_common_remove_object(handle,
1221 DBG_DEBUG("unlink of %s failed %s\n",
1222 smb_fname->base_name,
1227 int chmod_acl_module_common(struct vfs_handle_struct *handle,
1228 const struct smb_filename *smb_fname,
1231 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1232 /* Only allow this on POSIX pathnames. */
1233 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1238 int fchmod_acl_module_common(struct vfs_handle_struct *handle,
1239 struct files_struct *fsp, mode_t mode)
1241 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1242 /* Only allow this on POSIX opens. */
1243 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1248 int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1249 struct files_struct *fsp, mode_t mode)
1251 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1252 /* Only allow this on POSIX opens. */
1253 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);