"Fossies" - the Fresh Open Source Software Archive

Member "fail2ban-0.11.1/fail2ban/client/actionreader.py" (11 Jan 2020, 3413 Bytes) of package /linux/misc/fail2ban-0.11.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "actionreader.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.10.5_vs_0.11.1.

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