1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/*
Unix SMB/CIFS implementation.
security descriptror utility functions
Copyright (C) Andrew Tridgell 2004
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
/*
return a blank security descriptor (no owners, dacl or sacl)
*/
struct security_descriptor *security_descriptor_initialise(TALLOC_CTX *mem_ctx)
{
struct security_descriptor *sd;
sd = talloc(mem_ctx, struct security_descriptor);
if (!sd) {
return NULL;
}
sd->revision = SD_REVISION;
/* we mark as self relative, even though it isn't while it remains
a pointer in memory because this simplifies the ndr code later.
All SDs that we store/emit are in fact SELF_RELATIVE
*/
sd->type = SEC_DESC_SELF_RELATIVE;
sd->owner_sid = NULL;
sd->group_sid = NULL;
sd->sacl = NULL;
sd->dacl = NULL;
return sd;
}
static struct security_acl *security_acl_dup(TALLOC_CTX *mem_ctx,
const struct security_acl *oacl)
{
struct security_acl *nacl;
int i;
nacl = talloc (mem_ctx, struct security_acl);
if (nacl == NULL) {
return NULL;
}
nacl->aces = talloc_memdup (nacl, oacl->aces, sizeof(struct security_ace) * oacl->num_aces);
if ((nacl->aces == NULL) && (oacl->num_aces > 0)) {
goto failed;
}
/* remapping array in trustee dom_sid from old acl to new acl */
for (i = 0; i < oacl->num_aces; i++) {
nacl->aces[i].trustee.sub_auths =
talloc_memdup(nacl->aces, nacl->aces[i].trustee.sub_auths,
sizeof(uint32_t) * nacl->aces[i].trustee.num_auths);
if ((nacl->aces[i].trustee.sub_auths == NULL) && (nacl->aces[i].trustee.num_auths > 0)) {
goto failed;
}
}
nacl->revision = oacl->revision;
nacl->size = oacl->size;
nacl->num_aces = oacl->num_aces;
return nacl;
failed:
talloc_free (nacl);
return NULL;
}
/*
talloc and copy a security descriptor
*/
struct security_descriptor *security_descriptor_copy(TALLOC_CTX *mem_ctx,
const struct security_descriptor *osd)
{
struct security_descriptor *nsd;
nsd = talloc_zero(mem_ctx, struct security_descriptor);
if (!nsd) {
return NULL;
}
if (osd->owner_sid) {
nsd->owner_sid = dom_sid_dup(nsd, osd->owner_sid);
if (nsd->owner_sid == NULL) {
goto failed;
}
}
if (osd->group_sid) {
nsd->group_sid = dom_sid_dup(nsd, osd->group_sid);
if (nsd->group_sid == NULL) {
goto failed;
}
}
if (osd->sacl) {
nsd->sacl = security_acl_dup(nsd, osd->sacl);
if (nsd->sacl == NULL) {
goto failed;
}
}
if (osd->dacl) {
nsd->dacl = security_acl_dup(nsd, osd->dacl);
if (nsd->dacl == NULL) {
goto failed;
}
}
return nsd;
failed:
talloc_free(nsd);
return NULL;
}
/*
add an ACE to the DACL of a security_descriptor
*/
NTSTATUS security_descriptor_dacl_add(struct security_descriptor *sd,
const struct security_ace *ace)
{
if (sd->dacl == NULL) {
sd->dacl = talloc(sd, struct security_acl);
if (sd->dacl == NULL) {
return NT_STATUS_NO_MEMORY;
}
sd->dacl->revision = NT4_ACL_REVISION;
sd->dacl->size = 0;
sd->dacl->num_aces = 0;
sd->dacl->aces = NULL;
}
sd->dacl->aces = talloc_realloc(sd->dacl, sd->dacl->aces,
struct security_ace, sd->dacl->num_aces+1);
if (sd->dacl->aces == NULL) {
return NT_STATUS_NO_MEMORY;
}
sd->dacl->aces[sd->dacl->num_aces] = *ace;
sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths =
talloc_memdup(sd->dacl->aces,
sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths,
sizeof(uint32_t) *
sd->dacl->aces[sd->dacl->num_aces].trustee.num_auths);
if (sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths == NULL) {
return NT_STATUS_NO_MEMORY;
}
sd->dacl->num_aces++;
sd->type |= SEC_DESC_DACL_PRESENT;
return NT_STATUS_OK;
}
/*
delete the ACE corresponding to the given trustee in the DACL of a security_descriptor
*/
NTSTATUS security_descriptor_dacl_del(struct security_descriptor *sd,
struct dom_sid *trustee)
{
int i;
if (sd->dacl == NULL) {
return NT_STATUS_OBJECT_NAME_NOT_FOUND;
}
for (i=0;i<sd->dacl->num_aces;i++) {
if (dom_sid_equal(trustee, &sd->dacl->aces[i].trustee)) {
memmove(&sd->dacl->aces[i], &sd->dacl->aces[i+1],
sizeof(sd->dacl->aces[i]) * (sd->dacl->num_aces - (i+1)));
sd->dacl->num_aces--;
if (sd->dacl->num_aces == 0) {
sd->dacl->aces = NULL;
}
return NT_STATUS_OK;
}
}
return NT_STATUS_OBJECT_NAME_NOT_FOUND;
}
/*
compare two security ace structures
*/
BOOL security_ace_equal(const struct security_ace *ace1,
const struct security_ace *ace2)
{
if (ace1 == ace2) return True;
if (!ace1 || !ace2) return False;
if (ace1->type != ace2->type) return False;
if (ace1->flags != ace2->flags) return False;
if (ace1->access_mask != ace2->access_mask) return False;
if (!dom_sid_equal(&ace1->trustee, &ace2->trustee)) return False;
return True;
}
/*
compare two security acl structures
*/
BOOL security_acl_equal(const struct security_acl *acl1,
const struct security_acl *acl2)
{
int i;
if (acl1 == acl2) return True;
if (!acl1 || !acl2) return False;
if (acl1->revision != acl2->revision) return False;
if (acl1->num_aces != acl2->num_aces) return False;
for (i=0;i<acl1->num_aces;i++) {
if (!security_ace_equal(&acl1->aces[i], &acl2->aces[i])) return False;
}
return True;
}
/*
compare two security descriptors.
*/
BOOL security_descriptor_equal(const struct security_descriptor *sd1,
const struct security_descriptor *sd2)
{
if (sd1 == sd2) return True;
if (!sd1 || !sd2) return False;
if (sd1->revision != sd2->revision) return False;
if (sd1->type != sd2->type) return False;
if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return False;
if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return False;
if (!security_acl_equal(sd1->sacl, sd2->sacl)) return False;
if (!security_acl_equal(sd1->dacl, sd2->dacl)) return False;
return True;
}
/*
compare two security descriptors, but allow certain (missing) parts
to be masked out of the comparison
*/
BOOL security_descriptor_mask_equal(const struct security_descriptor *sd1,
const struct security_descriptor *sd2,
uint32_t mask)
{
if (sd1 == sd2) return True;
if (!sd1 || !sd2) return False;
if (sd1->revision != sd2->revision) return False;
if ((sd1->type & mask) != (sd2->type & mask)) return False;
if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return False;
if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return False;
if ((mask & SEC_DESC_DACL_PRESENT) && !security_acl_equal(sd1->dacl, sd2->dacl)) return False;
if ((mask & SEC_DESC_SACL_PRESENT) && !security_acl_equal(sd1->sacl, sd2->sacl)) return False;
return True;
}
/*
create a security descriptor using string SIDs. This is used by the
torture code to allow the easy creation of complex ACLs
This is a varargs function. The list of DACL ACEs ends with a NULL sid.
Each ACE contains a set of 4 parameters:
SID, ACCESS_TYPE, MASK, FLAGS
a typical call would be:
sd = security_descriptor_create(mem_ctx,
mysid,
mygroup,
SID_NT_AUTHENTICATED_USERS,
SEC_ACE_TYPE_ACCESS_ALLOWED,
SEC_FILE_ALL,
SEC_ACE_FLAG_OBJECT_INHERIT,
NULL);
that would create a sd with one DACL ACE
*/
struct security_descriptor *security_descriptor_create(TALLOC_CTX *mem_ctx,
const char *owner_sid,
const char *group_sid,
...)
{
va_list ap;
struct security_descriptor *sd;
const char *sidstr;
sd = security_descriptor_initialise(mem_ctx);
if (sd == NULL) return NULL;
if (owner_sid) {
sd->owner_sid = dom_sid_parse_talloc(sd, owner_sid);
if (sd->owner_sid == NULL) {
talloc_free(sd);
return NULL;
}
}
if (group_sid) {
sd->group_sid = dom_sid_parse_talloc(sd, group_sid);
if (sd->group_sid == NULL) {
talloc_free(sd);
return NULL;
}
}
va_start(ap, group_sid);
while ((sidstr = va_arg(ap, const char *))) {
struct dom_sid *sid;
struct security_ace *ace = talloc(sd, struct security_ace);
NTSTATUS status;
if (ace == NULL) {
talloc_free(sd);
va_end(ap);
return NULL;
}
ace->type = va_arg(ap, unsigned int);
ace->access_mask = va_arg(ap, unsigned int);
ace->flags = va_arg(ap, unsigned int);
sid = dom_sid_parse_talloc(ace, sidstr);
if (sid == NULL) {
va_end(ap);
talloc_free(sd);
return NULL;
}
ace->trustee = *sid;
status = security_descriptor_dacl_add(sd, ace);
/* TODO: check: would talloc_free(ace) here be correct? */
if (!NT_STATUS_IS_OK(status)) {
va_end(ap);
talloc_free(sd);
return NULL;
}
}
va_end(ap);
return sd;
}
|