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:
@ -3454,7 +3454,7 @@ struct confirm_ssh_host_key_dialog_ctx {
|
||||
char *keytype;
|
||||
char *keystr;
|
||||
char *more_info;
|
||||
void (*callback)(void *callback_ctx, int result);
|
||||
void (*callback)(void *callback_ctx, SeatPromptResult result);
|
||||
void *callback_ctx;
|
||||
Seat *seat;
|
||||
|
||||
@ -3468,7 +3468,7 @@ static void confirm_ssh_host_key_result_callback(void *vctx, int result)
|
||||
(struct confirm_ssh_host_key_dialog_ctx *)vctx;
|
||||
|
||||
if (result >= 0) {
|
||||
int logical_result;
|
||||
SeatPromptResult logical_result;
|
||||
|
||||
/*
|
||||
* Convert the dialog-box return value (one of three
|
||||
@ -3478,11 +3478,11 @@ static void confirm_ssh_host_key_result_callback(void *vctx, int result)
|
||||
*/
|
||||
if (result == 2) {
|
||||
store_host_key(ctx->host, ctx->port, ctx->keytype, ctx->keystr);
|
||||
logical_result = 1; /* continue with connection */
|
||||
logical_result = SPR_OK;
|
||||
} else if (result == 1) {
|
||||
logical_result = 1; /* continue with connection */
|
||||
logical_result = SPR_OK;
|
||||
} else {
|
||||
logical_result = 0; /* do not continue with connection */
|
||||
logical_result = SPR_USER_ABORT;
|
||||
}
|
||||
|
||||
ctx->callback(ctx->callback_ctx, logical_result);
|
||||
@ -3539,10 +3539,10 @@ static void more_info_button_clicked(GtkButton *button, gpointer vctx)
|
||||
&buttons_ok, more_info_closed, ctx);
|
||||
}
|
||||
|
||||
int gtk_seat_confirm_ssh_host_key(
|
||||
SeatPromptResult gtk_seat_confirm_ssh_host_key(
|
||||
Seat *seat, const char *host, int port, const char *keytype,
|
||||
char *keystr, const char *keydisp, char **fingerprints, bool mismatch,
|
||||
void (*callback)(void *ctx, int result), void *ctx)
|
||||
void (*callback)(void *ctx, SeatPromptResult result), void *ctx)
|
||||
{
|
||||
static const char absenttxt[] =
|
||||
"The host key is not cached for this server:\n\n"
|
||||
@ -3641,25 +3641,28 @@ int gtk_seat_confirm_ssh_host_key(
|
||||
|
||||
sfree(text);
|
||||
|
||||
return -1; /* dialog still in progress */
|
||||
return SPR_INCOMPLETE; /* dialog still in progress */
|
||||
}
|
||||
|
||||
struct simple_prompt_result_ctx {
|
||||
void (*callback)(void *callback_ctx, int result);
|
||||
struct simple_prompt_result_spr_ctx {
|
||||
void (*callback)(void *callback_ctx, SeatPromptResult spr);
|
||||
void *callback_ctx;
|
||||
Seat *seat;
|
||||
enum DialogSlot dialog_slot;
|
||||
};
|
||||
|
||||
static void simple_prompt_result_callback(void *vctx, int result)
|
||||
static void simple_prompt_result_spr_callback(void *vctx, int result)
|
||||
{
|
||||
struct simple_prompt_result_ctx *ctx =
|
||||
(struct simple_prompt_result_ctx *)vctx;
|
||||
struct simple_prompt_result_spr_ctx *ctx =
|
||||
(struct simple_prompt_result_spr_ctx *)vctx;
|
||||
|
||||
unregister_dialog(ctx->seat, ctx->dialog_slot);
|
||||
|
||||
if (result >= 0)
|
||||
ctx->callback(ctx->callback_ctx, result);
|
||||
if (result == 0)
|
||||
ctx->callback(ctx->callback_ctx, SPR_USER_ABORT);
|
||||
else if (result > 0)
|
||||
ctx->callback(ctx->callback_ctx, SPR_OK);
|
||||
/* if <0, we're cleaning up for some other reason */
|
||||
|
||||
/*
|
||||
* Clean up this context structure, whether or not a result was
|
||||
@ -3672,9 +3675,9 @@ static void simple_prompt_result_callback(void *vctx, int result)
|
||||
* Ask whether the selected algorithm is acceptable (since it was
|
||||
* below the configured 'warn' threshold).
|
||||
*/
|
||||
int gtk_seat_confirm_weak_crypto_primitive(
|
||||
SeatPromptResult gtk_seat_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)
|
||||
{
|
||||
static const char msg[] =
|
||||
"The first %s supported by the server is "
|
||||
@ -3682,12 +3685,12 @@ int gtk_seat_confirm_weak_crypto_primitive(
|
||||
"Continue with connection?";
|
||||
|
||||
char *text;
|
||||
struct simple_prompt_result_ctx *result_ctx;
|
||||
struct simple_prompt_result_spr_ctx *result_ctx;
|
||||
GtkWidget *mainwin, *msgbox;
|
||||
|
||||
text = dupprintf(msg, algtype, algname);
|
||||
|
||||
result_ctx = snew(struct simple_prompt_result_ctx);
|
||||
result_ctx = snew(struct simple_prompt_result_spr_ctx);
|
||||
result_ctx->callback = callback;
|
||||
result_ctx->callback_ctx = ctx;
|
||||
result_ctx->seat = seat;
|
||||
@ -3697,17 +3700,17 @@ int gtk_seat_confirm_weak_crypto_primitive(
|
||||
msgbox = create_message_box(
|
||||
mainwin, "PuTTY Security Alert", text,
|
||||
string_width("Reasonably long line of text as a width template"),
|
||||
false, &buttons_yn, simple_prompt_result_callback, result_ctx);
|
||||
false, &buttons_yn, simple_prompt_result_spr_callback, result_ctx);
|
||||
register_dialog(seat, result_ctx->dialog_slot, msgbox);
|
||||
|
||||
sfree(text);
|
||||
|
||||
return -1; /* dialog still in progress */
|
||||
return SPR_INCOMPLETE;
|
||||
}
|
||||
|
||||
int gtk_seat_confirm_weak_cached_hostkey(
|
||||
SeatPromptResult gtk_seat_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)
|
||||
{
|
||||
static const char msg[] =
|
||||
"The first host key type we have stored for this server\n"
|
||||
@ -3718,12 +3721,12 @@ int gtk_seat_confirm_weak_cached_hostkey(
|
||||
"Continue with connection?";
|
||||
|
||||
char *text;
|
||||
struct simple_prompt_result_ctx *result_ctx;
|
||||
struct simple_prompt_result_spr_ctx *result_ctx;
|
||||
GtkWidget *mainwin, *msgbox;
|
||||
|
||||
text = dupprintf(msg, algname, betteralgs);
|
||||
|
||||
result_ctx = snew(struct simple_prompt_result_ctx);
|
||||
result_ctx = snew(struct simple_prompt_result_spr_ctx);
|
||||
result_ctx->callback = callback;
|
||||
result_ctx->callback_ctx = ctx;
|
||||
result_ctx->seat = seat;
|
||||
@ -3734,12 +3737,12 @@ int gtk_seat_confirm_weak_cached_hostkey(
|
||||
mainwin, "PuTTY Security Alert", text,
|
||||
string_width("is ecdsa-nistp521, which is below the configured"
|
||||
" warning threshold."),
|
||||
false, &buttons_yn, simple_prompt_result_callback, result_ctx);
|
||||
false, &buttons_yn, simple_prompt_result_spr_callback, result_ctx);
|
||||
register_dialog(seat, result_ctx->dialog_slot, msgbox);
|
||||
|
||||
sfree(text);
|
||||
|
||||
return -1; /* dialog still in progress */
|
||||
return SPR_INCOMPLETE;
|
||||
}
|
||||
|
||||
void old_keyfile_warning(void)
|
||||
@ -4149,6 +4152,30 @@ void logevent_dlg(eventlog_stuff *es, const char *string)
|
||||
}
|
||||
}
|
||||
|
||||
struct simple_prompt_result_int_ctx {
|
||||
void (*callback)(void *callback_ctx, int result);
|
||||
void *callback_ctx;
|
||||
Seat *seat;
|
||||
enum DialogSlot dialog_slot;
|
||||
};
|
||||
|
||||
static void simple_prompt_result_int_callback(void *vctx, int result)
|
||||
{
|
||||
struct simple_prompt_result_int_ctx *ctx =
|
||||
(struct simple_prompt_result_int_ctx *)vctx;
|
||||
|
||||
unregister_dialog(ctx->seat, ctx->dialog_slot);
|
||||
|
||||
if (result >= 0)
|
||||
ctx->callback(ctx->callback_ctx, result);
|
||||
|
||||
/*
|
||||
* Clean up this context structure, whether or not a result was
|
||||
* ever actually delivered from the dialog box.
|
||||
*/
|
||||
sfree(ctx);
|
||||
}
|
||||
|
||||
int gtkdlg_askappend(Seat *seat, Filename *filename,
|
||||
void (*callback)(void *ctx, int result), void *ctx)
|
||||
{
|
||||
@ -4168,13 +4195,13 @@ int gtkdlg_askappend(Seat *seat, Filename *filename,
|
||||
|
||||
char *message;
|
||||
char *mbtitle;
|
||||
struct simple_prompt_result_ctx *result_ctx;
|
||||
struct simple_prompt_result_int_ctx *result_ctx;
|
||||
GtkWidget *mainwin, *msgbox;
|
||||
|
||||
message = dupprintf(msgtemplate, FILENAME_MAX, filename->path);
|
||||
mbtitle = dupprintf("%s Log to File", appname);
|
||||
|
||||
result_ctx = snew(struct simple_prompt_result_ctx);
|
||||
result_ctx = snew(struct simple_prompt_result_int_ctx);
|
||||
result_ctx->callback = callback;
|
||||
result_ctx->callback_ctx = ctx;
|
||||
result_ctx->seat = seat;
|
||||
@ -4184,7 +4211,7 @@ int gtkdlg_askappend(Seat *seat, Filename *filename,
|
||||
msgbox = create_message_box(
|
||||
mainwin, mbtitle, message,
|
||||
string_width("LINE OF TEXT SUITABLE FOR THE ASKAPPEND WIDTH"),
|
||||
false, &buttons_append, simple_prompt_result_callback, result_ctx);
|
||||
false, &buttons_append, simple_prompt_result_int_callback, result_ctx);
|
||||
register_dialog(seat, result_ctx->dialog_slot, msgbox);
|
||||
|
||||
sfree(message);
|
||||
|
Reference in New Issue
Block a user