Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1090 lines
26 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: connectui.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #include <SnapBase.h>
  12. #include "resource.h"
  13. #include "connection.h"
  14. #include "connectionui.h"
  15. #include <aclpage.h>
  16. #ifdef DEBUG_ALLOCATOR
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. #endif
  23. ////////////////////////////////////////////////////////////////////////////////
  24. extern LPCWSTR g_lpszGC;
  25. extern LPCWSTR g_lpszLDAP;
  26. extern LPCWSTR g_lpszRootDSE;
  27. ///////////////////////////////////////////////////////////////////////////////
  28. BEGIN_MESSAGE_MAP(CADSIEditConnectDialog, CDialog)
  29. //{{AFX_MSG_MAP(CADsObjectDialog)
  30. ON_CBN_SELCHANGE(IDC_NC_BOX, OnSelChangeContextList)
  31. ON_CBN_SELCHANGE(IDC_DOMAIN_SERVER_BOX, OnSelChangeDSList)
  32. ON_CBN_SELCHANGE(IDC_DN_BOX, OnSelChangeDNList)
  33. ON_CBN_EDITCHANGE(IDC_DOMAIN_SERVER_BOX, OnEditChangeDSList)
  34. ON_CBN_EDITCHANGE(IDC_DN_BOX, OnEditChangeDNList)
  35. ON_BN_CLICKED(IDC_DN_RADIO, OnDNRadio)
  36. ON_BN_CLICKED(IDC_NC_RADIO, OnNCRadio)
  37. ON_BN_CLICKED(IDC_DOMAIN_SERVER_RADIO, OnDSRadio)
  38. ON_BN_CLICKED(IDC_DEFAULT_RADIO, OnDefaultRadio)
  39. ON_BN_CLICKED(IDC_ADVANCED_BUTTON, OnAdvanced)
  40. //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42. CADSIEditConnectDialog::CADSIEditConnectDialog(CContainerNode* pRootnode,
  43. CTreeNode* pTreeNode,
  44. CComponentDataObject* pComponentData,
  45. CConnectionData* pConnectData
  46. ) : CDialog(IDD_CONNECTION_DIALOG)
  47. {
  48. m_pContainerNode = pRootnode;
  49. m_pTreeNode = pTreeNode;
  50. m_pComponentData = pComponentData;
  51. m_pNewConnectData = pConnectData;
  52. m_szDisplayExtra = L"";
  53. m_sDefaultServerName = L"";
  54. }
  55. CADSIEditConnectDialog::~CADSIEditConnectDialog()
  56. {
  57. if (m_bNewConnect && m_pNewConnectData != NULL)
  58. delete m_pNewConnectData;
  59. }
  60. BOOL CADSIEditConnectDialog::OnInitDialog()
  61. {
  62. CDialog::OnInitDialog();
  63. CConnectionData* pConnectData = GetConnectionData();
  64. if (pConnectData == NULL)
  65. {
  66. m_pNewConnectData = new CConnectionData();
  67. if (m_pNewConnectData)
  68. {
  69. m_bNewConnect = TRUE;
  70. CString sServerName;
  71. m_pNewConnectData->GetDomainServer(sServerName);
  72. if (sServerName == L"")
  73. {
  74. HRESULT hr = CConnectionData::GetServerNameFromDefault(m_pNewConnectData);
  75. if (SUCCEEDED(hr))
  76. {
  77. m_pNewConnectData->GetDomainServer(m_sDefaultServerName);
  78. }
  79. }
  80. m_pNewConnectData->GetDomainServer(m_szDisplayExtra);
  81. }
  82. }
  83. else
  84. {
  85. m_pNewConnectData = pConnectData;
  86. m_bNewConnect = FALSE;
  87. }
  88. LoadNamingContext();
  89. SetupUI();
  90. SetDirty();
  91. return TRUE;
  92. }
  93. void CADSIEditConnectDialog::LoadNamingContext()
  94. {
  95. CComboBox* pcNCBox = (CComboBox*)GetDlgItem(IDC_NC_BOX);
  96. m_szDomain.LoadString(IDS_DOMAIN_NC);
  97. m_szConfigContainer.LoadString(IDS_CONFIG_CONTAINER);
  98. m_szRootDSE.LoadString(IDS_ROOTDSE);
  99. m_szSchema.LoadString(IDS_SCHEMA);
  100. pcNCBox->AddString(m_szDomain);
  101. pcNCBox->AddString(m_szConfigContainer);
  102. pcNCBox->AddString(m_szRootDSE);
  103. pcNCBox->AddString(m_szSchema);
  104. }
  105. void CADSIEditConnectDialog::SetupUI()
  106. {
  107. CComboBox* pcDNBox = (CComboBox*)GetDlgItem(IDC_DN_BOX);
  108. CComboBox* pcNCBox = (CComboBox*)GetDlgItem(IDC_NC_BOX);
  109. CComboBox* pcDomainServerBox = (CComboBox*)GetDlgItem(IDC_DOMAIN_SERVER_BOX);
  110. CButton* pcDNRadio = (CButton*)GetDlgItem(IDC_DN_RADIO);
  111. CButton* pcNCRadio = (CButton*)GetDlgItem(IDC_NC_RADIO);
  112. CButton* pcDSRadio = (CButton*)GetDlgItem(IDC_DOMAIN_SERVER_RADIO);
  113. CButton* pcDefaultRadio = (CButton*)GetDlgItem(IDC_DEFAULT_RADIO);
  114. CEdit* pcNameBox = (CEdit*)GetDlgItem(IDC_CONNECTION_NAME);
  115. //Setup UI to reflect data
  116. LoadMRUs();
  117. CString sDistinguishedName;
  118. m_pNewConnectData->GetDistinguishedName(sDistinguishedName);
  119. if (!sDistinguishedName.IsEmpty())
  120. {
  121. int iIndex = pcDNBox->FindStringExact(-1, sDistinguishedName);
  122. if (iIndex != CB_ERR)
  123. {
  124. pcDNBox->SetCurSel(iIndex);
  125. }
  126. else
  127. {
  128. int nIndex = pcDNBox->AddString(sDistinguishedName);
  129. pcDNBox->SetCurSel(nIndex);
  130. }
  131. OnSelChangeDNList();
  132. pcDNRadio->SetCheck(BST_CHECKED);
  133. }
  134. else
  135. {
  136. CString sNamingContext;
  137. m_pNewConnectData->GetNamingContext(sNamingContext);
  138. int iIndex = pcNCBox->FindStringExact(-1, sNamingContext);
  139. if (iIndex != CB_ERR)
  140. {
  141. pcNCBox->SetCurSel(iIndex);
  142. }
  143. else
  144. {
  145. pcNCBox->SetCurSel(0);
  146. }
  147. OnSelChangeContextList();
  148. pcNCRadio->SetCheck(BST_CHECKED);
  149. }
  150. CString sServer;
  151. m_pNewConnectData->GetDomainServer(sServer);
  152. BOOL bUserDefinedServer;
  153. bUserDefinedServer = m_pNewConnectData->GetUserDefinedServer();
  154. if (!sServer.IsEmpty() && bUserDefinedServer)
  155. {
  156. int iIndex = pcDomainServerBox->FindStringExact(-1, sServer);
  157. if (iIndex != CB_ERR)
  158. {
  159. pcDomainServerBox->SetCurSel(iIndex);
  160. }
  161. else
  162. {
  163. int nIndex = pcDomainServerBox->AddString(sServer);
  164. pcDomainServerBox->SetCurSel(nIndex);
  165. }
  166. OnSelChangeDSList();
  167. pcDSRadio->SetCheck(BST_CHECKED);
  168. }
  169. else
  170. {
  171. pcDefaultRadio->SetCheck(BST_CHECKED);
  172. }
  173. CString sName;
  174. m_pNewConnectData->GetName(sName);
  175. pcNameBox->SetLimitText(MAX_CONNECT_NAME_LENGTH);
  176. if (sName.IsEmpty())
  177. {
  178. if (pcNCRadio->GetCheck())
  179. {
  180. CString szNCName;
  181. pcNCBox->GetLBText(pcNCBox->GetCurSel(), szNCName);
  182. pcNameBox->SetWindowText(szNCName);
  183. m_pNewConnectData->GetDomainServer(m_szDisplayExtra);
  184. }
  185. else
  186. {
  187. CString szMyConnection;
  188. szMyConnection.LoadString(IDS_MY_CONNECTION);
  189. pcNameBox->SetWindowText(szMyConnection);
  190. }
  191. }
  192. else
  193. {
  194. pcNameBox->SetWindowText(sName);
  195. }
  196. SetAndDisplayPath();
  197. }
  198. void CADSIEditConnectDialog::LoadMRUs()
  199. {
  200. CComboBox* pcDomainServerBox = (CComboBox*)GetDlgItem(IDC_DOMAIN_SERVER_BOX);
  201. CComboBox* pcDNBox = (CComboBox*)GetDlgItem(IDC_DN_BOX);
  202. CADSIEditRootData* pRootNode = GetRootNode();
  203. CStringList sServerMRU, sDNMRU;
  204. pRootNode->GetServerMRU(&sServerMRU);
  205. pRootNode->GetDNMRU(&sDNMRU);
  206. POSITION pos = sServerMRU.GetHeadPosition();
  207. while (pos != NULL)
  208. {
  209. CString sMRU;
  210. sMRU = sServerMRU.GetNext(pos);
  211. pcDomainServerBox->AddString(sMRU);
  212. }
  213. pos = sDNMRU.GetHeadPosition();
  214. while (pos != NULL)
  215. {
  216. CString sMRU;
  217. sMRU = sDNMRU.GetNext(pos);
  218. pcDNBox->AddString(sMRU);
  219. }
  220. }
  221. void CADSIEditConnectDialog::SaveMRUs()
  222. {
  223. CADSIEditRootData* pRootNode = GetRootNode();
  224. CStringList sDNMRU, sServerMRU;
  225. CString sDS, sDN;
  226. BOOL bFound = FALSE;
  227. m_pNewConnectData->GetDistinguishedName(sDN);
  228. m_pNewConnectData->GetDomainServer(sDS);
  229. pRootNode->GetServerMRU(&sServerMRU);
  230. pRootNode->GetDNMRU(&sDNMRU);
  231. POSITION pos = sServerMRU.GetHeadPosition();
  232. while (pos != NULL)
  233. {
  234. CString sServer;
  235. sServer = sServerMRU.GetNext(pos);
  236. if (sServer == sDS)
  237. {
  238. bFound = TRUE;
  239. break;
  240. }
  241. }
  242. if (!bFound && !sDS.IsEmpty())
  243. {
  244. sServerMRU.AddHead(sDS);
  245. pRootNode->SetServerMRU(&sServerMRU);
  246. }
  247. bFound = FALSE;
  248. pos = sDNMRU.GetHeadPosition();
  249. while (pos != NULL)
  250. {
  251. CString sDistinguishedName;
  252. sDistinguishedName = sDNMRU.GetNext(pos);
  253. if (sDistinguishedName == sDN)
  254. {
  255. bFound = TRUE;
  256. break;
  257. }
  258. }
  259. if (!bFound && !sDN.IsEmpty())
  260. {
  261. sDNMRU.AddHead(sDN);
  262. pRootNode->SetDNMRU(&sDNMRU);
  263. }
  264. }
  265. void CADSIEditConnectDialog::OnDNRadio()
  266. {
  267. SetDirty();
  268. SetAndDisplayPath();
  269. }
  270. void CADSIEditConnectDialog::OnNCRadio()
  271. {
  272. SetDirty();
  273. SetAndDisplayPath();
  274. }
  275. void CADSIEditConnectDialog::OnDSRadio()
  276. {
  277. CComboBox* pcDomainServerBox = (CComboBox*)GetDlgItem(IDC_DOMAIN_SERVER_BOX);
  278. CString szDS;
  279. pcDomainServerBox->GetWindowText(szDS);
  280. m_pNewConnectData->SetDomainServer(szDS);
  281. m_pNewConnectData->SetUserDefinedServer(TRUE);
  282. SetDirty();
  283. SetAndDisplayPath();
  284. }
  285. void CADSIEditConnectDialog::OnDefaultRadio()
  286. {
  287. m_pNewConnectData->SetUserDefinedServer(FALSE);
  288. if (m_pNewConnectData->IsGC())
  289. {
  290. m_pNewConnectData->SetDomainServer(L"");
  291. }
  292. else
  293. {
  294. m_pNewConnectData->SetDomainServer(m_sDefaultServerName);
  295. }
  296. SetDirty();
  297. SetAndDisplayPath();
  298. }
  299. void CADSIEditConnectDialog::SetAndDisplayPath()
  300. {
  301. CEdit* pcPathBox = (CEdit*)GetDlgItem(IDC_FULLPATH_BOX);
  302. CButton* pcDSRadio = (CButton*)GetDlgItem(IDC_DOMAIN_SERVER_RADIO);
  303. CButton* pcDNRadio = (CButton*)GetDlgItem(IDC_DN_RADIO);
  304. CComboBox* pcNCBox = (CComboBox*)GetDlgItem(IDC_NC_BOX);
  305. // Get data from connection node
  306. //
  307. CString szLDAP, sServer, sPort, sDistinguishedName, sNamingContext;
  308. m_pNewConnectData->GetLDAP(szLDAP);
  309. m_pNewConnectData->GetDomainServer(sServer);
  310. m_pNewConnectData->GetPort(sPort);
  311. m_pNewConnectData->GetDistinguishedName(sDistinguishedName);
  312. m_pNewConnectData->GetNamingContext(sNamingContext);
  313. m_pNewConnectData->SetRootDSE(FALSE);
  314. CString szFullPath;
  315. if (!sServer.IsEmpty())
  316. {
  317. szFullPath = szFullPath + sServer;
  318. if (!sPort.IsEmpty())
  319. {
  320. szFullPath = szFullPath + _T(":") + sPort + _T("/");
  321. }
  322. else
  323. {
  324. szFullPath = szFullPath + _T("/");
  325. }
  326. }
  327. if (pcDNRadio->GetCheck() && !sDistinguishedName.IsEmpty())
  328. {
  329. szFullPath = szFullPath + sDistinguishedName;
  330. if (wcscmp(sDistinguishedName, g_lpszRootDSE) == 0)
  331. {
  332. m_pNewConnectData->SetRootDSE(TRUE);
  333. }
  334. }
  335. else
  336. {
  337. szFullPath = szFullPath + sNamingContext;
  338. if (wcscmp(sNamingContext, g_lpszRootDSE) == 0)
  339. {
  340. m_pNewConnectData->SetRootDSE(TRUE);
  341. }
  342. }
  343. m_pNewConnectData->GetDomainServer(m_szDisplayExtra);
  344. m_szDisplayExtra = L" [" + m_szDisplayExtra + L"]";
  345. szFullPath = szLDAP + szFullPath;
  346. pcPathBox->SetWindowText(szFullPath);
  347. }
  348. void CADSIEditConnectDialog::OnSelChangeContextList()
  349. {
  350. CComboBox* pcNCBox = (CComboBox*)GetDlgItem(IDC_NC_BOX);
  351. CButton* pcDNRadio = (CButton*)GetDlgItem(IDC_DN_RADIO);
  352. CButton* pcNCRadio = (CButton*)GetDlgItem(IDC_NC_RADIO);
  353. CEdit* pcNameBox = (CEdit*)GetDlgItem(IDC_CONNECTION_NAME);
  354. CString szContext;
  355. SetDirty();
  356. pcNCBox->GetLBText(pcNCBox->GetCurSel(), szContext);
  357. m_pNewConnectData->SetNamingContext(szContext);
  358. pcNCRadio->SetCheck(BST_CHECKED);
  359. pcDNRadio->SetCheck(BST_UNCHECKED);
  360. CString sName;
  361. pcNCBox->GetLBText(pcNCBox->GetCurSel(), sName);
  362. pcNameBox->SetWindowText(sName);
  363. SetAndDisplayPath();
  364. }
  365. void CADSIEditConnectDialog::OnSelChangeDSList()
  366. {
  367. CComboBox* pcDomainServerBox = (CComboBox*)GetDlgItem(IDC_DOMAIN_SERVER_BOX);
  368. CButton* pcDSRadio = (CButton*)GetDlgItem(IDC_DOMAIN_SERVER_RADIO);
  369. CButton* pcDefaultRadio = (CButton*)GetDlgItem(IDC_DEFAULT_RADIO);
  370. SetDirty();
  371. if (pcDomainServerBox->GetCount() > 0)
  372. {
  373. CString sServer;
  374. pcDomainServerBox->GetLBText(pcDomainServerBox->GetCurSel(), sServer);
  375. m_pNewConnectData->SetDomainServer(sServer);
  376. m_pNewConnectData->SetUserDefinedServer(TRUE);
  377. }
  378. pcDSRadio->SetCheck(BST_CHECKED);
  379. pcDefaultRadio->SetCheck(BST_UNCHECKED);
  380. SetAndDisplayPath();
  381. }
  382. void CADSIEditConnectDialog::OnSelChangeDNList()
  383. {
  384. CComboBox* pcDNBox = (CComboBox*)GetDlgItem(IDC_DN_BOX);
  385. CButton* pcDNRadio = (CButton*)GetDlgItem(IDC_DN_RADIO);
  386. CButton* pcNCRadio = (CButton*)GetDlgItem(IDC_NC_RADIO);
  387. SetDirty();
  388. if (pcDNBox->GetCount() > 0)
  389. {
  390. CString sDistinguishedName;
  391. pcDNBox->GetLBText(pcDNBox->GetCurSel(), sDistinguishedName);
  392. m_pNewConnectData->SetDistinguishedName(sDistinguishedName);
  393. }
  394. pcDNRadio->SetCheck(BST_CHECKED);
  395. pcNCRadio->SetCheck(BST_UNCHECKED);
  396. SetAndDisplayPath();
  397. }
  398. void CADSIEditConnectDialog::OnEditChangeDSList()
  399. {
  400. CComboBox* pcDomainServerBox = (CComboBox*)GetDlgItem(IDC_DOMAIN_SERVER_BOX);
  401. CButton* pcDSRadio = (CButton*)GetDlgItem(IDC_DOMAIN_SERVER_RADIO);
  402. CButton* pcDefaultRadio = (CButton*)GetDlgItem(IDC_DEFAULT_RADIO);
  403. SetDirty();
  404. CString szDS, sOldDS;
  405. pcDomainServerBox->GetWindowText(szDS);
  406. m_pNewConnectData->SetDomainServer(szDS);
  407. pcDSRadio->SetCheck(BST_CHECKED);
  408. pcDefaultRadio->SetCheck(BST_UNCHECKED);
  409. SetAndDisplayPath();
  410. m_pNewConnectData->SetUserDefinedServer(TRUE);
  411. }
  412. void CADSIEditConnectDialog::OnEditChangeDNList()
  413. {
  414. CComboBox* pcDNBox = (CComboBox*)GetDlgItem(IDC_DN_BOX);
  415. CButton* pcDNRadio = (CButton*)GetDlgItem(IDC_DN_RADIO);
  416. CButton* pcNCRadio = (CButton*)GetDlgItem(IDC_NC_RADIO);
  417. SetDirty();
  418. CString s, sOldDN;
  419. pcDNBox->GetWindowText(s);
  420. m_pNewConnectData->SetDistinguishedName(s);
  421. pcDNRadio->SetCheck(BST_CHECKED);
  422. pcNCRadio->SetCheck(BST_UNCHECKED);
  423. SetAndDisplayPath();
  424. }
  425. void CADSIEditConnectDialog::OnAdvanced()
  426. {
  427. CWaitCursor cursor;
  428. CADSIEditRootData* pRootNode = GetRootNode();
  429. CADSIEditAdvancedConnectionDialog AdvancedDialog(NULL,
  430. pRootNode,
  431. m_pComponentData,
  432. m_pNewConnectData);
  433. if (AdvancedDialog.DoModal() == IDOK)
  434. {
  435. cursor.Restore();
  436. if (m_pNewConnectData->IsGC() && !m_pNewConnectData->GetUserDefinedServer())
  437. {
  438. m_pNewConnectData->SetDomainServer(L"");
  439. }
  440. else if (!m_pNewConnectData->IsGC())
  441. {
  442. CConnectionData::GetServerNameFromDefault(m_pNewConnectData);
  443. }
  444. SetDirty();
  445. SetAndDisplayPath();
  446. }
  447. }
  448. void CADSIEditConnectDialog::OnOK()
  449. {
  450. if (OnApply())
  451. {
  452. CDialog::OnOK();
  453. }
  454. }
  455. BOOL CADSIEditConnectDialog::OnApply()
  456. {
  457. CEdit* pcNameBox = (CEdit*)GetDlgItem(IDC_CONNECTION_NAME);
  458. CADSIEditRootData* pRootNode = GetRootNode();
  459. ASSERT(pRootNode != NULL);
  460. CComponentDataObject* pComponentData = GetComponentData();
  461. BSTR bstrPath;
  462. CString sName;
  463. pcNameBox->GetWindowText(sName);
  464. m_pNewConnectData->SetName(sName);
  465. if (m_bDirty)
  466. {
  467. if (!DoDirty())
  468. {
  469. return FALSE;
  470. }
  471. }
  472. else
  473. {
  474. if (pRootNode->GetDisplayName() != sName)
  475. {
  476. pRootNode->SetDisplayName(sName + m_szDisplayExtra);
  477. }
  478. else
  479. {
  480. ADSIEditMessageBox(IDS_MSG_CONNECTION_NAME, MB_OK);
  481. return FALSE;
  482. }
  483. }
  484. return TRUE;
  485. }
  486. BOOL CADSIEditConnectDialog::DoDirty()
  487. {
  488. CEdit* pcNameBox = (CEdit*)GetDlgItem(IDC_CONNECTION_NAME);
  489. CADSIEditContainerNode* pTreeNode = dynamic_cast<CADSIEditContainerNode*>(GetTreeNode());
  490. CADSIEditRootData* pRootNode = GetRootNode();
  491. ASSERT(pRootNode != NULL);
  492. CComponentDataObject* pComponentData = GetComponentData();
  493. SaveMRUs();
  494. CString sRootDSE, s;
  495. BuildRootDSE(sRootDSE);
  496. CComPtr<IADs> spRootADs;
  497. HRESULT hr, hCredResult;
  498. hr = OpenObjectWithCredentials(
  499. m_pNewConnectData,
  500. m_pNewConnectData->GetCredentialObject()->UseCredentials(),
  501. (LPWSTR)(LPCWSTR)sRootDSE,
  502. IID_IADs,
  503. (LPVOID*) &spRootADs,
  504. GetSafeHwnd(),
  505. hCredResult
  506. );
  507. if ( FAILED(hr) )
  508. {
  509. if (SUCCEEDED(hCredResult))
  510. {
  511. ADSIEditErrorMessage(hr);
  512. }
  513. return FALSE;
  514. }
  515. CString sNamingContext, sDistinguishedName, sServerName;
  516. m_pNewConnectData->GetNamingContext(sNamingContext);
  517. m_pNewConnectData->GetDistinguishedName(sDistinguishedName);
  518. if ( m_pNewConnectData->IsRootDSE())
  519. {
  520. s = g_lpszRootDSE;
  521. if (!m_bNewConnect)
  522. {
  523. CString sName;
  524. pcNameBox->GetWindowText(sName);
  525. m_pNewConnectData->SetName(sName);
  526. CString szProvider, sServer, sPort, sPath;
  527. m_pNewConnectData->GetDomainServer(sServer);
  528. m_pNewConnectData->GetPort(sPort);
  529. m_pNewConnectData->GetLDAP(szProvider);
  530. if (sServer != _T(""))
  531. {
  532. if (sPort != _T(""))
  533. {
  534. sPath = szProvider + sServer + _T(":") + sPort + _T("/") + CString(sName);
  535. }
  536. else
  537. {
  538. sPath = szProvider + sServer + _T("/") + CString(sName);
  539. }
  540. }
  541. else
  542. {
  543. sPath = szProvider + CString(sName);
  544. }
  545. m_pNewConnectData->SetPath(sPath);
  546. ASSERT(pTreeNode != NULL);
  547. pTreeNode->SetDisplayName(sName + m_szDisplayExtra);
  548. m_pNewConnectData->SetBasePath(_T(""));
  549. }
  550. else
  551. {
  552. // Name
  553. LPWSTR objectName;
  554. spRootADs->get_Name(&objectName);
  555. if (objectName == NULL)
  556. {
  557. ADSIEditMessageBox(IDS_MSG_ROOTDSE_ERROR, MB_OK);
  558. return FALSE;
  559. }
  560. m_pNewConnectData->SetName(objectName);
  561. CString szProvider, sServer, sPort, sPath;
  562. m_pNewConnectData->GetDomainServer(sServer);
  563. m_pNewConnectData->GetPort(sPort);
  564. m_pNewConnectData->GetLDAP(szProvider);
  565. if (sServer != _T(""))
  566. {
  567. if (sPort != _T(""))
  568. {
  569. sPath = szProvider + sServer + _T(":") + sPort + _T("/") + CString(objectName);
  570. }
  571. else
  572. {
  573. sPath = szProvider + sServer + _T("/") + CString(objectName);
  574. }
  575. }
  576. else
  577. {
  578. sPath = szProvider + CString(objectName);
  579. }
  580. m_pNewConnectData->SetPath(sPath);
  581. m_pNewConnectData->SetClass(_T(""));
  582. CString sName;
  583. pcNameBox->GetWindowText(sName);
  584. if (sName.GetLength() > 0)
  585. {
  586. //Create a connection node
  587. m_pNewConnectData->SetName(sName);
  588. CADSIEditConnectionNode *pConnectNode = new CADSIEditConnectionNode(m_pNewConnectData);
  589. pConnectNode->SetDisplayName(sName + m_szDisplayExtra);
  590. pConnectNode->GetConnectionData()->SetConnectionNode(pConnectNode);
  591. VERIFY(pRootNode->AddChildToListAndUI(pConnectNode, pComponentData));
  592. pComponentData->SetDescriptionBarText(pRootNode);
  593. }
  594. else
  595. {
  596. ADSIEditMessageBox(IDS_MSG_CONNECTION_NAME, MB_OK);
  597. return FALSE;
  598. }
  599. }
  600. } //if RootDSE
  601. else
  602. {
  603. CComBSTR bstrPath;
  604. if (!BuildNamingContext(bstrPath))
  605. {
  606. return FALSE;
  607. }
  608. if (!BuildPath(s, (BSTR)bstrPath, spRootADs))
  609. {
  610. return FALSE;
  611. }
  612. if (!m_bNewConnect)
  613. {
  614. CString sName;
  615. pcNameBox->GetWindowText(sName);
  616. m_pNewConnectData->SetName(sName);
  617. m_pNewConnectData->SetPath(s);
  618. ASSERT(pTreeNode != NULL);
  619. pTreeNode->SetDisplayName(sName + m_szDisplayExtra);
  620. if (!pTreeNode->OnEnumerate(pComponentData))
  621. {
  622. return FALSE;
  623. }
  624. /* if (!pTreeNode->OnRefresh(pComponentData))
  625. {
  626. return FALSE;
  627. }
  628. */
  629. }
  630. else
  631. {
  632. CComPtr<IDirectoryObject> spDirObject;
  633. hr = OpenObjectWithCredentials(
  634. m_pNewConnectData,
  635. m_pNewConnectData->GetCredentialObject()->UseCredentials(),
  636. (LPWSTR)(LPCWSTR)s,
  637. IID_IDirectoryObject,
  638. (LPVOID*) &spDirObject,
  639. GetSafeHwnd(),
  640. hCredResult
  641. );
  642. if ( FAILED(hr) )
  643. {
  644. if (SUCCEEDED(hCredResult))
  645. {
  646. ADSIEditErrorMessage(hr);
  647. }
  648. return FALSE;
  649. }
  650. ADS_OBJECT_INFO* pInfo;
  651. hr = spDirObject->GetObjectInformation(&pInfo);
  652. if (FAILED(hr))
  653. {
  654. ADSIEditErrorMessage(hr);
  655. return FALSE;
  656. }
  657. // Name
  658. m_pNewConnectData->SetName(pInfo->pszRDN);
  659. m_pNewConnectData->SetPath(s);
  660. // Class
  661. m_pNewConnectData->SetClass(pInfo->pszClassName);
  662. FreeADsMem(pInfo);
  663. CString sName;
  664. pcNameBox->GetWindowText(sName);
  665. if (sName.GetLength() > 0)
  666. {
  667. //Create a connection node
  668. m_pNewConnectData->SetName(sName);
  669. CADSIEditConnectionNode *pConnectNode = new CADSIEditConnectionNode(m_pNewConnectData);
  670. pConnectNode->SetDisplayName(sName + m_szDisplayExtra);
  671. pConnectNode->GetConnectionData()->SetConnectionNode(pConnectNode);
  672. VERIFY(pRootNode->AddChildToListAndUI(pConnectNode, pComponentData));
  673. pComponentData->SetDescriptionBarText(pRootNode);
  674. }
  675. else
  676. {
  677. ADSIEditMessageBox(IDS_MSG_CONNECTION_NAME, MB_OK);
  678. return FALSE;
  679. }
  680. }
  681. } //else
  682. return TRUE;
  683. }
  684. BOOL CADSIEditConnectDialog::BuildPath(CString& s, BSTR bstrPath, IADs* pADs)
  685. {
  686. CButton* pcDNRadio = (CButton*)GetDlgItem(IDC_DN_RADIO);
  687. CButton* pcNCRadio = (CButton*)GetDlgItem(IDC_NC_RADIO);
  688. HRESULT hr;
  689. CString szLDAP, basePath, sServer, sPort, sDistinguishedName;
  690. m_pNewConnectData->GetLDAP(szLDAP);
  691. m_pNewConnectData->GetDomainServer(sServer);
  692. m_pNewConnectData->GetPort(sPort);
  693. m_pNewConnectData->GetDistinguishedName(sDistinguishedName);
  694. if ( pcNCRadio->GetCheck())
  695. {
  696. VARIANT var;
  697. VariantInit(&var);
  698. hr = pADs->Get( bstrPath, &var );
  699. if ( FAILED(hr) )
  700. {
  701. VariantClear(&var);
  702. return FALSE;
  703. }
  704. if (!sServer.IsEmpty())
  705. {
  706. s = s + sServer;
  707. if (!sPort.IsEmpty())
  708. {
  709. s = s + _T(":") + sPort + _T("/");
  710. }
  711. else
  712. {
  713. s = s + _T("/");
  714. }
  715. }
  716. s = s + V_BSTR(&var);
  717. basePath = V_BSTR(&var);
  718. VariantClear(&var);
  719. }
  720. else if(pcDNRadio->GetCheck())
  721. {
  722. if (!sServer.IsEmpty())
  723. {
  724. s = s + sServer;
  725. if (!sPort.IsEmpty())
  726. {
  727. s = s + _T(":") + sPort + _T("/");
  728. }
  729. else
  730. {
  731. s = s + _T("/");
  732. }
  733. }
  734. s = s + sDistinguishedName;
  735. basePath = sDistinguishedName;
  736. }
  737. m_pNewConnectData->SetBasePath(basePath);
  738. s = szLDAP + s;
  739. return TRUE;
  740. }
  741. BOOL CADSIEditConnectDialog::BuildNamingContext(CComBSTR& bstrPath)
  742. {
  743. CButton* pcDNRadio = (CButton*)GetDlgItem(IDC_DN_RADIO);
  744. CButton* pcNCRadio = (CButton*)GetDlgItem(IDC_NC_RADIO);
  745. CString sNamingContext;
  746. m_pNewConnectData->GetNamingContext(sNamingContext);
  747. if ( pcNCRadio->GetCheck())
  748. {
  749. if ( sNamingContext == m_szDomain)
  750. {
  751. bstrPath = SysAllocString( L"defaultNamingContext");
  752. }
  753. else if ( sNamingContext == m_szSchema)
  754. {
  755. bstrPath = SysAllocString(L"schemaNamingContext");
  756. }
  757. else if ( sNamingContext == m_szConfigContainer)
  758. {
  759. bstrPath = SysAllocString(L"configurationNamingContext");
  760. }
  761. else
  762. {
  763. bstrPath = SysAllocString( L"defaultNamingContext");
  764. }
  765. m_pNewConnectData->SetDistinguishedName(_T(""));
  766. }
  767. else if (pcDNRadio->GetCheck())
  768. {
  769. CString sDistinguishedName;
  770. m_pNewConnectData->GetDistinguishedName(sDistinguishedName);
  771. if (sDistinguishedName.Find(L'=') == -1)
  772. {
  773. int iResult = ADSIEditMessageBox(IDS_MSG_NOT_X500_PATH, MB_YESNO);
  774. if (iResult == IDNO)
  775. {
  776. return FALSE;
  777. }
  778. }
  779. bstrPath = sDistinguishedName.AllocSysString();
  780. m_pNewConnectData->SetNamingContext(_T(""));
  781. }
  782. return TRUE;
  783. }
  784. void CADSIEditConnectDialog::BuildRootDSE(CString& sRootDSE)
  785. {
  786. CButton* pDefaultRadio = (CButton*)GetDlgItem(IDC_DEFAULT_RADIO);
  787. CString sServer, sPort, sLDAP;
  788. m_pNewConnectData->GetDomainServer(sServer);
  789. m_pNewConnectData->GetPort(sPort);
  790. m_pNewConnectData->GetLDAP(sLDAP);
  791. if (!sServer.IsEmpty())
  792. {
  793. sRootDSE = sLDAP + sServer;
  794. if (!sPort.IsEmpty())
  795. {
  796. sRootDSE = sRootDSE + _T(":") + sPort + _T("/");
  797. }
  798. else
  799. {
  800. sRootDSE = sRootDSE + _T("/");
  801. }
  802. sRootDSE = sRootDSE + g_lpszRootDSE;
  803. }
  804. else
  805. {
  806. sRootDSE = sLDAP + g_lpszRootDSE;
  807. }
  808. }
  809. /////////////////////////////////////////////////////////////////////////////////////////////////
  810. // CADSIEditAdvancedConnectionDialog :
  811. BEGIN_MESSAGE_MAP(CADSIEditAdvancedConnectionDialog, CDialog)
  812. //{{AFX_MSG_MAP(CADsObjectDialog)
  813. ON_BN_CLICKED(IDC_CREDENTIALS_CHECK, OnCredentials)
  814. //}}AFX_MSG_MAP
  815. END_MESSAGE_MAP()
  816. CADSIEditAdvancedConnectionDialog::CADSIEditAdvancedConnectionDialog(CContainerNode* pRootDataNode,
  817. CTreeNode* pContainerNode, CComponentDataObject* pComponentData, CConnectionData* pConnectData)
  818. : CDialog(IDD_CONNECTION_ADVANCED)
  819. {
  820. // Get the local data
  821. //
  822. m_pTreeNode = pContainerNode;
  823. m_pContainerNode = pRootDataNode;
  824. ASSERT(pComponentData != NULL);
  825. m_pComponentData = pComponentData;
  826. ASSERT(pConnectData != NULL);
  827. m_pConnectData = pConnectData;
  828. }
  829. CADSIEditAdvancedConnectionDialog::~CADSIEditAdvancedConnectionDialog()
  830. {
  831. }
  832. BOOL CADSIEditAdvancedConnectionDialog::OnInitDialog()
  833. {
  834. CDialog::OnInitDialog();
  835. // Attach all the controls
  836. //
  837. CEdit* pcPortBox = (CEdit*)GetDlgItem(IDC_PORT);
  838. CButton* pcLDAPRadio = (CButton*)GetDlgItem(IDC_LDAP_RADIO);
  839. CButton* pcGCRadio = (CButton*)GetDlgItem(IDC_GC_RADIO);
  840. CButton* pcCredCheck = (CButton*)GetDlgItem(IDC_CREDENTIALS_CHECK);
  841. CEdit* pcUsernameBox = (CEdit*)GetDlgItem(IDC_USERNAME);
  842. CEdit* pcPasswordBox = (CEdit*)GetDlgItem(IDC_PASSWORD);
  843. // disable IME support on numeric edit fields
  844. ImmAssociateContext(pcPortBox->GetSafeHwnd(), NULL);
  845. // Set the initial state of the controls
  846. //
  847. CString sLDAP;
  848. m_pConnectData->GetLDAP(sLDAP);
  849. if (wcscmp(sLDAP, g_lpszLDAP) == 0)
  850. {
  851. pcLDAPRadio->SetCheck(BST_CHECKED);
  852. }
  853. else
  854. {
  855. pcGCRadio->SetCheck(BST_CHECKED);
  856. }
  857. CString sPort;
  858. m_pConnectData->GetPort(sPort);
  859. pcPortBox->SetWindowText(sPort);
  860. if (m_pConnectData->GetCredentialObject()->UseCredentials())
  861. {
  862. CString sUser;
  863. m_pConnectData->GetCredentialObject()->GetUsername(sUser);
  864. pcCredCheck->SetCheck(TRUE);
  865. OnCredentials();
  866. pcUsernameBox->SetWindowText(sUser);
  867. }
  868. pcPasswordBox->SetLimitText(MAX_PASSWORD_LENGTH);
  869. return TRUE;
  870. }
  871. void CADSIEditAdvancedConnectionDialog::OnOK()
  872. {
  873. OnApply();
  874. CDialog::OnOK();
  875. }
  876. BOOL CADSIEditAdvancedConnectionDialog::OnApply()
  877. {
  878. CEdit* pcPortBox = (CEdit*)GetDlgItem(IDC_PORT);
  879. CButton* pcLDAPRadio = (CButton*)GetDlgItem(IDC_LDAP_RADIO);
  880. CButton* pcCredCheck = (CButton*)GetDlgItem(IDC_CREDENTIALS_CHECK);
  881. CEdit* pcUsernameBox = (CEdit*)GetDlgItem(IDC_USERNAME);
  882. CEdit* pcPasswordBox = (CEdit*)GetDlgItem(IDC_PASSWORD);
  883. // Make the connection data reflect the controls
  884. //
  885. CString sPort;
  886. pcPortBox->GetWindowText(sPort);
  887. m_pConnectData->SetPort(sPort);
  888. if (pcLDAPRadio->GetCheck())
  889. {
  890. m_pConnectData->SetLDAP(g_lpszLDAP);
  891. }
  892. else
  893. {
  894. m_pConnectData->SetLDAP(g_lpszGC);
  895. }
  896. if (pcCredCheck->GetCheck())
  897. {
  898. // Get user name and password
  899. //
  900. CString sUser;
  901. pcUsernameBox->GetWindowText(sUser);
  902. HWND hWnd = pcPasswordBox->GetSafeHwnd();
  903. m_pConnectData->GetCredentialObject()->SetUsername(sUser);
  904. m_pConnectData->GetCredentialObject()->SetPasswordFromHwnd(hWnd);
  905. m_pConnectData->GetCredentialObject()->SetUseCredentials(TRUE);
  906. }
  907. else
  908. {
  909. m_pConnectData->GetCredentialObject()->SetUseCredentials(FALSE);
  910. }
  911. return TRUE;
  912. }
  913. void CADSIEditAdvancedConnectionDialog::OnCredentials()
  914. {
  915. CButton* pcCredCheck = (CButton*)GetDlgItem(IDC_CREDENTIALS_CHECK);
  916. CButton* pcCredGroup = (CButton*)GetDlgItem(IDC_CREDENTIALS_GROUP);
  917. CStatic* pcCredUser = (CStatic*)GetDlgItem(IDC_CREDENTIALS_USER);
  918. CStatic* pcCredPassword = (CStatic*)GetDlgItem(IDC_CREDENTIALS_PASSWORD);
  919. CEdit* pcUsernameBox = (CEdit*)GetDlgItem(IDC_USERNAME);
  920. CEdit* pcPasswordBox = (CEdit*)GetDlgItem(IDC_PASSWORD);
  921. BOOL bResult = pcCredCheck->GetCheck();
  922. if (bResult)
  923. {
  924. // Enable Username and password fields
  925. //
  926. pcCredGroup->EnableWindow(bResult);
  927. pcCredUser->EnableWindow(bResult);
  928. pcCredPassword->EnableWindow(bResult);
  929. pcUsernameBox->EnableWindow(bResult);
  930. pcPasswordBox->EnableWindow(bResult);
  931. }
  932. else
  933. {
  934. // Enable Username and password fields
  935. //
  936. pcCredGroup->EnableWindow(FALSE);
  937. pcCredUser->EnableWindow(FALSE);
  938. pcCredPassword->EnableWindow(FALSE);
  939. pcUsernameBox->EnableWindow(FALSE);
  940. pcPasswordBox->EnableWindow(FALSE);
  941. }
  942. }