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/>.
32 const DOM_SID global_sid_World_Domain = /* Everyone domain */
33 { 1, 0, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
34 const DOM_SID global_sid_World = /* Everyone */
35 { 1, 1, {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_Creator_Owner_Domain = /* Creator Owner domain */
37 { 1, 0, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
38 const DOM_SID global_sid_NT_Authority = /* NT Authority */
39 { 1, 0, {0,0,0,0,0,5}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
40 const DOM_SID global_sid_System = /* System */
41 { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
42 const DOM_SID global_sid_NULL = /* NULL sid */
43 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
44 const DOM_SID global_sid_Authenticated_Users = /* All authenticated rids */
45 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
46 const DOM_SID global_sid_Network = /* Network rids */
47 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
49 const DOM_SID global_sid_Creator_Owner = /* Creator Owner */
50 { 1, 1, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
51 const DOM_SID global_sid_Creator_Group = /* Creator Group */
52 { 1, 1, {0,0,0,0,0,3}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
53 const DOM_SID global_sid_Anonymous = /* Anonymous login */
54 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
56 const DOM_SID global_sid_Builtin = /* Local well-known domain */
57 { 1, 1, {0,0,0,0,0,5}, {32,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
58 const DOM_SID global_sid_Builtin_Administrators = /* Builtin administrators */
59 { 1, 2, {0,0,0,0,0,5}, {32,544,0,0,0,0,0,0,0,0,0,0,0,0,0}};
60 const DOM_SID global_sid_Builtin_Users = /* Builtin users */
61 { 1, 2, {0,0,0,0,0,5}, {32,545,0,0,0,0,0,0,0,0,0,0,0,0,0}};
62 const DOM_SID global_sid_Builtin_Guests = /* Builtin guest users */
63 { 1, 2, {0,0,0,0,0,5}, {32,546,0,0,0,0,0,0,0,0,0,0,0,0,0}};
64 const DOM_SID global_sid_Builtin_Power_Users = /* Builtin power users */
65 { 1, 2, {0,0,0,0,0,5}, {32,547,0,0,0,0,0,0,0,0,0,0,0,0,0}};
66 const DOM_SID global_sid_Builtin_Account_Operators = /* Builtin account operators */
67 { 1, 2, {0,0,0,0,0,5}, {32,548,0,0,0,0,0,0,0,0,0,0,0,0,0}};
68 const DOM_SID global_sid_Builtin_Server_Operators = /* Builtin server operators */
69 { 1, 2, {0,0,0,0,0,5}, {32,549,0,0,0,0,0,0,0,0,0,0,0,0,0}};
70 const DOM_SID global_sid_Builtin_Print_Operators = /* Builtin print operators */
71 { 1, 2, {0,0,0,0,0,5}, {32,550,0,0,0,0,0,0,0,0,0,0,0,0,0}};
72 const DOM_SID global_sid_Builtin_Backup_Operators = /* Builtin backup operators */
73 { 1, 2, {0,0,0,0,0,5}, {32,551,0,0,0,0,0,0,0,0,0,0,0,0,0}};
74 const DOM_SID global_sid_Builtin_Replicator = /* Builtin replicator */
75 { 1, 2, {0,0,0,0,0,5}, {32,552,0,0,0,0,0,0,0,0,0,0,0,0,0}};
76 const DOM_SID global_sid_Builtin_PreWin2kAccess = /* Builtin pre win2k access */
77 { 1, 2, {0,0,0,0,0,5}, {32,554,0,0,0,0,0,0,0,0,0,0,0,0,0}};
79 const DOM_SID global_sid_Unix_Users = /* Unmapped Unix users */
80 { 1, 1, {0,0,0,0,0,22}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
81 const DOM_SID global_sid_Unix_Groups = /* Unmapped Unix groups */
82 { 1, 1, {0,0,0,0,0,22}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
84 /* Unused, left here for documentary purposes */
86 #define SECURITY_NULL_SID_AUTHORITY 0
87 #define SECURITY_WORLD_SID_AUTHORITY 1
88 #define SECURITY_LOCAL_SID_AUTHORITY 2
89 #define SECURITY_CREATOR_SID_AUTHORITY 3
90 #define SECURITY_NT_AUTHORITY 5
94 * An NT compatible anonymous token.
97 static DOM_SID anon_sid_array[3] =
98 { { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
99 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
100 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
101 NT_USER_TOKEN anonymous_token = { 3, anon_sid_array, SE_NONE };
103 static DOM_SID system_sid_array[1] =
104 { { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
105 NT_USER_TOKEN system_token = { 1, system_sid_array, SE_ALL_PRIVS };
107 /****************************************************************************
108 Lookup string names for SID types.
109 ****************************************************************************/
111 static const struct {
112 enum lsa_SidType sid_type;
114 } sid_name_type[] = {
115 {SID_NAME_USER, "User"},
116 {SID_NAME_DOM_GRP, "Domain Group"},
117 {SID_NAME_DOMAIN, "Domain"},
118 {SID_NAME_ALIAS, "Local Group"},
119 {SID_NAME_WKN_GRP, "Well-known Group"},
120 {SID_NAME_DELETED, "Deleted Account"},
121 {SID_NAME_INVALID, "Invalid Account"},
122 {SID_NAME_UNKNOWN, "UNKNOWN"},
123 {SID_NAME_COMPUTER, "Computer"},
125 {(enum lsa_SidType)0, NULL}
128 const char *sid_type_lookup(uint32 sid_type)
132 /* Look through list */
133 while(sid_name_type[i].sid_type != 0) {
134 if (sid_name_type[i].sid_type == sid_type)
135 return sid_name_type[i].string;
140 return "SID *TYPE* is INVALID";
143 /**************************************************************************
144 Create the SYSTEM token.
145 ***************************************************************************/
147 NT_USER_TOKEN *get_system_token(void)
149 return &system_token;
152 /******************************************************************
153 get the default domain/netbios name to be used when dealing
154 with our passdb list of accounts
155 ******************************************************************/
157 const char *get_global_sam_name(void)
159 if ((lp_server_role() == ROLE_DOMAIN_PDC) || (lp_server_role() == ROLE_DOMAIN_BDC)) {
160 return lp_workgroup();
162 return global_myname();
165 /*****************************************************************
166 Convert a SID to an ascii string.
167 *****************************************************************/
169 char *sid_to_string(fstring sidstr_out, const DOM_SID *sid)
176 fstrcpy(sidstr_out, "(NULL SID)");
181 * BIG NOTE: this function only does SIDS where the identauth is not >= 2^32
182 * in a range of 2^48.
184 ia = (sid->id_auth[5]) +
185 (sid->id_auth[4] << 8 ) +
186 (sid->id_auth[3] << 16) +
187 (sid->id_auth[2] << 24);
189 slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
191 for (i = 0; i < sid->num_auths; i++) {
192 slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
193 fstrcat(sidstr_out, subauth);
199 /*****************************************************************
200 Useful function for debug lines.
201 *****************************************************************/
203 const char *sid_string_static(const DOM_SID *sid)
205 static fstring sid_str;
206 sid_to_string(sid_str, sid);
210 /*****************************************************************
211 Convert a string to a SID. Returns True on success, False on fail.
212 *****************************************************************/
214 BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
218 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
221 if ((sidstr[0] != 'S' && sidstr[0] != 's') || sidstr[1] != '-') {
222 DEBUG(3,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
226 ZERO_STRUCTP(sidout);
228 /* Get the revision number. */
230 conv = (uint32) strtoul(p, &q, 10);
231 if (!q || (*q != '-')) {
232 DEBUG(3,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
235 sidout->sid_rev_num = (uint8) conv;
239 conv = (uint32) strtoul(q, &q, 10);
240 if (!q || (*q != '-')) {
241 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
244 /* identauth in decimal should be < 2^32 */
245 /* NOTE - the conv value is in big-endian format. */
246 sidout->id_auth[0] = 0;
247 sidout->id_auth[1] = 0;
248 sidout->id_auth[2] = (conv & 0xff000000) >> 24;
249 sidout->id_auth[3] = (conv & 0x00ff0000) >> 16;
250 sidout->id_auth[4] = (conv & 0x0000ff00) >> 8;
251 sidout->id_auth[5] = (conv & 0x000000ff);
254 sidout->num_auths = 0;
256 for(conv = (uint32) strtoul(q, &q, 10);
257 q && (*q =='-' || *q =='\0') && (sidout->num_auths < MAXSUBAUTHS);
258 conv = (uint32) strtoul(q, &q, 10)) {
259 sid_append_rid(sidout, conv);
268 DOM_SID *string_sid_talloc(TALLOC_CTX *mem_ctx, const char *sidstr)
270 DOM_SID *result = TALLOC_P(mem_ctx, DOM_SID);
275 if (!string_to_sid(result, sidstr))
281 /*****************************************************************
282 Add a rid to the end of a sid
283 *****************************************************************/
285 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
287 if (sid->num_auths < MAXSUBAUTHS) {
288 sid->sub_auths[sid->num_auths++] = rid;
294 BOOL sid_compose(DOM_SID *dst, const DOM_SID *domain_sid, uint32 rid)
296 sid_copy(dst, domain_sid);
297 return sid_append_rid(dst, rid);
300 /*****************************************************************
301 Removes the last rid from the end of a sid
302 *****************************************************************/
304 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
306 if (sid->num_auths > 0) {
308 *rid = sid->sub_auths[sid->num_auths];
314 /*****************************************************************
315 Return the last rid from the end of a sid
316 *****************************************************************/
318 BOOL sid_peek_rid(const DOM_SID *sid, uint32 *rid)
323 if (sid->num_auths > 0) {
324 *rid = sid->sub_auths[sid->num_auths - 1];
330 /*****************************************************************
331 Return the last rid from the end of a sid
332 and check the sid against the exp_dom_sid
333 *****************************************************************/
335 BOOL sid_peek_check_rid(const DOM_SID *exp_dom_sid, const DOM_SID *sid, uint32 *rid)
337 if (!exp_dom_sid || !sid || !rid)
340 if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
344 if (sid_compare_domain(exp_dom_sid, sid)!=0){
349 return sid_peek_rid(sid, rid);
352 /*****************************************************************
354 *****************************************************************/
356 void sid_copy(DOM_SID *dst, const DOM_SID *src)
362 dst->sid_rev_num = src->sid_rev_num;
363 dst->num_auths = src->num_auths;
365 memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
367 for (i = 0; i < src->num_auths; i++)
368 dst->sub_auths[i] = src->sub_auths[i];
371 /*****************************************************************
372 Write a sid out into on-the-wire format.
373 *****************************************************************/
375 BOOL sid_linearize(char *outbuf, size_t len, const DOM_SID *sid)
379 if (len < sid_size(sid))
382 SCVAL(outbuf,0,sid->sid_rev_num);
383 SCVAL(outbuf,1,sid->num_auths);
384 memcpy(&outbuf[2], sid->id_auth, 6);
385 for(i = 0; i < sid->num_auths; i++)
386 SIVAL(outbuf, 8 + (i*4), sid->sub_auths[i]);
391 /*****************************************************************
392 Parse a on-the-wire SID to a DOM_SID.
393 *****************************************************************/
395 BOOL sid_parse(const char *inbuf, size_t len, DOM_SID *sid)
403 sid->sid_rev_num = CVAL(inbuf, 0);
404 sid->num_auths = CVAL(inbuf, 1);
405 memcpy(sid->id_auth, inbuf+2, 6);
406 if (len < 8 + sid->num_auths*4)
408 for (i=0;i<sid->num_auths;i++)
409 sid->sub_auths[i] = IVAL(inbuf, 8+i*4);
413 /*****************************************************************
414 Compare the auth portion of two sids.
415 *****************************************************************/
417 static int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2)
428 if (sid1->sid_rev_num != sid2->sid_rev_num)
429 return sid1->sid_rev_num - sid2->sid_rev_num;
431 for (i = 0; i < 6; i++)
432 if (sid1->id_auth[i] != sid2->id_auth[i])
433 return sid1->id_auth[i] - sid2->id_auth[i];
438 /*****************************************************************
440 *****************************************************************/
442 int sid_compare(const DOM_SID *sid1, const DOM_SID *sid2)
453 /* Compare most likely different rids, first: i.e start at end */
454 if (sid1->num_auths != sid2->num_auths)
455 return sid1->num_auths - sid2->num_auths;
457 for (i = sid1->num_auths-1; i >= 0; --i)
458 if (sid1->sub_auths[i] != sid2->sub_auths[i])
459 return sid1->sub_auths[i] - sid2->sub_auths[i];
461 return sid_compare_auth(sid1, sid2);
464 /*****************************************************************
465 See if 2 SIDs are in the same domain
466 this just compares the leading sub-auths
467 *****************************************************************/
469 int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
473 n = MIN(sid1->num_auths, sid2->num_auths);
475 for (i = n-1; i >= 0; --i)
476 if (sid1->sub_auths[i] != sid2->sub_auths[i])
477 return sid1->sub_auths[i] - sid2->sub_auths[i];
479 return sid_compare_auth(sid1, sid2);
482 /*****************************************************************
484 *****************************************************************/
486 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
488 return sid_compare(sid1, sid2) == 0;
491 /*****************************************************************
492 Calculates size of a sid.
493 *****************************************************************/
495 size_t sid_size(const DOM_SID *sid)
500 return sid->num_auths * sizeof(uint32) + 8;
503 /*****************************************************************
504 Returns true if SID is internal (and non-mappable).
505 *****************************************************************/
507 BOOL non_mappable_sid(DOM_SID *sid)
513 sid_split_rid(&dom, &rid);
515 if (sid_equal(&dom, &global_sid_Builtin))
518 if (sid_equal(&dom, &global_sid_NT_Authority))
524 /*****************************************************************
525 Return the binary string representation of a DOM_SID.
527 *****************************************************************/
529 char *sid_binstring(const DOM_SID *sid)
532 int len = sid_size(sid);
533 buf = (char *)SMB_MALLOC(len);
536 sid_linearize(buf, len, sid);
537 s = binary_string_rfc2254(buf, len);
542 /*****************************************************************
543 Return the binary string representation of a DOM_SID.
545 *****************************************************************/
547 char *sid_binstring_hex(const DOM_SID *sid)
550 int len = sid_size(sid);
551 buf = (char *)SMB_MALLOC(len);
554 sid_linearize(buf, len, sid);
555 s = binary_string(buf, len);
560 /*******************************************************************
561 Tallocs a duplicate SID.
562 ********************************************************************/
564 DOM_SID *sid_dup_talloc(TALLOC_CTX *ctx, const DOM_SID *src)
571 if((dst = TALLOC_ZERO_P(ctx, DOM_SID)) != NULL) {
578 /********************************************************************
579 Add SID to an array SIDs
580 ********************************************************************/
582 BOOL add_sid_to_array(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
583 DOM_SID **sids, size_t *num)
585 *sids = TALLOC_REALLOC_ARRAY(mem_ctx, *sids, DOM_SID,
592 sid_copy(&((*sids)[*num]), sid);
599 /********************************************************************
600 Add SID to an array SIDs ensuring that it is not already there
601 ********************************************************************/
603 BOOL add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
604 DOM_SID **sids, size_t *num_sids)
608 for (i=0; i<(*num_sids); i++) {
609 if (sid_compare(sid, &(*sids)[i]) == 0)
613 return add_sid_to_array(mem_ctx, sid, sids, num_sids);
616 /********************************************************************
617 Remove SID from an array
618 ********************************************************************/
620 void del_sid_from_array(const DOM_SID *sid, DOM_SID **sids, size_t *num)
622 DOM_SID *sid_list = *sids;
625 for ( i=0; i<*num; i++ ) {
627 /* if we find the SID, then decrement the count
628 and break out of the loop */
630 if ( sid_equal(sid, &sid_list[i]) ) {
636 /* This loop will copy the remainder of the array
637 if i < num of sids ni the array */
639 for ( ; i<*num; i++ )
640 sid_copy( &sid_list[i], &sid_list[i+1] );
645 BOOL add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
646 uint32 rid, uint32 **pp_rids, size_t *p_num)
650 for (i=0; i<*p_num; i++) {
651 if ((*pp_rids)[i] == rid)
655 *pp_rids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_rids, uint32, *p_num+1);
657 if (*pp_rids == NULL) {
662 (*pp_rids)[*p_num] = rid;
667 BOOL is_null_sid(const DOM_SID *sid)
669 static const DOM_SID null_sid = {0};
670 return sid_equal(sid, &null_sid);