2 * uxcons.c: various interactive-prompt routines shared between the
3 * Unix console PuTTY tools
17 int console_batch_mode = FALSE;
19 static void *console_logctx = NULL;
21 static struct termios orig_termios_stderr;
22 static int stderr_is_a_tty;
24 void stderr_tty_init()
26 /* Ensure that if stderr is a tty, we can get it back to a sane state. */
27 if ((flags & FLAG_STDERR_TTY) && isatty(STDERR_FILENO)) {
28 stderr_is_a_tty = TRUE;
29 tcgetattr(STDERR_FILENO, &orig_termios_stderr);
33 void premsg(struct termios *cf)
35 if (stderr_is_a_tty) {
36 tcgetattr(STDERR_FILENO, cf);
37 tcsetattr(STDERR_FILENO, TCSADRAIN, &orig_termios_stderr);
40 void postmsg(struct termios *cf)
43 tcsetattr(STDERR_FILENO, TCSADRAIN, cf);
49 void cleanup_exit(int code)
59 void set_busy_status(void *frontend, int status)
63 void update_specials_menu(void *frontend)
67 void notify_remote_exit(void *frontend)
71 void timer_change_notify(long next)
75 int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
76 char *keystr, char *fingerprint,
77 void (*callback)(void *ctx, int result), void *ctx)
81 static const char absentmsg_batch[] =
82 "The server's host key is not cached. You have no guarantee\n"
83 "that the server is the computer you think it is.\n"
84 "The server's %s key fingerprint is:\n"
86 "Connection abandoned.\n";
87 static const char absentmsg[] =
88 "The server's host key is not cached. You have no guarantee\n"
89 "that the server is the computer you think it is.\n"
90 "The server's %s key fingerprint is:\n"
92 "If you trust this host, enter \"y\" to add the key to\n"
93 "PuTTY's cache and carry on connecting.\n"
94 "If you want to carry on connecting just once, without\n"
95 "adding the key to the cache, enter \"n\".\n"
96 "If you do not trust this host, press Return to abandon the\n"
98 "Store key in cache? (y/n) ";
100 static const char wrongmsg_batch[] =
101 "WARNING - POTENTIAL SECURITY BREACH!\n"
102 "The server's host key does not match the one PuTTY has\n"
103 "cached. This means that either the server administrator\n"
104 "has changed the host key, or you have actually connected\n"
105 "to another computer pretending to be the server.\n"
106 "The new %s key fingerprint is:\n"
108 "Connection abandoned.\n";
109 static const char wrongmsg[] =
110 "WARNING - POTENTIAL SECURITY BREACH!\n"
111 "The server's host key does not match the one PuTTY has\n"
112 "cached. This means that either the server administrator\n"
113 "has changed the host key, or you have actually connected\n"
114 "to another computer pretending to be the server.\n"
115 "The new %s key fingerprint is:\n"
117 "If you were expecting this change and trust the new key,\n"
118 "enter \"y\" to update PuTTY's cache and continue connecting.\n"
119 "If you want to carry on connecting but without updating\n"
120 "the cache, enter \"n\".\n"
121 "If you want to abandon the connection completely, press\n"
122 "Return to cancel. Pressing Return is the ONLY guaranteed\n"
124 "Update cached key? (y/n, Return cancels connection) ";
126 static const char abandoned[] = "Connection abandoned.\n";
134 ret = verify_host_key(host, port, keytype, keystr);
136 if (ret == 0) /* success - key matched OK */
140 if (ret == 2) { /* key was different */
141 if (console_batch_mode) {
142 fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
145 fprintf(stderr, wrongmsg, keytype, fingerprint);
148 if (ret == 1) { /* key was absent */
149 if (console_batch_mode) {
150 fprintf(stderr, absentmsg_batch, keytype, fingerprint);
153 fprintf(stderr, absentmsg, keytype, fingerprint);
158 struct termios oldmode, newmode;
159 tcgetattr(0, &oldmode);
161 newmode.c_lflag |= ECHO | ISIG | ICANON;
162 tcsetattr(0, TCSANOW, &newmode);
164 if (read(0, line, sizeof(line) - 1) <= 0)
166 tcsetattr(0, TCSANOW, &oldmode);
169 if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
170 if (line[0] == 'y' || line[0] == 'Y')
171 store_host_key(host, port, keytype, keystr);
175 fprintf(stderr, abandoned);
182 * Ask whether the selected algorithm is acceptable (since it was
183 * below the configured 'warn' threshold).
185 int askalg(void *frontend, const char *algtype, const char *algname,
186 void (*callback)(void *ctx, int result), void *ctx)
188 static const char msg[] =
189 "The first %s supported by the server is\n"
190 "%s, which is below the configured warning threshold.\n"
191 "Continue with connection? (y/n) ";
192 static const char msg_batch[] =
193 "The first %s supported by the server is\n"
194 "%s, which is below the configured warning threshold.\n"
195 "Connection abandoned.\n";
196 static const char abandoned[] = "Connection abandoned.\n";
202 if (console_batch_mode) {
203 fprintf(stderr, msg_batch, algtype, algname);
207 fprintf(stderr, msg, algtype, algname);
211 struct termios oldmode, newmode;
212 tcgetattr(0, &oldmode);
214 newmode.c_lflag |= ECHO | ISIG | ICANON;
215 tcsetattr(0, TCSANOW, &newmode);
217 if (read(0, line, sizeof(line) - 1) <= 0)
219 tcsetattr(0, TCSANOW, &oldmode);
222 if (line[0] == 'y' || line[0] == 'Y') {
226 fprintf(stderr, abandoned);
233 * Ask whether to wipe a session log file before writing to it.
234 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
236 int askappend(void *frontend, Filename filename,
237 void (*callback)(void *ctx, int result), void *ctx)
239 static const char msgtemplate[] =
240 "The session log file \"%.*s\" already exists.\n"
241 "You can overwrite it with a new session log,\n"
242 "append your session log to the end of it,\n"
243 "or disable session logging for this session.\n"
244 "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
245 "or just press Return to disable logging.\n"
246 "Wipe the log file? (y/n, Return cancels logging) ";
248 static const char msgtemplate_batch[] =
249 "The session log file \"%.*s\" already exists.\n"
250 "Logging will not be enabled.\n";
256 if (console_batch_mode) {
257 fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path);
261 fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path);
265 struct termios oldmode, newmode;
266 tcgetattr(0, &oldmode);
268 newmode.c_lflag |= ECHO | ISIG | ICANON;
269 tcsetattr(0, TCSANOW, &newmode);
271 if (read(0, line, sizeof(line) - 1) <= 0)
273 tcsetattr(0, TCSANOW, &oldmode);
277 if (line[0] == 'y' || line[0] == 'Y')
279 else if (line[0] == 'n' || line[0] == 'N')
286 * Warn about the obsolescent key file format.
288 * Uniquely among these functions, this one does _not_ expect a
289 * frontend handle. This means that if PuTTY is ported to a
290 * platform which requires frontend handles, this function will be
291 * an anomaly. Fortunately, the problem it addresses will not have
292 * been present on that platform, so it can plausibly be
293 * implemented as an empty function.
295 void old_keyfile_warning(void)
297 static const char message[] =
298 "You are loading an SSH-2 private key which has an\n"
299 "old version of the file format. This means your key\n"
300 "file is not fully tamperproof. Future versions of\n"
301 "PuTTY may stop supporting this private key format,\n"
302 "so we recommend you convert your key to the new\n"
305 "Once the key is loaded into PuTTYgen, you can perform\n"
306 "this conversion simply by saving it again.\n";
310 fputs(message, stderr);
314 void console_provide_logctx(void *logctx)
316 console_logctx = logctx;
319 void logevent(void *frontend, const char *string)
324 log_eventlog(console_logctx, string);
329 * Special function to print text to the console for password
330 * prompts and the like. Uses /dev/tty or stderr, in that order of
331 * preference; also sanitises escape sequences out of the text, on
332 * the basis that it might have been sent by a hostile SSH server
333 * doing malicious keyboard-interactive.
335 static void console_prompt_text(FILE **confp, const char *data, int len)
340 if ((*confp = fopen("/dev/tty", "w")) == NULL)
344 for (i = 0; i < len; i++)
345 if ((data[i] & 0x60) || (data[i] == '\n'))
346 fputc(data[i], *confp);
350 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
356 * Zero all the results, in case we abort half-way through.
360 for (i = 0; i < p->n_prompts; i++)
361 memset(p->prompts[i]->result, 0, p->prompts[i]->result_len);
364 if (p->n_prompts && console_batch_mode)
370 /* We only print the `name' caption if we have to... */
371 if (p->name_reqd && p->name) {
372 size_t l = strlen(p->name);
373 console_prompt_text(&confp, p->name, l);
374 if (p->name[l-1] != '\n')
375 console_prompt_text(&confp, "\n", 1);
377 /* ...but we always print any `instruction'. */
378 if (p->instruction) {
379 size_t l = strlen(p->instruction);
380 console_prompt_text(&confp, p->instruction, l);
381 if (p->instruction[l-1] != '\n')
382 console_prompt_text(&confp, "\n", 1);
385 for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {
387 struct termios oldmode, newmode;
389 prompt_t *pr = p->prompts[curr_prompt];
391 tcgetattr(0, &oldmode);
393 newmode.c_lflag |= ISIG | ICANON;
395 newmode.c_lflag &= ~ECHO;
397 newmode.c_lflag |= ECHO;
398 tcsetattr(0, TCSANOW, &newmode);
400 console_prompt_text(&confp, pr->prompt, strlen(pr->prompt));
402 i = read(0, pr->result, pr->result_len - 1);
404 tcsetattr(0, TCSANOW, &oldmode);
406 if (i > 0 && pr->result[i-1] == '\n')
408 pr->result[i] = '\0';
411 console_prompt_text(&confp, "\n", 1);
415 if (confp && confp != stderr)
418 return 1; /* success */
421 void frontend_keypress(void *handle)
424 * This is nothing but a stub, in console code.
429 int is_interactive(void)
435 * X11-forwarding-related things suitable for console.
438 char *platform_get_x_display(void) {
439 return dupstr(getenv("DISPLAY"));