s3:winbind: remove the method SET_MAPPING from winbind's API
[ira/wip.git] / source3 / lib / util_sid.c
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 3 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, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "../librpc/gen_ndr/ndr_security.h"
27 #include "../librpc/gen_ndr/netlogon.h"
28 #include "../libcli/security/dom_sid.h"
29
30 /*
31  * Some useful sids, more well known sids can be found at
32  * http://support.microsoft.com/kb/243330/EN-US/
33  */
34
35
36 const struct dom_sid global_sid_World_Domain =               /* Everyone domain */
37 { 1, 0, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
38 const struct dom_sid global_sid_World =                      /* Everyone */
39 { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
40 const struct dom_sid global_sid_Creator_Owner_Domain =       /* Creator Owner domain */
41 { 1, 0, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
42 const struct dom_sid global_sid_NT_Authority =                  /* NT Authority */
43 { 1, 0, {0,0,0,0,0,5}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
44 const struct dom_sid global_sid_System =                        /* System */
45 { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
46 const struct dom_sid global_sid_NULL =                          /* NULL sid */
47 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
48 const struct dom_sid global_sid_Authenticated_Users =   /* All authenticated rids */
49 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
50 #if 0
51 /* for documentation */
52 const struct dom_sid global_sid_Restriced =                     /* Restriced Code */
53 { 1, 1, {0,0,0,0,0,5}, {12,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
54 #endif
55 const struct dom_sid global_sid_Network =                       /* Network rids */
56 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
57
58 const struct dom_sid global_sid_Creator_Owner =         /* Creator Owner */
59 { 1, 1, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
60 const struct dom_sid global_sid_Creator_Group =         /* Creator Group */
61 { 1, 1, {0,0,0,0,0,3}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
62 const struct dom_sid global_sid_Anonymous =                     /* Anonymous login */
63 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
64
65 const struct dom_sid global_sid_Builtin =                       /* Local well-known domain */
66 { 1, 1, {0,0,0,0,0,5}, {32,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
67 const struct dom_sid global_sid_Builtin_Administrators =        /* Builtin administrators */
68 { 1, 2, {0,0,0,0,0,5}, {32,544,0,0,0,0,0,0,0,0,0,0,0,0,0}};
69 const struct dom_sid global_sid_Builtin_Users =         /* Builtin users */
70 { 1, 2, {0,0,0,0,0,5}, {32,545,0,0,0,0,0,0,0,0,0,0,0,0,0}};
71 const struct dom_sid global_sid_Builtin_Guests =                /* Builtin guest users */
72 { 1, 2, {0,0,0,0,0,5}, {32,546,0,0,0,0,0,0,0,0,0,0,0,0,0}};
73 const struct dom_sid global_sid_Builtin_Power_Users =   /* Builtin power users */
74 { 1, 2, {0,0,0,0,0,5}, {32,547,0,0,0,0,0,0,0,0,0,0,0,0,0}};
75 const struct dom_sid global_sid_Builtin_Account_Operators =     /* Builtin account operators */
76 { 1, 2, {0,0,0,0,0,5}, {32,548,0,0,0,0,0,0,0,0,0,0,0,0,0}};
77 const struct dom_sid global_sid_Builtin_Server_Operators =      /* Builtin server operators */
78 { 1, 2, {0,0,0,0,0,5}, {32,549,0,0,0,0,0,0,0,0,0,0,0,0,0}};
79 const struct dom_sid global_sid_Builtin_Print_Operators =       /* Builtin print operators */
80 { 1, 2, {0,0,0,0,0,5}, {32,550,0,0,0,0,0,0,0,0,0,0,0,0,0}};
81 const struct dom_sid global_sid_Builtin_Backup_Operators =      /* Builtin backup operators */
82 { 1, 2, {0,0,0,0,0,5}, {32,551,0,0,0,0,0,0,0,0,0,0,0,0,0}};
83 const struct dom_sid global_sid_Builtin_Replicator =            /* Builtin replicator */
84 { 1, 2, {0,0,0,0,0,5}, {32,552,0,0,0,0,0,0,0,0,0,0,0,0,0}};
85 const struct dom_sid global_sid_Builtin_PreWin2kAccess =        /* Builtin pre win2k access */
86 { 1, 2, {0,0,0,0,0,5}, {32,554,0,0,0,0,0,0,0,0,0,0,0,0,0}};
87
88 const struct dom_sid global_sid_Unix_Users =                    /* Unmapped Unix users */
89 { 1, 1, {0,0,0,0,0,22}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
90 const struct dom_sid global_sid_Unix_Groups =                   /* Unmapped Unix groups */
91 { 1, 1, {0,0,0,0,0,22}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
92
93 /* Unused, left here for documentary purposes */
94 #if 0
95 #define SECURITY_NULL_SID_AUTHORITY    0
96 #define SECURITY_WORLD_SID_AUTHORITY   1
97 #define SECURITY_LOCAL_SID_AUTHORITY   2
98 #define SECURITY_CREATOR_SID_AUTHORITY 3
99 #define SECURITY_NT_AUTHORITY          5
100 #endif
101
102 /*
103  * An NT compatible anonymous token.
104  */
105
106 static struct dom_sid anon_sid_array[3] =
107 { { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
108   { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
109   { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
110 NT_USER_TOKEN anonymous_token = { 3, anon_sid_array, SE_NONE };
111
112 static struct dom_sid system_sid_array[1] =
113 { { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
114 NT_USER_TOKEN system_token = { 1, system_sid_array, SE_ALL_PRIVS };
115
116 /****************************************************************************
117  Lookup string names for SID types.
118 ****************************************************************************/
119
120 static const struct {
121         enum lsa_SidType sid_type;
122         const char *string;
123 } sid_name_type[] = {
124         {SID_NAME_USER, "User"},
125         {SID_NAME_DOM_GRP, "Domain Group"},
126         {SID_NAME_DOMAIN, "Domain"},
127         {SID_NAME_ALIAS, "Local Group"},
128         {SID_NAME_WKN_GRP, "Well-known Group"},
129         {SID_NAME_DELETED, "Deleted Account"},
130         {SID_NAME_INVALID, "Invalid Account"},
131         {SID_NAME_UNKNOWN, "UNKNOWN"},
132         {SID_NAME_COMPUTER, "Computer"},
133
134         {(enum lsa_SidType)0, NULL}
135 };
136
137 const char *sid_type_lookup(uint32 sid_type) 
138 {
139         int i = 0;
140
141         /* Look through list */
142         while(sid_name_type[i].sid_type != 0) {
143                 if (sid_name_type[i].sid_type == sid_type)
144                         return sid_name_type[i].string;
145                 i++;
146         }
147
148         /* Default return */
149         return "SID *TYPE* is INVALID";
150 }
151
152 /**************************************************************************
153  Create the SYSTEM token.
154 ***************************************************************************/
155
156 NT_USER_TOKEN *get_system_token(void) 
157 {
158         return &system_token;
159 }
160
161 /*****************************************************************
162  Convert a SID to an ascii string.
163 *****************************************************************/
164
165 char *sid_to_fstring(fstring sidstr_out, const struct dom_sid *sid)
166 {
167         char *str = sid_string_talloc(talloc_tos(), sid);
168         fstrcpy(sidstr_out, str);
169         TALLOC_FREE(str);
170         return sidstr_out;
171 }
172
173 /*****************************************************************
174  Essentially a renamed dom_sid_string from
175  ../libcli/security/dom_sid.c with a panic if it didn't work.
176 *****************************************************************/
177
178 char *sid_string_talloc(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
179 {
180         char *result = dom_sid_string(mem_ctx, sid);
181         SMB_ASSERT(result != NULL);
182         return result;
183 }
184
185 /*****************************************************************
186  Useful function for debug lines.
187 *****************************************************************/
188
189 char *sid_string_dbg(const struct dom_sid *sid)
190 {
191         return sid_string_talloc(talloc_tos(), sid);
192 }
193
194 /*****************************************************************
195  Use with care!
196 *****************************************************************/
197
198 char *sid_string_tos(const struct dom_sid *sid)
199 {
200         return sid_string_talloc(talloc_tos(), sid);
201 }
202
203 /*****************************************************************
204  Convert a string to a SID. Returns True on success, False on fail.
205 *****************************************************************/  
206
207 bool string_to_sid(struct dom_sid *sidout, const char *sidstr)
208 {
209         const char *p;
210         char *q;
211         /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
212         uint32 conv;
213
214         if ((sidstr[0] != 'S' && sidstr[0] != 's') || sidstr[1] != '-') {
215                 goto format_error;
216         }
217
218         ZERO_STRUCTP(sidout);
219
220         /* Get the revision number. */
221         p = sidstr + 2;
222
223         if (!isdigit(*p)) {
224                 goto format_error;
225         }
226
227         conv = (uint32) strtoul(p, &q, 10);
228         if (!q || (*q != '-')) {
229                 goto format_error;
230         }
231         sidout->sid_rev_num = (uint8) conv;
232         q++;
233
234         if (!isdigit(*q)) {
235                 goto format_error;
236         }
237
238         /* get identauth */
239         conv = (uint32) strtoul(q, &q, 10);
240         if (!q || (*q != '-')) {
241                 goto format_error;
242         }
243         /* identauth in decimal should be <  2^32 */
244         /* NOTE - the conv value is in big-endian format. */
245         sidout->id_auth[0] = 0;
246         sidout->id_auth[1] = 0;
247         sidout->id_auth[2] = (conv & 0xff000000) >> 24;
248         sidout->id_auth[3] = (conv & 0x00ff0000) >> 16;
249         sidout->id_auth[4] = (conv & 0x0000ff00) >> 8;
250         sidout->id_auth[5] = (conv & 0x000000ff);
251
252         q++;
253         sidout->num_auths = 0;
254
255         while (true) {
256                 char *end;
257
258                 if (!isdigit(*q)) {
259                         goto format_error;
260                 }
261
262                 conv = strtoul(q, &end, 10);
263                 if (end == q) {
264                         goto format_error;
265                 }
266
267                 if (!sid_append_rid(sidout, conv)) {
268                         DEBUG(3, ("Too many sid auths in %s\n", sidstr));
269                         return false;
270                 }
271
272                 q = end;
273                 if (*q == '\0') {
274                         break;
275                 }
276                 if (*q != '-') {
277                         goto format_error;
278                 }
279                 q += 1;
280         }
281         return true;
282
283 format_error:
284         DEBUG(3, ("string_to_sid: SID %s is not in a valid format\n", sidstr));
285         return false;
286 }
287
288 /*****************************************************************
289  Add a rid to the end of a sid
290 *****************************************************************/  
291
292 bool sid_append_rid(struct dom_sid *sid, uint32 rid)
293 {
294         if (sid->num_auths < MAXSUBAUTHS) {
295                 sid->sub_auths[sid->num_auths++] = rid;
296                 return True;
297         }
298         return False;
299 }
300
301 bool sid_compose(struct dom_sid *dst, const struct dom_sid *domain_sid, uint32 rid)
302 {
303         sid_copy(dst, domain_sid);
304         return sid_append_rid(dst, rid);
305 }
306
307 /*****************************************************************
308  Removes the last rid from the end of a sid
309 *****************************************************************/  
310
311 bool sid_split_rid(struct dom_sid *sid, uint32 *rid)
312 {
313         if (sid->num_auths > 0) {
314                 sid->num_auths--;
315                 *rid = sid->sub_auths[sid->num_auths];
316                 return True;
317         }
318         return False;
319 }
320
321 /*****************************************************************
322  Return the last rid from the end of a sid
323 *****************************************************************/  
324
325 bool sid_peek_rid(const struct dom_sid *sid, uint32 *rid)
326 {
327         if (!sid || !rid)
328                 return False;           
329
330         if (sid->num_auths > 0) {
331                 *rid = sid->sub_auths[sid->num_auths - 1];
332                 return True;
333         }
334         return False;
335 }
336
337 /*****************************************************************
338  Return the last rid from the end of a sid
339  and check the sid against the exp_dom_sid  
340 *****************************************************************/  
341
342 bool sid_peek_check_rid(const struct dom_sid *exp_dom_sid, const struct dom_sid *sid, uint32 *rid)
343 {
344         if (!exp_dom_sid || !sid || !rid)
345                 return False;
346
347         if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
348                 return False;
349         }
350
351         if (sid_compare_domain(exp_dom_sid, sid)!=0){
352                 *rid=(-1);
353                 return False;
354         }
355
356         return sid_peek_rid(sid, rid);
357 }
358
359 /*****************************************************************
360  Copies a sid
361 *****************************************************************/  
362
363 void sid_copy(struct dom_sid *dst, const struct dom_sid *src)
364 {
365         int i;
366
367         ZERO_STRUCTP(dst);
368
369         dst->sid_rev_num = src->sid_rev_num;
370         dst->num_auths = src->num_auths;
371
372         memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
373
374         for (i = 0; i < src->num_auths; i++)
375                 dst->sub_auths[i] = src->sub_auths[i];
376 }
377
378 /*****************************************************************
379  Write a sid out into on-the-wire format.
380 *****************************************************************/  
381
382 bool sid_linearize(char *outbuf, size_t len, const struct dom_sid *sid)
383 {
384         size_t i;
385
386         if (len < ndr_size_dom_sid(sid, 0))
387                 return False;
388
389         SCVAL(outbuf,0,sid->sid_rev_num);
390         SCVAL(outbuf,1,sid->num_auths);
391         memcpy(&outbuf[2], sid->id_auth, 6);
392         for(i = 0; i < sid->num_auths; i++)
393                 SIVAL(outbuf, 8 + (i*4), sid->sub_auths[i]);
394
395         return True;
396 }
397
398 /*****************************************************************
399  Parse a on-the-wire SID to a struct dom_sid.
400 *****************************************************************/  
401
402 bool sid_parse(const char *inbuf, size_t len, struct dom_sid *sid)
403 {
404         int i;
405         if (len < 8)
406                 return False;
407
408         ZERO_STRUCTP(sid);
409
410         sid->sid_rev_num = CVAL(inbuf, 0);
411         sid->num_auths = CVAL(inbuf, 1);
412         memcpy(sid->id_auth, inbuf+2, 6);
413         if (len < 8 + sid->num_auths*4)
414                 return False;
415         for (i=0;i<sid->num_auths;i++)
416                 sid->sub_auths[i] = IVAL(inbuf, 8+i*4);
417         return True;
418 }
419
420 /*****************************************************************
421  Compare the auth portion of two sids.
422 *****************************************************************/  
423
424 static int sid_compare_auth(const struct dom_sid *sid1, const struct dom_sid *sid2)
425 {
426         int i;
427
428         if (sid1 == sid2)
429                 return 0;
430         if (!sid1)
431                 return -1;
432         if (!sid2)
433                 return 1;
434
435         if (sid1->sid_rev_num != sid2->sid_rev_num)
436                 return sid1->sid_rev_num - sid2->sid_rev_num;
437
438         for (i = 0; i < 6; i++)
439                 if (sid1->id_auth[i] != sid2->id_auth[i])
440                         return sid1->id_auth[i] - sid2->id_auth[i];
441
442         return 0;
443 }
444
445 /*****************************************************************
446  Compare two sids.
447 *****************************************************************/  
448
449 int sid_compare(const struct dom_sid *sid1, const struct dom_sid *sid2)
450 {
451         int i;
452
453         if (sid1 == sid2)
454                 return 0;
455         if (!sid1)
456                 return -1;
457         if (!sid2)
458                 return 1;
459
460         /* Compare most likely different rids, first: i.e start at end */
461         if (sid1->num_auths != sid2->num_auths)
462                 return sid1->num_auths - sid2->num_auths;
463
464         for (i = sid1->num_auths-1; i >= 0; --i)
465                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
466                         return sid1->sub_auths[i] - sid2->sub_auths[i];
467
468         return sid_compare_auth(sid1, sid2);
469 }
470
471 /*****************************************************************
472  See if 2 SIDs are in the same domain
473  this just compares the leading sub-auths
474 *****************************************************************/  
475
476 int sid_compare_domain(const struct dom_sid *sid1, const struct dom_sid *sid2)
477 {
478         int n, i;
479
480         n = MIN(sid1->num_auths, sid2->num_auths);
481
482         for (i = n-1; i >= 0; --i)
483                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
484                         return sid1->sub_auths[i] - sid2->sub_auths[i];
485
486         return sid_compare_auth(sid1, sid2);
487 }
488
489 /*****************************************************************
490  Compare two sids.
491 *****************************************************************/  
492
493 bool sid_equal(const struct dom_sid *sid1, const struct dom_sid *sid2)
494 {
495         return sid_compare(sid1, sid2) == 0;
496 }
497
498 /*****************************************************************
499  Returns true if SID is internal (and non-mappable).
500 *****************************************************************/
501
502 bool non_mappable_sid(struct dom_sid *sid)
503 {
504         struct dom_sid dom;
505         uint32 rid;
506
507         sid_copy(&dom, sid);
508         sid_split_rid(&dom, &rid);
509
510         if (sid_equal(&dom, &global_sid_Builtin))
511                 return True;
512
513         if (sid_equal(&dom, &global_sid_NT_Authority))
514                 return True;
515
516         return False;
517 }
518
519 /*****************************************************************
520  Return the binary string representation of a struct dom_sid.
521  Caller must free.
522 *****************************************************************/
523
524 char *sid_binstring(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
525 {
526         uint8_t *buf;
527         char *s;
528         int len = ndr_size_dom_sid(sid, 0);
529         buf = talloc_array(mem_ctx, uint8_t, len);
530         if (!buf) {
531                 return NULL;
532         }
533         sid_linearize((char *)buf, len, sid);
534         s = binary_string_rfc2254(mem_ctx, buf, len);
535         TALLOC_FREE(buf);
536         return s;
537 }
538
539 /*****************************************************************
540  Return the binary string representation of a struct dom_sid.
541  Caller must free.
542 *****************************************************************/
543
544 char *sid_binstring_hex(const struct dom_sid *sid)
545 {
546         char *buf, *s;
547         int len = ndr_size_dom_sid(sid, 0);
548         buf = (char *)SMB_MALLOC(len);
549         if (!buf)
550                 return NULL;
551         sid_linearize(buf, len, sid);
552         s = binary_string(buf, len);
553         free(buf);
554         return s;
555 }
556
557 /*******************************************************************
558  Tallocs a duplicate SID. 
559 ********************************************************************/ 
560
561 struct dom_sid *sid_dup_talloc(TALLOC_CTX *ctx, const struct dom_sid *src)
562 {
563         struct dom_sid *dst;
564
565         if (src == NULL) {
566                 return NULL;
567         }
568         dst = talloc_zero(ctx, struct dom_sid);
569         if (dst == NULL) {
570                 return NULL;
571         }
572         sid_copy(dst, src);
573         return dst;
574 }
575
576 /********************************************************************
577  Add SID to an array SIDs
578 ********************************************************************/
579
580 NTSTATUS add_sid_to_array(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
581                           struct dom_sid **sids, size_t *num)
582 {
583         *sids = TALLOC_REALLOC_ARRAY(mem_ctx, *sids, struct dom_sid,
584                                              (*num)+1);
585         if (*sids == NULL) {
586                 *num = 0;
587                 return NT_STATUS_NO_MEMORY;
588         }
589
590         sid_copy(&((*sids)[*num]), sid);
591         *num += 1;
592
593         return NT_STATUS_OK;
594 }
595
596
597 /********************************************************************
598  Add SID to an array SIDs ensuring that it is not already there
599 ********************************************************************/
600
601 NTSTATUS add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
602                                  struct dom_sid **sids, size_t *num_sids)
603 {
604         size_t i;
605
606         for (i=0; i<(*num_sids); i++) {
607                 if (sid_compare(sid, &(*sids)[i]) == 0)
608                         return NT_STATUS_OK;
609         }
610
611         return add_sid_to_array(mem_ctx, sid, sids, num_sids);
612 }
613
614 /********************************************************************
615  Remove SID from an array
616 ********************************************************************/
617
618 void del_sid_from_array(const struct dom_sid *sid, struct dom_sid **sids, size_t *num)
619 {
620         struct dom_sid *sid_list = *sids;
621         size_t i;
622
623         for ( i=0; i<*num; i++ ) {
624
625                 /* if we find the SID, then decrement the count
626                    and break out of the loop */
627
628                 if ( sid_equal(sid, &sid_list[i]) ) {
629                         *num -= 1;
630                         break;
631                 }
632         }
633
634         /* This loop will copy the remainder of the array 
635            if i < num of sids ni the array */
636
637         for ( ; i<*num; i++ ) 
638                 sid_copy( &sid_list[i], &sid_list[i+1] );
639
640         return;
641 }
642
643 bool add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
644                                     uint32 rid, uint32 **pp_rids, size_t *p_num)
645 {
646         size_t i;
647
648         for (i=0; i<*p_num; i++) {
649                 if ((*pp_rids)[i] == rid)
650                         return True;
651         }
652
653         *pp_rids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_rids, uint32, *p_num+1);
654
655         if (*pp_rids == NULL) {
656                 *p_num = 0;
657                 return False;
658         }
659
660         (*pp_rids)[*p_num] = rid;
661         *p_num += 1;
662         return True;
663 }
664
665 bool is_null_sid(const struct dom_sid *sid)
666 {
667         static const struct dom_sid null_sid = {0};
668         return sid_equal(sid, &null_sid);
669 }
670
671 bool is_sid_in_token(const NT_USER_TOKEN *token, const struct dom_sid *sid)
672 {
673         int i;
674
675         for (i=0; i<token->num_sids; i++) {
676                 if (sid_compare(sid, &token->user_sids[i]) == 0)
677                         return true;
678         }
679         return false;
680 }
681
682 NTSTATUS sid_array_from_info3(TALLOC_CTX *mem_ctx,
683                               const struct netr_SamInfo3 *info3,
684                               struct dom_sid **user_sids,
685                               size_t *num_user_sids,
686                               bool include_user_group_rid,
687                               bool skip_ressource_groups)
688 {
689         NTSTATUS status;
690         struct dom_sid sid;
691         struct dom_sid *sid_array = NULL;
692         size_t num_sids = 0;
693         int i;
694
695         if (include_user_group_rid) {
696                 if (!sid_compose(&sid, info3->base.domain_sid, info3->base.rid)) {
697                         DEBUG(3, ("could not compose user SID from rid 0x%x\n",
698                                   info3->base.rid));
699                         return NT_STATUS_INVALID_PARAMETER;
700                 }
701                 status = add_sid_to_array(mem_ctx, &sid, &sid_array, &num_sids);
702                 if (!NT_STATUS_IS_OK(status)) {
703                         DEBUG(3, ("could not append user SID from rid 0x%x\n",
704                                   info3->base.rid));
705                         return status;
706                 }
707         }
708
709         if (!sid_compose(&sid, info3->base.domain_sid, info3->base.primary_gid)) {
710                 DEBUG(3, ("could not compose group SID from rid 0x%x\n",
711                           info3->base.primary_gid));
712                 return NT_STATUS_INVALID_PARAMETER;
713         }
714         status = add_sid_to_array(mem_ctx, &sid, &sid_array, &num_sids);
715         if (!NT_STATUS_IS_OK(status)) {
716                 DEBUG(3, ("could not append group SID from rid 0x%x\n",
717                           info3->base.rid));
718                 return status;
719         }
720
721         for (i = 0; i < info3->base.groups.count; i++) {
722                 /* Don't add the primary group sid twice. */
723                 if (info3->base.primary_gid == info3->base.groups.rids[i].rid) {
724                         continue;
725                 }
726                 if (!sid_compose(&sid, info3->base.domain_sid,
727                                  info3->base.groups.rids[i].rid)) {
728                         DEBUG(3, ("could not compose SID from additional group "
729                                   "rid 0x%x\n", info3->base.groups.rids[i].rid));
730                         return NT_STATUS_INVALID_PARAMETER;
731                 }
732                 status = add_sid_to_array(mem_ctx, &sid, &sid_array, &num_sids);
733                 if (!NT_STATUS_IS_OK(status)) {
734                         DEBUG(3, ("could not append SID from additional group "
735                                   "rid 0x%x\n", info3->base.groups.rids[i].rid));
736                         return status;
737                 }
738         }
739
740         /* Copy 'other' sids.  We need to do sid filtering here to
741            prevent possible elevation of privileges.  See:
742
743            http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
744          */
745
746         for (i = 0; i < info3->sidcount; i++) {
747
748                 if (skip_ressource_groups &&
749                     (info3->sids[i].attributes & SE_GROUP_RESOURCE)) {
750                         continue;
751                 }
752
753                 status = add_sid_to_array(mem_ctx, info3->sids[i].sid,
754                                       &sid_array, &num_sids);
755                 if (!NT_STATUS_IS_OK(status)) {
756                         DEBUG(3, ("could not add SID to array: %s\n",
757                                   sid_string_dbg(info3->sids[i].sid)));
758                         return status;
759                 }
760         }
761
762         *user_sids = sid_array;
763         *num_user_sids = num_sids;
764
765         return NT_STATUS_OK;
766 }