~ubuntu-branches/ubuntu/saucy/fail2ban/saucy

« back to all changes in this revision

Viewing changes to server/filterpyinotify.py

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-05-13 11:58:56 UTC
  • mfrom: (1.1.15) (10.2.13 sid)
  • Revision ID: package-import@ubuntu.com-20130513115856-x7six9p53qcie0vl
Tags: 0.8.9-1
* New upstream release
  - significant improvements in documentation (Closes: #400416)
  - roundcube auth filter (Closes: #699442)
  - enforces C locale for dates (Closes: #686341)
  - provides bash_completion.d/fail2ban
* debian/jail.conf:
  - added findtime and documentation on those basic options from jail.conf
    (Closes: #704568)
  - added new sample jails definitions for ssh-route, ssh-iptables-ipset{4,6},
    roundcube-auth, sogo-auth, mysqld-auth
* debian/control:
  - suggest system-log-daemon (Closes: #691001)
  - boost policy compliance to 3.9.4
* debian/rules:
  - run fail2ban's unittests at build time but ignore the failures
    (there are still some known issues to fix up to guarantee robust testing
    in clean chroots etc).
    Only pyinotify was added to build-depends since gamin might still be
    buggy on older releases and get stuck, which would complicate
    backporting

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
__copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2011-2012 Lee Clemens, 2012 Yaroslav Halchenko"
24
24
__license__ = "GPL"
25
25
 
 
26
import time, logging, pyinotify
 
27
 
 
28
from distutils.version import LooseVersion
 
29
from os.path import dirname, sep as pathsep
 
30
 
26
31
from failmanager import FailManagerEmpty
27
32
from filter import FileFilter
28
33
from mytime import MyTime
29
34
 
30
 
import time, logging, pyinotify
31
 
 
32
 
from os.path import dirname, sep as pathsep
 
35
 
 
36
if not hasattr(pyinotify, '__version__') \
 
37
  or LooseVersion(pyinotify.__version__) < '0.8.3':
 
38
  raise ImportError("Fail2Ban requires pyinotify >= 0.8.3")
 
39
 
 
40
# Verify that pyinotify is functional on this system
 
41
# Even though imports -- might be dysfunctional, e.g. as on kfreebsd
 
42
try:
 
43
        manager = pyinotify.WatchManager()
 
44
        del manager
 
45
except Exception, e:
 
46
        raise ImportError("Pyinotify is probably not functional on this system: %s"
 
47
                                          % str(e))
33
48
 
34
49
# Gets the instance of the logger.
35
50
logSys = logging.getLogger("fail2ban.filter")
57
72
                logSys.debug("Created FilterPyinotify")
58
73
 
59
74
 
60
 
        def callback(self, event):
 
75
        def callback(self, event, origin=''):
 
76
                logSys.debug("%sCallback for Event: %s", origin, event)
61
77
                path = event.pathname
62
 
                if event.mask == pyinotify.IN_CREATE:
 
78
                if event.mask & ( pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO ):
 
79
                        # skip directories altogether
 
80
                        if event.mask & pyinotify.IN_ISDIR:
 
81
                                logSys.debug("Ignoring creation of directory %s", path)
 
82
                                return
63
83
                        # check if that is a file we care about
64
84
                        if not path in self.__watches:
65
 
                                logSys.debug("Ignoring creation of %s we do not monitor" % path)
 
85
                                logSys.debug("Ignoring creation of %s we do not monitor", path)
66
86
                                return
67
87
                        else:
68
88
                                # we need to substitute the watcher with a new one, so first
94
114
        def _addFileWatcher(self, path):
95
115
                wd = self.__monitor.add_watch(path, pyinotify.IN_MODIFY)
96
116
                self.__watches.update(wd)
97
 
                logSys.debug("Added file watcher for %s" % path)
98
 
                # process the file since we did get even 
 
117
                logSys.debug("Added file watcher for %s", path)
 
118
                # process the file since we did get even
99
119
                self._process_file(path)
100
120
 
101
121
 
104
124
                wd = self.__monitor.rm_watch(wdInt)
105
125
                if wd[wdInt]:
106
126
                        del self.__watches[path]
107
 
                        logSys.debug("Removed file watcher for %s" % path)
 
127
                        logSys.debug("Removed file watcher for %s", path)
108
128
                        return True
109
129
                else:
110
130
                        return False
119
139
                if not (path_dir in self.__watches):
120
140
                        # we need to watch also  the directory for IN_CREATE
121
141
                        self.__watches.update(
122
 
                                self.__monitor.add_watch(path_dir, pyinotify.IN_CREATE))
123
 
                        logSys.debug("Added monitor for the parent directory %s" % path_dir)
 
142
                                self.__monitor.add_watch(path_dir, pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO))
 
143
                        logSys.debug("Added monitor for the parent directory %s", path_dir)
124
144
 
125
145
                self._addFileWatcher(path)
126
146
 
141
161
                        # since there is no other monitored file under this directory
142
162
                        wdInt = self.__watches.pop(path_dir)
143
163
                        _ = self.__monitor.rm_watch(wdInt)
144
 
                        logSys.debug("Removed monitor for the parent directory %s" % path_dir)
 
164
                        logSys.debug("Removed monitor for the parent directory %s", path_dir)
145
165
 
146
166
 
147
167
        ##
155
175
                self.__notifier = pyinotify.ThreadedNotifier(self.__monitor,
156
176
                        ProcessPyinotify(self))
157
177
                self.__notifier.start()
158
 
                logSys.debug("pyinotifier started for %s." % self.jail.getName())
 
178
                logSys.debug("pyinotifier started for %s.", self.jail.getName())
159
179
                # TODO: verify that there is nothing really to be done for
160
180
                #       idle jails
161
181
                return True
191
211
 
192
212
        # just need default, since using mask on watch to limit events
193
213
        def process_default(self, event):
194
 
                logSys.debug("Callback for Event: %s" % event)
195
 
                self.__FileFilter.callback(event)
 
214
                self.__FileFilter.callback(event, origin='Default ')