mirror of https://github.com/fail2ban/fail2ban
applied Cyril"s modifications to my patch for missing chain
parent
501955f36a
commit
91df9c7526
|
@ -1,4 +1,4 @@
|
||||||
fail2ban (0.5.4-5.9) unstable; urgency=low
|
fail2ban (0.5.4-5.10) unstable; urgency=low
|
||||||
|
|
||||||
* Added a notification regarding the importance of 0.5.4-5 change of
|
* Added a notification regarding the importance of 0.5.4-5 change of
|
||||||
failregex in the config file.
|
failregex in the config file.
|
||||||
|
@ -14,6 +14,7 @@ fail2ban (0.5.4-5.9) unstable; urgency=low
|
||||||
* Introduced fwcheck option to verify consistency of the
|
* Introduced fwcheck option to verify consistency of the
|
||||||
chains. Implemented automatic restart of fail2ban main function in
|
chains. Implemented automatic restart of fail2ban main function in
|
||||||
case if check of fwban or fwban command failed (closes: #329163, #331695).
|
case if check of fwban or fwban command failed (closes: #329163, #331695).
|
||||||
|
(Introduced patch was further adjusted by upstream author)
|
||||||
|
|
||||||
-- Yaroslav Halchenko <debian@onerussian.com> Mon, 3 Oct 2005 22:26:28 -1000
|
-- Yaroslav Halchenko <debian@onerussian.com> Mon, 3 Oct 2005 22:26:28 -1000
|
||||||
|
|
||||||
|
|
25
fail2ban.py
25
fail2ban.py
|
@ -100,9 +100,7 @@ def initializeFwRules():
|
||||||
executeCmd(conf["cmdstart"], conf["debug"])
|
executeCmd(conf["cmdstart"], conf["debug"])
|
||||||
# Execute start command of each section
|
# Execute start command of each section
|
||||||
for element in logFwList:
|
for element in logFwList:
|
||||||
l = element[4]
|
element[2].initialize(conf["debug"])
|
||||||
executeCmd(l["fwstart"], conf["debug"])
|
|
||||||
|
|
||||||
|
|
||||||
def reBan():
|
def reBan():
|
||||||
""" For each section asks the Firewall to reban known IPs
|
""" For each section asks the Firewall to reban known IPs
|
||||||
|
@ -117,13 +115,12 @@ def restoreFwRules():
|
||||||
for element in logFwList:
|
for element in logFwList:
|
||||||
try:
|
try:
|
||||||
element[2].flushBanList(conf["debug"])
|
element[2].flushBanList(conf["debug"])
|
||||||
except ExternalError, e:
|
except ExternalError:
|
||||||
# nothing bad really - we can survive :-)
|
# nothing bad really - we can survive :-)
|
||||||
pass
|
pass
|
||||||
# Execute end command of each section
|
# Execute end command of each section
|
||||||
for element in logFwList:
|
for element in logFwList:
|
||||||
l = element[4]
|
element[2].restore(conf["debug"])
|
||||||
executeCmd(l["fwend"], conf["debug"])
|
|
||||||
# Execute global end command
|
# Execute global end command
|
||||||
executeCmd(conf["cmdend"], conf["debug"])
|
executeCmd(conf["cmdend"], conf["debug"])
|
||||||
|
|
||||||
|
@ -220,6 +217,7 @@ def main():
|
||||||
["int", "reinittime", 100],
|
["int", "reinittime", 100],
|
||||||
["int", "maxreinits", 100])
|
["int", "maxreinits", 100])
|
||||||
|
|
||||||
|
|
||||||
# Gets global configuration options
|
# Gets global configuration options
|
||||||
conf.update(confReader.getLogOptions("DEFAULT", optionValues))
|
conf.update(confReader.getLogOptions("DEFAULT", optionValues))
|
||||||
|
|
||||||
|
@ -398,7 +396,7 @@ def main():
|
||||||
fObj = Firewall(l["fwban"], l["fwunban"], l["fwcheck"], l["bantime"])
|
fObj = Firewall(l["fwban"], l["fwunban"], l["fwcheck"], l["bantime"])
|
||||||
# Links them into a list. I'm not really happy
|
# Links them into a list. I'm not really happy
|
||||||
# with this :/
|
# with this :/
|
||||||
logFwList.append([t, lObj, fObj, dict(), l])
|
logFwList.append([t, lObj, fObj, dict()])
|
||||||
|
|
||||||
logSys.info("Enabled sections: %s"%enabledSections)
|
logSys.info("Enabled sections: %s"%enabledSections)
|
||||||
|
|
||||||
|
@ -416,8 +414,8 @@ def main():
|
||||||
logSys.warn(ip + " is not a valid IP address")
|
logSys.warn(ip + " is not a valid IP address")
|
||||||
|
|
||||||
initializeFwRules()
|
initializeFwRules()
|
||||||
|
# try to reinit once if it fails immediately
|
||||||
lastReinitTime = time.time()-conf["reinittime"]-1 # try to reinit once if it fails immediately
|
lastReinitTime = time.time() - conf["reinittime"] - 1
|
||||||
reinits = 0
|
reinits = 0
|
||||||
# Main loop
|
# Main loop
|
||||||
while True:
|
while True:
|
||||||
|
@ -488,11 +486,12 @@ def main():
|
||||||
logSys.warn("#%d reinitialization of firewalls"%reinits)
|
logSys.warn("#%d reinitialization of firewalls"%reinits)
|
||||||
lastReinitTime = unixTime
|
lastReinitTime = unixTime
|
||||||
else:
|
else:
|
||||||
logSys.error("Exiting: reinits follow too often, or too many reinit attempts")
|
logSys.error("Exiting: reinits follow too often, or too many " +
|
||||||
|
"reinit attempts")
|
||||||
killApp()
|
killApp()
|
||||||
|
|
||||||
# save firewalls to keep a list of IPs for rebanning
|
# save firewalls to keep a list of IPs for rebanning
|
||||||
logFwListCopy = copy.deepcopy(logFwList)
|
logFwListCopy = copy.deepcopy(logFwList)
|
||||||
|
try:
|
||||||
# restore as much as possible
|
# restore as much as possible
|
||||||
restoreFwRules()
|
restoreFwRules()
|
||||||
# reinitialize all the chains
|
# reinitialize all the chains
|
||||||
|
@ -501,6 +500,10 @@ def main():
|
||||||
logFwList.__init__(logFwListCopy)
|
logFwList.__init__(logFwListCopy)
|
||||||
# reBan known IPs
|
# reBan known IPs
|
||||||
reBan()
|
reBan()
|
||||||
|
except ExternalError:
|
||||||
|
raise ExternalError("Big Oops happened: situation is out of " +
|
||||||
|
"control. Something is wrong with your " +
|
||||||
|
"setup. Please check your settings")
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
# When the user press <ctrl>+<c> we exit nicely.
|
# When the user press <ctrl>+<c> we exit nicely.
|
||||||
killApp()
|
killApp()
|
||||||
|
|
|
@ -28,7 +28,6 @@ import time, os, logging, re
|
||||||
|
|
||||||
from utils.process import executeCmd
|
from utils.process import executeCmd
|
||||||
from utils.strings import replaceTag
|
from utils.strings import replaceTag
|
||||||
from utils.process import ExternalError
|
|
||||||
|
|
||||||
# Gets the instance of the logger.
|
# Gets the instance of the logger.
|
||||||
logSys = logging.getLogger("fail2ban")
|
logSys = logging.getLogger("fail2ban")
|
||||||
|
@ -42,23 +41,44 @@ class Firewall:
|
||||||
self.banRule = banRule
|
self.banRule = banRule
|
||||||
self.unBanRule = unBanRule
|
self.unBanRule = unBanRule
|
||||||
self.checkRule = checkRule
|
self.checkRule = checkRule
|
||||||
|
self.startRule = ""
|
||||||
|
self.endRule = ""
|
||||||
self.banTime = banTime
|
self.banTime = banTime
|
||||||
self.banList = dict()
|
self.banList = dict()
|
||||||
|
|
||||||
|
def setStartRule(self, cmd):
|
||||||
|
self.startRule = cmd
|
||||||
|
|
||||||
|
def getStartRule(self):
|
||||||
|
return self.startRule
|
||||||
|
|
||||||
|
def setEndRule(self, cmd):
|
||||||
|
self.endRule = cmd
|
||||||
|
|
||||||
|
def getEndRule(self):
|
||||||
|
return self.endRule
|
||||||
|
|
||||||
|
def initialize(self, debug):
|
||||||
|
logSys.debug("Initialize firewall rules")
|
||||||
|
executeCmd(self.startRule, debug)
|
||||||
|
|
||||||
|
def restore(self, debug):
|
||||||
|
logSys.debug("Restore firewall rules")
|
||||||
|
executeCmd(self.endRule, debug)
|
||||||
|
|
||||||
def addBanIP(self, aInfo, debug):
|
def addBanIP(self, aInfo, debug):
|
||||||
""" Bans an IP.
|
""" Bans an IP.
|
||||||
"""
|
"""
|
||||||
ip = aInfo["ip"]
|
ip = aInfo["ip"]
|
||||||
self.runCheck("pre-fwban", debug)
|
|
||||||
if not self.inBanList(ip):
|
if not self.inBanList(ip):
|
||||||
crtTime = time.time()
|
crtTime = time.time()
|
||||||
logSys.warn("Ban " + ip)
|
logSys.warn("Ban " + ip)
|
||||||
self.banList[ip] = crtTime
|
self.banList[ip] = crtTime
|
||||||
aInfo["bantime"] = crtTime
|
aInfo["bantime"] = crtTime
|
||||||
cmd = self.banIP(aInfo)
|
self.runCheck(debug)
|
||||||
if executeCmd(cmd, debug):
|
executeCmd(self.banIP(aInfo), debug)
|
||||||
raise ExternalError("Firewall: execution of fwban command '%s' failed"%cmd)
|
|
||||||
else:
|
else:
|
||||||
|
self.runCheck(debug)
|
||||||
logSys.error(ip+" already in ban list")
|
logSys.error(ip+" already in ban list")
|
||||||
|
|
||||||
def delBanIP(self, aInfo, debug):
|
def delBanIP(self, aInfo, debug):
|
||||||
|
@ -68,13 +88,14 @@ class Firewall:
|
||||||
if self.inBanList(ip):
|
if self.inBanList(ip):
|
||||||
logSys.warn("Unban " + ip)
|
logSys.warn("Unban " + ip)
|
||||||
del self.banList[ip]
|
del self.banList[ip]
|
||||||
self.runCheck("pre-fwunban", debug)
|
self.runCheck(debug)
|
||||||
executeCmd(self.unBanIP(aInfo), debug)
|
executeCmd(self.unBanIP(aInfo), debug)
|
||||||
else:
|
else:
|
||||||
logSys.error(ip+" not in ban list")
|
logSys.error(ip+" not in ban list")
|
||||||
|
|
||||||
def reBan(self, debug):
|
def reBan(self, debug):
|
||||||
""" Re-Bans known IPs.
|
""" Re-Bans known IPs.
|
||||||
|
TODO: implement "failures" and "failtime"
|
||||||
"""
|
"""
|
||||||
for ip in self.banList:
|
for ip in self.banList:
|
||||||
aInfo = {"ip": ip,
|
aInfo = {"ip": ip,
|
||||||
|
@ -82,21 +103,19 @@ class Firewall:
|
||||||
logSys.warn("ReBan " + ip)
|
logSys.warn("ReBan " + ip)
|
||||||
# next piece is similar to the on in addBanIp
|
# next piece is similar to the on in addBanIp
|
||||||
# so might be one more function will not hurt
|
# so might be one more function will not hurt
|
||||||
self.runCheck("pre-fw-reban", debug)
|
self.runCheck(debug)
|
||||||
cmd = self.banIP(aInfo)
|
executeCmd(self.banIP(aInfo), debug)
|
||||||
if executeCmd(cmd, debug):
|
|
||||||
raise ExternalError("Firewall: execution of fwban command '%s' failed"%cmd)
|
|
||||||
|
|
||||||
def inBanList(self, ip):
|
def inBanList(self, ip):
|
||||||
""" Checks if IP is in ban list.
|
""" Checks if IP is in ban list.
|
||||||
"""
|
"""
|
||||||
return self.banList.has_key(ip)
|
return self.banList.has_key(ip)
|
||||||
|
|
||||||
def runCheck(self, location, debug):
|
def runCheck(self, debug):
|
||||||
""" Runs fwcheck command and throws an exception if it returns non-0 result """
|
""" Runs fwcheck command and throws an exception if it returns non-0
|
||||||
if executeCmd(self.checkRule, debug):
|
result
|
||||||
raise ExternalError("Firewall: %s fwcheck command '%s' failed"
|
"""
|
||||||
%(location,self.checkRule))
|
executeCmd(self.checkRule, debug)
|
||||||
|
|
||||||
def checkForUnBan(self, debug):
|
def checkForUnBan(self, debug):
|
||||||
""" Check for IP to remove from ban list.
|
""" Check for IP to remove from ban list.
|
||||||
|
|
|
@ -30,7 +30,8 @@ import os, logging, signal
|
||||||
logSys = logging.getLogger("fail2ban")
|
logSys = logging.getLogger("fail2ban")
|
||||||
|
|
||||||
class ExternalError(UserWarning):
|
class ExternalError(UserWarning):
|
||||||
""" Exception to warn about failed fwcheck or fwban command """
|
""" Exception to warn about failed command
|
||||||
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def createDaemon():
|
def createDaemon():
|
||||||
|
@ -130,6 +131,7 @@ def executeCmd(cmd, debug):
|
||||||
retval = os.system(cmd)
|
retval = os.system(cmd)
|
||||||
if not retval == 0:
|
if not retval == 0:
|
||||||
logSys.error("'" + cmd + "' returned " + `retval`)
|
logSys.error("'" + cmd + "' returned " + `retval`)
|
||||||
|
raise ExternalError("Execution of command '%s' failed" % cmd)
|
||||||
return retval
|
return retval
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in New Issue