summaryrefslogtreecommitdiff
path: root/src/skey-popup.c
blob: 7209cf641ed94b7d4c99e987baade5bc52b1c0b1 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Copyright © 2002 Jonathan Blandford <jrb@gnome.org>
 * Copyright © 2008 Christian Persch
 *
 * Mate-terminal 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 3 of the License, or
 * (at your option) any later version.
 *
 * Mate-terminal 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, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "terminal-intl.h"

#include "terminal-util.h"
#include "terminal-screen.h"
#include "skey-popup.h"
#include "skey/skey.h"
#include <stdlib.h>
#include <string.h>

#define SKEY_PREFIX "s/key "
#define OTP_PREFIX  "otp-"

typedef struct
{
	TerminalScreen *screen;
	char *seed;
	int seq;
	int hash;
} SkeyData;

static void
skey_data_free (SkeyData *data)
{
	g_free (data->seed);
	g_free (data);
}

static gboolean
extract_seq_and_seed (const gchar  *skey_match,
                      gint         *seq,
                      gchar       **seed)
{
	gchar *end_ptr = NULL;

	/* FIXME: use g_ascii_strtoll */
	*seq = strtol (skey_match + strlen (SKEY_PREFIX), &end_ptr, 0);

	if (end_ptr == NULL || *end_ptr == '\000')
		return FALSE;

	*seed = g_strdup (end_ptr + 1);

	return TRUE;
}

static gboolean
extract_hash_seq_and_seed (const gchar  *otp_match,
                           gint         *hash,
                           gint         *seq,
                           gchar       **seed)
{
	gchar *end_ptr = NULL;
	const gchar *p = otp_match + strlen (OTP_PREFIX);
	gint len = 3;

	if (strncmp (p, "md4", 3) == 0)
		*hash = MD4;
	else if (strncmp (p, "md5", 3) == 0)
		*hash = MD5;
	else if (strncmp (p, "sha1", 4) == 0)
	{
		*hash = SHA1;
		len++;
	}
	else
		return FALSE;

	p += len;

	/* RFC mandates the following skipping */
	while (*p == ' ' || *p == '\t')
	{
		if (*p == '\0')
			return FALSE;

		p++;
	}

	/* FIXME: use g_ascii_strtoll */
	*seq = strtol (p, &end_ptr, 0);

	if (end_ptr == NULL || *end_ptr == '\000')
		return FALSE;

	p = end_ptr;

	while (*p == ' ' || *p == '\t')
	{
		if (*p == '\0')
			return FALSE;
		p++;
	}

	*seed = g_strdup (p);
	return TRUE;
}

static void
skey_challenge_response_cb (GtkWidget *dialog,
                            int response_id,
                            SkeyData *data)
{
	if (response_id == GTK_RESPONSE_OK)
	{
		GtkWidget *entry;
		const char *password;
		char *response;

		entry = g_object_get_data (G_OBJECT (dialog), "skey-entry");
		password = gtk_entry_get_text (GTK_ENTRY (entry));

		/* FIXME: fix skey to use g_malloc */
		response = skey (data->hash, data->seq, data->seed, password);
		if (response)
		{
			VteTerminal *vte_terminal = VTE_TERMINAL (data->screen);
			static const char newline[2] = "\n";

			vte_terminal_feed_child (vte_terminal, response, strlen (response));
			vte_terminal_feed_child (vte_terminal, newline, strlen (newline));
			free (response);
		}
	}

	gtk_widget_destroy (dialog);
}

void
terminal_skey_do_popup (GtkWindow *window,
                        TerminalScreen *screen,
                        const gchar    *skey_match)
{
	GtkWidget *dialog, *label, *entry, *ok_button;
	char *title_text;
	char *seed;
	int seq;
	int hash = MD5;
	SkeyData *data;

	if (strncmp (SKEY_PREFIX, skey_match, strlen (SKEY_PREFIX)) == 0)
	{
		if (!extract_seq_and_seed (skey_match, &seq, &seed))
		{
			terminal_util_show_error_dialog (window, NULL, NULL,
			                                 _("The text you clicked on doesn't "
			                                   "seem to be a valid S/Key "
			                                   "challenge."));
			return;
		}
	}
	else
	{
		if (!extract_hash_seq_and_seed (skey_match, &hash, &seq, &seed))
		{
			terminal_util_show_error_dialog (window, NULL, NULL,
			                                 _("The text you clicked on doesn't "
			                                   "seem to be a valid OTP "
			                                   "challenge."));
			return;
		}
	}

	if (!terminal_util_load_builder_resource (TERMINAL_RESOURCES_PATH_PREFIX G_DIR_SEPARATOR_S "ui/skey-challenge.ui",
	                                      "skey-dialog", &dialog,
	                                      "skey-entry", &entry,
	                                      "text-label", &label,
	                                      "skey-ok-button", &ok_button,
	                                      NULL))
	{
		g_free (seed);
		return;
	}

	title_text = g_strdup_printf ("<big><b>%s</b></big>",
	                              gtk_label_get_text (GTK_LABEL (label)));
	gtk_label_set_label (GTK_LABEL (label), title_text);
	g_free (title_text);

	g_object_set_data (G_OBJECT (dialog), "skey-entry", entry);

	gtk_widget_grab_focus (entry);
	gtk_widget_grab_default (ok_button);
	gtk_entry_set_text (GTK_ENTRY (entry), "");

	gtk_window_set_transient_for (GTK_WINDOW (dialog), window);
	gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
	gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);

	/* FIXME: make this dialogue close if the screen closes! */

	data = g_new (SkeyData, 1);
	data->hash = hash;
	data->seq = seq;
	data->seed = seed;
	data->screen = screen;

	g_signal_connect_data (dialog, "response",
	                       G_CALLBACK (skey_challenge_response_cb),
	                       data, (GClosureNotify) skey_data_free, 0);
	g_signal_connect (dialog, "delete-event",
	                  G_CALLBACK (terminal_util_dialog_response_on_delete), NULL);

	gtk_window_present (GTK_WINDOW (dialog));
}