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.

503 lines
11 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 <snmptempl.h>
  15. #include <snmpmt.h>
  16. #include <typeinfo.h>
  17. #include <process.h>
  18. #include <objbase.h>
  19. #include <stdio.h>
  20. #include <wbemidl.h>
  21. #include <snmplog.h>
  22. #include <snmpcl.h>
  23. #include <snmpcont.h>
  24. #include <snmptype.h>
  25. #include <snmpauto.h>
  26. #include <snmpevt.h>
  27. #include <snmpthrd.h>
  28. #include <snmpobj.h>
  29. #include <smir.h>
  30. #include <notify.h>
  31. #include <evtdefs.h>
  32. #include <evtthrd.h>
  33. #include <evtmap.h>
  34. #include <evtprov.h>
  35. #include <evtencap.h>
  36. extern CEventProviderWorkerThread* g_pWorkerThread ;
  37. CEncapMapper::CEncapMapper() : m_WbemSnmpObj(NULL)
  38. {
  39. }
  40. HRESULT CEncapMapper::GetSpecificPropertyValue(long lNumElements, MYWBEM_NAME_ELEMENT *aElements,
  41. long lFlags, VARIANT *pvValue)
  42. {
  43. //all specific properties have one element, the property name...
  44. if ((lNumElements != 1) || (0 != aElements[0].m_nType))
  45. {
  46. DebugMacro9(
  47. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  48. __FILE__,__LINE__,
  49. L"CEncapMapper::GetSpecificPropertyValue invalid parameters\r\n");
  50. )
  51. return WBEM_E_FAILED;
  52. }
  53. BOOL bContinue = TRUE;
  54. if (NULL == m_WbemSnmpObj)
  55. {
  56. m_WbemSnmpObj = new WbemSnmpClassObject;
  57. IWbemClassObject *pObj = NULL;
  58. GetClassInstance(&pObj);
  59. if (NULL != pObj)
  60. {
  61. WbemSnmpErrorObject errorObj;
  62. if (!m_WbemSnmpObj->Set(errorObj, pObj, FALSE))
  63. {
  64. bContinue = FALSE;
  65. }
  66. }
  67. else
  68. {
  69. bContinue = FALSE;
  70. }
  71. }
  72. if (bContinue)
  73. {
  74. WbemSnmpProperty *snmpProp = m_WbemSnmpObj->FindProperty(aElements[0].Element.m_wszPropertyName);
  75. if (NULL != snmpProp)
  76. {
  77. //set the property value using the snmp varbind
  78. //and get the property value and return TRUE...
  79. if (SetAndGetProperty(snmpProp, pvValue))
  80. {
  81. DebugMacro9(
  82. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  83. __FILE__,__LINE__,
  84. L"CEncapMapper::GetSpecificPropertyValue successful\r\n");
  85. )
  86. return NOERROR;
  87. }
  88. }
  89. }
  90. else if (NULL != m_WbemSnmpObj)
  91. {
  92. delete m_WbemSnmpObj;
  93. m_WbemSnmpObj = NULL;
  94. }
  95. DebugMacro9(
  96. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  97. __FILE__,__LINE__,
  98. L"CEncapMapper::GetSpecificPropertyValue failed\r\n");
  99. )
  100. return WBEM_E_FAILED;
  101. }
  102. CEncapMapper::~CEncapMapper()
  103. {
  104. if (NULL != m_WbemSnmpObj)
  105. {
  106. delete m_WbemSnmpObj;
  107. }
  108. }
  109. BOOL CEncapMapper::SetAndGetProperty(WbemSnmpProperty *wbemSnmpProp, VARIANT *pvValue)
  110. {
  111. //Get the varbind qualifier value, set the keyvalues qualifier and set the
  112. //property value using the snmp varbind.
  113. //Get the property value and return TRUE;
  114. WbemSnmpQualifier *vbQual = wbemSnmpProp->FindQualifier(EVENT_VBINDEX_QUAL);
  115. if (NULL == vbQual)
  116. {
  117. DebugMacro9(
  118. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  119. __FILE__,__LINE__,
  120. L"CEncapMapper::SetAndGetProperty missing parameter's vbindex qualifier\r\n");
  121. )
  122. return FALSE;
  123. }
  124. VARIANT v;
  125. VariantInit(&v);
  126. ULONG vbindex;
  127. //has to be at least 3, i.e. third vb, to be a specific property...
  128. if (!vbQual->GetValue(v) || (VT_I4 != v.vt) || (2 > v.lVal))
  129. {
  130. VariantClear(&v);
  131. DebugMacro9(
  132. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  133. __FILE__,__LINE__,
  134. L"CEncapMapper::SetAndGetProperty invalid index qual\r\n");
  135. )
  136. return FALSE;
  137. }
  138. if (v.lVal >= m_vbs.length)
  139. {
  140. DebugMacro9(
  141. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  142. __FILE__,__LINE__,
  143. L"CEncapMapper::SetAndGetProperty no value return TRUE\r\n");
  144. )
  145. return TRUE;
  146. }
  147. vbindex = v.lVal - 1; //zero based in our array
  148. VariantClear(&v);
  149. if (m_vbs.vbs[vbindex].fDone)
  150. {
  151. //we've done this one already,
  152. //just get the property value and return it..
  153. CIMTYPE cimType;
  154. if (!wbemSnmpProp->GetValue(*pvValue, cimType))
  155. {
  156. pvValue->vt = VT_NULL;
  157. }
  158. DebugMacro9(
  159. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  160. __FILE__,__LINE__,
  161. L"CEncapMapper::SetAndGetProperty return value\r\n");
  162. )
  163. return TRUE;
  164. }
  165. const SnmpObjectIdentifier& vb_OID = m_vbs.vbs[vbindex].pVarBind->GetInstance();
  166. WbemSnmpQualifier *oidQual = wbemSnmpProp->FindQualifier(WBEM_QUALIFIER_OBJECT_IDENTIFIER);
  167. BOOL bMatchedOID = FALSE;
  168. m_vbs.vbs[vbindex].fDone = TRUE;
  169. if (NULL == oidQual)
  170. {
  171. DebugMacro9(
  172. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  173. __FILE__,__LINE__,
  174. L"CEncapMapper::SetAndGetProperty missing parameter's OID qualifier\r\n");
  175. )
  176. }
  177. else
  178. {
  179. SnmpInstanceType *oid_valuetype = oidQual->GetValue () ;
  180. if ( typeid ( *oid_valuetype ) == typeid ( SnmpObjectIdentifierType ) )
  181. {
  182. SnmpObjectIdentifier oid_value = *(SnmpObjectIdentifier *) oid_valuetype->GetValueEncoding () ;
  183. //compare without the instance info
  184. if (oid_value.GetValueLength() < vb_OID.GetValueLength())
  185. {
  186. SnmpObjectIdentifier* common = vb_OID.Cut(oid_value);
  187. if (NULL != common)
  188. {
  189. if ((*common) == oid_value)
  190. {
  191. bMatchedOID = TRUE;
  192. }
  193. delete common;
  194. }
  195. }
  196. }
  197. else
  198. {
  199. DebugMacro9(
  200. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  201. __FILE__,__LINE__,
  202. L"CEncapMapper::SetAndGetProperty parameter's OID is of wrong type\r\n");
  203. )
  204. }
  205. }
  206. if (wbemSnmpProp->SetValue(&(m_vbs.vbs[vbindex].pVarBind->GetValue())))
  207. {
  208. CIMTYPE cimType;
  209. if (wbemSnmpProp->GetValue(*pvValue, cimType))
  210. {
  211. }
  212. }
  213. else
  214. {
  215. bMatchedOID = FALSE;
  216. }
  217. if (!bMatchedOID)
  218. {
  219. WbemSnmpQualifier *qualifier = NULL ;
  220. wbemSnmpProp->AddQualifier ( WBEM_QUALIFIER_TYPE_MISMATCH ) ;
  221. if ( qualifier = wbemSnmpProp->FindQualifier ( WBEM_QUALIFIER_TYPE_MISMATCH ) )
  222. {
  223. IWbemQualifierSet *t_QualifierSet = NULL;
  224. IWbemClassObject *pObj = NULL;
  225. GetClassInstance(&pObj);
  226. HRESULT result = pObj->GetPropertyQualifierSet ( wbemSnmpProp->GetName () , & t_QualifierSet ) ;
  227. if ( SUCCEEDED ( result ) )
  228. {
  229. SnmpIntegerType integer ( 1 , NULL ) ;
  230. qualifier->SetValue ( t_QualifierSet , integer ) ;
  231. }
  232. t_QualifierSet->Release () ;
  233. }
  234. }
  235. DebugMacro9(
  236. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  237. __FILE__,__LINE__,
  238. L"CEncapMapper::SetAndGetProperty returning TRUE\r\n");
  239. )
  240. return TRUE;
  241. }
  242. void CEncapMapper::GenerateInstance(IWbemClassObject** ppInst)
  243. {
  244. if (NULL == ppInst)
  245. {
  246. //invalid out parameter
  247. DebugMacro9(
  248. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  249. __FILE__,__LINE__,
  250. L"CEncapMapper::GenerateInstance invalid parameter\r\n");
  251. )
  252. return;
  253. }
  254. //set out parameter to NULL;
  255. *ppInst = NULL;
  256. IWbemClassObject *pObj = NULL;
  257. GetClassInstance(&pObj);
  258. if (NULL == pObj)
  259. {
  260. //failed to get class instance
  261. DebugMacro9(
  262. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  263. __FILE__,__LINE__,
  264. L"CEncapMapper::GenerateInstance failed to get class defn\r\n");
  265. )
  266. return;
  267. }
  268. #if 0
  269. HRESULT result = S_OK ;
  270. #else
  271. //get all the property names and set their values...
  272. SAFEARRAY* pPropNames;
  273. HRESULT result = pObj->GetNames(NULL, WBEM_FLAG_NONSYSTEM_ONLY, NULL, &pPropNames);
  274. if (FAILED(result))
  275. {
  276. //failed to get the property names
  277. DebugMacro9(
  278. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  279. __FILE__,__LINE__,
  280. L"CEncapMapper::GenerateInstance failed to get parameter list\r\n");
  281. )
  282. return;
  283. }
  284. //time to <insert expletive> around with a safearray...
  285. //work out the size of the safearray and access the data
  286. if(SafeArrayGetDim(pPropNames) != 1)
  287. {
  288. //wrong dimensions in this array
  289. SafeArrayDestroy(pPropNames);
  290. DebugMacro9(
  291. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  292. __FILE__,__LINE__,
  293. L"CEncapMapper::GenerateInstance parameter array is WRONG!\r\n");
  294. )
  295. return;
  296. }
  297. LONG arraylen = pPropNames->rgsabound[0].cElements;
  298. BSTR *pbstr;
  299. result = SafeArrayAccessData(pPropNames, (void **)&pbstr);
  300. if(FAILED(result))
  301. {
  302. SafeArrayDestroy(pPropNames);
  303. DebugMacro9(
  304. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  305. __FILE__,__LINE__,
  306. L"CEncapMapper::GenerateInstance failed to access parameter array\r\n");
  307. )
  308. return;
  309. }
  310. DebugMacro9(
  311. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  312. __FILE__,__LINE__,
  313. L"CEncapMapper::GenerateInstance setting properties\r\n");
  314. )
  315. BOOL t_bSetProp = FALSE;
  316. //iterate through the names and set the properties...
  317. for (LONG i = 0; i < arraylen; i++)
  318. {
  319. VARIANT v;
  320. MYWBEM_NAME_ELEMENT property_struct;
  321. property_struct.m_nType = 0; //string value
  322. property_struct.Element.m_wszPropertyName = pbstr[i];
  323. result = GetPropertyValue(1, &property_struct, 0, &v);
  324. if (FAILED(result))
  325. {
  326. DebugMacro9(
  327. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  328. __FILE__,__LINE__,
  329. L"CEncapMapper::GenerateInstance failed to get value for %s\r\n",
  330. pbstr[i]);
  331. )
  332. continue;
  333. }
  334. else
  335. {
  336. t_bSetProp = TRUE;
  337. }
  338. if ((v.vt != VT_NULL) && (v.vt != VT_EMPTY))
  339. {
  340. DebugMacro9(
  341. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  342. __FILE__,__LINE__,
  343. L"CEncapMapper::GenerateInstance setting property %s\r\n",
  344. pbstr[i]);
  345. )
  346. result = pObj->Put(pbstr[i], 0, &v, 0);
  347. DebugMacro9(
  348. if (FAILED(result))
  349. {
  350. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  351. __FILE__,__LINE__,
  352. L"CEncapMapper::GenerateInstance setting property %s\r\n",
  353. pbstr[i]);
  354. }
  355. )
  356. }
  357. VariantClear(&v);
  358. }
  359. SafeArrayUnaccessData(pPropNames);
  360. SafeArrayDestroy(pPropNames);
  361. #endif
  362. //if a single property has been put send it on....
  363. if (t_bSetProp)
  364. {
  365. pObj->AddRef();
  366. *ppInst = pObj;
  367. }
  368. DebugMacro9(
  369. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  370. __FILE__,__LINE__,
  371. L"CEncapMapper::GenerateInstance returning\r\n");
  372. )
  373. }
  374. void CEncapMapper::ResetData()
  375. {
  376. //do class specific stuff then call parent class's reset
  377. if (NULL != m_WbemSnmpObj)
  378. {
  379. delete m_WbemSnmpObj;
  380. m_WbemSnmpObj = NULL;
  381. }
  382. CMapToEvent::ResetData();
  383. }
  384. BOOL CEncapMapper::GetSpecificClass()
  385. {
  386. //Build path of mapper instance...
  387. CString path(MAPPER_CLASS_PATH_PREFIX);
  388. path += m_oid;
  389. path += '\"';
  390. BSTR pathstr = path.AllocSysString();
  391. IWbemClassObject *pObj = NULL;
  392. HRESULT result = g_pWorkerThread->GetServerWrap ()->GetMapperObject(pathstr, NULL, & pObj );
  393. SysFreeString(pathstr);
  394. if (result == S_OK)
  395. {
  396. VARIANT v;
  397. VariantInit(&v);
  398. result = pObj->Get(MAPPER_CLASS_EVENTCLASSPROP, 0, &v, NULL, NULL);
  399. pObj->Release();
  400. if (SUCCEEDED(result) && (VT_BSTR == v.vt))
  401. {
  402. m_class = v.bstrVal;
  403. VariantClear(&v);
  404. DebugMacro9(
  405. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  406. __FILE__,__LINE__,
  407. L"CEncapMapper::GetSpecificClass got class defn\r\n");
  408. )
  409. return TRUE;
  410. }
  411. else
  412. {
  413. VariantClear(&v);
  414. }
  415. }
  416. DebugMacro9(
  417. SnmpDebugLog :: s_SnmpDebugLog->WriteFileAndLine (
  418. __FILE__,__LINE__,
  419. L"CEncapMapper::GetSpecificClass failed to get class defn\r\n");
  420. )
  421. return FALSE;
  422. }