r3494: got rid of include/rewrite.h, and split out the dynconfig.h header
[sfrench/samba-autobuild/.git] / source4 / 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  Compare the auth portion of two sids.
29 *****************************************************************/  
30
31 static int sid_compare_auth(const struct dom_sid *sid1, const struct dom_sid *sid2)
32 {
33         int i;
34
35         if (sid1 == sid2)
36                 return 0;
37         if (!sid1)
38                 return -1;
39         if (!sid2)
40                 return 1;
41
42         if (sid1->sid_rev_num != sid2->sid_rev_num)
43                 return sid1->sid_rev_num - sid2->sid_rev_num;
44
45         for (i = 0; i < 6; i++)
46                 if (sid1->id_auth[i] != sid2->id_auth[i])
47                         return sid1->id_auth[i] - sid2->id_auth[i];
48
49         return 0;
50 }
51
52 /*****************************************************************
53  Compare two sids.
54 *****************************************************************/  
55
56 static int sid_compare(const struct dom_sid *sid1, const struct dom_sid *sid2)
57 {
58         int i;
59
60         if (sid1 == sid2)
61                 return 0;
62         if (!sid1)
63                 return -1;
64         if (!sid2)
65                 return 1;
66
67         /* Compare most likely different rids, first: i.e start at end */
68         if (sid1->num_auths != sid2->num_auths)
69                 return sid1->num_auths - sid2->num_auths;
70
71         for (i = sid1->num_auths-1; i >= 0; --i)
72                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
73                         return sid1->sub_auths[i] - sid2->sub_auths[i];
74
75         return sid_compare_auth(sid1, sid2);
76 }
77
78 /*****************************************************************
79  Compare two sids.
80 *****************************************************************/  
81
82 BOOL sid_equal(const struct dom_sid *sid1, const struct dom_sid *sid2)
83 {
84         return sid_compare(sid1, sid2) == 0;
85 }