/usr/share/denyhosts/DenyHosts/report.py is in denyhosts 2.10-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | import logging
import re
import socket
from types import ListType, TupleType
from util import is_true
try:
import syslog
HAS_SYSLOG = True
except ImportError:
HAS_SYSLOG = False
debug = logging.getLogger("report").debug
warn = logging.getLogger("report").warn
IP_ADDR_REGEX = re.compile(r"""(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})""")
class Report:
def __init__(self, hostname_lookup, use_syslog=False):
self.report = ""
if use_syslog and not HAS_SYSLOG:
warn("syslog is unavailable on this platform")
self.use_syslog = use_syslog and HAS_SYSLOG
if self.use_syslog:
syslog.openlog("denyhosts")
self.hostname_lookup = is_true(hostname_lookup)
def empty(self):
if self.report: return 0
else: return 1
def clear(self):
self.report = ""
def get_report(self):
return self.report
def add_section(self, message, iterable):
self.report += "%s:\n\n" % message
for i in iterable:
if type(i) in (TupleType, ListType):
extra = ": %d\n" % i[1]
i = i[0]
else:
extra = ""
if self.hostname_lookup:
hostname = self.get_hostname(i)
debug("get_host: %s", hostname)
else: hostname = i
self.report += "%s%s\n" % (hostname, extra)
if self.use_syslog:
syslog.syslog("%s - %s%s" %(message, hostname, extra))
self.report += "\n" + "-" * 70 + "\n"
def get_hostname(self, text):
m = IP_ADDR_REGEX.search(text)
if m:
start = m.start()
ip = m.group('ip')
text = text[:start]
else:
return text
hostname = socket.getfqdn(ip)
if hostname == ip:
hostname = "unknown"
return "%s (%s)" % (ip, hostname)
|