1## modulesPage.py - show selinux mappings 2## Copyright (C) 2006-2009 Red Hat, Inc. 3 4## This program is free software; you can redistribute it and/or modify 5## it under the terms of the GNU General Public License as published by 6## the Free Software Foundation; either version 2 of the License, or 7## (at your option) any later version. 8 9## This program is distributed in the hope that it will be useful, 10## but WITHOUT ANY WARRANTY; without even the implied warranty of 11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12## GNU General Public License for more details. 13 14## You should have received a copy of the GNU General Public License 15## along with this program; if not, write to the Free Software 16## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 18## Author: Dan Walsh 19import string 20import gtk 21import gtk.glade 22import os 23import commands 24import gobject 25import sys 26import seobject 27import selinux 28from semanagePage import * 29from subprocess import Popen, PIPE 30 31## 32## I18N 33## 34PROGNAME = "policycoreutils" 35import gettext 36gettext.bindtextdomain(PROGNAME, "/usr/share/locale") 37gettext.textdomain(PROGNAME) 38try: 39 gettext.install(PROGNAME, 40 localedir="/usr/share/locale", 41 unicode=False, 42 codeset='utf-8') 43except IOError: 44 import __builtin__ 45 __builtin__.__dict__['_'] = unicode 46 47 48class modulesPage(semanagePage): 49 50 def __init__(self, xml): 51 semanagePage.__init__(self, xml, "modules", _("Policy Module")) 52 self.module_filter = xml.get_widget("modulesFilterEntry") 53 self.module_filter.connect("focus_out_event", self.filter_changed) 54 self.module_filter.connect("activate", self.filter_changed) 55 self.audit_enabled = False 56 57 self.store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING) 58 self.view.set_model(self.store) 59 self.store.set_sort_column_id(0, gtk.SORT_ASCENDING) 60 col = gtk.TreeViewColumn(_("Module Name"), gtk.CellRendererText(), text=0) 61 col.set_sort_column_id(0) 62 col.set_resizable(True) 63 self.view.append_column(col) 64 self.store.set_sort_column_id(0, gtk.SORT_ASCENDING) 65 col = gtk.TreeViewColumn(_("Version"), gtk.CellRendererText(), text=1) 66 self.enable_audit_button = xml.get_widget("enableAuditButton") 67 self.enable_audit_button.connect("clicked", self.enable_audit) 68 self.new_button = xml.get_widget("newModuleButton") 69 self.new_button.connect("clicked", self.new_module) 70 col.set_sort_column_id(1) 71 col.set_resizable(True) 72 self.view.append_column(col) 73 self.store.set_sort_func(1, self.sort_int, "") 74 status, self.policy_type = selinux.selinux_getpolicytype() 75 76 self.load() 77 78 def sort_int(self, treemodel, iter1, iter2, user_data): 79 try: 80 p1 = int(treemodel.get_value(iter1, 1)) 81 p2 = int(treemodel.get_value(iter1, 1)) 82 if p1 > p2: 83 return 1 84 if p1 == p2: 85 return 0 86 return -1 87 except: 88 return 0 89 90 def load(self, filter=""): 91 self.filter = filter 92 self.store.clear() 93 try: 94 fd = Popen("semodule -l", shell=True, stdout=PIPE).stdout 95 l = fd.readlines() 96 fd.close() 97 for i in l: 98 module, ver, newline = i.split('\t') 99 if not (self.match(module, filter) or self.match(ver, filter)): 100 continue 101 iter = self.store.append() 102 self.store.set_value(iter, 0, module.strip()) 103 self.store.set_value(iter, 1, ver.strip()) 104 except: 105 pass 106 self.view.get_selection().select_path((0,)) 107 108 def new_module(self, args): 109 try: 110 Popen(["/usr/share/system-config-selinux/polgengui.py"]) 111 except ValueError, e: 112 self.error(e.args[0]) 113 114 def delete(self): 115 store, iter = self.view.get_selection().get_selected() 116 module = store.get_value(iter, 0) 117 try: 118 self.wait() 119 status, output = commands.getstatusoutput("semodule -r %s" % module) 120 self.ready() 121 if status != 0: 122 self.error(output) 123 else: 124 store.remove(iter) 125 self.view.get_selection().select_path((0,)) 126 127 except ValueError, e: 128 self.error(e.args[0]) 129 130 def enable_audit(self, button): 131 self.audit_enabled = not self.audit_enabled 132 try: 133 self.wait() 134 if self.audit_enabled: 135 status, output = commands.getstatusoutput("semodule -DB") 136 button.set_label(_("Disable Audit")) 137 else: 138 status, output = commands.getstatusoutput("semodule -B") 139 button.set_label(_("Enable Audit")) 140 self.ready() 141 142 if status != 0: 143 self.error(output) 144 145 except ValueError, e: 146 self.error(e.args[0]) 147 148 def disable_audit(self, button): 149 try: 150 self.wait() 151 status, output = commands.getstatusoutput("semodule -B") 152 self.ready() 153 if status != 0: 154 self.error(output) 155 156 except ValueError, e: 157 self.error(e.args[0]) 158 159 def propertiesDialog(self): 160 # Do nothing 161 return 162 163 def addDialog(self): 164 dialog = gtk.FileChooserDialog(_("Load Policy Module"), 165 None, 166 gtk.FILE_CHOOSER_ACTION_OPEN, 167 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, 168 gtk.STOCK_OPEN, gtk.RESPONSE_OK)) 169 dialog.set_default_response(gtk.RESPONSE_OK) 170 171 filter = gtk.FileFilter() 172 filter.set_name("Policy Files") 173 filter.add_pattern("*.pp") 174 dialog.add_filter(filter) 175 176 response = dialog.run() 177 if response == gtk.RESPONSE_OK: 178 self.add(dialog.get_filename()) 179 dialog.destroy() 180 181 def add(self, file): 182 try: 183 self.wait() 184 status, output = commands.getstatusoutput("semodule -i %s" % file) 185 self.ready() 186 if status != 0: 187 self.error(output) 188 else: 189 self.load() 190 191 except ValueError, e: 192 self.error(e.args[0]) 193