TortoiseGitPLink: Correctly report fingerprint to users
[TortoiseGit.git] / src / TortoisePlink / Windows / WINCONS.C
blobee96bbf539aee21b4518e61058817b0b0977762f
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
14 #include "LoginDialog.h"\r
16 int console_batch_mode = FALSE;\r
18 static void *console_logctx = NULL;\r
20 /*\r
21  * Clean up and exit.\r
22  */\r
23 void cleanup_exit(int code)\r
24 {\r
25     /*\r
26      * Clean up.\r
27      */\r
28     sk_cleanup();\r
30     random_save_seed();\r
31 #ifdef MSCRYPTOAPI\r
32     crypto_wrapup();\r
33 #endif\r
35     exit(code);\r
36 }\r
38 void set_busy_status(void *frontend, int status)\r
39 {\r
40 }\r
42 void notify_remote_exit(void *frontend)\r
43 {\r
44 }\r
46 void timer_change_notify(long next)\r
47 {\r
48 }\r
50 int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,\r
51                         char *keystr, char *fingerprint,\r
52                         void (*callback)(void *ctx, int result), void *ctx)\r
53 {\r
54     int ret;\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
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
105     static const char abandoned[] = "Connection abandoned.\n";\r
107         static const char mbtitle[] = "%s Security Alert";\r
109     /*\r
110      * Verify the key against the registry.\r
111      */\r
112     ret = verify_host_key(host, port, keytype, keystr);\r
114     if (ret == 0)                      /* success - key matched OK */\r
115         return 1;\r
117     if (ret == 2) {                    /* key was different */\r
118         int mbret;\r
119         char *message, *title;\r
121         message = dupprintf(wrongmsg, keytype, fingerprint, appname);\r
122         title = dupprintf(mbtitle, appname);\r
124         mbret = MessageBox(GetParentHwnd(), message, title, MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON3);\r
125         sfree(message);\r
126         sfree(title);\r
127         if (mbret == IDYES) {\r
128                 store_host_key(host, port, keytype, keystr);\r
129                 return 1;\r
130         }\r
131         else if (mbret == IDNO) \r
132         {\r
133                 return 1;\r
134         }\r
135         else\r
136                 return 0;\r
137         }\r
139     if (ret == 1) {                    /* key was absent */\r
140         int mbret;\r
141         char *message, *title;\r
142         message = dupprintf(absentmsg, keytype, fingerprint, appname);\r
143         title = dupprintf(mbtitle, appname);\r
144         mbret = MessageBox(GetParentHwnd(), message, title,\r
145                 MB_ICONWARNING | MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON3);\r
146         sfree(message);\r
147         sfree(title);\r
148         if (mbret == IDYES)\r
149         {\r
150                 store_host_key(host, port, keytype, keystr);\r
151                 return 1;\r
152         }\r
153         else if (mbret == IDNO)\r
154         {\r
155                 return 1;\r
156         }\r
157         else\r
158                 return 0;\r
159     }\r
160         return 1;\r
163 void update_specials_menu(void *frontend)\r
167 /*\r
168  * Ask whether the selected algorithm is acceptable (since it was\r
169  * below the configured 'warn' threshold).\r
170  */\r
171 int askalg(void *frontend, const char *algtype, const char *algname,\r
172            void (*callback)(void *ctx, int result), void *ctx)\r
174     static const char msg[] =\r
175         "The first %s supported by the server is\n"\r
176         "%s, which is below the configured warning threshold.\n"\r
177         "Continue with connection? (y/n) ";\r
179         static const char mbtitle[] = "%s Security Alert";\r
181         int mbret;\r
182         char *message, *title;\r
184         message = dupprintf(msg, algtype, algname);\r
185         title = dupprintf(mbtitle, appname);\r
187         mbret = MessageBox(GetParentHwnd(), message, title, MB_ICONWARNING|MB_YESNO);\r
188         sfree(message);\r
189         sfree(title);\r
190         if (mbret == IDYES)\r
191                 return 1;\r
192         else\r
193                 return 0;\r
196 /*\r
197  * Ask whether to wipe a session log file before writing to it.\r
198  * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).\r
199  */\r
200 int askappend(void *frontend, Filename filename,\r
201               void (*callback)(void *ctx, int result), void *ctx)\r
203     HANDLE hin;\r
204     DWORD savemode, i;\r
206     static const char msgtemplate[] =\r
207         "The session log file \"%.*s\" already exists.\n"\r
208         "You can overwrite it with a new session log,\n"\r
209         "append your session log to the end of it,\n"\r
210         "or disable session logging for this session.\n"\r
211         "Enter \"y\" to wipe the file, \"n\" to append to it,\n"\r
212         "or just press Return to disable logging.\n"\r
213         "Wipe the log file? (y/n, Return cancels logging) ";\r
215     static const char msgtemplate_batch[] =\r
216         "The session log file \"%.*s\" already exists.\n"\r
217         "Logging will not be enabled.\n";\r
219     char line[32];\r
221     if (console_batch_mode) {\r
222         fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path);\r
223         fflush(stderr);\r
224         return 0;\r
225     }\r
226     fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path);\r
227     fflush(stderr);\r
229     hin = GetStdHandle(STD_INPUT_HANDLE);\r
230     GetConsoleMode(hin, &savemode);\r
231     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |\r
232                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));\r
233     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);\r
234     SetConsoleMode(hin, savemode);\r
236     if (line[0] == 'y' || line[0] == 'Y')\r
237         return 2;\r
238     else if (line[0] == 'n' || line[0] == 'N')\r
239         return 1;\r
240     else\r
241         return 0;\r
244 /*\r
245  * Warn about the obsolescent key file format.\r
246  * \r
247  * Uniquely among these functions, this one does _not_ expect a\r
248  * frontend handle. This means that if PuTTY is ported to a\r
249  * platform which requires frontend handles, this function will be\r
250  * an anomaly. Fortunately, the problem it addresses will not have\r
251  * been present on that platform, so it can plausibly be\r
252  * implemented as an empty function.\r
253  */\r
254 void old_keyfile_warning(void)\r
256     static const char message[] =\r
257         "You are loading an SSH-2 private key which has an\n"\r
258         "old version of the file format. This means your key\n"\r
259         "file is not fully tamperproof. Future versions of\n"\r
260         "PuTTY may stop supporting this private key format,\n"\r
261         "so we recommend you convert your key to the new\n"\r
262         "format.\n"\r
263         "\n"\r
264         "Once the key is loaded into PuTTYgen, you can perform\n"\r
265         "this conversion simply by saving it again.\n";\r
267     fputs(message, stderr);\r
270 /*\r
271  * Display the fingerprints of the PGP Master Keys to the user.\r
272  */\r
273 void pgp_fingerprints(void)\r
275     fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"\r
276           "be used to establish a trust path from this executable to another\n"\r
277           "one. See the manual for more information.\n"\r
278           "(Note: these fingerprints have nothing to do with SSH!)\n"\r
279           "\n"\r
280           "PuTTY Master Key (RSA), 1024-bit:\n"\r
281           "  " PGP_RSA_MASTER_KEY_FP "\n"\r
282           "PuTTY Master Key (DSA), 1024-bit:\n"\r
283           "  " PGP_DSA_MASTER_KEY_FP "\n", stdout);\r
286 void console_provide_logctx(void *logctx)\r
288     console_logctx = logctx;\r
291 void logevent(void *frontend, const char *string)\r
293     log_eventlog(console_logctx, string);\r
296 static void console_data_untrusted(HANDLE hout, const char *data, int len)\r
298     DWORD dummy;\r
299     /* FIXME: control-character filtering */\r
300     WriteFile(hout, data, len, &dummy, NULL);\r
303 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)\r
305     size_t curr_prompt;\r
307     /*\r
308      * Zero all the results, in case we abort half-way through.\r
309      */\r
310     {\r
311         int i;\r
312         for (i = 0; i < (int)p->n_prompts; i++)\r
313             memset(p->prompts[i]->result, 0, p->prompts[i]->result_len);\r
314     }\r
316     if (console_batch_mode)\r
317         return 0;\r
320     for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {\r
321                 \r
322         prompt_t *pr = p->prompts[curr_prompt];\r
323         if (!DoLoginDialog(pr->result, pr->result_len-1, pr->prompt))\r
324         return 0;\r
325     }\r
327     return 1; /* success */\r
331 void frontend_keypress(void *handle)\r
333     /*\r
334      * This is nothing but a stub, in console code.\r
335      */\r
336     return;\r