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