2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Luke Kenneth Caseson Leighton 1998-1999
6 Copyright (C) Jeremy Allison 1999
7 Copyright (C) Stefan (metze) Metzmacher 2002
8 Copyright (C) Simo Sorce 2002
9 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2005
10 Copyright (C) Andrew Bartlett 2010
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "../librpc/gen_ndr/netlogon.h"
29 #include "../libcli/security/security.h"
32 * Some useful sids, more well known sids can be found at
33 * http://support.microsoft.com/kb/243330/EN-US/
38 const struct dom_sid global_sid_World_Domain = /* Everyone domain */
39 { 1, 0, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
41 const struct dom_sid global_sid_World = /* Everyone */
42 { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
44 const struct dom_sid global_sid_Local_Authority = /* Local Authority */
45 { 1, 0, {0,0,0,0,0,2}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
47 const struct dom_sid global_sid_Creator_Owner_Domain = /* Creator Owner domain */
48 { 1, 0, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
50 const struct dom_sid global_sid_NT_Authority = /* NT Authority */
51 { 1, 0, {0,0,0,0,0,5}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
53 const struct dom_sid global_sid_System = /* System */
54 { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
56 const struct dom_sid global_sid_NULL = /* NULL sid */
57 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
59 const struct dom_sid global_sid_Authenticated_Users = /* All authenticated rids */
60 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
62 /* for documentation S-1-5-12 */
63 const struct dom_sid global_sid_Restriced = /* Restriced Code */
64 { 1, 1, {0,0,0,0,0,5}, {12,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
68 const struct dom_sid global_sid_Asserted_Identity = /* Asserted Identity */
69 { 1, 0, {0,0,0,0,0,18}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
71 const struct dom_sid global_sid_Asserted_Identity_Service = /* Asserted Identity Service */
72 { 1, 1, {0,0,0,0,0,18}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
74 const struct dom_sid global_sid_Asserted_Identity_Authentication_Authority = /* Asserted Identity Authentication Authority */
75 { 1, 1, {0,0,0,0,0,18}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
78 const struct dom_sid global_sid_Network = /* Network rids */
79 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
82 const struct dom_sid global_sid_Creator_Owner = /* Creator Owner */
83 { 1, 1, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
85 const struct dom_sid global_sid_Creator_Group = /* Creator Group */
86 { 1, 1, {0,0,0,0,0,3}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
88 const struct dom_sid global_sid_Owner_Rights = /* Owner Rights */
89 { 1, 1, {0,0,0,0,0,3}, {4,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
91 const struct dom_sid global_sid_Anonymous = /* Anonymous login */
92 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
94 const struct dom_sid global_sid_Enterprise_DCs = /* Enterprise DCs */
95 { 1, 1, {0,0,0,0,0,5}, {9,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
97 const struct dom_sid global_sid_Builtin = /* Local well-known domain */
98 { 1, 1, {0,0,0,0,0,5}, {32,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
100 const struct dom_sid global_sid_Builtin_Administrators = /* Builtin administrators */
101 { 1, 2, {0,0,0,0,0,5}, {32,544,0,0,0,0,0,0,0,0,0,0,0,0,0}};
103 const struct dom_sid global_sid_Builtin_Users = /* Builtin users */
104 { 1, 2, {0,0,0,0,0,5}, {32,545,0,0,0,0,0,0,0,0,0,0,0,0,0}};
106 const struct dom_sid global_sid_Builtin_Guests = /* Builtin guest users */
107 { 1, 2, {0,0,0,0,0,5}, {32,546,0,0,0,0,0,0,0,0,0,0,0,0,0}};
109 const struct dom_sid global_sid_Builtin_Power_Users = /* Builtin power users */
110 { 1, 2, {0,0,0,0,0,5}, {32,547,0,0,0,0,0,0,0,0,0,0,0,0,0}};
112 const struct dom_sid global_sid_Builtin_Account_Operators = /* Builtin account operators */
113 { 1, 2, {0,0,0,0,0,5}, {32,548,0,0,0,0,0,0,0,0,0,0,0,0,0}};
115 const struct dom_sid global_sid_Builtin_Server_Operators = /* Builtin server operators */
116 { 1, 2, {0,0,0,0,0,5}, {32,549,0,0,0,0,0,0,0,0,0,0,0,0,0}};
118 const struct dom_sid global_sid_Builtin_Print_Operators = /* Builtin print operators */
119 { 1, 2, {0,0,0,0,0,5}, {32,550,0,0,0,0,0,0,0,0,0,0,0,0,0}};
121 const struct dom_sid global_sid_Builtin_Backup_Operators = /* Builtin backup operators */
122 { 1, 2, {0,0,0,0,0,5}, {32,551,0,0,0,0,0,0,0,0,0,0,0,0,0}};
124 const struct dom_sid global_sid_Builtin_Replicator = /* Builtin replicator */
125 { 1, 2, {0,0,0,0,0,5}, {32,552,0,0,0,0,0,0,0,0,0,0,0,0,0}};
127 const struct dom_sid global_sid_Builtin_PreWin2kAccess = /* Builtin pre win2k access */
128 { 1, 2, {0,0,0,0,0,5}, {32,554,0,0,0,0,0,0,0,0,0,0,0,0,0}};
131 const struct dom_sid global_sid_Unix_Users = /* Unmapped Unix users */
132 { 1, 1, {0,0,0,0,0,22}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
134 const struct dom_sid global_sid_Unix_Groups = /* Unmapped Unix groups */
135 { 1, 1, {0,0,0,0,0,22}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
138 * http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
141 const struct dom_sid global_sid_Unix_NFS = /* MS NFS and Apple style */
142 { 1, 1, {0,0,0,0,0,5}, {88,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
144 const struct dom_sid global_sid_Unix_NFS_Users = /* Unix uid, MS NFS and Apple style */
145 { 1, 2, {0,0,0,0,0,5}, {88,1,0,0,0,0,0,0,0,0,0,0,0,0,0}};
147 const struct dom_sid global_sid_Unix_NFS_Groups = /* Unix gid, MS NFS and Apple style */
148 { 1, 2, {0,0,0,0,0,5}, {88,2,0,0,0,0,0,0,0,0,0,0,0,0,0}};
150 const struct dom_sid global_sid_Unix_NFS_Mode = /* Unix mode */
151 { 1, 2, {0,0,0,0,0,5}, {88,3,0,0,0,0,0,0,0,0,0,0,0,0,0}};
152 /* Unused, left here for documentary purposes */
154 const struct dom_sid global_sid_Unix_NFS_Other = /* Unix other, MS NFS and Apple style */
155 { 1, 2, {0,0,0,0,0,5}, {88,4,0,0,0,0,0,0,0,0,0,0,0,0,0}};
158 /* Unused, left here for documentary purposes */
160 #define SECURITY_NULL_SID_AUTHORITY 0
161 #define SECURITY_WORLD_SID_AUTHORITY 1
162 #define SECURITY_LOCAL_SID_AUTHORITY 2
163 #define SECURITY_CREATOR_SID_AUTHORITY 3
164 #define SECURITY_NT_AUTHORITY 5
167 static struct dom_sid system_sid_array[1] =
168 { { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
169 static const struct security_token system_token = {
170 .num_sids = ARRAY_SIZE(system_sid_array),
171 .sids = system_sid_array,
172 .privilege_mask = SE_ALL_PRIVS
175 /****************************************************************************
176 Lookup string names for SID types.
177 ****************************************************************************/
179 static const struct {
180 enum lsa_SidType sid_type;
182 } sid_name_type[] = {
183 {SID_NAME_USE_NONE, "None"},
184 {SID_NAME_USER, "User"},
185 {SID_NAME_DOM_GRP, "Domain Group"},
186 {SID_NAME_DOMAIN, "Domain"},
187 {SID_NAME_ALIAS, "Local Group"},
188 {SID_NAME_WKN_GRP, "Well-known Group"},
189 {SID_NAME_DELETED, "Deleted Account"},
190 {SID_NAME_INVALID, "Invalid Account"},
191 {SID_NAME_UNKNOWN, "UNKNOWN"},
192 {SID_NAME_COMPUTER, "Computer"},
193 {SID_NAME_LABEL, "Mandatory Label"}
196 const char *sid_type_lookup(uint32_t sid_type)
200 /* Look through list */
201 for (i=0; i < ARRAY_SIZE(sid_name_type); i++) {
202 if (sid_name_type[i].sid_type == sid_type) {
203 return sid_name_type[i].string;
208 return "SID *TYPE* is INVALID";
211 /**************************************************************************
212 Create the SYSTEM token.
213 ***************************************************************************/
215 const struct security_token *get_system_token(void)
217 return &system_token;
220 bool sid_compose(struct dom_sid *dst, const struct dom_sid *domain_sid, uint32_t rid)
222 sid_copy(dst, domain_sid);
223 return sid_append_rid(dst, rid);
226 /*****************************************************************
227 Removes the last rid from the end of a sid
228 *****************************************************************/
230 bool sid_split_rid(struct dom_sid *sid, uint32_t *rid)
232 if (sid->num_auths > 0) {
235 *rid = sid->sub_auths[sid->num_auths];
242 /*****************************************************************
243 Return the last rid from the end of a sid
244 *****************************************************************/
246 bool sid_peek_rid(const struct dom_sid *sid, uint32_t *rid)
251 if (sid->num_auths > 0) {
252 *rid = sid->sub_auths[sid->num_auths - 1];
258 /*****************************************************************
259 Return the last rid from the end of a sid
260 and check the sid against the exp_dom_sid
261 *****************************************************************/
263 bool sid_peek_check_rid(const struct dom_sid *exp_dom_sid, const struct dom_sid *sid, uint32_t *rid)
265 if (!exp_dom_sid || !sid || !rid)
268 if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
272 if (sid_compare_domain(exp_dom_sid, sid)!=0){
277 return sid_peek_rid(sid, rid);
280 /*****************************************************************
282 *****************************************************************/
284 void sid_copy(struct dom_sid *dst, const struct dom_sid *src)
288 *dst = (struct dom_sid) {
289 .sid_rev_num = src->sid_rev_num,
290 .num_auths = src->num_auths,
293 memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
295 for (i = 0; i < src->num_auths; i++)
296 dst->sub_auths[i] = src->sub_auths[i];
299 /*****************************************************************
300 Parse a on-the-wire SID to a struct dom_sid.
301 *****************************************************************/
303 struct sid_parse_ret sid_parse(
304 const uint8_t *inbuf, size_t len, struct dom_sid *sid)
306 DATA_BLOB in = data_blob_const(inbuf, len);
307 enum ndr_err_code ndr_err;
309 ndr_err = ndr_pull_struct_blob_all(
310 &in, NULL, sid, (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
311 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
312 return (struct sid_parse_ret) { .len = -1 };
314 return (struct sid_parse_ret) { .len = ndr_size_dom_sid(sid, 0) };
317 /*****************************************************************
318 See if 2 SIDs are in the same domain
319 this just compares the leading sub-auths
320 *****************************************************************/
322 int sid_compare_domain(const struct dom_sid *sid1, const struct dom_sid *sid2)
326 n = MIN(sid1->num_auths, sid2->num_auths);
328 for (i = n-1; i >= 0; --i)
329 if (sid1->sub_auths[i] != sid2->sub_auths[i])
330 return sid1->sub_auths[i] - sid2->sub_auths[i];
332 return dom_sid_compare_auth(sid1, sid2);
335 /********************************************************************
336 Add SID to an array SIDs
337 ********************************************************************/
339 NTSTATUS add_sid_to_array(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
340 struct dom_sid **sids, uint32_t *num)
344 if ((*num) == UINT32_MAX) {
345 return NT_STATUS_INTEGER_OVERFLOW;
348 tmp = talloc_realloc(mem_ctx, *sids, struct dom_sid, (*num)+1);
351 return NT_STATUS_NO_MEMORY;
355 sid_copy(&((*sids)[*num]), sid);
362 /********************************************************************
363 Add SID to an array SIDs ensuring that it is not already there
364 ********************************************************************/
366 NTSTATUS add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
367 struct dom_sid **sids, uint32_t *num_sids)
371 for (i=0; i<(*num_sids); i++) {
372 if (dom_sid_equal(sid, &(*sids)[i])) {
377 return add_sid_to_array(mem_ctx, sid, sids, num_sids);
380 /********************************************************************
381 Remove SID from an array
382 ********************************************************************/
384 void del_sid_from_array(const struct dom_sid *sid, struct dom_sid **sids,
387 struct dom_sid *sid_list = *sids;
390 for ( i=0; i<*num; i++ ) {
392 /* if we find the SID, then decrement the count
393 and break out of the loop */
395 if (dom_sid_equal(sid, &sid_list[i])) {
401 /* This loop will copy the remainder of the array
402 if i < num of sids in the array */
404 for ( ; i<*num; i++ ) {
405 sid_copy( &sid_list[i], &sid_list[i+1] );
411 bool add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
412 uint32_t rid, uint32_t **pp_rids, size_t *p_num)
416 for (i=0; i<*p_num; i++) {
417 if ((*pp_rids)[i] == rid)
421 *pp_rids = talloc_realloc(mem_ctx, *pp_rids, uint32_t, *p_num+1);
423 if (*pp_rids == NULL) {
428 (*pp_rids)[*p_num] = rid;
433 bool is_null_sid(const struct dom_sid *sid)
435 const struct dom_sid null_sid = {0};
436 return dom_sid_equal(sid, &null_sid);
440 * See [MS-LSAT] 3.1.1.1.1 Predefined Translation Database and Corresponding View
442 struct predefined_name_mapping {
444 enum lsa_SidType type;
448 struct predefined_domain_mapping {
452 const struct predefined_name_mapping *names;
455 /* S-1-${AUTHORITY} */
456 #define _SID0(authority) \
457 { 1, 0, {0,0,0,0,0,authority}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
458 /* S-1-${AUTHORITY}-${SUB1} */
459 #define _SID1(authority,sub1) \
460 { 1, 1, {0,0,0,0,0,authority}, {sub1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
461 /* S-1-${AUTHORITY}-${SUB1}-${SUB2} */
462 #define _SID2(authority,sub1,sub2) \
463 { 1, 2, {0,0,0,0,0,authority}, {sub1,sub2,0,0,0,0,0,0,0,0,0,0,0,0,0}}
468 static const struct predefined_name_mapping predefined_names_S_1_0[] = {
471 .type = SID_NAME_WKN_GRP,
472 .sid = _SID1(0, 0), /* S-1-0-0 */
479 static const struct predefined_name_mapping predefined_names_S_1_1[] = {
482 .type = SID_NAME_WKN_GRP,
483 .sid = _SID1(1, 0), /* S-1-1-0 */
490 static const struct predefined_name_mapping predefined_names_S_1_2[] = {
493 .type = SID_NAME_WKN_GRP,
494 .sid = _SID1(2, 0), /* S-1-2-0 */
501 static const struct predefined_name_mapping predefined_names_S_1_3[] = {
503 .name = "CREATOR OWNER",
504 .type = SID_NAME_WKN_GRP,
505 .sid = _SID1(3, 0), /* S-1-3-0 */
508 .name = "CREATOR GROUP",
509 .type = SID_NAME_WKN_GRP,
510 .sid = _SID1(3, 1), /* S-1-3-1 */
513 .name = "CREATOR OWNER SERVER",
514 .type = SID_NAME_WKN_GRP,
515 .sid = _SID1(3, 0), /* S-1-3-2 */
518 .name = "CREATOR GROUP SERVER",
519 .type = SID_NAME_WKN_GRP,
520 .sid = _SID1(3, 1), /* S-1-3-3 */
523 .name = "OWNER RIGHTS",
524 .type = SID_NAME_WKN_GRP,
525 .sid = _SID1(3, 4), /* S-1-3-4 */
530 * S-1-5 only 'NT Pseudo Domain'
532 static const struct predefined_name_mapping predefined_names_S_1_5p[] = {
534 .name = "NT Pseudo Domain",
535 .type = SID_NAME_DOMAIN,
536 .sid = _SID0(5), /* S-1-5 */
541 * S-1-5 'NT AUTHORITY'
543 static const struct predefined_name_mapping predefined_names_S_1_5a[] = {
546 .type = SID_NAME_WKN_GRP,
547 .sid = _SID1(5, 1), /* S-1-5-1 */
551 .type = SID_NAME_WKN_GRP,
552 .sid = _SID1(5, 2), /* S-1-5-2 */
556 .type = SID_NAME_WKN_GRP,
557 .sid = _SID1(5, 3), /* S-1-5-3 */
560 .name = "INTERACTIVE",
561 .type = SID_NAME_WKN_GRP,
562 .sid = _SID1(5, 4), /* S-1-5-4 */
566 .type = SID_NAME_WKN_GRP,
567 .sid = _SID1(5, 6), /* S-1-5-6 */
570 .name = "ANONYMOUS LOGON",
571 .type = SID_NAME_WKN_GRP,
572 .sid = _SID1(5, 7), /* S-1-5-7 */
576 .type = SID_NAME_WKN_GRP,
577 .sid = _SID1(5, 8), /* S-1-5-8 */
580 .name = "ENTERPRISE DOMAIN CONTROLLERS",
581 .type = SID_NAME_WKN_GRP,
582 .sid = _SID1(5, 9), /* S-1-5-9 */
586 .type = SID_NAME_WKN_GRP,
587 .sid = _SID1(5, 10), /* S-1-5-10 */
590 .name = "Authenticated Users",
591 .type = SID_NAME_WKN_GRP,
592 .sid = _SID1(5, 11), /* S-1-5-11 */
595 .name = "RESTRICTED",
596 .type = SID_NAME_WKN_GRP,
597 .sid = _SID1(5, 12), /* S-1-5-12 */
600 .name = "TERMINAL SERVER USER",
601 .type = SID_NAME_WKN_GRP,
602 .sid = _SID1(5, 13), /* S-1-5-13 */
605 .name = "REMOTE INTERACTIVE LOGON",
606 .type = SID_NAME_WKN_GRP,
607 .sid = _SID1(5, 14), /* S-1-5-14 */
610 .name = "This Organization",
611 .type = SID_NAME_WKN_GRP,
612 .sid = _SID1(5, 15), /* S-1-5-15 */
616 .type = SID_NAME_WKN_GRP,
617 .sid = _SID1(5, 17), /* S-1-5-17 */
621 .type = SID_NAME_WKN_GRP,
622 .sid = _SID1(5, 18), /* S-1-5-18 */
625 .name = "LOCAL SERVICE",
626 .type = SID_NAME_WKN_GRP,
627 .sid = _SID1(5, 19), /* S-1-5-19 */
630 .name = "NETWORK SERVICE",
631 .type = SID_NAME_WKN_GRP,
632 .sid = _SID1(5, 20), /* S-1-5-20 */
635 .name = "WRITE RESTRICTED",
636 .type = SID_NAME_WKN_GRP,
637 .sid = _SID1(5, 33), /* S-1-5-33 */
640 .name = "Other Organization",
641 .type = SID_NAME_WKN_GRP,
642 .sid = _SID1(5, 1000), /* S-1-5-1000 */
649 static const struct predefined_name_mapping predefined_names_S_1_5_32[] = {
652 .type = SID_NAME_DOMAIN,
653 .sid = _SID1(5, 32), /* S-1-5-32 */
660 static const struct predefined_name_mapping predefined_names_S_1_5_64[] = {
662 .name = "NTLM Authentication",
663 .type = SID_NAME_WKN_GRP,
664 .sid = _SID2(5, 64, 10), /* S-1-5-64-10 */
667 .name = "SChannel Authentication",
668 .type = SID_NAME_WKN_GRP,
669 .sid = _SID2(5, 64, 14), /* S-1-5-64-14 */
672 .name = "Digest Authentication",
673 .type = SID_NAME_WKN_GRP,
674 .sid = _SID2(5, 64, 21), /* S-1-5-64-21 */
681 static const struct predefined_name_mapping predefined_names_S_1_7[] = {
684 .type = SID_NAME_DOMAIN,
685 .sid = _SID0(7), /* S-1-7 */
692 static const struct predefined_name_mapping predefined_names_S_1_16[] = {
694 .name = "Mandatory Label",
695 .type = SID_NAME_DOMAIN,
696 .sid = _SID0(16), /* S-1-16 */
699 .name = "Untrusted Mandatory Level",
700 .type = SID_NAME_LABEL,
701 .sid = _SID1(16, 0), /* S-1-16-0 */
704 .name = "Low Mandatory Level",
705 .type = SID_NAME_LABEL,
706 .sid = _SID1(16, 4096), /* S-1-16-4096 */
709 .name = "Medium Mandatory Level",
710 .type = SID_NAME_LABEL,
711 .sid = _SID1(16, 8192), /* S-1-16-8192 */
714 .name = "High Mandatory Level",
715 .type = SID_NAME_LABEL,
716 .sid = _SID1(16, 12288), /* S-1-16-12288 */
719 .name = "System Mandatory Level",
720 .type = SID_NAME_LABEL,
721 .sid = _SID1(16, 16384), /* S-1-16-16384 */
724 .name = "Protected Process Mandatory Level",
725 .type = SID_NAME_LABEL,
726 .sid = _SID1(16, 20480), /* S-1-16-20480 */
730 static const struct predefined_domain_mapping predefined_domains[] = {
733 .sid = _SID0(0), /* S-1-0 */
734 .num_names = ARRAY_SIZE(predefined_names_S_1_0),
735 .names = predefined_names_S_1_0,
739 .sid = _SID0(1), /* S-1-1 */
740 .num_names = ARRAY_SIZE(predefined_names_S_1_1),
741 .names = predefined_names_S_1_1,
745 .sid = _SID0(2), /* S-1-2 */
746 .num_names = ARRAY_SIZE(predefined_names_S_1_2),
747 .names = predefined_names_S_1_2,
751 .sid = _SID0(3), /* S-1-3 */
752 .num_names = ARRAY_SIZE(predefined_names_S_1_3),
753 .names = predefined_names_S_1_3,
757 .sid = _SID0(3), /* S-1-3 */
758 .num_names = ARRAY_SIZE(predefined_names_S_1_3),
759 .names = predefined_names_S_1_3,
762 * S-1-5 is split here
764 * 'NT Pseudo Domain' has precedence before 'NT AUTHORITY'.
766 * In a LookupSids with multiple sids e.g. S-1-5 and S-1-5-7
767 * the domain section (struct lsa_DomainInfo) gets
768 * 'NT Pseudo Domain' with S-1-5. If asked in reversed order
769 * S-1-5-7 and then S-1-5, you get struct lsa_DomainInfo
770 * with 'NT AUTHORITY' and S-1-5.
773 .domain = "NT Pseudo Domain",
774 .sid = _SID0(5), /* S-1-5 */
775 .num_names = ARRAY_SIZE(predefined_names_S_1_5p),
776 .names = predefined_names_S_1_5p,
779 .domain = "NT AUTHORITY",
780 .sid = _SID0(5), /* S-1-5 */
781 .num_names = ARRAY_SIZE(predefined_names_S_1_5a),
782 .names = predefined_names_S_1_5a,
786 .sid = _SID1(5, 32), /* S-1-5-32 */
787 .num_names = ARRAY_SIZE(predefined_names_S_1_5_32),
788 .names = predefined_names_S_1_5_32,
791 * 'NT AUTHORITY' again with S-1-5-64 this time
794 .domain = "NT AUTHORITY",
795 .sid = _SID1(5, 64), /* S-1-5-64 */
796 .num_names = ARRAY_SIZE(predefined_names_S_1_5_64),
797 .names = predefined_names_S_1_5_64,
800 .domain = "Internet$",
801 .sid = _SID0(7), /* S-1-7 */
802 .num_names = ARRAY_SIZE(predefined_names_S_1_7),
803 .names = predefined_names_S_1_7,
806 .domain = "Mandatory Label",
807 .sid = _SID0(16), /* S-1-16 */
808 .num_names = ARRAY_SIZE(predefined_names_S_1_16),
809 .names = predefined_names_S_1_16,
813 NTSTATUS dom_sid_lookup_predefined_name(const char *name,
814 const struct dom_sid **sid,
815 enum lsa_SidType *type,
816 const struct dom_sid **authority_sid,
817 const char **authority_name)
820 const char *domain = "";
821 size_t domain_len = 0;
826 *type = SID_NAME_UNKNOWN;
827 *authority_sid = NULL;
828 *authority_name = NULL;
834 p = strchr(name, '\\');
837 domain_len = PTR_DIFF(p, domain);
841 match = strequal(name, "");
844 * Strange, but that's what W2012R2 does.
849 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
850 const struct predefined_domain_mapping *d =
851 &predefined_domains[di];
854 if (domain_len != 0) {
857 cmp = strncasecmp(d->domain, domain, domain_len);
863 for (ni = 0; ni < d->num_names; ni++) {
864 const struct predefined_name_mapping *n =
867 match = strequal(n->name, name);
874 *authority_sid = &d->sid;
875 *authority_name = d->domain;
880 return NT_STATUS_NONE_MAPPED;
883 bool dom_sid_lookup_is_predefined_domain(const char *domain)
888 if (domain == NULL) {
892 match = strequal(domain, "");
895 * Strange, but that's what W2012R2 does.
900 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
901 const struct predefined_domain_mapping *d =
902 &predefined_domains[di];
905 cmp = strcasecmp(d->domain, domain);
916 NTSTATUS dom_sid_lookup_predefined_sid(const struct dom_sid *sid,
918 enum lsa_SidType *type,
919 const struct dom_sid **authority_sid,
920 const char **authority_name)
923 bool match_domain = false;
926 *type = SID_NAME_UNKNOWN;
927 *authority_sid = NULL;
928 *authority_name = NULL;
931 return NT_STATUS_INVALID_SID;
934 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
935 const struct predefined_domain_mapping *d =
936 &predefined_domains[di];
940 cmp = dom_sid_compare_auth(&d->sid, sid);
947 for (ni = 0; ni < d->num_names; ni++) {
948 const struct predefined_name_mapping *n =
951 cmp = dom_sid_compare(&n->sid, sid);
958 *authority_sid = &d->sid;
959 *authority_name = d->domain;
965 return NT_STATUS_INVALID_SID;
968 return NT_STATUS_NONE_MAPPED;