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
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
2006-07-16 22:21:58 +00:00
|
|
|
# $Revision$
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2006-07-16 22:21:58 +00:00
|
|
|
__version__ = "$Revision$"
|
|
|
|
__date__ = "$Date$"
|
2006-06-26 20:05:00 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__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:
|
|
|
|
|
2006-10-16 19:42:50 +00:00
|
|
|
def __init__(self, name, backend = "auto"):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__name = name
|
|
|
|
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)
|
2006-10-16 19:42:50 +00:00
|
|
|
if backend == "polling":
|
|
|
|
self.__initPoller()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self.__initGamin()
|
|
|
|
except ImportError:
|
|
|
|
self.__initPoller()
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__action = Actions(self)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-10-16 19:42:50 +00:00
|
|
|
def __initPoller(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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def setName(self, 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
|