5271d3a
# HG changeset patch
5271d3a
# User "Daniel P. Berrange <berrange@redhat.com>"
5271d3a
# Date 1241720553 -3600
5271d3a
# Node ID 5b61bd10a66b91d40ad5652a8f39b14273175292
5271d3a
# Parent  6082392f2279e21a66482c58e230c2fc695eb66e
5271d3a
Extend VNC auth handling to cope with fetching a username too & record username in gconf
5271d3a
5271d3a
diff -r 6082392f2279 -r 5b61bd10a66b src/virtManager/config.py
5271d3a
--- a/src/virtManager/config.py	Wed May 06 16:47:10 2009 +0100
5271d3a
+++ b/src/virtManager/config.py	Thu May 07 19:22:33 2009 +0100
5271d3a
@@ -23,6 +23,7 @@
5271d3a
 
5271d3a
 import gtk.gdk
5271d3a
 import libvirt
5271d3a
+import logging
5271d3a
 
5271d3a
 from virtManager.keyring import vmmKeyring
5271d3a
 from virtManager.secret import vmmSecret
5271d3a
@@ -299,6 +300,7 @@
5271d3a
 
5271d3a
     def has_keyring(self):
5271d3a
         if self.keyring == None:
5271d3a
+            logging.warning("Initializing keyring")
5271d3a
             self.keyring = vmmKeyring()
5271d3a
         return self.keyring.is_available()
5271d3a
 
5271d3a
@@ -314,26 +316,30 @@
5271d3a
 
5271d3a
     def get_console_password(self, vm):
5271d3a
         _id = self.conf.get_int(self.conf_dir + "/console/passwords/" + vm.get_uuid())
5271d3a
+        username = self.conf.get_string(self.conf_dir + "/console/usernames/" + vm.get_uuid())
5271d3a
+
5271d3a
+        if username is None:
5271d3a
+            username = ""
5271d3a
 
5271d3a
         if _id != None:
5271d3a
             if not(self.has_keyring()):
5271d3a
-                return ""
5271d3a
+                return ("", "")
5271d3a
 
5271d3a
             secret = self.keyring.get_secret(_id)
5271d3a
             if secret != None and secret.get_name() == self.get_secret_name(vm):
5271d3a
                 if not(secret.has_attribute("hvuri")):
5271d3a
-                    return ""
5271d3a
+                    return ("", "")
5271d3a
                 if secret.get_attribute("hvuri") != vm.get_connection().get_uri():
5271d3a
-                    return ""
5271d3a
+                    return ("", "")
5271d3a
                 if not(secret.has_attribute("uuid")):
5271d3a
-                    return ""
5271d3a
+                    return ("", "")
5271d3a
                 if secret.get_attribute("uuid") != vm.get_uuid():
5271d3a
-                    return ""
5271d3a
+                    return ("", "")
5271d3a
 
5271d3a
-                return secret.get_secret()
5271d3a
-        return ""
5271d3a
+                return (secret.get_secret(), username)
5271d3a
+        return ("", username)
5271d3a
 
5271d3a
-    def set_console_password(self, vm, password):
5271d3a
+    def set_console_password(self, vm, password, username=""):
5271d3a
         if not(self.has_keyring()):
5271d3a
             return
5271d3a
 
5271d3a
@@ -346,6 +352,7 @@
5271d3a
         _id = self.keyring.add_secret(secret)
5271d3a
         if _id != None:
5271d3a
             self.conf.set_int(self.conf_dir + "/console/passwords/" + vm.get_uuid(), _id)
5271d3a
+            self.conf.set_string(self.conf_dir + "/console/usernames/" + vm.get_uuid(), username)
5271d3a
 
5271d3a
     def get_url_list_length(self):
5271d3a
         length = self.conf.get_int(self.conf_dir + "/urls/url-list-length")
5271d3a
diff -r 6082392f2279 -r 5b61bd10a66b src/virtManager/details.py
5271d3a
--- a/src/virtManager/details.py	Wed May 06 16:47:10 2009 +0100
5271d3a
+++ b/src/virtManager/details.py	Thu May 07 19:22:33 2009 +0100
5271d3a
@@ -500,7 +500,7 @@
5271d3a
         self.update_scaling()
5271d3a
 
5271d3a
     def auth_login(self, ignore):
5271d3a
-        self.set_password()
5271d3a
+        self.set_credentials()
5271d3a
         self.activate_viewer_page()
5271d3a
 
5271d3a
     def toggle_toolbar(self, src):
5271d3a
@@ -1302,23 +1302,44 @@
5271d3a
                      traceback.format_exc (stacktrace))
5271d3a
             logging.error(details)
5271d3a
 
5271d3a
-    def set_password(self, src=None):
5271d3a
-        txt = self.window.get_widget("console-auth-password")
5271d3a
-        self.vncViewer.set_credential(gtkvnc.CREDENTIAL_PASSWORD,
5271d3a
-                                      txt.get_text())
5271d3a
+    def set_credentials(self, src=None):
5271d3a
+        passwd = self.window.get_widget("console-auth-password")
5271d3a
+        if passwd.flags() & gtk.VISIBLE:
5271d3a
+            self.vncViewer.set_credential(gtkvnc.CREDENTIAL_PASSWORD,
5271d3a
+                                          passwd.get_text())
5271d3a
+        username = self.window.get_widget("console-auth-username")
5271d3a
+        if username.flags() & gtk.VISIBLE:
5271d3a
+            self.vncViewer.set_credential(gtkvnc.CREDENTIAL_USERNAME,
5271d3a
+                                          username.get_text())
5271d3a
+
5271d3a
+        if self.window.get_widget("console-auth-remember").get_active():
5271d3a
+            self.config.set_console_password(self.vm, passwd.get_text(), username.get_text())
5271d3a
 
5271d3a
     def _vnc_auth_credential(self, src, credList):
5271d3a
         for i in range(len(credList)):
5271d3a
-            logging.debug("Got credential request %s", str(credList[i]))
5271d3a
-            if credList[i] == gtkvnc.CREDENTIAL_PASSWORD:
5271d3a
-                self.activate_auth_page()
5271d3a
-            elif credList[i] == gtkvnc.CREDENTIAL_CLIENTNAME:
5271d3a
-                self.vncViewer.set_credential(credList[i], "libvirt-vnc")
5271d3a
-            else:
5271d3a
-                # Force it to stop re-trying
5271d3a
+            if credList[i] not in (gtkvnc.CREDENTIAL_PASSWORD, gtkvnc.CREDENTIAL_USERNAME, gtkvnc.CREDENTIAL_CLIENTNAME):
5271d3a
+                self.err.show_err(summary=_("Unable to provide requested credentials to the VNC server"),
5271d3a
+                                  details=_("The credential type %s is not supported") % (str(credList[i])),
5271d3a
+                                  title=_("Unable to authenticate"),
5271d3a
+                                  async=True)
5271d3a
                 self.vncViewerRetriesScheduled = 10
5271d3a
                 self.vncViewer.close()
5271d3a
                 self.activate_unavailable_page(_("Unsupported console authentication type"))
5271d3a
+                return
5271d3a
+
5271d3a
+        withUsername = False
5271d3a
+        withPassword = False
5271d3a
+        for i in range(len(credList)):
5271d3a
+            logging.debug("Got credential request %s", str(credList[i]))
5271d3a
+            if credList[i] == gtkvnc.CREDENTIAL_PASSWORD:
5271d3a
+                withPassword = True
5271d3a
+            elif credList[i] == gtkvnc.CREDENTIAL_USERNAME:
5271d3a
+                withUsername = True
5271d3a
+            elif credList[i] == gtkvnc.CREDENTIAL_CLIENTNAME:
5271d3a
+                self.vncViewer.set_credential(credList[i], "libvirt-vnc")
5271d3a
+
5271d3a
+        if withUsername or withPassword:
5271d3a
+            self.activate_auth_page(withPassword, withUsername)
5271d3a
 
5271d3a
     def activate_unavailable_page(self, msg):
5271d3a
         self.window.get_widget("console-pages").set_current_page(PAGE_UNAVAILABLE)
5271d3a
@@ -1329,20 +1350,41 @@
5271d3a
         self.window.get_widget("console-pages").set_current_page(PAGE_SCREENSHOT)
5271d3a
         self.window.get_widget("details-menu-vm-screenshot").set_sensitive(True)
5271d3a
 
5271d3a
-    def activate_auth_page(self):
5271d3a
-        pw = self.config.get_console_password(self.vm)
5271d3a
+    def activate_auth_page(self, withPassword=True, withUsername=False):
5271d3a
+        (pw, username) = self.config.get_console_password(self.vm)
5271d3a
         self.window.get_widget("details-menu-vm-screenshot").set_sensitive(False)
5271d3a
+
5271d3a
+        if withPassword:
5271d3a
+            self.window.get_widget("console-auth-password").show()
5271d3a
+            self.window.get_widget("label-auth-password").show()
5271d3a
+        else:
5271d3a
+            self.window.get_widget("console-auth-password").hide()
5271d3a
+            self.window.get_widget("label-auth-password").hide()
5271d3a
+
5271d3a
+        if withUsername:
5271d3a
+            self.window.get_widget("console-auth-username").show()
5271d3a
+            self.window.get_widget("label-auth-username").show()
5271d3a
+        else:
5271d3a
+            self.window.get_widget("console-auth-username").hide()
5271d3a
+            self.window.get_widget("label-auth-username").hide()
5271d3a
+
5271d3a
+        self.window.get_widget("console-auth-username").set_text(username)
5271d3a
         self.window.get_widget("console-auth-password").set_text(pw)
5271d3a
-        self.window.get_widget("console-auth-password").grab_focus()
5271d3a
+
5271d3a
         if self.config.has_keyring():
5271d3a
             self.window.get_widget("console-auth-remember").set_sensitive(True)
5271d3a
-            if pw != None and pw != "":
5271d3a
+            if pw != "" or username != "":
5271d3a
                 self.window.get_widget("console-auth-remember").set_active(True)
5271d3a
             else:
5271d3a
                 self.window.get_widget("console-auth-remember").set_active(False)
5271d3a
         else:
5271d3a
             self.window.get_widget("console-auth-remember").set_sensitive(False)
5271d3a
         self.window.get_widget("console-pages").set_current_page(PAGE_AUTHENTICATE)
5271d3a
+        if withUsername:
5271d3a
+            self.window.get_widget("console-auth-username").grab_focus()
5271d3a
+        else:
5271d3a
+            self.window.get_widget("console-auth-password").grab_focus()
5271d3a
+
5271d3a
 
5271d3a
     def activate_viewer_page(self):
5271d3a
         self.window.get_widget("console-pages").set_current_page(PAGE_VNCVIEWER)
5271d3a
diff -r 6082392f2279 -r 5b61bd10a66b src/virtManager/keyring.py
5271d3a
--- a/src/virtManager/keyring.py	Wed May 06 16:47:10 2009 +0100
5271d3a
+++ b/src/virtManager/keyring.py	Thu May 07 19:22:33 2009 +0100
5271d3a
@@ -38,6 +38,8 @@
5271d3a
                 if not("default" in gnomekeyring.list_keyring_names_sync()):
5271d3a
                     gnomekeyring.create_sync("default", None)
5271d3a
                 self.keyring = gnomekeyring.get_default_keyring_sync()
5271d3a
+                if self.keyring == None:
5271d3a
+                    logging.warning("Failed to create default keyring")
5271d3a
             except:
5271d3a
                 logging.warning(("Keyring unavailable: '%s'") % (str((sys.exc_info())[0]) + " "  + str((sys.exc_info())[1])))
5271d3a
                 self.keyring = None
5271d3a
@@ -61,6 +63,7 @@
5271d3a
 
5271d3a
             return _id
5271d3a
         except:
5271d3a
+            logging.warning(("Failed to add secret: '%s'") % (str((sys.exc_info())[0]) + " "  + str((sys.exc_info())[1])))
5271d3a
             return None
5271d3a
 
5271d3a
     def get_secret(self, _id):
5271d3a
diff -r 6082392f2279 -r 5b61bd10a66b src/vmm-details.glade
5271d3a
--- a/src/vmm-details.glade	Wed May 06 16:47:10 2009 +0100
5271d3a
+++ b/src/vmm-details.glade	Thu May 07 19:22:33 2009 +0100
5271d3a
@@ -553,7 +553,7 @@
5271d3a
                   <widget class="GtkTable" id="console-auth">
5271d3a
                     <property name="visible">True</property>
5271d3a
                     <property name="border_width">3</property>
5271d3a
-                    <property name="n_rows">2</property>
5271d3a
+                    <property name="n_rows">3</property>
5271d3a
                     <property name="n_columns">3</property>
5271d3a
                     <property name="column_spacing">3</property>
5271d3a
                     <property name="row_spacing">3</property>
5271d3a
@@ -564,6 +564,80 @@
5271d3a
                       <placeholder/>
5271d3a
                     </child>
5271d3a
                     <child>
5271d3a
+                      <placeholder/>
5271d3a
+                    </child>
5271d3a
+                    <child>
5271d3a
+                      <widget class="GtkLabel" id="label-auth-password">
5271d3a
+                        <property name="visible">True</property>
5271d3a
+                        <property name="xalign">0</property>
5271d3a
+                        <property name="label" translatable="yes">Password:</property>
5271d3a
+                      </widget>
5271d3a
+                      <packing>
5271d3a
+                        <property name="top_attach">1</property>
5271d3a
+                        <property name="bottom_attach">2</property>
5271d3a
+                        <property name="x_options">GTK_FILL</property>
5271d3a
+                        <property name="y_options"></property>
5271d3a
+                      </packing>
5271d3a
+                    </child>
5271d3a
+                    <child>
5271d3a
+                      <widget class="GtkEntry" id="console-auth-password">
5271d3a
+                        <property name="visible">True</property>
5271d3a
+                        <property name="can_focus">True</property>
5271d3a
+                        <property name="visibility">False</property>
5271d3a
+                        <property name="invisible_char">*</property>
5271d3a
+                      </widget>
5271d3a
+                      <packing>
5271d3a
+                        <property name="left_attach">1</property>
5271d3a
+                        <property name="right_attach">2</property>
5271d3a
+                        <property name="top_attach">1</property>
5271d3a
+                        <property name="bottom_attach">2</property>
5271d3a
+                        <property name="y_options"></property>
5271d3a
+                      </packing>
5271d3a
+                    </child>
5271d3a
+                    <child>
5271d3a
+                      <widget class="GtkCheckButton" id="console-auth-remember">
5271d3a
+                        <property name="visible">True</property>
5271d3a
+                        <property name="can_focus">True</property>
5271d3a
+                        <property name="label" translatable="yes">Save this password in your keyring</property>
5271d3a
+                        <property name="use_underline">True</property>
5271d3a
+                        <property name="yalign">0</property>
5271d3a
+                        <property name="response_id">0</property>
5271d3a
+                        <property name="draw_indicator">True</property>
5271d3a
+                      </widget>
5271d3a
+                      <packing>
5271d3a
+                        <property name="left_attach">1</property>
5271d3a
+                        <property name="right_attach">2</property>
5271d3a
+                        <property name="top_attach">2</property>
5271d3a
+                        <property name="bottom_attach">3</property>
5271d3a
+                        <property name="x_options">GTK_FILL</property>
5271d3a
+                        <property name="y_options"></property>
5271d3a
+                      </packing>
5271d3a
+                    </child>
5271d3a
+                    <child>
5271d3a
+                      <widget class="GtkLabel" id="label-auth-username">
5271d3a
+                        <property name="visible">True</property>
5271d3a
+                        <property name="xalign">0</property>
5271d3a
+                        <property name="label" translatable="yes">Username:</property>
5271d3a
+                      </widget>
5271d3a
+                      <packing>
5271d3a
+                        <property name="x_options">GTK_FILL</property>
5271d3a
+                        <property name="y_options"></property>
5271d3a
+                      </packing>
5271d3a
+                    </child>
5271d3a
+                    <child>
5271d3a
+                      <widget class="GtkEntry" id="console-auth-username">
5271d3a
+                        <property name="visible">True</property>
5271d3a
+                        <property name="can_focus">True</property>
5271d3a
+                        <property name="invisible_char">*</property>
5271d3a
+                        <signal name="activate" handler="on_console_auth_password_activate"/>
5271d3a
+                      </widget>
5271d3a
+                      <packing>
5271d3a
+                        <property name="left_attach">1</property>
5271d3a
+                        <property name="right_attach">2</property>
5271d3a
+                        <property name="y_options"></property>
5271d3a
+                      </packing>
5271d3a
+                    </child>
5271d3a
+                    <child>
5271d3a
                       <widget class="GtkButton" id="console-auth-login">
5271d3a
                         <property name="visible">True</property>
5271d3a
                         <property name="can_focus">True</property>
5271d3a
@@ -608,53 +682,12 @@
5271d3a
                       <packing>
5271d3a
                         <property name="left_attach">2</property>
5271d3a
                         <property name="right_attach">3</property>
5271d3a
-                        <property name="x_options">GTK_FILL</property>
5271d3a
-                        <property name="y_options"></property>
5271d3a
-                      </packing>
5271d3a
-                    </child>
5271d3a
-                    <child>
5271d3a
-                      <widget class="GtkEntry" id="console-auth-password">
5271d3a
-                        <property name="visible">True</property>
5271d3a
-                        <property name="can_focus">True</property>
5271d3a
-                        <property name="visibility">False</property>
5271d3a
-                        <property name="invisible_char">*</property>
5271d3a
-                        <signal name="activate" handler="on_console_auth_password_activate"/>
5271d3a
-                      </widget>
5271d3a
-                      <packing>
5271d3a
-                        <property name="left_attach">1</property>
5271d3a
-                        <property name="right_attach">2</property>
5271d3a
-                        <property name="y_options"></property>
5271d3a
-                      </packing>
5271d3a
-                    </child>
5271d3a
-                    <child>
5271d3a
-                      <widget class="GtkCheckButton" id="console-auth-remember">
5271d3a
-                        <property name="visible">True</property>
5271d3a
-                        <property name="can_focus">True</property>
5271d3a
-                        <property name="label" translatable="yes">Save this password in your keyring</property>
5271d3a
-                        <property name="use_underline">True</property>
5271d3a
-                        <property name="response_id">0</property>
5271d3a
-                        <property name="draw_indicator">True</property>
5271d3a
-                      </widget>
5271d3a
-                      <packing>
5271d3a
-                        <property name="left_attach">1</property>
5271d3a
-                        <property name="right_attach">2</property>
5271d3a
                         <property name="top_attach">1</property>
5271d3a
                         <property name="bottom_attach">2</property>
5271d3a
                         <property name="x_options">GTK_FILL</property>
5271d3a
                         <property name="y_options"></property>
5271d3a
                       </packing>
5271d3a
                     </child>
5271d3a
-                    <child>
5271d3a
-                      <widget class="GtkLabel" id="label436">
5271d3a
-                        <property name="visible">True</property>
5271d3a
-                        <property name="xalign">0</property>
5271d3a
-                        <property name="label" translatable="yes">Password:</property>
5271d3a
-                      </widget>
5271d3a
-                      <packing>
5271d3a
-                        <property name="x_options">GTK_FILL</property>
5271d3a
-                        <property name="y_options"></property>
5271d3a
-                      </packing>
5271d3a
-                    </child>
5271d3a
                   </widget>
5271d3a
                   <packing>
5271d3a
                     <property name="position">2</property>