jail.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: 697 $
00020 
00021 __author__ = "Cyril Jaquier"
00022 __version__ = "$Revision: 697 $"
00023 __date__ = "$Date: 2008-05-19 23:08:36 +0200 (Mon, 19 May 2008) $"
00024 __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
00025 __license__ = "GPL"
00026 
00027 import Queue, logging
00028 
00029 from actions import Actions
00030 
00031 # Gets the instance of the logger.
00032 logSys = logging.getLogger("fail2ban.jail")
00033 
00034 class Jail:
00035     
00036     def __init__(self, name, backend = "auto"):
00037         self.__name = name
00038         self.__queue = Queue.Queue()
00039         self.__filter = None
00040         logSys.info("Creating new jail '%s'" % self.__name)
00041         if backend == "polling":
00042             self.__initPoller()
00043         else:
00044             try:
00045                 self.__initGamin()
00046             except ImportError:
00047                 self.__initPoller()
00048         self.__action = Actions(self)
00049     
00050     def __initPoller(self):
00051         logSys.info("Jail '%s' uses poller" % self.__name)
00052         from filterpoll import FilterPoll
00053         self.__filter = FilterPoll(self)
00054     
00055     def __initGamin(self):
00056         # Try to import gamin
00057         import gamin
00058         logSys.info("Jail '%s' uses Gamin" % self.__name)
00059         from filtergamin import FilterGamin
00060         self.__filter = FilterGamin(self)
00061     
00062     def setName(self, name):
00063         self.__name = name
00064     
00065     def getName(self):
00066         return self.__name
00067     
00068     def getFilter(self):
00069         return self.__filter
00070     
00071     def getAction(self):
00072         return self.__action
00073     
00074     def putFailTicket(self, ticket):
00075         self.__queue.put(ticket)
00076     
00077     def getFailTicket(self):
00078         try:
00079             return self.__queue.get(False)
00080         except Queue.Empty:
00081             return False
00082     
00083     def start(self):
00084         self.__filter.start()
00085         self.__action.start()
00086         logSys.info("Jail '%s' started" % self.__name)
00087     
00088     def stop(self):
00089         self.__filter.stop()
00090         self.__action.stop()
00091         self.__filter.join()
00092         self.__action.join()
00093         logSys.info("Jail '%s' stopped" % self.__name)
00094     
00095     def isAlive(self):
00096         isAlive0 = self.__filter.isAlive()
00097         isAlive1 = self.__action.isAlive()
00098         return isAlive0 or isAlive1
00099     
00100     def setIdle(self, value):
00101         self.__filter.setIdle(value)
00102         self.__action.setIdle(value)
00103     
00104     def getIdle(self):
00105         return self.__filter.getIdle() or self.__action.getIdle()
00106     
00107     def getStatus(self):
00108         fStatus = self.__filter.status()
00109         aStatus = self.__action.status()
00110         ret = [("filter", fStatus), 
00111                ("action", aStatus)]
00112         return ret
Generated on Sat May 18 03:01:45 2013 for Fail2Ban by  doxygen 1.6.3