banmanager.py

Go to the documentation of this file.
00001 # This file is part of Fail2Ban.
00002 #
00003 # Fail2Ban is free software; you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation; either version 2 of the License, or
00006 # (at your option) any later version.
00007 #
00008 # Fail2Ban is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with Fail2Ban; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 
00017 # Author: Cyril Jaquier
00018 # 
00019 # $Revision: 686 $
00020 
00021 __author__ = "Cyril Jaquier"
00022 __version__ = "$Revision: 686 $"
00023 __date__ = "$Date: 2008-04-13 19:48:52 +0200 (Sun, 13 Apr 2008) $"
00024 __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
00025 __license__ = "GPL"
00026 
00027 from ticket import BanTicket
00028 from threading import Lock
00029 from mytime import MyTime
00030 import logging
00031 
00032 # Gets the instance of the logger.
00033 logSys = logging.getLogger("fail2ban.action")
00034 
00035 ##
00036 # Banning Manager.
00037 #
00038 # Manage the banned IP addresses. Convert FailTicket to BanTicket.
00039 # This class is mainly used by the Action class.
00040 
00041 class BanManager:
00042     
00043     ##
00044     # Constructor.
00045     #
00046     # Initialize members with default values.
00047     
00048     def __init__(self):
00049         ## Mutex used to protect the ban list.
00050         self.__lock = Lock()
00051         ## The ban list.
00052         self.__banList = list()
00053         ## The amount of time an IP address gets banned.
00054         self.__banTime = 600
00055         ## Total number of banned IP address
00056         self.__banTotal = 0
00057     
00058     ##
00059     # Set the ban time.
00060     #
00061     # Set the amount of time an IP address get banned.
00062     # @param value the time
00063     
00064     def setBanTime(self, value):
00065         try:
00066             self.__lock.acquire()
00067             self.__banTime = int(value)
00068         finally:
00069             self.__lock.release()
00070     
00071     ##
00072     # Get the ban time.
00073     #
00074     # Get the amount of time an IP address get banned.
00075     # @return the time
00076     
00077     def getBanTime(self):
00078         try:
00079             self.__lock.acquire()
00080             return self.__banTime
00081         finally:
00082             self.__lock.release()
00083     
00084     ##
00085     # Set the total number of banned address.
00086     #
00087     # @param value total number
00088     
00089     def setBanTotal(self, value):
00090         try:
00091             self.__lock.acquire()
00092             self.__banTotal = value
00093         finally:
00094             self.__lock.release()
00095     
00096     ##
00097     # Get the total number of banned address.
00098     #
00099     # @return the total number
00100     
00101     def getBanTotal(self):
00102         try:
00103             self.__lock.acquire()
00104             return self.__banTotal
00105         finally:
00106             self.__lock.release()
00107 
00108     ##
00109     # Returns a copy of the IP list.
00110     #
00111     # @return IP list
00112     
00113     def getBanList(self):
00114         try:
00115             self.__lock.acquire()
00116             return [m.getIP() for m in self.__banList]
00117         finally:
00118             self.__lock.release()
00119 
00120     ##
00121     # Create a ban ticket.
00122     #
00123     # Create a BanTicket from a FailTicket. The timestamp of the BanTicket
00124     # is the current time. This is a static method.
00125     # @param ticket the FailTicket
00126     # @return a BanTicket
00127     
00128     #@staticmethod
00129     def createBanTicket(ticket):
00130         ip = ticket.getIP()
00131         #lastTime = ticket.getTime()
00132         lastTime = MyTime.time()
00133         banTicket = BanTicket(ip, lastTime)
00134         banTicket.setAttempt(ticket.getAttempt())
00135         return banTicket
00136     createBanTicket = staticmethod(createBanTicket)
00137     
00138     ##
00139     # Add a ban ticket.
00140     #
00141     # Add a BanTicket instance into the ban list.
00142     # @param ticket the ticket
00143     # @return True if the IP address is not in the ban list
00144     
00145     def addBanTicket(self, ticket):
00146         try:
00147             self.__lock.acquire()
00148             if not self.__inBanList(ticket):
00149                 self.__banList.append(ticket)
00150                 self.__banTotal += 1
00151                 return True
00152             return False
00153         finally:
00154             self.__lock.release()
00155     
00156     
00157     ##
00158     # Get the size of the ban list.
00159     #
00160     # @return the size
00161     
00162     def size(self):
00163         try:
00164             self.__lock.acquire()
00165             return len(self.__banList)
00166         finally:
00167             self.__lock.release()
00168     
00169     ##
00170     # Check if a ticket is in the list.
00171     #
00172     # Check if a BanTicket with a given IP address is already in the
00173     # ban list.
00174     # @param ticket the ticket
00175     # @return True if a ticket already exists
00176     
00177     def __inBanList(self, ticket):
00178         for i in self.__banList:
00179             if ticket.getIP() == i.getIP():
00180                 return True
00181         return False
00182     
00183     ##
00184     # Get the list of IP address to unban.
00185     #
00186     # Return a list of BanTicket which need to be unbanned.
00187     # @param time the time
00188     # @return the list of ticket to unban
00189     
00190     def unBanList(self, time):
00191         try:
00192             self.__lock.acquire()
00193             # Permanent banning
00194             if self.__banTime < 0:
00195                 return list()
00196 
00197             # Gets the list of ticket to remove.
00198             unBanList = [ticket for ticket in self.__banList
00199                          if ticket.getTime() < time - self.__banTime]
00200             
00201             # Removes tickets.
00202             self.__banList = [ticket for ticket in self.__banList
00203                               if ticket not in unBanList]
00204                         
00205             return unBanList
00206         finally:
00207             self.__lock.release()
00208     
00209     ##
00210     # Flush the ban list.
00211     #
00212     # Get the ban list and initialize it with an empty one.
00213     # @return the complete ban list
00214     
00215     def flushBanList(self):
00216         try:
00217             self.__lock.acquire()
00218             uBList = self.__banList
00219             self.__banList = list()
00220             return uBList
00221         finally:
00222             self.__lock.release()
Generated on Sun May 19 03:01:59 2013 for Fail2Ban by  doxygen 1.6.3