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"
|
|
|
|
|
2006-09-17 22:02:22 +00:00
|
|
|
from threading import Lock
|
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:
|
|
|
|
|
|
|
|
def __init__(self, server):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock = Lock()
|
|
|
|
self.__server = server
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def proceed(self, action):
|
|
|
|
# Deserialize object
|
|
|
|
try:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.acquire()
|
2006-09-17 22:02:22 +00:00
|
|
|
logSys.debug("Action: " + `action`)
|
|
|
|
try:
|
2006-09-19 20:38:32 +00:00
|
|
|
ret = self.__actionHandler(action)
|
2006-09-17 22:02:22 +00:00
|
|
|
ack = 0, ret
|
|
|
|
except Exception, e:
|
|
|
|
logSys.warn("Invalid command: " + `action`)
|
|
|
|
ack = 1, e
|
|
|
|
return ack
|
|
|
|
finally:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.release()
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Handle an action.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __actionHandler(self, action):
|
2006-06-26 20:05:00 +00:00
|
|
|
if action[0] == "ping":
|
|
|
|
return "pong"
|
|
|
|
elif action[0] == "add":
|
|
|
|
name = action[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:
|
|
|
|
backend = action[2]
|
|
|
|
except IndexError:
|
|
|
|
backend = "auto"
|
|
|
|
self.__server.addJail(name, backend)
|
2006-06-26 20:05:00 +00:00
|
|
|
return name
|
|
|
|
elif action[0] == "start":
|
|
|
|
name = action[1]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.startJail(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
return None
|
|
|
|
elif action[0] == "stop":
|
2006-08-17 22:48:42 +00:00
|
|
|
if len(action) == 1:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.quit()
|
2006-08-20 21:07:28 +00:00
|
|
|
elif action[1] == "all":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.stopAllJail()
|
2006-08-17 22:48:42 +00:00
|
|
|
else:
|
|
|
|
name = action[1]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.stopJail(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
return None
|
|
|
|
elif action[0] == "sleep":
|
|
|
|
value = action[1]
|
|
|
|
time.sleep(int(value))
|
|
|
|
return None
|
|
|
|
elif action[0] == "set":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__actionSet(action[1:])
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[0] == "get":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__actionGet(action[1:])
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[0] == "status":
|
2006-08-17 22:48:42 +00:00
|
|
|
return self.status(action[1:])
|
2006-06-26 20:05:00 +00:00
|
|
|
raise Exception("Invalid command")
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __actionSet(self, action):
|
2006-06-26 20:05:00 +00:00
|
|
|
name = action[0]
|
|
|
|
# Logging
|
|
|
|
if name == "loglevel":
|
|
|
|
value = int(action[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":
|
|
|
|
value = action[1]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setLogTarget(value)
|
|
|
|
return self.__server.getLogTarget()
|
2006-06-26 20:05:00 +00:00
|
|
|
# Jail
|
2006-07-08 16:51:14 +00:00
|
|
|
elif action[1] == "idle":
|
2006-06-26 20:05:00 +00:00
|
|
|
if action[2] == "on":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setIdleJail(name, True)
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[2] == "off":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setIdleJail(name, False)
|
|
|
|
return self.__server.getIdleJail(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
# Filter
|
2006-09-17 22:02:22 +00:00
|
|
|
elif action[1] == "addignoreip":
|
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.addIgnoreIP(name, value)
|
|
|
|
return self.__server.getIgnoreIP(name)
|
2006-09-17 22:02:22 +00:00
|
|
|
elif action[1] == "delignoreip":
|
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.delIgnoreIP(name, value)
|
|
|
|
return self.__server.getIgnoreIP(name)
|
2006-09-13 21:31:22 +00:00
|
|
|
elif action[1] == "addlogpath":
|
|
|
|
value = action[2:]
|
|
|
|
for path in value:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.addLogPath(name, path)
|
|
|
|
return self.__server.getLogPath(name)
|
2006-09-13 21:31:22 +00:00
|
|
|
elif action[1] == "dellogpath":
|
2006-06-26 20:05:00 +00:00
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.delLogPath(name, value)
|
|
|
|
return self.__server.getLogPath(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[1] == "timeregex":
|
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setTimeRegex(name, value)
|
|
|
|
return self.__server.getTimeRegex(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[1] == "timepattern":
|
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setTimePattern(name, value)
|
|
|
|
return self.__server.getTimePattern(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[1] == "failregex":
|
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setFailRegex(name, value)
|
|
|
|
return self.__server.getFailRegex(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[1] == "maxtime":
|
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setMaxTime(name, int(value))
|
|
|
|
return self.__server.getMaxTime(name)
|
2006-08-28 20:22:15 +00:00
|
|
|
elif action[1] == "findtime":
|
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setFindTime(name, int(value))
|
|
|
|
return self.__server.getFindTime(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[1] == "maxretry":
|
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setMaxRetry(name, int(value))
|
|
|
|
return self.__server.getMaxRetry(name)
|
2006-06-26 20:05:00 +00:00
|
|
|
# Action
|
|
|
|
elif action[1] == "bantime":
|
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setBanTime(name, int(value))
|
|
|
|
return self.__server.getBanTime(name)
|
2006-07-08 16:51:14 +00:00
|
|
|
elif action[1] == "addaction":
|
2006-06-26 20:05:00 +00:00
|
|
|
value = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.addAction(name, value)
|
|
|
|
return self.__server.getLastAction(name).getName()
|
2006-07-08 16:51:14 +00:00
|
|
|
elif action[1] == "delaction":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.delAction(name, value)
|
2006-07-08 16:51:14 +00:00
|
|
|
return None
|
2006-07-16 21:35:08 +00:00
|
|
|
elif action[1] == "setcinfo":
|
|
|
|
act = action[2]
|
|
|
|
key = action[3]
|
|
|
|
value = action[4]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setCInfo(name, act, key, value)
|
|
|
|
return self.__server.getCInfo(name, act, key)
|
2006-07-16 21:35:08 +00:00
|
|
|
elif action[1] == "delcinfo":
|
|
|
|
act = action[2]
|
|
|
|
key = action[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-07-08 16:51:14 +00:00
|
|
|
elif action[1] == "actionstart":
|
|
|
|
act = action[2]
|
|
|
|
value = action[3]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setActionStart(name, act, value)
|
|
|
|
return self.__server.getActionStart(name, act)
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[1] == "actionstop":
|
2006-07-08 16:51:14 +00:00
|
|
|
act = action[2]
|
|
|
|
value = action[3]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setActionStop(name, act, value)
|
|
|
|
return self.__server.getActionStop(name, act)
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[1] == "actioncheck":
|
2006-07-08 16:51:14 +00:00
|
|
|
act = action[2]
|
|
|
|
value = action[3]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setActionCheck(name, act, value)
|
|
|
|
return self.__server.getActionCheck(name, act)
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[1] == "actionban":
|
2006-07-08 16:51:14 +00:00
|
|
|
act = action[2]
|
|
|
|
value = action[3]
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.setActionBan(name, act, value)
|
|
|
|
return self.__server.getActionBan(name, act)
|
2006-06-26 20:05:00 +00:00
|
|
|
elif action[1] == "actionunban":
|
2006-07-08 16:51:14 +00:00
|
|
|
act = action[2]
|
|
|
|
value = action[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-09-19 20:38:32 +00:00
|
|
|
def __actionGet(self, action):
|
2006-06-26 20:05:00 +00:00
|
|
|
name = action[0]
|
|
|
|
# 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-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "logpath":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getLogPath(name)
|
2006-09-17 22:02:22 +00:00
|
|
|
elif action[1] == "ignoreip":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getIgnoreIP(name)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "timeregex":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getTimeRegex(name)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "timepattern":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getTimePattern(name)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "failregex":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getFailRegex(name)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "maxtime":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getMaxTime(name)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "findtime":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getFindTime(name)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[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-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "bantime":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getBanTime(name)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "addaction":
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getLastAction(name).getName()
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "actionstart":
|
|
|
|
act = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getActionStart(name, act)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "actionstop":
|
|
|
|
act = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getActionStop(name, act)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "actioncheck":
|
|
|
|
act = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getActionCheck(name, act)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "actionban":
|
|
|
|
act = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getActionBan(name, act)
|
2006-08-28 21:37:39 +00:00
|
|
|
elif action[1] == "actionunban":
|
|
|
|
act = action[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.getActionUnban(name, act)
|
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
|
|
|
|
|
|
|
def status(self, action):
|
|
|
|
if len(action) == 0:
|
2006-09-19 20:38:32 +00:00
|
|
|
return self.__server.status()
|
2006-06-26 20:05:00 +00:00
|
|
|
else:
|
|
|
|
name = action[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)")
|
|
|
|
|