[virt-tools-list] [PATCH virt-viewer V2 1/2] remote-viewer: allow username in ovirt URIs

Jonathon Jongsma jjongsma at redhat.com
Thu Aug 7 15:20:55 UTC 2014


When the user launches remote-viewer with an ovirt URI of the form

        ovirt://user@host/vmname

Pre-populate the authentication dialog with the specified username. We
don't support specifying the password on the commandline, since that is
a potential security risk.

rhbz#1061826
---

Changes since V1: coding style fixups (Fabiano) and grab focus on password
entry if username is pre-filled (Christophe)

 src/remote-viewer.c    | 21 +++++++++++++++++----
 src/virt-viewer-auth.c | 13 ++++++++++++-
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/remote-viewer.c b/src/remote-viewer.c
index 3a0a71e..b2cf748 100644
--- a/src/remote-viewer.c
+++ b/src/remote-viewer.c
@@ -624,7 +624,7 @@ remote_viewer_window_added(VirtViewerApp *app,
 
 #ifdef HAVE_OVIRT
 static gboolean
-parse_ovirt_uri(const gchar *uri_str, char **rest_uri, char **name)
+parse_ovirt_uri(const gchar *uri_str, char **rest_uri, char **name, char **username)
 {
     char *vm_name = NULL;
     char *rel_path;
@@ -662,6 +662,9 @@ parse_ovirt_uri(const gchar *uri_str, char **rest_uri, char **name)
     vm_name = path_elements[element_count-1];
     path_elements[element_count-1] = NULL;
 
+    if (username && uri->user)
+        *username = g_strdup(uri->user);
+
     /* build final URI */
     rel_path = g_strjoinv("/", path_elements);
     /* FIXME: how to decide between http and https? */
@@ -681,10 +684,14 @@ static gboolean
 authenticate_cb(RestProxy *proxy, G_GNUC_UNUSED RestProxyAuth *auth,
                 G_GNUC_UNUSED gboolean retrying, gpointer user_data)
 {
-    gchar *username;
-    gchar *password;
+    gchar *username = NULL;
+    gchar *password = NULL;
     VirtViewerWindow *window;
 
+    g_object_get(proxy,
+                 "username", &username,
+                 NULL);
+
     window = virt_viewer_app_get_main_window(VIRT_VIEWER_APP(user_data));
     int ret = virt_viewer_auth_collect_credentials(virt_viewer_window_get_window(window),
                                                    "oVirt",
@@ -716,6 +723,7 @@ create_ovirt_session(VirtViewerApp *app, const char *uri)
     GError *error = NULL;
     char *rest_uri = NULL;
     char *vm_name = NULL;
+    char *username = NULL;
     gboolean success = FALSE;
     guint port;
     guint secure_port;
@@ -730,11 +738,15 @@ create_ovirt_session(VirtViewerApp *app, const char *uri)
 
     g_return_val_if_fail(VIRT_VIEWER_IS_APP(app), FALSE);
 
-    if (!parse_ovirt_uri(uri, &rest_uri, &vm_name))
+    if (!parse_ovirt_uri(uri, &rest_uri, &vm_name, &username))
         goto error;
     proxy = ovirt_proxy_new(rest_uri);
     if (proxy == NULL)
         goto error;
+
+    g_object_set(proxy,
+                 "username", username,
+                 NULL);
     ovirt_set_proxy_options(proxy);
     g_signal_connect(G_OBJECT(proxy), "authenticate",
                      G_CALLBACK(authenticate_cb), app);
@@ -817,6 +829,7 @@ create_ovirt_session(VirtViewerApp *app, const char *uri)
     success = TRUE;
 
 error:
+    g_free(username);
     g_free(rest_uri);
     g_free(vm_name);
     g_free(ticket);
diff --git a/src/virt-viewer-auth.c b/src/virt-viewer-auth.c
index a5b6393..a796619 100644
--- a/src/virt-viewer-auth.c
+++ b/src/virt-viewer-auth.c
@@ -32,6 +32,10 @@
 #include "virt-viewer-auth.h"
 
 
+/* NOTE: if username is provided, and *username is non-NULL, the user input
+ * field will be pre-filled with this value. The existing string will be freed
+ * before setting the output parameter to the user-entered value.
+ */
 int
 virt_viewer_auth_collect_credentials(GtkWindow *window,
                                      const char *type,
@@ -60,6 +64,11 @@ virt_viewer_auth_collect_credentials(GtkWindow *window,
     promptPassword = GTK_WIDGET(gtk_builder_get_object(creds, "prompt-password"));
 
     gtk_widget_set_sensitive(credUsername, username != NULL);
+    if (username && *username) {
+        gtk_entry_set_text(GTK_ENTRY(credUsername), *username);
+        /* if username is pre-filled, move focus to password field */
+        gtk_widget_grab_focus(credPassword);
+    }
     gtk_widget_set_sensitive(promptUsername, username != NULL);
     gtk_widget_set_sensitive(credPassword, password != NULL);
     gtk_widget_set_sensitive(promptPassword, password != NULL);
@@ -82,8 +91,10 @@ virt_viewer_auth_collect_credentials(GtkWindow *window,
     gtk_widget_hide(dialog);
 
     if (response == GTK_RESPONSE_OK) {
-        if (username)
+        if (username) {
+            g_free(*username);
             *username = g_strdup(gtk_entry_get_text(GTK_ENTRY(credUsername)));
+        }
         if (password)
             *password = g_strdup(gtk_entry_get_text(GTK_ENTRY(credPassword)));
     }
-- 
1.9.3




More information about the virt-tools-list mailing list