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