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.

418 lines
10 KiB

  1. //***************************************************************************
  2. //
  3. // NTEVTQUERY.CPP
  4. //
  5. // Module: WBEM NT EVENT PROVIDER
  6. //
  7. // Purpose: Contains the taskobject implementation
  8. //
  9. // Copyright (c) 1996-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. WbemTaskObject :: WbemTaskObject (
  14. CImpNTEvtProv *a_Provider ,
  15. IWbemObjectSink *a_NotificationHandler ,
  16. ULONG a_OperationFlag ,
  17. IWbemContext *a_Ctx
  18. ) : m_State ( WBEM_TASKSTATE_START ) ,
  19. m_OperationFlag ( a_OperationFlag ) ,
  20. m_Provider ( a_Provider ) ,
  21. m_NotificationHandler ( a_NotificationHandler ) ,
  22. m_Ctx ( a_Ctx ) ,
  23. m_RequestHandle ( 0 ) ,
  24. m_ClassObject ( NULL ) ,
  25. m_AClassObject ( NULL )
  26. {
  27. //No need to AddRef these 'cos our lifetime is less than the the function creating us!!
  28. // m_Provider->AddRef () ;
  29. // m_NotificationHandler->AddRef () ;
  30. // m_Ctx->AddRef () ;
  31. HRESULT hr = CImpNTEvtProv::GetImpersonation();
  32. if (FAILED(hr))
  33. {
  34. //either FAILED or ACCESS_DENIED
  35. if (hr == WBEM_E_FAILED)
  36. {
  37. m_ErrorObject.SetStatus (WBEM_PROV_E_FAILED);
  38. m_ErrorObject.SetWbemStatus (WBEM_E_FAILED);
  39. m_ErrorObject.SetMessage (L"Failed to impersonate client");
  40. }
  41. else
  42. {
  43. m_ErrorObject.SetStatus (WBEM_PROV_E_ACCESS_DENIED);
  44. m_ErrorObject.SetWbemStatus (WBEM_E_ACCESS_DENIED);
  45. m_ErrorObject.SetMessage (L"Access denied, impersonation level too low");
  46. }
  47. }
  48. }
  49. WbemTaskObject :: ~WbemTaskObject ()
  50. {
  51. //Didn't AddRef so don't Release
  52. // m_Provider->Release () ;
  53. // m_NotificationHandler->Release () ;
  54. // m_Ctx->Release () ;
  55. if ( m_ClassObject )
  56. m_ClassObject->Release () ;
  57. if ( m_AClassObject )
  58. m_AClassObject->Release () ;
  59. WbemCoRevertToSelf();
  60. }
  61. WbemProvErrorObject &WbemTaskObject :: GetErrorObject ()
  62. {
  63. return m_ErrorObject ;
  64. }
  65. BOOL WbemTaskObject :: GetClassObject ( BSTR a_Class )
  66. {
  67. BOOL retVal = FALSE;
  68. IWbemServices *t_Server = m_Provider->GetServer() ;
  69. if (t_Server)
  70. {
  71. retVal = GetClassObject(a_Class, FALSE, t_Server, m_Ctx, &m_ClassObject);
  72. if (retVal)
  73. {
  74. GetClassObject(a_Class, TRUE, t_Server, m_Ctx, &m_AClassObject);
  75. }
  76. t_Server->Release () ;
  77. }
  78. return retVal;
  79. }
  80. BOOL WbemTaskObject :: GetClassObject ( BSTR a_Class, BOOL a_bAmended, IWbemServices *a_Server, IWbemContext *a_Ctx, IWbemClassObject **a_ppClass )
  81. {
  82. DWORD dwIndex = NT_EVTLOG_MAX_CLASSES;
  83. BOOL retVal = FALSE;
  84. if (a_Class && a_ppClass)
  85. {
  86. if (_wcsicmp(a_Class, NTEVT_CLASS) == 0)
  87. {
  88. dwIndex = a_bAmended ? 0 : 1;
  89. }
  90. else if (_wcsicmp(a_Class, NTEVTLOG_CLASS) == 0)
  91. {
  92. dwIndex = a_bAmended ? 2 : 3;
  93. }
  94. else if (_wcsicmp(a_Class, ASSOC_LOGRECORD) == 0)
  95. {
  96. dwIndex = 4;
  97. }
  98. else if (_wcsicmp(a_Class, ASSOC_USERRECORD) == 0)
  99. {
  100. dwIndex = 5;
  101. }
  102. else if (_wcsicmp(a_Class, ASSOC_COMPRECORD) == 0)
  103. {
  104. dwIndex = 6;
  105. }
  106. if (dwIndex < NT_EVTLOG_MAX_CLASSES)
  107. {
  108. if (g_ClassArray[dwIndex] == NULL)
  109. {
  110. if (SUCCEEDED(a_Server->GetObject (
  111. a_Class ,
  112. a_bAmended ? WBEM_FLAG_USE_AMENDED_QUALIFIERS : 0 ,
  113. a_Ctx,
  114. a_ppClass,
  115. NULL
  116. ) ))
  117. {
  118. g_ClassArray[dwIndex] = *a_ppClass ;
  119. g_ClassArray[dwIndex]->AddRef() ;
  120. retVal = TRUE;
  121. }
  122. }
  123. else
  124. {
  125. *a_ppClass = g_ClassArray[dwIndex];
  126. (*a_ppClass)->AddRef();
  127. retVal = TRUE;
  128. }
  129. }
  130. }
  131. return retVal;
  132. }
  133. BOOL WbemTaskObject :: GetExtendedNotifyStatusObject ( IWbemClassObject **a_NotifyObject )
  134. {
  135. IWbemClassObject *t_NotificationClassObject = NULL ;
  136. IWbemClassObject *t_ErrorObject = NULL ;
  137. BOOL t_Status = TRUE ;
  138. WbemProvErrorObject t_ErrorStatusObject ;
  139. if ( t_NotificationClassObject = m_Provider->GetExtendedNotificationObject ( t_ErrorStatusObject, m_Ctx ) )
  140. {
  141. HRESULT t_Result = t_NotificationClassObject->SpawnInstance ( 0 , a_NotifyObject ) ;
  142. if ( SUCCEEDED ( t_Result ) )
  143. {
  144. VARIANT t_Variant ;
  145. VariantInit ( &t_Variant ) ;
  146. t_Variant.vt = VT_I4 ;
  147. t_Variant.lVal = m_ErrorObject.GetWbemStatus () ;
  148. t_Result = (*a_NotifyObject)->Put ( WBEM_PROPERTY_STATUSCODE , 0 , & t_Variant , 0 ) ;
  149. VariantClear ( &t_Variant ) ;
  150. #if 0
  151. if ( SUCCEEDED ( t_Result ) )
  152. {
  153. t_Variant.vt = VT_I4 ;
  154. t_Variant.lVal = m_ErrorObject.GetStatus () ;
  155. t_Result = (*a_NotifyObject)->Put ( WBEM_PROPERTY_PROVSTATUSCODE , 0 , & t_Variant , 0 ) ;
  156. VariantClear ( &t_Variant ) ;
  157. #endif
  158. if ( SUCCEEDED ( t_Result ) )
  159. {
  160. if ( m_ErrorObject.GetMessage () )
  161. {
  162. t_Variant.vt = VT_BSTR ;
  163. t_Variant.bstrVal = SysAllocString ( m_ErrorObject.GetMessage () ) ;
  164. if ( NULL != t_Variant.bstrVal )
  165. {
  166. t_Result = (*a_NotifyObject)->Put ( WBEM_PROPERTY_PROVSTATUSMESSAGE , 0 , & t_Variant , 0 ) ;
  167. VariantClear ( &t_Variant ) ;
  168. if ( m_ErrorObject.GetPrivilegeFailed() )
  169. {
  170. if (m_ErrorObject.SetPrivRequiredVariant(t_Variant))
  171. {
  172. t_Result = (*a_NotifyObject)->Put(WBEM_PROPERTY_PRIVREQUIRED, 0, &t_Variant, 0);
  173. VariantClear ( &t_Variant ) ;
  174. if (SUCCEEDED(t_Result))
  175. {
  176. if (m_ErrorObject.SetPrivFailedVariant(t_Variant))
  177. {
  178. t_Result = (*a_NotifyObject)->Put(WBEM_PROPERTY_PRIVNOTHELD, 0, &t_Variant, 0);
  179. VariantClear ( &t_Variant ) ;
  180. }
  181. else
  182. {
  183. t_Result = WBEM_E_FAILED;
  184. }
  185. }
  186. }
  187. else
  188. {
  189. t_Result = WBEM_E_FAILED;
  190. }
  191. }
  192. }
  193. else
  194. {
  195. t_Result = WBEM_E_OUT_OF_MEMORY ;
  196. }
  197. if ( ! SUCCEEDED ( t_Result ) )
  198. {
  199. (*a_NotifyObject)->Release () ;
  200. *a_NotifyObject = NULL;
  201. t_Status = GetNotifyStatusObject ( a_NotifyObject ) ;
  202. }
  203. }
  204. }
  205. else
  206. {
  207. (*a_NotifyObject)->Release () ;
  208. t_Status = GetNotifyStatusObject ( a_NotifyObject ) ;
  209. }
  210. #if 0
  211. }
  212. else
  213. {
  214. (*a_NotifyObject)->Release () ;
  215. t_Status = GetNotifyStatusObject ( a_NotifyObject ) ;
  216. }
  217. #endif
  218. t_NotificationClassObject->Release () ;
  219. }
  220. else
  221. {
  222. t_Status = GetNotifyStatusObject ( a_NotifyObject ) ;
  223. }
  224. }
  225. else
  226. {
  227. t_Status = GetNotifyStatusObject ( a_NotifyObject ) ;
  228. }
  229. return t_Status ;
  230. }
  231. BOOL WbemTaskObject :: GetNotifyStatusObject ( IWbemClassObject **a_NotifyObject )
  232. {
  233. IWbemClassObject *t_NotificationClassObject = NULL ;
  234. BOOL t_Status = TRUE ;
  235. WbemProvErrorObject t_ErrorStatusObject ;
  236. if ( t_NotificationClassObject = m_Provider->GetNotificationObject ( t_ErrorStatusObject, m_Ctx ) )
  237. {
  238. HRESULT t_Result = t_NotificationClassObject->SpawnInstance ( 0 , a_NotifyObject ) ;
  239. if ( SUCCEEDED ( t_Result ) )
  240. {
  241. VARIANT t_Variant ;
  242. VariantInit ( &t_Variant ) ;
  243. t_Variant.vt = VT_I4 ;
  244. t_Variant.lVal = m_ErrorObject.GetWbemStatus () ;
  245. t_Result = (*a_NotifyObject)->Put ( WBEM_PROPERTY_STATUSCODE , 0 , & t_Variant , 0 ) ;
  246. if ( SUCCEEDED ( t_Result ) )
  247. {
  248. if ( m_ErrorObject.GetMessage () )
  249. {
  250. t_Variant.vt = VT_BSTR ;
  251. t_Variant.bstrVal = SysAllocString ( m_ErrorObject.GetMessage () ) ;
  252. if ( NULL != t_Variant.bstrVal )
  253. {
  254. t_Result = (*a_NotifyObject)->Put ( WBEM_PROPERTY_PROVSTATUSMESSAGE , 0 , & t_Variant , 0 ) ;
  255. VariantClear ( &t_Variant ) ;
  256. }
  257. else
  258. {
  259. t_Result = WBEM_E_OUT_OF_MEMORY ;
  260. }
  261. if ( ! SUCCEEDED ( t_Result ) )
  262. {
  263. t_Status = FALSE ;
  264. (*a_NotifyObject)->Release () ;
  265. (*a_NotifyObject)=NULL ;
  266. }
  267. }
  268. }
  269. else
  270. {
  271. (*a_NotifyObject)->Release () ;
  272. (*a_NotifyObject)=NULL ;
  273. t_Status = FALSE ;
  274. }
  275. VariantClear ( &t_Variant ) ;
  276. t_NotificationClassObject->Release () ;
  277. }
  278. else
  279. {
  280. t_Status = FALSE ;
  281. }
  282. }
  283. else
  284. {
  285. t_Status = FALSE ;
  286. }
  287. return t_Status ;
  288. }
  289. BOOL WbemProvErrorObject::SetPrivVariant ( VARIANT &a_V, DWORD dwVal )
  290. {
  291. BOOL retVal = FALSE;
  292. SAFEARRAYBOUND rgsabound[1];
  293. SAFEARRAY* psa = NULL;
  294. rgsabound[0].lLbound = 0;
  295. VariantInit(&a_V);
  296. rgsabound[0].cElements = 0;
  297. if (dwVal & PROV_PRIV_BACKUP)
  298. {
  299. rgsabound[0].cElements++;
  300. }
  301. if (dwVal & PROV_PRIV_SECURITY)
  302. {
  303. rgsabound[0].cElements++;
  304. }
  305. if (rgsabound[0].cElements != 0)
  306. {
  307. psa = SafeArrayCreate(VT_BSTR, 1, rgsabound);
  308. BSTR* pBstr = NULL;
  309. if (NULL != psa)
  310. {
  311. if (SUCCEEDED(SafeArrayAccessData(psa, (void **)&pBstr)))
  312. {
  313. DWORD indx = 0;
  314. retVal = TRUE ;
  315. if (dwVal & PROV_PRIV_SECURITY)
  316. {
  317. BSTR t_Str = SysAllocString(SE_SECURITY_NAME);
  318. pBstr[indx++] = t_Str ;
  319. if ( NULL == t_Str )
  320. {
  321. retVal = FALSE ;
  322. }
  323. }
  324. if ( retVal )
  325. {
  326. if (dwVal & PROV_PRIV_BACKUP)
  327. {
  328. BSTR t_Str = SysAllocString(SE_BACKUP_NAME);
  329. pBstr[indx] = t_Str ;
  330. if ( NULL == t_Str )
  331. {
  332. retVal = FALSE ;
  333. }
  334. }
  335. }
  336. SafeArrayUnaccessData(psa);
  337. if ( retVal )
  338. {
  339. a_V.vt = VT_ARRAY|VT_BSTR;
  340. a_V.parray = psa;
  341. }
  342. else
  343. {
  344. SafeArrayDestroy ( psa ) ;
  345. }
  346. }
  347. }
  348. }
  349. if (!retVal)
  350. {
  351. VariantClear(&a_V);
  352. }
  353. return retVal;
  354. }