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.

428 lines
9.1 KiB

  1. #include "StdAfx.h"
  2. #include "ADMTScript.h"
  3. #include "ComputerMigration.h"
  4. #include "Error.h"
  5. #include "VarSetOptions.h"
  6. #include "VarSetAccountOptions.h"
  7. #include "VarSetSecurity.h"
  8. //---------------------------------------------------------------------------
  9. // Computer Migration Class
  10. //---------------------------------------------------------------------------
  11. CComputerMigration::CComputerMigration() :
  12. m_lTranslationOption(admtTranslateReplace),
  13. m_bTranslateFilesAndFolders(false),
  14. m_bTranslateLocalGroups(false),
  15. m_bTranslatePrinters(false),
  16. m_bTranslateRegistry(false),
  17. m_bTranslateShares(false),
  18. m_bTranslateUserProfiles(false),
  19. m_bTranslateUserRights(false),
  20. m_lRestartDelay(5)
  21. {
  22. }
  23. CComputerMigration::~CComputerMigration()
  24. {
  25. }
  26. // IComputerMigration Implementation ----------------------------------------
  27. // TranslationOption Property
  28. STDMETHODIMP CComputerMigration::put_TranslationOption(long lOption)
  29. {
  30. HRESULT hr = S_OK;
  31. if (IsTranslationOptionValid(lOption))
  32. {
  33. m_lTranslationOption = lOption;
  34. }
  35. else
  36. {
  37. hr = AdmtSetError(CLSID_Migration, IID_IComputerMigration, E_INVALIDARG, IDS_E_TRANSLATION_OPTION_INVALID);
  38. }
  39. return hr;
  40. }
  41. STDMETHODIMP CComputerMigration::get_TranslationOption(long* plOption)
  42. {
  43. *plOption = m_lTranslationOption;
  44. return S_OK;
  45. }
  46. // TranslateFilesAndFolders Property
  47. STDMETHODIMP CComputerMigration::put_TranslateFilesAndFolders(VARIANT_BOOL bTranslate)
  48. {
  49. m_bTranslateFilesAndFolders = bTranslate ? true : false;
  50. return S_OK;
  51. }
  52. STDMETHODIMP CComputerMigration::get_TranslateFilesAndFolders(VARIANT_BOOL* pbTranslate)
  53. {
  54. *pbTranslate = m_bTranslateFilesAndFolders ? VARIANT_TRUE : VARIANT_FALSE;
  55. return S_OK;
  56. }
  57. // TranslateLocalGroups Property
  58. STDMETHODIMP CComputerMigration::put_TranslateLocalGroups(VARIANT_BOOL bTranslate)
  59. {
  60. m_bTranslateLocalGroups = bTranslate ? true : false;
  61. return S_OK;
  62. }
  63. STDMETHODIMP CComputerMigration::get_TranslateLocalGroups(VARIANT_BOOL* pbTranslate)
  64. {
  65. *pbTranslate = m_bTranslateLocalGroups ? VARIANT_TRUE : VARIANT_FALSE;
  66. return S_OK;
  67. }
  68. // TranslatePrinters Property
  69. STDMETHODIMP CComputerMigration::put_TranslatePrinters(VARIANT_BOOL bTranslate)
  70. {
  71. m_bTranslatePrinters = bTranslate ? true : false;
  72. return S_OK;
  73. }
  74. STDMETHODIMP CComputerMigration::get_TranslatePrinters(VARIANT_BOOL* pbTranslate)
  75. {
  76. *pbTranslate = m_bTranslatePrinters ? VARIANT_TRUE : VARIANT_FALSE;
  77. return S_OK;
  78. }
  79. // TranslateRegistry Property
  80. STDMETHODIMP CComputerMigration::put_TranslateRegistry(VARIANT_BOOL bTranslate)
  81. {
  82. m_bTranslateRegistry = bTranslate ? true : false;
  83. return S_OK;
  84. }
  85. STDMETHODIMP CComputerMigration::get_TranslateRegistry(VARIANT_BOOL* pbTranslate)
  86. {
  87. *pbTranslate = m_bTranslateRegistry ? VARIANT_TRUE : VARIANT_FALSE;
  88. return S_OK;
  89. }
  90. // TranslateShares Property
  91. STDMETHODIMP CComputerMigration::put_TranslateShares(VARIANT_BOOL bTranslate)
  92. {
  93. m_bTranslateShares = bTranslate ? true : false;
  94. return S_OK;
  95. }
  96. STDMETHODIMP CComputerMigration::get_TranslateShares(VARIANT_BOOL* pbTranslate)
  97. {
  98. *pbTranslate = m_bTranslateShares ? VARIANT_TRUE : VARIANT_FALSE;
  99. return S_OK;
  100. }
  101. // TranslateUserProfiles Property
  102. STDMETHODIMP CComputerMigration::put_TranslateUserProfiles(VARIANT_BOOL bTranslate)
  103. {
  104. m_bTranslateUserProfiles = bTranslate ? true : false;
  105. return S_OK;
  106. }
  107. STDMETHODIMP CComputerMigration::get_TranslateUserProfiles(VARIANT_BOOL* pbTranslate)
  108. {
  109. *pbTranslate = m_bTranslateUserProfiles ? VARIANT_TRUE : VARIANT_FALSE;
  110. return S_OK;
  111. }
  112. // TranslateUserRights Property
  113. STDMETHODIMP CComputerMigration::put_TranslateUserRights(VARIANT_BOOL bTranslate)
  114. {
  115. m_bTranslateUserRights = bTranslate ? true : false;
  116. return S_OK;
  117. }
  118. STDMETHODIMP CComputerMigration::get_TranslateUserRights(VARIANT_BOOL* pbTranslate)
  119. {
  120. *pbTranslate = m_bTranslateUserRights ? VARIANT_TRUE : VARIANT_FALSE;
  121. return S_OK;
  122. }
  123. // RestartDelay Property
  124. STDMETHODIMP CComputerMigration::put_RestartDelay(long lTime)
  125. {
  126. HRESULT hr = S_OK;
  127. if ((lTime >= 1) && (lTime <= 10))
  128. {
  129. m_lRestartDelay = lTime;
  130. }
  131. else
  132. {
  133. hr = AdmtSetError(CLSID_Migration, IID_IComputerMigration, E_INVALIDARG, IDS_E_RESTART_DELAY_INVALID);
  134. }
  135. return hr;
  136. }
  137. STDMETHODIMP CComputerMigration::get_RestartDelay(long* plTime)
  138. {
  139. *plTime = m_lRestartDelay;
  140. return S_OK;
  141. }
  142. // Migrate Method
  143. STDMETHODIMP CComputerMigration::Migrate(long lOptions, VARIANT vntInclude, VARIANT vntExclude)
  144. {
  145. HRESULT hr = S_OK;
  146. MutexWait();
  147. bool bLogOpen = _Module.OpenLog();
  148. try
  149. {
  150. _Module.Log(ErrI, IDS_STARTED_COMPUTER_MIGRATION);
  151. InitSourceDomainAndContainer();
  152. InitTargetDomainAndContainer();
  153. VerifyInterIntraForest();
  154. ValidateMigrationParameters();
  155. DoOption(lOptions, vntInclude, vntExclude);
  156. }
  157. catch (_com_error& ce)
  158. {
  159. hr = AdmtSetError(CLSID_Migration, IID_IComputerMigration, ce, IDS_E_CANT_MIGRATE_COMPUTERS);
  160. }
  161. catch (...)
  162. {
  163. hr = AdmtSetError(CLSID_Migration, IID_IComputerMigration, E_FAIL, IDS_E_CANT_MIGRATE_COMPUTERS);
  164. }
  165. if (bLogOpen)
  166. {
  167. _Module.CloseLog();
  168. }
  169. MutexRelease();
  170. return hr;
  171. }
  172. // Implementation -----------------------------------------------------------
  173. // ValidateMigrationParameters Method
  174. void CComputerMigration::ValidateMigrationParameters()
  175. {
  176. bool bIntraForest = m_spInternal->IntraForest ? true : false;
  177. if (bIntraForest)
  178. {
  179. // validate conflict option
  180. long lConflictOptions = m_spInternal->ConflictOptions;
  181. long lConflictOption = lConflictOptions & 0x0F;
  182. if (lConflictOption == admtReplaceConflicting)
  183. {
  184. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, IDS_E_INTRA_FOREST_REPLACE);
  185. }
  186. }
  187. }
  188. // DoNames Method
  189. void CComputerMigration::DoNames()
  190. {
  191. CDomainAccounts aComputers;
  192. m_SourceDomain.QueryComputers(GetSourceContainer(), false, m_setIncludeNames, m_setExcludeNames, aComputers);
  193. DoComputers(aComputers, GetTargetContainer());
  194. }
  195. // DoDomain Method
  196. void CComputerMigration::DoDomain()
  197. {
  198. CContainer& rSource = GetSourceContainer();
  199. CContainer& rTarget = GetTargetContainer();
  200. if (m_nRecurseMaintain == 2)
  201. {
  202. rTarget.CreateContainerHierarchy(rSource);
  203. }
  204. DoContainers(rSource, rTarget);
  205. }
  206. // DoContainers Method
  207. void CComputerMigration::DoContainers(CContainer& rSource, CContainer& rTarget)
  208. {
  209. DoComputers(rSource, rTarget);
  210. if (m_nRecurseMaintain == 2)
  211. {
  212. ContainerVector aContainers;
  213. rSource.QueryContainers(aContainers);
  214. for (ContainerVector::iterator it = aContainers.begin(); it != aContainers.end(); it++)
  215. {
  216. DoContainers(*it, rTarget.GetContainer(it->GetName()));
  217. }
  218. }
  219. }
  220. // DoComputers Method
  221. void CComputerMigration::DoComputers(CContainer& rSource, CContainer& rTarget)
  222. {
  223. CDomainAccounts aComputers;
  224. rSource.QueryComputers(false, m_nRecurseMaintain == 1, m_setExcludeNames, aComputers);
  225. DoComputers(aComputers, rTarget);
  226. }
  227. // DoComputers Method
  228. void CComputerMigration::DoComputers(CDomainAccounts& rComputers, CContainer& rTarget)
  229. {
  230. if (rComputers.size() > 0)
  231. {
  232. CVarSet aVarSet;
  233. SetOptions(rTarget.GetPath(), aVarSet);
  234. SetAccountOptions(aVarSet);
  235. SetSecurity(aVarSet);
  236. VerifyRenameConflictPrefixSuffixValid();
  237. FillInVarSetForComputers(rComputers, false, true, true, m_lRestartDelay, aVarSet);
  238. rComputers.clear();
  239. if (m_bTranslateUserProfiles)
  240. {
  241. aVarSet.Put(_T("PlugIn.%ld"), 0, _T("{0EB9FBE9-397D-4D09-A65E-ABF1790CC470}"));
  242. }
  243. else
  244. {
  245. aVarSet.Put(_T("PlugIn.%ld"), 0, _T("None"));
  246. }
  247. #ifdef _DEBUG
  248. aVarSet.Dump();
  249. #endif
  250. PerformMigration(aVarSet);
  251. SaveSettings(aVarSet);
  252. }
  253. }
  254. // SetOptions Method
  255. void CComputerMigration::SetOptions(_bstr_t strTargetOu, CVarSet& rVarSet)
  256. {
  257. CVarSetOptions aOptions(rVarSet);
  258. aOptions.SetTest(m_spInternal->TestMigration ? true : false);
  259. aOptions.SetUndo(false);
  260. aOptions.SetWizard(_T("computer"));
  261. aOptions.SetIntraForest(m_spInternal->IntraForest ? true : false);
  262. aOptions.SetSourceDomain(m_SourceDomain.NameFlat(), m_SourceDomain.NameDns(), m_SourceDomain.Sid());
  263. aOptions.SetTargetDomain(m_TargetDomain.NameFlat(), m_TargetDomain.NameDns());
  264. aOptions.SetTargetOu(strTargetOu);
  265. aOptions.SetRenameOptions(m_spInternal->RenameOption, m_spInternal->RenamePrefixOrSuffix);
  266. aOptions.SetRestartDelay(m_lRestartDelay);
  267. }
  268. // SetAccountOptions Method
  269. void CComputerMigration::SetAccountOptions(CVarSet& rVarSet)
  270. {
  271. CVarSetAccountOptions aOptions(rVarSet);
  272. // naming conflict options
  273. // removing existing members of groups being replaced is not applicable
  274. aOptions.SetConflictOptions(
  275. m_spInternal->ConflictOptions & ~admtRemoveExistingMembers,
  276. m_spInternal->ConflictPrefixOrSuffix
  277. );
  278. aOptions.SetMigrateSids(false);
  279. aOptions.SetComputerMigrationOptions();
  280. aOptions.SetExcludedComputerProps(m_spInternal->ComputerPropertiesToExclude);
  281. }
  282. // SetSecurity Method
  283. void CComputerMigration::SetSecurity(CVarSet& rVarSet)
  284. {
  285. CVarSetSecurity aSecurity(rVarSet);
  286. aSecurity.SetTranslationOption(m_lTranslationOption);
  287. aSecurity.SetTranslateContainers(false);
  288. aSecurity.SetTranslateFiles(m_bTranslateFilesAndFolders);
  289. aSecurity.SetTranslateLocalGroups(m_bTranslateLocalGroups);
  290. aSecurity.SetTranslatePrinters(m_bTranslatePrinters);
  291. aSecurity.SetTranslateRegistry(m_bTranslateRegistry);
  292. aSecurity.SetTranslateShares(m_bTranslateShares);
  293. aSecurity.SetTranslateUserProfiles(m_bTranslateUserProfiles);
  294. aSecurity.SetTranslateUserRights(m_bTranslateUserRights);
  295. }