Initial stab at SFTP file upload.

This commit is contained in:
Michael Jumper 2013-10-18 18:20:46 -07:00
parent 02fece0a29
commit b7e1e898c9
3 changed files with 93 additions and 0 deletions

View File

@ -37,6 +37,8 @@
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#include <sys/select.h> #include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -50,6 +52,7 @@
#include <guacamole/error.h> #include <guacamole/error.h>
#include <libssh/libssh.h> #include <libssh/libssh.h>
#include <libssh/sftp.h>
#include "libssh_compat.h" #include "libssh_compat.h"
#include "guac_handlers.h" #include "guac_handlers.h"
@ -450,3 +453,82 @@ int ssh_guac_client_free_handler(guac_client* client) {
return 0; return 0;
} }
int ssh_guac_client_file_handler(guac_client* client, guac_stream* stream,
char* mimetype, char* filename) {
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
sftp_file file;
/* Open file via SFTP */
file = sftp_open(client_data->sftp_session, filename,
O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
/* Inform of status */
if (file != NULL) {
guac_protocol_send_ack(client->socket, stream, "SFTP - File opened",
GUAC_PROTOCOL_STATUS_SUCCESS);
guac_socket_flush(client->socket);
}
else {
guac_client_log_error(client, "Unable to open file: %s",
ssh_get_error(client_data->session));
guac_protocol_send_ack(client->socket, stream, "SFTP - Open failed",
GUAC_PROTOCOL_STATUS_INTERNAL_ERROR);
guac_socket_flush(client->socket);
}
/* Store file within stream */
stream->data = file;
return 0;
}
int ssh_guac_client_blob_handler(guac_client* client, guac_stream* stream,
void* data, int length) {
/* Pull file from stream */
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
sftp_file file = (sftp_file) stream->data;
/* Attempt write */
if (sftp_write(file, data, length) == length) {
guac_protocol_send_ack(client->socket, stream, "SFTP - OK",
GUAC_PROTOCOL_STATUS_SUCCESS);
guac_socket_flush(client->socket);
}
/* Inform of any errors */
else {
guac_client_log_error(client, "Unable to write to file: %s",
ssh_get_error(client_data->session));
guac_protocol_send_ack(client->socket, stream, "SFTP - Write failed",
GUAC_PROTOCOL_STATUS_INTERNAL_ERROR);
guac_socket_flush(client->socket);
}
return 0;
}
int ssh_guac_client_end_handler(guac_client* client, guac_stream* stream) {
/* Pull file from stream */
sftp_file file = (sftp_file) stream->data;
/* Attempt to close file */
if (sftp_close(file) == SSH_OK) {
guac_protocol_send_ack(client->socket, stream, "SFTP - OK",
GUAC_PROTOCOL_STATUS_SUCCESS);
guac_socket_flush(client->socket);
}
else {
guac_client_log_error(client, "Unable to close file");
guac_protocol_send_ack(client->socket, stream, "SFTP - Close failed",
GUAC_PROTOCOL_STATUS_INTERNAL_ERROR);
guac_socket_flush(client->socket);
}
return 0;
}

View File

@ -47,6 +47,11 @@ int ssh_guac_client_mouse_handler(guac_client* client, int x, int y, int mask);
int ssh_guac_client_clipboard_handler(guac_client* client, char* data); int ssh_guac_client_clipboard_handler(guac_client* client, char* data);
int ssh_guac_client_size_handler(guac_client* client, int width, int height); int ssh_guac_client_size_handler(guac_client* client, int width, int height);
int ssh_guac_client_free_handler(guac_client* client); int ssh_guac_client_free_handler(guac_client* client);
int ssh_guac_client_file_handler(guac_client* client, guac_stream* stream,
char* mimetype, char* filename);
int ssh_guac_client_blob_handler(guac_client* client, guac_stream* stream,
void* data, int length);
int ssh_guac_client_end_handler(guac_client* client, guac_stream* stream);
#endif #endif

View File

@ -49,6 +49,7 @@
#include "client.h" #include "client.h"
#include "common.h" #include "common.h"
#include "guac_handlers.h"
/** /**
* Reads a single line from STDIN. * Reads a single line from STDIN.
@ -227,6 +228,11 @@ void* ssh_client_thread(void* data) {
return NULL; return NULL;
} }
/* Set file handlers */
client->file_handler = ssh_guac_client_file_handler;
client->blob_handler = ssh_guac_client_blob_handler;
client->end_handler = ssh_guac_client_end_handler;
guac_client_log_info(client, "SFTP session initialized"); guac_client_log_info(client, "SFTP session initialized");
} }