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"
35 * Some useful sids, more well known sids can be found at
36 * http://support.microsoft.com/kb/243330/EN-US/
41 const struct dom_sid global_sid_World_Domain = /* Everyone domain */
42 { 1, 0, {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_World = /* Everyone */
45 { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
47 const struct dom_sid global_sid_Local_Authority = /* Local Authority */
48 { 1, 0, {0,0,0,0,0,2}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
50 const struct dom_sid global_sid_Creator_Owner_Domain = /* Creator Owner domain */
51 { 1, 0, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
53 const struct dom_sid global_sid_NT_Authority = /* NT Authority */
54 { 1, 0, {0,0,0,0,0,5}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
56 const struct dom_sid global_sid_System = /* System */
57 { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
59 const struct dom_sid global_sid_NULL = /* NULL sid */
60 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
62 const struct dom_sid global_sid_Authenticated_Users = /* All authenticated rids */
63 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
65 /* for documentation S-1-5-12 */
66 const struct dom_sid global_sid_Restriced = /* Restriced Code */
67 { 1, 1, {0,0,0,0,0,5}, {12,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
71 const struct dom_sid global_sid_Asserted_Identity = /* Asserted Identity */
72 { 1, 0, {0,0,0,0,0,18}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
74 const struct dom_sid global_sid_Asserted_Identity_Service = /* Asserted Identity Service */
75 { 1, 1, {0,0,0,0,0,18}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
77 const struct dom_sid global_sid_Asserted_Identity_Authentication_Authority = /* Asserted Identity Authentication Authority */
78 { 1, 1, {0,0,0,0,0,18}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
81 const struct dom_sid global_sid_Network = /* Network rids */
82 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
85 const struct dom_sid global_sid_Creator_Owner = /* Creator Owner */
86 { 1, 1, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
88 const struct dom_sid global_sid_Creator_Group = /* Creator Group */
89 { 1, 1, {0,0,0,0,0,3}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
91 const struct dom_sid global_sid_Owner_Rights = /* Owner Rights */
92 { 1, 1, {0,0,0,0,0,3}, {4,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
94 const struct dom_sid global_sid_Anonymous = /* Anonymous login */
95 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
97 const struct dom_sid global_sid_Enterprise_DCs = /* Enterprise DCs */
98 { 1, 1, {0,0,0,0,0,5}, {9,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
100 const struct dom_sid global_sid_Builtin = /* Local well-known domain */
101 { 1, 1, {0,0,0,0,0,5}, {32,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
103 const struct dom_sid global_sid_Builtin_Administrators = /* Builtin administrators */
104 { 1, 2, {0,0,0,0,0,5}, {32,544,0,0,0,0,0,0,0,0,0,0,0,0,0}};
106 const struct dom_sid global_sid_Builtin_Users = /* Builtin users */
107 { 1, 2, {0,0,0,0,0,5}, {32,545,0,0,0,0,0,0,0,0,0,0,0,0,0}};
109 const struct dom_sid global_sid_Builtin_Guests = /* Builtin guest users */
110 { 1, 2, {0,0,0,0,0,5}, {32,546,0,0,0,0,0,0,0,0,0,0,0,0,0}};
112 const struct dom_sid global_sid_Builtin_Power_Users = /* Builtin power users */
113 { 1, 2, {0,0,0,0,0,5}, {32,547,0,0,0,0,0,0,0,0,0,0,0,0,0}};
115 const struct dom_sid global_sid_Builtin_Account_Operators = /* Builtin account operators */
116 { 1, 2, {0,0,0,0,0,5}, {32,548,0,0,0,0,0,0,0,0,0,0,0,0,0}};
118 const struct dom_sid global_sid_Builtin_Server_Operators = /* Builtin server operators */
119 { 1, 2, {0,0,0,0,0,5}, {32,549,0,0,0,0,0,0,0,0,0,0,0,0,0}};
121 const struct dom_sid global_sid_Builtin_Print_Operators = /* Builtin print operators */
122 { 1, 2, {0,0,0,0,0,5}, {32,550,0,0,0,0,0,0,0,0,0,0,0,0,0}};
124 const struct dom_sid global_sid_Builtin_Backup_Operators = /* Builtin backup operators */
125 { 1, 2, {0,0,0,0,0,5}, {32,551,0,0,0,0,0,0,0,0,0,0,0,0,0}};
127 const struct dom_sid global_sid_Builtin_Replicator = /* Builtin replicator */
128 { 1, 2, {0,0,0,0,0,5}, {32,552,0,0,0,0,0,0,0,0,0,0,0,0,0}};
130 const struct dom_sid global_sid_Builtin_PreWin2kAccess = /* Builtin pre win2k access */
131 { 1, 2, {0,0,0,0,0,5}, {32,554,0,0,0,0,0,0,0,0,0,0,0,0,0}};
134 const struct dom_sid global_sid_Unix_Users = /* Unmapped Unix users */
135 { 1, 1, {0,0,0,0,0,22}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
137 const struct dom_sid global_sid_Unix_Groups = /* Unmapped Unix groups */
138 { 1, 1, {0,0,0,0,0,22}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
141 * http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
144 const struct dom_sid global_sid_Unix_NFS = /* MS NFS and Apple style */
145 { 1, 1, {0,0,0,0,0,5}, {88,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
147 const struct dom_sid global_sid_Unix_NFS_Users = /* Unix uid, MS NFS and Apple style */
148 { 1, 2, {0,0,0,0,0,5}, {88,1,0,0,0,0,0,0,0,0,0,0,0,0,0}};
150 const struct dom_sid global_sid_Unix_NFS_Groups = /* Unix gid, MS NFS and Apple style */
151 { 1, 2, {0,0,0,0,0,5}, {88,2,0,0,0,0,0,0,0,0,0,0,0,0,0}};
153 const struct dom_sid global_sid_Unix_NFS_Mode = /* Unix mode */
154 { 1, 2, {0,0,0,0,0,5}, {88,3,0,0,0,0,0,0,0,0,0,0,0,0,0}};
155 /* Unused, left here for documentary purposes */
157 const struct dom_sid global_sid_Unix_NFS_Other = /* Unix other, MS NFS and Apple style */
158 { 1, 2, {0,0,0,0,0,5}, {88,4,0,0,0,0,0,0,0,0,0,0,0,0,0}};
161 /* Unused, left here for documentary purposes */
163 #define SECURITY_NULL_SID_AUTHORITY 0
164 #define SECURITY_WORLD_SID_AUTHORITY 1
165 #define SECURITY_LOCAL_SID_AUTHORITY 2
166 #define SECURITY_CREATOR_SID_AUTHORITY 3
167 #define SECURITY_NT_AUTHORITY 5
170 static struct dom_sid system_sid_array[1] =
171 { { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
172 static const struct security_token system_token = {
173 .num_sids = ARRAY_SIZE(system_sid_array),
174 .sids = system_sid_array,
175 .privilege_mask = SE_ALL_PRIVS
178 /****************************************************************************
179 Lookup string names for SID types.
180 ****************************************************************************/
182 static const struct {
183 enum lsa_SidType sid_type;
185 } sid_name_type[] = {
186 {SID_NAME_USE_NONE, "None"},
187 {SID_NAME_USER, "User"},
188 {SID_NAME_DOM_GRP, "Domain Group"},
189 {SID_NAME_DOMAIN, "Domain"},
190 {SID_NAME_ALIAS, "Local Group"},
191 {SID_NAME_WKN_GRP, "Well-known Group"},
192 {SID_NAME_DELETED, "Deleted Account"},
193 {SID_NAME_INVALID, "Invalid Account"},
194 {SID_NAME_UNKNOWN, "UNKNOWN"},
195 {SID_NAME_COMPUTER, "Computer"},
196 {SID_NAME_LABEL, "Mandatory Label"}
199 const char *sid_type_lookup(uint32_t sid_type)
203 /* Look through list */
204 for (i=0; i < ARRAY_SIZE(sid_name_type); i++) {
205 if (sid_name_type[i].sid_type == sid_type) {
206 return sid_name_type[i].string;
211 return "SID *TYPE* is INVALID";
214 /**************************************************************************
215 Create the SYSTEM token.
216 ***************************************************************************/
218 const struct security_token *get_system_token(void)
220 return &system_token;
223 bool sid_compose(struct dom_sid *dst, const struct dom_sid *domain_sid, uint32_t rid)
225 sid_copy(dst, domain_sid);
226 return sid_append_rid(dst, rid);
229 /*****************************************************************
230 Removes the last rid from the end of a sid
231 *****************************************************************/
233 bool sid_split_rid(struct dom_sid *sid, uint32_t *rid)
235 if (sid->num_auths > 0) {
238 *rid = sid->sub_auths[sid->num_auths];
245 /*****************************************************************
246 Return the last rid from the end of a sid
247 *****************************************************************/
249 bool sid_peek_rid(const struct dom_sid *sid, uint32_t *rid)
254 if (sid->num_auths > 0) {
255 *rid = sid->sub_auths[sid->num_auths - 1];
261 /*****************************************************************
262 Return the last rid from the end of a sid
263 and check the sid against the exp_dom_sid
264 *****************************************************************/
266 bool sid_peek_check_rid(const struct dom_sid *exp_dom_sid, const struct dom_sid *sid, uint32_t *rid)
268 if (!exp_dom_sid || !sid || !rid)
271 if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
275 if (sid_compare_domain(exp_dom_sid, sid)!=0){
280 return sid_peek_rid(sid, rid);
283 /*****************************************************************
285 *****************************************************************/
287 void sid_copy(struct dom_sid *dst, const struct dom_sid *src)
291 *dst = (struct dom_sid) {
292 .sid_rev_num = src->sid_rev_num,
293 .num_auths = src->num_auths,
296 memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
298 for (i = 0; i < src->num_auths; i++)
299 dst->sub_auths[i] = src->sub_auths[i];
302 /*****************************************************************
303 Parse a on-the-wire SID to a struct dom_sid.
304 *****************************************************************/
306 ssize_t sid_parse(const uint8_t *inbuf, size_t len, struct dom_sid *sid)
308 DATA_BLOB in = data_blob_const(inbuf, len);
309 enum ndr_err_code ndr_err;
311 ndr_err = ndr_pull_struct_blob_all(
312 &in, NULL, sid, (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
313 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
316 return ndr_size_dom_sid(sid, 0);
319 /*****************************************************************
320 See if 2 SIDs are in the same domain
321 this just compares the leading sub-auths
322 *****************************************************************/
324 int sid_compare_domain(const struct dom_sid *sid1, const struct dom_sid *sid2)
328 n = MIN(sid1->num_auths, sid2->num_auths);
330 for (i = n-1; i >= 0; --i)
331 if (sid1->sub_auths[i] != sid2->sub_auths[i])
332 return sid1->sub_auths[i] - sid2->sub_auths[i];
334 return dom_sid_compare_auth(sid1, sid2);
337 /********************************************************************
338 Add SID to an array SIDs
339 ********************************************************************/
341 NTSTATUS add_sid_to_array(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
342 struct dom_sid **sids, uint32_t *num)
346 if ((*num) == UINT32_MAX) {
347 return NT_STATUS_INTEGER_OVERFLOW;
350 tmp = talloc_realloc(mem_ctx, *sids, struct dom_sid, (*num)+1);
353 return NT_STATUS_NO_MEMORY;
357 sid_copy(&((*sids)[*num]), sid);
364 /********************************************************************
365 Add SID to an array SIDs ensuring that it is not already there
366 ********************************************************************/
368 NTSTATUS add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
369 struct dom_sid **sids, uint32_t *num_sids)
373 for (i=0; i<(*num_sids); i++) {
374 if (dom_sid_equal(sid, &(*sids)[i])) {
379 return add_sid_to_array(mem_ctx, sid, sids, num_sids);
382 /********************************************************************
383 Remove SID from an array
384 ********************************************************************/
386 void del_sid_from_array(const struct dom_sid *sid, struct dom_sid **sids,
389 struct dom_sid *sid_list = *sids;
392 for ( i=0; i<*num; i++ ) {
394 /* if we find the SID, then decrement the count
395 and break out of the loop */
397 if (dom_sid_equal(sid, &sid_list[i])) {
403 /* This loop will copy the remainder of the array
404 if i < num of sids in the array */
406 for ( ; i<*num; i++ ) {
407 sid_copy( &sid_list[i], &sid_list[i+1] );
413 bool add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
414 uint32_t rid, uint32_t **pp_rids, size_t *p_num)
418 for (i=0; i<*p_num; i++) {
419 if ((*pp_rids)[i] == rid)
423 *pp_rids = talloc_realloc(mem_ctx, *pp_rids, uint32_t, *p_num+1);
425 if (*pp_rids == NULL) {
430 (*pp_rids)[*p_num] = rid;
435 bool is_null_sid(const struct dom_sid *sid)
437 const struct dom_sid null_sid = {0};
438 return dom_sid_equal(sid, &null_sid);
442 * See [MS-LSAT] 3.1.1.1.1 Predefined Translation Database and Corresponding View
444 struct predefined_name_mapping {
446 enum lsa_SidType type;
450 struct predefined_domain_mapping {
454 const struct predefined_name_mapping *names;
457 /* S-1-${AUTHORITY} */
458 #define _SID0(authority) \
459 { 1, 0, {0,0,0,0,0,authority}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
460 /* S-1-${AUTHORITY}-${SUB1} */
461 #define _SID1(authority,sub1) \
462 { 1, 1, {0,0,0,0,0,authority}, {sub1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
463 /* S-1-${AUTHORITY}-${SUB1}-${SUB2} */
464 #define _SID2(authority,sub1,sub2) \
465 { 1, 2, {0,0,0,0,0,authority}, {sub1,sub2,0,0,0,0,0,0,0,0,0,0,0,0,0}}
470 static const struct predefined_name_mapping predefined_names_S_1_0[] = {
473 .type = SID_NAME_WKN_GRP,
474 .sid = _SID1(0, 0), /* S-1-0-0 */
481 static const struct predefined_name_mapping predefined_names_S_1_1[] = {
484 .type = SID_NAME_WKN_GRP,
485 .sid = _SID1(1, 0), /* S-1-1-0 */
492 static const struct predefined_name_mapping predefined_names_S_1_2[] = {
495 .type = SID_NAME_WKN_GRP,
496 .sid = _SID1(2, 0), /* S-1-2-0 */
503 static const struct predefined_name_mapping predefined_names_S_1_3[] = {
505 .name = "CREATOR OWNER",
506 .type = SID_NAME_WKN_GRP,
507 .sid = _SID1(3, 0), /* S-1-3-0 */
510 .name = "CREATOR GROUP",
511 .type = SID_NAME_WKN_GRP,
512 .sid = _SID1(3, 1), /* S-1-3-1 */
515 .name = "CREATOR OWNER SERVER",
516 .type = SID_NAME_WKN_GRP,
517 .sid = _SID1(3, 0), /* S-1-3-2 */
520 .name = "CREATOR GROUP SERVER",
521 .type = SID_NAME_WKN_GRP,
522 .sid = _SID1(3, 1), /* S-1-3-3 */
525 .name = "OWNER RIGHTS",
526 .type = SID_NAME_WKN_GRP,
527 .sid = _SID1(3, 4), /* S-1-3-4 */
532 * S-1-5 only 'NT Pseudo Domain'
534 static const struct predefined_name_mapping predefined_names_S_1_5p[] = {
536 .name = "NT Pseudo Domain",
537 .type = SID_NAME_DOMAIN,
538 .sid = _SID0(5), /* S-1-5 */
543 * S-1-5 'NT AUTHORITY'
545 static const struct predefined_name_mapping predefined_names_S_1_5a[] = {
548 .type = SID_NAME_WKN_GRP,
549 .sid = _SID1(5, 1), /* S-1-5-1 */
553 .type = SID_NAME_WKN_GRP,
554 .sid = _SID1(5, 2), /* S-1-5-2 */
558 .type = SID_NAME_WKN_GRP,
559 .sid = _SID1(5, 3), /* S-1-5-3 */
562 .name = "INTERACTIVE",
563 .type = SID_NAME_WKN_GRP,
564 .sid = _SID1(5, 4), /* S-1-5-4 */
568 .type = SID_NAME_WKN_GRP,
569 .sid = _SID1(5, 6), /* S-1-5-6 */
572 .name = "ANONYMOUS LOGON",
573 .type = SID_NAME_WKN_GRP,
574 .sid = _SID1(5, 7), /* S-1-5-7 */
578 .type = SID_NAME_WKN_GRP,
579 .sid = _SID1(5, 8), /* S-1-5-8 */
582 .name = "ENTERPRISE DOMAIN CONTROLLERS",
583 .type = SID_NAME_WKN_GRP,
584 .sid = _SID1(5, 9), /* S-1-5-9 */
588 .type = SID_NAME_WKN_GRP,
589 .sid = _SID1(5, 10), /* S-1-5-10 */
592 .name = "Authenticated Users",
593 .type = SID_NAME_WKN_GRP,
594 .sid = _SID1(5, 11), /* S-1-5-11 */
597 .name = "RESTRICTED",
598 .type = SID_NAME_WKN_GRP,
599 .sid = _SID1(5, 12), /* S-1-5-12 */
602 .name = "TERMINAL SERVER USER",
603 .type = SID_NAME_WKN_GRP,
604 .sid = _SID1(5, 13), /* S-1-5-13 */
607 .name = "REMOTE INTERACTIVE LOGON",
608 .type = SID_NAME_WKN_GRP,
609 .sid = _SID1(5, 14), /* S-1-5-14 */
612 .name = "This Organization",
613 .type = SID_NAME_WKN_GRP,
614 .sid = _SID1(5, 15), /* S-1-5-15 */
618 .type = SID_NAME_WKN_GRP,
619 .sid = _SID1(5, 17), /* S-1-5-17 */
623 .type = SID_NAME_WKN_GRP,
624 .sid = _SID1(5, 18), /* S-1-5-18 */
627 .name = "LOCAL SERVICE",
628 .type = SID_NAME_WKN_GRP,
629 .sid = _SID1(5, 19), /* S-1-5-19 */
632 .name = "NETWORK SERVICE",
633 .type = SID_NAME_WKN_GRP,
634 .sid = _SID1(5, 20), /* S-1-5-20 */
637 .name = "WRITE RESTRICTED",
638 .type = SID_NAME_WKN_GRP,
639 .sid = _SID1(5, 33), /* S-1-5-33 */
642 .name = "Other Organization",
643 .type = SID_NAME_WKN_GRP,
644 .sid = _SID1(5, 1000), /* S-1-5-1000 */
651 static const struct predefined_name_mapping predefined_names_S_1_5_32[] = {
654 .type = SID_NAME_DOMAIN,
655 .sid = _SID1(5, 32), /* S-1-5-32 */
662 static const struct predefined_name_mapping predefined_names_S_1_5_64[] = {
664 .name = "NTLM Authentication",
665 .type = SID_NAME_WKN_GRP,
666 .sid = _SID2(5, 64, 10), /* S-1-5-64-10 */
669 .name = "SChannel Authentication",
670 .type = SID_NAME_WKN_GRP,
671 .sid = _SID2(5, 64, 14), /* S-1-5-64-14 */
674 .name = "Digest Authentication",
675 .type = SID_NAME_WKN_GRP,
676 .sid = _SID2(5, 64, 21), /* S-1-5-64-21 */
683 static const struct predefined_name_mapping predefined_names_S_1_7[] = {
686 .type = SID_NAME_DOMAIN,
687 .sid = _SID0(7), /* S-1-7 */
694 static const struct predefined_name_mapping predefined_names_S_1_16[] = {
696 .name = "Mandatory Label",
697 .type = SID_NAME_DOMAIN,
698 .sid = _SID0(16), /* S-1-16 */
701 .name = "Untrusted Mandatory Level",
702 .type = SID_NAME_LABEL,
703 .sid = _SID1(16, 0), /* S-1-16-0 */
706 .name = "Low Mandatory Level",
707 .type = SID_NAME_LABEL,
708 .sid = _SID1(16, 4096), /* S-1-16-4096 */
711 .name = "Medium Mandatory Level",
712 .type = SID_NAME_LABEL,
713 .sid = _SID1(16, 8192), /* S-1-16-8192 */
716 .name = "High Mandatory Level",
717 .type = SID_NAME_LABEL,
718 .sid = _SID1(16, 12288), /* S-1-16-12288 */
721 .name = "System Mandatory Level",
722 .type = SID_NAME_LABEL,
723 .sid = _SID1(16, 16384), /* S-1-16-16384 */
726 .name = "Protected Process Mandatory Level",
727 .type = SID_NAME_LABEL,
728 .sid = _SID1(16, 20480), /* S-1-16-20480 */
732 static const struct predefined_domain_mapping predefined_domains[] = {
735 .sid = _SID0(0), /* S-1-0 */
736 .num_names = ARRAY_SIZE(predefined_names_S_1_0),
737 .names = predefined_names_S_1_0,
741 .sid = _SID0(1), /* S-1-1 */
742 .num_names = ARRAY_SIZE(predefined_names_S_1_1),
743 .names = predefined_names_S_1_1,
747 .sid = _SID0(2), /* S-1-2 */
748 .num_names = ARRAY_SIZE(predefined_names_S_1_2),
749 .names = predefined_names_S_1_2,
753 .sid = _SID0(3), /* S-1-3 */
754 .num_names = ARRAY_SIZE(predefined_names_S_1_3),
755 .names = predefined_names_S_1_3,
759 .sid = _SID0(3), /* S-1-3 */
760 .num_names = ARRAY_SIZE(predefined_names_S_1_3),
761 .names = predefined_names_S_1_3,
764 * S-1-5 is split here
766 * 'NT Pseudo Domain' has precedence before 'NT AUTHORITY'.
768 * In a LookupSids with multiple sids e.g. S-1-5 and S-1-5-7
769 * the domain section (struct lsa_DomainInfo) gets
770 * 'NT Pseudo Domain' with S-1-5. If asked in reversed order
771 * S-1-5-7 and then S-1-5, you get struct lsa_DomainInfo
772 * with 'NT AUTHORITY' and S-1-5.
775 .domain = "NT Pseudo Domain",
776 .sid = _SID0(5), /* S-1-5 */
777 .num_names = ARRAY_SIZE(predefined_names_S_1_5p),
778 .names = predefined_names_S_1_5p,
781 .domain = "NT AUTHORITY",
782 .sid = _SID0(5), /* S-1-5 */
783 .num_names = ARRAY_SIZE(predefined_names_S_1_5a),
784 .names = predefined_names_S_1_5a,
788 .sid = _SID1(5, 32), /* S-1-5-32 */
789 .num_names = ARRAY_SIZE(predefined_names_S_1_5_32),
790 .names = predefined_names_S_1_5_32,
793 * 'NT AUTHORITY' again with S-1-5-64 this time
796 .domain = "NT AUTHORITY",
797 .sid = _SID1(5, 64), /* S-1-5-64 */
798 .num_names = ARRAY_SIZE(predefined_names_S_1_5_64),
799 .names = predefined_names_S_1_5_64,
802 .domain = "Internet$",
803 .sid = _SID0(7), /* S-1-7 */
804 .num_names = ARRAY_SIZE(predefined_names_S_1_7),
805 .names = predefined_names_S_1_7,
808 .domain = "Mandatory Label",
809 .sid = _SID0(16), /* S-1-16 */
810 .num_names = ARRAY_SIZE(predefined_names_S_1_16),
811 .names = predefined_names_S_1_16,
815 NTSTATUS dom_sid_lookup_predefined_name(const char *name,
816 const struct dom_sid **sid,
817 enum lsa_SidType *type,
818 const struct dom_sid **authority_sid,
819 const char **authority_name)
822 const char *domain = "";
823 size_t domain_len = 0;
828 *type = SID_NAME_UNKNOWN;
829 *authority_sid = NULL;
830 *authority_name = NULL;
836 p = strchr(name, '\\');
839 domain_len = PTR_DIFF(p, domain);
843 match = strequal(name, "");
846 * Strange, but that's what W2012R2 does.
851 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
852 const struct predefined_domain_mapping *d =
853 &predefined_domains[di];
856 if (domain_len != 0) {
859 cmp = strncasecmp(d->domain, domain, domain_len);
865 for (ni = 0; ni < d->num_names; ni++) {
866 const struct predefined_name_mapping *n =
869 match = strequal(n->name, name);
876 *authority_sid = &d->sid;
877 *authority_name = d->domain;
882 return NT_STATUS_NONE_MAPPED;
885 bool dom_sid_lookup_is_predefined_domain(const char *domain)
890 if (domain == NULL) {
894 match = strequal(domain, "");
897 * Strange, but that's what W2012R2 does.
902 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
903 const struct predefined_domain_mapping *d =
904 &predefined_domains[di];
907 cmp = strcasecmp(d->domain, domain);
918 NTSTATUS dom_sid_lookup_predefined_sid(const struct dom_sid *sid,
920 enum lsa_SidType *type,
921 const struct dom_sid **authority_sid,
922 const char **authority_name)
925 bool match_domain = false;
928 *type = SID_NAME_UNKNOWN;
929 *authority_sid = NULL;
930 *authority_name = NULL;
933 return NT_STATUS_INVALID_SID;
936 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
937 const struct predefined_domain_mapping *d =
938 &predefined_domains[di];
942 cmp = dom_sid_compare_auth(&d->sid, sid);
949 for (ni = 0; ni < d->num_names; ni++) {
950 const struct predefined_name_mapping *n =
953 cmp = dom_sid_compare(&n->sid, sid);
960 *authority_sid = &d->sid;
961 *authority_name = d->domain;
967 return NT_STATUS_INVALID_SID;
970 return NT_STATUS_NONE_MAPPED;