2011-10-07 19:47:50 +00:00
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 noet :
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
# This file is part of Fail2Ban.
|
|
|
|
#
|
|
|
|
# Fail2Ban is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Fail2Ban is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Fail2Ban; if not, write to the Free Software
|
2011-11-21 12:20:20 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
|
2012-01-10 00:30:49 +00:00
|
|
|
__author__ = "Cyril Jaquier, Lee Clemens, Yaroslav Halchenko"
|
2012-01-09 02:29:43 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2011-2012 Lee Clemens, 2012 Yaroslav Halchenko"
|
2006-06-26 20:05:00 +00:00
|
|
|
__license__ = "GPL"
|
|
|
|
|
2006-09-14 22:05:32 +00:00
|
|
|
import Queue, logging
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
from actions import Actions
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-09-14 22:05:32 +00:00
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.jail")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
class Jail:
|
2012-01-09 02:29:43 +00:00
|
|
|
|
2012-01-10 00:30:49 +00:00
|
|
|
#Known backends. Each backend should have corresponding __initBackend method
|
2012-11-01 19:34:20 +00:00
|
|
|
# yoh: stored in a list instead of a tuple since only
|
|
|
|
# list had .index until 2.6
|
|
|
|
_BACKENDS = ['pyinotify', 'gamin', 'polling']
|
2012-01-09 02:29:43 +00:00
|
|
|
|
2006-10-16 19:42:50 +00:00
|
|
|
def __init__(self, name, backend = "auto"):
|
2013-05-13 02:19:17 +00:00
|
|
|
self.setName(name)
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__queue = Queue.Queue()
|
2006-10-16 19:42:50 +00:00
|
|
|
self.__filter = None
|
2008-05-19 21:05:32 +00:00
|
|
|
logSys.info("Creating new jail '%s'" % self.__name)
|
2012-01-09 02:29:43 +00:00
|
|
|
self._setBackend(backend)
|
2011-12-30 05:18:52 +00:00
|
|
|
|
2012-02-11 02:59:26 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return "%s(%r)" % (self.__class__.__name__, self.__name)
|
|
|
|
|
2012-01-09 02:29:43 +00:00
|
|
|
def _setBackend(self, backend):
|
|
|
|
backend = backend.lower() # to assure consistent matching
|
|
|
|
|
|
|
|
backends = self._BACKENDS
|
|
|
|
if backend != 'auto':
|
|
|
|
# we have got strict specification of the backend to use
|
|
|
|
if not (backend in self._BACKENDS):
|
2012-01-12 06:05:51 +00:00
|
|
|
logSys.error("Unknown backend %s. Must be among %s or 'auto'"
|
|
|
|
% (backend, backends))
|
2012-01-09 02:29:43 +00:00
|
|
|
raise ValueError("Unknown backend %s. Must be among %s or 'auto'"
|
2012-01-12 06:05:51 +00:00
|
|
|
% (backend, backends))
|
2012-01-09 02:29:43 +00:00
|
|
|
# so explore starting from it till the 'end'
|
|
|
|
backends = backends[backends.index(backend):]
|
|
|
|
|
|
|
|
for b in backends:
|
|
|
|
initmethod = getattr(self, '_init%s' % b.capitalize())
|
2011-12-30 05:18:52 +00:00
|
|
|
try:
|
2012-01-09 02:29:43 +00:00
|
|
|
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)
|
|
|
|
return # we are done
|
|
|
|
except ImportError, e:
|
|
|
|
logSys.debug(
|
|
|
|
"Backend %r failed to initialize due to %s" % (b, e))
|
2012-01-12 05:29:36 +00:00
|
|
|
# log error since runtime error message isn't printed, INVALID COMMAND
|
|
|
|
logSys.error(
|
2012-01-12 05:32:37 +00:00
|
|
|
"Failed to initialize any backend for Jail %r" % self.__name)
|
2012-01-09 02:29:43 +00:00
|
|
|
raise RuntimeError(
|
2012-01-12 05:33:12 +00:00
|
|
|
"Failed to initialize any backend for Jail %r" % self.__name)
|
2011-12-30 05:18:52 +00:00
|
|
|
|
2012-01-09 02:29:43 +00:00
|
|
|
|
2012-01-11 22:20:07 +00:00
|
|
|
def _initPolling(self):
|
2008-05-19 21:05:32 +00:00
|
|
|
logSys.info("Jail '%s' uses poller" % self.__name)
|
2006-10-16 19:42:50 +00:00
|
|
|
from filterpoll import FilterPoll
|
|
|
|
self.__filter = FilterPoll(self)
|
|
|
|
|
2012-01-09 02:29:43 +00:00
|
|
|
def _initGamin(self):
|
2006-10-24 19:40:51 +00:00
|
|
|
# Try to import gamin
|
2006-10-16 19:42:50 +00:00
|
|
|
import gamin
|
2008-05-19 21:05:32 +00:00
|
|
|
logSys.info("Jail '%s' uses Gamin" % self.__name)
|
2006-10-16 19:42:50 +00:00
|
|
|
from filtergamin import FilterGamin
|
|
|
|
self.__filter = FilterGamin(self)
|
|
|
|
|
2012-01-09 02:29:43 +00:00
|
|
|
def _initPyinotify(self):
|
2011-12-30 05:18:52 +00:00
|
|
|
# Try to import pyinotify
|
|
|
|
import pyinotify
|
|
|
|
logSys.info("Jail '%s' uses pyinotify" % self.__name)
|
|
|
|
from filterpyinotify import FilterPyinotify
|
|
|
|
self.__filter = FilterPyinotify(self)
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def setName(self, name):
|
2013-05-13 02:19:17 +00:00
|
|
|
if len(name) >= 20:
|
|
|
|
logSys.warning("Jail name %r might be too long and some commands "
|
|
|
|
"might not function correctly. Please shorten"
|
|
|
|
% name)
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__name = name
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getName(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
return self.__name
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getFilter(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
return self.__filter
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getAction(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
return self.__action
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def putFailTicket(self, ticket):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__queue.put(ticket)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getFailTicket(self):
|
|
|
|
try:
|
2006-11-06 22:48:46 +00:00
|
|
|
return self.__queue.get(False)
|
|
|
|
except Queue.Empty:
|
|
|
|
return False
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def start(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__filter.start()
|
|
|
|
self.__action.start()
|
2008-05-19 21:05:32 +00:00
|
|
|
logSys.info("Jail '%s' started" % self.__name)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def stop(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__filter.stop()
|
|
|
|
self.__action.stop()
|
|
|
|
self.__filter.join()
|
|
|
|
self.__action.join()
|
2008-05-19 21:05:32 +00:00
|
|
|
logSys.info("Jail '%s' stopped" % self.__name)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2007-03-26 21:17:31 +00:00
|
|
|
def isAlive(self):
|
|
|
|
isAlive0 = self.__filter.isAlive()
|
|
|
|
isAlive1 = self.__action.isAlive()
|
|
|
|
return isAlive0 or isAlive1
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def setIdle(self, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__filter.setIdle(value)
|
|
|
|
self.__action.setIdle(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getIdle(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
return self.__filter.getIdle() or self.__action.getIdle()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getStatus(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
fStatus = self.__filter.status()
|
|
|
|
aStatus = self.__action.status()
|
|
|
|
ret = [("filter", fStatus),
|
|
|
|
("action", aStatus)]
|
|
|
|
return ret
|