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 3 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, see <http://www.gnu.org/licenses/>.
26 #include "libcli/security/dom_sid.h"
29 * Some useful sids, more well known sids can be found at
30 * http://support.microsoft.com/kb/243330/EN-US/
34 const DOM_SID global_sid_World_Domain = /* Everyone domain */
35 { 1, 0, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
36 const DOM_SID global_sid_World = /* Everyone */
37 { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
38 const DOM_SID global_sid_Creator_Owner_Domain = /* Creator Owner domain */
39 { 1, 0, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
40 const DOM_SID global_sid_NT_Authority = /* NT Authority */
41 { 1, 0, {0,0,0,0,0,5}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
42 const DOM_SID global_sid_System = /* System */
43 { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
44 const DOM_SID global_sid_NULL = /* NULL sid */
45 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
46 const DOM_SID global_sid_Authenticated_Users = /* All authenticated rids */
47 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
49 /* for documentation */
50 const DOM_SID global_sid_Restriced = /* Restriced Code */
51 { 1, 1, {0,0,0,0,0,5}, {12,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
53 const DOM_SID global_sid_Network = /* Network rids */
54 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
56 const DOM_SID global_sid_Creator_Owner = /* Creator Owner */
57 { 1, 1, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
58 const DOM_SID global_sid_Creator_Group = /* Creator Group */
59 { 1, 1, {0,0,0,0,0,3}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
60 const DOM_SID global_sid_Anonymous = /* Anonymous login */
61 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
63 const DOM_SID global_sid_Builtin = /* Local well-known domain */
64 { 1, 1, {0,0,0,0,0,5}, {32,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
65 const DOM_SID global_sid_Builtin_Administrators = /* Builtin administrators */
66 { 1, 2, {0,0,0,0,0,5}, {32,544,0,0,0,0,0,0,0,0,0,0,0,0,0}};
67 const DOM_SID global_sid_Builtin_Users = /* Builtin users */
68 { 1, 2, {0,0,0,0,0,5}, {32,545,0,0,0,0,0,0,0,0,0,0,0,0,0}};
69 const DOM_SID global_sid_Builtin_Guests = /* Builtin guest users */
70 { 1, 2, {0,0,0,0,0,5}, {32,546,0,0,0,0,0,0,0,0,0,0,0,0,0}};
71 const DOM_SID global_sid_Builtin_Power_Users = /* Builtin power users */
72 { 1, 2, {0,0,0,0,0,5}, {32,547,0,0,0,0,0,0,0,0,0,0,0,0,0}};
73 const DOM_SID global_sid_Builtin_Account_Operators = /* Builtin account operators */
74 { 1, 2, {0,0,0,0,0,5}, {32,548,0,0,0,0,0,0,0,0,0,0,0,0,0}};
75 const DOM_SID global_sid_Builtin_Server_Operators = /* Builtin server operators */
76 { 1, 2, {0,0,0,0,0,5}, {32,549,0,0,0,0,0,0,0,0,0,0,0,0,0}};
77 const DOM_SID global_sid_Builtin_Print_Operators = /* Builtin print operators */
78 { 1, 2, {0,0,0,0,0,5}, {32,550,0,0,0,0,0,0,0,0,0,0,0,0,0}};
79 const DOM_SID global_sid_Builtin_Backup_Operators = /* Builtin backup operators */
80 { 1, 2, {0,0,0,0,0,5}, {32,551,0,0,0,0,0,0,0,0,0,0,0,0,0}};
81 const DOM_SID global_sid_Builtin_Replicator = /* Builtin replicator */
82 { 1, 2, {0,0,0,0,0,5}, {32,552,0,0,0,0,0,0,0,0,0,0,0,0,0}};
83 const DOM_SID global_sid_Builtin_PreWin2kAccess = /* Builtin pre win2k access */
84 { 1, 2, {0,0,0,0,0,5}, {32,554,0,0,0,0,0,0,0,0,0,0,0,0,0}};
86 const DOM_SID global_sid_Unix_Users = /* Unmapped Unix users */
87 { 1, 1, {0,0,0,0,0,22}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
88 const DOM_SID global_sid_Unix_Groups = /* Unmapped Unix groups */
89 { 1, 1, {0,0,0,0,0,22}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
91 /* Unused, left here for documentary purposes */
93 #define SECURITY_NULL_SID_AUTHORITY 0
94 #define SECURITY_WORLD_SID_AUTHORITY 1
95 #define SECURITY_LOCAL_SID_AUTHORITY 2
96 #define SECURITY_CREATOR_SID_AUTHORITY 3
97 #define SECURITY_NT_AUTHORITY 5
101 * An NT compatible anonymous token.
104 static DOM_SID anon_sid_array[3] =
105 { { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
106 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
107 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
108 NT_USER_TOKEN anonymous_token = { 3, anon_sid_array, SE_NONE };
110 static DOM_SID system_sid_array[1] =
111 { { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
112 NT_USER_TOKEN system_token = { 1, system_sid_array, SE_ALL_PRIVS };
114 /****************************************************************************
115 Lookup string names for SID types.
116 ****************************************************************************/
118 static const struct {
119 enum lsa_SidType sid_type;
121 } sid_name_type[] = {
122 {SID_NAME_USER, "User"},
123 {SID_NAME_DOM_GRP, "Domain Group"},
124 {SID_NAME_DOMAIN, "Domain"},
125 {SID_NAME_ALIAS, "Local Group"},
126 {SID_NAME_WKN_GRP, "Well-known Group"},
127 {SID_NAME_DELETED, "Deleted Account"},
128 {SID_NAME_INVALID, "Invalid Account"},
129 {SID_NAME_UNKNOWN, "UNKNOWN"},
130 {SID_NAME_COMPUTER, "Computer"},
132 {(enum lsa_SidType)0, NULL}
135 const char *sid_type_lookup(uint32 sid_type)
139 /* Look through list */
140 while(sid_name_type[i].sid_type != 0) {
141 if (sid_name_type[i].sid_type == sid_type)
142 return sid_name_type[i].string;
147 return "SID *TYPE* is INVALID";
150 /**************************************************************************
151 Create the SYSTEM token.
152 ***************************************************************************/
154 NT_USER_TOKEN *get_system_token(void)
156 return &system_token;
159 /******************************************************************
160 get the default domain/netbios name to be used when dealing
161 with our passdb list of accounts
162 ******************************************************************/
164 const char *get_global_sam_name(void)
166 if ((lp_server_role() == ROLE_DOMAIN_PDC) || (lp_server_role() == ROLE_DOMAIN_BDC)) {
167 return lp_workgroup();
169 return global_myname();
172 /*****************************************************************
173 Convert a SID to an ascii string.
174 *****************************************************************/
176 char *sid_to_fstring(fstring sidstr_out, const DOM_SID *sid)
178 char *str = sid_string_talloc(talloc_tos(), sid);
179 fstrcpy(sidstr_out, str);
184 /*****************************************************************
185 Essentially a renamed dom_sid_string from librpc/ndr with a
186 panic if it didn't work
188 This introduces a dependency on librpc/ndr/sid.o which can easily
189 be turned around if necessary
190 *****************************************************************/
192 char *sid_string_talloc(TALLOC_CTX *mem_ctx, const DOM_SID *sid)
194 char *result = dom_sid_string(mem_ctx, sid);
195 SMB_ASSERT(result != NULL);
199 /*****************************************************************
200 Useful function for debug lines.
201 *****************************************************************/
203 char *sid_string_dbg(const DOM_SID *sid)
205 return sid_string_talloc(talloc_tos(), sid);
208 /*****************************************************************
210 *****************************************************************/
212 char *sid_string_tos(const DOM_SID *sid)
214 return sid_string_talloc(talloc_tos(), sid);
217 /*****************************************************************
218 Convert a string to a SID. Returns True on success, False on fail.
219 *****************************************************************/
221 bool string_to_sid(struct dom_sid *sidout, const char *sidstr)
223 if (!dom_sid_parse(sidstr, sidout)) {
224 DEBUG(3, ("string_to_sid: Sid %s is not in a valid format.\n",
231 /*****************************************************************
232 Add a rid to the end of a sid
233 *****************************************************************/
235 bool sid_append_rid(DOM_SID *sid, uint32 rid)
237 if (sid->num_auths < MAXSUBAUTHS) {
238 sid->sub_auths[sid->num_auths++] = rid;
244 bool sid_compose(DOM_SID *dst, const DOM_SID *domain_sid, uint32 rid)
246 sid_copy(dst, domain_sid);
247 return sid_append_rid(dst, rid);
250 /*****************************************************************
251 Removes the last rid from the end of a sid
252 *****************************************************************/
254 bool sid_split_rid(DOM_SID *sid, uint32 *rid)
256 if (sid->num_auths > 0) {
258 *rid = sid->sub_auths[sid->num_auths];
264 /*****************************************************************
265 Return the last rid from the end of a sid
266 *****************************************************************/
268 bool sid_peek_rid(const DOM_SID *sid, uint32 *rid)
273 if (sid->num_auths > 0) {
274 *rid = sid->sub_auths[sid->num_auths - 1];
280 /*****************************************************************
281 Return the last rid from the end of a sid
282 and check the sid against the exp_dom_sid
283 *****************************************************************/
285 bool sid_peek_check_rid(const DOM_SID *exp_dom_sid, const DOM_SID *sid, uint32 *rid)
287 if (!exp_dom_sid || !sid || !rid)
290 if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
294 if (sid_compare_domain(exp_dom_sid, sid)!=0){
299 return sid_peek_rid(sid, rid);
302 /*****************************************************************
304 *****************************************************************/
306 void sid_copy(DOM_SID *dst, const DOM_SID *src)
312 dst->sid_rev_num = src->sid_rev_num;
313 dst->num_auths = src->num_auths;
315 memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
317 for (i = 0; i < src->num_auths; i++)
318 dst->sub_auths[i] = src->sub_auths[i];
321 /*****************************************************************
322 Write a sid out into on-the-wire format.
323 *****************************************************************/
325 bool sid_linearize(char *outbuf, size_t len, const DOM_SID *sid)
329 if (len < ndr_size_dom_sid(sid, NULL, 0))
332 SCVAL(outbuf,0,sid->sid_rev_num);
333 SCVAL(outbuf,1,sid->num_auths);
334 memcpy(&outbuf[2], sid->id_auth, 6);
335 for(i = 0; i < sid->num_auths; i++)
336 SIVAL(outbuf, 8 + (i*4), sid->sub_auths[i]);
341 /*****************************************************************
342 Parse a on-the-wire SID to a DOM_SID.
343 *****************************************************************/
345 bool sid_parse(const char *inbuf, size_t len, DOM_SID *sid)
353 sid->sid_rev_num = CVAL(inbuf, 0);
354 sid->num_auths = CVAL(inbuf, 1);
355 memcpy(sid->id_auth, inbuf+2, 6);
356 if (len < 8 + sid->num_auths*4)
358 for (i=0;i<sid->num_auths;i++)
359 sid->sub_auths[i] = IVAL(inbuf, 8+i*4);
363 /*****************************************************************
364 Compare the auth portion of two sids.
365 *****************************************************************/
367 static int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2)
378 if (sid1->sid_rev_num != sid2->sid_rev_num)
379 return sid1->sid_rev_num - sid2->sid_rev_num;
381 for (i = 0; i < 6; i++)
382 if (sid1->id_auth[i] != sid2->id_auth[i])
383 return sid1->id_auth[i] - sid2->id_auth[i];
388 /*****************************************************************
390 *****************************************************************/
392 int sid_compare(const DOM_SID *sid1, const DOM_SID *sid2)
403 /* Compare most likely different rids, first: i.e start at end */
404 if (sid1->num_auths != sid2->num_auths)
405 return sid1->num_auths - sid2->num_auths;
407 for (i = sid1->num_auths-1; i >= 0; --i)
408 if (sid1->sub_auths[i] != sid2->sub_auths[i])
409 return sid1->sub_auths[i] - sid2->sub_auths[i];
411 return sid_compare_auth(sid1, sid2);
414 /*****************************************************************
415 See if 2 SIDs are in the same domain
416 this just compares the leading sub-auths
417 *****************************************************************/
419 int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
423 n = MIN(sid1->num_auths, sid2->num_auths);
425 for (i = n-1; i >= 0; --i)
426 if (sid1->sub_auths[i] != sid2->sub_auths[i])
427 return sid1->sub_auths[i] - sid2->sub_auths[i];
429 return sid_compare_auth(sid1, sid2);
432 /*****************************************************************
434 *****************************************************************/
436 bool sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
438 return sid_compare(sid1, sid2) == 0;
441 /*****************************************************************
442 Returns true if SID is internal (and non-mappable).
443 *****************************************************************/
445 bool non_mappable_sid(DOM_SID *sid)
451 sid_split_rid(&dom, &rid);
453 if (sid_equal(&dom, &global_sid_Builtin))
456 if (sid_equal(&dom, &global_sid_NT_Authority))
462 /*****************************************************************
463 Return the binary string representation of a DOM_SID.
465 *****************************************************************/
467 char *sid_binstring(TALLOC_CTX *mem_ctx, const DOM_SID *sid)
471 int len = ndr_size_dom_sid(sid, NULL, 0);
472 buf = talloc_array(mem_ctx, uint8_t, len);
476 sid_linearize((char *)buf, len, sid);
477 s = binary_string_rfc2254(mem_ctx, buf, len);
482 /*****************************************************************
483 Return the binary string representation of a DOM_SID.
485 *****************************************************************/
487 char *sid_binstring_hex(const DOM_SID *sid)
490 int len = ndr_size_dom_sid(sid, NULL, 0);
491 buf = (char *)SMB_MALLOC(len);
494 sid_linearize(buf, len, sid);
495 s = binary_string(buf, len);
500 /*******************************************************************
501 Tallocs a duplicate SID.
502 ********************************************************************/
504 struct dom_sid *sid_dup_talloc(TALLOC_CTX *ctx, const struct dom_sid *src)
511 dst = talloc_zero(ctx, struct dom_sid);
519 /********************************************************************
520 Add SID to an array SIDs
521 ********************************************************************/
523 NTSTATUS add_sid_to_array(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
524 DOM_SID **sids, size_t *num)
526 *sids = TALLOC_REALLOC_ARRAY(mem_ctx, *sids, DOM_SID,
530 return NT_STATUS_NO_MEMORY;
533 sid_copy(&((*sids)[*num]), sid);
540 /********************************************************************
541 Add SID to an array SIDs ensuring that it is not already there
542 ********************************************************************/
544 NTSTATUS add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
545 DOM_SID **sids, size_t *num_sids)
549 for (i=0; i<(*num_sids); i++) {
550 if (sid_compare(sid, &(*sids)[i]) == 0)
554 return add_sid_to_array(mem_ctx, sid, sids, num_sids);
557 /********************************************************************
558 Remove SID from an array
559 ********************************************************************/
561 void del_sid_from_array(const DOM_SID *sid, DOM_SID **sids, size_t *num)
563 DOM_SID *sid_list = *sids;
566 for ( i=0; i<*num; i++ ) {
568 /* if we find the SID, then decrement the count
569 and break out of the loop */
571 if ( sid_equal(sid, &sid_list[i]) ) {
577 /* This loop will copy the remainder of the array
578 if i < num of sids ni the array */
580 for ( ; i<*num; i++ )
581 sid_copy( &sid_list[i], &sid_list[i+1] );
586 bool add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
587 uint32 rid, uint32 **pp_rids, size_t *p_num)
591 for (i=0; i<*p_num; i++) {
592 if ((*pp_rids)[i] == rid)
596 *pp_rids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_rids, uint32, *p_num+1);
598 if (*pp_rids == NULL) {
603 (*pp_rids)[*p_num] = rid;
608 bool is_null_sid(const DOM_SID *sid)
610 static const DOM_SID null_sid = {0};
611 return sid_equal(sid, &null_sid);
614 bool is_sid_in_token(const NT_USER_TOKEN *token, const DOM_SID *sid)
618 for (i=0; i<token->num_sids; i++) {
619 if (sid_compare(sid, &token->user_sids[i]) == 0)
625 NTSTATUS sid_array_from_info3(TALLOC_CTX *mem_ctx,
626 const struct netr_SamInfo3 *info3,
628 size_t *num_user_sids,
629 bool include_user_group_rid,
630 bool skip_ressource_groups)
634 DOM_SID *sid_array = NULL;
638 if (include_user_group_rid) {
639 if (!sid_compose(&sid, info3->base.domain_sid, info3->base.rid)) {
640 DEBUG(3, ("could not compose user SID from rid 0x%x\n",
642 return NT_STATUS_INVALID_PARAMETER;
644 status = add_sid_to_array(mem_ctx, &sid, &sid_array, &num_sids);
645 if (!NT_STATUS_IS_OK(status)) {
646 DEBUG(3, ("could not append user SID from rid 0x%x\n",
652 if (!sid_compose(&sid, info3->base.domain_sid, info3->base.primary_gid)) {
653 DEBUG(3, ("could not compose group SID from rid 0x%x\n",
654 info3->base.primary_gid));
655 return NT_STATUS_INVALID_PARAMETER;
657 status = add_sid_to_array(mem_ctx, &sid, &sid_array, &num_sids);
658 if (!NT_STATUS_IS_OK(status)) {
659 DEBUG(3, ("could not append group SID from rid 0x%x\n",
664 for (i = 0; i < info3->base.groups.count; i++) {
665 /* Don't add the primary group sid twice. */
666 if (info3->base.primary_gid == info3->base.groups.rids[i].rid) {
669 if (!sid_compose(&sid, info3->base.domain_sid,
670 info3->base.groups.rids[i].rid)) {
671 DEBUG(3, ("could not compose SID from additional group "
672 "rid 0x%x\n", info3->base.groups.rids[i].rid));
673 return NT_STATUS_INVALID_PARAMETER;
675 status = add_sid_to_array(mem_ctx, &sid, &sid_array, &num_sids);
676 if (!NT_STATUS_IS_OK(status)) {
677 DEBUG(3, ("could not append SID from additional group "
678 "rid 0x%x\n", info3->base.groups.rids[i].rid));
683 /* Copy 'other' sids. We need to do sid filtering here to
684 prevent possible elevation of privileges. See:
686 http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
689 for (i = 0; i < info3->sidcount; i++) {
691 if (skip_ressource_groups &&
692 (info3->sids[i].attributes & SE_GROUP_RESOURCE)) {
696 status = add_sid_to_array(mem_ctx, info3->sids[i].sid,
697 &sid_array, &num_sids);
698 if (!NT_STATUS_IS_OK(status)) {
699 DEBUG(3, ("could not add SID to array: %s\n",
700 sid_string_dbg(info3->sids[i].sid)));
705 *user_sids = sid_array;
706 *num_user_sids = num_sids;