Fixed a typo
[reactos.git] / reactos / ntoskrnl / se / sid.c
1 /* $Id: sid.c,v 1.6 2000/10/08 19:12:01 ekohl Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * PURPOSE: Security manager
6 * FILE: ntoskrnl/se/sid.c
7 * PROGRAMER: David Welch <welch@cwcom.net>
8 * REVISION HISTORY:
9 * 26/07/98: Added stubs for security functions
10 */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15
16 #include <internal/debug.h>
17
18 /* FUNCTIONS ***************************************************************/
19
20 BOOLEAN STDCALL RtlValidSid (PSID Sid)
21 {
22 if ((Sid->Revision & 0xf) != 1)
23 {
24 return(FALSE);
25 }
26 if (Sid->SubAuthorityCount > 15)
27 {
28 return(FALSE);
29 }
30 return(TRUE);
31 }
32
33 ULONG STDCALL RtlLengthRequiredSid (UCHAR SubAuthorityCount)
34 {
35 return(sizeof(SID) + (SubAuthorityCount - 1) * sizeof(ULONG));
36 }
37
38 NTSTATUS STDCALL RtlInitializeSid (PSID Sid,
39 PSID_IDENTIFIER_AUTHORITY IdentifierAuthority,
40 UCHAR SubAuthorityCount)
41 {
42 Sid->Revision = 1;
43 Sid->SubAuthorityCount = SubAuthorityCount;
44 memcpy(&Sid->IdentifierAuthority, IdentifierAuthority,
45 sizeof(SID_IDENTIFIER_AUTHORITY));
46 return(STATUS_SUCCESS);
47 }
48
49 PULONG STDCALL RtlSubAuthoritySid (PSID Sid, ULONG SubAuthority)
50 {
51 return(&Sid->SubAuthority[SubAuthority]);
52 }
53
54 PUCHAR STDCALL RtlSubAuthorityCountSid (PSID Sid)
55 {
56 return(&Sid->SubAuthorityCount);
57 }
58
59 BOOLEAN STDCALL RtlEqualSid (PSID Sid1, PSID Sid2)
60 {
61 if (Sid1->Revision != Sid2->Revision)
62 {
63 return(FALSE);
64 }
65 if ((*RtlSubAuthorityCountSid(Sid1)) !=
66 (*RtlSubAuthorityCountSid(Sid2)))
67 {
68 return(FALSE);
69 }
70 if (memcmp(Sid1, Sid2, RtlLengthSid(Sid1) != 0))
71 {
72 return(FALSE);
73 }
74 return(TRUE);
75 }
76
77 ULONG STDCALL RtlLengthSid (PSID Sid)
78 {
79 return(sizeof(SID) + (Sid->SubAuthorityCount-1)*4);
80 }
81
82 NTSTATUS STDCALL RtlCopySid (ULONG BufferLength, PSID Dest, PSID Src)
83 {
84 if (BufferLength < RtlLengthSid(Src))
85 {
86 return(STATUS_UNSUCCESSFUL);
87 }
88 memmove(Dest, Src, RtlLengthSid(Src));
89 return(STATUS_SUCCESS);
90 }
91
92 NTSTATUS STDCALL
93 RtlConvertSidToUnicodeString(PUNICODE_STRING String,
94 PSID Sid,
95 BOOLEAN AllocateString)
96 {
97 WCHAR Buffer[256];
98 PWSTR Ptr;
99 ULONG Length;
100 ULONG i;
101
102 if (!RtlValidSid(Sid))
103 return STATUS_INVALID_SID;
104
105 Ptr = Buffer;
106 Ptr += swprintf (Ptr,
107 L"S-%u-",
108 Sid->Revision);
109
110 if(!Sid->IdentifierAuthority.Value[0] &&
111 !Sid->IdentifierAuthority.Value[1])
112 {
113 Ptr += swprintf(Ptr,
114 L"%u",
115 (ULONG)Sid->IdentifierAuthority.Value[2] << 24 |
116 (ULONG)Sid->IdentifierAuthority.Value[3] << 16 |
117 (ULONG)Sid->IdentifierAuthority.Value[4] << 8 |
118 (ULONG)Sid->IdentifierAuthority.Value[5]);
119 }
120 else
121 {
122 Ptr += swprintf(Ptr,
123 L"0x%02hx%02hx%02hx%02hx%02hx%02hx",
124 Sid->IdentifierAuthority.Value[0],
125 Sid->IdentifierAuthority.Value[1],
126 Sid->IdentifierAuthority.Value[2],
127 Sid->IdentifierAuthority.Value[3],
128 Sid->IdentifierAuthority.Value[4],
129 Sid->IdentifierAuthority.Value[5]);
130 }
131
132 for (i = 0; i < Sid->SubAuthorityCount; i++)
133 {
134 Ptr += swprintf(Ptr,
135 L"-%u",
136 Sid->SubAuthority[i]);
137 }
138
139 Length = (Ptr - Buffer) * sizeof(WCHAR);
140
141 if (AllocateString)
142 {
143 String->Buffer = ExAllocatePool(NonPagedPool,
144 Length + sizeof(WCHAR));
145 if (String->Buffer == NULL)
146 return STATUS_NO_MEMORY;
147
148 String->MaximumLength = Length + sizeof(WCHAR);
149 }
150 else
151 {
152 if (Length > String->MaximumLength)
153 return STATUS_BUFFER_TOO_SMALL;
154 }
155 String->Length = Length;
156 memmove(String->Buffer,
157 Buffer,
158 Length);
159 if (Length < String->MaximumLength)
160 String->Buffer[Length] = 0;
161
162 return STATUS_SUCCESS;
163 }
164
165 /* EOF */