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/>.
23 #include "lib/util/debug.h"
24 #include "libcli/security/security.h"
25 #include "librpc/gen_ndr/ndr_misc.h"
26 #include "lib/util/smb_strtox.h"
27 #include "system/locale.h"
29 struct sddl_transition_state {
30 const struct dom_sid *machine_sid;
31 const struct dom_sid *domain_sid;
32 const struct dom_sid *forest_sid;
40 static bool sddl_map_flag(
41 const struct flag_map *map,
46 while (map->name != NULL) {
47 size_t len = strlen(map->name);
48 int cmp = strncmp(map->name, str, len);
61 map a series of letter codes into a uint32_t
63 static bool sddl_map_flags(const struct flag_map *map, const char *str,
64 uint32_t *pflags, size_t *plen,
65 bool unknown_flag_is_part_of_next_thing)
67 const char *str0 = str;
72 while (str[0] != '\0' && isupper((unsigned char)str[0])) {
77 found = sddl_map_flag(map, str, &len, &flags);
89 * For ACL flags, unknown_flag_is_part_of_next_thing is set,
90 * and we expect some more stuff that isn't flags.
92 * For ACE flags, unknown_flag_is_part_of_next_thing is unset,
93 * and the flags have been tokenised into their own little
94 * string. We don't expect anything here, even whitespace.
96 if (*str == '\0' || unknown_flag_is_part_of_next_thing) {
99 DBG_WARNING("Unknown flag - '%s' in '%s'\n", str, str0);
105 a mapping between the 2 letter SID codes and sid strings
107 static const struct {
110 uint32_t machine_rid;
114 { .code = "WD", .sid = SID_WORLD },
116 { .code = "CO", .sid = SID_CREATOR_OWNER },
117 { .code = "CG", .sid = SID_CREATOR_GROUP },
118 { .code = "OW", .sid = SID_OWNER_RIGHTS },
120 { .code = "NU", .sid = SID_NT_NETWORK },
121 { .code = "IU", .sid = SID_NT_INTERACTIVE },
122 { .code = "SU", .sid = SID_NT_SERVICE },
123 { .code = "AN", .sid = SID_NT_ANONYMOUS },
124 { .code = "ED", .sid = SID_NT_ENTERPRISE_DCS },
125 { .code = "PS", .sid = SID_NT_SELF },
126 { .code = "AU", .sid = SID_NT_AUTHENTICATED_USERS },
127 { .code = "RC", .sid = SID_NT_RESTRICTED },
128 { .code = "SY", .sid = SID_NT_SYSTEM },
129 { .code = "LS", .sid = SID_NT_LOCAL_SERVICE },
130 { .code = "NS", .sid = SID_NT_NETWORK_SERVICE },
131 { .code = "WR", .sid = SID_SECURITY_RESTRICTED_CODE },
133 { .code = "BA", .sid = SID_BUILTIN_ADMINISTRATORS },
134 { .code = "BU", .sid = SID_BUILTIN_USERS },
135 { .code = "BG", .sid = SID_BUILTIN_GUESTS },
136 { .code = "PU", .sid = SID_BUILTIN_POWER_USERS },
137 { .code = "AO", .sid = SID_BUILTIN_ACCOUNT_OPERATORS },
138 { .code = "SO", .sid = SID_BUILTIN_SERVER_OPERATORS },
139 { .code = "PO", .sid = SID_BUILTIN_PRINT_OPERATORS },
140 { .code = "BO", .sid = SID_BUILTIN_BACKUP_OPERATORS },
141 { .code = "RE", .sid = SID_BUILTIN_REPLICATOR },
142 { .code = "RU", .sid = SID_BUILTIN_PREW2K },
143 { .code = "RD", .sid = SID_BUILTIN_REMOTE_DESKTOP_USERS },
144 { .code = "NO", .sid = SID_BUILTIN_NETWORK_CONF_OPERATORS },
146 { .code = "MU", .sid = SID_BUILTIN_PERFMON_USERS },
147 { .code = "LU", .sid = SID_BUILTIN_PERFLOG_USERS },
148 { .code = "IS", .sid = SID_BUILTIN_IUSERS },
149 { .code = "CY", .sid = SID_BUILTIN_CRYPTO_OPERATORS },
150 { .code = "ER", .sid = SID_BUILTIN_EVENT_LOG_READERS },
151 { .code = "CD", .sid = SID_BUILTIN_CERT_SERV_DCOM_ACCESS },
152 { .code = "RA", .sid = SID_BUILTIN_RDS_REMOTE_ACCESS_SERVERS },
153 { .code = "ES", .sid = SID_BUILTIN_RDS_ENDPOINT_SERVERS },
154 { .code = "MS", .sid = SID_BUILTIN_RDS_MANAGEMENT_SERVERS },
155 { .code = "HA", .sid = SID_BUILTIN_HYPER_V_ADMINS },
156 { .code = "AA", .sid = SID_BUILTIN_ACCESS_CONTROL_ASSISTANCE_OPS },
157 { .code = "RM", .sid = SID_BUILTIN_REMOTE_MANAGEMENT_USERS },
159 { .code = "UD", .sid = SID_USER_MODE_DRIVERS },
161 { .code = "AC", .sid = SID_SECURITY_BUILTIN_PACKAGE_ANY_PACKAGE },
163 { .code = "LW", .sid = SID_SECURITY_MANDATORY_LOW },
164 { .code = "ME", .sid = SID_SECURITY_MANDATORY_MEDIUM },
165 { .code = "MP", .sid = SID_SECURITY_MANDATORY_MEDIUM_PLUS },
166 { .code = "HI", .sid = SID_SECURITY_MANDATORY_HIGH },
167 { .code = "SI", .sid = SID_SECURITY_MANDATORY_SYSTEM },
169 { .code = "AS", .sid = SID_AUTHENTICATION_AUTHORITY_ASSERTED_IDENTITY },
170 { .code = "SS", .sid = SID_SERVICE_ASSERTED_IDENTITY },
172 { .code = "RO", .forest_rid = DOMAIN_RID_ENTERPRISE_READONLY_DCS },
174 { .code = "LA", .machine_rid = DOMAIN_RID_ADMINISTRATOR },
175 { .code = "LG", .machine_rid = DOMAIN_RID_GUEST },
177 { .code = "DA", .domain_rid = DOMAIN_RID_ADMINS },
178 { .code = "DU", .domain_rid = DOMAIN_RID_USERS },
179 { .code = "DG", .domain_rid = DOMAIN_RID_GUESTS },
180 { .code = "DC", .domain_rid = DOMAIN_RID_DOMAIN_MEMBERS },
181 { .code = "DD", .domain_rid = DOMAIN_RID_DCS },
182 { .code = "CA", .domain_rid = DOMAIN_RID_CERT_ADMINS },
183 { .code = "SA", .forest_rid = DOMAIN_RID_SCHEMA_ADMINS },
184 { .code = "EA", .forest_rid = DOMAIN_RID_ENTERPRISE_ADMINS },
185 { .code = "PA", .domain_rid = DOMAIN_RID_POLICY_ADMINS },
187 { .code = "CN", .domain_rid = DOMAIN_RID_CLONEABLE_CONTROLLERS },
189 { .code = "AP", .domain_rid = DOMAIN_RID_PROTECTED_USERS },
190 { .code = "KA", .domain_rid = DOMAIN_RID_KEY_ADMINS },
191 { .code = "EK", .forest_rid = DOMAIN_RID_ENTERPRISE_KEY_ADMINS },
193 { .code = "RS", .domain_rid = DOMAIN_RID_RAS_SERVERS }
198 It can either be a special 2 letter code, or in S-* format
200 static struct dom_sid *sddl_decode_sid(TALLOC_CTX *mem_ctx, const char **sddlp,
201 struct sddl_transition_state *state)
203 const char *sddl = (*sddlp);
206 /* see if its in the numeric format */
207 if (strncmp(sddl, "S-", 2) == 0) {
208 struct dom_sid *sid = NULL;
209 char *sid_str = NULL;
210 const char *end = NULL;
212 size_t len = strspn(sddl + 2, "-0123456789ABCDEFabcdefxX") + 2;
213 if (len < 5) { /* S-1-x */
216 if (sddl[len - 1] == 'D' && sddl[len] == ':') {
218 * we have run into the "D:" dacl marker, mistaking it
219 * for a hex digit. There is no other way for this
220 * pair to occur at the end of a SID in SDDL.
225 sid_str = talloc_strndup(mem_ctx, sddl, len);
226 if (sid_str == NULL) {
229 sid = talloc(mem_ctx, struct dom_sid);
231 TALLOC_FREE(sid_str);
234 ok = dom_sid_parse_endp(sid_str, sid, &end);
236 DBG_WARNING("could not parse SID '%s'\n", sid_str);
237 TALLOC_FREE(sid_str);
241 if (end - sid_str != len) {
242 DBG_WARNING("trailing junk after SID '%s'\n", sid_str);
243 TALLOC_FREE(sid_str);
247 TALLOC_FREE(sid_str);
252 /* now check for one of the special codes */
253 for (i=0;i<ARRAY_SIZE(sid_codes);i++) {
254 if (strncmp(sid_codes[i].code, sddl, 2) == 0) break;
256 if (i == ARRAY_SIZE(sid_codes)) {
257 DEBUG(1,("Unknown sddl sid code '%2.2s'\n", sddl));
264 if (sid_codes[i].machine_rid != 0) {
265 return dom_sid_add_rid(mem_ctx, state->machine_sid,
266 sid_codes[i].machine_rid);
269 if (sid_codes[i].domain_rid != 0) {
270 return dom_sid_add_rid(mem_ctx, state->domain_sid,
271 sid_codes[i].domain_rid);
274 if (sid_codes[i].forest_rid != 0) {
275 return dom_sid_add_rid(mem_ctx, state->forest_sid,
276 sid_codes[i].forest_rid);
279 return dom_sid_parse_talloc(mem_ctx, sid_codes[i].sid);
282 static const struct flag_map ace_types[] = {
283 { "AU", SEC_ACE_TYPE_SYSTEM_AUDIT },
284 { "AL", SEC_ACE_TYPE_SYSTEM_ALARM },
285 { "OA", SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT },
286 { "OD", SEC_ACE_TYPE_ACCESS_DENIED_OBJECT },
287 { "OU", SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT },
288 { "OL", SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT },
289 { "A", SEC_ACE_TYPE_ACCESS_ALLOWED },
290 { "D", SEC_ACE_TYPE_ACCESS_DENIED },
294 static const struct flag_map ace_flags[] = {
295 { "OI", SEC_ACE_FLAG_OBJECT_INHERIT },
296 { "CI", SEC_ACE_FLAG_CONTAINER_INHERIT },
297 { "NP", SEC_ACE_FLAG_NO_PROPAGATE_INHERIT },
298 { "IO", SEC_ACE_FLAG_INHERIT_ONLY },
299 { "ID", SEC_ACE_FLAG_INHERITED_ACE },
300 { "SA", SEC_ACE_FLAG_SUCCESSFUL_ACCESS },
301 { "FA", SEC_ACE_FLAG_FAILED_ACCESS },
305 static const struct flag_map ace_access_mask[] = {
306 { "CC", SEC_ADS_CREATE_CHILD },
307 { "DC", SEC_ADS_DELETE_CHILD },
308 { "LC", SEC_ADS_LIST },
309 { "SW", SEC_ADS_SELF_WRITE },
310 { "RP", SEC_ADS_READ_PROP },
311 { "WP", SEC_ADS_WRITE_PROP },
312 { "DT", SEC_ADS_DELETE_TREE },
313 { "LO", SEC_ADS_LIST_OBJECT },
314 { "CR", SEC_ADS_CONTROL_ACCESS },
315 { "SD", SEC_STD_DELETE },
316 { "RC", SEC_STD_READ_CONTROL },
317 { "WD", SEC_STD_WRITE_DAC },
318 { "WO", SEC_STD_WRITE_OWNER },
319 { "GA", SEC_GENERIC_ALL },
320 { "GX", SEC_GENERIC_EXECUTE },
321 { "GW", SEC_GENERIC_WRITE },
322 { "GR", SEC_GENERIC_READ },
326 static const struct flag_map decode_ace_access_mask[] = {
327 { "FA", FILE_ALL_ACCESS },
328 { "FR", FILE_GENERIC_READ },
329 { "FW", FILE_GENERIC_WRITE },
330 { "FX", FILE_GENERIC_EXECUTE },
334 static bool sddl_decode_access(const char *str, uint32_t *pmask)
336 const char *str0 = str;
339 unsigned long long numeric_mask;
342 * The access mask can be a number or a series of flags.
344 * Canonically the number is expressed in hexadecimal (with 0x), but
345 * per MS-DTYP and Windows behaviour, octal and decimal numbers are
348 * Windows has two behaviours we choose not to replicate:
350 * 1. numbers exceeding 0xffffffff are truncated at that point,
351 * turning on all access flags.
353 * 2. negative numbers are accepted, so e.g. -2 becomes 0xfffffffe.
355 numeric_mask = smb_strtoull(str, &end, 0, &err, SMB_STR_STANDARD);
357 if (numeric_mask > UINT32_MAX) {
358 DBG_WARNING("Bad numeric flag value - %llu in %s\n",
362 if (end - str > sizeof("037777777777")) {
363 /* here's the tricky thing: if a number is big
364 * enough to overflow the uint64, it might end
365 * up small enough to fit in the uint32, and
366 * we'd miss that it overflowed. So we count
367 * the digits -- any more than 12 (for
368 * "037777777777") is too long for 32 bits,
369 * and the shortest 64-bit wrapping string is
370 * 19 (for "0x1" + 16 zeros).
372 DBG_WARNING("Bad numeric flag value in %s\n", str0);
375 *pmask = numeric_mask;
378 /* It's not a positive number, so we'll look for flags */
380 while ((str[0] != '\0') && isupper(str[0])) {
385 found = sddl_map_flag(
386 ace_access_mask, str, &len, &flags);
387 found |= sddl_map_flag(
388 decode_ace_access_mask, str, &len, &flags);
390 DEBUG(1, ("Unknown flag - %s in %s\n", str, str0));
403 return true on success, false on failure
404 note that this routine modifies the string
406 static bool sddl_decode_ace(TALLOC_CTX *mem_ctx, struct security_ace *ace, char *str,
407 struct sddl_transition_state *state)
419 /* parse out the 6 tokens */
422 char *ptr = strchr(str, ';');
423 if (ptr == NULL) return false;
430 ok = sddl_map_flag(ace_types, tok[0], &len, &v);
432 DBG_WARNING("Unknown ACE type - %s\n", tok[0]);
435 if (tok[0][len] != '\0') {
436 DBG_WARNING("Garbage after ACE type - %s\n", tok[0]);
443 if (!sddl_map_flags(ace_flags, tok[1], &v, NULL, false)) {
449 ok = sddl_decode_access(tok[2], &ace->access_mask);
455 if (tok[3][0] != 0) {
456 NTSTATUS status = GUID_from_string(tok[3],
457 &ace->object.object.type.type);
458 if (!NT_STATUS_IS_OK(status)) {
461 ace->object.object.flags |= SEC_ACE_OBJECT_TYPE_PRESENT;
465 if (tok[4][0] != 0) {
466 NTSTATUS status = GUID_from_string(tok[4],
467 &ace->object.object.inherited_type.inherited_type);
468 if (!NT_STATUS_IS_OK(status)) {
471 ace->object.object.flags |= SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT;
476 sid = sddl_decode_sid(mem_ctx, &s, state);
486 static const struct flag_map acl_flags[] = {
487 { "P", SEC_DESC_DACL_PROTECTED },
488 { "AR", SEC_DESC_DACL_AUTO_INHERIT_REQ },
489 { "AI", SEC_DESC_DACL_AUTO_INHERITED },
496 static struct security_acl *sddl_decode_acl(struct security_descriptor *sd,
497 const char **sddlp, uint32_t *flags,
498 struct sddl_transition_state *state)
500 const char *sddl = *sddlp;
501 struct security_acl *acl;
506 acl = talloc_zero(sd, struct security_acl);
507 if (acl == NULL) return NULL;
508 acl->revision = SECURITY_ACL_REVISION_ADS;
510 if (isupper(sddl[0]) && sddl[1] == ':') {
511 /* its an empty ACL */
515 /* work out the ACL flags */
516 if (!sddl_map_flags(acl_flags, sddl, flags, &len, true)) {
523 while (*sddl == '(') {
525 len = strcspn(sddl+1, ")");
526 astr = talloc_strndup(acl, sddl+1, len);
527 if (astr == NULL || sddl[len+1] != ')') {
531 acl->aces = talloc_realloc(acl, acl->aces, struct security_ace,
533 if (acl->aces == NULL) {
537 if (!sddl_decode_ace(acl->aces, &acl->aces[acl->num_aces],
542 switch (acl->aces[acl->num_aces].type) {
543 case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
544 case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
545 case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
546 case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
547 acl->revision = SECURITY_ACL_REVISION_ADS;
562 decode a security descriptor in SDDL format
564 struct security_descriptor *sddl_decode(TALLOC_CTX *mem_ctx, const char *sddl,
565 const struct dom_sid *domain_sid)
567 struct sddl_transition_state state = {
569 * TODO: verify .machine_rid values really belong to
570 * to the machine_sid on a member, once
571 * we pass machine_sid from the caller...
573 .machine_sid = domain_sid,
574 .domain_sid = domain_sid,
575 .forest_sid = domain_sid,
577 struct security_descriptor *sd;
578 sd = talloc_zero(mem_ctx, struct security_descriptor);
580 sd->revision = SECURITY_DESCRIPTOR_REVISION_1;
581 sd->type = SEC_DESC_SELF_RELATIVE;
586 if (sddl[1] != ':') goto failed;
591 if (sd->dacl != NULL) goto failed;
592 sd->dacl = sddl_decode_acl(sd, &sddl, &flags, &state);
593 if (sd->dacl == NULL) goto failed;
594 sd->type |= flags | SEC_DESC_DACL_PRESENT;
597 if (sd->sacl != NULL) goto failed;
598 sd->sacl = sddl_decode_acl(sd, &sddl, &flags, &state);
599 if (sd->sacl == NULL) goto failed;
600 /* this relies on the SEC_DESC_SACL_* flags being
601 1 bit shifted from the SEC_DESC_DACL_* flags */
602 sd->type |= (flags<<1) | SEC_DESC_SACL_PRESENT;
605 if (sd->owner_sid != NULL) goto failed;
606 sd->owner_sid = sddl_decode_sid(sd, &sddl, &state);
607 if (sd->owner_sid == NULL) goto failed;
610 if (sd->group_sid != NULL) goto failed;
611 sd->group_sid = sddl_decode_sid(sd, &sddl, &state);
612 if (sd->group_sid == NULL) goto failed;
622 DEBUG(2,("Badly formatted SDDL '%s'\n", sddl));
628 turn a set of flags into a string
630 static char *sddl_flags_to_string(TALLOC_CTX *mem_ctx, const struct flag_map *map,
631 uint32_t flags, bool check_all)
636 /* try to find an exact match */
637 for (i=0;map[i].name;i++) {
638 if (map[i].flag == flags) {
639 return talloc_strdup(mem_ctx, map[i].name);
643 s = talloc_strdup(mem_ctx, "");
646 for (i=0;map[i].name;i++) {
647 if ((flags & map[i].flag) != 0) {
648 s = talloc_asprintf_append_buffer(s, "%s", map[i].name);
649 if (s == NULL) goto failed;
650 flags &= ~map[i].flag;
654 if (check_all && flags != 0) {
666 encode a sid in SDDL format
668 static char *sddl_encode_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
669 struct sddl_transition_state *state)
671 bool in_machine = dom_sid_in_domain(state->machine_sid, sid);
672 bool in_domain = dom_sid_in_domain(state->domain_sid, sid);
673 bool in_forest = dom_sid_in_domain(state->forest_sid, sid);
674 struct dom_sid_buf buf;
675 const char *sidstr = dom_sid_str_buf(sid, &buf);
679 if (sid->num_auths > 1) {
680 rid = sid->sub_auths[sid->num_auths-1];
683 for (i=0;i<ARRAY_SIZE(sid_codes);i++) {
684 /* seen if its a well known sid */
685 if (sid_codes[i].sid != NULL) {
688 cmp = strcmp(sidstr, sid_codes[i].sid);
693 return talloc_strdup(mem_ctx, sid_codes[i].code);
700 if (in_machine && sid_codes[i].machine_rid == rid) {
701 return talloc_strdup(mem_ctx, sid_codes[i].code);
703 if (in_domain && sid_codes[i].domain_rid == rid) {
704 return talloc_strdup(mem_ctx, sid_codes[i].code);
706 if (in_forest && sid_codes[i].forest_rid == rid) {
707 return talloc_strdup(mem_ctx, sid_codes[i].code);
711 return talloc_strdup(mem_ctx, sidstr);
716 encode an ACE in SDDL format
718 static char *sddl_transition_encode_ace(TALLOC_CTX *mem_ctx, const struct security_ace *ace,
719 struct sddl_transition_state *state)
723 struct GUID_txt_buf object_buf, iobject_buf;
724 const char *sddl_type="", *sddl_flags="", *sddl_mask="",
725 *sddl_object="", *sddl_iobject="", *sddl_trustee="";
727 tmp_ctx = talloc_new(mem_ctx);
728 if (tmp_ctx == NULL) {
729 DEBUG(0, ("talloc_new failed\n"));
733 sddl_type = sddl_flags_to_string(tmp_ctx, ace_types, ace->type, true);
734 if (sddl_type == NULL) {
738 sddl_flags = sddl_flags_to_string(tmp_ctx, ace_flags, ace->flags,
740 if (sddl_flags == NULL) {
744 sddl_mask = sddl_flags_to_string(tmp_ctx, ace_access_mask,
745 ace->access_mask, true);
746 if (sddl_mask == NULL) {
747 sddl_mask = talloc_asprintf(tmp_ctx, "0x%x",
749 if (sddl_mask == NULL) {
754 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
755 ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT ||
756 ace->type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT ||
757 ace->type == SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT) {
758 const struct security_ace_object *object = &ace->object.object;
760 if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
761 sddl_object = GUID_buf_string(
762 &object->type.type, &object_buf);
765 if (ace->object.object.flags &
766 SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
767 sddl_iobject = GUID_buf_string(
768 &object->inherited_type.inherited_type,
773 sddl_trustee = sddl_encode_sid(tmp_ctx, &ace->trustee, state);
774 if (sddl_trustee == NULL) {
778 sddl = talloc_asprintf(mem_ctx, "%s;%s;%s;%s;%s;%s",
779 sddl_type, sddl_flags, sddl_mask, sddl_object,
780 sddl_iobject, sddl_trustee);
783 talloc_free(tmp_ctx);
787 char *sddl_encode_ace(TALLOC_CTX *mem_ctx, const struct security_ace *ace,
788 const struct dom_sid *domain_sid)
790 struct sddl_transition_state state = {
792 * TODO: verify .machine_rid values really belong to
793 * to the machine_sid on a member, once
794 * we pass machine_sid from the caller...
796 .machine_sid = domain_sid,
797 .domain_sid = domain_sid,
798 .forest_sid = domain_sid,
800 return sddl_transition_encode_ace(mem_ctx, ace, &state);
804 encode an ACL in SDDL format
806 static char *sddl_encode_acl(TALLOC_CTX *mem_ctx, const struct security_acl *acl,
807 uint32_t flags, struct sddl_transition_state *state)
812 /* add any ACL flags */
813 sddl = sddl_flags_to_string(mem_ctx, acl_flags, flags, false);
814 if (sddl == NULL) goto failed;
816 /* now the ACEs, encoded in braces */
817 for (i=0;i<acl->num_aces;i++) {
818 char *ace = sddl_transition_encode_ace(sddl, &acl->aces[i], state);
819 if (ace == NULL) goto failed;
820 sddl = talloc_asprintf_append_buffer(sddl, "(%s)", ace);
821 if (sddl == NULL) goto failed;
834 encode a security descriptor to SDDL format
836 char *sddl_encode(TALLOC_CTX *mem_ctx, const struct security_descriptor *sd,
837 const struct dom_sid *domain_sid)
839 struct sddl_transition_state state = {
841 * TODO: verify .machine_rid values really belong to
842 * to the machine_sid on a member, once
843 * we pass machine_sid from the caller...
845 .machine_sid = domain_sid,
846 .domain_sid = domain_sid,
847 .forest_sid = domain_sid,
852 /* start with a blank string */
853 sddl = talloc_strdup(mem_ctx, "");
854 if (sddl == NULL) goto failed;
856 tmp_ctx = talloc_new(mem_ctx);
858 if (sd->owner_sid != NULL) {
859 char *sid = sddl_encode_sid(tmp_ctx, sd->owner_sid, &state);
860 if (sid == NULL) goto failed;
861 sddl = talloc_asprintf_append_buffer(sddl, "O:%s", sid);
862 if (sddl == NULL) goto failed;
865 if (sd->group_sid != NULL) {
866 char *sid = sddl_encode_sid(tmp_ctx, sd->group_sid, &state);
867 if (sid == NULL) goto failed;
868 sddl = talloc_asprintf_append_buffer(sddl, "G:%s", sid);
869 if (sddl == NULL) goto failed;
872 if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl != NULL) {
873 char *acl = sddl_encode_acl(tmp_ctx, sd->dacl, sd->type, &state);
874 if (acl == NULL) goto failed;
875 sddl = talloc_asprintf_append_buffer(sddl, "D:%s", acl);
876 if (sddl == NULL) goto failed;
879 if ((sd->type & SEC_DESC_SACL_PRESENT) && sd->sacl != NULL) {
880 char *acl = sddl_encode_acl(tmp_ctx, sd->sacl, sd->type>>1, &state);
881 if (acl == NULL) goto failed;
882 sddl = talloc_asprintf_append_buffer(sddl, "S:%s", acl);
883 if (sddl == NULL) goto failed;
886 talloc_free(tmp_ctx);