From 8e3c1b73e907b82a26239b554ec30c55f1242cc9 Mon Sep 17 00:00:00 2001 From: hazg Date: Mon, 21 Oct 2013 13:00:04 +0400 Subject: [PATCH 1/7] ignorecommand --- client/filterreader.py | 26 +++---- client/jailreader.py | 29 ++++---- common/protocol.py | 72 ++++++++++--------- config/jail.conf | 3 + server/filter.py | 43 ++++++++--- server/server.py | 158 +++++++++++++++++++++-------------------- server/transmitter.py | 30 ++++---- 7 files changed, 202 insertions(+), 159 deletions(-) diff --git a/client/filterreader.py b/client/filterreader.py index f75190f9..0c81f083 100644 --- a/client/filterreader.py +++ b/client/filterreader.py @@ -18,7 +18,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Author: Cyril Jaquier -# +# __author__ = "Cyril Jaquier" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" @@ -31,32 +31,34 @@ from configreader import ConfigReader logSys = logging.getLogger("fail2ban.client.config") class FilterReader(ConfigReader): - + def __init__(self, fileName, name, **kwargs): ConfigReader.__init__(self, **kwargs) self.__file = fileName self.__name = name - + def setFile(self, fileName): self.__file = fileName - + def getFile(self): return self.__file - + def setName(self, name): self.__name = name - + def getName(self): return self.__name - + def read(self): return ConfigReader.read(self, "filter.d/" + self.__file) - + def getOptions(self, pOpts): opts = [["string", "ignoreregex", ""], - ["string", "failregex", ""]] + ["string", "failregex", ""], + ["string", "ignorecommand", ""] + ] self.__opts = ConfigReader.getOptions(self, "Definition", opts, pOpts) - + def convert(self): stream = list() for opt in self.__opts: @@ -69,6 +71,6 @@ class FilterReader(ConfigReader): for regex in self.__opts[opt].split('\n'): # Do not send a command if the rule is empty. if regex != '': - stream.append(["set", self.__name, "addignoreregex", regex]) + stream.append(["set", self.__name, "addignoreregex", regex]) return stream - + diff --git a/client/jailreader.py b/client/jailreader.py index 7fbac423..7073c0cb 100644 --- a/client/jailreader.py +++ b/client/jailreader.py @@ -18,7 +18,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Author: Cyril Jaquier -# +# __author__ = "Cyril Jaquier" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" @@ -34,25 +34,25 @@ from actionreader import ActionReader logSys = logging.getLogger("fail2ban.client.config") class JailReader(ConfigReader): - + actionCRE = re.compile("^((?:\w|-|_|\.)+)(?:\[(.*)\])?$") - + def __init__(self, name, force_enable=False, **kwargs): ConfigReader.__init__(self, **kwargs) self.__name = name self.__filter = None self.__force_enable = force_enable self.__actions = list() - + def setName(self, value): self.__name = value - + def getName(self): return self.__name - + def read(self): return ConfigReader.read(self, "jail") - + def isEnabled(self): return self.__force_enable or self.__opts["enabled"] @@ -81,12 +81,13 @@ class JailReader(ConfigReader): ["int", "bantime", 600], ["string", "usedns", "warn"], ["string", "failregex", None], + ["string", "ignorecommand", None], ["string", "ignoreregex", None], ["string", "ignoreip", None], ["string", "filter", ""], ["string", "action", ""]] self.__opts = ConfigReader.getOptions(self, self.__name, opts) - + if self.isEnabled(): # Read filter self.__filter = FilterReader(self.__opts["filter"], self.__name, @@ -97,7 +98,7 @@ class JailReader(ConfigReader): else: logSys.error("Unable to read the filter") return False - + # Read action for act in self.__opts["action"].split('\n'): try: @@ -118,7 +119,7 @@ class JailReader(ConfigReader): if not len(self.__actions): logSys.warn("No actions were defined for %s" % self.__name) return True - + def convert(self, allow_no_files=False): """Convert read before __opts to the commands stream @@ -160,6 +161,8 @@ class JailReader(ConfigReader): stream.append(["set", self.__name, "usedns", self.__opts[opt]]) elif opt == "failregex": stream.append(["set", self.__name, "addfailregex", self.__opts[opt]]) + elif opt == "ignorecommand": + stream.append(["set", self.__name, "ignorecommand", self.__opts[opt]]) elif opt == "ignoreregex": for regex in self.__opts[opt].split('\n'): # Do not send a command if the rule is empty. @@ -170,7 +173,7 @@ class JailReader(ConfigReader): stream.extend(action.convert()) stream.insert(0, ["add", self.__name, backend]) return stream - + #@staticmethod def splitAction(action): m = JailReader.actionCRE.match(action) @@ -202,12 +205,12 @@ class JailReader(ConfigReader): actions += "" else: actions += c - + # Split using , actionsSplit = actions.split(',') # Replace the tag with , actionsSplit = [n.replace("", ',') for n in actionsSplit] - + for param in actionsSplit: p = param.split('=') try: diff --git a/common/protocol.py b/common/protocol.py index 9309ce7f..9e5ede7a 100644 --- a/common/protocol.py +++ b/common/protocol.py @@ -18,7 +18,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Author: Cyril Jaquier -# +# __author__ = "Cyril Jaquier" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" @@ -31,49 +31,51 @@ import textwrap protocol = [ ['', "BASIC", ""], -["start", "starts the server and the jails"], -["reload", "reloads the configuration"], -["reload ", "reloads the jail "], -["stop", "stops all jails and terminate the server"], -["status", "gets the current status of the server"], -["ping", "tests if the server is alive"], -["help", "return this output"], +["start", "starts the server and the jails"], +["reload", "reloads the configuration"], +["reload ", "reloads the jail "], +["stop", "stops all jails and terminate the server"], +["status", "gets the current status of the server"], +["ping", "tests if the server is alive"], +["help", "return this output"], ['', "LOGGING", ""], -["set loglevel ", "sets logging level to . 0 is minimal, 4 is debug"], -["get loglevel", "gets the logging level"], -["set logtarget ", "sets logging target to . Can be STDOUT, STDERR, SYSLOG or a file"], -["get logtarget", "gets logging target"], +["set loglevel ", "sets logging level to . 0 is minimal, 4 is debug"], +["get loglevel", "gets the logging level"], +["set logtarget ", "sets logging target to . Can be STDOUT, STDERR, SYSLOG or a file"], +["get logtarget", "gets logging target"], ['', "JAIL CONTROL", ""], -["add ", "creates using "], -["start ", "starts the jail "], -["stop ", "stops the jail . The jail is removed"], +["add ", "creates using "], +["start ", "starts the jail "], +["stop ", "stops the jail . The jail is removed"], ["status ", "gets the current status of "], ['', "JAIL CONFIGURATION", ""], -["set idle on|off", "sets the idle state of "], -["set addignoreip ", "adds to the ignore list of "], -["set delignoreip ", "removes from the ignore list of "], -["set addlogpath ", "adds to the monitoring list of "], +["set ignorecommand ", "sets ignorecommand of "], +["set idle on|off", "sets the idle state of "], +["set addignoreip ", "adds to the ignore list of "], +["set delignoreip ", "removes from the ignore list of "], +["set addlogpath ", "adds to the monitoring list of "], ["set dellogpath ", "removes from the monitoring list of "], -["set addfailregex ", "adds the regular expression which must match failures for "], -["set delfailregex ", "removes the regular expression at for failregex"], +["set addfailregex ", "adds the regular expression which must match failures for "], +["set delfailregex ", "removes the regular expression at for failregex"], ["set addignoreregex ", "adds the regular expression which should match pattern to exclude for "], -["set delignoreregex ", "removes the regular expression at for ignoreregex"], -["set findtime