Line data Source code
1 : /*! 2 : * \file esys/repo/libssh2/sshimpl_libssh2.cpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2021 Michel Gillet 9 : * Distributed under the MIT License. 10 : * (See accompanying file LICENSE.txt or 11 : * copy at https://opensource.org/licenses/MIT) 12 : * 13 : * __legal_e__ 14 : * \endcond 15 : * 16 : */ 17 : 18 : #include "esys/repo/esysrepo_prec.h" 19 : #include "esys/repo/libssh2/sshimpl.h" 20 : #include "esys/repo/libssh2/ssh.h" 21 : 22 : #include <sstream> 23 : 24 : namespace esys::repo::libssh2 25 : { 26 : 27 : std::unique_ptr<SSHImpl::InitRelease> SSHImpl::s_init_release; 28 : 29 204 : SSHImpl::SSHImpl(SSH *self) 30 204 : : m_self(self) 31 : { 32 204 : if (s_init_release == nullptr) s_init_release = std::make_unique<SSHImpl::InitRelease>(); 33 204 : } 34 : 35 408 : SSHImpl::~SSHImpl() = default; 36 : 37 1 : bool SSHImpl::is_agent_present() 38 : { 39 1 : std::lock_guard<std::mutex> lock(m_mutex); 40 : 41 1 : LIBSSH2_SESSION *session = libssh2_session_init(); 42 1 : LIBSSH2_AGENT *agent = libssh2_agent_init(session); 43 : 44 1 : if (!self()->get_agent_identity_path().empty()) 45 1 : libssh2_agent_set_identity_path(agent, self()->get_agent_identity_path().c_str()); 46 : 47 1 : int error = libssh2_agent_connect(agent); 48 1 : if (error != LIBSSH2_ERROR_NONE) 49 : { 50 0 : char *msg = nullptr; 51 0 : libssh2_session_last_error(session, &msg, nullptr, 0); 52 : 53 0 : std::ostringstream oss; 54 0 : oss << "agent error (" << error << ") : " << msg << std::endl; 55 0 : self()->error(oss.str()); 56 0 : } 57 : // else 58 : // self()->debug(0, "SSH agent detected"); 59 : 60 1 : libssh2_agent_disconnect(agent); 61 1 : libssh2_agent_free(agent); 62 1 : libssh2_session_free(session); 63 : 64 1 : return (error == LIBSSH2_ERROR_NONE); 65 1 : } 66 : 67 2 : SSH *SSHImpl::self() 68 : { 69 2 : return m_self; 70 : } 71 : 72 : } // namespace esys::repo::libssh2