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-09-25 17:03:48 +00:00
|
|
|
import logging, time
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.comm")
|
|
|
|
|
|
|
|
class Transmitter:
|
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
##
|
|
|
|
# Constructor.
|
|
|
|
#
|
|
|
|
# @param The server reference
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def __init__(self, server):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server = server
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
##
|
|
|
|
# Proceeds a command.
|
|
|
|
#
|
|
|
|
# Proceeds an incoming command.
|
|
|
|
# @param command The incoming command
|
|
|
|
|
|
|
|
def proceed(self, command):
|
2006-06-26 20:05:00 +00:00
|
|
|
# Deserialize object
|
2006-11-06 22:48:46 +00:00
|
|
|
logSys.debug("Command: " + `command`)
|
2006-06-26 20:05:00 +00:00
|
|
|
try:
|
2006-11-06 22:48:46 +00:00
|
|
|
ret = self.__commandHandler(command)
|
|
|
|
ack = 0, ret
|
|
|
|
except Exception, e:
|
2012-08-02 23:48:59 +00:00
|
|
|
logSys.warn("Command %r has failed. Received %r"
|
|
|
|
% (command, e))
|
2006-11-06 22:48:46 +00:00
|
|
|
ack = 1, e
|
|
|
|
return ack
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
2006-11-06 22:48:46 +00:00
|
|
|
# Handle an command.
|
2006-06-26 20:05:00 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
def __commandHandler(self, command):
|
|
|
|
if command[0] == "ping":
|
2006-06-26 20:05:00 +00:00
|
|
|
return "pong"
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[0] == "add":
|
|
|
|
name = command[1]
|
2006-08-20 21:07:28 +00:00
|
|
|
if name == "all":
|
|
|
|
raise Exception("Reserved name")
|
2006-10-16 19:42:50 +00:00
|
|
|
try:
|
2006-11-06 22:48:46 +00:00
|
|
|
backend = command[2]
|
2006-10-16 19:42:50 +00:00
|
|
|
except IndexError:
|
|
|
|
backend = "auto"
|
|
|
|
self.__server.addJail(name, backend)
|
2006-06-26 20:05:00 +00:00
|
|
|
return name
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[0] == "start":
|
|
|
|
name = command[1]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.startJail(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
return None
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[0] == "stop":
|
|
|
|
if len(command) == 1:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.quit()
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "all":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.stopAllJail()
|
2006-08-17 22:48:42 +00:00
|
|
|
else:
|
2006-11-06 22:48:46 +00:00
|
|
|
name = command[1]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.stopJail(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
return None
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[0] == "sleep":
|
|
|
|
value = command[1]
|
2006-06-26 20:05:00 +00:00
|
|
|
time.sleep(int(value))
|
|
|
|
return None
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[0] == "set":
|
|
|
|
return self.__commandSet(command[1:])
|
|
|
|
elif command[0] == "get":
|
|
|
|
return self.__commandGet(command[1:])
|
|
|
|
elif command[0] == "status":
|
|
|
|
return self.status(command[1:])
|
2006-06-26 20:05:00 +00:00
|
|
|
raise Exception("Invalid command")
|
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
def __commandSet(self, command):
|
|
|
|
name = command[0]
|
2006-06-26 20:05:00 +00:00
|
|
|
# Logging
|
|
|
|
if name == "loglevel":
|
2006-11-06 22:48:46 +00:00
|
|
|
value = int(command[1])
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setLogLevel(value)
|
|
|
|
return self.__server.getLogLevel()
|
2006-08-06 21:24:06 +00:00
|
|
|
elif name == "logtarget":
|
2006-11-06 22:48:46 +00:00
|
|
|
value = command[1]
|
2013-03-10 14:55:39 +00:00
|
|
|
if self.__server.setLogTarget(value):
|
|
|
|
return self.__server.getLogTarget()
|
|
|
|
else:
|
|
|
|
raise Exception("Failed to change log target")
|
2006-06-26 20:05:00 +00:00
|
|
|
# Jail
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "idle":
|
|
|
|
if command[2] == "on":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setIdleJail(name, True)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[2] == "off":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setIdleJail(name, False)
|
2013-03-10 14:55:39 +00:00
|
|
|
else:
|
|
|
|
raise Exception("Invalid idle option, must be 'yes' or 'no'")
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getIdleJail(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
# Filter
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "addignoreip":
|
|
|
|
value = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.addIgnoreIP(name, value)
|
|
|
|
return self.__server.getIgnoreIP(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "delignoreip":
|
|
|
|
value = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.delIgnoreIP(name, value)
|
|
|
|
return self.__server.getIgnoreIP(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "addlogpath":
|
|
|
|
value = command[2:]
|
2006-09-13 21:31:22 +00:00
|
|
|
for path in value:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.addLogPath(name, path)
|
|
|
|
return self.__server.getLogPath(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "dellogpath":
|
|
|
|
value = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.delLogPath(name, value)
|
|
|
|
return self.__server.getLogPath(name)
|
2006-12-23 16:31:00 +00:00
|
|
|
elif command[1] == "addfailregex":
|
2006-11-06 22:48:46 +00:00
|
|
|
value = command[2]
|
2006-12-23 16:31:00 +00:00
|
|
|
self.__server.addFailRegex(name, value)
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getFailRegex(name)
|
2006-12-23 16:31:00 +00:00
|
|
|
elif command[1] == "delfailregex":
|
|
|
|
value = int(command[2])
|
|
|
|
self.__server.delFailRegex(name, value)
|
|
|
|
return self.__server.getFailRegex(name)
|
|
|
|
elif command[1] == "addignoreregex":
|
2006-11-12 14:52:36 +00:00
|
|
|
value = command[2]
|
2006-12-23 16:31:00 +00:00
|
|
|
self.__server.addIgnoreRegex(name, value)
|
|
|
|
return self.__server.getIgnoreRegex(name)
|
|
|
|
elif command[1] == "delignoreregex":
|
|
|
|
value = int(command[2])
|
|
|
|
self.__server.delIgnoreRegex(name, value)
|
2006-11-12 14:52:36 +00:00
|
|
|
return self.__server.getIgnoreRegex(name)
|
2012-01-13 04:23:41 +00:00
|
|
|
elif command[1] == "usedns":
|
|
|
|
value = command[2]
|
|
|
|
self.__server.setUseDns(name, value)
|
|
|
|
return self.__server.getUseDns(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "findtime":
|
|
|
|
value = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setFindTime(name, int(value))
|
|
|
|
return self.__server.getFindTime(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "maxretry":
|
|
|
|
value = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setMaxRetry(name, int(value))
|
|
|
|
return self.__server.getMaxRetry(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
# command
|
|
|
|
elif command[1] == "bantime":
|
|
|
|
value = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setBanTime(name, int(value))
|
|
|
|
return self.__server.getBanTime(name)
|
2009-08-30 18:26:15 +00:00
|
|
|
elif command[1] == "banip":
|
|
|
|
value = command[2]
|
|
|
|
return self.__server.setBanIP(name,value)
|
2012-11-05 23:07:33 +00:00
|
|
|
elif command[1] == "unbanip":
|
|
|
|
value = command[2]
|
|
|
|
return self.__server.setUnbanIP(name,value)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "addaction":
|
|
|
|
value = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.addAction(name, value)
|
2006-11-12 10:52:42 +00:00
|
|
|
return self.__server.getLastAction(name).getName()
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "delaction":
|
2013-02-20 23:24:46 +00:00
|
|
|
value = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.delAction(name, value)
|
2006-07-08 16:51:14 +00:00
|
|
|
return None
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "setcinfo":
|
|
|
|
act = command[2]
|
|
|
|
key = command[3]
|
2013-02-07 10:50:28 +00:00
|
|
|
value = " ".join(command[4:])
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setCInfo(name, act, key, value)
|
|
|
|
return self.__server.getCInfo(name, act, key)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "delcinfo":
|
|
|
|
act = command[2]
|
|
|
|
key = command[3]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.delCInfo(name, act, key)
|
2006-07-16 21:35:08 +00:00
|
|
|
return None
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "actionstart":
|
|
|
|
act = command[2]
|
2013-02-07 10:50:28 +00:00
|
|
|
value = " ".join(command[3:])
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setActionStart(name, act, value)
|
|
|
|
return self.__server.getActionStart(name, act)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "actionstop":
|
|
|
|
act = command[2]
|
2013-02-07 10:50:28 +00:00
|
|
|
value = " ".join(command[3:])
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setActionStop(name, act, value)
|
|
|
|
return self.__server.getActionStop(name, act)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "actioncheck":
|
|
|
|
act = command[2]
|
2013-02-07 10:50:28 +00:00
|
|
|
value = " ".join(command[3:])
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setActionCheck(name, act, value)
|
|
|
|
return self.__server.getActionCheck(name, act)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "actionban":
|
|
|
|
act = command[2]
|
2013-02-07 10:50:28 +00:00
|
|
|
value = " ".join(command[3:])
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setActionBan(name, act, value)
|
|
|
|
return self.__server.getActionBan(name, act)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "actionunban":
|
|
|
|
act = command[2]
|
2013-02-07 10:50:28 +00:00
|
|
|
value = " ".join(command[3:])
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setActionUnban(name, act, value)
|
|
|
|
return self.__server.getActionUnban(name, act)
|
2006-08-28 20:22:15 +00:00
|
|
|
raise Exception("Invalid command (no set action or not yet implemented)")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
def __commandGet(self, command):
|
|
|
|
name = command[0]
|
2006-06-26 20:05:00 +00:00
|
|
|
# Logging
|
|
|
|
if name == "loglevel":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getLogLevel()
|
2006-09-17 22:02:22 +00:00
|
|
|
elif name == "logtarget":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getLogTarget()
|
2006-09-17 22:02:22 +00:00
|
|
|
# Filter
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "logpath":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getLogPath(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "ignoreip":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getIgnoreIP(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "failregex":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getFailRegex(name)
|
2006-11-12 14:52:36 +00:00
|
|
|
elif command[1] == "ignoreregex":
|
|
|
|
return self.__server.getIgnoreRegex(name)
|
2012-01-13 04:23:41 +00:00
|
|
|
elif command[1] == "usedns":
|
|
|
|
return self.__server.getUseDns(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "findtime":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getFindTime(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "maxretry":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getMaxRetry(name)
|
2006-09-17 22:02:22 +00:00
|
|
|
# Action
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "bantime":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getBanTime(name)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "addaction":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getLastAction(name).getName()
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "actionstart":
|
|
|
|
act = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getActionStart(name, act)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "actionstop":
|
|
|
|
act = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getActionStop(name, act)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "actioncheck":
|
|
|
|
act = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getActionCheck(name, act)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "actionban":
|
|
|
|
act = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getActionBan(name, act)
|
2006-11-06 22:48:46 +00:00
|
|
|
elif command[1] == "actionunban":
|
|
|
|
act = command[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getActionUnban(name, act)
|
2013-02-20 23:33:39 +00:00
|
|
|
elif command[1] == "cinfo":
|
|
|
|
act = command[2]
|
|
|
|
key = command[3]
|
|
|
|
return self.__server.getCInfo(name, act, key)
|
2006-08-28 20:22:15 +00:00
|
|
|
raise Exception("Invalid command (no get action or not yet implemented)")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
def status(self, command):
|
|
|
|
if len(command) == 0:
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.status()
|
2013-03-10 14:55:39 +00:00
|
|
|
elif len(command) == 1:
|
2006-11-06 22:48:46 +00:00
|
|
|
name = command[0]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.statusJail(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
raise Exception("Invalid command (no status)")
|
2011-10-07 19:47:50 +00:00
|
|
|
|