mirror of https://github.com/fail2ban/fail2ban
RF: traverse known backends in a loop
should now be - easier to comprehend - more scalable happens new backends come - more informative with all the info and debug messages NB not sure why __setBackend was instance's attribute, now there is a method _setBackendpull/25/head
parent
7413817f9a
commit
f90d53fc26
|
@ -18,13 +18,9 @@
|
||||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
|
||||||
# Author: Cyril Jaquier
|
# Author: Cyril Jaquier
|
||||||
#
|
|
||||||
# $Revision$
|
|
||||||
|
|
||||||
__author__ = "Cyril Jaquier"
|
__author__ = "Cyril Jaquier, Lee Celemens, Yaroslav Halchenko"
|
||||||
__version__ = "$Revision$"
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2011-2012 Lee Clemens, 2012 Yaroslav Halchenko"
|
||||||
__date__ = "$Date$"
|
|
||||||
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
||||||
__license__ = "GPL"
|
__license__ = "GPL"
|
||||||
|
|
||||||
import Queue, logging
|
import Queue, logging
|
||||||
|
@ -36,57 +32,61 @@ logSys = logging.getLogger("fail2ban.jail")
|
||||||
|
|
||||||
class Jail:
|
class Jail:
|
||||||
|
|
||||||
|
_BACKENDS = ('pyinotify', 'gamin', 'pull')
|
||||||
|
"""Known backends. Each backend should have corresponding
|
||||||
|
__initBackend method
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name, backend = "auto"):
|
def __init__(self, name, backend = "auto"):
|
||||||
self.__name = name
|
self.__name = name
|
||||||
self.__queue = Queue.Queue()
|
self.__queue = Queue.Queue()
|
||||||
self.__filter = None
|
self.__filter = None
|
||||||
logSys.info("Creating new jail '%s'" % self.__name)
|
logSys.info("Creating new jail '%s'" % self.__name)
|
||||||
self.__setBackend = False
|
self._setBackend(backend)
|
||||||
if backend == "auto":
|
|
||||||
# Quick-escape for auto (default/fall-back condition)
|
|
||||||
self.__setBackend = False
|
|
||||||
elif backend == "pyinotify":
|
|
||||||
try:
|
|
||||||
self.__initPyinotify()
|
|
||||||
self.__setBackend = True
|
|
||||||
except ImportError:
|
|
||||||
self.__setBackend = False
|
|
||||||
elif backend == "gamin":
|
|
||||||
try:
|
|
||||||
self.__initGamin()
|
|
||||||
self.__setBackend = True
|
|
||||||
except ImportError:
|
|
||||||
self.__setBackend = False
|
|
||||||
elif backend == "polling":
|
|
||||||
self.__initPoller()
|
|
||||||
self.__setBackend = True
|
|
||||||
|
|
||||||
if not self.__setBackend:
|
def _setBackend(self, backend):
|
||||||
# If auto, or unrecognized, or failed using an explicit value
|
backend = backend.lower() # to assure consistent matching
|
||||||
try:
|
|
||||||
self.__initPyinotify()
|
|
||||||
except ImportError:
|
|
||||||
try:
|
|
||||||
self.__initGamin()
|
|
||||||
except ImportError:
|
|
||||||
self.__initPoller()
|
|
||||||
self.__setBackend = True
|
|
||||||
|
|
||||||
|
backends = self._BACKENDS
|
||||||
|
if backend != 'auto':
|
||||||
|
# we have got strict specification of the backend to use
|
||||||
|
if not (backend in self._BACKENDS):
|
||||||
|
raise ValueError("Unknown backend %s. Must be among %s or 'auto'"
|
||||||
|
% (backend, backends))
|
||||||
|
# so explore starting from it till the 'end'
|
||||||
|
backends = backends[backends.index(backend):]
|
||||||
|
|
||||||
|
for b in backends:
|
||||||
|
initmethod = getattr(self, '_init%s' % b.capitalize())
|
||||||
|
try:
|
||||||
|
initmethod()
|
||||||
|
if backend != 'auto' and b != backend:
|
||||||
|
logSys.warning("Could only initiated %r backend whenever "
|
||||||
|
"%r was requested" % (b, backend))
|
||||||
|
else:
|
||||||
|
logSys.info("Initiated %r backend" % b)
|
||||||
self.__action = Actions(self)
|
self.__action = Actions(self)
|
||||||
|
return # we are done
|
||||||
|
except ImportError, e:
|
||||||
|
logSys.debug(
|
||||||
|
"Backend %r failed to initialize due to %s" % (b, e))
|
||||||
|
raise RuntimeError(
|
||||||
|
"We should have initialized at least 'polling' backend")
|
||||||
|
|
||||||
def __initPoller(self):
|
|
||||||
|
def _initPoller(self):
|
||||||
logSys.info("Jail '%s' uses poller" % self.__name)
|
logSys.info("Jail '%s' uses poller" % self.__name)
|
||||||
from filterpoll import FilterPoll
|
from filterpoll import FilterPoll
|
||||||
self.__filter = FilterPoll(self)
|
self.__filter = FilterPoll(self)
|
||||||
|
|
||||||
def __initGamin(self):
|
def _initGamin(self):
|
||||||
# Try to import gamin
|
# Try to import gamin
|
||||||
import gamin
|
import gamin
|
||||||
logSys.info("Jail '%s' uses Gamin" % self.__name)
|
logSys.info("Jail '%s' uses Gamin" % self.__name)
|
||||||
from filtergamin import FilterGamin
|
from filtergamin import FilterGamin
|
||||||
self.__filter = FilterGamin(self)
|
self.__filter = FilterGamin(self)
|
||||||
|
|
||||||
def __initPyinotify(self):
|
def _initPyinotify(self):
|
||||||
# Try to import pyinotify
|
# Try to import pyinotify
|
||||||
import pyinotify
|
import pyinotify
|
||||||
logSys.info("Jail '%s' uses pyinotify" % self.__name)
|
logSys.info("Jail '%s' uses pyinotify" % self.__name)
|
||||||
|
|
Loading…
Reference in New Issue