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 203 : SSHImpl::SSHImpl(SSH *self) 30 203 : : m_self(self) 31 : { 32 203 : if (s_init_release == nullptr) s_init_release = std::make_unique<SSHImpl::InitRelease>(); 33 203 : } 34 : 35 406 : SSHImpl::~SSHImpl() = default; 36 : 37 139 : bool SSHImpl::is_agent_present() 38 : { 39 139 : std::lock_guard<std::mutex> lock(m_mutex); 40 : 41 139 : LIBSSH2_SESSION *session = libssh2_session_init(); 42 139 : LIBSSH2_AGENT *agent = libssh2_agent_init(session); 43 : 44 139 : if (!self()->get_agent_identity_path().empty()) 45 139 : libssh2_agent_set_identity_path(agent, self()->get_agent_identity_path().c_str()); 46 : 47 139 : int error = libssh2_agent_connect(agent); 48 139 : 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 139 : libssh2_agent_disconnect(agent); 61 139 : libssh2_agent_free(agent); 62 139 : libssh2_session_free(session); 63 : 64 139 : return (error == LIBSSH2_ERROR_NONE); 65 139 : } 66 : 67 278 : SSH *SSHImpl::self() 68 : { 69 278 : return m_self; 70 : } 71 : 72 : } // namespace esys::repo::libssh2