libcli:security: sddl_map_flags rejects trailing nonsense
[samba.git] / libcli / security / sddl.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    security descriptor description language functions
5
6    Copyright (C) Andrew Tridgell                2005
7
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.
12
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.
17
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/>.
20 */
21
22 #include "replace.h"
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"
28
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;
33 };
34
35 struct flag_map {
36         const char *name;
37         uint32_t flag;
38 };
39
40 static bool sddl_map_flag(
41         const struct flag_map *map,
42         const char *str,
43         size_t *plen,
44         uint32_t *pflag)
45 {
46         while (map->name != NULL) {
47                 size_t len = strlen(map->name);
48                 int cmp = strncmp(map->name, str, len);
49
50                 if (cmp == 0) {
51                         *plen = len;
52                         *pflag = map->flag;
53                         return true;
54                 }
55                 map += 1;
56         }
57         return false;
58 }
59
60 /*
61   map a series of letter codes into a uint32_t
62 */
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)
66 {
67         const char *str0 = str;
68         if (plen != NULL) {
69                 *plen = 0;
70         }
71         *pflags = 0;
72         while (str[0] != '\0' && isupper((unsigned char)str[0])) {
73                 size_t len;
74                 uint32_t flags;
75                 bool found;
76
77                 found = sddl_map_flag(map, str, &len, &flags);
78                 if (!found) {
79                         break;
80                 }
81
82                 *pflags |= flags;
83                 if (plen != NULL) {
84                         *plen += len;
85                 }
86                 str += len;
87         }
88         /*
89          * For ACL flags, unknown_flag_is_part_of_next_thing is set,
90          * and we expect some more stuff that isn't flags.
91          *
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.
95          */
96         if (*str == '\0' || unknown_flag_is_part_of_next_thing) {
97                 return true;
98         }
99         DBG_WARNING("Unknown flag - '%s' in '%s'\n", str, str0);
100         return false;
101 }
102
103
104 /*
105   a mapping between the 2 letter SID codes and sid strings
106 */
107 static const struct {
108         const char *code;
109         const char *sid;
110         uint32_t machine_rid;
111         uint32_t domain_rid;
112         uint32_t forest_rid;
113 } sid_codes[] = {
114         { .code = "WD", .sid = SID_WORLD },
115
116         { .code = "CO", .sid = SID_CREATOR_OWNER },
117         { .code = "CG", .sid = SID_CREATOR_GROUP },
118         { .code = "OW", .sid = SID_OWNER_RIGHTS },
119
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 },
132
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 },
145
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 },
158
159         { .code = "UD", .sid = SID_USER_MODE_DRIVERS },
160
161         { .code = "AC", .sid = SID_SECURITY_BUILTIN_PACKAGE_ANY_PACKAGE },
162
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 },
168
169         { .code = "AS", .sid = SID_AUTHENTICATION_AUTHORITY_ASSERTED_IDENTITY },
170         { .code = "SS", .sid = SID_SERVICE_ASSERTED_IDENTITY },
171
172         { .code = "RO", .forest_rid = DOMAIN_RID_ENTERPRISE_READONLY_DCS },
173
174         { .code = "LA", .machine_rid = DOMAIN_RID_ADMINISTRATOR },
175         { .code = "LG", .machine_rid = DOMAIN_RID_GUEST },
176
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 },
186
187         { .code = "CN", .domain_rid = DOMAIN_RID_CLONEABLE_CONTROLLERS },
188
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 },
192
193         { .code = "RS", .domain_rid = DOMAIN_RID_RAS_SERVERS }
194 };
195
196 /*
197   decode a SID
198   It can either be a special 2 letter code, or in S-* format
199 */
200 static struct dom_sid *sddl_decode_sid(TALLOC_CTX *mem_ctx, const char **sddlp,
201                                        struct sddl_transition_state *state)
202 {
203         const char *sddl = (*sddlp);
204         size_t i;
205
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;
211                 bool ok;
212                 size_t len = strspn(sddl + 2, "-0123456789ABCDEFabcdefxX") + 2;
213                 if (len < 5) { /* S-1-x */
214                         return NULL;
215                 }
216                 if (sddl[len - 1] == 'D' && sddl[len] == ':') {
217                         /*
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.
221                          */
222                         len--;
223                 }
224
225                 sid_str = talloc_strndup(mem_ctx, sddl, len);
226                 if (sid_str == NULL) {
227                         return NULL;
228                 }
229                 sid = talloc(mem_ctx, struct dom_sid);
230                 if (sid == NULL) {
231                         TALLOC_FREE(sid_str);
232                         return NULL;
233                 };
234                 ok = dom_sid_parse_endp(sid_str, sid, &end);
235                 if (!ok) {
236                         DBG_WARNING("could not parse SID '%s'\n", sid_str);
237                         TALLOC_FREE(sid_str);
238                         TALLOC_FREE(sid);
239                         return NULL;
240                 }
241                 if (end - sid_str != len) {
242                         DBG_WARNING("trailing junk after SID '%s'\n", sid_str);
243                         TALLOC_FREE(sid_str);
244                         TALLOC_FREE(sid);
245                         return NULL;
246                 }
247                 TALLOC_FREE(sid_str);
248                 (*sddlp) += len;
249                 return sid;
250         }
251
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;
255         }
256         if (i == ARRAY_SIZE(sid_codes)) {
257                 DEBUG(1,("Unknown sddl sid code '%2.2s'\n", sddl));
258                 return NULL;
259         }
260
261         (*sddlp) += 2;
262
263
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);
267         }
268
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);
272         }
273
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);
277         }
278
279         return dom_sid_parse_talloc(mem_ctx, sid_codes[i].sid);
280 }
281
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 },
291         { NULL, 0 }
292 };
293
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 },
302         { NULL, 0 },
303 };
304
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 },
323         { NULL, 0 }
324 };
325
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 },
331         { NULL, 0 },
332 };
333
334 static bool sddl_decode_access(const char *str, uint32_t *pmask)
335 {
336         const char *str0 = str;
337         char *end = NULL;
338         uint32_t mask = 0;
339         unsigned long long numeric_mask;
340         int err;
341         /*
342          * The access mask can be a number or a series of flags.
343          *
344          * Canonically the number is expressed in hexadecimal (with 0x), but
345          * per MS-DTYP and Windows behaviour, octal and decimal numbers are
346          * also accepted.
347          *
348          * Windows has two behaviours we choose not to replicate:
349          *
350          * 1. numbers exceeding 0xffffffff are truncated at that point,
351          *    turning on all access flags.
352          *
353          * 2. negative numbers are accepted, so e.g. -2 becomes 0xfffffffe.
354          */
355         numeric_mask = smb_strtoull(str, &end, 0, &err, SMB_STR_STANDARD);
356         if (err == 0) {
357                 if (numeric_mask > UINT32_MAX) {
358                         DBG_WARNING("Bad numeric flag value - %llu in %s\n",
359                                     numeric_mask, str0);
360                         return false;
361                 }
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).
371                          */
372                         DBG_WARNING("Bad numeric flag value in %s\n", str0);
373                         return false;
374                 }
375                 *pmask = numeric_mask;
376                 return true;
377         }
378         /* It's not a positive number, so we'll look for flags */
379
380         while ((str[0] != '\0') && isupper(str[0])) {
381                 uint32_t flags = 0;
382                 size_t len = 0;
383                 bool found;
384
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);
389                 if (!found) {
390                         DEBUG(1, ("Unknown flag - %s in %s\n", str, str0));
391                         return false;
392                 }
393                 mask |= flags;
394                 str += len;
395         }
396
397         *pmask = mask;
398         return true;
399 }
400
401 /*
402   decode an ACE
403   return true on success, false on failure
404   note that this routine modifies the string
405 */
406 static bool sddl_decode_ace(TALLOC_CTX *mem_ctx, struct security_ace *ace, char *str,
407                             struct sddl_transition_state *state)
408 {
409         const char *tok[6];
410         const char *s;
411         int i;
412         uint32_t v;
413         struct dom_sid *sid;
414         bool ok;
415         size_t len;
416
417         ZERO_STRUCTP(ace);
418
419         /* parse out the 6 tokens */
420         tok[0] = str;
421         for (i=0;i<5;i++) {
422                 char *ptr = strchr(str, ';');
423                 if (ptr == NULL) return false;
424                 *ptr = 0;
425                 str = ptr+1;
426                 tok[i+1] = str;
427         }
428
429         /* parse ace type */
430         ok = sddl_map_flag(ace_types, tok[0], &len, &v);
431         if (!ok) {
432                 DBG_WARNING("Unknown ACE type - %s\n", tok[0]);
433                 return false;
434         }
435         if (tok[0][len] != '\0') {
436                 DBG_WARNING("Garbage after ACE type - %s\n", tok[0]);
437                 return false;
438         }
439
440         ace->type = v;
441
442         /* ace flags */
443         if (!sddl_map_flags(ace_flags, tok[1], &v, NULL, false)) {
444                 return false;
445         }
446         ace->flags = v;
447
448         /* access mask */
449         ok = sddl_decode_access(tok[2], &ace->access_mask);
450         if (!ok) {
451                 return false;
452         }
453
454         /* object */
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)) {
459                         return false;
460                 }
461                 ace->object.object.flags |= SEC_ACE_OBJECT_TYPE_PRESENT;
462         }
463
464         /* inherit object */
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)) {
469                         return false;
470                 }
471                 ace->object.object.flags |= SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT;
472         }
473
474         /* trustee */
475         s = tok[5];
476         sid = sddl_decode_sid(mem_ctx, &s, state);
477         if (sid == NULL) {
478                 return false;
479         }
480         ace->trustee = *sid;
481         talloc_free(sid);
482
483         return true;
484 }
485
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 },
490         { NULL, 0 }
491 };
492
493 /*
494   decode an ACL
495 */
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)
499 {
500         const char *sddl = *sddlp;
501         struct security_acl *acl;
502         size_t len;
503
504         *flags = 0;
505
506         acl = talloc_zero(sd, struct security_acl);
507         if (acl == NULL) return NULL;
508         acl->revision = SECURITY_ACL_REVISION_ADS;
509
510         if (isupper(sddl[0]) && sddl[1] == ':') {
511                 /* its an empty ACL */
512                 return acl;
513         }
514
515         /* work out the ACL flags */
516         if (!sddl_map_flags(acl_flags, sddl, flags, &len, true)) {
517                 talloc_free(acl);
518                 return NULL;
519         }
520         sddl += len;
521
522         /* now the ACEs */
523         while (*sddl == '(') {
524                 char *astr;
525                 len = strcspn(sddl+1, ")");
526                 astr = talloc_strndup(acl, sddl+1, len);
527                 if (astr == NULL || sddl[len+1] != ')') {
528                         talloc_free(acl);
529                         return NULL;
530                 }
531                 acl->aces = talloc_realloc(acl, acl->aces, struct security_ace,
532                                            acl->num_aces+1);
533                 if (acl->aces == NULL) {
534                         talloc_free(acl);
535                         return NULL;
536                 }
537                 if (!sddl_decode_ace(acl->aces, &acl->aces[acl->num_aces],
538                                      astr, state)) {
539                         talloc_free(acl);
540                         return NULL;
541                 }
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;
548                         break;
549                 default:
550                         break;
551                 }
552                 talloc_free(astr);
553                 sddl += len+2;
554                 acl->num_aces++;
555         }
556
557         (*sddlp) = sddl;
558         return acl;
559 }
560
561 /*
562   decode a security descriptor in SDDL format
563 */
564 struct security_descriptor *sddl_decode(TALLOC_CTX *mem_ctx, const char *sddl,
565                                         const struct dom_sid *domain_sid)
566 {
567         struct sddl_transition_state state = {
568                 /*
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...
572                  */
573                 .machine_sid = domain_sid,
574                 .domain_sid = domain_sid,
575                 .forest_sid = domain_sid,
576         };
577         struct security_descriptor *sd;
578         sd = talloc_zero(mem_ctx, struct security_descriptor);
579
580         sd->revision = SECURITY_DESCRIPTOR_REVISION_1;
581         sd->type     = SEC_DESC_SELF_RELATIVE;
582
583         while (*sddl) {
584                 uint32_t flags;
585                 char c = sddl[0];
586                 if (sddl[1] != ':') goto failed;
587
588                 sddl += 2;
589                 switch (c) {
590                 case 'D':
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;
595                         break;
596                 case 'S':
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;
603                         break;
604                 case 'O':
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;
608                         break;
609                 case 'G':
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;
613                         break;
614                 default:
615                         goto failed;
616                 }
617         }
618
619         return sd;
620
621 failed:
622         DEBUG(2,("Badly formatted SDDL '%s'\n", sddl));
623         talloc_free(sd);
624         return NULL;
625 }
626
627 /*
628   turn a set of flags into a string
629 */
630 static char *sddl_flags_to_string(TALLOC_CTX *mem_ctx, const struct flag_map *map,
631                                   uint32_t flags, bool check_all)
632 {
633         int i;
634         char *s;
635
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);
640                 }
641         }
642
643         s = talloc_strdup(mem_ctx, "");
644
645         /* now by bits */
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;
651                 }
652         }
653
654         if (check_all && flags != 0) {
655                 goto failed;
656         }
657
658         return s;
659
660 failed:
661         talloc_free(s);
662         return NULL;
663 }
664
665 /*
666   encode a sid in SDDL format
667 */
668 static char *sddl_encode_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
669                              struct sddl_transition_state *state)
670 {
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);
676         uint32_t rid = 0;
677         size_t i;
678
679         if (sid->num_auths > 1) {
680                 rid = sid->sub_auths[sid->num_auths-1];
681         }
682
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) {
686                         int cmp;
687
688                         cmp = strcmp(sidstr, sid_codes[i].sid);
689                         if (cmp != 0) {
690                                 continue;
691                         }
692
693                         return talloc_strdup(mem_ctx, sid_codes[i].code);
694                 }
695
696                 if (rid == 0) {
697                         continue;
698                 }
699
700                 if (in_machine && sid_codes[i].machine_rid == rid) {
701                         return talloc_strdup(mem_ctx, sid_codes[i].code);
702                 }
703                 if (in_domain && sid_codes[i].domain_rid == rid) {
704                         return talloc_strdup(mem_ctx, sid_codes[i].code);
705                 }
706                 if (in_forest && sid_codes[i].forest_rid == rid) {
707                         return talloc_strdup(mem_ctx, sid_codes[i].code);
708                 }
709         }
710
711         return talloc_strdup(mem_ctx, sidstr);
712 }
713
714
715 /*
716   encode an ACE in SDDL format
717 */
718 static char *sddl_transition_encode_ace(TALLOC_CTX *mem_ctx, const struct security_ace *ace,
719                                         struct sddl_transition_state *state)
720 {
721         char *sddl = NULL;
722         TALLOC_CTX *tmp_ctx;
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="";
726
727         tmp_ctx = talloc_new(mem_ctx);
728         if (tmp_ctx == NULL) {
729                 DEBUG(0, ("talloc_new failed\n"));
730                 return NULL;
731         }
732
733         sddl_type = sddl_flags_to_string(tmp_ctx, ace_types, ace->type, true);
734         if (sddl_type == NULL) {
735                 goto failed;
736         }
737
738         sddl_flags = sddl_flags_to_string(tmp_ctx, ace_flags, ace->flags,
739                                           true);
740         if (sddl_flags == NULL) {
741                 goto failed;
742         }
743
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",
748                                              ace->access_mask);
749                 if (sddl_mask == NULL) {
750                         goto failed;
751                 }
752         }
753
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;
759
760                 if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
761                         sddl_object = GUID_buf_string(
762                                 &object->type.type, &object_buf);
763                 }
764
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,
769                                 &iobject_buf);
770                 }
771         }
772
773         sddl_trustee = sddl_encode_sid(tmp_ctx, &ace->trustee, state);
774         if (sddl_trustee == NULL) {
775                 goto failed;
776         }
777
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);
781
782 failed:
783         talloc_free(tmp_ctx);
784         return sddl;
785 }
786
787 char *sddl_encode_ace(TALLOC_CTX *mem_ctx, const struct security_ace *ace,
788                       const struct dom_sid *domain_sid)
789 {
790         struct sddl_transition_state state = {
791                 /*
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...
795                  */
796                 .machine_sid = domain_sid,
797                 .domain_sid = domain_sid,
798                 .forest_sid = domain_sid,
799         };
800         return sddl_transition_encode_ace(mem_ctx, ace, &state);
801 }
802
803 /*
804   encode an ACL in SDDL format
805 */
806 static char *sddl_encode_acl(TALLOC_CTX *mem_ctx, const struct security_acl *acl,
807                              uint32_t flags, struct sddl_transition_state *state)
808 {
809         char *sddl;
810         uint32_t i;
811
812         /* add any ACL flags */
813         sddl = sddl_flags_to_string(mem_ctx, acl_flags, flags, false);
814         if (sddl == NULL) goto failed;
815
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;
822                 talloc_free(ace);
823         }
824
825         return sddl;
826
827 failed:
828         talloc_free(sddl);
829         return NULL;
830 }
831
832
833 /*
834   encode a security descriptor to SDDL format
835 */
836 char *sddl_encode(TALLOC_CTX *mem_ctx, const struct security_descriptor *sd,
837                   const struct dom_sid *domain_sid)
838 {
839         struct sddl_transition_state state = {
840                 /*
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...
844                  */
845                 .machine_sid = domain_sid,
846                 .domain_sid = domain_sid,
847                 .forest_sid = domain_sid,
848         };
849         char *sddl;
850         TALLOC_CTX *tmp_ctx;
851
852         /* start with a blank string */
853         sddl = talloc_strdup(mem_ctx, "");
854         if (sddl == NULL) goto failed;
855
856         tmp_ctx = talloc_new(mem_ctx);
857
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;
863         }
864
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;
870         }
871
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;
877         }
878
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;
884         }
885
886         talloc_free(tmp_ctx);
887         return sddl;
888
889 failed:
890         talloc_free(sddl);
891         return NULL;
892 }