2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is libguac-client-ssh.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Michael Jumper.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2011-12-30 22:34:04 +00:00
|
|
|
* James Muehlner <dagger10k@users.sourceforge.net>
|
2011-08-04 18:46:21 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-08-10 07:02:06 +00:00
|
|
|
#include <sys/select.h>
|
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <libssh/libssh.h>
|
|
|
|
|
|
|
|
#include <cairo/cairo.h>
|
|
|
|
#include <pango/pangocairo.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>
|
|
|
|
|
|
|
|
#include "ssh_handlers.h"
|
|
|
|
#include "ssh_client.h"
|
|
|
|
|
|
|
|
int ssh_guac_client_handle_messages(guac_client* client) {
|
|
|
|
|
2011-11-26 23:35:45 +00:00
|
|
|
guac_socket* socket = client->socket;
|
2011-08-04 18:46:21 +00:00
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
|
|
|
char buffer[8192];
|
|
|
|
|
2011-08-10 07:02:06 +00:00
|
|
|
ssh_channel channels[2], out_channels[2];
|
2011-08-04 18:46:21 +00:00
|
|
|
struct timeval timeout;
|
2011-08-10 07:02:06 +00:00
|
|
|
fd_set fds;
|
|
|
|
int ssh_fd;
|
2011-08-10 01:32:54 +00:00
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
/* Channels to read */
|
2011-08-10 07:02:06 +00:00
|
|
|
channels[0] = client_data->term_channel;
|
|
|
|
channels[1] = NULL;
|
|
|
|
|
|
|
|
/* Get SSH file descriptor */
|
|
|
|
ssh_fd = ssh_get_fd(client_data->session);
|
|
|
|
|
|
|
|
/* Build fd_set */
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(ssh_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 */
|
2011-08-10 07:02:06 +00:00
|
|
|
if (ssh_select(channels, out_channels, ssh_fd+1, &fds, &timeout)
|
|
|
|
== SSH_OK) {
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
int bytes_read = 0;
|
|
|
|
|
2013-03-29 09:51:31 +00:00
|
|
|
/* Clear cursor */
|
|
|
|
guac_terminal_toggle_reverse(client_data->term,
|
|
|
|
client_data->term->cursor_row,
|
|
|
|
client_data->term->cursor_col);
|
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
/* While data available, write to terminal */
|
|
|
|
while (channel_is_open(client_data->term_channel)
|
|
|
|
&& !channel_is_eof(client_data->term_channel)
|
|
|
|
&& (bytes_read = channel_read_nonblocking(client_data->term_channel, buffer, sizeof(buffer), 0)) > 0) {
|
|
|
|
|
2012-12-09 08:35:37 +00:00
|
|
|
if (guac_terminal_write(client_data->term, buffer, bytes_read)
|
2011-11-26 23:35:45 +00:00
|
|
|
|| guac_socket_flush(socket))
|
2011-08-10 01:32:54 +00:00
|
|
|
return 1;
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify on error */
|
|
|
|
if (bytes_read < 0) {
|
2011-11-26 23:35:45 +00:00
|
|
|
guac_protocol_send_error(socket, "Error reading data.");
|
|
|
|
guac_socket_flush(socket);
|
2011-08-04 18:46:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-03-29 09:51:31 +00:00
|
|
|
/* Draw cursor */
|
|
|
|
guac_terminal_toggle_reverse(client_data->term,
|
|
|
|
client_data->term->cursor_row,
|
|
|
|
client_data->term->cursor_col);
|
2013-03-27 11:11:56 +00:00
|
|
|
|
2013-03-29 09:51:31 +00:00
|
|
|
/* Flush terminal delta */
|
|
|
|
guac_terminal_delta_flush(client_data->term->delta, client_data->term);
|
|
|
|
|
|
|
|
}
|
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_clipboard_handler(guac_client* client, char* data) {
|
|
|
|
|
|
|
|
ssh_guac_client_data* client_data = (ssh_guac_client_data*) client->data;
|
|
|
|
|
|
|
|
free(client_data->clipboard_data);
|
|
|
|
|
|
|
|
client_data->clipboard_data = strdup(data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-01 08:59:15 +00:00
|
|
|
/* Determine which buttons were just released */
|
|
|
|
int released_mask = client_data->mouse_mask & ~mask;
|
2011-12-30 22:34:04 +00:00
|
|
|
client_data->mouse_mask = mask;
|
|
|
|
|
|
|
|
/* Paste contents of clipboard on right mouse button up */
|
2013-04-01 08:59:15 +00:00
|
|
|
if ((released_mask & GUAC_CLIENT_MOUSE_RIGHT)
|
|
|
|
&& client_data->clipboard_data != NULL) {
|
2011-12-30 22:34:04 +00:00
|
|
|
|
|
|
|
int length = strlen(client_data->clipboard_data);
|
2013-04-01 08:59:15 +00:00
|
|
|
if (length)
|
|
|
|
return channel_write(client_data->term_channel,
|
|
|
|
client_data->clipboard_data, length);
|
|
|
|
|
|
|
|
}
|
2011-12-30 22:34:04 +00:00
|
|
|
|
2013-04-01 08:59:15 +00:00
|
|
|
/* Scroll up if wheel moved up */
|
|
|
|
if (released_mask & GUAC_CLIENT_MOUSE_SCROLL_UP) {
|
|
|
|
/* STUB */
|
|
|
|
guac_client_log_info(client, "stub: scroll up");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scroll down if wheel moved down */
|
|
|
|
if (released_mask & GUAC_CLIENT_MOUSE_SCROLL_DOWN) {
|
|
|
|
/* STUB */
|
|
|
|
guac_client_log_info(client, "stub: scroll down");
|
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;
|
|
|
|
|
2011-08-22 06:24:40 +00:00
|
|
|
/* Track modifiers */
|
|
|
|
if (keysym == 0xFFE3) {
|
|
|
|
client_data->mod_ctrl = pressed;
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:46:21 +00:00
|
|
|
/* If key pressed */
|
2011-08-22 06:24:40 +00:00
|
|
|
else if (pressed) {
|
2011-08-04 18:46:21 +00:00
|
|
|
|
|
|
|
/* If simple ASCII key */
|
2011-08-10 18:03:38 +00:00
|
|
|
if (keysym >= 0x00 && keysym <= 0xFF) {
|
|
|
|
char data = (char) keysym;
|
2011-08-22 06:24:40 +00:00
|
|
|
|
|
|
|
/* Handle Ctrl modifier */
|
|
|
|
if (client_data->mod_ctrl) {
|
|
|
|
if (keysym >= 'A' && keysym <= 'Z')
|
|
|
|
data = (char) (keysym - 'A' + 1);
|
|
|
|
else if (keysym >= 'a' && keysym <= 'z')
|
|
|
|
data = (char) (keysym - 'a' + 1);
|
|
|
|
}
|
|
|
|
|
2011-08-10 18:03:38 +00:00
|
|
|
return channel_write(client_data->term_channel, &data, 1);
|
2011-08-22 06:24:40 +00:00
|
|
|
|
2011-08-10 18:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2011-08-10 18:03:38 +00:00
|
|
|
int length = 0;
|
|
|
|
const char* data = NULL;
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2011-08-10 18:03:38 +00:00
|
|
|
if (keysym == 0xFF08) { data = "\x08"; length = 1; }
|
|
|
|
else if (keysym == 0xFF09) { data = "\x09"; length = 1; }
|
|
|
|
else if (keysym == 0xFF0D) { data = "\x0D"; length = 1; }
|
|
|
|
else if (keysym == 0xFF1B) { data = "\x1B"; length = 1; }
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2011-08-10 18:03:38 +00:00
|
|
|
/* Arrow keys */
|
|
|
|
else if (keysym == 0xFF52) { data = "\x1B\x5B""A"; length = 3; }
|
|
|
|
else if (keysym == 0xFF54) { data = "\x1B\x5B""B"; length = 3; }
|
|
|
|
else if (keysym == 0xFF53) { data = "\x1B\x5B""C"; length = 3; }
|
|
|
|
else if (keysym == 0xFF51) { data = "\x1B\x5B""D"; length = 3; }
|
|
|
|
|
2012-12-17 01:51:49 +00:00
|
|
|
/* Ignore other keys */
|
|
|
|
else return 0;
|
|
|
|
|
2011-08-10 18:03:38 +00:00
|
|
|
return channel_write(client_data->term_channel, data, length);
|
|
|
|
}
|
2011-08-04 18:46:21 +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;
|
|
|
|
|
|
|
|
/* Free terminal */
|
2012-12-09 08:35:37 +00:00
|
|
|
guac_terminal_free(guac_client_data->term);
|
2011-12-30 22:34:04 +00:00
|
|
|
|
|
|
|
/* Free clipboard data */
|
|
|
|
free(guac_client_data->clipboard_data);
|
|
|
|
|
|
|
|
/* Free generic data struct */
|
|
|
|
free(client->data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|