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.

311 lines
11 KiB

  1. #include "stdafx.h"
  2. #include "utils.h"
  3. #include <atlbase.h>
  4. CComModule _Module;
  5. #include <atlcom.h>
  6. #include <atlimpl.cpp>
  7. #include "seo.h"
  8. #include "seolib.h"
  9. #include "seo_i.c"
  10. #define INITGUID
  11. #include <initguid.h>
  12. #include "smtpguid.h"
  13. #define STR_SMTP_NTFSDRV_DISPLAY_NAME "Exchange Ntfs Store Driver"
  14. #define STR_SMTP_NTFSDRV_SINKCLASS "Exchange.NtfsDrv"
  15. #define LONG_SMTP_NTFSDRV_PRIORITY 28000
  16. // {C028FD82-F943-11d0-85BD-00C04FB960EA}
  17. DEFINE_GUID(NNTP_SOURCE_TYPE_GUID,
  18. 0xc028fd82, 0xf943, 0x11d0, 0x85, 0xbd, 0x0, 0xc0, 0x4f, 0xb9, 0x60, 0xea);
  19. DEFINE_GUID(GUID_SMTP_NTFSDRV_BINDING,
  20. 0x609b7e3a, 0xc918, 0x11d1, 0xaa, 0x5e, 0x0, 0xc0, 0x4f, 0xa3, 0x5b, 0x82);
  21. HRESULT RegisterSEOService()
  22. {
  23. HRESULT hr;
  24. //
  25. // see if we've done the service level registration by getting the list
  26. // of source types and seeing if the SMTP source type is registered
  27. //
  28. CComPtr<IEventManager> pEventManager;
  29. hr = CoCreateInstance(CLSID_CEventManager, NULL, CLSCTX_ALL,
  30. IID_IEventManager, (LPVOID *) &pEventManager);
  31. if (hr != S_OK)
  32. return hr;
  33. CComPtr<IEventSourceTypes> pSourceTypes;
  34. hr = pEventManager->get_SourceTypes(&pSourceTypes);
  35. if (FAILED(hr))
  36. return hr;
  37. CComPtr<IEventSourceType> pSourceType;
  38. CComBSTR bstrSourceTypeGUID = (LPCOLESTR) CStringGUID(GUID_SMTP_SOURCE_TYPE);
  39. hr = pSourceTypes->Item(&CComVariant(bstrSourceTypeGUID), &pSourceType);
  40. if (FAILED(hr))
  41. return hr;
  42. // if this failed then we need to register the source type and event
  43. // component categories
  44. if (hr == S_FALSE)
  45. {
  46. // register the component categories
  47. CComPtr<IEventComCat> pComCat;
  48. hr = CoCreateInstance(CLSID_CEventComCat, NULL, CLSCTX_ALL,
  49. IID_IEventComCat, (LPVOID *) &pComCat);
  50. if (hr != S_OK)
  51. return hr;
  52. // register the source type
  53. hr = pSourceTypes->Add(bstrSourceTypeGUID, &pSourceType);
  54. if (FAILED(hr))
  55. return hr;
  56. _ASSERT(hr == S_OK);
  57. CComBSTR bstrSourceTypeDisplayName = "SMTP Server";
  58. hr = pSourceType->put_DisplayName(bstrSourceTypeDisplayName);
  59. if (FAILED(hr))
  60. return hr;
  61. hr = pSourceType->Save();
  62. if (FAILED(hr))
  63. return hr;
  64. // add the event types to the source type
  65. CComPtr<IEventTypes> pEventTypes;
  66. hr = pSourceType->get_EventTypes(&pEventTypes);
  67. if (FAILED(hr))
  68. return hr;
  69. //
  70. // Register the event categories
  71. //
  72. struct {
  73. CONST GUID * pcatid;
  74. LPSTR szDisplayName;
  75. } rgCATTable[] = {
  76. //
  77. // Protocol event categories
  78. //
  79. { &CATID_SMTP_ON_INBOUND_COMMAND, "SMTP Protocol OnInboundCommand" },
  80. { &CATID_SMTP_ON_SERVER_RESPONSE, "SMTP Protocol OnServerResponse" },
  81. { &CATID_SMTP_ON_SESSION_START, "SMTP Protocol OnSessionStart" },
  82. { &CATID_SMTP_ON_MESSAGE_START, "SMTP Protocol OnMessageStart" },
  83. { &CATID_SMTP_ON_PER_RECIPIENT, "SMTP Protocol OnPerRecipient" },
  84. { &CATID_SMTP_ON_BEFORE_DATA, "SMTP Protocol OnBeforeData" },
  85. { &CATID_SMTP_ON_SESSION_END, "SMTP Protocol OnSessionEnd" },
  86. { &CATID_SMTP_LOG, "SMTP OnEventLog" },
  87. //
  88. // Transport event categories
  89. //
  90. { &CATID_SMTP_STORE_DRIVER, "SMTP StoreDriver" },
  91. { &CATID_SMTP_TRANSPORT_SUBMISSION, "SMTP Transport OnSubmission" },
  92. { &CATID_SMTP_TRANSPORT_PRECATEGORIZE, "SMTP Transport OnPreCategorize" },
  93. { &CATID_SMTP_TRANSPORT_CATEGORIZE, "SMTP Transport OnCategorize" },
  94. { &CATID_SMTP_TRANSPORT_POSTCATEGORIZE, "SMTP Transport OnPostCategorize" },
  95. { &CATID_SMTP_TRANSPORT_ROUTER, "SMTP Transport OnGetMessageRouter" },
  96. { &CATID_SMTP_MSGTRACKLOG, "SMTP Transport OnMsgTrackLog" },
  97. { &CATID_SMTP_DNSRESOLVERRECORDSINK, "SMTP Transport OnDnsResolveRecord" },
  98. { &CATID_SMTP_MAXMSGSIZE, "SMTP Transport OnMaxMsgSize" },
  99. { &CATID_SMTP_GET_AUX_DOMAIN_INFO_FLAGS, "SMTP Transport GetAuxiliaryDomainInfoFlags" }
  100. };
  101. for(DWORD dwCount = 0;
  102. dwCount < (sizeof(rgCATTable)/sizeof(rgCATTable[0]));
  103. dwCount++) {
  104. CComBSTR bstrCATID = (LPCOLESTR) CStringGUID( *(rgCATTable[dwCount].pcatid) );
  105. CComBSTR bstrDisplayName = rgCATTable[dwCount].szDisplayName;
  106. //
  107. // Register the category
  108. //
  109. hr = pComCat->RegisterCategory( bstrCATID, bstrDisplayName, 0);
  110. if(FAILED(hr))
  111. return hr;
  112. //
  113. // Add the category to the SMTP source type
  114. //
  115. hr = pEventTypes->Add( bstrCATID );
  116. if(FAILED(hr))
  117. return hr;
  118. }
  119. }
  120. return S_OK;
  121. }
  122. HRESULT pRegisterSEOForSmtp(BOOL fSetUpSourceType)
  123. {
  124. HRESULT hr;
  125. CComPtr<IEventUtil> pEventUtil;
  126. TCHAR szDisplayName[32];
  127. CComPtr<IEventBindingManager> pBindingManager;
  128. CComPtr<IEventBindings> pBindings;
  129. CComPtr<IEventBinding> pBinding;
  130. CComPtr<IEventPropertyBag> pSourceProps;
  131. DebugOutput(_T("Registering Server Events"));
  132. // Register the source type, event types
  133. if (fSetUpSourceType)
  134. {
  135. DebugOutput(_T("Setting up source and event types"));
  136. hr = RegisterSEOService();
  137. }
  138. // Set up the default site (instance)
  139. lstrcpy(szDisplayName,_T("smtpsvc 1"));
  140. hr = CoCreateInstance(CLSID_CEventUtil,NULL,CLSCTX_ALL,IID_IEventUtil,(LPVOID *) &pEventUtil);
  141. if (FAILED(hr)) return(hr);
  142. hr = pEventUtil->RegisterSource(CComBSTR((LPCWSTR) CStringGUID(GUID_SMTP_SOURCE_TYPE)),
  143. CComBSTR((LPCWSTR) CStringGUID(GUID_SMTPSVC_SOURCE)),
  144. 1,
  145. CComBSTR(_T("smtpsvc")),
  146. CComBSTR(_T("")),
  147. CComBSTR(_T("event.metabasedatabasemanager")),
  148. CComBSTR(szDisplayName),
  149. &pBindingManager);
  150. if (FAILED(hr)) goto Exit;
  151. hr = pBindingManager->get_Bindings(CComBSTR((LPCWSTR) CStringGUID(CATID_SMTP_STORE_DRIVER)),
  152. &pBindings);
  153. if (FAILED(hr)) goto Exit;
  154. // Set up the NTFS driver sink
  155. hr = pBindings->Add(CComBSTR((LPCWSTR) CStringGUID(GUID_SMTP_NTFSDRV_BINDING)),&pBinding);
  156. if (FAILED(hr)) goto Exit;
  157. hr = pBinding->put_DisplayName(CComBSTR(STR_SMTP_NTFSDRV_DISPLAY_NAME));
  158. if (FAILED(hr)) goto Exit;
  159. hr = pBinding->put_SinkClass(CComBSTR(STR_SMTP_NTFSDRV_SINKCLASS));
  160. if (FAILED(hr)) goto Exit;
  161. hr = pBinding->get_SourceProperties(&pSourceProps);
  162. if (FAILED(hr)) goto Exit;
  163. hr = pSourceProps->Add(CComBSTR(_T("priority")),&CComVariant(LONG_SMTP_NTFSDRV_PRIORITY));
  164. if (FAILED(hr)) goto Exit;
  165. hr = pBinding->Save();
  166. if (FAILED(hr)) goto Exit;
  167. // hr = pBindingManager->get_Bindings(CComBSTR((LPCWSTR) CStringGUID(CATID_SMTP_ON_DELIVERY)),
  168. // &pBindings);
  169. Exit:
  170. return(hr);
  171. }
  172. HRESULT RegisterSEOForSmtp(BOOL fSetUpSourceType)
  173. {
  174. HRESULT hr;
  175. hr = CoInitialize(NULL);
  176. if (FAILED(hr))
  177. {
  178. DebugOutput(_T("Cannot CoInitialize()"));
  179. return(hr);
  180. }
  181. hr = pRegisterSEOForSmtp(fSetUpSourceType);
  182. CoUninitialize();
  183. return(hr);
  184. }
  185. HRESULT UnregisterSEOSourcesForSourceType(GUID guidSourceType) {
  186. HRESULT hr;
  187. //
  188. // find the NNTP source type in the event manager
  189. //
  190. CComPtr<IEventManager> pEventManager;
  191. hr = CoCreateInstance(CLSID_CEventManager, NULL, CLSCTX_ALL,
  192. IID_IEventManager, (LPVOID *) &pEventManager);
  193. if (hr != S_OK) return hr;
  194. CComPtr<IEventSourceTypes> pSourceTypes;
  195. hr = pEventManager->get_SourceTypes(&pSourceTypes);
  196. if (FAILED(hr)) return hr;
  197. CComPtr<IEventSourceType> pSourceType;
  198. CComBSTR bstrSourceTypeGUID = (LPCOLESTR) CStringGUID(guidSourceType);
  199. hr = pSourceTypes->Item(&CComVariant(bstrSourceTypeGUID), &pSourceType);
  200. _ASSERT(hr != S_OK || pSourceType != NULL);
  201. if (hr != S_OK) return hr;
  202. //
  203. // get the list of sources registered for this source type
  204. //
  205. CComPtr<IEventSources> pSources;
  206. hr = pSourceType->get_Sources(&pSources);
  207. if (FAILED(hr)) return hr;
  208. CComPtr<IEnumVARIANT> pSourceEnum;
  209. hr = pSources->get__NewEnum((IUnknown **) &pSourceEnum);
  210. if (FAILED(hr)) return hr;
  211. do {
  212. VARIANT varSource;
  213. hr = pSourceEnum->Next(1, &varSource, NULL);
  214. if (FAILED(hr)) return hr;
  215. if (hr == S_OK) {
  216. if (varSource.vt == VT_DISPATCH) {
  217. CComPtr<IEventSource> pSource;
  218. // QI for the IEventSource interface
  219. hr = varSource.punkVal->QueryInterface(IID_IEventSource,
  220. (void **) &pSource);
  221. if (FAILED(hr)) return hr;
  222. varSource.punkVal->Release();
  223. // get the binding manager
  224. CComBSTR bstrSourceID;
  225. hr = pSource->get_ID(&bstrSourceID);
  226. if (FAILED(hr)) return hr;
  227. hr = pSources->Remove(&CComVariant(bstrSourceID));
  228. _ASSERT(SUCCEEDED(hr));
  229. pSource.Release();
  230. } else {
  231. _ASSERT(FALSE);
  232. }
  233. }
  234. } while (hr == S_OK);
  235. return S_OK;
  236. }
  237. HRESULT UnregisterSEOSourcesForSMTP(void) {
  238. HRESULT hr;
  239. hr = CoInitialize(NULL);
  240. if (FAILED(hr))
  241. {
  242. DebugOutput(_T("Cannot CoInitialize()"));
  243. return(hr);
  244. }
  245. hr = UnregisterSEOSourcesForSourceType(GUID_SMTP_SOURCE_TYPE);
  246. CoUninitialize();
  247. return hr;
  248. }
  249. HRESULT UnregisterSEOSourcesForNNTP(void) {
  250. HRESULT hr;
  251. hr = CoInitialize(NULL);
  252. if (FAILED(hr))
  253. {
  254. DebugOutput(_T("Cannot CoInitialize()"));
  255. return(hr);
  256. }
  257. hr = UnregisterSEOSourcesForSourceType(NNTP_SOURCE_TYPE_GUID);
  258. CoUninitialize();
  259. return hr;
  260. }