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
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
2006-07-16 22:21:58 +00:00
|
|
|
# $Revision$
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2006-07-16 22:21:58 +00:00
|
|
|
__version__ = "$Revision$"
|
|
|
|
__date__ = "$Date$"
|
2006-06-26 20:05:00 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
|
|
|
from threading import Thread
|
|
|
|
import socket, time, logging, pickle, os, os.path
|
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.comm")
|
|
|
|
|
|
|
|
class SSocket(Thread):
|
|
|
|
|
2006-08-28 20:16:32 +00:00
|
|
|
END_STRING = "<F2B_END_COMMAND>"
|
|
|
|
SOCKET_FILE = "/tmp/fail2ban.sock"
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def __init__(self, transmitter):
|
|
|
|
Thread.__init__(self)
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__transmit = transmitter
|
|
|
|
self.__isRunning = False
|
2006-06-26 20:05:00 +00:00
|
|
|
logSys.debug("Created SSocket")
|
|
|
|
|
2006-08-17 22:48:42 +00:00
|
|
|
def initialize(self, force = False):
|
2006-06-26 20:05:00 +00:00
|
|
|
# Remove socket
|
2006-08-28 20:16:32 +00:00
|
|
|
if os.path.exists(SSocket.SOCKET_FILE):
|
2006-08-17 22:48:42 +00:00
|
|
|
logSys.error("Fail2ban seems to be already running")
|
|
|
|
if force:
|
|
|
|
logSys.warn("Forcing execution of the server")
|
2006-08-28 20:16:32 +00:00
|
|
|
os.remove(SSocket.SOCKET_FILE)
|
2006-08-17 22:48:42 +00:00
|
|
|
else:
|
|
|
|
raise SSocketErrorException("Server already running")
|
2006-06-26 20:05:00 +00:00
|
|
|
# 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)
|
|
|
|
#self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2006-09-07 22:24:17 +00:00
|
|
|
#self.ssock.setblocking(False)
|
2006-09-10 20:50:07 +00:00
|
|
|
# Do not use a blocking socket as there is problem at shutdown.
|
|
|
|
# Use a timeout instead. Daemon exits at most 'timeout' seconds
|
|
|
|
# after the command.
|
|
|
|
self.ssock.settimeout(1)
|
2006-06-26 20:05:00 +00:00
|
|
|
# Bind the socket to a public host and a well-known port
|
|
|
|
#self.ssock.bind(("localhost", 2222))
|
2006-08-28 20:16:32 +00:00
|
|
|
self.ssock.bind(SSocket.SOCKET_FILE)
|
2006-06-26 20:05:00 +00:00
|
|
|
# Become a server socket
|
2006-09-07 22:24:17 +00:00
|
|
|
self.ssock.listen(5)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def run(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__isRunning = True
|
|
|
|
while self.__isRunning:
|
2006-09-10 20:50:07 +00:00
|
|
|
try:
|
|
|
|
(csock, address) = self.ssock.accept()
|
2006-09-19 20:38:32 +00:00
|
|
|
thread = SocketWorker(csock, self.__transmit)
|
2006-09-10 20:50:07 +00:00
|
|
|
thread.start()
|
|
|
|
except socket.timeout:
|
|
|
|
# Do nothing here
|
|
|
|
pass
|
2006-06-26 20:05:00 +00:00
|
|
|
self.ssock.close()
|
|
|
|
# Remove socket
|
2006-08-28 20:16:32 +00:00
|
|
|
if os.path.exists(SSocket.SOCKET_FILE):
|
|
|
|
logSys.debug("Removed socket file " + SSocket.SOCKET_FILE)
|
|
|
|
os.remove(SSocket.SOCKET_FILE)
|
2006-06-26 20:05:00 +00:00
|
|
|
logSys.debug("Socket shutdown")
|
|
|
|
return True
|
|
|
|
|
|
|
|
##
|
|
|
|
# Stop the thread.
|
|
|
|
#
|
|
|
|
# Set the isRunning flag to False.
|
|
|
|
# @bug It seems to be some concurrency problem with this flag
|
|
|
|
|
|
|
|
def stop(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__isRunning = False
|
2006-09-07 22:24:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SocketWorker(Thread):
|
|
|
|
|
|
|
|
def __init__(self, csock, transmitter):
|
|
|
|
Thread.__init__(self)
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__csock = csock
|
|
|
|
self.__transmit = transmitter
|
2006-09-07 22:24:17 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
logSys.debug("Starting new thread to handle the request")
|
2006-09-19 20:38:32 +00:00
|
|
|
msg = self.__receive(self.__csock)
|
|
|
|
msg = self.__transmit.proceed(msg)
|
|
|
|
self.__send(self.__csock, msg)
|
|
|
|
self.__csock.close()
|
2006-09-10 20:50:07 +00:00
|
|
|
logSys.debug("Connection closed")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __send(self, socket, msg):
|
2006-06-26 20:05:00 +00:00
|
|
|
obj = pickle.dumps(msg)
|
2006-08-28 20:16:32 +00:00
|
|
|
socket.send(obj + SSocket.END_STRING)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __receive(self, socket):
|
2006-06-26 20:05:00 +00:00
|
|
|
msg = ''
|
2006-08-28 20:16:32 +00:00
|
|
|
while msg.rfind(SSocket.END_STRING) == -1:
|
2006-06-26 20:05:00 +00:00
|
|
|
chunk = socket.recv(6)
|
|
|
|
if chunk == '':
|
|
|
|
raise RuntimeError, "socket connection broken"
|
|
|
|
msg = msg + chunk
|
|
|
|
return pickle.loads(msg)
|
2006-08-17 22:48:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SSocketErrorException(Exception):
|
|
|
|
pass
|