mirror of https://github.com/fail2ban/fail2ban
code review and inline docu
parent
32f3c1dbf3
commit
6ba0546824
|
@ -200,13 +200,16 @@ after = 1.conf
|
||||||
def get_sections(self):
|
def get_sections(self):
|
||||||
return self._sections
|
return self._sections
|
||||||
|
|
||||||
def options(self, section, onlyOwn=False):
|
def options(self, section, withDefault=True):
|
||||||
"""Return a list of option names for the given section name."""
|
"""Return a list of option names for the given section name.
|
||||||
|
|
||||||
|
Parameter `withDefault` controls the include of names from section `[DEFAULT]`
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
opts = self._sections[section]
|
opts = self._sections[section]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise NoSectionError(section)
|
raise NoSectionError(section)
|
||||||
if not onlyOwn:
|
if withDefault:
|
||||||
# mix it with defaults:
|
# mix it with defaults:
|
||||||
return set(opts.keys()) | set(self._defaults)
|
return set(opts.keys()) | set(self._defaults)
|
||||||
# only own option names:
|
# only own option names:
|
||||||
|
|
|
@ -126,9 +126,13 @@ class ConfigReader():
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise NoSectionError(section)
|
raise NoSectionError(section)
|
||||||
|
|
||||||
def options(self, section, onlyOwn=False):
|
def options(self, section, withDefault=False):
|
||||||
|
"""Return a list of option names for the given section name.
|
||||||
|
|
||||||
|
Parameter `withDefault` controls the include of names from section `[DEFAULT]`
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
return self._cfg.options(section, onlyOwn)
|
return self._cfg.options(section, withDefault)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise NoSectionError(section)
|
raise NoSectionError(section)
|
||||||
|
|
||||||
|
@ -317,7 +321,7 @@ class DefinitionInitConfigReader(ConfigReader):
|
||||||
if self.has_section("Init"):
|
if self.has_section("Init"):
|
||||||
# get only own options (without options from default):
|
# get only own options (without options from default):
|
||||||
getopt = lambda opt: self.get("Init", opt)
|
getopt = lambda opt: self.get("Init", opt)
|
||||||
for opt in self.options("Init", onlyOwn=True):
|
for opt in self.options("Init", withDefault=False):
|
||||||
if opt == '__name__': continue
|
if opt == '__name__': continue
|
||||||
v = None
|
v = None
|
||||||
if not opt.startswith('known/'):
|
if not opt.startswith('known/'):
|
||||||
|
|
|
@ -118,6 +118,15 @@ class Utils():
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def buildShellCmd(realCmd, varsDict):
|
def buildShellCmd(realCmd, varsDict):
|
||||||
|
"""Generates new shell command as array, contains map as variables to
|
||||||
|
arguments statement (varsStat), the command (realCmd) used this variables and
|
||||||
|
the list of the arguments, mapped from varsDict
|
||||||
|
|
||||||
|
Example:
|
||||||
|
buildShellCmd('echo "V2: $v2, V1: $v1"', {"v1": "val 1", "v2": "val 2", "vUnused": "unused var"})
|
||||||
|
returns:
|
||||||
|
['v1=$0 v2=$1 vUnused=$2 \necho "V2: $v2, V1: $v1"', 'val 1', 'val 2', 'unused var']
|
||||||
|
"""
|
||||||
# build map as array of vars and command line array:
|
# build map as array of vars and command line array:
|
||||||
varsStat = ""
|
varsStat = ""
|
||||||
if not isinstance(realCmd, list):
|
if not isinstance(realCmd, list):
|
||||||
|
@ -172,7 +181,7 @@ class Utils():
|
||||||
else: # pragma: no cover - currently unused
|
else: # pragma: no cover - currently unused
|
||||||
env = _merge_dicts(os.environ, varsDict)
|
env = _merge_dicts(os.environ, varsDict)
|
||||||
realCmdId = id(realCmd)
|
realCmdId = id(realCmd)
|
||||||
outCmd = lambda level: logSys.log(level, "%x -- exec: %s", realCmdId, realCmd)
|
logCmd = lambda level: logSys.log(level, "%x -- exec: %s", realCmdId, realCmd)
|
||||||
try:
|
try:
|
||||||
popen = subprocess.Popen(
|
popen = subprocess.Popen(
|
||||||
realCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell, env=env,
|
realCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell, env=env,
|
||||||
|
@ -190,7 +199,7 @@ class Utils():
|
||||||
retcode = retcode[1]
|
retcode = retcode[1]
|
||||||
# if timeout:
|
# if timeout:
|
||||||
if retcode is None:
|
if retcode is None:
|
||||||
if outCmd: outCmd(logging.ERROR); outCmd = None
|
if logCmd: logCmd(logging.ERROR); logCmd = None
|
||||||
logSys.error("%x -- timed out after %s seconds." %
|
logSys.error("%x -- timed out after %s seconds." %
|
||||||
(realCmdId, timeout))
|
(realCmdId, timeout))
|
||||||
pgid = os.getpgid(popen.pid)
|
pgid = os.getpgid(popen.pid)
|
||||||
|
@ -207,6 +216,7 @@ class Utils():
|
||||||
if retcode is None and not Utils.pid_exists(pgid): # pragma: no cover
|
if retcode is None and not Utils.pid_exists(pgid): # pragma: no cover
|
||||||
retcode = signal.SIGKILL
|
retcode = signal.SIGKILL
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
|
if logCmd: logCmd(logging.ERROR); logCmd = None
|
||||||
stderr = "%s -- failed with %s" % (realCmd, e)
|
stderr = "%s -- failed with %s" % (realCmd, e)
|
||||||
logSys.error(stderr)
|
logSys.error(stderr)
|
||||||
if not popen:
|
if not popen:
|
||||||
|
@ -214,7 +224,7 @@ class Utils():
|
||||||
|
|
||||||
std_level = logging.DEBUG if retcode in success_codes else logging.ERROR
|
std_level = logging.DEBUG if retcode in success_codes else logging.ERROR
|
||||||
if std_level > logSys.getEffectiveLevel():
|
if std_level > logSys.getEffectiveLevel():
|
||||||
if outCmd: outCmd(std_level-1); outCmd = None
|
if logCmd: logCmd(std_level-1); logCmd = None
|
||||||
# if we need output (to return or to log it):
|
# if we need output (to return or to log it):
|
||||||
if output or std_level >= logSys.getEffectiveLevel():
|
if output or std_level >= logSys.getEffectiveLevel():
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue