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.

405 lines
8.3 KiB

  1. #include "StdAfx.h"
  2. #include "ADMTScript.h"
  3. #include "GroupMigration.h"
  4. #include "Error.h"
  5. #include "VarSetOptions.h"
  6. #include "VarSetAccountOptions.h"
  7. #include "VarSetSecurity.h"
  8. //---------------------------------------------------------------------------
  9. // Group Migration Class
  10. //---------------------------------------------------------------------------
  11. CGroupMigration::CGroupMigration() :
  12. m_bMigrateSids(false),
  13. m_bUpdateGroupRights(false),
  14. m_bUpdateMigrated(false),
  15. m_bFixGroupMembership(true),
  16. m_bMigrateMembers(false),
  17. m_lDisableOption(admtEnableTarget),
  18. m_lSourceExpiration(admtNoExpiration),
  19. m_bTranslateRoamingProfile(false)
  20. {
  21. }
  22. CGroupMigration::~CGroupMigration()
  23. {
  24. }
  25. // IGroupMigration Implementation -------------------------------------------
  26. // MigrateSIDs Property
  27. STDMETHODIMP CGroupMigration::put_MigrateSIDs(VARIANT_BOOL bMigrate)
  28. {
  29. m_bMigrateSids = bMigrate ? true : false;
  30. return S_OK;
  31. }
  32. STDMETHODIMP CGroupMigration::get_MigrateSIDs(VARIANT_BOOL* pbMigrate)
  33. {
  34. *pbMigrate = m_bMigrateSids ? VARIANT_TRUE : VARIANT_FALSE;
  35. return S_OK;
  36. }
  37. // UpdateGroupRights Property
  38. STDMETHODIMP CGroupMigration::put_UpdateGroupRights(VARIANT_BOOL bUpdate)
  39. {
  40. m_bUpdateGroupRights = bUpdate ? true : false;
  41. return S_OK;
  42. }
  43. STDMETHODIMP CGroupMigration::get_UpdateGroupRights(VARIANT_BOOL* pbUpdate)
  44. {
  45. *pbUpdate = m_bUpdateGroupRights ? VARIANT_TRUE : VARIANT_FALSE;
  46. return S_OK;
  47. }
  48. // UpdatePreviouslyMigratedObjects Property
  49. STDMETHODIMP CGroupMigration::put_UpdatePreviouslyMigratedObjects(VARIANT_BOOL bUpdate)
  50. {
  51. m_bUpdateMigrated = bUpdate ? true : false;
  52. return S_OK;
  53. }
  54. STDMETHODIMP CGroupMigration::get_UpdatePreviouslyMigratedObjects(VARIANT_BOOL* pbUpdate)
  55. {
  56. *pbUpdate = m_bUpdateMigrated ? VARIANT_TRUE : VARIANT_FALSE;
  57. return S_OK;
  58. }
  59. // FixGroupMembership Property
  60. STDMETHODIMP CGroupMigration::put_FixGroupMembership(VARIANT_BOOL bFix)
  61. {
  62. m_bFixGroupMembership = bFix ? true : false;
  63. return S_OK;
  64. }
  65. STDMETHODIMP CGroupMigration::get_FixGroupMembership(VARIANT_BOOL* pbFix)
  66. {
  67. *pbFix = m_bFixGroupMembership ? VARIANT_TRUE : VARIANT_FALSE;
  68. return S_OK;
  69. }
  70. // MigrateMembers Property
  71. STDMETHODIMP CGroupMigration::put_MigrateMembers(VARIANT_BOOL bMigrate)
  72. {
  73. m_bMigrateMembers = bMigrate ? true : false;
  74. return S_OK;
  75. }
  76. STDMETHODIMP CGroupMigration::get_MigrateMembers(VARIANT_BOOL* pbMigrate)
  77. {
  78. *pbMigrate = m_bMigrateMembers ? VARIANT_TRUE : VARIANT_FALSE;
  79. return S_OK;
  80. }
  81. // DisableOption Property
  82. STDMETHODIMP CGroupMigration::put_DisableOption(long lOption)
  83. {
  84. HRESULT hr = S_OK;
  85. if (IsDisableOptionValid(lOption))
  86. {
  87. m_lDisableOption = lOption;
  88. }
  89. else
  90. {
  91. hr = AdmtSetError(CLSID_Migration, IID_IGroupMigration, E_INVALIDARG, IDS_E_DISABLE_OPTION_INVALID);
  92. }
  93. return hr;
  94. }
  95. STDMETHODIMP CGroupMigration::get_DisableOption(long* plOption)
  96. {
  97. *plOption = m_lDisableOption;
  98. return S_OK;
  99. }
  100. // SourceExpiration Property
  101. STDMETHODIMP CGroupMigration::put_SourceExpiration(long lExpiration)
  102. {
  103. HRESULT hr = S_OK;
  104. if (IsSourceExpirationValid(lExpiration))
  105. {
  106. m_lSourceExpiration = lExpiration;
  107. }
  108. else
  109. {
  110. hr = AdmtSetError(CLSID_Migration, IID_IGroupMigration, E_INVALIDARG, IDS_E_SOURCE_EXPIRATION_INVALID);
  111. }
  112. return hr;
  113. }
  114. STDMETHODIMP CGroupMigration::get_SourceExpiration(long* plExpiration)
  115. {
  116. *plExpiration = m_lSourceExpiration;
  117. return S_OK;
  118. }
  119. // TranslateRoamingProfile Property
  120. STDMETHODIMP CGroupMigration::put_TranslateRoamingProfile(VARIANT_BOOL bTranslate)
  121. {
  122. m_bTranslateRoamingProfile = bTranslate ? true : false;
  123. return S_OK;
  124. }
  125. STDMETHODIMP CGroupMigration::get_TranslateRoamingProfile(VARIANT_BOOL* pbTranslate)
  126. {
  127. *pbTranslate = m_bTranslateRoamingProfile ? VARIANT_TRUE : VARIANT_FALSE;
  128. return S_OK;
  129. }
  130. // Migrate Method
  131. STDMETHODIMP CGroupMigration::Migrate(long lOptions, VARIANT vntInclude, VARIANT vntExclude)
  132. {
  133. HRESULT hr = S_OK;
  134. MutexWait();
  135. bool bLogOpen = _Module.OpenLog();
  136. try
  137. {
  138. _Module.Log(ErrI, IDS_STARTED_GROUP_MIGRATION);
  139. InitSourceDomainAndContainer();
  140. InitTargetDomainAndContainer();
  141. VerifyInterIntraForest();
  142. ValidateMigrationParameters();
  143. if (m_bMigrateSids)
  144. {
  145. VerifyCanAddSidHistory();
  146. }
  147. VerifyPasswordOption();
  148. DoOption(lOptions, vntInclude, vntExclude);
  149. }
  150. catch (_com_error& ce)
  151. {
  152. hr = AdmtSetError(CLSID_Migration, IID_IGroupMigration, ce, IDS_E_CANT_MIGRATE_GROUPS);
  153. }
  154. catch (...)
  155. {
  156. hr = AdmtSetError(CLSID_Migration, IID_IGroupMigration, E_FAIL, IDS_E_CANT_MIGRATE_GROUPS);
  157. }
  158. if (bLogOpen)
  159. {
  160. _Module.CloseLog();
  161. }
  162. MutexRelease();
  163. return hr;
  164. }
  165. // Implementation -----------------------------------------------------------
  166. // ValidateMigrationParameters Method
  167. void CGroupMigration::ValidateMigrationParameters()
  168. {
  169. bool bIntraForest = m_spInternal->IntraForest ? true : false;
  170. if (bIntraForest)
  171. {
  172. // validate conflict option
  173. long lConflictOptions = m_spInternal->ConflictOptions;
  174. long lConflictOption = lConflictOptions & 0x0F;
  175. if (lConflictOption == admtReplaceConflicting)
  176. {
  177. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, IDS_E_INTRA_FOREST_REPLACE);
  178. }
  179. }
  180. }
  181. // DoNames Method
  182. void CGroupMigration::DoNames()
  183. {
  184. CDomainAccounts aGroups;
  185. m_SourceDomain.QueryGroups(GetSourceContainer(), m_setIncludeNames, m_setExcludeNames, aGroups);
  186. DoGroups(aGroups, GetTargetContainer());
  187. }
  188. // DoDomain Method
  189. void CGroupMigration::DoDomain()
  190. {
  191. CContainer& rSource = GetSourceContainer();
  192. CContainer& rTarget = GetTargetContainer();
  193. if (m_nRecurseMaintain == 2)
  194. {
  195. rTarget.CreateContainerHierarchy(rSource);
  196. }
  197. DoContainers(rSource, rTarget);
  198. }
  199. // DoContainers Method
  200. void CGroupMigration::DoContainers(CContainer& rSource, CContainer& rTarget)
  201. {
  202. DoGroups(rSource, rTarget);
  203. if (m_nRecurseMaintain == 2)
  204. {
  205. ContainerVector aContainers;
  206. rSource.QueryContainers(aContainers);
  207. for (ContainerVector::iterator it = aContainers.begin(); it != aContainers.end(); it++)
  208. {
  209. DoContainers(*it, rTarget.GetContainer(it->GetName()));
  210. }
  211. }
  212. }
  213. // DoGroups Method
  214. void CGroupMigration::DoGroups(CContainer& rSource, CContainer& rTarget)
  215. {
  216. CDomainAccounts aGroups;
  217. rSource.QueryGroups(m_nRecurseMaintain == 1, m_setExcludeNames, aGroups);
  218. DoGroups(aGroups, rTarget);
  219. }
  220. // DoGroups Method
  221. void CGroupMigration::DoGroups(CDomainAccounts& rGroups, CContainer& rTarget)
  222. {
  223. if (rGroups.size() > 0)
  224. {
  225. CVarSet aVarSet;
  226. SetOptions(rTarget.GetPath(), aVarSet);
  227. SetAccountOptions(aVarSet);
  228. VerifyRenameConflictPrefixSuffixValid();
  229. FillInVarSetForGroups(rGroups, aVarSet);
  230. rGroups.clear();
  231. #ifdef _DEBUG
  232. aVarSet.Dump();
  233. #endif
  234. PerformMigration(aVarSet);
  235. SaveSettings(aVarSet);
  236. if ((m_nRecurseMaintain == 2) && m_bMigrateMembers)
  237. {
  238. FixObjectsInHierarchy(_T("user"));
  239. }
  240. }
  241. }
  242. // SetOptions Method
  243. void CGroupMigration::SetOptions(_bstr_t strTargetOu, CVarSet& rVarSet)
  244. {
  245. CVarSetOptions aOptions(rVarSet);
  246. aOptions.SetTest(m_spInternal->TestMigration ? true : false);
  247. aOptions.SetUndo(false);
  248. aOptions.SetWizard(_T("group"));
  249. aOptions.SetIntraForest(m_spInternal->IntraForest ? true : false);
  250. aOptions.SetSourceDomain(m_SourceDomain.NameFlat(), m_SourceDomain.NameDns(), m_SourceDomain.Sid());
  251. aOptions.SetTargetDomain(m_TargetDomain.NameFlat(), m_TargetDomain.NameDns());
  252. aOptions.SetTargetOu(strTargetOu);
  253. if (m_spInternal->PasswordOption == admtCopyPassword)
  254. {
  255. aOptions.SetTargetServer(m_TargetDomain.DomainControllerName());
  256. }
  257. aOptions.SetRenameOptions(m_spInternal->RenameOption, m_spInternal->RenamePrefixOrSuffix);
  258. }
  259. // SetAccountOptions Method
  260. void CGroupMigration::SetAccountOptions(CVarSet& rVarSet)
  261. {
  262. CVarSetAccountOptions aOptions(rVarSet);
  263. aOptions.SetPasswordOption(m_spInternal->PasswordOption, m_spInternal->PasswordServer);
  264. aOptions.SetPasswordFile(m_spInternal->PasswordFile);
  265. aOptions.SetConflictOptions(m_spInternal->ConflictOptions, m_spInternal->ConflictPrefixOrSuffix);
  266. aOptions.SetDisableOption(m_lDisableOption);
  267. aOptions.SetSourceExpiration(m_lSourceExpiration);
  268. aOptions.SetMigrateSids(m_bMigrateSids);
  269. aOptions.SetGroupMigrationOptions(m_bMigrateMembers, m_bUpdateMigrated);
  270. // aOptions.SetSidHistoryCredentials(NULL, NULL, NULL);
  271. aOptions.SetFixGroupMembership(m_bFixGroupMembership);
  272. aOptions.SetUpdateUserRights(m_bUpdateGroupRights);
  273. aOptions.SetTranslateRoamingProfile(m_bTranslateRoamingProfile);
  274. if (m_bMigrateMembers)
  275. {
  276. aOptions.SetExcludedUserProps(m_spInternal->UserPropertiesToExclude);
  277. }
  278. aOptions.SetExcludedGroupProps(m_spInternal->GroupPropertiesToExclude);
  279. }