diff options
author | Simon Tatham <anakin@pobox.com> | 2021-09-14 13:57:21 +0300 |
---|---|---|
committer | Simon Tatham <anakin@pobox.com> | 2021-09-14 15:19:33 +0300 |
commit | cd8a7181fddf42e14c44f024de71732bc2a6c715 (patch) | |
tree | 7e0d77b58988f347b614a24a25f8ba012ecf9922 /otherbackends | |
parent | 9f0e7d291558989cc44f98cf8603d5cf2787ce3a (diff) |
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.
Diffstat (limited to 'otherbackends')
-rw-r--r-- | otherbackends/rlogin.c | 69 |
1 files changed, 29 insertions, 40 deletions
diff --git a/otherbackends/rlogin.c b/otherbackends/rlogin.c index 11deb89c..09f1ab1d 100644 --- a/otherbackends/rlogin.c +++ b/otherbackends/rlogin.c @@ -35,6 +35,7 @@ struct Rlogin { static void rlogin_startup(Rlogin *rlogin, int prompt_result, const char *ruser); +static void rlogin_try_username_prompt(void *ctx); static void c_write(Rlogin *rlogin, const void *buf, size_t len) { @@ -80,18 +81,11 @@ static void rlogin_log(Plug *plug, PlugLogType type, SockAddr *addr, int port, rlogin->prompt->to_server = true; rlogin->prompt->from_server = false; rlogin->prompt->name = dupstr("Rlogin login name"); + rlogin->prompt->callback = rlogin_try_username_prompt; + rlogin->prompt->callback_ctx = rlogin; add_prompt(rlogin->prompt, dupstr("rlogin username: "), true); - - int ret = seat_get_userpass_input(rlogin->seat, rlogin->prompt, - NULL); - if (ret >= 0) { - /* Next terminal output will come from server */ - seat_set_trust_status(rlogin->seat, false); - rlogin_startup(rlogin, ret, prompt_get_result_ref( - rlogin->prompt->prompts[0])); - } + rlogin_try_username_prompt(rlogin); } - } } @@ -300,45 +294,36 @@ static void rlogin_reconfig(Backend *be, Conf *conf) { } +static void rlogin_try_username_prompt(void *ctx) +{ + Rlogin *rlogin = (Rlogin *)ctx; + + int ret = seat_get_userpass_input(rlogin->seat, rlogin->prompt); + if (ret < 0) + return; + + /* Next terminal output will come from server */ + seat_set_trust_status(rlogin->seat, false); + + /* Send the rlogin setup protocol data, and then we're ready to + * start receiving normal input to send down the wire, which + * rlogin_startup will signal to rlogin_sendok by nulling out + * rlogin->prompt. */ + rlogin_startup( + rlogin, ret, prompt_get_result_ref(rlogin->prompt->prompts[0])); +} + /* * Called to send data down the rlogin connection. */ static void rlogin_send(Backend *be, const char *buf, size_t len) { Rlogin *rlogin = container_of(be, Rlogin, backend); - bufchain bc; if (rlogin->s == NULL) return; - bufchain_init(&bc); - bufchain_add(&bc, buf, len); - - if (rlogin->prompt) { - /* - * We're still prompting for a username, and aren't talking - * directly to the network connection yet. - */ - int ret = seat_get_userpass_input(rlogin->seat, rlogin->prompt, &bc); - if (ret >= 0) { - /* Next terminal output will come from server */ - seat_set_trust_status(rlogin->seat, false); - rlogin_startup(rlogin, ret, prompt_get_result_ref( - rlogin->prompt->prompts[0])); - /* that nulls out rlogin->prompt, so then we'll start sending - * data down the wire in the obvious way */ - } - } - - if (!rlogin->prompt) { - while (bufchain_size(&bc) > 0) { - ptrlen data = bufchain_prefix(&bc); - rlogin->bufsize = sk_write(rlogin->s, data.ptr, data.len); - bufchain_consume(&bc, len); - } - } - - bufchain_clear(&bc); + rlogin->bufsize = sk_write(rlogin->s, buf, len); } /* @@ -398,8 +383,12 @@ static bool rlogin_connected(Backend *be) static bool rlogin_sendok(Backend *be) { + /* + * We only want to receive input data if the socket is connected + * and we're not still at the username prompt stage. + */ Rlogin *rlogin = container_of(be, Rlogin, backend); - return rlogin->socket_connected; + return rlogin->socket_connected && !rlogin->prompt; } static void rlogin_unthrottle(Backend *be, size_t backlog) |