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.

717 lines
13 KiB

  1. /*++
  2. Copyright (c) 1994-1998 Microsoft Corporation
  3. Module Name :
  4. ipdomdlg.cpp
  5. Abstract:
  6. IP and domain security restrictions
  7. Author:
  8. Ronald Meijer (ronaldm)
  9. Project:
  10. Internet Services Manager
  11. Revision History:
  12. --*/
  13. //
  14. // Include Files
  15. //
  16. #include "stdafx.h"
  17. #include "resource.h"
  18. #include "common.h"
  19. #include "inetprop.h"
  20. #include "supdlgs.h"
  21. #include "ipdomdlg.h"
  22. //
  23. // Needed for granted/denied icons
  24. //
  25. //#include "..\comprop\resource.h"
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31. #define ILIST_DENY 0
  32. #define ILIST_GRANT 1
  33. #define ITYPE_DNS 0
  34. #define ITYPE_IP 1
  35. CIPDomainDlg::CIPDomainDlg(
  36. IN BOOL & fIpDirty,
  37. IN BOOL & fDefaultGranted,
  38. IN BOOL & fOldDefaultGranted,
  39. IN CObListPlus & oblAccessList,
  40. IN CWnd * pParent OPTIONAL
  41. )
  42. /*++
  43. Routine Description:
  44. IP/Domain access restrictions dialog constructor
  45. Argumentss:
  46. CWnd * pParent : Parent window
  47. Return Value:
  48. N/A
  49. --*/
  50. : CEmphasizedDialog(CIPDomainDlg::IDD, pParent),
  51. m_ListBoxRes(
  52. IDB_ACCESS,
  53. m_list_IpAddresses.nBitmaps
  54. ),
  55. m_oblAccessListGranted(),
  56. m_oblAccessListDenied(),
  57. m_oblReturnList(),
  58. m_list_IpAddresses(TRUE),
  59. m_fIpDirty(fIpDirty),
  60. m_fOldDefaultGranted(fOldDefaultGranted),
  61. m_fDefaultGranted(fDefaultGranted)
  62. {
  63. #if 0 // Keep class wizard happy
  64. //{{AFX_DATA_INIT(CIPDomainDlg)
  65. m_nGrantedDenied = 0;
  66. //}}AFX_DATA_INIT
  67. #endif // 0
  68. //
  69. // Keep a temporary copy of these
  70. //
  71. m_oblAccessListGranted.SetOwnership(FALSE);
  72. m_oblAccessListDenied.SetOwnership(FALSE);
  73. m_oblReturnList.SetOwnership(FALSE);
  74. CObListIter obli(oblAccessList);
  75. const CIPAccessDescriptor * pAccess;
  76. while (NULL != (pAccess = (CIPAccessDescriptor *)obli.Next()))
  77. {
  78. if (pAccess->HasAccess())
  79. {
  80. m_oblAccessListGranted.AddTail((CObject *)pAccess);
  81. }
  82. else
  83. {
  84. m_oblAccessListDenied.AddTail((CObject *)pAccess);
  85. }
  86. }
  87. m_list_IpAddresses.AttachResources(&m_ListBoxRes);
  88. m_nGrantedDenied = m_fDefaultGranted ? DEFAULT_GRANTED : DEFAULT_DENIED;
  89. m_pCurrentList = m_fDefaultGranted ? &m_oblAccessListDenied : &m_oblAccessListGranted;
  90. }
  91. void
  92. CIPDomainDlg::DoDataExchange(
  93. IN CDataExchange * pDX
  94. )
  95. /*++
  96. Routine Description:
  97. Initialise/Store control data
  98. Arguments:
  99. CDataExchange * pDX - DDX/DDV control structure
  100. Return Value:
  101. None
  102. --*/
  103. {
  104. CEmphasizedDialog::DoDataExchange(pDX);
  105. //{{AFX_DATA_MAP(CIPDomainDlg)
  106. DDX_Control(pDX, IDC_RADIO_GRANTED, m_radio_Granted);
  107. DDX_Control(pDX, IDC_BUTTON_ADD, m_button_Add);
  108. DDX_Control(pDX, IDC_BUTTON_REMOVE, m_button_Remove);
  109. DDX_Control(pDX, IDC_BUTTON_EDIT, m_button_Edit);
  110. DDX_Control(pDX, IDC_ICON_GRANTED, m_icon_Granted);
  111. DDX_Control(pDX, IDC_ICON_DENIED, m_icon_Denied);
  112. DDX_Radio(pDX, IDC_RADIO_GRANTED, m_nGrantedDenied);
  113. //}}AFX_DATA_MAP
  114. //
  115. // Private DDX/DDV Routines
  116. //
  117. DDX_Control(pDX, IDC_RADIO_DENIED, m_radio_Denied);
  118. DDX_Control(pDX, IDC_LIST_IP_ADDRESSES, m_list_IpAddresses);
  119. }
  120. //
  121. // Message Map
  122. //
  123. BEGIN_MESSAGE_MAP(CIPDomainDlg, CEmphasizedDialog)
  124. //{{AFX_MSG_MAP(CIPDomainDlg)
  125. ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
  126. ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
  127. ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove)
  128. ON_LBN_DBLCLK(IDC_LIST_IP_ADDRESSES, OnDblclkListIpAddresses)
  129. ON_LBN_ERRSPACE(IDC_LIST_IP_ADDRESSES, OnErrspaceListIpAddresses)
  130. ON_BN_CLICKED(IDC_RADIO_GRANTED, OnRadioGranted)
  131. ON_BN_CLICKED(IDC_RADIO_DENIED, OnRadioDenied)
  132. ON_LBN_SELCHANGE(IDC_LIST_IP_ADDRESSES, OnSelchangeListIpAddresses)
  133. ON_WM_VKEYTOITEM()
  134. //}}AFX_MSG_MAP
  135. END_MESSAGE_MAP()
  136. //
  137. // Message Handlers
  138. //
  139. // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  140. void
  141. CIPDomainDlg::OnButtonAdd()
  142. /*++
  143. Routine Description:
  144. 'Add' button handler
  145. Arguments:
  146. None
  147. Return Value:
  148. None
  149. --*/
  150. {
  151. if (ShowPropertiesDialog(TRUE) == IDOK)
  152. {
  153. m_fIpDirty = TRUE;
  154. SetControlStates();
  155. }
  156. }
  157. void
  158. CIPDomainDlg::OnButtonEdit()
  159. /*++
  160. Routine Description:
  161. 'Edit' button handler
  162. Arguments:
  163. None
  164. Return Value:
  165. None
  166. --*/
  167. {
  168. if (ShowPropertiesDialog(FALSE) == IDOK)
  169. {
  170. m_fIpDirty = TRUE;
  171. SetControlStates();
  172. }
  173. }
  174. void
  175. CIPDomainDlg::OnButtonRemove()
  176. /*++
  177. Routine Description:
  178. 'Remove' button handler
  179. Arguments:
  180. None
  181. Return Value:
  182. None
  183. --*/
  184. {
  185. int nSel = 0;
  186. int nCurSel = m_list_IpAddresses.GetCurSel();
  187. while (m_list_IpAddresses.GetNextSelectedItem(&nSel))
  188. {
  189. m_pCurrentList->RemoveIndex(nSel);
  190. m_list_IpAddresses.DeleteString(nSel);
  191. }
  192. m_fIpDirty = TRUE;
  193. if (nCurSel > 0)
  194. {
  195. --nCurSel;
  196. }
  197. m_list_IpAddresses.SetCurSel(nCurSel);
  198. if (!SetControlStates())
  199. {
  200. m_button_Add.SetFocus();
  201. }
  202. }
  203. BOOL
  204. CIPDomainDlg::SetControlStates()
  205. /*++
  206. Routine Description:
  207. Set button states depending on content of the listbox and the controls
  208. Arguments:
  209. None
  210. Return Value:
  211. TRUE if at least one item is currently selected in the listbox.
  212. --*/
  213. {
  214. BOOL fSomeSelection = m_list_IpAddresses.GetSelCount() > 0;
  215. m_button_Edit.EnableWindow(m_list_IpAddresses.GetSelCount() == 1);
  216. m_button_Remove.EnableWindow(m_list_IpAddresses.GetSelCount() > 0);
  217. return fSomeSelection;
  218. }
  219. void
  220. CIPDomainDlg::FillListBox(
  221. IN CIPAccessDescriptor * pSelection OPTIONAL
  222. )
  223. /*++
  224. Routine Description:
  225. Fill the ip address listbox from the oblist of access entries
  226. Arguments:
  227. CIPAccessDescriptor * pSelection : Item to be selected or NULL.
  228. Return Value:
  229. None
  230. --*/
  231. {
  232. CObListIter obli(*m_pCurrentList);
  233. const CIPAccessDescriptor * pAccess;
  234. m_list_IpAddresses.SetRedraw(FALSE);
  235. m_list_IpAddresses.ResetContent();
  236. int cItems = 0 ;
  237. int nSel = LB_ERR, nItem;
  238. for ( /**/; pAccess = (CIPAccessDescriptor *)obli.Next(); ++cItems)
  239. {
  240. //
  241. // We only list those not adhering to the default
  242. //
  243. if (pAccess->HasAccess() != m_fDefaultGranted)
  244. {
  245. nItem = m_list_IpAddresses.AddItem(pAccess);
  246. if (pAccess == pSelection)
  247. {
  248. //
  249. // Found item to be selected
  250. //
  251. nSel = nItem;
  252. }
  253. }
  254. }
  255. m_list_IpAddresses.SetCurSel(nSel);
  256. m_list_IpAddresses.SetRedraw(TRUE);
  257. }
  258. DWORD
  259. CIPDomainDlg::SortAccessList()
  260. /*++
  261. Routine Description:
  262. Sorting the access list by grant denied and ip address
  263. FillListBox() should be called after this because
  264. the listbox will no longer reflect the true status
  265. of the list of directories.
  266. Arguments:
  267. None
  268. Return Value:
  269. Error Return code
  270. --*/
  271. {
  272. BeginWaitCursor();
  273. DWORD dw = m_pCurrentList->Sort((CObjectPlus::PCOBJPLUS_ORDER_FUNC)
  274. &CIPAccessDescriptor::OrderByAddress);
  275. EndWaitCursor();
  276. return dw;
  277. }
  278. INT_PTR
  279. CIPDomainDlg::ShowPropertiesDialog(
  280. IN BOOL fAdd
  281. )
  282. /*++
  283. Routine Description:
  284. Bring up the dialog used for add or edit. Return the value returned
  285. by the dialog
  286. Arguments:
  287. BOOL fAdd : If TRUE, create new item. Otherwise, edit existing item
  288. Return Value:
  289. Dialog return code (IDOK/IDCANCEL)
  290. --*/
  291. {
  292. //
  293. // Bring up the dialog
  294. //
  295. CIPAccessDescriptor * pAccess = NULL;
  296. int nCurSel = LB_ERR;
  297. if (!fAdd)
  298. {
  299. //
  300. // Edit existing entry -- there better be only one...
  301. //
  302. pAccess = m_list_IpAddresses.GetSelectedItem();
  303. ASSERT(pAccess != NULL);
  304. if (pAccess == NULL)
  305. {
  306. //
  307. // Double click?
  308. //
  309. return IDCANCEL;
  310. }
  311. }
  312. CIPAccessDlg dlgAccess(
  313. m_fDefaultGranted,
  314. pAccess,
  315. m_pCurrentList,
  316. this,
  317. TRUE
  318. );
  319. INT_PTR nReturn = dlgAccess.DoModal();
  320. if (nReturn == IDOK)
  321. {
  322. CError err;
  323. ASSERT(pAccess != NULL);
  324. if (pAccess == NULL)
  325. {
  326. err = ERROR_NOT_ENOUGH_MEMORY;
  327. }
  328. else
  329. {
  330. try
  331. {
  332. if (fAdd)
  333. {
  334. m_pCurrentList->AddTail(pAccess);
  335. }
  336. SortAccessList();
  337. FillListBox(pAccess);
  338. }
  339. catch(CMemoryException * e)
  340. {
  341. err = ERROR_NOT_ENOUGH_MEMORY;
  342. e->Delete();
  343. }
  344. }
  345. err.MessageBoxOnFailure(m_hWnd);
  346. }
  347. return nReturn;
  348. }
  349. void
  350. CIPDomainDlg::OnDblclkListIpAddresses()
  351. /*++
  352. Routine Description:
  353. Double click handler for IP listbox
  354. Arguments:
  355. None
  356. Return Value:
  357. None
  358. --*/
  359. {
  360. OnButtonEdit();
  361. }
  362. void
  363. CIPDomainDlg::OnErrspaceListIpAddresses()
  364. /*++
  365. Routine Description:
  366. Error -- out of memory error for IP listbox
  367. Arguments:
  368. None
  369. Return Value:
  370. None
  371. --*/
  372. {
  373. SetControlStates();
  374. }
  375. void
  376. CIPDomainDlg::OnSelchangeListIpAddresses()
  377. /*++
  378. Routine Description:
  379. ip address 'selection change' notification handler
  380. Arguments:
  381. None
  382. Return Value:
  383. None
  384. --*/
  385. {
  386. SetControlStates();
  387. }
  388. BOOL
  389. CIPDomainDlg::OnInitDialog()
  390. /*++
  391. Routine Description:
  392. WM_INITDIALOG handler. Initialize the dialog.
  393. Arguments:
  394. None.
  395. Return Value:
  396. TRUE if no focus is to be set automatically, FALSE if the focus
  397. is already set.
  398. --*/
  399. {
  400. CEmphasizedDialog::OnInitDialog();
  401. m_icon_Granted.SetIcon(::AfxGetApp()->LoadIcon(IDI_GRANTED));
  402. m_icon_Denied.SetIcon(::AfxGetApp()->LoadIcon(IDI_DENIED));
  403. m_list_IpAddresses.Initialize();
  404. FillListBox();
  405. SetControlStates();
  406. return TRUE;
  407. }
  408. void
  409. CIPDomainDlg::OnRadioGranted()
  410. /*++
  411. Routine Description:
  412. 'Granted' radio button handler.
  413. Granted by default has been selected. Refill the listbox with
  414. items that have been explicitly denied. Although we can
  415. only have a deny list or a grant list, we keep both of them
  416. around until it comes time to saving the information.
  417. Arguments:
  418. None
  419. Return Value:
  420. None
  421. --*/
  422. {
  423. if (!m_fDefaultGranted)
  424. {
  425. m_fDefaultGranted = TRUE;
  426. m_pCurrentList = &m_oblAccessListDenied;
  427. FillListBox();
  428. SetControlStates();
  429. }
  430. }
  431. void
  432. CIPDomainDlg::OnRadioDenied()
  433. /*++
  434. Routine Description:
  435. 'Denied' radio button handler. Same as above, with reverse granted
  436. and denied.
  437. Arguments:
  438. None
  439. Return Value:
  440. None
  441. --*/
  442. {
  443. if (m_fDefaultGranted)
  444. {
  445. m_fDefaultGranted = FALSE;
  446. m_pCurrentList = &m_oblAccessListGranted;
  447. FillListBox();
  448. SetControlStates();
  449. }
  450. }
  451. int
  452. CIPDomainDlg::OnVKeyToItem(
  453. IN UINT nKey,
  454. IN CListBox * pListBox,
  455. IN UINT nIndex
  456. )
  457. /*++
  458. Routine Description:
  459. Map virtual keys to commands for ip listbox
  460. Arguments:
  461. UINT nKey Specifies the virtual-key code of the key
  462. that the user pressed.
  463. CListBox * pListBox Specifies a pointer to the list box. The
  464. pointer may be temporary and should not be stored for later use.
  465. UINT nIndex Specifies the current caret position.
  466. Return Value:
  467. -2 : No further action necessary
  468. -1 : Perform default action for the keystroke
  469. >=0 : Indicates the default action should be performed on the index
  470. specified.
  471. --*/
  472. {
  473. switch(nKey)
  474. {
  475. case VK_DELETE:
  476. OnButtonRemove();
  477. break;
  478. case VK_INSERT:
  479. OnButtonAdd();
  480. break;
  481. default:
  482. //
  483. // Not completely handled by this function, let
  484. // windows handle the remaining default action.
  485. //
  486. return -1;
  487. }
  488. //
  489. // No further action is neccesary.
  490. //
  491. return -2;
  492. }
  493. CObListPlus&
  494. CIPDomainDlg::GetAccessList()
  495. {
  496. m_oblReturnList.RemoveAll();
  497. m_oblReturnList.AddTail(&m_oblAccessListGranted);
  498. m_oblReturnList.AddTail(&m_oblAccessListDenied);
  499. return m_oblReturnList;
  500. }