Nicolas Iooss | 72dc5c6 | 2019-02-17 21:57:41 +0100 | [diff] [blame] | 1 | #!/usr/bin/python3 -Es |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 2 | # |
| 3 | # system-config-selinux.py - GUI for SELinux Config tool in system-config-selinux |
| 4 | # |
| 5 | # Dan Walsh <dwalsh@redhat.com> |
| 6 | # |
| 7 | # Copyright 2006-2009 Red Hat, Inc. |
| 8 | # |
| 9 | # This program is free software; you can redistribute it and/or modify |
| 10 | # it under the terms of the GNU General Public License as published by |
| 11 | # the Free Software Foundation; either version 2 of the License, or |
| 12 | # (at your option) any later version. |
| 13 | # |
| 14 | # This program is distributed in the hope that it will be useful, |
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | # GNU General Public License for more details. |
| 18 | # |
| 19 | # You should have received a copy of the GNU General Public License |
| 20 | # along with this program; if not, write to the Free Software |
| 21 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | # |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 23 | import os |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 24 | import signal |
Dan Walsh | dd6c619 | 2012-11-05 14:59:46 -0500 | [diff] [blame] | 25 | import sys |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 26 | import gi |
| 27 | gi.require_version('Gtk', '3.0') |
Dan Walsh | dd6c619 | 2012-11-05 14:59:46 -0500 | [diff] [blame] | 28 | try: |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 29 | from gi.repository import Gtk |
Jason Zaman | 4d340e4 | 2016-08-05 02:34:03 +0800 | [diff] [blame] | 30 | except RuntimeError as e: |
| 31 | print("system-config-selinux:", e) |
| 32 | print("This is a graphical application and requires DISPLAY to be set.") |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 33 | sys.exit(1) |
Dan Walsh | dd6c619 | 2012-11-05 14:59:46 -0500 | [diff] [blame] | 34 | |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 35 | from gi.repository import GObject |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 36 | import statusPage |
| 37 | import booleansPage |
| 38 | import loginsPage |
| 39 | import usersPage |
| 40 | import portsPage |
| 41 | import modulesPage |
| 42 | import domainsPage |
| 43 | import fcontextPage |
| 44 | import selinux |
| 45 | ## |
| 46 | ## I18N |
| 47 | ## |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 48 | PROGNAME = "policycoreutils" |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 49 | try: |
Jason Zaman | af59544 | 2016-08-05 02:34:02 +0800 | [diff] [blame] | 50 | import gettext |
| 51 | kwargs = {} |
| 52 | if sys.version_info < (3,): |
| 53 | kwargs['unicode'] = True |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 54 | gettext.install(PROGNAME, |
| 55 | localedir="/usr/share/locale", |
Jason Zaman | af59544 | 2016-08-05 02:34:02 +0800 | [diff] [blame] | 56 | codeset='utf-8', |
| 57 | **kwargs) |
| 58 | except: |
| 59 | try: |
| 60 | import builtins |
| 61 | builtins.__dict__['_'] = str |
| 62 | except ImportError: |
| 63 | import __builtin__ |
| 64 | __builtin__.__dict__['_'] = unicode |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 65 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 66 | version = "1.0" |
| 67 | |
| 68 | sys.path.append('/usr/share/system-config-selinux') |
| 69 | |
| 70 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 71 | ## |
| 72 | ## Pull in the Glade file |
| 73 | ## |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 74 | xml = Gtk.Builder() |
| 75 | xml.set_translation_domain(PROGNAME) |
| 76 | if os.access("system-config-selinux.ui", os.F_OK): |
| 77 | xml.add_from_file("system-config-selinux.ui") |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 78 | else: |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 79 | xml.add_from_file("/usr/share/system-config-selinux/system-config-selinux.ui") |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 80 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 81 | |
| 82 | class childWindow: |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 83 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 84 | def __init__(self): |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 85 | self.tabs = [] |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 86 | self.xml = xml |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 87 | xml.connect_signals({ |
| 88 | "on_quit_activate": self.destroy, |
| 89 | "on_delete_clicked": self.delete, |
| 90 | "on_add_clicked": self.add, |
| 91 | "on_properties_clicked": self.properties, |
| 92 | "on_local_clicked": self.on_local_clicked, |
| 93 | "on_policy_activate": self.policy, |
| 94 | "on_logging_activate": self.logging, |
| 95 | "on_about_activate": self.on_about_activate, |
| 96 | }) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 97 | self.add_page(statusPage.statusPage(xml)) |
| 98 | if selinux.is_selinux_enabled() > 0: |
| 99 | try: |
| 100 | self.add_page(booleansPage.booleansPage(xml)) |
| 101 | self.add_page(fcontextPage.fcontextPage(xml)) |
| 102 | self.add_page(loginsPage.loginsPage(xml)) |
| 103 | self.add_page(usersPage.usersPage(xml)) |
| 104 | self.add_page(portsPage.portsPage(xml)) |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 105 | self.add_page(modulesPage.modulesPage(xml)) # modules |
| 106 | self.add_page(domainsPage.domainsPage(xml)) # domains |
Jason Zaman | 4d340e4 | 2016-08-05 02:34:03 +0800 | [diff] [blame] | 107 | except ValueError as e: |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 108 | self.error(e.message) |
| 109 | |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 110 | self.add_menu = xml.get_object("add_menu_item") |
| 111 | self.properties_menu = xml.get_object("properties_menu_item") |
| 112 | self.delete_menu = xml.get_object("delete_menu_item") |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 113 | |
| 114 | def error(self, message): |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 115 | dlg = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR, |
| 116 | Gtk.ButtonsType.CLOSE, |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 117 | message) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 118 | dlg.set_position(Gtk.WindowPosition.MOUSE) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 119 | dlg.show_all() |
| 120 | dlg.run() |
| 121 | dlg.destroy() |
| 122 | |
| 123 | def add_page(self, page): |
| 124 | self.tabs.append(page) |
| 125 | |
| 126 | def policy(self, args): |
| 127 | os.spawnl(os.P_NOWAIT, "/usr/share/system-config-selinux/semanagegui.py") |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 128 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 129 | def logging(self, args): |
| 130 | os.spawnl(os.P_NOWAIT, "/usr/bin/seaudit") |
| 131 | |
| 132 | def delete(self, args): |
| 133 | self.tabs[self.notebook.get_current_page()].deleteDialog() |
| 134 | |
| 135 | def add(self, args): |
| 136 | self.tabs[self.notebook.get_current_page()].addDialog() |
| 137 | |
| 138 | def properties(self, args): |
| 139 | self.tabs[self.notebook.get_current_page()].propertiesDialog() |
| 140 | |
| 141 | def on_local_clicked(self, button): |
| 142 | self.tabs[self.notebook.get_current_page()].on_local_clicked(button) |
| 143 | |
| 144 | def on_about_activate(self, args): |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 145 | dlg = xml.get_object("aboutWindow") |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 146 | dlg.run() |
| 147 | dlg.hide() |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 148 | |
| 149 | def destroy(self, args): |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 150 | Gtk.main_quit() |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 151 | |
| 152 | def use_menus(self, use_menus): |
| 153 | self.add_menu.set_sensitive(use_menus) |
| 154 | self.properties_menu.set_sensitive(use_menus) |
| 155 | self.delete_menu.set_sensitive(use_menus) |
| 156 | |
| 157 | def itemSelected(self, selection): |
| 158 | store, rows = selection.get_selected_rows() |
| 159 | if store != None and len(rows) > 0: |
| 160 | self.notebook.set_current_page(rows[0][0]) |
| 161 | self.use_menus(self.tabs[rows[0][0]].use_menus()) |
| 162 | else: |
| 163 | self.notebook.set_current_page(0) |
| 164 | self.use_menus(self.tabs[0].use_menus()) |
| 165 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 166 | def setupScreen(self): |
| 167 | # Bring in widgets from glade file. |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 168 | self.mainWindow = self.xml.get_object("mainWindow") |
| 169 | self.notebook = self.xml.get_object("notebook") |
| 170 | self.view = self.xml.get_object("selectView") |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 171 | self.view.get_selection().connect("changed", self.itemSelected) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 172 | self.store = Gtk.ListStore(GObject.TYPE_STRING) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 173 | self.view.set_model(self.store) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 174 | col = Gtk.TreeViewColumn("", Gtk.CellRendererText(), text=0) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 175 | col.set_resizable(True) |
| 176 | self.view.append_column(col) |
| 177 | |
| 178 | for page in self.tabs: |
| 179 | iter = self.store.append() |
| 180 | self.store.set_value(iter, 0, page.get_description()) |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 181 | self.view.get_selection().select_path((0,)) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 182 | |
| 183 | def stand_alone(self): |
Nicolas Iooss | b550c0e | 2019-08-05 22:11:20 +0200 | [diff] [blame] | 184 | desktopName = _("Configure SELinux") |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 185 | |
| 186 | self.setupScreen() |
| 187 | |
| 188 | self.mainWindow.connect("destroy", self.destroy) |
| 189 | |
| 190 | self.mainWindow.show_all() |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 191 | Gtk.main() |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 192 | |
| 193 | if __name__ == "__main__": |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 194 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 195 | |
| 196 | app = childWindow() |
| 197 | app.stand_alone() |