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