GUACAMOLE-860: [WIP] Initial copy of telnet to tn5250 protocol.
This commit is contained in:
parent
289ceac222
commit
22fe81913f
65
src/protocols/tn5250/Makefile.am
Normal file
65
src/protocols/tn5250/Makefile.am
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
|
# NOTE: Parts of this file (Makefile.am) are automatically transcluded verbatim
|
||||||
|
# into Makefile.in. Though the build system (GNU Autotools) automatically adds
|
||||||
|
# its own license boilerplate to the generated Makefile.in, that boilerplate
|
||||||
|
# does not apply to the transcluded portions of Makefile.am which are licensed
|
||||||
|
# to you by the ASF under the Apache License, Version 2.0, as described above.
|
||||||
|
#
|
||||||
|
|
||||||
|
AUTOMAKE_OPTIONS = foreign
|
||||||
|
ACLOCAL_AMFLAGS = -I m4
|
||||||
|
|
||||||
|
lib_LTLIBRARIES = libguac-client-tn5250.la
|
||||||
|
|
||||||
|
libguac_client_tn5250_la_SOURCES = \
|
||||||
|
argv.c \
|
||||||
|
client.c \
|
||||||
|
clipboard.c \
|
||||||
|
input.c \
|
||||||
|
pipe.c \
|
||||||
|
settings.c \
|
||||||
|
tn5250.c \
|
||||||
|
user.c
|
||||||
|
|
||||||
|
noinst_HEADERS = \
|
||||||
|
argv.h \
|
||||||
|
client.h \
|
||||||
|
clipboard.h \
|
||||||
|
input.h \
|
||||||
|
pipe.h \
|
||||||
|
settings.h \
|
||||||
|
tn5250.h \
|
||||||
|
user.h
|
||||||
|
|
||||||
|
libguac_client_tn5250_la_CFLAGS = \
|
||||||
|
-Werror -Wall -Iinclude \
|
||||||
|
@LIBGUAC_INCLUDE@ \
|
||||||
|
@TERMINAL_INCLUDE@
|
||||||
|
|
||||||
|
libguac_client_tn5250_la_LIBADD = \
|
||||||
|
@COMMON_LTLIB@ \
|
||||||
|
@LIBGUAC_LTLIB@ \
|
||||||
|
@TERMINAL_LTLIB@
|
||||||
|
|
||||||
|
libguac_client_tn5250_la_LDFLAGS = \
|
||||||
|
-version-info 0:0:0 \
|
||||||
|
@PTHREAD_LIBS@ \
|
||||||
|
@TN5250_LIBS@
|
||||||
|
|
196
src/protocols/tn5250/argv.c
Normal file
196
src/protocols/tn5250/argv.c
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
/*
|
||||||
|
* 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 "tn5250.h"
|
||||||
|
#include "terminal/terminal.h"
|
||||||
|
|
||||||
|
#include <guacamole/protocol.h>
|
||||||
|
#include <guacamole/socket.h>
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All telnet connection settings which may be updated by unprivileged users
|
||||||
|
* 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_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';
|
||||||
|
|
||||||
|
/* Apply changes to chosen setting */
|
||||||
|
switch (argv->setting) {
|
||||||
|
|
||||||
|
/* Update color scheme */
|
||||||
|
case GUAC_TELNET_ARGV_SETTING_COLOR_SCHEME:
|
||||||
|
guac_terminal_apply_color_scheme(terminal, argv->buffer);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Update font name */
|
||||||
|
case GUAC_TELNET_ARGV_SETTING_FONT_NAME:
|
||||||
|
guac_terminal_apply_font(terminal, argv->buffer, -1, 0);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Update font size */
|
||||||
|
case GUAC_TELNET_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,
|
||||||
|
telnet_client->settings->resolution);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Update terminal window size if connected */
|
||||||
|
if (telnet_client->telnet != NULL && telnet_client->naws_enabled)
|
||||||
|
guac_telnet_send_naws(telnet_client->telnet, terminal->term_width,
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
41
src/protocols/tn5250/argv.h
Normal file
41
src/protocols/tn5250/argv.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* 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>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum number of bytes to allow for any argument value received via an
|
||||||
|
* argv stream, including null terminator.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_ARGV_MAX_LENGTH 16384
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
|
100
src/protocols/tn5250/client.c
Normal file
100
src/protocols/tn5250/client.c
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* 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 "client.h"
|
||||||
|
#include "common/recording.h"
|
||||||
|
#include "settings.h"
|
||||||
|
#include "telnet.h"
|
||||||
|
#include "terminal/terminal.h"
|
||||||
|
#include "user.h"
|
||||||
|
|
||||||
|
#include <langinfo.h>
|
||||||
|
#include <locale.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <guacamole/client.h>
|
||||||
|
|
||||||
|
int guac_client_init(guac_client* client) {
|
||||||
|
|
||||||
|
/* Set client args */
|
||||||
|
client->args = GUAC_TELNET_CLIENT_ARGS;
|
||||||
|
|
||||||
|
/* Allocate client instance data */
|
||||||
|
guac_telnet_client* telnet_client = calloc(1, sizeof(guac_telnet_client));
|
||||||
|
client->data = telnet_client;
|
||||||
|
|
||||||
|
/* Init clipboard */
|
||||||
|
telnet_client->clipboard = guac_common_clipboard_alloc(GUAC_TELNET_CLIPBOARD_MAX_LENGTH);
|
||||||
|
|
||||||
|
/* Init telnet client */
|
||||||
|
telnet_client->socket_fd = -1;
|
||||||
|
telnet_client->naws_enabled = 0;
|
||||||
|
telnet_client->echo_enabled = 1;
|
||||||
|
|
||||||
|
/* Set handlers */
|
||||||
|
client->join_handler = guac_telnet_user_join_handler;
|
||||||
|
client->free_handler = guac_telnet_client_free_handler;
|
||||||
|
|
||||||
|
/* Set locale and warn if not UTF-8 */
|
||||||
|
setlocale(LC_CTYPE, "");
|
||||||
|
if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) {
|
||||||
|
guac_client_log(client, GUAC_LOG_INFO,
|
||||||
|
"Current locale does not use UTF-8. Some characters may "
|
||||||
|
"not render correctly.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Success */
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int guac_telnet_client_free_handler(guac_client* client) {
|
||||||
|
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
|
||||||
|
/* Close telnet connection */
|
||||||
|
if (telnet_client->socket_fd != -1)
|
||||||
|
close(telnet_client->socket_fd);
|
||||||
|
|
||||||
|
/* Clean up recording, if in progress */
|
||||||
|
if (telnet_client->recording != NULL)
|
||||||
|
guac_common_recording_free(telnet_client->recording);
|
||||||
|
|
||||||
|
/* Kill terminal */
|
||||||
|
guac_terminal_free(telnet_client->term);
|
||||||
|
|
||||||
|
/* Wait for and free telnet session, if connected */
|
||||||
|
if (telnet_client->telnet != NULL) {
|
||||||
|
pthread_join(telnet_client->client_thread, NULL);
|
||||||
|
telnet_free(telnet_client->telnet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Free settings */
|
||||||
|
if (telnet_client->settings != NULL)
|
||||||
|
guac_telnet_settings_free(telnet_client->settings);
|
||||||
|
|
||||||
|
guac_common_clipboard_free(telnet_client->clipboard);
|
||||||
|
free(telnet_client);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
44
src/protocols/tn5250/client.h
Normal file
44
src/protocols/tn5250/client.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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__CLIENT_H
|
||||||
|
#define GUAC_TELNET__CLIENT_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "terminal/terminal.h"
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <regex.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include <libtelnet.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum number of bytes to allow within the clipboard.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_CLIPBOARD_MAX_LENGTH 262144
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free handler. Required by libguac and called when the guac_client is
|
||||||
|
* disconnected and must be cleaned up.
|
||||||
|
*/
|
||||||
|
guac_client_free_handler guac_telnet_client_free_handler;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
62
src/protocols/tn5250/clipboard.c
Normal file
62
src/protocols/tn5250/clipboard.c
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* 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 "clipboard.h"
|
||||||
|
#include "common/clipboard.h"
|
||||||
|
#include "telnet.h"
|
||||||
|
#include "terminal/terminal.h"
|
||||||
|
|
||||||
|
#include <guacamole/client.h>
|
||||||
|
#include <guacamole/stream.h>
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
|
||||||
|
int guac_telnet_clipboard_handler(guac_user* user, guac_stream* stream,
|
||||||
|
char* mimetype) {
|
||||||
|
|
||||||
|
/* Clear clipboard and prepare for new data */
|
||||||
|
guac_client* client = user->client;
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
guac_common_clipboard_reset(telnet_client->clipboard, mimetype);
|
||||||
|
|
||||||
|
/* Set handlers for clipboard stream */
|
||||||
|
stream->blob_handler = guac_telnet_clipboard_blob_handler;
|
||||||
|
stream->end_handler = guac_telnet_clipboard_end_handler;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int guac_telnet_clipboard_blob_handler(guac_user* user, guac_stream* stream,
|
||||||
|
void* data, int length) {
|
||||||
|
|
||||||
|
/* Append new data */
|
||||||
|
guac_client* client = user->client;
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
guac_common_clipboard_append(telnet_client->clipboard, data, length);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int guac_telnet_clipboard_end_handler(guac_user* user, guac_stream* stream) {
|
||||||
|
|
||||||
|
/* Nothing to do - clipboard is implemented within client */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
43
src/protocols/tn5250/clipboard.h
Normal file
43
src/protocols/tn5250/clipboard.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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_CLIPBOARD_H
|
||||||
|
#define GUAC_TELNET_CLIPBOARD_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for inbound clipboard streams.
|
||||||
|
*/
|
||||||
|
guac_user_clipboard_handler guac_telnet_clipboard_handler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for data received along clipboard streams.
|
||||||
|
*/
|
||||||
|
guac_user_blob_handler guac_telnet_clipboard_blob_handler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for end-of-stream related to clipboard.
|
||||||
|
*/
|
||||||
|
guac_user_end_handler guac_telnet_clipboard_end_handler;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
140
src/protocols/tn5250/input.c
Normal file
140
src/protocols/tn5250/input.c
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
/*
|
||||||
|
* 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 "common/recording.h"
|
||||||
|
#include "input.h"
|
||||||
|
#include "terminal/terminal.h"
|
||||||
|
#include "telnet.h"
|
||||||
|
|
||||||
|
#include <guacamole/client.h>
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
#include <libtelnet.h>
|
||||||
|
|
||||||
|
#include <regex.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int guac_telnet_user_mouse_handler(guac_user* user, int x, int y, int mask) {
|
||||||
|
|
||||||
|
guac_client* client = user->client;
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
guac_telnet_settings* settings = telnet_client->settings;
|
||||||
|
guac_terminal* term = telnet_client->term;
|
||||||
|
|
||||||
|
/* Skip if terminal not yet ready */
|
||||||
|
if (term == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Report mouse position within recording */
|
||||||
|
if (telnet_client->recording != NULL)
|
||||||
|
guac_common_recording_report_mouse(telnet_client->recording, x, y,
|
||||||
|
mask);
|
||||||
|
|
||||||
|
/* Send mouse if not searching for password or username */
|
||||||
|
if (settings->password_regex == NULL && settings->username_regex == NULL)
|
||||||
|
guac_terminal_send_mouse(term, user, x, y, mask);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int guac_telnet_user_key_handler(guac_user* user, int keysym, int pressed) {
|
||||||
|
|
||||||
|
guac_client* client = user->client;
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
guac_telnet_settings* settings = telnet_client->settings;
|
||||||
|
guac_terminal* term = telnet_client->term;
|
||||||
|
|
||||||
|
/* Report key state within recording */
|
||||||
|
if (telnet_client->recording != NULL)
|
||||||
|
guac_common_recording_report_key(telnet_client->recording,
|
||||||
|
keysym, pressed);
|
||||||
|
|
||||||
|
/* Skip if terminal not yet ready */
|
||||||
|
if (term == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Stop searching for password */
|
||||||
|
if (settings->password_regex != NULL) {
|
||||||
|
|
||||||
|
guac_client_log(client, GUAC_LOG_DEBUG,
|
||||||
|
"Stopping password prompt search due to user input.");
|
||||||
|
|
||||||
|
regfree(settings->password_regex);
|
||||||
|
free(settings->password_regex);
|
||||||
|
settings->password_regex = NULL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Stop searching for username */
|
||||||
|
if (settings->username_regex != NULL) {
|
||||||
|
|
||||||
|
guac_client_log(client, GUAC_LOG_DEBUG,
|
||||||
|
"Stopping username prompt search due to user input.");
|
||||||
|
|
||||||
|
regfree(settings->username_regex);
|
||||||
|
free(settings->username_regex);
|
||||||
|
settings->username_regex = NULL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Intercept and handle Pause / Break / Ctrl+0 as "IAC BRK" */
|
||||||
|
if (pressed && (
|
||||||
|
keysym == 0xFF13 /* Pause */
|
||||||
|
|| keysym == 0xFF6B /* Break */
|
||||||
|
|| (term->mod_ctrl && keysym == '0') /* Ctrl + 0 */
|
||||||
|
)) {
|
||||||
|
|
||||||
|
/* Send IAC BRK */
|
||||||
|
telnet_iac(telnet_client->telnet, TELNET_BREAK);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Send key */
|
||||||
|
guac_terminal_send_key(term, keysym, pressed);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int guac_telnet_user_size_handler(guac_user* user, int width, int height) {
|
||||||
|
|
||||||
|
/* Get terminal */
|
||||||
|
guac_client* client = user->client;
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
guac_terminal* terminal = telnet_client->term;
|
||||||
|
|
||||||
|
/* Skip if terminal not yet ready */
|
||||||
|
if (terminal == NULL)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Resize terminal */
|
||||||
|
guac_terminal_resize(terminal, width, height);
|
||||||
|
|
||||||
|
/* Update terminal window size if connected */
|
||||||
|
if (telnet_client->telnet != NULL && telnet_client->naws_enabled)
|
||||||
|
guac_telnet_send_naws(telnet_client->telnet, terminal->term_width,
|
||||||
|
terminal->term_height);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
46
src/protocols/tn5250/input.h
Normal file
46
src/protocols/tn5250/input.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* 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_INPUT_H
|
||||||
|
#define GUAC_TELNET_INPUT_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for key events. Required by libguac and called whenever key events
|
||||||
|
* are received.
|
||||||
|
*/
|
||||||
|
guac_user_key_handler guac_telnet_user_key_handler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for mouse events. Required by libguac and called whenever mouse
|
||||||
|
* events are received.
|
||||||
|
*/
|
||||||
|
guac_user_mouse_handler guac_telnet_user_mouse_handler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for size events. Required by libguac and called whenever the remote
|
||||||
|
* display (window) is resized.
|
||||||
|
*/
|
||||||
|
guac_user_size_handler guac_telnet_user_size_handler;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
50
src/protocols/tn5250/pipe.c
Normal file
50
src/protocols/tn5250/pipe.c
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* 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 "pipe.h"
|
||||||
|
#include "telnet.h"
|
||||||
|
#include "terminal/terminal.h"
|
||||||
|
|
||||||
|
#include <guacamole/protocol.h>
|
||||||
|
#include <guacamole/socket.h>
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int guac_telnet_pipe_handler(guac_user* user, guac_stream* stream,
|
||||||
|
char* mimetype, char* name) {
|
||||||
|
|
||||||
|
guac_client* client = user->client;
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
|
||||||
|
/* Redirect STDIN if pipe has required name */
|
||||||
|
if (strcmp(name, GUAC_TELNET_STDIN_PIPE_NAME) == 0) {
|
||||||
|
guac_terminal_send_stream(telnet_client->term, user, stream);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* No other inbound pipe streams are supported */
|
||||||
|
guac_protocol_send_ack(user->socket, stream, "No such input stream.",
|
||||||
|
GUAC_PROTOCOL_STATUS_RESOURCE_NOT_FOUND);
|
||||||
|
guac_socket_flush(user->socket);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
42
src/protocols/tn5250/pipe.h
Normal file
42
src/protocols/tn5250/pipe.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* 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_PIPE_H
|
||||||
|
#define GUAC_TELNET_PIPE_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name reserved for the inbound pipe stream which forces the terminal
|
||||||
|
* emulator's STDIN to be received from the pipe.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_STDIN_PIPE_NAME "STDIN"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles an incoming stream from a Guacamole "pipe" instruction. If the pipe
|
||||||
|
* is named "STDIN", the the contents of the pipe stream are redirected to
|
||||||
|
* STDIN of the terminal emulator for as long as the pipe is open.
|
||||||
|
*/
|
||||||
|
guac_user_pipe_handler guac_telnet_pipe_handler;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
497
src/protocols/tn5250/settings.c
Normal file
497
src/protocols/tn5250/settings.c
Normal file
@ -0,0 +1,497 @@
|
|||||||
|
/*
|
||||||
|
* 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 "settings.h"
|
||||||
|
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <regex.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
/* Client plugin arguments */
|
||||||
|
const char* GUAC_TN5250_CLIENT_ARGS[] = {
|
||||||
|
"hostname",
|
||||||
|
"port",
|
||||||
|
"username",
|
||||||
|
"username-regex",
|
||||||
|
"password",
|
||||||
|
"password-regex",
|
||||||
|
"font-name",
|
||||||
|
"font-size",
|
||||||
|
"color-scheme",
|
||||||
|
"typescript-path",
|
||||||
|
"typescript-name",
|
||||||
|
"create-typescript-path",
|
||||||
|
"recording-path",
|
||||||
|
"recording-name",
|
||||||
|
"recording-exclude-output",
|
||||||
|
"recording-exclude-mouse",
|
||||||
|
"recording-include-keys",
|
||||||
|
"create-recording-path",
|
||||||
|
"read-only",
|
||||||
|
"backspace",
|
||||||
|
"terminal-type",
|
||||||
|
"scrollback",
|
||||||
|
"login-success-regex",
|
||||||
|
"login-failure-regex",
|
||||||
|
"disable-copy",
|
||||||
|
"disable-paste",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
enum TELNET_ARGS_IDX {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The hostname to connect to. Required.
|
||||||
|
*/
|
||||||
|
IDX_HOSTNAME,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The port to connect to. Optional.
|
||||||
|
*/
|
||||||
|
IDX_PORT,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the user to login as. Optional.
|
||||||
|
*/
|
||||||
|
IDX_USERNAME,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The regular expression to use when searching for the username/login
|
||||||
|
* prompt. Optional.
|
||||||
|
*/
|
||||||
|
IDX_USERNAME_REGEX,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The password to use when logging in. Optional.
|
||||||
|
*/
|
||||||
|
IDX_PASSWORD,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The regular expression to use when searching for the password prompt.
|
||||||
|
* Optional.
|
||||||
|
*/
|
||||||
|
IDX_PASSWORD_REGEX,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the font to use within the terminal.
|
||||||
|
*/
|
||||||
|
IDX_FONT_NAME,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The size of the font to use within the terminal, in points.
|
||||||
|
*/
|
||||||
|
IDX_FONT_SIZE,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The color scheme to use, as a series of semicolon-separated color-value
|
||||||
|
* pairs: "background: <color>", "foreground: <color>", or
|
||||||
|
* "color<n>: <color>", where <n> is a number from 0 to 255, and <color> is
|
||||||
|
* "color<n>" or an X11 color code (e.g. "aqua" or "rgb:12/34/56").
|
||||||
|
* The color scheme can also be one of the special values: "black-white",
|
||||||
|
* "white-black", "gray-black", or "green-black".
|
||||||
|
*/
|
||||||
|
IDX_COLOR_SCHEME,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The full absolute path to the directory in which typescripts should be
|
||||||
|
* written.
|
||||||
|
*/
|
||||||
|
IDX_TYPESCRIPT_PATH,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name that should be given to typescripts which are written in the
|
||||||
|
* given path. Each typescript will consist of two files: "NAME" and
|
||||||
|
* "NAME.timing".
|
||||||
|
*/
|
||||||
|
IDX_TYPESCRIPT_NAME,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the specified typescript path should automatically be created
|
||||||
|
* if it does not yet exist.
|
||||||
|
*/
|
||||||
|
IDX_CREATE_TYPESCRIPT_PATH,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The full absolute path to the directory in which screen recordings
|
||||||
|
* should be written.
|
||||||
|
*/
|
||||||
|
IDX_RECORDING_PATH,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name that should be given to screen recordings which are written in
|
||||||
|
* the given path.
|
||||||
|
*/
|
||||||
|
IDX_RECORDING_NAME,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether output which is broadcast to each connected client (graphics,
|
||||||
|
* streams, etc.) should NOT be included in the session recording. Output
|
||||||
|
* is included by default, as it is necessary for any recording which must
|
||||||
|
* later be viewable as video.
|
||||||
|
*/
|
||||||
|
IDX_RECORDING_EXCLUDE_OUTPUT,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether changes to mouse state, such as position and buttons pressed or
|
||||||
|
* released, should NOT be included in the session recording. Mouse state
|
||||||
|
* is included by default, as it is necessary for the mouse cursor to be
|
||||||
|
* rendered in any resulting video.
|
||||||
|
*/
|
||||||
|
IDX_RECORDING_EXCLUDE_MOUSE,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether keys pressed and released should be included in the session
|
||||||
|
* recording. Key events are NOT included by default within the recording,
|
||||||
|
* as doing so has privacy and security implications. Including key events
|
||||||
|
* may be necessary in certain auditing contexts, but should only be done
|
||||||
|
* with caution. Key events can easily contain sensitive information, such
|
||||||
|
* as passwords, credit card numbers, etc.
|
||||||
|
*/
|
||||||
|
IDX_RECORDING_INCLUDE_KEYS,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the specified screen recording path should automatically be
|
||||||
|
* created if it does not yet exist.
|
||||||
|
*/
|
||||||
|
IDX_CREATE_RECORDING_PATH,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* "true" if this connection should be read-only (user input should be
|
||||||
|
* dropped), "false" or blank otherwise.
|
||||||
|
*/
|
||||||
|
IDX_READ_ONLY,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ASCII code, as an integer to use for the backspace key, or 127
|
||||||
|
* if not specified.
|
||||||
|
*/
|
||||||
|
IDX_BACKSPACE,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The terminal emulator type that is passed to the remote system (e.g.
|
||||||
|
* "xterm" or "xterm-256color"). "linux" is used if unspecified.
|
||||||
|
*/
|
||||||
|
IDX_TERMINAL_TYPE,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum size of the scrollback buffer in rows.
|
||||||
|
*/
|
||||||
|
IDX_SCROLLBACK,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The regular expression to use when searching for whether login was
|
||||||
|
* successful. This parameter is optional. If given, the
|
||||||
|
* "login-failure-regex" parameter must also be specified, and the first
|
||||||
|
* frame of the Guacamole connection will be withheld until login
|
||||||
|
* success/failure has been determined.
|
||||||
|
*/
|
||||||
|
IDX_LOGIN_SUCCESS_REGEX,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The regular expression to use when searching for whether login was
|
||||||
|
* unsuccessful. This parameter is optional. If given, the
|
||||||
|
* "login-success-regex" parameter must also be specified, and the first
|
||||||
|
* frame of the Guacamole connection will be withheld until login
|
||||||
|
* success/failure has been determined.
|
||||||
|
*/
|
||||||
|
IDX_LOGIN_FAILURE_REGEX,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether outbound clipboard access should be blocked. If set to "true",
|
||||||
|
* it will not be possible to copy data from the terminal to the client
|
||||||
|
* using the clipboard. By default, clipboard access is not blocked.
|
||||||
|
*/
|
||||||
|
IDX_DISABLE_COPY,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether inbound clipboard access should be blocked. If set to "true", it
|
||||||
|
* will not be possible to paste data from the client to the terminal using
|
||||||
|
* the clipboard. By default, clipboard access is not blocked.
|
||||||
|
*/
|
||||||
|
IDX_DISABLE_PASTE,
|
||||||
|
|
||||||
|
TELNET_ARGS_COUNT
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compiles the given regular expression, returning NULL if compilation fails
|
||||||
|
* or of the given regular expression is NULL. The returned regex_t must be
|
||||||
|
* freed with regfree() AND free(), or with guac_telnet_regex_free().
|
||||||
|
*
|
||||||
|
* @param user
|
||||||
|
* The user who provided the setting associated with the given regex
|
||||||
|
* pattern. Error messages will be logged on behalf of this user.
|
||||||
|
*
|
||||||
|
* @param pattern
|
||||||
|
* The regular expression pattern to compile.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* The compiled regular expression, or NULL if compilation fails or NULL
|
||||||
|
* was originally provided for the pattern.
|
||||||
|
*/
|
||||||
|
static regex_t* guac_telnet_compile_regex(guac_user* user, char* pattern) {
|
||||||
|
|
||||||
|
/* Nothing to compile if no pattern provided */
|
||||||
|
if (pattern == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
int compile_result;
|
||||||
|
regex_t* regex = malloc(sizeof(regex_t));
|
||||||
|
|
||||||
|
/* Compile regular expression */
|
||||||
|
compile_result = regcomp(regex, pattern,
|
||||||
|
REG_EXTENDED | REG_NOSUB | REG_ICASE | REG_NEWLINE);
|
||||||
|
|
||||||
|
/* Notify of failure to parse/compile */
|
||||||
|
if (compile_result != 0) {
|
||||||
|
guac_user_log(user, GUAC_LOG_ERROR, "Regular expression '%s' "
|
||||||
|
"could not be compiled.", pattern);
|
||||||
|
free(regex);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return regex;
|
||||||
|
}
|
||||||
|
|
||||||
|
void guac_telnet_regex_free(regex_t** regex) {
|
||||||
|
if (*regex != NULL) {
|
||||||
|
regfree(*regex);
|
||||||
|
free(*regex);
|
||||||
|
*regex = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
guac_telnet_settings* guac_telnet_parse_args(guac_user* user,
|
||||||
|
int argc, const char** argv) {
|
||||||
|
|
||||||
|
/* Validate arg count */
|
||||||
|
if (argc != TELNET_ARGS_COUNT) {
|
||||||
|
guac_user_log(user, GUAC_LOG_WARNING, "Incorrect number of connection "
|
||||||
|
"parameters provided: expected %i, got %i.",
|
||||||
|
TELNET_ARGS_COUNT, argc);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
guac_telnet_settings* settings = calloc(1, sizeof(guac_telnet_settings));
|
||||||
|
|
||||||
|
/* Read parameters */
|
||||||
|
settings->hostname =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_HOSTNAME, "");
|
||||||
|
|
||||||
|
/* Read username */
|
||||||
|
settings->username =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_USERNAME, NULL);
|
||||||
|
|
||||||
|
/* Read username regex only if username is specified */
|
||||||
|
if (settings->username != NULL) {
|
||||||
|
settings->username_regex = guac_telnet_compile_regex(user,
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_USERNAME_REGEX, GUAC_TELNET_DEFAULT_USERNAME_REGEX));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read password */
|
||||||
|
settings->password =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_PASSWORD, NULL);
|
||||||
|
|
||||||
|
/* Read password regex only if password is specified */
|
||||||
|
if (settings->password != NULL) {
|
||||||
|
settings->password_regex = guac_telnet_compile_regex(user,
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_PASSWORD_REGEX, GUAC_TELNET_DEFAULT_PASSWORD_REGEX));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read optional login success detection regex */
|
||||||
|
settings->login_success_regex = guac_telnet_compile_regex(user,
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_LOGIN_SUCCESS_REGEX, NULL));
|
||||||
|
|
||||||
|
/* Read optional login failure detection regex */
|
||||||
|
settings->login_failure_regex = guac_telnet_compile_regex(user,
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_LOGIN_FAILURE_REGEX, NULL));
|
||||||
|
|
||||||
|
/* Both login success and login failure regexes must be provided if either
|
||||||
|
* is present at all */
|
||||||
|
if (settings->login_success_regex != NULL
|
||||||
|
&& settings->login_failure_regex == NULL) {
|
||||||
|
guac_telnet_regex_free(&settings->login_success_regex);
|
||||||
|
guac_user_log(user, GUAC_LOG_WARNING, "Ignoring provided value for "
|
||||||
|
"\"%s\" as \"%s\" must also be provided.",
|
||||||
|
GUAC_TELNET_CLIENT_ARGS[IDX_LOGIN_SUCCESS_REGEX],
|
||||||
|
GUAC_TELNET_CLIENT_ARGS[IDX_LOGIN_FAILURE_REGEX]);
|
||||||
|
}
|
||||||
|
else if (settings->login_failure_regex != NULL
|
||||||
|
&& settings->login_success_regex == NULL) {
|
||||||
|
guac_telnet_regex_free(&settings->login_failure_regex);
|
||||||
|
guac_user_log(user, GUAC_LOG_WARNING, "Ignoring provided value for "
|
||||||
|
"\"%s\" as \"%s\" must also be provided.",
|
||||||
|
GUAC_TELNET_CLIENT_ARGS[IDX_LOGIN_FAILURE_REGEX],
|
||||||
|
GUAC_TELNET_CLIENT_ARGS[IDX_LOGIN_SUCCESS_REGEX]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read-only mode */
|
||||||
|
settings->read_only =
|
||||||
|
guac_user_parse_args_boolean(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_READ_ONLY, false);
|
||||||
|
|
||||||
|
/* Read maximum scrollback size */
|
||||||
|
settings->max_scrollback =
|
||||||
|
guac_user_parse_args_int(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_SCROLLBACK, GUAC_TELNET_DEFAULT_MAX_SCROLLBACK);
|
||||||
|
|
||||||
|
/* Read font name */
|
||||||
|
settings->font_name =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_FONT_NAME, GUAC_TELNET_DEFAULT_FONT_NAME);
|
||||||
|
|
||||||
|
/* Read font size */
|
||||||
|
settings->font_size =
|
||||||
|
guac_user_parse_args_int(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_FONT_SIZE, GUAC_TELNET_DEFAULT_FONT_SIZE);
|
||||||
|
|
||||||
|
/* Copy requested color scheme */
|
||||||
|
settings->color_scheme =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_COLOR_SCHEME, "");
|
||||||
|
|
||||||
|
/* Pull width/height/resolution directly from user */
|
||||||
|
settings->width = user->info.optimal_width;
|
||||||
|
settings->height = user->info.optimal_height;
|
||||||
|
settings->resolution = user->info.optimal_resolution;
|
||||||
|
|
||||||
|
/* Read port */
|
||||||
|
settings->port =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_PORT, GUAC_TELNET_DEFAULT_PORT);
|
||||||
|
|
||||||
|
/* Read typescript path */
|
||||||
|
settings->typescript_path =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_TYPESCRIPT_PATH, NULL);
|
||||||
|
|
||||||
|
/* Read typescript name */
|
||||||
|
settings->typescript_name =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_TYPESCRIPT_NAME, GUAC_TELNET_DEFAULT_TYPESCRIPT_NAME);
|
||||||
|
|
||||||
|
/* Parse path creation flag */
|
||||||
|
settings->create_typescript_path =
|
||||||
|
guac_user_parse_args_boolean(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_CREATE_TYPESCRIPT_PATH, false);
|
||||||
|
|
||||||
|
/* Read recording path */
|
||||||
|
settings->recording_path =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_RECORDING_PATH, NULL);
|
||||||
|
|
||||||
|
/* Read recording name */
|
||||||
|
settings->recording_name =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_RECORDING_NAME, GUAC_TELNET_DEFAULT_RECORDING_NAME);
|
||||||
|
|
||||||
|
/* Parse output exclusion flag */
|
||||||
|
settings->recording_exclude_output =
|
||||||
|
guac_user_parse_args_boolean(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_RECORDING_EXCLUDE_OUTPUT, false);
|
||||||
|
|
||||||
|
/* Parse mouse exclusion flag */
|
||||||
|
settings->recording_exclude_mouse =
|
||||||
|
guac_user_parse_args_boolean(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_RECORDING_EXCLUDE_MOUSE, false);
|
||||||
|
|
||||||
|
/* Parse key event inclusion flag */
|
||||||
|
settings->recording_include_keys =
|
||||||
|
guac_user_parse_args_boolean(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_RECORDING_INCLUDE_KEYS, false);
|
||||||
|
|
||||||
|
/* Parse path creation flag */
|
||||||
|
settings->create_recording_path =
|
||||||
|
guac_user_parse_args_boolean(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_CREATE_RECORDING_PATH, false);
|
||||||
|
|
||||||
|
/* Parse backspace key code */
|
||||||
|
settings->backspace =
|
||||||
|
guac_user_parse_args_int(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_BACKSPACE, 127);
|
||||||
|
|
||||||
|
/* Read terminal emulator type. */
|
||||||
|
settings->terminal_type =
|
||||||
|
guac_user_parse_args_string(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_TERMINAL_TYPE, "linux");
|
||||||
|
|
||||||
|
/* Parse clipboard copy disable flag */
|
||||||
|
settings->disable_copy =
|
||||||
|
guac_user_parse_args_boolean(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_DISABLE_COPY, false);
|
||||||
|
|
||||||
|
/* Parse clipboard paste disable flag */
|
||||||
|
settings->disable_paste =
|
||||||
|
guac_user_parse_args_boolean(user, GUAC_TELNET_CLIENT_ARGS, argv,
|
||||||
|
IDX_DISABLE_PASTE, false);
|
||||||
|
|
||||||
|
/* Parsing was successful */
|
||||||
|
return settings;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void guac_telnet_settings_free(guac_telnet_settings* settings) {
|
||||||
|
|
||||||
|
/* Free network connection information */
|
||||||
|
free(settings->hostname);
|
||||||
|
free(settings->port);
|
||||||
|
|
||||||
|
/* Free credentials */
|
||||||
|
free(settings->username);
|
||||||
|
free(settings->password);
|
||||||
|
|
||||||
|
/* Free various regexes */
|
||||||
|
guac_telnet_regex_free(&settings->username_regex);
|
||||||
|
guac_telnet_regex_free(&settings->password_regex);
|
||||||
|
guac_telnet_regex_free(&settings->login_success_regex);
|
||||||
|
guac_telnet_regex_free(&settings->login_failure_regex);
|
||||||
|
|
||||||
|
/* Free display preferences */
|
||||||
|
free(settings->font_name);
|
||||||
|
free(settings->color_scheme);
|
||||||
|
|
||||||
|
/* Free typescript settings */
|
||||||
|
free(settings->typescript_name);
|
||||||
|
free(settings->typescript_path);
|
||||||
|
|
||||||
|
/* Free screen recording settings */
|
||||||
|
free(settings->recording_name);
|
||||||
|
free(settings->recording_path);
|
||||||
|
|
||||||
|
/* Free terminal emulator type. */
|
||||||
|
free(settings->terminal_type);
|
||||||
|
|
||||||
|
/* Free overall structure */
|
||||||
|
free(settings);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
309
src/protocols/tn5250/settings.h
Normal file
309
src/protocols/tn5250/settings.h
Normal file
@ -0,0 +1,309 @@
|
|||||||
|
/*
|
||||||
|
* 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_TN5250_SETTINGS_H
|
||||||
|
#define GUAC_TN5250_SETTINGS_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <regex.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the font to use for the terminal if no name is specified.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_DEFAULT_FONT_NAME "monospace"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The size of the font to use for the terminal if no font size is specified,
|
||||||
|
* in points.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_DEFAULT_FONT_SIZE 12
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The port to connect to when initiating any telnet connection, if no other
|
||||||
|
* port is specified.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_DEFAULT_PORT "23"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The filename to use for the typescript, if not specified.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_DEFAULT_TYPESCRIPT_NAME "typescript"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The filename to use for the screen recording, if not specified.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_DEFAULT_RECORDING_NAME "recording"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The regular expression to use when searching for the username/login prompt
|
||||||
|
* if no other regular expression is specified.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_DEFAULT_USERNAME_REGEX "[Ll]ogin:"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The regular expression to use when searching for the password prompt if no
|
||||||
|
* other regular expression is specified.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_DEFAULT_PASSWORD_REGEX "[Pp]assword:"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default maximum scrollback size in rows.
|
||||||
|
*/
|
||||||
|
#define GUAC_TELNET_DEFAULT_MAX_SCROLLBACK 1000
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Settings for the telnet connection. The values for this structure are parsed
|
||||||
|
* from the arguments given during the Guacamole protocol handshake using the
|
||||||
|
* guac_telnet_parse_args() function.
|
||||||
|
*/
|
||||||
|
typedef struct guac_telnet_settings {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The hostname of the telnet server to connect to.
|
||||||
|
*/
|
||||||
|
char* hostname;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The port of the telnet server to connect to.
|
||||||
|
*/
|
||||||
|
char* port;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the user to login as, if any. If no username is specified,
|
||||||
|
* this will be NULL.
|
||||||
|
*/
|
||||||
|
char* username;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The regular expression to use when searching for the username/login
|
||||||
|
* prompt. If no username is specified, this will be NULL. If a username
|
||||||
|
* is specified, this will either be the specified username regex, or the
|
||||||
|
* default username regex.
|
||||||
|
*/
|
||||||
|
regex_t* username_regex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The password to give when authenticating, if any. If no password is
|
||||||
|
* specified, this will be NULL.
|
||||||
|
*/
|
||||||
|
char* password;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The regular expression to use when searching for the password prompt. If
|
||||||
|
* no password is specified, this will be NULL. If a password is specified,
|
||||||
|
* this will either be the specified password regex, or the default
|
||||||
|
* password regex.
|
||||||
|
*/
|
||||||
|
regex_t* password_regex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The regular expression to use when searching for whether login was
|
||||||
|
* successful. If no such regex is specified, or if no login failure regex
|
||||||
|
* was specified, this will be NULL.
|
||||||
|
*/
|
||||||
|
regex_t* login_success_regex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The regular expression to use when searching for whether login failed.
|
||||||
|
* If no such regex is specified, or if no login success regex was
|
||||||
|
* specified, this will be NULL.
|
||||||
|
*/
|
||||||
|
regex_t* login_failure_regex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether this connection is read-only, and user input should be dropped.
|
||||||
|
*/
|
||||||
|
bool read_only;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum size of the scrollback buffer in rows.
|
||||||
|
*/
|
||||||
|
int max_scrollback;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the font to use for display rendering.
|
||||||
|
*/
|
||||||
|
char* font_name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The size of the font to use, in points.
|
||||||
|
*/
|
||||||
|
int font_size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the color scheme to use.
|
||||||
|
*/
|
||||||
|
char* color_scheme;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The desired width of the terminal display, in pixels.
|
||||||
|
*/
|
||||||
|
int width;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The desired height of the terminal display, in pixels.
|
||||||
|
*/
|
||||||
|
int height;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The desired screen resolution, in DPI.
|
||||||
|
*/
|
||||||
|
int resolution;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether outbound clipboard access should be blocked. If set, it will not
|
||||||
|
* be possible to copy data from the terminal to the client using the
|
||||||
|
* clipboard.
|
||||||
|
*/
|
||||||
|
bool disable_copy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether inbound clipboard access should be blocked. If set, it will not
|
||||||
|
* be possible to paste data from the client to the terminal using the
|
||||||
|
* clipboard.
|
||||||
|
*/
|
||||||
|
bool disable_paste;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The path in which the typescript should be saved, if enabled. If no
|
||||||
|
* typescript should be saved, this will be NULL.
|
||||||
|
*/
|
||||||
|
char* typescript_path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The filename to use for the typescript, if enabled.
|
||||||
|
*/
|
||||||
|
char* typescript_name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the typescript path should be automatically created if it does
|
||||||
|
* not already exist.
|
||||||
|
*/
|
||||||
|
bool create_typescript_path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The path in which the screen recording should be saved, if enabled. If
|
||||||
|
* no screen recording should be saved, this will be NULL.
|
||||||
|
*/
|
||||||
|
char* recording_path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The filename to use for the screen recording, if enabled.
|
||||||
|
*/
|
||||||
|
char* recording_name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the screen recording path should be automatically created if it
|
||||||
|
* does not already exist.
|
||||||
|
*/
|
||||||
|
bool create_recording_path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether output which is broadcast to each connected client (graphics,
|
||||||
|
* streams, etc.) should NOT be included in the session recording. Output
|
||||||
|
* is included by default, as it is necessary for any recording which must
|
||||||
|
* later be viewable as video.
|
||||||
|
*/
|
||||||
|
bool recording_exclude_output;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether changes to mouse state, such as position and buttons pressed or
|
||||||
|
* released, should NOT be included in the session recording. Mouse state
|
||||||
|
* is included by default, as it is necessary for the mouse cursor to be
|
||||||
|
* rendered in any resulting video.
|
||||||
|
*/
|
||||||
|
bool recording_exclude_mouse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether keys pressed and released should be included in the session
|
||||||
|
* recording. Key events are NOT included by default within the recording,
|
||||||
|
* as doing so has privacy and security implications. Including key events
|
||||||
|
* may be necessary in certain auditing contexts, but should only be done
|
||||||
|
* with caution. Key events can easily contain sensitive information, such
|
||||||
|
* as passwords, credit card numbers, etc.
|
||||||
|
*/
|
||||||
|
bool recording_include_keys;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ASCII code, as an integer, that the telnet client will use when the
|
||||||
|
* backspace key is pressed. By default, this is 127, ASCII delete, if
|
||||||
|
* not specified in the client settings.
|
||||||
|
*/
|
||||||
|
int backspace;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The terminal emulator type that is passed to the remote system.
|
||||||
|
*/
|
||||||
|
char* terminal_type;
|
||||||
|
|
||||||
|
} guac_telnet_settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses all given args, storing them in a newly-allocated settings object. If
|
||||||
|
* the args fail to parse, NULL is returned.
|
||||||
|
*
|
||||||
|
* @param user
|
||||||
|
* The user who submitted the given arguments while joining the
|
||||||
|
* connection.
|
||||||
|
*
|
||||||
|
* @param argc
|
||||||
|
* The number of arguments within the argv array.
|
||||||
|
*
|
||||||
|
* @param argv
|
||||||
|
* The values of all arguments provided by the user.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* A newly-allocated settings object which must be freed with
|
||||||
|
* guac_telnet_settings_free() when no longer needed. If the arguments fail
|
||||||
|
* to parse, NULL is returned.
|
||||||
|
*/
|
||||||
|
guac_telnet_settings* guac_telnet_parse_args(guac_user* user,
|
||||||
|
int argc, const char** argv);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frees the regex pointed to by the given pointer, assigning the value NULL to
|
||||||
|
* that pointer once the regex is freed. If the pointer already contains NULL,
|
||||||
|
* this function has no effect.
|
||||||
|
*
|
||||||
|
* @param regex
|
||||||
|
* The address of the pointer to the regex that should be freed.
|
||||||
|
*/
|
||||||
|
void guac_telnet_regex_free(regex_t** regex);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frees the given guac_telnet_settings object, having been previously
|
||||||
|
* allocated via guac_telnet_parse_args().
|
||||||
|
*
|
||||||
|
* @param settings
|
||||||
|
* The settings object to free.
|
||||||
|
*/
|
||||||
|
void guac_telnet_settings_free(guac_telnet_settings* settings);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NULL-terminated array of accepted client args.
|
||||||
|
*/
|
||||||
|
extern const char* GUAC_TELNET_CLIENT_ARGS[];
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
636
src/protocols/tn5250/tn5250.c
Normal file
636
src/protocols/tn5250/tn5250.c
Normal file
@ -0,0 +1,636 @@
|
|||||||
|
/*
|
||||||
|
* 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 "common/recording.h"
|
||||||
|
#include "tn5250.h"
|
||||||
|
#include "terminal/terminal.h"
|
||||||
|
|
||||||
|
#include <guacamole/client.h>
|
||||||
|
#include <guacamole/protocol.h>
|
||||||
|
#include <libtelnet.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Support levels for various telnet options, required for connection
|
||||||
|
* negotiation by telnet_init(), part of libtelnet.
|
||||||
|
*/
|
||||||
|
static const telnet_telopt_t __telnet_options[] = {
|
||||||
|
{ TELNET_TELOPT_ECHO, TELNET_WONT, TELNET_DO },
|
||||||
|
{ TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT },
|
||||||
|
{ TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO },
|
||||||
|
{ TELNET_TELOPT_MSSP, TELNET_WONT, TELNET_DO },
|
||||||
|
{ TELNET_TELOPT_NAWS, TELNET_WILL, TELNET_DONT },
|
||||||
|
{ TELNET_TELOPT_NEW_ENVIRON, TELNET_WILL, TELNET_DONT },
|
||||||
|
{ -1, 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write the entire buffer given to the specified file descriptor, retrying
|
||||||
|
* the write automatically if necessary. This function will return a value
|
||||||
|
* not equal to the buffer's size iff an error occurs which prevents all
|
||||||
|
* future writes.
|
||||||
|
*
|
||||||
|
* @param fd The file descriptor to write to.
|
||||||
|
* @param buffer The buffer to write.
|
||||||
|
* @param size The number of bytes from the buffer to write.
|
||||||
|
*/
|
||||||
|
static int __guac_telnet_write_all(int fd, const char* buffer, int size) {
|
||||||
|
|
||||||
|
int remaining = size;
|
||||||
|
while (remaining > 0) {
|
||||||
|
|
||||||
|
/* Attempt to write data */
|
||||||
|
int ret_val = write(fd, buffer, remaining);
|
||||||
|
if (ret_val <= 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
/* If successful, contine with what data remains (if any) */
|
||||||
|
remaining -= ret_val;
|
||||||
|
buffer += ret_val;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return size;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Matches the given line against the given regex, returning true and sending
|
||||||
|
* the given value if a match is found. An enter keypress is automatically
|
||||||
|
* sent after the value is sent.
|
||||||
|
*
|
||||||
|
* @param client
|
||||||
|
* The guac_client associated with the telnet session.
|
||||||
|
*
|
||||||
|
* @param regex
|
||||||
|
* The regex to search for within the given line buffer.
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* The string value to send through STDIN of the telnet session if a
|
||||||
|
* match is found, or NULL if no value should be sent.
|
||||||
|
*
|
||||||
|
* @param line_buffer
|
||||||
|
* The line of character data to test.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* true if a match is found, false otherwise.
|
||||||
|
*/
|
||||||
|
static bool guac_telnet_regex_exec(guac_client* client, regex_t* regex,
|
||||||
|
const char* value, const char* line_buffer) {
|
||||||
|
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
|
||||||
|
/* Send value upon match */
|
||||||
|
if (regexec(regex, line_buffer, 0, NULL, 0) == 0) {
|
||||||
|
|
||||||
|
/* Send value */
|
||||||
|
if (value != NULL) {
|
||||||
|
guac_terminal_send_string(telnet_client->term, value);
|
||||||
|
guac_terminal_send_string(telnet_client->term, "\x0D");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Stop searching for prompt */
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Matches the given line against the various stored regexes, automatically
|
||||||
|
* sending the configured username, password, or reporting login
|
||||||
|
* success/failure depending on context. If no search is in progress, either
|
||||||
|
* because no regexes have been defined or because all applicable searches have
|
||||||
|
* completed, this function has no effect.
|
||||||
|
*
|
||||||
|
* @param client
|
||||||
|
* The guac_client associated with the telnet session.
|
||||||
|
*
|
||||||
|
* @param line_buffer
|
||||||
|
* The line of character data to test.
|
||||||
|
*/
|
||||||
|
static void guac_telnet_search_line(guac_client* client, const char* line_buffer) {
|
||||||
|
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
guac_telnet_settings* settings = telnet_client->settings;
|
||||||
|
|
||||||
|
/* Continue search for username prompt */
|
||||||
|
if (settings->username_regex != NULL) {
|
||||||
|
if (guac_telnet_regex_exec(client, settings->username_regex,
|
||||||
|
settings->username, line_buffer)) {
|
||||||
|
guac_client_log(client, GUAC_LOG_DEBUG, "Username sent");
|
||||||
|
guac_telnet_regex_free(&settings->username_regex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Continue search for password prompt */
|
||||||
|
if (settings->password_regex != NULL) {
|
||||||
|
if (guac_telnet_regex_exec(client, settings->password_regex,
|
||||||
|
settings->password, line_buffer)) {
|
||||||
|
|
||||||
|
guac_client_log(client, GUAC_LOG_DEBUG, "Password sent");
|
||||||
|
|
||||||
|
/* Do not continue searching for username/password once password is sent */
|
||||||
|
guac_telnet_regex_free(&settings->username_regex);
|
||||||
|
guac_telnet_regex_free(&settings->password_regex);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Continue search for login success */
|
||||||
|
if (settings->login_success_regex != NULL) {
|
||||||
|
if (guac_telnet_regex_exec(client, settings->login_success_regex,
|
||||||
|
NULL, line_buffer)) {
|
||||||
|
|
||||||
|
/* Allow terminal to render now that login has been deemed successful */
|
||||||
|
guac_client_log(client, GUAC_LOG_DEBUG, "Login successful");
|
||||||
|
guac_terminal_start(telnet_client->term);
|
||||||
|
|
||||||
|
/* Stop all searches */
|
||||||
|
guac_telnet_regex_free(&settings->username_regex);
|
||||||
|
guac_telnet_regex_free(&settings->password_regex);
|
||||||
|
guac_telnet_regex_free(&settings->login_success_regex);
|
||||||
|
guac_telnet_regex_free(&settings->login_failure_regex);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Continue search for login failure */
|
||||||
|
if (settings->login_failure_regex != NULL) {
|
||||||
|
if (guac_telnet_regex_exec(client, settings->login_failure_regex,
|
||||||
|
NULL, line_buffer)) {
|
||||||
|
|
||||||
|
/* Advise that login has failed and connection should be closed */
|
||||||
|
guac_client_abort(client,
|
||||||
|
GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
|
||||||
|
"Login failed");
|
||||||
|
|
||||||
|
/* Stop all searches */
|
||||||
|
guac_telnet_regex_free(&settings->username_regex);
|
||||||
|
guac_telnet_regex_free(&settings->password_regex);
|
||||||
|
guac_telnet_regex_free(&settings->login_success_regex);
|
||||||
|
guac_telnet_regex_free(&settings->login_failure_regex);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches for a line matching the various stored regexes, automatically
|
||||||
|
* sending the configured username, password, or reporting login
|
||||||
|
* success/failure depending on context. If no search is in progress, either
|
||||||
|
* because no regexes have been defined or because all applicable searches
|
||||||
|
* have completed, this function has no effect.
|
||||||
|
*
|
||||||
|
* @param client
|
||||||
|
* The guac_client associated with the telnet session.
|
||||||
|
*
|
||||||
|
* @param buffer
|
||||||
|
* The buffer of received data to search through.
|
||||||
|
*
|
||||||
|
* @param size
|
||||||
|
* The size of the given buffer, in bytes.
|
||||||
|
*/
|
||||||
|
static void guac_telnet_search(guac_client* client, const char* buffer, int size) {
|
||||||
|
|
||||||
|
static char line_buffer[1024] = {0};
|
||||||
|
static int length = 0;
|
||||||
|
|
||||||
|
/* Append all characters in buffer to current line */
|
||||||
|
const char* current = buffer;
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
|
||||||
|
char c = *(current++);
|
||||||
|
|
||||||
|
/* Attempt pattern match and clear buffer upon reading newline */
|
||||||
|
if (c == '\n') {
|
||||||
|
if (length > 0) {
|
||||||
|
line_buffer[length] = '\0';
|
||||||
|
guac_telnet_search_line(client, line_buffer);
|
||||||
|
length = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Append all non-newline characters to line buffer as long as space
|
||||||
|
* remains */
|
||||||
|
else if (length < sizeof(line_buffer) - 1)
|
||||||
|
line_buffer[length++] = c;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Attempt pattern match if an unfinished line remains (may be a prompt) */
|
||||||
|
if (length > 0) {
|
||||||
|
line_buffer[length] = '\0';
|
||||||
|
guac_telnet_search_line(client, line_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event handler, as defined by libtelnet. This function is passed to
|
||||||
|
* telnet_init() and will be called for every event fired by libtelnet,
|
||||||
|
* including feature enable/disable and receipt/transmission of data.
|
||||||
|
*/
|
||||||
|
static void __guac_telnet_event_handler(telnet_t* telnet, telnet_event_t* event, void* data) {
|
||||||
|
|
||||||
|
guac_client* client = (guac_client*) data;
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
guac_telnet_settings* settings = telnet_client->settings;
|
||||||
|
|
||||||
|
switch (event->type) {
|
||||||
|
|
||||||
|
/* Terminal output received */
|
||||||
|
case TELNET_EV_DATA:
|
||||||
|
guac_terminal_write(telnet_client->term, event->data.buffer, event->data.size);
|
||||||
|
guac_telnet_search(client, event->data.buffer, event->data.size);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Data destined for remote end */
|
||||||
|
case TELNET_EV_SEND:
|
||||||
|
if (__guac_telnet_write_all(telnet_client->socket_fd, event->data.buffer, event->data.size)
|
||||||
|
!= event->data.size)
|
||||||
|
guac_client_stop(client);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Remote feature enabled */
|
||||||
|
case TELNET_EV_WILL:
|
||||||
|
if (event->neg.telopt == TELNET_TELOPT_ECHO)
|
||||||
|
telnet_client->echo_enabled = 0; /* Disable local echo, as remote will echo */
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Remote feature disabled */
|
||||||
|
case TELNET_EV_WONT:
|
||||||
|
if (event->neg.telopt == TELNET_TELOPT_ECHO)
|
||||||
|
telnet_client->echo_enabled = 1; /* Enable local echo, as remote won't echo */
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Local feature enable */
|
||||||
|
case TELNET_EV_DO:
|
||||||
|
if (event->neg.telopt == TELNET_TELOPT_NAWS) {
|
||||||
|
telnet_client->naws_enabled = 1;
|
||||||
|
guac_telnet_send_naws(telnet, telnet_client->term->term_width, telnet_client->term->term_height);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Terminal type request */
|
||||||
|
case TELNET_EV_TTYPE:
|
||||||
|
if (event->ttype.cmd == TELNET_TTYPE_SEND)
|
||||||
|
telnet_ttype_is(telnet_client->telnet, settings->terminal_type);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Environment request */
|
||||||
|
case TELNET_EV_ENVIRON:
|
||||||
|
|
||||||
|
/* Only send USER if entire environment was requested */
|
||||||
|
if (event->environ.size == 0)
|
||||||
|
guac_telnet_send_user(telnet, settings->username);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Connection warnings */
|
||||||
|
case TELNET_EV_WARNING:
|
||||||
|
guac_client_log(client, GUAC_LOG_WARNING, "%s", event->error.msg);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Connection errors */
|
||||||
|
case TELNET_EV_ERROR:
|
||||||
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR,
|
||||||
|
"Telnet connection closing with error: %s", event->error.msg);
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Ignore other events */
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Input thread, started by the main telnet client thread. This thread
|
||||||
|
* continuously reads from the terminal's STDIN and transfers all read
|
||||||
|
* data to the telnet connection.
|
||||||
|
*
|
||||||
|
* @param data The current guac_client instance.
|
||||||
|
* @return Always NULL.
|
||||||
|
*/
|
||||||
|
static void* __guac_telnet_input_thread(void* data) {
|
||||||
|
|
||||||
|
guac_client* client = (guac_client*) data;
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
|
||||||
|
char buffer[8192];
|
||||||
|
int bytes_read;
|
||||||
|
|
||||||
|
/* Write all data read */
|
||||||
|
while ((bytes_read = guac_terminal_read_stdin(telnet_client->term, buffer, sizeof(buffer))) > 0) {
|
||||||
|
telnet_send(telnet_client->telnet, buffer, bytes_read);
|
||||||
|
if (telnet_client->echo_enabled)
|
||||||
|
guac_terminal_write(telnet_client->term, buffer, bytes_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connects to the telnet server specified within the data associated
|
||||||
|
* with the given guac_client, which will have been populated by
|
||||||
|
* guac_client_init.
|
||||||
|
*
|
||||||
|
* @return The connected telnet instance, if successful, or NULL if the
|
||||||
|
* connection fails for any reason.
|
||||||
|
*/
|
||||||
|
static telnet_t* __guac_telnet_create_session(guac_client* client) {
|
||||||
|
|
||||||
|
int retval;
|
||||||
|
|
||||||
|
int fd;
|
||||||
|
struct addrinfo* addresses;
|
||||||
|
struct addrinfo* current_address;
|
||||||
|
|
||||||
|
char connected_address[1024];
|
||||||
|
char connected_port[64];
|
||||||
|
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
guac_telnet_settings* settings = telnet_client->settings;
|
||||||
|
|
||||||
|
struct addrinfo hints = {
|
||||||
|
.ai_family = AF_UNSPEC,
|
||||||
|
.ai_socktype = SOCK_STREAM,
|
||||||
|
.ai_protocol = IPPROTO_TCP
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Get socket */
|
||||||
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
|
/* Get addresses connection */
|
||||||
|
if ((retval = getaddrinfo(settings->hostname, settings->port,
|
||||||
|
&hints, &addresses))) {
|
||||||
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Error parsing given address or port: %s",
|
||||||
|
gai_strerror(retval));
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Attempt connection to each address until success */
|
||||||
|
current_address = addresses;
|
||||||
|
while (current_address != NULL) {
|
||||||
|
|
||||||
|
int retval;
|
||||||
|
|
||||||
|
/* Resolve hostname */
|
||||||
|
if ((retval = getnameinfo(current_address->ai_addr,
|
||||||
|
current_address->ai_addrlen,
|
||||||
|
connected_address, sizeof(connected_address),
|
||||||
|
connected_port, sizeof(connected_port),
|
||||||
|
NI_NUMERICHOST | NI_NUMERICSERV)))
|
||||||
|
guac_client_log(client, GUAC_LOG_DEBUG, "Unable to resolve host: %s", gai_strerror(retval));
|
||||||
|
|
||||||
|
/* Connect */
|
||||||
|
if (connect(fd, current_address->ai_addr,
|
||||||
|
current_address->ai_addrlen) == 0) {
|
||||||
|
|
||||||
|
guac_client_log(client, GUAC_LOG_DEBUG, "Successfully connected to "
|
||||||
|
"host %s, port %s", connected_address, connected_port);
|
||||||
|
|
||||||
|
/* Done if successful connect */
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Otherwise log information regarding bind failure */
|
||||||
|
else
|
||||||
|
guac_client_log(client, GUAC_LOG_DEBUG, "Unable to connect to "
|
||||||
|
"host %s, port %s: %s",
|
||||||
|
connected_address, connected_port, strerror(errno));
|
||||||
|
|
||||||
|
current_address = current_address->ai_next;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If unable to connect to anything, fail */
|
||||||
|
if (current_address == NULL) {
|
||||||
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_NOT_FOUND,
|
||||||
|
"Unable to connect to any addresses.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Free addrinfo */
|
||||||
|
freeaddrinfo(addresses);
|
||||||
|
|
||||||
|
/* Open telnet session */
|
||||||
|
telnet_t* telnet = telnet_init(__telnet_options, __guac_telnet_event_handler, 0, client);
|
||||||
|
if (telnet == NULL) {
|
||||||
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Telnet client allocation failed.");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Save file descriptor */
|
||||||
|
telnet_client->socket_fd = fd;
|
||||||
|
|
||||||
|
return telnet;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a 16-bit value over the given telnet connection with the byte order
|
||||||
|
* required by the telnet protocol.
|
||||||
|
*
|
||||||
|
* @param telnet The telnet connection to use.
|
||||||
|
* @param value The value to send.
|
||||||
|
*/
|
||||||
|
static void __guac_telnet_send_uint16(telnet_t* telnet, uint16_t value) {
|
||||||
|
|
||||||
|
unsigned char buffer[2];
|
||||||
|
buffer[0] = (value >> 8) & 0xFF;
|
||||||
|
buffer[1] = value & 0xFF;
|
||||||
|
|
||||||
|
telnet_send(telnet, (char*) buffer, 2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends an 8-bit value over the given telnet connection.
|
||||||
|
*
|
||||||
|
* @param telnet The telnet connection to use.
|
||||||
|
* @param value The value to send.
|
||||||
|
*/
|
||||||
|
static void __guac_telnet_send_uint8(telnet_t* telnet, uint8_t value) {
|
||||||
|
telnet_send(telnet, (char*) (&value), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void guac_telnet_send_naws(telnet_t* telnet, uint16_t width, uint16_t height) {
|
||||||
|
telnet_begin_sb(telnet, TELNET_TELOPT_NAWS);
|
||||||
|
__guac_telnet_send_uint16(telnet, width);
|
||||||
|
__guac_telnet_send_uint16(telnet, height);
|
||||||
|
telnet_finish_sb(telnet);
|
||||||
|
}
|
||||||
|
|
||||||
|
void guac_telnet_send_user(telnet_t* telnet, const char* username) {
|
||||||
|
|
||||||
|
/* IAC SB NEW-ENVIRON IS */
|
||||||
|
telnet_begin_sb(telnet, TELNET_TELOPT_NEW_ENVIRON);
|
||||||
|
__guac_telnet_send_uint8(telnet, TELNET_ENVIRON_IS);
|
||||||
|
|
||||||
|
/* Only send username if defined */
|
||||||
|
if (username != NULL) {
|
||||||
|
|
||||||
|
/* VAR "USER" */
|
||||||
|
__guac_telnet_send_uint8(telnet, TELNET_ENVIRON_VAR);
|
||||||
|
telnet_send(telnet, "USER", 4);
|
||||||
|
|
||||||
|
/* VALUE username */
|
||||||
|
__guac_telnet_send_uint8(telnet, TELNET_ENVIRON_VALUE);
|
||||||
|
telnet_send(telnet, username, strlen(username));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* IAC SE */
|
||||||
|
telnet_finish_sb(telnet);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits for data on the given file descriptor for up to one second. The
|
||||||
|
* return value is identical to that of select(): 0 on timeout, < 0 on
|
||||||
|
* error, and > 0 on success.
|
||||||
|
*
|
||||||
|
* @param socket_fd The file descriptor to wait for.
|
||||||
|
* @return A value greater than zero on success, zero on timeout, and
|
||||||
|
* less than zero on error.
|
||||||
|
*/
|
||||||
|
static int __guac_telnet_wait(int socket_fd) {
|
||||||
|
|
||||||
|
/* Build array of file descriptors */
|
||||||
|
struct pollfd fds[] = {{
|
||||||
|
.fd = socket_fd,
|
||||||
|
.events = POLLIN,
|
||||||
|
.revents = 0,
|
||||||
|
}};
|
||||||
|
|
||||||
|
/* Wait for one second */
|
||||||
|
return poll(fds, 1, 1000);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void* guac_telnet_client_thread(void* data) {
|
||||||
|
|
||||||
|
guac_client* client = (guac_client*) data;
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
guac_telnet_settings* settings = telnet_client->settings;
|
||||||
|
|
||||||
|
pthread_t input_thread;
|
||||||
|
char buffer[8192];
|
||||||
|
int wait_result;
|
||||||
|
|
||||||
|
/* Set up screen recording, if requested */
|
||||||
|
if (settings->recording_path != NULL) {
|
||||||
|
telnet_client->recording = guac_common_recording_create(client,
|
||||||
|
settings->recording_path,
|
||||||
|
settings->recording_name,
|
||||||
|
settings->create_recording_path,
|
||||||
|
!settings->recording_exclude_output,
|
||||||
|
!settings->recording_exclude_mouse,
|
||||||
|
settings->recording_include_keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create terminal */
|
||||||
|
telnet_client->term = guac_terminal_create(client,
|
||||||
|
telnet_client->clipboard, settings->disable_copy,
|
||||||
|
settings->max_scrollback, settings->font_name, settings->font_size,
|
||||||
|
settings->resolution, settings->width, settings->height,
|
||||||
|
settings->color_scheme, settings->backspace);
|
||||||
|
|
||||||
|
/* Fail if terminal init failed */
|
||||||
|
if (telnet_client->term == NULL) {
|
||||||
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
|
||||||
|
"Terminal initialization failed");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set up typescript, if requested */
|
||||||
|
if (settings->typescript_path != NULL) {
|
||||||
|
guac_terminal_create_typescript(telnet_client->term,
|
||||||
|
settings->typescript_path,
|
||||||
|
settings->typescript_name,
|
||||||
|
settings->create_typescript_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Open telnet session */
|
||||||
|
telnet_client->telnet = __guac_telnet_create_session(client);
|
||||||
|
if (telnet_client->telnet == NULL) {
|
||||||
|
/* Already aborted within __guac_telnet_create_session() */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Logged in */
|
||||||
|
guac_client_log(client, GUAC_LOG_INFO, "Telnet connection successful.");
|
||||||
|
|
||||||
|
/* Allow terminal to render if login success/failure detection is not
|
||||||
|
* enabled */
|
||||||
|
if (settings->login_success_regex == NULL
|
||||||
|
&& settings->login_failure_regex == NULL)
|
||||||
|
guac_terminal_start(telnet_client->term);
|
||||||
|
|
||||||
|
/* Start input thread */
|
||||||
|
if (pthread_create(&(input_thread), NULL, __guac_telnet_input_thread, (void*) client)) {
|
||||||
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Unable to start input thread");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* While data available, write to terminal */
|
||||||
|
while ((wait_result = __guac_telnet_wait(telnet_client->socket_fd)) >= 0) {
|
||||||
|
|
||||||
|
/* Resume waiting of no data available */
|
||||||
|
if (wait_result == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int bytes_read = read(telnet_client->socket_fd, buffer, sizeof(buffer));
|
||||||
|
if (bytes_read <= 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
telnet_recv(telnet_client->telnet, buffer, bytes_read);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Kill client and Wait for input thread to die */
|
||||||
|
guac_client_stop(client);
|
||||||
|
pthread_join(input_thread, NULL);
|
||||||
|
|
||||||
|
guac_client_log(client, GUAC_LOG_INFO, "Telnet connection ended.");
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
106
src/protocols/tn5250/tn5250.h
Normal file
106
src/protocols/tn5250/tn5250.h
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* 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_TN5250_H
|
||||||
|
#define GUAC_TN5250_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "common/clipboard.h"
|
||||||
|
#include "common/recording.h"
|
||||||
|
#include "settings.h"
|
||||||
|
#include "terminal/terminal.h"
|
||||||
|
|
||||||
|
#include <libtelnet.h>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Telnet-specific client data.
|
||||||
|
*/
|
||||||
|
typedef struct guac_telnet_client {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Telnet connection settings.
|
||||||
|
*/
|
||||||
|
guac_telnet_settings* settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The telnet client thread.
|
||||||
|
*/
|
||||||
|
pthread_t client_thread;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The file descriptor of the socket connected to the telnet server,
|
||||||
|
* or -1 if no connection has been established.
|
||||||
|
*/
|
||||||
|
int socket_fd;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Telnet connection, used by the telnet client thread.
|
||||||
|
*/
|
||||||
|
telnet_t* telnet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether window size should be sent when the window is resized.
|
||||||
|
*/
|
||||||
|
int naws_enabled;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether all user input should be automatically echoed to the
|
||||||
|
* terminal.
|
||||||
|
*/
|
||||||
|
int echo_enabled;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current clipboard contents.
|
||||||
|
*/
|
||||||
|
guac_common_clipboard* clipboard;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The terminal which will render all output from the telnet client.
|
||||||
|
*/
|
||||||
|
guac_terminal* term;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The in-progress session recording, or NULL if no recording is in
|
||||||
|
* progress.
|
||||||
|
*/
|
||||||
|
guac_common_recording* recording;
|
||||||
|
|
||||||
|
} guac_telnet_client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main telnet client thread, handling transfer of telnet output to STDOUT.
|
||||||
|
*/
|
||||||
|
void* guac_telnet_client_thread(void* data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a telnet NAWS message indicating the given terminal window dimensions
|
||||||
|
* in characters.
|
||||||
|
*/
|
||||||
|
void guac_telnet_send_naws(telnet_t* telnet, uint16_t width, uint16_t height);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the given username by setting the remote USER environment variable
|
||||||
|
* using the telnet NEW-ENVIRON option.
|
||||||
|
*/
|
||||||
|
void guac_telnet_send_user(telnet_t* telnet, const char* username);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
121
src/protocols/tn5250/user.c
Normal file
121
src/protocols/tn5250/user.c
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* 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 "clipboard.h"
|
||||||
|
#include "input.h"
|
||||||
|
#include "pipe.h"
|
||||||
|
#include "settings.h"
|
||||||
|
#include "tn5250.h"
|
||||||
|
#include "terminal/terminal.h"
|
||||||
|
#include "user.h"
|
||||||
|
|
||||||
|
#include <guacamole/client.h>
|
||||||
|
#include <guacamole/socket.h>
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int guac_telnet_user_join_handler(guac_user* user, int argc, char** argv) {
|
||||||
|
|
||||||
|
guac_client* client = user->client;
|
||||||
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
||||||
|
|
||||||
|
/* Parse provided arguments */
|
||||||
|
guac_telnet_settings* settings = guac_telnet_parse_args(user,
|
||||||
|
argc, (const char**) argv);
|
||||||
|
|
||||||
|
/* Fail if settings cannot be parsed */
|
||||||
|
if (settings == NULL) {
|
||||||
|
guac_user_log(user, GUAC_LOG_INFO,
|
||||||
|
"Badly formatted client arguments.");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Store settings at user level */
|
||||||
|
user->data = settings;
|
||||||
|
|
||||||
|
/* Connect via telnet if owner */
|
||||||
|
if (user->owner) {
|
||||||
|
|
||||||
|
/* Store owner's settings at client level */
|
||||||
|
telnet_client->settings = settings;
|
||||||
|
|
||||||
|
/* Start client thread */
|
||||||
|
if (pthread_create(&(telnet_client->client_thread), NULL,
|
||||||
|
guac_telnet_client_thread, (void*) client)) {
|
||||||
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
|
||||||
|
"Unable to start telnet client thread");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If not owner, synchronize with current display */
|
||||||
|
else {
|
||||||
|
guac_terminal_dup(telnet_client->term, user, user->socket);
|
||||||
|
guac_socket_flush(user->socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Only handle events if not read-only */
|
||||||
|
if (!settings->read_only) {
|
||||||
|
|
||||||
|
/* General mouse/keyboard events */
|
||||||
|
user->key_handler = guac_telnet_user_key_handler;
|
||||||
|
user->mouse_handler = guac_telnet_user_mouse_handler;
|
||||||
|
|
||||||
|
/* Inbound (client to server) clipboard transfer */
|
||||||
|
if (!settings->disable_paste)
|
||||||
|
user->clipboard_handler = guac_telnet_clipboard_handler;
|
||||||
|
|
||||||
|
/* STDIN redirection */
|
||||||
|
user->pipe_handler = guac_telnet_pipe_handler;
|
||||||
|
|
||||||
|
/* Updates to connection parameters */
|
||||||
|
user->argv_handler = guac_telnet_argv_handler;
|
||||||
|
|
||||||
|
/* Display size change events */
|
||||||
|
user->size_handler = guac_telnet_user_size_handler;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int guac_telnet_user_leave_handler(guac_user* user) {
|
||||||
|
|
||||||
|
guac_telnet_client* telnet_client =
|
||||||
|
(guac_telnet_client*) user->client->data;
|
||||||
|
|
||||||
|
/* Update shared cursor state */
|
||||||
|
guac_common_cursor_remove_user(telnet_client->term->cursor, user);
|
||||||
|
|
||||||
|
/* Free settings if not owner (owner settings will be freed with client) */
|
||||||
|
if (!user->owner) {
|
||||||
|
guac_telnet_settings* settings = (guac_telnet_settings*) user->data;
|
||||||
|
guac_telnet_settings_free(settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
38
src/protocols/tn5250/user.h
Normal file
38
src/protocols/tn5250/user.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* 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_TN5250_USER_H
|
||||||
|
#define GUAC_TN5250_USER_H
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <guacamole/user.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for joining users.
|
||||||
|
*/
|
||||||
|
guac_user_join_handler guac_telnet_user_join_handler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler for leaving users.
|
||||||
|
*/
|
||||||
|
guac_user_leave_handler guac_telnet_user_leave_handler;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user