dnscrypto-proxy: Support files updated.
[tomato.git] / release / src / router / samba3 / source / lib / util_sid.c
blob603ed79ead25d08eccaa255dc59e89b1a44de808
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
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
29 * Some useful sids
33 const DOM_SID global_sid_World_Domain = /* Everyone domain */
34 { 1, 0, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
35 const DOM_SID global_sid_World = /* Everyone */
36 { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
37 const DOM_SID global_sid_Creator_Owner_Domain = /* Creator Owner domain */
38 { 1, 0, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
39 const DOM_SID global_sid_NT_Authority = /* NT Authority */
40 { 1, 0, {0,0,0,0,0,5}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
41 const DOM_SID global_sid_System = /* System */
42 { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
43 const DOM_SID global_sid_NULL = /* NULL sid */
44 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
45 const DOM_SID global_sid_Authenticated_Users = /* All authenticated rids */
46 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
47 const DOM_SID global_sid_Network = /* Network rids */
48 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
50 const DOM_SID global_sid_Creator_Owner = /* Creator Owner */
51 { 1, 1, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
52 const DOM_SID global_sid_Creator_Group = /* Creator Group */
53 { 1, 1, {0,0,0,0,0,3}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
54 const DOM_SID global_sid_Anonymous = /* Anonymous login */
55 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
57 const DOM_SID global_sid_Builtin = /* Local well-known domain */
58 { 1, 1, {0,0,0,0,0,5}, {32,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
59 const DOM_SID global_sid_Builtin_Administrators = /* Builtin administrators */
60 { 1, 2, {0,0,0,0,0,5}, {32,544,0,0,0,0,0,0,0,0,0,0,0,0,0}};
61 const DOM_SID global_sid_Builtin_Users = /* Builtin users */
62 { 1, 2, {0,0,0,0,0,5}, {32,545,0,0,0,0,0,0,0,0,0,0,0,0,0}};
63 const DOM_SID global_sid_Builtin_Guests = /* Builtin guest users */
64 { 1, 2, {0,0,0,0,0,5}, {32,546,0,0,0,0,0,0,0,0,0,0,0,0,0}};
65 const DOM_SID global_sid_Builtin_Power_Users = /* Builtin power users */
66 { 1, 2, {0,0,0,0,0,5}, {32,547,0,0,0,0,0,0,0,0,0,0,0,0,0}};
67 const DOM_SID global_sid_Builtin_Account_Operators = /* Builtin account operators */
68 { 1, 2, {0,0,0,0,0,5}, {32,548,0,0,0,0,0,0,0,0,0,0,0,0,0}};
69 const DOM_SID global_sid_Builtin_Server_Operators = /* Builtin server operators */
70 { 1, 2, {0,0,0,0,0,5}, {32,549,0,0,0,0,0,0,0,0,0,0,0,0,0}};
71 const DOM_SID global_sid_Builtin_Print_Operators = /* Builtin print operators */
72 { 1, 2, {0,0,0,0,0,5}, {32,550,0,0,0,0,0,0,0,0,0,0,0,0,0}};
73 const DOM_SID global_sid_Builtin_Backup_Operators = /* Builtin backup operators */
74 { 1, 2, {0,0,0,0,0,5}, {32,551,0,0,0,0,0,0,0,0,0,0,0,0,0}};
75 const DOM_SID global_sid_Builtin_Replicator = /* Builtin replicator */
76 { 1, 2, {0,0,0,0,0,5}, {32,552,0,0,0,0,0,0,0,0,0,0,0,0,0}};
77 const DOM_SID global_sid_Builtin_PreWin2kAccess = /* Builtin pre win2k access */
78 { 1, 2, {0,0,0,0,0,5}, {32,554,0,0,0,0,0,0,0,0,0,0,0,0,0}};
80 const DOM_SID global_sid_Unix_Users = /* Unmapped Unix users */
81 { 1, 1, {0,0,0,0,0,22}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
82 const DOM_SID global_sid_Unix_Groups = /* Unmapped Unix groups */
83 { 1, 1, {0,0,0,0,0,22}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
85 /* Unused, left here for documentary purposes */
86 #if 0
87 #define SECURITY_NULL_SID_AUTHORITY 0
88 #define SECURITY_WORLD_SID_AUTHORITY 1
89 #define SECURITY_LOCAL_SID_AUTHORITY 2
90 #define SECURITY_CREATOR_SID_AUTHORITY 3
91 #define SECURITY_NT_AUTHORITY 5
92 #endif
95 * An NT compatible anonymous token.
98 static DOM_SID anon_sid_array[3] =
99 { { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
100 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
101 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
102 NT_USER_TOKEN anonymous_token = { 3, anon_sid_array, SE_NONE };
104 static DOM_SID system_sid_array[1] =
105 { { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
106 NT_USER_TOKEN system_token = { 1, system_sid_array, SE_ALL_PRIVS };
108 /****************************************************************************
109 Lookup string names for SID types.
110 ****************************************************************************/
112 static const struct {
113 enum lsa_SidType sid_type;
114 const char *string;
115 } sid_name_type[] = {
116 {SID_NAME_USER, "User"},
117 {SID_NAME_DOM_GRP, "Domain Group"},
118 {SID_NAME_DOMAIN, "Domain"},
119 {SID_NAME_ALIAS, "Local Group"},
120 {SID_NAME_WKN_GRP, "Well-known Group"},
121 {SID_NAME_DELETED, "Deleted Account"},
122 {SID_NAME_INVALID, "Invalid Account"},
123 {SID_NAME_UNKNOWN, "UNKNOWN"},
124 {SID_NAME_COMPUTER, "Computer"},
126 {(enum lsa_SidType)0, NULL}
129 const char *sid_type_lookup(uint32 sid_type)
131 int i = 0;
133 /* Look through list */
134 while(sid_name_type[i].sid_type != 0) {
135 if (sid_name_type[i].sid_type == sid_type)
136 return sid_name_type[i].string;
137 i++;
140 /* Default return */
141 return "SID *TYPE* is INVALID";
144 /**************************************************************************
145 Create the SYSTEM token.
146 ***************************************************************************/
148 NT_USER_TOKEN *get_system_token(void)
150 return &system_token;
153 /******************************************************************
154 get the default domain/netbios name to be used when dealing
155 with our passdb list of accounts
156 ******************************************************************/
158 const char *get_global_sam_name(void)
160 if ((lp_server_role() == ROLE_DOMAIN_PDC) || (lp_server_role() == ROLE_DOMAIN_BDC)) {
161 return lp_workgroup();
163 return global_myname();
166 /*****************************************************************
167 Convert a SID to an ascii string.
168 *****************************************************************/
170 char *sid_to_string(fstring sidstr_out, const DOM_SID *sid)
172 char subauth[16];
173 int i;
174 uint32 ia;
176 if (!sid) {
177 fstrcpy(sidstr_out, "(NULL SID)");
178 return sidstr_out;
182 * BIG NOTE: this function only does SIDS where the identauth is not >= 2^32
183 * in a range of 2^48.
185 ia = (sid->id_auth[5]) +
186 (sid->id_auth[4] << 8 ) +
187 (sid->id_auth[3] << 16) +
188 (sid->id_auth[2] << 24);
190 slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
192 for (i = 0; i < sid->num_auths; i++) {
193 slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
194 fstrcat(sidstr_out, subauth);
197 return sidstr_out;
200 /*****************************************************************
201 Useful function for debug lines.
202 *****************************************************************/
204 const char *sid_string_static(const DOM_SID *sid)
206 static fstring sid_str;
207 sid_to_string(sid_str, sid);
208 return sid_str;
211 /*****************************************************************
212 Convert a string to a SID. Returns True on success, False on fail.
213 *****************************************************************/
215 BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
217 const char *p;
218 char *q;
219 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
220 uint32 conv;
222 if ((sidstr[0] != 'S' && sidstr[0] != 's') || sidstr[1] != '-') {
223 DEBUG(3,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
224 return False;
227 ZERO_STRUCTP(sidout);
229 /* Get the revision number. */
230 p = sidstr + 2;
231 conv = (uint32) strtoul(p, &q, 10);
232 if (!q || (*q != '-')) {
233 DEBUG(3,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
234 return False;
236 sidout->sid_rev_num = (uint8) conv;
237 q++;
239 /* get identauth */
240 conv = (uint32) strtoul(q, &q, 10);
241 if (!q || (*q != '-')) {
242 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
243 return False;
245 /* identauth in decimal should be < 2^32 */
246 /* NOTE - the conv value is in big-endian format. */
247 sidout->id_auth[0] = 0;
248 sidout->id_auth[1] = 0;
249 sidout->id_auth[2] = (conv & 0xff000000) >> 24;
250 sidout->id_auth[3] = (conv & 0x00ff0000) >> 16;
251 sidout->id_auth[4] = (conv & 0x0000ff00) >> 8;
252 sidout->id_auth[5] = (conv & 0x000000ff);
254 q++;
255 sidout->num_auths = 0;
257 for(conv = (uint32) strtoul(q, &q, 10);
258 q && (*q =='-' || *q =='\0') && (sidout->num_auths < MAXSUBAUTHS);
259 conv = (uint32) strtoul(q, &q, 10)) {
260 sid_append_rid(sidout, conv);
261 if (*q == '\0')
262 break;
263 q++;
266 return True;
269 DOM_SID *string_sid_talloc(TALLOC_CTX *mem_ctx, const char *sidstr)
271 DOM_SID *result = TALLOC_P(mem_ctx, DOM_SID);
273 if (result == NULL)
274 return NULL;
276 if (!string_to_sid(result, sidstr))
277 return NULL;
279 return result;
282 /*****************************************************************
283 Add a rid to the end of a sid
284 *****************************************************************/
286 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
288 if (sid->num_auths < MAXSUBAUTHS) {
289 sid->sub_auths[sid->num_auths++] = rid;
290 return True;
292 return False;
295 BOOL sid_compose(DOM_SID *dst, const DOM_SID *domain_sid, uint32 rid)
297 sid_copy(dst, domain_sid);
298 return sid_append_rid(dst, rid);
301 /*****************************************************************
302 Removes the last rid from the end of a sid
303 *****************************************************************/
305 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
307 if (sid->num_auths > 0) {
308 sid->num_auths--;
309 *rid = sid->sub_auths[sid->num_auths];
310 return True;
312 return False;
315 /*****************************************************************
316 Return the last rid from the end of a sid
317 *****************************************************************/
319 BOOL sid_peek_rid(const DOM_SID *sid, uint32 *rid)
321 if (!sid || !rid)
322 return False;
324 if (sid->num_auths > 0) {
325 *rid = sid->sub_auths[sid->num_auths - 1];
326 return True;
328 return False;
331 /*****************************************************************
332 Return the last rid from the end of a sid
333 and check the sid against the exp_dom_sid
334 *****************************************************************/
336 BOOL sid_peek_check_rid(const DOM_SID *exp_dom_sid, const DOM_SID *sid, uint32 *rid)
338 if (!exp_dom_sid || !sid || !rid)
339 return False;
341 if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
342 return False;
345 if (sid_compare_domain(exp_dom_sid, sid)!=0){
346 *rid=(-1);
347 return False;
350 return sid_peek_rid(sid, rid);
353 /*****************************************************************
354 Copies a sid
355 *****************************************************************/
357 void sid_copy(DOM_SID *dst, const DOM_SID *src)
359 int i;
361 ZERO_STRUCTP(dst);
363 dst->sid_rev_num = src->sid_rev_num;
364 dst->num_auths = src->num_auths;
366 memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
368 for (i = 0; i < src->num_auths; i++)
369 dst->sub_auths[i] = src->sub_auths[i];
372 /*****************************************************************
373 Write a sid out into on-the-wire format.
374 *****************************************************************/
376 BOOL sid_linearize(char *outbuf, size_t len, const DOM_SID *sid)
378 size_t i;
380 if (len < sid_size(sid))
381 return False;
383 SCVAL(outbuf,0,sid->sid_rev_num);
384 SCVAL(outbuf,1,sid->num_auths);
385 memcpy(&outbuf[2], sid->id_auth, 6);
386 for(i = 0; i < sid->num_auths; i++)
387 SIVAL(outbuf, 8 + (i*4), sid->sub_auths[i]);
389 return True;
392 /*****************************************************************
393 Parse a on-the-wire SID to a DOM_SID.
394 *****************************************************************/
396 BOOL sid_parse(const char *inbuf, size_t len, DOM_SID *sid)
398 int i;
399 if (len < 8)
400 return False;
402 ZERO_STRUCTP(sid);
404 sid->sid_rev_num = CVAL(inbuf, 0);
405 sid->num_auths = CVAL(inbuf, 1);
406 if (sid->num_auths > MAXSUBAUTHS) {
407 return False;
409 memcpy(sid->id_auth, inbuf+2, 6);
410 if (len < 8 + sid->num_auths*4)
411 return False;
412 for (i=0;i<sid->num_auths;i++)
413 sid->sub_auths[i] = IVAL(inbuf, 8+i*4);
414 return True;
417 /*****************************************************************
418 Compare the auth portion of two sids.
419 *****************************************************************/
421 static int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2)
423 int i;
425 if (sid1 == sid2)
426 return 0;
427 if (!sid1)
428 return -1;
429 if (!sid2)
430 return 1;
432 if (sid1->sid_rev_num != sid2->sid_rev_num)
433 return sid1->sid_rev_num - sid2->sid_rev_num;
435 for (i = 0; i < 6; i++)
436 if (sid1->id_auth[i] != sid2->id_auth[i])
437 return sid1->id_auth[i] - sid2->id_auth[i];
439 return 0;
442 /*****************************************************************
443 Compare two sids.
444 *****************************************************************/
446 int sid_compare(const DOM_SID *sid1, const DOM_SID *sid2)
448 int i;
450 if (sid1 == sid2)
451 return 0;
452 if (!sid1)
453 return -1;
454 if (!sid2)
455 return 1;
457 /* Compare most likely different rids, first: i.e start at end */
458 if (sid1->num_auths != sid2->num_auths)
459 return sid1->num_auths - sid2->num_auths;
461 for (i = sid1->num_auths-1; i >= 0; --i)
462 if (sid1->sub_auths[i] != sid2->sub_auths[i])
463 return sid1->sub_auths[i] - sid2->sub_auths[i];
465 return sid_compare_auth(sid1, sid2);
468 /*****************************************************************
469 See if 2 SIDs are in the same domain
470 this just compares the leading sub-auths
471 *****************************************************************/
473 int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
475 int n, i;
477 n = MIN(sid1->num_auths, sid2->num_auths);
479 for (i = n-1; i >= 0; --i)
480 if (sid1->sub_auths[i] != sid2->sub_auths[i])
481 return sid1->sub_auths[i] - sid2->sub_auths[i];
483 return sid_compare_auth(sid1, sid2);
486 /*****************************************************************
487 Compare two sids.
488 *****************************************************************/
490 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
492 return sid_compare(sid1, sid2) == 0;
495 /*****************************************************************
496 Calculates size of a sid.
497 *****************************************************************/
499 size_t sid_size(const DOM_SID *sid)
501 if (sid == NULL)
502 return 0;
504 return sid->num_auths * sizeof(uint32) + 8;
507 /*****************************************************************
508 Returns true if SID is internal (and non-mappable).
509 *****************************************************************/
511 BOOL non_mappable_sid(DOM_SID *sid)
513 DOM_SID dom;
514 uint32 rid;
516 sid_copy(&dom, sid);
517 sid_split_rid(&dom, &rid);
519 if (sid_equal(&dom, &global_sid_Builtin))
520 return True;
522 if (sid_equal(&dom, &global_sid_NT_Authority))
523 return True;
525 return False;
528 /*****************************************************************
529 Return the binary string representation of a DOM_SID.
530 Caller must free.
531 *****************************************************************/
533 char *sid_binstring(const DOM_SID *sid)
535 char *buf, *s;
536 int len = sid_size(sid);
537 buf = (char *)SMB_MALLOC(len);
538 if (!buf)
539 return NULL;
540 sid_linearize(buf, len, sid);
541 s = binary_string_rfc2254(buf, len);
542 free(buf);
543 return s;
546 /*****************************************************************
547 Return the binary string representation of a DOM_SID.
548 Caller must free.
549 *****************************************************************/
551 char *sid_binstring_hex(const DOM_SID *sid)
553 char *buf, *s;
554 int len = sid_size(sid);
555 buf = (char *)SMB_MALLOC(len);
556 if (!buf)
557 return NULL;
558 sid_linearize(buf, len, sid);
559 s = binary_string(buf, len);
560 free(buf);
561 return s;
564 /*******************************************************************
565 Tallocs a duplicate SID.
566 ********************************************************************/
568 DOM_SID *sid_dup_talloc(TALLOC_CTX *ctx, const DOM_SID *src)
570 DOM_SID *dst;
572 if(!src)
573 return NULL;
575 if((dst = TALLOC_ZERO_P(ctx, DOM_SID)) != NULL) {
576 sid_copy( dst, src);
579 return dst;
582 /********************************************************************
583 Add SID to an array SIDs
584 ********************************************************************/
586 BOOL add_sid_to_array(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
587 DOM_SID **sids, size_t *num)
589 *sids = TALLOC_REALLOC_ARRAY(mem_ctx, *sids, DOM_SID,
590 (*num)+1);
591 if (*sids == NULL) {
592 *num = 0;
593 return False;
596 sid_copy(&((*sids)[*num]), sid);
597 *num += 1;
599 return True;
603 /********************************************************************
604 Add SID to an array SIDs ensuring that it is not already there
605 ********************************************************************/
607 BOOL add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
608 DOM_SID **sids, size_t *num_sids)
610 size_t i;
612 for (i=0; i<(*num_sids); i++) {
613 if (sid_compare(sid, &(*sids)[i]) == 0)
614 return True;
617 return add_sid_to_array(mem_ctx, sid, sids, num_sids);
620 /********************************************************************
621 Remove SID from an array
622 ********************************************************************/
624 void del_sid_from_array(const DOM_SID *sid, DOM_SID **sids, size_t *num)
626 DOM_SID *sid_list = *sids;
627 size_t i;
629 for ( i=0; i<*num; i++ ) {
631 /* if we find the SID, then decrement the count
632 and break out of the loop */
634 if ( sid_equal(sid, &sid_list[i]) ) {
635 *num -= 1;
636 break;
640 /* This loop will copy the remainder of the array
641 if i < num of sids ni the array */
643 for ( ; i<*num; i++ )
644 sid_copy( &sid_list[i], &sid_list[i+1] );
646 return;
649 BOOL add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
650 uint32 rid, uint32 **pp_rids, size_t *p_num)
652 size_t i;
654 for (i=0; i<*p_num; i++) {
655 if ((*pp_rids)[i] == rid)
656 return True;
659 *pp_rids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_rids, uint32, *p_num+1);
661 if (*pp_rids == NULL) {
662 *p_num = 0;
663 return False;
666 (*pp_rids)[*p_num] = rid;
667 *p_num += 1;
668 return True;
671 BOOL is_null_sid(const DOM_SID *sid)
673 static const DOM_SID null_sid = {0};
674 return sid_equal(sid, &null_sid);