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-07-08 16:51:14 +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-07-08 16:51:14 +00:00
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
|
|
|
from banmanager import BanManager
|
|
|
|
from jailthread import JailThread
|
|
|
|
from action import Action
|
2006-10-18 22:30:57 +00:00
|
|
|
from mytime import MyTime
|
2006-09-25 17:03:48 +00:00
|
|
|
import time, logging
|
2006-07-08 16:51:14 +00:00
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.actions")
|
|
|
|
|
|
|
|
##
|
|
|
|
# Execute commands.
|
|
|
|
#
|
|
|
|
# This class reads the failures from the Jail queue and decide if an
|
|
|
|
# action has to be taken. A BanManager take care of the banned IP
|
|
|
|
# addresses.
|
|
|
|
|
|
|
|
class Actions(JailThread):
|
|
|
|
|
|
|
|
##
|
|
|
|
# Constructor.
|
|
|
|
#
|
|
|
|
# Initialize the filter object with default values.
|
|
|
|
# @param jail the jail object
|
|
|
|
|
|
|
|
def __init__(self, jail):
|
2006-10-24 19:40:51 +00:00
|
|
|
JailThread.__init__(self)
|
2006-07-08 16:51:14 +00:00
|
|
|
## The jail which contains this action.
|
|
|
|
self.jail = jail
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actions = list()
|
2006-07-08 16:51:14 +00:00
|
|
|
## The ban manager.
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__banManager = BanManager()
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Adds an action.
|
|
|
|
#
|
|
|
|
# @param name The action name
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def addAction(self, name):
|
|
|
|
action = Action(name)
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actions.append(action)
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Removes an action.
|
|
|
|
#
|
|
|
|
# @param name The action name
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def delAction(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
for action in self.__actions:
|
2006-07-08 16:51:14 +00:00
|
|
|
if action.getName() == name:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actions.remove(action)
|
2013-03-10 14:55:39 +00:00
|
|
|
return
|
|
|
|
raise KeyError("Invalid Action name: %s" % name)
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Returns an action.
|
|
|
|
#
|
|
|
|
# Raises a KeyError exception if the action does not exist.
|
|
|
|
#
|
|
|
|
# @param name the action name
|
|
|
|
# @return the action
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getAction(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
for action in self.__actions:
|
2006-07-08 16:51:14 +00:00
|
|
|
if action.getName() == name:
|
|
|
|
return action
|
2013-03-10 14:55:39 +00:00
|
|
|
raise KeyError("Invalid Action name")
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Returns the last defined action.
|
|
|
|
#
|
|
|
|
# @return The last defined action.
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getLastAction(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
action = self.__actions.pop()
|
|
|
|
self.__actions.append(action)
|
2006-07-08 16:51:14 +00:00
|
|
|
return action
|
|
|
|
|
|
|
|
##
|
|
|
|
# Set the ban time.
|
|
|
|
#
|
|
|
|
# @param value the time
|
|
|
|
|
|
|
|
def setBanTime(self, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__banManager.setBanTime(value)
|
2006-07-08 16:51:14 +00:00
|
|
|
logSys.info("Set banTime = %s" % value)
|
|
|
|
|
|
|
|
##
|
|
|
|
# Get the ban time.
|
|
|
|
#
|
|
|
|
# @return the time
|
|
|
|
|
|
|
|
def getBanTime(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__banManager.getBanTime()
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2012-11-05 23:07:33 +00:00
|
|
|
##
|
|
|
|
# Remove a banned IP now, rather than waiting for it to expire, even if set to never expire.
|
|
|
|
#
|
|
|
|
# @return the IP string or 'None' if not unbanned.
|
|
|
|
def removeBannedIP(self, ip):
|
|
|
|
# Find the ticket with the IP.
|
|
|
|
ticket = self.__banManager.getTicketByIP(ip)
|
2012-11-06 01:37:06 +00:00
|
|
|
if ticket is not None:
|
2012-11-05 23:07:33 +00:00
|
|
|
# Unban the IP.
|
|
|
|
self.__unBan(ticket)
|
|
|
|
return ip
|
2013-03-10 14:55:39 +00:00
|
|
|
raise ValueError("IP %s is not banned" % ip)
|
2012-11-05 23:07:33 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
##
|
|
|
|
# Main loop.
|
|
|
|
#
|
|
|
|
# This function is the main loop of the thread. It checks the Jail
|
|
|
|
# queue and executes commands when an IP address is banned.
|
|
|
|
# @return True when the thread exits nicely
|
|
|
|
|
|
|
|
def run(self):
|
2007-03-26 21:17:31 +00:00
|
|
|
self.setActive(True)
|
2006-09-19 20:38:32 +00:00
|
|
|
for action in self.__actions:
|
2006-07-16 21:35:08 +00:00
|
|
|
action.execActionStart()
|
2007-03-26 21:17:31 +00:00
|
|
|
while self._isActive():
|
2006-09-19 20:38:32 +00:00
|
|
|
if not self.getIdle():
|
2006-07-08 16:51:14 +00:00
|
|
|
#logSys.debug(self.jail.getName() + ": action")
|
2006-09-19 20:38:32 +00:00
|
|
|
ret = self.__checkBan()
|
2006-07-08 16:51:14 +00:00
|
|
|
if not ret:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__checkUnBan()
|
|
|
|
time.sleep(self.getSleepTime())
|
2006-07-08 16:51:14 +00:00
|
|
|
else:
|
2006-09-19 20:38:32 +00:00
|
|
|
time.sleep(self.getSleepTime())
|
|
|
|
self.__flushBan()
|
|
|
|
for action in self.__actions:
|
2006-07-16 21:35:08 +00:00
|
|
|
action.execActionStop()
|
2006-07-08 16:51:14 +00:00
|
|
|
logSys.debug(self.jail.getName() + ": action terminated")
|
|
|
|
return True
|
|
|
|
|
|
|
|
##
|
|
|
|
# Check for IP address to ban.
|
|
|
|
#
|
|
|
|
# Look in the Jail queue for FailTicket. If a ticket is available,
|
|
|
|
# it executes the "ban" command and add a ticket to the BanManager.
|
|
|
|
# @return True if an IP address get banned
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __checkBan(self):
|
2006-07-08 16:51:14 +00:00
|
|
|
ticket = self.jail.getFailTicket()
|
|
|
|
if ticket != False:
|
|
|
|
aInfo = dict()
|
|
|
|
bTicket = BanManager.createBanTicket(ticket)
|
|
|
|
aInfo["ip"] = bTicket.getIP()
|
2006-08-06 21:25:23 +00:00
|
|
|
aInfo["failures"] = bTicket.getAttempt()
|
2006-11-12 10:56:21 +00:00
|
|
|
aInfo["time"] = bTicket.getTime()
|
2011-11-07 19:06:20 +00:00
|
|
|
aInfo["matches"] = "".join(bTicket.getMatches())
|
2006-11-12 10:56:21 +00:00
|
|
|
if self.__banManager.addBanTicket(bTicket):
|
2012-11-26 17:01:42 +00:00
|
|
|
logSys.warn("[%s] Ban %s" % (self.jail.getName(), aInfo["ip"]))
|
2006-11-12 10:56:21 +00:00
|
|
|
for action in self.__actions:
|
|
|
|
action.execActionBan(aInfo)
|
|
|
|
return True
|
|
|
|
else:
|
2012-11-06 02:30:07 +00:00
|
|
|
logSys.info("[%s] %s already banned" % (self.jail.getName(),
|
2012-11-26 17:01:42 +00:00
|
|
|
aInfo["ip"]))
|
2006-07-08 16:51:14 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
##
|
|
|
|
# Check for IP address to unban.
|
|
|
|
#
|
|
|
|
# Unban IP address which are outdated.
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __checkUnBan(self):
|
2006-10-18 22:30:57 +00:00
|
|
|
for ticket in self.__banManager.unBanList(MyTime.time()):
|
2006-11-12 10:56:21 +00:00
|
|
|
self.__unBan(ticket)
|
2006-07-08 16:51:14 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Flush the ban list.
|
|
|
|
#
|
|
|
|
# Unban all IP address which are still in the banning list.
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __flushBan(self):
|
2006-07-08 16:51:14 +00:00
|
|
|
logSys.debug("Flush ban list")
|
2006-09-19 20:38:32 +00:00
|
|
|
for ticket in self.__banManager.flushBanList():
|
2006-11-12 10:56:21 +00:00
|
|
|
self.__unBan(ticket)
|
|
|
|
|
|
|
|
##
|
|
|
|
# Unbans host corresponding to the ticket.
|
|
|
|
#
|
|
|
|
# Executes the actions in order to unban the host given in the
|
|
|
|
# ticket.
|
|
|
|
|
|
|
|
def __unBan(self, ticket):
|
|
|
|
aInfo = dict()
|
|
|
|
aInfo["ip"] = ticket.getIP()
|
|
|
|
aInfo["failures"] = ticket.getAttempt()
|
|
|
|
aInfo["time"] = ticket.getTime()
|
2011-11-07 19:06:20 +00:00
|
|
|
aInfo["matches"] = "".join(ticket.getMatches())
|
2006-11-12 10:56:21 +00:00
|
|
|
logSys.warn("[%s] Unban %s" % (self.jail.getName(), aInfo["ip"]))
|
|
|
|
for action in self.__actions:
|
|
|
|
action.execActionUnban(aInfo)
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Get the status of the filter.
|
|
|
|
#
|
|
|
|
# Get some informations about the filter state such as the total
|
|
|
|
# number of failures.
|
|
|
|
# @return a list with tuple
|
|
|
|
|
|
|
|
def status(self):
|
2006-11-12 10:56:21 +00:00
|
|
|
ret = [("Currently banned", self.__banManager.size()),
|
2007-01-29 21:46:59 +00:00
|
|
|
("Total banned", self.__banManager.getBanTotal()),
|
|
|
|
("IP list", self.__banManager.getBanList())]
|
2006-07-08 16:51:14 +00:00
|
|
|
return ret
|