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

Richer data type for interactive prompt results.

All the seat functions that request an interactive prompt of some kind
to the user - both the main seat_get_userpass_input and the various
confirmation dialogs for things like host keys - were using a simple
int return value, with the general semantics of 0 = "fail", 1 =
"proceed" (and in the case of seat_get_userpass_input, answers to the
prompts were provided), and -1 = "request in progress, wait for a
callback".

In this commit I change all those functions' return types to a new
struct called SeatPromptResult, whose primary field is an enum
replacing those simple integer values.

The main purpose is that the enum has not three but _four_ values: the
"fail" result has been split into 'user abort' and 'software abort'.
The distinction is that a user abort occurs as a result of an
interactive UI action, such as the user clicking 'cancel' in a dialog
box or hitting ^D or ^C at a terminal password prompt - and therefore,
there's no need to display an error message telling the user that the
interactive operation has failed, because the user already knows,
because they _did_ it. 'Software abort' is from any other cause, where
PuTTY is the first to know there was a problem, and has to tell the
user.

We already had this 'user abort' vs 'software abort' distinction in
other parts of the code - the SSH backend has separate termination
functions which protocol layers can call. But we assumed that any
failure from an interactive prompt request fell into the 'user abort'
category, which is not true. A couple of examples: if you configure a
host key fingerprint in your saved session via the SSH > Host keys
pane, and the server presents a host key that doesn't match it, then
verify_ssh_host_key would report that the user had aborted the
connection, and feel no need to tell the user what had gone wrong!
Similarly, if a password provided on the command line was not
accepted, then (after I fixed the semantics of that in the previous
commit) the same wrong handling would occur.

So now, those Seat prompt functions too can communicate whether the
user or the software originated a connection abort. And in the latter
case, we also provide an error message to present to the user. Result:
in those two example cases (and others), error messages should no
longer go missing.

Implementation note: to avoid the hassle of having the error message
in a SeatPromptResult being a dynamically allocated string (and hence,
every recipient of one must always check whether it's non-NULL and
free it on every exit path, plus being careful about copying the
struct around), I've instead arranged that the structure contains a
function pointer and a couple of parameters, so that the string form
of the message can be constructed on demand. That way, the only users
who need to free it are the ones who actually _asked_ for it in the
first place, which is a much smaller set.

(This is one of the rare occasions that I regret not having C++'s
extra features available in this code base - a unique_ptr or
shared_ptr to a string would have been just the thing here, and the
compiler would have done all the hard work for me of remembering where
to insert the frees!)
This commit is contained in:
Simon Tatham
2021-12-28 17:52:00 +00:00
parent a82ab70b0b
commit a2ff884512
51 changed files with 648 additions and 372 deletions

View File

@ -655,12 +655,12 @@ static void proxy_http_process_queue(ProxyNegotiator *pn)
add_prompt(s->prompts, dupstr("Proxy password: "), false);
while (true) {
int prompt_result = seat_get_userpass_input(
SeatPromptResult spr = seat_get_userpass_input(
interactor_announce(pn->itr), s->prompts);
if (prompt_result > 0) {
if (spr.kind == SPRK_OK) {
break;
} else if (prompt_result == 0) {
pn->aborted = true;
} else if (spr_is_abort(spr)) {
proxy_spr_abort(pn, spr);
crStopV;
}
crReturnV;

View File

@ -149,16 +149,22 @@ static void local_proxy_opener_coroutine(void *vctx)
}
while (true) {
int prompt_result = seat_get_userpass_input(
SeatPromptResult spr = seat_get_userpass_input(
interactor_announce(&lp->interactor), lp->prompts);
if (prompt_result > 0) {
if (spr.kind == SPRK_OK) {
break;
} else if (prompt_result == 0) {
} else if (spr.kind == SPRK_USER_ABORT) {
local_proxy_opener_cleanup_interactor(lp);
plug_closing_user_abort(lp->plug);
/* That will have freed us, so we must just return
* without calling any crStop */
return;
} else if (spr.kind == SPRK_SW_ABORT) {
local_proxy_opener_cleanup_interactor(lp);
char *err = spr_get_error_message(spr);
plug_closing_error(lp->plug, err);
sfree(err);
return; /* without crStop, as above */
}
crReturnV;
}

View File

@ -463,6 +463,16 @@ prompts_t *proxy_new_prompts(ProxySocket *ps)
return prs;
}
void proxy_spr_abort(ProxyNegotiator *pn, SeatPromptResult spr)
{
if (spr.kind == SPRK_SW_ABORT) {
pn->error = spr_get_error_message(spr);
} else {
assert(spr.kind == SPRK_USER_ABORT);
pn->aborted = true;
}
}
Socket *new_connection(SockAddr *addr, const char *hostname,
int port, bool privport,
bool oobinline, bool nodelay, bool keepalive,

View File

@ -91,10 +91,11 @@ extern const ProxyNegotiatorVT socks5_proxy_negotiator_vt;
extern const ProxyNegotiatorVT telnet_proxy_negotiator_vt;
/*
* Centralised function to allow ProxyNegotiators to get hold of a
* prompts_t.
* Centralised functions to allow ProxyNegotiators to get hold of a
* prompts_t, and to deal with SeatPromptResults coming back.
*/
prompts_t *proxy_new_prompts(ProxySocket *ps);
void proxy_spr_abort(ProxyNegotiator *pn, SeatPromptResult spr);
/*
* This may be reused by local-command proxies on individual

View File

@ -191,12 +191,12 @@ static void proxy_socks5_process_queue(ProxyNegotiator *pn)
}
while (true) {
int prompt_result = seat_get_userpass_input(
SeatPromptResult spr = seat_get_userpass_input(
interactor_announce(pn->itr), s->prompts);
if (prompt_result > 0) {
if (spr.kind == SPRK_OK) {
break;
} else if (prompt_result == 0) {
pn->aborted = true;
} else if (spr_is_abort(spr)) {
proxy_spr_abort(pn, spr);
crStopV;
}
crReturnV;

View File

@ -330,7 +330,7 @@ static void sshproxy_notify_remote_disconnect(Seat *seat)
queue_toplevel_callback(sshproxy_notify_remote_disconnect_callback, sp);
}
static int sshproxy_get_userpass_input(Seat *seat, prompts_t *p)
static SeatPromptResult sshproxy_get_userpass_input(Seat *seat, prompts_t *p)
{
SshProxy *sp = container_of(seat, SshProxy, seat);
@ -349,7 +349,8 @@ static int sshproxy_get_userpass_input(Seat *seat, prompts_t *p)
*/
sshproxy_error(sp, "Unable to provide interactive authentication "
"requested by proxy SSH connection");
return 0;
return SPR_SW_ABORT("Noninteractive SSH proxy cannot perform "
"interactive authentication");
}
static void sshproxy_connection_fatal_callback(void *vctx)
@ -368,10 +369,10 @@ static void sshproxy_connection_fatal(Seat *seat, const char *message)
}
}
static int sshproxy_confirm_ssh_host_key(
static SeatPromptResult sshproxy_confirm_ssh_host_key(
Seat *seat, const char *host, int port, const char *keytype,
char *keystr, const char *keydisp, char **key_fingerprints, bool mismatch,
void (*callback)(void *ctx, int result), void *ctx)
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
{
SshProxy *sp = container_of(seat, SshProxy, seat);
@ -390,12 +391,12 @@ static int sshproxy_confirm_ssh_host_key(
* option in the absence of interactive confirmation, i.e. abort
* the connection.
*/
return 0;
return SPR_SW_ABORT("Noninteractive SSH proxy cannot confirm host key");
}
static int sshproxy_confirm_weak_crypto_primitive(
static SeatPromptResult sshproxy_confirm_weak_crypto_primitive(
Seat *seat, const char *algtype, const char *algname,
void (*callback)(void *ctx, int result), void *ctx)
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
{
SshProxy *sp = container_of(seat, SshProxy, seat);
@ -415,12 +416,13 @@ static int sshproxy_confirm_weak_crypto_primitive(
sshproxy_error(sp, "First %s supported by server is %s, below warning "
"threshold. Abandoning proxy SSH connection.",
algtype, algname);
return 0;
return SPR_SW_ABORT("Noninteractive SSH proxy cannot confirm "
"weak crypto primitive");
}
static int sshproxy_confirm_weak_cached_hostkey(
static SeatPromptResult sshproxy_confirm_weak_cached_hostkey(
Seat *seat, const char *algname, const char *betteralgs,
void (*callback)(void *ctx, int result), void *ctx)
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
{
SshProxy *sp = container_of(seat, SshProxy, seat);
@ -440,7 +442,8 @@ static int sshproxy_confirm_weak_cached_hostkey(
sshproxy_error(sp, "First host key type stored for server is %s, below "
"warning threshold. Abandoning proxy SSH connection.",
algname);
return 0;
return SPR_SW_ABORT("Noninteractive SSH proxy cannot confirm "
"weak cached host key");
}
static StripCtrlChars *sshproxy_stripctrl_new(

View File

@ -288,12 +288,12 @@ static void proxy_telnet_process_queue(ProxyNegotiator *pn)
crReturnV;
while (true) {
int prompt_result = seat_get_userpass_input(
SeatPromptResult spr = seat_get_userpass_input(
interactor_announce(pn->itr), s->prompts);
if (prompt_result > 0) {
if (spr.kind == SPRK_OK) {
break;
} else if (prompt_result == 0) {
pn->aborted = true;
} else if (spr_is_abort(spr)) {
proxy_spr_abort(pn, spr);
crStopV;
}
crReturnV;