2013-12-29 04:53:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Glyptodon LLC
|
2013-05-18 03:47:05 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2013-05-18 03:47:05 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-05-18 03:47:05 +00:00
|
|
|
*
|
2013-12-29 04:53:12 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "config.h"
|
2013-05-18 03:47:05 +00:00
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "client.h"
|
2015-07-09 19:02:31 +00:00
|
|
|
#include "guac_ssh.h"
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "sftp.h"
|
|
|
|
#include "ssh_key.h"
|
2014-06-11 17:06:18 +00:00
|
|
|
#include "terminal.h"
|
|
|
|
|
|
|
|
#ifdef ENABLE_SSH_AGENT
|
|
|
|
#include "ssh_agent.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <libssh2.h>
|
|
|
|
#include <libssh2_sftp.h>
|
|
|
|
#include <guacamole/client.h>
|
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <guacamole/socket.h>
|
2015-01-05 03:56:01 +00:00
|
|
|
#include <openssl/err.h>
|
2014-06-11 17:06:18 +00:00
|
|
|
#include <openssl/ssl.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
|
2014-07-21 17:27:39 +00:00
|
|
|
#ifdef LIBSSH2_USES_GCRYPT
|
|
|
|
#include <gcrypt.h>
|
|
|
|
#endif
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <pthread.h>
|
2014-06-11 17:06:18 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
2013-05-18 03:47:05 +00:00
|
|
|
#include <stdio.h>
|
2014-06-11 17:06:18 +00:00
|
|
|
#include <stdlib.h>
|
2013-05-18 05:53:13 +00:00
|
|
|
#include <string.h>
|
2014-04-11 20:45:19 +00:00
|
|
|
#include <sys/select.h>
|
2013-12-02 00:09:48 +00:00
|
|
|
#include <sys/socket.h>
|
2014-06-11 17:06:18 +00:00
|
|
|
#include <sys/time.h>
|
2013-12-02 10:00:37 +00:00
|
|
|
|
2013-05-20 08:23:21 +00:00
|
|
|
void* ssh_input_thread(void* data) {
|
|
|
|
|
|
|
|
guac_client* client = (guac_client*) data;
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
|
|
|
|
|
|
|
char buffer[8192];
|
|
|
|
int bytes_read;
|
|
|
|
|
|
|
|
/* Write all data read */
|
2014-07-21 18:15:55 +00:00
|
|
|
while ((bytes_read = guac_terminal_read_stdin(client_data->term, buffer, sizeof(buffer))) > 0) {
|
|
|
|
pthread_mutex_lock(&(client_data->term_channel_lock));
|
2013-12-02 00:09:48 +00:00
|
|
|
libssh2_channel_write(client_data->term_channel, buffer, bytes_read);
|
2014-07-21 18:15:55 +00:00
|
|
|
pthread_mutex_unlock(&(client_data->term_channel_lock));
|
|
|
|
}
|
2013-05-20 08:23:21 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-02 00:31:11 +00:00
|
|
|
static int __sign_callback(LIBSSH2_SESSION* session,
|
|
|
|
unsigned char** sig, size_t* sig_len,
|
|
|
|
const unsigned char* data, size_t data_len, void **abstract) {
|
|
|
|
|
|
|
|
ssh_key* key = (ssh_key*) abstract;
|
2014-04-11 20:45:19 +00:00
|
|
|
int length;
|
2013-12-02 00:31:11 +00:00
|
|
|
|
|
|
|
/* Allocate space for signature */
|
|
|
|
*sig = malloc(4096);
|
|
|
|
|
|
|
|
/* Sign with key */
|
2014-04-11 20:45:19 +00:00
|
|
|
length = ssh_key_sign(key, (const char*) data, data_len, *sig);
|
|
|
|
if (length < 0)
|
2013-12-02 00:31:11 +00:00
|
|
|
return 1;
|
|
|
|
|
2014-04-11 20:45:19 +00:00
|
|
|
*sig_len = length;
|
2013-12-02 00:31:11 +00:00
|
|
|
return 0;
|
2014-06-13 16:12:51 +00:00
|
|
|
}
|
2013-12-02 00:31:11 +00:00
|
|
|
|
2014-06-13 16:12:51 +00:00
|
|
|
/**
|
|
|
|
* Callback for the keyboard-interactive authentication method. Currently
|
|
|
|
* suports just one prompt for the password.
|
|
|
|
*/
|
|
|
|
static void __kbd_callback(const char *name, int name_len,
|
|
|
|
const char *instruction, int instruction_len,
|
|
|
|
int num_prompts,
|
|
|
|
const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
|
|
|
|
LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
|
2014-07-17 18:49:24 +00:00
|
|
|
void **abstract) {
|
|
|
|
|
|
|
|
guac_client* client = (guac_client*) *abstract;
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
|
|
|
|
2014-06-13 16:12:51 +00:00
|
|
|
if (num_prompts == 1) {
|
|
|
|
responses[0].text = strdup(client_data->password);
|
|
|
|
responses[0].length = strlen(client_data->password);
|
|
|
|
}
|
2014-07-17 18:49:24 +00:00
|
|
|
else
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_WARNING,
|
|
|
|
"Unsupported number of keyboard-interactive prompts: %i",
|
|
|
|
num_prompts);
|
2013-12-02 00:31:11 +00:00
|
|
|
|
2014-07-17 18:49:24 +00:00
|
|
|
}
|
2014-06-13 16:12:51 +00:00
|
|
|
|
2013-12-03 10:22:46 +00:00
|
|
|
static LIBSSH2_SESSION* __guac_ssh_create_session(guac_client* client,
|
|
|
|
int* socket_fd) {
|
2013-12-02 00:09:48 +00:00
|
|
|
|
2013-12-02 00:26:41 +00:00
|
|
|
int retval;
|
|
|
|
|
2013-12-02 00:09:48 +00:00
|
|
|
int fd;
|
2013-12-02 00:26:41 +00:00
|
|
|
struct addrinfo* addresses;
|
|
|
|
struct addrinfo* current_address;
|
|
|
|
|
|
|
|
char connected_address[1024];
|
|
|
|
char connected_port[64];
|
2014-07-17 18:49:24 +00:00
|
|
|
char *user_authlist;
|
2013-10-19 05:05:03 +00:00
|
|
|
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
|
|
|
|
2013-12-02 00:26:41 +00:00
|
|
|
struct addrinfo hints = {
|
|
|
|
.ai_family = AF_UNSPEC,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
.ai_protocol = IPPROTO_TCP
|
|
|
|
};
|
|
|
|
|
2013-12-02 00:09:48 +00:00
|
|
|
/* Get socket */
|
|
|
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
2013-12-02 00:26:41 +00:00
|
|
|
/* Get addresses connection */
|
|
|
|
if ((retval = getaddrinfo(client_data->hostname, client_data->port,
|
|
|
|
&hints, &addresses))) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Error parsing given address or port: %s",
|
2013-12-02 00:26:41 +00:00
|
|
|
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));
|
2013-12-02 00:26:41 +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 "
|
2013-12-02 00:26:41 +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 "
|
2013-12-02 00:26:41 +00:00
|
|
|
"host %s, port %s: %s",
|
|
|
|
connected_address, connected_port, strerror(errno));
|
|
|
|
|
|
|
|
current_address = current_address->ai_next;
|
|
|
|
|
|
|
|
}
|
2013-12-02 00:09:48 +00:00
|
|
|
|
2013-12-02 00:26:41 +00:00
|
|
|
/* If unable to connect to anything, fail */
|
|
|
|
if (current_address == NULL) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to connect to any addresses.");
|
2013-12-02 00:09:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-02 17:23:31 +00:00
|
|
|
/* Free addrinfo */
|
|
|
|
freeaddrinfo(addresses);
|
|
|
|
|
2013-10-19 05:05:03 +00:00
|
|
|
/* Open SSH session */
|
2013-12-02 01:05:55 +00:00
|
|
|
LIBSSH2_SESSION* session = libssh2_session_init_ex(NULL, NULL,
|
|
|
|
NULL, client);
|
2013-10-19 05:05:03 +00:00
|
|
|
if (session == NULL) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Session allocation failed.");
|
2013-12-02 00:09:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform handshake */
|
|
|
|
if (libssh2_session_handshake(session, fd)) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "SSH handshake failed.");
|
2013-10-19 05:05:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-03 10:22:46 +00:00
|
|
|
/* Save file descriptor */
|
|
|
|
if (socket_fd != NULL)
|
|
|
|
*socket_fd = fd;
|
|
|
|
|
2014-07-17 18:49:24 +00:00
|
|
|
/* Get list of suported authentication methods */
|
|
|
|
user_authlist = libssh2_userauth_list(session, client_data->username, strlen(client_data->username));
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG, "Supported authentication methods: %s", user_authlist);
|
2014-06-13 16:12:51 +00:00
|
|
|
|
2013-10-31 06:19:11 +00:00
|
|
|
/* Authenticate with key if available */
|
|
|
|
if (client_data->key != NULL) {
|
2014-07-17 18:49:24 +00:00
|
|
|
|
|
|
|
/* Check if public key auth is suported on the server */
|
|
|
|
if (strstr(user_authlist, "publickey") == NULL) {
|
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
|
|
|
|
"Public key authentication not suported");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-06-13 16:12:51 +00:00
|
|
|
|
2013-12-02 00:31:11 +00:00
|
|
|
if (!libssh2_userauth_publickey(session, client_data->username,
|
|
|
|
(unsigned char*) client_data->key->public_key,
|
|
|
|
client_data->key->public_key_length,
|
|
|
|
__sign_callback, (void**) client_data->key))
|
2013-10-31 06:19:11 +00:00
|
|
|
return session;
|
|
|
|
else {
|
2013-12-02 00:31:11 +00:00
|
|
|
char* error_message;
|
|
|
|
libssh2_session_last_error(session, &error_message, NULL, 0);
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
|
2013-12-02 00:31:11 +00:00
|
|
|
"Public key authentication failed: %s", error_message);
|
2013-10-31 06:19:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Authenticate with password */
|
2014-07-17 18:49:24 +00:00
|
|
|
if (strstr(user_authlist, "password") != NULL) {
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG, "Using password authentication method");
|
2014-07-17 18:49:24 +00:00
|
|
|
retval = libssh2_userauth_password(session, client_data->username, client_data->password);
|
|
|
|
}
|
|
|
|
else if (strstr(user_authlist, "keyboard-interactive") != NULL) {
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG, "Using keyboard-interactive authentication method");
|
2014-07-17 18:49:24 +00:00
|
|
|
retval = libssh2_userauth_keyboard_interactive(session, client_data->username, &__kbd_callback);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_BAD_TYPE, "No known authentication methods");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retval == 0)
|
2013-10-31 06:19:11 +00:00
|
|
|
return session;
|
2014-07-17 18:49:24 +00:00
|
|
|
|
2013-10-31 06:19:11 +00:00
|
|
|
else {
|
2013-12-02 00:09:48 +00:00
|
|
|
char* error_message;
|
|
|
|
libssh2_session_last_error(session, &error_message, NULL, 0);
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
|
2013-12-02 00:09:48 +00:00
|
|
|
"Password authentication failed: %s", error_message);
|
2013-10-19 05:05:03 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-18 05:53:13 +00:00
|
|
|
void* ssh_client_thread(void* data) {
|
|
|
|
|
2013-05-20 07:33:17 +00:00
|
|
|
guac_client* client = (guac_client*) data;
|
2013-05-20 08:23:21 +00:00
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
2013-05-20 07:33:17 +00:00
|
|
|
|
2013-05-26 06:15:55 +00:00
|
|
|
char name[1024];
|
|
|
|
|
2013-05-20 08:23:21 +00:00
|
|
|
guac_socket* socket = client->socket;
|
|
|
|
char buffer[8192];
|
2013-05-20 17:44:43 +00:00
|
|
|
int bytes_read = -1234;
|
2013-05-20 08:23:21 +00:00
|
|
|
|
2013-12-03 10:22:46 +00:00
|
|
|
int socket_fd;
|
2013-05-20 08:23:21 +00:00
|
|
|
|
|
|
|
pthread_t input_thread;
|
2013-05-18 05:53:13 +00:00
|
|
|
|
2015-07-09 19:02:31 +00:00
|
|
|
/* Init SSH base libraries */
|
|
|
|
if (guac_common_ssh_init(client))
|
2014-07-21 17:27:39 +00:00
|
|
|
return NULL;
|
2013-12-02 00:56:36 +00:00
|
|
|
|
2013-05-18 05:53:13 +00:00
|
|
|
/* Get username */
|
2014-03-22 02:47:42 +00:00
|
|
|
if (client_data->username[0] == 0)
|
2014-05-06 23:41:48 +00:00
|
|
|
guac_terminal_prompt(client_data->term, "Login as: ",
|
|
|
|
client_data->username, sizeof(client_data->username), true);
|
2013-05-18 05:53:13 +00:00
|
|
|
|
2013-05-26 06:15:55 +00:00
|
|
|
/* Send new name */
|
|
|
|
snprintf(name, sizeof(name)-1, "%s@%s", client_data->username, client_data->hostname);
|
|
|
|
guac_protocol_send_name(socket, name);
|
|
|
|
|
2013-10-31 06:19:11 +00:00
|
|
|
/* If key specified, import */
|
|
|
|
if (client_data->key_base64[0] != 0) {
|
|
|
|
|
2015-01-05 03:56:01 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG,
|
|
|
|
"Attempting private key import (WITHOUT passphrase)");
|
|
|
|
|
2013-10-31 06:19:11 +00:00
|
|
|
/* Attempt to read key without passphrase */
|
2013-12-02 00:56:36 +00:00
|
|
|
client_data->key = ssh_key_alloc(client_data->key_base64,
|
|
|
|
strlen(client_data->key_base64), "");
|
2013-10-31 06:19:11 +00:00
|
|
|
|
|
|
|
/* On failure, attempt with passphrase */
|
2013-12-02 00:56:36 +00:00
|
|
|
if (client_data->key == NULL) {
|
2013-10-31 06:19:11 +00:00
|
|
|
|
2015-01-05 03:56:01 +00:00
|
|
|
/* Log failure of initial attempt */
|
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG,
|
|
|
|
"Initial import failed: %s", ssh_key_error());
|
|
|
|
|
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG,
|
|
|
|
"Re-attempting private key import (WITH passphrase)");
|
|
|
|
|
2013-10-31 06:19:11 +00:00
|
|
|
/* Prompt for passphrase if missing */
|
2014-03-22 02:47:42 +00:00
|
|
|
if (client_data->key_passphrase[0] == 0)
|
2014-05-06 23:41:48 +00:00
|
|
|
guac_terminal_prompt(client_data->term, "Key passphrase: ",
|
|
|
|
client_data->key_passphrase, sizeof(client_data->key_passphrase), false);
|
2013-10-31 06:19:11 +00:00
|
|
|
|
|
|
|
/* Import key with passphrase */
|
2013-12-02 00:56:36 +00:00
|
|
|
client_data->key = ssh_key_alloc(client_data->key_base64,
|
|
|
|
strlen(client_data->key_base64),
|
|
|
|
client_data->key_passphrase);
|
|
|
|
|
|
|
|
/* If still failing, give up */
|
|
|
|
if (client_data->key == NULL) {
|
2015-01-05 03:17:40 +00:00
|
|
|
guac_client_abort(client,
|
|
|
|
GUAC_PROTOCOL_STATUS_CLIENT_UNAUTHORIZED,
|
2015-01-05 03:56:01 +00:00
|
|
|
"Auth key import failed: %s", ssh_key_error());
|
2013-10-31 06:19:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* end decrypt key with passphrase */
|
2013-05-20 08:23:21 +00:00
|
|
|
|
2013-12-02 00:56:36 +00:00
|
|
|
/* Success */
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Auth key successfully imported.");
|
2013-12-02 00:56:36 +00:00
|
|
|
|
2013-10-31 06:19:11 +00:00
|
|
|
} /* end if key given */
|
|
|
|
|
|
|
|
/* Otherwise, get password if not provided */
|
2014-03-22 02:47:42 +00:00
|
|
|
else if (client_data->password[0] == 0)
|
2014-05-06 23:41:48 +00:00
|
|
|
guac_terminal_prompt(client_data->term, "Password: ",
|
|
|
|
client_data->password, sizeof(client_data->password), false);
|
2013-05-26 06:15:55 +00:00
|
|
|
|
2013-05-22 18:54:28 +00:00
|
|
|
/* Clear screen */
|
2014-05-06 23:41:48 +00:00
|
|
|
guac_terminal_printf(client_data->term, "\x1B[H\x1B[J");
|
2013-05-22 18:54:28 +00:00
|
|
|
|
2013-05-20 08:23:21 +00:00
|
|
|
/* Open SSH session */
|
2013-12-03 10:22:46 +00:00
|
|
|
client_data->session = __guac_ssh_create_session(client, &socket_fd);
|
2013-05-20 08:23:21 +00:00
|
|
|
if (client_data->session == NULL) {
|
2014-03-22 02:47:42 +00:00
|
|
|
/* Already aborted within __guac_ssh_create_session() */
|
2013-05-20 08:23:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-21 18:23:36 +00:00
|
|
|
pthread_mutex_init(&client_data->term_channel_lock, NULL);
|
|
|
|
|
2013-05-20 08:23:21 +00:00
|
|
|
/* Open channel for terminal */
|
2014-03-22 02:47:42 +00:00
|
|
|
client_data->term_channel = libssh2_channel_open_session(client_data->session);
|
2013-05-20 08:23:21 +00:00
|
|
|
if (client_data->term_channel == NULL) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to open terminal channel.");
|
2013-05-18 05:53:13 +00:00
|
|
|
return NULL;
|
2013-05-20 08:23:21 +00:00
|
|
|
}
|
2013-05-18 05:53:13 +00:00
|
|
|
|
2013-12-02 10:00:37 +00:00
|
|
|
#ifdef ENABLE_SSH_AGENT
|
2013-12-02 10:07:17 +00:00
|
|
|
/* Start SSH agent forwarding, if enabled */
|
|
|
|
if (client_data->enable_agent) {
|
|
|
|
libssh2_session_callback_set(client_data->session,
|
|
|
|
LIBSSH2_CALLBACK_AUTH_AGENT, (void*) ssh_auth_agent_callback);
|
|
|
|
|
|
|
|
/* Request agent forwarding */
|
|
|
|
if (libssh2_channel_request_auth_agent(client_data->term_channel))
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_ERROR, "Agent forwarding request failed");
|
2013-12-02 10:07:17 +00:00
|
|
|
else
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Agent forwarding enabled.");
|
2013-12-02 10:07:17 +00:00
|
|
|
}
|
2013-12-03 10:22:46 +00:00
|
|
|
|
|
|
|
client_data->auth_agent = NULL;
|
2013-12-02 10:00:37 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-18 22:37:16 +00:00
|
|
|
/* Start SFTP session as well, if enabled */
|
|
|
|
if (client_data->enable_sftp) {
|
|
|
|
|
2014-05-07 00:14:40 +00:00
|
|
|
/* Init handlers for Guacamole-specific console codes */
|
|
|
|
client_data->term->upload_path_handler = guac_sftp_set_upload_path;
|
|
|
|
client_data->term->file_download_handler = guac_sftp_download_file;
|
|
|
|
|
2013-10-19 05:05:03 +00:00
|
|
|
/* Create SSH session specific for SFTP */
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG, "Reconnecting for SFTP...");
|
2013-12-03 10:22:46 +00:00
|
|
|
client_data->sftp_ssh_session = __guac_ssh_create_session(client, NULL);
|
2014-03-22 02:47:42 +00:00
|
|
|
if (client_data->sftp_ssh_session == NULL) {
|
|
|
|
/* Already aborted within __guac_ssh_create_session() */
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-19 05:05:03 +00:00
|
|
|
|
2013-10-18 22:37:16 +00:00
|
|
|
/* Request SFTP */
|
2014-03-22 02:47:42 +00:00
|
|
|
client_data->sftp_session = libssh2_sftp_init(client_data->sftp_ssh_session);
|
2013-10-18 22:37:16 +00:00
|
|
|
if (client_data->sftp_session == NULL) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to start SFTP session.");
|
2013-10-18 22:37:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-10 20:47:36 +00:00
|
|
|
/* Set file handler */
|
2013-10-26 23:30:06 +00:00
|
|
|
client->file_handler = guac_sftp_file_handler;
|
2013-10-19 01:20:46 +00:00
|
|
|
|
2015-06-21 05:17:27 +00:00
|
|
|
/* Expose filesystem */
|
2015-07-01 17:43:20 +00:00
|
|
|
client_data->sftp_filesystem = guac_sftp_expose_filesystem(client);
|
2015-06-21 05:17:27 +00:00
|
|
|
|
2014-11-29 01:20:02 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_DEBUG, "SFTP session initialized");
|
2013-10-18 22:37:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-20 08:23:21 +00:00
|
|
|
/* Request PTY */
|
2014-03-22 02:47:42 +00:00
|
|
|
if (libssh2_channel_request_pty_ex(client_data->term_channel, "linux", sizeof("linux")-1, NULL, 0,
|
|
|
|
client_data->term->term_width, client_data->term->term_height, 0, 0)) {
|
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to allocate PTY.");
|
2013-05-20 08:23:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Request shell */
|
2013-12-02 00:09:48 +00:00
|
|
|
if (libssh2_channel_shell(client_data->term_channel)) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to associate shell with PTY.");
|
2013-05-20 08:23:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Logged in */
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "SSH connection successful.");
|
2013-05-20 08:23:21 +00:00
|
|
|
|
|
|
|
/* Start input thread */
|
|
|
|
if (pthread_create(&(input_thread), NULL, ssh_input_thread, (void*) client)) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Unable to start input thread");
|
2013-05-20 08:23:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-03 10:22:46 +00:00
|
|
|
/* Set non-blocking */
|
|
|
|
libssh2_session_set_blocking(client_data->session, 0);
|
|
|
|
|
2013-05-20 08:23:21 +00:00
|
|
|
/* While data available, write to terminal */
|
2013-12-03 10:22:46 +00:00
|
|
|
bytes_read = 0;
|
2014-07-21 18:15:55 +00:00
|
|
|
for (;;) {
|
2013-05-20 17:44:43 +00:00
|
|
|
|
2013-12-03 10:22:46 +00:00
|
|
|
/* Track total amount of data read */
|
|
|
|
int total_read = 0;
|
|
|
|
|
2014-07-21 18:15:55 +00:00
|
|
|
pthread_mutex_lock(&(client_data->term_channel_lock));
|
|
|
|
|
|
|
|
/* Stop reading at EOF */
|
|
|
|
if (libssh2_channel_eof(client_data->term_channel)) {
|
|
|
|
pthread_mutex_unlock(&(client_data->term_channel_lock));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-03 10:22:46 +00:00
|
|
|
/* Read terminal data */
|
|
|
|
bytes_read = libssh2_channel_read(client_data->term_channel,
|
|
|
|
buffer, sizeof(buffer));
|
2013-05-20 08:23:21 +00:00
|
|
|
|
2014-07-21 18:15:55 +00:00
|
|
|
pthread_mutex_unlock(&(client_data->term_channel_lock));
|
|
|
|
|
2013-05-26 06:05:58 +00:00
|
|
|
/* Attempt to write data received. Exit on failure. */
|
|
|
|
if (bytes_read > 0) {
|
2014-05-06 23:41:48 +00:00
|
|
|
int written = guac_terminal_write_stdout(client_data->term, buffer, bytes_read);
|
2013-05-26 06:05:58 +00:00
|
|
|
if (written < 0)
|
|
|
|
break;
|
2013-12-03 10:22:46 +00:00
|
|
|
|
|
|
|
total_read += bytes_read;
|
|
|
|
}
|
|
|
|
|
2014-01-02 04:18:43 +00:00
|
|
|
else if (bytes_read < 0 && bytes_read != LIBSSH2_ERROR_EAGAIN)
|
|
|
|
break;
|
|
|
|
|
2013-12-03 17:21:47 +00:00
|
|
|
#ifdef ENABLE_SSH_AGENT
|
2013-12-03 10:22:46 +00:00
|
|
|
/* If agent open, handle any agent packets */
|
|
|
|
if (client_data->auth_agent != NULL) {
|
|
|
|
bytes_read = ssh_auth_agent_read(client_data->auth_agent);
|
|
|
|
if (bytes_read > 0)
|
|
|
|
total_read += bytes_read;
|
|
|
|
else if (bytes_read < 0 && bytes_read != LIBSSH2_ERROR_EAGAIN)
|
|
|
|
client_data->auth_agent = NULL;
|
|
|
|
}
|
2013-12-03 17:21:47 +00:00
|
|
|
#endif
|
2013-12-03 10:22:46 +00:00
|
|
|
|
|
|
|
/* Wait for more data if reads turn up empty */
|
|
|
|
if (total_read == 0) {
|
|
|
|
fd_set fds;
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(socket_fd, &fds);
|
|
|
|
|
|
|
|
/* Wait for one second */
|
|
|
|
timeout.tv_sec = 1;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
if (select(socket_fd+1, &fds, NULL, NULL, &timeout) < 0)
|
|
|
|
break;
|
2013-05-26 06:05:58 +00:00
|
|
|
}
|
2013-05-20 08:23:21 +00:00
|
|
|
|
|
|
|
}
|
2013-05-18 05:53:13 +00:00
|
|
|
|
2014-01-02 04:18:43 +00:00
|
|
|
/* Kill client and Wait for input thread to die */
|
|
|
|
guac_client_stop(client);
|
2013-05-20 17:52:47 +00:00
|
|
|
pthread_join(input_thread, NULL);
|
|
|
|
|
2014-07-21 18:15:55 +00:00
|
|
|
pthread_mutex_destroy(&client_data->term_channel_lock);
|
2015-07-09 19:02:31 +00:00
|
|
|
guac_common_ssh_uninit();
|
2014-07-21 18:15:55 +00:00
|
|
|
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "SSH connection ended.");
|
2013-05-18 03:47:05 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|