diff --git a/fail2ban-server b/fail2ban-server index 071c83dd9..718813ccb 100755 --- a/fail2ban-server +++ b/fail2ban-server @@ -50,6 +50,7 @@ class Fail2banServer: self.argv = None self.conf = dict() self.conf["background"] = True + self.conf["force"] = False def dispUsage(self): """ Prints Fail2Ban command line options and exits @@ -61,6 +62,7 @@ class Fail2banServer: print print " -b start in background" print " -f start in foreground" + print " -x force execution of the server" print " -h display this help message" print print "Report bugs to " @@ -74,6 +76,8 @@ class Fail2banServer: self.conf["background"] = True if opt[0] == "-f": self.conf["background"] = False + if opt[0] == "-x": + self.conf["force"] = True if opt[0] in ["-h", "--help"]: self.dispUsage() @@ -90,7 +94,7 @@ class Fail2banServer: # Reads the command line options. try: - cmdOpts = 'bfh' + cmdOpts = 'bfxh' cmdLongOpts = ['help'] optList, args = getopt.getopt(self.argv[1:], cmdOpts, cmdLongOpts) except getopt.GetoptError: @@ -107,7 +111,7 @@ class Fail2banServer: try: self.server = Server() - self.server.start() + self.server.start(self.conf["force"]) except Exception, e: print e self.server.quit() diff --git a/server/server.py b/server/server.py index 08dfff20b..93157a07e 100644 --- a/server/server.py +++ b/server/server.py @@ -42,9 +42,9 @@ class Server: self.setLogLevel(self.logLevel) self.setLogTarget(self.logTarget) - def start(self): + def start(self, force): # Start the communication - self.transm.start() + self.transm.start(force) def quit(self): for jail in self.jails.copy(): diff --git a/server/ssocket.py b/server/ssocket.py index c047f413e..b25754b20 100644 --- a/server/ssocket.py +++ b/server/ssocket.py @@ -39,10 +39,15 @@ class SSocket(Thread): self.isRunning = False logSys.debug("Created SSocket") - def initialize(self): + def initialize(self, force = False): # Remove socket if os.path.exists(self.socketFile): - os.remove(self.socketFile) + logSys.error("Fail2ban seems to be already running") + if force: + logSys.warn("Forcing execution of the server") + os.remove(self.socketFile) + else: + raise SSocketErrorException("Server already running") # Create an INET, STREAMing socket #self.ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.ssock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -101,3 +106,7 @@ class SSocket(Thread): raise RuntimeError, "socket connection broken" msg = msg + chunk return pickle.loads(msg) + + +class SSocketErrorException(Exception): + pass diff --git a/server/transmitter.py b/server/transmitter.py index a2653bf87..81977fc6d 100644 --- a/server/transmitter.py +++ b/server/transmitter.py @@ -25,6 +25,7 @@ __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" from ssocket import SSocket +from ssocket import SSocketErrorException import re, pickle, logging # Gets the instance of the logger. @@ -36,9 +37,12 @@ class Transmitter: self.server = server self.socket = SSocket(self) - def start(self): - self.socket.initialize() - self.socket.start() + def start(self, force): + try: + self.socket.initialize(force) + self.socket.start() + except SSocketErrorException: + logSys.error("Could not start server") ## # Stop the transmitter. @@ -78,8 +82,11 @@ class Transmitter: self.server.startJail(name) return None elif action[0] == "stop": - name = action[1] - self.server.stopJail(name) + if len(action) == 1: + self.server.quit() + else: + name = action[1] + self.server.stopJail(name) return None elif action[0] == "sleep": value = action[1] @@ -90,10 +97,7 @@ class Transmitter: elif action[0] == "get": return self.actionGet(action[1:]) elif action[0] == "status": - return self.status(action[1:]) - elif action[0] == "quit": - self.server.quit() - return None + return self.status(action[1:]) raise Exception("Invalid command") def actionSet(self, action):