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