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,
78 struct security_descriptor **ppdesc,
79 uint16_t *p_hash_type,
80 uint8_t hash[XATTR_SD_HASH_SIZE])
82 struct xattr_NTACL xacl;
83 enum ndr_err_code ndr_err;
85 TALLOC_CTX *frame = talloc_stackframe();
87 ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
88 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
90 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
91 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
92 ndr_errstr(ndr_err)));
94 return ndr_map_error2ntstatus(ndr_err);
97 switch (xacl.version) {
99 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
100 xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
101 xacl.info.sd->owner_sid,
102 xacl.info.sd->group_sid,
106 /* No hash - null out. */
107 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
108 memset(hash, '\0', XATTR_SD_HASH_SIZE);
111 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
112 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
113 xacl.info.sd_hs2->sd->owner_sid,
114 xacl.info.sd_hs2->sd->group_sid,
115 xacl.info.sd_hs2->sd->sacl,
116 xacl.info.sd_hs2->sd->dacl,
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_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
125 xacl.info.sd_hs3->sd->owner_sid,
126 xacl.info.sd_hs3->sd->group_sid,
127 xacl.info.sd_hs3->sd->sacl,
128 xacl.info.sd_hs3->sd->dacl,
130 *p_hash_type = xacl.info.sd_hs3->hash_type;
131 /* Current version 3. */
132 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
136 return NT_STATUS_REVISION_MISMATCH;
141 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
144 /*******************************************************************
145 Create a DATA_BLOB from a security descriptor.
146 *******************************************************************/
148 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
151 uint8_t hash[XATTR_SD_HASH_SIZE])
153 struct xattr_NTACL xacl;
154 struct security_descriptor_hash_v3 sd_hs3;
155 enum ndr_err_code ndr_err;
156 TALLOC_CTX *ctx = talloc_tos();
162 xacl.info.sd_hs3 = &sd_hs3;
163 xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
164 xacl.info.sd_hs3->hash_type = hash_type;
165 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
167 ndr_err = ndr_push_struct_blob(
169 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
171 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
172 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
173 ndr_errstr(ndr_err)));
174 return ndr_map_error2ntstatus(ndr_err);
180 /*******************************************************************
181 Add in 3 inheritable components for a non-inheritable directory ACL.
182 CREATOR_OWNER/CREATOR_GROUP/WORLD.
183 *******************************************************************/
185 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
187 SMB_STRUCT_STAT *psbuf,
188 struct security_descriptor *psd)
190 struct connection_struct *conn = handle->conn;
191 int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
192 struct smb_filename smb_fname;
193 enum security_ace_type acltype;
194 uint32_t access_mask;
198 struct security_ace *new_ace_list;
201 new_ace_list = talloc_zero_array(psd->dacl,
206 * make_sec_acl() at the bottom of this function
207 * dupliates new_ace_list
209 new_ace_list = talloc_zero_array(talloc_tos(),
214 if (new_ace_list == NULL) {
215 return NT_STATUS_NO_MEMORY;
218 /* Fake a quick smb_filename. */
219 ZERO_STRUCT(smb_fname);
220 smb_fname.st = *psbuf;
221 smb_fname.base_name = discard_const_p(char, name);
223 dir_mode = unix_mode(conn,
224 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
225 file_mode = unix_mode(conn,
226 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
228 mode = dir_mode | file_mode;
230 DEBUG(10, ("add_directory_inheritable_components: directory %s, "
233 (unsigned int)mode ));
236 memcpy(new_ace_list, psd->dacl->aces,
237 num_aces * sizeof(struct security_ace));
239 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
242 init_sec_ace(&new_ace_list[num_aces],
243 &global_sid_Creator_Owner,
246 SEC_ACE_FLAG_CONTAINER_INHERIT|
247 SEC_ACE_FLAG_OBJECT_INHERIT|
248 SEC_ACE_FLAG_INHERIT_ONLY);
249 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
250 (mode << 3) & 0700, false);
251 init_sec_ace(&new_ace_list[num_aces+1],
252 &global_sid_Creator_Group,
255 SEC_ACE_FLAG_CONTAINER_INHERIT|
256 SEC_ACE_FLAG_OBJECT_INHERIT|
257 SEC_ACE_FLAG_INHERIT_ONLY);
258 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
259 (mode << 6) & 0700, false);
260 init_sec_ace(&new_ace_list[num_aces+2],
264 SEC_ACE_FLAG_CONTAINER_INHERIT|
265 SEC_ACE_FLAG_OBJECT_INHERIT|
266 SEC_ACE_FLAG_INHERIT_ONLY);
268 psd->dacl->aces = new_ace_list;
269 psd->dacl->num_aces += 3;
271 psd->dacl = make_sec_acl(psd,
275 if (psd->dacl == NULL) {
276 return NT_STATUS_NO_MEMORY;
282 /*******************************************************************
283 Pull a DATA_BLOB from an xattr given a pathname.
284 If the hash doesn't match, or doesn't exist - return the underlying
286 *******************************************************************/
288 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
291 uint32_t security_info,
293 struct security_descriptor **ppdesc)
295 DATA_BLOB blob = data_blob_null;
297 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
298 uint8_t hash[XATTR_SD_HASH_SIZE];
299 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
300 struct security_descriptor *psd = NULL;
301 struct security_descriptor *pdesc_next = NULL;
302 bool ignore_file_system_acl = lp_parm_bool(SNUM(handle->conn),
304 "ignore system acls",
307 if (fsp && name == NULL) {
308 name = fsp->fsp_name->base_name;
311 DEBUG(10, ("get_nt_acl_internal: name=%s\n", name));
313 /* Get the full underlying sd for the hash
314 or to return as backup. */
316 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
322 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
329 if (!NT_STATUS_IS_OK(status)) {
330 DEBUG(10, ("get_nt_acl_internal: get_next_acl for file %s "
337 status = get_acl_blob(talloc_tos(), handle, fsp, name, &blob);
338 if (!NT_STATUS_IS_OK(status)) {
339 DEBUG(10, ("get_nt_acl_internal: get_acl_blob returned %s\n",
345 status = parse_acl_blob(&blob, mem_ctx, &psd,
346 &hash_type, &hash[0]);
347 if (!NT_STATUS_IS_OK(status)) {
348 DEBUG(10, ("parse_acl_blob returned %s\n",
354 /* Ensure the hash type is one we know. */
356 case XATTR_SD_HASH_TYPE_NONE:
357 /* No hash, just return blob sd. */
359 case XATTR_SD_HASH_TYPE_SHA256:
362 DEBUG(10, ("get_nt_acl_internal: ACL blob revision "
363 "mismatch (%u) for file %s\n",
364 (unsigned int)hash_type,
371 if (ignore_file_system_acl) {
375 status = hash_sd_sha256(pdesc_next, hash_tmp);
376 if (!NT_STATUS_IS_OK(status)) {
382 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
383 /* Hash matches, return blob sd. */
384 DEBUG(10, ("get_nt_acl_internal: blob hash "
385 "matches for file %s\n",
390 /* Hash doesn't match, return underlying sd. */
391 DEBUG(10, ("get_nt_acl_internal: blob hash "
392 "does not match for file %s - returning "
393 "file system SD mapping.\n",
396 if (DEBUGLEVEL >= 10) {
397 DEBUG(10,("get_nt_acl_internal: acl for blob hash for %s is:\n",
399 NDR_PRINT_DEBUG(security_descriptor, pdesc_next);
407 if (psd != pdesc_next) {
408 /* We're returning the blob, throw
409 * away the filesystem SD. */
410 TALLOC_FREE(pdesc_next);
412 SMB_STRUCT_STAT sbuf;
413 SMB_STRUCT_STAT *psbuf = &sbuf;
414 bool is_directory = false;
416 * We're returning the underlying ACL from the
417 * filesystem. If it's a directory, and has no
418 * inheritable ACE entries we have to fake them.
421 status = vfs_stat_fsp(fsp);
422 if (!NT_STATUS_IS_OK(status)) {
425 psbuf = &fsp->fsp_name->st;
427 int ret = vfs_stat_smb_fname(handle->conn,
431 return map_nt_error_from_unix(errno);
434 is_directory = S_ISDIR(psbuf->st_ex_mode);
436 if (ignore_file_system_acl) {
437 TALLOC_FREE(pdesc_next);
438 status = make_default_filesystem_acl(mem_ctx,
442 if (!NT_STATUS_IS_OK(status)) {
447 !sd_has_inheritable_components(psd,
449 status = add_directory_inheritable_components(
454 if (!NT_STATUS_IS_OK(status)) {
458 /* The underlying POSIX module always sets
459 the ~SEC_DESC_DACL_PROTECTED bit, as ACLs
460 can't be inherited in this way under POSIX.
461 Remove it for Windows-style ACLs. */
462 psd->type &= ~SEC_DESC_DACL_PROTECTED;
466 if (!(security_info & SECINFO_OWNER)) {
467 psd->owner_sid = NULL;
469 if (!(security_info & SECINFO_GROUP)) {
470 psd->group_sid = NULL;
472 if (!(security_info & SECINFO_DACL)) {
473 psd->type &= ~SEC_DESC_DACL_PRESENT;
476 if (!(security_info & SECINFO_SACL)) {
477 psd->type &= ~SEC_DESC_SACL_PRESENT;
481 TALLOC_FREE(blob.data);
484 if (DEBUGLEVEL >= 10) {
485 DEBUG(10,("get_nt_acl_internal: returning acl for %s is:\n",
487 NDR_PRINT_DEBUG(security_descriptor, psd);
493 /*********************************************************************
494 Fetch a security descriptor given an fsp.
495 *********************************************************************/
497 static NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle,
499 uint32_t security_info,
501 struct security_descriptor **ppdesc)
503 return get_nt_acl_internal(handle, fsp,
504 NULL, security_info, mem_ctx, ppdesc);
507 /*********************************************************************
508 Fetch a security descriptor given a pathname.
509 *********************************************************************/
511 static NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
513 uint32_t security_info,
515 struct security_descriptor **ppdesc)
517 return get_nt_acl_internal(handle, NULL,
518 name, security_info, mem_ctx, ppdesc);
521 /*********************************************************************
522 Store a security descriptor given an fsp.
523 *********************************************************************/
525 static NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
526 uint32_t security_info_sent, const struct security_descriptor *orig_psd)
530 struct security_descriptor *pdesc_next = NULL;
531 struct security_descriptor *psd = NULL;
532 uint8_t hash[XATTR_SD_HASH_SIZE];
533 bool chown_needed = false;
534 TALLOC_CTX *frame = talloc_stackframe();
536 if (DEBUGLEVEL >= 10) {
537 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
539 NDR_PRINT_DEBUG(security_descriptor,
540 discard_const_p(struct security_descriptor, orig_psd));
543 status = get_nt_acl_internal(handle, fsp,
545 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
549 if (!NT_STATUS_IS_OK(status)) {
554 psd->revision = orig_psd->revision;
555 /* All our SD's are self relative. */
556 psd->type = orig_psd->type | SEC_DESC_SELF_RELATIVE;
558 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
559 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
560 /* We're changing the owner. */
563 psd->owner_sid = orig_psd->owner_sid;
565 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
566 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
567 /* We're changing the group. */
570 psd->group_sid = orig_psd->group_sid;
572 if (security_info_sent & SECINFO_DACL) {
573 psd->dacl = orig_psd->dacl;
574 psd->type |= SEC_DESC_DACL_PRESENT;
576 if (security_info_sent & SECINFO_SACL) {
577 psd->sacl = orig_psd->sacl;
578 psd->type |= SEC_DESC_SACL_PRESENT;
581 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
582 if (!NT_STATUS_IS_OK(status)) {
583 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
587 /* We got access denied here. If we're already root,
588 or we didn't need to do a chown, or the fsp isn't
589 open with WRITE_OWNER access, just return. */
590 if (get_current_uid(handle->conn) == 0 ||
591 chown_needed == false ||
592 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
593 return NT_STATUS_ACCESS_DENIED;
596 DEBUG(10,("fset_nt_acl_common: overriding chown on file %s "
599 sid_string_tos(psd->owner_sid)
602 /* Ok, we failed to chown and we have
603 SEC_STD_WRITE_OWNER access - override. */
605 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
606 security_info_sent, psd);
608 if (!NT_STATUS_IS_OK(status)) {
614 /* Get the full underlying sd, then hash. */
615 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
621 if (!NT_STATUS_IS_OK(status)) {
626 status = hash_sd_sha256(pdesc_next, hash);
627 if (!NT_STATUS_IS_OK(status)) {
632 if (DEBUGLEVEL >= 10) {
633 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s\n",
635 NDR_PRINT_DEBUG(security_descriptor,
636 discard_const_p(struct security_descriptor, psd));
638 DEBUG(10,("fset_nt_acl_xattr: storing has in xattr sd based on \n"));
639 NDR_PRINT_DEBUG(security_descriptor,
640 discard_const_p(struct security_descriptor, pdesc_next));
642 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
643 if (!NT_STATUS_IS_OK(status)) {
644 DEBUG(10, ("fset_nt_acl_xattr: create_acl_blob failed\n"));
649 status = store_acl_blob_fsp(handle, fsp, &blob);
655 static int acl_common_remove_object(vfs_handle_struct *handle,
659 connection_struct *conn = handle->conn;
661 files_struct *fsp = NULL;
663 char *parent_dir = NULL;
664 const char *final_component = NULL;
665 struct smb_filename local_fname;
667 char *saved_dir = NULL;
669 saved_dir = vfs_GetWd(talloc_tos(),conn);
675 if (!parent_dirname(talloc_tos(), path,
676 &parent_dir, &final_component)) {
677 saved_errno = ENOMEM;
681 DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
682 is_directory ? "directory" : "file",
683 parent_dir, final_component ));
685 /* cd into the parent dir to pin it. */
686 ret = vfs_ChDir(conn, parent_dir);
692 ZERO_STRUCT(local_fname);
693 local_fname.base_name = discard_const_p(char, final_component);
695 /* Must use lstat here. */
696 ret = SMB_VFS_LSTAT(conn, &local_fname);
702 /* Ensure we have this file open with DELETE access. */
703 id = vfs_file_id_from_sbuf(conn, &local_fname.st);
704 for (fsp = file_find_di_first(conn->sconn, id); fsp;
705 fsp = file_find_di_next(fsp)) {
706 if (fsp->access_mask & DELETE_ACCESS &&
707 fsp->delete_on_close) {
708 /* We did open this for delete,
709 * allow the delete as root.
716 DEBUG(10,("acl_common_remove_object: %s %s/%s "
717 "not an open file\n",
718 is_directory ? "directory" : "file",
719 parent_dir, final_component ));
720 saved_errno = EACCES;
726 ret = SMB_VFS_NEXT_RMDIR(handle, final_component);
728 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
738 TALLOC_FREE(parent_dir);
741 vfs_ChDir(conn, saved_dir);
749 static int rmdir_acl_common(struct vfs_handle_struct *handle,
754 /* Try the normal rmdir first. */
755 ret = SMB_VFS_NEXT_RMDIR(handle, path);
759 if (errno == EACCES || errno == EPERM) {
760 /* Failed due to access denied,
761 see if we need to root override. */
762 return acl_common_remove_object(handle,
767 DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
773 static int unlink_acl_common(struct vfs_handle_struct *handle,
774 const struct smb_filename *smb_fname)
778 /* Try the normal unlink first. */
779 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
783 if (errno == EACCES || errno == EPERM) {
784 /* Failed due to access denied,
785 see if we need to root override. */
787 /* Don't do anything fancy for streams. */
788 if (smb_fname->stream_name) {
791 return acl_common_remove_object(handle,
792 smb_fname->base_name,
796 DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
797 smb_fname->base_name,
802 static int chmod_acl_module_common(struct vfs_handle_struct *handle,
803 const char *path, mode_t mode)
805 if (lp_posix_pathnames()) {
806 /* Only allow this on POSIX pathnames. */
807 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
812 static int fchmod_acl_module_common(struct vfs_handle_struct *handle,
813 struct files_struct *fsp, mode_t mode)
815 if (fsp->posix_open) {
816 /* Only allow this on POSIX opens. */
817 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
822 static int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
823 const char *name, mode_t mode)
825 if (lp_posix_pathnames()) {
826 /* Only allow this on POSIX pathnames. */
827 return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
832 static int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
833 struct files_struct *fsp, mode_t mode)
835 if (fsp->posix_open) {
836 /* Only allow this on POSIX opens. */
837 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);