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"
28 static NTSTATUS
create_acl_blob(const struct security_descriptor
*psd
,
31 uint8_t hash
[XATTR_SD_HASH_SIZE
]);
33 static NTSTATUS
get_acl_blob(TALLOC_CTX
*ctx
,
34 vfs_handle_struct
*handle
,
39 static NTSTATUS
store_acl_blob_fsp(vfs_handle_struct
*handle
,
43 #define HASH_SECURITY_INFO (SECINFO_OWNER | \
48 /*******************************************************************
49 Hash a security descriptor.
50 *******************************************************************/
52 static NTSTATUS
hash_sd_sha256(struct security_descriptor
*psd
,
59 memset(hash
, '\0', XATTR_SD_HASH_SIZE
);
60 status
= create_acl_blob(psd
, &blob
, XATTR_SD_HASH_TYPE_SHA256
, hash
);
61 if (!NT_STATUS_IS_OK(status
)) {
65 samba_SHA256_Init(&tctx
);
66 samba_SHA256_Update(&tctx
, blob
.data
, blob
.length
);
67 samba_SHA256_Final(hash
, &tctx
);
72 /*******************************************************************
73 Parse out a struct security_descriptor from a DATA_BLOB.
74 *******************************************************************/
76 static NTSTATUS
parse_acl_blob(const DATA_BLOB
*pblob
,
77 struct security_descriptor
**ppdesc
,
78 uint16_t *p_hash_type
,
79 uint8_t hash
[XATTR_SD_HASH_SIZE
])
81 TALLOC_CTX
*ctx
= talloc_tos();
82 struct xattr_NTACL xacl
;
83 enum ndr_err_code ndr_err
;
86 ndr_err
= ndr_pull_struct_blob(pblob
, ctx
, &xacl
,
87 (ndr_pull_flags_fn_t
)ndr_pull_xattr_NTACL
);
89 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
90 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
91 ndr_errstr(ndr_err
)));
92 return ndr_map_error2ntstatus(ndr_err
);
95 switch (xacl
.version
) {
97 *ppdesc
= make_sec_desc(ctx
, SD_REVISION
,
98 xacl
.info
.sd
->type
| SEC_DESC_SELF_RELATIVE
,
99 xacl
.info
.sd
->owner_sid
,
100 xacl
.info
.sd
->group_sid
,
104 /* No hash - null out. */
105 *p_hash_type
= XATTR_SD_HASH_TYPE_NONE
;
106 memset(hash
, '\0', XATTR_SD_HASH_SIZE
);
109 *ppdesc
= make_sec_desc(ctx
, SD_REVISION
,
110 xacl
.info
.sd_hs2
->sd
->type
| SEC_DESC_SELF_RELATIVE
,
111 xacl
.info
.sd_hs2
->sd
->owner_sid
,
112 xacl
.info
.sd_hs2
->sd
->group_sid
,
113 xacl
.info
.sd_hs2
->sd
->sacl
,
114 xacl
.info
.sd_hs2
->sd
->dacl
,
116 /* No hash - null out. */
117 *p_hash_type
= XATTR_SD_HASH_TYPE_NONE
;
118 memset(hash
, '\0', XATTR_SD_HASH_SIZE
);
121 *ppdesc
= make_sec_desc(ctx
, SD_REVISION
,
122 xacl
.info
.sd_hs3
->sd
->type
| SEC_DESC_SELF_RELATIVE
,
123 xacl
.info
.sd_hs3
->sd
->owner_sid
,
124 xacl
.info
.sd_hs3
->sd
->group_sid
,
125 xacl
.info
.sd_hs3
->sd
->sacl
,
126 xacl
.info
.sd_hs3
->sd
->dacl
,
128 *p_hash_type
= xacl
.info
.sd_hs3
->hash_type
;
129 /* Current version 3. */
130 memcpy(hash
, xacl
.info
.sd_hs3
->hash
, XATTR_SD_HASH_SIZE
);
133 return NT_STATUS_REVISION_MISMATCH
;
136 TALLOC_FREE(xacl
.info
.sd
);
138 return (*ppdesc
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
141 /*******************************************************************
142 Create a DATA_BLOB from a security descriptor.
143 *******************************************************************/
145 static NTSTATUS
create_acl_blob(const struct security_descriptor
*psd
,
148 uint8_t hash
[XATTR_SD_HASH_SIZE
])
150 struct xattr_NTACL xacl
;
151 struct security_descriptor_hash_v3 sd_hs3
;
152 enum ndr_err_code ndr_err
;
153 TALLOC_CTX
*ctx
= talloc_tos();
159 xacl
.info
.sd_hs3
= &sd_hs3
;
160 xacl
.info
.sd_hs3
->sd
= discard_const_p(struct security_descriptor
, psd
);
161 xacl
.info
.sd_hs3
->hash_type
= hash_type
;
162 memcpy(&xacl
.info
.sd_hs3
->hash
[0], hash
, XATTR_SD_HASH_SIZE
);
164 ndr_err
= ndr_push_struct_blob(
166 (ndr_push_flags_fn_t
)ndr_push_xattr_NTACL
);
168 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
169 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
170 ndr_errstr(ndr_err
)));
171 return ndr_map_error2ntstatus(ndr_err
);
177 /*******************************************************************
178 Add in 3 inheritable components for a non-inheritable directory ACL.
179 CREATOR_OWNER/CREATOR_GROUP/WORLD.
180 *******************************************************************/
182 static NTSTATUS
add_directory_inheritable_components(vfs_handle_struct
*handle
,
184 SMB_STRUCT_STAT
*psbuf
,
185 struct security_descriptor
*psd
)
187 struct connection_struct
*conn
= handle
->conn
;
188 int num_aces
= (psd
->dacl
? psd
->dacl
->num_aces
: 0);
189 struct smb_filename smb_fname
;
190 enum security_ace_type acltype
;
191 uint32_t access_mask
;
195 struct security_ace
*new_ace_list
= talloc_zero_array(talloc_tos(),
199 if (new_ace_list
== NULL
) {
200 return NT_STATUS_NO_MEMORY
;
203 /* Fake a quick smb_filename. */
204 ZERO_STRUCT(smb_fname
);
205 smb_fname
.st
= *psbuf
;
206 smb_fname
.base_name
= discard_const_p(char, name
);
208 dir_mode
= unix_mode(conn
,
209 FILE_ATTRIBUTE_DIRECTORY
, &smb_fname
, NULL
);
210 file_mode
= unix_mode(conn
,
211 FILE_ATTRIBUTE_ARCHIVE
, &smb_fname
, NULL
);
213 mode
= dir_mode
| file_mode
;
215 DEBUG(10, ("add_directory_inheritable_components: directory %s, "
218 (unsigned int)mode
));
221 memcpy(new_ace_list
, psd
->dacl
->aces
,
222 num_aces
* sizeof(struct security_ace
));
224 access_mask
= map_canon_ace_perms(SNUM(conn
), &acltype
,
227 init_sec_ace(&new_ace_list
[num_aces
],
228 &global_sid_Creator_Owner
,
231 SEC_ACE_FLAG_CONTAINER_INHERIT
|
232 SEC_ACE_FLAG_OBJECT_INHERIT
|
233 SEC_ACE_FLAG_INHERIT_ONLY
);
234 access_mask
= map_canon_ace_perms(SNUM(conn
), &acltype
,
235 (mode
<< 3) & 0700, false);
236 init_sec_ace(&new_ace_list
[num_aces
+1],
237 &global_sid_Creator_Group
,
240 SEC_ACE_FLAG_CONTAINER_INHERIT
|
241 SEC_ACE_FLAG_OBJECT_INHERIT
|
242 SEC_ACE_FLAG_INHERIT_ONLY
);
243 access_mask
= map_canon_ace_perms(SNUM(conn
), &acltype
,
244 (mode
<< 6) & 0700, false);
245 init_sec_ace(&new_ace_list
[num_aces
+2],
249 SEC_ACE_FLAG_CONTAINER_INHERIT
|
250 SEC_ACE_FLAG_OBJECT_INHERIT
|
251 SEC_ACE_FLAG_INHERIT_ONLY
);
253 psd
->dacl
->aces
= new_ace_list
;
254 psd
->dacl
->num_aces
+= 3;
256 psd
->dacl
= make_sec_acl(talloc_tos(),
260 if (psd
->dacl
== NULL
) {
261 return NT_STATUS_NO_MEMORY
;
267 /*******************************************************************
268 Pull a DATA_BLOB from an xattr given a pathname.
269 If the hash doesn't match, or doesn't exist - return the underlying
271 *******************************************************************/
273 static NTSTATUS
get_nt_acl_internal(vfs_handle_struct
*handle
,
276 uint32_t security_info
,
277 struct security_descriptor
**ppdesc
)
279 DATA_BLOB blob
= data_blob_null
;
281 uint16_t hash_type
= XATTR_SD_HASH_TYPE_NONE
;
282 uint8_t hash
[XATTR_SD_HASH_SIZE
];
283 uint8_t hash_tmp
[XATTR_SD_HASH_SIZE
];
284 struct security_descriptor
*psd
= NULL
;
285 struct security_descriptor
*pdesc_next
= NULL
;
286 bool ignore_file_system_acl
= lp_parm_bool(SNUM(handle
->conn
),
288 "ignore system acls",
291 if (fsp
&& name
== NULL
) {
292 name
= fsp
->fsp_name
->base_name
;
295 DEBUG(10, ("get_nt_acl_internal: name=%s\n", name
));
297 /* Get the full underlying sd for the hash
298 or to return as backup. */
300 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
,
305 status
= SMB_VFS_NEXT_GET_NT_ACL(handle
,
311 if (!NT_STATUS_IS_OK(status
)) {
312 DEBUG(10, ("get_nt_acl_internal: get_next_acl for file %s "
319 status
= get_acl_blob(talloc_tos(), handle
, fsp
, name
, &blob
);
320 if (!NT_STATUS_IS_OK(status
)) {
321 DEBUG(10, ("get_nt_acl_internal: get_acl_blob returned %s\n",
327 status
= parse_acl_blob(&blob
, &psd
,
328 &hash_type
, &hash
[0]);
329 if (!NT_STATUS_IS_OK(status
)) {
330 DEBUG(10, ("parse_acl_blob returned %s\n",
336 /* Ensure the hash type is one we know. */
338 case XATTR_SD_HASH_TYPE_NONE
:
339 /* No hash, just return blob sd. */
341 case XATTR_SD_HASH_TYPE_SHA256
:
344 DEBUG(10, ("get_nt_acl_internal: ACL blob revision "
345 "mismatch (%u) for file %s\n",
346 (unsigned int)hash_type
,
353 if (ignore_file_system_acl
) {
357 status
= hash_sd_sha256(pdesc_next
, hash_tmp
);
358 if (!NT_STATUS_IS_OK(status
)) {
364 if (memcmp(&hash
[0], &hash_tmp
[0], XATTR_SD_HASH_SIZE
) == 0) {
365 /* Hash matches, return blob sd. */
366 DEBUG(10, ("get_nt_acl_internal: blob hash "
367 "matches for file %s\n",
372 /* Hash doesn't match, return underlying sd. */
373 DEBUG(10, ("get_nt_acl_internal: blob hash "
374 "does not match for file %s - returning "
375 "file system SD mapping.\n",
383 if (psd
!= pdesc_next
) {
384 /* We're returning the blob, throw
385 * away the filesystem SD. */
386 TALLOC_FREE(pdesc_next
);
388 SMB_STRUCT_STAT sbuf
;
389 SMB_STRUCT_STAT
*psbuf
= &sbuf
;
390 bool is_directory
= false;
392 * We're returning the underlying ACL from the
393 * filesystem. If it's a directory, and has no
394 * inheritable ACE entries we have to fake them.
397 status
= vfs_stat_fsp(fsp
);
398 if (!NT_STATUS_IS_OK(status
)) {
401 psbuf
= &fsp
->fsp_name
->st
;
403 int ret
= vfs_stat_smb_fname(handle
->conn
,
407 return map_nt_error_from_unix(errno
);
410 is_directory
= S_ISDIR(psbuf
->st_ex_mode
);
412 if (ignore_file_system_acl
) {
413 TALLOC_FREE(pdesc_next
);
414 status
= make_default_filesystem_acl(talloc_tos(),
418 if (!NT_STATUS_IS_OK(status
)) {
423 !sd_has_inheritable_components(psd
,
425 status
= add_directory_inheritable_components(
430 if (!NT_STATUS_IS_OK(status
)) {
434 /* The underlying POSIX module always sets
435 the ~SEC_DESC_DACL_PROTECTED bit, as ACLs
436 can't be inherited in this way under POSIX.
437 Remove it for Windows-style ACLs. */
438 psd
->type
&= ~SEC_DESC_DACL_PROTECTED
;
442 if (!(security_info
& SECINFO_OWNER
)) {
443 psd
->owner_sid
= NULL
;
445 if (!(security_info
& SECINFO_GROUP
)) {
446 psd
->group_sid
= NULL
;
448 if (!(security_info
& SECINFO_DACL
)) {
449 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
452 if (!(security_info
& SECINFO_SACL
)) {
453 psd
->type
&= ~SEC_DESC_SACL_PRESENT
;
457 TALLOC_FREE(blob
.data
);
460 if (DEBUGLEVEL
>= 10) {
461 DEBUG(10,("get_nt_acl_internal: returning acl for %s is:\n",
463 NDR_PRINT_DEBUG(security_descriptor
, psd
);
469 /*********************************************************************
470 Fetch a security descriptor given an fsp.
471 *********************************************************************/
473 static NTSTATUS
fget_nt_acl_common(vfs_handle_struct
*handle
, files_struct
*fsp
,
474 uint32_t security_info
, struct security_descriptor
**ppdesc
)
476 return get_nt_acl_internal(handle
, fsp
,
477 NULL
, security_info
, ppdesc
);
480 /*********************************************************************
481 Fetch a security descriptor given a pathname.
482 *********************************************************************/
484 static NTSTATUS
get_nt_acl_common(vfs_handle_struct
*handle
,
485 const char *name
, uint32_t security_info
, struct security_descriptor
**ppdesc
)
487 return get_nt_acl_internal(handle
, NULL
,
488 name
, security_info
, ppdesc
);
491 /*********************************************************************
492 Store a security descriptor given an fsp.
493 *********************************************************************/
495 static NTSTATUS
fset_nt_acl_common(vfs_handle_struct
*handle
, files_struct
*fsp
,
496 uint32_t security_info_sent
, const struct security_descriptor
*orig_psd
)
500 struct security_descriptor
*pdesc_next
= NULL
;
501 struct security_descriptor
*psd
= NULL
;
502 uint8_t hash
[XATTR_SD_HASH_SIZE
];
503 bool chown_needed
= false;
505 if (DEBUGLEVEL
>= 10) {
506 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
508 NDR_PRINT_DEBUG(security_descriptor
,
509 discard_const_p(struct security_descriptor
, orig_psd
));
512 status
= get_nt_acl_internal(handle
, fsp
,
514 SECINFO_OWNER
|SECINFO_GROUP
|SECINFO_DACL
|SECINFO_SACL
,
517 if (!NT_STATUS_IS_OK(status
)) {
521 psd
->revision
= orig_psd
->revision
;
522 /* All our SD's are self relative. */
523 psd
->type
= orig_psd
->type
| SEC_DESC_SELF_RELATIVE
;
525 if ((security_info_sent
& SECINFO_OWNER
) && (orig_psd
->owner_sid
!= NULL
)) {
526 if (!dom_sid_equal(orig_psd
->owner_sid
, psd
->owner_sid
)) {
527 /* We're changing the owner. */
530 psd
->owner_sid
= orig_psd
->owner_sid
;
532 if ((security_info_sent
& SECINFO_GROUP
) && (orig_psd
->group_sid
!= NULL
)) {
533 if (!dom_sid_equal(orig_psd
->group_sid
, psd
->group_sid
)) {
534 /* We're changing the group. */
537 psd
->group_sid
= orig_psd
->group_sid
;
539 if (security_info_sent
& SECINFO_DACL
) {
540 psd
->dacl
= orig_psd
->dacl
;
541 psd
->type
|= SEC_DESC_DACL_PRESENT
;
543 if (security_info_sent
& SECINFO_SACL
) {
544 psd
->sacl
= orig_psd
->sacl
;
545 psd
->type
|= SEC_DESC_SACL_PRESENT
;
548 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
549 if (!NT_STATUS_IS_OK(status
)) {
550 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
553 /* We got access denied here. If we're already root,
554 or we didn't need to do a chown, or the fsp isn't
555 open with WRITE_OWNER access, just return. */
556 if (get_current_uid(handle
->conn
) == 0 ||
557 chown_needed
== false ||
558 !(fsp
->access_mask
& SEC_STD_WRITE_OWNER
)) {
559 return NT_STATUS_ACCESS_DENIED
;
562 DEBUG(10,("fset_nt_acl_common: overriding chown on file %s "
565 sid_string_tos(psd
->owner_sid
)
568 /* Ok, we failed to chown and we have
569 SEC_STD_WRITE_OWNER access - override. */
571 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
,
572 security_info_sent
, psd
);
574 if (!NT_STATUS_IS_OK(status
)) {
579 /* Get the full underlying sd, then hash. */
580 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
,
585 if (!NT_STATUS_IS_OK(status
)) {
589 status
= hash_sd_sha256(pdesc_next
, hash
);
590 if (!NT_STATUS_IS_OK(status
)) {
594 if (DEBUGLEVEL
>= 10) {
595 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s\n",
597 NDR_PRINT_DEBUG(security_descriptor
,
598 discard_const_p(struct security_descriptor
, psd
));
600 status
= create_acl_blob(psd
, &blob
, XATTR_SD_HASH_TYPE_SHA256
, hash
);
601 if (!NT_STATUS_IS_OK(status
)) {
602 DEBUG(10, ("fset_nt_acl_xattr: create_acl_blob failed\n"));
606 status
= store_acl_blob_fsp(handle
, fsp
, &blob
);
611 static int acl_common_remove_object(vfs_handle_struct
*handle
,
615 connection_struct
*conn
= handle
->conn
;
617 files_struct
*fsp
= NULL
;
619 char *parent_dir
= NULL
;
620 const char *final_component
= NULL
;
621 struct smb_filename local_fname
;
623 char *saved_dir
= NULL
;
625 saved_dir
= vfs_GetWd(talloc_tos(),conn
);
631 if (!parent_dirname(talloc_tos(), path
,
632 &parent_dir
, &final_component
)) {
633 saved_errno
= ENOMEM
;
637 DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
638 is_directory
? "directory" : "file",
639 parent_dir
, final_component
));
641 /* cd into the parent dir to pin it. */
642 ret
= vfs_ChDir(conn
, parent_dir
);
648 ZERO_STRUCT(local_fname
);
649 local_fname
.base_name
= discard_const_p(char, final_component
);
651 /* Must use lstat here. */
652 ret
= SMB_VFS_LSTAT(conn
, &local_fname
);
658 /* Ensure we have this file open with DELETE access. */
659 id
= vfs_file_id_from_sbuf(conn
, &local_fname
.st
);
660 for (fsp
= file_find_di_first(conn
->sconn
, id
); fsp
;
661 fsp
= file_find_di_next(fsp
)) {
662 if (fsp
->access_mask
& DELETE_ACCESS
&&
663 fsp
->delete_on_close
) {
664 /* We did open this for delete,
665 * allow the delete as root.
672 DEBUG(10,("acl_common_remove_object: %s %s/%s "
673 "not an open file\n",
674 is_directory
? "directory" : "file",
675 parent_dir
, final_component
));
676 saved_errno
= EACCES
;
682 ret
= SMB_VFS_NEXT_RMDIR(handle
, final_component
);
684 ret
= SMB_VFS_NEXT_UNLINK(handle
, &local_fname
);
694 TALLOC_FREE(parent_dir
);
697 vfs_ChDir(conn
, saved_dir
);
705 static int rmdir_acl_common(struct vfs_handle_struct
*handle
,
710 /* Try the normal rmdir first. */
711 ret
= SMB_VFS_NEXT_RMDIR(handle
, path
);
715 if (errno
== EACCES
|| errno
== EPERM
) {
716 /* Failed due to access denied,
717 see if we need to root override. */
718 return acl_common_remove_object(handle
,
723 DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
729 static int unlink_acl_common(struct vfs_handle_struct
*handle
,
730 const struct smb_filename
*smb_fname
)
734 /* Try the normal unlink first. */
735 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname
);
739 if (errno
== EACCES
|| errno
== EPERM
) {
740 /* Failed due to access denied,
741 see if we need to root override. */
743 /* Don't do anything fancy for streams. */
744 if (smb_fname
->stream_name
) {
747 return acl_common_remove_object(handle
,
748 smb_fname
->base_name
,
752 DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
753 smb_fname
->base_name
,
758 static int chmod_acl_module_common(struct vfs_handle_struct
*handle
,
759 const char *path
, mode_t mode
)
761 if (lp_posix_pathnames()) {
762 /* Only allow this on POSIX pathnames. */
763 return SMB_VFS_NEXT_CHMOD(handle
, path
, mode
);
768 static int fchmod_acl_module_common(struct vfs_handle_struct
*handle
,
769 struct files_struct
*fsp
, mode_t mode
)
771 if (fsp
->posix_open
) {
772 /* Only allow this on POSIX opens. */
773 return SMB_VFS_NEXT_FCHMOD(handle
, fsp
, mode
);
778 static int chmod_acl_acl_module_common(struct vfs_handle_struct
*handle
,
779 const char *name
, mode_t mode
)
781 if (lp_posix_pathnames()) {
782 /* Only allow this on POSIX pathnames. */
783 return SMB_VFS_NEXT_CHMOD_ACL(handle
, name
, mode
);
788 static int fchmod_acl_acl_module_common(struct vfs_handle_struct
*handle
,
789 struct files_struct
*fsp
, mode_t mode
)
791 if (fsp
->posix_open
) {
792 /* Only allow this on POSIX opens. */
793 return SMB_VFS_NEXT_FCHMOD_ACL(handle
, fsp
, mode
);