summaryrefslogtreecommitdiffstats
path: root/src/virt-viewer-auth.c
blob: 81f09bc2c00f6b4527ac656a69f9c484acfb8429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Virt Viewer: A virtual machine console viewer
 *
 * Copyright (C) 2007-2012 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <gtk/gtk.h>
#include <string.h>

#ifdef HAVE_GTK_VNC
#include <vncdisplay.h>
#endif

#include "virt-viewer-auth.h"


int
virt_viewer_auth_collect_credentials(GtkWindow *window,
                                     const char *type,
                                     const char *address,
                                     char **username,
                                     char **password)
{
    GtkWidget *dialog = NULL;
    GtkBuilder *creds = virt_viewer_util_load_ui("virt-viewer-auth.xml");
    GtkWidget *credUsername;
    GtkWidget *credPassword;
    GtkWidget *promptUsername;
    GtkWidget *promptPassword;
    GtkWidget *labelMessage;
    int response;
    char *message;

    dialog = GTK_WIDGET(gtk_builder_get_object(creds, "auth"));
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
    gtk_window_set_transient_for(GTK_WINDOW(dialog), window);

    labelMessage = GTK_WIDGET(gtk_builder_get_object(creds, "message"));
    credUsername = GTK_WIDGET(gtk_builder_get_object(creds, "cred-username"));
    promptUsername = GTK_WIDGET(gtk_builder_get_object(creds, "prompt-username"));
    credPassword = GTK_WIDGET(gtk_builder_get_object(creds, "cred-password"));
    promptPassword = GTK_WIDGET(gtk_builder_get_object(creds, "prompt-password"));

    gtk_widget_set_sensitive(credUsername, username != NULL);
    gtk_widget_set_sensitive(promptUsername, username != NULL);
    gtk_widget_set_sensitive(credPassword, password != NULL);
    gtk_widget_set_sensitive(promptPassword, password != NULL);

    if (address) {
        message = g_strdup_printf("Authentication is required for the %s connection to:\n\n"
                                  "<b>%s</b>\n\n",
                                  type,
                                  address);
    } else {
        message = g_strdup_printf("Authentication is required for the %s connection:\n",
                                  type);
    }

    gtk_label_set_markup(GTK_LABEL(labelMessage), message);
    g_free(message);

    gtk_widget_show_all(dialog);
    response = gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_hide(dialog);

    if (response == GTK_RESPONSE_OK) {
        if (username)
            *username = g_strdup(gtk_entry_get_text(GTK_ENTRY(credUsername)));
        if (password)
            *password = g_strdup(gtk_entry_get_text(GTK_ENTRY(credPassword)));
    }

    gtk_widget_destroy(GTK_WIDGET(dialog));
    g_object_unref(G_OBJECT(creds));

    return response == GTK_RESPONSE_OK ? 0 : -1;
}

#ifdef HAVE_GTK_VNC
void
virt_viewer_auth_vnc_credentials(VirtViewerSession *session,
                                 GtkWindow *window,
                                 GtkWidget *vnc,
                                 GValueArray *credList,
                                 char *vncAddress)
{
    char *username = NULL, *password = NULL;
    gboolean wantPassword = FALSE, wantUsername = FALSE;
    int i;

    DEBUG_LOG("Got VNC credential request for %d credential(s)", credList->n_values);

    for (i = 0 ; i < credList->n_values ; i++) {
        GValue *cred = g_value_array_get_nth(credList, i);
        switch (g_value_get_enum(cred)) {
        case VNC_DISPLAY_CREDENTIAL_USERNAME:
            wantUsername = TRUE;
            break;
        case VNC_DISPLAY_CREDENTIAL_PASSWORD:
            wantPassword = TRUE;
            break;
        case VNC_DISPLAY_CREDENTIAL_CLIENTNAME:
            break;
        default:
            DEBUG_LOG("Unsupported credential type %d", g_value_get_enum(cred));
            vnc_display_close(VNC_DISPLAY(vnc));
            goto cleanup;
        }
    }

    VirtViewerFile *file = virt_viewer_session_get_file(session);
    if (file != NULL) {
        if (wantUsername && virt_viewer_file_is_set(file, "username")) {
            username = virt_viewer_file_get_username(file);
            wantUsername = FALSE;
        }
        if (wantPassword && virt_viewer_file_is_set(file, "password")) {
            password = virt_viewer_file_get_password(file);
            g_message("%s", password);
            wantPassword = FALSE;
        }
    }

    if (wantUsername || wantPassword) {
        int ret = virt_viewer_auth_collect_credentials(window,
                                                       "VNC", vncAddress,
                                                       wantUsername ? &username : NULL,
                                                       wantPassword ? &password : NULL);

        if (ret < 0) {
            vnc_display_close(VNC_DISPLAY(vnc));
            goto cleanup;
        }
    }

    for (i = 0 ; i < credList->n_values ; i++) {
        GValue *cred = g_value_array_get_nth(credList, i);
        switch (g_value_get_enum(cred)) {
        case VNC_DISPLAY_CREDENTIAL_USERNAME:
            if (!username ||
                vnc_display_set_credential(VNC_DISPLAY(vnc),
                                           g_value_get_enum(cred),
                                           username)) {
                DEBUG_LOG("Failed to set credential type %d", g_value_get_enum(cred));
                vnc_display_close(VNC_DISPLAY(vnc));
            }
            break;
        case VNC_DISPLAY_CREDENTIAL_PASSWORD:
            if (!password ||
                vnc_display_set_credential(VNC_DISPLAY(vnc),
                                           g_value_get_enum(cred),
                                           password)) {
                DEBUG_LOG("Failed to set credential type %d", g_value_get_enum(cred));
                vnc_display_close(VNC_DISPLAY(vnc));
            }
            break;
        case VNC_DISPLAY_CREDENTIAL_CLIENTNAME:
            if (vnc_display_set_credential(VNC_DISPLAY(vnc),
                                           g_value_get_enum(cred),
                                           "libvirt")) {
                DEBUG_LOG("Failed to set credential type %d", g_value_get_enum(cred));
                vnc_display_close(VNC_DISPLAY(vnc));
            }
            break;
        default:
            DEBUG_LOG("Unsupported credential type %d", g_value_get_enum(cred));
            vnc_display_close(VNC_DISPLAY(vnc));
        }
    }

 cleanup:
    g_free(username);
    g_free(password);
}
#endif

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  indent-tabs-mode: nil
 * End:
 */