"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "fail2ban/server/server.py" between
fail2ban-0.10.5.tar.gz and fail2ban-0.11.1.tar.gz

About:

server.py  (fail2ban-0.10.5):server.py  (fail2ban-0.11.1)
skipping to change at line 35 skipping to change at line 35
__license__ = "GPL" __license__ = "GPL"
import threading import threading
from threading import Lock, RLock from threading import Lock, RLock
import logging import logging
import os import os
import signal import signal
import stat import stat
import sys import sys
from .observer import Observers, ObserverThread
from .jails import Jails from .jails import Jails
from .filter import FileFilter, JournalFilter from .filter import FileFilter, JournalFilter
from .transmitter import Transmitter from .transmitter import Transmitter
from .asyncserver import AsyncServer, AsyncServerException from .asyncserver import AsyncServer, AsyncServerException
from .. import version from .. import version
from ..helpers import getLogger, _as_bool, extractOptions, str2LogLevel, \ from ..helpers import getLogger, _as_bool, extractOptions, str2LogLevel, \
getVerbosityFormat, excepthook, prctl_set_th_name getVerbosityFormat, excepthook, prctl_set_th_name
# Gets the instance of the logger. # Gets the instance of the logger.
logSys = getLogger(__name__) logSys = getLogger(__name__)
skipping to change at line 97 skipping to change at line 98
def __sigUSR1handler(self, signum, fname): # pragma: no cover - indirect tested def __sigUSR1handler(self, signum, fname): # pragma: no cover - indirect tested
logSys.debug("Caught signal %d. Flushing logs", signum) logSys.debug("Caught signal %d. Flushing logs", signum)
self.flushLogs() self.flushLogs()
def _rebindSignal(self, s, new): def _rebindSignal(self, s, new):
"""Bind new signal handler while storing old one in _prev_signals """ """Bind new signal handler while storing old one in _prev_signals """
self.__prev_signals[s] = signal.getsignal(s) self.__prev_signals[s] = signal.getsignal(s)
signal.signal(s, new) signal.signal(s, new)
def start(self, sock, pidfile, force=False, conf={}): def start(self, sock, pidfile, force=False, observer=True, conf={}):
# First set the mask to only allow access to owner # First set the mask to only allow access to owner
os.umask(0077) os.umask(0077)
# Second daemonize before logging etc, because it will close all handles: # Second daemonize before logging etc, because it will close all handles:
if self.__daemon: # pragma: no cover if self.__daemon: # pragma: no cover
logSys.info("Starting in daemon mode") logSys.info("Starting in daemon mode")
ret = self.__createDaemon() ret = self.__createDaemon()
# If forked parent - return here (parent process will con figure server later): # If forked parent - return here (parent process will con figure server later):
if ret is None: if ret is None:
return False return False
# If error: # If error:
skipping to change at line 147 skipping to change at line 148
# Creates a PID file. # Creates a PID file.
try: try:
logSys.debug("Creating PID file %s", pidfile) logSys.debug("Creating PID file %s", pidfile)
pidFile = open(pidfile, 'w') pidFile = open(pidfile, 'w')
pidFile.write("%s\n" % os.getpid()) pidFile.write("%s\n" % os.getpid())
pidFile.close() pidFile.close()
except (OSError, IOError) as e: # pragma: no cover except (OSError, IOError) as e: # pragma: no cover
logSys.error("Unable to create PID file: %s", e) logSys.error("Unable to create PID file: %s", e)
# Create observers and start it:
if observer:
if Observers.Main is None:
Observers.Main = ObserverThread()
Observers.Main.start()
# Start the communication # Start the communication
logSys.debug("Starting communication") logSys.debug("Starting communication")
try: try:
self.__asyncServer = AsyncServer(self.__transm) self.__asyncServer = AsyncServer(self.__transm)
self.__asyncServer.onstart = conf.get('onstart') self.__asyncServer.onstart = conf.get('onstart')
self.__asyncServer.start(sock, force) self.__asyncServer.start(sock, force)
except AsyncServerException as e: except AsyncServerException as e:
logSys.error("Could not start server: %s", e) logSys.error("Could not start server: %s", e)
# Stop (if not yet already executed): # Stop (if not yet already executed):
skipping to change at line 186 skipping to change at line 193
# are exiting) # are exiting)
# See https://github.com/fail2ban/fail2ban/issues/7 # See https://github.com/fail2ban/fail2ban/issues/7
if self.__asyncServer is not None: if self.__asyncServer is not None:
self.__asyncServer.stop_communication() self.__asyncServer.stop_communication()
# Restore default signal handlers: # Restore default signal handlers:
if _thread_name() == '_MainThread': if _thread_name() == '_MainThread':
for s, sh in self.__prev_signals.iteritems(): for s, sh in self.__prev_signals.iteritems():
signal.signal(s, sh) signal.signal(s, sh)
# Give observer a small chance to complete its work before exit
if Observers.Main is not None:
Observers.Main.stop()
# Now stop all the jails # Now stop all the jails
self.stopAllJail() self.stopAllJail()
# Explicit close database (server can leave in a thread, # Explicit close database (server can leave in a thread,
# so delayed GC can prevent commiting changes) # so delayed GC can prevent commiting changes)
if self.__db: if self.__db:
self.__db.close() self.__db.close()
self.__db = None self.__db = None
# Stop observer and exit
if Observers.Main is not None:
Observers.Main.stop()
Observers.Main = None
# Stop async # Stop async
if self.__asyncServer is not None: if self.__asyncServer is not None:
self.__asyncServer.stop() self.__asyncServer.stop()
self.__asyncServer = None self.__asyncServer = None
logSys.info("Exiting Fail2ban") logSys.info("Exiting Fail2ban")
def addJail(self, name, backend): def addJail(self, name, backend):
addflg = True addflg = True
if self.__reload_state.get(name) and self.__jails.exists(name): if self.__reload_state.get(name) and self.__jails.exists(name):
jail = self.__jails[name] jail = self.__jails[name]
skipping to change at line 505 skipping to change at line 520
# unban given or all (if value is None): # unban given or all (if value is None):
cnt = 0 cnt = 0
ifexists |= (name is None) ifexists |= (name is None)
for jail in jails: for jail in jails:
cnt += jail.actions.removeBannedIP(value, ifexists=ifexis ts) cnt += jail.actions.removeBannedIP(value, ifexists=ifexis ts)
return cnt return cnt
def getBanTime(self, name): def getBanTime(self, name):
return self.__jails[name].actions.getBanTime() return self.__jails[name].actions.getBanTime()
def getBanList(self, name, withTime=False):
"""Returns the list of banned IP addresses for a jail.
Parameters
----------
name : str
The name of a jail.
Returns
-------
list
The list of banned IP addresses.
"""
return self.__jails[name].actions.getBanList(withTime)
def setBanTimeExtra(self, name, opt, value):
self.__jails[name].setBanTimeExtra(opt, value)
def getBanTimeExtra(self, name, opt):
return self.__jails[name].getBanTimeExtra(opt)
def isStarted(self): def isStarted(self):
return self.__asyncServer is not None and self.__asyncServer.isAc tive() return self.__asyncServer is not None and self.__asyncServer.isAc tive()
def isAlive(self, jailnum=None): def isAlive(self, jailnum=None):
if jailnum is not None and len(self.__jails) != jailnum: if jailnum is not None and len(self.__jails) != jailnum:
return 0 return 0
for jail in self.__jails.values(): for jail in self.__jails.values():
if not jail.isAlive(): if not jail.isAlive():
return 0 return 0
return 1 return 1
skipping to change at line 747 skipping to change at line 783
if filename.lower() == "none": if filename.lower() == "none":
self.__db = None self.__db = None
else: else:
if Fail2BanDb is not None: if Fail2BanDb is not None:
self.__db = Fail2BanDb(filename) self.__db = Fail2BanDb(filename)
self.__db.delAllJails() self.__db.delAllJails()
else: # pragma: no cover else: # pragma: no cover
logSys.error( logSys.error(
"Unable to import fail2ban database modul e as sqlite " "Unable to import fail2ban database modul e as sqlite "
"is not available.") "is not available.")
if Observers.Main is not None:
Observers.Main.db_set(self.__db)
def getDatabase(self): def getDatabase(self):
return self.__db return self.__db
def __createDaemon(self): # pragma: no cover def __createDaemon(self): # pragma: no cover
""" Detach a process from the controlling terminal and run it in the """ Detach a process from the controlling terminal and run it in the
background as a daemon. background as a daemon.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/2 78731 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/2 78731
""" """
 End of changes. 7 change blocks. 
1 lines changed or deleted 39 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)