Implement ConvertSidToStringSidA/w
[reactos.git] / reactos / lib / advapi32 / sec / sid.c
1 /* $Id: sid.c,v 1.15 2004/08/23 21:16:26 gvg Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/advapi32/sec/sid.c
6 * PURPOSE: Security ID functions
7 */
8
9 #include "advapi32.h"
10 #include <debug.h>
11
12
13 /*
14 * @implemented
15 */
16 BOOL STDCALL
17 AllocateLocallyUniqueId(PLUID Luid)
18 {
19 NTSTATUS Status;
20
21 Status = NtAllocateLocallyUniqueId (Luid);
22 if (!NT_SUCCESS (Status))
23 {
24 SetLastError (RtlNtStatusToDosError (Status));
25 return FALSE;
26 }
27
28 return TRUE;
29 }
30
31
32 /*
33 * @implemented
34 */
35 BOOL STDCALL
36 AllocateAndInitializeSid (PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
37 BYTE nSubAuthorityCount,
38 DWORD dwSubAuthority0,
39 DWORD dwSubAuthority1,
40 DWORD dwSubAuthority2,
41 DWORD dwSubAuthority3,
42 DWORD dwSubAuthority4,
43 DWORD dwSubAuthority5,
44 DWORD dwSubAuthority6,
45 DWORD dwSubAuthority7,
46 PSID *pSid)
47 {
48 NTSTATUS Status;
49
50 Status = RtlAllocateAndInitializeSid (pIdentifierAuthority,
51 nSubAuthorityCount,
52 dwSubAuthority0,
53 dwSubAuthority1,
54 dwSubAuthority2,
55 dwSubAuthority3,
56 dwSubAuthority4,
57 dwSubAuthority5,
58 dwSubAuthority6,
59 dwSubAuthority7,
60 pSid);
61 if (!NT_SUCCESS (Status))
62 {
63 SetLastError (RtlNtStatusToDosError (Status));
64 return FALSE;
65 }
66
67 return TRUE;
68 }
69
70
71 /*
72 * @implemented
73 */
74 BOOL STDCALL
75 CopySid (DWORD nDestinationSidLength,
76 PSID pDestinationSid,
77 PSID pSourceSid)
78 {
79 NTSTATUS Status;
80
81 Status = RtlCopySid (nDestinationSidLength,
82 pDestinationSid,
83 pSourceSid);
84 if (!NT_SUCCESS (Status))
85 {
86 SetLastError (RtlNtStatusToDosError (Status));
87 return FALSE;
88 }
89
90 return TRUE;
91 }
92
93
94 /*
95 * @implemented
96 */
97 BOOL STDCALL
98 EqualPrefixSid (PSID pSid1,
99 PSID pSid2)
100 {
101 return RtlEqualPrefixSid (pSid1, pSid2);
102 }
103
104
105 /*
106 * @implemented
107 */
108 BOOL STDCALL
109 EqualSid (PSID pSid1,
110 PSID pSid2)
111 {
112 return RtlEqualSid (pSid1, pSid2);
113 }
114
115
116 /*
117 * @implemented
118 *
119 * RETURNS
120 * Docs says this function does NOT return a value
121 * even thou it's defined to return a PVOID...
122 */
123 PVOID STDCALL
124 FreeSid (PSID pSid)
125 {
126 return RtlFreeSid (pSid);
127 }
128
129
130 /*
131 * @implemented
132 */
133 DWORD STDCALL
134 GetLengthSid (PSID pSid)
135 {
136 return (DWORD)RtlLengthSid (pSid);
137 }
138
139
140 /*
141 * @implemented
142 */
143 PSID_IDENTIFIER_AUTHORITY STDCALL
144 GetSidIdentifierAuthority (PSID pSid)
145 {
146 return RtlIdentifierAuthoritySid (pSid);
147 }
148
149
150 /*
151 * @implemented
152 */
153 DWORD STDCALL
154 GetSidLengthRequired (UCHAR nSubAuthorityCount)
155 {
156 return (DWORD)RtlLengthRequiredSid (nSubAuthorityCount);
157 }
158
159
160 /*
161 * @implemented
162 */
163 PDWORD STDCALL
164 GetSidSubAuthority (PSID pSid,
165 DWORD nSubAuthority)
166 {
167 return (PDWORD)RtlSubAuthoritySid (pSid, nSubAuthority);
168 }
169
170
171 /*
172 * @implemented
173 */
174 PUCHAR STDCALL
175 GetSidSubAuthorityCount (PSID pSid)
176 {
177 return RtlSubAuthorityCountSid (pSid);
178 }
179
180
181 /*
182 * @implemented
183 */
184 BOOL STDCALL
185 InitializeSid (PSID Sid,
186 PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
187 BYTE nSubAuthorityCount)
188 {
189 NTSTATUS Status;
190
191 Status = RtlInitializeSid (Sid,
192 pIdentifierAuthority,
193 nSubAuthorityCount);
194 if (!NT_SUCCESS (Status))
195 {
196 SetLastError (RtlNtStatusToDosError (Status));
197 return FALSE;
198 }
199
200 return TRUE;
201 }
202
203
204 /*
205 * @implemented
206 */
207 BOOL STDCALL
208 IsValidSid (PSID pSid)
209 {
210 return (BOOL)RtlValidSid (pSid);
211 }
212
213 /*
214 * @implemented
215 */
216 BOOL STDCALL
217 ConvertSidToStringSidW(PSID Sid, LPWSTR *StringSid)
218 {
219 NTSTATUS Status;
220 UNICODE_STRING UnicodeString;
221 WCHAR FixedBuffer[64];
222
223 if (! RtlValidSid(Sid))
224 {
225 SetLastError(ERROR_INVALID_SID);
226 return FALSE;
227 }
228
229 UnicodeString.Length = 0;
230 UnicodeString.MaximumLength = sizeof(FixedBuffer);
231 UnicodeString.Buffer = FixedBuffer;
232 Status = RtlConvertSidToUnicodeString(&UnicodeString, Sid, FALSE);
233 if (STATUS_BUFFER_TOO_SMALL == Status)
234 {
235 Status = RtlConvertSidToUnicodeString(&UnicodeString, Sid, TRUE);
236 }
237 if (! NT_SUCCESS(Status))
238 {
239 SetLastError(RtlNtStatusToDosError(Status));
240 return FALSE;
241 }
242
243 *StringSid = LocalAlloc(LMEM_FIXED, UnicodeString.Length + sizeof(WCHAR));
244 if (NULL == *StringSid)
245 {
246 if (UnicodeString.Buffer != FixedBuffer)
247 {
248 RtlFreeUnicodeString(&UnicodeString);
249 }
250 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
251 return FALSE;
252 }
253
254 MoveMemory(*StringSid, UnicodeString.Buffer, UnicodeString.Length);
255 ZeroMemory((PCHAR) *StringSid + UnicodeString.Length, sizeof(WCHAR));
256 if (UnicodeString.Buffer != FixedBuffer)
257 {
258 RtlFreeUnicodeString(&UnicodeString);
259 }
260
261 return TRUE;
262 }
263
264
265 /*
266 * @implemented
267 */
268 BOOL STDCALL
269 ConvertSidToStringSidA(PSID Sid, LPSTR *StringSid)
270 {
271 LPWSTR StringSidW;
272 int Len;
273
274 if (! ConvertSidToStringSidW(Sid, &StringSidW))
275 {
276 return FALSE;
277 }
278
279 Len = WideCharToMultiByte(CP_ACP, 0, StringSidW, -1, NULL, 0, NULL, NULL);
280 if (Len <= 0)
281 {
282 LocalFree(StringSidW);
283 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
284 return FALSE;
285 }
286 *StringSid = LocalAlloc(LMEM_FIXED, Len);
287 if (NULL == *StringSid)
288 {
289 LocalFree(StringSidW);
290 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
291 return FALSE;
292 }
293
294 if (! WideCharToMultiByte(CP_ACP, 0, StringSidW, -1, *StringSid, Len, NULL, NULL))
295 {
296 LocalFree(StringSid);
297 LocalFree(StringSidW);
298 return FALSE;
299 }
300
301 LocalFree(StringSidW);
302
303 return TRUE;
304 }
305
306 /* EOF */