Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 1 | ## domainsPage.py - show selinux domains |
| 2 | ## Copyright (C) 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 |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 19 | import os |
Jason Zaman | 05d1cea | 2016-08-05 02:34:04 +0800 | [diff] [blame] | 20 | try: |
| 21 | from subprocess import getstatusoutput |
| 22 | except ImportError: |
| 23 | from commands import getstatusoutput |
| 24 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 25 | import sys |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 26 | from gi.repository import GObject, Gtk |
Jason Zaman | b43991f | 2016-08-05 02:34:01 +0800 | [diff] [blame] | 27 | import sepolicy |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 28 | from semanagePage import * |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 29 | |
| 30 | ## |
| 31 | ## I18N |
| 32 | ## |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 33 | PROGNAME = "policycoreutils" |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 34 | try: |
Jason Zaman | af59544 | 2016-08-05 02:34:02 +0800 | [diff] [blame] | 35 | import gettext |
| 36 | kwargs = {} |
| 37 | if sys.version_info < (3,): |
| 38 | kwargs['unicode'] = True |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 39 | gettext.install(PROGNAME, |
| 40 | localedir="/usr/share/locale", |
Jason Zaman | af59544 | 2016-08-05 02:34:02 +0800 | [diff] [blame] | 41 | codeset='utf-8', |
| 42 | **kwargs) |
| 43 | except: |
| 44 | try: |
| 45 | import builtins |
| 46 | builtins.__dict__['_'] = str |
| 47 | except ImportError: |
| 48 | import __builtin__ |
| 49 | __builtin__.__dict__['_'] = unicode |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 50 | |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 51 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 52 | class domainsPage(semanagePage): |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 53 | |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 54 | def __init__(self, xml): |
| 55 | semanagePage.__init__(self, xml, "domains", _("Process Domain")) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 56 | self.domain_filter = xml.get_object("domainsFilterEntry") |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 57 | self.domain_filter.connect("focus_out_event", self.filter_changed) |
| 58 | self.domain_filter.connect("activate", self.filter_changed) |
| 59 | |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 60 | self.store = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_STRING) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 61 | self.view.set_model(self.store) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 62 | self.store.set_sort_column_id(0, Gtk.SortType.ASCENDING) |
| 63 | col = Gtk.TreeViewColumn(_("Domain Name"), Gtk.CellRendererText(), text=0) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 64 | col.set_sort_column_id(0) |
| 65 | col.set_resizable(True) |
| 66 | self.view.append_column(col) |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 67 | self.store.set_sort_column_id(0, Gtk.SortType.ASCENDING) |
| 68 | col = Gtk.TreeViewColumn(_("Mode"), Gtk.CellRendererText(), text=1) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 69 | col.set_sort_column_id(1) |
| 70 | col.set_resizable(True) |
| 71 | self.view.append_column(col) |
| 72 | self.view.get_selection().connect("changed", self.itemSelected) |
| 73 | |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 74 | self.permissive_button = xml.get_object("permissiveButton") |
| 75 | self.enforcing_button = xml.get_object("enforcingButton") |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 76 | |
Jason Zaman | b43991f | 2016-08-05 02:34:01 +0800 | [diff] [blame] | 77 | self.domains = sepolicy.get_all_entrypoint_domains() |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 78 | self.load() |
| 79 | |
| 80 | def get_modules(self): |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 81 | modules = [] |
| 82 | fd = os.popen("semodule -l") |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 83 | mods = fd.readlines() |
| 84 | fd.close() |
| 85 | for l in mods: |
| 86 | modules.append(l.split()[0]) |
| 87 | return modules |
| 88 | |
| 89 | def load(self, filter=""): |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 90 | self.filter = filter |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 91 | self.store.clear() |
| 92 | try: |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 93 | modules = self.get_modules() |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 94 | for domain in self.domains: |
| 95 | if not self.match(domain, filter): |
| 96 | continue |
| 97 | iter = self.store.append() |
| 98 | self.store.set_value(iter, 0, domain) |
| 99 | t = "permissive_%s_t" % domain |
| 100 | if t in modules: |
| 101 | self.store.set_value(iter, 1, _("Permissive")) |
| 102 | else: |
| 103 | self.store.set_value(iter, 1, "") |
| 104 | except: |
| 105 | pass |
Jason Zaman | 789d0eb | 2015-07-24 16:07:13 +0800 | [diff] [blame] | 106 | self.view.get_selection().select_path((0,)) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 107 | |
| 108 | def itemSelected(self, selection): |
| 109 | store, iter = selection.get_selected() |
Nicolas Iooss | 0f3beeb | 2017-09-20 08:56:54 +0200 | [diff] [blame] | 110 | if iter is None: |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 111 | return |
| 112 | p = store.get_value(iter, 1) == _("Permissive") |
| 113 | self.permissive_button.set_sensitive(not p) |
| 114 | self.enforcing_button.set_sensitive(p) |
| 115 | |
| 116 | def deleteDialog(self): |
| 117 | # Do nothing |
| 118 | return self.delete() |
| 119 | |
| 120 | def delete(self): |
| 121 | selection = self.view.get_selection() |
| 122 | store, iter = selection.get_selected() |
| 123 | domain = store.get_value(iter, 0) |
| 124 | try: |
| 125 | self.wait() |
Jason Zaman | 05d1cea | 2016-08-05 02:34:04 +0800 | [diff] [blame] | 126 | status, output = getstatusoutput("semanage permissive -d %s_t" % domain) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 127 | self.ready() |
| 128 | if status != 0: |
| 129 | self.error(output) |
| 130 | else: |
| 131 | domain = store.set_value(iter, 1, "") |
| 132 | self.itemSelected(selection) |
| 133 | |
Jason Zaman | 4d340e4 | 2016-08-05 02:34:03 +0800 | [diff] [blame] | 134 | except ValueError as e: |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 135 | self.error(e.args[0]) |
| 136 | |
| 137 | def propertiesDialog(self): |
| 138 | # Do nothing |
| 139 | return |
| 140 | |
| 141 | def addDialog(self): |
| 142 | # Do nothing |
| 143 | return self.add() |
| 144 | |
| 145 | def add(self): |
| 146 | selection = self.view.get_selection() |
| 147 | store, iter = selection.get_selected() |
| 148 | domain = store.get_value(iter, 0) |
| 149 | try: |
| 150 | self.wait() |
Jason Zaman | 05d1cea | 2016-08-05 02:34:04 +0800 | [diff] [blame] | 151 | status, output = getstatusoutput("semanage permissive -a %s_t" % domain) |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 152 | self.ready() |
| 153 | if status != 0: |
| 154 | self.error(output) |
| 155 | else: |
| 156 | domain = store.set_value(iter, 1, _("Permissive")) |
| 157 | self.itemSelected(selection) |
| 158 | |
Jason Zaman | 4d340e4 | 2016-08-05 02:34:03 +0800 | [diff] [blame] | 159 | except ValueError as e: |
Dan Walsh | 514af85 | 2012-04-13 11:04:45 -0400 | [diff] [blame] | 160 | self.error(e.args[0]) |