From c59a668832519b2f713366cac221715d609b5fff Mon Sep 17 00:00:00 2001 From: oblivion Date: Sat, 12 Oct 2019 22:54:29 +0000 Subject: [PATCH] SSH: SshSession - Transport method prefrerences handling is fixed. git-svn-id: svn://ultimatepp.org/upp/trunk@13637 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/SSH/Session.cpp | 34 ++++++++++++++++++++++------------ uppsrc/Core/SSH/Session.h | 2 +- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/uppsrc/Core/SSH/Session.cpp b/uppsrc/Core/SSH/Session.cpp index 54ea13277..465376288 100644 --- a/uppsrc/Core/SSH/Session.cpp +++ b/uppsrc/Core/SSH/Session.cpp @@ -175,14 +175,19 @@ bool SshSession::Connect(const String& host, int port, const String& user, const return true; })) goto Bailout; - if(!Run([=] () mutable { - if(session->iomethods.IsEmpty()) - return true; - int rc = libssh2_session_method_pref(ssh->session, session->iomethods.GetKey(0), ~GetMethodNames(0)); + while(!session->iomethods.IsEmpty()) { + if(!Run([=] () mutable { + int method = session->iomethods.GetKey(0); + String mnames = GetMethodNames(method); + int rc = libssh2_session_method_pref(ssh->session, method, ~mnames); if(!WouldBlock(rc) && rc < 0) SetError(rc); - if(!rc) LLOG("Transport methods are successfully set."); + if(!rc && !session->iomethods.IsEmpty()) { + LLOG("Transport method: #" << method << " is set to [" << mnames << "]"); + session->iomethods.Remove(0); + } return !rc; - })) goto Bailout; + })) goto Bailout; + } if(!Run([=] () mutable { int rc = libssh2_session_handshake(ssh->session, session->socket.GetSOCKET()); @@ -196,8 +201,10 @@ bool SshSession::Connect(const String& host, int port, const String& user, const if(!Run([=] () mutable { session->fingerprint = libssh2_hostkey_hash(ssh->session, session->hashtype); - if(session->fingerprint.IsEmpty()) LLOG("Warning: Fingerprint is not available!."); - LDUMPHEX(session->fingerprint); + if(session->fingerprint.IsEmpty()) + LLOG("Warning: Fingerprint is not available!."); + else + LLOG("Fingerprint: " << HexString(session->fingerprint, 1, ':')); if(WhenVerify && !WhenVerify(host, port)) SetError(-1); return true; @@ -206,7 +213,7 @@ bool SshSession::Connect(const String& host, int port, const String& user, const if(!Run([=] () mutable { session->authmethods = libssh2_userauth_list(ssh->session, user, user.GetLength()); if(session->authmethods.IsEmpty()) { if(!WouldBlock()) SetError(-1); return false; } - LLOG("Authentication methods successfully retrieved."); + LLOG("Authentication methods list successfully retrieved: [" << session->authmethods << "]"); WhenAuth(); return true; })) goto Bailout; @@ -341,9 +348,12 @@ ValueMap SshSession::GetMethods() String SshSession::GetMethodNames(int type) { String names; - const auto& v = session->iomethods[type]; - for(int i = 0; i < v.GetCount(); i++) - names << v[i].To() << (i < v.GetCount() - 1 ? "," : ""); + const Value& v = session->iomethods[type]; + if(IsValueArray(v)) { + for(int i = 0; i < v.GetCount(); i++) + names << v[i].To() << (i < v.GetCount() - 1 ? "," : ""); + } + else names << v; return pick(names); } diff --git a/uppsrc/Core/SSH/Session.h b/uppsrc/Core/SSH/Session.h index b09597da8..60bb76ec1 100644 --- a/uppsrc/Core/SSH/Session.h +++ b/uppsrc/Core/SSH/Session.h @@ -29,7 +29,7 @@ public: SshSession& HashType(Hash h) { session->hashtype = h == HASH_SHA1 ? LIBSSH2_HOSTKEY_HASH_SHA1 : LIBSSH2_HOSTKEY_HASH_MD5; return *this; } SshSession& Keys(const String& prikey, const String& pubkey, const String& phrase, bool fromfile = true); - SshSession& Method(int type, Value method) { session->iomethods(type) = pick(method); return *this; } + SshSession& Method(int type, Value method) { session->iomethods(type) << pick(method); return *this; } SshSession& Methods(ValueMap methods) { session->iomethods = pick(methods); return *this; } SshSession& PasswordAuth() { session->authmethod = PASSWORD; return *this; }