1## 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
19import string
20import gtk
21import gtk.glade
22import os
23try:
24    from subprocess import getstatusoutput
25except ImportError:
26    from commands import getstatusoutput
27
28import gobject
29import sys
30import seobject
31import selinux
32from semanagePage import *
33from subprocess import Popen, PIPE
34
35##
36## I18N
37##
38PROGNAME = "policycoreutils"
39try:
40    import gettext
41    kwargs = {}
42    if sys.version_info < (3,):
43        kwargs['unicode'] = True
44    gettext.install(PROGNAME,
45                    localedir="/usr/share/locale",
46                    codeset='utf-8',
47                    **kwargs)
48except:
49    try:
50        import builtins
51        builtins.__dict__['_'] = str
52    except ImportError:
53        import __builtin__
54        __builtin__.__dict__['_'] = unicode
55
56
57class modulesPage(semanagePage):
58
59    def __init__(self, xml):
60        semanagePage.__init__(self, xml, "modules", _("Policy Module"))
61        self.module_filter = xml.get_widget("modulesFilterEntry")
62        self.module_filter.connect("focus_out_event", self.filter_changed)
63        self.module_filter.connect("activate", self.filter_changed)
64        self.audit_enabled = False
65
66        self.store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
67        self.view.set_model(self.store)
68        self.store.set_sort_column_id(0, gtk.SORT_ASCENDING)
69        col = gtk.TreeViewColumn(_("Module Name"), gtk.CellRendererText(), text=0)
70        col.set_sort_column_id(0)
71        col.set_resizable(True)
72        self.view.append_column(col)
73        self.store.set_sort_column_id(0, gtk.SORT_ASCENDING)
74        col = gtk.TreeViewColumn(_("Version"), gtk.CellRendererText(), text=1)
75        self.enable_audit_button = xml.get_widget("enableAuditButton")
76        self.enable_audit_button.connect("clicked", self.enable_audit)
77        self.new_button = xml.get_widget("newModuleButton")
78        self.new_button.connect("clicked", self.new_module)
79        col.set_sort_column_id(1)
80        col.set_resizable(True)
81        self.view.append_column(col)
82        self.store.set_sort_func(1, self.sort_int, "")
83        status, self.policy_type = selinux.selinux_getpolicytype()
84
85        self.load()
86
87    def sort_int(self, treemodel, iter1, iter2, user_data):
88        try:
89            p1 = int(treemodel.get_value(iter1, 1))
90            p2 = int(treemodel.get_value(iter1, 1))
91            if p1 > p2:
92                return 1
93            if p1 == p2:
94                return 0
95            return -1
96        except:
97            return 0
98
99    def load(self, filter=""):
100        self.filter = filter
101        self.store.clear()
102        try:
103            fd = Popen("semodule -l", shell=True, stdout=PIPE).stdout
104            l = fd.readlines()
105            fd.close()
106            for i in l:
107                module, ver, newline = i.split('\t')
108                if not (self.match(module, filter) or self.match(ver, filter)):
109                    continue
110                iter = self.store.append()
111                self.store.set_value(iter, 0, module.strip())
112                self.store.set_value(iter, 1, ver.strip())
113        except:
114            pass
115        self.view.get_selection().select_path((0,))
116
117    def new_module(self, args):
118        try:
119            Popen(["/usr/share/system-config-selinux/polgengui.py"])
120        except ValueError as e:
121            self.error(e.args[0])
122
123    def delete(self):
124        store, iter = self.view.get_selection().get_selected()
125        module = store.get_value(iter, 0)
126        try:
127            self.wait()
128            status, output = getstatusoutput("semodule -r %s" % module)
129            self.ready()
130            if status != 0:
131                self.error(output)
132            else:
133                store.remove(iter)
134                self.view.get_selection().select_path((0,))
135
136        except ValueError as e:
137            self.error(e.args[0])
138
139    def enable_audit(self, button):
140        self.audit_enabled = not self.audit_enabled
141        try:
142            self.wait()
143            if self.audit_enabled:
144                status, output = getstatusoutput("semodule -DB")
145                button.set_label(_("Disable Audit"))
146            else:
147                status, output = getstatusoutput("semodule -B")
148                button.set_label(_("Enable Audit"))
149            self.ready()
150
151            if status != 0:
152                self.error(output)
153
154        except ValueError as e:
155            self.error(e.args[0])
156
157    def disable_audit(self, button):
158        try:
159            self.wait()
160            status, output = getstatusoutput("semodule -B")
161            self.ready()
162            if status != 0:
163                self.error(output)
164
165        except ValueError as e:
166            self.error(e.args[0])
167
168    def propertiesDialog(self):
169        # Do nothing
170        return
171
172    def addDialog(self):
173        dialog = gtk.FileChooserDialog(_("Load Policy Module"),
174                                       None,
175                                       gtk.FILE_CHOOSER_ACTION_OPEN,
176                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
177                                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))
178        dialog.set_default_response(gtk.RESPONSE_OK)
179
180        filter = gtk.FileFilter()
181        filter.set_name("Policy Files")
182        filter.add_pattern("*.pp")
183        dialog.add_filter(filter)
184
185        response = dialog.run()
186        if response == gtk.RESPONSE_OK:
187            self.add(dialog.get_filename())
188        dialog.destroy()
189
190    def add(self, file):
191        try:
192            self.wait()
193            status, output = getstatusoutput("semodule -i %s" % file)
194            self.ready()
195            if status != 0:
196                self.error(output)
197            else:
198                self.load()
199
200        except ValueError as e:
201            self.error(e.args[0])
202