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

477 lines
14 KiB
C
Raw Normal View History

/*
* Copyright (C) 2013 Glyptodon LLC
2011-02-16 02:47:51 +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-02-16 02:47:51 +00:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2011-02-16 02:47:51 +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"
#include "client.h"
#include "clipboard.h"
#include "guac_clipboard.h"
2014-01-07 00:18:54 +00:00
#include "guac_dot_cursor.h"
2014-01-01 22:44:28 +00:00
#include "guac_handlers.h"
2014-01-07 00:18:54 +00:00
#include "guac_pointer_cursor.h"
2014-01-01 22:44:28 +00:00
#include "vnc_handlers.h"
2010-12-08 21:14:04 +00:00
2014-06-11 18:15:35 +00:00
#ifdef ENABLE_PULSE
#include "pulse.h"
#endif
2010-12-08 21:14:04 +00:00
#ifdef ENABLE_COMMON_SSH
#include "guac_sftp.h"
#include "guac_ssh.h"
#include "sftp.h"
#endif
2010-12-08 21:14:04 +00:00
#include <rfb/rfbclient.h>
2014-06-11 18:15:35 +00:00
#include <rfb/rfbproto.h>
2014-01-01 22:44:28 +00:00
#include <guacamole/client.h>
#include <guacamole/protocol.h>
#include <guacamole/socket.h>
2013-08-09 00:25:08 +00:00
2014-06-11 18:15:35 +00:00
#include <stdlib.h>
#include <string.h>
#include <time.h>
2011-12-22 02:35:42 +00:00
/* Client plugin arguments */
const char* GUAC_CLIENT_ARGS[] = {
"hostname",
"port",
2011-02-11 06:56:47 +00:00
"read-only",
2011-03-14 01:58:31 +00:00
"encodings",
"password",
"swap-red-blue",
2013-02-10 00:39:31 +00:00
"color-depth",
"cursor",
2013-08-21 20:43:47 +00:00
"autoretry",
#ifdef ENABLE_VNC_REPEATER
"dest-host",
"dest-port",
#endif
2013-08-09 00:25:08 +00:00
#ifdef ENABLE_PULSE
2013-08-10 01:21:32 +00:00
"enable-audio",
"audio-servername",
2013-08-09 00:25:08 +00:00
#endif
#ifdef ENABLE_VNC_LISTEN
2013-09-19 21:41:28 +00:00
"reverse-connect",
"listen-timeout",
#endif
#ifdef ENABLE_COMMON_SSH
"enable-sftp",
"sftp-hostname",
"sftp-port",
"sftp-username",
"sftp-password",
"sftp-private-key",
"sftp-passphrase",
#endif
NULL
};
enum VNC_ARGS_IDX {
IDX_HOSTNAME,
IDX_PORT,
IDX_READ_ONLY,
IDX_ENCODINGS,
IDX_PASSWORD,
IDX_SWAP_RED_BLUE,
IDX_COLOR_DEPTH,
IDX_CURSOR,
2013-08-21 20:43:47 +00:00
IDX_AUTORETRY,
#ifdef ENABLE_VNC_REPEATER
IDX_DEST_HOST,
IDX_DEST_PORT,
#endif
2013-08-09 00:25:08 +00:00
#ifdef ENABLE_PULSE
2013-08-10 01:21:32 +00:00
IDX_ENABLE_AUDIO,
IDX_AUDIO_SERVERNAME,
2013-08-09 00:25:08 +00:00
#endif
#ifdef ENABLE_VNC_LISTEN
2013-09-19 21:41:28 +00:00
IDX_REVERSE_CONNECT,
IDX_LISTEN_TIMEOUT,
#endif
#ifdef ENABLE_COMMON_SSH
IDX_ENABLE_SFTP,
IDX_SFTP_HOSTNAME,
IDX_SFTP_PORT,
IDX_SFTP_USERNAME,
IDX_SFTP_PASSWORD,
IDX_SFTP_PRIVATE_KEY,
IDX_SFTP_PASSPHRASE,
#endif
VNC_ARGS_COUNT
};
2011-12-22 02:35:42 +00:00
char* __GUAC_CLIENT = "GUAC_CLIENT";
2011-12-22 02:18:25 +00:00
2013-08-21 20:43:47 +00:00
/**
* Allocates a new rfbClient instance given the parameters stored within the
* client, returning NULL on failure.
*/
static rfbClient* __guac_vnc_get_client(guac_client* client) {
rfbClient* rfb_client = rfbGetClient(8, 3, 4); /* 32-bpp client */
vnc_guac_client_data* guac_client_data =
(vnc_guac_client_data*) client->data;
/* Store Guac client in rfb client */
rfbClientSetClientData(rfb_client, __GUAC_CLIENT, client);
/* Framebuffer update handler */
rfb_client->GotFrameBufferUpdate = guac_vnc_update;
rfb_client->GotCopyRect = guac_vnc_copyrect;
/* Do not handle clipboard and local cursor if read-only */
if (guac_client_data->read_only == 0) {
/* Clipboard */
rfb_client->GotXCutText = guac_vnc_cut_text;
/* Set remote cursor */
if (guac_client_data->remote_cursor)
rfb_client->appData.useRemoteCursor = FALSE;
else {
/* Enable client-side cursor */
rfb_client->appData.useRemoteCursor = TRUE;
rfb_client->GotCursorShape = guac_vnc_cursor;
}
}
/* Password */
rfb_client->GetPassword = guac_vnc_get_password;
/* Depth */
guac_vnc_set_pixel_format(rfb_client, guac_client_data->color_depth);
/* Hook into allocation so we can handle resize. */
guac_client_data->rfb_MallocFrameBuffer = rfb_client->MallocFrameBuffer;
rfb_client->MallocFrameBuffer = guac_vnc_malloc_framebuffer;
rfb_client->canHandleNewFBSize = 1;
/* Set hostname and port */
rfb_client->serverHost = strdup(guac_client_data->hostname);
rfb_client->serverPort = guac_client_data->port;
#ifdef ENABLE_VNC_REPEATER
/* Set repeater parameters if specified */
if (guac_client_data->dest_host) {
rfb_client->destHost = strdup(guac_client_data->dest_host);
rfb_client->destPort = guac_client_data->dest_port;
}
#endif
#ifdef ENABLE_VNC_LISTEN
2013-09-19 21:41:28 +00:00
/* If reverse connection enabled, start listening */
if (guac_client_data->reverse_connect) {
guac_client_log(client, GUAC_LOG_INFO, "Listening for connections on port %i",
2013-09-19 21:41:28 +00:00
guac_client_data->port);
/* Listen for connection from server */
rfb_client->listenPort = guac_client_data->port;
if (listenForIncomingConnectionsNoFork(rfb_client,
guac_client_data->listen_timeout*1000) <= 0)
return NULL;
}
#endif
2013-09-19 21:41:28 +00:00
/* Set encodings if provided */
if (guac_client_data->encodings)
rfb_client->appData.encodingsString =
strdup(guac_client_data->encodings);
2013-08-21 20:43:47 +00:00
/* Connect */
if (rfbInitClient(rfb_client, NULL, NULL))
return rfb_client;
/* If connection fails, return NULL */
return NULL;
}
2010-12-08 21:14:04 +00:00
int guac_client_init(guac_client* client, int argc, char** argv) {
rfbClient* rfb_client;
vnc_guac_client_data* guac_client_data;
2013-08-21 20:43:47 +00:00
int retries_remaining;
2010-12-08 21:14:04 +00:00
2011-12-22 02:18:25 +00:00
/* Set up libvncclient logging */
2011-12-22 02:35:42 +00:00
rfbClientLog = guac_vnc_client_log_info;
rfbClientErr = guac_vnc_client_log_error;
2011-03-20 07:22:22 +00:00
2011-02-11 06:56:47 +00:00
/*** PARSE ARGUMENTS ***/
2010-12-08 21:14:04 +00:00
if (argc != VNC_ARGS_COUNT) {
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Wrong argument count received.");
2010-12-08 21:14:04 +00:00
return 1;
}
/* Alloc client data */
guac_client_data = malloc(sizeof(vnc_guac_client_data));
client->data = guac_client_data;
2013-08-21 20:43:47 +00:00
guac_client_data->hostname = strdup(argv[IDX_HOSTNAME]);
guac_client_data->port = atoi(argv[IDX_PORT]);
guac_client_data->password = strdup(argv[IDX_PASSWORD]); /* NOTE: freed by libvncclient */
guac_client_data->default_surface = NULL;
/* Set flags */
2013-08-21 20:43:47 +00:00
guac_client_data->remote_cursor = (strcmp(argv[IDX_CURSOR], "remote") == 0);
guac_client_data->swap_red_blue = (strcmp(argv[IDX_SWAP_RED_BLUE], "true") == 0);
guac_client_data->read_only = (strcmp(argv[IDX_READ_ONLY], "true") == 0);
2010-12-08 21:14:04 +00:00
2013-08-28 23:16:55 +00:00
/* Parse color depth */
guac_client_data->color_depth = atoi(argv[IDX_COLOR_DEPTH]);
2013-08-21 20:43:47 +00:00
#ifdef ENABLE_VNC_REPEATER
/* Set repeater parameters if specified */
if (argv[IDX_DEST_HOST][0] != '\0')
guac_client_data->dest_host = strdup(argv[IDX_DEST_HOST]);
else
guac_client_data->dest_host = NULL;
2011-02-11 06:56:47 +00:00
2013-08-21 20:43:47 +00:00
if (argv[IDX_DEST_PORT][0] != '\0')
guac_client_data->dest_port = atoi(argv[IDX_DEST_PORT]);
#endif
2011-02-11 06:56:47 +00:00
2013-08-21 20:43:47 +00:00
/* Set encodings if specified */
if (argv[IDX_ENCODINGS][0] != '\0')
guac_client_data->encodings = strdup(argv[IDX_ENCODINGS]);
else
guac_client_data->encodings = NULL;
2013-08-21 20:43:47 +00:00
/* Parse autoretry */
if (argv[IDX_AUTORETRY][0] != '\0')
retries_remaining = atoi(argv[IDX_AUTORETRY]);
else
retries_remaining = 0;
2011-02-11 06:56:47 +00:00
#ifdef ENABLE_VNC_LISTEN
2013-09-19 21:41:28 +00:00
/* Set reverse-connection flag */
guac_client_data->reverse_connect =
(strcmp(argv[IDX_REVERSE_CONNECT], "true") == 0);
/* Parse listen timeout */
if (argv[IDX_LISTEN_TIMEOUT][0] != '\0')
guac_client_data->listen_timeout = atoi(argv[IDX_LISTEN_TIMEOUT]);
else
guac_client_data->listen_timeout = 5000;
#endif
2013-09-19 21:41:28 +00:00
/* Init clipboard */
guac_client_data->clipboard = guac_common_clipboard_alloc(GUAC_VNC_CLIPBOARD_MAX_LENGTH);
/* Ensure connection is kept alive during lengthy connects */
guac_socket_require_keep_alive(client->socket);
2013-08-21 20:43:47 +00:00
/* Attempt connection */
rfb_client = __guac_vnc_get_client(client);
2011-02-11 06:56:47 +00:00
2013-08-21 20:43:47 +00:00
/* If unsuccessful, retry as many times as specified */
while (!rfb_client && retries_remaining > 0) {
2011-02-11 06:56:47 +00:00
struct timespec guac_vnc_connect_interval = {
.tv_sec = GUAC_VNC_CONNECT_INTERVAL/1000,
.tv_nsec = (GUAC_VNC_CONNECT_INTERVAL%1000)*1000000
};
guac_client_log(client, GUAC_LOG_INFO,
2013-08-21 20:43:47 +00:00
"Connect failed. Waiting %ims before retrying...",
GUAC_VNC_CONNECT_INTERVAL);
2013-08-21 20:43:47 +00:00
/* Wait for given interval then retry */
nanosleep(&guac_vnc_connect_interval, NULL);
2013-08-21 20:43:47 +00:00
rfb_client = __guac_vnc_get_client(client);
retries_remaining--;
2013-02-10 00:39:31 +00:00
2013-08-21 20:43:47 +00:00
}
/* If the final connect attempt fails, return error */
if (!rfb_client) {
guac_client_abort(client, GUAC_PROTOCOL_STATUS_UPSTREAM_ERROR, "Unable to connect to VNC server.");
2013-08-21 20:43:47 +00:00
return 1;
}
2013-08-09 00:25:08 +00:00
#ifdef ENABLE_PULSE
2013-08-10 01:21:32 +00:00
guac_client_data->audio_enabled =
(strcmp(argv[IDX_ENABLE_AUDIO], "true") == 0);
/* If an encoding is available, load an audio stream */
if (guac_client_data->audio_enabled) {
guac_client_data->audio = guac_audio_stream_alloc(client, NULL);
2013-08-10 01:21:32 +00:00
/* Load servername if specified */
if (argv[IDX_AUDIO_SERVERNAME][0] != '\0')
guac_client_data->pa_servername =
strdup(argv[IDX_AUDIO_SERVERNAME]);
else
guac_client_data->pa_servername = NULL;
/* If successful, init audio system */
if (guac_client_data->audio != NULL) {
guac_client_log(client, GUAC_LOG_INFO,
"Audio will be encoded as %s",
guac_client_data->audio->encoder->mimetype);
2013-08-09 22:11:31 +00:00
/* Require threadsafe sockets if audio enabled */
guac_socket_require_threadsafe(client->socket);
/* Start audio stream */
guac_pa_start_stream(client);
}
/* Otherwise, audio loading failed */
else
guac_client_log(client, GUAC_LOG_INFO,
"No available audio encoding. Sound disabled.");
} /* end if audio enabled */
2013-08-09 00:25:08 +00:00
#endif
#ifdef ENABLE_COMMON_SSH
guac_common_ssh_init(client);
/* Connect via SSH if SFTP is enabled */
if (strcmp(argv[IDX_ENABLE_SFTP], "true") == 0) {
guac_client_log(client, GUAC_LOG_DEBUG,
"Connecting via SSH for SFTP filesystem access.");
guac_common_ssh_user* user =
guac_common_ssh_create_user(argv[IDX_SFTP_USERNAME]);
/* Import private key, if given */
if (argv[IDX_SFTP_PRIVATE_KEY][0] != '\0') {
guac_client_log(client, GUAC_LOG_DEBUG,
"Authenticating with private key.");
/* Abort if private key cannot be read */
if (guac_common_ssh_user_import_key(user,
argv[IDX_SFTP_PRIVATE_KEY],
argv[IDX_SFTP_PASSPHRASE])) {
guac_common_ssh_destroy_user(user);
return 1;
}
}
/* Otherwise, use specified password */
else {
guac_client_log(client, GUAC_LOG_DEBUG,
"Authenticating with password.");
guac_common_ssh_user_set_password(user, argv[IDX_SFTP_PASSWORD]);
}
/* Parse hostname - use VNC hostname by default */
const char* sftp_hostname = argv[IDX_SFTP_HOSTNAME];
if (sftp_hostname[0] == '\0')
sftp_hostname = guac_client_data->hostname;
/* Parse port, defaulting to standard SSH port */
const char* sftp_port = argv[IDX_SFTP_PORT];
if (sftp_port[0] == '\0')
sftp_port = "22";
/* Attempt SSH connection */
guac_common_ssh_session* session =
guac_common_ssh_create_session(client, sftp_hostname, sftp_port,
user);
/* Fail if SSH connection does not succeed */
if (session == NULL) {
/* Already aborted within guac_common_ssh_create_session() */
guac_common_ssh_destroy_user(user);
return 1;
}
/* Load and expose filesystem */
guac_client_data->sftp_filesystem =
guac_common_ssh_create_sftp_filesystem(session, "/");
/* Abort if SFTP connection fails */
if (guac_client_data->sftp_filesystem == NULL)
return 1;
/* Set file handler for basic uploads */
client->file_handler = guac_vnc_sftp_file_handler;
guac_client_log(client, GUAC_LOG_DEBUG,
"SFTP connection succeeded.");
}
#endif
2010-12-08 21:14:04 +00:00
/* Set remaining client data */
guac_client_data->rfb_client = rfb_client;
guac_client_data->copy_rect_used = 0;
guac_client_data->cursor = guac_client_alloc_buffer(client);
2010-12-08 21:14:04 +00:00
/* Set handlers */
client->handle_messages = vnc_guac_client_handle_messages;
client->free_handler = vnc_guac_client_free_handler;
2013-08-21 20:43:47 +00:00
/* If not read-only, set input handlers and pointer */
if (guac_client_data->read_only == 0) {
/* Only handle mouse/keyboard/clipboard if not read-only */
2011-02-11 06:56:47 +00:00
client->mouse_handler = vnc_guac_client_mouse_handler;
client->key_handler = vnc_guac_client_key_handler;
client->clipboard_handler = guac_vnc_clipboard_handler;
2013-08-21 20:43:47 +00:00
2014-01-02 19:41:32 +00:00
/* If not read-only but cursor is remote, set a dot cursor */
2013-08-21 20:43:47 +00:00
if (guac_client_data->remote_cursor)
2014-01-07 00:18:54 +00:00
guac_common_set_dot_cursor(client);
2013-08-21 20:43:47 +00:00
/* Otherwise, set pointer until explicitly requested otherwise */
else
2014-01-07 00:18:54 +00:00
guac_common_set_pointer_cursor(client);
2011-02-11 06:56:47 +00:00
}
2010-12-08 21:14:04 +00:00
/* Send name */
guac_protocol_send_name(client->socket, rfb_client->desktopName);
2010-12-08 21:14:04 +00:00
/* Create default surface */
guac_client_data->default_surface = guac_common_surface_alloc(client->socket, GUAC_DEFAULT_LAYER,
rfb_client->width, rfb_client->height);
2010-12-08 21:14:04 +00:00
return 0;
}