vfs_acl_common: In add_directory_inheritable_components allocate on psd as parent
[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                         return NT_STATUS_ACCESS_DENIED;
594                 }
595
596                 DEBUG(10,("fset_nt_acl_common: overriding chown on file %s "
597                         "for sid %s\n",
598                         fsp_str_dbg(fsp),
599                         sid_string_tos(psd->owner_sid)
600                         ));
601
602                 /* Ok, we failed to chown and we have
603                    SEC_STD_WRITE_OWNER access - override. */
604                 become_root();
605                 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
606                                 security_info_sent, psd);
607                 unbecome_root();
608                 if (!NT_STATUS_IS_OK(status)) {
609                         TALLOC_FREE(frame);
610                         return status;
611                 }
612         }
613
614         /* Get the full underlying sd, then hash. */
615         status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
616                                           fsp,
617                                           HASH_SECURITY_INFO,
618                                           frame,
619                                           &pdesc_next);
620
621         if (!NT_STATUS_IS_OK(status)) {
622                 TALLOC_FREE(frame);
623                 return status;
624         }
625
626         status = hash_sd_sha256(pdesc_next, hash);
627         if (!NT_STATUS_IS_OK(status)) {
628                 TALLOC_FREE(frame);
629                 return status;
630         }
631
632         if (DEBUGLEVEL >= 10) {
633                 DEBUG(10,("fset_nt_acl_xattr: storing xattr sd for file %s\n",
634                           fsp_str_dbg(fsp)));
635                 NDR_PRINT_DEBUG(security_descriptor,
636                         discard_const_p(struct security_descriptor, psd));
637
638                 DEBUG(10,("fset_nt_acl_xattr: storing has in xattr sd based on \n"));
639                 NDR_PRINT_DEBUG(security_descriptor,
640                         discard_const_p(struct security_descriptor, pdesc_next));
641         }
642         status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
643         if (!NT_STATUS_IS_OK(status)) {
644                 DEBUG(10, ("fset_nt_acl_xattr: create_acl_blob failed\n"));
645                 TALLOC_FREE(frame);
646                 return status;
647         }
648
649         status = store_acl_blob_fsp(handle, fsp, &blob);
650
651         TALLOC_FREE(frame);
652         return status;
653 }
654
655 static int acl_common_remove_object(vfs_handle_struct *handle,
656                                         const char *path,
657                                         bool is_directory)
658 {
659         connection_struct *conn = handle->conn;
660         struct file_id id;
661         files_struct *fsp = NULL;
662         int ret = 0;
663         char *parent_dir = NULL;
664         const char *final_component = NULL;
665         struct smb_filename local_fname;
666         int saved_errno = 0;
667         char *saved_dir = NULL;
668
669         saved_dir = vfs_GetWd(talloc_tos(),conn);
670         if (!saved_dir) {
671                 saved_errno = errno;
672                 goto out;
673         }
674
675         if (!parent_dirname(talloc_tos(), path,
676                         &parent_dir, &final_component)) {
677                 saved_errno = ENOMEM;
678                 goto out;
679         }
680
681         DEBUG(10,("acl_common_remove_object: removing %s %s/%s\n",
682                 is_directory ? "directory" : "file",
683                 parent_dir, final_component ));
684
685         /* cd into the parent dir to pin it. */
686         ret = vfs_ChDir(conn, parent_dir);
687         if (ret == -1) {
688                 saved_errno = errno;
689                 goto out;
690         }
691
692         ZERO_STRUCT(local_fname);
693         local_fname.base_name = discard_const_p(char, final_component);
694
695         /* Must use lstat here. */
696         ret = SMB_VFS_LSTAT(conn, &local_fname);
697         if (ret == -1) {
698                 saved_errno = errno;
699                 goto out;
700         }
701
702         /* Ensure we have this file open with DELETE access. */
703         id = vfs_file_id_from_sbuf(conn, &local_fname.st);
704         for (fsp = file_find_di_first(conn->sconn, id); fsp;
705                      fsp = file_find_di_next(fsp)) {
706                 if (fsp->access_mask & DELETE_ACCESS &&
707                                 fsp->delete_on_close) {
708                         /* We did open this for delete,
709                          * allow the delete as root.
710                          */
711                         break;
712                 }
713         }
714
715         if (!fsp) {
716                 DEBUG(10,("acl_common_remove_object: %s %s/%s "
717                         "not an open file\n",
718                         is_directory ? "directory" : "file",
719                         parent_dir, final_component ));
720                 saved_errno = EACCES;
721                 goto out;
722         }
723
724         become_root();
725         if (is_directory) {
726                 ret = SMB_VFS_NEXT_RMDIR(handle, final_component);
727         } else {
728                 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
729         }
730         unbecome_root();
731
732         if (ret == -1) {
733                 saved_errno = errno;
734         }
735
736   out:
737
738         TALLOC_FREE(parent_dir);
739
740         if (saved_dir) {
741                 vfs_ChDir(conn, saved_dir);
742         }
743         if (saved_errno) {
744                 errno = saved_errno;
745         }
746         return ret;
747 }
748
749 static int rmdir_acl_common(struct vfs_handle_struct *handle,
750                                 const char *path)
751 {
752         int ret;
753
754         /* Try the normal rmdir first. */
755         ret = SMB_VFS_NEXT_RMDIR(handle, path);
756         if (ret == 0) {
757                 return 0;
758         }
759         if (errno == EACCES || errno == EPERM) {
760                 /* Failed due to access denied,
761                    see if we need to root override. */
762                 return acl_common_remove_object(handle,
763                                                 path,
764                                                 true);
765         }
766
767         DEBUG(10,("rmdir_acl_common: unlink of %s failed %s\n",
768                 path,
769                 strerror(errno) ));
770         return -1;
771 }
772
773 static int unlink_acl_common(struct vfs_handle_struct *handle,
774                         const struct smb_filename *smb_fname)
775 {
776         int ret;
777
778         /* Try the normal unlink first. */
779         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
780         if (ret == 0) {
781                 return 0;
782         }
783         if (errno == EACCES || errno == EPERM) {
784                 /* Failed due to access denied,
785                    see if we need to root override. */
786
787                 /* Don't do anything fancy for streams. */
788                 if (smb_fname->stream_name) {
789                         return -1;
790                 }
791                 return acl_common_remove_object(handle,
792                                         smb_fname->base_name,
793                                         false);
794         }
795
796         DEBUG(10,("unlink_acl_common: unlink of %s failed %s\n",
797                 smb_fname->base_name,
798                 strerror(errno) ));
799         return -1;
800 }
801
802 static int chmod_acl_module_common(struct vfs_handle_struct *handle,
803                         const char *path, mode_t mode)
804 {
805         if (lp_posix_pathnames()) {
806                 /* Only allow this on POSIX pathnames. */
807                 return SMB_VFS_NEXT_CHMOD(handle, path, mode);
808         }
809         return 0;
810 }
811
812 static int fchmod_acl_module_common(struct vfs_handle_struct *handle,
813                         struct files_struct *fsp, mode_t mode)
814 {
815         if (fsp->posix_open) {
816                 /* Only allow this on POSIX opens. */
817                 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
818         }
819         return 0;
820 }
821
822 static int chmod_acl_acl_module_common(struct vfs_handle_struct *handle,
823                         const char *name, mode_t mode)
824 {
825         if (lp_posix_pathnames()) {
826                 /* Only allow this on POSIX pathnames. */
827                 return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
828         }
829         return 0;
830 }
831
832 static int fchmod_acl_acl_module_common(struct vfs_handle_struct *handle,
833                         struct files_struct *fsp, mode_t mode)
834 {
835         if (fsp->posix_open) {
836                 /* Only allow this on POSIX opens. */
837                 return SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
838         }
839         return 0;
840 }