3 from sfatables.globals import *
11 arguments = {'match':None,'target':None}
12 processors = {'match':None,'target':None}
13 context = {'match':None,'target':None}
15 def apply_processor(self, type, rspec, output_xpath_filter=None):
16 processor = self.processors[type]
19 filepath = 'processors/' + processor
22 styledoc = libxml2.parseFile(filepath)
23 style = libxslt.parseStylesheetDoc(styledoc)
24 doc = libxml2.parseDoc(rspec)
25 result = style.applyStylesheet(doc, None)
26 if (output_xpath_filter):
27 p = result.xpathNewContext()
28 xpath_result = p.xpathEval(output_xpath_filter)
29 if (xpath_result == []):
30 raise Exception("Could not apply processor %s."%processor)
32 stylesheet_result = xpath_result[0].content
35 stylesheet_result = style.saveResultToString(result)
36 style.freeStylesheet()
41 return stylesheet_result
43 def match(self, rspec):
44 match_result = self.apply_processor('match',rspec,"//result/@verdict")
45 return (match_result=='True')
47 def target(self, rspec):
48 target_result = self.apply_processor('target',rspec,None)
51 def apply_interpreted(self, rspec):
55 # if (match(match_args, rspec)
56 # then target(target_args, rspec)
59 if (self.match(rspec)):
60 return self.target(rspec)
65 def apply_compiled(rspec):
69 def load_xml_extension (self, type, chain, rule_number):
70 filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
72 self.xmldoc = libxml2.parseFile(filename)
73 p = self.xmldoc.xpathNewContext()
75 context = p.xpathEval('//context/@select')
76 self.context[type] = context[0].content
78 processor = p.xpathEval('//processor/@filename')
80 context = p.xpathEval('//attributes/attribute[@terminal="yes"]')
84 self.processors[type] = processor[0].content
85 self.arguments[type] = p.xpathEval('//rule')
90 def wrap_rspec (self, type, rspec):
91 argument = self.arguments[type]
92 p = rspec.xmldoc.xpathNewContext()
93 root_node = p.xpathEval('/RSpec')
94 if (not root_node or not root_node):
95 raise Exception('An evil aggregate manager sent me a malformed RSpec. Please see the stack trace to identify it.')
97 root_node.addChild(arguments[type])
100 def __init__(self, chain, rule_number):
102 self.load_xml_extension('match', chain, rule_number)
103 self.load_xml_extension('target',chain, rule_number)
104 self.rule_number = rule_number
110 self.xmldoc.freeDoc()