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.

549 lines
17 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: WBEM NT EVENT PROVIDER
  6. //
  7. // Purpose: Contains the gloabal dll functions
  8. //
  9. // Copyright (c) 1996-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #include <olectl.h>
  14. //OK we need these globals
  15. HINSTANCE g_hInst = NULL;
  16. CEventProviderManager* g_pMgr = NULL;
  17. CCriticalSection g_ProvLock;
  18. ProvDebugLog* CNTEventProvider::g_NTEvtDebugLog = ProvDebugLog::GetProvDebugLog(LOG_EVTPROV);
  19. CDllMap CEventlogRecord::sm_dllMap;
  20. CSIDMap CEventlogRecord::sm_usersMap;
  21. CMutex* CNTEventProvider::g_secMutex = NULL;
  22. PSID CNTEventProvider::s_NetworkServiceSid = NULL;
  23. PSID CNTEventProvider::s_LocalServiceSid = NULL;
  24. PSID CNTEventProvider::s_AliasBackupOpsSid = NULL;
  25. PSID CNTEventProvider::s_AliasSystemOpsSid = NULL;
  26. PSID CNTEventProvider::s_AliasGuestsSid = NULL;
  27. PSID CNTEventProvider::s_LocalSystemSid = NULL;
  28. PSID CNTEventProvider::s_AliasAdminsSid = NULL;
  29. PSID CNTEventProvider::s_AnonymousLogonSid = NULL;
  30. PSID CNTEventProvider::s_WorldSid = NULL;
  31. IWbemClassObject *WbemTaskObject::g_ClassArray[] = { NULL, NULL, NULL, NULL, NULL };
  32. //***************************************************************************
  33. //
  34. // LibMain32
  35. //
  36. // Purpose: Entry point for DLL. Good place for initialization.
  37. // Return: TRUE if OK.
  38. //***************************************************************************
  39. BOOL APIENTRY DllMain (
  40. HINSTANCE hInstance,
  41. ULONG ulReason ,
  42. LPVOID pvReserved
  43. )
  44. {
  45. SetStructuredExceptionHandler seh;
  46. BOOL status = TRUE ;
  47. try
  48. {
  49. if ( DLL_PROCESS_DETACH == ulReason )
  50. {
  51. }
  52. else if ( DLL_PROCESS_ATTACH == ulReason )
  53. {
  54. g_hInst=hInstance;
  55. DisableThreadLibraryCalls(hInstance);
  56. }
  57. else if ( DLL_THREAD_DETACH == ulReason )
  58. {
  59. }
  60. else if ( DLL_THREAD_ATTACH == ulReason )
  61. {
  62. }
  63. }
  64. catch(Structured_Exception e_SE)
  65. {
  66. status = FALSE;
  67. }
  68. catch(Heap_Exception e_HE)
  69. {
  70. status = FALSE;
  71. }
  72. catch(...)
  73. {
  74. status = FALSE;
  75. }
  76. return status;
  77. }
  78. //***************************************************************************
  79. //
  80. // DllGetClassObject
  81. //
  82. // Purpose: Called by Ole when some client wants a a class factory. Return
  83. // one only if it is the sort of class this DLL supports.
  84. //
  85. //***************************************************************************
  86. STDAPI DllGetClassObject (
  87. REFCLSID rclsid ,
  88. REFIID riid,
  89. void **ppv
  90. )
  91. {
  92. HRESULT status = S_OK ;
  93. SetStructuredExceptionHandler seh;
  94. try
  95. {
  96. if (g_ProvLock.Lock())
  97. {
  98. if ( rclsid == CLSID_CNTEventProviderClassFactory )
  99. {
  100. CNTEventlogEventProviderClassFactory *lpunk = new CNTEventlogEventProviderClassFactory;
  101. if ( lpunk == NULL )
  102. {
  103. status = E_OUTOFMEMORY ;
  104. }
  105. else
  106. {
  107. status = lpunk->QueryInterface ( riid , ppv ) ;
  108. if ( FAILED ( status ) )
  109. {
  110. delete lpunk ;
  111. }
  112. }
  113. }
  114. else if ( rclsid == CLSID_CNTEventInstanceProviderClassFactory )
  115. {
  116. CNTEventlogInstanceProviderClassFactory *lpunk = new CNTEventlogInstanceProviderClassFactory;
  117. if ( lpunk == NULL )
  118. {
  119. status = E_OUTOFMEMORY ;
  120. }
  121. else
  122. {
  123. status = lpunk->QueryInterface ( riid , ppv ) ;
  124. if ( FAILED ( status ) )
  125. {
  126. delete lpunk ;
  127. }
  128. }
  129. }
  130. else
  131. {
  132. status = CLASS_E_CLASSNOTAVAILABLE ;
  133. }
  134. g_ProvLock.Unlock();
  135. }
  136. else
  137. {
  138. status = E_UNEXPECTED ;
  139. }
  140. }
  141. catch(Structured_Exception e_SE)
  142. {
  143. status = E_UNEXPECTED;
  144. }
  145. catch(Heap_Exception e_HE)
  146. {
  147. status = E_OUTOFMEMORY;
  148. }
  149. catch(...)
  150. {
  151. status = E_UNEXPECTED;
  152. }
  153. return status ;
  154. }
  155. //***************************************************************************
  156. //
  157. // DllCanUnloadNow
  158. //
  159. // Purpose: Called periodically by Ole in order to determine if the
  160. // DLL can be freed.//
  161. // Return: TRUE if there are no objects in use and the class factory
  162. // isn't locked.
  163. //***************************************************************************
  164. STDAPI DllCanUnloadNow ()
  165. {
  166. /*
  167. * Place code in critical section
  168. */
  169. BOOL unload = FALSE;
  170. HRESULT status = S_OK ;
  171. SetStructuredExceptionHandler seh;
  172. try
  173. {
  174. if (g_ProvLock.Lock())
  175. {
  176. unload = (0 == CNTEventProviderClassFactory :: locksInProgress)
  177. && (0 == CNTEventProviderClassFactory :: objectsInProgress);
  178. if (unload)
  179. {
  180. for (DWORD i = 0; i < NT_EVTLOG_MAX_CLASSES; i++)
  181. {
  182. if (WbemTaskObject::g_ClassArray[i])
  183. {
  184. WbemTaskObject::g_ClassArray[i]->Release();
  185. WbemTaskObject::g_ClassArray[i] = NULL;
  186. }
  187. }
  188. CEventlogRecord::EmptyDllMap();
  189. CEventlogRecord::EmptyUsersMap();
  190. delete g_pMgr;
  191. g_pMgr = NULL;
  192. CNTEventProvider::FreeGlobalSIDs();
  193. if (CNTEventProvider::g_secMutex != NULL)
  194. {
  195. delete CNTEventProvider::g_secMutex;
  196. CNTEventProvider::g_secMutex = NULL;
  197. }
  198. }
  199. g_ProvLock.Unlock();
  200. }
  201. }
  202. catch(Structured_Exception e_SE)
  203. {
  204. unload = FALSE;
  205. }
  206. catch(Heap_Exception e_HE)
  207. {
  208. unload = FALSE;
  209. }
  210. catch(...)
  211. {
  212. unload = FALSE;
  213. }
  214. return unload ? ResultFromScode ( S_OK ) : ResultFromScode ( S_FALSE ) ;
  215. }
  216. //Strings used during self registeration
  217. #define REG_FORMAT2_STR L"%s%s"
  218. #define REG_FORMAT3_STR L"%s%s\\%s"
  219. #define VER_IND_STR L"VersionIndependentProgID"
  220. #define NOT_INTERT_STR L"NotInsertable"
  221. #define INPROC32_STR L"InprocServer32"
  222. #define PROGID_STR L"ProgID"
  223. #define THREADING_MODULE_STR L"ThreadingModel"
  224. #define APARTMENT_STR L"Both"
  225. #define CLSID_STR L"CLSID\\"
  226. #define PROVIDER_NAME_STR L"Microsoft WBEM NT Eventlog Event Provider"
  227. #define PROVIDER_STR L"WBEM.NT.EVENTLOG.EVENT.PROVIDER"
  228. #define H_PROVIDER_STR L"SOFTWARE\\Classes\\WBEM.NT.EVENTLOG.EVENT.PROVIDER"
  229. #define PROVIDER_CVER_STR L"SOFTWARE\\Classes\\WBEM.NT.EVENTLOG.EVENT.PROVIDER\\CurVer"
  230. #define PROVIDER_CLSID_STR L"SOFTWARE\\Classes\\WBEM.NT.EVENTLOG.EVENT.PROVIDER\\CLSID"
  231. #define PROVIDER_VER_CLSID_STR L"WBEM.NT.EVENTLOG.EVENT.PROVIDER.0\\CLSID"
  232. #define PROVIDER_VER_STR L"WBEM.NT.EVENTLOG.EVENT.PROVIDER.0"
  233. #define H_PROVIDER_VER_STR L"SOFTWARE\\Classes\\WBEM.NT.EVENTLOG.EVENT.PROVIDER.0"
  234. #define INST_PROVIDER_NAME_STR L"Microsoft WBEM NT Eventlog Instance Provider"
  235. #define INST_PROVIDER_STR L"WBEM.NT.EVENTLOG.INSTANCE.PROVIDER"
  236. #define H_INST_PROVIDER_STR L"SOFTWARE\\Classes\\WBEM.NT.EVENTLOG.INSTANCE.PROVIDER"
  237. #define INST_PROVIDER_CVER_STR L"SOFTWARE\\Classes\\WBEM.NT.EVENTLOG.INSTANCE.PROVIDER\\CurVer"
  238. #define INST_PROVIDER_CLSID_STR L"SOFTWARE\\Classes\\WBEM.NT.EVENTLOG.INSTANCE.PROVIDER\\CLSID"
  239. #define INST_PROVIDER_VER_CLSID_STR L"SOFTWARE\\Classes\\WBEM.NT.EVENTLOG.INSTANCE.PROVIDER.0\\CLSID"
  240. #define INST_PROVIDER_VER_STR L"WBEM.NT.EVENTLOG.INSTANCE.PROVIDER.0"
  241. #define H_INST_PROVIDER_VER_STR L"SOFTWARE\\Classes\\WBEM.NT.EVENTLOG.INSTANCE.PROVIDER.0"
  242. /***************************************************************************
  243. * SetKeyAndValue
  244. *
  245. * Purpose:
  246. * Private helper function for DllRegisterServer that creates
  247. * a key, sets a value, and closes that key.
  248. *
  249. * Parameters:
  250. * pszKey LPTSTR to the ame of the key
  251. * pszSubkey LPTSTR ro the name of a subkey
  252. * pszValue LPTSTR to the value to store
  253. *
  254. * Return Value:
  255. * BOOL TRUE if successful, FALSE otherwise.
  256. ***************************************************************************/
  257. BOOL SetKeyAndValue(wchar_t* pszKey, wchar_t* pszSubkey, wchar_t* pszValueName, wchar_t* pszValue)
  258. {
  259. HKEY hKey;
  260. wchar_t szKey[256];
  261. StringCchCopyW ( szKey, 256, HKEYCLASSES );
  262. if ( FAILED ( StringCchCatW ( szKey, 256, pszKey ) ) )
  263. {
  264. return FALSE;
  265. }
  266. if (NULL!=pszSubkey)
  267. {
  268. if ( FAILED ( StringCchCatW ( szKey, 256, L"\\") ) )
  269. {
  270. return FALSE;
  271. }
  272. if ( FAILED ( StringCchCatW ( szKey, 256, pszSubkey ) ) )
  273. {
  274. return FALSE;
  275. }
  276. }
  277. if (ERROR_SUCCESS!=RegCreateKeyEx(HKEY_LOCAL_MACHINE
  278. , szKey, 0, NULL, REG_OPTION_NON_VOLATILE
  279. , KEY_ALL_ACCESS, NULL, &hKey, NULL))
  280. return FALSE;
  281. if (NULL!=pszValue)
  282. {
  283. if (ERROR_SUCCESS != RegSetValueEx(hKey, pszValueName, 0, REG_SZ, (BYTE *)pszValue
  284. , (lstrlen(pszValue)+1)*sizeof(wchar_t)))
  285. return FALSE;
  286. }
  287. RegCloseKey(hKey);
  288. return TRUE;
  289. }
  290. /***************************************************************************
  291. * DllRegisterServer
  292. *
  293. * Purpose:
  294. * Instructs the server to create its own registry entries
  295. *
  296. * Parameters:
  297. * None
  298. *
  299. * Return Value:
  300. * HRESULT NOERROR if registration successful, error
  301. * otherwise.
  302. ***************************************************************************/
  303. STDAPI DllRegisterServer()
  304. {
  305. HRESULT status = S_OK ;
  306. SetStructuredExceptionHandler seh;
  307. try
  308. {
  309. wchar_t szModule[MAX_PATH + 1];
  310. DWORD t_Status = GetModuleFileName(g_hInst,(wchar_t*)szModule, MAX_PATH + 1);
  311. if ( t_Status == 0 )
  312. {
  313. return E_UNEXPECTED ;
  314. }
  315. wchar_t szProviderClassID[128];
  316. wchar_t szProviderCLSIDClassID[128];
  317. StringFromGUID2(CLSID_CNTEventProviderClassFactory,szProviderClassID, 128);
  318. StringCchCopyW ( szProviderCLSIDClassID, 128, CLSID_STR );
  319. if ( FAILED ( StringCchCatW ( szProviderCLSIDClassID, 128, szProviderClassID ) ) )
  320. {
  321. return SELFREG_E_CLASS;
  322. }
  323. //Create entries under CLSID
  324. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NULL, NULL, PROVIDER_NAME_STR))
  325. return SELFREG_E_CLASS;
  326. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, PROGID_STR, NULL, PROVIDER_VER_STR))
  327. return SELFREG_E_CLASS;
  328. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, VER_IND_STR, NULL, PROVIDER_STR))
  329. return SELFREG_E_CLASS;
  330. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NOT_INTERT_STR, NULL, NULL))
  331. return SELFREG_E_CLASS;
  332. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR, NULL,szModule))
  333. return SELFREG_E_CLASS;
  334. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR,THREADING_MODULE_STR, APARTMENT_STR))
  335. return SELFREG_E_CLASS;
  336. wchar_t szInstProviderClassID[128];
  337. wchar_t szInstProviderCLSIDClassID[128];
  338. StringFromGUID2(CLSID_CNTEventInstanceProviderClassFactory,szInstProviderClassID, 128);
  339. StringCchCopyW ( szInstProviderCLSIDClassID, 128, CLSID_STR );
  340. if ( FAILED ( StringCchCatW ( szInstProviderCLSIDClassID, 128, szInstProviderClassID ) ) )
  341. {
  342. return SELFREG_E_CLASS;
  343. }
  344. //Create entries under CLSID
  345. if (FALSE ==SetKeyAndValue(szInstProviderCLSIDClassID, NULL, NULL, INST_PROVIDER_NAME_STR))
  346. return SELFREG_E_CLASS;
  347. if (FALSE ==SetKeyAndValue(szInstProviderCLSIDClassID, PROGID_STR, NULL, INST_PROVIDER_VER_STR))
  348. return SELFREG_E_CLASS;
  349. if (FALSE ==SetKeyAndValue(szInstProviderCLSIDClassID, VER_IND_STR, NULL, INST_PROVIDER_STR))
  350. return SELFREG_E_CLASS;
  351. if (FALSE ==SetKeyAndValue(szInstProviderCLSIDClassID, NOT_INTERT_STR, NULL, NULL))
  352. return SELFREG_E_CLASS;
  353. if (FALSE ==SetKeyAndValue(szInstProviderCLSIDClassID, INPROC32_STR, NULL,szModule))
  354. return SELFREG_E_CLASS;
  355. if (FALSE ==SetKeyAndValue(szInstProviderCLSIDClassID, INPROC32_STR,THREADING_MODULE_STR, APARTMENT_STR))
  356. return SELFREG_E_CLASS;
  357. }
  358. catch(Structured_Exception e_SE)
  359. {
  360. status = E_UNEXPECTED;
  361. }
  362. catch(Heap_Exception e_HE)
  363. {
  364. status = E_OUTOFMEMORY;
  365. }
  366. catch(...)
  367. {
  368. status = E_UNEXPECTED;
  369. }
  370. return status ;
  371. }
  372. /***************************************************************************
  373. * DllUnregisterServer
  374. *
  375. * Purpose:
  376. * Instructs the server to remove its own registry entries
  377. *
  378. * Parameters:
  379. * None
  380. *
  381. * Return Value:
  382. * HRESULT NOERROR if registration successful, error
  383. * otherwise.
  384. ***************************************************************************/
  385. STDAPI DllUnregisterServer(void)
  386. {
  387. HRESULT status = S_OK ;
  388. SetStructuredExceptionHandler seh;
  389. try
  390. {
  391. wchar_t szTemp[128];
  392. wchar_t szProviderClassID[128];
  393. wchar_t szProviderCLSIDClassID[128];
  394. //event provider
  395. StringFromGUID2(CLSID_CNTEventProviderClassFactory,szProviderClassID, 128);
  396. StringCchCopyW ( szProviderCLSIDClassID, 128, CLSID_STR );
  397. if ( FAILED ( StringCchCatW ( szProviderCLSIDClassID, 128, szProviderClassID ) ) )
  398. {
  399. return SELFREG_E_CLASS ;
  400. }
  401. //Delete ProgID keys
  402. RegDeleteKey(HKEY_LOCAL_MACHINE, PROVIDER_CVER_STR);
  403. RegDeleteKey(HKEY_LOCAL_MACHINE, PROVIDER_CLSID_STR);
  404. RegDeleteKey(HKEY_LOCAL_MACHINE, H_PROVIDER_STR);
  405. //Delete VersionIndependentProgID keys
  406. RegDeleteKey(HKEY_LOCAL_MACHINE, PROVIDER_VER_CLSID_STR);
  407. RegDeleteKey(HKEY_LOCAL_MACHINE, H_PROVIDER_VER_STR);
  408. //Delete entries under CLSID
  409. StringCchPrintf(szTemp, 128, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, PROGID_STR);
  410. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  411. StringCchPrintf(szTemp, 128, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, VER_IND_STR);
  412. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  413. StringCchPrintf(szTemp, 128, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, NOT_INTERT_STR);
  414. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  415. StringCchPrintf(szTemp, 128, REG_FORMAT3_STR, HKEYCLASSES, szProviderCLSIDClassID, INPROC32_STR);
  416. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  417. StringCchPrintf(szTemp, 128, REG_FORMAT2_STR, HKEYCLASSES, szProviderCLSIDClassID);
  418. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  419. wchar_t szInstProviderClassID[128];
  420. wchar_t szInstProviderCLSIDClassID[128];
  421. //instance provider
  422. StringFromGUID2(CLSID_CNTEventInstanceProviderClassFactory, szInstProviderClassID, 128);
  423. StringCchCopyW ( szInstProviderCLSIDClassID, 128, CLSID_STR );
  424. if ( FAILED ( StringCchCatW ( szInstProviderCLSIDClassID, 128, szInstProviderClassID ) ) )
  425. {
  426. return SELFREG_E_CLASS ;
  427. }
  428. //Delete ProgID keys
  429. RegDeleteKey(HKEY_LOCAL_MACHINE, INST_PROVIDER_CVER_STR);
  430. RegDeleteKey(HKEY_LOCAL_MACHINE, INST_PROVIDER_CLSID_STR);
  431. RegDeleteKey(HKEY_LOCAL_MACHINE, H_INST_PROVIDER_STR);
  432. //Delete VersionIndependentProgID keys
  433. RegDeleteKey(HKEY_LOCAL_MACHINE, INST_PROVIDER_VER_CLSID_STR);
  434. RegDeleteKey(HKEY_LOCAL_MACHINE, H_INST_PROVIDER_VER_STR);
  435. //Delete entries under CLSID
  436. StringCchPrintf(szTemp, 128, REG_FORMAT3_STR, HKEYCLASSES, szInstProviderCLSIDClassID, PROGID_STR);
  437. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  438. StringCchPrintf(szTemp, 128, REG_FORMAT3_STR, HKEYCLASSES, szInstProviderCLSIDClassID, VER_IND_STR);
  439. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  440. StringCchPrintf(szTemp, 128, REG_FORMAT3_STR, HKEYCLASSES, szInstProviderCLSIDClassID, NOT_INTERT_STR);
  441. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  442. StringCchPrintf(szTemp, 128, REG_FORMAT3_STR, HKEYCLASSES, szInstProviderCLSIDClassID, INPROC32_STR);
  443. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  444. StringCchPrintf(szTemp, 128, REG_FORMAT2_STR, HKEYCLASSES, szInstProviderCLSIDClassID);
  445. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  446. }
  447. catch(Structured_Exception e_SE)
  448. {
  449. status = E_UNEXPECTED;
  450. }
  451. catch(Heap_Exception e_HE)
  452. {
  453. status = E_OUTOFMEMORY;
  454. }
  455. catch(...)
  456. {
  457. status = E_UNEXPECTED;
  458. }
  459. return status ;
  460. }