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.

232 lines
5.2 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <wbemcomn.h>
  4. #include <activeds.h>
  5. #include <ArrTempl.h>
  6. #include <comutil.h>
  7. #undef _ASSERT
  8. #include <atlbase.h>
  9. #include <activeds.h>
  10. #include "Utility.h"
  11. /*********************************************************************
  12. ************** Active Directory Methods ******************************
  13. *********************************************************************/
  14. #define MAX_ATTR 4
  15. HRESULT WMIGPO_CIMToAD(IWbemClassObject *pSrcWMIGPOObj, IDirectoryObject *pDestContainer, long lFlags)
  16. {
  17. HRESULT
  18. hres = WBEM_S_NO_ERROR;
  19. CComVariant
  20. v1, v2, v3;
  21. long
  22. nArgs = 0,
  23. numRules = 0,
  24. c1;
  25. CIMTYPE vtType1;
  26. CComPtr<IDispatch>
  27. pDisp;
  28. CComPtr<IWbemClassObject>
  29. pRuleObj;
  30. CComPtr<IDirectoryObject>
  31. pDestSomObj;
  32. CComPtr<IADsContainer>
  33. pADsContainer;
  34. ADSVALUE
  35. AdsValue[MAX_ATTR];
  36. ADS_ATTR_INFO
  37. attrInfo[MAX_ATTR];
  38. Init_AdsAttrInfo(&attrInfo[nArgs], g_bstrADObjectClass, ADS_ATTR_UPDATE, ADSTYPE_CASE_IGNORE_STRING, &AdsValue[nArgs], 1);
  39. AdsValue[nArgs].CaseIgnoreString = g_bstrADClassWMIGPO;
  40. nArgs++;
  41. // **** TargetClass
  42. hres = pSrcWMIGPOObj->Get(L"PolicyTemplate", 0, &v1, NULL, NULL);
  43. if(FAILED(hres)) return hres;
  44. if(v1.vt != (VT_ARRAY | VT_BSTR))
  45. {
  46. return WBEM_E_ILLEGAL_NULL;
  47. }
  48. else
  49. {
  50. SafeArray<BSTR, VT_BSTR>
  51. Array1(&v1);
  52. QString
  53. references;
  54. for(c1 = 0; c1 < Array1.Size(); c1++)
  55. {
  56. references << Array1[c1] << L";";
  57. }
  58. Init_AdsAttrInfo(&attrInfo[nArgs], g_bstrADTargetClass, ADS_ATTR_UPDATE, ADSTYPE_CASE_IGNORE_STRING, &AdsValue[nArgs], 1);
  59. AdsValue[nArgs].CaseIgnoreString = references;
  60. nArgs++;
  61. // **** create AD WMIGPO object
  62. hres = pDestContainer->QueryInterface(IID_IADsContainer, (void**)&pADsContainer);
  63. if(FAILED(hres) || (pADsContainer == NULL)) return WBEM_E_FAILED;
  64. if(SUCCEEDED(hres = pADsContainer->GetObject(g_bstrADClassWMIGPO, L"CN=SINGLE_WMIGPO", &pDisp)))
  65. {
  66. if(pDisp == NULL) return WBEM_E_FAILED;
  67. if(lFlags & WBEM_FLAG_CREATE_ONLY) return WBEM_E_ALREADY_EXISTS;
  68. CComPtr<IADsDeleteOps>
  69. pDeleteOps;
  70. hres = pDisp->QueryInterface(IID_IADsDeleteOps, (void **)&pDeleteOps);
  71. if(FAILED(hres) || (pDeleteOps == NULL)) return WBEM_E_FAILED;
  72. hres = pDeleteOps->DeleteObject(0);
  73. }
  74. else
  75. {
  76. if(WBEM_FLAG_UPDATE_ONLY & lFlags) return WBEM_E_NOT_FOUND;
  77. }
  78. hres = pDestContainer->CreateDSObject(L"CN=SINGLE_WMIGPO", attrInfo, nArgs, &pDisp);
  79. }
  80. return hres;
  81. }
  82. HRESULT WMIGPO_ADToCIM(IWbemClassObject **ppDestWMIGPOObj, IDirectoryObject *pSrcWMIGPOObj, IWbemServices *pDestCIM)
  83. {
  84. HRESULT
  85. hres = WBEM_S_NO_ERROR;
  86. CComVariant
  87. v1;
  88. wchar_t* AttrNames[] =
  89. {
  90. g_bstrADTargetClass
  91. };
  92. ADsStruct<ADS_ATTR_INFO>
  93. pAttrInfo;
  94. ADS_SEARCH_HANDLE
  95. SearchHandle;
  96. ADS_SEARCH_COLUMN
  97. SearchColumn;
  98. unsigned long
  99. c1, c2, dwReturn;
  100. CComPtr<IUnknown>
  101. pUnknown;
  102. CComPtr<IDirectorySearch>
  103. pDirSrch;
  104. CComPtr<IWbemClassObject>
  105. pClassDef,
  106. pDestWMIGPOObj;
  107. IWbemContext
  108. *pCtx = 0;
  109. ADsStruct<ADS_OBJECT_INFO>
  110. pInfo;
  111. // **** create empty WMIGPO object
  112. hres = pDestCIM->GetObject(g_bstrClassWMIGPO, 0, pCtx, &pClassDef, NULL);
  113. if(FAILED(hres)) return hres;
  114. if(pClassDef == NULL) return WBEM_E_FAILED;
  115. hres = pClassDef->SpawnInstance(0, ppDestWMIGPOObj);
  116. if(FAILED(hres)) return hres;
  117. pDestWMIGPOObj = *ppDestWMIGPOObj;
  118. if(pDestWMIGPOObj == NULL) return WBEM_E_INVALID_CLASS;
  119. // **** get object attributes
  120. hres = pSrcWMIGPOObj->GetObjectAttributes(AttrNames, 1, &pAttrInfo, &dwReturn);
  121. if(FAILED(hres)) return hres;
  122. if(pAttrInfo == NULL) return WBEM_E_NOT_FOUND;
  123. // **** DsPath
  124. hres = pSrcWMIGPOObj->GetObjectInformation(&pInfo);
  125. if(SUCCEEDED(hres) && (pInfo != NULL))
  126. {
  127. v1 = pInfo->pszParentDN;
  128. hres = pDestWMIGPOObj->Put(g_bstrDsPath, 0, &v1, 0);
  129. }
  130. if(pInfo == NULL) return WBEM_E_FAILED;
  131. if(FAILED(hres)) return hres;
  132. for(c1 = 0; c1 < dwReturn; c1++)
  133. {
  134. // **** TargetClass
  135. if(0 == _wcsicmp((pAttrInfo + c1)->pszAttrName, g_bstrADTargetClass))
  136. {
  137. wchar_t
  138. *pStrBegin = NULL,
  139. *pStrEnd = NULL;
  140. pStrBegin = (pAttrInfo + c1)->pADsValues->CaseIgnoreString;
  141. if(NULL != pStrBegin)
  142. {
  143. pStrEnd = wcsstr(pStrBegin, L";");
  144. while(NULL != pStrBegin)
  145. {
  146. if(NULL != pStrEnd)
  147. *pStrEnd = L'\0';
  148. hres = pDestWMIGPOObj->Get(L"PolicyTemplate", 0, &v1, NULL, NULL);
  149. if(FAILED(hres)) return hres;
  150. SafeArray<BSTR, VT_BSTR>
  151. TargetClasses(&v1);
  152. VariantClear(&v1);
  153. TargetClasses.ReDim(0, TargetClasses.Size() + 1);
  154. TargetClasses[TargetClasses.IndexMax()] = SysAllocString(pStrBegin);
  155. V_VT(&v1) = (VT_ARRAY | VT_BSTR);
  156. V_ARRAY(&v1) = TargetClasses.Data();
  157. hres = pDestWMIGPOObj->Put(L"PolicyTemplate", 0, &v1, 0);
  158. if(FAILED(hres)) return hres;
  159. VariantClear(&v1);
  160. pStrBegin = pStrEnd + 1;
  161. if(L'\0' == *pStrBegin)
  162. pStrBegin = NULL;
  163. else
  164. pStrEnd = wcsstr(pStrBegin, L";");
  165. }
  166. }
  167. }
  168. VariantClear(&v1);
  169. }
  170. return WBEM_S_NO_ERROR;
  171. }