From c1f5c4d1898a506029bdb381b62192f77ecf6835 Mon Sep 17 00:00:00 2001 From: Cyril Jaquier Date: Sun, 20 May 2007 21:46:23 +0000 Subject: [PATCH] - Removed Python 2.4 code. Need more testing git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@583 a942ae1a-1317-0410-a47c-b1dcaea8d605 --- CHANGELOG | 6 +++++- client/configreader.py | 6 ++++-- client/configurator.py | 6 ++++-- client/csocket.py | 3 ++- client/jailreader.py | 3 ++- fail2ban-client | 11 +++++++++-- fail2ban-regex | 9 ++++++--- server/action.py | 6 ++++-- server/banmanager.py | 3 ++- server/datedetector.py | 5 ++--- server/datestrptime.py | 3 ++- server/filter.py | 27 ++++++++++++++++++--------- server/mytime.py | 9 ++++++--- server/ssocket.py | 6 ++++-- testcases/actiontestcase.py | 2 +- testcases/banmanagertestcase.py | 8 ++++---- testcases/filtertestcase.py | 10 +++++----- 17 files changed, 80 insertions(+), 43 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 94a7b411..92b9e0d9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,9 +4,13 @@ |_| \__,_|_|_/___|_.__/\__,_|_||_| ============================================================= -Fail2Ban (version 0.8.0) 2007/05/03 +Fail2Ban (version 0.9.0) 2007/??/?? ============================================================= +ver. 0.9.0 (2007/??/??) - alpha +---------- +- Removed Python 2.4 code. Need more testing + ver. 0.8.0 (2007/05/03) - stable ---------- - Fixed RedHat init script. Thanks to Jonathan Underwood diff --git a/client/configreader.py b/client/configreader.py index 1185bb65..46b3c5dd 100644 --- a/client/configreader.py +++ b/client/configreader.py @@ -39,14 +39,16 @@ class ConfigReader(SafeConfigParser): SafeConfigParser.__init__(self) self.__opts = None - @staticmethod + #@staticmethod def setBaseDir(folderName): path = folderName.rstrip('/') ConfigReader.BASE_DIRECTORY = path + '/' + setBaseDir = staticmethod(setBaseDir) - @staticmethod + #@staticmethod def getBaseDir(): return ConfigReader.BASE_DIRECTORY + getBaseDir = staticmethod(getBaseDir) def read(self, filename): basename = ConfigReader.BASE_DIRECTORY + filename diff --git a/client/configurator.py b/client/configurator.py index 8181a889..80ff968f 100644 --- a/client/configurator.py +++ b/client/configurator.py @@ -40,13 +40,15 @@ class Configurator: self.__fail2ban = Fail2banReader() self.__jails = JailsReader() - @staticmethod + #@staticmethod def setBaseDir(folderName): ConfigReader.setBaseDir(folderName) + setBaseDir = staticmethod(setBaseDir) - @staticmethod + #@staticmethod def getBaseDir(): return ConfigReader.getBaseDir() + getBaseDir = staticmethod(getBaseDir) def readEarly(self): self.__fail2ban.read() diff --git a/client/csocket.py b/client/csocket.py index 34d0844d..56f6fc20 100644 --- a/client/csocket.py +++ b/client/csocket.py @@ -47,7 +47,7 @@ class CSocket: self.__csock.close() return ret - @staticmethod + #@staticmethod def receive(sock): msg = '' while msg.rfind(CSocket.END_STRING) == -1: @@ -56,3 +56,4 @@ class CSocket: raise RuntimeError, "socket connection broken" msg = msg + chunk return loads(msg) + receive = staticmethod(receive) diff --git a/client/jailreader.py b/client/jailreader.py index 3588daab..58e87381 100644 --- a/client/jailreader.py +++ b/client/jailreader.py @@ -129,7 +129,7 @@ class JailReader(ConfigReader): stream.insert(0, ["add", self.__name, backend]) return stream - @staticmethod + #@staticmethod def splitAction(action): m = JailReader.actionCRE.match(action) d = dict() @@ -165,3 +165,4 @@ class JailReader(ConfigReader): except IndexError: logSys.error("Invalid argument %s in '%s'" % (p, m.group(2))) return [m.group(1), d] + splitAction = staticmethod(splitAction) diff --git a/fail2ban-client b/fail2ban-client index 32064682..2920bfc0 100755 --- a/fail2ban-client +++ b/fail2ban-client @@ -26,7 +26,8 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" import sys, string, os, pickle, re, logging, signal -import getopt, time, readline, shlex, socket +import getopt, time, shlex, socket +#import readline # Inserts our own modules path first in the list # fix for bug #343821 @@ -325,6 +326,11 @@ class Fail2banClient: # Interactive mode if self.__conf["interactive"]: + try: + import readline + except ImportError: + logSys.error("Readline not available") + return False try: ret = True if len(args) > 0: @@ -357,11 +363,12 @@ class Fail2banClient: self.__stream = self.__configurator.getConfigStream() return ret - @staticmethod + #@staticmethod def dumpConfig(cmd): for c in cmd: print c return True + dumpConfig = staticmethod(dumpConfig) class ServerExecutionException(Exception): diff --git a/fail2ban-regex b/fail2ban-regex index 28ea4b43..898c0e63 100755 --- a/fail2ban-regex +++ b/fail2ban-regex @@ -79,7 +79,7 @@ class Fail2banRegex: logging.getLogger("fail2ban").addHandler(self.__hdlr) logging.getLogger("fail2ban").setLevel(logging.ERROR) - @staticmethod + #@staticmethod def dispVersion(): print "Fail2Ban v" + version print @@ -89,8 +89,9 @@ class Fail2banRegex: print print "Written by Cyril Jaquier ." print "Many contributions by Yaroslav O. Halchenko ." + dispVersion = staticmethod(dispVersion) - @staticmethod + #@staticmethod def dispUsage(): print "Usage: "+sys.argv[0]+" [OPTIONS] " print @@ -112,6 +113,7 @@ class Fail2banRegex: print " filename path to a filter file (filter.d/sshd.conf)" print print "Report bugs to " + dispUsage = staticmethod(dispUsage) def getCmdLineOptions(self, optList): """ Gets the command line options @@ -124,9 +126,10 @@ class Fail2banRegex: self.dispVersion() sys.exit(0) - @staticmethod + #@staticmethod def logIsFile(value): return os.path.isfile(value) + logIsFile = staticmethod(logIsFile) def readRegex(self, value): if os.path.isfile(value): diff --git a/server/action.py b/server/action.py index 10dfd479..78d0b231 100644 --- a/server/action.py +++ b/server/action.py @@ -231,7 +231,7 @@ class Action: # @param aInfo the properties # @return a string - @staticmethod + #@staticmethod def replaceTag(query, aInfo): """ Replace tags in query """ @@ -241,6 +241,7 @@ class Action: # New line string = string.replace("
", '\n') return string + replaceTag = staticmethod(replaceTag) ## # Executes a command with preliminary checks and substitutions. @@ -297,7 +298,7 @@ class Action: # @param realCmd the command to execute # @return True if the command succeeded - @staticmethod + #@staticmethod def executeCmd(realCmd): logSys.debug(realCmd) try: @@ -312,3 +313,4 @@ class Action: except OSError, e: logSys.error("%s failed with %s" % (realCmd, e)) return False + executeCmd = staticmethod(executeCmd) diff --git a/server/banmanager.py b/server/banmanager.py index 254f4c61..8e068f6e 100644 --- a/server/banmanager.py +++ b/server/banmanager.py @@ -125,7 +125,7 @@ class BanManager: # @param ticket the FailTicket # @return a BanTicket - @staticmethod + #@staticmethod def createBanTicket(ticket): ip = ticket.getIP() #lastTime = ticket.getTime() @@ -133,6 +133,7 @@ class BanManager: banTicket = BanTicket(ip, lastTime) banTicket.setAttempt(ticket.getAttempt()) return banTicket + createBanTicket = staticmethod(createBanTicket) ## # Add a ban ticket. diff --git a/server/datedetector.py b/server/datedetector.py index 6b9986f5..c133e113 100644 --- a/server/datedetector.py +++ b/server/datedetector.py @@ -158,8 +158,7 @@ class DateDetector: try: self.__lock.acquire() logSys.debug("Sorting the template list") - self.__templates.sort(cmp = lambda x, y: - cmp(x.getHits(), y.getHits()), - reverse = True) + self.__templates.sort(lambda x, y: cmp(x.getHits(), y.getHits())) + self.__templates.reverse() finally: self.__lock.release() diff --git a/server/datestrptime.py b/server/datestrptime.py index bd2d351a..4fec591a 100644 --- a/server/datestrptime.py +++ b/server/datestrptime.py @@ -54,13 +54,14 @@ class DateStrptime(DateTemplate): def __init__(self): DateTemplate.__init__(self) - @staticmethod + #@staticmethod def convertLocale(date): for t in DateStrptime.TABLE: for m in DateStrptime.TABLE[t]: if date.find(m) >= 0: return date.replace(m, t) return date + convertLocale = staticmethod(convertLocale) def getDate(self, line): date = None diff --git a/server/filter.py b/server/filter.py index 131c6d02..52d2642a 100644 --- a/server/filter.py +++ b/server/filter.py @@ -477,10 +477,11 @@ import socket, struct class DNSUtils: - DNS_CRE = re.compile("(?:(?:\w|-)+\.){2,}\w+") + #DNS_CRE = re.compile("(?:(?:\w|-)+\.){2,}\w+") + DNS_CRE = re.compile("\S+") IP_CRE = re.compile("(?:\d{1,3}\.){3}\d{1,3}") - @staticmethod + #@staticmethod def dnsToIp(dns): """ Convert a DNS into an IP address using the Python socket module. Thanks to Kevin Drapel. @@ -491,8 +492,9 @@ class DNSUtils: logSys.warn("Unable to find a corresponding IP address for %s" % dns) return list() + dnsToIp = staticmethod(dnsToIp) - @staticmethod + #@staticmethod def textToDns(text): """ Search for possible DNS in an arbitrary text. Thanks to Tom Pike. @@ -502,8 +504,9 @@ class DNSUtils: return match else: return None + textToDns = staticmethod(textToDns) - @staticmethod + #@staticmethod def searchIP(text): """ Search if an IP address if directly available and return it. @@ -513,8 +516,9 @@ class DNSUtils: return match else: return None + searchIP = staticmethod(searchIP) - @staticmethod + #@staticmethod def isValidIP(string): """ Return true if str is a valid IP """ @@ -524,8 +528,9 @@ class DNSUtils: return True except socket.error: return False + isValidIP = staticmethod(isValidIP) - @staticmethod + #@staticmethod def textToIp(text): """ Return the IP of DNS found in a given text. """ @@ -544,8 +549,9 @@ class DNSUtils: for e in ip: ipList.append(e) return ipList + textToIp = staticmethod(textToIp) - @staticmethod + #@staticmethod def cidr(i, n): """ Convert an IP address string with a CIDR mask into a 32-bit integer. @@ -553,15 +559,18 @@ class DNSUtils: # 32-bit IPv4 address mask MASK = 0xFFFFFFFFL return ~(MASK >> n) & MASK & DNSUtils.addr2bin(i) + cidr = staticmethod(cidr) - @staticmethod + #@staticmethod def addr2bin(string): """ Convert a string IPv4 address into an unsigned integer. """ return struct.unpack("!L", socket.inet_aton(string))[0] + addr2bin = staticmethod(addr2bin) - @staticmethod + #@staticmethod def bin2addr(addr): """ Convert a numeric IPv4 address into string n.n.n.n form. """ return socket.inet_ntoa(struct.pack("!L", addr)) + bin2addr = staticmethod(bin2addr) diff --git a/server/mytime.py b/server/mytime.py index 2b7dfd0a..b6762c4f 100644 --- a/server/mytime.py +++ b/server/mytime.py @@ -46,31 +46,34 @@ class MyTime: # # @param t the time to set or None - @staticmethod + #@staticmethod def setTime(t): MyTime.myTime = t + setTime = staticmethod(setTime) ## # Equivalent to time.time() # # @return time.time() if setTime was called with None - @staticmethod + #@staticmethod def time(): if MyTime.myTime == None: return time.time() else: return MyTime.myTime + time = staticmethod(time) ## # Equivalent to time.gmtime() # # @return time.gmtime() if setTime was called with None - @staticmethod + #@staticmethod def gmtime(): if MyTime.myTime == None: return time.gmtime() else: return time.gmtime(MyTime.myTime) + gmtime = staticmethod(gmtime) \ No newline at end of file diff --git a/server/ssocket.py b/server/ssocket.py index f56f7199..27552153 100644 --- a/server/ssocket.py +++ b/server/ssocket.py @@ -116,12 +116,13 @@ class SocketWorker(Thread): self.__csock.close() logSys.debug("Connection closed") - @staticmethod + #@staticmethod def __send(sock, msg): obj = dumps(msg, HIGHEST_PROTOCOL) sock.send(obj + SSocket.END_STRING) + __send = staticmethod(__send) - @staticmethod + #@staticmethod def __receive(sock): msg = '' while msg.rfind(SSocket.END_STRING) == -1: @@ -130,6 +131,7 @@ class SocketWorker(Thread): raise RuntimeError, "socket connection broken" msg = msg + chunk return loads(msg) + __receive = staticmethod(__receive) class SSocketErrorException(Exception): diff --git a/testcases/actiontestcase.py b/testcases/actiontestcase.py index bc6f39ad..908726ba 100644 --- a/testcases/actiontestcase.py +++ b/testcases/actiontestcase.py @@ -43,5 +43,5 @@ class ExecuteAction(unittest.TestCase): self.__action.setActionBan("echo -n") self.__action.setActionCheck("[ -e /tmp/fail2ban.test ]") - self.assertTrue(self.__action.execActionBan(None)) + self.failUnless(self.__action.execActionBan(None)) \ No newline at end of file diff --git a/testcases/banmanagertestcase.py b/testcases/banmanagertestcase.py index fee58631..cb266ce0 100644 --- a/testcases/banmanagertestcase.py +++ b/testcases/banmanagertestcase.py @@ -34,7 +34,7 @@ class AddFailure(unittest.TestCase): """Call before every test case.""" self.__ticket = BanTicket('193.168.0.128', 1167605999.0) self.__banManager = BanManager() - self.assertTrue(self.__banManager.addBanTicket(self.__ticket)) + self.failUnless(self.__banManager.addBanTicket(self.__ticket)) def tearDown(self): """Call after every test case.""" @@ -43,14 +43,14 @@ class AddFailure(unittest.TestCase): self.assertEqual(self.__banManager.size(), 1) def testAddDuplicate(self): - self.assertFalse(self.__banManager.addBanTicket(self.__ticket)) + self.failIf(self.__banManager.addBanTicket(self.__ticket)) self.assertEqual(self.__banManager.size(), 1) def _testInListOK(self): ticket = BanTicket('193.168.0.128', 1167605999.0) - self.assertTrue(self.__banManager.inBanList(ticket)) + self.failUnless(self.__banManager.inBanList(ticket)) def _testInListNOK(self): ticket = BanTicket('111.111.1.111', 1167605999.0) - self.assertFalse(self.__banManager.inBanList(ticket)) + self.failIf(self.__banManager.inBanList(ticket)) \ No newline at end of file diff --git a/testcases/filtertestcase.py b/testcases/filtertestcase.py index 520b1ef3..a69633df 100644 --- a/testcases/filtertestcase.py +++ b/testcases/filtertestcase.py @@ -43,19 +43,19 @@ class IgnoreIP(unittest.TestCase): ipList = "127.0.0.1", "192.168.0.1", "255.255.255.255", "99.99.99.99" for ip in ipList: self.__filter.addIgnoreIP(ip) - self.assertTrue(self.__filter.inIgnoreIPList(ip)) + self.failUnless(self.__filter.inIgnoreIPList(ip)) # Test DNS self.__filter.addIgnoreIP("www.epfl.ch") - self.assertTrue(self.__filter.inIgnoreIPList("128.178.50.12")) + self.failUnless(self.__filter.inIgnoreIPList("128.178.50.12")) def testIgnoreIPNOK(self): ipList = "", "999.999.999.999", "abcdef", "192.168.0." for ip in ipList: self.__filter.addIgnoreIP(ip) - self.assertFalse(self.__filter.inIgnoreIPList(ip)) + self.failIf(self.__filter.inIgnoreIPList(ip)) # Test DNS self.__filter.addIgnoreIP("www.epfl.ch") - self.assertFalse(self.__filter.inIgnoreIPList("127.177.50.10")) + self.failIf(self.__filter.inIgnoreIPList("127.177.50.10")) class LogFile(unittest.TestCase): @@ -74,7 +74,7 @@ class LogFile(unittest.TestCase): # self.__filter.openLogFile(LogFile.FILENAME) def testIsModified(self): - self.assertTrue(self.__filter.isModified(LogFile.FILENAME)) + self.failUnless(self.__filter.isModified(LogFile.FILENAME)) class GetFailures(unittest.TestCase):