s3:smb_macros.h: add IS_AD_DC as addition to IS_DC
[Samba.git] / libcli / security / sddl.c
blob9a5a2500e93b0cecde09151e33c63155fbbd8022
1 /*
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 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/>.
22 #include "includes.h"
23 #include "libcli/security/security.h"
24 #include "librpc/gen_ndr/ndr_misc.h"
25 #include "system/locale.h"
27 struct flag_map {
28 const char *name;
29 uint32_t flag;
33 map a series of letter codes into a uint32_t
35 static bool sddl_map_flags(const struct flag_map *map, const char *str,
36 uint32_t *flags, size_t *len)
38 const char *str0 = str;
39 if (len) *len = 0;
40 *flags = 0;
41 while (str[0] && isupper(str[0])) {
42 int i;
43 for (i=0;map[i].name;i++) {
44 size_t l = strlen(map[i].name);
45 if (strncmp(map[i].name, str, l) == 0) {
46 *flags |= map[i].flag;
47 str += l;
48 if (len) *len += l;
49 break;
52 if (map[i].name == NULL) {
53 DEBUG(1, ("Unknown flag - %s in %s\n", str, str0));
54 return false;
57 return true;
61 a mapping between the 2 letter SID codes and sid strings
63 static const struct {
64 const char *code;
65 const char *sid;
66 uint32_t rid;
67 } sid_codes[] = {
68 { "WD", SID_WORLD },
70 { "CO", SID_CREATOR_OWNER },
71 { "CG", SID_CREATOR_GROUP },
73 { "NU", SID_NT_NETWORK },
74 { "IU", SID_NT_INTERACTIVE },
75 { "SU", SID_NT_SERVICE },
76 { "AN", SID_NT_ANONYMOUS },
77 { "ED", SID_NT_ENTERPRISE_DCS },
78 { "PS", SID_NT_SELF },
79 { "AU", SID_NT_AUTHENTICATED_USERS },
80 { "RC", SID_NT_RESTRICTED },
81 { "SY", SID_NT_SYSTEM },
82 { "LS", SID_NT_LOCAL_SERVICE },
83 { "NS", SID_NT_NETWORK_SERVICE },
84 { "IS", SID_NT_IUSR },
86 { "BA", SID_BUILTIN_ADMINISTRATORS },
87 { "BU", SID_BUILTIN_USERS },
88 { "BG", SID_BUILTIN_GUESTS },
89 { "PU", SID_BUILTIN_POWER_USERS },
90 { "AO", SID_BUILTIN_ACCOUNT_OPERATORS },
91 { "SO", SID_BUILTIN_SERVER_OPERATORS },
92 { "PO", SID_BUILTIN_PRINT_OPERATORS },
93 { "BO", SID_BUILTIN_BACKUP_OPERATORS },
94 { "RE", SID_BUILTIN_REPLICATOR },
95 { "BR", SID_BUILTIN_RAS_SERVERS },
96 { "RU", SID_BUILTIN_PREW2K },
97 { "RD", SID_BUILTIN_REMOTE_DESKTOP_USERS },
98 { "NO", SID_BUILTIN_NETWORK_CONF_OPERATORS },
99 { "IF", SID_BUILTIN_INCOMING_FOREST_TRUST },
101 { "LA", NULL, DOMAIN_RID_ADMINISTRATOR },
102 { "LG", NULL, DOMAIN_RID_GUEST },
103 { "LK", NULL, DOMAIN_RID_KRBTGT },
105 { "ER", NULL, DOMAIN_RID_ENTERPRISE_READONLY_DCS },
106 { "DA", NULL, DOMAIN_RID_ADMINS },
107 { "DU", NULL, DOMAIN_RID_USERS },
108 { "DG", NULL, DOMAIN_RID_GUESTS },
109 { "DC", NULL, DOMAIN_RID_DOMAIN_MEMBERS },
110 { "DD", NULL, DOMAIN_RID_DCS },
111 { "CA", NULL, DOMAIN_RID_CERT_ADMINS },
112 { "SA", NULL, DOMAIN_RID_SCHEMA_ADMINS },
113 { "EA", NULL, DOMAIN_RID_ENTERPRISE_ADMINS },
114 { "PA", NULL, DOMAIN_RID_POLICY_ADMINS },
115 { "RO", NULL, DOMAIN_RID_READONLY_DCS },
116 { "RS", NULL, DOMAIN_RID_RAS_SERVERS }
120 decode a SID
121 It can either be a special 2 letter code, or in S-* format
123 static struct dom_sid *sddl_decode_sid(TALLOC_CTX *mem_ctx, const char **sddlp,
124 const struct dom_sid *domain_sid)
126 const char *sddl = (*sddlp);
127 int i;
129 /* see if its in the numeric format */
130 if (strncmp(sddl, "S-", 2) == 0) {
131 struct dom_sid *sid;
132 char *sid_str;
133 size_t len = strspn(sddl+2, "-0123456789");
134 sid_str = talloc_strndup(mem_ctx, sddl, len+2);
135 if (!sid_str) {
136 return NULL;
138 (*sddlp) += len+2;
139 sid = dom_sid_parse_talloc(mem_ctx, sid_str);
140 talloc_free(sid_str);
141 return sid;
144 /* now check for one of the special codes */
145 for (i=0;i<ARRAY_SIZE(sid_codes);i++) {
146 if (strncmp(sid_codes[i].code, sddl, 2) == 0) break;
148 if (i == ARRAY_SIZE(sid_codes)) {
149 DEBUG(1,("Unknown sddl sid code '%2.2s'\n", sddl));
150 return NULL;
153 (*sddlp) += 2;
155 if (sid_codes[i].sid == NULL) {
156 return dom_sid_add_rid(mem_ctx, domain_sid, sid_codes[i].rid);
159 return dom_sid_parse_talloc(mem_ctx, sid_codes[i].sid);
162 static const struct flag_map ace_types[] = {
163 { "AU", SEC_ACE_TYPE_SYSTEM_AUDIT },
164 { "AL", SEC_ACE_TYPE_SYSTEM_ALARM },
165 { "OA", SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT },
166 { "OD", SEC_ACE_TYPE_ACCESS_DENIED_OBJECT },
167 { "OU", SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT },
168 { "OL", SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT },
169 { "A", SEC_ACE_TYPE_ACCESS_ALLOWED },
170 { "D", SEC_ACE_TYPE_ACCESS_DENIED },
171 { NULL, 0 }
174 static const struct flag_map ace_flags[] = {
175 { "OI", SEC_ACE_FLAG_OBJECT_INHERIT },
176 { "CI", SEC_ACE_FLAG_CONTAINER_INHERIT },
177 { "NP", SEC_ACE_FLAG_NO_PROPAGATE_INHERIT },
178 { "IO", SEC_ACE_FLAG_INHERIT_ONLY },
179 { "ID", SEC_ACE_FLAG_INHERITED_ACE },
180 { "SA", SEC_ACE_FLAG_SUCCESSFUL_ACCESS },
181 { "FA", SEC_ACE_FLAG_FAILED_ACCESS },
182 { NULL, 0 },
185 static const struct flag_map ace_access_mask[] = {
186 { "RP", SEC_ADS_READ_PROP },
187 { "WP", SEC_ADS_WRITE_PROP },
188 { "CR", SEC_ADS_CONTROL_ACCESS },
189 { "CC", SEC_ADS_CREATE_CHILD },
190 { "DC", SEC_ADS_DELETE_CHILD },
191 { "LC", SEC_ADS_LIST },
192 { "LO", SEC_ADS_LIST_OBJECT },
193 { "RC", SEC_STD_READ_CONTROL },
194 { "WO", SEC_STD_WRITE_OWNER },
195 { "WD", SEC_STD_WRITE_DAC },
196 { "SD", SEC_STD_DELETE },
197 { "DT", SEC_ADS_DELETE_TREE },
198 { "SW", SEC_ADS_SELF_WRITE },
199 { "GA", SEC_GENERIC_ALL },
200 { "GR", SEC_GENERIC_READ },
201 { "GW", SEC_GENERIC_WRITE },
202 { "GX", SEC_GENERIC_EXECUTE },
203 { NULL, 0 }
207 decode an ACE
208 return true on success, false on failure
209 note that this routine modifies the string
211 static bool sddl_decode_ace(TALLOC_CTX *mem_ctx, struct security_ace *ace, char *str,
212 const struct dom_sid *domain_sid)
214 const char *tok[6];
215 const char *s;
216 int i;
217 uint32_t v;
218 struct dom_sid *sid;
220 ZERO_STRUCTP(ace);
222 /* parse out the 6 tokens */
223 tok[0] = str;
224 for (i=0;i<5;i++) {
225 char *ptr = strchr(str, ';');
226 if (ptr == NULL) return false;
227 *ptr = 0;
228 str = ptr+1;
229 tok[i+1] = str;
232 /* parse ace type */
233 if (!sddl_map_flags(ace_types, tok[0], &v, NULL)) {
234 return false;
236 ace->type = v;
238 /* ace flags */
239 if (!sddl_map_flags(ace_flags, tok[1], &v, NULL)) {
240 return false;
242 ace->flags = v;
244 /* access mask */
245 if (strncmp(tok[2], "0x", 2) == 0) {
246 ace->access_mask = strtol(tok[2], NULL, 16);
247 } else {
248 if (!sddl_map_flags(ace_access_mask, tok[2], &v, NULL)) {
249 return false;
251 ace->access_mask = v;
254 /* object */
255 if (tok[3][0] != 0) {
256 NTSTATUS status = GUID_from_string(tok[3],
257 &ace->object.object.type.type);
258 if (!NT_STATUS_IS_OK(status)) {
259 return false;
261 ace->object.object.flags |= SEC_ACE_OBJECT_TYPE_PRESENT;
264 /* inherit object */
265 if (tok[4][0] != 0) {
266 NTSTATUS status = GUID_from_string(tok[4],
267 &ace->object.object.inherited_type.inherited_type);
268 if (!NT_STATUS_IS_OK(status)) {
269 return false;
271 ace->object.object.flags |= SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT;
274 /* trustee */
275 s = tok[5];
276 sid = sddl_decode_sid(mem_ctx, &s, domain_sid);
277 if (sid == NULL) {
278 return false;
280 ace->trustee = *sid;
281 talloc_free(sid);
283 return true;
286 static const struct flag_map acl_flags[] = {
287 { "P", SEC_DESC_DACL_PROTECTED },
288 { "AR", SEC_DESC_DACL_AUTO_INHERIT_REQ },
289 { "AI", SEC_DESC_DACL_AUTO_INHERITED },
290 { NULL, 0 }
294 decode an ACL
296 static struct security_acl *sddl_decode_acl(struct security_descriptor *sd,
297 const char **sddlp, uint32_t *flags,
298 const struct dom_sid *domain_sid)
300 const char *sddl = *sddlp;
301 struct security_acl *acl;
302 size_t len;
304 *flags = 0;
306 acl = talloc_zero(sd, struct security_acl);
307 if (acl == NULL) return NULL;
308 acl->revision = SECURITY_ACL_REVISION_ADS;
310 if (isupper(sddl[0]) && sddl[1] == ':') {
311 /* its an empty ACL */
312 return acl;
315 /* work out the ACL flags */
316 if (!sddl_map_flags(acl_flags, sddl, flags, &len)) {
317 talloc_free(acl);
318 return NULL;
320 sddl += len;
322 /* now the ACEs */
323 while (*sddl == '(') {
324 char *astr;
325 len = strcspn(sddl+1, ")");
326 astr = talloc_strndup(acl, sddl+1, len);
327 if (astr == NULL || sddl[len+1] != ')') {
328 talloc_free(acl);
329 return NULL;
331 acl->aces = talloc_realloc(acl, acl->aces, struct security_ace,
332 acl->num_aces+1);
333 if (acl->aces == NULL) {
334 talloc_free(acl);
335 return NULL;
337 if (!sddl_decode_ace(acl->aces, &acl->aces[acl->num_aces],
338 astr, domain_sid)) {
339 talloc_free(acl);
340 return NULL;
342 switch (acl->aces[acl->num_aces].type) {
343 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
344 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
345 case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
346 case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
347 acl->revision = SECURITY_ACL_REVISION_ADS;
348 break;
349 default:
350 break;
352 talloc_free(astr);
353 sddl += len+2;
354 acl->num_aces++;
357 (*sddlp) = sddl;
358 return acl;
362 decode a security descriptor in SDDL format
364 struct security_descriptor *sddl_decode(TALLOC_CTX *mem_ctx, const char *sddl,
365 const struct dom_sid *domain_sid)
367 struct security_descriptor *sd;
368 sd = talloc_zero(mem_ctx, struct security_descriptor);
370 sd->revision = SECURITY_DESCRIPTOR_REVISION_1;
371 sd->type = SEC_DESC_SELF_RELATIVE;
373 while (*sddl) {
374 uint32_t flags;
375 char c = sddl[0];
376 if (sddl[1] != ':') goto failed;
378 sddl += 2;
379 switch (c) {
380 case 'D':
381 if (sd->dacl != NULL) goto failed;
382 sd->dacl = sddl_decode_acl(sd, &sddl, &flags, domain_sid);
383 if (sd->dacl == NULL) goto failed;
384 sd->type |= flags | SEC_DESC_DACL_PRESENT;
385 break;
386 case 'S':
387 if (sd->sacl != NULL) goto failed;
388 sd->sacl = sddl_decode_acl(sd, &sddl, &flags, domain_sid);
389 if (sd->sacl == NULL) goto failed;
390 /* this relies on the SEC_DESC_SACL_* flags being
391 1 bit shifted from the SEC_DESC_DACL_* flags */
392 sd->type |= (flags<<1) | SEC_DESC_SACL_PRESENT;
393 break;
394 case 'O':
395 if (sd->owner_sid != NULL) goto failed;
396 sd->owner_sid = sddl_decode_sid(sd, &sddl, domain_sid);
397 if (sd->owner_sid == NULL) goto failed;
398 break;
399 case 'G':
400 if (sd->group_sid != NULL) goto failed;
401 sd->group_sid = sddl_decode_sid(sd, &sddl, domain_sid);
402 if (sd->group_sid == NULL) goto failed;
403 break;
407 return sd;
409 failed:
410 DEBUG(2,("Badly formatted SDDL '%s'\n", sddl));
411 talloc_free(sd);
412 return NULL;
416 turn a set of flags into a string
418 static char *sddl_flags_to_string(TALLOC_CTX *mem_ctx, const struct flag_map *map,
419 uint32_t flags, bool check_all)
421 int i;
422 char *s;
424 /* try to find an exact match */
425 for (i=0;map[i].name;i++) {
426 if (map[i].flag == flags) {
427 return talloc_strdup(mem_ctx, map[i].name);
431 s = talloc_strdup(mem_ctx, "");
433 /* now by bits */
434 for (i=0;map[i].name;i++) {
435 if ((flags & map[i].flag) != 0) {
436 s = talloc_asprintf_append_buffer(s, "%s", map[i].name);
437 if (s == NULL) goto failed;
438 flags &= ~map[i].flag;
442 if (check_all && flags != 0) {
443 goto failed;
446 return s;
448 failed:
449 talloc_free(s);
450 return NULL;
454 encode a sid in SDDL format
456 static char *sddl_encode_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
457 const struct dom_sid *domain_sid)
459 int i;
460 char *sidstr;
462 sidstr = dom_sid_string(mem_ctx, sid);
463 if (sidstr == NULL) return NULL;
465 /* seen if its a well known sid */
466 for (i=0;sid_codes[i].sid;i++) {
467 if (strcmp(sidstr, sid_codes[i].sid) == 0) {
468 talloc_free(sidstr);
469 return talloc_strdup(mem_ctx, sid_codes[i].code);
473 /* or a well known rid in our domain */
474 if (dom_sid_in_domain(domain_sid, sid)) {
475 uint32_t rid = sid->sub_auths[sid->num_auths-1];
476 for (;i<ARRAY_SIZE(sid_codes);i++) {
477 if (rid == sid_codes[i].rid) {
478 talloc_free(sidstr);
479 return talloc_strdup(mem_ctx, sid_codes[i].code);
484 talloc_free(sidstr);
486 /* TODO: encode well known sids as two letter codes */
487 return dom_sid_string(mem_ctx, sid);
492 encode an ACE in SDDL format
494 static char *sddl_encode_ace(TALLOC_CTX *mem_ctx, const struct security_ace *ace,
495 const struct dom_sid *domain_sid)
497 char *sddl = NULL;
498 TALLOC_CTX *tmp_ctx;
499 const char *sddl_type="", *sddl_flags="", *sddl_mask="",
500 *sddl_object="", *sddl_iobject="", *sddl_trustee="";
502 tmp_ctx = talloc_new(mem_ctx);
503 if (tmp_ctx == NULL) {
504 DEBUG(0, ("talloc_new failed\n"));
505 return NULL;
508 sddl_type = sddl_flags_to_string(tmp_ctx, ace_types, ace->type, true);
509 if (sddl_type == NULL) {
510 goto failed;
513 sddl_flags = sddl_flags_to_string(tmp_ctx, ace_flags, ace->flags,
514 true);
515 if (sddl_flags == NULL) {
516 goto failed;
519 sddl_mask = sddl_flags_to_string(tmp_ctx, ace_access_mask,
520 ace->access_mask, true);
521 if (sddl_mask == NULL) {
522 sddl_mask = talloc_asprintf(tmp_ctx, "0x%08x",
523 ace->access_mask);
524 if (sddl_mask == NULL) {
525 goto failed;
529 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
530 ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT ||
531 ace->type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT ||
532 ace->type == SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT) {
533 if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
534 sddl_object = GUID_string(
535 tmp_ctx, &ace->object.object.type.type);
536 if (sddl_object == NULL) {
537 goto failed;
541 if (ace->object.object.flags & SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
542 sddl_iobject = GUID_string(tmp_ctx, &ace->object.object.inherited_type.inherited_type);
543 if (sddl_iobject == NULL) {
544 goto failed;
549 sddl_trustee = sddl_encode_sid(tmp_ctx, &ace->trustee, domain_sid);
550 if (sddl_trustee == NULL) {
551 goto failed;
554 sddl = talloc_asprintf(mem_ctx, "%s;%s;%s;%s;%s;%s",
555 sddl_type, sddl_flags, sddl_mask, sddl_object,
556 sddl_iobject, sddl_trustee);
558 failed:
559 talloc_free(tmp_ctx);
560 return sddl;
564 encode an ACL in SDDL format
566 static char *sddl_encode_acl(TALLOC_CTX *mem_ctx, const struct security_acl *acl,
567 uint32_t flags, const struct dom_sid *domain_sid)
569 char *sddl;
570 uint32_t i;
572 /* add any ACL flags */
573 sddl = sddl_flags_to_string(mem_ctx, acl_flags, flags, false);
574 if (sddl == NULL) goto failed;
576 /* now the ACEs, encoded in braces */
577 for (i=0;i<acl->num_aces;i++) {
578 char *ace = sddl_encode_ace(sddl, &acl->aces[i], domain_sid);
579 if (ace == NULL) goto failed;
580 sddl = talloc_asprintf_append_buffer(sddl, "(%s)", ace);
581 if (sddl == NULL) goto failed;
582 talloc_free(ace);
585 return sddl;
587 failed:
588 talloc_free(sddl);
589 return NULL;
594 encode a security descriptor to SDDL format
596 char *sddl_encode(TALLOC_CTX *mem_ctx, const struct security_descriptor *sd,
597 const struct dom_sid *domain_sid)
599 char *sddl;
600 TALLOC_CTX *tmp_ctx;
602 /* start with a blank string */
603 sddl = talloc_strdup(mem_ctx, "");
604 if (sddl == NULL) goto failed;
606 tmp_ctx = talloc_new(mem_ctx);
608 if (sd->owner_sid != NULL) {
609 char *sid = sddl_encode_sid(tmp_ctx, sd->owner_sid, domain_sid);
610 if (sid == NULL) goto failed;
611 sddl = talloc_asprintf_append_buffer(sddl, "O:%s", sid);
612 if (sddl == NULL) goto failed;
615 if (sd->group_sid != NULL) {
616 char *sid = sddl_encode_sid(tmp_ctx, sd->group_sid, domain_sid);
617 if (sid == NULL) goto failed;
618 sddl = talloc_asprintf_append_buffer(sddl, "G:%s", sid);
619 if (sddl == NULL) goto failed;
622 if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl != NULL) {
623 char *acl = sddl_encode_acl(tmp_ctx, sd->dacl, sd->type, domain_sid);
624 if (acl == NULL) goto failed;
625 sddl = talloc_asprintf_append_buffer(sddl, "D:%s", acl);
626 if (sddl == NULL) goto failed;
629 if ((sd->type & SEC_DESC_SACL_PRESENT) && sd->sacl != NULL) {
630 char *acl = sddl_encode_acl(tmp_ctx, sd->sacl, sd->type>>1, domain_sid);
631 if (acl == NULL) goto failed;
632 sddl = talloc_asprintf_append_buffer(sddl, "S:%s", acl);
633 if (sddl == NULL) goto failed;
636 talloc_free(tmp_ctx);
637 return sddl;
639 failed:
640 talloc_free(sddl);
641 return NULL;