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.

126 lines
4.0 KiB

  1. // --------------------------------------------------------------------------
  2. // Module Name: TokenUtil.cpp
  3. //
  4. // Copyright (c) 1999-2000, Microsoft Corporation
  5. //
  6. // Functions that are useful for token manipulation.
  7. //
  8. // History: 1999-08-18 vtan created
  9. // 1999-11-16 vtan separate file
  10. // 2000-02-01 vtan moved from Neptune to Whistler
  11. // 2000-03-31 vtan duplicated from ds to shell
  12. // --------------------------------------------------------------------------
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. #include <windows.h>
  17. #include "TokenUtil.h"
  18. // --------------------------------------------------------------------------
  19. // ::OpenEffectiveToken
  20. //
  21. // Arguments: dwDesiredAccess = Access to open the handle with.
  22. //
  23. // Returns: BOOL
  24. //
  25. // Purpose: Opens the effective token. If the thread is impersonating then
  26. // this is opened. Otherwise the process token is opened.
  27. //
  28. // History: 2000-03-31 vtan created
  29. // --------------------------------------------------------------------------
  30. STDAPI_(BOOL) OpenEffectiveToken (IN DWORD dwDesiredAccess, OUT HANDLE *phToken)
  31. {
  32. BOOL fResult;
  33. if (IsBadWritePtr(phToken, sizeof(*phToken)))
  34. {
  35. SetLastError(ERROR_INVALID_PARAMETER);
  36. fResult = FALSE;
  37. }
  38. else
  39. {
  40. *phToken = NULL;
  41. fResult = OpenThreadToken(GetCurrentThread(), dwDesiredAccess, FALSE, phToken);
  42. if ((fResult == FALSE) && (GetLastError() == ERROR_NO_TOKEN))
  43. {
  44. fResult = OpenProcessToken(GetCurrentProcess(), dwDesiredAccess, phToken);
  45. }
  46. }
  47. return(fResult);
  48. }
  49. // --------------------------------------------------------------------------
  50. // CPrivilegeEnable::CPrivilegeEnable
  51. //
  52. // Arguments: pszName = Name of the privilege to enable.
  53. //
  54. // Returns: <none>
  55. //
  56. // Purpose: Gets the current state of the privilege and enables it. The
  57. // privilege is specified by name and looked up.
  58. //
  59. // History: 1999-08-23 vtan created
  60. // --------------------------------------------------------------------------
  61. CPrivilegeEnable::CPrivilegeEnable (const TCHAR *pszName) :
  62. _fSet(false),
  63. _hToken(NULL)
  64. {
  65. if (OpenEffectiveToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &_hToken) != FALSE)
  66. {
  67. TOKEN_PRIVILEGES newPrivilege;
  68. if (LookupPrivilegeValue(NULL, pszName, &newPrivilege.Privileges[0].Luid) != FALSE)
  69. {
  70. DWORD dwReturnTokenPrivilegesSize;
  71. newPrivilege.PrivilegeCount = 1;
  72. newPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  73. _fSet = (AdjustTokenPrivileges(_hToken,
  74. FALSE,
  75. &newPrivilege,
  76. sizeof(newPrivilege),
  77. &_tokenPrivilegePrevious,
  78. &dwReturnTokenPrivilegesSize) != FALSE);
  79. }
  80. }
  81. }
  82. // --------------------------------------------------------------------------
  83. // CPrivilegeEnable::~CPrivilegeEnable
  84. //
  85. // Arguments: <none>
  86. //
  87. // Returns: <none>
  88. //
  89. // Purpose: Restores the previous state of the privilege prior to
  90. // instantiation of the object.
  91. //
  92. // History: 1999-08-23 vtan created
  93. // --------------------------------------------------------------------------
  94. CPrivilegeEnable::~CPrivilegeEnable (void)
  95. {
  96. if (_fSet)
  97. {
  98. (BOOL)AdjustTokenPrivileges(_hToken,
  99. FALSE,
  100. &_tokenPrivilegePrevious,
  101. 0,
  102. NULL,
  103. NULL);
  104. }
  105. if (_hToken != NULL)
  106. {
  107. (BOOL)CloseHandle(_hToken);
  108. _hToken = NULL;
  109. }
  110. }