source: trunk-3.0/source/lib/util_sid.c@ 102

Last change on this file since 102 was 39, checked in by Paul Smedley, 18 years ago

Upgrade source to 3.0.25a

File size: 19.1 KB
Line 
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
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.
15
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.
20
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.
24*/
25
26#include "includes.h"
27
28/*
29 * Some useful sids
30 */
31
32
33const 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}};
35const 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}};
37const 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}};
39const 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}};
41const 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}};
43const 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}};
45const 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}};
47const 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}};
49
50const 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}};
52const 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}};
54const 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}};
56
57const 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}};
59const 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}};
61const 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}};
63const 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}};
65const 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}};
67const 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}};
69const 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}};
71const 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}};
73const 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}};
75const 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}};
77const 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}};
79
80const 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}};
82const 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}};
84
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
93
94/*
95 * An NT compatible anonymous token.
96 */
97
98static 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}} };
102NT_USER_TOKEN anonymous_token = { 3, anon_sid_array, SE_NONE };
103
104static 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}} };
106NT_USER_TOKEN system_token = { 1, system_sid_array, SE_ALL_PRIVS };
107
108/****************************************************************************
109 Lookup string names for SID types.
110****************************************************************************/
111
112static 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"},
125
126 {(enum lsa_SidType)0, NULL}
127};
128
129const char *sid_type_lookup(uint32 sid_type)
130{
131 int i = 0;
132
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++;
138 }
139
140 /* Default return */
141 return "SID *TYPE* is INVALID";
142}
143
144/**************************************************************************
145 Create the SYSTEM token.
146***************************************************************************/
147
148NT_USER_TOKEN *get_system_token(void)
149{
150 return &system_token;
151}
152
153/******************************************************************
154 get the default domain/netbios name to be used when dealing
155 with our passdb list of accounts
156******************************************************************/
157
158const char *get_global_sam_name(void)
159{
160 if ((lp_server_role() == ROLE_DOMAIN_PDC) || (lp_server_role() == ROLE_DOMAIN_BDC)) {
161 return lp_workgroup();
162 }
163 return global_myname();
164}
165
166/*****************************************************************
167 Convert a SID to an ascii string.
168*****************************************************************/
169
170char *sid_to_string(fstring sidstr_out, const DOM_SID *sid)
171{
172 char subauth[16];
173 int i;
174 uint32 ia;
175
176 if (!sid) {
177 fstrcpy(sidstr_out, "(NULL SID)");
178 return sidstr_out;
179 }
180
181 /*
182 * BIG NOTE: this function only does SIDS where the identauth is not >= 2^32
183 * in a range of 2^48.
184 */
185 ia = (sid->id_auth[5]) +
186 (sid->id_auth[4] << 8 ) +
187 (sid->id_auth[3] << 16) +
188 (sid->id_auth[2] << 24);
189
190 slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
191
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);
195 }
196
197 return sidstr_out;
198}
199
200/*****************************************************************
201 Useful function for debug lines.
202*****************************************************************/
203
204const char *sid_string_static(const DOM_SID *sid)
205{
206 static fstring sid_str;
207 sid_to_string(sid_str, sid);
208 return sid_str;
209}
210
211/*****************************************************************
212 Convert a string to a SID. Returns True on success, False on fail.
213*****************************************************************/
214
215BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
216{
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;
221
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;
225 }
226
227 ZERO_STRUCTP(sidout);
228
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;
235 }
236 sidout->sid_rev_num = (uint8) conv;
237 q++;
238
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;
244 }
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);
253
254 q++;
255 sidout->num_auths = 0;
256
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++;
264 }
265
266 return True;
267}
268
269DOM_SID *string_sid_talloc(TALLOC_CTX *mem_ctx, const char *sidstr)
270{
271 DOM_SID *result = TALLOC_P(mem_ctx, DOM_SID);
272
273 if (result == NULL)
274 return NULL;
275
276 if (!string_to_sid(result, sidstr))
277 return NULL;
278
279 return result;
280}
281
282/*****************************************************************
283 Add a rid to the end of a sid
284*****************************************************************/
285
286BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
287{
288 if (sid->num_auths < MAXSUBAUTHS) {
289 sid->sub_auths[sid->num_auths++] = rid;
290 return True;
291 }
292 return False;
293}
294
295BOOL sid_compose(DOM_SID *dst, const DOM_SID *domain_sid, uint32 rid)
296{
297 sid_copy(dst, domain_sid);
298 return sid_append_rid(dst, rid);
299}
300
301/*****************************************************************
302 Removes the last rid from the end of a sid
303*****************************************************************/
304
305BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
306{
307 if (sid->num_auths > 0) {
308 sid->num_auths--;
309 *rid = sid->sub_auths[sid->num_auths];
310 return True;
311 }
312 return False;
313}
314
315/*****************************************************************
316 Return the last rid from the end of a sid
317*****************************************************************/
318
319BOOL sid_peek_rid(const DOM_SID *sid, uint32 *rid)
320{
321 if (!sid || !rid)
322 return False;
323
324 if (sid->num_auths > 0) {
325 *rid = sid->sub_auths[sid->num_auths - 1];
326 return True;
327 }
328 return False;
329}
330
331/*****************************************************************
332 Return the last rid from the end of a sid
333 and check the sid against the exp_dom_sid
334*****************************************************************/
335
336BOOL sid_peek_check_rid(const DOM_SID *exp_dom_sid, const DOM_SID *sid, uint32 *rid)
337{
338 if (!exp_dom_sid || !sid || !rid)
339 return False;
340
341 if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
342 return False;
343 }
344
345 if (sid_compare_domain(exp_dom_sid, sid)!=0){
346 *rid=(-1);
347 return False;
348 }
349
350 return sid_peek_rid(sid, rid);
351}
352
353/*****************************************************************
354 Copies a sid
355*****************************************************************/
356
357void sid_copy(DOM_SID *dst, const DOM_SID *src)
358{
359 int i;
360
361 ZERO_STRUCTP(dst);
362
363 dst->sid_rev_num = src->sid_rev_num;
364 dst->num_auths = src->num_auths;
365
366 memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
367
368 for (i = 0; i < src->num_auths; i++)
369 dst->sub_auths[i] = src->sub_auths[i];
370}
371
372/*****************************************************************
373 Write a sid out into on-the-wire format.
374*****************************************************************/
375
376BOOL sid_linearize(char *outbuf, size_t len, const DOM_SID *sid)
377{
378 size_t i;
379
380 if (len < sid_size(sid))
381 return False;
382
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]);
388
389 return True;
390}
391
392/*****************************************************************
393 Parse a on-the-wire SID to a DOM_SID.
394*****************************************************************/
395
396BOOL sid_parse(const char *inbuf, size_t len, DOM_SID *sid)
397{
398 int i;
399 if (len < 8)
400 return False;
401
402 ZERO_STRUCTP(sid);
403
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;
412}
413
414/*****************************************************************
415 Compare the auth portion of two sids.
416*****************************************************************/
417
418static int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2)
419{
420 int i;
421
422 if (sid1 == sid2)
423 return 0;
424 if (!sid1)
425 return -1;
426 if (!sid2)
427 return 1;
428
429 if (sid1->sid_rev_num != sid2->sid_rev_num)
430 return sid1->sid_rev_num - sid2->sid_rev_num;
431
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];
435
436 return 0;
437}
438
439/*****************************************************************
440 Compare two sids.
441*****************************************************************/
442
443int sid_compare(const DOM_SID *sid1, const DOM_SID *sid2)
444{
445 int i;
446
447 if (sid1 == sid2)
448 return 0;
449 if (!sid1)
450 return -1;
451 if (!sid2)
452 return 1;
453
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;
457
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];
461
462 return sid_compare_auth(sid1, sid2);
463}
464
465/*****************************************************************
466 See if 2 SIDs are in the same domain
467 this just compares the leading sub-auths
468*****************************************************************/
469
470int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
471{
472 int n, i;
473
474 n = MIN(sid1->num_auths, sid2->num_auths);
475
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];
479
480 return sid_compare_auth(sid1, sid2);
481}
482
483/*****************************************************************
484 Compare two sids.
485*****************************************************************/
486
487BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
488{
489 return sid_compare(sid1, sid2) == 0;
490}
491
492/*****************************************************************
493 Calculates size of a sid.
494*****************************************************************/
495
496size_t sid_size(const DOM_SID *sid)
497{
498 if (sid == NULL)
499 return 0;
500
501 return sid->num_auths * sizeof(uint32) + 8;
502}
503
504/*****************************************************************
505 Returns true if SID is internal (and non-mappable).
506*****************************************************************/
507
508BOOL non_mappable_sid(DOM_SID *sid)
509{
510 DOM_SID dom;
511 uint32 rid;
512
513 sid_copy(&dom, sid);
514 sid_split_rid(&dom, &rid);
515
516 if (sid_equal(&dom, &global_sid_Builtin))
517 return True;
518
519 if (sid_equal(&dom, &global_sid_NT_Authority))
520 return True;
521
522 return False;
523}
524
525/*****************************************************************
526 Return the binary string representation of a DOM_SID.
527 Caller must free.
528*****************************************************************/
529
530char *sid_binstring(const DOM_SID *sid)
531{
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;
541}
542
543/*****************************************************************
544 Return the binary string representation of a DOM_SID.
545 Caller must free.
546*****************************************************************/
547
548char *sid_binstring_hex(const DOM_SID *sid)
549{
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;
559}
560
561/*******************************************************************
562 Tallocs a duplicate SID.
563********************************************************************/
564
565DOM_SID *sid_dup_talloc(TALLOC_CTX *ctx, const DOM_SID *src)
566{
567 DOM_SID *dst;
568
569 if(!src)
570 return NULL;
571
572 if((dst = TALLOC_ZERO_P(ctx, DOM_SID)) != NULL) {
573 sid_copy( dst, src);
574 }
575
576 return dst;
577}
578
579/********************************************************************
580 Add SID to an array SIDs
581********************************************************************/
582
583BOOL add_sid_to_array(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
584 DOM_SID **sids, size_t *num)
585{
586 *sids = TALLOC_REALLOC_ARRAY(mem_ctx, *sids, DOM_SID,
587 (*num)+1);
588 if (*sids == NULL) {
589 *num = 0;
590 return False;
591 }
592
593 sid_copy(&((*sids)[*num]), sid);
594 *num += 1;
595
596 return True;
597}
598
599
600/********************************************************************
601 Add SID to an array SIDs ensuring that it is not already there
602********************************************************************/
603
604BOOL add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
605 DOM_SID **sids, size_t *num_sids)
606{
607 size_t i;
608
609 for (i=0; i<(*num_sids); i++) {
610 if (sid_compare(sid, &(*sids)[i]) == 0)
611 return True;
612 }
613
614 return add_sid_to_array(mem_ctx, sid, sids, num_sids);
615}
616
617/********************************************************************
618 Remove SID from an array
619********************************************************************/
620
621void del_sid_from_array(const DOM_SID *sid, DOM_SID **sids, size_t *num)
622{
623 DOM_SID *sid_list = *sids;
624 size_t i;
625
626 for ( i=0; i<*num; i++ ) {
627
628 /* if we find the SID, then decrement the count
629 and break out of the loop */
630
631 if ( sid_equal(sid, &sid_list[i]) ) {
632 *num -= 1;
633 break;
634 }
635 }
636
637 /* This loop will copy the remainder of the array
638 if i < num of sids ni the array */
639
640 for ( ; i<*num; i++ )
641 sid_copy( &sid_list[i], &sid_list[i+1] );
642
643 return;
644}
645
646BOOL add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
647 uint32 rid, uint32 **pp_rids, size_t *p_num)
648{
649 size_t i;
650
651 for (i=0; i<*p_num; i++) {
652 if ((*pp_rids)[i] == rid)
653 return True;
654 }
655
656 *pp_rids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_rids, uint32, *p_num+1);
657
658 if (*pp_rids == NULL) {
659 *p_num = 0;
660 return False;
661 }
662
663 (*pp_rids)[*p_num] = rid;
664 *p_num += 1;
665 return True;
666}
667
668BOOL is_null_sid(const DOM_SID *sid)
669{
670 static const DOM_SID null_sid = {0};
671 return sid_equal(sid, &null_sid);
672}
Note: See TracBrowser for help on using the repository browser.