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.

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