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