GUACAMOLE-630: Allow color scheme to be changed from webapp via argv streams.

This commit is contained in:
Michael Jumper 2018-09-23 00:24:19 -07:00
parent 6f9f2189f2
commit 2f16eadb35
12 changed files with 385 additions and 5 deletions

View File

@ -29,6 +29,7 @@ ACLOCAL_AMFLAGS = -I m4
lib_LTLIBRARIES = libguac-client-ssh.la lib_LTLIBRARIES = libguac-client-ssh.la
libguac_client_ssh_la_SOURCES = \ libguac_client_ssh_la_SOURCES = \
argv.c \
client.c \ client.c \
clipboard.c \ clipboard.c \
input.c \ input.c \
@ -40,6 +41,7 @@ libguac_client_ssh_la_SOURCES = \
user.c user.c
noinst_HEADERS = \ noinst_HEADERS = \
argv.h \
client.h \ client.h \
clipboard.h \ clipboard.h \
input.h \ input.h \

128
src/protocols/ssh/argv.c Normal file
View File

@ -0,0 +1,128 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
#include "config.h"
#include "argv.h"
#include "ssh.h"
#include "terminal/terminal.h"
#include <guacamole/protocol.h>
#include <guacamole/socket.h>
#include <guacamole/user.h>
#include <stdlib.h>
#include <string.h>
/**
* The value or current status of a connection parameter received over an
* "argv" stream.
*/
typedef struct guac_ssh_argv {
/**
* Buffer space for containing the received argument value.
*/
char buffer[16384];
/**
* The number of bytes received so far.
*/
int length;
} guac_ssh_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_ssh_argv_blob_handler(guac_user* user,
guac_stream* stream, void* data, int length) {
guac_ssh_argv* argv = (guac_ssh_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_ssh_argv_end_handler(guac_user* user,
guac_stream* stream) {
guac_client* client = user->client;
guac_ssh_client* telnet_client = (guac_ssh_client*) client->data;
guac_terminal* terminal = telnet_client->term;
/* Append null terminator to value */
guac_ssh_argv* argv = (guac_ssh_argv*) stream->data;
argv->buffer[argv->length] = '\0';
/* Update color scheme */
guac_terminal_apply_color_scheme(terminal, argv->buffer);
free(argv);
return 0;
}
int guac_ssh_argv_handler(guac_user* user, guac_stream* stream,
char* mimetype, char* name) {
/* Allow users to update the color scheme */
if (strcmp(name, "color-scheme") == 0) {
guac_ssh_argv* argv = malloc(sizeof(guac_ssh_argv));
argv->length = 0;
/* Prepare stream to receive argument value */
stream->blob_handler = guac_ssh_argv_blob_handler;
stream->end_handler = guac_ssh_argv_end_handler;
stream->data = argv;
/* Signal stream is ready */
guac_protocol_send_ack(user->socket, stream, "Ready for color "
"scheme.", GUAC_PROTOCOL_STATUS_SUCCESS);
guac_socket_flush(user->socket);
return 0;
}
/* No other connection parameters may be updated */
guac_protocol_send_ack(user->socket, stream, "Not allowed.",
GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN);
guac_socket_flush(user->socket);
return 0;
}

35
src/protocols/ssh/argv.h Normal file
View File

@ -0,0 +1,35 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
#ifndef GUAC_SSH_ARGV_H
#define GUAC_SSH_ARGV_H
#include "config.h"
#include <guacamole/user.h>
/**
* Handles an incoming stream from a Guacamole "argv" instruction, updating the
* given connection parameter if that parameter is allowed to be updated.
*/
guac_user_argv_handler guac_ssh_argv_handler;
#endif

View File

@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "argv.h"
#include "clipboard.h" #include "clipboard.h"
#include "common/display.h" #include "common/display.h"
#include "input.h" #include "input.h"
@ -87,6 +88,9 @@ int guac_ssh_user_join_handler(guac_user* user, int argc, char** argv) {
/* STDIN redirection */ /* STDIN redirection */
user->pipe_handler = guac_ssh_pipe_handler; user->pipe_handler = guac_ssh_pipe_handler;
/* Updates to connection parameters */
user->argv_handler = guac_ssh_argv_handler;
/* Display size change events */ /* Display size change events */
user->size_handler = guac_ssh_user_size_handler; user->size_handler = guac_ssh_user_size_handler;

View File

@ -29,6 +29,7 @@ ACLOCAL_AMFLAGS = -I m4
lib_LTLIBRARIES = libguac-client-telnet.la lib_LTLIBRARIES = libguac-client-telnet.la
libguac_client_telnet_la_SOURCES = \ libguac_client_telnet_la_SOURCES = \
argv.c \
client.c \ client.c \
clipboard.c \ clipboard.c \
input.c \ input.c \
@ -38,6 +39,7 @@ libguac_client_telnet_la_SOURCES = \
user.c user.c
noinst_HEADERS = \ noinst_HEADERS = \
argv.h \
client.h \ client.h \
clipboard.h \ clipboard.h \
input.h \ input.h \

128
src/protocols/telnet/argv.c Normal file
View File

@ -0,0 +1,128 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
#include "config.h"
#include "argv.h"
#include "telnet.h"
#include "terminal/terminal.h"
#include <guacamole/protocol.h>
#include <guacamole/socket.h>
#include <guacamole/user.h>
#include <stdlib.h>
#include <string.h>
/**
* The value or current status of a connection parameter received over an
* "argv" stream.
*/
typedef struct guac_telnet_argv {
/**
* Buffer space for containing the received argument value.
*/
char buffer[16384];
/**
* 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) {
guac_client* client = user->client;
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
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';
/* Update color scheme */
guac_terminal_apply_color_scheme(terminal, argv->buffer);
free(argv);
return 0;
}
int guac_telnet_argv_handler(guac_user* user, guac_stream* stream,
char* mimetype, char* name) {
/* Allow users to update the color scheme */
if (strcmp(name, "color-scheme") == 0) {
guac_telnet_argv* argv = malloc(sizeof(guac_telnet_argv));
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 color "
"scheme.", GUAC_PROTOCOL_STATUS_SUCCESS);
guac_socket_flush(user->socket);
return 0;
}
/* No other connection parameters may be updated */
guac_protocol_send_ack(user->socket, stream, "Not allowed.",
GUAC_PROTOCOL_STATUS_CLIENT_FORBIDDEN);
guac_socket_flush(user->socket);
return 0;
}

View File

@ -0,0 +1,35 @@
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
#ifndef GUAC_TELNET_ARGV_H
#define GUAC_TELNET_ARGV_H
#include "config.h"
#include <guacamole/user.h>
/**
* Handles an incoming stream from a Guacamole "argv" instruction, updating the
* given connection parameter if that parameter is allowed to be updated.
*/
guac_user_argv_handler guac_telnet_argv_handler;
#endif

View File

@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "argv.h"
#include "clipboard.h" #include "clipboard.h"
#include "input.h" #include "input.h"
#include "pipe.h" #include "pipe.h"
@ -86,6 +87,9 @@ int guac_telnet_user_join_handler(guac_user* user, int argc, char** argv) {
/* STDIN redirection */ /* STDIN redirection */
user->pipe_handler = guac_telnet_pipe_handler; user->pipe_handler = guac_telnet_pipe_handler;
/* Updates to connection parameters */
user->argv_handler = guac_telnet_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;

View File

@ -199,7 +199,7 @@ int __guac_terminal_set(guac_terminal_display* display, int row, int col, int co
guac_terminal_display* guac_terminal_display_alloc(guac_client* client, guac_terminal_display* guac_terminal_display_alloc(guac_client* client,
const char* font_name, int font_size, int dpi, const char* font_name, int font_size, int dpi,
guac_terminal_color* foreground, guac_terminal_color* background, guac_terminal_color* foreground, guac_terminal_color* background,
const guac_terminal_color (*palette)[256]) { guac_terminal_color (*palette)[256]) {
PangoFontMap* font_map; PangoFontMap* font_map;
PangoFont* font; PangoFont* font;
@ -271,7 +271,7 @@ guac_terminal_display* guac_terminal_display_alloc(guac_client* client,
void guac_terminal_display_free(guac_terminal_display* display) { void guac_terminal_display_free(guac_terminal_display* display) {
/* Free default palette. */ /* Free default palette. */
free((void*) display->default_palette); free(display->default_palette);
/* Free operations buffers */ /* Free operations buffers */
free(display->operations); free(display->operations);

View File

@ -384,7 +384,7 @@ guac_terminal* guac_terminal_create(guac_client* client,
font_name, font_size, dpi, font_name, font_size, dpi,
&default_char.attributes.foreground, &default_char.attributes.foreground,
&default_char.attributes.background, &default_char.attributes.background,
(const guac_terminal_color(*)[256]) default_palette); (guac_terminal_color(*)[256]) default_palette);
/* Fail if display init failed */ /* Fail if display init failed */
if (term->display == NULL) { if (term->display == NULL) {
@ -1940,3 +1940,30 @@ void guac_terminal_dup(guac_terminal* term, guac_user* user,
} }
void guac_terminal_apply_color_scheme(guac_terminal* terminal,
const char* color_scheme) {
guac_client* client = terminal->client;
guac_terminal_char* default_char = &terminal->default_char;
guac_terminal_display* display = terminal->display;
/* Reinitialize default terminal colors with values from color scheme */
guac_terminal_parse_color_scheme(client, color_scheme,
&default_char->attributes.foreground,
&default_char->attributes.background,
display->default_palette);
/* Reinitialize default attributes of buffer and display */
terminal->buffer->default_character = *default_char;
display->default_foreground = default_char->attributes.foreground;
display->default_background = default_char->attributes.background;
/* Redraw background with new color */
guac_terminal_repaint_default_layer(terminal, client->socket);
/* Force reset of terminal state */
guac_terminal_reset(terminal);
guac_terminal_notify(terminal);
}

View File

@ -142,7 +142,7 @@ typedef struct guac_terminal_display {
* The default palette. Use GUAC_TERMINAL_INITIAL_PALETTE if null. * The default palette. Use GUAC_TERMINAL_INITIAL_PALETTE if null.
* Must free on destruction if not null. * Must free on destruction if not null.
*/ */
const guac_terminal_color (*default_palette)[256]; guac_terminal_color (*default_palette)[256];
/** /**
* Default foreground color for all glyphs. * Default foreground color for all glyphs.
@ -215,7 +215,7 @@ typedef struct guac_terminal_display {
guac_terminal_display* guac_terminal_display_alloc(guac_client* client, guac_terminal_display* guac_terminal_display_alloc(guac_client* client,
const char* font_name, int font_size, int dpi, const char* font_name, int font_size, int dpi,
guac_terminal_color* foreground, guac_terminal_color* background, guac_terminal_color* foreground, guac_terminal_color* background,
const guac_terminal_color (*palette)[256]); guac_terminal_color (*palette)[256]);
/** /**
* Frees the given display. * Frees the given display.

View File

@ -1072,5 +1072,20 @@ int guac_terminal_create_typescript(guac_terminal* term, const char* path,
*/ */
int guac_terminal_available_scroll(guac_terminal* term); int guac_terminal_available_scroll(guac_terminal* term);
/**
* Immediately applies the given color scheme to the given terminal, overriding
* the color scheme provided when the terminal was created. Applying the color
* scheme implicitly clears the display and resets the terminal state. Valid
* color schemes are those accepted by guac_terminal_parse_color_scheme().
*
* @param terminal
* The terminal to apply the color scheme to.
*
* @param color_scheme
* The color scheme to apply.
*/
void guac_terminal_apply_color_scheme(guac_terminal* terminal,
const char* color_scheme);
#endif #endif