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.

1746 lines
47 KiB

  1. #include "StdAfx.h"
  2. #include "ADMTScript.h"
  3. #include "MigrationBase.h"
  4. #include <LM.h>
  5. #include <DsGetDc.h>
  6. #include <NtSecApi.h>
  7. #include <Sddl.h>
  8. #include <dsrole.h>
  9. #include "SidHistoryFlags.h"
  10. #include "VerifyConfiguration.h"
  11. #include "Error.h"
  12. #include "VarSetAccounts.h"
  13. #include "VarSetServers.h"
  14. #include "FixHierarchy.h"
  15. #include "GetDcName.h"
  16. using namespace _com_util;
  17. #ifndef STATUS_SUCCESS
  18. #define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
  19. #define STATUS_ACCESS_DENIED ((NTSTATUS)0xC0000022L)
  20. #define STATUS_OBJECT_NAME_NOT_FOUND ((NTSTATUS)0xC0000034L)
  21. #endif
  22. namespace MigrationBase
  23. {
  24. bool __stdcall IsInboundTrustDefined(PCWSTR pszDomain);
  25. bool __stdcall IsOutboundTrustDefined(PCWSTR pszDomainController, PCWSTR pszDomainSid);
  26. DWORD __stdcall GetOutboundTrustStatus(PCWSTR pszDomainController, PCWSTR pszDomain);
  27. void GetNamesFromData(VARIANT& vntData, StringSet& setNames);
  28. void GetNamesFromVariant(VARIANT* pvnt, StringSet& setNames);
  29. void GetNamesFromString(BSTR bstr, StringSet& setNames);
  30. void GetNamesFromStringArray(SAFEARRAY* psa, StringSet& setNames);
  31. void GetNamesFromVariantArray(SAFEARRAY* psa, StringSet& setNames);
  32. void GetNamesFromFile(VARIANT& vntData, StringSet& setNames);
  33. void GetNamesFromFile(LPCTSTR pszFileName, StringSet& setNames);
  34. void GetNamesFromStringA(LPCSTR pchString, DWORD cchString, StringSet& setNames);
  35. void GetNamesFromStringW(LPCWSTR pchString, DWORD cchString, StringSet& setNames);
  36. _bstr_t RemoveTrailingDollarSign(LPCTSTR pszName);
  37. void __stdcall AdmtCheckError(HRESULT hr)
  38. {
  39. if (FAILED(hr))
  40. {
  41. IErrorInfo* pErrorInfo = NULL;
  42. if (GetErrorInfo(0, &pErrorInfo) == S_OK)
  43. {
  44. _com_raise_error(hr, pErrorInfo);
  45. }
  46. else
  47. {
  48. AdmtThrowError(GUID_NULL, GUID_NULL, hr);
  49. }
  50. }
  51. }
  52. }
  53. using namespace MigrationBase;
  54. //---------------------------------------------------------------------------
  55. // MigrationBase Class
  56. //---------------------------------------------------------------------------
  57. // Constructor
  58. CMigrationBase::CMigrationBase() :
  59. m_nRecurseMaintain(0),
  60. m_Mutex(ADMT_MUTEX)
  61. {
  62. }
  63. // Destructor
  64. CMigrationBase::~CMigrationBase()
  65. {
  66. }
  67. // InitSourceDomainAndContainer Method
  68. void CMigrationBase::InitSourceDomainAndContainer(bool bMustExist)
  69. {
  70. try
  71. {
  72. m_SourceDomain.Initialize(m_spInternal->SourceDomain);
  73. m_SourceContainer = m_SourceDomain.GetContainer(m_spInternal->SourceOu);
  74. }
  75. catch (_com_error& ce)
  76. {
  77. //
  78. // if the domain must exist then throw error
  79. // otherwise at least the domain name must be specified
  80. //
  81. if (bMustExist || (ce.Error() == E_INVALIDARG))
  82. {
  83. throw;
  84. }
  85. }
  86. }
  87. // InitTargetDomainAndContainer Method
  88. void CMigrationBase::InitTargetDomainAndContainer()
  89. {
  90. m_TargetDomain.Initialize(m_spInternal->TargetDomain);
  91. m_TargetContainer = m_TargetDomain.GetContainer(m_spInternal->TargetOu);
  92. // verify target domain is in native mode
  93. if (m_TargetDomain.NativeMode() == false)
  94. {
  95. AdmtThrowError(
  96. GUID_NULL, GUID_NULL,
  97. E_INVALIDARG, IDS_E_TARGET_DOMAIN_NOT_NATIVE_MODE,
  98. (LPCTSTR)m_TargetDomain.Name()
  99. );
  100. }
  101. VerifyTargetContainerPathLength();
  102. }
  103. // VerifyInterIntraForest Method
  104. void CMigrationBase::VerifyInterIntraForest()
  105. {
  106. // if the source and target domains have the same forest name then they are intra-forest
  107. bool bIntraForest = m_spInternal->IntraForest ? true : false;
  108. if (m_SourceDomain.ForestName() == m_TargetDomain.ForestName())
  109. {
  110. // intra-forest must be set to true to match the domains
  111. if (!bIntraForest)
  112. {
  113. AdmtThrowError(
  114. GUID_NULL, GUID_NULL,
  115. E_INVALIDARG, IDS_E_NOT_INTER_FOREST,
  116. (LPCTSTR)m_SourceDomain.Name(), (LPCTSTR)m_TargetDomain.Name()
  117. );
  118. }
  119. }
  120. else
  121. {
  122. // intra-forest must be set to false to match the domains
  123. if (bIntraForest)
  124. {
  125. AdmtThrowError(
  126. GUID_NULL, GUID_NULL,
  127. E_INVALIDARG, IDS_E_NOT_INTRA_FOREST,
  128. (LPCTSTR)m_SourceDomain.Name(), (LPCTSTR)m_TargetDomain.Name()
  129. );
  130. }
  131. }
  132. }
  133. //-----------------------------------------------------------------------------
  134. // VerifyCallerDelegated Method
  135. //
  136. // Synopsis
  137. // If an intra-forest move operation is being performed then verify that the
  138. // calling user's account has not been marked as sensitive and therefore
  139. // cannot be delegated. As the move operation is performed on the domain
  140. // controller which has the RID master role in the source domain it is
  141. // necessary to delegate the user's security context.
  142. //
  143. // Note that a failure to verify whether the caller's account is marked
  144. // sensitive or whether we are running on the source domain controller
  145. // holding the RID master role will not generate an error.
  146. //
  147. // Arguments
  148. // None
  149. //
  150. // Return Value
  151. // None. An exception with rich error information is thrown if the caller's
  152. // account is marked as sensitive.
  153. //-----------------------------------------------------------------------------
  154. void CMigrationBase::VerifyCallerDelegated()
  155. {
  156. //
  157. // It is only necessary to check this for intra-forest.
  158. //
  159. bool bIntraForest = m_spInternal->IntraForest ? true : false;
  160. if (bIntraForest)
  161. {
  162. bool bDelegatable = false;
  163. HRESULT hr = IsCallerDelegatable(bDelegatable);
  164. if (SUCCEEDED(hr))
  165. {
  166. if (bDelegatable == false)
  167. {
  168. //
  169. // Caller's account is not delegatable. Retrieve name of domain controller
  170. // in the source domain that holds the RID master role and the name of this
  171. // computer.
  172. //
  173. _bstr_t strDnsName;
  174. _bstr_t strFlatName;
  175. hr = GetRidPoolAllocator4(m_SourceDomain.Name(), strDnsName, strFlatName);
  176. if (SUCCEEDED(hr))
  177. {
  178. _TCHAR szComputerName[MAX_PATH];
  179. DWORD cchComputerName = sizeof(szComputerName) / sizeof(szComputerName[0]);
  180. if (GetComputerNameEx(ComputerNameDnsFullyQualified, szComputerName, &cchComputerName))
  181. {
  182. //
  183. // If this computer is not the domain controller holding the
  184. // RID master role in the source domain then generate error.
  185. //
  186. if (_tcsicmp(szComputerName, strDnsName) != 0)
  187. {
  188. AdmtThrowError(GUID_NULL, GUID_NULL, E_FAIL, IDS_E_CALLER_NOT_DELEGATED);
  189. }
  190. }
  191. else
  192. {
  193. DWORD dwError = GetLastError();
  194. hr = HRESULT_FROM_WIN32(dwError);
  195. }
  196. }
  197. }
  198. }
  199. if (FAILED(hr))
  200. {
  201. _Module.Log(ErrW, IDS_E_UNABLE_VERIFY_CALLER_NOT_DELEGATED, _com_error(hr));
  202. }
  203. }
  204. }
  205. //-----------------------------------------------------------------------------
  206. // SetDefaultExcludedSystemProperties
  207. //
  208. // Synopsis
  209. // Sets the default system property exclusion list if the list has not already
  210. // been set. Note that the default system property exclusion list consists of
  211. // the mail, proxyAddresses and all attributes not marked as being part of the
  212. // base schema.
  213. //
  214. // Arguments
  215. // None
  216. //
  217. // Return Value
  218. // None - generate warning message in log if a failure occurs.
  219. //-----------------------------------------------------------------------------
  220. void CMigrationBase::SetDefaultExcludedSystemProperties()
  221. {
  222. try
  223. {
  224. //
  225. // Only perform if inter-forest migration and
  226. // system properties exclusion set value is zero.
  227. //
  228. if (m_spInternal->IntraForest == VARIANT_FALSE)
  229. {
  230. IIManageDBPtr spIManageDB(__uuidof(IManageDB));
  231. IVarSetPtr spSettings(__uuidof(VarSet));
  232. IUnknownPtr spUnknown(spSettings);
  233. IUnknown* punk = spUnknown;
  234. spIManageDB->GetSettings(&punk);
  235. long lSet = spSettings->get(GET_BSTR(DCTVS_AccountOptions_ExcludedSystemPropsSet));
  236. if (lSet == 0)
  237. {
  238. IObjPropBuilderPtr spObjPropBuilder(__uuidof(ObjPropBuilder));
  239. _bstr_t strNonBaseProperties = spObjPropBuilder->GetNonBaseProperties(m_TargetDomain.Name());
  240. _bstr_t strProperties = _T("mail,proxyAddresses,") + strNonBaseProperties;
  241. spSettings->put(GET_BSTR(DCTVS_AccountOptions_ExcludedSystemProps), strProperties);
  242. spIManageDB->SaveSettings(punk);
  243. }
  244. }
  245. }
  246. catch (_com_error& ce)
  247. {
  248. _Module.Log(ErrW, IDS_E_UNABLE_SET_EXCLUDED_SYSTEM_PROPERTIES, ce);
  249. }
  250. }
  251. // DoOption Method
  252. void CMigrationBase::DoOption(long lOptions, VARIANT& vntInclude, VARIANT& vntExclude)
  253. {
  254. m_setIncludeNames.clear();
  255. m_setExcludeNames.clear();
  256. InitRecurseMaintainOption(lOptions);
  257. GetExcludeNames(vntExclude, m_setExcludeNames);
  258. switch (lOptions & 0xFF)
  259. {
  260. case admtNone:
  261. {
  262. DoNone();
  263. break;
  264. }
  265. case admtData:
  266. {
  267. GetNamesFromData(vntInclude, m_setIncludeNames);
  268. DoNames();
  269. break;
  270. }
  271. case admtFile:
  272. {
  273. GetNamesFromFile(vntInclude, m_setIncludeNames);
  274. DoNames();
  275. break;
  276. }
  277. case admtDomain:
  278. {
  279. m_setIncludeNames.clear();
  280. DoDomain();
  281. break;
  282. }
  283. default:
  284. {
  285. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, IDS_E_INVALID_OPTION);
  286. break;
  287. }
  288. }
  289. }
  290. // DoNone Method
  291. void CMigrationBase::DoNone()
  292. {
  293. }
  294. // DoNames Method
  295. void CMigrationBase::DoNames()
  296. {
  297. }
  298. // DoDomain Method
  299. void CMigrationBase::DoDomain()
  300. {
  301. }
  302. // InitRecurseMaintainOption Method
  303. void CMigrationBase::InitRecurseMaintainOption(long lOptions)
  304. {
  305. switch (lOptions & 0xFF)
  306. {
  307. case admtData:
  308. case admtFile:
  309. {
  310. if (lOptions & 0xFF00)
  311. {
  312. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, IDS_E_DATA_OPTION_FLAGS_NOT_ALLOWED);
  313. }
  314. m_nRecurseMaintain = 0;
  315. break;
  316. }
  317. case admtDomain:
  318. {
  319. m_nRecurseMaintain = 0;
  320. if (lOptions & admtRecurse)
  321. {
  322. ++m_nRecurseMaintain;
  323. if (lOptions & admtMaintainHierarchy)
  324. {
  325. ++m_nRecurseMaintain;
  326. }
  327. }
  328. break;
  329. }
  330. default:
  331. {
  332. m_nRecurseMaintain = 0;
  333. break;
  334. }
  335. }
  336. }
  337. // GetExcludeNames Method
  338. void CMigrationBase::GetExcludeNames(VARIANT& vntExclude, StringSet& setExcludeNames)
  339. {
  340. try
  341. {
  342. switch (V_VT(&vntExclude))
  343. {
  344. case VT_EMPTY:
  345. case VT_ERROR:
  346. {
  347. setExcludeNames.clear();
  348. break;
  349. }
  350. case VT_BSTR:
  351. {
  352. GetNamesFromFile(V_BSTR(&vntExclude), setExcludeNames);
  353. break;
  354. }
  355. case VT_BSTR|VT_BYREF:
  356. {
  357. BSTR* pbstr = V_BSTRREF(&vntExclude);
  358. if (pbstr)
  359. {
  360. GetNamesFromFile(*pbstr, setExcludeNames);
  361. }
  362. break;
  363. }
  364. case VT_BSTR|VT_ARRAY:
  365. {
  366. GetNamesFromStringArray(V_ARRAY(&vntExclude), setExcludeNames);
  367. break;
  368. }
  369. case VT_BSTR|VT_ARRAY|VT_BYREF:
  370. {
  371. SAFEARRAY** ppsa = V_ARRAYREF(&vntExclude);
  372. if (ppsa)
  373. {
  374. GetNamesFromStringArray(*ppsa, setExcludeNames);
  375. }
  376. break;
  377. }
  378. case VT_VARIANT|VT_BYREF:
  379. {
  380. VARIANT* pvnt = V_VARIANTREF(&vntExclude);
  381. if (pvnt)
  382. {
  383. GetExcludeNames(*pvnt, setExcludeNames);
  384. }
  385. break;
  386. }
  387. case VT_VARIANT|VT_ARRAY:
  388. {
  389. GetNamesFromVariantArray(V_ARRAY(&vntExclude), setExcludeNames);
  390. break;
  391. }
  392. case VT_VARIANT|VT_ARRAY|VT_BYREF:
  393. {
  394. SAFEARRAY** ppsa = V_ARRAYREF(&vntExclude);
  395. if (ppsa)
  396. {
  397. GetNamesFromVariantArray(*ppsa, setExcludeNames);
  398. }
  399. break;
  400. }
  401. default:
  402. {
  403. _com_issue_error(E_INVALIDARG);
  404. break;
  405. }
  406. }
  407. }
  408. catch (_com_error& ce)
  409. {
  410. AdmtThrowError(GUID_NULL, GUID_NULL, ce.Error(), IDS_E_INVALID_EXCLUDE_DATA_TYPE);
  411. }
  412. catch (...)
  413. {
  414. AdmtThrowError(GUID_NULL, GUID_NULL, E_FAIL, IDS_E_INVALID_EXCLUDE_DATA_TYPE);
  415. }
  416. }
  417. // FillInVarSetForUsers Method
  418. void CMigrationBase::FillInVarSetForUsers(CDomainAccounts& rUsers, CVarSet& rVarSet)
  419. {
  420. CVarSetAccounts aAccounts(rVarSet);
  421. for (CDomainAccounts::iterator it = rUsers.begin(); it != rUsers.end(); it++)
  422. {
  423. aAccounts.AddAccount(_T("User"), it->GetADsPath(), it->GetName(), it->GetUserPrincipalName());
  424. }
  425. }
  426. // FillInVarSetForGroups Method
  427. void CMigrationBase::FillInVarSetForGroups(CDomainAccounts& rGroups, CVarSet& rVarSet)
  428. {
  429. CVarSetAccounts aAccounts(rVarSet);
  430. for (CDomainAccounts::iterator it = rGroups.begin(); it != rGroups.end(); it++)
  431. {
  432. aAccounts.AddAccount(_T("Group"), it->GetADsPath(), it->GetName());
  433. }
  434. }
  435. // FillInVarSetForComputers Method
  436. void CMigrationBase::FillInVarSetForComputers(CDomainAccounts& rComputers, bool bMigrateOnly, bool bMoveToTarget, bool bReboot, long lRebootDelay, CVarSet& rVarSet)
  437. {
  438. CVarSetAccounts aAccounts(rVarSet);
  439. CVarSetServers aServers(rVarSet);
  440. for (CDomainAccounts::iterator it = rComputers.begin(); it != rComputers.end(); it++)
  441. {
  442. // remove trailing '$'
  443. // ADMT doesn't accept true SAM account name
  444. _bstr_t strName = RemoveTrailingDollarSign(it->GetSamAccountName());
  445. aAccounts.AddAccount(_T("Computer"), strName);
  446. aServers.AddServer(strName, it->GetDnsHostName(), bMigrateOnly, bMoveToTarget, bReboot, lRebootDelay);
  447. }
  448. }
  449. // VerifyRenameConflictPrefixSuffixValid Method
  450. void CMigrationBase::VerifyRenameConflictPrefixSuffixValid()
  451. {
  452. int nTotalPrefixSuffixLength = 0;
  453. long lRenameOption = m_spInternal->RenameOption;
  454. if ((lRenameOption == admtRenameWithPrefix) || (lRenameOption == admtRenameWithSuffix))
  455. {
  456. _bstr_t strPrefixSuffix = m_spInternal->RenamePrefixOrSuffix;
  457. nTotalPrefixSuffixLength += strPrefixSuffix.length();
  458. }
  459. long lConflictOption = m_spInternal->ConflictOptions & 0x0F;
  460. if ((lConflictOption == admtRenameConflictingWithSuffix) || (lConflictOption == admtRenameConflictingWithPrefix))
  461. {
  462. _bstr_t strPrefixSuffix = m_spInternal->ConflictPrefixOrSuffix;
  463. nTotalPrefixSuffixLength += strPrefixSuffix.length();
  464. }
  465. if (nTotalPrefixSuffixLength > MAXIMUM_PREFIX_SUFFIX_LENGTH)
  466. {
  467. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, IDS_E_PREFIX_SUFFIX_TOO_LONG, MAXIMUM_PREFIX_SUFFIX_LENGTH);
  468. }
  469. }
  470. // VerifyCanAddSidHistory Method
  471. void CMigrationBase::VerifyCanAddSidHistory()
  472. {
  473. bool bMessageDefined = false;
  474. try
  475. {
  476. long lErrorFlags = 0;
  477. IAccessCheckerPtr spAccessChecker(__uuidof(AccessChecker));
  478. spAccessChecker->CanUseAddSidHistory(
  479. m_SourceDomain.Name(),
  480. m_TargetDomain.Name(),
  481. m_TargetDomain.DomainControllerName(),
  482. &lErrorFlags
  483. );
  484. if (lErrorFlags != 0)
  485. {
  486. _bstr_t strError;
  487. CComBSTR str;
  488. if (lErrorFlags & F_NO_AUDITING_SOURCE)
  489. {
  490. str.LoadString(IDS_E_NO_AUDITING_SOURCE);
  491. strError += str.operator BSTR();
  492. }
  493. if (lErrorFlags & F_NO_AUDITING_TARGET)
  494. {
  495. str.LoadString(IDS_E_NO_AUDITING_TARGET);
  496. strError += str.operator BSTR();
  497. }
  498. if (lErrorFlags & F_NO_LOCAL_GROUP)
  499. {
  500. str.LoadString(IDS_E_NO_SID_HISTORY_LOCAL_GROUP);
  501. strError += str.operator BSTR();
  502. }
  503. if (lErrorFlags & F_NO_REG_KEY)
  504. {
  505. str.LoadString(IDS_E_NO_SID_HISTORY_REGISTRY_ENTRY);
  506. strError += str.operator BSTR();
  507. }
  508. if (lErrorFlags & F_NOT_DOMAIN_ADMIN)
  509. {
  510. str.LoadString(IDS_E_NO_SID_HISTORY_DOMAIN_ADMIN);
  511. strError += str.operator BSTR();
  512. }
  513. bMessageDefined = true;
  514. AdmtThrowError(GUID_NULL, GUID_NULL, E_FAIL, IDS_E_SID_HISTORY_CONFIGURATION, (LPCTSTR)strError);
  515. }
  516. //
  517. // If adding SID history from a downlevel (Windows NT 4 or earlier) domain and not using
  518. // explicit credentials then DsAddSidHistory requires that the call be made on a domain
  519. // controller in the target domain and that the source domain trusts the target domain.
  520. //
  521. // No credentials are supplied only when using scripting or the command-line therefore
  522. // this check is only performed here.
  523. //
  524. if (m_SourceDomain.UpLevel() == false)
  525. {
  526. //
  527. // The source domain is downlevel.
  528. //
  529. //
  530. // Verify that this computer is in the target domain.
  531. //
  532. CADsADSystemInfo siSystemInfo;
  533. _bstr_t strThisDomain = siSystemInfo.GetDomainDNSName();
  534. _bstr_t strTargetDomain = m_TargetDomain.NameDns();
  535. if (!strThisDomain || !strTargetDomain)
  536. {
  537. _com_issue_error(E_OUTOFMEMORY);
  538. }
  539. if (_wcsicmp((PCWSTR)strThisDomain, (PCWSTR)strTargetDomain) != 0)
  540. {
  541. bMessageDefined = true;
  542. AdmtThrowError(GUID_NULL, GUID_NULL, HRESULT_FROM_WIN32(ERROR_DS_MUST_BE_RUN_ON_DST_DC), IDS_E_SID_HISTORY_MUST_RUN_ON_DOMAIN_CONTROLLER);
  543. }
  544. //
  545. // Verify that this computer is a domain controller.
  546. //
  547. PSERVER_INFO_101 psiInfo = NULL;
  548. NET_API_STATUS nasStatus = NetServerGetInfo(NULL, 101, (LPBYTE*)&psiInfo);
  549. if (nasStatus != ERROR_SUCCESS)
  550. {
  551. _com_issue_error(HRESULT_FROM_WIN32(nasStatus));
  552. }
  553. bool bIsDC = (psiInfo->sv101_type & (SV_TYPE_DOMAIN_CTRL|SV_TYPE_DOMAIN_BAKCTRL)) != 0;
  554. NetApiBufferFree(psiInfo);
  555. if (!bIsDC)
  556. {
  557. bMessageDefined = true;
  558. AdmtThrowError(GUID_NULL, GUID_NULL, HRESULT_FROM_WIN32(ERROR_DS_MUST_BE_RUN_ON_DST_DC), IDS_E_SID_HISTORY_MUST_RUN_ON_DOMAIN_CONTROLLER);
  559. }
  560. //
  561. // Verify trusted domain object exists in target domain
  562. // for source domain and that an inbound trust is defined.
  563. //
  564. if (IsInboundTrustDefined(m_SourceDomain.NameFlat()) == false)
  565. {
  566. bMessageDefined = true;
  567. AdmtThrowError(GUID_NULL, GUID_NULL, HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), IDS_E_SID_HISTORY_SOURCE_MUST_TRUST_TARGET);
  568. }
  569. //
  570. // Verify trusted domain object exists in source domain for
  571. // target domain which specifies an outbound trust.
  572. //
  573. if (IsOutboundTrustDefined(m_SourceDomain.DomainControllerName(), m_TargetDomain.Sid()) == false)
  574. {
  575. bMessageDefined = true;
  576. AdmtThrowError(GUID_NULL, GUID_NULL, HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), IDS_E_SID_HISTORY_SOURCE_MUST_TRUST_TARGET);
  577. }
  578. //
  579. // Check outbound trust status on source domain controller.
  580. //
  581. DWORD dwError = GetOutboundTrustStatus(m_SourceDomain.DomainControllerName(), m_TargetDomain.NameFlat());
  582. if (dwError != ERROR_SUCCESS)
  583. {
  584. bMessageDefined = true;
  585. AdmtThrowError(GUID_NULL, GUID_NULL, HRESULT_FROM_WIN32(dwError), IDS_E_SID_HISTORY_SOURCE_MUST_TRUST_TARGET);
  586. }
  587. }
  588. }
  589. catch (_com_error& ce)
  590. {
  591. if (bMessageDefined)
  592. {
  593. throw;
  594. }
  595. else
  596. {
  597. AdmtThrowError(GUID_NULL, GUID_NULL, ce, IDS_E_CAN_ADD_SID_HISTORY);
  598. }
  599. }
  600. catch (...)
  601. {
  602. AdmtThrowError(GUID_NULL, GUID_NULL, E_FAIL, IDS_E_CAN_ADD_SID_HISTORY);
  603. }
  604. }
  605. // VerifyTargetContainerPathLength Method
  606. void CMigrationBase::VerifyTargetContainerPathLength()
  607. {
  608. _bstr_t strPath = GetTargetContainer().GetPath();
  609. if (strPath.length() > 999)
  610. {
  611. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, IDS_E_TARGET_CONTAINER_PATH_TOO_LONG);
  612. }
  613. }
  614. // VerifyPasswordServer Method
  615. void CMigrationBase::VerifyPasswordOption()
  616. {
  617. if (m_spInternal->PasswordOption == admtCopyPassword)
  618. {
  619. _bstr_t strServer = m_spInternal->PasswordServer;
  620. // a password server must be specified for copy password option
  621. if (strServer.length() == 0)
  622. {
  623. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, IDS_E_PASSWORD_DC_NOT_SPECIFIED);
  624. }
  625. //
  626. // verify that password server exists and is a domain controller
  627. //
  628. _bstr_t strPrefixedServer;
  629. _bstr_t strUnprefixedServer;
  630. if (_tcsncmp(strServer, _T("\\\\"), 2) == 0)
  631. {
  632. strPrefixedServer = strServer;
  633. strUnprefixedServer = &(((const wchar_t*)strServer)[2]);
  634. }
  635. else
  636. {
  637. strPrefixedServer = _T("\\\\") + strServer;
  638. strUnprefixedServer = strServer;
  639. }
  640. PSERVER_INFO_101 psiInfo;
  641. NET_API_STATUS nasStatus = NetServerGetInfo(strPrefixedServer, 101, (LPBYTE*)&psiInfo);
  642. if (nasStatus != NERR_Success)
  643. {
  644. AdmtThrowError(GUID_NULL, GUID_NULL, HRESULT_FROM_WIN32(nasStatus), IDS_E_PASSWORD_DC_NOT_FOUND, (LPCTSTR)strServer);
  645. }
  646. UINT uMsgId = 0;
  647. if (psiInfo->sv101_platform_id != PLATFORM_ID_NT)
  648. {
  649. uMsgId = IDS_E_PASSWORD_DC_NOT_NT;
  650. }
  651. else if (!(psiInfo->sv101_type & SV_TYPE_DOMAIN_CTRL) && !(psiInfo->sv101_type & SV_TYPE_DOMAIN_BAKCTRL))
  652. {
  653. uMsgId = IDS_E_PASSWORD_DC_NOT_DC;
  654. }
  655. NetApiBufferFree(psiInfo);
  656. if (uMsgId)
  657. {
  658. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, uMsgId, (LPCTSTR)strServer);
  659. }
  660. //
  661. // Verify that the password server is in fact a domain controller for
  662. // the source domain.
  663. //
  664. DSROLE_PRIMARY_DOMAIN_INFO_BASIC * pDomInfo = NULL;
  665. DWORD err = DsRoleGetPrimaryDomainInformation(strUnprefixedServer,
  666. DsRolePrimaryDomainInfoBasic,
  667. (PBYTE*)&pDomInfo);
  668. if (err != NO_ERROR)
  669. {
  670. AdmtThrowError(GUID_NULL, GUID_NULL, HRESULT_FROM_WIN32(err), IDS_E_PASSWORD_DC_NOT_FOUND, (LPCTSTR)strServer);
  671. }
  672. // compare them
  673. if ( ( (pDomInfo->DomainNameFlat != NULL) &&
  674. ((const wchar_t*)m_SourceDomain.NameFlat() != NULL) &&
  675. (_wcsicmp(pDomInfo->DomainNameFlat, (const wchar_t*)m_SourceDomain.NameFlat())==0) ) ||
  676. ( (pDomInfo->DomainNameDns != NULL) &&
  677. ((const wchar_t*)m_SourceDomain.NameDns() != NULL) &&
  678. (_wcsicmp(pDomInfo->DomainNameDns, (const wchar_t*)m_SourceDomain.NameDns())==0) ) )
  679. {
  680. // at least one of them matches
  681. uMsgId = 0;
  682. }
  683. else
  684. {
  685. // no match
  686. uMsgId = IDS_E_PASSWORD_DC_WRONG_DOMAIN;
  687. }
  688. DsRoleFreeMemory(pDomInfo);
  689. if (uMsgId)
  690. {
  691. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, uMsgId, (LPCTSTR)strServer);
  692. }
  693. //
  694. // verify that password server is configured properly
  695. //
  696. IPasswordMigrationPtr spPasswordMigration(__uuidof(PasswordMigration));
  697. spPasswordMigration->EstablishSession(strPrefixedServer, m_TargetDomain.DomainControllerName());
  698. }
  699. }
  700. // PerformMigration Method
  701. void CMigrationBase::PerformMigration(CVarSet& rVarSet)
  702. {
  703. IPerformMigrationTaskPtr spMigrator(__uuidof(Migrator));
  704. try
  705. {
  706. AdmtCheckError(spMigrator->raw_PerformMigrationTask(IUnknownPtr(rVarSet.GetInterface()), 0));
  707. }
  708. catch (_com_error& ce)
  709. {
  710. if (ce.Error() == MIGRATOR_E_PROCESSES_STILL_RUNNING)
  711. {
  712. AdmtThrowError(GUID_NULL, GUID_NULL, ce.Error(), IDS_E_ADMT_PROCESS_RUNNING);
  713. }
  714. else
  715. {
  716. throw;
  717. }
  718. }
  719. }
  720. // FixObjectsInHierarchy Method
  721. void CMigrationBase::FixObjectsInHierarchy(LPCTSTR pszType)
  722. {
  723. CFixObjectsInHierarchy fix;
  724. fix.SetObjectType(pszType);
  725. fix.SetIntraForest(m_spInternal->IntraForest ? true : false);
  726. long lOptions = m_spInternal->ConflictOptions;
  727. long lOption = lOptions & 0x0F;
  728. long lFlags = lOptions & 0xF0;
  729. fix.SetFixReplaced((lOption == admtReplaceConflicting) && (lFlags & admtMoveReplacedAccounts));
  730. fix.SetSourceContainerPath(m_SourceContainer.GetPath());
  731. fix.SetTargetContainerPath(m_TargetContainer.GetPath());
  732. fix.FixObjects();
  733. }
  734. //---------------------------------------------------------------------------
  735. namespace MigrationBase
  736. {
  737. // GetNamesFromData Method
  738. void GetNamesFromData(VARIANT& vntData, StringSet& setNames)
  739. {
  740. try
  741. {
  742. GetNamesFromVariant(&vntData, setNames);
  743. }
  744. catch (_com_error& ce)
  745. {
  746. AdmtThrowError(GUID_NULL, GUID_NULL, ce.Error(), IDS_E_INVALID_DATA_OPTION_DATA_TYPE);
  747. }
  748. catch (...)
  749. {
  750. AdmtThrowError(GUID_NULL, GUID_NULL, E_FAIL, IDS_E_INVALID_DATA_OPTION_DATA_TYPE);
  751. }
  752. }
  753. // GetNamesFromVariant Method
  754. void GetNamesFromVariant(VARIANT* pvntData, StringSet& setNames)
  755. {
  756. switch (V_VT(pvntData))
  757. {
  758. case VT_BSTR:
  759. {
  760. GetNamesFromString(V_BSTR(pvntData), setNames);
  761. break;
  762. }
  763. case VT_BSTR|VT_BYREF:
  764. {
  765. BSTR* pbstr = V_BSTRREF(pvntData);
  766. if (pbstr)
  767. {
  768. GetNamesFromString(*pbstr, setNames);
  769. }
  770. break;
  771. }
  772. case VT_BSTR|VT_ARRAY:
  773. {
  774. GetNamesFromStringArray(V_ARRAY(pvntData), setNames);
  775. break;
  776. }
  777. case VT_BSTR|VT_ARRAY|VT_BYREF:
  778. {
  779. SAFEARRAY** ppsa = V_ARRAYREF(pvntData);
  780. if (ppsa)
  781. {
  782. GetNamesFromStringArray(*ppsa, setNames);
  783. }
  784. break;
  785. }
  786. case VT_VARIANT|VT_BYREF:
  787. {
  788. VARIANT* pvnt = V_VARIANTREF(pvntData);
  789. if (pvnt)
  790. {
  791. GetNamesFromVariant(pvnt, setNames);
  792. }
  793. break;
  794. }
  795. case VT_VARIANT|VT_ARRAY:
  796. {
  797. GetNamesFromVariantArray(V_ARRAY(pvntData), setNames);
  798. break;
  799. }
  800. case VT_VARIANT|VT_ARRAY|VT_BYREF:
  801. {
  802. SAFEARRAY** ppsa = V_ARRAYREF(pvntData);
  803. if (ppsa)
  804. {
  805. GetNamesFromVariantArray(*ppsa, setNames);
  806. }
  807. break;
  808. }
  809. case VT_EMPTY:
  810. {
  811. // ignore empty variants
  812. break;
  813. }
  814. default:
  815. {
  816. _com_issue_error(E_INVALIDARG);
  817. break;
  818. }
  819. }
  820. }
  821. // GetNamesFromString Method
  822. void GetNamesFromString(BSTR bstr, StringSet& setNames)
  823. {
  824. if (bstr)
  825. {
  826. UINT cch = SysStringLen(bstr);
  827. if (cch > 0)
  828. {
  829. GetNamesFromStringW(bstr, cch, setNames);
  830. }
  831. }
  832. }
  833. // GetNamesFromStringArray Method
  834. void GetNamesFromStringArray(SAFEARRAY* psa, StringSet& setNames)
  835. {
  836. BSTR* pbstr;
  837. HRESULT hr = SafeArrayAccessData(psa, (void**)&pbstr);
  838. if (SUCCEEDED(hr))
  839. {
  840. try
  841. {
  842. UINT uDimensionCount = psa->cDims;
  843. for (UINT uDimension = 0; uDimension < uDimensionCount; uDimension++)
  844. {
  845. UINT uElementCount = psa->rgsabound[uDimension].cElements;
  846. for (UINT uElement = 0; uElement < uElementCount; uElement++)
  847. {
  848. setNames.insert(_bstr_t(*pbstr++));
  849. }
  850. }
  851. SafeArrayUnaccessData(psa);
  852. }
  853. catch (...)
  854. {
  855. SafeArrayUnaccessData(psa);
  856. throw;
  857. }
  858. }
  859. }
  860. // GetNamesFromVariantArray Method
  861. void GetNamesFromVariantArray(SAFEARRAY* psa, StringSet& setNames)
  862. {
  863. VARIANT* pvnt;
  864. HRESULT hr = SafeArrayAccessData(psa, (void**)&pvnt);
  865. if (SUCCEEDED(hr))
  866. {
  867. try
  868. {
  869. UINT uDimensionCount = psa->cDims;
  870. for (UINT uDimension = 0; uDimension < uDimensionCount; uDimension++)
  871. {
  872. UINT uElementCount = psa->rgsabound[uDimension].cElements;
  873. for (UINT uElement = 0; uElement < uElementCount; uElement++)
  874. {
  875. GetNamesFromVariant(pvnt++, setNames);
  876. }
  877. }
  878. SafeArrayUnaccessData(psa);
  879. }
  880. catch (...)
  881. {
  882. SafeArrayUnaccessData(psa);
  883. throw;
  884. }
  885. }
  886. }
  887. // GetNamesFromFile Method
  888. //
  889. // - the maximum file size this implementation can handle is 4,294,967,295 bytes
  890. void GetNamesFromFile(VARIANT& vntData, StringSet& setNames)
  891. {
  892. bool bInvalidArg = false;
  893. switch (V_VT(&vntData))
  894. {
  895. case VT_BSTR:
  896. {
  897. BSTR bstr = V_BSTR(&vntData);
  898. if (bstr)
  899. {
  900. GetNamesFromFile(bstr, setNames);
  901. }
  902. else
  903. {
  904. bInvalidArg = true;
  905. }
  906. break;
  907. }
  908. case VT_BSTR|VT_BYREF:
  909. {
  910. BSTR* pbstr = V_BSTRREF(&vntData);
  911. if (pbstr && *pbstr)
  912. {
  913. GetNamesFromFile(*pbstr, setNames);
  914. }
  915. else
  916. {
  917. bInvalidArg = true;
  918. }
  919. break;
  920. }
  921. case VT_VARIANT|VT_BYREF:
  922. {
  923. VARIANT* pvnt = V_VARIANTREF(&vntData);
  924. if (pvnt)
  925. {
  926. GetNamesFromFile(*pvnt, setNames);
  927. }
  928. else
  929. {
  930. bInvalidArg = true;
  931. }
  932. break;
  933. }
  934. default:
  935. {
  936. bInvalidArg = true;
  937. break;
  938. }
  939. }
  940. if (bInvalidArg)
  941. {
  942. AdmtThrowError(GUID_NULL, GUID_NULL, E_INVALIDARG, IDS_E_INVALID_FILE_OPTION_DATA_TYPE);
  943. }
  944. }
  945. // GetNamesFromFile Method
  946. //
  947. // - the maximum file size this implementation can handle is 4,294,967,295 bytes
  948. void GetNamesFromFile(LPCTSTR pszFileName, StringSet& setNames)
  949. {
  950. HRESULT hr = S_OK;
  951. if (pszFileName)
  952. {
  953. HANDLE hFile = CreateFile(pszFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
  954. if (hFile != INVALID_HANDLE_VALUE)
  955. {
  956. DWORD dwFileSize = GetFileSize(hFile, NULL);
  957. if (dwFileSize > 0)
  958. {
  959. HANDLE hFileMappingObject = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  960. if (hFileMappingObject != NULL)
  961. {
  962. LPVOID pvBase = MapViewOfFile(hFileMappingObject, FILE_MAP_READ, 0, 0, 0);
  963. if (pvBase != NULL)
  964. {
  965. // if Unicode signature assume Unicode file
  966. // otherwise it must be an ANSI file
  967. LPCWSTR pwcs = (LPCWSTR)pvBase;
  968. if ((dwFileSize >= 2) && (*pwcs == L'\xFEFF'))
  969. {
  970. GetNamesFromStringW(pwcs + 1, dwFileSize / sizeof(WCHAR) - 1, setNames);
  971. }
  972. else
  973. {
  974. GetNamesFromStringA((LPCSTR)pvBase, dwFileSize, setNames);
  975. }
  976. UnmapViewOfFile(pvBase);
  977. }
  978. else
  979. {
  980. hr = HRESULT_FROM_WIN32(GetLastError());
  981. }
  982. CloseHandle(hFileMappingObject);
  983. }
  984. else
  985. {
  986. hr = HRESULT_FROM_WIN32(GetLastError());
  987. }
  988. }
  989. CloseHandle(hFile);
  990. }
  991. else
  992. {
  993. hr = HRESULT_FROM_WIN32(GetLastError());
  994. }
  995. }
  996. else
  997. {
  998. hr = E_INVALIDARG;
  999. }
  1000. if (FAILED(hr))
  1001. {
  1002. AdmtThrowError(GUID_NULL, GUID_NULL, hr, IDS_E_INCLUDE_NAMES_FILE, pszFileName);
  1003. }
  1004. }
  1005. // GetNamesFromStringA Method
  1006. void GetNamesFromStringA(LPCSTR pchString, DWORD cchString, StringSet& setNames)
  1007. {
  1008. static const CHAR chSeparators[] = "\t\n\r";
  1009. LPSTR pszName = NULL;
  1010. size_t cchName = 0;
  1011. try
  1012. {
  1013. LPCSTR pchStringEnd = &pchString[cchString];
  1014. for (LPCSTR pch = pchString; pch < pchStringEnd; pch++)
  1015. {
  1016. // skip space characters
  1017. while ((pch < pchStringEnd) && (*pch == ' '))
  1018. {
  1019. ++pch;
  1020. }
  1021. // beginning of name
  1022. LPCSTR pchBeg = pch;
  1023. // scan for separator saving pointer to last non-whitespace character
  1024. LPCSTR pchEnd = pch;
  1025. while ((pch < pchStringEnd) && (strchr(chSeparators, *pch) == NULL))
  1026. {
  1027. if (*pch++ != ' ')
  1028. {
  1029. pchEnd = pch;
  1030. }
  1031. }
  1032. // insert name which doesn't contain any leading or trailing whitespace characters
  1033. if (pchEnd > pchBeg)
  1034. {
  1035. size_t cch = pchEnd - pchBeg;
  1036. //
  1037. // If potential size of buffer does not exceed maximum value of size_t.
  1038. //
  1039. if (cch < (cch + 256))
  1040. {
  1041. //
  1042. // If buffer size is less than or equal to the number
  1043. // of characters in the name then reallocate the buffer.
  1044. // Note that this accounts for the final null character.
  1045. //
  1046. if (cchName <= cch)
  1047. {
  1048. //
  1049. // Delete current buffer. Increase buffer size to a multiple
  1050. // of 256 characters greater than the length of the current
  1051. // name. Note that this allows for the final null character.
  1052. // Allocate a new buffer.
  1053. //
  1054. delete [] pszName;
  1055. while (cchName <= cch)
  1056. {
  1057. cchName += 256;
  1058. }
  1059. pszName = new CHAR[cchName];
  1060. if (pszName == NULL)
  1061. {
  1062. _com_issue_error(E_OUTOFMEMORY);
  1063. }
  1064. }
  1065. strncpy(pszName, pchBeg, cch);
  1066. pszName[cch] = '\0';
  1067. setNames.insert(_bstr_t(pszName));
  1068. }
  1069. else
  1070. {
  1071. //
  1072. // Should never get here as this means the pointer
  1073. // difference is within 256 characters of the maximum
  1074. // value of the size_t data type.
  1075. //
  1076. _com_issue_error(E_FAIL);
  1077. }
  1078. }
  1079. }
  1080. }
  1081. catch (...)
  1082. {
  1083. delete [] pszName;
  1084. throw;
  1085. }
  1086. delete [] pszName;
  1087. }
  1088. // GetNamesFromStringW Method
  1089. void GetNamesFromStringW(LPCWSTR pchString, DWORD cchString, StringSet& setNames)
  1090. {
  1091. static const WCHAR chSeparators[] = L"\t\n\r";
  1092. LPCWSTR pchStringEnd = &pchString[cchString];
  1093. for (LPCWSTR pch = pchString; pch < pchStringEnd; pch++)
  1094. {
  1095. // skip space characters
  1096. while ((pch < pchStringEnd) && (*pch == L' '))
  1097. {
  1098. ++pch;
  1099. }
  1100. // beginning of name
  1101. LPCWSTR pchBeg = pch;
  1102. // scan for separator saving pointer to last non-whitespace character
  1103. LPCWSTR pchEnd = pch;
  1104. while ((pch < pchStringEnd) && (wcschr(chSeparators, *pch) == NULL))
  1105. {
  1106. if (*pch++ != L' ')
  1107. {
  1108. pchEnd = pch;
  1109. }
  1110. }
  1111. // insert name which doesn't contain any leading or trailing whitespace characters
  1112. if (pchEnd > pchBeg)
  1113. {
  1114. _bstr_t strName(SysAllocStringLen(pchBeg, pchEnd - pchBeg), false);
  1115. setNames.insert(strName);
  1116. }
  1117. }
  1118. }
  1119. // RemoveTrailingDollarSign Method
  1120. _bstr_t RemoveTrailingDollarSign(LPCTSTR pszName)
  1121. {
  1122. LPTSTR psz = _T("");
  1123. if (pszName)
  1124. {
  1125. size_t cch = _tcslen(pszName);
  1126. if (cch > 0)
  1127. {
  1128. psz = reinterpret_cast<LPTSTR>(_alloca((cch + 1) * sizeof(_TCHAR)));
  1129. _tcscpy(psz, pszName);
  1130. LPTSTR p = &psz[cch - 1];
  1131. if (*p == _T('$'))
  1132. {
  1133. *p = _T('\0');
  1134. }
  1135. }
  1136. }
  1137. return psz;
  1138. }
  1139. //---------------------------------------------------------------------------
  1140. // IsInboundTrustDefined Function
  1141. //
  1142. // Synopsis
  1143. // Verifies that a trusted domain object exists for the specified domain and
  1144. // that an inbound trust is defined (i.e. the specified domain trusts this
  1145. // domain).
  1146. //
  1147. // Arguments
  1148. // IN pszDomain - the name of the trusting domain
  1149. //
  1150. // Return
  1151. // True - trusted domain object exists and an inbound trust is defined
  1152. // False - either trusted domain object does not exist or an inbound trust
  1153. // is not defined
  1154. //---------------------------------------------------------------------------
  1155. bool __stdcall IsInboundTrustDefined(PCWSTR pszDomain)
  1156. {
  1157. bool bTrust = false;
  1158. LSA_HANDLE lsahPolicy = NULL;
  1159. PTRUSTED_DOMAIN_INFORMATION_EX ptdieInfo = NULL;
  1160. try
  1161. {
  1162. //
  1163. // Open local policy object with view local information access.
  1164. //
  1165. LSA_OBJECT_ATTRIBUTES lsaoa = { sizeof(LSA_OBJECT_ATTRIBUTES), NULL, NULL, 0, NULL, NULL };
  1166. NTSTATUS ntsStatus = LsaOpenPolicy(NULL, &lsaoa, POLICY_VIEW_LOCAL_INFORMATION, &lsahPolicy);
  1167. if (ntsStatus != STATUS_SUCCESS)
  1168. {
  1169. _com_issue_error(HRESULT_FROM_WIN32(LsaNtStatusToWinError(ntsStatus)));
  1170. }
  1171. //
  1172. // Query for trusted domain object for specified domain.
  1173. //
  1174. PWSTR pwsDomain = const_cast<PWSTR>(pszDomain);
  1175. USHORT cbDomain = wcslen(pszDomain) * sizeof(WCHAR);
  1176. LSA_UNICODE_STRING lsausDomain = { cbDomain, cbDomain, pwsDomain };
  1177. ntsStatus = LsaQueryTrustedDomainInfoByName(
  1178. lsahPolicy,
  1179. &lsausDomain,
  1180. TrustedDomainInformationEx,
  1181. (PVOID*)&ptdieInfo
  1182. );
  1183. if (ntsStatus == STATUS_SUCCESS)
  1184. {
  1185. //
  1186. // Trusted domain object exists. Verify
  1187. // that an inbound trust is defined.
  1188. //
  1189. ULONG ulDirection = ptdieInfo->TrustDirection;
  1190. if ((ulDirection == TRUST_DIRECTION_INBOUND) || (ulDirection == TRUST_DIRECTION_BIDIRECTIONAL))
  1191. {
  1192. bTrust = true;
  1193. }
  1194. }
  1195. else
  1196. {
  1197. //
  1198. // If error is not that trusted domain object
  1199. // does not exist then generate exception.
  1200. //
  1201. if (ntsStatus != STATUS_OBJECT_NAME_NOT_FOUND)
  1202. {
  1203. _com_issue_error(HRESULT_FROM_WIN32(LsaNtStatusToWinError(ntsStatus)));
  1204. }
  1205. }
  1206. //
  1207. // Clean up.
  1208. //
  1209. if (ptdieInfo)
  1210. {
  1211. LsaFreeMemory(ptdieInfo);
  1212. }
  1213. if (lsahPolicy)
  1214. {
  1215. LsaClose(lsahPolicy);
  1216. }
  1217. }
  1218. catch (...)
  1219. {
  1220. if (ptdieInfo)
  1221. {
  1222. LsaFreeMemory(ptdieInfo);
  1223. }
  1224. if (lsahPolicy)
  1225. {
  1226. LsaClose(lsahPolicy);
  1227. }
  1228. throw;
  1229. }
  1230. return bTrust;
  1231. }
  1232. //---------------------------------------------------------------------------
  1233. // IsOutboundTrustDefined Function
  1234. //
  1235. // Synopsis
  1236. // Verifies that a trusted domain object exists for the specified domain on
  1237. // the specified domain controller (i.e. the domain of the specified domain
  1238. // controller trusts the specified domain).
  1239. //
  1240. // Note that this function should only be used for downlevel (NT4 or earlier)
  1241. // domains and that simply the presence of a trusted domain object is
  1242. // sufficient in this case to indicate an outbound trust.
  1243. //
  1244. // Arguments
  1245. // IN pszDomainController - the name of a domain controller in the trusting
  1246. // domain
  1247. // IN pszDomainSid - the SID of the trusted domain in string format
  1248. //
  1249. // Return
  1250. // True - trusted domain object exists
  1251. // False - trusted domain object does not exist
  1252. //---------------------------------------------------------------------------
  1253. bool __stdcall IsOutboundTrustDefined(PCWSTR pszDomainController, PCWSTR pszDomainSid)
  1254. {
  1255. bool bTrust = false;
  1256. LSA_HANDLE lsahPolicy = NULL;
  1257. PSID psidDomain = NULL;
  1258. PTRUSTED_DOMAIN_NAME_INFO ptdniDomainNameInfo = NULL;
  1259. try
  1260. {
  1261. //
  1262. // Open policy object on specified domain controller
  1263. // with view local information access.
  1264. //
  1265. PWSTR pwsDomainController = const_cast<PWSTR>(pszDomainController);
  1266. USHORT cbDomainController = wcslen(pszDomainController) * sizeof(WCHAR);
  1267. LSA_UNICODE_STRING lsausDomainController = { cbDomainController, cbDomainController, pwsDomainController };
  1268. LSA_OBJECT_ATTRIBUTES lsaoa = { sizeof(LSA_OBJECT_ATTRIBUTES), NULL, NULL, 0, NULL, NULL };
  1269. NTSTATUS ntsStatus = LsaOpenPolicy(&lsausDomainController, &lsaoa, POLICY_VIEW_LOCAL_INFORMATION, &lsahPolicy);
  1270. if (ntsStatus != STATUS_SUCCESS)
  1271. {
  1272. _com_issue_error(HRESULT_FROM_WIN32(LsaNtStatusToWinError(ntsStatus)));
  1273. }
  1274. //
  1275. // Convert SID from string format to binary format.
  1276. //
  1277. if (!ConvertStringSidToSid(pszDomainSid, &psidDomain))
  1278. {
  1279. DWORD dwError = GetLastError();
  1280. _com_issue_error(HRESULT_FROM_WIN32(dwError));
  1281. }
  1282. //
  1283. // Query for trusted domain object. Note that LsaQueryTrustedDomainInfo is
  1284. // used because LsaQueryTrustedDomainInfoByName is only supported on
  1285. // Windows 2000 or later.
  1286. //
  1287. ntsStatus = LsaQueryTrustedDomainInfo(
  1288. lsahPolicy,
  1289. psidDomain,
  1290. TrustedDomainNameInformation,
  1291. (PVOID*)&ptdniDomainNameInfo
  1292. );
  1293. switch (ntsStatus)
  1294. {
  1295. case STATUS_SUCCESS:
  1296. {
  1297. //
  1298. // The trusted domain object exists.
  1299. //
  1300. bTrust = true;
  1301. break;
  1302. }
  1303. case STATUS_OBJECT_NAME_NOT_FOUND:
  1304. {
  1305. //
  1306. // The trusted domain object does not exist.
  1307. //
  1308. break;
  1309. }
  1310. default:
  1311. {
  1312. //
  1313. // Another error has occurred therefore generate an exception.
  1314. //
  1315. _com_issue_error(HRESULT_FROM_WIN32(LsaNtStatusToWinError(ntsStatus)));
  1316. break;
  1317. }
  1318. }
  1319. //
  1320. // Clean up.
  1321. //
  1322. if (ptdniDomainNameInfo)
  1323. {
  1324. LsaFreeMemory(ptdniDomainNameInfo);
  1325. }
  1326. if (psidDomain)
  1327. {
  1328. LocalFree(psidDomain);
  1329. }
  1330. if (lsahPolicy)
  1331. {
  1332. LsaClose(lsahPolicy);
  1333. }
  1334. }
  1335. catch (...)
  1336. {
  1337. if (ptdniDomainNameInfo)
  1338. {
  1339. LsaFreeMemory(ptdniDomainNameInfo);
  1340. }
  1341. if (psidDomain)
  1342. {
  1343. LocalFree(psidDomain);
  1344. }
  1345. if (lsahPolicy)
  1346. {
  1347. LsaClose(lsahPolicy);
  1348. }
  1349. throw;
  1350. }
  1351. return bTrust;
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. // GetOutboundTrustStatus Function
  1355. //
  1356. // Synopsis
  1357. // Retrieves the trust connection status for the specified domain on the
  1358. // specified domain controller. The status represents the last connection
  1359. // status of the secure channel but does not quarantee that a future request
  1360. // will succeed. The only way to really verify the secure channel is to
  1361. // reset the secure channel which should not be done arbitrarily.
  1362. //
  1363. // Arguments
  1364. // IN pszDomainController - the name of a domain controller in the trusting
  1365. // domain
  1366. // IN pszDomain - the name of the trusted domain
  1367. //
  1368. // Return
  1369. // ERROR_SUCCESS - the last connection status is okay otherwise the last
  1370. // connection status error
  1371. //---------------------------------------------------------------------------
  1372. DWORD __stdcall GetOutboundTrustStatus(PCWSTR pszDomainController, PCWSTR pszDomain)
  1373. {
  1374. PNETLOGON_INFO_2 pni2Info = NULL;
  1375. NET_API_STATUS nasStatus = I_NetLogonControl2(
  1376. pszDomainController,
  1377. NETLOGON_CONTROL_TC_QUERY,
  1378. 2,
  1379. (LPBYTE)&pszDomain,
  1380. (LPBYTE*)&pni2Info
  1381. );
  1382. if (nasStatus == ERROR_SUCCESS)
  1383. {
  1384. nasStatus = pni2Info->netlog2_tc_connection_status;
  1385. }
  1386. if (pni2Info)
  1387. {
  1388. NetApiBufferFree(pni2Info);
  1389. }
  1390. return nasStatus;
  1391. }
  1392. } // namespace