2006-08-28 20:20:18 +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
|
|
|
|
#
|
2008-03-06 01:19:06 +00:00
|
|
|
# $Revision: 644 $
|
2006-08-28 20:20:18 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2008-03-06 01:19:06 +00:00
|
|
|
__version__ = "$Revision: 644 $"
|
|
|
|
__date__ = "$Date: 2008-01-15 00:12:21 +0100 (Tue, 15 Jan 2008) $"
|
2006-08-28 20:20:18 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2006-09-17 22:01:37 +00:00
|
|
|
from server.jails import UnknownJailException
|
|
|
|
from server.jails import DuplicateJailException
|
2006-08-28 20:20:18 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.client.config")
|
|
|
|
|
|
|
|
##
|
|
|
|
# Beautify the output of the client.
|
|
|
|
#
|
|
|
|
# Fail2ban server only return unformatted return codes which need to be
|
|
|
|
# converted into user readable messages.
|
|
|
|
|
|
|
|
class Beautifier:
|
|
|
|
|
|
|
|
def __init__(self, cmd = None):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__inputCmd = cmd
|
2006-08-28 20:20:18 +00:00
|
|
|
|
|
|
|
def setInputCmd(self, cmd):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__inputCmd = cmd
|
2006-08-28 20:20:18 +00:00
|
|
|
|
|
|
|
def getInputCmd(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__inputCmd
|
2006-08-28 20:20:18 +00:00
|
|
|
|
|
|
|
def beautify(self, response):
|
2006-09-19 20:38:32 +00:00
|
|
|
logSys.debug("Beautify " + `response` + " with " + `self.__inputCmd`)
|
|
|
|
inC = self.__inputCmd
|
2006-08-28 20:20:18 +00:00
|
|
|
msg = response
|
2006-09-17 22:01:37 +00:00
|
|
|
try:
|
2006-09-25 20:45:23 +00:00
|
|
|
if inC[0] == "ping":
|
|
|
|
msg = "Server replied: " + response
|
|
|
|
elif inC[0] == "start":
|
2006-09-17 22:01:37 +00:00
|
|
|
msg = "Jail started"
|
|
|
|
elif inC[0] == "stop":
|
|
|
|
if len(inC) == 1:
|
|
|
|
if response == None:
|
|
|
|
msg = "Shutdown successful"
|
|
|
|
else:
|
|
|
|
if response == None:
|
|
|
|
msg = "Jail stopped"
|
|
|
|
elif inC[0] == "add":
|
|
|
|
msg = "Added jail " + response
|
|
|
|
elif inC[0:1] == ['status']:
|
|
|
|
if len(inC) > 1:
|
2007-02-11 23:21:56 +00:00
|
|
|
# Create IP list
|
|
|
|
ipList = ""
|
|
|
|
for ip in response[1][1][2][1]:
|
|
|
|
ipList += ip + " "
|
2008-03-06 01:19:06 +00:00
|
|
|
# Creates file list.
|
|
|
|
fileList = ""
|
|
|
|
for f in response[0][1][2][1]:
|
|
|
|
fileList += f + " "
|
2007-02-11 23:21:56 +00:00
|
|
|
# Display information
|
2006-09-17 22:01:37 +00:00
|
|
|
msg = "Status for the jail: " + inC[1] + "\n"
|
|
|
|
msg = msg + "|- " + response[0][0] + "\n"
|
2008-03-06 01:19:06 +00:00
|
|
|
msg = msg + "| |- " + response[0][1][2][0] + ":\t" + fileList + "\n"
|
2007-01-29 21:46:59 +00:00
|
|
|
msg = msg + "| |- " + response[0][1][0][0] + ":\t" + `response[0][1][0][1]` + "\n"
|
|
|
|
msg = msg + "| `- " + response[0][1][1][0] + ":\t" + `response[0][1][1][1]` + "\n"
|
2006-09-17 22:01:37 +00:00
|
|
|
msg = msg + "`- " + response[1][0] + "\n"
|
2007-01-29 21:46:59 +00:00
|
|
|
msg = msg + " |- " + response[1][1][0][0] + ":\t" + `response[1][1][0][1]` + "\n"
|
2007-02-11 23:21:56 +00:00
|
|
|
msg = msg + " | `- " + response[1][1][2][0] + ":\t" + ipList + "\n"
|
2007-01-29 21:46:59 +00:00
|
|
|
msg = msg + " `- " + response[1][1][1][0] + ":\t" + `response[1][1][1][1]`
|
2006-09-17 22:01:37 +00:00
|
|
|
else:
|
|
|
|
msg = "Status\n"
|
|
|
|
msg = msg + "|- " + response[0][0] + ":\t" + `response[0][1]` + "\n"
|
|
|
|
msg = msg + "`- " + response[1][0] + ":\t\t" + response[1][1]
|
|
|
|
elif inC[1] == "logtarget":
|
|
|
|
msg = "Current logging target is:\n"
|
|
|
|
msg = msg + "`- " + response
|
|
|
|
elif inC[1:2] == ['loglevel']:
|
|
|
|
msg = "Current logging level is "
|
|
|
|
if response == 1:
|
|
|
|
msg = msg + "ERROR"
|
|
|
|
elif response == 2:
|
|
|
|
msg = msg + "WARN"
|
|
|
|
elif response == 3:
|
|
|
|
msg = msg + "INFO"
|
|
|
|
elif response == 4:
|
|
|
|
msg = msg + "DEBUG"
|
|
|
|
else:
|
|
|
|
msg = msg + `response`
|
|
|
|
elif inC[2] in ("logpath", "addlogpath", "dellogpath"):
|
|
|
|
if len(response) == 0:
|
|
|
|
msg = "No file is currently monitored"
|
|
|
|
else:
|
|
|
|
msg = "Current monitored log file(s):\n"
|
|
|
|
for path in response[:-1]:
|
|
|
|
msg = msg + "|- " + path + "\n"
|
|
|
|
msg = msg + "`- " + response[len(response)-1]
|
|
|
|
elif inC[2] in ("ignoreip", "addignoreip", "delignoreip"):
|
|
|
|
if len(response) == 0:
|
|
|
|
msg = "No IP address/network is ignored"
|
|
|
|
else:
|
|
|
|
msg = "These IP addresses/networks are ignored:\n"
|
|
|
|
for ip in response[:-1]:
|
|
|
|
msg = msg + "|- " + ip + "\n"
|
2006-12-23 16:31:00 +00:00
|
|
|
msg = msg + "`- " + response[len(response)-1]
|
|
|
|
elif inC[2] in ("failregex", "addfailregex", "delfailregex",
|
|
|
|
"ignoreregex", "addignoreregex", "delignoreregex"):
|
|
|
|
if len(response) == 0:
|
|
|
|
msg = "No regular expression is defined"
|
|
|
|
else:
|
|
|
|
msg = "The following regular expression are defined:\n"
|
|
|
|
c = 0
|
|
|
|
for ip in response[:-1]:
|
|
|
|
msg = msg + "|- [" + str(c) + "]: " + ip + "\n"
|
|
|
|
c += 1
|
|
|
|
msg = msg + "`- [" + str(c) + "]: " + response[len(response)-1]
|
2006-09-17 22:01:37 +00:00
|
|
|
except Exception:
|
|
|
|
logSys.warn("Beautifier error. Please report the error")
|
2006-09-19 20:38:32 +00:00
|
|
|
logSys.error("Beautify " + `response` + " with " + `self.__inputCmd` +
|
2006-09-17 22:01:37 +00:00
|
|
|
" failed")
|
|
|
|
msg = msg + `response`
|
2006-08-28 20:20:18 +00:00
|
|
|
return msg
|
|
|
|
|
|
|
|
def beautifyError(self, response):
|
2006-09-19 20:38:32 +00:00
|
|
|
logSys.debug("Beautify (error) " + `response` + " with " + `self.__inputCmd`)
|
2006-08-28 20:20:18 +00:00
|
|
|
msg = response
|
2006-09-17 22:01:37 +00:00
|
|
|
if isinstance(response, UnknownJailException):
|
2006-08-28 20:20:18 +00:00
|
|
|
msg = "Sorry but the jail '" + response[0] + "' does not exist"
|
|
|
|
elif isinstance(response, IndexError):
|
|
|
|
msg = "Sorry but the command is invalid"
|
2006-09-17 22:01:37 +00:00
|
|
|
elif isinstance(response, DuplicateJailException):
|
2006-08-28 20:20:18 +00:00
|
|
|
msg = "The jail '" + response[0] + "' already exists"
|
|
|
|
return msg
|