2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * PURPOSE: Security manager
6 * PROGRAMER: David Welch <welch@cwcom.net>
9 /* INCLUDES *****************************************************************/
16 #define TAG_SID TAG('p', 'S', 'i', 'd')
18 /* FUNCTIONS ***************************************************************/
21 RtlValidSid(IN PSID Sid_
)
27 if ((Sid
->Revision
!= SID_REVISION
) ||
28 (Sid
->SubAuthorityCount
> SID_MAX_SUB_AUTHORITIES
))
41 RtlLengthRequiredSid(IN ULONG SubAuthorityCount
)
45 return (sizeof(SID
) - (ANYSIZE_ARRAY
* sizeof(ULONG
)) +
46 (SubAuthorityCount
* sizeof(ULONG
)));
54 RtlInitializeSid(IN PSID Sid_
,
55 IN PSID_IDENTIFIER_AUTHORITY IdentifierAuthority
,
56 IN UCHAR SubAuthorityCount
)
62 Sid
->Revision
= SID_REVISION
;
63 Sid
->SubAuthorityCount
= SubAuthorityCount
;
64 memcpy(&Sid
->IdentifierAuthority
,
66 sizeof(SID_IDENTIFIER_AUTHORITY
));
68 return STATUS_SUCCESS
;
76 RtlSubAuthoritySid(IN PSID Sid_
,
77 IN ULONG SubAuthority
)
83 return &Sid
->SubAuthority
[SubAuthority
];
91 RtlSubAuthorityCountSid(IN PSID Sid_
)
97 return &Sid
->SubAuthorityCount
;
105 RtlEqualSid(IN PSID Sid1_
,
114 if (Sid1
->Revision
!= Sid2
->Revision
||
115 (*RtlSubAuthorityCountSid(Sid1
)) != (*RtlSubAuthorityCountSid(Sid2
)))
120 SidLen
= RtlLengthSid(Sid1
);
121 return RtlCompareMemory(Sid1
, Sid2
, SidLen
) == SidLen
;
129 RtlLengthSid(IN PSID Sid_
)
135 return (sizeof(SID
) - sizeof(Sid
->SubAuthority
) +
136 (Sid
->SubAuthorityCount
* sizeof(ULONG
)));
144 RtlCopySid(ULONG BufferLength
,
150 if (BufferLength
< RtlLengthSid(Src
))
152 return STATUS_UNSUCCESSFUL
;
159 return STATUS_SUCCESS
;
167 RtlCopySidAndAttributesArray(ULONG Count
,
168 PSID_AND_ATTRIBUTES Src
,
170 PSID_AND_ATTRIBUTES Dest
,
172 PVOID
* RemainingSidArea
,
173 PULONG RemainingSidAreaSize
)
181 Length
= SidAreaSize
;
183 for (i
=0; i
<Count
; i
++)
185 if (RtlLengthSid(Src
[i
].Sid
) > Length
)
187 return(STATUS_BUFFER_TOO_SMALL
);
189 SidLength
= RtlLengthSid(Src
[i
].Sid
);
190 Length
= Length
- SidLength
;
191 Dest
[i
].Sid
= SidArea
;
192 Dest
[i
].Attributes
= Src
[i
].Attributes
;
193 RtlCopySid(SidLength
,
196 SidArea
= (PVOID
)((ULONG_PTR
)SidArea
+ SidLength
);
198 *RemainingSidArea
= SidArea
;
199 *RemainingSidAreaSize
= Length
;
200 return(STATUS_SUCCESS
);
207 PSID_IDENTIFIER_AUTHORITY NTAPI
208 RtlIdentifierAuthoritySid(IN PSID Sid_
)
214 return &Sid
->IdentifierAuthority
;
222 RtlAllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY IdentifierAuthority
,
223 UCHAR SubAuthorityCount
,
238 if (SubAuthorityCount
> 8)
239 return STATUS_INVALID_SID
;
242 return STATUS_INVALID_PARAMETER
;
244 pSid
= RtlpAllocateMemory(RtlLengthRequiredSid(SubAuthorityCount
),
247 return STATUS_NO_MEMORY
;
249 pSid
->Revision
= SID_REVISION
;
250 pSid
->SubAuthorityCount
= SubAuthorityCount
;
251 memcpy(&pSid
->IdentifierAuthority
,
253 sizeof(SID_IDENTIFIER_AUTHORITY
));
255 switch (SubAuthorityCount
)
258 pSid
->SubAuthority
[7] = SubAuthority7
;
260 pSid
->SubAuthority
[6] = SubAuthority6
;
262 pSid
->SubAuthority
[5] = SubAuthority5
;
264 pSid
->SubAuthority
[4] = SubAuthority4
;
266 pSid
->SubAuthority
[3] = SubAuthority3
;
268 pSid
->SubAuthority
[2] = SubAuthority2
;
270 pSid
->SubAuthority
[1] = SubAuthority1
;
272 pSid
->SubAuthority
[0] = SubAuthority0
;
278 return STATUS_SUCCESS
;
286 * Docs says FreeSid does NOT return a value
287 * even thou it's defined to return a PVOID...
290 RtlFreeSid(IN PSID Sid
)
294 RtlpFreeMemory(Sid
, TAG_SID
);
303 RtlEqualPrefixSid(IN PSID Sid1_
,
311 return(Sid1
->SubAuthorityCount
== Sid2
->SubAuthorityCount
&&
312 !RtlCompareMemory(Sid1
, Sid2
,
313 (Sid1
->SubAuthorityCount
- 1) * sizeof(DWORD
) + 8));
321 RtlConvertSidToUnicodeString(PUNICODE_STRING String
,
323 BOOLEAN AllocateBuffer
)
333 if (RtlValidSid (Sid
) == FALSE
)
334 return STATUS_INVALID_SID
;
337 wcs
+= swprintf (wcs
, L
"S-%u-", Sid
->Revision
);
338 if (Sid
->IdentifierAuthority
.Value
[0] == 0 &&
339 Sid
->IdentifierAuthority
.Value
[1] == 0)
341 wcs
+= swprintf (wcs
,
343 (ULONG
)Sid
->IdentifierAuthority
.Value
[2] << 24 |
344 (ULONG
)Sid
->IdentifierAuthority
.Value
[3] << 16 |
345 (ULONG
)Sid
->IdentifierAuthority
.Value
[4] << 8 |
346 (ULONG
)Sid
->IdentifierAuthority
.Value
[5]);
350 wcs
+= swprintf (wcs
,
351 L
"0x%02hx%02hx%02hx%02hx%02hx%02hx",
352 Sid
->IdentifierAuthority
.Value
[0],
353 Sid
->IdentifierAuthority
.Value
[1],
354 Sid
->IdentifierAuthority
.Value
[2],
355 Sid
->IdentifierAuthority
.Value
[3],
356 Sid
->IdentifierAuthority
.Value
[4],
357 Sid
->IdentifierAuthority
.Value
[5]);
360 for (i
= 0; i
< Sid
->SubAuthorityCount
; i
++)
362 wcs
+= swprintf (wcs
,
364 Sid
->SubAuthority
[i
]);
369 if (!RtlCreateUnicodeString(String
,
372 return STATUS_NO_MEMORY
;
377 Length
= (wcs
- Buffer
) * sizeof(WCHAR
);
379 if (Length
> String
->MaximumLength
)
380 return STATUS_BUFFER_TOO_SMALL
;
382 String
->Length
= Length
;
383 RtlCopyMemory (String
->Buffer
,
386 if (Length
< String
->MaximumLength
)
387 String
->Buffer
[Length
/ sizeof(WCHAR
)] = 0;
390 return STATUS_SUCCESS
;