2004-10-14 10:38:22 +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
|
|
|
# Modified by: Yaroslav Halchenko (SafeConfigParserWithIncludes)
|
|
|
|
# $Revision: 656 $
|
2004-10-14 10:38:22 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2008-03-06 01:19:06 +00:00
|
|
|
__version__ = "$Revision: 656 $"
|
|
|
|
__date__ = "$Date: 2008-03-04 01:17:56 +0100 (Tue, 04 Mar 2008) $"
|
2004-10-14 10:38:22 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2006-09-07 21:00:44 +00:00
|
|
|
import logging, os
|
2008-03-06 01:19:06 +00:00
|
|
|
from configparserinc import SafeConfigParserWithIncludes
|
2006-10-24 19:40:51 +00:00
|
|
|
from ConfigParser import NoOptionError, NoSectionError
|
2005-02-18 13:30:54 +00:00
|
|
|
|
2005-11-20 17:07:47 +00:00
|
|
|
# Gets the instance of the logger.
|
2006-06-26 20:05:00 +00:00
|
|
|
logSys = logging.getLogger("fail2ban.client.config")
|
2005-11-20 17:07:47 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
class ConfigReader(SafeConfigParserWithIncludes):
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-10-24 19:40:51 +00:00
|
|
|
BASE_DIRECTORY = "/etc/fail2ban/"
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2008-03-06 01:19:06 +00:00
|
|
|
SafeConfigParserWithIncludes.__init__(self)
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__opts = None
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-10-24 19:40:51 +00:00
|
|
|
def setBaseDir(folderName):
|
|
|
|
path = folderName.rstrip('/')
|
|
|
|
ConfigReader.BASE_DIRECTORY = path + '/'
|
2008-03-06 01:19:06 +00:00
|
|
|
setBaseDir = staticmethod(setBaseDir)
|
2005-02-18 21:48:34 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-06-26 20:05:00 +00:00
|
|
|
def getBaseDir():
|
2006-10-24 19:40:51 +00:00
|
|
|
return ConfigReader.BASE_DIRECTORY
|
2008-03-06 01:19:06 +00:00
|
|
|
getBaseDir = staticmethod(getBaseDir)
|
2005-02-18 21:48:34 +00:00
|
|
|
|
2006-09-27 21:01:31 +00:00
|
|
|
def read(self, filename):
|
2006-10-24 19:40:51 +00:00
|
|
|
basename = ConfigReader.BASE_DIRECTORY + filename
|
2006-06-26 20:05:00 +00:00
|
|
|
logSys.debug("Reading " + basename)
|
2006-09-07 21:00:44 +00:00
|
|
|
bConf = basename + ".conf"
|
|
|
|
bLocal = basename + ".local"
|
|
|
|
if os.path.exists(bConf) or os.path.exists(bLocal):
|
2008-03-06 01:19:06 +00:00
|
|
|
SafeConfigParserWithIncludes.read(self, [bConf, bLocal])
|
2006-09-07 21:00:44 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
logSys.error(bConf + " and " + bLocal + " do not exist")
|
|
|
|
return False
|
2005-11-20 17:07:47 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
##
|
|
|
|
# Read the options.
|
|
|
|
#
|
|
|
|
# Read the given option in the configuration file. Default values
|
|
|
|
# are used...
|
2005-11-20 17:07:47 +00:00
|
|
|
# Each optionValues entry is composed of an array with:
|
|
|
|
# 0 -> the type of the option
|
|
|
|
# 1 -> the name of the option
|
|
|
|
# 2 -> the default value for the option
|
2006-11-12 14:52:36 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def getOptions(self, sec, options, pOptions = None):
|
2005-02-18 21:48:34 +00:00
|
|
|
values = dict()
|
2005-11-20 17:07:47 +00:00
|
|
|
for option in options:
|
2005-02-18 21:48:34 +00:00
|
|
|
try:
|
2005-02-22 21:10:08 +00:00
|
|
|
if option[0] == "bool":
|
2006-06-26 20:05:00 +00:00
|
|
|
v = self.getboolean(sec, option[1])
|
2005-02-22 21:10:08 +00:00
|
|
|
elif option[0] == "int":
|
2006-06-26 20:05:00 +00:00
|
|
|
v = self.getint(sec, option[1])
|
2005-02-22 21:10:08 +00:00
|
|
|
else:
|
2006-06-26 20:05:00 +00:00
|
|
|
v = self.get(sec, option[1])
|
|
|
|
if not pOptions == None and option[1] in pOptions:
|
|
|
|
continue
|
2005-02-22 21:10:08 +00:00
|
|
|
values[option[1]] = v
|
2006-08-22 21:59:51 +00:00
|
|
|
except NoSectionError, e:
|
|
|
|
# No "Definition" section or wrong basedir
|
|
|
|
logSys.error(e)
|
2006-10-18 20:10:18 +00:00
|
|
|
values[option[1]] = option[2]
|
2005-02-18 21:48:34 +00:00
|
|
|
except NoOptionError:
|
2006-06-26 20:05:00 +00:00
|
|
|
if not option[2] == None:
|
2006-11-12 14:52:36 +00:00
|
|
|
logSys.warn("'%s' not defined in '%s'. Using default value"
|
|
|
|
% (option[1], sec))
|
2006-06-26 20:05:00 +00:00
|
|
|
values[option[1]] = option[2]
|
2005-11-20 17:07:47 +00:00
|
|
|
except ValueError:
|
|
|
|
logSys.warn("Wrong value for '" + option[1] + "' in '" + sec +
|
|
|
|
"'. Using default one: '" + `option[2]` + "'")
|
2005-02-23 17:47:30 +00:00
|
|
|
values[option[1]] = option[2]
|
2006-09-19 20:38:32 +00:00
|
|
|
return values
|