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
|
|
|
|
#
|
2008-07-21 14:13:13 +00:00
|
|
|
# $Revision: 682 $
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2008-07-21 14:13:13 +00:00
|
|
|
__version__ = "$Revision: 682 $"
|
|
|
|
__date__ = "$Date: 2008-04-08 00:25:16 +0200 (Tue, 08 Apr 2008) $"
|
2006-06-26 20:05:00 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2006-09-25 17:03:48 +00:00
|
|
|
import logging, os
|
2006-10-23 20:37:11 +00:00
|
|
|
#from subprocess import call
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
2006-07-08 16:51:14 +00:00
|
|
|
logSys = logging.getLogger("fail2ban.actions.action")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# 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.
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
class Action:
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def __init__(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__name = name
|
|
|
|
self.__cInfo = dict()
|
2006-06-26 20:05:00 +00:00
|
|
|
## Command executed in order to initialize the system.
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actionStart = ''
|
2006-06-26 20:05:00 +00:00
|
|
|
## Command executed when an IP address gets banned.
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actionBan = ''
|
2006-06-26 20:05:00 +00:00
|
|
|
## Command executed when an IP address gets removed.
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actionUnban = ''
|
2006-06-26 20:05:00 +00:00
|
|
|
## Command executed in order to check requirements.
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actionCheck = ''
|
2006-06-26 20:05:00 +00:00
|
|
|
## Command executed in order to stop the system.
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actionStop = ''
|
2006-06-26 20:05:00 +00:00
|
|
|
logSys.debug("Created Action")
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Sets the action name.
|
|
|
|
#
|
|
|
|
# @param name the name of the action
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setName(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__name = name
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Returns the action name.
|
|
|
|
#
|
|
|
|
# @return the name of the action
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getName(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__name
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Sets a "CInfo".
|
|
|
|
#
|
|
|
|
# CInfo are statically defined properties. They can be definied by
|
|
|
|
# the user and are used to set e-mail addresses, port, host or
|
|
|
|
# anything that should not change during the life of the server.
|
|
|
|
#
|
|
|
|
# @param key the property name
|
|
|
|
# @param value the property value
|
|
|
|
|
2006-07-16 21:35:08 +00:00
|
|
|
def setCInfo(self, key, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__cInfo[key] = value
|
2006-07-16 21:35:08 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Returns a "CInfo".
|
|
|
|
#
|
|
|
|
# @param key the property name
|
|
|
|
|
2006-07-16 21:35:08 +00:00
|
|
|
def getCInfo(self, key):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__cInfo[key]
|
2006-07-16 21:35:08 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Removes a "CInfo".
|
|
|
|
#
|
|
|
|
# @param key the property name
|
|
|
|
|
2006-07-16 21:35:08 +00:00
|
|
|
def delCInfo(self, key):
|
2006-09-19 20:38:32 +00:00
|
|
|
del self.__cInfo[key]
|
2006-07-16 21:35:08 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
##
|
|
|
|
# Set the "start" command.
|
|
|
|
#
|
|
|
|
# @param value the command
|
|
|
|
|
|
|
|
def setActionStart(self, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actionStart = value
|
2008-07-21 14:13:13 +00:00
|
|
|
logSys.debug("Set actionStart = %s" % value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Get the "start" command.
|
|
|
|
#
|
|
|
|
# @return the command
|
|
|
|
|
|
|
|
def getActionStart(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__actionStart
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Executes the action "start" command.
|
|
|
|
#
|
|
|
|
# Replaces the tags in the action command with value of "cInfo"
|
|
|
|
# and executes the resulting command.
|
|
|
|
#
|
|
|
|
# @return True if the command succeeded
|
|
|
|
|
2006-07-16 21:35:08 +00:00
|
|
|
def execActionStart(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
startCmd = Action.replaceTag(self.__actionStart, self.__cInfo)
|
2006-07-16 21:35:08 +00:00
|
|
|
return Action.executeCmd(startCmd)
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
##
|
|
|
|
# Set the "ban" command.
|
|
|
|
#
|
|
|
|
# @param value the command
|
|
|
|
|
|
|
|
def setActionBan(self, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actionBan = value
|
2008-07-21 14:13:13 +00:00
|
|
|
logSys.debug("Set actionBan = %s" % value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Get the "ban" command.
|
|
|
|
#
|
|
|
|
# @return the command
|
|
|
|
|
|
|
|
def getActionBan(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__actionBan
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Executes the action "ban" command.
|
|
|
|
#
|
|
|
|
# @return True if the command succeeded
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def execActionBan(self, aInfo):
|
2006-10-24 19:40:51 +00:00
|
|
|
return self.__processCmd(self.__actionBan, aInfo)
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
##
|
|
|
|
# Set the "unban" command.
|
|
|
|
#
|
|
|
|
# @param value the command
|
|
|
|
|
|
|
|
def setActionUnban(self, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actionUnban = value
|
2008-07-21 14:13:13 +00:00
|
|
|
logSys.debug("Set actionUnban = %s" % value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Get the "unban" command.
|
|
|
|
#
|
|
|
|
# @return the command
|
|
|
|
|
|
|
|
def getActionUnban(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__actionUnban
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Executes the action "unban" command.
|
|
|
|
#
|
|
|
|
# @return True if the command succeeded
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def execActionUnban(self, aInfo):
|
2006-10-24 19:40:51 +00:00
|
|
|
return self.__processCmd(self.__actionUnban, aInfo)
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
##
|
|
|
|
# Set the "check" command.
|
|
|
|
#
|
|
|
|
# @param value the command
|
|
|
|
|
|
|
|
def setActionCheck(self, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actionCheck = value
|
2008-07-21 14:13:13 +00:00
|
|
|
logSys.debug("Set actionCheck = %s" % value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Get the "check" command.
|
|
|
|
#
|
|
|
|
# @return the command
|
|
|
|
|
|
|
|
def getActionCheck(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__actionCheck
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Set the "stop" command.
|
|
|
|
#
|
|
|
|
# @param value the command
|
|
|
|
|
|
|
|
def setActionStop(self, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actionStop = value
|
2008-07-21 14:13:13 +00:00
|
|
|
logSys.debug("Set actionStop = %s" % value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Get the "stop" command.
|
|
|
|
#
|
|
|
|
# @return the command
|
|
|
|
|
|
|
|
def getActionStop(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__actionStop
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Executes the action "stop" command.
|
|
|
|
#
|
|
|
|
# Replaces the tags in the action command with value of "cInfo"
|
|
|
|
# and executes the resulting command.
|
|
|
|
#
|
|
|
|
# @return True if the command succeeded
|
|
|
|
|
2006-07-16 21:35:08 +00:00
|
|
|
def execActionStop(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
stopCmd = Action.replaceTag(self.__actionStop, self.__cInfo)
|
2006-07-16 21:35:08 +00:00
|
|
|
return Action.executeCmd(stopCmd)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Replaces tags in query with property values in aInfo.
|
|
|
|
#
|
|
|
|
# @param query the query string with tags
|
|
|
|
# @param aInfo the properties
|
|
|
|
# @return a string
|
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-06-26 20:05:00 +00:00
|
|
|
def replaceTag(query, aInfo):
|
|
|
|
""" Replace tags in query
|
|
|
|
"""
|
|
|
|
string = query
|
|
|
|
for tag in aInfo:
|
|
|
|
string = string.replace('<' + tag + '>', str(aInfo[tag]))
|
|
|
|
# New line
|
|
|
|
string = string.replace("<br>", '\n')
|
|
|
|
return string
|
2008-03-06 01:19:06 +00:00
|
|
|
replaceTag = staticmethod(replaceTag)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Executes a command with preliminary checks and substitutions.
|
|
|
|
#
|
|
|
|
# Before executing any commands, executes the "check" command first
|
|
|
|
# in order to check if prerequirements are met. If this check fails,
|
|
|
|
# it tries to restore a sane environnement before executing the real
|
|
|
|
# command.
|
|
|
|
# Replaces "aInfo" and "cInfo" in the query too.
|
|
|
|
#
|
|
|
|
# @param cmd The command to execute
|
|
|
|
# @param aInfo Dynamic properties
|
|
|
|
# @return True if the command succeeded
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __processCmd(self, cmd, aInfo = None):
|
2006-06-26 20:05:00 +00:00
|
|
|
""" Executes an OS command.
|
|
|
|
"""
|
|
|
|
if cmd == "":
|
|
|
|
logSys.debug("Nothing to do")
|
|
|
|
return True
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
checkCmd = Action.replaceTag(self.__actionCheck, self.__cInfo)
|
2006-07-16 21:35:08 +00:00
|
|
|
if not Action.executeCmd(checkCmd):
|
|
|
|
logSys.error("Invariant check failed. Trying to restore a sane" +
|
|
|
|
" environment")
|
2006-09-19 20:38:32 +00:00
|
|
|
stopCmd = Action.replaceTag(self.__actionStop, self.__cInfo)
|
2006-07-16 21:35:08 +00:00
|
|
|
Action.executeCmd(stopCmd)
|
2006-09-19 20:38:32 +00:00
|
|
|
startCmd = Action.replaceTag(self.__actionStart, self.__cInfo)
|
2006-07-16 21:35:08 +00:00
|
|
|
Action.executeCmd(startCmd)
|
|
|
|
if not Action.executeCmd(checkCmd):
|
|
|
|
logSys.fatal("Unable to restore environment")
|
|
|
|
return False
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
# Replace tags
|
|
|
|
if not aInfo == None:
|
|
|
|
realCmd = Action.replaceTag(cmd, aInfo)
|
|
|
|
else:
|
|
|
|
realCmd = cmd
|
|
|
|
|
2006-07-16 21:35:08 +00:00
|
|
|
# Replace static fields
|
2006-09-19 20:38:32 +00:00
|
|
|
realCmd = Action.replaceTag(realCmd, self.__cInfo)
|
2006-07-16 21:35:08 +00:00
|
|
|
|
2006-10-24 19:40:51 +00:00
|
|
|
return Action.executeCmd(realCmd)
|
2006-07-16 21:35:08 +00:00
|
|
|
|
2007-03-07 20:54:32 +00:00
|
|
|
##
|
|
|
|
# Executes a command.
|
|
|
|
#
|
|
|
|
# We need a shell here because commands are mainly shell script. They
|
|
|
|
# contain pipe, redirection, etc.
|
|
|
|
#
|
|
|
|
# @todo Force the use of bash!?
|
|
|
|
# @todo Kill the command after a given timeout
|
|
|
|
#
|
|
|
|
# @param realCmd the command to execute
|
|
|
|
# @return True if the command succeeded
|
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-07-16 21:35:08 +00:00
|
|
|
def executeCmd(realCmd):
|
2006-07-08 16:51:14 +00:00
|
|
|
logSys.debug(realCmd)
|
2006-09-21 21:20:29 +00:00
|
|
|
try:
|
2006-09-21 22:38:21 +00:00
|
|
|
# The following line gives deadlock with multiple jails
|
2006-10-23 20:37:11 +00:00
|
|
|
#retcode = call(realCmd, shell=True)
|
|
|
|
retcode = os.system(realCmd)
|
2006-10-23 20:12:15 +00:00
|
|
|
if retcode == 0:
|
|
|
|
logSys.debug("%s returned successfully" % realCmd)
|
2006-09-21 21:20:29 +00:00
|
|
|
return True
|
2006-10-23 20:12:15 +00:00
|
|
|
else:
|
|
|
|
logSys.error("%s returned %x" % (realCmd, retcode))
|
2006-09-21 21:20:29 +00:00
|
|
|
except OSError, e:
|
|
|
|
logSys.error("%s failed with %s" % (realCmd, e))
|
|
|
|
return False
|
2008-03-06 01:19:06 +00:00
|
|
|
executeCmd = staticmethod(executeCmd)
|
|
|
|
|