OSDN Git Service

Enhance all settings encryption.
[ffftp/ffftp.git] / contrib / putty / WINDOWS / WINCONS.C
1 /*\r
2  * wincons.c - various interactive-prompt routines shared between\r
3  * the Windows console PuTTY tools\r
4  */\r
5 \r
6 #include <stdio.h>\r
7 #include <stdlib.h>\r
8 #include <stdarg.h>\r
9 \r
10 #include "putty.h"\r
11 #include "storage.h"\r
12 #include "ssh.h"\r
13 \r
14 int console_batch_mode = FALSE;\r
15 \r
16 static void *console_logctx = NULL;\r
17 \r
18 /*\r
19  * Clean up and exit.\r
20  */\r
21 void cleanup_exit(int code)\r
22 {\r
23     /*\r
24      * Clean up.\r
25      */\r
26     sk_cleanup();\r
27 \r
28     random_save_seed();\r
29 #ifdef MSCRYPTOAPI\r
30     crypto_wrapup();\r
31 #endif\r
32 \r
33     exit(code);\r
34 }\r
35 \r
36 void set_busy_status(void *frontend, int status)\r
37 {\r
38 }\r
39 \r
40 void notify_remote_exit(void *frontend)\r
41 {\r
42 }\r
43 \r
44 void timer_change_notify(unsigned long next)\r
45 {\r
46 }\r
47 \r
48 int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,\r
49                         char *keystr, char *fingerprint,\r
50                         void (*callback)(void *ctx, int result), void *ctx)\r
51 {\r
52     int ret;\r
53     HANDLE hin;\r
54     DWORD savemode, i;\r
55 \r
56     static const char absentmsg_batch[] =\r
57         "The server's host key is not cached in the registry. You\n"\r
58         "have no guarantee that the server is the computer you\n"\r
59         "think it is.\n"\r
60         "The server's %s key fingerprint is:\n"\r
61         "%s\n"\r
62         "Connection abandoned.\n";\r
63     static const char absentmsg[] =\r
64         "The server's host key is not cached in the registry. You\n"\r
65         "have no guarantee that the server is the computer you\n"\r
66         "think it is.\n"\r
67         "The server's %s key fingerprint is:\n"\r
68         "%s\n"\r
69         "If you trust this host, enter \"y\" to add the key to\n"\r
70         "PuTTY's cache and carry on connecting.\n"\r
71         "If you want to carry on connecting just once, without\n"\r
72         "adding the key to the cache, enter \"n\".\n"\r
73         "If you do not trust this host, press Return to abandon the\n"\r
74         "connection.\n"\r
75         "Store key in cache? (y/n) ";\r
76 \r
77     static const char wrongmsg_batch[] =\r
78         "WARNING - POTENTIAL SECURITY BREACH!\n"\r
79         "The server's host key does not match the one PuTTY has\n"\r
80         "cached in the registry. This means that either the\n"\r
81         "server administrator has changed the host key, or you\n"\r
82         "have actually connected to another computer pretending\n"\r
83         "to be the server.\n"\r
84         "The new %s key fingerprint is:\n"\r
85         "%s\n"\r
86         "Connection abandoned.\n";\r
87     static const char wrongmsg[] =\r
88         "WARNING - POTENTIAL SECURITY BREACH!\n"\r
89         "The server's host key does not match the one PuTTY has\n"\r
90         "cached in the registry. This means that either the\n"\r
91         "server administrator has changed the host key, or you\n"\r
92         "have actually connected to another computer pretending\n"\r
93         "to be the server.\n"\r
94         "The new %s key fingerprint is:\n"\r
95         "%s\n"\r
96         "If you were expecting this change and trust the new key,\n"\r
97         "enter \"y\" to update PuTTY's cache and continue connecting.\n"\r
98         "If you want to carry on connecting but without updating\n"\r
99         "the cache, enter \"n\".\n"\r
100         "If you want to abandon the connection completely, press\n"\r
101         "Return to cancel. Pressing Return is the ONLY guaranteed\n"\r
102         "safe choice.\n"\r
103         "Update cached key? (y/n, Return cancels connection) ";\r
104 \r
105     static const char abandoned[] = "Connection abandoned.\n";\r
106 \r
107     char line[32];\r
108 \r
109     /*\r
110      * Verify the key against the registry.\r
111      */\r
112     ret = verify_host_key(host, port, keytype, keystr);\r
113 \r
114     if (ret == 0)                      /* success - key matched OK */\r
115         return 1;\r
116 \r
117     if (ret == 2) {                    /* key was different */\r
118         if (console_batch_mode) {\r
119             fprintf(stderr, wrongmsg_batch, keytype, fingerprint);\r
120             return 0;\r
121         }\r
122         fprintf(stderr, wrongmsg, keytype, fingerprint);\r
123         fflush(stderr);\r
124     }\r
125     if (ret == 1) {                    /* key was absent */\r
126         if (console_batch_mode) {\r
127             fprintf(stderr, absentmsg_batch, keytype, fingerprint);\r
128             return 0;\r
129         }\r
130         fprintf(stderr, absentmsg, keytype, fingerprint);\r
131         fflush(stderr);\r
132     }\r
133 \r
134     hin = GetStdHandle(STD_INPUT_HANDLE);\r
135     GetConsoleMode(hin, &savemode);\r
136     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |\r
137                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));\r
138     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);\r
139     SetConsoleMode(hin, savemode);\r
140 \r
141     if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {\r
142         if (line[0] == 'y' || line[0] == 'Y')\r
143             store_host_key(host, port, keytype, keystr);\r
144         return 1;\r
145     } else {\r
146         fprintf(stderr, abandoned);\r
147         return 0;\r
148     }\r
149 }\r
150 \r
151 void update_specials_menu(void *frontend)\r
152 {\r
153 }\r
154 \r
155 /*\r
156  * Ask whether the selected algorithm is acceptable (since it was\r
157  * below the configured 'warn' threshold).\r
158  */\r
159 int askalg(void *frontend, const char *algtype, const char *algname,\r
160            void (*callback)(void *ctx, int result), void *ctx)\r
161 {\r
162     HANDLE hin;\r
163     DWORD savemode, i;\r
164 \r
165     static const char msg[] =\r
166         "The first %s supported by the server is\n"\r
167         "%s, which is below the configured warning threshold.\n"\r
168         "Continue with connection? (y/n) ";\r
169     static const char msg_batch[] =\r
170         "The first %s supported by the server is\n"\r
171         "%s, which is below the configured warning threshold.\n"\r
172         "Connection abandoned.\n";\r
173     static const char abandoned[] = "Connection abandoned.\n";\r
174 \r
175     char line[32];\r
176 \r
177     if (console_batch_mode) {\r
178         fprintf(stderr, msg_batch, algtype, algname);\r
179         return 0;\r
180     }\r
181 \r
182     fprintf(stderr, msg, algtype, algname);\r
183     fflush(stderr);\r
184 \r
185     hin = GetStdHandle(STD_INPUT_HANDLE);\r
186     GetConsoleMode(hin, &savemode);\r
187     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |\r
188                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));\r
189     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);\r
190     SetConsoleMode(hin, savemode);\r
191 \r
192     if (line[0] == 'y' || line[0] == 'Y') {\r
193         return 1;\r
194     } else {\r
195         fprintf(stderr, abandoned);\r
196         return 0;\r
197     }\r
198 }\r
199 \r
200 /*\r
201  * Ask whether to wipe a session log file before writing to it.\r
202  * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).\r
203  */\r
204 int askappend(void *frontend, Filename *filename,\r
205               void (*callback)(void *ctx, int result), void *ctx)\r
206 {\r
207     HANDLE hin;\r
208     DWORD savemode, i;\r
209 \r
210     static const char msgtemplate[] =\r
211         "The session log file \"%.*s\" already exists.\n"\r
212         "You can overwrite it with a new session log,\n"\r
213         "append your session log to the end of it,\n"\r
214         "or disable session logging for this session.\n"\r
215         "Enter \"y\" to wipe the file, \"n\" to append to it,\n"\r
216         "or just press Return to disable logging.\n"\r
217         "Wipe the log file? (y/n, Return cancels logging) ";\r
218 \r
219     static const char msgtemplate_batch[] =\r
220         "The session log file \"%.*s\" already exists.\n"\r
221         "Logging will not be enabled.\n";\r
222 \r
223     char line[32];\r
224 \r
225     if (console_batch_mode) {\r
226         fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename->path);\r
227         fflush(stderr);\r
228         return 0;\r
229     }\r
230     fprintf(stderr, msgtemplate, FILENAME_MAX, filename->path);\r
231     fflush(stderr);\r
232 \r
233     hin = GetStdHandle(STD_INPUT_HANDLE);\r
234     GetConsoleMode(hin, &savemode);\r
235     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |\r
236                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));\r
237     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);\r
238     SetConsoleMode(hin, savemode);\r
239 \r
240     if (line[0] == 'y' || line[0] == 'Y')\r
241         return 2;\r
242     else if (line[0] == 'n' || line[0] == 'N')\r
243         return 1;\r
244     else\r
245         return 0;\r
246 }\r
247 \r
248 /*\r
249  * Warn about the obsolescent key file format.\r
250  * \r
251  * Uniquely among these functions, this one does _not_ expect a\r
252  * frontend handle. This means that if PuTTY is ported to a\r
253  * platform which requires frontend handles, this function will be\r
254  * an anomaly. Fortunately, the problem it addresses will not have\r
255  * been present on that platform, so it can plausibly be\r
256  * implemented as an empty function.\r
257  */\r
258 void old_keyfile_warning(void)\r
259 {\r
260     static const char message[] =\r
261         "You are loading an SSH-2 private key which has an\n"\r
262         "old version of the file format. This means your key\n"\r
263         "file is not fully tamperproof. Future versions of\n"\r
264         "PuTTY may stop supporting this private key format,\n"\r
265         "so we recommend you convert your key to the new\n"\r
266         "format.\n"\r
267         "\n"\r
268         "Once the key is loaded into PuTTYgen, you can perform\n"\r
269         "this conversion simply by saving it again.\n";\r
270 \r
271     fputs(message, stderr);\r
272 }\r
273 \r
274 /*\r
275  * Display the fingerprints of the PGP Master Keys to the user.\r
276  */\r
277 void pgp_fingerprints(void)\r
278 {\r
279     fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"\r
280           "be used to establish a trust path from this executable to another\n"\r
281           "one. See the manual for more information.\n"\r
282           "(Note: these fingerprints have nothing to do with SSH!)\n"\r
283           "\n"\r
284           "PuTTY Master Key (RSA), 1024-bit:\n"\r
285           "  " PGP_RSA_MASTER_KEY_FP "\n"\r
286           "PuTTY Master Key (DSA), 1024-bit:\n"\r
287           "  " PGP_DSA_MASTER_KEY_FP "\n", stdout);\r
288 }\r
289 \r
290 void console_provide_logctx(void *logctx)\r
291 {\r
292     console_logctx = logctx;\r
293 }\r
294 \r
295 void logevent(void *frontend, const char *string)\r
296 {\r
297     log_eventlog(console_logctx, string);\r
298 }\r
299 \r
300 static void console_data_untrusted(HANDLE hout, const char *data, int len)\r
301 {\r
302     DWORD dummy;\r
303     /* FIXME: control-character filtering */\r
304     WriteFile(hout, data, len, &dummy, NULL);\r
305 }\r
306 \r
307 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)\r
308 {\r
309     HANDLE hin, hout;\r
310     size_t curr_prompt;\r
311 \r
312     /*\r
313      * Zero all the results, in case we abort half-way through.\r
314      */\r
315     {\r
316         int i;\r
317         for (i = 0; i < (int)p->n_prompts; i++)\r
318             prompt_set_result(p->prompts[i], "");\r
319     }\r
320 \r
321     /*\r
322      * The prompts_t might contain a message to be displayed but no\r
323      * actual prompt. More usually, though, it will contain\r
324      * questions that the user needs to answer, in which case we\r
325      * need to ensure that we're able to get the answers.\r
326      */\r
327     if (p->n_prompts) {\r
328         if (console_batch_mode)\r
329             return 0;\r
330         hin = GetStdHandle(STD_INPUT_HANDLE);\r
331         if (hin == INVALID_HANDLE_VALUE) {\r
332             fprintf(stderr, "Cannot get standard input handle\n");\r
333             cleanup_exit(1);\r
334         }\r
335     }\r
336 \r
337     /*\r
338      * And if we have anything to print, we need standard output.\r
339      */\r
340     if ((p->name_reqd && p->name) || p->instruction || p->n_prompts) {\r
341         hout = GetStdHandle(STD_OUTPUT_HANDLE);\r
342         if (hout == INVALID_HANDLE_VALUE) {\r
343             fprintf(stderr, "Cannot get standard output handle\n");\r
344             cleanup_exit(1);\r
345         }\r
346     }\r
347 \r
348     /*\r
349      * Preamble.\r
350      */\r
351     /* We only print the `name' caption if we have to... */\r
352     if (p->name_reqd && p->name) {\r
353         size_t l = strlen(p->name);\r
354         console_data_untrusted(hout, p->name, l);\r
355         if (p->name[l-1] != '\n')\r
356             console_data_untrusted(hout, "\n", 1);\r
357     }\r
358     /* ...but we always print any `instruction'. */\r
359     if (p->instruction) {\r
360         size_t l = strlen(p->instruction);\r
361         console_data_untrusted(hout, p->instruction, l);\r
362         if (p->instruction[l-1] != '\n')\r
363             console_data_untrusted(hout, "\n", 1);\r
364     }\r
365 \r
366     for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {\r
367 \r
368         DWORD savemode, newmode;\r
369         int len;\r
370         prompt_t *pr = p->prompts[curr_prompt];\r
371 \r
372         GetConsoleMode(hin, &savemode);\r
373         newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;\r
374         if (!pr->echo)\r
375             newmode &= ~ENABLE_ECHO_INPUT;\r
376         else\r
377             newmode |= ENABLE_ECHO_INPUT;\r
378         SetConsoleMode(hin, newmode);\r
379 \r
380         console_data_untrusted(hout, pr->prompt, strlen(pr->prompt));\r
381 \r
382         len = 0;\r
383         while (1) {\r
384             DWORD ret = 0;\r
385             BOOL r;\r
386 \r
387             prompt_ensure_result_size(pr, len * 5 / 4 + 512);\r
388 \r
389             r = ReadFile(hin, pr->result + len, pr->resultsize - len - 1,\r
390                          &ret, NULL);\r
391 \r
392             if (!r || ret == 0) {\r
393                 len = -1;\r
394                 break;\r
395             }\r
396             len += ret;\r
397             if (pr->result[len - 1] == '\n') {\r
398                 len--;\r
399                 if (pr->result[len - 1] == '\r')\r
400                     len--;\r
401                 break;\r
402             }\r
403         }\r
404 \r
405         SetConsoleMode(hin, savemode);\r
406 \r
407         if (!pr->echo) {\r
408             DWORD dummy;\r
409             WriteFile(hout, "\r\n", 2, &dummy, NULL);\r
410         }\r
411 \r
412         if (len < 0) {\r
413             return 0;                  /* failure due to read error */\r
414         }\r
415 \r
416         pr->result[len] = '\0';\r
417     }\r
418 \r
419     return 1; /* success */\r
420 }\r
421 \r
422 void frontend_keypress(void *handle)\r
423 {\r
424     /*\r
425      * This is nothing but a stub, in console code.\r
426      */\r
427     return;\r
428 }\r