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.

265 lines
6.1 KiB

  1. // Connection.h
  2. // This class is the hSource returned by WMIEventSourceConnect.
  3. #pragma once
  4. /////////////////////////////////////////////////////////////////////////////
  5. // CConnection
  6. #include "NCObjApi.h"
  7. #include "Buffer.h"
  8. #include "ReportEvent.h"
  9. #include "NamedPipe.h"
  10. #include "corex.h"
  11. #include <list>
  12. #include <map>
  13. class CMyString
  14. {
  15. LPWSTR m_wsz;
  16. void Set( LPCWSTR wsz )
  17. {
  18. delete [] m_wsz;
  19. if ( wsz != NULL )
  20. {
  21. m_wsz = new WCHAR[wcslen(wsz)+1];
  22. if ( m_wsz != NULL )
  23. {
  24. StringCchCopyW( m_wsz, wcslen(wsz)+1, wsz );
  25. }
  26. else
  27. {
  28. throw CX_MemoryException();
  29. }
  30. }
  31. else
  32. {
  33. m_wsz = NULL;
  34. }
  35. }
  36. public:
  37. CMyString( LPCWSTR wsz = NULL ) : m_wsz( NULL ) { Set(wsz); }
  38. CMyString( const CMyString& rws ) : m_wsz(NULL) { *this = rws; }
  39. const CMyString & operator=( const CMyString& rws ) { Set( rws.m_wsz ); return *this; }
  40. ~CMyString() { delete [] m_wsz; }
  41. bool operator< ( const CMyString& rws ) const
  42. { return _wcsicmp( m_wsz, rws.m_wsz ) < 0; }
  43. };
  44. typedef CMyString wstring;
  45. class CEvent;
  46. class CTransport;
  47. typedef std::list< CEvent*, wbem_allocator<CEvent*> > CEventList;
  48. typedef CEventList::iterator CEventListIterator;
  49. #define NUM_TRANSPORTS 2
  50. enum TRANSPORT_INDEX
  51. {
  52. TRANS_NAMED_PIPE,
  53. TRANS_EVENT_TRACE,
  54. };
  55. struct NC_SRVMSG_REPLY;
  56. class CSink
  57. {
  58. public:
  59. LPEVENT_SOURCE_CALLBACK
  60. m_pCallback;
  61. LPVOID m_pUserData;
  62. CReportEventMap m_mapReportEvents;
  63. CRITICAL_SECTION m_cs;
  64. CSink();
  65. ~CSink();
  66. BOOL Init(
  67. CConnection *pConnection,
  68. DWORD dwID,
  69. LPVOID pUserData,
  70. LPEVENT_SOURCE_CALLBACK pCallback);
  71. BOOL IsReady();
  72. DWORD GetSinkID() { return m_dwSinkID; }
  73. // Used for keeping track of the events created with the CConnection.
  74. void AddEvent(CEvent *pEvent);
  75. void RemoveEvent(CEvent *pEvent);
  76. void ResetEventBufferLayoutSent();
  77. BOOL IsEventClassEnabled(LPCWSTR szEventClass);
  78. CConnection *GetConnection() { return m_pConnection; }
  79. void Lock() { EnterCriticalSection(&m_cs); }
  80. void Unlock() { LeaveCriticalSection(&m_cs); }
  81. void EnableEventUsingList(CEvent *pEvent);
  82. protected:
  83. friend CConnection;
  84. typedef std::map<wstring, int, std::less<wstring>, wbem_allocator<int> > CStrToIntMap;
  85. typedef CStrToIntMap::iterator CStrToIntMapIterator;
  86. CConnection *m_pConnection;
  87. CEventList m_listEvents;
  88. CStrToIntMap m_mapEnabledEvents;
  89. DWORD m_dwSinkID;
  90. void AddToEnabledEventList(CBuffer *pBuffer);
  91. void RemoveFromEnabledEventList(CBuffer *pBuffer);
  92. void EnableAndDisableEvents();
  93. };
  94. class CConnection
  95. {
  96. public:
  97. BOOL m_bDone;
  98. HANDLE m_heventDone,
  99. m_hthreadSend,
  100. m_heventEventsPending,
  101. m_heventBufferNotFull,
  102. m_heventBufferFull;
  103. WCHAR m_szBaseNamespace[MAX_PATH * 2],
  104. m_szBaseProviderName[MAX_PATH * 2];
  105. CRITICAL_SECTION
  106. m_cs,
  107. m_csBuffer;
  108. DWORD m_dwSendLatency;
  109. BOOL m_bUseBatchSend;
  110. BOOL m_bWMIResync;
  111. HANDLE m_heventWMIInit,
  112. m_hthreadWMIInit;
  113. CTransport *m_pTransport;
  114. CConnection(BOOL bBatchSend, DWORD dwBatchBufferSize, DWORD dwMaxSendLatency);
  115. ~CConnection();
  116. BOOL Init(
  117. LPCWSTR szNamespace,
  118. LPCWSTR szProviderName,
  119. LPVOID pUserData,
  120. LPEVENT_SOURCE_CALLBACK pCallback);
  121. void Deinit();
  122. void Lock() { EnterCriticalSection(&m_cs); }
  123. void Unlock() { LeaveCriticalSection(&m_cs); }
  124. BOOL SendMessagesOverTransport( PBYTE pBuffer, DWORD cBuffer );
  125. BOOL SendData(LPBYTE pBuffer, DWORD dwSize);
  126. BOOL ResyncWithWMI();
  127. BOOL IndicateProvEnabled();
  128. void IndicateProvDisabled();
  129. void StopThreads();
  130. BOOL SendInitInfo();
  131. BOOL WaitingForWMIInit() { return m_bWMIResync; }
  132. HRESULT ProcessMessage(LPBYTE pData, DWORD dwSize);
  133. BOOL IsReady() { return m_pTransport && m_pTransport->IsReady(); }
  134. CSink *GetSink(DWORD dwID);
  135. CSink *GetMainSink() { return &m_sinkMain; }
  136. void RemoveSink(CSink *pSink);
  137. protected:
  138. typedef std::map<DWORD, CSink*, std::less<DWORD>, wbem_allocator<CSink*> > CSinkMap;
  139. typedef CSinkMap::iterator CSinkMapIterator;
  140. CSink m_sinkMain;
  141. CSinkMap m_mapSink;
  142. DWORD m_dwNextSinkID;
  143. CBuffer m_bufferSend;
  144. BOOL StartProviderReadyThread();
  145. BOOL StartSendThread();
  146. void StopSendThread();
  147. BOOL SendDataOverTransports(LPBYTE pBuffer, DWORD dwSize);
  148. static DWORD WINAPI SendThreadProc(CConnection *pThis);
  149. static void GetBaseName(LPCWSTR szName, LPWSTR szBase);
  150. BOOL StartWaitWMIInitThread();
  151. BOOL InitTransport();
  152. static DWORD WINAPI WaitWMIInitThreadProc(CConnection *pThis);
  153. };
  154. class CInCritSec
  155. {
  156. public:
  157. CInCritSec(CRITICAL_SECTION *pCS)
  158. {
  159. EnterCriticalSection(pCS);
  160. m_pCS = pCS;
  161. }
  162. ~CInCritSec()
  163. {
  164. LeaveCriticalSection(m_pCS);
  165. }
  166. protected:
  167. CRITICAL_SECTION *m_pCS;
  168. };
  169. class CCondInCritSec
  170. {
  171. public:
  172. CCondInCritSec(CRITICAL_SECTION *pCS, BOOL bDoLock)
  173. {
  174. if (bDoLock)
  175. {
  176. EnterCriticalSection(pCS);
  177. m_pCS = pCS;
  178. }
  179. else
  180. m_pCS = NULL;
  181. }
  182. ~CCondInCritSec()
  183. {
  184. if (m_pCS)
  185. LeaveCriticalSection(m_pCS);
  186. }
  187. protected:
  188. CRITICAL_SECTION *m_pCS;
  189. };
  190. // SDDL string description:
  191. // D: Security Descriptor
  192. // A: Access allowed
  193. // 0x1f0003: EVENT_ALL_ACCESS
  194. // BA: Built-in administrators
  195. // 0x100000: SYNCHRONIZE
  196. // WD: Everyone
  197. #define ESS_EVENT_SDDL L"D:(A;;0x1f0003;;;BA)(A;;0x100000;;;WD)"
  198. // Security helper
  199. BOOL GetRelativeSD(
  200. SECURITY_DESCRIPTOR *pSDIn,
  201. SECURITY_DESCRIPTOR **ppSDOut,
  202. BOOL *pbFree);