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.

1329 lines
42 KiB

  1. // Trust.cpp : Implementation of CTrustMgrApp and DLL registration.
  2. #include "stdafx.h"
  3. //#import "\bin\McsVarSetMin.tlb" no_namespace, named_guids
  4. #import "VarSet.tlb" no_namespace, named_guids rename("property", "aproperty")
  5. #include "TrustMgr.h"
  6. #include "Trust.h"
  7. #include "Common.hpp"
  8. #include "UString.hpp"
  9. #include "ResStr.h"
  10. #include "ErrDct.hpp"
  11. #include "EaLen.hpp"
  12. #include "LSAUtils.h"
  13. #include <lm.h>
  14. #include "GetDcName.h"
  15. #include <iads.h>
  16. #include <adshlp.h>
  17. #include "ntsecapi.h"
  18. #include "SecPI.h"
  19. #include "cipher.hpp"
  20. #ifndef TRUST_ATTRIBUTE_FOREST_TRANSITIVE
  21. #define TRUST_ATTRIBUTE_FOREST_TRANSITIVE 0x00000008 // This link may contain forest trust information
  22. #endif
  23. StringLoader gString;
  24. TErrorDct err;
  25. // This method is called by the dispatcher to verify that this is a valid plug-in
  26. // Only valid plug-ins will be sent out with the agents
  27. // The purpose of this check is to make it more difficult for unauthorized parties
  28. // to use our plug-in interface, since it is currently undocumented.
  29. STDMETHODIMP CTrust::Verify(/*[in,out]*/ULONG * pData,/*[in]*/ULONG size)
  30. {
  31. McsChallenge * pMcsChallenge;
  32. long lTemp1;
  33. long lTemp2;
  34. if( size == sizeof(McsChallenge) )
  35. {
  36. pMcsChallenge = (McsChallenge*)(pData);
  37. SimpleCipher((LPBYTE)pMcsChallenge,size);
  38. pMcsChallenge->MCS[0] = 'M';
  39. pMcsChallenge->MCS[1] = 'C';
  40. pMcsChallenge->MCS[2] = 'S';
  41. pMcsChallenge->MCS[3] = 0;
  42. lTemp1 = pMcsChallenge->lRand1 + pMcsChallenge->lRand2;
  43. lTemp2 = pMcsChallenge->lRand2 - pMcsChallenge->lRand1;
  44. pMcsChallenge->lRand1 = lTemp1;
  45. pMcsChallenge->lRand2 = lTemp2;
  46. pMcsChallenge->lTime += 100;
  47. SimpleCipher((LPBYTE)pMcsChallenge,size);
  48. }
  49. else
  50. return E_FAIL;
  51. return S_OK;
  52. }
  53. /////////////////////////////////////////////////////////////////////////////
  54. //
  55. STDMETHODIMP CTrust::QueryTrust(BSTR domTrusting, BSTR domTrusted, IUnknown **pVarSet)
  56. {
  57. HRESULT hr = S_OK;
  58. return hr;
  59. }
  60. STDMETHODIMP CTrust::CreateTrust(BSTR domTrusting, BSTR domTrusted, BOOL bBidirectional, BOOL *pbErrorFromTrusting, BOOL *pbErrorFromTrusted)
  61. {
  62. HRESULT hr = S_OK;
  63. *pbErrorFromTrusting = FALSE;
  64. *pbErrorFromTrusted = FALSE;
  65. hr = CheckAndCreate(domTrusting,domTrusted,NULL,NULL,NULL,NULL,NULL,NULL,TRUE,bBidirectional,pbErrorFromTrusting,pbErrorFromTrusted);
  66. return HRESULT_FROM_WIN32(hr);
  67. }
  68. STDMETHODIMP
  69. CTrust::CreateTrustWithCreds(
  70. BSTR domTrusting,
  71. BSTR domTrusted,
  72. BSTR credTrustingDomain,
  73. BSTR credTrustingAccount,
  74. BSTR credTrustingPassword,
  75. BSTR credTrustedDomain,
  76. BSTR credTrustedAccount,
  77. BSTR credTrustedPassword,
  78. BOOL bBidirectional,
  79. BOOL *pbErrorFromTrusting,
  80. BOOL *pbErrorFromTrusted
  81. )
  82. {
  83. HRESULT hr = S_OK;
  84. *pbErrorFromTrusting = FALSE;
  85. *pbErrorFromTrusted = FALSE;
  86. hr = CheckAndCreate(domTrusting,domTrusted,credTrustingDomain,credTrustingAccount,credTrustingPassword,
  87. credTrustedDomain,credTrustedAccount,credTrustedPassword,TRUE,bBidirectional,
  88. pbErrorFromTrusting,pbErrorFromTrusted);
  89. return hr;
  90. }
  91. STDMETHODIMP CTrust::GetRegisterableFiles(/* [out] */SAFEARRAY ** pArray)
  92. {
  93. SAFEARRAYBOUND bound[1] = { 0, 0 };
  94. // this plug-in runs locally, no files to distribute
  95. (*pArray) = SafeArrayCreate(VT_BSTR,1,bound);
  96. return S_OK;
  97. }
  98. STDMETHODIMP CTrust::GetRequiredFiles(/* [out] */SAFEARRAY ** pArray)
  99. {
  100. SAFEARRAYBOUND bound[1] = { 0, 0 };
  101. // this plug-in runs locally, no files to distribute
  102. (*pArray) = SafeArrayCreate(VT_BSTR,1,bound);
  103. return S_OK;
  104. }
  105. STDMETHODIMP CTrust::GetDescription(/* [out] */ BSTR * description)
  106. {
  107. (*description) = SysAllocString(L"Sets up needed trusts between domains.");
  108. return S_OK;
  109. }
  110. BOOL IsDownLevel(WCHAR * sComputer)
  111. {
  112. BOOL bDownlevel = TRUE;
  113. WKSTA_INFO_100 * pInfo;
  114. long rc = NetWkstaGetInfo(sComputer,100,(LPBYTE*)&pInfo);
  115. if ( ! rc )
  116. {
  117. if ( pInfo->wki100_ver_major >= 5 )
  118. {
  119. bDownlevel = FALSE;
  120. }
  121. NetApiBufferFree(pInfo);
  122. }
  123. return bDownlevel;
  124. }
  125. // Helper function that finds a trusts in our varset list
  126. LONG CTrust::FindInboundTrust(IVarSet * pVarSet,WCHAR * sName,LONG max)
  127. {
  128. LONG ndx = -1;
  129. LONG curr = 0;
  130. WCHAR key[100];
  131. _bstr_t tName;
  132. for ( curr = 0 ; curr < max ; curr++ )
  133. {
  134. swprintf(key,L"Trusts.%ld",curr);
  135. tName = pVarSet->get(key);
  136. if ( ! UStrICmp(tName,sName) )
  137. {
  138. // found it!
  139. ndx = curr;
  140. break;
  141. }
  142. }
  143. return ndx;
  144. }
  145. HRESULT
  146. CTrust::CheckAndCreateTrustingSide(
  147. LSA_HANDLE hTrusting,
  148. WCHAR * trustingDomain,
  149. WCHAR * trustedDomain,
  150. WCHAR * trustingComp,
  151. WCHAR * trustedComp,
  152. WCHAR * trustedDNSName,
  153. BYTE * trustedSid,
  154. BOOL bCreate,
  155. BOOL bBidirectional,
  156. BOOL bDownLevel,
  157. BOOL * pbErrorFromTrusting,
  158. BOOL * pbErrorFromTrusted
  159. )
  160. {
  161. DWORD rc = S_OK;
  162. // if credentials are specified, use them
  163. NTSTATUS status;
  164. LSA_AUTH_INFORMATION curr;
  165. LSA_AUTH_INFORMATION prev;
  166. WCHAR password[] = L"password";
  167. if ( bCreate )
  168. {
  169. // set up the auth information for the trust relationship
  170. curr.AuthInfo = (LPBYTE)password;
  171. curr.AuthInfoLength = UStrLen(password) * sizeof(WCHAR);
  172. curr.AuthType = TRUST_AUTH_TYPE_CLEAR;
  173. curr.LastUpdateTime.QuadPart = 0;
  174. prev.AuthInfo = (LPBYTE)password;
  175. prev.AuthInfoLength = UStrLen(password) * sizeof(WCHAR);
  176. prev.AuthType = TRUST_AUTH_TYPE_CLEAR;
  177. prev.LastUpdateTime.QuadPart = 0;
  178. // set up the trusting side of the relationship
  179. if ( IsDownLevel(trustingComp) )
  180. {
  181. TRUSTED_DOMAIN_NAME_INFO nameInfo;
  182. InitLsaString(&nameInfo.Name,const_cast<WCHAR*>(trustedDomain));
  183. status = LsaSetTrustedDomainInformation(hTrusting,trustedSid,TrustedDomainNameInformation,&nameInfo);
  184. rc = LsaNtStatusToWinError(status);
  185. if ( ! rc || rc == ERROR_ALREADY_EXISTS )
  186. {
  187. // set the password for the new trust
  188. TRUSTED_PASSWORD_INFO pwdInfo;
  189. InitLsaString(&pwdInfo.Password,password);
  190. InitLsaString(&pwdInfo.OldPassword,NULL);
  191. status = LsaSetTrustedDomainInformation(hTrusting,trustedSid,TrustedPasswordInformation,&pwdInfo);
  192. rc = LsaNtStatusToWinError(status);
  193. }
  194. }
  195. else
  196. {
  197. // for Win2K domain, use LsaCreateTrustedDomainEx
  198. // to create the trustedDomain object.
  199. LSA_UNICODE_STRING sTemp;
  200. TRUSTED_DOMAIN_INFORMATION_EX trustedInfo;
  201. TRUSTED_DOMAIN_AUTH_INFORMATION trustAuth;
  202. InitLsaString(&sTemp, const_cast<WCHAR*>(trustedDomain));
  203. trustedInfo.FlatName = sTemp;
  204. if (bDownLevel)
  205. InitLsaString(&sTemp, trustedDomain);
  206. else
  207. InitLsaString(&sTemp, trustedDNSName);
  208. trustedInfo.Name = sTemp;
  209. trustedInfo.Sid = trustedSid;
  210. if (bDownLevel)
  211. {
  212. trustedInfo.TrustType = TRUST_TYPE_DOWNLEVEL;
  213. }
  214. else
  215. {
  216. trustedInfo.TrustType = TRUST_TYPE_UPLEVEL;
  217. }
  218. if ( bBidirectional )
  219. trustedInfo.TrustDirection = TRUST_DIRECTION_BIDIRECTIONAL;
  220. else
  221. trustedInfo.TrustDirection = TRUST_DIRECTION_OUTBOUND;
  222. trustedInfo.TrustAttributes = TRUST_ATTRIBUTE_NON_TRANSITIVE;
  223. trustAuth.IncomingAuthInfos = bBidirectional ? 1 : 0;
  224. trustAuth.OutgoingAuthInfos = 1;
  225. trustAuth.IncomingAuthenticationInformation = bBidirectional ? &curr : NULL;
  226. trustAuth.IncomingPreviousAuthenticationInformation = NULL;
  227. trustAuth.OutgoingAuthenticationInformation = &curr;
  228. trustAuth.OutgoingPreviousAuthenticationInformation = NULL;
  229. LSA_HANDLE hTemp = NULL;
  230. status = LsaCreateTrustedDomainEx( hTrusting, &trustedInfo, &trustAuth, 0, &hTemp );
  231. rc = LsaNtStatusToWinError(status);
  232. // if the trust already exists, update its password
  233. if ( status == STATUS_OBJECT_NAME_COLLISION )
  234. {
  235. TRUSTED_DOMAIN_INFORMATION_EX * pTrustedInfo = NULL;
  236. status = LsaQueryTrustedDomainInfo(hTrusting,trustedSid,TrustedDomainInformationEx,(LPVOID*)&pTrustedInfo);
  237. if ( ! status )
  238. {
  239. pTrustedInfo->TrustDirection |= trustedInfo.TrustDirection;
  240. status = LsaSetTrustedDomainInfoByName(hTrusting,&trustedInfo.Name,TrustedDomainInformationEx,(LPVOID*)pTrustedInfo);
  241. if ( ! status )
  242. {
  243. status = LsaSetTrustedDomainInfoByName(hTrusting,&trustedInfo.Name,TrustedDomainAuthInformation,(LPVOID*)&trustAuth);
  244. }
  245. }
  246. rc = LsaNtStatusToWinError(status);
  247. }
  248. if( ! rc )
  249. {
  250. if (hTemp)
  251. LsaClose(hTemp);
  252. }
  253. }
  254. }
  255. return rc;
  256. }
  257. HRESULT
  258. CTrust::CheckAndCreateTrustedSide(
  259. LSA_HANDLE hTrusted,
  260. WCHAR * trustingDomain,
  261. WCHAR * trustedDomain,
  262. WCHAR * trustingComp,
  263. WCHAR * trustedComp,
  264. WCHAR * trustingDNSName,
  265. BYTE * trustingSid,
  266. BOOL bCreate,
  267. BOOL bBidirectional,
  268. BOOL bDownLevel,
  269. BOOL * pbErrorFromTrusting,
  270. BOOL * pbErrorFromTrusted
  271. )
  272. {
  273. DWORD rc = S_OK;
  274. LSA_HANDLE hTrusting = NULL;
  275. NTSTATUS status;
  276. LSA_AUTH_INFORMATION curr;
  277. LSA_AUTH_INFORMATION prev;
  278. WCHAR password[] = L"password";
  279. // set up the auth information for the trust relationship
  280. curr.AuthInfo = (LPBYTE)password;
  281. curr.AuthInfoLength = UStrLen(password) * sizeof(WCHAR);
  282. curr.AuthType = TRUST_AUTH_TYPE_CLEAR;
  283. curr.LastUpdateTime.QuadPart = 0;
  284. prev.AuthInfo = (LPBYTE)password;
  285. prev.AuthInfoLength = UStrLen(password) * sizeof(WCHAR);
  286. prev.AuthType = TRUST_AUTH_TYPE_CLEAR;
  287. prev.LastUpdateTime.QuadPart = 0;
  288. // set up the trusted side of the relationship
  289. if ( IsDownLevel(trustedComp) )
  290. {
  291. // create an inter-domain trust account for the trusting domain on the trusted domain
  292. USER_INFO_1 uInfo;
  293. DWORD parmErr;
  294. WCHAR name[LEN_Account];
  295. memset(&uInfo,0,(sizeof uInfo));
  296. UStrCpy(name,trustingDomain);
  297. name[UStrLen(name) + 1] = 0;
  298. name[UStrLen(name)] = L'$';
  299. if ( ! bCreate )
  300. {
  301. USER_INFO_1 * tempInfo = NULL;
  302. rc = NetUserGetInfo(trustedComp,name,1,(LPBYTE*)&tempInfo);
  303. if ( ! rc )
  304. {
  305. // the trust exists
  306. NetApiBufferFree(tempInfo);
  307. rc = NERR_UserExists;
  308. }
  309. else
  310. {
  311. if ( rc != NERR_UserNotFound )
  312. {
  313. err.SysMsgWrite(ErrE,rc,DCT_MSG_TRUSTING_DOM_GETINFO_FAILED_SSD,trustingDomain,trustedComp,rc);
  314. }
  315. }
  316. }
  317. else
  318. {
  319. // this creates the trust account if it doesn't already exist
  320. // if the account does exist, reset its password
  321. uInfo.usri1_flags = UF_SCRIPT | UF_INTERDOMAIN_TRUST_ACCOUNT;
  322. uInfo.usri1_name = name;
  323. uInfo.usri1_password = password;
  324. uInfo.usri1_priv = 1;
  325. rc = NetUserAdd(trustedComp,1,(LPBYTE)&uInfo,&parmErr);
  326. if ( rc && rc != NERR_UserExists )
  327. {
  328. err.SysMsgWrite(ErrE,rc,DCT_MSG_TRUSTING_DOM_CREATE_FAILED_SSD,trustingDomain,trustedDomain,rc);
  329. }
  330. else if ( rc == NERR_UserExists )
  331. {
  332. // reset the password for the existing trust account
  333. USER_INFO_1003 pwdInfo;
  334. DWORD parmErr;
  335. pwdInfo.usri1003_password = password;
  336. rc = NetUserSetInfo(trustedComp,name,1003,(LPBYTE)&pwdInfo,&parmErr);
  337. }
  338. }
  339. }
  340. else
  341. {
  342. // Win2K, all trusts exist as trusted domain objects
  343. // Create the trustedDomain object.
  344. LSA_UNICODE_STRING sTemp;
  345. TRUSTED_DOMAIN_INFORMATION_EX trustedInfo;
  346. TRUSTED_DOMAIN_AUTH_INFORMATION trustAuth;
  347. InitLsaString(&sTemp, const_cast<WCHAR*>(trustingDomain));
  348. trustedInfo.FlatName = sTemp;
  349. if (bDownLevel)
  350. InitLsaString(&sTemp, trustingDomain);
  351. else
  352. InitLsaString(&sTemp, trustingDNSName);
  353. trustedInfo.Name = sTemp;
  354. trustedInfo.Sid = trustingSid;
  355. if (bDownLevel)
  356. {
  357. trustedInfo.TrustType = TRUST_TYPE_DOWNLEVEL;
  358. }
  359. else
  360. {
  361. trustedInfo.TrustType = TRUST_TYPE_UPLEVEL;
  362. }
  363. if ( bBidirectional )
  364. trustedInfo.TrustDirection = TRUST_DIRECTION_BIDIRECTIONAL;
  365. else
  366. trustedInfo.TrustDirection = TRUST_DIRECTION_INBOUND;
  367. trustedInfo.TrustAttributes = TRUST_ATTRIBUTE_NON_TRANSITIVE;
  368. trustAuth.IncomingAuthInfos = 1;
  369. trustAuth.OutgoingAuthInfos = bBidirectional ? 1 : 0;
  370. trustAuth.OutgoingAuthenticationInformation = bBidirectional ? &curr : NULL;
  371. trustAuth.OutgoingPreviousAuthenticationInformation = NULL;
  372. trustAuth.IncomingAuthenticationInformation = &curr;
  373. trustAuth.IncomingPreviousAuthenticationInformation = NULL;
  374. if ( bCreate )
  375. {
  376. status = LsaCreateTrustedDomainEx( hTrusted, &trustedInfo, &trustAuth, POLICY_VIEW_LOCAL_INFORMATION |
  377. POLICY_TRUST_ADMIN | POLICY_CREATE_SECRET, &hTrusting );
  378. if ( status == STATUS_OBJECT_NAME_COLLISION )
  379. {
  380. TRUSTED_DOMAIN_INFORMATION_EX * pTrustedInfo = NULL;
  381. // Get the old information
  382. status = LsaQueryTrustedDomainInfoByName(hTrusted,&sTemp,TrustedDomainInformationEx,(LPVOID*)&pTrustedInfo);
  383. if ( ! status )
  384. {
  385. pTrustedInfo->TrustAttributes |= trustedInfo.TrustAttributes;
  386. pTrustedInfo->TrustDirection |= trustedInfo.TrustDirection;
  387. status = LsaSetTrustedDomainInfoByName(hTrusted,&sTemp,TrustedDomainInformationEx,pTrustedInfo);
  388. if (! status )
  389. {
  390. status = LsaSetTrustedDomainInfoByName(hTrusted,&sTemp,TrustedDomainAuthInformation,&trustAuth);
  391. }
  392. LsaFreeMemory(pTrustedInfo);
  393. }
  394. }
  395. }
  396. else
  397. {
  398. TRUSTED_DOMAIN_INFORMATION_EX * pTrustedInfo = NULL;
  399. status = LsaQueryTrustedDomainInfoByName(hTrusted,&sTemp,TrustedDomainInformationEx,(LPVOID*)&pTrustedInfo);
  400. if ( ! status )
  401. {
  402. LsaFreeMemory(pTrustedInfo);
  403. }
  404. }
  405. rc = LsaNtStatusToWinError(status);
  406. if ( ! rc )
  407. {
  408. LsaClose(hTrusting);
  409. hTrusting = NULL;
  410. }
  411. }
  412. if ( bCreate && bBidirectional && IsDownLevel(trustingComp) )
  413. {
  414. // set up the trust account for the other side of the relationship
  415. // For Win2K, both sides of the bidirectional trust are handled together,
  416. // but NT4 bidirectional trusts require 2 separate actions
  417. USER_INFO_1 uInfo;
  418. DWORD parmErr;
  419. WCHAR name2[LEN_Account];
  420. memset(&uInfo,0,(sizeof uInfo));
  421. UStrCpy(name2,trustedDomain);
  422. name2[UStrLen(name2) + 1] = 0;
  423. name2[UStrLen(name2)] = L'$';
  424. uInfo.usri1_flags = UF_SCRIPT | UF_INTERDOMAIN_TRUST_ACCOUNT;
  425. uInfo.usri1_name = name2;
  426. uInfo.usri1_password = password;
  427. uInfo.usri1_priv = 1;
  428. rc = NetUserAdd(trustingComp,1,(LPBYTE)&uInfo,&parmErr);
  429. if ( rc == NERR_UserExists )
  430. {
  431. LPUSER_INFO_1 puInfo;
  432. rc = NetUserGetInfo(trustingComp, name2, 1, (LPBYTE*)&puInfo);
  433. if ( !rc )
  434. {
  435. puInfo->usri1_flags &= UF_INTERDOMAIN_TRUST_ACCOUNT;
  436. puInfo->usri1_password = password;
  437. rc = NetUserSetInfo(trustingComp,name2,1,(LPBYTE)puInfo,&parmErr);
  438. NetApiBufferFree(puInfo);
  439. }
  440. else
  441. {
  442. err.MsgWrite(0, DCT_MSG_INVALID_ACCOUNT_S, name2);
  443. }
  444. }
  445. else if ( rc )
  446. {
  447. err.SysMsgWrite(ErrE,rc,DCT_MSG_TRUSTING_DOM_CREATE_FAILED_SSD,trustingDomain,trustedDomain,rc);
  448. }
  449. }
  450. return rc;
  451. }
  452. // Main function used to create trusts
  453. HRESULT
  454. CTrust::CheckAndCreate(
  455. WCHAR * trustingDomain,
  456. WCHAR * trustedDomain,
  457. WCHAR * credDomainTrusting,
  458. WCHAR * credAccountTrusting,
  459. WCHAR * credPasswordTrusting,
  460. WCHAR * credDomainTrusted,
  461. WCHAR * credAccountTrusted,
  462. WCHAR * credPasswordTrusted,
  463. BOOL bCreate,
  464. BOOL bBidirectional,
  465. BOOL * pbErrorFromTrusting,
  466. BOOL * pbErrorFromTrusted
  467. )
  468. {
  469. DWORD rc = 0;
  470. _bstr_t trustingDom;
  471. _bstr_t trustedDom;
  472. _bstr_t trustingComp;
  473. _bstr_t trustedComp;
  474. _bstr_t trustingDNSName;
  475. _bstr_t trustedDNSName;
  476. BYTE trustingSid[200];
  477. BYTE trustedSid[200];
  478. WCHAR name[LEN_Account];
  479. DWORD lenName = DIM(name);
  480. DWORD lenSid = DIM(trustingSid);
  481. SID_NAME_USE snu;
  482. WCHAR * curr = NULL;
  483. BOOL bConnectTrusted = FALSE;
  484. BOOL bConnectTrusting = FALSE;
  485. LSA_HANDLE hTrusting = NULL;
  486. LSA_HANDLE hTrusted = NULL;
  487. BOOL bDownLevel = FALSE;
  488. // Get the DC names, and domain SIDs for the source and target domains
  489. rc = GetDomainNames5(trustingDomain, trustingDom, trustingDNSName);
  490. if (rc)
  491. *pbErrorFromTrusting = TRUE;
  492. if (!rc)
  493. {
  494. rc = GetDomainNames5(trustedDomain, trustedDom, trustedDNSName);
  495. if (rc)
  496. *pbErrorFromTrusted = TRUE;
  497. }
  498. if (!rc)
  499. {
  500. // check whether the trust should be down level or not
  501. if (!trustingDNSName || !trustedDNSName)
  502. bDownLevel = TRUE;
  503. rc = GetDcName5(trustingDom, DS_PDC_REQUIRED, trustingComp);
  504. if ( rc )
  505. {
  506. *pbErrorFromTrusting = TRUE;
  507. err.SysMsgWrite(ErrE,rc,DCT_MSG_GET_DCNAME_FAILED_SD,trustingDom,rc);
  508. }
  509. }
  510. if ( ! rc )
  511. {
  512. rc = GetDcName5(trustedDom, DS_PDC_REQUIRED, trustedComp);
  513. if ( rc )
  514. {
  515. *pbErrorFromTrusted = TRUE;
  516. err.SysMsgWrite(ErrE,rc,DCT_MSG_GET_DCNAME_FAILED_SD,trustedDom,rc);
  517. }
  518. }
  519. if (!rc)
  520. {
  521. if ( credAccountTrusted && *credAccountTrusted )
  522. {
  523. if ( EstablishSession(trustedComp,credDomainTrusted,credAccountTrusted,credPasswordTrusted,TRUE) )
  524. {
  525. bConnectTrusted = TRUE;
  526. }
  527. else
  528. {
  529. rc = GetLastError();
  530. *pbErrorFromTrusted = TRUE;
  531. }
  532. }
  533. }
  534. if (!rc)
  535. {
  536. if ( credAccountTrusting && *credAccountTrusting )
  537. {
  538. if ( EstablishSession(trustingComp,credDomainTrusting,credAccountTrusting,credPasswordTrusting,TRUE) )
  539. {
  540. bConnectTrusting = TRUE;
  541. }
  542. else
  543. {
  544. rc = GetLastError();
  545. *pbErrorFromTrusting = TRUE;
  546. }
  547. }
  548. }
  549. // Need to get the computer name and the SIDs for the domains.
  550. if ( ! rc && ! LookupAccountName(trustingComp,trustingDom,trustingSid,&lenSid,name,&lenName,&snu) )
  551. {
  552. rc = GetLastError();
  553. *pbErrorFromTrusting = TRUE;
  554. err.SysMsgWrite(ErrE,rc,DCT_MSG_GET_DOMAIN_SID_FAILED_1,trustingDom,rc);
  555. }
  556. lenSid = DIM(trustedSid);
  557. lenName = DIM(name);
  558. if (! rc && ! LookupAccountName(trustedComp,trustedDom,trustedSid,&lenSid,name,&lenName,&snu) )
  559. {
  560. rc = GetLastError();
  561. *pbErrorFromTrusted = TRUE;
  562. err.SysMsgWrite(ErrE,rc,DCT_MSG_GET_DOMAIN_SID_FAILED_1,trustedDom,rc);
  563. }
  564. // check whether we are able to call CheckAndCreateTrustedSide and CheckAndCreateTrustingSide
  565. // we need to be able to open policy to trusting and trusted computers with the following access right
  566. // POLICY_VIEW_LOCAL_INFORMATION
  567. // POLICY_TRUST_ADMIN
  568. // POLICY_CREATE_SECRET
  569. if (!rc)
  570. {
  571. NTSTATUS status;
  572. WCHAR* szComputerFailed = NULL;
  573. status = OpenPolicy(trustingComp,
  574. POLICY_VIEW_LOCAL_INFORMATION|POLICY_TRUST_ADMIN|POLICY_CREATE_SECRET,
  575. &hTrusting);
  576. if (status == STATUS_SUCCESS)
  577. {
  578. status = OpenPolicy(trustedComp,
  579. POLICY_VIEW_LOCAL_INFORMATION|POLICY_TRUST_ADMIN|POLICY_CREATE_SECRET,
  580. &hTrusted);
  581. if (status != STATUS_SUCCESS)
  582. {
  583. szComputerFailed = trustedComp;
  584. *pbErrorFromTrusted = TRUE;
  585. }
  586. }
  587. else
  588. {
  589. szComputerFailed = trustingComp;
  590. *pbErrorFromTrusting = TRUE;
  591. }
  592. rc = LsaNtStatusToWinError(status);
  593. if (rc != ERROR_SUCCESS)
  594. err.SysMsgWrite(ErrE, rc, DCT_MSG_LSA_OPEN_FAILED_SD, szComputerFailed, rc);
  595. }
  596. // check the trusted side of the trust first
  597. if ( ! rc )
  598. {
  599. rc = CheckAndCreateTrustedSide(hTrusted, trustingDom,trustedDom,trustingComp,trustedComp,trustingDNSName,trustingSid,
  600. bCreate,bBidirectional, bDownLevel, pbErrorFromTrusting, pbErrorFromTrusted);
  601. }
  602. if ( ! rc )
  603. {
  604. rc = CheckAndCreateTrustingSide(hTrusting,trustingDom,trustedDom,trustingComp,trustedComp,trustedDNSName,trustedSid,
  605. bCreate,bBidirectional, bDownLevel, pbErrorFromTrusting, pbErrorFromTrusted);
  606. }
  607. // close LSA_HANDLE hTrusting and hTrusted
  608. if (hTrusting != NULL)
  609. LsaClose(hTrusting);
  610. if (hTrusted)
  611. LsaClose(hTrusted);
  612. if ( bConnectTrusted )
  613. {
  614. EstablishSession(trustedComp,credDomainTrusted,credAccountTrusted,credPasswordTrusted,FALSE);
  615. }
  616. if ( bConnectTrusting )
  617. {
  618. EstablishSession(trustingComp,credDomainTrusting,credAccountTrusting,credPasswordTrusting,FALSE);
  619. }
  620. return HRESULT_FROM_WIN32(rc);
  621. }
  622. long CTrust::EnumerateTrustedDomains(WCHAR * domain,BOOL bIsTarget,IVarSet * pVarSet,long ndxStart)
  623. {
  624. DWORD rcOs; // OS return code
  625. LSA_HANDLE hPolicy;
  626. NTSTATUS status;
  627. _bstr_t computer;
  628. DOMAIN_CONTROLLER_INFO * pInfo;
  629. WCHAR sName[LEN_Domain];
  630. WCHAR key[100];
  631. long ndxTrust = ndxStart;
  632. /* PDS_DOMAIN_TRUSTS *ChildDomains;
  633. ULONG numChilds;
  634. */
  635. err.MsgWrite(0,DCT_MSG_ENUMERATING_TRUSTED_DOMAINS_S,domain);
  636. // open a handle to the source domain
  637. rcOs = GetAnyDcName5(domain, computer);
  638. if ( rcOs )
  639. {
  640. err.SysMsgWrite(ErrE,rcOs,DCT_MSG_GET_DCNAME_FAILED_SD,domain,rcOs);
  641. }
  642. if ( ! rcOs )
  643. {
  644. if ( IsDownLevel(computer) )
  645. {
  646. //Enumerate the trusted domains until there are no more to return.
  647. status = OpenPolicy(computer, POLICY_VIEW_LOCAL_INFORMATION ,&hPolicy);
  648. if ( status == STATUS_SUCCESS )
  649. {
  650. LSA_ENUMERATION_HANDLE lsaEnumHandle=0; // start an enum
  651. PLSA_TRUST_INFORMATION trustInfo = NULL;
  652. ULONG ulReturned; // number of items returned
  653. NTSTATUS status;
  654. DWORD rc;
  655. do {
  656. status = LsaEnumerateTrustedDomains(
  657. hPolicy, // open policy handle
  658. &lsaEnumHandle, // enumeration tracker
  659. (void**)&trustInfo, // buffer to receive data
  660. 32000, // recommended buffer size
  661. &ulReturned // number of items returned
  662. );
  663. //Check the return status for error.
  664. rc = LsaNtStatusToWinError(status);
  665. if( (rc != ERROR_SUCCESS) &&
  666. (rc != ERROR_MORE_DATA) &&
  667. (rc != ERROR_NO_MORE_ITEMS)
  668. )
  669. {
  670. err.SysMsgWrite(ErrE,rcOs,DCT_MSG_TRUSTED_ENUM_FAILED_SD,domain,rcOs);
  671. }
  672. else
  673. {
  674. // . . . Code to use the Trusted Domain information
  675. for ( ULONG ndx = 0 ; ndx < ulReturned ; ndx++ )
  676. {
  677. _bstr_t direction;
  678. UStrCpy(sName,trustInfo[ndx].Name.Buffer, ( trustInfo[ndx].Name.Length / (sizeof WCHAR)) + 1);
  679. TRUSTED_DOMAIN_INFORMATION_EX * pTrustedInfo = NULL;
  680. status = LsaQueryTrustedDomainInfo(hPolicy,trustInfo[ndx].Sid,TrustedDomainInformationEx,(LPVOID*)&pTrustedInfo);
  681. if ( ! status )
  682. {
  683. switch ( pTrustedInfo->TrustDirection )
  684. {
  685. case TRUST_DIRECTION_DISABLED:
  686. direction = GET_BSTR(IDS_TRUST_DIRECTION_DISABLED);
  687. break;
  688. case TRUST_DIRECTION_INBOUND:
  689. direction = GET_BSTR(IDS_TRUST_DIRECTION_INBOUND);
  690. break;
  691. case TRUST_DIRECTION_OUTBOUND:
  692. direction = GET_BSTR(IDS_TRUST_DIRECTION_OUTBOUND);
  693. break;
  694. case TRUST_DIRECTION_BIDIRECTIONAL:
  695. direction = GET_BSTR(IDS_TRUST_DIRECTION_BIDIRECTIONAL);
  696. break;
  697. default:
  698. break;
  699. };
  700. if ( ! bIsTarget )
  701. {
  702. swprintf(key,L"Trusts.%ld.Type",ndxTrust);
  703. pVarSet->put(key, GET_BSTR(IDS_TRUST_RELATION_EXTERNAL));
  704. }
  705. LsaFreeMemory(pTrustedInfo);
  706. }
  707. else
  708. {
  709. rcOs = LsaNtStatusToWinError(status);
  710. // My logic here is that we are checking Trusted domains here so this is atleast true
  711. // check whether this trust is already listed as an inbound trust
  712. //* direction = L"Outbound";
  713. direction = GET_BSTR(IDS_TRUST_DIRECTION_OUTBOUND);
  714. }
  715. if ( ! bIsTarget )
  716. {
  717. swprintf(key,L"Trusts.%ld",ndxTrust);
  718. pVarSet->put(key,sName);
  719. swprintf(key,L"Trusts.%ld.Direction",ndxTrust);
  720. pVarSet->put(key,direction);
  721. swprintf(key,L"Trusts.%ld.ExistsForTarget",ndxTrust);
  722. //* pVarSet->put(key,L"No");
  723. pVarSet->put(key,GET_BSTR(IDS_No));
  724. err.MsgWrite(0,DCT_MSG_SOURCE_TRUSTS_THIS_SS,sName,domain);
  725. }
  726. long ndx2 = FindInboundTrust(pVarSet,sName,ndxTrust);
  727. if ( ndx2 != -1 )
  728. {
  729. if ( ! bIsTarget )
  730. {
  731. // we've already seen this trust as an inbound trust
  732. // update the existing record!
  733. WCHAR key2[1000];
  734. swprintf(key2,L"Trusts.%ld.Direction",ndx2);
  735. //* pVarSet->put(key2,L"Bidirectional");
  736. pVarSet->put(key2,GET_BSTR(IDS_TRUST_DIRECTION_BIDIRECTIONAL));
  737. continue; // don't update the trust entry index, since we used the existing
  738. // entry instead of creating a new one
  739. }
  740. else
  741. {
  742. swprintf(key,L"Trusts.%ld.ExistsForTarget",ndx2);
  743. //* pVarSet->put(key,L"Yes");
  744. pVarSet->put(key,GET_BSTR(IDS_YES));
  745. err.MsgWrite(0,DCT_MSG_TARGET_TRUSTS_THIS_SS,domain,sName);
  746. }
  747. }
  748. swprintf(key,L"Trusts.%ld.ExistsForTarget",ndxTrust);
  749. // check the trusted domain, to see if the target already trusts it
  750. //if ( UStrICmp(sName,target) )
  751. // {
  752. // continue;
  753. // }
  754. if ( ! bIsTarget )
  755. ndxTrust++;
  756. }
  757. // Free the buffer.
  758. LsaFreeMemory(trustInfo);
  759. }
  760. } while (rc == ERROR_SUCCESS || rc == ERROR_MORE_DATA );
  761. LsaClose(hPolicy);
  762. }
  763. }
  764. else
  765. {
  766. ULONG ulCount;
  767. PDS_DOMAIN_TRUSTS pDomainTrusts;
  768. DWORD dwError = DsEnumerateDomainTrusts(
  769. computer,
  770. DS_DOMAIN_IN_FOREST|DS_DOMAIN_DIRECT_INBOUND|DS_DOMAIN_DIRECT_OUTBOUND,
  771. &pDomainTrusts,
  772. &ulCount
  773. );
  774. if (dwError == NO_ERROR)
  775. {
  776. ULONG ulIndex;
  777. ULONG ulDomainIndex = (ULONG)-1L;
  778. ULONG ulParentIndex = (ULONG)-1L;
  779. // find local domain
  780. for (ulIndex = 0; ulIndex < ulCount; ulIndex++)
  781. {
  782. if (pDomainTrusts[ulIndex].Flags & DS_DOMAIN_PRIMARY)
  783. {
  784. ulDomainIndex = ulIndex;
  785. if (!(pDomainTrusts[ulIndex].Flags & DS_DOMAIN_TREE_ROOT))
  786. {
  787. ulParentIndex = pDomainTrusts[ulIndex].ParentIndex;
  788. }
  789. break;
  790. }
  791. }
  792. for (ulIndex = 0; ulIndex < ulCount; ulIndex++)
  793. {
  794. DS_DOMAIN_TRUSTS& rDomainTrust = pDomainTrusts[ulIndex];
  795. // filter out indirect trusts
  796. if (!(rDomainTrust.Flags & (DS_DOMAIN_DIRECT_INBOUND|DS_DOMAIN_DIRECT_OUTBOUND)))
  797. {
  798. continue;
  799. }
  800. // trusted or trusting domain name
  801. _bstr_t bstrName(rDomainTrust.DnsDomainName ? rDomainTrust.DnsDomainName : rDomainTrust.NetbiosDomainName);
  802. // trust direction
  803. _bstr_t bstrDirection;
  804. switch (rDomainTrust.Flags & (DS_DOMAIN_DIRECT_INBOUND|DS_DOMAIN_DIRECT_OUTBOUND))
  805. {
  806. case DS_DOMAIN_DIRECT_INBOUND:
  807. bstrDirection = GET_BSTR(IDS_TRUST_DIRECTION_INBOUND);
  808. break;
  809. case DS_DOMAIN_DIRECT_OUTBOUND:
  810. bstrDirection = GET_BSTR(IDS_TRUST_DIRECTION_OUTBOUND);
  811. break;
  812. case DS_DOMAIN_DIRECT_INBOUND|DS_DOMAIN_DIRECT_OUTBOUND:
  813. bstrDirection = GET_BSTR(IDS_TRUST_DIRECTION_BIDIRECTIONAL);
  814. break;
  815. default:
  816. // bstrDirection = ;
  817. break;
  818. }
  819. // trust relationship
  820. _bstr_t bstrRelationship;
  821. if (ulIndex == ulParentIndex)
  822. {
  823. bstrRelationship = GET_BSTR(IDS_TRUST_RELATION_PARENT);
  824. }
  825. else if (rDomainTrust.Flags & DS_DOMAIN_IN_FOREST)
  826. {
  827. if (rDomainTrust.ParentIndex == ulDomainIndex)
  828. {
  829. bstrRelationship = GET_BSTR(IDS_TRUST_RELATION_CHILD);
  830. }
  831. else if ((rDomainTrust.Flags & DS_DOMAIN_TREE_ROOT) && (pDomainTrusts[ulDomainIndex].Flags & DS_DOMAIN_TREE_ROOT))
  832. {
  833. bstrRelationship = GET_BSTR(IDS_TRUST_RELATION_ROOT);
  834. }
  835. else
  836. {
  837. bstrRelationship = GET_BSTR(IDS_TRUST_RELATION_SHORTCUT);
  838. }
  839. }
  840. else
  841. {
  842. switch (rDomainTrust.TrustType)
  843. {
  844. case TRUST_TYPE_DOWNLEVEL:
  845. case TRUST_TYPE_UPLEVEL:
  846. bstrRelationship = (rDomainTrust.TrustAttributes & TRUST_ATTRIBUTE_FOREST_TRANSITIVE) ? GET_BSTR(IDS_TRUST_RELATION_FOREST) : GET_BSTR(IDS_TRUST_RELATION_EXTERNAL);
  847. break;
  848. case TRUST_TYPE_MIT:
  849. bstrRelationship = GET_BSTR(IDS_TRUST_RELATION_MIT);
  850. break;
  851. default:
  852. bstrRelationship = GET_BSTR(IDS_TRUST_RELATION_UNKNOWN);
  853. break;
  854. }
  855. }
  856. if (bIsTarget)
  857. {
  858. // if same trust was found on source domain and the trust
  859. // directions match update exists for target to yes
  860. LONG lSourceIndex = FindInboundTrust(pVarSet, bstrName, ndxTrust);
  861. if (lSourceIndex >= 0)
  862. {
  863. // get source trust direction
  864. swprintf(key, L"Trusts.%ld.Direction", lSourceIndex);
  865. _bstr_t bstrSourceDirection = pVarSet->get(key);
  866. // if target trust direction is bi-directional or
  867. // target trust direction equals source trust direction
  868. // then set exists for target to yes
  869. bool bExists = false;
  870. if (bstrDirection == GET_BSTR(IDS_TRUST_DIRECTION_BIDIRECTIONAL))
  871. {
  872. bExists = true;
  873. }
  874. else if (bstrDirection == bstrSourceDirection)
  875. {
  876. bExists = true;
  877. }
  878. if (bExists)
  879. {
  880. swprintf(key, L"Trusts.%ld.ExistsForTarget", lSourceIndex);
  881. pVarSet->put(key, GET_BSTR(IDS_YES));
  882. // write trust directions to log
  883. if (rDomainTrust.Flags & DS_DOMAIN_DIRECT_OUTBOUND)
  884. {
  885. err.MsgWrite(0, DCT_MSG_TARGET_TRUSTS_THIS_SS, domain, (LPCTSTR)bstrName);
  886. }
  887. if (rDomainTrust.Flags & DS_DOMAIN_DIRECT_INBOUND)
  888. {
  889. err.MsgWrite(0, DCT_MSG_TARGET_TRUSTED_BY_THIS_SS, domain, (LPCTSTR)bstrName);
  890. }
  891. }
  892. }
  893. }
  894. else
  895. {
  896. // domain name
  897. swprintf(key, L"Trusts.%ld", ndxTrust);
  898. pVarSet->put(key, bstrName);
  899. // trust direction
  900. swprintf(key, L"Trusts.%ld.Direction", ndxTrust);
  901. pVarSet->put(key, bstrDirection);
  902. // trust relationship
  903. if (bstrRelationship.length() > 0)
  904. {
  905. swprintf(key, L"Trusts.%ld.Type", ndxTrust);
  906. pVarSet->put(key, bstrRelationship);
  907. }
  908. // trust exists on target
  909. // initially set to no until target domain is enumerated
  910. swprintf(key, L"Trusts.%ld.ExistsForTarget", ndxTrust);
  911. pVarSet->put(key, GET_BSTR(IDS_No));
  912. // write trust directions to log
  913. if (rDomainTrust.Flags & DS_DOMAIN_DIRECT_OUTBOUND)
  914. {
  915. err.MsgWrite(0, DCT_MSG_SOURCE_TRUSTS_THIS_SS, (LPCTSTR)bstrName, domain);
  916. }
  917. if (rDomainTrust.Flags & DS_DOMAIN_DIRECT_INBOUND)
  918. {
  919. err.MsgWrite(0, DCT_MSG_SOURCE_IS_TRUSTED_BY_THIS_SS, (LPCTSTR)bstrName, domain);
  920. }
  921. ++ndxTrust;
  922. }
  923. }
  924. NetApiBufferFree(pDomainTrusts);
  925. }
  926. else
  927. {
  928. err.SysMsgWrite(ErrE, dwError, DCT_MSG_TRUSTED_ENUM_FAILED_SD, domain, dwError);
  929. }
  930. }
  931. }
  932. if ( bIsTarget )
  933. {
  934. // make sure we have "Yes" for the target domain itself
  935. long ndx2 = FindInboundTrust(pVarSet,domain,ndxTrust);
  936. if ( ndx2 != -1 )
  937. {
  938. swprintf(key,L"Trusts.%ld.ExistsForTarget",ndx2);
  939. //* pVarSet->put(key,L"Yes");
  940. pVarSet->put(key,GET_BSTR(IDS_YES));
  941. }
  942. }
  943. return ndxTrust;
  944. }
  945. long CTrust::EnumerateTrustingDomains(WCHAR * domain,BOOL bIsTarget,IVarSet * pVarSet,long ndxStart)
  946. {
  947. DWORD rcOs; // OS return code
  948. DWORD hEnum=0; // enumeration handle
  949. USER_INFO_1 * pNetUsers=NULL; // NetUserEnum array buffer
  950. USER_INFO_1 * pNetUser; // NetUserEnum array item
  951. DWORD nRead; // Entries read.
  952. DWORD nTotal; // Entries total.
  953. WCHAR sName[LEN_Domain]; // Domain name
  954. WCHAR * pNameEnd; // Null at end
  955. _bstr_t computer;
  956. DOMAIN_CONTROLLER_INFO * pInfo;
  957. long ndx = ndxStart;
  958. WCHAR key[100];
  959. err.MsgWrite(0,DCT_MSG_ENUMERATING_TRUSTING_DOMAINS_S,domain);
  960. rcOs = GetAnyDcName5(domain, computer);
  961. if ( rcOs )
  962. {
  963. return ndx;
  964. }
  965. // get the trusting domains for the NT 4 domain
  966. // for Win2K domains, the trusting domains will be listed as Incoming in the Trusted Domain enumeration
  967. if ( IsDownLevel(computer) )
  968. {
  969. do
  970. {
  971. nRead = 0;
  972. nTotal = 0;
  973. rcOs = NetUserEnum(
  974. computer,
  975. 1,
  976. FILTER_INTERDOMAIN_TRUST_ACCOUNT,
  977. (BYTE **) &pNetUsers,
  978. 10240,
  979. &nRead,
  980. &nTotal,
  981. &hEnum );
  982. switch ( rcOs )
  983. {
  984. case 0:
  985. case ERROR_MORE_DATA:
  986. for ( pNetUser = pNetUsers;
  987. pNetUser < pNetUsers + nRead;
  988. pNetUser++ )
  989. {
  990. // skip trust accounts whose password age is older than 30 days to avoid
  991. // delays caused by trying to enumerate defunct trusts
  992. if ( pNetUser->usri1_password_age > 60 * 60 * 24 * 30 ) // 30 days (age is in seconds)
  993. {
  994. err.MsgWrite(0,DCT_MSG_SKIPPING_OLD_TRUST_SD,pNetUser->usri1_name,
  995. pNetUser->usri1_password_age / ( 60*60*24) );
  996. continue;
  997. }
  998. safecopy( sName, pNetUser->usri1_name );
  999. pNameEnd = sName + UStrLen( sName );
  1000. if ( (pNameEnd > sName) && (pNameEnd[-1] == L'$') )
  1001. {
  1002. pNameEnd[-1] = L'\0';
  1003. }
  1004. if ( *sName )
  1005. {
  1006. // Found a (probably) valid trust!
  1007. if ( ! bIsTarget )
  1008. {
  1009. // for the source domain, simply add the trusts to the list in the varset
  1010. swprintf(key,L"Trusts.%ld",ndx);
  1011. pVarSet->put(key,sName);
  1012. swprintf(key,L"Trusts.%ld.Direction",ndx);
  1013. //* pVarSet->put(key,L"Inbound");
  1014. pVarSet->put(key,GET_BSTR(IDS_TRUST_DIRECTION_INBOUND));
  1015. swprintf(key,L"Trusts.%ld.Type",ndx);
  1016. pVarSet->put(key, GET_BSTR(IDS_TRUST_RELATION_EXTERNAL));
  1017. swprintf(key,L"Trusts.%ld.ExistsForTarget",ndx);
  1018. //* pVarSet->put(key,L"No");
  1019. pVarSet->put(key,GET_BSTR(IDS_No));
  1020. err.MsgWrite(0,DCT_MSG_SOURCE_IS_TRUSTED_BY_THIS_SS,sName,domain);
  1021. ndx++;
  1022. }
  1023. else
  1024. {
  1025. // for the target domain, look for this trust in the varset
  1026. // and if it is there, mark that it exists on the target
  1027. long ndxTemp = FindInboundTrust(pVarSet,sName,ndxStart);
  1028. if ( ndxTemp != -1 )
  1029. {
  1030. swprintf(key,L"Trusts.%ld.ExistsForTarget",ndxTemp);
  1031. //* pVarSet->put(key,L"Yes");
  1032. pVarSet->put(key,GET_BSTR(IDS_YES));
  1033. err.MsgWrite(0,DCT_MSG_TARGET_TRUSTS_THIS_SS,sName,domain);
  1034. }
  1035. }
  1036. }
  1037. }
  1038. break;
  1039. default:
  1040. break;
  1041. }
  1042. if ( pNetUsers )
  1043. {
  1044. NetApiBufferFree( pNetUsers );
  1045. pNetUsers = NULL;
  1046. }
  1047. } while ( rcOs == ERROR_MORE_DATA );
  1048. }
  1049. // Win2K domain, don't need to enumerate the trusting domains here - they will all be included in the
  1050. // trusted domain enum
  1051. return ndx;
  1052. }
  1053. /*void CheckProc(void * arg,void * data)
  1054. {
  1055. CTrust * tr = (CTrust*)arg;
  1056. }*/
  1057. STDMETHODIMP CTrust::PreMigrationTask(/* [in] */IUnknown * pVarSet)
  1058. {
  1059. /* IVarSetPtr pVS = pVarSet;
  1060. BOOL bCreate;
  1061. _bstr_t source = pVS->get(GET_BSTR(DCTVS_Options_SourceDomain));
  1062. _bstr_t target = pVS->get(GET_BSTR(DCTVS_Options_TargetDomain));
  1063. _bstr_t logfile = pVS->get(GET_BSTR(DCTVS_Options_Logfile));
  1064. _bstr_t localOnly = pVS->get(GET_BSTR(DCTVS_Options_LocalProcessingOnly));
  1065. _bstr_t docreate = pVS->get(L"Options.CreateTrusts");
  1066. if ( !UStrICmp(localOnly,GET_STRING(IDS_YES)) )
  1067. {
  1068. // don't do anything in local agent mode
  1069. return S_OK;
  1070. }
  1071. if ( !UStrICmp(docreate,GET_STRING(IDS_YES)) )
  1072. {
  1073. bCreate = TRUE;
  1074. }
  1075. else
  1076. {
  1077. bCreate = FALSE;
  1078. }
  1079. pVS->put(GET_BSTR(DCTVS_CurrentOperation),L"Verifying trust relationships");
  1080. err.LogOpen(logfile,1);
  1081. err.LevelBeepSet(1000);
  1082. err.LogClose();
  1083. */
  1084. return S_OK;
  1085. }
  1086. STDMETHODIMP CTrust::PostMigrationTask(/* [in] */IUnknown * pVarSet)
  1087. {
  1088. return S_OK;
  1089. }
  1090. STDMETHODIMP CTrust::GetName(/* [out] */BSTR * name)
  1091. {
  1092. (*name) = SysAllocString(L"Trust Manager");
  1093. return S_OK;
  1094. }
  1095. STDMETHODIMP CTrust::GetResultString(/* [in] */IUnknown * pVarSet,/* [out] */ BSTR * text)
  1096. {
  1097. WCHAR buffer[100] = L"";
  1098. IVarSetPtr pVS;
  1099. pVS = pVarSet;
  1100. (*text) = SysAllocString(buffer);
  1101. return S_OK;
  1102. }
  1103. STDMETHODIMP CTrust::StoreResults(/* [in] */IUnknown * pVarSet)
  1104. {
  1105. return S_OK;
  1106. }
  1107. STDMETHODIMP CTrust::ConfigureSettings(/*[in]*/IUnknown * pVarSet)
  1108. {
  1109. return S_OK;
  1110. }
  1111. STDMETHODIMP CTrust::QueryTrusts(BSTR domainSource,BSTR domainTarget, BSTR sLogFile, IUnknown **pVarSet)
  1112. {
  1113. HRESULT hr = S_OK;
  1114. IVarSetPtr pVS(CLSID_VarSet);
  1115. long ndx;
  1116. _bstr_t sFile = sLogFile;
  1117. err.LogOpen((WCHAR*) sFile, 1);
  1118. err.LevelBeepSet(1000);
  1119. hr = pVS.QueryInterface(IID_IUnknown,(long**)pVarSet);
  1120. // Add a blank line to help differentiate different runs
  1121. err.MsgWrite(0,DCT_MSG_GENERIC_S,L"");
  1122. ndx = EnumerateTrustingDomains(domainSource,FALSE,pVS,0);
  1123. EnumerateTrustingDomains(domainTarget,TRUE,pVS,0);
  1124. ndx = EnumerateTrustedDomains(domainSource,FALSE,pVS,ndx);
  1125. EnumerateTrustedDomains(domainTarget,TRUE,pVS,ndx);
  1126. //err.LogClose();
  1127. pVS->put(L"Trusts",ndx);
  1128. return hr;
  1129. }