1 From: Marco Bertorello <marco@bertorello.ns0.it>
2 Date: Thu, 14 Apr 2011 00:11:35 +0200
3 Subject: 08_foreground_mode
5 Add a useful switch to denyhost for run in foreground/debugging mode
7 DenyHosts/deny_hosts.py | 9 +++++++--
8 denyhosts.py | 20 +++++++++++++-------
9 2 files changed, 20 insertions(+), 9 deletions(-)
11 diff --git a/DenyHosts/deny_hosts.py b/DenyHosts/deny_hosts.py
12 index 7a985e6..f308264 100644
13 --- a/DenyHosts/deny_hosts.py
14 +++ b/DenyHosts/deny_hosts.py
15 @@ -41,7 +41,7 @@ error = logging.getLogger("denyhosts").error
17 def __init__(self, logfile, prefs, lock_file,
18 ignore_offset=0, first_time=0,
19 - noemail=0, daemon=0):
20 + noemail=0, daemon=0, foreground=0):
21 self.__denied_hosts = {}
23 self.__lock_file = lock_file
24 @@ -49,6 +49,7 @@ class DenyHosts:
25 self.__noemail = noemail
26 self.__report = Report(prefs.get("HOSTNAME_LOOKUP"), is_true(prefs['SYSLOG_REPORT']))
27 self.__daemon = daemon
28 + self.__foreground = foreground
29 self.__sync_server = prefs.get('SYNC_SERVER')
30 self.__sync_upload = is_true(prefs.get("SYNC_UPLOAD"))
31 self.__sync_download = is_true(prefs.get("SYNC_DOWNLOAD"))
32 @@ -87,7 +88,7 @@ class DenyHosts:
33 info("Log file size has not changed. Nothing to do.")
37 + if daemon and not foreground:
38 info("launching DenyHosts daemon (version %s)..." % VERSION)
39 #logging.getLogger().setLevel(logging.WARN)
41 @@ -101,6 +102,10 @@ class DenyHosts:
42 self.runDaemon(logfile, last_offset)
44 die("Error creating daemon: %s (%d)" % (retCode[1], retCode[0]))
46 + info("launching DenyHosts (version %s)..." % VERSION)
47 + self.__lock_file.remove()
48 + self.runDaemon(logfile, last_offset)
51 def killDaemon(self, signum, frame):
52 diff --git a/denyhosts.py b/denyhosts.py
53 index 48bd837..727e565 100755
56 @@ -34,6 +34,7 @@ def usage():
57 print " --migrate: migrate your HOSTS_DENY file so that it is suitable for --purge"
58 print " --purge: expire entries older than your PURGE_DENY setting"
59 print " --daemon: run DenyHosts in daemon mode"
60 + print " --foreground: run DenyHosts in foreground mode"
61 print " --sync: run DenyHosts synchronization mode"
62 print " --version: Prints the version of DenyHosts and exits"
64 @@ -63,6 +64,7 @@ if __name__ == '__main__':
72 @@ -70,8 +72,8 @@ if __name__ == '__main__':
73 (opts, getopts) = getopt.getopt(args, 'f:c:dinuvps?hV',
74 ["file=", "ignore", "verbose", "debug",
75 "help", "noemail", "config=", "version",
76 - "migrate", "purge", "daemon", "sync",
78 + "migrate", "purge", "daemon", "foreground",
79 + "sync", "upgrade099"])
81 print "\nInvalid command line option detected."
83 @@ -101,6 +103,8 @@ if __name__ == '__main__':
87 + if opt == '--foreground':
89 if opt == '--upgrade099':
91 if opt == '--version':
92 @@ -131,21 +135,21 @@ if __name__ == '__main__':
96 - if upgrade099 and not daemon:
97 + if upgrade099 and not (daemon or foreground):
98 if not prefs.get('PURGE_DENY'):
100 die("You have supplied the --upgrade099 flag, however you have not set PURGE_DENY in your configuration file")
102 u = UpgradeTo099(prefs.get("HOSTS_DENY"))
104 - if migrate and not daemon:
105 + if migrate and not (daemon or foreground):
106 if not prefs.get('PURGE_DENY'):
108 die("You have supplied the --migrate flag however you have not set PURGE_DENY in your configuration file.")
110 m = Migrate(prefs.get("HOSTS_DENY"))
112 - if purge and not daemon:
113 + if purge and not (daemon or foreground):
114 purge_time = prefs.get('PURGE_DENY')
117 @@ -162,7 +166,9 @@ if __name__ == '__main__':
120 dh = DenyHosts(f, prefs, lock_file, ignore_offset,
121 - first_time, noemail, daemon)
122 + first_time, noemail, daemon, foreground)
123 + except KeyboardInterrupt:
125 except SystemExit, e:
128 @@ -170,7 +176,7 @@ if __name__ == '__main__':
129 print "\nDenyHosts exited abnormally"
132 - if sync_mode and not daemon:
133 + if sync_mode and not (daemon or foreground):
134 if not prefs.get('SYNC_SERVER'):
136 die("You have provided the --sync flag however your configuration file is missing a value for SYNC_SERVER.")