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