2014-05-07 18:32:19 +00:00
|
|
|
/*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
2014-05-07 18:32:19 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-07 18:32:19 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-05-07 18:32:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2016-09-12 00:28:50 +00:00
|
|
|
#include "common/recording.h"
|
2016-03-15 02:47:36 +00:00
|
|
|
#include "telnet.h"
|
2017-02-27 22:34:46 +00:00
|
|
|
#include "terminal/terminal.h"
|
2014-05-07 18:32:19 +00:00
|
|
|
|
2014-06-11 18:09:44 +00:00
|
|
|
#include <guacamole/client.h>
|
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <libtelnet.h>
|
|
|
|
|
2014-05-07 18:32:19 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
2016-11-11 21:13:48 +00:00
|
|
|
#include <poll.h>
|
2014-05-07 18:32:19 +00:00
|
|
|
#include <pthread.h>
|
2018-09-02 21:32:05 +00:00
|
|
|
#include <stdbool.h>
|
2014-06-17 19:24:07 +00:00
|
|
|
#include <stdlib.h>
|
2014-05-07 18:32:19 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
2014-06-11 18:09:44 +00:00
|
|
|
#include <sys/time.h>
|
2014-05-07 18:32:19 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-05-08 04:19:15 +00:00
|
|
|
/**
|
|
|
|
* Support levels for various telnet options, required for connection
|
|
|
|
* negotiation by telnet_init(), part of libtelnet.
|
|
|
|
*/
|
2014-05-07 18:32:19 +00:00
|
|
|
static const telnet_telopt_t __telnet_options[] = {
|
2014-06-17 01:07:13 +00:00
|
|
|
{ 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 },
|
2014-05-07 18:32:19 +00:00
|
|
|
{ -1, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2014-05-08 04:19:15 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-05-07 21:36:49 +00:00
|
|
|
static int __guac_telnet_write_all(int fd, const char* buffer, int size) {
|
2014-05-07 18:32:19 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-17 19:24:07 +00:00
|
|
|
/**
|
2018-09-03 05:59:18 +00:00
|
|
|
* 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.
|
2018-09-02 21:32:05 +00:00
|
|
|
*
|
|
|
|
* @param client
|
|
|
|
* The guac_client associated with the telnet session.
|
|
|
|
*
|
|
|
|
* @param regex
|
2018-09-03 05:59:18 +00:00
|
|
|
* The regex to search for within the given line buffer.
|
2018-09-02 21:32:05 +00:00
|
|
|
*
|
|
|
|
* @param value
|
2018-09-03 05:59:18 +00:00
|
|
|
* The string value to send through STDIN of the telnet session if a
|
|
|
|
* match is found, or NULL if no value should be sent.
|
2018-09-02 21:32:05 +00:00
|
|
|
*
|
2018-09-03 05:59:18 +00:00
|
|
|
* @param line_buffer
|
|
|
|
* The line of character data to test.
|
2018-09-02 21:32:05 +00:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* true if a match is found, false otherwise.
|
2014-06-17 19:24:07 +00:00
|
|
|
*/
|
2018-09-03 05:59:18 +00:00
|
|
|
static bool guac_telnet_regex_exec(guac_client* client, regex_t* regex,
|
|
|
|
const char* value, const char* line_buffer) {
|
2014-06-17 19:24:07 +00:00
|
|
|
|
2016-03-15 02:47:36 +00:00
|
|
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
2014-06-17 19:24:07 +00:00
|
|
|
|
2014-07-17 19:39:19 +00:00
|
|
|
/* Send value upon match */
|
2014-07-02 20:19:12 +00:00
|
|
|
if (regexec(regex, line_buffer, 0, NULL, 0) == 0) {
|
2014-06-17 19:24:07 +00:00
|
|
|
|
2014-07-17 19:39:19 +00:00
|
|
|
/* Send value */
|
2018-09-02 21:32:05 +00:00
|
|
|
if (value != NULL) {
|
|
|
|
guac_terminal_send_string(telnet_client->term, value);
|
|
|
|
guac_terminal_send_string(telnet_client->term, "\x0D");
|
|
|
|
}
|
2014-06-17 19:24:07 +00:00
|
|
|
|
2014-07-17 19:39:19 +00:00
|
|
|
/* Stop searching for prompt */
|
2018-09-02 21:32:05 +00:00
|
|
|
return true;
|
2014-07-17 19:39:19 +00:00
|
|
|
|
2014-06-17 19:24:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 21:32:05 +00:00
|
|
|
return false;
|
2018-09-03 05:59:18 +00:00
|
|
|
|
2014-06-17 19:24:07 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 04:19:15 +00:00
|
|
|
/**
|
2018-09-03 05:59:18 +00:00
|
|
|
* 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.
|
2014-05-08 04:19:15 +00:00
|
|
|
*/
|
2018-09-03 05:59:18 +00:00
|
|
|
static void guac_telnet_search_line(guac_client* client, const char* line_buffer) {
|
2014-05-07 18:32:19 +00:00
|
|
|
|
2016-03-15 02:47:36 +00:00
|
|
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
|
|
|
guac_telnet_settings* settings = telnet_client->settings;
|
2014-05-07 18:32:19 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 18:32:19 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/* Continue search for password prompt */
|
|
|
|
if (settings->password_regex != NULL) {
|
|
|
|
if (guac_telnet_regex_exec(client, settings->password_regex,
|
|
|
|
settings->password, line_buffer)) {
|
2014-06-17 19:24:07 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG, "Password sent");
|
2014-06-17 19:24:07 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/* 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);
|
2014-07-17 19:39:19 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-29 01:20:02 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/* Continue search for login success */
|
|
|
|
if (settings->login_success_regex != NULL) {
|
|
|
|
if (guac_telnet_regex_exec(client, settings->login_success_regex,
|
|
|
|
NULL, line_buffer)) {
|
2014-07-17 19:39:19 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/* 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);
|
2018-09-02 21:32:05 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-02 21:32:05 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/* Continue search for login failure */
|
|
|
|
if (settings->login_failure_regex != NULL) {
|
|
|
|
if (guac_telnet_regex_exec(client, settings->login_failure_regex,
|
|
|
|
NULL, line_buffer)) {
|
2018-09-02 21:32:05 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/* Advise that login has failed and connection should be closed */
|
|
|
|
guac_client_abort(client,
|
|
|
|
GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
|
|
|
|
"Login failed");
|
2018-09-02 21:32:05 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/* 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 21:32:05 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
}
|
2018-09-02 21:32:05 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2018-09-02 21:32:05 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/* Append all characters in buffer to current line */
|
|
|
|
const char* current = buffer;
|
|
|
|
for (int i = 0; i < size; i++) {
|
2018-09-02 21:32:05 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
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;
|
2018-09-02 21:32:05 +00:00
|
|
|
}
|
2018-09-03 05:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Append all non-newline characters to line buffer as long as space
|
|
|
|
* remains */
|
|
|
|
else if (length < sizeof(line_buffer) - 1)
|
|
|
|
line_buffer[length++] = c;
|
|
|
|
|
|
|
|
}
|
2018-09-02 21:32:05 +00:00
|
|
|
|
2018-09-03 05:59:18 +00:00
|
|
|
/* 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);
|
2014-05-07 18:32:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Data destined for remote end */
|
|
|
|
case TELNET_EV_SEND:
|
2016-03-15 02:47:36 +00:00
|
|
|
if (__guac_telnet_write_all(telnet_client->socket_fd, event->data.buffer, event->data.size)
|
2014-05-07 21:36:49 +00:00
|
|
|
!= event->data.size)
|
2014-05-07 18:32:19 +00:00
|
|
|
guac_client_stop(client);
|
|
|
|
break;
|
|
|
|
|
2014-05-07 22:30:48 +00:00
|
|
|
/* Remote feature enabled */
|
|
|
|
case TELNET_EV_WILL:
|
|
|
|
if (event->neg.telopt == TELNET_TELOPT_ECHO)
|
2016-03-15 02:47:36 +00:00
|
|
|
telnet_client->echo_enabled = 0; /* Disable local echo, as remote will echo */
|
2014-05-07 22:30:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Remote feature disabled */
|
|
|
|
case TELNET_EV_WONT:
|
|
|
|
if (event->neg.telopt == TELNET_TELOPT_ECHO)
|
2016-03-15 02:47:36 +00:00
|
|
|
telnet_client->echo_enabled = 1; /* Enable local echo, as remote won't echo */
|
2014-05-07 22:30:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Local feature enable */
|
2014-05-07 21:19:17 +00:00
|
|
|
case TELNET_EV_DO:
|
|
|
|
if (event->neg.telopt == TELNET_TELOPT_NAWS) {
|
2016-03-15 02:47:36 +00:00
|
|
|
telnet_client->naws_enabled = 1;
|
|
|
|
guac_telnet_send_naws(telnet, telnet_client->term->term_width, telnet_client->term->term_height);
|
2014-05-07 21:19:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-05-07 18:32:19 +00:00
|
|
|
/* Terminal type request */
|
|
|
|
case TELNET_EV_TTYPE:
|
|
|
|
if (event->ttype.cmd == TELNET_TTYPE_SEND)
|
2018-01-10 04:11:12 +00:00
|
|
|
telnet_ttype_is(telnet_client->telnet, settings->terminal_type);
|
2014-05-07 18:32:19 +00:00
|
|
|
break;
|
|
|
|
|
2014-06-17 01:07:13 +00:00
|
|
|
/* Environment request */
|
|
|
|
case TELNET_EV_ENVIRON:
|
2014-07-17 19:15:43 +00:00
|
|
|
|
2014-06-17 01:07:13 +00:00
|
|
|
/* Only send USER if entire environment was requested */
|
|
|
|
if (event->environ.size == 0)
|
2016-03-15 02:47:36 +00:00
|
|
|
guac_telnet_send_user(telnet, settings->username);
|
2014-06-17 01:07:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-05-07 18:32:19 +00:00
|
|
|
/* Connection warnings */
|
|
|
|
case TELNET_EV_WARNING:
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_WARNING, "%s", event->error.msg);
|
2014-05-07 18:32:19 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-08 04:19:15 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-05-07 21:36:49 +00:00
|
|
|
static void* __guac_telnet_input_thread(void* data) {
|
2014-05-07 18:32:19 +00:00
|
|
|
|
|
|
|
guac_client* client = (guac_client*) data;
|
2016-03-15 02:47:36 +00:00
|
|
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
2014-05-07 18:32:19 +00:00
|
|
|
|
|
|
|
char buffer[8192];
|
|
|
|
int bytes_read;
|
|
|
|
|
|
|
|
/* Write all data read */
|
2016-03-15 02:47:36 +00:00
|
|
|
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)
|
2017-01-15 23:31:33 +00:00
|
|
|
guac_terminal_write(telnet_client->term, buffer, bytes_read);
|
2014-05-07 22:30:48 +00:00
|
|
|
}
|
2014-05-07 18:32:19 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-08 04:19:15 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-05-07 18:32:19 +00:00
|
|
|
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];
|
|
|
|
|
2016-03-15 02:47:36 +00:00
|
|
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
|
|
|
guac_telnet_settings* settings = telnet_client->settings;
|
2014-05-07 18:32:19 +00:00
|
|
|
|
|
|
|
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 */
|
2016-03-15 02:47:36 +00:00
|
|
|
if ((retval = getaddrinfo(settings->hostname, settings->port,
|
2014-05-07 18:32:19 +00:00
|
|
|
&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)))
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG, "Unable to resolve host: %s", gai_strerror(retval));
|
2014-05-07 18:32:19 +00:00
|
|
|
|
|
|
|
/* Connect */
|
|
|
|
if (connect(fd, current_address->ai_addr,
|
|
|
|
current_address->ai_addrlen) == 0) {
|
|
|
|
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG, "Successfully connected to "
|
2014-05-07 18:32:19 +00:00
|
|
|
"host %s, port %s", connected_address, connected_port);
|
|
|
|
|
|
|
|
/* Done if successful connect */
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise log information regarding bind failure */
|
|
|
|
else
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG, "Unable to connect to "
|
2014-05-07 18:32:19 +00:00
|
|
|
"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) {
|
2017-02-16 05:37:59 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_NOT_FOUND,
|
|
|
|
"Unable to connect to any addresses.");
|
2014-05-07 18:32:19 +00:00
|
|
|
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 */
|
2016-03-15 02:47:36 +00:00
|
|
|
telnet_client->socket_fd = fd;
|
2014-05-07 18:32:19 +00:00
|
|
|
|
|
|
|
return telnet;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-08 04:19:15 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-05-07 21:19:17 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-17 01:07:13 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2014-05-07 21:19:17 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-06-17 01:07:13 +00:00
|
|
|
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);
|
|
|
|
|
2016-03-15 02:47:36 +00:00
|
|
|
/* Only send username if defined */
|
|
|
|
if (username != NULL) {
|
2014-06-17 01:07:13 +00:00
|
|
|
|
2016-03-15 02:47:36 +00:00
|
|
|
/* 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));
|
|
|
|
|
|
|
|
}
|
2014-06-17 01:07:13 +00:00
|
|
|
|
|
|
|
/* IAC SE */
|
|
|
|
telnet_finish_sb(telnet);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-08 04:19:15 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-05-07 22:19:53 +00:00
|
|
|
static int __guac_telnet_wait(int socket_fd) {
|
|
|
|
|
2016-11-11 21:13:48 +00:00
|
|
|
/* Build array of file descriptors */
|
|
|
|
struct pollfd fds[] = {{
|
|
|
|
.fd = socket_fd,
|
|
|
|
.events = POLLIN,
|
|
|
|
.revents = 0,
|
|
|
|
}};
|
2014-05-07 22:19:53 +00:00
|
|
|
|
|
|
|
/* Wait for one second */
|
2016-11-11 21:13:48 +00:00
|
|
|
return poll(fds, 1, 1000);
|
2014-05-07 22:19:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-07 21:36:49 +00:00
|
|
|
void* guac_telnet_client_thread(void* data) {
|
2014-05-07 18:32:19 +00:00
|
|
|
|
|
|
|
guac_client* client = (guac_client*) data;
|
2016-03-15 02:47:36 +00:00
|
|
|
guac_telnet_client* telnet_client = (guac_telnet_client*) client->data;
|
|
|
|
guac_telnet_settings* settings = telnet_client->settings;
|
2014-05-07 18:32:19 +00:00
|
|
|
|
|
|
|
pthread_t input_thread;
|
|
|
|
char buffer[8192];
|
2014-05-07 22:19:53 +00:00
|
|
|
int wait_result;
|
2014-05-07 18:32:19 +00:00
|
|
|
|
2016-03-15 03:26:31 +00:00
|
|
|
/* Set up screen recording, if requested */
|
|
|
|
if (settings->recording_path != NULL) {
|
2017-11-27 17:38:20 +00:00
|
|
|
telnet_client->recording = guac_common_recording_create(client,
|
2016-03-15 03:26:31 +00:00
|
|
|
settings->recording_path,
|
|
|
|
settings->recording_name,
|
2017-12-08 20:06:30 +00:00
|
|
|
settings->create_recording_path,
|
|
|
|
!settings->recording_exclude_output,
|
|
|
|
!settings->recording_exclude_mouse,
|
|
|
|
settings->recording_include_keys);
|
2016-03-15 03:26:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 02:47:36 +00:00
|
|
|
/* Create terminal */
|
|
|
|
telnet_client->term = guac_terminal_create(client,
|
2018-07-31 06:01:27 +00:00
|
|
|
telnet_client->clipboard,
|
2018-08-13 07:48:46 +00:00
|
|
|
settings->max_scrollback, settings->font_name, settings->font_size,
|
2016-03-15 02:47:36 +00:00
|
|
|
settings->resolution, settings->width, settings->height,
|
2018-02-24 03:14:11 +00:00
|
|
|
settings->color_scheme, settings->backspace);
|
2016-03-15 02:47:36 +00:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:32:19 +00:00
|
|
|
/* Open telnet session */
|
2016-03-15 02:47:36 +00:00
|
|
|
telnet_client->telnet = __guac_telnet_create_session(client);
|
|
|
|
if (telnet_client->telnet == NULL) {
|
2014-05-07 18:32:19 +00:00
|
|
|
/* Already aborted within __guac_telnet_create_session() */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Logged in */
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Telnet connection successful.");
|
2014-05-07 18:32:19 +00:00
|
|
|
|
2018-09-02 21:32:05 +00:00
|
|
|
/* 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);
|
|
|
|
|
2014-05-07 18:32:19 +00:00
|
|
|
/* Start input thread */
|
2014-05-07 21:36:49 +00:00
|
|
|
if (pthread_create(&(input_thread), NULL, __guac_telnet_input_thread, (void*) client)) {
|
2014-05-07 18:32:19 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Unable to start input thread");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* While data available, write to terminal */
|
2016-03-15 02:47:36 +00:00
|
|
|
while ((wait_result = __guac_telnet_wait(telnet_client->socket_fd)) >= 0) {
|
2014-05-07 22:19:53 +00:00
|
|
|
|
|
|
|
/* Resume waiting of no data available */
|
|
|
|
if (wait_result == 0)
|
|
|
|
continue;
|
|
|
|
|
2016-03-15 02:47:36 +00:00
|
|
|
int bytes_read = read(telnet_client->socket_fd, buffer, sizeof(buffer));
|
2014-05-07 22:19:53 +00:00
|
|
|
if (bytes_read <= 0)
|
|
|
|
break;
|
|
|
|
|
2016-03-15 02:47:36 +00:00
|
|
|
telnet_recv(telnet_client->telnet, buffer, bytes_read);
|
2014-05-07 18:32:19 +00:00
|
|
|
|
2014-05-07 22:19:53 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 18:32:19 +00:00
|
|
|
/* Kill client and Wait for input thread to die */
|
|
|
|
guac_client_stop(client);
|
|
|
|
pthread_join(input_thread, NULL);
|
|
|
|
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Telnet connection ended.");
|
2014-05-07 18:32:19 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|