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.

918 lines
23 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <wbemcomn.h>
  4. #include <wbemtime.h>
  5. #include <activeds.h>
  6. #include <ArrTempl.h>
  7. #include <comutil.h>
  8. #undef _ASSERT
  9. #include <atlbase.h>
  10. #include <activeds.h>
  11. #include <string.h>
  12. #include "Utility.h"
  13. /*********************************************************************
  14. ************** Active Directory Methods ******************************
  15. *********************************************************************/
  16. #define MAX_ATTR_SOM 20
  17. #define MAX_ATTR_RULE 5
  18. #define DELIMITER L';'
  19. #define DELIMITER_STR L";"
  20. HRESULT Som_CIMToAD(IWbemClassObject *pSrcPolicyObj, IDirectoryObject *pDestContainer, long lFlags)
  21. {
  22. HRESULT
  23. hres = WBEM_S_NO_ERROR;
  24. CComVariant
  25. v[MAX_ATTR_SOM];
  26. long
  27. nArgs_SOM = 0,
  28. c1;
  29. CComPtr<IDispatch>
  30. pDisp;
  31. CComPtr<IDirectoryObject>
  32. pDestSomObj;
  33. ADsObjAutoDelete
  34. AutoDelete;
  35. CComQIPtr<IADsContainer, &IID_IADsContainer>
  36. pADsContainer = pDestContainer;
  37. SafeArray<IUnknown*, VT_UNKNOWN>
  38. Array1;
  39. BYTE defaultBuffer[2048];
  40. ULONG bWritten = 0;
  41. LARGE_INTEGER offset;
  42. ADSVALUE
  43. AdsValue[MAX_ATTR_SOM];
  44. ADS_ATTR_INFO
  45. attrInfo[MAX_ATTR_SOM];
  46. WBEMTime
  47. wtCurrentTime;
  48. SYSTEMTIME
  49. SystemTime;
  50. CComBSTR
  51. bstrID,
  52. bstrCurrentTimeDTMF,
  53. RulesBuffer,
  54. NULL_STRING(L"\0"),
  55. SomName(L"CN=");
  56. // **** get current time
  57. GetSystemTime(&SystemTime);
  58. wtCurrentTime = SystemTime;
  59. bstrCurrentTimeDTMF.Attach(wtCurrentTime.GetDMTF(FALSE));
  60. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  61. g_bstrADObjectClass,
  62. ADS_ATTR_UPDATE,
  63. ADSTYPE_CASE_IGNORE_STRING,
  64. &AdsValue[nArgs_SOM],
  65. 1);
  66. AdsValue[nArgs_SOM].CaseIgnoreString = g_bstrADClassSom;
  67. int lTypeIndex = nArgs_SOM++;
  68. // **** ID
  69. hres = pSrcPolicyObj->Get(g_bstrID, 0, &v[nArgs_SOM], NULL, NULL);
  70. if(FAILED(hres)) return hres;
  71. if ((v[nArgs_SOM].vt == VT_BSTR) && (v[nArgs_SOM].bstrVal != NULL))
  72. {
  73. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  74. g_bstrADID,
  75. ADS_ATTR_UPDATE,
  76. ADSTYPE_CASE_IGNORE_STRING,
  77. &AdsValue[nArgs_SOM],
  78. 1);
  79. AdsValue[nArgs_SOM].CaseIgnoreString = V_BSTR(&v[nArgs_SOM]);
  80. }
  81. else
  82. return WBEM_E_ILLEGAL_NULL;
  83. int lIDIndex = nArgs_SOM++;
  84. SomName.Append(AdsValue[lIDIndex].CaseIgnoreString);
  85. // **** security descriptor
  86. // PSECURITY_DESCRIPTOR
  87. // pSD = GetADSecurityDescriptor(pDestContainer);
  88. // CNtSecurityDescriptor
  89. // cSD(pSD, TRUE);
  90. // if(NULL == pSD)
  91. // {
  92. // ERRORTRACE((LOG_ESS, "POLICMAN: could not create security descriptor for som filter object\n"));
  93. // return WBEM_E_OUT_OF_MEMORY;
  94. // }
  95. ADsStruct<ADS_ATTR_INFO>
  96. SecDescValue;
  97. CNtSecurityDescriptor cSD;
  98. // flag to indicate whether we're updating an existing object as opposed to creating a new one.
  99. bool bEditExisting = false;
  100. pDisp.Release();
  101. if(SUCCEEDED(hres = pADsContainer->GetObject(g_bstrADClassSom, SomName, &pDisp)))
  102. {
  103. bEditExisting = true;
  104. if(lFlags & WBEM_FLAG_CREATE_ONLY) return WBEM_E_ALREADY_EXISTS;
  105. // HACK HACK HACK!
  106. // okay, at this point we know that we're editing an existing object.
  107. // therefor, we do *not* want to set the id or type
  108. // back up the array pointer - all the ATL classes should clean up after themselves with no problem.
  109. nArgs_SOM = 0;
  110. // we'll simply leave the existing security descriptor in place
  111. /*************************************************
  112. CComQIPtr<IDirectoryObject, &IID_IDirectoryObject>
  113. pDirObj = pDisp;
  114. PADS_ATTR_INFO pAttrInfo = NULL;
  115. pDisp = NULL;
  116. LPWSTR pAttrName = L"ntSecurityDescriptor";
  117. DWORD dwReturn;
  118. hres = pDirObj->GetObjectAttributes(&pAttrName, 1, &pAttrInfo, &dwReturn);
  119. if((dwReturn != 1) || (pAttrInfo->dwADsType != ADSTYPE_NT_SECURITY_DESCRIPTOR)) return WBEM_E_FAILED;
  120. SecDescValue = pAttrInfo;
  121. if(FAILED(hres)) return hres;
  122. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  123. L"ntSecurityDescriptor",
  124. ADS_ATTR_UPDATE,
  125. ADSTYPE_NT_SECURITY_DESCRIPTOR,
  126. pAttrInfo->pADsValues,
  127. 1);
  128. ***************************************************/
  129. }
  130. else
  131. {
  132. if(WBEM_FLAG_UPDATE_ONLY & lFlags)
  133. return WBEM_E_NOT_FOUND;
  134. bEditExisting = false;
  135. hres = GetOwnerSecurityDescriptor(cSD);
  136. if (FAILED(hres)) return hres;
  137. if(CNtSecurityDescriptor::NoError == cSD.GetStatus())
  138. {
  139. AdsValue[nArgs_SOM].SecurityDescriptor.dwLength = cSD.GetSize();
  140. AdsValue[nArgs_SOM].SecurityDescriptor.lpValue = (LPBYTE) cSD.GetPtr();
  141. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  142. L"ntSecurityDescriptor",
  143. ADS_ATTR_UPDATE,
  144. ADSTYPE_NT_SECURITY_DESCRIPTOR,
  145. &AdsValue[nArgs_SOM],
  146. 1);
  147. }
  148. else
  149. return WBEM_E_FAILED;
  150. nArgs_SOM++;
  151. }
  152. pDisp.Release();
  153. // **** Name
  154. hres = pSrcPolicyObj->Get(g_bstrName, 0, &v[nArgs_SOM], NULL, NULL);
  155. if(FAILED(hres)) return hres;
  156. if ((v[nArgs_SOM].vt == VT_BSTR) && (v[nArgs_SOM].bstrVal != NULL))
  157. {
  158. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  159. g_bstrADName,
  160. ADS_ATTR_UPDATE,
  161. ADSTYPE_CASE_IGNORE_STRING,
  162. &AdsValue[nArgs_SOM],
  163. 1);
  164. AdsValue[nArgs_SOM].CaseIgnoreString = V_BSTR(&v[nArgs_SOM]);
  165. nArgs_SOM++;
  166. }
  167. else
  168. return WBEM_E_ILLEGAL_NULL;
  169. // **** Description
  170. hres = pSrcPolicyObj->Get(g_bstrDescription, 0, &v[nArgs_SOM], NULL, NULL);
  171. if(FAILED(hres)) return hres;
  172. if ((v[nArgs_SOM].vt == VT_BSTR) && (v[nArgs_SOM].bstrVal != NULL))
  173. {
  174. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  175. g_bstrADDescription,
  176. ADS_ATTR_UPDATE,
  177. ADSTYPE_CASE_IGNORE_STRING,
  178. &AdsValue[nArgs_SOM],
  179. 1);
  180. AdsValue[nArgs_SOM].CaseIgnoreString = V_BSTR(&v[nArgs_SOM]);
  181. nArgs_SOM++;
  182. }
  183. else if (bEditExisting)
  184. {
  185. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  186. g_bstrADDescription,
  187. ADS_ATTR_CLEAR,
  188. ADSTYPE_CASE_IGNORE_STRING,
  189. &AdsValue[nArgs_SOM],
  190. 1);
  191. AdsValue[nArgs_SOM].CaseIgnoreString = NULL_STRING;
  192. nArgs_SOM++;
  193. }
  194. // **** SourceOrganization
  195. hres = pSrcPolicyObj->Get(g_bstrSourceOrganization, 0, &v[nArgs_SOM], NULL, NULL);
  196. if(FAILED(hres)) return hres;
  197. if ((v[nArgs_SOM].vt == VT_BSTR) && (v[nArgs_SOM].bstrVal != NULL))
  198. {
  199. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  200. g_bstrADSourceOrganization,
  201. ADS_ATTR_UPDATE,
  202. ADSTYPE_CASE_IGNORE_STRING,
  203. &AdsValue[nArgs_SOM],
  204. 1);
  205. AdsValue[nArgs_SOM].CaseIgnoreString = V_BSTR(&v[nArgs_SOM]);
  206. nArgs_SOM++;
  207. }
  208. else if (bEditExisting)
  209. {
  210. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  211. g_bstrADSourceOrganization,
  212. ADS_ATTR_CLEAR,
  213. ADSTYPE_CASE_IGNORE_STRING,
  214. &AdsValue[nArgs_SOM],
  215. 1);
  216. AdsValue[nArgs_SOM].CaseIgnoreString = NULL_STRING;
  217. nArgs_SOM++;
  218. }
  219. // **** Author
  220. hres = pSrcPolicyObj->Get(g_bstrAuthor, 0, &v[nArgs_SOM], NULL, NULL);
  221. if(FAILED(hres)) return hres;
  222. if ((v[nArgs_SOM].vt == VT_BSTR) && (v[nArgs_SOM].bstrVal != NULL))
  223. {
  224. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  225. g_bstrADAuthor,
  226. ADS_ATTR_UPDATE,
  227. ADSTYPE_CASE_IGNORE_STRING,
  228. &AdsValue[nArgs_SOM],
  229. 1);
  230. AdsValue[nArgs_SOM].CaseIgnoreString = V_BSTR(&v[nArgs_SOM]);
  231. nArgs_SOM++;
  232. }
  233. else if (bEditExisting)
  234. {
  235. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  236. g_bstrADAuthor,
  237. ADS_ATTR_CLEAR,
  238. ADSTYPE_CASE_IGNORE_STRING,
  239. &AdsValue[nArgs_SOM],
  240. 1);
  241. AdsValue[nArgs_SOM].CaseIgnoreString = NULL_STRING;
  242. nArgs_SOM++;
  243. }
  244. // **** ChangeDate
  245. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  246. g_bstrADChangeDate,
  247. ADS_ATTR_UPDATE,
  248. ADSTYPE_CASE_IGNORE_STRING,
  249. &AdsValue[nArgs_SOM],
  250. 1);
  251. AdsValue[nArgs_SOM].CaseIgnoreString = bstrCurrentTimeDTMF;
  252. nArgs_SOM++;
  253. // **** CreationDate
  254. // **** if object already exists, leave it be
  255. // CComVariant
  256. // vCreationDate;
  257. BSTR creationDate = NULL;
  258. if (!bEditExisting)
  259. {
  260. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  261. g_bstrADCreationDate,
  262. ADS_ATTR_UPDATE,
  263. ADSTYPE_CASE_IGNORE_STRING,
  264. &AdsValue[nArgs_SOM],
  265. 1);
  266. AdsValue[nArgs_SOM].CaseIgnoreString = bstrCurrentTimeDTMF;
  267. nArgs_SOM++;
  268. }
  269. /***********************************
  270. if(SUCCEEDED(hres = pADsContainer->GetObject(NULL, SomName, &pDisp)))
  271. {
  272. CComQIPtr<IADs, &IID_IADs>
  273. pADsLegacyObj = pDisp;
  274. hres = pADsLegacyObj->Get(g_bstrADCreationDate, &vCreationDate);
  275. if (SUCCEEDED(hres))
  276. creationDate = vCreationDate.bstrVal;
  277. else if (hres == E_ADS_PROPERTY_NOT_FOUND)
  278. // support for legacy objects, might not have creation date filled
  279. creationDate = NULL;
  280. else return hres;
  281. }
  282. else
  283. creationDate = wtCurrentTime.GetDMTF(FALSE);
  284. if (creationDate != NULL)
  285. {
  286. AdsValue[nArgs_SOM].CaseIgnoreString = creationDate;
  287. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  288. g_bstrADCreationDate,
  289. ADS_ATTR_UPDATE,
  290. ADSTYPE_CASE_IGNORE_STRING,
  291. &AdsValue[nArgs_SOM],
  292. 1);
  293. nArgs_SOM++;
  294. }
  295. **********************************/
  296. // **** Rules
  297. hres = pSrcPolicyObj->Get(g_bstrRules, 0, &v[nArgs_SOM], NULL, NULL);
  298. if(FAILED(hres)) return hres;
  299. if(v[nArgs_SOM].vt != (VT_ARRAY | VT_UNKNOWN)) return WBEM_E_TYPE_MISMATCH;
  300. Array1 = &v[nArgs_SOM];
  301. wchar_t
  302. swArraySize[20];
  303. _itow(Array1.Size(), swArraySize, 10);
  304. RulesBuffer = swArraySize;
  305. RulesBuffer += DELIMITER_STR;
  306. for(c1 = 0; c1 < Array1.Size(); c1++)
  307. {
  308. CComVariant
  309. vLanguage,
  310. vNameSpace,
  311. vQuery;
  312. int
  313. languageLength,
  314. nameSpaceLength,
  315. queryLength;
  316. CComPtr<IWbemClassObject>
  317. pRuleObj;
  318. hres = Array1[c1]->QueryInterface(IID_IWbemClassObject, (void **)&pRuleObj);
  319. if(FAILED(hres)) return hres;
  320. if(pRuleObj == NULL) return WBEM_E_FAILED;
  321. // **** QueryLanguage
  322. hres = pRuleObj->Get(g_bstrQueryLanguage, 0, &vLanguage, NULL, NULL);
  323. if(FAILED(hres)) return hres;
  324. if((vLanguage.vt != VT_BSTR) || (vLanguage.bstrVal == NULL))
  325. return WBEM_E_ILLEGAL_NULL;
  326. languageLength = wcslen(vLanguage.bstrVal);
  327. _itow(languageLength, swArraySize, 10);
  328. RulesBuffer += swArraySize;
  329. RulesBuffer += DELIMITER_STR;
  330. // **** NameSpace
  331. hres = pRuleObj->Get(g_bstrTargetNameSpace, 0, &vNameSpace, NULL, NULL);
  332. if(FAILED(hres)) return hres;
  333. if((vNameSpace.vt != VT_BSTR) || (vNameSpace.bstrVal == NULL))
  334. return WBEM_E_ILLEGAL_NULL;
  335. nameSpaceLength = wcslen(vNameSpace.bstrVal);
  336. _itow(nameSpaceLength, swArraySize, 10);
  337. RulesBuffer += swArraySize;
  338. RulesBuffer += DELIMITER_STR;
  339. // **** Query
  340. hres = pRuleObj->Get(g_bstrQuery, 0, &vQuery, NULL, NULL);
  341. if(FAILED(hres)) return hres;
  342. if((vQuery.vt != VT_BSTR) || (vQuery.bstrVal == NULL))
  343. return WBEM_E_ILLEGAL_NULL;
  344. queryLength = wcslen(vQuery.bstrVal);
  345. _itow(queryLength, swArraySize, 10);
  346. RulesBuffer += swArraySize;
  347. RulesBuffer += DELIMITER_STR;
  348. // **** write the contents of the current rule
  349. RulesBuffer += vLanguage.bstrVal;
  350. RulesBuffer += DELIMITER_STR;
  351. RulesBuffer += vNameSpace.bstrVal;
  352. RulesBuffer += DELIMITER_STR;
  353. RulesBuffer += vQuery.bstrVal;
  354. RulesBuffer += DELIMITER_STR;
  355. }
  356. Init_AdsAttrInfo(&attrInfo[nArgs_SOM],
  357. g_bstrADParam2,
  358. ADS_ATTR_UPDATE,
  359. ADSTYPE_CASE_IGNORE_STRING,
  360. &AdsValue[nArgs_SOM],
  361. 1);
  362. AdsValue[nArgs_SOM].CaseIgnoreString = (BSTR)RulesBuffer;
  363. nArgs_SOM++;
  364. // **** create AD SOM object
  365. pDisp.Release();
  366. if (bEditExisting && SUCCEEDED(hres = pADsContainer->GetObject(g_bstrADClassSom, SomName, &pDisp)))
  367. {
  368. if(!pDisp) return WBEM_E_FAILED;
  369. CComQIPtr<IDirectoryObject>
  370. pDirObj = pDisp;
  371. DWORD dwAttrsModified;
  372. hres = pDirObj->SetObjectAttributes(attrInfo, nArgs_SOM, &dwAttrsModified);
  373. if(FAILED(hres))
  374. {
  375. ERRORTRACE((LOG_ESS, "POLICMAN: SetObjectAttributes failed: 0x%08X\n", hres));
  376. return hres;
  377. }
  378. }
  379. else
  380. {
  381. pDisp.Release(); hres = pDestContainer->CreateDSObject(SomName, attrInfo, nArgs_SOM, &pDisp);
  382. if(FAILED(hres) || (!pDisp))
  383. {
  384. ERRORTRACE((LOG_ESS, "POLICMAN: CreateDSObject failed: 0x%08X\n", hres));
  385. return hres;
  386. }
  387. }
  388. return WBEM_S_NO_ERROR;
  389. }
  390. struct ReleaseSearchHandle
  391. {
  392. ADS_SEARCH_HANDLE
  393. pHandle;
  394. CComPtr<IDirectorySearch>
  395. pDirSrch;
  396. ReleaseSearchHandle(ADS_SEARCH_HANDLE pIHandle, CComPtr<IDirectorySearch> &pIDirSrch)
  397. { pDirSrch = pIDirSrch; pHandle = pIHandle; }
  398. ~ReleaseSearchHandle(void)
  399. {if(pHandle) pDirSrch->CloseSearchHandle(pHandle); }
  400. };
  401. HRESULT Som_ADToCIM(IWbemClassObject **ppDestSomObj,
  402. IDirectoryObject *pSrcSomObj,
  403. IWbemServices *pDestCIM)
  404. {
  405. HRESULT
  406. hres = WBEM_S_NO_ERROR;
  407. CComVariant
  408. v1;
  409. wchar_t
  410. *AttrNames[] =
  411. {
  412. g_bstrADID,
  413. g_bstrADName,
  414. g_bstrADDescription,
  415. g_bstrADSourceOrganization,
  416. g_bstrADAuthor,
  417. g_bstrADChangeDate,
  418. g_bstrADCreationDate
  419. },
  420. *AttrNames2[] =
  421. {
  422. g_bstrADParam2
  423. };
  424. ADsStruct<ADS_ATTR_INFO>
  425. pAttrInfo,
  426. pAttrInfo2;
  427. unsigned long
  428. c1, c2, dwReturn, dwReturn2;
  429. CComPtr<IWbemClassObject>
  430. pClassDef,
  431. pClassDef_RULE,
  432. pDestSomObj;
  433. IWbemContext
  434. *pCtx = 0;
  435. ADsStruct<ADS_OBJECT_INFO>
  436. pInfo;
  437. // **** create empty som object
  438. hres = pDestCIM->GetObject(g_bstrClassSom, 0, pCtx, &pClassDef, NULL);
  439. if(FAILED(hres)) return hres;
  440. if(pClassDef == NULL) return WBEM_E_FAILED;
  441. hres = pClassDef->SpawnInstance(0, ppDestSomObj);
  442. if(FAILED(hres)) return hres;
  443. pDestSomObj = *ppDestSomObj;
  444. if(pDestSomObj == NULL) return WBEM_E_NOT_FOUND;
  445. // **** get object attributes
  446. hres = pSrcSomObj->GetObjectAttributes(AttrNames, 7, &pAttrInfo, &dwReturn);
  447. if(FAILED(hres)) return hres;
  448. if(pAttrInfo == NULL) return WBEM_E_NOT_FOUND;
  449. // **** get Param2 attribute
  450. hres = pSrcSomObj->GetObjectAttributes(AttrNames2, 1, &pAttrInfo2, &dwReturn2);
  451. if(FAILED(hres)) return hres;
  452. // **** Domain
  453. hres = pSrcSomObj->GetObjectInformation(&pInfo);
  454. if(SUCCEEDED(hres) && (pInfo != NULL))
  455. {
  456. QString
  457. LDAPName(pInfo->pszObjectDN),
  458. DomainName;
  459. hres = DomainNameFromDistName(DomainName, LDAPName);
  460. v1 = (wchar_t*)DomainName;
  461. hres = pDestSomObj->Put(g_bstrDomain, 0, &v1, 0);
  462. }
  463. if(pInfo == NULL) return WBEM_E_FAILED;
  464. if(FAILED(hres)) return hres;
  465. for(c1 = 0; c1 < dwReturn; c1++)
  466. {
  467. if((pAttrInfo + c1) == NULL)
  468. return WBEM_E_OUT_OF_MEMORY;
  469. BSTR
  470. bstrName = (pAttrInfo + c1)->pszAttrName,
  471. bstrValue = (pAttrInfo + c1)->pADsValues->CaseIgnoreString;
  472. if((NULL == bstrName) || (NULL == bstrValue))
  473. return WBEM_E_OUT_OF_MEMORY;
  474. v1 = bstrValue;
  475. // **** ID
  476. if(0 == _wcsicmp(bstrName, g_bstrADID))
  477. {
  478. hres = pDestSomObj->Put(g_bstrID, 0, &v1, 0);
  479. }
  480. // **** Name
  481. else if(0 == _wcsicmp(bstrName, g_bstrADName))
  482. {
  483. hres = pDestSomObj->Put(g_bstrName, 0, &v1, 0);
  484. }
  485. // **** Description
  486. else if(0 == _wcsicmp(bstrName, g_bstrADDescription))
  487. {
  488. hres = pDestSomObj->Put(g_bstrDescription, 0, &v1, 0);
  489. }
  490. // **** SourceOrganization
  491. else if(0 == _wcsicmp(bstrName, g_bstrADSourceOrganization))
  492. {
  493. hres = pDestSomObj->Put(g_bstrSourceOrganization, 0, &v1, 0);
  494. }
  495. // **** Author
  496. else if(0 == _wcsicmp(bstrName, g_bstrADAuthor))
  497. {
  498. hres = pDestSomObj->Put(g_bstrAuthor, 0, &v1, 0);
  499. }
  500. // **** ChangeDate
  501. else if(0 == _wcsicmp(bstrName, g_bstrADChangeDate))
  502. {
  503. hres = pDestSomObj->Put(g_bstrChangeDate, 0, &v1, 0);
  504. }
  505. // **** CreationDate
  506. else if(0 == _wcsicmp(bstrName, g_bstrADCreationDate))
  507. {
  508. hres = pDestSomObj->Put(g_bstrCreationDate, 0, &v1, 0);
  509. }
  510. if(FAILED(hres)) return hres;
  511. }
  512. // **** cache rule class definition
  513. hres = pDestCIM->GetObject(g_bstrClassRule, 0, pCtx, &pClassDef_RULE, NULL);
  514. if(FAILED(hres)) return hres;
  515. if(pClassDef_RULE == NULL) return hres;
  516. // **** now, get Rule objects that are children of this som object
  517. if(dwReturn2)
  518. {
  519. CComBSTR
  520. RulesBuffer = pAttrInfo2->pADsValues->CaseIgnoreString;
  521. wchar_t
  522. *pBeginChar = RulesBuffer,
  523. *pEndChar = RulesBuffer;
  524. if(NULL == pEndChar) return WBEM_S_NO_ERROR;
  525. // **** get number of rules
  526. pEndChar = wcschr(pEndChar, DELIMITER);
  527. if(NULL == pEndChar) return WBEM_S_NO_ERROR;
  528. *pEndChar = L'\0';
  529. int
  530. cElt = 0,
  531. nElts = _wtoi(pBeginChar);
  532. for(cElt = 0; (pEndChar) && (cElt < nElts); cElt++)
  533. {
  534. CComVariant
  535. vLanguage,
  536. vTargetNameSpace,
  537. vQuery,
  538. vRules1,
  539. vRules2;
  540. int
  541. numScanned,
  542. langLength,
  543. nameSpaceLength,
  544. queryLength;
  545. CComPtr<IWbemClassObject>
  546. pDestRuleObj;
  547. CComPtr<IUnknown>
  548. pUnknown;
  549. // **** get length of fields
  550. pBeginChar = pEndChar + 1;
  551. numScanned = swscanf(pBeginChar, L"%d;%d;%d;", &langLength, &nameSpaceLength, &queryLength);
  552. if(3 != numScanned) break;
  553. pEndChar = wcschr(pEndChar + 1, DELIMITER);
  554. pEndChar = wcschr(pEndChar + 1, DELIMITER);
  555. pEndChar = wcschr(pEndChar + 1, DELIMITER);
  556. // **** create new rule object
  557. hres = pClassDef_RULE->SpawnInstance(0, &pDestRuleObj);
  558. if(FAILED(hres)) return hres;
  559. if(pDestRuleObj == NULL) return WBEM_E_NOT_FOUND;
  560. // **** QueryLanguage
  561. pBeginChar = pEndChar + 1;
  562. pEndChar = pBeginChar + langLength;
  563. if(pEndChar)
  564. {
  565. *pEndChar = L'\0';
  566. vLanguage = pBeginChar;
  567. hres = pDestRuleObj->Put(g_bstrQueryLanguage, 0, &vLanguage, 0);
  568. if(FAILED(hres)) return hres;
  569. }
  570. else break;
  571. // **** NameSpace
  572. pBeginChar = pEndChar + 1;
  573. pEndChar = pBeginChar + nameSpaceLength;
  574. if(pEndChar)
  575. {
  576. *pEndChar = L'\0';
  577. vTargetNameSpace = pBeginChar;
  578. hres = pDestRuleObj->Put(g_bstrTargetNameSpace, 0, &vTargetNameSpace, 0);
  579. if(FAILED(hres)) return hres;
  580. }
  581. else break;
  582. // **** QueryLanguage
  583. pBeginChar = pEndChar + 1;
  584. pEndChar = pBeginChar + queryLength;
  585. if(pEndChar)
  586. {
  587. *pEndChar = L'\0';
  588. vQuery = pBeginChar;
  589. hres = pDestRuleObj->Put(g_bstrQuery, 0, &vQuery, 0);
  590. if(FAILED(hres)) return hres;
  591. }
  592. else break;
  593. // **** stuff new rule object into SOM object
  594. hres = pDestSomObj->Get(g_bstrRules, 0, &vRules1, NULL, NULL);
  595. if(FAILED(hres)) return hres;
  596. SafeArray<IUnknown*, VT_UNKNOWN>
  597. Rules(&vRules1);
  598. hres = pDestRuleObj->QueryInterface(IID_IUnknown, (void**)&pUnknown);
  599. if(FAILED(hres)) return hres;
  600. if(pUnknown == NULL) return WBEM_E_FAILED;
  601. Rules.ReDim(0, Rules.Size() + 1);
  602. Rules[Rules.IndexMax()] = pUnknown;
  603. Rules[Rules.IndexMax()]->AddRef();
  604. // **** place array in dest som object
  605. V_VT(&vRules2) = (VT_ARRAY | Rules.Type());
  606. V_ARRAY(&vRules2) = Rules.Data();
  607. hres = pDestSomObj->Put(g_bstrRules, 0, &vRules2, 0);
  608. if(FAILED(hres)) return hres;
  609. }
  610. }
  611. else
  612. {
  613. ADS_SEARCH_HANDLE
  614. SearchHandle;
  615. ADS_SEARCH_COLUMN
  616. SearchColumn;
  617. CComPtr<IDirectorySearch>
  618. pDirSrch;
  619. hres = pSrcSomObj->QueryInterface(IID_IDirectorySearch, (void **)&pDirSrch);
  620. if(FAILED(hres)) return hres;
  621. CComBSTR
  622. qsQuery(L"(objectClass=");
  623. qsQuery.Append(g_bstrADClassRule);
  624. qsQuery.Append(L")");
  625. hres = pDirSrch->ExecuteSearch(qsQuery, NULL, -1, &SearchHandle);
  626. if(FAILED(hres)) return hres;
  627. ReleaseSearchHandle
  628. HandleReleaseMe(SearchHandle, pDirSrch);
  629. while(S_OK == (hres = pDirSrch->GetNextRow(SearchHandle)))
  630. {
  631. CComVariant
  632. vLanguage,
  633. vNameSpace,
  634. vQuery,
  635. vRules1,
  636. vRules2;
  637. CComPtr<IUnknown>
  638. pUnknown;
  639. CComPtr<IWbemClassObject>
  640. pDestRuleObj;
  641. // **** create empty rule object
  642. hres = pClassDef_RULE->SpawnInstance(0, &pDestRuleObj);
  643. if(FAILED(hres)) return hres;
  644. if(pDestRuleObj == NULL) return WBEM_E_NOT_FOUND;
  645. // **** QueryLanguage
  646. hres = pDirSrch->GetColumn(SearchHandle, g_bstrADQueryLanguage, &SearchColumn);
  647. if((SUCCEEDED(hres)) && (ADSTYPE_INVALID != SearchColumn.dwADsType) && (NULL != SearchColumn.pADsValues))
  648. {
  649. vLanguage = SearchColumn.pADsValues->CaseIgnoreString;
  650. hres = pDestRuleObj->Put(g_bstrQueryLanguage, 0, &vLanguage, 0);
  651. pDirSrch->FreeColumn(&SearchColumn);
  652. if(FAILED(hres)) return hres;
  653. }
  654. // **** TargetNameSpace
  655. hres = pDirSrch->GetColumn(SearchHandle, g_bstrADTargetNameSpace, &SearchColumn);
  656. if((SUCCEEDED(hres)) && (ADSTYPE_INVALID != SearchColumn.dwADsType) && (NULL != SearchColumn.pADsValues))
  657. {
  658. vNameSpace = SearchColumn.pADsValues->CaseIgnoreString;
  659. hres = pDestRuleObj->Put(g_bstrTargetNameSpace, 0, &vNameSpace, 0);
  660. pDirSrch->FreeColumn(&SearchColumn);
  661. if(FAILED(hres)) return hres;
  662. }
  663. // **** Query
  664. hres = pDirSrch->GetColumn(SearchHandle, g_bstrADQuery, &SearchColumn);
  665. if((SUCCEEDED(hres)) && (ADSTYPE_INVALID != SearchColumn.dwADsType) && (NULL != SearchColumn.pADsValues))
  666. {
  667. vQuery = SearchColumn.pADsValues->CaseIgnoreString;
  668. hres = pDestRuleObj->Put(g_bstrQuery, 0, &vQuery, 0);
  669. hres = pDirSrch->FreeColumn(&SearchColumn);
  670. if(FAILED(hres)) return hres;
  671. }
  672. // **** stuff new rule object into SOM object
  673. hres = pDestSomObj->Get(g_bstrRules, 0, &vRules1, NULL, NULL);
  674. if(FAILED(hres)) return hres;
  675. SafeArray<IUnknown*, VT_UNKNOWN>
  676. Rules(&vRules1);
  677. hres = pDestRuleObj->QueryInterface(IID_IUnknown, (void**)&pUnknown);
  678. if(FAILED(hres)) return hres;
  679. if(pUnknown == NULL) return WBEM_E_FAILED;
  680. Rules.ReDim(0, Rules.Size() + 1);
  681. Rules[Rules.IndexMax()] = pUnknown;
  682. Rules[Rules.IndexMax()]->AddRef();
  683. // **** place array in dest som object
  684. V_VT(&vRules2) = (VT_ARRAY | Rules.Type());
  685. V_ARRAY(&vRules2) = Rules.Data();
  686. hres = pDestSomObj->Put(g_bstrRules, 0, &vRules2, 0);
  687. if(FAILED(hres)) return hres;
  688. }
  689. }
  690. return WBEM_S_NO_ERROR;
  691. }