2016-03-01 05:40:39 +00:00
|
|
|
/*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
2016-03-01 05:40:39 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-03-01 05:40:39 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-03-01 05:40:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2017-07-01 22:51:15 +00:00
|
|
|
#include "client.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "socket.h"
|
|
|
|
#include "user.h"
|
2016-03-01 05:40:39 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
2016-09-11 20:18:27 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-06-11 02:16:22 +00:00
|
|
|
/**
|
|
|
|
* Parameters required by the user input thread.
|
|
|
|
*/
|
2017-07-01 22:51:15 +00:00
|
|
|
typedef struct guac_user_input_thread_params {
|
2017-06-11 02:16:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The parser which will be used throughout the user's session.
|
|
|
|
*/
|
|
|
|
guac_parser* parser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the connected user.
|
|
|
|
*/
|
|
|
|
guac_user* user;
|
|
|
|
|
2017-07-01 22:24:20 +00:00
|
|
|
/**
|
|
|
|
* The number of microseconds to wait for instructions from a connected
|
|
|
|
* user before closing the connection with an error.
|
|
|
|
*/
|
|
|
|
int usec_timeout;
|
|
|
|
|
2017-07-01 22:51:15 +00:00
|
|
|
} guac_user_input_thread_params;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints an error message using the logging facilities of the given user,
|
|
|
|
* automatically including any information present in guac_error.
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* The guac_user associated with the error that occurred.
|
|
|
|
*
|
|
|
|
* @param level
|
|
|
|
* The level at which to log this message.
|
|
|
|
*
|
|
|
|
* @param message
|
|
|
|
* The message to log.
|
|
|
|
*/
|
|
|
|
static void guac_user_log_guac_error(guac_user* user,
|
|
|
|
guac_client_log_level level, const char* message) {
|
|
|
|
|
|
|
|
if (guac_error != GUAC_STATUS_SUCCESS) {
|
|
|
|
|
|
|
|
/* If error message provided, include in log */
|
|
|
|
if (guac_error_message != NULL)
|
|
|
|
guac_user_log(user, level, "%s: %s", message,
|
|
|
|
guac_error_message);
|
|
|
|
|
|
|
|
/* Otherwise just log with standard status string */
|
|
|
|
else
|
|
|
|
guac_user_log(user, level, "%s: %s", message,
|
|
|
|
guac_status_string(guac_error));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just log message if no status code */
|
|
|
|
else
|
|
|
|
guac_user_log(user, level, "%s", message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs a reasonable explanatory message regarding handshake failure based on
|
|
|
|
* the current value of guac_error.
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* The guac_user associated with the failed Guacamole protocol handshake.
|
|
|
|
*/
|
|
|
|
static void guac_user_log_handshake_failure(guac_user* user) {
|
|
|
|
|
|
|
|
if (guac_error == GUAC_STATUS_CLOSED)
|
|
|
|
guac_user_log(user, GUAC_LOG_INFO,
|
|
|
|
"Guacamole connection closed during handshake");
|
|
|
|
else if (guac_error == GUAC_STATUS_PROTOCOL_ERROR)
|
|
|
|
guac_user_log(user, GUAC_LOG_ERROR,
|
|
|
|
"Guacamole protocol violation. Perhaps the version of "
|
|
|
|
"guacamole-client is incompatible with this version of "
|
|
|
|
"libguac?");
|
|
|
|
else
|
|
|
|
guac_user_log(user, GUAC_LOG_WARNING,
|
|
|
|
"Guacamole handshake failed: %s",
|
|
|
|
guac_status_string(guac_error));
|
|
|
|
|
|
|
|
}
|
2017-06-11 02:16:22 +00:00
|
|
|
|
2016-09-11 20:18:27 +00:00
|
|
|
/**
|
|
|
|
* Copies the given array of mimetypes (strings) into a newly-allocated NULL-
|
|
|
|
* terminated array of strings. Both the array and the strings within the array
|
2017-07-01 22:51:15 +00:00
|
|
|
* are newly-allocated and must be later freed via guac_free_mimetypes().
|
2016-09-11 20:18:27 +00:00
|
|
|
*
|
|
|
|
* @param mimetypes
|
|
|
|
* The array of mimetypes to copy.
|
|
|
|
*
|
|
|
|
* @param count
|
|
|
|
* The number of mimetypes in the given array.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* A newly-allocated, NULL-terminated array containing newly-allocated
|
|
|
|
* copies of each of the mimetypes provided in the original mimetypes
|
|
|
|
* array.
|
|
|
|
*/
|
2017-07-01 22:51:15 +00:00
|
|
|
static char** guac_copy_mimetypes(char** mimetypes, int count) {
|
2016-09-11 20:18:27 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Allocate sufficient space for NULL-terminated array of mimetypes */
|
|
|
|
char** mimetypes_copy = malloc(sizeof(char*) * (count+1));
|
|
|
|
|
|
|
|
/* Copy each provided mimetype */
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
mimetypes_copy[i] = strdup(mimetypes[i]);
|
|
|
|
|
|
|
|
/* Terminate with NULL */
|
|
|
|
mimetypes_copy[count] = NULL;
|
|
|
|
|
|
|
|
return mimetypes_copy;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees the given array of mimetypes, including the space allocated to each
|
|
|
|
* mimetype string within the array. The provided array of mimetypes MUST have
|
2017-07-01 22:51:15 +00:00
|
|
|
* been allocated with guac_copy_mimetypes().
|
2016-09-11 20:18:27 +00:00
|
|
|
*
|
|
|
|
* @param mimetypes
|
|
|
|
* The NULL-terminated array of mimetypes to free. This array MUST have
|
2017-07-01 22:51:15 +00:00
|
|
|
* been previously allocated with guac_copy_mimetypes().
|
2016-09-11 20:18:27 +00:00
|
|
|
*/
|
2017-07-01 22:51:15 +00:00
|
|
|
static void guac_free_mimetypes(char** mimetypes) {
|
2016-09-11 20:18:27 +00:00
|
|
|
|
|
|
|
char** current_mimetype = mimetypes;
|
|
|
|
|
|
|
|
/* Free all strings within NULL-terminated mimetype array */
|
|
|
|
while (*current_mimetype != NULL) {
|
|
|
|
free(*current_mimetype);
|
|
|
|
current_mimetype++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the array itself, now that its contents have been freed */
|
|
|
|
free(mimetypes);
|
|
|
|
|
|
|
|
}
|
2016-03-01 05:40:39 +00:00
|
|
|
|
2017-06-11 02:16:22 +00:00
|
|
|
/**
|
|
|
|
* The thread which handles all user input, calling event handlers for received
|
|
|
|
* instructions.
|
|
|
|
*
|
|
|
|
* @param data
|
2017-07-01 22:51:15 +00:00
|
|
|
* A pointer to a guac_user_input_thread_params structure describing the
|
2017-06-11 02:16:22 +00:00
|
|
|
* user whose input is being handled and the guac_parser with which to
|
|
|
|
* handle it.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* Always NULL.
|
|
|
|
*/
|
2017-07-01 22:51:15 +00:00
|
|
|
static void* guac_user_input_thread(void* data) {
|
2016-03-01 05:40:39 +00:00
|
|
|
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_input_thread_params* params =
|
|
|
|
(guac_user_input_thread_params*) data;
|
2017-07-01 22:24:20 +00:00
|
|
|
|
|
|
|
int usec_timeout = params->usec_timeout;
|
2016-03-01 05:40:39 +00:00
|
|
|
guac_user* user = params->user;
|
|
|
|
guac_parser* parser = params->parser;
|
|
|
|
guac_client* client = user->client;
|
|
|
|
guac_socket* socket = user->socket;
|
|
|
|
|
|
|
|
/* Guacamole user input loop */
|
|
|
|
while (client->state == GUAC_CLIENT_RUNNING && user->active) {
|
|
|
|
|
|
|
|
/* Read instruction, stop on error */
|
2017-07-01 22:24:20 +00:00
|
|
|
if (guac_parser_read(parser, socket, usec_timeout)) {
|
2016-03-01 05:40:39 +00:00
|
|
|
|
|
|
|
if (guac_error == GUAC_STATUS_TIMEOUT)
|
|
|
|
guac_user_abort(user, GUAC_PROTOCOL_STATUS_CLIENT_TIMEOUT, "User is not responding.");
|
|
|
|
|
|
|
|
else {
|
|
|
|
if (guac_error != GUAC_STATUS_CLOSED)
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_log_guac_error(user, GUAC_LOG_WARNING,
|
2016-03-01 05:40:39 +00:00
|
|
|
"Guacamole connection failure");
|
|
|
|
guac_user_stop(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset guac_error and guac_error_message (user/client handlers are not
|
|
|
|
* guaranteed to set these) */
|
|
|
|
guac_error = GUAC_STATUS_SUCCESS;
|
|
|
|
guac_error_message = NULL;
|
|
|
|
|
|
|
|
/* Call handler, stop on error */
|
|
|
|
if (guac_user_handle_instruction(user, parser->opcode, parser->argc, parser->argv) < 0) {
|
|
|
|
|
|
|
|
/* Log error */
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_log_guac_error(user, GUAC_LOG_WARNING,
|
2016-03-01 05:40:39 +00:00
|
|
|
"User connection aborted");
|
|
|
|
|
|
|
|
/* Log handler details */
|
|
|
|
guac_user_log(user, GUAC_LOG_DEBUG, "Failing instruction handler in user was \"%s\"", parser->opcode);
|
|
|
|
|
|
|
|
guac_user_stop(user);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-11 02:16:22 +00:00
|
|
|
/**
|
|
|
|
* Starts the input/output threads of a new user. This function will block
|
|
|
|
* until the user disconnects. If an error prevents the input/output threads
|
|
|
|
* from starting, guac_user_stop() will be invoked on the given user.
|
|
|
|
*
|
|
|
|
* @param parser
|
|
|
|
* The guac_parser to use to handle all input from the given user.
|
|
|
|
*
|
|
|
|
* @param user
|
|
|
|
* The user whose associated I/O transfer threads should be started.
|
|
|
|
*
|
2017-07-01 22:24:20 +00:00
|
|
|
* @param usec_timeout
|
|
|
|
* The number of microseconds to wait for instructions from the given
|
|
|
|
* user before closing the connection with an error.
|
|
|
|
*
|
2017-06-11 02:16:22 +00:00
|
|
|
* @return
|
|
|
|
* Zero if the I/O threads started successfully and user has disconnected,
|
|
|
|
* or non-zero if the I/O threads could not be started.
|
|
|
|
*/
|
2017-07-01 22:51:15 +00:00
|
|
|
static int guac_user_start(guac_parser* parser, guac_user* user,
|
2017-07-01 22:24:20 +00:00
|
|
|
int usec_timeout) {
|
2016-03-01 05:40:39 +00:00
|
|
|
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_input_thread_params params = {
|
2016-03-01 05:40:39 +00:00
|
|
|
.parser = parser,
|
2017-07-01 22:24:20 +00:00
|
|
|
.user = user,
|
|
|
|
.usec_timeout = usec_timeout
|
2016-03-01 05:40:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pthread_t input_thread;
|
|
|
|
|
2017-07-01 22:51:15 +00:00
|
|
|
if (pthread_create(&input_thread, NULL, guac_user_input_thread, (void*) ¶ms)) {
|
2016-03-01 05:40:39 +00:00
|
|
|
guac_user_log(user, GUAC_LOG_ERROR, "Unable to start input thread");
|
|
|
|
guac_user_stop(user);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for I/O threads */
|
|
|
|
pthread_join(input_thread, NULL);
|
|
|
|
|
2016-11-07 18:29:00 +00:00
|
|
|
/* Explicitly signal disconnect */
|
|
|
|
guac_protocol_send_disconnect(user->socket);
|
|
|
|
guac_socket_flush(user->socket);
|
|
|
|
|
2016-03-01 05:40:39 +00:00
|
|
|
/* Done */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-01 22:51:15 +00:00
|
|
|
int guac_user_handle_connection(guac_user* user, int usec_timeout) {
|
2016-09-11 20:18:27 +00:00
|
|
|
|
|
|
|
guac_socket* socket = user->socket;
|
|
|
|
guac_client* client = user->client;
|
|
|
|
|
|
|
|
/* Send args */
|
|
|
|
if (guac_protocol_send_args(socket, client->args)
|
|
|
|
|| guac_socket_flush(socket)) {
|
|
|
|
|
|
|
|
/* Log error */
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_log_handshake_failure(user);
|
|
|
|
guac_user_log_guac_error(user, GUAC_LOG_DEBUG,
|
2016-09-11 20:18:27 +00:00
|
|
|
"Error sending \"args\" to new user");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
guac_parser* parser = guac_parser_alloc();
|
|
|
|
|
|
|
|
/* Get optimal screen size */
|
2017-07-01 22:24:20 +00:00
|
|
|
if (guac_parser_expect(parser, socket, usec_timeout, "size")) {
|
2016-09-11 20:18:27 +00:00
|
|
|
|
|
|
|
/* Log error */
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_log_handshake_failure(user);
|
|
|
|
guac_user_log_guac_error(user, GUAC_LOG_DEBUG,
|
2016-09-11 20:18:27 +00:00
|
|
|
"Error reading \"size\"");
|
|
|
|
|
|
|
|
guac_parser_free(parser);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate content of size instruction */
|
|
|
|
if (parser->argc < 2) {
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_log(user, GUAC_LOG_ERROR, "Received \"size\" "
|
2016-09-11 20:18:27 +00:00
|
|
|
"instruction lacked required arguments.");
|
|
|
|
guac_parser_free(parser);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse optimal screen dimensions from size instruction */
|
|
|
|
user->info.optimal_width = atoi(parser->argv[0]);
|
|
|
|
user->info.optimal_height = atoi(parser->argv[1]);
|
|
|
|
|
2017-07-01 22:51:15 +00:00
|
|
|
/* If DPI given, set the user resolution */
|
2016-09-11 20:18:27 +00:00
|
|
|
if (parser->argc >= 3)
|
|
|
|
user->info.optimal_resolution = atoi(parser->argv[2]);
|
|
|
|
|
|
|
|
/* Otherwise, use a safe default for rough backwards compatibility */
|
|
|
|
else
|
|
|
|
user->info.optimal_resolution = 96;
|
|
|
|
|
|
|
|
/* Get supported audio formats */
|
2017-07-01 22:24:20 +00:00
|
|
|
if (guac_parser_expect(parser, socket, usec_timeout, "audio")) {
|
2016-09-11 20:18:27 +00:00
|
|
|
|
|
|
|
/* Log error */
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_log_handshake_failure(user);
|
|
|
|
guac_user_log_guac_error(user, GUAC_LOG_DEBUG,
|
2016-09-11 20:18:27 +00:00
|
|
|
"Error reading \"audio\"");
|
|
|
|
|
|
|
|
guac_parser_free(parser);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store audio mimetypes */
|
2017-07-01 22:51:15 +00:00
|
|
|
char** audio_mimetypes = guac_copy_mimetypes(parser->argv, parser->argc);
|
2016-09-11 20:18:27 +00:00
|
|
|
user->info.audio_mimetypes = (const char**) audio_mimetypes;
|
|
|
|
|
|
|
|
/* Get supported video formats */
|
2017-07-01 22:24:20 +00:00
|
|
|
if (guac_parser_expect(parser, socket, usec_timeout, "video")) {
|
2016-09-11 20:18:27 +00:00
|
|
|
|
|
|
|
/* Log error */
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_log_handshake_failure(user);
|
|
|
|
guac_user_log_guac_error(user, GUAC_LOG_DEBUG,
|
2016-09-11 20:18:27 +00:00
|
|
|
"Error reading \"video\"");
|
|
|
|
|
|
|
|
guac_parser_free(parser);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store video mimetypes */
|
2017-07-01 22:51:15 +00:00
|
|
|
char** video_mimetypes = guac_copy_mimetypes(parser->argv, parser->argc);
|
2016-09-11 20:18:27 +00:00
|
|
|
user->info.video_mimetypes = (const char**) video_mimetypes;
|
|
|
|
|
|
|
|
/* Get supported image formats */
|
2017-07-01 22:24:20 +00:00
|
|
|
if (guac_parser_expect(parser, socket, usec_timeout, "image")) {
|
2016-09-11 20:18:27 +00:00
|
|
|
|
|
|
|
/* Log error */
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_log_handshake_failure(user);
|
|
|
|
guac_user_log_guac_error(user, GUAC_LOG_DEBUG,
|
2016-09-11 20:18:27 +00:00
|
|
|
"Error reading \"image\"");
|
|
|
|
|
|
|
|
guac_parser_free(parser);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store image mimetypes */
|
2017-07-01 22:51:15 +00:00
|
|
|
char** image_mimetypes = guac_copy_mimetypes(parser->argv, parser->argc);
|
2016-09-11 20:18:27 +00:00
|
|
|
user->info.image_mimetypes = (const char**) image_mimetypes;
|
|
|
|
|
|
|
|
/* Get args from connect instruction */
|
2017-07-01 22:24:20 +00:00
|
|
|
if (guac_parser_expect(parser, socket, usec_timeout, "connect")) {
|
2016-09-11 20:18:27 +00:00
|
|
|
|
|
|
|
/* Log error */
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_log_handshake_failure(user);
|
|
|
|
guac_user_log_guac_error(user, GUAC_LOG_DEBUG,
|
2016-09-11 20:18:27 +00:00
|
|
|
"Error reading \"connect\"");
|
|
|
|
|
|
|
|
guac_parser_free(parser);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Acknowledge connection availability */
|
|
|
|
guac_protocol_send_ready(socket, client->connection_id);
|
|
|
|
guac_socket_flush(socket);
|
|
|
|
|
|
|
|
/* Attempt join */
|
|
|
|
if (guac_client_add_user(client, user, parser->argc, parser->argv))
|
|
|
|
guac_client_log(client, GUAC_LOG_ERROR, "User \"%s\" could NOT "
|
|
|
|
"join connection \"%s\"", user->user_id, client->connection_id);
|
|
|
|
|
|
|
|
/* Begin user connection if join successful */
|
|
|
|
else {
|
|
|
|
|
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "User \"%s\" joined connection "
|
|
|
|
"\"%s\" (%i users now present)", user->user_id,
|
|
|
|
client->connection_id, client->connected_users);
|
|
|
|
|
|
|
|
/* Handle user I/O, wait for connection to terminate */
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_user_start(parser, user, usec_timeout);
|
2016-09-11 20:18:27 +00:00
|
|
|
|
|
|
|
/* Remove/free user */
|
|
|
|
guac_client_remove_user(client, user);
|
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "User \"%s\" disconnected (%i "
|
|
|
|
"users remain)", user->user_id, client->connected_users);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free mimetype lists */
|
2017-07-01 22:51:15 +00:00
|
|
|
guac_free_mimetypes(audio_mimetypes);
|
|
|
|
guac_free_mimetypes(video_mimetypes);
|
|
|
|
guac_free_mimetypes(image_mimetypes);
|
2016-09-11 20:18:27 +00:00
|
|
|
|
|
|
|
guac_parser_free(parser);
|
|
|
|
|
|
|
|
/* Successful disconnect */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|