r7415: * big change -- volker's new async winbindd from trunk
[samba.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       
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 /*
28  * Some useful sids
29  */
30
31
32 const DOM_SID global_sid_World_Domain =               /* Everyone domain */
33 { 1, 0, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
34 const DOM_SID global_sid_World =                      /* Everyone */
35 { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
36 const DOM_SID global_sid_Creator_Owner_Domain =       /* Creator Owner domain */
37 { 1, 0, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
38 const DOM_SID global_sid_NT_Authority =                 /* NT Authority */
39 { 1, 0, {0,0,0,0,0,5}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
40 const DOM_SID global_sid_System =                       /* System */
41 { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
42 const DOM_SID global_sid_NULL =                         /* NULL sid */
43 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
44 const DOM_SID global_sid_Authenticated_Users =  /* All authenticated rids */
45 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
46 const DOM_SID global_sid_Network =                      /* Network rids */
47 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
48
49 const DOM_SID global_sid_Creator_Owner =                /* Creator Owner */
50 { 1, 1, {0,0,0,0,0,3}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
51 const DOM_SID global_sid_Creator_Group =                /* Creator Group */
52 { 1, 1, {0,0,0,0,0,3}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
53 const DOM_SID global_sid_Anonymous =                    /* Anonymous login */
54 { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
55
56 const DOM_SID global_sid_Builtin =                      /* Local well-known domain */
57 { 1, 1, {0,0,0,0,0,5}, {32,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
58 const DOM_SID global_sid_Builtin_Administrators =       /* Builtin administrators */
59 { 1, 2, {0,0,0,0,0,5}, {32,544,0,0,0,0,0,0,0,0,0,0,0,0,0}};
60 const DOM_SID global_sid_Builtin_Users =                /* Builtin users */
61 { 1, 2, {0,0,0,0,0,5}, {32,545,0,0,0,0,0,0,0,0,0,0,0,0,0}};
62 const DOM_SID global_sid_Builtin_Guests =               /* Builtin guest users */
63 { 1, 2, {0,0,0,0,0,5}, {32,546,0,0,0,0,0,0,0,0,0,0,0,0,0}};
64 const DOM_SID global_sid_Builtin_Power_Users =  /* Builtin power users */
65 { 1, 2, {0,0,0,0,0,5}, {32,547,0,0,0,0,0,0,0,0,0,0,0,0,0}};
66 const DOM_SID global_sid_Builtin_Account_Operators =    /* Builtin account operators */
67 { 1, 2, {0,0,0,0,0,5}, {32,548,0,0,0,0,0,0,0,0,0,0,0,0,0}};
68 const DOM_SID global_sid_Builtin_Server_Operators =     /* Builtin server operators */
69 { 1, 2, {0,0,0,0,0,5}, {32,549,0,0,0,0,0,0,0,0,0,0,0,0,0}};
70 const DOM_SID global_sid_Builtin_Print_Operators =      /* Builtin print operators */
71 { 1, 2, {0,0,0,0,0,5}, {32,550,0,0,0,0,0,0,0,0,0,0,0,0,0}};
72 const DOM_SID global_sid_Builtin_Backup_Operators =     /* Builtin backup operators */
73 { 1, 2, {0,0,0,0,0,5}, {32,551,0,0,0,0,0,0,0,0,0,0,0,0,0}};
74 const DOM_SID global_sid_Builtin_Replicator =           /* Builtin replicator */
75 { 1, 2, {0,0,0,0,0,5}, {32,552,0,0,0,0,0,0,0,0,0,0,0,0,0}};
76
77 #define SECURITY_NULL_SID_AUTHORITY    0
78 #define SECURITY_WORLD_SID_AUTHORITY   1
79 #define SECURITY_LOCAL_SID_AUTHORITY   2
80 #define SECURITY_CREATOR_SID_AUTHORITY 3
81 #define SECURITY_NT_AUTHORITY          5
82
83 /*
84  * An NT compatible anonymous token.
85  */
86
87 static DOM_SID anon_sid_array[3] =
88 { { 1, 1, {0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
89   { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}},
90   { 1, 1, {0,0,0,0,0,5}, {7,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
91 NT_USER_TOKEN anonymous_token = { 3, anon_sid_array, SE_NONE };
92
93 static DOM_SID system_sid_array[1] =
94 { { 1, 1, {0,0,0,0,0,5}, {18,0,0,0,0,0,0,0,0,0,0,0,0,0,0}} };
95 NT_USER_TOKEN system_token = { 1, system_sid_array, SE_ALL_PRIVS };
96
97 /****************************************************************************
98  Lookup string names for SID types.
99 ****************************************************************************/
100
101 static const struct {
102         enum SID_NAME_USE sid_type;
103         const char *string;
104 } sid_name_type[] = {
105         {SID_NAME_USER, "User"},
106         {SID_NAME_DOM_GRP, "Domain Group"},
107         {SID_NAME_DOMAIN, "Domain"},
108         {SID_NAME_ALIAS, "Local Group"},
109         {SID_NAME_WKN_GRP, "Well-known Group"},
110         {SID_NAME_DELETED, "Deleted Account"},
111         {SID_NAME_INVALID, "Invalid Account"},
112         {SID_NAME_UNKNOWN, "UNKNOWN"},
113         {SID_NAME_COMPUTER, "Computer"},
114
115         {(enum SID_NAME_USE)0, NULL}
116 };
117
118 const char *sid_type_lookup(uint32 sid_type) 
119 {
120         int i = 0;
121
122         /* Look through list */
123         while(sid_name_type[i].sid_type != 0) {
124                 if (sid_name_type[i].sid_type == sid_type)
125                         return sid_name_type[i].string;
126                 i++;
127         }
128
129         /* Default return */
130         return "SID *TYPE* is INVALID";
131 }
132
133 /**************************************************************************
134  Create the SYSTEM token.
135 ***************************************************************************/
136
137 NT_USER_TOKEN *get_system_token(void) 
138 {
139         return &system_token;
140 }
141
142 /******************************************************************
143  get the default domain/netbios name to be used when dealing 
144  with our passdb list of accounts
145 ******************************************************************/
146
147 const char *get_global_sam_name(void) 
148 {
149         if ((lp_server_role() == ROLE_DOMAIN_PDC) || (lp_server_role() == ROLE_DOMAIN_BDC)) {
150                 return lp_workgroup();
151         }
152         return global_myname();
153 }
154
155 /**************************************************************************
156  Splits a name of format \DOMAIN\name or name into its two components.
157  Sets the DOMAIN name to global_myname() if it has not been specified.
158 ***************************************************************************/
159
160 void split_domain_name(const char *fullname, char *domain, char *name)
161 {
162         pstring full_name;
163         const char *sep;
164         char *p;
165
166         sep = lp_winbind_separator();
167
168         *domain = *name = '\0';
169
170         if (fullname[0] == sep[0] || fullname[0] == '\\')
171                 fullname++;
172
173         pstrcpy(full_name, fullname);
174         p = strchr_m(full_name+1, '\\');
175         if (!p) p = strchr_m(full_name+1, sep[0]);
176
177         if (p != NULL) {
178                 *p = 0;
179                 fstrcpy(domain, full_name);
180                 fstrcpy(name, p+1);
181         } else {
182                 fstrcpy(domain, get_global_sam_name());
183                 fstrcpy(name, full_name);
184         }
185
186         DEBUG(10,("split_domain_name:name '%s' split into domain :'%s' and user :'%s'\n",
187                         fullname, domain, name));
188 }
189
190 /****************************************************************************
191  Test if a SID is wellknown and resolvable.
192 ****************************************************************************/
193
194 BOOL resolvable_wellknown_sid(DOM_SID *sid)
195 {
196         uint32 ia = (sid->id_auth[5]) +
197                         (sid->id_auth[4] << 8 ) +
198                         (sid->id_auth[3] << 16) +
199                         (sid->id_auth[2] << 24);
200
201         if (sid->sid_rev_num != SEC_DESC_REVISION || sid->num_auths < 1)
202                 return False;
203
204         return (ia == SECURITY_WORLD_SID_AUTHORITY ||
205                 ia == SECURITY_CREATOR_SID_AUTHORITY);
206 }
207
208 /*****************************************************************
209  Convert a SID to an ascii string.
210 *****************************************************************/
211
212 char *sid_to_string(fstring sidstr_out, const DOM_SID *sid)
213 {
214         char subauth[16];
215         int i;
216         uint32 ia;
217   
218         if (!sid) {
219                 fstrcpy(sidstr_out, "(NULL SID)");
220                 return sidstr_out;
221         }
222
223         /*
224          * BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 
225          * in a range of 2^48.
226          */
227         ia = (sid->id_auth[5]) +
228                 (sid->id_auth[4] << 8 ) +
229                 (sid->id_auth[3] << 16) +
230                 (sid->id_auth[2] << 24);
231
232         slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
233
234         for (i = 0; i < sid->num_auths; i++) {
235                 slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
236                 fstrcat(sidstr_out, subauth);
237         }
238
239         return sidstr_out;
240 }
241
242 /*****************************************************************
243  Useful function for debug lines.
244 *****************************************************************/  
245
246 const char *sid_string_static(const DOM_SID *sid)
247 {
248         static fstring sid_str;
249         sid_to_string(sid_str, sid);
250         return sid_str;
251 }
252
253 /*****************************************************************
254  Convert a string to a SID. Returns True on success, False on fail.
255 *****************************************************************/  
256    
257 BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
258 {
259         pstring tok;
260         char *q;
261         const char *p;
262         /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
263         uint32 ia;
264   
265         if (StrnCaseCmp( sidstr, "S-", 2)) {
266                 DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
267                 return False;
268         }
269
270         memset((char *)sidout, '\0', sizeof(DOM_SID));
271
272         p = q = SMB_STRDUP(sidstr + 2);
273         if (p == NULL) {
274                 DEBUG(0, ("string_to_sid: out of memory!\n"));
275                 return False;
276         }
277
278         if (!next_token(&p, tok, "-", sizeof(tok))) {
279                 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
280                 SAFE_FREE(q);
281                 return False;
282         }
283
284         /* Get the revision number. */
285         sidout->sid_rev_num = (uint8)strtoul(tok, NULL, 10);
286
287         if (!next_token(&p, tok, "-", sizeof(tok))) {
288                 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
289                 SAFE_FREE(q);
290                 return False;
291         }
292
293         /* identauth in decimal should be <  2^32 */
294         ia = (uint32)strtoul(tok, NULL, 10);
295
296         /* NOTE - the ia value is in big-endian format. */
297         sidout->id_auth[0] = 0;
298         sidout->id_auth[1] = 0;
299         sidout->id_auth[2] = (ia & 0xff000000) >> 24;
300         sidout->id_auth[3] = (ia & 0x00ff0000) >> 16;
301         sidout->id_auth[4] = (ia & 0x0000ff00) >> 8;
302         sidout->id_auth[5] = (ia & 0x000000ff);
303
304         sidout->num_auths = 0;
305
306         while(next_token(&p, tok, "-", sizeof(tok)) && 
307                 sidout->num_auths < MAXSUBAUTHS) {
308                 /* 
309                  * NOTE - the subauths are in native machine-endian format. They
310                  * are converted to little-endian when linearized onto the wire.
311                  */
312                 sid_append_rid(sidout, (uint32)strtoul(tok, NULL, 10));
313         }
314
315         SAFE_FREE(q);
316         return True;
317 }
318
319 DOM_SID *string_sid_talloc(TALLOC_CTX *mem_ctx, const char *sidstr)
320 {
321         DOM_SID *result = TALLOC_P(mem_ctx, DOM_SID);
322
323         if (result == NULL)
324                 return NULL;
325
326         if (!string_to_sid(result, sidstr))
327                 return NULL;
328
329         return result;
330 }
331
332 /*****************************************************************
333  Add a rid to the end of a sid
334 *****************************************************************/  
335
336 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
337 {
338         if (sid->num_auths < MAXSUBAUTHS) {
339                 sid->sub_auths[sid->num_auths++] = rid;
340                 return True;
341         }
342         return False;
343 }
344
345 BOOL sid_compose(DOM_SID *dst, const DOM_SID *domain_sid, uint32 rid)
346 {
347         sid_copy(dst, domain_sid);
348         return sid_append_rid(dst, rid);
349 }
350
351 /*****************************************************************
352  Removes the last rid from the end of a sid
353 *****************************************************************/  
354
355 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
356 {
357         if (sid->num_auths > 0) {
358                 sid->num_auths--;
359                 *rid = sid->sub_auths[sid->num_auths];
360                 return True;
361         }
362         return False;
363 }
364
365 /*****************************************************************
366  Return the last rid from the end of a sid
367 *****************************************************************/  
368
369 BOOL sid_peek_rid(const DOM_SID *sid, uint32 *rid)
370 {
371         if (!sid || !rid)
372                 return False;           
373         
374         if (sid->num_auths > 0) {
375                 *rid = sid->sub_auths[sid->num_auths - 1];
376                 return True;
377         }
378         return False;
379 }
380
381 /*****************************************************************
382  Return the last rid from the end of a sid
383  and check the sid against the exp_dom_sid  
384 *****************************************************************/  
385
386 BOOL sid_peek_check_rid(const DOM_SID *exp_dom_sid, const DOM_SID *sid, uint32 *rid)
387 {
388         if (!exp_dom_sid || !sid || !rid)
389                 return False;
390                         
391         if (sid->num_auths != (exp_dom_sid->num_auths+1)) {
392                 return False;
393         }
394
395         if (sid_compare_domain(exp_dom_sid, sid)!=0){
396                 *rid=(-1);
397                 return False;
398         }
399         
400         return sid_peek_rid(sid, rid);
401 }
402
403 /*****************************************************************
404  Copies a sid
405 *****************************************************************/  
406
407 void sid_copy(DOM_SID *dst, const DOM_SID *src)
408 {
409         int i;
410
411         ZERO_STRUCTP(dst);
412
413         dst->sid_rev_num = src->sid_rev_num;
414         dst->num_auths = src->num_auths;
415
416         memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
417
418         for (i = 0; i < src->num_auths; i++)
419                 dst->sub_auths[i] = src->sub_auths[i];
420 }
421
422 /*****************************************************************
423  Write a sid out into on-the-wire format.
424 *****************************************************************/  
425
426 BOOL sid_linearize(char *outbuf, size_t len, const DOM_SID *sid)
427 {
428         size_t i;
429
430         if (len < sid_size(sid))
431                 return False;
432
433         SCVAL(outbuf,0,sid->sid_rev_num);
434         SCVAL(outbuf,1,sid->num_auths);
435         memcpy(&outbuf[2], sid->id_auth, 6);
436         for(i = 0; i < sid->num_auths; i++)
437                 SIVAL(outbuf, 8 + (i*4), sid->sub_auths[i]);
438
439         return True;
440 }
441
442 /*****************************************************************
443  Parse a on-the-wire SID to a DOM_SID.
444 *****************************************************************/  
445
446 BOOL sid_parse(const char *inbuf, size_t len, DOM_SID *sid)
447 {
448         int i;
449         if (len < 8)
450                 return False;
451
452         ZERO_STRUCTP(sid);
453
454         sid->sid_rev_num = CVAL(inbuf, 0);
455         sid->num_auths = CVAL(inbuf, 1);
456         memcpy(sid->id_auth, inbuf+2, 6);
457         if (len < 8 + sid->num_auths*4)
458                 return False;
459         for (i=0;i<sid->num_auths;i++)
460                 sid->sub_auths[i] = IVAL(inbuf, 8+i*4);
461         return True;
462 }
463
464 /*****************************************************************
465  Compare the auth portion of two sids.
466 *****************************************************************/  
467
468 static int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2)
469 {
470         int i;
471
472         if (sid1 == sid2)
473                 return 0;
474         if (!sid1)
475                 return -1;
476         if (!sid2)
477                 return 1;
478
479         if (sid1->sid_rev_num != sid2->sid_rev_num)
480                 return sid1->sid_rev_num - sid2->sid_rev_num;
481
482         for (i = 0; i < 6; i++)
483                 if (sid1->id_auth[i] != sid2->id_auth[i])
484                         return sid1->id_auth[i] - sid2->id_auth[i];
485
486         return 0;
487 }
488
489 /*****************************************************************
490  Compare two sids.
491 *****************************************************************/  
492
493 int sid_compare(const DOM_SID *sid1, const DOM_SID *sid2)
494 {
495         int i;
496
497         if (sid1 == sid2)
498                 return 0;
499         if (!sid1)
500                 return -1;
501         if (!sid2)
502                 return 1;
503
504         /* Compare most likely different rids, first: i.e start at end */
505         if (sid1->num_auths != sid2->num_auths)
506                 return sid1->num_auths - sid2->num_auths;
507
508         for (i = sid1->num_auths-1; i >= 0; --i)
509                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
510                         return sid1->sub_auths[i] - sid2->sub_auths[i];
511
512         return sid_compare_auth(sid1, sid2);
513 }
514
515 /*****************************************************************
516  See if 2 SIDs are in the same domain
517  this just compares the leading sub-auths
518 *****************************************************************/  
519
520 int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
521 {
522         int n, i;
523
524         n = MIN(sid1->num_auths, sid2->num_auths);
525
526         for (i = n-1; i >= 0; --i)
527                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
528                         return sid1->sub_auths[i] - sid2->sub_auths[i];
529
530         return sid_compare_auth(sid1, sid2);
531 }
532
533 /*****************************************************************
534  Compare two sids.
535 *****************************************************************/  
536
537 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
538 {
539         return sid_compare(sid1, sid2) == 0;
540 }
541
542 /*****************************************************************
543  Check if the SID is the builtin SID (S-1-5-32).
544 *****************************************************************/  
545
546 BOOL sid_check_is_builtin(const DOM_SID *sid)
547 {
548         return sid_equal(sid, &global_sid_Builtin);
549 }
550
551 /*****************************************************************
552  Check if the SID is one of the builtin SIDs (S-1-5-32-a).
553 *****************************************************************/  
554
555 BOOL sid_check_is_in_builtin(const DOM_SID *sid)
556 {
557         DOM_SID dom_sid;
558         uint32 rid;
559
560         sid_copy(&dom_sid, sid);
561         sid_split_rid(&dom_sid, &rid);
562         
563         return sid_equal(&dom_sid, &global_sid_Builtin);
564 }
565
566 /*****************************************************************
567  Calculates size of a sid.
568 *****************************************************************/  
569
570 size_t sid_size(const DOM_SID *sid)
571 {
572         if (sid == NULL)
573                 return 0;
574
575         return sid->num_auths * sizeof(uint32) + 8;
576 }
577
578 /*****************************************************************
579  Returns true if SID is internal (and non-mappable).
580 *****************************************************************/
581
582 BOOL non_mappable_sid(DOM_SID *sid)
583 {
584         DOM_SID dom;
585         uint32 rid;
586
587         sid_copy(&dom, sid);
588         sid_split_rid(&dom, &rid);
589
590         if (sid_equal(&dom, &global_sid_Builtin))
591                 return True;
592
593         if (sid_equal(&dom, &global_sid_NT_Authority))
594                 return True;
595
596         return False;
597 }
598
599 /*****************************************************************
600  Return the binary string representation of a DOM_SID.
601  Caller must free.
602 *****************************************************************/
603
604 char *sid_binstring(const DOM_SID *sid)
605 {
606         char *buf, *s;
607         int len = sid_size(sid);
608         buf = SMB_MALLOC(len);
609         if (!buf)
610                 return NULL;
611         sid_linearize(buf, len, sid);
612         s = binary_string(buf, len);
613         free(buf);
614         return s;
615 }
616
617 /*******************************************************************
618  Tallocs a duplicate SID. 
619 ********************************************************************/ 
620
621 DOM_SID *sid_dup_talloc(TALLOC_CTX *ctx, const DOM_SID *src)
622 {
623         DOM_SID *dst;
624         
625         if(!src)
626                 return NULL;
627         
628         if((dst = TALLOC_ZERO_P(ctx, DOM_SID)) != NULL) {
629                 sid_copy( dst, src);
630         }
631         
632         return dst;
633 }
634
635 /********************************************************************
636  Add SID to an array SIDs
637 ********************************************************************/
638
639 void add_sid_to_array(TALLOC_CTX *mem_ctx, const DOM_SID *sid, 
640                       DOM_SID **sids, int *num)
641 {
642         if (mem_ctx != NULL)
643                 *sids = TALLOC_REALLOC_ARRAY(mem_ctx, *sids, DOM_SID,
644                                              (*num)+1);
645         else
646                 *sids = SMB_REALLOC_ARRAY(*sids, DOM_SID, (*num)+1);
647
648         if (*sids == NULL)
649                 return;
650
651         sid_copy(&((*sids)[*num]), sid);
652         *num += 1;
653
654         return;
655 }
656
657
658 /********************************************************************
659  Add SID to an array SIDs ensuring that it is not already there
660 ********************************************************************/
661
662 void add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
663                              DOM_SID **sids, int *num_sids)
664 {
665         int i;
666
667         for (i=0; i<(*num_sids); i++) {
668                 if (sid_compare(sid, &(*sids)[i]) == 0)
669                         return;
670         }
671
672         add_sid_to_array(mem_ctx, sid, sids, num_sids);
673 }
674
675 /********************************************************************
676  Remove SID from an array
677 ********************************************************************/
678
679 void del_sid_from_array(const DOM_SID *sid, DOM_SID **sids, int *num)
680 {
681         DOM_SID *sid_list = *sids;
682         int i;
683
684         for ( i=0; i<*num; i++ ) {
685
686                 /* if we find the SID, then decrement the count
687                    and break out of the loop */
688
689                 if ( sid_equal(sid, &sid_list[i]) ) {
690                         *num -= 1;
691                         break;
692                 }
693         }
694
695         /* This loop will copy the remainder of the array 
696            if i < num of sids ni the array */
697
698         for ( ; i<*num; i++ ) 
699                 sid_copy( &sid_list[i], &sid_list[i+1] );
700         
701         return;
702 }
703