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