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.

412 lines
11 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // eapnegotiate.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Defines the class EapNegotiate.
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include "stdafx.h"
  15. #include "resource.h"
  16. #include "eapnegotiate.h"
  17. #include "eapconfig.h"
  18. #include "eapadd.h"
  19. #include "rrascfg.h"
  20. BEGIN_MESSAGE_MAP(EapNegotiate, CHelpDialog)
  21. ON_LBN_SELCHANGE(IDC_LIST_EAP_SELECTED, OnItemChangedListEap)
  22. ON_BN_CLICKED(IDC_BUTTON_ADD_EAP_PROVIDER, OnButtonAdd)
  23. ON_BN_CLICKED(IDC_BUTTON_EDIT_EAP_PROVIDER, OnButtonEdit)
  24. ON_BN_CLICKED(IDC_BUTTON_REMOVE_EAP_PROVIDER, OnButtonRemove)
  25. ON_BN_CLICKED(IDC_BUTTON_EAP_UP, OnButtonMoveUp)
  26. ON_BN_CLICKED(IDC_BUTTON_EAP_DOWN, OnButtonMoveDown)
  27. END_MESSAGE_MAP()
  28. EapNegotiate::EapNegotiate(
  29. CWnd* pParent,
  30. EapConfig& eapConfig,
  31. CRASProfileMerge& profile,
  32. bool fromProfile
  33. )
  34. : CHelpDialog(IDD_EAP_NEGOCIATE, pParent),
  35. m_eapConfig(eapConfig),
  36. m_profile(profile),
  37. m_listBox(NULL)
  38. {
  39. if (fromProfile)
  40. {
  41. UpdateProfileTypesSelected();
  42. }
  43. }
  44. EapNegotiate::~EapNegotiate()
  45. {
  46. delete m_listBox;
  47. }
  48. // CAUTION: call only from the constructor
  49. void EapNegotiate::UpdateProfileTypesSelected()
  50. {
  51. m_eapConfig.typesSelected.DeleteAll();
  52. // Get the eap types that are in the profile
  53. for (int i = 0; i < m_profile.m_dwArrayEapTypes.GetSize(); ++i)
  54. {
  55. int j = m_eapConfig.ids.Find(m_profile.m_dwArrayEapTypes.GetAt(i));
  56. // if in the list, add it
  57. if (j != -1)
  58. {
  59. m_eapConfig.typesSelected.AddDuplicate(*m_eapConfig.types.GetAt(j));
  60. }
  61. }
  62. }
  63. BOOL EapNegotiate::OnInitDialog()
  64. {
  65. HRESULT hr = CHelpDialog::OnInitDialog();
  66. m_listBox = new CStrBox<CListBox>(
  67. this,
  68. IDC_LIST_EAP_SELECTED,
  69. m_eapConfig.typesSelected
  70. );
  71. if (m_listBox == NULL)
  72. {
  73. AfxMessageBox(IDS_OUTOFMEMORY);
  74. return TRUE; // return TRUE unless you set the focus to a control
  75. // EXCEPTION: OCX Property Pages should return FALSE
  76. }
  77. m_listBox->Fill();
  78. // Take action based on whether list is empty or not.
  79. int boxSize = m_eapConfig.typesSelected.GetSize();
  80. if( boxSize > 0 )
  81. {
  82. // Select the first element.
  83. m_listBox->Select(0);
  84. }
  85. UpdateTypesNotSelected();
  86. UpdateButtons();
  87. return hr;
  88. }
  89. void EapNegotiate::OnItemChangedListEap()
  90. {
  91. UpdateButtons();
  92. }
  93. void EapNegotiate::OnButtonAdd()
  94. {
  95. EapAdd eapAdd(this, m_eapConfig);
  96. if (eapAdd.DoModal() == IDOK)
  97. {
  98. m_listBox->Fill();
  99. // select the last one (the one just added)
  100. m_listBox->Select(m_eapConfig.typesSelected.GetSize() - 1);
  101. UpdateTypesNotSelected();
  102. // update the buttons...
  103. UpdateButtons();
  104. }
  105. }
  106. void EapNegotiate::OnButtonEdit()
  107. {
  108. // enable / disable configure button based on if the type has config clsID
  109. int i = m_listBox->GetSelected();
  110. int position;
  111. BOOL bEnableConfig = FALSE;
  112. if (i != -1)
  113. {
  114. // find the type corresponding to the selected item
  115. position = m_eapConfig.types.Find(*m_eapConfig.typesSelected.GetAt(i));
  116. bEnableConfig = !(m_eapConfig.infoArray.ElementAt(position)
  117. .m_stConfigCLSID.IsEmpty());
  118. }
  119. if (!bEnableConfig)
  120. {
  121. // bEnableConfig can be false because either:
  122. // nothing is selected in the list. So the button should be disabled
  123. // there's no UI to config the EAP provider (CLSID is empty)
  124. return;
  125. }
  126. // everything below should succeed if the EAP provider is properly installed
  127. // because there is a CLSID to configure it.
  128. CComPtr<IEAPProviderConfig> spEAPConfig;
  129. HRESULT hr;
  130. GUID guid;
  131. do
  132. {
  133. hr = CLSIDFromString((LPTSTR) (LPCTSTR)
  134. (m_eapConfig.infoArray.ElementAt(position).m_stConfigCLSID),
  135. &guid);
  136. if (FAILED(hr))
  137. {
  138. break;
  139. }
  140. // Create the EAP provider object
  141. hr = CoCreateInstance(
  142. guid,
  143. NULL,
  144. CLSCTX_INPROC_SERVER,
  145. __uuidof(IEAPProviderConfig),
  146. (LPVOID *) &spEAPConfig);
  147. if (FAILED(hr))
  148. {
  149. break;
  150. }
  151. // Configure this EAP provider
  152. // EAP configure displays its own error message, so no hr is kept
  153. DWORD dwId = _wtol(m_eapConfig.infoArray.ElementAt(position).m_stKey);
  154. ULONG_PTR uConnection = 0;
  155. if ( SUCCEEDED(spEAPConfig->Initialize(
  156. m_profile.m_strMachineName,
  157. dwId,
  158. &uConnection
  159. )) )
  160. {
  161. CComPtr<IEAPProviderConfig2> spEAPConfig2;
  162. hr = spEAPConfig->QueryInterface(
  163. __uuidof(IEAPProviderConfig2),
  164. reinterpret_cast<void**>(&spEAPConfig2)
  165. );
  166. if (SUCCEEDED(hr))
  167. {
  168. EapProfile::ConstConfigData inData;
  169. m_eapProfile.Get(static_cast<BYTE>(dwId), inData);
  170. EapProfile::ConfigData outData = { 0, 0 };
  171. hr = spEAPConfig2->ServerInvokeConfigUI2(
  172. dwId,
  173. uConnection,
  174. GetSafeHwnd(),
  175. inData.value,
  176. inData.length,
  177. &(outData.value),
  178. &(outData.length)
  179. );
  180. if (SUCCEEDED(hr))
  181. {
  182. hr = m_eapProfile.Set(static_cast<BYTE>(dwId), outData);
  183. CoTaskMemFree(outData.value);
  184. }
  185. }
  186. else
  187. {
  188. // Bring up the configuration UI for this EAP
  189. hr = spEAPConfig->ServerInvokeConfigUI(
  190. dwId,
  191. uConnection,
  192. GetSafeHwnd(),
  193. 0,
  194. 0
  195. );
  196. }
  197. spEAPConfig->Uninitialize(dwId, uConnection);
  198. }
  199. }
  200. while(false);
  201. if ( FAILED(hr) )
  202. {
  203. // Bring up an error message
  204. // ------------------------------------------------------------
  205. ReportError(hr, IDS_ERR_CONFIG_EAP, GetSafeHwnd());
  206. }
  207. }
  208. void EapNegotiate::OnButtonRemove()
  209. {
  210. int pos = m_listBox->GetSelected();
  211. if (pos != -1)
  212. {
  213. int idx = m_eapConfig.types.Find(*m_eapConfig.typesSelected.GetAt(pos));
  214. if (idx != -1)
  215. {
  216. m_eapProfile.Erase(m_eapConfig.ids.ElementAt(idx));
  217. }
  218. }
  219. // remove from the UI and from the CStrArray (memory freed)
  220. m_listBox->DeleteSelected();
  221. if (m_eapConfig.typesSelected.GetSize() > 0)
  222. {
  223. m_listBox->Select(0);
  224. }
  225. UpdateTypesNotSelected();
  226. UpdateButtons();
  227. }
  228. void EapNegotiate::OnButtonMoveUp()
  229. {
  230. int i = m_listBox->GetSelected();
  231. if (i != LB_ERR)
  232. {
  233. ASSERT(i != 0);
  234. CString* pString = m_eapConfig.typesSelected.GetAt(i-1);
  235. m_eapConfig.typesSelected.SetAt(i-1, m_eapConfig.typesSelected.GetAt(i));
  236. m_eapConfig.typesSelected.SetAt(i, pString);
  237. m_listBox->Fill();
  238. m_listBox->Select(i-1);
  239. UpdateArrowsButtons(i-1);
  240. }
  241. }
  242. void EapNegotiate::OnButtonMoveDown()
  243. {
  244. int i = m_listBox->GetSelected();
  245. if (i != LB_ERR)
  246. {
  247. ASSERT(i < (m_eapConfig.idsSelected.GetSize() - 1));
  248. CString* pString = m_eapConfig.typesSelected.GetAt(i+1);
  249. m_eapConfig.typesSelected.SetAt(i+1, m_eapConfig.typesSelected.GetAt(i));
  250. m_eapConfig.typesSelected.SetAt(i, pString);
  251. m_listBox->Fill();
  252. m_listBox->Select(i+1);
  253. UpdateArrowsButtons(i+1);
  254. }
  255. }
  256. void EapNegotiate::UpdateButtons()
  257. {
  258. int selected = m_listBox->GetSelected();
  259. if (selected == LB_ERR)
  260. {
  261. m_buttonRemove.EnableWindow(FALSE);
  262. }
  263. else
  264. {
  265. m_buttonRemove.EnableWindow(TRUE);
  266. }
  267. UpdateAddButton();
  268. UpdateArrowsButtons(selected);
  269. UpdateEditButton(selected);
  270. }
  271. void EapNegotiate::UpdateAddButton()
  272. {
  273. if( m_typesNotSelected.GetSize() > 0 )
  274. {
  275. m_buttonAdd.EnableWindow(TRUE);
  276. }
  277. else
  278. {
  279. m_buttonAdd.EnableWindow(FALSE);
  280. }
  281. }
  282. void EapNegotiate::UpdateArrowsButtons(int selectedItem)
  283. {
  284. // The focus has to be set to make sure it is not on a
  285. // disabled control
  286. HWND hWnd = ::GetFocus();
  287. if (selectedItem == LB_ERR)
  288. {
  289. m_buttonUp.EnableWindow(FALSE);
  290. m_buttonDown.EnableWindow(FALSE);
  291. ::SetFocus(GetDlgItem(IDOK)->m_hWnd);
  292. return;
  293. }
  294. int typesInBox = m_eapConfig.typesSelected.GetSize();
  295. if (typesInBox == 1)
  296. {
  297. // one selected but total = 1
  298. m_buttonUp.EnableWindow(FALSE);
  299. m_buttonDown.EnableWindow(FALSE);
  300. ::SetFocus(GetDlgItem(IDOK)->m_hWnd);
  301. }
  302. else
  303. {
  304. // more than one provider in the box
  305. if (selectedItem == 0)
  306. {
  307. // first one
  308. m_buttonUp.EnableWindow(FALSE);
  309. m_buttonDown.EnableWindow(TRUE);
  310. m_buttonDown.SetFocus();
  311. }
  312. else if (selectedItem == (typesInBox - 1) )
  313. {
  314. //last one
  315. m_buttonUp.EnableWindow(TRUE);
  316. m_buttonUp.SetFocus();
  317. m_buttonDown.EnableWindow(FALSE);
  318. }
  319. else
  320. {
  321. // middle
  322. m_buttonUp.EnableWindow(TRUE);
  323. m_buttonDown.EnableWindow(TRUE);
  324. }
  325. }
  326. }
  327. void EapNegotiate::UpdateEditButton(int selectedItem)
  328. {
  329. if (selectedItem == LB_ERR)
  330. {
  331. m_buttonEdit.EnableWindow(FALSE);
  332. return;
  333. }
  334. int position = m_eapConfig.types.Find(
  335. *m_eapConfig.typesSelected.GetAt(selectedItem));
  336. if (m_eapConfig.infoArray.ElementAt(position).m_stConfigCLSID.IsEmpty())
  337. {
  338. m_buttonEdit.EnableWindow(FALSE);
  339. }
  340. else
  341. {
  342. m_buttonEdit.EnableWindow(TRUE);
  343. }
  344. m_buttonRemove.EnableWindow(TRUE);
  345. }
  346. void EapNegotiate::UpdateTypesNotSelected()
  347. {
  348. m_eapConfig.GetEapTypesNotSelected(m_typesNotSelected);
  349. }
  350. void EapNegotiate::DoDataExchange(CDataExchange* pDX)
  351. {
  352. CHelpDialog::DoDataExchange(pDX);
  353. DDX_Control(pDX, IDC_BUTTON_EAP_UP, m_buttonUp);
  354. DDX_Control(pDX, IDC_BUTTON_EAP_DOWN, m_buttonDown);
  355. DDX_Control(pDX, IDC_BUTTON_ADD_EAP_PROVIDER, m_buttonAdd);
  356. DDX_Control(pDX, IDC_BUTTON_EDIT_EAP_PROVIDER, m_buttonEdit);
  357. DDX_Control(pDX, IDC_BUTTON_REMOVE_EAP_PROVIDER, m_buttonRemove);
  358. }