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.

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