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) print "Use %11s file : %s" % (regex, value)
try: reader = FilterReader(value, 'fail2ban-regex-jail', {})
reader.read(value) reader.setBaseDir(None)
print "Use %11s file : %s" % (regex, value)
# TODO: reuse functionality in client
regex_values = [
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
# Read out and set possible value of maxlines if reader.readexplicit():
try: reader.getOptions(None)
maxlines = reader.get("Init", "maxlines") readercommands = reader.convert()
except (NoSectionError, NoOptionError): regex_values = [
# No [Init].maxlines found. RegexStat(m[3])
pass for m in filter(
lambda x: x[0] == 'set' and x[2] == "add%sregex" % regextype,
readercommands)]
# Read out and set possible value of maxlines
for command in readercommands:
if command[2] == "maxlines":
maxlines = int(command[3])
try:
self.setMaxLines(maxlines)
except ValueError:
print "ERROR: Invalid value for maxlines (%(maxlines)r) " \
"read from %(value)s" % locals()
return False
elif command[2] == 'addjournalmatch':
journalmatch = command[3]
self.setJournalMatch(shlex.split(journalmatch))
else: else:
try: print "ERROR: failed to read %s" % value
self.setMaxLines(maxlines) return False
except ValueError:
print "ERROR: Invalid value for maxlines (%(maxlines)r) " \
"read from %(value)s" % locals()
return False
# Read out and set possible value for journalmatch
try:
journalmatch = reader.get("Init", "journalmatch")
except (NoSectionError, NoOptionError):
# No [Init].journalmatch found.
pass
else:
self.setJournalMatch(shlex.split(journalmatch))
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

@ -157,6 +157,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(