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