| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : cl_ssh.cpp |
| 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 |
#if USE_SFTP |
| 27 |
#include "StringUtils.h" |
| 28 |
#include "file_logger.h" |
| 29 |
|
| 30 |
#include <wx/string.h> |
| 31 |
#include <wx/textdlg.h> |
| 32 |
#include <wx/thread.h> |
| 33 |
#include <wx/translation.h> |
| 34 |
#ifdef __WXMSW__ |
| 35 |
#include "wx/msw/winundef.h" |
| 36 |
#endif |
| 37 |
#include "cl_ssh.h" |
| 38 |
|
| 39 |
#include <libssh/libssh.h> |
| 40 |
|
| 41 |
wxDEFINE_EVENT(wxEVT_SSH_COMMAND_OUTPUT, clCommandEvent); |
| 42 |
wxDEFINE_EVENT(wxEVT_SSH_COMMAND_COMPLETED, clCommandEvent); |
| 43 |
wxDEFINE_EVENT(wxEVT_SSH_COMMAND_ERROR, clCommandEvent); |
| 44 |
wxDEFINE_EVENT(wxEVT_SSH_CONNECTED, clCommandEvent); |
| 45 |
|
| 46 |
clSSH::clSSH(const wxString& host, const wxString& user, const wxString& pass, int port) |
| 47 |
: m_host(host) |
| 48 |
, m_username(user) |
| 49 |
, m_password(pass) |
| 50 |
, m_port(port) |
| 51 |
, m_connected(false) |
| 52 |
, m_session(NULL) |
| 53 |
, m_channel(NULL) |
| 54 |
, m_timer(NULL) |
| 55 |
, m_owner(NULL) |
| 56 |
{ |
| 57 |
m_timer = new wxTimer(this); |
| 58 |
Bind(wxEVT_TIMER, &clSSH::OnCheckRemoteOutut, this, m_timer->GetId()); |
| 59 |
} |
| 60 |
|
| 61 |
clSSH::clSSH() |
| 62 |
: m_port(22) |
| 63 |
, m_connected(false) |
| 64 |
, m_session(NULL) |
| 65 |
, m_channel(NULL) |
| 66 |
, m_timer(NULL) |
| 67 |
, m_owner(NULL) |
| 68 |
{ |
| 69 |
m_timer = new wxTimer(this); |
| 70 |
Bind(wxEVT_TIMER, &clSSH::OnCheckRemoteOutut, this, m_timer->GetId()); |
| 71 |
} |
| 72 |
|
| 73 |
clSSH::~clSSH() { Close(); } |
| 74 |
|
| 75 |
void clSSH::Connect(int seconds) |
| 76 |
{ |
| 77 |
// Start ssh-agent before we attempt to connect |
| 78 |
m_sshAgent.reset(new clSSHAgent()); |
| 79 |
|
| 80 |
m_session = ssh_new(); |
| 81 |
if(!m_session) { |
| 82 |
throw clException("ssh_new failed!"); |
| 83 |
} |
| 84 |
|
| 85 |
ssh_set_blocking(m_session, 0); |
| 86 |
int verbosity = SSH_LOG_NOLOG; |
| 87 |
std::string host = StringUtils::ToStdString(m_host); |
| 88 |
std::string user = StringUtils::ToStdString(GetUsername()); |
| 89 |
ssh_options_set(m_session, SSH_OPTIONS_HOST, host.c_str()); |
| 90 |
ssh_options_set(m_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 91 |
ssh_options_set(m_session, SSH_OPTIONS_PORT, &m_port); |
| 92 |
ssh_options_set(m_session, SSH_OPTIONS_USER, user.c_str()); |
| 93 |
|
| 94 |
// Connect the session |
| 95 |
int retries = seconds * 100; |
| 96 |
if(retries < 0) { |
| 97 |
retries = 1; |
| 98 |
} |
| 99 |
DoConnectWithRetries(retries); |
| 100 |
ssh_set_blocking(m_session, 1); |
| 101 |
} |
| 102 |
|
| 103 |
bool clSSH::AuthenticateServer(wxString& message) |
| 104 |
{ |
| 105 |
int state; |
| 106 |
unsigned char* hash = NULL; |
| 107 |
char* hexa = NULL; |
| 108 |
|
| 109 |
message.Clear(); |
| 110 |
|
| 111 |
#if LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 6, 5) |
| 112 |
int hlen = 0; |
| 113 |
hlen = ssh_get_pubkey_hash(m_session, &hash); |
| 114 |
if(hlen < 0) { |
| 115 |
throw clException("Unable to obtain server public key!"); |
| 116 |
} |
| 117 |
#else |
| 118 |
size_t hlen = 0; |
| 119 |
ssh_key key = NULL; |
| 120 |
#if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 8, 0) |
| 121 |
ssh_get_server_publickey(m_session, &key); |
| 122 |
#else |
| 123 |
ssh_get_publickey(m_session, &key); |
| 124 |
#endif |
| 125 |
ssh_get_publickey_hash(key, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen); |
| 126 |
if(hlen == 0) { |
| 127 |
throw clException("Unable to obtain server public key!"); |
| 128 |
} |
| 129 |
#endif |
| 130 |
|
| 131 |
#if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 8, 0) |
| 132 |
state = ssh_session_is_known_server(m_session); |
| 133 |
#else |
| 134 |
state = ssh_is_server_known(m_session); |
| 135 |
#endif |
| 136 |
switch(state) { |
| 137 |
case SSH_SERVER_KNOWN_OK: |
| 138 |
free(hash); |
| 139 |
return true; |
| 140 |
|
| 141 |
case SSH_SERVER_KNOWN_CHANGED: |
| 142 |
hexa = ssh_get_hexa(hash, hlen); |
| 143 |
message << _("Host key for server changed: it is now:\n") << hexa << "\n" << _("Accept server authentication?"); |
| 144 |
free(hexa); |
| 145 |
free(hash); |
| 146 |
return false; |
| 147 |
|
| 148 |
case SSH_SERVER_FOUND_OTHER: |
| 149 |
message << _("The host key for this server was not found but another type of key exists.\n") |
| 150 |
<< _("An attacker might change the default server key to confuse your client into thinking the key " |
| 151 |
"does not exist\n") |
| 152 |
<< _("Accept server authentication?"); |
| 153 |
free(hash); |
| 154 |
return false; |
| 155 |
|
| 156 |
case SSH_SERVER_FILE_NOT_FOUND: |
| 157 |
message << _("Could not find known host file.\n") |
| 158 |
<< _("If you accept the host key here, the file will be automatically created.\n"); |
| 159 |
/* fallback to SSH_SERVER_NOT_KNOWN behavior */ |
| 160 |
case SSH_SERVER_NOT_KNOWN: |
| 161 |
hexa = ssh_get_hexa(hash, hlen); |
| 162 |
message << _("The server is unknown. Do you trust the host key?\n") << _("Public key hash: ") << hexa << "\n" |
| 163 |
<< _("Accept server authentication?"); |
| 164 |
free(hexa); |
| 165 |
free(hash); |
| 166 |
return false; |
| 167 |
|
| 168 |
default: |
| 169 |
case SSH_SERVER_ERROR: |
| 170 |
throw clException(wxString() << "An error occurred: " << ssh_get_error(m_session)); |
| 171 |
} |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
void clSSH::AcceptServerAuthentication() |
| 176 |
{ |
| 177 |
if(!m_session) { |
| 178 |
throw clException("NULL SSH session"); |
| 179 |
} |
| 180 |
#if LIBSSH_VERSION_INT >= SSH_VERSION_INT(0, 8, 0) |
| 181 |
ssh_session_update_known_hosts(m_session); |
| 182 |
#else |
| 183 |
ssh_write_knownhost(m_session); |
| 184 |
#endif |
| 185 |
} |
| 186 |
|
| 187 |
#define THROW_OR_FALSE(msg) \ |
| 188 |
if(throwExc) { \ |
| 189 |
throw clException(msg); \ |
| 190 |
} \ |
| 191 |
return false; |
| 192 |
|
| 193 |
bool clSSH::LoginAuthNone(bool throwExc) |
| 194 |
{ |
| 195 |
clDEBUG() << "Trying to ssh using `ssh_userauth_none`" << endl; |
| 196 |
if(!m_session) { |
| 197 |
THROW_OR_FALSE("NULL SSH session"); |
| 198 |
} |
| 199 |
|
| 200 |
int rc; |
| 201 |
// interactive keyboard method failed, try another method |
| 202 |
auto username = StringUtils::ToStdString(GetUsername()); |
| 203 |
rc = ssh_userauth_none(m_session, username.c_str()); |
| 204 |
if(rc == SSH_AUTH_SUCCESS) { |
| 205 |
return true; |
| 206 |
} |
| 207 |
THROW_OR_FALSE(_("ssh_userauth_none failed")); |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
bool clSSH::LoginPassword(bool throwExc) |
| 212 |
{ |
| 213 |
if(!m_session) { |
| 214 |
THROW_OR_FALSE("NULL SSH session"); |
| 215 |
} |
| 216 |
|
| 217 |
int rc; |
| 218 |
// interactive keyboard method failed, try another method |
| 219 |
rc = ssh_userauth_password(m_session, NULL, GetPassword().mb_str().data()); |
| 220 |
if(rc == SSH_AUTH_SUCCESS) { |
| 221 |
return true; |
| 222 |
|
| 223 |
} else if(rc == SSH_AUTH_DENIED) { |
| 224 |
THROW_OR_FALSE(_("Login failed: invalid username/password")); |
| 225 |
|
| 226 |
} else { |
| 227 |
THROW_OR_FALSE(wxString() << _("Authentication error: ") << ssh_get_error(m_session)); |
| 228 |
} |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
bool clSSH::LoginInteractiveKBD(bool throwExc) |
| 233 |
{ |
| 234 |
if(!m_session) { |
| 235 |
THROW_OR_FALSE("NULL SSH session"); |
| 236 |
} |
| 237 |
|
| 238 |
int rc; |
| 239 |
rc = ssh_userauth_kbdint(m_session, NULL, NULL); |
| 240 |
if(rc == SSH_AUTH_INFO) { |
| 241 |
while(rc == SSH_AUTH_INFO) { |
| 242 |
const char *name, *instruction; |
| 243 |
int nprompts, iprompt; |
| 244 |
name = ssh_userauth_kbdint_getname(m_session); |
| 245 |
instruction = ssh_userauth_kbdint_getinstruction(m_session); |
| 246 |
nprompts = ssh_userauth_kbdint_getnprompts(m_session); |
| 247 |
wxUnusedVar(name); |
| 248 |
wxUnusedVar(instruction); |
| 249 |
for(iprompt = 0; iprompt < nprompts; iprompt++) { |
| 250 |
const char* prompt; |
| 251 |
char echo; |
| 252 |
prompt = ssh_userauth_kbdint_getprompt(m_session, iprompt, &echo); |
| 253 |
if(echo) { |
| 254 |
wxString answer = ::wxGetTextFromUser(prompt, "SSH"); |
| 255 |
if(answer.IsEmpty()) { |
| 256 |
THROW_OR_FALSE(wxString() << "Login error: " << ssh_get_error(m_session)); |
| 257 |
} |
| 258 |
if(ssh_userauth_kbdint_setanswer(m_session, iprompt, answer.mb_str(wxConvUTF8).data()) < 0) { |
| 259 |
THROW_OR_FALSE(wxString() << "Login error: " << ssh_get_error(m_session)); |
| 260 |
} |
| 261 |
} else { |
| 262 |
if(ssh_userauth_kbdint_setanswer(m_session, iprompt, GetPassword().mb_str(wxConvUTF8).data()) < 0) { |
| 263 |
THROW_OR_FALSE(wxString() << "Login error: " << ssh_get_error(m_session)); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
rc = ssh_userauth_kbdint(m_session, NULL, NULL); |
| 268 |
} |
| 269 |
return true; // success |
| 270 |
} |
| 271 |
THROW_OR_FALSE(_("Interactive Keyboard is not enabled for this server")); |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
bool clSSH::LoginPublicKey(bool throwExc) |
| 276 |
{ |
| 277 |
if(!m_session) { |
| 278 |
THROW_OR_FALSE("NULL SSH session"); |
| 279 |
} |
| 280 |
|
| 281 |
int rc; |
| 282 |
rc = ssh_userauth_publickey_auto(m_session, nullptr, nullptr); |
| 283 |
if(rc != SSH_AUTH_SUCCESS) { |
| 284 |
THROW_OR_FALSE(wxString() << _("Public Key error: ") << ssh_get_error(m_session)); |
| 285 |
} |
| 286 |
return true; |
| 287 |
} |
| 288 |
|
| 289 |
void clSSH::Close() |
| 290 |
{ |
| 291 |
m_timer->Stop(); |
| 292 |
Unbind(wxEVT_TIMER, &clSSH::OnCheckRemoteOutut, this, m_timer->GetId()); |
| 293 |
wxDELETE(m_timer); |
| 294 |
|
| 295 |
DoCloseChannel(); |
| 296 |
|
| 297 |
if(m_session && m_connected) { |
| 298 |
ssh_disconnect(m_session); |
| 299 |
} |
| 300 |
|
| 301 |
if(m_session) { |
| 302 |
ssh_free(m_session); |
| 303 |
} |
| 304 |
|
| 305 |
m_connected = false; |
| 306 |
m_session = NULL; |
| 307 |
m_channel = NULL; |
| 308 |
} |
| 309 |
|
| 310 |
void clSSH::Login() |
| 311 |
{ |
| 312 |
int rc; |
| 313 |
|
| 314 |
rc = ssh_userauth_none(m_session, NULL); |
| 315 |
if(rc == SSH_AUTH_SUCCESS) { |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
std::string username = StringUtils::ToStdString(GetUsername()); |
| 320 |
ssh_options_set(m_session, SSH_OPTIONS_USER, username.c_str()); |
| 321 |
|
| 322 |
// Try all the known methods |
| 323 |
std::vector<decltype(&clSSH::LoginPublicKey)> methods; |
| 324 |
methods.reserve(4); |
| 325 |
|
| 326 |
methods.push_back(&clSSH::LoginPublicKey); |
| 327 |
methods.push_back(&clSSH::LoginPassword); |
| 328 |
methods.push_back(&clSSH::LoginInteractiveKBD); |
| 329 |
methods.push_back(&clSSH::LoginAuthNone); |
| 330 |
|
| 331 |
bool authenticated = false; |
| 332 |
for(auto func : methods) { |
| 333 |
try { |
| 334 |
(this->*func)(true); |
| 335 |
} catch(clException& e) { |
| 336 |
clDEBUG() << "failed to authenticate." << e.What() << endl; |
| 337 |
continue; |
| 338 |
} |
| 339 |
authenticated = true; |
| 340 |
break; |
| 341 |
} |
| 342 |
if(!authenticated) { |
| 343 |
throw clException("Unable to login to server"); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
void clSSH::ExecuteShellCommand(wxEvtHandler* owner, const wxString& command) |
| 348 |
{ |
| 349 |
DoOpenChannel(); |
| 350 |
|
| 351 |
m_owner = owner; |
| 352 |
if(!m_owner) { |
| 353 |
throw clException(wxString() << "No owner specified for output"); |
| 354 |
} |
| 355 |
|
| 356 |
wxCharBuffer buffer = command.mb_str(wxConvUTF8); |
| 357 |
int rc = ssh_channel_write(m_channel, buffer.data(), buffer.length()); |
| 358 |
if(rc != (int)buffer.length()) { |
| 359 |
throw clException("SSH Socket error"); |
| 360 |
} |
| 361 |
|
| 362 |
// Start a timer to check for the output on 10ms intervals |
| 363 |
if(!m_timer->IsRunning()) { |
| 364 |
m_timer->Start(50); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
void clSSH::OnCheckRemoteOutut(wxTimerEvent& event) |
| 369 |
{ |
| 370 |
if(!m_channel) |
| 371 |
return; |
| 372 |
|
| 373 |
char buffer[1024]; |
| 374 |
int nbytes = ssh_channel_read_nonblocking(m_channel, buffer, sizeof(buffer), 0); |
| 375 |
if(nbytes > 0) { |
| 376 |
wxString strOutput = wxString::FromUTF8((const char*)buffer, nbytes); |
| 377 |
clCommandEvent sshEvent(wxEVT_SSH_COMMAND_OUTPUT); |
| 378 |
sshEvent.SetString(strOutput); |
| 379 |
m_owner->AddPendingEvent(sshEvent); |
| 380 |
|
| 381 |
} else if(nbytes == SSH_ERROR) { |
| 382 |
m_timer->Stop(); |
| 383 |
DoCloseChannel(); |
| 384 |
clCommandEvent sshEvent(wxEVT_SSH_COMMAND_ERROR); |
| 385 |
sshEvent.SetString(ssh_get_error(m_session)); |
| 386 |
m_owner->AddPendingEvent(sshEvent); |
| 387 |
|
| 388 |
} else { |
| 389 |
// nbytes == 0 |
| 390 |
if(ssh_channel_is_eof(m_channel)) { |
| 391 |
m_timer->Stop(); |
| 392 |
DoCloseChannel(); |
| 393 |
// EOF was sent, nothing more to read |
| 394 |
clCommandEvent sshEvent(wxEVT_SSH_COMMAND_COMPLETED); |
| 395 |
m_owner->AddPendingEvent(sshEvent); |
| 396 |
} else { |
| 397 |
// Nothing to read, no error |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
void clSSH::DoCloseChannel() |
| 403 |
{ |
| 404 |
// Properly close the channel |
| 405 |
if(m_channel) { |
| 406 |
ssh_channel_close(m_channel); |
| 407 |
ssh_channel_send_eof(m_channel); |
| 408 |
ssh_channel_free(m_channel); |
| 409 |
} |
| 410 |
m_channel = NULL; |
| 411 |
} |
| 412 |
|
| 413 |
void clSSH::DoOpenChannel() |
| 414 |
{ |
| 415 |
if(m_channel) |
| 416 |
return; |
| 417 |
|
| 418 |
m_channel = ssh_channel_new(m_session); |
| 419 |
if(!m_channel) { |
| 420 |
throw clException(ssh_get_error(m_session)); |
| 421 |
} |
| 422 |
|
| 423 |
int rc = ssh_channel_open_session(m_channel); |
| 424 |
if(rc != SSH_OK) { |
| 425 |
throw clException(ssh_get_error(m_session)); |
| 426 |
} |
| 427 |
|
| 428 |
rc = ssh_channel_request_pty(m_channel); |
| 429 |
if(rc != SSH_OK) { |
| 430 |
throw clException(ssh_get_error(m_session)); |
| 431 |
} |
| 432 |
|
| 433 |
rc = ssh_channel_change_pty_size(m_channel, 80, 24); |
| 434 |
if(rc != SSH_OK) { |
| 435 |
throw clException(ssh_get_error(m_session)); |
| 436 |
} |
| 437 |
|
| 438 |
rc = ssh_channel_request_shell(m_channel); |
| 439 |
if(rc != SSH_OK) { |
| 440 |
throw clException(ssh_get_error(m_session)); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
void clSSH::DoConnectWithRetries(int retries) |
| 445 |
{ |
| 446 |
while(retries) { |
| 447 |
int rc = ssh_connect(m_session); |
| 448 |
if(rc == SSH_AGAIN) { |
| 449 |
if(wxThread::IsMain()) { |
| 450 |
::wxSafeYield(); |
| 451 |
} |
| 452 |
wxThread::Sleep(10); |
| 453 |
--retries; |
| 454 |
continue; |
| 455 |
} |
| 456 |
if(rc == SSH_OK) { |
| 457 |
m_connected = true; |
| 458 |
return; |
| 459 |
} else { |
| 460 |
throw clException(ssh_get_error(m_session)); |
| 461 |
} |
| 462 |
} |
| 463 |
throw clException("Connect timeout"); |
| 464 |
} |
| 465 |
|
| 466 |
void clSSH::SendIgnore() |
| 467 |
{ |
| 468 |
if(!m_session) { |
| 469 |
throw clException("Session not opened"); |
| 470 |
} |
| 471 |
if(ssh_send_ignore(m_session, "ping") != SSH_OK) { |
| 472 |
throw clException("Failed to send ignore message"); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
#endif // USE_SFTP |