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
|
|
|
|
2013-05-03 03:25:43 +00:00
|
|
|
__author__ = "Cyril Jaquier and Fail2Ban Contributors"
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2011-2012 Yaroslav Halchenko"
|
2006-06-26 20:05:00 +00:00
|
|
|
__license__ = "GPL"
|
|
|
|
|
2006-09-25 17:03:48 +00:00
|
|
|
import logging, os
|
2013-04-29 02:37:16 +00:00
|
|
|
import threading, re
|
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
|
|
|
|
2011-09-24 02:28:45 +00:00
|
|
|
# Create a lock for running system commands
|
|
|
|
_cmd_lock = threading.Lock()
|
|
|
|
|
2012-11-07 16:33:17 +00:00
|
|
|
# Some hints on common abnormal exit codes
|
|
|
|
_RETCODE_HINTS = {
|
|
|
|
0x7f00: '"Command not found". Make sure that all commands in %(realCmd)r '
|
|
|
|
'are in the PATH of fail2ban-server process '
|
|
|
|
'(grep -a PATH= /proc/`pidof -x fail2ban-server`/environ). '
|
|
|
|
'You may want to start '
|
|
|
|
'"fail2ban-server -f" separately, initiate it with '
|
|
|
|
'"fail2ban-client reload" in another shell session and observe if '
|
|
|
|
'additional informative error messages appear in the terminals.'
|
|
|
|
}
|
|
|
|
|
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-04-07 22:25:16 +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):
|
2013-04-29 02:37:16 +00:00
|
|
|
if self.__cInfo:
|
|
|
|
if not Action.substituteRecursiveTags(self.__cInfo):
|
2013-04-29 22:14:50 +00:00
|
|
|
logSys.error("Cinfo/definitions contain self referencing definitions and cannot be resolved")
|
2013-04-29 02:37:16 +00:00
|
|
|
return False
|
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-04-07 22:25:16 +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-04-07 22:25:16 +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-04-07 22:25:16 +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-04-07 22:25:16 +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)
|
2012-10-09 02:14:51 +00:00
|
|
|
|
2013-04-29 02:37:16 +00:00
|
|
|
##
|
2013-04-29 22:07:21 +00:00
|
|
|
# Sort out tag definitions within other tags
|
2013-04-29 02:37:16 +00:00
|
|
|
#
|
|
|
|
# so: becomes:
|
|
|
|
# a = 3 a = 3
|
|
|
|
# b = <a>_3 b = 3_3
|
|
|
|
# @param tags, a dictionary
|
2013-04-29 22:07:21 +00:00
|
|
|
# @returns tags altered or False if there is a recursive definition
|
2013-04-29 02:37:16 +00:00
|
|
|
#@staticmethod
|
|
|
|
def substituteRecursiveTags(tags):
|
|
|
|
t = re.compile(r'<([^ >]+)>')
|
|
|
|
for tag, value in tags.iteritems():
|
|
|
|
value = str(value)
|
|
|
|
m = t.search(value)
|
|
|
|
while m:
|
|
|
|
if m.group(1) == tag:
|
2013-04-29 22:07:21 +00:00
|
|
|
# recursive definitions are bad
|
2013-04-29 02:37:16 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if tags.has_key(m.group(1)):
|
|
|
|
value = value[0:m.start()] + tags[m.group(1)] + value[m.end():]
|
|
|
|
m = t.search(value, m.start())
|
|
|
|
else:
|
2013-04-29 22:14:50 +00:00
|
|
|
# Missing tags are ok so we just continue on searching.
|
|
|
|
# cInfo can contain aInfo elements like <HOST> and valid shell
|
|
|
|
# constructs like <STDIN>.
|
2013-04-29 02:37:16 +00:00
|
|
|
m = t.search(value, m.start() + 1)
|
|
|
|
tags[tag] = value
|
|
|
|
return tags
|
|
|
|
substituteRecursiveTags = staticmethod(substituteRecursiveTags)
|
|
|
|
|
|
|
|
#@staticmethod
|
2012-10-09 02:14:51 +00:00
|
|
|
def escapeTag(tag):
|
2013-01-25 12:37:22 +00:00
|
|
|
for c in '\\#&;`|*?~<>^()[]{}$\n\'"':
|
2012-10-09 02:14:51 +00:00
|
|
|
if c in tag:
|
|
|
|
tag = tag.replace(c, '\\' + c)
|
|
|
|
return tag
|
|
|
|
escapeTag = staticmethod(escapeTag)
|
|
|
|
|
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
|
|
|
|
|
2007-12-16 21:38:04 +00:00
|
|
|
#@staticmethod
|
2006-06-26 20:05:00 +00:00
|
|
|
def replaceTag(query, aInfo):
|
|
|
|
""" Replace tags in query
|
|
|
|
"""
|
|
|
|
string = query
|
2012-10-09 02:14:51 +00:00
|
|
|
for tag, value in aInfo.iteritems():
|
|
|
|
value = str(value) # assure string
|
|
|
|
if tag == 'matches':
|
|
|
|
# That one needs to be escaped since its content is
|
|
|
|
# out of our control
|
2012-11-05 21:54:19 +00:00
|
|
|
value = Action.escapeTag(value)
|
2012-10-09 02:14:51 +00:00
|
|
|
string = string.replace('<' + tag + '>', value)
|
2006-06-26 20:05:00 +00:00
|
|
|
# New line
|
|
|
|
string = string.replace("<br>", '\n')
|
|
|
|
return string
|
2007-12-16 21:38:04 +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
|
2013-03-10 22:05:33 +00:00
|
|
|
# in order to check if pre-requirements are met. If this check fails,
|
|
|
|
# it tries to restore a sane environment before executing the real
|
2007-03-07 20:54:32 +00:00
|
|
|
# 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")
|
2013-04-14 14:57:37 +00:00
|
|
|
self.execActionStop()
|
|
|
|
self.execActionStart()
|
2006-07-16 21:35:08 +00:00
|
|
|
if not Action.executeCmd(checkCmd):
|
|
|
|
logSys.fatal("Unable to restore environment")
|
|
|
|
return False
|
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
# Replace tags
|
2013-05-03 03:25:43 +00:00
|
|
|
if not aInfo is None:
|
2006-07-08 16:51:14 +00:00
|
|
|
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
|
|
|
|
|
2007-12-16 21:38:04 +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)
|
2013-09-29 03:14:52 +00:00
|
|
|
if realCmd is None or realCmd == "":
|
|
|
|
logSys.debug("Nothing to do")
|
|
|
|
return True
|
|
|
|
|
2011-09-24 02:28:45 +00:00
|
|
|
_cmd_lock.acquire()
|
|
|
|
try: # Try wrapped within another try needed for python version < 2.5
|
|
|
|
try:
|
|
|
|
# The following line gives deadlock with multiple jails
|
|
|
|
#retcode = call(realCmd, shell=True)
|
|
|
|
retcode = os.system(realCmd)
|
|
|
|
if retcode == 0:
|
|
|
|
logSys.debug("%s returned successfully" % realCmd)
|
|
|
|
return True
|
|
|
|
else:
|
2012-11-07 16:33:17 +00:00
|
|
|
msg = _RETCODE_HINTS.get(retcode, None)
|
|
|
|
logSys.error("%s returned %x" % (realCmd, retcode))
|
|
|
|
if msg:
|
|
|
|
logSys.info("HINT on %x: %s"
|
|
|
|
% (retcode, msg % locals()))
|
2011-09-24 02:28:45 +00:00
|
|
|
except OSError, e:
|
|
|
|
logSys.error("%s failed with %s" % (realCmd, e))
|
|
|
|
finally:
|
|
|
|
_cmd_lock.release()
|
2006-09-21 21:20:29 +00:00
|
|
|
return False
|
2007-12-16 21:38:04 +00:00
|
|
|
executeCmd = staticmethod(executeCmd)
|
|
|
|
|