Revert "ctdb-tests: Don't bother shutting down daemons in ctdb_init()"
[Samba.git] / libcli / security / util_sid.c
blobe47ed04c30f8b69af02a348b0e305803871e577d
1 /*
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/>.
26 #include "includes.h"
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/
37 /* S-1-1 */
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}};
40 /* S-1-1-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}};
43 /* S-1-2 */
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}};
46 /* S-1-3 */
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}};
49 /* S-1-5 */
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}};
52 /* S-1-5-18 */
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}};
55 /* S-1-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}};
58 /* S-1-5-11 */
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}};
61 #if 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}};
65 #endif
67 /* S-1-18 */
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}};
70 /* S-1-18-1 */
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}};
73 /* S-1-18-2 */
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}};
77 /* S-1-5-2 */
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}};
81 /* S-1-3 */
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}};
84 /* S-1-3-1 */
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}};
87 /* S-1-3-4 */
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}};
90 /* S-1-5-7 */
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}};
93 /* S-1-5-9 */
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}};
96 /* S-1-5-32 */
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}};
99 /* S-1-5-32-544 */
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}};
102 /* S-1-5-32-545 */
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}};
105 /* S-1-5-32-546 */
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}};
108 /* S-1-5-32-547 */
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}};
111 /* S-1-5-32-548 */
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}};
114 /* S-1-5-32-549 */
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}};
117 /* S-1-5-32-550 */
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}};
120 /* S-1-5-32-551 */
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}};
123 /* S-1-5-32-552 */
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}};
126 /* S-1-5-32-554 */
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}};
130 /* S-1-22-1 */
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}};
133 /* S-1-22-2 */
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
140 /* S-1-5-88 */
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}};
143 /* S-1-5-88-1 */
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}};
146 /* S-1-5-88-2 */
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}};
149 /* S-1-5-88-3 */
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 */
153 #if 0
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}};
156 #endif
158 /* Unused, left here for documentary purposes */
159 #if 0
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
165 #endif
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;
181 const char *string;
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)
198 size_t i;
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;
207 /* Default return */
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) {
233 sid->num_auths--;
234 if (rid != NULL) {
235 *rid = sid->sub_auths[sid->num_auths];
237 return true;
239 return false;
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)
248 if (!sid || !rid)
249 return false;
251 if (sid->num_auths > 0) {
252 *rid = sid->sub_auths[sid->num_auths - 1];
253 return true;
255 return false;
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)
266 return false;
268 if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
269 return false;
272 if (sid_compare_domain(exp_dom_sid, sid)!=0){
273 *rid=(-1);
274 return false;
277 return sid_peek_rid(sid, rid);
280 /*****************************************************************
281 Copies a sid
282 *****************************************************************/
284 void sid_copy(struct dom_sid *dst, const struct dom_sid *src)
286 int i;
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 ssize_t sid_parse(const uint8_t *inbuf, size_t len, struct dom_sid *sid)
305 DATA_BLOB in = data_blob_const(inbuf, len);
306 enum ndr_err_code ndr_err;
308 ndr_err = ndr_pull_struct_blob_all(
309 &in, NULL, sid, (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
310 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
311 return -1;
313 return ndr_size_dom_sid(sid, 0);
316 /*****************************************************************
317 See if 2 SIDs are in the same domain
318 this just compares the leading sub-auths
319 *****************************************************************/
321 int sid_compare_domain(const struct dom_sid *sid1, const struct dom_sid *sid2)
323 int n, i;
325 n = MIN(sid1->num_auths, sid2->num_auths);
327 for (i = n-1; i >= 0; --i)
328 if (sid1->sub_auths[i] != sid2->sub_auths[i])
329 return sid1->sub_auths[i] - sid2->sub_auths[i];
331 return dom_sid_compare_auth(sid1, sid2);
334 /********************************************************************
335 Add SID to an array SIDs
336 ********************************************************************/
338 NTSTATUS add_sid_to_array(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
339 struct dom_sid **sids, uint32_t *num)
341 struct dom_sid *tmp;
343 if ((*num) == UINT32_MAX) {
344 return NT_STATUS_INTEGER_OVERFLOW;
347 tmp = talloc_realloc(mem_ctx, *sids, struct dom_sid, (*num)+1);
348 if (tmp == NULL) {
349 *num = 0;
350 return NT_STATUS_NO_MEMORY;
352 *sids = tmp;
354 sid_copy(&((*sids)[*num]), sid);
355 *num += 1;
357 return NT_STATUS_OK;
361 /********************************************************************
362 Add SID to an array SIDs ensuring that it is not already there
363 ********************************************************************/
365 NTSTATUS add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
366 struct dom_sid **sids, uint32_t *num_sids)
368 uint32_t i;
370 for (i=0; i<(*num_sids); i++) {
371 if (dom_sid_equal(sid, &(*sids)[i])) {
372 return NT_STATUS_OK;
376 return add_sid_to_array(mem_ctx, sid, sids, num_sids);
379 /********************************************************************
380 Remove SID from an array
381 ********************************************************************/
383 void del_sid_from_array(const struct dom_sid *sid, struct dom_sid **sids,
384 uint32_t *num)
386 struct dom_sid *sid_list = *sids;
387 uint32_t i;
389 for ( i=0; i<*num; i++ ) {
391 /* if we find the SID, then decrement the count
392 and break out of the loop */
394 if (dom_sid_equal(sid, &sid_list[i])) {
395 *num -= 1;
396 break;
400 /* This loop will copy the remainder of the array
401 if i < num of sids in the array */
403 for ( ; i<*num; i++ ) {
404 sid_copy( &sid_list[i], &sid_list[i+1] );
407 return;
410 bool add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
411 uint32_t rid, uint32_t **pp_rids, size_t *p_num)
413 size_t i;
415 for (i=0; i<*p_num; i++) {
416 if ((*pp_rids)[i] == rid)
417 return true;
420 *pp_rids = talloc_realloc(mem_ctx, *pp_rids, uint32_t, *p_num+1);
422 if (*pp_rids == NULL) {
423 *p_num = 0;
424 return false;
427 (*pp_rids)[*p_num] = rid;
428 *p_num += 1;
429 return true;
432 bool is_null_sid(const struct dom_sid *sid)
434 const struct dom_sid null_sid = {0};
435 return dom_sid_equal(sid, &null_sid);
439 * See [MS-LSAT] 3.1.1.1.1 Predefined Translation Database and Corresponding View
441 struct predefined_name_mapping {
442 const char *name;
443 enum lsa_SidType type;
444 struct dom_sid sid;
447 struct predefined_domain_mapping {
448 const char *domain;
449 struct dom_sid sid;
450 size_t num_names;
451 const struct predefined_name_mapping *names;
454 /* S-1-${AUTHORITY} */
455 #define _SID0(authority) \
456 { 1, 0, {0,0,0,0,0,authority}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
457 /* S-1-${AUTHORITY}-${SUB1} */
458 #define _SID1(authority,sub1) \
459 { 1, 1, {0,0,0,0,0,authority}, {sub1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
460 /* S-1-${AUTHORITY}-${SUB1}-${SUB2} */
461 #define _SID2(authority,sub1,sub2) \
462 { 1, 2, {0,0,0,0,0,authority}, {sub1,sub2,0,0,0,0,0,0,0,0,0,0,0,0,0}}
465 * S-1-0
467 static const struct predefined_name_mapping predefined_names_S_1_0[] = {
469 .name = "NULL SID",
470 .type = SID_NAME_WKN_GRP,
471 .sid = _SID1(0, 0), /* S-1-0-0 */
476 * S-1-1
478 static const struct predefined_name_mapping predefined_names_S_1_1[] = {
480 .name = "Everyone",
481 .type = SID_NAME_WKN_GRP,
482 .sid = _SID1(1, 0), /* S-1-1-0 */
487 * S-1-2
489 static const struct predefined_name_mapping predefined_names_S_1_2[] = {
491 .name = "LOCAL",
492 .type = SID_NAME_WKN_GRP,
493 .sid = _SID1(2, 0), /* S-1-2-0 */
498 * S-1-3
500 static const struct predefined_name_mapping predefined_names_S_1_3[] = {
502 .name = "CREATOR OWNER",
503 .type = SID_NAME_WKN_GRP,
504 .sid = _SID1(3, 0), /* S-1-3-0 */
507 .name = "CREATOR GROUP",
508 .type = SID_NAME_WKN_GRP,
509 .sid = _SID1(3, 1), /* S-1-3-1 */
512 .name = "CREATOR OWNER SERVER",
513 .type = SID_NAME_WKN_GRP,
514 .sid = _SID1(3, 0), /* S-1-3-2 */
517 .name = "CREATOR GROUP SERVER",
518 .type = SID_NAME_WKN_GRP,
519 .sid = _SID1(3, 1), /* S-1-3-3 */
522 .name = "OWNER RIGHTS",
523 .type = SID_NAME_WKN_GRP,
524 .sid = _SID1(3, 4), /* S-1-3-4 */
529 * S-1-5 only 'NT Pseudo Domain'
531 static const struct predefined_name_mapping predefined_names_S_1_5p[] = {
533 .name = "NT Pseudo Domain",
534 .type = SID_NAME_DOMAIN,
535 .sid = _SID0(5), /* S-1-5 */
540 * S-1-5 'NT AUTHORITY'
542 static const struct predefined_name_mapping predefined_names_S_1_5a[] = {
544 .name = "DIALUP",
545 .type = SID_NAME_WKN_GRP,
546 .sid = _SID1(5, 1), /* S-1-5-1 */
549 .name = "NETWORK",
550 .type = SID_NAME_WKN_GRP,
551 .sid = _SID1(5, 2), /* S-1-5-2 */
554 .name = "BATCH",
555 .type = SID_NAME_WKN_GRP,
556 .sid = _SID1(5, 3), /* S-1-5-3 */
559 .name = "INTERACTIVE",
560 .type = SID_NAME_WKN_GRP,
561 .sid = _SID1(5, 4), /* S-1-5-4 */
564 .name = "SERVICE",
565 .type = SID_NAME_WKN_GRP,
566 .sid = _SID1(5, 6), /* S-1-5-6 */
569 .name = "ANONYMOUS LOGON",
570 .type = SID_NAME_WKN_GRP,
571 .sid = _SID1(5, 7), /* S-1-5-7 */
574 .name = "PROXY",
575 .type = SID_NAME_WKN_GRP,
576 .sid = _SID1(5, 8), /* S-1-5-8 */
579 .name = "ENTERPRISE DOMAIN CONTROLLERS",
580 .type = SID_NAME_WKN_GRP,
581 .sid = _SID1(5, 9), /* S-1-5-9 */
584 .name = "SELF",
585 .type = SID_NAME_WKN_GRP,
586 .sid = _SID1(5, 10), /* S-1-5-10 */
589 .name = "Authenticated Users",
590 .type = SID_NAME_WKN_GRP,
591 .sid = _SID1(5, 11), /* S-1-5-11 */
594 .name = "RESTRICTED",
595 .type = SID_NAME_WKN_GRP,
596 .sid = _SID1(5, 12), /* S-1-5-12 */
599 .name = "TERMINAL SERVER USER",
600 .type = SID_NAME_WKN_GRP,
601 .sid = _SID1(5, 13), /* S-1-5-13 */
604 .name = "REMOTE INTERACTIVE LOGON",
605 .type = SID_NAME_WKN_GRP,
606 .sid = _SID1(5, 14), /* S-1-5-14 */
609 .name = "This Organization",
610 .type = SID_NAME_WKN_GRP,
611 .sid = _SID1(5, 15), /* S-1-5-15 */
614 .name = "IUSR",
615 .type = SID_NAME_WKN_GRP,
616 .sid = _SID1(5, 17), /* S-1-5-17 */
619 .name = "SYSTEM",
620 .type = SID_NAME_WKN_GRP,
621 .sid = _SID1(5, 18), /* S-1-5-18 */
624 .name = "LOCAL SERVICE",
625 .type = SID_NAME_WKN_GRP,
626 .sid = _SID1(5, 19), /* S-1-5-19 */
629 .name = "NETWORK SERVICE",
630 .type = SID_NAME_WKN_GRP,
631 .sid = _SID1(5, 20), /* S-1-5-20 */
634 .name = "WRITE RESTRICTED",
635 .type = SID_NAME_WKN_GRP,
636 .sid = _SID1(5, 33), /* S-1-5-33 */
639 .name = "Other Organization",
640 .type = SID_NAME_WKN_GRP,
641 .sid = _SID1(5, 1000), /* S-1-5-1000 */
646 * S-1-5-32
648 static const struct predefined_name_mapping predefined_names_S_1_5_32[] = {
650 .name = "BUILTIN",
651 .type = SID_NAME_DOMAIN,
652 .sid = _SID1(5, 32), /* S-1-5-32 */
657 * S-1-5-64
659 static const struct predefined_name_mapping predefined_names_S_1_5_64[] = {
661 .name = "NTLM Authentication",
662 .type = SID_NAME_WKN_GRP,
663 .sid = _SID2(5, 64, 10), /* S-1-5-64-10 */
666 .name = "SChannel Authentication",
667 .type = SID_NAME_WKN_GRP,
668 .sid = _SID2(5, 64, 14), /* S-1-5-64-14 */
671 .name = "Digest Authentication",
672 .type = SID_NAME_WKN_GRP,
673 .sid = _SID2(5, 64, 21), /* S-1-5-64-21 */
678 * S-1-7
680 static const struct predefined_name_mapping predefined_names_S_1_7[] = {
682 .name = "Internet$",
683 .type = SID_NAME_DOMAIN,
684 .sid = _SID0(7), /* S-1-7 */
689 * S-1-16
691 static const struct predefined_name_mapping predefined_names_S_1_16[] = {
693 .name = "Mandatory Label",
694 .type = SID_NAME_DOMAIN,
695 .sid = _SID0(16), /* S-1-16 */
698 .name = "Untrusted Mandatory Level",
699 .type = SID_NAME_LABEL,
700 .sid = _SID1(16, 0), /* S-1-16-0 */
703 .name = "Low Mandatory Level",
704 .type = SID_NAME_LABEL,
705 .sid = _SID1(16, 4096), /* S-1-16-4096 */
708 .name = "Medium Mandatory Level",
709 .type = SID_NAME_LABEL,
710 .sid = _SID1(16, 8192), /* S-1-16-8192 */
713 .name = "High Mandatory Level",
714 .type = SID_NAME_LABEL,
715 .sid = _SID1(16, 12288), /* S-1-16-12288 */
718 .name = "System Mandatory Level",
719 .type = SID_NAME_LABEL,
720 .sid = _SID1(16, 16384), /* S-1-16-16384 */
723 .name = "Protected Process Mandatory Level",
724 .type = SID_NAME_LABEL,
725 .sid = _SID1(16, 20480), /* S-1-16-20480 */
729 static const struct predefined_domain_mapping predefined_domains[] = {
731 .domain = "",
732 .sid = _SID0(0), /* S-1-0 */
733 .num_names = ARRAY_SIZE(predefined_names_S_1_0),
734 .names = predefined_names_S_1_0,
737 .domain = "",
738 .sid = _SID0(1), /* S-1-1 */
739 .num_names = ARRAY_SIZE(predefined_names_S_1_1),
740 .names = predefined_names_S_1_1,
743 .domain = "",
744 .sid = _SID0(2), /* S-1-2 */
745 .num_names = ARRAY_SIZE(predefined_names_S_1_2),
746 .names = predefined_names_S_1_2,
749 .domain = "",
750 .sid = _SID0(3), /* S-1-3 */
751 .num_names = ARRAY_SIZE(predefined_names_S_1_3),
752 .names = predefined_names_S_1_3,
755 .domain = "",
756 .sid = _SID0(3), /* S-1-3 */
757 .num_names = ARRAY_SIZE(predefined_names_S_1_3),
758 .names = predefined_names_S_1_3,
761 * S-1-5 is split here
763 * 'NT Pseudo Domain' has precedence before 'NT AUTHORITY'.
765 * In a LookupSids with multiple sids e.g. S-1-5 and S-1-5-7
766 * the domain section (struct lsa_DomainInfo) gets
767 * 'NT Pseudo Domain' with S-1-5. If asked in reversed order
768 * S-1-5-7 and then S-1-5, you get struct lsa_DomainInfo
769 * with 'NT AUTHORITY' and S-1-5.
772 .domain = "NT Pseudo Domain",
773 .sid = _SID0(5), /* S-1-5 */
774 .num_names = ARRAY_SIZE(predefined_names_S_1_5p),
775 .names = predefined_names_S_1_5p,
778 .domain = "NT AUTHORITY",
779 .sid = _SID0(5), /* S-1-5 */
780 .num_names = ARRAY_SIZE(predefined_names_S_1_5a),
781 .names = predefined_names_S_1_5a,
784 .domain = "BUILTIN",
785 .sid = _SID1(5, 32), /* S-1-5-32 */
786 .num_names = ARRAY_SIZE(predefined_names_S_1_5_32),
787 .names = predefined_names_S_1_5_32,
790 * 'NT AUTHORITY' again with S-1-5-64 this time
793 .domain = "NT AUTHORITY",
794 .sid = _SID1(5, 64), /* S-1-5-64 */
795 .num_names = ARRAY_SIZE(predefined_names_S_1_5_64),
796 .names = predefined_names_S_1_5_64,
799 .domain = "Internet$",
800 .sid = _SID0(7), /* S-1-7 */
801 .num_names = ARRAY_SIZE(predefined_names_S_1_7),
802 .names = predefined_names_S_1_7,
805 .domain = "Mandatory Label",
806 .sid = _SID0(16), /* S-1-16 */
807 .num_names = ARRAY_SIZE(predefined_names_S_1_16),
808 .names = predefined_names_S_1_16,
812 NTSTATUS dom_sid_lookup_predefined_name(const char *name,
813 const struct dom_sid **sid,
814 enum lsa_SidType *type,
815 const struct dom_sid **authority_sid,
816 const char **authority_name)
818 size_t di;
819 const char *domain = "";
820 size_t domain_len = 0;
821 const char *p;
822 bool match;
824 *sid = NULL;
825 *type = SID_NAME_UNKNOWN;
826 *authority_sid = NULL;
827 *authority_name = NULL;
829 if (name == NULL) {
830 name = "";
833 p = strchr(name, '\\');
834 if (p != NULL) {
835 domain = name;
836 domain_len = PTR_DIFF(p, domain);
837 name = p + 1;
840 match = strequal(name, "");
841 if (match) {
843 * Strange, but that's what W2012R2 does.
845 name = "BUILTIN";
848 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
849 const struct predefined_domain_mapping *d =
850 &predefined_domains[di];
851 size_t ni;
853 if (domain_len != 0) {
854 int cmp;
856 cmp = strncasecmp(d->domain, domain, domain_len);
857 if (cmp != 0) {
858 continue;
862 for (ni = 0; ni < d->num_names; ni++) {
863 const struct predefined_name_mapping *n =
864 &d->names[ni];
866 match = strequal(n->name, name);
867 if (!match) {
868 continue;
871 *sid = &n->sid;
872 *type = n->type;
873 *authority_sid = &d->sid;
874 *authority_name = d->domain;
875 return NT_STATUS_OK;
879 return NT_STATUS_NONE_MAPPED;
882 bool dom_sid_lookup_is_predefined_domain(const char *domain)
884 size_t di;
885 bool match;
887 if (domain == NULL) {
888 domain = "";
891 match = strequal(domain, "");
892 if (match) {
894 * Strange, but that's what W2012R2 does.
896 domain = "BUILTIN";
899 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
900 const struct predefined_domain_mapping *d =
901 &predefined_domains[di];
902 int cmp;
904 cmp = strcasecmp(d->domain, domain);
905 if (cmp != 0) {
906 continue;
909 return true;
912 return false;
915 NTSTATUS dom_sid_lookup_predefined_sid(const struct dom_sid *sid,
916 const char **name,
917 enum lsa_SidType *type,
918 const struct dom_sid **authority_sid,
919 const char **authority_name)
921 size_t di;
922 bool match_domain = false;
924 *name = NULL;
925 *type = SID_NAME_UNKNOWN;
926 *authority_sid = NULL;
927 *authority_name = NULL;
929 if (sid == NULL) {
930 return NT_STATUS_INVALID_SID;
933 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
934 const struct predefined_domain_mapping *d =
935 &predefined_domains[di];
936 size_t ni;
937 int cmp;
939 cmp = dom_sid_compare_auth(&d->sid, sid);
940 if (cmp != 0) {
941 continue;
944 match_domain = true;
946 for (ni = 0; ni < d->num_names; ni++) {
947 const struct predefined_name_mapping *n =
948 &d->names[ni];
950 cmp = dom_sid_compare(&n->sid, sid);
951 if (cmp != 0) {
952 continue;
955 *name = n->name;
956 *type = n->type;
957 *authority_sid = &d->sid;
958 *authority_name = d->domain;
959 return NT_STATUS_OK;
963 if (!match_domain) {
964 return NT_STATUS_INVALID_SID;
967 return NT_STATUS_NONE_MAPPED;