Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygensid.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: See COPYING in the top level directory 00003 * PROJECT: ReactOS system libraries 00004 * PURPOSE: Security manager 00005 * FILE: lib/rtl/sid.c 00006 * PROGRAMER: David Welch <welch@cwcom.net> 00007 */ 00008 00009 /* INCLUDES *****************************************************************/ 00010 00011 #include <rtl.h> 00012 00013 #define NDEBUG 00014 #include <debug.h> 00015 00016 #define TAG_SID 'diSp' 00017 00018 /* FUNCTIONS ***************************************************************/ 00019 00020 BOOLEAN NTAPI 00021 RtlValidSid(IN PSID Sid_) 00022 { 00023 PISID Sid = Sid_; 00024 00025 PAGED_CODE_RTL(); 00026 00027 if ((Sid->Revision != SID_REVISION) || 00028 (Sid->SubAuthorityCount > SID_MAX_SUB_AUTHORITIES)) 00029 { 00030 return FALSE; 00031 } 00032 00033 return TRUE; 00034 } 00035 00036 00037 /* 00038 * @implemented 00039 */ 00040 ULONG NTAPI 00041 RtlLengthRequiredSid(IN ULONG SubAuthorityCount) 00042 { 00043 PAGED_CODE_RTL(); 00044 00045 return (ULONG)FIELD_OFFSET(SID, 00046 SubAuthority[SubAuthorityCount]); 00047 } 00048 00049 00050 /* 00051 * @implemented 00052 */ 00053 NTSTATUS NTAPI 00054 RtlInitializeSid(IN PSID Sid_, 00055 IN PSID_IDENTIFIER_AUTHORITY IdentifierAuthority, 00056 IN UCHAR SubAuthorityCount) 00057 { 00058 PISID Sid = Sid_; 00059 00060 PAGED_CODE_RTL(); 00061 00062 Sid->Revision = SID_REVISION; 00063 Sid->SubAuthorityCount = SubAuthorityCount; 00064 memcpy(&Sid->IdentifierAuthority, 00065 IdentifierAuthority, 00066 sizeof(SID_IDENTIFIER_AUTHORITY)); 00067 00068 return STATUS_SUCCESS; 00069 } 00070 00071 00072 /* 00073 * @implemented 00074 */ 00075 PULONG NTAPI 00076 RtlSubAuthoritySid(IN PSID Sid_, 00077 IN ULONG SubAuthority) 00078 { 00079 PISID Sid = Sid_; 00080 00081 PAGED_CODE_RTL(); 00082 00083 return (PULONG)&Sid->SubAuthority[SubAuthority]; 00084 } 00085 00086 00087 /* 00088 * @implemented 00089 */ 00090 PUCHAR NTAPI 00091 RtlSubAuthorityCountSid(IN PSID Sid_) 00092 { 00093 PISID Sid = Sid_; 00094 00095 PAGED_CODE_RTL(); 00096 00097 return &Sid->SubAuthorityCount; 00098 } 00099 00100 00101 /* 00102 * @implemented 00103 */ 00104 BOOLEAN NTAPI 00105 RtlEqualSid(IN PSID Sid1_, 00106 IN PSID Sid2_) 00107 { 00108 PISID Sid1 = Sid1_; 00109 PISID Sid2 = Sid2_; 00110 SIZE_T SidLen; 00111 00112 PAGED_CODE_RTL(); 00113 00114 if (Sid1->Revision != Sid2->Revision || 00115 (*RtlSubAuthorityCountSid(Sid1)) != (*RtlSubAuthorityCountSid(Sid2))) 00116 { 00117 return(FALSE); 00118 } 00119 00120 SidLen = RtlLengthSid(Sid1); 00121 return RtlCompareMemory(Sid1, Sid2, SidLen) == SidLen; 00122 } 00123 00124 00125 /* 00126 * @implemented 00127 */ 00128 ULONG NTAPI 00129 RtlLengthSid(IN PSID Sid_) 00130 { 00131 PISID Sid = Sid_; 00132 00133 PAGED_CODE_RTL(); 00134 00135 return (ULONG)FIELD_OFFSET(SID, 00136 SubAuthority[Sid->SubAuthorityCount]); 00137 } 00138 00139 00140 /* 00141 * @implemented 00142 */ 00143 NTSTATUS NTAPI 00144 RtlCopySid(ULONG BufferLength, 00145 PSID Dest, 00146 PSID Src) 00147 { 00148 PAGED_CODE_RTL(); 00149 00150 if (BufferLength < RtlLengthSid(Src)) 00151 { 00152 return STATUS_UNSUCCESSFUL; 00153 } 00154 00155 memmove(Dest, 00156 Src, 00157 RtlLengthSid(Src)); 00158 00159 return STATUS_SUCCESS; 00160 } 00161 00162 00163 /* 00164 * @implemented 00165 */ 00166 NTSTATUS NTAPI 00167 RtlCopySidAndAttributesArray(ULONG Count, 00168 PSID_AND_ATTRIBUTES Src, 00169 ULONG SidAreaSize, 00170 PSID_AND_ATTRIBUTES Dest, 00171 PVOID SidArea, 00172 PVOID* RemainingSidArea, 00173 PULONG RemainingSidAreaSize) 00174 { 00175 ULONG SidLength; 00176 ULONG Length; 00177 ULONG i; 00178 00179 PAGED_CODE_RTL(); 00180 00181 Length = SidAreaSize; 00182 00183 for (i=0; i<Count; i++) 00184 { 00185 if (RtlLengthSid(Src[i].Sid) > Length) 00186 { 00187 return(STATUS_BUFFER_TOO_SMALL); 00188 } 00189 SidLength = RtlLengthSid(Src[i].Sid); 00190 Length = Length - SidLength; 00191 Dest[i].Sid = SidArea; 00192 Dest[i].Attributes = Src[i].Attributes; 00193 RtlCopySid(SidLength, 00194 SidArea, 00195 Src[i].Sid); 00196 SidArea = (PVOID)((ULONG_PTR)SidArea + SidLength); 00197 } 00198 *RemainingSidArea = SidArea; 00199 *RemainingSidAreaSize = Length; 00200 return(STATUS_SUCCESS); 00201 } 00202 00203 00204 /* 00205 * @implemented 00206 */ 00207 PSID_IDENTIFIER_AUTHORITY NTAPI 00208 RtlIdentifierAuthoritySid(IN PSID Sid_) 00209 { 00210 PISID Sid = Sid_; 00211 00212 PAGED_CODE_RTL(); 00213 00214 return &Sid->IdentifierAuthority; 00215 } 00216 00217 00218 /* 00219 * @implemented 00220 */ 00221 NTSTATUS NTAPI 00222 RtlAllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY IdentifierAuthority, 00223 UCHAR SubAuthorityCount, 00224 ULONG SubAuthority0, 00225 ULONG SubAuthority1, 00226 ULONG SubAuthority2, 00227 ULONG SubAuthority3, 00228 ULONG SubAuthority4, 00229 ULONG SubAuthority5, 00230 ULONG SubAuthority6, 00231 ULONG SubAuthority7, 00232 PSID *Sid) 00233 { 00234 PISID pSid; 00235 00236 PAGED_CODE_RTL(); 00237 00238 if (SubAuthorityCount > 8) 00239 return STATUS_INVALID_SID; 00240 00241 if (Sid == NULL) 00242 return STATUS_INVALID_PARAMETER; 00243 00244 pSid = RtlpAllocateMemory(RtlLengthRequiredSid(SubAuthorityCount), 00245 TAG_SID); 00246 if (pSid == NULL) 00247 return STATUS_NO_MEMORY; 00248 00249 pSid->Revision = SID_REVISION; 00250 pSid->SubAuthorityCount = SubAuthorityCount; 00251 memcpy(&pSid->IdentifierAuthority, 00252 IdentifierAuthority, 00253 sizeof(SID_IDENTIFIER_AUTHORITY)); 00254 00255 switch (SubAuthorityCount) 00256 { 00257 case 8: 00258 pSid->SubAuthority[7] = SubAuthority7; 00259 case 7: 00260 pSid->SubAuthority[6] = SubAuthority6; 00261 case 6: 00262 pSid->SubAuthority[5] = SubAuthority5; 00263 case 5: 00264 pSid->SubAuthority[4] = SubAuthority4; 00265 case 4: 00266 pSid->SubAuthority[3] = SubAuthority3; 00267 case 3: 00268 pSid->SubAuthority[2] = SubAuthority2; 00269 case 2: 00270 pSid->SubAuthority[1] = SubAuthority1; 00271 case 1: 00272 pSid->SubAuthority[0] = SubAuthority0; 00273 break; 00274 } 00275 00276 *Sid = pSid; 00277 00278 return STATUS_SUCCESS; 00279 } 00280 00281 00282 /* 00283 * @implemented 00284 * 00285 * RETURNS 00286 * Docs says FreeSid does NOT return a value 00287 * even thou it's defined to return a PVOID... 00288 */ 00289 PVOID NTAPI 00290 RtlFreeSid(IN PSID Sid) 00291 { 00292 PAGED_CODE_RTL(); 00293 00294 RtlpFreeMemory(Sid, TAG_SID); 00295 return NULL; 00296 } 00297 00298 00299 /* 00300 * @implemented 00301 */ 00302 BOOLEAN NTAPI 00303 RtlEqualPrefixSid(IN PSID Sid1_, 00304 IN PSID Sid2_) 00305 { 00306 PISID Sid1 = Sid1_; 00307 PISID Sid2 = Sid2_; 00308 SIZE_T SidLen; 00309 00310 PAGED_CODE_RTL(); 00311 00312 if (Sid1->SubAuthorityCount == Sid2->SubAuthorityCount) 00313 { 00314 SidLen = FIELD_OFFSET(SID, 00315 SubAuthority[Sid1->SubAuthorityCount]); 00316 return RtlCompareMemory(Sid1, 00317 Sid2, 00318 SidLen) == SidLen; 00319 } 00320 00321 return FALSE; 00322 } 00323 00324 00325 /* 00326 * @implemented 00327 */ 00328 NTSTATUS NTAPI 00329 RtlConvertSidToUnicodeString(PUNICODE_STRING String, 00330 PSID Sid_, 00331 BOOLEAN AllocateBuffer) 00332 { 00333 WCHAR Buffer[256]; 00334 PWSTR wcs; 00335 SIZE_T Length; 00336 ULONG i; 00337 PISID Sid = Sid_; 00338 00339 PAGED_CODE_RTL(); 00340 00341 if (RtlValidSid (Sid) == FALSE) 00342 return STATUS_INVALID_SID; 00343 00344 wcs = Buffer; 00345 wcs += swprintf (wcs, L"S-%u-", Sid->Revision); 00346 if (Sid->IdentifierAuthority.Value[0] == 0 && 00347 Sid->IdentifierAuthority.Value[1] == 0) 00348 { 00349 wcs += swprintf (wcs, 00350 L"%lu", 00351 (ULONG)Sid->IdentifierAuthority.Value[2] << 24 | 00352 (ULONG)Sid->IdentifierAuthority.Value[3] << 16 | 00353 (ULONG)Sid->IdentifierAuthority.Value[4] << 8 | 00354 (ULONG)Sid->IdentifierAuthority.Value[5]); 00355 } 00356 else 00357 { 00358 wcs += swprintf (wcs, 00359 L"0x%02hx%02hx%02hx%02hx%02hx%02hx", 00360 Sid->IdentifierAuthority.Value[0], 00361 Sid->IdentifierAuthority.Value[1], 00362 Sid->IdentifierAuthority.Value[2], 00363 Sid->IdentifierAuthority.Value[3], 00364 Sid->IdentifierAuthority.Value[4], 00365 Sid->IdentifierAuthority.Value[5]); 00366 } 00367 00368 for (i = 0; i < Sid->SubAuthorityCount; i++) 00369 { 00370 wcs += swprintf (wcs, 00371 L"-%u", 00372 Sid->SubAuthority[i]); 00373 } 00374 00375 if (AllocateBuffer) 00376 { 00377 if (!RtlCreateUnicodeString(String, 00378 Buffer)) 00379 { 00380 return STATUS_NO_MEMORY; 00381 } 00382 } 00383 else 00384 { 00385 Length = (wcs - Buffer) * sizeof(WCHAR); 00386 00387 if (Length > String->MaximumLength) 00388 return STATUS_BUFFER_TOO_SMALL; 00389 00390 String->Length = (USHORT)Length; 00391 RtlCopyMemory (String->Buffer, 00392 Buffer, 00393 Length); 00394 if (Length < String->MaximumLength) 00395 String->Buffer[Length / sizeof(WCHAR)] = 0; 00396 } 00397 00398 return STATUS_SUCCESS; 00399 } 00400 00401 /* EOF */ Generated on Sun May 27 2012 04:22:44 for ReactOS by
1.7.6.1
|