1# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
2# vi: set ft=python sts=4 ts=4 sw=4 noet :
3
4# This file is part of Fail2Ban.
5#
6# Fail2Ban is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# Fail2Ban is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with Fail2Ban; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
20# Author: Cyril Jaquier
21#
22
23__author__ = "Cyril Jaquier"
24__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
25__license__ = "GPL"
26
27from .fail2banreader import Fail2banReader
28from .jailsreader import JailsReader
29from ..helpers import getLogger
30
31# Gets the instance of the logger.
32logSys = getLogger(__name__)
33
34
35class Configurator:
36
37	def __init__(self, force_enable=False, share_config=None):
38		self.__settings = dict()
39		self.__streams = dict()
40		# always share all config readers:
41		if share_config is None:
42			share_config = dict()
43		self.__share_config = share_config
44		self.__fail2ban = Fail2banReader(share_config=share_config)
45		self.__jails = JailsReader(force_enable=force_enable, share_config=share_config)
46
47	def Reload(self):
48		# clear all shared handlers:
49		self.__share_config.clear()
50
51	def setBaseDir(self, folderName):
52		self.__fail2ban.setBaseDir(folderName)
53		self.__jails.setBaseDir(folderName)
54
55	def getBaseDir(self):
56		fail2ban_basedir = self.__fail2ban.getBaseDir()
57		jails_basedir = self.__jails.getBaseDir()
58		if fail2ban_basedir != jails_basedir:
59			logSys.error("fail2ban.conf and jails.conf readers have differing "
60						 "basedirs: %r and %r. "
61						 "Returning the one for fail2ban.conf"
62						 % (fail2ban_basedir, jails_basedir))
63		return fail2ban_basedir
64
65	def readEarly(self):
66		self.__fail2ban.read()
67
68	def readAll(self):
69		self.readEarly()
70		self.__jails.read()
71
72	def getEarlyOptions(self):
73		return self.__fail2ban.getEarlyOptions()
74
75	def getOptions(self, jail=None, updateMainOpt=None, ignoreWrong=True):
76		self.__fail2ban.getOptions(updateMainOpt)
77		return self.__jails.getOptions(jail, ignoreWrong=ignoreWrong)
78
79	def convertToProtocol(self, allow_no_files=False):
80		self.__streams["general"] = self.__fail2ban.convert()
81		self.__streams["jails"] = self.__jails.convert(allow_no_files=allow_no_files)
82
83	def getConfigStream(self):
84		cmds = list()
85		for opt in self.__streams["general"]:
86			cmds.append(opt)
87		for opt in self.__streams["jails"]:
88			cmds.append(opt)
89		return cmds
90
91