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-09-17 21:59:29 +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-09-17 21:59:29 +00:00
|
|
|
|
2013-01-28 14:46:50 +00:00
|
|
|
__author__ = "Cyril Jaquier, Yaroslav Halchenko"
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2013- Yaroslav Halchenko"
|
2006-09-17 21:59:29 +00:00
|
|
|
__license__ = "GPL"
|
|
|
|
|
2013-01-28 14:46:50 +00:00
|
|
|
from common.exceptions import DuplicateJailException, UnknownJailException
|
2006-09-17 21:59:29 +00:00
|
|
|
|
|
|
|
from jail import Jail
|
|
|
|
from threading import Lock
|
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
##
|
|
|
|
# Handles the jails.
|
|
|
|
#
|
|
|
|
# This class handles the jails. Creation, deletion or access to a jail must be
|
|
|
|
# done through this class. This class is thread-safe which is not the case of
|
|
|
|
# the jail itself, including filter and actions.
|
|
|
|
|
2006-09-17 21:59:29 +00:00
|
|
|
class Jails:
|
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
##
|
|
|
|
# Constructor.
|
|
|
|
|
2006-09-17 21:59:29 +00:00
|
|
|
def __init__(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock = Lock()
|
|
|
|
self.__jails = dict()
|
2006-09-17 21:59:29 +00:00
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
##
|
|
|
|
# Adds a jail.
|
|
|
|
#
|
|
|
|
# Adds a new jail which should use the given backend. Raises a
|
|
|
|
# <code>DuplicateJailException</code> if the jail is already defined.
|
|
|
|
# @param name The name of the jail
|
|
|
|
# @param backend The backend to use
|
|
|
|
|
2006-10-16 19:42:50 +00:00
|
|
|
def add(self, name, backend):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__lock.acquire()
|
|
|
|
if self.__jails.has_key(name):
|
|
|
|
raise DuplicateJailException(name)
|
|
|
|
else:
|
|
|
|
self.__jails[name] = Jail(name, backend)
|
|
|
|
finally:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.release()
|
2006-09-17 21:59:29 +00:00
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
##
|
|
|
|
# Removes a jail.
|
|
|
|
#
|
|
|
|
# Removes the jail <code>name</code>. Raise an <code>UnknownJailException</code>
|
|
|
|
# if the jail does not exist.
|
|
|
|
# @param name The name of the jail
|
|
|
|
|
2006-09-17 21:59:29 +00:00
|
|
|
def remove(self, name):
|
2006-11-06 22:48:46 +00:00
|
|
|
try:
|
|
|
|
self.__lock.acquire()
|
|
|
|
if self.__jails.has_key(name):
|
|
|
|
del self.__jails[name]
|
|
|
|
else:
|
|
|
|
raise UnknownJailException(name)
|
|
|
|
finally:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.release()
|
2006-11-06 22:48:46 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Returns a jail.
|
|
|
|
#
|
|
|
|
# Returns the jail <code>name</code>. Raise an <code>UnknownJailException</code>
|
|
|
|
# if the jail does not exist.
|
|
|
|
# @param name The name of the jail
|
2006-09-17 21:59:29 +00:00
|
|
|
|
|
|
|
def get(self, name):
|
|
|
|
try:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.acquire()
|
|
|
|
if self.__jails.has_key(name):
|
|
|
|
jail = self.__jails[name]
|
2006-09-17 21:59:29 +00:00
|
|
|
return jail
|
|
|
|
else:
|
|
|
|
raise UnknownJailException(name)
|
|
|
|
finally:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.release()
|
2006-09-17 21:59:29 +00:00
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
##
|
|
|
|
# Returns an action class instance.
|
|
|
|
#
|
|
|
|
# Returns the action object of the jail <code>name</code>. Raise an
|
|
|
|
# <code>UnknownJailException</code> if the jail does not exist.
|
|
|
|
# @param name The name of the jail
|
|
|
|
|
2006-09-17 21:59:29 +00:00
|
|
|
def getAction(self, name):
|
|
|
|
try:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.acquire()
|
|
|
|
if self.__jails.has_key(name):
|
|
|
|
action = self.__jails[name].getAction()
|
2006-09-17 21:59:29 +00:00
|
|
|
return action
|
|
|
|
else:
|
|
|
|
raise UnknownJailException(name)
|
|
|
|
finally:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.release()
|
2006-09-17 21:59:29 +00:00
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
##
|
|
|
|
# Returns a filter class instance.
|
|
|
|
#
|
|
|
|
# Returns the filter object of the jail <code>name</code>. Raise an
|
|
|
|
# <code>UnknownJailException</code> if the jail does not exist.
|
|
|
|
# @param name The name of the jail
|
|
|
|
|
2006-09-17 21:59:29 +00:00
|
|
|
def getFilter(self, name):
|
|
|
|
try:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.acquire()
|
|
|
|
if self.__jails.has_key(name):
|
|
|
|
action = self.__jails[name].getFilter()
|
2006-09-17 21:59:29 +00:00
|
|
|
return action
|
|
|
|
else:
|
|
|
|
raise UnknownJailException(name)
|
|
|
|
finally:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.release()
|
2006-09-17 21:59:29 +00:00
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
##
|
|
|
|
# Returns the jails.
|
|
|
|
#
|
|
|
|
# Returns a copy of the jails list.
|
|
|
|
|
2006-09-17 21:59:29 +00:00
|
|
|
def getAll(self):
|
|
|
|
try:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.acquire()
|
|
|
|
return self.__jails.copy()
|
2006-09-17 21:59:29 +00:00
|
|
|
finally:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.release()
|
2006-09-17 21:59:29 +00:00
|
|
|
|
2006-11-06 22:48:46 +00:00
|
|
|
##
|
|
|
|
# Returns the size of the jails.
|
|
|
|
#
|
|
|
|
# Returns the number of jails.
|
|
|
|
|
2006-09-17 21:59:29 +00:00
|
|
|
def size(self):
|
|
|
|
try:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.acquire()
|
|
|
|
return len(self.__jails)
|
2006-09-17 21:59:29 +00:00
|
|
|
finally:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__lock.release()
|
2006-09-17 21:59:29 +00:00
|
|
|
|