mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-03-20 13:48:38 -05:00

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.
180 lines
7.7 KiB
C
180 lines
7.7 KiB
C
/*
|
|
* Abstraction of the various layers of SSH packet-level protocol,
|
|
* general enough to take in all three of the main SSH-2 layers and
|
|
* both of the SSH-1 phases.
|
|
*/
|
|
|
|
#ifndef PUTTY_SSHPPL_H
|
|
#define PUTTY_SSHPPL_H
|
|
|
|
typedef void (*packet_handler_fn_t)(PacketProtocolLayer *ppl, PktIn *pktin);
|
|
typedef struct PacketProtocolLayerVtable PacketProtocolLayerVtable;
|
|
|
|
struct PacketProtocolLayerVtable {
|
|
void (*free)(PacketProtocolLayer *);
|
|
void (*process_queue)(PacketProtocolLayer *ppl);
|
|
bool (*get_specials)(
|
|
PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx);
|
|
void (*special_cmd)(
|
|
PacketProtocolLayer *ppl, SessionSpecialCode code, int arg);
|
|
bool (*want_user_input)(PacketProtocolLayer *ppl);
|
|
void (*got_user_input)(PacketProtocolLayer *ppl);
|
|
void (*reconfigure)(PacketProtocolLayer *ppl, Conf *conf);
|
|
size_t (*queued_data_size)(PacketProtocolLayer *ppl);
|
|
|
|
/* Protocol-level name of this layer. */
|
|
const char *name;
|
|
};
|
|
|
|
struct PacketProtocolLayer {
|
|
const struct PacketProtocolLayerVtable *vt;
|
|
|
|
/* Link to the underlying SSH BPP. */
|
|
BinaryPacketProtocol *bpp;
|
|
|
|
/* Queue from which the layer receives its input packets, and one
|
|
* to put its output packets on. */
|
|
PktInQueue *in_pq;
|
|
PktOutQueue *out_pq;
|
|
|
|
/* Idempotent callback that in_pq will be linked to, causing a
|
|
* call to the process_queue method. in_pq points to this, so it
|
|
* will be automatically triggered by pushing things on the
|
|
* layer's input queue, but it can also be triggered on purpose. */
|
|
IdempotentCallback ic_process_queue;
|
|
|
|
/* Owner's pointer to this layer. Permits a layer to unilaterally
|
|
* abdicate in favour of a replacement, by overwriting this
|
|
* pointer and then freeing itself. */
|
|
PacketProtocolLayer **selfptr;
|
|
|
|
/* Bufchain of keyboard input from the user, for login prompts and
|
|
* similar. */
|
|
bufchain *user_input;
|
|
|
|
/* Logging and error-reporting facilities. */
|
|
LogContext *logctx;
|
|
Seat *seat; /* for dialog boxes, session output etc */
|
|
Ssh *ssh; /* for session termination + assorted connection-layer ops */
|
|
|
|
/* Known bugs in the remote implementation. */
|
|
unsigned remote_bugs;
|
|
};
|
|
|
|
static inline void ssh_ppl_process_queue(PacketProtocolLayer *ppl)
|
|
{ ppl->vt->process_queue(ppl); }
|
|
static inline bool ssh_ppl_get_specials(
|
|
PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
|
|
{ return ppl->vt->get_specials(ppl, add_special, ctx); }
|
|
static inline void ssh_ppl_special_cmd(
|
|
PacketProtocolLayer *ppl, SessionSpecialCode code, int arg)
|
|
{ ppl->vt->special_cmd(ppl, code, arg); }
|
|
static inline bool ssh_ppl_want_user_input(PacketProtocolLayer *ppl)
|
|
{ return ppl->vt->want_user_input(ppl); }
|
|
static inline void ssh_ppl_got_user_input(PacketProtocolLayer *ppl)
|
|
{ ppl->vt->got_user_input(ppl); }
|
|
static inline void ssh_ppl_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
|
|
{ ppl->vt->reconfigure(ppl, conf); }
|
|
static inline size_t ssh_ppl_queued_data_size(PacketProtocolLayer *ppl)
|
|
{ return ppl->vt->queued_data_size(ppl); }
|
|
|
|
/* ssh_ppl_free is more than just a macro wrapper on the vtable; it
|
|
* does centralised parts of the freeing too. */
|
|
void ssh_ppl_free(PacketProtocolLayer *ppl);
|
|
|
|
/* Helper routine to point a PPL at its input and output queues. Also
|
|
* sets up the IdempotentCallback on the input queue to trigger a call
|
|
* to process_queue whenever packets are added to it. */
|
|
void ssh_ppl_setup_queues(PacketProtocolLayer *ppl,
|
|
PktInQueue *inq, PktOutQueue *outq);
|
|
|
|
/* Routine a PPL can call to abdicate in favour of a replacement, by
|
|
* overwriting ppl->selfptr. Has the side effect of freeing 'old', so
|
|
* if 'old' actually called this (which is likely) then it should
|
|
* avoid dereferencing itself on return from this function! */
|
|
void ssh_ppl_replace(PacketProtocolLayer *old, PacketProtocolLayer *new);
|
|
|
|
/* Default implementation of queued_data_size, which just adds up the
|
|
* sizes of all the packets in pq_out. A layer can override this if it
|
|
* has other things to take into account as well. */
|
|
size_t ssh_ppl_default_queued_data_size(PacketProtocolLayer *ppl);
|
|
|
|
PacketProtocolLayer *ssh1_login_new(
|
|
Conf *conf, const char *host, int port,
|
|
PacketProtocolLayer *successor_layer);
|
|
PacketProtocolLayer *ssh1_connection_new(
|
|
Ssh *ssh, Conf *conf, ConnectionLayer **cl_out);
|
|
|
|
struct DataTransferStats;
|
|
struct ssh_connection_shared_gss_state;
|
|
PacketProtocolLayer *ssh2_transport_new(
|
|
Conf *conf, const char *host, int port, const char *fullhostname,
|
|
const char *client_greeting, const char *server_greeting,
|
|
struct ssh_connection_shared_gss_state *shgss,
|
|
struct DataTransferStats *stats, PacketProtocolLayer *higher_layer,
|
|
const SshServerConfig *ssc);
|
|
PacketProtocolLayer *ssh2_userauth_new(
|
|
PacketProtocolLayer *successor_layer,
|
|
const char *hostname, const char *fullhostname,
|
|
Filename *keyfile, bool show_banner, bool tryagent, bool notrivialauth,
|
|
const char *default_username, bool change_username,
|
|
bool try_ki_auth,
|
|
bool try_gssapi_auth, bool try_gssapi_kex_auth,
|
|
bool gssapi_fwd, struct ssh_connection_shared_gss_state *shgss);
|
|
PacketProtocolLayer *ssh2_connection_new(
|
|
Ssh *ssh, ssh_sharing_state *connshare, bool is_simple,
|
|
Conf *conf, const char *peer_verstring, ConnectionLayer **cl_out);
|
|
|
|
/* Can't put this in the userauth constructor without having a
|
|
* dependency loop at setup time (transport and userauth can't _both_
|
|
* be constructed second and given a pointer to the other). */
|
|
void ssh2_userauth_set_transport_layer(PacketProtocolLayer *userauth,
|
|
PacketProtocolLayer *transport);
|
|
|
|
/* Convenience macro for protocol layers to send formatted strings to
|
|
* the Event Log. Assumes a function parameter called 'ppl' is in
|
|
* scope. */
|
|
#define ppl_logevent(...) ( \
|
|
logevent_and_free((ppl)->logctx, dupprintf(__VA_ARGS__)))
|
|
|
|
/* Convenience macro for protocol layers to send formatted strings to
|
|
* the terminal. Also expects 'ppl' to be in scope. */
|
|
#define ppl_printf(...) \
|
|
ssh_ppl_user_output_string_and_free(ppl, dupprintf(__VA_ARGS__))
|
|
void ssh_ppl_user_output_string_and_free(PacketProtocolLayer *ppl, char *text);
|
|
|
|
/* Methods for userauth to communicate back to the transport layer */
|
|
ptrlen ssh2_transport_get_session_id(PacketProtocolLayer *ssh2_transport_ptr);
|
|
void ssh2_transport_notify_auth_done(PacketProtocolLayer *ssh2_transport_ptr);
|
|
|
|
/* Shared method between ssh2 layers (defined in transport2.c) to
|
|
* handle the common packets between login and connection: DISCONNECT,
|
|
* DEBUG and IGNORE. Those messages are handled by the ssh2transport
|
|
* layer if we have one, but in bare ssh2-connection mode they have to
|
|
* be handled by ssh2connection. */
|
|
bool ssh2_common_filter_queue(PacketProtocolLayer *ppl);
|
|
|
|
/* Method for making a prompts_t in such a way that it will install a
|
|
* callback that causes this PPL's process_queue method to be called
|
|
* when asynchronous prompt input completes. */
|
|
prompts_t *ssh_ppl_new_prompts(PacketProtocolLayer *ppl);
|
|
|
|
/* Methods for ssh1login to pass protocol flags to ssh1connection */
|
|
void ssh1_connection_set_protoflags(
|
|
PacketProtocolLayer *ppl, int local, int remote);
|
|
|
|
/* Shared get_specials method between the two ssh1 layers */
|
|
bool ssh1_common_get_specials(PacketProtocolLayer *, add_special_fn_t, void *);
|
|
|
|
/* Other shared functions between ssh1 layers */
|
|
bool ssh1_common_filter_queue(PacketProtocolLayer *ppl);
|
|
void ssh1_compute_session_id(
|
|
unsigned char *session_id, const unsigned char *cookie,
|
|
RSAKey *hostkey, RSAKey *servkey);
|
|
|
|
/* Method used by the SSH server */
|
|
void ssh2_transport_provide_hostkeys(PacketProtocolLayer *ssh2_transport_ptr,
|
|
ssh_key *const *hostkeys, int nhostkeys);
|
|
|
|
#endif /* PUTTY_SSHPPL_H */
|