2011-12-30 05:18:52 +00:00
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 noet :
|
|
|
|
|
|
|
|
# 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-12-31 07:01:19 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-12-30 05:18:52 +00:00
|
|
|
|
2012-01-09 02:27:12 +00:00
|
|
|
# Original author: Cyril Jaquier
|
2011-12-30 05:18:52 +00:00
|
|
|
|
2012-01-09 02:27:12 +00:00
|
|
|
__author__ = "Cyril Jaquier, Lee Clemens, Yaroslav Halchenko"
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2011-2012 Lee Clemens, 2012 Yaroslav Halchenko"
|
2011-12-30 05:18:52 +00:00
|
|
|
__license__ = "GPL"
|
|
|
|
|
2013-05-10 15:40:12 +00:00
|
|
|
import time, logging, pyinotify
|
|
|
|
|
2012-11-06 18:14:42 +00:00
|
|
|
from distutils.version import LooseVersion
|
2013-05-10 15:40:12 +00:00
|
|
|
from os.path import dirname, sep as pathsep
|
2012-11-06 18:14:42 +00:00
|
|
|
|
2011-12-30 05:18:52 +00:00
|
|
|
from failmanager import FailManagerEmpty
|
|
|
|
from filter import FileFilter
|
|
|
|
from mytime import MyTime
|
|
|
|
|
|
|
|
|
2012-11-06 18:14:42 +00:00
|
|
|
if not hasattr(pyinotify, '__version__') \
|
|
|
|
or LooseVersion(pyinotify.__version__) < '0.8.3':
|
|
|
|
raise ImportError("Fail2Ban requires pyinotify >= 0.8.3")
|
|
|
|
|
2013-05-10 15:40:12 +00:00
|
|
|
# Verify that pyinotify is functional on this system
|
|
|
|
# Even though imports -- might be dysfunctional, e.g. as on kfreebsd
|
|
|
|
try:
|
|
|
|
manager = pyinotify.WatchManager()
|
|
|
|
del manager
|
|
|
|
except Exception, e:
|
|
|
|
raise ImportError("Pyinotify is probably not functional on this system: %s"
|
|
|
|
% str(e))
|
2012-07-19 17:28:48 +00:00
|
|
|
|
2011-12-30 05:18:52 +00:00
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.filter")
|
|
|
|
|
|
|
|
##
|
|
|
|
# Log reader class.
|
|
|
|
#
|
|
|
|
# This class reads a log file and detects login failures or anything else
|
|
|
|
# that matches a given regular expression. This class is instantiated by
|
|
|
|
# a Jail object.
|
|
|
|
|
|
|
|
class FilterPyinotify(FileFilter):
|
2011-12-30 05:28:17 +00:00
|
|
|
##
|
2011-12-30 05:18:52 +00:00
|
|
|
# Constructor.
|
|
|
|
#
|
|
|
|
# Initialize the filter object with default values.
|
|
|
|
# @param jail the jail object
|
2011-12-30 05:28:17 +00:00
|
|
|
|
2011-12-30 05:18:52 +00:00
|
|
|
def __init__(self, jail):
|
|
|
|
FileFilter.__init__(self, jail)
|
|
|
|
self.__modified = False
|
2011-12-30 05:28:17 +00:00
|
|
|
# Pyinotify watch manager
|
2011-12-31 08:37:43 +00:00
|
|
|
self.__monitor = pyinotify.WatchManager()
|
2011-12-31 08:33:58 +00:00
|
|
|
self.__watches = dict()
|
2012-01-04 01:14:24 +00:00
|
|
|
logSys.debug("Created FilterPyinotify")
|
2011-12-30 05:18:52 +00:00
|
|
|
|
|
|
|
|
2013-03-26 01:29:39 +00:00
|
|
|
def callback(self, event, origin=''):
|
|
|
|
logSys.debug("%sCallback for Event: %s", origin, event)
|
2012-07-19 21:26:09 +00:00
|
|
|
path = event.pathname
|
2013-04-29 17:54:14 +00:00
|
|
|
if event.mask & ( pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO ):
|
2013-01-25 21:01:35 +00:00
|
|
|
# skip directories altogether
|
|
|
|
if event.mask & pyinotify.IN_ISDIR:
|
2013-03-26 01:29:39 +00:00
|
|
|
logSys.debug("Ignoring creation of directory %s", path)
|
2013-01-25 21:01:35 +00:00
|
|
|
return
|
2012-07-19 21:26:09 +00:00
|
|
|
# check if that is a file we care about
|
|
|
|
if not path in self.__watches:
|
2013-03-26 01:29:39 +00:00
|
|
|
logSys.debug("Ignoring creation of %s we do not monitor", path)
|
2012-07-19 21:26:09 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
# we need to substitute the watcher with a new one, so first
|
|
|
|
# remove old one
|
|
|
|
self._delFileWatcher(path)
|
|
|
|
# place a new one
|
|
|
|
self._addFileWatcher(path)
|
|
|
|
|
|
|
|
self._process_file(path)
|
|
|
|
|
|
|
|
|
|
|
|
def _process_file(self, path):
|
|
|
|
"""Process a given file
|
|
|
|
|
|
|
|
TODO -- RF:
|
|
|
|
this is a common logic and must be shared/provided by FileFilter
|
|
|
|
"""
|
2011-12-30 05:18:52 +00:00
|
|
|
self.getFailures(path)
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
ticket = self.failManager.toBan()
|
|
|
|
self.jail.putFailTicket(ticket)
|
|
|
|
except FailManagerEmpty:
|
|
|
|
self.failManager.cleanup(MyTime.time())
|
|
|
|
self.dateDetector.sortTemplate()
|
|
|
|
self.__modified = False
|
|
|
|
|
2012-07-19 21:26:09 +00:00
|
|
|
|
|
|
|
def _addFileWatcher(self, path):
|
|
|
|
wd = self.__monitor.add_watch(path, pyinotify.IN_MODIFY)
|
|
|
|
self.__watches.update(wd)
|
2013-03-26 01:29:39 +00:00
|
|
|
logSys.debug("Added file watcher for %s", path)
|
2012-07-19 21:26:09 +00:00
|
|
|
|
|
|
|
def _delFileWatcher(self, path):
|
|
|
|
wdInt = self.__watches[path]
|
|
|
|
wd = self.__monitor.rm_watch(wdInt)
|
|
|
|
if wd[wdInt]:
|
|
|
|
del self.__watches[path]
|
2013-03-26 01:29:39 +00:00
|
|
|
logSys.debug("Removed file watcher for %s", path)
|
2012-07-19 21:26:09 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2011-12-30 05:18:52 +00:00
|
|
|
##
|
|
|
|
# Add a log file path
|
|
|
|
#
|
|
|
|
# @param path log file path
|
2011-12-30 05:28:17 +00:00
|
|
|
|
2012-07-19 21:26:09 +00:00
|
|
|
def _addLogPath(self, path):
|
|
|
|
path_dir = dirname(path)
|
|
|
|
if not (path_dir in self.__watches):
|
|
|
|
# we need to watch also the directory for IN_CREATE
|
|
|
|
self.__watches.update(
|
2013-04-29 17:54:14 +00:00
|
|
|
self.__monitor.add_watch(path_dir, pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO))
|
2013-03-26 01:29:39 +00:00
|
|
|
logSys.debug("Added monitor for the parent directory %s", path_dir)
|
2012-07-19 17:28:48 +00:00
|
|
|
|
2012-07-19 21:26:09 +00:00
|
|
|
self._addFileWatcher(path)
|
2013-12-23 09:18:32 +00:00
|
|
|
self._process_file(path)
|
2011-12-30 05:18:52 +00:00
|
|
|
|
2012-07-19 17:28:48 +00:00
|
|
|
|
|
|
|
##
|
2011-12-30 05:18:52 +00:00
|
|
|
# Delete a log path
|
|
|
|
#
|
|
|
|
# @param path the log file to delete
|
|
|
|
|
2012-07-19 21:26:09 +00:00
|
|
|
def _delLogPath(self, path):
|
|
|
|
if not self._delFileWatcher(path):
|
|
|
|
logSys.error("Failed to remove watch on path: %s", path)
|
2011-12-30 05:18:52 +00:00
|
|
|
|
2012-07-19 21:26:09 +00:00
|
|
|
path_dir = dirname(path)
|
|
|
|
if not len([k for k in self.__watches
|
|
|
|
if k.startswith(path_dir + pathsep)]):
|
|
|
|
# Remove watches for the directory
|
|
|
|
# since there is no other monitored file under this directory
|
|
|
|
wdInt = self.__watches.pop(path_dir)
|
|
|
|
_ = self.__monitor.rm_watch(wdInt)
|
2013-03-26 01:29:39 +00:00
|
|
|
logSys.debug("Removed monitor for the parent directory %s", path_dir)
|
2012-07-19 17:28:48 +00:00
|
|
|
|
|
|
|
|
2011-12-30 05:18:52 +00:00
|
|
|
##
|
|
|
|
# Main loop.
|
|
|
|
#
|
2012-07-19 05:14:02 +00:00
|
|
|
# Since all detection is offloaded to pyinotifier -- no manual
|
|
|
|
# loop is necessary
|
2011-12-30 05:18:52 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.setActive(True)
|
2011-12-31 08:37:43 +00:00
|
|
|
self.__notifier = pyinotify.ThreadedNotifier(self.__monitor,
|
2011-12-30 05:18:52 +00:00
|
|
|
ProcessPyinotify(self))
|
2011-12-31 08:37:43 +00:00
|
|
|
self.__notifier.start()
|
2013-03-26 01:29:39 +00:00
|
|
|
logSys.debug("pyinotifier started for %s.", self.jail.getName())
|
2012-07-19 05:14:02 +00:00
|
|
|
# TODO: verify that there is nothing really to be done for
|
|
|
|
# idle jails
|
2011-12-30 05:18:52 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
##
|
|
|
|
# Call super.stop() and then stop the 'Notifier'
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
super(FilterPyinotify, self).stop()
|
2012-07-19 05:14:02 +00:00
|
|
|
|
|
|
|
# Stop the notifier thread
|
2011-12-31 08:37:43 +00:00
|
|
|
self.__notifier.stop()
|
2012-07-19 05:14:02 +00:00
|
|
|
self.__notifier.join() # to not exit before notifier does
|
|
|
|
self.__cleanup() # for pedantic ones
|
2011-12-30 05:18:52 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Deallocates the resources used by pyinotify.
|
|
|
|
|
|
|
|
def __cleanup(self):
|
2012-07-19 05:14:02 +00:00
|
|
|
self.__notifier = None
|
|
|
|
self.__monitor = None
|
2011-12-30 05:28:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProcessPyinotify(pyinotify.ProcessEvent):
|
|
|
|
def __init__(self, FileFilter, **kargs):
|
2012-01-09 02:27:12 +00:00
|
|
|
#super(ProcessPyinotify, self).__init__(**kargs)
|
|
|
|
# for some reason root class _ProcessEvent is old-style (is
|
|
|
|
# not derived from object), so to play safe let's avoid super
|
|
|
|
# for now, and call superclass directly
|
|
|
|
pyinotify.ProcessEvent.__init__(self, **kargs)
|
2011-12-30 05:28:17 +00:00
|
|
|
self.__FileFilter = FileFilter
|
|
|
|
pass
|
|
|
|
|
|
|
|
# just need default, since using mask on watch to limit events
|
|
|
|
def process_default(self, event):
|
2013-03-26 01:29:39 +00:00
|
|
|
self.__FileFilter.callback(event, origin='Default ')
|