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.

624 lines
14 KiB

  1. //***************************************************************************
  2. //
  3. // File:
  4. //
  5. // Module: MS SNMP Provider
  6. //
  7. // Purpose:
  8. //
  9. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #include <provexpt.h>
  14. #include <objbase.h>
  15. #include <olectl.h>
  16. #include <corafx.h>
  17. #include <wbemidl.h>
  18. #include "guids.h"
  19. #include <snmpstd.h>
  20. #include <snmpmt.h>
  21. #include <snmptempl.h>
  22. #include <snmpcont.h>
  23. #include <snmpevt.h>
  24. #include <snmpthrd.h>
  25. #include <snmplog.h>
  26. #include <instpath.h>
  27. #include <snmpcl.h>
  28. #include <snmptype.h>
  29. #include <snmpobj.h>
  30. #include <smir.h>
  31. #include "classfac.h"
  32. #include "clasprov.h"
  33. #include "propprov.h"
  34. #include <corstore.h>
  35. #include <corrsnmp.h>
  36. #include <correlat.h>
  37. #include <notify.h>
  38. #include <cormap.h>
  39. #include <evtdefs.h>
  40. #include <evtthrd.h>
  41. #include <evtmap.h>
  42. #include <evtprov.h>
  43. ISmirDatabase* g_pNotifyInt = NULL;
  44. CCorrCacheNotify* gp_notify = NULL;
  45. CCorrCacheWrapper* g_CacheWrapper = NULL;
  46. CCorrelatorMap* g_Map = NULL;
  47. //OK we need this one
  48. HINSTANCE g_hInst=NULL;
  49. CEventProviderThread* g_pProvThrd = NULL;
  50. CEventProviderWorkerThread* g_pWorkerThread = NULL;
  51. CRITICAL_SECTION s_ProviderCriticalSection ;
  52. CRITICAL_SECTION g_CacheCriticalSection ;
  53. extern void ProviderStartup () ;
  54. extern void ProviderClosedown () ;
  55. //***************************************************************************
  56. //
  57. // LibMain32
  58. //
  59. // Purpose: Entry point for DLL. Good place for initialization.
  60. // Return: TRUE if OK.
  61. //***************************************************************************
  62. BOOL APIENTRY DllMain (
  63. HINSTANCE hInstance,
  64. ULONG ulReason ,
  65. LPVOID pvReserved
  66. )
  67. {
  68. SetStructuredExceptionHandler seh;
  69. try
  70. {
  71. g_hInst=hInstance;
  72. BOOL status = TRUE ;
  73. if ( DLL_PROCESS_DETACH == ulReason )
  74. {
  75. DeleteCriticalSection ( & s_ProviderCriticalSection ) ;
  76. DeleteCriticalSection ( & g_CacheCriticalSection ) ;
  77. status = TRUE ;
  78. }
  79. else if ( DLL_PROCESS_ATTACH == ulReason )
  80. {
  81. DisableThreadLibraryCalls(hInstance);
  82. InitializeCriticalSection ( & s_ProviderCriticalSection ) ;
  83. InitializeCriticalSection ( & g_CacheCriticalSection ) ;
  84. status = TRUE ;
  85. }
  86. else if ( DLL_THREAD_DETACH == ulReason )
  87. {
  88. status = TRUE ;
  89. }
  90. else if ( DLL_THREAD_ATTACH == ulReason )
  91. {
  92. status = TRUE ;
  93. }
  94. return TRUE ;
  95. }
  96. catch(Structured_Exception e_SE)
  97. {
  98. return FALSE;
  99. }
  100. catch(Heap_Exception e_HE)
  101. {
  102. return FALSE;
  103. }
  104. catch(...)
  105. {
  106. return FALSE;
  107. }
  108. }
  109. //***************************************************************************
  110. //
  111. // DllGetClassObject
  112. //
  113. // Purpose: Called by Ole when some client wants a a class factory. Return
  114. // one only if it is the sort of class this DLL supports.
  115. //
  116. //***************************************************************************
  117. STDAPI DllGetClassObject (
  118. REFCLSID rclsid ,
  119. REFIID riid,
  120. void **ppv
  121. )
  122. {
  123. SetStructuredExceptionHandler seh;
  124. try
  125. {
  126. HRESULT status = S_OK ;
  127. if ( rclsid == CLSID_CClasProvClassFactory )
  128. {
  129. CClasProvClassFactory *lpunk = new CClasProvClassFactory ;
  130. if ( lpunk == NULL )
  131. {
  132. status = E_OUTOFMEMORY ;
  133. }
  134. else
  135. {
  136. status = lpunk->QueryInterface ( riid , ppv ) ;
  137. if ( FAILED ( status ) )
  138. {
  139. delete lpunk ;
  140. }
  141. else
  142. {
  143. }
  144. }
  145. }
  146. else if ( rclsid == CLSID_CPropProvClassFactory )
  147. {
  148. CPropProvClassFactory *lpunk = new CPropProvClassFactory ;
  149. if ( lpunk == NULL )
  150. {
  151. status = E_OUTOFMEMORY ;
  152. }
  153. else
  154. {
  155. status = lpunk->QueryInterface ( riid , ppv ) ;
  156. if ( FAILED ( status ) )
  157. {
  158. delete lpunk ;
  159. }
  160. else
  161. {
  162. }
  163. }
  164. }
  165. else if ( rclsid == CLSID_CSNMPReftEventProviderClassFactory )
  166. {
  167. CSNMPRefEventProviderClassFactory *lpunk = new CSNMPRefEventProviderClassFactory;
  168. if ( lpunk == NULL )
  169. {
  170. status = E_OUTOFMEMORY ;
  171. }
  172. else
  173. {
  174. status = lpunk->QueryInterface ( riid , ppv ) ;
  175. if ( FAILED ( status ) )
  176. {
  177. delete lpunk ;
  178. }
  179. }
  180. }
  181. else if ( rclsid == CLSID_CSNMPEncapEventProviderClassFactory )
  182. {
  183. CSNMPEncapEventProviderClassFactory *lpunk = new CSNMPEncapEventProviderClassFactory;
  184. if ( lpunk == NULL )
  185. {
  186. status = E_OUTOFMEMORY ;
  187. }
  188. else
  189. {
  190. status = lpunk->QueryInterface ( riid , ppv ) ;
  191. if ( FAILED ( status ) )
  192. {
  193. delete lpunk ;
  194. }
  195. }
  196. }
  197. else
  198. {
  199. status = CLASS_E_CLASSNOTAVAILABLE ;
  200. }
  201. return status ;
  202. }
  203. catch(Structured_Exception e_SE)
  204. {
  205. return E_UNEXPECTED;
  206. }
  207. catch(Heap_Exception e_HE)
  208. {
  209. return E_OUTOFMEMORY;
  210. }
  211. catch(...)
  212. {
  213. return E_UNEXPECTED;
  214. }
  215. }
  216. //***************************************************************************
  217. //
  218. // DllCanUnloadNow
  219. //
  220. // Purpose: Called periodically by Ole in order to determine if the
  221. // DLL can be freed.//
  222. // Return: TRUE if there are no objects in use and the class factory
  223. // isn't locked.
  224. //***************************************************************************
  225. STDAPI DllCanUnloadNow ()
  226. {
  227. SetStructuredExceptionHandler seh;
  228. try
  229. {
  230. /*
  231. * Place code in critical section
  232. */
  233. EnterCriticalSection ( & s_ProviderCriticalSection ) ;
  234. ISmirDatabase* t_pNotifyInt = NULL;
  235. CCorrCacheNotify* t_pnotify = NULL;
  236. CEventCacheNotify* t_notify = NULL;
  237. ISmirDatabase* t_pNotifyIntThread = NULL;
  238. BOOL unload = (
  239. CClasProvClassFactory :: locksInProgress ||
  240. CClasProvClassFactory :: objectsInProgress ||
  241. CPropProvClassFactory :: locksInProgress ||
  242. CPropProvClassFactory :: objectsInProgress ||
  243. CSNMPEventProviderClassFactory :: locksInProgress ||
  244. CSNMPEventProviderClassFactory :: objectsInProgress
  245. ) ;
  246. unload = ! unload ;
  247. if ( unload )
  248. {
  249. if ( CImpClasProv :: s_Initialised )
  250. {
  251. CImpClasProv :: s_Initialised = FALSE ;
  252. CCorrelator :: TerminateCorrelator (&t_pNotifyInt, &t_pnotify) ;
  253. SnmpClassLibrary :: Closedown () ;
  254. SnmpDebugLog :: Closedown () ;
  255. SnmpThreadObject :: Closedown () ;
  256. ProviderClosedown () ;
  257. }
  258. if ( CImpPropProv :: s_Initialised )
  259. {
  260. CImpPropProv :: s_Initialised = FALSE ;
  261. SnmpClassLibrary :: Closedown () ;
  262. SnmpDebugLog :: Closedown () ;
  263. SnmpThreadObject :: Closedown () ;
  264. ProviderClosedown () ;
  265. }
  266. if ( g_pProvThrd )
  267. {
  268. delete g_pProvThrd;
  269. g_pProvThrd = NULL;
  270. g_pWorkerThread->GetDeleteNotifyParams(&t_pNotifyIntThread, &t_notify);
  271. g_pWorkerThread->SignalThreadShutdown();
  272. g_pWorkerThread = NULL;
  273. SnmpClassLibrary :: Closedown () ;
  274. SnmpDebugLog :: Closedown () ;
  275. SnmpThreadObject :: Closedown () ;
  276. ProviderClosedown () ;
  277. }
  278. }
  279. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  280. if (unload)
  281. {
  282. if (t_pNotifyInt)
  283. {
  284. if (t_pnotify)
  285. {
  286. t_pNotifyInt->DeleteNotify(t_pnotify->GetCookie());
  287. }
  288. t_pNotifyInt->Release();
  289. t_pNotifyInt = NULL;
  290. }
  291. if (t_pnotify)
  292. {
  293. t_pnotify->Release();
  294. t_pnotify = NULL;
  295. }
  296. if (t_pNotifyIntThread)
  297. {
  298. if (t_notify)
  299. {
  300. t_pNotifyIntThread->DeleteNotify(t_notify->GetCookie());
  301. }
  302. t_pNotifyIntThread->Release();
  303. t_pNotifyIntThread = NULL;
  304. }
  305. if (t_notify)
  306. {
  307. t_notify->Release();
  308. t_notify = NULL;
  309. }
  310. }
  311. return unload ? ResultFromScode ( S_OK ) : ResultFromScode ( S_FALSE ) ;
  312. }
  313. catch(Structured_Exception e_SE)
  314. {
  315. return S_FALSE;
  316. }
  317. catch(Heap_Exception e_HE)
  318. {
  319. return S_FALSE;
  320. }
  321. catch(...)
  322. {
  323. return S_FALSE;
  324. }
  325. }
  326. #define REG_FORMAT_STR L"%s\\%s"
  327. #define NOT_INSERT_STR L"NotInsertable"
  328. #define INPROC32_STR L"InprocServer32"
  329. #define LOCALSERVER32_STR L"LocalServer32"
  330. #define THREADING_MODULE_STR L"ThreadingModel"
  331. #define APARTMENT_STR L"Both"
  332. #define APPID_STR L"Software\\Classes\\AppID\\"
  333. #define CLSID_STR L"Software\\Classes\\CLSID\\"
  334. #define CLASS_PROVIDER_NAME_STR L"Microsoft WBEM SNMP Class Provider"
  335. #define INSTANCE_PROVIDER_NAME_STR L"Microsoft WBEM SNMP Instance Provider"
  336. #define EVENT_PROVIDER_NAME_STR L"Microsoft WBEM SNMP Event Provider"
  337. /***************************************************************************
  338. * SetKeyAndValue
  339. *
  340. * Purpose:
  341. * Private helper function for DllRegisterServer that creates
  342. * a key, sets a value, and closes that key.
  343. *
  344. * Parameters:
  345. * pszKey LPTSTR to the ame of the key
  346. * pszSubkey LPTSTR ro the name of a subkey
  347. * pszValue LPTSTR to the value to store
  348. *
  349. * Return Value:
  350. * BOOL TRUE if successful, FALSE otherwise.
  351. ***************************************************************************/
  352. BOOL SetKeyAndValue(wchar_t* pszKey, wchar_t* pszSubkey, wchar_t* pszValueName, wchar_t* pszValue)
  353. {
  354. HKEY hKey;
  355. wchar_t szKey[256];
  356. wcscpy(szKey, pszKey);
  357. if (NULL!=pszSubkey)
  358. {
  359. wcscat(szKey, L"\\");
  360. wcscat(szKey, pszSubkey);
  361. }
  362. if (ERROR_SUCCESS!=RegCreateKeyEx(HKEY_LOCAL_MACHINE
  363. , szKey, 0, NULL, REG_OPTION_NON_VOLATILE
  364. , KEY_ALL_ACCESS, NULL, &hKey, NULL))
  365. return FALSE;
  366. if (NULL!=pszValue)
  367. {
  368. if (ERROR_SUCCESS != RegSetValueEx(hKey, pszValueName, 0, REG_SZ, (BYTE *)pszValue
  369. , (lstrlen(pszValue)+1)*sizeof(wchar_t)))
  370. return FALSE;
  371. }
  372. RegCloseKey(hKey);
  373. return TRUE;
  374. }
  375. /***************************************************************************
  376. * DllRegisterServer
  377. *
  378. * Purpose:
  379. * Instructs the server to create its own registry entries
  380. *
  381. * Parameters:
  382. * None
  383. *
  384. * Return Value:
  385. * HRESULT NOERROR if registration successful, error
  386. * otherwise.
  387. ***************************************************************************/
  388. STDAPI RegisterServer( GUID a_ProviderClassId , wchar_t *a_ProviderName )
  389. {
  390. wchar_t szModule[512];
  391. GetModuleFileName(g_hInst,(wchar_t*)szModule, sizeof(szModule)/sizeof(wchar_t));
  392. wchar_t szProviderClassID[128];
  393. wchar_t szProviderCLSIDClassID[128];
  394. int iRet = StringFromGUID2(a_ProviderClassId,szProviderClassID, 128);
  395. #ifdef LOCALSERVER
  396. TCHAR szProviderCLSIDAppID[128];
  397. _tcscpy(szProviderCLSIDAppID,APPID_STR);
  398. _tcscat(szProviderCLSIDAppID,szProviderClassID);
  399. if (FALSE ==SetKeyAndValue(szProviderCLSIDAppID, NULL, NULL, a_ProviderName ))
  400. return SELFREG_E_CLASS;
  401. #endif
  402. wcscpy(szProviderCLSIDClassID,CLSID_STR);
  403. wcscat(szProviderCLSIDClassID,szProviderClassID);
  404. //Create entries under CLSID
  405. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NULL, NULL, a_ProviderName ))
  406. return SELFREG_E_CLASS;
  407. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, NOT_INSERT_STR, NULL, NULL))
  408. return SELFREG_E_CLASS;
  409. #ifdef LOCALSERVER
  410. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, LOCALSERVER32_STR, NULL,szModule))
  411. return SELFREG_E_CLASS;
  412. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, LOCALSERVER32_STR,THREADING_MODULE_STR, APARTMENT_STR))
  413. return SELFREG_E_CLASS;
  414. #else
  415. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR, NULL,szModule))
  416. return SELFREG_E_CLASS;
  417. if (FALSE ==SetKeyAndValue(szProviderCLSIDClassID, INPROC32_STR,THREADING_MODULE_STR, APARTMENT_STR))
  418. return SELFREG_E_CLASS;
  419. #endif
  420. return S_OK;
  421. }
  422. /***************************************************************************
  423. * DllUnregisterServer
  424. *
  425. * Purpose:
  426. * Instructs the server to remove its own registry entries
  427. *
  428. * Parameters:
  429. * None
  430. *
  431. * Return Value:
  432. * HRESULT NOERROR if registration successful, error
  433. * otherwise.
  434. ***************************************************************************/
  435. STDAPI UnregisterServer( GUID a_ProviderClassId )
  436. {
  437. wchar_t szTemp[128];
  438. wchar_t szProviderClassID[128];
  439. wchar_t szProviderCLSIDClassID[128];
  440. int iRet = StringFromGUID2(a_ProviderClassId ,szProviderClassID, 128);
  441. wcscpy(szProviderCLSIDClassID,CLSID_STR);
  442. wcscat(szProviderCLSIDClassID,szProviderClassID);
  443. //Delete entries under CLSID
  444. wsprintf(szTemp, REG_FORMAT_STR, szProviderCLSIDClassID, NOT_INSERT_STR);
  445. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  446. #ifdef LOCALSERVER
  447. TCHAR szProviderCLSIDAppID[128];
  448. _tcscpy(szProviderCLSIDAppID,APPID_STR);
  449. _tcscat(szProviderCLSIDAppID,szProviderClassID);
  450. //Delete entries under APPID
  451. DWORD t_Status = RegDeleteKey(HKEY_CLASSES_ROOT, szProviderCLSIDAppID);
  452. wsprintf(szTemp, REG_FORMAT_STR,szProviderCLSIDClassID, LOCALSERVER32_STR);
  453. t_Status = RegDeleteKey(HKEY_CLASSES_ROOT, szTemp);
  454. #else
  455. wsprintf(szTemp, REG_FORMAT_STR,szProviderCLSIDClassID, INPROC32_STR);
  456. RegDeleteKey(HKEY_LOCAL_MACHINE, szTemp);
  457. #endif
  458. RegDeleteKey(HKEY_LOCAL_MACHINE, szProviderCLSIDClassID);
  459. return S_OK;
  460. }
  461. STDAPI DllRegisterServer()
  462. {
  463. SetStructuredExceptionHandler seh;
  464. try
  465. {
  466. HRESULT t_Result ;
  467. t_Result = RegisterServer ( CLSID_CPropProvClassFactory , INSTANCE_PROVIDER_NAME_STR ) ;
  468. t_Result = FAILED ( t_Result ) ? t_Result : RegisterServer ( CLSID_CClasProvClassFactory , CLASS_PROVIDER_NAME_STR ) ;
  469. t_Result = FAILED ( t_Result ) ? t_Result : RegisterServer ( CLSID_CSNMPReftEventProviderClassFactory , EVENT_PROVIDER_NAME_STR ) ;
  470. t_Result = FAILED ( t_Result ) ? t_Result : RegisterServer ( CLSID_CSNMPEncapEventProviderClassFactory , EVENT_PROVIDER_NAME_STR ) ;
  471. return t_Result ;
  472. }
  473. catch(Structured_Exception e_SE)
  474. {
  475. return E_UNEXPECTED;
  476. }
  477. catch(Heap_Exception e_HE)
  478. {
  479. return E_OUTOFMEMORY;
  480. }
  481. catch(...)
  482. {
  483. return E_UNEXPECTED;
  484. }
  485. }
  486. STDAPI DllUnregisterServer(void)
  487. {
  488. SetStructuredExceptionHandler seh;
  489. try
  490. {
  491. HRESULT t_Result ;
  492. t_Result = UnregisterServer ( CLSID_CPropProvClassFactory ) ;
  493. t_Result = FAILED ( t_Result ) ? t_Result : UnregisterServer ( CLSID_CClasProvClassFactory ) ;
  494. t_Result = FAILED ( t_Result ) ? t_Result : UnregisterServer ( CLSID_CSNMPReftEventProviderClassFactory ) ;
  495. t_Result = FAILED ( t_Result ) ? t_Result : UnregisterServer ( CLSID_CSNMPEncapEventProviderClassFactory ) ;
  496. return t_Result ;
  497. }
  498. catch(Structured_Exception e_SE)
  499. {
  500. return E_UNEXPECTED;
  501. }
  502. catch(Heap_Exception e_HE)
  503. {
  504. return E_OUTOFMEMORY;
  505. }
  506. catch(...)
  507. {
  508. return E_UNEXPECTED;
  509. }
  510. }