vfs: Use a blocking function in vfs_zfsacl for system ACL blobs
[Samba/gebeck_regimport.git] / source3 / modules / vfs_acl_common.c
blobef2dda1b4554b521ce6c6af7fe0cd873017e2ae7
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_sd_sha256(struct security_descriptor *psd,
53 uint8_t *hash)
55 DATA_BLOB blob;
56 SHA256_CTX tctx;
57 NTSTATUS status;
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)) {
62 return status;
65 samba_SHA256_Init(&tctx);
66 samba_SHA256_Update(&tctx, blob.data, blob.length);
67 samba_SHA256_Final(hash, &tctx);
69 return NT_STATUS_OK;
72 /*******************************************************************
73 Parse out a struct security_descriptor from a DATA_BLOB.
74 *******************************************************************/
76 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
77 TALLOC_CTX *mem_ctx,
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;
84 size_t sd_size;
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)));
93 TALLOC_FREE(frame);
94 return ndr_map_error2ntstatus(ndr_err);
97 switch (xacl.version) {
98 case 1:
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,
103 xacl.info.sd->sacl,
104 xacl.info.sd->dacl,
105 &sd_size);
106 /* No hash - null out. */
107 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
108 memset(hash, '\0', XATTR_SD_HASH_SIZE);
109 break;
110 case 2:
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,
117 &sd_size);
118 /* No hash - null out. */
119 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
120 memset(hash, '\0', XATTR_SD_HASH_SIZE);
121 break;
122 case 3:
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,
129 &sd_size);
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);
133 break;
134 default:
135 TALLOC_FREE(frame);
136 return NT_STATUS_REVISION_MISMATCH;
139 TALLOC_FREE(frame);
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,
149 DATA_BLOB *pblob,
150 uint16_t hash_type,
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();
158 ZERO_STRUCT(xacl);
159 ZERO_STRUCT(sd_hs3);
161 xacl.version = 3;
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(
168 pblob, ctx, &xacl,
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);
177 return NT_STATUS_OK;
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,
186 const char *name,
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;
195 mode_t dir_mode;
196 mode_t file_mode;
197 mode_t mode;
198 struct security_ace *new_ace_list = talloc_zero_array(talloc_tos(),
199 struct security_ace,
200 num_aces + 3);
202 if (new_ace_list == NULL) {
203 return NT_STATUS_NO_MEMORY;
206 /* Fake a quick smb_filename. */
207 ZERO_STRUCT(smb_fname);
208 smb_fname.st = *psbuf;
209 smb_fname.base_name = discard_const_p(char, name);
211 dir_mode = unix_mode(conn,
212 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
213 file_mode = unix_mode(conn,
214 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
216 mode = dir_mode | file_mode;
218 DEBUG(10, ("add_directory_inheritable_components: directory %s, "
219 "mode = 0%o\n",
220 name,
221 (unsigned int)mode ));
223 if (num_aces) {
224 memcpy(new_ace_list, psd->dacl->aces,
225 num_aces * sizeof(struct security_ace));
227 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
228 mode & 0700, false);
230 init_sec_ace(&new_ace_list[num_aces],
231 &global_sid_Creator_Owner,
232 acltype,
233 access_mask,
234 SEC_ACE_FLAG_CONTAINER_INHERIT|
235 SEC_ACE_FLAG_OBJECT_INHERIT|
236 SEC_ACE_FLAG_INHERIT_ONLY);
237 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
238 (mode << 3) & 0700, false);
239 init_sec_ace(&new_ace_list[num_aces+1],
240 &global_sid_Creator_Group,
241 acltype,
242 access_mask,
243 SEC_ACE_FLAG_CONTAINER_INHERIT|
244 SEC_ACE_FLAG_OBJECT_INHERIT|
245 SEC_ACE_FLAG_INHERIT_ONLY);
246 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
247 (mode << 6) & 0700, false);
248 init_sec_ace(&new_ace_list[num_aces+2],
249 &global_sid_World,
250 acltype,
251 access_mask,
252 SEC_ACE_FLAG_CONTAINER_INHERIT|
253 SEC_ACE_FLAG_OBJECT_INHERIT|
254 SEC_ACE_FLAG_INHERIT_ONLY);
255 if (psd->dacl) {
256 psd->dacl->aces = new_ace_list;
257 psd->dacl->num_aces += 3;
258 } else {
259 psd->dacl = make_sec_acl(talloc_tos(),
260 NT4_ACL_REVISION,
262 new_ace_list);
263 if (psd->dacl == NULL) {
264 return NT_STATUS_NO_MEMORY;
267 return NT_STATUS_OK;
270 /*******************************************************************
271 Pull a DATA_BLOB from an xattr given a pathname.
272 If the hash doesn't match, or doesn't exist - return the underlying
273 filesystem sd.
274 *******************************************************************/
276 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
277 files_struct *fsp,
278 const char *name,
279 uint32_t security_info,
280 TALLOC_CTX *mem_ctx,
281 struct security_descriptor **ppdesc)
283 DATA_BLOB blob = data_blob_null;
284 NTSTATUS status;
285 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
286 uint8_t hash[XATTR_SD_HASH_SIZE];
287 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
288 struct security_descriptor *psd = NULL;
289 struct security_descriptor *pdesc_next = NULL;
290 bool ignore_file_system_acl = lp_parm_bool(SNUM(handle->conn),
291 ACL_MODULE_NAME,
292 "ignore system acls",
293 false);
295 if (fsp && name == NULL) {
296 name = fsp->fsp_name->base_name;
299 DEBUG(10, ("get_nt_acl_internal: name=%s\n", name));
301 /* Get the full underlying sd for the hash
302 or to return as backup. */
303 if (fsp) {
304 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
305 fsp,
306 HASH_SECURITY_INFO,
307 mem_ctx,
308 &pdesc_next);
309 } else {
310 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
311 name,
312 HASH_SECURITY_INFO,
313 mem_ctx,
314 &pdesc_next);
317 if (!NT_STATUS_IS_OK(status)) {
318 DEBUG(10, ("get_nt_acl_internal: get_next_acl for file %s "
319 "returned %s\n",
320 name,
321 nt_errstr(status)));
322 return status;
325 status = get_acl_blob(talloc_tos(), handle, fsp, name, &blob);
326 if (!NT_STATUS_IS_OK(status)) {
327 DEBUG(10, ("get_nt_acl_internal: get_acl_blob returned %s\n",
328 nt_errstr(status)));
329 psd = pdesc_next;
330 goto out;
333 status = parse_acl_blob(&blob, mem_ctx, &psd,
334 &hash_type, &hash[0]);
335 if (!NT_STATUS_IS_OK(status)) {
336 DEBUG(10, ("parse_acl_blob returned %s\n",
337 nt_errstr(status)));
338 psd = pdesc_next;
339 goto out;
342 /* Ensure the hash type is one we know. */
343 switch (hash_type) {
344 case XATTR_SD_HASH_TYPE_NONE:
345 /* No hash, just return blob sd. */
346 goto out;
347 case XATTR_SD_HASH_TYPE_SHA256:
348 break;
349 default:
350 DEBUG(10, ("get_nt_acl_internal: ACL blob revision "
351 "mismatch (%u) for file %s\n",
352 (unsigned int)hash_type,
353 name));
354 TALLOC_FREE(psd);
355 psd = pdesc_next;
356 goto out;
359 if (ignore_file_system_acl) {
360 goto out;
363 status = hash_sd_sha256(pdesc_next, hash_tmp);
364 if (!NT_STATUS_IS_OK(status)) {
365 TALLOC_FREE(psd);
366 psd = pdesc_next;
367 goto out;
370 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
371 /* Hash matches, return blob sd. */
372 DEBUG(10, ("get_nt_acl_internal: blob hash "
373 "matches for file %s\n",
374 name ));
375 goto out;
378 /* Hash doesn't match, return underlying sd. */
379 DEBUG(10, ("get_nt_acl_internal: blob hash "
380 "does not match for file %s - returning "
381 "file system SD mapping.\n",
382 name ));
384 if (DEBUGLEVEL >= 10) {
385 DEBUG(10,("get_nt_acl_internal: acl for blob hash for %s is:\n",
386 name ));
387 NDR_PRINT_DEBUG(security_descriptor, pdesc_next);
390 TALLOC_FREE(psd);
391 psd = pdesc_next;
393 out:
395 if (psd != pdesc_next) {
396 /* We're returning the blob, throw
397 * away the filesystem SD. */
398 TALLOC_FREE(pdesc_next);
399 } else {
400 SMB_STRUCT_STAT sbuf;
401 SMB_STRUCT_STAT *psbuf = &sbuf;
402 bool is_directory = false;
404 * We're returning the underlying ACL from the
405 * filesystem. If it's a directory, and has no
406 * inheritable ACE entries we have to fake them.
408 if (fsp) {
409 status = vfs_stat_fsp(fsp);
410 if (!NT_STATUS_IS_OK(status)) {
411 return status;
413 psbuf = &fsp->fsp_name->st;
414 } else {
415 int ret = vfs_stat_smb_fname(handle->conn,
416 name,
417 &sbuf);
418 if (ret == -1) {
419 return map_nt_error_from_unix(errno);
422 is_directory = S_ISDIR(psbuf->st_ex_mode);
424 if (ignore_file_system_acl) {
425 TALLOC_FREE(pdesc_next);
426 status = make_default_filesystem_acl(mem_ctx,
427 name,
428 psbuf,
429 &psd);
430 if (!NT_STATUS_IS_OK(status)) {
431 return status;
433 } else {
434 if (is_directory &&
435 !sd_has_inheritable_components(psd,
436 true)) {
437 status = add_directory_inheritable_components(
438 handle,
439 name,
440 psbuf,
441 psd);
442 if (!NT_STATUS_IS_OK(status)) {
443 return status;
446 /* The underlying POSIX module always sets
447 the ~SEC_DESC_DACL_PROTECTED bit, as ACLs
448 can't be inherited in this way under POSIX.
449 Remove it for Windows-style ACLs. */
450 psd->type &= ~SEC_DESC_DACL_PROTECTED;
454 if (!(security_info & SECINFO_OWNER)) {
455 psd->owner_sid = NULL;
457 if (!(security_info & SECINFO_GROUP)) {
458 psd->group_sid = NULL;
460 if (!(security_info & SECINFO_DACL)) {
461 psd->type &= ~SEC_DESC_DACL_PRESENT;
462 psd->dacl = NULL;
464 if (!(security_info & SECINFO_SACL)) {
465 psd->type &= ~SEC_DESC_SACL_PRESENT;
466 psd->sacl = NULL;
469 TALLOC_FREE(blob.data);
470 *ppdesc = psd;
472 if (DEBUGLEVEL >= 10) {
473 DEBUG(10,("get_nt_acl_internal: returning acl for %s is:\n",
474 name ));
475 NDR_PRINT_DEBUG(security_descriptor, psd);
478 return NT_STATUS_OK;
481 /*********************************************************************
482 Fetch a security descriptor given an fsp.
483 *********************************************************************/
485 static NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle,
486 files_struct *fsp,
487 uint32_t security_info,
488 TALLOC_CTX *mem_ctx,
489 struct security_descriptor **ppdesc)
491 return get_nt_acl_internal(handle, fsp,
492 NULL, security_info, mem_ctx, ppdesc);
495 /*********************************************************************
496 Fetch a security descriptor given a pathname.
497 *********************************************************************/
499 static NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
500 const char *name,
501 uint32_t security_info,
502 TALLOC_CTX *mem_ctx,
503 struct security_descriptor **ppdesc)
505 return get_nt_acl_internal(handle, NULL,
506 name, security_info, mem_ctx, ppdesc);
509 /*********************************************************************
510 Store a security descriptor given an fsp.
511 *********************************************************************/
513 static NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
514 uint32_t security_info_sent, const struct security_descriptor *orig_psd)
516 NTSTATUS status;
517 DATA_BLOB blob;
518 struct security_descriptor *pdesc_next = NULL;
519 struct security_descriptor *psd = NULL;
520 uint8_t hash[XATTR_SD_HASH_SIZE];
521 bool chown_needed = false;
522 TALLOC_CTX *frame = talloc_stackframe();
524 if (DEBUGLEVEL >= 10) {
525 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
526 fsp_str_dbg(fsp)));
527 NDR_PRINT_DEBUG(security_descriptor,
528 discard_const_p(struct security_descriptor, orig_psd));
531 status = get_nt_acl_internal(handle, fsp,
532 NULL,
533 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
534 frame,
535 &psd);
537 if (!NT_STATUS_IS_OK(status)) {
538 TALLOC_FREE(frame);
539 return status;
542 psd->revision = orig_psd->revision;
543 /* All our SD's are self relative. */
544 psd->type = orig_psd->type | SEC_DESC_SELF_RELATIVE;
546 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
547 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
548 /* We're changing the owner. */
549 chown_needed = true;
551 psd->owner_sid = orig_psd->owner_sid;
553 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
554 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
555 /* We're changing the group. */
556 chown_needed = true;
558 psd->group_sid = orig_psd->group_sid;
560 if (security_info_sent & SECINFO_DACL) {
561 psd->dacl = orig_psd->dacl;
562 psd->type |= SEC_DESC_DACL_PRESENT;
564 if (security_info_sent & SECINFO_SACL) {
565 psd->sacl = orig_psd->sacl;
566 psd->type |= SEC_DESC_SACL_PRESENT;
569 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
570 if (!NT_STATUS_IS_OK(status)) {
571 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
572 TALLOC_FREE(frame);
573 return status;
575 /* We got access denied here. If we're already root,
576 or we didn't need to do a chown, or the fsp isn't
577 open with WRITE_OWNER access, just return. */
578 if (get_current_uid(handle->conn) == 0 ||
579 chown_needed == false ||
580 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
581 return NT_STATUS_ACCESS_DENIED;
584 DEBUG(10,("fset_nt_acl_common: overriding chown on file %s "
585 "for sid %s\n",
586 fsp_str_dbg(fsp),
587 sid_string_tos(psd->owner_sid)
590 /* Ok, we failed to chown and we have
591 SEC_STD_WRITE_OWNER access - override. */
592 become_root();
593 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
594 security_info_sent, psd);
595 unbecome_root();
596 if (!NT_STATUS_IS_OK(status)) {
597 TALLOC_FREE(frame);
598 return status;
602 /* Get the full underlying sd, then hash. */
603 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
604 fsp,
605 HASH_SECURITY_INFO,
606 frame,
607 &pdesc_next);
609 if (!NT_STATUS_IS_OK(status)) {
610 TALLOC_FREE(frame);
611 return status;
614 status = hash_sd_sha256(pdesc_next, hash);
615 if (!NT_STATUS_IS_OK(status)) {
616 TALLOC_FREE(frame);
617 return status;
620 if (DEBUGLEVEL >= 10) {
621 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s\n",
622 fsp_str_dbg(fsp)));
623 NDR_PRINT_DEBUG(security_descriptor,
624 discard_const_p(struct security_descriptor, psd));
626 DEBUG(10,("fset_nt_acl_xattr: storing has in xattr sd based on \n"));
627 NDR_PRINT_DEBUG(security_descriptor,
628 discard_const_p(struct security_descriptor, pdesc_next));
630 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
631 if (!NT_STATUS_IS_OK(status)) {
632 DEBUG(10, ("fset_nt_acl_xattr: create_acl_blob failed\n"));
633 TALLOC_FREE(frame);
634 return status;
637 status = store_acl_blob_fsp(handle, fsp, &blob);
639 TALLOC_FREE(frame);
640 return status;
643 static int acl_common_remove_object(vfs_handle_struct *handle,
644 const char *path,
645 bool is_directory)
647 connection_struct *conn = handle->conn;
648 struct file_id id;
649 files_struct *fsp = NULL;
650 int ret = 0;
651 char *parent_dir = NULL;
652 const char *final_component = NULL;
653 struct smb_filename local_fname;
654 int saved_errno = 0;
655 char *saved_dir = NULL;
657 saved_dir = vfs_GetWd(talloc_tos(),conn);
658 if (!saved_dir) {
659 saved_errno = errno;
660 goto out;
663 if (!parent_dirname(talloc_tos(), path,
664 &parent_dir, &final_component)) {
665 saved_errno = ENOMEM;
666 goto out;
669 DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
670 is_directory ? "directory" : "file",
671 parent_dir, final_component ));
673 /* cd into the parent dir to pin it. */
674 ret = vfs_ChDir(conn, parent_dir);
675 if (ret == -1) {
676 saved_errno = errno;
677 goto out;
680 ZERO_STRUCT(local_fname);
681 local_fname.base_name = discard_const_p(char, final_component);
683 /* Must use lstat here. */
684 ret = SMB_VFS_LSTAT(conn, &local_fname);
685 if (ret == -1) {
686 saved_errno = errno;
687 goto out;
690 /* Ensure we have this file open with DELETE access. */
691 id = vfs_file_id_from_sbuf(conn, &local_fname.st);
692 for (fsp = file_find_di_first(conn->sconn, id); fsp;
693 fsp = file_find_di_next(fsp)) {
694 if (fsp->access_mask & DELETE_ACCESS &&
695 fsp->delete_on_close) {
696 /* We did open this for delete,
697 * allow the delete as root.
699 break;
703 if (!fsp) {
704 DEBUG(10,("acl_common_remove_object: %s %s/%s "
705 "not an open file\n",
706 is_directory ? "directory" : "file",
707 parent_dir, final_component ));
708 saved_errno = EACCES;
709 goto out;
712 become_root();
713 if (is_directory) {
714 ret = SMB_VFS_NEXT_RMDIR(handle, final_component);
715 } else {
716 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
718 unbecome_root();
720 if (ret == -1) {
721 saved_errno = errno;
724 out:
726 TALLOC_FREE(parent_dir);
728 if (saved_dir) {
729 vfs_ChDir(conn, saved_dir);
731 if (saved_errno) {
732 errno = saved_errno;
734 return ret;
737 static int rmdir_acl_common(struct vfs_handle_struct *handle,
738 const char *path)
740 int ret;
742 /* Try the normal rmdir first. */
743 ret = SMB_VFS_NEXT_RMDIR(handle, path);
744 if (ret == 0) {
745 return 0;
747 if (errno == EACCES || errno == EPERM) {
748 /* Failed due to access denied,
749 see if we need to root override. */
750 return acl_common_remove_object(handle,
751 path,
752 true);
755 DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
756 path,
757 strerror(errno) ));
758 return -1;
761 static int unlink_acl_common(struct vfs_handle_struct *handle,
762 const struct smb_filename *smb_fname)
764 int ret;
766 /* Try the normal unlink first. */
767 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
768 if (ret == 0) {
769 return 0;
771 if (errno == EACCES || errno == EPERM) {
772 /* Failed due to access denied,
773 see if we need to root override. */
775 /* Don't do anything fancy for streams. */
776 if (smb_fname->stream_name) {
777 return -1;
779 return acl_common_remove_object(handle,
780 smb_fname->base_name,
781 false);
784 DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
785 smb_fname->base_name,
786 strerror(errno) ));
787 return -1;
790 static int chmod_acl_module_common(struct vfs_handle_struct *handle,
791 const char *path, mode_t mode)
793 if (lp_posix_pathnames()) {
794 /* Only allow this on POSIX pathnames. */
795 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
797 return 0;
800 static int fchmod_acl_module_common(struct vfs_handle_struct *handle,
801 struct files_struct *fsp, mode_t mode)
803 if (fsp->posix_open) {
804 /* Only allow this on POSIX opens. */
805 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
807 return 0;
810 static int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
811 const char *name, mode_t mode)
813 if (lp_posix_pathnames()) {
814 /* Only allow this on POSIX pathnames. */
815 return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
817 return 0;
820 static int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
821 struct files_struct *fsp, mode_t mode)
823 if (fsp->posix_open) {
824 /* Only allow this on POSIX opens. */
825 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
827 return 0;