From 91c2e6b4d54c2a3655bc5cde43ca2e3a8f3fc410 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 22 Feb 2020 15:29:45 +0000 Subject: [PATCH] Permit protocol selection in file transfer tools. PSCP and PSFTP can only work over a protocol enough like SSH to be able to run subsystems (or at the very least a remote command, for old-style PSCP). Historically we've implemented this restriction by having them not support any protocol-selection command-line options at all, and hardwiring them to instantiating ssh_backend. This commit regularises them to be more like the rest of the tools. You can select a protocol using the appropriate command-line option, provided it's a protocol in those tools' backends[] array. And the setup code will find the BackendVtable to instantiate by the usual method of calling backend_vt_from_proto. Currently, this makes essentially no difference: those tools link in be_ssh.c, which means the only supported backend is SSH. So the effect is that now -ssh is an accepted option with no effect, instead of being rejected. But it opens the way to add other protocols that are SSH-like enough to run file transfer over. --- be_ssh.c | 8 ++++---- cmdline.c | 2 +- pscp.c | 12 ++++++++---- psftp.c | 12 ++++++++---- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/be_ssh.c b/be_ssh.c index 40737e52..69e8b8ab 100644 --- a/be_ssh.c +++ b/be_ssh.c @@ -1,8 +1,7 @@ /* - * Linking module for programs that are restricted to only using SSH - * (pscp and psftp). These do not support selection of backend, but - * must still have a backends[] array mentioning SSH because - * settings.c will want to consult it during session load. + * Linking module for programs that are restricted to only using + * SSH-type protocols (pscp and psftp). These still have a choice of + * two actual backends, because they can also speak PROT_SSHCONN. */ #include @@ -12,5 +11,6 @@ const int be_default_protocol = PROT_SSH; const struct BackendVtable *const backends[] = { &ssh_backend, + &sshconn_backend, NULL }; diff --git a/cmdline.c b/cmdline.c index 826fc358..e7cdc9aa 100644 --- a/cmdline.c +++ b/cmdline.c @@ -408,7 +408,7 @@ int cmdline_process_param(const char *p, char *value, for (size_t i = 0; backends[i]; i++) { if (p[0] == '-' && !strcmp(p+1, backends[i]->id)) { RETURN(1); - UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK); + UNAVAILABLE_IN(TOOLTYPE_NONNETWORK); SAVEABLE(0); set_protocol(conf, backends[i]->protocol); if (backends[i]->default_port) diff --git a/pscp.c b/pscp.c index 0fde461f..69e1e40e 100644 --- a/pscp.c +++ b/pscp.c @@ -322,10 +322,12 @@ static void do_cmd(char *host, char *user, char *cmd) } /* - * Force use of SSH. (If they got the protocol wrong we assume the - * port is useless too.) + * Force protocol to SSH if the user has somehow contrived to + * select one we don't support (e.g. by loading an inappropriate + * saved session). In that situation we assume the port number is + * useless too.) */ - if (conf_get_int(conf, CONF_protocol) != PROT_SSH) { + if (!backend_vt_from_proto(conf_get_int(conf, CONF_protocol))) { conf_set_int(conf, CONF_protocol, PROT_SSH); conf_set_int(conf, CONF_port, 22); } @@ -449,7 +451,9 @@ static void do_cmd(char *host, char *user, char *cmd) platform_psftp_pre_conn_setup(console_cli_logpolicy); - err = backend_init(&ssh_backend, pscp_seat, &backend, logctx, conf, + err = backend_init(backend_vt_from_proto( + conf_get_int(conf, CONF_protocol)), + pscp_seat, &backend, logctx, conf, conf_get_str(conf, CONF_host), conf_get_int(conf, CONF_port), &realhost, 0, diff --git a/psftp.c b/psftp.c index efb5bbff..8399ac07 100644 --- a/psftp.c +++ b/psftp.c @@ -2593,10 +2593,12 @@ static int psftp_connect(char *userhost, char *user, int portnumber) } /* - * Force use of SSH. (If they got the protocol wrong we assume the - * port is useless too.) + * Force protocol to SSH if the user has somehow contrived to + * select one we don't support (e.g. by loading an inappropriate + * saved session). In that situation we assume the port number is + * useless too.) */ - if (conf_get_int(conf, CONF_protocol) != PROT_SSH) { + if (!backend_vt_from_proto(conf_get_int(conf, CONF_protocol))) { conf_set_int(conf, CONF_protocol, PROT_SSH); conf_set_int(conf, CONF_port, 22); } @@ -2713,7 +2715,9 @@ static int psftp_connect(char *userhost, char *user, int portnumber) platform_psftp_pre_conn_setup(console_cli_logpolicy); - err = backend_init(&ssh_backend, psftp_seat, &backend, psftp_logctx, conf, + err = backend_init(backend_vt_from_proto( + conf_get_int(conf, CONF_protocol)), + psftp_seat, &backend, psftp_logctx, conf, conf_get_str(conf, CONF_host), conf_get_int(conf, CONF_port), &realhost, 0,