mirror of https://github.com/fail2ban/fail2ban
- Fixed some Pylint warnings/errors
git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@433 a942ae1a-1317-0410-a47c-b1dcaea8d6050.x
parent
4fd934aa83
commit
71b9fe8fe8
|
@ -38,8 +38,8 @@ class ActionReader(ConfigReader):
|
||||||
self.__cInfo = action[1]
|
self.__cInfo = action[1]
|
||||||
self.__name = name
|
self.__name = name
|
||||||
|
|
||||||
def setFile(self, file):
|
def setFile(self, fileName):
|
||||||
self.__file = file
|
self.__file = fileName
|
||||||
|
|
||||||
def getFile(self):
|
def getFile(self):
|
||||||
return self.__file
|
return self.__file
|
||||||
|
|
|
@ -25,33 +25,31 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
||||||
__license__ = "GPL"
|
__license__ = "GPL"
|
||||||
|
|
||||||
import logging, os
|
import logging, os
|
||||||
from ConfigParser import *
|
from ConfigParser import SafeConfigParser
|
||||||
|
from ConfigParser import NoOptionError, NoSectionError
|
||||||
|
|
||||||
# Gets the instance of the logger.
|
# Gets the instance of the logger.
|
||||||
logSys = logging.getLogger("fail2ban.client.config")
|
logSys = logging.getLogger("fail2ban.client.config")
|
||||||
|
|
||||||
class ConfigReader(SafeConfigParser):
|
class ConfigReader(SafeConfigParser):
|
||||||
|
|
||||||
basedir = "/etc/fail2ban/"
|
BASE_DIRECTORY = "/etc/fail2ban/"
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
SafeConfigParser.__init__(self)
|
SafeConfigParser.__init__(self)
|
||||||
self.__opts = None
|
self.__opts = None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def setBaseDir(dir):
|
def setBaseDir(folderName):
|
||||||
global basedir
|
path = folderName.rstrip('/')
|
||||||
path = dir.rstrip('/')
|
ConfigReader.BASE_DIRECTORY = path + '/'
|
||||||
basedir = path + '/'
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def getBaseDir():
|
def getBaseDir():
|
||||||
global basedir
|
return ConfigReader.BASE_DIRECTORY
|
||||||
return basedir
|
|
||||||
|
|
||||||
def read(self, filename):
|
def read(self, filename):
|
||||||
global basedir
|
basename = ConfigReader.BASE_DIRECTORY + filename
|
||||||
basename = basedir + filename
|
|
||||||
logSys.debug("Reading " + basename)
|
logSys.debug("Reading " + basename)
|
||||||
bConf = basename + ".conf"
|
bConf = basename + ".conf"
|
||||||
bLocal = basename + ".local"
|
bLocal = basename + ".local"
|
||||||
|
|
|
@ -40,10 +40,12 @@ class Configurator:
|
||||||
self.__fail2ban = Fail2banReader()
|
self.__fail2ban = Fail2banReader()
|
||||||
self.__jails = JailsReader()
|
self.__jails = JailsReader()
|
||||||
|
|
||||||
def setBaseDir(self, dir):
|
@staticmethod
|
||||||
ConfigReader.setBaseDir(dir)
|
def setBaseDir(folderName):
|
||||||
|
ConfigReader.setBaseDir(folderName)
|
||||||
|
|
||||||
def getBaseDir(self):
|
@staticmethod
|
||||||
|
def getBaseDir():
|
||||||
return ConfigReader.getBaseDir()
|
return ConfigReader.getBaseDir()
|
||||||
|
|
||||||
def readEarly(self):
|
def readEarly(self):
|
||||||
|
@ -57,8 +59,8 @@ class Configurator:
|
||||||
return self.__fail2ban.getEarlyOptions()
|
return self.__fail2ban.getEarlyOptions()
|
||||||
|
|
||||||
def getAllOptions(self):
|
def getAllOptions(self):
|
||||||
self.__settings["general"] = self.__fail2ban.getOptions()
|
self.__fail2ban.getOptions()
|
||||||
self.__settings["jails"] = self.__jails.getOptions()
|
self.__jails.getOptions()
|
||||||
|
|
||||||
def convertToProtocol(self):
|
def convertToProtocol(self):
|
||||||
self.__streams["general"] = self.__fail2ban.convert()
|
self.__streams["general"] = self.__fail2ban.convert()
|
||||||
|
|
|
@ -46,10 +46,11 @@ class CSocket:
|
||||||
self.__csock.close()
|
self.__csock.close()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def receive(self, socket):
|
@staticmethod
|
||||||
|
def receive(sock):
|
||||||
msg = ''
|
msg = ''
|
||||||
while msg.rfind(CSocket.END_STRING) == -1:
|
while msg.rfind(CSocket.END_STRING) == -1:
|
||||||
chunk = socket.recv(6)
|
chunk = sock.recv(6)
|
||||||
if chunk == '':
|
if chunk == '':
|
||||||
raise RuntimeError, "socket connection broken"
|
raise RuntimeError, "socket connection broken"
|
||||||
msg = msg + chunk
|
msg = msg + chunk
|
||||||
|
|
|
@ -32,13 +32,13 @@ logSys = logging.getLogger("fail2ban.client.config")
|
||||||
|
|
||||||
class FilterReader(ConfigReader):
|
class FilterReader(ConfigReader):
|
||||||
|
|
||||||
def __init__(self, file, name):
|
def __init__(self, fileName, name):
|
||||||
ConfigReader.__init__(self)
|
ConfigReader.__init__(self)
|
||||||
self.__file = file
|
self.__file = fileName
|
||||||
self.__name = name
|
self.__name = name
|
||||||
|
|
||||||
def setFile(self, file):
|
def setFile(self, fileName):
|
||||||
self.__file = file
|
self.__file = fileName
|
||||||
|
|
||||||
def getFile(self):
|
def getFile(self):
|
||||||
return self.__file
|
return self.__file
|
||||||
|
|
|
@ -125,7 +125,7 @@ class JailReader(ConfigReader):
|
||||||
def splitAction(action):
|
def splitAction(action):
|
||||||
m = JailReader.actionCRE.match(action)
|
m = JailReader.actionCRE.match(action)
|
||||||
d = dict()
|
d = dict()
|
||||||
if m.group(2) <> None:
|
if not m.group(2) == None:
|
||||||
for param in m.group(2).split(','):
|
for param in m.group(2).split(','):
|
||||||
p = param.split('=')
|
p = param.split('=')
|
||||||
d[p[0].strip()] = p[1].strip()
|
d[p[0].strip()] = p[1].strip()
|
||||||
|
|
|
@ -55,11 +55,6 @@ class JailsReader(ConfigReader):
|
||||||
else:
|
else:
|
||||||
logSys.error("Errors in jail '" + sec + "'. Skipping...")
|
logSys.error("Errors in jail '" + sec + "'. Skipping...")
|
||||||
|
|
||||||
def getFilterOptions(self, file):
|
|
||||||
filter = FilterReader(file)
|
|
||||||
filter.read()
|
|
||||||
return filter.getOptions()
|
|
||||||
|
|
||||||
def convert(self):
|
def convert(self):
|
||||||
stream = list()
|
stream = list()
|
||||||
for opt in self.__opts:
|
for opt in self.__opts:
|
||||||
|
|
|
@ -108,7 +108,7 @@ class Action:
|
||||||
return self.__actionBan
|
return self.__actionBan
|
||||||
|
|
||||||
def execActionBan(self, aInfo):
|
def execActionBan(self, aInfo):
|
||||||
return self.__processCmd(self.__actionBan, aInfo);
|
return self.__processCmd(self.__actionBan, aInfo)
|
||||||
|
|
||||||
##
|
##
|
||||||
# Set the "unban" command.
|
# Set the "unban" command.
|
||||||
|
@ -128,7 +128,7 @@ class Action:
|
||||||
return self.__actionUnban
|
return self.__actionUnban
|
||||||
|
|
||||||
def execActionUnban(self, aInfo):
|
def execActionUnban(self, aInfo):
|
||||||
return self.__processCmd(self.__actionUnban, aInfo);
|
return self.__processCmd(self.__actionUnban, aInfo)
|
||||||
|
|
||||||
##
|
##
|
||||||
# Set the "check" command.
|
# Set the "check" command.
|
||||||
|
|
|
@ -49,7 +49,7 @@ class Actions(JailThread):
|
||||||
# @param jail the jail object
|
# @param jail the jail object
|
||||||
|
|
||||||
def __init__(self, jail):
|
def __init__(self, jail):
|
||||||
JailThread.__init__(self, jail)
|
JailThread.__init__(self)
|
||||||
## The jail which contains this action.
|
## The jail which contains this action.
|
||||||
self.jail = jail
|
self.jail = jail
|
||||||
self.__actions = list()
|
self.__actions = list()
|
||||||
|
|
|
@ -27,7 +27,7 @@ __license__ = "GPL"
|
||||||
from banticket import BanTicket
|
from banticket import BanTicket
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
from mytime import MyTime
|
from mytime import MyTime
|
||||||
import time, logging
|
import logging
|
||||||
|
|
||||||
# Gets the instance of the logger.
|
# Gets the instance of the logger.
|
||||||
logSys = logging.getLogger("fail2ban.action")
|
logSys = logging.getLogger("fail2ban.action")
|
||||||
|
|
|
@ -92,7 +92,7 @@ class DateDetector:
|
||||||
self.__lock.acquire()
|
self.__lock.acquire()
|
||||||
for template in self.__templates:
|
for template in self.__templates:
|
||||||
match = template.matchDate(line)
|
match = template.matchDate(line)
|
||||||
if match <> None:
|
if not match == None:
|
||||||
self.__lock.release()
|
self.__lock.release()
|
||||||
return match
|
return match
|
||||||
self.__lock.release()
|
self.__lock.release()
|
||||||
|
|
|
@ -51,7 +51,7 @@ class Filter(JailThread):
|
||||||
# @param jail the jail object
|
# @param jail the jail object
|
||||||
|
|
||||||
def __init__(self, jail):
|
def __init__(self, jail):
|
||||||
JailThread.__init__(self, jail)
|
JailThread.__init__(self)
|
||||||
## The jail which contains this filter.
|
## The jail which contains this filter.
|
||||||
self.jail = jail
|
self.jail = jail
|
||||||
## The failures manager.
|
## The failures manager.
|
||||||
|
@ -347,7 +347,6 @@ class Filter(JailThread):
|
||||||
# is created and is added to the FailManager.
|
# is created and is added to the FailManager.
|
||||||
|
|
||||||
def getFailures(self, filename):
|
def getFailures(self, filename):
|
||||||
ipList = dict()
|
|
||||||
ret = self.__openLogFile(filename)
|
ret = self.__openLogFile(filename)
|
||||||
if not ret:
|
if not ret:
|
||||||
logSys.error("Unable to get failures in " + filename)
|
logSys.error("Unable to get failures in " + filename)
|
||||||
|
@ -392,7 +391,7 @@ class Filter(JailThread):
|
||||||
match = self.__failRegexObj.search(line)
|
match = self.__failRegexObj.search(line)
|
||||||
if match:
|
if match:
|
||||||
date = self.dateDetector.getUnixTime(match.string)
|
date = self.dateDetector.getUnixTime(match.string)
|
||||||
if date <> None:
|
if not date == None:
|
||||||
try:
|
try:
|
||||||
ipMatch = DNSUtils.textToIp(match.group("host"))
|
ipMatch = DNSUtils.textToIp(match.group("host"))
|
||||||
if ipMatch:
|
if ipMatch:
|
||||||
|
@ -463,10 +462,10 @@ class DNSUtils:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def isValidIP(str):
|
def isValidIP(string):
|
||||||
""" Return true if str is a valid IP
|
""" Return true if str is a valid IP
|
||||||
"""
|
"""
|
||||||
s = str.split('/', 1)
|
s = string.split('/', 1)
|
||||||
try:
|
try:
|
||||||
socket.inet_aton(s[0])
|
socket.inet_aton(s[0])
|
||||||
return True
|
return True
|
||||||
|
@ -503,10 +502,10 @@ class DNSUtils:
|
||||||
return ~(MASK >> n) & MASK & DNSUtils.addr2bin(i)
|
return ~(MASK >> n) & MASK & DNSUtils.addr2bin(i)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def addr2bin(str):
|
def addr2bin(string):
|
||||||
""" Convert a string IPv4 address into an unsigned integer.
|
""" 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
|
@staticmethod
|
||||||
def bin2addr(addr):
|
def bin2addr(addr):
|
||||||
|
|
|
@ -95,9 +95,9 @@ class FilterPoll(Filter):
|
||||||
while self.isActive():
|
while self.isActive():
|
||||||
if not self.getIdle():
|
if not self.getIdle():
|
||||||
# Get file modification
|
# Get file modification
|
||||||
for file in self.getLogPath():
|
for f in self.getLogPath():
|
||||||
if self.isModified(file):
|
if self.isModified(f):
|
||||||
self.getFailures(file)
|
self.getFailures(f)
|
||||||
self.modified = True
|
self.modified = True
|
||||||
|
|
||||||
if self.modified:
|
if self.modified:
|
||||||
|
|
|
@ -54,6 +54,7 @@ class Jail:
|
||||||
self.__filter = FilterPoll(self)
|
self.__filter = FilterPoll(self)
|
||||||
|
|
||||||
def __initGamin(self):
|
def __initGamin(self):
|
||||||
|
# Try to import gamin
|
||||||
import gamin
|
import gamin
|
||||||
logSys.info("Using Gamin")
|
logSys.info("Using Gamin")
|
||||||
from filtergamin import FilterGamin
|
from filtergamin import FilterGamin
|
||||||
|
|
|
@ -38,7 +38,7 @@ class JailThread(Thread):
|
||||||
# Initialize the filter object with default values.
|
# Initialize the filter object with default values.
|
||||||
# @param jail the jail object
|
# @param jail the jail object
|
||||||
|
|
||||||
def __init__(self, jail):
|
def __init__(self):
|
||||||
Thread.__init__(self)
|
Thread.__init__(self)
|
||||||
## Control the state of the thread.
|
## Control the state of the thread.
|
||||||
self.__isRunning = False
|
self.__isRunning = False
|
||||||
|
|
|
@ -111,11 +111,11 @@ class Server:
|
||||||
def getIgnoreIP(self, name):
|
def getIgnoreIP(self, name):
|
||||||
return self.__jails.getFilter(name).getIgnoreIP()
|
return self.__jails.getFilter(name).getIgnoreIP()
|
||||||
|
|
||||||
def addLogPath(self, name, file):
|
def addLogPath(self, name, fileName):
|
||||||
self.__jails.getFilter(name).addLogPath(file)
|
self.__jails.getFilter(name).addLogPath(fileName)
|
||||||
|
|
||||||
def delLogPath(self, name, file):
|
def delLogPath(self, name, fileName):
|
||||||
self.__jails.getFilter(name).delLogPath(file)
|
self.__jails.getFilter(name).delLogPath(fileName)
|
||||||
|
|
||||||
def getLogPath(self, name):
|
def getLogPath(self, name):
|
||||||
return self.__jails.getFilter(name).getLogPath()
|
return self.__jails.getFilter(name).getLogPath()
|
||||||
|
|
|
@ -40,6 +40,7 @@ class SSocket(Thread):
|
||||||
self.__transmit = transmitter
|
self.__transmit = transmitter
|
||||||
self.__isRunning = False
|
self.__isRunning = False
|
||||||
self.__socket = "/tmp/fail2ban.sock"
|
self.__socket = "/tmp/fail2ban.sock"
|
||||||
|
self.__ssock = None
|
||||||
logSys.debug("Created SSocket")
|
logSys.debug("Created SSocket")
|
||||||
|
|
||||||
def initialize(self, sock = "/tmp/fail2ban.sock", force = False):
|
def initialize(self, sock = "/tmp/fail2ban.sock", force = False):
|
||||||
|
@ -53,31 +54,31 @@ class SSocket(Thread):
|
||||||
else:
|
else:
|
||||||
raise SSocketErrorException("Server already running")
|
raise SSocketErrorException("Server already running")
|
||||||
# Create an INET, STREAMing socket
|
# Create an INET, STREAMing socket
|
||||||
#self.ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
#self.__ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.ssock = socket.socket(socket.AF_UNIX, 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.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
#self.ssock.setblocking(False)
|
#self.__ssock.setblocking(False)
|
||||||
# Do not use a blocking socket as there is problem at shutdown.
|
# Do not use a blocking socket as there is problem at shutdown.
|
||||||
# Use a timeout instead. Daemon exits at most 'timeout' seconds
|
# Use a timeout instead. Daemon exits at most 'timeout' seconds
|
||||||
# after the command.
|
# after the command.
|
||||||
self.ssock.settimeout(1)
|
self.__ssock.settimeout(1)
|
||||||
# Bind the socket to a public host and a well-known port
|
# Bind the socket to a public host and a well-known port
|
||||||
#self.ssock.bind(("localhost", 2222))
|
#self.__ssock.bind(("localhost", 2222))
|
||||||
self.ssock.bind(sock)
|
self.__ssock.bind(sock)
|
||||||
# Become a server socket
|
# Become a server socket
|
||||||
self.ssock.listen(5)
|
self.__ssock.listen(5)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.__isRunning = True
|
self.__isRunning = True
|
||||||
while self.__isRunning:
|
while self.__isRunning:
|
||||||
try:
|
try:
|
||||||
(csock, address) = self.ssock.accept()
|
(csock, address) = self.__ssock.accept()
|
||||||
thread = SocketWorker(csock, self.__transmit)
|
thread = SocketWorker(csock, self.__transmit)
|
||||||
thread.start()
|
thread.start()
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
# Do nothing here
|
# Do nothing here
|
||||||
pass
|
pass
|
||||||
self.ssock.close()
|
self.__ssock.close()
|
||||||
# Remove socket
|
# Remove socket
|
||||||
if os.path.exists(self.__socket):
|
if os.path.exists(self.__socket):
|
||||||
logSys.debug("Removed socket file " + self.__socket)
|
logSys.debug("Removed socket file " + self.__socket)
|
||||||
|
@ -110,14 +111,16 @@ class SocketWorker(Thread):
|
||||||
self.__csock.close()
|
self.__csock.close()
|
||||||
logSys.debug("Connection closed")
|
logSys.debug("Connection closed")
|
||||||
|
|
||||||
def __send(self, socket, msg):
|
@staticmethod
|
||||||
|
def __send(sock, msg):
|
||||||
obj = dumps(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 = ''
|
msg = ''
|
||||||
while msg.rfind(SSocket.END_STRING) == -1:
|
while msg.rfind(SSocket.END_STRING) == -1:
|
||||||
chunk = socket.recv(6)
|
chunk = sock.recv(6)
|
||||||
if chunk == '':
|
if chunk == '':
|
||||||
raise RuntimeError, "socket connection broken"
|
raise RuntimeError, "socket connection broken"
|
||||||
msg = msg + chunk
|
msg = msg + chunk
|
||||||
|
|
Loading…
Reference in New Issue