From cdefe52c6759e96a5415e7c79056995325d44deb Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 14 Jul 2015 11:02:48 -0700 Subject: [PATCH] GUAC-1171: Ensure user and session objects are automatically free'd, and that this is documented. --- src/common-ssh/guac_sftp.h | 3 ++- src/common-ssh/guac_ssh.c | 10 ++++++++++ src/common-ssh/guac_ssh.h | 3 ++- src/protocols/rdp/client.c | 5 ++++- src/protocols/vnc/client.c | 5 ++++- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/common-ssh/guac_sftp.h b/src/common-ssh/guac_sftp.h index 13511115..6fa9f1f6 100644 --- a/src/common-ssh/guac_sftp.h +++ b/src/common-ssh/guac_sftp.h @@ -94,7 +94,8 @@ typedef struct guac_common_ssh_sftp_ls_state { * guac_common_ssh_destroy_sftp_filesystem(). * * @param session - * The session to use to provide SFTP. + * The session to use to provide SFTP. This session will automatically be + * destroyed when this filesystem is destroyed. * * @param name * The name to send as the name of the filesystem. diff --git a/src/common-ssh/guac_ssh.c b/src/common-ssh/guac_ssh.c index 9aa308d9..b9547034 100644 --- a/src/common-ssh/guac_ssh.c +++ b/src/common-ssh/guac_ssh.c @@ -22,6 +22,7 @@ #include "guac_ssh.h" #include "guac_ssh_key.h" +#include "guac_ssh_user.h" #include #include @@ -477,8 +478,17 @@ guac_common_ssh_session* guac_common_ssh_create_session(guac_client* client, } void guac_common_ssh_destroy_session(guac_common_ssh_session* session) { + + /* Disconnect and clean up libssh2 */ libssh2_session_disconnect(session->session, "Bye"); libssh2_session_free(session->session); + + /* Destroy associated user */ + if (session->user) + guac_common_ssh_destroy_user(session->user); + + /* Free all other data */ free(session); + } diff --git a/src/common-ssh/guac_ssh.h b/src/common-ssh/guac_ssh.h index cf9e535e..64c69134 100644 --- a/src/common-ssh/guac_ssh.h +++ b/src/common-ssh/guac_ssh.h @@ -91,7 +91,8 @@ void guac_common_ssh_uninit(); * The port to connect to on the given hostname. * * @param user - * The user to authenticate as, once connected. + * The user to authenticate as, once connected. This user will be + * automatically destroyed when this session is destroyed. * * @return * A new SSH session if the connection and authentication succeed, or NULL diff --git a/src/protocols/rdp/client.c b/src/protocols/rdp/client.c index 03cef18a..aad284e9 100644 --- a/src/protocols/rdp/client.c +++ b/src/protocols/rdp/client.c @@ -842,8 +842,10 @@ int guac_client_init(guac_client* client, int argc, char** argv) { /* Abort if private key cannot be read */ if (guac_common_ssh_user_import_key(user, argv[IDX_SFTP_PRIVATE_KEY], - argv[IDX_SFTP_PASSPHRASE])) + argv[IDX_SFTP_PASSPHRASE])) { + guac_common_ssh_destroy_user(user); return 1; + } } @@ -880,6 +882,7 @@ int guac_client_init(guac_client* client, int argc, char** argv) { /* 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; } diff --git a/src/protocols/vnc/client.c b/src/protocols/vnc/client.c index 49c53569..426b6ced 100644 --- a/src/protocols/vnc/client.c +++ b/src/protocols/vnc/client.c @@ -384,8 +384,10 @@ int guac_client_init(guac_client* client, int argc, char** argv) { /* Abort if private key cannot be read */ if (guac_common_ssh_user_import_key(user, argv[IDX_SFTP_PRIVATE_KEY], - argv[IDX_SFTP_PASSPHRASE])) + argv[IDX_SFTP_PASSPHRASE])) { + guac_common_ssh_destroy_user(user); return 1; + } } @@ -414,6 +416,7 @@ int guac_client_init(guac_client* client, int argc, char** argv) { /* 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; }