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.

1680 lines
51 KiB

  1. //***************************************************************************
  2. //
  3. // UTILS.CPP
  4. //
  5. // Module: WBEM Instance provider
  6. //
  7. // Purpose: General purpose utilities.
  8. //
  9. // Copyright (c)1998 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "iisprov.h"
  13. //define the static synch object
  14. CSynchObject CUtils::s_synObject;
  15. /////////////////////////////////////////////////////////////////////////////
  16. //
  17. // CUtils::MzCat
  18. //
  19. // Synopsis:
  20. // The metabase has this animal called METADATA_STRINGSZ which has the
  21. // following form: <string><null><string><null><null>. MzCat concatenates
  22. // strings in the defined way. *a_ppdst has the new pointer upon exit. The
  23. // previous value of *a_ppdst is delelted. *a_ppdst == NULL is handled.
  24. //
  25. /////////////////////////////////////////////////////////////////////////////
  26. void CUtils::MzCat (
  27. WCHAR** a_ppdst,
  28. const WCHAR* a_psz
  29. )
  30. {
  31. WCHAR *t_psrc, *t_pdst, *t_pnew;
  32. int t_ilen;
  33. if (a_psz == NULL)
  34. throw WBEM_E_FAILED;
  35. if (*a_ppdst)
  36. {
  37. for ( t_ilen=0, t_psrc = *a_ppdst
  38. ; *t_psrc || *(t_psrc+1)
  39. ; t_psrc++, t_ilen++
  40. )
  41. {
  42. ;
  43. }
  44. t_ilen = t_ilen + wcslen(a_psz)+3;
  45. }
  46. else t_ilen = wcslen(a_psz)+2;
  47. t_pnew = t_pdst = new WCHAR[t_ilen];
  48. if (!t_pdst)
  49. throw WBEM_E_OUT_OF_MEMORY;
  50. if (*a_ppdst)
  51. {
  52. for ( t_psrc = *a_ppdst
  53. ; *t_psrc || *(t_psrc+1)
  54. ; t_pdst++, t_psrc++
  55. )
  56. {
  57. *t_pdst = *t_psrc;
  58. }
  59. *t_pdst = L'\0';
  60. *t_pdst++;
  61. }
  62. wcscpy(t_pdst,a_psz);
  63. *(t_pnew+t_ilen-1)=L'\0';
  64. delete *a_ppdst;
  65. *a_ppdst=t_pnew;
  66. }
  67. /////////////////////////////////////////////////////////////////////////////
  68. //
  69. // CUtils::GetToken
  70. //
  71. // Synopsis:
  72. // *a_ppsz is a pointer to string being parsed. a_pszTok returns the next
  73. // token.
  74. //
  75. /////////////////////////////////////////////////////////////////////////////
  76. void CUtils::GetToken(
  77. WCHAR** a_ppsz,
  78. WCHAR* a_pszTok
  79. )
  80. {
  81. if (*a_ppsz == NULL)
  82. {
  83. *a_pszTok = L'\0';
  84. return;
  85. }
  86. while ( **a_ppsz != L' ' && **a_ppsz )
  87. {
  88. *a_pszTok++ = **a_ppsz,(*a_ppsz)++;
  89. }
  90. *a_pszTok = L'\0';
  91. while ( **a_ppsz == L' ' )
  92. {
  93. (*a_ppsz)++;
  94. }
  95. }
  96. /////////////////////////////////////////////////////////////////////////////
  97. //
  98. // CUtils::GetKey
  99. //
  100. // Synopsis:
  101. // Return the KeyRef pointer from the ParsedObjectPath for the given string.
  102. //
  103. /////////////////////////////////////////////////////////////////////////////
  104. KeyRef* CUtils::GetKey(
  105. ParsedObjectPath* a_p,
  106. WCHAR* a_psz
  107. )
  108. {
  109. KeyRef* t_pkr;
  110. DWORD t_numkeys = a_p->m_dwNumKeys;
  111. DWORD t_c;
  112. for ( t_c=0; t_numkeys; t_numkeys--,t_c++ )
  113. {
  114. t_pkr = *(a_p->m_paKeys + t_c);
  115. if (!lstrcmpiW(t_pkr->m_pName,a_psz))
  116. return t_pkr;
  117. }
  118. return NULL;
  119. }
  120. /////////////////////////////////////////////////////////////////////////////
  121. //
  122. // CUtils::GetAssociation
  123. //
  124. // Synopsis:
  125. // Association a_pszAssociationName is returned in a_ppAssociation if found.
  126. // Returns true if association is found false otherwise.
  127. //
  128. /////////////////////////////////////////////////////////////////////////////
  129. bool CUtils::GetAssociation(
  130. LPCWSTR a_pszAssociationName,
  131. WMI_ASSOCIATION** a_ppassociation
  132. )
  133. {
  134. WMI_ASSOCIATION** t_ppassociation;
  135. if (a_pszAssociationName == NULL || a_ppassociation==NULL)
  136. throw WBEM_E_INVALID_CLASS;
  137. for ( t_ppassociation = WMI_ASSOCIATION_DATA::s_WmiAssociations
  138. ; *t_ppassociation != NULL
  139. ;t_ppassociation++
  140. )
  141. {
  142. if (_wcsicmp(a_pszAssociationName,(*t_ppassociation)->pszAssociationName) ==0)
  143. {
  144. *a_ppassociation = *t_ppassociation;
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. /////////////////////////////////////////////////////////////////////////////
  151. //
  152. // CUtils::GetMetabasePath
  153. //
  154. // Synopsis:
  155. //
  156. /////////////////////////////////////////////////////////////////////////////
  157. void CUtils::GetMetabasePath(
  158. IWbemClassObject* a_pObj,
  159. ParsedObjectPath* a_p,
  160. WMI_CLASS* a_pclass,
  161. _bstr_t& a_bstrPath
  162. )
  163. {
  164. KeyRef* t_pkr;
  165. WCHAR* t_pszKey = a_pclass->pszKeyName;
  166. WCHAR* t_psz;
  167. if (a_p == NULL || a_pclass == NULL)
  168. throw WBEM_E_FAILED;
  169. if (a_pclass->pszKeyName == NULL)
  170. {
  171. a_bstrPath = a_pclass->pszMetabaseKey;
  172. return;
  173. }
  174. t_psz = new WCHAR[wcslen(t_pszKey) + 1];
  175. if(!t_psz)
  176. throw WBEM_E_OUT_OF_MEMORY;
  177. try
  178. {
  179. a_bstrPath = a_pclass->pszMetabaseKey;
  180. for ( GetToken(&t_pszKey,t_psz); *t_psz; GetToken(&t_pszKey,t_psz) )
  181. {
  182. if (*t_psz == L'/')
  183. a_bstrPath += t_psz;
  184. else
  185. {
  186. if (*t_psz == L'#')
  187. t_pkr = GetKey(a_p,&t_psz[1]);
  188. else
  189. t_pkr = GetKey(a_p,t_psz);
  190. if(t_pkr == NULL)
  191. break;
  192. if (a_pObj)
  193. {
  194. _bstr_t t_bstr = t_pkr->m_pName;
  195. HRESULT t_hr = a_pObj->Put(t_bstr, 0, &t_pkr->m_vValue, 0);
  196. THROW_ON_ERROR(t_hr);
  197. }
  198. switch ((t_pkr)->m_vValue.vt)
  199. {
  200. case VT_I4:
  201. swprintf(t_psz,L"/%d",t_pkr->m_vValue.lVal);
  202. a_bstrPath += t_psz;
  203. break;
  204. case VT_BSTR:
  205. a_bstrPath += L"/";
  206. a_bstrPath += t_pkr->m_vValue.bstrVal;
  207. break;
  208. }
  209. }
  210. }
  211. delete [] t_psz;
  212. }
  213. catch(...)
  214. {
  215. delete [] t_psz;
  216. }
  217. return;
  218. }
  219. /////////////////////////////////////////////////////////////////////////////
  220. //
  221. // CUtils::GetClass
  222. //
  223. // Synopsis:
  224. // Class a_pszClass is returned in a_ppclass if found.
  225. // Returns true if association is found false otherwise.
  226. //
  227. /////////////////////////////////////////////////////////////////////////////
  228. bool CUtils::GetClass(
  229. LPCWSTR a_pszClass,
  230. WMI_CLASS** a_ppclass
  231. )
  232. {
  233. WMI_CLASS** t_ppclass;
  234. if (a_pszClass == NULL || a_ppclass==NULL)
  235. throw WBEM_E_INVALID_CLASS;
  236. for (t_ppclass = WMI_CLASS_DATA::s_WmiClasses; *t_ppclass != NULL;t_ppclass++)
  237. if (_wcsicmp(a_pszClass,(*t_ppclass)->pszClassName) ==0)
  238. {
  239. *a_ppclass = *t_ppclass;
  240. return true;
  241. }
  242. return false;
  243. }
  244. /////////////////////////////////////////////////////////////////////////////
  245. //
  246. // CUtils::GetMethod
  247. //
  248. // Synopsis:
  249. // The Method descriptor for a_pszMethod is returned via a_ppMethod if found
  250. // NULL otherwise. Returns true if found false otherwise.
  251. //
  252. /////////////////////////////////////////////////////////////////////////////
  253. bool CUtils::GetMethod(
  254. LPCWSTR a_pszMethod,
  255. WMI_METHOD** a_ppmethodList,
  256. WMI_METHOD** a_ppmethod
  257. )
  258. {
  259. WMI_METHOD** t_ppmethod;
  260. if (a_pszMethod == NULL || a_ppmethod == NULL)
  261. throw WBEM_E_FAILED;
  262. for (t_ppmethod = a_ppmethodList; *t_ppmethod != NULL;t_ppmethod++)
  263. if (_wcsicmp(a_pszMethod,(*t_ppmethod)->pszMethodName) ==0)
  264. {
  265. *a_ppmethod = *t_ppmethod;
  266. return true;
  267. }
  268. return false;
  269. }
  270. /////////////////////////////////////////////////////////////////////////////
  271. //
  272. // CUtils::ExecMethodAsync
  273. //
  274. // Synopsis:
  275. //
  276. /////////////////////////////////////////////////////////////////////////////
  277. void CUtils::ExecMethodAsync(
  278. BSTR a_strObjectPath,
  279. BSTR a_strMethodName,
  280. IWbemContext* a_pCtx,
  281. IWbemClassObject* a_pInParams,
  282. IWbemObjectSink* a_pHandler,
  283. CWbemServices* a_pNameSpace
  284. )
  285. {
  286. WMI_CLASS* t_pWMIClass;
  287. CObjectPathParser t_PathParser(e_ParserAcceptRelativeNamespace);
  288. ParsedObjectPath* t_pParsedObject = NULL;
  289. _bstr_t t_bstrMbPath;
  290. WMI_METHOD* t_ppmethod;
  291. METADATA_HANDLE t_hKey = NULL;
  292. try
  293. {
  294. if (t_PathParser.Parse(a_strObjectPath, &t_pParsedObject) != CObjectPathParser::NoError)
  295. throw WBEM_E_INVALID_PARAMETER;
  296. if (t_pParsedObject == NULL)
  297. throw WBEM_E_FAILED;
  298. if (!GetClass(t_pParsedObject->m_pClass,&t_pWMIClass))
  299. throw WBEM_E_INVALID_CLASS;
  300. if (!GetMethod(a_strMethodName, t_pWMIClass->ppMethod, &t_ppmethod ))
  301. throw WBEM_E_NOT_SUPPORTED;
  302. GetMetabasePath(NULL,t_pParsedObject,t_pWMIClass,t_bstrMbPath);
  303. switch(t_pWMIClass->eKeyType)
  304. {
  305. case IIsFtpService:
  306. if(a_pHandler == NULL)
  307. throw WBEM_E_INVALID_PARAMETER;
  308. ExecFtpServiceMethod(
  309. t_bstrMbPath,
  310. t_pWMIClass->pszClassName,
  311. t_ppmethod->pszMethodName,
  312. a_pCtx,
  313. a_pInParams,
  314. a_pHandler,
  315. a_pNameSpace
  316. );
  317. break;
  318. case IIsWebService:
  319. if(a_pHandler == NULL)
  320. throw WBEM_E_INVALID_PARAMETER;
  321. ExecWebServiceMethod(
  322. t_bstrMbPath,
  323. t_pWMIClass->pszClassName,
  324. t_ppmethod->pszMethodName,
  325. a_pCtx,
  326. a_pInParams,
  327. a_pHandler,
  328. a_pNameSpace
  329. );
  330. break;
  331. case IIsFtpServer:
  332. case IIsWebServer:
  333. {
  334. CMetabase t_metabase;
  335. t_hKey = t_metabase.OpenKey(t_bstrMbPath, true);
  336. t_metabase.PutMethod(t_hKey, t_ppmethod->dwMDId);
  337. t_metabase.CloseKey(t_hKey);
  338. // check if the method call is successful.
  339. Sleep(500); // 0.5 sec
  340. t_hKey = t_metabase.OpenKey(t_bstrMbPath, false);
  341. long lWin32Error = t_metabase.GetWin32Error(t_hKey);
  342. t_metabase.CloseKey(t_hKey);
  343. THROW_ON_ERROR(HRESULT_FROM_WIN32(lWin32Error));
  344. }
  345. break;
  346. case IIsWebVirtualDir:
  347. case IIsWebDirectory:
  348. if(a_pHandler == NULL)
  349. throw WBEM_E_INVALID_PARAMETER;
  350. ExecWebAppMethod(
  351. t_bstrMbPath,
  352. t_pWMIClass->pszClassName,
  353. t_ppmethod->pszMethodName,
  354. a_pCtx,
  355. a_pInParams,
  356. a_pHandler,
  357. a_pNameSpace
  358. );
  359. break;
  360. case IIsComputer:
  361. if(a_pHandler == NULL)
  362. throw WBEM_E_INVALID_PARAMETER;
  363. ExecComputerMethod(
  364. t_bstrMbPath,
  365. t_pWMIClass->pszClassName,
  366. t_ppmethod->pszMethodName,
  367. a_pCtx,
  368. a_pInParams,
  369. a_pHandler,
  370. a_pNameSpace
  371. );
  372. break;
  373. case IIsCertMapper:
  374. if(a_pHandler == NULL)
  375. throw WBEM_E_INVALID_PARAMETER;
  376. ExecCertMapperMethod(
  377. t_bstrMbPath,
  378. t_pWMIClass->pszClassName,
  379. t_ppmethod->pszMethodName,
  380. a_pCtx,
  381. a_pInParams,
  382. a_pHandler,
  383. a_pNameSpace
  384. );
  385. break;
  386. default:
  387. break;
  388. }
  389. if (t_pParsedObject)
  390. t_PathParser.Free(t_pParsedObject);
  391. }
  392. catch (...)
  393. {
  394. if (t_pParsedObject)
  395. t_PathParser.Free(t_pParsedObject);
  396. throw;
  397. };
  398. }
  399. /////////////////////////////////////////////////////////////////////////////
  400. //
  401. // CUtils::DeleteObjectAsync
  402. //
  403. // Synopsis:
  404. //
  405. /////////////////////////////////////////////////////////////////////////////
  406. void CUtils::DeleteObjectAsync(
  407. CWbemServices* m_pNamespace,
  408. ParsedObjectPath* a_pParsedObject,
  409. CMetabase& a_metabase
  410. )
  411. {
  412. HRESULT t_hr = ERROR_SUCCESS;
  413. _bstr_t t_bstrMbPath;
  414. WMI_CLASS* t_pWMIClass;
  415. METADATA_HANDLE t_hKey = NULL;
  416. if (m_pNamespace==NULL || a_pParsedObject==NULL)
  417. throw WBEM_E_INVALID_PARAMETER;
  418. if (!GetClass(a_pParsedObject->m_pClass,&t_pWMIClass))
  419. throw WBEM_E_INVALID_CLASS;
  420. // get the mata path of object
  421. GetMetabasePath(NULL,a_pParsedObject,t_pWMIClass,t_bstrMbPath);
  422. // check if the path is not existed
  423. if(!a_metabase.CheckKey(t_bstrMbPath))
  424. throw WBEM_E_INVALID_PARAMETER;
  425. try
  426. {
  427. // if AdminACL
  428. if( t_pWMIClass->eKeyType == TYPE_AdminACL )
  429. throw WBEM_E_NOT_SUPPORTED;
  430. else if(t_pWMIClass->eKeyType == TYPE_IPSecurity )
  431. {
  432. t_hKey = a_metabase.OpenKey(t_bstrMbPath, true);
  433. a_metabase.DeleteData(t_hKey, MD_IP_SEC, BINARY_METADATA);
  434. a_metabase.CloseKey(t_hKey);
  435. return;
  436. }
  437. else if(t_pWMIClass->eKeyType == TYPE_AdminACE)
  438. {
  439. CAdminACL objACL;
  440. t_hr = objACL.OpenSD(t_bstrMbPath);
  441. if(SUCCEEDED(t_hr))
  442. t_hr = objACL.DeleteObjectAsync(a_pParsedObject);
  443. THROW_ON_ERROR(t_hr);
  444. return;
  445. }
  446. t_hKey = a_metabase.OpenKey(METADATA_MASTER_ROOT_HANDLE, true);
  447. t_hr = a_metabase.DeleteKey(t_hKey, t_bstrMbPath);
  448. THROW_ON_ERROR(t_hr);
  449. a_metabase.CloseKey(t_hKey);
  450. }
  451. catch (...)
  452. {
  453. a_metabase.CloseKey(t_hKey);
  454. throw;
  455. };
  456. }
  457. /////////////////////////////////////////////////////////////////////////////
  458. //
  459. // CUtils::GetObjectAsync
  460. //
  461. // Synopsis:
  462. //
  463. /////////////////////////////////////////////////////////////////////////////
  464. HRESULT CUtils::GetObjectAsync(
  465. CWbemServices* m_pNamespace,
  466. IWbemClassObject** a_ppObj,
  467. ParsedObjectPath* a_pParsedObject,
  468. CMetabase& a_metabase
  469. )
  470. {
  471. HRESULT t_hr = WBEM_E_FAILED;
  472. IWbemClassObject* t_pClass = NULL;
  473. METABASE_PROPERTY** t_ppmbp;
  474. _bstr_t t_bstrMbPath;
  475. WMI_CLASS* t_pWMIClass;
  476. METADATA_HANDLE t_hKey = NULL;
  477. if (m_pNamespace==NULL || a_ppObj==NULL || a_pParsedObject==NULL)
  478. return WBEM_E_INVALID_PARAMETER;
  479. try
  480. {
  481. if (!GetClass(a_pParsedObject->m_pClass,&t_pWMIClass))
  482. return WBEM_E_INVALID_CLASS;
  483. t_hr = m_pNamespace->GetObject(
  484. a_pParsedObject->m_pClass,
  485. 0,
  486. NULL,
  487. &t_pClass,
  488. NULL
  489. );
  490. THROW_ON_ERROR(t_hr);
  491. t_hr = t_pClass->SpawnInstance(0, a_ppObj);
  492. t_pClass->Release();
  493. THROW_ON_ERROR(t_hr);
  494. GetMetabasePath(*a_ppObj,a_pParsedObject,t_pWMIClass,t_bstrMbPath);
  495. // if AdminACL
  496. if( t_pWMIClass->eKeyType == TYPE_AdminACL ||
  497. t_pWMIClass->eKeyType == TYPE_AdminACE
  498. )
  499. {
  500. CAdminACL objACL;
  501. t_hr = objACL.OpenSD(t_bstrMbPath);
  502. if(SUCCEEDED(t_hr))
  503. t_hr = objACL.GetObjectAsync(*a_ppObj, a_pParsedObject, t_pWMIClass);
  504. return t_hr;
  505. }
  506. else if( t_pWMIClass->eKeyType == TYPE_IPSecurity ) // IPSecurity
  507. {
  508. CIPSecurity IPSecurity;
  509. t_hr = IPSecurity.OpenSD(t_bstrMbPath);
  510. if(SUCCEEDED(t_hr))
  511. t_hr = IPSecurity.GetObjectAsync(*a_ppObj);
  512. return t_hr;
  513. }
  514. t_hKey = a_metabase.OpenKey(t_bstrMbPath, false);
  515. _variant_t t_vt;
  516. for (t_ppmbp=t_pWMIClass->ppmbp;*t_ppmbp; t_ppmbp++)
  517. {
  518. switch ((*t_ppmbp)->dwMDDataType)
  519. {
  520. case DWORD_METADATA:
  521. a_metabase.GetDword(t_hKey, *t_ppmbp, t_vt);
  522. break;
  523. case EXPANDSZ_METADATA:
  524. case STRING_METADATA:
  525. a_metabase.GetString(t_hKey, *t_ppmbp, t_vt);
  526. break;
  527. case MULTISZ_METADATA:
  528. a_metabase.GetMultiSz(t_hKey, *t_ppmbp, t_vt);
  529. break;
  530. default:
  531. break;
  532. }
  533. _bstr_t t_bstr = (*t_ppmbp)->pszPropName;
  534. t_hr = (*a_ppObj)->Put(t_bstr, 0, &t_vt, 0);
  535. t_vt.Clear();
  536. if(FAILED(t_hr))
  537. break;
  538. }
  539. a_metabase.CloseKey(t_hKey);
  540. }
  541. catch (...)
  542. {
  543. a_metabase.CloseKey(t_hKey);
  544. if (*a_ppObj)
  545. {
  546. (*a_ppObj)->Release();
  547. *a_ppObj = NULL;
  548. }
  549. };
  550. return t_hr;
  551. }
  552. /////////////////////////////////////////////////////////////////////////////
  553. //
  554. // CUtils::PutObjectAsync
  555. //
  556. // Synopsis:
  557. //
  558. //
  559. /////////////////////////////////////////////////////////////////////////////
  560. void CUtils::PutObjectAsync(
  561. IWbemClassObject* a_pObj,
  562. IWbemClassObject* a_pObjOld,
  563. ParsedObjectPath* a_pParsedObject,
  564. long a_lFlags
  565. )
  566. {
  567. HRESULT t_hr = ERROR_SUCCESS;
  568. METABASE_PROPERTY** t_ppmbp;
  569. _bstr_t t_bstrMbPath;
  570. WMI_CLASS* t_pWMIClass;
  571. METADATA_HANDLE t_hKey = NULL;
  572. bool t_boolOverrideParent = false;
  573. if (a_pObj==NULL || a_pParsedObject==NULL)
  574. throw WBEM_E_INVALID_PARAMETER;
  575. if (!GetClass(a_pParsedObject->m_pClass,&t_pWMIClass))
  576. throw WBEM_E_INVALID_CLASS;
  577. GetMetabasePath(NULL,a_pParsedObject,t_pWMIClass,t_bstrMbPath);
  578. // if AdminACL
  579. if( t_pWMIClass->eKeyType == TYPE_AdminACL ||
  580. t_pWMIClass->eKeyType == TYPE_AdminACE
  581. )
  582. {
  583. CAdminACL objACL;
  584. t_hr = objACL.OpenSD(t_bstrMbPath);
  585. if( SUCCEEDED(t_hr) )
  586. t_hr = objACL.PutObjectAsync(a_pObj, a_pParsedObject, t_pWMIClass);
  587. THROW_ON_ERROR(t_hr);
  588. return;
  589. }
  590. if( t_pWMIClass->eKeyType == TYPE_IPSecurity ) // IPSecurity
  591. {
  592. CIPSecurity objIPSec;
  593. t_hr = objIPSec.OpenSD(t_bstrMbPath);
  594. if( SUCCEEDED(t_hr) )
  595. t_hr = objIPSec.PutObjectAsync(a_pObj);
  596. THROW_ON_ERROR(t_hr);
  597. return;
  598. }
  599. // Get Instance Qualifiers
  600. IWbemQualifierSet* t_pQualSet = NULL;
  601. BSTR t_bstrQualName = NULL;
  602. VARIANT t_varQualValue;
  603. t_hr = a_pObj->GetQualifierSet(&t_pQualSet);
  604. if(SUCCEEDED(t_hr)) {
  605. t_hr = t_pQualSet->BeginEnumeration(0);
  606. }
  607. THROW_ON_ERROR(t_hr);
  608. // Looking for OverrideParent qualifier
  609. while(!t_boolOverrideParent)
  610. {
  611. t_hr = t_pQualSet->Next(0, &t_bstrQualName, &t_varQualValue, NULL);
  612. if(t_hr == WBEM_S_NO_MORE_DATA || !SUCCEEDED(t_hr)) {
  613. // No more qualifiers.
  614. // We don't need to worry about cleanup - nothing was allocated.
  615. break;
  616. }
  617. if(lstrcmpW(t_bstrQualName, WSZ_OVERRIDE_PARENT) == 0) {
  618. if(t_varQualValue.vt == VT_BOOL) {
  619. if(t_varQualValue.boolVal) {
  620. t_boolOverrideParent = true;
  621. }
  622. }
  623. }
  624. SysFreeString(t_bstrQualName);
  625. VariantClear(&t_varQualValue);
  626. }
  627. t_pQualSet->Release();
  628. if(!SUCCEEDED(t_hr))
  629. THROW_ON_ERROR(t_hr);
  630. t_hr = ERROR_SUCCESS;
  631. // open key
  632. CMetabase t_metabase;
  633. t_hKey = t_metabase.CreateKey(t_bstrMbPath);
  634. try
  635. {
  636. _variant_t t_vt;
  637. _variant_t t_vtOld;
  638. _bstr_t t_bstr;
  639. for (t_ppmbp=t_pWMIClass->ppmbp;*t_ppmbp && t_hr==ERROR_SUCCESS; t_ppmbp++)
  640. {
  641. t_bstr = (*t_ppmbp)->pszPropName;
  642. t_hr = a_pObj->Get(t_bstr, 0, &t_vt, NULL, NULL);
  643. THROW_E_ON_ERROR(t_hr,*t_ppmbp);
  644. if(a_pObjOld != NULL) {
  645. t_hr = a_pObjOld->Get(t_bstr, 0, &t_vtOld, NULL, NULL);
  646. THROW_E_ON_ERROR(t_hr,*t_ppmbp);
  647. }
  648. if (t_vt.vt == VT_NULL)
  649. {
  650. // Only delete non-flag properties.
  651. if ((*t_ppmbp)->dwMDMask == 0)
  652. {
  653. t_metabase.DeleteData(t_hKey, *t_ppmbp);
  654. }
  655. continue;
  656. }
  657. switch ((*t_ppmbp)->dwMDDataType)
  658. {
  659. case DWORD_METADATA:
  660. t_metabase.PutDword(t_hKey, *t_ppmbp,t_vt,&t_vtOld,t_boolOverrideParent);
  661. break;
  662. case EXPANDSZ_METADATA:
  663. case STRING_METADATA:
  664. t_metabase.PutString(t_hKey, *t_ppmbp,t_vt,&t_vtOld,t_boolOverrideParent);
  665. break;
  666. case MULTISZ_METADATA:
  667. t_metabase.PutMultiSz(t_hKey, *t_ppmbp,t_vt,&t_vtOld,t_boolOverrideParent);
  668. break;
  669. default:
  670. break;
  671. }
  672. t_vt.Clear();
  673. t_vtOld.Clear();
  674. }
  675. WCHAR szBuffer[MAX_KEY_TYPE_SIZE];
  676. if(TypeEnumToString(szBuffer, t_pWMIClass->eKeyType))
  677. {
  678. t_vt = szBuffer;
  679. t_metabase.PutString(t_hKey, &METABASE_PROPERTY_DATA::s_KeyType, t_vt, NULL);
  680. }
  681. t_metabase.CloseKey(t_hKey);
  682. }
  683. catch(...)
  684. {
  685. t_metabase.CloseKey(t_hKey);
  686. throw;
  687. }
  688. }
  689. /////////////////////////////////////////////////////////////////////////////
  690. //
  691. // CUtils::EnumObjectAsync
  692. //
  693. // Synopsis:
  694. //
  695. //
  696. /////////////////////////////////////////////////////////////////////////////
  697. void CUtils::EnumObjectAsync(
  698. BSTR a_ClassName,
  699. CWbemServices* m_pNamespace,
  700. IWbemObjectSink FAR* a_pHandler
  701. )
  702. {
  703. WMI_CLASS* t_pClass;
  704. WMI_ASSOCIATION* t_pAssociation = NULL;
  705. ParsedObjectPath t_ParsedObject; //deconstructer frees memory
  706. CObjectPathParser t_PathParser(e_ParserAcceptRelativeNamespace);
  707. if (GetAssociation(a_ClassName,&t_pAssociation))
  708. {
  709. CEnum EnumAssociation;
  710. EnumAssociation.Init(
  711. a_pHandler,
  712. m_pNamespace,
  713. &t_ParsedObject,
  714. t_pAssociation->pcRight->pszMetabaseKey,
  715. t_pAssociation
  716. );
  717. EnumAssociation.Recurse(
  718. NULL,
  719. IIsComputer,
  720. NULL,
  721. t_pAssociation->pcRight->pszKeyName,
  722. t_pAssociation->pcRight->eKeyType
  723. );
  724. }
  725. else if (GetClass(a_ClassName,&t_pClass))
  726. {
  727. if (!t_ParsedObject.SetClassName(t_pClass->pszClassName))
  728. throw WBEM_E_FAILED;
  729. CEnum EnumObject;
  730. EnumObject.Init(
  731. a_pHandler,
  732. m_pNamespace,
  733. &t_ParsedObject,
  734. t_pClass->pszMetabaseKey,
  735. NULL
  736. );
  737. EnumObject.Recurse(
  738. NULL,
  739. NO_TYPE,
  740. NULL,
  741. t_pClass->pszKeyName,
  742. t_pClass->eKeyType
  743. );
  744. }
  745. else
  746. throw WBEM_E_INVALID_CLASS;
  747. }
  748. bool CUtils::TypeStringToEnum(
  749. enum_KEY_TYPE& a_eType,
  750. LPCWSTR a_szTypeString
  751. )
  752. {
  753. if(!lstrcmpiW(a_szTypeString, L"IIsWebVirtualDir"))
  754. a_eType = IIsWebVirtualDir;
  755. else if(!lstrcmpiW(a_szTypeString, L"IIsWebDirectory"))
  756. a_eType = IIsWebDirectory;
  757. else if(!lstrcmpiW(a_szTypeString, L"IIsWebFile"))
  758. a_eType = IIsWebFile;
  759. else if(!lstrcmpiW(a_szTypeString, L"IIsWebServer"))
  760. a_eType = IIsWebServer;
  761. else if(!lstrcmpiW(a_szTypeString, L"IIsWebService"))
  762. a_eType = IIsWebService;
  763. else if(!lstrcmpiW(a_szTypeString, L"IIsFtpVirtualDir"))
  764. a_eType = IIsFtpVirtualDir;
  765. else if(!lstrcmpiW(a_szTypeString, L"IIsFtpServer"))
  766. a_eType = IIsFtpServer;
  767. else if(!lstrcmpiW(a_szTypeString, L"IIsFtpService"))
  768. a_eType = IIsFtpService;
  769. else if(!lstrcmpiW(a_szTypeString, L"IIsFilters"))
  770. a_eType = IIsFilters;
  771. else if(!lstrcmpiW(a_szTypeString, L"IIsFilter"))
  772. a_eType = IIsFilter;
  773. else if(!lstrcmpiW(a_szTypeString, L"IIsWebInfo"))
  774. a_eType = IIsWebInfo;
  775. else if(!lstrcmpiW(a_szTypeString, L"IIsFtpInfo"))
  776. a_eType = IIsFtpInfo;
  777. else if(!lstrcmpiW(a_szTypeString, L"IIsCertMapper"))
  778. a_eType = IIsCertMapper;
  779. else if(!lstrcmpiW(a_szTypeString, L"IIsComputer"))
  780. a_eType = IIsComputer;
  781. else if(!lstrcmpiW(a_szTypeString, L"IIsMimeMap"))
  782. a_eType = IIsMimeMap;
  783. else if(!lstrcmpiW(a_szTypeString, L"IIsLogModules"))
  784. a_eType = IIsLogModules;
  785. else if(!lstrcmpiW(a_szTypeString, L"IIsLogModule"))
  786. a_eType = IIsLogModule;
  787. else if(!lstrcmpiW(a_szTypeString, L"IIsCompressionSchemes"))
  788. a_eType = IIsCompressionSchemes;
  789. else if(!lstrcmpiW(a_szTypeString, L"IIsCompressionScheme"))
  790. a_eType = IIsCompressionScheme;
  791. else
  792. return false;
  793. return true;
  794. }
  795. bool CUtils::TypeEnumToString(
  796. LPWSTR a_szTypeString,
  797. enum_KEY_TYPE a_eType
  798. )
  799. {
  800. bool bRet = true;
  801. switch(a_eType)
  802. {
  803. case IIsComputer:
  804. lstrcpyW(a_szTypeString, L"IIsComputer");
  805. break;
  806. case IIsMimeMap:
  807. lstrcpyW(a_szTypeString, L"IIsMimeMap");
  808. break;
  809. case IIsLogModules:
  810. lstrcpyW(a_szTypeString, L"IIsLogModules");
  811. break;
  812. case IIsLogModule:
  813. lstrcpyW(a_szTypeString, L"IIsLogModule");
  814. break;
  815. case IIsFtpService:
  816. lstrcpyW(a_szTypeString, L"IIsFtpService");
  817. break;
  818. case IIsFtpInfo:
  819. lstrcpyW(a_szTypeString, L"IIsFtpInfo");
  820. break;
  821. case IIsFtpServer:
  822. lstrcpyW(a_szTypeString, L"IIsFtpServer");
  823. break;
  824. case IIsFtpVirtualDir:
  825. lstrcpyW(a_szTypeString, L"IIsFtpVirtualDir");
  826. break;
  827. case IIsWebService:
  828. lstrcpyW(a_szTypeString, L"IIsWebService");
  829. break;
  830. case IIsWebInfo:
  831. lstrcpyW(a_szTypeString, L"IIsWebInfo");
  832. break;
  833. case IIsFilters:
  834. lstrcpyW(a_szTypeString, L"IIsFilters");
  835. break;
  836. case IIsFilter:
  837. lstrcpyW(a_szTypeString, L"IIsFilter");
  838. break;
  839. case IIsWebServer:
  840. lstrcpyW(a_szTypeString, L"IIsWebServer");
  841. break;
  842. case IIsCertMapper:
  843. lstrcpyW(a_szTypeString, L"IIsCertMapper");
  844. break;
  845. case IIsWebVirtualDir:
  846. lstrcpyW(a_szTypeString, L"IIsWebVirtualDir");
  847. break;
  848. case IIsWebDirectory:
  849. lstrcpyW(a_szTypeString, L"IIsWebDirectory");
  850. break;
  851. case IIsWebFile:
  852. lstrcpyW(a_szTypeString, L"IIsWebFile");
  853. break;
  854. case IIsCompressionSchemes:
  855. lstrcpyW(a_szTypeString, L"IIsCompressionSchemes");
  856. break;
  857. case IIsCompressionScheme:
  858. lstrcpyW(a_szTypeString, L"IIsCompressionScheme");
  859. break;
  860. default:
  861. bRet = false;
  862. break;
  863. }
  864. return bRet;
  865. }
  866. void CUtils::ExecWebAppMethod(
  867. LPCWSTR a_szMbPath,
  868. LPCWSTR a_szClassName,
  869. LPCWSTR a_szMethodName,
  870. IWbemContext* a_pCtx,
  871. IWbemClassObject* a_pInParams,
  872. IWbemObjectSink* a_pHandler,
  873. CWbemServices* a_pNameSpace
  874. )
  875. {
  876. HRESULT hr;
  877. _variant_t t_vt;
  878. CWebAppMethod obj;
  879. if(!lstrcmpiW(a_szMethodName, L"AppCreate"))
  880. {
  881. if(a_pInParams == NULL)
  882. throw WBEM_E_INVALID_PARAMETER;
  883. a_pInParams->Get(L"InProcFlag", 0, &t_vt, NULL, NULL);
  884. hr = obj.AppCreate(a_szMbPath, t_vt);
  885. }
  886. else if(!lstrcmpiW(a_szMethodName, L"AppCreate2"))
  887. {
  888. if(a_pInParams == NULL)
  889. throw WBEM_E_INVALID_PARAMETER;
  890. a_pInParams->Get(L"AppMode", 0, &t_vt, NULL, NULL);
  891. hr = obj.AppCreate2(a_szMbPath, t_vt);
  892. }
  893. else if(!lstrcmpiW(a_szMethodName, L"AppDelete"))
  894. {
  895. if(a_pInParams == NULL)
  896. throw WBEM_E_INVALID_PARAMETER;
  897. a_pInParams->Get(L"Recursive", 0, &t_vt, NULL, NULL);
  898. hr = obj.AppDelete(a_szMbPath, t_vt);
  899. }
  900. else if(!lstrcmpiW(a_szMethodName, L"AppDisable"))
  901. {
  902. if(a_pInParams == NULL)
  903. throw WBEM_E_INVALID_PARAMETER;
  904. a_pInParams->Get(L"Recursive", 0, &t_vt, NULL, NULL);
  905. hr = obj.AppDisable(a_szMbPath, t_vt);
  906. }
  907. else if(!lstrcmpiW(a_szMethodName, L"AppEnable"))
  908. {
  909. if(a_pInParams == NULL)
  910. throw WBEM_E_INVALID_PARAMETER;
  911. a_pInParams->Get(L"Recursive", 0, &t_vt, NULL, NULL);
  912. hr = obj.AppEnable(a_szMbPath, t_vt);
  913. }
  914. else if(!lstrcmpiW(a_szMethodName, L"AppUnLoad"))
  915. {
  916. if(a_pInParams == NULL)
  917. throw WBEM_E_INVALID_PARAMETER;
  918. a_pInParams->Get(L"Recursive", 0, &t_vt, NULL, NULL);
  919. hr = obj.AppUnLoad(a_szMbPath, t_vt);
  920. }
  921. else if(!lstrcmpiW(a_szMethodName, L"AppGetStatus"))
  922. {
  923. // call method - AppGetStatus
  924. DWORD dwStatus;
  925. hr = obj.AppGetStatus(a_szMbPath, &dwStatus);
  926. THROW_ON_ERROR(hr);
  927. IWbemClassObject* pClass = NULL;
  928. IWbemClassObject* pMethodClass = NULL;
  929. IWbemClassObject* pOutParams = NULL;
  930. hr = a_pNameSpace->GetObject(_bstr_t(a_szClassName), 0, a_pCtx, &pClass, NULL);
  931. THROW_ON_ERROR(hr);
  932. // This method returns values, and so create an instance of the
  933. // output argument class.
  934. hr = pClass->GetMethod(a_szMethodName, 0, NULL, &pMethodClass);
  935. pClass->Release();
  936. THROW_ON_ERROR(hr);
  937. hr = pMethodClass->SpawnInstance(0, &pOutParams);
  938. pMethodClass->Release();
  939. THROW_ON_ERROR(hr);
  940. // put it into the output object
  941. t_vt.vt = VT_I4;
  942. t_vt.lVal = dwStatus;
  943. hr = pOutParams->Put(L"ReturnValue", 0, &t_vt, 0);
  944. THROW_ON_ERROR(hr);
  945. // Send the output object back to the client via the sink. Then
  946. // release the pointers and free the strings.
  947. hr = a_pHandler->Indicate(1, &pOutParams);
  948. pOutParams->Release();
  949. }
  950. else if(!lstrcmpiW(a_szMethodName, L"AspAppRestart"))
  951. {
  952. if(a_pInParams == NULL)
  953. throw WBEM_E_INVALID_PARAMETER;
  954. hr = obj.AspAppRestart(a_szMbPath);
  955. }
  956. else
  957. hr = WBEM_E_NOT_SUPPORTED;
  958. THROW_ON_ERROR(hr);
  959. }
  960. void CUtils::ExecFtpServiceMethod(
  961. LPCWSTR a_szMbPath,
  962. LPCWSTR a_szClassName,
  963. LPCWSTR a_szMethodName,
  964. IWbemContext* a_pCtx,
  965. IWbemClassObject* a_pInParams,
  966. IWbemObjectSink* a_pHandler,
  967. CWbemServices* a_pNameSpace
  968. )
  969. {
  970. HRESULT hr = ERROR_SUCCESS;
  971. _variant_t t_vt1, t_vt2, t_vt3, t_vt4;
  972. CMetabase t_metabase;
  973. METADATA_HANDLE t_hKey;
  974. if(!lstrcmpiW(a_szMethodName, L"CreateNewServer"))
  975. {
  976. if(a_pInParams == NULL)
  977. throw WBEM_E_INVALID_PARAMETER;
  978. // synchronize
  979. s_synObject.Enter();
  980. try
  981. {
  982. // get in params
  983. a_pInParams->Get(L"ServerComment", 0, &t_vt2, NULL, NULL);
  984. a_pInParams->Get(L"ServerBindings", 0, &t_vt3, NULL, NULL);
  985. a_pInParams->Get(L"PathOfRootVitualDir", 0, &t_vt4, NULL, NULL);
  986. _bstr_t t_bstrServicePath = a_szMbPath;
  987. _bstr_t t_bstrServerPath = t_bstrServicePath;
  988. // check the optional [in] parameter "ServerNumber"
  989. a_pInParams->Get(L"ServerNumber", 0, &t_vt1, NULL, NULL);
  990. if( t_vt1.vt == VT_BSTR )
  991. {
  992. t_bstrServerPath += L"/";
  993. t_bstrServerPath += t_vt1.bstrVal;
  994. // check if the server path is not existed
  995. if(t_metabase.CheckKey(t_bstrServerPath))
  996. throw WBEM_E_INVALID_PARAMETER;
  997. }
  998. else // if no server is specified
  999. {
  1000. // find an unique server name(number) and create it
  1001. FindUniqueServerName(t_bstrServicePath, t_bstrServerPath);
  1002. }
  1003. // create new server
  1004. _bstr_t t_bstrKeyPath = t_bstrServerPath;
  1005. t_hKey = t_metabase.CreateKey(t_bstrKeyPath);
  1006. t_vt1 = L"IIsFtpServer";
  1007. t_metabase.PutString(t_hKey, &METABASE_PROPERTY_DATA::s_KeyType, t_vt1, NULL);
  1008. t_metabase.PutString(t_hKey, &METABASE_PROPERTY_DATA::s_ServerComment, t_vt2, NULL);
  1009. t_metabase.PutMultiSz(t_hKey, &METABASE_PROPERTY_DATA::s_ServerBindings, t_vt3, NULL);
  1010. t_metabase.CloseKey(t_hKey);
  1011. // create root of virtualdir
  1012. t_bstrKeyPath += L"/";
  1013. t_bstrKeyPath += L"ROOT";
  1014. t_hKey = t_metabase.CreateKey(t_bstrKeyPath);
  1015. t_vt1 = L"IIsFtpVirtualDir";
  1016. t_metabase.PutString(t_hKey, &METABASE_PROPERTY_DATA::s_KeyType, t_vt1, NULL);
  1017. t_metabase.PutString(t_hKey, &METABASE_PROPERTY_DATA::s_Path, t_vt4, NULL);
  1018. t_metabase.CloseKey(t_hKey);
  1019. // out server name
  1020. IWbemClassObject* pClass = NULL;
  1021. IWbemClassObject* pMethodClass = NULL;
  1022. IWbemClassObject* pOutParams = NULL;
  1023. hr = a_pNameSpace->GetObject(_bstr_t(a_szClassName), 0, a_pCtx, &pClass, NULL);
  1024. THROW_ON_ERROR(hr);
  1025. // This method returns values, and so create an instance of the
  1026. // output argument class.
  1027. hr = pClass->GetMethod(a_szMethodName, 0, NULL, &pMethodClass);
  1028. pClass->Release();
  1029. THROW_ON_ERROR(hr);
  1030. hr = pMethodClass->SpawnInstance(0, &pOutParams);
  1031. pMethodClass->Release();
  1032. THROW_ON_ERROR(hr);
  1033. // find root key of server and make server name
  1034. WCHAR szServerName[METADATA_MAX_NAME_LEN];
  1035. lstrcpy(szServerName, t_bstrServerPath);
  1036. WMI_CLASS* t_pWMIClass = NULL;
  1037. _bstr_t t_bstrRootKey = L"/LM";
  1038. _bstr_t t_bstrServerName = L"IIs_FtpServer.Name = \"";
  1039. if(GetClass(L"IIs_FtpServer",&t_pWMIClass))
  1040. {
  1041. t_bstrRootKey = t_pWMIClass->pszMetabaseKey;
  1042. t_bstrServerName = t_pWMIClass->pszClassName;
  1043. t_bstrServerName += L".";
  1044. t_bstrServerName += t_pWMIClass->pszKeyName;
  1045. t_bstrServerName += L" = \"";
  1046. }
  1047. t_bstrServerName += szServerName + t_bstrRootKey.length() + 1; // remove root key from server path
  1048. t_bstrServerName += L"\"";
  1049. // put it into the output object
  1050. // out "Server name"
  1051. t_vt1 = t_bstrServerName;
  1052. hr = pOutParams->Put(L"ReturnValue", 0, &t_vt1, 0);
  1053. THROW_ON_ERROR(hr);
  1054. // Send the output object back to the client via the sink. Then
  1055. // release the pointers and free the strings.
  1056. hr = a_pHandler->Indicate(1, &pOutParams);
  1057. pOutParams->Release();
  1058. }
  1059. catch(...)
  1060. {
  1061. hr = WBEM_E_FAILED;
  1062. }
  1063. // synchronize: release
  1064. s_synObject.Leave();
  1065. }
  1066. else
  1067. hr = WBEM_E_NOT_SUPPORTED;
  1068. THROW_ON_ERROR(hr);
  1069. }
  1070. void CUtils::ExecWebServiceMethod(
  1071. LPCWSTR a_szMbPath,
  1072. LPCWSTR a_szClassName,
  1073. LPCWSTR a_szMethodName,
  1074. IWbemContext* a_pCtx,
  1075. IWbemClassObject* a_pInParams,
  1076. IWbemObjectSink* a_pHandler,
  1077. CWbemServices* a_pNameSpace
  1078. )
  1079. {
  1080. HRESULT hr = ERROR_SUCCESS;
  1081. _variant_t t_vt1, t_vt2, t_vt3, t_vt4;
  1082. CMetabase t_metabase;
  1083. METADATA_HANDLE t_hKey;
  1084. if(!lstrcmpiW(a_szMethodName, L"CreateNewServer"))
  1085. {
  1086. if(a_pInParams == NULL)
  1087. throw WBEM_E_INVALID_PARAMETER;
  1088. // synchronize
  1089. s_synObject.Enter();
  1090. try
  1091. {
  1092. // get in params
  1093. a_pInParams->Get(L"ServerComment", 0, &t_vt2, NULL, NULL);
  1094. a_pInParams->Get(L"ServerBindings", 0, &t_vt3, NULL, NULL);
  1095. a_pInParams->Get(L"PathOfRootVitualDir", 0, &t_vt4, NULL, NULL);
  1096. _bstr_t t_bstrServicePath = a_szMbPath;
  1097. _bstr_t t_bstrServerPath = t_bstrServicePath;
  1098. // check the optional [in] parameter "ServerNumber"
  1099. a_pInParams->Get(L"ServerNumber", 0, &t_vt1, NULL, NULL);
  1100. if( t_vt1.vt == VT_BSTR )
  1101. {
  1102. t_bstrServerPath += L"/";
  1103. t_bstrServerPath += t_vt1.bstrVal;
  1104. // check if the server path is not existed
  1105. if(t_metabase.CheckKey(t_bstrServerPath))
  1106. throw WBEM_E_INVALID_PARAMETER;
  1107. }
  1108. else // if no server is specified
  1109. {
  1110. // find an unique server name(number) and create it
  1111. FindUniqueServerName(t_bstrServicePath, t_bstrServerPath);
  1112. }
  1113. // create new server
  1114. _bstr_t t_bstrKeyPath = t_bstrServerPath;
  1115. t_hKey = t_metabase.CreateKey(t_bstrKeyPath);
  1116. t_vt1 = L"IIsWebServer";
  1117. t_metabase.PutString(t_hKey, &METABASE_PROPERTY_DATA::s_KeyType, t_vt1, NULL);
  1118. t_metabase.PutString(t_hKey, &METABASE_PROPERTY_DATA::s_ServerComment, t_vt2, NULL);
  1119. t_metabase.PutMultiSz(t_hKey, &METABASE_PROPERTY_DATA::s_ServerBindings, t_vt3, NULL);
  1120. t_metabase.CloseKey(t_hKey);
  1121. // create root of virtualdir
  1122. t_bstrKeyPath += L"/";
  1123. t_bstrKeyPath += L"ROOT";
  1124. t_hKey = t_metabase.CreateKey(t_bstrKeyPath);
  1125. t_vt1 = L"IIsWebVirtualDir";
  1126. t_metabase.PutString(t_hKey, &METABASE_PROPERTY_DATA::s_KeyType, t_vt1, NULL);
  1127. t_metabase.PutString(t_hKey, &METABASE_PROPERTY_DATA::s_Path, t_vt4, NULL);
  1128. t_vt1 = t_bstrKeyPath;
  1129. t_metabase.PutString(t_hKey, &METABASE_PROPERTY_DATA::s_AppRoot, t_vt1, NULL);
  1130. t_metabase.CloseKey(t_hKey);
  1131. // out server name
  1132. IWbemClassObject* pClass = NULL;
  1133. IWbemClassObject* pMethodClass = NULL;
  1134. IWbemClassObject* pOutParams = NULL;
  1135. hr = a_pNameSpace->GetObject(_bstr_t(a_szClassName), 0, a_pCtx, &pClass, NULL);
  1136. THROW_ON_ERROR(hr);
  1137. // This method returns values, and so create an instance of the
  1138. // output argument class.
  1139. hr = pClass->GetMethod(a_szMethodName, 0, NULL, &pMethodClass);
  1140. pClass->Release();
  1141. THROW_ON_ERROR(hr);
  1142. hr = pMethodClass->SpawnInstance(0, &pOutParams);
  1143. pMethodClass->Release();
  1144. THROW_ON_ERROR(hr);
  1145. // find root key of server and make server name
  1146. WCHAR szServerName[METADATA_MAX_NAME_LEN];
  1147. lstrcpy(szServerName, t_bstrServerPath);
  1148. WMI_CLASS* t_pWMIClass = NULL;
  1149. _bstr_t t_bstrRootKey = L"/LM";
  1150. _bstr_t t_bstrServerName = L"IIs_WebServer.Name = \"";
  1151. if(GetClass(L"IIs_WebServer",&t_pWMIClass))
  1152. {
  1153. t_bstrRootKey = t_pWMIClass->pszMetabaseKey;
  1154. t_bstrServerName = t_pWMIClass->pszClassName;
  1155. t_bstrServerName += L".";
  1156. t_bstrServerName += t_pWMIClass->pszKeyName;
  1157. t_bstrServerName += L" = \"";
  1158. }
  1159. t_bstrServerName += szServerName + t_bstrRootKey.length() + 1; // remove root key from server path
  1160. t_bstrServerName += L"\"";
  1161. // put it into the output object
  1162. // out "Server name"
  1163. t_vt1 = t_bstrServerName;
  1164. hr = pOutParams->Put(L"ReturnValue", 0, &t_vt1, 0);
  1165. THROW_ON_ERROR(hr);
  1166. // Send the output object back to the client via the sink. Then
  1167. // release the pointers and free the strings.
  1168. hr = a_pHandler->Indicate(1, &pOutParams);
  1169. pOutParams->Release();
  1170. }
  1171. catch(...)
  1172. {
  1173. hr = WBEM_E_FAILED;
  1174. }
  1175. // synchronize: release
  1176. s_synObject.Leave();
  1177. }
  1178. else
  1179. hr = WBEM_E_NOT_SUPPORTED;
  1180. THROW_ON_ERROR(hr);
  1181. }
  1182. void CUtils::ExecComputerMethod(
  1183. LPCWSTR a_szMbPath,
  1184. LPCWSTR a_szClassName,
  1185. LPCWSTR a_szMethodName,
  1186. IWbemContext* a_pCtx,
  1187. IWbemClassObject* a_pInParams,
  1188. IWbemObjectSink* a_pHandler,
  1189. CWbemServices* a_pNameSpace
  1190. )
  1191. {
  1192. HRESULT hr;
  1193. _variant_t t_vt1, t_vt2, t_vt3, t_vt4;
  1194. CMetabase obj;
  1195. if(!lstrcmpiW(a_szMethodName, L"EnumBackups"))
  1196. {
  1197. if(a_pInParams == NULL)
  1198. throw WBEM_E_INVALID_PARAMETER;
  1199. // get in params
  1200. a_pInParams->Get(L"BackupLocation", 0, &t_vt1, NULL, NULL);
  1201. a_pInParams->Get(L"IndexIn", 0, &t_vt2, NULL, NULL);
  1202. // make in/out params
  1203. WCHAR BackupLocation[MD_BACKUP_MAX_LEN];
  1204. lstrcpyW(BackupLocation, _bstr_t(t_vt1));
  1205. // define out params
  1206. DWORD BackupVersionOut;
  1207. FILETIME BackupDateTimeOut;
  1208. // call method - EnumBackups.
  1209. hr = obj.EnumBackups(BackupLocation, &BackupVersionOut, &BackupDateTimeOut, t_vt2.lVal);
  1210. THROW_ON_ERROR(hr);
  1211. IWbemClassObject* pClass = NULL;
  1212. IWbemClassObject* pMethodClass = NULL;
  1213. IWbemClassObject* pOutParams = NULL;
  1214. hr = a_pNameSpace->GetObject(_bstr_t(a_szClassName), 0, a_pCtx, &pClass, NULL);
  1215. THROW_ON_ERROR(hr);
  1216. // This method returns values, and so create an instance of the
  1217. // output argument class.
  1218. hr = pClass->GetMethod(a_szMethodName, 0, NULL, &pMethodClass);
  1219. pClass->Release();
  1220. THROW_ON_ERROR(hr);
  1221. hr = pMethodClass->SpawnInstance(0, &pOutParams);
  1222. pMethodClass->Release();
  1223. THROW_ON_ERROR(hr);
  1224. // put it into the output object
  1225. // out BackupLocation
  1226. t_vt1 = BackupLocation;
  1227. hr = pOutParams->Put(L"BackupLocation", 0, &t_vt1, 0);
  1228. THROW_ON_ERROR(hr);
  1229. // out BackupVersionOut
  1230. t_vt1.vt = VT_I4;
  1231. t_vt1.lVal = BackupVersionOut;
  1232. hr = pOutParams->Put(L"BackupVersionOut", 0, &t_vt1, 0);
  1233. THROW_ON_ERROR(hr);
  1234. // out BackupDateTimeOut (UTC time)
  1235. SYSTEMTIME systime;
  1236. FileTimeToSystemTime(&BackupDateTimeOut, &systime);
  1237. WCHAR datetime[30];
  1238. swprintf(
  1239. datetime,
  1240. L"%04d%02d%02d%02d%02d%02d.%06d+000",
  1241. systime.wYear,
  1242. systime.wMonth,
  1243. systime.wDay,
  1244. systime.wHour,
  1245. systime.wMinute,
  1246. systime.wSecond,
  1247. systime.wMilliseconds
  1248. );
  1249. t_vt1 = datetime;
  1250. hr = pOutParams->Put(L"BackupDateTimeOut", 0, &t_vt1, 0);
  1251. THROW_ON_ERROR(hr);
  1252. // Send the output object back to the client via the sink. Then
  1253. // release the pointers and free the strings.
  1254. hr = a_pHandler->Indicate(1, &pOutParams);
  1255. pOutParams->Release();
  1256. }
  1257. else if(!lstrcmpiW(a_szMethodName, L"Backup"))
  1258. {
  1259. if(a_pInParams == NULL)
  1260. throw WBEM_E_INVALID_PARAMETER;
  1261. a_pInParams->Get(L"BackupLocation", 0, &t_vt1, NULL, NULL);
  1262. a_pInParams->Get(L"BackupVersion", 0, &t_vt2, NULL, NULL);
  1263. a_pInParams->Get(L"BackupFlags", 0, &t_vt3, NULL, NULL);
  1264. hr = obj.Backup(_bstr_t(t_vt1), t_vt2.lVal, t_vt3.lVal);
  1265. }
  1266. else if(!lstrcmpiW(a_szMethodName, L"DeleteBackup"))
  1267. {
  1268. if(a_pInParams == NULL)
  1269. throw WBEM_E_INVALID_PARAMETER;
  1270. a_pInParams->Get(L"BackupLocation", 0, &t_vt1, NULL, NULL);
  1271. a_pInParams->Get(L"BackupVersion", 0, &t_vt2, NULL, NULL);
  1272. hr = obj.DeleteBackup(_bstr_t(t_vt1), t_vt2.lVal);
  1273. }
  1274. else if(!lstrcmpiW(a_szMethodName, L"Restore"))
  1275. {
  1276. if(a_pInParams == NULL)
  1277. throw WBEM_E_INVALID_PARAMETER;
  1278. a_pInParams->Get(L"BackupLocation", 0, &t_vt1, NULL, NULL);
  1279. a_pInParams->Get(L"BackupVersion", 0, &t_vt2, NULL, NULL);
  1280. a_pInParams->Get(L"BackupFlags", 0, &t_vt3, NULL, NULL);
  1281. hr = obj.Restore(_bstr_t(t_vt1), t_vt2.lVal, t_vt3.lVal);
  1282. }
  1283. else
  1284. hr = WBEM_E_NOT_SUPPORTED;
  1285. THROW_ON_ERROR(hr);
  1286. }
  1287. void CUtils::ExecCertMapperMethod(
  1288. LPCWSTR a_szMbPath,
  1289. LPCWSTR a_szClassName,
  1290. LPCWSTR a_szMethodName,
  1291. IWbemContext* a_pCtx,
  1292. IWbemClassObject* a_pInParams,
  1293. IWbemObjectSink* a_pHandler,
  1294. CWbemServices* a_pNameSpace
  1295. )
  1296. {
  1297. HRESULT hr;
  1298. _variant_t t_vt1, t_vt2, t_vt3=L"1", t_vt4=L"1", t_vt5=L"1", t_vt6=L"1", t_vt7=L"1";
  1299. CCertMapperMethod obj(a_szMbPath);
  1300. if(!lstrcmpiW(a_szMethodName, L"CreateMapping"))
  1301. {
  1302. if(a_pInParams == NULL)
  1303. throw WBEM_E_INVALID_PARAMETER;
  1304. a_pInParams->Get(L"vCert", 0, &t_vt1, NULL, NULL);
  1305. a_pInParams->Get(L"NtAcct", 0, &t_vt2, NULL, NULL);
  1306. a_pInParams->Get(L"NtPwd", 0, &t_vt3, NULL, NULL);
  1307. a_pInParams->Get(L"strName", 0, &t_vt4, NULL, NULL);
  1308. a_pInParams->Get(L"IEnabled", 0, &t_vt5, NULL, NULL);
  1309. // call method - CreateMapping.
  1310. hr = obj.CreateMapping(t_vt1, t_vt2.bstrVal, t_vt3.bstrVal, t_vt4.bstrVal, t_vt5);
  1311. }
  1312. else if(!lstrcmpiW(a_szMethodName, L"DeleteMapping"))
  1313. {
  1314. if(a_pInParams == NULL)
  1315. throw WBEM_E_INVALID_PARAMETER;
  1316. a_pInParams->Get(L"IMethod", 0, &t_vt1, NULL, NULL);
  1317. a_pInParams->Get(L"vKey", 0, &t_vt2, NULL, NULL);
  1318. // call method - DeleteMapping.
  1319. hr = obj.DeleteMapping(t_vt1, t_vt2);
  1320. }
  1321. else if(!lstrcmpiW(a_szMethodName, L"GetMapping"))
  1322. {
  1323. if(a_pInParams == NULL)
  1324. throw WBEM_E_INVALID_PARAMETER;
  1325. // get in params
  1326. a_pInParams->Get(L"IMethod", 0, &t_vt1, NULL, NULL);
  1327. a_pInParams->Get(L"vKey", 0, &t_vt2, NULL, NULL);
  1328. // call method - GetMapping.
  1329. hr = obj.GetMapping(
  1330. t_vt1,
  1331. t_vt2,
  1332. &t_vt3,
  1333. &t_vt4,
  1334. &t_vt5,
  1335. &t_vt6,
  1336. &t_vt7
  1337. );
  1338. THROW_ON_ERROR(hr);
  1339. IWbemClassObject* pClass = NULL;
  1340. IWbemClassObject* pMethodClass = NULL;
  1341. IWbemClassObject* pOutParams = NULL;
  1342. hr = a_pNameSpace->GetObject(_bstr_t(a_szClassName), 0, a_pCtx, &pClass, NULL);
  1343. THROW_ON_ERROR(hr);
  1344. // This method returns values, and so create an instance of the
  1345. // output argument class.
  1346. hr = pClass->GetMethod(a_szMethodName, 0, NULL, &pMethodClass);
  1347. pClass->Release();
  1348. THROW_ON_ERROR(hr);
  1349. hr = pMethodClass->SpawnInstance(0, &pOutParams);
  1350. pMethodClass->Release();
  1351. THROW_ON_ERROR(hr);
  1352. // put them into the output object
  1353. hr = pOutParams->Put(L"vCert", 0, &t_vt3, 0);
  1354. THROW_ON_ERROR(hr);
  1355. hr = pOutParams->Put(L"NtAcct", 0, &t_vt4, 0);
  1356. THROW_ON_ERROR(hr);
  1357. hr = pOutParams->Put(L"NtPwd", 0, &t_vt5, 0);
  1358. THROW_ON_ERROR(hr);
  1359. hr = pOutParams->Put(L"strName", 0, &t_vt6, 0);
  1360. THROW_ON_ERROR(hr);
  1361. hr = pOutParams->Put(L"IEnabled", 0, &t_vt7, 0);
  1362. THROW_ON_ERROR(hr);
  1363. // Send the output object back to the client via the sink. Then
  1364. // release the pointers and free the strings.
  1365. hr = a_pHandler->Indicate(1, &pOutParams);
  1366. pOutParams->Release();
  1367. }
  1368. else if(!lstrcmpiW(a_szMethodName, L"SetAcct"))
  1369. {
  1370. if(a_pInParams == NULL)
  1371. throw WBEM_E_INVALID_PARAMETER;
  1372. a_pInParams->Get(L"IMethod", 0, &t_vt1, NULL, NULL);
  1373. a_pInParams->Get(L"vKey", 0, &t_vt2, NULL, NULL);
  1374. a_pInParams->Get(L"NtAcct", 0, &t_vt3, NULL, NULL);
  1375. // call method - SetAcct.
  1376. hr = obj.SetAcct(t_vt1, t_vt2, t_vt3.bstrVal);
  1377. }
  1378. else if(!lstrcmpiW(a_szMethodName, L"SetEnabled"))
  1379. {
  1380. if(a_pInParams == NULL)
  1381. throw WBEM_E_INVALID_PARAMETER;
  1382. a_pInParams->Get(L"IMethod", 0, &t_vt1, NULL, NULL);
  1383. a_pInParams->Get(L"vKey", 0, &t_vt2, NULL, NULL);
  1384. a_pInParams->Get(L"IEnabled", 0, &t_vt3, NULL, NULL);
  1385. // call method - SetEnabled.
  1386. hr = obj.SetEnabled(t_vt1, t_vt2, t_vt3);
  1387. }
  1388. else if(!lstrcmpiW(a_szMethodName, L"SetName"))
  1389. {
  1390. if(a_pInParams == NULL)
  1391. throw WBEM_E_INVALID_PARAMETER;
  1392. a_pInParams->Get(L"IMethod", 0, &t_vt1, NULL, NULL);
  1393. a_pInParams->Get(L"vKey", 0, &t_vt2, NULL, NULL);
  1394. a_pInParams->Get(L"strName", 0, &t_vt3, NULL, NULL);
  1395. // call method - SetName.
  1396. hr = obj.SetName(t_vt1, t_vt2, t_vt3.bstrVal);
  1397. }
  1398. else if(!lstrcmpiW(a_szMethodName, L"SetPwd"))
  1399. {
  1400. if(a_pInParams == NULL)
  1401. throw WBEM_E_INVALID_PARAMETER;
  1402. a_pInParams->Get(L"IMethod", 0, &t_vt1, NULL, NULL);
  1403. a_pInParams->Get(L"vKey", 0, &t_vt2, NULL, NULL);
  1404. a_pInParams->Get(L"NtPwd", 0, &t_vt3, NULL, NULL);
  1405. // call method - SetPwd.
  1406. hr = obj.SetPwd(t_vt1, t_vt2, t_vt3.bstrVal);
  1407. }
  1408. else
  1409. hr = WBEM_E_NOT_SUPPORTED;
  1410. THROW_ON_ERROR(hr);
  1411. }
  1412. void CUtils::FindUniqueServerName(
  1413. LPCWSTR a_szMbPath,
  1414. _bstr_t& a_bstrServerPath
  1415. )
  1416. {
  1417. CMetabase t_metabase;
  1418. WCHAR t_szServerNumber[15];
  1419. _bstr_t t_bstrKeyPath;
  1420. DWORD dwServerNumber = 0;
  1421. do
  1422. {
  1423. dwServerNumber++;
  1424. _ltow(dwServerNumber, t_szServerNumber, 10);
  1425. // create server key
  1426. t_bstrKeyPath = a_szMbPath;
  1427. t_bstrKeyPath += L"/";
  1428. t_bstrKeyPath += t_szServerNumber;
  1429. // check if the server is not existed
  1430. if(!t_metabase.CheckKey(t_bstrKeyPath))
  1431. break;
  1432. }while( 1 );
  1433. a_bstrServerPath = t_bstrKeyPath;
  1434. }
  1435. void CUtils::Throw_Exception(
  1436. HRESULT a_hr,
  1437. METABASE_PROPERTY* a_pmbp
  1438. )
  1439. {
  1440. CIIsProvException t_e;
  1441. t_e.m_hr = a_hr;
  1442. t_e.m_psz = a_pmbp->pszPropName;
  1443. throw(t_e);
  1444. }