forked from SELinuxProject/selinux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooleansPage.py
254 lines (214 loc) · 7.79 KB
/
booleansPage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#
# booleansPage.py - GUI for Booleans page in system-config-securitylevel
#
# Dan Walsh <[email protected]>
#
# Copyright 2006, 2007 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import sys
from gi.repository import Gdk, GObject, Gtk
import seobject
import semanagePage
INSTALLPATH = '/usr/share/system-config-selinux'
sys.path.append(INSTALLPATH)
try:
from subprocess import getstatusoutput
except ImportError:
from commands import getstatusoutput
ENFORCING = 0
PERMISSIVE = 1
DISABLED = 2
##
## I18N
##
PROGNAME = "selinux-gui"
try:
import gettext
kwargs = {}
if sys.version_info < (3,):
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs,
fallback=True)
_ = t.gettext
except:
try:
import builtins
builtins.__dict__['_'] = str
except ImportError:
import __builtin__
__builtin__.__dict__['_'] = unicode
class Modifier:
def __init__(self, name, on, save):
self.on = on
self.name = name
self.save = save
def set(self, value):
self.on = value
self.save = True
def isOn(self):
return self.on
class Boolean(Modifier):
def __init__(self, name, val, save=False):
Modifier.__init__(self, name, val, save)
ACTIVE = 0
MODULE = 1
DESC = 2
BOOLEAN = 3
class booleansPage:
def __init__(self, xml, doDebug=None):
self.xml = xml
self.window = self.xml.get_object("mainWindow").get_root_window()
self.local = False
self.types = []
self.selinuxsupport = True
self.typechanged = False
self.doDebug = doDebug
self.busy_cursor = Gdk.Cursor.new(Gdk.CursorType.WATCH)
self.ready_cursor = Gdk.Cursor.new(Gdk.CursorType.LEFT_PTR)
# Bring in widgets from glade file.
self.booleansFilter = xml.get_object("booleansFilter")
self.booleansFilter.connect("focus_out_event", self.filter_changed)
self.booleansFilter.connect("activate", self.filter_changed)
self.booleansFilter.connect("changed", self.filter_changed)
self.booleansView = xml.get_object("booleansView")
self.revertButton = xml.get_object("booleanRevertButton")
self.revertButton.set_sensitive(self.local)
self.revertButton.connect("clicked", self.on_revert_clicked)
listStore = Gtk.ListStore(GObject.TYPE_STRING)
cell = Gtk.CellRendererText()
self.store = Gtk.ListStore(GObject.TYPE_BOOLEAN, GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING)
self.store.set_sort_column_id(1, Gtk.SortType.ASCENDING)
self.booleansView.set_model(self.store)
checkbox = Gtk.CellRendererToggle()
checkbox.connect("toggled", self.boolean_toggled)
col = Gtk.TreeViewColumn('Active', checkbox, active=ACTIVE)
col.set_clickable(True)
col.set_sort_column_id(ACTIVE)
self.booleansView.append_column(col)
col = Gtk.TreeViewColumn("Module", Gtk.CellRendererText(), text=MODULE)
col.set_sort_column_id(MODULE)
col.set_resizable(True)
self.booleansView.append_column(col)
col = Gtk.TreeViewColumn("Description", Gtk.CellRendererText(), text=DESC)
col.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
col.set_fixed_width(400)
col.set_sort_column_id(DESC)
col.set_resizable(True)
self.booleansView.append_column(col)
col = Gtk.TreeViewColumn("Name", Gtk.CellRendererText(), text=BOOLEAN)
col.set_sort_column_id(BOOLEAN)
col.set_resizable(True)
self.booleansView.set_search_equal_func(self.__search)
self.booleansView.append_column(col)
self.filter = ""
self.load(self.filter)
def error(self, message):
dlg = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR,
Gtk.ButtonsType.CLOSE,
message)
dlg.set_position(Gtk.WindowPosition.MOUSE)
dlg.show_all()
dlg.run()
dlg.destroy()
def __search(self, model, col, key, i):
sort_col = self.store.get_sort_column_id()[0]
if sort_col > 0:
val = model.get_value(i, sort_col)
if val.lower().startswith(key.lower()):
return False
return True
def wait(self):
self.window.set_cursor(self.busy_cursor)
semanagePage.idle_func()
def ready(self):
self.window.set_cursor(self.ready_cursor)
semanagePage.idle_func()
def deleteDialog(self):
store, iter = self.booleansView.get_selection().get_selected()
if iter is None:
return
boolean = store.get_value(iter, BOOLEAN)
# change cursor
if boolean is None:
return
try:
self.wait()
(rc, out) = getstatusoutput("semanage boolean -d %s" % boolean)
self.ready()
if rc != 0:
return self.error(out)
self.load(self.filter)
except ValueError as e:
self.error(e.args[0])
def filter_changed(self, *arg):
filter = arg[0].get_text()
if filter != self.filter:
self.load(filter)
self.filter = filter
def use_menus(self):
return False
def get_description(self):
return _("Boolean")
def match(self, key, filter=""):
try:
f = filter.lower()
cat = self.booleans.get_category(key).lower()
val = self.booleans.get_desc(key).lower()
k = key.lower()
return val.find(f) >= 0 or k.find(f) >= 0 or cat.find(f) >= 0
except:
return False
def load(self, filter=None):
self.store.clear()
self.booleans = seobject.booleanRecords()
booleansList = self.booleans.get_all(self.local)
for name in booleansList:
rec = booleansList[name]
if self.match(name, filter):
iter = self.store.append()
self.store.set_value(iter, ACTIVE, rec[2] == 1)
self.store.set_value(iter, MODULE, self.booleans.get_category(name))
self.store.set_value(iter, DESC, self.booleans.get_desc(name))
self.store.set_value(iter, BOOLEAN, name)
def boolean_toggled(self, widget, row):
iter = self.store.get_iter(row)
val = self.store.get_value(iter, ACTIVE)
key = self.store.get_value(iter, BOOLEAN)
self.store.set_value(iter, ACTIVE, not val)
self.wait()
setsebool = "/usr/sbin/setsebool -P %s %d" % (key, not val)
rc, out = getstatusoutput(setsebool)
if rc != 0:
self.error(out)
self.load(self.filter)
self.ready()
def on_revert_clicked(self, button):
self.wait()
setsebool = "semanage boolean --deleteall"
getstatusoutput(setsebool)
self.load(self.filter)
self.ready()
def on_local_clicked(self, button):
self.local = not self.local
self.revertButton.set_sensitive(self.local)
if self.local:
button.set_label(_("all"))
else:
button.set_label(_("Customized"))
self.load(self.filter)
return True