summaryrefslogtreecommitdiff
path: root/libcli/security/create_descriptor.c
blob: 92528965d1fb63e86c1c7e290b593929f58ff9ba (plain)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
   Copyright (C) Nadezhda Ivanova 2009

   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 3 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, see <http://www.gnu.org/licenses/>.
*/

/*
 *  Name: create_descriptor
 *
 *  Component: routines for calculating and creating security descriptors
 *  as described in MS-DTYP 2.5.3.x
 *
 *  Description:
 *
 *
 *  Author: Nadezhda Ivanova
 */
#include "includes.h"
#include "libcli/security/security.h"
#include "librpc/gen_ndr/ndr_security.h"

/* Todos:
 * build the security token dacl as follows:
 * SYSTEM: GA, OWNER: GA, LOGIN_SID:GW|GE
 * Need session id information for the login SID. Probably
 * the best place for this is during token creation
 *
 * Implement SD Invariants
 * ACE sorting rules
 * LDAP_SERVER_SD_FLAGS_OID control
 * ADTS 7.1.3.3 needs to be clarified
 */

/* the mapping function for generic rights for DS.(GA,GR,GW,GX)
 * The mapping function is passed as an argument to the
 * descriptor calculating routine and depends on the security
 * manager that calls the calculating routine.
 * TODO: need similar mappings for the file system and
 * registry security managers in order to make this code
 * generic for all security managers
 */

uint32_t map_generic_rights_ds(uint32_t access_mask)
{
	if (access_mask & SEC_GENERIC_ALL) {
		access_mask |= SEC_ADS_GENERIC_ALL;
		access_mask &= ~SEC_GENERIC_ALL;
	}

	if (access_mask & SEC_GENERIC_EXECUTE) {
		access_mask |= SEC_ADS_GENERIC_EXECUTE;
		access_mask &= ~SEC_GENERIC_EXECUTE;
	}

	if (access_mask & SEC_GENERIC_WRITE) {
		access_mask |= SEC_ADS_GENERIC_WRITE;
		access_mask &= ~SEC_GENERIC_WRITE;
	}

	if (access_mask & SEC_GENERIC_READ) {
		access_mask |= SEC_ADS_GENERIC_READ;
		access_mask &= ~SEC_GENERIC_READ;
	}

	return access_mask;
}

/* Not sure what this has to be,
* and it does not seem to have any influence */
static bool object_in_list(struct GUID *object_list, struct GUID *object)
{
	return true;
}
 
/* returns true if the ACE gontains generic information
 * that needs to be processed additionally */
 
static bool desc_ace_has_generic(TALLOC_CTX *mem_ctx,
			     struct security_ace *ace)
{
	struct dom_sid *co, *cg;
	co = dom_sid_parse_talloc(mem_ctx,  SID_CREATOR_OWNER);
	cg = dom_sid_parse_talloc(mem_ctx,  SID_CREATOR_GROUP);
	if (ace->access_mask & SEC_GENERIC_ALL || ace->access_mask & SEC_GENERIC_READ ||
	    ace->access_mask & SEC_GENERIC_WRITE || ace->access_mask & SEC_GENERIC_EXECUTE) {
		return true;
	}
	if (dom_sid_equal(&ace->trustee, co) || dom_sid_equal(&ace->trustee, cg)) {
		return true;
	}
	return false;
}

/* creates an ace in which the generic information is expanded */

static void desc_expand_generic(TALLOC_CTX *mem_ctx,
				struct security_ace *new_ace,
				struct dom_sid *owner,
				struct dom_sid *group)
{
	struct dom_sid *co, *cg;
	co = dom_sid_parse_talloc(mem_ctx,  SID_CREATOR_OWNER);
	cg = dom_sid_parse_talloc(mem_ctx,  SID_CREATOR_GROUP);
	new_ace->access_mask = map_generic_rights_ds(new_ace->access_mask);
	if (dom_sid_equal(&new_ace->trustee, co)) {
		new_ace->trustee = *owner;
	}
	if (dom_sid_equal(&new_ace->trustee, cg)) {
		new_ace->trustee = *group;
	}
	new_ace->flags = 0x0;
}

static struct security_acl *calculate_inherited_from_parent(TALLOC_CTX *mem_ctx,
							    struct security_acl *acl,
							    bool is_container,
							    struct dom_sid *owner,
							    struct dom_sid *group,
							    struct GUID *object_list)
{
	uint32_t i;
	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	struct security_acl *tmp_acl = talloc_zero(mem_ctx, struct security_acl);
	struct dom_sid *co, *cg;
	if (!tmp_acl) {
		return NULL;
	}

	if (!acl) {
		return NULL;
	}
	co = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_OWNER);
	cg = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_GROUP);

	for (i=0; i < acl->num_aces; i++) {
		struct security_ace *ace = &acl->aces[i];
		if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ||
		    (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT)) {
			tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
						       struct security_ace,
						       tmp_acl->num_aces+1);
			if (tmp_acl->aces == NULL) {
				talloc_free(tmp_ctx);
				return NULL;
			}

			tmp_acl->aces[tmp_acl->num_aces] = *ace;
			tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERITED_ACE;
			/* remove IO flag from the child's ace */
			if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
			    !desc_ace_has_generic(tmp_ctx, ace)) {
				tmp_acl->aces[tmp_acl->num_aces].flags &= ~SEC_ACE_FLAG_INHERIT_ONLY;
			}

			if (is_container && (ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
			    tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;

			if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
			    ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT) {
				if (!object_in_list(object_list, &ace->object.object.type.type)) {
					tmp_acl->aces[tmp_acl->num_aces].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
				}

			}
			tmp_acl->num_aces++;
			if (is_container) {
				if (!(ace->flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) &&
				    (desc_ace_has_generic(tmp_ctx, ace))) {
					    tmp_acl->aces = talloc_realloc(tmp_acl,
									   tmp_acl->aces,
									   struct security_ace,
									   tmp_acl->num_aces+1);
					    if (tmp_acl->aces == NULL) {
						    talloc_free(tmp_ctx);
						    return NULL;
					    }
					    tmp_acl->aces[tmp_acl->num_aces] = *ace;
					    desc_expand_generic(tmp_ctx,
								&tmp_acl->aces[tmp_acl->num_aces],
								owner,
								group);
					    tmp_acl->aces[tmp_acl->num_aces].flags = SEC_ACE_FLAG_INHERITED_ACE;
					    tmp_acl->num_aces++;
				}
			}
		}
	}
	if (tmp_acl->num_aces == 0) {
		return NULL;
	}
	if (acl) {
		tmp_acl->revision = acl->revision;
	}
	return tmp_acl;
}

static struct security_acl *process_user_acl(TALLOC_CTX *mem_ctx,
					     struct security_acl *acl,
					     bool is_container,
					     struct dom_sid *owner,
					     struct dom_sid *group,
					     struct GUID *object_list,
					     bool is_protected)
{
	uint32_t i;
	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	struct security_acl *tmp_acl = talloc_zero(tmp_ctx, struct security_acl);
	struct security_acl *new_acl;
	struct dom_sid *co, *cg;

	if (!acl)
		return NULL;

	if (!tmp_acl)
		return NULL;

	tmp_acl->revision = acl->revision;
	DEBUG(6,(__location__ ": acl revision %d\n", acl->revision));

	co = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_OWNER);
	cg = dom_sid_parse_talloc(tmp_ctx,  SID_CREATOR_GROUP);

	for (i=0; i < acl->num_aces; i++){
		struct security_ace *ace = &acl->aces[i];
		/* Remove ID flags from user-provided ACEs
		 * if we break inheritance, ignore them otherwise */
		if (ace->flags & SEC_ACE_FLAG_INHERITED_ACE) {
			if (is_protected) {
				ace->flags &= ~SEC_ACE_FLAG_INHERITED_ACE;
			} else {
				continue;
			}
		}

		if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY &&
		    !(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT ||
		      ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT))
			continue;

		tmp_acl->aces = talloc_realloc(tmp_acl,
					       tmp_acl->aces,
					       struct security_ace,
					       tmp_acl->num_aces+1);
		tmp_acl->aces[tmp_acl->num_aces] = *ace;
		tmp_acl->num_aces++;
		if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
			continue;
		}
		/* if the ACE contains CO, CG, GA, GE, GR or GW, and is inheritable
		 * it has to be expanded to two aces, the original as IO,
		 * and another one where these are translated */
		if (desc_ace_has_generic(tmp_ctx, ace)) {
			if (!(ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT)) {
				desc_expand_generic(tmp_ctx,
						    &tmp_acl->aces[tmp_acl->num_aces-1],
						    owner,
						    group);
			} else {
				/*The original ACE becomes read only */
				tmp_acl->aces[tmp_acl->num_aces-1].flags |= SEC_ACE_FLAG_INHERIT_ONLY;
				tmp_acl->aces = talloc_realloc(tmp_acl, tmp_acl->aces,
							       struct security_ace,
							       tmp_acl->num_aces+1);
				/* add a new ACE with expanded generic info */
				tmp_acl->aces[tmp_acl->num_aces] = *ace;
				desc_expand_generic(tmp_ctx,
						    &tmp_acl->aces[tmp_acl->num_aces],
						    owner,
						    group);
				tmp_acl->num_aces++;
			}
		}
	}
	new_acl = security_acl_dup(mem_ctx,tmp_acl);

	if (new_acl)
		new_acl->revision = acl->revision;

	talloc_free(tmp_ctx);
	return new_acl;
}

static void cr_descr_log_descriptor(struct security_descriptor *sd,
				    const char *message,
				    int level)
{
	if (sd) {
		DEBUG(level,("%s: %s\n", message,
			     ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,
						     "", sd)));
	}
	else {
		DEBUG(level,("%s: NULL\n", message));
	}
}

#if 0
static void cr_descr_log_acl(struct security_acl *acl,
				    const char *message,
				    int level)
{
	if (acl) {
		DEBUG(level,("%s: %s\n", message,
			     ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_acl,
						     "", acl)));
	}
	else {
		DEBUG(level,("%s: NULL\n", message));
	}
}
#endif

static bool compute_acl(struct security_descriptor *parent_sd,
			struct security_descriptor *creator_sd,
			bool is_container,
			uint32_t inherit_flags,
			struct GUID *object_list,
			uint32_t (*generic_map)(uint32_t access_mask),
			struct security_token *token,
			struct security_descriptor *new_sd) /* INOUT argument */
{
	struct security_acl *user_dacl, *user_sacl, *inherited_dacl, *inherited_sacl;
	int level = 10;

	if (!parent_sd || !(inherit_flags & SEC_DACL_AUTO_INHERIT)) {
		inherited_dacl = NULL;
	} else if (creator_sd && (creator_sd->type & SEC_DESC_DACL_PROTECTED)) {
		inherited_dacl = NULL;
	} else {
		inherited_dacl = calculate_inherited_from_parent(new_sd,
								 parent_sd->dacl,
								 is_container,
								 new_sd->owner_sid,
								 new_sd->group_sid,
								 object_list);
	}


	if (!parent_sd || !(inherit_flags & SEC_SACL_AUTO_INHERIT)) {
		inherited_sacl = NULL;
	} else if (creator_sd && (creator_sd->type & SEC_DESC_SACL_PROTECTED)) {
		inherited_sacl = NULL;
	} else {
		inherited_sacl = calculate_inherited_from_parent(new_sd,
								 parent_sd->sacl,
								 is_container,
								 new_sd->owner_sid,
								 new_sd->group_sid,
								 object_list);
	}

	if (!creator_sd || (inherit_flags & SEC_DEFAULT_DESCRIPTOR)) {
		user_dacl = NULL;
		user_sacl = NULL;
	} else {
		user_dacl = process_user_acl(new_sd,
					     creator_sd->dacl,
					     is_container,
					     new_sd->owner_sid,
					     new_sd->group_sid,
					     object_list,
					     creator_sd->type & SEC_DESC_DACL_PROTECTED);
		user_sacl = process_user_acl(new_sd,
					     creator_sd->sacl,
					     is_container,
					     new_sd->owner_sid,
					     new_sd->group_sid,
					     object_list,
					     creator_sd->type & SEC_DESC_SACL_PROTECTED);
	}
	cr_descr_log_descriptor(parent_sd, __location__"parent_sd", level);
	cr_descr_log_descriptor(creator_sd,__location__ "creator_sd", level);

	new_sd->dacl = security_acl_concatenate(new_sd, user_dacl, inherited_dacl);
	if (new_sd->dacl) {
		new_sd->type |= SEC_DESC_DACL_PRESENT;
	}
	if (inherited_dacl) {
		new_sd->type |= SEC_DESC_DACL_AUTO_INHERITED;
	}

	new_sd->sacl = security_acl_concatenate(new_sd, user_sacl, inherited_sacl);
	if (new_sd->sacl) {
		new_sd->type |= SEC_DESC_SACL_PRESENT;
	}
	if (inherited_sacl) {
		new_sd->type |= SEC_DESC_SACL_AUTO_INHERITED;
	}
	/* This is a hack to handle the fact that
	 * apprantly any AI flag provided by the user is preserved */
	if (creator_sd)
		new_sd->type |= creator_sd->type;
	cr_descr_log_descriptor(new_sd, __location__"final sd", level);
	return true;
}

struct security_descriptor *create_security_descriptor(TALLOC_CTX *mem_ctx,
						       struct security_descriptor *parent_sd,
						       struct security_descriptor *creator_sd,
						       bool is_container,
						       struct GUID *object_list,
						       uint32_t inherit_flags,
						       struct security_token *token,
						       struct dom_sid *default_owner, /* valid only for DS, NULL for the other RSs */
						       struct dom_sid *default_group, /* valid only for DS, NULL for the other RSs */
						       uint32_t (*generic_map)(uint32_t access_mask))
{
	struct security_descriptor *new_sd;
	struct dom_sid *new_owner = NULL;
	struct dom_sid *new_group = NULL;

	new_sd = security_descriptor_initialise(mem_ctx);
	if (!new_sd) {
		return NULL;
	}

	if (!creator_sd || !creator_sd->owner_sid) {
		if ((inherit_flags & SEC_OWNER_FROM_PARENT) && parent_sd) {
			new_owner = parent_sd->owner_sid;
		} else if (!default_owner) {
			new_owner = &token->sids[PRIMARY_USER_SID_INDEX];
		} else {
			new_owner = default_owner;
			new_sd->type |= SEC_DESC_OWNER_DEFAULTED;
		}
	} else {
		new_owner = creator_sd->owner_sid;
	}

	if (!creator_sd || !creator_sd->group_sid){
		if ((inherit_flags & SEC_GROUP_FROM_PARENT) && parent_sd) {
			new_group = parent_sd->group_sid;
		} else if (!default_group && token->num_sids > PRIMARY_GROUP_SID_INDEX) {
			new_group = &token->sids[PRIMARY_GROUP_SID_INDEX];
		} else if (!default_group) {
			/* This will happen only for anonymous, which has no other groups */
			new_group = &token->sids[PRIMARY_USER_SID_INDEX];
		} else {
			new_group = default_group;
			new_sd->type |= SEC_DESC_GROUP_DEFAULTED;
		}
	} else {
		new_group = creator_sd->group_sid;
	}

	new_sd->owner_sid = talloc_memdup(new_sd, new_owner, sizeof(struct dom_sid));
	new_sd->group_sid = talloc_memdup(new_sd, new_group, sizeof(struct dom_sid));
	if (!new_sd->owner_sid || !new_sd->group_sid){
		talloc_free(new_sd);
		return NULL;
	}

	if (!compute_acl(parent_sd, creator_sd,
			 is_container, inherit_flags, object_list,
			 generic_map,token,new_sd)){
		talloc_free(new_sd);
		return NULL;
	}

	return new_sd;
}