mirror of
https://github.com/fail2ban/fail2ban.git
synced 2026-08-27 03:46:05 +00:00
- One step forward to 0.7.0
git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@250 a942ae1a-1317-0410-a47c-b1dcaea8d605
This commit is contained in:
parent
ea1948eff4
commit
12c222bd1c
21 changed files with 550 additions and 210 deletions
158
server/action.py
158
server/action.py
|
|
@ -24,13 +24,10 @@ __date__ = "$Date: 2004/10/10 13:33:40 $"
|
|||
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
||||
__license__ = "GPL"
|
||||
|
||||
from banmanager import BanManager
|
||||
from failmanager import FailManager, FailManagerEmpty
|
||||
from jailthread import JailThread
|
||||
import time, logging, os
|
||||
|
||||
# Gets the instance of the logger.
|
||||
logSys = logging.getLogger("fail2ban.action")
|
||||
logSys = logging.getLogger("fail2ban.actions.action")
|
||||
|
||||
##
|
||||
# Execute commands.
|
||||
|
|
@ -39,20 +36,10 @@ logSys = logging.getLogger("fail2ban.action")
|
|||
# action has to be taken. A BanManager take care of the banned IP
|
||||
# addresses.
|
||||
|
||||
class Action(JailThread):
|
||||
class Action:
|
||||
|
||||
##
|
||||
# Constructor.
|
||||
#
|
||||
# Initialize the filter object with default values.
|
||||
# @param jail the jail object
|
||||
|
||||
def __init__(self, jail):
|
||||
JailThread.__init__(self, jail)
|
||||
## The jail which contains this action.
|
||||
self.jail = jail
|
||||
## The ban manager.
|
||||
self.banManager = BanManager()
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
## Command executed in order to initialize the system.
|
||||
self.actionStart = ''
|
||||
## Command executed when an IP address gets banned.
|
||||
|
|
@ -64,7 +51,13 @@ class Action(JailThread):
|
|||
## Command executed in order to stop the system.
|
||||
self.actionStop = ''
|
||||
logSys.debug("Created Action")
|
||||
|
||||
|
||||
def setName(self, name):
|
||||
self.name = name
|
||||
|
||||
def getName(self):
|
||||
return self.name
|
||||
|
||||
##
|
||||
# Set the "start" command.
|
||||
#
|
||||
|
|
@ -82,6 +75,9 @@ class Action(JailThread):
|
|||
def getActionStart(self):
|
||||
return self.actionStart
|
||||
|
||||
def execActionStart(self, aInfo):
|
||||
return self.executeCmd(self.actionStart, aInfo);
|
||||
|
||||
##
|
||||
# Set the "ban" command.
|
||||
#
|
||||
|
|
@ -99,6 +95,9 @@ class Action(JailThread):
|
|||
def getActionBan(self):
|
||||
return self.actionBan
|
||||
|
||||
def execActionBan(self, aInfo):
|
||||
return self.executeCmd(self.actionBan, aInfo);
|
||||
|
||||
##
|
||||
# Set the "unban" command.
|
||||
#
|
||||
|
|
@ -116,6 +115,9 @@ class Action(JailThread):
|
|||
def getActionUnban(self):
|
||||
return self.actionUnban
|
||||
|
||||
def execActionUnban(self, aInfo):
|
||||
return self.executeCmd(self.actionUnban, aInfo);
|
||||
|
||||
##
|
||||
# Set the "check" command.
|
||||
#
|
||||
|
|
@ -133,6 +135,9 @@ class Action(JailThread):
|
|||
def getActionCheck(self):
|
||||
return self.actionCheck
|
||||
|
||||
def execActionCheck(self, aInfo):
|
||||
return self.executeCmd(self.actionCheck, aInfo);
|
||||
|
||||
##
|
||||
# Set the "stop" command.
|
||||
#
|
||||
|
|
@ -150,104 +155,8 @@ class Action(JailThread):
|
|||
def getActionStop(self):
|
||||
return self.actionStop
|
||||
|
||||
##
|
||||
# Set the ban time.
|
||||
#
|
||||
# @param value the time
|
||||
|
||||
def setBanTime(self, value):
|
||||
self.banManager.setBanTime(value)
|
||||
logSys.info("Set banTime = %s" % value)
|
||||
|
||||
##
|
||||
# Get the ban time.
|
||||
#
|
||||
# @return the time
|
||||
|
||||
def getBanTime(self):
|
||||
return self.banManager.getBanTime()
|
||||
|
||||
##
|
||||
# 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):
|
||||
self.executeCmd(self.actionStart)
|
||||
self.setActive(True)
|
||||
while self.isActive():
|
||||
if not self.isIdle:
|
||||
#logSys.debug(self.jail.getName() + ": action")
|
||||
ret = self.checkBan()
|
||||
if not ret:
|
||||
self.checkUnBan()
|
||||
time.sleep(self.sleepTime)
|
||||
else:
|
||||
time.sleep(self.sleepTime)
|
||||
self.flushBan()
|
||||
self.executeCmd(self.actionStop)
|
||||
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
|
||||
|
||||
def checkBan(self):
|
||||
logSys.debug("Check for IP address to ban")
|
||||
ticket = self.jail.getFailTicket()
|
||||
if ticket != False:
|
||||
aInfo = dict()
|
||||
bTicket = BanManager.createBanTicket(ticket)
|
||||
aInfo["ip"] = bTicket.getIP()
|
||||
logSys.info("Ban %s" % aInfo["ip"])
|
||||
self.executeCmd(self.replaceTag(self.actionBan, aInfo))
|
||||
self.banManager.addBanTicket(bTicket)
|
||||
return True
|
||||
return False
|
||||
|
||||
##
|
||||
# Check for IP address to unban.
|
||||
#
|
||||
# Unban IP address which are outdated.
|
||||
|
||||
def checkUnBan(self):
|
||||
logSys.debug("Check for IP address to unban")
|
||||
for ticket in self.banManager.unBanList(time.time()):
|
||||
aInfo = dict()
|
||||
aInfo["ip"] = ticket.getIP()
|
||||
logSys.info("Unban %s" % aInfo["ip"])
|
||||
self.executeCmd(self.replaceTag(self.actionUnban, aInfo))
|
||||
|
||||
##
|
||||
# Flush the ban list.
|
||||
#
|
||||
# Unban all IP address which are still in the banning list.
|
||||
|
||||
def flushBan(self):
|
||||
logSys.debug("Flush ban list")
|
||||
for ticket in self.banManager.flushBanList():
|
||||
aInfo = dict()
|
||||
aInfo["ip"] = ticket.getIP()
|
||||
logSys.info("Unban %s" % aInfo["ip"])
|
||||
self.executeCmd(self.replaceTag(self.actionUnban, aInfo))
|
||||
|
||||
##
|
||||
# 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):
|
||||
ret = [("Currently banned", self.banManager.size()),
|
||||
("Total banned", self.banManager.getBanTotal())]
|
||||
return ret
|
||||
def execActionStop(self, aInfo):
|
||||
return self.executeCmd(self.actionStop, aInfo);
|
||||
|
||||
@staticmethod
|
||||
def replaceTag(query, aInfo):
|
||||
|
|
@ -261,21 +170,26 @@ class Action(JailThread):
|
|||
return string
|
||||
|
||||
@staticmethod
|
||||
def executeCmd(cmd):
|
||||
def executeCmd(cmd, aInfo = None):
|
||||
""" Executes an OS command.
|
||||
"""
|
||||
if cmd == "":
|
||||
logSys.debug("Nothing to do")
|
||||
return True
|
||||
|
||||
logSys.debug(cmd)
|
||||
retval = os.system(cmd)
|
||||
# Replace tags
|
||||
if not aInfo == None:
|
||||
realCmd = Action.replaceTag(cmd, aInfo)
|
||||
else:
|
||||
realCmd = cmd
|
||||
|
||||
logSys.debug(realCmd)
|
||||
retval = os.system(realCmd)
|
||||
#if not retval == 0:
|
||||
# logSys.error("'" + cmd + "' returned " + `retval`)
|
||||
# raise Exception("Execution of command '%s' failed" % cmd)
|
||||
if retval == 0:
|
||||
return True
|
||||
else:
|
||||
logSys.error("%s returned %x" % (cmd, retval))
|
||||
return False
|
||||
|
||||
logSys.error("%s returned %x" % (realCmd, retval))
|
||||
return False
|
||||
183
server/actions.py
Normal file
183
server/actions.py
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
# 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
|
||||
#
|
||||
# $Revision: 1.1 $
|
||||
|
||||
__author__ = "Cyril Jaquier"
|
||||
__version__ = "$Revision: 1.1 $"
|
||||
__date__ = "$Date: 2004/10/10 13:33:40 $"
|
||||
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
||||
__license__ = "GPL"
|
||||
|
||||
from banmanager import BanManager
|
||||
from failmanager import FailManager, FailManagerEmpty
|
||||
from jailthread import JailThread
|
||||
from action import Action
|
||||
import time, logging, os
|
||||
|
||||
# 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):
|
||||
JailThread.__init__(self, jail)
|
||||
## The jail which contains this action.
|
||||
self.jail = jail
|
||||
self.actions = list()
|
||||
## The ban manager.
|
||||
self.banManager = BanManager()
|
||||
|
||||
def addAction(self, name):
|
||||
action = Action(name)
|
||||
self.actions.append(action)
|
||||
|
||||
def delAction(self, name):
|
||||
for action in self.actions:
|
||||
if action.getName() == name:
|
||||
self.actions.remove(action)
|
||||
break
|
||||
|
||||
def getAction(self, name):
|
||||
for action in self.actions:
|
||||
if action.getName() == name:
|
||||
return action
|
||||
raise KeyError
|
||||
|
||||
def getLastAction(self):
|
||||
action = self.actions.pop()
|
||||
self.actions.append(action)
|
||||
return action
|
||||
|
||||
##
|
||||
# Set the ban time.
|
||||
#
|
||||
# @param value the time
|
||||
|
||||
def setBanTime(self, value):
|
||||
self.banManager.setBanTime(value)
|
||||
logSys.info("Set banTime = %s" % value)
|
||||
|
||||
##
|
||||
# Get the ban time.
|
||||
#
|
||||
# @return the time
|
||||
|
||||
def getBanTime(self):
|
||||
return self.banManager.getBanTime()
|
||||
|
||||
##
|
||||
# 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):
|
||||
for action in self.actions:
|
||||
action.execActionStart(None)
|
||||
self.setActive(True)
|
||||
while self.isActive():
|
||||
if not self.isIdle:
|
||||
#logSys.debug(self.jail.getName() + ": action")
|
||||
ret = self.checkBan()
|
||||
if not ret:
|
||||
self.checkUnBan()
|
||||
time.sleep(self.sleepTime)
|
||||
else:
|
||||
time.sleep(self.sleepTime)
|
||||
self.flushBan()
|
||||
for action in self.actions:
|
||||
action.execActionStop(None)
|
||||
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
|
||||
|
||||
def checkBan(self):
|
||||
logSys.debug("Check for IP address to ban")
|
||||
ticket = self.jail.getFailTicket()
|
||||
if ticket != False:
|
||||
aInfo = dict()
|
||||
bTicket = BanManager.createBanTicket(ticket)
|
||||
aInfo["ip"] = bTicket.getIP()
|
||||
logSys.info("Ban %s" % aInfo["ip"])
|
||||
for action in self.actions:
|
||||
action.execActionBan(aInfo)
|
||||
self.banManager.addBanTicket(bTicket)
|
||||
return True
|
||||
return False
|
||||
|
||||
##
|
||||
# Check for IP address to unban.
|
||||
#
|
||||
# Unban IP address which are outdated.
|
||||
|
||||
def checkUnBan(self):
|
||||
logSys.debug("Check for IP address to unban")
|
||||
for ticket in self.banManager.unBanList(time.time()):
|
||||
aInfo = dict()
|
||||
aInfo["ip"] = ticket.getIP()
|
||||
logSys.info("Unban %s" % aInfo["ip"])
|
||||
for action in self.actions:
|
||||
action.execActionUnban(aInfo)
|
||||
|
||||
##
|
||||
# Flush the ban list.
|
||||
#
|
||||
# Unban all IP address which are still in the banning list.
|
||||
|
||||
def flushBan(self):
|
||||
logSys.debug("Flush ban list")
|
||||
for ticket in self.banManager.flushBanList():
|
||||
aInfo = dict()
|
||||
aInfo["ip"] = ticket.getIP()
|
||||
logSys.info("Unban %s" % aInfo["ip"])
|
||||
for action in self.actions:
|
||||
action.execActionUnban(aInfo)
|
||||
|
||||
##
|
||||
# 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):
|
||||
ret = [("Currently banned", self.banManager.size()),
|
||||
("Total banned", self.banManager.getBanTotal())]
|
||||
return ret
|
||||
|
||||
|
|
@ -24,7 +24,7 @@ __date__ = "$Date: 2004/10/10 13:33:40 $"
|
|||
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
||||
__license__ = "GPL"
|
||||
|
||||
from action import Action
|
||||
from actions import Actions
|
||||
from filter import Filter
|
||||
import Queue
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ class Jail:
|
|||
self.name = name
|
||||
self.queue = Queue.Queue()
|
||||
self.filter = Filter(self)
|
||||
self.action = Action(self)
|
||||
self.action = Actions(self)
|
||||
|
||||
def setName(self, name):
|
||||
self.name = name
|
||||
|
|
|
|||
|
|
@ -162,7 +162,25 @@ class Server:
|
|||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
# Action
|
||||
# Action
|
||||
def addAction(self, name, value):
|
||||
if self.jails.has_key(name):
|
||||
self.jails[name].getAction().addAction(value)
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def getLastAction(self, name):
|
||||
if self.jails.has_key(name):
|
||||
return self.jails[name].getAction().getLastAction()
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def delAction(self, name, value):
|
||||
if self.jails.has_key(name):
|
||||
self.jails[name].getAction().delAction(value)
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def setBanTime(self, name, value):
|
||||
if self.jails.has_key(name):
|
||||
self.jails[name].getAction().setBanTime(value)
|
||||
|
|
@ -175,63 +193,63 @@ class Server:
|
|||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def setActionStart(self, name, value):
|
||||
def setActionStart(self, name, action, value):
|
||||
if self.jails.has_key(name):
|
||||
self.jails[name].getAction().setActionStart(value)
|
||||
self.jails[name].getAction().getAction(action).setActionStart(value)
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def getActionStart(self, name):
|
||||
def getActionStart(self, name, action):
|
||||
if self.jails.has_key(name):
|
||||
return self.jails[name].getAction().getActionStart()
|
||||
return self.jails[name].getAction().getAction(action).getActionStart()
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def setActionStop(self, name, value):
|
||||
def setActionStop(self, name, action, value):
|
||||
if self.jails.has_key(name):
|
||||
self.jails[name].getAction().setActionStop(value)
|
||||
self.jails[name].getAction().getAction(action).setActionStop(value)
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def getActionStop(self, name):
|
||||
def getActionStop(self, name, action):
|
||||
if self.jails.has_key(name):
|
||||
return self.jails[name].getAction().getActionStop()
|
||||
return self.jails[name].getAction().getAction(action).getActionStop()
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def setActionCheck(self, name, value):
|
||||
def setActionCheck(self, name, action, value):
|
||||
if self.jails.has_key(name):
|
||||
self.jails[name].getAction().setActionCheck(value)
|
||||
self.jails[name].getAction().getAction(action).setActionCheck(value)
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def getActionCheck(self, name):
|
||||
def getActionCheck(self, name, action):
|
||||
if self.jails.has_key(name):
|
||||
return self.jails[name].getAction().getActionCheck()
|
||||
return self.jails[name].getAction().getAction(action).getActionCheck()
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def setActionBan(self, name, value):
|
||||
def setActionBan(self, name, action, value):
|
||||
if self.jails.has_key(name):
|
||||
self.jails[name].getAction().setActionBan(value)
|
||||
self.jails[name].getAction().getAction(action).setActionBan(value)
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def getActionBan(self, name):
|
||||
def getActionBan(self, name, action):
|
||||
if self.jails.has_key(name):
|
||||
return self.jails[name].getAction().getActionBan()
|
||||
return self.jails[name].getAction().getAction(action).getActionBan()
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def setActionUnban(self, name, value):
|
||||
def setActionUnban(self, name, action, value):
|
||||
if self.jails.has_key(name):
|
||||
self.jails[name].getAction().setActionUnban(value)
|
||||
self.jails[name].getAction().getAction(action).setActionUnban(value)
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
def getActionUnban(self, name):
|
||||
def getActionUnban(self, name, action):
|
||||
if self.jails.has_key(name):
|
||||
return self.jails[name].getAction().getActionUnban()
|
||||
return self.jails[name].getAction().getAction(action).getActionUnban()
|
||||
else:
|
||||
raise ServerUnknownJail(name)
|
||||
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ class Transmitter:
|
|||
self.server.setLogLevel(value)
|
||||
return self.server.getLogLevel()
|
||||
# Jail
|
||||
if action[1] == "idle":
|
||||
elif action[1] == "idle":
|
||||
if action[2] == "on":
|
||||
self.server.setIdleJail(name, True)
|
||||
elif action[2] == "off":
|
||||
|
|
@ -140,26 +140,38 @@ class Transmitter:
|
|||
value = action[2]
|
||||
self.server.setBanTime(name, int(value))
|
||||
return self.server.getBanTime(name)
|
||||
elif action[1] == "addaction":
|
||||
value = action[2]
|
||||
self.server.addAction(name, value)
|
||||
return self.server.getLastAction(name).getName()
|
||||
elif action[1] == "delaction":
|
||||
self.server.delAction(name, value)
|
||||
return None
|
||||
elif action[1] == "actionstart":
|
||||
value = action[2]
|
||||
self.server.setActionStart(name, value)
|
||||
return self.server.getActionStart(name)
|
||||
act = action[2]
|
||||
value = action[3]
|
||||
self.server.setActionStart(name, act, value)
|
||||
return self.server.getActionStart(name, act)
|
||||
elif action[1] == "actionstop":
|
||||
value = action[2]
|
||||
self.server.setActionStop(name, value)
|
||||
return self.server.getActionStop(name)
|
||||
act = action[2]
|
||||
value = action[3]
|
||||
self.server.setActionStop(name, act, value)
|
||||
return self.server.getActionStop(name, act)
|
||||
elif action[1] == "actioncheck":
|
||||
value = action[2]
|
||||
self.server.setActionCheck(name, value)
|
||||
return self.server.getActionCheck(name)
|
||||
act = action[2]
|
||||
value = action[3]
|
||||
self.server.setActionCheck(name, act, value)
|
||||
return self.server.getActionCheck(name, act)
|
||||
elif action[1] == "actionban":
|
||||
value = action[2]
|
||||
self.server.setActionBan(name, value)
|
||||
return self.server.getActionBan(name)
|
||||
act = action[2]
|
||||
value = action[3]
|
||||
self.server.setActionBan(name, act, value)
|
||||
return self.server.getActionBan(name, act)
|
||||
elif action[1] == "actionunban":
|
||||
value = action[2]
|
||||
self.server.setActionUnban(name, value)
|
||||
return self.server.getActionUnban(name)
|
||||
act = action[2]
|
||||
value = action[3]
|
||||
self.server.setActionUnban(name, act, value)
|
||||
return self.server.getActionUnban(name, act)
|
||||
raise Exception("Invalid command (no set action)")
|
||||
|
||||
def actionGet(self, action):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue