1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-06-30 19:12:48 -05:00

Factor out some common code in {ux,win}cons.c.

The assorted host-key and warning prompt messages have no reason to
differ between the two platforms, so let's centralise them. Also,
while I'm here, some basic support functions that are the same in both
modules.
This commit is contained in:
Simon Tatham
2021-03-13 09:24:17 +00:00
parent 670f9d8620
commit 3c6ab5bbb7
5 changed files with 214 additions and 318 deletions

View File

@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
@ -17,8 +16,7 @@
#include "putty.h"
#include "storage.h"
#include "ssh.h"
bool console_batch_mode = false;
#include "console.h"
static struct termios orig_termios_stderr;
static bool stderr_is_a_tty;
@ -45,9 +43,6 @@ void postmsg(struct termios *cf)
tcsetattr(STDERR_FILENO, TCSADRAIN, cf);
}
/*
* Clean up and exit.
*/
void cleanup_exit(int code)
{
/*
@ -58,9 +53,6 @@ void cleanup_exit(int code)
exit(code);
}
/*
* Various error message and/or fatal exit functions.
*/
void console_print_error_msg(const char *prefix, const char *msg)
{
struct termios cf;
@ -73,49 +65,6 @@ void console_print_error_msg(const char *prefix, const char *msg)
postmsg(&cf);
}
void console_print_error_msg_fmt_v(
const char *prefix, const char *fmt, va_list ap)
{
char *msg = dupvprintf(fmt, ap);
console_print_error_msg(prefix, msg);
sfree(msg);
}
void console_print_error_msg_fmt(const char *prefix, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
console_print_error_msg_fmt_v(prefix, fmt, ap);
va_end(ap);
}
void modalfatalbox(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
console_print_error_msg_fmt_v("FATAL ERROR", fmt, ap);
va_end(ap);
cleanup_exit(1);
}
void nonfatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
console_print_error_msg_fmt_v("ERROR", fmt, ap);
va_end(ap);
}
void console_connection_fatal(Seat *seat, const char *msg)
{
console_print_error_msg("FATAL ERROR", msg);
cleanup_exit(1);
}
void timer_change_notify(unsigned long next)
{
}
/*
* Wrapper around Unix read(2), suitable for use on a file descriptor
* that's been set into nonblocking mode. Handles EAGAIN/EWOULDBLOCK
@ -160,55 +109,9 @@ int console_verify_ssh_host_key(
{
int ret;
static const char absentmsg_batch[] =
"The server's host key is not cached. You have no guarantee\n"
"that the server is the computer you think it is.\n"
"The server's %s key fingerprint is:\n"
"%s\n"
"Connection abandoned.\n";
static const char absentmsg[] =
"The server's host key is not cached. You have no guarantee\n"
"that the server is the computer you think it is.\n"
"The server's %s key fingerprint is:\n"
"%s\n"
"If you trust this host, enter \"y\" to add the key to\n"
"PuTTY's cache and carry on connecting.\n"
"If you want to carry on connecting just once, without\n"
"adding the key to the cache, enter \"n\".\n"
"If you do not trust this host, press Return to abandon the\n"
"connection.\n"
"Store key in cache? (y/n) ";
static const char wrongmsg_batch[] =
"WARNING - POTENTIAL SECURITY BREACH!\n"
"The server's host key does not match the one PuTTY has\n"
"cached. This means that either the server administrator\n"
"has changed the host key, or you have actually connected\n"
"to another computer pretending to be the server.\n"
"The new %s key fingerprint is:\n"
"%s\n"
"Connection abandoned.\n";
static const char wrongmsg[] =
"WARNING - POTENTIAL SECURITY BREACH!\n"
"The server's host key does not match the one PuTTY has\n"
"cached. This means that either the server administrator\n"
"has changed the host key, or you have actually connected\n"
"to another computer pretending to be the server.\n"
"The new %s key fingerprint is:\n"
"%s\n"
"If you were expecting this change and trust the new key,\n"
"enter \"y\" to update PuTTY's cache and continue connecting.\n"
"If you want to carry on connecting but without updating\n"
"the cache, enter \"n\".\n"
"If you want to abandon the connection completely, press\n"
"Return to cancel. Pressing Return is the ONLY guaranteed\n"
"safe choice.\n"
"Update cached key? (y/n, Return cancels connection) ";
static const char abandoned[] = "Connection abandoned.\n";
char line[32];
struct termios cf;
const char *common_fmt, *intro, *prompt;
/*
* Verify the key.
@ -220,41 +123,44 @@ int console_verify_ssh_host_key(
premsg(&cf);
if (ret == 2) { /* key was different */
if (console_batch_mode) {
fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
return 0;
}
fprintf(stderr, wrongmsg, keytype, fingerprint);
fflush(stderr);
}
if (ret == 1) { /* key was absent */
if (console_batch_mode) {
fprintf(stderr, absentmsg_batch, keytype, fingerprint);
return 0;
}
fprintf(stderr, absentmsg, keytype, fingerprint);
fflush(stderr);
common_fmt = hk_wrongmsg_common_fmt;
intro = hk_wrongmsg_interactive_intro;
prompt = hk_wrongmsg_interactive_prompt;
} else { /* key was absent */
common_fmt = hk_absentmsg_common_fmt;
intro = hk_absentmsg_interactive_intro;
prompt = hk_absentmsg_interactive_prompt;
}
{
struct termios oldmode, newmode;
tcgetattr(0, &oldmode);
newmode = oldmode;
newmode.c_lflag |= ECHO | ISIG | ICANON;
tcsetattr(0, TCSANOW, &newmode);
line[0] = '\0';
if (block_and_read(0, line, sizeof(line) - 1) <= 0)
/* handled below */;
tcsetattr(0, TCSANOW, &oldmode);
fprintf(stderr, common_fmt, keytype, fingerprint);
if (console_batch_mode) {
fputs(console_abandoned_msg, stderr);
return 0;
}
fputs(intro, stderr);
fflush(stderr);
fputs(prompt, stderr);
fflush(stderr);
struct termios oldmode, newmode;
tcgetattr(0, &oldmode);
newmode = oldmode;
newmode.c_lflag |= ECHO | ISIG | ICANON;
tcsetattr(0, TCSANOW, &newmode);
line[0] = '\0';
if (block_and_read(0, line, sizeof(line) - 1) <= 0)
/* handled below */;
tcsetattr(0, TCSANOW, &oldmode);
if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
if (line[0] == 'y' || line[0] == 'Y')
store_host_key(host, port, keytype, keystr);
postmsg(&cf);
return 1;
} else {
fprintf(stderr, abandoned);
fputs(console_abandoned_msg, stderr);
postmsg(&cf);
return 0;
}
@ -264,26 +170,18 @@ int console_confirm_weak_crypto_primitive(
Seat *seat, const char *algtype, const char *algname,
void (*callback)(void *ctx, int result), void *ctx)
{
static const char msg[] =
"The first %s supported by the server is\n"
"%s, which is below the configured warning threshold.\n"
"Continue with connection? (y/n) ";
static const char msg_batch[] =
"The first %s supported by the server is\n"
"%s, which is below the configured warning threshold.\n"
"Connection abandoned.\n";
static const char abandoned[] = "Connection abandoned.\n";
char line[32];
struct termios cf;
premsg(&cf);
fprintf(stderr, weakcrypto_msg_common_fmt, algtype, algname);
if (console_batch_mode) {
fprintf(stderr, msg_batch, algtype, algname);
fputs(console_abandoned_msg, stderr);
return 0;
}
fprintf(stderr, msg, algtype, algname);
fputs(console_continue_prompt, stderr);
fflush(stderr);
{
@ -302,7 +200,7 @@ int console_confirm_weak_crypto_primitive(
postmsg(&cf);
return 1;
} else {
fprintf(stderr, abandoned);
fputs(console_abandoned_msg, stderr);
postmsg(&cf);
return 0;
}
@ -312,32 +210,18 @@ int console_confirm_weak_cached_hostkey(
Seat *seat, const char *algname, const char *betteralgs,
void (*callback)(void *ctx, int result), void *ctx)
{
static const char msg[] =
"The first host key type we have stored for this server\n"
"is %s, which is below the configured warning threshold.\n"
"The server also provides the following types of host key\n"
"above the threshold, which we do not have stored:\n"
"%s\n"
"Continue with connection? (y/n) ";
static const char msg_batch[] =
"The first host key type we have stored for this server\n"
"is %s, which is below the configured warning threshold.\n"
"The server also provides the following types of host key\n"
"above the threshold, which we do not have stored:\n"
"%s\n"
"Connection abandoned.\n";
static const char abandoned[] = "Connection abandoned.\n";
char line[32];
struct termios cf;
premsg(&cf);
fprintf(stderr, weakhk_msg_common_fmt, algname, betteralgs);
if (console_batch_mode) {
fprintf(stderr, msg_batch, algname, betteralgs);
fputs(console_abandoned_msg, stderr);
return 0;
}
fprintf(stderr, msg, algname, betteralgs);
fputs(console_continue_prompt, stderr);
fflush(stderr);
{
@ -356,7 +240,7 @@ int console_confirm_weak_cached_hostkey(
postmsg(&cf);
return 1;
} else {
fprintf(stderr, abandoned);
fputs(console_abandoned_msg, stderr);
postmsg(&cf);
return 0;
}