2013-12-29 04:53:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Glyptodon LLC
|
2013-10-26 23:30:06 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2013-10-26 23:30:06 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-10-26 23:30:06 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2013-10-26 23:30:06 +00:00
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "config.h"
|
2013-10-26 23:30:06 +00:00
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "client.h"
|
2014-06-11 17:06:18 +00:00
|
|
|
#include "sftp.h"
|
2014-01-01 22:44:28 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
2013-10-28 16:28:06 +00:00
|
|
|
#include <libgen.h>
|
2013-10-26 23:30:06 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-06-11 17:06:18 +00:00
|
|
|
#include <libssh2_sftp.h>
|
2013-10-26 23:30:06 +00:00
|
|
|
#include <guacamole/client.h>
|
|
|
|
#include <guacamole/protocol.h>
|
2014-04-10 02:09:41 +00:00
|
|
|
#include <guacamole/socket.h>
|
2013-10-26 23:30:06 +00:00
|
|
|
#include <guacamole/stream.h>
|
|
|
|
|
|
|
|
static bool __ssh_guac_valid_filename(char* filename) {
|
|
|
|
|
|
|
|
/* Disallow "." as a filename */
|
|
|
|
if (strcmp(filename, ".") == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Disallow ".." as a filename */
|
|
|
|
if (strcmp(filename, "..") == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Search for path separator characters */
|
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
char c = *(filename++);
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Replace slashes with underscores */
|
|
|
|
if (c == '\\' || c == '/')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If filename does not contain a path, it's ok */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int guac_sftp_file_handler(guac_client* client, guac_stream* stream,
|
|
|
|
char* mimetype, char* filename) {
|
|
|
|
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
2013-10-28 21:11:04 +00:00
|
|
|
char fullpath[GUAC_SFTP_MAX_PATH];
|
2013-12-01 23:39:29 +00:00
|
|
|
LIBSSH2_SFTP_HANDLE* file;
|
2013-10-28 21:11:04 +00:00
|
|
|
int i;
|
2013-10-26 23:30:06 +00:00
|
|
|
|
|
|
|
/* Ensure filename is a valid filename and not a path */
|
|
|
|
if (!__ssh_guac_valid_filename(filename)) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_protocol_send_ack(client->socket, stream, "SFTP: Illegal filename",
|
2014-03-18 19:26:01 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_CLIENT_BAD_REQUEST);
|
2013-10-26 23:30:06 +00:00
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-28 21:11:04 +00:00
|
|
|
/* Copy upload path, append trailing slash */
|
|
|
|
for (i=0; i<GUAC_SFTP_MAX_PATH; i++) {
|
|
|
|
char c = client_data->sftp_upload_path[i];
|
|
|
|
if (c == '\0') {
|
|
|
|
fullpath[i++] = '/';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fullpath[i] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append filename */
|
|
|
|
for (; i<GUAC_SFTP_MAX_PATH; i++) {
|
|
|
|
char c = *(filename++);
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
fullpath[i] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If path + filename exceeds max length, abort */
|
|
|
|
if (i == GUAC_SFTP_MAX_PATH) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_protocol_send_ack(client->socket, stream, "SFTP: Name too long", GUAC_PROTOCOL_STATUS_CLIENT_OVERRUN);
|
2013-10-28 21:11:04 +00:00
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate path string */
|
|
|
|
fullpath[i] = '\0';
|
|
|
|
|
2013-10-26 23:30:06 +00:00
|
|
|
/* Open file via SFTP */
|
2013-12-01 23:39:29 +00:00
|
|
|
file = libssh2_sftp_open(client_data->sftp_session, fullpath,
|
|
|
|
LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC,
|
|
|
|
S_IRUSR | S_IWUSR);
|
2013-10-26 23:30:06 +00:00
|
|
|
|
|
|
|
/* Inform of status */
|
|
|
|
if (file != NULL) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_protocol_send_ack(client->socket, stream, "SFTP: File opened", GUAC_PROTOCOL_STATUS_SUCCESS);
|
2013-10-26 23:30:06 +00:00
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
}
|
|
|
|
else {
|
2013-10-28 21:11:04 +00:00
|
|
|
guac_client_log_error(client, "Unable to open file \"%s\": %s",
|
2013-12-01 23:39:29 +00:00
|
|
|
fullpath, libssh2_sftp_last_error(client_data->sftp_session));
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_protocol_send_ack(client->socket, stream, "SFTP: Open failed", GUAC_PROTOCOL_STATUS_RESOURCE_NOT_FOUND);
|
2013-10-26 23:30:06 +00:00
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
}
|
|
|
|
|
2014-04-10 20:47:36 +00:00
|
|
|
/* Set handlers for file stream */
|
|
|
|
stream->blob_handler = guac_sftp_blob_handler;
|
|
|
|
stream->end_handler = guac_sftp_end_handler;
|
|
|
|
|
2013-10-26 23:30:06 +00:00
|
|
|
/* Store file within stream */
|
|
|
|
stream->data = file;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int guac_sftp_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;
|
2013-12-01 23:39:29 +00:00
|
|
|
LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
|
2013-10-26 23:30:06 +00:00
|
|
|
|
|
|
|
/* Attempt write */
|
2013-12-01 23:39:29 +00:00
|
|
|
if (libssh2_sftp_write(file, data, length) == length) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_protocol_send_ack(client->socket, stream, "SFTP: OK", GUAC_PROTOCOL_STATUS_SUCCESS);
|
2013-10-26 23:30:06 +00:00
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inform of any errors */
|
|
|
|
else {
|
|
|
|
guac_client_log_error(client, "Unable to write to file: %s",
|
2013-12-01 23:39:29 +00:00
|
|
|
libssh2_sftp_last_error(client_data->sftp_session));
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_protocol_send_ack(client->socket, stream, "SFTP: Write failed", GUAC_PROTOCOL_STATUS_SERVER_ERROR);
|
2013-10-26 23:30:06 +00:00
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int guac_sftp_end_handler(guac_client* client, guac_stream* stream) {
|
|
|
|
|
|
|
|
/* Pull file from stream */
|
2013-12-01 23:39:29 +00:00
|
|
|
LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
|
2013-10-26 23:30:06 +00:00
|
|
|
|
|
|
|
/* Attempt to close file */
|
2013-12-01 23:39:29 +00:00
|
|
|
if (libssh2_sftp_close(file) == 0) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_protocol_send_ack(client->socket, stream, "SFTP: OK", GUAC_PROTOCOL_STATUS_SUCCESS);
|
2013-10-26 23:30:06 +00:00
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
guac_client_log_error(client, "Unable to close file");
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_protocol_send_ack(client->socket, stream, "SFTP: Close failed", GUAC_PROTOCOL_STATUS_SERVER_ERROR);
|
2013-10-26 23:30:06 +00:00
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-28 15:40:19 +00:00
|
|
|
int guac_sftp_ack_handler(guac_client* client, guac_stream* stream,
|
|
|
|
char* message, guac_protocol_status status) {
|
2013-10-28 16:28:06 +00:00
|
|
|
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
2013-12-01 23:39:29 +00:00
|
|
|
LIBSSH2_SFTP_HANDLE* file = (LIBSSH2_SFTP_HANDLE*) stream->data;
|
2013-10-28 16:28:06 +00:00
|
|
|
|
|
|
|
/* If successful, read data */
|
|
|
|
if (status == GUAC_PROTOCOL_STATUS_SUCCESS) {
|
|
|
|
|
|
|
|
/* Attempt read into buffer */
|
|
|
|
char buffer[4096];
|
2013-12-01 23:39:29 +00:00
|
|
|
int bytes_read = libssh2_sftp_read(file, buffer, sizeof(buffer));
|
2013-10-28 16:28:06 +00:00
|
|
|
|
|
|
|
/* If bytes read, send as blob */
|
|
|
|
if (bytes_read > 0)
|
|
|
|
guac_protocol_send_blob(client->socket, stream,
|
|
|
|
buffer, bytes_read);
|
|
|
|
|
|
|
|
/* If EOF, send end */
|
|
|
|
else if (bytes_read == 0) {
|
|
|
|
guac_protocol_send_end(client->socket, stream);
|
|
|
|
guac_client_free_stream(client, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, fail stream */
|
|
|
|
else {
|
|
|
|
guac_client_log_error(client, "Error reading file: %s",
|
2013-12-01 23:39:29 +00:00
|
|
|
libssh2_sftp_last_error(client_data->sftp_session));
|
2013-10-28 16:28:06 +00:00
|
|
|
guac_protocol_send_end(client->socket, stream);
|
|
|
|
guac_client_free_stream(client, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, return stream to client */
|
|
|
|
else
|
|
|
|
guac_client_free_stream(client, stream);
|
|
|
|
|
2013-10-28 15:40:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
guac_stream* guac_sftp_download_file(guac_client* client,
|
2013-10-28 16:28:06 +00:00
|
|
|
char* filename) {
|
|
|
|
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
|
|
|
guac_stream* stream;
|
2013-12-01 23:39:29 +00:00
|
|
|
LIBSSH2_SFTP_HANDLE* file;
|
2013-10-28 16:28:06 +00:00
|
|
|
|
|
|
|
/* Attempt to open file for reading */
|
2013-12-01 23:39:29 +00:00
|
|
|
file = libssh2_sftp_open(client_data->sftp_session, filename,
|
|
|
|
LIBSSH2_FXF_READ, 0);
|
2013-10-28 16:28:06 +00:00
|
|
|
if (file == NULL) {
|
|
|
|
guac_client_log_error(client, "Unable to read file \"%s\": %s",
|
2013-12-01 23:39:29 +00:00
|
|
|
filename,
|
|
|
|
libssh2_sftp_last_error(client_data->sftp_session));
|
2013-10-28 16:28:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate stream */
|
|
|
|
stream = guac_client_alloc_stream(client);
|
2014-04-10 20:47:36 +00:00
|
|
|
stream->ack_handler = guac_sftp_ack_handler;
|
2013-10-28 16:28:06 +00:00
|
|
|
stream->data = file;
|
|
|
|
|
|
|
|
/* Send stream start, strip name */
|
|
|
|
filename = basename(filename);
|
|
|
|
guac_protocol_send_file(client->socket, stream,
|
|
|
|
"application/octet-stream", filename);
|
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
|
|
|
|
return stream;
|
2013-10-28 15:40:19 +00:00
|
|
|
|
2013-10-26 23:30:06 +00:00
|
|
|
}
|
|
|
|
|
2013-10-28 21:11:04 +00:00
|
|
|
void guac_sftp_set_upload_path(guac_client* client, char* path) {
|
|
|
|
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
|
|
|
int length = strnlen(path, GUAC_SFTP_MAX_PATH);
|
|
|
|
|
|
|
|
/* Ignore requests which exceed maximum-allowed path */
|
|
|
|
if (length > GUAC_SFTP_MAX_PATH) {
|
|
|
|
guac_client_log_error(client,
|
|
|
|
"Submitted path exceeds limit of %i bytes",
|
|
|
|
GUAC_SFTP_MAX_PATH);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy path */
|
|
|
|
memcpy(client_data->sftp_upload_path, path, length);
|
|
|
|
|
|
|
|
}
|
|
|
|
|