mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-06 14:02:47 -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:
@ -104,43 +104,53 @@ static int block_and_read(int fd, void *buf, size_t len)
|
||||
|
||||
SeatPromptResult console_confirm_ssh_host_key(
|
||||
Seat *seat, const char *host, int port, const char *keytype,
|
||||
char *keystr, const char *keydisp, char **fingerprints, bool mismatch,
|
||||
char *keystr, SeatDialogText *text, HelpCtx helpctx,
|
||||
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
|
||||
{
|
||||
char line[32];
|
||||
struct termios cf;
|
||||
char *common;
|
||||
const char *intro, *prompt;
|
||||
const char *prompt;
|
||||
|
||||
FingerprintType fptype_default =
|
||||
ssh2_pick_default_fingerprint(fingerprints);
|
||||
|
||||
if (mismatch) { /* key was different */
|
||||
common = hk_wrongmsg_common(host, port, keytype,
|
||||
fingerprints[fptype_default]);
|
||||
intro = hk_wrongmsg_interactive_intro;
|
||||
prompt = hk_wrongmsg_interactive_prompt;
|
||||
} else { /* key was absent */
|
||||
common = hk_absentmsg_common(host, port, keytype,
|
||||
fingerprints[fptype_default]);
|
||||
intro = hk_absentmsg_interactive_intro;
|
||||
prompt = hk_absentmsg_interactive_prompt;
|
||||
}
|
||||
stdio_sink errsink[1];
|
||||
stdio_sink_init(errsink, stderr);
|
||||
|
||||
premsg(&cf);
|
||||
fputs(common, stderr);
|
||||
sfree(common);
|
||||
|
||||
if (console_batch_mode) {
|
||||
fputs(console_abandoned_msg, stderr);
|
||||
postmsg(&cf);
|
||||
return SPR_SW_ABORT("Cannot confirm a host key in batch mode");
|
||||
for (SeatDialogTextItem *item = text->items,
|
||||
*end = item+text->nitems; item < end; item++) {
|
||||
switch (item->type) {
|
||||
case SDT_PARA:
|
||||
wordwrap(BinarySink_UPCAST(errsink),
|
||||
ptrlen_from_asciz(item->text), 60);
|
||||
fputc('\n', stderr);
|
||||
break;
|
||||
case SDT_DISPLAY:
|
||||
fprintf(stderr, " %s\n", item->text);
|
||||
break;
|
||||
case SDT_SCARY_HEADING:
|
||||
/* Can't change font size or weight in this context */
|
||||
fprintf(stderr, "%s\n", item->text);
|
||||
break;
|
||||
case SDT_BATCH_ABORT:
|
||||
if (console_batch_mode) {
|
||||
fprintf(stderr, "%s\n", item->text);
|
||||
fflush(stderr);
|
||||
postmsg(&cf);
|
||||
return SPR_SW_ABORT("Cannot confirm a host key in batch mode");
|
||||
}
|
||||
break;
|
||||
case SDT_PROMPT:
|
||||
prompt = item->text;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fputs(intro, stderr);
|
||||
fflush(stderr);
|
||||
while (true) {
|
||||
fputs(prompt, stderr);
|
||||
fprintf(stderr,
|
||||
"%s (y/n, Return cancels connection, i for more info) ",
|
||||
prompt);
|
||||
fflush(stderr);
|
||||
|
||||
struct termios oldmode, newmode;
|
||||
@ -154,13 +164,22 @@ SeatPromptResult console_confirm_ssh_host_key(
|
||||
tcsetattr(0, TCSANOW, &oldmode);
|
||||
|
||||
if (line[0] == 'i' || line[0] == 'I') {
|
||||
fprintf(stderr, "Full public key:\n%s\n", keydisp);
|
||||
if (fingerprints[SSH_FPTYPE_SHA256])
|
||||
fprintf(stderr, "SHA256 key fingerprint:\n%s\n",
|
||||
fingerprints[SSH_FPTYPE_SHA256]);
|
||||
if (fingerprints[SSH_FPTYPE_MD5])
|
||||
fprintf(stderr, "MD5 key fingerprint:\n%s\n",
|
||||
fingerprints[SSH_FPTYPE_MD5]);
|
||||
for (SeatDialogTextItem *item = text->items,
|
||||
*end = item+text->nitems; item < end; item++) {
|
||||
switch (item->type) {
|
||||
case SDT_MORE_INFO_KEY:
|
||||
fprintf(stderr, "%s", item->text);
|
||||
break;
|
||||
case SDT_MORE_INFO_VALUE_SHORT:
|
||||
fprintf(stderr, ": %s\n", item->text);
|
||||
break;
|
||||
case SDT_MORE_INFO_VALUE_BLOB:
|
||||
fprintf(stderr, ":\n%s\n", item->text);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
Reference in New Issue
Block a user