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.

567 lines
17 KiB

  1. //***************************************************************************
  2. //
  3. // NTEVTMTHD.CPP
  4. //
  5. // Module: WBEM NT EVENT PROVIDER
  6. //
  7. // Purpose: Contains the ExecMethod implementation
  8. //
  9. // Copyright (c) 1996-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. BOOL ExecMethodAsyncEventObject :: ExecMethod ( WbemProvErrorObject &a_ErrorObject )
  14. {
  15. DebugOut(
  16. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  17. _T(__FILE__),__LINE__,
  18. L"ExecMethodAsyncEventObject :: ExecMethod\r\n"
  19. ) ;
  20. )
  21. if (FAILED(m_ErrorObject.GetWbemStatus()))
  22. {
  23. return FALSE;
  24. }
  25. BOOL t_Status = ! m_ObjectPathParser.Parse ( m_ObjectPath , &m_ParsedObjectPath ) ;
  26. if ( t_Status )
  27. {
  28. if ( _wcsicmp ( m_ParsedObjectPath->m_pClass , NTEVTLOG_CLASS ) == 0 )
  29. {
  30. t_Status = GetClassObject ( m_ParsedObjectPath->m_pClass ) ;
  31. if ( t_Status )
  32. {
  33. IWbemClassObject * tmp = NULL;
  34. IWbemClassObject * outclass = NULL;
  35. t_Status = SUCCEEDED(m_ClassObject->GetMethod(m_Method, 0, &tmp, &outclass));
  36. if (tmp != NULL)
  37. {
  38. tmp->Release();
  39. }
  40. if ((t_Status) && (outclass != NULL))
  41. {
  42. t_Status = SUCCEEDED(outclass->SpawnInstance(0, &m_pOutClass));
  43. if (!t_Status)
  44. {
  45. a_ErrorObject.SetStatus ( WBEM_PROV_E_UNEXPECTED ) ;
  46. a_ErrorObject.SetWbemStatus ( WBEM_E_FAILED ) ;
  47. a_ErrorObject.SetMessage ( L"Unable to spawn result object" ) ;
  48. DebugOut(
  49. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  50. _T(__FILE__),__LINE__,
  51. L"ExecMethodAsyncEventObject :: ExecMethod:Unable to spawn result object\r\n"
  52. ) ;
  53. )
  54. }
  55. else
  56. {
  57. t_Status = Dispatch_EventLog ( a_ErrorObject ) ;
  58. if ( t_Status )
  59. {
  60. m_State = WBEM_TASKSTATE_ASYNCHRONOUSCOMPLETE ;
  61. }
  62. }
  63. outclass->Release();
  64. }
  65. else
  66. {
  67. t_Status = FALSE;
  68. a_ErrorObject.SetStatus ( WBEM_PROV_E_UNEXPECTED ) ;
  69. a_ErrorObject.SetWbemStatus ( WBEM_E_FAILED ) ;
  70. a_ErrorObject.SetMessage ( L"Unable to get result class object" ) ;
  71. DebugOut(
  72. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  73. _T(__FILE__),__LINE__,
  74. L"ExecMethodAsyncEventObject :: ExecMethod:Unable to get result class object\r\n"
  75. ) ;
  76. )
  77. }
  78. }
  79. else
  80. {
  81. t_Status = FALSE ;
  82. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_CLASS ) ;
  83. a_ErrorObject.SetWbemStatus ( WBEM_E_FAILED ) ;
  84. a_ErrorObject.SetMessage ( L"Class definition not found" ) ;
  85. DebugOut(
  86. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  87. _T(__FILE__),__LINE__,
  88. L"ExecMethodAsyncEventObject :: ExecMethod:Class definition not found\r\n"
  89. ) ;
  90. )
  91. }
  92. }
  93. else
  94. {
  95. t_Status = FALSE ;
  96. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_CLASS ) ;
  97. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_CLASS ) ;
  98. a_ErrorObject.SetMessage ( L"Dynamic NT Eventlog Provider does not methods support on this class" ) ;
  99. DebugOut(
  100. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  101. _T(__FILE__),__LINE__,
  102. L"ExecMethodAsyncEventObject :: ExecMethod:Dynamic NT Eventlog Provider does not methods support on this class\r\n"
  103. ) ;
  104. )
  105. }
  106. }
  107. else
  108. {
  109. t_Status = FALSE ;
  110. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_CLASS ) ;
  111. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_PARAMETER ) ;
  112. a_ErrorObject.SetMessage ( L"Unable to parse object path" ) ;
  113. DebugOut(
  114. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  115. _T(__FILE__),__LINE__,
  116. L"ExecMethodAsyncEventObject :: ExecMethod:Unable to parse object path %s\r\n",
  117. m_ObjectPath
  118. ) ;
  119. )
  120. }
  121. DebugOut(
  122. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  123. _T(__FILE__),__LINE__,
  124. L"ExecMethodAsyncEventObject :: ExecMethod:returning with %lx\r\n",
  125. t_Status
  126. ) ;
  127. )
  128. return t_Status ;
  129. }
  130. BOOL ExecMethodAsyncEventObject :: Dispatch_EventLog ( WbemProvErrorObject &a_ErrorObject )
  131. {
  132. DebugOut(
  133. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  134. _T(__FILE__),__LINE__,
  135. L"ExecMethodAsyncEventObject :: Dispatch_EventLog\r\n"
  136. ) ;
  137. )
  138. if (m_ParsedObjectPath->m_dwNumKeys != 1)
  139. {
  140. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_PARAMETER ) ;
  141. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_PARAMETER ) ;
  142. a_ErrorObject.SetMessage ( L"Object path has incorrect number of keys." ) ;
  143. DebugOut(
  144. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  145. _T(__FILE__),__LINE__,
  146. L"GetObjectAsyncEventObject :: Dispatch_EventLog: Wrong number of key values\r\n"
  147. ) ;
  148. )
  149. return FALSE;
  150. }
  151. BOOL t_Status ;
  152. KeyRef *t_Key1 = m_ParsedObjectPath->m_paKeys [ 0 ] ;
  153. if ( t_Key1 )
  154. {
  155. if ((t_Key1->m_pName == NULL) || _wcsicmp ( t_Key1->m_pName , PROP_NAME ) == 0 )
  156. {
  157. if ( t_Key1->m_vValue.vt == VT_BSTR )
  158. {
  159. t_Status = ExecMethod_EventLog ( a_ErrorObject , t_Key1 ) ;
  160. }
  161. else
  162. {
  163. t_Status = FALSE ;
  164. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_PARAMETER ) ;
  165. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_PARAMETER ) ;
  166. a_ErrorObject.SetMessage ( L"Key value have incorrect type" ) ;
  167. DebugOut(
  168. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  169. _T(__FILE__),__LINE__,
  170. L"ExecMethodAsyncEventObject :: Dispatch_EventLog: Key value has incorrect type\r\n"
  171. ) ;
  172. )
  173. }
  174. }
  175. else
  176. {
  177. t_Status = FALSE ;
  178. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_PARAMETER ) ;
  179. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_PARAMETER ) ;
  180. a_ErrorObject.SetMessage ( L"Key value has incorrect name" ) ;
  181. DebugOut(
  182. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  183. _T(__FILE__),__LINE__,
  184. L"ExecMethodAsyncEventObject :: Dispatch_EventLog: Key value has incorrect name\r\n"
  185. ) ;
  186. )
  187. }
  188. }
  189. else
  190. {
  191. t_Status = FALSE ;
  192. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_PARAMETER ) ;
  193. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_PARAMETER ) ;
  194. a_ErrorObject.SetMessage ( L"Failed to get key value" ) ;
  195. DebugOut(
  196. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  197. _T(__FILE__),__LINE__,
  198. L"ExecMethodAsyncEventObject :: Dispatch_EventLog:Failed to get key value\r\n"
  199. ) ;
  200. )
  201. }
  202. DebugOut(
  203. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  204. _T(__FILE__),__LINE__,
  205. L"ExecMethodAsyncEventObject :: Dispatch_EventLog:returning with %lx\r\n",
  206. t_Status
  207. ) ;
  208. )
  209. return t_Status ;
  210. }
  211. BOOL ExecMethodAsyncEventObject :: ExecMethod_EventLog ( WbemProvErrorObject &a_ErrorObject , KeyRef *a_FileKey)
  212. {
  213. DebugOut(
  214. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  215. _T(__FILE__),__LINE__,
  216. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog\r\n"
  217. ) ;
  218. )
  219. BOOL retVal = TRUE;
  220. VARIANT v;
  221. VariantInit(&v);
  222. CIMTYPE cT = VT_NULL;
  223. if (m_InParamObject != NULL)
  224. {
  225. HRESULT hr = m_InParamObject->Get(METHOD_PARAM, 0, &v, &cT, NULL);
  226. if (SUCCEEDED(hr))
  227. {
  228. if (cT != CIM_STRING)
  229. {
  230. retVal = FALSE;
  231. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_PARAMETER ) ;
  232. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_PARAMETER ) ;
  233. a_ErrorObject.SetMessage ( L"ArchiveFileName parameter should be a string" ) ;
  234. DebugOut(
  235. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  236. _T(__FILE__),__LINE__,
  237. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:ArchiveFileName parameter should be a string\r\n"
  238. ) ;
  239. )
  240. }
  241. }
  242. else
  243. {
  244. retVal = FALSE;
  245. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_PARAMETER ) ;
  246. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_PARAMETER ) ;
  247. a_ErrorObject.SetMessage ( L"ArchiveFileName parameter not found in supplied InParam object" ) ;
  248. DebugOut(
  249. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  250. _T(__FILE__),__LINE__,
  251. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:ArchiveFileName parameter not found in supplied InParam object\r\n"
  252. ) ;
  253. )
  254. }
  255. }
  256. DWORD x = ERROR_SUCCESS;
  257. if (retVal)
  258. {
  259. if (_wcsicmp(m_Method, L"ClearEventlog") == 0)
  260. {
  261. wchar_t* param = NULL;
  262. if ((v.vt != VT_BSTR) && (v.vt != VT_NULL))
  263. {
  264. retVal = FALSE;
  265. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_PARAMETER ) ;
  266. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_PARAMETER ) ;
  267. a_ErrorObject.SetMessage ( L"ArchiveFileName parameter not found in supplied InParam object" ) ;
  268. DebugOut(
  269. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  270. _T(__FILE__),__LINE__,
  271. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:ArchiveFileName parameter not found in supplied InParam object\r\n"
  272. ) ;
  273. )
  274. }
  275. else if (v.vt == VT_BSTR)
  276. {
  277. param = v.bstrVal;
  278. }
  279. if (retVal)
  280. {
  281. CStringW log = CEventLogFile::GetLogName((const wchar_t*)a_FileKey->m_vValue.bstrVal);
  282. if (log.IsEmpty())
  283. {
  284. retVal = FALSE;
  285. a_ErrorObject.SetStatus ( WBEM_PROV_E_UNEXPECTED ) ;
  286. a_ErrorObject.SetWbemStatus ( WBEM_E_FAILED ) ;
  287. a_ErrorObject.SetMessage ( L"Could not find specified Eventlog" ) ;
  288. DebugOut(
  289. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  290. _T(__FILE__),__LINE__,
  291. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:Could not find specified Eventlog\r\n"
  292. ) ;
  293. )
  294. }
  295. else
  296. {
  297. CEventlogFileAttributes evtLog(log);
  298. x = evtLog.EventLogOperation(param, TRUE, a_ErrorObject, retVal);
  299. }
  300. }
  301. }
  302. else if (_wcsicmp(m_Method, L"BackupEventlog") == 0)
  303. {
  304. if (v.vt != VT_BSTR)
  305. {
  306. retVal = FALSE;
  307. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_PARAMETER ) ;
  308. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_PARAMETER ) ;
  309. a_ErrorObject.SetMessage ( L"ArchiveFileName parameter not found in supplied InParam object" ) ;
  310. DebugOut(
  311. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  312. _T(__FILE__),__LINE__,
  313. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:ArchiveFileName parameter not found in supplied InParam object\r\n"
  314. ) ;
  315. )
  316. }
  317. else
  318. {
  319. CStringW log = CEventLogFile::GetLogName((const wchar_t*)a_FileKey->m_vValue.bstrVal);
  320. if (log.IsEmpty())
  321. {
  322. retVal = FALSE;
  323. a_ErrorObject.SetStatus ( WBEM_PROV_E_UNEXPECTED ) ;
  324. a_ErrorObject.SetWbemStatus ( WBEM_E_FAILED ) ;
  325. a_ErrorObject.SetMessage ( L"Could not find specified Eventlog" ) ;
  326. DebugOut(
  327. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  328. _T(__FILE__),__LINE__,
  329. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:Could not find specified Eventlog\r\n"
  330. ) ;
  331. )
  332. }
  333. else
  334. {
  335. CEventlogFileAttributes evtLog(log);
  336. x = evtLog.EventLogOperation(v.bstrVal, FALSE, a_ErrorObject, retVal);
  337. }
  338. }
  339. }
  340. else
  341. {
  342. retVal = FALSE;
  343. a_ErrorObject.SetStatus ( WBEM_PROV_E_INVALID_PARAMETER ) ;
  344. a_ErrorObject.SetWbemStatus ( WBEM_E_INVALID_PARAMETER ) ;
  345. a_ErrorObject.SetMessage ( L"Unknown method name supplied." ) ;
  346. DebugOut(
  347. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  348. _T(__FILE__),__LINE__,
  349. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:Unknown method name supplied.\r\n"
  350. ) ;
  351. )
  352. }
  353. }
  354. if (x != ERROR_SUCCESS)
  355. {
  356. wchar_t* buff = NULL;
  357. if (0 == FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  358. NULL, x, 0, (LPWSTR) &buff, 80, NULL))
  359. {
  360. DWORD dwErr = GetLastError();
  361. DebugOut(
  362. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  363. _T(__FILE__),__LINE__,
  364. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:Calling underlying Eventlog API failed.\r\n"
  365. ) ;
  366. )
  367. }
  368. else
  369. {
  370. DebugOut(
  371. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  372. _T(__FILE__),__LINE__,
  373. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:%s\r\n",
  374. buff
  375. ) ;
  376. )
  377. LocalFree(buff);
  378. }
  379. }
  380. VariantClear(&v);
  381. if (retVal)
  382. {
  383. // set out-param
  384. VariantInit(&v);
  385. v.vt = VT_I4;
  386. v.lVal = x;
  387. HRESULT hr = m_pOutClass->Put(METHOD_RESULT_PARAM, 0, &v, 0);
  388. if (FAILED(hr))
  389. {
  390. retVal = FALSE;
  391. a_ErrorObject.SetStatus ( WBEM_PROV_E_UNEXPECTED ) ;
  392. a_ErrorObject.SetWbemStatus ( WBEM_E_FAILED ) ;
  393. a_ErrorObject.SetMessage ( L"Unable to put OutParam value." ) ;
  394. DebugOut(
  395. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  396. _T(__FILE__),__LINE__,
  397. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:Unable to put OutParam value.\r\n"
  398. ) ;
  399. )
  400. m_pOutClass->Release();
  401. m_pOutClass = NULL;
  402. }
  403. else
  404. {
  405. m_bIndicateOutParam = TRUE;
  406. }
  407. VariantClear(&v);
  408. }
  409. DebugOut(
  410. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine (
  411. _T(__FILE__),__LINE__,
  412. L"ExecMethodAsyncEventObject :: ExecMethod_EventLog:returning with %lx\r\n",
  413. retVal
  414. ) ;
  415. )
  416. return retVal;
  417. }
  418. ExecMethodAsyncEventObject :: ExecMethodAsyncEventObject (
  419. CImpNTEvtProv *a_Provider ,
  420. wchar_t *a_ObjectPath ,
  421. wchar_t *a_MethodName,
  422. ULONG a_Flag ,
  423. IWbemClassObject *a_InParams ,
  424. IWbemObjectSink *a_NotificationHandler ,
  425. IWbemContext *pCtx
  426. ) : WbemTaskObject ( a_Provider , a_NotificationHandler , a_Flag , pCtx ) ,
  427. m_Class ( NULL ), m_InParamObject( NULL ), m_bIndicateOutParam ( FALSE ),
  428. m_pOutClass ( NULL ), m_ParsedObjectPath ( NULL )
  429. {
  430. m_InParamObject = a_InParams ;
  431. if (m_InParamObject != NULL)
  432. {
  433. m_InParamObject->AddRef();
  434. }
  435. m_ObjectPath = UnicodeStringDuplicate ( a_ObjectPath ) ;
  436. m_Method = UnicodeStringDuplicate ( a_MethodName ) ;
  437. }
  438. ExecMethodAsyncEventObject :: ~ExecMethodAsyncEventObject ()
  439. {
  440. if (m_pOutClass != NULL)
  441. {
  442. if ( m_bIndicateOutParam && (SUCCEEDED(m_ErrorObject.GetWbemStatus ())) )
  443. {
  444. HRESULT t_Result = m_NotificationHandler->Indicate(1, &m_pOutClass);
  445. }
  446. m_pOutClass->Release();
  447. }
  448. if (m_InParamObject != NULL)
  449. {
  450. m_InParamObject->Release();
  451. }
  452. delete [] m_ObjectPath ;
  453. delete [] m_Method ;
  454. delete m_ParsedObjectPath;
  455. // Get Status object
  456. IWbemClassObject *t_NotifyStatus = NULL ;
  457. BOOL t_Status = TRUE;
  458. if (WBEM_NO_ERROR != m_ErrorObject.GetWbemStatus ())
  459. {
  460. t_Status = GetExtendedNotifyStatusObject ( &t_NotifyStatus ) ;
  461. }
  462. if ( t_Status )
  463. {
  464. HRESULT t_Result = m_NotificationHandler->SetStatus ( 0 , m_ErrorObject.GetWbemStatus () , 0 , t_NotifyStatus ) ;
  465. if (t_NotifyStatus)
  466. {
  467. t_NotifyStatus->Release () ;
  468. }
  469. }
  470. else
  471. {
  472. HRESULT t_Result = m_NotificationHandler->SetStatus ( 0 , m_ErrorObject.GetWbemStatus () , 0 , NULL ) ;
  473. }
  474. }
  475. void ExecMethodAsyncEventObject :: Process ()
  476. {
  477. ExecMethod ( m_ErrorObject ) ;
  478. }