3 from sfatables.command import Command
4 from sfatables.globals import sfatables_config, target_dir, match_dir
8 self.options = [('-A','--add')]
9 self.help = 'Add a rule to a chain'
14 def getnextfilename(self,type,chain):
15 dir = sfatables_config + "/"+chain;
18 for (root, dirs, files) in os.walk(dir):
20 if (file.startswith('sfatables-') and file.endswith(type)):
21 number_str = file.split('-')[1]
22 number = int(number_str)
23 if (number>last_rule_number):
24 last_rule_number = number
26 return "sfatables-%d-%s"%(last_rule_number+1,type)
28 def call_gen(self, chain, type, dir, options):
29 filename = os.path.join(dir, options.name+".xml")
30 xmldoc = libxml2.parseFile(filename)
32 p = xmldoc.xpathNewContext()
34 supplied_arguments = options.arguments
35 if (hasattr(options,'element') and options.element):
36 element = options.element
40 for option in supplied_arguments:
41 option_name = option['name']
42 option_value = getattr(options,option_name)
44 if (hasattr(options,option_name) and getattr(options,option_name)):
45 context = p.xpathEval("//rule[@element='%s' or @element='*']/argument[name='%s']"%(element, option_name))
47 raise Exception('Unknown option %s for match %s and element %s'%(option,option['name'], element))
49 # Add the value of option
50 valueNode = libxml2.newNode('value')
51 valueNode.addContent(option_value)
52 context[0].addChild(valueNode)
54 filename = self.getnextfilename(type,chain)
55 file_path = os.path.join(sfatables_config, chain, filename)
56 if not os.path.isdir(os.path.dirname(file_path)):
57 os.makedirs(os.path.dirname(file_path))
58 xmldoc.saveFile(file_path)
64 def call(self, command_options, match_options, target_options):
65 chain = command_options.args[0]
66 ret = self.call_gen(chain, 'match',match_dir, match_options)
68 ret = self.call_gen(chain, 'target',target_dir, target_options)