]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/wincons.c
Windows Plink: treat EOF at host key prompt as 'abort connection'.
[PuTTY.git] / windows / wincons.c
1 /*
2  * wincons.c - various interactive-prompt routines shared between
3  * the Windows console PuTTY tools
4  */
5
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
14 int console_batch_mode = FALSE;
15
16 static void *console_logctx = NULL;
17
18 /*
19  * Clean up and exit.
20  */
21 void cleanup_exit(int code)
22 {
23     /*
24      * Clean up.
25      */
26     sk_cleanup();
27
28     random_save_seed();
29 #ifdef MSCRYPTOAPI
30     crypto_wrapup();
31 #endif
32
33     exit(code);
34 }
35
36 void set_busy_status(void *frontend, int status)
37 {
38 }
39
40 void notify_remote_exit(void *frontend)
41 {
42 }
43
44 void timer_change_notify(unsigned long next)
45 {
46 }
47
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)
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"
60         "The server's %s key fingerprint is:\n"
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"
67         "The server's %s key fingerprint is:\n"
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"
84         "The new %s key fingerprint is:\n"
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"
94         "The new %s key fingerprint is:\n"
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 */
115         return 1;
116
117     if (ret == 2) {                    /* key was different */
118         if (console_batch_mode) {
119             fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
120             return 0;
121         }
122         fprintf(stderr, wrongmsg, keytype, fingerprint);
123         fflush(stderr);
124     }
125     if (ret == 1) {                    /* key was absent */
126         if (console_batch_mode) {
127             fprintf(stderr, absentmsg_batch, keytype, fingerprint);
128             return 0;
129         }
130         fprintf(stderr, absentmsg, keytype, fingerprint);
131         fflush(stderr);
132     }
133
134     line[0] = '\0';         /* fail safe if ReadFile returns no data */
135
136     hin = GetStdHandle(STD_INPUT_HANDLE);
137     GetConsoleMode(hin, &savemode);
138     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
139                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
140     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
141     SetConsoleMode(hin, savemode);
142
143     if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
144         if (line[0] == 'y' || line[0] == 'Y')
145             store_host_key(host, port, keytype, keystr);
146         return 1;
147     } else {
148         fprintf(stderr, abandoned);
149         return 0;
150     }
151 }
152
153 void update_specials_menu(void *frontend)
154 {
155 }
156
157 /*
158  * Ask whether the selected algorithm is acceptable (since it was
159  * below the configured 'warn' threshold).
160  */
161 int askalg(void *frontend, const char *algtype, const char *algname,
162            void (*callback)(void *ctx, int result), void *ctx)
163 {
164     HANDLE hin;
165     DWORD savemode, i;
166
167     static const char msg[] =
168         "The first %s supported by the server is\n"
169         "%s, which is below the configured warning threshold.\n"
170         "Continue with connection? (y/n) ";
171     static const char msg_batch[] =
172         "The first %s supported by the server is\n"
173         "%s, which is below the configured warning threshold.\n"
174         "Connection abandoned.\n";
175     static const char abandoned[] = "Connection abandoned.\n";
176
177     char line[32];
178
179     if (console_batch_mode) {
180         fprintf(stderr, msg_batch, algtype, algname);
181         return 0;
182     }
183
184     fprintf(stderr, msg, algtype, algname);
185     fflush(stderr);
186
187     hin = GetStdHandle(STD_INPUT_HANDLE);
188     GetConsoleMode(hin, &savemode);
189     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
190                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
191     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
192     SetConsoleMode(hin, savemode);
193
194     if (line[0] == 'y' || line[0] == 'Y') {
195         return 1;
196     } else {
197         fprintf(stderr, abandoned);
198         return 0;
199     }
200 }
201
202 int askhk(void *frontend, const char *algname, const char *betteralgs,
203           void (*callback)(void *ctx, int result), void *ctx)
204 {
205     HANDLE hin;
206     DWORD savemode, i;
207
208     static const char msg[] =
209         "The first host key type we have stored for this server\n"
210         "is %s, which is below the configured warning threshold.\n"
211         "The server also provides the following types of host key\n"
212         "above the threshold, which we do not have stored:\n"
213         "%s\n"
214         "Continue with connection? (y/n) ";
215     static const char msg_batch[] =
216         "The first host key type we have stored for this server\n"
217         "is %s, which is below the configured warning threshold.\n"
218         "The server also provides the following types of host key\n"
219         "above the threshold, which we do not have stored:\n"
220         "%s\n"
221         "Connection abandoned.\n";
222     static const char abandoned[] = "Connection abandoned.\n";
223
224     char line[32];
225
226     if (console_batch_mode) {
227         fprintf(stderr, msg_batch, algname, betteralgs);
228         return 0;
229     }
230
231     fprintf(stderr, msg, algname, betteralgs);
232     fflush(stderr);
233
234     hin = GetStdHandle(STD_INPUT_HANDLE);
235     GetConsoleMode(hin, &savemode);
236     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
237                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
238     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
239     SetConsoleMode(hin, savemode);
240
241     if (line[0] == 'y' || line[0] == 'Y') {
242         return 1;
243     } else {
244         fprintf(stderr, abandoned);
245         return 0;
246     }
247 }
248
249 /*
250  * Ask whether to wipe a session log file before writing to it.
251  * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
252  */
253 int askappend(void *frontend, Filename *filename,
254               void (*callback)(void *ctx, int result), void *ctx)
255 {
256     HANDLE hin;
257     DWORD savemode, i;
258
259     static const char msgtemplate[] =
260         "The session log file \"%.*s\" already exists.\n"
261         "You can overwrite it with a new session log,\n"
262         "append your session log to the end of it,\n"
263         "or disable session logging for this session.\n"
264         "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
265         "or just press Return to disable logging.\n"
266         "Wipe the log file? (y/n, Return cancels logging) ";
267
268     static const char msgtemplate_batch[] =
269         "The session log file \"%.*s\" already exists.\n"
270         "Logging will not be enabled.\n";
271
272     char line[32];
273
274     if (console_batch_mode) {
275         fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename->path);
276         fflush(stderr);
277         return 0;
278     }
279     fprintf(stderr, msgtemplate, FILENAME_MAX, filename->path);
280     fflush(stderr);
281
282     hin = GetStdHandle(STD_INPUT_HANDLE);
283     GetConsoleMode(hin, &savemode);
284     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
285                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
286     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
287     SetConsoleMode(hin, savemode);
288
289     if (line[0] == 'y' || line[0] == 'Y')
290         return 2;
291     else if (line[0] == 'n' || line[0] == 'N')
292         return 1;
293     else
294         return 0;
295 }
296
297 /*
298  * Warn about the obsolescent key file format.
299  * 
300  * Uniquely among these functions, this one does _not_ expect a
301  * frontend handle. This means that if PuTTY is ported to a
302  * platform which requires frontend handles, this function will be
303  * an anomaly. Fortunately, the problem it addresses will not have
304  * been present on that platform, so it can plausibly be
305  * implemented as an empty function.
306  */
307 void old_keyfile_warning(void)
308 {
309     static const char message[] =
310         "You are loading an SSH-2 private key which has an\n"
311         "old version of the file format. This means your key\n"
312         "file is not fully tamperproof. Future versions of\n"
313         "PuTTY may stop supporting this private key format,\n"
314         "so we recommend you convert your key to the new\n"
315         "format.\n"
316         "\n"
317         "Once the key is loaded into PuTTYgen, you can perform\n"
318         "this conversion simply by saving it again.\n";
319
320     fputs(message, stderr);
321 }
322
323 /*
324  * Display the fingerprints of the PGP Master Keys to the user.
325  */
326 void pgp_fingerprints(void)
327 {
328     fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
329           "be used to establish a trust path from this executable to another\n"
330           "one. See the manual for more information.\n"
331           "(Note: these fingerprints have nothing to do with SSH!)\n"
332           "\n"
333           "PuTTY Master Key as of 2015 (RSA, 4096-bit):\n"
334           "  " PGP_MASTER_KEY_FP "\n\n"
335           "Original PuTTY Master Key (RSA, 1024-bit):\n"
336           "  " PGP_RSA_MASTER_KEY_FP "\n"
337           "Original PuTTY Master Key (DSA, 1024-bit):\n"
338           "  " PGP_DSA_MASTER_KEY_FP "\n", stdout);
339 }
340
341 void console_provide_logctx(void *logctx)
342 {
343     console_logctx = logctx;
344 }
345
346 void logevent(void *frontend, const char *string)
347 {
348     log_eventlog(console_logctx, string);
349 }
350
351 static void console_data_untrusted(HANDLE hout, const char *data, int len)
352 {
353     DWORD dummy;
354     /* FIXME: control-character filtering */
355     WriteFile(hout, data, len, &dummy, NULL);
356 }
357
358 int console_get_userpass_input(prompts_t *p,
359                                const unsigned char *in, int inlen)
360 {
361     HANDLE hin, hout;
362     size_t curr_prompt;
363
364     /*
365      * Zero all the results, in case we abort half-way through.
366      */
367     {
368         int i;
369         for (i = 0; i < (int)p->n_prompts; i++)
370             prompt_set_result(p->prompts[i], "");
371     }
372
373     /*
374      * The prompts_t might contain a message to be displayed but no
375      * actual prompt. More usually, though, it will contain
376      * questions that the user needs to answer, in which case we
377      * need to ensure that we're able to get the answers.
378      */
379     if (p->n_prompts) {
380         if (console_batch_mode)
381             return 0;
382         hin = GetStdHandle(STD_INPUT_HANDLE);
383         if (hin == INVALID_HANDLE_VALUE) {
384             fprintf(stderr, "Cannot get standard input handle\n");
385             cleanup_exit(1);
386         }
387     }
388
389     /*
390      * And if we have anything to print, we need standard output.
391      */
392     if ((p->name_reqd && p->name) || p->instruction || p->n_prompts) {
393         hout = GetStdHandle(STD_OUTPUT_HANDLE);
394         if (hout == INVALID_HANDLE_VALUE) {
395             fprintf(stderr, "Cannot get standard output handle\n");
396             cleanup_exit(1);
397         }
398     }
399
400     /*
401      * Preamble.
402      */
403     /* We only print the `name' caption if we have to... */
404     if (p->name_reqd && p->name) {
405         size_t l = strlen(p->name);
406         console_data_untrusted(hout, p->name, l);
407         if (p->name[l-1] != '\n')
408             console_data_untrusted(hout, "\n", 1);
409     }
410     /* ...but we always print any `instruction'. */
411     if (p->instruction) {
412         size_t l = strlen(p->instruction);
413         console_data_untrusted(hout, p->instruction, l);
414         if (p->instruction[l-1] != '\n')
415             console_data_untrusted(hout, "\n", 1);
416     }
417
418     for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {
419
420         DWORD savemode, newmode;
421         int len;
422         prompt_t *pr = p->prompts[curr_prompt];
423
424         GetConsoleMode(hin, &savemode);
425         newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
426         if (!pr->echo)
427             newmode &= ~ENABLE_ECHO_INPUT;
428         else
429             newmode |= ENABLE_ECHO_INPUT;
430         SetConsoleMode(hin, newmode);
431
432         console_data_untrusted(hout, pr->prompt, strlen(pr->prompt));
433
434         len = 0;
435         while (1) {
436             DWORD ret = 0;
437             BOOL r;
438
439             prompt_ensure_result_size(pr, len * 5 / 4 + 512);
440
441             r = ReadFile(hin, pr->result + len, pr->resultsize - len - 1,
442                          &ret, NULL);
443
444             if (!r || ret == 0) {
445                 len = -1;
446                 break;
447             }
448             len += ret;
449             if (pr->result[len - 1] == '\n') {
450                 len--;
451                 if (pr->result[len - 1] == '\r')
452                     len--;
453                 break;
454             }
455         }
456
457         SetConsoleMode(hin, savemode);
458
459         if (!pr->echo) {
460             DWORD dummy;
461             WriteFile(hout, "\r\n", 2, &dummy, NULL);
462         }
463
464         if (len < 0) {
465             return 0;                  /* failure due to read error */
466         }
467
468         pr->result[len] = '\0';
469     }
470
471     return 1; /* success */
472 }
473
474 void frontend_keypress(void *handle)
475 {
476     /*
477      * This is nothing but a stub, in console code.
478      */
479     return;
480 }