2 * console.c: various interactive-prompt routines shared between
3 * the Windows console PuTTY tools
14 int console_batch_mode = FALSE;
16 static void *console_logctx = NULL;
21 void cleanup_exit(int code)
36 void set_busy_status(void *frontend, int status)
40 void notify_remote_exit(void *frontend)
44 void timer_change_notify(long next)
48 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
49 char *keystr, char *fingerprint)
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"
59 "The server's %s key fingerprint is:\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"
66 "The server's %s key fingerprint is:\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"
74 "Store key in cache? (y/n) ";
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"
83 "The new %s key fingerprint is:\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"
93 "The new %s key fingerprint is:\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"
102 "Update cached key? (y/n, Return cancels connection) ";
104 static const char abandoned[] = "Connection abandoned.\n";
109 * Verify the key against the registry.
111 ret = verify_host_key(host, port, keytype, keystr);
113 if (ret == 0) /* success - key matched OK */
116 if (ret == 2) { /* key was different */
117 if (console_batch_mode) {
118 fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
121 fprintf(stderr, wrongmsg, keytype, fingerprint);
124 if (ret == 1) { /* key was absent */
125 if (console_batch_mode) {
126 fprintf(stderr, absentmsg_batch, keytype, fingerprint);
129 fprintf(stderr, absentmsg, keytype, fingerprint);
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);
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);
144 fprintf(stderr, abandoned);
149 void update_specials_menu(void *frontend)
154 * Ask whether the selected algorithm is acceptable (since it was
155 * below the configured 'warn' threshold).
157 void askalg(void *frontend, const char *algtype, const char *algname)
162 static const char msg[] =
163 "The first %s supported by the server is\n"
164 "%s, which is below the configured warning threshold.\n"
165 "Continue with connection? (y/n) ";
166 static const char msg_batch[] =
167 "The first %s supported by the server is\n"
168 "%s, which is below the configured warning threshold.\n"
169 "Connection abandoned.\n";
170 static const char abandoned[] = "Connection abandoned.\n";
174 if (console_batch_mode) {
175 fprintf(stderr, msg_batch, algtype, algname);
179 fprintf(stderr, msg, algtype, algname);
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);
189 if (line[0] == 'y' || line[0] == 'Y') {
192 fprintf(stderr, abandoned);
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).
201 int askappend(void *frontend, Filename filename)
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) ";
215 static const char msgtemplate_batch[] =
216 "The session log file \"%.*s\" already exists.\n"
217 "Logging will not be enabled.\n";
221 if (console_batch_mode) {
222 fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path);
226 fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path);
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);
236 if (line[0] == 'y' || line[0] == 'Y')
238 else if (line[0] == 'n' || line[0] == 'N')
245 * Warn about the obsolescent key file format.
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.
254 void old_keyfile_warning(void)
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"
264 "Once the key is loaded into PuTTYgen, you can perform\n"
265 "this conversion simply by saving it again.\n";
267 fputs(message, stderr);
270 void console_provide_logctx(void *logctx)
272 console_logctx = logctx;
275 void logevent(void *frontend, const char *string)
278 log_eventlog(console_logctx, string);
281 int console_get_line(const char *prompt, char *str,
282 int maxlen, int is_pw)
285 DWORD savemode, newmode, i;
287 if (console_batch_mode) {
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");
299 GetConsoleMode(hin, &savemode);
300 newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
302 newmode &= ~ENABLE_ECHO_INPUT;
304 newmode |= ENABLE_ECHO_INPUT;
305 SetConsoleMode(hin, newmode);
307 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
308 ReadFile(hin, str, maxlen - 1, &i, NULL);
310 SetConsoleMode(hin, savemode);
312 if ((int) i > maxlen)
319 WriteFile(hout, "\r\n", 2, &i, NULL);
325 void frontend_keypress(void *handle)
328 * This is nothing but a stub, in console code.