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