• 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        self.fcontextTypeEntry = xml.get_object("fcontextTypeEntry")
106        self.fcontextMLSEntry = xml.get_object("fcontextMLSEntry")
107
108    def match(self, fcon_dict, k, filter):
109        try:
110            f = filter.lower()
111            for con in k:
112                k = con.lower()
113                if k.find(f) >= 0:
114                    return True
115            for con in fcon_dict[k]:
116                k = con.lower()
117                if k.find(f) >= 0:
118                    return True
119        except:
120            pass
121        return False
122
123    def load(self, filter=""):
124        self.filter = filter
125        self.fcontext = seobject.fcontextRecords()
126        self.store.clear()
127        fcon_dict = self.fcontext.get_all(self.local)
128        for k in sorted(fcon_dict.keys()):
129            if not self.match(fcon_dict, k, filter):
130                continue
131            iter = self.store.append()
132            self.store.set_value(iter, SPEC_COL, k[0])
133            self.store.set_value(iter, FTYPE_COL, k[1])
134            if fcon_dict[k]:
135                rec = "%s:%s" % (fcon_dict[k][2], seobject.translate(fcon_dict[k][3], False))
136            else:
137                rec = "<<None>>"
138            self.store.set_value(iter, TYPE_COL, rec)
139        self.view.get_selection().select_path((0,))
140
141    def filter_changed(self, *arg):
142        filter = arg[0].get_text()
143        if filter != self.filter:
144            self.load(filter)
145
146    def dialogInit(self):
147        store, iter = self.view.get_selection().get_selected()
148        self.fcontextEntry.set_text(store.get_value(iter, SPEC_COL))
149        self.fcontextEntry.set_sensitive(False)
150        scontext = store.get_value(iter, TYPE_COL)
151        scon = context(scontext)
152        self.fcontextTypeEntry.set_text(scon.type)
153        self.fcontextMLSEntry.set_text(scon.mls)
154        type = store.get_value(iter, FTYPE_COL)
155        liststore = self.fcontextFileTypeCombo.get_model()
156        iter = liststore.get_iter_first()
157        while iter != None and liststore.get_value(iter, 0) != type:
158            iter = liststore.iter_next(iter)
159        if iter != None:
160            self.fcontextFileTypeCombo.set_active_iter(iter)
161        self.fcontextFileTypeCombo.set_sensitive(False)
162
163    def dialogClear(self):
164        self.fcontextEntry.set_text("")
165        self.fcontextEntry.set_sensitive(True)
166        self.fcontextFileTypeCombo.set_sensitive(True)
167        self.fcontextFileTypeCombo.set_active(0)
168        self.fcontextTypeEntry.set_text("")
169        self.fcontextMLSEntry.set_text("s0")
170
171    def delete(self):
172        store, iter = self.view.get_selection().get_selected()
173        try:
174            fspec = store.get_value(iter, SPEC_COL)
175            ftype = store.get_value(iter, FTYPE_COL)
176            self.wait()
177            (rc, out) = getstatusoutput("semanage fcontext -d -f '%s' '%s'" % (seobject.file_type_str_to_option[ftype], fspec))
178            self.ready()
179
180            if rc != 0:
181                return self.error(out)
182            store.remove(iter)
183            self.view.get_selection().select_path((0,))
184        except ValueError as e:
185            self.error(e.args[0])
186
187    def add(self):
188        fspec = self.fcontextEntry.get_text().strip()
189        type = self.fcontextTypeEntry.get_text().strip()
190        mls = self.fcontextMLSEntry.get_text().strip()
191        list_model = self.fcontextFileTypeCombo.get_model()
192        it = self.fcontextFileTypeCombo.get_active_iter()
193        ftype = list_model.get_value(it, 0)
194        self.wait()
195        (rc, out) = getstatusoutput("semanage fcontext -a -t %s -r %s -f '%s' '%s'" % (type, mls, seobject.file_type_str_to_option[ftype], fspec))
196        self.ready()
197        if rc != 0:
198            self.error(out)
199            return False
200
201        iter = self.store.append()
202        self.store.set_value(iter, SPEC_COL, fspec)
203        self.store.set_value(iter, FTYPE_COL, ftype)
204        self.store.set_value(iter, TYPE_COL, "%s:%s" % (type, mls))
205
206    def modify(self):
207        fspec = self.fcontextEntry.get_text().strip()
208        type = self.fcontextTypeEntry.get_text().strip()
209        mls = self.fcontextMLSEntry.get_text().strip()
210        list_model = self.fcontextFileTypeCombo.get_model()
211        iter = self.fcontextFileTypeCombo.get_active_iter()
212        ftype = list_model.get_value(iter, 0)
213        self.wait()
214        (rc, out) = getstatusoutput("semanage fcontext -m -t %s -r %s -f '%s' '%s'" % (type, mls, seobject.file_type_str_to_option[ftype], fspec))
215        self.ready()
216        if rc != 0:
217            self.error(out)
218            return False
219
220        store, iter = self.view.get_selection().get_selected()
221        self.store.set_value(iter, SPEC_COL, fspec)
222        self.store.set_value(iter, FTYPE_COL, ftype)
223        self.store.set_value(iter, TYPE_COL, "%s:%s" % (type, mls))
224