selftest:Samba4: report when samba is started and ready
[Samba.git] / source3 / modules / vfs_acl_common.c
blob5cc75ca99c0e4afa7b6f1936da950bf67327a25f
1 /*
2 * Store Windows ACLs in data store - common functions.
3 * #included into modules/vfs_acl_xattr.c and modules/vfs_acl_tdb.c
5 * Copyright (C) Volker Lendecke, 2008
6 * Copyright (C) Jeremy Allison, 2009
7 * Copyright (C) Ralph Böhme, 2016
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "vfs_acl_common.h"
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "librpc/gen_ndr/ndr_xattr.h"
28 #include "../libcli/security/security.h"
29 #include "../librpc/gen_ndr/ndr_security.h"
30 #include "../lib/util/bitmap.h"
31 #include "lib/crypto/sha256.h"
32 #include "passdb/lookup_sid.h"
34 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
35 DATA_BLOB *pblob,
36 uint16_t hash_type,
37 uint8_t hash[XATTR_SD_HASH_SIZE]);
39 #define HASH_SECURITY_INFO (SECINFO_OWNER | \
40 SECINFO_GROUP | \
41 SECINFO_DACL | \
42 SECINFO_SACL)
44 bool init_acl_common_config(vfs_handle_struct *handle,
45 const char *module_name)
47 struct acl_common_config *config = NULL;
48 const struct enum_list *default_acl_style_list = NULL;
50 default_acl_style_list = get_default_acl_style_list();
52 config = talloc_zero(handle->conn, struct acl_common_config);
53 if (config == NULL) {
54 DBG_ERR("talloc_zero() failed\n");
55 errno = ENOMEM;
56 return false;
59 config->ignore_system_acls = lp_parm_bool(SNUM(handle->conn),
60 module_name,
61 "ignore system acls",
62 false);
63 config->default_acl_style = lp_parm_enum(SNUM(handle->conn),
64 module_name,
65 "default acl style",
66 default_acl_style_list,
67 DEFAULT_ACL_POSIX);
69 SMB_VFS_HANDLE_SET_DATA(handle, config, NULL,
70 struct acl_common_config,
71 return false);
73 return true;
77 /*******************************************************************
78 Hash a security descriptor.
79 *******************************************************************/
81 static NTSTATUS hash_blob_sha256(DATA_BLOB blob,
82 uint8_t *hash)
84 SHA256_CTX tctx;
86 memset(hash, '\0', XATTR_SD_HASH_SIZE);
88 samba_SHA256_Init(&tctx);
89 samba_SHA256_Update(&tctx, blob.data, blob.length);
90 samba_SHA256_Final(hash, &tctx);
92 return NT_STATUS_OK;
95 /*******************************************************************
96 Hash a security descriptor.
97 *******************************************************************/
99 static NTSTATUS hash_sd_sha256(struct security_descriptor *psd,
100 uint8_t *hash)
102 DATA_BLOB blob;
103 NTSTATUS status;
105 memset(hash, '\0', XATTR_SD_HASH_SIZE);
106 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
107 if (!NT_STATUS_IS_OK(status)) {
108 return status;
110 return hash_blob_sha256(blob, hash);
113 /*******************************************************************
114 Parse out a struct security_descriptor from a DATA_BLOB.
115 *******************************************************************/
117 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
118 TALLOC_CTX *mem_ctx,
119 struct security_descriptor **ppdesc,
120 uint16_t *p_hash_type,
121 uint16_t *p_version,
122 uint8_t hash[XATTR_SD_HASH_SIZE],
123 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
125 struct xattr_NTACL xacl;
126 enum ndr_err_code ndr_err;
127 size_t sd_size;
128 TALLOC_CTX *frame = talloc_stackframe();
130 ndr_err = ndr_pull_struct_blob(pblob, frame, &xacl,
131 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
133 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
134 DBG_INFO("ndr_pull_xattr_NTACL failed: %s\n",
135 ndr_errstr(ndr_err));
136 TALLOC_FREE(frame);
137 return ndr_map_error2ntstatus(ndr_err);
140 *p_version = xacl.version;
142 switch (xacl.version) {
143 case 1:
144 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
145 xacl.info.sd->type | SEC_DESC_SELF_RELATIVE,
146 xacl.info.sd->owner_sid,
147 xacl.info.sd->group_sid,
148 xacl.info.sd->sacl,
149 xacl.info.sd->dacl,
150 &sd_size);
151 /* No hash - null out. */
152 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
153 memset(hash, '\0', XATTR_SD_HASH_SIZE);
154 break;
155 case 2:
156 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
157 xacl.info.sd_hs2->sd->type | SEC_DESC_SELF_RELATIVE,
158 xacl.info.sd_hs2->sd->owner_sid,
159 xacl.info.sd_hs2->sd->group_sid,
160 xacl.info.sd_hs2->sd->sacl,
161 xacl.info.sd_hs2->sd->dacl,
162 &sd_size);
163 /* No hash - null out. */
164 *p_hash_type = XATTR_SD_HASH_TYPE_NONE;
165 memset(hash, '\0', XATTR_SD_HASH_SIZE);
166 break;
167 case 3:
168 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
169 xacl.info.sd_hs3->sd->type | SEC_DESC_SELF_RELATIVE,
170 xacl.info.sd_hs3->sd->owner_sid,
171 xacl.info.sd_hs3->sd->group_sid,
172 xacl.info.sd_hs3->sd->sacl,
173 xacl.info.sd_hs3->sd->dacl,
174 &sd_size);
175 *p_hash_type = xacl.info.sd_hs3->hash_type;
176 /* Current version 3 (if no sys acl hash available). */
177 memcpy(hash, xacl.info.sd_hs3->hash, XATTR_SD_HASH_SIZE);
178 break;
179 case 4:
180 *ppdesc = make_sec_desc(mem_ctx, SD_REVISION,
181 xacl.info.sd_hs4->sd->type | SEC_DESC_SELF_RELATIVE,
182 xacl.info.sd_hs4->sd->owner_sid,
183 xacl.info.sd_hs4->sd->group_sid,
184 xacl.info.sd_hs4->sd->sacl,
185 xacl.info.sd_hs4->sd->dacl,
186 &sd_size);
187 *p_hash_type = xacl.info.sd_hs4->hash_type;
188 /* Current version 4. */
189 memcpy(hash, xacl.info.sd_hs4->hash, XATTR_SD_HASH_SIZE);
190 memcpy(sys_acl_hash, xacl.info.sd_hs4->sys_acl_hash, XATTR_SD_HASH_SIZE);
191 break;
192 default:
193 TALLOC_FREE(frame);
194 return NT_STATUS_REVISION_MISMATCH;
197 TALLOC_FREE(frame);
199 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
202 /*******************************************************************
203 Create a DATA_BLOB from a hash of the security descriptor storead at
204 the system layer and the NT ACL we wish to preserve
205 *******************************************************************/
207 static NTSTATUS create_acl_blob(const struct security_descriptor *psd,
208 DATA_BLOB *pblob,
209 uint16_t hash_type,
210 uint8_t hash[XATTR_SD_HASH_SIZE])
212 struct xattr_NTACL xacl;
213 struct security_descriptor_hash_v3 sd_hs3;
214 enum ndr_err_code ndr_err;
215 TALLOC_CTX *ctx = talloc_tos();
217 ZERO_STRUCT(xacl);
218 ZERO_STRUCT(sd_hs3);
220 xacl.version = 3;
221 xacl.info.sd_hs3 = &sd_hs3;
222 xacl.info.sd_hs3->sd = discard_const_p(struct security_descriptor, psd);
223 xacl.info.sd_hs3->hash_type = hash_type;
224 memcpy(&xacl.info.sd_hs3->hash[0], hash, XATTR_SD_HASH_SIZE);
226 ndr_err = ndr_push_struct_blob(
227 pblob, ctx, &xacl,
228 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
230 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
231 DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
232 ndr_errstr(ndr_err));
233 return ndr_map_error2ntstatus(ndr_err);
236 return NT_STATUS_OK;
239 /*******************************************************************
240 Create a DATA_BLOB from a hash of the security descriptors
241 (system and NT) stored at the system layer and the NT ACL we wish
242 to preserve.
243 *******************************************************************/
245 static NTSTATUS create_sys_acl_blob(const struct security_descriptor *psd,
246 DATA_BLOB *pblob,
247 uint16_t hash_type,
248 uint8_t hash[XATTR_SD_HASH_SIZE],
249 const char *description,
250 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE])
252 struct xattr_NTACL xacl;
253 struct security_descriptor_hash_v4 sd_hs4;
254 enum ndr_err_code ndr_err;
255 TALLOC_CTX *ctx = talloc_tos();
256 NTTIME nttime_now;
257 struct timeval now = timeval_current();
258 nttime_now = timeval_to_nttime(&now);
260 ZERO_STRUCT(xacl);
261 ZERO_STRUCT(sd_hs4);
263 xacl.version = 4;
264 xacl.info.sd_hs4 = &sd_hs4;
265 xacl.info.sd_hs4->sd = discard_const_p(struct security_descriptor, psd);
266 xacl.info.sd_hs4->hash_type = hash_type;
267 memcpy(&xacl.info.sd_hs4->hash[0], hash, XATTR_SD_HASH_SIZE);
268 xacl.info.sd_hs4->description = description;
269 xacl.info.sd_hs4->time = nttime_now;
270 memcpy(&xacl.info.sd_hs4->sys_acl_hash[0], sys_acl_hash, XATTR_SD_HASH_SIZE);
272 ndr_err = ndr_push_struct_blob(
273 pblob, ctx, &xacl,
274 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
276 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
277 DBG_INFO("ndr_push_xattr_NTACL failed: %s\n",
278 ndr_errstr(ndr_err));
279 return ndr_map_error2ntstatus(ndr_err);
282 return NT_STATUS_OK;
285 /*******************************************************************
286 Add in 3 inheritable components for a non-inheritable directory ACL.
287 CREATOR_OWNER/CREATOR_GROUP/WORLD.
288 *******************************************************************/
290 static NTSTATUS add_directory_inheritable_components(vfs_handle_struct *handle,
291 const char *name,
292 SMB_STRUCT_STAT *psbuf,
293 struct security_descriptor *psd)
295 struct connection_struct *conn = handle->conn;
296 int num_aces = (psd->dacl ? psd->dacl->num_aces : 0);
297 struct smb_filename smb_fname;
298 enum security_ace_type acltype;
299 uint32_t access_mask;
300 mode_t dir_mode;
301 mode_t file_mode;
302 mode_t mode;
303 struct security_ace *new_ace_list;
305 if (psd->dacl) {
306 new_ace_list = talloc_zero_array(psd->dacl,
307 struct security_ace,
308 num_aces + 3);
309 } else {
311 * make_sec_acl() at the bottom of this function
312 * dupliates new_ace_list
314 new_ace_list = talloc_zero_array(talloc_tos(),
315 struct security_ace,
316 num_aces + 3);
319 if (new_ace_list == NULL) {
320 return NT_STATUS_NO_MEMORY;
323 /* Fake a quick smb_filename. */
324 ZERO_STRUCT(smb_fname);
325 smb_fname.st = *psbuf;
326 smb_fname.base_name = discard_const_p(char, name);
328 dir_mode = unix_mode(conn,
329 FILE_ATTRIBUTE_DIRECTORY, &smb_fname, NULL);
330 file_mode = unix_mode(conn,
331 FILE_ATTRIBUTE_ARCHIVE, &smb_fname, NULL);
333 mode = dir_mode | file_mode;
335 DBG_DEBUG("directory %s, mode = 0%o\n", name, (unsigned int)mode);
337 if (num_aces) {
338 memcpy(new_ace_list, psd->dacl->aces,
339 num_aces * sizeof(struct security_ace));
341 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
342 mode & 0700, false);
344 init_sec_ace(&new_ace_list[num_aces],
345 &global_sid_Creator_Owner,
346 acltype,
347 access_mask,
348 SEC_ACE_FLAG_CONTAINER_INHERIT|
349 SEC_ACE_FLAG_OBJECT_INHERIT|
350 SEC_ACE_FLAG_INHERIT_ONLY);
351 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
352 (mode << 3) & 0700, false);
353 init_sec_ace(&new_ace_list[num_aces+1],
354 &global_sid_Creator_Group,
355 acltype,
356 access_mask,
357 SEC_ACE_FLAG_CONTAINER_INHERIT|
358 SEC_ACE_FLAG_OBJECT_INHERIT|
359 SEC_ACE_FLAG_INHERIT_ONLY);
360 access_mask = map_canon_ace_perms(SNUM(conn), &acltype,
361 (mode << 6) & 0700, false);
362 init_sec_ace(&new_ace_list[num_aces+2],
363 &global_sid_World,
364 acltype,
365 access_mask,
366 SEC_ACE_FLAG_CONTAINER_INHERIT|
367 SEC_ACE_FLAG_OBJECT_INHERIT|
368 SEC_ACE_FLAG_INHERIT_ONLY);
369 if (psd->dacl) {
370 psd->dacl->aces = new_ace_list;
371 psd->dacl->num_aces += 3;
372 psd->dacl->size += new_ace_list[num_aces].size +
373 new_ace_list[num_aces+1].size +
374 new_ace_list[num_aces+2].size;
375 } else {
376 psd->dacl = make_sec_acl(psd,
377 NT4_ACL_REVISION,
379 new_ace_list);
380 if (psd->dacl == NULL) {
381 return NT_STATUS_NO_MEMORY;
384 return NT_STATUS_OK;
388 * Validate an ACL blob
390 * This validates an ACL blob against the underlying filesystem ACL. If this
391 * function returns NT_STATUS_OK ppsd can be
393 * 1. the ACL from the blob (psd_from_fs=false), or
394 * 2. the ACL from the fs (psd_from_fs=true), or
395 * 3. NULL (!)
397 * If the return value is anything else then NT_STATUS_OK, ppsd is set to NULL
398 * and psd_from_fs set to false.
400 * Returning the underlying filesystem ACL in case no. 2 is really just an
401 * optimisation, because some validations have to fetch the filesytem ACL as
402 * part of the validation, so we already have it available and callers might
403 * need it as well.
405 static NTSTATUS validate_nt_acl_blob(TALLOC_CTX *mem_ctx,
406 vfs_handle_struct *handle,
407 files_struct *fsp,
408 const struct smb_filename *smb_fname,
409 const DATA_BLOB *blob,
410 struct security_descriptor **ppsd,
411 bool *psd_is_from_fs)
413 NTSTATUS status;
414 uint16_t hash_type = XATTR_SD_HASH_TYPE_NONE;
415 uint16_t xattr_version = 0;
416 uint8_t hash[XATTR_SD_HASH_SIZE];
417 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
418 uint8_t hash_tmp[XATTR_SD_HASH_SIZE];
419 uint8_t sys_acl_hash_tmp[XATTR_SD_HASH_SIZE];
420 struct security_descriptor *psd = NULL;
421 struct security_descriptor *psd_blob = NULL;
422 struct security_descriptor *psd_fs = NULL;
423 char *sys_acl_blob_description = NULL;
424 DATA_BLOB sys_acl_blob = { 0 };
425 struct acl_common_config *config = NULL;
427 *ppsd = NULL;
428 *psd_is_from_fs = false;
430 SMB_VFS_HANDLE_GET_DATA(handle, config,
431 struct acl_common_config,
432 return NT_STATUS_UNSUCCESSFUL);
434 status = parse_acl_blob(blob,
435 mem_ctx,
436 &psd_blob,
437 &hash_type,
438 &xattr_version,
439 &hash[0],
440 &sys_acl_hash[0]);
441 if (!NT_STATUS_IS_OK(status)) {
442 DBG_DEBUG("parse_acl_blob returned %s\n", nt_errstr(status));
443 goto fail;
446 /* determine which type of xattr we got */
447 switch (xattr_version) {
448 case 1:
449 case 2:
450 /* These xattr types are unilatteral, they do not
451 * require confirmation of the hash. In particular,
452 * the NTVFS file server uses version 1, but
453 * 'samba-tool ntacl' can set these as well */
454 *ppsd = psd_blob;
455 return NT_STATUS_OK;
456 case 3:
457 case 4:
458 if (config->ignore_system_acls) {
459 *ppsd = psd_blob;
460 return NT_STATUS_OK;
463 break;
464 default:
465 DBG_DEBUG("ACL blob revision mismatch (%u) for file %s\n",
466 (unsigned int)hash_type, smb_fname->base_name);
467 TALLOC_FREE(psd_blob);
468 return NT_STATUS_OK;
471 /* determine which type of xattr we got */
472 if (hash_type != XATTR_SD_HASH_TYPE_SHA256) {
473 DBG_DEBUG("ACL blob hash type (%u) unexpected for file %s\n",
474 (unsigned int)hash_type, smb_fname->base_name);
475 TALLOC_FREE(psd_blob);
476 return NT_STATUS_OK;
479 /* determine which type of xattr we got */
480 switch (xattr_version) {
481 case 4:
483 int ret;
484 if (fsp) {
485 /* Get the full underlying sd, then hash. */
486 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
487 fsp,
488 mem_ctx,
489 &sys_acl_blob_description,
490 &sys_acl_blob);
491 } else {
492 /* Get the full underlying sd, then hash. */
493 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(handle,
494 smb_fname,
495 mem_ctx,
496 &sys_acl_blob_description,
497 &sys_acl_blob);
500 /* If we fail to get the ACL blob (for some reason) then this
501 * is not fatal, we just work based on the NT ACL only */
502 if (ret == 0) {
503 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash_tmp);
504 if (!NT_STATUS_IS_OK(status)) {
505 goto fail;
508 TALLOC_FREE(sys_acl_blob_description);
509 TALLOC_FREE(sys_acl_blob.data);
511 if (memcmp(&sys_acl_hash[0], &sys_acl_hash_tmp[0],
512 XATTR_SD_HASH_SIZE) == 0) {
513 /* Hash matches, return blob sd. */
514 DBG_DEBUG("blob hash matches for file %s\n",
515 smb_fname->base_name);
516 *ppsd = psd_blob;
517 return NT_STATUS_OK;
521 /* Otherwise, fall though and see if the NT ACL hash matches */
522 FALL_THROUGH;
524 case 3:
525 /* Get the full underlying sd for the hash
526 or to return as backup. */
527 if (fsp) {
528 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
529 fsp,
530 HASH_SECURITY_INFO,
531 mem_ctx,
532 &psd_fs);
533 } else {
534 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
535 smb_fname,
536 HASH_SECURITY_INFO,
537 mem_ctx,
538 &psd_fs);
541 if (!NT_STATUS_IS_OK(status)) {
542 DBG_DEBUG("get_next_acl for file %s returned %s\n",
543 smb_fname->base_name, nt_errstr(status));
544 goto fail;
547 status = hash_sd_sha256(psd_fs, hash_tmp);
548 if (!NT_STATUS_IS_OK(status)) {
549 TALLOC_FREE(psd_blob);
550 *ppsd = psd_fs;
551 *psd_is_from_fs = true;
552 return NT_STATUS_OK;
555 if (memcmp(&hash[0], &hash_tmp[0], XATTR_SD_HASH_SIZE) == 0) {
556 /* Hash matches, return blob sd. */
557 DBG_DEBUG("blob hash matches for file %s\n",
558 smb_fname->base_name);
559 *ppsd = psd_blob;
560 return NT_STATUS_OK;
563 /* Hash doesn't match, return underlying sd. */
564 DBG_DEBUG("blob hash does not match for file %s - returning "
565 "file system SD mapping.\n",
566 smb_fname->base_name);
568 if (DEBUGLEVEL >= 10) {
569 DBG_DEBUG("acl for blob hash for %s is:\n",
570 smb_fname->base_name);
571 NDR_PRINT_DEBUG(security_descriptor, psd_fs);
574 TALLOC_FREE(psd_blob);
575 *ppsd = psd_fs;
576 *psd_is_from_fs = true;
579 return NT_STATUS_OK;
581 fail:
582 TALLOC_FREE(psd);
583 TALLOC_FREE(psd_blob);
584 TALLOC_FREE(psd_fs);
585 TALLOC_FREE(sys_acl_blob_description);
586 TALLOC_FREE(sys_acl_blob.data);
587 return status;
590 static NTSTATUS stat_fsp_or_smb_fname(vfs_handle_struct *handle,
591 files_struct *fsp,
592 const struct smb_filename *smb_fname,
593 SMB_STRUCT_STAT *sbuf,
594 SMB_STRUCT_STAT **psbuf)
596 NTSTATUS status;
597 int ret;
599 if (fsp) {
600 status = vfs_stat_fsp(fsp);
601 if (!NT_STATUS_IS_OK(status)) {
602 return status;
604 *psbuf = &fsp->fsp_name->st;
605 } else {
607 * https://bugzilla.samba.org/show_bug.cgi?id=11249
609 * We are currently guaranteed that 'name' here is a
610 * smb_fname->base_name, which *cannot* contain a stream name
611 * (':'). vfs_stat_smb_fname() splits a name into a base name +
612 * stream name, which when we get here we know we've already
613 * done. So we have to call the stat or lstat VFS calls
614 * directly here. Else, a base_name that contains a ':' (from a
615 * demangled name) will get split again.
617 * FIXME.
618 * This uglyness will go away once smb_fname is fully plumbed
619 * through the VFS.
621 ret = vfs_stat_smb_basename(handle->conn,
622 smb_fname,
623 sbuf);
624 if (ret == -1) {
625 return map_nt_error_from_unix(errno);
629 return NT_STATUS_OK;
632 /*******************************************************************
633 Pull a DATA_BLOB from an xattr given a pathname.
634 If the hash doesn't match, or doesn't exist - return the underlying
635 filesystem sd.
636 *******************************************************************/
638 NTSTATUS get_nt_acl_common(
639 NTSTATUS (*get_acl_blob_fn)(TALLOC_CTX *ctx,
640 vfs_handle_struct *handle,
641 files_struct *fsp,
642 const struct smb_filename *smb_fname,
643 DATA_BLOB *pblob),
644 vfs_handle_struct *handle,
645 files_struct *fsp,
646 const struct smb_filename *smb_fname_in,
647 uint32_t security_info,
648 TALLOC_CTX *mem_ctx,
649 struct security_descriptor **ppdesc)
651 DATA_BLOB blob = data_blob_null;
652 NTSTATUS status;
653 struct security_descriptor *psd = NULL;
654 const struct smb_filename *smb_fname = NULL;
655 bool psd_is_from_fs = false;
656 struct acl_common_config *config = NULL;
658 SMB_VFS_HANDLE_GET_DATA(handle, config,
659 struct acl_common_config,
660 return NT_STATUS_UNSUCCESSFUL);
662 if (fsp && smb_fname_in == NULL) {
663 smb_fname = fsp->fsp_name;
664 } else {
665 smb_fname = smb_fname_in;
668 DBG_DEBUG("name=%s\n", smb_fname->base_name);
670 status = get_acl_blob_fn(mem_ctx, handle, fsp, smb_fname, &blob);
671 if (NT_STATUS_IS_OK(status)) {
672 status = validate_nt_acl_blob(mem_ctx,
673 handle,
674 fsp,
675 smb_fname,
676 &blob,
677 &psd,
678 &psd_is_from_fs);
679 TALLOC_FREE(blob.data);
680 if (!NT_STATUS_IS_OK(status)) {
681 DBG_DEBUG("ACL validation for [%s] failed\n",
682 smb_fname->base_name);
683 goto fail;
687 if (psd == NULL) {
688 /* Get the full underlying sd, as we failed to get the
689 * blob for the hash, or the revision/hash type wasn't
690 * known */
692 if (config->ignore_system_acls) {
693 SMB_STRUCT_STAT sbuf;
694 SMB_STRUCT_STAT *psbuf = &sbuf;
696 status = stat_fsp_or_smb_fname(handle, fsp, smb_fname,
697 &sbuf, &psbuf);
698 if (!NT_STATUS_IS_OK(status)) {
699 goto fail;
702 status = make_default_filesystem_acl(
703 mem_ctx,
704 config->default_acl_style,
705 smb_fname->base_name,
706 psbuf,
707 &psd);
708 if (!NT_STATUS_IS_OK(status)) {
709 goto fail;
711 } else {
712 if (fsp) {
713 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
714 fsp,
715 security_info,
716 mem_ctx,
717 &psd);
718 } else {
719 status = SMB_VFS_NEXT_GET_NT_ACL(handle,
720 smb_fname,
721 security_info,
722 mem_ctx,
723 &psd);
726 if (!NT_STATUS_IS_OK(status)) {
727 DBG_DEBUG("get_next_acl for file %s "
728 "returned %s\n",
729 smb_fname->base_name,
730 nt_errstr(status));
731 goto fail;
734 psd_is_from_fs = true;
738 if (psd_is_from_fs) {
739 SMB_STRUCT_STAT sbuf;
740 SMB_STRUCT_STAT *psbuf = &sbuf;
741 bool is_directory = false;
744 * We're returning the underlying ACL from the
745 * filesystem. If it's a directory, and has no
746 * inheritable ACE entries we have to fake them.
749 status = stat_fsp_or_smb_fname(handle, fsp, smb_fname,
750 &sbuf, &psbuf);
751 if (!NT_STATUS_IS_OK(status)) {
752 goto fail;
755 is_directory = S_ISDIR(psbuf->st_ex_mode);
757 if (is_directory && !sd_has_inheritable_components(psd, true)) {
758 status = add_directory_inheritable_components(
759 handle,
760 smb_fname->base_name,
761 psbuf,
762 psd);
763 if (!NT_STATUS_IS_OK(status)) {
764 goto fail;
769 * The underlying POSIX module always sets the
770 * ~SEC_DESC_DACL_PROTECTED bit, as ACLs can't be inherited in
771 * this way under POSIX. Remove it for Windows-style ACLs.
773 psd->type &= ~SEC_DESC_DACL_PROTECTED;
776 if (!(security_info & SECINFO_OWNER)) {
777 psd->owner_sid = NULL;
779 if (!(security_info & SECINFO_GROUP)) {
780 psd->group_sid = NULL;
782 if (!(security_info & SECINFO_DACL)) {
783 psd->type &= ~SEC_DESC_DACL_PRESENT;
784 psd->dacl = NULL;
786 if (!(security_info & SECINFO_SACL)) {
787 psd->type &= ~SEC_DESC_SACL_PRESENT;
788 psd->sacl = NULL;
791 if (DEBUGLEVEL >= 10) {
792 DBG_DEBUG("returning acl for %s is:\n",
793 smb_fname->base_name);
794 NDR_PRINT_DEBUG(security_descriptor, psd);
797 *ppdesc = psd;
799 return NT_STATUS_OK;
801 fail:
802 TALLOC_FREE(psd);
803 return status;
806 /*********************************************************************
807 Set the underlying ACL (e.g. POSIX ACLS, POSIX owner, etc)
808 *********************************************************************/
809 static NTSTATUS set_underlying_acl(vfs_handle_struct *handle, files_struct *fsp,
810 struct security_descriptor *psd,
811 uint32_t security_info_sent,
812 bool chown_needed)
814 NTSTATUS status;
815 const struct security_token *token = NULL;
816 struct dom_sid_buf buf;
818 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
819 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
820 return status;
823 /* We got access denied here. If we're already root,
824 or we didn't need to do a chown, or the fsp isn't
825 open with WRITE_OWNER access, just return. */
826 if (get_current_uid(handle->conn) == 0 || chown_needed == false ||
827 !(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
828 return NT_STATUS_ACCESS_DENIED;
832 * Only allow take-ownership, not give-ownership. That's the way Windows
833 * implements SEC_STD_WRITE_OWNER. MS-FSA 2.1.5.16 just states: If
834 * InputBuffer.OwnerSid is not a valid owner SID for a file in the
835 * objectstore, as determined in an implementation specific manner, the
836 * object store MUST return STATUS_INVALID_OWNER.
838 token = get_current_nttok(fsp->conn);
839 if (!security_token_is_sid(token, psd->owner_sid)) {
840 return NT_STATUS_INVALID_OWNER;
843 DBG_DEBUG("overriding chown on file %s for sid %s\n",
844 fsp_str_dbg(fsp),
845 dom_sid_str_buf(psd->owner_sid, &buf));
847 /* Ok, we failed to chown and we have
848 SEC_STD_WRITE_OWNER access - override. */
849 become_root();
850 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
851 unbecome_root();
853 return status;
856 /*********************************************************************
857 Store a v3 security descriptor
858 *********************************************************************/
859 static NTSTATUS store_v3_blob(
860 NTSTATUS (*store_acl_blob_fsp_fn)(vfs_handle_struct *handle,
861 files_struct *fsp,
862 DATA_BLOB *pblob),
863 vfs_handle_struct *handle, files_struct *fsp,
864 struct security_descriptor *psd,
865 struct security_descriptor *pdesc_next,
866 uint8_t hash[XATTR_SD_HASH_SIZE])
868 NTSTATUS status;
869 DATA_BLOB blob;
871 if (DEBUGLEVEL >= 10) {
872 DBG_DEBUG("storing xattr sd for file %s\n",
873 fsp_str_dbg(fsp));
874 NDR_PRINT_DEBUG(
875 security_descriptor,
876 discard_const_p(struct security_descriptor, psd));
878 if (pdesc_next != NULL) {
879 DBG_DEBUG("storing xattr sd based on \n");
880 NDR_PRINT_DEBUG(
881 security_descriptor,
882 discard_const_p(struct security_descriptor,
883 pdesc_next));
884 } else {
885 DBG_DEBUG("ignoring underlying sd\n");
888 status = create_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash);
889 if (!NT_STATUS_IS_OK(status)) {
890 DBG_DEBUG("create_acl_blob failed\n");
891 return status;
894 status = store_acl_blob_fsp_fn(handle, fsp, &blob);
895 return status;
898 /*********************************************************************
899 Store a security descriptor given an fsp.
900 *********************************************************************/
902 NTSTATUS fset_nt_acl_common(
903 NTSTATUS (*get_acl_blob_fn)(TALLOC_CTX *ctx,
904 vfs_handle_struct *handle,
905 files_struct *fsp,
906 const struct smb_filename *smb_fname,
907 DATA_BLOB *pblob),
908 NTSTATUS (*store_acl_blob_fsp_fn)(vfs_handle_struct *handle,
909 files_struct *fsp,
910 DATA_BLOB *pblob),
911 const char *module_name,
912 vfs_handle_struct *handle, files_struct *fsp,
913 uint32_t security_info_sent,
914 const struct security_descriptor *orig_psd)
916 NTSTATUS status;
917 int ret;
918 DATA_BLOB blob, sys_acl_blob;
919 struct security_descriptor *pdesc_next = NULL;
920 struct security_descriptor *psd = NULL;
921 uint8_t hash[XATTR_SD_HASH_SIZE];
922 uint8_t sys_acl_hash[XATTR_SD_HASH_SIZE];
923 bool chown_needed = false;
924 char *sys_acl_description;
925 TALLOC_CTX *frame = talloc_stackframe();
926 bool ignore_file_system_acl = lp_parm_bool(
927 SNUM(handle->conn), module_name, "ignore system acls", false);
929 if (DEBUGLEVEL >= 10) {
930 DBG_DEBUG("incoming sd for file %s\n", fsp_str_dbg(fsp));
931 NDR_PRINT_DEBUG(security_descriptor,
932 discard_const_p(struct security_descriptor, orig_psd));
935 status = get_nt_acl_common(get_acl_blob_fn, handle, fsp,
936 NULL,
937 SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL,
938 frame,
939 &psd);
941 if (!NT_STATUS_IS_OK(status)) {
942 TALLOC_FREE(frame);
943 return status;
946 psd->revision = orig_psd->revision;
947 if (security_info_sent & SECINFO_DACL) {
948 psd->type = orig_psd->type;
949 /* All our SD's are self relative. */
950 psd->type |= SEC_DESC_SELF_RELATIVE;
953 if ((security_info_sent & SECINFO_OWNER) && (orig_psd->owner_sid != NULL)) {
954 if (!dom_sid_equal(orig_psd->owner_sid, psd->owner_sid)) {
955 /* We're changing the owner. */
956 chown_needed = true;
958 psd->owner_sid = orig_psd->owner_sid;
960 if ((security_info_sent & SECINFO_GROUP) && (orig_psd->group_sid != NULL)) {
961 if (!dom_sid_equal(orig_psd->group_sid, psd->group_sid)) {
962 /* We're changing the group. */
963 chown_needed = true;
965 psd->group_sid = orig_psd->group_sid;
967 if (security_info_sent & SECINFO_DACL) {
968 if (security_descriptor_with_ms_nfs(orig_psd)) {
970 * If the sd contains a MS NFS SID, do
971 * nothing, it's a chmod() request from OS X
972 * with AAPL context.
974 TALLOC_FREE(frame);
975 return NT_STATUS_OK;
977 psd->dacl = orig_psd->dacl;
978 psd->type |= SEC_DESC_DACL_PRESENT;
980 if (security_info_sent & SECINFO_SACL) {
981 psd->sacl = orig_psd->sacl;
982 psd->type |= SEC_DESC_SACL_PRESENT;
985 if (ignore_file_system_acl) {
986 if (chown_needed) {
987 /* send only ownership stuff to lower layer */
988 security_info_sent &= (SECINFO_OWNER | SECINFO_GROUP);
989 status = set_underlying_acl(handle, fsp, psd,
990 security_info_sent, true);
991 if (!NT_STATUS_IS_OK(status)) {
992 TALLOC_FREE(frame);
993 return status;
996 ZERO_ARRAY(hash);
997 status = store_v3_blob(store_acl_blob_fsp_fn, handle, fsp, psd,
998 NULL, hash);
1000 TALLOC_FREE(frame);
1001 return status;
1004 status = set_underlying_acl(handle, fsp, psd, security_info_sent,
1005 chown_needed);
1006 if (!NT_STATUS_IS_OK(status)) {
1007 TALLOC_FREE(frame);
1008 return status;
1011 /* Get the full underlying sd, then hash. */
1012 status = SMB_VFS_NEXT_FGET_NT_ACL(handle,
1013 fsp,
1014 HASH_SECURITY_INFO,
1015 frame,
1016 &pdesc_next);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 TALLOC_FREE(frame);
1020 return status;
1023 status = hash_sd_sha256(pdesc_next, hash);
1024 if (!NT_STATUS_IS_OK(status)) {
1025 TALLOC_FREE(frame);
1026 return status;
1029 /* Get the full underlying sd, then hash. */
1030 ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle,
1031 fsp,
1032 frame,
1033 &sys_acl_description,
1034 &sys_acl_blob);
1036 /* If we fail to get the ACL blob (for some reason) then this
1037 * is not fatal, we just work based on the NT ACL only */
1038 if (ret != 0) {
1039 status = store_v3_blob(store_acl_blob_fsp_fn, handle, fsp, psd,
1040 pdesc_next, hash);
1042 TALLOC_FREE(frame);
1043 return status;
1046 status = hash_blob_sha256(sys_acl_blob, sys_acl_hash);
1047 if (!NT_STATUS_IS_OK(status)) {
1048 TALLOC_FREE(frame);
1049 return status;
1052 if (DEBUGLEVEL >= 10) {
1053 DBG_DEBUG("storing xattr sd for file %s based on system ACL\n",
1054 fsp_str_dbg(fsp));
1055 NDR_PRINT_DEBUG(security_descriptor,
1056 discard_const_p(struct security_descriptor, psd));
1058 DBG_DEBUG("storing hash in xattr sd based on system ACL and:\n");
1059 NDR_PRINT_DEBUG(security_descriptor,
1060 discard_const_p(struct security_descriptor, pdesc_next));
1063 /* We store hashes of both the sys ACL blob and the NT
1064 * security desciptor mapped from that ACL so as to improve
1065 * our chances against some inadvertant change breaking the
1066 * hash used */
1067 status = create_sys_acl_blob(psd, &blob, XATTR_SD_HASH_TYPE_SHA256, hash,
1068 sys_acl_description, sys_acl_hash);
1069 if (!NT_STATUS_IS_OK(status)) {
1070 DBG_DEBUG("create_sys_acl_blob failed\n");
1071 TALLOC_FREE(frame);
1072 return status;
1075 status = store_acl_blob_fsp_fn(handle, fsp, &blob);
1077 TALLOC_FREE(frame);
1078 return status;
1081 static int acl_common_remove_object(vfs_handle_struct *handle,
1082 const struct smb_filename *smb_fname,
1083 bool is_directory)
1085 connection_struct *conn = handle->conn;
1086 struct file_id id;
1087 files_struct *fsp = NULL;
1088 int ret = 0;
1089 char *parent_dir = NULL;
1090 const char *final_component = NULL;
1091 struct smb_filename local_fname = {0};
1092 struct smb_filename parent_dir_fname = {0};
1093 int saved_errno = 0;
1094 struct smb_filename *saved_dir_fname = NULL;
1096 saved_dir_fname = vfs_GetWd(talloc_tos(),conn);
1097 if (saved_dir_fname == NULL) {
1098 saved_errno = errno;
1099 goto out;
1102 if (!parent_dirname(talloc_tos(), smb_fname->base_name,
1103 &parent_dir, &final_component)) {
1104 saved_errno = ENOMEM;
1105 goto out;
1108 DBG_DEBUG("removing %s %s/%s\n", is_directory ? "directory" : "file",
1109 parent_dir, final_component);
1111 parent_dir_fname = (struct smb_filename) { .base_name = parent_dir };
1113 /* cd into the parent dir to pin it. */
1114 ret = vfs_ChDir(conn, &parent_dir_fname);
1115 if (ret == -1) {
1116 saved_errno = errno;
1117 goto out;
1120 local_fname.base_name = discard_const_p(char, final_component);
1122 /* Must use lstat here. */
1123 ret = SMB_VFS_LSTAT(conn, &local_fname);
1124 if (ret == -1) {
1125 saved_errno = errno;
1126 goto out;
1129 /* Ensure we have this file open with DELETE access. */
1130 id = vfs_file_id_from_sbuf(conn, &local_fname.st);
1131 for (fsp = file_find_di_first(conn->sconn, id); fsp;
1132 fsp = file_find_di_next(fsp)) {
1133 if (fsp->access_mask & DELETE_ACCESS &&
1134 fsp->delete_on_close) {
1135 /* We did open this for delete,
1136 * allow the delete as root.
1138 break;
1142 if (!fsp) {
1143 DBG_DEBUG("%s %s/%s not an open file\n",
1144 is_directory ? "directory" : "file",
1145 parent_dir, final_component);
1146 saved_errno = EACCES;
1147 goto out;
1150 become_root();
1151 if (is_directory) {
1152 ret = SMB_VFS_NEXT_RMDIR(handle, &local_fname);
1153 } else {
1154 ret = SMB_VFS_NEXT_UNLINK(handle, &local_fname);
1156 unbecome_root();
1158 if (ret == -1) {
1159 saved_errno = errno;
1162 out:
1164 TALLOC_FREE(parent_dir);
1166 if (saved_dir_fname) {
1167 vfs_ChDir(conn, saved_dir_fname);
1168 TALLOC_FREE(saved_dir_fname);
1170 if (saved_errno) {
1171 errno = saved_errno;
1173 return ret;
1176 int rmdir_acl_common(struct vfs_handle_struct *handle,
1177 const struct smb_filename *smb_fname)
1179 int ret;
1181 /* Try the normal rmdir first. */
1182 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname);
1183 if (ret == 0) {
1184 return 0;
1186 if (errno == EACCES || errno == EPERM) {
1187 /* Failed due to access denied,
1188 see if we need to root override. */
1189 return acl_common_remove_object(handle,
1190 smb_fname,
1191 true);
1194 DBG_DEBUG("unlink of %s failed %s\n",
1195 smb_fname->base_name,
1196 strerror(errno));
1197 return -1;
1200 int unlink_acl_common(struct vfs_handle_struct *handle,
1201 const struct smb_filename *smb_fname)
1203 int ret;
1205 /* Try the normal unlink first. */
1206 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
1207 if (ret == 0) {
1208 return 0;
1210 if (errno == EACCES || errno == EPERM) {
1211 /* Failed due to access denied,
1212 see if we need to root override. */
1214 /* Don't do anything fancy for streams. */
1215 if (smb_fname->stream_name) {
1216 return -1;
1218 return acl_common_remove_object(handle,
1219 smb_fname,
1220 false);
1223 DBG_DEBUG("unlink of %s failed %s\n",
1224 smb_fname->base_name,
1225 strerror(errno));
1226 return -1;
1229 int chmod_acl_module_common(struct vfs_handle_struct *handle,
1230 const struct smb_filename *smb_fname,
1231 mode_t mode)
1233 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH) {
1234 /* Only allow this on POSIX pathnames. */
1235 return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
1237 return 0;
1240 int fchmod_acl_module_common(struct vfs_handle_struct *handle,
1241 struct files_struct *fsp, mode_t mode)
1243 if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
1244 /* Only allow this on POSIX opens. */
1245 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1247 return 0;