GUACAMOLE-221: Migrate telnet handling of "argv" to guac_argv_*() convenience API.

This commit is contained in:
Michael Jumper 2020-07-01 19:02:50 -07:00
parent 2f6de25418
commit 08a57d3375
5 changed files with 51 additions and 169 deletions

View File

@ -29,127 +29,27 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
/** int guac_telnet_argv_callback(guac_user* user, const char* mimetype,
* All telnet connection settings which may be updated by unprivileged users const char* name, const char* value, void* data) {
* through "argv" streams.
*/
typedef enum guac_telnet_argv_setting {
/**
* The color scheme of the terminal.
*/
GUAC_TELNET_ARGV_SETTING_COLOR_SCHEME,
/**
* The name of the font family used by the terminal.
*/
GUAC_TELNET_ARGV_SETTING_FONT_NAME,
/**
* The size of the font used by the terminal, in points.
*/
GUAC_TELNET_ARGV_SETTING_FONT_SIZE
} guac_telnet_argv_setting;
/**
* The value or current status of a connection parameter received over an
* "argv" stream.
*/
typedef struct guac_telnet_argv {
/**
* The specific setting being updated.
*/
guac_telnet_argv_setting setting;
/**
* Buffer space for containing the received argument value.
*/
char buffer[GUAC_TELNET_ARGV_MAX_LENGTH];
/**
* The number of bytes received so far.
*/
int length;
} guac_telnet_argv;
/**
* Handler for "blob" instructions which appends the data from received blobs
* to the end of the in-progress argument value buffer.
*
* @see guac_user_blob_handler
*/
static int guac_telnet_argv_blob_handler(guac_user* user,
guac_stream* stream, void* data, int length) {
guac_telnet_argv* argv = (guac_telnet_argv*) stream->data;
/* Calculate buffer size remaining, including space for null terminator,
* adjusting received length accordingly */
int remaining = sizeof(argv->buffer) - argv->length - 1;
if (length > remaining)
length = remaining;
/* Append received data to end of buffer */
memcpy(argv->buffer + argv->length, data, length);
argv->length += length;
return 0;
}
/**
* Handler for "end" instructions which applies the changes specified by the
* argument value buffer associated with the stream.
*
* @see guac_user_end_handler
*/
static int guac_telnet_argv_end_handler(guac_user* user,
guac_stream* stream) {
int size;
guac_client* client = user->client; guac_client* client = user->client;
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data; guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
guac_terminal* terminal = telnet_client->term; guac_terminal* terminal = telnet_client->term;
/* Append null terminator to value */
guac_telnet_argv* argv = (guac_telnet_argv*) stream->data;
argv->buffer[argv->length] = '\0';
/* Apply changes to chosen setting */
switch (argv->setting) {
/* Update color scheme */ /* Update color scheme */
case GUAC_TELNET_ARGV_SETTING_COLOR_SCHEME: if (strcmp(name, GUAC_TELNET_ARGV_COLOR_SCHEME) == 0)
guac_terminal_apply_color_scheme(terminal, argv->buffer); guac_terminal_apply_color_scheme(terminal, value);
guac_client_stream_argv(client, client->socket, "text/plain",
"color-scheme", argv->buffer);
break;
/* Update font name */ /* Update font name */
case GUAC_TELNET_ARGV_SETTING_FONT_NAME: else if (strcmp(name, GUAC_TELNET_ARGV_FONT_NAME) == 0)
guac_terminal_apply_font(terminal, argv->buffer, -1, 0); guac_terminal_apply_font(terminal, value, -1, 0);
guac_client_stream_argv(client, client->socket, "text/plain",
"font-name", argv->buffer);
break;
/* Update font size */
case GUAC_TELNET_ARGV_SETTING_FONT_SIZE:
/* Update only if font size is sane */ /* Update only if font size is sane */
size = atoi(argv->buffer); else if (strcmp(name, GUAC_TELNET_ARGV_FONT_SIZE) == 0) {
if (size > 0) { int size = atoi(value);
if (size > 0)
guac_terminal_apply_font(terminal, NULL, size, guac_terminal_apply_font(terminal, NULL, size,
telnet_client->settings->resolution); telnet_client->settings->resolution);
guac_client_stream_argv(client, client->socket, "text/plain",
"font-size", argv->buffer);
}
break;
} }
/* Update terminal window size if connected */ /* Update terminal window size if connected */
@ -157,45 +57,6 @@ static int guac_telnet_argv_end_handler(guac_user* user,
guac_telnet_send_naws(telnet_client->telnet, terminal->term_width, guac_telnet_send_naws(telnet_client->telnet, terminal->term_width,
terminal->term_height); terminal->term_height);
free(argv);
return 0;
}
int guac_telnet_argv_handler(guac_user* user, guac_stream* stream,
char* mimetype, char* name) {
guac_telnet_argv_setting setting;
/* Allow users to update the color scheme and font details */
if (strcmp(name, "color-scheme") == 0)
setting = GUAC_TELNET_ARGV_SETTING_COLOR_SCHEME;
else if (strcmp(name, "font-name") == 0)
setting = GUAC_TELNET_ARGV_SETTING_FONT_NAME;
else if (strcmp(name, "font-size") == 0)
setting = GUAC_TELNET_ARGV_SETTING_FONT_SIZE;
/* No other connection parameters may be updated */
else {
guac_protocol_send_ack(user->socket, stream, "Not allowed.",
GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN);
guac_socket_flush(user->socket);
return 0;
}
guac_telnet_argv* argv = malloc(sizeof(guac_telnet_argv));
argv->setting = setting;
argv->length = 0;
/* Prepare stream to receive argument value */
stream->blob_handler = guac_telnet_argv_blob_handler;
stream->end_handler = guac_telnet_argv_end_handler;
stream->data = argv;
/* Signal stream is ready */
guac_protocol_send_ack(user->socket, stream, "Ready for updated "
"parameter.", GUAC_PROTOCOL_STATUS_SUCCESS);
guac_socket_flush(user->socket);
return 0; return 0;
} }
@ -206,18 +67,18 @@ void* guac_telnet_send_current_argv(guac_user* user, void* data) {
guac_terminal* terminal = telnet_client->term; guac_terminal* terminal = telnet_client->term;
/* Send current color scheme */ /* Send current color scheme */
guac_user_stream_argv(user, user->socket, "text/plain", "color-scheme", guac_user_stream_argv(user, user->socket, "text/plain",
terminal->color_scheme); GUAC_TELNET_ARGV_COLOR_SCHEME, terminal->color_scheme);
/* Send current font name */ /* Send current font name */
guac_user_stream_argv(user, user->socket, "text/plain", "font-name", guac_user_stream_argv(user, user->socket, "text/plain",
terminal->font_name); GUAC_TELNET_ARGV_FONT_NAME, terminal->font_name);
/* Send current font size */ /* Send current font size */
char font_size[64]; char font_size[64];
sprintf(font_size, "%i", terminal->font_size); sprintf(font_size, "%i", terminal->font_size);
guac_user_stream_argv(user, user->socket, "text/plain", "font-size", guac_user_stream_argv(user, user->socket, "text/plain",
font_size); GUAC_TELNET_ARGV_FONT_SIZE, font_size);
return NULL; return NULL;

View File

@ -23,19 +23,32 @@
#include "config.h" #include "config.h"
#include <guacamole/argv.h>
#include <guacamole/user.h> #include <guacamole/user.h>
/** /**
* The maximum number of bytes to allow for any argument value received via an * The name of the parameter that specifies/updates the color scheme used by
* argv stream, including null terminator. * the terminal emulator.
*/ */
#define GUAC_TELNET_ARGV_MAX_LENGTH 16384 #define GUAC_TELNET_ARGV_COLOR_SCHEME "color-scheme"
/** /**
* Handles an incoming stream from a Guacamole "argv" instruction, updating the * The name of the parameter that specifies/updates the name of the font used
* given connection parameter if that parameter is allowed to be updated. * by the terminal emulator.
*/ */
guac_user_argv_handler guac_telnet_argv_handler; #define GUAC_TELNET_ARGV_FONT_NAME "font-name"
/**
* The name of the parameter that specifies/updates the font size used by the
* terminal emulator.
*/
#define GUAC_TELNET_ARGV_FONT_SIZE "font-size"
/**
* Handles a received argument value from a Guacamole "argv" instruction,
* updating the given connection parameter.
*/
guac_argv_callback guac_telnet_argv_callback;
/** /**
* Sends the current values of all non-sensitive parameters which may be set * Sends the current values of all non-sensitive parameters which may be set

View File

@ -18,6 +18,7 @@
*/ */
#include "config.h" #include "config.h"
#include "argv.h"
#include "client.h" #include "client.h"
#include "common/recording.h" #include "common/recording.h"
#include "settings.h" #include "settings.h"
@ -31,6 +32,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <guacamole/argv.h>
#include <guacamole/client.h> #include <guacamole/client.h>
int guac_client_init(guac_client* client) { int guac_client_init(guac_client* client) {
@ -54,6 +56,11 @@ int guac_client_init(guac_client* client) {
client->join_handler = guac_telnet_user_join_handler; client->join_handler = guac_telnet_user_join_handler;
client->free_handler = guac_telnet_client_free_handler; client->free_handler = guac_telnet_client_free_handler;
/* Register handlers for argument values that may be sent after the handshake */
guac_argv_register(GUAC_TELNET_ARGV_COLOR_SCHEME, guac_telnet_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
guac_argv_register(GUAC_TELNET_ARGV_FONT_NAME, guac_telnet_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
guac_argv_register(GUAC_TELNET_ARGV_FONT_SIZE, guac_telnet_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
/* Set locale and warn if not UTF-8 */ /* Set locale and warn if not UTF-8 */
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) { if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) {

View File

@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "argv.h"
#include "common/defaults.h" #include "common/defaults.h"
#include "settings.h" #include "settings.h"
@ -39,9 +40,9 @@ const char* GUAC_TELNET_CLIENT_ARGS[] = {
"username-regex", "username-regex",
"password", "password",
"password-regex", "password-regex",
"font-name", GUAC_TELNET_ARGV_FONT_NAME,
"font-size", GUAC_TELNET_ARGV_FONT_SIZE,
"color-scheme", GUAC_TELNET_ARGV_COLOR_SCHEME,
"typescript-path", "typescript-path",
"typescript-name", "typescript-name",
"create-typescript-path", "create-typescript-path",

View File

@ -92,7 +92,7 @@ int guac_telnet_user_join_handler(guac_user* user, int argc, char** argv) {
user->pipe_handler = guac_telnet_pipe_handler; user->pipe_handler = guac_telnet_pipe_handler;
/* Updates to connection parameters */ /* Updates to connection parameters */
user->argv_handler = guac_telnet_argv_handler; user->argv_handler = guac_argv_handler;
/* Display size change events */ /* Display size change events */
user->size_handler = guac_telnet_user_size_handler; user->size_handler = guac_telnet_user_size_handler;