1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 03:22:48 -05:00

New abstraction 'Seat', to pass to backends.

This is a new vtable-based abstraction which is passed to a backend in
place of Frontend, and it implements only the subset of the Frontend
functions needed by a backend. (Many other Frontend functions still
exist, notably the wide range of things called by terminal.c providing
platform-independent operations on the GUI terminal window.)

The purpose of making it a vtable is that this opens up the
possibility of creating a backend as an internal implementation detail
of some other activity, by providing just that one backend with a
custom Seat that implements the methods differently.

For example, this refactoring should make it feasible to directly
implement an SSH proxy type, aka the 'jump host' feature supported by
OpenSSH, aka 'open a secondary SSH session in MAINCHAN_DIRECT_TCP
mode, and then expose the main channel of that as the Socket for the
primary connection'. (Which of course you can already do by spawning
'plink -nc' as a separate proxy process, but this would permit it in
the _same_ process without anything getting confused.)

I've centralised a full set of stub methods in misc.c for the new
abstraction, which allows me to get rid of several annoying stubs in
the previous code. Also, while I'm here, I've moved a lot of
duplicated modalfatalbox() type functions from application main
program files into wincons.c / uxcons.c, which I think saves
duplication overall. (A minor visible effect is that the prefixes on
those console-based fatal error messages will now be more consistent
between applications.)
This commit is contained in:
Simon Tatham
2018-10-11 19:58:42 +01:00
parent 109df9f46b
commit b4c8fd9d86
42 changed files with 1046 additions and 719 deletions

View File

@ -28,21 +28,65 @@ void cleanup_exit(int code)
exit(code);
}
void set_busy_status(Frontend *frontend, int status)
/*
* Various error message and/or fatal exit functions.
*/
void console_print_error_msg(const char *prefix, const char *msg)
{
fputs(prefix, stderr);
fputs(": ", stderr);
fputs(msg, stderr);
fputc('\n', stderr);
fflush(stderr);
}
void notify_remote_exit(Frontend *frontend)
void console_print_error_msg_fmt_v(
const char *prefix, const char *fmt, va_list ap)
{
char *msg = dupvprintf(fmt, ap);
console_print_error_msg(prefix, msg);
sfree(msg);
}
void console_print_error_msg_fmt(const char *prefix, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
console_print_error_msg_fmt_v(prefix, fmt, ap);
va_end(ap);
}
void modalfatalbox(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
console_print_error_msg_fmt_v("FATAL ERROR", fmt, ap);
va_end(ap);
cleanup_exit(1);
}
void nonfatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
console_print_error_msg_fmt_v("ERROR", fmt, ap);
va_end(ap);
}
void console_connection_fatal(Seat *seat, const char *msg)
{
console_print_error_msg("FATAL ERROR", msg);
cleanup_exit(1);
}
void timer_change_notify(unsigned long next)
{
}
int verify_ssh_host_key(Frontend *frontend, char *host, int port,
const char *keytype, char *keystr, char *fingerprint,
void (*callback)(void *ctx, int result), void *ctx)
int console_verify_ssh_host_key(
Seat *seat, const char *host, int port,
const char *keytype, char *keystr, char *fingerprint,
void (*callback)(void *ctx, int result), void *ctx)
{
int ret;
HANDLE hin;
@ -145,16 +189,9 @@ int verify_ssh_host_key(Frontend *frontend, char *host, int port,
}
}
void update_specials_menu(Frontend *frontend)
{
}
/*
* Ask whether the selected algorithm is acceptable (since it was
* below the configured 'warn' threshold).
*/
int askalg(Frontend *frontend, const char *algtype, const char *algname,
void (*callback)(void *ctx, int result), void *ctx)
int console_confirm_weak_crypto_primitive(
Seat *seat, const char *algtype, const char *algname,
void (*callback)(void *ctx, int result), void *ctx)
{
HANDLE hin;
DWORD savemode, i;
@ -194,8 +231,9 @@ int askalg(Frontend *frontend, const char *algtype, const char *algname,
}
}
int askhk(Frontend *frontend, const char *algname, const char *betteralgs,
void (*callback)(void *ctx, int result), void *ctx)
int console_confirm_weak_cached_hostkey(
Seat *seat, const char *algname, const char *betteralgs,
void (*callback)(void *ctx, int result), void *ctx)
{
HANDLE hin;
DWORD savemode, i;

View File

@ -798,10 +798,11 @@ static void win_gui_eventlog(LogPolicy *lp, const char *string)
static void win_gui_logging_error(LogPolicy *lp, const char *event)
{
extern Seat win_seat[1];
/* Send 'can't open log file' errors to the terminal window.
* (Marked as stderr, although terminal.c won't care.) */
from_backend(NULL, 1, event, strlen(event));
from_backend(NULL, 1, "\r\n", 2);
seat_stderr(win_seat, event, strlen(event));
seat_stderr(win_seat, "\r\n", 2);
}
void showeventlog(HWND hwnd)
@ -819,9 +820,10 @@ void showabout(HWND hwnd)
DialogBox(hinst, MAKEINTRESOURCE(IDD_ABOUTBOX), hwnd, AboutProc);
}
int verify_ssh_host_key(Frontend *frontend, char *host, int port,
const char *keytype, char *keystr, char *fingerprint,
void (*callback)(void *ctx, int result), void *ctx)
int win_seat_verify_ssh_host_key(
Seat *seat, const char *host, int port,
const char *keytype, char *keystr, char *fingerprint,
void (*callback)(void *ctx, int result), void *ctx)
{
int ret;
@ -903,8 +905,9 @@ int verify_ssh_host_key(Frontend *frontend, char *host, int port,
* Ask whether the selected algorithm is acceptable (since it was
* below the configured 'warn' threshold).
*/
int askalg(Frontend *frontend, const char *algtype, const char *algname,
void (*callback)(void *ctx, int result), void *ctx)
int win_seat_confirm_weak_crypto_primitive(
Seat *seat, const char *algtype, const char *algname,
void (*callback)(void *ctx, int result), void *ctx)
{
static const char mbtitle[] = "%s Security Alert";
static const char msg[] =
@ -928,8 +931,9 @@ int askalg(Frontend *frontend, const char *algtype, const char *algname,
return 0;
}
int askhk(Frontend *frontend, const char *algname, const char *betteralgs,
void (*callback)(void *ctx, int result), void *ctx)
int win_seat_confirm_weak_cached_hostkey(
Seat *seat, const char *algname, const char *betteralgs,
void (*callback)(void *ctx, int result), void *ctx)
{
static const char mbtitle[] = "%s Security Alert";
static const char msg[] =

View File

@ -213,7 +213,7 @@ static Mouse_Button lastbtn;
static int send_raw_mouse = 0;
static int wheel_accumulator = 0;
static int busy_status = BUSY_NOT;
static BusyStatus busy_status = BUSY_NOT;
static char *window_name, *icon_name;
@ -230,21 +230,57 @@ static UINT wm_mousewheel = WM_MOUSEWHEEL;
const int share_can_be_downstream = TRUE;
const int share_can_be_upstream = TRUE;
/* Dummy routine, only required in plink. */
void frontend_echoedit_update(Frontend *frontend, int echo, int edit)
{
}
int frontend_is_utf8(Frontend *frontend)
{
return ucsdata.line_codepage == CP_UTF8;
}
char *get_ttymode(Frontend *frontend, const char *mode)
static int win_seat_is_utf8(Seat *seat)
{
return frontend_is_utf8(NULL);
}
char *win_seat_get_ttymode(Seat *seat, const char *mode)
{
return term_get_ttymode(term, mode);
}
int win_seat_get_char_cell_size(Seat *seat, int *x, int *y)
{
*x = font_width;
*y = font_height;
return TRUE;
}
static int win_seat_output(Seat *seat, int is_stderr, const void *, int);
static int win_seat_eof(Seat *seat);
static int win_seat_get_userpass_input(
Seat *seat, prompts_t *p, bufchain *input);
static void win_seat_notify_remote_exit(Seat *seat);
static void win_seat_connection_fatal(Seat *seat, const char *msg);
static void win_seat_update_specials_menu(Seat *seat);
static void win_seat_set_busy_status(Seat *seat, BusyStatus status);
static const SeatVtable win_seat_vt = {
win_seat_output,
win_seat_eof,
win_seat_get_userpass_input,
win_seat_notify_remote_exit,
win_seat_connection_fatal,
win_seat_update_specials_menu,
win_seat_get_ttymode,
win_seat_set_busy_status,
win_seat_verify_ssh_host_key,
win_seat_confirm_weak_crypto_primitive,
win_seat_confirm_weak_cached_hostkey,
win_seat_is_utf8,
nullseat_echoedit_update,
nullseat_get_x_display,
nullseat_get_windowid,
win_seat_get_char_cell_size,
};
Seat win_seat[1] = {{ &win_seat_vt }};
static void start_backend(void)
{
const struct BackendVtable *vt;
@ -266,7 +302,7 @@ static void start_backend(void)
cleanup_exit(1);
}
error = backend_init(vt, NULL, &backend, logctx, conf,
error = backend_init(vt, win_seat, &backend, logctx, conf,
conf_get_str(conf, CONF_host),
conf_get_int(conf, CONF_port),
&realhost,
@ -298,7 +334,7 @@ static void start_backend(void)
/*
* Set up a line discipline.
*/
ldisc = ldisc_create(conf, term, backend, NULL);
ldisc = ldisc_create(conf, term, backend, win_seat);
/*
* Destroy the Restart Session menu item. (This will return
@ -332,7 +368,7 @@ static void close_session(void *ignored_context)
backend_free(backend);
backend = NULL;
term_provide_backend(term, NULL);
update_specials_menu(NULL);
seat_update_specials_menu(win_seat);
}
/*
@ -945,7 +981,7 @@ static void update_savedsess_menu(void)
/*
* Update the Special Commands submenu.
*/
void update_specials_menu(Frontend *frontend)
static void win_seat_update_specials_menu(Seat *seat)
{
HMENU new_menu;
int i, j;
@ -1051,7 +1087,7 @@ static void update_mouse_pointer(void)
}
}
void set_busy_status(Frontend *frontend, int status)
static void win_seat_set_busy_status(Seat *seat, BusyStatus status)
{
busy_status = status;
update_mouse_pointer();
@ -1070,17 +1106,12 @@ void set_raw_mouse_mode(Frontend *frontend, int activate)
/*
* Print a message box and close the connection.
*/
void connection_fatal(Frontend *frontend, const char *fmt, ...)
static void win_seat_connection_fatal(Seat *seat, const char *msg)
{
va_list ap;
char *stuff, morestuff[100];
char title[100];
va_start(ap, fmt);
stuff = dupvprintf(fmt, ap);
va_end(ap);
sprintf(morestuff, "%.70s Fatal Error", appname);
MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
sfree(stuff);
sprintf(title, "%.70s Fatal Error", appname);
MessageBox(hwnd, msg, title, MB_ICONERROR | MB_OK);
if (conf_get_int(conf, CONF_close_on_exit) == FORCE_ON)
PostQuitMessage(1);
@ -1969,7 +2000,7 @@ static int is_alt_pressed(void)
static int resizing;
void notify_remote_exit(Frontend *frontend)
static void win_seat_notify_remote_exit(Seat *seat)
{
int exitcode, close_on_exit;
@ -5907,17 +5938,19 @@ static void flip_full_screen()
}
}
int from_backend(Frontend *frontend, int is_stderr, const void *data, int len)
static int win_seat_output(Seat *seat, int is_stderr,
const void *data, int len)
{
return term_data(term, is_stderr, data, len);
}
int from_backend_eof(Frontend *frontend)
static int win_seat_eof(Seat *seat)
{
return TRUE; /* do respond to incoming EOF with outgoing */
}
int get_userpass_input(prompts_t *p, bufchain *input)
static int win_seat_get_userpass_input(
Seat *seat, prompts_t *p, bufchain *input)
{
int ret;
ret = cmdline_get_passwd_input(p);

View File

@ -22,51 +22,12 @@ struct agent_callback {
int len;
};
void modalfatalbox(const char *p, ...)
void cmdline_error(const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "FATAL ERROR: ");
va_start(ap, p);
vfprintf(stderr, p, ap);
va_start(ap, fmt);
console_print_error_msg_fmt_v("plink", fmt, ap);
va_end(ap);
fputc('\n', stderr);
if (logctx) {
log_free(logctx);
logctx = NULL;
}
cleanup_exit(1);
}
void nonfatal(const char *p, ...)
{
va_list ap;
fprintf(stderr, "ERROR: ");
va_start(ap, p);
vfprintf(stderr, p, ap);
va_end(ap);
fputc('\n', stderr);
}
void connection_fatal(Frontend *frontend, const char *p, ...)
{
va_list ap;
fprintf(stderr, "FATAL ERROR: ");
va_start(ap, p);
vfprintf(stderr, p, ap);
va_end(ap);
fputc('\n', stderr);
if (logctx) {
log_free(logctx);
logctx = NULL;
}
cleanup_exit(1);
}
void cmdline_error(const char *p, ...)
{
va_list ap;
fprintf(stderr, "plink: ");
va_start(ap, p);
vfprintf(stderr, p, ap);
va_end(ap);
fputc('\n', stderr);
exit(1);
}
@ -83,7 +44,7 @@ int term_ldisc(Terminal *term, int mode)
{
return FALSE;
}
void frontend_echoedit_update(Frontend *frontend, int echo, int edit)
static void plink_echoedit_update(Seat *seat, int echo, int edit)
{
/* Update stdin read mode to reflect changes in line discipline. */
DWORD mode;
@ -100,10 +61,7 @@ void frontend_echoedit_update(Frontend *frontend, int echo, int edit)
SetConsoleMode(inhandle, mode);
}
char *get_ttymode(Frontend *frontend, const char *mode) { return NULL; }
int from_backend(Frontend *frontend, int is_stderr,
const void *data, int len)
static int plink_output(Seat *seat, int is_stderr, const void *data, int len)
{
if (is_stderr) {
handle_write(stderr_handle, data, len);
@ -114,13 +72,13 @@ int from_backend(Frontend *frontend, int is_stderr,
return handle_backlog(stdout_handle) + handle_backlog(stderr_handle);
}
int from_backend_eof(Frontend *frontend)
static int plink_eof(Seat *seat)
{
handle_write_eof(stdout_handle);
return FALSE; /* do not respond to incoming EOF with outgoing */
}
int get_userpass_input(prompts_t *p, bufchain *input)
static int plink_get_userpass_input(Seat *seat, prompts_t *p, bufchain *input)
{
int ret;
ret = cmdline_get_passwd_input(p);
@ -129,6 +87,26 @@ int get_userpass_input(prompts_t *p, bufchain *input)
return ret;
}
static const SeatVtable plink_seat_vt = {
plink_output,
plink_eof,
plink_get_userpass_input,
nullseat_notify_remote_exit,
console_connection_fatal,
nullseat_update_specials_menu,
nullseat_get_ttymode,
nullseat_set_busy_status,
console_verify_ssh_host_key,
console_confirm_weak_crypto_primitive,
console_confirm_weak_cached_hostkey,
nullseat_is_never_utf8,
plink_echoedit_update,
nullseat_get_x_display,
nullseat_get_windowid,
nullseat_get_char_cell_size,
};
static Seat plink_seat[1] = {{ &plink_seat_vt }};
static DWORD main_thread_id;
void agent_schedule_callback(void (*callback)(void *, void *, int),
@ -476,7 +454,7 @@ int main(int argc, char **argv)
int nodelay = conf_get_int(conf, CONF_tcp_nodelay) &&
(GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR);
error = backend_init(vt, NULL, &backend, logctx, conf,
error = backend_init(vt, plink_seat, &backend, logctx, conf,
conf_get_str(conf, CONF_host),
conf_get_int(conf, CONF_port),
&realhost, nodelay,

View File

@ -14,7 +14,7 @@ typedef struct Serial Serial;
struct Serial {
HANDLE port;
struct handle *out, *in;
Frontend *frontend;
Seat *seat;
LogContext *logctx;
int bufsize;
long clearbreak_time;
@ -60,15 +60,15 @@ static int serial_gotdata(struct handle *h, void *data, int len)
serial_terminate(serial);
notify_remote_exit(serial->frontend);
seat_notify_remote_exit(serial->seat);
logevent(serial->logctx, error_msg);
connection_fatal(serial->frontend, "%s", error_msg);
seat_connection_fatal(serial->seat, "%s", error_msg);
return 0; /* placate optimiser */
} else {
return from_backend(serial->frontend, 0, data, len);
return seat_stdout(serial->seat, data, len);
}
}
@ -80,11 +80,11 @@ static void serial_sentdata(struct handle *h, int new_backlog)
serial_terminate(serial);
notify_remote_exit(serial->frontend);
seat_notify_remote_exit(serial->seat);
logevent(serial->logctx, error_msg);
connection_fatal(serial->frontend, "%s", error_msg);
seat_connection_fatal(serial->seat, "%s", error_msg);
} else {
serial->bufsize = new_backlog;
}
@ -190,7 +190,7 @@ static const char *serial_configure(Serial *serial, HANDLE serport, Conf *conf)
* Also places the canonical host name into `realhost'. It must be
* freed by the caller.
*/
static const char *serial_init(Frontend *frontend, Backend **backend_handle,
static const char *serial_init(Seat *seat, Backend **backend_handle,
LogContext *logctx, Conf *conf,
const char *host, int port,
char **realhost, int nodelay, int keepalive)
@ -208,7 +208,7 @@ static const char *serial_init(Frontend *frontend, Backend **backend_handle,
serial->backend.vt = &serial_backend;
*backend_handle = &serial->backend;
serial->frontend = frontend;
serial->seat = seat;
serial->logctx = logctx;
serline = conf_get_str(conf, CONF_serline);
@ -265,7 +265,7 @@ static const char *serial_init(Frontend *frontend, Backend **backend_handle,
/*
* Specials are always available.
*/
update_specials_menu(serial->frontend);
seat_update_specials_menu(serial->seat);
return NULL;
}

View File

@ -13,9 +13,7 @@
#include "int64.h"
#include "winsecur.h"
char *get_ttymode(Frontend *frontend, const char *mode) { return NULL; }
int get_userpass_input(prompts_t *p, bufchain *input)
int filexfer_get_userpass_input(Seat *seat, prompts_t *p, bufchain *input)
{
int ret;
ret = cmdline_get_passwd_input(p);

View File

@ -237,11 +237,25 @@ void quit_help(HWND hwnd);
/*
* The terminal and logging context are notionally local to the
* Windows front end, but they must be shared between window.c and
* windlg.c. Likewise the saved-sessions list.
* windlg.c. Likewise the Seat structure for the Windows GUI.
*/
GLOBAL Terminal *term;
GLOBAL LogContext *logctx;
/*
* GUI seat methods in windlg.c.
*/
int win_seat_verify_ssh_host_key(
Seat *seat, const char *host, int port,
const char *keytype, char *keystr, char *key_fingerprint,
void (*callback)(void *ctx, int result), void *ctx);
int win_seat_confirm_weak_crypto_primitive(
Seat *seat, const char *algtype, const char *algname,
void (*callback)(void *ctx, int result), void *ctx);
int win_seat_confirm_weak_cached_hostkey(
Seat *seat, const char *algname, const char *betteralgs,
void (*callback)(void *ctx, int result), void *ctx);
/*
* Windows-specific clipboard helper function shared with windlg.c,
* which takes the data string in the system code page instead of