fail2ban.server.action module
*****************************

class fail2ban.server.action.ActionBase(jail, name)

   Bases: "object"

   An abstract base class for actions in Fail2Ban.

   Action Base is a base definition of what methods need to be in
   place to create a Python based action for Fail2Ban. This class can
   be inherited from to ease implementation. Required methods:

   * __init__(jail, name)

   * start()

   * stop()

   * ban(aInfo)

   * unban(aInfo)

   Called when action is created, but before the jail/actions is
   started. This should carry out necessary methods to initialise the
   action but not "start" the action.

   Parameters:
      **jail** : Jail
         The jail in which the action belongs to.

      **name** : str
         Name assigned to the action.

   -[ Notes ]-

   Any additional arguments specified in *jail.conf* or passed via
   *fail2ban-client* will be passed as keyword arguments.

   -[ Methods ]-

   +------------+--------------------------------------------------------------------------------------------+
   | "ban"(sel  | Executed when a ban occurs.                                                                |
   | f, aInfo)  |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "reban"(s  | Executed when a ban occurs.                                                                |
   | elf,       |                                                                                            |
   | aInfo)     |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "start"(s  | Executed when the jail/action is started.                                                  |
   | elf)       |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "stop"(se  | Executed when the jail/action is stopped.                                                  |
   | lf)        |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "unban"(s  | Executed when a ban expires.                                                               |
   | elf,       |                                                                                            |
   | aInfo)     |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+

   ban(self, aInfo)

      Executed when a ban occurs.

      Parameters:
         **aInfo** : dict
            Dictionary which includes information in relation to the
            ban.

   reban(self, aInfo)

      Executed when a ban occurs.

      Parameters:
         **aInfo** : dict
            Dictionary which includes information in relation to the
            ban.

   start(self)

      Executed when the jail/action is started.

   stop(self)

      Executed when the jail/action is stopped.

   unban(self, aInfo)

      Executed when a ban expires.

      Parameters:
         **aInfo** : dict
            Dictionary which includes information in relation to the
            ban.

class fail2ban.server.action.CallingMap(*args, **kwargs)

   Bases: "MutableMapping", "object"

   A Mapping type which returns the result of callable values.

   *CallingMap* behaves similar to a standard python dictionary, with
   the exception that any values which are callable, are called and
   the result is returned as the value. No error handling is in place,
   such that any errors raised in the callable will raised as usual.
   Actual dictionary is stored in property *data*, and can be accessed
   to obtain original callable values.

   Attributes:
      **data** : dict
         The dictionary data which can be accessed to obtain items
         uncalled

   -[ Methods ]-

   +------------+--------------------------------------------------------------------------------------------+
   | "clear"(s  |                                                                                            |
   | elf)       |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "get"(sel  |                                                                                            |
   | f, key[,   |                                                                                            |
   | default])  |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "items"(s  |                                                                                            |
   | elf)       |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "keys"(se  |                                                                                            |
   | lf)        |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "pop"(sel  | If key is not found, d is returned if given, otherwise KeyError is raised.                 |
   | f, key[,   |                                                                                            |
   | default])  |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "popitem"  | as a 2-tuple; but raise KeyError if D is empty.                                            |
   | (self)     |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "setdefau  |                                                                                            |
   | lt"(self,  |                                                                                            |
   | key[,      |                                                                                            |
   | default])  |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "update"(  | If E present and has a .keys() method, does:     for k in E: D[k] = E[k] If E present and  |
   | self[,     | lacks .keys() method, does:     for (k, v) in E: D[k] = v In either case, this is followed |
   | other])    | by: for k, v in F.items(): D[k] = v                                                        |
   +------------+--------------------------------------------------------------------------------------------+
   | "values"(  |                                                                                            |
   | self)      |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+

   +----------------+------------+
   | **copy**       |            |
   +----------------+------------+
   | **getRawItem** |            |
   +----------------+------------+
   | **reset**      |            |
   +----------------+------------+

   CM_REPR_ITEMS = ()

   copy(self)

   data

   getRawItem(self, key)

   immutable

   reset(self, immutable=True)

   storage

class fail2ban.server.action.CommandAction(jail, name)

   Bases: "ActionBase"

   A action which executes OS shell commands.

   This is the default type of action which Fail2Ban uses.

   Default sets all commands for actions as empty string, such no
   command is executed.

   Parameters:
      **jail** : Jail
         The jail in which the action belongs to.

      **name** : str
         Name assigned to the action.

   Attributes:
      **actionban**
      **actioncheck**
      **actionreban**
      **actionreload**
      **actionrepair**
      **actionstart**
      **actionstop**
      **actionunban**
      **timeout**
   -[ Methods ]-

   +------------+--------------------------------------------------------------------------------------------+
   | "ban"(sel  | Executes the given command ("actionban" or "actionreban").                                 |
   | f, aInfo[, |                                                                                            |
   | cmd])      |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "clearAll  | Clear all lists/dicts parameters (used by reloading)                                       |
   | Params"(s  |                                                                                            |
   | elf)       |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "consiste  | Executes the invariant check with repair if expected (conditional).                        |
   | ncyCheck"  |                                                                                            |
   | (self[, b  |                                                                                            |
   | eforeRepa  |                                                                                            |
   | ir])       |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "escapeTa  | Escape characters which may be used for command injection.                                 |
   | g"(value)  |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "executeC  | Executes a command.                                                                        |
   | md"(realC  |                                                                                            |
   | md[,       |                                                                                            |
   | timeout])  |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "flush"(s  | Executes the "actionflush" command.                                                        |
   | elf)       |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "invalida  | Increments ban epoch of jail and this action, so already banned tickets would cause a re-  |
   | teBanEpoc  | ban for all tickets with previous epoch.                                                   |
   | h"(self)   |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "prolong"  | Executes the "actionprolong" command.                                                      |
   | (self,     |                                                                                            |
   | aInfo)     |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "reban"(s  | Executes the "actionreban" command if available, otherwise simply repeat "actionban".      |
   | elf,       |                                                                                            |
   | aInfo)     |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "reload"(  | Executes the "actionreload" command.                                                       |
   | self, \*\  |                                                                                            |
   | *kwargs)   |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "replaceD  | Replaces dynamical tags in *query* with property values.                                   |
   | ynamicTag  |                                                                                            |
   | s"(realCm  |                                                                                            |
   | d, aInfo[, |                                                                                            |
   | escapeVal  |                                                                                            |
   | ])         |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "replaceT  | Replaces tags in *query* with property values.                                             |
   | ag"(query, |                                                                                            |
   | aInfo[, c  |                                                                                            |
   | onditiona  |                                                                                            |
   | l, ...])   |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "start"(s  | Executes the "actionstart" command.                                                        |
   | elf)       |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "stop"(se  | Executes the "actionstop" command.                                                         |
   | lf)        |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+
   | "unban"(s  | Executes the "actionunban" command.                                                        |
   | elf,       |                                                                                            |
   | aInfo)     |                                                                                            |
   +------------+--------------------------------------------------------------------------------------------+

   ESCAPE_CRE = re.compile('[\\\\#&;`|*?~<>\\^\\(\\)\\[\\]{}$\'"\\n\\r]')

   ESCAPE_VN_CRE = re.compile('\\W')

   ban(self, aInfo, cmd='<actionban>')

      Executes the given command ("actionban" or "actionreban").

      Replaces the tags in the action command with actions properties
      and ban information, and executes the resulting command.

      Parameters:
         **aInfo** : dict
            Dictionary which includes information in relation to the
            ban.

   property banEpoch

   clearAllParams(self)

      Clear all lists/dicts parameters (used by reloading)

   consistencyCheck(self, beforeRepair=None)

      Executes the invariant check with repair if expected
      (conditional).

   classmethod escapeTag(value)

      Escape characters which may be used for command injection.

      Parameters:
         **value** : str
            A string of which characters will be escaped.

      Returns:
         str
            *value* with certain characters escaped.

      -[ Notes ]-

      The following characters are escaped:

         \#&;`|*?~<>^()[]{}$'"

   static executeCmd(realCmd, timeout=60, \*\*kwargs)

      Executes a command.

      Parameters:
         **realCmd** : str
            The command to execute.

         **timeout** : int
            The time out in seconds for the command.

      Returns:
         bool
            True if the command succeeded.

      Raises:
         OSError
            If command fails to be executed.

         RuntimeError
            If command execution times out.

   flush(self)

      Executes the "actionflush" command.

      Command executed in order to flush all bans at once (e. g. by
      stop/shutdown  the system), instead of unbanning of each single
      ticket.

      Replaces the tags in the action command with actions properties
      and executes the resulting command.

   invalidateBanEpoch(self)

      Increments ban epoch of jail and this action, so already banned
      tickets would cause a re-ban for all tickets with previous
      epoch.

   prolong(self, aInfo)

      Executes the "actionprolong" command.

      Replaces the tags in the action command with actions properties
      and ban information, and executes the resulting command.

      Parameters:
         **aInfo** : dict
            Dictionary which includes information in relation to the
            ban.

   reban(self, aInfo)

      Executes the "actionreban" command if available, otherwise
      simply repeat "actionban".

      Replaces the tags in the action command with actions properties
      and ban information, and executes the resulting command.

      Parameters:
         **aInfo** : dict
            Dictionary which includes information in relation to the
            ban.

   reload(self, \*\*kwargs)

      Executes the "actionreload" command.

      Parameters:
         **kwargs** : dict
            Currently unused, because CommandAction do not support
            initOpts

         **Replaces the tags in the action command with actions
         properties**
         **and executes the resulting command.**
   classmethod replaceDynamicTags(realCmd, aInfo, escapeVal=None)

      Replaces dynamical tags in *query* with property values.

      Parameters:
         **query** : str
            String with tags.

         **aInfo** : dict
            Tags(keys) and associated values for substitution in
            query.

      Returns:
         str
            shell script as string or array with tags replaced (direct
            or as variables).

   classmethod replaceTag(query, aInfo, conditional='', addrepl=None, cache=None)

      Replaces tags in *query* with property values.

      Parameters:
         **query** : str
            String with tags.

         **aInfo** : dict
            Tags(keys) and associated values for substitution in
            query.

      Returns:
         str
            *query* string with tags replaced.

   start(self)

      Executes the "actionstart" command.

      Replace the tags in the action command with actions properties
      and executes the resulting command.

   stop(self)

      Executes the "actionstop" command.

      Replaces the tags in the action command with actions properties
      and executes the resulting command.

   unban(self, aInfo)

      Executes the "actionunban" command.

      Replaces the tags in the action command with actions properties
      and ban information, and executes the resulting command.

      Parameters:
         **aInfo** : dict
            Dictionary which includes information in relation to the
            ban.
