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 "blank.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "guac_handlers.h"
|
|
|
|
#include "ibar.h"
|
|
|
|
#include "ssh_client.h"
|
|
|
|
#include "terminal.h"
|
|
|
|
|
|
|
|
#include <pthread.h>
|
2011-07-30 22:12:28 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-05-18 03:20:51 +00:00
|
|
|
#include <unistd.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
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
ssh_guac_client_data* client_data = malloc(sizeof(ssh_guac_client_data));
|
|
|
|
|
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 07:17:31 +00:00
|
|
|
client_data->mod_alt =
|
|
|
|
client_data->mod_ctrl =
|
|
|
|
client_data->mod_shift = 0;
|
2014-04-09 18:40:09 +00:00
|
|
|
client_data->clipboard = guac_common_clipboard_alloc(GUAC_SSH_CLIPBOARD_MAX_LENGTH);
|
2013-05-20 17:27:53 +00:00
|
|
|
client_data->term_channel = NULL;
|
2011-08-01 03:51:19 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
2013-10-30 22:46:13 +00:00
|
|
|
client_data->key = NULL;
|
2013-10-31 06:19:11 +00:00
|
|
|
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;
|
|
|
|
client_data->sftp_session = NULL;
|
2013-10-19 05:05:03 +00:00
|
|
|
client_data->sftp_ssh_session = NULL;
|
2013-10-28 21:11:04 +00:00
|
|
|
strcpy(client_data->sftp_upload_path, ".");
|
2013-10-18 22:37:16 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
/* 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,
|
2013-05-26 06:50:13 +00:00
|
|
|
client->info.optimal_width, client->info.optimal_height);
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-09 21:02:52 +00:00
|
|
|
/* Set up I-bar pointer */
|
2013-04-09 21:09:41 +00:00
|
|
|
client_data->ibar_cursor = guac_ssh_create_ibar(client);
|
2013-04-09 20:54:23 +00:00
|
|
|
|
2013-04-09 21:02:52 +00:00
|
|
|
/* Set up blank pointer */
|
|
|
|
client_data->blank_cursor = guac_ssh_create_blank(client);
|
|
|
|
|
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
|
|
|
|
2013-04-09 21:09:41 +00:00
|
|
|
/* Initialize pointer */
|
|
|
|
client_data->current_cursor = client_data->blank_cursor;
|
|
|
|
guac_ssh_set_cursor(client, client_data->current_cursor);
|
2013-03-29 10:14:08 +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;
|
2011-12-30 22:34:04 +00:00
|
|
|
client->clipboard_handler = ssh_guac_client_clipboard_handler;
|
2013-05-18 03:20:51 +00:00
|
|
|
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;
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|