|
|
|
@ -27,7 +27,6 @@ __license__ = "GPL"
|
|
|
|
|
import threading
|
|
|
|
|
from threading import Lock, RLock
|
|
|
|
|
import logging
|
|
|
|
|
import logging.handlers
|
|
|
|
|
import os
|
|
|
|
|
import signal
|
|
|
|
|
import stat
|
|
|
|
@ -152,6 +151,23 @@ class Server:
|
|
|
|
|
self.__asyncServer.start(sock, force)
|
|
|
|
|
except AsyncServerException as e:
|
|
|
|
|
logSys.error("Could not start server: %s", e)
|
|
|
|
|
|
|
|
|
|
logSys.info("Shutdown in progress...")
|
|
|
|
|
|
|
|
|
|
# Restore default signal handlers:
|
|
|
|
|
if _thread_name() == '_MainThread':
|
|
|
|
|
for s, sh in self.__prev_signals.iteritems():
|
|
|
|
|
signal.signal(s, sh)
|
|
|
|
|
|
|
|
|
|
# Now stop all the jails
|
|
|
|
|
self.stopAllJail()
|
|
|
|
|
|
|
|
|
|
# Explicit close database (server can leave in a thread,
|
|
|
|
|
# so delayed GC can prevent commiting changes)
|
|
|
|
|
if self.__db:
|
|
|
|
|
self.__db.close()
|
|
|
|
|
self.__db = None
|
|
|
|
|
|
|
|
|
|
# Removes the PID file.
|
|
|
|
|
try:
|
|
|
|
|
logSys.debug("Remove PID file %s", pidfile)
|
|
|
|
@ -159,8 +175,10 @@ class Server:
|
|
|
|
|
except (OSError, IOError) as e: # pragma: no cover
|
|
|
|
|
logSys.error("Unable to remove PID file: %s", e)
|
|
|
|
|
logSys.info("Exiting Fail2ban")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def quit(self):
|
|
|
|
|
# Prevent to call quit twice:
|
|
|
|
|
self.quit = lambda: False
|
|
|
|
|
# Stop communication first because if jail's unban action
|
|
|
|
|
# tries to communicate via fail2ban-client we get a lockup
|
|
|
|
|
# among threads. So the simplest resolution is to stop all
|
|
|
|
@ -171,28 +189,6 @@ class Server:
|
|
|
|
|
self.__asyncServer.stop()
|
|
|
|
|
self.__asyncServer = None
|
|
|
|
|
|
|
|
|
|
# Now stop all the jails
|
|
|
|
|
self.stopAllJail()
|
|
|
|
|
|
|
|
|
|
# Explicit close database (server can leave in a thread,
|
|
|
|
|
# so delayed GC can prevent commiting changes)
|
|
|
|
|
if self.__db:
|
|
|
|
|
self.__db.close()
|
|
|
|
|
self.__db = None
|
|
|
|
|
|
|
|
|
|
# Only now shutdown the logging.
|
|
|
|
|
if self.__logTarget is not None:
|
|
|
|
|
with self.__loggingLock:
|
|
|
|
|
logging.shutdown()
|
|
|
|
|
|
|
|
|
|
# Restore default signal handlers:
|
|
|
|
|
if _thread_name() == '_MainThread':
|
|
|
|
|
for s, sh in self.__prev_signals.iteritems():
|
|
|
|
|
signal.signal(s, sh)
|
|
|
|
|
|
|
|
|
|
# Prevent to call quit twice:
|
|
|
|
|
self.quit = lambda: False
|
|
|
|
|
|
|
|
|
|
def addJail(self, name, backend):
|
|
|
|
|
addflg = True
|
|
|
|
|
if self.__reload_state.get(name) and self.__jails.exists(name):
|
|
|
|
@ -561,10 +557,8 @@ class Server:
|
|
|
|
|
self.__logTarget = target
|
|
|
|
|
return True
|
|
|
|
|
# set a format which is simpler for console use
|
|
|
|
|
fmt = "%(asctime)s %(name)-24s[%(process)d]: %(levelname)-7s %(message)s"
|
|
|
|
|
fmt = "%(name)-24s[%(process)d]: %(levelname)-7s %(message)s"
|
|
|
|
|
if systarget == "SYSLOG":
|
|
|
|
|
# Syslog daemons already add date to the message.
|
|
|
|
|
fmt = "%(name)s[%(process)d]: %(levelname)s %(message)s"
|
|
|
|
|
facility = logging.handlers.SysLogHandler.LOG_DAEMON
|
|
|
|
|
if self.__syslogSocket == "auto":
|
|
|
|
|
import platform
|
|
|
|
@ -581,7 +575,7 @@ class Server:
|
|
|
|
|
"Syslog socket file: %s does not exists"
|
|
|
|
|
" or is not a socket" % self.__syslogSocket)
|
|
|
|
|
return False
|
|
|
|
|
elif systarget == "STDOUT":
|
|
|
|
|
elif systarget in ("STDOUT", "SYSOUT"):
|
|
|
|
|
hdlr = logging.StreamHandler(sys.stdout)
|
|
|
|
|
elif systarget == "STDERR":
|
|
|
|
|
hdlr = logging.StreamHandler(sys.stderr)
|
|
|
|
@ -615,8 +609,14 @@ class Server:
|
|
|
|
|
if logger.getEffectiveLevel() <= logging.DEBUG: # pragma: no cover
|
|
|
|
|
if self.__verbose is None:
|
|
|
|
|
self.__verbose = logging.DEBUG - logger.getEffectiveLevel() + 1
|
|
|
|
|
# If handler don't already add date to the message:
|
|
|
|
|
addtime = systarget not in ("SYSLOG", "SYSOUT")
|
|
|
|
|
# verbose log-format:
|
|
|
|
|
if self.__verbose is not None and self.__verbose > 2: # pragma: no cover
|
|
|
|
|
fmt = getVerbosityFormat(self.__verbose-1)
|
|
|
|
|
fmt = getVerbosityFormat(self.__verbose-1,
|
|
|
|
|
addtime=addtime)
|
|
|
|
|
elif addtime:
|
|
|
|
|
fmt = "%(asctime)s " + fmt
|
|
|
|
|
# tell the handler to use this format
|
|
|
|
|
hdlr.setFormatter(logging.Formatter(fmt))
|
|
|
|
|
logger.addHandler(hdlr)
|
|
|
|
|