vfs_acl_xattr|tdb: add option to control default ACL style
[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 #include "passdb/lookup_sid.h"
28
29 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
30                         DATA_BLOB *pblob,
31                         uint16_t hash_type,
32                         uint8_t hash[XATTR_SD_HASH_SIZE]);
33
34 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
35                         vfs_handle_struct *handle,
36                         files_struct *fsp,
37                         const struct smb_filename *smb_fname,
38                         DATA_BLOB *pblob);
39
40 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
41                         files_struct *fsp,
42                         DATA_BLOB *pblob);
43
44 #define HASH_SECURITY_INFO (SECINFO_OWNER | \
45                                 SECINFO_GROUP | \
46                                 SECINFO_DACL | \
47                                 SECINFO_SACL)
48
49 enum default_acl_style {DEFAULT_ACL_POSIX, DEFAULT_ACL_WINDOWS};
50
51 static const struct enum_list default_acl_style[] = {
52         {DEFAULT_ACL_POSIX,     "posix"},
53         {DEFAULT_ACL_WINDOWS,   "windows"}
54 };
55
56 struct acl_common_config {
57         bool ignore_system_acls;
58         enum default_acl_style default_acl_style;
59 };
60
61 static bool init_acl_common_config(vfs_handle_struct *handle)
62 {
63         struct acl_common_config *config = NULL;
64
65         config = talloc_zero(handle->conn, struct acl_common_config);
66         if (config == NULL) {
67                 DBG_ERR("talloc_zero() failed\n");
68                 errno = ENOMEM;
69                 return false;
70         }
71
72         config->ignore_system_acls = lp_parm_bool(SNUM(handle->conn),
73                                                   ACL_MODULE_NAME,
74                                                   "ignore system acls",
75                                                   false);
76         config->default_acl_style = lp_parm_enum(SNUM(handle->conn),
77                                                  ACL_MODULE_NAME,
78                                                  "default acl style",
79                                                  default_acl_style,
80                                                  DEFAULT_ACL_POSIX);
81
82         SMB_VFS_HANDLE_SET_DATA(handle, config, NULL,
83                                 struct acl_common_config,
84                                 return false);
85
86         return true;
87 }
88
89
90 /*******************************************************************
91  Hash a security descriptor.
92 *******************************************************************/
93
94 static NTSTATUS hash_blob_sha256(DATA_BLOB blob,
95                                  uint8_t *hash)
96 {
97         SHA256_CTX tctx;
98
99         memset(hash, '\0', XATTR_SD_HASH_SIZE);
100
101         samba_SHA256_Init(&tctx);
102         samba_SHA256_Update(&tctx, blob.data, blob.length);
103         samba_SHA256_Final(hash, &tctx);
104
105         return NT_STATUS_OK;
106 }
107
108 /*******************************************************************
109  Hash a security descriptor.
110 *******************************************************************/
111
112 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
113                         uint8_t *hash)
114 {
115         DATA_BLOB blob;
116         NTSTATUS status;
117
118         memset(hash, '\0', XATTR_SD_HASH_SIZE);
119         status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
120         if (!NT_STATUS_IS_OK(status)) {
121                 return status;
122         }
123         return hash_blob_sha256(blob, hash);
124 }
125
126 /*******************************************************************
127  Parse out a struct security_descriptor from a DATA_BLOB.
128 *******************************************************************/
129
130 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
131                                TALLOC_CTX *mem_ctx,
132                                struct security_descriptor **ppdesc,
133                                uint16_t *p_hash_type,
134                                uint16_t *p_version,
135                                uint8_t hash[XATTR_SD_HASH_SIZE],
136                                uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
137 {
138         struct xattr_NTACL xacl;
139         enum ndr_err_code ndr_err;
140         size_t sd_size;
141         TALLOC_CTX *frame = talloc_stackframe();
142
143         ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
144                         (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
145
146         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
147                 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
148                         ndr_errstr(ndr_err)));
149                 TALLOC_FREE(frame);
150                 return ndr_map_error2ntstatus(ndr_err);
151         }
152
153         *p_version = xacl.version;
154
155         switch (xacl.version) {
156                 case 1:
157                         *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
158                                         xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
159                                         xacl.info.sd->owner_sid,
160                                         xacl.info.sd->group_sid,
161                                         xacl.info.sd->sacl,
162                                         xacl.info.sd->dacl,
163                                         &sd_size);
164                         /* No hash - null out. */
165                         *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
166                         memset(hash, '\0', XATTR_SD_HASH_SIZE);
167                         break;
168                 case 2:
169                         *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
170                                         xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
171                                         xacl.info.sd_hs2->sd->owner_sid,
172                                         xacl.info.sd_hs2->sd->group_sid,
173                                         xacl.info.sd_hs2->sd->sacl,
174                                         xacl.info.sd_hs2->sd->dacl,
175                                         &sd_size);
176                         /* No hash - null out. */
177                         *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
178                         memset(hash, '\0', XATTR_SD_HASH_SIZE);
179                         break;
180                 case 3:
181                         *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
182                                         xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
183                                         xacl.info.sd_hs3->sd->owner_sid,
184                                         xacl.info.sd_hs3->sd->group_sid,
185                                         xacl.info.sd_hs3->sd->sacl,
186                                         xacl.info.sd_hs3->sd->dacl,
187                                         &sd_size);
188                         *p_hash_type = xacl.info.sd_hs3->hash_type;
189                         /* Current version 3 (if no sys acl hash available). */
190                         memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
191                         break;
192                 case 4:
193                         *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
194                                         xacl.info.sd_hs4->sd->type | SEC_DESC_SELF_RELATIVE,
195                                         xacl.info.sd_hs4->sd->owner_sid,
196                                         xacl.info.sd_hs4->sd->group_sid,
197                                         xacl.info.sd_hs4->sd->sacl,
198                                         xacl.info.sd_hs4->sd->dacl,
199                                         &sd_size);
200                         *p_hash_type = xacl.info.sd_hs4->hash_type;
201                         /* Current version 4. */
202                         memcpy(hash, xacl.info.sd_hs4->hash, XATTR_SD_HASH_SIZE);
203                         memcpy(sys_acl_hash, xacl.info.sd_hs4->sys_acl_hash, XATTR_SD_HASH_SIZE);
204                         break;
205                 default:
206                         TALLOC_FREE(frame);
207                         return NT_STATUS_REVISION_MISMATCH;
208         }
209
210         TALLOC_FREE(frame);
211
212         return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
213 }
214
215 /*******************************************************************
216  Create a DATA_BLOB from a hash of the security descriptor storead at
217  the system layer and the NT ACL we wish to preserve
218 *******************************************************************/
219
220 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
221                         DATA_BLOB *pblob,
222                         uint16_t hash_type,
223                         uint8_t hash[XATTR_SD_HASH_SIZE])
224 {
225         struct xattr_NTACL xacl;
226         struct security_descriptor_hash_v3 sd_hs3;
227         enum ndr_err_code ndr_err;
228         TALLOC_CTX *ctx = talloc_tos();
229
230         ZERO_STRUCT(xacl);
231         ZERO_STRUCT(sd_hs3);
232
233         xacl.version = 3;
234         xacl.info.sd_hs3 = &sd_hs3;
235         xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
236         xacl.info.sd_hs3->hash_type = hash_type;
237         memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
238
239         ndr_err = ndr_push_struct_blob(
240                         pblob, ctx, &xacl,
241                         (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
242
243         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
244                 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
245                         ndr_errstr(ndr_err)));
246                 return ndr_map_error2ntstatus(ndr_err);
247         }
248
249         return NT_STATUS_OK;
250 }
251
252 /*******************************************************************
253  Create a DATA_BLOB from a hash of the security descriptors 
254  (system and NT) stored at the system layer and the NT ACL we wish 
255  to preserve.
256 *******************************************************************/
257
258 static NTSTATUS create_sys_acl_blob(const struct security_descriptor *psd,
259                                     DATA_BLOB *pblob,
260                                     uint16_t hash_type,
261                                     uint8_t hash[XATTR_SD_HASH_SIZE],
262                                     const char *description,
263                                     uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
264 {
265         struct xattr_NTACL xacl;
266         struct security_descriptor_hash_v4 sd_hs4;
267         enum ndr_err_code ndr_err;
268         TALLOC_CTX *ctx = talloc_tos();
269         NTTIME nttime_now;
270         struct timeval now = timeval_current();
271         nttime_now = timeval_to_nttime(&now);
272
273         ZERO_STRUCT(xacl);
274         ZERO_STRUCT(sd_hs4);
275
276         xacl.version = 4;
277         xacl.info.sd_hs4 = &sd_hs4;
278         xacl.info.sd_hs4->sd = discard_const_p(struct security_descriptor, psd);
279         xacl.info.sd_hs4->hash_type = hash_type;
280         memcpy(&xacl.info.sd_hs4->hash[0], hash, XATTR_SD_HASH_SIZE);
281         xacl.info.sd_hs4->description = description;
282         xacl.info.sd_hs4->time = nttime_now;
283         memcpy(&xacl.info.sd_hs4->sys_acl_hash[0], sys_acl_hash, XATTR_SD_HASH_SIZE);
284
285         ndr_err = ndr_push_struct_blob(
286                         pblob, ctx, &xacl,
287                         (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
288
289         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
290                 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
291                         ndr_errstr(ndr_err)));
292                 return ndr_map_error2ntstatus(ndr_err);
293         }
294
295         return NT_STATUS_OK;
296 }
297
298 /*******************************************************************
299  Add in 3 inheritable components for a non-inheritable directory ACL.
300  CREATOR_OWNER/CREATOR_GROUP/WORLD.
301 *******************************************************************/
302
303 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
304                                 const char *name,
305                                 SMB_STRUCT_STAT *psbuf,
306                                 struct security_descriptor *psd)
307 {
308         struct connection_struct *conn = handle->conn;
309         int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
310         struct smb_filename smb_fname;
311         enum security_ace_type acltype;
312         uint32_t access_mask;
313         mode_t dir_mode;
314         mode_t file_mode;
315         mode_t mode;
316         struct security_ace *new_ace_list;
317
318         if (psd->dacl) {
319                 new_ace_list = talloc_zero_array(psd->dacl,
320                                                  struct security_ace,
321                                                  num_aces + 3);
322         } else {
323                 /*
324                  * make_sec_acl() at the bottom of this function
325                  * dupliates new_ace_list
326                  */
327                 new_ace_list = talloc_zero_array(talloc_tos(),
328                                                  struct security_ace,
329                                                  num_aces + 3);
330         }
331
332         if (new_ace_list == NULL) {
333                 return NT_STATUS_NO_MEMORY;
334         }
335
336         /* Fake a quick smb_filename. */
337         ZERO_STRUCT(smb_fname);
338         smb_fname.st = *psbuf;
339         smb_fname.base_name = discard_const_p(char, name);
340
341         dir_mode = unix_mode(conn,
342                         FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
343         file_mode = unix_mode(conn,
344                         FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
345
346         mode = dir_mode | file_mode;
347
348         DEBUG(10, ("add_directory_inheritable_components: directory %s, "
349                 "mode = 0%o\n",
350                 name,
351                 (unsigned int)mode ));
352
353         if (num_aces) {
354                 memcpy(new_ace_list, psd->dacl->aces,
355                         num_aces * sizeof(struct security_ace));
356         }
357         access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
358                                 mode & 0700, false);
359
360         init_sec_ace(&new_ace_list[num_aces],
361                         &global_sid_Creator_Owner,
362                         acltype,
363                         access_mask,
364                         SEC_ACE_FLAG_CONTAINER_INHERIT|
365                                 SEC_ACE_FLAG_OBJECT_INHERIT|
366                                 SEC_ACE_FLAG_INHERIT_ONLY);
367         access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
368                                 (mode << 3) & 0700, false);
369         init_sec_ace(&new_ace_list[num_aces+1],
370                         &global_sid_Creator_Group,
371                         acltype,
372                         access_mask,
373                         SEC_ACE_FLAG_CONTAINER_INHERIT|
374                                 SEC_ACE_FLAG_OBJECT_INHERIT|
375                                 SEC_ACE_FLAG_INHERIT_ONLY);
376         access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
377                                 (mode << 6) & 0700, false);
378         init_sec_ace(&new_ace_list[num_aces+2],
379                         &global_sid_World,
380                         acltype,
381                         access_mask,
382                         SEC_ACE_FLAG_CONTAINER_INHERIT|
383                                 SEC_ACE_FLAG_OBJECT_INHERIT|
384                                 SEC_ACE_FLAG_INHERIT_ONLY);
385         if (psd->dacl) {
386                 psd->dacl->aces = new_ace_list;
387                 psd->dacl->num_aces += 3;
388                 psd->dacl->size += new_ace_list[num_aces].size +
389                         new_ace_list[num_aces+1].size +
390                         new_ace_list[num_aces+2].size;
391         } else {
392                 psd->dacl = make_sec_acl(psd,
393                                 NT4_ACL_REVISION,
394                                 3,
395                                 new_ace_list);
396                 if (psd->dacl == NULL) {
397                         return NT_STATUS_NO_MEMORY;
398                 }
399         }
400         return NT_STATUS_OK;
401 }
402
403 static NTSTATUS make_default_acl_posix(TALLOC_CTX *ctx,
404                                        const char *name,
405                                        SMB_STRUCT_STAT *psbuf,
406                                        struct security_descriptor **ppdesc)
407 {
408         struct dom_sid owner_sid, group_sid;
409         size_t size = 0;
410         struct security_ace aces[4];
411         uint32_t access_mask = 0;
412         mode_t mode = psbuf->st_ex_mode;
413         struct security_acl *new_dacl = NULL;
414         int idx = 0;
415
416         DBG_DEBUG("file %s mode = 0%o\n",name, (int)mode);
417
418         uid_to_sid(&owner_sid, psbuf->st_ex_uid);
419         gid_to_sid(&group_sid, psbuf->st_ex_gid);
420
421         /*
422          We provide up to 4 ACEs
423                 - Owner
424                 - Group
425                 - Everyone
426                 - NT System
427         */
428
429         if (mode & S_IRUSR) {
430                 if (mode & S_IWUSR) {
431                         access_mask |= SEC_RIGHTS_FILE_ALL;
432                 } else {
433                         access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
434                 }
435         }
436         if (mode & S_IWUSR) {
437                 access_mask |= SEC_RIGHTS_FILE_WRITE | SEC_STD_DELETE;
438         }
439
440         init_sec_ace(&aces[idx],
441                         &owner_sid,
442                         SEC_ACE_TYPE_ACCESS_ALLOWED,
443                         access_mask,
444                         0);
445         idx++;
446
447         access_mask = 0;
448         if (mode & S_IRGRP) {
449                 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
450         }
451         if (mode & S_IWGRP) {
452                 /* note that delete is not granted - this matches posix behaviour */
453                 access_mask |= SEC_RIGHTS_FILE_WRITE;
454         }
455         if (access_mask) {
456                 init_sec_ace(&aces[idx],
457                         &group_sid,
458                         SEC_ACE_TYPE_ACCESS_ALLOWED,
459                         access_mask,
460                         0);
461                 idx++;
462         }
463
464         access_mask = 0;
465         if (mode & S_IROTH) {
466                 access_mask |= SEC_RIGHTS_FILE_READ | SEC_FILE_EXECUTE;
467         }
468         if (mode & S_IWOTH) {
469                 access_mask |= SEC_RIGHTS_FILE_WRITE;
470         }
471         if (access_mask) {
472                 init_sec_ace(&aces[idx],
473                         &global_sid_World,
474                         SEC_ACE_TYPE_ACCESS_ALLOWED,
475                         access_mask,
476                         0);
477                 idx++;
478         }
479
480         init_sec_ace(&aces[idx],
481                         &global_sid_System,
482                         SEC_ACE_TYPE_ACCESS_ALLOWED,
483                         SEC_RIGHTS_FILE_ALL,
484                         0);
485         idx++;
486
487         new_dacl = make_sec_acl(ctx,
488                         NT4_ACL_REVISION,
489                         idx,
490                         aces);
491
492         if (!new_dacl) {
493                 return NT_STATUS_NO_MEMORY;
494         }
495
496         *ppdesc = make_sec_desc(ctx,
497                         SECURITY_DESCRIPTOR_REVISION_1,
498                         SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
499                         &owner_sid,
500                         &group_sid,
501                         NULL,
502                         new_dacl,
503                         &size);
504         if (!*ppdesc) {
505                 return NT_STATUS_NO_MEMORY;
506         }
507         return NT_STATUS_OK;
508 }
509
510 static NTSTATUS make_default_filesystem_acl(TALLOC_CTX *ctx,
511                                             struct acl_common_config *config,
512                                             const char *name,
513                                             SMB_STRUCT_STAT *psbuf,
514                                             struct security_descriptor **ppdesc)
515 {
516         NTSTATUS status;
517
518         switch (config->default_acl_style) {
519
520         case DEFAULT_ACL_POSIX:
521                 status =  make_default_acl_posix(ctx, name, psbuf, ppdesc);
522                 break;
523
524         default:
525                 DBG_ERR("unknown acl style %d", config->default_acl_style);
526                 status = NT_STATUS_INTERNAL_ERROR;
527                 break;
528         }
529
530         return status;
531 }
532
533 /**
534  * Validate an ACL blob
535  *
536  * This validates an ACL blob against the underlying filesystem ACL. If this
537  * function returns NT_STATUS_OK ppsd can be
538  *
539  * 1. the ACL from the blob (psd_from_fs=false), or
540  * 2. the ACL from the fs (psd_from_fs=true), or
541  * 3. NULL (!)
542  *
543  * If the return value is anything else then NT_STATUS_OK, ppsd is set to NULL
544  * and psd_from_fs set to false.
545  *
546  * Returning the underlying filesystem ACL in case no. 2 is really just an
547  * optimisation, because some validations have to fetch the filesytem ACL as
548  * part of the validation, so we already have it available and callers might
549  * need it as well.
550  **/
551 static NTSTATUS validate_nt_acl_blob(TALLOC_CTX *mem_ctx,
552                                      vfs_handle_struct *handle,
553                                      files_struct *fsp,
554                                      const struct smb_filename *smb_fname,
555                                      const DATA_BLOB *blob,
556                                      struct security_descriptor **ppsd,
557                                      bool *psd_is_from_fs)
558 {
559         NTSTATUS status;
560         uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
561         uint16_t xattr_version = 0;
562         uint8_t hash[XATTR_SD_HASH_SIZE];
563         uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
564         uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
565         uint8_t sys_acl_hash_tmp[XATTR_SD_HASH_SIZE];
566         struct security_descriptor *psd = NULL;
567         struct security_descriptor *psd_blob = NULL;
568         struct security_descriptor *psd_fs = NULL;
569         char *sys_acl_blob_description = NULL;
570         DATA_BLOB sys_acl_blob = { 0 };
571         struct acl_common_config *config = NULL;
572
573         *ppsd = NULL;
574         *psd_is_from_fs = false;
575
576         SMB_VFS_HANDLE_GET_DATA(handle, config,
577                                 struct acl_common_config,
578                                 return NT_STATUS_UNSUCCESSFUL);
579
580         status = parse_acl_blob(blob,
581                                 mem_ctx,
582                                 &psd_blob,
583                                 &hash_type,
584                                 &xattr_version,
585                                 &hash[0],
586                                 &sys_acl_hash[0]);
587         if (!NT_STATUS_IS_OK(status)) {
588                 DBG_DEBUG("parse_acl_blob returned %s\n", nt_errstr(status));
589                 goto fail;
590         }
591
592         /* determine which type of xattr we got */
593         switch (xattr_version) {
594         case 1:
595         case 2:
596                 /* These xattr types are unilatteral, they do not
597                  * require confirmation of the hash.  In particular,
598                  * the NTVFS file server uses version 1, but
599                  * 'samba-tool ntacl' can set these as well */
600                 *ppsd = psd_blob;
601                 return NT_STATUS_OK;
602         case 3:
603         case 4:
604                 if (config->ignore_system_acls) {
605                         *ppsd = psd_blob;
606                         return NT_STATUS_OK;
607                 }
608
609                 break;
610         default:
611                 DBG_DEBUG("ACL blob revision mismatch (%u) for file %s\n",
612                           (unsigned int)hash_type, smb_fname->base_name);
613                 TALLOC_FREE(psd_blob);
614                 return NT_STATUS_OK;
615         }
616
617         /* determine which type of xattr we got */
618         if (hash_type != XATTR_SD_HASH_TYPE_SHA256) {
619                 DBG_DEBUG("ACL blob hash type (%u) unexpected for file %s\n",
620                           (unsigned int)hash_type, smb_fname->base_name);
621                 TALLOC_FREE(psd_blob);
622                 return NT_STATUS_OK;
623         }
624
625         /* determine which type of xattr we got */
626         switch (xattr_version) {
627         case 4:
628         {
629                 int ret;
630                 if (fsp) {
631                         /* Get the full underlying sd, then hash. */
632                         ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
633                                                                fsp,
634                                                                mem_ctx,
635                                                                &sys_acl_blob_description,
636                                                                &sys_acl_blob);
637                 } else {
638                         /* Get the full underlying sd, then hash. */
639                         ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle,
640                                                  smb_fname->base_name,
641                                                  mem_ctx,
642                                                  &sys_acl_blob_description,
643                                                  &sys_acl_blob);
644                 }
645
646                 /* If we fail to get the ACL blob (for some reason) then this
647                  * is not fatal, we just work based on the NT ACL only */
648                 if (ret == 0) {
649                         status = hash_blob_sha256(sys_acl_blob, sys_acl_hash_tmp);
650                         if (!NT_STATUS_IS_OK(status)) {
651                                 goto fail;
652                         }
653
654                         TALLOC_FREE(sys_acl_blob_description);
655                         TALLOC_FREE(sys_acl_blob.data);
656
657                         if (memcmp(&sys_acl_hash[0], &sys_acl_hash_tmp[0], 
658                                    XATTR_SD_HASH_SIZE) == 0) {
659                                 /* Hash matches, return blob sd. */
660                                 DBG_DEBUG("blob hash matches for file %s\n",
661                                           smb_fname->base_name);
662                                 *ppsd = psd_blob;
663                                 return NT_STATUS_OK;
664                         }
665                 }
666
667                 /* Otherwise, fall though and see if the NT ACL hash matches */
668         }
669         case 3:
670                 /* Get the full underlying sd for the hash
671                    or to return as backup. */
672                 if (fsp) {
673                         status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
674                                                           fsp,
675                                                           HASH_SECURITY_INFO,
676                                                           mem_ctx,
677                                                           &psd_fs);
678                 } else {
679                         status = SMB_VFS_NEXT_GET_NT_ACL(handle,
680                                                          smb_fname,
681                                                          HASH_SECURITY_INFO,
682                                                          mem_ctx,
683                                                          &psd_fs);
684                 }
685
686                 if (!NT_STATUS_IS_OK(status)) {
687                         DBG_DEBUG("get_next_acl for file %s returned %s\n",
688                                   smb_fname->base_name, nt_errstr(status));
689                         goto fail;
690                 }
691
692                 status = hash_sd_sha256(psd_fs, hash_tmp);
693                 if (!NT_STATUS_IS_OK(status)) {
694                         TALLOC_FREE(psd_blob);
695                         *ppsd = psd_fs;
696                         *psd_is_from_fs = true;
697                         return NT_STATUS_OK;
698                 }
699
700                 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
701                         /* Hash matches, return blob sd. */
702                         DBG_DEBUG("blob hash matches for file %s\n",
703                                   smb_fname->base_name);
704                         *ppsd = psd_blob;
705                         return NT_STATUS_OK;
706                 }
707
708                 /* Hash doesn't match, return underlying sd. */
709                 DBG_DEBUG("blob hash does not match for file %s - returning "
710                           "file system SD mapping.\n",
711                           smb_fname->base_name);
712
713                 if (DEBUGLEVEL >= 10) {
714                         DBG_DEBUG("acl for blob hash for %s is:\n",
715                                   smb_fname->base_name);
716                         NDR_PRINT_DEBUG(security_descriptor, psd_fs);
717                 }
718
719                 TALLOC_FREE(psd_blob);
720                 *ppsd = psd_fs;
721                 *psd_is_from_fs = true;
722         }
723
724         return NT_STATUS_OK;
725
726 fail:
727         TALLOC_FREE(psd);
728         TALLOC_FREE(psd_blob);
729         TALLOC_FREE(psd_fs);
730         TALLOC_FREE(sys_acl_blob_description);
731         TALLOC_FREE(sys_acl_blob.data);
732         return status;
733 }
734
735 static NTSTATUS stat_fsp_or_smb_fname(vfs_handle_struct *handle,
736                                       files_struct *fsp,
737                                       const struct smb_filename *smb_fname,
738                                       SMB_STRUCT_STAT *sbuf,
739                                       SMB_STRUCT_STAT **psbuf)
740 {
741         NTSTATUS status;
742         int ret;
743
744         if (fsp) {
745                 status = vfs_stat_fsp(fsp);
746                 if (!NT_STATUS_IS_OK(status)) {
747                         return status;
748                 }
749                 *psbuf = &fsp->fsp_name->st;
750         } else {
751                 /*
752                  * https://bugzilla.samba.org/show_bug.cgi?id=11249
753                  *
754                  * We are currently guaranteed that 'name' here is a
755                  * smb_fname->base_name, which *cannot* contain a stream name
756                  * (':'). vfs_stat_smb_fname() splits a name into a base name +
757                  * stream name, which when we get here we know we've already
758                  * done.  So we have to call the stat or lstat VFS calls
759                  * directly here. Else, a base_name that contains a ':' (from a
760                  * demangled name) will get split again.
761                  *
762                  * FIXME.
763                  * This uglyness will go away once smb_fname is fully plumbed
764                  * through the VFS.
765                  */
766                 ret = vfs_stat_smb_basename(handle->conn,
767                                             smb_fname,
768                                             sbuf);
769                 if (ret == -1) {
770                         return map_nt_error_from_unix(errno);
771                 }
772         }
773
774         return NT_STATUS_OK;
775 }
776
777 /*******************************************************************
778  Pull a DATA_BLOB from an xattr given a pathname.
779  If the hash doesn't match, or doesn't exist - return the underlying
780  filesystem sd.
781 *******************************************************************/
782
783 static NTSTATUS get_nt_acl_internal(vfs_handle_struct *handle,
784                                     files_struct *fsp,
785                                     const struct smb_filename *smb_fname_in,
786                                     uint32_t security_info,
787                                     TALLOC_CTX *mem_ctx,
788                                     struct security_descriptor **ppdesc)
789 {
790         DATA_BLOB blob = data_blob_null;
791         NTSTATUS status;
792         struct security_descriptor *psd = NULL;
793         const struct smb_filename *smb_fname = NULL;
794         bool psd_is_from_fs = false;
795         struct acl_common_config *config = NULL;
796
797         SMB_VFS_HANDLE_GET_DATA(handle, config,
798                                 struct acl_common_config,
799                                 return NT_STATUS_UNSUCCESSFUL);
800
801         if (fsp && smb_fname_in == NULL) {
802                 smb_fname = fsp->fsp_name;
803         } else {
804                 smb_fname = smb_fname_in;
805         }
806
807         DEBUG(10, ("get_nt_acl_internal: name=%s\n", smb_fname->base_name));
808
809         status = get_acl_blob(mem_ctx, handle, fsp, smb_fname, &blob);
810         if (NT_STATUS_IS_OK(status)) {
811                 status = validate_nt_acl_blob(mem_ctx,
812                                               handle,
813                                               fsp,
814                                               smb_fname,
815                                               &blob,
816                                               &psd,
817                                               &psd_is_from_fs);
818                 TALLOC_FREE(blob.data);
819                 if (!NT_STATUS_IS_OK(status)) {
820                         DBG_DEBUG("ACL validation for [%s] failed\n",
821                                   smb_fname->base_name);
822                         goto fail;
823                 }
824         }
825
826         if (psd == NULL) {
827                 /* Get the full underlying sd, as we failed to get the
828                  * blob for the hash, or the revision/hash type wasn't
829                  * known */
830
831                 if (config->ignore_system_acls) {
832                         SMB_STRUCT_STAT sbuf;
833                         SMB_STRUCT_STAT *psbuf = &sbuf;
834
835                         status = stat_fsp_or_smb_fname(handle, fsp, smb_fname,
836                                                        &sbuf, &psbuf);
837                         if (!NT_STATUS_IS_OK(status)) {
838                                 goto fail;
839                         }
840
841                         status = make_default_filesystem_acl(
842                                 mem_ctx,
843                                 config,
844                                 smb_fname->base_name,
845                                 psbuf,
846                                 &psd);
847                         if (!NT_STATUS_IS_OK(status)) {
848                                 goto fail;
849                         }
850                 } else {
851                         if (fsp) {
852                                 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
853                                                                   fsp,
854                                                                   security_info,
855                                                                   mem_ctx,
856                                                                   &psd);
857                         } else {
858                                 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
859                                                                  smb_fname,
860                                                                  security_info,
861                                                                  mem_ctx,
862                                                                  &psd);
863                         }
864
865                         if (!NT_STATUS_IS_OK(status)) {
866                                 DBG_DEBUG("get_next_acl for file %s "
867                                           "returned %s\n",
868                                           smb_fname->base_name,
869                                           nt_errstr(status));
870                                 goto fail;
871                         }
872
873                         psd_is_from_fs = true;
874                 }
875         }
876
877         if (psd_is_from_fs) {
878                 SMB_STRUCT_STAT sbuf;
879                 SMB_STRUCT_STAT *psbuf = &sbuf;
880                 bool is_directory = false;
881
882                 /*
883                  * We're returning the underlying ACL from the
884                  * filesystem. If it's a directory, and has no
885                  * inheritable ACE entries we have to fake them.
886                  */
887
888                 status = stat_fsp_or_smb_fname(handle, fsp, smb_fname,
889                                                &sbuf, &psbuf);
890                 if (!NT_STATUS_IS_OK(status)) {
891                         goto fail;
892                 }
893
894                 is_directory = S_ISDIR(psbuf->st_ex_mode);
895
896                 if (is_directory && !sd_has_inheritable_components(psd, true)) {
897                         status = add_directory_inheritable_components(
898                                 handle,
899                                 smb_fname->base_name,
900                                 psbuf,
901                                 psd);
902                         if (!NT_STATUS_IS_OK(status)) {
903                                 goto fail;
904                         }
905                 }
906
907                 /*
908                  * The underlying POSIX module always sets the
909                  * ~SEC_DESC_DACL_PROTECTED bit, as ACLs can't be inherited in
910                  * this way under POSIX. Remove it for Windows-style ACLs.
911                  */
912                 psd->type &= ~SEC_DESC_DACL_PROTECTED;
913         }
914
915         if (!(security_info & SECINFO_OWNER)) {
916                 psd->owner_sid = NULL;
917         }
918         if (!(security_info & SECINFO_GROUP)) {
919                 psd->group_sid = NULL;
920         }
921         if (!(security_info & SECINFO_DACL)) {
922                 psd->type &= ~SEC_DESC_DACL_PRESENT;
923                 psd->dacl = NULL;
924         }
925         if (!(security_info & SECINFO_SACL)) {
926                 psd->type &= ~SEC_DESC_SACL_PRESENT;
927                 psd->sacl = NULL;
928         }
929
930         if (DEBUGLEVEL >= 10) {
931                 DEBUG(10,("get_nt_acl_internal: returning acl for %s is:\n",
932                         smb_fname->base_name ));
933                 NDR_PRINT_DEBUG(security_descriptor, psd);
934         }
935
936         *ppdesc = psd;
937
938         return NT_STATUS_OK;
939
940 fail:
941         TALLOC_FREE(psd);
942         return status;
943 }
944
945 /*********************************************************************
946  Fetch a security descriptor given an fsp.
947 *********************************************************************/
948
949 static NTSTATUS fget_nt_acl_common(vfs_handle_struct *handle,
950                                    files_struct *fsp,
951                                    uint32_t security_info,
952                                    TALLOC_CTX *mem_ctx,
953                                    struct security_descriptor **ppdesc)
954 {
955         return get_nt_acl_internal(handle, fsp,
956                                    NULL, security_info, mem_ctx, ppdesc);
957 }
958
959 /*********************************************************************
960  Fetch a security descriptor given a pathname.
961 *********************************************************************/
962
963 static NTSTATUS get_nt_acl_common(vfs_handle_struct *handle,
964                                   const struct smb_filename *smb_fname,
965                                   uint32_t security_info,
966                                   TALLOC_CTX *mem_ctx,
967                                   struct security_descriptor **ppdesc)
968 {
969         return get_nt_acl_internal(handle,
970                                 NULL,
971                                 smb_fname,
972                                 security_info,
973                                 mem_ctx,
974                                 ppdesc);
975 }
976
977 /*********************************************************************
978  Set the underlying ACL (e.g. POSIX ACLS, POSIX owner, etc)
979 *********************************************************************/
980 static NTSTATUS set_underlying_acl(vfs_handle_struct *handle, files_struct *fsp,
981                                    struct security_descriptor *psd,
982                                    uint32_t security_info_sent,
983                                    bool chown_needed)
984 {
985         NTSTATUS status =
986             SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
987         if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
988                 return status;
989         }
990
991         /* We got access denied here. If we're already root,
992            or we didn't need to do a chown, or the fsp isn't
993            open with WRITE_OWNER access, just return. */
994         if (get_current_uid(handle->conn) == 0 || chown_needed == false ||
995             !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
996                 return NT_STATUS_ACCESS_DENIED;
997         }
998
999         DEBUG(10, ("fset_nt_acl_common: overriding chown on file %s "
1000                    "for sid %s\n",
1001                    fsp_str_dbg(fsp), sid_string_tos(psd->owner_sid)));
1002
1003         /* Ok, we failed to chown and we have
1004            SEC_STD_WRITE_OWNER access - override. */
1005         become_root();
1006         status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
1007         unbecome_root();
1008
1009         return status;
1010 }
1011
1012 /*********************************************************************
1013  Store a v3 security descriptor
1014 *********************************************************************/
1015 static NTSTATUS store_v3_blob(vfs_handle_struct *handle, files_struct *fsp,
1016                               struct security_descriptor *psd,
1017                               struct security_descriptor *pdesc_next,
1018                               uint8_t hash[XATTR_SD_HASH_SIZE])
1019 {
1020         NTSTATUS status;
1021         DATA_BLOB blob;
1022
1023         if (DEBUGLEVEL >= 10) {
1024                 DEBUG(10, ("fset_nt_acl_xattr: storing xattr sd for file %s\n",
1025                            fsp_str_dbg(fsp)));
1026                 NDR_PRINT_DEBUG(
1027                     security_descriptor,
1028                     discard_const_p(struct security_descriptor, psd));
1029
1030                 if (pdesc_next != NULL) {
1031                         DEBUG(10, ("fset_nt_acl_xattr: storing has in xattr sd "
1032                                    "based on \n"));
1033                         NDR_PRINT_DEBUG(
1034                             security_descriptor,
1035                             discard_const_p(struct security_descriptor,
1036                                             pdesc_next));
1037                 } else {
1038                         DEBUG(10,
1039                               ("fset_nt_acl_xattr: ignoring underlying sd\n"));
1040                 }
1041         }
1042         status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
1043         if (!NT_STATUS_IS_OK(status)) {
1044                 DEBUG(10, ("fset_nt_acl_xattr: create_acl_blob failed\n"));
1045                 return status;
1046         }
1047
1048         status = store_acl_blob_fsp(handle, fsp, &blob);
1049         return status;
1050 }
1051
1052 /*********************************************************************
1053  Store a security descriptor given an fsp.
1054 *********************************************************************/
1055
1056 static NTSTATUS fset_nt_acl_common(vfs_handle_struct *handle, files_struct *fsp,
1057         uint32_t security_info_sent, const struct security_descriptor *orig_psd)
1058 {
1059         NTSTATUS status;
1060         int ret;
1061         DATA_BLOB blob, sys_acl_blob;
1062         struct security_descriptor *pdesc_next = NULL;
1063         struct security_descriptor *psd = NULL;
1064         uint8_t hash[XATTR_SD_HASH_SIZE];
1065         uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
1066         bool chown_needed = false;
1067         char *sys_acl_description;
1068         TALLOC_CTX *frame = talloc_stackframe();
1069         bool ignore_file_system_acl = lp_parm_bool(
1070             SNUM(handle->conn), ACL_MODULE_NAME, "ignore system acls", false);
1071
1072         if (DEBUGLEVEL >= 10) {
1073                 DEBUG(10,("fset_nt_acl_xattr: incoming sd for file %s\n",
1074                           fsp_str_dbg(fsp)));
1075                 NDR_PRINT_DEBUG(security_descriptor,
1076                         discard_const_p(struct security_descriptor, orig_psd));
1077         }
1078
1079         status = get_nt_acl_internal(handle, fsp,
1080                         NULL,
1081                         SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
1082                                      frame,
1083                         &psd);
1084
1085         if (!NT_STATUS_IS_OK(status)) {
1086                 TALLOC_FREE(frame);
1087                 return status;
1088         }
1089
1090         psd->revision = orig_psd->revision;
1091         /* All our SD's are self relative. */
1092         psd->type = orig_psd->type | SEC_DESC_SELF_RELATIVE;
1093
1094         if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
1095                 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
1096                         /* We're changing the owner. */
1097                         chown_needed = true;
1098                 }
1099                 psd->owner_sid = orig_psd->owner_sid;
1100         }
1101         if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
1102                 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
1103                         /* We're changing the group. */
1104                         chown_needed = true;
1105                 }
1106                 psd->group_sid = orig_psd->group_sid;
1107         }
1108         if (security_info_sent & SECINFO_DACL) {
1109                 if (security_descriptor_with_ms_nfs(orig_psd)) {
1110                         /*
1111                          * If the sd contains a MS NFS SID, do
1112                          * nothing, it's a chmod() request from OS X
1113                          * with AAPL context.
1114                          */
1115                         TALLOC_FREE(frame);
1116                         return NT_STATUS_OK;
1117                 }
1118                 psd->dacl = orig_psd->dacl;
1119                 psd->type |= SEC_DESC_DACL_PRESENT;
1120         }
1121         if (security_info_sent & SECINFO_SACL) {
1122                 psd->sacl = orig_psd->sacl;
1123                 psd->type |= SEC_DESC_SACL_PRESENT;
1124         }
1125
1126         if (ignore_file_system_acl) {
1127                 if (chown_needed) {
1128                         /* send only ownership stuff to lower layer */
1129                         security_info_sent &= (SECINFO_OWNER | SECINFO_GROUP);
1130                         status = set_underlying_acl(handle, fsp, psd,
1131                                                     security_info_sent, true);
1132                         if (!NT_STATUS_IS_OK(status)) {
1133                                 TALLOC_FREE(frame);
1134                                 return status;
1135                         }
1136                 }
1137                 ZERO_ARRAY(hash);
1138                 status = store_v3_blob(handle, fsp, psd, NULL, hash);
1139
1140                 TALLOC_FREE(frame);
1141                 return status;
1142         }
1143
1144         status = set_underlying_acl(handle, fsp, psd, security_info_sent,
1145                                     chown_needed);
1146         if (!NT_STATUS_IS_OK(status)) {
1147                 TALLOC_FREE(frame);
1148                 return status;
1149         }
1150
1151         /* Get the full underlying sd, then hash. */
1152         status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
1153                                           fsp,
1154                                           HASH_SECURITY_INFO,
1155                                           frame,
1156                                           &pdesc_next);
1157
1158         if (!NT_STATUS_IS_OK(status)) {
1159                 TALLOC_FREE(frame);
1160                 return status;
1161         }
1162
1163         status = hash_sd_sha256(pdesc_next, hash);
1164         if (!NT_STATUS_IS_OK(status)) {
1165                 TALLOC_FREE(frame);
1166                 return status;
1167         }
1168
1169         /* Get the full underlying sd, then hash. */
1170         ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
1171                                                fsp,
1172                                                frame,
1173                                                &sys_acl_description,
1174                                                &sys_acl_blob);
1175
1176         /* If we fail to get the ACL blob (for some reason) then this
1177          * is not fatal, we just work based on the NT ACL only */
1178         if (ret != 0) {
1179                 status = store_v3_blob(handle, fsp, psd, pdesc_next, hash);
1180
1181                 TALLOC_FREE(frame);
1182                 return status;
1183         }
1184
1185         status = hash_blob_sha256(sys_acl_blob, sys_acl_hash);
1186         if (!NT_STATUS_IS_OK(status)) {
1187                 TALLOC_FREE(frame);
1188                 return status;
1189         }
1190
1191         if (DEBUGLEVEL >= 10) {
1192                 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s based on system ACL\n",
1193                           fsp_str_dbg(fsp)));
1194                 NDR_PRINT_DEBUG(security_descriptor,
1195                                 discard_const_p(struct security_descriptor, psd));
1196
1197                 DEBUG(10,("fset_nt_acl_xattr: storing hash in xattr sd based on system ACL and:\n"));
1198                 NDR_PRINT_DEBUG(security_descriptor,
1199                                 discard_const_p(struct security_descriptor, pdesc_next));
1200         }
1201
1202         /* We store hashes of both the sys ACL blob and the NT
1203          * security desciptor mapped from that ACL so as to improve
1204          * our chances against some inadvertant change breaking the
1205          * hash used */
1206         status = create_sys_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash, 
1207                                      sys_acl_description, sys_acl_hash);
1208         if (!NT_STATUS_IS_OK(status)) {
1209                 DEBUG(10, ("fset_nt_acl_xattr: create_sys_acl_blob failed\n"));
1210                 TALLOC_FREE(frame);
1211                 return status;
1212         }
1213
1214         status = store_acl_blob_fsp(handle, fsp, &blob);
1215
1216         TALLOC_FREE(frame);
1217         return status;
1218 }
1219
1220 static int acl_common_remove_object(vfs_handle_struct *handle,
1221                                         const char *path,
1222                                         bool is_directory)
1223 {
1224         connection_struct *conn = handle->conn;
1225         struct file_id id;
1226         files_struct *fsp = NULL;
1227         int ret = 0;
1228         char *parent_dir = NULL;
1229         const char *final_component = NULL;
1230         struct smb_filename local_fname;
1231         int saved_errno = 0;
1232         char *saved_dir = NULL;
1233
1234         saved_dir = vfs_GetWd(talloc_tos(),conn);
1235         if (!saved_dir) {
1236                 saved_errno = errno;
1237                 goto out;
1238         }
1239
1240         if (!parent_dirname(talloc_tos(), path,
1241                         &parent_dir, &final_component)) {
1242                 saved_errno = ENOMEM;
1243                 goto out;
1244         }
1245
1246         DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
1247                 is_directory ? "directory" : "file",
1248                 parent_dir, final_component ));
1249
1250         /* cd into the parent dir to pin it. */
1251         ret = vfs_ChDir(conn, parent_dir);
1252         if (ret == -1) {
1253                 saved_errno = errno;
1254                 goto out;
1255         }
1256
1257         ZERO_STRUCT(local_fname);
1258         local_fname.base_name = discard_const_p(char, final_component);
1259
1260         /* Must use lstat here. */
1261         ret = SMB_VFS_LSTAT(conn, &local_fname);
1262         if (ret == -1) {
1263                 saved_errno = errno;
1264                 goto out;
1265         }
1266
1267         /* Ensure we have this file open with DELETE access. */
1268         id = vfs_file_id_from_sbuf(conn, &local_fname.st);
1269         for (fsp = file_find_di_first(conn->sconn, id); fsp;
1270                      fsp = file_find_di_next(fsp)) {
1271                 if (fsp->access_mask & DELETE_ACCESS &&
1272                                 fsp->delete_on_close) {
1273                         /* We did open this for delete,
1274                          * allow the delete as root.
1275                          */
1276                         break;
1277                 }
1278         }
1279
1280         if (!fsp) {
1281                 DEBUG(10,("acl_common_remove_object: %s %s/%s "
1282                         "not an open file\n",
1283                         is_directory ? "directory" : "file",
1284                         parent_dir, final_component ));
1285                 saved_errno = EACCES;
1286                 goto out;
1287         }
1288
1289         become_root();
1290         if (is_directory) {
1291                 ret = SMB_VFS_NEXT_RMDIR(handle, &local_fname);
1292         } else {
1293                 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
1294         }
1295         unbecome_root();
1296
1297         if (ret == -1) {
1298                 saved_errno = errno;
1299         }
1300
1301   out:
1302
1303         TALLOC_FREE(parent_dir);
1304
1305         if (saved_dir) {
1306                 vfs_ChDir(conn, saved_dir);
1307         }
1308         if (saved_errno) {
1309                 errno = saved_errno;
1310         }
1311         return ret;
1312 }
1313
1314 static int rmdir_acl_common(struct vfs_handle_struct *handle,
1315                                 const struct smb_filename *smb_fname)
1316 {
1317         int ret;
1318
1319         /* Try the normal rmdir first. */
1320         ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1321         if (ret == 0) {
1322                 return 0;
1323         }
1324         if (errno == EACCES || errno == EPERM) {
1325                 /* Failed due to access denied,
1326                    see if we need to root override. */
1327                 return acl_common_remove_object(handle,
1328                                                 smb_fname->base_name,
1329                                                 true);
1330         }
1331
1332         DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
1333                 smb_fname->base_name,
1334                 strerror(errno) ));
1335         return -1;
1336 }
1337
1338 static int unlink_acl_common(struct vfs_handle_struct *handle,
1339                         const struct smb_filename *smb_fname)
1340 {
1341         int ret;
1342
1343         /* Try the normal unlink first. */
1344         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1345         if (ret == 0) {
1346                 return 0;
1347         }
1348         if (errno == EACCES || errno == EPERM) {
1349                 /* Failed due to access denied,
1350                    see if we need to root override. */
1351
1352                 /* Don't do anything fancy for streams. */
1353                 if (smb_fname->stream_name) {
1354                         return -1;
1355                 }
1356                 return acl_common_remove_object(handle,
1357                                         smb_fname->base_name,
1358                                         false);
1359         }
1360
1361         DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
1362                 smb_fname->base_name,
1363                 strerror(errno) ));
1364         return -1;
1365 }
1366
1367 static int chmod_acl_module_common(struct vfs_handle_struct *handle,
1368                         const struct smb_filename *smb_fname,
1369                         mode_t mode)
1370 {
1371         if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1372                 /* Only allow this on POSIX pathnames. */
1373                 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1374         }
1375         return 0;
1376 }
1377
1378 static int fchmod_acl_module_common(struct vfs_handle_struct *handle,
1379                         struct files_struct *fsp, mode_t mode)
1380 {
1381         if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1382                 /* Only allow this on POSIX opens. */
1383                 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1384         }
1385         return 0;
1386 }
1387
1388 static int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1389                         const struct smb_filename *smb_fname,
1390                         mode_t mode)
1391 {
1392         if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1393                 /* Only allow this on POSIX pathnames. */
1394                 return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
1395         }
1396         return 0;
1397 }
1398
1399 static int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
1400                         struct files_struct *fsp, mode_t mode)
1401 {
1402         if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1403                 /* Only allow this on POSIX opens. */
1404                 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
1405         }
1406         return 0;
1407 }