From: Andy Bavier Date: Tue, 27 Oct 2009 21:18:12 +0000 (+0000) Subject: Very basic Insert command X-Git-Tag: sfa-0.9-6~94 X-Git-Url: http://git.onelab.eu/?p=sfa.git;a=commitdiff_plain;h=54723f230312171bc348159040e3a7b1be0af519 Very basic Insert command --- diff --git a/sfatables/commands/Insert.py b/sfatables/commands/Insert.py new file mode 100644 index 00000000..d4010920 --- /dev/null +++ b/sfatables/commands/Insert.py @@ -0,0 +1,65 @@ +import os, time +import libxml2 +from sfatables.command import Command +from sfatables.globals import * + +class Insert(Command): + def __init__(self): + self.options = [('-I','--insert')] + self.help = 'Insert a rule into a chain' + self.matches = True + self.targets = True + return + + def call_gen(self, chain, type, dir, options, file_path): + filename = os.path.join(dir, options.name+".xml") + xmldoc = libxml2.parseFile(filename) + + p = xmldoc.xpathNewContext() + + supplied_arguments = options.arguments + if (hasattr(options,'element') and options.element): + element = options.element + else: + element='*' + + for option in supplied_arguments: + option_name = option['name'] + option_value = getattr(options,option_name) + + if (hasattr(options,option_name) and getattr(options,option_name)): + context = p.xpathEval("//rule[@element='%s' or @element='*']/argument[name='%s']"%(element, option_name)) + if (not context): + raise Exception('Unknown option %s for match %s and element %s'%(option,option['name'], element)) + else: + # Add the value of option + valueNode = libxml2.newNode('value') + valueNode.addContent(option_value) + context[0].addChild(valueNode) + + if not os.path.isdir(os.path.dirname(file_path)): + os.makedirs(os.path.dirname(file_path)) + xmldoc.saveFile(file_path) + p.xpathFreeContext() + xmldoc.freeDoc() + + return True + + def call(self, command_options, match_options, target_options): + if (len(command_options.args)<2): + print "Please specify the chain and the rule number to insert, e.g. sfatables -I INCOMING 1 -- ...." + return + + chain = command_options.args[0] + + rule_number = command_options.args[1] + chain_dir = sfatables_config + "/" + chain + + match_path = chain_dir + "/" + "sfatables-%s-match"%rule_number + target_path = chain_dir + "/" + "sfatables-%s-target"%rule_number + + ret = self.call_gen(chain, 'match',match_dir, match_options, match_path) + if (ret): + ret = self.call_gen(chain, 'target',target_dir, target_options, target_path) + + return ret diff --git a/sfatables/commands/__init__.py b/sfatables/commands/__init__.py index 7f95863b..273696c1 100644 --- a/sfatables/commands/__init__.py +++ b/sfatables/commands/__init__.py @@ -1,6 +1,7 @@ all = """ Add Delete +Insert List SetDefault """.split()