blob: 370bbee40786cc70b5e5d8d3e8a6c09017e1886b [file] [log] [blame]
Dan Walsh514af852012-04-13 11:04:45 -04001## 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
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020019from gi.repository import GObject, Gtk
Dan Walsh514af852012-04-13 11:04:45 -040020import seobject
Jason Zaman05d1cea2016-08-05 02:34:04 +080021try:
22 from subprocess import getstatusoutput
23except ImportError:
24 from commands import getstatusoutput
25
Jason Zaman789d0eb2015-07-24 16:07:13 +080026from semanagePage import *
Dan Walsh514af852012-04-13 11:04:45 -040027
28SPEC_COL = 0
29TYPE_COL = 1
30FTYPE_COL = 2
31
Jason Zaman789d0eb2015-07-24 16:07:13 +080032
Dan Walsh514af852012-04-13 11:04:45 -040033class context:
Jason Zaman789d0eb2015-07-24 16:07:13 +080034
Dan Walsh514af852012-04-13 11:04:45 -040035 def __init__(self, scontext):
36 self.scontext = scontext
Jason Zaman789d0eb2015-07-24 16:07:13 +080037 con = scontext.split(":")
Dan Walsh514af852012-04-13 11:04:45 -040038 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##
Jason Zaman789d0eb2015-07-24 16:07:13 +080050PROGNAME = "policycoreutils"
Dan Walsh514af852012-04-13 11:04:45 -040051try:
Jason Zamanaf595442016-08-05 02:34:02 +080052 import gettext
53 kwargs = {}
54 if sys.version_info < (3,):
55 kwargs['unicode'] = True
Dan Walsh514af852012-04-13 11:04:45 -040056 gettext.install(PROGNAME,
57 localedir="/usr/share/locale",
Jason Zamanaf595442016-08-05 02:34:02 +080058 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
Dan Walsh514af852012-04-13 11:04:45 -040067
68
69class fcontextPage(semanagePage):
Jason Zaman789d0eb2015-07-24 16:07:13 +080070
Dan Walsh514af852012-04-13 11:04:45 -040071 def __init__(self, xml):
72 semanagePage.__init__(self, xml, "fcontext", _("File Labeling"))
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020073 self.fcontextFilter = xml.get_object("fcontextFilterEntry")
Dan Walsh514af852012-04-13 11:04:45 -040074 self.fcontextFilter.connect("focus_out_event", self.filter_changed)
75 self.fcontextFilter.connect("activate", self.filter_changed)
76
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020077 self.store = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING)
78 self.view = xml.get_object("fcontextView")
Dan Walsh514af852012-04-13 11:04:45 -040079 self.view.set_model(self.store)
80 self.view.set_search_equal_func(self.search)
81
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020082 col = Gtk.TreeViewColumn(_("File\nSpecification"), Gtk.CellRendererText(), text=SPEC_COL)
83 col.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
Jason Zaman789d0eb2015-07-24 16:07:13 +080084 col.set_fixed_width(250)
Dan Walsh514af852012-04-13 11:04:45 -040085
86 col.set_sort_column_id(SPEC_COL)
87 col.set_resizable(True)
88 self.view.append_column(col)
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020089 col = Gtk.TreeViewColumn(_("Selinux\nFile Type"), Gtk.CellRendererText(), text=TYPE_COL)
Dan Walsh514af852012-04-13 11:04:45 -040090
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020091 col.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
Jason Zaman789d0eb2015-07-24 16:07:13 +080092 col.set_fixed_width(250)
Dan Walsh514af852012-04-13 11:04:45 -040093 col.set_sort_column_id(TYPE_COL)
94 col.set_resizable(True)
95 self.view.append_column(col)
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020096 col = Gtk.TreeViewColumn(_("File\nType"), Gtk.CellRendererText(), text=2)
Dan Walsh514af852012-04-13 11:04:45 -040097 col.set_sort_column_id(FTYPE_COL)
98 col.set_resizable(True)
99 self.view.append_column(col)
100
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200101 self.store.set_sort_column_id(SPEC_COL, Gtk.SortType.ASCENDING)
Dan Walsh514af852012-04-13 11:04:45 -0400102 self.load()
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200103 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")
Dan Walsh514af852012-04-13 11:04:45 -0400107
108 def match(self, fcon_dict, k, filter):
109 try:
Jason Zaman789d0eb2015-07-24 16:07:13 +0800110 f = filter.lower()
Dan Walsh514af852012-04-13 11:04:45 -0400111 for con in k:
Jason Zaman789d0eb2015-07-24 16:07:13 +0800112 k = con.lower()
Dan Walsh514af852012-04-13 11:04:45 -0400113 if k.find(f) >= 0:
114 return True
115 for con in fcon_dict[k]:
Jason Zaman789d0eb2015-07-24 16:07:13 +0800116 k = con.lower()
Dan Walsh514af852012-04-13 11:04:45 -0400117 if k.find(f) >= 0:
118 return True
119 except:
120 pass
121 return False
122
123 def load(self, filter=""):
Jason Zaman789d0eb2015-07-24 16:07:13 +0800124 self.filter = filter
125 self.fcontext = seobject.fcontextRecords()
Dan Walsh514af852012-04-13 11:04:45 -0400126 self.store.clear()
Jason Zaman789d0eb2015-07-24 16:07:13 +0800127 fcon_dict = self.fcontext.get_all(self.local)
Jason Zaman4d340e42016-08-05 02:34:03 +0800128 for k in sorted(fcon_dict.keys()):
Dan Walsh514af852012-04-13 11:04:45 -0400129 if not self.match(fcon_dict, k, filter):
130 continue
Jason Zaman789d0eb2015-07-24 16:07:13 +0800131 iter = self.store.append()
Dan Walsh514af852012-04-13 11:04:45 -0400132 self.store.set_value(iter, SPEC_COL, k[0])
133 self.store.set_value(iter, FTYPE_COL, k[1])
134 if fcon_dict[k]:
Jason Zaman789d0eb2015-07-24 16:07:13 +0800135 rec = "%s:%s" % (fcon_dict[k][2], seobject.translate(fcon_dict[k][3], False))
Dan Walsh514af852012-04-13 11:04:45 -0400136 else:
Jason Zaman789d0eb2015-07-24 16:07:13 +0800137 rec = "<<None>>"
Dan Walsh514af852012-04-13 11:04:45 -0400138 self.store.set_value(iter, TYPE_COL, rec)
Jason Zaman789d0eb2015-07-24 16:07:13 +0800139 self.view.get_selection().select_path((0,))
Dan Walsh514af852012-04-13 11:04:45 -0400140
141 def filter_changed(self, *arg):
Jason Zaman789d0eb2015-07-24 16:07:13 +0800142 filter = arg[0].get_text()
Dan Walsh514af852012-04-13 11:04:45 -0400143 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)
Jason Zaman789d0eb2015-07-24 16:07:13 +0800151 scon = context(scontext)
Dan Walsh514af852012-04-13 11:04:45 -0400152 self.fcontextTypeEntry.set_text(scon.type)
153 self.fcontextMLSEntry.set_text(scon.mls)
Jason Zaman789d0eb2015-07-24 16:07:13 +0800154 type = store.get_value(iter, FTYPE_COL)
155 liststore = self.fcontextFileTypeCombo.get_model()
Dan Walsh514af852012-04-13 11:04:45 -0400156 iter = liststore.get_iter_first()
Jason Zaman789d0eb2015-07-24 16:07:13 +0800157 while iter != None and liststore.get_value(iter, 0) != type:
Dan Walsh514af852012-04-13 11:04:45 -0400158 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)
Vit Mojzis3217d712018-03-01 12:03:06 +0100167 self.fcontextFileTypeCombo.set_active(0)
Dan Walsh514af852012-04-13 11:04:45 -0400168 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:
Jason Zaman789d0eb2015-07-24 16:07:13 +0800174 fspec = store.get_value(iter, SPEC_COL)
175 ftype = store.get_value(iter, FTYPE_COL)
Dan Walsh514af852012-04-13 11:04:45 -0400176 self.wait()
Vit Mojzis530904e2016-10-19 14:36:03 +0200177 (rc, out) = getstatusoutput("semanage fcontext -d -f '%s' '%s'" % (seobject.file_type_str_to_option[ftype], fspec))
Dan Walsh514af852012-04-13 11:04:45 -0400178 self.ready()
179
180 if rc != 0:
181 return self.error(out)
182 store.remove(iter)
Jason Zaman789d0eb2015-07-24 16:07:13 +0800183 self.view.get_selection().select_path((0,))
Jason Zaman4d340e42016-08-05 02:34:03 +0800184 except ValueError as e:
Dan Walsh514af852012-04-13 11:04:45 -0400185 self.error(e.args[0])
186
187 def add(self):
Jason Zaman789d0eb2015-07-24 16:07:13 +0800188 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()
Vit Mojzis530904e2016-10-19 14:36:03 +0200192 it = self.fcontextFileTypeCombo.get_active_iter()
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200193 ftype = list_model.get_value(it, 0)
Dan Walsh514af852012-04-13 11:04:45 -0400194 self.wait()
Vit Mojzis530904e2016-10-19 14:36:03 +0200195 (rc, out) = getstatusoutput("semanage fcontext -a -t %s -r %s -f '%s' '%s'" % (type, mls, seobject.file_type_str_to_option[ftype], fspec))
Dan Walsh514af852012-04-13 11:04:45 -0400196 self.ready()
197 if rc != 0:
198 self.error(out)
199 return False
200
Jason Zaman789d0eb2015-07-24 16:07:13 +0800201 iter = self.store.append()
Dan Walsh514af852012-04-13 11:04:45 -0400202 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):
Jason Zaman789d0eb2015-07-24 16:07:13 +0800207 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()
Dan Walsh514af852012-04-13 11:04:45 -0400211 iter = self.fcontextFileTypeCombo.get_active_iter()
Jason Zaman789d0eb2015-07-24 16:07:13 +0800212 ftype = list_model.get_value(iter, 0)
Dan Walsh514af852012-04-13 11:04:45 -0400213 self.wait()
Vit Mojzis530904e2016-10-19 14:36:03 +0200214 (rc, out) = getstatusoutput("semanage fcontext -m -t %s -r %s -f '%s' '%s'" % (type, mls, seobject.file_type_str_to_option[ftype], fspec))
Dan Walsh514af852012-04-13 11:04:45 -0400215 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))