2 Unix SMB/CIFS implementation.
4 security descriptror 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"
26 return a blank security descriptor (no owners, dacl or sacl)
28 struct security_descriptor *security_descriptor_initialise(TALLOC_CTX *mem_ctx)
30 struct security_descriptor *sd;
32 sd = talloc(mem_ctx, struct security_descriptor);
37 sd->revision = SD_REVISION;
38 /* we mark as self relative, even though it isn't while it remains
39 a pointer in memory because this simplifies the ndr code later.
40 All SDs that we store/emit are in fact SELF_RELATIVE
42 sd->type = SEC_DESC_SELF_RELATIVE;
52 static struct security_acl *security_acl_dup(TALLOC_CTX *mem_ctx,
53 const struct security_acl *oacl)
55 struct security_acl *nacl;
58 nacl = talloc (mem_ctx, struct security_acl);
63 nacl->aces = (struct security_ace *)talloc_memdup (nacl, oacl->aces, sizeof(struct security_ace) * oacl->num_aces);
64 if ((nacl->aces == NULL) && (oacl->num_aces > 0)) {
68 /* remapping array in trustee dom_sid from old acl to new acl */
70 for (i = 0; i < oacl->num_aces; i++) {
71 nacl->aces[i].trustee.sub_auths =
72 (uint32_t *)talloc_memdup(nacl->aces, nacl->aces[i].trustee.sub_auths,
73 sizeof(uint32_t) * nacl->aces[i].trustee.num_auths);
75 if ((nacl->aces[i].trustee.sub_auths == NULL) && (nacl->aces[i].trustee.num_auths > 0)) {
80 nacl->revision = oacl->revision;
81 nacl->size = oacl->size;
82 nacl->num_aces = oacl->num_aces;
93 talloc and copy a security descriptor
95 struct security_descriptor *security_descriptor_copy(TALLOC_CTX *mem_ctx,
96 const struct security_descriptor *osd)
98 struct security_descriptor *nsd;
100 nsd = talloc_zero(mem_ctx, struct security_descriptor);
105 if (osd->owner_sid) {
106 nsd->owner_sid = dom_sid_dup(nsd, osd->owner_sid);
107 if (nsd->owner_sid == NULL) {
112 if (osd->group_sid) {
113 nsd->group_sid = dom_sid_dup(nsd, osd->group_sid);
114 if (nsd->group_sid == NULL) {
120 nsd->sacl = security_acl_dup(nsd, osd->sacl);
121 if (nsd->sacl == NULL) {
127 nsd->dacl = security_acl_dup(nsd, osd->dacl);
128 if (nsd->dacl == NULL) {
142 add an ACE to the DACL of a security_descriptor
144 NTSTATUS security_descriptor_dacl_add(struct security_descriptor *sd,
145 const struct security_ace *ace)
147 if (sd->dacl == NULL) {
148 sd->dacl = talloc(sd, struct security_acl);
149 if (sd->dacl == NULL) {
150 return NT_STATUS_NO_MEMORY;
152 sd->dacl->revision = SECURITY_ACL_REVISION_NT4;
154 sd->dacl->num_aces = 0;
155 sd->dacl->aces = NULL;
158 sd->dacl->aces = talloc_realloc(sd->dacl, sd->dacl->aces,
159 struct security_ace, sd->dacl->num_aces+1);
160 if (sd->dacl->aces == NULL) {
161 return NT_STATUS_NO_MEMORY;
164 sd->dacl->aces[sd->dacl->num_aces] = *ace;
165 sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths =
166 (uint32_t *)talloc_memdup(sd->dacl->aces,
167 sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths,
169 sd->dacl->aces[sd->dacl->num_aces].trustee.num_auths);
170 if (sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths == NULL) {
171 return NT_STATUS_NO_MEMORY;
174 switch (sd->dacl->aces[sd->dacl->num_aces].type) {
175 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
176 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
177 case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
178 case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
179 sd->dacl->revision = SECURITY_ACL_REVISION_ADS;
185 sd->dacl->num_aces++;
187 sd->type |= SEC_DESC_DACL_PRESENT;
194 delete the ACE corresponding to the given trustee in the DACL of a security_descriptor
196 NTSTATUS security_descriptor_dacl_del(struct security_descriptor *sd,
197 struct dom_sid *trustee)
202 if (sd->dacl == NULL) {
203 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
206 /* there can be multiple ace's for one trustee */
207 for (i=0;i<sd->dacl->num_aces;i++) {
208 if (dom_sid_equal(trustee, &sd->dacl->aces[i].trustee)) {
209 memmove(&sd->dacl->aces[i], &sd->dacl->aces[i+1],
210 sizeof(sd->dacl->aces[i]) * (sd->dacl->num_aces - (i+1)));
211 sd->dacl->num_aces--;
212 if (sd->dacl->num_aces == 0) {
213 sd->dacl->aces = NULL;
220 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
223 sd->dacl->revision = SECURITY_ACL_REVISION_NT4;
225 for (i=0;i<sd->dacl->num_aces;i++) {
226 switch (sd->dacl->aces[i].type) {
227 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
228 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
229 case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
230 case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
231 sd->dacl->revision = SECURITY_ACL_REVISION_ADS;
234 break; /* only for the switch statement */
243 compare two security ace structures
245 bool security_ace_equal(const struct security_ace *ace1,
246 const struct security_ace *ace2)
248 if (ace1 == ace2) return true;
249 if (!ace1 || !ace2) return false;
250 if (ace1->type != ace2->type) return false;
251 if (ace1->flags != ace2->flags) return false;
252 if (ace1->access_mask != ace2->access_mask) return false;
253 if (!dom_sid_equal(&ace1->trustee, &ace2->trustee)) return false;
260 compare two security acl structures
262 bool security_acl_equal(const struct security_acl *acl1,
263 const struct security_acl *acl2)
267 if (acl1 == acl2) return true;
268 if (!acl1 || !acl2) return false;
269 if (acl1->revision != acl2->revision) return false;
270 if (acl1->num_aces != acl2->num_aces) return false;
272 for (i=0;i<acl1->num_aces;i++) {
273 if (!security_ace_equal(&acl1->aces[i], &acl2->aces[i])) return false;
279 compare two security descriptors.
281 bool security_descriptor_equal(const struct security_descriptor *sd1,
282 const struct security_descriptor *sd2)
284 if (sd1 == sd2) return true;
285 if (!sd1 || !sd2) return false;
286 if (sd1->revision != sd2->revision) return false;
287 if (sd1->type != sd2->type) return false;
289 if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return false;
290 if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return false;
291 if (!security_acl_equal(sd1->sacl, sd2->sacl)) return false;
292 if (!security_acl_equal(sd1->dacl, sd2->dacl)) return false;
298 compare two security descriptors, but allow certain (missing) parts
299 to be masked out of the comparison
301 bool security_descriptor_mask_equal(const struct security_descriptor *sd1,
302 const struct security_descriptor *sd2,
305 if (sd1 == sd2) return true;
306 if (!sd1 || !sd2) return false;
307 if (sd1->revision != sd2->revision) return false;
308 if ((sd1->type & mask) != (sd2->type & mask)) return false;
310 if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return false;
311 if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return false;
312 if ((mask & SEC_DESC_DACL_PRESENT) && !security_acl_equal(sd1->dacl, sd2->dacl)) return false;
313 if ((mask & SEC_DESC_SACL_PRESENT) && !security_acl_equal(sd1->sacl, sd2->sacl)) return false;
320 create a security descriptor using string SIDs. This is used by the
321 torture code to allow the easy creation of complex ACLs
322 This is a varargs function. The list of DACL ACEs ends with a NULL sid.
324 Each ACE contains a set of 4 parameters:
325 SID, ACCESS_TYPE, MASK, FLAGS
327 a typical call would be:
329 sd = security_descriptor_create(mem_ctx,
332 SID_NT_AUTHENTICATED_USERS,
333 SEC_ACE_TYPE_ACCESS_ALLOWED,
335 SEC_ACE_FLAG_OBJECT_INHERIT,
337 that would create a sd with one DACL ACE
339 struct security_descriptor *security_descriptor_create(TALLOC_CTX *mem_ctx,
340 const char *owner_sid,
341 const char *group_sid,
345 struct security_descriptor *sd;
348 sd = security_descriptor_initialise(mem_ctx);
349 if (sd == NULL) return NULL;
352 sd->owner_sid = dom_sid_parse_talloc(sd, owner_sid);
353 if (sd->owner_sid == NULL) {
359 sd->group_sid = dom_sid_parse_talloc(sd, group_sid);
360 if (sd->group_sid == NULL) {
366 va_start(ap, group_sid);
367 while ((sidstr = va_arg(ap, const char *))) {
369 struct security_ace *ace = talloc(sd, struct security_ace);
377 ace->type = va_arg(ap, unsigned int);
378 ace->access_mask = va_arg(ap, unsigned int);
379 ace->flags = va_arg(ap, unsigned int);
380 sid = dom_sid_parse_talloc(ace, sidstr);
387 status = security_descriptor_dacl_add(sd, ace);
388 /* TODO: check: would talloc_free(ace) here be correct? */
389 if (!NT_STATUS_IS_OK(status)) {