[NTDLL]
[reactos.git] / subsystems / win32 / csrsrv / procsup.c
index 52a4427..3796145 100644 (file)
@@ -499,210 +499,262 @@ CsrInsertProcess(IN PCSR_PROCESS Parent OPTIONAL,
 /* PUBLIC FUNCTIONS ***********************************************************/
 
 /*++
- * @name CsrGetProcessLuid
+ * @name CsrCreateProcess
  * @implemented NT4
  *
  * Do nothing for 500ms.
  *
- * @param hProcess
- *        Optional handle to the process whose LUID should be returned.
+ * @param ArgumentCount
+ *        Description of the parameter. Wrapped to more lines on ~70th
+ *        column.
  *
- * @param Luid
- *        Pointer to a LUID Pointer which will receive the CSR Process' LUID
+ * @param Arguments
+ *        Description of the parameter. Wrapped to more lines on ~70th
+ *        column.
  *
  * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
  *         otherwise.
  *
- * @remarks If hProcess is not supplied, then the current thread's token will
- *          be used. If that too is missing, then the current process' token
- *          will be used.
+ * @remarks None.
  *
  *--*/
 NTSTATUS
 NTAPI
-CsrGetProcessLuid(HANDLE hProcess OPTIONAL,
-                  PLUID Luid)
+CsrCreateProcess(IN HANDLE hProcess,
+                 IN HANDLE hThread,
+                 IN PCLIENT_ID ClientId,
+                 IN PCSR_NT_SESSION NtSession,
+                 IN ULONG Flags,
+                 IN PCLIENT_ID DebugCid)
 {
-    HANDLE hToken = NULL;
+    PCSR_THREAD CurrentThread = CsrGetClientThread();
+    CLIENT_ID CurrentCid;
+    PCSR_PROCESS CurrentProcess;
+    PVOID ProcessData;
+    ULONG i;
+    PCSR_PROCESS CsrProcess;
     NTSTATUS Status;
-    ULONG Length;
-    PTOKEN_STATISTICS TokenStats;
+    PCSR_THREAD CsrThread;
+    KERNEL_USER_TIMES KernelTimes;
 
-    /* Check if we have a handle to a CSR Process */
-    if (!hProcess)
+    /* Get the current CID and lock Processes */
+    CurrentCid = CurrentThread->ClientId;
+    CsrAcquireProcessLock();
+
+    /* Get the current CSR Thread */
+    CurrentThread = CsrLocateThreadByClientId(&CurrentProcess, &CurrentCid);
+    if (!CurrentThread)
     {
-        /* We don't, so try opening the Thread's Token */
-        Status = NtOpenThreadToken(NtCurrentThread(),
-                                   TOKEN_QUERY,
-                                   FALSE,
-                                   &hToken);
+        /* We've failed to locate the thread */
+        CsrReleaseProcessLock();
+        return STATUS_THREAD_IS_TERMINATING;
+    }
 
-        /* Check for success */
-        if (!NT_SUCCESS(Status))
+    /* Allocate a new Process Object */
+    CsrProcess = CsrAllocateProcess();
+    if (!CsrProcess)
+    {
+        /* Couldn't allocate Process */
+        CsrReleaseProcessLock();
+        return STATUS_NO_MEMORY;
+    }
+
+    /* Inherit the Process Data */
+    CurrentProcess = CurrentThread->Process;
+    ProcessData = &CurrentProcess->ServerData[CSR_SERVER_DLL_MAX];
+    for (i = 0; i < CSR_SERVER_DLL_MAX; i++)
+    {
+        /* Check if the DLL is Loaded and has Per Process Data */
+        if ((CsrLoadedServerDll[i]) && (CsrLoadedServerDll[i]->SizeOfProcessData))
         {
-            /* If we got some other failure, then return and quit */
-            if (Status != STATUS_NO_TOKEN) return Status;
+            /* Set the pointer */
+            CsrProcess->ServerData[i] = ProcessData;
 
-            /* We don't have a Thread Token, use a Process Token */
-            hProcess = NtCurrentProcess();
-            hToken = NULL;
+            /* Copy the Data */
+            RtlMoveMemory(ProcessData,
+                          CurrentProcess->ServerData[i],
+                          CsrLoadedServerDll[i]->SizeOfProcessData);
+
+            /* Update next data pointer */
+            ProcessData = (PVOID)((ULONG_PTR)ProcessData +
+                                  CsrLoadedServerDll[i]->SizeOfProcessData);
+        }
+        else
+        {
+            /* No data for this Server */
+            CsrProcess->ServerData[i] = NULL;
         }
     }
 
-    /* Check if we have a token by now */
-    if (!hToken)
+    /* Set the Exception port for us */
+    Status = NtSetInformationProcess(hProcess,
+                                     ProcessExceptionPort,
+                                     &CsrApiPort,
+                                     sizeof(HANDLE));
+    if (!NT_SUCCESS(Status))
     {
-        /* No token yet, so open the Process Token */
-        Status = NtOpenProcessToken(hProcess,
-                                    TOKEN_QUERY,
-                                    &hToken);
+        /* Failed */
+        CsrDeallocateProcess(CsrProcess);
+        CsrReleaseProcessLock();
+        return STATUS_NO_MEMORY;
+    }
+
+    /* Check if CreateProcess got CREATE_NEW_PROCESS_GROUP */
+    if (!(Flags & CsrProcessCreateNewGroup))
+    {
+        /* Create new data */
+        CsrProcess->ProcessGroupId = HandleToUlong(ClientId->UniqueProcess);
+        CsrProcess->ProcessGroupSequence = CsrProcess->SequenceNumber;
+    }
+    else
+    {
+        /* Copy it from the current process */
+        CsrProcess->ProcessGroupId = CurrentProcess->ProcessGroupId;
+        CsrProcess->ProcessGroupSequence = CurrentProcess->ProcessGroupSequence;
+    }
+
+    /* Check if this is a console process */
+    if (Flags & CsrProcessIsConsoleApp) CsrProcess->Flags |= CsrProcessIsConsoleApp;
+
+    /* Mask out non-debug flags */
+    Flags &= ~(CsrProcessIsConsoleApp | CsrProcessCreateNewGroup | CsrProcessPriorityFlags);
+
+    /* Check if every process will be debugged */
+    if (!(Flags) && (CurrentProcess->DebugFlags & CsrDebugProcessChildren))
+    {
+        /* Pass it on to the current process */
+        CsrProcess->DebugFlags = CsrDebugProcessChildren;
+        CsrProcess->DebugCid = CurrentProcess->DebugCid;
+    }
+
+    /* Check if Debugging was used on this process */
+    if ((Flags & (CsrDebugOnlyThisProcess | CsrDebugProcessChildren)) && (DebugCid))
+    {
+        /* Save the debug flag used */
+        CsrProcess->DebugFlags = Flags;
+
+        /* Save the CID */
+        CsrProcess->DebugCid = *DebugCid;
+    }
+
+    /* Check if Debugging is enabled */
+    if (CsrProcess->DebugFlags)
+    {
+        /* Set the Debug Port for us */
+        Status = NtSetInformationProcess(hProcess,
+                                         ProcessDebugPort,
+                                         &CsrApiPort,
+                                         sizeof(HANDLE));
+        ASSERT(NT_SUCCESS(Status));
         if (!NT_SUCCESS(Status))
         {
-            /* Still no token, return the error */
-            return Status;
+            /* Failed */
+            CsrDeallocateProcess(CsrProcess);
+            CsrReleaseProcessLock();
+            return STATUS_NO_MEMORY;
         }
     }
 
-    /* Now get the size we'll need for the Token Information */
-    Status = NtQueryInformationToken(hToken,
-                                     TokenStatistics,
-                                     NULL,
-                                     0,
-                                     &Length);
+    /* Get the Thread Create Time */
+    Status = NtQueryInformationThread(hThread,
+                                      ThreadTimes,
+                                      (PVOID)&KernelTimes,
+                                      sizeof(KernelTimes),
+                                      NULL);
+    if (!NT_SUCCESS(Status))
+    {
+        /* Failed */
+        CsrDeallocateProcess(CsrProcess);
+        CsrReleaseProcessLock();
+        return STATUS_NO_MEMORY;
+    }
 
-    /* Allocate memory for the Token Info */
-    if (!(TokenStats = RtlAllocateHeap(CsrHeap, 0, Length)))
+    /* Allocate a CSR Thread Structure */
+    CsrThread = CsrAllocateThread(CsrProcess);
+    if (!CsrThread)
     {
-        /* Fail and close the token */
-        NtClose(hToken);
+        /* Failed */
+        CsrDeallocateProcess(CsrProcess);
+        CsrReleaseProcessLock();
         return STATUS_NO_MEMORY;
     }
 
-    /* Now query the information */
-    Status = NtQueryInformationToken(hToken,
-                                     TokenStatistics,
-                                     TokenStats,
-                                     Length,
-                                     &Length);
+    /* Save the data we have */
+    CsrThread->CreateTime = KernelTimes.CreateTime;
+    CsrThread->ClientId = *ClientId;
+    CsrThread->ThreadHandle = hThread;
+    ProtectHandle(hThread);
+    CsrThread->Flags = 0;
 
-    /* Close the handle */
-    NtClose(hToken);
+    /* Insert the Thread into the Process */
+    CsrInsertThread(CsrProcess, CsrThread);
 
-    /* Check for success */
-    if (NT_SUCCESS(Status))
-    {
-        /* Return the LUID */
-        *Luid = TokenStats->AuthenticationId;
-    }
+    /* Reference the session */
+    CsrReferenceNtSession(NtSession);
+    CsrProcess->NtSession = NtSession;
 
-    /* Free the query information */
-    RtlFreeHeap(CsrHeap, 0, TokenStats);
+    /* Setup Process Data */
+    CsrProcess->ClientId = *ClientId;
+    CsrProcess->ProcessHandle = hProcess;
+    CsrProcess->ShutdownLevel = 0x280;
 
-    /* Return the Status */
+    /* Set the Priority to Background */
+    CsrSetBackgroundPriority(CsrProcess);
+
+    /* Insert the Process */
+    CsrInsertProcess(NULL, CurrentProcess, CsrProcess);
+
+    /* Release lock and return */
+    CsrReleaseProcessLock();
     return Status;
 }
 
 /*++
- * @name CsrImpersonateClient
+ * @name CsrDebugProcess
  * @implemented NT4
  *
- * The CsrImpersonateClient will impersonate the given CSR Thread.
+ * The CsrDebugProcess routine is deprecated in NT 5.1 and higher. It is
+ * exported only for compatibility with older CSR Server DLLs.
  *
- * @param CsrThread
- *        Pointer to the CSR Thread to impersonate.
+ * @param CsrProcess
+ *        Deprecated.
  *
- * @return TRUE if impersionation suceeded, false otherwise.
+ * @return Deprecated
  *
- * @remarks Impersonation can be recursive.
+ * @remarks Deprecated.
  *
  *--*/
-BOOLEAN
+NTSTATUS
 NTAPI
-CsrImpersonateClient(IN PCSR_THREAD CsrThread)
+CsrDebugProcess(IN PCSR_PROCESS CsrProcess)
 {
-    NTSTATUS Status;
-    PCSR_THREAD CurrentThread = CsrGetClientThread();
-
-    /* Use the current thread if none given */
-    if (!CsrThread) CsrThread = CurrentThread;
-
-    /* Still no thread, something is wrong */
-    if (!CsrThread)
-    {
-        /* Failure */
-        return FALSE;
-    }
-
-    /* Make the call */
-    Status = NtImpersonateThread(NtCurrentThread(),
-                                 CsrThread->ThreadHandle,
-                                 &CsrSecurityQos);
-
-    if (!NT_SUCCESS(Status))
-    {
-        /* Failure */
-/*
-        DPRINT1("CSRSS: Can't impersonate client thread - Status = %lx\n", Status);
-        if (Status != STATUS_BAD_IMPERSONATION_LEVEL) DbgBreakPoint();
-*/
-        return FALSE;
-    }
-
-    /* Increase the impersonation count for the current thread */
-    if (CurrentThread) ++CurrentThread->ImpersonationCount;
-
-    /* Return Success */
-    return TRUE;
+    /* CSR does not handle debugging anymore */
+    DPRINT("CSRSRV: %s(%08lx) called\n", __FUNCTION__, CsrProcess);
+    return STATUS_UNSUCCESSFUL;
 }
 
 /*++
- * @name CsrRevertToSelf
+ * @name CsrDebugProcessStop
  * @implemented NT4
  *
- * The CsrRevertToSelf routine will attempt to remove an active impersonation.
+ * The CsrDebugProcessStop routine is deprecated in NT 5.1 and higher. It is
+ * exported only for compatibility with older CSR Server DLLs.
  *
- * @param None.
+ * @param CsrProcess
+ *        Deprecated.
  *
- * @return TRUE if the reversion was succesful, false otherwise.
+ * @return Deprecated
  *
- * @remarks Impersonation can be recursive; as such, the impersonation token
- *          will only be deleted once the CSR Thread's impersonaton count
- *          has reached zero.
+ * @remarks Deprecated.
  *
  *--*/
-BOOLEAN
-NTAPI
-CsrRevertToSelf(VOID)
-{
-    NTSTATUS Status;
-    PCSR_THREAD CurrentThread = CsrGetClientThread();
-    HANDLE ImpersonationToken = NULL;
-
-    /* Check if we have a Current Thread */
-    if (CurrentThread)
-    {
-        /* Make sure impersonation is on */
-        if (!CurrentThread->ImpersonationCount)
-        {
-            // DPRINT1("CSRSS: CsrRevertToSelf called while not impersonating\n");
-            // DbgBreakPoint();
-            return FALSE;
-        }
-        else if (--CurrentThread->ImpersonationCount > 0)
-        {
-            /* Success; impersonation count decreased but still not zero */
-            return TRUE;
-        }
-    }
-
-    /* Impersonation has been totally removed, revert to ourselves */
-    Status = NtSetInformationThread(NtCurrentThread(),
-                                    ThreadImpersonationToken,
-                                    &ImpersonationToken,
-                                    sizeof(HANDLE));
-
-    /* Return TRUE or FALSE */
-    return NT_SUCCESS(Status);
+NTSTATUS
+NTAPI
+CsrDebugProcessStop(IN PCSR_PROCESS CsrProcess)
+{
+    /* CSR does not handle debugging anymore */
+    DPRINT("CSRSRV: %s(%08lx) called\n", __FUNCTION__, CsrProcess);
+    return STATUS_UNSUCCESSFUL;
 }
 
 /*++
@@ -837,271 +889,160 @@ CsrDestroyProcess(IN PCLIENT_ID Cid,
 }
 
 /*++
- * @name CsrCreateProcess
+ * @name CsrGetProcessLuid
  * @implemented NT4
  *
  * Do nothing for 500ms.
  *
- * @param ArgumentCount
- *        Description of the parameter. Wrapped to more lines on ~70th
- *        column.
+ * @param hProcess
+ *        Optional handle to the process whose LUID should be returned.
  *
- * @param Arguments
- *        Description of the parameter. Wrapped to more lines on ~70th
- *        column.
+ * @param Luid
+ *        Pointer to a LUID Pointer which will receive the CSR Process' LUID
  *
  * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
  *         otherwise.
  *
- * @remarks None.
+ * @remarks If hProcess is not supplied, then the current thread's token will
+ *          be used. If that too is missing, then the current process' token
+ *          will be used.
  *
  *--*/
 NTSTATUS
 NTAPI
-CsrCreateProcess(IN HANDLE hProcess,
-                 IN HANDLE hThread,
-                 IN PCLIENT_ID ClientId,
-                 IN PCSR_NT_SESSION NtSession,
-                 IN ULONG Flags,
-                 IN PCLIENT_ID DebugCid)
+CsrGetProcessLuid(IN HANDLE hProcess OPTIONAL,
+                  OUT PLUID Luid)
 {
-    PCSR_THREAD CurrentThread = CsrGetClientThread();
-    CLIENT_ID CurrentCid;
-    PCSR_PROCESS CurrentProcess;
-    PVOID ProcessData;
-    ULONG i;
-    PCSR_PROCESS CsrProcess;
+    HANDLE hToken = NULL;
     NTSTATUS Status;
-    PCSR_THREAD CsrThread;
-    KERNEL_USER_TIMES KernelTimes;
-
-    /* Get the current CID and lock Processes */
-    CurrentCid = CurrentThread->ClientId;
-    CsrAcquireProcessLock();
-
-    /* Get the current CSR Thread */
-    CurrentThread = CsrLocateThreadByClientId(&CurrentProcess, &CurrentCid);
-    if (!CurrentThread)
-    {
-        /* We've failed to locate the thread */
-        CsrReleaseProcessLock();
-        return STATUS_THREAD_IS_TERMINATING;
-    }
+    ULONG Length;
+    PTOKEN_STATISTICS TokenStats;
 
-    /* Allocate a new Process Object */
-    CsrProcess = CsrAllocateProcess();
-    if (!CsrProcess)
+    /* Check if we have a handle to a CSR Process */
+    if (!hProcess)
     {
-        /* Couldn't allocate Process */
-        CsrReleaseProcessLock();
-        return STATUS_NO_MEMORY;
-    }
+        /* We don't, so try opening the Thread's Token */
+        Status = NtOpenThreadToken(NtCurrentThread(),
+                                   TOKEN_QUERY,
+                                   FALSE,
+                                   &hToken);
 
-    /* Inherit the Process Data */
-    CurrentProcess = CurrentThread->Process;
-    ProcessData = &CurrentProcess->ServerData[CSR_SERVER_DLL_MAX];
-    for (i = 0; i < CSR_SERVER_DLL_MAX; i++)
-    {
-        /* Check if the DLL is Loaded and has Per Process Data */
-        if ((CsrLoadedServerDll[i]) && (CsrLoadedServerDll[i]->SizeOfProcessData))
+        /* Check for success */
+        if (!NT_SUCCESS(Status))
         {
-            /* Set the pointer */
-            CsrProcess->ServerData[i] = ProcessData;
-
-            /* Copy the Data */
-            RtlMoveMemory(ProcessData,
-                          CurrentProcess->ServerData[i],
-                          CsrLoadedServerDll[i]->SizeOfProcessData);
+            /* If we got some other failure, then return and quit */
+            if (Status != STATUS_NO_TOKEN) return Status;
 
-            /* Update next data pointer */
-            ProcessData = (PVOID)((ULONG_PTR)ProcessData +
-                                  CsrLoadedServerDll[i]->SizeOfProcessData);
-        }
-        else
-        {
-            /* No data for this Server */
-            CsrProcess->ServerData[i] = NULL;
+            /* We don't have a Thread Token, use a Process Token */
+            hProcess = NtCurrentProcess();
+            hToken = NULL;
         }
     }
 
-    /* Set the Exception port for us */
-    Status = NtSetInformationProcess(hProcess,
-                                     ProcessExceptionPort,
-                                     &CsrApiPort,
-                                     sizeof(HANDLE));
-    if (!NT_SUCCESS(Status))
-    {
-        /* Failed */
-        CsrDeallocateProcess(CsrProcess);
-        CsrReleaseProcessLock();
-        return STATUS_NO_MEMORY;
-    }
-
-    /* Check if CreateProcess got CREATE_NEW_PROCESS_GROUP */
-    if (!(Flags & CsrProcessCreateNewGroup))
-    {
-        /* Create new data */
-        CsrProcess->ProcessGroupId = HandleToUlong(ClientId->UniqueProcess);
-        CsrProcess->ProcessGroupSequence = CsrProcess->SequenceNumber;
-    }
-    else
-    {
-        /* Copy it from the current process */
-        CsrProcess->ProcessGroupId = CurrentProcess->ProcessGroupId;
-        CsrProcess->ProcessGroupSequence = CurrentProcess->ProcessGroupSequence;
-    }
-
-    /* Check if this is a console process */
-    if (Flags & CsrProcessIsConsoleApp) CsrProcess->Flags |= CsrProcessIsConsoleApp;
-
-    /* Mask out non-debug flags */
-    Flags &= ~(CsrProcessIsConsoleApp | CsrProcessCreateNewGroup | CsrProcessPriorityFlags);
-
-    /* Check if every process will be debugged */
-    if (!(Flags) && (CurrentProcess->DebugFlags & CsrDebugProcessChildren))
-    {
-        /* Pass it on to the current process */
-        CsrProcess->DebugFlags = CsrDebugProcessChildren;
-        CsrProcess->DebugCid = CurrentProcess->DebugCid;
-    }
-
-    /* Check if Debugging was used on this process */
-    if ((Flags & (CsrDebugOnlyThisProcess | CsrDebugProcessChildren)) && (DebugCid))
-    {
-        /* Save the debug flag used */
-        CsrProcess->DebugFlags = Flags;
-
-        /* Save the CID */
-        CsrProcess->DebugCid = *DebugCid;
-    }
-
-    /* Check if Debugging is enabled */
-    if (CsrProcess->DebugFlags)
+    /* Check if we have a token by now */
+    if (!hToken)
     {
-        /* Set the Debug Port for us */
-        Status = NtSetInformationProcess(hProcess,
-                                         ProcessDebugPort,
-                                         &CsrApiPort,
-                                         sizeof(HANDLE));
-        ASSERT(NT_SUCCESS(Status));
+        /* No token yet, so open the Process Token */
+        Status = NtOpenProcessToken(hProcess,
+                                    TOKEN_QUERY,
+                                    &hToken);
         if (!NT_SUCCESS(Status))
         {
-            /* Failed */
-            CsrDeallocateProcess(CsrProcess);
-            CsrReleaseProcessLock();
-            return STATUS_NO_MEMORY;
+            /* Still no token, return the error */
+            return Status;
         }
     }
 
-    /* Get the Thread Create Time */
-    Status = NtQueryInformationThread(hThread,
-                                      ThreadTimes,
-                                      (PVOID)&KernelTimes,
-                                      sizeof(KernelTimes),
-                                      NULL);
-    if (!NT_SUCCESS(Status))
-    {
-        /* Failed */
-        CsrDeallocateProcess(CsrProcess);
-        CsrReleaseProcessLock();
-        return STATUS_NO_MEMORY;
-    }
+    /* Now get the size we'll need for the Token Information */
+    Status = NtQueryInformationToken(hToken,
+                                     TokenStatistics,
+                                     NULL,
+                                     0,
+                                     &Length);
 
-    /* Allocate a CSR Thread Structure */
-    CsrThread = CsrAllocateThread(CsrProcess);
-    if (!CsrThread)
+    /* Allocate memory for the Token Info */
+    if (!(TokenStats = RtlAllocateHeap(CsrHeap, 0, Length)))
     {
-        /* Failed */
-        CsrDeallocateProcess(CsrProcess);
-        CsrReleaseProcessLock();
+        /* Fail and close the token */
+        NtClose(hToken);
         return STATUS_NO_MEMORY;
     }
 
-    /* Save the data we have */
-    CsrThread->CreateTime = KernelTimes.CreateTime;
-    CsrThread->ClientId = *ClientId;
-    CsrThread->ThreadHandle = hThread;
-    ProtectHandle(hThread);
-    CsrThread->Flags = 0;
-
-    /* Insert the Thread into the Process */
-    CsrInsertThread(CsrProcess, CsrThread);
-
-    /* Reference the session */
-    CsrReferenceNtSession(NtSession);
-    CsrProcess->NtSession = NtSession;
-
-    /* Setup Process Data */
-    CsrProcess->ClientId = *ClientId;
-    CsrProcess->ProcessHandle = hProcess;
-    CsrProcess->ShutdownLevel = 0x280;
-
-    /* Set the Priority to Background */
-    CsrSetBackgroundPriority(CsrProcess);
-
-    /* Insert the Process */
-    CsrInsertProcess(NULL, CurrentProcess, CsrProcess);
+    /* Now query the information */
+    Status = NtQueryInformationToken(hToken,
+                                     TokenStatistics,
+                                     TokenStats,
+                                     Length,
+                                     &Length);
 
-    /* Release lock and return */
-    CsrReleaseProcessLock();
-    return Status;
-}
+    /* Close the handle */
+    NtClose(hToken);
 
-/*++
- * @name CsrUnlockProcess
- * @implemented NT4
- *
- * The CsrUnlockProcess undoes a previous CsrLockProcessByClientId operation.
- *
- * @param CsrProcess
- *        Pointer to a previously locked CSR Process.
- *
- * @return STATUS_SUCCESS.
- *
- * @remarks This routine must be called with the Process Lock held.
- *
- *--*/
-NTSTATUS
-NTAPI
-CsrUnlockProcess(IN PCSR_PROCESS CsrProcess)
-{
-    /* Dereference the process */
-    CsrLockedDereferenceProcess(CsrProcess);
+    /* Check for success */
+    if (NT_SUCCESS(Status))
+    {
+        /* Return the LUID */
+        *Luid = TokenStats->AuthenticationId;
+    }
 
-    /* Release the lock and return */
-    CsrReleaseProcessLock();
-    return STATUS_SUCCESS;
+    /* Free the query information */
+    RtlFreeHeap(CsrHeap, 0, TokenStats);
+
+    /* Return the Status */
+    return Status;
 }
 
 /*++
- * @name CsrSetBackgroundPriority
+ * @name CsrImpersonateClient
  * @implemented NT4
  *
- * The CsrSetBackgroundPriority routine sets the priority for the given CSR
- * Process as a Background priority.
+ * The CsrImpersonateClient will impersonate the given CSR Thread.
  *
- * @param CsrProcess
- *        Pointer to the CSR Process whose priority will be modified.
+ * @param CsrThread
+ *        Pointer to the CSR Thread to impersonate.
  *
- * @return None.
+ * @return TRUE if impersionation suceeded, false otherwise.
  *
- * @remarks None.
+ * @remarks Impersonation can be recursive.
  *
  *--*/
-VOID
+BOOLEAN
 NTAPI
-CsrSetBackgroundPriority(IN PCSR_PROCESS CsrProcess)
+CsrImpersonateClient(IN PCSR_THREAD CsrThread)
 {
-    PROCESS_PRIORITY_CLASS PriorityClass;
+    NTSTATUS Status;
+    PCSR_THREAD CurrentThread = CsrGetClientThread();
 
-    /* Set the Foreground bit off */
-    PriorityClass.Foreground = FALSE;
+    /* Use the current thread if none given */
+    if (!CsrThread) CsrThread = CurrentThread;
 
-    /* Set the new Priority */
-    NtSetInformationProcess(CsrProcess->ProcessHandle,
-                            ProcessPriorityClass,
-                            &PriorityClass,
-                            sizeof(PriorityClass));
+    /* Still no thread, something is wrong */
+    if (!CsrThread)
+    {
+        /* Failure */
+        return FALSE;
+    }
+
+    /* Make the call */
+    Status = NtImpersonateThread(NtCurrentThread(),
+                                 CsrThread->ThreadHandle,
+                                 &CsrSecurityQos);
+
+    if (!NT_SUCCESS(Status))
+    {
+        /* Failure */
+/*
+        DPRINT1("CSRSS: Can't impersonate client thread - Status = %lx\n", Status);
+        if (Status != STATUS_BAD_IMPERSONATION_LEVEL) DbgBreakPoint();
+*/
+        return FALSE;
+    }
+
+    /* Increase the impersonation count for the current thread */
+    if (CurrentThread) ++CurrentThread->ImpersonationCount;
+
+    /* Return Success */
+    return TRUE;
 }
 
 /*++
@@ -1176,6 +1117,118 @@ CsrLockProcessByClientId(IN HANDLE Pid,
     return Status;
 }
 
+/*++
+ * @name CsrRevertToSelf
+ * @implemented NT4
+ *
+ * The CsrRevertToSelf routine will attempt to remove an active impersonation.
+ *
+ * @param None.
+ *
+ * @return TRUE if the reversion was succesful, false otherwise.
+ *
+ * @remarks Impersonation can be recursive; as such, the impersonation token
+ *          will only be deleted once the CSR Thread's impersonaton count
+ *          has reached zero.
+ *
+ *--*/
+BOOLEAN
+NTAPI
+CsrRevertToSelf(VOID)
+{
+    NTSTATUS Status;
+    PCSR_THREAD CurrentThread = CsrGetClientThread();
+    HANDLE ImpersonationToken = NULL;
+
+    /* Check if we have a Current Thread */
+    if (CurrentThread)
+    {
+        /* Make sure impersonation is on */
+        if (!CurrentThread->ImpersonationCount)
+        {
+            // DPRINT1("CSRSS: CsrRevertToSelf called while not impersonating\n");
+            // DbgBreakPoint();
+            return FALSE;
+        }
+        else if (--CurrentThread->ImpersonationCount > 0)
+        {
+            /* Success; impersonation count decreased but still not zero */
+            return TRUE;
+        }
+    }
+
+    /* Impersonation has been totally removed, revert to ourselves */
+    Status = NtSetInformationThread(NtCurrentThread(),
+                                    ThreadImpersonationToken,
+                                    &ImpersonationToken,
+                                    sizeof(HANDLE));
+
+    /* Return TRUE or FALSE */
+    return NT_SUCCESS(Status);
+}
+
+/*++
+ * @name CsrSetBackgroundPriority
+ * @implemented NT4
+ *
+ * The CsrSetBackgroundPriority routine sets the priority for the given CSR
+ * Process as a Background priority.
+ *
+ * @param CsrProcess
+ *        Pointer to the CSR Process whose priority will be modified.
+ *
+ * @return None.
+ *
+ * @remarks None.
+ *
+ *--*/
+VOID
+NTAPI
+CsrSetBackgroundPriority(IN PCSR_PROCESS CsrProcess)
+{
+    PROCESS_PRIORITY_CLASS PriorityClass;
+
+    /* Set the Foreground bit off */
+    PriorityClass.Foreground = FALSE;
+
+    /* Set the new Priority */
+    NtSetInformationProcess(CsrProcess->ProcessHandle,
+                            ProcessPriorityClass,
+                            &PriorityClass,
+                            sizeof(PriorityClass));
+}
+
+/*++
+ * @name CsrSetForegroundPriority
+ * @implemented NT4
+ *
+ * The CsrSetForegroundPriority routine sets the priority for the given CSR
+ * Process as a Foreground priority.
+ *
+ * @param CsrProcess
+ *        Pointer to the CSR Process whose priority will be modified.
+ *
+ * @return None.
+ *
+ * @remarks None.
+ *
+ *--*/
+VOID
+NTAPI
+CsrSetForegroundPriority(IN PCSR_PROCESS CsrProcess)
+{
+    PROCESS_PRIORITY_CLASS PriorityClass;
+
+    /* Set the Foreground bit on */
+    PriorityClass.Foreground = TRUE;
+
+    /* Set the new Priority */
+    NtSetInformationProcess(CsrProcess->ProcessHandle,
+                            ProcessPriorityClass,
+                            &PriorityClass,
+                            sizeof(PriorityClass));
+}
+
 /*++
  * @name CsrShutdownProcesses
  * @implemented NT4
@@ -1231,7 +1284,8 @@ CsrShutdownProcesses(IN PLUID CallerLuid,
     }
 
     /* Set shudown Priority */
-    CsrpSetToShutdownPriority();
+    // CsrpSetToShutdownPriority();
+    CsrSetToShutdownPriority();
 
     /* Start looping */
     while (TRUE)
@@ -1305,11 +1359,14 @@ CsrShutdownProcesses(IN PLUID CallerLuid,
 
 Quickie:
     /* Return to normal priority */
-    CsrpSetToNormalPriority();
+    // CsrpSetToNormalPriority();
+    CsrSetToNormalPriority();
+
     return Status;
 }
 
 /* FIXME: Temporary hack. This is really "CsrShutdownProcess", mostly. Used by win32csr */
+#if 0
 NTSTATUS
 WINAPI
 CsrEnumProcesses(IN CSRSS_ENUM_PROCESS_PROC EnumProc,
@@ -1400,84 +1457,32 @@ Quickie:
     CsrSetToNormalPriority();
     return Status;
 }
+#endif
 
 /*++
- * @name CsrDebugProcess
- * @implemented NT4
- *
- * The CsrDebugProcess routine is deprecated in NT 5.1 and higher. It is
- * exported only for compatibility with older CSR Server DLLs.
- *
- * @param CsrProcess
- *        Deprecated.
- *
- * @return Deprecated
- *
- * @remarks Deprecated.
- *
- *--*/
-NTSTATUS
-NTAPI
-CsrDebugProcess(IN PCSR_PROCESS CsrProcess)
-{
-    /* CSR does not handle debugging anymore */
-    DPRINT("CSRSRV: %s(%08lx) called\n", __FUNCTION__, CsrProcess);
-    return STATUS_UNSUCCESSFUL;
-}
-
-/*++
- * @name CsrDebugProcessStop
+ * @name CsrUnlockProcess
  * @implemented NT4
  *
- * The CsrDebugProcessStop routine is deprecated in NT 5.1 and higher. It is
- * exported only for compatibility with older CSR Server DLLs.
+ * The CsrUnlockProcess undoes a previous CsrLockProcessByClientId operation.
  *
  * @param CsrProcess
- *        Deprecated.
+ *        Pointer to a previously locked CSR Process.
  *
- * @return Deprecated
+ * @return STATUS_SUCCESS.
  *
- * @remarks Deprecated.
+ * @remarks This routine must be called with the Process Lock held.
  *
  *--*/
 NTSTATUS
 NTAPI
-CsrDebugProcessStop(IN PCSR_PROCESS CsrProcess)
-{
-    /* CSR does not handle debugging anymore */
-    DPRINT("CSRSRV: %s(%08lx) called\n", __FUNCTION__, CsrProcess);
-    return STATUS_UNSUCCESSFUL;
-}
-
-/*++
- * @name CsrSetForegroundPriority
- * @implemented NT4
- *
- * The CsrSetForegroundPriority routine sets the priority for the given CSR
- * Process as a Foreground priority.
- *
- * @param CsrProcess
- *        Pointer to the CSR Process whose priority will be modified.
- *
- * @return None.
- *
- * @remarks None.
- *
- *--*/
-VOID
-NTAPI
-CsrSetForegroundPriority(IN PCSR_PROCESS CsrProcess)
+CsrUnlockProcess(IN PCSR_PROCESS CsrProcess)
 {
-    PROCESS_PRIORITY_CLASS PriorityClass;
-
-    /* Set the Foreground bit on */
-    PriorityClass.Foreground = TRUE;
+    /* Dereference the process */
+    CsrLockedDereferenceProcess(CsrProcess);
 
-    /* Set the new Priority */
-    NtSetInformationProcess(CsrProcess->ProcessHandle,
-                            ProcessPriorityClass,
-                            &PriorityClass,
-                            sizeof(PriorityClass));
+    /* Release the lock and return */
+    CsrReleaseProcessLock();
+    return STATUS_SUCCESS;
 }
 
 /* EOF */