1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 19:42: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,17 +5,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "putty.h"
#include "storage.h"
#include "ssh.h"
#include "console.h"
bool console_batch_mode = false;
/*
* Clean up and exit.
*/
void cleanup_exit(int code)
{
/*
@ -28,9 +23,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)
{
fputs(prefix, stderr);
@ -40,49 +32,6 @@ void console_print_error_msg(const char *prefix, const char *msg)
fflush(stderr);
}
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)
{
}
int console_verify_ssh_host_key(
Seat *seat, const char *host, int port,
const char *keytype, char *keystr, char *fingerprint,
@ -91,57 +40,7 @@ int console_verify_ssh_host_key(
int ret;
HANDLE hin;
DWORD savemode, i;
static const char absentmsg_batch[] =
"The server's host key is not cached in the registry. You\n"
"have no guarantee that the server is the computer you\n"
"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 in the registry. You\n"
"have no guarantee that the server is the computer you\n"
"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 in the registry. This means that either the\n"
"server administrator has changed the host key, or you\n"
"have actually connected to another computer pretending\n"
"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 in the registry. This means that either the\n"
"server administrator has changed the host key, or you\n"
"have actually connected to another computer pretending\n"
"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";
const char *common_fmt, *intro, *prompt;
char line[32];
@ -154,23 +53,28 @@ int console_verify_ssh_host_key(
return 1;
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;
}
line[0] = '\0'; /* fail safe if ReadFile returns no data */
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);
line[0] = '\0'; /* fail safe if ReadFile returns no data */
hin = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(hin, &savemode);
@ -184,7 +88,7 @@ int console_verify_ssh_host_key(
store_host_key(host, port, keytype, keystr);
return 1;
} else {
fprintf(stderr, abandoned);
fputs(console_abandoned_msg, stderr);
return 0;
}
}
@ -196,24 +100,16 @@ int console_confirm_weak_crypto_primitive(
HANDLE hin;
DWORD savemode, i;
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];
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);
hin = GetStdHandle(STD_INPUT_HANDLE);
@ -226,7 +122,7 @@ int console_confirm_weak_crypto_primitive(
if (line[0] == 'y' || line[0] == 'Y') {
return 1;
} else {
fprintf(stderr, abandoned);
fputs(console_abandoned_msg, stderr);
return 0;
}
}
@ -238,30 +134,16 @@ int console_confirm_weak_cached_hostkey(
HANDLE hin;
DWORD savemode, i;
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];
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);
hin = GetStdHandle(STD_INPUT_HANDLE);
@ -274,7 +156,7 @@ int console_confirm_weak_cached_hostkey(
if (line[0] == 'y' || line[0] == 'Y') {
return 1;
} else {
fprintf(stderr, abandoned);
fputs(console_abandoned_msg, stderr);
return 0;
}
}