guacamole-spice-protocol/src/protocols/ssh/client.c

211 lines
5.9 KiB
C
Raw Normal View History

/*
* Copyright (C) 2013 Glyptodon LLC
2011-07-30 22:12:28 +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
*
* 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
*
* 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"
#include "clipboard.h"
2014-01-01 22:44:28 +00:00
#include "guac_handlers.h"
#include "ssh_client.h"
#include "terminal.h"
#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-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>
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",
"port",
"username",
"password",
2013-05-26 06:50:13 +00:00
"font-name",
"font-size",
"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
};
enum __SSH_ARGS_IDX {
2013-05-26 06:50:13 +00:00
/**
* The hostname to connect to. Required.
*/
IDX_HOSTNAME,
2013-05-26 06:50:13 +00:00
/**
* The port to connect to. Optional.
*/
IDX_PORT,
2013-05-26 06:50:13 +00:00
/**
* The name of the user to login as. Optional.
*/
IDX_USERNAME,
2013-05-26 06:50:13 +00:00
/**
* The password to use when logging in. Optional.
*/
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,
/**
* 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
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 = calloc(1, sizeof(ssh_guac_client_data));
2011-08-01 03:51:19 +00:00
/* Init client data */
2011-08-01 03:51:19 +00:00
client->data = client_data;
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;
}
/* 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.");
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;
/* 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
/* Create terminal */
client_data->term = guac_terminal_create(client,
client_data->font_name, client_data->font_size,
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
/* 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");
return -1;
}
/* Ensure main socket is threadsafe */
guac_socket_require_threadsafe(socket);
/* 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
/* 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;
client->clipboard_handler = guac_ssh_clipboard_handler;
2011-08-01 03:51:19 +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;
}