Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.5 KiB

  1. /*
  2. Copyright (c) 1992,1993 Microsoft Corporation
  3. Module Name:
  4. psutl.c
  5. Abstract:
  6. This module has a utility function which uses an NT call to set the access
  7. token of a thread.
  8. Author:
  9. James Bratsanos <v-jimbr@microsoft.com or mcrafts!jamesb>
  10. Revision History:
  11. 05 May 1993 Added duplicate code and open token without imporsonation
  12. 06 Dec 1992 Initial
  13. Notes: Tab stop: 4
  14. --*/
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include <windows.h>
  19. #include "debug.h"
  20. /*** PsUtlSetThreadToken
  21. *
  22. * This function takes a handle to a thread and copies the current threads
  23. * access token, to the thread passed in. The token is duplicated so
  24. * no changes by the new thread affect the old thread access token.
  25. *
  26. * Entry:
  27. * hThreadToSet: Handle of the thread to update the Access token so it
  28. * matches the current thread
  29. * Return Value:
  30. *
  31. * TRUE = Success
  32. * FALSE = Failure
  33. */
  34. BOOL PsUtlSetThreadToken( HANDLE hThreadToSet )
  35. {
  36. HANDLE hNewToken;
  37. HANDLE hAssigned;
  38. BOOL bRetVal=TRUE;
  39. NTSTATUS ntStatusRet;
  40. //
  41. // Get the access token of the current thread in such a way as to copy it
  42. //
  43. if (OpenThreadToken(GetCurrentThread(),
  44. TOKEN_DUPLICATE | TOKEN_IMPERSONATE,
  45. TRUE,
  46. &hNewToken)) {
  47. //
  48. // Now that we have the Token lets copy it.
  49. //
  50. if (DuplicateToken( hNewToken, SecurityImpersonation, &hAssigned)) {
  51. //
  52. // At the time there was no exposed function in Win32 to do this.
  53. //
  54. ntStatusRet = NtSetInformationThread(hThreadToSet,
  55. ThreadImpersonationToken,
  56. &hAssigned,
  57. sizeof(hAssigned));
  58. //
  59. // Close off the handle since we dont need it anymore
  60. //
  61. CloseHandle( hAssigned);
  62. if (!(bRetVal = ( ntStatusRet == STATUS_SUCCESS ))) {
  63. DBGOUT((TEXT("NtSetInformationThread failed, psprint")));
  64. }
  65. } else {
  66. bRetVal = FALSE;
  67. DBGOUT((TEXT("Duplicate Token Fails %d"),GetLastError()));
  68. }
  69. CloseHandle(hNewToken);
  70. }else{
  71. DBGOUT((TEXT("OpenThreadTokenFailed %d"),GetLastError()));
  72. bRetVal = FALSE;
  73. }
  74. return(bRetVal);
  75. }
  76.