www.pudn.com > BesidesFTPServer.rar > SecurityManager.cpp


/****************************************************************/ 
/*																*/ 
/*  SecurityManager.cpp											*/ 
/*																*/ 
/*  Implementation of the CSecurityManager class.				*/ 
/*																*/ 
/*  Programmed by Pablo van der Meer							*/ 
/*  Copyright Pablo Software Solutions 2002						*/ 
/*	http://www.pablovandermeer.nl								*/ 
/*																*/ 
/*  Last updated: 05 september 2002								*/ 
/*																*/ 
/****************************************************************/ 
 
 
#include "stdafx.h" 
#include "ftpserver.h" 
#include "SecurityManager.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
CSecurityManager::CSecurityManager() 
{ 
	GetAppDir(m_strFilename); 
	m_strFilename += "security.dat"; 
} 
 
CSecurityManager::~CSecurityManager() 
{ 
 
} 
 
 
/********************************************************************/ 
/*																	*/ 
/* Function name : Serialize										*/ 
/* Description   : Call this function to store/load security data	*/ 
/*																	*/ 
/********************************************************************/ 
BOOL CSecurityManager::Serialize(BOOL bStoring) 
{ 
	static const TCHAR* lpszSignature = _T("LQB Software Solutions - Security"); 
 
	CFile file; 
 
	if (file.Open(m_strFilename, bStoring ? CFile::modeWrite|CFile::modeCreate : CFile::modeRead)) 
	{ 
		TRY 
		{ 
			CString str;  
			CArchive ar(&file, bStoring ? CArchive::store : CArchive::load); 
			 
			if (bStoring) 
			{ 
				// save signature 
				ar << CString(lpszSignature); 
 
				// Save the changed user details 
				m_IPFilterList.Serialize(ar); 
 
				ar.Flush(); 
			} 
			else 
			{ 
				// load signature 
				ar >> str; 
				// if this the file we are looking for ? 
				if (str.Compare(lpszSignature) == 0) 
				{ 
					m_IPFilterList.Serialize(ar); 
				} 
			} 
			ar.Close(); 
			file.Close(); 
		} 
		CATCH_ALL(e) 
		{ 
			// catch all exceptions that might happen ... 
			return FALSE; 
		} 
		END_CATCH_ALL 
	} 
	return TRUE; 
} 
 
 
/********************************************************************/ 
/*																	*/ 
/* Function name : GetIPFilterList									*/ 
/* Description   : Get list of blocked IP addresses					*/ 
/*																	*/ 
/********************************************************************/ 
void CSecurityManager::GetIPFilterList(CStringArray &strArray) 
{ 
	m_CriticalSection.Lock(); 
	strArray.RemoveAll(); 
	strArray.Copy(m_IPFilterList); 
	m_CriticalSection.Unlock(); 
} 
 
 
/********************************************************************/ 
/*																	*/ 
/* Function name : UpdateIPFilterList								*/ 
/* Description   : Update list of IP addresses that are blocked.	*/ 
/*																	*/ 
/********************************************************************/ 
void CSecurityManager::UpdateIPFilterList(CStringArray &strArray) 
{ 
	m_CriticalSection.Lock(); 
	m_IPFilterList.RemoveAll(); 
	m_IPFilterList.Copy(strArray); 
	Serialize(TRUE); 
	m_CriticalSection.Unlock(); 
} 
 
 
/********************************************************************/ 
/*																	*/ 
/* Function name : IsIPAddressInList								*/ 
/* Description   : Check if specified IP is in list.				*/ 
/*																	*/ 
/********************************************************************/ 
BOOL CSecurityManager::IsIPAddressInList(LPCTSTR lpszIPAddress) 
{ 
	m_CriticalSection.Lock(); 
	for (int i=0; i