Skip to content

Commit

Permalink
Version 0.9.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Schwartz authored and mruffalo committed Oct 7, 2009
1 parent 1ce8b4d commit 83c62a5
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 31 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.txt
@@ -1,5 +1,13 @@
DENYHOSTS CHANGELOG

0.9.2
=====

- Daemon configuration params DAEMON_SLEEP and DAEMON_PURGE can now be given in the same format accepted by
PURGE_DENY (eg. 1d, 5h, 1y, etc...). If no unit qualifier (eg. s, m, d, h, w, y) is given then 's' (seconds) is
assumed.


0.9.1
=====

Expand Down
3 changes: 2 additions & 1 deletion DenyHosts/constants.py
Expand Up @@ -26,7 +26,8 @@
CONFIG_FILE = "denyhosts.cfg"
TAB_OFFSET = 40
DENY_DELIMITER = "# DenyHosts:"
PURGE_TIME_LOOKUP = {'m': 60, # minute
TIME_SPEC_LOOKUP = {'s': 1, # s
'm': 60, # minute
'h': 3600, # hour
'd': 86400, # day
'w': 604800, # week
Expand Down
8 changes: 4 additions & 4 deletions DenyHosts/deny_hosts.py
Expand Up @@ -20,7 +20,7 @@
from regex import *
from daemon import createDaemon
from denyfileutil import Purge
from util import parse_host
from util import parse_host, calculate_seconds

debug = logging.getLogger("denyhosts").debug
info = logging.getLogger("denyhosts").info
Expand Down Expand Up @@ -110,10 +110,10 @@ def runDaemon(self, logfile, last_offset):

secure_log = self.__prefs.get('SECURE_LOG')
info("monitoring log: %s", secure_log)
daemon_sleep = self.__prefs.get('DAEMON_SLEEP')
purge_time = self.__prefs.get('PURGE_DENY')
daemon_sleep = calculate_seconds(self.__prefs.get('DAEMON_SLEEP'))
purge_time = calculate_seconds(self.__prefs.get('PURGE_DENY'))
if purge_time:
daemon_purge = self.__prefs.get('DAEMON_PURGE')
daemon_purge = calculate_seconds(self.__prefs.get('DAEMON_PURGE'))
daemon_purge = max(daemon_sleep, daemon_purge)
purge_sleep_ratio = daemon_purge / daemon_sleep
info("daemon_purge: %ld", daemon_purge)
Expand Down
20 changes: 3 additions & 17 deletions DenyHosts/denyfileutil.py
Expand Up @@ -3,10 +3,9 @@
import time
import logging

from constants import TAB_OFFSET, PURGE_TIME_LOOKUP, DENY_DELIMITER
from regex import PURGE_TIME_REGEX
from constants import TAB_OFFSET, DENY_DELIMITER
from loginattempt import AbusiveHosts
from util import parse_host
from util import parse_host, calculate_seconds

debug = logging.getLogger("denyfileutil").debug
info = logging.getLogger("denyfileutil").info
Expand Down Expand Up @@ -89,7 +88,7 @@ def create_temp(self, data):
class Purge(DenyFileUtilBase):
def __init__(self, deny_file, purge_timestr, work_dir):
DenyFileUtilBase.__init__(self, deny_file, "purge")
cutoff = self.calculate(purge_timestr)
cutoff = calculate_seconds(purge_timestr)

self.cutoff = long(time.time()) - cutoff
debug("relative cutoff: %ld (seconds)", cutoff)
Expand All @@ -111,19 +110,6 @@ def __init__(self, deny_file, purge_timestr, work_dir):

info("num entries purged: %d", num_purged)

def calculate(self, timestr):
m = PURGE_TIME_REGEX.search(timestr)
if not m:
raise Exception, "Invalid PURGE_TIME specification: string format"

units = int(m.group('units'))
period = m.group('period')

if units == 0:
raise Exception, "Invalid PURGE_TIME specification: units = 0"
# anything older than cutoff will get removed
return units * PURGE_TIME_LOOKUP[period]


def create_temp(self, data):
purged_hosts = []
Expand Down
8 changes: 3 additions & 5 deletions DenyHosts/prefs.py
Expand Up @@ -12,8 +12,8 @@ def __init__(self, path=None):
'SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS': 'yes',
'HOSTNAME_LOOKUP': 'yes',
'DAEMON_LOG': '/var/log/denyhosts',
'DAEMON_SLEEP': 30,
'DAEMON_PURGE': 3600}
'DAEMON_SLEEP': '30s',
'DAEMON_PURGE': '1h'}

# reqd[0]: required field name
# reqd[1]: is value required? (False = value can be blank)
Expand All @@ -34,9 +34,7 @@ def __init__(self, path=None):
'HOSTS_DENY',
'DAEMON_LOG')

self.to_int = ('DENY_THRESHOLD',
'DAEMON_PURGE',
'DAEMON_SLEEP')
self.to_int = ('DENY_THRESHOLD', )

if path: self.load_settings(path)

Expand Down
2 changes: 1 addition & 1 deletion DenyHosts/regex.py
Expand Up @@ -19,7 +19,7 @@

SUCCESSFUL_ENTRY_REGEX = re.compile(r"""Accepted (?P<method>.*) for (?P<user>.*?) from (::ffff:)?(?P<host>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})""")

PURGE_TIME_REGEX = re.compile(r"""(?P<units>\d*)\s*(?P<period>[mhdwy])""")
TIME_SPEC_REGEX = re.compile(r"""(?P<units>\d*)\s*(?P<period>[smhdwy])?""")

ALLOWED_REGEX = re.compile(r"""(?P<first_3bits>\d{1,3}\.\d{1,3}\.\d{1,3}\.)((?P<fourth>\d{1,3})|(?P<ip_wildcard>\*)|\[(?P<ip_range>\d{1,3}\-\d{1,3})\])""")

Expand Down
23 changes: 22 additions & 1 deletion DenyHosts/util.py
Expand Up @@ -3,7 +3,8 @@
import time
from smtplib import SMTP
import logging
from constants import BSD_STYLE
from constants import BSD_STYLE, TIME_SPEC_LOOKUP
from regex import TIME_SPEC_REGEX

debug = logging.getLogger("util").debug

Expand All @@ -23,6 +24,26 @@ def is_true(s):
def is_false(s):
return not is_true(s)


def calculate_seconds(timestr, zero_ok=False):
# return the number of seconds in a given timestr such as 1d (1 day),
# 13w (13 weeks), 5s (5seconds), etc...

m = TIME_SPEC_REGEX.search(timestr)
if not m:
raise Exception, "Invalid time specification: string format error: %s", timestr

units = int(m.group('units'))
period = m.group('period') or 's' # seconds is the default

if units == 0 and not zero_ok:
raise Exception, "Invalid time specification: units = 0"

seconds = units * TIME_SPEC_LOOKUP[period]
debug("converted %s to %ld seconds: ", timestr, seconds)
return seconds


def parse_host(line):
# parses a line from /etc/hosts.deny
# returns the ip address
Expand Down
2 changes: 1 addition & 1 deletion DenyHosts/version.py
@@ -1 +1 @@
VERSION="0.9.1"
VERSION="0.9.2"
2 changes: 1 addition & 1 deletion PKG-INFO
@@ -1,6 +1,6 @@
Metadata-Version: 1.0
Name: DenyHosts
Version: 0.9.1
Version: 0.9.2
Summary: DenyHosts is a utility to help sys admins thwart ssh hackers
Home-page: http://denyhosts.sourceforge.net
Author: Phil Schwartz
Expand Down

0 comments on commit 83c62a5

Please sign in to comment.