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
) + (SubAuthorityCount
- 1) * sizeof(ULONG
));
53 RtlInitializeSid(IN PSID Sid_
,
54 IN PSID_IDENTIFIER_AUTHORITY IdentifierAuthority
,
55 IN UCHAR SubAuthorityCount
)
61 Sid
->Revision
= SID_REVISION
;
62 Sid
->SubAuthorityCount
= SubAuthorityCount
;
63 memcpy(&Sid
->IdentifierAuthority
,
65 sizeof(SID_IDENTIFIER_AUTHORITY
));
67 return STATUS_SUCCESS
;
75 RtlSubAuthoritySid(IN PSID Sid_
,
76 IN ULONG SubAuthority
)
82 return &Sid
->SubAuthority
[SubAuthority
];
90 RtlSubAuthorityCountSid(IN PSID Sid_
)
96 return &Sid
->SubAuthorityCount
;
104 RtlEqualSid(IN PSID Sid1_
,
112 if (Sid1
->Revision
!= Sid2
->Revision
)
116 if ((*RtlSubAuthorityCountSid(Sid1
)) != (*RtlSubAuthorityCountSid(Sid2
)))
120 if (RtlCompareMemory(Sid1
, Sid2
, RtlLengthSid(Sid1
)) != RtlLengthSid(Sid1
))
132 RtlLengthSid(IN PSID Sid_
)
138 return (sizeof(SID
) + (Sid
->SubAuthorityCount
-1) * sizeof(ULONG
));
146 RtlCopySid(ULONG BufferLength
,
152 if (BufferLength
< RtlLengthSid(Src
))
154 return STATUS_UNSUCCESSFUL
;
161 return STATUS_SUCCESS
;
169 RtlCopySidAndAttributesArray(ULONG Count
,
170 PSID_AND_ATTRIBUTES Src
,
172 PSID_AND_ATTRIBUTES Dest
,
174 PVOID
* RemainingSidArea
,
175 PULONG RemainingSidAreaSize
)
183 Length
= SidAreaSize
;
185 for (i
=0; i
<Count
; i
++)
187 if (RtlLengthSid(Src
[i
].Sid
) > Length
)
189 return(STATUS_BUFFER_TOO_SMALL
);
191 SidLength
= RtlLengthSid(Src
[i
].Sid
);
192 Length
= Length
- SidLength
;
193 Dest
[i
].Sid
= SidArea
;
194 Dest
[i
].Attributes
= Src
[i
].Attributes
;
195 RtlCopySid(SidLength
,
198 SidArea
= (PVOID
)((ULONG_PTR
)SidArea
+ SidLength
);
200 *RemainingSidArea
= SidArea
;
201 *RemainingSidAreaSize
= Length
;
202 return(STATUS_SUCCESS
);
209 PSID_IDENTIFIER_AUTHORITY STDCALL
210 RtlIdentifierAuthoritySid(IN PSID Sid_
)
216 return &Sid
->IdentifierAuthority
;
224 RtlAllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY IdentifierAuthority
,
225 UCHAR SubAuthorityCount
,
240 if (SubAuthorityCount
> 8)
241 return STATUS_INVALID_SID
;
244 return STATUS_INVALID_PARAMETER
;
246 pSid
= RtlpAllocateMemory(sizeof(SID
) + (SubAuthorityCount
- 1) * sizeof(ULONG
),
249 return STATUS_NO_MEMORY
;
251 pSid
->Revision
= SID_REVISION
;
252 pSid
->SubAuthorityCount
= SubAuthorityCount
;
253 memcpy(&pSid
->IdentifierAuthority
,
255 sizeof(SID_IDENTIFIER_AUTHORITY
));
257 switch (SubAuthorityCount
)
260 pSid
->SubAuthority
[7] = SubAuthority7
;
262 pSid
->SubAuthority
[6] = SubAuthority6
;
264 pSid
->SubAuthority
[5] = SubAuthority5
;
266 pSid
->SubAuthority
[4] = SubAuthority4
;
268 pSid
->SubAuthority
[3] = SubAuthority3
;
270 pSid
->SubAuthority
[2] = SubAuthority2
;
272 pSid
->SubAuthority
[1] = SubAuthority1
;
274 pSid
->SubAuthority
[0] = SubAuthority0
;
280 return STATUS_SUCCESS
;
288 * Docs says FreeSid does NOT return a value
289 * even thou it's defined to return a PVOID...
292 RtlFreeSid(IN PSID Sid
)
296 RtlpFreeMemory(Sid
, TAG_SID
);
305 RtlEqualPrefixSid(IN PSID Sid1_
,
313 return(Sid1
->SubAuthorityCount
== Sid2
->SubAuthorityCount
&&
314 !RtlCompareMemory(Sid1
, Sid2
,
315 (Sid1
->SubAuthorityCount
- 1) * sizeof(DWORD
) + 8));
323 RtlConvertSidToUnicodeString(PUNICODE_STRING String
,
325 BOOLEAN AllocateBuffer
)
335 if (RtlValidSid (Sid
) == FALSE
)
336 return STATUS_INVALID_SID
;
339 wcs
+= swprintf (wcs
, L
"S-%u-", Sid
->Revision
);
340 if (Sid
->IdentifierAuthority
.Value
[0] == 0 &&
341 Sid
->IdentifierAuthority
.Value
[1] == 0)
343 wcs
+= swprintf (wcs
,
345 (ULONG
)Sid
->IdentifierAuthority
.Value
[2] << 24 |
346 (ULONG
)Sid
->IdentifierAuthority
.Value
[3] << 16 |
347 (ULONG
)Sid
->IdentifierAuthority
.Value
[4] << 8 |
348 (ULONG
)Sid
->IdentifierAuthority
.Value
[5]);
352 wcs
+= swprintf (wcs
,
353 L
"0x%02hx%02hx%02hx%02hx%02hx%02hx",
354 Sid
->IdentifierAuthority
.Value
[0],
355 Sid
->IdentifierAuthority
.Value
[1],
356 Sid
->IdentifierAuthority
.Value
[2],
357 Sid
->IdentifierAuthority
.Value
[3],
358 Sid
->IdentifierAuthority
.Value
[4],
359 Sid
->IdentifierAuthority
.Value
[5]);
362 for (i
= 0; i
< Sid
->SubAuthorityCount
; i
++)
364 wcs
+= swprintf (wcs
,
366 Sid
->SubAuthority
[i
]);
369 Length
= (wcs
- Buffer
) * sizeof(WCHAR
);
372 String
->Buffer
= RtlpAllocateMemory(Length
+ sizeof(WCHAR
),
374 if (String
->Buffer
== NULL
)
375 return STATUS_NO_MEMORY
;
376 String
->MaximumLength
= Length
+ sizeof(WCHAR
);
380 if (Length
> String
->MaximumLength
)
381 return STATUS_BUFFER_TOO_SMALL
;
384 String
->Length
= Length
;
385 RtlCopyMemory (String
->Buffer
,
388 if (Length
< String
->MaximumLength
)
389 String
->Buffer
[Length
/ sizeof(WCHAR
)] = 0;
391 return STATUS_SUCCESS
;