2013-12-29 04:53:12 +00:00
|
|
|
/*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
2011-07-30 22:12:28 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-30 22:12:28 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-12-29 04:53:12 +00:00
|
|
|
*/
|
|
|
|
|
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"
|
2018-07-31 06:01:27 +00:00
|
|
|
#include "common/clipboard.h"
|
2017-11-27 17:38:20 +00:00
|
|
|
#include "common/recording.h"
|
2017-02-27 22:28:23 +00:00
|
|
|
#include "common-ssh/sftp.h"
|
2016-03-01 05:51:42 +00:00
|
|
|
#include "ssh.h"
|
2017-02-27 22:34:46 +00:00
|
|
|
#include "terminal/terminal.h"
|
2016-03-01 05:51:42 +00:00
|
|
|
#include "user.h"
|
2014-01-01 22:44:28 +00:00
|
|
|
|
2014-06-02 23:01:13 +00:00
|
|
|
#include <langinfo.h>
|
|
|
|
#include <locale.h>
|
2011-07-30 22:12:28 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-08-01 20:31:48 +00:00
|
|
|
|
2011-07-30 22:12:28 +00:00
|
|
|
#include <guacamole/client.h>
|
2011-08-04 18:46:21 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
int guac_client_init(guac_client* client) {
|
2013-05-26 06:50:13 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Set client args */
|
|
|
|
client->args = GUAC_SSH_CLIENT_ARGS;
|
2011-08-01 03:51:19 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Allocate client instance data */
|
|
|
|
guac_ssh_client* ssh_client = calloc(1, sizeof(guac_ssh_client));
|
|
|
|
client->data = ssh_client;
|
2013-05-26 06:50:13 +00:00
|
|
|
|
2018-07-31 06:01:27 +00:00
|
|
|
/* Init clipboard */
|
|
|
|
ssh_client->clipboard = guac_common_clipboard_alloc(GUAC_SSH_CLIPBOARD_MAX_LENGTH);
|
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Set handlers */
|
|
|
|
client->join_handler = guac_ssh_user_join_handler;
|
|
|
|
client->free_handler = guac_ssh_client_free_handler;
|
2013-05-20 08:23:21 +00:00
|
|
|
|
2014-06-02 23:01:13 +00:00
|
|
|
/* Set locale and warn if not UTF-8 */
|
|
|
|
setlocale(LC_CTYPE, "");
|
2016-03-01 05:51:42 +00:00
|
|
|
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-10-18 22:37:16 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Success */
|
|
|
|
return 0;
|
2013-12-02 10:07:17 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
}
|
2013-05-26 06:50:13 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
int guac_ssh_client_free_handler(guac_client* client) {
|
2015-10-19 22:58:44 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
guac_ssh_client* ssh_client = (guac_ssh_client*) client->data;
|
2013-05-20 08:23:21 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Close SSH channel */
|
|
|
|
if (ssh_client->term_channel != NULL) {
|
|
|
|
libssh2_channel_send_eof(ssh_client->term_channel);
|
|
|
|
libssh2_channel_close(ssh_client->term_channel);
|
2013-08-23 21:10:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Free terminal (which may still be using term_channel) */
|
|
|
|
if (ssh_client->term != NULL) {
|
2017-10-18 19:08:32 +00:00
|
|
|
/* Stop the terminal to unblock any pending reads/writes */
|
|
|
|
guac_terminal_stop(ssh_client->term);
|
|
|
|
|
|
|
|
/* Wait ssh_client_thread to finish before freeing the terminal */
|
2016-03-01 05:51:42 +00:00
|
|
|
pthread_join(ssh_client->client_thread, NULL);
|
2017-10-18 19:08:32 +00:00
|
|
|
guac_terminal_free(ssh_client->term);
|
2016-03-01 05:51:42 +00:00
|
|
|
}
|
2016-01-27 20:40:35 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Free terminal channel now that the terminal is finished */
|
|
|
|
if (ssh_client->term_channel != NULL)
|
|
|
|
libssh2_channel_free(ssh_client->term_channel);
|
2016-01-27 20:40:35 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Clean up the SFTP filesystem object and session */
|
|
|
|
if (ssh_client->sftp_filesystem) {
|
|
|
|
guac_common_ssh_destroy_sftp_filesystem(ssh_client->sftp_filesystem);
|
|
|
|
guac_common_ssh_destroy_session(ssh_client->sftp_session);
|
2016-01-27 20:40:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 17:38:20 +00:00
|
|
|
/* Clean up recording, if in progress */
|
|
|
|
if (ssh_client->recording != NULL)
|
|
|
|
guac_common_recording_free(ssh_client->recording);
|
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Free interactive SSH session */
|
|
|
|
if (ssh_client->session != NULL)
|
|
|
|
guac_common_ssh_destroy_session(ssh_client->session);
|
2014-07-10 23:49:13 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Free SSH client credentials */
|
|
|
|
if (ssh_client->user != NULL)
|
|
|
|
guac_common_ssh_destroy_user(ssh_client->user);
|
2011-08-01 03:51:19 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Free parsed settings */
|
|
|
|
if (ssh_client->settings != NULL)
|
|
|
|
guac_ssh_settings_free(ssh_client->settings);
|
2011-08-01 03:51:19 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
/* Free client structure */
|
2018-07-31 06:01:27 +00:00
|
|
|
guac_common_clipboard_free(ssh_client->clipboard);
|
2016-03-01 05:51:42 +00:00
|
|
|
free(ssh_client);
|
2011-08-01 03:51:19 +00:00
|
|
|
|
2016-03-01 05:51:42 +00:00
|
|
|
guac_common_ssh_uninit();
|
2011-07-30 22:12:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|