Release 971012
[wine] / win32 / security.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "windows.h"
6 #include "ntdll.h"
7 #include "xmalloc.h"
8 #include "stddebug.h"
9 #include "debug.h"
10
11 BOOL32 WINAPI IsValidSid (LPSID pSid);
12 BOOL32 WINAPI EqualSid (LPSID pSid1, LPSID pSid2);
13 BOOL32 WINAPI EqualPrefixSid (LPSID pSid1, LPSID pSid2);
14 DWORD  WINAPI GetSidLengthRequired (BYTE nSubAuthorityCount);
15 BOOL32 WINAPI AllocateAndInitializeSid(LPSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, BYTE nSubAuthorityCount, DWORD nSubAuthority0, DWORD nSubAuthority1, DWORD nSubAuthority2, DWORD nSubAuthority3,    DWORD nSubAuthority4, DWORD nSubAuthority5, DWORD nSubAuthority6, DWORD nSubAuthority7, LPSID *pSid);
16 VOID*  WINAPI FreeSid(LPSID pSid);
17 BOOL32 WINAPI InitializeSid (LPSID pSid, LPSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, BYTE nSubAuthorityCount);
18 LPSID_IDENTIFIER_AUTHORITY WINAPI GetSidIdentifierAuthority(LPSID pSid);
19 DWORD* WINAPI GetSidSubAuthority(LPSID pSid, DWORD nSubAuthority);
20 BYTE*  WINAPI GetSidSubAuthorityCount(LPSID pSid);
21 DWORD  WINAPI GetLengthSid(LPSID pSid);
22 BOOL32 WINAPI CopySid(DWORD nDestinationSidLength, LPSID pDestinationSid, LPSID pSourceSid);
23
24 /***********************************************************************
25  *           IsValidSid  (ADVAPI.80)
26  */
27 BOOL32 WINAPI IsValidSid (LPSID pSid) {
28     if (!pSid || pSid->Revision != SID_REVISION)
29         return FALSE;
30
31     return TRUE;
32 }
33
34 /***********************************************************************
35  *           EqualSid  (ADVAPI.40)
36  */
37 BOOL32 WINAPI EqualSid (LPSID pSid1, LPSID pSid2) {
38     if (!IsValidSid(pSid1) || !IsValidSid(pSid2))
39         return FALSE;
40
41     if (*GetSidSubAuthorityCount(pSid1) != *GetSidSubAuthorityCount(pSid2))
42         return FALSE;
43
44     if (memcmp(pSid1, pSid2, GetLengthSid(pSid1)) != 0)
45         return FALSE;
46
47     return TRUE;
48 }
49
50 /***********************************************************************
51  *           EqualPrefixSid  (ADVAPI.39)
52  */
53 BOOL32 WINAPI EqualPrefixSid (LPSID pSid1, LPSID pSid2) {
54     if (!IsValidSid(pSid1) || !IsValidSid(pSid2))
55         return FALSE;
56
57     if (*GetSidSubAuthorityCount(pSid1) != *GetSidSubAuthorityCount(pSid2))
58         return FALSE;
59
60     if (memcmp(pSid1, pSid2, GetSidLengthRequired(pSid1->SubAuthorityCount - 1))
61  != 0)
62         return FALSE;
63
64     return TRUE;
65 }
66
67 /***********************************************************************
68  *           GetSidLengthRequired  (ADVAPI.63)
69  */
70 DWORD WINAPI GetSidLengthRequired (BYTE nSubAuthorityCount) {
71     return sizeof (SID) + (nSubAuthorityCount - 1 * sizeof (DWORD));
72 }
73
74 /***********************************************************************
75  *           AllocateAndInitializeSid  (ADVAPI.11)
76  */
77 BOOL32 WINAPI AllocateAndInitializeSid(LPSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
78     BYTE nSubAuthorityCount,
79     DWORD nSubAuthority0, DWORD nSubAuthority1,
80     DWORD nSubAuthority2, DWORD nSubAuthority3,
81     DWORD nSubAuthority4, DWORD nSubAuthority5,
82     DWORD nSubAuthority6, DWORD nSubAuthority7,
83     LPSID *pSid) {
84
85     *pSid = xmalloc(GetSidLengthRequired(nSubAuthorityCount));
86     (*pSid)->Revision = SID_REVISION;
87     if (pIdentifierAuthority)
88         memcpy(&(*pSid)->IdentifierAuthority, pIdentifierAuthority,
89                sizeof (SID_IDENTIFIER_AUTHORITY));
90     *GetSidSubAuthorityCount(*pSid) = nSubAuthorityCount;
91
92     if (nSubAuthorityCount > 0)
93         *GetSidSubAuthority(*pSid, 0) = nSubAuthority0;
94     if (nSubAuthorityCount > 1)
95         *GetSidSubAuthority(*pSid, 1) = nSubAuthority1;
96     if (nSubAuthorityCount > 2)
97         *GetSidSubAuthority(*pSid, 2) = nSubAuthority2;
98     if (nSubAuthorityCount > 3)
99         *GetSidSubAuthority(*pSid, 3) = nSubAuthority3;
100     if (nSubAuthorityCount > 4)
101         *GetSidSubAuthority(*pSid, 4) = nSubAuthority4;
102     if (nSubAuthorityCount > 5)
103         *GetSidSubAuthority(*pSid, 5) = nSubAuthority5;
104     if (nSubAuthorityCount > 6)
105         *GetSidSubAuthority(*pSid, 6) = nSubAuthority6;
106     if (nSubAuthorityCount > 7)
107         *GetSidSubAuthority(*pSid, 7) = nSubAuthority7;
108
109     return TRUE;
110 }
111
112 /***********************************************************************
113  *           FreeSid  (ADVAPI.42)
114  */
115 VOID* WINAPI FreeSid(LPSID pSid) {
116     free(pSid);
117     return NULL;
118 }
119
120 /***********************************************************************
121  *           InitializeSecurityDescriptor  (ADVAPI.73)
122  */
123 BOOL32 WINAPI InitializeSecurityDescriptor( SECURITY_DESCRIPTOR *pDescr,
124                                             DWORD revision )
125 {
126     fprintf( stdnimp, "InitializeSecurityDescriptor: empty stub\n" );
127     return TRUE;
128 }
129
130
131 /***********************************************************************
132  *           InitializeSid  (ADVAPI.74)
133  */
134 BOOL32 WINAPI InitializeSid (LPSID pSid, LPSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
135                     BYTE nSubAuthorityCount)
136 {
137     int i;
138
139     pSid->Revision = SID_REVISION;
140     if (pIdentifierAuthority)
141         memcpy(&pSid->IdentifierAuthority, pIdentifierAuthority,
142                sizeof (SID_IDENTIFIER_AUTHORITY));
143     *GetSidSubAuthorityCount(pSid) = nSubAuthorityCount;
144
145     for (i = 0; i < nSubAuthorityCount; i++)
146         *GetSidSubAuthority(pSid, i) = 0;
147
148     return TRUE;
149 }
150
151 /***********************************************************************
152  *           GetSidIdentifierAuthority  (ADVAPI.62)
153  */
154 LPSID_IDENTIFIER_AUTHORITY WINAPI GetSidIdentifierAuthority (LPSID pSid)
155 {
156     return &pSid->IdentifierAuthority;
157 }
158
159 /***********************************************************************
160  *           GetSidSubAuthority  (ADVAPI.64)
161  */
162 DWORD * WINAPI GetSidSubAuthority (LPSID pSid, DWORD nSubAuthority)
163 {
164     return &pSid->SubAuthority[nSubAuthority];
165 }
166
167 /***********************************************************************
168  *           GetSidSubAuthorityCount  (ADVAPI.65)
169  */
170 BYTE * WINAPI GetSidSubAuthorityCount (LPSID pSid)
171 {
172     return &pSid->SubAuthorityCount;
173 }
174
175 /***********************************************************************
176  *           GetLengthSid  (ADVAPI.48)
177  */
178 DWORD WINAPI GetLengthSid (LPSID pSid)
179 {
180     return GetSidLengthRequired(*GetSidSubAuthorityCount(pSid));
181 }
182
183 /***********************************************************************
184  *           CopySid  (ADVAPI.24)
185  */
186 BOOL32 WINAPI CopySid (DWORD nDestinationSidLength, LPSID pDestinationSid,
187                        LPSID pSourceSid)
188 {
189
190     if (!IsValidSid(pSourceSid))
191         return FALSE;
192
193     if (nDestinationSidLength < GetLengthSid(pSourceSid))
194         return FALSE;
195
196     memcpy(pDestinationSid, pSourceSid, GetLengthSid(pSourceSid));
197
198     return TRUE;
199 }