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 'diSp'
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 (ULONG
)FIELD_OFFSET(SID
,
46 SubAuthority
[SubAuthorityCount
]);
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 (PULONG
)&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 (ULONG
)FIELD_OFFSET(SID
,
136 SubAuthority
[Sid
->SubAuthorityCount
]);
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_
,
312 if (Sid1
->SubAuthorityCount
== Sid2
->SubAuthorityCount
)
314 SidLen
= FIELD_OFFSET(SID
,
315 SubAuthority
[Sid1
->SubAuthorityCount
]);
316 return RtlCompareMemory(Sid1
,
329 RtlConvertSidToUnicodeString(PUNICODE_STRING String
,
331 BOOLEAN AllocateBuffer
)
341 if (RtlValidSid (Sid
) == FALSE
)
342 return STATUS_INVALID_SID
;
345 wcs
+= swprintf (wcs
, L
"S-%u-", Sid
->Revision
);
346 if (Sid
->IdentifierAuthority
.Value
[0] == 0 &&
347 Sid
->IdentifierAuthority
.Value
[1] == 0)
349 wcs
+= swprintf (wcs
,
351 (ULONG
)Sid
->IdentifierAuthority
.Value
[2] << 24 |
352 (ULONG
)Sid
->IdentifierAuthority
.Value
[3] << 16 |
353 (ULONG
)Sid
->IdentifierAuthority
.Value
[4] << 8 |
354 (ULONG
)Sid
->IdentifierAuthority
.Value
[5]);
358 wcs
+= swprintf (wcs
,
359 L
"0x%02hx%02hx%02hx%02hx%02hx%02hx",
360 Sid
->IdentifierAuthority
.Value
[0],
361 Sid
->IdentifierAuthority
.Value
[1],
362 Sid
->IdentifierAuthority
.Value
[2],
363 Sid
->IdentifierAuthority
.Value
[3],
364 Sid
->IdentifierAuthority
.Value
[4],
365 Sid
->IdentifierAuthority
.Value
[5]);
368 for (i
= 0; i
< Sid
->SubAuthorityCount
; i
++)
370 wcs
+= swprintf (wcs
,
372 Sid
->SubAuthority
[i
]);
377 if (!RtlCreateUnicodeString(String
,
380 return STATUS_NO_MEMORY
;
385 Length
= (wcs
- Buffer
) * sizeof(WCHAR
);
387 if (Length
> String
->MaximumLength
)
388 return STATUS_BUFFER_TOO_SMALL
;
390 String
->Length
= Length
;
391 RtlCopyMemory (String
->Buffer
,
394 if (Length
< String
->MaximumLength
)
395 String
->Buffer
[Length
/ sizeof(WCHAR
)] = 0;
398 return STATUS_SUCCESS
;