|
|
|
@ -25,7 +25,7 @@ __date__ = "$Date: 2005/09/13 20:42:33 $"
|
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
|
|
|
|
|
import time, sys, getopt, os, string, signal, logging, logging.handlers
|
|
|
|
|
import time, sys, getopt, os, string, signal, logging, logging.handlers, copy
|
|
|
|
|
from ConfigParser import *
|
|
|
|
|
|
|
|
|
|
from version import version
|
|
|
|
@ -92,12 +92,34 @@ def sigTERMhandler(signum, frame):
|
|
|
|
|
logSys.debug("Signal handler called with sig "+`signum`)
|
|
|
|
|
killApp()
|
|
|
|
|
|
|
|
|
|
def initializeFwRules():
|
|
|
|
|
""" Initializes firewalls by running cmdstart and then
|
|
|
|
|
fwstart for each section
|
|
|
|
|
"""
|
|
|
|
|
# Execute global start command
|
|
|
|
|
executeCmd(conf["cmdstart"], conf["debug"])
|
|
|
|
|
# Execute start command of each section
|
|
|
|
|
for element in logFwList:
|
|
|
|
|
l = element[4]
|
|
|
|
|
executeCmd(l["fwstart"], conf["debug"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reBan():
|
|
|
|
|
""" For each section asks the Firewall to reban known IPs
|
|
|
|
|
"""
|
|
|
|
|
for element in logFwList:
|
|
|
|
|
element[2].reBan(conf["debug"])
|
|
|
|
|
|
|
|
|
|
def restoreFwRules():
|
|
|
|
|
""" Flush the ban list
|
|
|
|
|
"""
|
|
|
|
|
logSys.warn("Restoring firewall rules...")
|
|
|
|
|
for element in logFwList:
|
|
|
|
|
try:
|
|
|
|
|
element[2].flushBanList(conf["debug"])
|
|
|
|
|
except ExternalError, e:
|
|
|
|
|
# nothing bad really - we can survive :-)
|
|
|
|
|
pass
|
|
|
|
|
# Execute end command of each section
|
|
|
|
|
for element in logFwList:
|
|
|
|
|
l = element[4]
|
|
|
|
@ -106,11 +128,15 @@ def restoreFwRules():
|
|
|
|
|
executeCmd(conf["cmdend"], conf["debug"])
|
|
|
|
|
|
|
|
|
|
def killApp():
|
|
|
|
|
""" Flush the ban list, remove and exit
|
|
|
|
|
""" Flush the ban list, remove the PID lock file and exit
|
|
|
|
|
nicely.
|
|
|
|
|
"""
|
|
|
|
|
# Restore Fw rules
|
|
|
|
|
restoreFwRules()
|
|
|
|
|
# Remove the PID lock
|
|
|
|
|
pidLock.remove()
|
|
|
|
|
logSys.info("Exiting...")
|
|
|
|
|
logging.shutdown()
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
def getCmdLineOptions(optList):
|
|
|
|
@ -138,12 +164,9 @@ def getCmdLineOptions(optList):
|
|
|
|
|
if opt[0] == "-k":
|
|
|
|
|
conf["kill"] = True
|
|
|
|
|
|
|
|
|
|
def main(secondaryStart):
|
|
|
|
|
def main():
|
|
|
|
|
""" Fail2Ban main function
|
|
|
|
|
"""
|
|
|
|
|
# (re)Initialize global variables
|
|
|
|
|
logFwList.__init__()
|
|
|
|
|
conf.clear()
|
|
|
|
|
|
|
|
|
|
# Add the default logging handler
|
|
|
|
|
stdout = logging.StreamHandler(sys.stdout)
|
|
|
|
@ -218,12 +241,8 @@ def main(secondaryStart):
|
|
|
|
|
except KeyError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# If it is not a hot restart
|
|
|
|
|
# fork, setup logging, create pid, check for root
|
|
|
|
|
if not secondaryStart:
|
|
|
|
|
# Start Fail2Ban in daemon mode
|
|
|
|
|
if conf["background"]:
|
|
|
|
|
logSys.debug("Daemonizing")
|
|
|
|
|
retCode = createDaemon()
|
|
|
|
|
signal.signal(signal.SIGTERM, sigTERMhandler)
|
|
|
|
|
if not retCode:
|
|
|
|
@ -301,6 +320,9 @@ def main(secondaryStart):
|
|
|
|
|
logSys.warn("DEBUG MODE: FIREWALL COMMANDS ARE _NOT_ EXECUTED BUT " +
|
|
|
|
|
"ONLY DISPLAYED IN THE LOG MESSAGES")
|
|
|
|
|
|
|
|
|
|
# Ignores IP list
|
|
|
|
|
ignoreIPList = conf["ignoreip"].split(' ')
|
|
|
|
|
|
|
|
|
|
# Checks for root user. This is necessary because log files
|
|
|
|
|
# are owned by root and firewall needs root access.
|
|
|
|
|
if not checkForRoot():
|
|
|
|
@ -319,9 +341,6 @@ def main(secondaryStart):
|
|
|
|
|
# Unable to create PID lock. Exit
|
|
|
|
|
sys.exit(-1)
|
|
|
|
|
|
|
|
|
|
# Ignores IP list
|
|
|
|
|
ignoreIPList = conf["ignoreip"].split(' ')
|
|
|
|
|
|
|
|
|
|
logSys.debug("ConfFile is " + conf["conffile"])
|
|
|
|
|
logSys.debug("BanTime is " + `conf["bantime"]`)
|
|
|
|
|
logSys.debug("FindTime is " + `conf["findtime"]`)
|
|
|
|
@ -394,12 +413,15 @@ def main(secondaryStart):
|
|
|
|
|
else:
|
|
|
|
|
logSys.warn(ip + " is not a valid IP address")
|
|
|
|
|
|
|
|
|
|
# Execute global start command
|
|
|
|
|
executeCmd(conf["cmdstart"], conf["debug"])
|
|
|
|
|
# Execute start command of each section
|
|
|
|
|
for element in logFwList:
|
|
|
|
|
l = element[4]
|
|
|
|
|
executeCmd(l["fwstart"], conf["debug"])
|
|
|
|
|
initializeFwRules()
|
|
|
|
|
|
|
|
|
|
# yoh: I don't think that this parameters need to be configured
|
|
|
|
|
# and probably maxRestarts should be removed
|
|
|
|
|
legitRestartTime = 10 # legitimate minimal restart time
|
|
|
|
|
maxRestarts = 100 # max number of times to perform restart
|
|
|
|
|
|
|
|
|
|
lastRestartTime = time.time()
|
|
|
|
|
restarts = 0
|
|
|
|
|
# Main loop
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
@ -459,10 +481,27 @@ def main(secondaryStart):
|
|
|
|
|
mail.sendmail(mailConf["subject"],
|
|
|
|
|
mailConf["message"], aInfo)
|
|
|
|
|
del element[3][attempt]
|
|
|
|
|
except ExternalError:
|
|
|
|
|
# restore as much as possible before restart
|
|
|
|
|
except ExternalError, e:
|
|
|
|
|
# Something wrong while dealing with Iptables.
|
|
|
|
|
# May be chain got removed?
|
|
|
|
|
logSys.error("Fail2Ban got a problem: " + e.__str__())
|
|
|
|
|
if (unixTime - lastRestartTime > legitRestartTime) and (restarts < maxRestarts):
|
|
|
|
|
logSys.error("Reinitializing firewalls for the %dst time "%restarts)
|
|
|
|
|
lastRestartTime = time.time()
|
|
|
|
|
else:
|
|
|
|
|
logSys.error("Exiting: restarts follow too often, or too many restart attempts")
|
|
|
|
|
killApp()
|
|
|
|
|
|
|
|
|
|
# save firewalls to keep a list of IPs for rebanning
|
|
|
|
|
logFwListCopy = copy.deepcopy(logFwList)
|
|
|
|
|
# restore as much as possible
|
|
|
|
|
restoreFwRules()
|
|
|
|
|
raise
|
|
|
|
|
# reinitialize all the chains
|
|
|
|
|
initializeFwRules()
|
|
|
|
|
# restore the lists of baned IPs
|
|
|
|
|
logFwList = logFwListCopy
|
|
|
|
|
# reBan known IPs
|
|
|
|
|
reBan()
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
# When the user press <ctrl>+<c> we exit nicely.
|
|
|
|
|
killApp()
|
|
|
|
|