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.

218 lines
6.7 KiB

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