2 * Store Windows ACLs in data store - common functions.
4 * Copyright (C) Volker Lendecke, 2008
5 * Copyright (C) Jeremy Allison, 2009
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* NOTE: This is an experimental module, not yet finished. JRA. */
24 #include "librpc/gen_ndr/xattr.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
26 #include "../lib/crypto/crypto.h"
29 #define DBGC_CLASS DBGC_VFS
31 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
34 uint8_t hash[XATTR_SD_HASH_SIZE]);
36 #define HASH_SECURITY_INFO (OWNER_SECURITY_INFORMATION | \
37 GROUP_SECURITY_INFORMATION | \
38 DACL_SECURITY_INFORMATION | \
39 SACL_SECURITY_INFORMATION)
41 /*******************************************************************
42 Hash a security descriptor.
43 *******************************************************************/
45 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
52 memset(hash, '\0', XATTR_SD_HASH_SIZE);
53 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
54 if (!NT_STATUS_IS_OK(status)) {
59 SHA256_Update(&tctx, blob.data, blob.length);
60 SHA256_Final(hash, &tctx);
65 /*******************************************************************
66 Parse out a struct security_descriptor from a DATA_BLOB.
67 *******************************************************************/
69 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
70 struct security_descriptor **ppdesc,
71 uint16_t *p_hash_type,
72 uint8_t hash[XATTR_SD_HASH_SIZE])
74 TALLOC_CTX *ctx = talloc_tos();
75 struct xattr_NTACL xacl;
76 enum ndr_err_code ndr_err;
79 ndr_err = ndr_pull_struct_blob(pblob, ctx, NULL, &xacl,
80 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
82 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
83 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
84 ndr_errstr(ndr_err)));
85 return ndr_map_error2ntstatus(ndr_err);;
88 switch (xacl.version) {
90 *ppdesc = make_sec_desc(ctx, SEC_DESC_REVISION,
91 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
92 xacl.info.sd_hs2->sd->owner_sid,
93 xacl.info.sd_hs2->sd->group_sid,
94 xacl.info.sd_hs2->sd->sacl,
95 xacl.info.sd_hs2->sd->dacl,
97 /* No hash - null out. */
98 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
99 memset(hash, '\0', XATTR_SD_HASH_SIZE);
102 *ppdesc = make_sec_desc(ctx, SEC_DESC_REVISION,
103 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
104 xacl.info.sd_hs3->sd->owner_sid,
105 xacl.info.sd_hs3->sd->group_sid,
106 xacl.info.sd_hs3->sd->sacl,
107 xacl.info.sd_hs3->sd->dacl,
109 *p_hash_type = xacl.info.sd_hs3->hash_type;
110 /* Current version 3. */
111 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
114 return NT_STATUS_REVISION_MISMATCH;
117 TALLOC_FREE(xacl.info.sd);
119 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
122 /*******************************************************************
123 Create a DATA_BLOB from a security descriptor.
124 *******************************************************************/
126 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
129 uint8_t hash[XATTR_SD_HASH_SIZE])
131 struct xattr_NTACL xacl;
132 struct security_descriptor_hash_v3 sd_hs3;
133 enum ndr_err_code ndr_err;
134 TALLOC_CTX *ctx = talloc_tos();
140 xacl.info.sd_hs3 = &sd_hs3;
141 xacl.info.sd_hs3->sd = CONST_DISCARD(struct security_descriptor *, psd);
142 xacl.info.sd_hs3->hash_type = hash_type;
143 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
145 ndr_err = ndr_push_struct_blob(
146 pblob, ctx, NULL, &xacl,
147 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
149 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
150 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
151 ndr_errstr(ndr_err)));
152 return ndr_map_error2ntstatus(ndr_err);;
158 /*******************************************************************
159 Store a DATA_BLOB into an xattr given a pathname.
160 *******************************************************************/
162 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
165 uint32_t security_info,
166 struct security_descriptor **ppdesc)
171 uint8_t hash[XATTR_SD_HASH_SIZE];
172 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
173 struct security_descriptor *pdesc_next = NULL;
175 if (fsp && name == NULL) {
176 name = fsp->fsp_name->base_name;
179 DEBUG(10, ("get_nt_acl_internal: name=%s\n", name));
181 status = get_acl_blob(talloc_tos(), handle, fsp, name, &blob);
182 if (!NT_STATUS_IS_OK(status)) {
183 DEBUG(10, ("get_acl_blob returned %s\n", nt_errstr(status)));
187 status = parse_acl_blob(&blob, ppdesc,
188 &hash_type, &hash[0]);
189 if (!NT_STATUS_IS_OK(status)) {
190 DEBUG(10, ("parse_acl_blob returned %s\n",
195 /* Ensure the hash type is one we know. */
197 case XATTR_SD_HASH_TYPE_NONE:
198 /* No hash, goto return blob sd. */
200 case XATTR_SD_HASH_TYPE_SHA256:
203 return NT_STATUS_REVISION_MISMATCH;
206 /* Get the full underlying sd, then hash. */
208 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
213 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
219 if (!NT_STATUS_IS_OK(status)) {
223 status = hash_sd_sha256(pdesc_next, hash_tmp);
224 if (!NT_STATUS_IS_OK(status)) {
228 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
229 TALLOC_FREE(pdesc_next);
230 /* Hash matches, return blob sd. */
234 /* Hash doesn't match, return underlying sd. */
236 if (!(security_info & OWNER_SECURITY_INFORMATION)) {
237 pdesc_next->owner_sid = NULL;
239 if (!(security_info & GROUP_SECURITY_INFORMATION)) {
240 pdesc_next->group_sid = NULL;
242 if (!(security_info & DACL_SECURITY_INFORMATION)) {
243 pdesc_next->dacl = NULL;
245 if (!(security_info & SACL_SECURITY_INFORMATION)) {
246 pdesc_next->sacl = NULL;
249 TALLOC_FREE(*ppdesc);
250 *ppdesc = pdesc_next;
254 if (!(security_info & OWNER_SECURITY_INFORMATION)) {
255 (*ppdesc)->owner_sid = NULL;
257 if (!(security_info & GROUP_SECURITY_INFORMATION)) {
258 (*ppdesc)->group_sid = NULL;
260 if (!(security_info & DACL_SECURITY_INFORMATION)) {
261 (*ppdesc)->dacl = NULL;
263 if (!(security_info & SACL_SECURITY_INFORMATION)) {
264 (*ppdesc)->sacl = NULL;
267 TALLOC_FREE(blob.data);
271 /*********************************************************************
272 Create a default security descriptor for a file in case no inheritance
273 exists. All permissions to the owner and SYSTEM.
274 *********************************************************************/
276 static struct security_descriptor *default_file_sd(TALLOC_CTX *mem_ctx,
277 SMB_STRUCT_STAT *psbuf)
279 struct dom_sid owner_sid, group_sid;
281 struct security_ace *pace = NULL;
282 struct security_acl *pacl = NULL;
284 uid_to_sid(&owner_sid, psbuf->st_ex_uid);
285 gid_to_sid(&group_sid, psbuf->st_ex_gid);
287 pace = TALLOC_ARRAY(mem_ctx, struct security_ace, 2);
292 init_sec_ace(&pace[0], &owner_sid, SEC_ACE_TYPE_ACCESS_ALLOWED,
293 SEC_RIGHTS_FILE_ALL, 0);
294 init_sec_ace(&pace[1], &global_sid_System, SEC_ACE_TYPE_ACCESS_ALLOWED,
295 SEC_RIGHTS_FILE_ALL, 0);
297 pacl = make_sec_acl(mem_ctx,
304 return make_sec_desc(mem_ctx,
305 SECURITY_DESCRIPTOR_REVISION_1,
306 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
314 /*********************************************************************
315 *********************************************************************/
317 static NTSTATUS inherit_new_acl(vfs_handle_struct *handle,
318 struct smb_filename *smb_fname,
322 TALLOC_CTX *ctx = talloc_tos();
324 struct security_descriptor *parent_desc = NULL;
325 struct security_descriptor *psd = NULL;
326 struct security_descriptor *pdesc_next = NULL;
330 uint8_t hash[XATTR_SD_HASH_SIZE];
332 if (!parent_dirname(ctx, smb_fname->base_name, &parent_name, NULL)) {
333 return NT_STATUS_NO_MEMORY;
336 DEBUG(10,("inherit_new_acl: check directory %s\n",
339 status = get_nt_acl_internal(handle,
342 (OWNER_SECURITY_INFORMATION |
343 GROUP_SECURITY_INFORMATION |
344 DACL_SECURITY_INFORMATION),
346 if (NT_STATUS_IS_OK(status)) {
347 /* Create an inherited descriptor from the parent. */
349 if (DEBUGLEVEL >= 10) {
350 DEBUG(10,("inherit_new_acl: parent acl is:\n"));
351 NDR_PRINT_DEBUG(security_descriptor, parent_desc);
354 status = se_create_child_secdesc(ctx,
358 &handle->conn->server_info->ptok->user_sids[PRIMARY_USER_SID_INDEX],
359 &handle->conn->server_info->ptok->user_sids[PRIMARY_GROUP_SID_INDEX],
361 if (!NT_STATUS_IS_OK(status)) {
365 if (DEBUGLEVEL >= 10) {
366 DEBUG(10,("inherit_new_acl: child acl is:\n"));
367 NDR_PRINT_DEBUG(security_descriptor, psd);
371 DEBUG(10,("inherit_new_acl: directory %s failed "
374 nt_errstr(status) ));
377 if (!psd || psd->dacl == NULL) {
381 if (fsp && !fsp->is_directory && fsp->fh->fd != -1) {
382 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
384 if (fsp && fsp->posix_open) {
385 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
387 ret = SMB_VFS_STAT(handle->conn, smb_fname);
391 return map_nt_error_from_unix(errno);
393 psd = default_file_sd(ctx, &smb_fname->st);
395 return NT_STATUS_NO_MEMORY;
398 if (DEBUGLEVEL >= 10) {
399 DEBUG(10,("inherit_new_acl: default acl is:\n"));
400 NDR_PRINT_DEBUG(security_descriptor, psd);
404 /* Object exists. Read the current SD to get the hash. */
406 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
411 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
412 smb_fname->base_name,
417 if (!NT_STATUS_IS_OK(status)) {
421 status = hash_sd_sha256(pdesc_next, hash);
422 if (!NT_STATUS_IS_OK(status)) {
425 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
426 if (!NT_STATUS_IS_OK(status)) {
430 return store_acl_blob_fsp(handle, fsp, &blob);
432 return store_acl_blob_pathname(handle, smb_fname->base_name,
437 /*********************************************************************
438 Check ACL on open. For new files inherit from parent directory.
439 *********************************************************************/
441 int open_acl_common(vfs_handle_struct *handle,
442 struct smb_filename *smb_fname,
447 uint32_t access_granted = 0;
448 struct security_descriptor *pdesc = NULL;
449 bool file_existed = true;
454 /* Stream open. Base filename open already did the ACL check. */
455 DEBUG(10,("open_acl_common: stream open on %s\n",
456 smb_fname_str_dbg(smb_fname) ));
457 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
460 status = get_full_smb_filename(talloc_tos(), smb_fname,
462 if (!NT_STATUS_IS_OK(status)) {
463 errno = map_errno_from_nt_status(status);
467 status = get_nt_acl_internal(handle,
470 (OWNER_SECURITY_INFORMATION |
471 GROUP_SECURITY_INFORMATION |
472 DACL_SECURITY_INFORMATION),
474 if (NT_STATUS_IS_OK(status)) {
475 /* See if we can access it. */
476 status = smb1_file_se_access_check(pdesc,
477 handle->conn->server_info->ptok,
480 if (!NT_STATUS_IS_OK(status)) {
481 DEBUG(10,("open_acl_xattr: file %s open "
482 "refused with error %s\n",
483 smb_fname_str_dbg(smb_fname),
484 nt_errstr(status) ));
485 errno = map_errno_from_nt_status(status);
488 } else if (NT_STATUS_EQUAL(status,NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
489 file_existed = false;
492 DEBUG(10,("open_acl_xattr: get_nt_acl_attr_internal for "
493 "file %s returned %s\n",
494 smb_fname_str_dbg(smb_fname),
495 nt_errstr(status) ));
497 fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
499 if (!file_existed && fsp->fh->fd != -1) {
500 /* File was created. Inherit from parent directory. */
501 status = fsp_set_smb_fname(fsp, smb_fname);
502 if (!NT_STATUS_IS_OK(status)) {
503 errno = map_errno_from_nt_status(status);
506 inherit_new_acl(handle, smb_fname, fsp, false);
512 int mkdir_acl_common(vfs_handle_struct *handle, const char *path, mode_t mode)
514 struct smb_filename *smb_fname = NULL;
515 int ret = SMB_VFS_NEXT_MKDIR(handle, path, mode);
522 status = create_synthetic_smb_fname(talloc_tos(), path, NULL, NULL,
524 if (!NT_STATUS_IS_OK(status)) {
525 errno = map_errno_from_nt_status(status);
529 /* New directory - inherit from parent. */
530 inherit_new_acl(handle, smb_fname, NULL, true);
531 TALLOC_FREE(smb_fname);
535 /*********************************************************************
536 Fetch a security descriptor given an fsp.
537 *********************************************************************/
539 NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
540 uint32_t security_info, struct security_descriptor **ppdesc)
542 return get_nt_acl_internal(handle, fsp,
543 NULL, security_info, ppdesc);
546 /*********************************************************************
547 Fetch a security descriptor given a pathname.
548 *********************************************************************/
550 NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
551 const char *name, uint32_t security_info, struct security_descriptor **ppdesc)
553 return get_nt_acl_internal(handle, NULL,
554 name, security_info, ppdesc);
557 /*********************************************************************
558 Store a security descriptor given an fsp.
559 *********************************************************************/
561 NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
562 uint32_t security_info_sent, const struct security_descriptor *psd)
566 struct security_descriptor *pdesc_next = NULL;
567 uint8_t hash[XATTR_SD_HASH_SIZE];
569 if (DEBUGLEVEL >= 10) {
570 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
572 NDR_PRINT_DEBUG(security_descriptor,
573 CONST_DISCARD(struct security_descriptor *,psd));
576 /* Ensure owner and group are set. */
577 if (!psd->owner_sid || !psd->group_sid) {
579 DOM_SID owner_sid, group_sid;
580 struct security_descriptor *nc_psd = dup_sec_desc(talloc_tos(), psd);
585 if (fsp->is_directory || fsp->fh->fd == -1) {
586 if (fsp->posix_open) {
587 ret = SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
589 ret = SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
592 ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
595 /* Lower level acl set succeeded,
596 * so still return OK. */
599 create_file_sids(&fsp->fsp_name->st, &owner_sid, &group_sid);
600 /* This is safe as nc_psd is discarded at fn exit. */
601 nc_psd->owner_sid = &owner_sid;
602 nc_psd->group_sid = &group_sid;
603 security_info_sent |= (OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION);
607 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
608 if (!NT_STATUS_IS_OK(status)) {
612 /* Get the full underlying sd, then hash. */
613 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
618 if (!NT_STATUS_IS_OK(status)) {
622 status = hash_sd_sha256(pdesc_next, hash);
623 if (!NT_STATUS_IS_OK(status)) {
627 if (DEBUGLEVEL >= 10) {
628 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s\n",
630 NDR_PRINT_DEBUG(security_descriptor,
631 CONST_DISCARD(struct security_descriptor *,psd));
633 create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
634 store_acl_blob_fsp(handle, fsp, &blob);