2006-06-26 20:05:00 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# 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
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
2007-10-16 21:01:20 +00:00
|
|
|
# $Revision: 477 $
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2007-10-16 21:01:20 +00:00
|
|
|
__version__ = "$Revision: 477 $"
|
|
|
|
__date__ = "$Date: 2006-12-03 23:01:18 +0100 (Sun, 03 Dec 2006) $"
|
2006-06-26 20:05:00 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2006-09-10 20:46:19 +00:00
|
|
|
import sys, string, os, pickle, re, logging
|
|
|
|
import getopt, time, readline, shlex, socket
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Inserts our own modules path first in the list
|
|
|
|
# fix for bug #343821
|
|
|
|
sys.path.insert(1, "/usr/lib/fail2ban")
|
|
|
|
|
|
|
|
# Now we can import our modules
|
2006-11-16 21:07:42 +00:00
|
|
|
from common.version import version
|
|
|
|
from common.protocol import printFormatted
|
2006-06-26 20:05:00 +00:00
|
|
|
from client.csocket import CSocket
|
|
|
|
from client.configurator import Configurator
|
2006-08-28 20:20:18 +00:00
|
|
|
from client.beautifier import Beautifier
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.client")
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# @todo This class needs cleanup.
|
|
|
|
|
|
|
|
class Fail2banClient:
|
|
|
|
|
2006-09-04 19:20:44 +00:00
|
|
|
prompt = "fail2ban> "
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def __init__(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__argv = None
|
|
|
|
self.__stream = None
|
2006-10-09 18:05:13 +00:00
|
|
|
self.__configurator = Configurator()
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf = dict()
|
|
|
|
self.__conf["conf"] = "/etc/fail2ban"
|
|
|
|
self.__conf["dump"] = False
|
|
|
|
self.__conf["force"] = False
|
2006-11-01 22:03:48 +00:00
|
|
|
self.__conf["verbose"] = 1
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf["interactive"] = False
|
2006-10-09 18:05:13 +00:00
|
|
|
self.__conf["socket"] = None
|
2006-09-12 21:33:00 +00:00
|
|
|
|
|
|
|
def dispVersion(self):
|
|
|
|
print "Fail2Ban v" + version
|
|
|
|
print
|
|
|
|
print "Copyright (c) 2004-2006 Cyril Jaquier"
|
|
|
|
print "Copyright of modifications held by their respective authors."
|
|
|
|
print "Licensed under the GNU General Public License v2 (GPL)."
|
|
|
|
print
|
|
|
|
print "Written by Cyril Jaquier <lostcontrol@users.sourceforge.net>."
|
|
|
|
print "Many contributions by Yaroslav O. Halchenko <debian@onerussian.com>."
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def dispUsage(self):
|
|
|
|
""" Prints Fail2Ban command line options and exits
|
|
|
|
"""
|
2006-09-19 20:38:32 +00:00
|
|
|
print "Usage: "+self.__argv[0]+" [OPTIONS]... <COMMAND>"
|
2006-06-26 20:05:00 +00:00
|
|
|
print
|
2006-07-08 16:51:14 +00:00
|
|
|
print "Fail2Ban v" + version + " reads log file that contains password failure report"
|
2006-06-26 20:05:00 +00:00
|
|
|
print "and bans the corresponding IP addresses using firewall rules."
|
|
|
|
print
|
2006-09-12 21:33:00 +00:00
|
|
|
print "Options:"
|
|
|
|
print " -c <DIR> configuration directory"
|
2006-10-04 22:17:53 +00:00
|
|
|
print " -s <FILE> socket path"
|
2006-09-12 21:33:00 +00:00
|
|
|
print " -d dump configuration. For debugging"
|
|
|
|
print " -i interactive mode"
|
|
|
|
print " -v increase verbosity"
|
|
|
|
print " -q decrease verbosity"
|
|
|
|
print " -x force execution of the server"
|
|
|
|
print " -h, --help display this help message"
|
|
|
|
print " -V, --version print the version"
|
2006-08-22 21:16:23 +00:00
|
|
|
print
|
2006-09-12 21:33:00 +00:00
|
|
|
print "Command:"
|
2006-11-12 21:58:47 +00:00
|
|
|
|
|
|
|
# Prints the protocol
|
|
|
|
printFormatted()
|
|
|
|
|
2006-08-22 21:16:23 +00:00
|
|
|
print
|
2006-06-26 20:05:00 +00:00
|
|
|
print "Report bugs to <lostcontrol@users.sourceforge.net>"
|
|
|
|
|
2006-09-04 19:20:44 +00:00
|
|
|
def dispInteractive(self):
|
|
|
|
print "Fail2Ban v" + version + " reads log file that contains password failure report"
|
|
|
|
print "and bans the corresponding IP addresses using firewall rules."
|
|
|
|
print
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __getCmdLineOptions(self, optList):
|
2006-06-26 20:05:00 +00:00
|
|
|
""" Gets the command line options
|
|
|
|
"""
|
|
|
|
for opt in optList:
|
2006-08-22 22:00:50 +00:00
|
|
|
if opt[0] == "-c":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf["conf"] = opt[1]
|
2006-10-04 22:17:53 +00:00
|
|
|
elif opt[0] == "-s":
|
|
|
|
self.__conf["socket"] = opt[1]
|
2006-08-22 22:00:50 +00:00
|
|
|
elif opt[0] == "-d":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf["dump"] = True
|
2006-08-20 21:59:27 +00:00
|
|
|
elif opt[0] == "-v":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf["verbose"] = self.__conf["verbose"] + 1
|
2006-08-20 21:59:27 +00:00
|
|
|
elif opt[0] == "-q":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf["verbose"] = self.__conf["verbose"] - 1
|
2006-08-20 21:59:27 +00:00
|
|
|
elif opt[0] == "-x":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf["force"] = True
|
2006-09-04 19:20:44 +00:00
|
|
|
elif opt[0] == "-i":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf["interactive"] = True
|
2006-08-20 21:59:27 +00:00
|
|
|
elif opt[0] in ["-h", "--help"]:
|
2006-06-26 20:05:00 +00:00
|
|
|
self.dispUsage()
|
2006-08-22 21:16:23 +00:00
|
|
|
sys.exit(0)
|
2006-09-12 21:33:00 +00:00
|
|
|
elif opt[0] in ["-V", "--version"]:
|
|
|
|
self.dispVersion()
|
|
|
|
sys.exit(0)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __ping(self):
|
2006-10-04 22:17:53 +00:00
|
|
|
return self.__processCmd([["ping"]], False)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-10-04 22:17:53 +00:00
|
|
|
def __processCmd(self, cmd, showRet = True):
|
2006-08-28 20:20:18 +00:00
|
|
|
beautifier = Beautifier()
|
2006-06-26 20:05:00 +00:00
|
|
|
for c in cmd:
|
2006-08-28 20:20:18 +00:00
|
|
|
beautifier.setInputCmd(c)
|
2006-06-26 20:05:00 +00:00
|
|
|
try:
|
2006-10-04 22:17:53 +00:00
|
|
|
client = CSocket(self.__conf["socket"])
|
2006-09-10 20:46:19 +00:00
|
|
|
ret = client.send(c)
|
|
|
|
if ret[0] == 0:
|
|
|
|
logSys.debug("OK : " + `ret[1]`)
|
|
|
|
if showRet:
|
|
|
|
print beautifier.beautify(ret[1])
|
|
|
|
else:
|
|
|
|
logSys.debug("NOK: " + `ret[1].args`)
|
|
|
|
print beautifier.beautifyError(ret[1])
|
|
|
|
return False
|
|
|
|
except socket.error:
|
2006-09-10 21:06:15 +00:00
|
|
|
if showRet:
|
|
|
|
logSys.error("Unable to contact server. Is it running?")
|
2006-09-10 20:46:19 +00:00
|
|
|
return False
|
2006-06-26 20:05:00 +00:00
|
|
|
except Exception, e:
|
2006-08-20 21:59:27 +00:00
|
|
|
if showRet:
|
|
|
|
logSys.error(e)
|
2006-06-26 20:05:00 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
##
|
|
|
|
# Process a command line.
|
|
|
|
#
|
|
|
|
# Process one command line and exit.
|
|
|
|
# @param cmd the command line
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __processCommand(self, cmd):
|
2006-08-20 21:59:27 +00:00
|
|
|
if len(cmd) == 1 and cmd[0] == "start":
|
2006-09-19 20:38:32 +00:00
|
|
|
if self.__ping():
|
2006-11-01 22:03:48 +00:00
|
|
|
logSys.error("Server already running")
|
2006-08-17 22:50:20 +00:00
|
|
|
return False
|
|
|
|
else:
|
2006-11-26 22:22:54 +00:00
|
|
|
self.__startServerAsync(self.__conf["socket"],
|
|
|
|
self.__conf["force"])
|
2006-08-17 22:50:20 +00:00
|
|
|
# Read the config while the server is starting
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__readConfig()
|
2006-08-17 22:50:20 +00:00
|
|
|
try:
|
|
|
|
# Wait for the server to start
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__waitOnServer()
|
2006-08-17 22:50:20 +00:00
|
|
|
# Configure the server
|
2006-10-04 22:17:53 +00:00
|
|
|
self.__processCmd(self.__stream, False)
|
2006-06-26 20:05:00 +00:00
|
|
|
return True
|
2006-08-17 22:50:20 +00:00
|
|
|
except ServerExecutionException:
|
|
|
|
logSys.error("Could not start server. Try -x option")
|
2006-06-26 20:05:00 +00:00
|
|
|
return False
|
2006-08-20 21:59:27 +00:00
|
|
|
elif len(cmd) == 1 and cmd[0] == "reload":
|
2006-09-19 20:38:32 +00:00
|
|
|
if self.__ping():
|
|
|
|
self.__readConfig()
|
2006-10-04 22:17:53 +00:00
|
|
|
self.__processCmd([['stop', 'all']], False)
|
2006-08-20 21:07:28 +00:00
|
|
|
# Configure the server
|
2006-10-04 22:17:53 +00:00
|
|
|
return self.__processCmd(self.__stream, False)
|
2006-08-20 21:07:28 +00:00
|
|
|
else:
|
|
|
|
logSys.error("Could not find server")
|
2006-08-22 21:16:23 +00:00
|
|
|
return False
|
2006-08-17 22:50:20 +00:00
|
|
|
else:
|
2006-10-04 22:17:53 +00:00
|
|
|
return self.__processCmd([cmd])
|
2006-08-17 22:50:20 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Start Fail2Ban server.
|
|
|
|
#
|
|
|
|
# Start the Fail2ban server in daemon mode.
|
|
|
|
|
2006-11-26 22:22:54 +00:00
|
|
|
def __startServerAsync(self, socket, force = False):
|
2006-12-03 22:01:18 +00:00
|
|
|
# Forks the current process.
|
2006-06-26 20:05:00 +00:00
|
|
|
pid = os.fork()
|
|
|
|
if pid == 0:
|
2006-12-03 22:01:18 +00:00
|
|
|
args = list()
|
|
|
|
args.append("fail2ban-server")
|
|
|
|
# Start in background mode.
|
|
|
|
args.append("-b")
|
|
|
|
# Set the socket path.
|
|
|
|
args.append("-s")
|
|
|
|
args.append(socket)
|
|
|
|
# Force the execution if needed.
|
|
|
|
if force:
|
|
|
|
args.append("-x")
|
2006-08-23 20:58:10 +00:00
|
|
|
try:
|
|
|
|
# Use the PATH env
|
|
|
|
os.execvp("fail2ban-server", args)
|
|
|
|
except OSError:
|
|
|
|
try:
|
|
|
|
# Use the current directory
|
|
|
|
os.execv("fail2ban-server", args)
|
|
|
|
except OSError:
|
|
|
|
print "Could not find fail2ban-server"
|
|
|
|
os.exit(-1)
|
2006-08-17 22:50:20 +00:00
|
|
|
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __waitOnServer(self):
|
2006-08-17 22:50:20 +00:00
|
|
|
# Wait for the server to start
|
|
|
|
cnt = 0
|
2006-09-19 20:38:32 +00:00
|
|
|
while not self.__ping():
|
2006-12-03 22:01:18 +00:00
|
|
|
# The server has 30 secondes to start.
|
|
|
|
if cnt >= 300:
|
2006-08-17 22:50:20 +00:00
|
|
|
raise ServerExecutionException("Failed to start server")
|
|
|
|
time.sleep(0.1)
|
2006-12-03 22:01:18 +00:00
|
|
|
cnt += 1
|
2006-08-17 22:50:20 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def start(self, argv):
|
|
|
|
# Command line options
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__argv = argv
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Reads the command line options.
|
|
|
|
try:
|
2006-10-04 22:17:53 +00:00
|
|
|
cmdOpts = 'hc:s:xdviqV'
|
2006-09-12 21:33:00 +00:00
|
|
|
cmdLongOpts = ['help', 'version']
|
2006-09-19 20:38:32 +00:00
|
|
|
optList, args = getopt.getopt(self.__argv[1:], cmdOpts, cmdLongOpts)
|
2006-06-26 20:05:00 +00:00
|
|
|
except getopt.GetoptError:
|
|
|
|
self.dispUsage()
|
2006-08-28 21:33:54 +00:00
|
|
|
return False
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__getCmdLineOptions(optList)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
verbose = self.__conf["verbose"]
|
2006-08-20 21:59:27 +00:00
|
|
|
if verbose <= 0:
|
|
|
|
logSys.setLevel(logging.ERROR)
|
|
|
|
elif verbose == 1:
|
|
|
|
logSys.setLevel(logging.WARN)
|
|
|
|
elif verbose == 2:
|
|
|
|
logSys.setLevel(logging.INFO)
|
|
|
|
else:
|
|
|
|
logSys.setLevel(logging.DEBUG)
|
2006-06-26 20:05:00 +00:00
|
|
|
# Add the default logging handler
|
|
|
|
stdout = logging.StreamHandler(sys.stdout)
|
|
|
|
# set a format which is simpler for console use
|
2006-08-20 21:59:27 +00:00
|
|
|
formatter = logging.Formatter('%(levelname)-6s %(message)s')
|
2006-06-26 20:05:00 +00:00
|
|
|
# tell the handler to use this format
|
|
|
|
stdout.setFormatter(formatter)
|
|
|
|
logSys.addHandler(stdout)
|
|
|
|
|
2006-10-09 18:05:13 +00:00
|
|
|
# Set the configuration path
|
|
|
|
self.__configurator.setBaseDir(self.__conf["conf"])
|
|
|
|
|
|
|
|
# Set socket path
|
|
|
|
self.__configurator.readEarly()
|
|
|
|
socket = self.__configurator.getEarlyOptions()
|
|
|
|
if self.__conf["socket"] == None:
|
|
|
|
self.__conf["socket"] = socket["socket"]
|
2006-11-01 22:03:48 +00:00
|
|
|
logSys.info("Using socket file " + self.__conf["socket"])
|
2006-10-09 18:05:13 +00:00
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
if self.__conf["dump"]:
|
|
|
|
self.__readConfig()
|
|
|
|
self.dumpConfig(self.__stream)
|
2006-09-04 19:20:44 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
# Interactive mode
|
2006-09-19 20:38:32 +00:00
|
|
|
if self.__conf["interactive"]:
|
2006-09-04 19:20:44 +00:00
|
|
|
try:
|
|
|
|
ret = True
|
|
|
|
if len(args) > 0:
|
2006-09-19 20:38:32 +00:00
|
|
|
ret = self.__processCommand(args)
|
2006-09-04 19:20:44 +00:00
|
|
|
if ret:
|
|
|
|
readline.parse_and_bind("tab: complete")
|
|
|
|
self.dispInteractive()
|
|
|
|
while True:
|
|
|
|
cmd = raw_input(self.prompt)
|
|
|
|
if cmd == "exit" or cmd == "quit":
|
|
|
|
# Exit
|
|
|
|
return True
|
|
|
|
if not cmd == "":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__processCommand(shlex.split(cmd))
|
2006-09-04 19:20:44 +00:00
|
|
|
except (EOFError, KeyboardInterrupt):
|
|
|
|
print
|
|
|
|
return True
|
|
|
|
# Single command mode
|
|
|
|
else:
|
|
|
|
if len(args) < 1:
|
|
|
|
self.dispUsage()
|
|
|
|
return False
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__processCommand(args)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __readConfig(self):
|
2006-06-26 20:05:00 +00:00
|
|
|
# Read the configuration
|
2006-10-09 18:05:13 +00:00
|
|
|
self.__configurator.readAll()
|
|
|
|
self.__configurator.getAllOptions()
|
|
|
|
self.__configurator.convertToProtocol()
|
|
|
|
self.__stream = self.__configurator.getConfigStream()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def dumpConfig(cmd):
|
|
|
|
for c in cmd:
|
|
|
|
print c
|
|
|
|
return True
|
|
|
|
|
2006-08-17 22:50:20 +00:00
|
|
|
|
|
|
|
class ServerExecutionException(Exception):
|
|
|
|
pass
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
client = Fail2banClient()
|
2006-08-22 21:16:23 +00:00
|
|
|
# Exit with correct return value
|
|
|
|
if client.start(sys.argv):
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
sys.exit(-1)
|