r12608: Remove some unused #include lines.
[abartlet/samba.git/.git] / source4 / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/iconv.h"
25
26 struct flag_map {
27         const char *name;
28         uint32_t flag;
29 };
30
31 /*
32   map a series of letter codes into a uint32_t
33 */
34 static BOOL sddl_map_flags(const struct flag_map *map, const char *str, 
35                            uint32_t *flags, size_t *len)
36 {
37         const char *str0 = str;
38         if (len) *len = 0;
39         *flags = 0;
40         while (str[0] && isupper(str[0])) {
41                 int i;
42                 for (i=0;map[i].name;i++) {
43                         size_t l = strlen(map[i].name);
44                         if (strncmp(map[i].name, str, l) == 0) {
45                                 *flags |= map[i].flag;
46                                 str += l;
47                                 if (len) *len += l;
48                                 break;
49                         }
50                 }
51                 if (map[i].name == NULL) {
52                         DEBUG(1, ("Unknown flag - %s in %s\n", str, str0));
53                         return False;
54                 }
55         }
56         return True;
57 }
58
59 /*
60   a mapping between the 2 letter SID codes and sid strings
61 */
62 static const struct {
63         const char *code;
64         const char *sid;
65         uint32_t rid;
66 } sid_codes[] = {
67         { "AO", SID_BUILTIN_ACCOUNT_OPERATORS },
68         { "BA", SID_BUILTIN_ADMINISTRATORS },
69         { "RU", SID_BUILTIN_PREW2K },
70         { "PO", SID_BUILTIN_PRINT_OPERATORS },
71         { "RS", SID_BUILTIN_RAS_SERVERS },
72
73         { "AU", SID_NT_AUTHENTICATED_USERS },
74         { "SY", SID_NT_SYSTEM },
75         { "PS", SID_NT_SELF },
76         { "WD", SID_WORLD },
77         { "ED", SID_NT_ENTERPRISE_DCS },
78
79         { "CO", SID_CREATOR_OWNER },
80         { "CG", SID_CREATOR_GROUP },
81
82         { "DA", NULL, DOMAIN_RID_ADMINS },
83         { "EA", NULL, DOMAIN_RID_ENTERPRISE_ADMINS },
84         { "DD", NULL, DOMAIN_RID_DCS },
85         { "DU", NULL, DOMAIN_RID_USERS },
86         { "CA", NULL, DOMAIN_RID_CERT_ADMINS },
87 };
88
89 /*
90   decode a SID
91   It can either be a special 2 letter code, or in S-* format
92 */
93 static struct dom_sid *sddl_decode_sid(TALLOC_CTX *mem_ctx, const char **sddlp,
94                                        const struct dom_sid *domain_sid)
95 {
96         const char *sddl = (*sddlp);
97         int i;
98
99         /* see if its in the numeric format */
100         if (strncmp(sddl, "S-", 2) == 0) {
101                 size_t len = strspn(sddl+2, "-0123456789");
102                 (*sddlp) += len+2;
103                 return dom_sid_parse_talloc(mem_ctx, sddl);
104         }
105
106         /* now check for one of the special codes */
107         for (i=0;i<ARRAY_SIZE(sid_codes);i++) {
108                 if (strncmp(sid_codes[i].code, sddl, 2) == 0) break;
109         }
110         if (i == ARRAY_SIZE(sid_codes)) {
111                 DEBUG(1,("Unknown sddl sid code '%2.2s'\n", sddl));
112                 return NULL;
113         }
114
115         (*sddlp) += 2;
116
117         if (sid_codes[i].sid == NULL) {
118                 return dom_sid_add_rid(mem_ctx, domain_sid, sid_codes[i].rid);
119         }
120
121         return dom_sid_parse_talloc(mem_ctx, sid_codes[i].sid);
122 }
123
124 static const struct flag_map ace_types[] = {
125         { "AU", SEC_ACE_TYPE_SYSTEM_AUDIT },
126         { "AL", SEC_ACE_TYPE_SYSTEM_ALARM },
127         { "OA", SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT },
128         { "OD", SEC_ACE_TYPE_ACCESS_DENIED_OBJECT },
129         { "OU", SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT },
130         { "OL", SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT },
131         { "A",  SEC_ACE_TYPE_ACCESS_ALLOWED },
132         { "D",  SEC_ACE_TYPE_ACCESS_DENIED },
133         { NULL, 0 }
134 };
135
136 static const struct flag_map ace_flags[] = {
137         { "OI", SEC_ACE_FLAG_OBJECT_INHERIT },
138         { "CI", SEC_ACE_FLAG_CONTAINER_INHERIT },
139         { "NP", SEC_ACE_FLAG_NO_PROPAGATE_INHERIT },
140         { "IO", SEC_ACE_FLAG_INHERIT_ONLY },
141         { "ID", SEC_ACE_FLAG_INHERITED_ACE },
142         { "SA", SEC_ACE_FLAG_SUCCESSFUL_ACCESS },
143         { "FA", SEC_ACE_FLAG_FAILED_ACCESS },
144         { NULL, 0 },
145 };
146
147 static const struct flag_map ace_access_mask[] = {
148         { "RP", SEC_ADS_READ_PROP },
149         { "WP", SEC_ADS_WRITE_PROP },
150         { "CR", SEC_ADS_CONTROL_ACCESS },
151         { "CC", SEC_ADS_CREATE_CHILD },
152         { "DC", SEC_ADS_DELETE_CHILD },
153         { "LC", SEC_ADS_LIST },
154         { "LO", SEC_ADS_LIST_OBJECT },
155         { "RC", SEC_STD_READ_CONTROL },
156         { "WO", SEC_STD_WRITE_OWNER },
157         { "WD", SEC_STD_WRITE_DAC },
158         { "SD", SEC_STD_DELETE },
159         { "DT", SEC_ADS_DELETE_TREE },
160         { "SW", SEC_ADS_SELF_WRITE },
161         { "GA", SEC_GENERIC_ALL },
162         { "GR", SEC_GENERIC_READ },
163         { "GW", SEC_GENERIC_WRITE },
164         { "GX", SEC_GENERIC_EXECUTE },
165         { NULL, 0 }
166 };
167
168 /*
169   decode an ACE
170   return True on success, False on failure
171   note that this routine modifies the string
172 */
173 static BOOL sddl_decode_ace(TALLOC_CTX *mem_ctx, struct security_ace *ace, char *str,
174                             const struct dom_sid *domain_sid)
175 {
176         const char *tok[6];
177         const char *s;
178         int i;
179         uint32_t v;
180         struct dom_sid *sid;
181
182         ZERO_STRUCTP(ace);
183
184         /* parse out the 6 tokens */
185         tok[0] = str;
186         for (i=0;i<5;i++) {
187                 char *ptr = strchr(str, ';');
188                 if (ptr == NULL) return False;
189                 *ptr = 0;
190                 str = ptr+1;
191                 tok[i+1] = str;
192         }
193
194         /* parse ace type */
195         if (!sddl_map_flags(ace_types, tok[0], &v, NULL)) {
196                 return False;
197         }
198         ace->type = v;
199
200         /* ace flags */
201         if (!sddl_map_flags(ace_flags, tok[1], &v, NULL)) {
202                 return False;
203         }
204         ace->flags = v;
205         
206         /* access mask */
207         if (strncmp(tok[2], "0x", 2) == 0) {
208                 ace->access_mask = strtol(tok[2], NULL, 16);
209         } else {
210                 if (!sddl_map_flags(ace_access_mask, tok[2], &v, NULL)) {
211                         return False;
212                 }
213                 ace->access_mask = v;
214         }
215
216         /* object */
217         if (tok[3][0] != 0) {
218                 NTSTATUS status = GUID_from_string(tok[3], 
219                                                    &ace->object.object.type.type);
220                 if (!NT_STATUS_IS_OK(status)) {
221                         return False;
222                 }
223                 ace->object.object.flags |= SEC_ACE_OBJECT_TYPE_PRESENT;
224         }
225
226         /* inherit object */
227         if (tok[4][0] != 0) {
228                 NTSTATUS status = GUID_from_string(tok[4], 
229                                                    &ace->object.object.inherited_type.inherited_type);
230                 if (!NT_STATUS_IS_OK(status)) {
231                         return False;
232                 }
233                 ace->object.object.flags |= SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT;
234         }
235
236         /* trustee */
237         s = tok[5];
238         sid = sddl_decode_sid(mem_ctx, &s, domain_sid);
239         if (sid == NULL) {
240                 return False;
241         }
242         ace->trustee = *sid;
243         talloc_steal(mem_ctx, sid->sub_auths);
244         talloc_free(sid);
245
246         return True;
247 }
248
249 static const struct flag_map acl_flags[] = {
250         { "P", SEC_DESC_DACL_PROTECTED },
251         { "AR", SEC_DESC_DACL_AUTO_INHERIT_REQ },
252         { "AI", SEC_DESC_DACL_AUTO_INHERITED },
253         { NULL, 0 }
254 };
255
256 /*
257   decode an ACL
258 */
259 static struct security_acl *sddl_decode_acl(struct security_descriptor *sd, 
260                                             const char **sddlp, uint32_t *flags,
261                                             const struct dom_sid *domain_sid)
262 {
263         const char *sddl = *sddlp;
264         struct security_acl *acl;
265         size_t len;
266
267         *flags = 0;
268
269         acl = talloc_zero(sd, struct security_acl);
270         if (acl == NULL) return NULL;
271         acl->revision = SECURITY_ACL_REVISION_NT4;
272
273         if (isupper(sddl[0]) && sddl[1] == ':') {
274                 /* its an empty ACL */
275                 return acl;
276         }
277
278         /* work out the ACL flags */
279         if (!sddl_map_flags(acl_flags, sddl, flags, &len)) {
280                 talloc_free(acl);
281                 return NULL;
282         }
283         sddl += len;
284
285         /* now the ACEs */
286         while (*sddl == '(') {
287                 char *astr;
288                 len = strcspn(sddl+1, ")");
289                 astr = talloc_strndup(acl, sddl+1, len);
290                 if (astr == NULL || sddl[len+1] != ')') {
291                         talloc_free(acl);
292                         return NULL;
293                 }
294                 acl->aces = talloc_realloc(acl, acl->aces, struct security_ace, 
295                                            acl->num_aces+1);
296                 if (acl->aces == NULL) {
297                         talloc_free(acl);
298                         return NULL;
299                 }
300                 if (!sddl_decode_ace(acl->aces, &acl->aces[acl->num_aces], 
301                                      astr, domain_sid)) {
302                         talloc_free(acl);
303                         return NULL;
304                 }
305                 talloc_free(astr);
306                 sddl += len+2;
307                 acl->num_aces++;
308         }
309
310         (*sddlp) = sddl;
311         return acl;
312 }
313
314 /*
315   decode a security descriptor in SDDL format
316 */
317 struct security_descriptor *sddl_decode(TALLOC_CTX *mem_ctx, const char *sddl,
318                                         const struct dom_sid *domain_sid)
319 {
320         struct security_descriptor *sd;
321         sd = talloc_zero(mem_ctx, struct security_descriptor);
322
323         sd->revision = SECURITY_DESCRIPTOR_REVISION_1;
324         sd->type     = SEC_DESC_SELF_RELATIVE;
325         
326         while (*sddl) {
327                 uint32_t flags;
328                 char c = sddl[0];
329                 if (sddl[1] != ':') goto failed;
330
331                 sddl += 2;
332                 switch (c) {
333                 case 'D':
334                         if (sd->dacl != NULL) goto failed;
335                         sd->dacl = sddl_decode_acl(sd, &sddl, &flags, domain_sid);
336                         if (sd->dacl == NULL) goto failed;
337                         sd->type |= flags | SEC_DESC_DACL_PRESENT;
338                         break;
339                 case 'S':
340                         if (sd->sacl != NULL) goto failed;
341                         sd->sacl = sddl_decode_acl(sd, &sddl, &flags, domain_sid);
342                         if (sd->sacl == NULL) goto failed;
343                         /* this relies on the SEC_DESC_SACL_* flags being
344                            1 bit shifted from the SEC_DESC_DACL_* flags */
345                         sd->type |= (flags<<1) | SEC_DESC_SACL_PRESENT;
346                         break;
347                 case 'O':
348                         if (sd->owner_sid != NULL) goto failed;
349                         sd->owner_sid = sddl_decode_sid(sd, &sddl, domain_sid);
350                         if (sd->owner_sid == NULL) goto failed;
351                         break;
352                 case 'G':
353                         if (sd->group_sid != NULL) goto failed;
354                         sd->group_sid = sddl_decode_sid(sd, &sddl, domain_sid);
355                         if (sd->group_sid == NULL) goto failed;
356                         break;
357                 }
358         }
359
360         return sd;
361
362 failed:
363         DEBUG(2,("Badly formatted SDDL '%s'\n", sddl));
364         talloc_free(sd);
365         return NULL;
366 }
367
368 /*
369   turn a set of flags into a string
370 */
371 static char *sddl_flags_to_string(TALLOC_CTX *mem_ctx, const struct flag_map *map,
372                                   uint32_t flags, BOOL check_all)
373 {
374         int i;
375         char *s;
376
377         /* try to find an exact match */
378         for (i=0;map[i].name;i++) {
379                 if (map[i].flag == flags) {
380                         return talloc_strdup(mem_ctx, map[i].name);
381                 }
382         }
383
384         s = talloc_strdup(mem_ctx, "");
385
386         /* now by bits */
387         for (i=0;map[i].name;i++) {
388                 if ((flags & map[i].flag) != 0) {
389                         s = talloc_asprintf_append(s, "%s", map[i].name);
390                         if (s == NULL) goto failed;
391                         flags &= ~map[i].flag;
392                 }
393         }
394
395         if (check_all && flags != 0) {
396                 goto failed;
397         }
398
399         return s;
400
401 failed:
402         talloc_free(s);
403         return NULL;
404 }
405
406 /*
407   encode a sid in SDDL format
408 */
409 static char *sddl_encode_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
410                              const struct dom_sid *domain_sid)
411 {
412         int i;
413         char *sidstr;
414
415         sidstr = dom_sid_string(mem_ctx, sid);
416         if (sidstr == NULL) return NULL;
417
418         /* seen if its a well known sid */ 
419         for (i=0;sid_codes[i].sid;i++) {
420                 if (strcmp(sidstr, sid_codes[i].sid) == 0) {
421                         talloc_free(sidstr);
422                         return talloc_strdup(mem_ctx, sid_codes[i].code);
423                 }
424         }
425
426         /* or a well known rid in our domain */
427         if (dom_sid_in_domain(domain_sid, sid)) {
428                 uint32_t rid = sid->sub_auths[sid->num_auths-1];
429                 for (;i<ARRAY_SIZE(sid_codes);i++) {
430                         if (rid == sid_codes[i].rid) {
431                                 talloc_free(sidstr);
432                                 return talloc_strdup(mem_ctx, sid_codes[i].code);
433                         }
434                 }
435         }
436         
437         talloc_free(sidstr);
438
439         /* TODO: encode well known sids as two letter codes */
440         return dom_sid_string(mem_ctx, sid);
441 }
442
443
444 /*
445   encode an ACE in SDDL format
446 */
447 static char *sddl_encode_ace(TALLOC_CTX *mem_ctx, const struct security_ace *ace,
448                              const struct dom_sid *domain_sid)
449 {
450         char *sddl = NULL;
451         TALLOC_CTX *tmp_ctx;
452         const char *s_type="", *s_flags="", *s_mask="", 
453                 *s_object="", *s_iobject="", *s_trustee="";
454
455         tmp_ctx = talloc_new(mem_ctx);
456         if (tmp_ctx == NULL) {
457                 DEBUG(0, ("talloc_new failed\n"));
458                 return NULL;
459         }
460
461         s_type = sddl_flags_to_string(tmp_ctx, ace_types, ace->type, True);
462         if (s_type == NULL) goto failed;
463
464         s_flags = sddl_flags_to_string(tmp_ctx, ace_flags, ace->flags, True);
465         if (s_flags == NULL) goto failed;
466
467         s_mask = sddl_flags_to_string(tmp_ctx, ace_access_mask, ace->access_mask, True);
468         if (s_mask == NULL) {
469                 s_mask = talloc_asprintf(tmp_ctx, "0x%08x", ace->access_mask);
470                 if (s_mask == NULL) goto failed;
471         }
472
473         if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
474             ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT ||
475             ace->type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT ||
476             ace->type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT) {
477                 if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
478                         s_object = GUID_string(tmp_ctx, &ace->object.object.type.type);
479                         if (s_object == NULL) goto failed;
480                 }
481
482                 if (ace->object.object.flags & SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
483                         s_iobject = GUID_string(tmp_ctx, &ace->object.object.inherited_type.inherited_type);
484                         if (s_iobject == NULL) goto failed;
485                 }
486         }
487         
488         s_trustee = sddl_encode_sid(tmp_ctx, &ace->trustee, domain_sid);
489         if (s_trustee == NULL) goto failed;
490
491         sddl = talloc_asprintf(mem_ctx, "%s;%s;%s;%s;%s;%s",
492                                s_type, s_flags, s_mask, s_object, s_iobject, s_trustee);
493
494 failed:
495         talloc_free(tmp_ctx);
496         return sddl;
497 }
498
499 /*
500   encode an ACL in SDDL format
501 */
502 static char *sddl_encode_acl(TALLOC_CTX *mem_ctx, const struct security_acl *acl,
503                              uint32_t flags, const struct dom_sid *domain_sid)
504 {
505         char *sddl;
506         int i;
507
508         /* add any ACL flags */
509         sddl = sddl_flags_to_string(mem_ctx, acl_flags, flags, False);
510         if (sddl == NULL) goto failed;
511
512         /* now the ACEs, encoded in braces */
513         for (i=0;i<acl->num_aces;i++) {
514                 char *ace = sddl_encode_ace(sddl, &acl->aces[i], domain_sid);
515                 if (ace == NULL) goto failed;
516                 sddl = talloc_asprintf_append(sddl, "(%s)", ace);
517                 if (sddl == NULL) goto failed;
518                 talloc_free(ace);
519         }
520
521         return sddl;
522
523 failed:
524         talloc_free(sddl);
525         return NULL;
526 }
527
528
529 /*
530   encode a security descriptor to SDDL format
531 */
532 char *sddl_encode(TALLOC_CTX *mem_ctx, const struct security_descriptor *sd,
533                   const struct dom_sid *domain_sid)
534 {
535         char *sddl;
536         TALLOC_CTX *tmp_ctx;
537
538         /* start with a blank string */
539         sddl = talloc_strdup(mem_ctx, "");
540         if (sddl == NULL) goto failed;
541
542         tmp_ctx = talloc_new(mem_ctx);
543
544         if (sd->owner_sid != NULL) {
545                 char *sid = sddl_encode_sid(tmp_ctx, sd->owner_sid, domain_sid);
546                 if (sid == NULL) goto failed;
547                 sddl = talloc_asprintf_append(sddl, "O:%s", sid);
548                 if (sddl == NULL) goto failed;
549         }
550
551         if (sd->group_sid != NULL) {
552                 char *sid = sddl_encode_sid(tmp_ctx, sd->group_sid, domain_sid);
553                 if (sid == NULL) goto failed;
554                 sddl = talloc_asprintf_append(sddl, "G:%s", sid);
555                 if (sddl == NULL) goto failed;
556         }
557
558         if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl != NULL) {
559                 char *acl = sddl_encode_acl(tmp_ctx, sd->dacl, sd->type, domain_sid);
560                 if (acl == NULL) goto failed;
561                 sddl = talloc_asprintf_append(sddl, "D:%s", acl);
562                 if (sddl == NULL) goto failed;
563         }
564
565         if ((sd->type & SEC_DESC_SACL_PRESENT) && sd->sacl != NULL) {
566                 char *acl = sddl_encode_acl(tmp_ctx, sd->sacl, sd->type>>1, domain_sid);
567                 if (acl == NULL) goto failed;
568                 sddl = talloc_asprintf_append(sddl, "S:%s", acl);
569                 if (sddl == NULL) goto failed;
570         }
571
572         talloc_free(tmp_ctx);
573         return sddl;
574
575 failed:
576         talloc_free(sddl);
577         return NULL;
578 }
579
580