From d1de20dd4181c9506c29d218c20b16fc7239cf38 Mon Sep 17 00:00:00 2001 From: sebres Date: Mon, 14 Aug 2017 18:29:36 +0200 Subject: [PATCH] Move some ticket-independent tag replacements from ActionInfo to ADD_REPL_TAGS (will be wrapped in replaceTag into calling map). Thus tags `` and `` could be used without ticket (a. g. in `actionstart` etc.). Closes gh-1859. --- fail2ban/server/action.py | 17 ++++++++++++++--- fail2ban/server/actions.py | 7 +------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/fail2ban/server/action.py b/fail2ban/server/action.py index d00458ba..81ffbe2f 100644 --- a/fail2ban/server/action.py +++ b/fail2ban/server/action.py @@ -33,7 +33,7 @@ from abc import ABCMeta from collections import MutableMapping from .failregex import mapTag2Opt -from .ipdns import asip +from .ipdns import asip, DNSUtils from .mytime import MyTime from .utils import Utils from ..helpers import getLogger, _merge_copy_dicts, substituteRecursiveTags, TAG_CRE, MAX_TAG_REPLACE_COUNT @@ -52,11 +52,18 @@ FCUSTAG_CRE = re.compile(r''); # currently uppercase only CONDITIONAL_FAM_RE = re.compile(r"^(\w+)\?(family)=") +# Special tags: +DYN_REPL_TAGS = { + # System-information: + "fq-hostname": lambda: str(DNSUtils.getHostname(fqdn=True)), + "sh-hostname": lambda: str(DNSUtils.getHostname(fqdn=False)) +} # New line, space ADD_REPL_TAGS = { "br": "\n", "sp": " " } +ADD_REPL_TAGS.update(DYN_REPL_TAGS) class CallingMap(MutableMapping, object): @@ -571,6 +578,8 @@ class CommandAction(ActionBase): if csubkey is not None: cache[csubkey] = subInfo + # additional replacement as calling map: + ADD_REPL_TAGS_CM = CallingMap(ADD_REPL_TAGS) # substitution callable, used by interpolation of each tag def substVal(m): tag = m.group(1) # tagname from match @@ -581,7 +590,7 @@ class CommandAction(ActionBase): value = subInfo.get(tag) if value is None: # fallback (no or default replacement) - return ADD_REPL_TAGS.get(tag, m.group()) + return ADD_REPL_TAGS_CM.get(tag, m.group()) value = str(value) # assure string if tag in cls._escapedTags: # That one needs to be escaped since its content is @@ -651,6 +660,8 @@ class CommandAction(ActionBase): # replacement for tag: return value + # additional replacement as calling map: + ADD_REPL_TAGS_CM = CallingMap(ADD_REPL_TAGS) # substitution callable, used by interpolation of each tag def substVal(m): tag = m.group(1) # tagname from match @@ -658,7 +669,7 @@ class CommandAction(ActionBase): value = aInfo[tag] except KeyError: # fallback (no or default replacement) - return ADD_REPL_TAGS.get(tag, m.group()) + return ADD_REPL_TAGS_CM.get(tag, m.group()) value = str(value) # assure string # replacement for tag: return escapeVal(tag, value) diff --git a/fail2ban/server/actions.py b/fail2ban/server/actions.py index f8f8d4e1..73042434 100644 --- a/fail2ban/server/actions.py +++ b/fail2ban/server/actions.py @@ -35,7 +35,6 @@ except ImportError: OrderedDict = dict from .banmanager import BanManager -from .ipdns import DNSUtils from .jailthread import JailThread from .action import ActionBase, CommandAction, CallingMap from .mytime import MyTime @@ -306,10 +305,7 @@ class Actions(JailThread, Mapping): "ipmatches": lambda self: "\n".join(self._mi4ip(True).getMatches()), "ipjailmatches": lambda self: "\n".join(self._mi4ip().getMatches()), "ipfailures": lambda self: self._mi4ip(True).getAttempt(), - "ipjailfailures": lambda self: self._mi4ip().getAttempt(), - # system-information: - "fq-hostname": lambda self: DNSUtils.getHostname(fqdn=True), - "sh-hostname": lambda self: DNSUtils.getHostname(fqdn=False) + "ipjailfailures": lambda self: self._mi4ip().getAttempt() } __slots__ = CallingMap.__slots__ + ('__ticket', '__jail', '__mi4ip') @@ -368,7 +364,6 @@ class Actions(JailThread, Mapping): def __getActionInfo(self, ticket): - ip = ticket.getIP() aInfo = Actions.ActionInfo(ticket, self._jail) return aInfo