File: clSSHChannel.cpp

package info (click to toggle)
codelite 14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (360 lines) | stat: -rw-r--r-- 11,624 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "StringUtils.h"
#include "clSSHChannel.h"
#include <wx/msgqueue.h>
#include <wx/regex.h>
#if USE_SFTP
#include "clJoinableThread.h"
#include "cl_exception.h"
#include <libssh/libssh.h>

wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_READ_ERROR, clCommandEvent);
wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_WRITE_ERROR, clCommandEvent);
wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_READ_OUTPUT, clCommandEvent);
wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_CLOSED, clCommandEvent);
wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_PTY, clCommandEvent);

//===-------------------------------------------------------------
// This thread is used when requesting a non interactive command
//===-------------------------------------------------------------
class clSSHChannelReader : public clJoinableThread
{
    wxEvtHandler* m_handler;
    SSHChannel_t m_channel;

protected:
    bool ReadChannel(bool isStderr)
    {
        int nStderr = isStderr ? 1 : 0;
        int bytes = ssh_channel_poll_timeout(m_channel, 50, nStderr);
        if(bytes == SSH_ERROR) {
            // an error
            clCommandEvent event(wxEVT_SSH_CHANNEL_READ_ERROR);
            m_handler->QueueEvent(event.Clone());
            return false;
        } else if(bytes == SSH_EOF) {
            // channel closed
            clCommandEvent event(wxEVT_SSH_CHANNEL_CLOSED);
            m_handler->QueueEvent(event.Clone());
            return false;
        } else if(bytes == 0) {
            // timeout
            return true;
        } else {
            // there is something to read
            char* buffer = new char[bytes + 1];
            if(ssh_channel_read(m_channel, buffer, bytes, nStderr) != bytes) {
                clCommandEvent event(wxEVT_SSH_CHANNEL_READ_ERROR);
                m_handler->QueueEvent(event.Clone());
                wxDELETEA(buffer);
                return false;
            } else {
                buffer[bytes] = 0;
                clCommandEvent event(wxEVT_SSH_CHANNEL_READ_OUTPUT);
                event.SetString(wxString(buffer, wxConvUTF8));
                m_handler->QueueEvent(event.Clone());
                wxDELETEA(buffer);
            }
        }
        return true;
    }

public:
    clSSHChannelReader(wxEvtHandler* handler, SSHChannel_t channel)
        : m_handler(handler)
        , m_channel(channel)
    {
    }
    virtual ~clSSHChannelReader() {}

    void* Entry()
    {
        while(!TestDestroy()) {
            // First, poll the channel
            if(!ReadChannel(false) || !ReadChannel(true)) { break; }
        }
        return NULL;
    }
};

//===-------------------------------------------------------------
// This thread is used when requesting an interactive shell
//===-------------------------------------------------------------
class clSSHChannelInteractiveThread : public clJoinableThread
{
    wxEvtHandler* m_handler;
    SSHChannel_t m_channel;
    wxMessageQueue<wxString>& m_Queue;
    char m_buffer[4096];
    wxRegEx m_reTTY;

public:
    clSSHChannelInteractiveThread(wxEvtHandler* handler, SSHChannel_t channel, wxMessageQueue<wxString>& Q)
        : m_handler(handler)
        , m_channel(channel)
        , m_Queue(Q)
    {
        m_reTTY.Compile("tty=([a-z/0-9]+)");
    }
    virtual ~clSSHChannelInteractiveThread() {}

protected:
    bool ReadChannel(bool isStderr)
    {
        // First, poll the channel
        m_buffer[0] = 0;
        int bytes = ssh_channel_read_nonblocking(m_channel, m_buffer, sizeof(m_buffer) - 1, 0);
        if(bytes == SSH_ERROR) {
            // an error
            clCommandEvent event(wxEVT_SSH_CHANNEL_READ_ERROR);
            m_handler->QueueEvent(event.Clone());
            return false;
        } else if(bytes == 0) {
            // 0 is a valid output
            // but we need to check for EOF
            if(ssh_channel_is_eof(m_channel)) {
                clCommandEvent event(wxEVT_SSH_CHANNEL_CLOSED);
                m_handler->QueueEvent(event.Clone());
                return false;
            } else {
                // timeout occured
                return true;
            }
        } else {
            // we read something...
            m_buffer[bytes] = 0;
            wxString messageRead(m_buffer, wxConvUTF8);

            // strip colours from the output
            StringUtils::StripTerminalColouring(messageRead, messageRead);

            if(m_reTTY.IsValid() && m_reTTY.Matches(messageRead)) {
                wxString wholeMatch = m_reTTY.GetMatch(messageRead, 0);
                clCommandEvent event(wxEVT_SSH_CHANNEL_PTY);
                wxString tty = m_reTTY.GetMatch(messageRead, 1);
                event.SetString(tty);
                m_handler->QueueEvent(event.Clone());
            }

            if(!messageRead.IsEmpty()) {
                clCommandEvent event(wxEVT_SSH_CHANNEL_READ_OUTPUT);
                event.SetString(messageRead);
                m_handler->QueueEvent(event.Clone());
            }
            return true;
        }
    }

public:
    void* Entry()
    {
        while(!TestDestroy()) {
            if(!ReadChannel(false) || !ReadChannel(true)) { break; }
            
            // Check if we got something to write
            wxString message;
            bool write_error = false;
            while(!write_error && (m_Queue.ReceiveTimeout(20, message) == wxMSGQUEUE_NO_ERROR)) {
                message.Trim().Trim(false);
                message << "\n";
                std::string pbuffer = message.mb_str(wxConvISO8859_1).data();
                if(ssh_channel_write(m_channel, pbuffer.c_str(), pbuffer.length()) == SSH_ERROR) {
                    clCommandEvent event(wxEVT_SSH_CHANNEL_WRITE_ERROR);
                    m_handler->AddPendingEvent(event);
                    write_error = true;
                    break;
                }
            }
            if(write_error) { break; }
        }
        return NULL;
    }
};

//===-------------------------------------------------------------
// The SSH channel
//===-------------------------------------------------------------
clSSHChannel::clSSHChannel(clSSH::Ptr_t ssh, clSSHChannel::eChannelType type, wxEvtHandler* owner)
    : m_ssh(ssh)
    , m_owner(owner)
    , m_type(type)
{
    Bind(wxEVT_SSH_CHANNEL_READ_ERROR, &clSSHChannel::OnReadError, this);
    Bind(wxEVT_SSH_CHANNEL_WRITE_ERROR, &clSSHChannel::OnWriteError, this);
    Bind(wxEVT_SSH_CHANNEL_READ_OUTPUT, &clSSHChannel::OnReadOutput, this);
    Bind(wxEVT_SSH_CHANNEL_CLOSED, &clSSHChannel::OnChannelClosed, this);
    Bind(wxEVT_SSH_CHANNEL_PTY, &clSSHChannel::OnChannelPty, this);
}

clSSHChannel::~clSSHChannel()
{
    Unbind(wxEVT_SSH_CHANNEL_READ_ERROR, &clSSHChannel::OnReadError, this);
    Unbind(wxEVT_SSH_CHANNEL_WRITE_ERROR, &clSSHChannel::OnWriteError, this);
    Unbind(wxEVT_SSH_CHANNEL_READ_OUTPUT, &clSSHChannel::OnReadOutput, this);
    Unbind(wxEVT_SSH_CHANNEL_CLOSED, &clSSHChannel::OnChannelClosed, this);
    Unbind(wxEVT_SSH_CHANNEL_PTY, &clSSHChannel::OnChannelPty, this);
    Close();
}

void clSSHChannel::Open()
{
    if(IsOpen()) { return; }
    if(!m_ssh) { throw clException("ssh session is not opened"); }
    m_channel = ssh_channel_new(m_ssh->GetSession());
    if(!m_channel) { throw clException(BuildError("Failed to allocte ssh channel")); }

    int rc = ssh_channel_open_session(m_channel);
    if(rc != SSH_OK) {
        ssh_channel_free(m_channel);
        m_channel = NULL;
        throw clException(BuildError("Failed to open ssh channel"));
    }

    // For an interactive channel, we also need pty
    if(IsInteractive()) {
        rc = ssh_channel_request_pty(m_channel);
        if(rc != SSH_OK) {
            ssh_channel_free(m_channel);
            m_channel = NULL;
            throw clException(BuildError("Failed to request pty"));
        }

        rc = ssh_channel_change_pty_size(m_channel, 1024, 24);
        if(rc != SSH_OK) {
            ssh_channel_free(m_channel);
            m_channel = NULL;
            throw clException(BuildError("Failed to change SSH pty size"));
        }

        rc = ssh_channel_request_shell(m_channel);
        if(rc != SSH_OK) {
            ssh_channel_free(m_channel);
            m_channel = NULL;
            throw clException(BuildError("Failed to request SSH shell"));
        }

        // Open the helper thread
        m_thread = new clSSHChannelInteractiveThread(this, m_channel, m_Queue);
        m_thread->Start();

        // Ask for the tty
        DoWrite("echo tty=`tty`");
    }
}

void clSSHChannel::Close()
{
    // Stop the worker thread
    wxDELETE(m_thread);

    if(IsOpen()) {
        ssh_channel_close(m_channel);
        ssh_channel_free(m_channel);
        m_channel = NULL;
    }
}

void clSSHChannel::Execute(const wxString& command)
{
    if(IsInteractive()) {
        DoWrite(command);

    } else {
        // Sanity
        if(m_thread) { throw clException("Channel is busy"); }
        if(!IsOpen()) { throw clException("Channel is not opened"); }
        int rc = ssh_channel_request_exec(m_channel, command.mb_str(wxConvUTF8).data());
        if(rc != SSH_OK) {
            Close();
            throw clException(BuildError("Execute failed"));
        }
        m_thread = new clSSHChannelReader(this, m_channel);
        m_thread->Start();
    }
}

wxString clSSHChannel::BuildError(const wxString& prefix) const
{
    if(!m_ssh) { return prefix; }
    wxString errmsg = ssh_get_error(m_ssh->GetSession());
    return wxString() << prefix << ". " << errmsg;
}

void clSSHChannel::Write(const wxString& message)
{
    if(IsInteractive()) { throw clException("Write is only available for interactive ssh channels"); }
    if(!IsOpen()) { throw clException("Channel is not opened"); }
    DoWrite(message);
}

void clSSHChannel::DoWrite(const wxString& buffer)
{
    if(IsInteractive()) { throw clException("Write is only available for interactive ssh channels"); }
    m_Queue.Post(buffer);
}

void clSSHChannel::OnReadError(clCommandEvent& event)
{
    event.SetString(BuildError("Read error"));
    m_owner->AddPendingEvent(event);
}

void clSSHChannel::OnWriteError(clCommandEvent& event)
{
    event.SetString(BuildError("Write error"));
    m_owner->AddPendingEvent(event);
}

void clSSHChannel::OnReadOutput(clCommandEvent& event) { m_owner->AddPendingEvent(event); }

void clSSHChannel::OnChannelClosed(clCommandEvent& event) { m_owner->AddPendingEvent(event); }

void clSSHChannel::OnChannelPty(clCommandEvent& event) { m_owner->AddPendingEvent(event); }

void clSSHChannel::SendSignal(wxSignal sig)
{
    if(!m_ssh) { throw clException("ssh session is not opened"); }
    if(!m_channel) { throw clException("ssh channel is not opened"); }

    const char* prefix = nullptr;
    switch(sig) {
    case wxSIGABRT:
        prefix = "ABRT";
        break;
    case wxSIGALRM:
        prefix = "ALRM";
        break;
    case wxSIGFPE:
        prefix = "FPE";
        break;
    case wxSIGHUP:
        prefix = "HUP";
        break;
    case wxSIGILL:
        prefix = "ILL";
        break;
    case wxSIGINT:
        prefix = "INT";
        break;
    case wxSIGKILL:
        prefix = "KILL";
        break;
    case wxSIGPIPE:
        prefix = "PIPE";
        break;
    case wxSIGQUIT:
        prefix = "QUIT";
        break;
    case wxSIGSEGV:
        prefix = "SEGV";
        break;
    case wxSIGTERM:
        prefix = "TERM";
        break;
    default:
        break;
    }
    if(!prefix) { throw clException("Requested to send an unknown signal"); }
    int rc = ssh_channel_request_send_signal(m_channel, prefix);
    if(rc != SSH_OK) { throw clException(BuildError("Failed to send signal")); }
}
#endif