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