00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifdef ENABLE_SSH
00016
00017 #include <globalsearch/sshmanager_libssh.h>
00018
00019 #include <globalsearch/macros.h>
00020 #include <globalsearch/sshconnection_libssh.h>
00021
00022 #include <QtCore/QDebug>
00023
00024 #define START //qDebug() << __PRETTY_FUNCTION__ << " called...";
00025 #define END //qDebug() << __PRETTY_FUNCTION__ << " finished...";
00026
00027 using namespace std;
00028
00029 namespace GlobalSearch {
00030
00031 SSHManagerLibSSH::SSHManagerLibSSH(unsigned int connections, OptBase *parent)
00032 : SSHManager(parent),
00033 m_connSemaphore(connections),
00034 m_connections(connections),
00035 m_isValid(false)
00036 {
00037 for (unsigned int i = 0; i < connections; i++) {
00038 m_conns.append(new SSHConnectionLibSSH(this));
00039
00040 }
00041 }
00042
00043 void SSHManagerLibSSH::makeConnections(const QString &host,
00044 const QString &user,
00045 const QString &pass,
00046 unsigned int port)
00047 {
00048 m_isValid = false;
00049 QMutexLocker locker (&m_lock);
00050 START;
00051
00052 m_host = host;
00053 m_user = user;
00054 m_pass = pass;
00055 m_port = port;
00056
00057 QList<SSHConnectionLibSSH*>::iterator it;
00058 for (it = m_conns.begin(); it != m_conns.end(); it++) {
00059 (*it)->setLoginDetails(m_host, m_user, m_pass, m_port);
00060
00061
00062 (*it)->connectSession(true);
00063 }
00064
00065 m_isValid = true;
00066 END;
00067 }
00068
00069 SSHManagerLibSSH::~SSHManagerLibSSH()
00070 {
00071 QMutexLocker locker (&m_lock);
00072 START;
00073
00074 QList<SSHConnectionLibSSH*>::iterator it;
00075 for (it = m_conns.begin(); it != m_conns.end(); it++) {
00076 while ((*it)->inUse()) {
00077
00078 qDebug() << "Spinning while waiting for SSHConnection to free."
00079 << *it;
00080 GS_SLEEP(1);
00081 }
00082 (*it)->setUsed(true);
00083 delete (*it);
00084 (*it) = 0;
00085 }
00086
00087 END;
00088 }
00089
00090 SSHConnection* SSHManagerLibSSH::getFreeConnection()
00091 {
00092
00093 m_connSemaphore.acquire();
00094
00095
00096
00097 QMutexLocker locker (&m_lock);
00098
00099 START;
00100
00101 QList<SSHConnectionLibSSH*>::iterator it;
00102 for (it = m_conns.begin(); it != m_conns.end(); it++) {
00103 if ((*it) && !(*it)->inUse()) {
00104 (*it)->setUsed(true);
00105
00106 if (!(*it)->reconnectIfNeeded()) {
00107 qWarning() << tr("Cannot connect to ssh server %1@%2:%3")
00108 .arg((*it)->getUser())
00109 .arg((*it)->getHost())
00110 .arg((*it)->getPort());
00111 return NULL;
00112 }
00113 END;
00114 return (*it);
00115 }
00116 }
00117
00118
00119 Q_ASSERT_X(false, Q_FUNC_INFO,
00120 "No SSHConnections available. This should not "
00121 "happen with the protection provided by "
00122 "m_connSemaphore. Is SSHManagerLibSSH::unlockConnection "
00123 "being called correctly?");
00124
00125
00126 m_connSemaphore.release();
00127
00128 return this->getFreeConnection();
00129 }
00130
00131 void SSHManagerLibSSH::unlockConnection(SSHConnection* ssh)
00132 {
00133 SSHConnectionLibSSH *libsshConn = qobject_cast<SSHConnectionLibSSH*>(ssh);
00134 Q_ASSERT_X(m_conns.contains(libsshConn), Q_FUNC_INFO,
00135 "Attempt to unlock an SSHConnectionLibSSH not owned by this "
00136 "SSHManagerLibSSH.");
00137 Q_ASSERT(libsshConn->inUse());
00138
00139 m_lock.lock();
00140 libsshConn->setUsed(false);
00141 m_connSemaphore.release();
00142 m_lock.unlock();
00143 }
00144
00145 QString SSHManagerLibSSH::getServerKeyHash()
00146 {
00147 return m_hexa;
00148 }
00149
00150 bool SSHManagerLibSSH::validateServerKey()
00151 {
00152 return SSHConnectionLibSSH::addKeyToKnownHosts(m_host, m_port);
00153 }
00154
00155 void SSHManagerLibSSH::setServerKey(const QString &hexa)
00156 {
00157 m_hexa = hexa;
00158 }
00159
00160 }
00161
00162 #endif // ENABLE_SSH