| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2013 by Eran Ifrah |
| 5 |
// file name : cl_ssh.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#ifndef CLSSH_H |
| 27 |
#define CLSSH_H |
| 28 |
|
| 29 |
#if USE_SFTP |
| 30 |
#include "clSSHAgent.hpp" |
| 31 |
#include "cl_command_event.h" |
| 32 |
#include "cl_exception.h" |
| 33 |
#include "codelite_exports.h" |
| 34 |
|
| 35 |
#include <errno.h> |
| 36 |
#include <memory> |
| 37 |
#include <wx/event.h> |
| 38 |
#include <wx/sharedptr.h> |
| 39 |
#include <wx/string.h> |
| 40 |
#include <wx/timer.h> |
| 41 |
|
| 42 |
// We do it this way to avoid exposing the include to ssh/libssh.h to files including this header |
| 43 |
struct ssh_session_struct; |
| 44 |
struct ssh_channel_struct; |
| 45 |
typedef struct ssh_session_struct* SSHSession_t; |
| 46 |
typedef struct ssh_channel_struct* SSHChannel_t; |
| 47 |
|
| 48 |
// Sent when a remote command over ssh has an output |
| 49 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_SSH_COMMAND_OUTPUT, clCommandEvent); |
| 50 |
// Sent when a remote command over ssh has completed |
| 51 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_SSH_COMMAND_COMPLETED, clCommandEvent); |
| 52 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_SSH_COMMAND_ERROR, clCommandEvent); |
| 53 |
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_SSH_CONNECTED, clCommandEvent); |
| 54 |
|
| 55 |
class WXDLLIMPEXP_CL clSSH : public wxEvtHandler |
| 56 |
{ |
| 57 |
protected: |
| 58 |
wxString m_host; |
| 59 |
wxString m_username; |
| 60 |
wxString m_password; |
| 61 |
int m_port; |
| 62 |
bool m_connected; |
| 63 |
SSHSession_t m_session; |
| 64 |
SSHChannel_t m_channel; |
| 65 |
wxTimer* m_timer; |
| 66 |
wxEvtHandler* m_owner; |
| 67 |
clSSHAgent::Ptr_t m_sshAgent; |
| 68 |
|
| 69 |
public: |
| 70 |
typedef std::shared_ptr<clSSH> Ptr_t; |
| 71 |
|
| 72 |
protected: |
| 73 |
void OnCheckRemoteOutut(wxTimerEvent& event); |
| 74 |
void DoCloseChannel(); |
| 75 |
void DoOpenChannel(); |
| 76 |
void DoConnectWithRetries(int retries); |
| 77 |
|
| 78 |
public: |
| 79 |
clSSH(const wxString& host, const wxString& user, const wxString& pass, int port = 22); |
| 80 |
clSSH(); |
| 81 |
virtual ~clSSH(); |
| 82 |
|
| 83 |
bool IsConnected() const { return m_connected; } |
| 84 |
bool IsCommandRunning() const { return m_channel != NULL; } |
| 85 |
|
| 86 |
/** |
| 87 |
* @brief send a message that should be ignored. This is useful for implemeting keep-alive |
| 88 |
*/ |
| 89 |
void SendIgnore(); |
| 90 |
|
| 91 |
/** |
| 92 |
* @brief connect to the remote server |
| 93 |
*/ |
| 94 |
void Connect(int seconds = 10); |
| 95 |
|
| 96 |
/** |
| 97 |
* @brief authenticate the server |
| 98 |
* @param [output] message in case the authentication failed, prompt the user with the message |
| 99 |
* @return true if the server could be authenticated, otherwise return false. |
| 100 |
* In case an error occurs, throw a clException |
| 101 |
*/ |
| 102 |
bool AuthenticateServer(wxString& message); |
| 103 |
|
| 104 |
/** |
| 105 |
* @brief accepts the server authentication and add it to the "known_hosts" |
| 106 |
*/ |
| 107 |
void AcceptServerAuthentication(); |
| 108 |
|
| 109 |
/** |
| 110 |
* @brief login to the server with the user credentials |
| 111 |
* @return true if we managed to login |
| 112 |
* @throw clException incase something really bad happened |
| 113 |
*/ |
| 114 |
bool LoginPassword(bool throwExc = true); |
| 115 |
|
| 116 |
/** |
| 117 |
* @brief login using ssh_userauth_none() |
| 118 |
*/ |
| 119 |
bool LoginAuthNone(bool throwExc = true); |
| 120 |
|
| 121 |
/** |
| 122 |
* @brief login using public key |
| 123 |
* @return true if we managed to login |
| 124 |
* @throw clException incase something really bad happened |
| 125 |
*/ |
| 126 |
bool LoginPublicKey(bool throwExc = true); |
| 127 |
|
| 128 |
/** |
| 129 |
* @brief login using interactive-keyboard method |
| 130 |
* @return true if we managed to login |
| 131 |
* @throw clException incase something really bad happened |
| 132 |
*/ |
| 133 |
bool LoginInteractiveKBD(bool throwExc = true); |
| 134 |
|
| 135 |
/** |
| 136 |
* @brief try to login using all the methods we support (interactive-kbd, user/pass and public key) |
| 137 |
*/ |
| 138 |
void Login(); |
| 139 |
|
| 140 |
/** |
| 141 |
* @brief close the SSH session |
| 142 |
* IMPORTANT: this will invalidate all other channels (like: scp) |
| 143 |
*/ |
| 144 |
void Close(); |
| 145 |
|
| 146 |
/** |
| 147 |
* @brief execute a remote command and return the output. open the shell if no is opened |
| 148 |
*/ |
| 149 |
void ExecuteShellCommand(wxEvtHandler* owner, const wxString& command); |
| 150 |
|
| 151 |
SSHSession_t GetSession() { return m_session; } |
| 152 |
|
| 153 |
void SetPassword(const wxString& password) { this->m_password = password; } |
| 154 |
void SetPort(int port) { this->m_port = port; } |
| 155 |
|
| 156 |
const wxString& GetPassword() const { return m_password; } |
| 157 |
int GetPort() const { return m_port; } |
| 158 |
void SetHost(const wxString& host) { this->m_host = host; } |
| 159 |
void SetUsername(const wxString& username) { this->m_username = username; } |
| 160 |
const wxString& GetHost() const { return m_host; } |
| 161 |
const wxString& GetUsername() const { return m_username; } |
| 162 |
}; |
| 163 |
#endif // USE_SFTP |
| 164 |
#endif // CLSSH_H |