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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/security/security.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);
38 sd->revision = SD_REVISION;
39 /* we mark as self relative, even though it isn't while it remains
40 a pointer in memory because this simplifies the ndr code later.
41 All SDs that we store/emit are in fact SELF_RELATIVE
43 sd->type = SEC_DESC_SELF_RELATIVE;
53 static struct security_acl *security_acl_dup(TALLOC_CTX *mem_ctx,
54 const struct security_acl *oacl)
56 struct security_acl *nacl;
59 nacl = talloc (mem_ctx, struct security_acl);
64 nacl->aces = talloc_memdup (nacl, oacl->aces, sizeof(struct security_ace) * oacl->num_aces);
65 if ((nacl->aces == NULL) && (oacl->num_aces > 0)) {
69 /* remapping array in trustee dom_sid from old acl to new acl */
71 for (i = 0; i < oacl->num_aces; i++) {
72 nacl->aces[i].trustee.sub_auths =
73 talloc_memdup(nacl->aces, nacl->aces[i].trustee.sub_auths,
74 sizeof(uint32_t) * nacl->aces[i].trustee.num_auths);
76 if ((nacl->aces[i].trustee.sub_auths == NULL) && (nacl->aces[i].trustee.num_auths > 0)) {
81 nacl->revision = oacl->revision;
82 nacl->size = oacl->size;
83 nacl->num_aces = oacl->num_aces;
94 talloc and copy a security descriptor
96 struct security_descriptor *security_descriptor_copy(TALLOC_CTX *mem_ctx,
97 const struct security_descriptor *osd)
99 struct security_descriptor *nsd;
101 nsd = talloc_zero(mem_ctx, struct security_descriptor);
106 if (osd->owner_sid) {
107 nsd->owner_sid = dom_sid_dup(nsd, osd->owner_sid);
108 if (nsd->owner_sid == NULL) {
113 if (osd->group_sid) {
114 nsd->group_sid = dom_sid_dup(nsd, osd->group_sid);
115 if (nsd->group_sid == NULL) {
121 nsd->sacl = security_acl_dup(nsd, osd->sacl);
122 if (nsd->sacl == NULL) {
128 nsd->dacl = security_acl_dup(nsd, osd->dacl);
129 if (nsd->dacl == NULL) {
143 add an ACE to the DACL of a security_descriptor
145 NTSTATUS security_descriptor_dacl_add(struct security_descriptor *sd,
146 const struct security_ace *ace)
148 if (sd->dacl == NULL) {
149 sd->dacl = talloc(sd, struct security_acl);
150 if (sd->dacl == NULL) {
151 return NT_STATUS_NO_MEMORY;
153 sd->dacl->revision = NT4_ACL_REVISION;
155 sd->dacl->num_aces = 0;
156 sd->dacl->aces = NULL;
159 sd->dacl->aces = talloc_realloc(sd->dacl, sd->dacl->aces,
160 struct security_ace, sd->dacl->num_aces+1);
161 if (sd->dacl->aces == NULL) {
162 return NT_STATUS_NO_MEMORY;
165 sd->dacl->aces[sd->dacl->num_aces] = *ace;
166 sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths =
167 talloc_memdup(sd->dacl->aces,
168 sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths,
170 sd->dacl->aces[sd->dacl->num_aces].trustee.num_auths);
171 if (sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths == NULL) {
172 return NT_STATUS_NO_MEMORY;
175 sd->dacl->num_aces++;
177 sd->type |= SEC_DESC_DACL_PRESENT;
184 delete the ACE corresponding to the given trustee in the DACL of a security_descriptor
186 NTSTATUS security_descriptor_dacl_del(struct security_descriptor *sd,
187 struct dom_sid *trustee)
191 if (sd->dacl == NULL) {
192 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
195 for (i=0;i<sd->dacl->num_aces;i++) {
196 if (dom_sid_equal(trustee, &sd->dacl->aces[i].trustee)) {
197 memmove(&sd->dacl->aces[i], &sd->dacl->aces[i+1],
198 sizeof(sd->dacl->aces[i]) * (sd->dacl->num_aces - (i+1)));
199 sd->dacl->num_aces--;
200 if (sd->dacl->num_aces == 0) {
201 sd->dacl->aces = NULL;
206 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
211 compare two security ace structures
213 BOOL security_ace_equal(const struct security_ace *ace1,
214 const struct security_ace *ace2)
216 if (ace1 == ace2) return True;
217 if (!ace1 || !ace2) return False;
218 if (ace1->type != ace2->type) return False;
219 if (ace1->flags != ace2->flags) return False;
220 if (ace1->access_mask != ace2->access_mask) return False;
221 if (!dom_sid_equal(&ace1->trustee, &ace2->trustee)) return False;
228 compare two security acl structures
230 BOOL security_acl_equal(const struct security_acl *acl1,
231 const struct security_acl *acl2)
235 if (acl1 == acl2) return True;
236 if (!acl1 || !acl2) return False;
237 if (acl1->revision != acl2->revision) return False;
238 if (acl1->num_aces != acl2->num_aces) return False;
240 for (i=0;i<acl1->num_aces;i++) {
241 if (!security_ace_equal(&acl1->aces[i], &acl2->aces[i])) return False;
247 compare two security descriptors.
249 BOOL security_descriptor_equal(const struct security_descriptor *sd1,
250 const struct security_descriptor *sd2)
252 if (sd1 == sd2) return True;
253 if (!sd1 || !sd2) return False;
254 if (sd1->revision != sd2->revision) return False;
255 if (sd1->type != sd2->type) return False;
257 if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return False;
258 if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return False;
259 if (!security_acl_equal(sd1->sacl, sd2->sacl)) return False;
260 if (!security_acl_equal(sd1->dacl, sd2->dacl)) return False;
266 compare two security descriptors, but allow certain (missing) parts
267 to be masked out of the comparison
269 BOOL security_descriptor_mask_equal(const struct security_descriptor *sd1,
270 const struct security_descriptor *sd2,
273 if (sd1 == sd2) return True;
274 if (!sd1 || !sd2) return False;
275 if (sd1->revision != sd2->revision) return False;
276 if ((sd1->type & mask) != (sd2->type & mask)) return False;
278 if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return False;
279 if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return False;
280 if ((mask & SEC_DESC_DACL_PRESENT) && !security_acl_equal(sd1->dacl, sd2->dacl)) return False;
281 if ((mask & SEC_DESC_SACL_PRESENT) && !security_acl_equal(sd1->sacl, sd2->sacl)) return False;
288 create a security descriptor using string SIDs. This is used by the
289 torture code to allow the easy creation of complex ACLs
290 This is a varargs function. The list of DACL ACEs ends with a NULL sid.
292 Each ACE contains a set of 4 parameters:
293 SID, ACCESS_TYPE, MASK, FLAGS
295 a typical call would be:
297 sd = security_descriptor_create(mem_ctx,
300 SID_AUTHENTICATED_USERS,
301 SEC_ACE_TYPE_ACCESS_ALLOWED,
303 SEC_ACE_FLAG_OBJECT_INHERIT,
305 that would create a sd with one DACL ACE
307 struct security_descriptor *security_descriptor_create(TALLOC_CTX *mem_ctx,
308 const char *owner_sid,
309 const char *group_sid,
313 struct security_descriptor *sd;
316 sd = security_descriptor_initialise(mem_ctx);
317 if (sd == NULL) return NULL;
320 sd->owner_sid = dom_sid_parse_talloc(mem_ctx, owner_sid);
321 if (sd->owner_sid == NULL) {
327 sd->group_sid = dom_sid_parse_talloc(mem_ctx, group_sid);
328 if (sd->group_sid == NULL) {
334 va_start(ap, group_sid);
335 while ((sidstr = va_arg(ap, const char *))) {
337 struct security_ace *ace = talloc(sd, struct security_ace);
345 ace->type = va_arg(ap, unsigned int);
346 ace->access_mask = va_arg(ap, unsigned int);
347 ace->flags = va_arg(ap, unsigned int);
348 sid = dom_sid_parse_talloc(ace, sidstr);
355 status = security_descriptor_dacl_add(sd, ace);
356 if (!NT_STATUS_IS_OK(status)) {