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 27import os 28 29from .configreader import DefinitionInitConfigReader 30from ..helpers import getLogger 31from ..server.action import CommandAction 32 33# Gets the instance of the logger. 34logSys = getLogger(__name__) 35 36 37class ActionReader(DefinitionInitConfigReader): 38 39 _configOpts = { 40 "actionstart": ["string", None], 41 "actionstart_on_demand": ["bool", None], 42 "actionstop": ["string", None], 43 "actionflush": ["string", None], 44 "actionreload": ["string", None], 45 "actioncheck": ["string", None], 46 "actionrepair": ["string", None], 47 "actionrepair_on_unban": ["bool", None], 48 "actionban": ["string", None], 49 "actionprolong": ["string", None], 50 "actionreban": ["string", None], 51 "actionunban": ["string", None], 52 "norestored": ["bool", None], 53 } 54 55 def __init__(self, file_, jailName, initOpts, **kwargs): 56 # always supply jail name as name parameter if not specified in options: 57 n = initOpts.get("name") 58 if n is None: 59 initOpts["name"] = n = jailName 60 actname = initOpts.get("actname") 61 if actname is None: 62 actname = file_ 63 # ensure we've unique action name per jail: 64 if n != jailName: 65 actname += n[len(jailName):] if n.startswith(jailName) else '-' + n 66 initOpts["actname"] = actname 67 self._name = actname 68 DefinitionInitConfigReader.__init__( 69 self, file_, jailName, initOpts, **kwargs) 70 71 def setFile(self, fileName): 72 self.__file = fileName 73 DefinitionInitConfigReader.setFile(self, os.path.join("action.d", fileName)) 74 75 def getFile(self): 76 return self.__file 77 78 def setName(self, name): 79 self._name = name 80 81 def getName(self): 82 return self._name 83 84 def convert(self): 85 opts = self.getCombined( 86 ignore=CommandAction._escapedTags | set(('timeout', 'bantime'))) 87 # stream-convert: 88 head = ["set", self._jailName] 89 stream = list() 90 stream.append(head + ["addaction", self._name]) 91 multi = [] 92 for opt, optval in opts.items(): 93 if opt in self._configOpts and not opt.startswith('known/'): 94 multi.append([opt, optval]) 95 if self._initOpts: 96 for opt, optval in self._initOpts.items(): 97 if opt not in self._configOpts and not opt.startswith('known/'): 98 multi.append([opt, optval]) 99 if len(multi) > 1: 100 stream.append(["multi-set", self._jailName, "action", self._name, multi]) 101 elif len(multi): 102 stream.append(["set", self._jailName, "action", self._name] + multi[0]) 103 104 return stream 105