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
8.6 KiB

  1. #include "StdAfx.h"
  2. #include "ADMTScript.h"
  3. #include "SecurityTranslation.h"
  4. #include "Error.h"
  5. #include "VarSetOptions.h"
  6. #include "VarSetAccountOptions.h"
  7. #include "VarSetSecurity.h"
  8. //---------------------------------------------------------------------------
  9. // Security Translation Class
  10. //---------------------------------------------------------------------------
  11. CSecurityTranslation::CSecurityTranslation() :
  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. {
  21. }
  22. CSecurityTranslation::~CSecurityTranslation()
  23. {
  24. }
  25. // ISecurityTranslation Implementation ----------------------------------------
  26. // TranslationOption Property
  27. STDMETHODIMP CSecurityTranslation::put_TranslationOption(long lOption)
  28. {
  29. HRESULT hr = S_OK;
  30. if (IsTranslationOptionValid(lOption))
  31. {
  32. m_lTranslationOption = lOption;
  33. }
  34. else
  35. {
  36. hr = AdmtSetError(CLSID_Migration, IID_ISecurityTranslation, E_INVALIDARG, IDS_E_TRANSLATION_OPTION_INVALID);
  37. }
  38. return hr;
  39. }
  40. STDMETHODIMP CSecurityTranslation::get_TranslationOption(long* plOption)
  41. {
  42. *plOption = m_lTranslationOption;
  43. return S_OK;
  44. }
  45. // TranslateFilesAndFolders Property
  46. STDMETHODIMP CSecurityTranslation::put_TranslateFilesAndFolders(VARIANT_BOOL bTranslate)
  47. {
  48. m_bTranslateFilesAndFolders = bTranslate ? true : false;
  49. return S_OK;
  50. }
  51. STDMETHODIMP CSecurityTranslation::get_TranslateFilesAndFolders(VARIANT_BOOL* pbTranslate)
  52. {
  53. *pbTranslate = m_bTranslateFilesAndFolders ? VARIANT_TRUE : VARIANT_FALSE;
  54. return S_OK;
  55. }
  56. // TranslateLocalGroups Property
  57. STDMETHODIMP CSecurityTranslation::put_TranslateLocalGroups(VARIANT_BOOL bTranslate)
  58. {
  59. m_bTranslateLocalGroups = bTranslate ? true : false;
  60. return S_OK;
  61. }
  62. STDMETHODIMP CSecurityTranslation::get_TranslateLocalGroups(VARIANT_BOOL* pbTranslate)
  63. {
  64. *pbTranslate = m_bTranslateLocalGroups ? VARIANT_TRUE : VARIANT_FALSE;
  65. return S_OK;
  66. }
  67. // TranslatePrinters Property
  68. STDMETHODIMP CSecurityTranslation::put_TranslatePrinters(VARIANT_BOOL bTranslate)
  69. {
  70. m_bTranslatePrinters = bTranslate ? true : false;
  71. return S_OK;
  72. }
  73. STDMETHODIMP CSecurityTranslation::get_TranslatePrinters(VARIANT_BOOL* pbTranslate)
  74. {
  75. *pbTranslate = m_bTranslatePrinters ? VARIANT_TRUE : VARIANT_FALSE;
  76. return S_OK;
  77. }
  78. // TranslateRegistry Property
  79. STDMETHODIMP CSecurityTranslation::put_TranslateRegistry(VARIANT_BOOL bTranslate)
  80. {
  81. m_bTranslateRegistry = bTranslate ? true : false;
  82. return S_OK;
  83. }
  84. STDMETHODIMP CSecurityTranslation::get_TranslateRegistry(VARIANT_BOOL* pbTranslate)
  85. {
  86. *pbTranslate = m_bTranslateRegistry ? VARIANT_TRUE : VARIANT_FALSE;
  87. return S_OK;
  88. }
  89. // TranslateShares Property
  90. STDMETHODIMP CSecurityTranslation::put_TranslateShares(VARIANT_BOOL bTranslate)
  91. {
  92. m_bTranslateShares = bTranslate ? true : false;
  93. return S_OK;
  94. }
  95. STDMETHODIMP CSecurityTranslation::get_TranslateShares(VARIANT_BOOL* pbTranslate)
  96. {
  97. *pbTranslate = m_bTranslateShares ? VARIANT_TRUE : VARIANT_FALSE;
  98. return S_OK;
  99. }
  100. // TranslateUserProfiles Property
  101. STDMETHODIMP CSecurityTranslation::put_TranslateUserProfiles(VARIANT_BOOL bTranslate)
  102. {
  103. m_bTranslateUserProfiles = bTranslate ? true : false;
  104. return S_OK;
  105. }
  106. STDMETHODIMP CSecurityTranslation::get_TranslateUserProfiles(VARIANT_BOOL* pbTranslate)
  107. {
  108. *pbTranslate = m_bTranslateUserProfiles ? VARIANT_TRUE : VARIANT_FALSE;
  109. return S_OK;
  110. }
  111. // TranslateUserRights Property
  112. STDMETHODIMP CSecurityTranslation::put_TranslateUserRights(VARIANT_BOOL bTranslate)
  113. {
  114. m_bTranslateUserRights = bTranslate ? true : false;
  115. return S_OK;
  116. }
  117. STDMETHODIMP CSecurityTranslation::get_TranslateUserRights(VARIANT_BOOL* pbTranslate)
  118. {
  119. *pbTranslate = m_bTranslateUserRights ? VARIANT_TRUE : VARIANT_FALSE;
  120. return S_OK;
  121. }
  122. // SidMappingFile Property
  123. STDMETHODIMP CSecurityTranslation::put_SidMappingFile(BSTR bstrFile)
  124. {
  125. HRESULT hr = S_OK;
  126. try
  127. {
  128. _bstr_t strFile = bstrFile;
  129. if (strFile.length() > 0)
  130. {
  131. _TCHAR szPath[_MAX_PATH];
  132. LPTSTR pszFilePart;
  133. DWORD cchPath = GetFullPathName(strFile, _MAX_PATH, szPath, &pszFilePart);
  134. if ((cchPath == 0) || (cchPath >= _MAX_PATH))
  135. {
  136. AdmtThrowError(
  137. GUID_NULL,
  138. GUID_NULL,
  139. HRESULT_FROM_WIN32(GetLastError()),
  140. IDS_E_SID_MAPPING_FILE,
  141. (LPCTSTR)strFile
  142. );
  143. }
  144. HANDLE hFile = CreateFile(
  145. szPath,
  146. GENERIC_READ,
  147. FILE_SHARE_READ|FILE_SHARE_WRITE,
  148. NULL,
  149. OPEN_EXISTING,
  150. FILE_ATTRIBUTE_NORMAL,
  151. NULL
  152. );
  153. if (hFile == INVALID_HANDLE_VALUE)
  154. {
  155. AdmtThrowError(
  156. GUID_NULL,
  157. GUID_NULL,
  158. HRESULT_FROM_WIN32(GetLastError()),
  159. IDS_E_SID_MAPPING_FILE,
  160. (LPCTSTR)strFile
  161. );
  162. }
  163. CloseHandle(hFile);
  164. m_bstrSidMappingFile = szPath;
  165. }
  166. else
  167. {
  168. m_bstrSidMappingFile = strFile;
  169. }
  170. }
  171. catch (_com_error& ce)
  172. {
  173. hr = AdmtSetError(CLSID_Migration, IID_ISecurityTranslation, ce);
  174. }
  175. catch (...)
  176. {
  177. hr = AdmtSetError(CLSID_Migration, IID_ISecurityTranslation, E_FAIL);
  178. }
  179. return hr;
  180. }
  181. STDMETHODIMP CSecurityTranslation::get_SidMappingFile(BSTR* pbstrFile)
  182. {
  183. HRESULT hr = S_OK;
  184. try
  185. {
  186. *pbstrFile = m_bstrSidMappingFile.copy();
  187. }
  188. catch (_com_error& ce)
  189. {
  190. hr = AdmtSetError(CLSID_Migration, IID_ISecurityTranslation, ce);
  191. }
  192. catch (...)
  193. {
  194. hr = AdmtSetError(CLSID_Migration, IID_ISecurityTranslation, E_FAIL);
  195. }
  196. return hr;
  197. }
  198. // Translate Method
  199. STDMETHODIMP CSecurityTranslation::Translate(long lOptions, VARIANT vntInclude, VARIANT vntExclude)
  200. {
  201. HRESULT hr = S_OK;
  202. MutexWait();
  203. bool bLogOpen = _Module.OpenLog();
  204. try
  205. {
  206. _Module.Log(ErrI, IDS_STARTED_SECURITY_TRANSLATION);
  207. InitSourceDomainAndContainer();
  208. InitTargetDomainAndContainer();
  209. DoOption(lOptions, vntInclude, vntExclude);
  210. }
  211. catch (_com_error& ce)
  212. {
  213. hr = AdmtSetError(CLSID_Migration, IID_ISecurityTranslation, ce, IDS_E_CANT_TRANSLATE_SECURITY);
  214. }
  215. catch (...)
  216. {
  217. hr = AdmtSetError(CLSID_Migration, IID_ISecurityTranslation, E_FAIL, IDS_E_CANT_TRANSLATE_SECURITY);
  218. }
  219. if (bLogOpen)
  220. {
  221. _Module.CloseLog();
  222. }
  223. MutexRelease();
  224. return hr;
  225. }
  226. // Implementation -----------------------------------------------------------
  227. // DoNames Method
  228. void CSecurityTranslation::DoNames()
  229. {
  230. CDomainAccounts aComputers;
  231. m_SourceDomain.QueryComputersAcrossDomains(GetSourceContainer(), true, m_setIncludeNames, m_setExcludeNames, aComputers);
  232. DoComputers(aComputers);
  233. }
  234. // DoDomain Method
  235. void CSecurityTranslation::DoDomain()
  236. {
  237. DoContainers(GetSourceContainer());
  238. }
  239. // DoContainers Method
  240. void CSecurityTranslation::DoContainers(CContainer& rSource)
  241. {
  242. DoComputers(rSource);
  243. }
  244. // DoComputers Method
  245. void CSecurityTranslation::DoComputers(CContainer& rSource)
  246. {
  247. CDomainAccounts aComputers;
  248. rSource.QueryComputers(true, m_nRecurseMaintain >= 1, m_setExcludeNames, aComputers);
  249. DoComputers(aComputers);
  250. }
  251. // DoComputers Method
  252. void CSecurityTranslation::DoComputers(CDomainAccounts& rComputers)
  253. {
  254. if (rComputers.size() > 0)
  255. {
  256. CVarSet aVarSet;
  257. SetOptions(aVarSet);
  258. SetAccountOptions(aVarSet);
  259. SetSecurity(aVarSet);
  260. FillInVarSetForComputers(rComputers, false, false, false, 0, aVarSet);
  261. rComputers.clear();
  262. if (m_bTranslateUserProfiles)
  263. {
  264. aVarSet.Put(_T("PlugIn.%ld"), 0, _T("{0EB9FBE9-397D-4D09-A65E-ABF1790CC470}"));
  265. }
  266. else
  267. {
  268. aVarSet.Put(_T("PlugIn.%ld"), 0, _T("None"));
  269. }
  270. PerformMigration(aVarSet);
  271. SaveSettings(aVarSet);
  272. }
  273. }
  274. // SetOptions Method
  275. void CSecurityTranslation::SetOptions(CVarSet& rVarSet)
  276. {
  277. CVarSetOptions aOptions(rVarSet);
  278. aOptions.SetTest(m_spInternal->TestMigration ? true : false);
  279. aOptions.SetUndo(false);
  280. aOptions.SetWizard(_T("security"));
  281. aOptions.SetIntraForest(m_spInternal->IntraForest ? true : false);
  282. aOptions.SetSourceDomain(m_SourceDomain.NameFlat(), m_SourceDomain.NameDns());
  283. aOptions.SetTargetDomain(m_TargetDomain.NameFlat(), m_TargetDomain.NameDns());
  284. }
  285. // SetAccountOptions Method
  286. void CSecurityTranslation::SetAccountOptions(CVarSet& rVarSet)
  287. {
  288. CVarSetAccountOptions aOptions(rVarSet);
  289. aOptions.SetSecurityTranslationOptions();
  290. aOptions.SetSecurityMapFile(m_bstrSidMappingFile);
  291. }
  292. // SetSecurity Method
  293. void CSecurityTranslation::SetSecurity(CVarSet& rVarSet)
  294. {
  295. CVarSetSecurity aSecurity(rVarSet);
  296. aSecurity.SetTranslationOption(m_lTranslationOption);
  297. aSecurity.SetTranslateContainers(false);
  298. aSecurity.SetTranslateFiles(m_bTranslateFilesAndFolders);
  299. aSecurity.SetTranslateLocalGroups(m_bTranslateLocalGroups);
  300. aSecurity.SetTranslatePrinters(m_bTranslatePrinters);
  301. aSecurity.SetTranslateRegistry(m_bTranslateRegistry);
  302. aSecurity.SetTranslateShares(m_bTranslateShares);
  303. aSecurity.SetTranslateUserProfiles(m_bTranslateUserProfiles);
  304. aSecurity.SetTranslateUserRights(m_bTranslateUserRights);
  305. }