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

Complete rework of terminal userpass input system.

The system for handling seat_get_userpass_input has always been
structured differently between GUI PuTTY and CLI tools like Plink.

In the CLI tools, password input is read directly from the OS
terminal/console device by console_get_userpass_input; this means that
you need to ensure the same terminal input data _hasn't_ already been
consumed by the main event loop and sent on to the backend. This is
achieved by the backend_sendok() method, which tells the event loop
when the backend has finished issuing password prompts, and hence,
when it's safe to start passing standard input to backend_send().

But in the GUI tools, input generated by the terminal window has
always been sent straight to backend_send(), regardless of whether
backend_sendok() says it wants it. So the terminal-based
implementation of username and password prompts has to work by
consuming input data that had _already_ been passed to the backend -
hence, any backend that needs to do that must keep its input on a
bufchain, and pass that bufchain to seat_get_userpass_input.

It's awkward that these two totally different systems coexist in the
first place. And now that SSH proxying needs to present interactive
prompts of its own, it's clear which one should win: the CLI style is
the Right Thing. So this change reworks the GUI side of the mechanism
to be more similar: terminal data now goes into a queue in the Ldisc,
and is not sent on to the backend until the backend says it's ready
for it via backend_sendok(). So terminal-based userpass prompts can
now consume data directly from that queue during the connection setup
stage.

As a result, the 'bufchain *' parameter has vanished from all the
userpass_input functions (both the official implementations of the
Seat trait method, and term_get_userpass_input() to which some of
those implementations delegate). The only function that actually used
that bufchain, namely term_get_userpass_input(), now instead reads
from the ldisc's input queue via a couple of new Ldisc functions.

(Not _trivial_ functions, since input buffered by Ldisc can be a
mixture of raw bytes and session specials like SS_EOL! The input queue
inside Ldisc is a bufchain containing a fiddly binary encoding that
can represent an arbitrary interleaving of those things.)

This greatly simplifies the calls to seat_get_userpass_input in
backends, which now don't have to mess about with passing their own
user_input bufchain around, or toggling their want_user_input flag
back and forth to request data to put on to that bufchain.

But the flip side is that now there has to be some _other_ method for
notifying the terminal when there's more input to be consumed during
an interactive prompt, and for notifying the backend when prompt input
has finished so that it can proceed to the next stage of the protocol.
This is done by a pair of extra callbacks: when more data is put on to
Ldisc's input queue, it triggers a call to term_get_userpass_input,
and when term_get_userpass_input finishes, it calls a callback
function provided in the prompts_t.

Therefore, any use of a prompts_t which *might* be asynchronous must
fill in the latter callback when setting up the prompts_t. In SSH, the
callback is centralised into a common PPL helper function, which
reinvokes the same PPL's process_queue coroutine; in rlogin we have to
set it up ourselves.

I'm sorry for this large and sprawling patch: I tried fairly hard to
break it up into individually comprehensible sub-patches, but I just
couldn't tease out any part of it that would stand sensibly alone.
This commit is contained in:
Simon Tatham
2021-09-14 11:57:21 +01:00
parent 9f0e7d2915
commit cd8a7181fd
22 changed files with 445 additions and 232 deletions

60
putty.h
View File

@ -741,6 +741,11 @@ extern const int be_default_protocol;
*/
extern const char *const appname;
/*
* Used by callback.c; declared up here so that prompts_t can use it
*/
typedef void (*toplevel_callback_fn_t)(void *ctx);
/*
* Mechanism for getting text strings such as usernames and passwords
* from the front-end.
@ -790,6 +795,17 @@ typedef struct {
prompt_t **prompts;
void *data; /* slot for housekeeping data, managed by
* seat_get_userpass_input(); initially NULL */
int idata; /* another slot private to the implementation */
/*
* Callback you can fill in to be notified when all the prompts'
* responses are available. After you receive this notification, a
* further call to the get_userpass_input function will return the
* final state of the prompts system, which is guaranteed not to
* be negative for 'still ongoing'.
*/
toplevel_callback_fn_t callback;
void *callback_ctx;
} prompts_t;
prompts_t *new_prompts(void);
void add_prompt(prompts_t *p, char *promptstr, bool echo);
@ -907,13 +923,7 @@ struct SeatVtable {
/*
* Try to get answers from a set of interactive login prompts. The
* prompts are provided in 'p'; the bufchain 'input' holds the
* data currently outstanding in the session's normal standard-
* input channel. Seats may implement this function by consuming
* data from 'input' (e.g. password prompts in GUI PuTTY,
* displayed in the same terminal as the subsequent session), or
* by doing something entirely different (e.g. directly
* interacting with standard I/O, or putting up a dialog box).
* prompts are provided in 'p'.
*
* A positive return value means that all prompts have had answers
* filled in. A zero return means that the user performed a
@ -939,7 +949,7 @@ struct SeatVtable {
* ever do want to move password prompts into a dialog box, I'll
* want a backend method for sending that notification.)
*/
int (*get_userpass_input)(Seat *seat, prompts_t *p, bufchain *input);
int (*get_userpass_input)(Seat *seat, prompts_t *p);
/*
* Notify the seat that the main session channel has been
@ -1155,9 +1165,8 @@ static inline bool seat_eof(Seat *seat)
{ return seat->vt->eof(seat); }
static inline void seat_sent(Seat *seat, size_t bufsize)
{ seat->vt->sent(seat, bufsize); }
static inline int seat_get_userpass_input(
Seat *seat, prompts_t *p, bufchain *input)
{ return seat->vt->get_userpass_input(seat, p, input); }
static inline int seat_get_userpass_input(Seat *seat, prompts_t *p)
{ return seat->vt->get_userpass_input(seat, p); }
static inline void seat_notify_session_started(Seat *seat)
{ seat->vt->notify_session_started(seat); }
static inline void seat_notify_remote_exit(Seat *seat)
@ -1233,7 +1242,7 @@ size_t nullseat_output(
Seat *seat, bool is_stderr, const void *data, size_t len);
bool nullseat_eof(Seat *seat);
void nullseat_sent(Seat *seat, size_t bufsize);
int nullseat_get_userpass_input(Seat *seat, prompts_t *p, bufchain *input);
int nullseat_get_userpass_input(Seat *seat, prompts_t *p);
void nullseat_notify_session_started(Seat *seat);
void nullseat_notify_remote_exit(Seat *seat);
void nullseat_notify_remote_disconnect(Seat *seat);
@ -1292,7 +1301,7 @@ bool console_can_set_trust_status(Seat *seat);
/*
* Other centralised seat functions.
*/
int filexfer_get_userpass_input(Seat *seat, prompts_t *p, bufchain *input);
int filexfer_get_userpass_input(Seat *seat, prompts_t *p);
bool cmdline_seat_verbose(Seat *seat);
/*
@ -1888,7 +1897,7 @@ void term_provide_backend(Terminal *term, Backend *backend);
void term_provide_logctx(Terminal *term, LogContext *logctx);
void term_set_focus(Terminal *term, bool has_focus);
char *term_get_ttymode(Terminal *term, const char *mode);
int term_get_userpass_input(Terminal *term, prompts_t *p, bufchain *input);
int term_get_userpass_input(Terminal *term, prompts_t *p);
void term_set_trust_status(Terminal *term, bool trusted);
void term_keyinput(Terminal *, int codepage, const void *buf, int len);
void term_keyinputw(Terminal *, const wchar_t * widebuf, int len);
@ -2055,6 +2064,28 @@ void ldisc_configure(Ldisc *, Conf *);
void ldisc_free(Ldisc *);
void ldisc_send(Ldisc *, const void *buf, int len, bool interactive);
void ldisc_echoedit_update(Ldisc *);
typedef struct LdiscInputToken {
/*
* Structure that encodes any single item of data that Ldisc can
* buffer: either a single character of raw data, or a session
* special.
*/
bool is_special;
union {
struct {
/* if is_special == false */
char chr;
};
struct {
/* if is_special == true */
SessionSpecialCode code;
int arg;
};
};
} LdiscInputToken;
bool ldisc_has_input_buffered(Ldisc *);
LdiscInputToken ldisc_get_input_token(Ldisc *); /* asserts there is input */
void ldisc_enable_prompt_callback(Ldisc *, prompts_t *);
void ldisc_check_sendok(Ldisc *);
/*
@ -2472,7 +2503,6 @@ unsigned long timing_last_clock(void);
* loop, as in PSFTP, for example - if a callback has run then perhaps
* it might have done whatever the loop's caller was waiting for.
*/
typedef void (*toplevel_callback_fn_t)(void *ctx);
void queue_toplevel_callback(toplevel_callback_fn_t fn, void *ctx);
bool run_toplevel_callbacks(void);
bool toplevel_callback_pending(void);