SecurityPage.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /****************************************************************/
  2. /* */
  3. /* SecurityPage.cpp */
  4. /* */
  5. /* Implementation of the CSecurityPage class. */
  6. /* This class is a part of the FTP Server. */
  7. /* */
  8. /* Programmed by LYFZ van der Meer */
  9. /* Copyright LYFZ Software Solutions 2002 */
  10. /* http://www.LYFZvandermeer.nl */
  11. /* */
  12. /* Last updated: 10 july 2002 */
  13. /* */
  14. /****************************************************************/
  15. #include "stdafx.h"
  16. #include "CPhotoFTPReceiveApp.h"
  17. #include "CPhotoFTPReceive.h"
  18. #include "SecurityPage.h"
  19. #include "AddIPDlg.h"
  20. extern CCPhotoFTPReceive theServer;
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. CSecurityPage::CSecurityPage(CWnd* pParent /*=NULL*/)
  27. : CDialogResize(CSecurityPage::IDD, pParent)
  28. {
  29. //{{AFX_DATA_INIT(CSecurityPage)
  30. m_bBlockAll = FALSE;
  31. //}}AFX_DATA_INIT
  32. }
  33. void CSecurityPage::DoDataExchange(CDataExchange* pDX)
  34. {
  35. CDialogResize::DoDataExchange(pDX);
  36. //{{AFX_DATA_MAP(CSecurityPage)
  37. DDX_Control(pDX, IDC_BLOCKEDLIST, m_BlockedList);
  38. DDX_Control(pDX, IDC_NONBLOCKEDLIST, m_NonBlockedList);
  39. DDX_Check(pDX, IDC_BLOCK_ALL, m_bBlockAll);
  40. //}}AFX_DATA_MAP
  41. }
  42. BEGIN_MESSAGE_MAP(CSecurityPage, CDialogResize)
  43. //{{AFX_MSG_MAP(CSecurityPage)
  44. ON_BN_CLICKED(IDC_BLOCK_ALL, OnBlockAll)
  45. ON_WM_DESTROY()
  46. ON_BN_CLICKED(IDC_ADD_BLOCK, OnAddBlock)
  47. ON_BN_CLICKED(IDC_EDIT_BLOCK, OnEditBlock)
  48. ON_BN_CLICKED(IDC_ADD_NONBLOCK, OnAddNonblock)
  49. ON_BN_CLICKED(IDC_EDIT_NONBLOCK, OnEditNonblock)
  50. ON_BN_CLICKED(IDC_REMOVE_BLOCK, OnRemoveBlock)
  51. ON_BN_CLICKED(IDC_REMOVE_NONBLOCK, OnRemoveNonblock)
  52. ON_LBN_DBLCLK(IDC_BLOCKEDLIST, OnDblclkBlockedlist)
  53. ON_LBN_DBLCLK(IDC_NONBLOCKEDLIST, OnDblclkNonblockedlist)
  54. //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56. BEGIN_DLGRESIZE_MAP(CSecurityPage)
  57. DLGRESIZE_CONTROL(IDC_BLOCKEDLIST, DLSZ_SIZE_X)
  58. DLGRESIZE_CONTROL(IDC_NONBLOCKEDLIST, DLSZ_SIZE_X)
  59. END_DLGRESIZE_MAP()
  60. /********************************************************************/
  61. /* */
  62. /* Function name : OnInitDialog */
  63. /* Description : Initialize dialog */
  64. /* */
  65. /********************************************************************/
  66. BOOL CSecurityPage::OnInitDialog()
  67. {
  68. CDialogResize::OnInitDialog();
  69. InitResizing(FALSE, FALSE, WS_CLIPCHILDREN);
  70. m_bBlockAll = AfxGetApp()->GetProfileInt("Settings", "BlockAll", 0);
  71. GetDlgItem(IDC_NONBLOCKEDLIST)->EnableWindow(m_bBlockAll);
  72. GetDlgItem(IDC_ADD_NONBLOCK)->EnableWindow(m_bBlockAll);
  73. GetDlgItem(IDC_EDIT_NONBLOCK)->EnableWindow(m_bBlockAll);
  74. GetDlgItem(IDC_REMOVE_NONBLOCK)->EnableWindow(m_bBlockAll);
  75. GetDlgItem(IDC_BLOCKEDLIST)->EnableWindow(!m_bBlockAll);
  76. GetDlgItem(IDC_ADD_BLOCK)->EnableWindow(!m_bBlockAll);
  77. GetDlgItem(IDC_EDIT_BLOCK)->EnableWindow(!m_bBlockAll);
  78. GetDlgItem(IDC_REMOVE_BLOCK)->EnableWindow(!m_bBlockAll);
  79. GetDlgItem(IDC_STATIC1)->EnableWindow(!m_bBlockAll);
  80. UpdateData(FALSE);
  81. CStringArray strArray;
  82. theServer.m_SecurityManager.GetBlockedList(strArray);
  83. for (int i=0; i < strArray.GetSize(); i++)
  84. {
  85. m_BlockedList.AddString(strArray[i]);
  86. }
  87. theServer.m_SecurityManager.GetNonBlockedList(strArray);
  88. for (int j=0; j < strArray.GetSize(); j++)
  89. {
  90. m_NonBlockedList.AddString(strArray[j]);
  91. }
  92. // get list of all ip addresses in use by this system (only show first two...)
  93. char szHostName[128];
  94. HOSTENT *lpHost=NULL;
  95. struct sockaddr_in sock;
  96. gethostname(szHostName, sizeof(szHostName));
  97. lpHost = gethostbyname(szHostName);
  98. if (lpHost != NULL)
  99. {
  100. for(int i=0; lpHost->h_addr_list[i] != NULL ;i++)
  101. {
  102. memcpy(&(sock.sin_addr), lpHost->h_addr_list[i], lpHost->h_length);
  103. if (i == 0)
  104. {
  105. SetDlgItemText(IDC_IPADDRESS1, inet_ntoa(sock.sin_addr));
  106. }
  107. else
  108. if (i == 1)
  109. {
  110. SetDlgItemText(IDC_IPADDRESS2, inet_ntoa(sock.sin_addr));
  111. }
  112. }
  113. }
  114. return TRUE;
  115. }
  116. /********************************************************************/
  117. /* */
  118. /* Function name : OnDestroy */
  119. /* Description : Dialog is about to be destroyed. */
  120. /* */
  121. /********************************************************************/
  122. void CSecurityPage::OnDestroy()
  123. {
  124. UpdateData();
  125. AfxGetApp()->WriteProfileInt("Settings", "BlockAll", m_bBlockAll);
  126. CDialogResize::OnDestroy();
  127. }
  128. /********************************************************************/
  129. /* */
  130. /* Function name : OnBlockAll */
  131. /* Description : Block all except... has been clicked. */
  132. /* */
  133. /********************************************************************/
  134. void CSecurityPage::OnBlockAll()
  135. {
  136. UpdateData();
  137. GetDlgItem(IDC_NONBLOCKEDLIST)->EnableWindow(m_bBlockAll);
  138. GetDlgItem(IDC_ADD_NONBLOCK)->EnableWindow(m_bBlockAll);
  139. GetDlgItem(IDC_EDIT_NONBLOCK)->EnableWindow(m_bBlockAll);
  140. GetDlgItem(IDC_REMOVE_NONBLOCK)->EnableWindow(m_bBlockAll);
  141. GetDlgItem(IDC_BLOCKEDLIST)->EnableWindow(!m_bBlockAll);
  142. GetDlgItem(IDC_ADD_BLOCK)->EnableWindow(!m_bBlockAll);
  143. GetDlgItem(IDC_EDIT_BLOCK)->EnableWindow(!m_bBlockAll);
  144. GetDlgItem(IDC_REMOVE_BLOCK)->EnableWindow(!m_bBlockAll);
  145. GetDlgItem(IDC_STATIC1)->EnableWindow(!m_bBlockAll);
  146. theServer.SetSecurityMode(!m_bBlockAll);
  147. }
  148. /********************************************************************/
  149. /* */
  150. /* Function name : OnAddBlock */
  151. /* Description : Add IP address to blocked list. */
  152. /* */
  153. /********************************************************************/
  154. void CSecurityPage::OnAddBlock()
  155. {
  156. CAddIPDlg dlg;
  157. if (dlg.DoModal() == IDOK)
  158. {
  159. for (int i=0; i < m_BlockedList.GetCount(); i++)
  160. {
  161. CString strText;
  162. m_BlockedList.GetText(i, strText);
  163. if (strText.CompareNoCase(dlg.m_strIPaddress) == 0)
  164. {
  165. // already exists !
  166. return;
  167. }
  168. }
  169. int nIndex = m_BlockedList.AddString(dlg.m_strIPaddress);
  170. m_BlockedList.SetCurSel(nIndex);
  171. UpdateSecurityData(0);
  172. }
  173. }
  174. /********************************************************************/
  175. /* */
  176. /* Function name : OnEditBlock */
  177. /* Description : Edit IP address from blocked list. */
  178. /* */
  179. /********************************************************************/
  180. void CSecurityPage::OnEditBlock()
  181. {
  182. int nIndex = m_BlockedList.GetCurSel();
  183. if (nIndex == -1)
  184. return;
  185. CAddIPDlg dlg;
  186. dlg.m_strTitle = "Edit IP address";
  187. m_BlockedList.GetText(nIndex, dlg.m_strIPaddress);
  188. if (dlg.DoModal() == IDOK)
  189. {
  190. for (int i=0; i < m_BlockedList.GetCount(); i++)
  191. {
  192. CString strText;
  193. m_BlockedList.GetText(i, strText);
  194. if (strText.CompareNoCase(dlg.m_strIPaddress) == 0)
  195. {
  196. // already exists !
  197. return;
  198. }
  199. }
  200. m_BlockedList.DeleteString(nIndex);
  201. nIndex = m_BlockedList.AddString(dlg.m_strIPaddress);
  202. m_BlockedList.SetCurSel(nIndex);
  203. UpdateSecurityData(0);
  204. }
  205. }
  206. /********************************************************************/
  207. /* */
  208. /* Function name : OnRemoveBlock */
  209. /* Description : Remove IP address from blocked list. */
  210. /* */
  211. /********************************************************************/
  212. void CSecurityPage::OnRemoveBlock()
  213. {
  214. int nIndex = m_BlockedList.GetCurSel();
  215. if (nIndex == -1)
  216. return;
  217. m_BlockedList.DeleteString(nIndex);
  218. m_BlockedList.SetCurSel(0);
  219. UpdateSecurityData(0);
  220. }
  221. /********************************************************************/
  222. /* */
  223. /* Function name : OnAddNonblock */
  224. /* Description : Add IP address to non-blocked list. */
  225. /* */
  226. /********************************************************************/
  227. void CSecurityPage::OnAddNonblock()
  228. {
  229. CAddIPDlg dlg;
  230. if (dlg.DoModal() == IDOK)
  231. {
  232. for (int i=0; i < m_NonBlockedList.GetCount(); i++)
  233. {
  234. CString strText;
  235. m_NonBlockedList.GetText(i, strText);
  236. if (strText.CompareNoCase(dlg.m_strIPaddress) == 0)
  237. {
  238. // already exists !
  239. return;
  240. }
  241. }
  242. int nIndex = m_NonBlockedList.AddString(dlg.m_strIPaddress);
  243. m_NonBlockedList.SetCurSel(nIndex);
  244. UpdateSecurityData(1);
  245. }
  246. }
  247. /********************************************************************/
  248. /* */
  249. /* Function name : OnEditNonblock */
  250. /* Description : Edit IP address from non-blocked list. */
  251. /* */
  252. /********************************************************************/
  253. void CSecurityPage::OnEditNonblock()
  254. {
  255. int nIndex = m_NonBlockedList.GetCurSel();
  256. if (nIndex == -1)
  257. return;
  258. CAddIPDlg dlg;
  259. dlg.m_strTitle = "Edit IP address";
  260. m_NonBlockedList.GetText(nIndex, dlg.m_strIPaddress);
  261. if (dlg.DoModal() == IDOK)
  262. {
  263. for (int i=0; i < m_NonBlockedList.GetCount(); i++)
  264. {
  265. CString strText;
  266. m_NonBlockedList.GetText(i, strText);
  267. if (strText.CompareNoCase(dlg.m_strIPaddress) == 0)
  268. {
  269. // already exists !
  270. return;
  271. }
  272. }
  273. m_NonBlockedList.DeleteString(nIndex);
  274. nIndex = m_NonBlockedList.AddString(dlg.m_strIPaddress);
  275. m_NonBlockedList.SetCurSel(nIndex);
  276. UpdateSecurityData(1);
  277. }
  278. }
  279. /********************************************************************/
  280. /* */
  281. /* Function name : OnRemoveNonblock */
  282. /* Description : Remove IP address from non-blocked list. */
  283. /* */
  284. /********************************************************************/
  285. void CSecurityPage::OnRemoveNonblock()
  286. {
  287. int nIndex = m_NonBlockedList.GetCurSel();
  288. if (nIndex == -1)
  289. return;
  290. m_NonBlockedList.DeleteString(nIndex);
  291. m_NonBlockedList.SetCurSel(0);
  292. UpdateSecurityData(1);
  293. }
  294. /********************************************************************/
  295. /* */
  296. /* Function name : UpdateSecurityData */
  297. /* Description : Update security data. */
  298. /* */
  299. /********************************************************************/
  300. void CSecurityPage::UpdateSecurityData(int nType)
  301. {
  302. CStringArray strArray;
  303. if (nType == 0)
  304. {
  305. for (int i=0; i < m_BlockedList.GetCount(); i++)
  306. {
  307. CString strText;
  308. m_BlockedList.GetText(i, strText);
  309. strArray.Add(strText);
  310. }
  311. theServer.m_SecurityManager.UpdateBlockedList(strArray);
  312. }
  313. else
  314. {
  315. for (int i=0; i < m_NonBlockedList.GetCount(); i++)
  316. {
  317. CString strText;
  318. m_NonBlockedList.GetText(i, strText);
  319. strArray.Add(strText);
  320. }
  321. theServer.m_SecurityManager.UpdateNonBlockedList(strArray);
  322. }
  323. }
  324. /********************************************************************/
  325. /* */
  326. /* Function name : AddIPToBlockList */
  327. /* Description : Add IP address to blocked list. */
  328. /* */
  329. /********************************************************************/
  330. void CSecurityPage::AddIPToBlockList(LPCTSTR lpszIP)
  331. {
  332. for (int i=0; i < m_BlockedList.GetCount(); i++)
  333. {
  334. CString strText;
  335. m_BlockedList.GetText(i, strText);
  336. if (strText.CompareNoCase(lpszIP) == 0)
  337. {
  338. // already exists !
  339. return;
  340. }
  341. }
  342. int nIndex = m_BlockedList.AddString(lpszIP);
  343. m_BlockedList.SetCurSel(nIndex);
  344. UpdateSecurityData(0);
  345. }
  346. /********************************************************************/
  347. /* */
  348. /* Function name : OnDblclkBlockedlist */
  349. /* Description : Edit IP address from blocked list. */
  350. /* */
  351. /********************************************************************/
  352. void CSecurityPage::OnDblclkBlockedlist()
  353. {
  354. OnEditBlock();
  355. }
  356. /********************************************************************/
  357. /* */
  358. /* Function name : OnDblclkNonblockedlist */
  359. /* Description : Edit IP address from non-blocked list. */
  360. /* */
  361. /********************************************************************/
  362. void CSecurityPage::OnDblclkNonblockedlist()
  363. {
  364. OnEditNonblock();
  365. }