www.pudn.com > FTPServerSrc.rar > SecurityPage.cpp
/****************************************************************/
/* */
/* SecurityPage.cpp */
/* */
/* Implementation of the CSecurityPage class. */
/* This class is a part of the FTP Server. */
/* */
/* Programmed by Pablo van der Meer */
/* Copyright Pablo Software Solutions 2002 */
/* http://www.pablovandermeer.nl */
/* */
/* Last updated: 05 september 2002 */
/* */
/****************************************************************/
#include "stdafx.h"
#include "FTPServerApp.h"
#include "ftpserver.h"
#include "SecurityPage.h"
#include "AddIPDlg.h"
extern CFTPServer theServer;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CSecurityPage::CSecurityPage(CWnd* pParent /*=NULL*/)
: CDialogResize(CSecurityPage::IDD, pParent)
{
//{{AFX_DATA_INIT(CSecurityPage)
m_nFilterMode = -1;
//}}AFX_DATA_INIT
}
void CSecurityPage::DoDataExchange(CDataExchange* pDX)
{
CDialogResize::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSecurityPage)
DDX_Control(pDX, IDC_REMOVE_IP, m_btnDel);
DDX_Control(pDX, IDC_EDIT_IP, m_btnEdit);
DDX_Control(pDX, IDC_ADD_IP, m_btnAdd);
DDX_Control(pDX, IDC_IPFILTERLIST, m_IPFilterList);
DDX_Radio(pDX, IDC_FILTERMODE, m_nFilterMode);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSecurityPage, CDialogResize)
//{{AFX_MSG_MAP(CSecurityPage)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_ADD_IP, OnAddIP)
ON_BN_CLICKED(IDC_EDIT_IP, OnEditIP)
ON_BN_CLICKED(IDC_REMOVE_IP, OnRemoveIP)
ON_LBN_DBLCLK(IDC_IPFILTERLIST, OnDblclkIPFilterlist)
ON_BN_CLICKED(IDC_FILTERMODE, OnFiltermode)
ON_BN_CLICKED(IDC_RADIO2, OnFiltermode)
ON_BN_CLICKED(IDC_RADIO3, OnFiltermode)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BEGIN_DLGRESIZE_MAP(CSecurityPage)
DLGRESIZE_CONTROL(IDC_IPFILTERLIST, DLSZ_SIZE_X | DLSZ_SIZE_Y)
DLGRESIZE_CONTROL(IDC_ADD_IP, DLSZ_MOVE_X)
DLGRESIZE_CONTROL(IDC_EDIT_IP, DLSZ_MOVE_X)
DLGRESIZE_CONTROL(IDC_REMOVE_IP, DLSZ_MOVE_X)
DLGRESIZE_CONTROL(IDC_FILTERMODE, DLSZ_MOVE_Y)
DLGRESIZE_CONTROL(IDC_RADIO2, DLSZ_MOVE_Y)
DLGRESIZE_CONTROL(IDC_RADIO3, DLSZ_MOVE_Y)
DLGRESIZE_CONTROL(IDC_STATIC1, DLSZ_MOVE_Y)
DLGRESIZE_CONTROL(IDC_STATIC2, DLSZ_MOVE_Y)
DLGRESIZE_CONTROL(IDC_STATIC3, DLSZ_MOVE_Y)
DLGRESIZE_CONTROL(IDC_IPADDRESS1, DLSZ_MOVE_Y)
DLGRESIZE_CONTROL(IDC_IPADDRESS2, DLSZ_MOVE_Y)
END_DLGRESIZE_MAP()
/********************************************************************/
/* */
/* Function name : OnInitDialog */
/* Description : Initialize dialog */
/* */
/********************************************************************/
BOOL CSecurityPage::OnInitDialog()
{
CDialogResize::OnInitDialog();
InitResizing(FALSE, FALSE, WS_CLIPCHILDREN);
m_nFilterMode = AfxGetApp()->GetProfileInt("Settings", "IPFilterMode", 0);
UpdateData(FALSE);
CStringArray strArray;
theServer.m_SecurityManager.GetIPFilterList(strArray);
for (int i=0; i < strArray.GetSize(); i++)
{
m_IPFilterList.AddString(strArray[i]);
}
// get list of all ip addresses in use by this system (only show first two...)
char szHostName[128];
HOSTENT *lpHost=NULL;
struct sockaddr_in sock;
gethostname(szHostName, sizeof(szHostName));
lpHost = gethostbyname(szHostName);
if (lpHost != NULL)
{
for(int i=0; lpHost->h_addr_list[i] != NULL ;i++)
{
memcpy(&(sock.sin_addr), lpHost->h_addr_list[i], lpHost->h_length);
if (i == 0)
{
SetDlgItemText(IDC_IPADDRESS1, inet_ntoa(sock.sin_addr));
}
else
if (i == 1)
{
SetDlgItemText(IDC_IPADDRESS2, inet_ntoa(sock.sin_addr));
}
}
}
m_btnAdd.SetIcon(IDI_ADDIP, 16, 16);
m_btnEdit.SetIcon(IDI_IP, 16, 16);
m_btnDel.SetIcon(IDI_DELIP, 16, 16);
return TRUE;
}
/********************************************************************/
/* */
/* Function name : OnDestroy */
/* Description : Dialog is about to be destroyed. */
/* */
/********************************************************************/
void CSecurityPage::OnDestroy()
{
UpdateData();
AfxGetApp()->WriteProfileInt("Settings", "IPFilterMode", m_nFilterMode);
CDialogResize::OnDestroy();
}
/********************************************************************/
/* */
/* Function name : OnAddIP */
/* Description : Add IP address to filter list. */
/* */
/********************************************************************/
void CSecurityPage::OnAddIP()
{
CAddIPDlg dlg;
if (dlg.DoModal() == IDOK)
{
for (int i=0; i < m_IPFilterList.GetCount(); i++)
{
CString strText;
m_IPFilterList.GetText(i, strText);
if (strText.CompareNoCase(dlg.m_strIPaddress) == 0)
{
// already exists !
return;
}
}
int nIndex = m_IPFilterList.AddString(dlg.m_strIPaddress);
m_IPFilterList.SetCurSel(nIndex);
UpdateSecurityData(0);
}
}
/********************************************************************/
/* */
/* Function name : OnEditIP */
/* Description : Edit IP address from filter list. */
/* */
/********************************************************************/
void CSecurityPage::OnEditIP()
{
int nIndex = m_IPFilterList.GetCurSel();
if (nIndex == -1)
return;
CAddIPDlg dlg;
dlg.m_strTitle = "Edit IP address";
m_IPFilterList.GetText(nIndex, dlg.m_strIPaddress);
if (dlg.DoModal() == IDOK)
{
for (int i=0; i < m_IPFilterList.GetCount(); i++)
{
CString strText;
m_IPFilterList.GetText(i, strText);
if (strText.CompareNoCase(dlg.m_strIPaddress) == 0)
{
// already exists !
return;
}
}
m_IPFilterList.DeleteString(nIndex);
nIndex = m_IPFilterList.AddString(dlg.m_strIPaddress);
m_IPFilterList.SetCurSel(nIndex);
UpdateSecurityData(0);
}
}
/********************************************************************/
/* */
/* Function name : OnRemoveIP */
/* Description : Remove IP address from filter list. */
/* */
/********************************************************************/
void CSecurityPage::OnRemoveIP()
{
int nIndex = m_IPFilterList.GetCurSel();
if (nIndex == -1)
return;
m_IPFilterList.DeleteString(nIndex);
m_IPFilterList.SetCurSel(0);
UpdateSecurityData(0);
}
/********************************************************************/
/* */
/* Function name : AddIPToIPFilterList */
/* Description : Add IP address to filter list. */
/* */
/********************************************************************/
void CSecurityPage::AddIPToIPFilterList(LPCTSTR lpszIP)
{
for (int i=0; i < m_IPFilterList.GetCount(); i++)
{
CString strText;
m_IPFilterList.GetText(i, strText);
if (strText.CompareNoCase(lpszIP) == 0)
{
// already exists !
return;
}
}
int nIndex = m_IPFilterList.AddString(lpszIP);
m_IPFilterList.SetCurSel(nIndex);
UpdateSecurityData(0);
}
/********************************************************************/
/* */
/* Function name : OnDblclkIPFilterlist */
/* Description : Edit IP address from filter list. */
/* */
/********************************************************************/
void CSecurityPage::OnDblclkIPFilterlist()
{
OnEditIP();
}
/********************************************************************/
/* */
/* Function name : UpdateSecurityData */
/* Description : Update security data. */
/* */
/********************************************************************/
void CSecurityPage::UpdateSecurityData(int nType)
{
CStringArray strArray;
if (nType == 0)
{
for (int i=0; i < m_IPFilterList.GetCount(); i++)
{
CString strText;
m_IPFilterList.GetText(i, strText);
strArray.Add(strText);
}
theServer.m_SecurityManager.UpdateIPFilterList(strArray);
}
}
void CSecurityPage::OnOK()
{
// CDialogResize::OnOK();
}
void CSecurityPage::OnCancel()
{
// CDialogResize::OnCancel();
}
/********************************************************************/
/* */
/* Function name : OnFiltermode */
/* Description : Filter mode has changed. */
/* */
/********************************************************************/
void CSecurityPage::OnFiltermode()
{
UpdateData();
theServer.SetSecurityMode(m_nFilterMode);
}