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