2007-01-21 22:19:57 +00:00
|
|
|
#!/usr/bin/python
|
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"
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2007-01-04 23:34:35 +00:00
|
|
|
import sys, string, os, pickle, re, logging, signal
|
2008-03-05 22:49:29 +00:00
|
|
|
import getopt, time, shlex, socket
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Inserts our own modules path first in the list
|
|
|
|
# fix for bug #343821
|
2013-01-28 14:54:08 +00:00
|
|
|
try:
|
|
|
|
from common.version import version
|
|
|
|
except ImportError, e:
|
2012-11-09 13:57:33 +00:00
|
|
|
sys.path.insert(1, "/usr/share/fail2ban")
|
2013-01-28 14:54:08 +00:00
|
|
|
from common.version import version
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2013-01-28 14:54:08 +00:00
|
|
|
# Now we can import the rest of modules
|
2006-11-16 21:07:42 +00:00
|
|
|
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:
|
|
|
|
|
2008-03-04 00:13:39 +00:00
|
|
|
SERVER = "fail2ban-server"
|
|
|
|
PROMPT = "fail2ban> "
|
2006-09-04 19:20:44 +00:00
|
|
|
|
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
|
2013-02-17 22:14:01 +00:00
|
|
|
self.__conf["pidfile"] = None
|
2006-09-12 21:33:00 +00:00
|
|
|
|
|
|
|
def dispVersion(self):
|
|
|
|
print "Fail2Ban v" + version
|
|
|
|
print
|
2012-11-09 13:58:19 +00:00
|
|
|
print "Copyright (c) 2004-2008 Cyril Jaquier, 2008- Fail2Ban Contributors"
|
2006-09-12 21:33:00 +00:00
|
|
|
print "Copyright of modifications held by their respective authors."
|
|
|
|
print "Licensed under the GNU General Public License v2 (GPL)."
|
|
|
|
print
|
2008-03-05 23:18:06 +00:00
|
|
|
print "Written by Cyril Jaquier <cyril.jaquier@fail2ban.org>."
|
2006-09-12 21:33:00 +00:00
|
|
|
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
|
|
|
|
"""
|
2007-01-29 20:27:01 +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"
|
2013-02-17 22:14:01 +00:00
|
|
|
print " -p <FILE> pidfile 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"
|
2007-01-04 23:34:35 +00:00
|
|
|
print " -x force execution of the server (remove socket file)"
|
2006-09-12 21:33:00 +00:00
|
|
|
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
|
2012-07-19 18:51:21 +00:00
|
|
|
print "Report bugs to https://github.com/fail2ban/fail2ban/issues"
|
2006-06-26 20:05:00 +00:00
|
|
|
|
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
|
2007-01-04 23:34:35 +00:00
|
|
|
|
|
|
|
def __sigTERMhandler(self, signum, frame):
|
|
|
|
# Print a new line because we probably come from wait
|
|
|
|
print
|
|
|
|
logSys.warn("Caught signal %d. Exiting" % signum)
|
|
|
|
sys.exit(-1)
|
|
|
|
|
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]
|
2013-02-17 22:14:01 +00:00
|
|
|
elif opt[0] == "-p":
|
|
|
|
self.__conf["pidfile"] = 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"]:
|
2013-02-19 04:08:44 +00:00
|
|
|
self.dispUsage()
|
|
|
|
sys.exit(0)
|
|
|
|
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:
|
2007-01-08 21:15:47 +00:00
|
|
|
# Read the config
|
|
|
|
ret = self.__readConfig()
|
|
|
|
# Do not continue if configuration is not 100% valid
|
|
|
|
if not ret:
|
|
|
|
return False
|
2013-02-19 04:08:44 +00:00
|
|
|
# verify that directory for the socket file exists
|
|
|
|
socket_dir = os.path.dirname(self.__conf["socket"])
|
|
|
|
if not os.path.exists(socket_dir):
|
|
|
|
logSys.error(
|
|
|
|
"There is no directory %s to contain the socket file %s."
|
|
|
|
% (socket_dir, self.__conf["socket"]))
|
|
|
|
return False
|
|
|
|
if not os.access(socket_dir, os.W_OK | os.X_OK):
|
|
|
|
logSys.error(
|
|
|
|
"Directory %s exists but not accessible for writing"
|
|
|
|
% (socket_dir,))
|
|
|
|
return False
|
2007-01-08 21:15:47 +00:00
|
|
|
# Start the server
|
2006-11-26 22:22:54 +00:00
|
|
|
self.__startServerAsync(self.__conf["socket"],
|
2013-02-17 22:14:01 +00:00
|
|
|
self.__conf["pidfile"],
|
2006-11-26 22:22:54 +00:00
|
|
|
self.__conf["force"])
|
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:
|
2013-02-19 04:08:44 +00:00
|
|
|
logSys.error("Could not start server. Maybe an old "
|
|
|
|
"socket file is still present. Try to "
|
|
|
|
"remove " + self.__conf["socket"] + ". If "
|
|
|
|
"you used fail2ban-client to start the "
|
2007-01-04 23:34:35 +00:00
|
|
|
"server, adding the -x option will do it")
|
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():
|
2007-01-08 21:15:47 +00:00
|
|
|
ret = self.__readConfig()
|
|
|
|
# Do not continue if configuration is not 100% valid
|
|
|
|
if not ret:
|
|
|
|
return False
|
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
|
2008-03-04 00:13:39 +00:00
|
|
|
elif len(cmd) == 2 and cmd[0] == "reload":
|
|
|
|
if self.__ping():
|
|
|
|
jail = cmd[1]
|
|
|
|
ret = self.__readJailConfig(jail)
|
|
|
|
# Do not continue if configuration is not 100% valid
|
|
|
|
if not ret:
|
|
|
|
return False
|
|
|
|
self.__processCmd([['stop', jail]], False)
|
|
|
|
# Configure the server
|
|
|
|
return self.__processCmd(self.__stream, False)
|
|
|
|
else:
|
|
|
|
logSys.error("Could not find server")
|
|
|
|
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.
|
|
|
|
|
2013-02-17 22:14:01 +00:00
|
|
|
def __startServerAsync(self, socket, pidfile, 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()
|
2008-03-04 00:13:39 +00:00
|
|
|
args.append(self.SERVER)
|
2006-12-03 22:01:18 +00:00
|
|
|
# Start in background mode.
|
|
|
|
args.append("-b")
|
|
|
|
# Set the socket path.
|
|
|
|
args.append("-s")
|
|
|
|
args.append(socket)
|
2013-02-17 22:14:01 +00:00
|
|
|
# Set the pidfile
|
|
|
|
args.append("-p")
|
|
|
|
args.append(pidfile)
|
2006-12-03 22:01:18 +00:00
|
|
|
# Force the execution if needed.
|
|
|
|
if force:
|
|
|
|
args.append("-x")
|
2006-08-23 20:58:10 +00:00
|
|
|
try:
|
2008-03-04 00:13:39 +00:00
|
|
|
# Use the current directory.
|
|
|
|
exe = os.path.abspath(os.path.join(sys.path[0], self.SERVER))
|
2013-02-19 04:08:44 +00:00
|
|
|
logSys.debug("Starting %r with args %r" % (exe, args))
|
2008-03-04 00:13:39 +00:00
|
|
|
os.execv(exe, args)
|
2006-08-23 20:58:10 +00:00
|
|
|
except OSError:
|
|
|
|
try:
|
2008-03-04 00:13:39 +00:00
|
|
|
# Use the PATH env.
|
2013-02-19 04:08:44 +00:00
|
|
|
logSys.warning("Initial start attempt failed. Starting %r with the same args" % (self.SERVER,))
|
2008-03-04 00:13:39 +00:00
|
|
|
os.execvp(self.SERVER, args)
|
2006-08-23 20:58:10 +00:00
|
|
|
except OSError:
|
2013-02-19 04:08:44 +00:00
|
|
|
logSys.error("Could not start %s" % self.SERVER)
|
2006-08-23 20:58:10 +00:00
|
|
|
os.exit(-1)
|
2013-02-19 04:08:44 +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
|
2007-01-04 23:34:35 +00:00
|
|
|
if self.__conf["verbose"] > 1:
|
|
|
|
pos = 0
|
|
|
|
delta = 1
|
|
|
|
mask = "[ ]"
|
2006-09-19 20:38:32 +00:00
|
|
|
while not self.__ping():
|
2007-01-04 23:34:35 +00:00
|
|
|
# Wonderful visual :)
|
|
|
|
if self.__conf["verbose"] > 1:
|
|
|
|
pos += delta
|
|
|
|
sys.stdout.write("\rINFO " + mask[:pos] + '#' + mask[pos+1:] +
|
|
|
|
" Waiting on the server...")
|
|
|
|
sys.stdout.flush()
|
|
|
|
if pos > len(mask)-3:
|
|
|
|
delta = -1
|
|
|
|
elif pos < 2:
|
|
|
|
delta = 1
|
2006-12-03 22:01:18 +00:00
|
|
|
# The server has 30 secondes to start.
|
|
|
|
if cnt >= 300:
|
2007-01-04 23:34:35 +00:00
|
|
|
if self.__conf["verbose"] > 1:
|
|
|
|
sys.stdout.write('\n')
|
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
|
2007-01-04 23:34:35 +00:00
|
|
|
if self.__conf["verbose"] > 1:
|
|
|
|
sys.stdout.write('\n')
|
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
|
|
|
|
2007-01-04 23:34:35 +00:00
|
|
|
# Install signal handlers
|
|
|
|
signal.signal(signal.SIGTERM, self.__sigTERMhandler)
|
|
|
|
signal.signal(signal.SIGINT, self.__sigTERMhandler)
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
# Reads the command line options.
|
|
|
|
try:
|
2013-02-17 22:14:01 +00:00
|
|
|
cmdOpts = 'hc:s:p: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()
|
2013-02-17 22:14:01 +00:00
|
|
|
conf = self.__configurator.getEarlyOptions()
|
2006-10-09 18:05:13 +00:00
|
|
|
if self.__conf["socket"] == None:
|
2013-02-17 22:14:01 +00:00
|
|
|
self.__conf["socket"] = conf["socket"]
|
|
|
|
if self.__conf["pidfile"] == None:
|
|
|
|
self.__conf["pidfile"] = conf["pidfile"]
|
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"]:
|
2007-01-08 21:15:47 +00:00
|
|
|
ret = self.__readConfig()
|
2006-09-19 20:38:32 +00:00
|
|
|
self.dumpConfig(self.__stream)
|
2007-01-08 21:15:47 +00:00
|
|
|
return ret
|
2006-09-04 19:20:44 +00:00
|
|
|
|
|
|
|
# Interactive mode
|
2006-09-19 20:38:32 +00:00
|
|
|
if self.__conf["interactive"]:
|
2008-03-05 22:49:29 +00:00
|
|
|
try:
|
|
|
|
import readline
|
|
|
|
except ImportError:
|
|
|
|
logSys.error("Readline not available")
|
|
|
|
return False
|
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:
|
2008-03-04 00:13:39 +00:00
|
|
|
cmd = raw_input(self.PROMPT)
|
2006-09-04 19:20:44 +00:00
|
|
|
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()
|
2008-03-04 00:13:39 +00:00
|
|
|
ret = self.__configurator.getOptions()
|
|
|
|
self.__configurator.convertToProtocol()
|
|
|
|
self.__stream = self.__configurator.getConfigStream()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def __readJailConfig(self, jail):
|
|
|
|
self.__configurator.readAll()
|
|
|
|
ret = self.__configurator.getOptions(jail)
|
2006-10-09 18:05:13 +00:00
|
|
|
self.__configurator.convertToProtocol()
|
|
|
|
self.__stream = self.__configurator.getConfigStream()
|
2007-01-08 21:15:47 +00:00
|
|
|
return ret
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2007-12-16 21:38:04 +00:00
|
|
|
#@staticmethod
|
2006-06-26 20:05:00 +00:00
|
|
|
def dumpConfig(cmd):
|
|
|
|
for c in cmd:
|
|
|
|
print c
|
|
|
|
return True
|
2007-12-16 21:38:04 +00:00
|
|
|
dumpConfig = staticmethod(dumpConfig)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-08-17 22:50:20 +00:00
|
|
|
|
|
|
|
class ServerExecutionException(Exception):
|
|
|
|
pass
|
|
|
|
|
2013-03-10 04:18:42 +00:00
|
|
|
if __name__ == "__main__": # pragma: no cover - can't test main
|
2006-06-26 20:05:00 +00:00
|
|
|
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)
|