2011-10-07 19:47:50 +00:00
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 noet :
|
|
|
|
|
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
|
2011-11-21 12:20:20 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2013-09-29 02:16:34 +00:00
|
|
|
import logging, re, glob, os.path
|
2006-09-13 21:31:22 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
from configreader import ConfigReader
|
|
|
|
from filterreader import FilterReader
|
|
|
|
from actionreader import ActionReader
|
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.client.config")
|
|
|
|
|
|
|
|
class JailReader(ConfigReader):
|
|
|
|
|
2013-12-12 20:07:09 +00:00
|
|
|
actionCRE = re.compile("^([\w_.-]+)(?:\[(.*)\])?$")
|
2006-08-20 22:32:44 +00:00
|
|
|
|
2013-03-26 04:01:12 +00:00
|
|
|
def __init__(self, name, force_enable=False, **kwargs):
|
2013-02-17 22:03:23 +00:00
|
|
|
ConfigReader.__init__(self, **kwargs)
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__name = name
|
|
|
|
self.__filter = None
|
2013-03-26 04:01:12 +00:00
|
|
|
self.__force_enable = force_enable
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__actions = list()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def setName(self, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__name = value
|
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):
|
2013-02-17 22:03:23 +00:00
|
|
|
return ConfigReader.read(self, "jail")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def isEnabled(self):
|
2013-12-13 10:00:34 +00:00
|
|
|
return self.__force_enable or ( self.__opts and self.__opts["enabled"] )
|
2013-09-29 02:16:34 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _glob(path):
|
|
|
|
"""Given a path for glob return list of files to be passed to server.
|
|
|
|
|
|
|
|
Dangling symlinks are warned about and not returned
|
|
|
|
"""
|
|
|
|
pathList = []
|
|
|
|
for p in glob.glob(path):
|
2013-12-12 09:08:42 +00:00
|
|
|
if os.path.exists(p):
|
2013-09-29 02:16:34 +00:00
|
|
|
pathList.append(p)
|
2013-12-12 09:08:42 +00:00
|
|
|
else:
|
|
|
|
logSys.warning("File %s is a dangling link, thus cannot be monitored" % p)
|
2013-09-29 02:16:34 +00:00
|
|
|
return pathList
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def getOptions(self):
|
|
|
|
opts = [["bool", "enabled", "false"],
|
2006-09-04 19:18:57 +00:00
|
|
|
["string", "logpath", "/var/log/messages"],
|
2006-10-16 19:42:50 +00:00
|
|
|
["string", "backend", "auto"],
|
2006-09-04 19:18:57 +00:00
|
|
|
["int", "maxretry", 3],
|
2006-11-18 15:15:58 +00:00
|
|
|
["int", "findtime", 600],
|
2006-07-08 16:51:14 +00:00
|
|
|
["int", "bantime", 600],
|
2012-01-13 04:23:41 +00:00
|
|
|
["string", "usedns", "warn"],
|
2006-11-12 14:52:36 +00:00
|
|
|
["string", "failregex", None],
|
|
|
|
["string", "ignoreregex", None],
|
2006-09-17 22:01:14 +00:00
|
|
|
["string", "ignoreip", None],
|
2006-06-26 20:05:00 +00:00
|
|
|
["string", "filter", ""],
|
|
|
|
["string", "action", ""]]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__opts = ConfigReader.getOptions(self, self.__name, opts)
|
2013-12-13 10:00:34 +00:00
|
|
|
if not self.__opts:
|
|
|
|
return False
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
if self.isEnabled():
|
|
|
|
# Read filter
|
2013-12-13 08:45:10 +00:00
|
|
|
if self.__opts["filter"]:
|
|
|
|
self.__filter = FilterReader(self.__opts["filter"], self.__name,
|
|
|
|
basedir=self.getBaseDir())
|
|
|
|
ret = self.__filter.read()
|
|
|
|
if ret:
|
|
|
|
self.__filter.getOptions(self.__opts)
|
|
|
|
else:
|
|
|
|
logSys.error("Unable to read the filter")
|
|
|
|
return False
|
2006-09-07 21:00:44 +00:00
|
|
|
else:
|
2013-12-13 11:36:00 +00:00
|
|
|
self.__filter = None
|
2013-12-13 08:45:10 +00:00
|
|
|
logSys.warn("No filter set for jail %s" % self.__name)
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
# Read action
|
2006-09-19 20:38:32 +00:00
|
|
|
for act in self.__opts["action"].split('\n'):
|
2006-08-20 20:38:08 +00:00
|
|
|
try:
|
2013-03-26 04:01:12 +00:00
|
|
|
if not act: # skip empty actions
|
|
|
|
continue
|
2006-08-20 20:38:08 +00:00
|
|
|
splitAct = JailReader.splitAction(act)
|
2013-02-17 22:03:23 +00:00
|
|
|
action = ActionReader(splitAct, self.__name, basedir=self.getBaseDir())
|
2006-09-07 21:00:44 +00:00
|
|
|
ret = action.read()
|
|
|
|
if ret:
|
2006-09-19 20:38:32 +00:00
|
|
|
action.getOptions(self.__opts)
|
|
|
|
self.__actions.append(action)
|
2006-09-07 21:00:44 +00:00
|
|
|
else:
|
|
|
|
raise AttributeError("Unable to read action")
|
2008-03-04 23:09:30 +00:00
|
|
|
except Exception, e:
|
2006-08-20 20:38:08 +00:00
|
|
|
logSys.error("Error in action definition " + act)
|
2013-03-26 04:01:12 +00:00
|
|
|
logSys.debug("Caught exception: %s" % (e,))
|
2006-09-07 21:00:44 +00:00
|
|
|
return False
|
2013-03-26 04:01:12 +00:00
|
|
|
if not len(self.__actions):
|
|
|
|
logSys.warn("No actions were defined for %s" % self.__name)
|
2006-09-07 21:00:44 +00:00
|
|
|
return True
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2013-06-21 16:44:37 +00:00
|
|
|
def convert(self, allow_no_files=False):
|
|
|
|
"""Convert read before __opts to the commands stream
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
allow_missing : bool
|
|
|
|
Either to allow log files to be missing entirely. Primarily is
|
|
|
|
used for testing
|
|
|
|
"""
|
|
|
|
|
2006-10-16 19:42:50 +00:00
|
|
|
stream = []
|
2006-09-19 20:38:32 +00:00
|
|
|
for opt in self.__opts:
|
2006-09-04 19:18:57 +00:00
|
|
|
if opt == "logpath":
|
2013-06-21 15:08:27 +00:00
|
|
|
found_files = 0
|
2006-10-01 21:19:24 +00:00
|
|
|
for path in self.__opts[opt].split("\n"):
|
2013-09-29 02:16:34 +00:00
|
|
|
pathList = JailReader._glob(path)
|
2006-10-01 21:19:24 +00:00
|
|
|
if len(pathList) == 0:
|
2013-06-21 15:08:27 +00:00
|
|
|
logSys.error("No file(s) found for glob %s" % path)
|
2006-10-01 21:19:24 +00:00
|
|
|
for p in pathList:
|
2013-06-21 15:08:27 +00:00
|
|
|
found_files += 1
|
2006-10-01 21:19:24 +00:00
|
|
|
stream.append(["set", self.__name, "addlogpath", p])
|
2013-06-21 16:44:37 +00:00
|
|
|
if not (found_files or allow_no_files):
|
2013-06-21 15:08:27 +00:00
|
|
|
raise ValueError(
|
|
|
|
"Have not found any log file for %s jail" % self.__name)
|
2006-10-16 19:42:50 +00:00
|
|
|
elif opt == "backend":
|
|
|
|
backend = self.__opts[opt]
|
2006-09-04 19:18:57 +00:00
|
|
|
elif opt == "maxretry":
|
2006-09-19 20:38:32 +00:00
|
|
|
stream.append(["set", self.__name, "maxretry", self.__opts[opt]])
|
2006-09-17 22:01:14 +00:00
|
|
|
elif opt == "ignoreip":
|
2006-10-19 19:51:59 +00:00
|
|
|
for ip in self.__opts[opt].split():
|
2006-12-23 23:20:16 +00:00
|
|
|
# Do not send a command if the rule is empty.
|
|
|
|
if ip != '':
|
|
|
|
stream.append(["set", self.__name, "addignoreip", ip])
|
2006-11-18 15:15:58 +00:00
|
|
|
elif opt == "findtime":
|
|
|
|
stream.append(["set", self.__name, "findtime", self.__opts[opt]])
|
2006-06-26 20:05:00 +00:00
|
|
|
elif opt == "bantime":
|
2006-09-19 20:38:32 +00:00
|
|
|
stream.append(["set", self.__name, "bantime", self.__opts[opt]])
|
2012-01-13 04:23:41 +00:00
|
|
|
elif opt == "usedns":
|
|
|
|
stream.append(["set", self.__name, "usedns", self.__opts[opt]])
|
2006-11-12 14:52:36 +00:00
|
|
|
elif opt == "failregex":
|
2011-07-29 02:08:31 +00:00
|
|
|
stream.append(["set", self.__name, "addfailregex", self.__opts[opt]])
|
2006-11-12 14:52:36 +00:00
|
|
|
elif opt == "ignoreregex":
|
2008-05-12 08:34:42 +00:00
|
|
|
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])
|
2013-12-13 11:36:00 +00:00
|
|
|
if self.__filter:
|
|
|
|
stream.extend(self.__filter.convert())
|
2006-09-19 20:38:32 +00:00
|
|
|
for action in self.__actions:
|
2006-07-08 16:51:14 +00:00
|
|
|
stream.extend(action.convert())
|
2006-10-16 19:42:50 +00:00
|
|
|
stream.insert(0, ["add", self.__name, backend])
|
2006-06-26 20:05:00 +00:00
|
|
|
return stream
|
2006-07-16 21:35:08 +00:00
|
|
|
|
2007-12-16 21:38:04 +00:00
|
|
|
#@staticmethod
|
2006-07-16 21:35:08 +00:00
|
|
|
def splitAction(action):
|
2006-08-20 22:32:44 +00:00
|
|
|
m = JailReader.actionCRE.match(action)
|
2006-07-16 21:35:08 +00:00
|
|
|
d = dict()
|
2013-12-12 20:07:09 +00:00
|
|
|
try:
|
|
|
|
mgroups = m.groups()
|
|
|
|
except AttributeError:
|
|
|
|
raise ValueError("While reading action %s we should have got 1 or "
|
|
|
|
"2 groups. Got: 0" % action)
|
2013-03-26 04:01:12 +00:00
|
|
|
if len(mgroups) == 2:
|
|
|
|
action_name, action_opts = mgroups
|
2013-12-12 20:35:30 +00:00
|
|
|
elif len(mgroups) == 1: # pragma: nocover - unreachable - .* on second group always matches
|
2013-03-26 04:01:12 +00:00
|
|
|
action_name, action_opts = mgroups[0], None
|
2013-12-12 20:07:09 +00:00
|
|
|
else: # pragma: nocover - unreachable - regex only can capture 2 groups
|
2013-03-26 04:01:12 +00:00
|
|
|
raise ValueError("While reading action %s we should have got up to "
|
|
|
|
"2 groups. Got: %r" % (action, mgroups))
|
|
|
|
if not action_opts is None:
|
2007-01-04 11:58:58 +00:00
|
|
|
# Huge bad hack :( This method really sucks. TODO Reimplement it.
|
|
|
|
actions = ""
|
|
|
|
escapeChar = None
|
|
|
|
allowComma = False
|
2013-03-26 04:01:12 +00:00
|
|
|
for c in action_opts:
|
2007-01-04 11:58:58 +00:00
|
|
|
if c in ('"', "'") and not allowComma:
|
|
|
|
# Start
|
|
|
|
escapeChar = c
|
|
|
|
allowComma = True
|
|
|
|
elif c == escapeChar:
|
|
|
|
# End
|
|
|
|
escapeChar = None
|
|
|
|
allowComma = False
|
|
|
|
else:
|
|
|
|
if c == ',' and allowComma:
|
|
|
|
actions += "<COMMA>"
|
|
|
|
else:
|
|
|
|
actions += c
|
|
|
|
|
|
|
|
# Split using ,
|
|
|
|
actionsSplit = actions.split(',')
|
|
|
|
# Replace the tag <COMMA> with ,
|
|
|
|
actionsSplit = [n.replace("<COMMA>", ',') for n in actionsSplit]
|
|
|
|
|
|
|
|
for param in actionsSplit:
|
2006-07-16 21:35:08 +00:00
|
|
|
p = param.split('=')
|
2006-10-30 23:02:05 +00:00
|
|
|
try:
|
|
|
|
d[p[0].strip()] = p[1].strip()
|
|
|
|
except IndexError:
|
2013-03-26 04:01:12 +00:00
|
|
|
logSys.error("Invalid argument %s in '%s'" % (p, action_opts))
|
|
|
|
return [action_name, d]
|
2007-12-16 21:38:04 +00:00
|
|
|
splitAction = staticmethod(splitAction)
|