2 * uxcons.c: various interactive-prompt routines shared between the
3 * Unix console PuTTY tools
20 int console_batch_mode = FALSE;
22 static void *console_logctx = NULL;
24 static struct termios orig_termios_stderr;
25 static int stderr_is_a_tty;
27 void stderr_tty_init()
29 /* Ensure that if stderr is a tty, we can get it back to a sane state. */
30 if ((flags & FLAG_STDERR_TTY) && isatty(STDERR_FILENO)) {
31 stderr_is_a_tty = TRUE;
32 tcgetattr(STDERR_FILENO, &orig_termios_stderr);
36 void premsg(struct termios *cf)
38 if (stderr_is_a_tty) {
39 tcgetattr(STDERR_FILENO, cf);
40 tcsetattr(STDERR_FILENO, TCSADRAIN, &orig_termios_stderr);
43 void postmsg(struct termios *cf)
46 tcsetattr(STDERR_FILENO, TCSADRAIN, cf);
52 void cleanup_exit(int code)
62 void set_busy_status(void *frontend, int status)
66 void update_specials_menu(void *frontend)
70 void notify_remote_exit(void *frontend)
74 void timer_change_notify(unsigned long next)
79 * Wrapper around Unix read(2), suitable for use on a file descriptor
80 * that's been set into nonblocking mode. Handles EAGAIN/EWOULDBLOCK
81 * by means of doing a one-fd select and then trying again; all other
82 * errors (including errors from select) are returned to the caller.
84 static int block_and_read(int fd, void *buf, size_t len)
88 while ((ret = read(fd, buf, len)) < 0 && (
93 (errno == EWOULDBLOCK) ||
100 ret = select(fd+1, &rfds, NULL, NULL, NULL);
104 assert(FD_ISSET(fd, &rfds));
110 int verify_ssh_host_key(void *frontend, char *host, int port,
111 const char *keytype, char *keystr, char *fingerprint,
112 void (*callback)(void *ctx, int result), void *ctx)
116 static const char absentmsg_batch[] =
117 "The server's host key is not cached. You have no guarantee\n"
118 "that the server is the computer you think it is.\n"
119 "The server's %s key fingerprint is:\n"
121 "Connection abandoned.\n";
122 static const char absentmsg[] =
123 "The server's host key is not cached. You have no guarantee\n"
124 "that the server is the computer you think it is.\n"
125 "The server's %s key fingerprint is:\n"
127 "If you trust this host, enter \"y\" to add the key to\n"
128 "PuTTY's cache and carry on connecting.\n"
129 "If you want to carry on connecting just once, without\n"
130 "adding the key to the cache, enter \"n\".\n"
131 "If you do not trust this host, press Return to abandon the\n"
133 "Store key in cache? (y/n) ";
135 static const char wrongmsg_batch[] =
136 "WARNING - POTENTIAL SECURITY BREACH!\n"
137 "The server's host key does not match the one PuTTY has\n"
138 "cached. This means that either the server administrator\n"
139 "has changed the host key, or you have actually connected\n"
140 "to another computer pretending to be the server.\n"
141 "The new %s key fingerprint is:\n"
143 "Connection abandoned.\n";
144 static const char wrongmsg[] =
145 "WARNING - POTENTIAL SECURITY BREACH!\n"
146 "The server's host key does not match the one PuTTY has\n"
147 "cached. This means that either the server administrator\n"
148 "has changed the host key, or you have actually connected\n"
149 "to another computer pretending to be the server.\n"
150 "The new %s key fingerprint is:\n"
152 "If you were expecting this change and trust the new key,\n"
153 "enter \"y\" to update PuTTY's cache and continue connecting.\n"
154 "If you want to carry on connecting but without updating\n"
155 "the cache, enter \"n\".\n"
156 "If you want to abandon the connection completely, press\n"
157 "Return to cancel. Pressing Return is the ONLY guaranteed\n"
159 "Update cached key? (y/n, Return cancels connection) ";
161 static const char abandoned[] = "Connection abandoned.\n";
169 ret = verify_host_key(host, port, keytype, keystr);
171 if (ret == 0) /* success - key matched OK */
175 if (ret == 2) { /* key was different */
176 if (console_batch_mode) {
177 fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
180 fprintf(stderr, wrongmsg, keytype, fingerprint);
183 if (ret == 1) { /* key was absent */
184 if (console_batch_mode) {
185 fprintf(stderr, absentmsg_batch, keytype, fingerprint);
188 fprintf(stderr, absentmsg, keytype, fingerprint);
193 struct termios oldmode, newmode;
194 tcgetattr(0, &oldmode);
196 newmode.c_lflag |= ECHO | ISIG | ICANON;
197 tcsetattr(0, TCSANOW, &newmode);
199 if (block_and_read(0, line, sizeof(line) - 1) <= 0)
201 tcsetattr(0, TCSANOW, &oldmode);
204 if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
205 if (line[0] == 'y' || line[0] == 'Y')
206 store_host_key(host, port, keytype, keystr);
210 fprintf(stderr, abandoned);
217 * Ask whether the selected algorithm is acceptable (since it was
218 * below the configured 'warn' threshold).
220 int askalg(void *frontend, const char *algtype, const char *algname,
221 void (*callback)(void *ctx, int result), void *ctx)
223 static const char msg[] =
224 "The first %s supported by the server is\n"
225 "%s, which is below the configured warning threshold.\n"
226 "Continue with connection? (y/n) ";
227 static const char msg_batch[] =
228 "The first %s supported by the server is\n"
229 "%s, which is below the configured warning threshold.\n"
230 "Connection abandoned.\n";
231 static const char abandoned[] = "Connection abandoned.\n";
237 if (console_batch_mode) {
238 fprintf(stderr, msg_batch, algtype, algname);
242 fprintf(stderr, msg, algtype, algname);
246 struct termios oldmode, newmode;
247 tcgetattr(0, &oldmode);
249 newmode.c_lflag |= ECHO | ISIG | ICANON;
250 tcsetattr(0, TCSANOW, &newmode);
252 if (block_and_read(0, line, sizeof(line) - 1) <= 0)
254 tcsetattr(0, TCSANOW, &oldmode);
257 if (line[0] == 'y' || line[0] == 'Y') {
261 fprintf(stderr, abandoned);
267 int askhk(void *frontend, const char *algname, const char *betteralgs,
268 void (*callback)(void *ctx, int result), void *ctx)
270 static const char msg[] =
271 "The first host key type we have stored for this server\n"
272 "is %s, which is below the configured warning threshold.\n"
273 "The server also provides the following types of host key\n"
274 "above the threshold, which we do not have stored:\n"
276 "Continue with connection? (y/n) ";
277 static const char msg_batch[] =
278 "The first host key type we have stored for this server\n"
279 "is %s, which is below the configured warning threshold.\n"
280 "The server also provides the following types of host key\n"
281 "above the threshold, which we do not have stored:\n"
283 "Connection abandoned.\n";
284 static const char abandoned[] = "Connection abandoned.\n";
290 if (console_batch_mode) {
291 fprintf(stderr, msg_batch, algname, betteralgs);
295 fprintf(stderr, msg, algname, betteralgs);
299 struct termios oldmode, newmode;
300 tcgetattr(0, &oldmode);
302 newmode.c_lflag |= ECHO | ISIG | ICANON;
303 tcsetattr(0, TCSANOW, &newmode);
305 if (block_and_read(0, line, sizeof(line) - 1) <= 0)
307 tcsetattr(0, TCSANOW, &oldmode);
310 if (line[0] == 'y' || line[0] == 'Y') {
314 fprintf(stderr, abandoned);
321 * Ask whether to wipe a session log file before writing to it.
322 * Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
324 int askappend(void *frontend, Filename *filename,
325 void (*callback)(void *ctx, int result), void *ctx)
327 static const char msgtemplate[] =
328 "The session log file \"%.*s\" already exists.\n"
329 "You can overwrite it with a new session log,\n"
330 "append your session log to the end of it,\n"
331 "or disable session logging for this session.\n"
332 "Enter \"y\" to wipe the file, \"n\" to append to it,\n"
333 "or just press Return to disable logging.\n"
334 "Wipe the log file? (y/n, Return cancels logging) ";
336 static const char msgtemplate_batch[] =
337 "The session log file \"%.*s\" already exists.\n"
338 "Logging will not be enabled.\n";
344 if (console_batch_mode) {
345 fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename->path);
349 fprintf(stderr, msgtemplate, FILENAME_MAX, filename->path);
353 struct termios oldmode, newmode;
354 tcgetattr(0, &oldmode);
356 newmode.c_lflag |= ECHO | ISIG | ICANON;
357 tcsetattr(0, TCSANOW, &newmode);
359 if (block_and_read(0, line, sizeof(line) - 1) <= 0)
361 tcsetattr(0, TCSANOW, &oldmode);
365 if (line[0] == 'y' || line[0] == 'Y')
367 else if (line[0] == 'n' || line[0] == 'N')
374 * Warn about the obsolescent key file format.
376 * Uniquely among these functions, this one does _not_ expect a
377 * frontend handle. This means that if PuTTY is ported to a
378 * platform which requires frontend handles, this function will be
379 * an anomaly. Fortunately, the problem it addresses will not have
380 * been present on that platform, so it can plausibly be
381 * implemented as an empty function.
383 void old_keyfile_warning(void)
385 static const char message[] =
386 "You are loading an SSH-2 private key which has an\n"
387 "old version of the file format. This means your key\n"
388 "file is not fully tamperproof. Future versions of\n"
389 "PuTTY may stop supporting this private key format,\n"
390 "so we recommend you convert your key to the new\n"
393 "Once the key is loaded into PuTTYgen, you can perform\n"
394 "this conversion simply by saving it again.\n";
398 fputs(message, stderr);
402 void console_provide_logctx(void *logctx)
404 console_logctx = logctx;
407 void logevent(void *frontend, const char *string)
410 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE))
413 log_eventlog(console_logctx, string);
414 if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE))
419 * Special functions to read and print to the console for password
420 * prompts and the like. Uses /dev/tty or stdin/stderr, in that order
421 * of preference; also sanitises escape sequences out of the text, on
422 * the basis that it might have been sent by a hostile SSH server
423 * doing malicious keyboard-interactive.
425 static void console_open(FILE **outfp, int *infd)
429 if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
431 *outfp = fdopen(*infd, "w");
437 static void console_close(FILE *outfp, int infd)
440 fclose(outfp); /* will automatically close infd too */
443 static void console_prompt_text(FILE *outfp, const char *data, int len)
447 for (i = 0; i < len; i++)
448 if ((data[i] & 0x60) || (data[i] == '\n'))
449 fputc(data[i], outfp);
453 int console_get_userpass_input(prompts_t *p, const unsigned char *in,
461 * Zero all the results, in case we abort half-way through.
465 for (i = 0; i < p->n_prompts; i++)
466 prompt_set_result(p->prompts[i], "");
469 if (p->n_prompts && console_batch_mode)
472 console_open(&outfp, &infd);
477 /* We only print the `name' caption if we have to... */
478 if (p->name_reqd && p->name) {
479 size_t l = strlen(p->name);
480 console_prompt_text(outfp, p->name, l);
481 if (p->name[l-1] != '\n')
482 console_prompt_text(outfp, "\n", 1);
484 /* ...but we always print any `instruction'. */
485 if (p->instruction) {
486 size_t l = strlen(p->instruction);
487 console_prompt_text(outfp, p->instruction, l);
488 if (p->instruction[l-1] != '\n')
489 console_prompt_text(outfp, "\n", 1);
492 for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {
494 struct termios oldmode, newmode;
496 prompt_t *pr = p->prompts[curr_prompt];
498 tcgetattr(infd, &oldmode);
500 newmode.c_lflag |= ISIG | ICANON;
502 newmode.c_lflag &= ~ECHO;
504 newmode.c_lflag |= ECHO;
505 tcsetattr(infd, TCSANOW, &newmode);
507 console_prompt_text(outfp, pr->prompt, strlen(pr->prompt));
513 prompt_ensure_result_size(pr, len * 5 / 4 + 512);
514 ret = read(infd, pr->result + len, pr->resultsize - len - 1);
520 if (pr->result[len - 1] == '\n') {
526 tcsetattr(infd, TCSANOW, &oldmode);
529 console_prompt_text(outfp, "\n", 1);
532 console_close(outfp, infd);
533 return 0; /* failure due to read error */
536 pr->result[len] = '\0';
539 console_close(outfp, infd);
541 return 1; /* success */
544 void frontend_keypress(void *handle)
547 * This is nothing but a stub, in console code.
552 int is_interactive(void)
558 * X11-forwarding-related things suitable for console.
561 char *platform_get_x_display(void) {
562 return dupstr(getenv("DISPLAY"));