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.

413 lines
9.7 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. SetDefaultExcludedSystemProperties();
  142. VerifyInterIntraForest();
  143. VerifyCallerDelegated();
  144. ValidateMigrationParameters();
  145. if (m_bMigrateSids)
  146. {
  147. VerifyCanAddSidHistory();
  148. }
  149. VerifyPasswordOption();
  150. DoOption(lOptions, vntInclude, vntExclude);
  151. }
  152. catch (_com_error& ce)
  153. {
  154. _Module.Log(ErrE, IDS_E_CANT_MIGRATE_GROUPS, ce);
  155. hr = AdmtSetError(CLSID_Migration, IID_IGroupMigration, ce, IDS_E_CANT_MIGRATE_GROUPS);
  156. }
  157. catch (...)
  158. {
  159. _Module.Log(ErrE, IDS_E_CANT_MIGRATE_GROUPS, _com_error(E_FAIL));
  160. hr = AdmtSetError(CLSID_Migration, IID_IGroupMigration, E_FAIL, IDS_E_CANT_MIGRATE_GROUPS);
  161. }
  162. if (bLogOpen)
  163. {
  164. _Module.CloseLog();
  165. }
  166. MutexRelease();
  167. return hr;
  168. }
  169. // Implementation -----------------------------------------------------------
  170. // ValidateMigrationParameters Method
  171. void CGroupMigration::ValidateMigrationParameters()
  172. {
  173. bool bIntraForest = m_spInternal->IntraForest ? true : false;
  174. if (bIntraForest)
  175. {
  176. // validate conflict option
  177. long lConflictOptions = m_spInternal->ConflictOptions;
  178. long lConflictOption = lConflictOptions & 0x0F;
  179. if (lConflictOption == admtReplaceConflicting)
  180. {
  181. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, IDS_E_INTRA_FOREST_REPLACE);
  182. }
  183. }
  184. }
  185. // DoNames Method
  186. void CGroupMigration::DoNames()
  187. {
  188. CDomainAccounts aGroups;
  189. m_SourceDomain.QueryGroups(GetSourceContainer(), m_setIncludeNames, m_setExcludeNames, aGroups);
  190. DoGroups(aGroups, GetTargetContainer());
  191. }
  192. // DoDomain Method
  193. void CGroupMigration::DoDomain()
  194. {
  195. CContainer& rSource = GetSourceContainer();
  196. CContainer& rTarget = GetTargetContainer();
  197. if (m_nRecurseMaintain == 2)
  198. {
  199. rTarget.CreateContainerHierarchy(rSource);
  200. }
  201. DoContainers(rSource, rTarget);
  202. }
  203. // DoContainers Method
  204. void CGroupMigration::DoContainers(CContainer& rSource, CContainer& rTarget)
  205. {
  206. DoGroups(rSource, rTarget);
  207. if (m_nRecurseMaintain == 2)
  208. {
  209. ContainerVector aContainers;
  210. rSource.QueryContainers(aContainers);
  211. for (ContainerVector::iterator it = aContainers.begin(); it != aContainers.end(); it++)
  212. {
  213. DoContainers(*it, rTarget.GetContainer(it->GetName()));
  214. }
  215. }
  216. }
  217. // DoGroups Method
  218. void CGroupMigration::DoGroups(CContainer& rSource, CContainer& rTarget)
  219. {
  220. CDomainAccounts aGroups;
  221. rSource.QueryGroups(m_nRecurseMaintain == 1, m_setExcludeNames, aGroups);
  222. DoGroups(aGroups, rTarget);
  223. }
  224. // DoGroups Method
  225. void CGroupMigration::DoGroups(CDomainAccounts& rGroups, CContainer& rTarget)
  226. {
  227. if (rGroups.size() > 0)
  228. {
  229. CVarSet aVarSet;
  230. SetOptions(rTarget.GetPath(), aVarSet);
  231. SetAccountOptions(aVarSet);
  232. VerifyRenameConflictPrefixSuffixValid();
  233. FillInVarSetForGroups(rGroups, aVarSet);
  234. rGroups.clear();
  235. #ifdef _DEBUG
  236. aVarSet.Dump();
  237. #endif
  238. PerformMigration(aVarSet);
  239. SaveSettings(aVarSet);
  240. if ((m_nRecurseMaintain == 2) && m_bMigrateMembers)
  241. {
  242. FixObjectsInHierarchy(_T("user"));
  243. }
  244. }
  245. }
  246. // SetOptions Method
  247. void CGroupMigration::SetOptions(_bstr_t strTargetOu, CVarSet& rVarSet)
  248. {
  249. CVarSetOptions aOptions(rVarSet);
  250. aOptions.SetTest(m_spInternal->TestMigration ? true : false);
  251. aOptions.SetUndo(false);
  252. aOptions.SetWizard(_T("group"));
  253. aOptions.SetIntraForest(m_spInternal->IntraForest ? true : false);
  254. aOptions.SetSourceDomain(m_SourceDomain.NameFlat(), m_SourceDomain.NameDns(), m_SourceDomain.Sid());
  255. aOptions.SetTargetDomain(m_TargetDomain.NameFlat(), m_TargetDomain.NameDns());
  256. aOptions.SetTargetOu(strTargetOu);
  257. if (m_bMigrateSids || (m_spInternal->PasswordOption == admtCopyPassword))
  258. {
  259. aOptions.SetTargetServer(
  260. m_TargetDomain.DomainControllerNameFlat(),
  261. m_TargetDomain.DomainControllerNameDns()
  262. );
  263. }
  264. aOptions.SetRenameOptions(m_spInternal->RenameOption, m_spInternal->RenamePrefixOrSuffix);
  265. }
  266. // SetAccountOptions Method
  267. void CGroupMigration::SetAccountOptions(CVarSet& rVarSet)
  268. {
  269. CVarSetAccountOptions aOptions(rVarSet);
  270. aOptions.SetPasswordOption(m_spInternal->PasswordOption, m_spInternal->PasswordServer);
  271. aOptions.SetPasswordFile(m_spInternal->PasswordFile);
  272. aOptions.SetConflictOptions(m_spInternal->ConflictOptions, m_spInternal->ConflictPrefixOrSuffix);
  273. aOptions.SetDisableOption(m_lDisableOption);
  274. aOptions.SetSourceExpiration(m_lSourceExpiration);
  275. aOptions.SetMigrateSids(m_bMigrateSids);
  276. aOptions.SetGroupMigrationOptions(m_bMigrateMembers, m_bUpdateMigrated);
  277. aOptions.SetFixGroupMembership(m_bFixGroupMembership);
  278. aOptions.SetUpdateUserRights(m_bUpdateGroupRights);
  279. aOptions.SetTranslateRoamingProfile(m_bTranslateRoamingProfile);
  280. if (m_bMigrateMembers)
  281. {
  282. aOptions.SetExcludedUserProps(m_spInternal->UserPropertiesToExclude);
  283. aOptions.SetExcludedInetOrgPersonProps(m_spInternal->InetOrgPersonPropertiesToExclude);
  284. }
  285. aOptions.SetExcludedGroupProps(m_spInternal->GroupPropertiesToExclude);
  286. }