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