Source code of Windows XP (NT5)
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.

212 lines
6.6 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999-2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // CEnableThreadPrivilege.cpp
  7. //
  8. // Description:
  9. // Contains the definition of the CEnableThreadPrivilege class.
  10. //
  11. // Maintained By:
  12. // Vij Vasu (Vvasu) 08-MAR-2000
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. //////////////////////////////////////////////////////////////////////////////
  16. // Include Files
  17. //////////////////////////////////////////////////////////////////////////////
  18. // The precompiled header.
  19. #include "pch.h"
  20. // The header for this file
  21. #include "CEnableThreadPrivilege.h"
  22. //////////////////////////////////////////////////////////////////////////////
  23. //++
  24. //
  25. // CEnableThreadPrivilege::CEnableThreadPrivilege
  26. //
  27. // Description:
  28. // Constructor of the CEnableThreadPrivilege class. Enables the specified
  29. // privilege.
  30. //
  31. // Arguments:
  32. // pcszPrivilegeNameIn
  33. // Name of the privilege to be enabled.
  34. //
  35. // Return Value:
  36. // None.
  37. //
  38. // Exceptions Thrown:
  39. // CRuntimeError
  40. // If any of the APIs fail.
  41. //
  42. //--
  43. //////////////////////////////////////////////////////////////////////////////
  44. CEnableThreadPrivilege::CEnableThreadPrivilege( const WCHAR * pcszPrivilegeNameIn )
  45. : m_hThreadToken( NULL )
  46. , m_fPrivilegeEnabled( false )
  47. {
  48. BCATraceScope1( "pcszPrivilegeNameIn = '%ws'", pcszPrivilegeNameIn );
  49. DWORD dwError = ERROR_SUCCESS;
  50. do
  51. {
  52. TOKEN_PRIVILEGES tpPrivilege;
  53. DWORD dwReturnLength = sizeof( m_tpPreviousState );
  54. DWORD dwBufferLength = sizeof( tpPrivilege );
  55. // Open the current thread token.
  56. if ( OpenThreadToken(
  57. GetCurrentThread()
  58. , TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
  59. , TRUE
  60. , &m_hThreadToken
  61. )
  62. == FALSE
  63. )
  64. {
  65. dwError = GetLastError();
  66. // If the thread has no token, then default to the process token.
  67. if ( dwError == ERROR_NO_TOKEN )
  68. {
  69. BCATraceMsg( "The thread has no token. Trying to open the process token." );
  70. if ( OpenProcessToken(
  71. GetCurrentProcess()
  72. , TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
  73. , &m_hThreadToken
  74. )
  75. == FALSE
  76. )
  77. {
  78. dwError = TW32( GetLastError() );
  79. BCATraceMsg1( "Error %#08x occurred trying to open the process token.", dwError );
  80. break;
  81. } // if: OpenProcessToken() failed.
  82. // The process token was opened. All is well.
  83. dwError = ERROR_SUCCESS;
  84. } // if: the thread has no token
  85. else
  86. {
  87. TW32( dwError );
  88. BCATraceMsg1( "Error %#08x occurred trying to open the thread token.", dwError );
  89. break;
  90. } // if: some other error occurred
  91. } // if: OpenThreadToken() failed
  92. //
  93. // Initialize the TOKEN_PRIVILEGES structure.
  94. //
  95. tpPrivilege.PrivilegeCount = 1;
  96. if ( LookupPrivilegeValue( NULL, pcszPrivilegeNameIn, &tpPrivilege.Privileges[0].Luid ) == FALSE )
  97. {
  98. dwError = TW32( GetLastError() );
  99. BCATraceMsg1( "Error %#08x occurred trying to lookup privilege value.", dwError );
  100. break;
  101. } // if: LookupPrivilegeValue() failed
  102. tpPrivilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  103. // Enable the desired privilege.
  104. if ( AdjustTokenPrivileges(
  105. m_hThreadToken
  106. , FALSE
  107. , &tpPrivilege
  108. , dwBufferLength
  109. , &m_tpPreviousState
  110. , &dwReturnLength
  111. )
  112. == FALSE
  113. )
  114. {
  115. dwError = TW32( GetLastError() );
  116. BCATraceMsg1( "Error %#08x occurred trying to enable the privilege.", dwError );
  117. break;
  118. } // if: AdjustTokenPrivileges() failed
  119. LogMsg( "Privilege '%ws' enabled for the current thread.", pcszPrivilegeNameIn );
  120. // Set a flag if the privilege was not already enabled.
  121. m_fPrivilegeEnabled = ( m_tpPreviousState.Privileges[0].Attributes != SE_PRIVILEGE_ENABLED );
  122. }
  123. while( false ); // dummy do-while loop to avoid gotos
  124. if ( dwError != ERROR_SUCCESS )
  125. {
  126. LogMsg( "Error %#08x occurred trying to enable privilege '%ws'.", dwError, pcszPrivilegeNameIn );
  127. BCATraceMsg2( "Error %#08x occurred trying to enable privilege '%ws'. Throwing exception.", dwError, pcszPrivilegeNameIn );
  128. THROW_RUNTIME_ERROR( HRESULT_FROM_WIN32( dwError ), IDS_ERROR_ENABLE_THREAD_PRIVILEGE );
  129. } // if:something went wrong
  130. } //*** CEnableThreadPrivilege::CEnableThreadPrivilege()
  131. //////////////////////////////////////////////////////////////////////////////
  132. //++
  133. //
  134. // CEnableThreadPrivilege::~CEnableThreadPrivilege
  135. //
  136. // Description:
  137. // Destructor of the CEnableThreadPrivilege class. Restores the specified
  138. // privilege to its original state.
  139. //
  140. // Arguments:
  141. // None.
  142. //
  143. // Return Value:
  144. // None.
  145. //
  146. // Exceptions Thrown:
  147. // None.
  148. //
  149. //--
  150. //////////////////////////////////////////////////////////////////////////////
  151. CEnableThreadPrivilege::~CEnableThreadPrivilege( void ) throw()
  152. {
  153. BCATraceScope( "" );
  154. DWORD dwError = ERROR_SUCCESS;
  155. if ( m_fPrivilegeEnabled )
  156. {
  157. if ( AdjustTokenPrivileges(
  158. m_hThreadToken
  159. , FALSE
  160. , &m_tpPreviousState
  161. , sizeof( m_tpPreviousState )
  162. , NULL
  163. , NULL
  164. )
  165. == FALSE
  166. )
  167. {
  168. dwError = TW32( GetLastError() );
  169. LogMsg( "Error %#08x occurred trying to restore privilege.", dwError );
  170. BCATraceMsg1( "Error %#08x occurred trying to restore privilege.", dwError );
  171. } // if: AdjustTokenPrivileges() failed
  172. else
  173. {
  174. LogMsg( "Privilege restored.", dwError );
  175. } // else: no errors
  176. } // if: the privilege was successfully enabled in the constructor
  177. else
  178. {
  179. LogMsg( "Privilege was enabled to begin with. Doing nothing.", dwError );
  180. }
  181. if ( m_hThreadToken != NULL )
  182. {
  183. CloseHandle( m_hThreadToken );
  184. } // if: the thread handle was opened
  185. } //*** CEnableThreadPrivilege::~CEnableThreadPrivilege()