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