2 Unix SMB/CIFS implementation.
4 security descriptor description language functions
6 Copyright (C) Andrew Tridgell 2005
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 "system/iconv.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26 #include "libcli/security/proto.h"
34 map a series of letter codes into a uint32_t
36 static BOOL sddl_map_flags(const struct flag_map *map, const char *str,
37 uint32_t *flags, size_t *len)
39 const char *str0 = str;
42 while (str[0] && isupper(str[0])) {
44 for (i=0;map[i].name;i++) {
45 size_t l = strlen(map[i].name);
46 if (strncmp(map[i].name, str, l) == 0) {
47 *flags |= map[i].flag;
53 if (map[i].name == NULL) {
54 DEBUG(1, ("Unknown flag - %s in %s\n", str, str0));
62 a mapping between the 2 letter SID codes and sid strings
69 { "AO", SID_BUILTIN_ACCOUNT_OPERATORS },
70 { "BA", SID_BUILTIN_ADMINISTRATORS },
71 { "RU", SID_BUILTIN_PREW2K },
72 { "PO", SID_BUILTIN_PRINT_OPERATORS },
73 { "RS", SID_BUILTIN_RAS_SERVERS },
75 { "AU", SID_NT_AUTHENTICATED_USERS },
76 { "SY", SID_NT_SYSTEM },
77 { "PS", SID_NT_SELF },
79 { "ED", SID_NT_ENTERPRISE_DCS },
81 { "CO", SID_CREATOR_OWNER },
82 { "CG", SID_CREATOR_GROUP },
84 { "DA", NULL, DOMAIN_RID_ADMINS },
85 { "EA", NULL, DOMAIN_RID_ENTERPRISE_ADMINS },
86 { "DD", NULL, DOMAIN_RID_DCS },
87 { "DU", NULL, DOMAIN_RID_USERS },
88 { "CA", NULL, DOMAIN_RID_CERT_ADMINS },
93 It can either be a special 2 letter code, or in S-* format
95 static struct dom_sid *sddl_decode_sid(TALLOC_CTX *mem_ctx, const char **sddlp,
96 const struct dom_sid *domain_sid)
98 const char *sddl = (*sddlp);
101 /* see if its in the numeric format */
102 if (strncmp(sddl, "S-", 2) == 0) {
103 size_t len = strspn(sddl+2, "-0123456789");
105 return dom_sid_parse_talloc(mem_ctx, sddl);
108 /* now check for one of the special codes */
109 for (i=0;i<ARRAY_SIZE(sid_codes);i++) {
110 if (strncmp(sid_codes[i].code, sddl, 2) == 0) break;
112 if (i == ARRAY_SIZE(sid_codes)) {
113 DEBUG(1,("Unknown sddl sid code '%2.2s'\n", sddl));
119 if (sid_codes[i].sid == NULL) {
120 return dom_sid_add_rid(mem_ctx, domain_sid, sid_codes[i].rid);
123 return dom_sid_parse_talloc(mem_ctx, sid_codes[i].sid);
126 static const struct flag_map ace_types[] = {
127 { "AU", SEC_ACE_TYPE_SYSTEM_AUDIT },
128 { "AL", SEC_ACE_TYPE_SYSTEM_ALARM },
129 { "OA", SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT },
130 { "OD", SEC_ACE_TYPE_ACCESS_DENIED_OBJECT },
131 { "OU", SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT },
132 { "OL", SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT },
133 { "A", SEC_ACE_TYPE_ACCESS_ALLOWED },
134 { "D", SEC_ACE_TYPE_ACCESS_DENIED },
138 static const struct flag_map ace_flags[] = {
139 { "OI", SEC_ACE_FLAG_OBJECT_INHERIT },
140 { "CI", SEC_ACE_FLAG_CONTAINER_INHERIT },
141 { "NP", SEC_ACE_FLAG_NO_PROPAGATE_INHERIT },
142 { "IO", SEC_ACE_FLAG_INHERIT_ONLY },
143 { "ID", SEC_ACE_FLAG_INHERITED_ACE },
144 { "SA", SEC_ACE_FLAG_SUCCESSFUL_ACCESS },
145 { "FA", SEC_ACE_FLAG_FAILED_ACCESS },
149 static const struct flag_map ace_access_mask[] = {
150 { "RP", SEC_ADS_READ_PROP },
151 { "WP", SEC_ADS_WRITE_PROP },
152 { "CR", SEC_ADS_CONTROL_ACCESS },
153 { "CC", SEC_ADS_CREATE_CHILD },
154 { "DC", SEC_ADS_DELETE_CHILD },
155 { "LC", SEC_ADS_LIST },
156 { "LO", SEC_ADS_LIST_OBJECT },
157 { "RC", SEC_STD_READ_CONTROL },
158 { "WO", SEC_STD_WRITE_OWNER },
159 { "WD", SEC_STD_WRITE_DAC },
160 { "SD", SEC_STD_DELETE },
161 { "DT", SEC_ADS_DELETE_TREE },
162 { "SW", SEC_ADS_SELF_WRITE },
163 { "GA", SEC_GENERIC_ALL },
164 { "GR", SEC_GENERIC_READ },
165 { "GW", SEC_GENERIC_WRITE },
166 { "GX", SEC_GENERIC_EXECUTE },
172 return True on success, False on failure
173 note that this routine modifies the string
175 static BOOL sddl_decode_ace(TALLOC_CTX *mem_ctx, struct security_ace *ace, char *str,
176 const struct dom_sid *domain_sid)
186 /* parse out the 6 tokens */
189 char *ptr = strchr(str, ';');
190 if (ptr == NULL) return False;
197 if (!sddl_map_flags(ace_types, tok[0], &v, NULL)) {
203 if (!sddl_map_flags(ace_flags, tok[1], &v, NULL)) {
209 if (strncmp(tok[2], "0x", 2) == 0) {
210 ace->access_mask = strtol(tok[2], NULL, 16);
212 if (!sddl_map_flags(ace_access_mask, tok[2], &v, NULL)) {
215 ace->access_mask = v;
219 if (tok[3][0] != 0) {
220 NTSTATUS status = GUID_from_string(tok[3],
221 &ace->object.object.type.type);
222 if (!NT_STATUS_IS_OK(status)) {
225 ace->object.object.flags |= SEC_ACE_OBJECT_TYPE_PRESENT;
229 if (tok[4][0] != 0) {
230 NTSTATUS status = GUID_from_string(tok[4],
231 &ace->object.object.inherited_type.inherited_type);
232 if (!NT_STATUS_IS_OK(status)) {
235 ace->object.object.flags |= SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT;
240 sid = sddl_decode_sid(mem_ctx, &s, domain_sid);
245 talloc_steal(mem_ctx, sid->sub_auths);
251 static const struct flag_map acl_flags[] = {
252 { "P", SEC_DESC_DACL_PROTECTED },
253 { "AR", SEC_DESC_DACL_AUTO_INHERIT_REQ },
254 { "AI", SEC_DESC_DACL_AUTO_INHERITED },
261 static struct security_acl *sddl_decode_acl(struct security_descriptor *sd,
262 const char **sddlp, uint32_t *flags,
263 const struct dom_sid *domain_sid)
265 const char *sddl = *sddlp;
266 struct security_acl *acl;
271 acl = talloc_zero(sd, struct security_acl);
272 if (acl == NULL) return NULL;
273 acl->revision = SECURITY_ACL_REVISION_NT4;
275 if (isupper(sddl[0]) && sddl[1] == ':') {
276 /* its an empty ACL */
280 /* work out the ACL flags */
281 if (!sddl_map_flags(acl_flags, sddl, flags, &len)) {
288 while (*sddl == '(') {
290 len = strcspn(sddl+1, ")");
291 astr = talloc_strndup(acl, sddl+1, len);
292 if (astr == NULL || sddl[len+1] != ')') {
296 acl->aces = talloc_realloc(acl, acl->aces, struct security_ace,
298 if (acl->aces == NULL) {
302 if (!sddl_decode_ace(acl->aces, &acl->aces[acl->num_aces],
317 decode a security descriptor in SDDL format
319 struct security_descriptor *sddl_decode(TALLOC_CTX *mem_ctx, const char *sddl,
320 const struct dom_sid *domain_sid)
322 struct security_descriptor *sd;
323 sd = talloc_zero(mem_ctx, struct security_descriptor);
325 sd->revision = SECURITY_DESCRIPTOR_REVISION_1;
326 sd->type = SEC_DESC_SELF_RELATIVE;
331 if (sddl[1] != ':') goto failed;
336 if (sd->dacl != NULL) goto failed;
337 sd->dacl = sddl_decode_acl(sd, &sddl, &flags, domain_sid);
338 if (sd->dacl == NULL) goto failed;
339 sd->type |= flags | SEC_DESC_DACL_PRESENT;
342 if (sd->sacl != NULL) goto failed;
343 sd->sacl = sddl_decode_acl(sd, &sddl, &flags, domain_sid);
344 if (sd->sacl == NULL) goto failed;
345 /* this relies on the SEC_DESC_SACL_* flags being
346 1 bit shifted from the SEC_DESC_DACL_* flags */
347 sd->type |= (flags<<1) | SEC_DESC_SACL_PRESENT;
350 if (sd->owner_sid != NULL) goto failed;
351 sd->owner_sid = sddl_decode_sid(sd, &sddl, domain_sid);
352 if (sd->owner_sid == NULL) goto failed;
355 if (sd->group_sid != NULL) goto failed;
356 sd->group_sid = sddl_decode_sid(sd, &sddl, domain_sid);
357 if (sd->group_sid == NULL) goto failed;
365 DEBUG(2,("Badly formatted SDDL '%s'\n", sddl));
371 turn a set of flags into a string
373 static char *sddl_flags_to_string(TALLOC_CTX *mem_ctx, const struct flag_map *map,
374 uint32_t flags, BOOL check_all)
379 /* try to find an exact match */
380 for (i=0;map[i].name;i++) {
381 if (map[i].flag == flags) {
382 return talloc_strdup(mem_ctx, map[i].name);
386 s = talloc_strdup(mem_ctx, "");
389 for (i=0;map[i].name;i++) {
390 if ((flags & map[i].flag) != 0) {
391 s = talloc_asprintf_append(s, "%s", map[i].name);
392 if (s == NULL) goto failed;
393 flags &= ~map[i].flag;
397 if (check_all && flags != 0) {
409 encode a sid in SDDL format
411 static char *sddl_encode_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
412 const struct dom_sid *domain_sid)
417 sidstr = dom_sid_string(mem_ctx, sid);
418 if (sidstr == NULL) return NULL;
420 /* seen if its a well known sid */
421 for (i=0;sid_codes[i].sid;i++) {
422 if (strcmp(sidstr, sid_codes[i].sid) == 0) {
424 return talloc_strdup(mem_ctx, sid_codes[i].code);
428 /* or a well known rid in our domain */
429 if (dom_sid_in_domain(domain_sid, sid)) {
430 uint32_t rid = sid->sub_auths[sid->num_auths-1];
431 for (;i<ARRAY_SIZE(sid_codes);i++) {
432 if (rid == sid_codes[i].rid) {
434 return talloc_strdup(mem_ctx, sid_codes[i].code);
441 /* TODO: encode well known sids as two letter codes */
442 return dom_sid_string(mem_ctx, sid);
447 encode an ACE in SDDL format
449 static char *sddl_encode_ace(TALLOC_CTX *mem_ctx, const struct security_ace *ace,
450 const struct dom_sid *domain_sid)
454 const char *s_type="", *s_flags="", *s_mask="",
455 *s_object="", *s_iobject="", *s_trustee="";
457 tmp_ctx = talloc_new(mem_ctx);
458 if (tmp_ctx == NULL) {
459 DEBUG(0, ("talloc_new failed\n"));
463 s_type = sddl_flags_to_string(tmp_ctx, ace_types, ace->type, True);
464 if (s_type == NULL) goto failed;
466 s_flags = sddl_flags_to_string(tmp_ctx, ace_flags, ace->flags, True);
467 if (s_flags == NULL) goto failed;
469 s_mask = sddl_flags_to_string(tmp_ctx, ace_access_mask, ace->access_mask, True);
470 if (s_mask == NULL) {
471 s_mask = talloc_asprintf(tmp_ctx, "0x%08x", ace->access_mask);
472 if (s_mask == NULL) goto failed;
475 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
476 ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT ||
477 ace->type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT ||
478 ace->type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT) {
479 if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
480 s_object = GUID_string(tmp_ctx, &ace->object.object.type.type);
481 if (s_object == NULL) goto failed;
484 if (ace->object.object.flags & SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
485 s_iobject = GUID_string(tmp_ctx, &ace->object.object.inherited_type.inherited_type);
486 if (s_iobject == NULL) goto failed;
490 s_trustee = sddl_encode_sid(tmp_ctx, &ace->trustee, domain_sid);
491 if (s_trustee == NULL) goto failed;
493 sddl = talloc_asprintf(mem_ctx, "%s;%s;%s;%s;%s;%s",
494 s_type, s_flags, s_mask, s_object, s_iobject, s_trustee);
497 talloc_free(tmp_ctx);
502 encode an ACL in SDDL format
504 static char *sddl_encode_acl(TALLOC_CTX *mem_ctx, const struct security_acl *acl,
505 uint32_t flags, const struct dom_sid *domain_sid)
510 /* add any ACL flags */
511 sddl = sddl_flags_to_string(mem_ctx, acl_flags, flags, False);
512 if (sddl == NULL) goto failed;
514 /* now the ACEs, encoded in braces */
515 for (i=0;i<acl->num_aces;i++) {
516 char *ace = sddl_encode_ace(sddl, &acl->aces[i], domain_sid);
517 if (ace == NULL) goto failed;
518 sddl = talloc_asprintf_append(sddl, "(%s)", ace);
519 if (sddl == NULL) goto failed;
532 encode a security descriptor to SDDL format
534 char *sddl_encode(TALLOC_CTX *mem_ctx, const struct security_descriptor *sd,
535 const struct dom_sid *domain_sid)
540 /* start with a blank string */
541 sddl = talloc_strdup(mem_ctx, "");
542 if (sddl == NULL) goto failed;
544 tmp_ctx = talloc_new(mem_ctx);
546 if (sd->owner_sid != NULL) {
547 char *sid = sddl_encode_sid(tmp_ctx, sd->owner_sid, domain_sid);
548 if (sid == NULL) goto failed;
549 sddl = talloc_asprintf_append(sddl, "O:%s", sid);
550 if (sddl == NULL) goto failed;
553 if (sd->group_sid != NULL) {
554 char *sid = sddl_encode_sid(tmp_ctx, sd->group_sid, domain_sid);
555 if (sid == NULL) goto failed;
556 sddl = talloc_asprintf_append(sddl, "G:%s", sid);
557 if (sddl == NULL) goto failed;
560 if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl != NULL) {
561 char *acl = sddl_encode_acl(tmp_ctx, sd->dacl, sd->type, domain_sid);
562 if (acl == NULL) goto failed;
563 sddl = talloc_asprintf_append(sddl, "D:%s", acl);
564 if (sddl == NULL) goto failed;
567 if ((sd->type & SEC_DESC_SACL_PRESENT) && sd->sacl != NULL) {
568 char *acl = sddl_encode_acl(tmp_ctx, sd->sacl, sd->type>>1, domain_sid);
569 if (acl == NULL) goto failed;
570 sddl = talloc_asprintf_append(sddl, "S:%s", acl);
571 if (sddl == NULL) goto failed;
574 talloc_free(tmp_ctx);