Fix second part of bug #8811 - sd_has_inheritable_components segfaults on an SD that...
[amitay/samba.git] / source3 / modules / vfs_acl_common.c
1 /*
2  * Store Windows ACLs in data store - common functions.
3  * #included into modules/vfs_acl_xattr.c and modules/vfs_acl_tdb.c
4  *
5  * Copyright (C) Volker Lendecke, 2008
6  * Copyright (C) Jeremy Allison, 2009
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
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"
27
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]);
32
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);
38
39 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
40                         files_struct *fsp,
41                         DATA_BLOB *pblob);
42
43 #define HASH_SECURITY_INFO (SECINFO_OWNER | \
44                                 SECINFO_GROUP | \
45                                 SECINFO_DACL | \
46                                 SECINFO_SACL)
47
48 /*******************************************************************
49  Hash a security descriptor.
50 *******************************************************************/
51
52 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
53                         uint8_t *hash)
54 {
55         DATA_BLOB blob;
56         SHA256_CTX tctx;
57         NTSTATUS status;
58
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;
63         }
64
65         samba_SHA256_Init(&tctx);
66         samba_SHA256_Update(&tctx, blob.data, blob.length);
67         samba_SHA256_Final(hash, &tctx);
68
69         return NT_STATUS_OK;
70 }
71
72 /*******************************************************************
73  Parse out a struct security_descriptor from a DATA_BLOB.
74 *******************************************************************/
75
76 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
77                                 struct security_descriptor **ppdesc,
78                                 uint16_t *p_hash_type,
79                                 uint8_t hash[XATTR_SD_HASH_SIZE])
80 {
81         TALLOC_CTX *ctx = talloc_tos();
82         struct xattr_NTACL xacl;
83         enum ndr_err_code ndr_err;
84         size_t sd_size;
85
86         ndr_err = ndr_pull_struct_blob(pblob, ctx, &xacl,
87                         (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
88
89         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
90                 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
91                         ndr_errstr(ndr_err)));
92                 return ndr_map_error2ntstatus(ndr_err);
93         }
94
95         switch (xacl.version) {
96                 case 1:
97                         *ppdesc = make_sec_desc(ctx, SD_REVISION,
98                                         xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
99                                         xacl.info.sd->owner_sid,
100                                         xacl.info.sd->group_sid,
101                                         xacl.info.sd->sacl,
102                                         xacl.info.sd->dacl,
103                                         &sd_size);
104                         /* No hash - null out. */
105                         *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
106                         memset(hash, '\0', XATTR_SD_HASH_SIZE);
107                         break;
108                 case 2:
109                         *ppdesc = make_sec_desc(ctx, SD_REVISION,
110                                         xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
111                                         xacl.info.sd_hs2->sd->owner_sid,
112                                         xacl.info.sd_hs2->sd->group_sid,
113                                         xacl.info.sd_hs2->sd->sacl,
114                                         xacl.info.sd_hs2->sd->dacl,
115                                         &sd_size);
116                         /* No hash - null out. */
117                         *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
118                         memset(hash, '\0', XATTR_SD_HASH_SIZE);
119                         break;
120                 case 3:
121                         *ppdesc = make_sec_desc(ctx, SD_REVISION,
122                                         xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
123                                         xacl.info.sd_hs3->sd->owner_sid,
124                                         xacl.info.sd_hs3->sd->group_sid,
125                                         xacl.info.sd_hs3->sd->sacl,
126                                         xacl.info.sd_hs3->sd->dacl,
127                                         &sd_size);
128                         *p_hash_type = xacl.info.sd_hs3->hash_type;
129                         /* Current version 3. */
130                         memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
131                         break;
132                 default:
133                         return NT_STATUS_REVISION_MISMATCH;
134         }
135
136         TALLOC_FREE(xacl.info.sd);
137
138         return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
139 }
140
141 /*******************************************************************
142  Create a DATA_BLOB from a security descriptor.
143 *******************************************************************/
144
145 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
146                         DATA_BLOB *pblob,
147                         uint16_t hash_type,
148                         uint8_t hash[XATTR_SD_HASH_SIZE])
149 {
150         struct xattr_NTACL xacl;
151         struct security_descriptor_hash_v3 sd_hs3;
152         enum ndr_err_code ndr_err;
153         TALLOC_CTX *ctx = talloc_tos();
154
155         ZERO_STRUCT(xacl);
156         ZERO_STRUCT(sd_hs3);
157
158         xacl.version = 3;
159         xacl.info.sd_hs3 = &sd_hs3;
160         xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
161         xacl.info.sd_hs3->hash_type = hash_type;
162         memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
163
164         ndr_err = ndr_push_struct_blob(
165                         pblob, ctx, &xacl,
166                         (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
167
168         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
169                 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
170                         ndr_errstr(ndr_err)));
171                 return ndr_map_error2ntstatus(ndr_err);
172         }
173
174         return NT_STATUS_OK;
175 }
176
177 /*******************************************************************
178  Add in 3 inheritable components for a non-inheritable directory ACL.
179  CREATOR_OWNER/CREATOR_GROUP/WORLD.
180 *******************************************************************/
181
182 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
183                                 const char *name,
184                                 SMB_STRUCT_STAT *psbuf,
185                                 struct security_descriptor *psd)
186 {
187         struct connection_struct *conn = handle->conn;
188         int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
189         struct smb_filename smb_fname;
190         enum security_ace_type acltype;
191         uint32_t access_mask;
192         mode_t dir_mode;
193         mode_t file_mode;
194         mode_t mode;
195         struct security_ace *new_ace_list = talloc_zero_array(talloc_tos(),
196                                                 struct security_ace,
197                                                 num_aces + 3);
198
199         if (new_ace_list == NULL) {
200                 return NT_STATUS_NO_MEMORY;
201         }
202
203         /* Fake a quick smb_filename. */
204         ZERO_STRUCT(smb_fname);
205         smb_fname.st = *psbuf;
206         smb_fname.base_name = discard_const_p(char, name);
207
208         dir_mode = unix_mode(conn,
209                         FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
210         file_mode = unix_mode(conn,
211                         FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
212
213         mode = dir_mode | file_mode;
214
215         DEBUG(10, ("add_directory_inheritable_components: directory %s, "
216                 "mode = 0%o\n",
217                 name,
218                 (unsigned int)mode ));
219
220         if (num_aces) {
221                 memcpy(new_ace_list, psd->dacl->aces,
222                         num_aces * sizeof(struct security_ace));
223         }
224         access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
225                                 mode & 0700, false);
226
227         init_sec_ace(&new_ace_list[num_aces],
228                         &global_sid_Creator_Owner,
229                         acltype,
230                         access_mask,
231                         SEC_ACE_FLAG_CONTAINER_INHERIT|
232                                 SEC_ACE_FLAG_OBJECT_INHERIT|
233                                 SEC_ACE_FLAG_INHERIT_ONLY);
234         access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
235                                 (mode << 3) & 0700, false);
236         init_sec_ace(&new_ace_list[num_aces+1],
237                         &global_sid_Creator_Group,
238                         acltype,
239                         access_mask,
240                         SEC_ACE_FLAG_CONTAINER_INHERIT|
241                                 SEC_ACE_FLAG_OBJECT_INHERIT|
242                                 SEC_ACE_FLAG_INHERIT_ONLY);
243         access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
244                                 (mode << 6) & 0700, false);
245         init_sec_ace(&new_ace_list[num_aces+2],
246                         &global_sid_World,
247                         acltype,
248                         access_mask,
249                         SEC_ACE_FLAG_CONTAINER_INHERIT|
250                                 SEC_ACE_FLAG_OBJECT_INHERIT|
251                                 SEC_ACE_FLAG_INHERIT_ONLY);
252         if (psd->dacl) {
253                 psd->dacl->aces = new_ace_list;
254                 psd->dacl->num_aces += 3;
255         } else {
256                 psd->dacl = make_sec_acl(talloc_tos(),
257                                 NT4_ACL_REVISION,
258                                 3,
259                                 new_ace_list);
260                 if (psd->dacl == NULL) {
261                         return NT_STATUS_NO_MEMORY;
262                 }
263         }
264         return NT_STATUS_OK;
265 }
266
267 /*******************************************************************
268  Pull a DATA_BLOB from an xattr given a pathname.
269  If the hash doesn't match, or doesn't exist - return the underlying
270  filesystem sd.
271 *******************************************************************/
272
273 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
274                                 files_struct *fsp,
275                                 const char *name,
276                                 uint32_t security_info,
277                                 struct security_descriptor **ppdesc)
278 {
279         DATA_BLOB blob = data_blob_null;
280         NTSTATUS status;
281         uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
282         uint8_t hash[XATTR_SD_HASH_SIZE];
283         uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
284         struct security_descriptor *psd = NULL;
285         struct security_descriptor *pdesc_next = NULL;
286         bool ignore_file_system_acl = lp_parm_bool(SNUM(handle->conn),
287                                                 ACL_MODULE_NAME,
288                                                 "ignore system acls",
289                                                 false);
290
291         if (fsp && name == NULL) {
292                 name = fsp->fsp_name->base_name;
293         }
294
295         DEBUG(10, ("get_nt_acl_internal: name=%s\n", name));
296
297         /* Get the full underlying sd for the hash
298            or to return as backup. */
299         if (fsp) {
300                 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
301                                 fsp,
302                                 HASH_SECURITY_INFO,
303                                 &pdesc_next);
304         } else {
305                 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
306                                 name,
307                                 HASH_SECURITY_INFO,
308                                 &pdesc_next);
309         }
310
311         if (!NT_STATUS_IS_OK(status)) {
312                 DEBUG(10, ("get_nt_acl_internal: get_next_acl for file %s "
313                         "returned %s\n",
314                         name,
315                         nt_errstr(status)));
316                 return status;
317         }
318
319         status = get_acl_blob(talloc_tos(), handle, fsp, name, &blob);
320         if (!NT_STATUS_IS_OK(status)) {
321                 DEBUG(10, ("get_nt_acl_internal: get_acl_blob returned %s\n",
322                         nt_errstr(status)));
323                 psd = pdesc_next;
324                 goto out;
325         }
326
327         status = parse_acl_blob(&blob, &psd,
328                                 &hash_type, &hash[0]);
329         if (!NT_STATUS_IS_OK(status)) {
330                 DEBUG(10, ("parse_acl_blob returned %s\n",
331                                 nt_errstr(status)));
332                 psd = pdesc_next;
333                 goto out;
334         }
335
336         /* Ensure the hash type is one we know. */
337         switch (hash_type) {
338                 case XATTR_SD_HASH_TYPE_NONE:
339                         /* No hash, just return blob sd. */
340                         goto out;
341                 case XATTR_SD_HASH_TYPE_SHA256:
342                         break;
343                 default:
344                         DEBUG(10, ("get_nt_acl_internal: ACL blob revision "
345                                 "mismatch (%u) for file %s\n",
346                                 (unsigned int)hash_type,
347                                 name));
348                         TALLOC_FREE(psd);
349                         psd = pdesc_next;
350                         goto out;
351         }
352
353         if (ignore_file_system_acl) {
354                 goto out;
355         }
356
357         status = hash_sd_sha256(pdesc_next, hash_tmp);
358         if (!NT_STATUS_IS_OK(status)) {
359                 TALLOC_FREE(psd);
360                 psd = pdesc_next;
361                 goto out;
362         }
363
364         if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
365                 /* Hash matches, return blob sd. */
366                 DEBUG(10, ("get_nt_acl_internal: blob hash "
367                         "matches for file %s\n",
368                         name ));
369                 goto out;
370         }
371
372         /* Hash doesn't match, return underlying sd. */
373         TALLOC_FREE(psd);
374         psd = pdesc_next;
375
376   out:
377
378         if (psd != pdesc_next) {
379                 /* We're returning the blob, throw
380                  * away the filesystem SD. */
381                 TALLOC_FREE(pdesc_next);
382         } else {
383                 SMB_STRUCT_STAT sbuf;
384                 SMB_STRUCT_STAT *psbuf = &sbuf;
385                 bool is_directory = false;
386                 /*
387                  * We're returning the underlying ACL from the
388                  * filesystem. If it's a directory, and has no
389                  * inheritable ACE entries we have to fake them.
390                  */
391                 if (fsp) {
392                         status = vfs_stat_fsp(fsp);
393                         if (!NT_STATUS_IS_OK(status)) {
394                                 return status;
395                         }
396                         psbuf = &fsp->fsp_name->st;
397                 } else {
398                         int ret = vfs_stat_smb_fname(handle->conn,
399                                                 name,
400                                                 &sbuf);
401                         if (ret == -1) {
402                                 return map_nt_error_from_unix(errno);
403                         }
404                 }
405                 is_directory = S_ISDIR(psbuf->st_ex_mode);
406
407                 if (ignore_file_system_acl) {
408                         TALLOC_FREE(pdesc_next);
409                         status = make_default_filesystem_acl(talloc_tos(),
410                                                 name,
411                                                 psbuf,
412                                                 &psd);
413                         if (!NT_STATUS_IS_OK(status)) {
414                                 return status;
415                         }
416                 } else {
417                         if (is_directory &&
418                                 !sd_has_inheritable_components(psd,
419                                                         true)) {
420                                 status = add_directory_inheritable_components(
421                                                         handle,
422                                                         name,
423                                                         psbuf,
424                                                         psd);
425                                 if (!NT_STATUS_IS_OK(status)) {
426                                         return status;
427                                 }
428                         }
429                         /* The underlying POSIX module always sets
430                            the ~SEC_DESC_DACL_PROTECTED bit, as ACLs
431                            can't be inherited in this way under POSIX.
432                            Remove it for Windows-style ACLs. */
433                         psd->type &= ~SEC_DESC_DACL_PROTECTED;
434                 }
435         }
436
437         if (!(security_info & SECINFO_OWNER)) {
438                 psd->owner_sid = NULL;
439         }
440         if (!(security_info & SECINFO_GROUP)) {
441                 psd->group_sid = NULL;
442         }
443         if (!(security_info & SECINFO_DACL)) {
444                 psd->type &= ~SEC_DESC_DACL_PRESENT;
445                 psd->dacl = NULL;
446         }
447         if (!(security_info & SECINFO_SACL)) {
448                 psd->type &= ~SEC_DESC_SACL_PRESENT;
449                 psd->sacl = NULL;
450         }
451
452         TALLOC_FREE(blob.data);
453         *ppdesc = psd;
454
455         if (DEBUGLEVEL >= 10) {
456                 DEBUG(10,("get_nt_acl_internal: returning acl for %s is:\n",
457                         name ));
458                 NDR_PRINT_DEBUG(security_descriptor, psd);
459         }
460
461         return NT_STATUS_OK;
462 }
463
464 /*********************************************************************
465  Fetch a security descriptor given an fsp.
466 *********************************************************************/
467
468 static NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
469         uint32_t security_info, struct security_descriptor **ppdesc)
470 {
471         return get_nt_acl_internal(handle, fsp,
472                                 NULL, security_info, ppdesc);
473 }
474
475 /*********************************************************************
476  Fetch a security descriptor given a pathname.
477 *********************************************************************/
478
479 static NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
480         const char *name, uint32_t security_info, struct security_descriptor **ppdesc)
481 {
482         return get_nt_acl_internal(handle, NULL,
483                                 name, security_info, ppdesc);
484 }
485
486 /*********************************************************************
487  Store a security descriptor given an fsp.
488 *********************************************************************/
489
490 static NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
491         uint32_t security_info_sent, const struct security_descriptor *orig_psd)
492 {
493         NTSTATUS status;
494         DATA_BLOB blob;
495         struct security_descriptor *pdesc_next = NULL;
496         struct security_descriptor *psd = NULL;
497         uint8_t hash[XATTR_SD_HASH_SIZE];
498         bool chown_needed = false;
499
500         if (DEBUGLEVEL >= 10) {
501                 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
502                           fsp_str_dbg(fsp)));
503                 NDR_PRINT_DEBUG(security_descriptor,
504                         discard_const_p(struct security_descriptor, orig_psd));
505         }
506
507         status = get_nt_acl_internal(handle, fsp,
508                         NULL,
509                         SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
510                         &psd);
511
512         if (!NT_STATUS_IS_OK(status)) {
513                 return status;
514         }
515
516         psd->revision = orig_psd->revision;
517         /* All our SD's are self relative. */
518         psd->type = orig_psd->type | SEC_DESC_SELF_RELATIVE;
519
520         if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
521                 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
522                         /* We're changing the owner. */
523                         chown_needed = true;
524                 }
525                 psd->owner_sid = orig_psd->owner_sid;
526         }
527         if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
528                 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
529                         /* We're changing the group. */
530                         chown_needed = true;
531                 }
532                 psd->group_sid = orig_psd->group_sid;
533         }
534         if (security_info_sent & SECINFO_DACL) {
535                 psd->dacl = orig_psd->dacl;
536                 psd->type |= SEC_DESC_DACL_PRESENT;
537         }
538         if (security_info_sent & SECINFO_SACL) {
539                 psd->sacl = orig_psd->sacl;
540                 psd->type |= SEC_DESC_SACL_PRESENT;
541         }
542
543         status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
544         if (!NT_STATUS_IS_OK(status)) {
545                 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
546                         return status;
547                 }
548                 /* We got access denied here. If we're already root,
549                    or we didn't need to do a chown, or the fsp isn't
550                    open with WRITE_OWNER access, just return. */
551                 if (get_current_uid(handle->conn) == 0 ||
552                                 chown_needed == false ||
553                                 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
554                         return NT_STATUS_ACCESS_DENIED;
555                 }
556
557                 DEBUG(10,("fset_nt_acl_common: overriding chown on file %s "
558                         "for sid %s\n",
559                         fsp_str_dbg(fsp),
560                         sid_string_tos(psd->owner_sid)
561                         ));
562
563                 /* Ok, we failed to chown and we have
564                    SEC_STD_WRITE_OWNER access - override. */
565                 become_root();
566                 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
567                                 security_info_sent, psd);
568                 unbecome_root();
569                 if (!NT_STATUS_IS_OK(status)) {
570                         return status;
571                 }
572         }
573
574         /* Get the full underlying sd, then hash. */
575         status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
576                                 fsp,
577                                 HASH_SECURITY_INFO,
578                                 &pdesc_next);
579
580         if (!NT_STATUS_IS_OK(status)) {
581                 return status;
582         }
583
584         status = hash_sd_sha256(pdesc_next, hash);
585         if (!NT_STATUS_IS_OK(status)) {
586                 return status;
587         }
588
589         if (DEBUGLEVEL >= 10) {
590                 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s\n",
591                           fsp_str_dbg(fsp)));
592                 NDR_PRINT_DEBUG(security_descriptor,
593                         discard_const_p(struct security_descriptor, psd));
594         }
595         create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
596         store_acl_blob_fsp(handle, fsp, &blob);
597
598         return NT_STATUS_OK;
599 }
600
601 static int acl_common_remove_object(vfs_handle_struct *handle,
602                                         const char *path,
603                                         bool is_directory)
604 {
605         connection_struct *conn = handle->conn;
606         struct file_id id;
607         files_struct *fsp = NULL;
608         int ret = 0;
609         char *parent_dir = NULL;
610         const char *final_component = NULL;
611         struct smb_filename local_fname;
612         int saved_errno = 0;
613         char *saved_dir = NULL;
614
615         saved_dir = vfs_GetWd(talloc_tos(),conn);
616         if (!saved_dir) {
617                 saved_errno = errno;
618                 goto out;
619         }
620
621         if (!parent_dirname(talloc_tos(), path,
622                         &parent_dir, &final_component)) {
623                 saved_errno = ENOMEM;
624                 goto out;
625         }
626
627         DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
628                 is_directory ? "directory" : "file",
629                 parent_dir, final_component ));
630
631         /* cd into the parent dir to pin it. */
632         ret = vfs_ChDir(conn, parent_dir);
633         if (ret == -1) {
634                 saved_errno = errno;
635                 goto out;
636         }
637
638         ZERO_STRUCT(local_fname);
639         local_fname.base_name = discard_const_p(char, final_component);
640
641         /* Must use lstat here. */
642         ret = SMB_VFS_LSTAT(conn, &local_fname);
643         if (ret == -1) {
644                 saved_errno = errno;
645                 goto out;
646         }
647
648         /* Ensure we have this file open with DELETE access. */
649         id = vfs_file_id_from_sbuf(conn, &local_fname.st);
650         for (fsp = file_find_di_first(conn->sconn, id); fsp;
651                      fsp = file_find_di_next(fsp)) {
652                 if (fsp->access_mask & DELETE_ACCESS &&
653                                 fsp->delete_on_close) {
654                         /* We did open this for delete,
655                          * allow the delete as root.
656                          */
657                         break;
658                 }
659         }
660
661         if (!fsp) {
662                 DEBUG(10,("acl_common_remove_object: %s %s/%s "
663                         "not an open file\n",
664                         is_directory ? "directory" : "file",
665                         parent_dir, final_component ));
666                 saved_errno = EACCES;
667                 goto out;
668         }
669
670         become_root();
671         if (is_directory) {
672                 ret = SMB_VFS_NEXT_RMDIR(handle, final_component);
673         } else {
674                 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
675         }
676         unbecome_root();
677
678         if (ret == -1) {
679                 saved_errno = errno;
680         }
681
682   out:
683
684         TALLOC_FREE(parent_dir);
685
686         if (saved_dir) {
687                 vfs_ChDir(conn, saved_dir);
688         }
689         if (saved_errno) {
690                 errno = saved_errno;
691         }
692         return ret;
693 }
694
695 static int rmdir_acl_common(struct vfs_handle_struct *handle,
696                                 const char *path)
697 {
698         int ret;
699
700         /* Try the normal rmdir first. */
701         ret = SMB_VFS_NEXT_RMDIR(handle, path);
702         if (ret == 0) {
703                 return 0;
704         }
705         if (errno == EACCES || errno == EPERM) {
706                 /* Failed due to access denied,
707                    see if we need to root override. */
708                 return acl_common_remove_object(handle,
709                                                 path,
710                                                 true);
711         }
712
713         DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
714                 path,
715                 strerror(errno) ));
716         return -1;
717 }
718
719 static int unlink_acl_common(struct vfs_handle_struct *handle,
720                         const struct smb_filename *smb_fname)
721 {
722         int ret;
723
724         /* Try the normal unlink first. */
725         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
726         if (ret == 0) {
727                 return 0;
728         }
729         if (errno == EACCES || errno == EPERM) {
730                 /* Failed due to access denied,
731                    see if we need to root override. */
732
733                 /* Don't do anything fancy for streams. */
734                 if (smb_fname->stream_name) {
735                         return -1;
736                 }
737                 return acl_common_remove_object(handle,
738                                         smb_fname->base_name,
739                                         false);
740         }
741
742         DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
743                 smb_fname->base_name,
744                 strerror(errno) ));
745         return -1;
746 }
747
748 static int chmod_acl_module_common(struct vfs_handle_struct *handle,
749                         const char *path, mode_t mode)
750 {
751         if (lp_posix_pathnames()) {
752                 /* Only allow this on POSIX pathnames. */
753                 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
754         }
755         return 0;
756 }
757
758 static int fchmod_acl_module_common(struct vfs_handle_struct *handle,
759                         struct files_struct *fsp, mode_t mode)
760 {
761         if (fsp->posix_open) {
762                 /* Only allow this on POSIX opens. */
763                 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
764         }
765         return 0;
766 }
767
768 static int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
769                         const char *name, mode_t mode)
770 {
771         if (lp_posix_pathnames()) {
772                 /* Only allow this on POSIX pathnames. */
773                 return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
774         }
775         return 0;
776 }
777
778 static int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
779                         struct files_struct *fsp, mode_t mode)
780 {
781         if (fsp->posix_open) {
782                 /* Only allow this on POSIX opens. */
783                 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
784         }
785         return 0;
786 }