1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 03:22:48 -05:00

Centralise most details of host-key prompting.

The text of the host key warnings was replicated in three places: the
Windows rc file, the GTK dialog setup function, and the console.c
shared between both platforms' CLI tools. Now it lives in just one
place, namely ssh/common.c where the rest of the centralised host-key
checking is done, so it'll be easier to adjust the wording in future.

This comes with some extra automation. Paragraph wrapping is no longer
done by hand in any version of these prompts. (Previously we let GTK
do the wrapping on GTK, but on Windows the resource file contained a
bunch of pre-wrapped LTEXT lines, and console.c had pre-wrapped
terminal messages.) And the dialog heights in Windows are determined
automatically based on the amount of stuff in the window.

The main idea of all this is that it'll be easier to set up more
elaborate kinds of host key prompt that deal with certificates (if,
e.g., a server sends us a certified host key which we don't trust the
CA for). But there are side benefits of this refactoring too: each
tool now reliably inserts its own appname in the prompts, and also, on
Windows the entire prompt text is copy-pastable.

Details of implementation: there's a new type SeatDialogText which
holds a set of (type, string) pairs describing the contents of a
prompt. Type codes distinguish ordinary text paragraphs, paragraphs to
be displayed prominently (like key fingerprints), the extra-bold scary
title at the top of the 'host key changed' version of the dialog, and
the various information that lives in the subsidiary 'more info' box.
ssh/common.c constructs this, and passes it to the Seat to present the
actual prompt.

In order to deal with the different UI for answering the prompt, I've
added an extra Seat method 'prompt_descriptions' which returns some
snippets of text to interpolate into the messages. ssh/common.c calls
that while it's still constructing the text, and incorporates the
resulting snippets into the SeatDialogText.

For the moment, this refactoring only affects the host key prompts.
The warnings about outmoded crypto are still done the old-fashioned
way; they probably ought to be similarly refactored to use this new
SeatDialogText system, but it's not immediately critical for the
purpose I have right now.
This commit is contained in:
Simon Tatham
2022-07-07 17:25:15 +01:00
parent 46332db26e
commit f1c8298000
25 changed files with 644 additions and 322 deletions

View File

@ -53,6 +53,7 @@ add_sources_from_current_dir(utils
ptrlen.c
read_file_into.c
seat_connection_fatal.c
seat_dialog_text.c
sessprep.c
sk_free_peer_info.c
smemclr.c

View File

@ -22,7 +22,7 @@ char *nullseat_get_ttymode(Seat *seat, const char *mode) { return NULL; }
void nullseat_set_busy_status(Seat *seat, BusyStatus status) {}
SeatPromptResult nullseat_confirm_ssh_host_key(
Seat *seat, const char *host, int port, const char *keytype,
char *keystr, const char *keydisp, char **key_fingerprints, bool mismatch,
char *keystr, SeatDialogText *text, HelpCtx helpctx,
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
{ return SPR_SW_ABORT("this seat can't handle interactive prompts"); }
SeatPromptResult nullseat_confirm_weak_crypto_primitive(
@ -52,3 +52,14 @@ bool nullseat_verbose_yes(Seat *seat) { return true; }
bool nullseat_interactive_no(Seat *seat) { return false; }
bool nullseat_interactive_yes(Seat *seat) { return true; }
bool nullseat_get_cursor_position(Seat *seat, int *x, int *y) { return false; }
const SeatDialogPromptDescriptions *nullseat_prompt_descriptions(Seat *seat)
{
static const SeatDialogPromptDescriptions descs = {
.hk_accept_action = "",
.hk_connect_once_action = "",
.hk_cancel_action = "",
.hk_cancel_action_Participle = "",
};
return &descs;
}

41
utils/seat_dialog_text.c Normal file
View File

@ -0,0 +1,41 @@
/*
* Helper routines for dealing with SeatDialogText structures.
*/
#include <stdarg.h>
#include "putty.h"
SeatDialogText *seat_dialog_text_new(void)
{
SeatDialogText *sdt = snew(SeatDialogText);
sdt->nitems = sdt->itemsize = 0;
sdt->items = NULL;
return sdt;
}
void seat_dialog_text_free(SeatDialogText *sdt)
{
for (size_t i = 0; i < sdt->nitems; i++)
sfree(sdt->items[i].text);
sfree(sdt->items);
sfree(sdt);
}
static void seat_dialog_text_append_v(
SeatDialogText *sdt, SeatDialogTextType type, const char *fmt, va_list ap)
{
sgrowarray(sdt->items, sdt->itemsize, sdt->nitems);
SeatDialogTextItem *item = &sdt->items[sdt->nitems++];
item->type = type;
item->text = dupvprintf(fmt, ap);
}
void seat_dialog_text_append(SeatDialogText *sdt, SeatDialogTextType type,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
seat_dialog_text_append_v(sdt, type, fmt, ap);
va_end(ap);
}

View File

@ -204,6 +204,17 @@ static bool tempseat_has_mixed_input_stream(Seat *seat)
return seat_has_mixed_input_stream(ts->realseat);
}
static const SeatDialogPromptDescriptions *tempseat_prompt_descriptions(
Seat *seat)
{
/* It might be OK to put this in the 'unreachable' category, but I
* think it's equally good to put it here, which allows for
* someone _preparing_ a prompt right now that they intend to
* present once the TempSeat has given way to the real one. */
TempSeat *ts = container_of(seat, TempSeat, seat);
return seat_prompt_descriptions(ts->realseat);
}
/* ----------------------------------------------------------------------
* Methods that should never be called on a TempSeat, so we can put an
* unreachable() in them.
@ -237,7 +248,7 @@ static size_t tempseat_banner(Seat *seat, const void *data, size_t len)
static SeatPromptResult tempseat_confirm_ssh_host_key(
Seat *seat, const char *host, int port, const char *keytype,
char *keystr, const char *keydisp, char **key_fingerprints, bool mismatch,
char *keystr, SeatDialogText *text, HelpCtx helpctx,
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
{
unreachable("confirm_ssh_host_key should never be called on TempSeat");
@ -322,6 +333,7 @@ static const struct SeatVtable tempseat_vt = {
.confirm_ssh_host_key = tempseat_confirm_ssh_host_key,
.confirm_weak_crypto_primitive = tempseat_confirm_weak_crypto_primitive,
.confirm_weak_cached_hostkey = tempseat_confirm_weak_cached_hostkey,
.prompt_descriptions = tempseat_prompt_descriptions,
.is_utf8 = tempseat_is_utf8,
.echoedit_update = tempseat_echoedit_update,
.get_x_display = tempseat_get_x_display,