2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-07-12 09:33:22 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-07-12 09:33:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-07-12 09:33:22 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:58:39 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-07-12 09:33:22 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-07-12 09:33:22 +02:00
|
|
|
|
|
|
|
|
#include "sshchannelmanager_p.h"
|
|
|
|
|
|
|
|
|
|
#include "sftpchannel.h"
|
|
|
|
|
#include "sftpchannel_p.h"
|
2012-06-19 13:03:48 +02:00
|
|
|
#include "sshdirecttcpiptunnel.h"
|
|
|
|
|
#include "sshdirecttcpiptunnel_p.h"
|
2016-03-31 18:57:03 +02:00
|
|
|
#include "sshforwardedtcpiptunnel.h"
|
|
|
|
|
#include "sshforwardedtcpiptunnel_p.h"
|
2010-07-12 09:33:22 +02:00
|
|
|
#include "sshincomingpacket_p.h"
|
2016-03-31 18:57:03 +02:00
|
|
|
#include "sshlogging_p.h"
|
2010-07-12 09:33:22 +02:00
|
|
|
#include "sshremoteprocess.h"
|
|
|
|
|
#include "sshremoteprocess_p.h"
|
|
|
|
|
#include "sshsendfacility_p.h"
|
2016-03-31 18:57:03 +02:00
|
|
|
#include "sshtcpipforwardserver.h"
|
|
|
|
|
#include "sshtcpipforwardserver_p.h"
|
2010-07-12 09:33:22 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QList>
|
2010-07-12 09:33:22 +02:00
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
namespace QSsh {
|
2010-07-12 09:33:22 +02:00
|
|
|
namespace Internal {
|
|
|
|
|
|
2010-08-16 22:10:34 +02:00
|
|
|
SshChannelManager::SshChannelManager(SshSendFacility &sendFacility,
|
|
|
|
|
QObject *parent)
|
|
|
|
|
: QObject(parent), m_sendFacility(sendFacility), m_nextLocalChannelId(0)
|
2010-07-12 09:33:22 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleChannelRequest(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
lookupChannel(packet.extractRecipientChannel())
|
|
|
|
|
->handleChannelRequest(packet);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 18:57:03 +02:00
|
|
|
void SshChannelManager::handleChannelOpen(const SshIncomingPacket &packet)
|
2010-07-12 09:33:22 +02:00
|
|
|
{
|
2016-03-31 18:57:03 +02:00
|
|
|
SshChannelOpen channelOpen = packet.extractChannelOpen();
|
|
|
|
|
|
|
|
|
|
SshTcpIpForwardServer::Ptr server;
|
|
|
|
|
|
|
|
|
|
foreach (const SshTcpIpForwardServer::Ptr &candidate, m_listeningForwardServers) {
|
|
|
|
|
if (candidate->port() == channelOpen.remotePort
|
|
|
|
|
&& candidate->bindAddress().toUtf8() == channelOpen.remoteAddress) {
|
|
|
|
|
server = candidate;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (server.isNull()) {
|
|
|
|
|
// Apparently the server knows a remoteAddress we are not aware of. There are plenty of ways
|
|
|
|
|
// to make that happen: /etc/hosts on the server, different writings for localhost,
|
|
|
|
|
// different DNS servers, ...
|
|
|
|
|
// Rather than trying to figure that out, we just use the first listening forwarder with the
|
|
|
|
|
// same port.
|
|
|
|
|
foreach (const SshTcpIpForwardServer::Ptr &candidate, m_listeningForwardServers) {
|
|
|
|
|
if (candidate->port() == channelOpen.remotePort) {
|
|
|
|
|
server = candidate;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (server.isNull()) {
|
|
|
|
|
SshOpenFailureType reason = (channelOpen.remotePort == 0) ?
|
|
|
|
|
SSH_OPEN_UNKNOWN_CHANNEL_TYPE : SSH_OPEN_ADMINISTRATIVELY_PROHIBITED;
|
|
|
|
|
try {
|
|
|
|
|
m_sendFacility.sendChannelOpenFailurePacket(channelOpen.remoteChannel, reason,
|
|
|
|
|
QByteArray());
|
|
|
|
|
} catch (const Botan::Exception &e) {
|
|
|
|
|
qCWarning(sshLog, "Botan error: %s", e.what());
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SshForwardedTcpIpTunnel::Ptr tunnel(new SshForwardedTcpIpTunnel(m_nextLocalChannelId++,
|
|
|
|
|
m_sendFacility));
|
|
|
|
|
tunnel->d->handleOpenSuccess(channelOpen.remoteChannel, channelOpen.remoteWindowSize,
|
|
|
|
|
channelOpen.remoteMaxPacketSize);
|
|
|
|
|
tunnel->open(QIODevice::ReadWrite);
|
|
|
|
|
server->setNewConnection(tunnel);
|
|
|
|
|
insertChannel(tunnel->d, tunnel);
|
2010-07-12 09:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleChannelOpenFailure(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
const SshChannelOpenFailure &failure = packet.extractChannelOpenFailure();
|
|
|
|
|
ChannelIterator it = lookupChannelAsIterator(failure.localChannel);
|
|
|
|
|
try {
|
|
|
|
|
it.value()->handleOpenFailure(failure.reasonString);
|
2014-11-28 11:15:37 +01:00
|
|
|
} catch (const SshServerException &e) {
|
2010-07-12 09:33:22 +02:00
|
|
|
removeChannel(it);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
removeChannel(it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleChannelOpenConfirmation(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
const SshChannelOpenConfirmation &confirmation
|
|
|
|
|
= packet.extractChannelOpenConfirmation();
|
|
|
|
|
lookupChannel(confirmation.localChannel)->handleOpenSuccess(confirmation.remoteChannel,
|
|
|
|
|
confirmation.remoteWindowSize, confirmation.remoteMaxPacketSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleChannelSuccess(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
lookupChannel(packet.extractRecipientChannel())->handleChannelSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleChannelFailure(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
lookupChannel(packet.extractRecipientChannel())->handleChannelFailure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleChannelWindowAdjust(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
const SshChannelWindowAdjust adjust = packet.extractWindowAdjust();
|
|
|
|
|
lookupChannel(adjust.localChannel)->handleWindowAdjust(adjust.bytesToAdd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleChannelData(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
const SshChannelData &data = packet.extractChannelData();
|
|
|
|
|
lookupChannel(data.localChannel)->handleChannelData(data.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleChannelExtendedData(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
const SshChannelExtendedData &data = packet.extractChannelExtendedData();
|
|
|
|
|
lookupChannel(data.localChannel)->handleChannelExtendedData(data.type, data.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleChannelEof(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
AbstractSshChannel * const channel
|
|
|
|
|
= lookupChannel(packet.extractRecipientChannel(), true);
|
|
|
|
|
if (channel)
|
|
|
|
|
channel->handleChannelEof();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleChannelClose(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
const quint32 channelId = packet.extractRecipientChannel();
|
|
|
|
|
|
|
|
|
|
ChannelIterator it = lookupChannelAsIterator(channelId, true);
|
|
|
|
|
if (it != m_channels.end()) {
|
|
|
|
|
it.value()->handleChannelClose();
|
|
|
|
|
removeChannel(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 18:57:03 +02:00
|
|
|
void SshChannelManager::handleRequestSuccess(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
if (m_waitingForwardServers.isEmpty()) {
|
|
|
|
|
throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR,
|
|
|
|
|
"Unexpected request success packet.",
|
|
|
|
|
tr("Unexpected request success packet."));
|
|
|
|
|
}
|
|
|
|
|
SshTcpIpForwardServer::Ptr server = m_waitingForwardServers.takeFirst();
|
|
|
|
|
if (server->state() == SshTcpIpForwardServer::Closing) {
|
|
|
|
|
server->setClosed();
|
|
|
|
|
} else if (server->state() == SshTcpIpForwardServer::Initializing) {
|
|
|
|
|
quint16 port = server->port();
|
|
|
|
|
if (port == 0)
|
|
|
|
|
port = packet.extractRequestSuccess().bindPort;
|
|
|
|
|
server->setListening(port);
|
|
|
|
|
m_listeningForwardServers.append(server);
|
|
|
|
|
} else {
|
|
|
|
|
QSSH_ASSERT(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshChannelManager::handleRequestFailure(const SshIncomingPacket &packet)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(packet);
|
|
|
|
|
if (m_waitingForwardServers.isEmpty()) {
|
|
|
|
|
throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR,
|
|
|
|
|
"Unexpected request failure packet.",
|
|
|
|
|
tr("Unexpected request failure packet."));
|
|
|
|
|
}
|
|
|
|
|
SshTcpIpForwardServer::Ptr tunnel = m_waitingForwardServers.takeFirst();
|
|
|
|
|
tunnel->setClosed();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 09:33:22 +02:00
|
|
|
SshChannelManager::ChannelIterator SshChannelManager::lookupChannelAsIterator(quint32 channelId,
|
|
|
|
|
bool allowNotFound)
|
|
|
|
|
{
|
|
|
|
|
ChannelIterator it = m_channels.find(channelId);
|
|
|
|
|
if (it == m_channels.end() && !allowNotFound) {
|
|
|
|
|
throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR,
|
|
|
|
|
"Invalid channel id.",
|
2010-08-17 12:37:41 +02:00
|
|
|
tr("Invalid channel id %1").arg(channelId));
|
2010-07-12 09:33:22 +02:00
|
|
|
}
|
|
|
|
|
return it;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AbstractSshChannel *SshChannelManager::lookupChannel(quint32 channelId,
|
|
|
|
|
bool allowNotFound)
|
|
|
|
|
{
|
|
|
|
|
ChannelIterator it = lookupChannelAsIterator(channelId, allowNotFound);
|
|
|
|
|
return it == m_channels.end() ? 0 : it.value();
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
QSsh::SshRemoteProcess::Ptr SshChannelManager::createRemoteProcess(const QByteArray &command)
|
2010-07-12 09:33:22 +02:00
|
|
|
{
|
|
|
|
|
SshRemoteProcess::Ptr proc(new SshRemoteProcess(command, m_nextLocalChannelId++, m_sendFacility));
|
|
|
|
|
insertChannel(proc->d, proc);
|
|
|
|
|
return proc;
|
2011-07-26 18:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
QSsh::SshRemoteProcess::Ptr SshChannelManager::createRemoteShell()
|
2011-07-26 18:13:11 +02:00
|
|
|
{
|
|
|
|
|
SshRemoteProcess::Ptr proc(new SshRemoteProcess(m_nextLocalChannelId++, m_sendFacility));
|
|
|
|
|
insertChannel(proc->d, proc);
|
|
|
|
|
return proc;
|
2010-07-12 09:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
QSsh::SftpChannel::Ptr SshChannelManager::createSftpChannel()
|
2010-07-12 09:33:22 +02:00
|
|
|
{
|
|
|
|
|
SftpChannel::Ptr sftp(new SftpChannel(m_nextLocalChannelId++, m_sendFacility));
|
|
|
|
|
insertChannel(sftp->d, sftp);
|
|
|
|
|
return sftp;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 18:57:03 +02:00
|
|
|
SshDirectTcpIpTunnel::Ptr SshChannelManager::createDirectTunnel(const QString &originatingHost,
|
2015-04-14 15:47:55 +02:00
|
|
|
quint16 originatingPort, const QString &remoteHost, quint16 remotePort)
|
2012-06-19 13:03:48 +02:00
|
|
|
{
|
2015-04-14 15:47:55 +02:00
|
|
|
SshDirectTcpIpTunnel::Ptr tunnel(new SshDirectTcpIpTunnel(m_nextLocalChannelId++,
|
|
|
|
|
originatingHost, originatingPort, remoteHost, remotePort, m_sendFacility));
|
2012-06-19 13:03:48 +02:00
|
|
|
insertChannel(tunnel->d, tunnel);
|
|
|
|
|
return tunnel;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 18:57:03 +02:00
|
|
|
SshTcpIpForwardServer::Ptr SshChannelManager::createForwardServer(const QString &remoteHost,
|
|
|
|
|
quint16 remotePort)
|
|
|
|
|
{
|
|
|
|
|
SshTcpIpForwardServer::Ptr server(new SshTcpIpForwardServer(remoteHost, remotePort,
|
|
|
|
|
m_sendFacility));
|
|
|
|
|
connect(server.data(), &SshTcpIpForwardServer::stateChanged,
|
|
|
|
|
this, [this, server](SshTcpIpForwardServer::State state) {
|
|
|
|
|
switch (state) {
|
|
|
|
|
case SshTcpIpForwardServer::Closing:
|
|
|
|
|
m_listeningForwardServers.removeOne(server);
|
|
|
|
|
// fall through
|
|
|
|
|
case SshTcpIpForwardServer::Initializing:
|
|
|
|
|
m_waitingForwardServers.append(server);
|
|
|
|
|
break;
|
|
|
|
|
case SshTcpIpForwardServer::Listening:
|
|
|
|
|
case SshTcpIpForwardServer::Inactive:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return server;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 09:33:22 +02:00
|
|
|
void SshChannelManager::insertChannel(AbstractSshChannel *priv,
|
|
|
|
|
const QSharedPointer<QObject> &pub)
|
|
|
|
|
{
|
2016-06-07 22:04:26 +03:00
|
|
|
connect(priv, &AbstractSshChannel::timeout, this, &SshChannelManager::timeout);
|
2010-07-12 09:33:22 +02:00
|
|
|
m_channels.insert(priv->localChannelId(), priv);
|
|
|
|
|
m_sessions.insert(priv, pub);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-17 17:33:06 +02:00
|
|
|
int SshChannelManager::closeAllChannels(CloseAllMode mode)
|
2010-07-12 09:33:22 +02:00
|
|
|
{
|
2012-10-09 18:18:49 +02:00
|
|
|
int count = 0;
|
|
|
|
|
for (ChannelIterator it = m_channels.begin(); it != m_channels.end(); ++it) {
|
|
|
|
|
AbstractSshChannel * const channel = it.value();
|
|
|
|
|
QSSH_ASSERT(channel->channelState() != AbstractSshChannel::Closed);
|
|
|
|
|
if (channel->channelState() != AbstractSshChannel::CloseRequested) {
|
|
|
|
|
channel->closeChannel();
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-17 17:33:06 +02:00
|
|
|
if (mode == CloseAllAndReset) {
|
|
|
|
|
m_channels.clear();
|
|
|
|
|
m_sessions.clear();
|
|
|
|
|
}
|
2012-06-20 15:55:30 +02:00
|
|
|
return count;
|
2010-07-12 09:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
2012-08-17 17:33:06 +02:00
|
|
|
int SshChannelManager::channelCount() const
|
|
|
|
|
{
|
|
|
|
|
return m_channels.count();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 09:33:22 +02:00
|
|
|
void SshChannelManager::removeChannel(ChannelIterator it)
|
|
|
|
|
{
|
2013-03-21 12:01:58 +01:00
|
|
|
if (it == m_channels.end()) {
|
|
|
|
|
throw SshClientException(SshInternalError,
|
|
|
|
|
QLatin1String("Internal error: Unexpected channel lookup failure"));
|
|
|
|
|
}
|
2010-07-12 09:33:22 +02:00
|
|
|
const int removeCount = m_sessions.remove(it.value());
|
2013-03-21 12:01:58 +01:00
|
|
|
if (removeCount != 1) {
|
|
|
|
|
throw SshClientException(SshInternalError,
|
2013-11-05 14:45:35 +01:00
|
|
|
QString::fromLatin1("Internal error: Unexpected session count %1 for channel.")
|
2013-03-21 12:01:58 +01:00
|
|
|
.arg(removeCount));
|
|
|
|
|
}
|
2010-07-12 09:33:22 +02:00
|
|
|
m_channels.erase(it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
2012-05-18 10:49:35 +02:00
|
|
|
} // namespace QSsh
|