2 * wincons.c - various interactive-prompt routines shared between
3 * the Windows console PuTTY tools
14 int console_batch_mode = FALSE;
16 static void *console_logctx = NULL;
21 void cleanup_exit(int code)
36 void set_busy_status(void *frontend, int status)
40 void notify_remote_exit(void *frontend)
44 void timer_change_notify(unsigned long next)
48 int verify_ssh_host_key(void *frontend, char *host, int port,
49 const char *keytype, char *keystr, char *fingerprint,
50 void (*callback)(void *ctx, int result), void *ctx)
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"
60 "The server's %s key fingerprint is:\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"
67 "The server's %s key fingerprint is:\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"
75 "Store key in cache? (y/n) ";
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"
84 "The new %s key fingerprint is:\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"
94 "The new %s key fingerprint is:\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"
103 "Update cached key? (y/n, Return cancels connection) ";
105 static const char abandoned[] = "Connection abandoned.\n";
110 * Verify the key against the registry.
112 ret = verify_host_key(host, port, keytype, keystr);
114 if (ret == 0) /* success - key matched OK */
117 if (ret == 2) { /* key was different */
118 if (console_batch_mode) {
119 fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
122 fprintf(stderr, wrongmsg, keytype, fingerprint);
125 if (ret == 1) { /* key was absent */
126 if (console_batch_mode) {
127 fprintf(stderr, absentmsg_batch, keytype, fingerprint);
130 fprintf(stderr, absentmsg, keytype, fingerprint);
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);
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);
146 fprintf(stderr, abandoned);
151 void update_specials_menu(void *frontend)
156 * Ask whether the selected algorithm is acceptable (since it was
157 * below the configured 'warn' threshold).
159 int askalg(void *frontend, const char *algtype, const char *algname,
160 void (*callback)(void *ctx, int result), void *ctx)
165 static const char msg[] =
166 "The first %s supported by the server is\n"
167 "%s, which is below the configured warning threshold.\n"
168 "Continue with connection? (y/n) ";
169 static const char msg_batch[] =
170 "The first %s supported by the server is\n"
171 "%s, which is below the configured warning threshold.\n"
172 "Connection abandoned.\n";
173 static const char abandoned[] = "Connection abandoned.\n";
177 if (console_batch_mode) {
178 fprintf(stderr, msg_batch, algtype, algname);
182 fprintf(stderr, msg, algtype, algname);
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);
192 if (line[0] == 'y' || line[0] == 'Y') {
195 fprintf(stderr, abandoned);
200 int askhk(void *frontend, const char *algname, const char *betteralgs,
201 void (*callback)(void *ctx, int result), void *ctx)
206 static const char msg[] =
207 "The first host key type we have stored for this server\n"
208 "is %s, which is below the configured warning threshold.\n"
209 "The server also provides the following types of host key\n"
210 "above the threshold, which we do not have stored:\n"
212 "Continue with connection? (y/n) ";
213 static const char msg_batch[] =
214 "The first host key type we have stored for this server\n"
215 "is %s, which is below the configured warning threshold.\n"
216 "The server also provides the following types of host key\n"
217 "above the threshold, which we do not have stored:\n"
219 "Connection abandoned.\n";
220 static const char abandoned[] = "Connection abandoned.\n";
224 if (console_batch_mode) {
225 fprintf(stderr, msg_batch, algname, betteralgs);
229 fprintf(stderr, msg, algname, betteralgs);
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);
239 if (line[0] == 'y' || line[0] == 'Y') {
242 fprintf(stderr, abandoned);
248 * Ask whether to wipe a session log file before writing to it.
249 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
251 int askappend(void *frontend, Filename *filename,
252 void (*callback)(void *ctx, int result), void *ctx)
257 static const char msgtemplate[] =
258 "The session log file \"%.*s\" already exists.\n"
259 "You can overwrite it with a new session log,\n"
260 "append your session log to the end of it,\n"
261 "or disable session logging for this session.\n"
262 "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
263 "or just press Return to disable logging.\n"
264 "Wipe the log file? (y/n, Return cancels logging) ";
266 static const char msgtemplate_batch[] =
267 "The session log file \"%.*s\" already exists.\n"
268 "Logging will not be enabled.\n";
272 if (console_batch_mode) {
273 fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename->path);
277 fprintf(stderr, msgtemplate, FILENAME_MAX, filename->path);
280 hin = GetStdHandle(STD_INPUT_HANDLE);
281 GetConsoleMode(hin, &savemode);
282 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
283 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
284 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
285 SetConsoleMode(hin, savemode);
287 if (line[0] == 'y' || line[0] == 'Y')
289 else if (line[0] == 'n' || line[0] == 'N')
296 * Warn about the obsolescent key file format.
298 * Uniquely among these functions, this one does _not_ expect a
299 * frontend handle. This means that if PuTTY is ported to a
300 * platform which requires frontend handles, this function will be
301 * an anomaly. Fortunately, the problem it addresses will not have
302 * been present on that platform, so it can plausibly be
303 * implemented as an empty function.
305 void old_keyfile_warning(void)
307 static const char message[] =
308 "You are loading an SSH-2 private key which has an\n"
309 "old version of the file format. This means your key\n"
310 "file is not fully tamperproof. Future versions of\n"
311 "PuTTY may stop supporting this private key format,\n"
312 "so we recommend you convert your key to the new\n"
315 "Once the key is loaded into PuTTYgen, you can perform\n"
316 "this conversion simply by saving it again.\n";
318 fputs(message, stderr);
322 * Display the fingerprints of the PGP Master Keys to the user.
324 void pgp_fingerprints(void)
326 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
327 "be used to establish a trust path from this executable to another\n"
328 "one. See the manual for more information.\n"
329 "(Note: these fingerprints have nothing to do with SSH!)\n"
331 "PuTTY Master Key as of 2015 (RSA, 4096-bit):\n"
332 " " PGP_MASTER_KEY_FP "\n\n"
333 "Original PuTTY Master Key (RSA, 1024-bit):\n"
334 " " PGP_RSA_MASTER_KEY_FP "\n"
335 "Original PuTTY Master Key (DSA, 1024-bit):\n"
336 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
339 void console_provide_logctx(void *logctx)
341 console_logctx = logctx;
344 void logevent(void *frontend, const char *string)
346 log_eventlog(console_logctx, string);
349 static void console_data_untrusted(HANDLE hout, const char *data, int len)
352 /* FIXME: control-character filtering */
353 WriteFile(hout, data, len, &dummy, NULL);
356 int console_get_userpass_input(prompts_t *p,
357 const unsigned char *in, int inlen)
363 * Zero all the results, in case we abort half-way through.
367 for (i = 0; i < (int)p->n_prompts; i++)
368 prompt_set_result(p->prompts[i], "");
372 * The prompts_t might contain a message to be displayed but no
373 * actual prompt. More usually, though, it will contain
374 * questions that the user needs to answer, in which case we
375 * need to ensure that we're able to get the answers.
378 if (console_batch_mode)
380 hin = GetStdHandle(STD_INPUT_HANDLE);
381 if (hin == INVALID_HANDLE_VALUE) {
382 fprintf(stderr, "Cannot get standard input handle\n");
388 * And if we have anything to print, we need standard output.
390 if ((p->name_reqd && p->name) || p->instruction || p->n_prompts) {
391 hout = GetStdHandle(STD_OUTPUT_HANDLE);
392 if (hout == INVALID_HANDLE_VALUE) {
393 fprintf(stderr, "Cannot get standard output handle\n");
401 /* We only print the `name' caption if we have to... */
402 if (p->name_reqd && p->name) {
403 size_t l = strlen(p->name);
404 console_data_untrusted(hout, p->name, l);
405 if (p->name[l-1] != '\n')
406 console_data_untrusted(hout, "\n", 1);
408 /* ...but we always print any `instruction'. */
409 if (p->instruction) {
410 size_t l = strlen(p->instruction);
411 console_data_untrusted(hout, p->instruction, l);
412 if (p->instruction[l-1] != '\n')
413 console_data_untrusted(hout, "\n", 1);
416 for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {
418 DWORD savemode, newmode;
420 prompt_t *pr = p->prompts[curr_prompt];
422 GetConsoleMode(hin, &savemode);
423 newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
425 newmode &= ~ENABLE_ECHO_INPUT;
427 newmode |= ENABLE_ECHO_INPUT;
428 SetConsoleMode(hin, newmode);
430 console_data_untrusted(hout, pr->prompt, strlen(pr->prompt));
437 prompt_ensure_result_size(pr, len * 5 / 4 + 512);
439 r = ReadFile(hin, pr->result + len, pr->resultsize - len - 1,
442 if (!r || ret == 0) {
447 if (pr->result[len - 1] == '\n') {
449 if (pr->result[len - 1] == '\r')
455 SetConsoleMode(hin, savemode);
459 WriteFile(hout, "\r\n", 2, &dummy, NULL);
463 return 0; /* failure due to read error */
466 pr->result[len] = '\0';
469 return 1; /* success */
472 void frontend_keypress(void *handle)
475 * This is nothing but a stub, in console code.