2 Unix SMB/CIFS implementation.
4 security descriptor utility functions
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
23 #include "libcli/security/security.h"
24 #include "librpc/ndr/libndr.h"
27 return a blank security descriptor (no owners, dacl or sacl)
29 struct security_descriptor
*security_descriptor_initialise(TALLOC_CTX
*mem_ctx
)
31 struct security_descriptor
*sd
;
33 sd
= talloc(mem_ctx
, struct security_descriptor
);
37 *sd
= (struct security_descriptor
){
38 .revision
= SD_REVISION
,
41 * we mark as self relative, even though it isn't
42 * while it remains a pointer in memory because this
43 * simplifies the ndr code later. All SDs that we
44 * store/emit are in fact SELF_RELATIVE
46 .type
= SEC_DESC_SELF_RELATIVE
,
52 struct security_acl
*security_acl_dup(TALLOC_CTX
*mem_ctx
,
53 const struct security_acl
*oacl
)
55 struct security_acl
*nacl
;
61 if (oacl
->aces
== NULL
&& oacl
->num_aces
> 0) {
65 nacl
= talloc (mem_ctx
, struct security_acl
);
70 *nacl
= (struct security_acl
) {
71 .revision
= oacl
->revision
,
73 .num_aces
= oacl
->num_aces
,
75 if (nacl
->num_aces
== 0) {
79 nacl
->aces
= (struct security_ace
*)talloc_memdup (nacl
, oacl
->aces
, sizeof(struct security_ace
) * oacl
->num_aces
);
80 if (nacl
->aces
== NULL
) {
92 struct security_acl
*security_acl_concatenate(TALLOC_CTX
*mem_ctx
,
93 const struct security_acl
*acl1
,
94 const struct security_acl
*acl2
)
96 struct security_acl
*nacl
;
103 nacl
= security_acl_dup(mem_ctx
, acl2
);
108 nacl
= security_acl_dup(mem_ctx
, acl1
);
112 nacl
= talloc (mem_ctx
, struct security_acl
);
117 nacl
->revision
= acl1
->revision
;
118 nacl
->size
= acl1
->size
+ acl2
->size
;
119 nacl
->num_aces
= acl1
->num_aces
+ acl2
->num_aces
;
121 if (nacl
->num_aces
== 0)
124 nacl
->aces
= (struct security_ace
*)talloc_array (mem_ctx
, struct security_ace
, acl1
->num_aces
+acl2
->num_aces
);
125 if ((nacl
->aces
== NULL
) && (nacl
->num_aces
> 0)) {
129 for (i
= 0; i
< acl1
->num_aces
; i
++)
130 nacl
->aces
[i
] = acl1
->aces
[i
];
131 for (i
= 0; i
< acl2
->num_aces
; i
++)
132 nacl
->aces
[i
+ acl1
->num_aces
] = acl2
->aces
[i
];
143 talloc and copy a security descriptor
145 struct security_descriptor
*security_descriptor_copy(TALLOC_CTX
*mem_ctx
,
146 const struct security_descriptor
*osd
)
148 struct security_descriptor
*nsd
;
150 nsd
= talloc_zero(mem_ctx
, struct security_descriptor
);
155 if (osd
->owner_sid
) {
156 nsd
->owner_sid
= dom_sid_dup(nsd
, osd
->owner_sid
);
157 if (nsd
->owner_sid
== NULL
) {
162 if (osd
->group_sid
) {
163 nsd
->group_sid
= dom_sid_dup(nsd
, osd
->group_sid
);
164 if (nsd
->group_sid
== NULL
) {
170 nsd
->sacl
= security_acl_dup(nsd
, osd
->sacl
);
171 if (nsd
->sacl
== NULL
) {
177 nsd
->dacl
= security_acl_dup(nsd
, osd
->dacl
);
178 if (nsd
->dacl
== NULL
) {
183 nsd
->revision
= osd
->revision
;
184 nsd
->type
= osd
->type
;
194 NTSTATUS
security_descriptor_for_client(TALLOC_CTX
*mem_ctx
,
195 const struct security_descriptor
*ssd
,
197 uint32_t access_granted
,
198 struct security_descriptor
**_csd
)
200 struct security_descriptor
*csd
= NULL
;
201 uint32_t access_required
= 0;
205 if (sec_info
& (SECINFO_OWNER
|SECINFO_GROUP
)) {
206 access_required
|= SEC_STD_READ_CONTROL
;
208 if (sec_info
& SECINFO_DACL
) {
209 access_required
|= SEC_STD_READ_CONTROL
;
211 if (sec_info
& SECINFO_SACL
) {
212 access_required
|= SEC_FLAG_SYSTEM_SECURITY
;
215 if (access_required
& (~access_granted
)) {
216 return NT_STATUS_ACCESS_DENIED
;
222 csd
= security_descriptor_copy(mem_ctx
, ssd
);
224 return NT_STATUS_NO_MEMORY
;
228 * ... and remove everything not wanted
231 if (!(sec_info
& SECINFO_OWNER
)) {
232 TALLOC_FREE(csd
->owner_sid
);
233 csd
->type
&= ~SEC_DESC_OWNER_DEFAULTED
;
235 if (!(sec_info
& SECINFO_GROUP
)) {
236 TALLOC_FREE(csd
->group_sid
);
237 csd
->type
&= ~SEC_DESC_GROUP_DEFAULTED
;
239 if (!(sec_info
& SECINFO_DACL
)) {
240 TALLOC_FREE(csd
->dacl
);
242 SEC_DESC_DACL_PRESENT
|
243 SEC_DESC_DACL_DEFAULTED
|
244 SEC_DESC_DACL_AUTO_INHERIT_REQ
|
245 SEC_DESC_DACL_AUTO_INHERITED
|
246 SEC_DESC_DACL_PROTECTED
|
247 SEC_DESC_DACL_TRUSTED
);
249 if (!(sec_info
& SECINFO_SACL
)) {
250 TALLOC_FREE(csd
->sacl
);
252 SEC_DESC_SACL_PRESENT
|
253 SEC_DESC_SACL_DEFAULTED
|
254 SEC_DESC_SACL_AUTO_INHERIT_REQ
|
255 SEC_DESC_SACL_AUTO_INHERITED
|
256 SEC_DESC_SACL_PROTECTED
|
257 SEC_DESC_SERVER_SECURITY
);
265 add an ACE to an ACL of a security_descriptor
268 static NTSTATUS
security_descriptor_acl_add(struct security_descriptor
*sd
,
270 const struct security_ace
*ace
,
273 struct security_acl
*acl
= NULL
;
283 acl
= talloc(sd
, struct security_acl
);
285 return NT_STATUS_NO_MEMORY
;
287 acl
->revision
= SECURITY_ACL_REVISION_NT4
;
294 idx
= (acl
->num_aces
+ 1) + _idx
;
300 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED
;
302 if (idx
> acl
->num_aces
) {
303 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED
;
306 acl
->aces
= talloc_realloc(acl
, acl
->aces
,
307 struct security_ace
, acl
->num_aces
+1);
308 if (acl
->aces
== NULL
) {
309 return NT_STATUS_NO_MEMORY
;
312 ARRAY_INSERT_ELEMENT(acl
->aces
, acl
->num_aces
, *ace
, idx
);
315 if (sec_ace_object(acl
->aces
[idx
].type
)) {
316 acl
->revision
= SECURITY_ACL_REVISION_ADS
;
321 sd
->type
|= SEC_DESC_SACL_PRESENT
;
324 sd
->type
|= SEC_DESC_DACL_PRESENT
;
331 add an ACE to the SACL of a security_descriptor
334 NTSTATUS
security_descriptor_sacl_add(struct security_descriptor
*sd
,
335 const struct security_ace
*ace
)
337 return security_descriptor_acl_add(sd
, true, ace
, -1);
341 insert an ACE at a given index to the SACL of a security_descriptor
343 idx can be negative, which means it's related to the new size from the
344 end, so -1 means the ace is appended at the end.
347 NTSTATUS
security_descriptor_sacl_insert(struct security_descriptor
*sd
,
348 const struct security_ace
*ace
,
351 return security_descriptor_acl_add(sd
, true, ace
, idx
);
355 add an ACE to the DACL of a security_descriptor
358 NTSTATUS
security_descriptor_dacl_add(struct security_descriptor
*sd
,
359 const struct security_ace
*ace
)
361 return security_descriptor_acl_add(sd
, false, ace
, -1);
365 insert an ACE at a given index to the DACL of a security_descriptor
367 idx can be negative, which means it's related to the new size from the
368 end, so -1 means the ace is appended at the end.
371 NTSTATUS
security_descriptor_dacl_insert(struct security_descriptor
*sd
,
372 const struct security_ace
*ace
,
375 return security_descriptor_acl_add(sd
, false, ace
, idx
);
379 delete the ACE corresponding to the given trustee in an ACL of a
383 static NTSTATUS
security_descriptor_acl_del(struct security_descriptor
*sd
,
385 const struct dom_sid
*trustee
)
389 struct security_acl
*acl
= NULL
;
398 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
401 /* there can be multiple ace's for one trustee */
405 while (i
<acl
->num_aces
) {
406 if (dom_sid_equal(trustee
, &acl
->aces
[i
].trustee
)) {
407 ARRAY_DEL_ELEMENT(acl
->aces
, i
, acl
->num_aces
);
409 if (acl
->num_aces
== 0) {
419 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
422 acl
->revision
= SECURITY_ACL_REVISION_NT4
;
424 for (i
=0;i
<acl
->num_aces
;i
++) {
425 if (sec_ace_object(acl
->aces
[i
].type
)) {
426 acl
->revision
= SECURITY_ACL_REVISION_ADS
;
435 delete the ACE corresponding to the given trustee in the DACL of a
439 NTSTATUS
security_descriptor_dacl_del(struct security_descriptor
*sd
,
440 const struct dom_sid
*trustee
)
442 return security_descriptor_acl_del(sd
, false, trustee
);
446 delete the ACE corresponding to the given trustee in the SACL of a
450 NTSTATUS
security_descriptor_sacl_del(struct security_descriptor
*sd
,
451 const struct dom_sid
*trustee
)
453 return security_descriptor_acl_del(sd
, true, trustee
);
457 delete the given ACE in the SACL or DACL of a security_descriptor
459 static NTSTATUS
security_descriptor_acl_del_ace(struct security_descriptor
*sd
,
461 const struct security_ace
*ace
)
465 struct security_acl
*acl
= NULL
;
474 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
477 for (i
=0;i
<acl
->num_aces
;i
++) {
478 if (security_ace_equal(ace
, &acl
->aces
[i
])) {
479 ARRAY_DEL_ELEMENT(acl
->aces
, i
, acl
->num_aces
);
481 if (acl
->num_aces
== 0) {
490 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
493 acl
->revision
= SECURITY_ACL_REVISION_NT4
;
495 for (i
=0;i
<acl
->num_aces
;i
++) {
496 if (sec_ace_object(acl
->aces
[i
].type
)) {
497 acl
->revision
= SECURITY_ACL_REVISION_ADS
;
505 NTSTATUS
security_descriptor_dacl_del_ace(struct security_descriptor
*sd
,
506 const struct security_ace
*ace
)
508 return security_descriptor_acl_del_ace(sd
, false, ace
);
511 NTSTATUS
security_descriptor_sacl_del_ace(struct security_descriptor
*sd
,
512 const struct security_ace
*ace
)
514 return security_descriptor_acl_del_ace(sd
, true, ace
);
517 static bool security_ace_object_equal(const struct security_ace_object
*object1
,
518 const struct security_ace_object
*object2
)
520 if (object1
== object2
) {
523 if ((object1
== NULL
) || (object2
== NULL
)) {
526 if (object1
->flags
!= object2
->flags
) {
529 if (object1
->flags
& SEC_ACE_OBJECT_TYPE_PRESENT
530 && !GUID_equal(&object1
->type
.type
, &object2
->type
.type
)) {
533 if (object1
->flags
& SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT
534 && !GUID_equal(&object1
->inherited_type
.inherited_type
,
535 &object2
->inherited_type
.inherited_type
)) {
543 static bool security_ace_claim_equal(const struct CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1
*claim1
,
544 const struct CLAIM_SECURITY_ATTRIBUTE_RELATIVE_V1
*claim2
)
548 if (claim1
== claim2
) {
551 if (claim1
== NULL
|| claim2
== NULL
) {
554 if (claim1
->name
!= NULL
&& claim2
->name
!= NULL
) {
555 if (strcasecmp_m(claim1
->name
, claim2
->name
) != 0) {
558 } else if (claim1
->name
!= NULL
|| claim2
->name
!= NULL
) {
561 if (claim1
->value_type
!= claim2
->value_type
) {
564 if (claim1
->flags
!= claim2
->flags
) {
567 if (claim1
->value_count
!= claim2
->value_count
) {
570 for (i
= 0; i
< claim1
->value_count
; ++i
) {
571 const union claim_values
*values1
= claim1
->values
;
572 const union claim_values
*values2
= claim2
->values
;
574 switch (claim1
->value_type
) {
575 case CLAIM_SECURITY_ATTRIBUTE_TYPE_INT64
:
576 if (values1
[i
].int_value
!= NULL
&& values2
[i
].int_value
!= NULL
) {
577 if (*values1
[i
].int_value
!= *values2
[i
].int_value
) {
580 } else if (values1
[i
].int_value
!= NULL
|| values2
[i
].int_value
!= NULL
) {
584 case CLAIM_SECURITY_ATTRIBUTE_TYPE_UINT64
:
585 case CLAIM_SECURITY_ATTRIBUTE_TYPE_BOOLEAN
:
586 if (values1
[i
].uint_value
!= NULL
&& values2
[i
].uint_value
!= NULL
) {
587 if (*values1
[i
].uint_value
!= *values2
[i
].uint_value
) {
590 } else if (values1
[i
].uint_value
!= NULL
|| values2
[i
].uint_value
!= NULL
) {
594 case CLAIM_SECURITY_ATTRIBUTE_TYPE_STRING
:
595 if (values1
[i
].string_value
!= NULL
&& values2
[i
].string_value
!= NULL
) {
596 if (strcasecmp_m(values1
[i
].string_value
, values2
[i
].string_value
) != 0) {
599 } else if (values1
[i
].string_value
!= NULL
|| values2
[i
].string_value
!= NULL
) {
603 case CLAIM_SECURITY_ATTRIBUTE_TYPE_SID
:
604 if (values1
[i
].sid_value
!= NULL
&& values2
[i
].sid_value
!= NULL
) {
605 if (data_blob_cmp(values1
[i
].sid_value
, values2
[i
].sid_value
) != 0) {
608 } else if (values1
[i
].sid_value
!= NULL
|| values2
[i
].sid_value
!= NULL
) {
612 case CLAIM_SECURITY_ATTRIBUTE_TYPE_OCTET_STRING
:
613 if (values1
[i
].octet_value
!= NULL
&& values2
[i
].octet_value
!= NULL
) {
614 if (data_blob_cmp(values1
[i
].octet_value
, values2
[i
].octet_value
) != 0) {
617 } else if (values1
[i
].octet_value
!= NULL
|| values2
[i
].octet_value
!= NULL
) {
630 compare two security ace structures
632 bool security_ace_equal(const struct security_ace
*ace1
,
633 const struct security_ace
*ace2
)
638 if ((ace1
== NULL
) || (ace2
== NULL
)) {
641 if (ace1
->type
!= ace2
->type
) {
644 if (ace1
->flags
!= ace2
->flags
) {
647 if (ace1
->access_mask
!= ace2
->access_mask
) {
650 if (sec_ace_object(ace1
->type
) &&
651 !security_ace_object_equal(&ace1
->object
.object
,
652 &ace2
->object
.object
))
656 if (!dom_sid_equal(&ace1
->trustee
, &ace2
->trustee
)) {
660 if (sec_ace_callback(ace1
->type
)) {
661 if (data_blob_cmp(&ace1
->coda
.conditions
, &ace2
->coda
.conditions
) != 0) {
664 } else if (sec_ace_resource(ace1
->type
)) {
665 if (!security_ace_claim_equal(&ace1
->coda
.claim
, &ace2
->coda
.claim
)) {
670 * Don’t require ace1->coda.ignored to match ace2->coda.ignored.
679 compare two security acl structures
681 bool security_acl_equal(const struct security_acl
*acl1
,
682 const struct security_acl
*acl2
)
686 if (acl1
== acl2
) return true;
687 if (!acl1
|| !acl2
) return false;
688 if (acl1
->revision
!= acl2
->revision
) return false;
689 if (acl1
->num_aces
!= acl2
->num_aces
) return false;
691 for (i
=0;i
<acl1
->num_aces
;i
++) {
692 if (!security_ace_equal(&acl1
->aces
[i
], &acl2
->aces
[i
])) return false;
698 compare two security descriptors.
700 bool security_descriptor_equal(const struct security_descriptor
*sd1
,
701 const struct security_descriptor
*sd2
)
703 if (sd1
== sd2
) return true;
704 if (!sd1
|| !sd2
) return false;
705 if (sd1
->revision
!= sd2
->revision
) return false;
706 if (sd1
->type
!= sd2
->type
) return false;
708 if (!dom_sid_equal(sd1
->owner_sid
, sd2
->owner_sid
)) return false;
709 if (!dom_sid_equal(sd1
->group_sid
, sd2
->group_sid
)) return false;
710 if (!security_acl_equal(sd1
->sacl
, sd2
->sacl
)) return false;
711 if (!security_acl_equal(sd1
->dacl
, sd2
->dacl
)) return false;
717 compare two security descriptors, but allow certain (missing) parts
718 to be masked out of the comparison
720 bool security_descriptor_mask_equal(const struct security_descriptor
*sd1
,
721 const struct security_descriptor
*sd2
,
724 if (sd1
== sd2
) return true;
725 if (!sd1
|| !sd2
) return false;
726 if (sd1
->revision
!= sd2
->revision
) return false;
727 if ((sd1
->type
& mask
) != (sd2
->type
& mask
)) return false;
729 if (!dom_sid_equal(sd1
->owner_sid
, sd2
->owner_sid
)) return false;
730 if (!dom_sid_equal(sd1
->group_sid
, sd2
->group_sid
)) return false;
731 if ((mask
& SEC_DESC_DACL_PRESENT
) && !security_acl_equal(sd1
->dacl
, sd2
->dacl
)) return false;
732 if ((mask
& SEC_DESC_SACL_PRESENT
) && !security_acl_equal(sd1
->sacl
, sd2
->sacl
)) return false;
738 static struct security_descriptor
*security_descriptor_appendv(struct security_descriptor
*sd
,
739 bool add_ace_to_sacl
,
744 while ((sidstr
= va_arg(ap
, const char *))) {
746 struct security_ace
*ace
= talloc_zero(sd
, struct security_ace
);
753 ace
->type
= va_arg(ap
, unsigned int);
754 ace
->access_mask
= va_arg(ap
, unsigned int);
755 ace
->flags
= va_arg(ap
, unsigned int);
756 sid
= dom_sid_parse_talloc(ace
, sidstr
);
762 if (add_ace_to_sacl
) {
763 status
= security_descriptor_sacl_add(sd
, ace
);
765 status
= security_descriptor_dacl_add(sd
, ace
);
767 /* TODO: check: would talloc_free(ace) here be correct? */
768 if (!NT_STATUS_IS_OK(status
)) {
777 static struct security_descriptor
*security_descriptor_createv(TALLOC_CTX
*mem_ctx
,
779 const char *owner_sid
,
780 const char *group_sid
,
781 bool add_ace_to_sacl
,
784 struct security_descriptor
*sd
;
786 sd
= security_descriptor_initialise(mem_ctx
);
794 sd
->owner_sid
= dom_sid_parse_talloc(sd
, owner_sid
);
795 if (sd
->owner_sid
== NULL
) {
801 sd
->group_sid
= dom_sid_parse_talloc(sd
, group_sid
);
802 if (sd
->group_sid
== NULL
) {
808 return security_descriptor_appendv(sd
, add_ace_to_sacl
, ap
);
812 create a security descriptor using string SIDs. This is used by the
813 torture code to allow the easy creation of complex ACLs
814 This is a varargs function. The list of DACL ACEs ends with a NULL sid.
816 Each ACE contains a set of 4 parameters:
817 SID, ACCESS_TYPE, MASK, FLAGS
819 a typical call would be:
821 sd = security_descriptor_dacl_create(mem_ctx,
825 SID_NT_AUTHENTICATED_USERS,
826 SEC_ACE_TYPE_ACCESS_ALLOWED,
828 SEC_ACE_FLAG_OBJECT_INHERIT,
830 that would create a sd with one DACL ACE
833 struct security_descriptor
*security_descriptor_dacl_create(TALLOC_CTX
*mem_ctx
,
835 const char *owner_sid
,
836 const char *group_sid
,
839 struct security_descriptor
*sd
= NULL
;
841 va_start(ap
, group_sid
);
842 sd
= security_descriptor_createv(mem_ctx
, sd_type
, owner_sid
,
843 group_sid
, false, ap
);
849 struct security_descriptor
*security_descriptor_sacl_create(TALLOC_CTX
*mem_ctx
,
851 const char *owner_sid
,
852 const char *group_sid
,
855 struct security_descriptor
*sd
= NULL
;
857 va_start(ap
, group_sid
);
858 sd
= security_descriptor_createv(mem_ctx
, sd_type
, owner_sid
,
859 group_sid
, true, ap
);
865 struct security_ace
*security_ace_create(TALLOC_CTX
*mem_ctx
,
867 enum security_ace_type type
,
868 uint32_t access_mask
,
872 struct security_ace
*ace
;
875 ace
= talloc_zero(mem_ctx
, struct security_ace
);
880 ok
= dom_sid_parse(sid_str
, &ace
->trustee
);
886 ace
->access_mask
= access_mask
;
892 /*******************************************************************
893 Check for MS NFS ACEs in a sd
894 *******************************************************************/
895 bool security_descriptor_with_ms_nfs(const struct security_descriptor
*psd
)
899 if (psd
->dacl
== NULL
) {
903 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
904 if (dom_sid_compare_domain(
905 &global_sid_Unix_NFS
,
906 &psd
->dacl
->aces
[i
].trustee
) == 0) {