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.

195 lines
6.4 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999-2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // CImpersonateUser.cpp
  7. //
  8. // Description:
  9. // Contains the definition of the CImpersonateUser class.
  10. //
  11. // Maintained By:
  12. // Vij Vasu (Vvasu) 16-MAY-2000
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. //////////////////////////////////////////////////////////////////////////////
  16. // Include Files
  17. //////////////////////////////////////////////////////////////////////////////
  18. // The precompiled header.
  19. #include "pch.h"
  20. // The header for this file
  21. #include "CImpersonateUser.h"
  22. //////////////////////////////////////////////////////////////////////////////
  23. //++
  24. //
  25. // CImpersonateUser::CImpersonateUser
  26. //
  27. // Description:
  28. // Constructor of the CImpersonateUser class. Begins impersonating the
  29. // user specified by the argument.
  30. //
  31. // Arguments:
  32. // hUserToken
  33. // Handle to the user account token to impersonate
  34. //
  35. // Return Value:
  36. // None.
  37. //
  38. // Exceptions Thrown:
  39. // CRuntimeError
  40. // If any of the APIs fail.
  41. //
  42. //--
  43. //////////////////////////////////////////////////////////////////////////////
  44. CImpersonateUser::CImpersonateUser( HANDLE hUserToken )
  45. : m_hThreadToken( NULL )
  46. , m_fWasImpersonating( false )
  47. {
  48. BCATraceScope1( "hUserToken = %p", hUserToken );
  49. DWORD dwError = ERROR_SUCCESS;
  50. do
  51. {
  52. // Check if this thread is already impersonating a client.
  53. {
  54. if ( OpenThreadToken(
  55. GetCurrentThread()
  56. , TOKEN_ALL_ACCESS
  57. , FALSE
  58. , &m_hThreadToken
  59. )
  60. == FALSE
  61. )
  62. {
  63. dwError = GetLastError();
  64. if ( dwError == ERROR_NO_TOKEN )
  65. {
  66. // There is no thread token, so we are not impersonating - this is ok.
  67. BCATraceMsg( "This thread is not impersonating anyone." );
  68. m_fWasImpersonating = false;
  69. dwError = ERROR_SUCCESS;
  70. } // if: there is no thread token
  71. else
  72. {
  73. TW32( dwError );
  74. BCATraceMsg( "OpenThreadToken() failed." );
  75. break;
  76. } // else: something really went wrong
  77. } // if: OpenThreadToken() failed
  78. else
  79. {
  80. TOKEN_TYPE ttTokenType;
  81. DWORD dwReturnLength;
  82. if ( GetTokenInformation(
  83. m_hThreadToken
  84. , TokenType
  85. , &ttTokenType
  86. , sizeof( ttTokenType )
  87. , &dwReturnLength
  88. )
  89. == FALSE
  90. )
  91. {
  92. dwError = TW32( GetLastError() );
  93. BCATraceMsg( "GetTokenInformation() failed." );
  94. break;
  95. } // if: GetTokenInformation() failed
  96. else
  97. {
  98. m_fWasImpersonating = ( ttTokenType == TokenImpersonation );
  99. BCATraceMsg1( "Is this thread impersonating anyone? %d ( 0 = No ).", m_fWasImpersonating );
  100. } // else: GetTokenInformation() succeeded
  101. } // else: OpenThreadToken() succeeded
  102. }
  103. // Try to impersonate the user.
  104. if ( ImpersonateLoggedOnUser( hUserToken ) == FALSE )
  105. {
  106. dwError = TW32( GetLastError() );
  107. BCATraceMsg( "ImpersonateLoggedOnUser() failed." );
  108. break;
  109. } // if: ImpersonateLoggedOnUser() failed
  110. BCATraceMsg( "Impersonation succeeded." );
  111. }
  112. while( false ); // dummy do-while loop to avoid gotos.
  113. if ( dwError != ERROR_SUCCESS )
  114. {
  115. LogMsg( "Error %#08x occurred trying to impersonate a user.", dwError );
  116. BCATraceMsg1( "Error %#08x occurred trying to impersonate a user. Throwing exception.", dwError );
  117. THROW_RUNTIME_ERROR( HRESULT_FROM_WIN32( dwError ), IDS_ERROR_IMPERSONATE_USER );
  118. } // if:something went wrong
  119. } //*** CImpersonateUser::CImpersonateUser()
  120. //////////////////////////////////////////////////////////////////////////////
  121. //++
  122. //
  123. // CImpersonateUser::~CImpersonateUser
  124. //
  125. // Description:
  126. // Destructor of the CImpersonateUser class. Reverts to the original token.
  127. //
  128. // Arguments:
  129. // None.
  130. //
  131. // Return Value:
  132. // None.
  133. //
  134. // Exceptions Thrown:
  135. // None.
  136. //
  137. //--
  138. //////////////////////////////////////////////////////////////////////////////
  139. CImpersonateUser::~CImpersonateUser( void ) throw()
  140. {
  141. BCATraceScope( "" );
  142. if ( m_fWasImpersonating )
  143. {
  144. // Try to revert to the previous impersonation.
  145. if ( ImpersonateLoggedOnUser( m_hThreadToken ) == FALSE )
  146. {
  147. // Something failed - nothing much we can do here
  148. DWORD dwError = TW32( GetLastError() );
  149. LogMsg( "!!! WARNING !!! Error %#08x occurred trying to revert to previous impersonation. Application may not run properly.", dwError );
  150. BCATraceMsg1( "!!! WARNING !!! Error %#08x occurred trying to revert to previous impersonation. Cannot throw exception from destructor. Application may not run properly.", dwError );
  151. } // if: ImpersonateLoggedOnUser() failed
  152. else
  153. {
  154. BCATraceMsg( "Successfully reverted to previous impersonation." );
  155. } // else: ImpersonateLoggedOnUser() succeeded
  156. } // if: we were impersonating someone when we started
  157. else
  158. {
  159. // Try to revert to self.
  160. if ( RevertToSelf() == FALSE )
  161. {
  162. DWORD dwError = TW32( GetLastError() );
  163. LogMsg( "!!! WARNING !!! Error %#08x occurred trying to revert to self. Application may not run properly.", dwError );
  164. BCATraceMsg1( "!!! WARNING !!! Error %#08x occurred trying to revert to self. Cannot throw exception from destructor. Application may not run properly.", dwError );
  165. } // if: RevertToSelf() failed
  166. else
  167. {
  168. BCATraceMsg( "Successfully reverted to self." );
  169. } // else: RevertToSelf() succeeded
  170. } // else: we weren't impersonating anyone to begin with
  171. } //*** CImpersonateUser::~CImpersonateUser()