2013-12-29 04:53:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Glyptodon LLC
|
2011-08-04 18:46:21 +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-08-04 18:46:21 +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-08-04 18:46:21 +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-08-04 18:46:21 +00:00
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "client.h"
|
2014-04-09 18:40:09 +00:00
|
|
|
#include "clipboard.h"
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "cursor.h"
|
|
|
|
#include "guac_handlers.h"
|
2011-08-10 07:02:06 +00:00
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <fcntl.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/stat.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
#include <cairo/cairo.h>
|
2011-11-26 23:35:45 +00:00
|
|
|
#include <guacamole/socket.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <guacamole/client.h>
|
2013-12-01 23:39:29 +00:00
|
|
|
#include <libssh2.h>
|
2014-01-01 22:44:28 +00:00
|
|
|
#include <pango/pangocairo.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
int ssh_guac_client_handle_messages(guac_client* client) {
|
|
|
|
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
|
|
|
char buffer[8192];
|
|
|
|
|
2013-05-18 03:20:51 +00:00
|
|
|
int ret_val;
|
2013-05-22 05:02:11 +00:00
|
|
|
int fd = client_data->term->stdout_pipe_fd[0];
|
2011-08-04 18:46:21 +00:00
|
|
|
struct timeval timeout;
|
2011-08-10 07:02:06 +00:00
|
|
|
fd_set fds;
|
|
|
|
|
|
|
|
/* Build fd_set */
|
|
|
|
FD_ZERO(&fds);
|
2013-05-18 03:20:51 +00:00
|
|
|
FD_SET(fd, &fds);
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
/* Time to wait */
|
2011-11-26 23:35:45 +00:00
|
|
|
timeout.tv_sec = 1;
|
|
|
|
timeout.tv_usec = 0;
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
/* Wait for data to be available */
|
2013-05-18 03:20:51 +00:00
|
|
|
ret_val = select(fd+1, &fds, NULL, NULL, &timeout);
|
|
|
|
if (ret_val > 0) {
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
int bytes_read = 0;
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
guac_terminal_lock(client_data->term);
|
2013-04-07 23:55:06 +00:00
|
|
|
|
2013-05-18 03:20:51 +00:00
|
|
|
/* Read data, write to terminal */
|
|
|
|
if ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) {
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2014-03-22 02:47:42 +00:00
|
|
|
if (guac_terminal_write(client_data->term, buffer, bytes_read)) {
|
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Error writing data");
|
2011-08-10 01:32:54 +00:00
|
|
|
return 1;
|
2014-03-22 02:47:42 +00:00
|
|
|
}
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify on error */
|
|
|
|
if (bytes_read < 0) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Error reading data");
|
2011-08-04 18:46:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* Flush terminal */
|
|
|
|
guac_terminal_flush(client_data->term);
|
|
|
|
guac_terminal_unlock(client_data->term);
|
2013-04-07 23:55:06 +00:00
|
|
|
|
2013-03-29 09:51:31 +00:00
|
|
|
}
|
2013-05-18 03:20:51 +00:00
|
|
|
else if (ret_val < 0) {
|
2014-03-22 02:47:42 +00:00
|
|
|
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Error waiting for data");
|
2013-05-18 03:20:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2013-03-27 11:11:56 +00:00
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-30 22:34:04 +00:00
|
|
|
int ssh_guac_client_mouse_handler(guac_client* client, int x, int y, int mask) {
|
|
|
|
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
2013-04-05 08:32:33 +00:00
|
|
|
guac_terminal* term = client_data->term;
|
2011-12-30 22:34:04 +00:00
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* Send mouse event */
|
|
|
|
guac_terminal_lock(term);
|
|
|
|
guac_terminal_send_mouse(term, x, y, mask);
|
|
|
|
guac_terminal_unlock(term);
|
2011-12-30 22:34:04 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-04-01 08:59:15 +00:00
|
|
|
|
2011-12-30 22:34:04 +00:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
int ssh_guac_client_key_handler(guac_client* client, int keysym, int pressed) {
|
|
|
|
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
2013-04-08 07:47:08 +00:00
|
|
|
guac_terminal* term = client_data->term;
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* Send key */
|
|
|
|
guac_terminal_lock(term);
|
|
|
|
guac_terminal_send_key(term, keysym, pressed);
|
|
|
|
guac_terminal_unlock(term);
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-01 23:54:29 +00:00
|
|
|
int ssh_guac_client_size_handler(guac_client* client, int width, int height) {
|
|
|
|
|
|
|
|
/* Get terminal */
|
|
|
|
ssh_guac_client_data* guac_client_data = (ssh_guac_client_data*) client->data;
|
|
|
|
guac_terminal* terminal = guac_client_data->term;
|
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* Resize terminal */
|
|
|
|
guac_terminal_lock(terminal);
|
|
|
|
guac_terminal_resize(terminal, width, height);
|
|
|
|
guac_terminal_unlock(terminal);
|
2013-05-01 23:54:29 +00:00
|
|
|
|
2014-05-06 01:58:53 +00:00
|
|
|
/* Update SSH pty size if connected */
|
|
|
|
if (guac_client_data->term_channel != NULL)
|
|
|
|
libssh2_channel_request_pty_size(guac_client_data->term_channel,
|
|
|
|
terminal->term_width, terminal->term_height);
|
2013-05-01 23:54:29 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-30 22:34:04 +00:00
|
|
|
int ssh_guac_client_free_handler(guac_client* client) {
|
|
|
|
|
|
|
|
ssh_guac_client_data* guac_client_data = (ssh_guac_client_data*) client->data;
|
|
|
|
|
2013-05-26 06:05:58 +00:00
|
|
|
/* Close SSH channel */
|
2013-05-26 06:50:13 +00:00
|
|
|
if (guac_client_data->term_channel != NULL) {
|
2013-12-01 23:39:29 +00:00
|
|
|
libssh2_channel_send_eof(guac_client_data->term_channel);
|
|
|
|
libssh2_channel_close(guac_client_data->term_channel);
|
2013-05-26 06:50:13 +00:00
|
|
|
}
|
2013-05-26 06:05:58 +00:00
|
|
|
|
2011-12-30 22:34:04 +00:00
|
|
|
/* Free terminal */
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal_free(guac_client_data->term);
|
2013-05-22 05:02:11 +00:00
|
|
|
pthread_join(guac_client_data->client_thread, NULL);
|
2011-12-30 22:34:04 +00:00
|
|
|
|
2013-05-26 06:05:58 +00:00
|
|
|
/* Free channels */
|
2013-12-01 23:39:29 +00:00
|
|
|
libssh2_channel_free(guac_client_data->term_channel);
|
2013-05-26 06:05:58 +00:00
|
|
|
|
2013-10-18 22:37:16 +00:00
|
|
|
/* Clean up SFTP */
|
|
|
|
if (guac_client_data->sftp_session)
|
2013-12-01 23:39:29 +00:00
|
|
|
libssh2_sftp_shutdown(guac_client_data->sftp_session);
|
2013-10-18 22:37:16 +00:00
|
|
|
|
2013-12-01 23:39:29 +00:00
|
|
|
if (guac_client_data->sftp_ssh_session) {
|
|
|
|
libssh2_session_disconnect(guac_client_data->sftp_ssh_session, "Bye");
|
|
|
|
libssh2_session_free(guac_client_data->sftp_ssh_session);
|
|
|
|
}
|
2013-10-19 05:05:03 +00:00
|
|
|
|
2013-05-26 06:05:58 +00:00
|
|
|
/* Free session */
|
2013-12-02 00:09:48 +00:00
|
|
|
if (guac_client_data->session != NULL)
|
|
|
|
libssh2_session_free(guac_client_data->session);
|
2013-05-26 06:05:58 +00:00
|
|
|
|
2013-10-30 22:46:13 +00:00
|
|
|
/* Free auth key */
|
|
|
|
if (guac_client_data->key != NULL)
|
|
|
|
ssh_key_free(guac_client_data->key);
|
|
|
|
|
2011-12-30 22:34:04 +00:00
|
|
|
/* Free generic data struct */
|
|
|
|
free(client->data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|