SecurityManager.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /****************************************************************/
  2. /* */
  3. /* SecurityManager.cpp */
  4. /* */
  5. /* Implementation of the CSecurityManager class. */
  6. /* */
  7. /* Programmed by LYFZ van der Meer */
  8. /* Copyright LYFZ Software Solutions 2002 */
  9. /* http://www.LYFZvandermeer.nl */
  10. /* */
  11. /* Last updated: 10 july 2002 */
  12. /* */
  13. /****************************************************************/
  14. #include "stdafx.h"
  15. #include "theDBServer.h"
  16. #include "SecurityManager.h"
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[]=__FILE__;
  20. #define new DEBUG_NEW
  21. #endif
  22. CSecurityManager::CSecurityManager()
  23. {
  24. GetAppDir(m_strFilename);
  25. m_strFilename += "security.dat";
  26. }
  27. CSecurityManager::~CSecurityManager()
  28. {
  29. }
  30. /********************************************************************/
  31. /* */
  32. /* Function name : Serialize */
  33. /* Description : Call this function to store/load security data */
  34. /* */
  35. /********************************************************************/
  36. BOOL CSecurityManager::Serialize(BOOL bStoring)
  37. {
  38. static const TCHAR* lpszSignature = _T("Li Software Solutions - Security");
  39. CFile file;
  40. if (file.Open(m_strFilename, bStoring ? CFile::modeWrite|CFile::modeCreate : CFile::modeRead))
  41. {
  42. TRY
  43. {
  44. CString str;
  45. CArchive ar(&file, bStoring ? CArchive::store : CArchive::load);
  46. if (bStoring)
  47. {
  48. // save signature
  49. ar << CString(lpszSignature);
  50. // Save the changed user details
  51. m_BlockedList.Serialize(ar);
  52. m_NonBlockedList.Serialize(ar);
  53. ar.Flush();
  54. }
  55. else
  56. {
  57. // load signature
  58. ar >> str;
  59. // if this the file we are looking for ?
  60. if (str.Compare(lpszSignature) == 0)
  61. {
  62. m_BlockedList.Serialize(ar);
  63. m_NonBlockedList.Serialize(ar);
  64. }
  65. }
  66. ar.Close();
  67. file.Close();
  68. }
  69. CATCH_ALL(e)
  70. {
  71. // catch all exceptions that might happen ...
  72. return FALSE;
  73. }
  74. END_CATCH_ALL
  75. }
  76. return TRUE;
  77. }
  78. /********************************************************************/
  79. /* */
  80. /* Function name : GetBlockedList */
  81. /* Description : Get list of blocked IP addresses */
  82. /* */
  83. /********************************************************************/
  84. void CSecurityManager::GetBlockedList(CStringArray &strArray)
  85. {
  86. m_CriticalSection.Lock();
  87. strArray.RemoveAll();
  88. strArray.Copy(m_BlockedList);
  89. m_CriticalSection.Unlock();
  90. }
  91. /********************************************************************/
  92. /* */
  93. /* Function name : GetNonBlockedList */
  94. /* Description : Get list of IP addresses that are not blocked. */
  95. /* */
  96. /********************************************************************/
  97. void CSecurityManager::GetNonBlockedList(CStringArray &strArray)
  98. {
  99. m_CriticalSection.Lock();
  100. strArray.RemoveAll();
  101. strArray.Copy(m_NonBlockedList);
  102. m_CriticalSection.Unlock();
  103. }
  104. /********************************************************************/
  105. /* */
  106. /* Function name : UpdateBlockedList */
  107. /* Description : Update list of IP addresses that are blocked. */
  108. /* */
  109. /********************************************************************/
  110. void CSecurityManager::UpdateBlockedList(CStringArray &strArray)
  111. {
  112. m_CriticalSection.Lock();
  113. m_BlockedList.RemoveAll();
  114. m_BlockedList.Copy(strArray);
  115. Serialize(TRUE);
  116. m_CriticalSection.Unlock();
  117. }
  118. /********************************************************************/
  119. /* */
  120. /* Function name : UpdateNonBlockedList */
  121. /* Description : Update list of IP addresses that are not blocked.*/
  122. /* */
  123. /********************************************************************/
  124. void CSecurityManager::UpdateNonBlockedList(CStringArray &strArray)
  125. {
  126. m_CriticalSection.Lock();
  127. m_NonBlockedList.RemoveAll();
  128. m_NonBlockedList.Copy(strArray);
  129. Serialize(TRUE);
  130. m_CriticalSection.Unlock();
  131. }
  132. /********************************************************************/
  133. /* */
  134. /* Function name : IsIPAddressBlocked */
  135. /* Description : Check if specified IP is blocked. */
  136. /* */
  137. /********************************************************************/
  138. BOOL CSecurityManager::IsIPAddressBlocked(LPCTSTR lpszIPAddress)
  139. {
  140. m_CriticalSection.Lock();
  141. for (int i=0; i<m_BlockedList.GetSize(); i++)
  142. {
  143. if (m_BlockedList[i].CompareNoCase(lpszIPAddress) == 0)
  144. {
  145. m_CriticalSection.Unlock();
  146. return TRUE;
  147. }
  148. // support for range
  149. if (m_BlockedList[i].Right(1) == '*')
  150. {
  151. CString strIP1 = m_BlockedList[i];
  152. strIP1.TrimRight('*');
  153. CString strIP2 = lpszIPAddress;
  154. if (strIP2.Find(strIP1, 0) != -1)
  155. {
  156. m_CriticalSection.Unlock();
  157. return TRUE;
  158. }
  159. }
  160. }
  161. m_CriticalSection.Unlock();
  162. return FALSE;
  163. }
  164. /********************************************************************/
  165. /* */
  166. /* Function name : IsIPAddressNonBlocked */
  167. /* Description : Check if specified IP is non-blocked. */
  168. /* */
  169. /********************************************************************/
  170. BOOL CSecurityManager::IsIPAddressNonBlocked(LPCTSTR lpszIPAddress)
  171. {
  172. m_CriticalSection.Lock();
  173. for (int i=0; i<m_NonBlockedList.GetSize(); i++)
  174. {
  175. if (m_NonBlockedList[i].CompareNoCase(lpszIPAddress) == 0)
  176. {
  177. m_CriticalSection.Unlock();
  178. return TRUE;
  179. }
  180. // support for range
  181. if (m_NonBlockedList[i].Right(1) == '*')
  182. {
  183. CString strIP1 = m_NonBlockedList[i];
  184. strIP1.TrimRight('*');
  185. CString strIP2 = lpszIPAddress;
  186. if (strIP2.Find(strIP1, 0) != -1)
  187. {
  188. m_CriticalSection.Unlock();
  189. return TRUE;
  190. }
  191. }
  192. }
  193. m_CriticalSection.Unlock();
  194. return FALSE;
  195. }