Thomas Weidenmueller <w3seek@reactos.com>
[reactos.git] / reactos / lib / rtl / sid.c
index 10427eb..1194d3a 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: sid.c,v 1.1 2004/05/31 19:29:02 gdalsnes Exp $
+/* $Id$
  *
  * COPYRIGHT:         See COPYING in the top level directory
  * PROJECT:           ReactOS kernel
@@ -13,6 +13,7 @@
 
 #define __NTDRIVER__
 #include <ddk/ntddk.h>
+#include <ntdll/rtl.h>
 #include <string.h>
 
 
 /* FUNCTIONS ***************************************************************/
 
 BOOLEAN STDCALL
-RtlValidSid(IN PSID Sid)
+RtlValidSid(IN PSID Sid_)
 {
-   if ((Sid->Revision & 0xf) != 1)
-   {
-      return(FALSE);
-   }
-   if (Sid->SubAuthorityCount > 15)
-   {
-      return(FALSE);
-   }
-   return(TRUE);
+  PISID Sid =  Sid_;
+  
+  PAGED_CODE_RTL();
+  
+  if ((Sid->Revision != SID_REVISION) ||
+      (Sid->SubAuthorityCount > SID_MAX_SUB_AUTHORITIES))
+    {
+      return FALSE;
+    }
+
+   return TRUE;
 }
 
 
@@ -42,7 +45,9 @@ RtlValidSid(IN PSID Sid)
 ULONG STDCALL
 RtlLengthRequiredSid(IN UCHAR SubAuthorityCount)
 {
-   return(sizeof(SID) + (SubAuthorityCount - 1) * sizeof(ULONG));
+  PAGED_CODE_RTL();
+  
+  return (sizeof(SID) + (SubAuthorityCount - 1) * sizeof(ULONG));
 }
 
 
@@ -50,16 +55,21 @@ RtlLengthRequiredSid(IN UCHAR SubAuthorityCount)
  * @implemented
  */
 NTSTATUS STDCALL
-RtlInitializeSid(IN PSID Sid,
+RtlInitializeSid(IN PSID Sid_,
                  IN PSID_IDENTIFIER_AUTHORITY IdentifierAuthority,
                  IN UCHAR SubAuthorityCount)
 {
-   Sid->Revision = 1;
-   Sid->SubAuthorityCount = SubAuthorityCount;
-   memcpy(&Sid->IdentifierAuthority,
-          IdentifierAuthority,
-          sizeof(SID_IDENTIFIER_AUTHORITY));
-   return(STATUS_SUCCESS);
+  PISID Sid =  Sid_;
+  
+  PAGED_CODE_RTL();
+  
+  Sid->Revision = SID_REVISION;
+  Sid->SubAuthorityCount = SubAuthorityCount;
+  memcpy(&Sid->IdentifierAuthority,
+         IdentifierAuthority,
+         sizeof(SID_IDENTIFIER_AUTHORITY));
+
+  return STATUS_SUCCESS;
 }
 
 
@@ -67,10 +77,14 @@ RtlInitializeSid(IN PSID Sid,
  * @implemented
  */
 PULONG STDCALL
-RtlSubAuthoritySid(IN PSID Sid,
+RtlSubAuthoritySid(IN PSID Sid_,
                    IN ULONG SubAuthority)
 {
-   return(&Sid->SubAuthority[SubAuthority]);
+  PISID Sid =  Sid_;
+  
+  PAGED_CODE_RTL();
+  
+  return &Sid->SubAuthority[SubAuthority];
 }
 
 
@@ -78,9 +92,13 @@ RtlSubAuthoritySid(IN PSID Sid,
  * @implemented
  */
 PUCHAR STDCALL
-RtlSubAuthorityCountSid(IN PSID Sid)
+RtlSubAuthorityCountSid(IN PSID Sid_)
 {
-   return(&Sid->SubAuthorityCount);
+  PISID Sid =  Sid_;
+  
+  PAGED_CODE_RTL();
+  
+  return &Sid->SubAuthorityCount;
 }
 
 
@@ -88,10 +106,15 @@ RtlSubAuthorityCountSid(IN PSID Sid)
  * @implemented
  */
 BOOLEAN STDCALL
-RtlEqualSid(IN PSID Sid1,
-            IN PSID Sid2)
+RtlEqualSid(IN PSID Sid1_,
+            IN PSID Sid2_)
 {
-   if (Sid1->Revision != Sid2->Revision)
+  PISID Sid1 =  Sid1_;
+  PISID Sid2 =  Sid2_;
+  
+  PAGED_CODE_RTL();
+    
+  if (Sid1->Revision != Sid2->Revision)
    {
       return(FALSE);
    }
@@ -99,7 +122,7 @@ RtlEqualSid(IN PSID Sid1,
    {
       return(FALSE);
    }
-   if (RtlCompareMemory(Sid1, Sid2, RtlLengthSid(Sid1)) != 0)
+   if (RtlCompareMemory(Sid1, Sid2, RtlLengthSid(Sid1)) != RtlLengthSid(Sid1))
    {
       return(FALSE);
    }
@@ -111,9 +134,13 @@ RtlEqualSid(IN PSID Sid1,
  * @implemented
  */
 ULONG STDCALL
-RtlLengthSid(IN PSID Sid)
+RtlLengthSid(IN PSID Sid_)
 {
-   return(sizeof(SID) + (Sid->SubAuthorityCount-1)*4);
+  PISID Sid =  Sid_;
+  
+  PAGED_CODE_RTL();
+  
+  return (sizeof(SID) + (Sid->SubAuthorityCount-1) * sizeof(ULONG));
 }
 
 
@@ -125,14 +152,18 @@ RtlCopySid(ULONG BufferLength,
            PSID Dest,
            PSID Src)
 {
-   if (BufferLength < RtlLengthSid(Src))
-   {
-      return(STATUS_UNSUCCESSFUL);
-   }
-   memmove(Dest,
-           Src,
-           RtlLengthSid(Src));
-   return(STATUS_SUCCESS);
+  PAGED_CODE_RTL();
+  
+  if (BufferLength < RtlLengthSid(Src))
+    {
+      return STATUS_UNSUCCESSFUL;
+    }
+
+  memmove(Dest,
+          Src,
+          RtlLengthSid(Src));
+
+  return STATUS_SUCCESS;
 }
 
 
@@ -151,6 +182,8 @@ RtlCopySidAndAttributesArray(ULONG Count,
    ULONG SidLength;
    ULONG Length;
    ULONG i;
+   
+   PAGED_CODE_RTL();
 
    Length = SidAreaSize;
 
@@ -167,7 +200,7 @@ RtlCopySidAndAttributesArray(ULONG Count,
       RtlCopySid(SidLength,
                  SidArea,
                  Src[i].Sid);
-      SidArea = SidArea + SidLength;
+      SidArea = (PVOID)((ULONG_PTR)SidArea + SidLength);
    }
    *RemainingSidArea = SidArea;
    *RemainingSidAreaSize = Length;
@@ -179,53 +212,55 @@ RtlCopySidAndAttributesArray(ULONG Count,
  * @implemented
  */
 PSID_IDENTIFIER_AUTHORITY STDCALL
-RtlIdentifierAuthoritySid(IN PSID Sid)
+RtlIdentifierAuthoritySid(IN PSID Sid_)
 {
-   return(&Sid->IdentifierAuthority);
+  PISID Sid =  Sid_;
+  
+  PAGED_CODE_RTL();
+  
+  return &Sid->IdentifierAuthority;
 }
 
 
 /*
  * @implemented
  */
-NTSTATUS
-STDCALL
-RtlAllocateAndInitializeSid (
-   PSID_IDENTIFIER_AUTHORITY IdentifierAuthority,
-   UCHAR    SubAuthorityCount,
-   ULONG    SubAuthority0,
-   ULONG    SubAuthority1,
-   ULONG    SubAuthority2,
-   ULONG    SubAuthority3,
-   ULONG    SubAuthority4,
-   ULONG    SubAuthority5,
-   ULONG    SubAuthority6,
-   ULONG    SubAuthority7,
-   PSID    *Sid
-)
+NTSTATUS STDCALL
+RtlAllocateAndInitializeSid(PSID_IDENTIFIER_AUTHORITY IdentifierAuthority,
+                           UCHAR SubAuthorityCount,
+                           ULONG SubAuthority0,
+                           ULONG SubAuthority1,
+                           ULONG SubAuthority2,
+                           ULONG SubAuthority3,
+                           ULONG SubAuthority4,
+                           ULONG SubAuthority5,
+                           ULONG SubAuthority6,
+                           ULONG SubAuthority7,
+                           PSID *Sid)
 {
-   PSID pSid;
+  PISID pSid;
+  
+  PAGED_CODE_RTL();
 
-   if (SubAuthorityCount > 8)
-      return STATUS_INVALID_SID;
+  if (SubAuthorityCount > 8)
+    return STATUS_INVALID_SID;
 
-   if (Sid == NULL)
-      return STATUS_INVALID_PARAMETER;
+  if (Sid == NULL)
+    return STATUS_INVALID_PARAMETER;
 
-   pSid = (PSID)RtlAllocateHeap (RtlGetProcessHeap (),
-                                 0,
-                                 SubAuthorityCount * sizeof(DWORD) + 8);
-   if (pSid == NULL)
-      return STATUS_NO_MEMORY;
+  pSid = (PSID)ExAllocatePool(PagedPool,
+                             sizeof(SID) + (SubAuthorityCount - 1) * sizeof(ULONG));
+  if (pSid == NULL)
+    return STATUS_NO_MEMORY;
 
-   pSid->Revision = 1;
-   pSid->SubAuthorityCount = SubAuthorityCount;
-   memcpy (&pSid->IdentifierAuthority,
-           IdentifierAuthority,
-           sizeof(SID_IDENTIFIER_AUTHORITY));
+  pSid->Revision = SID_REVISION;
+  pSid->SubAuthorityCount = SubAuthorityCount;
+  memcpy(&pSid->IdentifierAuthority,
+         IdentifierAuthority,
+         sizeof(SID_IDENTIFIER_AUTHORITY));
 
-   switch (SubAuthorityCount)
-   {
+  switch (SubAuthorityCount)
+    {
       case 8:
          pSid->SubAuthority[7] = SubAuthority7;
       case 7:
@@ -243,24 +278,28 @@ RtlAllocateAndInitializeSid (
       case 1:
          pSid->SubAuthority[0] = SubAuthority0;
          break;
-   }
+    }
 
-   *Sid = pSid;
+  *Sid = pSid;
 
-   return STATUS_SUCCESS;
+  return STATUS_SUCCESS;
 }
 
 
 /*
  * @implemented
+ *
+ * RETURNS
+ *  Docs says FreeSid does NOT return a value
+ *  even thou it's defined to return a PVOID...
  */
-PSID STDCALL
+PVOID STDCALL
 RtlFreeSid(IN PSID Sid)
 {
-   RtlFreeHeap(RtlGetProcessHeap(),
-               0,
-               Sid);
-   return(Sid);
+   PAGED_CODE_RTL();
+   
+   ExFreePool(Sid);
+   return NULL;
 }
 
 
@@ -268,9 +307,14 @@ RtlFreeSid(IN PSID Sid)
  * @implemented
  */
 BOOLEAN STDCALL
-RtlEqualPrefixSid(IN PSID Sid1,
-                  IN PSID Sid2)
+RtlEqualPrefixSid(IN PSID Sid1_,
+                  IN PSID Sid2_)
 {
+  PISID Sid1 =  Sid1_;
+  PISID Sid2 =  Sid2_;
+  
+  PAGED_CODE_RTL();
+    
    return(Sid1->SubAuthorityCount == Sid2->SubAuthorityCount &&
           !RtlCompareMemory(Sid1, Sid2,
                             (Sid1->SubAuthorityCount - 1) * sizeof(DWORD) + 8));
@@ -282,13 +326,16 @@ RtlEqualPrefixSid(IN PSID Sid1,
  */
 NTSTATUS STDCALL
 RtlConvertSidToUnicodeString(PUNICODE_STRING String,
-                             PSID Sid,
+                             PSID Sid_,
                              BOOLEAN AllocateBuffer)
 {
    WCHAR Buffer[256];
    PWSTR wcs;
    ULONG Length;
    ULONG i;
+   PISID Sid =  Sid_;
+   
+   PAGED_CODE_RTL();
 
    if (RtlValidSid (Sid) == FALSE)
       return STATUS_INVALID_SID;
@@ -327,9 +374,7 @@ RtlConvertSidToUnicodeString(PUNICODE_STRING String,
    Length = (wcs - Buffer) * sizeof(WCHAR);
    if (AllocateBuffer)
    {
-      String->Buffer = RtlAllocateHeap (RtlGetProcessHeap (),
-                                        0,
-                                        Length + sizeof(WCHAR));
+      String->Buffer = ExAllocatePool(PagedPool,Length + sizeof(WCHAR));
       if (String->Buffer == NULL)
          return STATUS_NO_MEMORY;
       String->MaximumLength = Length + sizeof(WCHAR);