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 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
25 uint8_t hash[XATTR_SD_HASH_SIZE]);
27 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
28 vfs_handle_struct *handle,
33 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
37 static NTSTATUS store_acl_blob_pathname(vfs_handle_struct *handle,
41 #define HASH_SECURITY_INFO (OWNER_SECURITY_INFORMATION | \
42 GROUP_SECURITY_INFORMATION | \
43 DACL_SECURITY_INFORMATION | \
44 SACL_SECURITY_INFORMATION)
46 /*******************************************************************
47 Hash a security descriptor.
48 *******************************************************************/
50 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
57 memset(hash, '\0', XATTR_SD_HASH_SIZE);
58 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
59 if (!NT_STATUS_IS_OK(status)) {
64 SHA256_Update(&tctx, blob.data, blob.length);
65 SHA256_Final(hash, &tctx);
70 /*******************************************************************
71 Parse out a struct security_descriptor from a DATA_BLOB.
72 *******************************************************************/
74 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
75 struct security_descriptor **ppdesc,
76 uint16_t *p_hash_type,
77 uint8_t hash[XATTR_SD_HASH_SIZE])
79 TALLOC_CTX *ctx = talloc_tos();
80 struct xattr_NTACL xacl;
81 enum ndr_err_code ndr_err;
84 ndr_err = ndr_pull_struct_blob(pblob, ctx, NULL, &xacl,
85 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
87 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
88 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
89 ndr_errstr(ndr_err)));
90 return ndr_map_error2ntstatus(ndr_err);;
93 switch (xacl.version) {
95 *ppdesc = make_sec_desc(ctx, SEC_DESC_REVISION,
96 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
97 xacl.info.sd_hs2->sd->owner_sid,
98 xacl.info.sd_hs2->sd->group_sid,
99 xacl.info.sd_hs2->sd->sacl,
100 xacl.info.sd_hs2->sd->dacl,
102 /* No hash - null out. */
103 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
104 memset(hash, '\0', XATTR_SD_HASH_SIZE);
107 *ppdesc = make_sec_desc(ctx, SEC_DESC_REVISION,
108 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
109 xacl.info.sd_hs3->sd->owner_sid,
110 xacl.info.sd_hs3->sd->group_sid,
111 xacl.info.sd_hs3->sd->sacl,
112 xacl.info.sd_hs3->sd->dacl,
114 *p_hash_type = xacl.info.sd_hs3->hash_type;
115 /* Current version 3. */
116 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
119 return NT_STATUS_REVISION_MISMATCH;
122 TALLOC_FREE(xacl.info.sd);
124 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
127 /*******************************************************************
128 Create a DATA_BLOB from a security descriptor.
129 *******************************************************************/
131 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
134 uint8_t hash[XATTR_SD_HASH_SIZE])
136 struct xattr_NTACL xacl;
137 struct security_descriptor_hash_v3 sd_hs3;
138 enum ndr_err_code ndr_err;
139 TALLOC_CTX *ctx = talloc_tos();
145 xacl.info.sd_hs3 = &sd_hs3;
146 xacl.info.sd_hs3->sd = CONST_DISCARD(struct security_descriptor *, psd);
147 xacl.info.sd_hs3->hash_type = hash_type;
148 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
150 ndr_err = ndr_push_struct_blob(
151 pblob, ctx, NULL, &xacl,
152 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
154 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
155 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
156 ndr_errstr(ndr_err)));
157 return ndr_map_error2ntstatus(ndr_err);;
163 /*******************************************************************
164 Pull a DATA_BLOB from an xattr given a pathname.
165 DOES NOT FALL BACK TO THE UNDERLYING ACLs ON THE FILESYSTEM.
166 *******************************************************************/
168 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
171 uint32_t security_info,
172 struct security_descriptor **ppdesc)
177 uint8_t hash[XATTR_SD_HASH_SIZE];
178 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
179 struct security_descriptor *pdesc_next = NULL;
181 if (fsp && name == NULL) {
182 name = fsp->fsp_name->base_name;
185 DEBUG(10, ("get_nt_acl_internal: name=%s\n", name));
187 status = get_acl_blob(talloc_tos(), handle, fsp, name, &blob);
188 if (!NT_STATUS_IS_OK(status)) {
189 DEBUG(10, ("get_nt_acl_internal: get_acl_blob returned %s\n",
194 status = parse_acl_blob(&blob, ppdesc,
195 &hash_type, &hash[0]);
196 if (!NT_STATUS_IS_OK(status)) {
197 DEBUG(10, ("parse_acl_blob returned %s\n",
202 /* Ensure the hash type is one we know. */
204 case XATTR_SD_HASH_TYPE_NONE:
205 /* No hash, goto return blob sd. */
207 case XATTR_SD_HASH_TYPE_SHA256:
210 return NT_STATUS_REVISION_MISMATCH;
213 /* Get the full underlying sd, then hash. */
215 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
220 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
226 if (!NT_STATUS_IS_OK(status)) {
230 status = hash_sd_sha256(pdesc_next, hash_tmp);
231 if (!NT_STATUS_IS_OK(status)) {
235 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
236 TALLOC_FREE(pdesc_next);
237 /* Hash matches, return blob sd. */
241 /* Hash doesn't match, return underlying sd. */
243 if (!(security_info & OWNER_SECURITY_INFORMATION)) {
244 pdesc_next->owner_sid = NULL;
246 if (!(security_info & GROUP_SECURITY_INFORMATION)) {
247 pdesc_next->group_sid = NULL;
249 if (!(security_info & DACL_SECURITY_INFORMATION)) {
250 pdesc_next->dacl = NULL;
252 if (!(security_info & SACL_SECURITY_INFORMATION)) {
253 pdesc_next->sacl = NULL;
256 TALLOC_FREE(*ppdesc);
257 *ppdesc = pdesc_next;
261 if (!(security_info & OWNER_SECURITY_INFORMATION)) {
262 (*ppdesc)->owner_sid = NULL;
264 if (!(security_info & GROUP_SECURITY_INFORMATION)) {
265 (*ppdesc)->group_sid = NULL;
267 if (!(security_info & DACL_SECURITY_INFORMATION)) {
268 (*ppdesc)->dacl = NULL;
270 if (!(security_info & SACL_SECURITY_INFORMATION)) {
271 (*ppdesc)->sacl = NULL;
274 TALLOC_FREE(blob.data);
278 /*********************************************************************
279 Create a default security descriptor for a file in case no inheritance
280 exists. All permissions to the owner and SYSTEM.
281 *********************************************************************/
283 static struct security_descriptor *default_file_sd(TALLOC_CTX *mem_ctx,
284 SMB_STRUCT_STAT *psbuf,
287 struct dom_sid owner_sid, group_sid;
289 struct security_ace *pace = NULL;
290 struct security_acl *pacl = NULL;
292 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
293 gid_to_sid(&group_sid, psbuf->st_ex_gid);
295 pace = TALLOC_ARRAY(mem_ctx, struct security_ace, 2);
300 /* If force_inherit is set, this means we are initializing the ACEs for
301 * a container and we want the ACEs for owner_sid and "SYSTEM" to be
302 * inheritable by their children (See Bug #6802).
305 init_sec_ace(&pace[0], &owner_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
306 SEC_RIGHTS_FILE_ALL, (force_inherit ?
307 (SEC_ACE_FLAG_OBJECT_INHERIT|
308 SEC_ACE_FLAG_CONTAINER_INHERIT) :
311 init_sec_ace(&pace[1], &global_sid_System, SEC_ACE_TYPE_ACCESS_ALLOWED,
312 SEC_RIGHTS_FILE_ALL, (force_inherit ?
313 (SEC_ACE_FLAG_OBJECT_INHERIT|
314 SEC_ACE_FLAG_CONTAINER_INHERIT) :
317 pacl = make_sec_acl(mem_ctx,
324 return make_sec_desc(mem_ctx,
325 SECURITY_DESCRIPTOR_REVISION_1,
326 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
334 /*********************************************************************
335 *********************************************************************/
337 static NTSTATUS inherit_new_acl(vfs_handle_struct *handle,
338 struct smb_filename *smb_fname,
342 TALLOC_CTX *ctx = talloc_tos();
344 struct security_descriptor *parent_desc = NULL;
345 struct security_descriptor *psd = NULL;
346 struct security_descriptor *pdesc_next = NULL;
350 bool force_inherit = false;
351 uint8_t hash[XATTR_SD_HASH_SIZE];
353 if (!parent_dirname(ctx, smb_fname->base_name, &parent_name, NULL)) {
354 return NT_STATUS_NO_MEMORY;
357 DEBUG(10,("inherit_new_acl: check directory %s\n",
360 status = get_nt_acl_internal(handle,
363 (OWNER_SECURITY_INFORMATION |
364 GROUP_SECURITY_INFORMATION |
365 DACL_SECURITY_INFORMATION),
367 if (NT_STATUS_IS_OK(status)) {
368 /* Create an inherited descriptor from the parent. */
370 if (DEBUGLEVEL >= 10) {
371 DEBUG(10,("inherit_new_acl: parent acl is:\n"));
372 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
375 status = se_create_child_secdesc(ctx,
379 &handle->conn->server_info->ptok->user_sids[PRIMARY_USER_SID_INDEX],
380 &handle->conn->server_info->ptok->user_sids[PRIMARY_GROUP_SID_INDEX],
382 if (!NT_STATUS_IS_OK(status)) {
386 if (DEBUGLEVEL >= 10) {
387 DEBUG(10,("inherit_new_acl: child acl is:\n"));
388 NDR_PRINT_DEBUG(security_descriptor, psd);
392 DEBUG(10,("inherit_new_acl: directory %s failed "
395 nt_errstr(status) ));
398 if (!psd || psd->dacl == NULL) {
402 status = vfs_stat_fsp(fsp);
403 smb_fname->st = fsp->fsp_name->st;
406 if (lp_posix_pathnames()) {
407 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
409 ret = SMB_VFS_STAT(handle->conn, smb_fname);
412 status = map_nt_error_from_unix(errno);
415 if (!NT_STATUS_IS_OK(status)) {
419 /* If we get here, we could have the following possibilities:
420 * 1. No ACLs exist on the parent container.
421 * 2. ACLs exist on the parent container but they were
424 * Check to see if case #1 occurred.
428 (parent_desc == NULL || parent_desc->dacl == NULL)) {
430 /* If no parent descriptor exists, then there were
431 * no ACLs on the parent and then we must create
432 * the ACLs on this newly created folder so that they
433 * will be inherited by their children (See Bug #6802).
436 force_inherit = true;
439 psd = default_file_sd(ctx, &smb_fname->st, force_inherit);
441 return NT_STATUS_NO_MEMORY;
444 if (DEBUGLEVEL >= 10) {
445 DEBUG(10,("inherit_new_acl: default acl is:\n"));
446 NDR_PRINT_DEBUG(security_descriptor, psd);
450 /* Object exists. Read the current SD to get the hash. */
452 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
457 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
458 smb_fname->base_name,
463 if (!NT_STATUS_IS_OK(status)) {
467 status = hash_sd_sha256(pdesc_next, hash);
468 if (!NT_STATUS_IS_OK(status)) {
471 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
472 if (!NT_STATUS_IS_OK(status)) {
476 return store_acl_blob_fsp(handle, fsp, &blob);
478 return store_acl_blob_pathname(handle, smb_fname->base_name,
483 static NTSTATUS check_parent_acl_common(vfs_handle_struct *handle,
485 uint32_t access_mask)
487 char *parent_name = NULL;
488 struct security_descriptor *parent_desc = NULL;
489 uint32_t access_granted = 0;
492 if (!parent_dirname(talloc_tos(), path, &parent_name, NULL)) {
493 return NT_STATUS_NO_MEMORY;
496 status = get_nt_acl_internal(handle,
499 (OWNER_SECURITY_INFORMATION |
500 GROUP_SECURITY_INFORMATION |
501 DACL_SECURITY_INFORMATION),
504 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
505 /* No Windows ACL stored as a blob. Let the
506 * underlying filesystem take care of checking
508 DEBUG(10,("check_parent_acl_common: no Windows ACL blob "
509 "stored on directory %s for "
516 if (!NT_STATUS_IS_OK(status)) {
517 DEBUG(10,("check_parent_acl_common: get_nt_acl_internal "
518 "on directory %s for "
519 "path %s returned %s\n",
522 nt_errstr(status) ));
525 status = smb1_file_se_access_check(parent_desc,
526 handle->conn->server_info->ptok,
529 if(!NT_STATUS_IS_OK(status)) {
530 DEBUG(10,("check_parent_acl_common: access check "
531 "on directory %s for "
532 "path %s for mask 0x%x returned %s\n",
536 nt_errstr(status) ));
542 /*********************************************************************
543 Check ACL on open. For new files inherit from parent directory.
544 *********************************************************************/
546 static int open_acl_common(vfs_handle_struct *handle,
547 struct smb_filename *smb_fname,
552 uint32_t access_granted = 0;
553 struct security_descriptor *pdesc = NULL;
554 bool file_existed = true;
559 /* Stream open. Base filename open already did the ACL check. */
560 DEBUG(10,("open_acl_common: stream open on %s\n",
561 smb_fname_str_dbg(smb_fname) ));
562 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
565 status = get_full_smb_filename(talloc_tos(), smb_fname,
567 if (!NT_STATUS_IS_OK(status)) {
571 status = get_nt_acl_internal(handle,
574 (OWNER_SECURITY_INFORMATION |
575 GROUP_SECURITY_INFORMATION |
576 DACL_SECURITY_INFORMATION),
578 if (NT_STATUS_IS_OK(status)) {
579 /* See if we can access it. */
580 status = smb1_file_se_access_check(pdesc,
581 handle->conn->server_info->ptok,
584 if (!NT_STATUS_IS_OK(status)) {
585 DEBUG(10,("open_acl_xattr: file %s open "
586 "refused with error %s\n",
587 smb_fname_str_dbg(smb_fname),
588 nt_errstr(status) ));
591 } else if (NT_STATUS_EQUAL(status,NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
592 file_existed = false;
594 * If O_CREAT is true then we're trying to create a file.
595 * Check the parent directory ACL will allow this.
597 if (flags & O_CREAT) {
598 status = check_parent_acl_common(handle, fname,
600 if (!NT_STATUS_IS_OK(status)) {
606 DEBUG(10,("open_acl_xattr: get_nt_acl_attr_internal for "
607 "file %s returned %s\n",
608 smb_fname_str_dbg(smb_fname),
609 nt_errstr(status) ));
611 fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
613 if (!file_existed && fsp->fh->fd != -1) {
614 /* File was created. Inherit from parent directory. */
615 status = fsp_set_smb_fname(fsp, smb_fname);
616 if (!NT_STATUS_IS_OK(status)) {
619 inherit_new_acl(handle, smb_fname, fsp, false);
626 errno = map_errno_from_nt_status(status);
630 static int mkdir_acl_common(vfs_handle_struct *handle, const char *path, mode_t mode)
632 struct smb_filename *smb_fname = NULL;
635 SMB_STRUCT_STAT sbuf;
637 ret = vfs_stat_smb_fname(handle->conn, path, &sbuf);
638 if (ret == -1 && errno == ENOENT) {
639 /* We're creating a new directory. */
640 status = check_parent_acl_common(handle, path,
642 if (!NT_STATUS_IS_OK(status)) {
643 errno = map_errno_from_nt_status(status);
648 ret = SMB_VFS_NEXT_MKDIR(handle, path, mode);
653 status = create_synthetic_smb_fname(talloc_tos(), path, NULL, NULL,
655 if (!NT_STATUS_IS_OK(status)) {
656 errno = map_errno_from_nt_status(status);
660 /* New directory - inherit from parent. */
661 inherit_new_acl(handle, smb_fname, NULL, true);
662 TALLOC_FREE(smb_fname);
666 /*********************************************************************
667 Fetch a security descriptor given an fsp.
668 *********************************************************************/
670 static NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
671 uint32_t security_info, struct security_descriptor **ppdesc)
673 NTSTATUS status = get_nt_acl_internal(handle, fsp,
674 NULL, security_info, ppdesc);
675 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
676 /* Pull the ACL from the underlying system. */
677 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
685 /*********************************************************************
686 Fetch a security descriptor given a pathname.
687 *********************************************************************/
689 static NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
690 const char *name, uint32_t security_info, struct security_descriptor **ppdesc)
692 NTSTATUS status = get_nt_acl_internal(handle, NULL,
693 name, security_info, ppdesc);
694 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
695 /* Pull the ACL from the underlying system. */
696 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
704 /*********************************************************************
705 Store a security descriptor given an fsp.
706 *********************************************************************/
708 static NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
709 uint32_t security_info_sent, const struct security_descriptor *psd)
713 struct security_descriptor *pdesc_next = NULL;
714 uint8_t hash[XATTR_SD_HASH_SIZE];
716 if (DEBUGLEVEL >= 10) {
717 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
719 NDR_PRINT_DEBUG(security_descriptor,
720 CONST_DISCARD(struct security_descriptor *,psd));
723 /* Ensure we have OWNER/GROUP/DACL set. */
725 if ((security_info_sent & (OWNER_SECURITY_INFORMATION|
726 GROUP_SECURITY_INFORMATION|
727 DACL_SECURITY_INFORMATION)) !=
728 (OWNER_SECURITY_INFORMATION|
729 GROUP_SECURITY_INFORMATION|
730 DACL_SECURITY_INFORMATION)) {
731 /* No we don't - read from the existing SD. */
732 struct security_descriptor *nc_psd = NULL;
734 status = get_nt_acl_internal(handle, fsp,
736 (OWNER_SECURITY_INFORMATION|
737 GROUP_SECURITY_INFORMATION|
738 DACL_SECURITY_INFORMATION),
741 if (!NT_STATUS_IS_OK(status)) {
745 /* This is safe as nc_psd is discarded at fn exit. */
746 if (security_info_sent & OWNER_SECURITY_INFORMATION) {
747 nc_psd->owner_sid = psd->owner_sid;
749 security_info_sent |= OWNER_SECURITY_INFORMATION;
751 if (security_info_sent & GROUP_SECURITY_INFORMATION) {
752 nc_psd->group_sid = psd->group_sid;
754 security_info_sent |= GROUP_SECURITY_INFORMATION;
756 if (security_info_sent & DACL_SECURITY_INFORMATION) {
757 nc_psd->dacl = dup_sec_acl(talloc_tos(), psd->dacl);
758 if (nc_psd->dacl == NULL) {
759 return NT_STATUS_NO_MEMORY;
762 security_info_sent |= DACL_SECURITY_INFORMATION;
766 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
767 if (!NT_STATUS_IS_OK(status)) {
771 /* Get the full underlying sd, then hash. */
772 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
777 if (!NT_STATUS_IS_OK(status)) {
781 status = hash_sd_sha256(pdesc_next, hash);
782 if (!NT_STATUS_IS_OK(status)) {
786 if (DEBUGLEVEL >= 10) {
787 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s\n",
789 NDR_PRINT_DEBUG(security_descriptor,
790 CONST_DISCARD(struct security_descriptor *,psd));
792 create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
793 store_acl_blob_fsp(handle, fsp, &blob);
798 static SMB_STRUCT_DIR *opendir_acl_common(vfs_handle_struct *handle,
799 const char *fname, const char *mask, uint32 attr)
801 NTSTATUS status = check_parent_acl_common(handle, fname, SEC_DIR_LIST);
803 if (!NT_STATUS_IS_OK(status)) {
804 errno = map_errno_from_nt_status(status);
807 return SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);