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.

257 lines
6.6 KiB

  1. /*****************************************************************************\
  2. Author: Corey Morgan (coreym)
  3. Copyright (c) Microsoft Corporation. All rights reserved.
  4. \*****************************************************************************/
  5. #include <FWcommon.h>
  6. #include <objbase.h>
  7. #include <initguid.h>
  8. #include <strsafe.h>
  9. HMODULE ghModule;
  10. WCHAR *EVENTTRACE_GUIDSTRING = L"{9a5dd473-d410-11d1-b829-00c04f94c7c3}";
  11. WCHAR *SYSMONLOG_GUIDSTRING = L"{f95e1664-7979-44f2-a040-496e7f500043}";
  12. CLSID CLSID_CIM_EVENTTRACE;
  13. CLSID CLSID_CIM_SYSMONLOG;
  14. long g_cLock=0;
  15. EXTERN_C BOOL LibMain32(HINSTANCE hInstance, ULONG ulReason
  16. , LPVOID pvReserved)
  17. {
  18. if (DLL_PROCESS_ATTACH==ulReason)
  19. ghModule = hInstance;
  20. return TRUE;
  21. }
  22. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  23. {
  24. HRESULT hr;
  25. CWbemGlueFactory *pObj;
  26. CLSIDFromString(EVENTTRACE_GUIDSTRING, &CLSID_CIM_EVENTTRACE );
  27. CLSIDFromString(SYSMONLOG_GUIDSTRING, &CLSID_CIM_SYSMONLOG );
  28. if( CLSID_CIM_EVENTTRACE != rclsid && CLSID_CIM_SYSMONLOG != rclsid ){
  29. return E_FAIL;
  30. }
  31. pObj= new CWbemGlueFactory();
  32. if( NULL==pObj ){
  33. return E_OUTOFMEMORY;
  34. }
  35. hr=pObj->QueryInterface(riid, ppv);
  36. if( FAILED(hr) ){
  37. delete pObj;
  38. }
  39. return hr;
  40. }
  41. STDAPI DllCanUnloadNow(void)
  42. {
  43. SCODE sc;
  44. if( (0L==g_cLock) &&
  45. CWbemProviderGlue::FrameworkLogoffDLL(L"EventTraceProv") &&
  46. CWbemProviderGlue::FrameworkLogoffDLL(L"SmonLogProv")){
  47. sc = S_OK;
  48. }else{
  49. sc = S_FALSE;
  50. }
  51. return sc;
  52. }
  53. BOOL Is4OrMore(void)
  54. {
  55. OSVERSIONINFO os;
  56. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  57. if(!GetVersionEx(&os)){
  58. return FALSE;
  59. }
  60. return os.dwMajorVersion >= 4;
  61. }
  62. STDAPI DllRegisterServer(void)
  63. {
  64. HRESULT hr;
  65. DWORD dwStatus = ERROR_SUCCESS;
  66. const size_t cchCLSID = 512;
  67. WCHAR szCLSID[cchCLSID];
  68. LPCWSTR szModule = L"%systemroot%\\system32\\wbem\\evntrprv.dll";
  69. LPWSTR pName;
  70. LPWSTR pModel = L"Both";
  71. HKEY hKey1 = NULL;
  72. HKEY hKey2 = NULL;
  73. // Event Trace Provider
  74. pName = L"Event Trace Logger Provider";
  75. hr = StringCchCopy( szCLSID, cchCLSID, L"SOFTWARE\\CLASSES\\CLSID\\" );
  76. if( FAILED(hr) ){ goto cleanup; }
  77. hr = StringCchCat( szCLSID, cchCLSID, EVENTTRACE_GUIDSTRING );
  78. if( FAILED(hr) ){ goto cleanup; }
  79. dwStatus = RegCreateKeyW(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  80. if( ERROR_SUCCESS != dwStatus ){
  81. goto cleanup;
  82. }
  83. RegSetValueExW(hKey1, NULL, 0, REG_SZ, (BYTE *)pName, (wcslen(pName)+1)*sizeof(WCHAR));
  84. dwStatus = RegCreateKeyW(hKey1, L"InprocServer32", &hKey2 );
  85. if( ERROR_SUCCESS != dwStatus ){
  86. goto cleanup;
  87. }
  88. RegSetValueExW(hKey2, NULL, 0, REG_EXPAND_SZ, (BYTE *)szModule, (wcslen(szModule)+1)*sizeof(WCHAR));
  89. RegSetValueExW(hKey2, L"ThreadingModel", 0, REG_SZ, (BYTE *)pModel, (wcslen(pModel)+1)*sizeof(WCHAR));
  90. if( NULL != hKey1 ){
  91. RegCloseKey(hKey1);
  92. hKey1 = NULL;
  93. }
  94. if( NULL != hKey2 ){
  95. RegCloseKey(hKey2);
  96. hKey2 = NULL;
  97. }
  98. // Sysmon Log Provider
  99. pName = L"System Log Provider";
  100. hr = StringCchCopy( szCLSID, cchCLSID, L"SOFTWARE\\CLASSES\\CLSID\\" );
  101. if( FAILED(hr) ){ goto cleanup; }
  102. hr = StringCchCat( szCLSID, cchCLSID, SYSMONLOG_GUIDSTRING );
  103. if( FAILED(hr) ){ goto cleanup; }
  104. dwStatus = RegCreateKeyW(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  105. if( ERROR_SUCCESS != dwStatus ){
  106. goto cleanup;
  107. }
  108. RegSetValueExW(hKey1, NULL, 0, REG_SZ, (BYTE *)pName, (wcslen(pName)+1)*sizeof(WCHAR));
  109. dwStatus = RegCreateKeyW(hKey1, L"InprocServer32", &hKey2 );
  110. if( ERROR_SUCCESS != dwStatus ){
  111. goto cleanup;
  112. }
  113. RegSetValueExW(hKey2, NULL, 0, REG_EXPAND_SZ, (BYTE *)szModule, (wcslen(szModule)+1)*sizeof(WCHAR));
  114. RegSetValueExW(hKey2, L"ThreadingModel", 0, REG_SZ, (BYTE *)pModel, (wcslen(pModel)+1)*sizeof(WCHAR));
  115. cleanup:
  116. if( NULL != hKey1 ){
  117. RegCloseKey(hKey1);
  118. }
  119. if( NULL != hKey2 ){
  120. RegCloseKey(hKey2);
  121. }
  122. if( FAILED(hr) ){
  123. dwStatus = hr;
  124. }
  125. return dwStatus;
  126. }
  127. STDAPI DllUnregisterServer(void)
  128. {
  129. HRESULT hr;
  130. const size_t cchSize = 128;
  131. WCHAR wcID[cchSize];
  132. WCHAR szCLSID[cchSize];
  133. HKEY hKey;
  134. // Event Trace Provider
  135. CLSIDFromString(EVENTTRACE_GUIDSTRING, &CLSID_CIM_EVENTTRACE);
  136. StringFromGUID2(CLSID_CIM_EVENTTRACE, wcID, cchSize);
  137. hr = StringCchCopy( szCLSID, cchSize, L"SOFTWARE\\CLASSES\\CLSID\\");
  138. if( FAILED(hr) ){ goto cleanup; }
  139. hr = StringCchCat( szCLSID, cchSize, wcID);
  140. if( FAILED(hr) ){ goto cleanup; }
  141. DWORD dwRet = RegOpenKeyW(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  142. if( dwRet == NO_ERROR ){
  143. RegDeleteKeyW(hKey, L"InProcServer32" );
  144. RegCloseKey(hKey);
  145. }
  146. dwRet = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\CLASSES\\CLSID\\", &hKey);
  147. if(dwRet == NO_ERROR){
  148. RegDeleteKeyW(hKey,wcID);
  149. RegCloseKey(hKey);
  150. }
  151. // System Log Provider
  152. CLSIDFromString(SYSMONLOG_GUIDSTRING, &CLSID_CIM_SYSMONLOG);
  153. StringFromGUID2(CLSID_CIM_SYSMONLOG, wcID, cchSize);
  154. hr = StringCchCopy( szCLSID, cchSize, L"SOFTWARE\\CLASSES\\CLSID\\");
  155. if( FAILED(hr) ){ goto cleanup; }
  156. hr = StringCchCat( szCLSID, cchSize, wcID);
  157. if( FAILED(hr) ){ goto cleanup; }
  158. dwRet = RegOpenKeyW(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  159. if( dwRet == NO_ERROR ){
  160. RegDeleteKeyW(hKey, L"InProcServer32" );
  161. RegCloseKey(hKey);
  162. }
  163. dwRet = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\CLASSES\\CLSID\\", &hKey);
  164. if(dwRet == NO_ERROR){
  165. RegDeleteKeyW(hKey,wcID);
  166. RegCloseKey(hKey);
  167. }
  168. cleanup:
  169. return NOERROR;
  170. }
  171. BOOL APIENTRY DllMain ( HINSTANCE hInstDLL,
  172. DWORD fdwReason,
  173. LPVOID lpReserved )
  174. {
  175. BOOL bRet = TRUE;
  176. switch( fdwReason ){
  177. case DLL_PROCESS_ATTACH:
  178. DisableThreadLibraryCalls(hInstDLL);
  179. ghModule = hInstDLL;
  180. bRet = CWbemProviderGlue::FrameworkLoginDLL(L"EventTraceProv");
  181. break;
  182. case DLL_THREAD_ATTACH:
  183. // Do thread-specific initialization.
  184. break;
  185. case DLL_THREAD_DETACH:
  186. // Do thread-specific cleanup.
  187. break;
  188. case DLL_PROCESS_DETACH:
  189. // Perform any necessary cleanup.
  190. break;
  191. }
  192. return bRet;
  193. }