lib/util: remove extra safe_string.h file
[samba.git] / libcli / security / util_sid.c
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
11
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.
16
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.
21
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/>.
24 */
25
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"
30
31 #undef strcasecmp
32 #undef strncasecmp
33
34 /*
35  * Some useful sids, more well known sids can be found at
36  * http://support.microsoft.com/kb/243330/EN-US/
37  */
38
39
40 /* S-1-1 */
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}};
43 /* S-1-1-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}};
46 /* S-1-2 */
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}};
49 /* S-1-3 */
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}};
52 /* S-1-5 */
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}};
55 /* S-1-5-18 */
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}};
58 /* S-1-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}};
61 /* S-1-5-11 */
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}};
64 #if 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}};
68 #endif
69
70 /* S-1-18 */
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}};
73 /* S-1-18-1 */
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}};
76 /* S-1-18-2 */
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}};
79
80 /* S-1-5-2 */
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}};
83
84 /* S-1-3 */
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}};
87 /* S-1-3-1 */
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}};
90 /* S-1-3-4 */
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}};
93 /* S-1-5-7 */
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}};
96 /* S-1-5-9 */
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}};
99 /* S-1-5-32 */
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}};
102 /* S-1-5-32-544 */
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}};
105 /* S-1-5-32-545 */
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}};
108 /* S-1-5-32-546 */
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}};
111 /* S-1-5-32-547 */
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}};
114 /* S-1-5-32-548 */
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}};
117 /* S-1-5-32-549 */
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}};
120 /* S-1-5-32-550 */
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}};
123 /* S-1-5-32-551 */
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}};
126 /* S-1-5-32-552 */
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}};
129 /* S-1-5-32-554 */
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}};
132
133 /* S-1-22-1 */
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}};
136 /* S-1-22-2 */
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}};
139
140 /*
141  * http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx
142  */
143 /* S-1-5-88 */
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}};
146 /* S-1-5-88-1 */
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}};
149 /* S-1-5-88-2 */
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}};
152 /* S-1-5-88-3 */
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 */
156 #if 0
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}};
159 #endif
160
161 /* Unused, left here for documentary purposes */
162 #if 0
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
168 #endif
169
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
176 };
177
178 /****************************************************************************
179  Lookup string names for SID types.
180 ****************************************************************************/
181
182 static const struct {
183         enum lsa_SidType sid_type;
184         const char *string;
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"}
197 };
198
199 const char *sid_type_lookup(uint32_t sid_type)
200 {
201         size_t i;
202
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;
207                 }
208         }
209
210         /* Default return */
211         return "SID *TYPE* is INVALID";
212 }
213
214 /**************************************************************************
215  Create the SYSTEM token.
216 ***************************************************************************/
217
218 const struct security_token *get_system_token(void)
219 {
220         return &system_token;
221 }
222
223 bool sid_compose(struct dom_sid *dst, const struct dom_sid *domain_sid, uint32_t rid)
224 {
225         sid_copy(dst, domain_sid);
226         return sid_append_rid(dst, rid);
227 }
228
229 /*****************************************************************
230  Removes the last rid from the end of a sid
231 *****************************************************************/
232
233 bool sid_split_rid(struct dom_sid *sid, uint32_t *rid)
234 {
235         if (sid->num_auths > 0) {
236                 sid->num_auths--;
237                 if (rid != NULL) {
238                         *rid = sid->sub_auths[sid->num_auths];
239                 }
240                 return true;
241         }
242         return false;
243 }
244
245 /*****************************************************************
246  Return the last rid from the end of a sid
247 *****************************************************************/
248
249 bool sid_peek_rid(const struct dom_sid *sid, uint32_t *rid)
250 {
251         if (!sid || !rid)
252                 return false;
253
254         if (sid->num_auths > 0) {
255                 *rid = sid->sub_auths[sid->num_auths - 1];
256                 return true;
257         }
258         return false;
259 }
260
261 /*****************************************************************
262  Return the last rid from the end of a sid
263  and check the sid against the exp_dom_sid
264 *****************************************************************/
265
266 bool sid_peek_check_rid(const struct dom_sid *exp_dom_sid, const struct dom_sid *sid, uint32_t *rid)
267 {
268         if (!exp_dom_sid || !sid || !rid)
269                 return false;
270
271         if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
272                 return false;
273         }
274
275         if (sid_compare_domain(exp_dom_sid, sid)!=0){
276                 *rid=(-1);
277                 return false;
278         }
279
280         return sid_peek_rid(sid, rid);
281 }
282
283 /*****************************************************************
284  Copies a sid
285 *****************************************************************/
286
287 void sid_copy(struct dom_sid *dst, const struct dom_sid *src)
288 {
289         int i;
290
291         *dst = (struct dom_sid) {
292                 .sid_rev_num = src->sid_rev_num,
293                 .num_auths = src->num_auths,
294         };
295
296         memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
297
298         for (i = 0; i < src->num_auths; i++)
299                 dst->sub_auths[i] = src->sub_auths[i];
300 }
301
302 /*****************************************************************
303  Parse a on-the-wire SID to a struct dom_sid.
304 *****************************************************************/
305
306 ssize_t sid_parse(const uint8_t *inbuf, size_t len, struct dom_sid *sid)
307 {
308         DATA_BLOB in = data_blob_const(inbuf, len);
309         enum ndr_err_code ndr_err;
310
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)) {
314                 return -1;
315         }
316         return ndr_size_dom_sid(sid, 0);
317 }
318
319 /*****************************************************************
320  See if 2 SIDs are in the same domain
321  this just compares the leading sub-auths
322 *****************************************************************/
323
324 int sid_compare_domain(const struct dom_sid *sid1, const struct dom_sid *sid2)
325 {
326         int n, i;
327
328         n = MIN(sid1->num_auths, sid2->num_auths);
329
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];
333
334         return dom_sid_compare_auth(sid1, sid2);
335 }
336
337 /********************************************************************
338  Add SID to an array SIDs
339 ********************************************************************/
340
341 NTSTATUS add_sid_to_array(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
342                           struct dom_sid **sids, uint32_t *num)
343 {
344         struct dom_sid *tmp;
345
346         if ((*num) == UINT32_MAX) {
347                 return NT_STATUS_INTEGER_OVERFLOW;
348         }
349
350         tmp = talloc_realloc(mem_ctx, *sids, struct dom_sid, (*num)+1);
351         if (tmp == NULL) {
352                 *num = 0;
353                 return NT_STATUS_NO_MEMORY;
354         }
355         *sids = tmp;
356
357         sid_copy(&((*sids)[*num]), sid);
358         *num += 1;
359
360         return NT_STATUS_OK;
361 }
362
363
364 /********************************************************************
365  Add SID to an array SIDs ensuring that it is not already there
366 ********************************************************************/
367
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)
370 {
371         uint32_t i;
372
373         for (i=0; i<(*num_sids); i++) {
374                 if (dom_sid_equal(sid, &(*sids)[i])) {
375                         return NT_STATUS_OK;
376                 }
377         }
378
379         return add_sid_to_array(mem_ctx, sid, sids, num_sids);
380 }
381
382 /********************************************************************
383  Remove SID from an array
384 ********************************************************************/
385
386 void del_sid_from_array(const struct dom_sid *sid, struct dom_sid **sids,
387                         uint32_t *num)
388 {
389         struct dom_sid *sid_list = *sids;
390         uint32_t i;
391
392         for ( i=0; i<*num; i++ ) {
393
394                 /* if we find the SID, then decrement the count
395                    and break out of the loop */
396
397                 if (dom_sid_equal(sid, &sid_list[i])) {
398                         *num -= 1;
399                         break;
400                 }
401         }
402
403         /* This loop will copy the remainder of the array
404            if i < num of sids in the array */
405
406         for ( ; i<*num; i++ ) {
407                 sid_copy( &sid_list[i], &sid_list[i+1] );
408         }
409
410         return;
411 }
412
413 bool add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
414                              uint32_t rid, uint32_t **pp_rids, size_t *p_num)
415 {
416         size_t i;
417
418         for (i=0; i<*p_num; i++) {
419                 if ((*pp_rids)[i] == rid)
420                         return true;
421         }
422
423         *pp_rids = talloc_realloc(mem_ctx, *pp_rids, uint32_t, *p_num+1);
424
425         if (*pp_rids == NULL) {
426                 *p_num = 0;
427                 return false;
428         }
429
430         (*pp_rids)[*p_num] = rid;
431         *p_num += 1;
432         return true;
433 }
434
435 bool is_null_sid(const struct dom_sid *sid)
436 {
437         const struct dom_sid null_sid = {0};
438         return dom_sid_equal(sid, &null_sid);
439 }
440
441 /*
442  * See [MS-LSAT] 3.1.1.1.1 Predefined Translation Database and Corresponding View
443  */
444 struct predefined_name_mapping {
445         const char *name;
446         enum lsa_SidType type;
447         struct dom_sid sid;
448 };
449
450 struct predefined_domain_mapping {
451         const char *domain;
452         struct dom_sid sid;
453         size_t num_names;
454         const struct predefined_name_mapping *names;
455 };
456
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}}
466
467 /*
468  * S-1-0
469  */
470 static const struct predefined_name_mapping predefined_names_S_1_0[] = {
471         {
472                 .name = "NULL SID",
473                 .type = SID_NAME_WKN_GRP,
474                 .sid = _SID1(0, 0), /* S-1-0-0 */
475         },
476 };
477
478 /*
479  * S-1-1
480  */
481 static const struct predefined_name_mapping predefined_names_S_1_1[] = {
482         {
483                 .name = "Everyone",
484                 .type = SID_NAME_WKN_GRP,
485                 .sid = _SID1(1, 0), /* S-1-1-0 */
486         },
487 };
488
489 /*
490  * S-1-2
491  */
492 static const struct predefined_name_mapping predefined_names_S_1_2[] = {
493         {
494                 .name = "LOCAL",
495                 .type = SID_NAME_WKN_GRP,
496                 .sid = _SID1(2, 0), /* S-1-2-0 */
497         },
498 };
499
500 /*
501  * S-1-3
502  */
503 static const struct predefined_name_mapping predefined_names_S_1_3[] = {
504         {
505                 .name = "CREATOR OWNER",
506                 .type = SID_NAME_WKN_GRP,
507                 .sid = _SID1(3, 0), /* S-1-3-0 */
508         },
509         {
510                 .name = "CREATOR GROUP",
511                 .type = SID_NAME_WKN_GRP,
512                 .sid = _SID1(3, 1), /* S-1-3-1 */
513         },
514         {
515                 .name = "CREATOR OWNER SERVER",
516                 .type = SID_NAME_WKN_GRP,
517                 .sid = _SID1(3, 0), /* S-1-3-2 */
518         },
519         {
520                 .name = "CREATOR GROUP SERVER",
521                 .type = SID_NAME_WKN_GRP,
522                 .sid = _SID1(3, 1), /* S-1-3-3 */
523         },
524         {
525                 .name = "OWNER RIGHTS",
526                 .type = SID_NAME_WKN_GRP,
527                 .sid = _SID1(3, 4), /* S-1-3-4 */
528         },
529 };
530
531 /*
532  * S-1-5 only 'NT Pseudo Domain'
533  */
534 static const struct predefined_name_mapping predefined_names_S_1_5p[] = {
535         {
536                 .name = "NT Pseudo Domain",
537                 .type = SID_NAME_DOMAIN,
538                 .sid = _SID0(5), /* S-1-5 */
539         },
540 };
541
542 /*
543  * S-1-5 'NT AUTHORITY'
544  */
545 static const struct predefined_name_mapping predefined_names_S_1_5a[] = {
546         {
547                 .name = "DIALUP",
548                 .type = SID_NAME_WKN_GRP,
549                 .sid = _SID1(5, 1), /* S-1-5-1 */
550         },
551         {
552                 .name = "NETWORK",
553                 .type = SID_NAME_WKN_GRP,
554                 .sid = _SID1(5, 2), /* S-1-5-2 */
555         },
556         {
557                 .name = "BATCH",
558                 .type = SID_NAME_WKN_GRP,
559                 .sid = _SID1(5, 3), /* S-1-5-3 */
560         },
561         {
562                 .name = "INTERACTIVE",
563                 .type = SID_NAME_WKN_GRP,
564                 .sid = _SID1(5, 4), /* S-1-5-4 */
565         },
566         {
567                 .name = "SERVICE",
568                 .type = SID_NAME_WKN_GRP,
569                 .sid = _SID1(5, 6), /* S-1-5-6 */
570         },
571         {
572                 .name = "ANONYMOUS LOGON",
573                 .type = SID_NAME_WKN_GRP,
574                 .sid = _SID1(5, 7), /* S-1-5-7 */
575         },
576         {
577                 .name = "PROXY",
578                 .type = SID_NAME_WKN_GRP,
579                 .sid = _SID1(5, 8), /* S-1-5-8 */
580         },
581         {
582                 .name = "ENTERPRISE DOMAIN CONTROLLERS",
583                 .type = SID_NAME_WKN_GRP,
584                 .sid = _SID1(5, 9), /* S-1-5-9 */
585         },
586         {
587                 .name = "SELF",
588                 .type = SID_NAME_WKN_GRP,
589                 .sid = _SID1(5, 10), /* S-1-5-10 */
590         },
591         {
592                 .name = "Authenticated Users",
593                 .type = SID_NAME_WKN_GRP,
594                 .sid = _SID1(5, 11), /* S-1-5-11 */
595         },
596         {
597                 .name = "RESTRICTED",
598                 .type = SID_NAME_WKN_GRP,
599                 .sid = _SID1(5, 12), /* S-1-5-12 */
600         },
601         {
602                 .name = "TERMINAL SERVER USER",
603                 .type = SID_NAME_WKN_GRP,
604                 .sid = _SID1(5, 13), /* S-1-5-13 */
605         },
606         {
607                 .name = "REMOTE INTERACTIVE LOGON",
608                 .type = SID_NAME_WKN_GRP,
609                 .sid = _SID1(5, 14), /* S-1-5-14 */
610         },
611         {
612                 .name = "This Organization",
613                 .type = SID_NAME_WKN_GRP,
614                 .sid = _SID1(5, 15), /* S-1-5-15 */
615         },
616         {
617                 .name = "IUSR",
618                 .type = SID_NAME_WKN_GRP,
619                 .sid = _SID1(5, 17), /* S-1-5-17 */
620         },
621         {
622                 .name = "SYSTEM",
623                 .type = SID_NAME_WKN_GRP,
624                 .sid = _SID1(5, 18), /* S-1-5-18 */
625         },
626         {
627                 .name = "LOCAL SERVICE",
628                 .type = SID_NAME_WKN_GRP,
629                 .sid = _SID1(5, 19), /* S-1-5-19 */
630         },
631         {
632                 .name = "NETWORK SERVICE",
633                 .type = SID_NAME_WKN_GRP,
634                 .sid = _SID1(5, 20), /* S-1-5-20 */
635         },
636         {
637                 .name = "WRITE RESTRICTED",
638                 .type = SID_NAME_WKN_GRP,
639                 .sid = _SID1(5, 33), /* S-1-5-33 */
640         },
641         {
642                 .name = "Other Organization",
643                 .type = SID_NAME_WKN_GRP,
644                 .sid = _SID1(5, 1000), /* S-1-5-1000 */
645         },
646 };
647
648 /*
649  * S-1-5-32
650  */
651 static const struct predefined_name_mapping predefined_names_S_1_5_32[] = {
652         {
653                 .name = "BUILTIN",
654                 .type = SID_NAME_DOMAIN,
655                 .sid = _SID1(5, 32), /* S-1-5-32 */
656         },
657 };
658
659 /*
660  * S-1-5-64
661  */
662 static const struct predefined_name_mapping predefined_names_S_1_5_64[] = {
663         {
664                 .name = "NTLM Authentication",
665                 .type = SID_NAME_WKN_GRP,
666                 .sid = _SID2(5, 64, 10), /* S-1-5-64-10 */
667         },
668         {
669                 .name = "SChannel Authentication",
670                 .type = SID_NAME_WKN_GRP,
671                 .sid = _SID2(5, 64, 14), /* S-1-5-64-14 */
672         },
673         {
674                 .name = "Digest Authentication",
675                 .type = SID_NAME_WKN_GRP,
676                 .sid = _SID2(5, 64, 21), /* S-1-5-64-21 */
677         },
678 };
679
680 /*
681  * S-1-7
682  */
683 static const struct predefined_name_mapping predefined_names_S_1_7[] = {
684         {
685                 .name = "Internet$",
686                 .type = SID_NAME_DOMAIN,
687                 .sid = _SID0(7), /* S-1-7 */
688         },
689 };
690
691 /*
692  * S-1-16
693  */
694 static const struct predefined_name_mapping predefined_names_S_1_16[] = {
695         {
696                 .name = "Mandatory Label",
697                 .type = SID_NAME_DOMAIN,
698                 .sid = _SID0(16), /* S-1-16 */
699         },
700         {
701                 .name = "Untrusted Mandatory Level",
702                 .type = SID_NAME_LABEL,
703                 .sid = _SID1(16, 0), /* S-1-16-0 */
704         },
705         {
706                 .name = "Low Mandatory Level",
707                 .type = SID_NAME_LABEL,
708                 .sid = _SID1(16, 4096), /* S-1-16-4096 */
709         },
710         {
711                 .name = "Medium Mandatory Level",
712                 .type = SID_NAME_LABEL,
713                 .sid = _SID1(16, 8192), /* S-1-16-8192 */
714         },
715         {
716                 .name = "High Mandatory Level",
717                 .type = SID_NAME_LABEL,
718                 .sid = _SID1(16, 12288), /* S-1-16-12288 */
719         },
720         {
721                 .name = "System Mandatory Level",
722                 .type = SID_NAME_LABEL,
723                 .sid = _SID1(16, 16384), /* S-1-16-16384 */
724         },
725         {
726                 .name = "Protected Process Mandatory Level",
727                 .type = SID_NAME_LABEL,
728                 .sid = _SID1(16, 20480), /* S-1-16-20480 */
729         },
730 };
731
732 static const struct predefined_domain_mapping predefined_domains[] = {
733         {
734                 .domain = "",
735                 .sid = _SID0(0), /* S-1-0 */
736                 .num_names = ARRAY_SIZE(predefined_names_S_1_0),
737                 .names = predefined_names_S_1_0,
738         },
739         {
740                 .domain = "",
741                 .sid = _SID0(1), /* S-1-1 */
742                 .num_names = ARRAY_SIZE(predefined_names_S_1_1),
743                 .names = predefined_names_S_1_1,
744         },
745         {
746                 .domain = "",
747                 .sid = _SID0(2), /* S-1-2 */
748                 .num_names = ARRAY_SIZE(predefined_names_S_1_2),
749                 .names = predefined_names_S_1_2,
750         },
751         {
752                 .domain = "",
753                 .sid = _SID0(3), /* S-1-3 */
754                 .num_names = ARRAY_SIZE(predefined_names_S_1_3),
755                 .names = predefined_names_S_1_3,
756         },
757         {
758                 .domain = "",
759                 .sid = _SID0(3), /* S-1-3 */
760                 .num_names = ARRAY_SIZE(predefined_names_S_1_3),
761                 .names = predefined_names_S_1_3,
762         },
763         /*
764          * S-1-5 is split here
765          *
766          * 'NT Pseudo Domain' has precedence before 'NT AUTHORITY'.
767          *
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.
773          */
774         {
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,
779         },
780         {
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,
785         },
786         {
787                 .domain = "BUILTIN",
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,
791         },
792         /*
793          * 'NT AUTHORITY' again with S-1-5-64 this time
794          */
795         {
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,
800         },
801         {
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,
806         },
807         {
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,
812         },
813 };
814
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)
820 {
821         size_t di;
822         const char *domain = "";
823         size_t domain_len = 0;
824         const char *p;
825         bool match;
826
827         *sid = NULL;
828         *type = SID_NAME_UNKNOWN;
829         *authority_sid = NULL;
830         *authority_name = NULL;
831
832         if (name == NULL) {
833                 name = "";
834         }
835
836         p = strchr(name, '\\');
837         if (p != NULL) {
838                 domain = name;
839                 domain_len = PTR_DIFF(p, domain);
840                 name = p + 1;
841         }
842
843         match = strequal(name, "");
844         if (match) {
845                 /*
846                  * Strange, but that's what W2012R2 does.
847                  */
848                 name = "BUILTIN";
849         }
850
851         for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
852                 const struct predefined_domain_mapping *d =
853                         &predefined_domains[di];
854                 size_t ni;
855
856                 if (domain_len != 0) {
857                         int cmp;
858
859                         cmp = strncasecmp(d->domain, domain, domain_len);
860                         if (cmp != 0) {
861                                 continue;
862                         }
863                 }
864
865                 for (ni = 0; ni < d->num_names; ni++) {
866                         const struct predefined_name_mapping *n =
867                                 &d->names[ni];
868
869                         match = strequal(n->name, name);
870                         if (!match) {
871                                 continue;
872                         }
873
874                         *sid = &n->sid;
875                         *type = n->type;
876                         *authority_sid = &d->sid;
877                         *authority_name = d->domain;
878                         return NT_STATUS_OK;
879                 }
880         }
881
882         return NT_STATUS_NONE_MAPPED;
883 }
884
885 bool dom_sid_lookup_is_predefined_domain(const char *domain)
886 {
887         size_t di;
888         bool match;
889
890         if (domain == NULL) {
891                 domain = "";
892         }
893
894         match = strequal(domain, "");
895         if (match) {
896                 /*
897                  * Strange, but that's what W2012R2 does.
898                  */
899                 domain = "BUILTIN";
900         }
901
902         for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
903                 const struct predefined_domain_mapping *d =
904                         &predefined_domains[di];
905                 int cmp;
906
907                 cmp = strcasecmp(d->domain, domain);
908                 if (cmp != 0) {
909                         continue;
910                 }
911
912                 return true;
913         }
914
915         return false;
916 }
917
918 NTSTATUS dom_sid_lookup_predefined_sid(const struct dom_sid *sid,
919                                        const char **name,
920                                        enum lsa_SidType *type,
921                                        const struct dom_sid **authority_sid,
922                                        const char **authority_name)
923 {
924         size_t di;
925         bool match_domain = false;
926
927         *name = NULL;
928         *type = SID_NAME_UNKNOWN;
929         *authority_sid = NULL;
930         *authority_name = NULL;
931
932         if (sid == NULL) {
933                 return NT_STATUS_INVALID_SID;
934         }
935
936         for (di = 0; di < ARRAY_SIZE(predefined_domains); di++) {
937                 const struct predefined_domain_mapping *d =
938                         &predefined_domains[di];
939                 size_t ni;
940                 int cmp;
941
942                 cmp = dom_sid_compare_auth(&d->sid, sid);
943                 if (cmp != 0) {
944                         continue;
945                 }
946
947                 match_domain = true;
948
949                 for (ni = 0; ni < d->num_names; ni++) {
950                         const struct predefined_name_mapping *n =
951                                 &d->names[ni];
952
953                         cmp = dom_sid_compare(&n->sid, sid);
954                         if (cmp != 0) {
955                                 continue;
956                         }
957
958                         *name = n->name;
959                         *type = n->type;
960                         *authority_sid = &d->sid;
961                         *authority_name = d->domain;
962                         return NT_STATUS_OK;
963                 }
964         }
965
966         if (!match_domain) {
967                 return NT_STATUS_INVALID_SID;
968         }
969
970         return NT_STATUS_NONE_MAPPED;
971 }