2 * console.c: various interactive-prompt routines shared between
3 * the console PuTTY tools
16 int console_batch_mode = FALSE;
21 void cleanup_exit(int code)
37 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
38 char *keystr, char *fingerprint)
44 static const char absentmsg_batch[] =
45 "The server's host key is not cached in the registry. You\n"
46 "have no guarantee that the server is the computer you\n"
48 "The server's key fingerprint is:\n"
50 "Connection abandoned.\n";
51 static const char absentmsg[] =
52 "The server's host key is not cached in the registry. You\n"
53 "have no guarantee that the server is the computer you\n"
55 "The server's key fingerprint is:\n"
57 "If you trust this host, enter \"y\" to add the key to\n"
58 "PuTTY's cache and carry on connecting.\n"
59 "If you want to carry on connecting just once, without\n"
60 "adding the key to the cache, enter \"n\".\n"
61 "If you do not trust this host, press Return to abandon the\n"
63 "Store key in cache? (y/n) ";
65 static const char wrongmsg_batch[] =
66 "WARNING - POTENTIAL SECURITY BREACH!\n"
67 "The server's host key does not match the one PuTTY has\n"
68 "cached in the registry. This means that either the\n"
69 "server administrator has changed the host key, or you\n"
70 "have actually connected to another computer pretending\n"
72 "The new key fingerprint is:\n"
74 "Connection abandoned.\n";
75 static const char wrongmsg[] =
76 "WARNING - POTENTIAL SECURITY BREACH!\n"
77 "The server's host key does not match the one PuTTY has\n"
78 "cached in the registry. This means that either the\n"
79 "server administrator has changed the host key, or you\n"
80 "have actually connected to another computer pretending\n"
82 "The new key fingerprint is:\n"
84 "If you were expecting this change and trust the new key,\n"
85 "enter \"y\" to update PuTTY's cache and continue connecting.\n"
86 "If you want to carry on connecting but without updating\n"
87 "the cache, enter \"n\".\n"
88 "If you want to abandon the connection completely, press\n"
89 "Return to cancel. Pressing Return is the ONLY guaranteed\n"
91 "Update cached key? (y/n, Return cancels connection) ";
93 static const char abandoned[] = "Connection abandoned.\n";
98 * Verify the key against the registry.
100 ret = verify_host_key(host, port, keytype, keystr);
102 if (ret == 0) /* success - key matched OK */
105 if (ret == 2) { /* key was different */
106 if (console_batch_mode) {
107 fprintf(stderr, wrongmsg_batch, fingerprint);
110 fprintf(stderr, wrongmsg, fingerprint);
113 if (ret == 1) { /* key was absent */
114 if (console_batch_mode) {
115 fprintf(stderr, absentmsg_batch, fingerprint);
118 fprintf(stderr, absentmsg, fingerprint);
122 hin = GetStdHandle(STD_INPUT_HANDLE);
123 GetConsoleMode(hin, &savemode);
124 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
125 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
126 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
127 SetConsoleMode(hin, savemode);
129 if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
130 if (line[0] == 'y' || line[0] == 'Y')
131 store_host_key(host, port, keytype, keystr);
133 fprintf(stderr, abandoned);
139 * Ask whether the selected cipher is acceptable (since it was
140 * below the configured 'warn' threshold).
141 * cs: 0 = both ways, 1 = client->server, 2 = server->client
143 void askcipher(void *frontend, char *ciphername, int cs)
148 static const char msg[] =
149 "The first %scipher supported by the server is\n"
150 "%s, which is below the configured warning threshold.\n"
151 "Continue with connection? (y/n) ";
152 static const char msg_batch[] =
153 "The first %scipher supported by the server is\n"
154 "%s, which is below the configured warning threshold.\n"
155 "Connection abandoned.\n";
156 static const char abandoned[] = "Connection abandoned.\n";
160 if (console_batch_mode) {
161 fprintf(stderr, msg_batch,
163 (cs == 1) ? "client-to-server " : "server-to-client ",
170 (cs == 1) ? "client-to-server " : "server-to-client ",
174 hin = GetStdHandle(STD_INPUT_HANDLE);
175 GetConsoleMode(hin, &savemode);
176 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
177 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
178 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
179 SetConsoleMode(hin, savemode);
181 if (line[0] == 'y' || line[0] == 'Y') {
184 fprintf(stderr, abandoned);
190 * Ask whether to wipe a session log file before writing to it.
191 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
193 int askappend(void *frontend, char *filename)
198 static const char msgtemplate[] =
199 "The session log file \"%.*s\" already exists.\n"
200 "You can overwrite it with a new session log,\n"
201 "append your session log to the end of it,\n"
202 "or disable session logging for this session.\n"
203 "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
204 "or just press Return to disable logging.\n"
205 "Wipe the log file? (y/n, Return cancels logging) ";
207 static const char msgtemplate_batch[] =
208 "The session log file \"%.*s\" already exists.\n"
209 "Logging will not be enabled.\n";
213 if (console_batch_mode) {
214 fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename);
218 fprintf(stderr, msgtemplate, FILENAME_MAX, filename);
221 hin = GetStdHandle(STD_INPUT_HANDLE);
222 GetConsoleMode(hin, &savemode);
223 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
224 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
225 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
226 SetConsoleMode(hin, savemode);
228 if (line[0] == 'y' || line[0] == 'Y')
230 else if (line[0] == 'n' || line[0] == 'N')
237 * Warn about the obsolescent key file format.
239 * Uniquely among these functions, this one does _not_ expect a
240 * frontend handle. This means that if PuTTY is ported to a
241 * platform which requires frontend handles, this function will be
242 * an anomaly. Fortunately, the problem it addresses will not have
243 * been present on that platform, so it can plausibly be
244 * implemented as an empty function.
246 void old_keyfile_warning(void)
248 static const char message[] =
249 "You are loading an SSH 2 private key which has an\n"
250 "old version of the file format. This means your key\n"
251 "file is not fully tamperproof. Future versions of\n"
252 "PuTTY may stop supporting this private key format,\n"
253 "so we recommend you convert your key to the new\n"
256 "Once the key is loaded into PuTTYgen, you can perform\n"
257 "this conversion simply by saving it again.\n";
259 fputs(message, stderr);
262 void logevent(void *frontend, char *string)
266 int console_get_line(const char *prompt, char *str,
267 int maxlen, int is_pw)
270 DWORD savemode, newmode, i;
272 if (console_batch_mode) {
276 hin = GetStdHandle(STD_INPUT_HANDLE);
277 hout = GetStdHandle(STD_OUTPUT_HANDLE);
278 if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) {
279 fprintf(stderr, "Cannot get standard input/output handles\n");
283 GetConsoleMode(hin, &savemode);
284 newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
286 newmode &= ~ENABLE_ECHO_INPUT;
288 newmode |= ENABLE_ECHO_INPUT;
289 SetConsoleMode(hin, newmode);
291 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
292 ReadFile(hin, str, maxlen - 1, &i, NULL);
294 SetConsoleMode(hin, savemode);
296 if ((int) i > maxlen)
303 WriteFile(hout, "\r\n", 2, &i, NULL);
309 void frontend_keypress(void *handle)
312 * This is nothing but a stub, in console code.