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