Browse Source

- Improved server startup checks

- Added "force" option "-x"

git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@270 a942ae1a-1317-0410-a47c-b1dcaea8d605
0.x
Cyril Jaquier 19 years ago
parent
commit
a694df4a9a
  1. 8
      fail2ban-server
  2. 4
      server/server.py
  3. 13
      server/ssocket.py
  4. 22
      server/transmitter.py

8
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 <lostcontrol@users.sourceforge.net>"
@ -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()

4
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():

13
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

22
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):

Loading…
Cancel
Save