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