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

This commit is contained in:
Michael Jumper 2020-07-01 18:44:23 -07:00
parent f8f2c7f747
commit 2f6de25418
5 changed files with 52 additions and 169 deletions

View File

@ -30,127 +30,27 @@
#include <stdlib.h>
#include <string.h>
/**
* All SSH connection settings which may be updated by unprivileged users
* through "argv" streams.
*/
typedef enum guac_ssh_argv_setting {
/**
* The color scheme of the terminal.
*/
GUAC_SSH_ARGV_SETTING_COLOR_SCHEME,
/**
* The name of the font family used by the terminal.
*/
GUAC_SSH_ARGV_SETTING_FONT_NAME,
/**
* The size of the font used by the terminal, in points.
*/
GUAC_SSH_ARGV_SETTING_FONT_SIZE
} guac_ssh_argv_setting;
/**
* The value or current status of a connection parameter received over an
* "argv" stream.
*/
typedef struct guac_ssh_argv {
/**
* The specific setting being updated.
*/
guac_ssh_argv_setting setting;
/**
* Buffer space for containing the received argument value.
*/
char buffer[GUAC_SSH_ARGV_MAX_LENGTH];
/**
* 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) {
int size;
int guac_ssh_argv_callback(guac_user* user, const char* mimetype,
const char* name, const char* value, void* data) {
guac_client* client = user->client;
guac_ssh_client* ssh_client = (guac_ssh_client*) client->data;
guac_terminal* terminal = ssh_client->term;
/* Append null terminator to value */
guac_ssh_argv* argv = (guac_ssh_argv*) stream->data;
argv->buffer[argv->length] = '\0';
/* Update color scheme */
if (strcmp(name, GUAC_SSH_ARGV_COLOR_SCHEME) == 0)
guac_terminal_apply_color_scheme(terminal, value);
/* Apply changes to chosen setting */
switch (argv->setting) {
/* Update color scheme */
case GUAC_SSH_ARGV_SETTING_COLOR_SCHEME:
guac_terminal_apply_color_scheme(terminal, argv->buffer);
guac_client_stream_argv(client, client->socket, "text/plain",
"color-scheme", argv->buffer);
break;
/* Update font name */
case GUAC_SSH_ARGV_SETTING_FONT_NAME:
guac_terminal_apply_font(terminal, argv->buffer, -1, 0);
guac_client_stream_argv(client, client->socket, "text/plain",
"font-name", argv->buffer);
break;
/* Update font size */
case GUAC_SSH_ARGV_SETTING_FONT_SIZE:
/* Update only if font size is sane */
size = atoi(argv->buffer);
if (size > 0) {
guac_terminal_apply_font(terminal, NULL, size,
ssh_client->settings->resolution);
guac_client_stream_argv(client, client->socket, "text/plain",
"font-size", argv->buffer);
}
break;
/* Update font name */
else if (strcmp(name, GUAC_SSH_ARGV_FONT_NAME) == 0)
guac_terminal_apply_font(terminal, value, -1, 0);
/* Update only if font size is sane */
else if (strcmp(name, GUAC_SSH_ARGV_FONT_SIZE) == 0) {
int size = atoi(value);
if (size > 0)
guac_terminal_apply_font(terminal, NULL, size,
ssh_client->settings->resolution);
}
/* Update SSH pty size if connected */
@ -161,45 +61,6 @@ static int guac_ssh_argv_end_handler(guac_user* user,
pthread_mutex_unlock(&(ssh_client->term_channel_lock));
}
free(argv);
return 0;
}
int guac_ssh_argv_handler(guac_user* user, guac_stream* stream,
char* mimetype, char* name) {
guac_ssh_argv_setting setting;
/* Allow users to update the color scheme and font details */
if (strcmp(name, "color-scheme") == 0)
setting = GUAC_SSH_ARGV_SETTING_COLOR_SCHEME;
else if (strcmp(name, "font-name") == 0)
setting = GUAC_SSH_ARGV_SETTING_FONT_NAME;
else if (strcmp(name, "font-size") == 0)
setting = GUAC_SSH_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_ssh_argv* argv = malloc(sizeof(guac_ssh_argv));
argv->setting = setting;
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 updated "
"parameter.", GUAC_PROTOCOL_STATUS_SUCCESS);
guac_socket_flush(user->socket);
return 0;
}
@ -210,18 +71,18 @@ void* guac_ssh_send_current_argv(guac_user* user, void* data) {
guac_terminal* terminal = ssh_client->term;
/* Send current color scheme */
guac_user_stream_argv(user, user->socket, "text/plain", "color-scheme",
terminal->color_scheme);
guac_user_stream_argv(user, user->socket, "text/plain",
GUAC_SSH_ARGV_COLOR_SCHEME, terminal->color_scheme);
/* Send current font name */
guac_user_stream_argv(user, user->socket, "text/plain", "font-name",
terminal->font_name);
guac_user_stream_argv(user, user->socket, "text/plain",
GUAC_SSH_ARGV_FONT_NAME, terminal->font_name);
/* Send current font size */
char font_size[64];
sprintf(font_size, "%i", terminal->font_size);
guac_user_stream_argv(user, user->socket, "text/plain", "font-size",
font_size);
guac_user_stream_argv(user, user->socket, "text/plain",
GUAC_SSH_ARGV_FONT_SIZE, font_size);
return NULL;

View File

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

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "argv.h"
#include "client.h"
#include "common/clipboard.h"
#include "common/recording.h"
@ -32,6 +33,7 @@
#include <stdlib.h>
#include <string.h>
#include <guacamole/argv.h>
#include <guacamole/client.h>
int guac_client_init(guac_client* client) {
@ -50,6 +52,11 @@ int guac_client_init(guac_client* client) {
client->join_handler = guac_ssh_user_join_handler;
client->free_handler = guac_ssh_client_free_handler;
/* Register handlers for argument values that may be sent after the handshake */
guac_argv_register(GUAC_SSH_ARGV_COLOR_SCHEME, guac_ssh_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
guac_argv_register(GUAC_SSH_ARGV_FONT_NAME, guac_ssh_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
guac_argv_register(GUAC_SSH_ARGV_FONT_SIZE, guac_ssh_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
/* Set locale and warn if not UTF-8 */
setlocale(LC_CTYPE, "");
if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) {

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "argv.h"
#include "client.h"
#include "common/defaults.h"
#include "settings.h"
@ -37,8 +38,8 @@ const char* GUAC_SSH_CLIENT_ARGS[] = {
"port",
"username",
"password",
"font-name",
"font-size",
GUAC_SSH_ARGV_FONT_NAME,
GUAC_SSH_ARGV_FONT_SIZE,
"enable-sftp",
"sftp-root-directory",
"sftp-disable-download",
@ -48,7 +49,7 @@ const char* GUAC_SSH_CLIENT_ARGS[] = {
#ifdef ENABLE_SSH_AGENT
"enable-agent",
#endif
"color-scheme",
GUAC_SSH_ARGV_COLOR_SCHEME,
"command",
"typescript-path",
"typescript-name",

View File

@ -29,6 +29,7 @@
#include "ssh.h"
#include "settings.h"
#include <guacamole/argv.h>
#include <guacamole/client.h>
#include <guacamole/socket.h>
#include <guacamole/user.h>
@ -93,7 +94,7 @@ int guac_ssh_user_join_handler(guac_user* user, int argc, char** argv) {
user->pipe_handler = guac_ssh_pipe_handler;
/* Updates to connection parameters */
user->argv_handler = guac_ssh_argv_handler;
user->argv_handler = guac_argv_handler;
/* Display size change events */
user->size_handler = guac_ssh_user_size_handler;