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