|
|
|
@ -25,7 +25,7 @@ __date__ = "$Date$"
|
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier" |
|
|
|
|
__license__ = "GPL" |
|
|
|
|
|
|
|
|
|
import sys, string, os, pickle, re, logging |
|
|
|
|
import sys, string, os, pickle, re, logging, signal |
|
|
|
|
import getopt, time, readline, shlex, socket |
|
|
|
|
|
|
|
|
|
# Inserts our own modules path first in the list |
|
|
|
@ -87,7 +87,7 @@ class Fail2banClient:
|
|
|
|
|
print " -i interactive mode" |
|
|
|
|
print " -v increase verbosity" |
|
|
|
|
print " -q decrease verbosity" |
|
|
|
|
print " -x force execution of the server" |
|
|
|
|
print " -x force execution of the server (remove socket file)" |
|
|
|
|
print " -h, --help display this help message" |
|
|
|
|
print " -V, --version print the version" |
|
|
|
|
print |
|
|
|
@ -103,7 +103,13 @@ class Fail2banClient:
|
|
|
|
|
print "Fail2Ban v" + version + " reads log file that contains password failure report" |
|
|
|
|
print "and bans the corresponding IP addresses using firewall rules." |
|
|
|
|
print |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
def __getCmdLineOptions(self, optList): |
|
|
|
|
""" Gets the command line options |
|
|
|
|
""" |
|
|
|
@ -180,7 +186,11 @@ class Fail2banClient:
|
|
|
|
|
self.__processCmd(self.__stream, False) |
|
|
|
|
return True |
|
|
|
|
except ServerExecutionException: |
|
|
|
|
logSys.error("Could not start server. Try -x option") |
|
|
|
|
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 " + |
|
|
|
|
"server, adding the -x option will do it") |
|
|
|
|
return False |
|
|
|
|
elif len(cmd) == 1 and cmd[0] == "reload": |
|
|
|
|
if self.__ping(): |
|
|
|
@ -229,18 +239,40 @@ class Fail2banClient:
|
|
|
|
|
def __waitOnServer(self): |
|
|
|
|
# Wait for the server to start |
|
|
|
|
cnt = 0 |
|
|
|
|
if self.__conf["verbose"] > 1: |
|
|
|
|
pos = 0 |
|
|
|
|
delta = 1 |
|
|
|
|
mask = "[ ]" |
|
|
|
|
while not self.__ping(): |
|
|
|
|
# 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 |
|
|
|
|
# The server has 30 secondes to start. |
|
|
|
|
if cnt >= 300: |
|
|
|
|
if self.__conf["verbose"] > 1: |
|
|
|
|
sys.stdout.write('\n') |
|
|
|
|
raise ServerExecutionException("Failed to start server") |
|
|
|
|
time.sleep(0.1) |
|
|
|
|
cnt += 1 |
|
|
|
|
if self.__conf["verbose"] > 1: |
|
|
|
|
sys.stdout.write('\n') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start(self, argv): |
|
|
|
|
# Command line options |
|
|
|
|
self.__argv = argv |
|
|
|
|
|
|
|
|
|
# Install signal handlers |
|
|
|
|
signal.signal(signal.SIGTERM, self.__sigTERMhandler) |
|
|
|
|
signal.signal(signal.SIGINT, self.__sigTERMhandler) |
|
|
|
|
|
|
|
|
|
# Reads the command line options. |
|
|
|
|
try: |
|
|
|
|
cmdOpts = 'hc:s:xdviqV' |
|
|
|
|