2011-10-07 19:47:50 +00:00
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 noet :
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
# This file is part of Fail2Ban.
|
|
|
|
#
|
|
|
|
# Fail2Ban is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Fail2Ban is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Fail2Ban; if not, write to the Free Software
|
2011-11-21 12:20:20 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
from threading import Lock, RLock
|
2006-09-17 21:59:29 +00:00
|
|
|
from jails import Jails
|
2006-06-26 20:05:00 +00:00
|
|
|
from transmitter import Transmitter
|
2007-12-14 21:19:00 +00:00
|
|
|
from asyncserver import AsyncServer
|
|
|
|
from asyncserver import AsyncServerException
|
2008-01-13 15:49:23 +00:00
|
|
|
from common import version
|
2006-09-25 17:03:48 +00:00
|
|
|
import logging, logging.handlers, sys, os, signal
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.server")
|
|
|
|
|
|
|
|
class Server:
|
2008-01-20 16:30:35 +00:00
|
|
|
|
2006-08-23 19:53:09 +00:00
|
|
|
def __init__(self, daemon = False):
|
2006-11-06 22:48:46 +00:00
|
|
|
self.__loggingLock = Lock()
|
|
|
|
self.__lock = RLock()
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails = Jails()
|
|
|
|
self.__daemon = daemon
|
|
|
|
self.__transm = Transmitter(self)
|
2007-12-14 21:19:00 +00:00
|
|
|
self.__asyncServer = AsyncServer(self.__transm)
|
2008-05-19 21:05:32 +00:00
|
|
|
self.__logLevel = None
|
|
|
|
self.__logTarget = None
|
2006-06-26 20:05:00 +00:00
|
|
|
# Set logging level
|
2008-05-19 21:05:32 +00:00
|
|
|
self.setLogLevel(3)
|
|
|
|
self.setLogTarget("STDOUT")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-10-30 22:47:30 +00:00
|
|
|
def __sigTERMhandler(self, signum, frame):
|
|
|
|
logSys.debug("Caught signal %d. Exiting" % signum)
|
|
|
|
self.quit()
|
|
|
|
|
2013-02-17 22:14:01 +00:00
|
|
|
def start(self, sock, pidfile, force = False):
|
2008-01-13 15:49:23 +00:00
|
|
|
logSys.info("Starting Fail2ban v" + version.version)
|
2006-10-30 22:47:30 +00:00
|
|
|
|
|
|
|
# Install signal handlers
|
|
|
|
signal.signal(signal.SIGTERM, self.__sigTERMhandler)
|
|
|
|
signal.signal(signal.SIGINT, self.__sigTERMhandler)
|
|
|
|
|
2006-09-17 21:59:29 +00:00
|
|
|
# First set the mask to only allow access to owner
|
|
|
|
os.umask(0077)
|
2013-04-14 14:55:18 +00:00
|
|
|
if self.__daemon: # pragma: no cover
|
2008-05-19 21:05:32 +00:00
|
|
|
logSys.info("Starting in daemon mode")
|
2006-09-19 20:38:32 +00:00
|
|
|
ret = self.__createDaemon()
|
2006-08-23 19:53:09 +00:00
|
|
|
if ret:
|
|
|
|
logSys.info("Daemon started")
|
|
|
|
else:
|
|
|
|
logSys.error("Could not create daemon")
|
|
|
|
raise ServerInitializationError("Could not create daemon")
|
2008-03-13 21:40:16 +00:00
|
|
|
|
|
|
|
# Creates a PID file.
|
|
|
|
try:
|
2013-02-17 22:14:01 +00:00
|
|
|
logSys.debug("Creating PID file %s" % pidfile)
|
|
|
|
pidFile = open(pidfile, 'w')
|
2008-03-13 21:40:16 +00:00
|
|
|
pidFile.write("%s\n" % os.getpid())
|
|
|
|
pidFile.close()
|
|
|
|
except IOError, e:
|
|
|
|
logSys.error("Unable to create PID file: %s" % e)
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
# Start the communication
|
2006-08-23 19:53:09 +00:00
|
|
|
logSys.debug("Starting communication")
|
2006-10-04 22:17:53 +00:00
|
|
|
try:
|
2007-12-14 21:19:00 +00:00
|
|
|
self.__asyncServer.start(sock, force)
|
2007-12-17 20:04:29 +00:00
|
|
|
except AsyncServerException, e:
|
|
|
|
logSys.error("Could not start server: %s", e)
|
2008-01-20 16:30:35 +00:00
|
|
|
# Removes the PID file.
|
|
|
|
try:
|
2013-02-17 22:14:01 +00:00
|
|
|
logSys.debug("Remove PID file %s" % pidfile)
|
|
|
|
os.remove(pidfile)
|
2008-01-20 16:30:35 +00:00
|
|
|
except OSError, e:
|
|
|
|
logSys.error("Unable to remove PID file: %s" % e)
|
2006-08-23 19:53:09 +00:00
|
|
|
logSys.info("Exiting Fail2ban")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2009-08-31 14:14:02 +00:00
|
|
|
def quit(self):
|
2011-11-22 00:36:48 +00:00
|
|
|
# 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
|
|
|
|
# communications first (which should be ok anyways since we
|
|
|
|
# are exiting)
|
|
|
|
# See https://github.com/fail2ban/fail2ban/issues/7
|
2009-08-31 14:14:02 +00:00
|
|
|
self.__asyncServer.stop()
|
2013-02-22 01:59:46 +00:00
|
|
|
|
2011-11-22 00:36:48 +00:00
|
|
|
# Now stop all the jails
|
|
|
|
self.stopAllJail()
|
2013-02-22 01:59:46 +00:00
|
|
|
|
|
|
|
# Only now shutdown the logging.
|
|
|
|
try:
|
|
|
|
self.__loggingLock.acquire()
|
|
|
|
logging.shutdown()
|
|
|
|
finally:
|
|
|
|
self.__loggingLock.release()
|
|
|
|
|
2009-08-31 14:14:02 +00:00
|
|
|
|
2006-10-16 19:42:50 +00:00
|
|
|
def addJail(self, name, backend):
|
|
|
|
self.__jails.add(name, backend)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def delJail(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.remove(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def startJail(self, name):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__lock.acquire()
|
2007-03-26 21:17:31 +00:00
|
|
|
if not self.isAlive(name):
|
2006-11-06 22:48:46 +00:00
|
|
|
self.__jails.get(name).start()
|
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def stopJail(self, name):
|
2011-11-22 00:35:14 +00:00
|
|
|
logSys.debug("Stopping jail %s" % name)
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__lock.acquire()
|
2007-03-26 21:17:31 +00:00
|
|
|
if self.isAlive(name):
|
2006-11-06 22:48:46 +00:00
|
|
|
self.__jails.get(name).stop()
|
|
|
|
self.delJail(name)
|
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-08-20 21:07:28 +00:00
|
|
|
|
|
|
|
def stopAllJail(self):
|
2011-11-22 00:35:14 +00:00
|
|
|
logSys.info("Stopping all jails")
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__lock.acquire()
|
|
|
|
for jail in self.__jails.getAll():
|
|
|
|
self.stopJail(jail)
|
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-08-28 20:22:15 +00:00
|
|
|
|
2007-03-26 21:17:31 +00:00
|
|
|
def isAlive(self, name):
|
|
|
|
return self.__jails.get(name).isAlive()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def setIdleJail(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.get(name).setIdle(value)
|
2006-09-17 21:59:29 +00:00
|
|
|
return True
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getIdleJail(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.get(name).getIdle()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Filter
|
2006-09-17 21:59:29 +00:00
|
|
|
def addIgnoreIP(self, name, ip):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getFilter(name).addIgnoreIP(ip)
|
2006-09-17 21:59:29 +00:00
|
|
|
|
|
|
|
def delIgnoreIP(self, name, ip):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getFilter(name).delIgnoreIP(ip)
|
2006-09-17 21:59:29 +00:00
|
|
|
|
|
|
|
def getIgnoreIP(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getFilter(name).getIgnoreIP()
|
2006-09-17 21:59:29 +00:00
|
|
|
|
2006-10-24 19:40:51 +00:00
|
|
|
def addLogPath(self, name, fileName):
|
2011-11-29 03:22:16 +00:00
|
|
|
self.__jails.getFilter(name).addLogPath(fileName)
|
2006-09-13 21:31:22 +00:00
|
|
|
|
2006-10-24 19:40:51 +00:00
|
|
|
def delLogPath(self, name, fileName):
|
|
|
|
self.__jails.getFilter(name).delLogPath(fileName)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getLogPath(self, name):
|
2008-03-17 23:18:07 +00:00
|
|
|
return [m.getFileName()
|
|
|
|
for m in self.__jails.getFilter(name).getLogPath()]
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-08-28 20:22:15 +00:00
|
|
|
def setFindTime(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getFilter(name).setFindTime(value)
|
2006-08-28 20:22:15 +00:00
|
|
|
|
2006-09-25 17:03:48 +00:00
|
|
|
def getFindTime(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getFilter(name).getFindTime()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-12-23 16:31:00 +00:00
|
|
|
def addFailRegex(self, name, value):
|
|
|
|
self.__jails.getFilter(name).addFailRegex(value)
|
|
|
|
|
|
|
|
def delFailRegex(self, name, index):
|
|
|
|
self.__jails.getFilter(name).delFailRegex(index)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getFailRegex(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getFilter(name).getFailRegex()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-12-23 16:31:00 +00:00
|
|
|
def addIgnoreRegex(self, name, value):
|
|
|
|
self.__jails.getFilter(name).addIgnoreRegex(value)
|
|
|
|
|
|
|
|
def delIgnoreRegex(self, name, index):
|
|
|
|
self.__jails.getFilter(name).delIgnoreRegex(index)
|
2006-11-12 14:52:36 +00:00
|
|
|
|
|
|
|
def getIgnoreRegex(self, name):
|
|
|
|
return self.__jails.getFilter(name).getIgnoreRegex()
|
|
|
|
|
2012-01-13 04:23:41 +00:00
|
|
|
def setUseDns(self, name, value):
|
|
|
|
self.__jails.getFilter(name).setUseDns(value)
|
|
|
|
|
|
|
|
def getUseDns(self, name):
|
|
|
|
return self.__jails.getFilter(name).getUseDns()
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def setMaxRetry(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getFilter(name).setMaxRetry(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def getMaxRetry(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getFilter(name).getMaxRetry()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
# Action
|
|
|
|
def addAction(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).addAction(value)
|
2006-07-08 16:51:14 +00:00
|
|
|
|
|
|
|
def getLastAction(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getLastAction()
|
2006-07-08 16:51:14 +00:00
|
|
|
|
|
|
|
def delAction(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).delAction(value)
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2006-07-16 21:35:08 +00:00
|
|
|
def setCInfo(self, name, action, key, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setCInfo(key, value)
|
2006-07-16 21:35:08 +00:00
|
|
|
|
|
|
|
def getCInfo(self, name, action, key):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getCInfo(key)
|
2006-07-16 21:35:08 +00:00
|
|
|
|
|
|
|
def delCInfo(self, name, action, key):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).delCInfo(key)
|
2006-07-16 21:35:08 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def setBanTime(self, name, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).setBanTime(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2009-08-30 18:26:15 +00:00
|
|
|
def setBanIP(self, name, value):
|
|
|
|
return self.__jails.getFilter(name).addBannedIP(value)
|
|
|
|
|
2012-11-05 23:07:33 +00:00
|
|
|
def setUnbanIP(self, name, value):
|
|
|
|
return self.__jails.getAction(name).removeBannedIP(value)
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def getBanTime(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getBanTime()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setActionStart(self, name, action, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setActionStart(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getActionStart(self, name, action):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getActionStart()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setActionStop(self, name, action, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setActionStop(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getActionStop(self, name, action):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getActionStop()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setActionCheck(self, name, action, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setActionCheck(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getActionCheck(self, name, action):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getActionCheck()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setActionBan(self, name, action, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setActionBan(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getActionBan(self, name, action):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getActionBan()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def setActionUnban(self, name, action, value):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__jails.getAction(name).getAction(action).setActionUnban(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-07-08 16:51:14 +00:00
|
|
|
def getActionUnban(self, name, action):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.getAction(name).getAction(action).getActionUnban()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Status
|
|
|
|
def status(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__lock.acquire()
|
|
|
|
jailList = ''
|
|
|
|
for jail in self.__jails.getAll():
|
|
|
|
jailList += jail + ', '
|
|
|
|
length = len(jailList)
|
|
|
|
if not length == 0:
|
|
|
|
jailList = jailList[:length-2]
|
|
|
|
ret = [("Number of jail", self.__jails.size()),
|
|
|
|
("Jail list", jailList)]
|
|
|
|
return ret
|
|
|
|
finally:
|
|
|
|
self.__lock.release()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def statusJail(self, name):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__jails.get(name).getStatus()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Logging
|
|
|
|
|
|
|
|
##
|
|
|
|
# Set the logging level.
|
|
|
|
#
|
|
|
|
# Incrementing the value gives more messages.
|
|
|
|
# 0 = FATAL
|
|
|
|
# 1 = ERROR
|
|
|
|
# 2 = WARNING
|
|
|
|
# 3 = INFO
|
|
|
|
# 4 = DEBUG
|
|
|
|
# @param value the level
|
|
|
|
|
|
|
|
def setLogLevel(self, value):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__loggingLock.acquire()
|
|
|
|
self.__logLevel = value
|
|
|
|
logLevel = logging.DEBUG
|
|
|
|
if value == 0:
|
|
|
|
logLevel = logging.FATAL
|
|
|
|
elif value == 1:
|
|
|
|
logLevel = logging.ERROR
|
|
|
|
elif value == 2:
|
|
|
|
logLevel = logging.WARNING
|
|
|
|
elif value == 3:
|
|
|
|
logLevel = logging.INFO
|
|
|
|
logging.getLogger("fail2ban").setLevel(logLevel)
|
|
|
|
finally:
|
|
|
|
self.__loggingLock.release()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Get the logging level.
|
|
|
|
#
|
|
|
|
# @see setLogLevel
|
|
|
|
# @return the log level
|
|
|
|
|
|
|
|
def getLogLevel(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__loggingLock.acquire()
|
|
|
|
return self.__logLevel
|
|
|
|
finally:
|
|
|
|
self.__loggingLock.release()
|
2006-08-06 21:24:06 +00:00
|
|
|
|
2006-12-18 22:30:30 +00:00
|
|
|
##
|
|
|
|
# Sets the logging target.
|
|
|
|
#
|
|
|
|
# target can be a file, SYSLOG, STDOUT or STDERR.
|
|
|
|
# @param target the logging target
|
|
|
|
|
2006-08-06 21:24:06 +00:00
|
|
|
def setLogTarget(self, target):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__loggingLock.acquire()
|
2007-11-25 20:51:32 +00:00
|
|
|
# set a format which is simpler for console use
|
|
|
|
formatter = logging.Formatter("%(asctime)s %(name)-16s: %(levelname)-6s %(message)s")
|
2006-11-06 22:48:46 +00:00
|
|
|
if target == "SYSLOG":
|
2007-11-25 20:51:32 +00:00
|
|
|
# Syslog daemons already add date to the message.
|
|
|
|
formatter = logging.Formatter("%(name)-16s: %(levelname)-6s %(message)s")
|
2006-12-13 23:02:46 +00:00
|
|
|
facility = logging.handlers.SysLogHandler.LOG_DAEMON
|
2006-12-18 22:30:30 +00:00
|
|
|
hdlr = logging.handlers.SysLogHandler("/dev/log",
|
2006-12-13 23:02:46 +00:00
|
|
|
facility = facility)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif target == "STDOUT":
|
|
|
|
hdlr = logging.StreamHandler(sys.stdout)
|
|
|
|
elif target == "STDERR":
|
|
|
|
hdlr = logging.StreamHandler(sys.stderr)
|
|
|
|
else:
|
|
|
|
# Target should be a file
|
|
|
|
try:
|
2007-03-22 22:17:41 +00:00
|
|
|
open(target, "a").close()
|
2006-11-06 22:48:46 +00:00
|
|
|
hdlr = logging.FileHandler(target)
|
|
|
|
except IOError:
|
|
|
|
logSys.error("Unable to log to " + target)
|
2006-12-18 22:30:30 +00:00
|
|
|
logSys.info("Logging to previous target " + self.__logTarget)
|
2006-11-06 22:48:46 +00:00
|
|
|
return False
|
2013-02-22 01:58:27 +00:00
|
|
|
# Removes previous handlers -- in reverse order since removeHandler
|
|
|
|
# alter the list in-place and that can confuses the iterable
|
|
|
|
for handler in logging.getLogger("fail2ban").handlers[::-1]:
|
|
|
|
# Remove the handler.
|
2007-03-22 22:17:41 +00:00
|
|
|
logging.getLogger("fail2ban").removeHandler(handler)
|
2013-02-22 01:58:27 +00:00
|
|
|
# And try to close -- it might be closed already
|
|
|
|
try:
|
|
|
|
handler.flush()
|
|
|
|
handler.close()
|
2013-04-14 14:55:18 +00:00
|
|
|
except (ValueError, KeyError): # pragma: no cover
|
2013-05-13 02:55:02 +00:00
|
|
|
# Is known to be thrown after logging was shutdown once
|
2013-02-22 01:58:27 +00:00
|
|
|
# with older Pythons -- seems to be safe to ignore there
|
2013-05-13 02:55:02 +00:00
|
|
|
# At least it was still failing on 2.6.2-0ubuntu1 (jaunty)
|
|
|
|
if sys.version_info >= (2,6,3):
|
|
|
|
raise
|
2006-11-06 22:48:46 +00:00
|
|
|
# tell the handler to use this format
|
|
|
|
hdlr.setFormatter(formatter)
|
|
|
|
logging.getLogger("fail2ban").addHandler(hdlr)
|
2008-05-19 21:05:32 +00:00
|
|
|
# Does not display this message at startup.
|
2013-05-03 03:25:43 +00:00
|
|
|
if not self.__logTarget is None:
|
2008-05-19 21:05:32 +00:00
|
|
|
logSys.info("Changed logging target to %s for Fail2ban v%s" %
|
|
|
|
(target, version.version))
|
|
|
|
# Sets the logging target.
|
|
|
|
self.__logTarget = target
|
2006-11-06 22:48:46 +00:00
|
|
|
return True
|
|
|
|
finally:
|
|
|
|
self.__loggingLock.release()
|
2006-08-06 21:24:06 +00:00
|
|
|
|
|
|
|
def getLogTarget(self):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__loggingLock.acquire()
|
|
|
|
return self.__logTarget
|
|
|
|
finally:
|
|
|
|
self.__loggingLock.release()
|
2006-08-23 19:53:09 +00:00
|
|
|
|
2013-04-14 14:55:18 +00:00
|
|
|
def __createDaemon(self): # pragma: no cover
|
2006-08-23 19:53:09 +00:00
|
|
|
""" Detach a process from the controlling terminal and run it in the
|
|
|
|
background as a daemon.
|
|
|
|
|
|
|
|
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Fork a child process so the parent can exit. This will return control
|
|
|
|
# to the command line or shell. This is required so that the new process
|
|
|
|
# is guaranteed not to be a process group leader. We have this guarantee
|
|
|
|
# because the process GID of the parent is inherited by the child, but
|
|
|
|
# the child gets a new PID, making it impossible for its PID to equal its
|
|
|
|
# PGID.
|
|
|
|
pid = os.fork()
|
|
|
|
except OSError, e:
|
|
|
|
return((e.errno, e.strerror)) # ERROR (return a tuple)
|
|
|
|
|
|
|
|
if pid == 0: # The first child.
|
|
|
|
|
|
|
|
# Next we call os.setsid() to become the session leader of this new
|
|
|
|
# session. The process also becomes the process group leader of the
|
|
|
|
# new process group. Since a controlling terminal is associated with a
|
|
|
|
# session, and this new session has not yet acquired a controlling
|
|
|
|
# terminal our process now has no controlling terminal. This shouldn't
|
|
|
|
# fail, since we're guaranteed that the child is not a process group
|
|
|
|
# leader.
|
|
|
|
os.setsid()
|
|
|
|
|
|
|
|
# When the first child terminates, all processes in the second child
|
|
|
|
# are sent a SIGHUP, so it's ignored.
|
|
|
|
signal.signal(signal.SIGHUP, signal.SIG_IGN)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Fork a second child to prevent zombies. Since the first child is
|
|
|
|
# a session leader without a controlling terminal, it's possible for
|
|
|
|
# it to acquire one by opening a terminal in the future. This second
|
|
|
|
# fork guarantees that the child is no longer a session leader, thus
|
|
|
|
# preventing the daemon from ever acquiring a controlling terminal.
|
|
|
|
pid = os.fork() # Fork a second child.
|
|
|
|
except OSError, e:
|
|
|
|
return((e.errno, e.strerror)) # ERROR (return a tuple)
|
|
|
|
|
|
|
|
if (pid == 0): # The second child.
|
|
|
|
# Ensure that the daemon doesn't keep any directory in use. Failure
|
|
|
|
# to do this could make a filesystem unmountable.
|
|
|
|
os.chdir("/")
|
|
|
|
else:
|
|
|
|
os._exit(0) # Exit parent (the first child) of the second child.
|
|
|
|
else:
|
|
|
|
os._exit(0) # Exit parent of the first child.
|
|
|
|
|
|
|
|
# Close all open files. Try the system configuration variable, SC_OPEN_MAX,
|
|
|
|
# for the maximum number of open files to close. If it doesn't exist, use
|
|
|
|
# the default value (configurable).
|
|
|
|
try:
|
|
|
|
maxfd = os.sysconf("SC_OPEN_MAX")
|
|
|
|
except (AttributeError, ValueError):
|
|
|
|
maxfd = 256 # default maximum
|
|
|
|
|
|
|
|
for fd in range(0, maxfd):
|
|
|
|
try:
|
|
|
|
os.close(fd)
|
|
|
|
except OSError: # ERROR (ignore)
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Redirect the standard file descriptors to /dev/null.
|
2006-10-24 19:40:51 +00:00
|
|
|
os.open("/dev/null", os.O_RDONLY) # standard input (0)
|
2006-08-23 19:53:09 +00:00
|
|
|
os.open("/dev/null", os.O_RDWR) # standard output (1)
|
|
|
|
os.open("/dev/null", os.O_RDWR) # standard error (2)
|
|
|
|
return True
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-08-23 19:53:09 +00:00
|
|
|
class ServerInitializationError(Exception):
|
2006-09-17 21:59:29 +00:00
|
|
|
pass
|