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.

583 lines
17 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. QENGINE.H
  5. Abstract:
  6. WinMgmt Query Engine
  7. History:
  8. raymcc 20-Dec-96 Created
  9. raymcc 14-Aug-99 Resubmit due to VSS problem.
  10. --*/
  11. #ifndef _QENGINE_H_
  12. #define _QENGINE_H_
  13. class CWbemNamespace;
  14. class CWQLParser;
  15. class CWmiMerger;
  16. class CMergerSink;
  17. //***************************************************************************
  18. //
  19. //***************************************************************************
  20. class CQueryEngine
  21. {
  22. static int QueryOptimizationTest(
  23. IN IWmiDbSession *pSession,
  24. IN IWmiDbHandle *pNsHandle,
  25. IN IWmiDbHandle *pScopeHandle,
  26. IN LPCWSTR wszClassName,
  27. IN QL_LEVEL_1_RPN_EXPRESSION *pExp,
  28. OUT CWbemObject **pClassDef,
  29. OUT LPWSTR *pPropToUse,
  30. OUT CVar **pValToUse,
  31. OUT int *pnType
  32. );
  33. static BOOL IsConjunctiveQuery(
  34. IN QL_LEVEL_1_RPN_EXPRESSION *pExp
  35. );
  36. static BOOL QueryKeyTest(
  37. IN QL_LEVEL_1_RPN_EXPRESSION *pExp,
  38. IN CWbemObject *pClassDef,
  39. IN CWStringArray &aKeyProps
  40. );
  41. static BOOL QueryIndexTest(
  42. IN QL_LEVEL_1_RPN_EXPRESSION *pExp,
  43. IN CWbemObject *pClsDef,
  44. IN CWStringArray &aIndexedProps,
  45. OUT LPWSTR *pPropToUse,
  46. OUT CVar **pValToUse,
  47. OUT int *pnType
  48. );
  49. static int KeyedQuery(
  50. IN IWmiDbSession *pSession,
  51. IN IWmiDbHandle *pNsHandle,
  52. IN QL_LEVEL_1_RPN_EXPRESSION *pExp,
  53. IN CWbemObject *pClassDef,
  54. IN DWORD dwFlags,
  55. IN CBasicObjectSink* pDest, // no status
  56. IN CWbemNamespace * pNs
  57. );
  58. static LPWSTR GetObjectPathFromQuery(
  59. IN CWbemObject *pClassDef,
  60. IN QL_LEVEL_1_RPN_EXPRESSION *pExp,
  61. IN CWbemNamespace * pNs
  62. );
  63. static HRESULT ExecQlQuery(
  64. IN CWbemNamespace *pNs,
  65. IN LPWSTR pszQuery,
  66. IN LONG lFlags,
  67. IN IWbemContext* pContext,
  68. IN CBasicObjectSink* pSink
  69. );
  70. static HRESULT ExecRepositoryQuery(
  71. IN CWbemNamespace *pNs,
  72. IN LPWSTR pszQuery,
  73. IN LONG lFlags,
  74. IN IWbemContext* pContext,
  75. IN CBasicObjectSink* pSink
  76. );
  77. static HRESULT ExecComplexQuery(
  78. IN CWbemNamespace *pNs,
  79. IN LPWSTR pszQuery,
  80. IN LONG lFlags,
  81. IN IWbemContext* pContext,
  82. IN CBasicObjectSink* pSink
  83. );
  84. // New Function
  85. static HRESULT EvaluateSubQuery(
  86. IN CWbemNamespace *pNs,
  87. IN CDynasty *pCurrentDyn,
  88. IN LPWSTR pszTextQuery,
  89. IN QL_LEVEL_1_RPN_EXPRESSION *pParsedQuery,
  90. IN IWbemContext* pContext,
  91. IN BOOL bSuppressStaticChild,
  92. IN CWmiMerger* pMerger,
  93. IN CMergerSink* pSink,
  94. long lFlags,
  95. bool bHasRightSibling = false
  96. );
  97. // Old Function
  98. static HRESULT EvaluateSubQuery_old(
  99. IN CWbemNamespace *pNs,
  100. IN CDynasty *pCurrentDyn,
  101. IN LPWSTR pszTextQuery,
  102. IN QL_LEVEL_1_RPN_EXPRESSION *pParsedQuery,
  103. IN IWbemContext* pContext,
  104. IN BOOL bSuppressStaticChild,
  105. IN CBasicObjectSink* pSink, // must have combining semantics
  106. long lFlags,
  107. bool bHasRightSibling = false
  108. );
  109. static HRESULT ExecAtomicDynQlQuery(
  110. IN CWbemNamespace *pNs,
  111. IN CDynasty* pDyn,
  112. IN LPWSTR pszQueryFormat,
  113. IN LPWSTR pszQuery,
  114. IN QL_LEVEL_1_RPN_EXPRESSION *pParsedQuery,
  115. IN LONG lFlags,
  116. IN IWbemContext* pContext,
  117. IN CBasicObjectSink* pDest, // must support selective filtering ,
  118. IN BOOL bHasChildren
  119. );
  120. static HRESULT DirectRead(
  121. IN CWbemNamespace *pNs,
  122. IN CDynasty *pCurrentDyn,
  123. IN LPWSTR wszTextQuery,
  124. IN QL_LEVEL_1_RPN_EXPRESSION *pParsedQuery,
  125. IN IWbemContext* pContext,
  126. IN CBasicObjectSink* pSink,
  127. IN long lFlags
  128. );
  129. static LPWSTR ConstructReferenceProviderQuery(
  130. CWbemNamespace* pNamespace, IWbemContext* pContext,
  131. LPCWSTR wszRefClass, CWbemClass* pRefClass, CWbemObject* pTargetObj,
  132. LPCWSTR wszTargetPathQ,
  133. LPCWSTR wszTargetClass, LPCWSTR wszTargetRole,
  134. LPCWSTR wszResultClass, LPCWSTR wszRequiredQualifier,
  135. LPCWSTR wszEndpointClass, LPCWSTR wszEndpointRole,
  136. CWStringArray& awsPossibleRoles
  137. );
  138. static HRESULT ExecReferencesQuery(CWbemNamespace* pNs,
  139. IWbemContext* pContext,
  140. LPCWSTR wszTargetPath, LPCWSTR wszTargetRole,
  141. LPCWSTR wszResultClass, LPCWSTR wszRequiredQualifier,
  142. LPCWSTR wszEndpointClass, LPCWSTR wszEndpointRole,
  143. CBasicObjectSink* pSink,
  144. DWORD dwQueryType
  145. );
  146. static HRESULT ExecSchemaReferencesQuery(CWbemNamespace* pNs,
  147. IWbemContext* pContext, CWbemClass* pClass,
  148. LPCWSTR wszRealClassName, LPCWSTR wszTargetRole,
  149. LPCWSTR wszResultClass, LPCWSTR wszRequiredQualifier,
  150. LPCWSTR wszEndpointClass, LPCWSTR wszEndpointRole,
  151. CBasicObjectSink* pSink
  152. );
  153. static HRESULT ExecNormalReferencesQuery(CWbemNamespace* pNs,
  154. IWbemContext* pContext, CWbemObject* pObj,
  155. LPCWSTR wszTargetPath, LPCWSTR wszTargetRole,
  156. LPCWSTR wszResultClass, LPCWSTR wszRequiredQualifier,
  157. LPCWSTR wszEndpointClass, LPCWSTR wszEndpointRole,
  158. BOOL bClassDefsOnly,
  159. CBasicObjectSink* pSink
  160. );
  161. static HRESULT EliminateDerivedProperties(
  162. IN QL_LEVEL_1_RPN_EXPRESSION* pOrigQuery,
  163. IN CWbemClass* pClass,
  164. IN BOOL bRelax,
  165. OUT QL_LEVEL_1_RPN_EXPRESSION** ppNewQuery
  166. );
  167. static BOOL IsTokenAboutClass(IN QL_LEVEL_1_TOKEN& Token,
  168. IN CWbemClass* pClass);
  169. static HRESULT AndQueryExpressions(
  170. IN QL_LEVEL_1_RPN_EXPRESSION* pFirst,
  171. IN QL_LEVEL_1_RPN_EXPRESSION* pSecond,
  172. OUT QL_LEVEL_1_RPN_EXPRESSION** ppNew
  173. );
  174. static HRESULT OrQueryExpressions(
  175. IN QL_LEVEL_1_RPN_EXPRESSION* pFirst,
  176. IN QL_LEVEL_1_RPN_EXPRESSION* pSecond,
  177. OUT QL_LEVEL_1_RPN_EXPRESSION** ppNew
  178. );
  179. static void AppendQueryExpression(
  180. IN QL_LEVEL_1_RPN_EXPRESSION* pDest,
  181. IN QL_LEVEL_1_RPN_EXPRESSION* pSource
  182. );
  183. static BSTR GetParentPath(CWbemInstance* pInst, LPCWSTR wszClassName);
  184. static HRESULT ExecSchemaQuery(
  185. IN CWbemNamespace *pNs,
  186. IN LPWSTR pszQuery,
  187. QL_LEVEL_1_RPN_EXPRESSION *pExp,
  188. IN IWbemContext* pContext,
  189. IN CBasicObjectSink* pSink
  190. );
  191. static HRESULT ValidateQuery(IN QL_LEVEL_1_RPN_EXPRESSION *pExp,
  192. IN CWbemClass *pClassDef
  193. );
  194. static HRESULT FindOverridenProperties(CDynasty* pDyn,
  195. CWStringArray& awsOverriden,
  196. bool bIncludeThis = false);
  197. public:
  198. enum { no_error, failed, syntax_error,
  199. invalid_query, invalid_query_language,
  200. use_key, use_table_scan, use_index,
  201. invalid_parameter, invalid_class,
  202. not_found
  203. };
  204. static int ExecAtomicDbQuery(
  205. IN IWmiDbSession *pSession,
  206. IN IWmiDbHandle *pNsHandle,
  207. IN IWmiDbHandle *pScopeHandle,
  208. IN LPCWSTR wszClassName,
  209. IN QL_LEVEL_1_RPN_EXPRESSION *pExp,
  210. IN CBasicObjectSink* pDest, // no status!
  211. IN CWbemNamespace * pNs
  212. );
  213. static HRESULT QueryAllClasses(
  214. IN CWbemNamespace *pNs,
  215. IN LPWSTR pszParent,
  216. OUT IEnumWbemClassObject **ppEnum
  217. );
  218. static HRESULT QueryImmediateClasses(
  219. IN CWbemNamespace *pNs,
  220. IN LPWSTR pszParent,
  221. OUT IEnumWbemClassObject **ppEnum
  222. );
  223. static HRESULT ExecQuery(
  224. IN CWbemNamespace *pNs,
  225. IN LPWSTR pQueryFormat,
  226. IN LPWSTR pQuery,
  227. IN LONG lFlags,
  228. IN IWbemContext* pContext,
  229. IN CBasicObjectSink* pSink
  230. );
  231. static BSTR AdjustPathToClass(LPCWSTR wszRelPath, LPCWSTR wszClassName);
  232. static LPWSTR NormalizePath(LPCWSTR wszObjectPath, CWbemNamespace * pNs);
  233. static LPWSTR GetSimplePropertyName(WBEM_PROPERTY_NAME& Name);
  234. static LPWSTR GetPrimaryName(WBEM_PROPERTY_NAME& Name);
  235. static BOOL IsAReferenceToClass(
  236. IN CWbemNamespace* pNamespace,
  237. IWbemContext* pContext,
  238. IN CWbemObject* pObj,
  239. IN LPCWSTR wszPropName,
  240. IN CWbemObject* pTargetClass,
  241. IN bool bCheckPropValue
  242. );
  243. static BOOL AreClassesRelated(
  244. IN CWbemNamespace* pNamespace,
  245. IWbemContext* pContext,
  246. CWbemObject* pClass1,
  247. LPCWSTR wszClass2
  248. );
  249. protected:
  250. static HRESULT EliminateDuplications(
  251. CRefedPointerArray<CWbemClass>& apClasses,
  252. LPCWSTR wszResultClass);
  253. };
  254. //***************************************************************************
  255. //
  256. //***************************************************************************
  257. class CQueryExpression
  258. {
  259. protected:
  260. QL_LEVEL_1_RPN_EXPRESSION* m_pExpr;
  261. long m_lRef;
  262. protected:
  263. virtual ~CQueryExpression()
  264. {
  265. delete m_pExpr;
  266. }
  267. public:
  268. CQueryExpression(QL_LEVEL_1_RPN_EXPRESSION* pExpr)
  269. : m_pExpr(pExpr), m_lRef(1)
  270. {
  271. }
  272. void AddRef() {InterlockedIncrement(&m_lRef);}
  273. void Release() {if(InterlockedDecrement(&m_lRef) == 0) delete this;}
  274. INTERNAL QL_LEVEL_1_RPN_EXPRESSION* GetExpr() { return m_pExpr;}
  275. };
  276. //***************************************************************************
  277. //
  278. //***************************************************************************
  279. class CQlFilteringSink : public CFilteringSink
  280. {
  281. protected:
  282. QL_LEVEL_1_RPN_EXPRESSION* m_pExpr;
  283. BOOL m_bFilterNow;
  284. CWbemNamespace * m_pNs;
  285. public:
  286. CQlFilteringSink(CBasicObjectSink* pDest,
  287. ADDREF QL_LEVEL_1_RPN_EXPRESSION* pExp,
  288. CWbemNamespace * pNamespace, BOOL bFilterNow = TRUE
  289. );
  290. ~CQlFilteringSink();
  291. STDMETHOD(Indicate)(long lObjectCount, IWbemClassObject** pObjArray);
  292. STDMETHOD(SetStatus)(long lFlags, long lParam, BSTR strParam,
  293. IWbemClassObject* pObjParam
  294. );
  295. virtual IWbemObjectSink* GetIndicateSink() {return this;}
  296. virtual IWbemObjectSink* GetStatusSink() {return this;}
  297. static BOOL Test(CWbemObject* pObj, QL_LEVEL_1_RPN_EXPRESSION* pExpr,
  298. CWbemNamespace * pNs
  299. );
  300. BOOL Test(CWbemObject* pObj);
  301. static int EvaluateToken(IWbemPropertySource *pTestObj,
  302. QL_LEVEL_1_TOKEN& Tok,
  303. CWbemNamespace * pNs
  304. );
  305. };
  306. //***************************************************************************
  307. //
  308. //***************************************************************************
  309. class CProjectingSink : public CForwardingSink
  310. {
  311. protected:
  312. CLimitationMapping m_Map;
  313. BOOL m_bValid;
  314. BOOL m_bProjecting;
  315. WString m_wsError;
  316. CCritSec m_cs;
  317. public:
  318. CProjectingSink(CBasicObjectSink* pDest,
  319. CWbemClass* pClassDef,
  320. READONLY QL_LEVEL_1_RPN_EXPRESSION* pExp,
  321. long lQueryFlags);
  322. BOOL IsValid() {return m_bValid;}
  323. STDMETHOD(Indicate)(long lObjectCount, IWbemClassObject** pObjArray);
  324. virtual IWbemObjectSink* GetIndicateSink() {return this;}
  325. };
  326. //***************************************************************************
  327. //
  328. //***************************************************************************
  329. class CClassDefsOnlyCancelSink : public CForwardingSink
  330. {
  331. protected:
  332. CCritSec m_cs;
  333. CWbemNamespace* m_pNamespace;
  334. WString m_wsTargetObj;
  335. CWStringArray* m_pawsRemainingRoles;
  336. BOOL m_bCancelled;
  337. public:
  338. CClassDefsOnlyCancelSink(CBasicObjectSink* pDest,
  339. CWbemNamespace* pNamespace,
  340. LPCWSTR wszTargetObj, CWStringArray* pawsPossibleRoles);
  341. ~CClassDefsOnlyCancelSink();
  342. STDMETHOD(Indicate)(long lObjectCount, IWbemClassObject** pObjArray);
  343. virtual IWbemObjectSink* GetIndicateSink() {return this;}
  344. STDMETHOD(SetStatus)(long lFlags, long lParam, BSTR strParam,
  345. IWbemClassObject* pObjParam);
  346. virtual IWbemObjectSink* GetStatusSink() {return this;}
  347. protected:
  348. BOOL DoHaveEnough(CWbemObject* pObj);
  349. };
  350. //***************************************************************************
  351. //
  352. //***************************************************************************
  353. class CMerger
  354. {
  355. protected:
  356. class CMemberSink : public CObjectSink
  357. {
  358. protected:
  359. CMerger* m_pMerger;
  360. public:
  361. CMemberSink(CMerger* pMerger) : CObjectSink(0), m_pMerger(pMerger)
  362. {}
  363. STDMETHOD(SetStatus)(long lFlags, long lParam, BSTR strParam,
  364. IWbemClassObject* pObjParam);
  365. virtual BOOL IsTrusted()
  366. {return m_pMerger->m_pDest->IsTrusted();}
  367. virtual BOOL IsApartmentSpecific()
  368. {return m_pMerger->m_pDest->IsApartmentSpecific();}
  369. };
  370. friend CMemberSink;
  371. class COwnSink : public CMemberSink
  372. {
  373. public:
  374. COwnSink(CMerger* pMerger) : CMemberSink(pMerger)
  375. {
  376. m_pMerger->AddRef();
  377. }
  378. ~COwnSink();
  379. STDMETHOD(Indicate)(long lObjectCount, IWbemClassObject** pObjArray);
  380. };
  381. friend COwnSink;
  382. class CChildSink : public CMemberSink
  383. {
  384. public:
  385. CChildSink(CMerger* pMerger) : CMemberSink(pMerger)
  386. {
  387. m_pMerger->AddRef();
  388. }
  389. ~CChildSink();
  390. STDMETHOD(Indicate)(long lObjectCount, IWbemClassObject** pObjArray);
  391. };
  392. friend CChildSink;
  393. public:
  394. struct CRecord
  395. {
  396. CWbemInstance* m_pData;
  397. BOOL m_bOwn;
  398. };
  399. protected:
  400. COwnSink* m_pOwnSink;
  401. CChildSink* m_pChildSink;
  402. CBasicObjectSink* m_pDest;
  403. BOOL m_bDerivedFromTarget;
  404. CWbemClass* m_pOwnClass;
  405. CWbemNamespace* m_pNamespace;
  406. IWbemContext* m_pContext;
  407. CCritSec m_cs;
  408. std::map<WString, CRecord, WSiless, wbem_allocator<CRecord> > m_map;
  409. BOOL m_bOwnDone;
  410. BOOL m_bChildrenDone;
  411. WString m_wsClass;
  412. long m_lRef;
  413. IServerSecurity* m_pSecurity;
  414. protected:
  415. HRESULT AddOwnObject(IWbemClassObject* pObj);
  416. HRESULT AddChildObject(IWbemClassObject* pObj);
  417. void Enter() { m_cs.Enter();}
  418. void Leave() { m_cs.Leave();}
  419. long AddRef();
  420. long Release();
  421. void OwnIsDone();
  422. void ChildrenAreDone();
  423. void DispatchChildren();
  424. void DispatchOwn();
  425. void GetKey(IWbemClassObject* pInst, WString& wsKey);
  426. void GetOwnInstance(LPCWSTR wszKey);
  427. BOOL IsDone() {return m_bOwnDone && m_bChildrenDone;}
  428. public:
  429. CMerger(CBasicObjectSink* pDest, CWbemClass* pOwnClass,
  430. CWbemNamespace* pNamespace = NULL,
  431. IWbemContext* pContext = NULL);
  432. ~CMerger();
  433. BOOL IsValid(){ return (m_pOwnSink && m_pChildSink); };
  434. void SetIsDerivedFromTarget(BOOL bIs);
  435. CBasicObjectSink* GetOwnSink() {return m_pOwnSink;}
  436. CBasicObjectSink* GetChildSink() {return m_pChildSink;}
  437. };
  438. //***************************************************************************
  439. //
  440. //***************************************************************************
  441. struct CProjectionRule
  442. {
  443. WString m_wsPropName;
  444. enum {e_Invalid, e_TakeAll, e_TakePart} m_eType;
  445. CUniquePointerArray<CProjectionRule> m_apPropRules;
  446. public:
  447. CProjectionRule() : m_eType(e_TakePart)
  448. {}
  449. CProjectionRule(LPCWSTR wszPropName)
  450. : m_wsPropName(wszPropName), m_eType(e_TakePart)
  451. {}
  452. CProjectionRule* Find(LPCWSTR wszName);
  453. int GetNumElements(){return m_apPropRules.GetSize();};
  454. };
  455. //***************************************************************************
  456. //
  457. //***************************************************************************
  458. class CComplexProjectionSink : public CForwardingSink
  459. {
  460. protected:
  461. CProjectionRule m_TopRule;
  462. WString m_FirstTable;
  463. WString m_FirstTableAlias;
  464. protected:
  465. void AddColumn(CFlexArray& aFields, LPCWSTR wszPrefix);
  466. HRESULT Project(IWbemClassObject* pObj, CProjectionRule* pRule,
  467. IWbemClassObject** ppProj);
  468. public:
  469. CComplexProjectionSink(CBasicObjectSink* pDest, CWQLScanner* pParser);
  470. ~CComplexProjectionSink();
  471. STDMETHOD(Indicate)(long lObjectCount, IWbemClassObject** pObjArray);
  472. virtual IWbemObjectSink* GetIndicateSink() {return this;}
  473. };
  474. #endif
  475.