This is hopefully the first version of sfatables that basically works.
[sfa.git] / sfatables / xmlrule.py
1 import libxml2
2 import libxslt
3 from sfatables.globals import *
4
5 class XMLRule:
6     def apply_processor(self, type, doc, output_xpath_filter=None):
7         processor = self.processors[type]
8
9         # XXX TO CLEAN UP
10         filepath = os.path.join(sfatables_config, 'processors', processor)
11         # XXX
12
13         print filepath
14         styledoc = libxml2.parseFile(filepath)
15         style = libxslt.parseStylesheetDoc(styledoc)
16         result = style.applyStylesheet(doc, None)
17         if (output_xpath_filter):
18             p = result.xpathNewContext()
19             xpath_result = p.xpathEval(output_xpath_filter)
20             if (xpath_result == []):
21                 raise Exception("Could not apply processor %s."%processor)
22
23             stylesheet_result = xpath_result
24             p.xpathFreeContext()
25         else:
26             stylesheet_result = result #style.saveResultToString(result)
27
28         style.freeStylesheet()
29         #doc.freeDoc()
30         #result.freeDoc()
31
32         return stylesheet_result
33
34     def wrap_up(self, doc):
35         filepath = os.path.join(sfatables_config, 'processors', self.final_processor)
36
37         if not os.path.exists(filepath):
38             raise Exception('Could not find final rule filter')
39
40         styledoc = libxml2.parseFile(filepath)
41         style = libxslt.parseStylesheetDoc(styledoc)
42         result = style.applyStylesheet(doc, None)
43         stylesheet_result = result#style.saveResultToString(result)
44         style.freeStylesheet()
45         #doc.freeDoc()
46         #result.freeDoc()
47
48         return stylesheet_result
49
50     def match(self, rspec):
51         match_result = self.apply_processor('match',rspec,"//result/@verdict") 
52         return (match_result[0].content=='True')
53
54     def target(self, rspec):
55         target_result = self.apply_processor('target',rspec,None)
56         return target_result
57
58     def add_rule_context_to_rspec(self, doc):
59         p = doc.xpathNewContext()
60         context = p.xpathEval("//rspec")
61         if (not context):
62             raise Exception('Request is not an rspec')
63         else:
64             # Add the request context
65             matchNode = libxml2.newNode('match-context')
66             for match_argument in self.arguments['match']:
67                 matchNode.addChild(match_argument)
68
69             targetNode = libxml2.newNode('target-context')
70             for target_argument in self.arguments['target']:
71                 targetNode.addChild(target_argument)
72
73             context[0].addChild(matchNode)
74             context[0].addChild(targetNode)
75         p.xpathFreeContext()
76
77         return doc
78
79     def apply_interpreted(self, rspec):
80         rspec = self.add_rule_context_to_rspec(rspec)
81         # Interpreted
82         #
83         # output =
84         #    if (match(match_args, rspec)
85         #       then target(target_args, rspec)
86         #       else rspec
87         
88         if (self.match(rspec)):
89             return self.wrap_up(self.target(rspec))
90         else:
91             return self.wrap_up(rspec)
92
93
94     def apply_compiled(rspec):
95         # Not supported yet
96         return None
97
98     def load_xml_extension (self, type, chain, rule_number):
99         filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
100
101         self.xmldoc = libxml2.parseFile(filename)
102         p = self.xmldoc.xpathNewContext()
103
104         context = p.xpathEval('//context/@select')
105         self.context[type] = context[0].content
106
107         processor = p.xpathEval('//processor/@filename')
108
109         context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
110         if (context != []):
111             self.terminal = 1
112         
113         self.processors[type] = processor[0].content
114         self.arguments[type] = p.xpathEval('//rule//argument[value!=""]')
115
116         p.xpathFreeContext()
117
118
119     def __init__(self, chain=None, rule_number=None):
120         self.rule_number = None
121         self.chain = None
122         self.xmldoc = None
123         self.terminal = 0
124         self.final_processor = '__sfatables_rule_wrap_up__.xsl'
125
126         self.arguments = {'match':None,'target':None}
127         self.processors = {'match':None,'target':None}
128         self.context = {'match':None,'target':None}
129
130         if (chain and rule_number):
131             self.load_xml_extension('match', chain, rule_number)
132             self.load_xml_extension('target',chain, rule_number)
133             self.rule_number = rule_number
134             self.chain = chain
135         return
136         
137     def free(self):
138         self.xmldoc.freeDoc()