2014-03-04 00:18:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Glyptodon LLC
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "client.h"
|
2014-06-11 01:45:14 +00:00
|
|
|
#include "guac_clipboard.h"
|
2016-03-01 05:50:00 +00:00
|
|
|
#include "rdp.h"
|
2014-03-04 00:18:14 +00:00
|
|
|
#include "rdp_fs.h"
|
|
|
|
#include "rdp_svc.h"
|
|
|
|
#include "rdp_stream.h"
|
|
|
|
|
2014-06-11 01:45:14 +00:00
|
|
|
#include <freerdp/freerdp.h>
|
2014-04-08 22:31:56 +00:00
|
|
|
#include <freerdp/channels/channels.h>
|
2014-03-04 00:18:14 +00:00
|
|
|
#include <guacamole/client.h>
|
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <guacamole/socket.h>
|
2014-04-10 02:09:41 +00:00
|
|
|
#include <guacamole/stream.h>
|
2014-03-04 00:18:14 +00:00
|
|
|
|
2014-04-08 22:31:56 +00:00
|
|
|
#ifdef HAVE_FREERDP_CLIENT_CLIPRDR_H
|
|
|
|
#include <freerdp/client/cliprdr.h>
|
|
|
|
#else
|
|
|
|
#include "compat/client-cliprdr.h"
|
|
|
|
#endif
|
|
|
|
|
2014-03-04 00:18:14 +00:00
|
|
|
#ifdef ENABLE_WINPR
|
2014-06-11 01:45:14 +00:00
|
|
|
#include <winpr/stream.h>
|
2014-03-04 00:18:14 +00:00
|
|
|
#include <winpr/wtypes.h>
|
|
|
|
#else
|
2014-06-11 01:45:14 +00:00
|
|
|
#include "compat/winpr-stream.h"
|
2014-03-04 00:18:14 +00:00
|
|
|
#include "compat/winpr-wtypes.h"
|
|
|
|
#endif
|
|
|
|
|
2014-06-11 01:45:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-03-04 00:18:14 +00:00
|
|
|
/**
|
|
|
|
* Writes the given filename to the given upload path, sanitizing the filename
|
|
|
|
* and translating the filename to the root directory.
|
|
|
|
*/
|
|
|
|
static void __generate_upload_path(const char* filename, char* path) {
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Add initial backslash */
|
|
|
|
*(path++) = '\\';
|
|
|
|
|
|
|
|
for (i=1; i<GUAC_RDP_FS_MAX_PATH; i++) {
|
|
|
|
|
|
|
|
/* Get current, stop at end */
|
|
|
|
char c = *(filename++);
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Replace special characters with underscores */
|
|
|
|
if (c == '/' || c == '\\')
|
|
|
|
c = '_';
|
|
|
|
|
|
|
|
*(path++) = c;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate path */
|
|
|
|
*path = '\0';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_upload_file_handler(guac_user* user, guac_stream* stream,
|
2014-03-04 00:18:14 +00:00
|
|
|
char* mimetype, char* filename) {
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_client* client = user->client;
|
|
|
|
guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
|
|
|
|
|
2014-03-04 00:18:14 +00:00
|
|
|
int file_id;
|
|
|
|
guac_rdp_stream* rdp_stream;
|
|
|
|
char file_path[GUAC_RDP_FS_MAX_PATH];
|
|
|
|
|
|
|
|
/* Get filesystem, return error if no filesystem */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_rdp_fs* fs = rdp_client->filesystem;
|
2014-03-04 00:18:14 +00:00
|
|
|
if (fs == NULL) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "FAIL (NO FS)",
|
2014-03-18 19:26:01 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_SERVER_ERROR);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Translate name */
|
|
|
|
__generate_upload_path(filename, file_path);
|
|
|
|
|
|
|
|
/* Open file */
|
|
|
|
file_id = guac_rdp_fs_open(fs, file_path, ACCESS_GENERIC_WRITE, 0,
|
|
|
|
DISP_FILE_OVERWRITE_IF, 0);
|
|
|
|
if (file_id < 0) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "FAIL (CANNOT OPEN)",
|
2014-03-18 19:26:01 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Init upload status */
|
|
|
|
rdp_stream = malloc(sizeof(guac_rdp_stream));
|
|
|
|
rdp_stream->type = GUAC_RDP_UPLOAD_STREAM;
|
|
|
|
rdp_stream->upload_status.offset = 0;
|
|
|
|
rdp_stream->upload_status.file_id = file_id;
|
|
|
|
stream->data = rdp_stream;
|
2014-04-10 21:53:32 +00:00
|
|
|
stream->blob_handler = guac_rdp_upload_blob_handler;
|
|
|
|
stream->end_handler = guac_rdp_upload_end_handler;
|
2014-03-04 00:18:14 +00:00
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "OK (STREAM BEGIN)",
|
2014-03-04 00:18:14 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_SUCCESS);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_svc_pipe_handler(guac_user* user, guac_stream* stream,
|
2014-03-04 00:18:14 +00:00
|
|
|
char* mimetype, char* name) {
|
|
|
|
|
|
|
|
guac_rdp_stream* rdp_stream;
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_rdp_svc* svc = guac_rdp_get_svc(user->client, name);
|
2014-03-04 00:18:14 +00:00
|
|
|
|
|
|
|
/* Fail if no such SVC */
|
|
|
|
if (svc == NULL) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_user_log(user, GUAC_LOG_ERROR,
|
2014-03-04 00:18:14 +00:00
|
|
|
"Requested non-existent pipe: \"%s\".",
|
|
|
|
name);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "FAIL (NO SUCH PIPE)",
|
2014-03-18 19:26:01 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_CLIENT_BAD_REQUEST);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_user_log(user, GUAC_LOG_ERROR,
|
2014-03-04 00:18:14 +00:00
|
|
|
"Inbound half of channel \"%s\" connected.",
|
|
|
|
name);
|
|
|
|
|
|
|
|
/* Init stream data */
|
|
|
|
stream->data = rdp_stream = malloc(sizeof(guac_rdp_stream));
|
2014-04-10 21:53:32 +00:00
|
|
|
stream->blob_handler = guac_rdp_svc_blob_handler;
|
2014-03-04 00:18:14 +00:00
|
|
|
rdp_stream->type = GUAC_RDP_INBOUND_SVC_STREAM;
|
|
|
|
rdp_stream->svc = svc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_clipboard_handler(guac_user* user, guac_stream* stream,
|
2014-04-08 02:22:53 +00:00
|
|
|
char* mimetype) {
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_client* client = user->client;
|
|
|
|
guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
|
2014-04-08 02:22:53 +00:00
|
|
|
guac_rdp_stream* rdp_stream;
|
|
|
|
|
|
|
|
/* Init stream data */
|
|
|
|
stream->data = rdp_stream = malloc(sizeof(guac_rdp_stream));
|
2014-04-10 21:53:32 +00:00
|
|
|
stream->blob_handler = guac_rdp_clipboard_blob_handler;
|
|
|
|
stream->end_handler = guac_rdp_clipboard_end_handler;
|
2014-04-08 02:22:53 +00:00
|
|
|
rdp_stream->type = GUAC_RDP_INBOUND_CLIPBOARD_STREAM;
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_common_clipboard_reset(rdp_client->clipboard, mimetype);
|
2014-04-08 02:22:53 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_upload_blob_handler(guac_user* user, guac_stream* stream,
|
2014-03-04 00:18:14 +00:00
|
|
|
void* data, int length) {
|
|
|
|
|
|
|
|
int bytes_written;
|
|
|
|
guac_rdp_stream* rdp_stream = (guac_rdp_stream*) stream->data;
|
|
|
|
|
|
|
|
/* Get filesystem, return error if no filesystem 0*/
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_client* client = user->client;
|
|
|
|
guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
|
|
|
|
guac_rdp_fs* fs = rdp_client->filesystem;
|
2014-03-04 00:18:14 +00:00
|
|
|
if (fs == NULL) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "FAIL (NO FS)",
|
2014-03-18 19:26:01 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_SERVER_ERROR);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write entire block */
|
|
|
|
while (length > 0) {
|
|
|
|
|
|
|
|
/* Attempt write */
|
|
|
|
bytes_written = guac_rdp_fs_write(fs,
|
|
|
|
rdp_stream->upload_status.file_id,
|
|
|
|
rdp_stream->upload_status.offset,
|
|
|
|
data, length);
|
|
|
|
|
|
|
|
/* On error, abort */
|
|
|
|
if (bytes_written < 0) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream,
|
2014-03-04 00:18:14 +00:00
|
|
|
"FAIL (BAD WRITE)",
|
2014-03-18 19:26:01 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update counters */
|
|
|
|
rdp_stream->upload_status.offset += bytes_written;
|
|
|
|
data += bytes_written;
|
|
|
|
length -= bytes_written;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "OK (DATA RECEIVED)",
|
2014-03-04 00:18:14 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_SUCCESS);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_svc_blob_handler(guac_user* user, guac_stream* stream,
|
2014-03-04 00:18:14 +00:00
|
|
|
void* data, int length) {
|
|
|
|
|
|
|
|
guac_rdp_stream* rdp_stream = (guac_rdp_stream*) stream->data;
|
|
|
|
|
|
|
|
/* Write blob data to SVC directly */
|
|
|
|
guac_rdp_svc_write(rdp_stream->svc, data, length);
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "OK (DATA RECEIVED)",
|
2014-03-04 00:18:14 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_SUCCESS);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_clipboard_blob_handler(guac_user* user, guac_stream* stream,
|
2014-04-08 02:22:53 +00:00
|
|
|
void* data, int length) {
|
2014-04-08 22:31:56 +00:00
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_client* client = user->client;
|
|
|
|
guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
|
|
|
|
guac_common_clipboard_append(rdp_client->clipboard, (char*) data, length);
|
2014-04-08 22:31:56 +00:00
|
|
|
|
2014-04-08 02:22:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_upload_end_handler(guac_user* user, guac_stream* stream) {
|
2014-03-04 00:18:14 +00:00
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_client* client = user->client;
|
|
|
|
guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
|
2014-03-04 00:18:14 +00:00
|
|
|
guac_rdp_stream* rdp_stream = (guac_rdp_stream*) stream->data;
|
|
|
|
|
|
|
|
/* Get filesystem, return error if no filesystem */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_rdp_fs* fs = rdp_client->filesystem;
|
2014-03-04 00:18:14 +00:00
|
|
|
if (fs == NULL) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "FAIL (NO FS)",
|
2014-03-18 19:26:01 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_SERVER_ERROR);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close file */
|
|
|
|
guac_rdp_fs_close(fs, rdp_stream->upload_status.file_id);
|
|
|
|
|
|
|
|
/* Acknowledge stream end */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "OK (STREAM END)",
|
2014-03-04 00:18:14 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_SUCCESS);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
|
|
|
|
free(rdp_stream);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_clipboard_end_handler(guac_user* user, guac_stream* stream) {
|
2014-04-08 22:31:56 +00:00
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_client* client = user->client;
|
|
|
|
guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
|
|
|
|
rdpChannels* channels = rdp_client->rdp_inst->context->channels;
|
2014-04-08 22:31:56 +00:00
|
|
|
|
|
|
|
RDP_CB_FORMAT_LIST_EVENT* format_list =
|
|
|
|
(RDP_CB_FORMAT_LIST_EVENT*) freerdp_event_new(
|
|
|
|
CliprdrChannel_Class,
|
|
|
|
CliprdrChannel_FormatList,
|
|
|
|
NULL, NULL);
|
|
|
|
|
2014-04-10 21:53:32 +00:00
|
|
|
/* Terminate clipboard data with NULL */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_common_clipboard_append(rdp_client->clipboard, "", 1);
|
2014-04-10 21:53:32 +00:00
|
|
|
|
2014-04-08 22:31:56 +00:00
|
|
|
/* Notify server that text data is now available */
|
|
|
|
format_list->formats = (UINT32*) malloc(sizeof(UINT32));
|
|
|
|
format_list->formats[0] = CB_FORMAT_TEXT;
|
|
|
|
format_list->formats[1] = CB_FORMAT_UNICODETEXT;
|
|
|
|
format_list->num_formats = 2;
|
|
|
|
|
|
|
|
freerdp_channels_send_event(channels, (wMessage*) format_list);
|
|
|
|
|
2014-04-08 02:22:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_download_ack_handler(guac_user* user, guac_stream* stream,
|
2014-03-04 00:18:14 +00:00
|
|
|
char* message, guac_protocol_status status) {
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_client* client = user->client;
|
|
|
|
guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
|
2014-03-04 00:18:14 +00:00
|
|
|
guac_rdp_stream* rdp_stream = (guac_rdp_stream*) stream->data;
|
|
|
|
|
|
|
|
/* Get filesystem, return error if no filesystem */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_rdp_fs* fs = rdp_client->filesystem;
|
2014-03-04 00:18:14 +00:00
|
|
|
if (fs == NULL) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "FAIL (NO FS)",
|
2014-03-18 19:26:01 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_SERVER_ERROR);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If successful, read data */
|
|
|
|
if (status == GUAC_PROTOCOL_STATUS_SUCCESS) {
|
|
|
|
|
|
|
|
/* Attempt read into buffer */
|
|
|
|
char buffer[4096];
|
|
|
|
int bytes_read = guac_rdp_fs_read(fs,
|
|
|
|
rdp_stream->download_status.file_id,
|
|
|
|
rdp_stream->download_status.offset, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
/* If bytes read, send as blob */
|
|
|
|
if (bytes_read > 0) {
|
|
|
|
rdp_stream->download_status.offset += bytes_read;
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_blob(user->socket, stream,
|
2014-03-04 00:18:14 +00:00
|
|
|
buffer, bytes_read);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If EOF, send end */
|
|
|
|
else if (bytes_read == 0) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_end(user->socket, stream);
|
|
|
|
guac_user_free_stream(user, stream);
|
2014-03-04 00:18:14 +00:00
|
|
|
free(rdp_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, fail stream */
|
|
|
|
else {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_user_log(user, GUAC_LOG_ERROR,
|
2014-03-04 00:18:14 +00:00
|
|
|
"Error reading file for download");
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_end(user->socket, stream);
|
|
|
|
guac_user_free_stream(user, stream);
|
2014-03-04 00:18:14 +00:00
|
|
|
free(rdp_stream);
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2014-03-04 00:18:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
/* Otherwise, return stream to user */
|
2014-03-04 00:18:14 +00:00
|
|
|
else
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_user_free_stream(user, stream);
|
2014-03-04 00:18:14 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_ls_ack_handler(guac_user* user, guac_stream* stream,
|
2015-07-06 06:14:18 +00:00
|
|
|
char* message, guac_protocol_status status) {
|
|
|
|
|
|
|
|
int blob_written = 0;
|
|
|
|
const char* filename;
|
|
|
|
|
|
|
|
guac_rdp_stream* rdp_stream = (guac_rdp_stream*) stream->data;
|
|
|
|
|
|
|
|
/* If unsuccessful, free stream and abort */
|
|
|
|
if (status != GUAC_PROTOCOL_STATUS_SUCCESS) {
|
|
|
|
guac_rdp_fs_close(rdp_stream->ls_status.fs,
|
|
|
|
rdp_stream->ls_status.file_id);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_user_free_stream(user, stream);
|
2015-07-06 06:14:18 +00:00
|
|
|
free(rdp_stream);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* While directory entries remain */
|
|
|
|
while ((filename = guac_rdp_fs_read_dir(rdp_stream->ls_status.fs,
|
|
|
|
rdp_stream->ls_status.file_id)) != NULL
|
|
|
|
&& !blob_written) {
|
|
|
|
|
|
|
|
char absolute_path[GUAC_RDP_FS_MAX_PATH];
|
|
|
|
|
|
|
|
/* Skip current and parent directory entries */
|
|
|
|
if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Concatenate into absolute path - skip if invalid */
|
|
|
|
if (!guac_rdp_fs_append_filename(absolute_path,
|
|
|
|
rdp_stream->ls_status.directory_name, filename)) {
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_user_log(user, GUAC_LOG_DEBUG,
|
2015-07-06 06:14:18 +00:00
|
|
|
"Skipping filename \"%s\" - filename is invalid or "
|
|
|
|
"resulting path is too long", filename);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attempt to open file to determine type */
|
|
|
|
int file_id = guac_rdp_fs_open(rdp_stream->ls_status.fs, absolute_path,
|
|
|
|
ACCESS_GENERIC_READ, 0, DISP_FILE_OPEN, 0);
|
|
|
|
if (file_id < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Get opened file */
|
|
|
|
guac_rdp_fs_file* file = guac_rdp_fs_get_file(rdp_stream->ls_status.fs,
|
|
|
|
file_id);
|
|
|
|
if (file == NULL) {
|
|
|
|
guac_client_log(rdp_stream->ls_status.fs->client, GUAC_LOG_DEBUG,
|
|
|
|
"%s: Successful open produced bad file_id: %i",
|
|
|
|
__func__, file_id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine mimetype */
|
|
|
|
const char* mimetype;
|
|
|
|
if (file->attributes & FILE_ATTRIBUTE_DIRECTORY)
|
2016-03-01 05:50:00 +00:00
|
|
|
mimetype = GUAC_USER_STREAM_INDEX_MIMETYPE;
|
2015-07-06 06:14:18 +00:00
|
|
|
else
|
|
|
|
mimetype = "application/octet-stream";
|
|
|
|
|
|
|
|
/* Write entry */
|
2016-03-01 05:50:00 +00:00
|
|
|
blob_written |= guac_common_json_write_property(user, stream,
|
2015-07-06 06:14:18 +00:00
|
|
|
&rdp_stream->ls_status.json_state, absolute_path, mimetype);
|
|
|
|
|
|
|
|
guac_rdp_fs_close(rdp_stream->ls_status.fs, file_id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Complete JSON and cleanup at end of directory */
|
|
|
|
if (filename == NULL) {
|
|
|
|
|
|
|
|
/* Complete JSON object */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_common_json_end_object(user, stream,
|
2015-07-06 06:14:18 +00:00
|
|
|
&rdp_stream->ls_status.json_state);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_common_json_flush(user, stream,
|
2015-07-06 06:14:18 +00:00
|
|
|
&rdp_stream->ls_status.json_state);
|
|
|
|
|
|
|
|
/* Clean up resources */
|
|
|
|
guac_rdp_fs_close(rdp_stream->ls_status.fs,
|
|
|
|
rdp_stream->ls_status.file_id);
|
|
|
|
free(rdp_stream);
|
|
|
|
|
|
|
|
/* Signal of stream */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_end(user->socket, stream);
|
|
|
|
guac_user_free_stream(user, stream);
|
2015-07-06 06:14:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2015-07-06 06:14:18 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_download_get_handler(guac_user* user, guac_object* object,
|
2015-07-06 06:14:18 +00:00
|
|
|
char* name) {
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_client* client = user->client;
|
|
|
|
guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
|
|
|
|
|
2015-07-06 06:14:18 +00:00
|
|
|
/* Get filesystem, ignore request if no filesystem */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_rdp_fs* fs = rdp_client->filesystem;
|
2015-07-06 06:14:18 +00:00
|
|
|
if (fs == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Attempt to open file for reading */
|
|
|
|
int file_id = guac_rdp_fs_open(fs, name, ACCESS_GENERIC_READ, 0,
|
|
|
|
DISP_FILE_OPEN, 0);
|
|
|
|
if (file_id < 0) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_user_log(user, GUAC_LOG_INFO, "Unable to read file \"%s\"",
|
2015-07-06 06:14:18 +00:00
|
|
|
name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get opened file */
|
|
|
|
guac_rdp_fs_file* file = guac_rdp_fs_get_file(fs, file_id);
|
|
|
|
if (file == NULL) {
|
|
|
|
guac_client_log(fs->client, GUAC_LOG_DEBUG,
|
|
|
|
"%s: Successful open produced bad file_id: %i",
|
|
|
|
__func__, file_id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If directory, send contents of directory */
|
|
|
|
if (file->attributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
|
|
|
|
|
|
/* Create stream data */
|
|
|
|
guac_rdp_stream* rdp_stream = malloc(sizeof(guac_rdp_stream));
|
|
|
|
rdp_stream->type = GUAC_RDP_LS_STREAM;
|
|
|
|
rdp_stream->ls_status.fs = fs;
|
|
|
|
rdp_stream->ls_status.file_id = file_id;
|
|
|
|
strncpy(rdp_stream->ls_status.directory_name, name,
|
2015-07-13 05:27:02 +00:00
|
|
|
sizeof(rdp_stream->ls_status.directory_name) - 1);
|
2015-07-06 06:14:18 +00:00
|
|
|
|
|
|
|
/* Allocate stream for body */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_stream* stream = guac_user_alloc_stream(user);
|
2015-07-06 06:14:18 +00:00
|
|
|
stream->ack_handler = guac_rdp_ls_ack_handler;
|
|
|
|
stream->data = rdp_stream;
|
|
|
|
|
|
|
|
/* Init JSON object state */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_common_json_begin_object(user, stream,
|
2015-07-06 06:14:18 +00:00
|
|
|
&rdp_stream->ls_status.json_state);
|
|
|
|
|
|
|
|
/* Associate new stream with get request */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_body(user->socket, object, stream,
|
|
|
|
GUAC_USER_STREAM_INDEX_MIMETYPE, name);
|
2015-07-06 06:14:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, send file contents */
|
|
|
|
else {
|
|
|
|
|
|
|
|
/* Create stream data */
|
|
|
|
guac_rdp_stream* rdp_stream = malloc(sizeof(guac_rdp_stream));
|
|
|
|
rdp_stream->type = GUAC_RDP_DOWNLOAD_STREAM;
|
|
|
|
rdp_stream->download_status.file_id = file_id;
|
|
|
|
rdp_stream->download_status.offset = 0;
|
|
|
|
|
|
|
|
/* Allocate stream for body */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_stream* stream = guac_user_alloc_stream(user);
|
2015-07-06 06:14:18 +00:00
|
|
|
stream->data = rdp_stream;
|
|
|
|
stream->ack_handler = guac_rdp_download_ack_handler;
|
|
|
|
|
|
|
|
/* Associate new stream with get request */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_body(user->socket, object, stream,
|
2015-07-06 06:14:18 +00:00
|
|
|
"application/octet-stream", name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2015-07-06 06:14:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
int guac_rdp_upload_put_handler(guac_user* user, guac_object* object,
|
2015-07-06 06:14:18 +00:00
|
|
|
guac_stream* stream, char* mimetype, char* name) {
|
|
|
|
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_client* client = user->client;
|
|
|
|
guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
|
|
|
|
|
2015-07-06 06:14:18 +00:00
|
|
|
/* Get filesystem, return error if no filesystem */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_rdp_fs* fs = rdp_client->filesystem;
|
2015-07-06 06:14:18 +00:00
|
|
|
if (fs == NULL) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "FAIL (NO FS)",
|
2015-07-06 06:14:18 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_SERVER_ERROR);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2015-07-06 06:14:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open file */
|
|
|
|
int file_id = guac_rdp_fs_open(fs, name, ACCESS_GENERIC_WRITE, 0,
|
|
|
|
DISP_FILE_OVERWRITE_IF, 0);
|
|
|
|
|
|
|
|
/* Abort on failure */
|
|
|
|
if (file_id < 0) {
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "FAIL (CANNOT OPEN)",
|
2015-07-06 06:14:18 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2015-07-06 06:14:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Init upload stream data */
|
|
|
|
guac_rdp_stream* rdp_stream = malloc(sizeof(guac_rdp_stream));
|
|
|
|
rdp_stream->type = GUAC_RDP_UPLOAD_STREAM;
|
|
|
|
rdp_stream->upload_status.offset = 0;
|
|
|
|
rdp_stream->upload_status.file_id = file_id;
|
|
|
|
|
|
|
|
/* Allocate stream, init for file upload */
|
|
|
|
stream->data = rdp_stream;
|
|
|
|
stream->blob_handler = guac_rdp_upload_blob_handler;
|
|
|
|
stream->end_handler = guac_rdp_upload_end_handler;
|
|
|
|
|
|
|
|
/* Acknowledge stream creation */
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_protocol_send_ack(user->socket, stream, "OK (STREAM BEGIN)",
|
2015-07-06 06:14:18 +00:00
|
|
|
GUAC_PROTOCOL_STATUS_SUCCESS);
|
2016-03-01 05:50:00 +00:00
|
|
|
guac_socket_flush(user->socket);
|
2015-07-06 06:14:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-04 00:18:14 +00:00
|
|
|
|