Source code of Windows XP (NT5)
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.

616 lines
16 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. instancehelper.cpp
  5. Abstract:
  6. Implementation of:
  7. CInstanceHelper
  8. Author:
  9. Mohit Srivastava 22-Mar-2001
  10. Revision History:
  11. --*/
  12. //
  13. // for metabase.h
  14. //
  15. extern "C" {
  16. #include <nt.h>
  17. #include <ntrtl.h>
  18. #include <nturtl.h>
  19. }
  20. #include <comdef.h>
  21. #include "iisfiles.h"
  22. #include "instancehelper.h"
  23. #include "utils.h"
  24. #include "iiswmimsg.h"
  25. #include "globalconstants.h"
  26. #include "adminacl.h"
  27. #include "ipsecurity.h"
  28. #include "metabase.h"
  29. #include "SmartPointer.h"
  30. #include "schemadynamic.h"
  31. extern CDynSchema* g_pDynSch;
  32. CInstanceHelper::CInstanceHelper(
  33. ParsedObjectPath* i_pParsedObjPath,
  34. CWbemServices* i_pNamespace) :
  35. m_PathParser(e_ParserAcceptRelativeNamespace)
  36. /*++
  37. Synopsis:
  38. Use this constructor when you have already parsed the object path.
  39. Caller owns the ParsedObjectPath.
  40. Arguments: [i_pParsedObjPath] -
  41. [i_pNamespace] -
  42. --*/
  43. {
  44. Init(i_pParsedObjPath, i_pNamespace);
  45. }
  46. CInstanceHelper::CInstanceHelper(
  47. BSTR i_bstrObjPath,
  48. CWbemServices* i_pNamespace) :
  49. m_PathParser(e_ParserAcceptRelativeNamespace)
  50. /*++
  51. Synopsis:
  52. Use this constructor when have not already parsed the object path.
  53. Arguments: [i_bstrObjPath] -
  54. [i_pNamespace] -
  55. --*/
  56. {
  57. DBG_ASSERT(i_bstrObjPath != NULL);
  58. DBG_ASSERT(i_pNamespace != NULL);
  59. TSmartPointer<ParsedObjectPath> spParsedObject;
  60. HRESULT hr = WBEM_S_NO_ERROR;
  61. hr = CUtils::ParserErrToHR( m_PathParser.Parse(i_bstrObjPath, &spParsedObject) );
  62. THROW_ON_ERROR(hr);
  63. Init(spParsedObject, i_pNamespace);
  64. m_pParsedObjPath = spParsedObject;
  65. spParsedObject = NULL;
  66. m_bOwnObjPath = true;
  67. }
  68. void CInstanceHelper::Init(
  69. ParsedObjectPath* i_pParsedObjPath,
  70. CWbemServices* i_pNamespace)
  71. /*++
  72. Synopsis:
  73. Called by constructors.
  74. Arguments: [i_pParsedObjPath] -
  75. [i_pNamespace] -
  76. --*/
  77. {
  78. m_pWmiClass = NULL;
  79. m_pWmiAssoc = NULL;
  80. DBG_ASSERT(i_pParsedObjPath != NULL);
  81. DBG_ASSERT(i_pNamespace != NULL);
  82. HRESULT hr = WBEM_S_NO_ERROR;
  83. if(CUtils::GetClass(i_pParsedObjPath->m_pClass,&m_pWmiClass))
  84. {
  85. }
  86. else if(CUtils::GetAssociation(i_pParsedObjPath->m_pClass,&m_pWmiAssoc))
  87. {
  88. }
  89. else
  90. {
  91. THROW_ON_ERROR(WBEM_E_INVALID_CLASS);
  92. }
  93. m_pParsedObjPath = i_pParsedObjPath;
  94. m_bOwnObjPath = false;
  95. m_pNamespace = i_pNamespace;
  96. THROW_ON_ERROR(hr);
  97. }
  98. void CInstanceHelper::GetAssociation(
  99. IWbemClassObject** o_ppObj,
  100. bool i_bVerifyLeft, //default(true)
  101. bool i_bVerifyRight) //default(true)
  102. /*++
  103. Synopsis:
  104. Specifying i_bVerifyLeft or i_bVerifyRight can be expensive, especially
  105. during enumeration. If you have already verified prior to calling this
  106. function that the left and/or right parts exist, then set these params
  107. to false.
  108. Arguments: [o_ppObj] - The WMI association that you "indicate" to WMI.
  109. [i_bVerifyLeft] - Verify left part of the association is valid.
  110. [i_bVerifyRight] - Verify right part of association is valid.
  111. --*/
  112. {
  113. DBG_ASSERT(o_ppObj != NULL);
  114. CComPtr<IWbemClassObject> spObj; // This is the obj that the client gets back
  115. HRESULT hr = WBEM_S_NO_ERROR;
  116. if(m_pParsedObjPath->m_dwNumKeys < 2)
  117. {
  118. THROW_ON_ERROR(WBEM_E_INVALID_CLASS);
  119. }
  120. KeyRef* pkrLeft = NULL;
  121. KeyRef* pkrRight = NULL;
  122. for(ULONG i = 0; i < m_pParsedObjPath->m_dwNumKeys; i++)
  123. {
  124. KeyRef* pkr = m_pParsedObjPath->m_paKeys[i];
  125. if(pkr->m_pName)
  126. {
  127. if( !pkrLeft &&
  128. _wcsicmp(pkr->m_pName, m_pWmiAssoc->pType->pszLeft) == 0 )
  129. {
  130. pkrLeft = pkr;
  131. }
  132. if( !pkrRight &&
  133. _wcsicmp(pkr->m_pName, m_pWmiAssoc->pType->pszRight) == 0 )
  134. {
  135. pkrRight = pkr;
  136. }
  137. }
  138. }
  139. if( !pkrLeft || !pkrRight ||
  140. pkrLeft->m_vValue.vt != VT_BSTR ||
  141. pkrRight->m_vValue.vt != VT_BSTR ||
  142. pkrLeft->m_vValue.bstrVal == NULL ||
  143. pkrRight->m_vValue.bstrVal == NULL )
  144. {
  145. THROW_ON_ERROR(WBEM_E_INVALID_OBJECT_PATH);
  146. }
  147. //
  148. // Now verify the two object paths are valid
  149. //
  150. bool abVerify[2];
  151. abVerify[0] = i_bVerifyLeft;
  152. abVerify[1] = i_bVerifyRight;
  153. KeyRef* apKr[2];
  154. apKr[0] = pkrLeft;
  155. apKr[1] = pkrRight;
  156. if(abVerify[0] || abVerify[1])
  157. {
  158. CMetabase metabase;
  159. CComPtr<IWbemClassObject> spObjTemp;
  160. for(ULONG i = 0; i < 2; i++)
  161. {
  162. if(abVerify[i])
  163. {
  164. spObjTemp = NULL;
  165. CInstanceHelper InstanceHelper(apKr[i]->m_vValue.bstrVal, m_pNamespace);
  166. if(InstanceHelper.IsAssoc())
  167. {
  168. THROW_ON_ERROR(WBEM_E_NOT_FOUND);
  169. }
  170. InstanceHelper.GetInstance(
  171. false,
  172. &metabase,
  173. &spObjTemp);
  174. }
  175. }
  176. }
  177. hr = CUtils::CreateEmptyInstance(m_pParsedObjPath->m_pClass, m_pNamespace, &spObj);
  178. THROW_ON_ERROR(hr);
  179. hr = spObj->Put(pkrLeft->m_pName, 0, &pkrLeft->m_vValue, 0);
  180. THROW_ON_ERROR(hr);
  181. hr = spObj->Put(pkrRight->m_pName, 0, &pkrRight->m_vValue, 0);
  182. THROW_ON_ERROR(hr);
  183. //
  184. // Set out parameters on success
  185. //
  186. *o_ppObj = spObj;
  187. (*o_ppObj)->AddRef();
  188. }
  189. void CInstanceHelper::GetInstance(
  190. bool i_bCreateKeyIfNotExist,
  191. CMetabase* io_pMetabase,
  192. IWbemClassObject** o_ppObj,
  193. SQL_LEVEL_1_RPN_EXPRESSION_EXT* i_pExp) // default(NULL)
  194. /*++
  195. Synopsis:
  196. Will throw an exception on failure (generallly instance not found in mb).
  197. If GetInstance finds the instance in the metabase, but i_pExp was specified,
  198. it is possible *o_ppObj will not be populated with an instance.
  199. This is a SUCCESS case.
  200. Arguments: [i_bCreateKeyIfNotExist] -
  201. [io_pMetabase] -
  202. [o_ppObj] - The only success case where *o_ppObj will be NULL
  203. is if it the instance is found in the metabase but i_pExp
  204. (i.e. a query) is specified and the query doesn't match.
  205. [i_pExp] - An optional query.
  206. --*/
  207. {
  208. DBG_ASSERT(o_ppObj != NULL);
  209. DBG_ASSERT(io_pMetabase != NULL);
  210. *o_ppObj = NULL;
  211. CComPtr<IWbemClassObject> spObj;
  212. HRESULT hr = WBEM_S_NO_ERROR;
  213. METABASE_PROPERTY** ppmbp;
  214. _bstr_t bstrMbPath;
  215. METADATA_HANDLE hKey = NULL;
  216. VARIANT vtTrue;
  217. vtTrue.boolVal = VARIANT_TRUE;
  218. vtTrue.vt = VT_BOOL;
  219. hr = CUtils::CreateEmptyInstance(m_pParsedObjPath->m_pClass, m_pNamespace, &spObj);
  220. THROW_ON_ERROR(hr);
  221. CUtils::GetMetabasePath(spObj, m_pParsedObjPath, m_pWmiClass, bstrMbPath);
  222. //
  223. // if AdminACL
  224. //
  225. if( m_pWmiClass->pkt == &METABASE_KEYTYPE_DATA::s_TYPE_AdminACL ||
  226. m_pWmiClass->pkt == &METABASE_KEYTYPE_DATA::s_TYPE_AdminACE
  227. )
  228. {
  229. CAdminACL objACL;
  230. hr = objACL.OpenSD(bstrMbPath);
  231. if(SUCCEEDED(hr))
  232. hr = objACL.GetObjectAsync(spObj, m_pParsedObjPath, m_pWmiClass);
  233. THROW_ON_ERROR(hr);
  234. *o_ppObj = spObj;
  235. (*o_ppObj)->AddRef();
  236. return;
  237. }
  238. //
  239. // if IPSecurity
  240. //
  241. else if( m_pWmiClass->pkt == &METABASE_KEYTYPE_DATA::s_TYPE_IPSecurity )
  242. {
  243. CIPSecurity IPSecurity;
  244. hr = IPSecurity.OpenSD(bstrMbPath, *io_pMetabase);
  245. if(SUCCEEDED(hr))
  246. hr = IPSecurity.GetObjectAsync(spObj);
  247. THROW_ON_ERROR(hr);
  248. *o_ppObj = spObj;
  249. (*o_ppObj)->AddRef();
  250. return;
  251. }
  252. if(!i_bCreateKeyIfNotExist)
  253. {
  254. hKey = io_pMetabase->OpenKey(bstrMbPath, false);
  255. }
  256. else
  257. {
  258. hKey = io_pMetabase->CreateKey(bstrMbPath);
  259. }
  260. _variant_t vt;
  261. //
  262. // If anything throws, CacheFree and then CloseKey is called automatically
  263. //
  264. io_pMetabase->CacheInit(hKey);
  265. //
  266. // Make sure requested keytype matches the keytype set at the node
  267. //
  268. if(!i_bCreateKeyIfNotExist)
  269. {
  270. io_pMetabase->Get(hKey, &METABASE_PROPERTY_DATA::s_KeyType, m_pNamespace, vt, NULL, NULL);
  271. if( vt.vt != VT_BSTR ||
  272. vt.bstrVal == NULL ||
  273. !CUtils::CompareKeyType(vt.bstrVal, m_pWmiClass->pkt) )
  274. {
  275. CIIsProvException e;
  276. e.SetMC(WBEM_E_NOT_FOUND, IISWMI_INVALID_KEYTYPE, NULL);
  277. throw e;
  278. }
  279. vt.Clear();
  280. }
  281. //
  282. // User wants to filter number of instances returned
  283. // Walk thru all filters, and try and get these first.
  284. //
  285. if(i_pExp && !i_pExp->GetContainsOrOrNot())
  286. {
  287. SQL_LEVEL_1_TOKEN* pToken = i_pExp->pArrayOfTokens;
  288. METABASE_PROPERTY* pMbpQuery = NULL;
  289. for(int i = 0; i < i_pExp->nNumTokens; i++, pToken++)
  290. {
  291. BOOL bInherited = false;
  292. BOOL bDefault = false;
  293. if( pToken->nTokenType == SQL_LEVEL_1_TOKEN::OP_EXPRESSION )
  294. {
  295. hr = g_pDynSch->GetHashProps()->Wmi_GetByKey(
  296. pToken->pPropertyName,
  297. &pMbpQuery);
  298. //
  299. // User requested a property that is not in the schema
  300. //
  301. if(FAILED(hr))
  302. {
  303. DBGPRINTF( (DBG_CONTEXT,
  304. "Property %ws not in schema\n", pToken->pPropertyName) );
  305. THROW_ON_ERROR(WBEM_E_INVALID_QUERY);
  306. }
  307. io_pMetabase->Get(hKey, pMbpQuery, m_pNamespace, vt, &bInherited, &bDefault);
  308. if(!CheckForQueryMatch(pToken, &vt))
  309. {
  310. //
  311. // We don't need to return this instance.
  312. // value from metabase is not what user wanted.
  313. //
  314. io_pMetabase->CacheFree();
  315. io_pMetabase->CloseKey(hKey);
  316. return;
  317. }
  318. PutProperty(spObj, pToken->pPropertyName, &vt, bInherited, bDefault);
  319. vt.Clear();
  320. }
  321. }
  322. }
  323. //
  324. // Walk thru all the properties in the class and put them in an instance
  325. // we will return back to WMI
  326. //
  327. for (ppmbp=m_pWmiClass->ppmbp;*ppmbp; ppmbp++)
  328. {
  329. BOOL bInherited = false;
  330. BOOL bDefault = false;
  331. BOOL bSkipProp = false;
  332. if(i_pExp)
  333. {
  334. if(!i_pExp->FindRequestedProperty((*ppmbp)->pszPropName))
  335. {
  336. //
  337. // User did not request this property
  338. //
  339. bSkipProp = true;
  340. }
  341. else if(!i_pExp->GetContainsOrOrNot() && i_pExp->GetFilter((*ppmbp)->pszPropName))
  342. {
  343. //
  344. // Right above for loop, we handled all filters already.
  345. //
  346. bSkipProp = true;
  347. }
  348. }
  349. if( !bSkipProp )
  350. {
  351. io_pMetabase->Get(hKey, *ppmbp, m_pNamespace, vt, &bInherited, &bDefault);
  352. _bstr_t bstrPropName = (*ppmbp)->pszPropName;
  353. PutProperty(spObj, bstrPropName, &vt, bInherited, bDefault);
  354. vt.Clear();
  355. }
  356. }
  357. io_pMetabase->CacheFree();
  358. io_pMetabase->CloseKey(hKey);
  359. hKey = NULL;
  360. //
  361. // Set qualifiers
  362. //
  363. LPCWSTR awszNames[2] = { g_wszInstanceName, g_wszInstanceExists };
  364. VARIANT apvValues[2];
  365. apvValues[0].bstrVal = bstrMbPath;
  366. apvValues[0].vt = VT_BSTR;
  367. apvValues[1].boolVal = vtTrue.boolVal;
  368. apvValues[1].vt = vtTrue.vt;
  369. hr = CUtils::SetQualifiers(spObj, awszNames, apvValues, 2,
  370. WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE);
  371. THROW_ON_ERROR(hr);
  372. //
  373. // Set out parameters on success
  374. //
  375. *o_ppObj = spObj;
  376. (*o_ppObj)->AddRef();
  377. }
  378. void CInstanceHelper::PutProperty(
  379. IWbemClassObject* i_pInstance,
  380. const BSTR i_bstrPropName,
  381. VARIANT* i_vtPropValue,
  382. BOOL i_bIsInherited,
  383. BOOL i_bIsDefault)
  384. {
  385. DBG_ASSERT(i_pInstance);
  386. DBG_ASSERT(i_bstrPropName);
  387. DBG_ASSERT(i_vtPropValue);
  388. HRESULT hr = WBEM_S_NO_ERROR;
  389. VARIANT vtTrue;
  390. vtTrue.boolVal = VARIANT_TRUE;
  391. vtTrue.vt = VT_BOOL;
  392. //
  393. // TODO: Log error if Put fails.
  394. //
  395. hr = i_pInstance->Put(i_bstrPropName, 0, i_vtPropValue, 0);
  396. if(FAILED(hr))
  397. {
  398. DBGPRINTF((
  399. DBG_CONTEXT,
  400. "The property %ws in class %ws is not in repository\n",
  401. i_bstrPropName,
  402. m_pWmiClass->pszClassName));
  403. }
  404. if(i_bIsInherited && SUCCEEDED(hr))
  405. {
  406. hr = CUtils::SetPropertyQualifiers(
  407. i_pInstance, i_bstrPropName, &g_wszIsInherit, &vtTrue, 1);
  408. THROW_ON_ERROR(hr);
  409. }
  410. else if(i_bIsDefault && SUCCEEDED(hr))
  411. {
  412. hr = CUtils::SetPropertyQualifiers(
  413. i_pInstance, i_bstrPropName, &g_wszIsDefault, &vtTrue, 1);
  414. THROW_ON_ERROR(hr);
  415. }
  416. }
  417. bool CInstanceHelper::CheckForQueryMatch(
  418. const SQL_LEVEL_1_TOKEN* i_pToken,
  419. const VARIANT* i_pvtMatch)
  420. /*++
  421. Synopsis:
  422. It's okay to return true even if there is not a match, but we should
  423. never do the opposite.
  424. Arguments: [i_pToken] -
  425. [i_pvtMatch] -
  426. Return Value:
  427. --*/{
  428. DBG_ASSERT(i_pToken);
  429. DBG_ASSERT(i_pvtMatch);
  430. DBG_ASSERT(i_pToken->nTokenType == SQL_LEVEL_1_TOKEN::OP_EXPRESSION);
  431. bool bTypesMatch = false;
  432. //
  433. // Used only for VT_BOOL and VT_I4
  434. //
  435. ULONG ulToken = 0;
  436. ULONG ulMatch = 0;
  437. if( i_pvtMatch->vt == i_pToken->vConstValue.vt )
  438. {
  439. bTypesMatch = true;
  440. }
  441. if(bTypesMatch)
  442. {
  443. switch(i_pvtMatch->vt)
  444. {
  445. case VT_BOOL:
  446. ulMatch = i_pvtMatch->boolVal ? 1 : 0;
  447. ulToken = i_pToken->vConstValue.boolVal ? 1 : 0;
  448. //
  449. // deliberate fall thru
  450. //
  451. case VT_I4:
  452. if(i_pvtMatch->vt == VT_I4)
  453. {
  454. ulMatch = i_pvtMatch->lVal;
  455. ulToken = i_pToken->vConstValue.lVal;
  456. }
  457. switch(i_pToken->nOperator)
  458. {
  459. case SQL_LEVEL_1_TOKEN::OP_EQUAL:
  460. if(ulToken != ulMatch)
  461. {
  462. return false;
  463. }
  464. break;
  465. case SQL_LEVEL_1_TOKEN::OP_NOT_EQUAL:
  466. if(ulToken == ulMatch)
  467. {
  468. return false;
  469. }
  470. break;
  471. case SQL_LEVEL_1_TOKEN::OP_EQUALorGREATERTHAN:
  472. if(ulToken < ulMatch)
  473. {
  474. return false;
  475. }
  476. break;
  477. case SQL_LEVEL_1_TOKEN::OP_EQUALorLESSTHAN:
  478. if(ulToken > ulMatch)
  479. {
  480. return false;
  481. }
  482. break;
  483. case SQL_LEVEL_1_TOKEN::OP_LESSTHAN:
  484. if(ulToken >= ulMatch)
  485. {
  486. return false;
  487. }
  488. break;
  489. case SQL_LEVEL_1_TOKEN::OP_GREATERTHAN:
  490. if(ulToken <= ulMatch)
  491. {
  492. return false;
  493. }
  494. break;
  495. }
  496. break;
  497. case VT_BSTR:
  498. if(i_pToken->vConstValue.bstrVal && i_pvtMatch->bstrVal)
  499. {
  500. switch(i_pToken->nOperator)
  501. {
  502. case SQL_LEVEL_1_TOKEN::OP_EQUAL:
  503. if(_wcsicmp(i_pToken->vConstValue.bstrVal, i_pvtMatch->bstrVal) != 0)
  504. {
  505. return false;
  506. }
  507. break;
  508. case SQL_LEVEL_1_TOKEN::OP_NOT_EQUAL:
  509. if(_wcsicmp(i_pToken->vConstValue.bstrVal, i_pvtMatch->bstrVal) == 0)
  510. {
  511. return false;
  512. }
  513. break;
  514. }
  515. }
  516. break;
  517. default:
  518. break;
  519. }
  520. }
  521. return true;
  522. }