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
,
40 static NTSTATUS
store_acl_blob_fsp(vfs_handle_struct
*handle
,
44 #define HASH_SECURITY_INFO (SECINFO_OWNER | \
49 enum default_acl_style
{DEFAULT_ACL_POSIX
, DEFAULT_ACL_WINDOWS
};
51 static const struct enum_list default_acl_style
[] = {
52 {DEFAULT_ACL_POSIX
, "posix"},
53 {DEFAULT_ACL_WINDOWS
, "windows"}
56 struct acl_common_config
{
57 bool ignore_system_acls
;
58 enum default_acl_style default_acl_style
;
61 static bool init_acl_common_config(vfs_handle_struct
*handle
)
63 struct acl_common_config
*config
= NULL
;
65 config
= talloc_zero(handle
->conn
, struct acl_common_config
);
67 DBG_ERR("talloc_zero() failed\n");
72 config
->ignore_system_acls
= lp_parm_bool(SNUM(handle
->conn
),
76 config
->default_acl_style
= lp_parm_enum(SNUM(handle
->conn
),
82 SMB_VFS_HANDLE_SET_DATA(handle
, config
, NULL
,
83 struct acl_common_config
,
90 /*******************************************************************
91 Hash a security descriptor.
92 *******************************************************************/
94 static NTSTATUS
hash_blob_sha256(DATA_BLOB blob
,
99 memset(hash
, '\0', XATTR_SD_HASH_SIZE
);
101 samba_SHA256_Init(&tctx
);
102 samba_SHA256_Update(&tctx
, blob
.data
, blob
.length
);
103 samba_SHA256_Final(hash
, &tctx
);
108 /*******************************************************************
109 Hash a security descriptor.
110 *******************************************************************/
112 static NTSTATUS
hash_sd_sha256(struct security_descriptor
*psd
,
118 memset(hash
, '\0', XATTR_SD_HASH_SIZE
);
119 status
= create_acl_blob(psd
, &blob
, XATTR_SD_HASH_TYPE_SHA256
, hash
);
120 if (!NT_STATUS_IS_OK(status
)) {
123 return hash_blob_sha256(blob
, hash
);
126 /*******************************************************************
127 Parse out a struct security_descriptor from a DATA_BLOB.
128 *******************************************************************/
130 static NTSTATUS
parse_acl_blob(const DATA_BLOB
*pblob
,
132 struct security_descriptor
**ppdesc
,
133 uint16_t *p_hash_type
,
135 uint8_t hash
[XATTR_SD_HASH_SIZE
],
136 uint8_t sys_acl_hash
[XATTR_SD_HASH_SIZE
])
138 struct xattr_NTACL xacl
;
139 enum ndr_err_code ndr_err
;
141 TALLOC_CTX
*frame
= talloc_stackframe();
143 ndr_err
= ndr_pull_struct_blob(pblob
, frame
, &xacl
,
144 (ndr_pull_flags_fn_t
)ndr_pull_xattr_NTACL
);
146 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
147 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
148 ndr_errstr(ndr_err
)));
150 return ndr_map_error2ntstatus(ndr_err
);
153 *p_version
= xacl
.version
;
155 switch (xacl
.version
) {
157 *ppdesc
= make_sec_desc(mem_ctx
, SD_REVISION
,
158 xacl
.info
.sd
->type
| SEC_DESC_SELF_RELATIVE
,
159 xacl
.info
.sd
->owner_sid
,
160 xacl
.info
.sd
->group_sid
,
164 /* No hash - null out. */
165 *p_hash_type
= XATTR_SD_HASH_TYPE_NONE
;
166 memset(hash
, '\0', XATTR_SD_HASH_SIZE
);
169 *ppdesc
= make_sec_desc(mem_ctx
, SD_REVISION
,
170 xacl
.info
.sd_hs2
->sd
->type
| SEC_DESC_SELF_RELATIVE
,
171 xacl
.info
.sd_hs2
->sd
->owner_sid
,
172 xacl
.info
.sd_hs2
->sd
->group_sid
,
173 xacl
.info
.sd_hs2
->sd
->sacl
,
174 xacl
.info
.sd_hs2
->sd
->dacl
,
176 /* No hash - null out. */
177 *p_hash_type
= XATTR_SD_HASH_TYPE_NONE
;
178 memset(hash
, '\0', XATTR_SD_HASH_SIZE
);
181 *ppdesc
= make_sec_desc(mem_ctx
, SD_REVISION
,
182 xacl
.info
.sd_hs3
->sd
->type
| SEC_DESC_SELF_RELATIVE
,
183 xacl
.info
.sd_hs3
->sd
->owner_sid
,
184 xacl
.info
.sd_hs3
->sd
->group_sid
,
185 xacl
.info
.sd_hs3
->sd
->sacl
,
186 xacl
.info
.sd_hs3
->sd
->dacl
,
188 *p_hash_type
= xacl
.info
.sd_hs3
->hash_type
;
189 /* Current version 3 (if no sys acl hash available). */
190 memcpy(hash
, xacl
.info
.sd_hs3
->hash
, XATTR_SD_HASH_SIZE
);
193 *ppdesc
= make_sec_desc(mem_ctx
, SD_REVISION
,
194 xacl
.info
.sd_hs4
->sd
->type
| SEC_DESC_SELF_RELATIVE
,
195 xacl
.info
.sd_hs4
->sd
->owner_sid
,
196 xacl
.info
.sd_hs4
->sd
->group_sid
,
197 xacl
.info
.sd_hs4
->sd
->sacl
,
198 xacl
.info
.sd_hs4
->sd
->dacl
,
200 *p_hash_type
= xacl
.info
.sd_hs4
->hash_type
;
201 /* Current version 4. */
202 memcpy(hash
, xacl
.info
.sd_hs4
->hash
, XATTR_SD_HASH_SIZE
);
203 memcpy(sys_acl_hash
, xacl
.info
.sd_hs4
->sys_acl_hash
, XATTR_SD_HASH_SIZE
);
207 return NT_STATUS_REVISION_MISMATCH
;
212 return (*ppdesc
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
215 /*******************************************************************
216 Create a DATA_BLOB from a hash of the security descriptor storead at
217 the system layer and the NT ACL we wish to preserve
218 *******************************************************************/
220 static NTSTATUS
create_acl_blob(const struct security_descriptor
*psd
,
223 uint8_t hash
[XATTR_SD_HASH_SIZE
])
225 struct xattr_NTACL xacl
;
226 struct security_descriptor_hash_v3 sd_hs3
;
227 enum ndr_err_code ndr_err
;
228 TALLOC_CTX
*ctx
= talloc_tos();
234 xacl
.info
.sd_hs3
= &sd_hs3
;
235 xacl
.info
.sd_hs3
->sd
= discard_const_p(struct security_descriptor
, psd
);
236 xacl
.info
.sd_hs3
->hash_type
= hash_type
;
237 memcpy(&xacl
.info
.sd_hs3
->hash
[0], hash
, XATTR_SD_HASH_SIZE
);
239 ndr_err
= ndr_push_struct_blob(
241 (ndr_push_flags_fn_t
)ndr_push_xattr_NTACL
);
243 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
244 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
245 ndr_errstr(ndr_err
)));
246 return ndr_map_error2ntstatus(ndr_err
);
252 /*******************************************************************
253 Create a DATA_BLOB from a hash of the security descriptors
254 (system and NT) stored at the system layer and the NT ACL we wish
256 *******************************************************************/
258 static NTSTATUS
create_sys_acl_blob(const struct security_descriptor
*psd
,
261 uint8_t hash
[XATTR_SD_HASH_SIZE
],
262 const char *description
,
263 uint8_t sys_acl_hash
[XATTR_SD_HASH_SIZE
])
265 struct xattr_NTACL xacl
;
266 struct security_descriptor_hash_v4 sd_hs4
;
267 enum ndr_err_code ndr_err
;
268 TALLOC_CTX
*ctx
= talloc_tos();
270 struct timeval now
= timeval_current();
271 nttime_now
= timeval_to_nttime(&now
);
277 xacl
.info
.sd_hs4
= &sd_hs4
;
278 xacl
.info
.sd_hs4
->sd
= discard_const_p(struct security_descriptor
, psd
);
279 xacl
.info
.sd_hs4
->hash_type
= hash_type
;
280 memcpy(&xacl
.info
.sd_hs4
->hash
[0], hash
, XATTR_SD_HASH_SIZE
);
281 xacl
.info
.sd_hs4
->description
= description
;
282 xacl
.info
.sd_hs4
->time
= nttime_now
;
283 memcpy(&xacl
.info
.sd_hs4
->sys_acl_hash
[0], sys_acl_hash
, XATTR_SD_HASH_SIZE
);
285 ndr_err
= ndr_push_struct_blob(
287 (ndr_push_flags_fn_t
)ndr_push_xattr_NTACL
);
289 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
290 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
291 ndr_errstr(ndr_err
)));
292 return ndr_map_error2ntstatus(ndr_err
);
298 /*******************************************************************
299 Add in 3 inheritable components for a non-inheritable directory ACL.
300 CREATOR_OWNER/CREATOR_GROUP/WORLD.
301 *******************************************************************/
303 static NTSTATUS
add_directory_inheritable_components(vfs_handle_struct
*handle
,
305 SMB_STRUCT_STAT
*psbuf
,
306 struct security_descriptor
*psd
)
308 struct connection_struct
*conn
= handle
->conn
;
309 int num_aces
= (psd
->dacl
? psd
->dacl
->num_aces
: 0);
310 struct smb_filename smb_fname
;
311 enum security_ace_type acltype
;
312 uint32_t access_mask
;
316 struct security_ace
*new_ace_list
;
319 new_ace_list
= talloc_zero_array(psd
->dacl
,
324 * make_sec_acl() at the bottom of this function
325 * dupliates new_ace_list
327 new_ace_list
= talloc_zero_array(talloc_tos(),
332 if (new_ace_list
== NULL
) {
333 return NT_STATUS_NO_MEMORY
;
336 /* Fake a quick smb_filename. */
337 ZERO_STRUCT(smb_fname
);
338 smb_fname
.st
= *psbuf
;
339 smb_fname
.base_name
= discard_const_p(char, name
);
341 dir_mode
= unix_mode(conn
,
342 FILE_ATTRIBUTE_DIRECTORY
, &smb_fname
, NULL
);
343 file_mode
= unix_mode(conn
,
344 FILE_ATTRIBUTE_ARCHIVE
, &smb_fname
, NULL
);
346 mode
= dir_mode
| file_mode
;
348 DEBUG(10, ("add_directory_inheritable_components: directory %s, "
351 (unsigned int)mode
));
354 memcpy(new_ace_list
, psd
->dacl
->aces
,
355 num_aces
* sizeof(struct security_ace
));
357 access_mask
= map_canon_ace_perms(SNUM(conn
), &acltype
,
360 init_sec_ace(&new_ace_list
[num_aces
],
361 &global_sid_Creator_Owner
,
364 SEC_ACE_FLAG_CONTAINER_INHERIT
|
365 SEC_ACE_FLAG_OBJECT_INHERIT
|
366 SEC_ACE_FLAG_INHERIT_ONLY
);
367 access_mask
= map_canon_ace_perms(SNUM(conn
), &acltype
,
368 (mode
<< 3) & 0700, false);
369 init_sec_ace(&new_ace_list
[num_aces
+1],
370 &global_sid_Creator_Group
,
373 SEC_ACE_FLAG_CONTAINER_INHERIT
|
374 SEC_ACE_FLAG_OBJECT_INHERIT
|
375 SEC_ACE_FLAG_INHERIT_ONLY
);
376 access_mask
= map_canon_ace_perms(SNUM(conn
), &acltype
,
377 (mode
<< 6) & 0700, false);
378 init_sec_ace(&new_ace_list
[num_aces
+2],
382 SEC_ACE_FLAG_CONTAINER_INHERIT
|
383 SEC_ACE_FLAG_OBJECT_INHERIT
|
384 SEC_ACE_FLAG_INHERIT_ONLY
);
386 psd
->dacl
->aces
= new_ace_list
;
387 psd
->dacl
->num_aces
+= 3;
388 psd
->dacl
->size
+= new_ace_list
[num_aces
].size
+
389 new_ace_list
[num_aces
+1].size
+
390 new_ace_list
[num_aces
+2].size
;
392 psd
->dacl
= make_sec_acl(psd
,
396 if (psd
->dacl
== NULL
) {
397 return NT_STATUS_NO_MEMORY
;
403 static NTSTATUS
make_default_acl_posix(TALLOC_CTX
*ctx
,
405 SMB_STRUCT_STAT
*psbuf
,
406 struct security_descriptor
**ppdesc
)
408 struct dom_sid owner_sid
, group_sid
;
410 struct security_ace aces
[4];
411 uint32_t access_mask
= 0;
412 mode_t mode
= psbuf
->st_ex_mode
;
413 struct security_acl
*new_dacl
= NULL
;
416 DBG_DEBUG("file %s mode = 0%o\n",name
, (int)mode
);
418 uid_to_sid(&owner_sid
, psbuf
->st_ex_uid
);
419 gid_to_sid(&group_sid
, psbuf
->st_ex_gid
);
422 We provide up to 4 ACEs
429 if (mode
& S_IRUSR
) {
430 if (mode
& S_IWUSR
) {
431 access_mask
|= SEC_RIGHTS_FILE_ALL
;
433 access_mask
|= SEC_RIGHTS_FILE_READ
| SEC_FILE_EXECUTE
;
436 if (mode
& S_IWUSR
) {
437 access_mask
|= SEC_RIGHTS_FILE_WRITE
| SEC_STD_DELETE
;
440 init_sec_ace(&aces
[idx
],
442 SEC_ACE_TYPE_ACCESS_ALLOWED
,
448 if (mode
& S_IRGRP
) {
449 access_mask
|= SEC_RIGHTS_FILE_READ
| SEC_FILE_EXECUTE
;
451 if (mode
& S_IWGRP
) {
452 /* note that delete is not granted - this matches posix behaviour */
453 access_mask
|= SEC_RIGHTS_FILE_WRITE
;
456 init_sec_ace(&aces
[idx
],
458 SEC_ACE_TYPE_ACCESS_ALLOWED
,
465 if (mode
& S_IROTH
) {
466 access_mask
|= SEC_RIGHTS_FILE_READ
| SEC_FILE_EXECUTE
;
468 if (mode
& S_IWOTH
) {
469 access_mask
|= SEC_RIGHTS_FILE_WRITE
;
472 init_sec_ace(&aces
[idx
],
474 SEC_ACE_TYPE_ACCESS_ALLOWED
,
480 init_sec_ace(&aces
[idx
],
482 SEC_ACE_TYPE_ACCESS_ALLOWED
,
487 new_dacl
= make_sec_acl(ctx
,
493 return NT_STATUS_NO_MEMORY
;
496 *ppdesc
= make_sec_desc(ctx
,
497 SECURITY_DESCRIPTOR_REVISION_1
,
498 SEC_DESC_SELF_RELATIVE
|SEC_DESC_DACL_PRESENT
,
505 return NT_STATUS_NO_MEMORY
;
510 static NTSTATUS
make_default_acl_windows(TALLOC_CTX
*ctx
,
512 SMB_STRUCT_STAT
*psbuf
,
513 struct security_descriptor
**ppdesc
)
515 struct dom_sid owner_sid
, group_sid
;
517 struct security_ace aces
[4];
518 uint32_t access_mask
= 0;
519 mode_t mode
= psbuf
->st_ex_mode
;
520 struct security_acl
*new_dacl
= NULL
;
523 DBG_DEBUG("file [%s] mode [0%o]\n", name
, (int)mode
);
525 uid_to_sid(&owner_sid
, psbuf
->st_ex_uid
);
526 gid_to_sid(&group_sid
, psbuf
->st_ex_gid
);
534 if (mode
& S_IRUSR
) {
535 if (mode
& S_IWUSR
) {
536 access_mask
|= SEC_RIGHTS_FILE_ALL
;
538 access_mask
|= SEC_RIGHTS_FILE_READ
| SEC_FILE_EXECUTE
;
541 if (mode
& S_IWUSR
) {
542 access_mask
|= SEC_RIGHTS_FILE_WRITE
| SEC_STD_DELETE
;
545 init_sec_ace(&aces
[idx
],
547 SEC_ACE_TYPE_ACCESS_ALLOWED
,
552 init_sec_ace(&aces
[idx
],
554 SEC_ACE_TYPE_ACCESS_ALLOWED
,
559 new_dacl
= make_sec_acl(ctx
,
565 return NT_STATUS_NO_MEMORY
;
568 *ppdesc
= make_sec_desc(ctx
,
569 SECURITY_DESCRIPTOR_REVISION_1
,
570 SEC_DESC_SELF_RELATIVE
|SEC_DESC_DACL_PRESENT
,
577 return NT_STATUS_NO_MEMORY
;
582 static NTSTATUS
make_default_filesystem_acl(TALLOC_CTX
*ctx
,
583 struct acl_common_config
*config
,
585 SMB_STRUCT_STAT
*psbuf
,
586 struct security_descriptor
**ppdesc
)
590 switch (config
->default_acl_style
) {
592 case DEFAULT_ACL_POSIX
:
593 status
= make_default_acl_posix(ctx
, name
, psbuf
, ppdesc
);
596 case DEFAULT_ACL_WINDOWS
:
597 status
= make_default_acl_windows(ctx
, name
, psbuf
, ppdesc
);
601 DBG_ERR("unknown acl style %d", config
->default_acl_style
);
602 status
= NT_STATUS_INTERNAL_ERROR
;
610 * Validate an ACL blob
612 * This validates an ACL blob against the underlying filesystem ACL. If this
613 * function returns NT_STATUS_OK ppsd can be
615 * 1. the ACL from the blob (psd_from_fs=false), or
616 * 2. the ACL from the fs (psd_from_fs=true), or
619 * If the return value is anything else then NT_STATUS_OK, ppsd is set to NULL
620 * and psd_from_fs set to false.
622 * Returning the underlying filesystem ACL in case no. 2 is really just an
623 * optimisation, because some validations have to fetch the filesytem ACL as
624 * part of the validation, so we already have it available and callers might
627 static NTSTATUS
validate_nt_acl_blob(TALLOC_CTX
*mem_ctx
,
628 vfs_handle_struct
*handle
,
631 const DATA_BLOB
*blob
,
632 struct security_descriptor
**ppsd
,
633 bool *psd_is_from_fs
)
636 uint16_t hash_type
= XATTR_SD_HASH_TYPE_NONE
;
637 uint16_t xattr_version
= 0;
638 uint8_t hash
[XATTR_SD_HASH_SIZE
];
639 uint8_t sys_acl_hash
[XATTR_SD_HASH_SIZE
];
640 uint8_t hash_tmp
[XATTR_SD_HASH_SIZE
];
641 uint8_t sys_acl_hash_tmp
[XATTR_SD_HASH_SIZE
];
642 struct security_descriptor
*psd
= NULL
;
643 struct security_descriptor
*psd_blob
= NULL
;
644 struct security_descriptor
*psd_fs
= NULL
;
645 char *sys_acl_blob_description
= NULL
;
646 DATA_BLOB sys_acl_blob
= { 0 };
647 struct acl_common_config
*config
= NULL
;
650 *psd_is_from_fs
= false;
652 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
653 struct acl_common_config
,
654 return NT_STATUS_UNSUCCESSFUL
);
656 status
= parse_acl_blob(blob
,
664 if (!NT_STATUS_IS_OK(status
)) {
665 DBG_DEBUG("parse_acl_blob returned %s\n", nt_errstr(status
));
669 /* determine which type of xattr we got */
670 switch (xattr_version
) {
673 /* These xattr types are unilatteral, they do not
674 * require confirmation of the hash. In particular,
675 * the NTVFS file server uses version 1, but
676 * 'samba-tool ntacl' can set these as well */
681 if (config
->ignore_system_acls
) {
688 DBG_DEBUG("ACL blob revision mismatch (%u) for file %s\n",
689 (unsigned int)hash_type
, name
);
690 TALLOC_FREE(psd_blob
);
694 /* determine which type of xattr we got */
695 if (hash_type
!= XATTR_SD_HASH_TYPE_SHA256
) {
696 DBG_DEBUG("ACL blob hash type (%u) unexpected for file %s\n",
697 (unsigned int)hash_type
, name
);
698 TALLOC_FREE(psd_blob
);
702 /* determine which type of xattr we got */
703 switch (xattr_version
) {
708 /* Get the full underlying sd, then hash. */
709 ret
= SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle
,
712 &sys_acl_blob_description
,
715 /* Get the full underlying sd, then hash. */
716 ret
= SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle
,
719 &sys_acl_blob_description
,
723 /* If we fail to get the ACL blob (for some reason) then this
724 * is not fatal, we just work based on the NT ACL only */
726 status
= hash_blob_sha256(sys_acl_blob
, sys_acl_hash_tmp
);
727 if (!NT_STATUS_IS_OK(status
)) {
731 TALLOC_FREE(sys_acl_blob_description
);
732 TALLOC_FREE(sys_acl_blob
.data
);
734 if (memcmp(&sys_acl_hash
[0], &sys_acl_hash_tmp
[0],
735 XATTR_SD_HASH_SIZE
) == 0) {
736 /* Hash matches, return blob sd. */
737 DBG_DEBUG("blob hash matches for file %s\n",
744 /* Otherwise, fall though and see if the NT ACL hash matches */
747 /* Get the full underlying sd for the hash
748 or to return as backup. */
750 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
,
756 status
= SMB_VFS_NEXT_GET_NT_ACL(handle
,
763 if (!NT_STATUS_IS_OK(status
)) {
764 DBG_DEBUG("get_next_acl for file %s returned %s\n",
765 name
, nt_errstr(status
));
769 status
= hash_sd_sha256(psd_fs
, hash_tmp
);
770 if (!NT_STATUS_IS_OK(status
)) {
771 TALLOC_FREE(psd_blob
);
773 *psd_is_from_fs
= true;
777 if (memcmp(&hash
[0], &hash_tmp
[0], XATTR_SD_HASH_SIZE
) == 0) {
778 /* Hash matches, return blob sd. */
779 DBG_DEBUG("blob hash matches for file %s\n", name
);
784 /* Hash doesn't match, return underlying sd. */
785 DBG_DEBUG("blob hash does not match for file %s - returning "
786 "file system SD mapping.\n", name
);
788 if (DEBUGLEVEL
>= 10) {
789 DBG_DEBUG("acl for blob hash for %s is:\n", 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_name(vfs_handle_struct
*handle
,
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
,
860 uint32_t security_info
,
862 struct security_descriptor
**ppdesc
)
864 DATA_BLOB blob
= data_blob_null
;
866 struct security_descriptor
*psd
= NULL
;
867 bool psd_is_from_fs
= false;
868 struct acl_common_config
*config
= NULL
;
870 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
871 struct acl_common_config
,
872 return NT_STATUS_UNSUCCESSFUL
);
874 if (fsp
&& name
== NULL
) {
875 name
= fsp
->fsp_name
->base_name
;
878 DEBUG(10, ("get_nt_acl_internal: name=%s\n", name
));
880 status
= get_acl_blob(mem_ctx
, handle
, fsp
, name
, &blob
);
881 if (NT_STATUS_IS_OK(status
)) {
882 status
= validate_nt_acl_blob(mem_ctx
,
889 TALLOC_FREE(blob
.data
);
890 if (!NT_STATUS_IS_OK(status
)) {
891 DBG_DEBUG("ACL validation for [%s] failed\n",
898 /* Get the full underlying sd, as we failed to get the
899 * blob for the hash, or the revision/hash type wasn't
902 if (config
->ignore_system_acls
) {
903 SMB_STRUCT_STAT sbuf
;
904 SMB_STRUCT_STAT
*psbuf
= &sbuf
;
906 status
= stat_fsp_or_name(handle
, fsp
, name
,
908 if (!NT_STATUS_IS_OK(status
)) {
912 status
= make_default_filesystem_acl(
918 if (!NT_STATUS_IS_OK(status
)) {
923 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
,
929 status
= SMB_VFS_NEXT_GET_NT_ACL(handle
,
936 if (!NT_STATUS_IS_OK(status
)) {
937 DBG_DEBUG("get_next_acl for file %s "
938 "returned %s\n", name
,
943 psd_is_from_fs
= true;
947 if (psd_is_from_fs
) {
948 SMB_STRUCT_STAT sbuf
;
949 SMB_STRUCT_STAT
*psbuf
= &sbuf
;
950 bool is_directory
= false;
953 * We're returning the underlying ACL from the
954 * filesystem. If it's a directory, and has no
955 * inheritable ACE entries we have to fake them.
958 status
= stat_fsp_or_name(handle
, fsp
, name
,
960 if (!NT_STATUS_IS_OK(status
)) {
964 is_directory
= S_ISDIR(psbuf
->st_ex_mode
);
966 if (is_directory
&& !sd_has_inheritable_components(psd
, true)) {
967 status
= add_directory_inheritable_components(
972 if (!NT_STATUS_IS_OK(status
)) {
978 * The underlying POSIX module always sets the
979 * ~SEC_DESC_DACL_PROTECTED bit, as ACLs can't be inherited in
980 * this way under POSIX. Remove it for Windows-style ACLs.
982 psd
->type
&= ~SEC_DESC_DACL_PROTECTED
;
985 if (!(security_info
& SECINFO_OWNER
)) {
986 psd
->owner_sid
= NULL
;
988 if (!(security_info
& SECINFO_GROUP
)) {
989 psd
->group_sid
= NULL
;
991 if (!(security_info
& SECINFO_DACL
)) {
992 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
995 if (!(security_info
& SECINFO_SACL
)) {
996 psd
->type
&= ~SEC_DESC_SACL_PRESENT
;
1000 if (DEBUGLEVEL
>= 10) {
1001 DEBUG(10,("get_nt_acl_internal: returning acl for %s is:\n",
1003 NDR_PRINT_DEBUG(security_descriptor
, psd
);
1008 return NT_STATUS_OK
;
1015 /*********************************************************************
1016 Fetch a security descriptor given an fsp.
1017 *********************************************************************/
1019 static NTSTATUS
fget_nt_acl_common(vfs_handle_struct
*handle
,
1021 uint32_t security_info
,
1022 TALLOC_CTX
*mem_ctx
,
1023 struct security_descriptor
**ppdesc
)
1025 return get_nt_acl_internal(handle
, fsp
,
1026 NULL
, security_info
, mem_ctx
, ppdesc
);
1029 /*********************************************************************
1030 Fetch a security descriptor given a pathname.
1031 *********************************************************************/
1033 static NTSTATUS
get_nt_acl_common(vfs_handle_struct
*handle
,
1035 uint32_t security_info
,
1036 TALLOC_CTX
*mem_ctx
,
1037 struct security_descriptor
**ppdesc
)
1039 return get_nt_acl_internal(handle
, NULL
,
1040 name
, security_info
, mem_ctx
, ppdesc
);
1043 /*********************************************************************
1044 Set the underlying ACL (e.g. POSIX ACLS, POSIX owner, etc)
1045 *********************************************************************/
1046 static NTSTATUS
set_underlying_acl(vfs_handle_struct
*handle
, files_struct
*fsp
,
1047 struct security_descriptor
*psd
,
1048 uint32_t security_info_sent
,
1052 SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
1053 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
1057 /* We got access denied here. If we're already root,
1058 or we didn't need to do a chown, or the fsp isn't
1059 open with WRITE_OWNER access, just return. */
1060 if (get_current_uid(handle
->conn
) == 0 || chown_needed
== false ||
1061 !(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
1062 return NT_STATUS_ACCESS_DENIED
;
1065 DEBUG(10, ("fset_nt_acl_common: overriding chown on file %s "
1067 fsp_str_dbg(fsp
), sid_string_tos(psd
->owner_sid
)));
1069 /* Ok, we failed to chown and we have
1070 SEC_STD_WRITE_OWNER access - override. */
1072 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
1078 /*********************************************************************
1079 Store a v3 security descriptor
1080 *********************************************************************/
1081 static NTSTATUS
store_v3_blob(vfs_handle_struct
*handle
, files_struct
*fsp
,
1082 struct security_descriptor
*psd
,
1083 struct security_descriptor
*pdesc_next
,
1084 uint8_t hash
[XATTR_SD_HASH_SIZE
])
1089 if (DEBUGLEVEL
>= 10) {
1090 DEBUG(10, ("fset_nt_acl_xattr: storing xattr sd for file %s\n",
1093 security_descriptor
,
1094 discard_const_p(struct security_descriptor
, psd
));
1096 if (pdesc_next
!= NULL
) {
1097 DEBUG(10, ("fset_nt_acl_xattr: storing has in xattr sd "
1100 security_descriptor
,
1101 discard_const_p(struct security_descriptor
,
1105 ("fset_nt_acl_xattr: ignoring underlying sd\n"));
1108 status
= create_acl_blob(psd
, &blob
, XATTR_SD_HASH_TYPE_SHA256
, hash
);
1109 if (!NT_STATUS_IS_OK(status
)) {
1110 DEBUG(10, ("fset_nt_acl_xattr: create_acl_blob failed\n"));
1114 status
= store_acl_blob_fsp(handle
, fsp
, &blob
);
1118 /*********************************************************************
1119 Store a security descriptor given an fsp.
1120 *********************************************************************/
1122 static NTSTATUS
fset_nt_acl_common(vfs_handle_struct
*handle
, files_struct
*fsp
,
1123 uint32_t security_info_sent
, const struct security_descriptor
*orig_psd
)
1127 DATA_BLOB blob
, sys_acl_blob
;
1128 struct security_descriptor
*pdesc_next
= NULL
;
1129 struct security_descriptor
*psd
= NULL
;
1130 uint8_t hash
[XATTR_SD_HASH_SIZE
];
1131 uint8_t sys_acl_hash
[XATTR_SD_HASH_SIZE
];
1132 bool chown_needed
= false;
1133 char *sys_acl_description
;
1134 TALLOC_CTX
*frame
= talloc_stackframe();
1135 bool ignore_file_system_acl
= lp_parm_bool(
1136 SNUM(handle
->conn
), ACL_MODULE_NAME
, "ignore system acls", false);
1138 if (DEBUGLEVEL
>= 10) {
1139 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
1141 NDR_PRINT_DEBUG(security_descriptor
,
1142 discard_const_p(struct security_descriptor
, orig_psd
));
1145 status
= get_nt_acl_internal(handle
, fsp
,
1147 SECINFO_OWNER
|SECINFO_GROUP
|SECINFO_DACL
|SECINFO_SACL
,
1151 if (!NT_STATUS_IS_OK(status
)) {
1156 psd
->revision
= orig_psd
->revision
;
1157 /* All our SD's are self relative. */
1158 psd
->type
= orig_psd
->type
| SEC_DESC_SELF_RELATIVE
;
1160 if ((security_info_sent
& SECINFO_OWNER
) && (orig_psd
->owner_sid
!= NULL
)) {
1161 if (!dom_sid_equal(orig_psd
->owner_sid
, psd
->owner_sid
)) {
1162 /* We're changing the owner. */
1163 chown_needed
= true;
1165 psd
->owner_sid
= orig_psd
->owner_sid
;
1167 if ((security_info_sent
& SECINFO_GROUP
) && (orig_psd
->group_sid
!= NULL
)) {
1168 if (!dom_sid_equal(orig_psd
->group_sid
, psd
->group_sid
)) {
1169 /* We're changing the group. */
1170 chown_needed
= true;
1172 psd
->group_sid
= orig_psd
->group_sid
;
1174 if (security_info_sent
& SECINFO_DACL
) {
1175 if (security_descriptor_with_ms_nfs(orig_psd
)) {
1177 * If the sd contains a MS NFS SID, do
1178 * nothing, it's a chmod() request from OS X
1179 * with AAPL context.
1182 return NT_STATUS_OK
;
1184 psd
->dacl
= orig_psd
->dacl
;
1185 psd
->type
|= SEC_DESC_DACL_PRESENT
;
1187 if (security_info_sent
& SECINFO_SACL
) {
1188 psd
->sacl
= orig_psd
->sacl
;
1189 psd
->type
|= SEC_DESC_SACL_PRESENT
;
1192 if (ignore_file_system_acl
) {
1194 /* send only ownership stuff to lower layer */
1195 security_info_sent
&= (SECINFO_OWNER
| SECINFO_GROUP
);
1196 status
= set_underlying_acl(handle
, fsp
, psd
,
1197 security_info_sent
, true);
1198 if (!NT_STATUS_IS_OK(status
)) {
1204 status
= store_v3_blob(handle
, fsp
, psd
, NULL
, hash
);
1210 status
= set_underlying_acl(handle
, fsp
, psd
, security_info_sent
,
1212 if (!NT_STATUS_IS_OK(status
)) {
1217 /* Get the full underlying sd, then hash. */
1218 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
,
1224 if (!NT_STATUS_IS_OK(status
)) {
1229 status
= hash_sd_sha256(pdesc_next
, hash
);
1230 if (!NT_STATUS_IS_OK(status
)) {
1235 /* Get the full underlying sd, then hash. */
1236 ret
= SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle
,
1239 &sys_acl_description
,
1242 /* If we fail to get the ACL blob (for some reason) then this
1243 * is not fatal, we just work based on the NT ACL only */
1245 status
= store_v3_blob(handle
, fsp
, psd
, pdesc_next
, hash
);
1251 status
= hash_blob_sha256(sys_acl_blob
, sys_acl_hash
);
1252 if (!NT_STATUS_IS_OK(status
)) {
1257 if (DEBUGLEVEL
>= 10) {
1258 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s based on system ACL\n",
1260 NDR_PRINT_DEBUG(security_descriptor
,
1261 discard_const_p(struct security_descriptor
, psd
));
1263 DEBUG(10,("fset_nt_acl_xattr: storing hash in xattr sd based on system ACL and:\n"));
1264 NDR_PRINT_DEBUG(security_descriptor
,
1265 discard_const_p(struct security_descriptor
, pdesc_next
));
1268 /* We store hashes of both the sys ACL blob and the NT
1269 * security desciptor mapped from that ACL so as to improve
1270 * our chances against some inadvertant change breaking the
1272 status
= create_sys_acl_blob(psd
, &blob
, XATTR_SD_HASH_TYPE_SHA256
, hash
,
1273 sys_acl_description
, sys_acl_hash
);
1274 if (!NT_STATUS_IS_OK(status
)) {
1275 DEBUG(10, ("fset_nt_acl_xattr: create_sys_acl_blob failed\n"));
1280 status
= store_acl_blob_fsp(handle
, fsp
, &blob
);
1286 static int acl_common_remove_object(vfs_handle_struct
*handle
,
1290 connection_struct
*conn
= handle
->conn
;
1292 files_struct
*fsp
= NULL
;
1294 char *parent_dir
= NULL
;
1295 const char *final_component
= NULL
;
1296 struct smb_filename local_fname
;
1297 int saved_errno
= 0;
1298 char *saved_dir
= NULL
;
1300 saved_dir
= vfs_GetWd(talloc_tos(),conn
);
1302 saved_errno
= errno
;
1306 if (!parent_dirname(talloc_tos(), path
,
1307 &parent_dir
, &final_component
)) {
1308 saved_errno
= ENOMEM
;
1312 DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
1313 is_directory
? "directory" : "file",
1314 parent_dir
, final_component
));
1316 /* cd into the parent dir to pin it. */
1317 ret
= vfs_ChDir(conn
, parent_dir
);
1319 saved_errno
= errno
;
1323 ZERO_STRUCT(local_fname
);
1324 local_fname
.base_name
= discard_const_p(char, final_component
);
1326 /* Must use lstat here. */
1327 ret
= SMB_VFS_LSTAT(conn
, &local_fname
);
1329 saved_errno
= errno
;
1333 /* Ensure we have this file open with DELETE access. */
1334 id
= vfs_file_id_from_sbuf(conn
, &local_fname
.st
);
1335 for (fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
1336 fsp
= file_find_di_next(fsp
)) {
1337 if (fsp
->access_mask
& DELETE_ACCESS
&&
1338 fsp
->delete_on_close
) {
1339 /* We did open this for delete,
1340 * allow the delete as root.
1347 DEBUG(10,("acl_common_remove_object: %s %s/%s "
1348 "not an open file\n",
1349 is_directory
? "directory" : "file",
1350 parent_dir
, final_component
));
1351 saved_errno
= EACCES
;
1357 ret
= SMB_VFS_NEXT_RMDIR(handle
, final_component
);
1359 ret
= SMB_VFS_NEXT_UNLINK(handle
, &local_fname
);
1364 saved_errno
= errno
;
1369 TALLOC_FREE(parent_dir
);
1372 vfs_ChDir(conn
, saved_dir
);
1375 errno
= saved_errno
;
1380 static int rmdir_acl_common(struct vfs_handle_struct
*handle
,
1385 /* Try the normal rmdir first. */
1386 ret
= SMB_VFS_NEXT_RMDIR(handle
, path
);
1390 if (errno
== EACCES
|| errno
== EPERM
) {
1391 /* Failed due to access denied,
1392 see if we need to root override. */
1393 return acl_common_remove_object(handle
,
1398 DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
1404 static int unlink_acl_common(struct vfs_handle_struct
*handle
,
1405 const struct smb_filename
*smb_fname
)
1409 /* Try the normal unlink first. */
1410 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
1414 if (errno
== EACCES
|| errno
== EPERM
) {
1415 /* Failed due to access denied,
1416 see if we need to root override. */
1418 /* Don't do anything fancy for streams. */
1419 if (smb_fname
->stream_name
) {
1422 return acl_common_remove_object(handle
,
1423 smb_fname
->base_name
,
1427 DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
1428 smb_fname
->base_name
,
1433 static int chmod_acl_module_common(struct vfs_handle_struct
*handle
,
1434 const char *path
, mode_t mode
)
1436 if (lp_posix_pathnames()) {
1437 /* Only allow this on POSIX pathnames. */
1438 return SMB_VFS_NEXT_CHMOD(handle
, path
, mode
);
1443 static int fchmod_acl_module_common(struct vfs_handle_struct
*handle
,
1444 struct files_struct
*fsp
, mode_t mode
)
1446 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
1447 /* Only allow this on POSIX opens. */
1448 return SMB_VFS_NEXT_FCHMOD(handle
, fsp
, mode
);
1453 static int chmod_acl_acl_module_common(struct vfs_handle_struct
*handle
,
1454 const char *name
, mode_t mode
)
1456 if (lp_posix_pathnames()) {
1457 /* Only allow this on POSIX pathnames. */
1458 return SMB_VFS_NEXT_CHMOD_ACL(handle
, name
, mode
);
1463 static int fchmod_acl_acl_module_common(struct vfs_handle_struct
*handle
,
1464 struct files_struct
*fsp
, mode_t mode
)
1466 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_OPEN
) {
1467 /* Only allow this on POSIX opens. */
1468 return SMB_VFS_NEXT_FCHMOD_ACL(handle
, fsp
, mode
);