PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/fstmerge/examples/Fail2ban/rev579-685/left-trunk-685/client/jailreader.py

https://github.com/RoDaniel/featurehouse
Python | 143 lines | 136 code | 7 blank | 0 comment | 1 complexity | 4e3383083ca52cbee1145fc8c786f481 MD5 | raw file
  1. __author__ = "Cyril Jaquier"
  2. __version__ = "$Revision: 1.1 $"
  3. __date__ = "$Date: 2010-07-25 12:46:55 $"
  4. __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
  5. __license__ = "GPL"
  6. import logging, re, glob
  7. from configreader import ConfigReader
  8. from filterreader import FilterReader
  9. from actionreader import ActionReader
  10. logSys = logging.getLogger("fail2ban.client.config")
  11. class JailReader(ConfigReader):
  12. actionCRE = re.compile("^((?:\w|-|_|\.)+)(?:\[(.*)\])?$")
  13. def __init__(self, name):
  14. ConfigReader.__init__(self)
  15. self.__name = name
  16. self.__filter = None
  17. self.__actions = list()
  18. def setName(self, value):
  19. self.__name = value
  20. def getName(self):
  21. return self.__name
  22. def read(self):
  23. ConfigReader.read(self, "jail")
  24. def isEnabled(self):
  25. return self.__opts["enabled"]
  26. def getOptions(self):
  27. opts = [["bool", "enabled", "false"],
  28. ["string", "logpath", "/var/log/messages"],
  29. ["string", "backend", "auto"],
  30. ["int", "maxretry", 3],
  31. ["int", "findtime", 600],
  32. ["int", "bantime", 600],
  33. ["string", "failregex", None],
  34. ["string", "ignoreregex", None],
  35. ["string", "ignoreip", None],
  36. ["string", "filter", ""],
  37. ["string", "action", ""]]
  38. self.__opts = ConfigReader.getOptions(self, self.__name, opts)
  39. if self.isEnabled():
  40. self.__filter = FilterReader(self.__opts["filter"], self.__name)
  41. ret = self.__filter.read()
  42. if ret:
  43. self.__filter.getOptions(self.__opts)
  44. else:
  45. logSys.error("Unable to read the filter")
  46. return False
  47. for act in self.__opts["action"].split('\n'):
  48. try:
  49. splitAct = JailReader.splitAction(act)
  50. action = ActionReader(splitAct, self.__name)
  51. ret = action.read()
  52. if ret:
  53. action.getOptions(self.__opts)
  54. self.__actions.append(action)
  55. else:
  56. raise AttributeError("Unable to read action")
  57. except AttributeError, e:
  58. logSys.error("Error in action definition " + act)
  59. logSys.debug(e)
  60. return False
  61. return True
  62. def convert(self):
  63. stream = []
  64. for opt in self.__opts:
  65. if opt == "logpath":
  66. for path in self.__opts[opt].split("\n"):
  67. pathList = glob.glob(path)
  68. if len(pathList) == 0:
  69. logSys.error("No file found for " + path)
  70. for p in pathList:
  71. stream.append(["set", self.__name, "addlogpath", p])
  72. elif opt == "backend":
  73. backend = self.__opts[opt]
  74. elif opt == "maxretry":
  75. stream.append(["set", self.__name, "maxretry", self.__opts[opt]])
  76. elif opt == "ignoreip":
  77. for ip in self.__opts[opt].split():
  78. if ip != '':
  79. stream.append(["set", self.__name, "addignoreip", ip])
  80. elif opt == "findtime":
  81. stream.append(["set", self.__name, "findtime", self.__opts[opt]])
  82. elif opt == "bantime":
  83. stream.append(["set", self.__name, "bantime", self.__opts[opt]])
  84. elif opt == "failregex":
  85. stream.append(["set", self.__name, "failregex", self.__opts[opt]])
  86. elif opt == "ignoreregex":
  87. stream.append(["set", self.__name, "ignoreregex", self.__opts[opt]])
  88. stream.extend(self.__filter.convert())
  89. for action in self.__actions:
  90. stream.extend(action.convert())
  91. stream.insert(0, ["add", self.__name, backend])
  92. return stream
  93. def splitAction(action):
  94. m = JailReader.actionCRE.match(action)
  95. d = dict()
  96. if not m.group(2) == None:
  97. actions = ""
  98. escapeChar = None
  99. allowComma = False
  100. for c in m.group(2):
  101. if c in ('"', "'") and not allowComma:
  102. escapeChar = c
  103. allowComma = True
  104. elif c == escapeChar:
  105. escapeChar = None
  106. allowComma = False
  107. else:
  108. if c == ',' and allowComma:
  109. actions += "<COMMA>"
  110. else:
  111. actions += c
  112. actionsSplit = actions.split(',')
  113. actionsSplit = [n.replace("<COMMA>", ',') for n in actionsSplit]
  114. for param in actionsSplit:
  115. p = param.split('=')
  116. try:
  117. d[p[0].strip()] = p[1].strip()
  118. except IndexError:
  119. logSys.error("Invalid argument %s in '%s'" % (p, m.group(2)))
  120. return [m.group(1), d]
  121. splitAction = staticmethod(splitAction)