mirror of https://github.com/fail2ban/fail2ban
- Moved every locking statements in a try..finally block
git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@553 a942ae1a-1317-0410-a47c-b1dcaea8d6050.x
parent
f02a915de1
commit
6426cf75aa
|
@ -13,6 +13,7 @@ ver. 0.?.? (2007/??/??) - ???
|
|||
- Added new filters/actions. Thanks to Yaroslav Halchenko
|
||||
- Added Suse init script and modified gentoo-initd. Thanks to
|
||||
Christian Rauch
|
||||
- Moved every locking statements in a try..finally block
|
||||
|
||||
ver. 0.7.7 (2007/02/08) - release candidate
|
||||
----------
|
||||
|
|
|
@ -62,9 +62,11 @@ class BanManager:
|
|||
# @param value the time
|
||||
|
||||
def setBanTime(self, value):
|
||||
self.__lock.acquire()
|
||||
self.__banTime = int(value)
|
||||
self.__lock.release()
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
self.__banTime = int(value)
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
||||
##
|
||||
# Get the ban time.
|
||||
|
@ -85,9 +87,11 @@ class BanManager:
|
|||
# @param value total number
|
||||
|
||||
def setBanTotal(self, value):
|
||||
self.__lock.acquire()
|
||||
self.__banTotal = value
|
||||
self.__lock.release()
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
self.__banTotal = value
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
||||
##
|
||||
# Get the total number of banned address.
|
||||
|
|
|
@ -42,44 +42,48 @@ class DateDetector:
|
|||
self.__defTemplate = DateStrptime()
|
||||
|
||||
def addDefaultTemplate(self):
|
||||
# standard
|
||||
template = DateStrptime()
|
||||
template.setName("Month Day Hour:Minute:Second")
|
||||
template.setRegex("^\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}")
|
||||
template.setPattern("%b %d %H:%M:%S")
|
||||
self.__templates.append(template)
|
||||
# asctime
|
||||
template = DateStrptime()
|
||||
template.setName("Weekday Month Day Hour:Minute:Second Year")
|
||||
template.setRegex("\S{3} \S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2} \d{4}")
|
||||
template.setPattern("%a %b %d %H:%M:%S %Y")
|
||||
self.__templates.append(template)
|
||||
# simple date
|
||||
template = DateStrptime()
|
||||
template.setName("Year/Month/Day Hour:Minute:Second")
|
||||
template.setRegex("\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}")
|
||||
template.setPattern("%Y/%m/%d %H:%M:%S")
|
||||
self.__templates.append(template)
|
||||
# Apache format [31/Oct/2006:09:22:55 -0000]
|
||||
template = DateStrptime()
|
||||
template.setName("Day/Month/Year:Hour:Minute:Second")
|
||||
template.setRegex("\d{2}/\S{3}/\d{4}:\d{2}:\d{2}:\d{2}")
|
||||
template.setPattern("%d/%b/%Y:%H:%M:%S")
|
||||
self.__templates.append(template)
|
||||
# Exim 2006-12-21 06:43:20
|
||||
template = DateStrptime()
|
||||
template.setName("Year-Month-Day Hour:Minute:Second")
|
||||
template.setRegex("\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}")
|
||||
template.setPattern("%Y-%m-%d %H:%M:%S")
|
||||
self.__templates.append(template)
|
||||
# TAI64N
|
||||
template = DateTai64n()
|
||||
template.setName("TAI64N")
|
||||
self.__templates.append(template)
|
||||
# Epoch
|
||||
template = DateEpoch()
|
||||
template.setName("Epoch")
|
||||
self.__templates.append(template)
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
# standard
|
||||
template = DateStrptime()
|
||||
template.setName("Month Day Hour:Minute:Second")
|
||||
template.setRegex("^\S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2}")
|
||||
template.setPattern("%b %d %H:%M:%S")
|
||||
self.__templates.append(template)
|
||||
# asctime
|
||||
template = DateStrptime()
|
||||
template.setName("Weekday Month Day Hour:Minute:Second Year")
|
||||
template.setRegex("\S{3} \S{3}\s{1,2}\d{1,2} \d{2}:\d{2}:\d{2} \d{4}")
|
||||
template.setPattern("%a %b %d %H:%M:%S %Y")
|
||||
self.__templates.append(template)
|
||||
# simple date
|
||||
template = DateStrptime()
|
||||
template.setName("Year/Month/Day Hour:Minute:Second")
|
||||
template.setRegex("\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}")
|
||||
template.setPattern("%Y/%m/%d %H:%M:%S")
|
||||
self.__templates.append(template)
|
||||
# Apache format [31/Oct/2006:09:22:55 -0000]
|
||||
template = DateStrptime()
|
||||
template.setName("Day/Month/Year:Hour:Minute:Second")
|
||||
template.setRegex("\d{2}/\S{3}/\d{4}:\d{2}:\d{2}:\d{2}")
|
||||
template.setPattern("%d/%b/%Y:%H:%M:%S")
|
||||
self.__templates.append(template)
|
||||
# Exim 2006-12-21 06:43:20
|
||||
template = DateStrptime()
|
||||
template.setName("Year-Month-Day Hour:Minute:Second")
|
||||
template.setRegex("\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}")
|
||||
template.setPattern("%Y-%m-%d %H:%M:%S")
|
||||
self.__templates.append(template)
|
||||
# TAI64N
|
||||
template = DateTai64n()
|
||||
template.setName("TAI64N")
|
||||
self.__templates.append(template)
|
||||
# Epoch
|
||||
template = DateEpoch()
|
||||
template.setName("Epoch")
|
||||
self.__templates.append(template)
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
||||
def getTemplates(self):
|
||||
return self.__templates
|
||||
|
@ -100,14 +104,15 @@ class DateDetector:
|
|||
if self.__defTemplate.isValid():
|
||||
return self.__defTemplate.matchDate(line)
|
||||
else:
|
||||
self.__lock.acquire()
|
||||
for template in self.__templates:
|
||||
match = template.matchDate(line)
|
||||
if not match == None:
|
||||
self.__lock.release()
|
||||
return match
|
||||
self.__lock.release()
|
||||
return None
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
for template in self.__templates:
|
||||
match = template.matchDate(line)
|
||||
if not match == None:
|
||||
return match
|
||||
return None
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
||||
def getTime(self, line):
|
||||
if self.__defTemplate.isValid():
|
||||
|
@ -117,19 +122,20 @@ class DateDetector:
|
|||
except ValueError:
|
||||
return None
|
||||
else:
|
||||
self.__lock.acquire()
|
||||
for template in self.__templates:
|
||||
try:
|
||||
date = template.getDate(line)
|
||||
if date == None:
|
||||
continue
|
||||
template.incHits()
|
||||
self.__lock.release()
|
||||
return date
|
||||
except ValueError:
|
||||
pass
|
||||
self.__lock.release()
|
||||
return None
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
for template in self.__templates:
|
||||
try:
|
||||
date = template.getDate(line)
|
||||
if date == None:
|
||||
continue
|
||||
template.incHits()
|
||||
return date
|
||||
except ValueError:
|
||||
pass
|
||||
return None
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
||||
def getUnixTime(self, line):
|
||||
date = self.getTime(line)
|
||||
|
@ -143,8 +149,11 @@ class DateDetector:
|
|||
# in this object and thus should be called from time to time.
|
||||
|
||||
def sortTemplate(self):
|
||||
self.__lock.acquire()
|
||||
logSys.debug("Sorting the template list")
|
||||
self.__templates.sort(cmp = lambda x, y: cmp(x.getHits(), y.getHits()),
|
||||
reverse=True)
|
||||
self.__lock.release()
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
logSys.debug("Sorting the template list")
|
||||
self.__templates.sort(cmp = lambda x, y:
|
||||
cmp(x.getHits(), y.getHits()),
|
||||
reverse = True)
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
|
|
@ -42,9 +42,11 @@ class FailManager:
|
|||
self.__failTotal = 0
|
||||
|
||||
def setFailTotal(self, value):
|
||||
self.__lock.acquire()
|
||||
self.__failTotal = value
|
||||
self.__lock.release()
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
self.__failTotal = value
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
||||
def getFailTotal(self):
|
||||
try:
|
||||
|
@ -54,9 +56,11 @@ class FailManager:
|
|||
self.__lock.release()
|
||||
|
||||
def setMaxRetry(self, value):
|
||||
self.__lock.acquire()
|
||||
self.__maxRetry = value
|
||||
self.__lock.release()
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
self.__maxRetry = value
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
||||
def getMaxRetry(self):
|
||||
try:
|
||||
|
@ -66,9 +70,11 @@ class FailManager:
|
|||
self.__lock.release()
|
||||
|
||||
def setMaxTime(self, value):
|
||||
self.__lock.acquire()
|
||||
self.__maxTime = value
|
||||
self.__lock.release()
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
self.__maxTime = value
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
||||
def getMaxTime(self):
|
||||
try:
|
||||
|
@ -78,20 +84,22 @@ class FailManager:
|
|||
self.__lock.release()
|
||||
|
||||
def addFailure(self, ticket):
|
||||
self.__lock.acquire()
|
||||
ip = ticket.getIP()
|
||||
unixTime = ticket.getTime()
|
||||
if self.__failList.has_key(ip):
|
||||
fData = self.__failList[ip]
|
||||
fData.inc()
|
||||
fData.setLastTime(unixTime)
|
||||
else:
|
||||
fData = FailData()
|
||||
fData.inc()
|
||||
fData.setLastTime(unixTime)
|
||||
self.__failList[ip] = fData
|
||||
self.__failTotal += 1
|
||||
self.__lock.release()
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
ip = ticket.getIP()
|
||||
unixTime = ticket.getTime()
|
||||
if self.__failList.has_key(ip):
|
||||
fData = self.__failList[ip]
|
||||
fData.inc()
|
||||
fData.setLastTime(unixTime)
|
||||
else:
|
||||
fData = FailData()
|
||||
fData.inc()
|
||||
fData.setLastTime(unixTime)
|
||||
self.__failList[ip] = fData
|
||||
self.__failTotal += 1
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
||||
def size(self):
|
||||
try:
|
||||
|
@ -101,12 +109,14 @@ class FailManager:
|
|||
self.__lock.release()
|
||||
|
||||
def cleanup(self, time):
|
||||
self.__lock.acquire()
|
||||
tmp = self.__failList.copy()
|
||||
for item in tmp:
|
||||
if tmp[item].getLastTime() < time - self.__maxTime:
|
||||
self.__delFailure(item)
|
||||
self.__lock.release()
|
||||
try:
|
||||
self.__lock.acquire()
|
||||
tmp = self.__failList.copy()
|
||||
for item in tmp:
|
||||
if tmp[item].getLastTime() < time - self.__maxTime:
|
||||
self.__delFailure(item)
|
||||
finally:
|
||||
self.__lock.release()
|
||||
|
||||
def __delFailure(self, ip):
|
||||
if self.__failList.has_key(ip):
|
||||
|
@ -129,4 +139,3 @@ class FailManager:
|
|||
|
||||
class FailManagerEmpty(Exception):
|
||||
pass
|
||||
|
Loading…
Reference in New Issue