r23432: Remove superfluous comment.
[Samba/bb.git] / source3 / lib / util_sid.c
blob032be9aa93b9d01791c9aa1612520e50dcbca0a9
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 memcpy(sid->id_auth, inbuf+2, 6);
407 if (len < 8 + sid->num_auths*4)
408 return False;
409 for (i=0;i<sid->num_auths;i++)
410 sid->sub_auths[i] = IVAL(inbuf, 8+i*4);
411 return True;
414 /*****************************************************************
415 Compare the auth portion of two sids.
416 *****************************************************************/
418 static int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2)
420 int i;
422 if (sid1 == sid2)
423 return 0;
424 if (!sid1)
425 return -1;
426 if (!sid2)
427 return 1;
429 if (sid1->sid_rev_num != sid2->sid_rev_num)
430 return sid1->sid_rev_num - sid2->sid_rev_num;
432 for (i = 0; i < 6; i++)
433 if (sid1->id_auth[i] != sid2->id_auth[i])
434 return sid1->id_auth[i] - sid2->id_auth[i];
436 return 0;
439 /*****************************************************************
440 Compare two sids.
441 *****************************************************************/
443 int sid_compare(const DOM_SID *sid1, const DOM_SID *sid2)
445 int i;
447 if (sid1 == sid2)
448 return 0;
449 if (!sid1)
450 return -1;
451 if (!sid2)
452 return 1;
454 /* Compare most likely different rids, first: i.e start at end */
455 if (sid1->num_auths != sid2->num_auths)
456 return sid1->num_auths - sid2->num_auths;
458 for (i = sid1->num_auths-1; i >= 0; --i)
459 if (sid1->sub_auths[i] != sid2->sub_auths[i])
460 return sid1->sub_auths[i] - sid2->sub_auths[i];
462 return sid_compare_auth(sid1, sid2);
465 /*****************************************************************
466 See if 2 SIDs are in the same domain
467 this just compares the leading sub-auths
468 *****************************************************************/
470 int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
472 int n, i;
474 n = MIN(sid1->num_auths, sid2->num_auths);
476 for (i = n-1; i >= 0; --i)
477 if (sid1->sub_auths[i] != sid2->sub_auths[i])
478 return sid1->sub_auths[i] - sid2->sub_auths[i];
480 return sid_compare_auth(sid1, sid2);
483 /*****************************************************************
484 Compare two sids.
485 *****************************************************************/
487 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
489 return sid_compare(sid1, sid2) == 0;
492 /*****************************************************************
493 Calculates size of a sid.
494 *****************************************************************/
496 size_t sid_size(const DOM_SID *sid)
498 if (sid == NULL)
499 return 0;
501 return sid->num_auths * sizeof(uint32) + 8;
504 /*****************************************************************
505 Returns true if SID is internal (and non-mappable).
506 *****************************************************************/
508 BOOL non_mappable_sid(DOM_SID *sid)
510 DOM_SID dom;
511 uint32 rid;
513 sid_copy(&dom, sid);
514 sid_split_rid(&dom, &rid);
516 if (sid_equal(&dom, &global_sid_Builtin))
517 return True;
519 if (sid_equal(&dom, &global_sid_NT_Authority))
520 return True;
522 return False;
525 /*****************************************************************
526 Return the binary string representation of a DOM_SID.
527 Caller must free.
528 *****************************************************************/
530 char *sid_binstring(const DOM_SID *sid)
532 char *buf, *s;
533 int len = sid_size(sid);
534 buf = (char *)SMB_MALLOC(len);
535 if (!buf)
536 return NULL;
537 sid_linearize(buf, len, sid);
538 s = binary_string_rfc2254(buf, len);
539 free(buf);
540 return s;
543 /*****************************************************************
544 Return the binary string representation of a DOM_SID.
545 Caller must free.
546 *****************************************************************/
548 char *sid_binstring_hex(const DOM_SID *sid)
550 char *buf, *s;
551 int len = sid_size(sid);
552 buf = (char *)SMB_MALLOC(len);
553 if (!buf)
554 return NULL;
555 sid_linearize(buf, len, sid);
556 s = binary_string(buf, len);
557 free(buf);
558 return s;
561 /*******************************************************************
562 Tallocs a duplicate SID.
563 ********************************************************************/
565 DOM_SID *sid_dup_talloc(TALLOC_CTX *ctx, const DOM_SID *src)
567 DOM_SID *dst;
569 if(!src)
570 return NULL;
572 if((dst = TALLOC_ZERO_P(ctx, DOM_SID)) != NULL) {
573 sid_copy( dst, src);
576 return dst;
579 /********************************************************************
580 Add SID to an array SIDs
581 ********************************************************************/
583 BOOL add_sid_to_array(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
584 DOM_SID **sids, size_t *num)
586 *sids = TALLOC_REALLOC_ARRAY(mem_ctx, *sids, DOM_SID,
587 (*num)+1);
588 if (*sids == NULL) {
589 *num = 0;
590 return False;
593 sid_copy(&((*sids)[*num]), sid);
594 *num += 1;
596 return True;
600 /********************************************************************
601 Add SID to an array SIDs ensuring that it is not already there
602 ********************************************************************/
604 BOOL add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
605 DOM_SID **sids, size_t *num_sids)
607 size_t i;
609 for (i=0; i<(*num_sids); i++) {
610 if (sid_compare(sid, &(*sids)[i]) == 0)
611 return True;
614 return add_sid_to_array(mem_ctx, sid, sids, num_sids);
617 /********************************************************************
618 Remove SID from an array
619 ********************************************************************/
621 void del_sid_from_array(const DOM_SID *sid, DOM_SID **sids, size_t *num)
623 DOM_SID *sid_list = *sids;
624 size_t i;
626 for ( i=0; i<*num; i++ ) {
628 /* if we find the SID, then decrement the count
629 and break out of the loop */
631 if ( sid_equal(sid, &sid_list[i]) ) {
632 *num -= 1;
633 break;
637 /* This loop will copy the remainder of the array
638 if i < num of sids ni the array */
640 for ( ; i<*num; i++ )
641 sid_copy( &sid_list[i], &sid_list[i+1] );
643 return;
646 BOOL add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
647 uint32 rid, uint32 **pp_rids, size_t *p_num)
649 size_t i;
651 for (i=0; i<*p_num; i++) {
652 if ((*pp_rids)[i] == rid)
653 return True;
656 *pp_rids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_rids, uint32, *p_num+1);
658 if (*pp_rids == NULL) {
659 *p_num = 0;
660 return False;
663 (*pp_rids)[*p_num] = rid;
664 *p_num += 1;
665 return True;
668 BOOL is_null_sid(const DOM_SID *sid)
670 static const DOM_SID null_sid = {0};
671 return sid_equal(sid, &null_sid);