123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- /****************************************************************/
- /* */
- /* SecurityManager.cpp */
- /* */
- /* Implementation of the CSecurityManager class. */
- /* */
- /* Programmed by LYFZ van der Meer */
- /* Copyright LYFZ Software Solutions 2002 */
- /* http://www.LYFZvandermeer.nl */
- /* */
- /* Last updated: 10 july 2002 */
- /* */
- /****************************************************************/
- #include "stdafx.h"
- #include "theDBServer.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("Li 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_BlockedList.Serialize(ar);
- m_NonBlockedList.Serialize(ar);
- ar.Flush();
- }
- else
- {
- // load signature
- ar >> str;
- // if this the file we are looking for ?
- if (str.Compare(lpszSignature) == 0)
- {
- m_BlockedList.Serialize(ar);
- m_NonBlockedList.Serialize(ar);
- }
- }
- ar.Close();
- file.Close();
- }
- CATCH_ALL(e)
- {
- // catch all exceptions that might happen ...
- return FALSE;
- }
- END_CATCH_ALL
- }
- return TRUE;
- }
- /********************************************************************/
- /* */
- /* Function name : GetBlockedList */
- /* Description : Get list of blocked IP addresses */
- /* */
- /********************************************************************/
- void CSecurityManager::GetBlockedList(CStringArray &strArray)
- {
- m_CriticalSection.Lock();
- strArray.RemoveAll();
- strArray.Copy(m_BlockedList);
- m_CriticalSection.Unlock();
- }
- /********************************************************************/
- /* */
- /* Function name : GetNonBlockedList */
- /* Description : Get list of IP addresses that are not blocked. */
- /* */
- /********************************************************************/
- void CSecurityManager::GetNonBlockedList(CStringArray &strArray)
- {
- m_CriticalSection.Lock();
- strArray.RemoveAll();
- strArray.Copy(m_NonBlockedList);
- m_CriticalSection.Unlock();
- }
- /********************************************************************/
- /* */
- /* Function name : UpdateBlockedList */
- /* Description : Update list of IP addresses that are blocked. */
- /* */
- /********************************************************************/
- void CSecurityManager::UpdateBlockedList(CStringArray &strArray)
- {
- m_CriticalSection.Lock();
- m_BlockedList.RemoveAll();
- m_BlockedList.Copy(strArray);
- Serialize(TRUE);
- m_CriticalSection.Unlock();
- }
- /********************************************************************/
- /* */
- /* Function name : UpdateNonBlockedList */
- /* Description : Update list of IP addresses that are not blocked.*/
- /* */
- /********************************************************************/
- void CSecurityManager::UpdateNonBlockedList(CStringArray &strArray)
- {
- m_CriticalSection.Lock();
- m_NonBlockedList.RemoveAll();
- m_NonBlockedList.Copy(strArray);
- Serialize(TRUE);
- m_CriticalSection.Unlock();
- }
- /********************************************************************/
- /* */
- /* Function name : IsIPAddressBlocked */
- /* Description : Check if specified IP is blocked. */
- /* */
- /********************************************************************/
- BOOL CSecurityManager::IsIPAddressBlocked(LPCTSTR lpszIPAddress)
- {
- m_CriticalSection.Lock();
- for (int i=0; i<m_BlockedList.GetSize(); i++)
- {
- if (m_BlockedList[i].CompareNoCase(lpszIPAddress) == 0)
- {
- m_CriticalSection.Unlock();
- return TRUE;
- }
- // support for range
- if (m_BlockedList[i].Right(1) == '*')
- {
- CString strIP1 = m_BlockedList[i];
- strIP1.TrimRight('*');
- CString strIP2 = lpszIPAddress;
- if (strIP2.Find(strIP1, 0) != -1)
- {
- m_CriticalSection.Unlock();
- return TRUE;
- }
- }
- }
- m_CriticalSection.Unlock();
- return FALSE;
- }
- /********************************************************************/
- /* */
- /* Function name : IsIPAddressNonBlocked */
- /* Description : Check if specified IP is non-blocked. */
- /* */
- /********************************************************************/
- BOOL CSecurityManager::IsIPAddressNonBlocked(LPCTSTR lpszIPAddress)
- {
- m_CriticalSection.Lock();
- for (int i=0; i<m_NonBlockedList.GetSize(); i++)
- {
- if (m_NonBlockedList[i].CompareNoCase(lpszIPAddress) == 0)
- {
- m_CriticalSection.Unlock();
- return TRUE;
- }
- // support for range
- if (m_NonBlockedList[i].Right(1) == '*')
- {
- CString strIP1 = m_NonBlockedList[i];
- strIP1.TrimRight('*');
- CString strIP2 = lpszIPAddress;
- if (strIP2.Find(strIP1, 0) != -1)
- {
- m_CriticalSection.Unlock();
- return TRUE;
- }
- }
- }
- m_CriticalSection.Unlock();
- return FALSE;
- }
|