jails.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: 504 $
00020 
00021 __author__ = "Cyril Jaquier"
00022 __version__ = "$Revision: 504 $"
00023 __date__ = "$Date: 2006-12-23 17:37:17 +0100 (Sat, 23 Dec 2006) $"
00024 __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
00025 __license__ = "GPL"
00026 
00027 
00028 from jail import Jail
00029 from threading import Lock
00030 
00031 ##
00032 # Handles the jails.
00033 #
00034 # This class handles the jails. Creation, deletion or access to a jail must be
00035 # done through this class. This class is thread-safe which is not the case of
00036 # the jail itself, including filter and actions.
00037 
00038 class Jails:
00039     
00040     ##
00041     # Constructor.
00042     
00043     def __init__(self):
00044         self.__lock = Lock()
00045         self.__jails = dict()
00046     
00047     ##
00048     # Adds a jail.
00049     #
00050     # Adds a new jail which should use the given backend. Raises a
00051     # <code>DuplicateJailException</code> if the jail is already defined.
00052     # @param name The name of the jail
00053     # @param backend The backend to use
00054     
00055     def add(self, name, backend):
00056         try:
00057             self.__lock.acquire()
00058             if self.__jails.has_key(name):
00059                 raise DuplicateJailException(name)
00060             else:
00061                 self.__jails[name] = Jail(name, backend)
00062         finally:
00063             self.__lock.release()
00064     
00065     ##
00066     # Removes a jail.
00067     #
00068     # Removes the jail <code>name</code>. Raise an <code>UnknownJailException</code>
00069     # if the jail does not exist.
00070     # @param name The name of the jail
00071     
00072     def remove(self, name):
00073         try:
00074             self.__lock.acquire()
00075             if self.__jails.has_key(name):
00076                 del self.__jails[name]
00077             else:
00078                 raise UnknownJailException(name)
00079         finally:
00080             self.__lock.release()
00081     
00082     ##
00083     # Returns a jail.
00084     #
00085     # Returns the jail <code>name</code>. Raise an <code>UnknownJailException</code>
00086     # if the jail does not exist.
00087     # @param name The name of the jail
00088     
00089     def get(self, name):
00090         try:
00091             self.__lock.acquire()
00092             if self.__jails.has_key(name):
00093                 jail = self.__jails[name]
00094                 return jail
00095             else:
00096                 raise UnknownJailException(name)
00097         finally:
00098             self.__lock.release()
00099     
00100     ##
00101     # Returns an action class instance.
00102     #
00103     # Returns the action object of the jail <code>name</code>. Raise an
00104     # <code>UnknownJailException</code> if the jail does not exist.
00105     # @param name The name of the jail
00106     
00107     def getAction(self, name):
00108         try:
00109             self.__lock.acquire()
00110             if self.__jails.has_key(name):
00111                 action = self.__jails[name].getAction()
00112                 return action
00113             else:
00114                 raise UnknownJailException(name)
00115         finally:
00116             self.__lock.release()
00117     
00118     ##
00119     # Returns a filter class instance.
00120     #
00121     # Returns the filter object of the jail <code>name</code>. Raise an
00122     # <code>UnknownJailException</code> if the jail does not exist.
00123     # @param name The name of the jail
00124     
00125     def getFilter(self, name):
00126         try:
00127             self.__lock.acquire()
00128             if self.__jails.has_key(name):
00129                 action = self.__jails[name].getFilter()
00130                 return action
00131             else:
00132                 raise UnknownJailException(name)
00133         finally:
00134             self.__lock.release()
00135     
00136     ##
00137     # Returns the jails.
00138     #
00139     # Returns a copy of the jails list.
00140     
00141     def getAll(self):
00142         try:
00143             self.__lock.acquire()
00144             return self.__jails.copy()
00145         finally:
00146             self.__lock.release()
00147     
00148     ##
00149     # Returns the size of the jails.
00150     #
00151     # Returns the number of jails.
00152     
00153     def size(self):
00154         try:
00155             self.__lock.acquire()
00156             return len(self.__jails)
00157         finally:
00158             self.__lock.release()
00159 
00160 
00161 class DuplicateJailException(Exception):
00162     pass
00163 
00164 class UnknownJailException(Exception):
00165     pass
Generated on Tue May 21 03:01:50 2013 for Fail2Ban by  doxygen 1.6.3