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)
29 if (cfg.protocol == PROT_SSH) {
39 void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
40 char *keystr, char *fingerprint)
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"
50 "The server's key fingerprint is:\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"
57 "The server's key fingerprint is:\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"
65 "Store key in cache? (y/n) ";
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"
74 "The new key fingerprint is:\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"
84 "The new key fingerprint is:\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"
93 "Update cached key? (y/n, Return cancels connection) ";
95 static const char abandoned[] = "Connection abandoned.\n";
100 * Verify the key against the registry.
102 ret = verify_host_key(host, port, keytype, keystr);
104 if (ret == 0) /* success - key matched OK */
107 if (ret == 2) { /* key was different */
108 if (console_batch_mode) {
109 fprintf(stderr, wrongmsg_batch, fingerprint);
112 fprintf(stderr, wrongmsg, fingerprint);
115 if (ret == 1) { /* key was absent */
116 if (console_batch_mode) {
117 fprintf(stderr, absentmsg_batch, fingerprint);
120 fprintf(stderr, absentmsg, fingerprint);
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);
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);
135 fprintf(stderr, abandoned);
141 * Ask whether the selected cipher is acceptable (since it was
142 * below the configured 'warn' threshold).
143 * cs: 0 = both ways, 1 = client->server, 2 = server->client
145 void askcipher(void *frontend, char *ciphername, int cs)
150 static const char msg[] =
151 "The first %scipher supported by the server is\n"
152 "%s, which is below the configured warning threshold.\n"
153 "Continue with connection? (y/n) ";
154 static const char msg_batch[] =
155 "The first %scipher supported by the server is\n"
156 "%s, which is below the configured warning threshold.\n"
157 "Connection abandoned.\n";
158 static const char abandoned[] = "Connection abandoned.\n";
162 if (console_batch_mode) {
163 fprintf(stderr, msg_batch,
165 (cs == 1) ? "client-to-server " : "server-to-client ",
172 (cs == 1) ? "client-to-server " : "server-to-client ",
176 hin = GetStdHandle(STD_INPUT_HANDLE);
177 GetConsoleMode(hin, &savemode);
178 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
179 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
180 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
181 SetConsoleMode(hin, savemode);
183 if (line[0] == 'y' || line[0] == 'Y') {
186 fprintf(stderr, abandoned);
192 * Ask whether to wipe a session log file before writing to it.
193 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
195 int askappend(void *frontend, char *filename)
200 static const char msgtemplate[] =
201 "The session log file \"%.*s\" already exists.\n"
202 "You can overwrite it with a new session log,\n"
203 "append your session log to the end of it,\n"
204 "or disable session logging for this session.\n"
205 "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
206 "or just press Return to disable logging.\n"
207 "Wipe the log file? (y/n, Return cancels logging) ";
209 static const char msgtemplate_batch[] =
210 "The session log file \"%.*s\" already exists.\n"
211 "Logging will not be enabled.\n";
215 if (cfg.logxfovr != LGXF_ASK) {
216 return ((cfg.logxfovr == LGXF_OVR) ? 2 : 1);
218 if (console_batch_mode) {
219 fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename);
223 fprintf(stderr, msgtemplate, FILENAME_MAX, filename);
226 hin = GetStdHandle(STD_INPUT_HANDLE);
227 GetConsoleMode(hin, &savemode);
228 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
229 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
230 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
231 SetConsoleMode(hin, savemode);
233 if (line[0] == 'y' || line[0] == 'Y')
235 else if (line[0] == 'n' || line[0] == 'N')
242 * Warn about the obsolescent key file format.
244 * Uniquely among these functions, this one does _not_ expect a
245 * frontend handle. This means that if PuTTY is ported to a
246 * platform which requires frontend handles, this function will be
247 * an anomaly. Fortunately, the problem it addresses will not have
248 * been present on that platform, so it can plausibly be
249 * implemented as an empty function.
251 void old_keyfile_warning(void)
253 static const char message[] =
254 "You are loading an SSH 2 private key which has an\n"
255 "old version of the file format. This means your key\n"
256 "file is not fully tamperproof. Future versions of\n"
257 "PuTTY may stop supporting this private key format,\n"
258 "so we recommend you convert your key to the new\n"
261 "Once the key is loaded into PuTTYgen, you can perform\n"
262 "this conversion simply by saving it again.\n";
264 fputs(message, stderr);
267 void logevent(void *frontend, char *string)
271 int console_get_line(const char *prompt, char *str,
272 int maxlen, int is_pw)
275 DWORD savemode, newmode, i;
277 if (console_batch_mode) {
281 hin = GetStdHandle(STD_INPUT_HANDLE);
282 hout = GetStdHandle(STD_OUTPUT_HANDLE);
283 if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) {
284 fprintf(stderr, "Cannot get standard input/output handles\n");
288 GetConsoleMode(hin, &savemode);
289 newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
291 newmode &= ~ENABLE_ECHO_INPUT;
293 newmode |= ENABLE_ECHO_INPUT;
294 SetConsoleMode(hin, newmode);
296 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
297 ReadFile(hin, str, maxlen - 1, &i, NULL);
299 SetConsoleMode(hin, savemode);
301 if ((int) i > maxlen)
308 WriteFile(hout, "\r\n", 2, &i, NULL);
314 void frontend_keypress(void *handle)
317 * This is nothing but a stub, in console code.