ENH: fix fail2ban-regex for filter arguement substition

pull/556/head
Daniel Black 2014-01-02 10:03:14 +11:00
parent 1b037a6f29
commit 58a5983367
2 changed files with 31 additions and 41 deletions

View File

@ -41,7 +41,7 @@ except ImportError:
journal = None journal = None
from fail2ban.version import version from fail2ban.version import version
from fail2ban.client.configparserinc import SafeConfigParserWithIncludes from fail2ban.client.filterreader import FilterReader
from fail2ban.server.filter import Filter from fail2ban.server.filter import Filter
from fail2ban.server.failregex import RegexException from fail2ban.server.failregex import RegexException
@ -206,8 +206,6 @@ class LineStats(object):
class Fail2banRegex(object): class Fail2banRegex(object):
CONFIG_DEFAULTS = {'configpath' : "/etc/fail2ban/"}
def __init__(self, opts): def __init__(self, opts):
self._verbose = opts.verbose self._verbose = opts.verbose
self._debuggex = opts.debuggex self._debuggex = opts.debuggex
@ -257,46 +255,34 @@ class Fail2banRegex(object):
assert(regextype in ('fail', 'ignore')) assert(regextype in ('fail', 'ignore'))
regex = regextype + 'regex' regex = regextype + 'regex'
if os.path.isfile(value): if os.path.isfile(value):
reader = SafeConfigParserWithIncludes(defaults=self.CONFIG_DEFAULTS)
try:
reader.read(value)
print "Use %11s file : %s" % (regex, value) print "Use %11s file : %s" % (regex, value)
# TODO: reuse functionality in client reader = FilterReader(value, 'fail2ban-regex-jail', {})
regex_values = [ reader.setBaseDir(None)
RegexStat(m)
for m in reader.get("Definition", regex).split('\n')
if m != ""]
except NoSectionError:
print "No [Definition] section in %s" % value
return False
except NoOptionError:
print "No %s option in %s" % (regex, value)
return False
except MissingSectionHeaderError:
print "No section headers in %s" % value
return False
if reader.readexplicit():
reader.getOptions(None)
readercommands = reader.convert()
regex_values = [
RegexStat(m[3])
for m in filter(
lambda x: x[0] == 'set' and x[2] == "add%sregex" % regextype,
readercommands)]
# Read out and set possible value of maxlines # Read out and set possible value of maxlines
try: for command in readercommands:
maxlines = reader.get("Init", "maxlines") if command[2] == "maxlines":
except (NoSectionError, NoOptionError): maxlines = int(command[3])
# No [Init].maxlines found.
pass
else:
try: try:
self.setMaxLines(maxlines) self.setMaxLines(maxlines)
except ValueError: except ValueError:
print "ERROR: Invalid value for maxlines (%(maxlines)r) " \ print "ERROR: Invalid value for maxlines (%(maxlines)r) " \
"read from %(value)s" % locals() "read from %(value)s" % locals()
return False return False
# Read out and set possible value for journalmatch elif command[2] == 'addjournalmatch':
try: journalmatch = command[3]
journalmatch = reader.get("Init", "journalmatch")
except (NoSectionError, NoOptionError):
# No [Init].journalmatch found.
pass
else:
self.setJournalMatch(shlex.split(journalmatch)) self.setJournalMatch(shlex.split(journalmatch))
else:
print "ERROR: failed to read %s" % value
return False
else: else:
print "Use %11s line : %s" % (regex, shortstr(value)) print "Use %11s line : %s" % (regex, shortstr(value))
regex_values = [RegexStat(value)] regex_values = [RegexStat(value)]

View File

@ -158,6 +158,10 @@ class DefinitionInitConfigReader(ConfigReader):
def read(self): def read(self):
return ConfigReader.read(self, self._file) return ConfigReader.read(self, self._file)
# needed for fail2ban-regex that doesn't need fancy directories
def readexplicit(self):
return SafeConfigParserWithIncludes.read(self, self._file)
def getOptions(self, pOpts): def getOptions(self, pOpts):
self._opts = ConfigReader.getOptions( self._opts = ConfigReader.getOptions(
self, "Definition", self._configOpts, pOpts) self, "Definition", self._configOpts, pOpts)