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

Framework for announcing which Interactor is talking.

All this Interactor business has been gradually working towards being
able to inform the user _which_ network connection is currently
presenting them with a password prompt (or whatever), in situations
where more than one of them might be, such as an SSH connection being
used as a proxy for another SSH connection when neither one has
one-touch login configured.

At some point, we have to arrange that any attempt to do a user
interaction during connection setup - be it a password prompt, a host
key confirmation dialog, or just displaying an SSH login banner -
makes it clear which host it's come from. That's going to mean calling
some kind of announcement function before doing any of those things.

But there are several of those functions in the Seat API, and calls to
them are scattered far and wide across the SSH backend. (And not even
just there - the Rlogin backend also uses seat_get_userpass_input).
How can we possibly make sure we don't forget a vital call site on
some obscure little-tested code path, and leave the user confused in
just that one case which nobody might notice for years?

Today I thought of a trick to solve that problem. We can use the C
type system to enforce it for us!

The plan is: we invent a new struct type which contains nothing but a
'Seat *'. Then, for every Seat method which does a thing that ought to
be clearly identified as relating to a particular Interactor, we
adjust the API for that function to take the new struct type where it
previously took a plain 'Seat *'. Or rather - doing less violence to
the existing code - we only need to adjust the API of the dispatch
functions inline in putty.h.

How does that help? Because the way you _get_ one of these
struct-wrapped Seat pointers is by calling interactor_announce() on
your Interactor, which will in turn call interactor_get_seat(), and
wrap the returned pointer into one of these structs.

The effect is that whenever the SSH (or Rlogin) code wants to call one
of those particular Seat methods, it _has_ to call
interactor_announce() just beforehand, which (once I finish all of
this) will make sure the user is aware of who is presenting the prompt
or banner or whatever. And you can't forget to call it, because if you
don't call it, then you just don't have a struct of the right type to
give to the Seat method you wanted to call!

(Of course, there's nothing stopping code from _deliberately_ taking a
Seat * it already has and wrapping it into the new struct. In fact
SshProxy has to do that, in order to forward these requests up the
chain of Seats. But the point is that you can't do it _by accident_,
just by forgetting to make a vital function call - when you do that,
you _know_ you're doing it on purpose.)

No functional change: the new interactor_announce() function exists,
and the type-system trick ensures it's called in all the right places,
but it doesn't actually _do_ anything yet.
This commit is contained in:
Simon Tatham
2021-10-30 18:05:36 +01:00
parent 89a390bdeb
commit f00c72cc2a
18 changed files with 125 additions and 64 deletions

18
proxy/interactor.c Normal file
View File

@ -0,0 +1,18 @@
/*
* Centralised functions for the Interactor trait.
*/
#include "putty.h"
InteractionReadySeat interactor_announce(Interactor *itr)
{
Seat *seat = interactor_get_seat(itr);
/* TODO: print an announcement of this Interactor's identity, when
* appropriate */
InteractionReadySeat iseat;
iseat.seat = seat;
return iseat;
}

View File

@ -267,6 +267,13 @@ static size_t sshproxy_output(Seat *seat, SeatOutputType type,
return bufchain_size(&sp->ssh_to_socket);
}
static inline InteractionReadySeat wrap(Seat *seat)
{
InteractionReadySeat iseat;
iseat.seat = seat;
return iseat;
}
static size_t sshproxy_banner(Seat *seat, const void *data, size_t len)
{
SshProxy *sp = container_of(seat, SshProxy, seat);
@ -275,7 +282,7 @@ static size_t sshproxy_banner(Seat *seat, const void *data, size_t len)
* If we have access to the outer Seat, pass the SSH login
* banner on to it.
*/
return seat_banner(sp->clientseat, data, len);
return seat_banner(wrap(sp->clientseat), data, len);
} else {
return 0;
}
@ -311,7 +318,7 @@ static int sshproxy_get_userpass_input(Seat *seat, prompts_t *p)
* If we have access to the outer Seat, pass this prompt
* request on to it. FIXME: appropriately adjusted
*/
return seat_get_userpass_input(sp->clientseat, p);
return seat_get_userpass_input(wrap(sp->clientseat), p);
}
/*
@ -353,7 +360,7 @@ static int sshproxy_confirm_ssh_host_key(
* request on to it. FIXME: appropriately adjusted
*/
return seat_confirm_ssh_host_key(
sp->clientseat, host, port, keytype, keystr, keydisp,
wrap(sp->clientseat), host, port, keytype, keystr, keydisp,
key_fingerprints, mismatch, callback, ctx);
}
@ -377,7 +384,7 @@ static int sshproxy_confirm_weak_crypto_primitive(
* request on to it. FIXME: appropriately adjusted
*/
return seat_confirm_weak_crypto_primitive(
sp->clientseat, algtype, algname, callback, ctx);
wrap(sp->clientseat), algtype, algname, callback, ctx);
}
/*
@ -402,7 +409,7 @@ static int sshproxy_confirm_weak_cached_hostkey(
* request on to it. FIXME: appropriately adjusted
*/
return seat_confirm_weak_cached_hostkey(
sp->clientseat, algname, betteralgs, callback, ctx);
wrap(sp->clientseat), algname, betteralgs, callback, ctx);
}
/*