1## fcontextPage.py - show selinux mappings
2## Copyright (C) 2006 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 gtk
20import gtk.glade
21import os
22import gobject
23import seobject
24import commands
25from semanagePage import *;
26
27SPEC_COL = 0
28TYPE_COL = 1
29FTYPE_COL = 2
30
31class context:
32    def __init__(self, scontext):
33        self.scontext = scontext
34        con=scontext.split(":")
35        self.type = con[0]
36        if len(con) > 1:
37            self.mls = con[1]
38        else:
39            self.mls = "s0"
40
41    def __str__(self):
42        return self.scontext
43
44##
45## I18N
46##
47PROGNAME="policycoreutils"
48
49import gettext
50gettext.bindtextdomain(PROGNAME, "/usr/share/locale")
51gettext.textdomain(PROGNAME)
52try:
53    gettext.install(PROGNAME,
54                    localedir="/usr/share/locale",
55                    unicode=False,
56                    codeset = 'utf-8')
57except IOError:
58    import __builtin__
59    __builtin__.__dict__['_'] = unicode
60
61
62class fcontextPage(semanagePage):
63    def __init__(self, xml):
64        semanagePage.__init__(self, xml, "fcontext", _("File Labeling"))
65        self.fcontextFilter = xml.get_widget("fcontextFilterEntry")
66        self.fcontextFilter.connect("focus_out_event", self.filter_changed)
67        self.fcontextFilter.connect("activate", self.filter_changed)
68
69        self.store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
70        self.view = xml.get_widget("fcontextView")
71        self.view.set_model(self.store)
72        self.view.set_search_equal_func(self.search)
73
74        col = gtk.TreeViewColumn(_("File\nSpecification"), gtk.CellRendererText(), text=SPEC_COL)
75	col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
76	col.set_fixed_width(250)
77
78        col.set_sort_column_id(SPEC_COL)
79        col.set_resizable(True)
80        self.view.append_column(col)
81        col = gtk.TreeViewColumn(_("Selinux\nFile Type"), gtk.CellRendererText(), text=TYPE_COL)
82
83	col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
84	col.set_fixed_width(250)
85        col.set_sort_column_id(TYPE_COL)
86        col.set_resizable(True)
87        self.view.append_column(col)
88        col = gtk.TreeViewColumn(_("File\nType"), gtk.CellRendererText(), text=2)
89        col.set_sort_column_id(FTYPE_COL)
90        col.set_resizable(True)
91        self.view.append_column(col)
92
93        self.store.set_sort_column_id(SPEC_COL, gtk.SORT_ASCENDING)
94        self.load()
95        self.fcontextEntry = xml.get_widget("fcontextEntry")
96        self.fcontextFileTypeCombo = xml.get_widget("fcontextFileTypeCombo")
97        liststore=self.fcontextFileTypeCombo.get_model()
98        for k in seobject.file_types:
99            if len(k) > 0 and  k[0] != '-':
100                iter=liststore.append()
101                liststore.set_value(iter, 0, k)
102        iter = liststore.get_iter_first()
103        self.fcontextFileTypeCombo.set_active_iter(iter)
104        self.fcontextTypeEntry = xml.get_widget("fcontextTypeEntry")
105        self.fcontextMLSEntry = xml.get_widget("fcontextMLSEntry")
106
107    def match(self, fcon_dict, k, filter):
108        try:
109            f=filter.lower()
110            for con in k:
111                k=con.lower()
112                if k.find(f) >= 0:
113                    return True
114            for con in fcon_dict[k]:
115                k=con.lower()
116                if k.find(f) >= 0:
117                    return True
118        except:
119            pass
120        return False
121
122    def load(self, filter=""):
123        self.filter=filter
124        self.fcontext=seobject.fcontextRecords()
125        self.store.clear()
126        fcon_dict=self.fcontext.get_all(self.local)
127        keys = fcon_dict.keys()
128        keys.sort()
129        for k in keys:
130            if not self.match(fcon_dict, k, filter):
131                continue
132            iter=self.store.append()
133            self.store.set_value(iter, SPEC_COL, k[0])
134            self.store.set_value(iter, FTYPE_COL, k[1])
135            if fcon_dict[k]:
136                rec="%s:%s" % (fcon_dict[k][2], seobject.translate(fcon_dict[k][3],False))
137            else:
138                rec="<<None>>"
139            self.store.set_value(iter, TYPE_COL, rec)
140        self.view.get_selection().select_path ((0,))
141
142    def filter_changed(self, *arg):
143        filter =  arg[0].get_text()
144        if filter != self.filter:
145            self.load(filter)
146
147    def dialogInit(self):
148        store, iter = self.view.get_selection().get_selected()
149        self.fcontextEntry.set_text(store.get_value(iter, SPEC_COL))
150        self.fcontextEntry.set_sensitive(False)
151        scontext = store.get_value(iter, TYPE_COL)
152        scon=context(scontext)
153        self.fcontextTypeEntry.set_text(scon.type)
154        self.fcontextMLSEntry.set_text(scon.mls)
155        type=store.get_value(iter, FTYPE_COL)
156        liststore=self.fcontextFileTypeCombo.get_model()
157        iter = liststore.get_iter_first()
158        while iter != None and liststore.get_value(iter,0) != type:
159            iter = liststore.iter_next(iter)
160        if iter != None:
161            self.fcontextFileTypeCombo.set_active_iter(iter)
162        self.fcontextFileTypeCombo.set_sensitive(False)
163
164    def dialogClear(self):
165        self.fcontextEntry.set_text("")
166        self.fcontextEntry.set_sensitive(True)
167        self.fcontextFileTypeCombo.set_sensitive(True)
168        self.fcontextTypeEntry.set_text("")
169        self.fcontextMLSEntry.set_text("s0")
170
171    def delete(self):
172        store, iter = self.view.get_selection().get_selected()
173        try:
174            fspec=store.get_value(iter, SPEC_COL)
175            ftype=store.get_value(iter, FTYPE_COL)
176            self.wait()
177            (rc, out) = commands.getstatusoutput("semanage fcontext -d -f '%s' '%s'" % (ftype, fspec))
178            self.ready()
179
180            if rc != 0:
181                return self.error(out)
182            store.remove(iter)
183            self.view.get_selection().select_path ((0,))
184        except ValueError, e:
185            self.error(e.args[0])
186
187    def add(self):
188        ftype=["", "--", "-d", "-c", "-b", "-s", "-l", "-p" ]
189        fspec=self.fcontextEntry.get_text().strip()
190        type=self.fcontextTypeEntry.get_text().strip()
191        mls=self.fcontextMLSEntry.get_text().strip()
192        list_model=self.fcontextFileTypeCombo.get_model()
193        active = self.fcontextFileTypeCombo.get_active()
194        self.wait()
195        (rc, out) = commands.getstatusoutput("semanage fcontext -a -t %s -r %s -f '%s' '%s'" % (type, mls, ftype[active], fspec))
196        self.ready()
197        if rc != 0:
198            self.error(out)
199            return False
200
201        iter=self.store.append()
202        self.store.set_value(iter, SPEC_COL, fspec)
203        self.store.set_value(iter, FTYPE_COL, ftype)
204        self.store.set_value(iter, TYPE_COL, "%s:%s" % (type, mls))
205
206    def modify(self):
207        fspec=self.fcontextEntry.get_text().strip()
208        type=self.fcontextTypeEntry.get_text().strip()
209        mls=self.fcontextMLSEntry.get_text().strip()
210        list_model=self.fcontextFileTypeCombo.get_model()
211        iter = self.fcontextFileTypeCombo.get_active_iter()
212        ftype=list_model.get_value(iter,0)
213        self.wait()
214        (rc, out) = commands.getstatusoutput("semanage fcontext -m -t %s -r %s -f '%s' '%s'" % (type, mls, ftype, fspec))
215        self.ready()
216        if rc != 0:
217            self.error(out)
218            return False
219
220        store, iter = self.view.get_selection().get_selected()
221        self.store.set_value(iter, SPEC_COL, fspec)
222        self.store.set_value(iter, FTYPE_COL, ftype)
223        self.store.set_value(iter, TYPE_COL, "%s:%s" % (type, mls))
224