Add a preference list for SSH-2 key exchange algorithms, on a new "Kex" panel
[u/mdw/putty] / windows / wincons.c
CommitLineData
ff2ae367 1/*
2 * console.c: various interactive-prompt routines shared between
f4ff9455 3 * the Windows console PuTTY tools
ff2ae367 4 */
5
ff2ae367 6#include <stdio.h>
7#include <stdlib.h>
8#include <stdarg.h>
9
10#include "putty.h"
11#include "storage.h"
12#include "ssh.h"
13
14int console_batch_mode = FALSE;
15
d3fef4a5 16static void *console_logctx = NULL;
17
93b581bd 18/*
19 * Clean up and exit.
20 */
21void cleanup_exit(int code)
22{
23 /*
24 * Clean up.
25 */
26 sk_cleanup();
93b581bd 27
bf61b566 28 random_save_seed();
93b581bd 29#ifdef MSCRYPTOAPI
bf61b566 30 crypto_wrapup();
93b581bd 31#endif
93b581bd 32
33 exit(code);
34}
35
39934deb 36void notify_remote_exit(void *frontend)
37{
38}
39
40void timer_change_notify(long next)
41{
42}
43
a8327734 44void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
ff2ae367 45 char *keystr, char *fingerprint)
46{
47 int ret;
48 HANDLE hin;
49 DWORD savemode, i;
50
51 static const char absentmsg_batch[] =
52 "The server's host key is not cached in the registry. You\n"
53 "have no guarantee that the server is the computer you\n"
54 "think it is.\n"
65f66c88 55 "The server's %s key fingerprint is:\n"
ff2ae367 56 "%s\n"
57 "Connection abandoned.\n";
58 static const char absentmsg[] =
59 "The server's host key is not cached in the registry. You\n"
60 "have no guarantee that the server is the computer you\n"
61 "think it is.\n"
65f66c88 62 "The server's %s key fingerprint is:\n"
ff2ae367 63 "%s\n"
64 "If you trust this host, enter \"y\" to add the key to\n"
65 "PuTTY's cache and carry on connecting.\n"
66 "If you want to carry on connecting just once, without\n"
67 "adding the key to the cache, enter \"n\".\n"
68 "If you do not trust this host, press Return to abandon the\n"
69 "connection.\n"
70 "Store key in cache? (y/n) ";
71
72 static const char wrongmsg_batch[] =
73 "WARNING - POTENTIAL SECURITY BREACH!\n"
74 "The server's host key does not match the one PuTTY has\n"
75 "cached in the registry. This means that either the\n"
76 "server administrator has changed the host key, or you\n"
77 "have actually connected to another computer pretending\n"
78 "to be the server.\n"
65f66c88 79 "The new %s key fingerprint is:\n"
ff2ae367 80 "%s\n"
81 "Connection abandoned.\n";
82 static const char wrongmsg[] =
83 "WARNING - POTENTIAL SECURITY BREACH!\n"
84 "The server's host key does not match the one PuTTY has\n"
85 "cached in the registry. This means that either the\n"
86 "server administrator has changed the host key, or you\n"
87 "have actually connected to another computer pretending\n"
88 "to be the server.\n"
65f66c88 89 "The new %s key fingerprint is:\n"
ff2ae367 90 "%s\n"
91 "If you were expecting this change and trust the new key,\n"
92 "enter \"y\" to update PuTTY's cache and continue connecting.\n"
93 "If you want to carry on connecting but without updating\n"
94 "the cache, enter \"n\".\n"
95 "If you want to abandon the connection completely, press\n"
96 "Return to cancel. Pressing Return is the ONLY guaranteed\n"
97 "safe choice.\n"
98 "Update cached key? (y/n, Return cancels connection) ";
99
100 static const char abandoned[] = "Connection abandoned.\n";
101
102 char line[32];
103
104 /*
105 * Verify the key against the registry.
106 */
107 ret = verify_host_key(host, port, keytype, keystr);
108
109 if (ret == 0) /* success - key matched OK */
110 return;
111
112 if (ret == 2) { /* key was different */
113 if (console_batch_mode) {
65f66c88 114 fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
93b581bd 115 cleanup_exit(1);
ff2ae367 116 }
65f66c88 117 fprintf(stderr, wrongmsg, keytype, fingerprint);
ff2ae367 118 fflush(stderr);
119 }
120 if (ret == 1) { /* key was absent */
121 if (console_batch_mode) {
65f66c88 122 fprintf(stderr, absentmsg_batch, keytype, fingerprint);
93b581bd 123 cleanup_exit(1);
ff2ae367 124 }
65f66c88 125 fprintf(stderr, absentmsg, keytype, fingerprint);
ff2ae367 126 fflush(stderr);
127 }
128
129 hin = GetStdHandle(STD_INPUT_HANDLE);
130 GetConsoleMode(hin, &savemode);
131 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
132 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
133 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
134 SetConsoleMode(hin, savemode);
135
136 if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
137 if (line[0] == 'y' || line[0] == 'Y')
138 store_host_key(host, port, keytype, keystr);
139 } else {
140 fprintf(stderr, abandoned);
93b581bd 141 cleanup_exit(0);
ff2ae367 142 }
143}
144
125105d1 145void update_specials_menu(void *frontend)
146{
147}
148
ff2ae367 149/*
83e7d008 150 * Ask whether the selected algorithm is acceptable (since it was
ff2ae367 151 * below the configured 'warn' threshold).
ff2ae367 152 */
83e7d008 153void askalg(void *frontend, const char *algtype, const char *algname)
ff2ae367 154{
155 HANDLE hin;
156 DWORD savemode, i;
157
158 static const char msg[] =
83e7d008 159 "The first %s supported by the server is\n"
ff2ae367 160 "%s, which is below the configured warning threshold.\n"
161 "Continue with connection? (y/n) ";
162 static const char msg_batch[] =
83e7d008 163 "The first %s supported by the server is\n"
ff2ae367 164 "%s, which is below the configured warning threshold.\n"
165 "Connection abandoned.\n";
166 static const char abandoned[] = "Connection abandoned.\n";
167
168 char line[32];
169
170 if (console_batch_mode) {
83e7d008 171 fprintf(stderr, msg_batch, algtype, algname);
93b581bd 172 cleanup_exit(1);
ff2ae367 173 }
174
83e7d008 175 fprintf(stderr, msg, algtype, algname);
ff2ae367 176 fflush(stderr);
177
178 hin = GetStdHandle(STD_INPUT_HANDLE);
179 GetConsoleMode(hin, &savemode);
180 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
181 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
182 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
183 SetConsoleMode(hin, savemode);
184
185 if (line[0] == 'y' || line[0] == 'Y') {
186 return;
187 } else {
188 fprintf(stderr, abandoned);
93b581bd 189 cleanup_exit(0);
ff2ae367 190 }
191}
192
193/*
194 * Ask whether to wipe a session log file before writing to it.
195 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
196 */
9a30e26b 197int askappend(void *frontend, Filename filename)
ff2ae367 198{
199 HANDLE hin;
200 DWORD savemode, i;
201
202 static const char msgtemplate[] =
203 "The session log file \"%.*s\" already exists.\n"
204 "You can overwrite it with a new session log,\n"
205 "append your session log to the end of it,\n"
206 "or disable session logging for this session.\n"
207 "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
208 "or just press Return to disable logging.\n"
209 "Wipe the log file? (y/n, Return cancels logging) ";
210
211 static const char msgtemplate_batch[] =
212 "The session log file \"%.*s\" already exists.\n"
213 "Logging will not be enabled.\n";
214
215 char line[32];
216
217 if (console_batch_mode) {
9a30e26b 218 fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path);
ff2ae367 219 fflush(stderr);
220 return 0;
221 }
9a30e26b 222 fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path);
ff2ae367 223 fflush(stderr);
224
225 hin = GetStdHandle(STD_INPUT_HANDLE);
226 GetConsoleMode(hin, &savemode);
227 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
228 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
229 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
230 SetConsoleMode(hin, savemode);
231
232 if (line[0] == 'y' || line[0] == 'Y')
233 return 2;
234 else if (line[0] == 'n' || line[0] == 'N')
235 return 1;
236 else
237 return 0;
238}
239
240/*
241 * Warn about the obsolescent key file format.
a8327734 242 *
243 * Uniquely among these functions, this one does _not_ expect a
244 * frontend handle. This means that if PuTTY is ported to a
245 * platform which requires frontend handles, this function will be
246 * an anomaly. Fortunately, the problem it addresses will not have
247 * been present on that platform, so it can plausibly be
248 * implemented as an empty function.
ff2ae367 249 */
250void old_keyfile_warning(void)
251{
252 static const char message[] =
253 "You are loading an SSH 2 private key which has an\n"
254 "old version of the file format. This means your key\n"
255 "file is not fully tamperproof. Future versions of\n"
256 "PuTTY may stop supporting this private key format,\n"
257 "so we recommend you convert your key to the new\n"
258 "format.\n"
259 "\n"
260 "Once the key is loaded into PuTTYgen, you can perform\n"
261 "this conversion simply by saving it again.\n";
262
263 fputs(message, stderr);
264}
265
d3fef4a5 266void console_provide_logctx(void *logctx)
267{
268 console_logctx = logctx;
269}
270
cbe2d68f 271void logevent(void *frontend, const char *string)
ff2ae367 272{
d3fef4a5 273 if (console_logctx)
274 log_eventlog(console_logctx, string);
ff2ae367 275}
276
ff2ae367 277int console_get_line(const char *prompt, char *str,
278 int maxlen, int is_pw)
279{
280 HANDLE hin, hout;
281 DWORD savemode, newmode, i;
282
ff2ae367 283 if (console_batch_mode) {
284 if (maxlen > 0)
285 str[0] = '\0';
a21a6a9a 286 return 0;
ff2ae367 287 } else {
288 hin = GetStdHandle(STD_INPUT_HANDLE);
289 hout = GetStdHandle(STD_OUTPUT_HANDLE);
290 if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) {
291 fprintf(stderr, "Cannot get standard input/output handles\n");
93b581bd 292 cleanup_exit(1);
ff2ae367 293 }
294
295 GetConsoleMode(hin, &savemode);
296 newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
297 if (is_pw)
298 newmode &= ~ENABLE_ECHO_INPUT;
299 else
300 newmode |= ENABLE_ECHO_INPUT;
301 SetConsoleMode(hin, newmode);
302
303 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
304 ReadFile(hin, str, maxlen - 1, &i, NULL);
305
306 SetConsoleMode(hin, savemode);
307
308 if ((int) i > maxlen)
309 i = maxlen - 1;
310 else
311 i = i - 2;
312 str[i] = '\0';
313
314 if (is_pw)
315 WriteFile(hout, "\r\n", 2, &i, NULL);
316
a21a6a9a 317 return 1;
ff2ae367 318 }
ff2ae367 319}
f8255dce 320
b9d7bcad 321void frontend_keypress(void *handle)
f8255dce 322{
323 /*
324 * This is nothing but a stub, in console code.
325 */
326 return;
327}