#!/usr/bin/python # Author: Joshua Brindle # # Copyright (C) 2003 - 2005 Tresys Technology, LLC # 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 2. """ This module generates configuration files and documentation from the SELinux reference policy XML format. """ import sys import getopt import pyplate import os import string from xml.dom.minidom import parse, parseString def read_policy_xml(filename): try: xml_fh = open(filename) except: error("error opening " + filename) try: doc = parseString(xml_fh.read()) except: xml_fh.close() error("Error while parsing xml") xml_fh.close() return doc def gen_tunable_conf(doc, file): for node in doc.getElementsByTagName("tunable"): s = string.split(node.firstChild.data, "\n") for line in s: file.write("# %s\n" % line) tun_name = tun_val = None for (name, value) in node.attributes.items(): if name == "name": tun_name = value elif name == "dftval": tun_val = value if tun_name and tun_val: file.write("%s = %s\n\n" % (tun_name, tun_val)) tun_name = tun_val = None def gen_module_conf(doc, file): file.write("#\n# This file contains a listing of available modules.\n") file.write("# To prevent a module from being used in policy\n") file.write("# creation, uncomment the line with its name.\n#\n") for node in doc.getElementsByTagName("module"): mod_name = mod_layer = None for (name, value) in node.attributes.items(): if name == "name": mod_name = value if name == "layer": mod_layer = value if mod_name and mod_layer: file.write("# Layer: %s\n# Module: %s\n#\n" % (mod_layer,mod_name)) for desc in node.getElementsByTagName("summary"): s = string.split(desc.firstChild.data, "\n") for line in s: file.write("# %s\n" % line) file.write("#\n#%s\n\n" % mod_name) def stupid_cmp(a, b): return cmp(a[0], b[0]) def int_cmp(a, b): return cmp(a["interface_name"], b["interface_name"]) def gen_doc_menu(mod_layer, module_list): menu = [] for layer, value in module_list.iteritems(): cur_menu = (layer, []) menu.append(cur_menu) if layer != mod_layer and mod_layer != None: continue #we are in our layer so fill in the other modules or we want them all for mod, desc in value.iteritems(): cur_menu[1].append((mod, desc)) menu.sort(stupid_cmp) for x in menu: x[1].sort(stupid_cmp) return menu def format_desc(node): desc_buf = '' for desc in node.childNodes: if desc.nodeName == "#text": desc_buf += "

" + desc.data + "

" elif desc.nodeName == "p": desc_buf += "

" + desc.firstChild.data + "

" for chld in desc.childNodes: if chld.nodeName == "ul": print "got a ul!" desc_buf += "