1 /* $Id: sid.c,v 1.4 2004/07/12 19:39:29 ekohl Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * PURPOSE: Security manager
7 * PROGRAMER: David Welch <welch@cwcom.net>
9 * 26/07/98: Added stubs for security functions
12 /* INCLUDES *****************************************************************/
15 #include <ddk/ntddk.h>
22 /* FUNCTIONS ***************************************************************/
25 RtlValidSid(IN PSID Sid
)
27 if ((Sid
->Revision
!= SID_REVISION
) ||
28 (Sid
->SubAuthorityCount
> SID_MAX_SUB_AUTHORITIES
))
41 RtlLengthRequiredSid(IN UCHAR SubAuthorityCount
)
43 return (sizeof(SID
) + (SubAuthorityCount
- 1) * sizeof(ULONG
));
51 RtlInitializeSid(IN PSID Sid
,
52 IN PSID_IDENTIFIER_AUTHORITY IdentifierAuthority
,
53 IN UCHAR SubAuthorityCount
)
55 Sid
->Revision
= SID_REVISION
;
56 Sid
->SubAuthorityCount
= SubAuthorityCount
;
57 memcpy(&Sid
->IdentifierAuthority
,
59 sizeof(SID_IDENTIFIER_AUTHORITY
));
61 return STATUS_SUCCESS
;
69 RtlSubAuthoritySid(IN PSID Sid
,
70 IN ULONG SubAuthority
)
72 return &Sid
->SubAuthority
[SubAuthority
];
80 RtlSubAuthorityCountSid(IN PSID Sid
)
82 return &Sid
->SubAuthorityCount
;
90 RtlEqualSid(IN PSID Sid1
,
93 if (Sid1
->Revision
!= Sid2
->Revision
)
97 if ((*RtlSubAuthorityCountSid(Sid1
)) != (*RtlSubAuthorityCountSid(Sid2
)))
101 if (RtlCompareMemory(Sid1
, Sid2
, RtlLengthSid(Sid1
)) != RtlLengthSid(Sid1
))
113 RtlLengthSid(IN PSID Sid
)
115 return (sizeof(SID
) + (Sid
->SubAuthorityCount
-1) * sizeof(ULONG
));
123 RtlCopySid(ULONG BufferLength
,
127 if (BufferLength
< RtlLengthSid(Src
))
129 return STATUS_UNSUCCESSFUL
;
136 return STATUS_SUCCESS
;
144 RtlCopySidAndAttributesArray(ULONG Count
,
145 PSID_AND_ATTRIBUTES Src
,
147 PSID_AND_ATTRIBUTES Dest
,
149 PVOID
* RemainingSidArea
,
150 PULONG RemainingSidAreaSize
)
156 Length
= SidAreaSize
;
158 for (i
=0; i
<Count
; i
++)
160 if (RtlLengthSid(Src
[i
].Sid
) > Length
)
162 return(STATUS_BUFFER_TOO_SMALL
);
164 SidLength
= RtlLengthSid(Src
[i
].Sid
);
165 Length
= Length
- SidLength
;
166 Dest
[i
].Sid
= SidArea
;
167 Dest
[i
].Attributes
= Src
[i
].Attributes
;
168 RtlCopySid(SidLength
,
171 SidArea
= SidArea
+ SidLength
;
173 *RemainingSidArea
= SidArea
;
174 *RemainingSidAreaSize
= Length
;
175 return(STATUS_SUCCESS
);
182 PSID_IDENTIFIER_AUTHORITY STDCALL
183 RtlIdentifierAuthoritySid(IN PSID Sid
)
185 return &Sid
->IdentifierAuthority
;
193 RtlAllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY IdentifierAuthority
,
194 UCHAR SubAuthorityCount
,
207 if (SubAuthorityCount
> 8)
208 return STATUS_INVALID_SID
;
211 return STATUS_INVALID_PARAMETER
;
213 pSid
= (PSID
)ExAllocatePool(PagedPool
,
214 sizeof(SID
) + (SubAuthorityCount
- 1) * sizeof(ULONG
));
216 return STATUS_NO_MEMORY
;
218 pSid
->Revision
= SID_REVISION
;
219 pSid
->SubAuthorityCount
= SubAuthorityCount
;
220 memcpy(&pSid
->IdentifierAuthority
,
222 sizeof(SID_IDENTIFIER_AUTHORITY
));
224 switch (SubAuthorityCount
)
227 pSid
->SubAuthority
[7] = SubAuthority7
;
229 pSid
->SubAuthority
[6] = SubAuthority6
;
231 pSid
->SubAuthority
[5] = SubAuthority5
;
233 pSid
->SubAuthority
[4] = SubAuthority4
;
235 pSid
->SubAuthority
[3] = SubAuthority3
;
237 pSid
->SubAuthority
[2] = SubAuthority2
;
239 pSid
->SubAuthority
[1] = SubAuthority1
;
241 pSid
->SubAuthority
[0] = SubAuthority0
;
247 return STATUS_SUCCESS
;
255 * Docs says FreeSid does NOT return a value
256 * even thou it's defined to return a PVOID...
259 RtlFreeSid(IN PSID Sid
)
270 RtlEqualPrefixSid(IN PSID Sid1
,
273 return(Sid1
->SubAuthorityCount
== Sid2
->SubAuthorityCount
&&
274 !RtlCompareMemory(Sid1
, Sid2
,
275 (Sid1
->SubAuthorityCount
- 1) * sizeof(DWORD
) + 8));
283 RtlConvertSidToUnicodeString(PUNICODE_STRING String
,
285 BOOLEAN AllocateBuffer
)
292 if (RtlValidSid (Sid
) == FALSE
)
293 return STATUS_INVALID_SID
;
296 wcs
+= swprintf (wcs
, L
"S-%u-", Sid
->Revision
);
297 if (Sid
->IdentifierAuthority
.Value
[0] == 0 &&
298 Sid
->IdentifierAuthority
.Value
[1] == 0)
300 wcs
+= swprintf (wcs
,
302 (ULONG
)Sid
->IdentifierAuthority
.Value
[2] << 24 |
303 (ULONG
)Sid
->IdentifierAuthority
.Value
[3] << 16 |
304 (ULONG
)Sid
->IdentifierAuthority
.Value
[4] << 8 |
305 (ULONG
)Sid
->IdentifierAuthority
.Value
[5]);
309 wcs
+= swprintf (wcs
,
310 L
"0x%02hx%02hx%02hx%02hx%02hx%02hx",
311 Sid
->IdentifierAuthority
.Value
[0],
312 Sid
->IdentifierAuthority
.Value
[1],
313 Sid
->IdentifierAuthority
.Value
[2],
314 Sid
->IdentifierAuthority
.Value
[3],
315 Sid
->IdentifierAuthority
.Value
[4],
316 Sid
->IdentifierAuthority
.Value
[5]);
319 for (i
= 0; i
< Sid
->SubAuthorityCount
; i
++)
321 wcs
+= swprintf (wcs
,
323 Sid
->SubAuthority
[i
]);
326 Length
= (wcs
- Buffer
) * sizeof(WCHAR
);
329 String
->Buffer
= ExAllocatePool(PagedPool
,Length
+ sizeof(WCHAR
));
330 if (String
->Buffer
== NULL
)
331 return STATUS_NO_MEMORY
;
332 String
->MaximumLength
= Length
+ sizeof(WCHAR
);
336 if (Length
> String
->MaximumLength
)
337 return STATUS_BUFFER_TOO_SMALL
;
340 String
->Length
= Length
;
341 RtlCopyMemory (String
->Buffer
,
344 if (Length
< String
->MaximumLength
)
345 String
->Buffer
[Length
/ sizeof(WCHAR
)] = 0;
347 return STATUS_SUCCESS
;