~ubuntu-branches/ubuntu/hardy/prewikka/hardy

« back to all changes in this revision

Viewing changes to prewikka/Filter.py

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2007-04-11 14:41:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070411144109-2hh7zx3amwd27b4l
Tags: upstream-0.9.10
ImportĀ upstreamĀ versionĀ 0.9.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004,2005 PreludeIDS Technologies. All Rights Reserved.
 
2
# Author: Nicolas Delon <nicolas.delon@prelude-ids.com>
 
3
#
 
4
# This file is part of the Prewikka program.
 
5
#
 
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)
 
9
# any later version.
 
10
#
 
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.
 
15
#
 
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.
 
19
 
 
20
 
 
21
import re
 
22
import prelude
 
23
 
 
24
from prewikka import utils
 
25
 
 
26
class Error(Exception):
 
27
    pass
 
28
 
 
29
 
 
30
class CriteriaIDMEF:
 
31
    def __init__(self, root=prelude.IDMEF_CLASS_ID_MESSAGE, text=""):
 
32
        self.CriteriaList = []
 
33
        self._idmef_class_tree(root, text, self.CriteriaList)
 
34
 
 
35
    def _idmef_class_tree(self, root, criteria_root, outlist):
 
36
 
 
37
        i = 0
 
38
        while True:
 
39
            name = prelude.idmef_class_get_child_name(root, i)
 
40
            if name == None: 
 
41
                break
 
42
            
 
43
            if criteria_root != None:
 
44
                criteria = "%s.%s" % (criteria_root, name)
 
45
            else:
 
46
                criteria = "%s" % (name)
 
47
 
 
48
            if criteria == "alert.target.file.linkage":
 
49
                break
 
50
 
 
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)
 
53
            else:
 
54
                outlist.append(criteria)
 
55
 
 
56
            i = i + 1
 
57
 
 
58
 
 
59
class Filter:
 
60
    def __init__(self, name, comment, elements, formula):
 
61
        self.name = name
 
62
        self.comment = comment
 
63
        self.elements = elements
 
64
        self.formula = formula
 
65
 
 
66
        crit = prelude.idmef_criteria_new_from_string(str(self))
 
67
        prelude.idmef_criteria_destroy(crit)
 
68
        
 
69
    def _replace(self, element):
 
70
        element = element.group(1)
 
71
        if element in ("and", "AND", "&&"):
 
72
            return "&&"
 
73
 
 
74
        if element in ("or", "OR", "||"):
 
75
            return "||"
 
76
 
 
77
        if not self.elements.has_key(element):
 
78
            raise Error(_("Invalid filter element '%s' referenced from filter formula") % element)
 
79
 
 
80
        criteria, operator, value = self.elements[element]
 
81
        return "%s %s '%s'" % (criteria, operator, utils.escape_criteria(value))
 
82
 
 
83
    def __str__(self):
 
84
        return re.sub("(\w+)", self._replace, self.formula)
 
85
 
 
86
 
 
87
 
 
88
AlertFilterList = CriteriaIDMEF(prelude.IDMEF_CLASS_ID_ALERT, "alert").CriteriaList
 
89
HeartbeatFilterList = CriteriaIDMEF(prelude.IDMEF_CLASS_ID_HEARTBEAT, "heartbeat").CriteriaList
 
90
 
 
91
 
 
92
if __name__ == "__main__":
 
93
    print Filter("foo", "",
 
94
                 { "A": ("alert.source(0).node.category", "=", "blah"),
 
95
                   "B": ("alert.messageid", "=", "2") },
 
96
                 "(A or B)")