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.

186 lines
4.8 KiB

  1. //=================================================================
  2. //
  3. // CLuidHelper.cpp
  4. //
  5. // Copyright (c) 2002 Microsoft Corporation, All Rights Reserved
  6. //
  7. //=================================================================
  8. #include "precomp.h"
  9. #include "CLuidHelper.h"
  10. #include "DllWrapperCreatorReg.h"
  11. #include <scopeguard.h>
  12. // {5356C625-EE38-4c4f-9370-D925C91A6D99}
  13. static const GUID g_guidSecur32Api =
  14. { 0x5356c625, 0xee38, 0x4c4f, { 0x93, 0x70, 0xd9, 0x25, 0xc9, 0x1a, 0x6d, 0x99 } };
  15. static const WCHAR g_tstrSecur32[] = L"Secur32.DLL" ;
  16. /******************************************************************************
  17. * Register this class with the CResourceManager.
  18. *****************************************************************************/
  19. CDllApiWraprCreatrReg<CSecur32Api, &g_guidSecur32Api, g_tstrSecur32> MyRegisteredSecur32Wrapper;
  20. /******************************************************************************
  21. * Constructor
  22. ******************************************************************************/
  23. CSecur32Api::CSecur32Api ( LPCWSTR a_tstrWrappedDllName ) : CDllWrapperBase ( a_tstrWrappedDllName ) ,
  24. m_pfncLsaEnumerateLogonSessions ( NULL ) ,
  25. m_pfncLsaGetLogonSessionData ( NULL ) ,
  26. m_pfncLsaFreeReturnBuffer ( NULL )
  27. {
  28. }
  29. /******************************************************************************
  30. * Destructor
  31. ******************************************************************************/
  32. CSecur32Api::~CSecur32Api ()
  33. {
  34. }
  35. bool CSecur32Api::Init ()
  36. {
  37. bool bRet = LoadLibrary () ;
  38. if ( bRet )
  39. {
  40. m_pfncLsaEnumerateLogonSessions =
  41. ( PFN_LSA_ENUMERATE_LOGON_SESSIONS ) GetProcAddress ( "LsaEnumerateLogonSessions" ) ;
  42. m_pfncLsaGetLogonSessionData =
  43. ( PFN_LSA_GET_LOGON_SESSION_DATA ) GetProcAddress ( "LsaGetLogonSessionData" ) ;
  44. m_pfncLsaFreeReturnBuffer =
  45. ( PFN_LSA_FREE_RETURN_BUFFER ) GetProcAddress ( "LsaFreeReturnBuffer" ) ;
  46. if (
  47. m_pfncLsaEnumerateLogonSessions == NULL ||
  48. m_pfncLsaGetLogonSessionData == NULL ||
  49. m_pfncLsaFreeReturnBuffer == NULL
  50. )
  51. {
  52. bRet = FALSE ;
  53. }
  54. }
  55. return bRet ;
  56. }
  57. NTSTATUS CSecur32Api::LsaEnumerateLogonSessions (
  58. PULONG LogonSessionCount,
  59. PLUID* LogonSessionList
  60. )
  61. {
  62. NTSTATUS NtStatus = STATUS_PROCEDURE_NOT_FOUND ;
  63. if ( m_pfncLsaEnumerateLogonSessions )
  64. {
  65. NtStatus = m_pfncLsaEnumerateLogonSessions ( LogonSessionCount, LogonSessionList ) ;
  66. }
  67. return NtStatus ;
  68. }
  69. NTSTATUS CSecur32Api::LsaGetLogonSessionData (
  70. PLUID LogonId,
  71. PSECURITY_LOGON_SESSION_DATA* ppLogonSessionData
  72. )
  73. {
  74. NTSTATUS NtStatus = STATUS_PROCEDURE_NOT_FOUND ;
  75. if ( m_pfncLsaGetLogonSessionData )
  76. {
  77. NtStatus = m_pfncLsaGetLogonSessionData ( LogonId, ppLogonSessionData ) ;
  78. }
  79. return NtStatus ;
  80. }
  81. NTSTATUS CSecur32Api::LsaFreeReturnBuffer ( PVOID Buffer )
  82. {
  83. NTSTATUS NtStatus = STATUS_PROCEDURE_NOT_FOUND ;
  84. if ( m_pfncLsaFreeReturnBuffer )
  85. {
  86. NtStatus = m_pfncLsaFreeReturnBuffer ( Buffer ) ;
  87. }
  88. return NtStatus ;
  89. }
  90. //
  91. // LUID HELPER
  92. //
  93. CLuidHelper::Resource::Resource () : m_pSecur32 ( NULL )
  94. {
  95. m_pSecur32 = ( CSecur32Api* ) CResourceManager::sm_TheResourceManager.GetResource ( g_guidSecur32Api, NULL ) ;
  96. }
  97. CLuidHelper::Resource::~Resource ()
  98. {
  99. if ( m_pSecur32 )
  100. {
  101. CResourceManager::sm_TheResourceManager.ReleaseResource ( g_guidSecur32Api, m_pSecur32 ) ;
  102. }
  103. }
  104. BOOL CLuidHelper::IsInteractiveSession ( PLUID session )
  105. {
  106. BOOL bResult = FALSE ;
  107. if ( NULL != session )
  108. {
  109. Resource res;
  110. if ( FALSE == !res )
  111. {
  112. PSECURITY_LOGON_SESSION_DATA data = NULL ;
  113. NTSTATUS Status = STATUS_SUCCESS ;
  114. if ( STATUS_SUCCESS == ( Status = res()->LsaGetLogonSessionData ( session, &data ) ) )
  115. {
  116. if ( NULL != data )
  117. {
  118. ScopeGuard datafree = MakeObjGuard ( (CSecur32Api) res, CSecur32Api::LsaFreeReturnBuffer, data ) ;
  119. if ( Interactive == ( SECURITY_LOGON_TYPE ) data->LogonType )
  120. {
  121. bResult = TRUE ;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. return bResult ;
  128. }
  129. NTSTATUS CLuidHelper::GetLUIDFromProcess ( HANDLE process, PLUID pLUID)
  130. {
  131. NTSTATUS Status = STATUS_UNSUCCESSFUL ;
  132. if ( process )
  133. {
  134. SmartCloseHandle token ;
  135. if ( OpenProcessToken ( process, TOKEN_QUERY, &token ) )
  136. {
  137. TOKEN_STATISTICS tokstats ;
  138. DWORD dwRetSize ;
  139. if ( GetTokenInformation ( token, TokenStatistics, &tokstats, sizeof ( TOKEN_STATISTICS ), &dwRetSize ) )
  140. {
  141. pLUID->LowPart = tokstats.AuthenticationId.LowPart ;
  142. pLUID->HighPart = tokstats.AuthenticationId.HighPart ;
  143. Status = STATUS_SUCCESS ;
  144. }
  145. else
  146. {
  147. Status = (NTSTATUS)NtCurrentTeb()->LastStatusValue ;
  148. }
  149. }
  150. else
  151. {
  152. Status = (NTSTATUS)NtCurrentTeb()->LastStatusValue ;
  153. }
  154. }
  155. return Status ;
  156. }