s4-dsdb: Tests for security checks on undelete operation
[Samba.git] / source3 / modules / vfs_acl_common.c
blob920c811de5e57494ab84a1f6d9a2e5ee31d072f1
1 /*
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,
29 DATA_BLOB *pblob,
30 uint16_t hash_type,
31 uint8_t hash[XATTR_SD_HASH_SIZE]);
33 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
34 vfs_handle_struct *handle,
35 files_struct *fsp,
36 const char *name,
37 DATA_BLOB *pblob);
39 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
40 files_struct *fsp,
41 DATA_BLOB *pblob);
43 #define HASH_SECURITY_INFO (SECINFO_OWNER | \
44 SECINFO_GROUP | \
45 SECINFO_DACL | \
46 SECINFO_SACL)
48 /*******************************************************************
49 Hash a security descriptor.
50 *******************************************************************/
52 static NTSTATUS hash_blob_sha256(DATA_BLOB blob,
53 uint8_t *hash)
55 SHA256_CTX tctx;
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);
63 return NT_STATUS_OK;
66 /*******************************************************************
67 Hash a security descriptor.
68 *******************************************************************/
70 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
71 uint8_t *hash)
73 DATA_BLOB blob;
74 NTSTATUS status;
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)) {
79 return 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,
89 TALLOC_CTX *mem_ctx,
90 struct security_descriptor **ppdesc,
91 uint16_t *p_hash_type,
92 uint16_t *p_version,
93 uint8_t hash[XATTR_SD_HASH_SIZE],
94 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
96 struct xattr_NTACL xacl;
97 enum ndr_err_code ndr_err;
98 size_t sd_size;
99 TALLOC_CTX *frame = talloc_stackframe();
101 ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
102 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
104 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
105 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
106 ndr_errstr(ndr_err)));
107 TALLOC_FREE(frame);
108 return ndr_map_error2ntstatus(ndr_err);
111 *p_version = xacl.version;
113 switch (xacl.version) {
114 case 1:
115 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
116 xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
117 xacl.info.sd->owner_sid,
118 xacl.info.sd->group_sid,
119 xacl.info.sd->sacl,
120 xacl.info.sd->dacl,
121 &sd_size);
122 /* No hash - null out. */
123 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
124 memset(hash, '\0', XATTR_SD_HASH_SIZE);
125 break;
126 case 2:
127 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
128 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
129 xacl.info.sd_hs2->sd->owner_sid,
130 xacl.info.sd_hs2->sd->group_sid,
131 xacl.info.sd_hs2->sd->sacl,
132 xacl.info.sd_hs2->sd->dacl,
133 &sd_size);
134 /* No hash - null out. */
135 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
136 memset(hash, '\0', XATTR_SD_HASH_SIZE);
137 break;
138 case 3:
139 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
140 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
141 xacl.info.sd_hs3->sd->owner_sid,
142 xacl.info.sd_hs3->sd->group_sid,
143 xacl.info.sd_hs3->sd->sacl,
144 xacl.info.sd_hs3->sd->dacl,
145 &sd_size);
146 *p_hash_type = xacl.info.sd_hs3->hash_type;
147 /* Current version 3 (if no sys acl hash available). */
148 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
149 break;
150 case 4:
151 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
152 xacl.info.sd_hs4->sd->type | SEC_DESC_SELF_RELATIVE,
153 xacl.info.sd_hs4->sd->owner_sid,
154 xacl.info.sd_hs4->sd->group_sid,
155 xacl.info.sd_hs4->sd->sacl,
156 xacl.info.sd_hs4->sd->dacl,
157 &sd_size);
158 *p_hash_type = xacl.info.sd_hs4->hash_type;
159 /* Current version 4. */
160 memcpy(hash, xacl.info.sd_hs4->hash, XATTR_SD_HASH_SIZE);
161 memcpy(sys_acl_hash, xacl.info.sd_hs4->sys_acl_hash, XATTR_SD_HASH_SIZE);
162 break;
163 default:
164 TALLOC_FREE(frame);
165 return NT_STATUS_REVISION_MISMATCH;
168 TALLOC_FREE(frame);
170 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
173 /*******************************************************************
174 Create a DATA_BLOB from a hash of the security descriptor storead at
175 the system layer and the NT ACL we wish to preserve
176 *******************************************************************/
178 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
179 DATA_BLOB *pblob,
180 uint16_t hash_type,
181 uint8_t hash[XATTR_SD_HASH_SIZE])
183 struct xattr_NTACL xacl;
184 struct security_descriptor_hash_v3 sd_hs3;
185 enum ndr_err_code ndr_err;
186 TALLOC_CTX *ctx = talloc_tos();
188 ZERO_STRUCT(xacl);
189 ZERO_STRUCT(sd_hs3);
191 xacl.version = 3;
192 xacl.info.sd_hs3 = &sd_hs3;
193 xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
194 xacl.info.sd_hs3->hash_type = hash_type;
195 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
197 ndr_err = ndr_push_struct_blob(
198 pblob, ctx, &xacl,
199 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
201 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
202 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
203 ndr_errstr(ndr_err)));
204 return ndr_map_error2ntstatus(ndr_err);
207 return NT_STATUS_OK;
210 /*******************************************************************
211 Create a DATA_BLOB from a hash of the security descriptors
212 (system and NT) stored at the system layer and the NT ACL we wish
213 to preserve.
214 *******************************************************************/
216 static NTSTATUS create_sys_acl_blob(const struct security_descriptor *psd,
217 DATA_BLOB *pblob,
218 uint16_t hash_type,
219 uint8_t hash[XATTR_SD_HASH_SIZE],
220 const char *description,
221 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
223 struct xattr_NTACL xacl;
224 struct security_descriptor_hash_v4 sd_hs4;
225 enum ndr_err_code ndr_err;
226 TALLOC_CTX *ctx = talloc_tos();
227 NTTIME nttime_now;
228 struct timeval now = timeval_current();
229 nttime_now = timeval_to_nttime(&now);
231 ZERO_STRUCT(xacl);
232 ZERO_STRUCT(sd_hs4);
234 xacl.version = 4;
235 xacl.info.sd_hs4 = &sd_hs4;
236 xacl.info.sd_hs4->sd = discard_const_p(struct security_descriptor, psd);
237 xacl.info.sd_hs4->hash_type = hash_type;
238 memcpy(&xacl.info.sd_hs4->hash[0], hash, XATTR_SD_HASH_SIZE);
239 xacl.info.sd_hs4->description = description;
240 xacl.info.sd_hs4->time = nttime_now;
241 memcpy(&xacl.info.sd_hs4->sys_acl_hash[0], sys_acl_hash, XATTR_SD_HASH_SIZE);
243 ndr_err = ndr_push_struct_blob(
244 pblob, ctx, &xacl,
245 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
247 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
248 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
249 ndr_errstr(ndr_err)));
250 return ndr_map_error2ntstatus(ndr_err);
253 return NT_STATUS_OK;
256 /*******************************************************************
257 Add in 3 inheritable components for a non-inheritable directory ACL.
258 CREATOR_OWNER/CREATOR_GROUP/WORLD.
259 *******************************************************************/
261 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
262 const char *name,
263 SMB_STRUCT_STAT *psbuf,
264 struct security_descriptor *psd)
266 struct connection_struct *conn = handle->conn;
267 int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
268 struct smb_filename smb_fname;
269 enum security_ace_type acltype;
270 uint32_t access_mask;
271 mode_t dir_mode;
272 mode_t file_mode;
273 mode_t mode;
274 struct security_ace *new_ace_list;
276 if (psd->dacl) {
277 new_ace_list = talloc_zero_array(psd->dacl,
278 struct security_ace,
279 num_aces + 3);
280 } else {
282 * make_sec_acl() at the bottom of this function
283 * dupliates new_ace_list
285 new_ace_list = talloc_zero_array(talloc_tos(),
286 struct security_ace,
287 num_aces + 3);
290 if (new_ace_list == NULL) {
291 return NT_STATUS_NO_MEMORY;
294 /* Fake a quick smb_filename. */
295 ZERO_STRUCT(smb_fname);
296 smb_fname.st = *psbuf;
297 smb_fname.base_name = discard_const_p(char, name);
299 dir_mode = unix_mode(conn,
300 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
301 file_mode = unix_mode(conn,
302 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
304 mode = dir_mode | file_mode;
306 DEBUG(10, ("add_directory_inheritable_components: directory %s, "
307 "mode = 0%o\n",
308 name,
309 (unsigned int)mode ));
311 if (num_aces) {
312 memcpy(new_ace_list, psd->dacl->aces,
313 num_aces * sizeof(struct security_ace));
315 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
316 mode & 0700, false);
318 init_sec_ace(&new_ace_list[num_aces],
319 &global_sid_Creator_Owner,
320 acltype,
321 access_mask,
322 SEC_ACE_FLAG_CONTAINER_INHERIT|
323 SEC_ACE_FLAG_OBJECT_INHERIT|
324 SEC_ACE_FLAG_INHERIT_ONLY);
325 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
326 (mode << 3) & 0700, false);
327 init_sec_ace(&new_ace_list[num_aces+1],
328 &global_sid_Creator_Group,
329 acltype,
330 access_mask,
331 SEC_ACE_FLAG_CONTAINER_INHERIT|
332 SEC_ACE_FLAG_OBJECT_INHERIT|
333 SEC_ACE_FLAG_INHERIT_ONLY);
334 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
335 (mode << 6) & 0700, false);
336 init_sec_ace(&new_ace_list[num_aces+2],
337 &global_sid_World,
338 acltype,
339 access_mask,
340 SEC_ACE_FLAG_CONTAINER_INHERIT|
341 SEC_ACE_FLAG_OBJECT_INHERIT|
342 SEC_ACE_FLAG_INHERIT_ONLY);
343 if (psd->dacl) {
344 psd->dacl->aces = new_ace_list;
345 psd->dacl->num_aces += 3;
346 psd->dacl->size += new_ace_list[num_aces].size +
347 new_ace_list[num_aces+1].size +
348 new_ace_list[num_aces+2].size;
349 } else {
350 psd->dacl = make_sec_acl(psd,
351 NT4_ACL_REVISION,
353 new_ace_list);
354 if (psd->dacl == NULL) {
355 return NT_STATUS_NO_MEMORY;
358 return NT_STATUS_OK;
361 /*******************************************************************
362 Pull a DATA_BLOB from an xattr given a pathname.
363 If the hash doesn't match, or doesn't exist - return the underlying
364 filesystem sd.
365 *******************************************************************/
367 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
368 files_struct *fsp,
369 const char *name,
370 uint32_t security_info,
371 TALLOC_CTX *mem_ctx,
372 struct security_descriptor **ppdesc)
374 DATA_BLOB blob = data_blob_null;
375 NTSTATUS status;
376 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
377 uint16_t xattr_version = 0;
378 uint8_t hash[XATTR_SD_HASH_SIZE];
379 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
380 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
381 uint8_t sys_acl_hash_tmp[XATTR_SD_HASH_SIZE];
382 struct security_descriptor *psd = NULL;
383 struct security_descriptor *pdesc_next = NULL;
384 bool ignore_file_system_acl = lp_parm_bool(SNUM(handle->conn),
385 ACL_MODULE_NAME,
386 "ignore system acls",
387 false);
388 TALLOC_CTX *frame = talloc_stackframe();
390 if (fsp && name == NULL) {
391 name = fsp->fsp_name->base_name;
394 DEBUG(10, ("get_nt_acl_internal: name=%s\n", name));
396 status = get_acl_blob(frame, handle, fsp, name, &blob);
397 if (!NT_STATUS_IS_OK(status)) {
398 DEBUG(10, ("get_nt_acl_internal: get_acl_blob returned %s\n",
399 nt_errstr(status)));
400 psd = NULL;
401 goto out;
402 } else {
403 status = parse_acl_blob(&blob, mem_ctx, &psd,
404 &hash_type, &xattr_version, &hash[0], &sys_acl_hash[0]);
405 if (!NT_STATUS_IS_OK(status)) {
406 DEBUG(10, ("parse_acl_blob returned %s\n",
407 nt_errstr(status)));
408 psd = NULL;
409 goto out;
413 /* Ensure we don't leak psd if we don't choose it.
415 * We don't allocate it onto frame as it is preferred not to
416 * steal from a talloc pool.
418 talloc_steal(frame, psd);
420 /* determine which type of xattr we got */
421 switch (xattr_version) {
422 case 1:
423 case 2:
424 /* These xattr types are unilatteral, they do not
425 * require confirmation of the hash. In particular,
426 * the NTVFS file server uses version 1, but
427 * 'samba-tool ntacl' can set these as well */
428 goto out;
429 case 3:
430 case 4:
431 if (ignore_file_system_acl) {
432 goto out;
435 break;
436 default:
437 DEBUG(10, ("get_nt_acl_internal: ACL blob revision "
438 "mismatch (%u) for file %s\n",
439 (unsigned int)hash_type,
440 name));
441 TALLOC_FREE(psd);
442 psd = NULL;
443 goto out;
446 /* determine which type of xattr we got */
447 if (hash_type != XATTR_SD_HASH_TYPE_SHA256) {
448 DEBUG(10, ("get_nt_acl_internal: ACL blob hash type "
449 "(%u) unexpected for file %s\n",
450 (unsigned int)hash_type,
451 name));
452 TALLOC_FREE(psd);
453 psd = NULL;
454 goto out;
457 /* determine which type of xattr we got */
458 switch (xattr_version) {
459 case 4:
461 int ret;
462 char *sys_acl_blob_description;
463 DATA_BLOB sys_acl_blob;
464 if (fsp) {
465 /* Get the full underlying sd, then hash. */
466 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
467 fsp,
468 frame,
469 &sys_acl_blob_description,
470 &sys_acl_blob);
471 } else {
472 /* Get the full underlying sd, then hash. */
473 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle,
474 name,
475 frame,
476 &sys_acl_blob_description,
477 &sys_acl_blob);
480 /* If we fail to get the ACL blob (for some reason) then this
481 * is not fatal, we just work based on the NT ACL only */
482 if (ret == 0) {
483 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash_tmp);
484 if (!NT_STATUS_IS_OK(status)) {
485 TALLOC_FREE(frame);
486 return status;
489 if (memcmp(&sys_acl_hash[0], &sys_acl_hash_tmp[0],
490 XATTR_SD_HASH_SIZE) == 0) {
491 /* Hash matches, return blob sd. */
492 DEBUG(10, ("get_nt_acl_internal: blob hash "
493 "matches for file %s\n",
494 name ));
495 goto out;
499 /* Otherwise, fall though and see if the NT ACL hash matches */
501 case 3:
502 /* Get the full underlying sd for the hash
503 or to return as backup. */
504 if (fsp) {
505 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
506 fsp,
507 HASH_SECURITY_INFO,
508 mem_ctx,
509 &pdesc_next);
510 } else {
511 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
512 name,
513 HASH_SECURITY_INFO,
514 mem_ctx,
515 &pdesc_next);
518 if (!NT_STATUS_IS_OK(status)) {
519 DEBUG(10, ("get_nt_acl_internal: get_next_acl for file %s "
520 "returned %s\n",
521 name,
522 nt_errstr(status)));
523 TALLOC_FREE(frame);
524 return status;
527 /* Ensure we don't leak psd_next if we don't choose it.
529 * We don't allocate it onto frame as it is preferred not to
530 * steal from a talloc pool.
532 talloc_steal(frame, pdesc_next);
534 status = hash_sd_sha256(pdesc_next, hash_tmp);
535 if (!NT_STATUS_IS_OK(status)) {
536 TALLOC_FREE(psd);
537 psd = pdesc_next;
538 goto out;
541 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
542 /* Hash matches, return blob sd. */
543 DEBUG(10, ("get_nt_acl_internal: blob hash "
544 "matches for file %s\n",
545 name ));
546 goto out;
549 /* Hash doesn't match, return underlying sd. */
550 DEBUG(10, ("get_nt_acl_internal: blob hash "
551 "does not match for file %s - returning "
552 "file system SD mapping.\n",
553 name ));
555 if (DEBUGLEVEL >= 10) {
556 DEBUG(10,("get_nt_acl_internal: acl for blob hash for %s is:\n",
557 name ));
558 NDR_PRINT_DEBUG(security_descriptor, pdesc_next);
561 TALLOC_FREE(psd);
562 psd = pdesc_next;
564 out:
566 if (psd == NULL) {
567 /* Get the full underlying sd, as we failed to get the
568 * blob for the hash, or the revision/hash type wasn't
569 * known */
570 if (fsp) {
571 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
572 fsp,
573 security_info,
574 mem_ctx,
575 &pdesc_next);
576 } else {
577 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
578 name,
579 security_info,
580 mem_ctx,
581 &pdesc_next);
584 if (!NT_STATUS_IS_OK(status)) {
585 DEBUG(10, ("get_nt_acl_internal: get_next_acl for file %s "
586 "returned %s\n",
587 name,
588 nt_errstr(status)));
589 TALLOC_FREE(frame);
590 return status;
593 /* Ensure we don't leak psd_next if we don't choose it.
595 * We don't allocate it onto frame as it is preferred not to
596 * steal from a talloc pool.
598 talloc_steal(frame, pdesc_next);
599 psd = pdesc_next;
602 if (psd != pdesc_next) {
603 /* We're returning the blob, throw
604 * away the filesystem SD. */
605 TALLOC_FREE(pdesc_next);
606 } else {
607 SMB_STRUCT_STAT sbuf;
608 SMB_STRUCT_STAT *psbuf = &sbuf;
609 bool is_directory = false;
611 * We're returning the underlying ACL from the
612 * filesystem. If it's a directory, and has no
613 * inheritable ACE entries we have to fake them.
615 if (fsp) {
616 status = vfs_stat_fsp(fsp);
617 if (!NT_STATUS_IS_OK(status)) {
618 TALLOC_FREE(frame);
619 return status;
621 psbuf = &fsp->fsp_name->st;
622 } else {
623 int ret = vfs_stat_smb_fname(handle->conn,
624 name,
625 &sbuf);
626 if (ret == -1) {
627 TALLOC_FREE(frame);
628 return map_nt_error_from_unix(errno);
631 is_directory = S_ISDIR(psbuf->st_ex_mode);
633 if (ignore_file_system_acl) {
634 TALLOC_FREE(pdesc_next);
635 status = make_default_filesystem_acl(mem_ctx,
636 name,
637 psbuf,
638 &psd);
639 if (!NT_STATUS_IS_OK(status)) {
640 TALLOC_FREE(frame);
641 return status;
643 } else {
644 if (is_directory &&
645 !sd_has_inheritable_components(psd,
646 true)) {
647 status = add_directory_inheritable_components(
648 handle,
649 name,
650 psbuf,
651 psd);
652 if (!NT_STATUS_IS_OK(status)) {
653 TALLOC_FREE(frame);
654 return status;
657 /* The underlying POSIX module always sets
658 the ~SEC_DESC_DACL_PROTECTED bit, as ACLs
659 can't be inherited in this way under POSIX.
660 Remove it for Windows-style ACLs. */
661 psd->type &= ~SEC_DESC_DACL_PROTECTED;
665 if (!(security_info & SECINFO_OWNER)) {
666 psd->owner_sid = NULL;
668 if (!(security_info & SECINFO_GROUP)) {
669 psd->group_sid = NULL;
671 if (!(security_info & SECINFO_DACL)) {
672 psd->type &= ~SEC_DESC_DACL_PRESENT;
673 psd->dacl = NULL;
675 if (!(security_info & SECINFO_SACL)) {
676 psd->type &= ~SEC_DESC_SACL_PRESENT;
677 psd->sacl = NULL;
680 TALLOC_FREE(blob.data);
682 if (DEBUGLEVEL >= 10) {
683 DEBUG(10,("get_nt_acl_internal: returning acl for %s is:\n",
684 name ));
685 NDR_PRINT_DEBUG(security_descriptor, psd);
688 /* The VFS API is that the ACL is expected to be on mem_ctx */
689 *ppdesc = talloc_move(mem_ctx, &psd);
691 TALLOC_FREE(frame);
692 return NT_STATUS_OK;
695 /*********************************************************************
696 Fetch a security descriptor given an fsp.
697 *********************************************************************/
699 static NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle,
700 files_struct *fsp,
701 uint32_t security_info,
702 TALLOC_CTX *mem_ctx,
703 struct security_descriptor **ppdesc)
705 return get_nt_acl_internal(handle, fsp,
706 NULL, security_info, mem_ctx, ppdesc);
709 /*********************************************************************
710 Fetch a security descriptor given a pathname.
711 *********************************************************************/
713 static NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
714 const char *name,
715 uint32_t security_info,
716 TALLOC_CTX *mem_ctx,
717 struct security_descriptor **ppdesc)
719 return get_nt_acl_internal(handle, NULL,
720 name, security_info, mem_ctx, ppdesc);
723 /*********************************************************************
724 Store a security descriptor given an fsp.
725 *********************************************************************/
727 static NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
728 uint32_t security_info_sent, const struct security_descriptor *orig_psd)
730 NTSTATUS status;
731 int ret;
732 DATA_BLOB blob, sys_acl_blob;
733 struct security_descriptor *pdesc_next = NULL;
734 struct security_descriptor *psd = NULL;
735 uint8_t hash[XATTR_SD_HASH_SIZE];
736 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
737 bool chown_needed = false;
738 char *sys_acl_description;
739 TALLOC_CTX *frame = talloc_stackframe();
741 if (DEBUGLEVEL >= 10) {
742 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
743 fsp_str_dbg(fsp)));
744 NDR_PRINT_DEBUG(security_descriptor,
745 discard_const_p(struct security_descriptor, orig_psd));
748 status = get_nt_acl_internal(handle, fsp,
749 NULL,
750 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
751 frame,
752 &psd);
754 if (!NT_STATUS_IS_OK(status)) {
755 TALLOC_FREE(frame);
756 return status;
759 psd->revision = orig_psd->revision;
760 /* All our SD's are self relative. */
761 psd->type = orig_psd->type | SEC_DESC_SELF_RELATIVE;
763 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
764 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
765 /* We're changing the owner. */
766 chown_needed = true;
768 psd->owner_sid = orig_psd->owner_sid;
770 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
771 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
772 /* We're changing the group. */
773 chown_needed = true;
775 psd->group_sid = orig_psd->group_sid;
777 if (security_info_sent & SECINFO_DACL) {
778 if (security_descriptor_with_ms_nfs(orig_psd)) {
780 * If the sd contains a MS NFS SID, do
781 * nothing, it's a chmod() request from OS X
782 * with AAPL context.
784 TALLOC_FREE(frame);
785 return NT_STATUS_OK;
787 psd->dacl = orig_psd->dacl;
788 psd->type |= SEC_DESC_DACL_PRESENT;
790 if (security_info_sent & SECINFO_SACL) {
791 psd->sacl = orig_psd->sacl;
792 psd->type |= SEC_DESC_SACL_PRESENT;
795 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
796 if (!NT_STATUS_IS_OK(status)) {
797 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
798 TALLOC_FREE(frame);
799 return status;
801 /* We got access denied here. If we're already root,
802 or we didn't need to do a chown, or the fsp isn't
803 open with WRITE_OWNER access, just return. */
804 if (get_current_uid(handle->conn) == 0 ||
805 chown_needed == false ||
806 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
807 TALLOC_FREE(frame);
808 return NT_STATUS_ACCESS_DENIED;
811 DEBUG(10,("fset_nt_acl_common: overriding chown on file %s "
812 "for sid %s\n",
813 fsp_str_dbg(fsp),
814 sid_string_tos(psd->owner_sid)
817 /* Ok, we failed to chown and we have
818 SEC_STD_WRITE_OWNER access - override. */
819 become_root();
820 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
821 security_info_sent, psd);
822 unbecome_root();
823 if (!NT_STATUS_IS_OK(status)) {
824 TALLOC_FREE(frame);
825 return status;
829 /* Get the full underlying sd, then hash. */
830 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
831 fsp,
832 HASH_SECURITY_INFO,
833 frame,
834 &pdesc_next);
836 if (!NT_STATUS_IS_OK(status)) {
837 TALLOC_FREE(frame);
838 return status;
841 status = hash_sd_sha256(pdesc_next, hash);
842 if (!NT_STATUS_IS_OK(status)) {
843 TALLOC_FREE(frame);
844 return status;
847 /* Get the full underlying sd, then hash. */
848 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
849 fsp,
850 frame,
851 &sys_acl_description,
852 &sys_acl_blob);
854 /* If we fail to get the ACL blob (for some reason) then this
855 * is not fatal, we just work based on the NT ACL only */
856 if (ret != 0) {
857 if (DEBUGLEVEL >= 10) {
858 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s\n",
859 fsp_str_dbg(fsp)));
860 NDR_PRINT_DEBUG(security_descriptor,
861 discard_const_p(struct security_descriptor, psd));
863 DEBUG(10,("fset_nt_acl_xattr: storing has in xattr sd based on \n"));
864 NDR_PRINT_DEBUG(security_descriptor,
865 discard_const_p(struct security_descriptor, pdesc_next));
867 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
868 if (!NT_STATUS_IS_OK(status)) {
869 DEBUG(10, ("fset_nt_acl_xattr: create_acl_blob failed\n"));
870 TALLOC_FREE(frame);
871 return status;
874 status = store_acl_blob_fsp(handle, fsp, &blob);
876 TALLOC_FREE(frame);
877 return status;
880 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash);
881 if (!NT_STATUS_IS_OK(status)) {
882 TALLOC_FREE(frame);
883 return status;
886 if (DEBUGLEVEL >= 10) {
887 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s based on system ACL\n",
888 fsp_str_dbg(fsp)));
889 NDR_PRINT_DEBUG(security_descriptor,
890 discard_const_p(struct security_descriptor, psd));
892 DEBUG(10,("fset_nt_acl_xattr: storing hash in xattr sd based on system ACL and:\n"));
893 NDR_PRINT_DEBUG(security_descriptor,
894 discard_const_p(struct security_descriptor, pdesc_next));
897 /* We store hashes of both the sys ACL blob and the NT
898 * security desciptor mapped from that ACL so as to improve
899 * our chances against some inadvertant change breaking the
900 * hash used */
901 status = create_sys_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash,
902 sys_acl_description, sys_acl_hash);
903 if (!NT_STATUS_IS_OK(status)) {
904 DEBUG(10, ("fset_nt_acl_xattr: create_sys_acl_blob failed\n"));
905 TALLOC_FREE(frame);
906 return status;
909 status = store_acl_blob_fsp(handle, fsp, &blob);
911 TALLOC_FREE(frame);
912 return status;
915 static int acl_common_remove_object(vfs_handle_struct *handle,
916 const char *path,
917 bool is_directory)
919 connection_struct *conn = handle->conn;
920 struct file_id id;
921 files_struct *fsp = NULL;
922 int ret = 0;
923 char *parent_dir = NULL;
924 const char *final_component = NULL;
925 struct smb_filename local_fname;
926 int saved_errno = 0;
927 char *saved_dir = NULL;
929 saved_dir = vfs_GetWd(talloc_tos(),conn);
930 if (!saved_dir) {
931 saved_errno = errno;
932 goto out;
935 if (!parent_dirname(talloc_tos(), path,
936 &parent_dir, &final_component)) {
937 saved_errno = ENOMEM;
938 goto out;
941 DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
942 is_directory ? "directory" : "file",
943 parent_dir, final_component ));
945 /* cd into the parent dir to pin it. */
946 ret = vfs_ChDir(conn, parent_dir);
947 if (ret == -1) {
948 saved_errno = errno;
949 goto out;
952 ZERO_STRUCT(local_fname);
953 local_fname.base_name = discard_const_p(char, final_component);
955 /* Must use lstat here. */
956 ret = SMB_VFS_LSTAT(conn, &local_fname);
957 if (ret == -1) {
958 saved_errno = errno;
959 goto out;
962 /* Ensure we have this file open with DELETE access. */
963 id = vfs_file_id_from_sbuf(conn, &local_fname.st);
964 for (fsp = file_find_di_first(conn->sconn, id); fsp;
965 fsp = file_find_di_next(fsp)) {
966 if (fsp->access_mask & DELETE_ACCESS &&
967 fsp->delete_on_close) {
968 /* We did open this for delete,
969 * allow the delete as root.
971 break;
975 if (!fsp) {
976 DEBUG(10,("acl_common_remove_object: %s %s/%s "
977 "not an open file\n",
978 is_directory ? "directory" : "file",
979 parent_dir, final_component ));
980 saved_errno = EACCES;
981 goto out;
984 become_root();
985 if (is_directory) {
986 ret = SMB_VFS_NEXT_RMDIR(handle, final_component);
987 } else {
988 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
990 unbecome_root();
992 if (ret == -1) {
993 saved_errno = errno;
996 out:
998 TALLOC_FREE(parent_dir);
1000 if (saved_dir) {
1001 vfs_ChDir(conn, saved_dir);
1003 if (saved_errno) {
1004 errno = saved_errno;
1006 return ret;
1009 static int rmdir_acl_common(struct vfs_handle_struct *handle,
1010 const char *path)
1012 int ret;
1014 /* Try the normal rmdir first. */
1015 ret = SMB_VFS_NEXT_RMDIR(handle, path);
1016 if (ret == 0) {
1017 return 0;
1019 if (errno == EACCES || errno == EPERM) {
1020 /* Failed due to access denied,
1021 see if we need to root override. */
1022 return acl_common_remove_object(handle,
1023 path,
1024 true);
1027 DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
1028 path,
1029 strerror(errno) ));
1030 return -1;
1033 static int unlink_acl_common(struct vfs_handle_struct *handle,
1034 const struct smb_filename *smb_fname)
1036 int ret;
1038 /* Try the normal unlink first. */
1039 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1040 if (ret == 0) {
1041 return 0;
1043 if (errno == EACCES || errno == EPERM) {
1044 /* Failed due to access denied,
1045 see if we need to root override. */
1047 /* Don't do anything fancy for streams. */
1048 if (smb_fname->stream_name) {
1049 return -1;
1051 return acl_common_remove_object(handle,
1052 smb_fname->base_name,
1053 false);
1056 DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
1057 smb_fname->base_name,
1058 strerror(errno) ));
1059 return -1;
1062 static int chmod_acl_module_common(struct vfs_handle_struct *handle,
1063 const char *path, mode_t mode)
1065 if (lp_posix_pathnames()) {
1066 /* Only allow this on POSIX pathnames. */
1067 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
1069 return 0;
1072 static int fchmod_acl_module_common(struct vfs_handle_struct *handle,
1073 struct files_struct *fsp, mode_t mode)
1075 if (fsp->posix_open) {
1076 /* Only allow this on POSIX opens. */
1077 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1079 return 0;
1082 static int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1083 const char *name, mode_t mode)
1085 if (lp_posix_pathnames()) {
1086 /* Only allow this on POSIX pathnames. */
1087 return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
1089 return 0;
1092 static int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1093 struct files_struct *fsp, mode_t mode)
1095 if (fsp->posix_open) {
1096 /* Only allow this on POSIX opens. */
1097 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
1099 return 0;