#!/usr/bin/env python # -*- coding: utf-8 -*- """ Copyright(C) 2007 INL Written by Damien Boucard <damien.boucard AT inl.fr> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>. --- l7xml.py is a module for importing and exporting a layer7 filtering content into an XML format. """ from l7models import * from nupyf.nupyf_etree import etree def load(file): """ Generate model objects from a given XML file. @param file: XML file-like object to import. @type file: file @rtype: L7RuleList """ doc = etree.parse(file) xml_policy = doc.getroot() l7rulelist = None xml_l7rules = xml_policy.find('l7rules') l7rulelist = L7RuleList(**dict(xml_l7rules.items())) for xml_l7rule in xml_l7rules.findall('l7rule'): l7rule = L7Rule(**dict(xml_l7rule.items())) for xml_elt in xml_l7rule.findall('elt'): l7ruleelt = L7RuleElt(**dict(xml_elt.items())) l7rule.append(l7ruleelt) l7rulelist.append(l7rule) l7rulelist.recompute_connmarks() return l7rulelist def dump(l7rulelist, file): """ Generate an XML file from a given nulayer7 model. @param l7rulelist: data to export into XML file. @type l7rulelist: L7RuleList @param file: XML file-like object to export to. @type file: file """ xml_policy = etree.Element("policy", version="1.0") xml_l7rules = etree.SubElement(xml_policy, "l7rules", mask="0x%08X" %l7rulelist.mask) for l7rule in l7rulelist: l7rule_dict = {} for (k, v) in l7rule.__dict__.iteritems(): if v is None or callable(v): continue elif k in ('name', 'action', 'defaultaction', 'prefix', 'defaultprefix', 'comment'): l7rule_dict[k] = v elif k == 'connmark': l7rule_dict[k] = "0x%08X" %v elif k == 'ID': l7rule_dict[k] = "%d" %v xml_l7rule = etree.SubElement(xml_l7rules, "l7rule", l7rule_dict) for l7ruleelt in l7rule: l7ruleelt_dict = {} for (k, v) in l7ruleelt.__dict__.iteritems(): if v is None or callable(v): continue elif k in ('l7proto', 'action', 'prefix', 'name', 'type'): l7ruleelt_dict[k] = v elif k == 'ID': l7ruleelt_dict[k] = "%d" %v xml_elt = etree.SubElement(xml_l7rule, "elt", l7ruleelt_dict) indent(xml_policy) doc = etree.ElementTree(xml_policy) #doc.docinfo.xml_version = "1.0" doc.write(file) def indent(elem, level=0): i = "\n" + level*" " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " for elem in elem: indent(elem, level+1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i if __name__ == "__main__": import sys from StringIO import StringIO s = StringIO() dump(load(open(sys.argv[1], 'r')), s) print s.getvalue()