- Fixed some Pylint warnings/errors

git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@433 a942ae1a-1317-0410-a47c-b1dcaea8d605
0.x
Cyril Jaquier 2006-10-24 19:40:51 +00:00
parent 4fd934aa83
commit 71b9fe8fe8
19 changed files with 67 additions and 68 deletions

View File

@ -22,4 +22,4 @@ __author__ = "Cyril Jaquier"
__version__ = "$Revision$"
__date__ = "$Date$"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
__license__ = "GPL"

View File

@ -38,8 +38,8 @@ class ActionReader(ConfigReader):
self.__cInfo = action[1]
self.__name = name
def setFile(self, file):
self.__file = file
def setFile(self, fileName):
self.__file = fileName
def getFile(self):
return self.__file

View File

@ -25,33 +25,31 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
import logging, os
from ConfigParser import *
from ConfigParser import SafeConfigParser
from ConfigParser import NoOptionError, NoSectionError
# Gets the instance of the logger.
logSys = logging.getLogger("fail2ban.client.config")
class ConfigReader(SafeConfigParser):
basedir = "/etc/fail2ban/"
BASE_DIRECTORY = "/etc/fail2ban/"
def __init__(self):
SafeConfigParser.__init__(self)
self.__opts = None
@staticmethod
def setBaseDir(dir):
global basedir
path = dir.rstrip('/')
basedir = path + '/'
def setBaseDir(folderName):
path = folderName.rstrip('/')
ConfigReader.BASE_DIRECTORY = path + '/'
@staticmethod
def getBaseDir():
global basedir
return basedir
return ConfigReader.BASE_DIRECTORY
def read(self, filename):
global basedir
basename = basedir + filename
basename = ConfigReader.BASE_DIRECTORY + filename
logSys.debug("Reading " + basename)
bConf = basename + ".conf"
bLocal = basename + ".local"

View File

@ -40,10 +40,12 @@ class Configurator:
self.__fail2ban = Fail2banReader()
self.__jails = JailsReader()
def setBaseDir(self, dir):
ConfigReader.setBaseDir(dir)
@staticmethod
def setBaseDir(folderName):
ConfigReader.setBaseDir(folderName)
def getBaseDir(self):
@staticmethod
def getBaseDir():
return ConfigReader.getBaseDir()
def readEarly(self):
@ -57,8 +59,8 @@ class Configurator:
return self.__fail2ban.getEarlyOptions()
def getAllOptions(self):
self.__settings["general"] = self.__fail2ban.getOptions()
self.__settings["jails"] = self.__jails.getOptions()
self.__fail2ban.getOptions()
self.__jails.getOptions()
def convertToProtocol(self):
self.__streams["general"] = self.__fail2ban.convert()

View File

@ -46,10 +46,11 @@ class CSocket:
self.__csock.close()
return ret
def receive(self, socket):
@staticmethod
def receive(sock):
msg = ''
while msg.rfind(CSocket.END_STRING) == -1:
chunk = socket.recv(6)
chunk = sock.recv(6)
if chunk == '':
raise RuntimeError, "socket connection broken"
msg = msg + chunk

View File

@ -32,13 +32,13 @@ logSys = logging.getLogger("fail2ban.client.config")
class FilterReader(ConfigReader):
def __init__(self, file, name):
def __init__(self, fileName, name):
ConfigReader.__init__(self)
self.__file = file
self.__file = fileName
self.__name = name
def setFile(self, file):
self.__file = file
def setFile(self, fileName):
self.__file = fileName
def getFile(self):
return self.__file

View File

@ -125,7 +125,7 @@ class JailReader(ConfigReader):
def splitAction(action):
m = JailReader.actionCRE.match(action)
d = dict()
if m.group(2) <> None:
if not m.group(2) == None:
for param in m.group(2).split(','):
p = param.split('=')
d[p[0].strip()] = p[1].strip()

View File

@ -55,11 +55,6 @@ class JailsReader(ConfigReader):
else:
logSys.error("Errors in jail '" + sec + "'. Skipping...")
def getFilterOptions(self, file):
filter = FilterReader(file)
filter.read()
return filter.getOptions()
def convert(self):
stream = list()
for opt in self.__opts:

View File

@ -22,4 +22,4 @@ __author__ = "Cyril Jaquier"
__version__ = "$Revision$"
__date__ = "$Date$"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL"
__license__ = "GPL"

View File

@ -108,7 +108,7 @@ class Action:
return self.__actionBan
def execActionBan(self, aInfo):
return self.__processCmd(self.__actionBan, aInfo);
return self.__processCmd(self.__actionBan, aInfo)
##
# Set the "unban" command.
@ -128,7 +128,7 @@ class Action:
return self.__actionUnban
def execActionUnban(self, aInfo):
return self.__processCmd(self.__actionUnban, aInfo);
return self.__processCmd(self.__actionUnban, aInfo)
##
# Set the "check" command.
@ -207,7 +207,7 @@ class Action:
# Replace static fields
realCmd = Action.replaceTag(realCmd, self.__cInfo)
return Action.executeCmd(realCmd)
return Action.executeCmd(realCmd)
@staticmethod
def executeCmd(realCmd):

View File

@ -49,7 +49,7 @@ class Actions(JailThread):
# @param jail the jail object
def __init__(self, jail):
JailThread.__init__(self, jail)
JailThread.__init__(self)
## The jail which contains this action.
self.jail = jail
self.__actions = list()

View File

@ -27,7 +27,7 @@ __license__ = "GPL"
from banticket import BanTicket
from threading import Lock
from mytime import MyTime
import time, logging
import logging
# Gets the instance of the logger.
logSys = logging.getLogger("fail2ban.action")

View File

@ -92,7 +92,7 @@ class DateDetector:
self.__lock.acquire()
for template in self.__templates:
match = template.matchDate(line)
if match <> None:
if not match == None:
self.__lock.release()
return match
self.__lock.release()

View File

@ -51,7 +51,7 @@ class Filter(JailThread):
# @param jail the jail object
def __init__(self, jail):
JailThread.__init__(self, jail)
JailThread.__init__(self)
## The jail which contains this filter.
self.jail = jail
## The failures manager.
@ -347,7 +347,6 @@ class Filter(JailThread):
# is created and is added to the FailManager.
def getFailures(self, filename):
ipList = dict()
ret = self.__openLogFile(filename)
if not ret:
logSys.error("Unable to get failures in " + filename)
@ -392,7 +391,7 @@ class Filter(JailThread):
match = self.__failRegexObj.search(line)
if match:
date = self.dateDetector.getUnixTime(match.string)
if date <> None:
if not date == None:
try:
ipMatch = DNSUtils.textToIp(match.group("host"))
if ipMatch:
@ -463,10 +462,10 @@ class DNSUtils:
return None
@staticmethod
def isValidIP(str):
def isValidIP(string):
""" Return true if str is a valid IP
"""
s = str.split('/', 1)
s = string.split('/', 1)
try:
socket.inet_aton(s[0])
return True
@ -503,10 +502,10 @@ class DNSUtils:
return ~(MASK >> n) & MASK & DNSUtils.addr2bin(i)
@staticmethod
def addr2bin(str):
def addr2bin(string):
""" Convert a string IPv4 address into an unsigned integer.
"""
return struct.unpack("!L", socket.inet_aton(str))[0]
return struct.unpack("!L", socket.inet_aton(string))[0]
@staticmethod
def bin2addr(addr):

View File

@ -95,9 +95,9 @@ class FilterPoll(Filter):
while self.isActive():
if not self.getIdle():
# Get file modification
for file in self.getLogPath():
if self.isModified(file):
self.getFailures(file)
for f in self.getLogPath():
if self.isModified(f):
self.getFailures(f)
self.modified = True
if self.modified:

View File

@ -54,6 +54,7 @@ class Jail:
self.__filter = FilterPoll(self)
def __initGamin(self):
# Try to import gamin
import gamin
logSys.info("Using Gamin")
from filtergamin import FilterGamin

View File

@ -38,7 +38,7 @@ class JailThread(Thread):
# Initialize the filter object with default values.
# @param jail the jail object
def __init__(self, jail):
def __init__(self):
Thread.__init__(self)
## Control the state of the thread.
self.__isRunning = False

View File

@ -34,7 +34,7 @@ import logging, logging.handlers, sys, os, signal
logSys = logging.getLogger("fail2ban.server")
class Server:
def __init__(self, daemon = False):
self.__jails = Jails()
self.__daemon = daemon
@ -111,11 +111,11 @@ class Server:
def getIgnoreIP(self, name):
return self.__jails.getFilter(name).getIgnoreIP()
def addLogPath(self, name, file):
self.__jails.getFilter(name).addLogPath(file)
def addLogPath(self, name, fileName):
self.__jails.getFilter(name).addLogPath(fileName)
def delLogPath(self, name, file):
self.__jails.getFilter(name).delLogPath(file)
def delLogPath(self, name, fileName):
self.__jails.getFilter(name).delLogPath(fileName)
def getLogPath(self, name):
return self.__jails.getFilter(name).getLogPath()
@ -356,7 +356,7 @@ class Server:
pass
# Redirect the standard file descriptors to /dev/null.
os.open("/dev/null", os.O_RDONLY) # standard input (0)
os.open("/dev/null", os.O_RDONLY) # standard input (0)
os.open("/dev/null", os.O_RDWR) # standard output (1)
os.open("/dev/null", os.O_RDWR) # standard error (2)
return True

View File

@ -40,6 +40,7 @@ class SSocket(Thread):
self.__transmit = transmitter
self.__isRunning = False
self.__socket = "/tmp/fail2ban.sock"
self.__ssock = None
logSys.debug("Created SSocket")
def initialize(self, sock = "/tmp/fail2ban.sock", force = False):
@ -53,31 +54,31 @@ class SSocket(Thread):
else:
raise SSocketErrorException("Server already running")
# Create an INET, STREAMing socket
#self.ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.ssock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
#self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#self.ssock.setblocking(False)
#self.__ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.__ssock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
#self.__ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#self.__ssock.setblocking(False)
# Do not use a blocking socket as there is problem at shutdown.
# Use a timeout instead. Daemon exits at most 'timeout' seconds
# after the command.
self.ssock.settimeout(1)
self.__ssock.settimeout(1)
# Bind the socket to a public host and a well-known port
#self.ssock.bind(("localhost", 2222))
self.ssock.bind(sock)
#self.__ssock.bind(("localhost", 2222))
self.__ssock.bind(sock)
# Become a server socket
self.ssock.listen(5)
self.__ssock.listen(5)
def run(self):
self.__isRunning = True
while self.__isRunning:
try:
(csock, address) = self.ssock.accept()
(csock, address) = self.__ssock.accept()
thread = SocketWorker(csock, self.__transmit)
thread.start()
except socket.timeout:
# Do nothing here
pass
self.ssock.close()
self.__ssock.close()
# Remove socket
if os.path.exists(self.__socket):
logSys.debug("Removed socket file " + self.__socket)
@ -110,14 +111,16 @@ class SocketWorker(Thread):
self.__csock.close()
logSys.debug("Connection closed")
def __send(self, socket, msg):
@staticmethod
def __send(sock, msg):
obj = dumps(msg)
socket.send(obj + SSocket.END_STRING)
sock.send(obj + SSocket.END_STRING)
def __receive(self, socket):
@staticmethod
def __receive(sock):
msg = ''
while msg.rfind(SSocket.END_STRING) == -1:
chunk = socket.recv(6)
chunk = sock.recv(6)
if chunk == '':
raise RuntimeError, "socket connection broken"
msg = msg + chunk