2013-12-29 04:53:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Glyptodon LLC
|
2011-07-30 22:12:28 +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:
|
2011-07-30 22:12:28 +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.
|
2011-07-30 22:12:28 +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"
|
2011-07-30 22:12:28 +00:00
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "client.h"
|
2014-04-10 20:47:36 +00:00
|
|
|
#include "clipboard.h"
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "guac_handlers.h"
|
|
|
|
#include "ssh_client.h"
|
|
|
|
#include "terminal.h"
|
|
|
|
|
2014-06-02 23:01:13 +00:00
|
|
|
#include <langinfo.h>
|
|
|
|
#include <locale.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <pthread.h>
|
2011-07-30 22:12:28 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-08-01 20:31:48 +00:00
|
|
|
|
2011-07-30 22:12:28 +00:00
|
|
|
#include <guacamole/client.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <guacamole/socket.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2013-05-26 06:50:13 +00:00
|
|
|
#define GUAC_SSH_DEFAULT_FONT_NAME "monospace"
|
|
|
|
#define GUAC_SSH_DEFAULT_FONT_SIZE 12
|
2013-12-02 00:26:41 +00:00
|
|
|
#define GUAC_SSH_DEFAULT_PORT "22"
|
2013-05-26 06:50:13 +00:00
|
|
|
|
2011-08-01 03:51:19 +00:00
|
|
|
/* Client plugin arguments */
|
|
|
|
const char* GUAC_CLIENT_ARGS[] = {
|
|
|
|
"hostname",
|
2013-05-26 06:15:55 +00:00
|
|
|
"port",
|
2013-05-10 04:43:46 +00:00
|
|
|
"username",
|
2011-08-01 20:31:48 +00:00
|
|
|
"password",
|
2013-05-26 06:50:13 +00:00
|
|
|
"font-name",
|
|
|
|
"font-size",
|
2013-10-18 22:37:16 +00:00
|
|
|
"enable-sftp",
|
2013-10-30 22:46:13 +00:00
|
|
|
"private-key",
|
|
|
|
"passphrase",
|
2013-12-02 10:07:17 +00:00
|
|
|
#ifdef ENABLE_SSH_AGENT
|
|
|
|
"enable-agent",
|
|
|
|
#endif
|
2015-07-28 23:46:32 +00:00
|
|
|
"color-scheme",
|
2015-10-19 22:58:44 +00:00
|
|
|
"command",
|
2016-01-25 21:12:24 +00:00
|
|
|
"typescript-path",
|
|
|
|
"typescript-name",
|
|
|
|
"create-typescript-path",
|
2011-08-01 03:51:19 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2013-05-26 06:15:55 +00:00
|
|
|
enum __SSH_ARGS_IDX {
|
2013-05-26 06:50:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The hostname to connect to. Required.
|
|
|
|
*/
|
2013-05-26 06:15:55 +00:00
|
|
|
IDX_HOSTNAME,
|
2013-05-26 06:50:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The port to connect to. Optional.
|
|
|
|
*/
|
2013-05-26 06:15:55 +00:00
|
|
|
IDX_PORT,
|
2013-05-26 06:50:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the user to login as. Optional.
|
|
|
|
*/
|
2013-05-26 06:15:55 +00:00
|
|
|
IDX_USERNAME,
|
2013-05-26 06:50:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The password to use when logging in. Optional.
|
|
|
|
*/
|
2013-05-26 06:15:55 +00:00
|
|
|
IDX_PASSWORD,
|
2013-05-26 06:50:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
|
2013-10-18 22:37:16 +00:00
|
|
|
/**
|
|
|
|
* Whether SFTP should be enabled.
|
|
|
|
*/
|
|
|
|
IDX_ENABLE_SFTP,
|
|
|
|
|
2013-10-30 22:46:13 +00:00
|
|
|
/**
|
|
|
|
* The private key to use for authentication, if any.
|
|
|
|
*/
|
|
|
|
IDX_PRIVATE_KEY,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The passphrase required to decrypt the private key, if any.
|
|
|
|
*/
|
|
|
|
IDX_PASSPHRASE,
|
|
|
|
|
2013-12-02 10:07:17 +00:00
|
|
|
#ifdef ENABLE_SSH_AGENT
|
|
|
|
/**
|
|
|
|
* Whether SSH agent forwarding support should be enabled.
|
|
|
|
*/
|
|
|
|
IDX_ENABLE_AGENT,
|
|
|
|
#endif
|
|
|
|
|
2015-07-28 23:46:32 +00:00
|
|
|
/**
|
|
|
|
* The name of the color scheme to use. Currently valid color schemes are:
|
|
|
|
* "black-white", "white-black", "gray-black", and "green-black", each
|
|
|
|
* following the "foreground-background" pattern. By default, this will be
|
|
|
|
* "gray-black".
|
|
|
|
*/
|
|
|
|
IDX_COLOR_SCHEME,
|
|
|
|
|
2015-10-19 22:58:44 +00:00
|
|
|
/**
|
|
|
|
* The command to run instead if the default shell. If omitted, a normal
|
|
|
|
* shell session will be created.
|
|
|
|
*/
|
|
|
|
IDX_COMMAND,
|
|
|
|
|
2016-01-25 21:12:24 +00:00
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
|
2013-05-26 06:15:55 +00:00
|
|
|
SSH_ARGS_COUNT
|
|
|
|
};
|
|
|
|
|
2011-07-30 22:12:28 +00:00
|
|
|
int guac_client_init(guac_client* client, int argc, char** argv) {
|
|
|
|
|
2011-11-26 23:35:45 +00:00
|
|
|
guac_socket* socket = client->socket;
|
2011-08-01 03:51:19 +00:00
|
|
|
|
2015-07-24 20:20:36 +00:00
|
|
|
ssh_guac_client_data* client_data = calloc(1, sizeof(ssh_guac_client_data));
|
2011-08-01 03:51:19 +00:00
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
/* Init client data */
|
2011-08-01 03:51:19 +00:00
|
|
|
client->data = client_data;
|
|
|
|
|
2013-05-26 06:15:55 +00:00
|
|
|
if (argc != SSH_ARGS_COUNT) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Wrong number of arguments");
|
2013-05-20 08:23:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-02 23:01:13 +00:00
|
|
|
/* Set locale and warn if not UTF-8 */
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0)
|
2014-11-08 00:32:19 +00:00
|
|
|
guac_client_log(client, GUAC_LOG_INFO, "Current locale does not use UTF-8. Some characters may not render correctly.");
|
2014-06-02 23:01:13 +00:00
|
|
|
|
2013-05-20 08:23:21 +00:00
|
|
|
/* Read parameters */
|
2013-05-26 06:50:13 +00:00
|
|
|
strcpy(client_data->hostname, argv[IDX_HOSTNAME]);
|
|
|
|
strcpy(client_data->username, argv[IDX_USERNAME]);
|
|
|
|
strcpy(client_data->password, argv[IDX_PASSWORD]);
|
|
|
|
|
2013-10-31 06:19:11 +00:00
|
|
|
/* Init public key auth information */
|
|
|
|
strcpy(client_data->key_base64, argv[IDX_PRIVATE_KEY]);
|
|
|
|
strcpy(client_data->key_passphrase, argv[IDX_PASSPHRASE]);
|
2013-10-30 22:46:13 +00:00
|
|
|
|
2013-05-26 06:50:13 +00:00
|
|
|
/* Read font name */
|
|
|
|
if (argv[IDX_FONT_NAME][0] != 0)
|
|
|
|
strcpy(client_data->font_name, argv[IDX_FONT_NAME]);
|
|
|
|
else
|
|
|
|
strcpy(client_data->font_name, GUAC_SSH_DEFAULT_FONT_NAME );
|
|
|
|
|
|
|
|
/* Read font size */
|
|
|
|
if (argv[IDX_FONT_SIZE][0] != 0)
|
|
|
|
client_data->font_size = atoi(argv[IDX_FONT_SIZE]);
|
|
|
|
else
|
|
|
|
client_data->font_size = GUAC_SSH_DEFAULT_FONT_SIZE;
|
|
|
|
|
2013-10-18 22:37:16 +00:00
|
|
|
/* Parse SFTP enable */
|
|
|
|
client_data->enable_sftp = strcmp(argv[IDX_ENABLE_SFTP], "true") == 0;
|
|
|
|
|
2013-12-02 10:07:17 +00:00
|
|
|
#ifdef ENABLE_SSH_AGENT
|
|
|
|
client_data->enable_agent = strcmp(argv[IDX_ENABLE_AGENT], "true") == 0;
|
|
|
|
#endif
|
|
|
|
|
2013-05-26 06:50:13 +00:00
|
|
|
/* Read port */
|
|
|
|
if (argv[IDX_PORT][0] != 0)
|
2013-12-02 00:26:41 +00:00
|
|
|
strcpy(client_data->port, argv[IDX_PORT]);
|
2013-05-26 06:50:13 +00:00
|
|
|
else
|
2013-12-02 00:26:41 +00:00
|
|
|
strcpy(client_data->port, GUAC_SSH_DEFAULT_PORT);
|
2013-05-26 06:50:13 +00:00
|
|
|
|
2015-10-19 22:58:44 +00:00
|
|
|
/* Read command, if any */
|
|
|
|
if (argv[IDX_COMMAND][0] != 0)
|
|
|
|
client_data->command = strdup(argv[IDX_COMMAND]);
|
|
|
|
|
2013-05-26 06:50:13 +00:00
|
|
|
/* Create terminal */
|
|
|
|
client_data->term = guac_terminal_create(client,
|
|
|
|
client_data->font_name, client_data->font_size,
|
2013-12-27 08:28:23 +00:00
|
|
|
client->info.optimal_resolution,
|
2015-07-28 22:59:20 +00:00
|
|
|
client->info.optimal_width, client->info.optimal_height,
|
2015-07-28 23:46:32 +00:00
|
|
|
argv[IDX_COLOR_SCHEME]);
|
2013-05-20 08:23:21 +00:00
|
|
|
|
2013-08-23 21:10:51 +00:00
|
|
|
/* Fail if terminal init failed */
|
|
|
|
if (client_data->term == NULL) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Terminal initialization failed");
|
2013-08-23 21:10:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-27 20:40:35 +00:00
|
|
|
/* Set up typescript, if requested */
|
|
|
|
const char* typescript_path = argv[IDX_TYPESCRIPT_PATH];
|
|
|
|
if (typescript_path[0] != 0) {
|
|
|
|
|
|
|
|
/* Default to "typescript" if no name provided */
|
|
|
|
const char* typescript_name = argv[IDX_TYPESCRIPT_NAME];
|
|
|
|
if (typescript_name[0] == 0)
|
|
|
|
typescript_name = "typescript";
|
|
|
|
|
|
|
|
/* Parse path creation flag */
|
|
|
|
int create_path =
|
|
|
|
strcmp(argv[IDX_CREATE_TYPESCRIPT_PATH], "true") == 0;
|
|
|
|
|
|
|
|
/* Create typescript */
|
|
|
|
guac_terminal_create_typescript(client_data->term, typescript_path,
|
|
|
|
typescript_name, create_path);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-10 23:49:13 +00:00
|
|
|
/* Ensure main socket is threadsafe */
|
|
|
|
guac_socket_require_threadsafe(socket);
|
|
|
|
|
2013-05-26 06:15:55 +00:00
|
|
|
/* Send initial name */
|
|
|
|
guac_protocol_send_name(socket, client_data->hostname);
|
2011-08-01 03:51:19 +00:00
|
|
|
|
2011-11-26 23:35:45 +00:00
|
|
|
guac_socket_flush(socket);
|
2011-08-01 03:51:19 +00:00
|
|
|
|
2013-05-18 03:20:51 +00:00
|
|
|
/* Set basic handlers */
|
|
|
|
client->handle_messages = ssh_guac_client_handle_messages;
|
|
|
|
client->key_handler = ssh_guac_client_key_handler;
|
|
|
|
client->mouse_handler = ssh_guac_client_mouse_handler;
|
|
|
|
client->size_handler = ssh_guac_client_size_handler;
|
|
|
|
client->free_handler = ssh_guac_client_free_handler;
|
2014-04-10 20:47:36 +00:00
|
|
|
client->clipboard_handler = guac_ssh_clipboard_handler;
|
2011-08-01 03:51:19 +00:00
|
|
|
|
2013-05-18 03:58:47 +00:00
|
|
|
/* Start client thread */
|
|
|
|
if (pthread_create(&(client_data->client_thread), NULL, ssh_client_thread, (void*) client)) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Unable to start SSH client thread");
|
2013-05-18 03:47:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-30 22:12:28 +00:00
|
|
|
/* Success */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|