blob: 0584acf9b3a45779660a6b0c9a46e99ceb3c24a2 [file] [log] [blame]
Dan Walsh514af852012-04-13 11:04:45 -04001## modulesPage.py - show selinux mappings
2## Copyright (C) 2006-2009 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 +020019import sys
20from subprocess import Popen, PIPE
Jason Zaman05d1cea2016-08-05 02:34:04 +080021try:
22 from subprocess import getstatusoutput
23except ImportError:
24 from commands import getstatusoutput
25
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020026from gi.repository import GObject, Gtk
Dan Walsh514af852012-04-13 11:04:45 -040027import selinux
Jason Zaman789d0eb2015-07-24 16:07:13 +080028from semanagePage import *
Dan Walsh514af852012-04-13 11:04:45 -040029
30##
31## I18N
32##
Jason Zaman789d0eb2015-07-24 16:07:13 +080033PROGNAME = "policycoreutils"
Dan Walsh514af852012-04-13 11:04:45 -040034try:
Jason Zamanaf595442016-08-05 02:34:02 +080035 import gettext
36 kwargs = {}
37 if sys.version_info < (3,):
38 kwargs['unicode'] = True
Dan Walsh514af852012-04-13 11:04:45 -040039 gettext.install(PROGNAME,
40 localedir="/usr/share/locale",
Jason Zamanaf595442016-08-05 02:34:02 +080041 codeset='utf-8',
42 **kwargs)
43except:
44 try:
45 import builtins
46 builtins.__dict__['_'] = str
47 except ImportError:
48 import __builtin__
49 __builtin__.__dict__['_'] = unicode
Dan Walsh514af852012-04-13 11:04:45 -040050
Jason Zaman789d0eb2015-07-24 16:07:13 +080051
Dan Walsh514af852012-04-13 11:04:45 -040052class modulesPage(semanagePage):
Jason Zaman789d0eb2015-07-24 16:07:13 +080053
Dan Walsh514af852012-04-13 11:04:45 -040054 def __init__(self, xml):
55 semanagePage.__init__(self, xml, "modules", _("Policy Module"))
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020056 self.module_filter = xml.get_object("modulesFilterEntry")
Dan Walsh514af852012-04-13 11:04:45 -040057 self.module_filter.connect("focus_out_event", self.filter_changed)
58 self.module_filter.connect("activate", self.filter_changed)
59 self.audit_enabled = False
60
Nicolas Iooss3b5e8fb2017-10-01 18:15:15 +020061 self.store = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_STRING,
62 GObject.TYPE_STRING)
Dan Walsh514af852012-04-13 11:04:45 -040063 self.view.set_model(self.store)
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020064 self.store.set_sort_column_id(0, Gtk.SortType.ASCENDING)
65 col = Gtk.TreeViewColumn(_("Module Name"), Gtk.CellRendererText(), text=0)
Dan Walsh514af852012-04-13 11:04:45 -040066 col.set_sort_column_id(0)
67 col.set_resizable(True)
68 self.view.append_column(col)
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020069 self.store.set_sort_column_id(0, Gtk.SortType.ASCENDING)
Nicolas Iooss3b5e8fb2017-10-01 18:15:15 +020070 col = Gtk.TreeViewColumn(_("Priority"), Gtk.CellRendererText(), text=1)
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020071 self.enable_audit_button = xml.get_object("enableAuditButton")
Dan Walsh514af852012-04-13 11:04:45 -040072 self.enable_audit_button.connect("clicked", self.enable_audit)
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +020073 self.new_button = xml.get_object("newModuleButton")
Dan Walsh514af852012-04-13 11:04:45 -040074 self.new_button.connect("clicked", self.new_module)
75 col.set_sort_column_id(1)
76 col.set_resizable(True)
77 self.view.append_column(col)
Nicolas Iooss3b5e8fb2017-10-01 18:15:15 +020078 self.store.set_sort_column_id(2, Gtk.SortType.ASCENDING)
79 col = Gtk.TreeViewColumn(_("Kind"), Gtk.CellRendererText(), text=2)
80 col.set_sort_column_id(2)
81 col.set_resizable(True)
82 self.view.append_column(col)
Jason Zaman789d0eb2015-07-24 16:07:13 +080083 self.store.set_sort_func(1, self.sort_int, "")
Dan Walsh514af852012-04-13 11:04:45 -040084 status, self.policy_type = selinux.selinux_getpolicytype()
85
86 self.load()
87
88 def sort_int(self, treemodel, iter1, iter2, user_data):
89 try:
Jason Zaman789d0eb2015-07-24 16:07:13 +080090 p1 = int(treemodel.get_value(iter1, 1))
91 p2 = int(treemodel.get_value(iter1, 1))
Dan Walsh514af852012-04-13 11:04:45 -040092 if p1 > p2:
93 return 1
94 if p1 == p2:
95 return 0
96 return -1
97 except:
98 return 0
99
100 def load(self, filter=""):
Jason Zaman789d0eb2015-07-24 16:07:13 +0800101 self.filter = filter
Dan Walsh514af852012-04-13 11:04:45 -0400102 self.store.clear()
103 try:
Nicolas Iooss3b5e8fb2017-10-01 18:15:15 +0200104 fd = Popen("semodule -lfull", shell=True, stdout=PIPE).stdout
Dan Walsh514af852012-04-13 11:04:45 -0400105 l = fd.readlines()
106 fd.close()
107 for i in l:
Nicolas Iooss3b5e8fb2017-10-01 18:15:15 +0200108 priority, module, kind = i.decode('utf-8').split()
109 if not (self.match(module, filter) or self.match(priority, filter)):
Dan Walsh514af852012-04-13 11:04:45 -0400110 continue
111 iter = self.store.append()
112 self.store.set_value(iter, 0, module.strip())
Nicolas Iooss3b5e8fb2017-10-01 18:15:15 +0200113 self.store.set_value(iter, 1, priority.strip())
114 self.store.set_value(iter, 2, kind.strip())
Dan Walsh514af852012-04-13 11:04:45 -0400115 except:
116 pass
Jason Zaman789d0eb2015-07-24 16:07:13 +0800117 self.view.get_selection().select_path((0,))
Dan Walsh514af852012-04-13 11:04:45 -0400118
119 def new_module(self, args):
120 try:
Petr Lautrbachc7785092019-03-05 17:38:55 +0100121 Popen(["selinux-polgengui"])
Jason Zaman4d340e42016-08-05 02:34:03 +0800122 except ValueError as e:
Dan Walsh514af852012-04-13 11:04:45 -0400123 self.error(e.args[0])
124
125 def delete(self):
126 store, iter = self.view.get_selection().get_selected()
127 module = store.get_value(iter, 0)
Petr Lautrbach5dfa95c2019-09-24 21:12:21 +0200128 priority = store.get_value(iter, 1)
Dan Walsh514af852012-04-13 11:04:45 -0400129 try:
130 self.wait()
Petr Lautrbach5dfa95c2019-09-24 21:12:21 +0200131 status, output = getstatusoutput("semodule -X %s -r %s" % (priority, module))
Dan Walsh514af852012-04-13 11:04:45 -0400132 self.ready()
133 if status != 0:
134 self.error(output)
135 else:
136 store.remove(iter)
Jason Zaman789d0eb2015-07-24 16:07:13 +0800137 self.view.get_selection().select_path((0,))
Dan Walsh514af852012-04-13 11:04:45 -0400138
Jason Zaman4d340e42016-08-05 02:34:03 +0800139 except ValueError as e:
Dan Walsh514af852012-04-13 11:04:45 -0400140 self.error(e.args[0])
141
142 def enable_audit(self, button):
143 self.audit_enabled = not self.audit_enabled
144 try:
145 self.wait()
146 if self.audit_enabled:
Jason Zaman05d1cea2016-08-05 02:34:04 +0800147 status, output = getstatusoutput("semodule -DB")
Dan Walsh514af852012-04-13 11:04:45 -0400148 button.set_label(_("Disable Audit"))
149 else:
Jason Zaman05d1cea2016-08-05 02:34:04 +0800150 status, output = getstatusoutput("semodule -B")
Dan Walsh514af852012-04-13 11:04:45 -0400151 button.set_label(_("Enable Audit"))
152 self.ready()
153
154 if status != 0:
155 self.error(output)
156
Jason Zaman4d340e42016-08-05 02:34:03 +0800157 except ValueError as e:
Dan Walsh514af852012-04-13 11:04:45 -0400158 self.error(e.args[0])
159
160 def disable_audit(self, button):
161 try:
162 self.wait()
Jason Zaman05d1cea2016-08-05 02:34:04 +0800163 status, output = getstatusoutput("semodule -B")
Dan Walsh514af852012-04-13 11:04:45 -0400164 self.ready()
165 if status != 0:
166 self.error(output)
167
Jason Zaman4d340e42016-08-05 02:34:03 +0800168 except ValueError as e:
Dan Walsh514af852012-04-13 11:04:45 -0400169 self.error(e.args[0])
170
171 def propertiesDialog(self):
172 # Do nothing
173 return
174
175 def addDialog(self):
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200176 dialog = Gtk.FileChooserDialog(_("Load Policy Module"),
Dan Walsh514af852012-04-13 11:04:45 -0400177 None,
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200178 Gtk.FileChooserAction.OPEN,
179 (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
180 Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
181 dialog.set_default_response(Gtk.ResponseType.OK)
Dan Walsh514af852012-04-13 11:04:45 -0400182
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200183 filter = Gtk.FileFilter()
Dan Walsh514af852012-04-13 11:04:45 -0400184 filter.set_name("Policy Files")
185 filter.add_pattern("*.pp")
186 dialog.add_filter(filter)
187
188 response = dialog.run()
Nicolas Iooss0f3beeb2017-09-20 08:56:54 +0200189 if response == Gtk.ResponseType.OK:
Dan Walsh514af852012-04-13 11:04:45 -0400190 self.add(dialog.get_filename())
191 dialog.destroy()
192
193 def add(self, file):
194 try:
195 self.wait()
Jason Zaman05d1cea2016-08-05 02:34:04 +0800196 status, output = getstatusoutput("semodule -i %s" % file)
Dan Walsh514af852012-04-13 11:04:45 -0400197 self.ready()
198 if status != 0:
199 self.error(output)
200 else:
201 self.load()
202
Jason Zaman4d340e42016-08-05 02:34:03 +0800203 except ValueError as e:
Dan Walsh514af852012-04-13 11:04:45 -0400204 self.error(e.args[0])