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.

747 lines
16 KiB

  1. /////////////////////////////////////////////////////////////////////
  2. //
  3. // CopyRight ( c ) 1999 Microsoft Corporation
  4. //
  5. // Module Name: instanceprov.cpp
  6. //
  7. // Description:
  8. // Implementation of CInstanceProv class
  9. //
  10. // Author:
  11. // Henry Wang ( henrywa ) March 8, 2000
  12. //
  13. //
  14. //////////////////////////////////////////////////////////////////////
  15. #include "DnsWmi.h"
  16. long glNumInst = 0;
  17. //***************************************************************************
  18. //
  19. // CInstanceProv::CInstanceProv
  20. // CInstanceProv::~CInstanceProv
  21. //
  22. //***************************************************************************
  23. CInstanceProv::CInstanceProv(
  24. BSTR ObjectPath,
  25. BSTR User,
  26. BSTR Password,
  27. IWbemContext * pCtx)
  28. {
  29. DBG_FN( "ctor" )
  30. DNS_DEBUG( INSTPROV, (
  31. "%s: count before increment is %d\n"
  32. " ObjectPath %S\n"
  33. " User %S\n",
  34. fn, g_cObj, ObjectPath, User ));
  35. InterlockedIncrement(&g_cObj);
  36. return;
  37. }
  38. CInstanceProv::~CInstanceProv(void)
  39. {
  40. DBG_FN( "dtor" )
  41. DNS_DEBUG( INSTPROV, (
  42. "%s: count before decrement is %d\n", fn, g_cObj ));
  43. InterlockedDecrement(&g_cObj);
  44. #ifdef _DEBUG
  45. _CrtDumpMemoryLeaks();
  46. #endif
  47. return;
  48. }
  49. /////////////////////////////////////////////////////////////////////////////
  50. //++
  51. //
  52. // CInstanceProv::DoExecQueryAsync
  53. //
  54. // Description:
  55. // enum instance for a given class
  56. //
  57. // Arguments:
  58. // QueryLanguage [IN]
  59. // A valid BSTR containing one of the query languages
  60. // supported by Windows Management. This must be WQL.
  61. // Query [IN]
  62. // A valid BSTR containing the text of the query
  63. // lFlags [IN] WMI flag
  64. // pCtx* [IN] WMI context
  65. // pHandler* [IN] WMI sink pointer
  66. //
  67. // Return Value:
  68. // WBEM_S_NO_ERROR
  69. //
  70. //--
  71. /////////////////////////////////////////////////////////////////////////////
  72. SCODE
  73. CInstanceProv::DoExecQueryAsync(
  74. BSTR QueryLanguage,
  75. BSTR Query,
  76. long lFlags,
  77. IWbemContext *pCtx,
  78. IWbemObjectSink *pHandler
  79. )
  80. {
  81. SQL_LEVEL_1_RPN_EXPRESSION * objQueryExpr = NULL;
  82. DBG_FN( "CIP::DoExecQueryAsync" )
  83. DNS_DEBUG( INSTPROV, (
  84. "%s: %S flags=%lu %S\n",
  85. fn, QueryLanguage, lFlags, Query ));
  86. SCODE sc = WBEM_S_NO_ERROR;
  87. try
  88. {
  89. CTextLexSource objQuerySource(Query);
  90. SQL1_Parser objParser(&objQuerySource);
  91. objParser.Parse( &objQueryExpr );
  92. if ( !objQueryExpr )
  93. {
  94. throw ( WBEM_E_OUT_OF_MEMORY );
  95. }
  96. CDnsBase* pDns = NULL ;
  97. sc = CreateClass(
  98. objQueryExpr->bsClassName,
  99. m_pNamespace,
  100. (void**) &pDns);
  101. if ( FAILED ( sc ) )
  102. {
  103. return sc;
  104. }
  105. auto_ptr<CDnsBase> pDnsBase(pDns);
  106. int nNumTokens = objQueryExpr->nNumTokens;
  107. CSqlEval* pEval = CSqlEval::CreateClass(
  108. objQueryExpr,
  109. &nNumTokens);
  110. auto_ptr<CSqlEval> apEval(pEval);
  111. sc = pDnsBase->ExecQuery(
  112. &(*apEval),
  113. lFlags,
  114. pCtx,
  115. pHandler);
  116. }
  117. catch ( CDnsProvException e )
  118. {
  119. CWbemClassObject Status;
  120. sc = SetExtendedStatus(e.what(), Status);
  121. if ( SUCCEEDED ( sc ) )
  122. {
  123. sc = pHandler->SetStatus(0, WBEM_E_FAILED,NULL,*(&Status));
  124. return sc;
  125. }
  126. }
  127. catch ( SCODE exSc )
  128. {
  129. sc = exSc;
  130. }
  131. catch ( ... )
  132. {
  133. sc = WBEM_E_FAILED;
  134. }
  135. delete objQueryExpr;
  136. pHandler->SetStatus(0, sc,NULL,NULL);
  137. return sc;
  138. };
  139. /////////////////////////////////////////////////////////////////////////////
  140. //++
  141. //
  142. // CInstanceProv::DoCreateInstanceEnumAsync
  143. //
  144. // Description:
  145. // enum instance for a given class
  146. //
  147. // Arguments:
  148. // RefStr [IN[ name the class to enumerate
  149. // lFlags [IN] WMI flag
  150. // pCtx* [IN] WMI context
  151. // pHandler* [IN] WMI sink pointer
  152. //
  153. // Return Value:
  154. // WBEM_S_NO_ERROR
  155. //
  156. //--
  157. /////////////////////////////////////////////////////////////////////////////
  158. SCODE
  159. CInstanceProv::DoCreateInstanceEnumAsync(
  160. BSTR RefStr,
  161. long lFlags,
  162. IWbemContext *pCtx,
  163. IWbemObjectSink *pHandler
  164. )
  165. {
  166. DBG_FN( "CIP::DoCreateInstanceEnumAsync" );
  167. DNS_DEBUG( INSTPROV, (
  168. "%s: flags=%lu %S\n",
  169. fn, lFlags, RefStr ));
  170. SCODE sc;
  171. int iCnt;
  172. // Do a check of arguments and make sure we have pointer to Namespace
  173. if(pHandler == NULL || m_pNamespace == NULL)
  174. return WBEM_E_INVALID_PARAMETER;
  175. try
  176. {
  177. CDnsBase* pDns = NULL ;
  178. sc = CreateClass(RefStr, m_pNamespace, (void**) &pDns);
  179. if( FAILED ( sc ) )
  180. {
  181. return sc;
  182. }
  183. auto_ptr<CDnsBase> pDnsBase(pDns);
  184. sc = pDnsBase->EnumInstance(
  185. lFlags,
  186. pCtx,
  187. pHandler);
  188. }
  189. catch(CDnsProvException e)
  190. {
  191. CWbemClassObject Status;
  192. sc = SetExtendedStatus(e.what(), Status);
  193. if ( SUCCEEDED ( sc ) )
  194. {
  195. sc = pHandler->SetStatus(0, WBEM_E_FAILED,NULL,*(&Status));
  196. return sc;
  197. }
  198. }
  199. catch(SCODE exSc)
  200. {
  201. sc = exSc;
  202. }
  203. catch(...)
  204. {
  205. sc = WBEM_E_FAILED;
  206. }
  207. pHandler->SetStatus(0, sc,NULL,NULL);
  208. return sc;
  209. }
  210. /////////////////////////////////////////////////////////////////////////////
  211. //++
  212. //
  213. // CInstanceProv::GetObject
  214. //
  215. // Description:
  216. // Creates an instance given a particular path value.
  217. //
  218. // Arguments:
  219. // ObjectPath [IN] object path to an object
  220. // lFlags [IN] WMI flag
  221. // pCtx* [IN] WMI context
  222. // pHandler* [IN] WMI sink pointer
  223. //
  224. // Return Value:
  225. // WBEM_S_NO_ERROR
  226. // win32 error
  227. //
  228. //--
  229. /////////////////////////////////////////////////////////////////////////////
  230. SCODE
  231. CInstanceProv::DoGetObjectAsync(
  232. BSTR ObjectPath,
  233. long lFlags,
  234. IWbemContext *pCtx,
  235. IWbemObjectSink FAR* pHandler
  236. )
  237. {
  238. DBG_FN( "CIP::DoGetObjectAsync" );
  239. DNS_DEBUG( INSTPROV, (
  240. "%s: flags=%lu %S\n",
  241. fn, lFlags, ObjectPath ));
  242. SCODE sc;
  243. IWbemClassObject FAR* pObj;
  244. BOOL bOK = FALSE;
  245. // Do a check of arguments and make sure we have pointer to Namespace
  246. if( ObjectPath == NULL || pHandler == NULL || m_pNamespace == NULL )
  247. {
  248. DNS_DEBUG( INSTPROV, (
  249. "%s: bad parameter - WBEM_E_INVALID_PARAMETER\n", fn ));
  250. return WBEM_E_INVALID_PARAMETER;
  251. }
  252. // do the get, pass the object on to the notify
  253. try
  254. {
  255. CObjPath ObjPath;
  256. if(!ObjPath.Init(ObjectPath))
  257. {
  258. DNS_DEBUG( INSTPROV, (
  259. "%s: bad object path - WBEM_E_INVALID_PARAMETER\n", fn ));
  260. return WBEM_E_INVALID_PARAMETER;
  261. }
  262. CDnsBase* pDns = NULL;
  263. wstring wstrClass = ObjPath.GetClassName();
  264. sc = CreateClass(
  265. (WCHAR*)wstrClass.data(),
  266. m_pNamespace,
  267. (void**) &pDns);
  268. if( FAILED(sc) )
  269. {
  270. DNS_DEBUG( INSTPROV, (
  271. "%s: CreateClass returned 0x%08X\n", fn, sc ));
  272. return sc;
  273. }
  274. auto_ptr<CDnsBase> pDnsBase(pDns);
  275. sc = pDnsBase->GetObject(
  276. ObjPath,
  277. lFlags,
  278. pCtx,
  279. pHandler);
  280. }
  281. catch( CDnsProvException e )
  282. {
  283. DNS_DEBUG( INSTPROV, (
  284. "%s: caught CDnsProvException \"%s\"\n", fn, e.what() ));
  285. CWbemClassObject Status;
  286. sc = SetExtendedStatus(e.what(), Status);
  287. if ( SUCCEEDED ( sc ) )
  288. {
  289. sc = pHandler->SetStatus(0, WBEM_E_FAILED,NULL,*(&Status));
  290. return sc;
  291. }
  292. }
  293. catch(SCODE exSc)
  294. {
  295. sc = exSc;
  296. }
  297. catch(...)
  298. {
  299. sc = WBEM_E_FAILED;
  300. }
  301. pHandler->SetStatus(0, sc,NULL,NULL);
  302. #ifdef _DEBUG
  303. // _CrtDumpMemoryLeaks();
  304. #endif
  305. return sc;
  306. }
  307. /////////////////////////////////////////////////////////////////////////////
  308. //++
  309. //
  310. // CInstanceProv::DoPutInstanceAsync
  311. //
  312. // Description:
  313. // save this instance
  314. //
  315. // Arguments:
  316. // pInst [IN] WMI object to be saved
  317. // lFlags [IN] WMI flag
  318. // pCtx* [IN] WMI context
  319. // pHandler* [IN] WMI sink pointer
  320. //
  321. // Return Value:
  322. // WBEM_S_NO_ERROR
  323. //
  324. //--
  325. /////////////////////////////////////////////////////////////////////////////
  326. SCODE
  327. CInstanceProv::DoPutInstanceAsync(
  328. IWbemClassObject *pInst,
  329. long lFlags,
  330. IWbemContext *pCtx,
  331. IWbemObjectSink *pHandler
  332. )
  333. {
  334. DBG_FN( "CIP::DoPutInstanceAsync" )
  335. DNS_DEBUG( INSTPROV, (
  336. "%s: flags=%lu pInst=%p\n",
  337. fn, lFlags, pInst ));
  338. SCODE sc;
  339. if(pInst == NULL || pHandler == NULL )
  340. {
  341. DNS_DEBUG( INSTPROV, (
  342. "%s: returning WBEM_E_INVALID_PARAMETER\n" ));
  343. return WBEM_E_INVALID_PARAMETER;
  344. }
  345. try
  346. {
  347. // get class name
  348. wstring wstrClass;
  349. CWbemClassObject Inst(pInst);
  350. Inst.GetProperty(
  351. wstrClass,
  352. L"__Class");
  353. wstring wstrPath;
  354. Inst.GetProperty(
  355. wstrPath,
  356. L"__RelPath");
  357. CDnsBase* pDns = NULL;
  358. sc = CreateClass(
  359. wstrClass.data(),
  360. m_pNamespace,
  361. (void**) &pDns);
  362. if( FAILED(sc) )
  363. {
  364. return sc;
  365. }
  366. auto_ptr<CDnsBase> pDnsBase(pDns);
  367. DNS_DEBUG( INSTPROV, (
  368. "%s: doing base PutInstance\n"
  369. " class: %S\n"
  370. " path: %S\n",
  371. fn,
  372. wstrClass.c_str(),
  373. wstrPath.c_str() ));
  374. sc = pDnsBase->PutInstance(
  375. pInst,
  376. lFlags,
  377. pCtx,
  378. pHandler);
  379. }
  380. catch(CDnsProvException e)
  381. {
  382. DNS_DEBUG( INSTPROV, (
  383. "%s: caught CDnsProvException \"%s\"\n", fn,
  384. e.what() ));
  385. CWbemClassObject Status;
  386. sc = SetExtendedStatus(e.what(), Status);
  387. if (SUCCEEDED ( sc ))
  388. {
  389. sc = pHandler->SetStatus(0, WBEM_E_FAILED,NULL,*(&Status));
  390. return sc;
  391. }
  392. }
  393. catch(SCODE exSc)
  394. {
  395. DNS_DEBUG( INSTPROV, (
  396. "%s: cauught SCODE 0x%08X\n", fn, exSc ));
  397. sc = exSc;
  398. }
  399. catch(...)
  400. {
  401. DNS_DEBUG( INSTPROV, (
  402. "%s: cauught unknown exception returning WBEM_E_FAILED\n", fn ));
  403. sc = WBEM_E_FAILED;
  404. }
  405. pHandler->SetStatus(0, sc,NULL,NULL);
  406. #ifdef _DEBUG
  407. // _CrtDumpMemoryLeaks();
  408. #endif
  409. return sc;
  410. }
  411. /////////////////////////////////////////////////////////////////////////////
  412. //++
  413. //
  414. // CInstanceProv::DoDeleteInstanceAsync
  415. //
  416. // Description:
  417. // delete this instance
  418. //
  419. // Arguments:
  420. // rObjPath [IN] ObjPath for the instance to be deleted
  421. // lFlags [IN] WMI flag
  422. // pCtx* [IN] WMI context
  423. // pHandler* [IN] WMI sink pointer
  424. //
  425. // Return Value:
  426. // WBEM_S_NO_ERROR
  427. //
  428. //--
  429. /////////////////////////////////////////////////////////////////////////////
  430. SCODE CInstanceProv::DoDeleteInstanceAsync(
  431. BSTR ObjectPath,
  432. long lFlags,
  433. IWbemContext * pCtx,
  434. IWbemObjectSink * pHandler
  435. )
  436. {
  437. DBG_FN( "CIP::DoDeleteInstanceAsync" );
  438. DNS_DEBUG( INSTPROV, (
  439. "%s: flags=%lu %S\n",
  440. fn, lFlags, ObjectPath ));
  441. SCODE sc;
  442. // Do a check of arguments and make sure we have pointer to Namespace
  443. if(ObjectPath == NULL || pHandler == NULL )
  444. {
  445. return WBEM_E_INVALID_PARAMETER;
  446. }
  447. // do the get, pass the object on to the notify
  448. try
  449. {
  450. CObjPath ObjPath;
  451. if(!ObjPath.Init(ObjectPath))
  452. {
  453. return WBEM_E_INVALID_PARAMETER;
  454. }
  455. CDnsBase* pDns = NULL;
  456. wstring wstrClass = ObjPath.GetClassName();
  457. sc = CreateClass(
  458. (WCHAR*)wstrClass.data(),
  459. m_pNamespace,
  460. (void**) &pDns);
  461. if( FAILED(sc) )
  462. {
  463. return sc;
  464. }
  465. auto_ptr<CDnsBase> pDnsBase(pDns);
  466. sc = pDnsBase->DeleteInstance(
  467. ObjPath,
  468. lFlags,
  469. pCtx,
  470. pHandler);
  471. }
  472. catch(CDnsProvException e)
  473. {
  474. CWbemClassObject Status;
  475. sc = SetExtendedStatus(e.what(), Status);
  476. if (SUCCEEDED ( sc ))
  477. {
  478. sc = pHandler->SetStatus(0, WBEM_E_FAILED,NULL,*(&Status));
  479. return sc;
  480. }
  481. }
  482. catch(SCODE exSc)
  483. {
  484. sc = exSc;
  485. }
  486. catch(...)
  487. {
  488. sc = WBEM_E_FAILED;
  489. }
  490. pHandler->SetStatus(0, sc,NULL,NULL);
  491. return sc;
  492. }
  493. /////////////////////////////////////////////////////////////////////////////
  494. //++
  495. //
  496. // CInstanceProv::DoExecMethodAsync
  497. //
  498. // Description:
  499. // execute methods for the given object
  500. //
  501. // Arguments:
  502. // ObjectPath [IN] object path to a given object
  503. // pwszMethodName [IN] name of the method to be invoked
  504. // lFlags [IN] WMI flag
  505. // pInParams* [IN] Input parameters for the method
  506. // pHandler* [IN] WMI sink pointer
  507. //
  508. // Return Value:
  509. // WBEM_S_NO_ERROR
  510. //
  511. //--
  512. /////////////////////////////////////////////////////////////////////////////
  513. SCODE
  514. CInstanceProv::DoExecMethodAsync(
  515. BSTR strObjectPath,
  516. BSTR strMethodName,
  517. long lFlags,
  518. IWbemContext *pCtx,
  519. IWbemClassObject *pInParams,
  520. IWbemObjectSink *pHandler
  521. )
  522. {
  523. DBG_FN( "CIP::DoExecMethodAsync" );
  524. DNS_DEBUG( INSTPROV, (
  525. "%s: flags=%lu method=%S %S\n",
  526. fn, lFlags, strMethodName, strObjectPath ));
  527. SCODE sc;
  528. if(strObjectPath == NULL || pHandler == NULL || m_pNamespace == NULL
  529. || strMethodName == NULL )
  530. return WBEM_E_INVALID_PARAMETER;
  531. CDnsBase * pDns;
  532. try
  533. {
  534. CObjPath ObjPath;
  535. if(!ObjPath.Init(strObjectPath))
  536. {
  537. return WBEM_E_INVALID_PARAMETER;
  538. }
  539. wstring wstrClass = ObjPath.GetClassName();
  540. sc = CreateClass(
  541. wstrClass.data(),
  542. m_pNamespace,
  543. (void**) &pDns);
  544. if( FAILED(sc) )
  545. {
  546. return sc;
  547. }
  548. auto_ptr<CDnsBase> pDnsBase(pDns);
  549. sc = pDnsBase->ExecuteMethod(
  550. ObjPath,
  551. strMethodName,
  552. lFlags,
  553. pInParams,
  554. pHandler);
  555. }
  556. catch(CDnsProvException e)
  557. {
  558. DNS_DEBUG( INSTPROV, (
  559. "%s: caught CDnsProvException %s\n",
  560. fn, e.what() ));
  561. CWbemClassObject Status;
  562. sc = SetExtendedStatus(e.what(), Status);
  563. if (SUCCEEDED ( sc ))
  564. {
  565. sc = pHandler->SetStatus(
  566. 0,
  567. WBEM_E_FAILED,
  568. NULL,
  569. *(&Status));
  570. return sc;
  571. }
  572. }
  573. catch(SCODE exSc)
  574. {
  575. sc = exSc;
  576. DNS_DEBUG( INSTPROV, ( "%s: caught SCODE 0x%08X\n", fn, sc ));
  577. }
  578. catch(...)
  579. {
  580. sc = WBEM_E_FAILED;
  581. DNS_DEBUG( INSTPROV, ( "%s: caught unknown exception returning WBEM_E_FAILED\n", fn ));
  582. }
  583. pHandler->SetStatus(0, sc,NULL,NULL);
  584. #ifdef _DEBUG
  585. // _CrtDumpMemoryLeaks();
  586. #endif
  587. DNS_DEBUG( INSTPROV, ( "%s: returning 0x%08X\n", fn, sc ));
  588. return sc;
  589. }
  590. /////////////////////////////////////////////////////////////////////////////
  591. //++
  592. //
  593. // CInstanceProv::SetExtendedStatus
  594. //
  595. // Description:
  596. // create and set extended error status
  597. //
  598. // Arguments:
  599. // ErrString [IN] Error message string
  600. // Inst [IN OUT] reference to WMI instance
  601. //
  602. // Return Value:
  603. // WBEM_S_NO_ERROR
  604. //
  605. //--
  606. /////////////////////////////////////////////////////////////////////////////
  607. SCODE
  608. CInstanceProv::SetExtendedStatus(
  609. const char * ErrString,
  610. CWbemClassObject & Inst )
  611. {
  612. DBG_FN( "CIP::SetExtendedStatus" );
  613. DNS_DEBUG( INSTPROV, ( "%s: error string = %s\n", fn, ErrString ));
  614. IWbemClassObject* pStatus;
  615. BSTR bstrStatus = SysAllocString(L"__ExtendedStatus");
  616. if( bstrStatus == NULL)
  617. {
  618. return WBEM_E_OUT_OF_MEMORY;
  619. }
  620. SCODE sc = m_pNamespace->GetObject(
  621. bstrStatus,
  622. 0,
  623. 0,
  624. &pStatus,
  625. NULL) ;
  626. SysFreeString(bstrStatus);
  627. if( SUCCEEDED ( sc ) )
  628. {
  629. sc = pStatus->SpawnInstance(0, &Inst);
  630. if ( SUCCEEDED ( sc ))
  631. {
  632. sc = Inst.SetProperty(
  633. ErrString,
  634. L"Description");
  635. }
  636. }
  637. return sc;
  638. }