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.

281 lines
5.9 KiB

  1. // sessions.cpp : Implementation of CsmtpadmApp and DLL registration.
  2. #include "stdafx.h"
  3. #include "smtpadm.h"
  4. #include "sessions.h"
  5. #include "oleutil.h"
  6. #include "smtpcmn.h"
  7. #include "smtptype.h"
  8. #include "smtpapi.h"
  9. #include <lmapibuf.h>
  10. // Must define THIS_FILE_* macros to use SmtpCreateException()
  11. #define THIS_FILE_HELP_CONTEXT 0
  12. #define THIS_FILE_PROG_ID _T("Smtpadm.Sessions.1")
  13. #define THIS_FILE_IID IID_ISmtpAdminSessions
  14. /////////////////////////////////////////////////////////////////////////////
  15. //
  16. //
  17. // Use a macro to define all the default methods
  18. //
  19. DECLARE_METHOD_IMPLEMENTATION_FOR_STANDARD_EXTENSION_INTERFACES(SmtpAdminSessions, CSmtpAdminSessions, IID_ISmtpAdminSessions)
  20. STDMETHODIMP CSmtpAdminSessions::InterfaceSupportsErrorInfo(REFIID riid)
  21. {
  22. static const IID* arr[] =
  23. {
  24. &IID_ISmtpAdminSessions,
  25. };
  26. for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  27. {
  28. if (InlineIsEqualGUID(*arr[i],riid))
  29. return S_OK;
  30. }
  31. return S_FALSE;
  32. }
  33. CSmtpAdminSessions::CSmtpAdminSessions () :
  34. m_cCount ( 0 ),
  35. m_dwId ( 0 ),
  36. m_dwConnectTime ( 0 ),
  37. m_pSessionInfo ( NULL ),
  38. m_fSetCursor ( FALSE )
  39. // CComBSTR's are initialized to NULL by default.
  40. {
  41. InitAsyncTrace ( );
  42. m_iadsImpl.SetService ( MD_SERVICE_NAME );
  43. m_iadsImpl.SetName ( _T("Sessions") );
  44. m_iadsImpl.SetClass ( _T("IIsSmtpSessions") );
  45. }
  46. CSmtpAdminSessions::~CSmtpAdminSessions ()
  47. {
  48. if ( m_pSessionInfo ) {
  49. NetApiBufferFree ( m_pSessionInfo );
  50. }
  51. // All CComBSTR's are freed automatically.
  52. }
  53. //
  54. // IADs methods:
  55. //
  56. DECLARE_SIMPLE_IADS_IMPLEMENTATION(CSmtpAdminSessions,m_iadsImpl)
  57. //
  58. // enum props
  59. //
  60. STDMETHODIMP CSmtpAdminSessions::get_Count ( long * plCount )
  61. {
  62. // Count should check to be sure the client enumerated.
  63. return StdPropertyGet ( m_cCount, plCount );
  64. }
  65. STDMETHODIMP CSmtpAdminSessions::get_UserName ( BSTR * pstrUsername )
  66. {
  67. return StdPropertyGet ( m_strUsername, pstrUsername );
  68. }
  69. STDMETHODIMP CSmtpAdminSessions::get_Host( BSTR * pstrHost )
  70. {
  71. return StdPropertyGet ( m_strHost, pstrHost );
  72. }
  73. STDMETHODIMP CSmtpAdminSessions::get_UserId ( long * plId )
  74. {
  75. return StdPropertyGet ( m_dwId, plId );
  76. }
  77. STDMETHODIMP CSmtpAdminSessions::put_UserId ( long lId )
  78. {
  79. return StdPropertyPut ( &m_dwId, lId );
  80. }
  81. STDMETHODIMP CSmtpAdminSessions::get_ConnectTime ( long * plConnectTime )
  82. {
  83. return StdPropertyGet ( m_dwConnectTime, plConnectTime );
  84. }
  85. //////////////////////////////////////////////////////////////////////
  86. // Methods:
  87. //////////////////////////////////////////////////////////////////////
  88. STDMETHODIMP CSmtpAdminSessions::Enumerate ( )
  89. {
  90. // Variables:
  91. HRESULT hr = NOERROR;
  92. NET_API_STATUS err;
  93. // Validate Server & Service Instance:
  94. if ( m_iadsImpl.QueryInstance() == 0 ) {
  95. return SmtpCreateException ( IDS_SMTPEXCEPTION_SERVICE_INSTANCE_CANT_BE_ZERO );
  96. }
  97. // Enumerating loses the cursor:
  98. m_fSetCursor = FALSE;
  99. if ( m_pSessionInfo ) {
  100. NetApiBufferFree ( m_pSessionInfo );
  101. m_cCount = 0;
  102. }
  103. // Call the enumerate sessions RPC:
  104. err = SmtpGetConnectedUserList (
  105. m_iadsImpl.QueryComputer(),
  106. &m_pSessionInfo,
  107. m_iadsImpl.QueryInstance()
  108. );
  109. if( err == NO_ERROR )
  110. {
  111. m_cCount = m_pSessionInfo->cEntries;
  112. }
  113. else
  114. {
  115. hr = SmtpCreateExceptionFromWin32Error ( err );
  116. goto Exit;
  117. }
  118. Exit:
  119. if ( FAILED(hr) && hr != DISP_E_EXCEPTION ) {
  120. hr = SmtpCreateExceptionFromHresult ( hr );
  121. }
  122. return hr;
  123. }
  124. STDMETHODIMP CSmtpAdminSessions::GetNth ( long lIndex )
  125. {
  126. HRESULT hr = NOERROR;
  127. // Did we enumerate first?
  128. if ( m_pSessionInfo == NULL ) {
  129. return SmtpCreateException ( IDS_SMTPEXCEPTION_DIDNT_ENUMERATE );
  130. }
  131. // Is the index valid?
  132. if ( lIndex < 0 || (DWORD) lIndex >= m_cCount ) {
  133. return SmtpCreateException ( IDS_SMTPEXCEPTION_INVALID_INDEX );
  134. }
  135. //
  136. // Copy the properties from m_pSessionInfo [ lIndex ] to member variables:
  137. //
  138. // ( CComBSTR handles free-ing of old properties )
  139. m_dwId = m_pSessionInfo->aConnUserEntry[ lIndex ].dwUserId;
  140. m_dwConnectTime = m_pSessionInfo->aConnUserEntry[ lIndex ].dwConnectTime;
  141. m_strUsername = m_pSessionInfo->aConnUserEntry[ lIndex ].lpszName;
  142. if ( m_strUsername == NULL ) {
  143. hr = E_OUTOFMEMORY;
  144. goto Exit;
  145. }
  146. m_strHost = m_pSessionInfo->aConnUserEntry[ lIndex ].lpszHost;
  147. if ( m_strHost == NULL ) {
  148. hr = E_OUTOFMEMORY;
  149. goto Exit;
  150. }
  151. // GetNth sets the cursor:
  152. m_fSetCursor = TRUE;
  153. Exit:
  154. if ( FAILED(hr) && hr != DISP_E_EXCEPTION ) {
  155. hr = SmtpCreateExceptionFromHresult ( hr );
  156. }
  157. return hr;
  158. }
  159. STDMETHODIMP CSmtpAdminSessions::Terminate ( )
  160. {
  161. HRESULT hr = NOERROR;
  162. DWORD err;
  163. // Validate Server & Service Instance:
  164. if ( m_iadsImpl.QueryInstance() == 0 ) {
  165. return SmtpCreateException ( IDS_SMTPEXCEPTION_SERVICE_INSTANCE_CANT_BE_ZERO );
  166. }
  167. // Call the TerminateSession RPC:
  168. err = SmtpDisconnectUser (
  169. m_iadsImpl.QueryComputer(),
  170. m_dwId,
  171. m_iadsImpl.QueryInstance()
  172. );
  173. if( err != NOERROR ) {
  174. hr = SmtpCreateExceptionFromWin32Error ( err );
  175. goto Exit;
  176. }
  177. Exit:
  178. if ( FAILED(hr) && hr != DISP_E_EXCEPTION ) {
  179. hr = SmtpCreateExceptionFromHresult ( hr );
  180. }
  181. return hr;
  182. }
  183. STDMETHODIMP CSmtpAdminSessions::TerminateAll ( )
  184. {
  185. // Did we enumerate first?
  186. HRESULT hr = NOERROR;
  187. DWORD ErrResult = 0;
  188. DWORD Err;
  189. DWORD i;
  190. // Validate Server & Service Instance:
  191. if ( m_iadsImpl.QueryInstance() == 0 ) {
  192. return SmtpCreateException ( IDS_SMTPEXCEPTION_SERVICE_INSTANCE_CANT_BE_ZERO );
  193. }
  194. // Make sure the user has enumerated:
  195. if ( m_pSessionInfo == NULL ) {
  196. return SmtpCreateException ( IDS_SMTPEXCEPTION_DIDNT_ENUMERATE );
  197. }
  198. // For Each Session:
  199. for ( i = 0; i < m_cCount; i++ ) {
  200. // Call the terminate session RPC:
  201. Err = SmtpDisconnectUser (
  202. m_iadsImpl.QueryComputer(),
  203. m_pSessionInfo->aConnUserEntry[ i ].dwUserId,
  204. m_iadsImpl.QueryInstance()
  205. );
  206. if ( Err != 0 && ErrResult == 0 ) {
  207. ErrResult = Err;
  208. }
  209. }
  210. if( ErrResult != NOERROR ) {
  211. hr = SmtpCreateExceptionFromWin32Error ( ErrResult );
  212. goto Exit;
  213. }
  214. Exit:
  215. if ( FAILED(hr) && hr != DISP_E_EXCEPTION ) {
  216. hr = SmtpCreateExceptionFromHresult ( hr );
  217. }
  218. return hr;
  219. }