| 1 |
#include "StringUtils.h" |
| 2 |
#include "clSSHChannel.hpp" |
| 3 |
#include <wx/msgqueue.h> |
| 4 |
#include <wx/regex.h> |
| 5 |
#if USE_SFTP |
| 6 |
#include "clJoinableThread.h" |
| 7 |
#include "cl_exception.h" |
| 8 |
#include <libssh/libssh.h> |
| 9 |
|
| 10 |
wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_READ_ERROR, clCommandEvent); |
| 11 |
wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_WRITE_ERROR, clCommandEvent); |
| 12 |
wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_READ_OUTPUT, clCommandEvent); |
| 13 |
wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_READ_STDERR, clCommandEvent); |
| 14 |
wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_CLOSED, clCommandEvent); |
| 15 |
wxDEFINE_EVENT(wxEVT_SSH_CHANNEL_PTY, clCommandEvent); |
| 16 |
|
| 17 |
//===------------------------------------------------------------- |
| 18 |
// This thread is used when requesting a non interactive command |
| 19 |
//===------------------------------------------------------------- |
| 20 |
class clSSHChannelReader : public clJoinableThread |
| 21 |
{ |
| 22 |
wxEvtHandler* m_handler; |
| 23 |
SSHChannel_t m_channel; |
| 24 |
bool m_wantStderr = false; |
| 25 |
|
| 26 |
protected: |
| 27 |
bool ReadChannel(bool isStderr) |
| 28 |
{ |
| 29 |
int bytes = ssh_channel_poll_timeout(m_channel, 50, isStderr ? 1 : 0); |
| 30 |
if(bytes == SSH_ERROR) { |
| 31 |
// an error |
| 32 |
clCommandEvent event(wxEVT_SSH_CHANNEL_READ_ERROR); |
| 33 |
m_handler->QueueEvent(event.Clone()); |
| 34 |
return false; |
| 35 |
} else if(bytes == SSH_EOF) { |
| 36 |
// channel closed |
| 37 |
clCommandEvent event(wxEVT_SSH_CHANNEL_CLOSED); |
| 38 |
m_handler->QueueEvent(event.Clone()); |
| 39 |
return false; |
| 40 |
} else if(bytes == 0) { |
| 41 |
// timeout |
| 42 |
return true; |
| 43 |
} else { |
| 44 |
// there is something to read |
| 45 |
char* buffer = new char[bytes + 1]; |
| 46 |
if(ssh_channel_read(m_channel, buffer, bytes, isStderr ? 1 : 0) != bytes) { |
| 47 |
clCommandEvent event(wxEVT_SSH_CHANNEL_READ_ERROR); |
| 48 |
m_handler->QueueEvent(event.Clone()); |
| 49 |
wxDELETEA(buffer); |
| 50 |
return false; |
| 51 |
} else { |
| 52 |
buffer[bytes] = 0; |
| 53 |
clCommandEvent event((isStderr && m_wantStderr) ? wxEVT_SSH_CHANNEL_READ_STDERR |
| 54 |
: wxEVT_SSH_CHANNEL_READ_OUTPUT); |
| 55 |
event.SetString(wxString(buffer, wxConvUTF8)); |
| 56 |
m_handler->QueueEvent(event.Clone()); |
| 57 |
wxDELETEA(buffer); |
| 58 |
} |
| 59 |
} |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
public: |
| 64 |
clSSHChannelReader(wxEvtHandler* handler, SSHChannel_t channel, bool wantStderr) |
| 65 |
: m_handler(handler) |
| 66 |
, m_channel(channel) |
| 67 |
, m_wantStderr(false) |
| 68 |
{ |
| 69 |
} |
| 70 |
virtual ~clSSHChannelReader() {} |
| 71 |
|
| 72 |
void* Entry() |
| 73 |
{ |
| 74 |
while(!TestDestroy()) { |
| 75 |
// First, poll the channel |
| 76 |
if(!ReadChannel(false) || !ReadChannel(true)) { |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
return NULL; |
| 81 |
} |
| 82 |
}; |
| 83 |
|
| 84 |
//===------------------------------------------------------------- |
| 85 |
// This thread is used when requesting an interactive shell |
| 86 |
//===------------------------------------------------------------- |
| 87 |
class clSSHChannelInteractiveThread : public clJoinableThread |
| 88 |
{ |
| 89 |
wxEvtHandler* m_handler; |
| 90 |
SSHChannel_t m_channel; |
| 91 |
wxMessageQueue<clSSHChannel::Message>& m_Queue; |
| 92 |
char m_buffer[4096]; |
| 93 |
wxRegEx m_reTTY; |
| 94 |
bool m_wantStderr = false; |
| 95 |
|
| 96 |
public: |
| 97 |
clSSHChannelInteractiveThread(wxEvtHandler* handler, SSHChannel_t channel, wxMessageQueue<clSSHChannel::Message>& Q, |
| 98 |
bool wantStderrEvents) |
| 99 |
: m_handler(handler) |
| 100 |
, m_channel(channel) |
| 101 |
, m_Queue(Q) |
| 102 |
, m_wantStderr(wantStderrEvents) |
| 103 |
{ |
| 104 |
m_reTTY.Compile("tty=([a-z/0-9]+)"); |
| 105 |
} |
| 106 |
virtual ~clSSHChannelInteractiveThread() {} |
| 107 |
|
| 108 |
protected: |
| 109 |
bool ReadChannel(bool isStderr) |
| 110 |
{ |
| 111 |
// First, poll the channel |
| 112 |
m_buffer[0] = 0; |
| 113 |
int bytes = ssh_channel_read_nonblocking(m_channel, m_buffer, sizeof(m_buffer) - 1, isStderr ? 1 : 0); |
| 114 |
if(bytes == SSH_ERROR) { |
| 115 |
// an error |
| 116 |
clCommandEvent event(wxEVT_SSH_CHANNEL_READ_ERROR); |
| 117 |
m_handler->QueueEvent(event.Clone()); |
| 118 |
return false; |
| 119 |
} else if(bytes == 0) { |
| 120 |
// 0 is a valid output |
| 121 |
// but we need to check for EOF |
| 122 |
if(ssh_channel_is_eof(m_channel)) { |
| 123 |
clCommandEvent event(wxEVT_SSH_CHANNEL_CLOSED); |
| 124 |
m_handler->QueueEvent(event.Clone()); |
| 125 |
return false; |
| 126 |
} else { |
| 127 |
// timeout occured |
| 128 |
return true; |
| 129 |
} |
| 130 |
} else { |
| 131 |
// we read something... |
| 132 |
m_buffer[bytes] = 0; |
| 133 |
wxString messageRead(m_buffer, wxConvUTF8); |
| 134 |
|
| 135 |
// strip colours from the output |
| 136 |
StringUtils::StripTerminalColouring(messageRead, messageRead); |
| 137 |
|
| 138 |
if(m_reTTY.IsValid() && m_reTTY.Matches(messageRead)) { |
| 139 |
wxString wholeMatch = m_reTTY.GetMatch(messageRead, 0); |
| 140 |
clCommandEvent event(wxEVT_SSH_CHANNEL_PTY); |
| 141 |
wxString tty = m_reTTY.GetMatch(messageRead, 1); |
| 142 |
event.SetString(tty); |
| 143 |
m_handler->QueueEvent(event.Clone()); |
| 144 |
} |
| 145 |
|
| 146 |
if(!messageRead.IsEmpty()) { |
| 147 |
clCommandEvent event((isStderr && m_wantStderr) ? wxEVT_SSH_CHANNEL_READ_STDERR |
| 148 |
: wxEVT_SSH_CHANNEL_READ_OUTPUT); |
| 149 |
event.SetString(messageRead); |
| 150 |
m_handler->QueueEvent(event.Clone()); |
| 151 |
} |
| 152 |
return true; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
public: |
| 157 |
void* Entry() |
| 158 |
{ |
| 159 |
while(!TestDestroy()) { |
| 160 |
if(!ReadChannel(false) || !ReadChannel(true)) { |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
// Check if we got something to write |
| 165 |
clSSHChannel::Message message; |
| 166 |
bool write_error = false; |
| 167 |
while(!write_error && (m_Queue.ReceiveTimeout(20, message) == wxMSGQUEUE_NO_ERROR)) { |
| 168 |
if(!message.raw) { |
| 169 |
message.buffer.Trim().Trim(false); |
| 170 |
if(message.buffer.empty()) { |
| 171 |
continue; |
| 172 |
} |
| 173 |
message.buffer << "\n"; |
| 174 |
} |
| 175 |
std::string pbuffer = message.buffer.mb_str(wxConvISO8859_1).data(); |
| 176 |
if(ssh_channel_write(m_channel, pbuffer.c_str(), pbuffer.length()) == SSH_ERROR) { |
| 177 |
clCommandEvent event(wxEVT_SSH_CHANNEL_WRITE_ERROR); |
| 178 |
m_handler->AddPendingEvent(event); |
| 179 |
write_error = true; |
| 180 |
break; |
| 181 |
} |
| 182 |
message = {}; |
| 183 |
} |
| 184 |
if(write_error) { |
| 185 |
break; |
| 186 |
} |
| 187 |
} |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
//===------------------------------------------------------------- |
| 193 |
// The SSH channel |
| 194 |
//===------------------------------------------------------------- |
| 195 |
clSSHChannel::clSSHChannel(clSSH::Ptr_t ssh, clSSHChannel::eChannelType type, wxEvtHandler* owner, |
| 196 |
bool wantStderrEvents) |
| 197 |
: m_ssh(ssh) |
| 198 |
, m_owner(owner) |
| 199 |
, m_type(type) |
| 200 |
, m_wantStderr(wantStderrEvents) |
| 201 |
{ |
| 202 |
Bind(wxEVT_SSH_CHANNEL_READ_ERROR, &clSSHChannel::OnReadError, this); |
| 203 |
Bind(wxEVT_SSH_CHANNEL_WRITE_ERROR, &clSSHChannel::OnWriteError, this); |
| 204 |
Bind(wxEVT_SSH_CHANNEL_READ_OUTPUT, &clSSHChannel::OnReadOutput, this); |
| 205 |
Bind(wxEVT_SSH_CHANNEL_READ_STDERR, &clSSHChannel::OnReadStderr, this); |
| 206 |
Bind(wxEVT_SSH_CHANNEL_CLOSED, &clSSHChannel::OnChannelClosed, this); |
| 207 |
Bind(wxEVT_SSH_CHANNEL_PTY, &clSSHChannel::OnChannelPty, this); |
| 208 |
} |
| 209 |
|
| 210 |
clSSHChannel::~clSSHChannel() |
| 211 |
{ |
| 212 |
Unbind(wxEVT_SSH_CHANNEL_READ_ERROR, &clSSHChannel::OnReadError, this); |
| 213 |
Unbind(wxEVT_SSH_CHANNEL_WRITE_ERROR, &clSSHChannel::OnWriteError, this); |
| 214 |
Unbind(wxEVT_SSH_CHANNEL_READ_OUTPUT, &clSSHChannel::OnReadOutput, this); |
| 215 |
Unbind(wxEVT_SSH_CHANNEL_READ_STDERR, &clSSHChannel::OnReadStderr, this); |
| 216 |
Unbind(wxEVT_SSH_CHANNEL_CLOSED, &clSSHChannel::OnChannelClosed, this); |
| 217 |
Unbind(wxEVT_SSH_CHANNEL_PTY, &clSSHChannel::OnChannelPty, this); |
| 218 |
Close(); |
| 219 |
} |
| 220 |
|
| 221 |
void clSSHChannel::Open() |
| 222 |
{ |
| 223 |
if(IsOpen()) { |
| 224 |
return; |
| 225 |
} |
| 226 |
if(!m_ssh) { |
| 227 |
throw clException("ssh session is not opened"); |
| 228 |
} |
| 229 |
m_channel = ssh_channel_new(m_ssh->GetSession()); |
| 230 |
if(!m_channel) { |
| 231 |
throw clException(BuildError("Failed to allocte ssh channel")); |
| 232 |
} |
| 233 |
|
| 234 |
int rc = ssh_channel_open_session(m_channel); |
| 235 |
if(rc != SSH_OK) { |
| 236 |
ssh_channel_free(m_channel); |
| 237 |
m_channel = NULL; |
| 238 |
throw clException(BuildError("Failed to open ssh channel")); |
| 239 |
} |
| 240 |
|
| 241 |
// For an interactive channel, we also need pty |
| 242 |
if(IsInteractive()) { |
| 243 |
rc = ssh_channel_request_pty(m_channel); |
| 244 |
if(rc != SSH_OK) { |
| 245 |
ssh_channel_free(m_channel); |
| 246 |
m_channel = NULL; |
| 247 |
throw clException(BuildError("Failed to request pty")); |
| 248 |
} |
| 249 |
|
| 250 |
rc = ssh_channel_change_pty_size(m_channel, 1024, 24); |
| 251 |
if(rc != SSH_OK) { |
| 252 |
ssh_channel_free(m_channel); |
| 253 |
m_channel = NULL; |
| 254 |
throw clException(BuildError("Failed to change SSH pty size")); |
| 255 |
} |
| 256 |
|
| 257 |
rc = ssh_channel_request_shell(m_channel); |
| 258 |
if(rc != SSH_OK) { |
| 259 |
ssh_channel_free(m_channel); |
| 260 |
m_channel = NULL; |
| 261 |
throw clException(BuildError("Failed to request SSH shell")); |
| 262 |
} |
| 263 |
// Open the helper thread |
| 264 |
m_thread = new clSSHChannelInteractiveThread(this, m_channel, m_Queue, m_wantStderr); |
| 265 |
m_thread->Start(); |
| 266 |
// Ask for the tty |
| 267 |
DoWrite("echo tty=`tty`", false); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
void clSSHChannel::Close() |
| 272 |
{ |
| 273 |
// Stop the worker thread |
| 274 |
wxDELETE(m_thread); |
| 275 |
|
| 276 |
if(IsOpen()) { |
| 277 |
ssh_channel_close(m_channel); |
| 278 |
ssh_channel_free(m_channel); |
| 279 |
m_channel = NULL; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
void clSSHChannel::Execute(const wxString& command) |
| 284 |
{ |
| 285 |
if(IsInteractive()) { |
| 286 |
DoWrite(command, false); |
| 287 |
|
| 288 |
} else { |
| 289 |
// Sanity |
| 290 |
if(m_thread) { |
| 291 |
throw clException("Channel is busy"); |
| 292 |
} |
| 293 |
if(!IsOpen()) { |
| 294 |
throw clException("Channel is not opened"); |
| 295 |
} |
| 296 |
int rc = ssh_channel_request_exec(m_channel, command.mb_str(wxConvUTF8).data()); |
| 297 |
if(rc != SSH_OK) { |
| 298 |
Close(); |
| 299 |
throw clException(BuildError("Execute failed")); |
| 300 |
} |
| 301 |
m_thread = new clSSHChannelReader(this, m_channel, m_wantStderr); |
| 302 |
m_thread->Start(); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
wxString clSSHChannel::BuildError(const wxString& prefix) const |
| 307 |
{ |
| 308 |
if(!m_ssh) { |
| 309 |
return prefix; |
| 310 |
} |
| 311 |
wxString errmsg = ssh_get_error(m_ssh->GetSession()); |
| 312 |
return wxString() << prefix << ". " << errmsg; |
| 313 |
} |
| 314 |
|
| 315 |
void clSSHChannel::Write(const wxString& message) |
| 316 |
{ |
| 317 |
if(!IsInteractive()) { |
| 318 |
throw clException("Write is only available for interactive ssh channels"); |
| 319 |
} |
| 320 |
if(!IsOpen()) { |
| 321 |
throw clException("Channel is not opened"); |
| 322 |
} |
| 323 |
DoWrite(message, false); |
| 324 |
} |
| 325 |
|
| 326 |
void clSSHChannel::DoWrite(const wxString& buffer, bool raw) |
| 327 |
{ |
| 328 |
if(!IsInteractive()) { |
| 329 |
throw clException("Write is only available for interactive ssh channels"); |
| 330 |
} |
| 331 |
|
| 332 |
Message m; |
| 333 |
m.raw = raw; |
| 334 |
m_Queue.Post(m); |
| 335 |
} |
| 336 |
|
| 337 |
void clSSHChannel::OnReadError(clCommandEvent& event) |
| 338 |
{ |
| 339 |
event.SetString(BuildError("Read error")); |
| 340 |
m_owner->AddPendingEvent(event); |
| 341 |
} |
| 342 |
|
| 343 |
void clSSHChannel::OnWriteError(clCommandEvent& event) |
| 344 |
{ |
| 345 |
event.SetString(BuildError("Write error")); |
| 346 |
m_owner->AddPendingEvent(event); |
| 347 |
} |
| 348 |
|
| 349 |
void clSSHChannel::OnReadOutput(clCommandEvent& event) { m_owner->AddPendingEvent(event); } |
| 350 |
void clSSHChannel::OnReadStderr(clCommandEvent& event) { m_owner->AddPendingEvent(event); } |
| 351 |
void clSSHChannel::OnChannelClosed(clCommandEvent& event) { m_owner->AddPendingEvent(event); } |
| 352 |
void clSSHChannel::OnChannelPty(clCommandEvent& event) { m_owner->AddPendingEvent(event); } |
| 353 |
|
| 354 |
void clSSHChannel::SendSignal(wxSignal sig) |
| 355 |
{ |
| 356 |
if(!m_ssh) { |
| 357 |
throw clException("ssh session is not opened"); |
| 358 |
} |
| 359 |
if(!m_channel) { |
| 360 |
throw clException("ssh channel is not opened"); |
| 361 |
} |
| 362 |
|
| 363 |
const char* prefix = nullptr; |
| 364 |
switch(sig) { |
| 365 |
case wxSIGABRT: |
| 366 |
prefix = "ABRT"; |
| 367 |
break; |
| 368 |
case wxSIGALRM: |
| 369 |
prefix = "ALRM"; |
| 370 |
break; |
| 371 |
case wxSIGFPE: |
| 372 |
prefix = "FPE"; |
| 373 |
break; |
| 374 |
case wxSIGHUP: |
| 375 |
prefix = "HUP"; |
| 376 |
break; |
| 377 |
case wxSIGILL: |
| 378 |
prefix = "ILL"; |
| 379 |
break; |
| 380 |
case wxSIGINT: |
| 381 |
prefix = "INT"; |
| 382 |
break; |
| 383 |
case wxSIGKILL: |
| 384 |
prefix = "KILL"; |
| 385 |
break; |
| 386 |
case wxSIGPIPE: |
| 387 |
prefix = "PIPE"; |
| 388 |
break; |
| 389 |
case wxSIGQUIT: |
| 390 |
prefix = "QUIT"; |
| 391 |
break; |
| 392 |
case wxSIGSEGV: |
| 393 |
prefix = "SEGV"; |
| 394 |
break; |
| 395 |
case wxSIGTERM: |
| 396 |
prefix = "TERM"; |
| 397 |
break; |
| 398 |
default: |
| 399 |
break; |
| 400 |
} |
| 401 |
if(!prefix) { |
| 402 |
throw clException("Requested to send an unknown signal"); |
| 403 |
} |
| 404 |
int rc = ssh_channel_request_send_signal(m_channel, prefix); |
| 405 |
if(rc != SSH_OK) { |
| 406 |
throw clException(BuildError("Failed to send signal")); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
void clSSHChannel::Detach() {} |
| 411 |
|
| 412 |
void clSSHChannel::WriteRaw(const wxString& message) |
| 413 |
{ |
| 414 |
if(!IsInteractive()) { |
| 415 |
throw clException("Write is only available for interactive ssh channels"); |
| 416 |
} |
| 417 |
if(!IsOpen()) { |
| 418 |
throw clException("Channel is not opened"); |
| 419 |
} |
| 420 |
DoWrite(message, true); |
| 421 |
} |
| 422 |
#endif |