mirror of https://github.com/fail2ban/fail2ban
ENH: allow to force enable all jails (for testing), do not crash for jails without actions (just warn)
also a bit more explicit handling of regexp groups in splitActionpull/155/head
parent
2fb053643e
commit
8fe4e11b67
|
@ -40,10 +40,11 @@ class JailReader(ConfigReader):
|
||||||
|
|
||||||
actionCRE = re.compile("^((?:\w|-|_|\.)+)(?:\[(.*)\])?$")
|
actionCRE = re.compile("^((?:\w|-|_|\.)+)(?:\[(.*)\])?$")
|
||||||
|
|
||||||
def __init__(self, name, **kwargs):
|
def __init__(self, name, force_enable=False, **kwargs):
|
||||||
ConfigReader.__init__(self, **kwargs)
|
ConfigReader.__init__(self, **kwargs)
|
||||||
self.__name = name
|
self.__name = name
|
||||||
self.__filter = None
|
self.__filter = None
|
||||||
|
self.__force_enable = force_enable
|
||||||
self.__actions = list()
|
self.__actions = list()
|
||||||
|
|
||||||
def setName(self, value):
|
def setName(self, value):
|
||||||
|
@ -56,7 +57,7 @@ class JailReader(ConfigReader):
|
||||||
return ConfigReader.read(self, "jail")
|
return ConfigReader.read(self, "jail")
|
||||||
|
|
||||||
def isEnabled(self):
|
def isEnabled(self):
|
||||||
return self.__opts["enabled"]
|
return self.__force_enable or self.__opts["enabled"]
|
||||||
|
|
||||||
def getOptions(self):
|
def getOptions(self):
|
||||||
opts = [["bool", "enabled", "false"],
|
opts = [["bool", "enabled", "false"],
|
||||||
|
@ -87,6 +88,8 @@ class JailReader(ConfigReader):
|
||||||
# Read action
|
# Read action
|
||||||
for act in self.__opts["action"].split('\n'):
|
for act in self.__opts["action"].split('\n'):
|
||||||
try:
|
try:
|
||||||
|
if not act: # skip empty actions
|
||||||
|
continue
|
||||||
splitAct = JailReader.splitAction(act)
|
splitAct = JailReader.splitAction(act)
|
||||||
action = ActionReader(splitAct, self.__name, basedir=self.getBaseDir())
|
action = ActionReader(splitAct, self.__name, basedir=self.getBaseDir())
|
||||||
ret = action.read()
|
ret = action.read()
|
||||||
|
@ -97,8 +100,10 @@ class JailReader(ConfigReader):
|
||||||
raise AttributeError("Unable to read action")
|
raise AttributeError("Unable to read action")
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
logSys.error("Error in action definition " + act)
|
logSys.error("Error in action definition " + act)
|
||||||
logSys.debug(e)
|
logSys.debug("Caught exception: %s" % (e,))
|
||||||
return False
|
return False
|
||||||
|
if not len(self.__actions):
|
||||||
|
logSys.warn("No actions were defined for %s" % self.__name)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def convert(self):
|
def convert(self):
|
||||||
|
@ -143,12 +148,20 @@ class JailReader(ConfigReader):
|
||||||
def splitAction(action):
|
def splitAction(action):
|
||||||
m = JailReader.actionCRE.match(action)
|
m = JailReader.actionCRE.match(action)
|
||||||
d = dict()
|
d = dict()
|
||||||
if not m.group(2) == None:
|
mgroups = m.groups()
|
||||||
|
if len(mgroups) == 2:
|
||||||
|
action_name, action_opts = mgroups
|
||||||
|
elif len(mgroups) == 1:
|
||||||
|
action_name, action_opts = mgroups[0], None
|
||||||
|
else:
|
||||||
|
raise ValueError("While reading action %s we should have got up to "
|
||||||
|
"2 groups. Got: %r" % (action, mgroups))
|
||||||
|
if not action_opts is None:
|
||||||
# Huge bad hack :( This method really sucks. TODO Reimplement it.
|
# Huge bad hack :( This method really sucks. TODO Reimplement it.
|
||||||
actions = ""
|
actions = ""
|
||||||
escapeChar = None
|
escapeChar = None
|
||||||
allowComma = False
|
allowComma = False
|
||||||
for c in m.group(2):
|
for c in action_opts:
|
||||||
if c in ('"', "'") and not allowComma:
|
if c in ('"', "'") and not allowComma:
|
||||||
# Start
|
# Start
|
||||||
escapeChar = c
|
escapeChar = c
|
||||||
|
@ -173,6 +186,6 @@ class JailReader(ConfigReader):
|
||||||
try:
|
try:
|
||||||
d[p[0].strip()] = p[1].strip()
|
d[p[0].strip()] = p[1].strip()
|
||||||
except IndexError:
|
except IndexError:
|
||||||
logSys.error("Invalid argument %s in '%s'" % (p, m.group(2)))
|
logSys.error("Invalid argument %s in '%s'" % (p, action_opts))
|
||||||
return [m.group(1), d]
|
return [action_name, d]
|
||||||
splitAction = staticmethod(splitAction)
|
splitAction = staticmethod(splitAction)
|
||||||
|
|
|
@ -36,9 +36,17 @@ logSys = logging.getLogger("fail2ban.client.config")
|
||||||
|
|
||||||
class JailsReader(ConfigReader):
|
class JailsReader(ConfigReader):
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, force_enable=False, **kwargs):
|
||||||
|
"""
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
force_enable : bool, optional
|
||||||
|
Passed to JailReader to force enable the jails.
|
||||||
|
It is for internal use
|
||||||
|
"""
|
||||||
ConfigReader.__init__(self, **kwargs)
|
ConfigReader.__init__(self, **kwargs)
|
||||||
self.__jails = list()
|
self.__jails = list()
|
||||||
|
self.__force_enable = force_enable
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
return ConfigReader.read(self, "jail")
|
return ConfigReader.read(self, "jail")
|
||||||
|
@ -49,7 +57,7 @@ class JailsReader(ConfigReader):
|
||||||
|
|
||||||
if section:
|
if section:
|
||||||
# Get the options of a specific jail.
|
# Get the options of a specific jail.
|
||||||
jail = JailReader(section, basedir=self.getBaseDir())
|
jail = JailReader(section, basedir=self.getBaseDir(), force_enable=self.__force_enable)
|
||||||
jail.read()
|
jail.read()
|
||||||
ret = jail.getOptions()
|
ret = jail.getOptions()
|
||||||
if ret:
|
if ret:
|
||||||
|
@ -62,7 +70,7 @@ class JailsReader(ConfigReader):
|
||||||
else:
|
else:
|
||||||
# Get the options of all jails.
|
# Get the options of all jails.
|
||||||
for sec in self.sections():
|
for sec in self.sections():
|
||||||
jail = JailReader(sec, basedir=self.getBaseDir())
|
jail = JailReader(sec, basedir=self.getBaseDir(), force_enable=self.__force_enable)
|
||||||
jail.read()
|
jail.read()
|
||||||
ret = jail.getOptions()
|
ret = jail.getOptions()
|
||||||
if ret:
|
if ret:
|
||||||
|
|
Loading…
Reference in New Issue