update for HEAD-2003091401
[reactos.git] / lib / ntdll / rtl / security.c
index a41a793..149940e 100644 (file)
 #include <ddk/ntddk.h>
 #include <ntdll/rtl.h>
 
+#define NDEBUG
+#include <ntdll/ntdll.h>
 
+
+/* FUNCTIONS ****************************************************************/
+
+/*
+ * @implemented
+ */
 NTSTATUS STDCALL
 RtlImpersonateSelf(IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
 {
@@ -62,4 +70,81 @@ RtlImpersonateSelf(IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
   return(Status);
 }
 
+
+/*
+ * @implemented
+ */
+NTSTATUS STDCALL
+RtlAdjustPrivilege(IN ULONG Privilege,
+                  IN BOOLEAN Enable,
+                  IN BOOLEAN CurrentThread,
+                  OUT PBOOLEAN Enabled)
+{
+  TOKEN_PRIVILEGES NewState;
+  TOKEN_PRIVILEGES OldState;
+  ULONG ReturnLength;
+  HANDLE TokenHandle;
+  NTSTATUS Status;
+
+  DPRINT ("RtlAdjustPrivilege() called\n");
+
+  if (CurrentThread)
+    {
+      Status = NtOpenThreadToken (NtCurrentThread (),
+                                 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
+                                 FALSE,
+                                 &TokenHandle);
+    }
+  else
+    {
+      Status = NtOpenProcessToken (NtCurrentProcess (),
+                                  TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
+                                  &TokenHandle);
+    }
+
+  if (!NT_SUCCESS (Status))
+    {
+      DPRINT1 ("Retrieving token handle failed (Status %lx)\n", Status);
+      return Status;
+    }
+
+  OldState.PrivilegeCount = 1;
+
+  NewState.PrivilegeCount = 1;
+  NewState.Privileges[0].Luid.LowPart = Privilege;
+  NewState.Privileges[0].Luid.HighPart = 0;
+  NewState.Privileges[0].Attributes = (Enable) ? SE_PRIVILEGE_ENABLED : 0;
+
+  Status = NtAdjustPrivilegesToken (TokenHandle,
+                                   FALSE,
+                                   &NewState,
+                                   sizeof(TOKEN_PRIVILEGES),
+                                   &OldState,
+                                   &ReturnLength);
+  NtClose (TokenHandle);
+  if (Status == STATUS_NOT_ALL_ASSIGNED)
+    {
+      DPRINT1 ("Failed to assign all privileges\n");
+      return STATUS_PRIVILEGE_NOT_HELD;
+    }
+  if (!NT_SUCCESS(Status))
+    {
+      DPRINT1 ("NtAdjustPrivilegesToken() failed (Status %lx)\n", Status);
+      return Status;
+    }
+
+  if (OldState.PrivilegeCount == 0)
+    {
+      *Enabled = Enable;
+    }
+  else
+    {
+      *Enabled = (OldState.Privileges[0].Attributes & SE_PRIVILEGE_ENABLED);
+    }
+
+  DPRINT ("RtlAdjustPrivilege() done\n");
+
+  return STATUS_SUCCESS;
+}
+
 /* EOF */