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
;
816 struct dom_sid_buf buf
;
818 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
819 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
823 /* We got access denied here. If we're already root,
824 or we didn't need to do a chown, or the fsp isn't
825 open with WRITE_OWNER access, just return. */
826 if (get_current_uid(handle
->conn
) == 0 || chown_needed
== false ||
827 !(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
828 return NT_STATUS_ACCESS_DENIED
;
832 * Only allow take-ownership, not give-ownership. That's the way Windows
833 * implements SEC_STD_WRITE_OWNER. MS-FSA 2.1.5.16 just states: If
834 * InputBuffer.OwnerSid is not a valid owner SID for a file in the
835 * objectstore, as determined in an implementation specific manner, the
836 * object store MUST return STATUS_INVALID_OWNER.
838 token
= get_current_nttok(fsp
->conn
);
839 if (!security_token_is_sid(token
, psd
->owner_sid
)) {
840 return NT_STATUS_INVALID_OWNER
;
843 DBG_DEBUG("overriding chown on file %s for sid %s\n",
845 dom_sid_str_buf(psd
->owner_sid
, &buf
));
847 /* Ok, we failed to chown and we have
848 SEC_STD_WRITE_OWNER access - override. */
850 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
856 /*********************************************************************
857 Store a v3 security descriptor
858 *********************************************************************/
859 static NTSTATUS
store_v3_blob(
860 NTSTATUS (*store_acl_blob_fsp_fn
)(vfs_handle_struct
*handle
,
863 vfs_handle_struct
*handle
, files_struct
*fsp
,
864 struct security_descriptor
*psd
,
865 struct security_descriptor
*pdesc_next
,
866 uint8_t hash
[XATTR_SD_HASH_SIZE
])
871 if (DEBUGLEVEL
>= 10) {
872 DBG_DEBUG("storing xattr sd for file %s\n",
876 discard_const_p(struct security_descriptor
, psd
));
878 if (pdesc_next
!= NULL
) {
879 DBG_DEBUG("storing xattr sd based on \n");
882 discard_const_p(struct security_descriptor
,
885 DBG_DEBUG("ignoring underlying sd\n");
888 status
= create_acl_blob(psd
, &blob
, XATTR_SD_HASH_TYPE_SHA256
, hash
);
889 if (!NT_STATUS_IS_OK(status
)) {
890 DBG_DEBUG("create_acl_blob failed\n");
894 status
= store_acl_blob_fsp_fn(handle
, fsp
, &blob
);
898 /*********************************************************************
899 Store a security descriptor given an fsp.
900 *********************************************************************/
902 NTSTATUS
fset_nt_acl_common(
903 NTSTATUS (*get_acl_blob_fn
)(TALLOC_CTX
*ctx
,
904 vfs_handle_struct
*handle
,
906 const struct smb_filename
*smb_fname
,
908 NTSTATUS (*store_acl_blob_fsp_fn
)(vfs_handle_struct
*handle
,
911 const char *module_name
,
912 vfs_handle_struct
*handle
, files_struct
*fsp
,
913 uint32_t security_info_sent
,
914 const struct security_descriptor
*orig_psd
)
918 DATA_BLOB blob
, sys_acl_blob
;
919 struct security_descriptor
*pdesc_next
= NULL
;
920 struct security_descriptor
*psd
= NULL
;
921 uint8_t hash
[XATTR_SD_HASH_SIZE
];
922 uint8_t sys_acl_hash
[XATTR_SD_HASH_SIZE
];
923 bool chown_needed
= false;
924 char *sys_acl_description
;
925 TALLOC_CTX
*frame
= talloc_stackframe();
926 bool ignore_file_system_acl
= lp_parm_bool(
927 SNUM(handle
->conn
), module_name
, "ignore system acls", false);
929 if (DEBUGLEVEL
>= 10) {
930 DBG_DEBUG("incoming sd for file %s\n", fsp_str_dbg(fsp
));
931 NDR_PRINT_DEBUG(security_descriptor
,
932 discard_const_p(struct security_descriptor
, orig_psd
));
935 status
= get_nt_acl_common(get_acl_blob_fn
, handle
, fsp
,
937 SECINFO_OWNER
|SECINFO_GROUP
|SECINFO_DACL
|SECINFO_SACL
,
941 if (!NT_STATUS_IS_OK(status
)) {
946 psd
->revision
= orig_psd
->revision
;
947 if (security_info_sent
& SECINFO_DACL
) {
948 psd
->type
= orig_psd
->type
;
949 /* All our SD's are self relative. */
950 psd
->type
|= SEC_DESC_SELF_RELATIVE
;
953 if ((security_info_sent
& SECINFO_OWNER
) && (orig_psd
->owner_sid
!= NULL
)) {
954 if (!dom_sid_equal(orig_psd
->owner_sid
, psd
->owner_sid
)) {
955 /* We're changing the owner. */
958 psd
->owner_sid
= orig_psd
->owner_sid
;
960 if ((security_info_sent
& SECINFO_GROUP
) && (orig_psd
->group_sid
!= NULL
)) {
961 if (!dom_sid_equal(orig_psd
->group_sid
, psd
->group_sid
)) {
962 /* We're changing the group. */
965 psd
->group_sid
= orig_psd
->group_sid
;
967 if (security_info_sent
& SECINFO_DACL
) {
968 if (security_descriptor_with_ms_nfs(orig_psd
)) {
970 * If the sd contains a MS NFS SID, do
971 * nothing, it's a chmod() request from OS X
977 psd
->dacl
= orig_psd
->dacl
;
978 psd
->type
|= SEC_DESC_DACL_PRESENT
;
980 if (security_info_sent
& SECINFO_SACL
) {
981 psd
->sacl
= orig_psd
->sacl
;
982 psd
->type
|= SEC_DESC_SACL_PRESENT
;
985 if (ignore_file_system_acl
) {
987 /* send only ownership stuff to lower layer */
988 security_info_sent
&= (SECINFO_OWNER
| SECINFO_GROUP
);
989 status
= set_underlying_acl(handle
, fsp
, psd
,
990 security_info_sent
, true);
991 if (!NT_STATUS_IS_OK(status
)) {
997 status
= store_v3_blob(store_acl_blob_fsp_fn
, handle
, fsp
, psd
,
1004 status
= set_underlying_acl(handle
, fsp
, psd
, security_info_sent
,
1006 if (!NT_STATUS_IS_OK(status
)) {
1011 /* Get the full underlying sd, then hash. */
1012 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
,
1018 if (!NT_STATUS_IS_OK(status
)) {
1023 status
= hash_sd_sha256(pdesc_next
, hash
);
1024 if (!NT_STATUS_IS_OK(status
)) {
1029 /* Get the full underlying sd, then hash. */
1030 ret
= SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle
,
1033 &sys_acl_description
,
1036 /* If we fail to get the ACL blob (for some reason) then this
1037 * is not fatal, we just work based on the NT ACL only */
1039 status
= store_v3_blob(store_acl_blob_fsp_fn
, handle
, fsp
, psd
,
1046 status
= hash_blob_sha256(sys_acl_blob
, sys_acl_hash
);
1047 if (!NT_STATUS_IS_OK(status
)) {
1052 if (DEBUGLEVEL
>= 10) {
1053 DBG_DEBUG("storing xattr sd for file %s based on system ACL\n",
1055 NDR_PRINT_DEBUG(security_descriptor
,
1056 discard_const_p(struct security_descriptor
, psd
));
1058 DBG_DEBUG("storing hash in xattr sd based on system ACL and:\n");
1059 NDR_PRINT_DEBUG(security_descriptor
,
1060 discard_const_p(struct security_descriptor
, pdesc_next
));
1063 /* We store hashes of both the sys ACL blob and the NT
1064 * security desciptor mapped from that ACL so as to improve
1065 * our chances against some inadvertant change breaking the
1067 status
= create_sys_acl_blob(psd
, &blob
, XATTR_SD_HASH_TYPE_SHA256
, hash
,
1068 sys_acl_description
, sys_acl_hash
);
1069 if (!NT_STATUS_IS_OK(status
)) {
1070 DBG_DEBUG("create_sys_acl_blob failed\n");
1075 status
= store_acl_blob_fsp_fn(handle
, fsp
, &blob
);
1081 static int acl_common_remove_object(vfs_handle_struct
*handle
,
1082 const struct smb_filename
*smb_fname
,
1085 connection_struct
*conn
= handle
->conn
;
1087 files_struct
*fsp
= NULL
;
1089 char *parent_dir
= NULL
;
1090 const char *final_component
= NULL
;
1091 struct smb_filename local_fname
= {0};
1092 struct smb_filename parent_dir_fname
= {0};
1093 int saved_errno
= 0;
1094 struct smb_filename
*saved_dir_fname
= NULL
;
1096 saved_dir_fname
= vfs_GetWd(talloc_tos(),conn
);
1097 if (saved_dir_fname
== NULL
) {
1098 saved_errno
= errno
;
1102 if (!parent_dirname(talloc_tos(), smb_fname
->base_name
,
1103 &parent_dir
, &final_component
)) {
1104 saved_errno
= ENOMEM
;
1108 DBG_DEBUG("removing %s %s/%s\n", is_directory
? "directory" : "file",
1109 parent_dir
, final_component
);
1111 parent_dir_fname
= (struct smb_filename
) { .base_name
= parent_dir
};
1113 /* cd into the parent dir to pin it. */
1114 ret
= vfs_ChDir(conn
, &parent_dir_fname
);
1116 saved_errno
= errno
;
1120 local_fname
.base_name
= discard_const_p(char, final_component
);
1122 /* Must use lstat here. */
1123 ret
= SMB_VFS_LSTAT(conn
, &local_fname
);
1125 saved_errno
= errno
;
1129 /* Ensure we have this file open with DELETE access. */
1130 id
= vfs_file_id_from_sbuf(conn
, &local_fname
.st
);
1131 for (fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1132 fsp
= file_find_di_next(fsp
)) {
1133 if (fsp
->access_mask
& DELETE_ACCESS
&&
1134 fsp
->delete_on_close
) {
1135 /* We did open this for delete,
1136 * allow the delete as root.
1143 DBG_DEBUG("%s %s/%s not an open file\n",
1144 is_directory
? "directory" : "file",
1145 parent_dir
, final_component
);
1146 saved_errno
= EACCES
;
1152 ret
= SMB_VFS_NEXT_RMDIR(handle
, &local_fname
);
1154 ret
= SMB_VFS_NEXT_UNLINK(handle
, &local_fname
);
1159 saved_errno
= errno
;
1164 TALLOC_FREE(parent_dir
);
1166 if (saved_dir_fname
) {
1167 vfs_ChDir(conn
, saved_dir_fname
);
1168 TALLOC_FREE(saved_dir_fname
);
1171 errno
= saved_errno
;
1176 int rmdir_acl_common(struct vfs_handle_struct
*handle
,
1177 const struct smb_filename
*smb_fname
)
1181 /* Try the normal rmdir first. */
1182 ret
= SMB_VFS_NEXT_RMDIR(handle
, smb_fname
);
1186 if (errno
== EACCES
|| errno
== EPERM
) {
1187 /* Failed due to access denied,
1188 see if we need to root override. */
1189 return acl_common_remove_object(handle
,
1194 DBG_DEBUG("unlink of %s failed %s\n",
1195 smb_fname
->base_name
,
1200 int unlink_acl_common(struct vfs_handle_struct
*handle
,
1201 const struct smb_filename
*smb_fname
)
1205 /* Try the normal unlink first. */
1206 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
1210 if (errno
== EACCES
|| errno
== EPERM
) {
1211 /* Failed due to access denied,
1212 see if we need to root override. */
1214 /* Don't do anything fancy for streams. */
1215 if (smb_fname
->stream_name
) {
1218 return acl_common_remove_object(handle
,
1223 DBG_DEBUG("unlink of %s failed %s\n",
1224 smb_fname
->base_name
,
1229 int chmod_acl_module_common(struct vfs_handle_struct
*handle
,
1230 const struct smb_filename
*smb_fname
,
1233 if (smb_fname
->flags
& SMB_FILENAME_POSIX_PATH
) {
1234 /* Only allow this on POSIX pathnames. */
1235 return SMB_VFS_NEXT_CHMOD(handle
, smb_fname
, mode
);
1240 int fchmod_acl_module_common(struct vfs_handle_struct
*handle
,
1241 struct files_struct
*fsp
, mode_t mode
)
1243 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
1244 /* Only allow this on POSIX opens. */
1245 return SMB_VFS_NEXT_FCHMOD(handle
, fsp
, mode
);