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 .configreader import ConfigReader
28from ..helpers import getLogger, str2LogLevel
29
30# Gets the instance of the logger.
31logSys = getLogger(__name__)
32
33
34class Fail2banReader(ConfigReader):
35
36	def __init__(self, **kwargs):
37		ConfigReader.__init__(self, **kwargs)
38
39	def read(self):
40		ConfigReader.read(self, "fail2ban")
41
42	def getEarlyOptions(self):
43		opts = [
44			["string", "socket", "/var/run/fail2ban/fail2ban.sock"],
45			["string", "pidfile", "/var/run/fail2ban/fail2ban.pid"],
46			["string", "loglevel", "INFO"],
47			["string", "logtarget", "/var/log/fail2ban.log"],
48			["string", "syslogsocket", "auto"]
49		]
50		return ConfigReader.getOptions(self, "Definition", opts)
51
52	def getOptions(self, updateMainOpt=None):
53		opts = [["string", "loglevel", "INFO" ],
54				["string", "logtarget", "STDERR"],
55				["string", "syslogsocket", "auto"],
56				["string", "dbfile", "/var/lib/fail2ban/fail2ban.sqlite3"],
57				["int",    "dbmaxmatches", None],
58				["string", "dbpurgeage", "1d"]]
59		self.__opts = ConfigReader.getOptions(self, "Definition", opts)
60		if updateMainOpt:
61			self.__opts.update(updateMainOpt)
62		# check given log-level:
63		str2LogLevel(self.__opts.get('loglevel', 0))
64		# thread options:
65		opts = [["int", "stacksize", ],
66		]
67		if self.has_section("Thread"):
68			thopt = ConfigReader.getOptions(self, "Thread", opts)
69			if thopt:
70				self.__opts['thread'] = thopt
71
72	def convert(self):
73		# Ensure logtarget/level set first so any db errors are captured
74		# Also dbfile should be set before all other database options.
75		# So adding order indices into items, to be stripped after sorting, upon return
76		order = {"thread":0, "syslogsocket":11, "loglevel":12, "logtarget":13,
77			"dbfile":50, "dbmaxmatches":51, "dbpurgeage":51}
78		stream = list()
79		for opt in self.__opts:
80			if opt in order:
81				stream.append((order[opt], ["set", opt, self.__opts[opt]]))
82		return [opt[1] for opt in sorted(stream)]
83
84