1## fcontextPage.py - show selinux mappings 2## Copyright (C) 2006 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 gtk 20import gtk.glade 21import os 22import gobject 23import seobject 24try: 25 from subprocess import getstatusoutput 26except ImportError: 27 from commands import getstatusoutput 28 29from semanagePage import * 30 31SPEC_COL = 0 32TYPE_COL = 1 33FTYPE_COL = 2 34 35 36class context: 37 38 def __init__(self, scontext): 39 self.scontext = scontext 40 con = scontext.split(":") 41 self.type = con[0] 42 if len(con) > 1: 43 self.mls = con[1] 44 else: 45 self.mls = "s0" 46 47 def __str__(self): 48 return self.scontext 49 50## 51## I18N 52## 53PROGNAME = "policycoreutils" 54try: 55 import gettext 56 kwargs = {} 57 if sys.version_info < (3,): 58 kwargs['unicode'] = True 59 gettext.install(PROGNAME, 60 localedir="/usr/share/locale", 61 codeset='utf-8', 62 **kwargs) 63except: 64 try: 65 import builtins 66 builtins.__dict__['_'] = str 67 except ImportError: 68 import __builtin__ 69 __builtin__.__dict__['_'] = unicode 70 71 72class fcontextPage(semanagePage): 73 74 def __init__(self, xml): 75 semanagePage.__init__(self, xml, "fcontext", _("File Labeling")) 76 self.fcontextFilter = xml.get_widget("fcontextFilterEntry") 77 self.fcontextFilter.connect("focus_out_event", self.filter_changed) 78 self.fcontextFilter.connect("activate", self.filter_changed) 79 80 self.store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING) 81 self.view = xml.get_widget("fcontextView") 82 self.view.set_model(self.store) 83 self.view.set_search_equal_func(self.search) 84 85 col = gtk.TreeViewColumn(_("File\nSpecification"), gtk.CellRendererText(), text=SPEC_COL) 86 col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) 87 col.set_fixed_width(250) 88 89 col.set_sort_column_id(SPEC_COL) 90 col.set_resizable(True) 91 self.view.append_column(col) 92 col = gtk.TreeViewColumn(_("Selinux\nFile Type"), gtk.CellRendererText(), text=TYPE_COL) 93 94 col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED) 95 col.set_fixed_width(250) 96 col.set_sort_column_id(TYPE_COL) 97 col.set_resizable(True) 98 self.view.append_column(col) 99 col = gtk.TreeViewColumn(_("File\nType"), gtk.CellRendererText(), text=2) 100 col.set_sort_column_id(FTYPE_COL) 101 col.set_resizable(True) 102 self.view.append_column(col) 103 104 self.store.set_sort_column_id(SPEC_COL, gtk.SORT_ASCENDING) 105 self.load() 106 self.fcontextEntry = xml.get_widget("fcontextEntry") 107 self.fcontextFileTypeCombo = xml.get_widget("fcontextFileTypeCombo") 108 self.fcontextTypeEntry = xml.get_widget("fcontextTypeEntry") 109 self.fcontextMLSEntry = xml.get_widget("fcontextMLSEntry") 110 111 def match(self, fcon_dict, k, filter): 112 try: 113 f = filter.lower() 114 for con in k: 115 k = con.lower() 116 if k.find(f) >= 0: 117 return True 118 for con in fcon_dict[k]: 119 k = con.lower() 120 if k.find(f) >= 0: 121 return True 122 except: 123 pass 124 return False 125 126 def load(self, filter=""): 127 self.filter = filter 128 self.fcontext = seobject.fcontextRecords() 129 self.store.clear() 130 fcon_dict = self.fcontext.get_all(self.local) 131 for k in sorted(fcon_dict.keys()): 132 if not self.match(fcon_dict, k, filter): 133 continue 134 iter = self.store.append() 135 self.store.set_value(iter, SPEC_COL, k[0]) 136 self.store.set_value(iter, FTYPE_COL, k[1]) 137 if fcon_dict[k]: 138 rec = "%s:%s" % (fcon_dict[k][2], seobject.translate(fcon_dict[k][3], False)) 139 else: 140 rec = "<<None>>" 141 self.store.set_value(iter, TYPE_COL, rec) 142 self.view.get_selection().select_path((0,)) 143 144 def filter_changed(self, *arg): 145 filter = arg[0].get_text() 146 if filter != self.filter: 147 self.load(filter) 148 149 def dialogInit(self): 150 store, iter = self.view.get_selection().get_selected() 151 self.fcontextEntry.set_text(store.get_value(iter, SPEC_COL)) 152 self.fcontextEntry.set_sensitive(False) 153 scontext = store.get_value(iter, TYPE_COL) 154 scon = context(scontext) 155 self.fcontextTypeEntry.set_text(scon.type) 156 self.fcontextMLSEntry.set_text(scon.mls) 157 type = store.get_value(iter, FTYPE_COL) 158 liststore = self.fcontextFileTypeCombo.get_model() 159 iter = liststore.get_iter_first() 160 while iter != None and liststore.get_value(iter, 0) != type: 161 iter = liststore.iter_next(iter) 162 if iter != None: 163 self.fcontextFileTypeCombo.set_active_iter(iter) 164 self.fcontextFileTypeCombo.set_sensitive(False) 165 166 def dialogClear(self): 167 self.fcontextEntry.set_text("") 168 self.fcontextEntry.set_sensitive(True) 169 self.fcontextFileTypeCombo.set_sensitive(True) 170 self.fcontextTypeEntry.set_text("") 171 self.fcontextMLSEntry.set_text("s0") 172 173 def delete(self): 174 store, iter = self.view.get_selection().get_selected() 175 try: 176 fspec = store.get_value(iter, SPEC_COL) 177 ftype = store.get_value(iter, FTYPE_COL) 178 self.wait() 179 (rc, out) = getstatusoutput("semanage fcontext -d -f '%s' '%s'" % (seobject.file_type_str_to_option[ftype], fspec)) 180 self.ready() 181 182 if rc != 0: 183 return self.error(out) 184 store.remove(iter) 185 self.view.get_selection().select_path((0,)) 186 except ValueError as e: 187 self.error(e.args[0]) 188 189 def add(self): 190 fspec = self.fcontextEntry.get_text().strip() 191 type = self.fcontextTypeEntry.get_text().strip() 192 mls = self.fcontextMLSEntry.get_text().strip() 193 list_model = self.fcontextFileTypeCombo.get_model() 194 it = self.fcontextFileTypeCombo.get_active_iter() 195 ftype = list_model.get_value(it,0) 196 self.wait() 197 (rc, out) = getstatusoutput("semanage fcontext -a -t %s -r %s -f '%s' '%s'" % (type, mls, seobject.file_type_str_to_option[ftype], fspec)) 198 self.ready() 199 if rc != 0: 200 self.error(out) 201 return False 202 203 iter = self.store.append() 204 self.store.set_value(iter, SPEC_COL, fspec) 205 self.store.set_value(iter, FTYPE_COL, ftype) 206 self.store.set_value(iter, TYPE_COL, "%s:%s" % (type, mls)) 207 208 def modify(self): 209 fspec = self.fcontextEntry.get_text().strip() 210 type = self.fcontextTypeEntry.get_text().strip() 211 mls = self.fcontextMLSEntry.get_text().strip() 212 list_model = self.fcontextFileTypeCombo.get_model() 213 iter = self.fcontextFileTypeCombo.get_active_iter() 214 ftype = list_model.get_value(iter, 0) 215 self.wait() 216 (rc, out) = getstatusoutput("semanage fcontext -m -t %s -r %s -f '%s' '%s'" % (type, mls, seobject.file_type_str_to_option[ftype], fspec)) 217 self.ready() 218 if rc != 0: 219 self.error(out) 220 return False 221 222 store, iter = self.view.get_selection().get_selected() 223 self.store.set_value(iter, SPEC_COL, fspec) 224 self.store.set_value(iter, FTYPE_COL, ftype) 225 self.store.set_value(iter, TYPE_COL, "%s:%s" % (type, mls)) 226