From 02fece0a29586efb95460b1f08689f95df337a8e Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 18 Oct 2013 15:37:16 -0700 Subject: [PATCH] Add enable-sftp option, init/free SFTP as needed. --- src/protocols/ssh/client.c | 10 ++++++++++ src/protocols/ssh/client.h | 11 +++++++++++ src/protocols/ssh/guac_handlers.c | 4 ++++ src/protocols/ssh/ssh_client.c | 25 +++++++++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/src/protocols/ssh/client.c b/src/protocols/ssh/client.c index 79f55617..c93c27ac 100644 --- a/src/protocols/ssh/client.c +++ b/src/protocols/ssh/client.c @@ -65,6 +65,7 @@ const char* GUAC_CLIENT_ARGS[] = { "password", "font-name", "font-size", + "enable-sftp", NULL }; @@ -100,6 +101,11 @@ enum __SSH_ARGS_IDX { */ IDX_FONT_SIZE, + /** + * Whether SFTP should be enabled. + */ + IDX_ENABLE_SFTP, + SSH_ARGS_COUNT }; @@ -139,6 +145,10 @@ int guac_client_init(guac_client* client, int argc, char** argv) { else client_data->font_size = GUAC_SSH_DEFAULT_FONT_SIZE; + /* Parse SFTP enable */ + client_data->enable_sftp = strcmp(argv[IDX_ENABLE_SFTP], "true") == 0; + client_data->sftp_session = NULL; + /* Read port */ if (argv[IDX_PORT][0] != 0) client_data->port = atoi(argv[IDX_PORT]); diff --git a/src/protocols/ssh/client.h b/src/protocols/ssh/client.h index 08437d03..6d4a0a24 100644 --- a/src/protocols/ssh/client.h +++ b/src/protocols/ssh/client.h @@ -41,6 +41,7 @@ #include #include +#include #include "terminal.h" #include "cursor.h" @@ -80,6 +81,11 @@ typedef struct ssh_guac_client_data { */ int font_size; + /** + * Whether SFTP is enabled. + */ + bool enable_sftp; + /** * The SSH client thread. */ @@ -90,6 +96,11 @@ typedef struct ssh_guac_client_data { */ ssh_session session; + /** + * SFTP session, used for file transfers. + */ + sftp_session sftp_session; + /** * SSH terminal channel, used by the SSH client thread. */ diff --git a/src/protocols/ssh/guac_handlers.c b/src/protocols/ssh/guac_handlers.c index ec95de43..412463b9 100644 --- a/src/protocols/ssh/guac_handlers.c +++ b/src/protocols/ssh/guac_handlers.c @@ -430,6 +430,10 @@ int ssh_guac_client_free_handler(guac_client* client) { /* Free channels */ ssh_channel_free(guac_client_data->term_channel); + /* Clean up SFTP */ + if (guac_client_data->sftp_session) + sftp_free(guac_client_data->sftp_session); + /* Free session */ ssh_free(guac_client_data->session); diff --git a/src/protocols/ssh/ssh_client.c b/src/protocols/ssh/ssh_client.c index 32dcc939..5f189788 100644 --- a/src/protocols/ssh/ssh_client.c +++ b/src/protocols/ssh/ssh_client.c @@ -45,6 +45,7 @@ #include #include +#include #include "client.h" #include "common.h" @@ -206,6 +207,30 @@ void* ssh_client_thread(void* data) { return NULL; } + /* Start SFTP session as well, if enabled */ + if (client_data->enable_sftp) { + + /* Request SFTP */ + client_data->sftp_session = sftp_new(client_data->session); + if (client_data->sftp_session == NULL) { + guac_protocol_send_error(socket, "Unable to start SFTP session..", + GUAC_PROTOCOL_STATUS_INTERNAL_ERROR); + guac_socket_flush(socket); + return NULL; + } + + /* Init SFTP */ + if (sftp_init(client_data->sftp_session) != SSH_OK) { + guac_protocol_send_error(socket, "Unable to initialize SFTP session.", + GUAC_PROTOCOL_STATUS_INTERNAL_ERROR); + guac_socket_flush(socket); + return NULL; + } + + guac_client_log_info(client, "SFTP session initialized"); + + } + /* Request PTY */ if (channel_request_pty_size(client_data->term_channel, "linux", client_data->term->term_width, client_data->term->term_height) != SSH_OK) {