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.

292 lines
7.6 KiB

  1. #include "StdAfx.h"
  2. #include "ADMTScript.h"
  3. #include "FixHierarchy.h"
  4. //---------------------------------------------------------------------------
  5. // Fix Objects in Hierarchy Class Implementation
  6. //---------------------------------------------------------------------------
  7. CFixObjectsInHierarchy::CFixObjectsInHierarchy() :
  8. m_bIntraForest(false),
  9. m_bFixReplaced(false)
  10. {
  11. }
  12. CFixObjectsInHierarchy::~CFixObjectsInHierarchy()
  13. {
  14. }
  15. void CFixObjectsInHierarchy::FixObjects()
  16. {
  17. m_Migrated.RetrieveMigratedObjects();
  18. int nCount = m_Migrated.GetCount();
  19. if (nCount > 0)
  20. {
  21. m_TargetPath.SetContainerPaths(m_strSourceContainerPath, m_strTargetContainerPath);
  22. for (int nIndex = 0; nIndex < nCount; nIndex++)
  23. {
  24. int nStatus = m_Migrated.GetObjectStatus(nIndex);
  25. if (m_bIntraForest || ((nStatus & CMigrated::STATUS_CREATED) || (m_bFixReplaced && (nStatus & CMigrated::STATUS_REPLACED))))
  26. {
  27. _bstr_t strType = m_Migrated.GetObjectType(nIndex);
  28. if ((strType.length() > 0) && (_tcsicmp(strType, m_strObjectType) == 0))
  29. {
  30. if (m_TargetPath.NeedsToMove(m_Migrated.GetObjectSourcePath(nIndex), m_Migrated.GetObjectTargetPath(nIndex)))
  31. {
  32. m_Containers.InsertObject(m_TargetPath.GetTargetContainerPath(), nIndex, m_TargetPath.GetTargetObjectCurrentPath());
  33. }
  34. }
  35. }
  36. }
  37. for (CContainers::iterator itContainer = m_Containers.begin(); itContainer != m_Containers.end(); itContainer++)
  38. {
  39. try
  40. {
  41. CADsContainer cContainer((LPCTSTR)itContainer->first);
  42. CObjects& ovObjects = itContainer->second;
  43. for (CObjects::iterator itObject = ovObjects.begin(); itObject != ovObjects.end(); itObject++)
  44. {
  45. try
  46. {
  47. SObject& oObject = *itObject;
  48. CADs account(IADsPtr(cContainer.MoveHere(oObject.m_strPath, _bstr_t())));
  49. m_Migrated.UpdateObjectTargetPath(oObject.m_nIndex, account.GetADsPath());
  50. }
  51. catch (_com_error& ce)
  52. {
  53. _Module.Log(ErrW, IDS_E_FIX_HIERARCHY_MOVE_OBJECT, (LPCTSTR)itObject->m_strPath, (LPCTSTR)itContainer->first, ce.ErrorMessage());
  54. }
  55. catch (...)
  56. {
  57. _Module.Log(ErrW, IDS_E_FIX_HIERARCHY_MOVE_OBJECT, (LPCTSTR)itObject->m_strPath, (LPCTSTR)itContainer->first, _com_error(E_FAIL).ErrorMessage());
  58. }
  59. }
  60. }
  61. catch (_com_error& ce)
  62. {
  63. _Module.Log(ErrW, IDS_E_FIX_HIERARCHY_BIND_TO_CONTAINER, (LPCTSTR)itContainer->first, ce.ErrorMessage());
  64. }
  65. catch (...)
  66. {
  67. _Module.Log(ErrW, IDS_E_FIX_HIERARCHY_BIND_TO_CONTAINER, (LPCTSTR)itContainer->first, _com_error(E_FAIL).ErrorMessage());
  68. }
  69. }
  70. }
  71. }
  72. //---------------------------------------------------------------------------
  73. // Migrated Objects Class Implementation
  74. //---------------------------------------------------------------------------
  75. CFixObjectsInHierarchy::CMigrated::CMigrated() :
  76. m_lActionId(-1),
  77. m_spDB(__uuidof(IManageDB))
  78. {
  79. }
  80. CFixObjectsInHierarchy::CMigrated::~CMigrated()
  81. {
  82. }
  83. int CFixObjectsInHierarchy::CMigrated::GetCount()
  84. {
  85. return long(m_vsObjects.Get(_T("MigratedObjects")));
  86. }
  87. _bstr_t CFixObjectsInHierarchy::CMigrated::GetObjectKey(int nIndex)
  88. {
  89. _TCHAR szKey[64];
  90. _stprintf(szKey, _T("MigratedObjects.%d"), nIndex);
  91. return szKey;
  92. }
  93. _bstr_t CFixObjectsInHierarchy::CMigrated::GetObjectType(int nIndex)
  94. {
  95. return m_vsObjects.Get(_T("MigratedObjects.%d.Type"), nIndex);
  96. }
  97. int CFixObjectsInHierarchy::CMigrated::GetObjectStatus(int nIndex)
  98. {
  99. return long(m_vsObjects.Get(_T("MigratedObjects.%d.status"), nIndex));
  100. }
  101. _bstr_t CFixObjectsInHierarchy::CMigrated::GetObjectSourcePath(int nIndex)
  102. {
  103. return m_vsObjects.Get(_T("MigratedObjects.%d.SourceAdsPath"), nIndex);
  104. }
  105. _bstr_t CFixObjectsInHierarchy::CMigrated::GetObjectTargetPath(int nIndex)
  106. {
  107. return m_vsObjects.Get(_T("MigratedObjects.%d.TargetAdsPath"), nIndex);
  108. }
  109. void CFixObjectsInHierarchy::CMigrated::RetrieveMigratedObjects(int nActionId)
  110. {
  111. if (nActionId > 0)
  112. {
  113. m_lActionId = nActionId;
  114. }
  115. else
  116. {
  117. m_spDB->GetCurrentActionID(&m_lActionId);
  118. }
  119. IUnknownPtr spUnknown(m_vsObjects.GetInterface());
  120. IUnknown* punk = spUnknown;
  121. m_spDB->GetMigratedObjects(m_lActionId, &punk);
  122. }
  123. void CFixObjectsInHierarchy::CMigrated::UpdateObjectTargetPath(int nIndex, _bstr_t strPath)
  124. {
  125. IVarSetPtr spVarSet(__uuidof(VarSet));
  126. spVarSet->ImportSubTree(_bstr_t(), IVarSetPtr(m_vsObjects.GetInterface()->getReference(GetObjectKey(nIndex))));
  127. spVarSet->put(_T("TargetAdsPath"), strPath);
  128. m_spDB->SaveMigratedObject(m_lActionId, IUnknownPtr(spVarSet));
  129. }
  130. //---------------------------------------------------------------------------
  131. // Target Path Class Implementation
  132. //---------------------------------------------------------------------------
  133. CFixObjectsInHierarchy::CTargetPath::CTargetPath()
  134. {
  135. }
  136. CFixObjectsInHierarchy::CTargetPath::~CTargetPath()
  137. {
  138. }
  139. void CFixObjectsInHierarchy::CTargetPath::SetContainerPaths(_bstr_t strSourceContainerPath, _bstr_t strTargetContainerPath)
  140. {
  141. m_pnSourceContainerPath.Set(strSourceContainerPath, ADS_SETTYPE_FULL);
  142. m_pnTargetContainerPath.Set(strTargetContainerPath, ADS_SETTYPE_FULL);
  143. }
  144. bool CFixObjectsInHierarchy::CTargetPath::NeedsToMove(_bstr_t strSourceObjectPath, _bstr_t strTargetObjectPath)
  145. {
  146. bool bNeedsToMove = false;
  147. // if the source object exists within the source root container hierarchy...
  148. CADsPathName pn(strSourceObjectPath);
  149. long lCount = pn.GetNumElements() - m_pnSourceContainerPath.GetNumElements();
  150. while (lCount-- > 0)
  151. {
  152. pn.RemoveLeafElement();
  153. }
  154. if (IsMatch(pn.Retrieve(ADS_FORMAT_X500_DN), m_pnSourceContainerPath.Retrieve(ADS_FORMAT_X500_DN)))
  155. {
  156. m_pnTargetObjectOldPath.Set(strTargetObjectPath, ADS_SETTYPE_FULL);
  157. // construct expected target object path
  158. m_pnTargetObjectNewPath.Set(m_pnTargetContainerPath.Retrieve(ADS_FORMAT_X500), ADS_SETTYPE_FULL);
  159. pn.Set(strSourceObjectPath, ADS_SETTYPE_FULL);
  160. long lIndex = pn.GetNumElements() - m_pnSourceContainerPath.GetNumElements();
  161. while (--lIndex >= 0)
  162. {
  163. m_pnTargetObjectNewPath.AddLeafElement(pn.GetElement(lIndex));
  164. }
  165. // compare expected target path with current target path
  166. if (!IsMatch(m_pnTargetObjectNewPath.Retrieve(ADS_FORMAT_X500_DN), m_pnTargetObjectOldPath.Retrieve(ADS_FORMAT_X500_DN)))
  167. {
  168. m_pnTargetObjectNewPath.Set(m_pnTargetObjectOldPath.Retrieve(ADS_FORMAT_SERVER), ADS_SETTYPE_SERVER);
  169. bNeedsToMove = true;
  170. }
  171. }
  172. return bNeedsToMove;
  173. }
  174. _bstr_t CFixObjectsInHierarchy::CTargetPath::GetTargetContainerPath()
  175. {
  176. CADsPathName pn(m_pnTargetObjectNewPath.Retrieve(ADS_FORMAT_X500));
  177. pn.RemoveLeafElement();
  178. return pn.Retrieve(ADS_FORMAT_X500);
  179. }
  180. _bstr_t CFixObjectsInHierarchy::CTargetPath::GetTargetObjectCurrentPath()
  181. {
  182. return m_pnTargetObjectOldPath.Retrieve(ADS_FORMAT_X500_NO_SERVER);
  183. }
  184. bool CFixObjectsInHierarchy::CTargetPath::IsMatch(LPCTSTR pszA, LPCTSTR pszB)
  185. {
  186. bool bMatch = false;
  187. if (pszA && pszB)
  188. {
  189. bMatch = (_tcsicmp(pszA, pszB) == 0) ? true : false;
  190. }
  191. return bMatch;
  192. }
  193. //---------------------------------------------------------------------------
  194. // Containers Class Implementation
  195. //---------------------------------------------------------------------------
  196. void CFixObjectsInHierarchy::CContainers::InsertObject(_bstr_t strContainerPath, int nObjectIndex, _bstr_t strObjectPathOld)
  197. {
  198. iterator it = find(strContainerPath);
  199. if (it == end())
  200. {
  201. _Pairib pair = insert(value_type(strContainerPath, CObjects()));
  202. it = pair.first;
  203. }
  204. CObjects& ov = it->second;
  205. ov.push_back(SObject(nObjectIndex, strObjectPathOld));
  206. }