vfs: Add helper function hash_blob_sha256 to vfs_acl_common.c
[Samba/gbeck.git] / source3 / modules / vfs_acl_common.c
blobe1555c7d9e6b2f9837bd785b689fcb35729111e0
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 uint8_t hash[XATTR_SD_HASH_SIZE])
94 struct xattr_NTACL xacl;
95 enum ndr_err_code ndr_err;
96 size_t sd_size;
97 TALLOC_CTX *frame = talloc_stackframe();
99 ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
100 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
102 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
103 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
104 ndr_errstr(ndr_err)));
105 TALLOC_FREE(frame);
106 return ndr_map_error2ntstatus(ndr_err);
109 switch (xacl.version) {
110 case 1:
111 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
112 xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
113 xacl.info.sd->owner_sid,
114 xacl.info.sd->group_sid,
115 xacl.info.sd->sacl,
116 xacl.info.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 2:
123 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
124 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
125 xacl.info.sd_hs2->sd->owner_sid,
126 xacl.info.sd_hs2->sd->group_sid,
127 xacl.info.sd_hs2->sd->sacl,
128 xacl.info.sd_hs2->sd->dacl,
129 &sd_size);
130 /* No hash - null out. */
131 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
132 memset(hash, '\0', XATTR_SD_HASH_SIZE);
133 break;
134 case 3:
135 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
136 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
137 xacl.info.sd_hs3->sd->owner_sid,
138 xacl.info.sd_hs3->sd->group_sid,
139 xacl.info.sd_hs3->sd->sacl,
140 xacl.info.sd_hs3->sd->dacl,
141 &sd_size);
142 *p_hash_type = xacl.info.sd_hs3->hash_type;
143 /* Current version 3. */
144 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
145 break;
146 default:
147 TALLOC_FREE(frame);
148 return NT_STATUS_REVISION_MISMATCH;
151 TALLOC_FREE(frame);
153 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
156 /*******************************************************************
157 Create a DATA_BLOB from a security descriptor.
158 *******************************************************************/
160 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
161 DATA_BLOB *pblob,
162 uint16_t hash_type,
163 uint8_t hash[XATTR_SD_HASH_SIZE])
165 struct xattr_NTACL xacl;
166 struct security_descriptor_hash_v3 sd_hs3;
167 enum ndr_err_code ndr_err;
168 TALLOC_CTX *ctx = talloc_tos();
170 ZERO_STRUCT(xacl);
171 ZERO_STRUCT(sd_hs3);
173 xacl.version = 3;
174 xacl.info.sd_hs3 = &sd_hs3;
175 xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
176 xacl.info.sd_hs3->hash_type = hash_type;
177 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
179 ndr_err = ndr_push_struct_blob(
180 pblob, ctx, &xacl,
181 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
183 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
184 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
185 ndr_errstr(ndr_err)));
186 return ndr_map_error2ntstatus(ndr_err);
189 return NT_STATUS_OK;
192 /*******************************************************************
193 Add in 3 inheritable components for a non-inheritable directory ACL.
194 CREATOR_OWNER/CREATOR_GROUP/WORLD.
195 *******************************************************************/
197 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
198 const char *name,
199 SMB_STRUCT_STAT *psbuf,
200 struct security_descriptor *psd)
202 struct connection_struct *conn = handle->conn;
203 int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
204 struct smb_filename smb_fname;
205 enum security_ace_type acltype;
206 uint32_t access_mask;
207 mode_t dir_mode;
208 mode_t file_mode;
209 mode_t mode;
210 struct security_ace *new_ace_list;
212 if (psd->dacl) {
213 new_ace_list = talloc_zero_array(psd->dacl,
214 struct security_ace,
215 num_aces + 3);
216 } else {
218 * make_sec_acl() at the bottom of this function
219 * dupliates new_ace_list
221 new_ace_list = talloc_zero_array(talloc_tos(),
222 struct security_ace,
223 num_aces + 3);
226 if (new_ace_list == NULL) {
227 return NT_STATUS_NO_MEMORY;
230 /* Fake a quick smb_filename. */
231 ZERO_STRUCT(smb_fname);
232 smb_fname.st = *psbuf;
233 smb_fname.base_name = discard_const_p(char, name);
235 dir_mode = unix_mode(conn,
236 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
237 file_mode = unix_mode(conn,
238 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
240 mode = dir_mode | file_mode;
242 DEBUG(10, ("add_directory_inheritable_components: directory %s, "
243 "mode = 0%o\n",
244 name,
245 (unsigned int)mode ));
247 if (num_aces) {
248 memcpy(new_ace_list, psd->dacl->aces,
249 num_aces * sizeof(struct security_ace));
251 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
252 mode & 0700, false);
254 init_sec_ace(&new_ace_list[num_aces],
255 &global_sid_Creator_Owner,
256 acltype,
257 access_mask,
258 SEC_ACE_FLAG_CONTAINER_INHERIT|
259 SEC_ACE_FLAG_OBJECT_INHERIT|
260 SEC_ACE_FLAG_INHERIT_ONLY);
261 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
262 (mode << 3) & 0700, false);
263 init_sec_ace(&new_ace_list[num_aces+1],
264 &global_sid_Creator_Group,
265 acltype,
266 access_mask,
267 SEC_ACE_FLAG_CONTAINER_INHERIT|
268 SEC_ACE_FLAG_OBJECT_INHERIT|
269 SEC_ACE_FLAG_INHERIT_ONLY);
270 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
271 (mode << 6) & 0700, false);
272 init_sec_ace(&new_ace_list[num_aces+2],
273 &global_sid_World,
274 acltype,
275 access_mask,
276 SEC_ACE_FLAG_CONTAINER_INHERIT|
277 SEC_ACE_FLAG_OBJECT_INHERIT|
278 SEC_ACE_FLAG_INHERIT_ONLY);
279 if (psd->dacl) {
280 psd->dacl->aces = new_ace_list;
281 psd->dacl->num_aces += 3;
282 } else {
283 psd->dacl = make_sec_acl(psd,
284 NT4_ACL_REVISION,
286 new_ace_list);
287 if (psd->dacl == NULL) {
288 return NT_STATUS_NO_MEMORY;
291 return NT_STATUS_OK;
294 /*******************************************************************
295 Pull a DATA_BLOB from an xattr given a pathname.
296 If the hash doesn't match, or doesn't exist - return the underlying
297 filesystem sd.
298 *******************************************************************/
300 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
301 files_struct *fsp,
302 const char *name,
303 uint32_t security_info,
304 TALLOC_CTX *mem_ctx,
305 struct security_descriptor **ppdesc)
307 DATA_BLOB blob = data_blob_null;
308 NTSTATUS status;
309 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
310 uint8_t hash[XATTR_SD_HASH_SIZE];
311 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
312 struct security_descriptor *psd = NULL;
313 struct security_descriptor *pdesc_next = NULL;
314 bool ignore_file_system_acl = lp_parm_bool(SNUM(handle->conn),
315 ACL_MODULE_NAME,
316 "ignore system acls",
317 false);
319 if (fsp && name == NULL) {
320 name = fsp->fsp_name->base_name;
323 DEBUG(10, ("get_nt_acl_internal: name=%s\n", name));
325 /* Get the full underlying sd for the hash
326 or to return as backup. */
327 if (fsp) {
328 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
329 fsp,
330 HASH_SECURITY_INFO,
331 mem_ctx,
332 &pdesc_next);
333 } else {
334 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
335 name,
336 HASH_SECURITY_INFO,
337 mem_ctx,
338 &pdesc_next);
341 if (!NT_STATUS_IS_OK(status)) {
342 DEBUG(10, ("get_nt_acl_internal: get_next_acl for file %s "
343 "returned %s\n",
344 name,
345 nt_errstr(status)));
346 return status;
349 status = get_acl_blob(talloc_tos(), handle, fsp, name, &blob);
350 if (!NT_STATUS_IS_OK(status)) {
351 DEBUG(10, ("get_nt_acl_internal: get_acl_blob returned %s\n",
352 nt_errstr(status)));
353 psd = pdesc_next;
354 goto out;
357 status = parse_acl_blob(&blob, mem_ctx, &psd,
358 &hash_type, &hash[0]);
359 if (!NT_STATUS_IS_OK(status)) {
360 DEBUG(10, ("parse_acl_blob returned %s\n",
361 nt_errstr(status)));
362 psd = pdesc_next;
363 goto out;
366 /* Ensure the hash type is one we know. */
367 switch (hash_type) {
368 case XATTR_SD_HASH_TYPE_NONE:
369 /* No hash, just return blob sd. */
370 goto out;
371 case XATTR_SD_HASH_TYPE_SHA256:
372 break;
373 default:
374 DEBUG(10, ("get_nt_acl_internal: ACL blob revision "
375 "mismatch (%u) for file %s\n",
376 (unsigned int)hash_type,
377 name));
378 TALLOC_FREE(psd);
379 psd = pdesc_next;
380 goto out;
383 if (ignore_file_system_acl) {
384 goto out;
387 status = hash_sd_sha256(pdesc_next, hash_tmp);
388 if (!NT_STATUS_IS_OK(status)) {
389 TALLOC_FREE(psd);
390 psd = pdesc_next;
391 goto out;
394 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
395 /* Hash matches, return blob sd. */
396 DEBUG(10, ("get_nt_acl_internal: blob hash "
397 "matches for file %s\n",
398 name ));
399 goto out;
402 /* Hash doesn't match, return underlying sd. */
403 DEBUG(10, ("get_nt_acl_internal: blob hash "
404 "does not match for file %s - returning "
405 "file system SD mapping.\n",
406 name ));
408 if (DEBUGLEVEL >= 10) {
409 DEBUG(10,("get_nt_acl_internal: acl for blob hash for %s is:\n",
410 name ));
411 NDR_PRINT_DEBUG(security_descriptor, pdesc_next);
414 TALLOC_FREE(psd);
415 psd = pdesc_next;
417 out:
419 if (psd != pdesc_next) {
420 /* We're returning the blob, throw
421 * away the filesystem SD. */
422 TALLOC_FREE(pdesc_next);
423 } else {
424 SMB_STRUCT_STAT sbuf;
425 SMB_STRUCT_STAT *psbuf = &sbuf;
426 bool is_directory = false;
428 * We're returning the underlying ACL from the
429 * filesystem. If it's a directory, and has no
430 * inheritable ACE entries we have to fake them.
432 if (fsp) {
433 status = vfs_stat_fsp(fsp);
434 if (!NT_STATUS_IS_OK(status)) {
435 return status;
437 psbuf = &fsp->fsp_name->st;
438 } else {
439 int ret = vfs_stat_smb_fname(handle->conn,
440 name,
441 &sbuf);
442 if (ret == -1) {
443 return map_nt_error_from_unix(errno);
446 is_directory = S_ISDIR(psbuf->st_ex_mode);
448 if (ignore_file_system_acl) {
449 TALLOC_FREE(pdesc_next);
450 status = make_default_filesystem_acl(mem_ctx,
451 name,
452 psbuf,
453 &psd);
454 if (!NT_STATUS_IS_OK(status)) {
455 return status;
457 } else {
458 if (is_directory &&
459 !sd_has_inheritable_components(psd,
460 true)) {
461 status = add_directory_inheritable_components(
462 handle,
463 name,
464 psbuf,
465 psd);
466 if (!NT_STATUS_IS_OK(status)) {
467 return status;
470 /* The underlying POSIX module always sets
471 the ~SEC_DESC_DACL_PROTECTED bit, as ACLs
472 can't be inherited in this way under POSIX.
473 Remove it for Windows-style ACLs. */
474 psd->type &= ~SEC_DESC_DACL_PROTECTED;
478 if (!(security_info & SECINFO_OWNER)) {
479 psd->owner_sid = NULL;
481 if (!(security_info & SECINFO_GROUP)) {
482 psd->group_sid = NULL;
484 if (!(security_info & SECINFO_DACL)) {
485 psd->type &= ~SEC_DESC_DACL_PRESENT;
486 psd->dacl = NULL;
488 if (!(security_info & SECINFO_SACL)) {
489 psd->type &= ~SEC_DESC_SACL_PRESENT;
490 psd->sacl = NULL;
493 TALLOC_FREE(blob.data);
494 *ppdesc = psd;
496 if (DEBUGLEVEL >= 10) {
497 DEBUG(10,("get_nt_acl_internal: returning acl for %s is:\n",
498 name ));
499 NDR_PRINT_DEBUG(security_descriptor, psd);
502 return NT_STATUS_OK;
505 /*********************************************************************
506 Fetch a security descriptor given an fsp.
507 *********************************************************************/
509 static NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle,
510 files_struct *fsp,
511 uint32_t security_info,
512 TALLOC_CTX *mem_ctx,
513 struct security_descriptor **ppdesc)
515 return get_nt_acl_internal(handle, fsp,
516 NULL, security_info, mem_ctx, ppdesc);
519 /*********************************************************************
520 Fetch a security descriptor given a pathname.
521 *********************************************************************/
523 static NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
524 const char *name,
525 uint32_t security_info,
526 TALLOC_CTX *mem_ctx,
527 struct security_descriptor **ppdesc)
529 return get_nt_acl_internal(handle, NULL,
530 name, security_info, mem_ctx, ppdesc);
533 /*********************************************************************
534 Store a security descriptor given an fsp.
535 *********************************************************************/
537 static NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
538 uint32_t security_info_sent, const struct security_descriptor *orig_psd)
540 NTSTATUS status;
541 DATA_BLOB blob;
542 struct security_descriptor *pdesc_next = NULL;
543 struct security_descriptor *psd = NULL;
544 uint8_t hash[XATTR_SD_HASH_SIZE];
545 bool chown_needed = false;
546 TALLOC_CTX *frame = talloc_stackframe();
548 if (DEBUGLEVEL >= 10) {
549 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
550 fsp_str_dbg(fsp)));
551 NDR_PRINT_DEBUG(security_descriptor,
552 discard_const_p(struct security_descriptor, orig_psd));
555 status = get_nt_acl_internal(handle, fsp,
556 NULL,
557 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
558 frame,
559 &psd);
561 if (!NT_STATUS_IS_OK(status)) {
562 TALLOC_FREE(frame);
563 return status;
566 psd->revision = orig_psd->revision;
567 /* All our SD's are self relative. */
568 psd->type = orig_psd->type | SEC_DESC_SELF_RELATIVE;
570 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
571 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
572 /* We're changing the owner. */
573 chown_needed = true;
575 psd->owner_sid = orig_psd->owner_sid;
577 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
578 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
579 /* We're changing the group. */
580 chown_needed = true;
582 psd->group_sid = orig_psd->group_sid;
584 if (security_info_sent & SECINFO_DACL) {
585 psd->dacl = orig_psd->dacl;
586 psd->type |= SEC_DESC_DACL_PRESENT;
588 if (security_info_sent & SECINFO_SACL) {
589 psd->sacl = orig_psd->sacl;
590 psd->type |= SEC_DESC_SACL_PRESENT;
593 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
594 if (!NT_STATUS_IS_OK(status)) {
595 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
596 TALLOC_FREE(frame);
597 return status;
599 /* We got access denied here. If we're already root,
600 or we didn't need to do a chown, or the fsp isn't
601 open with WRITE_OWNER access, just return. */
602 if (get_current_uid(handle->conn) == 0 ||
603 chown_needed == false ||
604 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
605 TALLOC_FREE(frame);
606 return NT_STATUS_ACCESS_DENIED;
609 DEBUG(10,("fset_nt_acl_common: overriding chown on file %s "
610 "for sid %s\n",
611 fsp_str_dbg(fsp),
612 sid_string_tos(psd->owner_sid)
615 /* Ok, we failed to chown and we have
616 SEC_STD_WRITE_OWNER access - override. */
617 become_root();
618 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
619 security_info_sent, psd);
620 unbecome_root();
621 if (!NT_STATUS_IS_OK(status)) {
622 TALLOC_FREE(frame);
623 return status;
627 /* Get the full underlying sd, then hash. */
628 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
629 fsp,
630 HASH_SECURITY_INFO,
631 frame,
632 &pdesc_next);
634 if (!NT_STATUS_IS_OK(status)) {
635 TALLOC_FREE(frame);
636 return status;
639 status = hash_sd_sha256(pdesc_next, hash);
640 if (!NT_STATUS_IS_OK(status)) {
641 TALLOC_FREE(frame);
642 return status;
645 if (DEBUGLEVEL >= 10) {
646 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s\n",
647 fsp_str_dbg(fsp)));
648 NDR_PRINT_DEBUG(security_descriptor,
649 discard_const_p(struct security_descriptor, psd));
651 DEBUG(10,("fset_nt_acl_xattr: storing has in xattr sd based on \n"));
652 NDR_PRINT_DEBUG(security_descriptor,
653 discard_const_p(struct security_descriptor, pdesc_next));
655 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
656 if (!NT_STATUS_IS_OK(status)) {
657 DEBUG(10, ("fset_nt_acl_xattr: create_acl_blob failed\n"));
658 TALLOC_FREE(frame);
659 return status;
662 status = store_acl_blob_fsp(handle, fsp, &blob);
664 TALLOC_FREE(frame);
665 return status;
668 static int acl_common_remove_object(vfs_handle_struct *handle,
669 const char *path,
670 bool is_directory)
672 connection_struct *conn = handle->conn;
673 struct file_id id;
674 files_struct *fsp = NULL;
675 int ret = 0;
676 char *parent_dir = NULL;
677 const char *final_component = NULL;
678 struct smb_filename local_fname;
679 int saved_errno = 0;
680 char *saved_dir = NULL;
682 saved_dir = vfs_GetWd(talloc_tos(),conn);
683 if (!saved_dir) {
684 saved_errno = errno;
685 goto out;
688 if (!parent_dirname(talloc_tos(), path,
689 &parent_dir, &final_component)) {
690 saved_errno = ENOMEM;
691 goto out;
694 DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
695 is_directory ? "directory" : "file",
696 parent_dir, final_component ));
698 /* cd into the parent dir to pin it. */
699 ret = vfs_ChDir(conn, parent_dir);
700 if (ret == -1) {
701 saved_errno = errno;
702 goto out;
705 ZERO_STRUCT(local_fname);
706 local_fname.base_name = discard_const_p(char, final_component);
708 /* Must use lstat here. */
709 ret = SMB_VFS_LSTAT(conn, &local_fname);
710 if (ret == -1) {
711 saved_errno = errno;
712 goto out;
715 /* Ensure we have this file open with DELETE access. */
716 id = vfs_file_id_from_sbuf(conn, &local_fname.st);
717 for (fsp = file_find_di_first(conn->sconn, id); fsp;
718 fsp = file_find_di_next(fsp)) {
719 if (fsp->access_mask & DELETE_ACCESS &&
720 fsp->delete_on_close) {
721 /* We did open this for delete,
722 * allow the delete as root.
724 break;
728 if (!fsp) {
729 DEBUG(10,("acl_common_remove_object: %s %s/%s "
730 "not an open file\n",
731 is_directory ? "directory" : "file",
732 parent_dir, final_component ));
733 saved_errno = EACCES;
734 goto out;
737 become_root();
738 if (is_directory) {
739 ret = SMB_VFS_NEXT_RMDIR(handle, final_component);
740 } else {
741 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
743 unbecome_root();
745 if (ret == -1) {
746 saved_errno = errno;
749 out:
751 TALLOC_FREE(parent_dir);
753 if (saved_dir) {
754 vfs_ChDir(conn, saved_dir);
756 if (saved_errno) {
757 errno = saved_errno;
759 return ret;
762 static int rmdir_acl_common(struct vfs_handle_struct *handle,
763 const char *path)
765 int ret;
767 /* Try the normal rmdir first. */
768 ret = SMB_VFS_NEXT_RMDIR(handle, path);
769 if (ret == 0) {
770 return 0;
772 if (errno == EACCES || errno == EPERM) {
773 /* Failed due to access denied,
774 see if we need to root override. */
775 return acl_common_remove_object(handle,
776 path,
777 true);
780 DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
781 path,
782 strerror(errno) ));
783 return -1;
786 static int unlink_acl_common(struct vfs_handle_struct *handle,
787 const struct smb_filename *smb_fname)
789 int ret;
791 /* Try the normal unlink first. */
792 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
793 if (ret == 0) {
794 return 0;
796 if (errno == EACCES || errno == EPERM) {
797 /* Failed due to access denied,
798 see if we need to root override. */
800 /* Don't do anything fancy for streams. */
801 if (smb_fname->stream_name) {
802 return -1;
804 return acl_common_remove_object(handle,
805 smb_fname->base_name,
806 false);
809 DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
810 smb_fname->base_name,
811 strerror(errno) ));
812 return -1;
815 static int chmod_acl_module_common(struct vfs_handle_struct *handle,
816 const char *path, mode_t mode)
818 if (lp_posix_pathnames()) {
819 /* Only allow this on POSIX pathnames. */
820 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
822 return 0;
825 static int fchmod_acl_module_common(struct vfs_handle_struct *handle,
826 struct files_struct *fsp, mode_t mode)
828 if (fsp->posix_open) {
829 /* Only allow this on POSIX opens. */
830 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
832 return 0;
835 static int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
836 const char *name, mode_t mode)
838 if (lp_posix_pathnames()) {
839 /* Only allow this on POSIX pathnames. */
840 return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
842 return 0;
845 static int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
846 struct files_struct *fsp, mode_t mode)
848 if (fsp->posix_open) {
849 /* Only allow this on POSIX opens. */
850 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
852 return 0;