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