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
|
|
|
|
#
|
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"
|
|
|
|
|
2006-10-31 23:36:59 +00:00
|
|
|
#from cPickle import dumps, loads, HIGHEST_PROTOCOL
|
|
|
|
from pickle import dumps, loads, HIGHEST_PROTOCOL
|
2006-09-25 17:03:48 +00:00
|
|
|
import socket
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
class CSocket:
|
|
|
|
|
2006-08-28 20:16:32 +00:00
|
|
|
END_STRING = "<F2B_END_COMMAND>"
|
|
|
|
|
2007-12-14 21:33:33 +00:00
|
|
|
def __init__(self, sock = "/var/run/fail2ban/fail2ban.sock"):
|
2006-06-26 20:05:00 +00:00
|
|
|
# Create an INET, STREAMing socket
|
|
|
|
#self.csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__csock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
2006-06-26 20:05:00 +00:00
|
|
|
#self.csock.connect(("localhost", 2222))
|
2006-10-04 22:17:53 +00:00
|
|
|
self.__csock.connect(sock)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def send(self, msg):
|
|
|
|
# Convert every list member to string
|
2006-11-12 21:55:57 +00:00
|
|
|
obj = dumps([str(m) for m in msg], HIGHEST_PROTOCOL)
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__csock.send(obj + CSocket.END_STRING)
|
|
|
|
ret = self.receive(self.__csock)
|
|
|
|
self.__csock.close()
|
2006-06-26 20:05:00 +00:00
|
|
|
return ret
|
|
|
|
|
2007-12-16 21:38:04 +00:00
|
|
|
#@staticmethod
|
2006-10-24 19:40:51 +00:00
|
|
|
def receive(sock):
|
2006-06-26 20:05:00 +00:00
|
|
|
msg = ''
|
2006-08-28 20:16:32 +00:00
|
|
|
while msg.rfind(CSocket.END_STRING) == -1:
|
2006-10-24 19:40:51 +00:00
|
|
|
chunk = sock.recv(6)
|
2006-06-26 20:05:00 +00:00
|
|
|
if chunk == '':
|
|
|
|
raise RuntimeError, "socket connection broken"
|
|
|
|
msg = msg + chunk
|
2006-09-25 17:03:48 +00:00
|
|
|
return loads(msg)
|
2007-12-16 21:38:04 +00:00
|
|
|
receive = staticmethod(receive)
|