2 * Copyright (C) 2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * Parts are derived from 'gvnccapture':
22 * Copyright (C) 2010 Daniel P. Berrange <dan@berrange.com>
24 * This library is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2.0 of the License, or (at your option) any later version.
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * Lesser General Public License for more details.
34 * You should have received a copy of the GNU Lesser General Public
35 * License along with this library; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
50 #include <glib/gi18n.h>
51 #include <vncconnection.h>
52 #include <vncbaseframebuffer.h>
56 static const guint preferable_auths[] = {
58 * Both these two provide TLS based auth, and can layer
59 * all the other auth types on top. So these two must
62 VNC_CONNECTION_AUTH_VENCRYPT,
63 VNC_CONNECTION_AUTH_TLS,
66 * Then stackable auth types in order of preference
68 VNC_CONNECTION_AUTH_SASL,
69 VNC_CONNECTION_AUTH_MSLOGON,
70 VNC_CONNECTION_AUTH_VNC,
75 VNC_CONNECTION_AUTH_NONE
80 get_credential (const gchar *prompt, gboolean doecho)
83 struct termios old, new;
94 /* Turn echoing off and fail if we can't. */
95 if (!doecho && tcgetattr (fileno (stdin), &old) != 0)
99 if (!doecho && tcsetattr(fileno(stdin), TCSAFLUSH, &new) != 0)
102 doecho = TRUE; /* Avoid unused parameter compile warning */
105 /* Read the password. */
106 if ((res = fgets(buf, n, stdin)) != NULL) {
108 if (res[len-1] == '\n')
112 #ifdef HAVE_TERMIOS_H
113 /* Restore terminal. */
116 (void) tcsetattr(fileno (stdin), TCSAFLUSH, &old);
120 return res ? g_strdup(res) : NULL;
124 vc_vnc_auth_credential (VncConnection *conn, GValueArray *credList,
127 self_t *self = opaque;
131 data = g_new0(char *, credList->n_values);
133 for (i = 0 ; i < credList->n_values ; i++) {
134 GValue *cred = g_value_array_get_nth(credList, i);
135 switch (g_value_get_enum(cred)) {
136 case VNC_CONNECTION_CREDENTIAL_PASSWORD:
137 data[i] = get_credential ("Password: ", FALSE);
140 fprintf (stderr, "vnc: failed to read password\n");
141 vnc_connection_shutdown(conn);
145 case VNC_CONNECTION_CREDENTIAL_USERNAME:
146 data[i] = get_credential ("Username: ", TRUE);
149 fprintf (stderr, "vnc: failed to read username\n");
150 vnc_connection_shutdown(conn);
154 case VNC_CONNECTION_CREDENTIAL_CLIENTNAME:
155 data[i] = g_strdup ("virt-click");
161 for (i = 0 ; i < credList->n_values ; i++) {
162 GValue *cred = g_value_array_get_nth(credList, i);
164 if (!vnc_connection_set_credential(conn,
165 g_value_get_enum(cred),
167 fprintf (stderr, "vnc: failed to set credential type %d %s\n",
168 g_value_get_enum(cred), data[i]);
169 vnc_connection_shutdown(conn);
173 fprintf (stderr, "vnc: unsupported credential type %d\n",
174 g_value_get_enum(cred));
175 vnc_connection_shutdown(conn);
180 for (i = 0 ; i < credList->n_values ; i++)
186 vc_vnc_auth_choose_type (VncConnection *conn,
188 gpointer opaque G_GNUC_UNUSED)
192 if (!types->n_values) {
193 VNC_DEBUG("No auth types to choose from");
197 for (i = 0 ; i < G_N_ELEMENTS(preferable_auths) ; i++) {
198 int pref = preferable_auths[i];
200 for (j = 0 ; j < types->n_values ; j++) {
201 GValue *type = g_value_array_get_nth(types, j);
202 VNC_DEBUG("Compare %d vs %d", pref, g_value_get_enum(type));
203 if (pref == g_value_get_enum(type)) {
204 VNC_DEBUG("Chosen auth %d", pref);
205 vnc_connection_set_auth_type(conn, pref);
211 GValue *type = g_value_array_get_nth(types, 0);
212 VNC_DEBUG("Chosen default auth %d", g_value_get_enum(type));
213 vnc_connection_set_auth_type(conn, g_value_get_enum(type));