1## loginsPage.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 sys 20try: 21 from subprocess import getstatusoutput 22except ImportError: 23 from commands import getstatusoutput 24 25from gi.repository import GObject, Gtk 26import seobject 27from semanagePage import * 28 29## 30## I18N 31## 32PROGNAME = "policycoreutils" 33try: 34 import gettext 35 kwargs = {} 36 if sys.version_info < (3,): 37 kwargs['unicode'] = True 38 gettext.install(PROGNAME, 39 localedir="/usr/share/locale", 40 codeset='utf-8', 41 **kwargs) 42except: 43 try: 44 import builtins 45 builtins.__dict__['_'] = str 46 except ImportError: 47 import __builtin__ 48 __builtin__.__dict__['_'] = unicode 49 50 51class loginsPage(semanagePage): 52 53 def __init__(self, xml): 54 self.firstTime = False 55 semanagePage.__init__(self, xml, "logins", _("User Mapping")) 56 self.store = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_STRING) 57 self.view.set_model(self.store) 58 self.store.set_sort_column_id(0, Gtk.SortType.ASCENDING) 59 col = Gtk.TreeViewColumn(_("Login\nName"), Gtk.CellRendererText(), text=0) 60 col.set_sort_column_id(0) 61 col.set_resizable(True) 62 self.view.append_column(col) 63 col = Gtk.TreeViewColumn(_("SELinux\nUser"), Gtk.CellRendererText(), text=1) 64 col.set_resizable(True) 65 self.view.append_column(col) 66 col = Gtk.TreeViewColumn(_("MLS/\nMCS Range"), Gtk.CellRendererText(), text=2) 67 col.set_resizable(True) 68 self.view.append_column(col) 69 self.load() 70 self.loginsNameEntry = xml.get_object("loginsNameEntry") 71 self.loginsSelinuxUserCombo = xml.get_object("loginsSelinuxUserCombo") 72 self.loginsMLSEntry = xml.get_object("loginsMLSEntry") 73 74 def load(self, filter=""): 75 self.filter = filter 76 self.login = seobject.loginRecords() 77 dict = self.login.get_all(0) 78 self.store.clear() 79 for k in sorted(dict.keys()): 80 range = seobject.translate(dict[k][1]) 81 if not (self.match(k, filter) or self.match(dict[k][0], filter) or self.match(range, filter)): 82 continue 83 iter = self.store.append() 84 self.store.set_value(iter, 0, k) 85 self.store.set_value(iter, 1, dict[k][0]) 86 self.store.set_value(iter, 2, range) 87 self.view.get_selection().select_path((0,)) 88 89 def __dialogSetup(self): 90 if self.firstTime: 91 return 92 self.firstTime = True 93 liststore = Gtk.ListStore(GObject.TYPE_STRING) 94 self.loginsSelinuxUserCombo.set_model(liststore) 95 cell = Gtk.CellRendererText() 96 self.loginsSelinuxUserCombo.pack_start(cell, True) 97 self.loginsSelinuxUserCombo.add_attribute(cell, 'text', 0) 98 99 selusers = seobject.seluserRecords().get_all(0) 100 for k in sorted(selusers.keys()): 101 if k != "system_u": 102 self.loginsSelinuxUserCombo.append_text(k) 103 104 iter = liststore.get_iter_first() 105 while liststore.get_value(iter, 0) != "user_u": 106 iter = liststore.iter_next(iter) 107 self.loginsSelinuxUserCombo.set_active_iter(iter) 108 109 def dialogInit(self): 110 self.__dialogSetup() 111 store, iter = self.view.get_selection().get_selected() 112 self.loginsNameEntry.set_text(store.get_value(iter, 0)) 113 self.loginsNameEntry.set_sensitive(False) 114 115 self.loginsMLSEntry.set_text(store.get_value(iter, 2)) 116 seuser = store.get_value(iter, 1) 117 liststore = self.loginsSelinuxUserCombo.get_model() 118 iter = liststore.get_iter_first() 119 while iter != None and liststore.get_value(iter, 0) != seuser: 120 iter = liststore.iter_next(iter) 121 if iter != None: 122 self.loginsSelinuxUserCombo.set_active_iter(iter) 123 124 def dialogClear(self): 125 self.__dialogSetup() 126 self.loginsNameEntry.set_text("") 127 self.loginsNameEntry.set_sensitive(True) 128 self.loginsMLSEntry.set_text("s0") 129 130 def delete(self): 131 store, iter = self.view.get_selection().get_selected() 132 try: 133 login = store.get_value(iter, 0) 134 if login == "root" or login == "__default__": 135 raise ValueError(_("Login '%s' is required") % login) 136 137 self.wait() 138 (rc, out) = getstatusoutput("semanage login -d %s" % login) 139 self.ready() 140 if rc != 0: 141 self.error(out) 142 return False 143 store.remove(iter) 144 self.view.get_selection().select_path((0,)) 145 except ValueError as e: 146 self.error(e.args[0]) 147 148 def add(self): 149 target = self.loginsNameEntry.get_text().strip() 150 serange = self.loginsMLSEntry.get_text().strip() 151 if serange == "": 152 serange = "s0" 153 list_model = self.loginsSelinuxUserCombo.get_model() 154 iter = self.loginsSelinuxUserCombo.get_active_iter() 155 seuser = list_model.get_value(iter, 0) 156 self.wait() 157 (rc, out) = getstatusoutput("semanage login -a -s %s -r %s %s" % (seuser, serange, target)) 158 self.ready() 159 if rc != 0: 160 self.error(out) 161 return False 162 163 iter = self.store.append() 164 self.store.set_value(iter, 0, target) 165 self.store.set_value(iter, 1, seuser) 166 self.store.set_value(iter, 2, seobject.translate(serange)) 167 168 def modify(self): 169 target = self.loginsNameEntry.get_text().strip() 170 serange = self.loginsMLSEntry.get_text().strip() 171 if serange == "": 172 serange = "s0" 173 list_model = self.loginsSelinuxUserCombo.get_model() 174 iter = self.loginsSelinuxUserCombo.get_active_iter() 175 seuser = list_model.get_value(iter, 0) 176 self.wait() 177 (rc, out) = getstatusoutput("semanage login -m -s %s -r %s %s" % (seuser, serange, target)) 178 self.ready() 179 if rc != 0: 180 self.error(out) 181 return False 182 183 store, iter = self.view.get_selection().get_selected() 184 self.store.set_value(iter, 0, target) 185 self.store.set_value(iter, 1, seuser) 186 self.store.set_value(iter, 2, seobject.translate(serange)) 187