smbd: Save 3 lines
[Samba.git] / libcli / security / util_sid.c
blob0942b2fe259d5ea6c4e6683649774e01f9023302
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 "replace.h"
27 #include "lib/util/samba_util.h"
28 #include "../librpc/gen_ndr/ndr_security.h"
29 #include "../librpc/gen_ndr/netlogon.h"
30 #include "../libcli/security/security.h"
31 #include "auth/auth.h"
34 #undef strcasecmp
35 #undef strncasecmp
38 * Some useful sids, more well known sids can be found at
39 * http://support.microsoft.com/kb/243330/EN-US/
43 /* S-1-1 */
44 const struct dom_sid global_sid_World_Domain = /* Everyone domain */
45 { 1, 0, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
46 /* S-1-1-0 */
47 const struct dom_sid global_sid_World = /* Everyone */
48 { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
49 /* S-1-2 */
50 const struct dom_sid global_sid_Local_Authority = /* Local Authority */
51 { 1, 0, {0,0,0,0,0,2}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
52 /* S-1-3 */
53 const struct dom_sid global_sid_Creator_Owner_Domain = /* Creator Owner domain */
54 { 1, 0, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
55 /* S-1-5 */
56 const struct dom_sid global_sid_NT_Authority = /* NT Authority */
57 { 1, 0, {0,0,0,0,0,5}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
58 /* S-1-5-18 */
59 const struct dom_sid global_sid_System = /* System */
60 { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
61 /* S-1-0-0 */
62 const struct dom_sid global_sid_NULL = /* NULL sid */
63 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
64 /* S-1-5-10 */
65 const struct dom_sid global_sid_Self = /* SELF */
66 { 1, 1, {0,0,0,0,0,5}, {10,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
67 /* S-1-5-11 */
68 const struct dom_sid global_sid_Authenticated_Users = /* All authenticated rids */
69 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
70 #if 0
71 /* for documentation S-1-5-12 */
72 const struct dom_sid global_sid_Restricted = /* Restricted Code */
73 { 1, 1, {0,0,0,0,0,5}, {12,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
74 #endif
76 /* S-1-18 */
77 const struct dom_sid global_sid_Asserted_Identity = /* Asserted Identity */
78 { 1, 0, {0,0,0,0,0,18}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
79 /* S-1-18-1 */
80 const struct dom_sid global_sid_Asserted_Identity_Authentication_Authority = /* Asserted Identity Authentication Authority */
81 { 1, 1, {0,0,0,0,0,18}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
82 /* S-1-18-2 */
83 const struct dom_sid global_sid_Asserted_Identity_Service = /* Asserted Identity Service */
84 { 1, 1, {0,0,0,0,0,18}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
86 /* S-1-18-3 */
87 const struct dom_sid global_sid_Fresh_Public_Key_Identity = /* Fresh Public Key Identity */
88 { 1, 1, {0,0,0,0,0,18}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
90 /* S-1-5-2 */
91 const struct dom_sid global_sid_Network = /* Network rids */
92 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
94 /* S-1-3 */
95 const struct dom_sid global_sid_Creator_Owner = /* Creator Owner */
96 { 1, 1, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
97 /* S-1-3-1 */
98 const struct dom_sid global_sid_Creator_Group = /* Creator Group */
99 { 1, 1, {0,0,0,0,0,3}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
100 /* S-1-3-4 */
101 const struct dom_sid global_sid_Owner_Rights = /* Owner Rights */
102 { 1, 1, {0,0,0,0,0,3}, {4,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
103 /* S-1-5-7 */
104 const struct dom_sid global_sid_Anonymous = /* Anonymous login */
105 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
106 /* S-1-5-9 */
107 const struct dom_sid global_sid_Enterprise_DCs = /* Enterprise DCs */
108 { 1, 1, {0,0,0,0,0,5}, {9,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
109 /* S-1-5-21-0-0-0-496 */
110 const struct dom_sid global_sid_Compounded_Authentication = /* Compounded Authentication */
111 {1, 5, {0,0,0,0,0,5}, {21,0,0,0,496,0,0,0,0,0,0,0,0,0,0}};
112 /* S-1-5-21-0-0-0-497 */
113 const struct dom_sid global_sid_Claims_Valid = /* Claims Valid */
114 {1, 5, {0,0,0,0,0,5}, {21,0,0,0,497,0,0,0,0,0,0,0,0,0,0}};
115 /* S-1-5-32 */
116 const struct dom_sid global_sid_Builtin = /* Local well-known domain */
117 { 1, 1, {0,0,0,0,0,5}, {32,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
118 /* S-1-5-32-544 */
119 const struct dom_sid global_sid_Builtin_Administrators = /* Builtin administrators */
120 { 1, 2, {0,0,0,0,0,5}, {32,544,0,0,0,0,0,0,0,0,0,0,0,0,0}};
121 /* S-1-5-32-545 */
122 const struct dom_sid global_sid_Builtin_Users = /* Builtin users */
123 { 1, 2, {0,0,0,0,0,5}, {32,545,0,0,0,0,0,0,0,0,0,0,0,0,0}};
124 /* S-1-5-32-546 */
125 const struct dom_sid global_sid_Builtin_Guests = /* Builtin guest users */
126 { 1, 2, {0,0,0,0,0,5}, {32,546,0,0,0,0,0,0,0,0,0,0,0,0,0}};
127 /* S-1-5-32-547 */
128 const struct dom_sid global_sid_Builtin_Power_Users = /* Builtin power users */
129 { 1, 2, {0,0,0,0,0,5}, {32,547,0,0,0,0,0,0,0,0,0,0,0,0,0}};
130 /* S-1-5-32-548 */
131 const struct dom_sid global_sid_Builtin_Account_Operators = /* Builtin account operators */
132 { 1, 2, {0,0,0,0,0,5}, {32,548,0,0,0,0,0,0,0,0,0,0,0,0,0}};
133 /* S-1-5-32-549 */
134 const struct dom_sid global_sid_Builtin_Server_Operators = /* Builtin server operators */
135 { 1, 2, {0,0,0,0,0,5}, {32,549,0,0,0,0,0,0,0,0,0,0,0,0,0}};
136 /* S-1-5-32-550 */
137 const struct dom_sid global_sid_Builtin_Print_Operators = /* Builtin print operators */
138 { 1, 2, {0,0,0,0,0,5}, {32,550,0,0,0,0,0,0,0,0,0,0,0,0,0}};
139 /* S-1-5-32-551 */
140 const struct dom_sid global_sid_Builtin_Backup_Operators = /* Builtin backup operators */
141 { 1, 2, {0,0,0,0,0,5}, {32,551,0,0,0,0,0,0,0,0,0,0,0,0,0}};
142 /* S-1-5-32-552 */
143 const struct dom_sid global_sid_Builtin_Replicator = /* Builtin replicator */
144 { 1, 2, {0,0,0,0,0,5}, {32,552,0,0,0,0,0,0,0,0,0,0,0,0,0}};
145 /* S-1-5-32-554 */
146 const struct dom_sid global_sid_Builtin_PreWin2kAccess = /* Builtin pre win2k access */
147 { 1, 2, {0,0,0,0,0,5}, {32,554,0,0,0,0,0,0,0,0,0,0,0,0,0}};
149 /* S-1-22-1 */
150 const struct dom_sid global_sid_Unix_Users = /* Unmapped Unix users */
151 { 1, 1, {0,0,0,0,0,22}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
152 /* S-1-22-2 */
153 const struct dom_sid global_sid_Unix_Groups = /* Unmapped Unix groups */
154 { 1, 1, {0,0,0,0,0,22}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
157 * http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
159 /* S-1-5-88 */
160 const struct dom_sid global_sid_Unix_NFS = /* MS NFS and Apple style */
161 { 1, 1, {0,0,0,0,0,5}, {88,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
162 /* S-1-5-88-1 */
163 const struct dom_sid global_sid_Unix_NFS_Users = /* Unix uid, MS NFS and Apple style */
164 { 1, 2, {0,0,0,0,0,5}, {88,1,0,0,0,0,0,0,0,0,0,0,0,0,0}};
165 /* S-1-5-88-2 */
166 const struct dom_sid global_sid_Unix_NFS_Groups = /* Unix gid, MS NFS and Apple style */
167 { 1, 2, {0,0,0,0,0,5}, {88,2,0,0,0,0,0,0,0,0,0,0,0,0,0}};
168 /* S-1-5-88-3 */
169 const struct dom_sid global_sid_Unix_NFS_Mode = /* Unix mode */
170 { 1, 2, {0,0,0,0,0,5}, {88,3,0,0,0,0,0,0,0,0,0,0,0,0,0}};
171 /* Unused, left here for documentary purposes */
172 #if 0
173 const struct dom_sid global_sid_Unix_NFS_Other = /* Unix other, MS NFS and Apple style */
174 { 1, 2, {0,0,0,0,0,5}, {88,4,0,0,0,0,0,0,0,0,0,0,0,0,0}};
175 #endif
177 /* Information passing via security token */
178 const struct dom_sid global_sid_Samba_SMB3 =
179 {1, 1, {0,0,0,0,0,22}, {1397571891, }};
181 const struct dom_sid global_sid_Samba_NPA_Flags = {1,
183 {0, 0, 0, 0, 0, 22},
185 2041152804,
188 /* Unused, left here for documentary purposes */
189 #if 0
190 #define SECURITY_NULL_SID_AUTHORITY 0
191 #define SECURITY_WORLD_SID_AUTHORITY 1
192 #define SECURITY_LOCAL_SID_AUTHORITY 2
193 #define SECURITY_CREATOR_SID_AUTHORITY 3
194 #define SECURITY_NT_AUTHORITY 5
195 #endif
197 static struct dom_sid system_sid_array[1] =
198 { { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
199 static const struct security_token system_token = {
200 .num_sids = ARRAY_SIZE(system_sid_array),
201 .sids = system_sid_array,
202 .privilege_mask = SE_ALL_PRIVS
205 /****************************************************************************
206 Lookup string names for SID types.
207 ****************************************************************************/
209 const char *sid_type_lookup(uint32_t sid_type)
211 switch (sid_type) {
212 case SID_NAME_USE_NONE:
213 return "None";
214 break;
215 case SID_NAME_USER:
216 return "User";
217 break;
218 case SID_NAME_DOM_GRP:
219 return "Domain Group";
220 break;
221 case SID_NAME_DOMAIN:
222 return "Domain";
223 break;
224 case SID_NAME_ALIAS:
225 return "Local Group";
226 break;
227 case SID_NAME_WKN_GRP:
228 return "Well-known Group";
229 break;
230 case SID_NAME_DELETED:
231 return "Deleted Account";
232 break;
233 case SID_NAME_INVALID:
234 return "Invalid Account";
235 break;
236 case SID_NAME_UNKNOWN:
237 return "UNKNOWN";
238 break;
239 case SID_NAME_COMPUTER:
240 return "Computer";
241 break;
242 case SID_NAME_LABEL:
243 return "Mandatory Label";
244 break;
247 /* Default return */
248 return "SID *TYPE* is INVALID";
251 /**************************************************************************
252 Create the SYSTEM token.
253 ***************************************************************************/
255 const struct security_token *get_system_token(void)
257 return &system_token;
260 bool sid_compose(struct dom_sid *dst, const struct dom_sid *domain_sid, uint32_t rid)
262 sid_copy(dst, domain_sid);
263 return sid_append_rid(dst, rid);
266 /*****************************************************************
267 Removes the last rid from the end of a sid
268 *****************************************************************/
270 bool sid_split_rid(struct dom_sid *sid, uint32_t *rid)
272 if (sid->num_auths > 0) {
273 sid->num_auths--;
274 if (rid != NULL) {
275 *rid = sid->sub_auths[sid->num_auths];
277 return true;
279 return false;
282 /*****************************************************************
283 Return the last rid from the end of a sid
284 *****************************************************************/
286 bool sid_peek_rid(const struct dom_sid *sid, uint32_t *rid)
288 if (!sid || !rid)
289 return false;
291 if (sid->num_auths > 0) {
292 *rid = sid->sub_auths[sid->num_auths - 1];
293 return true;
295 return false;
298 /*****************************************************************
299 Return the last rid from the end of a sid
300 and check the sid against the exp_dom_sid
301 *****************************************************************/
303 bool sid_peek_check_rid(const struct dom_sid *exp_dom_sid, const struct dom_sid *sid, uint32_t *rid)
305 if (!exp_dom_sid || !sid || !rid)
306 return false;
308 if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
309 return false;
312 if (dom_sid_compare_domain(exp_dom_sid, sid)!=0){
313 *rid=(-1);
314 return false;
317 return sid_peek_rid(sid, rid);
320 /*****************************************************************
321 Copies a sid
322 *****************************************************************/
324 void sid_copy(struct dom_sid *dst, const struct dom_sid *src)
326 int i;
328 *dst = (struct dom_sid) {
329 .sid_rev_num = src->sid_rev_num,
330 .num_auths = src->num_auths,
333 memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
335 for (i = 0; i < src->num_auths; i++)
336 dst->sub_auths[i] = src->sub_auths[i];
339 /*****************************************************************
340 Parse a on-the-wire SID to a struct dom_sid.
341 *****************************************************************/
343 ssize_t sid_parse(const uint8_t *inbuf, size_t len, struct dom_sid *sid)
345 DATA_BLOB in = data_blob_const(inbuf, len);
346 enum ndr_err_code ndr_err;
348 ndr_err = ndr_pull_struct_blob_all(
349 &in, NULL, sid, (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
350 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
351 return -1;
353 return ndr_size_dom_sid(sid, 0);
356 /********************************************************************
357 Add SID to an array of SIDs
358 ********************************************************************/
360 NTSTATUS add_sid_to_array(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
361 struct dom_sid **sids, uint32_t *num)
363 struct dom_sid *tmp;
365 if ((*num) == UINT32_MAX) {
366 return NT_STATUS_INTEGER_OVERFLOW;
369 tmp = talloc_realloc(mem_ctx, *sids, struct dom_sid, (*num)+1);
370 if (tmp == NULL) {
371 *num = 0;
372 return NT_STATUS_NO_MEMORY;
374 *sids = tmp;
376 sid_copy(&((*sids)[*num]), sid);
377 *num += 1;
379 return NT_STATUS_OK;
383 /********************************************************************
384 Add SID to an array of SIDs ensuring that it is not already there
385 ********************************************************************/
387 NTSTATUS add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
388 struct dom_sid **sids, uint32_t *num_sids)
390 bool contains;
392 contains = sids_contains_sid(*sids, *num_sids, sid);
393 if (contains) {
394 return NT_STATUS_OK;
397 return add_sid_to_array(mem_ctx, sid, sids, num_sids);
401 * Appends a SID and attribute to an array of auth_SidAttr.
403 * @param [in] mem_ctx Talloc memory context on which to allocate the array.
404 * @param [in] sid The SID to append.
405 * @param [in] attrs SE_GROUP_* flags to go with the SID.
406 * @param [inout] sids A pointer to the auth_SidAttr array.
407 * @param [inout] num A pointer to the size of the auth_SidArray array.
408 * @returns NT_STATUS_OK on success.
410 NTSTATUS add_sid_to_array_attrs(TALLOC_CTX *mem_ctx,
411 const struct dom_sid *sid, uint32_t attrs,
412 struct auth_SidAttr **sids, uint32_t *num)
414 struct auth_SidAttr *tmp = NULL;
416 if ((*num) == UINT32_MAX) {
417 return NT_STATUS_INTEGER_OVERFLOW;
420 tmp = talloc_realloc(mem_ctx, *sids, struct auth_SidAttr, (*num)+1);
421 if (tmp == NULL) {
422 *num = 0;
423 return NT_STATUS_NO_MEMORY;
425 *sids = tmp;
427 sid_copy(&((*sids)[*num].sid), sid);
428 (*sids)[*num].attrs = attrs;
429 *num += 1;
431 return NT_STATUS_OK;
436 * Appends a SID and attribute to an array of auth_SidAttr,
437 * ensuring that it is not already there.
439 * @param [in] mem_ctx Talloc memory context on which to allocate the array.
440 * @param [in] sid The SID to append.
441 * @param [in] attrs SE_GROUP_* flags to go with the SID.
442 * @param [inout] sids A pointer to the auth_SidAttr array.
443 * @param [inout] num_sids A pointer to the size of the auth_SidArray array.
444 * @returns NT_STATUS_OK on success.
446 NTSTATUS add_sid_to_array_attrs_unique(TALLOC_CTX *mem_ctx,
447 const struct dom_sid *sid, uint32_t attrs,
448 struct auth_SidAttr **sids, uint32_t *num_sids)
450 bool contains;
452 contains = sids_contains_sid_attrs(*sids, *num_sids, sid, attrs);
453 if (contains) {
454 return NT_STATUS_OK;
457 return add_sid_to_array_attrs(mem_ctx, sid, attrs, sids, num_sids);
460 /********************************************************************
461 Remove SID from an array
462 ********************************************************************/
464 void del_sid_from_array(const struct dom_sid *sid, struct dom_sid **sids,
465 uint32_t *num)
467 struct dom_sid *sid_list = *sids;
468 uint32_t i;
470 for ( i=0; i<*num; i++ ) {
472 /* if we find the SID, then decrement the count
473 and break out of the loop */
475 if (dom_sid_equal(sid, &sid_list[i])) {
476 *num -= 1;
477 break;
481 /* This loop will copy the remainder of the array
482 if i < num of sids in the array */
484 for ( ; i<*num; i++ ) {
485 sid_copy( &sid_list[i], &sid_list[i+1] );
489 bool add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
490 uint32_t rid, uint32_t **pp_rids, size_t *p_num)
492 size_t i;
494 for (i=0; i<*p_num; i++) {
495 if ((*pp_rids)[i] == rid)
496 return true;
499 *pp_rids = talloc_realloc(mem_ctx, *pp_rids, uint32_t, *p_num+1);
501 if (*pp_rids == NULL) {
502 *p_num = 0;
503 return false;
506 (*pp_rids)[*p_num] = rid;
507 *p_num += 1;
508 return true;
511 bool is_null_sid(const struct dom_sid *sid)
513 static const struct dom_sid null_sid = {0};
514 return dom_sid_equal(sid, &null_sid);
518 * Return true if an array of SIDs contains a certain SID.
520 * @param [in] sids The SID array.
521 * @param [in] num_sids The size of the SID array.
522 * @param [in] sid The SID in question.
523 * @returns true if the array contains the SID.
525 bool sids_contains_sid(const struct dom_sid *sids,
526 const uint32_t num_sids,
527 const struct dom_sid *sid)
529 uint32_t i;
531 for (i = 0; i < num_sids; i++) {
532 if (dom_sid_equal(&sids[i], sid)) {
533 return true;
536 return false;
540 * Return true if an array of auth_SidAttr contains a certain SID.
542 * @param [in] sids The auth_SidAttr array.
543 * @param [in] num_sids The size of the auth_SidArray array.
544 * @param [in] sid The SID in question.
545 * @returns true if the array contains the SID.
547 bool sid_attrs_contains_sid(const struct auth_SidAttr *sids,
548 const uint32_t num_sids,
549 const struct dom_sid *sid)
551 uint32_t i;
553 for (i = 0; i < num_sids; i++) {
554 if (dom_sid_equal(&sids[i].sid, sid)) {
555 return true;
558 return false;
562 * Return true if an array of auth_SidAttr contains a certain SID with certain
563 * attributes.
565 * @param [in] sids The auth_SidAttr array.
566 * @param [in] num_sids The size of the auth_SidArray array.
567 * @param [in] sid The SID in question.
568 * @param [in] attrs The attributes of the SID.
569 * @returns true if the array contains the SID.
571 bool sids_contains_sid_attrs(const struct auth_SidAttr *sids,
572 const uint32_t num_sids,
573 const struct dom_sid *sid,
574 uint32_t attrs)
576 uint32_t i;
578 for (i = 0; i < num_sids; i++) {
579 if (attrs != sids[i].attrs) {
580 continue;
582 if (!dom_sid_equal(&sids[i].sid, sid)) {
583 continue;
586 return true;
588 return false;
592 * See [MS-LSAT] 3.1.1.1.1 Predefined Translation Database and Corresponding View
594 struct predefined_name_mapping {
595 const char *name;
596 enum lsa_SidType type;
597 struct dom_sid sid;
600 struct predefined_domain_mapping {
601 const char *domain;
602 struct dom_sid sid;
603 size_t num_names;
604 const struct predefined_name_mapping *names;
607 /* S-1-${AUTHORITY} */
608 #define _SID0(authority) \
609 { 1, 0, {0,0,0,0,0,authority}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
610 /* S-1-${AUTHORITY}-${SUB1} */
611 #define _SID1(authority,sub1) \
612 { 1, 1, {0,0,0,0,0,authority}, {sub1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
613 /* S-1-${AUTHORITY}-${SUB1}-${SUB2} */
614 #define _SID2(authority,sub1,sub2) \
615 { 1, 2, {0,0,0,0,0,authority}, {sub1,sub2,0,0,0,0,0,0,0,0,0,0,0,0,0}}
618 * S-1-0
620 static const struct predefined_name_mapping predefined_names_S_1_0[] = {
622 .name = "NULL SID",
623 .type = SID_NAME_WKN_GRP,
624 .sid = _SID1(0, 0), /* S-1-0-0 */
629 * S-1-1
631 static const struct predefined_name_mapping predefined_names_S_1_1[] = {
633 .name = "Everyone",
634 .type = SID_NAME_WKN_GRP,
635 .sid = _SID1(1, 0), /* S-1-1-0 */
640 * S-1-2
642 static const struct predefined_name_mapping predefined_names_S_1_2[] = {
644 .name = "LOCAL",
645 .type = SID_NAME_WKN_GRP,
646 .sid = _SID1(2, 0), /* S-1-2-0 */
651 * S-1-3
653 static const struct predefined_name_mapping predefined_names_S_1_3[] = {
655 .name = "CREATOR OWNER",
656 .type = SID_NAME_WKN_GRP,
657 .sid = _SID1(3, 0), /* S-1-3-0 */
660 .name = "CREATOR GROUP",
661 .type = SID_NAME_WKN_GRP,
662 .sid = _SID1(3, 1), /* S-1-3-1 */
665 .name = "CREATOR OWNER SERVER",
666 .type = SID_NAME_WKN_GRP,
667 .sid = _SID1(3, 0), /* S-1-3-2 */
670 .name = "CREATOR GROUP SERVER",
671 .type = SID_NAME_WKN_GRP,
672 .sid = _SID1(3, 1), /* S-1-3-3 */
675 .name = "OWNER RIGHTS",
676 .type = SID_NAME_WKN_GRP,
677 .sid = _SID1(3, 4), /* S-1-3-4 */
682 * S-1-5 only 'NT Pseudo Domain'
684 static const struct predefined_name_mapping predefined_names_S_1_5p[] = {
686 .name = "NT Pseudo Domain",
687 .type = SID_NAME_DOMAIN,
688 .sid = _SID0(5), /* S-1-5 */
693 * S-1-5 'NT AUTHORITY'
695 static const struct predefined_name_mapping predefined_names_S_1_5a[] = {
697 .name = "DIALUP",
698 .type = SID_NAME_WKN_GRP,
699 .sid = _SID1(5, 1), /* S-1-5-1 */
702 .name = "NETWORK",
703 .type = SID_NAME_WKN_GRP,
704 .sid = _SID1(5, 2), /* S-1-5-2 */
707 .name = "BATCH",
708 .type = SID_NAME_WKN_GRP,
709 .sid = _SID1(5, 3), /* S-1-5-3 */
712 .name = "INTERACTIVE",
713 .type = SID_NAME_WKN_GRP,
714 .sid = _SID1(5, 4), /* S-1-5-4 */
717 .name = "SERVICE",
718 .type = SID_NAME_WKN_GRP,
719 .sid = _SID1(5, 6), /* S-1-5-6 */
722 .name = "ANONYMOUS LOGON",
723 .type = SID_NAME_WKN_GRP,
724 .sid = _SID1(5, 7), /* S-1-5-7 */
727 .name = "PROXY",
728 .type = SID_NAME_WKN_GRP,
729 .sid = _SID1(5, 8), /* S-1-5-8 */
732 .name = "ENTERPRISE DOMAIN CONTROLLERS",
733 .type = SID_NAME_WKN_GRP,
734 .sid = _SID1(5, 9), /* S-1-5-9 */
737 .name = "SELF",
738 .type = SID_NAME_WKN_GRP,
739 .sid = _SID1(5, 10), /* S-1-5-10 */
742 .name = "Authenticated Users",
743 .type = SID_NAME_WKN_GRP,
744 .sid = _SID1(5, 11), /* S-1-5-11 */
747 .name = "RESTRICTED",
748 .type = SID_NAME_WKN_GRP,
749 .sid = _SID1(5, 12), /* S-1-5-12 */
752 .name = "TERMINAL SERVER USER",
753 .type = SID_NAME_WKN_GRP,
754 .sid = _SID1(5, 13), /* S-1-5-13 */
757 .name = "REMOTE INTERACTIVE LOGON",
758 .type = SID_NAME_WKN_GRP,
759 .sid = _SID1(5, 14), /* S-1-5-14 */
762 .name = "This Organization",
763 .type = SID_NAME_WKN_GRP,
764 .sid = _SID1(5, 15), /* S-1-5-15 */
767 .name = "IUSR",
768 .type = SID_NAME_WKN_GRP,
769 .sid = _SID1(5, 17), /* S-1-5-17 */
772 .name = "SYSTEM",
773 .type = SID_NAME_WKN_GRP,
774 .sid = _SID1(5, 18), /* S-1-5-18 */
777 .name = "LOCAL SERVICE",
778 .type = SID_NAME_WKN_GRP,
779 .sid = _SID1(5, 19), /* S-1-5-19 */
782 .name = "NETWORK SERVICE",
783 .type = SID_NAME_WKN_GRP,
784 .sid = _SID1(5, 20), /* S-1-5-20 */
787 .name = "WRITE RESTRICTED",
788 .type = SID_NAME_WKN_GRP,
789 .sid = _SID1(5, 33), /* S-1-5-33 */
792 .name = "Other Organization",
793 .type = SID_NAME_WKN_GRP,
794 .sid = _SID1(5, 1000), /* S-1-5-1000 */
799 * S-1-5-32
801 static const struct predefined_name_mapping predefined_names_S_1_5_32[] = {
803 .name = "BUILTIN",
804 .type = SID_NAME_DOMAIN,
805 .sid = _SID1(5, 32), /* S-1-5-32 */
810 * S-1-5-64
812 static const struct predefined_name_mapping predefined_names_S_1_5_64[] = {
814 .name = "NTLM Authentication",
815 .type = SID_NAME_WKN_GRP,
816 .sid = _SID2(5, 64, 10), /* S-1-5-64-10 */
819 .name = "SChannel Authentication",
820 .type = SID_NAME_WKN_GRP,
821 .sid = _SID2(5, 64, 14), /* S-1-5-64-14 */
824 .name = "Digest Authentication",
825 .type = SID_NAME_WKN_GRP,
826 .sid = _SID2(5, 64, 21), /* S-1-5-64-21 */
831 * S-1-7
833 static const struct predefined_name_mapping predefined_names_S_1_7[] = {
835 .name = "Internet$",
836 .type = SID_NAME_DOMAIN,
837 .sid = _SID0(7), /* S-1-7 */
842 * S-1-16
844 static const struct predefined_name_mapping predefined_names_S_1_16[] = {
846 .name = "Mandatory Label",
847 .type = SID_NAME_DOMAIN,
848 .sid = _SID0(16), /* S-1-16 */
851 .name = "Untrusted Mandatory Level",
852 .type = SID_NAME_LABEL,
853 .sid = _SID1(16, 0), /* S-1-16-0 */
856 .name = "Low Mandatory Level",
857 .type = SID_NAME_LABEL,
858 .sid = _SID1(16, 4096), /* S-1-16-4096 */
861 .name = "Medium Mandatory Level",
862 .type = SID_NAME_LABEL,
863 .sid = _SID1(16, 8192), /* S-1-16-8192 */
866 .name = "High Mandatory Level",
867 .type = SID_NAME_LABEL,
868 .sid = _SID1(16, 12288), /* S-1-16-12288 */
871 .name = "System Mandatory Level",
872 .type = SID_NAME_LABEL,
873 .sid = _SID1(16, 16384), /* S-1-16-16384 */
876 .name = "Protected Process Mandatory Level",
877 .type = SID_NAME_LABEL,
878 .sid = _SID1(16, 20480), /* S-1-16-20480 */
882 static const struct predefined_domain_mapping predefined_domains[] = {
884 .domain = "",
885 .sid = _SID0(0), /* S-1-0 */
886 .num_names = ARRAY_SIZE(predefined_names_S_1_0),
887 .names = predefined_names_S_1_0,
890 .domain = "",
891 .sid = _SID0(1), /* S-1-1 */
892 .num_names = ARRAY_SIZE(predefined_names_S_1_1),
893 .names = predefined_names_S_1_1,
896 .domain = "",
897 .sid = _SID0(2), /* S-1-2 */
898 .num_names = ARRAY_SIZE(predefined_names_S_1_2),
899 .names = predefined_names_S_1_2,
902 .domain = "",
903 .sid = _SID0(3), /* S-1-3 */
904 .num_names = ARRAY_SIZE(predefined_names_S_1_3),
905 .names = predefined_names_S_1_3,
908 .domain = "",
909 .sid = _SID0(3), /* S-1-3 */
910 .num_names = ARRAY_SIZE(predefined_names_S_1_3),
911 .names = predefined_names_S_1_3,
914 * S-1-5 is split here
916 * 'NT Pseudo Domain' has precedence before 'NT AUTHORITY'.
918 * In a LookupSids with multiple sids e.g. S-1-5 and S-1-5-7
919 * the domain section (struct lsa_DomainInfo) gets
920 * 'NT Pseudo Domain' with S-1-5. If asked in reversed order
921 * S-1-5-7 and then S-1-5, you get struct lsa_DomainInfo
922 * with 'NT AUTHORITY' and S-1-5.
925 .domain = "NT Pseudo Domain",
926 .sid = _SID0(5), /* S-1-5 */
927 .num_names = ARRAY_SIZE(predefined_names_S_1_5p),
928 .names = predefined_names_S_1_5p,
931 .domain = "NT AUTHORITY",
932 .sid = _SID0(5), /* S-1-5 */
933 .num_names = ARRAY_SIZE(predefined_names_S_1_5a),
934 .names = predefined_names_S_1_5a,
937 .domain = "BUILTIN",
938 .sid = _SID1(5, 32), /* S-1-5-32 */
939 .num_names = ARRAY_SIZE(predefined_names_S_1_5_32),
940 .names = predefined_names_S_1_5_32,
943 * 'NT AUTHORITY' again with S-1-5-64 this time
946 .domain = "NT AUTHORITY",
947 .sid = _SID1(5, 64), /* S-1-5-64 */
948 .num_names = ARRAY_SIZE(predefined_names_S_1_5_64),
949 .names = predefined_names_S_1_5_64,
952 .domain = "Internet$",
953 .sid = _SID0(7), /* S-1-7 */
954 .num_names = ARRAY_SIZE(predefined_names_S_1_7),
955 .names = predefined_names_S_1_7,
958 .domain = "Mandatory Label",
959 .sid = _SID0(16), /* S-1-16 */
960 .num_names = ARRAY_SIZE(predefined_names_S_1_16),
961 .names = predefined_names_S_1_16,
965 NTSTATUS dom_sid_lookup_predefined_name(const char *name,
966 const struct dom_sid **sid,
967 enum lsa_SidType *type,
968 const struct dom_sid **authority_sid,
969 const char **authority_name)
971 size_t di;
972 const char *domain = "";
973 size_t domain_len = 0;
974 const char *p;
975 bool match;
977 *sid = NULL;
978 *type = SID_NAME_UNKNOWN;
979 *authority_sid = NULL;
980 *authority_name = NULL;
982 if (name == NULL) {
983 name = "";
986 p = strchr(name, '\\');
987 if (p != NULL) {
988 domain = name;
989 domain_len = PTR_DIFF(p, domain);
990 name = p + 1;
993 match = strequal(name, "");
994 if (match) {
996 * Strange, but that's what W2012R2 does.
998 name = "BUILTIN";
1001 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
1002 const struct predefined_domain_mapping *d =
1003 &predefined_domains[di];
1004 size_t ni;
1006 if (domain_len != 0) {
1007 int cmp;
1009 cmp = strncasecmp(d->domain, domain, domain_len);
1010 if (cmp != 0) {
1011 continue;
1015 for (ni = 0; ni < d->num_names; ni++) {
1016 const struct predefined_name_mapping *n =
1017 &d->names[ni];
1019 match = strequal(n->name, name);
1020 if (!match) {
1021 continue;
1024 *sid = &n->sid;
1025 *type = n->type;
1026 *authority_sid = &d->sid;
1027 *authority_name = d->domain;
1028 return NT_STATUS_OK;
1032 return NT_STATUS_NONE_MAPPED;
1035 bool dom_sid_lookup_is_predefined_domain(const char *domain)
1037 size_t di;
1038 bool match;
1040 if (domain == NULL) {
1041 domain = "";
1044 match = strequal(domain, "");
1045 if (match) {
1047 * Strange, but that's what W2012R2 does.
1049 domain = "BUILTIN";
1052 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
1053 const struct predefined_domain_mapping *d =
1054 &predefined_domains[di];
1055 int cmp;
1057 cmp = strcasecmp(d->domain, domain);
1058 if (cmp != 0) {
1059 continue;
1062 return true;
1065 return false;
1068 NTSTATUS dom_sid_lookup_predefined_sid(const struct dom_sid *sid,
1069 const char **name,
1070 enum lsa_SidType *type,
1071 const struct dom_sid **authority_sid,
1072 const char **authority_name)
1074 size_t di;
1075 bool match_domain = false;
1077 *name = NULL;
1078 *type = SID_NAME_UNKNOWN;
1079 *authority_sid = NULL;
1080 *authority_name = NULL;
1082 if (sid == NULL) {
1083 return NT_STATUS_INVALID_SID;
1086 for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
1087 const struct predefined_domain_mapping *d =
1088 &predefined_domains[di];
1089 size_t ni;
1090 int cmp;
1092 cmp = dom_sid_compare_auth(&d->sid, sid);
1093 if (cmp != 0) {
1094 continue;
1097 match_domain = true;
1099 for (ni = 0; ni < d->num_names; ni++) {
1100 const struct predefined_name_mapping *n =
1101 &d->names[ni];
1103 cmp = dom_sid_compare(&n->sid, sid);
1104 if (cmp != 0) {
1105 continue;
1108 *name = n->name;
1109 *type = n->type;
1110 *authority_sid = &d->sid;
1111 *authority_name = d->domain;
1112 return NT_STATUS_OK;
1116 if (!match_domain) {
1117 return NT_STATUS_INVALID_SID;
1120 return NT_STATUS_NONE_MAPPED;