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_blob_sha256(DATA_BLOB blob,
57 memset(hash, '\0', XATTR_SD_HASH_SIZE);
59 samba_SHA256_Init(&tctx);
60 samba_SHA256_Update(&tctx, blob.data, blob.length);
61 samba_SHA256_Final(hash, &tctx);
66 /*******************************************************************
67 Hash a security descriptor.
68 *******************************************************************/
70 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
76 memset(hash, '\0', XATTR_SD_HASH_SIZE);
77 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
78 if (!NT_STATUS_IS_OK(status)) {
81 return hash_blob_sha256(blob, hash);
84 /*******************************************************************
85 Parse out a struct security_descriptor from a DATA_BLOB.
86 *******************************************************************/
88 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
90 struct security_descriptor **ppdesc,
91 uint16_t *p_hash_type,
92 uint8_t hash[XATTR_SD_HASH_SIZE])
94 struct xattr_NTACL xacl;
95 enum ndr_err_code ndr_err;
97 TALLOC_CTX *frame = talloc_stackframe();
99 ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
100 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
102 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
103 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
104 ndr_errstr(ndr_err)));
106 return ndr_map_error2ntstatus(ndr_err);
109 switch (xacl.version) {
111 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
112 xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
113 xacl.info.sd->owner_sid,
114 xacl.info.sd->group_sid,
118 /* No hash - null out. */
119 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
120 memset(hash, '\0', XATTR_SD_HASH_SIZE);
123 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
124 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
125 xacl.info.sd_hs2->sd->owner_sid,
126 xacl.info.sd_hs2->sd->group_sid,
127 xacl.info.sd_hs2->sd->sacl,
128 xacl.info.sd_hs2->sd->dacl,
130 /* No hash - null out. */
131 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
132 memset(hash, '\0', XATTR_SD_HASH_SIZE);
135 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
136 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
137 xacl.info.sd_hs3->sd->owner_sid,
138 xacl.info.sd_hs3->sd->group_sid,
139 xacl.info.sd_hs3->sd->sacl,
140 xacl.info.sd_hs3->sd->dacl,
142 *p_hash_type = xacl.info.sd_hs3->hash_type;
143 /* Current version 3. */
144 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
148 return NT_STATUS_REVISION_MISMATCH;
153 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
156 /*******************************************************************
157 Create a DATA_BLOB from a security descriptor.
158 *******************************************************************/
160 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
163 uint8_t hash[XATTR_SD_HASH_SIZE])
165 struct xattr_NTACL xacl;
166 struct security_descriptor_hash_v3 sd_hs3;
167 enum ndr_err_code ndr_err;
168 TALLOC_CTX *ctx = talloc_tos();
174 xacl.info.sd_hs3 = &sd_hs3;
175 xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
176 xacl.info.sd_hs3->hash_type = hash_type;
177 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
179 ndr_err = ndr_push_struct_blob(
181 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
183 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
184 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
185 ndr_errstr(ndr_err)));
186 return ndr_map_error2ntstatus(ndr_err);
192 /*******************************************************************
193 Add in 3 inheritable components for a non-inheritable directory ACL.
194 CREATOR_OWNER/CREATOR_GROUP/WORLD.
195 *******************************************************************/
197 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
199 SMB_STRUCT_STAT *psbuf,
200 struct security_descriptor *psd)
202 struct connection_struct *conn = handle->conn;
203 int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
204 struct smb_filename smb_fname;
205 enum security_ace_type acltype;
206 uint32_t access_mask;
210 struct security_ace *new_ace_list;
213 new_ace_list = talloc_zero_array(psd->dacl,
218 * make_sec_acl() at the bottom of this function
219 * dupliates new_ace_list
221 new_ace_list = talloc_zero_array(talloc_tos(),
226 if (new_ace_list == NULL) {
227 return NT_STATUS_NO_MEMORY;
230 /* Fake a quick smb_filename. */
231 ZERO_STRUCT(smb_fname);
232 smb_fname.st = *psbuf;
233 smb_fname.base_name = discard_const_p(char, name);
235 dir_mode = unix_mode(conn,
236 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
237 file_mode = unix_mode(conn,
238 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
240 mode = dir_mode | file_mode;
242 DEBUG(10, ("add_directory_inheritable_components: directory %s, "
245 (unsigned int)mode ));
248 memcpy(new_ace_list, psd->dacl->aces,
249 num_aces * sizeof(struct security_ace));
251 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
254 init_sec_ace(&new_ace_list[num_aces],
255 &global_sid_Creator_Owner,
258 SEC_ACE_FLAG_CONTAINER_INHERIT|
259 SEC_ACE_FLAG_OBJECT_INHERIT|
260 SEC_ACE_FLAG_INHERIT_ONLY);
261 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
262 (mode << 3) & 0700, false);
263 init_sec_ace(&new_ace_list[num_aces+1],
264 &global_sid_Creator_Group,
267 SEC_ACE_FLAG_CONTAINER_INHERIT|
268 SEC_ACE_FLAG_OBJECT_INHERIT|
269 SEC_ACE_FLAG_INHERIT_ONLY);
270 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
271 (mode << 6) & 0700, false);
272 init_sec_ace(&new_ace_list[num_aces+2],
276 SEC_ACE_FLAG_CONTAINER_INHERIT|
277 SEC_ACE_FLAG_OBJECT_INHERIT|
278 SEC_ACE_FLAG_INHERIT_ONLY);
280 psd->dacl->aces = new_ace_list;
281 psd->dacl->num_aces += 3;
283 psd->dacl = make_sec_acl(psd,
287 if (psd->dacl == NULL) {
288 return NT_STATUS_NO_MEMORY;
294 /*******************************************************************
295 Pull a DATA_BLOB from an xattr given a pathname.
296 If the hash doesn't match, or doesn't exist - return the underlying
298 *******************************************************************/
300 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
303 uint32_t security_info,
305 struct security_descriptor **ppdesc)
307 DATA_BLOB blob = data_blob_null;
309 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
310 uint8_t hash[XATTR_SD_HASH_SIZE];
311 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
312 struct security_descriptor *psd = NULL;
313 struct security_descriptor *pdesc_next = NULL;
314 bool ignore_file_system_acl = lp_parm_bool(SNUM(handle->conn),
316 "ignore system acls",
319 if (fsp && name == NULL) {
320 name = fsp->fsp_name->base_name;
323 DEBUG(10, ("get_nt_acl_internal: name=%s\n", name));
325 /* Get the full underlying sd for the hash
326 or to return as backup. */
328 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
334 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
341 if (!NT_STATUS_IS_OK(status)) {
342 DEBUG(10, ("get_nt_acl_internal: get_next_acl for file %s "
349 status = get_acl_blob(talloc_tos(), handle, fsp, name, &blob);
350 if (!NT_STATUS_IS_OK(status)) {
351 DEBUG(10, ("get_nt_acl_internal: get_acl_blob returned %s\n",
357 status = parse_acl_blob(&blob, mem_ctx, &psd,
358 &hash_type, &hash[0]);
359 if (!NT_STATUS_IS_OK(status)) {
360 DEBUG(10, ("parse_acl_blob returned %s\n",
366 /* Ensure the hash type is one we know. */
368 case XATTR_SD_HASH_TYPE_NONE:
369 /* No hash, just return blob sd. */
371 case XATTR_SD_HASH_TYPE_SHA256:
374 DEBUG(10, ("get_nt_acl_internal: ACL blob revision "
375 "mismatch (%u) for file %s\n",
376 (unsigned int)hash_type,
383 if (ignore_file_system_acl) {
387 status = hash_sd_sha256(pdesc_next, hash_tmp);
388 if (!NT_STATUS_IS_OK(status)) {
394 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
395 /* Hash matches, return blob sd. */
396 DEBUG(10, ("get_nt_acl_internal: blob hash "
397 "matches for file %s\n",
402 /* Hash doesn't match, return underlying sd. */
403 DEBUG(10, ("get_nt_acl_internal: blob hash "
404 "does not match for file %s - returning "
405 "file system SD mapping.\n",
408 if (DEBUGLEVEL >= 10) {
409 DEBUG(10,("get_nt_acl_internal: acl for blob hash for %s is:\n",
411 NDR_PRINT_DEBUG(security_descriptor, pdesc_next);
419 if (psd != pdesc_next) {
420 /* We're returning the blob, throw
421 * away the filesystem SD. */
422 TALLOC_FREE(pdesc_next);
424 SMB_STRUCT_STAT sbuf;
425 SMB_STRUCT_STAT *psbuf = &sbuf;
426 bool is_directory = false;
428 * We're returning the underlying ACL from the
429 * filesystem. If it's a directory, and has no
430 * inheritable ACE entries we have to fake them.
433 status = vfs_stat_fsp(fsp);
434 if (!NT_STATUS_IS_OK(status)) {
437 psbuf = &fsp->fsp_name->st;
439 int ret = vfs_stat_smb_fname(handle->conn,
443 return map_nt_error_from_unix(errno);
446 is_directory = S_ISDIR(psbuf->st_ex_mode);
448 if (ignore_file_system_acl) {
449 TALLOC_FREE(pdesc_next);
450 status = make_default_filesystem_acl(mem_ctx,
454 if (!NT_STATUS_IS_OK(status)) {
459 !sd_has_inheritable_components(psd,
461 status = add_directory_inheritable_components(
466 if (!NT_STATUS_IS_OK(status)) {
470 /* The underlying POSIX module always sets
471 the ~SEC_DESC_DACL_PROTECTED bit, as ACLs
472 can't be inherited in this way under POSIX.
473 Remove it for Windows-style ACLs. */
474 psd->type &= ~SEC_DESC_DACL_PROTECTED;
478 if (!(security_info & SECINFO_OWNER)) {
479 psd->owner_sid = NULL;
481 if (!(security_info & SECINFO_GROUP)) {
482 psd->group_sid = NULL;
484 if (!(security_info & SECINFO_DACL)) {
485 psd->type &= ~SEC_DESC_DACL_PRESENT;
488 if (!(security_info & SECINFO_SACL)) {
489 psd->type &= ~SEC_DESC_SACL_PRESENT;
493 TALLOC_FREE(blob.data);
496 if (DEBUGLEVEL >= 10) {
497 DEBUG(10,("get_nt_acl_internal: returning acl for %s is:\n",
499 NDR_PRINT_DEBUG(security_descriptor, psd);
505 /*********************************************************************
506 Fetch a security descriptor given an fsp.
507 *********************************************************************/
509 static NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle,
511 uint32_t security_info,
513 struct security_descriptor **ppdesc)
515 return get_nt_acl_internal(handle, fsp,
516 NULL, security_info, mem_ctx, ppdesc);
519 /*********************************************************************
520 Fetch a security descriptor given a pathname.
521 *********************************************************************/
523 static NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
525 uint32_t security_info,
527 struct security_descriptor **ppdesc)
529 return get_nt_acl_internal(handle, NULL,
530 name, security_info, mem_ctx, ppdesc);
533 /*********************************************************************
534 Store a security descriptor given an fsp.
535 *********************************************************************/
537 static NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
538 uint32_t security_info_sent, const struct security_descriptor *orig_psd)
542 struct security_descriptor *pdesc_next = NULL;
543 struct security_descriptor *psd = NULL;
544 uint8_t hash[XATTR_SD_HASH_SIZE];
545 bool chown_needed = false;
546 TALLOC_CTX *frame = talloc_stackframe();
548 if (DEBUGLEVEL >= 10) {
549 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
551 NDR_PRINT_DEBUG(security_descriptor,
552 discard_const_p(struct security_descriptor, orig_psd));
555 status = get_nt_acl_internal(handle, fsp,
557 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
561 if (!NT_STATUS_IS_OK(status)) {
566 psd->revision = orig_psd->revision;
567 /* All our SD's are self relative. */
568 psd->type = orig_psd->type | SEC_DESC_SELF_RELATIVE;
570 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
571 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
572 /* We're changing the owner. */
575 psd->owner_sid = orig_psd->owner_sid;
577 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
578 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
579 /* We're changing the group. */
582 psd->group_sid = orig_psd->group_sid;
584 if (security_info_sent & SECINFO_DACL) {
585 psd->dacl = orig_psd->dacl;
586 psd->type |= SEC_DESC_DACL_PRESENT;
588 if (security_info_sent & SECINFO_SACL) {
589 psd->sacl = orig_psd->sacl;
590 psd->type |= SEC_DESC_SACL_PRESENT;
593 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
594 if (!NT_STATUS_IS_OK(status)) {
595 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
599 /* We got access denied here. If we're already root,
600 or we didn't need to do a chown, or the fsp isn't
601 open with WRITE_OWNER access, just return. */
602 if (get_current_uid(handle->conn) == 0 ||
603 chown_needed == false ||
604 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
606 return NT_STATUS_ACCESS_DENIED;
609 DEBUG(10,("fset_nt_acl_common: overriding chown on file %s "
612 sid_string_tos(psd->owner_sid)
615 /* Ok, we failed to chown and we have
616 SEC_STD_WRITE_OWNER access - override. */
618 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
619 security_info_sent, psd);
621 if (!NT_STATUS_IS_OK(status)) {
627 /* Get the full underlying sd, then hash. */
628 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
634 if (!NT_STATUS_IS_OK(status)) {
639 status = hash_sd_sha256(pdesc_next, hash);
640 if (!NT_STATUS_IS_OK(status)) {
645 if (DEBUGLEVEL >= 10) {
646 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s\n",
648 NDR_PRINT_DEBUG(security_descriptor,
649 discard_const_p(struct security_descriptor, psd));
651 DEBUG(10,("fset_nt_acl_xattr: storing has in xattr sd based on \n"));
652 NDR_PRINT_DEBUG(security_descriptor,
653 discard_const_p(struct security_descriptor, pdesc_next));
655 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
656 if (!NT_STATUS_IS_OK(status)) {
657 DEBUG(10, ("fset_nt_acl_xattr: create_acl_blob failed\n"));
662 status = store_acl_blob_fsp(handle, fsp, &blob);
668 static int acl_common_remove_object(vfs_handle_struct *handle,
672 connection_struct *conn = handle->conn;
674 files_struct *fsp = NULL;
676 char *parent_dir = NULL;
677 const char *final_component = NULL;
678 struct smb_filename local_fname;
680 char *saved_dir = NULL;
682 saved_dir = vfs_GetWd(talloc_tos(),conn);
688 if (!parent_dirname(talloc_tos(), path,
689 &parent_dir, &final_component)) {
690 saved_errno = ENOMEM;
694 DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
695 is_directory ? "directory" : "file",
696 parent_dir, final_component ));
698 /* cd into the parent dir to pin it. */
699 ret = vfs_ChDir(conn, parent_dir);
705 ZERO_STRUCT(local_fname);
706 local_fname.base_name = discard_const_p(char, final_component);
708 /* Must use lstat here. */
709 ret = SMB_VFS_LSTAT(conn, &local_fname);
715 /* Ensure we have this file open with DELETE access. */
716 id = vfs_file_id_from_sbuf(conn, &local_fname.st);
717 for (fsp = file_find_di_first(conn->sconn, id); fsp;
718 fsp = file_find_di_next(fsp)) {
719 if (fsp->access_mask & DELETE_ACCESS &&
720 fsp->delete_on_close) {
721 /* We did open this for delete,
722 * allow the delete as root.
729 DEBUG(10,("acl_common_remove_object: %s %s/%s "
730 "not an open file\n",
731 is_directory ? "directory" : "file",
732 parent_dir, final_component ));
733 saved_errno = EACCES;
739 ret = SMB_VFS_NEXT_RMDIR(handle, final_component);
741 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
751 TALLOC_FREE(parent_dir);
754 vfs_ChDir(conn, saved_dir);
762 static int rmdir_acl_common(struct vfs_handle_struct *handle,
767 /* Try the normal rmdir first. */
768 ret = SMB_VFS_NEXT_RMDIR(handle, path);
772 if (errno == EACCES || errno == EPERM) {
773 /* Failed due to access denied,
774 see if we need to root override. */
775 return acl_common_remove_object(handle,
780 DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
786 static int unlink_acl_common(struct vfs_handle_struct *handle,
787 const struct smb_filename *smb_fname)
791 /* Try the normal unlink first. */
792 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
796 if (errno == EACCES || errno == EPERM) {
797 /* Failed due to access denied,
798 see if we need to root override. */
800 /* Don't do anything fancy for streams. */
801 if (smb_fname->stream_name) {
804 return acl_common_remove_object(handle,
805 smb_fname->base_name,
809 DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
810 smb_fname->base_name,
815 static int chmod_acl_module_common(struct vfs_handle_struct *handle,
816 const char *path, mode_t mode)
818 if (lp_posix_pathnames()) {
819 /* Only allow this on POSIX pathnames. */
820 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
825 static int fchmod_acl_module_common(struct vfs_handle_struct *handle,
826 struct files_struct *fsp, mode_t mode)
828 if (fsp->posix_open) {
829 /* Only allow this on POSIX opens. */
830 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
835 static int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
836 const char *name, mode_t mode)
838 if (lp_posix_pathnames()) {
839 /* Only allow this on POSIX pathnames. */
840 return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
845 static int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
846 struct files_struct *fsp, mode_t mode)
848 if (fsp->posix_open) {
849 /* Only allow this on POSIX opens. */
850 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);