~ubuntu-branches/ubuntu/precise/virt-viewer/precise

« back to all changes in this revision

Viewing changes to src/auth.c

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville
  • Date: 2010-07-12 23:09:44 UTC
  • mfrom: (1.1.2 upstream) (0.4.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100712230944-2tvx01h0xas5cfkx
Tags: 0.2.1-1ubuntu1
* Merge with Debian unstable. Remaining changes:
  - Put plugins in /usr/lib/{xulrunner-addons,mozilla,firefox-addons}/plugins
    instead of the iceape and iceweasel equivalents.
  - mozilla-virt-viewer Depends on firefox | seamonkey instead of iceape and
    iceweasel.
  - Add Plugin Finder Service metadata for web plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Virt Viewer: A virtual machine console viewer
 
3
 *
 
4
 * Copyright (C) 2007-2009 Red Hat,
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 *
 
20
 * Author: Daniel P. Berrange <berrange@redhat.com>
 
21
 */
 
22
 
 
23
#include <config.h>
 
24
 
 
25
#include <vncdisplay.h>
 
26
#include <gtk/gtk.h>
 
27
#include <glade/glade.h>
 
28
#include <string.h>
 
29
 
 
30
#include "auth.h"
 
31
 
 
32
 
 
33
static int
 
34
viewer_auth_collect_credentials(const char *type,
 
35
                                const char *address,
 
36
                                char **username,
 
37
                                char **password)
 
38
{
 
39
        GtkWidget *dialog = NULL;
 
40
        GladeXML *creds = viewer_load_glade("auth.glade", "auth");
 
41
        GtkWidget *credUsername;
 
42
        GtkWidget *credPassword;
 
43
        GtkWidget *promptUsername;
 
44
        GtkWidget *promptPassword;
 
45
        GtkWidget *labelMessage;
 
46
        int response;
 
47
        char *message;
 
48
 
 
49
        dialog = glade_xml_get_widget(creds, "auth");
 
50
        gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
 
51
 
 
52
        labelMessage = glade_xml_get_widget(creds, "message");
 
53
        credUsername = glade_xml_get_widget(creds, "cred-username");
 
54
        promptUsername = glade_xml_get_widget(creds, "prompt-username");
 
55
        credPassword = glade_xml_get_widget(creds, "cred-password");
 
56
        promptPassword = glade_xml_get_widget(creds, "prompt-password");
 
57
 
 
58
        gtk_widget_set_sensitive(credUsername, username != NULL);
 
59
        gtk_widget_set_sensitive(promptUsername, username != NULL);
 
60
        gtk_widget_set_sensitive(credPassword, password != NULL);
 
61
        gtk_widget_set_sensitive(promptPassword, password != NULL);
 
62
 
 
63
        message = g_strdup_printf("Authentication is required for the %s connection to:\n\n"
 
64
                                  "<b>%s</b>\n\n",
 
65
                                  type,
 
66
                                  address ? address : "<unknown>");
 
67
 
 
68
        gtk_label_set_markup(GTK_LABEL(labelMessage), message);
 
69
        g_free(message);
 
70
 
 
71
        gtk_widget_show_all(dialog);
 
72
        response = gtk_dialog_run(GTK_DIALOG(dialog));
 
73
        gtk_widget_hide(dialog);
 
74
 
 
75
        if (response == GTK_RESPONSE_OK) {
 
76
                if (username)
 
77
                        *username = g_strdup(gtk_entry_get_text(GTK_ENTRY(credUsername)));
 
78
                if (password)
 
79
                        *password = g_strdup(gtk_entry_get_text(GTK_ENTRY(credPassword)));
 
80
        }
 
81
 
 
82
        gtk_widget_destroy(GTK_WIDGET(dialog));
 
83
 
 
84
        return response == GTK_RESPONSE_OK ? 0 : -1;
 
85
}
 
86
 
 
87
void viewer_auth_vnc_credentials(GtkWidget *vnc, GValueArray *credList, char **vncAddress)
 
88
{
 
89
        char *username = NULL, *password = NULL;
 
90
        gboolean wantPassword = FALSE, wantUsername = FALSE;
 
91
        int i;
 
92
 
 
93
        DEBUG_LOG("Got VNC credential request for %d credential(s)", credList->n_values);
 
94
 
 
95
        for (i = 0 ; i < credList->n_values ; i++) {
 
96
                GValue *cred = g_value_array_get_nth(credList, i);
 
97
                switch (g_value_get_enum(cred)) {
 
98
                case VNC_DISPLAY_CREDENTIAL_USERNAME:
 
99
                        wantUsername = TRUE;
 
100
                        break;
 
101
                case VNC_DISPLAY_CREDENTIAL_PASSWORD:
 
102
                        wantPassword = TRUE;
 
103
                        break;
 
104
                case VNC_DISPLAY_CREDENTIAL_CLIENTNAME:
 
105
                        break;
 
106
                default:
 
107
                        DEBUG_LOG("Unsupported credential type %d", g_value_get_enum(cred));
 
108
                        vnc_display_close(VNC_DISPLAY(vnc));
 
109
                        goto cleanup;
 
110
                }
 
111
        }
 
112
 
 
113
        if (wantUsername || wantPassword) {
 
114
                int ret = viewer_auth_collect_credentials("VNC", vncAddress ? *vncAddress : NULL,
 
115
                                                          wantUsername ? &username : NULL,
 
116
                                                          wantPassword ? &password : NULL);
 
117
 
 
118
                if (ret < 0) {
 
119
                        vnc_display_close(VNC_DISPLAY(vnc));
 
120
                        goto cleanup;
 
121
                }
 
122
        }
 
123
 
 
124
        for (i = 0 ; i < credList->n_values ; i++) {
 
125
                GValue *cred = g_value_array_get_nth(credList, i);
 
126
                switch (g_value_get_enum(cred)) {
 
127
                case VNC_DISPLAY_CREDENTIAL_USERNAME:
 
128
                        if (!username ||
 
129
                            vnc_display_set_credential(VNC_DISPLAY(vnc),
 
130
                                                       g_value_get_enum(cred),
 
131
                                                       username)) {
 
132
                                DEBUG_LOG("Failed to set credential type %d", g_value_get_enum(cred));
 
133
                                vnc_display_close(VNC_DISPLAY(vnc));
 
134
                        }
 
135
                        break;
 
136
                case VNC_DISPLAY_CREDENTIAL_PASSWORD:
 
137
                        if (!password ||
 
138
                            vnc_display_set_credential(VNC_DISPLAY(vnc),
 
139
                                                       g_value_get_enum(cred),
 
140
                                                       password)) {
 
141
                                DEBUG_LOG("Failed to set credential type %d", g_value_get_enum(cred));
 
142
                                vnc_display_close(VNC_DISPLAY(vnc));
 
143
                        }
 
144
                        break;
 
145
                case VNC_DISPLAY_CREDENTIAL_CLIENTNAME:
 
146
                        if (vnc_display_set_credential(VNC_DISPLAY(vnc),
 
147
                                                       g_value_get_enum(cred),
 
148
                                                       "libvirt")) {
 
149
                                DEBUG_LOG("Failed to set credential type %d", g_value_get_enum(cred));
 
150
                                vnc_display_close(VNC_DISPLAY(vnc));
 
151
                        }
 
152
                        break;
 
153
                default:
 
154
                        DEBUG_LOG("Unsupported credential type %d", g_value_get_enum(cred));
 
155
                        vnc_display_close(VNC_DISPLAY(vnc));
 
156
                }
 
157
        }
 
158
 
 
159
 cleanup:
 
160
        g_free(username);
 
161
        g_free(password);
 
162
}
 
163
 
 
164
 
 
165
 
 
166
int
 
167
viewer_auth_libvirt_credentials(virConnectCredentialPtr cred,
 
168
                                unsigned int ncred,
 
169
                                void *cbdata)
 
170
{
 
171
        char **username = NULL, **password = NULL;
 
172
        const char *uri = cbdata;
 
173
        int i;
 
174
        int ret = -1;
 
175
 
 
176
        DEBUG_LOG("Got libvirt credential request for %d credential(s)", ncred);
 
177
 
 
178
        for (i = 0 ; i < ncred ; i++) {
 
179
                switch (cred[i].type) {
 
180
                case VIR_CRED_USERNAME:
 
181
                case VIR_CRED_AUTHNAME:
 
182
                        username = &cred[i].result;
 
183
                        break;
 
184
                case VIR_CRED_PASSPHRASE:
 
185
                        password = &cred[i].result;
 
186
                        break;
 
187
                default:
 
188
                        DEBUG_LOG("Unsupported libvirt credential %d", cred[i].type);
 
189
                        return -1;
 
190
                }
 
191
        }
 
192
 
 
193
        if (username || password) {
 
194
                ret = viewer_auth_collect_credentials("libvirt", uri,
 
195
                                                      username, password);
 
196
                if (ret < 0)
 
197
                        goto cleanup;
 
198
        } else {
 
199
                ret = 0;
 
200
        }
 
201
 
 
202
        for (i = 0 ; i < ncred ; i++) {
 
203
                switch (cred[i].type) {
 
204
                case VIR_CRED_AUTHNAME:
 
205
                case VIR_CRED_USERNAME:
 
206
                case VIR_CRED_PASSPHRASE:
 
207
                        if (cred[i].result)
 
208
                                cred[i].resultlen = strlen(cred[i].result);
 
209
                        else
 
210
                                cred[i].resultlen = 0;
 
211
                        DEBUG_LOG("Got '%s' %d %d", cred[i].result, cred[i].resultlen, cred[i].type);
 
212
                        break;
 
213
                }
 
214
        }
 
215
 
 
216
 cleanup:
 
217
        DEBUG_LOG("Return %d", ret);
 
218
        return ret;
 
219
}
 
220
 
 
221
 
 
222
 
 
223
 
 
224
/*
 
225
 * Local variables:
 
226
 *  c-indent-level: 8
 
227
 *  c-basic-offset: 8
 
228
 *  tab-width: 8
 
229
 * End:
 
230
 */