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
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
2006-07-16 22:21:58 +00:00
|
|
|
# $Revision$
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2006-07-16 22:21:58 +00:00
|
|
|
__version__ = "$Revision$"
|
|
|
|
__date__ = "$Date$"
|
2006-06-26 20:05:00 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from configreader import ConfigReader
|
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.client.config")
|
|
|
|
|
|
|
|
class FilterReader(ConfigReader):
|
|
|
|
|
2006-10-24 19:40:51 +00:00
|
|
|
def __init__(self, fileName, name):
|
2006-06-26 20:05:00 +00:00
|
|
|
ConfigReader.__init__(self)
|
2006-10-24 19:40:51 +00:00
|
|
|
self.__file = fileName
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__name = name
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-10-24 19:40:51 +00:00
|
|
|
def setFile(self, fileName):
|
|
|
|
self.__file = fileName
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getFile(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__file
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def setName(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__name = name
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getName(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__name
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def read(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return ConfigReader.read(self, "filter.d/" + self.__file)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getOptions(self, pOpts):
|
2006-09-05 21:16:28 +00:00
|
|
|
opts = [["string", "timeregex", None],
|
|
|
|
["string", "timepattern", None],
|
2006-11-12 14:52:36 +00:00
|
|
|
["string", "ignoreregex", ""],
|
2006-09-04 19:18:57 +00:00
|
|
|
["string", "failregex", ""]]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__opts = ConfigReader.getOptions(self, "Definition", opts, pOpts)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def convert(self):
|
|
|
|
stream = list()
|
2006-09-19 20:38:32 +00:00
|
|
|
for opt in self.__opts:
|
2006-09-04 19:18:57 +00:00
|
|
|
if opt == "timeregex":
|
2006-09-19 20:38:32 +00:00
|
|
|
stream.append(["set", self.__name, "timeregex", self.__opts[opt]])
|
2006-06-26 20:05:00 +00:00
|
|
|
elif opt == "timepattern":
|
2006-09-19 20:38:32 +00:00
|
|
|
stream.append(["set", self.__name, "timepattern", self.__opts[opt]])
|
2006-06-26 20:05:00 +00:00
|
|
|
elif opt == "failregex":
|
2006-12-23 16:31:00 +00:00
|
|
|
for regex in self.__opts[opt].split('\n'):
|
2006-12-23 23:20:16 +00:00
|
|
|
# Do not send a command if the rule is empty.
|
|
|
|
if regex != '':
|
|
|
|
stream.append(["set", self.__name, "addfailregex", regex])
|
2006-11-12 14:52:36 +00:00
|
|
|
elif opt == "ignoreregex":
|
2006-12-23 16:31:00 +00:00
|
|
|
for regex in self.__opts[opt].split('\n'):
|
2006-12-23 23:20:16 +00:00
|
|
|
# Do not send a command if the rule is empty.
|
|
|
|
if regex != '':
|
|
|
|
stream.append(["set", self.__name, "addignoreregex", regex])
|
2006-06-26 20:05:00 +00:00
|
|
|
return stream
|
|
|
|
|