1
# Copyright (C) 2004,2005 PreludeIDS Technologies. All Rights Reserved.
2
# Author: Nicolas Delon <nicolas.delon@prelude-ids.com>
4
# This file is part of the Prewikka program.
6
# This program 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, or (at your option)
11
# This program 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.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; see the file COPYING. If not, write to
18
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
from prewikka import utils
26
class Error(Exception):
31
def __init__(self, root=prelude.IDMEF_CLASS_ID_MESSAGE, text=""):
32
self.CriteriaList = []
33
self._idmef_class_tree(root, text, self.CriteriaList)
35
def _idmef_class_tree(self, root, criteria_root, outlist):
39
name = prelude.idmef_class_get_child_name(root, i)
43
if criteria_root != None:
44
criteria = "%s.%s" % (criteria_root, name)
46
criteria = "%s" % (name)
48
if criteria == "alert.target.file.linkage":
51
if prelude.idmef_class_get_child_value_type(root, i) == prelude.IDMEF_VALUE_TYPE_CLASS:
52
self._idmef_class_tree(prelude.idmef_class_get_child_class(root, i), criteria, outlist)
54
outlist.append(criteria)
60
def __init__(self, name, comment, elements, formula):
62
self.comment = comment
63
self.elements = elements
64
self.formula = formula
66
crit = prelude.idmef_criteria_new_from_string(str(self))
67
prelude.idmef_criteria_destroy(crit)
69
def _replace(self, element):
70
element = element.group(1)
71
if element in ("and", "AND", "&&"):
74
if element in ("or", "OR", "||"):
77
if not self.elements.has_key(element):
78
raise Error(_("Invalid filter element '%s' referenced from filter formula") % element)
80
criteria, operator, value = self.elements[element]
81
return "%s %s '%s'" % (criteria, operator, utils.escape_criteria(value))
84
return re.sub("(\w+)", self._replace, self.formula)
88
AlertFilterList = CriteriaIDMEF(prelude.IDMEF_CLASS_ID_ALERT, "alert").CriteriaList
89
HeartbeatFilterList = CriteriaIDMEF(prelude.IDMEF_CLASS_ID_HEARTBEAT, "heartbeat").CriteriaList
92
if __name__ == "__main__":
93
print Filter("foo", "",
94
{ "A": ("alert.source(0).node.category", "=", "blah"),
95
"B": ("alert.messageid", "=", "2") },