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.

446 lines
9.9 KiB

  1. // sessions.cpp : Implementation of CnntpadmApp and DLL registration.
  2. #include "stdafx.h"
  3. #include "nntpcmn.h"
  4. #include "oleutil.h"
  5. #include "sessions.h"
  6. #include "nntptype.h"
  7. #include "nntpapi.h"
  8. #include <lmapibuf.h>
  9. // Must define THIS_FILE_* macros to use NntpCreateException()
  10. #define THIS_FILE_HELP_CONTEXT 0
  11. #define THIS_FILE_PROG_ID _T("Nntpadm.Sessions.1")
  12. #define THIS_FILE_IID IID_INntpAdminSessions
  13. /////////////////////////////////////////////////////////////////////////////
  14. //
  15. //
  16. // Use a macro to define all the default methods
  17. //
  18. DECLARE_METHOD_IMPLEMENTATION_FOR_STANDARD_EXTENSION_INTERFACES(NntpAdminSessions, CNntpAdminSessions, IID_INntpAdminSessions)
  19. STDMETHODIMP CNntpAdminSessions::InterfaceSupportsErrorInfo(REFIID riid)
  20. {
  21. static const IID* arr[] =
  22. {
  23. &IID_INntpAdminSessions,
  24. };
  25. for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  26. {
  27. if (InlineIsEqualGUID(*arr[i],riid))
  28. return S_OK;
  29. }
  30. return S_FALSE;
  31. }
  32. CNntpAdminSessions::CNntpAdminSessions () :
  33. m_cCount ( 0 ),
  34. m_dwIpAddress ( 0 ),
  35. m_dwPort ( 0 ),
  36. m_dwAuthenticationType ( 0 ),
  37. m_fIsAnonymous ( FALSE ),
  38. m_dateStartTime ( 0 ),
  39. m_pSessionInfo ( NULL ),
  40. m_fSetCursor ( FALSE )
  41. // CComBSTR's are initialized to NULL by default.
  42. {
  43. InitAsyncTrace ( );
  44. m_iadsImpl.SetService ( MD_SERVICE_NAME );
  45. m_iadsImpl.SetName ( _T("Sessions") );
  46. m_iadsImpl.SetClass ( _T("IIsNntpSessions") );
  47. }
  48. CNntpAdminSessions::~CNntpAdminSessions ()
  49. {
  50. if ( m_pSessionInfo ) {
  51. NetApiBufferFree ( m_pSessionInfo );
  52. }
  53. // All CComBSTR's are freed automatically.
  54. TermAsyncTrace ( );
  55. }
  56. //
  57. // IADs methods:
  58. //
  59. DECLARE_SIMPLE_IADS_IMPLEMENTATION(CNntpAdminSessions,m_iadsImpl)
  60. //
  61. // Properties:
  62. //
  63. STDMETHODIMP CNntpAdminSessions::get_Count ( long * plCount )
  64. {
  65. // Count should check to be sure the client enumerated.
  66. return StdPropertyGet ( m_cCount, plCount );
  67. }
  68. STDMETHODIMP CNntpAdminSessions::get_Username ( BSTR * pstrUsername )
  69. {
  70. return StdPropertyGet ( m_strUsername, pstrUsername );
  71. }
  72. STDMETHODIMP CNntpAdminSessions::put_Username ( BSTR strUsername )
  73. {
  74. _ASSERT ( strUsername );
  75. _ASSERT ( IS_VALID_STRING ( strUsername ) );
  76. if ( strUsername == NULL ) {
  77. return E_POINTER;
  78. }
  79. if ( lstrcmp ( strUsername, _T("") ) == 0 ) {
  80. m_strUsername.Empty();
  81. return NOERROR;
  82. }
  83. else {
  84. return StdPropertyPut ( &m_strUsername, strUsername );
  85. }
  86. }
  87. STDMETHODIMP CNntpAdminSessions::get_IpAddress ( BSTR * pstrIpAddress )
  88. {
  89. return StdPropertyGet ( m_strIpAddress, pstrIpAddress );
  90. }
  91. STDMETHODIMP CNntpAdminSessions::put_IpAddress ( BSTR strIpAddress )
  92. {
  93. _ASSERT ( strIpAddress );
  94. _ASSERT ( IS_VALID_STRING ( strIpAddress ) );
  95. if ( strIpAddress == NULL ) {
  96. return E_POINTER;
  97. }
  98. if ( lstrcmp ( strIpAddress, _T("") ) == 0 ) {
  99. m_strIpAddress.Empty();
  100. m_dwIpAddress = 0;
  101. return NOERROR;
  102. }
  103. else {
  104. // The IP Address value has two properties, so keep them in sync.
  105. StringToInetAddress ( strIpAddress, &m_dwIpAddress );
  106. return StdPropertyPut ( &m_strIpAddress, strIpAddress );
  107. }
  108. }
  109. STDMETHODIMP CNntpAdminSessions::get_IntegerIpAddress ( long * plIpAddress )
  110. {
  111. return StdPropertyGet ( m_dwIpAddress, plIpAddress );
  112. }
  113. STDMETHODIMP CNntpAdminSessions::put_IntegerIpAddress ( long lIpAddress )
  114. {
  115. HRESULT hr = NOERROR;
  116. WCHAR wszAddress[100];
  117. DWORD dwOldIpAddress = m_dwIpAddress;
  118. hr = StdPropertyPut ( &m_dwIpAddress, lIpAddress );
  119. if ( FAILED (hr) ) {
  120. goto Exit;
  121. }
  122. // The IP Address value has two properties, so keep them in sync.
  123. if ( !InetAddressToString ( lIpAddress, wszAddress, 100 ) ) {
  124. hr = E_FAIL;
  125. goto Exit;
  126. }
  127. m_strIpAddress = wszAddress;
  128. if ( m_strIpAddress == NULL ) {
  129. hr = E_OUTOFMEMORY;
  130. goto Exit;
  131. }
  132. Exit:
  133. if ( FAILED (hr) ) {
  134. // We failed, so put back the old IP address:
  135. m_dwIpAddress = dwOldIpAddress;
  136. }
  137. return hr;
  138. }
  139. STDMETHODIMP CNntpAdminSessions::get_Port ( long * plPort )
  140. {
  141. CHECK_FOR_SET_CURSOR ( m_pSessionInfo != NULL, m_fSetCursor );
  142. return StdPropertyGet ( m_dwPort, plPort );
  143. }
  144. STDMETHODIMP CNntpAdminSessions::get_AuthenticationType ( long * plAuthenticationType )
  145. {
  146. CHECK_FOR_SET_CURSOR ( m_pSessionInfo != NULL, m_fSetCursor );
  147. return StdPropertyGet ( m_dwAuthenticationType, plAuthenticationType );
  148. }
  149. STDMETHODIMP CNntpAdminSessions::get_IsAnonymous ( BOOL * pfAnonymous )
  150. {
  151. CHECK_FOR_SET_CURSOR ( m_pSessionInfo != NULL, m_fSetCursor );
  152. return StdPropertyGet ( m_fIsAnonymous, pfAnonymous );
  153. }
  154. STDMETHODIMP CNntpAdminSessions::get_StartTime ( DATE * pdateStart )
  155. {
  156. CHECK_FOR_SET_CURSOR ( m_pSessionInfo != NULL, m_fSetCursor );
  157. return StdPropertyGet ( m_dateStartTime, pdateStart );
  158. }
  159. //////////////////////////////////////////////////////////////////////
  160. // Methods:
  161. //////////////////////////////////////////////////////////////////////
  162. STDMETHODIMP CNntpAdminSessions::Enumerate ( )
  163. {
  164. TraceFunctEnter ( "CNntpAdminSessions::Enumerate" );
  165. // Variables:
  166. HRESULT hr = NOERROR;
  167. NET_API_STATUS err;
  168. // Validate Server & Service Instance:
  169. if ( m_iadsImpl.QueryInstance() == 0 ) {
  170. return NntpCreateException ( IDS_NNTPEXCEPTION_SERVICE_INSTANCE_CANT_BE_ZERO );
  171. }
  172. // Enumerating loses the cursor:
  173. m_fSetCursor = FALSE;
  174. if ( m_pSessionInfo ) {
  175. NetApiBufferFree ( m_pSessionInfo );
  176. }
  177. // Call the enumerate sessions RPC:
  178. err = NntpEnumerateSessions (
  179. m_iadsImpl.QueryComputer(),
  180. m_iadsImpl.QueryInstance(),
  181. &m_cCount,
  182. &m_pSessionInfo
  183. );
  184. if ( err != NOERROR ) {
  185. hr = RETURNCODETOHRESULT ( err );
  186. goto Exit;
  187. }
  188. Exit:
  189. TRACE_HRESULT(hr);
  190. TraceFunctLeave ();
  191. return hr;
  192. }
  193. STDMETHODIMP CNntpAdminSessions::GetNth ( long lIndex )
  194. {
  195. TraceFunctEnter ( "CNntpAdminSessions::GetNth" );
  196. HRESULT hr = NOERROR;
  197. FILETIME ftLocal;
  198. SYSTEMTIME st;
  199. WCHAR wszUsername[MAX_USER_NAME_LENGTH + 1];
  200. WCHAR wszIpAddress[256];
  201. DWORD cchCopied;
  202. *wszUsername = NULL;
  203. // Did we enumerate first?
  204. if ( m_pSessionInfo == NULL ) {
  205. return NntpCreateException ( IDS_NNTPEXCEPTION_DIDNT_ENUMERATE );
  206. }
  207. // Is the index valid?
  208. if ( lIndex < 0 || (DWORD) lIndex >= m_cCount ) {
  209. return NntpCreateException ( IDS_NNTPEXCEPTION_INVALID_INDEX );
  210. }
  211. //
  212. // Copy the properties from m_pSessionInfo [ lIndex ] to member variables:
  213. //
  214. // ( CComBSTR handles free-ing of old properties )
  215. FileTimeToLocalFileTime ( &m_pSessionInfo[ lIndex ].SessionStartTime, &ftLocal );
  216. FileTimeToSystemTime ( &ftLocal, &st );
  217. SystemTimeToVariantTime ( &st, &m_dateStartTime );
  218. m_dwIpAddress = m_pSessionInfo[ lIndex ].IPAddress;
  219. m_dwAuthenticationType = m_pSessionInfo[ lIndex ].AuthenticationType;
  220. m_dwPort = m_pSessionInfo[ lIndex ].PortConnected;
  221. m_fIsAnonymous = m_pSessionInfo[ lIndex ].fAnonymous;
  222. cchCopied = MultiByteToWideChar (
  223. CP_ACP,
  224. MB_PRECOMPOSED | MB_USEGLYPHCHARS,
  225. m_pSessionInfo[ lIndex ].UserName,
  226. -1,
  227. wszUsername,
  228. MAX_USER_NAME_LENGTH
  229. );
  230. m_strUsername = wszUsername;
  231. if ( m_strUsername == NULL ) {
  232. hr = E_OUTOFMEMORY;
  233. goto Exit;
  234. }
  235. InetAddressToString ( m_dwIpAddress, wszIpAddress, 256 );
  236. m_strIpAddress = wszIpAddress;
  237. if ( m_strIpAddress == NULL ) {
  238. hr = E_OUTOFMEMORY;
  239. goto Exit;
  240. }
  241. // GetNth sets the cursor:
  242. m_fSetCursor = TRUE;
  243. Exit:
  244. TRACE_HRESULT(hr);
  245. TraceFunctLeave ();
  246. return hr;
  247. }
  248. STDMETHODIMP CNntpAdminSessions::Terminate ( )
  249. {
  250. TraceFunctEnter ( "CNntpAdminSessions::Terminate" );
  251. HRESULT hr = NOERROR;
  252. DWORD err = NOERROR;
  253. char szAnsiUsername[ MAX_USER_NAME_LENGTH + 1];
  254. char szAnsiIpAddress[ 50 ];
  255. DWORD cchCopied;
  256. szAnsiUsername[0] = NULL;
  257. szAnsiIpAddress[0] = NULL;
  258. // Validate Server & Service Instance:
  259. if ( m_iadsImpl.QueryInstance() == 0 ) {
  260. return NntpCreateException ( IDS_NNTPEXCEPTION_SERVICE_INSTANCE_CANT_BE_ZERO );
  261. }
  262. // Check Username & IpAddress parameters:
  263. if ( m_strUsername == NULL && m_strIpAddress == NULL ) {
  264. return NntpCreateException ( IDS_NNTPEXCEPTION_MUST_SUPPLY_USERNAME_OR_IPADDRESS );
  265. }
  266. // Translate the username & ipaddress to ANSI.
  267. if ( m_strUsername != NULL ) {
  268. cchCopied = WideCharToMultiByte (
  269. CP_ACP,
  270. 0,
  271. m_strUsername,
  272. -1,
  273. szAnsiUsername,
  274. MAX_USER_NAME_LENGTH,
  275. NULL,
  276. NULL
  277. );
  278. }
  279. if ( m_strIpAddress != NULL ) {
  280. cchCopied = WideCharToMultiByte (
  281. CP_ACP,
  282. 0,
  283. m_strIpAddress,
  284. -1,
  285. szAnsiIpAddress,
  286. 50,
  287. NULL,
  288. NULL
  289. );
  290. }
  291. // Call the TerminateSession RPC:
  292. err = NntpTerminateSession (
  293. m_iadsImpl.QueryComputer(),
  294. m_iadsImpl.QueryInstance(),
  295. m_strUsername ? szAnsiUsername : NULL,
  296. m_strIpAddress ? szAnsiIpAddress : NULL
  297. );
  298. if ( err != NOERROR ) {
  299. hr = RETURNCODETOHRESULT ( err );
  300. goto Exit;
  301. }
  302. Exit:
  303. TRACE_HRESULT(hr);
  304. TraceFunctLeave ();
  305. return hr;
  306. }
  307. STDMETHODIMP CNntpAdminSessions::TerminateAll ( )
  308. {
  309. TraceFunctEnter ( "CNntpAdminSessions::TerminateAll" );
  310. // Did we enumerate first?
  311. HRESULT hr = NOERROR;
  312. DWORD ErrResult = NOERROR;
  313. DWORD Err = NOERROR;
  314. DWORD i;
  315. // Validate Server & Service Instance:
  316. if ( m_iadsImpl.QueryInstance() == 0 ) {
  317. return NntpCreateException ( IDS_NNTPEXCEPTION_SERVICE_INSTANCE_CANT_BE_ZERO );
  318. }
  319. #if 0
  320. // Make sure the user has enumerated:
  321. if ( m_pSessionInfo == NULL ) {
  322. return NntpCreateException ( IDS_NNTPEXCEPTION_DIDNT_ENUMERATE );
  323. }
  324. #endif
  325. // For Each Session:
  326. for ( i = 0; i < m_cCount; i++ ) {
  327. // Call the terminate session RPC:
  328. Err = NntpTerminateSession (
  329. m_iadsImpl.QueryComputer(),
  330. m_iadsImpl.QueryInstance(),
  331. m_pSessionInfo[ i ].UserName,
  332. NULL
  333. );
  334. if ( Err != 0 && ErrResult == 0 ) {
  335. ErrResult = Err;
  336. }
  337. }
  338. if ( ErrResult != NOERROR ) {
  339. hr = RETURNCODETOHRESULT ( ErrResult );
  340. goto Exit;
  341. }
  342. Exit:
  343. TRACE_HRESULT(hr);
  344. TraceFunctLeave ();
  345. return hr;
  346. }