2 Unix SMB/CIFS implementation.
4 security descriptor description language functions
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
24 #include "system/iconv.h"
25 #include "librpc/gen_ndr/ndr_security.h"
33 map a series of letter codes into a uint32_t
35 static BOOL sddl_map_flags(const struct flag_map *map, const char *str,
36 uint32_t *flags, size_t *len)
38 const char *str0 = str;
41 while (str[0] && isupper(str[0])) {
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;
52 if (map[i].name == NULL) {
53 DEBUG(1, ("Unknown flag - %s in %s\n", str, str0));
61 a mapping between the 2 letter SID codes and sid strings
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 },
74 { "AU", SID_NT_AUTHENTICATED_USERS },
75 { "SY", SID_NT_SYSTEM },
76 { "PS", SID_NT_SELF },
78 { "ED", SID_NT_ENTERPRISE_DCS },
80 { "CO", SID_CREATOR_OWNER },
81 { "CG", SID_CREATOR_GROUP },
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 },
92 It can either be a special 2 letter code, or in S-* format
94 static struct dom_sid *sddl_decode_sid(TALLOC_CTX *mem_ctx, const char **sddlp,
95 struct dom_sid *domain_sid)
97 const char *sddl = (*sddlp);
100 /* see if its in the numeric format */
101 if (strncmp(sddl, "S-", 2) == 0) {
102 size_t len = strspn(sddl+2, "-0123456789");
104 return dom_sid_parse_talloc(mem_ctx, sddl);
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;
111 if (i == ARRAY_SIZE(sid_codes)) {
112 DEBUG(1,("Unknown sddl sid code '%2.2s'\n", sddl));
118 if (sid_codes[i].sid == NULL) {
119 return dom_sid_add_rid(mem_ctx, domain_sid, sid_codes[i].rid);
122 return dom_sid_parse_talloc(mem_ctx, sid_codes[i].sid);
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 },
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 },
148 static const struct flag_map ace_access_mask[] = {
149 { "RC", SEC_STD_READ_CONTROL },
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 { "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 },
171 return True on success, False on failure
172 note that this routine modifies the string
174 static BOOL sddl_decode_ace(TALLOC_CTX *mem_ctx, struct security_ace *ace, char *str,
175 struct dom_sid *domain_sid)
185 /* parse out the 6 tokens */
188 char *ptr = strchr(str, ';');
189 if (ptr == NULL) return False;
196 if (!sddl_map_flags(ace_types, tok[0], &v, NULL)) {
202 if (!sddl_map_flags(ace_flags, tok[1], &v, NULL)) {
208 if (strncmp(tok[2], "0x", 2) == 0) {
209 ace->access_mask = strtol(tok[2], NULL, 16);
211 if (!sddl_map_flags(ace_access_mask, tok[2], &v, NULL)) {
214 ace->access_mask = v;
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)) {
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)) {
237 sid = sddl_decode_sid(mem_ctx, &s, domain_sid);
242 talloc_steal(mem_ctx, sid->sub_auths);
248 static const struct flag_map acl_flags[] = {
249 { "P", SEC_DESC_DACL_PROTECTED },
250 { "AR", SEC_DESC_DACL_AUTO_INHERIT_REQ },
251 { "AI", SEC_DESC_DACL_AUTO_INHERITED },
258 static struct security_acl *sddl_decode_acl(struct security_descriptor *sd,
259 const char **sddlp, uint32_t *flags,
260 struct dom_sid *domain_sid)
262 const char *sddl = *sddlp;
263 struct security_acl *acl;
266 acl = talloc_zero(sd, struct security_acl);
267 if (acl == NULL) return NULL;
268 acl->revision = SECURITY_ACL_REVISION_NT4;
270 if (isupper(sddl[0]) && sddl[1] == ':') {
271 /* its an empty ACL */
275 /* work out the ACL flags */
276 if (!sddl_map_flags(acl_flags, sddl, flags, &len)) {
283 while (*sddl == '(') {
284 len = strcspn(sddl+1, ")");
285 char *astr = talloc_strndup(acl, sddl+1, len);
286 if (astr == NULL || sddl[len+1] != ')') {
290 acl->aces = talloc_realloc(acl, acl->aces, struct security_ace,
292 if (acl->aces == NULL) {
296 if (!sddl_decode_ace(acl->aces, &acl->aces[acl->num_aces],
311 decode a security descriptor in SDDL format
313 struct security_descriptor *sddl_decode(TALLOC_CTX *mem_ctx, const char *sddl,
314 struct dom_sid *domain_sid)
316 struct security_descriptor *sd;
317 sd = talloc_zero(mem_ctx, struct security_descriptor);
319 sd->revision = SECURITY_DESCRIPTOR_REVISION_1;
320 sd->type = SEC_DESC_SELF_RELATIVE;
325 if (sddl[1] != ':') goto failed;
330 if (sd->dacl != NULL) goto failed;
331 sd->dacl = sddl_decode_acl(sd, &sddl, &flags, domain_sid);
332 if (sd->dacl == NULL) goto failed;
333 sd->type |= flags | SEC_DESC_DACL_PRESENT;
336 if (sd->sacl != NULL) goto failed;
337 sd->sacl = sddl_decode_acl(sd, &sddl, &flags, domain_sid);
338 if (sd->sacl == NULL) goto failed;
339 /* this relies on the SEC_DESC_SACL_* flags being
340 1 bit shifted from the SEC_DESC_DACL_* flags */
341 sd->type |= (flags<<1) | SEC_DESC_SACL_PRESENT;
344 if (sd->owner_sid != NULL) goto failed;
345 sd->owner_sid = sddl_decode_sid(sd, &sddl, domain_sid);
346 if (sd->owner_sid == NULL) goto failed;
349 if (sd->group_sid != NULL) goto failed;
350 sd->group_sid = sddl_decode_sid(sd, &sddl, domain_sid);
351 if (sd->group_sid == NULL) goto failed;
359 DEBUG(2,("Badly formatted SDDL '%s'\n", sddl));