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.

694 lines
16 KiB

  1. //***************************************************************************
  2. //
  3. // NTEVTPROV.CPP
  4. //
  5. // Module: WBEM NT EVENT PROVIDER
  6. //
  7. // Purpose: Contains the WBEM interface for event provider classes
  8. //
  9. // Copyright (c) 1996-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #include "ql.h"
  14. #include "analyser.h"
  15. BOOL ObtainedSerialAccess(CMutex* pLock)
  16. {
  17. BOOL bResult = FALSE;
  18. if (pLock != NULL)
  19. {
  20. if (pLock->Lock())
  21. {
  22. bResult = TRUE;
  23. }
  24. }
  25. return bResult;
  26. }
  27. void ReleaseSerialAccess(CMutex* pLock)
  28. {
  29. if (pLock != NULL)
  30. {
  31. pLock->Unlock();
  32. }
  33. }
  34. void CNTEventProvider::AllocateGlobalSIDs()
  35. {
  36. SID_IDENTIFIER_AUTHORITY t_WorldAuthoritySid = SECURITY_WORLD_SID_AUTHORITY;
  37. if (!AllocateAndInitializeSid(
  38. &t_WorldAuthoritySid,
  39. 1,
  40. SECURITY_WORLD_RID,
  41. 0,
  42. 0, 0, 0, 0, 0, 0,
  43. &s_WorldSid))
  44. {
  45. s_WorldSid = NULL;
  46. }
  47. SID_IDENTIFIER_AUTHORITY t_NTAuthoritySid = SECURITY_NT_AUTHORITY;
  48. if (!AllocateAndInitializeSid(
  49. &t_NTAuthoritySid,
  50. 1,
  51. SECURITY_ANONYMOUS_LOGON_RID,
  52. 0, 0, 0, 0, 0, 0, 0,
  53. &s_AnonymousLogonSid))
  54. {
  55. s_AnonymousLogonSid = NULL;
  56. }
  57. if (!AllocateAndInitializeSid(
  58. &t_NTAuthoritySid,
  59. 2,
  60. SECURITY_BUILTIN_DOMAIN_RID,
  61. DOMAIN_ALIAS_RID_ADMINS,
  62. 0, 0, 0, 0, 0, 0,
  63. &s_AliasAdminsSid))
  64. {
  65. s_AliasAdminsSid = NULL;
  66. }
  67. if (!AllocateAndInitializeSid(
  68. &t_NTAuthoritySid,
  69. 1,
  70. SECURITY_LOCAL_SYSTEM_RID,
  71. 0, 0, 0, 0, 0, 0, 0,
  72. &s_LocalSystemSid
  73. ))
  74. {
  75. s_LocalSystemSid = NULL;
  76. }
  77. if (!AllocateAndInitializeSid(
  78. &t_NTAuthoritySid,
  79. 2,
  80. SECURITY_BUILTIN_DOMAIN_RID,
  81. DOMAIN_ALIAS_RID_GUESTS,
  82. 0,0,0,0,0,0,
  83. &s_AliasGuestsSid
  84. ))
  85. {
  86. s_AliasGuestsSid = NULL;
  87. }
  88. if (!AllocateAndInitializeSid(
  89. &t_NTAuthoritySid,
  90. 2,
  91. SECURITY_BUILTIN_DOMAIN_RID,
  92. DOMAIN_ALIAS_RID_SYSTEM_OPS,
  93. 0,0,0,0,0,0,
  94. &s_AliasSystemOpsSid
  95. ))
  96. {
  97. s_AliasSystemOpsSid = NULL;
  98. }
  99. if (!AllocateAndInitializeSid(
  100. &t_NTAuthoritySid,
  101. 2,
  102. SECURITY_BUILTIN_DOMAIN_RID,
  103. DOMAIN_ALIAS_RID_BACKUP_OPS,
  104. 0,0,0,0,0,0,
  105. &s_AliasBackupOpsSid
  106. ))
  107. {
  108. s_AliasBackupOpsSid = NULL;
  109. }
  110. if (!AllocateAndInitializeSid(
  111. &t_NTAuthoritySid,
  112. 1,
  113. SECURITY_LOCAL_SERVICE_RID,
  114. 0,
  115. 0, 0, 0, 0, 0, 0,
  116. &s_LocalServiceSid
  117. ))
  118. {
  119. s_LocalServiceSid = NULL;
  120. }
  121. if (!AllocateAndInitializeSid(
  122. &t_NTAuthoritySid,
  123. 1,
  124. SECURITY_NETWORK_SERVICE_RID,
  125. 0,
  126. 0, 0, 0, 0, 0, 0,
  127. &s_NetworkServiceSid
  128. ))
  129. {
  130. s_NetworkServiceSid = NULL;
  131. }
  132. }
  133. void CNTEventProvider::FreeGlobalSIDs()
  134. {
  135. if (s_NetworkServiceSid)
  136. {
  137. FreeSid(s_NetworkServiceSid);
  138. s_NetworkServiceSid = NULL;
  139. }
  140. if (s_LocalServiceSid)
  141. {
  142. FreeSid(s_LocalServiceSid);
  143. s_LocalServiceSid = NULL;
  144. }
  145. if (s_AliasBackupOpsSid)
  146. {
  147. FreeSid(s_AliasBackupOpsSid);
  148. s_AliasBackupOpsSid = NULL;
  149. }
  150. if (s_AliasSystemOpsSid)
  151. {
  152. FreeSid(s_AliasSystemOpsSid);
  153. s_AliasSystemOpsSid = NULL;
  154. }
  155. if (s_AliasGuestsSid)
  156. {
  157. FreeSid(s_AliasGuestsSid);
  158. s_AliasGuestsSid = NULL;
  159. }
  160. if (s_LocalSystemSid)
  161. {
  162. FreeSid(s_LocalSystemSid);
  163. s_LocalSystemSid = NULL;
  164. }
  165. if (s_AliasAdminsSid)
  166. {
  167. FreeSid(s_AliasAdminsSid);
  168. s_AliasAdminsSid = NULL;
  169. }
  170. if (s_AnonymousLogonSid)
  171. {
  172. FreeSid(s_AnonymousLogonSid);
  173. s_AnonymousLogonSid = NULL;
  174. }
  175. if (s_WorldSid)
  176. {
  177. FreeSid(s_WorldSid);
  178. s_WorldSid = NULL;
  179. }
  180. }
  181. BOOL CNTEventProvider::GlobalSIDsOK()
  182. {
  183. return (s_NetworkServiceSid
  184. && s_LocalServiceSid
  185. && s_AliasBackupOpsSid
  186. && s_AliasSystemOpsSid
  187. && s_AliasGuestsSid
  188. && s_LocalSystemSid
  189. && s_AliasAdminsSid
  190. && s_AnonymousLogonSid
  191. && s_WorldSid);
  192. }
  193. STDMETHODIMP CNTEventProvider::AccessCheck (
  194. LPCWSTR wszQueryLanguage,
  195. LPCWSTR wszQuery,
  196. LONG lSidLength,
  197. const BYTE __RPC_FAR *pSid
  198. )
  199. {
  200. HRESULT t_Status = WBEM_E_ACCESS_DENIED;
  201. SetStructuredExceptionHandler seh;
  202. try
  203. {
  204. DebugOut(
  205. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  206. L"Entering CNTEventProvider::AccessCheck\r\n");
  207. )
  208. if (lSidLength > 0)
  209. {
  210. if (pSid != NULL)
  211. {
  212. // permanent consumer: hope core did its job
  213. return WBEM_S_SUBJECT_TO_SDS;
  214. }
  215. else
  216. {
  217. return WBEM_E_ACCESS_DENIED;
  218. }
  219. }
  220. if (FAILED(CImpNTEvtProv::GetImpersonation()))
  221. {
  222. return WBEM_E_ACCESS_DENIED;
  223. }
  224. QL_LEVEL_1_RPN_EXPRESSION* pExpr;
  225. CTextLexSource Source(wszQuery);
  226. QL1_Parser Parser(&Source);
  227. int iError = CAbstractQl1Parser::SUCCESS;
  228. if( ( iError = Parser.Parse(&pExpr) ) == 0)
  229. {
  230. // Analyze this
  231. QL_LEVEL_1_RPN_EXPRESSION* pNewExpr;
  232. CPropertyName MyProp;
  233. MyProp.AddElement(TARGET_PROP);
  234. MyProp.AddElement(LOGFILE_PROP);
  235. if(SUCCEEDED(CQueryAnalyser::GetNecessaryQueryForProperty(pExpr, MyProp, pNewExpr)))
  236. {
  237. CStringArray t_wsVals;
  238. HRESULT t_hres = CQueryAnalyser::GetValuesForProp(pNewExpr, MyProp, t_wsVals);
  239. if(SUCCEEDED(t_hres))
  240. {
  241. //grant access and set false if a failure occurs...
  242. t_Status = S_OK;
  243. // awsVals contains the list of files
  244. for (int x = 0; x < t_wsVals.GetSize(); x++)
  245. {
  246. DWORD t_dwReason = 0;
  247. HANDLE t_hEvtlog = CEventLogFile::OpenLocalEventLog(t_wsVals[x], &t_dwReason);
  248. if (t_hEvtlog == NULL)
  249. {
  250. if (t_dwReason != ERROR_FILE_NOT_FOUND)
  251. {
  252. DebugOut(
  253. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  254. L"Entering CNTEventProvider::AccessCheck - Failed to verify logfile access\r\n");
  255. )
  256. t_Status = WBEM_E_ACCESS_DENIED;
  257. break;
  258. }
  259. else
  260. {
  261. DebugOut(
  262. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  263. L"Entering CNTEventProvider::AccessCheck - Logfile not found assuming access allowed for log\r\n");
  264. )
  265. }
  266. }
  267. else
  268. {
  269. CloseEventLog(t_hEvtlog);
  270. }
  271. }
  272. }
  273. else if(t_hres == WBEMESS_E_REGISTRATION_TOO_BROAD)
  274. {
  275. // user asked for all, check all logs....
  276. HKEY hkResult = NULL;
  277. LONG t_lErr = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  278. EVENTLOG_BASE, 0,
  279. KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS,
  280. &hkResult);
  281. if (t_lErr == ERROR_SUCCESS)
  282. {
  283. DWORD iValue = 0;
  284. WCHAR t_logname[MAX_PATH+1];
  285. DWORD t_lognameSize = MAX_PATH+1;
  286. //grant access and set false if a failure occurs...
  287. t_Status = S_OK;
  288. // read all entries under this key to find all logfiles...
  289. while ((t_lErr = RegEnumKey(hkResult, iValue, t_logname, t_lognameSize)) != ERROR_NO_MORE_ITEMS)
  290. {
  291. // if error during read
  292. if (t_lErr != ERROR_SUCCESS)
  293. {
  294. // indicate error
  295. t_Status = WBEM_E_ACCESS_DENIED;
  296. break;
  297. }
  298. //open logfile
  299. DWORD t_dwReason = 0;
  300. HANDLE t_hEvtlog = CEventLogFile::OpenLocalEventLog(t_logname, &t_dwReason);
  301. if (t_hEvtlog == NULL)
  302. {
  303. if (t_dwReason != ERROR_FILE_NOT_FOUND)
  304. {
  305. DebugOut(
  306. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  307. L"Entering CNTEventProvider::AccessCheck - Failed to verify logfile access\r\n");
  308. )
  309. t_Status = WBEM_E_ACCESS_DENIED;
  310. break;
  311. }
  312. else
  313. {
  314. DebugOut(
  315. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  316. L"Entering CNTEventProvider::AccessCheck - Logfile not found assuming access allowed for log\r\n");
  317. )
  318. }
  319. }
  320. else
  321. {
  322. CloseEventLog(t_hEvtlog);
  323. }
  324. // read next parameter
  325. iValue++;
  326. } // end while
  327. RegCloseKey(hkResult);
  328. }
  329. }
  330. t_wsVals.RemoveAll();
  331. delete pNewExpr;
  332. }
  333. delete pExpr;
  334. }
  335. else
  336. {
  337. if ( iError == CAbstractQl1Parser::SYNTAX_ERROR )
  338. {
  339. t_Status = WBEM_E_INVALID_QUERY;
  340. }
  341. else if ( iError == CAbstractQl1Parser::LEXICAL_ERROR )
  342. {
  343. t_Status = WBEM_E_UNPARSABLE_QUERY;
  344. }
  345. else if ( iError == CAbstractQl1Parser::FAILED )
  346. {
  347. t_Status = WBEM_E_FAILED;
  348. }
  349. else if ( iError == CAbstractQl1Parser::BUFFER_TOO_SMALL )
  350. {
  351. // as this is unexpected to happen
  352. t_Status = WBEM_E_UNEXPECTED;
  353. }
  354. }
  355. WbemCoRevertToSelf();
  356. DebugOut(
  357. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  358. L"Leaving CNTEventProvider::AccessCheck\r\n");
  359. )
  360. }
  361. catch(Structured_Exception e_SE)
  362. {
  363. WbemCoRevertToSelf();
  364. t_Status = WBEM_E_UNEXPECTED;
  365. }
  366. catch(Heap_Exception e_HE)
  367. {
  368. WbemCoRevertToSelf();
  369. t_Status = WBEM_E_OUT_OF_MEMORY;
  370. }
  371. catch(...)
  372. {
  373. WbemCoRevertToSelf();
  374. t_Status = WBEM_E_UNEXPECTED;
  375. }
  376. return t_Status;
  377. }
  378. STDMETHODIMP CNTEventProvider::Initialize (
  379. LPWSTR pszUser,
  380. LONG lFlags,
  381. LPWSTR pszNamespace,
  382. LPWSTR pszLocale,
  383. IWbemServices *pCIMOM, // For anybody
  384. IWbemContext *pCtx,
  385. IWbemProviderInitSink *pInitSink // For init signals
  386. )
  387. {
  388. DebugOut(
  389. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  390. L"Entering CNTEventProvider::Initialize\r\n");
  391. )
  392. HRESULT t_Status = WBEM_NO_ERROR;
  393. SetStructuredExceptionHandler seh;
  394. try
  395. {
  396. if (GlobalSIDsOK())
  397. {
  398. m_pNamespace = pCIMOM;
  399. m_pNamespace->AddRef();
  400. m_Mgr->SetFirstSinceLogon(pCIMOM, pCtx);
  401. pInitSink->SetStatus ( WBEM_S_INITIALIZED , 0 );
  402. }
  403. else
  404. {
  405. pInitSink->SetStatus ( WBEM_E_UNEXPECTED , 0 );
  406. }
  407. DebugOut(
  408. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  409. L"Leaving CNTEventProvider::Initialize with SUCCEEDED\r\n");
  410. )
  411. }
  412. catch(Structured_Exception e_SE)
  413. {
  414. t_Status = WBEM_E_UNEXPECTED;
  415. }
  416. catch(Heap_Exception e_HE)
  417. {
  418. t_Status = WBEM_E_OUT_OF_MEMORY;
  419. }
  420. catch(...)
  421. {
  422. t_Status = WBEM_E_UNEXPECTED;
  423. }
  424. return t_Status;
  425. }
  426. STDMETHODIMP CNTEventProvider::ProvideEvents(IWbemObjectSink* pSink, LONG lFlags)
  427. {
  428. HRESULT t_Status = WBEM_NO_ERROR;
  429. SetStructuredExceptionHandler seh;
  430. try
  431. {
  432. DebugOut(
  433. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  434. L"Entering CNTEventProvider::ProvideEvents\r\n");
  435. )
  436. m_pEventSink = pSink;
  437. m_pEventSink->AddRef();
  438. if (!m_Mgr->Register(this))
  439. {
  440. DebugOut(
  441. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  442. L"Leaving CNTEventProvider::ProvideEvents with FAILED\r\n");
  443. )
  444. return WBEM_E_FAILED;
  445. }
  446. DebugOut(
  447. CNTEventProvider::g_NTEvtDebugLog->WriteFileAndLine(_T(__FILE__),__LINE__,
  448. L"Leaving CNTEventProvider::ProvideEvents with SUCCEEDED\r\n");
  449. )
  450. }
  451. catch(Structured_Exception e_SE)
  452. {
  453. t_Status = WBEM_E_UNEXPECTED;
  454. }
  455. catch(Heap_Exception e_HE)
  456. {
  457. t_Status = WBEM_E_OUT_OF_MEMORY;
  458. }
  459. catch(...)
  460. {
  461. t_Status = WBEM_E_UNEXPECTED;
  462. }
  463. return t_Status;
  464. }
  465. CNTEventProvider::~CNTEventProvider()
  466. {
  467. if (m_pNamespace != NULL)
  468. {
  469. m_pNamespace->Release();
  470. }
  471. if (m_pEventSink != NULL)
  472. {
  473. m_pEventSink->Release();
  474. }
  475. }
  476. CNTEventProvider::CNTEventProvider(CEventProviderManager* mgr) : m_pNamespace(NULL), m_pEventSink(NULL)
  477. {
  478. m_Mgr = mgr;
  479. m_ref = 0;
  480. }
  481. IWbemServices* CNTEventProvider::GetNamespace()
  482. {
  483. m_pNamespace->AddRef();
  484. return m_pNamespace;
  485. }
  486. IWbemObjectSink* CNTEventProvider::GetEventSink()
  487. {
  488. m_pEventSink->AddRef();
  489. return m_pEventSink;
  490. }
  491. void CNTEventProvider::ReleaseAll()
  492. {
  493. //release dependencies
  494. m_pNamespace->Release();
  495. m_pEventSink->Release();
  496. Release();
  497. }
  498. void CNTEventProvider::AddRefAll()
  499. {
  500. //addref dependencies
  501. m_pNamespace->AddRef();
  502. m_pEventSink->AddRef();
  503. AddRef();
  504. }
  505. STDMETHODIMP_( ULONG ) CNTEventProvider::AddRef()
  506. {
  507. SetStructuredExceptionHandler seh;
  508. try
  509. {
  510. InterlockedIncrement(&(CNTEventProviderClassFactory::objectsInProgress));
  511. return InterlockedIncrement ( &m_ref ) ;
  512. }
  513. catch(Structured_Exception e_SE)
  514. {
  515. return 0;
  516. }
  517. catch(Heap_Exception e_HE)
  518. {
  519. return 0;
  520. }
  521. catch(...)
  522. {
  523. return 0;
  524. }
  525. }
  526. STDMETHODIMP_(ULONG) CNTEventProvider::Release()
  527. {
  528. SetStructuredExceptionHandler seh;
  529. try
  530. {
  531. long ret;
  532. if ( 0 == (ret = InterlockedDecrement(&m_ref)) )
  533. {
  534. delete this;
  535. }
  536. else if ( 1 == ret )
  537. {
  538. m_Mgr->UnRegister(this);
  539. }
  540. InterlockedDecrement(&(CNTEventProviderClassFactory::objectsInProgress));
  541. return ret;
  542. }
  543. catch(Structured_Exception e_SE)
  544. {
  545. return 0;
  546. }
  547. catch(Heap_Exception e_HE)
  548. {
  549. return 0;
  550. }
  551. catch(...)
  552. {
  553. return 0;
  554. }
  555. }
  556. STDMETHODIMP CNTEventProvider::QueryInterface(REFIID riid, PVOID* ppv)
  557. {
  558. SetStructuredExceptionHandler seh;
  559. try
  560. {
  561. *ppv = NULL;
  562. if (IID_IUnknown == riid)
  563. {
  564. *ppv=(IWbemEventProvider*)this;
  565. }
  566. else if (IID_IWbemEventProvider == riid)
  567. {
  568. *ppv=(IWbemEventProvider*)this;
  569. }
  570. else if (IID_IWbemProviderInit == riid)
  571. {
  572. *ppv= (IWbemProviderInit*)this;
  573. }
  574. else if (IID_IWbemEventProviderSecurity == riid)
  575. {
  576. *ppv= (IWbemEventProviderSecurity*)this;
  577. }
  578. if (NULL==*ppv)
  579. {
  580. return E_NOINTERFACE;
  581. }
  582. //AddRef any interface we'll return.
  583. ((LPUNKNOWN)*ppv)->AddRef();
  584. return NOERROR;
  585. }
  586. catch(Structured_Exception e_SE)
  587. {
  588. return E_UNEXPECTED;
  589. }
  590. catch(Heap_Exception e_HE)
  591. {
  592. return E_OUTOFMEMORY;
  593. }
  594. catch(...)
  595. {
  596. return E_UNEXPECTED;
  597. }
  598. }