From 53a1087d1321758ab1dc0a6ad737c135249bd5e6 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Fri, 18 May 2012 10:49:35 +0200 Subject: Move SSH support into a dedicated library. It does not belong into libUtils, which is a collection of small unrelated utility classes. Task-number: QTCREATORBUG-7218 Change-Id: Id92b9f28678afec93e6f07166adfde6550f38072 Reviewed-by: Eike Ziller --- qtcreator.qbp | 1 + src/libs/libs.pro | 1 + src/libs/ssh/sftpchannel.cpp | 980 +++++++++++++++++++++ src/libs/ssh/sftpchannel.h | 113 +++ src/libs/ssh/sftpchannel_p.h | 135 +++ src/libs/ssh/sftpdefs.cpp | 35 + src/libs/ssh/sftpdefs.h | 69 ++ src/libs/ssh/sftpfilesystemmodel.cpp | 386 ++++++++ src/libs/ssh/sftpfilesystemmodel.h | 110 +++ src/libs/ssh/sftpincomingpacket.cpp | 225 +++++ src/libs/ssh/sftpincomingpacket_p.h | 114 +++ src/libs/ssh/sftpoperation.cpp | 227 +++++ src/libs/ssh/sftpoperation_p.h | 254 ++++++ src/libs/ssh/sftpoutgoingpacket.cpp | 229 +++++ src/libs/ssh/sftpoutgoingpacket_p.h | 94 ++ src/libs/ssh/sftppacket.cpp | 56 ++ src/libs/ssh/sftppacket_p.h | 119 +++ src/libs/ssh/ssh.pri | 2 + src/libs/ssh/ssh.pro | 67 ++ src/libs/ssh/ssh.qbs | 51 ++ src/libs/ssh/ssh_dependencies.pri | 1 + src/libs/ssh/ssh_global.h | 49 ++ src/libs/ssh/sshbotanconversions_p.h | 102 +++ src/libs/ssh/sshcapabilities.cpp | 106 +++ src/libs/ssh/sshcapabilities_p.h | 75 ++ src/libs/ssh/sshchannel.cpp | 263 ++++++ src/libs/ssh/sshchannel_p.h | 124 +++ src/libs/ssh/sshchannelmanager.cpp | 198 +++++ src/libs/ssh/sshchannelmanager_p.h | 97 ++ src/libs/ssh/sshconnection.cpp | 728 +++++++++++++++ src/libs/ssh/sshconnection.h | 124 +++ src/libs/ssh/sshconnection_p.h | 177 ++++ src/libs/ssh/sshconnectionmanager.cpp | 227 +++++ src/libs/ssh/sshconnectionmanager.h | 68 ++ src/libs/ssh/sshcryptofacility.cpp | 387 ++++++++ src/libs/ssh/sshcryptofacility_p.h | 157 ++++ src/libs/ssh/ssherrors.h | 46 + src/libs/ssh/sshexception_p.h | 92 ++ src/libs/ssh/sshincomingpacket.cpp | 463 ++++++++++ src/libs/ssh/sshincomingpacket_p.h | 195 ++++ src/libs/ssh/sshkeycreationdialog.cpp | 145 +++ src/libs/ssh/sshkeycreationdialog.h | 70 ++ src/libs/ssh/sshkeycreationdialog.ui | 272 ++++++ src/libs/ssh/sshkeyexchange.cpp | 225 +++++ src/libs/ssh/sshkeyexchange_p.h | 90 ++ src/libs/ssh/sshkeygenerator.cpp | 201 +++++ src/libs/ssh/sshkeygenerator.h | 85 ++ src/libs/ssh/sshkeypasswordretriever.cpp | 65 ++ src/libs/ssh/sshkeypasswordretriever_p.h | 52 ++ src/libs/ssh/sshoutgoingpacket.cpp | 325 +++++++ src/libs/ssh/sshoutgoingpacket_p.h | 108 +++ src/libs/ssh/sshpacket.cpp | 168 ++++ src/libs/ssh/sshpacket_p.h | 146 +++ src/libs/ssh/sshpacketparser.cpp | 156 ++++ src/libs/ssh/sshpacketparser_p.h | 84 ++ src/libs/ssh/sshpseudoterminal.h | 120 +++ src/libs/ssh/sshremoteprocess.cpp | 384 ++++++++ src/libs/ssh/sshremoteprocess.h | 130 +++ src/libs/ssh/sshremoteprocess_p.h | 114 +++ src/libs/ssh/sshremoteprocessrunner.cpp | 269 ++++++ src/libs/ssh/sshremoteprocessrunner.h | 94 ++ src/libs/ssh/sshsendfacility.cpp | 222 +++++ src/libs/ssh/sshsendfacility_p.h | 101 +++ src/libs/utils/ssh/sftpchannel.cpp | 980 --------------------- src/libs/utils/ssh/sftpchannel.h | 113 --- src/libs/utils/ssh/sftpchannel_p.h | 135 --- src/libs/utils/ssh/sftpdefs.cpp | 35 - src/libs/utils/ssh/sftpdefs.h | 69 -- src/libs/utils/ssh/sftpfilesystemmodel.cpp | 388 -------- src/libs/utils/ssh/sftpfilesystemmodel.h | 110 --- src/libs/utils/ssh/sftpincomingpacket.cpp | 225 ----- src/libs/utils/ssh/sftpincomingpacket_p.h | 114 --- src/libs/utils/ssh/sftpoperation.cpp | 227 ----- src/libs/utils/ssh/sftpoperation_p.h | 254 ------ src/libs/utils/ssh/sftpoutgoingpacket.cpp | 229 ----- src/libs/utils/ssh/sftpoutgoingpacket_p.h | 94 -- src/libs/utils/ssh/sftppacket.cpp | 56 -- src/libs/utils/ssh/sftppacket_p.h | 119 --- src/libs/utils/ssh/sshbotanconversions_p.h | 102 --- src/libs/utils/ssh/sshcapabilities.cpp | 106 --- src/libs/utils/ssh/sshcapabilities_p.h | 75 -- src/libs/utils/ssh/sshchannel.cpp | 263 ------ src/libs/utils/ssh/sshchannel_p.h | 124 --- src/libs/utils/ssh/sshchannelmanager.cpp | 198 ----- src/libs/utils/ssh/sshchannelmanager_p.h | 97 -- src/libs/utils/ssh/sshconnection.cpp | 730 --------------- src/libs/utils/ssh/sshconnection.h | 124 --- src/libs/utils/ssh/sshconnection_p.h | 177 ---- src/libs/utils/ssh/sshconnectionmanager.cpp | 227 ----- src/libs/utils/ssh/sshconnectionmanager.h | 68 -- src/libs/utils/ssh/sshcryptofacility.cpp | 387 -------- src/libs/utils/ssh/sshcryptofacility_p.h | 157 ---- src/libs/utils/ssh/ssherrors.h | 46 - src/libs/utils/ssh/sshexception_p.h | 92 -- src/libs/utils/ssh/sshincomingpacket.cpp | 463 ---------- src/libs/utils/ssh/sshincomingpacket_p.h | 195 ---- src/libs/utils/ssh/sshkeycreationdialog.cpp | 132 --- src/libs/utils/ssh/sshkeycreationdialog.h | 69 -- src/libs/utils/ssh/sshkeycreationdialog.ui | 251 ------ src/libs/utils/ssh/sshkeyexchange.cpp | 225 ----- src/libs/utils/ssh/sshkeyexchange_p.h | 90 -- src/libs/utils/ssh/sshkeygenerator.cpp | 201 ----- src/libs/utils/ssh/sshkeygenerator.h | 85 -- src/libs/utils/ssh/sshkeypasswordretriever.cpp | 65 -- src/libs/utils/ssh/sshkeypasswordretriever_p.h | 52 -- src/libs/utils/ssh/sshoutgoingpacket.cpp | 325 ------- src/libs/utils/ssh/sshoutgoingpacket_p.h | 108 --- src/libs/utils/ssh/sshpacket.cpp | 168 ---- src/libs/utils/ssh/sshpacket_p.h | 146 --- src/libs/utils/ssh/sshpacketparser.cpp | 156 ---- src/libs/utils/ssh/sshpacketparser_p.h | 84 -- src/libs/utils/ssh/sshpseudoterminal.h | 120 --- src/libs/utils/ssh/sshremoteprocess.cpp | 386 -------- src/libs/utils/ssh/sshremoteprocess.h | 130 --- src/libs/utils/ssh/sshremoteprocess_p.h | 114 --- src/libs/utils/ssh/sshremoteprocessrunner.cpp | 271 ------ src/libs/utils/ssh/sshremoteprocessrunner.h | 94 -- src/libs/utils/ssh/sshsendfacility.cpp | 222 ----- src/libs/utils/ssh/sshsendfacility_p.h | 101 --- src/libs/utils/utils-lib.pri | 60 +- src/libs/utils/utils.pro | 3 +- src/libs/utils/utils.qbs | 59 +- src/libs/utils/utils_dependencies.pri | 1 - src/plugins/analyzerbase/analyzermanager.cpp | 2 +- src/plugins/analyzerbase/analyzerstartparameters.h | 4 +- src/plugins/analyzerbase/startremotedialog.cpp | 10 +- src/plugins/analyzerbase/startremotedialog.h | 4 +- src/plugins/cpaster/cpaster.qbs | 2 +- src/plugins/debugger/debugger.qbs | 2 + src/plugins/debugger/debugger_dependencies.pri | 1 + src/plugins/debugger/debuggerplugin.cpp | 4 +- src/plugins/debugger/debuggerstartparameters.h | 4 +- src/plugins/debugger/gdb/remotegdbprocess.cpp | 27 +- src/plugins/debugger/gdb/remotegdbprocess.h | 16 +- src/plugins/debugger/lldb/lldbenginehost.cpp | 12 +- src/plugins/debugger/lldb/lldbenginehost.h | 18 +- src/plugins/git/git.qbs | 2 +- src/plugins/madde/maddedevicetester.cpp | 4 +- src/plugins/madde/maddedevicetester.h | 4 +- .../madde/maddeuploadandinstallpackagesteps.cpp | 2 +- src/plugins/madde/maemodeploybymountsteps.cpp | 2 +- src/plugins/madde/maemodeploymentmounter.cpp | 6 +- src/plugins/madde/maemodeploymentmounter.h | 6 +- src/plugins/madde/maemodeviceconfigwizard.cpp | 11 +- src/plugins/madde/maemopublisherfremantlefree.cpp | 4 +- src/plugins/madde/maemopublisherfremantlefree.h | 8 +- src/plugins/madde/maemoqemumanager.h | 14 +- src/plugins/madde/maemoremotecopyfacility.cpp | 6 +- src/plugins/madde/maemoremotecopyfacility.h | 8 +- src/plugins/madde/maemoremotemounter.cpp | 5 +- src/plugins/madde/maemoremotemounter.h | 12 +- src/plugins/madde/maemorunconfiguration.cpp | 2 +- src/plugins/madde/maemosshrunner.cpp | 4 +- .../abstractremotelinuxdeployservice.cpp | 8 +- .../remotelinux/abstractremotelinuxdeployservice.h | 4 +- .../abstractuploadandinstallpackageservice.cpp | 2 +- .../remotelinux/genericdirectuploadservice.cpp | 14 +- .../remotelinux/genericdirectuploadservice.h | 4 +- .../genericlinuxdeviceconfigurationwidget.cpp | 5 +- .../genericlinuxdeviceconfigurationwizard.cpp | 4 +- .../genericlinuxdeviceconfigurationwizardpages.cpp | 1 + .../genericlinuxdeviceconfigurationwizardpages.h | 4 +- .../remotelinux/linuxdeviceconfiguration.cpp | 4 +- src/plugins/remotelinux/linuxdeviceconfiguration.h | 10 +- src/plugins/remotelinux/linuxdevicetester.cpp | 8 +- src/plugins/remotelinux/linuxdevicetester.h | 4 +- src/plugins/remotelinux/packageuploader.cpp | 12 +- src/plugins/remotelinux/packageuploader.h | 12 +- .../remotelinux/publickeydeploymentdialog.cpp | 2 +- src/plugins/remotelinux/remotelinux.qbs | 2 + .../remotelinux/remotelinuxapplicationrunner.cpp | 17 +- .../remotelinux/remotelinuxapplicationrunner.h | 8 +- .../remotelinuxcustomcommanddeployservice.cpp | 4 +- .../remotelinux/remotelinuxdebugsupport.cpp | 2 +- .../remotelinux/remotelinuxenvironmentreader.cpp | 12 +- .../remotelinux/remotelinuxenvironmentreader.h | 4 +- .../remotelinux/remotelinuxpackageinstaller.cpp | 8 +- src/plugins/remotelinux/remotelinuxprocesslist.cpp | 4 +- .../remotelinux/remotelinuxrunconfiguration.cpp | 1 + .../remotelinux/remotelinuxrunconfiguration.h | 4 +- .../remotelinux/remotelinuxusedportsgatherer.cpp | 9 +- src/plugins/remotelinux/sshkeydeployer.cpp | 4 +- src/plugins/remotelinux/sshkeydeployer.h | 4 +- src/plugins/remotelinux/startgdbserverdialog.cpp | 5 +- .../valgrind/callgrind/callgrindcontroller.cpp | 10 +- .../valgrind/callgrind/callgrindcontroller.h | 16 +- src/plugins/valgrind/valgrindprocess.cpp | 34 +- src/plugins/valgrind/valgrindprocess.h | 20 +- src/plugins/valgrind/valgrindrunner.cpp | 10 +- src/plugins/valgrind/valgrindrunner.h | 6 +- tests/manual/ssh/errorhandling/main.cpp | 14 +- .../ssh/remoteprocess/argumentscollector.cpp | 5 +- .../manual/ssh/remoteprocess/argumentscollector.h | 6 +- tests/manual/ssh/remoteprocess/main.cpp | 6 +- .../manual/ssh/remoteprocess/remoteprocesstest.cpp | 14 +- tests/manual/ssh/remoteprocess/remoteprocesstest.h | 14 +- tests/manual/ssh/sftp/argumentscollector.cpp | 3 +- tests/manual/ssh/sftp/argumentscollector.h | 3 +- tests/manual/ssh/sftp/main.cpp | 9 +- tests/manual/ssh/sftp/parameters.h | 4 +- tests/manual/ssh/sftp/sftptest.cpp | 14 +- tests/manual/ssh/sftp/sftptest.h | 40 +- tests/manual/ssh/sftpfsmodel/main.cpp | 6 +- tests/manual/ssh/sftpfsmodel/window.cpp | 10 +- tests/manual/ssh/sftpfsmodel/window.h | 8 +- tests/manual/ssh/shell/main.cpp | 6 +- tests/manual/ssh/shell/shell.cpp | 10 +- tests/manual/ssh/shell/shell.h | 8 +- tests/manual/ssh/ssh.pri | 9 +- 209 files changed, 10911 insertions(+), 10836 deletions(-) create mode 100644 src/libs/ssh/sftpchannel.cpp create mode 100644 src/libs/ssh/sftpchannel.h create mode 100644 src/libs/ssh/sftpchannel_p.h create mode 100644 src/libs/ssh/sftpdefs.cpp create mode 100644 src/libs/ssh/sftpdefs.h create mode 100644 src/libs/ssh/sftpfilesystemmodel.cpp create mode 100644 src/libs/ssh/sftpfilesystemmodel.h create mode 100644 src/libs/ssh/sftpincomingpacket.cpp create mode 100644 src/libs/ssh/sftpincomingpacket_p.h create mode 100644 src/libs/ssh/sftpoperation.cpp create mode 100644 src/libs/ssh/sftpoperation_p.h create mode 100644 src/libs/ssh/sftpoutgoingpacket.cpp create mode 100644 src/libs/ssh/sftpoutgoingpacket_p.h create mode 100644 src/libs/ssh/sftppacket.cpp create mode 100644 src/libs/ssh/sftppacket_p.h create mode 100644 src/libs/ssh/ssh.pri create mode 100644 src/libs/ssh/ssh.pro create mode 100644 src/libs/ssh/ssh.qbs create mode 100644 src/libs/ssh/ssh_dependencies.pri create mode 100644 src/libs/ssh/ssh_global.h create mode 100644 src/libs/ssh/sshbotanconversions_p.h create mode 100644 src/libs/ssh/sshcapabilities.cpp create mode 100644 src/libs/ssh/sshcapabilities_p.h create mode 100644 src/libs/ssh/sshchannel.cpp create mode 100644 src/libs/ssh/sshchannel_p.h create mode 100644 src/libs/ssh/sshchannelmanager.cpp create mode 100644 src/libs/ssh/sshchannelmanager_p.h create mode 100644 src/libs/ssh/sshconnection.cpp create mode 100644 src/libs/ssh/sshconnection.h create mode 100644 src/libs/ssh/sshconnection_p.h create mode 100644 src/libs/ssh/sshconnectionmanager.cpp create mode 100644 src/libs/ssh/sshconnectionmanager.h create mode 100644 src/libs/ssh/sshcryptofacility.cpp create mode 100644 src/libs/ssh/sshcryptofacility_p.h create mode 100644 src/libs/ssh/ssherrors.h create mode 100644 src/libs/ssh/sshexception_p.h create mode 100644 src/libs/ssh/sshincomingpacket.cpp create mode 100644 src/libs/ssh/sshincomingpacket_p.h create mode 100644 src/libs/ssh/sshkeycreationdialog.cpp create mode 100644 src/libs/ssh/sshkeycreationdialog.h create mode 100644 src/libs/ssh/sshkeycreationdialog.ui create mode 100644 src/libs/ssh/sshkeyexchange.cpp create mode 100644 src/libs/ssh/sshkeyexchange_p.h create mode 100644 src/libs/ssh/sshkeygenerator.cpp create mode 100644 src/libs/ssh/sshkeygenerator.h create mode 100644 src/libs/ssh/sshkeypasswordretriever.cpp create mode 100644 src/libs/ssh/sshkeypasswordretriever_p.h create mode 100644 src/libs/ssh/sshoutgoingpacket.cpp create mode 100644 src/libs/ssh/sshoutgoingpacket_p.h create mode 100644 src/libs/ssh/sshpacket.cpp create mode 100644 src/libs/ssh/sshpacket_p.h create mode 100644 src/libs/ssh/sshpacketparser.cpp create mode 100644 src/libs/ssh/sshpacketparser_p.h create mode 100644 src/libs/ssh/sshpseudoterminal.h create mode 100644 src/libs/ssh/sshremoteprocess.cpp create mode 100644 src/libs/ssh/sshremoteprocess.h create mode 100644 src/libs/ssh/sshremoteprocess_p.h create mode 100644 src/libs/ssh/sshremoteprocessrunner.cpp create mode 100644 src/libs/ssh/sshremoteprocessrunner.h create mode 100644 src/libs/ssh/sshsendfacility.cpp create mode 100644 src/libs/ssh/sshsendfacility_p.h delete mode 100644 src/libs/utils/ssh/sftpchannel.cpp delete mode 100644 src/libs/utils/ssh/sftpchannel.h delete mode 100644 src/libs/utils/ssh/sftpchannel_p.h delete mode 100644 src/libs/utils/ssh/sftpdefs.cpp delete mode 100644 src/libs/utils/ssh/sftpdefs.h delete mode 100644 src/libs/utils/ssh/sftpfilesystemmodel.cpp delete mode 100644 src/libs/utils/ssh/sftpfilesystemmodel.h delete mode 100644 src/libs/utils/ssh/sftpincomingpacket.cpp delete mode 100644 src/libs/utils/ssh/sftpincomingpacket_p.h delete mode 100644 src/libs/utils/ssh/sftpoperation.cpp delete mode 100644 src/libs/utils/ssh/sftpoperation_p.h delete mode 100644 src/libs/utils/ssh/sftpoutgoingpacket.cpp delete mode 100644 src/libs/utils/ssh/sftpoutgoingpacket_p.h delete mode 100644 src/libs/utils/ssh/sftppacket.cpp delete mode 100644 src/libs/utils/ssh/sftppacket_p.h delete mode 100644 src/libs/utils/ssh/sshbotanconversions_p.h delete mode 100644 src/libs/utils/ssh/sshcapabilities.cpp delete mode 100644 src/libs/utils/ssh/sshcapabilities_p.h delete mode 100644 src/libs/utils/ssh/sshchannel.cpp delete mode 100644 src/libs/utils/ssh/sshchannel_p.h delete mode 100644 src/libs/utils/ssh/sshchannelmanager.cpp delete mode 100644 src/libs/utils/ssh/sshchannelmanager_p.h delete mode 100644 src/libs/utils/ssh/sshconnection.cpp delete mode 100644 src/libs/utils/ssh/sshconnection.h delete mode 100644 src/libs/utils/ssh/sshconnection_p.h delete mode 100644 src/libs/utils/ssh/sshconnectionmanager.cpp delete mode 100644 src/libs/utils/ssh/sshconnectionmanager.h delete mode 100644 src/libs/utils/ssh/sshcryptofacility.cpp delete mode 100644 src/libs/utils/ssh/sshcryptofacility_p.h delete mode 100644 src/libs/utils/ssh/ssherrors.h delete mode 100644 src/libs/utils/ssh/sshexception_p.h delete mode 100644 src/libs/utils/ssh/sshincomingpacket.cpp delete mode 100644 src/libs/utils/ssh/sshincomingpacket_p.h delete mode 100644 src/libs/utils/ssh/sshkeycreationdialog.cpp delete mode 100644 src/libs/utils/ssh/sshkeycreationdialog.h delete mode 100644 src/libs/utils/ssh/sshkeycreationdialog.ui delete mode 100644 src/libs/utils/ssh/sshkeyexchange.cpp delete mode 100644 src/libs/utils/ssh/sshkeyexchange_p.h delete mode 100644 src/libs/utils/ssh/sshkeygenerator.cpp delete mode 100644 src/libs/utils/ssh/sshkeygenerator.h delete mode 100644 src/libs/utils/ssh/sshkeypasswordretriever.cpp delete mode 100644 src/libs/utils/ssh/sshkeypasswordretriever_p.h delete mode 100644 src/libs/utils/ssh/sshoutgoingpacket.cpp delete mode 100644 src/libs/utils/ssh/sshoutgoingpacket_p.h delete mode 100644 src/libs/utils/ssh/sshpacket.cpp delete mode 100644 src/libs/utils/ssh/sshpacket_p.h delete mode 100644 src/libs/utils/ssh/sshpacketparser.cpp delete mode 100644 src/libs/utils/ssh/sshpacketparser_p.h delete mode 100644 src/libs/utils/ssh/sshpseudoterminal.h delete mode 100644 src/libs/utils/ssh/sshremoteprocess.cpp delete mode 100644 src/libs/utils/ssh/sshremoteprocess.h delete mode 100644 src/libs/utils/ssh/sshremoteprocess_p.h delete mode 100644 src/libs/utils/ssh/sshremoteprocessrunner.cpp delete mode 100644 src/libs/utils/ssh/sshremoteprocessrunner.h delete mode 100644 src/libs/utils/ssh/sshsendfacility.cpp delete mode 100644 src/libs/utils/ssh/sshsendfacility_p.h diff --git a/qtcreator.qbp b/qtcreator.qbp index cd6307a39d..9e2206b569 100644 --- a/qtcreator.qbp +++ b/qtcreator.qbp @@ -23,6 +23,7 @@ Project { "src/libs/qmldebug/qmldebug.qbs", "src/libs/qtcomponents/styleitem/styleitem.qbs", "src/libs/symbianutils/symbianutils.qbs", + "src/libs/ssh/ssh.qbs", "src/libs/utils/utils.qbs", "src/libs/zeroconf/zeroconf.qbs", "src/plugins/analyzerbase/analyzerbase.qbs", diff --git a/src/libs/libs.pro b/src/libs/libs.pro index 374c06f937..b8a7e4e742 100644 --- a/src/libs/libs.pro +++ b/src/libs/libs.pro @@ -16,6 +16,7 @@ SUBDIRS = \ glsl \ qmleditorwidgets \ qtcomponents/styleitem \ + ssh \ zeroconf win32:SUBDIRS += utils/process_ctrlc_stub.pro diff --git a/src/libs/ssh/sftpchannel.cpp b/src/libs/ssh/sftpchannel.cpp new file mode 100644 index 0000000000..c4a2181868 --- /dev/null +++ b/src/libs/ssh/sftpchannel.cpp @@ -0,0 +1,980 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sftpchannel.h" +#include "sftpchannel_p.h" + +#include "sshexception_p.h" +#include "sshincomingpacket_p.h" +#include "sshsendfacility_p.h" + +#include +#include + +/*! + \class QSsh::SftpChannel + + \brief This class provides SFTP operations. + + Objects are created via SshConnection::createSftpChannel(). + The channel needs to be initialized with + a call to initialize() and is closed via closeChannel(). After closing + a channel, no more operations are possible. It cannot be re-opened + using initialize(); use SshConnection::createSftpChannel() if you need + a new one. + + After the initialized() signal has been emitted, operations can be started. + All SFTP operations are asynchronous (non-blocking) and can be in-flight + simultaneously (though callers must ensure that concurrently running jobs + are independent of each other, e.g. they must not write to the same file). + Operations are identified by their job id, which is returned by + the respective member function. If the function can right away detect that + the operation cannot succeed, it returns SftpInvalidJob. If an error occurs + later, the finished() signal is emitted for the respective job with a + non-empty error string. + + Note that directory names must not have a trailing slash. +*/ + +namespace QSsh { +namespace Internal { +namespace { + const quint32 ProtocolVersion = 3; + + QString errorMessage(const QString &serverMessage, + const QString &alternativeMessage) + { + return serverMessage.isEmpty() ? alternativeMessage : serverMessage; + } + + QString errorMessage(const SftpStatusResponse &response, + const QString &alternativeMessage) + { + return response.status == SSH_FX_OK ? QString() + : errorMessage(response.errorString, alternativeMessage); + } +} // anonymous namespace +} // namespace Internal + +SftpChannel::SftpChannel(quint32 channelId, + Internal::SshSendFacility &sendFacility) + : d(new Internal::SftpChannelPrivate(channelId, sendFacility, this)) +{ + connect(d, SIGNAL(initialized()), this, SIGNAL(initialized()), + Qt::QueuedConnection); + connect(d, SIGNAL(initializationFailed(QString)), this, + SIGNAL(initializationFailed(QString)), Qt::QueuedConnection); + connect(d, SIGNAL(dataAvailable(QSsh::SftpJobId,QString)), this, + SIGNAL(dataAvailable(QSsh::SftpJobId,QString)), Qt::QueuedConnection); + connect(d, SIGNAL(fileInfoAvailable(QSsh::SftpJobId,QList)), this, + SIGNAL(fileInfoAvailable(QSsh::SftpJobId,QList)), + Qt::QueuedConnection); + connect(d, SIGNAL(finished(QSsh::SftpJobId,QString)), this, + SIGNAL(finished(QSsh::SftpJobId,QString)), Qt::QueuedConnection); + connect(d, SIGNAL(closed()), this, SIGNAL(closed()), Qt::QueuedConnection); +} + +SftpChannel::State SftpChannel::state() const +{ + switch (d->channelState()) { + case Internal::AbstractSshChannel::Inactive: + return Uninitialized; + case Internal::AbstractSshChannel::SessionRequested: + return Initializing; + case Internal::AbstractSshChannel::CloseRequested: + return Closing; + case Internal::AbstractSshChannel::Closed: + return Closed; + case Internal::AbstractSshChannel::SessionEstablished: + return d->m_sftpState == Internal::SftpChannelPrivate::Initialized + ? Initialized : Initializing; + default: + Q_ASSERT(!"Oh no, we forgot to handle a channel state!"); + return Closed; // For the compiler. + } +} + +void SftpChannel::initialize() +{ + d->requestSessionStart(); + d->m_sftpState = Internal::SftpChannelPrivate::SubsystemRequested; +} + +void SftpChannel::closeChannel() +{ + d->closeChannel(); +} + +SftpJobId SftpChannel::statFile(const QString &path) +{ + return d->createJob(Internal::SftpStatFile::Ptr( + new Internal::SftpStatFile(++d->m_nextJobId, path))); +} + +SftpJobId SftpChannel::listDirectory(const QString &path) +{ + return d->createJob(Internal::SftpListDir::Ptr( + new Internal::SftpListDir(++d->m_nextJobId, path))); +} + +SftpJobId SftpChannel::createDirectory(const QString &path) +{ + return d->createJob(Internal::SftpMakeDir::Ptr( + new Internal::SftpMakeDir(++d->m_nextJobId, path))); +} + +SftpJobId SftpChannel::removeDirectory(const QString &path) +{ + return d->createJob(Internal::SftpRmDir::Ptr( + new Internal::SftpRmDir(++d->m_nextJobId, path))); +} + +SftpJobId SftpChannel::removeFile(const QString &path) +{ + return d->createJob(Internal::SftpRm::Ptr( + new Internal::SftpRm(++d->m_nextJobId, path))); +} + +SftpJobId SftpChannel::renameFileOrDirectory(const QString &oldPath, + const QString &newPath) +{ + return d->createJob(Internal::SftpRename::Ptr( + new Internal::SftpRename(++d->m_nextJobId, oldPath, newPath))); +} + +SftpJobId SftpChannel::createLink(const QString &filePath, const QString &target) +{ + return d->createJob(Internal::SftpCreateLink::Ptr( + new Internal::SftpCreateLink(++d->m_nextJobId, filePath, target))); +} + +SftpJobId SftpChannel::createFile(const QString &path, SftpOverwriteMode mode) +{ + return d->createJob(Internal::SftpCreateFile::Ptr( + new Internal::SftpCreateFile(++d->m_nextJobId, path, mode))); +} + +SftpJobId SftpChannel::uploadFile(const QString &localFilePath, + const QString &remoteFilePath, SftpOverwriteMode mode) +{ + QSharedPointer localFile(new QFile(localFilePath)); + if (!localFile->open(QIODevice::ReadOnly)) + return SftpInvalidJob; + return d->createJob(Internal::SftpUploadFile::Ptr( + new Internal::SftpUploadFile(++d->m_nextJobId, remoteFilePath, localFile, mode))); +} + +SftpJobId SftpChannel::downloadFile(const QString &remoteFilePath, + const QString &localFilePath, SftpOverwriteMode mode) +{ + QSharedPointer localFile(new QFile(localFilePath)); + if (mode == SftpSkipExisting && localFile->exists()) + return SftpInvalidJob; + QIODevice::OpenMode openMode = QIODevice::WriteOnly; + if (mode == SftpOverwriteExisting) + openMode |= QIODevice::Truncate; + else if (mode == SftpAppendToExisting) + openMode |= QIODevice::Append; + if (!localFile->open(openMode)) + return SftpInvalidJob; + return d->createJob(Internal::SftpDownload::Ptr( + new Internal::SftpDownload(++d->m_nextJobId, remoteFilePath, localFile))); +} + +SftpJobId SftpChannel::uploadDir(const QString &localDirPath, + const QString &remoteParentDirPath) +{ + if (state() != Initialized) + return SftpInvalidJob; + const QDir localDir(localDirPath); + if (!localDir.exists() || !localDir.isReadable()) + return SftpInvalidJob; + const Internal::SftpUploadDir::Ptr uploadDirOp( + new Internal::SftpUploadDir(++d->m_nextJobId)); + const QString remoteDirPath + = remoteParentDirPath + QLatin1Char('/') + localDir.dirName(); + const Internal::SftpMakeDir::Ptr mkdirOp( + new Internal::SftpMakeDir(++d->m_nextJobId, remoteDirPath, uploadDirOp)); + uploadDirOp->mkdirsInProgress.insert(mkdirOp, + Internal::SftpUploadDir::Dir(localDirPath, remoteDirPath)); + d->createJob(mkdirOp); + return uploadDirOp->jobId; +} + +SftpChannel::~SftpChannel() +{ + delete d; +} + + +namespace Internal { + +SftpChannelPrivate::SftpChannelPrivate(quint32 channelId, + SshSendFacility &sendFacility, SftpChannel *sftp) + : AbstractSshChannel(channelId, sendFacility), + m_nextJobId(0), m_sftpState(Inactive), m_sftp(sftp) +{ +} + +SftpJobId SftpChannelPrivate::createJob(const AbstractSftpOperation::Ptr &job) +{ + if (m_sftp->state() != SftpChannel::Initialized) + return SftpInvalidJob; + m_jobs.insert(job->jobId, job); + sendData(job->initialPacket(m_outgoingPacket).rawData()); + return job->jobId; +} + +void SftpChannelPrivate::handleChannelSuccess() +{ + if (channelState() == CloseRequested) + return; +#ifdef CREATOR_SSH_DEBUG + qDebug("sftp subsystem initialized"); +#endif + sendData(m_outgoingPacket.generateInit(ProtocolVersion).rawData()); + m_sftpState = InitSent; +} + +void SftpChannelPrivate::handleChannelFailure() +{ + if (channelState() == CloseRequested) + return; + + if (m_sftpState != SubsystemRequested) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_MSG_CHANNEL_FAILURE packet."); + } + emit initializationFailed(tr("Server could not start sftp subsystem.")); + closeChannel(); +} + +void SftpChannelPrivate::handleChannelDataInternal(const QByteArray &data) +{ + if (channelState() == CloseRequested) + return; + + m_incomingData += data; + m_incomingPacket.consumeData(m_incomingData); + while (m_incomingPacket.isComplete()) { + handleCurrentPacket(); + m_incomingPacket.clear(); + m_incomingPacket.consumeData(m_incomingData); + } +} + +void SftpChannelPrivate::handleChannelExtendedDataInternal(quint32 type, + const QByteArray &data) +{ + qWarning("Unexpected extended data '%s' of type %d on SFTP channel.", + data.data(), type); +} + +void SftpChannelPrivate::handleExitStatus(const SshChannelExitStatus &exitStatus) +{ + const char * const message = "Remote SFTP service exited with exit code %d"; +#ifdef CREATOR_SSH_DEBUG + qDebug(message, exitStatus.exitStatus); +#else + if (exitStatus.exitStatus != 0) + qWarning(message, exitStatus.exitStatus); +#endif +} + +void SftpChannelPrivate::handleExitSignal(const SshChannelExitSignal &signal) +{ + qWarning("Remote SFTP service killed; signal was %s", signal.signal.data()); +} + +void SftpChannelPrivate::handleCurrentPacket() +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("Handling SFTP packet of type %d", m_incomingPacket.type()); +#endif + switch (m_incomingPacket.type()) { + case SSH_FXP_VERSION: + handleServerVersion(); + break; + case SSH_FXP_HANDLE: + handleHandle(); + break; + case SSH_FXP_NAME: + handleName(); + break; + case SSH_FXP_STATUS: + handleStatus(); + break; + case SSH_FXP_DATA: + handleReadData(); + break; + case SSH_FXP_ATTRS: + handleAttrs(); + break; + default: + throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected packet.", + tr("Unexpected packet of type %1.").arg(m_incomingPacket.type())); + } +} + +void SftpChannelPrivate::handleServerVersion() +{ + checkChannelActive(); + if (m_sftpState != InitSent) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_FXP_VERSION packet."); + } + +#ifdef CREATOR_SSH_DEBUG + qDebug("sftp init received"); +#endif + const quint32 serverVersion = m_incomingPacket.extractServerVersion(); + if (serverVersion != ProtocolVersion) { + emit initializationFailed(tr("Protocol version mismatch: Expected %1, got %2") + .arg(serverVersion).arg(ProtocolVersion)); + closeChannel(); + } else { + m_sftpState = Initialized; + emit initialized(); + } +} + +void SftpChannelPrivate::handleHandle() +{ + const SftpHandleResponse &response = m_incomingPacket.asHandleResponse(); + JobMap::Iterator it = lookupJob(response.requestId); + const QSharedPointer job + = it.value().dynamicCast(); + if (job.isNull()) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_FXP_HANDLE packet."); + } + if (job->state != AbstractSftpOperationWithHandle::OpenRequested) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_FXP_HANDLE packet."); + } + job->remoteHandle = response.handle; + job->state = AbstractSftpOperationWithHandle::Open; + + switch (it.value()->type()) { + case AbstractSftpOperation::ListDir: + handleLsHandle(it); + break; + case AbstractSftpOperation::CreateFile: + handleCreateFileHandle(it); + break; + case AbstractSftpOperation::Download: + handleGetHandle(it); + break; + case AbstractSftpOperation::UploadFile: + handlePutHandle(it); + break; + default: + Q_ASSERT(!"Oh no, I forgot to handle an SFTP operation type!"); + } +} + +void SftpChannelPrivate::handleLsHandle(const JobMap::Iterator &it) +{ + SftpListDir::Ptr op = it.value().staticCast(); + sendData(m_outgoingPacket.generateReadDir(op->remoteHandle, + op->jobId).rawData()); +} + +void SftpChannelPrivate::handleCreateFileHandle(const JobMap::Iterator &it) +{ + SftpCreateFile::Ptr op = it.value().staticCast(); + sendData(m_outgoingPacket.generateCloseHandle(op->remoteHandle, + op->jobId).rawData()); +} + +void SftpChannelPrivate::handleGetHandle(const JobMap::Iterator &it) +{ + SftpDownload::Ptr op = it.value().staticCast(); + sendData(m_outgoingPacket.generateFstat(op->remoteHandle, + op->jobId).rawData()); + op->statRequested = true; +} + +void SftpChannelPrivate::handlePutHandle(const JobMap::Iterator &it) +{ + SftpUploadFile::Ptr op = it.value().staticCast(); + if (op->parentJob && op->parentJob->hasError) + sendTransferCloseHandle(op, it.key()); + + // OpenSSH does not implement the RFC's append functionality, so we + // have to emulate it. + if (op->mode == SftpAppendToExisting) { + sendData(m_outgoingPacket.generateFstat(op->remoteHandle, + op->jobId).rawData()); + op->statRequested = true; + } else { + spawnWriteRequests(it); + } +} + +void SftpChannelPrivate::handleStatus() +{ + const SftpStatusResponse &response = m_incomingPacket.asStatusResponse(); +#ifdef CREATOR_SSH_DEBUG + qDebug("%s: status = %d", Q_FUNC_INFO, response.status); +#endif + JobMap::Iterator it = lookupJob(response.requestId); + switch (it.value()->type()) { + case AbstractSftpOperation::ListDir: + handleLsStatus(it, response); + break; + case AbstractSftpOperation::Download: + handleGetStatus(it, response); + break; + case AbstractSftpOperation::UploadFile: + handlePutStatus(it, response); + break; + case AbstractSftpOperation::MakeDir: + handleMkdirStatus(it, response); + break; + case AbstractSftpOperation::StatFile: + case AbstractSftpOperation::RmDir: + case AbstractSftpOperation::Rm: + case AbstractSftpOperation::Rename: + case AbstractSftpOperation::CreateFile: + case AbstractSftpOperation::CreateLink: + handleStatusGeneric(it, response); + break; + } +} + +void SftpChannelPrivate::handleStatusGeneric(const JobMap::Iterator &it, + const SftpStatusResponse &response) +{ + AbstractSftpOperation::Ptr op = it.value(); + const QString error = errorMessage(response, tr("Unknown error.")); + emit finished(op->jobId, error); + m_jobs.erase(it); +} + +void SftpChannelPrivate::handleMkdirStatus(const JobMap::Iterator &it, + const SftpStatusResponse &response) +{ + SftpMakeDir::Ptr op = it.value().staticCast(); + if (op->parentJob == SftpUploadDir::Ptr()) { + handleStatusGeneric(it, response); + return; + } + if (op->parentJob->hasError) { + m_jobs.erase(it); + return; + } + + typedef QMap::Iterator DirIt; + DirIt dirIt = op->parentJob->mkdirsInProgress.find(op); + Q_ASSERT(dirIt != op->parentJob->mkdirsInProgress.end()); + const QString &remoteDir = dirIt.value().remoteDir; + if (response.status == SSH_FX_OK) { + emit dataAvailable(op->parentJob->jobId, + tr("Created remote directory '%1'.").arg(remoteDir)); + } else if (response.status == SSH_FX_FAILURE) { + emit dataAvailable(op->parentJob->jobId, + tr("Remote directory '%1' already exists.").arg(remoteDir)); + } else { + op->parentJob->setError(); + emit finished(op->parentJob->jobId, + tr("Error creating directory '%1': %2") + .arg(remoteDir, response.errorString)); + m_jobs.erase(it); + return; + } + + QDir localDir(dirIt.value().localDir); + const QFileInfoList &dirInfos + = localDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); + foreach (const QFileInfo &dirInfo, dirInfos) { + const QString remoteSubDir = remoteDir + QLatin1Char('/') + dirInfo.fileName(); + const SftpMakeDir::Ptr mkdirOp( + new SftpMakeDir(++m_nextJobId, remoteSubDir, op->parentJob)); + op->parentJob->mkdirsInProgress.insert(mkdirOp, + SftpUploadDir::Dir(dirInfo.absoluteFilePath(), remoteSubDir)); + createJob(mkdirOp); + } + + const QFileInfoList &fileInfos = localDir.entryInfoList(QDir::Files); + foreach (const QFileInfo &fileInfo, fileInfos) { + QSharedPointer localFile(new QFile(fileInfo.absoluteFilePath())); + if (!localFile->open(QIODevice::ReadOnly)) { + op->parentJob->setError(); + emit finished(op->parentJob->jobId, + tr("Could not open local file '%1': %2") + .arg(fileInfo.absoluteFilePath(), localFile->errorString())); + m_jobs.erase(it); + return; + } + + const QString remoteFilePath = remoteDir + QLatin1Char('/') + fileInfo.fileName(); + SftpUploadFile::Ptr uploadFileOp(new SftpUploadFile(++m_nextJobId, + remoteFilePath, localFile, SftpOverwriteExisting, op->parentJob)); + createJob(uploadFileOp); + op->parentJob->uploadsInProgress.append(uploadFileOp); + } + + op->parentJob->mkdirsInProgress.erase(dirIt); + if (op->parentJob->mkdirsInProgress.isEmpty() + && op->parentJob->uploadsInProgress.isEmpty()) + emit finished(op->parentJob->jobId); + m_jobs.erase(it); +} + +void SftpChannelPrivate::handleLsStatus(const JobMap::Iterator &it, + const SftpStatusResponse &response) +{ + SftpListDir::Ptr op = it.value().staticCast(); + switch (op->state) { + case SftpListDir::OpenRequested: + emit finished(op->jobId, errorMessage(response.errorString, + tr("Remote directory could not be opened for reading."))); + m_jobs.erase(it); + break; + case SftpListDir::Open: + if (response.status != SSH_FX_EOF) + reportRequestError(op, errorMessage(response.errorString, + tr("Failed to list remote directory contents."))); + op->state = SftpListDir::CloseRequested; + sendData(m_outgoingPacket.generateCloseHandle(op->remoteHandle, + op->jobId).rawData()); + break; + case SftpListDir::CloseRequested: + if (!op->hasError) { + const QString error = errorMessage(response, + tr("Failed to close remote directory.")); + emit finished(op->jobId, error); + } + m_jobs.erase(it); + break; + default: + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_FXP_STATUS packet."); + } +} + +void SftpChannelPrivate::handleGetStatus(const JobMap::Iterator &it, + const SftpStatusResponse &response) +{ + SftpDownload::Ptr op = it.value().staticCast(); + switch (op->state) { + case SftpDownload::OpenRequested: + emit finished(op->jobId, + errorMessage(response.errorString, + tr("Failed to open remote file for reading."))); + m_jobs.erase(it); + break; + case SftpDownload::Open: + if (op->statRequested) { + reportRequestError(op, errorMessage(response.errorString, + tr("Failed retrieve information on the remote file ('stat' failed)."))); + sendTransferCloseHandle(op, response.requestId); + } else { + if ((response.status != SSH_FX_EOF || response.requestId != op->eofId) + && !op->hasError) + reportRequestError(op, errorMessage(response.errorString, + tr("Failed to read remote file."))); + finishTransferRequest(it); + } + break; + case SftpDownload::CloseRequested: + Q_ASSERT(op->inFlightCount == 1); + if (!op->hasError) { + if (response.status == SSH_FX_OK) + emit finished(op->jobId); + else + reportRequestError(op, errorMessage(response.errorString, + tr("Failed to close remote file."))); + } + removeTransferRequest(it); + break; + default: + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_FXP_STATUS packet."); + } +} + +void SftpChannelPrivate::handlePutStatus(const JobMap::Iterator &it, + const SftpStatusResponse &response) +{ + SftpUploadFile::Ptr job = it.value().staticCast(); + switch (job->state) { + case SftpUploadFile::OpenRequested: { + bool emitError = false; + if (job->parentJob) { + if (!job->parentJob->hasError) { + job->parentJob->setError(); + emitError = true; + } + } else { + emitError = true; + } + + if (emitError) { + emit finished(job->jobId, + errorMessage(response.errorString, + tr("Failed to open remote file for writing."))); + } + m_jobs.erase(it); + break; + } + case SftpUploadFile::Open: + if (job->hasError || (job->parentJob && job->parentJob->hasError)) { + job->hasError = true; + finishTransferRequest(it); + return; + } + + if (response.status == SSH_FX_OK) { + sendWriteRequest(it); + } else { + if (job->parentJob) + job->parentJob->setError(); + reportRequestError(job, errorMessage(response.errorString, + tr("Failed to write remote file."))); + finishTransferRequest(it); + } + break; + case SftpUploadFile::CloseRequested: + Q_ASSERT(job->inFlightCount == 1); + if (job->hasError || (job->parentJob && job->parentJob->hasError)) { + m_jobs.erase(it); + return; + } + + if (response.status == SSH_FX_OK) { + if (job->parentJob) { + job->parentJob->uploadsInProgress.removeOne(job); + if (job->parentJob->mkdirsInProgress.isEmpty() + && job->parentJob->uploadsInProgress.isEmpty()) + emit finished(job->parentJob->jobId); + } else { + emit finished(job->jobId); + } + } else { + const QString error = errorMessage(response.errorString, + tr("Failed to close remote file.")); + if (job->parentJob) { + job->parentJob->setError(); + emit finished(job->parentJob->jobId, error); + } else { + emit finished(job->jobId, error); + } + } + m_jobs.erase(it); + break; + default: + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_FXP_STATUS packet."); + } +} + +void SftpChannelPrivate::handleName() +{ + const SftpNameResponse &response = m_incomingPacket.asNameResponse(); + JobMap::Iterator it = lookupJob(response.requestId); + switch (it.value()->type()) { + case AbstractSftpOperation::ListDir: { + SftpListDir::Ptr op = it.value().staticCast(); + if (op->state != SftpListDir::Open) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_FXP_NAME packet."); + } + + QList fileInfoList; + for (int i = 0; i < response.files.count(); ++i) { + const SftpFile &file = response.files.at(i); + + SftpFileInfo fileInfo; + fileInfo.name = file.fileName; + attributesToFileInfo(file.attributes, fileInfo); + fileInfoList << fileInfo; + } + emit fileInfoAvailable(op->jobId, fileInfoList); + sendData(m_outgoingPacket.generateReadDir(op->remoteHandle, + op->jobId).rawData()); + break; + } + default: + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_FXP_NAME packet."); + } +} + +void SftpChannelPrivate::handleReadData() +{ + const SftpDataResponse &response = m_incomingPacket.asDataResponse(); + JobMap::Iterator it = lookupJob(response.requestId); + if (it.value()->type() != AbstractSftpOperation::Download) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_FXP_DATA packet."); + } + + SftpDownload::Ptr op = it.value().staticCast(); + if (op->hasError) { + finishTransferRequest(it); + return; + } + + if (!op->localFile->seek(op->offsets[response.requestId])) { + reportRequestError(op, op->localFile->errorString()); + finishTransferRequest(it); + return; + } + + if (op->localFile->write(response.data) != response.data.size()) { + reportRequestError(op, op->localFile->errorString()); + finishTransferRequest(it); + return; + } + + if (op->offset >= op->fileSize && op->fileSize != 0) + finishTransferRequest(it); + else + sendReadRequest(op, response.requestId); +} + +void SftpChannelPrivate::handleAttrs() +{ + const SftpAttrsResponse &response = m_incomingPacket.asAttrsResponse(); + JobMap::Iterator it = lookupJob(response.requestId); + + SftpStatFile::Ptr statOp = it.value().dynamicCast(); + if (statOp) { + SftpFileInfo fileInfo; + fileInfo.name = QFileInfo(statOp->path).fileName(); + attributesToFileInfo(response.attrs, fileInfo); + emit fileInfoAvailable(it.key(), QList() << fileInfo); + emit finished(it.key()); + m_jobs.erase(it); + return; + } + + AbstractSftpTransfer::Ptr transfer + = it.value().dynamicCast(); + if (!transfer || transfer->state != AbstractSftpTransfer::Open + || !transfer->statRequested) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_FXP_ATTRS packet."); + } + Q_ASSERT(transfer->type() == AbstractSftpOperation::UploadFile + || transfer->type() == AbstractSftpOperation::Download); + + if (transfer->type() == AbstractSftpOperation::Download) { + SftpDownload::Ptr op = transfer.staticCast(); + if (response.attrs.sizePresent) { + op->fileSize = response.attrs.size; + } else { + op->fileSize = 0; + op->eofId = op->jobId; + } + op->statRequested = false; + spawnReadRequests(op); + } else { + SftpUploadFile::Ptr op = transfer.staticCast(); + if (op->parentJob && op->parentJob->hasError) { + op->hasError = true; + sendTransferCloseHandle(op, op->jobId); + return; + } + + if (response.attrs.sizePresent) { + op->offset = response.attrs.size; + spawnWriteRequests(it); + } else { + if (op->parentJob) + op->parentJob->setError(); + reportRequestError(op, tr("Cannot append to remote file: " + "Server does not support the file size attribute.")); + sendTransferCloseHandle(op, op->jobId); + } + } +} + +SftpChannelPrivate::JobMap::Iterator SftpChannelPrivate::lookupJob(SftpJobId id) +{ + JobMap::Iterator it = m_jobs.find(id); + if (it == m_jobs.end()) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid request id in SFTP packet."); + } + return it; +} + +void SftpChannelPrivate::closeHook() +{ + m_jobs.clear(); + m_incomingData.clear(); + m_incomingPacket.clear(); + emit closed(); +} + +void SftpChannelPrivate::handleOpenSuccessInternal() +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("SFTP session started"); +#endif + m_sendFacility.sendSftpPacket(remoteChannel()); + m_sftpState = SubsystemRequested; +} + +void SftpChannelPrivate::handleOpenFailureInternal(const QString &reason) +{ + if (channelState() != SessionRequested) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_MSG_CHANNEL_OPEN_FAILURE packet."); + } + emit initializationFailed(tr("Server could not start session: %1").arg(reason)); +} + +void SftpChannelPrivate::sendReadRequest(const SftpDownload::Ptr &job, + quint32 requestId) +{ + Q_ASSERT(job->eofId == SftpInvalidJob); + sendData(m_outgoingPacket.generateReadFile(job->remoteHandle, job->offset, + AbstractSftpPacket::MaxDataSize, requestId).rawData()); + job->offsets[requestId] = job->offset; + job->offset += AbstractSftpPacket::MaxDataSize; + if (job->offset >= job->fileSize) + job->eofId = requestId; +} + +void SftpChannelPrivate::reportRequestError(const AbstractSftpOperationWithHandle::Ptr &job, + const QString &error) +{ + emit finished(job->jobId, error); + job->hasError = true; +} + +void SftpChannelPrivate::finishTransferRequest(const JobMap::Iterator &it) +{ + AbstractSftpTransfer::Ptr job = it.value().staticCast(); + if (job->inFlightCount == 1) + sendTransferCloseHandle(job, it.key()); + else + removeTransferRequest(it); +} + +void SftpChannelPrivate::sendTransferCloseHandle(const AbstractSftpTransfer::Ptr &job, + quint32 requestId) +{ + sendData(m_outgoingPacket.generateCloseHandle(job->remoteHandle, + requestId).rawData()); + job->state = SftpDownload::CloseRequested; +} + +void SftpChannelPrivate::attributesToFileInfo(const SftpFileAttributes &attributes, + SftpFileInfo &fileInfo) const +{ + if (attributes.sizePresent) { + fileInfo.sizeValid = true; + fileInfo.size = attributes.size; + } + if (attributes.permissionsPresent) { + if (attributes.permissions & 0x8000) // S_IFREG + fileInfo.type = FileTypeRegular; + else if (attributes.permissions & 0x4000) // S_IFDIR + fileInfo.type = FileTypeDirectory; + else + fileInfo.type = FileTypeOther; + fileInfo.permissionsValid = true; + fileInfo.permissions = 0; + if (attributes.permissions & 00001) // S_IXOTH + fileInfo.permissions |= QFile::ExeOther; + if (attributes.permissions & 00002) // S_IWOTH + fileInfo.permissions |= QFile::WriteOther; + if (attributes.permissions & 00004) // S_IROTH + fileInfo.permissions |= QFile::ReadOther; + if (attributes.permissions & 00010) // S_IXGRP + fileInfo.permissions |= QFile::ExeGroup; + if (attributes.permissions & 00020) // S_IWGRP + fileInfo.permissions |= QFile::WriteGroup; + if (attributes.permissions & 00040) // S_IRGRP + fileInfo.permissions |= QFile::ReadGroup; + if (attributes.permissions & 00100) // S_IXUSR + fileInfo.permissions |= QFile::ExeUser | QFile::ExeOwner; + if (attributes.permissions & 00200) // S_IWUSR + fileInfo.permissions |= QFile::WriteUser | QFile::WriteOwner; + if (attributes.permissions & 00400) // S_IRUSR + fileInfo.permissions |= QFile::ReadUser | QFile::ReadOwner; + } +} + +void SftpChannelPrivate::removeTransferRequest(const JobMap::Iterator &it) +{ + --it.value().staticCast()->inFlightCount; + m_jobs.erase(it); +} + +void SftpChannelPrivate::sendWriteRequest(const JobMap::Iterator &it) +{ + SftpUploadFile::Ptr job = it.value().staticCast(); + QByteArray data = job->localFile->read(AbstractSftpPacket::MaxDataSize); + if (job->localFile->error() != QFile::NoError) { + if (job->parentJob) + job->parentJob->setError(); + reportRequestError(job, tr("Error reading local file: %1") + .arg(job->localFile->errorString())); + finishTransferRequest(it); + } else if (data.isEmpty()) { + finishTransferRequest(it); + } else { + sendData(m_outgoingPacket.generateWriteFile(job->remoteHandle, + job->offset, data, it.key()).rawData()); + job->offset += AbstractSftpPacket::MaxDataSize; + } +} + +void SftpChannelPrivate::spawnWriteRequests(const JobMap::Iterator &it) +{ + SftpUploadFile::Ptr op = it.value().staticCast(); + op->calculateInFlightCount(AbstractSftpPacket::MaxDataSize); + sendWriteRequest(it); + for (int i = 1; !op->hasError && i < op->inFlightCount; ++i) + sendWriteRequest(m_jobs.insert(++m_nextJobId, op)); +} + +void SftpChannelPrivate::spawnReadRequests(const SftpDownload::Ptr &job) +{ + job->calculateInFlightCount(AbstractSftpPacket::MaxDataSize); + sendReadRequest(job, job->jobId); + for (int i = 1; i < job->inFlightCount; ++i) { + const quint32 requestId = ++m_nextJobId; + m_jobs.insert(requestId, job); + sendReadRequest(job, requestId); + } +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sftpchannel.h b/src/libs/ssh/sftpchannel.h new file mode 100644 index 0000000000..e1a5605023 --- /dev/null +++ b/src/libs/ssh/sftpchannel.h @@ -0,0 +1,113 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SFTCHANNEL_H +#define SFTCHANNEL_H + +#include "sftpdefs.h" +#include "sftpincomingpacket_p.h" + +#include "ssh_global.h" + +#include +#include +#include +#include + +namespace QSsh { + +namespace Internal { +class SftpChannelPrivate; +class SshChannelManager; +class SshSendFacility; +} // namespace Internal + +class QSSH_EXPORT SftpChannel : public QObject +{ + Q_OBJECT + + friend class Internal::SftpChannelPrivate; + friend class Internal::SshChannelManager; +public: + typedef QSharedPointer Ptr; + + enum State { Uninitialized, Initializing, Initialized, Closing, Closed }; + State state() const; + + void initialize(); + void closeChannel(); + + SftpJobId statFile(const QString &path); + SftpJobId listDirectory(const QString &dirPath); + SftpJobId createDirectory(const QString &dirPath); + SftpJobId removeDirectory(const QString &dirPath); + SftpJobId removeFile(const QString &filePath); + SftpJobId renameFileOrDirectory(const QString &oldPath, + const QString &newPath); + SftpJobId createFile(const QString &filePath, SftpOverwriteMode mode); + SftpJobId createLink(const QString &filePath, const QString &target); + SftpJobId uploadFile(const QString &localFilePath, + const QString &remoteFilePath, SftpOverwriteMode mode); + SftpJobId downloadFile(const QString &remoteFilePath, + const QString &localFilePath, SftpOverwriteMode mode); + SftpJobId uploadDir(const QString &localDirPath, + const QString &remoteParentDirPath); + + ~SftpChannel(); + +signals: + void initialized(); + void initializationFailed(const QString &reason); + void closed(); + + // error.isEmpty <=> finished successfully + void finished(QSsh::SftpJobId job, const QString &error = QString()); + + // TODO: Also emit for each file copied by uploadDir(). + void dataAvailable(QSsh::SftpJobId job, const QString &data); + + /* + * This signal is emitted as a result of: + * - statFile() (with the list having exactly one element) + * - listDirectory() (potentially more than once) + */ + void fileInfoAvailable(QSsh::SftpJobId job, const QList &fileInfoList); + +private: + SftpChannel(quint32 channelId, Internal::SshSendFacility &sendFacility); + + Internal::SftpChannelPrivate *d; +}; + +} // namespace QSsh + +#endif // SFTPCHANNEL_H diff --git a/src/libs/ssh/sftpchannel_p.h b/src/libs/ssh/sftpchannel_p.h new file mode 100644 index 0000000000..040a39262a --- /dev/null +++ b/src/libs/ssh/sftpchannel_p.h @@ -0,0 +1,135 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SFTCHANNEL_P_H +#define SFTCHANNEL_P_H + +#include "sftpdefs.h" +#include "sftpincomingpacket_p.h" +#include "sftpoperation_p.h" +#include "sftpoutgoingpacket_p.h" +#include "sshchannel_p.h" + +#include +#include + +namespace QSsh { +class SftpChannel; +namespace Internal { + +class SftpChannelPrivate : public AbstractSshChannel +{ + Q_OBJECT + friend class QSsh::SftpChannel; +public: + + enum SftpState { Inactive, SubsystemRequested, InitSent, Initialized }; + + virtual void handleChannelSuccess(); + virtual void handleChannelFailure(); + + virtual void closeHook(); + +signals: + void initialized(); + void initializationFailed(const QString &reason); + void closed(); + void finished(QSsh::SftpJobId job, const QString &error = QString()); + void dataAvailable(QSsh::SftpJobId job, const QString &data); + void fileInfoAvailable(QSsh::SftpJobId job, const QList &fileInfoList); + +private: + typedef QMap JobMap; + + SftpChannelPrivate(quint32 channelId, SshSendFacility &sendFacility, + SftpChannel *sftp); + SftpJobId createJob(const AbstractSftpOperation::Ptr &job); + + virtual void handleOpenSuccessInternal(); + virtual void handleOpenFailureInternal(const QString &reason); + virtual void handleChannelDataInternal(const QByteArray &data); + virtual void handleChannelExtendedDataInternal(quint32 type, + const QByteArray &data); + virtual void handleExitStatus(const SshChannelExitStatus &exitStatus); + virtual void handleExitSignal(const SshChannelExitSignal &signal); + + void handleCurrentPacket(); + void handleServerVersion(); + void handleHandle(); + void handleStatus(); + void handleName(); + void handleReadData(); + void handleAttrs(); + + void handleStatusGeneric(const JobMap::Iterator &it, + const SftpStatusResponse &response); + void handleMkdirStatus(const JobMap::Iterator &it, + const SftpStatusResponse &response); + void handleLsStatus(const JobMap::Iterator &it, + const SftpStatusResponse &response); + void handleGetStatus(const JobMap::Iterator &it, + const SftpStatusResponse &response); + void handlePutStatus(const JobMap::Iterator &it, + const SftpStatusResponse &response); + + void handleLsHandle(const JobMap::Iterator &it); + void handleCreateFileHandle(const JobMap::Iterator &it); + void handleGetHandle(const JobMap::Iterator &it); + void handlePutHandle(const JobMap::Iterator &it); + + void spawnReadRequests(const SftpDownload::Ptr &job); + void spawnWriteRequests(const JobMap::Iterator &it); + void sendReadRequest(const SftpDownload::Ptr &job, quint32 requestId); + void sendWriteRequest(const JobMap::Iterator &it); + void finishTransferRequest(const JobMap::Iterator &it); + void removeTransferRequest(const JobMap::Iterator &it); + void reportRequestError(const AbstractSftpOperationWithHandle::Ptr &job, + const QString &error); + void sendTransferCloseHandle(const AbstractSftpTransfer::Ptr &job, + quint32 requestId); + + void attributesToFileInfo(const SftpFileAttributes &attributes, SftpFileInfo &fileInfo) const; + + JobMap::Iterator lookupJob(SftpJobId id); + JobMap m_jobs; + SftpOutgoingPacket m_outgoingPacket; + SftpIncomingPacket m_incomingPacket; + QByteArray m_incomingData; + SftpJobId m_nextJobId; + SftpState m_sftpState; + SftpChannel *m_sftp; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SFTPCHANNEL_P_H diff --git a/src/libs/ssh/sftpdefs.cpp b/src/libs/ssh/sftpdefs.cpp new file mode 100644 index 0000000000..f8c1b957b9 --- /dev/null +++ b/src/libs/ssh/sftpdefs.cpp @@ -0,0 +1,35 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sftpdefs.h" + +namespace QSsh { const SftpJobId SftpInvalidJob = 0; } diff --git a/src/libs/ssh/sftpdefs.h b/src/libs/ssh/sftpdefs.h new file mode 100644 index 0000000000..c383b12472 --- /dev/null +++ b/src/libs/ssh/sftpdefs.h @@ -0,0 +1,69 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SFTPDEFS_H +#define SFTPDEFS_H + +#include "ssh_global.h" + +#include +#include + +namespace QSsh { + +typedef quint32 SftpJobId; +QSSH_EXPORT extern const SftpJobId SftpInvalidJob; + +enum SftpOverwriteMode { + SftpOverwriteExisting, SftpAppendToExisting, SftpSkipExisting +}; + +enum SftpFileType { FileTypeRegular, FileTypeDirectory, FileTypeOther, FileTypeUnknown }; + +class QSSH_EXPORT SftpFileInfo +{ +public: + SftpFileInfo() : type(FileTypeUnknown), sizeValid(false), permissionsValid(false) { } + + QString name; + SftpFileType type; + quint64 size; + QFile::Permissions permissions; + + // The RFC allows an SFTP server not to support any file attributes beyond the name. + bool sizeValid; + bool permissionsValid; +}; + +} // namespace QSsh + +#endif // SFTPDEFS_H diff --git a/src/libs/ssh/sftpfilesystemmodel.cpp b/src/libs/ssh/sftpfilesystemmodel.cpp new file mode 100644 index 0000000000..6d24c1b8c6 --- /dev/null +++ b/src/libs/ssh/sftpfilesystemmodel.cpp @@ -0,0 +1,386 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ +#include "sftpfilesystemmodel.h" + +#include "sftpchannel.h" +#include "sshconnection.h" +#include "sshconnectionmanager.h" + +#include +#include +#include +#include +#include + +namespace QSsh { +namespace Internal { +namespace { + +class SftpDirNode; +class SftpFileNode +{ +public: + SftpFileNode() : parent(0) { } + virtual ~SftpFileNode() { } + + QString path; + SftpFileInfo fileInfo; + SftpDirNode *parent; +}; + +class SftpDirNode : public SftpFileNode +{ +public: + SftpDirNode() : lsState(LsNotYetCalled) { } + ~SftpDirNode() { qDeleteAll(children); } + + enum { LsNotYetCalled, LsRunning, LsFinished } lsState; + QList children; +}; + +typedef QHash DirNodeHash; + +SftpFileNode *indexToFileNode(const QModelIndex &index) +{ + return static_cast(index.internalPointer()); +} + +SftpDirNode *indexToDirNode(const QModelIndex &index) +{ + SftpFileNode * const fileNode = indexToFileNode(index); + QSSH_ASSERT(fileNode); + return dynamic_cast(fileNode); +} + +} // anonymous namespace + +class SftpFileSystemModelPrivate +{ +public: + SshConnection::Ptr sshConnection; + SftpChannel::Ptr sftpChannel; + QString rootDirectory; + SftpFileNode *rootNode; + SftpJobId statJobId; + DirNodeHash lsOps; + QList externalJobs; +}; +} // namespace Internal + +using namespace Internal; + +SftpFileSystemModel::SftpFileSystemModel(QObject *parent) + : QAbstractItemModel(parent), d(new SftpFileSystemModelPrivate) +{ + d->rootDirectory = QLatin1String("/"); + d->rootNode = 0; + d->statJobId = SftpInvalidJob; +} + +SftpFileSystemModel::~SftpFileSystemModel() +{ + shutDown(); + delete d; +} + +void SftpFileSystemModel::setSshConnection(const SshConnectionParameters &sshParams) +{ + QSSH_ASSERT_AND_RETURN(!d->sshConnection); + d->sshConnection = SshConnectionManager::instance().acquireConnection(sshParams); + connect(d->sshConnection.data(), SIGNAL(error(QSsh::SshError)), + SLOT(handleSshConnectionFailure())); + if (d->sshConnection->state() == SshConnection::Connected) { + handleSshConnectionEstablished(); + return; + } + connect(d->sshConnection.data(), SIGNAL(connected()), SLOT(handleSshConnectionEstablished())); + if (d->sshConnection->state() == SshConnection::Unconnected) + d->sshConnection->connectToHost(); +} + +void SftpFileSystemModel::setRootDirectory(const QString &path) +{ + beginResetModel(); + d->rootDirectory = path; + delete d->rootNode; + d->rootNode = 0; + d->lsOps.clear(); + d->statJobId = SftpInvalidJob; + endResetModel(); + statRootDirectory(); +} + +QString SftpFileSystemModel::rootDirectory() const +{ + return d->rootDirectory; +} + +SftpJobId SftpFileSystemModel::downloadFile(const QModelIndex &index, const QString &targetFilePath) +{ + QSSH_ASSERT_AND_RETURN_VALUE(d->rootNode, SftpInvalidJob); + const SftpFileNode * const fileNode = indexToFileNode(index); + QSSH_ASSERT_AND_RETURN_VALUE(fileNode, SftpInvalidJob); + QSSH_ASSERT_AND_RETURN_VALUE(fileNode->fileInfo.type == FileTypeRegular, SftpInvalidJob); + const SftpJobId jobId = d->sftpChannel->downloadFile(fileNode->path, targetFilePath, + SftpOverwriteExisting); + if (jobId != SftpInvalidJob) + d->externalJobs << jobId; + return jobId; +} + +int SftpFileSystemModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return 2; // type + name +} + +QVariant SftpFileSystemModel::data(const QModelIndex &index, int role) const +{ + const SftpFileNode * const node = indexToFileNode(index); + if (index.column() == 0 && role == Qt::DecorationRole) { + switch (node->fileInfo.type) { + case FileTypeRegular: + case FileTypeOther: + return QIcon(QLatin1String(":/core/images/unknownfile.png")); + case FileTypeDirectory: + return QIcon(QLatin1String(":/core/images/dir.png")); + case FileTypeUnknown: + return QIcon(QLatin1String(":/core/images/help.png")); // Shows a question mark. + } + } + if (index.column() == 1) { + if (role == Qt::DisplayRole) + return node->fileInfo.name; + if (role == PathRole) + return node->path; + } + return QVariant(); +} + +Qt::ItemFlags SftpFileSystemModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) + return Qt::NoItemFlags; + return Qt::ItemIsSelectable | Qt::ItemIsEnabled; +} + +QVariant SftpFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation != Qt::Horizontal) + return QVariant(); + if (role != Qt::DisplayRole) + return QVariant(); + if (section == 0) + return tr("File Type"); + if (section == 1) + return tr("File Name"); + return QVariant(); +} + +QModelIndex SftpFileSystemModel::index(int row, int column, const QModelIndex &parent) const +{ + if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent)) + return QModelIndex(); + if (!d->rootNode) + return QModelIndex(); + if (!parent.isValid()) + return createIndex(row, column, d->rootNode); + const SftpDirNode * const parentNode = indexToDirNode(parent); + QSSH_ASSERT_AND_RETURN_VALUE(parentNode, QModelIndex()); + QSSH_ASSERT_AND_RETURN_VALUE(row < parentNode->children.count(), QModelIndex()); + SftpFileNode * const childNode = parentNode->children.at(row); + return createIndex(row, column, childNode); +} + +QModelIndex SftpFileSystemModel::parent(const QModelIndex &child) const +{ + if (!child.isValid()) // Don't assert on this, since the model tester tries it. + return QModelIndex(); + + const SftpFileNode * const childNode = indexToFileNode(child); + QSSH_ASSERT_AND_RETURN_VALUE(childNode, QModelIndex()); + if (childNode == d->rootNode) + return QModelIndex(); + SftpDirNode * const parentNode = childNode->parent; + if (parentNode == d->rootNode) + return createIndex(0, 0, d->rootNode); + const SftpDirNode * const grandParentNode = parentNode->parent; + QSSH_ASSERT_AND_RETURN_VALUE(grandParentNode, QModelIndex()); + return createIndex(grandParentNode->children.indexOf(parentNode), 0, parentNode); +} + +int SftpFileSystemModel::rowCount(const QModelIndex &parent) const +{ + if (!d->rootNode) + return 0; + if (!parent.isValid()) + return 1; + if (parent.column() != 0) + return 0; + SftpDirNode * const dirNode = indexToDirNode(parent); + if (!dirNode) + return 0; + if (dirNode->lsState != SftpDirNode::LsNotYetCalled) + return dirNode->children.count(); + d->lsOps.insert(d->sftpChannel->listDirectory(dirNode->path), dirNode); + dirNode->lsState = SftpDirNode::LsRunning; + return 0; +} + +void SftpFileSystemModel::statRootDirectory() +{ + d->statJobId = d->sftpChannel->statFile(d->rootDirectory); +} + +void SftpFileSystemModel::shutDown() +{ + if (d->sftpChannel) { + disconnect(d->sftpChannel.data(), 0, this, 0); + d->sftpChannel->closeChannel(); + d->sftpChannel.clear(); + } + if (d->sshConnection) { + disconnect(d->sshConnection.data(), 0, this, 0); + SshConnectionManager::instance().releaseConnection(d->sshConnection); + d->sshConnection.clear(); + } + delete d->rootNode; + d->rootNode = 0; +} + +void SftpFileSystemModel::handleSshConnectionFailure() +{ + emit connectionError(d->sshConnection->errorString()); + beginResetModel(); + shutDown(); + endResetModel(); +} + +void SftpFileSystemModel::handleSftpChannelInitialized() +{ + connect(d->sftpChannel.data(), + SIGNAL(fileInfoAvailable(QSsh::SftpJobId,QList)), + SLOT(handleFileInfo(QSsh::SftpJobId,QList))); + connect(d->sftpChannel.data(), SIGNAL(finished(QSsh::SftpJobId,QString)), + SLOT(handleSftpJobFinished(QSsh::SftpJobId,QString))); + statRootDirectory(); +} + +void SftpFileSystemModel::handleSshConnectionEstablished() +{ + d->sftpChannel = d->sshConnection->createSftpChannel(); + connect(d->sftpChannel.data(), SIGNAL(initialized()), SLOT(handleSftpChannelInitialized())); + connect(d->sftpChannel.data(), SIGNAL(initializationFailed(QString)), + SLOT(handleSftpChannelInitializationFailed(QString))); + d->sftpChannel->initialize(); +} + +void SftpFileSystemModel::handleSftpChannelInitializationFailed(const QString &reason) +{ + emit connectionError(reason); + beginResetModel(); + shutDown(); + endResetModel(); +} + +void SftpFileSystemModel::handleFileInfo(SftpJobId jobId, const QList &fileInfoList) +{ + if (jobId == d->statJobId) { + QSSH_ASSERT_AND_RETURN(!d->rootNode); + beginInsertRows(QModelIndex(), 0, 0); + d->rootNode = new SftpDirNode; + d->rootNode->path = d->rootDirectory; + d->rootNode->fileInfo = fileInfoList.first(); + d->rootNode->fileInfo.name = d->rootDirectory == QLatin1String("/") + ? d->rootDirectory : QFileInfo(d->rootDirectory).fileName(); + endInsertRows(); + return; + } + SftpDirNode * const parentNode = d->lsOps.value(jobId); + QSSH_ASSERT_AND_RETURN(parentNode); + QList filteredList; + foreach (const SftpFileInfo &fi, fileInfoList) { + if (fi.name != QLatin1String(".") && fi.name != QLatin1String("..")) + filteredList << fi; + } + if (filteredList.isEmpty()) + return; + + // In theory beginInsertRows() should suffice, but that fails to have an effect + // if rowCount() returned 0 earlier. + emit layoutAboutToBeChanged(); + + foreach (const SftpFileInfo &fileInfo, filteredList) { + SftpFileNode *childNode; + if (fileInfo.type == FileTypeDirectory) + childNode = new SftpDirNode; + else + childNode = new SftpFileNode; + childNode->path = parentNode->path; + if (!childNode->path.endsWith(QLatin1Char('/'))) + childNode->path += QLatin1Char('/'); + childNode->path += fileInfo.name; + childNode->fileInfo = fileInfo; + childNode->parent = parentNode; + parentNode->children << childNode; + } + emit layoutChanged(); // Should be endInsertRows(), see above. +} + +void SftpFileSystemModel::handleSftpJobFinished(SftpJobId jobId, const QString &errorMessage) +{ + if (jobId == d->statJobId) { + d->statJobId = SftpInvalidJob; + if (!errorMessage.isEmpty()) + emit sftpOperationFailed(tr("Error getting 'stat' info about '%1': %2") + .arg(rootDirectory(), errorMessage)); + return; + } + + DirNodeHash::Iterator it = d->lsOps.find(jobId); + if (it != d->lsOps.end()) { + QSSH_ASSERT(it.value()->lsState == SftpDirNode::LsRunning); + it.value()->lsState = SftpDirNode::LsFinished; + if (!errorMessage.isEmpty()) + emit sftpOperationFailed(tr("Error listing contents of directory '%1': %2") + .arg(it.value()->path, errorMessage)); + d->lsOps.erase(it); + return; + } + + const int jobIndex = d->externalJobs.indexOf(jobId); + QSSH_ASSERT_AND_RETURN(jobIndex != -1); + d->externalJobs.removeAt(jobIndex); + emit sftpOperationFinished(jobId, errorMessage); +} + +} // namespace QSsh diff --git a/src/libs/ssh/sftpfilesystemmodel.h b/src/libs/ssh/sftpfilesystemmodel.h new file mode 100644 index 0000000000..43690cff3f --- /dev/null +++ b/src/libs/ssh/sftpfilesystemmodel.h @@ -0,0 +1,110 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ +#ifndef SFTPFILESYSTEMMODEL_H +#define SFTPFILESYSTEMMODEL_H + +#include "sftpdefs.h" + +#include "ssh_global.h" + +#include + +namespace QSsh { +class SshConnectionParameters; + +namespace Internal { class SftpFileSystemModelPrivate; } + +// Very simple read-only model. Symbolic links are not followed. +class QSSH_EXPORT SftpFileSystemModel : public QAbstractItemModel +{ + Q_OBJECT +public: + // Use this to get the full path of a file or directory via data(). + static const int PathRole = Qt::UserRole; + + explicit SftpFileSystemModel(QObject *parent = 0); + ~SftpFileSystemModel(); + + /* + * Once this is called, an SFTP connection is established and the model is populated. + * The effect of additional calls is undefined. + */ + void setSshConnection(const SshConnectionParameters &sshParams); + + void setRootDirectory(const QString &path); // Default is "/". + QString rootDirectory() const; + + SftpJobId downloadFile(const QModelIndex &index, const QString &targetFilePath); + +signals: + /* + * E.g. "Permission denied". Note that this can happen without direct user intervention, + * due to e.g. the view calling rowCount() on a non-readable directory. This signal should + * therefore not result in a message box or similar, since it might occur very often. + */ + void sftpOperationFailed(const QString &errorMessage); + + /* + * This error is not recoverable. The model will not have any content after + * the signal has been emitted. + */ + void connectionError(const QString &errorMessage); + + // Success <=> error.isEmpty(). + void sftpOperationFinished(QSsh::SftpJobId, const QString &error); + +private slots: + void handleSshConnectionEstablished(); + void handleSshConnectionFailure(); + void handleSftpChannelInitialized(); + void handleSftpChannelInitializationFailed(const QString &reason); + void handleFileInfo(QSsh::SftpJobId jobId, const QList &fileInfoList); + void handleSftpJobFinished(QSsh::SftpJobId jobId, const QString &errorMessage); + +private: + int columnCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + Qt::ItemFlags flags(const QModelIndex &index) const; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; + QModelIndex parent(const QModelIndex &child) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + + void statRootDirectory(); + void shutDown(); + + Internal::SftpFileSystemModelPrivate * const d; +}; + +} // namespace QSsh; + +#endif // SFTPFILESYSTEMMODEL_H diff --git a/src/libs/ssh/sftpincomingpacket.cpp b/src/libs/ssh/sftpincomingpacket.cpp new file mode 100644 index 0000000000..8b878bb699 --- /dev/null +++ b/src/libs/ssh/sftpincomingpacket.cpp @@ -0,0 +1,225 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sftpincomingpacket_p.h" + +#include "sshexception_p.h" +#include "sshpacketparser_p.h" + +namespace QSsh { +namespace Internal { + +SftpIncomingPacket::SftpIncomingPacket() : m_length(0) +{ +} + +void SftpIncomingPacket::consumeData(QByteArray &newData) +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("%s: current data size = %d, new data size = %d", Q_FUNC_INFO, + m_data.size(), newData.size()); +#endif + + if (isComplete() || dataSize() + newData.size() < sizeof m_length) + return; + + if (dataSize() < sizeof m_length) { + moveFirstBytes(m_data, newData, sizeof m_length - m_data.size()); + m_length = SshPacketParser::asUint32(m_data, static_cast(0)); + if (m_length < static_cast(TypeOffset + 1) + || m_length > MaxPacketSize) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid length field in SFTP packet."); + } + } + + moveFirstBytes(m_data, newData, + qMin(m_length - dataSize() + 4, newData.size())); +} + +void SftpIncomingPacket::moveFirstBytes(QByteArray &target, QByteArray &source, + int n) +{ + target.append(source.left(n)); + source.remove(0, n); +} + +bool SftpIncomingPacket::isComplete() const +{ + return m_length == dataSize() - 4; +} + +void SftpIncomingPacket::clear() +{ + m_data.clear(); + m_length = 0; +} + +quint32 SftpIncomingPacket::extractServerVersion() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_FXP_VERSION); + try { + return SshPacketParser::asUint32(m_data, TypeOffset + 1); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_FXP_VERSION packet."); + } +} + +SftpHandleResponse SftpIncomingPacket::asHandleResponse() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_FXP_HANDLE); + try { + SftpHandleResponse response; + quint32 offset = RequestIdOffset; + response.requestId = SshPacketParser::asUint32(m_data, &offset); + response.handle = SshPacketParser::asString(m_data, &offset); + return response; + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_FXP_HANDLE packet"); + } +} + +SftpStatusResponse SftpIncomingPacket::asStatusResponse() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_FXP_STATUS); + try { + SftpStatusResponse response; + quint32 offset = RequestIdOffset; + response.requestId = SshPacketParser::asUint32(m_data, &offset); + response.status = static_cast(SshPacketParser::asUint32(m_data, &offset)); + response.errorString = SshPacketParser::asUserString(m_data, &offset); + response.language = SshPacketParser::asString(m_data, &offset); + return response; + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_FXP_STATUS packet."); + } +} + +SftpNameResponse SftpIncomingPacket::asNameResponse() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_FXP_NAME); + try { + SftpNameResponse response; + quint32 offset = RequestIdOffset; + response.requestId = SshPacketParser::asUint32(m_data, &offset); + const quint32 count = SshPacketParser::asUint32(m_data, &offset); + for (quint32 i = 0; i < count; ++i) + response.files << asFile(offset); + return response; + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_FXP_NAME packet."); + } +} + +SftpDataResponse SftpIncomingPacket::asDataResponse() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_FXP_DATA); + try { + SftpDataResponse response; + quint32 offset = RequestIdOffset; + response.requestId = SshPacketParser::asUint32(m_data, &offset); + response.data = SshPacketParser::asString(m_data, &offset); + return response; + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_FXP_DATA packet."); + } +} + +SftpAttrsResponse SftpIncomingPacket::asAttrsResponse() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_FXP_ATTRS); + try { + SftpAttrsResponse response; + quint32 offset = RequestIdOffset; + response.requestId = SshPacketParser::asUint32(m_data, &offset); + response.attrs = asFileAttributes(offset); + return response; + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_FXP_ATTRS packet."); + } +} + +SftpFile SftpIncomingPacket::asFile(quint32 &offset) const +{ + SftpFile file; + file.fileName + = QString::fromUtf8(SshPacketParser::asString(m_data, &offset)); + file.longName + = QString::fromUtf8(SshPacketParser::asString(m_data, &offset)); + file.attributes = asFileAttributes(offset); + return file; +} + +SftpFileAttributes SftpIncomingPacket::asFileAttributes(quint32 &offset) const +{ + SftpFileAttributes attributes; + const quint32 flags = SshPacketParser::asUint32(m_data, &offset); + attributes.sizePresent = flags & SSH_FILEXFER_ATTR_SIZE; + attributes.timesPresent = flags & SSH_FILEXFER_ATTR_ACMODTIME; + attributes.uidAndGidPresent = flags & SSH_FILEXFER_ATTR_UIDGID; + attributes.permissionsPresent = flags & SSH_FILEXFER_ATTR_PERMISSIONS; + if (attributes.sizePresent) + attributes.size = SshPacketParser::asUint64(m_data, &offset); + if (attributes.uidAndGidPresent) { + attributes.uid = SshPacketParser::asUint32(m_data, &offset); + attributes.gid = SshPacketParser::asUint32(m_data, &offset); + } + if (attributes.permissionsPresent) + attributes.permissions = SshPacketParser::asUint32(m_data, &offset); + if (attributes.timesPresent) { + attributes.atime = SshPacketParser::asUint32(m_data, &offset); + attributes.mtime = SshPacketParser::asUint32(m_data, &offset); + } + if (flags & SSH_FILEXFER_ATTR_EXTENDED) { + const quint32 count = SshPacketParser::asUint32(m_data, &offset); + for (quint32 i = 0; i < count; ++i) { + SshPacketParser::asString(m_data, &offset); + SshPacketParser::asString(m_data, &offset); + } + } + return attributes; +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sftpincomingpacket_p.h b/src/libs/ssh/sftpincomingpacket_p.h new file mode 100644 index 0000000000..7f62ffcdce --- /dev/null +++ b/src/libs/ssh/sftpincomingpacket_p.h @@ -0,0 +1,114 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SFTPINCOMINGPACKET_P_H +#define SFTPINCOMINGPACKET_P_H + +#include "sftppacket_p.h" + +namespace QSsh { +namespace Internal { + +struct SftpHandleResponse { + quint32 requestId; + QByteArray handle; +}; + +struct SftpStatusResponse { + quint32 requestId; + SftpStatusCode status; + QString errorString; + QByteArray language; +}; + +struct SftpFileAttributes { + bool sizePresent; + bool timesPresent; + bool uidAndGidPresent; + bool permissionsPresent; + quint64 size; + quint32 uid; + quint32 gid; + quint32 permissions; + quint32 atime; + quint32 mtime; +}; + +struct SftpFile { + QString fileName; + QString longName; // Not present in later RFCs, so we don't expose this to the user. + SftpFileAttributes attributes; +}; + +struct SftpNameResponse { + quint32 requestId; + QList files; +}; + +struct SftpDataResponse { + quint32 requestId; + QByteArray data; +}; + +struct SftpAttrsResponse { + quint32 requestId; + SftpFileAttributes attrs; +}; + +class SftpIncomingPacket : public AbstractSftpPacket +{ +public: + SftpIncomingPacket(); + + void consumeData(QByteArray &data); + void clear(); + bool isComplete() const; + quint32 extractServerVersion() const; + SftpHandleResponse asHandleResponse() const; + SftpStatusResponse asStatusResponse() const; + SftpNameResponse asNameResponse() const; + SftpDataResponse asDataResponse() const; + SftpAttrsResponse asAttrsResponse() const; + +private: + void moveFirstBytes(QByteArray &target, QByteArray &source, int n); + + SftpFileAttributes asFileAttributes(quint32 &offset) const; + SftpFile asFile(quint32 &offset) const; + + quint32 m_length; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SFTPINCOMINGPACKET_P_H diff --git a/src/libs/ssh/sftpoperation.cpp b/src/libs/ssh/sftpoperation.cpp new file mode 100644 index 0000000000..1ad62e0dfa --- /dev/null +++ b/src/libs/ssh/sftpoperation.cpp @@ -0,0 +1,227 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sftpoperation_p.h" + +#include "sftpoutgoingpacket_p.h" + +#include + +namespace QSsh { +namespace Internal { + +AbstractSftpOperation::AbstractSftpOperation(SftpJobId jobId) : jobId(jobId) +{ +} + +AbstractSftpOperation::~AbstractSftpOperation() { } + + +SftpStatFile::SftpStatFile(SftpJobId jobId, const QString &path) + : AbstractSftpOperation(jobId), path(path) +{ +} + +SftpOutgoingPacket &SftpStatFile::initialPacket(SftpOutgoingPacket &packet) +{ + return packet.generateStat(path, jobId); +} + +SftpMakeDir::SftpMakeDir(SftpJobId jobId, const QString &path, + const SftpUploadDir::Ptr &parentJob) + : AbstractSftpOperation(jobId), parentJob(parentJob), remoteDir(path) +{ +} + +SftpOutgoingPacket &SftpMakeDir::initialPacket(SftpOutgoingPacket &packet) +{ + return packet.generateMkDir(remoteDir, jobId); +} + + +SftpRmDir::SftpRmDir(SftpJobId, const QString &path) + : AbstractSftpOperation(jobId), remoteDir(path) +{ +} + +SftpOutgoingPacket &SftpRmDir::initialPacket(SftpOutgoingPacket &packet) +{ + return packet.generateRmDir(remoteDir, jobId); +} + + +SftpRm::SftpRm(SftpJobId jobId, const QString &path) + : AbstractSftpOperation(jobId), remoteFile(path) {} + +SftpOutgoingPacket &SftpRm::initialPacket(SftpOutgoingPacket &packet) +{ + return packet.generateRm(remoteFile, jobId); +} + + +SftpRename::SftpRename(SftpJobId jobId, const QString &oldPath, + const QString &newPath) + : AbstractSftpOperation(jobId), oldPath(oldPath), newPath(newPath) +{ +} + +SftpOutgoingPacket &SftpRename::initialPacket(SftpOutgoingPacket &packet) +{ + return packet.generateRename(oldPath, newPath, jobId); +} + + +SftpCreateLink::SftpCreateLink(SftpJobId jobId, const QString &filePath, const QString &target) + : AbstractSftpOperation(jobId), filePath(filePath), target(target) +{ +} + +SftpOutgoingPacket &SftpCreateLink::initialPacket(SftpOutgoingPacket &packet) +{ + return packet.generateCreateLink(filePath, target, jobId); +} + + +AbstractSftpOperationWithHandle::AbstractSftpOperationWithHandle(SftpJobId jobId, + const QString &remotePath) + : AbstractSftpOperation(jobId), + remotePath(remotePath), state(Inactive), hasError(false) +{ +} + +AbstractSftpOperationWithHandle::~AbstractSftpOperationWithHandle() { } + + +SftpListDir::SftpListDir(SftpJobId jobId, const QString &path) + : AbstractSftpOperationWithHandle(jobId, path) +{ +} + +SftpOutgoingPacket &SftpListDir::initialPacket(SftpOutgoingPacket &packet) +{ + state = OpenRequested; + return packet.generateOpenDir(remotePath, jobId); +} + + +SftpCreateFile::SftpCreateFile(SftpJobId jobId, const QString &path, + SftpOverwriteMode mode) + : AbstractSftpOperationWithHandle(jobId, path), mode(mode) +{ +} + +SftpOutgoingPacket & SftpCreateFile::initialPacket(SftpOutgoingPacket &packet) +{ + state = OpenRequested; + return packet.generateOpenFileForWriting(remotePath, mode, + SftpOutgoingPacket::DefaultPermissions, jobId); +} + + +const int AbstractSftpTransfer::MaxInFlightCount = 10; // Experimentally found to be enough. + +AbstractSftpTransfer::AbstractSftpTransfer(SftpJobId jobId, const QString &remotePath, + const QSharedPointer &localFile) + : AbstractSftpOperationWithHandle(jobId, remotePath), + localFile(localFile), fileSize(0), offset(0), inFlightCount(0), + statRequested(false) +{ +} + +AbstractSftpTransfer::~AbstractSftpTransfer() {} + +void AbstractSftpTransfer::calculateInFlightCount(quint32 chunkSize) +{ + if (fileSize == 0) { + inFlightCount = 1; + } else { + inFlightCount = fileSize / chunkSize; + if (fileSize % chunkSize) + ++inFlightCount; + if (inFlightCount > MaxInFlightCount) + inFlightCount = MaxInFlightCount; + } +} + + +SftpDownload::SftpDownload(SftpJobId jobId, const QString &remotePath, + const QSharedPointer &localFile) + : AbstractSftpTransfer(jobId, remotePath, localFile), eofId(SftpInvalidJob) +{ +} + +SftpOutgoingPacket &SftpDownload::initialPacket(SftpOutgoingPacket &packet) +{ + state = OpenRequested; + return packet.generateOpenFileForReading(remotePath, jobId); +} + + +SftpUploadFile::SftpUploadFile(SftpJobId jobId, const QString &remotePath, + const QSharedPointer &localFile, SftpOverwriteMode mode, + const SftpUploadDir::Ptr &parentJob) + : AbstractSftpTransfer(jobId, remotePath, localFile), + parentJob(parentJob), mode(mode) +{ + fileSize = localFile->size(); +} + +SftpOutgoingPacket &SftpUploadFile::initialPacket(SftpOutgoingPacket &packet) +{ + state = OpenRequested; + quint32 permissions = 0; + const QFile::Permissions &qtPermissions = localFile->permissions(); + if (qtPermissions & QFile::ExeOther) + permissions |= 1 << 0; + if (qtPermissions & QFile::WriteOther) + permissions |= 1 << 1; + if (qtPermissions & QFile::ReadOther) + permissions |= 1 << 2; + if (qtPermissions & QFile::ExeGroup) + permissions |= 1<< 3; + if (qtPermissions & QFile::WriteGroup) + permissions |= 1<< 4; + if (qtPermissions & QFile::ReadGroup) + permissions |= 1<< 5; + if (qtPermissions & QFile::ExeOwner) + permissions |= 1<< 6; + if (qtPermissions & QFile::WriteOwner) + permissions |= 1<< 7; + if (qtPermissions & QFile::ReadOwner) + permissions |= 1<< 8; + return packet.generateOpenFileForWriting(remotePath, mode, permissions, jobId); +} + +SftpUploadDir::~SftpUploadDir() {} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sftpoperation_p.h b/src/libs/ssh/sftpoperation_p.h new file mode 100644 index 0000000000..43baa4fb6f --- /dev/null +++ b/src/libs/ssh/sftpoperation_p.h @@ -0,0 +1,254 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SFTPOPERATION_P_H +#define SFTPOPERATION_P_H + +#include "sftpdefs.h" + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QFile; +QT_END_NAMESPACE + +namespace QSsh { +namespace Internal { + +class SftpOutgoingPacket; + +struct AbstractSftpOperation +{ + typedef QSharedPointer Ptr; + enum Type { + StatFile, ListDir, MakeDir, RmDir, Rm, Rename, CreateLink, CreateFile, Download, UploadFile + }; + + AbstractSftpOperation(SftpJobId jobId); + virtual ~AbstractSftpOperation(); + virtual Type type() const = 0; + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet) = 0; + + const SftpJobId jobId; + +private: + AbstractSftpOperation(const AbstractSftpOperation &); + AbstractSftpOperation &operator=(const AbstractSftpOperation &); +}; + +struct SftpUploadDir; + +struct SftpStatFile : public AbstractSftpOperation +{ + typedef QSharedPointer Ptr; + + SftpStatFile(SftpJobId jobId, const QString &path); + virtual Type type() const { return StatFile; } + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); + + const QString path; +}; + +struct SftpMakeDir : public AbstractSftpOperation +{ + typedef QSharedPointer Ptr; + + SftpMakeDir(SftpJobId jobId, const QString &path, + const QSharedPointer &parentJob = QSharedPointer()); + virtual Type type() const { return MakeDir; } + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); + + const QSharedPointer parentJob; + const QString remoteDir; +}; + +struct SftpRmDir : public AbstractSftpOperation +{ + typedef QSharedPointer Ptr; + + SftpRmDir(SftpJobId jobId, const QString &path); + virtual Type type() const { return RmDir; } + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); + + const QString remoteDir; +}; + +struct SftpRm : public AbstractSftpOperation +{ + typedef QSharedPointer Ptr; + + SftpRm(SftpJobId jobId, const QString &path); + virtual Type type() const { return Rm; } + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); + + const QString remoteFile; +}; + +struct SftpRename : public AbstractSftpOperation +{ + typedef QSharedPointer Ptr; + + SftpRename(SftpJobId jobId, const QString &oldPath, const QString &newPath); + virtual Type type() const { return Rename; } + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); + + const QString oldPath; + const QString newPath; +}; + +struct SftpCreateLink : public AbstractSftpOperation +{ + typedef QSharedPointer Ptr; + + SftpCreateLink(SftpJobId jobId, const QString &filePath, const QString &target); + virtual Type type() const { return CreateLink; } + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); + + const QString filePath; + const QString target; +}; + + +struct AbstractSftpOperationWithHandle : public AbstractSftpOperation +{ + typedef QSharedPointer Ptr; + enum State { Inactive, OpenRequested, Open, CloseRequested }; + + AbstractSftpOperationWithHandle(SftpJobId jobId, const QString &remotePath); + ~AbstractSftpOperationWithHandle(); + + const QString remotePath; + QByteArray remoteHandle; + State state; + bool hasError; +}; + + +struct SftpListDir : public AbstractSftpOperationWithHandle +{ + typedef QSharedPointer Ptr; + + SftpListDir(SftpJobId jobId, const QString &path); + virtual Type type() const { return ListDir; } + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); +}; + + +struct SftpCreateFile : public AbstractSftpOperationWithHandle +{ + typedef QSharedPointer Ptr; + + SftpCreateFile(SftpJobId jobId, const QString &path, SftpOverwriteMode mode); + virtual Type type() const { return CreateFile; } + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); + + const SftpOverwriteMode mode; +}; + +struct AbstractSftpTransfer : public AbstractSftpOperationWithHandle +{ + typedef QSharedPointer Ptr; + + AbstractSftpTransfer(SftpJobId jobId, const QString &remotePath, + const QSharedPointer &localFile); + ~AbstractSftpTransfer(); + void calculateInFlightCount(quint32 chunkSize); + + static const int MaxInFlightCount; + + const QSharedPointer localFile; + quint64 fileSize; + quint64 offset; + int inFlightCount; + bool statRequested; +}; + +struct SftpDownload : public AbstractSftpTransfer +{ + typedef QSharedPointer Ptr; + SftpDownload(SftpJobId jobId, const QString &remotePath, + const QSharedPointer &localFile); + virtual Type type() const { return Download; } + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); + + QMap offsets; + SftpJobId eofId; +}; + +struct SftpUploadFile : public AbstractSftpTransfer +{ + typedef QSharedPointer Ptr; + + SftpUploadFile(SftpJobId jobId, const QString &remotePath, + const QSharedPointer &localFile, SftpOverwriteMode mode, + const QSharedPointer &parentJob = QSharedPointer()); + virtual Type type() const { return UploadFile; } + virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); + + const QSharedPointer parentJob; + SftpOverwriteMode mode; +}; + +// Composite operation. +struct SftpUploadDir +{ + typedef QSharedPointer Ptr; + + struct Dir { + Dir(const QString &l, const QString &r) : localDir(l), remoteDir(r) {} + QString localDir; + QString remoteDir; + }; + + SftpUploadDir(SftpJobId jobId) : jobId(jobId), hasError(false) {} + ~SftpUploadDir(); + + void setError() + { + hasError = true; + uploadsInProgress.clear(); + mkdirsInProgress.clear(); + } + + const SftpJobId jobId; + bool hasError; + QList uploadsInProgress; + QMap mkdirsInProgress; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SFTPOPERATION_P_H diff --git a/src/libs/ssh/sftpoutgoingpacket.cpp b/src/libs/ssh/sftpoutgoingpacket.cpp new file mode 100644 index 0000000000..722cc98006 --- /dev/null +++ b/src/libs/ssh/sftpoutgoingpacket.cpp @@ -0,0 +1,229 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sftpoutgoingpacket_p.h" + +#include "sshpacket_p.h" + +#include + +#include + +namespace QSsh { +namespace Internal { + +namespace { + const quint32 DefaultAttributes = 0; + const quint32 SSH_FXF_READ = 0x00000001; + const quint32 SSH_FXF_WRITE = 0x00000002; + const quint32 SSH_FXF_APPEND = 0x00000004; + const quint32 SSH_FXF_CREAT = 0x00000008; + const quint32 SSH_FXF_TRUNC = 0x00000010; + const quint32 SSH_FXF_EXCL = 0x00000020; +} + +SftpOutgoingPacket::SftpOutgoingPacket() +{ +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateInit(quint32 version) +{ + return init(SSH_FXP_INIT, 0).appendInt(version).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateStat(const QString &path, quint32 requestId) +{ + return init(SSH_FXP_LSTAT, requestId).appendString(path).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateOpenDir(const QString &path, + quint32 requestId) +{ + return init(SSH_FXP_OPENDIR, requestId).appendString(path).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateReadDir(const QByteArray &handle, + quint32 requestId) +{ + return init(SSH_FXP_READDIR, requestId).appendString(handle).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateCloseHandle(const QByteArray &handle, + quint32 requestId) +{ + return init(SSH_FXP_CLOSE, requestId).appendString(handle).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateMkDir(const QString &path, + quint32 requestId) +{ + return init(SSH_FXP_MKDIR, requestId).appendString(path) + .appendInt(DefaultAttributes).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateRmDir(const QString &path, + quint32 requestId) +{ + return init(SSH_FXP_RMDIR, requestId).appendString(path).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateRm(const QString &path, + quint32 requestId) +{ + return init(SSH_FXP_REMOVE, requestId).appendString(path).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateRename(const QString &oldPath, + const QString &newPath, quint32 requestId) +{ + return init(SSH_FXP_RENAME, requestId).appendString(oldPath) + .appendString(newPath).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFileForWriting(const QString &path, + SftpOverwriteMode mode, quint32 permissions, quint32 requestId) +{ + QList attributes; + if (permissions != DefaultPermissions) + attributes << SSH_FILEXFER_ATTR_PERMISSIONS << permissions; + else + attributes << DefaultAttributes; + return generateOpenFile(path, Write, mode, attributes, requestId); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFileForReading(const QString &path, + quint32 requestId) +{ + // Note: Overwrite mode is irrelevant and will be ignored. + return generateOpenFile(path, Read, SftpSkipExisting, QList() << DefaultAttributes, + requestId); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateReadFile(const QByteArray &handle, + quint64 offset, quint32 length, quint32 requestId) +{ + return init(SSH_FXP_READ, requestId).appendString(handle).appendInt64(offset) + .appendInt(length).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateFstat(const QByteArray &handle, + quint32 requestId) +{ + return init(SSH_FXP_FSTAT, requestId).appendString(handle).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateWriteFile(const QByteArray &handle, + quint64 offset, const QByteArray &data, quint32 requestId) +{ + return init(SSH_FXP_WRITE, requestId).appendString(handle) + .appendInt64(offset).appendString(data).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateCreateLink(const QString &filePath, + const QString &target, quint32 requestId) +{ + return init(SSH_FXP_SYMLINK, requestId).appendString(filePath).appendString(target).finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFile(const QString &path, + OpenType openType, SftpOverwriteMode mode, const QList &attributes, quint32 requestId) +{ + quint32 pFlags; + switch (openType) { + case Read: + pFlags = SSH_FXF_READ; + break; + case Write: + pFlags = SSH_FXF_WRITE | SSH_FXF_CREAT; + switch (mode) { + case SftpOverwriteExisting: pFlags |= SSH_FXF_TRUNC; break; + case SftpAppendToExisting: pFlags |= SSH_FXF_APPEND; break; + case SftpSkipExisting: pFlags |= SSH_FXF_EXCL; break; + } + break; + } + + init(SSH_FXP_OPEN, requestId).appendString(path).appendInt(pFlags); + foreach (const quint32 attribute, attributes) + appendInt(attribute); + return finalize(); +} + +SftpOutgoingPacket &SftpOutgoingPacket::init(SftpPacketType type, + quint32 requestId) +{ + m_data.resize(TypeOffset + 1); + m_data[TypeOffset] = type; + if (type != SSH_FXP_INIT) { + appendInt(requestId); +#ifdef CREATOR_SSH_DEBUG + qDebug("Generating SFTP packet of type %d with request id %u", type, + requestId); +#endif + } + return *this; +} + +SftpOutgoingPacket &SftpOutgoingPacket::appendInt(quint32 val) +{ + m_data.append(AbstractSshPacket::encodeInt(val)); + return *this; +} + +SftpOutgoingPacket &SftpOutgoingPacket::appendInt64(quint64 value) +{ + m_data.append(AbstractSshPacket::encodeInt(value)); + return *this; +} + +SftpOutgoingPacket &SftpOutgoingPacket::appendString(const QString &string) +{ + m_data.append(AbstractSshPacket::encodeString(string.toUtf8())); + return *this; +} + +SftpOutgoingPacket &SftpOutgoingPacket::appendString(const QByteArray &string) +{ + m_data += AbstractSshPacket::encodeString(string); + return *this; +} + +SftpOutgoingPacket &SftpOutgoingPacket::finalize() +{ + AbstractSshPacket::setLengthField(m_data); + return *this; +} + +const quint32 SftpOutgoingPacket::DefaultPermissions = std::numeric_limits::max(); + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sftpoutgoingpacket_p.h b/src/libs/ssh/sftpoutgoingpacket_p.h new file mode 100644 index 0000000000..d53f2dcc0e --- /dev/null +++ b/src/libs/ssh/sftpoutgoingpacket_p.h @@ -0,0 +1,94 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SFTPOUTGOINGPACKET_P_H +#define SFTPOUTGOINGPACKET_P_H + +#include "sftppacket_p.h" +#include "sftpdefs.h" + +namespace QSsh { +namespace Internal { + +class SftpOutgoingPacket : public AbstractSftpPacket +{ +public: + SftpOutgoingPacket(); + SftpOutgoingPacket &generateInit(quint32 version); + SftpOutgoingPacket &generateStat(const QString &path, quint32 requestId); + SftpOutgoingPacket &generateOpenDir(const QString &path, quint32 requestId); + SftpOutgoingPacket &generateReadDir(const QByteArray &handle, + quint32 requestId); + SftpOutgoingPacket &generateCloseHandle(const QByteArray &handle, + quint32 requestId); + SftpOutgoingPacket &generateMkDir(const QString &path, quint32 requestId); + SftpOutgoingPacket &generateRmDir(const QString &path, quint32 requestId); + SftpOutgoingPacket &generateRm(const QString &path, quint32 requestId); + SftpOutgoingPacket &generateRename(const QString &oldPath, + const QString &newPath, quint32 requestId); + SftpOutgoingPacket &generateOpenFileForWriting(const QString &path, + SftpOverwriteMode mode, quint32 permissions, quint32 requestId); + SftpOutgoingPacket &generateOpenFileForReading(const QString &path, + quint32 requestId); + SftpOutgoingPacket &generateReadFile(const QByteArray &handle, + quint64 offset, quint32 length, quint32 requestId); + SftpOutgoingPacket &generateFstat(const QByteArray &handle, + quint32 requestId); + SftpOutgoingPacket &generateWriteFile(const QByteArray &handle, + quint64 offset, const QByteArray &data, quint32 requestId); + + // Note: OpenSSH's SFTP server has a bug that reverses the filePath and target + // arguments, so this operation is not portable. + SftpOutgoingPacket &generateCreateLink(const QString &filePath, const QString &target, + quint32 requestId); + + static const quint32 DefaultPermissions; + +private: + static QByteArray encodeString(const QString &string); + + enum OpenType { Read, Write }; + SftpOutgoingPacket &generateOpenFile(const QString &path, OpenType openType, + SftpOverwriteMode mode, const QList &attributes, quint32 requestId); + + SftpOutgoingPacket &init(SftpPacketType type, quint32 requestId); + SftpOutgoingPacket &appendInt(quint32 value); + SftpOutgoingPacket &appendInt64(quint64 value); + SftpOutgoingPacket &appendString(const QString &string); + SftpOutgoingPacket &appendString(const QByteArray &string); + SftpOutgoingPacket &finalize(); +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SFTPOUTGOINGPACKET_P_H diff --git a/src/libs/ssh/sftppacket.cpp b/src/libs/ssh/sftppacket.cpp new file mode 100644 index 0000000000..205f7ae0ca --- /dev/null +++ b/src/libs/ssh/sftppacket.cpp @@ -0,0 +1,56 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sftppacket_p.h" + +#include "sshpacketparser_p.h" + +namespace QSsh { +namespace Internal { + +const quint32 AbstractSftpPacket::MaxDataSize = 32000; +const quint32 AbstractSftpPacket::MaxPacketSize = 34000; +const int AbstractSftpPacket::TypeOffset = 4; +const int AbstractSftpPacket::RequestIdOffset = TypeOffset + 1; +const int AbstractSftpPacket::PayloadOffset = RequestIdOffset + 4; + +AbstractSftpPacket::AbstractSftpPacket() +{ +} + +quint32 AbstractSftpPacket::requestId() const +{ + return SshPacketParser::asUint32(m_data, RequestIdOffset); +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sftppacket_p.h b/src/libs/ssh/sftppacket_p.h new file mode 100644 index 0000000000..2a8345f7b5 --- /dev/null +++ b/src/libs/ssh/sftppacket_p.h @@ -0,0 +1,119 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SFTPPACKET_P_H +#define SFTPPACKET_P_H + +#include +#include +#include + +namespace QSsh { +namespace Internal { + +enum SftpPacketType { + SSH_FXP_INIT = 1, + SSH_FXP_VERSION = 2, + SSH_FXP_OPEN = 3, + SSH_FXP_CLOSE = 4, + SSH_FXP_READ = 5, + SSH_FXP_WRITE = 6, + SSH_FXP_LSTAT = 7, + SSH_FXP_FSTAT = 8, + SSH_FXP_SETSTAT = 9, + SSH_FXP_FSETSTAT = 10, + SSH_FXP_OPENDIR = 11, + SSH_FXP_READDIR = 12, + SSH_FXP_REMOVE = 13, + SSH_FXP_MKDIR = 14, + SSH_FXP_RMDIR = 15, + SSH_FXP_REALPATH = 16, + SSH_FXP_STAT = 17, + SSH_FXP_RENAME = 18, + SSH_FXP_READLINK = 19, + SSH_FXP_SYMLINK = 20, // Removed from later protocol versions. Try not to use. + + SSH_FXP_STATUS = 101, + SSH_FXP_HANDLE = 102, + SSH_FXP_DATA = 103, + SSH_FXP_NAME = 104, + SSH_FXP_ATTRS = 105, + + SSH_FXP_EXTENDED = 200, + SSH_FXP_EXTENDED_REPLY = 201 +}; + +enum SftpStatusCode { + SSH_FX_OK = 0, + SSH_FX_EOF = 1, + SSH_FX_NO_SUCH_FILE = 2, + SSH_FX_PERMISSION_DENIED = 3, + SSH_FX_FAILURE = 4, + SSH_FX_BAD_MESSAGE = 5, + SSH_FX_NO_CONNECTION = 6, + SSH_FX_CONNECTION_LOST = 7, + SSH_FX_OP_UNSUPPORTED = 8 +}; + +enum SftpAttributeType { + SSH_FILEXFER_ATTR_SIZE = 0x00000001, + SSH_FILEXFER_ATTR_UIDGID = 0x00000002, + SSH_FILEXFER_ATTR_PERMISSIONS = 0x00000004, + SSH_FILEXFER_ATTR_ACMODTIME = 0x00000008, + SSH_FILEXFER_ATTR_EXTENDED = 0x80000000 +}; + +class AbstractSftpPacket +{ +public: + AbstractSftpPacket(); + quint32 requestId() const; + const QByteArray &rawData() const { return m_data; } + SftpPacketType type() const { return static_cast(m_data.at(TypeOffset)); } + + static const quint32 MaxDataSize; // "Pure" data size per read/writepacket. + static const quint32 MaxPacketSize; + +protected: + quint32 dataSize() const { return static_cast(m_data.size()); } + + static const int TypeOffset; + static const int RequestIdOffset; + static const int PayloadOffset; + + QByteArray m_data; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SFTPPACKET_P_H diff --git a/src/libs/ssh/ssh.pri b/src/libs/ssh/ssh.pri new file mode 100644 index 0000000000..fe58b81cba --- /dev/null +++ b/src/libs/ssh/ssh.pri @@ -0,0 +1,2 @@ +include(ssh_dependencies.pri) +LIBS *= -l$$qtLibraryName(QtcSsh) diff --git a/src/libs/ssh/ssh.pro b/src/libs/ssh/ssh.pro new file mode 100644 index 0000000000..2df5b0430b --- /dev/null +++ b/src/libs/ssh/ssh.pro @@ -0,0 +1,67 @@ +TEMPLATE = lib +TARGET = QtcSsh +QT += gui network +DEFINES += QSSH_LIBRARY + +include(../../qtcreatorlibrary.pri) +include(ssh_dependencies.pri) + +SOURCES = $$PWD/sshsendfacility.cpp \ + $$PWD/sshremoteprocess.cpp \ + $$PWD/sshpacketparser.cpp \ + $$PWD/sshpacket.cpp \ + $$PWD/sshoutgoingpacket.cpp \ + $$PWD/sshkeygenerator.cpp \ + $$PWD/sshkeyexchange.cpp \ + $$PWD/sshincomingpacket.cpp \ + $$PWD/sshcryptofacility.cpp \ + $$PWD/sshconnection.cpp \ + $$PWD/sshchannelmanager.cpp \ + $$PWD/sshchannel.cpp \ + $$PWD/sshcapabilities.cpp \ + $$PWD/sftppacket.cpp \ + $$PWD/sftpoutgoingpacket.cpp \ + $$PWD/sftpoperation.cpp \ + $$PWD/sftpincomingpacket.cpp \ + $$PWD/sftpdefs.cpp \ + $$PWD/sftpchannel.cpp \ + $$PWD/sshremoteprocessrunner.cpp \ + $$PWD/sshconnectionmanager.cpp \ + $$PWD/sshkeypasswordretriever.cpp \ + $$PWD/sftpfilesystemmodel.cpp \ + $$PWD/sshkeycreationdialog.cpp + +HEADERS = $$PWD/sshsendfacility_p.h \ + $$PWD/sshremoteprocess.h \ + $$PWD/sshremoteprocess_p.h \ + $$PWD/sshpacketparser_p.h \ + $$PWD/sshpacket_p.h \ + $$PWD/sshoutgoingpacket_p.h \ + $$PWD/sshkeygenerator.h \ + $$PWD/sshkeyexchange_p.h \ + $$PWD/sshincomingpacket_p.h \ + $$PWD/sshexception_p.h \ + $$PWD/ssherrors.h \ + $$PWD/sshcryptofacility_p.h \ + $$PWD/sshconnection.h \ + $$PWD/sshconnection_p.h \ + $$PWD/sshchannelmanager_p.h \ + $$PWD/sshchannel_p.h \ + $$PWD/sshcapabilities_p.h \ + $$PWD/sshbotanconversions_p.h \ + $$PWD/sftppacket_p.h \ + $$PWD/sftpoutgoingpacket_p.h \ + $$PWD/sftpoperation_p.h \ + $$PWD/sftpincomingpacket_p.h \ + $$PWD/sftpdefs.h \ + $$PWD/sftpchannel.h \ + $$PWD/sftpchannel_p.h \ + $$PWD/sshremoteprocessrunner.h \ + $$PWD/sshconnectionmanager.h \ + $$PWD/sshpseudoterminal.h \ + $$PWD/sshkeypasswordretriever_p.h \ + $$PWD/sftpfilesystemmodel.h \ + $$PWD/sshkeycreationdialog.h \ + $$PWD/ssh_global.h + +FORMS = $$PWD/sshkeycreationdialog.ui diff --git a/src/libs/ssh/ssh.qbs b/src/libs/ssh/ssh.qbs new file mode 100644 index 0000000000..ff108d4967 --- /dev/null +++ b/src/libs/ssh/ssh.qbs @@ -0,0 +1,51 @@ +import qbs.base 1.0 +import "../QtcLibrary.qbs" as QtcLibrary + +QtcLibrary { + name: "QtcSsh" + + cpp.defines: ["QSSH_LIBRARY"] + cpp.includePaths: [ ".", "..", + "../..", + "../3rdparty/botan/build", + buildDirectory + ] + + Depends { name: "cpp" } + Depends { name: "Qt"; submodules: ['gui', 'network' ] } + Depends { name: "Botan" } + + files: [ + "sftpchannel.h", "sftpchannel_p.h", "sftpchannel.cpp", + "sftpdefs.cpp", "sftpdefs.h", + "sftpincomingpacket.cpp", "sftpincomingpacket_p.h", + "sftpoperation.cpp", "sftpoperation_p.h", + "sftpoutgoingpacket.cpp", "sftpoutgoingpacket_p.h", + "sftppacket.cpp", "sftppacket_p.h", + "sshcapabilities_p.h", "sshcapabilities.cpp", + "sshchannel.cpp", "sshchannel_p.h", + "sshchannelmanager.cpp", "sshchannelmanager_p.h", + "sshconnection.h", "sshconnection_p.h", "sshconnection.cpp", + "sshconnectionmanager.cpp", "sshconnectionmanager.h", + "sshcryptofacility.cpp", "sshcryptofacility_p.h", + "sshkeyexchange.cpp", "sshkeyexchange_p.h", + "sshkeypasswordretriever_p.h", + "sshoutgoingpacket.cpp", "sshoutgoingpacket_p.h", + "sshpacket.cpp", "sshpacket_p.h", + "sshpacketparser.cpp", "sshpacketparser_p.h", + "sshremoteprocess.cpp", "sshremoteprocess.h", "sshremoteprocess_p.h", + "sshremoteprocessrunner.cpp", "sshremoteprocessrunner.h", + "sshsendfacility.cpp", "sshsendfacility_p.h", + "sshkeypasswordretriever.cpp", + "sshkeygenerator.cpp", "sshkeygenerator.h", + "sshkeycreationdialog.cpp", "sshkeycreationdialog.h", "sshkeycreationdialog.ui", + "sshincomingpacket_p.h", "sshincomingpacket.cpp", + "ssherrors.h", + "sshexception_p.h", + "sshpseudoterminal.h", + "sshbotanconversions_p.h" + ] + + ProductModule { Depends { name: "Qt"; submodules: ["widgets", "network"] } } +} + diff --git a/src/libs/ssh/ssh_dependencies.pri b/src/libs/ssh/ssh_dependencies.pri new file mode 100644 index 0000000000..e2a3c96f45 --- /dev/null +++ b/src/libs/ssh/ssh_dependencies.pri @@ -0,0 +1 @@ +include(../3rdparty/botan/botan.pri) diff --git a/src/libs/ssh/ssh_global.h b/src/libs/ssh/ssh_global.h new file mode 100644 index 0000000000..ca89d2dcfd --- /dev/null +++ b/src/libs/ssh/ssh_global.h @@ -0,0 +1,49 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSH_GLOBAL_H +#define SSH_GLOBAL_H + +#include + +#if defined(QSSH_LIBRARY) +# define QSSH_EXPORT Q_DECL_EXPORT +#else +# define QSSH_EXPORT Q_DECL_IMPORT +#endif + +#define QSSH_PRINT_WARNING qWarning("Soft assert at %s:%d", __FILE__, __LINE__) +#define QSSH_ASSERT(cond) do { if (!(cond)) { QSSH_PRINT_WARNING; } } while (false) +#define QSSH_ASSERT_AND_RETURN(cond) do { if (!(cond)) { QSSH_PRINT_WARNING; return; } } while (false) +#define QSSH_ASSERT_AND_RETURN_VALUE(cond, value) do { if (!(cond)) { QSSH_PRINT_WARNING; return value; } } while (false) + +#endif // SSH_GLOBAL_H diff --git a/src/libs/ssh/sshbotanconversions_p.h b/src/libs/ssh/sshbotanconversions_p.h new file mode 100644 index 0000000000..ea52148693 --- /dev/null +++ b/src/libs/ssh/sshbotanconversions_p.h @@ -0,0 +1,102 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef BYTEARRAYCONVERSIONS_P_H +#define BYTEARRAYCONVERSIONS_P_H + +#include "sshcapabilities_p.h" + +#include +#include + +namespace QSsh { +namespace Internal { + +inline const Botan::byte *convertByteArray(const QByteArray &a) +{ + return reinterpret_cast(a.constData()); +} + +inline Botan::byte *convertByteArray(QByteArray &a) +{ + return reinterpret_cast(a.data()); +} + +inline QByteArray convertByteArray(const Botan::SecureVector &v) +{ + return QByteArray(reinterpret_cast(v.begin()), v.size()); +} + +inline const char *botanKeyExchangeAlgoName(const QByteArray &rfcAlgoName) +{ + Q_ASSERT(rfcAlgoName == SshCapabilities::DiffieHellmanGroup1Sha1 + || rfcAlgoName == SshCapabilities::DiffieHellmanGroup14Sha1); + return rfcAlgoName == SshCapabilities::DiffieHellmanGroup1Sha1 + ? "modp/ietf/1024" : "modp/ietf/2048"; +} + +inline const char *botanCryptAlgoName(const QByteArray &rfcAlgoName) +{ + Q_ASSERT(rfcAlgoName == SshCapabilities::CryptAlgo3Des + || rfcAlgoName == SshCapabilities::CryptAlgoAes128); + return rfcAlgoName == SshCapabilities::CryptAlgo3Des + ? "TripleDES" : "AES-128"; +} + +inline const char *botanEmsaAlgoName(const QByteArray &rfcAlgoName) +{ + Q_ASSERT(rfcAlgoName == SshCapabilities::PubKeyDss + || rfcAlgoName == SshCapabilities::PubKeyRsa); + return rfcAlgoName == SshCapabilities::PubKeyDss + ? "EMSA1(SHA-1)" : "EMSA3(SHA-1)"; +} + +inline const char *botanSha1Name() { return "SHA-1"; } + +inline const char *botanHMacAlgoName(const QByteArray &rfcAlgoName) +{ + Q_ASSERT(rfcAlgoName == SshCapabilities::HMacSha1); + Q_UNUSED(rfcAlgoName); + return botanSha1Name(); +} + +inline quint32 botanHMacKeyLen(const QByteArray &rfcAlgoName) +{ + Q_ASSERT(rfcAlgoName == SshCapabilities::HMacSha1); + Q_UNUSED(rfcAlgoName); + return 20; +} + +} // namespace Internal +} // namespace QSsh + +#endif // BYTEARRAYCONVERSIONS_P_H diff --git a/src/libs/ssh/sshcapabilities.cpp b/src/libs/ssh/sshcapabilities.cpp new file mode 100644 index 0000000000..340fedcf4f --- /dev/null +++ b/src/libs/ssh/sshcapabilities.cpp @@ -0,0 +1,106 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshcapabilities_p.h" + +#include "sshexception_p.h" + +#include +#include + +namespace QSsh { +namespace Internal { + +namespace { + QByteArray listAsByteArray(const QList &list) + { + QByteArray array; + foreach(const QByteArray &elem, list) + array += elem + ','; + if (!array.isEmpty()) + array.remove(array.count() - 1, 1); + return array; + } +} // anonymous namspace + +const QByteArray SshCapabilities::DiffieHellmanGroup1Sha1("diffie-hellman-group1-sha1"); +const QByteArray SshCapabilities::DiffieHellmanGroup14Sha1("diffie-hellman-group14-sha1"); +const QList SshCapabilities::KeyExchangeMethods + = QList() << SshCapabilities::DiffieHellmanGroup1Sha1 + << SshCapabilities::DiffieHellmanGroup14Sha1; + +const QByteArray SshCapabilities::PubKeyDss("ssh-dss"); +const QByteArray SshCapabilities::PubKeyRsa("ssh-rsa"); +const QList SshCapabilities::PublicKeyAlgorithms + = QList() << SshCapabilities::PubKeyRsa + << SshCapabilities::PubKeyDss; + +const QByteArray SshCapabilities::CryptAlgo3Des("3des-cbc"); +const QByteArray SshCapabilities::CryptAlgoAes128("aes128-cbc"); +const QList SshCapabilities::EncryptionAlgorithms + = QList() << SshCapabilities::CryptAlgoAes128 + << SshCapabilities::CryptAlgo3Des; + +const QByteArray SshCapabilities::HMacSha1("hmac-sha1"); +const QByteArray SshCapabilities::HMacSha196("hmac-sha1-96"); +const QList SshCapabilities::MacAlgorithms + = QList() /* << SshCapabilities::HMacSha196 */ + << SshCapabilities::HMacSha1; + +const QList SshCapabilities::CompressionAlgorithms + = QList() << "none"; + +const QByteArray SshCapabilities::SshConnectionService("ssh-connection"); + +const QByteArray SshCapabilities::PublicKeyAuthMethod("publickey"); +const QByteArray SshCapabilities::PasswordAuthMethod("password"); + + +QByteArray SshCapabilities::findBestMatch(const QList &myCapabilities, + const QList &serverCapabilities) +{ + foreach (const QByteArray &myCapability, myCapabilities) { + if (serverCapabilities.contains(myCapability)) + return myCapability; + } + + throw SshServerException(SSH_DISCONNECT_KEY_EXCHANGE_FAILED, + "Server and client capabilities do not match.", + QCoreApplication::translate("SshConnection", + "Server and client capabilities don't match. " + "Client list was: %1.\nServer list was %2.") + .arg(QString::fromLocal8Bit(listAsByteArray(myCapabilities).data())) + .arg(QString::fromLocal8Bit(listAsByteArray(serverCapabilities).data()))); +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshcapabilities_p.h b/src/libs/ssh/sshcapabilities_p.h new file mode 100644 index 0000000000..c36163e6a9 --- /dev/null +++ b/src/libs/ssh/sshcapabilities_p.h @@ -0,0 +1,75 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef CAPABILITIES_P_H +#define CAPABILITIES_P_H + +#include +#include + +namespace QSsh { +namespace Internal { + +class SshCapabilities +{ +public: + static const QByteArray DiffieHellmanGroup1Sha1; + static const QByteArray DiffieHellmanGroup14Sha1; + static const QList KeyExchangeMethods; + + static const QByteArray PubKeyDss; + static const QByteArray PubKeyRsa; + static const QList PublicKeyAlgorithms; + + static const QByteArray CryptAlgo3Des; + static const QByteArray CryptAlgoAes128; + static const QList EncryptionAlgorithms; + + static const QByteArray HMacSha1; + static const QByteArray HMacSha196; + static const QList MacAlgorithms; + + static const QList CompressionAlgorithms; + + static const QByteArray SshConnectionService; + + static const QByteArray PublicKeyAuthMethod; + static const QByteArray PasswordAuthMethod; + + static QByteArray findBestMatch(const QList &myCapabilities, + const QList &serverCapabilities); +}; + +} // namespace Internal +} // namespace QSsh + +#endif // CAPABILITIES_P_H diff --git a/src/libs/ssh/sshchannel.cpp b/src/libs/ssh/sshchannel.cpp new file mode 100644 index 0000000000..0d8fbfbaae --- /dev/null +++ b/src/libs/ssh/sshchannel.cpp @@ -0,0 +1,263 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshchannel_p.h" + +#include "sshincomingpacket_p.h" +#include "sshsendfacility_p.h" + +#include + +#include + +namespace QSsh { +namespace Internal { + +namespace { + const quint32 MinMaxPacketSize = 32768; + const quint32 MaxPacketSize = 16 * 1024 * 1024; + const quint32 InitialWindowSize = MaxPacketSize; + const quint32 NoChannel = 0xffffffffu; +} // anonymous namespace + +AbstractSshChannel::AbstractSshChannel(quint32 channelId, + SshSendFacility &sendFacility) + : m_sendFacility(sendFacility), m_timeoutTimer(new QTimer(this)), + m_localChannel(channelId), m_remoteChannel(NoChannel), + m_localWindowSize(InitialWindowSize), m_remoteWindowSize(0), + m_state(Inactive) +{ + m_timeoutTimer->setSingleShot(true); + connect(m_timeoutTimer, SIGNAL(timeout()), this, SIGNAL(timeout())); +} + +AbstractSshChannel::~AbstractSshChannel() +{ + +} + +void AbstractSshChannel::setChannelState(ChannelState state) +{ + m_state = state; + if (state == Closed) + closeHook(); +} + +void AbstractSshChannel::requestSessionStart() +{ + // Note: We are just being paranoid here about the Botan exceptions, + // which are extremely unlikely to happen, because if there was a problem + // with our cryptography stuff, it would have hit us before, on + // establishing the connection. + try { + m_sendFacility.sendSessionPacket(m_localChannel, InitialWindowSize, + MaxPacketSize); + setChannelState(SessionRequested); + m_timeoutTimer->start(ReplyTimeout); + } catch (Botan::Exception &e) { + qDebug("Botan error: %s", e.what()); + closeChannel(); + } +} + +void AbstractSshChannel::sendData(const QByteArray &data) +{ + try { + m_sendBuffer += data; + flushSendBuffer(); + } catch (Botan::Exception &e) { + qDebug("Botan error: %s", e.what()); + closeChannel(); + } +} + +void AbstractSshChannel::handleWindowAdjust(quint32 bytesToAdd) +{ + checkChannelActive(); + + const quint64 newValue = m_remoteWindowSize + bytesToAdd; + if (newValue > 0xffffffffu) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Illegal window size requested."); + } + + m_remoteWindowSize = newValue; + flushSendBuffer(); +} + +void AbstractSshChannel::flushSendBuffer() +{ + const quint32 bytesToSend = qMin(m_remoteMaxPacketSize, + qMin(m_remoteWindowSize, m_sendBuffer.size())); + if (bytesToSend > 0) { + const QByteArray &data = m_sendBuffer.left(bytesToSend); + m_sendFacility.sendChannelDataPacket(m_remoteChannel, data); + m_sendBuffer.remove(0, bytesToSend); + m_remoteWindowSize -= bytesToSend; + } +} + +void AbstractSshChannel::handleOpenSuccess(quint32 remoteChannelId, + quint32 remoteWindowSize, quint32 remoteMaxPacketSize) +{ + if (m_state != SessionRequested) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_MSG_CHANNEL_OPEN_CONFIRMATION packet."); + } + m_timeoutTimer->stop(); + + if (remoteMaxPacketSize < MinMaxPacketSize) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Maximum packet size too low."); + } + +#ifdef CREATOR_SSH_DEBUG + qDebug("Channel opened. remote channel id: %u, remote window size: %u, " + "remote max packet size: %u", + remoteChannelId, remoteWindowSize, remoteMaxPacketSize); +#endif + m_remoteChannel = remoteChannelId; + m_remoteWindowSize = remoteWindowSize; + m_remoteMaxPacketSize = remoteMaxPacketSize - sizeof(quint32) - sizeof m_remoteChannel - 1; + // Original value includes packet type, channel number and length field for string. + setChannelState(SessionEstablished); + handleOpenSuccessInternal(); +} + +void AbstractSshChannel::handleOpenFailure(const QString &reason) +{ + if (m_state != SessionRequested) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_MSG_CHANNEL_OPEN_FAILURE packet."); + } + m_timeoutTimer->stop(); + +#ifdef CREATOR_SSH_DEBUG + qDebug("Channel open request failed for channel %u", m_localChannel); +#endif + handleOpenFailureInternal(reason); +} + +void AbstractSshChannel::handleChannelEof() +{ + if (m_state == Inactive || m_state == Closed) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_MSG_CHANNEL_EOF message."); + } + m_localWindowSize = 0; +} + +void AbstractSshChannel::handleChannelClose() +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("Receiving CLOSE for channel %u", m_localChannel); +#endif + if (channelState() == Inactive || channelState() == Closed) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_MSG_CHANNEL_CLOSE message."); + } + closeChannel(); + setChannelState(Closed); +} + +void AbstractSshChannel::handleChannelData(const QByteArray &data) +{ + const int bytesToDeliver = handleChannelOrExtendedChannelData(data); + handleChannelDataInternal(bytesToDeliver == data.size() + ? data : data.left(bytesToDeliver)); +} + +void AbstractSshChannel::handleChannelExtendedData(quint32 type, const QByteArray &data) +{ + const int bytesToDeliver = handleChannelOrExtendedChannelData(data); + handleChannelExtendedDataInternal(type, bytesToDeliver == data.size() + ? data : data.left(bytesToDeliver)); +} + +void AbstractSshChannel::handleChannelRequest(const SshIncomingPacket &packet) +{ + checkChannelActive(); + const QByteArray &requestType = packet.extractChannelRequestType(); + if (requestType == SshIncomingPacket::ExitStatusType) + handleExitStatus(packet.extractChannelExitStatus()); + else if (requestType == SshIncomingPacket::ExitSignalType) + handleExitSignal(packet.extractChannelExitSignal()); + else if (requestType != "eow@openssh.com") // Suppress warning for this one, as it's sent all the time. + qWarning("Ignoring unknown request type '%s'", requestType.data()); +} + +int AbstractSshChannel::handleChannelOrExtendedChannelData(const QByteArray &data) +{ + checkChannelActive(); + + const int bytesToDeliver = qMin(data.size(), maxDataSize()); + if (bytesToDeliver != data.size()) + qWarning("Misbehaving server does not respect local window, clipping."); + + m_localWindowSize -= bytesToDeliver; + if (m_localWindowSize < MaxPacketSize) { + m_localWindowSize += MaxPacketSize; + m_sendFacility.sendWindowAdjustPacket(m_remoteChannel, + MaxPacketSize); + } + return bytesToDeliver; +} + +void AbstractSshChannel::closeChannel() +{ + if (m_state == CloseRequested) { + m_timeoutTimer->stop(); + } else if (m_state != Closed) { + if (m_state == Inactive) { + setChannelState(Closed); + } else { + setChannelState(CloseRequested); + m_sendFacility.sendChannelEofPacket(m_remoteChannel); + m_sendFacility.sendChannelClosePacket(m_remoteChannel); + } + } +} + +void AbstractSshChannel::checkChannelActive() +{ + if (channelState() == Inactive || channelState() == Closed) + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Channel not open."); +} + +quint32 AbstractSshChannel::maxDataSize() const +{ + return qMin(m_localWindowSize, MaxPacketSize); +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshchannel_p.h b/src/libs/ssh/sshchannel_p.h new file mode 100644 index 0000000000..70e55d9c8f --- /dev/null +++ b/src/libs/ssh/sshchannel_p.h @@ -0,0 +1,124 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHCHANNEL_P_H +#define SSHCHANNEL_P_H + +#include +#include +#include + +QT_FORWARD_DECLARE_CLASS(QTimer) + +namespace QSsh { +namespace Internal { + +struct SshChannelExitSignal; +struct SshChannelExitStatus; +class SshIncomingPacket; +class SshSendFacility; + +class AbstractSshChannel : public QObject +{ + Q_OBJECT +public: + enum ChannelState { + Inactive, SessionRequested, SessionEstablished, CloseRequested, Closed + }; + + ChannelState channelState() const { return m_state; } + void setChannelState(ChannelState state); + + quint32 localChannelId() const { return m_localChannel; } + quint32 remoteChannel() const { return m_remoteChannel; } + + virtual void handleChannelSuccess() = 0; + virtual void handleChannelFailure() = 0; + + virtual void closeHook() = 0; + + void handleOpenSuccess(quint32 remoteChannelId, quint32 remoteWindowSize, + quint32 remoteMaxPacketSize); + void handleOpenFailure(const QString &reason); + void handleWindowAdjust(quint32 bytesToAdd); + void handleChannelEof(); + void handleChannelClose(); + void handleChannelData(const QByteArray &data); + void handleChannelExtendedData(quint32 type, const QByteArray &data); + void handleChannelRequest(const SshIncomingPacket &packet); + + void requestSessionStart(); + void sendData(const QByteArray &data); + void closeChannel(); + + virtual ~AbstractSshChannel(); + + static const int ReplyTimeout = 10000; // milli seconds + +signals: + void timeout(); + +protected: + AbstractSshChannel(quint32 channelId, SshSendFacility &sendFacility); + + quint32 maxDataSize() const; + void checkChannelActive(); + + SshSendFacility &m_sendFacility; + QTimer * const m_timeoutTimer; + +private: + virtual void handleOpenSuccessInternal() = 0; + virtual void handleOpenFailureInternal(const QString &reason) = 0; + virtual void handleChannelDataInternal(const QByteArray &data) = 0; + virtual void handleChannelExtendedDataInternal(quint32 type, + const QByteArray &data) = 0; + virtual void handleExitStatus(const SshChannelExitStatus &exitStatus) = 0; + virtual void handleExitSignal(const SshChannelExitSignal &signal) = 0; + + void setState(ChannelState newState); + void flushSendBuffer(); + int handleChannelOrExtendedChannelData(const QByteArray &data); + + const quint32 m_localChannel; + quint32 m_remoteChannel; + quint32 m_localWindowSize; + quint32 m_remoteWindowSize; + quint32 m_remoteMaxPacketSize; + ChannelState m_state; + QByteArray m_sendBuffer; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHCHANNEL_P_H diff --git a/src/libs/ssh/sshchannelmanager.cpp b/src/libs/ssh/sshchannelmanager.cpp new file mode 100644 index 0000000000..409f2a586f --- /dev/null +++ b/src/libs/ssh/sshchannelmanager.cpp @@ -0,0 +1,198 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshchannelmanager_p.h" + +#include "sftpchannel.h" +#include "sftpchannel_p.h" +#include "sshincomingpacket_p.h" +#include "sshremoteprocess.h" +#include "sshremoteprocess_p.h" +#include "sshsendfacility_p.h" + +#include + +namespace QSsh { +namespace Internal { + +SshChannelManager::SshChannelManager(SshSendFacility &sendFacility, + QObject *parent) + : QObject(parent), m_sendFacility(sendFacility), m_nextLocalChannelId(0) +{ +} + +void SshChannelManager::handleChannelRequest(const SshIncomingPacket &packet) +{ + lookupChannel(packet.extractRecipientChannel()) + ->handleChannelRequest(packet); +} + +void SshChannelManager::handleChannelOpen(const SshIncomingPacket &) +{ + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Server tried to open channel on client."); +} + +void SshChannelManager::handleChannelOpenFailure(const SshIncomingPacket &packet) +{ + const SshChannelOpenFailure &failure = packet.extractChannelOpenFailure(); + ChannelIterator it = lookupChannelAsIterator(failure.localChannel); + try { + it.value()->handleOpenFailure(failure.reasonString); + } catch (SshServerException &e) { + 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); + } +} + +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.", + tr("Invalid channel id %1").arg(channelId)); + } + return it; +} + +AbstractSshChannel *SshChannelManager::lookupChannel(quint32 channelId, + bool allowNotFound) +{ + ChannelIterator it = lookupChannelAsIterator(channelId, allowNotFound); + return it == m_channels.end() ? 0 : it.value(); +} + +QSsh::SshRemoteProcess::Ptr SshChannelManager::createRemoteProcess(const QByteArray &command) +{ + SshRemoteProcess::Ptr proc(new SshRemoteProcess(command, m_nextLocalChannelId++, m_sendFacility)); + insertChannel(proc->d, proc); + return proc; +} + +QSsh::SshRemoteProcess::Ptr SshChannelManager::createRemoteShell() +{ + SshRemoteProcess::Ptr proc(new SshRemoteProcess(m_nextLocalChannelId++, m_sendFacility)); + insertChannel(proc->d, proc); + return proc; +} + +QSsh::SftpChannel::Ptr SshChannelManager::createSftpChannel() +{ + SftpChannel::Ptr sftp(new SftpChannel(m_nextLocalChannelId++, m_sendFacility)); + insertChannel(sftp->d, sftp); + return sftp; +} + +void SshChannelManager::insertChannel(AbstractSshChannel *priv, + const QSharedPointer &pub) +{ + connect(priv, SIGNAL(timeout()), this, SIGNAL(timeout())); + m_channels.insert(priv->localChannelId(), priv); + m_sessions.insert(priv, pub); +} + +void SshChannelManager::closeAllChannels() +{ + for (ChannelIterator it = m_channels.begin(); it != m_channels.end(); ++it) + it.value()->closeChannel(); + m_channels.clear(); + m_sessions.clear(); +} + +void SshChannelManager::removeChannel(ChannelIterator it) +{ + Q_ASSERT(it != m_channels.end() && "Unexpected channel lookup failure."); + const int removeCount = m_sessions.remove(it.value()); + Q_ASSERT(removeCount == 1 && "Session for channel not found."); + m_channels.erase(it); +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshchannelmanager_p.h b/src/libs/ssh/sshchannelmanager_p.h new file mode 100644 index 0000000000..9cc88e9dd6 --- /dev/null +++ b/src/libs/ssh/sshchannelmanager_p.h @@ -0,0 +1,97 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHCHANNELLAYER_P_H +#define SSHCHANNELLAYER_P_H + +#include +#include +#include + +namespace QSsh { + +class SftpChannel; +class SshRemoteProcess; + +namespace Internal { + +class AbstractSshChannel; +class SshIncomingPacket; +class SshSendFacility; + +class SshChannelManager : public QObject +{ + Q_OBJECT +public: + SshChannelManager(SshSendFacility &sendFacility, QObject *parent); + + QSharedPointer createRemoteProcess(const QByteArray &command); + QSharedPointer createRemoteShell(); + QSharedPointer createSftpChannel(); + void closeAllChannels(); + + void handleChannelRequest(const SshIncomingPacket &packet); + void handleChannelOpen(const SshIncomingPacket &packet); + void handleChannelOpenFailure(const SshIncomingPacket &packet); + void handleChannelOpenConfirmation(const SshIncomingPacket &packet); + void handleChannelSuccess(const SshIncomingPacket &packet); + void handleChannelFailure(const SshIncomingPacket &packet); + void handleChannelWindowAdjust(const SshIncomingPacket &packet); + void handleChannelData(const SshIncomingPacket &packet); + void handleChannelExtendedData(const SshIncomingPacket &packet); + void handleChannelEof(const SshIncomingPacket &packet); + void handleChannelClose(const SshIncomingPacket &packet); + +signals: + void timeout(); + +private: + typedef QHash::Iterator ChannelIterator; + + ChannelIterator lookupChannelAsIterator(quint32 channelId, + bool allowNotFound = false); + AbstractSshChannel *lookupChannel(quint32 channelId, + bool allowNotFound = false); + void removeChannel(ChannelIterator it); + void insertChannel(AbstractSshChannel *priv, + const QSharedPointer &pub); + + SshSendFacility &m_sendFacility; + QHash m_channels; + QHash > m_sessions; + quint32 m_nextLocalChannelId; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHCHANNELLAYER_P_H diff --git a/src/libs/ssh/sshconnection.cpp b/src/libs/ssh/sshconnection.cpp new file mode 100644 index 0000000000..a9ee712ec5 --- /dev/null +++ b/src/libs/ssh/sshconnection.cpp @@ -0,0 +1,728 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshconnection.h" +#include "sshconnection_p.h" + +#include "sftpchannel.h" +#include "sshcapabilities_p.h" +#include "sshchannelmanager_p.h" +#include "sshcryptofacility_p.h" +#include "sshexception_p.h" +#include "sshkeyexchange_p.h" + +#include +#include + +#include +#include +#include +#include +#include + +/*! + \class QSsh::SshConnection + + \brief This class provides an SSH connection, implementing protocol version 2.0 + + It can spawn channels for remote execution and SFTP operations (version 3). + It operates asynchronously (non-blocking) and is not thread-safe. +*/ + +namespace QSsh { + +namespace { + const QByteArray ClientId("SSH-2.0-QtCreator\r\n"); + + bool staticInitializationsDone = false; + QMutex staticInitMutex; + + void doStaticInitializationsIfNecessary() + { + QMutexLocker locker(&staticInitMutex); + if (!staticInitializationsDone) { + Botan::LibraryInitializer::initialize("thread_safe=true"); + qRegisterMetaType("QSsh::SshError"); + qRegisterMetaType("QSsh::SftpJobId"); + qRegisterMetaType("QSsh::SftpFileInfo"); + qRegisterMetaType >("QList"); + staticInitializationsDone = true; + } + } +} // anonymous namespace + + +SshConnectionParameters::SshConnectionParameters() : + timeout(0), authenticationType(AuthenticationByKey), port(0), proxyType(NoProxy) +{ +} + +static inline bool equals(const SshConnectionParameters &p1, const SshConnectionParameters &p2) +{ + return p1.host == p2.host && p1.userName == p2.userName + && p1.authenticationType == p2.authenticationType + && (p1.authenticationType == SshConnectionParameters::AuthenticationByPassword ? + p1.password == p2.password : p1.privateKeyFile == p2.privateKeyFile) + && p1.timeout == p2.timeout && p1.port == p2.port; +} + +bool operator==(const SshConnectionParameters &p1, const SshConnectionParameters &p2) +{ + return equals(p1, p2); +} + +bool operator!=(const SshConnectionParameters &p1, const SshConnectionParameters &p2) +{ + return !equals(p1, p2); +} + +// TODO: Mechanism for checking the host key. First connection to host: save, later: compare + +SshConnection::Ptr SshConnection::create(const SshConnectionParameters &serverInfo) +{ + doStaticInitializationsIfNecessary(); + return Ptr(new SshConnection(serverInfo)); +} + +SshConnection::SshConnection(const SshConnectionParameters &serverInfo) + : d(new Internal::SshConnectionPrivate(this, serverInfo)) +{ + connect(d, SIGNAL(connected()), this, SIGNAL(connected()), + Qt::QueuedConnection); + connect(d, SIGNAL(dataAvailable(QString)), this, + SIGNAL(dataAvailable(QString)), Qt::QueuedConnection); + connect(d, SIGNAL(disconnected()), this, SIGNAL(disconnected()), + Qt::QueuedConnection); + connect(d, SIGNAL(error(QSsh::SshError)), this, + SIGNAL(error(QSsh::SshError)), Qt::QueuedConnection); +} + +void SshConnection::connectToHost() +{ + d->connectToHost(); +} + +void SshConnection::disconnectFromHost() +{ + d->closeConnection(Internal::SSH_DISCONNECT_BY_APPLICATION, SshNoError, "", + QString()); +} + +SshConnection::State SshConnection::state() const +{ + switch (d->state()) { + case Internal::SocketUnconnected: + return Unconnected; + case Internal::ConnectionEstablished: + return Connected; + default: + return Connecting; + } +} + +SshError SshConnection::errorState() const +{ + return d->error(); +} + +QString SshConnection::errorString() const +{ + return d->errorString(); +} + +SshConnectionParameters SshConnection::connectionParameters() const +{ + return d->m_connParams; +} + +SshConnectionInfo SshConnection::connectionInfo() const +{ + QSSH_ASSERT_AND_RETURN_VALUE(state() == Connected, SshConnectionInfo()); + + return SshConnectionInfo(d->m_socket->localAddress(), d->m_socket->localPort(), + d->m_socket->peerAddress(), d->m_socket->peerPort()); +} + +SshConnection::~SshConnection() +{ + disconnect(); + disconnectFromHost(); + delete d; +} + +QSharedPointer SshConnection::createRemoteProcess(const QByteArray &command) +{ + QSSH_ASSERT_AND_RETURN_VALUE(state() == Connected, QSharedPointer()); + return d->createRemoteProcess(command); +} + +QSharedPointer SshConnection::createRemoteShell() +{ + QSSH_ASSERT_AND_RETURN_VALUE(state() == Connected, QSharedPointer()); + return d->createRemoteShell(); +} + +QSharedPointer SshConnection::createSftpChannel() +{ + QSSH_ASSERT_AND_RETURN_VALUE(state() == Connected, QSharedPointer()); + return d->createSftpChannel(); +} + + +namespace Internal { + +SshConnectionPrivate::SshConnectionPrivate(SshConnection *conn, + const SshConnectionParameters &serverInfo) + : m_socket(new QTcpSocket(this)), m_state(SocketUnconnected), + m_sendFacility(m_socket), + m_channelManager(new SshChannelManager(m_sendFacility, this)), + m_connParams(serverInfo), m_error(SshNoError), m_ignoreNextPacket(false), + m_conn(conn) +{ + setupPacketHandlers(); + m_socket->setProxy(m_connParams.proxyType == SshConnectionParameters::DefaultProxy + ? QNetworkProxy::DefaultProxy : QNetworkProxy::NoProxy); + m_timeoutTimer.setSingleShot(true); + m_timeoutTimer.setInterval(m_connParams.timeout * 1000); + m_keepAliveTimer.setSingleShot(true); + m_keepAliveTimer.setInterval(10000); + connect(m_channelManager, SIGNAL(timeout()), this, SLOT(handleTimeout())); +} + +SshConnectionPrivate::~SshConnectionPrivate() +{ + disconnect(); +} + +void SshConnectionPrivate::setupPacketHandlers() +{ + typedef SshConnectionPrivate This; + + setupPacketHandler(SSH_MSG_KEXINIT, StateList() << SocketConnected + << ConnectionEstablished, &This::handleKeyExchangeInitPacket); + setupPacketHandler(SSH_MSG_KEXDH_REPLY, StateList() << SocketConnected + << ConnectionEstablished, &This::handleKeyExchangeReplyPacket); + + setupPacketHandler(SSH_MSG_NEWKEYS, StateList() << SocketConnected + << ConnectionEstablished, &This::handleNewKeysPacket); + setupPacketHandler(SSH_MSG_SERVICE_ACCEPT, + StateList() << UserAuthServiceRequested, + &This::handleServiceAcceptPacket); + setupPacketHandler(SSH_MSG_USERAUTH_PASSWD_CHANGEREQ, + StateList() << UserAuthRequested, &This::handlePasswordExpiredPacket); + setupPacketHandler(SSH_MSG_GLOBAL_REQUEST, + StateList() << ConnectionEstablished, &This::handleGlobalRequest); + + const StateList authReqList = StateList() << UserAuthRequested; + setupPacketHandler(SSH_MSG_USERAUTH_BANNER, authReqList, + &This::handleUserAuthBannerPacket); + setupPacketHandler(SSH_MSG_USERAUTH_SUCCESS, authReqList, + &This::handleUserAuthSuccessPacket); + setupPacketHandler(SSH_MSG_USERAUTH_FAILURE, authReqList, + &This::handleUserAuthFailurePacket); + + const StateList connectedList + = StateList() << ConnectionEstablished; + setupPacketHandler(SSH_MSG_CHANNEL_REQUEST, connectedList, + &This::handleChannelRequest); + setupPacketHandler(SSH_MSG_CHANNEL_OPEN, connectedList, + &This::handleChannelOpen); + setupPacketHandler(SSH_MSG_CHANNEL_OPEN_FAILURE, connectedList, + &This::handleChannelOpenFailure); + setupPacketHandler(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, connectedList, + &This::handleChannelOpenConfirmation); + setupPacketHandler(SSH_MSG_CHANNEL_SUCCESS, connectedList, + &This::handleChannelSuccess); + setupPacketHandler(SSH_MSG_CHANNEL_FAILURE, connectedList, + &This::handleChannelFailure); + setupPacketHandler(SSH_MSG_CHANNEL_WINDOW_ADJUST, connectedList, + &This::handleChannelWindowAdjust); + setupPacketHandler(SSH_MSG_CHANNEL_DATA, connectedList, + &This::handleChannelData); + setupPacketHandler(SSH_MSG_CHANNEL_EXTENDED_DATA, connectedList, + &This::handleChannelExtendedData); + + const StateList connectedOrClosedList + = StateList() << SocketUnconnected << ConnectionEstablished; + setupPacketHandler(SSH_MSG_CHANNEL_EOF, connectedOrClosedList, + &This::handleChannelEof); + setupPacketHandler(SSH_MSG_CHANNEL_CLOSE, connectedOrClosedList, + &This::handleChannelClose); + + setupPacketHandler(SSH_MSG_DISCONNECT, StateList() << SocketConnected + << UserAuthServiceRequested << UserAuthRequested + << ConnectionEstablished, &This::handleDisconnect); + + setupPacketHandler(SSH_MSG_UNIMPLEMENTED, + StateList() << ConnectionEstablished, &This::handleUnimplementedPacket); +} + +void SshConnectionPrivate::setupPacketHandler(SshPacketType type, + const SshConnectionPrivate::StateList &states, + SshConnectionPrivate::PacketHandler handler) +{ + m_packetHandlers.insert(type, HandlerInStates(states, handler)); +} + +void SshConnectionPrivate::handleSocketConnected() +{ + m_state = SocketConnected; + sendData(ClientId); +} + +void SshConnectionPrivate::handleIncomingData() +{ + if (m_state == SocketUnconnected) + return; // For stuff queued in the event loop after we've called closeConnection(); + + try { + if (!canUseSocket()) + return; + m_incomingData += m_socket->readAll(); +#ifdef CREATOR_SSH_DEBUG + qDebug("state = %d, remote data size = %d", m_state, + m_incomingData.count()); +#endif + if (m_state == SocketConnected) + handleServerId(); + handlePackets(); + } catch (SshServerException &e) { + closeConnection(e.error, SshProtocolError, e.errorStringServer, + tr("SSH Protocol error: %1").arg(e.errorStringUser)); + } catch (SshClientException &e) { + closeConnection(SSH_DISCONNECT_BY_APPLICATION, e.error, "", + e.errorString); + } catch (Botan::Exception &e) { + closeConnection(SSH_DISCONNECT_BY_APPLICATION, SshInternalError, "", + tr("Botan library exception: %1").arg(QString::fromAscii(e.what()))); + } +} + +void SshConnectionPrivate::handleServerId() +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("%s: incoming data size = %d, incoming data = '%s'", + Q_FUNC_INFO, m_incomingData.count(), m_incomingData.data()); +#endif + const int idOffset = m_incomingData.indexOf("SSH-"); + if (idOffset == -1) + return; + m_incomingData.remove(0, idOffset); + if (m_incomingData.size() < 7) + return; + const QByteArray &version = m_incomingData.mid(4, 3); + if (version != "2.0") { + throw SshServerException(SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED, + "Invalid protocol version.", + tr("Invalid protocol version: Expected '2.0', got '%1'.") + .arg(SshPacketParser::asUserString(version))); + } + const int endOffset = m_incomingData.indexOf("\r\n"); + if (endOffset == -1) + return; + if (m_incomingData.at(7) != '-') { + throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid server id.", tr("Invalid server id '%1'.") + .arg(SshPacketParser::asUserString(m_incomingData))); + } + + m_keyExchange.reset(new SshKeyExchange(m_sendFacility)); + m_serverId = m_incomingData.left(endOffset); + m_keyExchange->sendKexInitPacket(m_serverId); + m_keyExchangeState = KexInitSent; + m_incomingData.remove(0, endOffset + 2); +} + +void SshConnectionPrivate::handlePackets() +{ + m_incomingPacket.consumeData(m_incomingData); + while (m_incomingPacket.isComplete()) { + handleCurrentPacket(); + m_incomingPacket.clear(); + m_incomingPacket.consumeData(m_incomingData); + } +} + +void SshConnectionPrivate::handleCurrentPacket() +{ + Q_ASSERT(m_incomingPacket.isComplete()); + Q_ASSERT(m_keyExchangeState == DhInitSent || !m_ignoreNextPacket); + + if (m_ignoreNextPacket) { + m_ignoreNextPacket = false; + return; + } + + QHash::ConstIterator it + = m_packetHandlers.find(m_incomingPacket.type()); + if (it == m_packetHandlers.end()) { + m_sendFacility.sendMsgUnimplementedPacket(m_incomingPacket.serverSeqNr()); + return; + } + if (!it.value().first.contains(m_state)) { + throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected packet.", tr("Unexpected packet of type %1.") + .arg(m_incomingPacket.type())); + } + (this->*it.value().second)(); +} + +void SshConnectionPrivate::handleKeyExchangeInitPacket() +{ + if (m_keyExchangeState != NoKeyExchange + && m_keyExchangeState != KexInitSent) { + throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected packet.", tr("Unexpected packet of type %1.") + .arg(m_incomingPacket.type())); + } + + // Server-initiated re-exchange. + if (m_keyExchangeState == NoKeyExchange) { + m_keyExchange.reset(new SshKeyExchange(m_sendFacility)); + m_keyExchange->sendKexInitPacket(m_serverId); + } + + // If the server sends a guessed packet, the guess must be wrong, + // because the algorithms we support require us to initiate the + // key exchange. + if (m_keyExchange->sendDhInitPacket(m_incomingPacket)) { + m_ignoreNextPacket = true; + } + + m_keyExchangeState = DhInitSent; +} + +void SshConnectionPrivate::handleKeyExchangeReplyPacket() +{ + if (m_keyExchangeState != DhInitSent) { + throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected packet.", tr("Unexpected packet of type %1.") + .arg(m_incomingPacket.type())); + } + + m_keyExchange->sendNewKeysPacket(m_incomingPacket, + ClientId.left(ClientId.size() - 2)); + m_sendFacility.recreateKeys(*m_keyExchange); + m_keyExchangeState = NewKeysSent; +} + +void SshConnectionPrivate::handleNewKeysPacket() +{ + if (m_keyExchangeState != NewKeysSent) { + throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected packet.", tr("Unexpected packet of type %1.") + .arg(m_incomingPacket.type())); + } + + m_incomingPacket.recreateKeys(*m_keyExchange); + m_keyExchange.reset(); + m_keyExchangeState = NoKeyExchange; + + if (m_state == SocketConnected) { + m_sendFacility.sendUserAuthServiceRequestPacket(); + m_state = UserAuthServiceRequested; + } +} + +void SshConnectionPrivate::handleServiceAcceptPacket() +{ + if (m_connParams.authenticationType == SshConnectionParameters::AuthenticationByPassword) { + m_sendFacility.sendUserAuthByPwdRequestPacket(m_connParams.userName.toUtf8(), + SshCapabilities::SshConnectionService, m_connParams.password.toUtf8()); + } else { + m_sendFacility.sendUserAuthByKeyRequestPacket(m_connParams.userName.toUtf8(), + SshCapabilities::SshConnectionService); + } + m_state = UserAuthRequested; +} + +void SshConnectionPrivate::handlePasswordExpiredPacket() +{ + if (m_connParams.authenticationType == SshConnectionParameters::AuthenticationByKey) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Got SSH_MSG_USERAUTH_PASSWD_CHANGEREQ, but did not use password."); + } + + throw SshClientException(SshAuthenticationError, tr("Password expired.")); +} + +void SshConnectionPrivate::handleUserAuthBannerPacket() +{ + emit dataAvailable(m_incomingPacket.extractUserAuthBanner().message); +} + +void SshConnectionPrivate::handleGlobalRequest() +{ + m_sendFacility.sendRequestFailurePacket(); +} + +void SshConnectionPrivate::handleUserAuthSuccessPacket() +{ + m_state = ConnectionEstablished; + m_timeoutTimer.stop(); + emit connected(); + m_lastInvalidMsgSeqNr = InvalidSeqNr; + connect(&m_keepAliveTimer, SIGNAL(timeout()), SLOT(sendKeepAlivePacket())); + m_keepAliveTimer.start(); +} + +void SshConnectionPrivate::handleUserAuthFailurePacket() +{ + m_timeoutTimer.stop(); + const QString errorMsg = m_connParams.authenticationType == SshConnectionParameters::AuthenticationByPassword + ? tr("Server rejected password.") : tr("Server rejected key."); + throw SshClientException(SshAuthenticationError, errorMsg); +} +void SshConnectionPrivate::handleDebugPacket() +{ + const SshDebug &msg = m_incomingPacket.extractDebug(); + if (msg.display) + emit dataAvailable(msg.message); +} + +void SshConnectionPrivate::handleUnimplementedPacket() +{ + const SshUnimplemented &msg = m_incomingPacket.extractUnimplemented(); + if (msg.invalidMsgSeqNr != m_lastInvalidMsgSeqNr) { + throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected packet", tr("The server sent an unexpected SSH packet " + "of type SSH_MSG_UNIMPLEMENTED.")); + } + m_lastInvalidMsgSeqNr = InvalidSeqNr; + m_timeoutTimer.stop(); + m_keepAliveTimer.start(); +} + +void SshConnectionPrivate::handleChannelRequest() +{ + m_channelManager->handleChannelRequest(m_incomingPacket); +} + +void SshConnectionPrivate::handleChannelOpen() +{ + m_channelManager->handleChannelOpen(m_incomingPacket); +} + +void SshConnectionPrivate::handleChannelOpenFailure() +{ + m_channelManager->handleChannelOpenFailure(m_incomingPacket); +} + +void SshConnectionPrivate::handleChannelOpenConfirmation() +{ + m_channelManager->handleChannelOpenConfirmation(m_incomingPacket); +} + +void SshConnectionPrivate::handleChannelSuccess() +{ + m_channelManager->handleChannelSuccess(m_incomingPacket); +} + +void SshConnectionPrivate::handleChannelFailure() +{ + m_channelManager->handleChannelFailure(m_incomingPacket); +} + +void SshConnectionPrivate::handleChannelWindowAdjust() +{ + m_channelManager->handleChannelWindowAdjust(m_incomingPacket); +} + +void SshConnectionPrivate::handleChannelData() +{ + m_channelManager->handleChannelData(m_incomingPacket); +} + +void SshConnectionPrivate::handleChannelExtendedData() +{ + m_channelManager->handleChannelExtendedData(m_incomingPacket); +} + +void SshConnectionPrivate::handleChannelEof() +{ + m_channelManager->handleChannelEof(m_incomingPacket); +} + +void SshConnectionPrivate::handleChannelClose() +{ + m_channelManager->handleChannelClose(m_incomingPacket); +} + +void SshConnectionPrivate::handleDisconnect() +{ + const SshDisconnect msg = m_incomingPacket.extractDisconnect(); + throw SshServerException(SSH_DISCONNECT_CONNECTION_LOST, + "", tr("Server closed connection: %1").arg(msg.description)); +} + + + +void SshConnectionPrivate::sendData(const QByteArray &data) +{ + if (canUseSocket()) + m_socket->write(data); +} + +void SshConnectionPrivate::handleSocketDisconnected() +{ + closeConnection(SSH_DISCONNECT_CONNECTION_LOST, SshClosedByServerError, + "Connection closed unexpectedly.", + tr("Connection closed unexpectedly.")); +} + +void SshConnectionPrivate::handleSocketError() +{ + if (m_error == SshNoError) { + closeConnection(SSH_DISCONNECT_CONNECTION_LOST, SshSocketError, + "Network error", m_socket->errorString()); + } +} + +void SshConnectionPrivate::handleTimeout() +{ + closeConnection(SSH_DISCONNECT_BY_APPLICATION, SshTimeoutError, "", + tr("Timeout waiting for reply from server.")); +} + +void SshConnectionPrivate::sendKeepAlivePacket() +{ + // This type of message is not allowed during key exchange. + if (m_keyExchangeState != NoKeyExchange) { + m_keepAliveTimer.start(); + return; + } + + Q_ASSERT(m_lastInvalidMsgSeqNr == InvalidSeqNr); + m_lastInvalidMsgSeqNr = m_sendFacility.nextClientSeqNr(); + m_sendFacility.sendInvalidPacket(); + m_timeoutTimer.start(); +} + +void SshConnectionPrivate::connectToHost() +{ + QSSH_ASSERT_AND_RETURN(m_state == SocketUnconnected); + + m_incomingData.clear(); + m_incomingPacket.reset(); + m_sendFacility.reset(); + m_error = SshNoError; + m_ignoreNextPacket = false; + m_errorString.clear(); + + try { + if (m_connParams.authenticationType == SshConnectionParameters::AuthenticationByKey) + createPrivateKey(); + } catch (const SshClientException &ex) { + m_error = ex.error; + m_errorString = ex.errorString; + emit error(m_error); + return; + } + + connect(m_socket, SIGNAL(connected()), this, SLOT(handleSocketConnected())); + connect(m_socket, SIGNAL(readyRead()), this, SLOT(handleIncomingData())); + connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, + SLOT(handleSocketError())); + connect(m_socket, SIGNAL(disconnected()), this, + SLOT(handleSocketDisconnected())); + connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(handleTimeout())); + m_state = SocketConnecting; + m_keyExchangeState = NoKeyExchange; + m_timeoutTimer.start(); + m_socket->connectToHost(m_connParams.host, m_connParams.port); +} + +void SshConnectionPrivate::closeConnection(SshErrorCode sshError, + SshError userError, const QByteArray &serverErrorString, + const QString &userErrorString) +{ + // Prevent endless loops by recursive exceptions. + if (m_state == SocketUnconnected || m_error != SshNoError) + return; + + m_error = userError; + m_errorString = userErrorString; + m_timeoutTimer.stop(); + disconnect(m_socket, 0, this, 0); + disconnect(&m_timeoutTimer, 0, this, 0); + m_keepAliveTimer.stop(); + disconnect(&m_keepAliveTimer, 0, this, 0); + try { + m_channelManager->closeAllChannels(); + m_sendFacility.sendDisconnectPacket(sshError, serverErrorString); + } catch (Botan::Exception &) {} // Nothing sensible to be done here. + if (m_error != SshNoError) + emit error(userError); + if (m_state == ConnectionEstablished) + emit disconnected(); + if (canUseSocket()) + m_socket->disconnectFromHost(); + m_state = SocketUnconnected; +} + +bool SshConnectionPrivate::canUseSocket() const +{ + return m_socket->isValid() + && m_socket->state() == QAbstractSocket::ConnectedState; +} + +void SshConnectionPrivate::createPrivateKey() +{ + if (m_connParams.privateKeyFile.isEmpty()) + throw SshClientException(SshKeyFileError, tr("No private key file given.")); + QFile keyFile(m_connParams.privateKeyFile); + if (!keyFile.open(QIODevice::ReadOnly)) { + throw SshClientException(SshKeyFileError, + tr("Private key file error: %1").arg(keyFile.errorString())); + } + m_sendFacility.createAuthenticationKey(keyFile.readAll()); +} + +QSharedPointer SshConnectionPrivate::createRemoteProcess(const QByteArray &command) +{ + return m_channelManager->createRemoteProcess(command); +} + +QSharedPointer SshConnectionPrivate::createRemoteShell() +{ + return m_channelManager->createRemoteShell(); +} + +QSharedPointer SshConnectionPrivate::createSftpChannel() +{ + return m_channelManager->createSftpChannel(); +} + +const quint64 SshConnectionPrivate::InvalidSeqNr = static_cast(-1); + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshconnection.h b/src/libs/ssh/sshconnection.h new file mode 100644 index 0000000000..b631c3e707 --- /dev/null +++ b/src/libs/ssh/sshconnection.h @@ -0,0 +1,124 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHCONNECTION_H +#define SSHCONNECTION_H + +#include "ssherrors.h" + +#include "ssh_global.h" + +#include +#include +#include +#include +#include + +namespace QSsh { +class SftpChannel; +class SshRemoteProcess; + +namespace Internal { +class SshConnectionPrivate; +} // namespace Internal + +class QSSH_EXPORT SshConnectionParameters +{ +public: + enum ProxyType { DefaultProxy, NoProxy }; + enum AuthenticationType { AuthenticationByPassword, AuthenticationByKey }; + SshConnectionParameters(); + + QString host; + QString userName; + QString password; + QString privateKeyFile; + int timeout; // In seconds. + AuthenticationType authenticationType; + quint16 port; + ProxyType proxyType; +}; + +QSSH_EXPORT bool operator==(const SshConnectionParameters &p1, const SshConnectionParameters &p2); +QSSH_EXPORT bool operator!=(const SshConnectionParameters &p1, const SshConnectionParameters &p2); + +class QSSH_EXPORT SshConnectionInfo +{ +public: + SshConnectionInfo() : localPort(0), peerPort(0) {} + SshConnectionInfo(const QHostAddress &la, quint16 lp, const QHostAddress &pa, quint16 pp) + : localAddress(la), localPort(lp), peerAddress(pa), peerPort(pp) {} + + QHostAddress localAddress; + quint16 localPort; + QHostAddress peerAddress; + quint16 peerPort; +}; + +class QSSH_EXPORT SshConnection : public QObject +{ + Q_OBJECT + +public: + enum State { Unconnected, Connecting, Connected }; + typedef QSharedPointer Ptr; + + static Ptr create(const SshConnectionParameters &serverInfo); + + void connectToHost(); + void disconnectFromHost(); + State state() const; + SshError errorState() const; + QString errorString() const; + SshConnectionParameters connectionParameters() const; + SshConnectionInfo connectionInfo() const; + ~SshConnection(); + + QSharedPointer createRemoteProcess(const QByteArray &command); + QSharedPointer createRemoteShell(); + QSharedPointer createSftpChannel(); + +signals: + void connected(); + void disconnected(); + void dataAvailable(const QString &message); + void error(QSsh::SshError); + +private: + SshConnection(const SshConnectionParameters &serverInfo); + + Internal::SshConnectionPrivate *d; +}; + +} // namespace QSsh + +#endif // SSHCONNECTION_H diff --git a/src/libs/ssh/sshconnection_p.h b/src/libs/ssh/sshconnection_p.h new file mode 100644 index 0000000000..b1765e0ec5 --- /dev/null +++ b/src/libs/ssh/sshconnection_p.h @@ -0,0 +1,177 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHCONNECTION_P_H +#define SSHCONNECTION_P_H + +#include "sshconnection.h" +#include "sshexception_p.h" +#include "sshincomingpacket_p.h" +#include "sshremoteprocess.h" +#include "sshsendfacility_p.h" + +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE +class QTcpSocket; +QT_END_NAMESPACE + +namespace Botan { class Exception; } + +namespace QSsh { +class SftpChannel; + +namespace Internal { +class SshChannelManager; + +// NOTE: When you add stuff here, don't forget to update m_packetHandlers. +enum SshStateInternal { + SocketUnconnected, // initial and after disconnect + SocketConnecting, // After connectToHost() + SocketConnected, // After socket's connected() signal + UserAuthServiceRequested, + UserAuthRequested, + ConnectionEstablished // After service has been started + // ... +}; + +enum SshKeyExchangeState { + NoKeyExchange, + KexInitSent, + DhInitSent, + NewKeysSent, + KeyExchangeSuccess // After server's DH_REPLY message +}; + +class SshConnectionPrivate : public QObject +{ + Q_OBJECT + friend class QSsh::SshConnection; +public: + SshConnectionPrivate(SshConnection *conn, + const SshConnectionParameters &serverInfo); + ~SshConnectionPrivate(); + + void connectToHost(); + void closeConnection(SshErrorCode sshError, SshError userError, + const QByteArray &serverErrorString, const QString &userErrorString); + QSharedPointer createRemoteProcess(const QByteArray &command); + QSharedPointer createRemoteShell(); + QSharedPointer createSftpChannel(); + SshStateInternal state() const { return m_state; } + SshError error() const { return m_error; } + QString errorString() const { return m_errorString; } + +signals: + void connected(); + void disconnected(); + void dataAvailable(const QString &message); + void error(QSsh::SshError); + +private: + Q_SLOT void handleSocketConnected(); + Q_SLOT void handleIncomingData(); + Q_SLOT void handleSocketError(); + Q_SLOT void handleSocketDisconnected(); + Q_SLOT void handleTimeout(); + Q_SLOT void sendKeepAlivePacket(); + + void handleServerId(); + void handlePackets(); + void handleCurrentPacket(); + void handleKeyExchangeInitPacket(); + void handleKeyExchangeReplyPacket(); + void handleNewKeysPacket(); + void handleServiceAcceptPacket(); + void handlePasswordExpiredPacket(); + void handleUserAuthSuccessPacket(); + void handleUserAuthFailurePacket(); + void handleUserAuthBannerPacket(); + void handleGlobalRequest(); + void handleDebugPacket(); + void handleUnimplementedPacket(); + void handleChannelRequest(); + void handleChannelOpen(); + void handleChannelOpenFailure(); + void handleChannelOpenConfirmation(); + void handleChannelSuccess(); + void handleChannelFailure(); + void handleChannelWindowAdjust(); + void handleChannelData(); + void handleChannelExtendedData(); + void handleChannelEof(); + void handleChannelClose(); + void handleDisconnect(); + bool canUseSocket() const; + void createPrivateKey(); + + void sendData(const QByteArray &data); + + typedef void (SshConnectionPrivate::*PacketHandler)(); + typedef QList StateList; + void setupPacketHandlers(); + void setupPacketHandler(SshPacketType type, const StateList &states, + PacketHandler handler); + + typedef QPair HandlerInStates; + QHash m_packetHandlers; + + static const quint64 InvalidSeqNr; + + QTcpSocket *m_socket; + SshStateInternal m_state; + SshKeyExchangeState m_keyExchangeState; + SshIncomingPacket m_incomingPacket; + SshSendFacility m_sendFacility; + SshChannelManager * const m_channelManager; + const SshConnectionParameters m_connParams; + QByteArray m_incomingData; + SshError m_error; + QString m_errorString; + QScopedPointer m_keyExchange; + QTimer m_timeoutTimer; + QTimer m_keepAliveTimer; + bool m_ignoreNextPacket; + SshConnection *m_conn; + quint64 m_lastInvalidMsgSeqNr; + QByteArray m_serverId; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHCONNECTION_P_H diff --git a/src/libs/ssh/sshconnectionmanager.cpp b/src/libs/ssh/sshconnectionmanager.cpp new file mode 100644 index 0000000000..2f5167a048 --- /dev/null +++ b/src/libs/ssh/sshconnectionmanager.cpp @@ -0,0 +1,227 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshconnectionmanager.h" + +#include "sshconnection.h" + +#include +#include +#include +#include +#include +#include + +namespace QSsh { +namespace Internal { + +class SshConnectionManagerPrivate : public QObject +{ + Q_OBJECT + +public: + + static QMutex instanceMutex; + static SshConnectionManager &instance() + { + static SshConnectionManager manager; + return manager; + } + + SshConnectionManagerPrivate() + { + moveToThread(QCoreApplication::instance()->thread()); + } + + QSharedPointer acquireConnection(const SshConnectionParameters &sshParams) + { + QMutexLocker locker(&m_listMutex); + + // Check in-use connections: + foreach (SshConnection::Ptr connection, m_acquiredConnections) { + if (connection->connectionParameters() != sshParams) + continue; + + if (connection->thread() != QThread::currentThread()) + break; + + if (m_deprecatedConnections.contains(connection)) // we were asked to no longer use this one... + break; + + m_acquiredConnections.append(connection); + return connection; + } + + // Checked cached open connections: + foreach (SshConnection::Ptr connection, m_unacquiredConnections) { + if (connection->state() != SshConnection::Connected + || connection->connectionParameters() != sshParams) + continue; + + if (connection->thread() != QThread::currentThread()) { + QMetaObject::invokeMethod(this, "switchToCallerThread", + Qt::BlockingQueuedConnection, + Q_ARG(SshConnection *, connection.data()), + Q_ARG(QObject *, QThread::currentThread())); + } + + m_unacquiredConnections.removeOne(connection); + m_acquiredConnections.append(connection); + return connection; + } + + // create a new connection: + SshConnection::Ptr connection = SshConnection::create(sshParams); + connect(connection.data(), SIGNAL(disconnected()), this, SLOT(cleanup())); + m_acquiredConnections.append(connection); + + return connection; + } + + void releaseConnection(const SshConnection::Ptr &connection) + { + QMutexLocker locker(&m_listMutex); + + m_acquiredConnections.removeOne(connection); + if (!m_acquiredConnections.contains(connection)) { + // no longer in use: + connection->moveToThread(QCoreApplication::instance()->thread()); + if (m_deprecatedConnections.contains(connection)) + m_deprecatedConnections.removeAll(connection); + else if (connection->state() == SshConnection::Connected) { + // Make sure to only keep one connection open + bool haveConnection = false; + foreach (SshConnection::Ptr conn, m_unacquiredConnections) { + if (conn->connectionParameters() == connection->connectionParameters()) { + haveConnection = true; + break; + } + } + if (!haveConnection) + m_unacquiredConnections.append(connection); + } + } + } + + void forceNewConnection(const SshConnectionParameters &sshParams) + { + QMutexLocker locker(&m_listMutex); + + SshConnection::Ptr toReset; + foreach (SshConnection::Ptr connection, m_unacquiredConnections) { + if (connection->connectionParameters() == sshParams) { + toReset = connection; + break; + } + } + + if (toReset.isNull()) { + foreach (SshConnection::Ptr connection, m_acquiredConnections) { + if (connection->connectionParameters() == sshParams) { + toReset = connection; + break; + } + } + } + + if (!toReset.isNull() && !m_deprecatedConnections.contains(toReset)) + m_deprecatedConnections.append(toReset); + } + +private: + Q_INVOKABLE void switchToCallerThread(SshConnection *connection, QObject *threadObj) + { + connection->moveToThread(qobject_cast(threadObj)); + } + +private slots: + void cleanup() + { + QMutexLocker locker(&m_listMutex); + + SshConnection *currentConnection = qobject_cast(sender()); + if (!currentConnection) + return; + + for (int i = m_unacquiredConnections.count() - 1; i >= 0; --i) { + if (m_unacquiredConnections.at(i) == currentConnection) + m_unacquiredConnections.removeAt(i); + } + } + +private: + // We expect the number of concurrently open connections to be small. + // If that turns out to not be the case, we can still use a data + // structure with faster access. + QList m_unacquiredConnections; + QList m_acquiredConnections; + QList m_deprecatedConnections; + QMutex m_listMutex; +}; + +QMutex SshConnectionManagerPrivate::instanceMutex; + +} // namespace Internal + +SshConnectionManager &SshConnectionManager::instance() +{ + QMutexLocker locker(&Internal::SshConnectionManagerPrivate::instanceMutex); + return Internal::SshConnectionManagerPrivate::instance(); +} + +SshConnectionManager::SshConnectionManager() + : d(new Internal::SshConnectionManagerPrivate) +{ +} + +SshConnectionManager::~SshConnectionManager() +{ +} + +SshConnection::Ptr SshConnectionManager::acquireConnection(const SshConnectionParameters &sshParams) +{ + return d->acquireConnection(sshParams); +} + +void SshConnectionManager::releaseConnection(const SshConnection::Ptr &connection) +{ + d->releaseConnection(connection); +} + +void SshConnectionManager::forceNewConnection(const SshConnectionParameters &sshParams) +{ + d->forceNewConnection(sshParams); +} + +} // namespace QSsh + +#include "sshconnectionmanager.moc" diff --git a/src/libs/ssh/sshconnectionmanager.h b/src/libs/ssh/sshconnectionmanager.h new file mode 100644 index 0000000000..f6f7c39228 --- /dev/null +++ b/src/libs/ssh/sshconnectionmanager.h @@ -0,0 +1,68 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHCONNECTIONMANAGER_H +#define SSHCONNECTIONMANAGER_H + +#include "ssh_global.h" + +#include +#include + +namespace QSsh { +class SshConnection; +class SshConnectionParameters; +namespace Internal { class SshConnectionManagerPrivate; } + +class QSSH_EXPORT SshConnectionManager +{ + friend class Internal::SshConnectionManagerPrivate; +public: + static SshConnectionManager &instance(); + + QSharedPointer acquireConnection(const SshConnectionParameters &sshParams); + void releaseConnection(const QSharedPointer &connection); + // Make sure the next acquireConnection with the given parameters will return a new connection. + void forceNewConnection(const SshConnectionParameters &sshParams); + +private: + explicit SshConnectionManager(); + virtual ~SshConnectionManager(); + SshConnectionManager(const SshConnectionManager &); + SshConnectionManager &operator=(const SshConnectionManager &); + + const QScopedPointer d; +}; + +} // namespace QSsh + +#endif // SSHCONNECTIONMANAGER_H diff --git a/src/libs/ssh/sshcryptofacility.cpp b/src/libs/ssh/sshcryptofacility.cpp new file mode 100644 index 0000000000..a795a6f56e --- /dev/null +++ b/src/libs/ssh/sshcryptofacility.cpp @@ -0,0 +1,387 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshcryptofacility_p.h" + +#include "sshbotanconversions_p.h" +#include "sshcapabilities_p.h" +#include "sshexception_p.h" +#include "sshkeyexchange_p.h" +#include "sshkeypasswordretriever_p.h" +#include "sshpacket_p.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +using namespace Botan; + +namespace QSsh { +namespace Internal { + +SshAbstractCryptoFacility::SshAbstractCryptoFacility() + : m_cipherBlockSize(0), m_macLength(0) +{ +} + +SshAbstractCryptoFacility::~SshAbstractCryptoFacility() {} + +void SshAbstractCryptoFacility::clearKeys() +{ + m_cipherBlockSize = 0; + m_macLength = 0; + m_sessionId.clear(); + m_pipe.reset(0); + m_hMac.reset(0); +} + +void SshAbstractCryptoFacility::recreateKeys(const SshKeyExchange &kex) +{ + checkInvariant(); + + if (m_sessionId.isEmpty()) + m_sessionId = kex.h(); + Algorithm_Factory &af = global_state().algorithm_factory(); + const std::string &cryptAlgo = botanCryptAlgoName(cryptAlgoName(kex)); + BlockCipher * const cipher = af.prototype_block_cipher(cryptAlgo)->clone(); + + m_cipherBlockSize = cipher->BLOCK_SIZE; + const QByteArray ivData = generateHash(kex, ivChar(), m_cipherBlockSize); + const InitializationVector iv(convertByteArray(ivData), m_cipherBlockSize); + + const quint32 keySize = max_keylength_of(cryptAlgo); + const QByteArray cryptKeyData = generateHash(kex, keyChar(), keySize); + SymmetricKey cryptKey(convertByteArray(cryptKeyData), keySize); + + BlockCipherMode * const cipherMode + = makeCipherMode(cipher, new Null_Padding, iv, cryptKey); + m_pipe.reset(new Pipe(cipherMode)); + + m_macLength = botanHMacKeyLen(hMacAlgoName(kex)); + const QByteArray hMacKeyData = generateHash(kex, macChar(), macLength()); + SymmetricKey hMacKey(convertByteArray(hMacKeyData), macLength()); + const HashFunction * const hMacProto + = af.prototype_hash_function(botanHMacAlgoName(hMacAlgoName(kex))); + m_hMac.reset(new HMAC(hMacProto->clone())); + m_hMac->set_key(hMacKey); +} + +void SshAbstractCryptoFacility::convert(QByteArray &data, quint32 offset, + quint32 dataSize) const +{ + Q_ASSERT(offset + dataSize <= static_cast(data.size())); + checkInvariant(); + + // Session id empty => No key exchange has happened yet. + if (dataSize == 0 || m_sessionId.isEmpty()) + return; + + if (dataSize % cipherBlockSize() != 0) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid packet size"); + } + m_pipe->process_msg(reinterpret_cast(data.constData()) + offset, + dataSize); + quint32 bytesRead = m_pipe->read(reinterpret_cast(data.data()) + offset, + dataSize, m_pipe->message_count() - 1); // Can't use Pipe::LAST_MESSAGE because of a VC bug. + Q_ASSERT(bytesRead == dataSize); +} + +QByteArray SshAbstractCryptoFacility::generateMac(const QByteArray &data, + quint32 dataSize) const +{ + return m_sessionId.isEmpty() + ? QByteArray() + : convertByteArray(m_hMac->process(reinterpret_cast(data.constData()), + dataSize)); +} + +QByteArray SshAbstractCryptoFacility::generateHash(const SshKeyExchange &kex, + char c, quint32 length) +{ + const QByteArray &k = kex.k(); + const QByteArray &h = kex.h(); + QByteArray data(k); + data.append(h).append(c).append(m_sessionId); + SecureVector key + = kex.hash()->process(convertByteArray(data), data.size()); + while (key.size() < length) { + SecureVector tmpKey; + tmpKey.append(convertByteArray(k), k.size()); + tmpKey.append(convertByteArray(h), h.size()); + tmpKey.append(key); + key.append(kex.hash()->process(tmpKey)); + } + return QByteArray(reinterpret_cast(key.begin()), length); +} + +void SshAbstractCryptoFacility::checkInvariant() const +{ + Q_ASSERT(m_sessionId.isEmpty() == !m_pipe); +} + + +const QByteArray SshEncryptionFacility::PrivKeyFileStartLineRsa("-----BEGIN RSA PRIVATE KEY-----"); +const QByteArray SshEncryptionFacility::PrivKeyFileStartLineDsa("-----BEGIN DSA PRIVATE KEY-----"); +const QByteArray SshEncryptionFacility::PrivKeyFileEndLineRsa("-----END RSA PRIVATE KEY-----"); +const QByteArray SshEncryptionFacility::PrivKeyFileEndLineDsa("-----END DSA PRIVATE KEY-----"); + +QByteArray SshEncryptionFacility::cryptAlgoName(const SshKeyExchange &kex) const +{ + return kex.encryptionAlgo(); +} + +QByteArray SshEncryptionFacility::hMacAlgoName(const SshKeyExchange &kex) const +{ + return kex.hMacAlgoClientToServer(); +} + +BlockCipherMode *SshEncryptionFacility::makeCipherMode(BlockCipher *cipher, + BlockCipherModePaddingMethod *paddingMethod, const InitializationVector &iv, + const SymmetricKey &key) +{ + return new CBC_Encryption(cipher, paddingMethod, key, iv); +} + +void SshEncryptionFacility::encrypt(QByteArray &data) const +{ + convert(data, 0, data.size()); +} + +void SshEncryptionFacility::createAuthenticationKey(const QByteArray &privKeyFileContents) +{ + if (privKeyFileContents == m_cachedPrivKeyContents) + return; + +#ifdef CREATOR_SSH_DEBUG + qDebug("%s: Key not cached, reading", Q_FUNC_INFO); +#endif + QList pubKeyParams; + QList allKeyParams; + QString error1; + QString error2; + if (!createAuthenticationKeyFromPKCS8(privKeyFileContents, pubKeyParams, allKeyParams, error1) + && !createAuthenticationKeyFromOpenSSL(privKeyFileContents, pubKeyParams, allKeyParams, + error2)) { +#ifdef CREATOR_SSH_DEBUG + qDebug("%s: %s\n\t%s\n", Q_FUNC_INFO, qPrintable(error1), qPrintable(error2)); +#endif + throw SshClientException(SshKeyFileError, SSH_TR("Decoding of private key file failed: " + "Format not understood.")); + } + + foreach (const BigInt &b, allKeyParams) { + if (b.is_zero()) { + throw SshClientException(SshKeyFileError, + SSH_TR("Decoding of private key file failed: Invalid zero parameter.")); + } + } + + m_authPubKeyBlob = AbstractSshPacket::encodeString(m_authKeyAlgoName); + foreach (const BigInt &b, pubKeyParams) + m_authPubKeyBlob += AbstractSshPacket::encodeMpInt(b); + m_cachedPrivKeyContents = privKeyFileContents; +} + +bool SshEncryptionFacility::createAuthenticationKeyFromPKCS8(const QByteArray &privKeyFileContents, + QList &pubKeyParams, QList &allKeyParams, QString &error) +{ + try { + Pipe pipe; + pipe.process_msg(convertByteArray(privKeyFileContents), privKeyFileContents.size()); + Private_Key * const key = PKCS8::load_key(pipe, m_rng, SshKeyPasswordRetriever()); + if (DSA_PrivateKey * const dsaKey = dynamic_cast(key)) { + m_authKeyAlgoName = SshCapabilities::PubKeyDss; + m_authKey.reset(dsaKey); + pubKeyParams << dsaKey->group_p() << dsaKey->group_q() + << dsaKey->group_g() << dsaKey->get_y(); + allKeyParams << pubKeyParams << dsaKey->get_x(); + } else if (RSA_PrivateKey * const rsaKey = dynamic_cast(key)) { + m_authKeyAlgoName = SshCapabilities::PubKeyRsa; + m_authKey.reset(rsaKey); + pubKeyParams << rsaKey->get_e() << rsaKey->get_n(); + allKeyParams << pubKeyParams << rsaKey->get_p() << rsaKey->get_q() + << rsaKey->get_d(); + } else { + qWarning("%s: Unexpected code flow, expected success or exception.", Q_FUNC_INFO); + return false; + } + } catch (const Botan::Exception &ex) { + error = QLatin1String(ex.what()); + return false; + } + return true; +} + +bool SshEncryptionFacility::createAuthenticationKeyFromOpenSSL(const QByteArray &privKeyFileContents, + QList &pubKeyParams, QList &allKeyParams, QString &error) +{ + try { + bool syntaxOk = true; + QList lines = privKeyFileContents.split('\n'); + while (lines.last().isEmpty()) + lines.removeLast(); + if (lines.count() < 3) { + syntaxOk = false; + } else if (lines.first() == PrivKeyFileStartLineRsa) { + if (lines.last() != PrivKeyFileEndLineRsa) + syntaxOk = false; + else + m_authKeyAlgoName = SshCapabilities::PubKeyRsa; + } else if (lines.first() == PrivKeyFileStartLineDsa) { + if (lines.last() != PrivKeyFileEndLineDsa) + syntaxOk = false; + else + m_authKeyAlgoName = SshCapabilities::PubKeyDss; + } else { + syntaxOk = false; + } + if (!syntaxOk) { + error = SSH_TR("Unexpected format."); + return false; + } + + QByteArray privateKeyBlob; + for (int i = 1; i < lines.size() - 1; ++i) + privateKeyBlob += lines.at(i); + privateKeyBlob = QByteArray::fromBase64(privateKeyBlob); + + BER_Decoder decoder(convertByteArray(privateKeyBlob), privateKeyBlob.size()); + BER_Decoder sequence = decoder.start_cons(SEQUENCE); + quint32 version; + sequence.decode (version); + if (version != 0) { + error = SSH_TR("Key encoding has version %1, expected 0.").arg(version); + return false; + } + + if (m_authKeyAlgoName == SshCapabilities::PubKeyDss) { + BigInt p, q, g, y, x; + sequence.decode (p).decode (q).decode (g).decode (y).decode (x); + DSA_PrivateKey * const dsaKey = new DSA_PrivateKey(m_rng, DL_Group(p, q, g), x); + m_authKey.reset(dsaKey); + pubKeyParams << p << q << g << y; + allKeyParams << pubKeyParams << x; + } else { + BigInt p, q, e, d, n; + sequence.decode(n).decode(e).decode(d).decode(p).decode(q); + RSA_PrivateKey * const rsaKey = new RSA_PrivateKey(m_rng, p, q, e, d, n); + m_authKey.reset(rsaKey); + pubKeyParams << e << n; + allKeyParams << pubKeyParams << p << q << d; + } + + sequence.discard_remaining(); + sequence.verify_end(); + } catch (const Botan::Exception &ex) { + error = QLatin1String(ex.what()); + return false; + } + return true; +} + +QByteArray SshEncryptionFacility::authenticationAlgorithmName() const +{ + Q_ASSERT(m_authKey); + return m_authKeyAlgoName; +} + +QByteArray SshEncryptionFacility::authenticationKeySignature(const QByteArray &data) const +{ + Q_ASSERT(m_authKey); + + QScopedPointer signer(get_pk_signer (*m_authKey, + botanEmsaAlgoName(m_authKeyAlgoName))); + QByteArray dataToSign = AbstractSshPacket::encodeString(sessionId()) + data; + QByteArray signature + = convertByteArray(signer->sign_message(convertByteArray(dataToSign), + dataToSign.size(), m_rng)); + return AbstractSshPacket::encodeString(m_authKeyAlgoName) + + AbstractSshPacket::encodeString(signature); +} + +QByteArray SshEncryptionFacility::getRandomNumbers(int count) const +{ + QByteArray data; + data.resize(count); + m_rng.randomize(convertByteArray(data), count); + return data; +} + +SshEncryptionFacility::~SshEncryptionFacility() {} + + +QByteArray SshDecryptionFacility::cryptAlgoName(const SshKeyExchange &kex) const +{ + return kex.decryptionAlgo(); +} + +QByteArray SshDecryptionFacility::hMacAlgoName(const SshKeyExchange &kex) const +{ + return kex.hMacAlgoServerToClient(); +} + +BlockCipherMode *SshDecryptionFacility::makeCipherMode(BlockCipher *cipher, + BlockCipherModePaddingMethod *paddingMethod, const InitializationVector &iv, + const SymmetricKey &key) +{ + return new CBC_Decryption(cipher, paddingMethod, key, iv); +} + +void SshDecryptionFacility::decrypt(QByteArray &data, quint32 offset, + quint32 dataSize) const +{ + convert(data, offset, dataSize); +#ifdef CREATOR_SSH_DEBUG + qDebug("Decrypted data:"); + const char * const start = data.constData() + offset; + const char * const end = start + dataSize; + for (const char *c = start; c < end; ++c) + qDebug() << "'" << *c << "' (0x" << (static_cast(*c) & 0xff) << ")"; +#endif +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshcryptofacility_p.h b/src/libs/ssh/sshcryptofacility_p.h new file mode 100644 index 0000000000..6784f6b05b --- /dev/null +++ b/src/libs/ssh/sshcryptofacility_p.h @@ -0,0 +1,157 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHABSTRACTCRYPTOFACILITY_P_H +#define SSHABSTRACTCRYPTOFACILITY_P_H + +#include +#include + +#include +#include + +namespace Botan { + class BigInt; + class BlockCipher; + class BlockCipherMode; + class BlockCipherModePaddingMethod; + class HashFunction; + class HMAC; + class Pipe; + class PK_Signing_Key; +} + +namespace QSsh { +namespace Internal { + +class SshKeyExchange; + +class SshAbstractCryptoFacility +{ +public: + virtual ~SshAbstractCryptoFacility(); + + void clearKeys(); + void recreateKeys(const SshKeyExchange &kex); + QByteArray generateMac(const QByteArray &data, quint32 dataSize) const; + quint32 cipherBlockSize() const { return m_cipherBlockSize; } + quint32 macLength() const { return m_macLength; } + +protected: + SshAbstractCryptoFacility(); + void convert(QByteArray &data, quint32 offset, quint32 dataSize) const; + QByteArray sessionId() const { return m_sessionId; } + +private: + SshAbstractCryptoFacility(const SshAbstractCryptoFacility &); + SshAbstractCryptoFacility &operator=(const SshAbstractCryptoFacility &); + + virtual QByteArray cryptAlgoName(const SshKeyExchange &kex) const = 0; + virtual QByteArray hMacAlgoName(const SshKeyExchange &kex) const = 0; + virtual Botan::BlockCipherMode *makeCipherMode(Botan::BlockCipher *cipher, + Botan::BlockCipherModePaddingMethod *paddingMethod, + const Botan::InitializationVector &iv, + const Botan::SymmetricKey &key) = 0; + virtual char ivChar() const = 0; + virtual char keyChar() const = 0; + virtual char macChar() const = 0; + + QByteArray generateHash(const SshKeyExchange &kex, char c, quint32 length); + void checkInvariant() const; + + QByteArray m_sessionId; + QScopedPointer m_pipe; + QScopedPointer m_hMac; + quint32 m_cipherBlockSize; + quint32 m_macLength; +}; + +class SshEncryptionFacility : public SshAbstractCryptoFacility +{ +public: + void encrypt(QByteArray &data) const; + + void createAuthenticationKey(const QByteArray &privKeyFileContents); + QByteArray authenticationAlgorithmName() const; + QByteArray authenticationPublicKey() const { return m_authPubKeyBlob; } + QByteArray authenticationKeySignature(const QByteArray &data) const; + QByteArray getRandomNumbers(int count) const; + + ~SshEncryptionFacility(); + +private: + virtual QByteArray cryptAlgoName(const SshKeyExchange &kex) const; + virtual QByteArray hMacAlgoName(const SshKeyExchange &kex) const; + virtual Botan::BlockCipherMode *makeCipherMode(Botan::BlockCipher *cipher, + Botan::BlockCipherModePaddingMethod *paddingMethod, + const Botan::InitializationVector &iv, const Botan::SymmetricKey &key); + virtual char ivChar() const { return 'A'; } + virtual char keyChar() const { return 'C'; } + virtual char macChar() const { return 'E'; } + + bool createAuthenticationKeyFromPKCS8(const QByteArray &privKeyFileContents, + QList &pubKeyParams, QList &allKeyParams, QString &error); + bool createAuthenticationKeyFromOpenSSL(const QByteArray &privKeyFileContents, + QList &pubKeyParams, QList &allKeyParams, QString &error); + + static const QByteArray PrivKeyFileStartLineRsa; + static const QByteArray PrivKeyFileStartLineDsa; + static const QByteArray PrivKeyFileEndLineRsa; + static const QByteArray PrivKeyFileEndLineDsa; + + QByteArray m_authKeyAlgoName; + QByteArray m_authPubKeyBlob; + QByteArray m_cachedPrivKeyContents; + QScopedPointer m_authKey; + mutable Botan::AutoSeeded_RNG m_rng; +}; + +class SshDecryptionFacility : public SshAbstractCryptoFacility +{ +public: + void decrypt(QByteArray &data, quint32 offset, quint32 dataSize) const; + +private: + virtual QByteArray cryptAlgoName(const SshKeyExchange &kex) const; + virtual QByteArray hMacAlgoName(const SshKeyExchange &kex) const; + virtual Botan::BlockCipherMode *makeCipherMode(Botan::BlockCipher *cipher, + Botan::BlockCipherModePaddingMethod *paddingMethod, + const Botan::InitializationVector &iv, const Botan::SymmetricKey &key); + virtual char ivChar() const { return 'B'; } + virtual char keyChar() const { return 'D'; } + virtual char macChar() const { return 'F'; } +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHABSTRACTCRYPTOFACILITY_P_H diff --git a/src/libs/ssh/ssherrors.h b/src/libs/ssh/ssherrors.h new file mode 100644 index 0000000000..d4ea7f82c4 --- /dev/null +++ b/src/libs/ssh/ssherrors.h @@ -0,0 +1,46 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHERRORS_P_H +#define SSHERRORS_P_H + +namespace QSsh { + +enum SshError { + SshNoError, SshSocketError, SshTimeoutError, SshProtocolError, + SshHostKeyError, SshKeyFileError, SshAuthenticationError, + SshClosedByServerError, SshInternalError +}; + +} // namespace QSsh + +#endif // SSHERRORS_P_H diff --git a/src/libs/ssh/sshexception_p.h b/src/libs/ssh/sshexception_p.h new file mode 100644 index 0000000000..4cfdfcd9c2 --- /dev/null +++ b/src/libs/ssh/sshexception_p.h @@ -0,0 +1,92 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHEXCEPTION_P_H +#define SSHEXCEPTION_P_H + +#include "ssherrors.h" + +#include +#include +#include + +namespace QSsh { +namespace Internal { + +enum SshErrorCode { + SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT = 1, + SSH_DISCONNECT_PROTOCOL_ERROR = 2, + SSH_DISCONNECT_KEY_EXCHANGE_FAILED = 3, + SSH_DISCONNECT_RESERVED = 4, + SSH_DISCONNECT_MAC_ERROR = 5, + SSH_DISCONNECT_COMPRESSION_ERROR = 6, + SSH_DISCONNECT_SERVICE_NOT_AVAILABLE = 7, + SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED = 8, + SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE = 9, + SSH_DISCONNECT_CONNECTION_LOST = 10, + SSH_DISCONNECT_BY_APPLICATION = 11, + SSH_DISCONNECT_TOO_MANY_CONNECTIONS = 12, + SSH_DISCONNECT_AUTH_CANCELLED_BY_USER = 13, + SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE = 14, + SSH_DISCONNECT_ILLEGAL_USER_NAME = 15 +}; + +#define SSH_TR(string) QCoreApplication::translate("SshConnection", string) + +#define SSH_SERVER_EXCEPTION(error, errorString) \ + SshServerException((error), (errorString), SSH_TR(errorString)) + +struct SshServerException +{ + SshServerException(SshErrorCode error, const QByteArray &errorStringServer, + const QString &errorStringUser) + : error(error), errorStringServer(errorStringServer), + errorStringUser(errorStringUser) {} + + const SshErrorCode error; + const QByteArray errorStringServer; + const QString errorStringUser; +}; + +struct SshClientException +{ + SshClientException(SshError error, const QString &errorString) + : error(error), errorString(errorString) {} + + const SshError error; + const QString errorString; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHEXCEPTION_P_H diff --git a/src/libs/ssh/sshincomingpacket.cpp b/src/libs/ssh/sshincomingpacket.cpp new file mode 100644 index 0000000000..22db307404 --- /dev/null +++ b/src/libs/ssh/sshincomingpacket.cpp @@ -0,0 +1,463 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshincomingpacket_p.h" + +#include "sshcapabilities_p.h" + +namespace QSsh { +namespace Internal { + +const QByteArray SshIncomingPacket::ExitStatusType("exit-status"); +const QByteArray SshIncomingPacket::ExitSignalType("exit-signal"); + +SshIncomingPacket::SshIncomingPacket() : m_serverSeqNr(0) { } + +quint32 SshIncomingPacket::cipherBlockSize() const +{ + return qMax(m_decrypter.cipherBlockSize(), 8U); +} + +quint32 SshIncomingPacket::macLength() const +{ + return m_decrypter.macLength(); +} + +void SshIncomingPacket::recreateKeys(const SshKeyExchange &keyExchange) +{ + m_decrypter.recreateKeys(keyExchange); +} + +void SshIncomingPacket::reset() +{ + clear(); + m_serverSeqNr = 0; + m_decrypter.clearKeys(); +} + +void SshIncomingPacket::consumeData(QByteArray &newData) +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("%s: current data size = %d, new data size = %d", + Q_FUNC_INFO, m_data.size(), newData.size()); +#endif + + if (isComplete() || newData.isEmpty()) + return; + + /* + * Until we have reached the minimum packet size, we cannot decrypt the + * length field. + */ + const quint32 minSize = minPacketSize(); + if (currentDataSize() < minSize) { + const int bytesToTake + = qMin(minSize - currentDataSize(), newData.size()); + moveFirstBytes(m_data, newData, bytesToTake); +#ifdef CREATOR_SSH_DEBUG + qDebug("Took %d bytes from new data", bytesToTake); +#endif + if (currentDataSize() < minSize) + return; + } + + const int bytesToTake + = qMin(length() + 4 + macLength() - currentDataSize(), + newData.size()); + moveFirstBytes(m_data, newData, bytesToTake); +#ifdef CREATOR_SSH_DEBUG + qDebug("Took %d bytes from new data", bytesToTake); +#endif + if (isComplete()) { +#ifdef CREATOR_SSH_DEBUG + qDebug("Message complete. Overall size: %u, payload size: %u", + m_data.size(), m_length - paddingLength() - 1); +#endif + decrypt(); + ++m_serverSeqNr; + } +} + +void SshIncomingPacket::decrypt() +{ + Q_ASSERT(isComplete()); + const quint32 netDataLength = length() + 4; + m_decrypter.decrypt(m_data, cipherBlockSize(), + netDataLength - cipherBlockSize()); + const QByteArray &mac = m_data.mid(netDataLength, macLength()); + if (mac != generateMac(m_decrypter, m_serverSeqNr)) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_MAC_ERROR, + "Message authentication failed."); + } +} + +void SshIncomingPacket::moveFirstBytes(QByteArray &target, QByteArray &source, + int n) +{ + target.append(source.left(n)); + source.remove(0, n); +} + +SshKeyExchangeInit SshIncomingPacket::extractKeyExchangeInitData() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_KEXINIT); + + SshKeyExchangeInit exchangeData; + try { + quint32 offset = TypeOffset + 1; + std::memcpy(exchangeData.cookie, &m_data.constData()[offset], + sizeof exchangeData.cookie); + offset += sizeof exchangeData.cookie; + exchangeData.keyAlgorithms + = SshPacketParser::asNameList(m_data, &offset); + exchangeData.serverHostKeyAlgorithms + = SshPacketParser::asNameList(m_data, &offset); + exchangeData.encryptionAlgorithmsClientToServer + = SshPacketParser::asNameList(m_data, &offset); + exchangeData.encryptionAlgorithmsServerToClient + = SshPacketParser::asNameList(m_data, &offset); + exchangeData.macAlgorithmsClientToServer + = SshPacketParser::asNameList(m_data, &offset); + exchangeData.macAlgorithmsServerToClient + = SshPacketParser::asNameList(m_data, &offset); + exchangeData.compressionAlgorithmsClientToServer + = SshPacketParser::asNameList(m_data, &offset); + exchangeData.compressionAlgorithmsServerToClient + = SshPacketParser::asNameList(m_data, &offset); + exchangeData.languagesClientToServer + = SshPacketParser::asNameList(m_data, &offset); + exchangeData.languagesServerToClient + = SshPacketParser::asNameList(m_data, &offset); + exchangeData.firstKexPacketFollows + = SshPacketParser::asBool(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED, + "Key exchange failed: Server sent invalid SSH_MSG_KEXINIT packet."); + } + return exchangeData; +} + +SshKeyExchangeReply SshIncomingPacket::extractKeyExchangeReply(const QByteArray &pubKeyAlgo) const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_KEXDH_REPLY); + + try { + SshKeyExchangeReply replyData; + quint32 offset = TypeOffset + 1; + const quint32 k_sLength + = SshPacketParser::asUint32(m_data, &offset); + if (offset + k_sLength > currentDataSize()) + throw SshPacketParseException(); + replyData.k_s = m_data.mid(offset - 4, k_sLength + 4); + if (SshPacketParser::asString(m_data, &offset) != pubKeyAlgo) + throw SshPacketParseException(); + + // DSS: p and q, RSA: e and n + replyData.parameters << SshPacketParser::asBigInt(m_data, &offset); + replyData.parameters << SshPacketParser::asBigInt(m_data, &offset); + + // g and y + if (pubKeyAlgo == SshCapabilities::PubKeyDss) { + replyData.parameters << SshPacketParser::asBigInt(m_data, &offset); + replyData.parameters << SshPacketParser::asBigInt(m_data, &offset); + } + + replyData.f = SshPacketParser::asBigInt(m_data, &offset); + offset += 4; + if (SshPacketParser::asString(m_data, &offset) != pubKeyAlgo) + throw SshPacketParseException(); + replyData.signatureBlob = SshPacketParser::asString(m_data, &offset); + return replyData; + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED, + "Key exchange failed: " + "Server sent invalid SSH_MSG_KEXDH_REPLY packet."); + } +} + +SshDisconnect SshIncomingPacket::extractDisconnect() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_DISCONNECT); + + SshDisconnect msg; + try { + quint32 offset = TypeOffset + 1; + msg.reasonCode = SshPacketParser::asUint32(m_data, &offset); + msg.description = SshPacketParser::asUserString(m_data, &offset); + msg.language = SshPacketParser::asString(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_MSG_DISCONNECT."); + } + + return msg; +} + +SshUserAuthBanner SshIncomingPacket::extractUserAuthBanner() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_USERAUTH_BANNER); + + try { + SshUserAuthBanner msg; + quint32 offset = TypeOffset + 1; + msg.message = SshPacketParser::asUserString(m_data, &offset); + msg.language = SshPacketParser::asString(m_data, &offset); + return msg; + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_MSG_USERAUTH_BANNER."); + } +} + +SshDebug SshIncomingPacket::extractDebug() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_DEBUG); + + try { + SshDebug msg; + quint32 offset = TypeOffset + 1; + msg.display = SshPacketParser::asBool(m_data, &offset); + msg.message = SshPacketParser::asUserString(m_data, &offset); + msg.language = SshPacketParser::asString(m_data, &offset); + return msg; + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_MSG_DEBUG."); + } +} + +SshUnimplemented SshIncomingPacket::extractUnimplemented() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_UNIMPLEMENTED); + + try { + SshUnimplemented msg; + quint32 offset = TypeOffset + 1; + msg.invalidMsgSeqNr = SshPacketParser::asUint32(m_data, &offset); + return msg; + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_MSG_UNIMPLEMENTED."); + } +} + +SshChannelOpenFailure SshIncomingPacket::extractChannelOpenFailure() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_CHANNEL_OPEN_FAILURE); + + SshChannelOpenFailure openFailure; + try { + quint32 offset = TypeOffset + 1; + openFailure.localChannel = SshPacketParser::asUint32(m_data, &offset); + openFailure.reasonCode = SshPacketParser::asUint32(m_data, &offset); + openFailure.reasonString = QString::fromLocal8Bit(SshPacketParser::asString(m_data, &offset)); + openFailure.language = SshPacketParser::asString(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Server sent invalid SSH_MSG_CHANNEL_OPEN_FAILURE packet."); + } + return openFailure; +} + +SshChannelOpenConfirmation SshIncomingPacket::extractChannelOpenConfirmation() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_CHANNEL_OPEN_CONFIRMATION); + + SshChannelOpenConfirmation confirmation; + try { + quint32 offset = TypeOffset + 1; + confirmation.localChannel = SshPacketParser::asUint32(m_data, &offset); + confirmation.remoteChannel = SshPacketParser::asUint32(m_data, &offset); + confirmation.remoteWindowSize = SshPacketParser::asUint32(m_data, &offset); + confirmation.remoteMaxPacketSize = SshPacketParser::asUint32(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Server sent invalid SSH_MSG_CHANNEL_OPEN_CONFIRMATION packet."); + } + return confirmation; +} + +SshChannelWindowAdjust SshIncomingPacket::extractWindowAdjust() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_CHANNEL_WINDOW_ADJUST); + + SshChannelWindowAdjust adjust; + try { + quint32 offset = TypeOffset + 1; + adjust.localChannel = SshPacketParser::asUint32(m_data, &offset); + adjust.bytesToAdd = SshPacketParser::asUint32(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_MSG_CHANNEL_WINDOW_ADJUST packet."); + } + return adjust; +} + +SshChannelData SshIncomingPacket::extractChannelData() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_CHANNEL_DATA); + + SshChannelData data; + try { + quint32 offset = TypeOffset + 1; + data.localChannel = SshPacketParser::asUint32(m_data, &offset); + data.data = SshPacketParser::asString(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_MSG_CHANNEL_DATA packet."); + } + return data; +} + +SshChannelExtendedData SshIncomingPacket::extractChannelExtendedData() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_CHANNEL_EXTENDED_DATA); + + SshChannelExtendedData data; + try { + quint32 offset = TypeOffset + 1; + data.localChannel = SshPacketParser::asUint32(m_data, &offset); + data.type = SshPacketParser::asUint32(m_data, &offset); + data.data = SshPacketParser::asString(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_MSG_CHANNEL_EXTENDED_DATA packet."); + } + return data; +} + +SshChannelExitStatus SshIncomingPacket::extractChannelExitStatus() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_CHANNEL_REQUEST); + + SshChannelExitStatus exitStatus; + try { + quint32 offset = TypeOffset + 1; + exitStatus.localChannel = SshPacketParser::asUint32(m_data, &offset); + const QByteArray &type = SshPacketParser::asString(m_data, &offset); + Q_ASSERT(type == ExitStatusType); + Q_UNUSED(type); + if (SshPacketParser::asBool(m_data, &offset)) + throw SshPacketParseException(); + exitStatus.exitStatus = SshPacketParser::asUint32(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid exit-status packet."); + } + return exitStatus; +} + +SshChannelExitSignal SshIncomingPacket::extractChannelExitSignal() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_CHANNEL_REQUEST); + + SshChannelExitSignal exitSignal; + try { + quint32 offset = TypeOffset + 1; + exitSignal.localChannel = SshPacketParser::asUint32(m_data, &offset); + const QByteArray &type = SshPacketParser::asString(m_data, &offset); + Q_ASSERT(type == ExitSignalType); + Q_UNUSED(type); + if (SshPacketParser::asBool(m_data, &offset)) + throw SshPacketParseException(); + exitSignal.signal = SshPacketParser::asString(m_data, &offset); + exitSignal.coreDumped = SshPacketParser::asBool(m_data, &offset); + exitSignal.error = SshPacketParser::asUserString(m_data, &offset); + exitSignal.language = SshPacketParser::asString(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid exit-signal packet."); + } + return exitSignal; +} + +quint32 SshIncomingPacket::extractRecipientChannel() const +{ + Q_ASSERT(isComplete()); + + try { + quint32 offset = TypeOffset + 1; + return SshPacketParser::asUint32(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Server sent invalid packet."); + } +} + +QByteArray SshIncomingPacket::extractChannelRequestType() const +{ + Q_ASSERT(isComplete()); + Q_ASSERT(type() == SSH_MSG_CHANNEL_REQUEST); + + try { + quint32 offset = TypeOffset + 1; + SshPacketParser::asUint32(m_data, &offset); + return SshPacketParser::asString(m_data, &offset); + } catch (SshPacketParseException &) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Invalid SSH_MSG_CHANNEL_REQUEST packet."); + } +} + +void SshIncomingPacket::calculateLength() const +{ + Q_ASSERT(currentDataSize() >= minPacketSize()); +#ifdef CREATOR_SSH_DEBUG + qDebug("Length field before decryption: %d-%d-%d-%d", m_data.at(0) & 0xff, + m_data.at(1) & 0xff, m_data.at(2) & 0xff, m_data.at(3) & 0xff); +#endif + m_decrypter.decrypt(m_data, 0, cipherBlockSize()); +#ifdef CREATOR_SSH_DEBUG + qDebug("Length field after decryption: %d-%d-%d-%d", m_data.at(0) & 0xff, m_data.at(1) & 0xff, m_data.at(2) & 0xff, m_data.at(3) & 0xff); + qDebug("message type = %d", m_data.at(TypeOffset)); +#endif + m_length = SshPacketParser::asUint32(m_data, static_cast(0)); +#ifdef CREATOR_SSH_DEBUG + qDebug("decrypted length is %u", m_length); +#endif +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshincomingpacket_p.h b/src/libs/ssh/sshincomingpacket_p.h new file mode 100644 index 0000000000..84d58783ed --- /dev/null +++ b/src/libs/ssh/sshincomingpacket_p.h @@ -0,0 +1,195 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHINCOMINGPACKET_P_H +#define SSHINCOMINGPACKET_P_H + +#include "sshpacket_p.h" + +#include "sshcryptofacility_p.h" +#include "sshpacketparser_p.h" + +#include +#include + +namespace QSsh { +namespace Internal { + +class SshKeyExchange; + +struct SshKeyExchangeInit +{ + char cookie[16]; + SshNameList keyAlgorithms; + SshNameList serverHostKeyAlgorithms; + SshNameList encryptionAlgorithmsClientToServer; + SshNameList encryptionAlgorithmsServerToClient; + SshNameList macAlgorithmsClientToServer; + SshNameList macAlgorithmsServerToClient; + SshNameList compressionAlgorithmsClientToServer; + SshNameList compressionAlgorithmsServerToClient; + SshNameList languagesClientToServer; + SshNameList languagesServerToClient; + bool firstKexPacketFollows; +}; + +struct SshKeyExchangeReply +{ + QByteArray k_s; + QList parameters; // DSS: p, q, g, y. RSA: e, n. + Botan::BigInt f; + QByteArray signatureBlob; +}; + +struct SshDisconnect +{ + quint32 reasonCode; + QString description; + QByteArray language; +}; + +struct SshUserAuthBanner +{ + QString message; + QByteArray language; +}; + +struct SshDebug +{ + bool display; + QString message; + QByteArray language; +}; + +struct SshUnimplemented +{ + quint32 invalidMsgSeqNr; +}; + +struct SshChannelOpenFailure +{ + quint32 localChannel; + quint32 reasonCode; + QString reasonString; + QByteArray language; +}; + +struct SshChannelOpenConfirmation +{ + quint32 localChannel; + quint32 remoteChannel; + quint32 remoteWindowSize; + quint32 remoteMaxPacketSize; +}; + +struct SshChannelWindowAdjust +{ + quint32 localChannel; + quint32 bytesToAdd; +}; + +struct SshChannelData +{ + quint32 localChannel; + QByteArray data; +}; + +struct SshChannelExtendedData +{ + quint32 localChannel; + quint32 type; + QByteArray data; +}; + +struct SshChannelExitStatus +{ + quint32 localChannel; + quint32 exitStatus; +}; + +struct SshChannelExitSignal +{ + quint32 localChannel; + QByteArray signal; + bool coreDumped; + QString error; + QByteArray language; +}; + + +class SshIncomingPacket : public AbstractSshPacket +{ +public: + SshIncomingPacket(); + + void consumeData(QByteArray &data); + void recreateKeys(const SshKeyExchange &keyExchange); + void reset(); + + SshKeyExchangeInit extractKeyExchangeInitData() const; + SshKeyExchangeReply extractKeyExchangeReply(const QByteArray &pubKeyAlgo) const; + SshDisconnect extractDisconnect() const; + SshUserAuthBanner extractUserAuthBanner() const; + SshDebug extractDebug() const; + SshUnimplemented extractUnimplemented() const; + + SshChannelOpenFailure extractChannelOpenFailure() const; + SshChannelOpenConfirmation extractChannelOpenConfirmation() const; + SshChannelWindowAdjust extractWindowAdjust() const; + SshChannelData extractChannelData() const; + SshChannelExtendedData extractChannelExtendedData() const; + SshChannelExitStatus extractChannelExitStatus() const; + SshChannelExitSignal extractChannelExitSignal() const; + quint32 extractRecipientChannel() const; + QByteArray extractChannelRequestType() const; + + quint32 serverSeqNr() const { return m_serverSeqNr; } + + static const QByteArray ExitStatusType; + static const QByteArray ExitSignalType; + +private: + virtual quint32 cipherBlockSize() const; + virtual quint32 macLength() const; + virtual void calculateLength() const; + + void decrypt(); + void moveFirstBytes(QByteArray &target, QByteArray &source, int n); + + quint32 m_serverSeqNr; + SshDecryptionFacility m_decrypter; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHINCOMINGPACKET_P_H diff --git a/src/libs/ssh/sshkeycreationdialog.cpp b/src/libs/ssh/sshkeycreationdialog.cpp new file mode 100644 index 0000000000..ab5595a49a --- /dev/null +++ b/src/libs/ssh/sshkeycreationdialog.cpp @@ -0,0 +1,145 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ +#include "sshkeycreationdialog.h" +#include "ui_sshkeycreationdialog.h" + +#include "sshkeygenerator.h" + +#include +#include +#include +#include +#include +#include +#include + +namespace QSsh { + +SshKeyCreationDialog::SshKeyCreationDialog(QWidget *parent) + : QDialog(parent), m_keyGenerator(0), m_ui(new Ui::SshKeyCreationDialog) +{ + m_ui->setupUi(this); + const QString defaultPath = QDesktopServices::storageLocation(QDesktopServices::HomeLocation) + + QLatin1String("/.ssh/qtc_id"); + setPrivateKeyFile(defaultPath); + + connect(m_ui->rsa, SIGNAL(toggled(bool)), this, SLOT(keyTypeChanged())); + connect(m_ui->dsa, SIGNAL(toggled(bool)), this, SLOT(keyTypeChanged())); + connect(m_ui->privateKeyFileButton, SIGNAL(clicked()), SLOT(handleBrowseButtonClicked())); + connect(m_ui->generateButton, SIGNAL(clicked()), this, SLOT(generateKeys())); +} + +SshKeyCreationDialog::~SshKeyCreationDialog() +{ + delete m_keyGenerator; +} + +void SshKeyCreationDialog::keyTypeChanged() +{ + m_ui->comboBox->setCurrentIndex(0); + m_ui->comboBox->setEnabled(m_ui->rsa->isChecked()); +} + +void SshKeyCreationDialog::generateKeys() +{ + const SshKeyGenerator::KeyType keyType = m_ui->rsa->isChecked() + ? SshKeyGenerator::Rsa + : SshKeyGenerator::Dsa; + + if (!m_keyGenerator) + m_keyGenerator = new SshKeyGenerator; + + QApplication::setOverrideCursor(Qt::BusyCursor); + const bool success = m_keyGenerator->generateKeys(keyType, SshKeyGenerator::Mixed, + m_ui->comboBox->currentText().toUShort()); + QApplication::restoreOverrideCursor(); + + if (success) { + saveKeys(); + } else { + QMessageBox::critical(this, tr("Key Generation Failed"), m_keyGenerator->error()); + } +} + +void SshKeyCreationDialog::handleBrowseButtonClicked() +{ + const QString filePath = QFileDialog::getSaveFileName(this, tr("Choose Private Key File Name")); + if (!filePath.isEmpty()) + setPrivateKeyFile(filePath); +} + +void SshKeyCreationDialog::setPrivateKeyFile(const QString &filePath) +{ + m_ui->privateKeyFileValueLabel->setText(filePath); + m_ui->generateButton->setEnabled(!privateKeyFilePath().isEmpty()); + m_ui->publicKeyFileLabel->setText(filePath + QLatin1String(".pub")); +} + +void SshKeyCreationDialog::saveKeys() +{ + const QString parentDir = QFileInfo(privateKeyFilePath()).dir().path(); + if (!QDir::root().mkpath(parentDir)) { + QMessageBox::critical(this, tr("Failure To Save Key File"), + tr("Failed to create directory: '%1'.").arg(parentDir)); + return; + } + + QFile privateKeyFile(privateKeyFilePath()); + if (!privateKeyFile.open(QIODevice::WriteOnly) + || !privateKeyFile.write(m_keyGenerator->privateKey())) { + QMessageBox::critical(this, tr("Saving Private Key File failed"), + tr("The private key file could not be saved: %1").arg(privateKeyFile.errorString())); + return; + } + QFile::setPermissions(privateKeyFilePath(), QFile::ReadOwner | QFile::WriteOwner); + + QFile publicKeyFile(publicKeyFilePath()); + if (!publicKeyFile.open(QIODevice::WriteOnly) + || !publicKeyFile.write(m_keyGenerator->publicKey())) { + QMessageBox::critical(this, tr("Saving Public Key File failed"), + tr("The public key file could not be saved: %1").arg(publicKeyFile.errorString())); + return; + } + + accept(); +} + +QString SshKeyCreationDialog::privateKeyFilePath() const +{ + return m_ui->privateKeyFileValueLabel->text(); +} + +QString SshKeyCreationDialog::publicKeyFilePath() const +{ + return m_ui->publicKeyFileLabel->text(); +} + +} // namespace QSsh diff --git a/src/libs/ssh/sshkeycreationdialog.h b/src/libs/ssh/sshkeycreationdialog.h new file mode 100644 index 0000000000..7f30792a3f --- /dev/null +++ b/src/libs/ssh/sshkeycreationdialog.h @@ -0,0 +1,70 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHKEYCREATIONDIALOG_H +#define SSHKEYCREATIONDIALOG_H + +#include "ssh_global.h" + +#include + +namespace QSsh { +class SshKeyGenerator; + +namespace Ui { class SshKeyCreationDialog; } + +class QSSH_EXPORT SshKeyCreationDialog : public QDialog +{ + Q_OBJECT +public: + SshKeyCreationDialog(QWidget *parent = 0); + ~SshKeyCreationDialog(); + + QString privateKeyFilePath() const; + QString publicKeyFilePath() const; + +private slots: + void keyTypeChanged(); + void generateKeys(); + void handleBrowseButtonClicked(); + +private: + void setPrivateKeyFile(const QString &filePath); + void saveKeys(); + +private: + SshKeyGenerator *m_keyGenerator; + Ui::SshKeyCreationDialog *m_ui; +}; + +} // namespace QSsh + +#endif // SSHKEYCREATIONDIALOG_H diff --git a/src/libs/ssh/sshkeycreationdialog.ui b/src/libs/ssh/sshkeycreationdialog.ui new file mode 100644 index 0000000000..d49681e65b --- /dev/null +++ b/src/libs/ssh/sshkeycreationdialog.ui @@ -0,0 +1,272 @@ + + + QSsh::SshKeyCreationDialog + + + true + + + + 0 + 0 + 295 + 223 + + + + + 0 + 0 + + + + SSH Key Configuration + + + + + + Options + + + + + + Key algorithm: + + + + + + + + + + 0 + 0 + + + + &RSA + + + true + + + + + + + + 0 + 0 + + + + &DSA + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + 0 + 0 + + + + Key &size: + + + comboBox + + + + + + + + + + 1024 + + + + + 2048 + + + + + 4096 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Private key file: + + + + + + + + + + + + + + + + Browse... + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Public key file: + + + + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + 6 + + + + + + 0 + 0 + + + + &Generate And Save Key Pair + + + + + + + + 0 + 0 + + + + &Cancel + + + + + + + Qt::Horizontal + + + + 0 + 0 + + + + + + + + + + + + closeButton + clicked() + QSsh::SshKeyCreationDialog + close() + + + 195 + 184 + + + 381 + 107 + + + + + diff --git a/src/libs/ssh/sshkeyexchange.cpp b/src/libs/ssh/sshkeyexchange.cpp new file mode 100644 index 0000000000..333f394dea --- /dev/null +++ b/src/libs/ssh/sshkeyexchange.cpp @@ -0,0 +1,225 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshkeyexchange_p.h" + +#include "sshbotanconversions_p.h" +#include "sshcapabilities_p.h" +#include "sshsendfacility_p.h" +#include "sshexception_p.h" +#include "sshincomingpacket_p.h" + +#include +#include +#include +#include +#include + +#ifdef CREATOR_SSH_DEBUG +#include +#endif +#include + +using namespace Botan; + +namespace QSsh { +namespace Internal { + +namespace { + + // For debugging + void printNameList(const char *listName, const SshNameList &list) + { +#ifdef CREATOR_SSH_DEBUG + qDebug("%s:", listName); + foreach (const QByteArray &name, list.names) + qDebug("%s", name.constData()); +#else + Q_UNUSED(listName); + Q_UNUSED(list); +#endif + } + +#ifdef CREATOR_SSH_DEBUG + void printData(const char *name, const QByteArray &data) + { + std::cerr << std::hex; + qDebug("The client thinks the %s has length %d and is:", name, data.count()); + for (int i = 0; i < data.count(); ++i) + std::cerr << (static_cast(data.at(i)) & 0xff) << ' '; + std::cerr << std::endl; + } +#endif + +} // anonymous namespace + +SshKeyExchange::SshKeyExchange(SshSendFacility &sendFacility) + : m_sendFacility(sendFacility) +{ +} + +SshKeyExchange::~SshKeyExchange() {} + +void SshKeyExchange::sendKexInitPacket(const QByteArray &serverId) +{ + m_serverId = serverId; + m_clientKexInitPayload = m_sendFacility.sendKeyExchangeInitPacket(); +} + +bool SshKeyExchange::sendDhInitPacket(const SshIncomingPacket &serverKexInit) +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("server requests key exchange"); +#endif + serverKexInit.printRawBytes(); + SshKeyExchangeInit kexInitParams + = serverKexInit.extractKeyExchangeInitData(); + + printNameList("Key Algorithms", kexInitParams.keyAlgorithms); + printNameList("Server Host Key Algorithms", kexInitParams.serverHostKeyAlgorithms); + printNameList("Encryption algorithms client to server", kexInitParams.encryptionAlgorithmsClientToServer); + printNameList("Encryption algorithms server to client", kexInitParams.encryptionAlgorithmsServerToClient); + printNameList("MAC algorithms client to server", kexInitParams.macAlgorithmsClientToServer); + printNameList("MAC algorithms server to client", kexInitParams.macAlgorithmsServerToClient); + printNameList("Compression algorithms client to server", kexInitParams.compressionAlgorithmsClientToServer); + printNameList("Compression algorithms client to server", kexInitParams.compressionAlgorithmsClientToServer); + printNameList("Languages client to server", kexInitParams.languagesClientToServer); + printNameList("Languages server to client", kexInitParams.languagesServerToClient); +#ifdef CREATOR_SSH_DEBUG + qDebug("First packet follows: %d", kexInitParams.firstKexPacketFollows); +#endif + + const QByteArray &keyAlgo + = SshCapabilities::findBestMatch(SshCapabilities::KeyExchangeMethods, + kexInitParams.keyAlgorithms.names); + m_serverHostKeyAlgo + = SshCapabilities::findBestMatch(SshCapabilities::PublicKeyAlgorithms, + kexInitParams.serverHostKeyAlgorithms.names); + m_encryptionAlgo + = SshCapabilities::findBestMatch(SshCapabilities::EncryptionAlgorithms, + kexInitParams.encryptionAlgorithmsClientToServer.names); + m_decryptionAlgo + = SshCapabilities::findBestMatch(SshCapabilities::EncryptionAlgorithms, + kexInitParams.encryptionAlgorithmsServerToClient.names); + m_c2sHMacAlgo + = SshCapabilities::findBestMatch(SshCapabilities::MacAlgorithms, + kexInitParams.macAlgorithmsClientToServer.names); + m_s2cHMacAlgo + = SshCapabilities::findBestMatch(SshCapabilities::MacAlgorithms, + kexInitParams.macAlgorithmsServerToClient.names); + SshCapabilities::findBestMatch(SshCapabilities::CompressionAlgorithms, + kexInitParams.compressionAlgorithmsClientToServer.names); + SshCapabilities::findBestMatch(SshCapabilities::CompressionAlgorithms, + kexInitParams.compressionAlgorithmsServerToClient.names); + + AutoSeeded_RNG rng; + m_dhKey.reset(new DH_PrivateKey(rng, + DL_Group(botanKeyExchangeAlgoName(keyAlgo)))); + + m_serverKexInitPayload = serverKexInit.payLoad(); + m_sendFacility.sendKeyDhInitPacket(m_dhKey->get_y()); + return kexInitParams.firstKexPacketFollows; +} + +void SshKeyExchange::sendNewKeysPacket(const SshIncomingPacket &dhReply, + const QByteArray &clientId) +{ + const SshKeyExchangeReply &reply + = dhReply.extractKeyExchangeReply(m_serverHostKeyAlgo); + if (reply.f <= 0 || reply.f >= m_dhKey->group_p()) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED, + "Server sent invalid f."); + } + + QByteArray concatenatedData = AbstractSshPacket::encodeString(clientId); + concatenatedData += AbstractSshPacket::encodeString(m_serverId); + concatenatedData += AbstractSshPacket::encodeString(m_clientKexInitPayload); + concatenatedData += AbstractSshPacket::encodeString(m_serverKexInitPayload); + concatenatedData += reply.k_s; + concatenatedData += AbstractSshPacket::encodeMpInt(m_dhKey->get_y()); + concatenatedData += AbstractSshPacket::encodeMpInt(reply.f); + SymmetricKey k = m_dhKey->derive_key(reply.f); + m_k = AbstractSshPacket::encodeMpInt(BigInt(k.begin(), k.length())); + concatenatedData += m_k; + + m_hash.reset(get_hash(botanSha1Name())); + const SecureVector &hashResult + = m_hash->process(convertByteArray(concatenatedData), + concatenatedData.size()); + m_h = convertByteArray(hashResult); + +#ifdef CREATOR_SSH_DEBUG + printData("Client Id", AbstractSshPacket::encodeString(clientId)); + printData("Server Id", AbstractSshPacket::encodeString(m_serverId)); + printData("Client Payload", AbstractSshPacket::encodeString(m_clientKexInitPayload)); + printData("Server payload", AbstractSshPacket::encodeString(m_serverKexInitPayload)); + printData("K_S", reply.k_s); + printData("y", AbstractSshPacket::encodeMpInt(m_dhKey->get_y())); + printData("f", AbstractSshPacket::encodeMpInt(reply.f)); + printData("K", m_k); + printData("Concatenated data", concatenatedData); + printData("H", m_h); +#endif // CREATOR_SSH_DEBUG + + QScopedPointer sigKey; + QScopedPointer verifier; + if (m_serverHostKeyAlgo == SshCapabilities::PubKeyDss) { + const DL_Group group(reply.parameters.at(0), reply.parameters.at(1), + reply.parameters.at(2)); + DSA_PublicKey * const dsaKey + = new DSA_PublicKey(group, reply.parameters.at(3)); + sigKey.reset(dsaKey); + verifier.reset(get_pk_verifier(*dsaKey, + botanEmsaAlgoName(SshCapabilities::PubKeyDss))); + } else if (m_serverHostKeyAlgo == SshCapabilities::PubKeyRsa) { + RSA_PublicKey * const rsaKey + = new RSA_PublicKey(reply.parameters.at(1), reply.parameters.at(0)); + sigKey.reset(rsaKey); + verifier.reset(get_pk_verifier(*rsaKey, + botanEmsaAlgoName(SshCapabilities::PubKeyRsa))); + } else { + Q_ASSERT(!"Impossible: Neither DSS nor RSA!"); + } + const byte * const botanH = convertByteArray(m_h); + const Botan::byte * const botanSig + = convertByteArray(reply.signatureBlob); + if (!verifier->verify_message(botanH, m_h.size(), botanSig, + reply.signatureBlob.size())) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED, + "Invalid signature in SSH_MSG_KEXDH_REPLY packet."); + } + + m_sendFacility.sendNewKeysPacket(); +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshkeyexchange_p.h b/src/libs/ssh/sshkeyexchange_p.h new file mode 100644 index 0000000000..eb6a13f30a --- /dev/null +++ b/src/libs/ssh/sshkeyexchange_p.h @@ -0,0 +1,90 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHKEYEXCHANGE_P_H +#define SSHKEYEXCHANGE_P_H + +#include + +#include +#include + +namespace Botan { class HashFunction; } + +namespace QSsh { +namespace Internal { + +class SshSendFacility; +class SshIncomingPacket; + +class SshKeyExchange +{ +public: + SshKeyExchange(SshSendFacility &sendFacility); + ~SshKeyExchange(); + + void sendKexInitPacket(const QByteArray &serverId); + + // Returns true <=> the server sends a guessed package. + bool sendDhInitPacket(const SshIncomingPacket &serverKexInit); + + void sendNewKeysPacket(const SshIncomingPacket &dhReply, + const QByteArray &clientId); + + QByteArray k() const { return m_k; } + QByteArray h() const { return m_h; } + Botan::HashFunction *hash() const { return m_hash.data(); } + QByteArray encryptionAlgo() const { return m_encryptionAlgo; } + QByteArray decryptionAlgo() const { return m_decryptionAlgo; } + QByteArray hMacAlgoClientToServer() const { return m_c2sHMacAlgo; } + QByteArray hMacAlgoServerToClient() const { return m_s2cHMacAlgo; } + +private: + QByteArray m_serverId; + QByteArray m_clientKexInitPayload; + QByteArray m_serverKexInitPayload; + QScopedPointer m_dhKey; + QByteArray m_k; + QByteArray m_h; + QByteArray m_serverHostKeyAlgo; + QByteArray m_encryptionAlgo; + QByteArray m_decryptionAlgo; + QByteArray m_c2sHMacAlgo; + QByteArray m_s2cHMacAlgo; + QScopedPointer m_hash; + SshSendFacility &m_sendFacility; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHKEYEXCHANGE_P_H diff --git a/src/libs/ssh/sshkeygenerator.cpp b/src/libs/ssh/sshkeygenerator.cpp new file mode 100644 index 0000000000..f71a2232c7 --- /dev/null +++ b/src/libs/ssh/sshkeygenerator.cpp @@ -0,0 +1,201 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshkeygenerator.h" + +#include "sshbotanconversions_p.h" +#include "sshcapabilities_p.h" +#include "sshpacket_p.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +namespace QSsh { + +using namespace Botan; +using namespace Internal; + +SshKeyGenerator::SshKeyGenerator() : m_type(Rsa) +{ +} + +bool SshKeyGenerator::generateKeys(KeyType type, PrivateKeyFormat format, int keySize, + EncryptionMode encryptionMode) +{ + m_type = type; + m_encryptionMode = encryptionMode; + + try { + AutoSeeded_RNG rng; + KeyPtr key; + if (m_type == Rsa) + key = KeyPtr(new RSA_PrivateKey(rng, keySize)); + else + key = KeyPtr(new DSA_PrivateKey(rng, DL_Group(rng, DL_Group::DSA_Kosherizer, keySize))); + switch (format) { + case Pkcs8: + generatePkcs8KeyStrings(key, rng); + break; + case OpenSsl: + generateOpenSslKeyStrings(key); + break; + case Mixed: + default: + generatePkcs8KeyString(key, true, rng); + generateOpenSslPublicKeyString(key); + } + return true; + } catch (Botan::Exception &e) { + m_error = tr("Error generating key: %1").arg(QString::fromAscii(e.what())); + return false; + } +} + +void SshKeyGenerator::generatePkcs8KeyStrings(const KeyPtr &key, Botan::RandomNumberGenerator &rng) +{ + generatePkcs8KeyString(key, false, rng); + generatePkcs8KeyString(key, true, rng); +} + +void SshKeyGenerator::generatePkcs8KeyString(const KeyPtr &key, bool privateKey, + Botan::RandomNumberGenerator &rng) +{ + Pipe pipe; + pipe.start_msg(); + QByteArray *keyData; + if (privateKey) { + QString password; + if (m_encryptionMode == DoOfferEncryption) + password = getPassword(); + if (!password.isEmpty()) + PKCS8::encrypt_key(*key, pipe, rng, password.toLocal8Bit().data()); + else + PKCS8::encode(*key, pipe); + keyData = &m_privateKey; + } else { + X509::encode(*key, pipe); + keyData = &m_publicKey; + } + pipe.end_msg(); + keyData->resize(pipe.remaining(pipe.message_count() - 1)); + pipe.read(convertByteArray(*keyData), keyData->size(), + pipe.message_count() - 1); +} + +void SshKeyGenerator::generateOpenSslKeyStrings(const KeyPtr &key) +{ + generateOpenSslPublicKeyString(key); + generateOpenSslPrivateKeyString(key); +} + +void SshKeyGenerator::generateOpenSslPublicKeyString(const KeyPtr &key) +{ + QList params; + QByteArray keyId; + if (m_type == Rsa) { + const QSharedPointer rsaKey = key.dynamicCast(); + params << rsaKey->get_e() << rsaKey->get_n(); + keyId = SshCapabilities::PubKeyRsa; + } else { + const QSharedPointer dsaKey = key.dynamicCast(); + params << dsaKey->group_p() << dsaKey->group_q() << dsaKey->group_g() << dsaKey->get_y(); + keyId = SshCapabilities::PubKeyDss; + } + + QByteArray publicKeyBlob = AbstractSshPacket::encodeString(keyId); + foreach (const BigInt &b, params) + publicKeyBlob += AbstractSshPacket::encodeMpInt(b); + publicKeyBlob = publicKeyBlob.toBase64(); + const QByteArray id = "QtCreator/" + + QDateTime::currentDateTime().toString(Qt::ISODate).toUtf8(); + m_publicKey = keyId + ' ' + publicKeyBlob + ' ' + id; +} + +void SshKeyGenerator::generateOpenSslPrivateKeyString(const KeyPtr &key) +{ + QList params; + QByteArray keyId; + const char *label; + if (m_type == Rsa) { + const QSharedPointer rsaKey + = key.dynamicCast(); + params << rsaKey->get_n() << rsaKey->get_e() << rsaKey->get_d() << rsaKey->get_p() + << rsaKey->get_q(); + keyId = SshCapabilities::PubKeyRsa; + label = "RSA PRIVATE KEY"; + } else { + const QSharedPointer dsaKey = key.dynamicCast(); + params << dsaKey->group_p() << dsaKey->group_q() << dsaKey->group_g() << dsaKey->get_y() + << dsaKey->get_x(); + keyId = SshCapabilities::PubKeyDss; + label = "DSA PRIVATE KEY"; + } + + DER_Encoder encoder; + encoder.start_cons(SEQUENCE).encode(0U); + foreach (const BigInt &b, params) + encoder.encode(b); + encoder.end_cons(); + m_privateKey = QByteArray(PEM_Code::encode (encoder.get_contents(), label).c_str()); +} + +QString SshKeyGenerator::getPassword() const +{ + QInputDialog d; + d.setInputMode(QInputDialog::TextInput); + d.setTextEchoMode(QLineEdit::Password); + d.setWindowTitle(tr("Password for Private Key")); + d.setLabelText(tr("It is recommended that you secure your private key\n" + "with a password, which you can enter below.")); + d.setOkButtonText(tr("Encrypt Key File")); + d.setCancelButtonText(tr("Do Not Encrypt Key File")); + int result = QDialog::Accepted; + QString password; + while (result == QDialog::Accepted && password.isEmpty()) { + result = d.exec(); + password = d.textValue(); + } + return result == QDialog::Accepted ? password : QString(); +} + +} // namespace QSsh diff --git a/src/libs/ssh/sshkeygenerator.h b/src/libs/ssh/sshkeygenerator.h new file mode 100644 index 0000000000..09bf1804ed --- /dev/null +++ b/src/libs/ssh/sshkeygenerator.h @@ -0,0 +1,85 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHKEYGENERATOR_H +#define SSHKEYGENERATOR_H + +#include "ssh_global.h" + +#include +#include + +namespace Botan { + class Private_Key; + class RandomNumberGenerator; +} + +namespace QSsh { + +class QSSH_EXPORT SshKeyGenerator +{ + Q_DECLARE_TR_FUNCTIONS(SshKeyGenerator) +public: + enum KeyType { Rsa, Dsa }; + enum PrivateKeyFormat { Pkcs8, OpenSsl, Mixed }; + enum EncryptionMode { DoOfferEncryption, DoNotOfferEncryption }; // Only relevant for Pkcs8 format. + + SshKeyGenerator(); + bool generateKeys(KeyType type, PrivateKeyFormat format, int keySize, + EncryptionMode encryptionMode = DoOfferEncryption); + + QString error() const { return m_error; } + QByteArray privateKey() const { return m_privateKey; } + QByteArray publicKey() const { return m_publicKey; } + KeyType type() const { return m_type; } + +private: + typedef QSharedPointer KeyPtr; + + void generatePkcs8KeyStrings(const KeyPtr &key, Botan::RandomNumberGenerator &rng); + void generatePkcs8KeyString(const KeyPtr &key, bool privateKey, + Botan::RandomNumberGenerator &rng); + void generateOpenSslKeyStrings(const KeyPtr &key); + void generateOpenSslPrivateKeyString(const KeyPtr &key); + void generateOpenSslPublicKeyString(const KeyPtr &key); + QString getPassword() const; + + QString m_error; + QByteArray m_publicKey; + QByteArray m_privateKey; + KeyType m_type; + EncryptionMode m_encryptionMode; +}; + +} // namespace QSsh + +#endif // SSHKEYGENERATOR_H diff --git a/src/libs/ssh/sshkeypasswordretriever.cpp b/src/libs/ssh/sshkeypasswordretriever.cpp new file mode 100644 index 0000000000..5e725c7f55 --- /dev/null +++ b/src/libs/ssh/sshkeypasswordretriever.cpp @@ -0,0 +1,65 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ +#include "sshkeypasswordretriever_p.h" + +#include +#include +#include + +#include + +namespace QSsh { +namespace Internal { + +std::string SshKeyPasswordRetriever::get_passphrase(const std::string &, const std::string &, + UI_Result &result) const +{ + const bool hasGui = dynamic_cast(QApplication::instance()); + if (hasGui) { + bool ok; + const QString &password = QInputDialog::getText(0, + QCoreApplication::translate("QSsh::Ssh", "Password Required"), + QCoreApplication::translate("QSsh::Ssh", "Please enter the password for your private key."), + QLineEdit::Password, QString(), &ok); + result = ok ? OK : CANCEL_ACTION; + return std::string(password.toLocal8Bit().data()); + } else { + result = OK; + std::string password; + std::cout << "Please enter the password for your private key (set echo off beforehand!): " << std::flush; + std::cin >> password; + return password; + } +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshkeypasswordretriever_p.h b/src/libs/ssh/sshkeypasswordretriever_p.h new file mode 100644 index 0000000000..b3a0b0f09e --- /dev/null +++ b/src/libs/ssh/sshkeypasswordretriever_p.h @@ -0,0 +1,52 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ +#ifndef KEYPASSWORDRETRIEVER_H +#define KEYPASSWORDRETRIEVER_H + +#include + +#include + +namespace QSsh { +namespace Internal { + +class SshKeyPasswordRetriever : public Botan::User_Interface +{ +public: + std::string get_passphrase(const std::string &what, const std::string &source, + UI_Result &result) const; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // KEYPASSWORDRETRIEVER_H diff --git a/src/libs/ssh/sshoutgoingpacket.cpp b/src/libs/ssh/sshoutgoingpacket.cpp new file mode 100644 index 0000000000..04a38d9927 --- /dev/null +++ b/src/libs/ssh/sshoutgoingpacket.cpp @@ -0,0 +1,325 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshoutgoingpacket_p.h" + +#include "sshcapabilities_p.h" +#include "sshcryptofacility_p.h" + +#include + +namespace QSsh { +namespace Internal { + +SshOutgoingPacket::SshOutgoingPacket(const SshEncryptionFacility &encrypter, + const quint32 &seqNr) : m_encrypter(encrypter), m_seqNr(seqNr) +{ +} + +quint32 SshOutgoingPacket::cipherBlockSize() const +{ + return qMax(m_encrypter.cipherBlockSize(), 4U); +} + +quint32 SshOutgoingPacket::macLength() const +{ + return m_encrypter.macLength(); +} + +QByteArray SshOutgoingPacket::generateKeyExchangeInitPacket() +{ + const QByteArray &supportedkeyExchangeMethods + = encodeNameList(SshCapabilities::KeyExchangeMethods); + const QByteArray &supportedPublicKeyAlgorithms + = encodeNameList(SshCapabilities::PublicKeyAlgorithms); + const QByteArray &supportedEncryptionAlgorithms + = encodeNameList(SshCapabilities::EncryptionAlgorithms); + const QByteArray &supportedMacAlgorithms + = encodeNameList(SshCapabilities::MacAlgorithms); + const QByteArray &supportedCompressionAlgorithms + = encodeNameList(SshCapabilities::CompressionAlgorithms); + const QByteArray &supportedLanguages = encodeNameList(QList()); + + init(SSH_MSG_KEXINIT); + m_data += m_encrypter.getRandomNumbers(16); + m_data.append(supportedkeyExchangeMethods); + m_data.append(supportedPublicKeyAlgorithms); + m_data.append(supportedEncryptionAlgorithms) + .append(supportedEncryptionAlgorithms); + m_data.append(supportedMacAlgorithms).append(supportedMacAlgorithms); + m_data.append(supportedCompressionAlgorithms) + .append(supportedCompressionAlgorithms); + m_data.append(supportedLanguages).append(supportedLanguages); + appendBool(false); // No guessed packet. + m_data.append(QByteArray(4, 0)); // Reserved. + QByteArray payload = m_data.mid(PayloadOffset); + finalize(); + return payload; +} + +void SshOutgoingPacket::generateKeyDhInitPacket(const Botan::BigInt &e) +{ + init(SSH_MSG_KEXDH_INIT).appendMpInt(e).finalize(); +} + +void SshOutgoingPacket::generateNewKeysPacket() +{ + init(SSH_MSG_NEWKEYS).finalize(); +} + +void SshOutgoingPacket::generateUserAuthServiceRequestPacket() +{ + generateServiceRequest("ssh-userauth"); +} + +void SshOutgoingPacket::generateServiceRequest(const QByteArray &service) +{ + init(SSH_MSG_SERVICE_REQUEST).appendString(service).finalize(); +} + +void SshOutgoingPacket::generateUserAuthByPwdRequestPacket(const QByteArray &user, + const QByteArray &service, const QByteArray &pwd) +{ + init(SSH_MSG_USERAUTH_REQUEST).appendString(user).appendString(service) + .appendString("password").appendBool(false).appendString(pwd) + .finalize(); +} + +void SshOutgoingPacket::generateUserAuthByKeyRequestPacket(const QByteArray &user, + const QByteArray &service) +{ + init(SSH_MSG_USERAUTH_REQUEST).appendString(user).appendString(service) + .appendString("publickey").appendBool(true) + .appendString(m_encrypter.authenticationAlgorithmName()) + .appendString(m_encrypter.authenticationPublicKey()); + const QByteArray &dataToSign = m_data.mid(PayloadOffset); + appendString(m_encrypter.authenticationKeySignature(dataToSign)); + finalize(); +} + +void SshOutgoingPacket::generateRequestFailurePacket() +{ + init(SSH_MSG_REQUEST_FAILURE).finalize(); +} + +void SshOutgoingPacket::generateIgnorePacket() +{ + init(SSH_MSG_IGNORE).finalize(); +} + +void SshOutgoingPacket::generateInvalidMessagePacket() +{ + init(SSH_MSG_INVALID).finalize(); +} + +void SshOutgoingPacket::generateSessionPacket(quint32 channelId, + quint32 windowSize, quint32 maxPacketSize) +{ + init(SSH_MSG_CHANNEL_OPEN).appendString("session").appendInt(channelId) + .appendInt(windowSize).appendInt(maxPacketSize).finalize(); +} + +void SshOutgoingPacket::generateEnvPacket(quint32 remoteChannel, + const QByteArray &var, const QByteArray &value) +{ + init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel).appendString("env") + .appendBool(false).appendString(var).appendString(value).finalize(); +} + +void SshOutgoingPacket::generatePtyRequestPacket(quint32 remoteChannel, + const SshPseudoTerminal &terminal) +{ + init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel) + .appendString("pty-req").appendBool(false) + .appendString(terminal.termType).appendInt(terminal.columnCount) + .appendInt(terminal.rowCount).appendInt(0).appendInt(0); + QByteArray modeString; + for (SshPseudoTerminal::ModeMap::ConstIterator it = terminal.modes.constBegin(); + it != terminal.modes.constEnd(); ++it) { + modeString += char(it.key()); + modeString += encodeInt(it.value()); + } + modeString += char(0); // TTY_OP_END + appendString(modeString).finalize(); +} + +void SshOutgoingPacket::generateExecPacket(quint32 remoteChannel, + const QByteArray &command) +{ + init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel).appendString("exec") + .appendBool(true).appendString(command).finalize(); +} + +void SshOutgoingPacket::generateShellPacket(quint32 remoteChannel) +{ + init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel).appendString("shell") + .appendBool(true).finalize(); +} + +void SshOutgoingPacket::generateSftpPacket(quint32 remoteChannel) +{ + init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel) + .appendString("subsystem").appendBool(true).appendString("sftp") + .finalize(); +} + +void SshOutgoingPacket::generateWindowAdjustPacket(quint32 remoteChannel, + quint32 bytesToAdd) +{ + init(SSH_MSG_CHANNEL_WINDOW_ADJUST).appendInt(remoteChannel) + .appendInt(bytesToAdd).finalize(); +} + +void SshOutgoingPacket::generateChannelDataPacket(quint32 remoteChannel, + const QByteArray &data) +{ + init(SSH_MSG_CHANNEL_DATA).appendInt(remoteChannel).appendString(data) + .finalize(); +} + +void SshOutgoingPacket::generateChannelSignalPacket(quint32 remoteChannel, + const QByteArray &signalName) +{ + init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel) + .appendString("signal").appendBool(false).appendString(signalName) + .finalize(); +} + +void SshOutgoingPacket::generateChannelEofPacket(quint32 remoteChannel) +{ + init(SSH_MSG_CHANNEL_EOF).appendInt(remoteChannel).finalize(); +} + +void SshOutgoingPacket::generateChannelClosePacket(quint32 remoteChannel) +{ + init(SSH_MSG_CHANNEL_CLOSE).appendInt(remoteChannel).finalize(); +} + +void SshOutgoingPacket::generateDisconnectPacket(SshErrorCode reason, + const QByteArray &reasonString) +{ + init(SSH_MSG_DISCONNECT).appendInt(reason).appendString(reasonString) + .appendString(QByteArray()).finalize(); +} + +void SshOutgoingPacket::generateMsgUnimplementedPacket(quint32 serverSeqNr) +{ + init(SSH_MSG_UNIMPLEMENTED).appendInt(serverSeqNr).finalize(); +} + +SshOutgoingPacket &SshOutgoingPacket::appendInt(quint32 val) +{ + m_data.append(encodeInt(val)); + return *this; +} + +SshOutgoingPacket &SshOutgoingPacket::appendMpInt(const Botan::BigInt &number) +{ + m_data.append(encodeMpInt(number)); + return *this; +} + +SshOutgoingPacket &SshOutgoingPacket::appendBool(bool b) +{ + m_data += static_cast(b); + return *this; +} + +SshOutgoingPacket &SshOutgoingPacket::appendString(const QByteArray &string) +{ + m_data.append(encodeString(string)); + return *this; +} + +SshOutgoingPacket &SshOutgoingPacket::init(SshPacketType type) +{ + m_data.resize(TypeOffset + 1); + m_data[TypeOffset] = type; + return *this; +} + +SshOutgoingPacket &SshOutgoingPacket::setPadding() +{ + m_data += m_encrypter.getRandomNumbers(MinPaddingLength); + int padLength = MinPaddingLength; + const int divisor = sizeDivisor(); + const int mod = m_data.size() % divisor; + padLength += divisor - mod; + m_data += m_encrypter.getRandomNumbers(padLength - MinPaddingLength); + m_data[PaddingLengthOffset] = padLength; + return *this; +} + +SshOutgoingPacket &SshOutgoingPacket::encrypt() +{ + const QByteArray &mac + = generateMac(m_encrypter, m_seqNr); + m_encrypter.encrypt(m_data); + m_data += mac; + return *this; +} + +void SshOutgoingPacket::finalize() +{ + setPadding(); + setLengthField(m_data); + m_length = m_data.size() - 4; +#ifdef CREATOR_SSH_DEBUG + qDebug("Encrypting packet of type %u", m_data.at(TypeOffset)); +#endif + encrypt(); +#ifdef CREATOR_SSH_DEBUG + qDebug("Sending packet of size %d", rawData().count()); +#endif + Q_ASSERT(isComplete()); +} + +int SshOutgoingPacket::sizeDivisor() const +{ + return qMax(cipherBlockSize(), 8U); +} + +QByteArray SshOutgoingPacket::encodeNameList(const QList &list) +{ + QByteArray data; + data.resize(4); + for (int i = 0; i < list.count(); ++i) { + if (i > 0) + data.append(','); + data.append(list.at(i)); + } + AbstractSshPacket::setLengthField(data); + return data; +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshoutgoingpacket_p.h b/src/libs/ssh/sshoutgoingpacket_p.h new file mode 100644 index 0000000000..4a12218ebc --- /dev/null +++ b/src/libs/ssh/sshoutgoingpacket_p.h @@ -0,0 +1,108 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHOUTGOINGPACKET_P_H +#define SSHOUTGOINGPACKET_P_H + +#include "sshpacket_p.h" + +#include "sshpseudoterminal.h" + +namespace QSsh { +namespace Internal { + +class SshEncryptionFacility; + +class SshOutgoingPacket : public AbstractSshPacket +{ +public: + SshOutgoingPacket(const SshEncryptionFacility &encrypter, + const quint32 &seqNr); + + QByteArray generateKeyExchangeInitPacket(); // Returns payload. + void generateKeyDhInitPacket(const Botan::BigInt &e); + void generateNewKeysPacket(); + void generateDisconnectPacket(SshErrorCode reason, + const QByteArray &reasonString); + void generateMsgUnimplementedPacket(quint32 serverSeqNr); + void generateUserAuthServiceRequestPacket(); + void generateUserAuthByPwdRequestPacket(const QByteArray &user, + const QByteArray &service, const QByteArray &pwd); + void generateUserAuthByKeyRequestPacket(const QByteArray &user, + const QByteArray &service); + void generateRequestFailurePacket(); + void generateIgnorePacket(); + void generateInvalidMessagePacket(); + void generateSessionPacket(quint32 channelId, quint32 windowSize, + quint32 maxPacketSize); + void generateEnvPacket(quint32 remoteChannel, const QByteArray &var, + const QByteArray &value); + void generatePtyRequestPacket(quint32 remoteChannel, + const SshPseudoTerminal &terminal); + void generateExecPacket(quint32 remoteChannel, const QByteArray &command); + void generateShellPacket(quint32 remoteChannel); + void generateSftpPacket(quint32 remoteChannel); + void generateWindowAdjustPacket(quint32 remoteChannel, quint32 bytesToAdd); + void generateChannelDataPacket(quint32 remoteChannel, + const QByteArray &data); + void generateChannelSignalPacket(quint32 remoteChannel, + const QByteArray &signalName); + void generateChannelEofPacket(quint32 remoteChannel); + void generateChannelClosePacket(quint32 remoteChannel); + +private: + virtual quint32 cipherBlockSize() const; + virtual quint32 macLength() const; + + static QByteArray encodeNameList(const QList &list); + + void generateServiceRequest(const QByteArray &service); + + SshOutgoingPacket &init(SshPacketType type); + SshOutgoingPacket &setPadding(); + SshOutgoingPacket &encrypt(); + void finalize(); + + SshOutgoingPacket &appendInt(quint32 val); + SshOutgoingPacket &appendString(const QByteArray &string); + SshOutgoingPacket &appendMpInt(const Botan::BigInt &number); + SshOutgoingPacket &appendBool(bool b); + int sizeDivisor() const; + + const SshEncryptionFacility &m_encrypter; + const quint32 &m_seqNr; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHOUTGOINGPACKET_P_H diff --git a/src/libs/ssh/sshpacket.cpp b/src/libs/ssh/sshpacket.cpp new file mode 100644 index 0000000000..3e6e408587 --- /dev/null +++ b/src/libs/ssh/sshpacket.cpp @@ -0,0 +1,168 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshpacket_p.h" + +#include "sshcapabilities_p.h" +#include "sshcryptofacility_p.h" +#include "sshexception_p.h" +#include "sshpacketparser_p.h" + +#include + +#include + +namespace QSsh { +namespace Internal { + +const quint32 AbstractSshPacket::PaddingLengthOffset = 4; +const quint32 AbstractSshPacket::PayloadOffset = PaddingLengthOffset + 1; +const quint32 AbstractSshPacket::TypeOffset = PayloadOffset; +const quint32 AbstractSshPacket::MinPaddingLength = 4; + +namespace { + + void printByteArray(const QByteArray &data) + { +#ifdef CREATOR_SSH_DEBUG + for (int i = 0; i < data.count(); ++i) + qDebug() << std::hex << (static_cast(data[i]) & 0xff) << " "; +#else + Q_UNUSED(data); +#endif + } +} // anonymous namespace + + +AbstractSshPacket::AbstractSshPacket() : m_length(0) { } +AbstractSshPacket::~AbstractSshPacket() {} + +bool AbstractSshPacket::isComplete() const +{ + if (currentDataSize() < minPacketSize()) + return false; + Q_ASSERT(4 + length() + macLength() >= currentDataSize()); + return 4 + length() + macLength() == currentDataSize(); +} + +void AbstractSshPacket::clear() +{ + m_data.clear(); + m_length = 0; +} + +SshPacketType AbstractSshPacket::type() const +{ + Q_ASSERT(isComplete()); + return static_cast(m_data.at(TypeOffset)); +} + +QByteArray AbstractSshPacket::payLoad() const +{ + return QByteArray(m_data.constData() + PayloadOffset, + length() - paddingLength() - 1); +} + +void AbstractSshPacket::printRawBytes() const +{ + printByteArray(m_data); +} + +QByteArray AbstractSshPacket::encodeString(const QByteArray &string) +{ + QByteArray data; + data.resize(4); + data += string; + setLengthField(data); + return data; +} + +QByteArray AbstractSshPacket::encodeMpInt(const Botan::BigInt &number) +{ + if (number.is_zero()) + return QByteArray(4, 0); + + int stringLength = number.bytes(); + const bool positiveAndMsbSet = number.sign() == Botan::BigInt::Positive + && (number.byte_at(stringLength - 1) & 0x80); + if (positiveAndMsbSet) + ++stringLength; + QByteArray data; + data.resize(4 + stringLength); + int pos = 4; + if (positiveAndMsbSet) + data[pos++] = '\0'; + number.binary_encode(reinterpret_cast(data.data()) + pos); + setLengthField(data); + return data; +} + +int AbstractSshPacket::paddingLength() const +{ + return m_data[PaddingLengthOffset]; +} + +quint32 AbstractSshPacket::length() const +{ + //Q_ASSERT(currentDataSize() >= minPacketSize()); + if (m_length == 0) + calculateLength(); + return m_length; +} + +void AbstractSshPacket::calculateLength() const +{ + m_length = SshPacketParser::asUint32(m_data, static_cast(0)); +} + +QByteArray AbstractSshPacket::generateMac(const SshAbstractCryptoFacility &crypt, + quint32 seqNr) const +{ + const quint32 seqNrBe = qToBigEndian(seqNr); + QByteArray data(reinterpret_cast(&seqNrBe), sizeof seqNrBe); + data += QByteArray(m_data.constData(), length() + 4); + return crypt.generateMac(data, data.size()); +} + +quint32 AbstractSshPacket::minPacketSize() const +{ + return qMax(cipherBlockSize(), 16) + macLength(); +} + +void AbstractSshPacket::setLengthField(QByteArray &data) +{ + const quint32 length = qToBigEndian(data.size() - 4); + data.replace(0, 4, reinterpret_cast(&length), 4); +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshpacket_p.h b/src/libs/ssh/sshpacket_p.h new file mode 100644 index 0000000000..22a7a9e826 --- /dev/null +++ b/src/libs/ssh/sshpacket_p.h @@ -0,0 +1,146 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHPACKET_P_H +#define SSHPACKET_P_H + +#include "sshexception_p.h" + +#include +#include +#include + +#include + +namespace QSsh { +namespace Internal { + +enum SshPacketType { + SSH_MSG_DISCONNECT = 1, + SSH_MSG_IGNORE = 2, + SSH_MSG_UNIMPLEMENTED = 3, + SSH_MSG_DEBUG = 4, + SSH_MSG_SERVICE_REQUEST = 5, + SSH_MSG_SERVICE_ACCEPT = 6, + + SSH_MSG_KEXINIT = 20, + SSH_MSG_NEWKEYS = 21, + SSH_MSG_KEXDH_INIT = 30, + SSH_MSG_KEXDH_REPLY = 31, + + SSH_MSG_USERAUTH_REQUEST = 50, + SSH_MSG_USERAUTH_FAILURE = 51, + SSH_MSG_USERAUTH_SUCCESS = 52, + SSH_MSG_USERAUTH_BANNER = 53, + SSH_MSG_USERAUTH_PK_OK = 60, + SSH_MSG_USERAUTH_PASSWD_CHANGEREQ = 60, + + SSH_MSG_GLOBAL_REQUEST = 80, + SSH_MSG_REQUEST_SUCCESS = 81, + SSH_MSG_REQUEST_FAILURE = 82, + + // TODO: We currently take no precautions against sending these messages + // during a key re-exchange, which is not allowed. + SSH_MSG_CHANNEL_OPEN = 90, + SSH_MSG_CHANNEL_OPEN_CONFIRMATION = 91, + SSH_MSG_CHANNEL_OPEN_FAILURE = 92, + SSH_MSG_CHANNEL_WINDOW_ADJUST = 93, + SSH_MSG_CHANNEL_DATA = 94, + SSH_MSG_CHANNEL_EXTENDED_DATA = 95, + SSH_MSG_CHANNEL_EOF = 96, + SSH_MSG_CHANNEL_CLOSE = 97, + SSH_MSG_CHANNEL_REQUEST = 98, + SSH_MSG_CHANNEL_SUCCESS = 99, + SSH_MSG_CHANNEL_FAILURE = 100, + + // Not completely safe, since the server may actually understand this + // message type as an extension. Switch to a different value in that case + // (between 128 and 191). + SSH_MSG_INVALID = 128 +}; + +enum SshExtendedDataType { SSH_EXTENDED_DATA_STDERR = 1 }; + +class SshAbstractCryptoFacility; + +class AbstractSshPacket +{ +public: + virtual ~AbstractSshPacket(); + + void clear(); + bool isComplete() const; + SshPacketType type() const; + + static QByteArray encodeString(const QByteArray &string); + static QByteArray encodeMpInt(const Botan::BigInt &number); + template static QByteArray encodeInt(T value) + { + const T valMsb = qToBigEndian(value); + return QByteArray(reinterpret_cast(&valMsb), sizeof valMsb); + } + + static void setLengthField(QByteArray &data); + + void printRawBytes() const; // For Debugging. + + const QByteArray &rawData() const { return m_data; } + + QByteArray payLoad() const; + +protected: + AbstractSshPacket(); + + virtual quint32 cipherBlockSize() const = 0; + virtual quint32 macLength() const = 0; + virtual void calculateLength() const; + + quint32 length() const; + int paddingLength() const; + quint32 minPacketSize() const; + quint32 currentDataSize() const { return m_data.size(); } + QByteArray generateMac(const SshAbstractCryptoFacility &crypt, + quint32 seqNr) const; + + static const quint32 PaddingLengthOffset; + static const quint32 PayloadOffset; + static const quint32 TypeOffset; + static const quint32 MinPaddingLength; + + mutable QByteArray m_data; + mutable quint32 m_length; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHPACKET_P_H diff --git a/src/libs/ssh/sshpacketparser.cpp b/src/libs/ssh/sshpacketparser.cpp new file mode 100644 index 0000000000..c5197fc01c --- /dev/null +++ b/src/libs/ssh/sshpacketparser.cpp @@ -0,0 +1,156 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshpacketparser_p.h" + +#include + +namespace QSsh { +namespace Internal { + +namespace { quint32 size(const QByteArray &data) { return data.size(); } } + +QString SshPacketParser::asUserString(const QByteArray &rawString) +{ + QByteArray filteredString; + filteredString.resize(rawString.size()); + for (int i = 0; i < rawString.size(); ++i) { + const char c = rawString.at(i); + filteredString[i] + = std::isprint(c) || c == '\n' || c == '\r' || c == '\t' ? c : '?'; + } + return QString::fromUtf8(filteredString); +} + +bool SshPacketParser::asBool(const QByteArray &data, quint32 offset) +{ + if (size(data) <= offset) + throw SshPacketParseException(); + return data.at(offset); +} + +bool SshPacketParser::asBool(const QByteArray &data, quint32 *offset) +{ + bool b = asBool(data, *offset); + ++(*offset); + return b; +} + + +quint32 SshPacketParser::asUint32(const QByteArray &data, quint32 offset) +{ + if (size(data) < offset + 4) + throw SshPacketParseException(); + const quint32 value = ((data.at(offset) & 0xff) << 24) + + ((data.at(offset + 1) & 0xff) << 16) + + ((data.at(offset + 2) & 0xff) << 8) + (data.at(offset + 3) & 0xff); + return value; +} + +quint32 SshPacketParser::asUint32(const QByteArray &data, quint32 *offset) +{ + const quint32 v = asUint32(data, *offset); + *offset += 4; + return v; +} + +quint64 SshPacketParser::asUint64(const QByteArray &data, quint32 offset) +{ + if (size(data) < offset + 8) + throw SshPacketParseException(); + const quint64 value = (static_cast(data.at(offset) & 0xff) << 56) + + (static_cast(data.at(offset + 1) & 0xff) << 48) + + (static_cast(data.at(offset + 2) & 0xff) << 40) + + (static_cast(data.at(offset + 3) & 0xff) << 32) + + ((data.at(offset + 4) & 0xff) << 24) + + ((data.at(offset + 5) & 0xff) << 16) + + ((data.at(offset + 6) & 0xff) << 8) + + (data.at(offset + 7) & 0xff); + return value; +} + +quint64 SshPacketParser::asUint64(const QByteArray &data, quint32 *offset) +{ + const quint64 val = asUint64(data, *offset); + *offset += 8; + return val; +} + +QByteArray SshPacketParser::asString(const QByteArray &data, quint32 *offset) +{ + const quint32 length = asUint32(data, offset); + if (size(data) < *offset + length) + throw SshPacketParseException(); + const QByteArray &string = data.mid(*offset, length); + *offset += length; + return string; +} + +QString SshPacketParser::asUserString(const QByteArray &data, quint32 *offset) +{ + return asUserString(asString(data, offset)); +} + +SshNameList SshPacketParser::asNameList(const QByteArray &data, quint32 *offset) +{ + const quint32 length = asUint32(data, offset); + const int listEndPos = *offset + length; + if (data.size() < listEndPos) + throw SshPacketParseException(); + SshNameList names(length + 4); + int nextNameOffset = *offset; + int nextCommaOffset = data.indexOf(',', nextNameOffset); + while (nextNameOffset > 0 && nextNameOffset < listEndPos) { + const int stringEndPos = nextCommaOffset == -1 + || nextCommaOffset > listEndPos ? listEndPos : nextCommaOffset; + names.names << QByteArray(data.constData() + nextNameOffset, + stringEndPos - nextNameOffset); + nextNameOffset = nextCommaOffset + 1; + nextCommaOffset = data.indexOf(',', nextNameOffset); + } + *offset += length; + return names; +} + +Botan::BigInt SshPacketParser::asBigInt(const QByteArray &data, quint32 *offset) +{ + const quint32 length = asUint32(data, offset); + if (length == 0) + return Botan::BigInt(); + const Botan::byte *numberStart + = reinterpret_cast(data.constData() + *offset); + *offset += length; + return Botan::BigInt::decode(numberStart, length); +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshpacketparser_p.h b/src/libs/ssh/sshpacketparser_p.h new file mode 100644 index 0000000000..775dc5e975 --- /dev/null +++ b/src/libs/ssh/sshpacketparser_p.h @@ -0,0 +1,84 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHPACKETPARSER_P_H +#define SSHPACKETPARSER_P_H + +#include + +#include +#include +#include + +namespace QSsh { +namespace Internal { + +struct SshNameList +{ + SshNameList() : originalLength(0) {} + SshNameList(quint32 originalLength) : originalLength(originalLength) {} + quint32 originalLength; + QList names; +}; + +class SshPacketParseException { }; + +// This class's functions try to read a byte array at a certain offset +// as the respective chunk of data as specified in the SSH RFCs. +// If they succeed, they update the offset, so they can easily +// be called in succession by client code. +// For convenience, some have also versions that don't update the offset, +// so they can be called with rvalues if the new value is not needed. +// If they fail, they throw an SshPacketParseException. +class SshPacketParser +{ +public: + static bool asBool(const QByteArray &data, quint32 offset); + static bool asBool(const QByteArray &data, quint32 *offset); + static quint16 asUint16(const QByteArray &data, quint32 offset); + static quint16 asUint16(const QByteArray &data, quint32 *offset); + static quint64 asUint64(const QByteArray &data, quint32 offset); + static quint64 asUint64(const QByteArray &data, quint32 *offset); + static quint32 asUint32(const QByteArray &data, quint32 offset); + static quint32 asUint32(const QByteArray &data, quint32 *offset); + static QByteArray asString(const QByteArray &data, quint32 *offset); + static QString asUserString(const QByteArray &data, quint32 *offset); + static SshNameList asNameList(const QByteArray &data, quint32 *offset); + static Botan::BigInt asBigInt(const QByteArray &data, quint32 *offset); + + static QString asUserString(const QByteArray &rawString); +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHPACKETPARSER_P_H diff --git a/src/libs/ssh/sshpseudoterminal.h b/src/libs/ssh/sshpseudoterminal.h new file mode 100644 index 0000000000..23875ef939 --- /dev/null +++ b/src/libs/ssh/sshpseudoterminal.h @@ -0,0 +1,120 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHPSEUDOTERMINAL_H +#define SSHPSEUDOTERMINAL_H + +#include "ssh_global.h" + +#include +#include + +namespace QSsh { + +class QSSH_EXPORT SshPseudoTerminal +{ + public: + explicit SshPseudoTerminal(const QByteArray &termType = "vt100", + int rowCount = 24, int columnCount = 80) + : termType(termType), rowCount(rowCount), columnCount(columnCount) {} + + QByteArray termType; + int rowCount; + int columnCount; + + enum Mode { + VINTR = 1, // Interrupt character. + VQUIT = 2, // The quit character (sends SIGQUIT signal on POSIX systems). + VERASE = 3, // Erase the character to left of the cursor. + VKILL = 4, // Kill the current input line. + VEOF = 5, // End-of-file character (sends EOF from the terminal). + VEOL = 6, // End-of-line character in addition to carriage return and/or linefeed. + VEOL2 = 7, // Additional end-of-line character. + VSTART = 8, // Continues paused output (normally control-Q). + VSTOP = 9, // Pauses output (normally control-S). + VSUSP = 10, // Suspends the current program. + VDSUSP = 11, // Another suspend character. + VREPRINT = 12, // Reprints the current input line. + VWERASE = 13, // Erases a word left of cursor. + VLNEXT = 14, // Enter the next character typed literally, even if it is a special character. + VFLUSH = 15, // Character to flush output. + VSWTCH = 16, // Switch to a different shell layer. + VSTATUS = 17, // Prints system status line (load, command, pid, etc). + VDISCARD = 18, // Toggles the flushing of terminal output. + + IGNPAR = 30, // The ignore parity flag. The parameter SHOULD be 0 if this flag is FALSE, and 1 if it is TRUE. + PARMRK = 31, // Mark parity and framing errors. + INPCK = 32, // Enable checking of parity errors. + ISTRIP = 33, // Strip 8th bit off characters. + INLCR = 34, // Map NL into CR on input. + IGNCR = 35, // Ignore CR on input. + ICRNL = 36, // Map CR to NL on input. + IUCLC = 37, // Translate uppercase characters to lowercase. + IXON = 38, // Enable output flow control. + IXANY = 39, // Any char will restart after stop. + IXOFF = 40, // Enable input flow control. + IMAXBEL = 41, // Ring bell on input queue full. + ISIG = 50, // Enable signals INTR, QUIT, [D]SUSP. + ICANON = 51, // Canonicalize input lines. + XCASE = 52, // Enable input and output of uppercase characters by preceding their lowercase equivalents with "\". + ECHO = 53, // Enable echoing. + ECHOE = 54, // Visually erase chars. + ECHOK = 55, // Kill character discards current line. + ECHONL = 56, // Echo NL even if ECHO is off. + NOFLSH = 57, // Don't flush after interrupt. + TOSTOP = 58, // Stop background jobs from output. + IEXTEN = 59, // Enable extensions. + ECHOCTL = 60, // Echo control characters as ^(Char). + ECHOKE = 61, // Visual erase for line kill. + PENDIN = 62, // Retype pending input. + OPOST = 70, // Enable output processing. + OLCUC = 71, // Convert lowercase to uppercase. + ONLCR = 72, // Map NL to CR-NL. + OCRNL = 73, // Translate carriage return to newline (output). + ONOCR = 74, // Translate newline to carriage return-newline (output). + ONLRET = 75, // Newline performs a carriage return (output). + CS7 = 90, // 7 bit mode. + CS8 = 91, // 8 bit mode. + PARENB = 92, // Parity enable. + PARODD = 93, // Odd parity, else even. + + TTY_OP_ISPEED = 128, // Specifies the input baud rate in bits per second. + TTY_OP_OSPEED = 129 // Specifies the output baud rate in bits per second. + }; + + typedef QHash ModeMap; + ModeMap modes; +}; + +} // namespace QSsh + +#endif // SSHPSEUDOTERMINAL_H diff --git a/src/libs/ssh/sshremoteprocess.cpp b/src/libs/ssh/sshremoteprocess.cpp new file mode 100644 index 0000000000..1bcba171fe --- /dev/null +++ b/src/libs/ssh/sshremoteprocess.cpp @@ -0,0 +1,384 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshremoteprocess.h" +#include "sshremoteprocess_p.h" + +#include "sshincomingpacket_p.h" +#include "sshsendfacility_p.h" + +#include + +#include + +#include +#include + +/*! + \class QSsh::SshRemoteProcess + + \brief This class implements an SSH channel for running a remote process. + + Objects are created via SshConnection::createRemoteProcess. + The process is started via the start() member function. + If the process needs a pseudo terminal, you can request one + via requestTerminal() before calling start(). + Note that this class does not support QIODevice's waitFor*() functions, i.e. it has + no synchronous mode. + */ + +namespace QSsh { + +const struct { + SshRemoteProcess::Signal signalEnum; + const char * const signalString; +} signalMap[] = { + { SshRemoteProcess::AbrtSignal, "ABRT" }, { SshRemoteProcess::AlrmSignal, "ALRM" }, + { SshRemoteProcess::FpeSignal, "FPE" }, { SshRemoteProcess::HupSignal, "HUP" }, + { SshRemoteProcess::IllSignal, "ILL" }, { SshRemoteProcess::IntSignal, "INT" }, + { SshRemoteProcess::KillSignal, "KILL" }, { SshRemoteProcess::PipeSignal, "PIPE" }, + { SshRemoteProcess::QuitSignal, "QUIT" }, { SshRemoteProcess::SegvSignal, "SEGV" }, + { SshRemoteProcess::TermSignal, "TERM" }, { SshRemoteProcess::Usr1Signal, "USR1" }, + { SshRemoteProcess::Usr2Signal, "USR2" } +}; + +SshRemoteProcess::SshRemoteProcess(const QByteArray &command, quint32 channelId, + Internal::SshSendFacility &sendFacility) + : d(new Internal::SshRemoteProcessPrivate(command, channelId, sendFacility, this)) +{ + init(); +} + +SshRemoteProcess::SshRemoteProcess(quint32 channelId, Internal::SshSendFacility &sendFacility) + : d(new Internal::SshRemoteProcessPrivate(channelId, sendFacility, this)) +{ + init(); +} + +SshRemoteProcess::~SshRemoteProcess() +{ + Q_ASSERT(d->channelState() == Internal::SshRemoteProcessPrivate::Inactive + || d->channelState() == Internal::SshRemoteProcessPrivate::CloseRequested + || d->channelState() == Internal::SshRemoteProcessPrivate::Closed); + delete d; +} + +bool SshRemoteProcess::atEnd() const +{ + return QIODevice::atEnd() && d->data().isEmpty(); +} + +qint64 SshRemoteProcess::bytesAvailable() const +{ + return QIODevice::bytesAvailable() + d->data().count(); +} + +bool SshRemoteProcess::canReadLine() const +{ + return QIODevice::canReadLine() || d->data().contains('\n'); +} + +QByteArray SshRemoteProcess::readAllStandardOutput() +{ + return readAllFromChannel(QProcess::StandardOutput); +} + +QByteArray SshRemoteProcess::readAllStandardError() +{ + return readAllFromChannel(QProcess::StandardError); +} + +QByteArray SshRemoteProcess::readAllFromChannel(QProcess::ProcessChannel channel) +{ + const QProcess::ProcessChannel currentReadChannel = readChannel(); + setReadChannel(channel); + const QByteArray &data = readAll(); + setReadChannel(currentReadChannel); + return data; +} + +void SshRemoteProcess::close() +{ + d->closeChannel(); + QIODevice::close(); +} + +qint64 SshRemoteProcess::readData(char *data, qint64 maxlen) +{ + const qint64 bytesRead = qMin(qint64(d->data().count()), maxlen); + memcpy(data, d->data().constData(), bytesRead); + d->data().remove(0, bytesRead); + return bytesRead; +} + +qint64 SshRemoteProcess::writeData(const char *data, qint64 len) +{ + if (isRunning()) { + d->sendData(QByteArray(data, len)); + return len; + } + return 0; +} + +QProcess::ProcessChannel SshRemoteProcess::readChannel() const +{ + return d->m_readChannel; +} + +void SshRemoteProcess::setReadChannel(QProcess::ProcessChannel channel) +{ + d->m_readChannel = channel; +} + +void SshRemoteProcess::init() +{ + connect(d, SIGNAL(started()), this, SIGNAL(started()), + Qt::QueuedConnection); + connect(d, SIGNAL(readyReadStandardOutput()), this, SIGNAL(readyReadStandardOutput()), + Qt::QueuedConnection); + connect(d, SIGNAL(readyRead()), this, SIGNAL(readyRead()), Qt::QueuedConnection); + connect(d, SIGNAL(readyReadStandardError()), this, + SIGNAL(readyReadStandardError()), Qt::QueuedConnection); + connect(d, SIGNAL(closed(int)), this, SIGNAL(closed(int)), Qt::QueuedConnection); +} + +void SshRemoteProcess::addToEnvironment(const QByteArray &var, const QByteArray &value) +{ + if (d->channelState() == Internal::SshRemoteProcessPrivate::Inactive) + d->m_env << qMakePair(var, value); // Cached locally and sent on start() +} + +void SshRemoteProcess::requestTerminal(const SshPseudoTerminal &terminal) +{ + QSSH_ASSERT_AND_RETURN(d->channelState() == Internal::SshRemoteProcessPrivate::Inactive); + d->m_useTerminal = true; + d->m_terminal = terminal; +} + +void SshRemoteProcess::start() +{ + if (d->channelState() == Internal::SshRemoteProcessPrivate::Inactive) { +#ifdef CREATOR_SSH_DEBUG + qDebug("process start requested, channel id = %u", d->localChannelId()); +#endif + QIODevice::open(QIODevice::ReadWrite); + d->requestSessionStart(); + } +} + +void SshRemoteProcess::sendSignal(Signal signal) +{ + try { + if (isRunning()) { + const char *signalString = 0; + for (size_t i = 0; i < sizeof signalMap/sizeof *signalMap && !signalString; ++i) { + if (signalMap[i].signalEnum == signal) + signalString = signalMap[i].signalString; + } + QSSH_ASSERT_AND_RETURN(signalString); + d->m_sendFacility.sendChannelSignalPacket(d->remoteChannel(), signalString); + } + } catch (Botan::Exception &e) { + setErrorString(QString::fromAscii(e.what())); + d->closeChannel(); + } +} + +bool SshRemoteProcess::isRunning() const +{ + return d->m_procState == Internal::SshRemoteProcessPrivate::Running; +} + +int SshRemoteProcess::exitCode() const { return d->m_exitCode; } + +SshRemoteProcess::Signal SshRemoteProcess::exitSignal() const +{ + return static_cast(d->m_signal); +} + +namespace Internal { + +SshRemoteProcessPrivate::SshRemoteProcessPrivate(const QByteArray &command, + quint32 channelId, SshSendFacility &sendFacility, SshRemoteProcess *proc) + : AbstractSshChannel(channelId, sendFacility), + m_command(command), + m_isShell(false), + m_useTerminal(false), + m_proc(proc) +{ + init(); +} + +SshRemoteProcessPrivate::SshRemoteProcessPrivate(quint32 channelId, SshSendFacility &sendFacility, + SshRemoteProcess *proc) + : AbstractSshChannel(channelId, sendFacility), + m_isShell(true), + m_useTerminal(true), + m_proc(proc) +{ + init(); +} + +void SshRemoteProcessPrivate::init() +{ + m_procState = NotYetStarted; + m_wasRunning = false; + m_exitCode = 0; + m_readChannel = QProcess::StandardOutput; + m_signal = SshRemoteProcess::NoSignal; +} + +void SshRemoteProcessPrivate::setProcState(ProcessState newState) +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("channel: old state = %d,new state = %d", m_procState, newState); +#endif + m_procState = newState; + if (newState == StartFailed) { + emit closed(SshRemoteProcess::FailedToStart); + } else if (newState == Running) { + m_wasRunning = true; + emit started(); + } +} + +QByteArray &SshRemoteProcessPrivate::data() +{ + return m_readChannel == QProcess::StandardOutput ? m_stdout : m_stderr; +} + +void SshRemoteProcessPrivate::closeHook() +{ + if (m_wasRunning) { + if (m_signal != SshRemoteProcess::NoSignal) + emit closed(SshRemoteProcess::KilledBySignal); + else + emit closed(SshRemoteProcess::ExitedNormally); + } +} + +void SshRemoteProcessPrivate::handleOpenSuccessInternal() +{ + foreach (const EnvVar &envVar, m_env) { + m_sendFacility.sendEnvPacket(remoteChannel(), envVar.first, + envVar.second); + } + + if (m_useTerminal) + m_sendFacility.sendPtyRequestPacket(remoteChannel(), m_terminal); + + if (m_isShell) + m_sendFacility.sendShellPacket(remoteChannel()); + else + m_sendFacility.sendExecPacket(remoteChannel(), m_command); + setProcState(ExecRequested); + m_timeoutTimer->start(ReplyTimeout); +} + +void SshRemoteProcessPrivate::handleOpenFailureInternal(const QString &reason) +{ + setProcState(StartFailed); + m_proc->setErrorString(reason); +} + +void SshRemoteProcessPrivate::handleChannelSuccess() +{ + if (m_procState != ExecRequested) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_MSG_CHANNEL_SUCCESS message."); + } + m_timeoutTimer->stop(); + setProcState(Running); +} + +void SshRemoteProcessPrivate::handleChannelFailure() +{ + if (m_procState != ExecRequested) { + throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, + "Unexpected SSH_MSG_CHANNEL_FAILURE message."); + } + m_timeoutTimer->stop(); + setProcState(StartFailed); + closeChannel(); +} + +void SshRemoteProcessPrivate::handleChannelDataInternal(const QByteArray &data) +{ + m_stdout += data; + emit readyReadStandardOutput(); + if (m_readChannel == QProcess::StandardOutput) + emit readyRead(); +} + +void SshRemoteProcessPrivate::handleChannelExtendedDataInternal(quint32 type, + const QByteArray &data) +{ + if (type != SSH_EXTENDED_DATA_STDERR) { + qWarning("Unknown extended data type %u", type); + } else { + m_stderr += data; + emit readyReadStandardError(); + if (m_readChannel == QProcess::StandardError) + emit readyRead(); + } +} + +void SshRemoteProcessPrivate::handleExitStatus(const SshChannelExitStatus &exitStatus) +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("Process exiting with exit code %d", exitStatus.exitStatus); +#endif + m_exitCode = exitStatus.exitStatus; + m_procState = Exited; +} + +void SshRemoteProcessPrivate::handleExitSignal(const SshChannelExitSignal &signal) +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("Exit due to signal %s", signal.signal.data()); +#endif + + for (size_t i = 0; i < sizeof signalMap/sizeof *signalMap; ++i) { + if (signalMap[i].signalString == signal.signal) { + m_signal = signalMap[i].signalEnum; + m_procState = Exited; + m_proc->setErrorString(tr("Process killed by signal")); + return; + } + } + + throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, "Invalid signal", + tr("Server sent invalid signal '%1'").arg(QString::fromUtf8(signal.signal))); +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshremoteprocess.h b/src/libs/ssh/sshremoteprocess.h new file mode 100644 index 0000000000..16c8f7a191 --- /dev/null +++ b/src/libs/ssh/sshremoteprocess.h @@ -0,0 +1,130 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHREMOTECOMMAND_H +#define SSHREMOTECOMMAND_H + +#include "ssh_global.h" + +#include +#include + +QT_BEGIN_NAMESPACE +class QByteArray; +QT_END_NAMESPACE + +namespace QSsh { +class SshPseudoTerminal; +namespace Internal { +class SshChannelManager; +class SshRemoteProcessPrivate; +class SshSendFacility; +} // namespace Internal + +// TODO: ProcessChannel +class QSSH_EXPORT SshRemoteProcess : public QIODevice +{ + Q_OBJECT + + friend class Internal::SshChannelManager; + friend class Internal::SshRemoteProcessPrivate; + +public: + typedef QSharedPointer Ptr; + enum ExitStatus { FailedToStart, KilledBySignal, ExitedNormally }; + enum Signal { + AbrtSignal, AlrmSignal, FpeSignal, HupSignal, IllSignal, IntSignal, KillSignal, PipeSignal, + QuitSignal, SegvSignal, TermSignal, Usr1Signal, Usr2Signal, NoSignal + }; + + ~SshRemoteProcess(); + + // QIODevice stuff + bool atEnd() const; + qint64 bytesAvailable() const; + bool canReadLine() const; + void close(); + bool isSequential() const { return true; } + + QProcess::ProcessChannel readChannel() const; + void setReadChannel(QProcess::ProcessChannel channel); + + /* + * Note that this is of limited value in practice, because servers are + * usually configured to ignore such requests for security reasons. + */ + void addToEnvironment(const QByteArray &var, const QByteArray &value); + + void requestTerminal(const SshPseudoTerminal &terminal); + void start(); + + bool isRunning() const; + int exitCode() const; + Signal exitSignal() const; + + QByteArray readAllStandardOutput(); + QByteArray readAllStandardError(); + + // Note: This is ignored by the OpenSSH server. + void sendSignal(Signal signal); + void kill() { sendSignal(KillSignal); } + +signals: + void started(); + + void readyReadStandardOutput(); + void readyReadStandardError(); + + /* + * Parameter is of type ExitStatus, but we use int because of + * signal/slot awkwardness (full namespace required). + */ + void closed(int exitStatus); + +private: + SshRemoteProcess(const QByteArray &command, quint32 channelId, + Internal::SshSendFacility &sendFacility); + SshRemoteProcess(quint32 channelId, Internal::SshSendFacility &sendFacility); + + // QIODevice stuff + qint64 readData(char *data, qint64 maxlen); + qint64 writeData(const char *data, qint64 len); + + void init(); + QByteArray readAllFromChannel(QProcess::ProcessChannel channel); + + Internal::SshRemoteProcessPrivate *d; +}; + +} // namespace QSsh + +#endif // SSHREMOTECOMMAND_H diff --git a/src/libs/ssh/sshremoteprocess_p.h b/src/libs/ssh/sshremoteprocess_p.h new file mode 100644 index 0000000000..1e64c307e2 --- /dev/null +++ b/src/libs/ssh/sshremoteprocess_p.h @@ -0,0 +1,114 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHREMOTEPROCESS_P_H +#define SSHREMOTEPROCESS_P_H + +#include "sshpseudoterminal.h" + +#include "sshchannel_p.h" + +#include +#include +#include + +namespace QSsh { +class SshRemoteProcess; + +namespace Internal { +class SshSendFacility; + +class SshRemoteProcessPrivate : public AbstractSshChannel +{ + Q_OBJECT + friend class QSsh::SshRemoteProcess; +public: + enum ProcessState { + NotYetStarted, ExecRequested, StartFailed, Running, Exited + }; + + virtual void handleChannelSuccess(); + virtual void handleChannelFailure(); + + virtual void closeHook(); + + QByteArray &data(); + +signals: + void started(); + void readyRead(); + void readyReadStandardOutput(); + void readyReadStandardError(); + void closed(int exitStatus); + +private: + SshRemoteProcessPrivate(const QByteArray &command, quint32 channelId, + SshSendFacility &sendFacility, SshRemoteProcess *proc); + SshRemoteProcessPrivate(quint32 channelId, SshSendFacility &sendFacility, + SshRemoteProcess *proc); + + virtual void handleOpenSuccessInternal(); + virtual void handleOpenFailureInternal(const QString &reason); + virtual void handleChannelDataInternal(const QByteArray &data); + virtual void handleChannelExtendedDataInternal(quint32 type, + const QByteArray &data); + virtual void handleExitStatus(const SshChannelExitStatus &exitStatus); + virtual void handleExitSignal(const SshChannelExitSignal &signal); + + void init(); + void setProcState(ProcessState newState); + + QProcess::ProcessChannel m_readChannel; + + ProcessState m_procState; + bool m_wasRunning; + int m_signal; + int m_exitCode; + + const QByteArray m_command; + const bool m_isShell; + + typedef QPair EnvVar; + QList m_env; + bool m_useTerminal; + SshPseudoTerminal m_terminal; + + QByteArray m_stdout; + QByteArray m_stderr; + + SshRemoteProcess *m_proc; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHREMOTEPROCESS_P_H diff --git a/src/libs/ssh/sshremoteprocessrunner.cpp b/src/libs/ssh/sshremoteprocessrunner.cpp new file mode 100644 index 0000000000..5af53eab4c --- /dev/null +++ b/src/libs/ssh/sshremoteprocessrunner.cpp @@ -0,0 +1,269 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshremoteprocessrunner.h" + +#include "sshconnectionmanager.h" +#include "sshpseudoterminal.h" + + +/*! + \class QSsh::SshRemoteProcessRunner + + \brief Convenience class for running a remote process over an SSH connection. +*/ + +namespace QSsh { +namespace Internal { +namespace { +enum State { Inactive, Connecting, Connected, ProcessRunning }; +} // anonymous namespace + +class SshRemoteProcessRunnerPrivate +{ +public: + SshRemoteProcessRunnerPrivate() : m_state(Inactive) {} + + SshRemoteProcess::Ptr m_process; + SshConnection::Ptr m_connection; + bool m_runInTerminal; + SshPseudoTerminal m_terminal; + QByteArray m_command; + QSsh::SshError m_lastConnectionError; + QString m_lastConnectionErrorString; + SshRemoteProcess::ExitStatus m_exitStatus; + SshRemoteProcess::Signal m_exitSignal; + int m_exitCode; + QString m_processErrorString; + State m_state; +}; + +} // namespace Internal + +using namespace Internal; + +SshRemoteProcessRunner::SshRemoteProcessRunner(QObject *parent) + : QObject(parent), d(new SshRemoteProcessRunnerPrivate) +{ +} + +SshRemoteProcessRunner::~SshRemoteProcessRunner() +{ + disconnect(); + setState(Inactive); + delete d; +} + +void SshRemoteProcessRunner::run(const QByteArray &command, + const SshConnectionParameters &sshParams) +{ + QSSH_ASSERT_AND_RETURN(d->m_state == Inactive); + + d->m_runInTerminal = false; + runInternal(command, sshParams); +} + +void SshRemoteProcessRunner::runInTerminal(const QByteArray &command, + const SshPseudoTerminal &terminal, const SshConnectionParameters &sshParams) +{ + d->m_terminal = terminal; + d->m_runInTerminal = true; + runInternal(command, sshParams); +} + +void SshRemoteProcessRunner::runInternal(const QByteArray &command, + const SshConnectionParameters &sshParams) +{ + setState(Connecting); + + d->m_lastConnectionError = SshNoError; + d->m_lastConnectionErrorString.clear(); + d->m_processErrorString.clear(); + d->m_exitSignal = SshRemoteProcess::NoSignal; + d->m_exitCode = -1; + d->m_command = command; + d->m_connection = SshConnectionManager::instance().acquireConnection(sshParams); + connect(d->m_connection.data(), SIGNAL(error(QSsh::SshError)), + SLOT(handleConnectionError(QSsh::SshError))); + connect(d->m_connection.data(), SIGNAL(disconnected()), SLOT(handleDisconnected())); + if (d->m_connection->state() == SshConnection::Connected) { + handleConnected(); + } else { + connect(d->m_connection.data(), SIGNAL(connected()), SLOT(handleConnected())); + if (d->m_connection->state() == SshConnection::Unconnected) + d->m_connection->connectToHost(); + } +} + +void SshRemoteProcessRunner::handleConnected() +{ + QSSH_ASSERT_AND_RETURN(d->m_state == Connecting); + setState(Connected); + + d->m_process = d->m_connection->createRemoteProcess(d->m_command); + connect(d->m_process.data(), SIGNAL(started()), SLOT(handleProcessStarted())); + connect(d->m_process.data(), SIGNAL(closed(int)), SLOT(handleProcessFinished(int))); + connect(d->m_process.data(), SIGNAL(readyReadStandardOutput()), SLOT(handleStdout())); + connect(d->m_process.data(), SIGNAL(readyReadStandardError()), SLOT(handleStderr())); + if (d->m_runInTerminal) + d->m_process->requestTerminal(d->m_terminal); + d->m_process->start(); +} + +void SshRemoteProcessRunner::handleConnectionError(QSsh::SshError error) +{ + d->m_lastConnectionError = error; + d->m_lastConnectionErrorString = d->m_connection->errorString(); + handleDisconnected(); + emit connectionError(); +} + +void SshRemoteProcessRunner::handleDisconnected() +{ + QSSH_ASSERT_AND_RETURN(d->m_state == Connecting || d->m_state == Connected + || d->m_state == ProcessRunning); + setState(Inactive); +} + +void SshRemoteProcessRunner::handleProcessStarted() +{ + QSSH_ASSERT_AND_RETURN(d->m_state == Connected); + + setState(ProcessRunning); + emit processStarted(); +} + +void SshRemoteProcessRunner::handleProcessFinished(int exitStatus) +{ + d->m_exitStatus = static_cast(exitStatus); + switch (d->m_exitStatus) { + case SshRemoteProcess::FailedToStart: + QSSH_ASSERT_AND_RETURN(d->m_state == Connected); + break; + case SshRemoteProcess::KilledBySignal: + QSSH_ASSERT_AND_RETURN(d->m_state == ProcessRunning); + d->m_exitSignal = d->m_process->exitSignal(); + break; + case SshRemoteProcess::ExitedNormally: + QSSH_ASSERT_AND_RETURN(d->m_state == ProcessRunning); + d->m_exitCode = d->m_process->exitCode(); + break; + default: + Q_ASSERT_X(false, Q_FUNC_INFO, "Impossible exit status."); + } + d->m_processErrorString = d->m_process->errorString(); + setState(Inactive); + emit processClosed(exitStatus); +} + +void SshRemoteProcessRunner::handleStdout() +{ + emit processOutputAvailable(d->m_process->readAllStandardOutput()); +} + +void SshRemoteProcessRunner::handleStderr() +{ + emit processErrorOutputAvailable(d->m_process->readAllStandardError()); +} + +void SshRemoteProcessRunner::setState(int newState) +{ + if (d->m_state == newState) + return; + + d->m_state = static_cast(newState); + if (d->m_state == Inactive) { + if (d->m_process) { + disconnect(d->m_process.data(), 0, this, 0); + d->m_process->close(); + d->m_process.clear(); + } + if (d->m_connection) { + disconnect(d->m_connection.data(), 0, this, 0); + SshConnectionManager::instance().releaseConnection(d->m_connection); + d->m_connection.clear(); + } + } +} + +QByteArray SshRemoteProcessRunner::command() const { return d->m_command; } +SshError SshRemoteProcessRunner::lastConnectionError() const { return d->m_lastConnectionError; } +QString SshRemoteProcessRunner::lastConnectionErrorString() const { + return d->m_lastConnectionErrorString; +} + +bool SshRemoteProcessRunner::isProcessRunning() const +{ + return d->m_process && d->m_process->isRunning(); +} + +SshRemoteProcess::ExitStatus SshRemoteProcessRunner::processExitStatus() const +{ + QSSH_ASSERT(!isProcessRunning()); + return d->m_exitStatus; +} + +SshRemoteProcess::Signal SshRemoteProcessRunner::processExitSignal() const +{ + QSSH_ASSERT(processExitStatus() == SshRemoteProcess::KilledBySignal); + return d->m_exitSignal; +} + +int SshRemoteProcessRunner::processExitCode() const +{ + QSSH_ASSERT(processExitStatus() == SshRemoteProcess::ExitedNormally); + return d->m_exitCode; +} + +QString SshRemoteProcessRunner::processErrorString() const +{ + return d->m_processErrorString; +} + +void SshRemoteProcessRunner::writeDataToProcess(const QByteArray &data) +{ + QSSH_ASSERT(isProcessRunning()); + d->m_process->write(data); +} + +void SshRemoteProcessRunner::sendSignalToProcess(SshRemoteProcess::Signal signal) +{ + QSSH_ASSERT(isProcessRunning()); + d->m_process->sendSignal(signal); +} + +void SshRemoteProcessRunner::cancel() +{ + setState(Inactive); +} + +} // namespace QSsh diff --git a/src/libs/ssh/sshremoteprocessrunner.h b/src/libs/ssh/sshremoteprocessrunner.h new file mode 100644 index 0000000000..57d755bc0f --- /dev/null +++ b/src/libs/ssh/sshremoteprocessrunner.h @@ -0,0 +1,94 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHREMOTEPROCESSRUNNER_H +#define SSHREMOTEPROCESSRUNNER_H + +#include "sshconnection.h" +#include "sshremoteprocess.h" + +namespace QSsh { +namespace Internal { +class SshRemoteProcessRunnerPrivate; +} // namespace Internal + +class QSSH_EXPORT SshRemoteProcessRunner : public QObject +{ + Q_OBJECT + +public: + SshRemoteProcessRunner(QObject *parent = 0); + ~SshRemoteProcessRunner(); + + void run(const QByteArray &command, const SshConnectionParameters &sshParams); + void runInTerminal(const QByteArray &command, const SshPseudoTerminal &terminal, + const SshConnectionParameters &sshParams); + QByteArray command() const; + + QSsh::SshError lastConnectionError() const; + QString lastConnectionErrorString() const; + + bool isProcessRunning() const; + void writeDataToProcess(const QByteArray &data); + void sendSignalToProcess(SshRemoteProcess::Signal signal); // No effect with OpenSSH server. + void cancel(); // Does not stop remote process, just frees SSH-related process resources. + SshRemoteProcess::ExitStatus processExitStatus() const; + SshRemoteProcess::Signal processExitSignal() const; + int processExitCode() const; + QString processErrorString() const; + +private slots: + void handleConnected(); + void handleConnectionError(QSsh::SshError error); + void handleDisconnected(); + void handleProcessStarted(); + void handleProcessFinished(int exitStatus); + void handleStdout(); + void handleStderr(); + +signals: + void connectionError(); + void processStarted(); + void processOutputAvailable(const QByteArray &output); + void processErrorOutputAvailable(const QByteArray &output); + void processClosed(int exitStatus); // values are of type SshRemoteProcess::ExitStatus + +private: + void runInternal(const QByteArray &command, const QSsh::SshConnectionParameters &sshParams); + void setState(int newState); + + Internal::SshRemoteProcessRunnerPrivate * const d; +}; + +} // namespace QSsh + +#endif // SSHREMOTEPROCESSRUNNER_H diff --git a/src/libs/ssh/sshsendfacility.cpp b/src/libs/ssh/sshsendfacility.cpp new file mode 100644 index 0000000000..2797dce1df --- /dev/null +++ b/src/libs/ssh/sshsendfacility.cpp @@ -0,0 +1,222 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#include "sshsendfacility_p.h" + +#include "sshkeyexchange_p.h" +#include "sshoutgoingpacket_p.h" + +#include + +namespace QSsh { +namespace Internal { + +SshSendFacility::SshSendFacility(QTcpSocket *socket) + : m_clientSeqNr(0), m_socket(socket), + m_outgoingPacket(m_encrypter, m_clientSeqNr) +{ +} + +void SshSendFacility::sendPacket() +{ +#ifdef CREATOR_SSH_DEBUG + qDebug("Sending packet, client seq nr is %u", m_clientSeqNr); +#endif + if (m_socket->isValid() + && m_socket->state() == QAbstractSocket::ConnectedState) { + m_socket->write(m_outgoingPacket.rawData()); + ++m_clientSeqNr; + } +} + +void SshSendFacility::reset() +{ + m_clientSeqNr = 0; + m_encrypter.clearKeys(); +} + +void SshSendFacility::recreateKeys(const SshKeyExchange &keyExchange) +{ + m_encrypter.recreateKeys(keyExchange); +} + +void SshSendFacility::createAuthenticationKey(const QByteArray &privKeyFileContents) +{ + m_encrypter.createAuthenticationKey(privKeyFileContents); +} + +QByteArray SshSendFacility::sendKeyExchangeInitPacket() +{ + const QByteArray &payLoad = m_outgoingPacket.generateKeyExchangeInitPacket(); + sendPacket(); + return payLoad; +} + +void SshSendFacility::sendKeyDhInitPacket(const Botan::BigInt &e) +{ + m_outgoingPacket.generateKeyDhInitPacket(e); + sendPacket(); +} + +void SshSendFacility::sendNewKeysPacket() +{ + m_outgoingPacket.generateNewKeysPacket(); + sendPacket(); +} + +void SshSendFacility::sendDisconnectPacket(SshErrorCode reason, + const QByteArray &reasonString) +{ + m_outgoingPacket.generateDisconnectPacket(reason, reasonString); + sendPacket(); + } + +void SshSendFacility::sendMsgUnimplementedPacket(quint32 serverSeqNr) +{ + m_outgoingPacket.generateMsgUnimplementedPacket(serverSeqNr); + sendPacket(); +} + +void SshSendFacility::sendUserAuthServiceRequestPacket() +{ + m_outgoingPacket.generateUserAuthServiceRequestPacket(); + sendPacket(); +} + +void SshSendFacility::sendUserAuthByPwdRequestPacket(const QByteArray &user, + const QByteArray &service, const QByteArray &pwd) +{ + m_outgoingPacket.generateUserAuthByPwdRequestPacket(user, service, pwd); + sendPacket(); + } + +void SshSendFacility::sendUserAuthByKeyRequestPacket(const QByteArray &user, + const QByteArray &service) +{ + m_outgoingPacket.generateUserAuthByKeyRequestPacket(user, service); + sendPacket(); +} + +void SshSendFacility::sendRequestFailurePacket() +{ + m_outgoingPacket.generateRequestFailurePacket(); + sendPacket(); +} + +void SshSendFacility::sendIgnorePacket() +{ + m_outgoingPacket.generateIgnorePacket(); + sendPacket(); +} + +void SshSendFacility::sendInvalidPacket() +{ + m_outgoingPacket.generateInvalidMessagePacket(); + sendPacket(); +} + +void SshSendFacility::sendSessionPacket(quint32 channelId, quint32 windowSize, + quint32 maxPacketSize) +{ + m_outgoingPacket.generateSessionPacket(channelId, windowSize, + maxPacketSize); + sendPacket(); +} + +void SshSendFacility::sendPtyRequestPacket(quint32 remoteChannel, + const SshPseudoTerminal &terminal) +{ + m_outgoingPacket.generatePtyRequestPacket(remoteChannel, terminal); + sendPacket(); +} + +void SshSendFacility::sendEnvPacket(quint32 remoteChannel, + const QByteArray &var, const QByteArray &value) +{ + m_outgoingPacket.generateEnvPacket(remoteChannel, var, value); + sendPacket(); +} + +void SshSendFacility::sendExecPacket(quint32 remoteChannel, + const QByteArray &command) +{ + m_outgoingPacket.generateExecPacket(remoteChannel, command); + sendPacket(); +} + +void SshSendFacility::sendShellPacket(quint32 remoteChannel) +{ + m_outgoingPacket.generateShellPacket(remoteChannel); + sendPacket(); +} + +void SshSendFacility::sendSftpPacket(quint32 remoteChannel) +{ + m_outgoingPacket.generateSftpPacket(remoteChannel); + sendPacket(); +} + +void SshSendFacility::sendWindowAdjustPacket(quint32 remoteChannel, + quint32 bytesToAdd) +{ + m_outgoingPacket.generateWindowAdjustPacket(remoteChannel, bytesToAdd); + sendPacket(); +} + +void SshSendFacility::sendChannelDataPacket(quint32 remoteChannel, + const QByteArray &data) +{ + m_outgoingPacket.generateChannelDataPacket(remoteChannel, data); + sendPacket(); +} + +void SshSendFacility::sendChannelSignalPacket(quint32 remoteChannel, + const QByteArray &signalName) +{ + m_outgoingPacket.generateChannelSignalPacket(remoteChannel, signalName); + sendPacket(); +} + +void SshSendFacility::sendChannelEofPacket(quint32 remoteChannel) +{ + m_outgoingPacket.generateChannelEofPacket(remoteChannel); + sendPacket(); +} + +void SshSendFacility::sendChannelClosePacket(quint32 remoteChannel) +{ + m_outgoingPacket.generateChannelClosePacket(remoteChannel); + sendPacket(); +} + +} // namespace Internal +} // namespace QSsh diff --git a/src/libs/ssh/sshsendfacility_p.h b/src/libs/ssh/sshsendfacility_p.h new file mode 100644 index 0000000000..2801d2a454 --- /dev/null +++ b/src/libs/ssh/sshsendfacility_p.h @@ -0,0 +1,101 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** +** GNU Lesser General Public License Usage +** +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this file. +** Please review the following information to ensure the GNU Lesser General +** Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** Other Usage +** +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +**************************************************************************/ + +#ifndef SSHCONNECTIONOUTSTATE_P_H +#define SSHCONNECTIONOUTSTATE_P_H + +#include "sshcryptofacility_p.h" +#include "sshoutgoingpacket_p.h" + +QT_BEGIN_NAMESPACE +class QTcpSocket; +QT_END_NAMESPACE + + +namespace QSsh { +class SshPseudoTerminal; + +namespace Internal { +class SshKeyExchange; + +class SshSendFacility +{ +public: + SshSendFacility(QTcpSocket *socket); + void reset(); + void recreateKeys(const SshKeyExchange &keyExchange); + void createAuthenticationKey(const QByteArray &privKeyFileContents); + + QByteArray sendKeyExchangeInitPacket(); + void sendKeyDhInitPacket(const Botan::BigInt &e); + void sendNewKeysPacket(); + void sendDisconnectPacket(SshErrorCode reason, + const QByteArray &reasonString); + void sendMsgUnimplementedPacket(quint32 serverSeqNr); + void sendUserAuthServiceRequestPacket(); + void sendUserAuthByPwdRequestPacket(const QByteArray &user, + const QByteArray &service, const QByteArray &pwd); + void sendUserAuthByKeyRequestPacket(const QByteArray &user, + const QByteArray &service); + void sendRequestFailurePacket(); + void sendIgnorePacket(); + void sendInvalidPacket(); + void sendSessionPacket(quint32 channelId, quint32 windowSize, + quint32 maxPacketSize); + void sendPtyRequestPacket(quint32 remoteChannel, + const SshPseudoTerminal &terminal); + void sendEnvPacket(quint32 remoteChannel, const QByteArray &var, + const QByteArray &value); + void sendExecPacket(quint32 remoteChannel, const QByteArray &command); + void sendShellPacket(quint32 remoteChannel); + void sendSftpPacket(quint32 remoteChannel); + void sendWindowAdjustPacket(quint32 remoteChannel, quint32 bytesToAdd); + void sendChannelDataPacket(quint32 remoteChannel, const QByteArray &data); + void sendChannelSignalPacket(quint32 remoteChannel, + const QByteArray &signalName); + void sendChannelEofPacket(quint32 remoteChannel); + void sendChannelClosePacket(quint32 remoteChannel); + quint32 nextClientSeqNr() const { return m_clientSeqNr; } + +private: + void sendPacket(); + + quint32 m_clientSeqNr; + SshEncryptionFacility m_encrypter; + QTcpSocket *m_socket; + SshOutgoingPacket m_outgoingPacket; +}; + +} // namespace Internal +} // namespace QSsh + +#endif // SSHCONNECTIONOUTSTATE_P_H diff --git a/src/libs/utils/ssh/sftpchannel.cpp b/src/libs/utils/ssh/sftpchannel.cpp deleted file mode 100644 index 585265745d..0000000000 --- a/src/libs/utils/ssh/sftpchannel.cpp +++ /dev/null @@ -1,980 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sftpchannel.h" -#include "sftpchannel_p.h" - -#include "sshexception_p.h" -#include "sshincomingpacket_p.h" -#include "sshsendfacility_p.h" - -#include -#include - -/*! - \class Utils::SftpChannel - - \brief This class provides SFTP operations. - - Objects are created via SshConnection::createSftpChannel(). - The channel needs to be initialized with - a call to initialize() and is closed via closeChannel(). After closing - a channel, no more operations are possible. It cannot be re-opened - using initialize(); use SshConnection::createSftpChannel() if you need - a new one. - - After the initialized() signal has been emitted, operations can be started. - All SFTP operations are asynchronous (non-blocking) and can be in-flight - simultaneously (though callers must ensure that concurrently running jobs - are independent of each other, e.g. they must not write to the same file). - Operations are identified by their job id, which is returned by - the respective member function. If the function can right away detect that - the operation cannot succeed, it returns SftpInvalidJob. If an error occurs - later, the finished() signal is emitted for the respective job with a - non-empty error string. - - Note that directory names must not have a trailing slash. -*/ - -namespace Utils { -namespace Internal { -namespace { - const quint32 ProtocolVersion = 3; - - QString errorMessage(const QString &serverMessage, - const QString &alternativeMessage) - { - return serverMessage.isEmpty() ? alternativeMessage : serverMessage; - } - - QString errorMessage(const SftpStatusResponse &response, - const QString &alternativeMessage) - { - return response.status == SSH_FX_OK ? QString() - : errorMessage(response.errorString, alternativeMessage); - } -} // anonymous namespace -} // namespace Internal - -SftpChannel::SftpChannel(quint32 channelId, - Internal::SshSendFacility &sendFacility) - : d(new Internal::SftpChannelPrivate(channelId, sendFacility, this)) -{ - connect(d, SIGNAL(initialized()), this, SIGNAL(initialized()), - Qt::QueuedConnection); - connect(d, SIGNAL(initializationFailed(QString)), this, - SIGNAL(initializationFailed(QString)), Qt::QueuedConnection); - connect(d, SIGNAL(dataAvailable(Utils::SftpJobId,QString)), this, - SIGNAL(dataAvailable(Utils::SftpJobId,QString)), Qt::QueuedConnection); - connect(d, SIGNAL(fileInfoAvailable(Utils::SftpJobId,QList)), this, - SIGNAL(fileInfoAvailable(Utils::SftpJobId,QList)), - Qt::QueuedConnection); - connect(d, SIGNAL(finished(Utils::SftpJobId,QString)), this, - SIGNAL(finished(Utils::SftpJobId,QString)), Qt::QueuedConnection); - connect(d, SIGNAL(closed()), this, SIGNAL(closed()), Qt::QueuedConnection); -} - -SftpChannel::State SftpChannel::state() const -{ - switch (d->channelState()) { - case Internal::AbstractSshChannel::Inactive: - return Uninitialized; - case Internal::AbstractSshChannel::SessionRequested: - return Initializing; - case Internal::AbstractSshChannel::CloseRequested: - return Closing; - case Internal::AbstractSshChannel::Closed: - return Closed; - case Internal::AbstractSshChannel::SessionEstablished: - return d->m_sftpState == Internal::SftpChannelPrivate::Initialized - ? Initialized : Initializing; - default: - Q_ASSERT(!"Oh no, we forgot to handle a channel state!"); - return Closed; // For the compiler. - } -} - -void SftpChannel::initialize() -{ - d->requestSessionStart(); - d->m_sftpState = Internal::SftpChannelPrivate::SubsystemRequested; -} - -void SftpChannel::closeChannel() -{ - d->closeChannel(); -} - -SftpJobId SftpChannel::statFile(const QString &path) -{ - return d->createJob(Internal::SftpStatFile::Ptr( - new Internal::SftpStatFile(++d->m_nextJobId, path))); -} - -SftpJobId SftpChannel::listDirectory(const QString &path) -{ - return d->createJob(Internal::SftpListDir::Ptr( - new Internal::SftpListDir(++d->m_nextJobId, path))); -} - -SftpJobId SftpChannel::createDirectory(const QString &path) -{ - return d->createJob(Internal::SftpMakeDir::Ptr( - new Internal::SftpMakeDir(++d->m_nextJobId, path))); -} - -SftpJobId SftpChannel::removeDirectory(const QString &path) -{ - return d->createJob(Internal::SftpRmDir::Ptr( - new Internal::SftpRmDir(++d->m_nextJobId, path))); -} - -SftpJobId SftpChannel::removeFile(const QString &path) -{ - return d->createJob(Internal::SftpRm::Ptr( - new Internal::SftpRm(++d->m_nextJobId, path))); -} - -SftpJobId SftpChannel::renameFileOrDirectory(const QString &oldPath, - const QString &newPath) -{ - return d->createJob(Internal::SftpRename::Ptr( - new Internal::SftpRename(++d->m_nextJobId, oldPath, newPath))); -} - -SftpJobId SftpChannel::createLink(const QString &filePath, const QString &target) -{ - return d->createJob(Internal::SftpCreateLink::Ptr( - new Internal::SftpCreateLink(++d->m_nextJobId, filePath, target))); -} - -SftpJobId SftpChannel::createFile(const QString &path, SftpOverwriteMode mode) -{ - return d->createJob(Internal::SftpCreateFile::Ptr( - new Internal::SftpCreateFile(++d->m_nextJobId, path, mode))); -} - -SftpJobId SftpChannel::uploadFile(const QString &localFilePath, - const QString &remoteFilePath, SftpOverwriteMode mode) -{ - QSharedPointer localFile(new QFile(localFilePath)); - if (!localFile->open(QIODevice::ReadOnly)) - return SftpInvalidJob; - return d->createJob(Internal::SftpUploadFile::Ptr( - new Internal::SftpUploadFile(++d->m_nextJobId, remoteFilePath, localFile, mode))); -} - -SftpJobId SftpChannel::downloadFile(const QString &remoteFilePath, - const QString &localFilePath, SftpOverwriteMode mode) -{ - QSharedPointer localFile(new QFile(localFilePath)); - if (mode == SftpSkipExisting && localFile->exists()) - return SftpInvalidJob; - QIODevice::OpenMode openMode = QIODevice::WriteOnly; - if (mode == SftpOverwriteExisting) - openMode |= QIODevice::Truncate; - else if (mode == SftpAppendToExisting) - openMode |= QIODevice::Append; - if (!localFile->open(openMode)) - return SftpInvalidJob; - return d->createJob(Internal::SftpDownload::Ptr( - new Internal::SftpDownload(++d->m_nextJobId, remoteFilePath, localFile))); -} - -SftpJobId SftpChannel::uploadDir(const QString &localDirPath, - const QString &remoteParentDirPath) -{ - if (state() != Initialized) - return SftpInvalidJob; - const QDir localDir(localDirPath); - if (!localDir.exists() || !localDir.isReadable()) - return SftpInvalidJob; - const Internal::SftpUploadDir::Ptr uploadDirOp( - new Internal::SftpUploadDir(++d->m_nextJobId)); - const QString remoteDirPath - = remoteParentDirPath + QLatin1Char('/') + localDir.dirName(); - const Internal::SftpMakeDir::Ptr mkdirOp( - new Internal::SftpMakeDir(++d->m_nextJobId, remoteDirPath, uploadDirOp)); - uploadDirOp->mkdirsInProgress.insert(mkdirOp, - Internal::SftpUploadDir::Dir(localDirPath, remoteDirPath)); - d->createJob(mkdirOp); - return uploadDirOp->jobId; -} - -SftpChannel::~SftpChannel() -{ - delete d; -} - - -namespace Internal { - -SftpChannelPrivate::SftpChannelPrivate(quint32 channelId, - SshSendFacility &sendFacility, SftpChannel *sftp) - : AbstractSshChannel(channelId, sendFacility), - m_nextJobId(0), m_sftpState(Inactive), m_sftp(sftp) -{ -} - -SftpJobId SftpChannelPrivate::createJob(const AbstractSftpOperation::Ptr &job) -{ - if (m_sftp->state() != SftpChannel::Initialized) - return SftpInvalidJob; - m_jobs.insert(job->jobId, job); - sendData(job->initialPacket(m_outgoingPacket).rawData()); - return job->jobId; -} - -void SftpChannelPrivate::handleChannelSuccess() -{ - if (channelState() == CloseRequested) - return; -#ifdef CREATOR_SSH_DEBUG - qDebug("sftp subsystem initialized"); -#endif - sendData(m_outgoingPacket.generateInit(ProtocolVersion).rawData()); - m_sftpState = InitSent; -} - -void SftpChannelPrivate::handleChannelFailure() -{ - if (channelState() == CloseRequested) - return; - - if (m_sftpState != SubsystemRequested) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_MSG_CHANNEL_FAILURE packet."); - } - emit initializationFailed(tr("Server could not start sftp subsystem.")); - closeChannel(); -} - -void SftpChannelPrivate::handleChannelDataInternal(const QByteArray &data) -{ - if (channelState() == CloseRequested) - return; - - m_incomingData += data; - m_incomingPacket.consumeData(m_incomingData); - while (m_incomingPacket.isComplete()) { - handleCurrentPacket(); - m_incomingPacket.clear(); - m_incomingPacket.consumeData(m_incomingData); - } -} - -void SftpChannelPrivate::handleChannelExtendedDataInternal(quint32 type, - const QByteArray &data) -{ - qWarning("Unexpected extended data '%s' of type %d on SFTP channel.", - data.data(), type); -} - -void SftpChannelPrivate::handleExitStatus(const SshChannelExitStatus &exitStatus) -{ - const char * const message = "Remote SFTP service exited with exit code %d"; -#ifdef CREATOR_SSH_DEBUG - qDebug(message, exitStatus.exitStatus); -#else - if (exitStatus.exitStatus != 0) - qWarning(message, exitStatus.exitStatus); -#endif -} - -void SftpChannelPrivate::handleExitSignal(const SshChannelExitSignal &signal) -{ - qWarning("Remote SFTP service killed; signal was %s", signal.signal.data()); -} - -void SftpChannelPrivate::handleCurrentPacket() -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("Handling SFTP packet of type %d", m_incomingPacket.type()); -#endif - switch (m_incomingPacket.type()) { - case SSH_FXP_VERSION: - handleServerVersion(); - break; - case SSH_FXP_HANDLE: - handleHandle(); - break; - case SSH_FXP_NAME: - handleName(); - break; - case SSH_FXP_STATUS: - handleStatus(); - break; - case SSH_FXP_DATA: - handleReadData(); - break; - case SSH_FXP_ATTRS: - handleAttrs(); - break; - default: - throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected packet.", - tr("Unexpected packet of type %1.").arg(m_incomingPacket.type())); - } -} - -void SftpChannelPrivate::handleServerVersion() -{ - checkChannelActive(); - if (m_sftpState != InitSent) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_FXP_VERSION packet."); - } - -#ifdef CREATOR_SSH_DEBUG - qDebug("sftp init received"); -#endif - const quint32 serverVersion = m_incomingPacket.extractServerVersion(); - if (serverVersion != ProtocolVersion) { - emit initializationFailed(tr("Protocol version mismatch: Expected %1, got %2") - .arg(serverVersion).arg(ProtocolVersion)); - closeChannel(); - } else { - m_sftpState = Initialized; - emit initialized(); - } -} - -void SftpChannelPrivate::handleHandle() -{ - const SftpHandleResponse &response = m_incomingPacket.asHandleResponse(); - JobMap::Iterator it = lookupJob(response.requestId); - const QSharedPointer job - = it.value().dynamicCast(); - if (job.isNull()) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_FXP_HANDLE packet."); - } - if (job->state != AbstractSftpOperationWithHandle::OpenRequested) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_FXP_HANDLE packet."); - } - job->remoteHandle = response.handle; - job->state = AbstractSftpOperationWithHandle::Open; - - switch (it.value()->type()) { - case AbstractSftpOperation::ListDir: - handleLsHandle(it); - break; - case AbstractSftpOperation::CreateFile: - handleCreateFileHandle(it); - break; - case AbstractSftpOperation::Download: - handleGetHandle(it); - break; - case AbstractSftpOperation::UploadFile: - handlePutHandle(it); - break; - default: - Q_ASSERT(!"Oh no, I forgot to handle an SFTP operation type!"); - } -} - -void SftpChannelPrivate::handleLsHandle(const JobMap::Iterator &it) -{ - SftpListDir::Ptr op = it.value().staticCast(); - sendData(m_outgoingPacket.generateReadDir(op->remoteHandle, - op->jobId).rawData()); -} - -void SftpChannelPrivate::handleCreateFileHandle(const JobMap::Iterator &it) -{ - SftpCreateFile::Ptr op = it.value().staticCast(); - sendData(m_outgoingPacket.generateCloseHandle(op->remoteHandle, - op->jobId).rawData()); -} - -void SftpChannelPrivate::handleGetHandle(const JobMap::Iterator &it) -{ - SftpDownload::Ptr op = it.value().staticCast(); - sendData(m_outgoingPacket.generateFstat(op->remoteHandle, - op->jobId).rawData()); - op->statRequested = true; -} - -void SftpChannelPrivate::handlePutHandle(const JobMap::Iterator &it) -{ - SftpUploadFile::Ptr op = it.value().staticCast(); - if (op->parentJob && op->parentJob->hasError) - sendTransferCloseHandle(op, it.key()); - - // OpenSSH does not implement the RFC's append functionality, so we - // have to emulate it. - if (op->mode == SftpAppendToExisting) { - sendData(m_outgoingPacket.generateFstat(op->remoteHandle, - op->jobId).rawData()); - op->statRequested = true; - } else { - spawnWriteRequests(it); - } -} - -void SftpChannelPrivate::handleStatus() -{ - const SftpStatusResponse &response = m_incomingPacket.asStatusResponse(); -#ifdef CREATOR_SSH_DEBUG - qDebug("%s: status = %d", Q_FUNC_INFO, response.status); -#endif - JobMap::Iterator it = lookupJob(response.requestId); - switch (it.value()->type()) { - case AbstractSftpOperation::ListDir: - handleLsStatus(it, response); - break; - case AbstractSftpOperation::Download: - handleGetStatus(it, response); - break; - case AbstractSftpOperation::UploadFile: - handlePutStatus(it, response); - break; - case AbstractSftpOperation::MakeDir: - handleMkdirStatus(it, response); - break; - case AbstractSftpOperation::StatFile: - case AbstractSftpOperation::RmDir: - case AbstractSftpOperation::Rm: - case AbstractSftpOperation::Rename: - case AbstractSftpOperation::CreateFile: - case AbstractSftpOperation::CreateLink: - handleStatusGeneric(it, response); - break; - } -} - -void SftpChannelPrivate::handleStatusGeneric(const JobMap::Iterator &it, - const SftpStatusResponse &response) -{ - AbstractSftpOperation::Ptr op = it.value(); - const QString error = errorMessage(response, tr("Unknown error.")); - emit finished(op->jobId, error); - m_jobs.erase(it); -} - -void SftpChannelPrivate::handleMkdirStatus(const JobMap::Iterator &it, - const SftpStatusResponse &response) -{ - SftpMakeDir::Ptr op = it.value().staticCast(); - if (op->parentJob == SftpUploadDir::Ptr()) { - handleStatusGeneric(it, response); - return; - } - if (op->parentJob->hasError) { - m_jobs.erase(it); - return; - } - - typedef QMap::Iterator DirIt; - DirIt dirIt = op->parentJob->mkdirsInProgress.find(op); - Q_ASSERT(dirIt != op->parentJob->mkdirsInProgress.end()); - const QString &remoteDir = dirIt.value().remoteDir; - if (response.status == SSH_FX_OK) { - emit dataAvailable(op->parentJob->jobId, - tr("Created remote directory '%1'.").arg(remoteDir)); - } else if (response.status == SSH_FX_FAILURE) { - emit dataAvailable(op->parentJob->jobId, - tr("Remote directory '%1' already exists.").arg(remoteDir)); - } else { - op->parentJob->setError(); - emit finished(op->parentJob->jobId, - tr("Error creating directory '%1': %2") - .arg(remoteDir, response.errorString)); - m_jobs.erase(it); - return; - } - - QDir localDir(dirIt.value().localDir); - const QFileInfoList &dirInfos - = localDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); - foreach (const QFileInfo &dirInfo, dirInfos) { - const QString remoteSubDir = remoteDir + QLatin1Char('/') + dirInfo.fileName(); - const SftpMakeDir::Ptr mkdirOp( - new SftpMakeDir(++m_nextJobId, remoteSubDir, op->parentJob)); - op->parentJob->mkdirsInProgress.insert(mkdirOp, - SftpUploadDir::Dir(dirInfo.absoluteFilePath(), remoteSubDir)); - createJob(mkdirOp); - } - - const QFileInfoList &fileInfos = localDir.entryInfoList(QDir::Files); - foreach (const QFileInfo &fileInfo, fileInfos) { - QSharedPointer localFile(new QFile(fileInfo.absoluteFilePath())); - if (!localFile->open(QIODevice::ReadOnly)) { - op->parentJob->setError(); - emit finished(op->parentJob->jobId, - tr("Could not open local file '%1': %2") - .arg(fileInfo.absoluteFilePath(), localFile->errorString())); - m_jobs.erase(it); - return; - } - - const QString remoteFilePath = remoteDir + QLatin1Char('/') + fileInfo.fileName(); - SftpUploadFile::Ptr uploadFileOp(new SftpUploadFile(++m_nextJobId, - remoteFilePath, localFile, SftpOverwriteExisting, op->parentJob)); - createJob(uploadFileOp); - op->parentJob->uploadsInProgress.append(uploadFileOp); - } - - op->parentJob->mkdirsInProgress.erase(dirIt); - if (op->parentJob->mkdirsInProgress.isEmpty() - && op->parentJob->uploadsInProgress.isEmpty()) - emit finished(op->parentJob->jobId); - m_jobs.erase(it); -} - -void SftpChannelPrivate::handleLsStatus(const JobMap::Iterator &it, - const SftpStatusResponse &response) -{ - SftpListDir::Ptr op = it.value().staticCast(); - switch (op->state) { - case SftpListDir::OpenRequested: - emit finished(op->jobId, errorMessage(response.errorString, - tr("Remote directory could not be opened for reading."))); - m_jobs.erase(it); - break; - case SftpListDir::Open: - if (response.status != SSH_FX_EOF) - reportRequestError(op, errorMessage(response.errorString, - tr("Failed to list remote directory contents."))); - op->state = SftpListDir::CloseRequested; - sendData(m_outgoingPacket.generateCloseHandle(op->remoteHandle, - op->jobId).rawData()); - break; - case SftpListDir::CloseRequested: - if (!op->hasError) { - const QString error = errorMessage(response, - tr("Failed to close remote directory.")); - emit finished(op->jobId, error); - } - m_jobs.erase(it); - break; - default: - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_FXP_STATUS packet."); - } -} - -void SftpChannelPrivate::handleGetStatus(const JobMap::Iterator &it, - const SftpStatusResponse &response) -{ - SftpDownload::Ptr op = it.value().staticCast(); - switch (op->state) { - case SftpDownload::OpenRequested: - emit finished(op->jobId, - errorMessage(response.errorString, - tr("Failed to open remote file for reading."))); - m_jobs.erase(it); - break; - case SftpDownload::Open: - if (op->statRequested) { - reportRequestError(op, errorMessage(response.errorString, - tr("Failed retrieve information on the remote file ('stat' failed)."))); - sendTransferCloseHandle(op, response.requestId); - } else { - if ((response.status != SSH_FX_EOF || response.requestId != op->eofId) - && !op->hasError) - reportRequestError(op, errorMessage(response.errorString, - tr("Failed to read remote file."))); - finishTransferRequest(it); - } - break; - case SftpDownload::CloseRequested: - Q_ASSERT(op->inFlightCount == 1); - if (!op->hasError) { - if (response.status == SSH_FX_OK) - emit finished(op->jobId); - else - reportRequestError(op, errorMessage(response.errorString, - tr("Failed to close remote file."))); - } - removeTransferRequest(it); - break; - default: - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_FXP_STATUS packet."); - } -} - -void SftpChannelPrivate::handlePutStatus(const JobMap::Iterator &it, - const SftpStatusResponse &response) -{ - SftpUploadFile::Ptr job = it.value().staticCast(); - switch (job->state) { - case SftpUploadFile::OpenRequested: { - bool emitError = false; - if (job->parentJob) { - if (!job->parentJob->hasError) { - job->parentJob->setError(); - emitError = true; - } - } else { - emitError = true; - } - - if (emitError) { - emit finished(job->jobId, - errorMessage(response.errorString, - tr("Failed to open remote file for writing."))); - } - m_jobs.erase(it); - break; - } - case SftpUploadFile::Open: - if (job->hasError || (job->parentJob && job->parentJob->hasError)) { - job->hasError = true; - finishTransferRequest(it); - return; - } - - if (response.status == SSH_FX_OK) { - sendWriteRequest(it); - } else { - if (job->parentJob) - job->parentJob->setError(); - reportRequestError(job, errorMessage(response.errorString, - tr("Failed to write remote file."))); - finishTransferRequest(it); - } - break; - case SftpUploadFile::CloseRequested: - Q_ASSERT(job->inFlightCount == 1); - if (job->hasError || (job->parentJob && job->parentJob->hasError)) { - m_jobs.erase(it); - return; - } - - if (response.status == SSH_FX_OK) { - if (job->parentJob) { - job->parentJob->uploadsInProgress.removeOne(job); - if (job->parentJob->mkdirsInProgress.isEmpty() - && job->parentJob->uploadsInProgress.isEmpty()) - emit finished(job->parentJob->jobId); - } else { - emit finished(job->jobId); - } - } else { - const QString error = errorMessage(response.errorString, - tr("Failed to close remote file.")); - if (job->parentJob) { - job->parentJob->setError(); - emit finished(job->parentJob->jobId, error); - } else { - emit finished(job->jobId, error); - } - } - m_jobs.erase(it); - break; - default: - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_FXP_STATUS packet."); - } -} - -void SftpChannelPrivate::handleName() -{ - const SftpNameResponse &response = m_incomingPacket.asNameResponse(); - JobMap::Iterator it = lookupJob(response.requestId); - switch (it.value()->type()) { - case AbstractSftpOperation::ListDir: { - SftpListDir::Ptr op = it.value().staticCast(); - if (op->state != SftpListDir::Open) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_FXP_NAME packet."); - } - - QList fileInfoList; - for (int i = 0; i < response.files.count(); ++i) { - const SftpFile &file = response.files.at(i); - - SftpFileInfo fileInfo; - fileInfo.name = file.fileName; - attributesToFileInfo(file.attributes, fileInfo); - fileInfoList << fileInfo; - } - emit fileInfoAvailable(op->jobId, fileInfoList); - sendData(m_outgoingPacket.generateReadDir(op->remoteHandle, - op->jobId).rawData()); - break; - } - default: - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_FXP_NAME packet."); - } -} - -void SftpChannelPrivate::handleReadData() -{ - const SftpDataResponse &response = m_incomingPacket.asDataResponse(); - JobMap::Iterator it = lookupJob(response.requestId); - if (it.value()->type() != AbstractSftpOperation::Download) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_FXP_DATA packet."); - } - - SftpDownload::Ptr op = it.value().staticCast(); - if (op->hasError) { - finishTransferRequest(it); - return; - } - - if (!op->localFile->seek(op->offsets[response.requestId])) { - reportRequestError(op, op->localFile->errorString()); - finishTransferRequest(it); - return; - } - - if (op->localFile->write(response.data) != response.data.size()) { - reportRequestError(op, op->localFile->errorString()); - finishTransferRequest(it); - return; - } - - if (op->offset >= op->fileSize && op->fileSize != 0) - finishTransferRequest(it); - else - sendReadRequest(op, response.requestId); -} - -void SftpChannelPrivate::handleAttrs() -{ - const SftpAttrsResponse &response = m_incomingPacket.asAttrsResponse(); - JobMap::Iterator it = lookupJob(response.requestId); - - SftpStatFile::Ptr statOp = it.value().dynamicCast(); - if (statOp) { - SftpFileInfo fileInfo; - fileInfo.name = QFileInfo(statOp->path).fileName(); - attributesToFileInfo(response.attrs, fileInfo); - emit fileInfoAvailable(it.key(), QList() << fileInfo); - emit finished(it.key()); - m_jobs.erase(it); - return; - } - - AbstractSftpTransfer::Ptr transfer - = it.value().dynamicCast(); - if (!transfer || transfer->state != AbstractSftpTransfer::Open - || !transfer->statRequested) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_FXP_ATTRS packet."); - } - Q_ASSERT(transfer->type() == AbstractSftpOperation::UploadFile - || transfer->type() == AbstractSftpOperation::Download); - - if (transfer->type() == AbstractSftpOperation::Download) { - SftpDownload::Ptr op = transfer.staticCast(); - if (response.attrs.sizePresent) { - op->fileSize = response.attrs.size; - } else { - op->fileSize = 0; - op->eofId = op->jobId; - } - op->statRequested = false; - spawnReadRequests(op); - } else { - SftpUploadFile::Ptr op = transfer.staticCast(); - if (op->parentJob && op->parentJob->hasError) { - op->hasError = true; - sendTransferCloseHandle(op, op->jobId); - return; - } - - if (response.attrs.sizePresent) { - op->offset = response.attrs.size; - spawnWriteRequests(it); - } else { - if (op->parentJob) - op->parentJob->setError(); - reportRequestError(op, tr("Cannot append to remote file: " - "Server does not support the file size attribute.")); - sendTransferCloseHandle(op, op->jobId); - } - } -} - -SftpChannelPrivate::JobMap::Iterator SftpChannelPrivate::lookupJob(SftpJobId id) -{ - JobMap::Iterator it = m_jobs.find(id); - if (it == m_jobs.end()) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid request id in SFTP packet."); - } - return it; -} - -void SftpChannelPrivate::closeHook() -{ - m_jobs.clear(); - m_incomingData.clear(); - m_incomingPacket.clear(); - emit closed(); -} - -void SftpChannelPrivate::handleOpenSuccessInternal() -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("SFTP session started"); -#endif - m_sendFacility.sendSftpPacket(remoteChannel()); - m_sftpState = SubsystemRequested; -} - -void SftpChannelPrivate::handleOpenFailureInternal(const QString &reason) -{ - if (channelState() != SessionRequested) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_MSG_CHANNEL_OPEN_FAILURE packet."); - } - emit initializationFailed(tr("Server could not start session: %1").arg(reason)); -} - -void SftpChannelPrivate::sendReadRequest(const SftpDownload::Ptr &job, - quint32 requestId) -{ - Q_ASSERT(job->eofId == SftpInvalidJob); - sendData(m_outgoingPacket.generateReadFile(job->remoteHandle, job->offset, - AbstractSftpPacket::MaxDataSize, requestId).rawData()); - job->offsets[requestId] = job->offset; - job->offset += AbstractSftpPacket::MaxDataSize; - if (job->offset >= job->fileSize) - job->eofId = requestId; -} - -void SftpChannelPrivate::reportRequestError(const AbstractSftpOperationWithHandle::Ptr &job, - const QString &error) -{ - emit finished(job->jobId, error); - job->hasError = true; -} - -void SftpChannelPrivate::finishTransferRequest(const JobMap::Iterator &it) -{ - AbstractSftpTransfer::Ptr job = it.value().staticCast(); - if (job->inFlightCount == 1) - sendTransferCloseHandle(job, it.key()); - else - removeTransferRequest(it); -} - -void SftpChannelPrivate::sendTransferCloseHandle(const AbstractSftpTransfer::Ptr &job, - quint32 requestId) -{ - sendData(m_outgoingPacket.generateCloseHandle(job->remoteHandle, - requestId).rawData()); - job->state = SftpDownload::CloseRequested; -} - -void SftpChannelPrivate::attributesToFileInfo(const SftpFileAttributes &attributes, - SftpFileInfo &fileInfo) const -{ - if (attributes.sizePresent) { - fileInfo.sizeValid = true; - fileInfo.size = attributes.size; - } - if (attributes.permissionsPresent) { - if (attributes.permissions & 0x8000) // S_IFREG - fileInfo.type = FileTypeRegular; - else if (attributes.permissions & 0x4000) // S_IFDIR - fileInfo.type = FileTypeDirectory; - else - fileInfo.type = FileTypeOther; - fileInfo.permissionsValid = true; - fileInfo.permissions = 0; - if (attributes.permissions & 00001) // S_IXOTH - fileInfo.permissions |= QFile::ExeOther; - if (attributes.permissions & 00002) // S_IWOTH - fileInfo.permissions |= QFile::WriteOther; - if (attributes.permissions & 00004) // S_IROTH - fileInfo.permissions |= QFile::ReadOther; - if (attributes.permissions & 00010) // S_IXGRP - fileInfo.permissions |= QFile::ExeGroup; - if (attributes.permissions & 00020) // S_IWGRP - fileInfo.permissions |= QFile::WriteGroup; - if (attributes.permissions & 00040) // S_IRGRP - fileInfo.permissions |= QFile::ReadGroup; - if (attributes.permissions & 00100) // S_IXUSR - fileInfo.permissions |= QFile::ExeUser | QFile::ExeOwner; - if (attributes.permissions & 00200) // S_IWUSR - fileInfo.permissions |= QFile::WriteUser | QFile::WriteOwner; - if (attributes.permissions & 00400) // S_IRUSR - fileInfo.permissions |= QFile::ReadUser | QFile::ReadOwner; - } -} - -void SftpChannelPrivate::removeTransferRequest(const JobMap::Iterator &it) -{ - --it.value().staticCast()->inFlightCount; - m_jobs.erase(it); -} - -void SftpChannelPrivate::sendWriteRequest(const JobMap::Iterator &it) -{ - SftpUploadFile::Ptr job = it.value().staticCast(); - QByteArray data = job->localFile->read(AbstractSftpPacket::MaxDataSize); - if (job->localFile->error() != QFile::NoError) { - if (job->parentJob) - job->parentJob->setError(); - reportRequestError(job, tr("Error reading local file: %1") - .arg(job->localFile->errorString())); - finishTransferRequest(it); - } else if (data.isEmpty()) { - finishTransferRequest(it); - } else { - sendData(m_outgoingPacket.generateWriteFile(job->remoteHandle, - job->offset, data, it.key()).rawData()); - job->offset += AbstractSftpPacket::MaxDataSize; - } -} - -void SftpChannelPrivate::spawnWriteRequests(const JobMap::Iterator &it) -{ - SftpUploadFile::Ptr op = it.value().staticCast(); - op->calculateInFlightCount(AbstractSftpPacket::MaxDataSize); - sendWriteRequest(it); - for (int i = 1; !op->hasError && i < op->inFlightCount; ++i) - sendWriteRequest(m_jobs.insert(++m_nextJobId, op)); -} - -void SftpChannelPrivate::spawnReadRequests(const SftpDownload::Ptr &job) -{ - job->calculateInFlightCount(AbstractSftpPacket::MaxDataSize); - sendReadRequest(job, job->jobId); - for (int i = 1; i < job->inFlightCount; ++i) { - const quint32 requestId = ++m_nextJobId; - m_jobs.insert(requestId, job); - sendReadRequest(job, requestId); - } -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sftpchannel.h b/src/libs/utils/ssh/sftpchannel.h deleted file mode 100644 index 31e99c3147..0000000000 --- a/src/libs/utils/ssh/sftpchannel.h +++ /dev/null @@ -1,113 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SFTCHANNEL_H -#define SFTCHANNEL_H - -#include "sftpdefs.h" -#include "sftpincomingpacket_p.h" - -#include - -#include -#include -#include -#include - -namespace Utils { - -namespace Internal { -class SftpChannelPrivate; -class SshChannelManager; -class SshSendFacility; -} // namespace Internal - -class QTCREATOR_UTILS_EXPORT SftpChannel : public QObject -{ - Q_OBJECT - - friend class Internal::SftpChannelPrivate; - friend class Internal::SshChannelManager; -public: - typedef QSharedPointer Ptr; - - enum State { Uninitialized, Initializing, Initialized, Closing, Closed }; - State state() const; - - void initialize(); - void closeChannel(); - - SftpJobId statFile(const QString &path); - SftpJobId listDirectory(const QString &dirPath); - SftpJobId createDirectory(const QString &dirPath); - SftpJobId removeDirectory(const QString &dirPath); - SftpJobId removeFile(const QString &filePath); - SftpJobId renameFileOrDirectory(const QString &oldPath, - const QString &newPath); - SftpJobId createFile(const QString &filePath, SftpOverwriteMode mode); - SftpJobId createLink(const QString &filePath, const QString &target); - SftpJobId uploadFile(const QString &localFilePath, - const QString &remoteFilePath, SftpOverwriteMode mode); - SftpJobId downloadFile(const QString &remoteFilePath, - const QString &localFilePath, SftpOverwriteMode mode); - SftpJobId uploadDir(const QString &localDirPath, - const QString &remoteParentDirPath); - - ~SftpChannel(); - -signals: - void initialized(); - void initializationFailed(const QString &reason); - void closed(); - - // error.isEmpty <=> finished successfully - void finished(Utils::SftpJobId job, const QString &error = QString()); - - // TODO: Also emit for each file copied by uploadDir(). - void dataAvailable(Utils::SftpJobId job, const QString &data); - - /* - * This signal is emitted as a result of: - * - statFile() (with the list having exactly one element) - * - listDirectory() (potentially more than once) - */ - void fileInfoAvailable(Utils::SftpJobId job, const QList &fileInfoList); - -private: - SftpChannel(quint32 channelId, Internal::SshSendFacility &sendFacility); - - Internal::SftpChannelPrivate *d; -}; - -} // namespace Utils - -#endif // SFTPCHANNEL_H diff --git a/src/libs/utils/ssh/sftpchannel_p.h b/src/libs/utils/ssh/sftpchannel_p.h deleted file mode 100644 index c2b2f79b18..0000000000 --- a/src/libs/utils/ssh/sftpchannel_p.h +++ /dev/null @@ -1,135 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SFTCHANNEL_P_H -#define SFTCHANNEL_P_H - -#include "sftpdefs.h" -#include "sftpincomingpacket_p.h" -#include "sftpoperation_p.h" -#include "sftpoutgoingpacket_p.h" -#include "sshchannel_p.h" - -#include -#include - -namespace Utils { -class SftpChannel; -namespace Internal { - -class SftpChannelPrivate : public AbstractSshChannel -{ - Q_OBJECT - friend class Utils::SftpChannel; -public: - - enum SftpState { Inactive, SubsystemRequested, InitSent, Initialized }; - - virtual void handleChannelSuccess(); - virtual void handleChannelFailure(); - - virtual void closeHook(); - -signals: - void initialized(); - void initializationFailed(const QString &reason); - void closed(); - void finished(Utils::SftpJobId job, const QString &error = QString()); - void dataAvailable(Utils::SftpJobId job, const QString &data); - void fileInfoAvailable(Utils::SftpJobId job, const QList &fileInfoList); - -private: - typedef QMap JobMap; - - SftpChannelPrivate(quint32 channelId, SshSendFacility &sendFacility, - SftpChannel *sftp); - SftpJobId createJob(const AbstractSftpOperation::Ptr &job); - - virtual void handleOpenSuccessInternal(); - virtual void handleOpenFailureInternal(const QString &reason); - virtual void handleChannelDataInternal(const QByteArray &data); - virtual void handleChannelExtendedDataInternal(quint32 type, - const QByteArray &data); - virtual void handleExitStatus(const SshChannelExitStatus &exitStatus); - virtual void handleExitSignal(const SshChannelExitSignal &signal); - - void handleCurrentPacket(); - void handleServerVersion(); - void handleHandle(); - void handleStatus(); - void handleName(); - void handleReadData(); - void handleAttrs(); - - void handleStatusGeneric(const JobMap::Iterator &it, - const SftpStatusResponse &response); - void handleMkdirStatus(const JobMap::Iterator &it, - const SftpStatusResponse &response); - void handleLsStatus(const JobMap::Iterator &it, - const SftpStatusResponse &response); - void handleGetStatus(const JobMap::Iterator &it, - const SftpStatusResponse &response); - void handlePutStatus(const JobMap::Iterator &it, - const SftpStatusResponse &response); - - void handleLsHandle(const JobMap::Iterator &it); - void handleCreateFileHandle(const JobMap::Iterator &it); - void handleGetHandle(const JobMap::Iterator &it); - void handlePutHandle(const JobMap::Iterator &it); - - void spawnReadRequests(const SftpDownload::Ptr &job); - void spawnWriteRequests(const JobMap::Iterator &it); - void sendReadRequest(const SftpDownload::Ptr &job, quint32 requestId); - void sendWriteRequest(const JobMap::Iterator &it); - void finishTransferRequest(const JobMap::Iterator &it); - void removeTransferRequest(const JobMap::Iterator &it); - void reportRequestError(const AbstractSftpOperationWithHandle::Ptr &job, - const QString &error); - void sendTransferCloseHandle(const AbstractSftpTransfer::Ptr &job, - quint32 requestId); - - void attributesToFileInfo(const SftpFileAttributes &attributes, SftpFileInfo &fileInfo) const; - - JobMap::Iterator lookupJob(SftpJobId id); - JobMap m_jobs; - SftpOutgoingPacket m_outgoingPacket; - SftpIncomingPacket m_incomingPacket; - QByteArray m_incomingData; - SftpJobId m_nextJobId; - SftpState m_sftpState; - SftpChannel *m_sftp; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SFTPCHANNEL_P_H diff --git a/src/libs/utils/ssh/sftpdefs.cpp b/src/libs/utils/ssh/sftpdefs.cpp deleted file mode 100644 index 8dfed93406..0000000000 --- a/src/libs/utils/ssh/sftpdefs.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sftpdefs.h" - -namespace Utils { const SftpJobId SftpInvalidJob = 0; } diff --git a/src/libs/utils/ssh/sftpdefs.h b/src/libs/utils/ssh/sftpdefs.h deleted file mode 100644 index 85087e126a..0000000000 --- a/src/libs/utils/ssh/sftpdefs.h +++ /dev/null @@ -1,69 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SFTPDEFS_H -#define SFTPDEFS_H - -#include - -#include -#include - -namespace Utils { - -typedef quint32 SftpJobId; -QTCREATOR_UTILS_EXPORT extern const SftpJobId SftpInvalidJob; - -enum SftpOverwriteMode { - SftpOverwriteExisting, SftpAppendToExisting, SftpSkipExisting -}; - -enum SftpFileType { FileTypeRegular, FileTypeDirectory, FileTypeOther, FileTypeUnknown }; - -class QTCREATOR_UTILS_EXPORT SftpFileInfo -{ -public: - SftpFileInfo() : type(FileTypeUnknown), sizeValid(false), permissionsValid(false) { } - - QString name; - SftpFileType type; - quint64 size; - QFile::Permissions permissions; - - // The RFC allows an SFTP server not to support any file attributes beyond the name. - bool sizeValid; - bool permissionsValid; -}; - -} // namespace Utils - -#endif // SFTPDEFS_H diff --git a/src/libs/utils/ssh/sftpfilesystemmodel.cpp b/src/libs/utils/ssh/sftpfilesystemmodel.cpp deleted file mode 100644 index d077c698cb..0000000000 --- a/src/libs/utils/ssh/sftpfilesystemmodel.cpp +++ /dev/null @@ -1,388 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ -#include "sftpfilesystemmodel.h" - -#include "sftpchannel.h" -#include "sshconnection.h" -#include "sshconnectionmanager.h" - -#include - -#include -#include -#include -#include -#include - -namespace Utils { -namespace Internal { -namespace { - -class SftpDirNode; -class SftpFileNode -{ -public: - SftpFileNode() : parent(0) { } - virtual ~SftpFileNode() { } - - QString path; - SftpFileInfo fileInfo; - SftpDirNode *parent; -}; - -class SftpDirNode : public SftpFileNode -{ -public: - SftpDirNode() : lsState(LsNotYetCalled) { } - ~SftpDirNode() { qDeleteAll(children); } - - enum { LsNotYetCalled, LsRunning, LsFinished } lsState; - QList children; -}; - -typedef QHash DirNodeHash; - -SftpFileNode *indexToFileNode(const QModelIndex &index) -{ - return static_cast(index.internalPointer()); -} - -SftpDirNode *indexToDirNode(const QModelIndex &index) -{ - SftpFileNode * const fileNode = indexToFileNode(index); - QTC_CHECK(fileNode); - return dynamic_cast(fileNode); -} - -} // anonymous namespace - -class SftpFileSystemModelPrivate -{ -public: - SshConnection::Ptr sshConnection; - SftpChannel::Ptr sftpChannel; - QString rootDirectory; - SftpFileNode *rootNode; - SftpJobId statJobId; - DirNodeHash lsOps; - QList externalJobs; -}; -} // namespace Internal - -using namespace Internal; - -SftpFileSystemModel::SftpFileSystemModel(QObject *parent) - : QAbstractItemModel(parent), d(new SftpFileSystemModelPrivate) -{ - d->rootDirectory = QLatin1String("/"); - d->rootNode = 0; - d->statJobId = SftpInvalidJob; -} - -SftpFileSystemModel::~SftpFileSystemModel() -{ - shutDown(); - delete d; -} - -void SftpFileSystemModel::setSshConnection(const SshConnectionParameters &sshParams) -{ - QTC_ASSERT(!d->sshConnection, return); - d->sshConnection = SshConnectionManager::instance().acquireConnection(sshParams); - connect(d->sshConnection.data(), SIGNAL(error(Utils::SshError)), - SLOT(handleSshConnectionFailure())); - if (d->sshConnection->state() == SshConnection::Connected) { - handleSshConnectionEstablished(); - return; - } - connect(d->sshConnection.data(), SIGNAL(connected()), SLOT(handleSshConnectionEstablished())); - if (d->sshConnection->state() == SshConnection::Unconnected) - d->sshConnection->connectToHost(); -} - -void SftpFileSystemModel::setRootDirectory(const QString &path) -{ - beginResetModel(); - d->rootDirectory = path; - delete d->rootNode; - d->rootNode = 0; - d->lsOps.clear(); - d->statJobId = SftpInvalidJob; - endResetModel(); - statRootDirectory(); -} - -QString SftpFileSystemModel::rootDirectory() const -{ - return d->rootDirectory; -} - -SftpJobId SftpFileSystemModel::downloadFile(const QModelIndex &index, const QString &targetFilePath) -{ - QTC_ASSERT(d->rootNode, return SftpInvalidJob); - const SftpFileNode * const fileNode = indexToFileNode(index); - QTC_ASSERT(fileNode, return SftpInvalidJob); - QTC_ASSERT(fileNode->fileInfo.type == FileTypeRegular, return SftpInvalidJob); - const SftpJobId jobId = d->sftpChannel->downloadFile(fileNode->path, targetFilePath, - SftpOverwriteExisting); - if (jobId != SftpInvalidJob) - d->externalJobs << jobId; - return jobId; -} - -int SftpFileSystemModel::columnCount(const QModelIndex &parent) const -{ - Q_UNUSED(parent); - return 2; // type + name -} - -QVariant SftpFileSystemModel::data(const QModelIndex &index, int role) const -{ - const SftpFileNode * const node = indexToFileNode(index); - if (index.column() == 0 && role == Qt::DecorationRole) { - switch (node->fileInfo.type) { - case FileTypeRegular: - case FileTypeOther: - return QIcon(QLatin1String(":/core/images/unknownfile.png")); - case FileTypeDirectory: - return QIcon(QLatin1String(":/core/images/dir.png")); - case FileTypeUnknown: - return QIcon(QLatin1String(":/core/images/help.png")); // Shows a question mark. - } - } - if (index.column() == 1) { - if (role == Qt::DisplayRole) - return node->fileInfo.name; - if (role == PathRole) - return node->path; - } - return QVariant(); -} - -Qt::ItemFlags SftpFileSystemModel::flags(const QModelIndex &index) const -{ - if (!index.isValid()) - return Qt::NoItemFlags; - return Qt::ItemIsSelectable | Qt::ItemIsEnabled; -} - -QVariant SftpFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - if (orientation != Qt::Horizontal) - return QVariant(); - if (role != Qt::DisplayRole) - return QVariant(); - if (section == 0) - return tr("File Type"); - if (section == 1) - return tr("File Name"); - return QVariant(); -} - -QModelIndex SftpFileSystemModel::index(int row, int column, const QModelIndex &parent) const -{ - if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent)) - return QModelIndex(); - if (!d->rootNode) - return QModelIndex(); - if (!parent.isValid()) - return createIndex(row, column, d->rootNode); - const SftpDirNode * const parentNode = indexToDirNode(parent); - QTC_ASSERT(parentNode, return QModelIndex()); - QTC_ASSERT(row < parentNode->children.count(), return QModelIndex()); - SftpFileNode * const childNode = parentNode->children.at(row); - return createIndex(row, column, childNode); -} - -QModelIndex SftpFileSystemModel::parent(const QModelIndex &child) const -{ - if (!child.isValid()) // Don't assert on this, since the model tester tries it. - return QModelIndex(); - - const SftpFileNode * const childNode = indexToFileNode(child); - QTC_ASSERT(childNode, return QModelIndex()); - if (childNode == d->rootNode) - return QModelIndex(); - SftpDirNode * const parentNode = childNode->parent; - if (parentNode == d->rootNode) - return createIndex(0, 0, d->rootNode); - const SftpDirNode * const grandParentNode = parentNode->parent; - QTC_ASSERT(grandParentNode, return QModelIndex()); - return createIndex(grandParentNode->children.indexOf(parentNode), 0, parentNode); -} - -int SftpFileSystemModel::rowCount(const QModelIndex &parent) const -{ - if (!d->rootNode) - return 0; - if (!parent.isValid()) - return 1; - if (parent.column() != 0) - return 0; - SftpDirNode * const dirNode = indexToDirNode(parent); - if (!dirNode) - return 0; - if (dirNode->lsState != SftpDirNode::LsNotYetCalled) - return dirNode->children.count(); - d->lsOps.insert(d->sftpChannel->listDirectory(dirNode->path), dirNode); - dirNode->lsState = SftpDirNode::LsRunning; - return 0; -} - -void SftpFileSystemModel::statRootDirectory() -{ - d->statJobId = d->sftpChannel->statFile(d->rootDirectory); -} - -void SftpFileSystemModel::shutDown() -{ - if (d->sftpChannel) { - disconnect(d->sftpChannel.data(), 0, this, 0); - d->sftpChannel->closeChannel(); - d->sftpChannel.clear(); - } - if (d->sshConnection) { - disconnect(d->sshConnection.data(), 0, this, 0); - SshConnectionManager::instance().releaseConnection(d->sshConnection); - d->sshConnection.clear(); - } - delete d->rootNode; - d->rootNode = 0; -} - -void SftpFileSystemModel::handleSshConnectionFailure() -{ - emit connectionError(d->sshConnection->errorString()); - beginResetModel(); - shutDown(); - endResetModel(); -} - -void SftpFileSystemModel::handleSftpChannelInitialized() -{ - connect(d->sftpChannel.data(), - SIGNAL(fileInfoAvailable(Utils::SftpJobId,QList)), - SLOT(handleFileInfo(Utils::SftpJobId,QList))); - connect(d->sftpChannel.data(), SIGNAL(finished(Utils::SftpJobId,QString)), - SLOT(handleSftpJobFinished(Utils::SftpJobId,QString))); - statRootDirectory(); -} - -void SftpFileSystemModel::handleSshConnectionEstablished() -{ - d->sftpChannel = d->sshConnection->createSftpChannel(); - connect(d->sftpChannel.data(), SIGNAL(initialized()), SLOT(handleSftpChannelInitialized())); - connect(d->sftpChannel.data(), SIGNAL(initializationFailed(QString)), - SLOT(handleSftpChannelInitializationFailed(QString))); - d->sftpChannel->initialize(); -} - -void SftpFileSystemModel::handleSftpChannelInitializationFailed(const QString &reason) -{ - emit connectionError(reason); - beginResetModel(); - shutDown(); - endResetModel(); -} - -void SftpFileSystemModel::handleFileInfo(SftpJobId jobId, const QList &fileInfoList) -{ - if (jobId == d->statJobId) { - QTC_ASSERT(!d->rootNode, return); - beginInsertRows(QModelIndex(), 0, 0); - d->rootNode = new SftpDirNode; - d->rootNode->path = d->rootDirectory; - d->rootNode->fileInfo = fileInfoList.first(); - d->rootNode->fileInfo.name = d->rootDirectory == QLatin1String("/") - ? d->rootDirectory : QFileInfo(d->rootDirectory).fileName(); - endInsertRows(); - return; - } - SftpDirNode * const parentNode = d->lsOps.value(jobId); - QTC_ASSERT(parentNode, return); - QList filteredList; - foreach (const SftpFileInfo &fi, fileInfoList) { - if (fi.name != QLatin1String(".") && fi.name != QLatin1String("..")) - filteredList << fi; - } - if (filteredList.isEmpty()) - return; - - // In theory beginInsertRows() should suffice, but that fails to have an effect - // if rowCount() returned 0 earlier. - emit layoutAboutToBeChanged(); - - foreach (const SftpFileInfo &fileInfo, filteredList) { - SftpFileNode *childNode; - if (fileInfo.type == FileTypeDirectory) - childNode = new SftpDirNode; - else - childNode = new SftpFileNode; - childNode->path = parentNode->path; - if (!childNode->path.endsWith(QLatin1Char('/'))) - childNode->path += QLatin1Char('/'); - childNode->path += fileInfo.name; - childNode->fileInfo = fileInfo; - childNode->parent = parentNode; - parentNode->children << childNode; - } - emit layoutChanged(); // Should be endInsertRows(), see above. -} - -void SftpFileSystemModel::handleSftpJobFinished(SftpJobId jobId, const QString &errorMessage) -{ - if (jobId == d->statJobId) { - d->statJobId = SftpInvalidJob; - if (!errorMessage.isEmpty()) - emit sftpOperationFailed(tr("Error getting 'stat' info about '%1': %2") - .arg(rootDirectory(), errorMessage)); - return; - } - - DirNodeHash::Iterator it = d->lsOps.find(jobId); - if (it != d->lsOps.end()) { - QTC_CHECK(it.value()->lsState == SftpDirNode::LsRunning); - it.value()->lsState = SftpDirNode::LsFinished; - if (!errorMessage.isEmpty()) - emit sftpOperationFailed(tr("Error listing contents of directory '%1': %2") - .arg(it.value()->path, errorMessage)); - d->lsOps.erase(it); - return; - } - - const int jobIndex = d->externalJobs.indexOf(jobId); - QTC_ASSERT(jobIndex != -1, return); - d->externalJobs.removeAt(jobIndex); - emit sftpOperationFinished(jobId, errorMessage); -} - -} // namespace Utils diff --git a/src/libs/utils/ssh/sftpfilesystemmodel.h b/src/libs/utils/ssh/sftpfilesystemmodel.h deleted file mode 100644 index f4b83799cd..0000000000 --- a/src/libs/utils/ssh/sftpfilesystemmodel.h +++ /dev/null @@ -1,110 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ -#ifndef SFTPFILESYSTEMMODEL_H -#define SFTPFILESYSTEMMODEL_H - -#include "sftpdefs.h" - -#include - -#include - -namespace Utils { -class SshConnectionParameters; - -namespace Internal { class SftpFileSystemModelPrivate; } - -// Very simple read-only model. Symbolic links are not followed. -class QTCREATOR_UTILS_EXPORT SftpFileSystemModel : public QAbstractItemModel -{ - Q_OBJECT -public: - // Use this to get the full path of a file or directory via data(). - static const int PathRole = Qt::UserRole; - - explicit SftpFileSystemModel(QObject *parent = 0); - ~SftpFileSystemModel(); - - /* - * Once this is called, an SFTP connection is established and the model is populated. - * The effect of additional calls is undefined. - */ - void setSshConnection(const SshConnectionParameters &sshParams); - - void setRootDirectory(const QString &path); // Default is "/". - QString rootDirectory() const; - - SftpJobId downloadFile(const QModelIndex &index, const QString &targetFilePath); - -signals: - /* - * E.g. "Permission denied". Note that this can happen without direct user intervention, - * due to e.g. the view calling rowCount() on a non-readable directory. This signal should - * therefore not result in a message box or similar, since it might occur very often. - */ - void sftpOperationFailed(const QString &errorMessage); - - /* - * This error is not recoverable. The model will not have any content after - * the signal has been emitted. - */ - void connectionError(const QString &errorMessage); - - // Success <=> error.isEmpty(). - void sftpOperationFinished(Utils::SftpJobId, const QString &error); - -private slots: - void handleSshConnectionEstablished(); - void handleSshConnectionFailure(); - void handleSftpChannelInitialized(); - void handleSftpChannelInitializationFailed(const QString &reason); - void handleFileInfo(Utils::SftpJobId jobId, const QList &fileInfoList); - void handleSftpJobFinished(Utils::SftpJobId jobId, const QString &errorMessage); - -private: - int columnCount(const QModelIndex &parent = QModelIndex()) const; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; - Qt::ItemFlags flags(const QModelIndex &index) const; - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; - QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; - QModelIndex parent(const QModelIndex &child) const; - int rowCount(const QModelIndex &parent = QModelIndex()) const; - - void statRootDirectory(); - void shutDown(); - - Internal::SftpFileSystemModelPrivate * const d; -}; - -} // namespace Utils; - -#endif // SFTPFILESYSTEMMODEL_H diff --git a/src/libs/utils/ssh/sftpincomingpacket.cpp b/src/libs/utils/ssh/sftpincomingpacket.cpp deleted file mode 100644 index e90bb39d4e..0000000000 --- a/src/libs/utils/ssh/sftpincomingpacket.cpp +++ /dev/null @@ -1,225 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sftpincomingpacket_p.h" - -#include "sshexception_p.h" -#include "sshpacketparser_p.h" - -namespace Utils { -namespace Internal { - -SftpIncomingPacket::SftpIncomingPacket() : m_length(0) -{ -} - -void SftpIncomingPacket::consumeData(QByteArray &newData) -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("%s: current data size = %d, new data size = %d", Q_FUNC_INFO, - m_data.size(), newData.size()); -#endif - - if (isComplete() || dataSize() + newData.size() < sizeof m_length) - return; - - if (dataSize() < sizeof m_length) { - moveFirstBytes(m_data, newData, sizeof m_length - m_data.size()); - m_length = SshPacketParser::asUint32(m_data, static_cast(0)); - if (m_length < static_cast(TypeOffset + 1) - || m_length > MaxPacketSize) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid length field in SFTP packet."); - } - } - - moveFirstBytes(m_data, newData, - qMin(m_length - dataSize() + 4, newData.size())); -} - -void SftpIncomingPacket::moveFirstBytes(QByteArray &target, QByteArray &source, - int n) -{ - target.append(source.left(n)); - source.remove(0, n); -} - -bool SftpIncomingPacket::isComplete() const -{ - return m_length == dataSize() - 4; -} - -void SftpIncomingPacket::clear() -{ - m_data.clear(); - m_length = 0; -} - -quint32 SftpIncomingPacket::extractServerVersion() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_FXP_VERSION); - try { - return SshPacketParser::asUint32(m_data, TypeOffset + 1); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_FXP_VERSION packet."); - } -} - -SftpHandleResponse SftpIncomingPacket::asHandleResponse() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_FXP_HANDLE); - try { - SftpHandleResponse response; - quint32 offset = RequestIdOffset; - response.requestId = SshPacketParser::asUint32(m_data, &offset); - response.handle = SshPacketParser::asString(m_data, &offset); - return response; - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_FXP_HANDLE packet"); - } -} - -SftpStatusResponse SftpIncomingPacket::asStatusResponse() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_FXP_STATUS); - try { - SftpStatusResponse response; - quint32 offset = RequestIdOffset; - response.requestId = SshPacketParser::asUint32(m_data, &offset); - response.status = static_cast(SshPacketParser::asUint32(m_data, &offset)); - response.errorString = SshPacketParser::asUserString(m_data, &offset); - response.language = SshPacketParser::asString(m_data, &offset); - return response; - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_FXP_STATUS packet."); - } -} - -SftpNameResponse SftpIncomingPacket::asNameResponse() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_FXP_NAME); - try { - SftpNameResponse response; - quint32 offset = RequestIdOffset; - response.requestId = SshPacketParser::asUint32(m_data, &offset); - const quint32 count = SshPacketParser::asUint32(m_data, &offset); - for (quint32 i = 0; i < count; ++i) - response.files << asFile(offset); - return response; - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_FXP_NAME packet."); - } -} - -SftpDataResponse SftpIncomingPacket::asDataResponse() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_FXP_DATA); - try { - SftpDataResponse response; - quint32 offset = RequestIdOffset; - response.requestId = SshPacketParser::asUint32(m_data, &offset); - response.data = SshPacketParser::asString(m_data, &offset); - return response; - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_FXP_DATA packet."); - } -} - -SftpAttrsResponse SftpIncomingPacket::asAttrsResponse() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_FXP_ATTRS); - try { - SftpAttrsResponse response; - quint32 offset = RequestIdOffset; - response.requestId = SshPacketParser::asUint32(m_data, &offset); - response.attrs = asFileAttributes(offset); - return response; - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_FXP_ATTRS packet."); - } -} - -SftpFile SftpIncomingPacket::asFile(quint32 &offset) const -{ - SftpFile file; - file.fileName - = QString::fromUtf8(SshPacketParser::asString(m_data, &offset)); - file.longName - = QString::fromUtf8(SshPacketParser::asString(m_data, &offset)); - file.attributes = asFileAttributes(offset); - return file; -} - -SftpFileAttributes SftpIncomingPacket::asFileAttributes(quint32 &offset) const -{ - SftpFileAttributes attributes; - const quint32 flags = SshPacketParser::asUint32(m_data, &offset); - attributes.sizePresent = flags & SSH_FILEXFER_ATTR_SIZE; - attributes.timesPresent = flags & SSH_FILEXFER_ATTR_ACMODTIME; - attributes.uidAndGidPresent = flags & SSH_FILEXFER_ATTR_UIDGID; - attributes.permissionsPresent = flags & SSH_FILEXFER_ATTR_PERMISSIONS; - if (attributes.sizePresent) - attributes.size = SshPacketParser::asUint64(m_data, &offset); - if (attributes.uidAndGidPresent) { - attributes.uid = SshPacketParser::asUint32(m_data, &offset); - attributes.gid = SshPacketParser::asUint32(m_data, &offset); - } - if (attributes.permissionsPresent) - attributes.permissions = SshPacketParser::asUint32(m_data, &offset); - if (attributes.timesPresent) { - attributes.atime = SshPacketParser::asUint32(m_data, &offset); - attributes.mtime = SshPacketParser::asUint32(m_data, &offset); - } - if (flags & SSH_FILEXFER_ATTR_EXTENDED) { - const quint32 count = SshPacketParser::asUint32(m_data, &offset); - for (quint32 i = 0; i < count; ++i) { - SshPacketParser::asString(m_data, &offset); - SshPacketParser::asString(m_data, &offset); - } - } - return attributes; -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sftpincomingpacket_p.h b/src/libs/utils/ssh/sftpincomingpacket_p.h deleted file mode 100644 index f1b271ead7..0000000000 --- a/src/libs/utils/ssh/sftpincomingpacket_p.h +++ /dev/null @@ -1,114 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SFTPINCOMINGPACKET_P_H -#define SFTPINCOMINGPACKET_P_H - -#include "sftppacket_p.h" - -namespace Utils { -namespace Internal { - -struct SftpHandleResponse { - quint32 requestId; - QByteArray handle; -}; - -struct SftpStatusResponse { - quint32 requestId; - SftpStatusCode status; - QString errorString; - QByteArray language; -}; - -struct SftpFileAttributes { - bool sizePresent; - bool timesPresent; - bool uidAndGidPresent; - bool permissionsPresent; - quint64 size; - quint32 uid; - quint32 gid; - quint32 permissions; - quint32 atime; - quint32 mtime; -}; - -struct SftpFile { - QString fileName; - QString longName; // Not present in later RFCs, so we don't expose this to the user. - SftpFileAttributes attributes; -}; - -struct SftpNameResponse { - quint32 requestId; - QList files; -}; - -struct SftpDataResponse { - quint32 requestId; - QByteArray data; -}; - -struct SftpAttrsResponse { - quint32 requestId; - SftpFileAttributes attrs; -}; - -class SftpIncomingPacket : public AbstractSftpPacket -{ -public: - SftpIncomingPacket(); - - void consumeData(QByteArray &data); - void clear(); - bool isComplete() const; - quint32 extractServerVersion() const; - SftpHandleResponse asHandleResponse() const; - SftpStatusResponse asStatusResponse() const; - SftpNameResponse asNameResponse() const; - SftpDataResponse asDataResponse() const; - SftpAttrsResponse asAttrsResponse() const; - -private: - void moveFirstBytes(QByteArray &target, QByteArray &source, int n); - - SftpFileAttributes asFileAttributes(quint32 &offset) const; - SftpFile asFile(quint32 &offset) const; - - quint32 m_length; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SFTPINCOMINGPACKET_P_H diff --git a/src/libs/utils/ssh/sftpoperation.cpp b/src/libs/utils/ssh/sftpoperation.cpp deleted file mode 100644 index 733741a9cb..0000000000 --- a/src/libs/utils/ssh/sftpoperation.cpp +++ /dev/null @@ -1,227 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sftpoperation_p.h" - -#include "sftpoutgoingpacket_p.h" - -#include - -namespace Utils { -namespace Internal { - -AbstractSftpOperation::AbstractSftpOperation(SftpJobId jobId) : jobId(jobId) -{ -} - -AbstractSftpOperation::~AbstractSftpOperation() { } - - -SftpStatFile::SftpStatFile(SftpJobId jobId, const QString &path) - : AbstractSftpOperation(jobId), path(path) -{ -} - -SftpOutgoingPacket &SftpStatFile::initialPacket(SftpOutgoingPacket &packet) -{ - return packet.generateStat(path, jobId); -} - -SftpMakeDir::SftpMakeDir(SftpJobId jobId, const QString &path, - const SftpUploadDir::Ptr &parentJob) - : AbstractSftpOperation(jobId), parentJob(parentJob), remoteDir(path) -{ -} - -SftpOutgoingPacket &SftpMakeDir::initialPacket(SftpOutgoingPacket &packet) -{ - return packet.generateMkDir(remoteDir, jobId); -} - - -SftpRmDir::SftpRmDir(SftpJobId, const QString &path) - : AbstractSftpOperation(jobId), remoteDir(path) -{ -} - -SftpOutgoingPacket &SftpRmDir::initialPacket(SftpOutgoingPacket &packet) -{ - return packet.generateRmDir(remoteDir, jobId); -} - - -SftpRm::SftpRm(SftpJobId jobId, const QString &path) - : AbstractSftpOperation(jobId), remoteFile(path) {} - -SftpOutgoingPacket &SftpRm::initialPacket(SftpOutgoingPacket &packet) -{ - return packet.generateRm(remoteFile, jobId); -} - - -SftpRename::SftpRename(SftpJobId jobId, const QString &oldPath, - const QString &newPath) - : AbstractSftpOperation(jobId), oldPath(oldPath), newPath(newPath) -{ -} - -SftpOutgoingPacket &SftpRename::initialPacket(SftpOutgoingPacket &packet) -{ - return packet.generateRename(oldPath, newPath, jobId); -} - - -SftpCreateLink::SftpCreateLink(SftpJobId jobId, const QString &filePath, const QString &target) - : AbstractSftpOperation(jobId), filePath(filePath), target(target) -{ -} - -SftpOutgoingPacket &SftpCreateLink::initialPacket(SftpOutgoingPacket &packet) -{ - return packet.generateCreateLink(filePath, target, jobId); -} - - -AbstractSftpOperationWithHandle::AbstractSftpOperationWithHandle(SftpJobId jobId, - const QString &remotePath) - : AbstractSftpOperation(jobId), - remotePath(remotePath), state(Inactive), hasError(false) -{ -} - -AbstractSftpOperationWithHandle::~AbstractSftpOperationWithHandle() { } - - -SftpListDir::SftpListDir(SftpJobId jobId, const QString &path) - : AbstractSftpOperationWithHandle(jobId, path) -{ -} - -SftpOutgoingPacket &SftpListDir::initialPacket(SftpOutgoingPacket &packet) -{ - state = OpenRequested; - return packet.generateOpenDir(remotePath, jobId); -} - - -SftpCreateFile::SftpCreateFile(SftpJobId jobId, const QString &path, - SftpOverwriteMode mode) - : AbstractSftpOperationWithHandle(jobId, path), mode(mode) -{ -} - -SftpOutgoingPacket & SftpCreateFile::initialPacket(SftpOutgoingPacket &packet) -{ - state = OpenRequested; - return packet.generateOpenFileForWriting(remotePath, mode, - SftpOutgoingPacket::DefaultPermissions, jobId); -} - - -const int AbstractSftpTransfer::MaxInFlightCount = 10; // Experimentally found to be enough. - -AbstractSftpTransfer::AbstractSftpTransfer(SftpJobId jobId, const QString &remotePath, - const QSharedPointer &localFile) - : AbstractSftpOperationWithHandle(jobId, remotePath), - localFile(localFile), fileSize(0), offset(0), inFlightCount(0), - statRequested(false) -{ -} - -AbstractSftpTransfer::~AbstractSftpTransfer() {} - -void AbstractSftpTransfer::calculateInFlightCount(quint32 chunkSize) -{ - if (fileSize == 0) { - inFlightCount = 1; - } else { - inFlightCount = fileSize / chunkSize; - if (fileSize % chunkSize) - ++inFlightCount; - if (inFlightCount > MaxInFlightCount) - inFlightCount = MaxInFlightCount; - } -} - - -SftpDownload::SftpDownload(SftpJobId jobId, const QString &remotePath, - const QSharedPointer &localFile) - : AbstractSftpTransfer(jobId, remotePath, localFile), eofId(SftpInvalidJob) -{ -} - -SftpOutgoingPacket &SftpDownload::initialPacket(SftpOutgoingPacket &packet) -{ - state = OpenRequested; - return packet.generateOpenFileForReading(remotePath, jobId); -} - - -SftpUploadFile::SftpUploadFile(SftpJobId jobId, const QString &remotePath, - const QSharedPointer &localFile, SftpOverwriteMode mode, - const SftpUploadDir::Ptr &parentJob) - : AbstractSftpTransfer(jobId, remotePath, localFile), - parentJob(parentJob), mode(mode) -{ - fileSize = localFile->size(); -} - -SftpOutgoingPacket &SftpUploadFile::initialPacket(SftpOutgoingPacket &packet) -{ - state = OpenRequested; - quint32 permissions = 0; - const QFile::Permissions &qtPermissions = localFile->permissions(); - if (qtPermissions & QFile::ExeOther) - permissions |= 1 << 0; - if (qtPermissions & QFile::WriteOther) - permissions |= 1 << 1; - if (qtPermissions & QFile::ReadOther) - permissions |= 1 << 2; - if (qtPermissions & QFile::ExeGroup) - permissions |= 1<< 3; - if (qtPermissions & QFile::WriteGroup) - permissions |= 1<< 4; - if (qtPermissions & QFile::ReadGroup) - permissions |= 1<< 5; - if (qtPermissions & QFile::ExeOwner) - permissions |= 1<< 6; - if (qtPermissions & QFile::WriteOwner) - permissions |= 1<< 7; - if (qtPermissions & QFile::ReadOwner) - permissions |= 1<< 8; - return packet.generateOpenFileForWriting(remotePath, mode, permissions, jobId); -} - -SftpUploadDir::~SftpUploadDir() {} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sftpoperation_p.h b/src/libs/utils/ssh/sftpoperation_p.h deleted file mode 100644 index eeaa69ac10..0000000000 --- a/src/libs/utils/ssh/sftpoperation_p.h +++ /dev/null @@ -1,254 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SFTPOPERATION_P_H -#define SFTPOPERATION_P_H - -#include "sftpdefs.h" - -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE -class QFile; -QT_END_NAMESPACE - -namespace Utils { -namespace Internal { - -class SftpOutgoingPacket; - -struct AbstractSftpOperation -{ - typedef QSharedPointer Ptr; - enum Type { - StatFile, ListDir, MakeDir, RmDir, Rm, Rename, CreateLink, CreateFile, Download, UploadFile - }; - - AbstractSftpOperation(SftpJobId jobId); - virtual ~AbstractSftpOperation(); - virtual Type type() const = 0; - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet) = 0; - - const SftpJobId jobId; - -private: - AbstractSftpOperation(const AbstractSftpOperation &); - AbstractSftpOperation &operator=(const AbstractSftpOperation &); -}; - -struct SftpUploadDir; - -struct SftpStatFile : public AbstractSftpOperation -{ - typedef QSharedPointer Ptr; - - SftpStatFile(SftpJobId jobId, const QString &path); - virtual Type type() const { return StatFile; } - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); - - const QString path; -}; - -struct SftpMakeDir : public AbstractSftpOperation -{ - typedef QSharedPointer Ptr; - - SftpMakeDir(SftpJobId jobId, const QString &path, - const QSharedPointer &parentJob = QSharedPointer()); - virtual Type type() const { return MakeDir; } - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); - - const QSharedPointer parentJob; - const QString remoteDir; -}; - -struct SftpRmDir : public AbstractSftpOperation -{ - typedef QSharedPointer Ptr; - - SftpRmDir(SftpJobId jobId, const QString &path); - virtual Type type() const { return RmDir; } - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); - - const QString remoteDir; -}; - -struct SftpRm : public AbstractSftpOperation -{ - typedef QSharedPointer Ptr; - - SftpRm(SftpJobId jobId, const QString &path); - virtual Type type() const { return Rm; } - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); - - const QString remoteFile; -}; - -struct SftpRename : public AbstractSftpOperation -{ - typedef QSharedPointer Ptr; - - SftpRename(SftpJobId jobId, const QString &oldPath, const QString &newPath); - virtual Type type() const { return Rename; } - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); - - const QString oldPath; - const QString newPath; -}; - -struct SftpCreateLink : public AbstractSftpOperation -{ - typedef QSharedPointer Ptr; - - SftpCreateLink(SftpJobId jobId, const QString &filePath, const QString &target); - virtual Type type() const { return CreateLink; } - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); - - const QString filePath; - const QString target; -}; - - -struct AbstractSftpOperationWithHandle : public AbstractSftpOperation -{ - typedef QSharedPointer Ptr; - enum State { Inactive, OpenRequested, Open, CloseRequested }; - - AbstractSftpOperationWithHandle(SftpJobId jobId, const QString &remotePath); - ~AbstractSftpOperationWithHandle(); - - const QString remotePath; - QByteArray remoteHandle; - State state; - bool hasError; -}; - - -struct SftpListDir : public AbstractSftpOperationWithHandle -{ - typedef QSharedPointer Ptr; - - SftpListDir(SftpJobId jobId, const QString &path); - virtual Type type() const { return ListDir; } - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); -}; - - -struct SftpCreateFile : public AbstractSftpOperationWithHandle -{ - typedef QSharedPointer Ptr; - - SftpCreateFile(SftpJobId jobId, const QString &path, SftpOverwriteMode mode); - virtual Type type() const { return CreateFile; } - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); - - const SftpOverwriteMode mode; -}; - -struct AbstractSftpTransfer : public AbstractSftpOperationWithHandle -{ - typedef QSharedPointer Ptr; - - AbstractSftpTransfer(SftpJobId jobId, const QString &remotePath, - const QSharedPointer &localFile); - ~AbstractSftpTransfer(); - void calculateInFlightCount(quint32 chunkSize); - - static const int MaxInFlightCount; - - const QSharedPointer localFile; - quint64 fileSize; - quint64 offset; - int inFlightCount; - bool statRequested; -}; - -struct SftpDownload : public AbstractSftpTransfer -{ - typedef QSharedPointer Ptr; - SftpDownload(SftpJobId jobId, const QString &remotePath, - const QSharedPointer &localFile); - virtual Type type() const { return Download; } - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); - - QMap offsets; - SftpJobId eofId; -}; - -struct SftpUploadFile : public AbstractSftpTransfer -{ - typedef QSharedPointer Ptr; - - SftpUploadFile(SftpJobId jobId, const QString &remotePath, - const QSharedPointer &localFile, SftpOverwriteMode mode, - const QSharedPointer &parentJob = QSharedPointer()); - virtual Type type() const { return UploadFile; } - virtual SftpOutgoingPacket &initialPacket(SftpOutgoingPacket &packet); - - const QSharedPointer parentJob; - SftpOverwriteMode mode; -}; - -// Composite operation. -struct SftpUploadDir -{ - typedef QSharedPointer Ptr; - - struct Dir { - Dir(const QString &l, const QString &r) : localDir(l), remoteDir(r) {} - QString localDir; - QString remoteDir; - }; - - SftpUploadDir(SftpJobId jobId) : jobId(jobId), hasError(false) {} - ~SftpUploadDir(); - - void setError() - { - hasError = true; - uploadsInProgress.clear(); - mkdirsInProgress.clear(); - } - - const SftpJobId jobId; - bool hasError; - QList uploadsInProgress; - QMap mkdirsInProgress; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SFTPOPERATION_P_H diff --git a/src/libs/utils/ssh/sftpoutgoingpacket.cpp b/src/libs/utils/ssh/sftpoutgoingpacket.cpp deleted file mode 100644 index 38412e6643..0000000000 --- a/src/libs/utils/ssh/sftpoutgoingpacket.cpp +++ /dev/null @@ -1,229 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sftpoutgoingpacket_p.h" - -#include "sshpacket_p.h" - -#include - -#include - -namespace Utils { -namespace Internal { - -namespace { - const quint32 DefaultAttributes = 0; - const quint32 SSH_FXF_READ = 0x00000001; - const quint32 SSH_FXF_WRITE = 0x00000002; - const quint32 SSH_FXF_APPEND = 0x00000004; - const quint32 SSH_FXF_CREAT = 0x00000008; - const quint32 SSH_FXF_TRUNC = 0x00000010; - const quint32 SSH_FXF_EXCL = 0x00000020; -} - -SftpOutgoingPacket::SftpOutgoingPacket() -{ -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateInit(quint32 version) -{ - return init(SSH_FXP_INIT, 0).appendInt(version).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateStat(const QString &path, quint32 requestId) -{ - return init(SSH_FXP_LSTAT, requestId).appendString(path).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateOpenDir(const QString &path, - quint32 requestId) -{ - return init(SSH_FXP_OPENDIR, requestId).appendString(path).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateReadDir(const QByteArray &handle, - quint32 requestId) -{ - return init(SSH_FXP_READDIR, requestId).appendString(handle).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateCloseHandle(const QByteArray &handle, - quint32 requestId) -{ - return init(SSH_FXP_CLOSE, requestId).appendString(handle).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateMkDir(const QString &path, - quint32 requestId) -{ - return init(SSH_FXP_MKDIR, requestId).appendString(path) - .appendInt(DefaultAttributes).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateRmDir(const QString &path, - quint32 requestId) -{ - return init(SSH_FXP_RMDIR, requestId).appendString(path).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateRm(const QString &path, - quint32 requestId) -{ - return init(SSH_FXP_REMOVE, requestId).appendString(path).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateRename(const QString &oldPath, - const QString &newPath, quint32 requestId) -{ - return init(SSH_FXP_RENAME, requestId).appendString(oldPath) - .appendString(newPath).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFileForWriting(const QString &path, - SftpOverwriteMode mode, quint32 permissions, quint32 requestId) -{ - QList attributes; - if (permissions != DefaultPermissions) - attributes << SSH_FILEXFER_ATTR_PERMISSIONS << permissions; - else - attributes << DefaultAttributes; - return generateOpenFile(path, Write, mode, attributes, requestId); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFileForReading(const QString &path, - quint32 requestId) -{ - // Note: Overwrite mode is irrelevant and will be ignored. - return generateOpenFile(path, Read, SftpSkipExisting, QList() << DefaultAttributes, - requestId); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateReadFile(const QByteArray &handle, - quint64 offset, quint32 length, quint32 requestId) -{ - return init(SSH_FXP_READ, requestId).appendString(handle).appendInt64(offset) - .appendInt(length).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateFstat(const QByteArray &handle, - quint32 requestId) -{ - return init(SSH_FXP_FSTAT, requestId).appendString(handle).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateWriteFile(const QByteArray &handle, - quint64 offset, const QByteArray &data, quint32 requestId) -{ - return init(SSH_FXP_WRITE, requestId).appendString(handle) - .appendInt64(offset).appendString(data).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateCreateLink(const QString &filePath, - const QString &target, quint32 requestId) -{ - return init(SSH_FXP_SYMLINK, requestId).appendString(filePath).appendString(target).finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::generateOpenFile(const QString &path, - OpenType openType, SftpOverwriteMode mode, const QList &attributes, quint32 requestId) -{ - quint32 pFlags; - switch (openType) { - case Read: - pFlags = SSH_FXF_READ; - break; - case Write: - pFlags = SSH_FXF_WRITE | SSH_FXF_CREAT; - switch (mode) { - case SftpOverwriteExisting: pFlags |= SSH_FXF_TRUNC; break; - case SftpAppendToExisting: pFlags |= SSH_FXF_APPEND; break; - case SftpSkipExisting: pFlags |= SSH_FXF_EXCL; break; - } - break; - } - - init(SSH_FXP_OPEN, requestId).appendString(path).appendInt(pFlags); - foreach (const quint32 attribute, attributes) - appendInt(attribute); - return finalize(); -} - -SftpOutgoingPacket &SftpOutgoingPacket::init(SftpPacketType type, - quint32 requestId) -{ - m_data.resize(TypeOffset + 1); - m_data[TypeOffset] = type; - if (type != SSH_FXP_INIT) { - appendInt(requestId); -#ifdef CREATOR_SSH_DEBUG - qDebug("Generating SFTP packet of type %d with request id %u", type, - requestId); -#endif - } - return *this; -} - -SftpOutgoingPacket &SftpOutgoingPacket::appendInt(quint32 val) -{ - m_data.append(AbstractSshPacket::encodeInt(val)); - return *this; -} - -SftpOutgoingPacket &SftpOutgoingPacket::appendInt64(quint64 value) -{ - m_data.append(AbstractSshPacket::encodeInt(value)); - return *this; -} - -SftpOutgoingPacket &SftpOutgoingPacket::appendString(const QString &string) -{ - m_data.append(AbstractSshPacket::encodeString(string.toUtf8())); - return *this; -} - -SftpOutgoingPacket &SftpOutgoingPacket::appendString(const QByteArray &string) -{ - m_data += AbstractSshPacket::encodeString(string); - return *this; -} - -SftpOutgoingPacket &SftpOutgoingPacket::finalize() -{ - AbstractSshPacket::setLengthField(m_data); - return *this; -} - -const quint32 SftpOutgoingPacket::DefaultPermissions = std::numeric_limits::max(); - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sftpoutgoingpacket_p.h b/src/libs/utils/ssh/sftpoutgoingpacket_p.h deleted file mode 100644 index d58128d89a..0000000000 --- a/src/libs/utils/ssh/sftpoutgoingpacket_p.h +++ /dev/null @@ -1,94 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SFTPOUTGOINGPACKET_P_H -#define SFTPOUTGOINGPACKET_P_H - -#include "sftppacket_p.h" -#include "sftpdefs.h" - -namespace Utils { -namespace Internal { - -class SftpOutgoingPacket : public AbstractSftpPacket -{ -public: - SftpOutgoingPacket(); - SftpOutgoingPacket &generateInit(quint32 version); - SftpOutgoingPacket &generateStat(const QString &path, quint32 requestId); - SftpOutgoingPacket &generateOpenDir(const QString &path, quint32 requestId); - SftpOutgoingPacket &generateReadDir(const QByteArray &handle, - quint32 requestId); - SftpOutgoingPacket &generateCloseHandle(const QByteArray &handle, - quint32 requestId); - SftpOutgoingPacket &generateMkDir(const QString &path, quint32 requestId); - SftpOutgoingPacket &generateRmDir(const QString &path, quint32 requestId); - SftpOutgoingPacket &generateRm(const QString &path, quint32 requestId); - SftpOutgoingPacket &generateRename(const QString &oldPath, - const QString &newPath, quint32 requestId); - SftpOutgoingPacket &generateOpenFileForWriting(const QString &path, - SftpOverwriteMode mode, quint32 permissions, quint32 requestId); - SftpOutgoingPacket &generateOpenFileForReading(const QString &path, - quint32 requestId); - SftpOutgoingPacket &generateReadFile(const QByteArray &handle, - quint64 offset, quint32 length, quint32 requestId); - SftpOutgoingPacket &generateFstat(const QByteArray &handle, - quint32 requestId); - SftpOutgoingPacket &generateWriteFile(const QByteArray &handle, - quint64 offset, const QByteArray &data, quint32 requestId); - - // Note: OpenSSH's SFTP server has a bug that reverses the filePath and target - // arguments, so this operation is not portable. - SftpOutgoingPacket &generateCreateLink(const QString &filePath, const QString &target, - quint32 requestId); - - static const quint32 DefaultPermissions; - -private: - static QByteArray encodeString(const QString &string); - - enum OpenType { Read, Write }; - SftpOutgoingPacket &generateOpenFile(const QString &path, OpenType openType, - SftpOverwriteMode mode, const QList &attributes, quint32 requestId); - - SftpOutgoingPacket &init(SftpPacketType type, quint32 requestId); - SftpOutgoingPacket &appendInt(quint32 value); - SftpOutgoingPacket &appendInt64(quint64 value); - SftpOutgoingPacket &appendString(const QString &string); - SftpOutgoingPacket &appendString(const QByteArray &string); - SftpOutgoingPacket &finalize(); -}; - -} // namespace Internal -} // namespace Utils - -#endif // SFTPOUTGOINGPACKET_P_H diff --git a/src/libs/utils/ssh/sftppacket.cpp b/src/libs/utils/ssh/sftppacket.cpp deleted file mode 100644 index 1ea448e2ac..0000000000 --- a/src/libs/utils/ssh/sftppacket.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sftppacket_p.h" - -#include "sshpacketparser_p.h" - -namespace Utils { -namespace Internal { - -const quint32 AbstractSftpPacket::MaxDataSize = 32000; -const quint32 AbstractSftpPacket::MaxPacketSize = 34000; -const int AbstractSftpPacket::TypeOffset = 4; -const int AbstractSftpPacket::RequestIdOffset = TypeOffset + 1; -const int AbstractSftpPacket::PayloadOffset = RequestIdOffset + 4; - -AbstractSftpPacket::AbstractSftpPacket() -{ -} - -quint32 AbstractSftpPacket::requestId() const -{ - return SshPacketParser::asUint32(m_data, RequestIdOffset); -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sftppacket_p.h b/src/libs/utils/ssh/sftppacket_p.h deleted file mode 100644 index 9666ef1e11..0000000000 --- a/src/libs/utils/ssh/sftppacket_p.h +++ /dev/null @@ -1,119 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SFTPPACKET_P_H -#define SFTPPACKET_P_H - -#include -#include -#include - -namespace Utils { -namespace Internal { - -enum SftpPacketType { - SSH_FXP_INIT = 1, - SSH_FXP_VERSION = 2, - SSH_FXP_OPEN = 3, - SSH_FXP_CLOSE = 4, - SSH_FXP_READ = 5, - SSH_FXP_WRITE = 6, - SSH_FXP_LSTAT = 7, - SSH_FXP_FSTAT = 8, - SSH_FXP_SETSTAT = 9, - SSH_FXP_FSETSTAT = 10, - SSH_FXP_OPENDIR = 11, - SSH_FXP_READDIR = 12, - SSH_FXP_REMOVE = 13, - SSH_FXP_MKDIR = 14, - SSH_FXP_RMDIR = 15, - SSH_FXP_REALPATH = 16, - SSH_FXP_STAT = 17, - SSH_FXP_RENAME = 18, - SSH_FXP_READLINK = 19, - SSH_FXP_SYMLINK = 20, // Removed from later protocol versions. Try not to use. - - SSH_FXP_STATUS = 101, - SSH_FXP_HANDLE = 102, - SSH_FXP_DATA = 103, - SSH_FXP_NAME = 104, - SSH_FXP_ATTRS = 105, - - SSH_FXP_EXTENDED = 200, - SSH_FXP_EXTENDED_REPLY = 201 -}; - -enum SftpStatusCode { - SSH_FX_OK = 0, - SSH_FX_EOF = 1, - SSH_FX_NO_SUCH_FILE = 2, - SSH_FX_PERMISSION_DENIED = 3, - SSH_FX_FAILURE = 4, - SSH_FX_BAD_MESSAGE = 5, - SSH_FX_NO_CONNECTION = 6, - SSH_FX_CONNECTION_LOST = 7, - SSH_FX_OP_UNSUPPORTED = 8 -}; - -enum SftpAttributeType { - SSH_FILEXFER_ATTR_SIZE = 0x00000001, - SSH_FILEXFER_ATTR_UIDGID = 0x00000002, - SSH_FILEXFER_ATTR_PERMISSIONS = 0x00000004, - SSH_FILEXFER_ATTR_ACMODTIME = 0x00000008, - SSH_FILEXFER_ATTR_EXTENDED = 0x80000000 -}; - -class AbstractSftpPacket -{ -public: - AbstractSftpPacket(); - quint32 requestId() const; - const QByteArray &rawData() const { return m_data; } - SftpPacketType type() const { return static_cast(m_data.at(TypeOffset)); } - - static const quint32 MaxDataSize; // "Pure" data size per read/writepacket. - static const quint32 MaxPacketSize; - -protected: - quint32 dataSize() const { return static_cast(m_data.size()); } - - static const int TypeOffset; - static const int RequestIdOffset; - static const int PayloadOffset; - - QByteArray m_data; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SFTPPACKET_P_H diff --git a/src/libs/utils/ssh/sshbotanconversions_p.h b/src/libs/utils/ssh/sshbotanconversions_p.h deleted file mode 100644 index 60c9ed8a65..0000000000 --- a/src/libs/utils/ssh/sshbotanconversions_p.h +++ /dev/null @@ -1,102 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef BYTEARRAYCONVERSIONS_P_H -#define BYTEARRAYCONVERSIONS_P_H - -#include "sshcapabilities_p.h" - -#include -#include - -namespace Utils { -namespace Internal { - -inline const Botan::byte *convertByteArray(const QByteArray &a) -{ - return reinterpret_cast(a.constData()); -} - -inline Botan::byte *convertByteArray(QByteArray &a) -{ - return reinterpret_cast(a.data()); -} - -inline QByteArray convertByteArray(const Botan::SecureVector &v) -{ - return QByteArray(reinterpret_cast(v.begin()), v.size()); -} - -inline const char *botanKeyExchangeAlgoName(const QByteArray &rfcAlgoName) -{ - Q_ASSERT(rfcAlgoName == SshCapabilities::DiffieHellmanGroup1Sha1 - || rfcAlgoName == SshCapabilities::DiffieHellmanGroup14Sha1); - return rfcAlgoName == SshCapabilities::DiffieHellmanGroup1Sha1 - ? "modp/ietf/1024" : "modp/ietf/2048"; -} - -inline const char *botanCryptAlgoName(const QByteArray &rfcAlgoName) -{ - Q_ASSERT(rfcAlgoName == SshCapabilities::CryptAlgo3Des - || rfcAlgoName == SshCapabilities::CryptAlgoAes128); - return rfcAlgoName == SshCapabilities::CryptAlgo3Des - ? "TripleDES" : "AES-128"; -} - -inline const char *botanEmsaAlgoName(const QByteArray &rfcAlgoName) -{ - Q_ASSERT(rfcAlgoName == SshCapabilities::PubKeyDss - || rfcAlgoName == SshCapabilities::PubKeyRsa); - return rfcAlgoName == SshCapabilities::PubKeyDss - ? "EMSA1(SHA-1)" : "EMSA3(SHA-1)"; -} - -inline const char *botanSha1Name() { return "SHA-1"; } - -inline const char *botanHMacAlgoName(const QByteArray &rfcAlgoName) -{ - Q_ASSERT(rfcAlgoName == SshCapabilities::HMacSha1); - Q_UNUSED(rfcAlgoName); - return botanSha1Name(); -} - -inline quint32 botanHMacKeyLen(const QByteArray &rfcAlgoName) -{ - Q_ASSERT(rfcAlgoName == SshCapabilities::HMacSha1); - Q_UNUSED(rfcAlgoName); - return 20; -} - -} // namespace Internal -} // namespace Utils - -#endif // BYTEARRAYCONVERSIONS_P_H diff --git a/src/libs/utils/ssh/sshcapabilities.cpp b/src/libs/utils/ssh/sshcapabilities.cpp deleted file mode 100644 index a5aa08ea77..0000000000 --- a/src/libs/utils/ssh/sshcapabilities.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshcapabilities_p.h" - -#include "sshexception_p.h" - -#include -#include - -namespace Utils { -namespace Internal { - -namespace { - QByteArray listAsByteArray(const QList &list) - { - QByteArray array; - foreach(const QByteArray &elem, list) - array += elem + ','; - if (!array.isEmpty()) - array.remove(array.count() - 1, 1); - return array; - } -} // anonymous namspace - -const QByteArray SshCapabilities::DiffieHellmanGroup1Sha1("diffie-hellman-group1-sha1"); -const QByteArray SshCapabilities::DiffieHellmanGroup14Sha1("diffie-hellman-group14-sha1"); -const QList SshCapabilities::KeyExchangeMethods - = QList() << SshCapabilities::DiffieHellmanGroup1Sha1 - << SshCapabilities::DiffieHellmanGroup14Sha1; - -const QByteArray SshCapabilities::PubKeyDss("ssh-dss"); -const QByteArray SshCapabilities::PubKeyRsa("ssh-rsa"); -const QList SshCapabilities::PublicKeyAlgorithms - = QList() << SshCapabilities::PubKeyRsa - << SshCapabilities::PubKeyDss; - -const QByteArray SshCapabilities::CryptAlgo3Des("3des-cbc"); -const QByteArray SshCapabilities::CryptAlgoAes128("aes128-cbc"); -const QList SshCapabilities::EncryptionAlgorithms - = QList() << SshCapabilities::CryptAlgoAes128 - << SshCapabilities::CryptAlgo3Des; - -const QByteArray SshCapabilities::HMacSha1("hmac-sha1"); -const QByteArray SshCapabilities::HMacSha196("hmac-sha1-96"); -const QList SshCapabilities::MacAlgorithms - = QList() /* << SshCapabilities::HMacSha196 */ - << SshCapabilities::HMacSha1; - -const QList SshCapabilities::CompressionAlgorithms - = QList() << "none"; - -const QByteArray SshCapabilities::SshConnectionService("ssh-connection"); - -const QByteArray SshCapabilities::PublicKeyAuthMethod("publickey"); -const QByteArray SshCapabilities::PasswordAuthMethod("password"); - - -QByteArray SshCapabilities::findBestMatch(const QList &myCapabilities, - const QList &serverCapabilities) -{ - foreach (const QByteArray &myCapability, myCapabilities) { - if (serverCapabilities.contains(myCapability)) - return myCapability; - } - - throw SshServerException(SSH_DISCONNECT_KEY_EXCHANGE_FAILED, - "Server and client capabilities do not match.", - QCoreApplication::translate("SshConnection", - "Server and client capabilities don't match. " - "Client list was: %1.\nServer list was %2.") - .arg(QString::fromLocal8Bit(listAsByteArray(myCapabilities).data())) - .arg(QString::fromLocal8Bit(listAsByteArray(serverCapabilities).data()))); -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshcapabilities_p.h b/src/libs/utils/ssh/sshcapabilities_p.h deleted file mode 100644 index cf322e6711..0000000000 --- a/src/libs/utils/ssh/sshcapabilities_p.h +++ /dev/null @@ -1,75 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef CAPABILITIES_P_H -#define CAPABILITIES_P_H - -#include -#include - -namespace Utils { -namespace Internal { - -class SshCapabilities -{ -public: - static const QByteArray DiffieHellmanGroup1Sha1; - static const QByteArray DiffieHellmanGroup14Sha1; - static const QList KeyExchangeMethods; - - static const QByteArray PubKeyDss; - static const QByteArray PubKeyRsa; - static const QList PublicKeyAlgorithms; - - static const QByteArray CryptAlgo3Des; - static const QByteArray CryptAlgoAes128; - static const QList EncryptionAlgorithms; - - static const QByteArray HMacSha1; - static const QByteArray HMacSha196; - static const QList MacAlgorithms; - - static const QList CompressionAlgorithms; - - static const QByteArray SshConnectionService; - - static const QByteArray PublicKeyAuthMethod; - static const QByteArray PasswordAuthMethod; - - static QByteArray findBestMatch(const QList &myCapabilities, - const QList &serverCapabilities); -}; - -} // namespace Internal -} // namespace Utils - -#endif // CAPABILITIES_P_H diff --git a/src/libs/utils/ssh/sshchannel.cpp b/src/libs/utils/ssh/sshchannel.cpp deleted file mode 100644 index 80e564c5cb..0000000000 --- a/src/libs/utils/ssh/sshchannel.cpp +++ /dev/null @@ -1,263 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshchannel_p.h" - -#include "sshincomingpacket_p.h" -#include "sshsendfacility_p.h" - -#include - -#include - -namespace Utils { -namespace Internal { - -namespace { - const quint32 MinMaxPacketSize = 32768; - const quint32 MaxPacketSize = 16 * 1024 * 1024; - const quint32 InitialWindowSize = MaxPacketSize; - const quint32 NoChannel = 0xffffffffu; -} // anonymous namespace - -AbstractSshChannel::AbstractSshChannel(quint32 channelId, - SshSendFacility &sendFacility) - : m_sendFacility(sendFacility), m_timeoutTimer(new QTimer(this)), - m_localChannel(channelId), m_remoteChannel(NoChannel), - m_localWindowSize(InitialWindowSize), m_remoteWindowSize(0), - m_state(Inactive) -{ - m_timeoutTimer->setSingleShot(true); - connect(m_timeoutTimer, SIGNAL(timeout()), this, SIGNAL(timeout())); -} - -AbstractSshChannel::~AbstractSshChannel() -{ - -} - -void AbstractSshChannel::setChannelState(ChannelState state) -{ - m_state = state; - if (state == Closed) - closeHook(); -} - -void AbstractSshChannel::requestSessionStart() -{ - // Note: We are just being paranoid here about the Botan exceptions, - // which are extremely unlikely to happen, because if there was a problem - // with our cryptography stuff, it would have hit us before, on - // establishing the connection. - try { - m_sendFacility.sendSessionPacket(m_localChannel, InitialWindowSize, - MaxPacketSize); - setChannelState(SessionRequested); - m_timeoutTimer->start(ReplyTimeout); - } catch (Botan::Exception &e) { - qDebug("Botan error: %s", e.what()); - closeChannel(); - } -} - -void AbstractSshChannel::sendData(const QByteArray &data) -{ - try { - m_sendBuffer += data; - flushSendBuffer(); - } catch (Botan::Exception &e) { - qDebug("Botan error: %s", e.what()); - closeChannel(); - } -} - -void AbstractSshChannel::handleWindowAdjust(quint32 bytesToAdd) -{ - checkChannelActive(); - - const quint64 newValue = m_remoteWindowSize + bytesToAdd; - if (newValue > 0xffffffffu) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Illegal window size requested."); - } - - m_remoteWindowSize = newValue; - flushSendBuffer(); -} - -void AbstractSshChannel::flushSendBuffer() -{ - const quint32 bytesToSend = qMin(m_remoteMaxPacketSize, - qMin(m_remoteWindowSize, m_sendBuffer.size())); - if (bytesToSend > 0) { - const QByteArray &data = m_sendBuffer.left(bytesToSend); - m_sendFacility.sendChannelDataPacket(m_remoteChannel, data); - m_sendBuffer.remove(0, bytesToSend); - m_remoteWindowSize -= bytesToSend; - } -} - -void AbstractSshChannel::handleOpenSuccess(quint32 remoteChannelId, - quint32 remoteWindowSize, quint32 remoteMaxPacketSize) -{ - if (m_state != SessionRequested) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_MSG_CHANNEL_OPEN_CONFIRMATION packet."); - } - m_timeoutTimer->stop(); - - if (remoteMaxPacketSize < MinMaxPacketSize) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Maximum packet size too low."); - } - -#ifdef CREATOR_SSH_DEBUG - qDebug("Channel opened. remote channel id: %u, remote window size: %u, " - "remote max packet size: %u", - remoteChannelId, remoteWindowSize, remoteMaxPacketSize); -#endif - m_remoteChannel = remoteChannelId; - m_remoteWindowSize = remoteWindowSize; - m_remoteMaxPacketSize = remoteMaxPacketSize - sizeof(quint32) - sizeof m_remoteChannel - 1; - // Original value includes packet type, channel number and length field for string. - setChannelState(SessionEstablished); - handleOpenSuccessInternal(); -} - -void AbstractSshChannel::handleOpenFailure(const QString &reason) -{ - if (m_state != SessionRequested) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_MSG_CHANNEL_OPEN_FAILURE packet."); - } - m_timeoutTimer->stop(); - -#ifdef CREATOR_SSH_DEBUG - qDebug("Channel open request failed for channel %u", m_localChannel); -#endif - handleOpenFailureInternal(reason); -} - -void AbstractSshChannel::handleChannelEof() -{ - if (m_state == Inactive || m_state == Closed) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_MSG_CHANNEL_EOF message."); - } - m_localWindowSize = 0; -} - -void AbstractSshChannel::handleChannelClose() -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("Receiving CLOSE for channel %u", m_localChannel); -#endif - if (channelState() == Inactive || channelState() == Closed) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_MSG_CHANNEL_CLOSE message."); - } - closeChannel(); - setChannelState(Closed); -} - -void AbstractSshChannel::handleChannelData(const QByteArray &data) -{ - const int bytesToDeliver = handleChannelOrExtendedChannelData(data); - handleChannelDataInternal(bytesToDeliver == data.size() - ? data : data.left(bytesToDeliver)); -} - -void AbstractSshChannel::handleChannelExtendedData(quint32 type, const QByteArray &data) -{ - const int bytesToDeliver = handleChannelOrExtendedChannelData(data); - handleChannelExtendedDataInternal(type, bytesToDeliver == data.size() - ? data : data.left(bytesToDeliver)); -} - -void AbstractSshChannel::handleChannelRequest(const SshIncomingPacket &packet) -{ - checkChannelActive(); - const QByteArray &requestType = packet.extractChannelRequestType(); - if (requestType == SshIncomingPacket::ExitStatusType) - handleExitStatus(packet.extractChannelExitStatus()); - else if (requestType == SshIncomingPacket::ExitSignalType) - handleExitSignal(packet.extractChannelExitSignal()); - else if (requestType != "eow@openssh.com") // Suppress warning for this one, as it's sent all the time. - qWarning("Ignoring unknown request type '%s'", requestType.data()); -} - -int AbstractSshChannel::handleChannelOrExtendedChannelData(const QByteArray &data) -{ - checkChannelActive(); - - const int bytesToDeliver = qMin(data.size(), maxDataSize()); - if (bytesToDeliver != data.size()) - qWarning("Misbehaving server does not respect local window, clipping."); - - m_localWindowSize -= bytesToDeliver; - if (m_localWindowSize < MaxPacketSize) { - m_localWindowSize += MaxPacketSize; - m_sendFacility.sendWindowAdjustPacket(m_remoteChannel, - MaxPacketSize); - } - return bytesToDeliver; -} - -void AbstractSshChannel::closeChannel() -{ - if (m_state == CloseRequested) { - m_timeoutTimer->stop(); - } else if (m_state != Closed) { - if (m_state == Inactive) { - setChannelState(Closed); - } else { - setChannelState(CloseRequested); - m_sendFacility.sendChannelEofPacket(m_remoteChannel); - m_sendFacility.sendChannelClosePacket(m_remoteChannel); - } - } -} - -void AbstractSshChannel::checkChannelActive() -{ - if (channelState() == Inactive || channelState() == Closed) - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Channel not open."); -} - -quint32 AbstractSshChannel::maxDataSize() const -{ - return qMin(m_localWindowSize, MaxPacketSize); -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshchannel_p.h b/src/libs/utils/ssh/sshchannel_p.h deleted file mode 100644 index 89e43d032b..0000000000 --- a/src/libs/utils/ssh/sshchannel_p.h +++ /dev/null @@ -1,124 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHCHANNEL_P_H -#define SSHCHANNEL_P_H - -#include -#include -#include - -QT_FORWARD_DECLARE_CLASS(QTimer) - -namespace Utils { -namespace Internal { - -struct SshChannelExitSignal; -struct SshChannelExitStatus; -class SshIncomingPacket; -class SshSendFacility; - -class AbstractSshChannel : public QObject -{ - Q_OBJECT -public: - enum ChannelState { - Inactive, SessionRequested, SessionEstablished, CloseRequested, Closed - }; - - ChannelState channelState() const { return m_state; } - void setChannelState(ChannelState state); - - quint32 localChannelId() const { return m_localChannel; } - quint32 remoteChannel() const { return m_remoteChannel; } - - virtual void handleChannelSuccess() = 0; - virtual void handleChannelFailure() = 0; - - virtual void closeHook() = 0; - - void handleOpenSuccess(quint32 remoteChannelId, quint32 remoteWindowSize, - quint32 remoteMaxPacketSize); - void handleOpenFailure(const QString &reason); - void handleWindowAdjust(quint32 bytesToAdd); - void handleChannelEof(); - void handleChannelClose(); - void handleChannelData(const QByteArray &data); - void handleChannelExtendedData(quint32 type, const QByteArray &data); - void handleChannelRequest(const SshIncomingPacket &packet); - - void requestSessionStart(); - void sendData(const QByteArray &data); - void closeChannel(); - - virtual ~AbstractSshChannel(); - - static const int ReplyTimeout = 10000; // milli seconds - -signals: - void timeout(); - -protected: - AbstractSshChannel(quint32 channelId, SshSendFacility &sendFacility); - - quint32 maxDataSize() const; - void checkChannelActive(); - - SshSendFacility &m_sendFacility; - QTimer * const m_timeoutTimer; - -private: - virtual void handleOpenSuccessInternal() = 0; - virtual void handleOpenFailureInternal(const QString &reason) = 0; - virtual void handleChannelDataInternal(const QByteArray &data) = 0; - virtual void handleChannelExtendedDataInternal(quint32 type, - const QByteArray &data) = 0; - virtual void handleExitStatus(const SshChannelExitStatus &exitStatus) = 0; - virtual void handleExitSignal(const SshChannelExitSignal &signal) = 0; - - void setState(ChannelState newState); - void flushSendBuffer(); - int handleChannelOrExtendedChannelData(const QByteArray &data); - - const quint32 m_localChannel; - quint32 m_remoteChannel; - quint32 m_localWindowSize; - quint32 m_remoteWindowSize; - quint32 m_remoteMaxPacketSize; - ChannelState m_state; - QByteArray m_sendBuffer; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHCHANNEL_P_H diff --git a/src/libs/utils/ssh/sshchannelmanager.cpp b/src/libs/utils/ssh/sshchannelmanager.cpp deleted file mode 100644 index 3ce488e2bc..0000000000 --- a/src/libs/utils/ssh/sshchannelmanager.cpp +++ /dev/null @@ -1,198 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshchannelmanager_p.h" - -#include "sftpchannel.h" -#include "sftpchannel_p.h" -#include "sshincomingpacket_p.h" -#include "sshremoteprocess.h" -#include "sshremoteprocess_p.h" -#include "sshsendfacility_p.h" - -#include - -namespace Utils { -namespace Internal { - -SshChannelManager::SshChannelManager(SshSendFacility &sendFacility, - QObject *parent) - : QObject(parent), m_sendFacility(sendFacility), m_nextLocalChannelId(0) -{ -} - -void SshChannelManager::handleChannelRequest(const SshIncomingPacket &packet) -{ - lookupChannel(packet.extractRecipientChannel()) - ->handleChannelRequest(packet); -} - -void SshChannelManager::handleChannelOpen(const SshIncomingPacket &) -{ - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Server tried to open channel on client."); -} - -void SshChannelManager::handleChannelOpenFailure(const SshIncomingPacket &packet) -{ - const SshChannelOpenFailure &failure = packet.extractChannelOpenFailure(); - ChannelIterator it = lookupChannelAsIterator(failure.localChannel); - try { - it.value()->handleOpenFailure(failure.reasonString); - } catch (SshServerException &e) { - 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); - } -} - -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.", - tr("Invalid channel id %1").arg(channelId)); - } - return it; -} - -AbstractSshChannel *SshChannelManager::lookupChannel(quint32 channelId, - bool allowNotFound) -{ - ChannelIterator it = lookupChannelAsIterator(channelId, allowNotFound); - return it == m_channels.end() ? 0 : it.value(); -} - -Utils::SshRemoteProcess::Ptr SshChannelManager::createRemoteProcess(const QByteArray &command) -{ - SshRemoteProcess::Ptr proc(new SshRemoteProcess(command, m_nextLocalChannelId++, m_sendFacility)); - insertChannel(proc->d, proc); - return proc; -} - -Utils::SshRemoteProcess::Ptr SshChannelManager::createRemoteShell() -{ - SshRemoteProcess::Ptr proc(new SshRemoteProcess(m_nextLocalChannelId++, m_sendFacility)); - insertChannel(proc->d, proc); - return proc; -} - -Utils::SftpChannel::Ptr SshChannelManager::createSftpChannel() -{ - SftpChannel::Ptr sftp(new SftpChannel(m_nextLocalChannelId++, m_sendFacility)); - insertChannel(sftp->d, sftp); - return sftp; -} - -void SshChannelManager::insertChannel(AbstractSshChannel *priv, - const QSharedPointer &pub) -{ - connect(priv, SIGNAL(timeout()), this, SIGNAL(timeout())); - m_channels.insert(priv->localChannelId(), priv); - m_sessions.insert(priv, pub); -} - -void SshChannelManager::closeAllChannels() -{ - for (ChannelIterator it = m_channels.begin(); it != m_channels.end(); ++it) - it.value()->closeChannel(); - m_channels.clear(); - m_sessions.clear(); -} - -void SshChannelManager::removeChannel(ChannelIterator it) -{ - Q_ASSERT(it != m_channels.end() && "Unexpected channel lookup failure."); - const int removeCount = m_sessions.remove(it.value()); - Q_ASSERT(removeCount == 1 && "Session for channel not found."); - m_channels.erase(it); -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshchannelmanager_p.h b/src/libs/utils/ssh/sshchannelmanager_p.h deleted file mode 100644 index ab88206a73..0000000000 --- a/src/libs/utils/ssh/sshchannelmanager_p.h +++ /dev/null @@ -1,97 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHCHANNELLAYER_P_H -#define SSHCHANNELLAYER_P_H - -#include -#include -#include - -namespace Utils { - -class SftpChannel; -class SshRemoteProcess; - -namespace Internal { - -class AbstractSshChannel; -class SshIncomingPacket; -class SshSendFacility; - -class SshChannelManager : public QObject -{ - Q_OBJECT -public: - SshChannelManager(SshSendFacility &sendFacility, QObject *parent); - - QSharedPointer createRemoteProcess(const QByteArray &command); - QSharedPointer createRemoteShell(); - QSharedPointer createSftpChannel(); - void closeAllChannels(); - - void handleChannelRequest(const SshIncomingPacket &packet); - void handleChannelOpen(const SshIncomingPacket &packet); - void handleChannelOpenFailure(const SshIncomingPacket &packet); - void handleChannelOpenConfirmation(const SshIncomingPacket &packet); - void handleChannelSuccess(const SshIncomingPacket &packet); - void handleChannelFailure(const SshIncomingPacket &packet); - void handleChannelWindowAdjust(const SshIncomingPacket &packet); - void handleChannelData(const SshIncomingPacket &packet); - void handleChannelExtendedData(const SshIncomingPacket &packet); - void handleChannelEof(const SshIncomingPacket &packet); - void handleChannelClose(const SshIncomingPacket &packet); - -signals: - void timeout(); - -private: - typedef QHash::Iterator ChannelIterator; - - ChannelIterator lookupChannelAsIterator(quint32 channelId, - bool allowNotFound = false); - AbstractSshChannel *lookupChannel(quint32 channelId, - bool allowNotFound = false); - void removeChannel(ChannelIterator it); - void insertChannel(AbstractSshChannel *priv, - const QSharedPointer &pub); - - SshSendFacility &m_sendFacility; - QHash m_channels; - QHash > m_sessions; - quint32 m_nextLocalChannelId; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHCHANNELLAYER_P_H diff --git a/src/libs/utils/ssh/sshconnection.cpp b/src/libs/utils/ssh/sshconnection.cpp deleted file mode 100644 index a69353801f..0000000000 --- a/src/libs/utils/ssh/sshconnection.cpp +++ /dev/null @@ -1,730 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshconnection.h" -#include "sshconnection_p.h" - -#include "sftpchannel.h" -#include "sshcapabilities_p.h" -#include "sshchannelmanager_p.h" -#include "sshcryptofacility_p.h" -#include "sshexception_p.h" -#include "sshkeyexchange_p.h" - -#include -#include - -#include -#include - -#include -#include -#include -#include -#include - -/*! - \class Utils::SshConnection - - \brief This class provides an SSH connection, implementing protocol version 2.0 - - It can spawn channels for remote execution and SFTP operations (version 3). - It operates asynchronously (non-blocking) and is not thread-safe. -*/ - -namespace Utils { - -namespace { - const QByteArray ClientId("SSH-2.0-QtCreator\r\n"); - - bool staticInitializationsDone = false; - QMutex staticInitMutex; - - void doStaticInitializationsIfNecessary() - { - QMutexLocker locker(&staticInitMutex); - if (!staticInitializationsDone) { - Botan::LibraryInitializer::initialize("thread_safe=true"); - qRegisterMetaType("Utils::SshError"); - qRegisterMetaType("Utils::SftpJobId"); - qRegisterMetaType("Utils::SftpFileInfo"); - qRegisterMetaType >("QList"); - staticInitializationsDone = true; - } - } -} // anonymous namespace - - -SshConnectionParameters::SshConnectionParameters() : - timeout(0), authenticationType(AuthenticationByKey), port(0), proxyType(NoProxy) -{ -} - -static inline bool equals(const SshConnectionParameters &p1, const SshConnectionParameters &p2) -{ - return p1.host == p2.host && p1.userName == p2.userName - && p1.authenticationType == p2.authenticationType - && (p1.authenticationType == SshConnectionParameters::AuthenticationByPassword ? - p1.password == p2.password : p1.privateKeyFile == p2.privateKeyFile) - && p1.timeout == p2.timeout && p1.port == p2.port; -} - -QTCREATOR_UTILS_EXPORT bool operator==(const SshConnectionParameters &p1, const SshConnectionParameters &p2) -{ - return equals(p1, p2); -} - -QTCREATOR_UTILS_EXPORT bool operator!=(const SshConnectionParameters &p1, const SshConnectionParameters &p2) -{ - return !equals(p1, p2); -} - -// TODO: Mechanism for checking the host key. First connection to host: save, later: compare - -SshConnection::Ptr SshConnection::create(const SshConnectionParameters &serverInfo) -{ - doStaticInitializationsIfNecessary(); - return Ptr(new SshConnection(serverInfo)); -} - -SshConnection::SshConnection(const SshConnectionParameters &serverInfo) - : d(new Internal::SshConnectionPrivate(this, serverInfo)) -{ - connect(d, SIGNAL(connected()), this, SIGNAL(connected()), - Qt::QueuedConnection); - connect(d, SIGNAL(dataAvailable(QString)), this, - SIGNAL(dataAvailable(QString)), Qt::QueuedConnection); - connect(d, SIGNAL(disconnected()), this, SIGNAL(disconnected()), - Qt::QueuedConnection); - connect(d, SIGNAL(error(Utils::SshError)), this, - SIGNAL(error(Utils::SshError)), Qt::QueuedConnection); -} - -void SshConnection::connectToHost() -{ - d->connectToHost(); -} - -void SshConnection::disconnectFromHost() -{ - d->closeConnection(Internal::SSH_DISCONNECT_BY_APPLICATION, SshNoError, "", - QString()); -} - -SshConnection::State SshConnection::state() const -{ - switch (d->state()) { - case Internal::SocketUnconnected: - return Unconnected; - case Internal::ConnectionEstablished: - return Connected; - default: - return Connecting; - } -} - -SshError SshConnection::errorState() const -{ - return d->error(); -} - -QString SshConnection::errorString() const -{ - return d->errorString(); -} - -SshConnectionParameters SshConnection::connectionParameters() const -{ - return d->m_connParams; -} - -SshConnectionInfo SshConnection::connectionInfo() const -{ - QTC_ASSERT(state() == Connected, return SshConnectionInfo()); - - return SshConnectionInfo(d->m_socket->localAddress(), d->m_socket->localPort(), - d->m_socket->peerAddress(), d->m_socket->peerPort()); -} - -SshConnection::~SshConnection() -{ - disconnect(); - disconnectFromHost(); - delete d; -} - -QSharedPointer SshConnection::createRemoteProcess(const QByteArray &command) -{ - QTC_ASSERT(state() == Connected, return QSharedPointer()); - return d->createRemoteProcess(command); -} - -QSharedPointer SshConnection::createRemoteShell() -{ - QTC_ASSERT(state() == Connected, return QSharedPointer()); - return d->createRemoteShell(); -} - -QSharedPointer SshConnection::createSftpChannel() -{ - QTC_ASSERT(state() == Connected, return QSharedPointer()); - return d->createSftpChannel(); -} - - -namespace Internal { - -SshConnectionPrivate::SshConnectionPrivate(SshConnection *conn, - const SshConnectionParameters &serverInfo) - : m_socket(new QTcpSocket(this)), m_state(SocketUnconnected), - m_sendFacility(m_socket), - m_channelManager(new SshChannelManager(m_sendFacility, this)), - m_connParams(serverInfo), m_error(SshNoError), m_ignoreNextPacket(false), - m_conn(conn) -{ - setupPacketHandlers(); - m_socket->setProxy(m_connParams.proxyType == SshConnectionParameters::DefaultProxy - ? QNetworkProxy::DefaultProxy : QNetworkProxy::NoProxy); - m_timeoutTimer.setSingleShot(true); - m_timeoutTimer.setInterval(m_connParams.timeout * 1000); - m_keepAliveTimer.setSingleShot(true); - m_keepAliveTimer.setInterval(10000); - connect(m_channelManager, SIGNAL(timeout()), this, SLOT(handleTimeout())); -} - -SshConnectionPrivate::~SshConnectionPrivate() -{ - disconnect(); -} - -void SshConnectionPrivate::setupPacketHandlers() -{ - typedef SshConnectionPrivate This; - - setupPacketHandler(SSH_MSG_KEXINIT, StateList() << SocketConnected - << ConnectionEstablished, &This::handleKeyExchangeInitPacket); - setupPacketHandler(SSH_MSG_KEXDH_REPLY, StateList() << SocketConnected - << ConnectionEstablished, &This::handleKeyExchangeReplyPacket); - - setupPacketHandler(SSH_MSG_NEWKEYS, StateList() << SocketConnected - << ConnectionEstablished, &This::handleNewKeysPacket); - setupPacketHandler(SSH_MSG_SERVICE_ACCEPT, - StateList() << UserAuthServiceRequested, - &This::handleServiceAcceptPacket); - setupPacketHandler(SSH_MSG_USERAUTH_PASSWD_CHANGEREQ, - StateList() << UserAuthRequested, &This::handlePasswordExpiredPacket); - setupPacketHandler(SSH_MSG_GLOBAL_REQUEST, - StateList() << ConnectionEstablished, &This::handleGlobalRequest); - - const StateList authReqList = StateList() << UserAuthRequested; - setupPacketHandler(SSH_MSG_USERAUTH_BANNER, authReqList, - &This::handleUserAuthBannerPacket); - setupPacketHandler(SSH_MSG_USERAUTH_SUCCESS, authReqList, - &This::handleUserAuthSuccessPacket); - setupPacketHandler(SSH_MSG_USERAUTH_FAILURE, authReqList, - &This::handleUserAuthFailurePacket); - - const StateList connectedList - = StateList() << ConnectionEstablished; - setupPacketHandler(SSH_MSG_CHANNEL_REQUEST, connectedList, - &This::handleChannelRequest); - setupPacketHandler(SSH_MSG_CHANNEL_OPEN, connectedList, - &This::handleChannelOpen); - setupPacketHandler(SSH_MSG_CHANNEL_OPEN_FAILURE, connectedList, - &This::handleChannelOpenFailure); - setupPacketHandler(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, connectedList, - &This::handleChannelOpenConfirmation); - setupPacketHandler(SSH_MSG_CHANNEL_SUCCESS, connectedList, - &This::handleChannelSuccess); - setupPacketHandler(SSH_MSG_CHANNEL_FAILURE, connectedList, - &This::handleChannelFailure); - setupPacketHandler(SSH_MSG_CHANNEL_WINDOW_ADJUST, connectedList, - &This::handleChannelWindowAdjust); - setupPacketHandler(SSH_MSG_CHANNEL_DATA, connectedList, - &This::handleChannelData); - setupPacketHandler(SSH_MSG_CHANNEL_EXTENDED_DATA, connectedList, - &This::handleChannelExtendedData); - - const StateList connectedOrClosedList - = StateList() << SocketUnconnected << ConnectionEstablished; - setupPacketHandler(SSH_MSG_CHANNEL_EOF, connectedOrClosedList, - &This::handleChannelEof); - setupPacketHandler(SSH_MSG_CHANNEL_CLOSE, connectedOrClosedList, - &This::handleChannelClose); - - setupPacketHandler(SSH_MSG_DISCONNECT, StateList() << SocketConnected - << UserAuthServiceRequested << UserAuthRequested - << ConnectionEstablished, &This::handleDisconnect); - - setupPacketHandler(SSH_MSG_UNIMPLEMENTED, - StateList() << ConnectionEstablished, &This::handleUnimplementedPacket); -} - -void SshConnectionPrivate::setupPacketHandler(SshPacketType type, - const SshConnectionPrivate::StateList &states, - SshConnectionPrivate::PacketHandler handler) -{ - m_packetHandlers.insert(type, HandlerInStates(states, handler)); -} - -void SshConnectionPrivate::handleSocketConnected() -{ - m_state = SocketConnected; - sendData(ClientId); -} - -void SshConnectionPrivate::handleIncomingData() -{ - if (m_state == SocketUnconnected) - return; // For stuff queued in the event loop after we've called closeConnection(); - - try { - if (!canUseSocket()) - return; - m_incomingData += m_socket->readAll(); -#ifdef CREATOR_SSH_DEBUG - qDebug("state = %d, remote data size = %d", m_state, - m_incomingData.count()); -#endif - if (m_state == SocketConnected) - handleServerId(); - handlePackets(); - } catch (SshServerException &e) { - closeConnection(e.error, SshProtocolError, e.errorStringServer, - tr("SSH Protocol error: %1").arg(e.errorStringUser)); - } catch (SshClientException &e) { - closeConnection(SSH_DISCONNECT_BY_APPLICATION, e.error, "", - e.errorString); - } catch (Botan::Exception &e) { - closeConnection(SSH_DISCONNECT_BY_APPLICATION, SshInternalError, "", - tr("Botan library exception: %1").arg(QString::fromAscii(e.what()))); - } -} - -void SshConnectionPrivate::handleServerId() -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("%s: incoming data size = %d, incoming data = '%s'", - Q_FUNC_INFO, m_incomingData.count(), m_incomingData.data()); -#endif - const int idOffset = m_incomingData.indexOf("SSH-"); - if (idOffset == -1) - return; - m_incomingData.remove(0, idOffset); - if (m_incomingData.size() < 7) - return; - const QByteArray &version = m_incomingData.mid(4, 3); - if (version != "2.0") { - throw SshServerException(SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED, - "Invalid protocol version.", - tr("Invalid protocol version: Expected '2.0', got '%1'.") - .arg(SshPacketParser::asUserString(version))); - } - const int endOffset = m_incomingData.indexOf("\r\n"); - if (endOffset == -1) - return; - if (m_incomingData.at(7) != '-') { - throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid server id.", tr("Invalid server id '%1'.") - .arg(SshPacketParser::asUserString(m_incomingData))); - } - - m_keyExchange.reset(new SshKeyExchange(m_sendFacility)); - m_serverId = m_incomingData.left(endOffset); - m_keyExchange->sendKexInitPacket(m_serverId); - m_keyExchangeState = KexInitSent; - m_incomingData.remove(0, endOffset + 2); -} - -void SshConnectionPrivate::handlePackets() -{ - m_incomingPacket.consumeData(m_incomingData); - while (m_incomingPacket.isComplete()) { - handleCurrentPacket(); - m_incomingPacket.clear(); - m_incomingPacket.consumeData(m_incomingData); - } -} - -void SshConnectionPrivate::handleCurrentPacket() -{ - Q_ASSERT(m_incomingPacket.isComplete()); - Q_ASSERT(m_keyExchangeState == DhInitSent || !m_ignoreNextPacket); - - if (m_ignoreNextPacket) { - m_ignoreNextPacket = false; - return; - } - - QHash::ConstIterator it - = m_packetHandlers.find(m_incomingPacket.type()); - if (it == m_packetHandlers.end()) { - m_sendFacility.sendMsgUnimplementedPacket(m_incomingPacket.serverSeqNr()); - return; - } - if (!it.value().first.contains(m_state)) { - throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected packet.", tr("Unexpected packet of type %1.") - .arg(m_incomingPacket.type())); - } - (this->*it.value().second)(); -} - -void SshConnectionPrivate::handleKeyExchangeInitPacket() -{ - if (m_keyExchangeState != NoKeyExchange - && m_keyExchangeState != KexInitSent) { - throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected packet.", tr("Unexpected packet of type %1.") - .arg(m_incomingPacket.type())); - } - - // Server-initiated re-exchange. - if (m_keyExchangeState == NoKeyExchange) { - m_keyExchange.reset(new SshKeyExchange(m_sendFacility)); - m_keyExchange->sendKexInitPacket(m_serverId); - } - - // If the server sends a guessed packet, the guess must be wrong, - // because the algorithms we support require us to initiate the - // key exchange. - if (m_keyExchange->sendDhInitPacket(m_incomingPacket)) { - m_ignoreNextPacket = true; - } - - m_keyExchangeState = DhInitSent; -} - -void SshConnectionPrivate::handleKeyExchangeReplyPacket() -{ - if (m_keyExchangeState != DhInitSent) { - throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected packet.", tr("Unexpected packet of type %1.") - .arg(m_incomingPacket.type())); - } - - m_keyExchange->sendNewKeysPacket(m_incomingPacket, - ClientId.left(ClientId.size() - 2)); - m_sendFacility.recreateKeys(*m_keyExchange); - m_keyExchangeState = NewKeysSent; -} - -void SshConnectionPrivate::handleNewKeysPacket() -{ - if (m_keyExchangeState != NewKeysSent) { - throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected packet.", tr("Unexpected packet of type %1.") - .arg(m_incomingPacket.type())); - } - - m_incomingPacket.recreateKeys(*m_keyExchange); - m_keyExchange.reset(); - m_keyExchangeState = NoKeyExchange; - - if (m_state == SocketConnected) { - m_sendFacility.sendUserAuthServiceRequestPacket(); - m_state = UserAuthServiceRequested; - } -} - -void SshConnectionPrivate::handleServiceAcceptPacket() -{ - if (m_connParams.authenticationType == SshConnectionParameters::AuthenticationByPassword) { - m_sendFacility.sendUserAuthByPwdRequestPacket(m_connParams.userName.toUtf8(), - SshCapabilities::SshConnectionService, m_connParams.password.toUtf8()); - } else { - m_sendFacility.sendUserAuthByKeyRequestPacket(m_connParams.userName.toUtf8(), - SshCapabilities::SshConnectionService); - } - m_state = UserAuthRequested; -} - -void SshConnectionPrivate::handlePasswordExpiredPacket() -{ - if (m_connParams.authenticationType == SshConnectionParameters::AuthenticationByKey) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Got SSH_MSG_USERAUTH_PASSWD_CHANGEREQ, but did not use password."); - } - - throw SshClientException(SshAuthenticationError, tr("Password expired.")); -} - -void SshConnectionPrivate::handleUserAuthBannerPacket() -{ - emit dataAvailable(m_incomingPacket.extractUserAuthBanner().message); -} - -void SshConnectionPrivate::handleGlobalRequest() -{ - m_sendFacility.sendRequestFailurePacket(); -} - -void SshConnectionPrivate::handleUserAuthSuccessPacket() -{ - m_state = ConnectionEstablished; - m_timeoutTimer.stop(); - emit connected(); - m_lastInvalidMsgSeqNr = InvalidSeqNr; - connect(&m_keepAliveTimer, SIGNAL(timeout()), SLOT(sendKeepAlivePacket())); - m_keepAliveTimer.start(); -} - -void SshConnectionPrivate::handleUserAuthFailurePacket() -{ - m_timeoutTimer.stop(); - const QString errorMsg = m_connParams.authenticationType == SshConnectionParameters::AuthenticationByPassword - ? tr("Server rejected password.") : tr("Server rejected key."); - throw SshClientException(SshAuthenticationError, errorMsg); -} -void SshConnectionPrivate::handleDebugPacket() -{ - const SshDebug &msg = m_incomingPacket.extractDebug(); - if (msg.display) - emit dataAvailable(msg.message); -} - -void SshConnectionPrivate::handleUnimplementedPacket() -{ - const SshUnimplemented &msg = m_incomingPacket.extractUnimplemented(); - if (msg.invalidMsgSeqNr != m_lastInvalidMsgSeqNr) { - throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected packet", tr("The server sent an unexpected SSH packet " - "of type SSH_MSG_UNIMPLEMENTED.")); - } - m_lastInvalidMsgSeqNr = InvalidSeqNr; - m_timeoutTimer.stop(); - m_keepAliveTimer.start(); -} - -void SshConnectionPrivate::handleChannelRequest() -{ - m_channelManager->handleChannelRequest(m_incomingPacket); -} - -void SshConnectionPrivate::handleChannelOpen() -{ - m_channelManager->handleChannelOpen(m_incomingPacket); -} - -void SshConnectionPrivate::handleChannelOpenFailure() -{ - m_channelManager->handleChannelOpenFailure(m_incomingPacket); -} - -void SshConnectionPrivate::handleChannelOpenConfirmation() -{ - m_channelManager->handleChannelOpenConfirmation(m_incomingPacket); -} - -void SshConnectionPrivate::handleChannelSuccess() -{ - m_channelManager->handleChannelSuccess(m_incomingPacket); -} - -void SshConnectionPrivate::handleChannelFailure() -{ - m_channelManager->handleChannelFailure(m_incomingPacket); -} - -void SshConnectionPrivate::handleChannelWindowAdjust() -{ - m_channelManager->handleChannelWindowAdjust(m_incomingPacket); -} - -void SshConnectionPrivate::handleChannelData() -{ - m_channelManager->handleChannelData(m_incomingPacket); -} - -void SshConnectionPrivate::handleChannelExtendedData() -{ - m_channelManager->handleChannelExtendedData(m_incomingPacket); -} - -void SshConnectionPrivate::handleChannelEof() -{ - m_channelManager->handleChannelEof(m_incomingPacket); -} - -void SshConnectionPrivate::handleChannelClose() -{ - m_channelManager->handleChannelClose(m_incomingPacket); -} - -void SshConnectionPrivate::handleDisconnect() -{ - const SshDisconnect msg = m_incomingPacket.extractDisconnect(); - throw SshServerException(SSH_DISCONNECT_CONNECTION_LOST, - "", tr("Server closed connection: %1").arg(msg.description)); -} - - - -void SshConnectionPrivate::sendData(const QByteArray &data) -{ - if (canUseSocket()) - m_socket->write(data); -} - -void SshConnectionPrivate::handleSocketDisconnected() -{ - closeConnection(SSH_DISCONNECT_CONNECTION_LOST, SshClosedByServerError, - "Connection closed unexpectedly.", - tr("Connection closed unexpectedly.")); -} - -void SshConnectionPrivate::handleSocketError() -{ - if (m_error == SshNoError) { - closeConnection(SSH_DISCONNECT_CONNECTION_LOST, SshSocketError, - "Network error", m_socket->errorString()); - } -} - -void SshConnectionPrivate::handleTimeout() -{ - closeConnection(SSH_DISCONNECT_BY_APPLICATION, SshTimeoutError, "", - tr("Timeout waiting for reply from server.")); -} - -void SshConnectionPrivate::sendKeepAlivePacket() -{ - // This type of message is not allowed during key exchange. - if (m_keyExchangeState != NoKeyExchange) { - m_keepAliveTimer.start(); - return; - } - - Q_ASSERT(m_lastInvalidMsgSeqNr == InvalidSeqNr); - m_lastInvalidMsgSeqNr = m_sendFacility.nextClientSeqNr(); - m_sendFacility.sendInvalidPacket(); - m_timeoutTimer.start(); -} - -void SshConnectionPrivate::connectToHost() -{ - QTC_ASSERT(m_state == SocketUnconnected, return); - - m_incomingData.clear(); - m_incomingPacket.reset(); - m_sendFacility.reset(); - m_error = SshNoError; - m_ignoreNextPacket = false; - m_errorString.clear(); - - try { - if (m_connParams.authenticationType == SshConnectionParameters::AuthenticationByKey) - createPrivateKey(); - } catch (const SshClientException &ex) { - m_error = ex.error; - m_errorString = ex.errorString; - emit error(m_error); - return; - } - - connect(m_socket, SIGNAL(connected()), this, SLOT(handleSocketConnected())); - connect(m_socket, SIGNAL(readyRead()), this, SLOT(handleIncomingData())); - connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, - SLOT(handleSocketError())); - connect(m_socket, SIGNAL(disconnected()), this, - SLOT(handleSocketDisconnected())); - connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(handleTimeout())); - m_state = SocketConnecting; - m_keyExchangeState = NoKeyExchange; - m_timeoutTimer.start(); - m_socket->connectToHost(m_connParams.host, m_connParams.port); -} - -void SshConnectionPrivate::closeConnection(SshErrorCode sshError, - SshError userError, const QByteArray &serverErrorString, - const QString &userErrorString) -{ - // Prevent endless loops by recursive exceptions. - if (m_state == SocketUnconnected || m_error != SshNoError) - return; - - m_error = userError; - m_errorString = userErrorString; - m_timeoutTimer.stop(); - disconnect(m_socket, 0, this, 0); - disconnect(&m_timeoutTimer, 0, this, 0); - m_keepAliveTimer.stop(); - disconnect(&m_keepAliveTimer, 0, this, 0); - try { - m_channelManager->closeAllChannels(); - m_sendFacility.sendDisconnectPacket(sshError, serverErrorString); - } catch (Botan::Exception &) {} // Nothing sensible to be done here. - if (m_error != SshNoError) - emit error(userError); - if (m_state == ConnectionEstablished) - emit disconnected(); - if (canUseSocket()) - m_socket->disconnectFromHost(); - m_state = SocketUnconnected; -} - -bool SshConnectionPrivate::canUseSocket() const -{ - return m_socket->isValid() - && m_socket->state() == QAbstractSocket::ConnectedState; -} - -void SshConnectionPrivate::createPrivateKey() -{ - Utils::FileReader reader; - if (m_connParams.privateKeyFile.isEmpty()) - throw SshClientException(SshKeyFileError, tr("No private key file given.")); - if (!reader.fetch(m_connParams.privateKeyFile)) - throw SshClientException(SshKeyFileError, - tr("Private key file error: %1").arg(reader.errorString())); - m_sendFacility.createAuthenticationKey(reader.data()); -} - -QSharedPointer SshConnectionPrivate::createRemoteProcess(const QByteArray &command) -{ - return m_channelManager->createRemoteProcess(command); -} - -QSharedPointer SshConnectionPrivate::createRemoteShell() -{ - return m_channelManager->createRemoteShell(); -} - -QSharedPointer SshConnectionPrivate::createSftpChannel() -{ - return m_channelManager->createSftpChannel(); -} - -const quint64 SshConnectionPrivate::InvalidSeqNr = static_cast(-1); - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshconnection.h b/src/libs/utils/ssh/sshconnection.h deleted file mode 100644 index 211502f585..0000000000 --- a/src/libs/utils/ssh/sshconnection.h +++ /dev/null @@ -1,124 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHCONNECTION_H -#define SSHCONNECTION_H - -#include "ssherrors.h" - -#include - -#include -#include -#include -#include -#include - -namespace Utils { -class SftpChannel; -class SshRemoteProcess; - -namespace Internal { -class SshConnectionPrivate; -} // namespace Internal - -class QTCREATOR_UTILS_EXPORT SshConnectionParameters -{ -public: - enum ProxyType { DefaultProxy, NoProxy }; - enum AuthenticationType { AuthenticationByPassword, AuthenticationByKey }; - SshConnectionParameters(); - - QString host; - QString userName; - QString password; - QString privateKeyFile; - int timeout; // In seconds. - AuthenticationType authenticationType; - quint16 port; - ProxyType proxyType; -}; - -QTCREATOR_UTILS_EXPORT bool operator==(const SshConnectionParameters &p1, const SshConnectionParameters &p2); -QTCREATOR_UTILS_EXPORT bool operator!=(const SshConnectionParameters &p1, const SshConnectionParameters &p2); - -class QTCREATOR_UTILS_EXPORT SshConnectionInfo -{ -public: - SshConnectionInfo() : localPort(0), peerPort(0) {} - SshConnectionInfo(const QHostAddress &la, quint16 lp, const QHostAddress &pa, quint16 pp) - : localAddress(la), localPort(lp), peerAddress(pa), peerPort(pp) {} - - QHostAddress localAddress; - quint16 localPort; - QHostAddress peerAddress; - quint16 peerPort; -}; - -class QTCREATOR_UTILS_EXPORT SshConnection : public QObject -{ - Q_OBJECT - -public: - enum State { Unconnected, Connecting, Connected }; - typedef QSharedPointer Ptr; - - static Ptr create(const SshConnectionParameters &serverInfo); - - void connectToHost(); - void disconnectFromHost(); - State state() const; - SshError errorState() const; - QString errorString() const; - SshConnectionParameters connectionParameters() const; - SshConnectionInfo connectionInfo() const; - ~SshConnection(); - - QSharedPointer createRemoteProcess(const QByteArray &command); - QSharedPointer createRemoteShell(); - QSharedPointer createSftpChannel(); - -signals: - void connected(); - void disconnected(); - void dataAvailable(const QString &message); - void error(Utils::SshError); - -private: - SshConnection(const SshConnectionParameters &serverInfo); - - Internal::SshConnectionPrivate *d; -}; - -} // namespace Utils - -#endif // SSHCONNECTION_H diff --git a/src/libs/utils/ssh/sshconnection_p.h b/src/libs/utils/ssh/sshconnection_p.h deleted file mode 100644 index 4756e05202..0000000000 --- a/src/libs/utils/ssh/sshconnection_p.h +++ /dev/null @@ -1,177 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHCONNECTION_P_H -#define SSHCONNECTION_P_H - -#include "sshconnection.h" -#include "sshexception_p.h" -#include "sshincomingpacket_p.h" -#include "sshremoteprocess.h" -#include "sshsendfacility_p.h" - -#include -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE -class QTcpSocket; -QT_END_NAMESPACE - -namespace Botan { class Exception; } - -namespace Utils { -class SftpChannel; - -namespace Internal { -class SshChannelManager; - -// NOTE: When you add stuff here, don't forget to update m_packetHandlers. -enum SshStateInternal { - SocketUnconnected, // initial and after disconnect - SocketConnecting, // After connectToHost() - SocketConnected, // After socket's connected() signal - UserAuthServiceRequested, - UserAuthRequested, - ConnectionEstablished // After service has been started - // ... -}; - -enum SshKeyExchangeState { - NoKeyExchange, - KexInitSent, - DhInitSent, - NewKeysSent, - KeyExchangeSuccess // After server's DH_REPLY message -}; - -class SshConnectionPrivate : public QObject -{ - Q_OBJECT - friend class Utils::SshConnection; -public: - SshConnectionPrivate(SshConnection *conn, - const SshConnectionParameters &serverInfo); - ~SshConnectionPrivate(); - - void connectToHost(); - void closeConnection(SshErrorCode sshError, SshError userError, - const QByteArray &serverErrorString, const QString &userErrorString); - QSharedPointer createRemoteProcess(const QByteArray &command); - QSharedPointer createRemoteShell(); - QSharedPointer createSftpChannel(); - SshStateInternal state() const { return m_state; } - SshError error() const { return m_error; } - QString errorString() const { return m_errorString; } - -signals: - void connected(); - void disconnected(); - void dataAvailable(const QString &message); - void error(Utils::SshError); - -private: - Q_SLOT void handleSocketConnected(); - Q_SLOT void handleIncomingData(); - Q_SLOT void handleSocketError(); - Q_SLOT void handleSocketDisconnected(); - Q_SLOT void handleTimeout(); - Q_SLOT void sendKeepAlivePacket(); - - void handleServerId(); - void handlePackets(); - void handleCurrentPacket(); - void handleKeyExchangeInitPacket(); - void handleKeyExchangeReplyPacket(); - void handleNewKeysPacket(); - void handleServiceAcceptPacket(); - void handlePasswordExpiredPacket(); - void handleUserAuthSuccessPacket(); - void handleUserAuthFailurePacket(); - void handleUserAuthBannerPacket(); - void handleGlobalRequest(); - void handleDebugPacket(); - void handleUnimplementedPacket(); - void handleChannelRequest(); - void handleChannelOpen(); - void handleChannelOpenFailure(); - void handleChannelOpenConfirmation(); - void handleChannelSuccess(); - void handleChannelFailure(); - void handleChannelWindowAdjust(); - void handleChannelData(); - void handleChannelExtendedData(); - void handleChannelEof(); - void handleChannelClose(); - void handleDisconnect(); - bool canUseSocket() const; - void createPrivateKey(); - - void sendData(const QByteArray &data); - - typedef void (SshConnectionPrivate::*PacketHandler)(); - typedef QList StateList; - void setupPacketHandlers(); - void setupPacketHandler(SshPacketType type, const StateList &states, - PacketHandler handler); - - typedef QPair HandlerInStates; - QHash m_packetHandlers; - - static const quint64 InvalidSeqNr; - - QTcpSocket *m_socket; - SshStateInternal m_state; - SshKeyExchangeState m_keyExchangeState; - SshIncomingPacket m_incomingPacket; - SshSendFacility m_sendFacility; - SshChannelManager * const m_channelManager; - const SshConnectionParameters m_connParams; - QByteArray m_incomingData; - SshError m_error; - QString m_errorString; - QScopedPointer m_keyExchange; - QTimer m_timeoutTimer; - QTimer m_keepAliveTimer; - bool m_ignoreNextPacket; - SshConnection *m_conn; - quint64 m_lastInvalidMsgSeqNr; - QByteArray m_serverId; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHCONNECTION_P_H diff --git a/src/libs/utils/ssh/sshconnectionmanager.cpp b/src/libs/utils/ssh/sshconnectionmanager.cpp deleted file mode 100644 index 8c1c8cd03e..0000000000 --- a/src/libs/utils/ssh/sshconnectionmanager.cpp +++ /dev/null @@ -1,227 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshconnectionmanager.h" - -#include "sshconnection.h" - -#include -#include -#include -#include -#include -#include - -namespace Utils { -namespace Internal { - -class SshConnectionManagerPrivate : public QObject -{ - Q_OBJECT - -public: - - static QMutex instanceMutex; - static SshConnectionManager &instance() - { - static SshConnectionManager manager; - return manager; - } - - SshConnectionManagerPrivate() - { - moveToThread(QCoreApplication::instance()->thread()); - } - - QSharedPointer acquireConnection(const SshConnectionParameters &sshParams) - { - QMutexLocker locker(&m_listMutex); - - // Check in-use connections: - foreach (SshConnection::Ptr connection, m_acquiredConnections) { - if (connection->connectionParameters() != sshParams) - continue; - - if (connection->thread() != QThread::currentThread()) - break; - - if (m_deprecatedConnections.contains(connection)) // we were asked to no longer use this one... - break; - - m_acquiredConnections.append(connection); - return connection; - } - - // Checked cached open connections: - foreach (SshConnection::Ptr connection, m_unacquiredConnections) { - if (connection->state() != SshConnection::Connected - || connection->connectionParameters() != sshParams) - continue; - - if (connection->thread() != QThread::currentThread()) { - QMetaObject::invokeMethod(this, "switchToCallerThread", - Qt::BlockingQueuedConnection, - Q_ARG(SshConnection *, connection.data()), - Q_ARG(QObject *, QThread::currentThread())); - } - - m_unacquiredConnections.removeOne(connection); - m_acquiredConnections.append(connection); - return connection; - } - - // create a new connection: - SshConnection::Ptr connection = SshConnection::create(sshParams); - connect(connection.data(), SIGNAL(disconnected()), this, SLOT(cleanup())); - m_acquiredConnections.append(connection); - - return connection; - } - - void releaseConnection(const SshConnection::Ptr &connection) - { - QMutexLocker locker(&m_listMutex); - - m_acquiredConnections.removeOne(connection); - if (!m_acquiredConnections.contains(connection)) { - // no longer in use: - connection->moveToThread(QCoreApplication::instance()->thread()); - if (m_deprecatedConnections.contains(connection)) - m_deprecatedConnections.removeAll(connection); - else if (connection->state() == SshConnection::Connected) { - // Make sure to only keep one connection open - bool haveConnection = false; - foreach (SshConnection::Ptr conn, m_unacquiredConnections) { - if (conn->connectionParameters() == connection->connectionParameters()) { - haveConnection = true; - break; - } - } - if (!haveConnection) - m_unacquiredConnections.append(connection); - } - } - } - - void forceNewConnection(const SshConnectionParameters &sshParams) - { - QMutexLocker locker(&m_listMutex); - - SshConnection::Ptr toReset; - foreach (SshConnection::Ptr connection, m_unacquiredConnections) { - if (connection->connectionParameters() == sshParams) { - toReset = connection; - break; - } - } - - if (toReset.isNull()) { - foreach (SshConnection::Ptr connection, m_acquiredConnections) { - if (connection->connectionParameters() == sshParams) { - toReset = connection; - break; - } - } - } - - if (!toReset.isNull() && !m_deprecatedConnections.contains(toReset)) - m_deprecatedConnections.append(toReset); - } - -private: - Q_INVOKABLE void switchToCallerThread(SshConnection *connection, QObject *threadObj) - { - connection->moveToThread(qobject_cast(threadObj)); - } - -private slots: - void cleanup() - { - QMutexLocker locker(&m_listMutex); - - SshConnection *currentConnection = qobject_cast(sender()); - if (!currentConnection) - return; - - for (int i = m_unacquiredConnections.count() - 1; i >= 0; --i) { - if (m_unacquiredConnections.at(i) == currentConnection) - m_unacquiredConnections.removeAt(i); - } - } - -private: - // We expect the number of concurrently open connections to be small. - // If that turns out to not be the case, we can still use a data - // structure with faster access. - QList m_unacquiredConnections; - QList m_acquiredConnections; - QList m_deprecatedConnections; - QMutex m_listMutex; -}; - -QMutex SshConnectionManagerPrivate::instanceMutex; - -} // namespace Internal - -SshConnectionManager &SshConnectionManager::instance() -{ - QMutexLocker locker(&Internal::SshConnectionManagerPrivate::instanceMutex); - return Internal::SshConnectionManagerPrivate::instance(); -} - -SshConnectionManager::SshConnectionManager() - : d(new Internal::SshConnectionManagerPrivate) -{ -} - -SshConnectionManager::~SshConnectionManager() -{ -} - -SshConnection::Ptr SshConnectionManager::acquireConnection(const SshConnectionParameters &sshParams) -{ - return d->acquireConnection(sshParams); -} - -void SshConnectionManager::releaseConnection(const SshConnection::Ptr &connection) -{ - d->releaseConnection(connection); -} - -void SshConnectionManager::forceNewConnection(const SshConnectionParameters &sshParams) -{ - d->forceNewConnection(sshParams); -} - -} // namespace Utils - -#include "sshconnectionmanager.moc" diff --git a/src/libs/utils/ssh/sshconnectionmanager.h b/src/libs/utils/ssh/sshconnectionmanager.h deleted file mode 100644 index 3b26d6c83b..0000000000 --- a/src/libs/utils/ssh/sshconnectionmanager.h +++ /dev/null @@ -1,68 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHCONNECTIONMANAGER_H -#define SSHCONNECTIONMANAGER_H - -#include - -#include -#include - -namespace Utils { -class SshConnection; -class SshConnectionParameters; -namespace Internal { class SshConnectionManagerPrivate; } - -class QTCREATOR_UTILS_EXPORT SshConnectionManager -{ - friend class Internal::SshConnectionManagerPrivate; -public: - static SshConnectionManager &instance(); - - QSharedPointer acquireConnection(const SshConnectionParameters &sshParams); - void releaseConnection(const QSharedPointer &connection); - // Make sure the next acquireConnection with the given parameters will return a new connection. - void forceNewConnection(const SshConnectionParameters &sshParams); - -private: - explicit SshConnectionManager(); - virtual ~SshConnectionManager(); - SshConnectionManager(const SshConnectionManager &); - SshConnectionManager &operator=(const SshConnectionManager &); - - const QScopedPointer d; -}; - -} // namespace Utils - -#endif // SSHCONNECTIONMANAGER_H diff --git a/src/libs/utils/ssh/sshcryptofacility.cpp b/src/libs/utils/ssh/sshcryptofacility.cpp deleted file mode 100644 index d5386d91c6..0000000000 --- a/src/libs/utils/ssh/sshcryptofacility.cpp +++ /dev/null @@ -1,387 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshcryptofacility_p.h" - -#include "sshbotanconversions_p.h" -#include "sshcapabilities_p.h" -#include "sshexception_p.h" -#include "sshkeyexchange_p.h" -#include "sshkeypasswordretriever_p.h" -#include "sshpacket_p.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -using namespace Botan; - -namespace Utils { -namespace Internal { - -SshAbstractCryptoFacility::SshAbstractCryptoFacility() - : m_cipherBlockSize(0), m_macLength(0) -{ -} - -SshAbstractCryptoFacility::~SshAbstractCryptoFacility() {} - -void SshAbstractCryptoFacility::clearKeys() -{ - m_cipherBlockSize = 0; - m_macLength = 0; - m_sessionId.clear(); - m_pipe.reset(0); - m_hMac.reset(0); -} - -void SshAbstractCryptoFacility::recreateKeys(const SshKeyExchange &kex) -{ - checkInvariant(); - - if (m_sessionId.isEmpty()) - m_sessionId = kex.h(); - Algorithm_Factory &af = global_state().algorithm_factory(); - const std::string &cryptAlgo = botanCryptAlgoName(cryptAlgoName(kex)); - BlockCipher * const cipher = af.prototype_block_cipher(cryptAlgo)->clone(); - - m_cipherBlockSize = cipher->BLOCK_SIZE; - const QByteArray ivData = generateHash(kex, ivChar(), m_cipherBlockSize); - const InitializationVector iv(convertByteArray(ivData), m_cipherBlockSize); - - const quint32 keySize = max_keylength_of(cryptAlgo); - const QByteArray cryptKeyData = generateHash(kex, keyChar(), keySize); - SymmetricKey cryptKey(convertByteArray(cryptKeyData), keySize); - - BlockCipherMode * const cipherMode - = makeCipherMode(cipher, new Null_Padding, iv, cryptKey); - m_pipe.reset(new Pipe(cipherMode)); - - m_macLength = botanHMacKeyLen(hMacAlgoName(kex)); - const QByteArray hMacKeyData = generateHash(kex, macChar(), macLength()); - SymmetricKey hMacKey(convertByteArray(hMacKeyData), macLength()); - const HashFunction * const hMacProto - = af.prototype_hash_function(botanHMacAlgoName(hMacAlgoName(kex))); - m_hMac.reset(new HMAC(hMacProto->clone())); - m_hMac->set_key(hMacKey); -} - -void SshAbstractCryptoFacility::convert(QByteArray &data, quint32 offset, - quint32 dataSize) const -{ - Q_ASSERT(offset + dataSize <= static_cast(data.size())); - checkInvariant(); - - // Session id empty => No key exchange has happened yet. - if (dataSize == 0 || m_sessionId.isEmpty()) - return; - - if (dataSize % cipherBlockSize() != 0) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid packet size"); - } - m_pipe->process_msg(reinterpret_cast(data.constData()) + offset, - dataSize); - quint32 bytesRead = m_pipe->read(reinterpret_cast(data.data()) + offset, - dataSize, m_pipe->message_count() - 1); // Can't use Pipe::LAST_MESSAGE because of a VC bug. - Q_ASSERT(bytesRead == dataSize); -} - -QByteArray SshAbstractCryptoFacility::generateMac(const QByteArray &data, - quint32 dataSize) const -{ - return m_sessionId.isEmpty() - ? QByteArray() - : convertByteArray(m_hMac->process(reinterpret_cast(data.constData()), - dataSize)); -} - -QByteArray SshAbstractCryptoFacility::generateHash(const SshKeyExchange &kex, - char c, quint32 length) -{ - const QByteArray &k = kex.k(); - const QByteArray &h = kex.h(); - QByteArray data(k); - data.append(h).append(c).append(m_sessionId); - SecureVector key - = kex.hash()->process(convertByteArray(data), data.size()); - while (key.size() < length) { - SecureVector tmpKey; - tmpKey.append(convertByteArray(k), k.size()); - tmpKey.append(convertByteArray(h), h.size()); - tmpKey.append(key); - key.append(kex.hash()->process(tmpKey)); - } - return QByteArray(reinterpret_cast(key.begin()), length); -} - -void SshAbstractCryptoFacility::checkInvariant() const -{ - Q_ASSERT(m_sessionId.isEmpty() == !m_pipe); -} - - -const QByteArray SshEncryptionFacility::PrivKeyFileStartLineRsa("-----BEGIN RSA PRIVATE KEY-----"); -const QByteArray SshEncryptionFacility::PrivKeyFileStartLineDsa("-----BEGIN DSA PRIVATE KEY-----"); -const QByteArray SshEncryptionFacility::PrivKeyFileEndLineRsa("-----END RSA PRIVATE KEY-----"); -const QByteArray SshEncryptionFacility::PrivKeyFileEndLineDsa("-----END DSA PRIVATE KEY-----"); - -QByteArray SshEncryptionFacility::cryptAlgoName(const SshKeyExchange &kex) const -{ - return kex.encryptionAlgo(); -} - -QByteArray SshEncryptionFacility::hMacAlgoName(const SshKeyExchange &kex) const -{ - return kex.hMacAlgoClientToServer(); -} - -BlockCipherMode *SshEncryptionFacility::makeCipherMode(BlockCipher *cipher, - BlockCipherModePaddingMethod *paddingMethod, const InitializationVector &iv, - const SymmetricKey &key) -{ - return new CBC_Encryption(cipher, paddingMethod, key, iv); -} - -void SshEncryptionFacility::encrypt(QByteArray &data) const -{ - convert(data, 0, data.size()); -} - -void SshEncryptionFacility::createAuthenticationKey(const QByteArray &privKeyFileContents) -{ - if (privKeyFileContents == m_cachedPrivKeyContents) - return; - -#ifdef CREATOR_SSH_DEBUG - qDebug("%s: Key not cached, reading", Q_FUNC_INFO); -#endif - QList pubKeyParams; - QList allKeyParams; - QString error1; - QString error2; - if (!createAuthenticationKeyFromPKCS8(privKeyFileContents, pubKeyParams, allKeyParams, error1) - && !createAuthenticationKeyFromOpenSSL(privKeyFileContents, pubKeyParams, allKeyParams, - error2)) { -#ifdef CREATOR_SSH_DEBUG - qDebug("%s: %s\n\t%s\n", Q_FUNC_INFO, qPrintable(error1), qPrintable(error2)); -#endif - throw SshClientException(SshKeyFileError, SSH_TR("Decoding of private key file failed: " - "Format not understood.")); - } - - foreach (const BigInt &b, allKeyParams) { - if (b.is_zero()) { - throw SshClientException(SshKeyFileError, - SSH_TR("Decoding of private key file failed: Invalid zero parameter.")); - } - } - - m_authPubKeyBlob = AbstractSshPacket::encodeString(m_authKeyAlgoName); - foreach (const BigInt &b, pubKeyParams) - m_authPubKeyBlob += AbstractSshPacket::encodeMpInt(b); - m_cachedPrivKeyContents = privKeyFileContents; -} - -bool SshEncryptionFacility::createAuthenticationKeyFromPKCS8(const QByteArray &privKeyFileContents, - QList &pubKeyParams, QList &allKeyParams, QString &error) -{ - try { - Pipe pipe; - pipe.process_msg(convertByteArray(privKeyFileContents), privKeyFileContents.size()); - Private_Key * const key = PKCS8::load_key(pipe, m_rng, SshKeyPasswordRetriever()); - if (DSA_PrivateKey * const dsaKey = dynamic_cast(key)) { - m_authKeyAlgoName = SshCapabilities::PubKeyDss; - m_authKey.reset(dsaKey); - pubKeyParams << dsaKey->group_p() << dsaKey->group_q() - << dsaKey->group_g() << dsaKey->get_y(); - allKeyParams << pubKeyParams << dsaKey->get_x(); - } else if (RSA_PrivateKey * const rsaKey = dynamic_cast(key)) { - m_authKeyAlgoName = SshCapabilities::PubKeyRsa; - m_authKey.reset(rsaKey); - pubKeyParams << rsaKey->get_e() << rsaKey->get_n(); - allKeyParams << pubKeyParams << rsaKey->get_p() << rsaKey->get_q() - << rsaKey->get_d(); - } else { - qWarning("%s: Unexpected code flow, expected success or exception.", Q_FUNC_INFO); - return false; - } - } catch (const Botan::Exception &ex) { - error = QLatin1String(ex.what()); - return false; - } - return true; -} - -bool SshEncryptionFacility::createAuthenticationKeyFromOpenSSL(const QByteArray &privKeyFileContents, - QList &pubKeyParams, QList &allKeyParams, QString &error) -{ - try { - bool syntaxOk = true; - QList lines = privKeyFileContents.split('\n'); - while (lines.last().isEmpty()) - lines.removeLast(); - if (lines.count() < 3) { - syntaxOk = false; - } else if (lines.first() == PrivKeyFileStartLineRsa) { - if (lines.last() != PrivKeyFileEndLineRsa) - syntaxOk = false; - else - m_authKeyAlgoName = SshCapabilities::PubKeyRsa; - } else if (lines.first() == PrivKeyFileStartLineDsa) { - if (lines.last() != PrivKeyFileEndLineDsa) - syntaxOk = false; - else - m_authKeyAlgoName = SshCapabilities::PubKeyDss; - } else { - syntaxOk = false; - } - if (!syntaxOk) { - error = SSH_TR("Unexpected format."); - return false; - } - - QByteArray privateKeyBlob; - for (int i = 1; i < lines.size() - 1; ++i) - privateKeyBlob += lines.at(i); - privateKeyBlob = QByteArray::fromBase64(privateKeyBlob); - - BER_Decoder decoder(convertByteArray(privateKeyBlob), privateKeyBlob.size()); - BER_Decoder sequence = decoder.start_cons(SEQUENCE); - quint32 version; - sequence.decode (version); - if (version != 0) { - error = SSH_TR("Key encoding has version %1, expected 0.").arg(version); - return false; - } - - if (m_authKeyAlgoName == SshCapabilities::PubKeyDss) { - BigInt p, q, g, y, x; - sequence.decode (p).decode (q).decode (g).decode (y).decode (x); - DSA_PrivateKey * const dsaKey = new DSA_PrivateKey(m_rng, DL_Group(p, q, g), x); - m_authKey.reset(dsaKey); - pubKeyParams << p << q << g << y; - allKeyParams << pubKeyParams << x; - } else { - BigInt p, q, e, d, n; - sequence.decode(n).decode(e).decode(d).decode(p).decode(q); - RSA_PrivateKey * const rsaKey = new RSA_PrivateKey(m_rng, p, q, e, d, n); - m_authKey.reset(rsaKey); - pubKeyParams << e << n; - allKeyParams << pubKeyParams << p << q << d; - } - - sequence.discard_remaining(); - sequence.verify_end(); - } catch (const Botan::Exception &ex) { - error = QLatin1String(ex.what()); - return false; - } - return true; -} - -QByteArray SshEncryptionFacility::authenticationAlgorithmName() const -{ - Q_ASSERT(m_authKey); - return m_authKeyAlgoName; -} - -QByteArray SshEncryptionFacility::authenticationKeySignature(const QByteArray &data) const -{ - Q_ASSERT(m_authKey); - - QScopedPointer signer(get_pk_signer (*m_authKey, - botanEmsaAlgoName(m_authKeyAlgoName))); - QByteArray dataToSign = AbstractSshPacket::encodeString(sessionId()) + data; - QByteArray signature - = convertByteArray(signer->sign_message(convertByteArray(dataToSign), - dataToSign.size(), m_rng)); - return AbstractSshPacket::encodeString(m_authKeyAlgoName) - + AbstractSshPacket::encodeString(signature); -} - -QByteArray SshEncryptionFacility::getRandomNumbers(int count) const -{ - QByteArray data; - data.resize(count); - m_rng.randomize(convertByteArray(data), count); - return data; -} - -SshEncryptionFacility::~SshEncryptionFacility() {} - - -QByteArray SshDecryptionFacility::cryptAlgoName(const SshKeyExchange &kex) const -{ - return kex.decryptionAlgo(); -} - -QByteArray SshDecryptionFacility::hMacAlgoName(const SshKeyExchange &kex) const -{ - return kex.hMacAlgoServerToClient(); -} - -BlockCipherMode *SshDecryptionFacility::makeCipherMode(BlockCipher *cipher, - BlockCipherModePaddingMethod *paddingMethod, const InitializationVector &iv, - const SymmetricKey &key) -{ - return new CBC_Decryption(cipher, paddingMethod, key, iv); -} - -void SshDecryptionFacility::decrypt(QByteArray &data, quint32 offset, - quint32 dataSize) const -{ - convert(data, offset, dataSize); -#ifdef CREATOR_SSH_DEBUG - qDebug("Decrypted data:"); - const char * const start = data.constData() + offset; - const char * const end = start + dataSize; - for (const char *c = start; c < end; ++c) - qDebug() << "'" << *c << "' (0x" << (static_cast(*c) & 0xff) << ")"; -#endif -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshcryptofacility_p.h b/src/libs/utils/ssh/sshcryptofacility_p.h deleted file mode 100644 index b19c18b9f4..0000000000 --- a/src/libs/utils/ssh/sshcryptofacility_p.h +++ /dev/null @@ -1,157 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHABSTRACTCRYPTOFACILITY_P_H -#define SSHABSTRACTCRYPTOFACILITY_P_H - -#include -#include - -#include -#include - -namespace Botan { - class BigInt; - class BlockCipher; - class BlockCipherMode; - class BlockCipherModePaddingMethod; - class HashFunction; - class HMAC; - class Pipe; - class PK_Signing_Key; -} - -namespace Utils { -namespace Internal { - -class SshKeyExchange; - -class SshAbstractCryptoFacility -{ -public: - virtual ~SshAbstractCryptoFacility(); - - void clearKeys(); - void recreateKeys(const SshKeyExchange &kex); - QByteArray generateMac(const QByteArray &data, quint32 dataSize) const; - quint32 cipherBlockSize() const { return m_cipherBlockSize; } - quint32 macLength() const { return m_macLength; } - -protected: - SshAbstractCryptoFacility(); - void convert(QByteArray &data, quint32 offset, quint32 dataSize) const; - QByteArray sessionId() const { return m_sessionId; } - -private: - SshAbstractCryptoFacility(const SshAbstractCryptoFacility &); - SshAbstractCryptoFacility &operator=(const SshAbstractCryptoFacility &); - - virtual QByteArray cryptAlgoName(const SshKeyExchange &kex) const = 0; - virtual QByteArray hMacAlgoName(const SshKeyExchange &kex) const = 0; - virtual Botan::BlockCipherMode *makeCipherMode(Botan::BlockCipher *cipher, - Botan::BlockCipherModePaddingMethod *paddingMethod, - const Botan::InitializationVector &iv, - const Botan::SymmetricKey &key) = 0; - virtual char ivChar() const = 0; - virtual char keyChar() const = 0; - virtual char macChar() const = 0; - - QByteArray generateHash(const SshKeyExchange &kex, char c, quint32 length); - void checkInvariant() const; - - QByteArray m_sessionId; - QScopedPointer m_pipe; - QScopedPointer m_hMac; - quint32 m_cipherBlockSize; - quint32 m_macLength; -}; - -class SshEncryptionFacility : public SshAbstractCryptoFacility -{ -public: - void encrypt(QByteArray &data) const; - - void createAuthenticationKey(const QByteArray &privKeyFileContents); - QByteArray authenticationAlgorithmName() const; - QByteArray authenticationPublicKey() const { return m_authPubKeyBlob; } - QByteArray authenticationKeySignature(const QByteArray &data) const; - QByteArray getRandomNumbers(int count) const; - - ~SshEncryptionFacility(); - -private: - virtual QByteArray cryptAlgoName(const SshKeyExchange &kex) const; - virtual QByteArray hMacAlgoName(const SshKeyExchange &kex) const; - virtual Botan::BlockCipherMode *makeCipherMode(Botan::BlockCipher *cipher, - Botan::BlockCipherModePaddingMethod *paddingMethod, - const Botan::InitializationVector &iv, const Botan::SymmetricKey &key); - virtual char ivChar() const { return 'A'; } - virtual char keyChar() const { return 'C'; } - virtual char macChar() const { return 'E'; } - - bool createAuthenticationKeyFromPKCS8(const QByteArray &privKeyFileContents, - QList &pubKeyParams, QList &allKeyParams, QString &error); - bool createAuthenticationKeyFromOpenSSL(const QByteArray &privKeyFileContents, - QList &pubKeyParams, QList &allKeyParams, QString &error); - - static const QByteArray PrivKeyFileStartLineRsa; - static const QByteArray PrivKeyFileStartLineDsa; - static const QByteArray PrivKeyFileEndLineRsa; - static const QByteArray PrivKeyFileEndLineDsa; - - QByteArray m_authKeyAlgoName; - QByteArray m_authPubKeyBlob; - QByteArray m_cachedPrivKeyContents; - QScopedPointer m_authKey; - mutable Botan::AutoSeeded_RNG m_rng; -}; - -class SshDecryptionFacility : public SshAbstractCryptoFacility -{ -public: - void decrypt(QByteArray &data, quint32 offset, quint32 dataSize) const; - -private: - virtual QByteArray cryptAlgoName(const SshKeyExchange &kex) const; - virtual QByteArray hMacAlgoName(const SshKeyExchange &kex) const; - virtual Botan::BlockCipherMode *makeCipherMode(Botan::BlockCipher *cipher, - Botan::BlockCipherModePaddingMethod *paddingMethod, - const Botan::InitializationVector &iv, const Botan::SymmetricKey &key); - virtual char ivChar() const { return 'B'; } - virtual char keyChar() const { return 'D'; } - virtual char macChar() const { return 'F'; } -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHABSTRACTCRYPTOFACILITY_P_H diff --git a/src/libs/utils/ssh/ssherrors.h b/src/libs/utils/ssh/ssherrors.h deleted file mode 100644 index c2b32a3c76..0000000000 --- a/src/libs/utils/ssh/ssherrors.h +++ /dev/null @@ -1,46 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHERRORS_P_H -#define SSHERRORS_P_H - -namespace Utils { - -enum SshError { - SshNoError, SshSocketError, SshTimeoutError, SshProtocolError, - SshHostKeyError, SshKeyFileError, SshAuthenticationError, - SshClosedByServerError, SshInternalError -}; - -} // namespace Utils - -#endif // SSHERRORS_P_H diff --git a/src/libs/utils/ssh/sshexception_p.h b/src/libs/utils/ssh/sshexception_p.h deleted file mode 100644 index e21c9d9fc9..0000000000 --- a/src/libs/utils/ssh/sshexception_p.h +++ /dev/null @@ -1,92 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHEXCEPTION_P_H -#define SSHEXCEPTION_P_H - -#include "ssherrors.h" - -#include -#include -#include - -namespace Utils { -namespace Internal { - -enum SshErrorCode { - SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT = 1, - SSH_DISCONNECT_PROTOCOL_ERROR = 2, - SSH_DISCONNECT_KEY_EXCHANGE_FAILED = 3, - SSH_DISCONNECT_RESERVED = 4, - SSH_DISCONNECT_MAC_ERROR = 5, - SSH_DISCONNECT_COMPRESSION_ERROR = 6, - SSH_DISCONNECT_SERVICE_NOT_AVAILABLE = 7, - SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED = 8, - SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE = 9, - SSH_DISCONNECT_CONNECTION_LOST = 10, - SSH_DISCONNECT_BY_APPLICATION = 11, - SSH_DISCONNECT_TOO_MANY_CONNECTIONS = 12, - SSH_DISCONNECT_AUTH_CANCELLED_BY_USER = 13, - SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE = 14, - SSH_DISCONNECT_ILLEGAL_USER_NAME = 15 -}; - -#define SSH_TR(string) QCoreApplication::translate("SshConnection", string) - -#define SSH_SERVER_EXCEPTION(error, errorString) \ - SshServerException((error), (errorString), SSH_TR(errorString)) - -struct SshServerException -{ - SshServerException(SshErrorCode error, const QByteArray &errorStringServer, - const QString &errorStringUser) - : error(error), errorStringServer(errorStringServer), - errorStringUser(errorStringUser) {} - - const SshErrorCode error; - const QByteArray errorStringServer; - const QString errorStringUser; -}; - -struct SshClientException -{ - SshClientException(SshError error, const QString &errorString) - : error(error), errorString(errorString) {} - - const SshError error; - const QString errorString; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHEXCEPTION_P_H diff --git a/src/libs/utils/ssh/sshincomingpacket.cpp b/src/libs/utils/ssh/sshincomingpacket.cpp deleted file mode 100644 index feb9560e31..0000000000 --- a/src/libs/utils/ssh/sshincomingpacket.cpp +++ /dev/null @@ -1,463 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshincomingpacket_p.h" - -#include "sshcapabilities_p.h" - -namespace Utils { -namespace Internal { - -const QByteArray SshIncomingPacket::ExitStatusType("exit-status"); -const QByteArray SshIncomingPacket::ExitSignalType("exit-signal"); - -SshIncomingPacket::SshIncomingPacket() : m_serverSeqNr(0) { } - -quint32 SshIncomingPacket::cipherBlockSize() const -{ - return qMax(m_decrypter.cipherBlockSize(), 8U); -} - -quint32 SshIncomingPacket::macLength() const -{ - return m_decrypter.macLength(); -} - -void SshIncomingPacket::recreateKeys(const SshKeyExchange &keyExchange) -{ - m_decrypter.recreateKeys(keyExchange); -} - -void SshIncomingPacket::reset() -{ - clear(); - m_serverSeqNr = 0; - m_decrypter.clearKeys(); -} - -void SshIncomingPacket::consumeData(QByteArray &newData) -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("%s: current data size = %d, new data size = %d", - Q_FUNC_INFO, m_data.size(), newData.size()); -#endif - - if (isComplete() || newData.isEmpty()) - return; - - /* - * Until we have reached the minimum packet size, we cannot decrypt the - * length field. - */ - const quint32 minSize = minPacketSize(); - if (currentDataSize() < minSize) { - const int bytesToTake - = qMin(minSize - currentDataSize(), newData.size()); - moveFirstBytes(m_data, newData, bytesToTake); -#ifdef CREATOR_SSH_DEBUG - qDebug("Took %d bytes from new data", bytesToTake); -#endif - if (currentDataSize() < minSize) - return; - } - - const int bytesToTake - = qMin(length() + 4 + macLength() - currentDataSize(), - newData.size()); - moveFirstBytes(m_data, newData, bytesToTake); -#ifdef CREATOR_SSH_DEBUG - qDebug("Took %d bytes from new data", bytesToTake); -#endif - if (isComplete()) { -#ifdef CREATOR_SSH_DEBUG - qDebug("Message complete. Overall size: %u, payload size: %u", - m_data.size(), m_length - paddingLength() - 1); -#endif - decrypt(); - ++m_serverSeqNr; - } -} - -void SshIncomingPacket::decrypt() -{ - Q_ASSERT(isComplete()); - const quint32 netDataLength = length() + 4; - m_decrypter.decrypt(m_data, cipherBlockSize(), - netDataLength - cipherBlockSize()); - const QByteArray &mac = m_data.mid(netDataLength, macLength()); - if (mac != generateMac(m_decrypter, m_serverSeqNr)) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_MAC_ERROR, - "Message authentication failed."); - } -} - -void SshIncomingPacket::moveFirstBytes(QByteArray &target, QByteArray &source, - int n) -{ - target.append(source.left(n)); - source.remove(0, n); -} - -SshKeyExchangeInit SshIncomingPacket::extractKeyExchangeInitData() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_KEXINIT); - - SshKeyExchangeInit exchangeData; - try { - quint32 offset = TypeOffset + 1; - std::memcpy(exchangeData.cookie, &m_data.constData()[offset], - sizeof exchangeData.cookie); - offset += sizeof exchangeData.cookie; - exchangeData.keyAlgorithms - = SshPacketParser::asNameList(m_data, &offset); - exchangeData.serverHostKeyAlgorithms - = SshPacketParser::asNameList(m_data, &offset); - exchangeData.encryptionAlgorithmsClientToServer - = SshPacketParser::asNameList(m_data, &offset); - exchangeData.encryptionAlgorithmsServerToClient - = SshPacketParser::asNameList(m_data, &offset); - exchangeData.macAlgorithmsClientToServer - = SshPacketParser::asNameList(m_data, &offset); - exchangeData.macAlgorithmsServerToClient - = SshPacketParser::asNameList(m_data, &offset); - exchangeData.compressionAlgorithmsClientToServer - = SshPacketParser::asNameList(m_data, &offset); - exchangeData.compressionAlgorithmsServerToClient - = SshPacketParser::asNameList(m_data, &offset); - exchangeData.languagesClientToServer - = SshPacketParser::asNameList(m_data, &offset); - exchangeData.languagesServerToClient - = SshPacketParser::asNameList(m_data, &offset); - exchangeData.firstKexPacketFollows - = SshPacketParser::asBool(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED, - "Key exchange failed: Server sent invalid SSH_MSG_KEXINIT packet."); - } - return exchangeData; -} - -SshKeyExchangeReply SshIncomingPacket::extractKeyExchangeReply(const QByteArray &pubKeyAlgo) const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_KEXDH_REPLY); - - try { - SshKeyExchangeReply replyData; - quint32 offset = TypeOffset + 1; - const quint32 k_sLength - = SshPacketParser::asUint32(m_data, &offset); - if (offset + k_sLength > currentDataSize()) - throw SshPacketParseException(); - replyData.k_s = m_data.mid(offset - 4, k_sLength + 4); - if (SshPacketParser::asString(m_data, &offset) != pubKeyAlgo) - throw SshPacketParseException(); - - // DSS: p and q, RSA: e and n - replyData.parameters << SshPacketParser::asBigInt(m_data, &offset); - replyData.parameters << SshPacketParser::asBigInt(m_data, &offset); - - // g and y - if (pubKeyAlgo == SshCapabilities::PubKeyDss) { - replyData.parameters << SshPacketParser::asBigInt(m_data, &offset); - replyData.parameters << SshPacketParser::asBigInt(m_data, &offset); - } - - replyData.f = SshPacketParser::asBigInt(m_data, &offset); - offset += 4; - if (SshPacketParser::asString(m_data, &offset) != pubKeyAlgo) - throw SshPacketParseException(); - replyData.signatureBlob = SshPacketParser::asString(m_data, &offset); - return replyData; - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED, - "Key exchange failed: " - "Server sent invalid SSH_MSG_KEXDH_REPLY packet."); - } -} - -SshDisconnect SshIncomingPacket::extractDisconnect() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_DISCONNECT); - - SshDisconnect msg; - try { - quint32 offset = TypeOffset + 1; - msg.reasonCode = SshPacketParser::asUint32(m_data, &offset); - msg.description = SshPacketParser::asUserString(m_data, &offset); - msg.language = SshPacketParser::asString(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_MSG_DISCONNECT."); - } - - return msg; -} - -SshUserAuthBanner SshIncomingPacket::extractUserAuthBanner() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_USERAUTH_BANNER); - - try { - SshUserAuthBanner msg; - quint32 offset = TypeOffset + 1; - msg.message = SshPacketParser::asUserString(m_data, &offset); - msg.language = SshPacketParser::asString(m_data, &offset); - return msg; - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_MSG_USERAUTH_BANNER."); - } -} - -SshDebug SshIncomingPacket::extractDebug() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_DEBUG); - - try { - SshDebug msg; - quint32 offset = TypeOffset + 1; - msg.display = SshPacketParser::asBool(m_data, &offset); - msg.message = SshPacketParser::asUserString(m_data, &offset); - msg.language = SshPacketParser::asString(m_data, &offset); - return msg; - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_MSG_DEBUG."); - } -} - -SshUnimplemented SshIncomingPacket::extractUnimplemented() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_UNIMPLEMENTED); - - try { - SshUnimplemented msg; - quint32 offset = TypeOffset + 1; - msg.invalidMsgSeqNr = SshPacketParser::asUint32(m_data, &offset); - return msg; - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_MSG_UNIMPLEMENTED."); - } -} - -SshChannelOpenFailure SshIncomingPacket::extractChannelOpenFailure() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_CHANNEL_OPEN_FAILURE); - - SshChannelOpenFailure openFailure; - try { - quint32 offset = TypeOffset + 1; - openFailure.localChannel = SshPacketParser::asUint32(m_data, &offset); - openFailure.reasonCode = SshPacketParser::asUint32(m_data, &offset); - openFailure.reasonString = QString::fromLocal8Bit(SshPacketParser::asString(m_data, &offset)); - openFailure.language = SshPacketParser::asString(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Server sent invalid SSH_MSG_CHANNEL_OPEN_FAILURE packet."); - } - return openFailure; -} - -SshChannelOpenConfirmation SshIncomingPacket::extractChannelOpenConfirmation() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_CHANNEL_OPEN_CONFIRMATION); - - SshChannelOpenConfirmation confirmation; - try { - quint32 offset = TypeOffset + 1; - confirmation.localChannel = SshPacketParser::asUint32(m_data, &offset); - confirmation.remoteChannel = SshPacketParser::asUint32(m_data, &offset); - confirmation.remoteWindowSize = SshPacketParser::asUint32(m_data, &offset); - confirmation.remoteMaxPacketSize = SshPacketParser::asUint32(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Server sent invalid SSH_MSG_CHANNEL_OPEN_CONFIRMATION packet."); - } - return confirmation; -} - -SshChannelWindowAdjust SshIncomingPacket::extractWindowAdjust() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_CHANNEL_WINDOW_ADJUST); - - SshChannelWindowAdjust adjust; - try { - quint32 offset = TypeOffset + 1; - adjust.localChannel = SshPacketParser::asUint32(m_data, &offset); - adjust.bytesToAdd = SshPacketParser::asUint32(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_MSG_CHANNEL_WINDOW_ADJUST packet."); - } - return adjust; -} - -SshChannelData SshIncomingPacket::extractChannelData() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_CHANNEL_DATA); - - SshChannelData data; - try { - quint32 offset = TypeOffset + 1; - data.localChannel = SshPacketParser::asUint32(m_data, &offset); - data.data = SshPacketParser::asString(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_MSG_CHANNEL_DATA packet."); - } - return data; -} - -SshChannelExtendedData SshIncomingPacket::extractChannelExtendedData() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_CHANNEL_EXTENDED_DATA); - - SshChannelExtendedData data; - try { - quint32 offset = TypeOffset + 1; - data.localChannel = SshPacketParser::asUint32(m_data, &offset); - data.type = SshPacketParser::asUint32(m_data, &offset); - data.data = SshPacketParser::asString(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_MSG_CHANNEL_EXTENDED_DATA packet."); - } - return data; -} - -SshChannelExitStatus SshIncomingPacket::extractChannelExitStatus() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_CHANNEL_REQUEST); - - SshChannelExitStatus exitStatus; - try { - quint32 offset = TypeOffset + 1; - exitStatus.localChannel = SshPacketParser::asUint32(m_data, &offset); - const QByteArray &type = SshPacketParser::asString(m_data, &offset); - Q_ASSERT(type == ExitStatusType); - Q_UNUSED(type); - if (SshPacketParser::asBool(m_data, &offset)) - throw SshPacketParseException(); - exitStatus.exitStatus = SshPacketParser::asUint32(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid exit-status packet."); - } - return exitStatus; -} - -SshChannelExitSignal SshIncomingPacket::extractChannelExitSignal() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_CHANNEL_REQUEST); - - SshChannelExitSignal exitSignal; - try { - quint32 offset = TypeOffset + 1; - exitSignal.localChannel = SshPacketParser::asUint32(m_data, &offset); - const QByteArray &type = SshPacketParser::asString(m_data, &offset); - Q_ASSERT(type == ExitSignalType); - Q_UNUSED(type); - if (SshPacketParser::asBool(m_data, &offset)) - throw SshPacketParseException(); - exitSignal.signal = SshPacketParser::asString(m_data, &offset); - exitSignal.coreDumped = SshPacketParser::asBool(m_data, &offset); - exitSignal.error = SshPacketParser::asUserString(m_data, &offset); - exitSignal.language = SshPacketParser::asString(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid exit-signal packet."); - } - return exitSignal; -} - -quint32 SshIncomingPacket::extractRecipientChannel() const -{ - Q_ASSERT(isComplete()); - - try { - quint32 offset = TypeOffset + 1; - return SshPacketParser::asUint32(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Server sent invalid packet."); - } -} - -QByteArray SshIncomingPacket::extractChannelRequestType() const -{ - Q_ASSERT(isComplete()); - Q_ASSERT(type() == SSH_MSG_CHANNEL_REQUEST); - - try { - quint32 offset = TypeOffset + 1; - SshPacketParser::asUint32(m_data, &offset); - return SshPacketParser::asString(m_data, &offset); - } catch (SshPacketParseException &) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Invalid SSH_MSG_CHANNEL_REQUEST packet."); - } -} - -void SshIncomingPacket::calculateLength() const -{ - Q_ASSERT(currentDataSize() >= minPacketSize()); -#ifdef CREATOR_SSH_DEBUG - qDebug("Length field before decryption: %d-%d-%d-%d", m_data.at(0) & 0xff, - m_data.at(1) & 0xff, m_data.at(2) & 0xff, m_data.at(3) & 0xff); -#endif - m_decrypter.decrypt(m_data, 0, cipherBlockSize()); -#ifdef CREATOR_SSH_DEBUG - qDebug("Length field after decryption: %d-%d-%d-%d", m_data.at(0) & 0xff, m_data.at(1) & 0xff, m_data.at(2) & 0xff, m_data.at(3) & 0xff); - qDebug("message type = %d", m_data.at(TypeOffset)); -#endif - m_length = SshPacketParser::asUint32(m_data, static_cast(0)); -#ifdef CREATOR_SSH_DEBUG - qDebug("decrypted length is %u", m_length); -#endif -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshincomingpacket_p.h b/src/libs/utils/ssh/sshincomingpacket_p.h deleted file mode 100644 index e632881d9b..0000000000 --- a/src/libs/utils/ssh/sshincomingpacket_p.h +++ /dev/null @@ -1,195 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHINCOMINGPACKET_P_H -#define SSHINCOMINGPACKET_P_H - -#include "sshpacket_p.h" - -#include "sshcryptofacility_p.h" -#include "sshpacketparser_p.h" - -#include -#include - -namespace Utils { -namespace Internal { - -class SshKeyExchange; - -struct SshKeyExchangeInit -{ - char cookie[16]; - SshNameList keyAlgorithms; - SshNameList serverHostKeyAlgorithms; - SshNameList encryptionAlgorithmsClientToServer; - SshNameList encryptionAlgorithmsServerToClient; - SshNameList macAlgorithmsClientToServer; - SshNameList macAlgorithmsServerToClient; - SshNameList compressionAlgorithmsClientToServer; - SshNameList compressionAlgorithmsServerToClient; - SshNameList languagesClientToServer; - SshNameList languagesServerToClient; - bool firstKexPacketFollows; -}; - -struct SshKeyExchangeReply -{ - QByteArray k_s; - QList parameters; // DSS: p, q, g, y. RSA: e, n. - Botan::BigInt f; - QByteArray signatureBlob; -}; - -struct SshDisconnect -{ - quint32 reasonCode; - QString description; - QByteArray language; -}; - -struct SshUserAuthBanner -{ - QString message; - QByteArray language; -}; - -struct SshDebug -{ - bool display; - QString message; - QByteArray language; -}; - -struct SshUnimplemented -{ - quint32 invalidMsgSeqNr; -}; - -struct SshChannelOpenFailure -{ - quint32 localChannel; - quint32 reasonCode; - QString reasonString; - QByteArray language; -}; - -struct SshChannelOpenConfirmation -{ - quint32 localChannel; - quint32 remoteChannel; - quint32 remoteWindowSize; - quint32 remoteMaxPacketSize; -}; - -struct SshChannelWindowAdjust -{ - quint32 localChannel; - quint32 bytesToAdd; -}; - -struct SshChannelData -{ - quint32 localChannel; - QByteArray data; -}; - -struct SshChannelExtendedData -{ - quint32 localChannel; - quint32 type; - QByteArray data; -}; - -struct SshChannelExitStatus -{ - quint32 localChannel; - quint32 exitStatus; -}; - -struct SshChannelExitSignal -{ - quint32 localChannel; - QByteArray signal; - bool coreDumped; - QString error; - QByteArray language; -}; - - -class SshIncomingPacket : public AbstractSshPacket -{ -public: - SshIncomingPacket(); - - void consumeData(QByteArray &data); - void recreateKeys(const SshKeyExchange &keyExchange); - void reset(); - - SshKeyExchangeInit extractKeyExchangeInitData() const; - SshKeyExchangeReply extractKeyExchangeReply(const QByteArray &pubKeyAlgo) const; - SshDisconnect extractDisconnect() const; - SshUserAuthBanner extractUserAuthBanner() const; - SshDebug extractDebug() const; - SshUnimplemented extractUnimplemented() const; - - SshChannelOpenFailure extractChannelOpenFailure() const; - SshChannelOpenConfirmation extractChannelOpenConfirmation() const; - SshChannelWindowAdjust extractWindowAdjust() const; - SshChannelData extractChannelData() const; - SshChannelExtendedData extractChannelExtendedData() const; - SshChannelExitStatus extractChannelExitStatus() const; - SshChannelExitSignal extractChannelExitSignal() const; - quint32 extractRecipientChannel() const; - QByteArray extractChannelRequestType() const; - - quint32 serverSeqNr() const { return m_serverSeqNr; } - - static const QByteArray ExitStatusType; - static const QByteArray ExitSignalType; - -private: - virtual quint32 cipherBlockSize() const; - virtual quint32 macLength() const; - virtual void calculateLength() const; - - void decrypt(); - void moveFirstBytes(QByteArray &target, QByteArray &source, int n); - - quint32 m_serverSeqNr; - SshDecryptionFacility m_decrypter; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHINCOMINGPACKET_P_H diff --git a/src/libs/utils/ssh/sshkeycreationdialog.cpp b/src/libs/utils/ssh/sshkeycreationdialog.cpp deleted file mode 100644 index 6e53f7e734..0000000000 --- a/src/libs/utils/ssh/sshkeycreationdialog.cpp +++ /dev/null @@ -1,132 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ -#include "sshkeycreationdialog.h" -#include "ui_sshkeycreationdialog.h" - -#include "sshkeygenerator.h" - -#include - -#include -#include -#include -#include -#include -#include - -namespace Utils { - -SshKeyCreationDialog::SshKeyCreationDialog(QWidget *parent) - : QDialog(parent), m_keyGenerator(0), m_ui(new Ui::SshKeyCreationDialog) -{ - m_ui->setupUi(this); - m_ui->privateKeyFilePathChooser->setExpectedKind(PathChooser::File); - const QString defaultPath = QDesktopServices::storageLocation(QDesktopServices::HomeLocation) - + QLatin1String("/.ssh/qtc_id"); - m_ui->privateKeyFilePathChooser->setPath(defaultPath); - filePathChanged(); - - connect(m_ui->rsa, SIGNAL(toggled(bool)), this, SLOT(keyTypeChanged())); - connect(m_ui->dsa, SIGNAL(toggled(bool)), this, SLOT(keyTypeChanged())); - connect(m_ui->privateKeyFilePathChooser, SIGNAL(changed(QString)), SLOT(filePathChanged())); - connect(m_ui->generateButton, SIGNAL(clicked()), this, SLOT(generateKeys())); -} - -SshKeyCreationDialog::~SshKeyCreationDialog() -{ - delete m_keyGenerator; -} - -void SshKeyCreationDialog::keyTypeChanged() -{ - m_ui->comboBox->setCurrentIndex(0); - m_ui->comboBox->setEnabled(m_ui->rsa->isChecked()); -} - -void SshKeyCreationDialog::generateKeys() -{ - const SshKeyGenerator::KeyType keyType = m_ui->rsa->isChecked() - ? SshKeyGenerator::Rsa - : SshKeyGenerator::Dsa; - - if (!m_keyGenerator) - m_keyGenerator = new SshKeyGenerator; - - QApplication::setOverrideCursor(Qt::BusyCursor); - const bool success = m_keyGenerator->generateKeys(keyType, SshKeyGenerator::Mixed, - m_ui->comboBox->currentText().toUShort()); - QApplication::restoreOverrideCursor(); - - if (success) { - saveKeys(); - } else { - QMessageBox::critical(this, tr("Key Generation Failed"), m_keyGenerator->error()); - } -} - -void SshKeyCreationDialog::filePathChanged() -{ - m_ui->generateButton->setEnabled(!privateKeyFilePath().isEmpty()); - m_ui->publicKeyFileLabel->setText(privateKeyFilePath() + QLatin1String(".pub")); -} - -void SshKeyCreationDialog::saveKeys() -{ - const QString parentDir = QFileInfo(privateKeyFilePath()).dir().path(); - if (!QDir::root().mkpath(parentDir)) { - QMessageBox::critical(this, tr("Failure To Save Key File"), - tr("Failed to create directory: '%1'.").arg(parentDir)); - return; - } - - FileSaver privSaver(privateKeyFilePath()); - privSaver.write(m_keyGenerator->privateKey()); - if (!privSaver.finalize(this)) - return; - QFile::setPermissions(privateKeyFilePath(), QFile::ReadOwner | QFile::WriteOwner); - - FileSaver pubSaver(publicKeyFilePath()); - pubSaver.write(m_keyGenerator->publicKey()); - if (pubSaver.finalize(this)) - accept(); -} - -QString SshKeyCreationDialog::privateKeyFilePath() const -{ - return m_ui->privateKeyFilePathChooser->path(); -} - -QString SshKeyCreationDialog::publicKeyFilePath() const -{ - return m_ui->publicKeyFileLabel->text(); -} - -} // namespace Utils diff --git a/src/libs/utils/ssh/sshkeycreationdialog.h b/src/libs/utils/ssh/sshkeycreationdialog.h deleted file mode 100644 index b8985294a1..0000000000 --- a/src/libs/utils/ssh/sshkeycreationdialog.h +++ /dev/null @@ -1,69 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHKEYCREATIONDIALOG_H -#define SSHKEYCREATIONDIALOG_H - -#include - -#include - -namespace Utils { -class SshKeyGenerator; - -namespace Ui { class SshKeyCreationDialog; } - -class QTCREATOR_UTILS_EXPORT SshKeyCreationDialog : public QDialog -{ - Q_OBJECT -public: - SshKeyCreationDialog(QWidget *parent = 0); - ~SshKeyCreationDialog(); - - QString privateKeyFilePath() const; - QString publicKeyFilePath() const; - -private slots: - void keyTypeChanged(); - void generateKeys(); - void filePathChanged(); - -private: - void saveKeys(); - -private: - SshKeyGenerator *m_keyGenerator; - Ui::SshKeyCreationDialog *m_ui; -}; - -} // namespace Utils - -#endif // SSHKEYCREATIONDIALOG_H diff --git a/src/libs/utils/ssh/sshkeycreationdialog.ui b/src/libs/utils/ssh/sshkeycreationdialog.ui deleted file mode 100644 index 9e20d07f9b..0000000000 --- a/src/libs/utils/ssh/sshkeycreationdialog.ui +++ /dev/null @@ -1,251 +0,0 @@ - - - Utils::SshKeyCreationDialog - - - true - - - - 0 - 0 - 295 - 223 - - - - - 0 - 0 - - - - SSH Key Configuration - - - - - - Options - - - - - - Key algorithm: - - - - - - - - - - 0 - 0 - - - - &RSA - - - true - - - - - - - - 0 - 0 - - - - &DSA - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - 0 - 0 - - - - Key &size: - - - comboBox - - - - - - - - - - 1024 - - - - - 2048 - - - - - 4096 - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - Private key file: - - - - - - - Public key file: - - - - - - - - - - - - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - 6 - - - - - - 0 - 0 - - - - &Generate And Save Key Pair - - - - - - - - 0 - 0 - - - - &Cancel - - - - - - - Qt::Horizontal - - - - 0 - 0 - - - - - - - - - - - Utils::PathChooser - QWidget -
utils/pathchooser.h
-
-
- - - - closeButton - clicked() - Utils::SshKeyCreationDialog - close() - - - 195 - 184 - - - 381 - 107 - - - - -
diff --git a/src/libs/utils/ssh/sshkeyexchange.cpp b/src/libs/utils/ssh/sshkeyexchange.cpp deleted file mode 100644 index 29a6a36f35..0000000000 --- a/src/libs/utils/ssh/sshkeyexchange.cpp +++ /dev/null @@ -1,225 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshkeyexchange_p.h" - -#include "sshbotanconversions_p.h" -#include "sshcapabilities_p.h" -#include "sshsendfacility_p.h" -#include "sshexception_p.h" -#include "sshincomingpacket_p.h" - -#include -#include -#include -#include -#include - -#ifdef CREATOR_SSH_DEBUG -#include -#endif -#include - -using namespace Botan; - -namespace Utils { -namespace Internal { - -namespace { - - // For debugging - void printNameList(const char *listName, const SshNameList &list) - { -#ifdef CREATOR_SSH_DEBUG - qDebug("%s:", listName); - foreach (const QByteArray &name, list.names) - qDebug("%s", name.constData()); -#else - Q_UNUSED(listName); - Q_UNUSED(list); -#endif - } - -#ifdef CREATOR_SSH_DEBUG - void printData(const char *name, const QByteArray &data) - { - std::cerr << std::hex; - qDebug("The client thinks the %s has length %d and is:", name, data.count()); - for (int i = 0; i < data.count(); ++i) - std::cerr << (static_cast(data.at(i)) & 0xff) << ' '; - std::cerr << std::endl; - } -#endif - -} // anonymous namespace - -SshKeyExchange::SshKeyExchange(SshSendFacility &sendFacility) - : m_sendFacility(sendFacility) -{ -} - -SshKeyExchange::~SshKeyExchange() {} - -void SshKeyExchange::sendKexInitPacket(const QByteArray &serverId) -{ - m_serverId = serverId; - m_clientKexInitPayload = m_sendFacility.sendKeyExchangeInitPacket(); -} - -bool SshKeyExchange::sendDhInitPacket(const SshIncomingPacket &serverKexInit) -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("server requests key exchange"); -#endif - serverKexInit.printRawBytes(); - SshKeyExchangeInit kexInitParams - = serverKexInit.extractKeyExchangeInitData(); - - printNameList("Key Algorithms", kexInitParams.keyAlgorithms); - printNameList("Server Host Key Algorithms", kexInitParams.serverHostKeyAlgorithms); - printNameList("Encryption algorithms client to server", kexInitParams.encryptionAlgorithmsClientToServer); - printNameList("Encryption algorithms server to client", kexInitParams.encryptionAlgorithmsServerToClient); - printNameList("MAC algorithms client to server", kexInitParams.macAlgorithmsClientToServer); - printNameList("MAC algorithms server to client", kexInitParams.macAlgorithmsServerToClient); - printNameList("Compression algorithms client to server", kexInitParams.compressionAlgorithmsClientToServer); - printNameList("Compression algorithms client to server", kexInitParams.compressionAlgorithmsClientToServer); - printNameList("Languages client to server", kexInitParams.languagesClientToServer); - printNameList("Languages server to client", kexInitParams.languagesServerToClient); -#ifdef CREATOR_SSH_DEBUG - qDebug("First packet follows: %d", kexInitParams.firstKexPacketFollows); -#endif - - const QByteArray &keyAlgo - = SshCapabilities::findBestMatch(SshCapabilities::KeyExchangeMethods, - kexInitParams.keyAlgorithms.names); - m_serverHostKeyAlgo - = SshCapabilities::findBestMatch(SshCapabilities::PublicKeyAlgorithms, - kexInitParams.serverHostKeyAlgorithms.names); - m_encryptionAlgo - = SshCapabilities::findBestMatch(SshCapabilities::EncryptionAlgorithms, - kexInitParams.encryptionAlgorithmsClientToServer.names); - m_decryptionAlgo - = SshCapabilities::findBestMatch(SshCapabilities::EncryptionAlgorithms, - kexInitParams.encryptionAlgorithmsServerToClient.names); - m_c2sHMacAlgo - = SshCapabilities::findBestMatch(SshCapabilities::MacAlgorithms, - kexInitParams.macAlgorithmsClientToServer.names); - m_s2cHMacAlgo - = SshCapabilities::findBestMatch(SshCapabilities::MacAlgorithms, - kexInitParams.macAlgorithmsServerToClient.names); - SshCapabilities::findBestMatch(SshCapabilities::CompressionAlgorithms, - kexInitParams.compressionAlgorithmsClientToServer.names); - SshCapabilities::findBestMatch(SshCapabilities::CompressionAlgorithms, - kexInitParams.compressionAlgorithmsServerToClient.names); - - AutoSeeded_RNG rng; - m_dhKey.reset(new DH_PrivateKey(rng, - DL_Group(botanKeyExchangeAlgoName(keyAlgo)))); - - m_serverKexInitPayload = serverKexInit.payLoad(); - m_sendFacility.sendKeyDhInitPacket(m_dhKey->get_y()); - return kexInitParams.firstKexPacketFollows; -} - -void SshKeyExchange::sendNewKeysPacket(const SshIncomingPacket &dhReply, - const QByteArray &clientId) -{ - const SshKeyExchangeReply &reply - = dhReply.extractKeyExchangeReply(m_serverHostKeyAlgo); - if (reply.f <= 0 || reply.f >= m_dhKey->group_p()) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED, - "Server sent invalid f."); - } - - QByteArray concatenatedData = AbstractSshPacket::encodeString(clientId); - concatenatedData += AbstractSshPacket::encodeString(m_serverId); - concatenatedData += AbstractSshPacket::encodeString(m_clientKexInitPayload); - concatenatedData += AbstractSshPacket::encodeString(m_serverKexInitPayload); - concatenatedData += reply.k_s; - concatenatedData += AbstractSshPacket::encodeMpInt(m_dhKey->get_y()); - concatenatedData += AbstractSshPacket::encodeMpInt(reply.f); - SymmetricKey k = m_dhKey->derive_key(reply.f); - m_k = AbstractSshPacket::encodeMpInt(BigInt(k.begin(), k.length())); - concatenatedData += m_k; - - m_hash.reset(get_hash(botanSha1Name())); - const SecureVector &hashResult - = m_hash->process(convertByteArray(concatenatedData), - concatenatedData.size()); - m_h = convertByteArray(hashResult); - -#ifdef CREATOR_SSH_DEBUG - printData("Client Id", AbstractSshPacket::encodeString(clientId)); - printData("Server Id", AbstractSshPacket::encodeString(m_serverId)); - printData("Client Payload", AbstractSshPacket::encodeString(m_clientKexInitPayload)); - printData("Server payload", AbstractSshPacket::encodeString(m_serverKexInitPayload)); - printData("K_S", reply.k_s); - printData("y", AbstractSshPacket::encodeMpInt(m_dhKey->get_y())); - printData("f", AbstractSshPacket::encodeMpInt(reply.f)); - printData("K", m_k); - printData("Concatenated data", concatenatedData); - printData("H", m_h); -#endif // CREATOR_SSH_DEBUG - - QScopedPointer sigKey; - QScopedPointer verifier; - if (m_serverHostKeyAlgo == SshCapabilities::PubKeyDss) { - const DL_Group group(reply.parameters.at(0), reply.parameters.at(1), - reply.parameters.at(2)); - DSA_PublicKey * const dsaKey - = new DSA_PublicKey(group, reply.parameters.at(3)); - sigKey.reset(dsaKey); - verifier.reset(get_pk_verifier(*dsaKey, - botanEmsaAlgoName(SshCapabilities::PubKeyDss))); - } else if (m_serverHostKeyAlgo == SshCapabilities::PubKeyRsa) { - RSA_PublicKey * const rsaKey - = new RSA_PublicKey(reply.parameters.at(1), reply.parameters.at(0)); - sigKey.reset(rsaKey); - verifier.reset(get_pk_verifier(*rsaKey, - botanEmsaAlgoName(SshCapabilities::PubKeyRsa))); - } else { - Q_ASSERT(!"Impossible: Neither DSS nor RSA!"); - } - const byte * const botanH = convertByteArray(m_h); - const Botan::byte * const botanSig - = convertByteArray(reply.signatureBlob); - if (!verifier->verify_message(botanH, m_h.size(), botanSig, - reply.signatureBlob.size())) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED, - "Invalid signature in SSH_MSG_KEXDH_REPLY packet."); - } - - m_sendFacility.sendNewKeysPacket(); -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshkeyexchange_p.h b/src/libs/utils/ssh/sshkeyexchange_p.h deleted file mode 100644 index 83be2fad86..0000000000 --- a/src/libs/utils/ssh/sshkeyexchange_p.h +++ /dev/null @@ -1,90 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHKEYEXCHANGE_P_H -#define SSHKEYEXCHANGE_P_H - -#include - -#include -#include - -namespace Botan { class HashFunction; } - -namespace Utils { -namespace Internal { - -class SshSendFacility; -class SshIncomingPacket; - -class SshKeyExchange -{ -public: - SshKeyExchange(SshSendFacility &sendFacility); - ~SshKeyExchange(); - - void sendKexInitPacket(const QByteArray &serverId); - - // Returns true <=> the server sends a guessed package. - bool sendDhInitPacket(const SshIncomingPacket &serverKexInit); - - void sendNewKeysPacket(const SshIncomingPacket &dhReply, - const QByteArray &clientId); - - QByteArray k() const { return m_k; } - QByteArray h() const { return m_h; } - Botan::HashFunction *hash() const { return m_hash.data(); } - QByteArray encryptionAlgo() const { return m_encryptionAlgo; } - QByteArray decryptionAlgo() const { return m_decryptionAlgo; } - QByteArray hMacAlgoClientToServer() const { return m_c2sHMacAlgo; } - QByteArray hMacAlgoServerToClient() const { return m_s2cHMacAlgo; } - -private: - QByteArray m_serverId; - QByteArray m_clientKexInitPayload; - QByteArray m_serverKexInitPayload; - QScopedPointer m_dhKey; - QByteArray m_k; - QByteArray m_h; - QByteArray m_serverHostKeyAlgo; - QByteArray m_encryptionAlgo; - QByteArray m_decryptionAlgo; - QByteArray m_c2sHMacAlgo; - QByteArray m_s2cHMacAlgo; - QScopedPointer m_hash; - SshSendFacility &m_sendFacility; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHKEYEXCHANGE_P_H diff --git a/src/libs/utils/ssh/sshkeygenerator.cpp b/src/libs/utils/ssh/sshkeygenerator.cpp deleted file mode 100644 index 0165644960..0000000000 --- a/src/libs/utils/ssh/sshkeygenerator.cpp +++ /dev/null @@ -1,201 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshkeygenerator.h" - -#include "sshbotanconversions_p.h" -#include "sshcapabilities_p.h" -#include "sshpacket_p.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -namespace Utils { - -using namespace Botan; -using namespace Internal; - -SshKeyGenerator::SshKeyGenerator() : m_type(Rsa) -{ -} - -bool SshKeyGenerator::generateKeys(KeyType type, PrivateKeyFormat format, int keySize, - EncryptionMode encryptionMode) -{ - m_type = type; - m_encryptionMode = encryptionMode; - - try { - AutoSeeded_RNG rng; - KeyPtr key; - if (m_type == Rsa) - key = KeyPtr(new RSA_PrivateKey(rng, keySize)); - else - key = KeyPtr(new DSA_PrivateKey(rng, DL_Group(rng, DL_Group::DSA_Kosherizer, keySize))); - switch (format) { - case Pkcs8: - generatePkcs8KeyStrings(key, rng); - break; - case OpenSsl: - generateOpenSslKeyStrings(key); - break; - case Mixed: - default: - generatePkcs8KeyString(key, true, rng); - generateOpenSslPublicKeyString(key); - } - return true; - } catch (Botan::Exception &e) { - m_error = tr("Error generating key: %1").arg(QString::fromAscii(e.what())); - return false; - } -} - -void SshKeyGenerator::generatePkcs8KeyStrings(const KeyPtr &key, Botan::RandomNumberGenerator &rng) -{ - generatePkcs8KeyString(key, false, rng); - generatePkcs8KeyString(key, true, rng); -} - -void SshKeyGenerator::generatePkcs8KeyString(const KeyPtr &key, bool privateKey, - Botan::RandomNumberGenerator &rng) -{ - Pipe pipe; - pipe.start_msg(); - QByteArray *keyData; - if (privateKey) { - QString password; - if (m_encryptionMode == DoOfferEncryption) - password = getPassword(); - if (!password.isEmpty()) - PKCS8::encrypt_key(*key, pipe, rng, password.toLocal8Bit().data()); - else - PKCS8::encode(*key, pipe); - keyData = &m_privateKey; - } else { - X509::encode(*key, pipe); - keyData = &m_publicKey; - } - pipe.end_msg(); - keyData->resize(pipe.remaining(pipe.message_count() - 1)); - pipe.read(convertByteArray(*keyData), keyData->size(), - pipe.message_count() - 1); -} - -void SshKeyGenerator::generateOpenSslKeyStrings(const KeyPtr &key) -{ - generateOpenSslPublicKeyString(key); - generateOpenSslPrivateKeyString(key); -} - -void SshKeyGenerator::generateOpenSslPublicKeyString(const KeyPtr &key) -{ - QList params; - QByteArray keyId; - if (m_type == Rsa) { - const QSharedPointer rsaKey = key.dynamicCast(); - params << rsaKey->get_e() << rsaKey->get_n(); - keyId = SshCapabilities::PubKeyRsa; - } else { - const QSharedPointer dsaKey = key.dynamicCast(); - params << dsaKey->group_p() << dsaKey->group_q() << dsaKey->group_g() << dsaKey->get_y(); - keyId = SshCapabilities::PubKeyDss; - } - - QByteArray publicKeyBlob = AbstractSshPacket::encodeString(keyId); - foreach (const BigInt &b, params) - publicKeyBlob += AbstractSshPacket::encodeMpInt(b); - publicKeyBlob = publicKeyBlob.toBase64(); - const QByteArray id = "QtCreator/" - + QDateTime::currentDateTime().toString(Qt::ISODate).toUtf8(); - m_publicKey = keyId + ' ' + publicKeyBlob + ' ' + id; -} - -void SshKeyGenerator::generateOpenSslPrivateKeyString(const KeyPtr &key) -{ - QList params; - QByteArray keyId; - const char *label; - if (m_type == Rsa) { - const QSharedPointer rsaKey - = key.dynamicCast(); - params << rsaKey->get_n() << rsaKey->get_e() << rsaKey->get_d() << rsaKey->get_p() - << rsaKey->get_q(); - keyId = SshCapabilities::PubKeyRsa; - label = "RSA PRIVATE KEY"; - } else { - const QSharedPointer dsaKey = key.dynamicCast(); - params << dsaKey->group_p() << dsaKey->group_q() << dsaKey->group_g() << dsaKey->get_y() - << dsaKey->get_x(); - keyId = SshCapabilities::PubKeyDss; - label = "DSA PRIVATE KEY"; - } - - DER_Encoder encoder; - encoder.start_cons(SEQUENCE).encode(0U); - foreach (const BigInt &b, params) - encoder.encode(b); - encoder.end_cons(); - m_privateKey = QByteArray(PEM_Code::encode (encoder.get_contents(), label).c_str()); -} - -QString SshKeyGenerator::getPassword() const -{ - QInputDialog d; - d.setInputMode(QInputDialog::TextInput); - d.setTextEchoMode(QLineEdit::Password); - d.setWindowTitle(tr("Password for Private Key")); - d.setLabelText(tr("It is recommended that you secure your private key\n" - "with a password, which you can enter below.")); - d.setOkButtonText(tr("Encrypt Key File")); - d.setCancelButtonText(tr("Do Not Encrypt Key File")); - int result = QDialog::Accepted; - QString password; - while (result == QDialog::Accepted && password.isEmpty()) { - result = d.exec(); - password = d.textValue(); - } - return result == QDialog::Accepted ? password : QString(); -} - -} // namespace Utils diff --git a/src/libs/utils/ssh/sshkeygenerator.h b/src/libs/utils/ssh/sshkeygenerator.h deleted file mode 100644 index 506039e3bc..0000000000 --- a/src/libs/utils/ssh/sshkeygenerator.h +++ /dev/null @@ -1,85 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHKEYGENERATOR_H -#define SSHKEYGENERATOR_H - -#include - -#include -#include - -namespace Botan { - class Private_Key; - class RandomNumberGenerator; -} - -namespace Utils { - -class QTCREATOR_UTILS_EXPORT SshKeyGenerator -{ - Q_DECLARE_TR_FUNCTIONS(SshKeyGenerator) -public: - enum KeyType { Rsa, Dsa }; - enum PrivateKeyFormat { Pkcs8, OpenSsl, Mixed }; - enum EncryptionMode { DoOfferEncryption, DoNotOfferEncryption }; // Only relevant for Pkcs8 format. - - SshKeyGenerator(); - bool generateKeys(KeyType type, PrivateKeyFormat format, int keySize, - EncryptionMode encryptionMode = DoOfferEncryption); - - QString error() const { return m_error; } - QByteArray privateKey() const { return m_privateKey; } - QByteArray publicKey() const { return m_publicKey; } - KeyType type() const { return m_type; } - -private: - typedef QSharedPointer KeyPtr; - - void generatePkcs8KeyStrings(const KeyPtr &key, Botan::RandomNumberGenerator &rng); - void generatePkcs8KeyString(const KeyPtr &key, bool privateKey, - Botan::RandomNumberGenerator &rng); - void generateOpenSslKeyStrings(const KeyPtr &key); - void generateOpenSslPrivateKeyString(const KeyPtr &key); - void generateOpenSslPublicKeyString(const KeyPtr &key); - QString getPassword() const; - - QString m_error; - QByteArray m_publicKey; - QByteArray m_privateKey; - KeyType m_type; - EncryptionMode m_encryptionMode; -}; - -} // namespace Utils - -#endif // SSHKEYGENERATOR_H diff --git a/src/libs/utils/ssh/sshkeypasswordretriever.cpp b/src/libs/utils/ssh/sshkeypasswordretriever.cpp deleted file mode 100644 index 7c00751ec6..0000000000 --- a/src/libs/utils/ssh/sshkeypasswordretriever.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ -#include "sshkeypasswordretriever_p.h" - -#include -#include -#include - -#include - -namespace Utils { -namespace Internal { - -std::string SshKeyPasswordRetriever::get_passphrase(const std::string &, const std::string &, - UI_Result &result) const -{ - const bool hasGui = dynamic_cast(QApplication::instance()); - if (hasGui) { - bool ok; - const QString &password = QInputDialog::getText(0, - QCoreApplication::translate("Utils::Ssh", "Password Required"), - QCoreApplication::translate("Utils::Ssh", "Please enter the password for your private key."), - QLineEdit::Password, QString(), &ok); - result = ok ? OK : CANCEL_ACTION; - return std::string(password.toLocal8Bit().data()); - } else { - result = OK; - std::string password; - std::cout << "Please enter the password for your private key (set echo off beforehand!): " << std::flush; - std::cin >> password; - return password; - } -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshkeypasswordretriever_p.h b/src/libs/utils/ssh/sshkeypasswordretriever_p.h deleted file mode 100644 index 505badb536..0000000000 --- a/src/libs/utils/ssh/sshkeypasswordretriever_p.h +++ /dev/null @@ -1,52 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ -#ifndef KEYPASSWORDRETRIEVER_H -#define KEYPASSWORDRETRIEVER_H - -#include - -#include - -namespace Utils { -namespace Internal { - -class SshKeyPasswordRetriever : public Botan::User_Interface -{ -public: - std::string get_passphrase(const std::string &what, const std::string &source, - UI_Result &result) const; -}; - -} // namespace Internal -} // namespace Utils - -#endif // KEYPASSWORDRETRIEVER_H diff --git a/src/libs/utils/ssh/sshoutgoingpacket.cpp b/src/libs/utils/ssh/sshoutgoingpacket.cpp deleted file mode 100644 index e79f65a60e..0000000000 --- a/src/libs/utils/ssh/sshoutgoingpacket.cpp +++ /dev/null @@ -1,325 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshoutgoingpacket_p.h" - -#include "sshcapabilities_p.h" -#include "sshcryptofacility_p.h" - -#include - -namespace Utils { -namespace Internal { - -SshOutgoingPacket::SshOutgoingPacket(const SshEncryptionFacility &encrypter, - const quint32 &seqNr) : m_encrypter(encrypter), m_seqNr(seqNr) -{ -} - -quint32 SshOutgoingPacket::cipherBlockSize() const -{ - return qMax(m_encrypter.cipherBlockSize(), 4U); -} - -quint32 SshOutgoingPacket::macLength() const -{ - return m_encrypter.macLength(); -} - -QByteArray SshOutgoingPacket::generateKeyExchangeInitPacket() -{ - const QByteArray &supportedkeyExchangeMethods - = encodeNameList(SshCapabilities::KeyExchangeMethods); - const QByteArray &supportedPublicKeyAlgorithms - = encodeNameList(SshCapabilities::PublicKeyAlgorithms); - const QByteArray &supportedEncryptionAlgorithms - = encodeNameList(SshCapabilities::EncryptionAlgorithms); - const QByteArray &supportedMacAlgorithms - = encodeNameList(SshCapabilities::MacAlgorithms); - const QByteArray &supportedCompressionAlgorithms - = encodeNameList(SshCapabilities::CompressionAlgorithms); - const QByteArray &supportedLanguages = encodeNameList(QList()); - - init(SSH_MSG_KEXINIT); - m_data += m_encrypter.getRandomNumbers(16); - m_data.append(supportedkeyExchangeMethods); - m_data.append(supportedPublicKeyAlgorithms); - m_data.append(supportedEncryptionAlgorithms) - .append(supportedEncryptionAlgorithms); - m_data.append(supportedMacAlgorithms).append(supportedMacAlgorithms); - m_data.append(supportedCompressionAlgorithms) - .append(supportedCompressionAlgorithms); - m_data.append(supportedLanguages).append(supportedLanguages); - appendBool(false); // No guessed packet. - m_data.append(QByteArray(4, 0)); // Reserved. - QByteArray payload = m_data.mid(PayloadOffset); - finalize(); - return payload; -} - -void SshOutgoingPacket::generateKeyDhInitPacket(const Botan::BigInt &e) -{ - init(SSH_MSG_KEXDH_INIT).appendMpInt(e).finalize(); -} - -void SshOutgoingPacket::generateNewKeysPacket() -{ - init(SSH_MSG_NEWKEYS).finalize(); -} - -void SshOutgoingPacket::generateUserAuthServiceRequestPacket() -{ - generateServiceRequest("ssh-userauth"); -} - -void SshOutgoingPacket::generateServiceRequest(const QByteArray &service) -{ - init(SSH_MSG_SERVICE_REQUEST).appendString(service).finalize(); -} - -void SshOutgoingPacket::generateUserAuthByPwdRequestPacket(const QByteArray &user, - const QByteArray &service, const QByteArray &pwd) -{ - init(SSH_MSG_USERAUTH_REQUEST).appendString(user).appendString(service) - .appendString("password").appendBool(false).appendString(pwd) - .finalize(); -} - -void SshOutgoingPacket::generateUserAuthByKeyRequestPacket(const QByteArray &user, - const QByteArray &service) -{ - init(SSH_MSG_USERAUTH_REQUEST).appendString(user).appendString(service) - .appendString("publickey").appendBool(true) - .appendString(m_encrypter.authenticationAlgorithmName()) - .appendString(m_encrypter.authenticationPublicKey()); - const QByteArray &dataToSign = m_data.mid(PayloadOffset); - appendString(m_encrypter.authenticationKeySignature(dataToSign)); - finalize(); -} - -void SshOutgoingPacket::generateRequestFailurePacket() -{ - init(SSH_MSG_REQUEST_FAILURE).finalize(); -} - -void SshOutgoingPacket::generateIgnorePacket() -{ - init(SSH_MSG_IGNORE).finalize(); -} - -void SshOutgoingPacket::generateInvalidMessagePacket() -{ - init(SSH_MSG_INVALID).finalize(); -} - -void SshOutgoingPacket::generateSessionPacket(quint32 channelId, - quint32 windowSize, quint32 maxPacketSize) -{ - init(SSH_MSG_CHANNEL_OPEN).appendString("session").appendInt(channelId) - .appendInt(windowSize).appendInt(maxPacketSize).finalize(); -} - -void SshOutgoingPacket::generateEnvPacket(quint32 remoteChannel, - const QByteArray &var, const QByteArray &value) -{ - init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel).appendString("env") - .appendBool(false).appendString(var).appendString(value).finalize(); -} - -void SshOutgoingPacket::generatePtyRequestPacket(quint32 remoteChannel, - const SshPseudoTerminal &terminal) -{ - init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel) - .appendString("pty-req").appendBool(false) - .appendString(terminal.termType).appendInt(terminal.columnCount) - .appendInt(terminal.rowCount).appendInt(0).appendInt(0); - QByteArray modeString; - for (SshPseudoTerminal::ModeMap::ConstIterator it = terminal.modes.constBegin(); - it != terminal.modes.constEnd(); ++it) { - modeString += char(it.key()); - modeString += encodeInt(it.value()); - } - modeString += char(0); // TTY_OP_END - appendString(modeString).finalize(); -} - -void SshOutgoingPacket::generateExecPacket(quint32 remoteChannel, - const QByteArray &command) -{ - init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel).appendString("exec") - .appendBool(true).appendString(command).finalize(); -} - -void SshOutgoingPacket::generateShellPacket(quint32 remoteChannel) -{ - init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel).appendString("shell") - .appendBool(true).finalize(); -} - -void SshOutgoingPacket::generateSftpPacket(quint32 remoteChannel) -{ - init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel) - .appendString("subsystem").appendBool(true).appendString("sftp") - .finalize(); -} - -void SshOutgoingPacket::generateWindowAdjustPacket(quint32 remoteChannel, - quint32 bytesToAdd) -{ - init(SSH_MSG_CHANNEL_WINDOW_ADJUST).appendInt(remoteChannel) - .appendInt(bytesToAdd).finalize(); -} - -void SshOutgoingPacket::generateChannelDataPacket(quint32 remoteChannel, - const QByteArray &data) -{ - init(SSH_MSG_CHANNEL_DATA).appendInt(remoteChannel).appendString(data) - .finalize(); -} - -void SshOutgoingPacket::generateChannelSignalPacket(quint32 remoteChannel, - const QByteArray &signalName) -{ - init(SSH_MSG_CHANNEL_REQUEST).appendInt(remoteChannel) - .appendString("signal").appendBool(false).appendString(signalName) - .finalize(); -} - -void SshOutgoingPacket::generateChannelEofPacket(quint32 remoteChannel) -{ - init(SSH_MSG_CHANNEL_EOF).appendInt(remoteChannel).finalize(); -} - -void SshOutgoingPacket::generateChannelClosePacket(quint32 remoteChannel) -{ - init(SSH_MSG_CHANNEL_CLOSE).appendInt(remoteChannel).finalize(); -} - -void SshOutgoingPacket::generateDisconnectPacket(SshErrorCode reason, - const QByteArray &reasonString) -{ - init(SSH_MSG_DISCONNECT).appendInt(reason).appendString(reasonString) - .appendString(QByteArray()).finalize(); -} - -void SshOutgoingPacket::generateMsgUnimplementedPacket(quint32 serverSeqNr) -{ - init(SSH_MSG_UNIMPLEMENTED).appendInt(serverSeqNr).finalize(); -} - -SshOutgoingPacket &SshOutgoingPacket::appendInt(quint32 val) -{ - m_data.append(encodeInt(val)); - return *this; -} - -SshOutgoingPacket &SshOutgoingPacket::appendMpInt(const Botan::BigInt &number) -{ - m_data.append(encodeMpInt(number)); - return *this; -} - -SshOutgoingPacket &SshOutgoingPacket::appendBool(bool b) -{ - m_data += static_cast(b); - return *this; -} - -SshOutgoingPacket &SshOutgoingPacket::appendString(const QByteArray &string) -{ - m_data.append(encodeString(string)); - return *this; -} - -SshOutgoingPacket &SshOutgoingPacket::init(SshPacketType type) -{ - m_data.resize(TypeOffset + 1); - m_data[TypeOffset] = type; - return *this; -} - -SshOutgoingPacket &SshOutgoingPacket::setPadding() -{ - m_data += m_encrypter.getRandomNumbers(MinPaddingLength); - int padLength = MinPaddingLength; - const int divisor = sizeDivisor(); - const int mod = m_data.size() % divisor; - padLength += divisor - mod; - m_data += m_encrypter.getRandomNumbers(padLength - MinPaddingLength); - m_data[PaddingLengthOffset] = padLength; - return *this; -} - -SshOutgoingPacket &SshOutgoingPacket::encrypt() -{ - const QByteArray &mac - = generateMac(m_encrypter, m_seqNr); - m_encrypter.encrypt(m_data); - m_data += mac; - return *this; -} - -void SshOutgoingPacket::finalize() -{ - setPadding(); - setLengthField(m_data); - m_length = m_data.size() - 4; -#ifdef CREATOR_SSH_DEBUG - qDebug("Encrypting packet of type %u", m_data.at(TypeOffset)); -#endif - encrypt(); -#ifdef CREATOR_SSH_DEBUG - qDebug("Sending packet of size %d", rawData().count()); -#endif - Q_ASSERT(isComplete()); -} - -int SshOutgoingPacket::sizeDivisor() const -{ - return qMax(cipherBlockSize(), 8U); -} - -QByteArray SshOutgoingPacket::encodeNameList(const QList &list) -{ - QByteArray data; - data.resize(4); - for (int i = 0; i < list.count(); ++i) { - if (i > 0) - data.append(','); - data.append(list.at(i)); - } - AbstractSshPacket::setLengthField(data); - return data; -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshoutgoingpacket_p.h b/src/libs/utils/ssh/sshoutgoingpacket_p.h deleted file mode 100644 index 546c5f247b..0000000000 --- a/src/libs/utils/ssh/sshoutgoingpacket_p.h +++ /dev/null @@ -1,108 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHOUTGOINGPACKET_P_H -#define SSHOUTGOINGPACKET_P_H - -#include "sshpacket_p.h" - -#include "sshpseudoterminal.h" - -namespace Utils { -namespace Internal { - -class SshEncryptionFacility; - -class SshOutgoingPacket : public AbstractSshPacket -{ -public: - SshOutgoingPacket(const SshEncryptionFacility &encrypter, - const quint32 &seqNr); - - QByteArray generateKeyExchangeInitPacket(); // Returns payload. - void generateKeyDhInitPacket(const Botan::BigInt &e); - void generateNewKeysPacket(); - void generateDisconnectPacket(SshErrorCode reason, - const QByteArray &reasonString); - void generateMsgUnimplementedPacket(quint32 serverSeqNr); - void generateUserAuthServiceRequestPacket(); - void generateUserAuthByPwdRequestPacket(const QByteArray &user, - const QByteArray &service, const QByteArray &pwd); - void generateUserAuthByKeyRequestPacket(const QByteArray &user, - const QByteArray &service); - void generateRequestFailurePacket(); - void generateIgnorePacket(); - void generateInvalidMessagePacket(); - void generateSessionPacket(quint32 channelId, quint32 windowSize, - quint32 maxPacketSize); - void generateEnvPacket(quint32 remoteChannel, const QByteArray &var, - const QByteArray &value); - void generatePtyRequestPacket(quint32 remoteChannel, - const SshPseudoTerminal &terminal); - void generateExecPacket(quint32 remoteChannel, const QByteArray &command); - void generateShellPacket(quint32 remoteChannel); - void generateSftpPacket(quint32 remoteChannel); - void generateWindowAdjustPacket(quint32 remoteChannel, quint32 bytesToAdd); - void generateChannelDataPacket(quint32 remoteChannel, - const QByteArray &data); - void generateChannelSignalPacket(quint32 remoteChannel, - const QByteArray &signalName); - void generateChannelEofPacket(quint32 remoteChannel); - void generateChannelClosePacket(quint32 remoteChannel); - -private: - virtual quint32 cipherBlockSize() const; - virtual quint32 macLength() const; - - static QByteArray encodeNameList(const QList &list); - - void generateServiceRequest(const QByteArray &service); - - SshOutgoingPacket &init(SshPacketType type); - SshOutgoingPacket &setPadding(); - SshOutgoingPacket &encrypt(); - void finalize(); - - SshOutgoingPacket &appendInt(quint32 val); - SshOutgoingPacket &appendString(const QByteArray &string); - SshOutgoingPacket &appendMpInt(const Botan::BigInt &number); - SshOutgoingPacket &appendBool(bool b); - int sizeDivisor() const; - - const SshEncryptionFacility &m_encrypter; - const quint32 &m_seqNr; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHOUTGOINGPACKET_P_H diff --git a/src/libs/utils/ssh/sshpacket.cpp b/src/libs/utils/ssh/sshpacket.cpp deleted file mode 100644 index 7af1f25078..0000000000 --- a/src/libs/utils/ssh/sshpacket.cpp +++ /dev/null @@ -1,168 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshpacket_p.h" - -#include "sshcapabilities_p.h" -#include "sshcryptofacility_p.h" -#include "sshexception_p.h" -#include "sshpacketparser_p.h" - -#include - -#include - -namespace Utils { -namespace Internal { - -const quint32 AbstractSshPacket::PaddingLengthOffset = 4; -const quint32 AbstractSshPacket::PayloadOffset = PaddingLengthOffset + 1; -const quint32 AbstractSshPacket::TypeOffset = PayloadOffset; -const quint32 AbstractSshPacket::MinPaddingLength = 4; - -namespace { - - void printByteArray(const QByteArray &data) - { -#ifdef CREATOR_SSH_DEBUG - for (int i = 0; i < data.count(); ++i) - qDebug() << std::hex << (static_cast(data[i]) & 0xff) << " "; -#else - Q_UNUSED(data); -#endif - } -} // anonymous namespace - - -AbstractSshPacket::AbstractSshPacket() : m_length(0) { } -AbstractSshPacket::~AbstractSshPacket() {} - -bool AbstractSshPacket::isComplete() const -{ - if (currentDataSize() < minPacketSize()) - return false; - Q_ASSERT(4 + length() + macLength() >= currentDataSize()); - return 4 + length() + macLength() == currentDataSize(); -} - -void AbstractSshPacket::clear() -{ - m_data.clear(); - m_length = 0; -} - -SshPacketType AbstractSshPacket::type() const -{ - Q_ASSERT(isComplete()); - return static_cast(m_data.at(TypeOffset)); -} - -QByteArray AbstractSshPacket::payLoad() const -{ - return QByteArray(m_data.constData() + PayloadOffset, - length() - paddingLength() - 1); -} - -void AbstractSshPacket::printRawBytes() const -{ - printByteArray(m_data); -} - -QByteArray AbstractSshPacket::encodeString(const QByteArray &string) -{ - QByteArray data; - data.resize(4); - data += string; - setLengthField(data); - return data; -} - -QByteArray AbstractSshPacket::encodeMpInt(const Botan::BigInt &number) -{ - if (number.is_zero()) - return QByteArray(4, 0); - - int stringLength = number.bytes(); - const bool positiveAndMsbSet = number.sign() == Botan::BigInt::Positive - && (number.byte_at(stringLength - 1) & 0x80); - if (positiveAndMsbSet) - ++stringLength; - QByteArray data; - data.resize(4 + stringLength); - int pos = 4; - if (positiveAndMsbSet) - data[pos++] = '\0'; - number.binary_encode(reinterpret_cast(data.data()) + pos); - setLengthField(data); - return data; -} - -int AbstractSshPacket::paddingLength() const -{ - return m_data[PaddingLengthOffset]; -} - -quint32 AbstractSshPacket::length() const -{ - //Q_ASSERT(currentDataSize() >= minPacketSize()); - if (m_length == 0) - calculateLength(); - return m_length; -} - -void AbstractSshPacket::calculateLength() const -{ - m_length = SshPacketParser::asUint32(m_data, static_cast(0)); -} - -QByteArray AbstractSshPacket::generateMac(const SshAbstractCryptoFacility &crypt, - quint32 seqNr) const -{ - const quint32 seqNrBe = qToBigEndian(seqNr); - QByteArray data(reinterpret_cast(&seqNrBe), sizeof seqNrBe); - data += QByteArray(m_data.constData(), length() + 4); - return crypt.generateMac(data, data.size()); -} - -quint32 AbstractSshPacket::minPacketSize() const -{ - return qMax(cipherBlockSize(), 16) + macLength(); -} - -void AbstractSshPacket::setLengthField(QByteArray &data) -{ - const quint32 length = qToBigEndian(data.size() - 4); - data.replace(0, 4, reinterpret_cast(&length), 4); -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshpacket_p.h b/src/libs/utils/ssh/sshpacket_p.h deleted file mode 100644 index 594f13c744..0000000000 --- a/src/libs/utils/ssh/sshpacket_p.h +++ /dev/null @@ -1,146 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHPACKET_P_H -#define SSHPACKET_P_H - -#include "sshexception_p.h" - -#include -#include -#include - -#include - -namespace Utils { -namespace Internal { - -enum SshPacketType { - SSH_MSG_DISCONNECT = 1, - SSH_MSG_IGNORE = 2, - SSH_MSG_UNIMPLEMENTED = 3, - SSH_MSG_DEBUG = 4, - SSH_MSG_SERVICE_REQUEST = 5, - SSH_MSG_SERVICE_ACCEPT = 6, - - SSH_MSG_KEXINIT = 20, - SSH_MSG_NEWKEYS = 21, - SSH_MSG_KEXDH_INIT = 30, - SSH_MSG_KEXDH_REPLY = 31, - - SSH_MSG_USERAUTH_REQUEST = 50, - SSH_MSG_USERAUTH_FAILURE = 51, - SSH_MSG_USERAUTH_SUCCESS = 52, - SSH_MSG_USERAUTH_BANNER = 53, - SSH_MSG_USERAUTH_PK_OK = 60, - SSH_MSG_USERAUTH_PASSWD_CHANGEREQ = 60, - - SSH_MSG_GLOBAL_REQUEST = 80, - SSH_MSG_REQUEST_SUCCESS = 81, - SSH_MSG_REQUEST_FAILURE = 82, - - // TODO: We currently take no precautions against sending these messages - // during a key re-exchange, which is not allowed. - SSH_MSG_CHANNEL_OPEN = 90, - SSH_MSG_CHANNEL_OPEN_CONFIRMATION = 91, - SSH_MSG_CHANNEL_OPEN_FAILURE = 92, - SSH_MSG_CHANNEL_WINDOW_ADJUST = 93, - SSH_MSG_CHANNEL_DATA = 94, - SSH_MSG_CHANNEL_EXTENDED_DATA = 95, - SSH_MSG_CHANNEL_EOF = 96, - SSH_MSG_CHANNEL_CLOSE = 97, - SSH_MSG_CHANNEL_REQUEST = 98, - SSH_MSG_CHANNEL_SUCCESS = 99, - SSH_MSG_CHANNEL_FAILURE = 100, - - // Not completely safe, since the server may actually understand this - // message type as an extension. Switch to a different value in that case - // (between 128 and 191). - SSH_MSG_INVALID = 128 -}; - -enum SshExtendedDataType { SSH_EXTENDED_DATA_STDERR = 1 }; - -class SshAbstractCryptoFacility; - -class AbstractSshPacket -{ -public: - virtual ~AbstractSshPacket(); - - void clear(); - bool isComplete() const; - SshPacketType type() const; - - static QByteArray encodeString(const QByteArray &string); - static QByteArray encodeMpInt(const Botan::BigInt &number); - template static QByteArray encodeInt(T value) - { - const T valMsb = qToBigEndian(value); - return QByteArray(reinterpret_cast(&valMsb), sizeof valMsb); - } - - static void setLengthField(QByteArray &data); - - void printRawBytes() const; // For Debugging. - - const QByteArray &rawData() const { return m_data; } - - QByteArray payLoad() const; - -protected: - AbstractSshPacket(); - - virtual quint32 cipherBlockSize() const = 0; - virtual quint32 macLength() const = 0; - virtual void calculateLength() const; - - quint32 length() const; - int paddingLength() const; - quint32 minPacketSize() const; - quint32 currentDataSize() const { return m_data.size(); } - QByteArray generateMac(const SshAbstractCryptoFacility &crypt, - quint32 seqNr) const; - - static const quint32 PaddingLengthOffset; - static const quint32 PayloadOffset; - static const quint32 TypeOffset; - static const quint32 MinPaddingLength; - - mutable QByteArray m_data; - mutable quint32 m_length; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHPACKET_P_H diff --git a/src/libs/utils/ssh/sshpacketparser.cpp b/src/libs/utils/ssh/sshpacketparser.cpp deleted file mode 100644 index ee12803930..0000000000 --- a/src/libs/utils/ssh/sshpacketparser.cpp +++ /dev/null @@ -1,156 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshpacketparser_p.h" - -#include - -namespace Utils { -namespace Internal { - -namespace { quint32 size(const QByteArray &data) { return data.size(); } } - -QString SshPacketParser::asUserString(const QByteArray &rawString) -{ - QByteArray filteredString; - filteredString.resize(rawString.size()); - for (int i = 0; i < rawString.size(); ++i) { - const char c = rawString.at(i); - filteredString[i] - = std::isprint(c) || c == '\n' || c == '\r' || c == '\t' ? c : '?'; - } - return QString::fromUtf8(filteredString); -} - -bool SshPacketParser::asBool(const QByteArray &data, quint32 offset) -{ - if (size(data) <= offset) - throw SshPacketParseException(); - return data.at(offset); -} - -bool SshPacketParser::asBool(const QByteArray &data, quint32 *offset) -{ - bool b = asBool(data, *offset); - ++(*offset); - return b; -} - - -quint32 SshPacketParser::asUint32(const QByteArray &data, quint32 offset) -{ - if (size(data) < offset + 4) - throw SshPacketParseException(); - const quint32 value = ((data.at(offset) & 0xff) << 24) - + ((data.at(offset + 1) & 0xff) << 16) - + ((data.at(offset + 2) & 0xff) << 8) + (data.at(offset + 3) & 0xff); - return value; -} - -quint32 SshPacketParser::asUint32(const QByteArray &data, quint32 *offset) -{ - const quint32 v = asUint32(data, *offset); - *offset += 4; - return v; -} - -quint64 SshPacketParser::asUint64(const QByteArray &data, quint32 offset) -{ - if (size(data) < offset + 8) - throw SshPacketParseException(); - const quint64 value = (static_cast(data.at(offset) & 0xff) << 56) - + (static_cast(data.at(offset + 1) & 0xff) << 48) - + (static_cast(data.at(offset + 2) & 0xff) << 40) - + (static_cast(data.at(offset + 3) & 0xff) << 32) - + ((data.at(offset + 4) & 0xff) << 24) - + ((data.at(offset + 5) & 0xff) << 16) - + ((data.at(offset + 6) & 0xff) << 8) - + (data.at(offset + 7) & 0xff); - return value; -} - -quint64 SshPacketParser::asUint64(const QByteArray &data, quint32 *offset) -{ - const quint64 val = asUint64(data, *offset); - *offset += 8; - return val; -} - -QByteArray SshPacketParser::asString(const QByteArray &data, quint32 *offset) -{ - const quint32 length = asUint32(data, offset); - if (size(data) < *offset + length) - throw SshPacketParseException(); - const QByteArray &string = data.mid(*offset, length); - *offset += length; - return string; -} - -QString SshPacketParser::asUserString(const QByteArray &data, quint32 *offset) -{ - return asUserString(asString(data, offset)); -} - -SshNameList SshPacketParser::asNameList(const QByteArray &data, quint32 *offset) -{ - const quint32 length = asUint32(data, offset); - const int listEndPos = *offset + length; - if (data.size() < listEndPos) - throw SshPacketParseException(); - SshNameList names(length + 4); - int nextNameOffset = *offset; - int nextCommaOffset = data.indexOf(',', nextNameOffset); - while (nextNameOffset > 0 && nextNameOffset < listEndPos) { - const int stringEndPos = nextCommaOffset == -1 - || nextCommaOffset > listEndPos ? listEndPos : nextCommaOffset; - names.names << QByteArray(data.constData() + nextNameOffset, - stringEndPos - nextNameOffset); - nextNameOffset = nextCommaOffset + 1; - nextCommaOffset = data.indexOf(',', nextNameOffset); - } - *offset += length; - return names; -} - -Botan::BigInt SshPacketParser::asBigInt(const QByteArray &data, quint32 *offset) -{ - const quint32 length = asUint32(data, offset); - if (length == 0) - return Botan::BigInt(); - const Botan::byte *numberStart - = reinterpret_cast(data.constData() + *offset); - *offset += length; - return Botan::BigInt::decode(numberStart, length); -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshpacketparser_p.h b/src/libs/utils/ssh/sshpacketparser_p.h deleted file mode 100644 index a1e0e7eab9..0000000000 --- a/src/libs/utils/ssh/sshpacketparser_p.h +++ /dev/null @@ -1,84 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHPACKETPARSER_P_H -#define SSHPACKETPARSER_P_H - -#include - -#include -#include -#include - -namespace Utils { -namespace Internal { - -struct SshNameList -{ - SshNameList() : originalLength(0) {} - SshNameList(quint32 originalLength) : originalLength(originalLength) {} - quint32 originalLength; - QList names; -}; - -class SshPacketParseException { }; - -// This class's functions try to read a byte array at a certain offset -// as the respective chunk of data as specified in the SSH RFCs. -// If they succeed, they update the offset, so they can easily -// be called in succession by client code. -// For convenience, some have also versions that don't update the offset, -// so they can be called with rvalues if the new value is not needed. -// If they fail, they throw an SshPacketParseException. -class SshPacketParser -{ -public: - static bool asBool(const QByteArray &data, quint32 offset); - static bool asBool(const QByteArray &data, quint32 *offset); - static quint16 asUint16(const QByteArray &data, quint32 offset); - static quint16 asUint16(const QByteArray &data, quint32 *offset); - static quint64 asUint64(const QByteArray &data, quint32 offset); - static quint64 asUint64(const QByteArray &data, quint32 *offset); - static quint32 asUint32(const QByteArray &data, quint32 offset); - static quint32 asUint32(const QByteArray &data, quint32 *offset); - static QByteArray asString(const QByteArray &data, quint32 *offset); - static QString asUserString(const QByteArray &data, quint32 *offset); - static SshNameList asNameList(const QByteArray &data, quint32 *offset); - static Botan::BigInt asBigInt(const QByteArray &data, quint32 *offset); - - static QString asUserString(const QByteArray &rawString); -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHPACKETPARSER_P_H diff --git a/src/libs/utils/ssh/sshpseudoterminal.h b/src/libs/utils/ssh/sshpseudoterminal.h deleted file mode 100644 index 263bcd574d..0000000000 --- a/src/libs/utils/ssh/sshpseudoterminal.h +++ /dev/null @@ -1,120 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHPSEUDOTERMINAL_H -#define SSHPSEUDOTERMINAL_H - -#include - -#include -#include - -namespace Utils { - -class QTCREATOR_UTILS_EXPORT SshPseudoTerminal -{ - public: - explicit SshPseudoTerminal(const QByteArray &termType = "vt100", - int rowCount = 24, int columnCount = 80) - : termType(termType), rowCount(rowCount), columnCount(columnCount) {} - - QByteArray termType; - int rowCount; - int columnCount; - - enum Mode { - VINTR = 1, // Interrupt character. - VQUIT = 2, // The quit character (sends SIGQUIT signal on POSIX systems). - VERASE = 3, // Erase the character to left of the cursor. - VKILL = 4, // Kill the current input line. - VEOF = 5, // End-of-file character (sends EOF from the terminal). - VEOL = 6, // End-of-line character in addition to carriage return and/or linefeed. - VEOL2 = 7, // Additional end-of-line character. - VSTART = 8, // Continues paused output (normally control-Q). - VSTOP = 9, // Pauses output (normally control-S). - VSUSP = 10, // Suspends the current program. - VDSUSP = 11, // Another suspend character. - VREPRINT = 12, // Reprints the current input line. - VWERASE = 13, // Erases a word left of cursor. - VLNEXT = 14, // Enter the next character typed literally, even if it is a special character. - VFLUSH = 15, // Character to flush output. - VSWTCH = 16, // Switch to a different shell layer. - VSTATUS = 17, // Prints system status line (load, command, pid, etc). - VDISCARD = 18, // Toggles the flushing of terminal output. - - IGNPAR = 30, // The ignore parity flag. The parameter SHOULD be 0 if this flag is FALSE, and 1 if it is TRUE. - PARMRK = 31, // Mark parity and framing errors. - INPCK = 32, // Enable checking of parity errors. - ISTRIP = 33, // Strip 8th bit off characters. - INLCR = 34, // Map NL into CR on input. - IGNCR = 35, // Ignore CR on input. - ICRNL = 36, // Map CR to NL on input. - IUCLC = 37, // Translate uppercase characters to lowercase. - IXON = 38, // Enable output flow control. - IXANY = 39, // Any char will restart after stop. - IXOFF = 40, // Enable input flow control. - IMAXBEL = 41, // Ring bell on input queue full. - ISIG = 50, // Enable signals INTR, QUIT, [D]SUSP. - ICANON = 51, // Canonicalize input lines. - XCASE = 52, // Enable input and output of uppercase characters by preceding their lowercase equivalents with "\". - ECHO = 53, // Enable echoing. - ECHOE = 54, // Visually erase chars. - ECHOK = 55, // Kill character discards current line. - ECHONL = 56, // Echo NL even if ECHO is off. - NOFLSH = 57, // Don't flush after interrupt. - TOSTOP = 58, // Stop background jobs from output. - IEXTEN = 59, // Enable extensions. - ECHOCTL = 60, // Echo control characters as ^(Char). - ECHOKE = 61, // Visual erase for line kill. - PENDIN = 62, // Retype pending input. - OPOST = 70, // Enable output processing. - OLCUC = 71, // Convert lowercase to uppercase. - ONLCR = 72, // Map NL to CR-NL. - OCRNL = 73, // Translate carriage return to newline (output). - ONOCR = 74, // Translate newline to carriage return-newline (output). - ONLRET = 75, // Newline performs a carriage return (output). - CS7 = 90, // 7 bit mode. - CS8 = 91, // 8 bit mode. - PARENB = 92, // Parity enable. - PARODD = 93, // Odd parity, else even. - - TTY_OP_ISPEED = 128, // Specifies the input baud rate in bits per second. - TTY_OP_OSPEED = 129 // Specifies the output baud rate in bits per second. - }; - - typedef QHash ModeMap; - ModeMap modes; -}; - -} // namespace Utils - -#endif // SSHPSEUDOTERMINAL_H diff --git a/src/libs/utils/ssh/sshremoteprocess.cpp b/src/libs/utils/ssh/sshremoteprocess.cpp deleted file mode 100644 index 813e1f824c..0000000000 --- a/src/libs/utils/ssh/sshremoteprocess.cpp +++ /dev/null @@ -1,386 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshremoteprocess.h" -#include "sshremoteprocess_p.h" - -#include "sshincomingpacket_p.h" -#include "sshsendfacility_p.h" - -#include - -#include - -#include - -#include -#include - -/*! - \class Utils::SshRemoteProcess - - \brief This class implements an SSH channel for running a remote process. - - Objects are created via SshConnection::createRemoteProcess. - The process is started via the start() member function. - If the process needs a pseudo terminal, you can request one - via requestTerminal() before calling start(). - Note that this class does not support QIODevice's waitFor*() functions, i.e. it has - no synchronous mode. - */ - -namespace Utils { - -const struct { - SshRemoteProcess::Signal signalEnum; - const char * const signalString; -} signalMap[] = { - { SshRemoteProcess::AbrtSignal, "ABRT" }, { SshRemoteProcess::AlrmSignal, "ALRM" }, - { SshRemoteProcess::FpeSignal, "FPE" }, { SshRemoteProcess::HupSignal, "HUP" }, - { SshRemoteProcess::IllSignal, "ILL" }, { SshRemoteProcess::IntSignal, "INT" }, - { SshRemoteProcess::KillSignal, "KILL" }, { SshRemoteProcess::PipeSignal, "PIPE" }, - { SshRemoteProcess::QuitSignal, "QUIT" }, { SshRemoteProcess::SegvSignal, "SEGV" }, - { SshRemoteProcess::TermSignal, "TERM" }, { SshRemoteProcess::Usr1Signal, "USR1" }, - { SshRemoteProcess::Usr2Signal, "USR2" } -}; - -SshRemoteProcess::SshRemoteProcess(const QByteArray &command, quint32 channelId, - Internal::SshSendFacility &sendFacility) - : d(new Internal::SshRemoteProcessPrivate(command, channelId, sendFacility, this)) -{ - init(); -} - -SshRemoteProcess::SshRemoteProcess(quint32 channelId, Internal::SshSendFacility &sendFacility) - : d(new Internal::SshRemoteProcessPrivate(channelId, sendFacility, this)) -{ - init(); -} - -SshRemoteProcess::~SshRemoteProcess() -{ - Q_ASSERT(d->channelState() == Internal::SshRemoteProcessPrivate::Inactive - || d->channelState() == Internal::SshRemoteProcessPrivate::CloseRequested - || d->channelState() == Internal::SshRemoteProcessPrivate::Closed); - delete d; -} - -bool SshRemoteProcess::atEnd() const -{ - return QIODevice::atEnd() && d->data().isEmpty(); -} - -qint64 SshRemoteProcess::bytesAvailable() const -{ - return QIODevice::bytesAvailable() + d->data().count(); -} - -bool SshRemoteProcess::canReadLine() const -{ - return QIODevice::canReadLine() || d->data().contains('\n'); -} - -QByteArray SshRemoteProcess::readAllStandardOutput() -{ - return readAllFromChannel(QProcess::StandardOutput); -} - -QByteArray SshRemoteProcess::readAllStandardError() -{ - return readAllFromChannel(QProcess::StandardError); -} - -QByteArray SshRemoteProcess::readAllFromChannel(QProcess::ProcessChannel channel) -{ - const QProcess::ProcessChannel currentReadChannel = readChannel(); - setReadChannel(channel); - const QByteArray &data = readAll(); - setReadChannel(currentReadChannel); - return data; -} - -void SshRemoteProcess::close() -{ - d->closeChannel(); - QIODevice::close(); -} - -qint64 SshRemoteProcess::readData(char *data, qint64 maxlen) -{ - const qint64 bytesRead = qMin(qint64(d->data().count()), maxlen); - memcpy(data, d->data().constData(), bytesRead); - d->data().remove(0, bytesRead); - return bytesRead; -} - -qint64 SshRemoteProcess::writeData(const char *data, qint64 len) -{ - if (isRunning()) { - d->sendData(QByteArray(data, len)); - return len; - } - return 0; -} - -QProcess::ProcessChannel SshRemoteProcess::readChannel() const -{ - return d->m_readChannel; -} - -void SshRemoteProcess::setReadChannel(QProcess::ProcessChannel channel) -{ - d->m_readChannel = channel; -} - -void SshRemoteProcess::init() -{ - connect(d, SIGNAL(started()), this, SIGNAL(started()), - Qt::QueuedConnection); - connect(d, SIGNAL(readyReadStandardOutput()), this, SIGNAL(readyReadStandardOutput()), - Qt::QueuedConnection); - connect(d, SIGNAL(readyRead()), this, SIGNAL(readyRead()), Qt::QueuedConnection); - connect(d, SIGNAL(readyReadStandardError()), this, - SIGNAL(readyReadStandardError()), Qt::QueuedConnection); - connect(d, SIGNAL(closed(int)), this, SIGNAL(closed(int)), Qt::QueuedConnection); -} - -void SshRemoteProcess::addToEnvironment(const QByteArray &var, const QByteArray &value) -{ - if (d->channelState() == Internal::SshRemoteProcessPrivate::Inactive) - d->m_env << qMakePair(var, value); // Cached locally and sent on start() -} - -void SshRemoteProcess::requestTerminal(const SshPseudoTerminal &terminal) -{ - QTC_ASSERT(d->channelState() == Internal::SshRemoteProcessPrivate::Inactive, return); - d->m_useTerminal = true; - d->m_terminal = terminal; -} - -void SshRemoteProcess::start() -{ - if (d->channelState() == Internal::SshRemoteProcessPrivate::Inactive) { -#ifdef CREATOR_SSH_DEBUG - qDebug("process start requested, channel id = %u", d->localChannelId()); -#endif - QIODevice::open(QIODevice::ReadWrite); - d->requestSessionStart(); - } -} - -void SshRemoteProcess::sendSignal(Signal signal) -{ - try { - if (isRunning()) { - const char *signalString = 0; - for (size_t i = 0; i < sizeof signalMap/sizeof *signalMap && !signalString; ++i) { - if (signalMap[i].signalEnum == signal) - signalString = signalMap[i].signalString; - } - QTC_ASSERT(signalString, return); - d->m_sendFacility.sendChannelSignalPacket(d->remoteChannel(), signalString); - } - } catch (Botan::Exception &e) { - setErrorString(QString::fromAscii(e.what())); - d->closeChannel(); - } -} - -bool SshRemoteProcess::isRunning() const -{ - return d->m_procState == Internal::SshRemoteProcessPrivate::Running; -} - -int SshRemoteProcess::exitCode() const { return d->m_exitCode; } - -SshRemoteProcess::Signal SshRemoteProcess::exitSignal() const -{ - return static_cast(d->m_signal); -} - -namespace Internal { - -SshRemoteProcessPrivate::SshRemoteProcessPrivate(const QByteArray &command, - quint32 channelId, SshSendFacility &sendFacility, SshRemoteProcess *proc) - : AbstractSshChannel(channelId, sendFacility), - m_command(command), - m_isShell(false), - m_useTerminal(false), - m_proc(proc) -{ - init(); -} - -SshRemoteProcessPrivate::SshRemoteProcessPrivate(quint32 channelId, SshSendFacility &sendFacility, - SshRemoteProcess *proc) - : AbstractSshChannel(channelId, sendFacility), - m_isShell(true), - m_useTerminal(true), - m_proc(proc) -{ - init(); -} - -void SshRemoteProcessPrivate::init() -{ - m_procState = NotYetStarted; - m_wasRunning = false; - m_exitCode = 0; - m_readChannel = QProcess::StandardOutput; - m_signal = SshRemoteProcess::NoSignal; -} - -void SshRemoteProcessPrivate::setProcState(ProcessState newState) -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("channel: old state = %d,new state = %d", m_procState, newState); -#endif - m_procState = newState; - if (newState == StartFailed) { - emit closed(SshRemoteProcess::FailedToStart); - } else if (newState == Running) { - m_wasRunning = true; - emit started(); - } -} - -QByteArray &SshRemoteProcessPrivate::data() -{ - return m_readChannel == QProcess::StandardOutput ? m_stdout : m_stderr; -} - -void SshRemoteProcessPrivate::closeHook() -{ - if (m_wasRunning) { - if (m_signal != SshRemoteProcess::NoSignal) - emit closed(SshRemoteProcess::KilledBySignal); - else - emit closed(SshRemoteProcess::ExitedNormally); - } -} - -void SshRemoteProcessPrivate::handleOpenSuccessInternal() -{ - foreach (const EnvVar &envVar, m_env) { - m_sendFacility.sendEnvPacket(remoteChannel(), envVar.first, - envVar.second); - } - - if (m_useTerminal) - m_sendFacility.sendPtyRequestPacket(remoteChannel(), m_terminal); - - if (m_isShell) - m_sendFacility.sendShellPacket(remoteChannel()); - else - m_sendFacility.sendExecPacket(remoteChannel(), m_command); - setProcState(ExecRequested); - m_timeoutTimer->start(ReplyTimeout); -} - -void SshRemoteProcessPrivate::handleOpenFailureInternal(const QString &reason) -{ - setProcState(StartFailed); - m_proc->setErrorString(reason); -} - -void SshRemoteProcessPrivate::handleChannelSuccess() -{ - if (m_procState != ExecRequested) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_MSG_CHANNEL_SUCCESS message."); - } - m_timeoutTimer->stop(); - setProcState(Running); -} - -void SshRemoteProcessPrivate::handleChannelFailure() -{ - if (m_procState != ExecRequested) { - throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_PROTOCOL_ERROR, - "Unexpected SSH_MSG_CHANNEL_FAILURE message."); - } - m_timeoutTimer->stop(); - setProcState(StartFailed); - closeChannel(); -} - -void SshRemoteProcessPrivate::handleChannelDataInternal(const QByteArray &data) -{ - m_stdout += data; - emit readyReadStandardOutput(); - if (m_readChannel == QProcess::StandardOutput) - emit readyRead(); -} - -void SshRemoteProcessPrivate::handleChannelExtendedDataInternal(quint32 type, - const QByteArray &data) -{ - if (type != SSH_EXTENDED_DATA_STDERR) { - qWarning("Unknown extended data type %u", type); - } else { - m_stderr += data; - emit readyReadStandardError(); - if (m_readChannel == QProcess::StandardError) - emit readyRead(); - } -} - -void SshRemoteProcessPrivate::handleExitStatus(const SshChannelExitStatus &exitStatus) -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("Process exiting with exit code %d", exitStatus.exitStatus); -#endif - m_exitCode = exitStatus.exitStatus; - m_procState = Exited; -} - -void SshRemoteProcessPrivate::handleExitSignal(const SshChannelExitSignal &signal) -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("Exit due to signal %s", signal.signal.data()); -#endif - - for (size_t i = 0; i < sizeof signalMap/sizeof *signalMap; ++i) { - if (signalMap[i].signalString == signal.signal) { - m_signal = signalMap[i].signalEnum; - m_procState = Exited; - m_proc->setErrorString(tr("Process killed by signal")); - return; - } - } - - throw SshServerException(SSH_DISCONNECT_PROTOCOL_ERROR, "Invalid signal", - tr("Server sent invalid signal '%1'").arg(QString::fromUtf8(signal.signal))); -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshremoteprocess.h b/src/libs/utils/ssh/sshremoteprocess.h deleted file mode 100644 index ed4e96e5d7..0000000000 --- a/src/libs/utils/ssh/sshremoteprocess.h +++ /dev/null @@ -1,130 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHREMOTECOMMAND_H -#define SSHREMOTECOMMAND_H - -#include - -#include -#include - -QT_BEGIN_NAMESPACE -class QByteArray; -QT_END_NAMESPACE - -namespace Utils { -class SshPseudoTerminal; -namespace Internal { -class SshChannelManager; -class SshRemoteProcessPrivate; -class SshSendFacility; -} // namespace Internal - -// TODO: ProcessChannel -class QTCREATOR_UTILS_EXPORT SshRemoteProcess : public QIODevice -{ - Q_OBJECT - - friend class Internal::SshChannelManager; - friend class Internal::SshRemoteProcessPrivate; - -public: - typedef QSharedPointer Ptr; - enum ExitStatus { FailedToStart, KilledBySignal, ExitedNormally }; - enum Signal { - AbrtSignal, AlrmSignal, FpeSignal, HupSignal, IllSignal, IntSignal, KillSignal, PipeSignal, - QuitSignal, SegvSignal, TermSignal, Usr1Signal, Usr2Signal, NoSignal - }; - - ~SshRemoteProcess(); - - // QIODevice stuff - bool atEnd() const; - qint64 bytesAvailable() const; - bool canReadLine() const; - void close(); - bool isSequential() const { return true; } - - QProcess::ProcessChannel readChannel() const; - void setReadChannel(QProcess::ProcessChannel channel); - - /* - * Note that this is of limited value in practice, because servers are - * usually configured to ignore such requests for security reasons. - */ - void addToEnvironment(const QByteArray &var, const QByteArray &value); - - void requestTerminal(const SshPseudoTerminal &terminal); - void start(); - - bool isRunning() const; - int exitCode() const; - Signal exitSignal() const; - - QByteArray readAllStandardOutput(); - QByteArray readAllStandardError(); - - // Note: This is ignored by the OpenSSH server. - void sendSignal(Signal signal); - void kill() { sendSignal(KillSignal); } - -signals: - void started(); - - void readyReadStandardOutput(); - void readyReadStandardError(); - - /* - * Parameter is of type ExitStatus, but we use int because of - * signal/slot awkwardness (full namespace required). - */ - void closed(int exitStatus); - -private: - SshRemoteProcess(const QByteArray &command, quint32 channelId, - Internal::SshSendFacility &sendFacility); - SshRemoteProcess(quint32 channelId, Internal::SshSendFacility &sendFacility); - - // QIODevice stuff - qint64 readData(char *data, qint64 maxlen); - qint64 writeData(const char *data, qint64 len); - - void init(); - QByteArray readAllFromChannel(QProcess::ProcessChannel channel); - - Internal::SshRemoteProcessPrivate *d; -}; - -} // namespace Utils - -#endif // SSHREMOTECOMMAND_H diff --git a/src/libs/utils/ssh/sshremoteprocess_p.h b/src/libs/utils/ssh/sshremoteprocess_p.h deleted file mode 100644 index eba60a6ac1..0000000000 --- a/src/libs/utils/ssh/sshremoteprocess_p.h +++ /dev/null @@ -1,114 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHREMOTEPROCESS_P_H -#define SSHREMOTEPROCESS_P_H - -#include "sshpseudoterminal.h" - -#include "sshchannel_p.h" - -#include -#include -#include - -namespace Utils { -class SshRemoteProcess; - -namespace Internal { -class SshSendFacility; - -class SshRemoteProcessPrivate : public AbstractSshChannel -{ - Q_OBJECT - friend class Utils::SshRemoteProcess; -public: - enum ProcessState { - NotYetStarted, ExecRequested, StartFailed, Running, Exited - }; - - virtual void handleChannelSuccess(); - virtual void handleChannelFailure(); - - virtual void closeHook(); - - QByteArray &data(); - -signals: - void started(); - void readyRead(); - void readyReadStandardOutput(); - void readyReadStandardError(); - void closed(int exitStatus); - -private: - SshRemoteProcessPrivate(const QByteArray &command, quint32 channelId, - SshSendFacility &sendFacility, SshRemoteProcess *proc); - SshRemoteProcessPrivate(quint32 channelId, SshSendFacility &sendFacility, - SshRemoteProcess *proc); - - virtual void handleOpenSuccessInternal(); - virtual void handleOpenFailureInternal(const QString &reason); - virtual void handleChannelDataInternal(const QByteArray &data); - virtual void handleChannelExtendedDataInternal(quint32 type, - const QByteArray &data); - virtual void handleExitStatus(const SshChannelExitStatus &exitStatus); - virtual void handleExitSignal(const SshChannelExitSignal &signal); - - void init(); - void setProcState(ProcessState newState); - - QProcess::ProcessChannel m_readChannel; - - ProcessState m_procState; - bool m_wasRunning; - int m_signal; - int m_exitCode; - - const QByteArray m_command; - const bool m_isShell; - - typedef QPair EnvVar; - QList m_env; - bool m_useTerminal; - SshPseudoTerminal m_terminal; - - QByteArray m_stdout; - QByteArray m_stderr; - - SshRemoteProcess *m_proc; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHREMOTEPROCESS_P_H diff --git a/src/libs/utils/ssh/sshremoteprocessrunner.cpp b/src/libs/utils/ssh/sshremoteprocessrunner.cpp deleted file mode 100644 index 16d77c471e..0000000000 --- a/src/libs/utils/ssh/sshremoteprocessrunner.cpp +++ /dev/null @@ -1,271 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshremoteprocessrunner.h" - -#include "sshconnectionmanager.h" -#include "sshpseudoterminal.h" - -#include - - -/*! - \class Utils::SshRemoteProcessRunner - - \brief Convenience class for running a remote process over an SSH connection. -*/ - -namespace Utils { -namespace Internal { -namespace { -enum State { Inactive, Connecting, Connected, ProcessRunning }; -} // anonymous namespace - -class SshRemoteProcessRunnerPrivate -{ -public: - SshRemoteProcessRunnerPrivate() : m_state(Inactive) {} - - SshRemoteProcess::Ptr m_process; - SshConnection::Ptr m_connection; - bool m_runInTerminal; - SshPseudoTerminal m_terminal; - QByteArray m_command; - Utils::SshError m_lastConnectionError; - QString m_lastConnectionErrorString; - SshRemoteProcess::ExitStatus m_exitStatus; - SshRemoteProcess::Signal m_exitSignal; - int m_exitCode; - QString m_processErrorString; - State m_state; -}; - -} // namespace Internal - -using namespace Internal; - -SshRemoteProcessRunner::SshRemoteProcessRunner(QObject *parent) - : QObject(parent), d(new SshRemoteProcessRunnerPrivate) -{ -} - -SshRemoteProcessRunner::~SshRemoteProcessRunner() -{ - disconnect(); - setState(Inactive); - delete d; -} - -void SshRemoteProcessRunner::run(const QByteArray &command, - const SshConnectionParameters &sshParams) -{ - QTC_ASSERT(d->m_state == Inactive, return); - - d->m_runInTerminal = false; - runInternal(command, sshParams); -} - -void SshRemoteProcessRunner::runInTerminal(const QByteArray &command, - const SshPseudoTerminal &terminal, const SshConnectionParameters &sshParams) -{ - d->m_terminal = terminal; - d->m_runInTerminal = true; - runInternal(command, sshParams); -} - -void SshRemoteProcessRunner::runInternal(const QByteArray &command, - const SshConnectionParameters &sshParams) -{ - setState(Connecting); - - d->m_lastConnectionError = SshNoError; - d->m_lastConnectionErrorString.clear(); - d->m_processErrorString.clear(); - d->m_exitSignal = SshRemoteProcess::NoSignal; - d->m_exitCode = -1; - d->m_command = command; - d->m_connection = SshConnectionManager::instance().acquireConnection(sshParams); - connect(d->m_connection.data(), SIGNAL(error(Utils::SshError)), - SLOT(handleConnectionError(Utils::SshError))); - connect(d->m_connection.data(), SIGNAL(disconnected()), SLOT(handleDisconnected())); - if (d->m_connection->state() == SshConnection::Connected) { - handleConnected(); - } else { - connect(d->m_connection.data(), SIGNAL(connected()), SLOT(handleConnected())); - if (d->m_connection->state() == SshConnection::Unconnected) - d->m_connection->connectToHost(); - } -} - -void SshRemoteProcessRunner::handleConnected() -{ - QTC_ASSERT(d->m_state == Connecting, return); - setState(Connected); - - d->m_process = d->m_connection->createRemoteProcess(d->m_command); - connect(d->m_process.data(), SIGNAL(started()), SLOT(handleProcessStarted())); - connect(d->m_process.data(), SIGNAL(closed(int)), SLOT(handleProcessFinished(int))); - connect(d->m_process.data(), SIGNAL(readyReadStandardOutput()), SLOT(handleStdout())); - connect(d->m_process.data(), SIGNAL(readyReadStandardError()), SLOT(handleStderr())); - if (d->m_runInTerminal) - d->m_process->requestTerminal(d->m_terminal); - d->m_process->start(); -} - -void SshRemoteProcessRunner::handleConnectionError(Utils::SshError error) -{ - d->m_lastConnectionError = error; - d->m_lastConnectionErrorString = d->m_connection->errorString(); - handleDisconnected(); - emit connectionError(); -} - -void SshRemoteProcessRunner::handleDisconnected() -{ - QTC_ASSERT(d->m_state == Connecting || d->m_state == Connected - || d->m_state == ProcessRunning, return); - setState(Inactive); -} - -void SshRemoteProcessRunner::handleProcessStarted() -{ - QTC_ASSERT(d->m_state == Connected, return); - - setState(ProcessRunning); - emit processStarted(); -} - -void SshRemoteProcessRunner::handleProcessFinished(int exitStatus) -{ - d->m_exitStatus = static_cast(exitStatus); - switch (d->m_exitStatus) { - case SshRemoteProcess::FailedToStart: - QTC_ASSERT(d->m_state == Connected, return); - break; - case SshRemoteProcess::KilledBySignal: - QTC_ASSERT(d->m_state == ProcessRunning, return); - d->m_exitSignal = d->m_process->exitSignal(); - break; - case SshRemoteProcess::ExitedNormally: - QTC_ASSERT(d->m_state == ProcessRunning, return); - d->m_exitCode = d->m_process->exitCode(); - break; - default: - Q_ASSERT_X(false, Q_FUNC_INFO, "Impossible exit status."); - } - d->m_processErrorString = d->m_process->errorString(); - setState(Inactive); - emit processClosed(exitStatus); -} - -void SshRemoteProcessRunner::handleStdout() -{ - emit processOutputAvailable(d->m_process->readAllStandardOutput()); -} - -void SshRemoteProcessRunner::handleStderr() -{ - emit processErrorOutputAvailable(d->m_process->readAllStandardError()); -} - -void SshRemoteProcessRunner::setState(int newState) -{ - if (d->m_state == newState) - return; - - d->m_state = static_cast(newState); - if (d->m_state == Inactive) { - if (d->m_process) { - disconnect(d->m_process.data(), 0, this, 0); - d->m_process->close(); - d->m_process.clear(); - } - if (d->m_connection) { - disconnect(d->m_connection.data(), 0, this, 0); - SshConnectionManager::instance().releaseConnection(d->m_connection); - d->m_connection.clear(); - } - } -} - -QByteArray SshRemoteProcessRunner::command() const { return d->m_command; } -SshError SshRemoteProcessRunner::lastConnectionError() const { return d->m_lastConnectionError; } -QString SshRemoteProcessRunner::lastConnectionErrorString() const { - return d->m_lastConnectionErrorString; -} - -bool SshRemoteProcessRunner::isProcessRunning() const -{ - return d->m_process && d->m_process->isRunning(); -} - -SshRemoteProcess::ExitStatus SshRemoteProcessRunner::processExitStatus() const -{ - QTC_CHECK(!isProcessRunning()); - return d->m_exitStatus; -} - -SshRemoteProcess::Signal SshRemoteProcessRunner::processExitSignal() const -{ - QTC_CHECK(processExitStatus() == SshRemoteProcess::KilledBySignal); - return d->m_exitSignal; -} - -int SshRemoteProcessRunner::processExitCode() const -{ - QTC_CHECK(processExitStatus() == SshRemoteProcess::ExitedNormally); - return d->m_exitCode; -} - -QString SshRemoteProcessRunner::processErrorString() const -{ - return d->m_processErrorString; -} - -void SshRemoteProcessRunner::writeDataToProcess(const QByteArray &data) -{ - QTC_ASSERT(isProcessRunning(), return); - d->m_process->write(data); -} - -void SshRemoteProcessRunner::sendSignalToProcess(SshRemoteProcess::Signal signal) -{ - QTC_ASSERT(isProcessRunning(), return); - d->m_process->sendSignal(signal); -} - -void SshRemoteProcessRunner::cancel() -{ - setState(Inactive); -} - -} // namespace Utils diff --git a/src/libs/utils/ssh/sshremoteprocessrunner.h b/src/libs/utils/ssh/sshremoteprocessrunner.h deleted file mode 100644 index 54bcf6d910..0000000000 --- a/src/libs/utils/ssh/sshremoteprocessrunner.h +++ /dev/null @@ -1,94 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHREMOTEPROCESSRUNNER_H -#define SSHREMOTEPROCESSRUNNER_H - -#include "sshconnection.h" -#include "sshremoteprocess.h" - -namespace Utils { -namespace Internal { -class SshRemoteProcessRunnerPrivate; -} // namespace Internal - -class QTCREATOR_UTILS_EXPORT SshRemoteProcessRunner : public QObject -{ - Q_OBJECT - -public: - SshRemoteProcessRunner(QObject *parent = 0); - ~SshRemoteProcessRunner(); - - void run(const QByteArray &command, const SshConnectionParameters &sshParams); - void runInTerminal(const QByteArray &command, const SshPseudoTerminal &terminal, - const SshConnectionParameters &sshParams); - QByteArray command() const; - - Utils::SshError lastConnectionError() const; - QString lastConnectionErrorString() const; - - bool isProcessRunning() const; - void writeDataToProcess(const QByteArray &data); - void sendSignalToProcess(SshRemoteProcess::Signal signal); // No effect with OpenSSH server. - void cancel(); // Does not stop remote process, just frees SSH-related process resources. - SshRemoteProcess::ExitStatus processExitStatus() const; - SshRemoteProcess::Signal processExitSignal() const; - int processExitCode() const; - QString processErrorString() const; - -private slots: - void handleConnected(); - void handleConnectionError(Utils::SshError error); - void handleDisconnected(); - void handleProcessStarted(); - void handleProcessFinished(int exitStatus); - void handleStdout(); - void handleStderr(); - -signals: - void connectionError(); - void processStarted(); - void processOutputAvailable(const QByteArray &output); - void processErrorOutputAvailable(const QByteArray &output); - void processClosed(int exitStatus); // values are of type SshRemoteProcess::ExitStatus - -private: - void runInternal(const QByteArray &command, const Utils::SshConnectionParameters &sshParams); - void setState(int newState); - - Internal::SshRemoteProcessRunnerPrivate * const d; -}; - -} // namespace Utils - -#endif // SSHREMOTEPROCESSRUNNER_H diff --git a/src/libs/utils/ssh/sshsendfacility.cpp b/src/libs/utils/ssh/sshsendfacility.cpp deleted file mode 100644 index 771d145405..0000000000 --- a/src/libs/utils/ssh/sshsendfacility.cpp +++ /dev/null @@ -1,222 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#include "sshsendfacility_p.h" - -#include "sshkeyexchange_p.h" -#include "sshoutgoingpacket_p.h" - -#include - -namespace Utils { -namespace Internal { - -SshSendFacility::SshSendFacility(QTcpSocket *socket) - : m_clientSeqNr(0), m_socket(socket), - m_outgoingPacket(m_encrypter, m_clientSeqNr) -{ -} - -void SshSendFacility::sendPacket() -{ -#ifdef CREATOR_SSH_DEBUG - qDebug("Sending packet, client seq nr is %u", m_clientSeqNr); -#endif - if (m_socket->isValid() - && m_socket->state() == QAbstractSocket::ConnectedState) { - m_socket->write(m_outgoingPacket.rawData()); - ++m_clientSeqNr; - } -} - -void SshSendFacility::reset() -{ - m_clientSeqNr = 0; - m_encrypter.clearKeys(); -} - -void SshSendFacility::recreateKeys(const SshKeyExchange &keyExchange) -{ - m_encrypter.recreateKeys(keyExchange); -} - -void SshSendFacility::createAuthenticationKey(const QByteArray &privKeyFileContents) -{ - m_encrypter.createAuthenticationKey(privKeyFileContents); -} - -QByteArray SshSendFacility::sendKeyExchangeInitPacket() -{ - const QByteArray &payLoad = m_outgoingPacket.generateKeyExchangeInitPacket(); - sendPacket(); - return payLoad; -} - -void SshSendFacility::sendKeyDhInitPacket(const Botan::BigInt &e) -{ - m_outgoingPacket.generateKeyDhInitPacket(e); - sendPacket(); -} - -void SshSendFacility::sendNewKeysPacket() -{ - m_outgoingPacket.generateNewKeysPacket(); - sendPacket(); -} - -void SshSendFacility::sendDisconnectPacket(SshErrorCode reason, - const QByteArray &reasonString) -{ - m_outgoingPacket.generateDisconnectPacket(reason, reasonString); - sendPacket(); - } - -void SshSendFacility::sendMsgUnimplementedPacket(quint32 serverSeqNr) -{ - m_outgoingPacket.generateMsgUnimplementedPacket(serverSeqNr); - sendPacket(); -} - -void SshSendFacility::sendUserAuthServiceRequestPacket() -{ - m_outgoingPacket.generateUserAuthServiceRequestPacket(); - sendPacket(); -} - -void SshSendFacility::sendUserAuthByPwdRequestPacket(const QByteArray &user, - const QByteArray &service, const QByteArray &pwd) -{ - m_outgoingPacket.generateUserAuthByPwdRequestPacket(user, service, pwd); - sendPacket(); - } - -void SshSendFacility::sendUserAuthByKeyRequestPacket(const QByteArray &user, - const QByteArray &service) -{ - m_outgoingPacket.generateUserAuthByKeyRequestPacket(user, service); - sendPacket(); -} - -void SshSendFacility::sendRequestFailurePacket() -{ - m_outgoingPacket.generateRequestFailurePacket(); - sendPacket(); -} - -void SshSendFacility::sendIgnorePacket() -{ - m_outgoingPacket.generateIgnorePacket(); - sendPacket(); -} - -void SshSendFacility::sendInvalidPacket() -{ - m_outgoingPacket.generateInvalidMessagePacket(); - sendPacket(); -} - -void SshSendFacility::sendSessionPacket(quint32 channelId, quint32 windowSize, - quint32 maxPacketSize) -{ - m_outgoingPacket.generateSessionPacket(channelId, windowSize, - maxPacketSize); - sendPacket(); -} - -void SshSendFacility::sendPtyRequestPacket(quint32 remoteChannel, - const SshPseudoTerminal &terminal) -{ - m_outgoingPacket.generatePtyRequestPacket(remoteChannel, terminal); - sendPacket(); -} - -void SshSendFacility::sendEnvPacket(quint32 remoteChannel, - const QByteArray &var, const QByteArray &value) -{ - m_outgoingPacket.generateEnvPacket(remoteChannel, var, value); - sendPacket(); -} - -void SshSendFacility::sendExecPacket(quint32 remoteChannel, - const QByteArray &command) -{ - m_outgoingPacket.generateExecPacket(remoteChannel, command); - sendPacket(); -} - -void SshSendFacility::sendShellPacket(quint32 remoteChannel) -{ - m_outgoingPacket.generateShellPacket(remoteChannel); - sendPacket(); -} - -void SshSendFacility::sendSftpPacket(quint32 remoteChannel) -{ - m_outgoingPacket.generateSftpPacket(remoteChannel); - sendPacket(); -} - -void SshSendFacility::sendWindowAdjustPacket(quint32 remoteChannel, - quint32 bytesToAdd) -{ - m_outgoingPacket.generateWindowAdjustPacket(remoteChannel, bytesToAdd); - sendPacket(); -} - -void SshSendFacility::sendChannelDataPacket(quint32 remoteChannel, - const QByteArray &data) -{ - m_outgoingPacket.generateChannelDataPacket(remoteChannel, data); - sendPacket(); -} - -void SshSendFacility::sendChannelSignalPacket(quint32 remoteChannel, - const QByteArray &signalName) -{ - m_outgoingPacket.generateChannelSignalPacket(remoteChannel, signalName); - sendPacket(); -} - -void SshSendFacility::sendChannelEofPacket(quint32 remoteChannel) -{ - m_outgoingPacket.generateChannelEofPacket(remoteChannel); - sendPacket(); -} - -void SshSendFacility::sendChannelClosePacket(quint32 remoteChannel) -{ - m_outgoingPacket.generateChannelClosePacket(remoteChannel); - sendPacket(); -} - -} // namespace Internal -} // namespace Utils diff --git a/src/libs/utils/ssh/sshsendfacility_p.h b/src/libs/utils/ssh/sshsendfacility_p.h deleted file mode 100644 index b7a243d3cc..0000000000 --- a/src/libs/utils/ssh/sshsendfacility_p.h +++ /dev/null @@ -1,101 +0,0 @@ -/************************************************************************** -** -** This file is part of Qt Creator -** -** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies). -** -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** -** GNU Lesser General Public License Usage -** -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this file. -** Please review the following information to ensure the GNU Lesser General -** Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** Other Usage -** -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -**************************************************************************/ - -#ifndef SSHCONNECTIONOUTSTATE_P_H -#define SSHCONNECTIONOUTSTATE_P_H - -#include "sshcryptofacility_p.h" -#include "sshoutgoingpacket_p.h" - -QT_BEGIN_NAMESPACE -class QTcpSocket; -QT_END_NAMESPACE - - -namespace Utils { -class SshPseudoTerminal; - -namespace Internal { -class SshKeyExchange; - -class SshSendFacility -{ -public: - SshSendFacility(QTcpSocket *socket); - void reset(); - void recreateKeys(const SshKeyExchange &keyExchange); - void createAuthenticationKey(const QByteArray &privKeyFileContents); - - QByteArray sendKeyExchangeInitPacket(); - void sendKeyDhInitPacket(const Botan::BigInt &e); - void sendNewKeysPacket(); - void sendDisconnectPacket(SshErrorCode reason, - const QByteArray &reasonString); - void sendMsgUnimplementedPacket(quint32 serverSeqNr); - void sendUserAuthServiceRequestPacket(); - void sendUserAuthByPwdRequestPacket(const QByteArray &user, - const QByteArray &service, const QByteArray &pwd); - void sendUserAuthByKeyRequestPacket(const QByteArray &user, - const QByteArray &service); - void sendRequestFailurePacket(); - void sendIgnorePacket(); - void sendInvalidPacket(); - void sendSessionPacket(quint32 channelId, quint32 windowSize, - quint32 maxPacketSize); - void sendPtyRequestPacket(quint32 remoteChannel, - const SshPseudoTerminal &terminal); - void sendEnvPacket(quint32 remoteChannel, const QByteArray &var, - const QByteArray &value); - void sendExecPacket(quint32 remoteChannel, const QByteArray &command); - void sendShellPacket(quint32 remoteChannel); - void sendSftpPacket(quint32 remoteChannel); - void sendWindowAdjustPacket(quint32 remoteChannel, quint32 bytesToAdd); - void sendChannelDataPacket(quint32 remoteChannel, const QByteArray &data); - void sendChannelSignalPacket(quint32 remoteChannel, - const QByteArray &signalName); - void sendChannelEofPacket(quint32 remoteChannel); - void sendChannelClosePacket(quint32 remoteChannel); - quint32 nextClientSeqNr() const { return m_clientSeqNr; } - -private: - void sendPacket(); - - quint32 m_clientSeqNr; - SshEncryptionFacility m_encrypter; - QTcpSocket *m_socket; - SshOutgoingPacket m_outgoingPacket; -}; - -} // namespace Internal -} // namespace Utils - -#endif // SSHCONNECTIONOUTSTATE_P_H diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri index 69050825cd..807f72cf5f 100644 --- a/src/libs/utils/utils-lib.pri +++ b/src/libs/utils/utils-lib.pri @@ -5,7 +5,7 @@ dll { } INCLUDEPATH += $$PWD -QT += network script +QT += script network CONFIG += exceptions # used by portlist.cpp, textfileformat.cpp, and ssh/* @@ -67,30 +67,6 @@ SOURCES += $$PWD/environment.cpp \ $$PWD/fileinprojectfinder.cpp \ $$PWD/ipaddresslineedit.cpp \ $$PWD/statuslabel.cpp \ - $$PWD/ssh/sshsendfacility.cpp \ - $$PWD/ssh/sshremoteprocess.cpp \ - $$PWD/ssh/sshpacketparser.cpp \ - $$PWD/ssh/sshpacket.cpp \ - $$PWD/ssh/sshoutgoingpacket.cpp \ - $$PWD/ssh/sshkeygenerator.cpp \ - $$PWD/ssh/sshkeyexchange.cpp \ - $$PWD/ssh/sshincomingpacket.cpp \ - $$PWD/ssh/sshcryptofacility.cpp \ - $$PWD/ssh/sshconnection.cpp \ - $$PWD/ssh/sshchannelmanager.cpp \ - $$PWD/ssh/sshchannel.cpp \ - $$PWD/ssh/sshcapabilities.cpp \ - $$PWD/ssh/sftppacket.cpp \ - $$PWD/ssh/sftpoutgoingpacket.cpp \ - $$PWD/ssh/sftpoperation.cpp \ - $$PWD/ssh/sftpincomingpacket.cpp \ - $$PWD/ssh/sftpdefs.cpp \ - $$PWD/ssh/sftpchannel.cpp \ - $$PWD/ssh/sshremoteprocessrunner.cpp \ - $$PWD/ssh/sshconnectionmanager.cpp \ - $$PWD/ssh/sshkeypasswordretriever.cpp \ - $$PWD/ssh/sftpfilesystemmodel.cpp \ - $$PWD/ssh/sshkeycreationdialog.cpp \ $$PWD/outputformatter.cpp \ $$PWD/flowlayout.cpp \ $$PWD/networkaccessmanager.cpp \ @@ -172,37 +148,6 @@ HEADERS += \ $$PWD/annotateditemdelegate.h \ $$PWD/fileinprojectfinder.h \ $$PWD/ipaddresslineedit.h \ - $$PWD/ssh/sshsendfacility_p.h \ - $$PWD/ssh/sshremoteprocess.h \ - $$PWD/ssh/sshremoteprocess_p.h \ - $$PWD/ssh/sshpacketparser_p.h \ - $$PWD/ssh/sshpacket_p.h \ - $$PWD/ssh/sshoutgoingpacket_p.h \ - $$PWD/ssh/sshkeygenerator.h \ - $$PWD/ssh/sshkeyexchange_p.h \ - $$PWD/ssh/sshincomingpacket_p.h \ - $$PWD/ssh/sshexception_p.h \ - $$PWD/ssh/ssherrors.h \ - $$PWD/ssh/sshcryptofacility_p.h \ - $$PWD/ssh/sshconnection.h \ - $$PWD/ssh/sshconnection_p.h \ - $$PWD/ssh/sshchannelmanager_p.h \ - $$PWD/ssh/sshchannel_p.h \ - $$PWD/ssh/sshcapabilities_p.h \ - $$PWD/ssh/sshbotanconversions_p.h \ - $$PWD/ssh/sftppacket_p.h \ - $$PWD/ssh/sftpoutgoingpacket_p.h \ - $$PWD/ssh/sftpoperation_p.h \ - $$PWD/ssh/sftpincomingpacket_p.h \ - $$PWD/ssh/sftpdefs.h \ - $$PWD/ssh/sftpchannel.h \ - $$PWD/ssh/sftpchannel_p.h \ - $$PWD/ssh/sshremoteprocessrunner.h \ - $$PWD/ssh/sshconnectionmanager.h \ - $$PWD/ssh/sshpseudoterminal.h \ - $$PWD/ssh/sshkeypasswordretriever_p.h \ - $$PWD/ssh/sftpfilesystemmodel.h \ - $$PWD/ssh/sshkeycreationdialog.h \ $$PWD/statuslabel.h \ $$PWD/outputformatter.h \ $$PWD/outputformat.h \ @@ -221,7 +166,6 @@ HEADERS += \ FORMS += $$PWD/filewizardpage.ui \ $$PWD/projectintropage.ui \ $$PWD/newclasswidget.ui \ - $$PWD/submiteditorwidget.ui \ - $$PWD/ssh/sshkeycreationdialog.ui + $$PWD/submiteditorwidget.ui RESOURCES += $$PWD/utils.qrc diff --git a/src/libs/utils/utils.pro b/src/libs/utils/utils.pro index 8841790040..7829c45097 100644 --- a/src/libs/utils/utils.pro +++ b/src/libs/utils/utils.pro @@ -1,7 +1,6 @@ TEMPLATE = lib TARGET = Utils -QT += gui \ - network +QT += gui network include(../../qtcreatorlibrary.pri) include(utils_dependencies.pri) diff --git a/src/libs/utils/utils.qbs b/src/libs/utils/utils.qbs index c96f6c222b..1d64d6f28e 100644 --- a/src/libs/utils/utils.qbs +++ b/src/libs/utils/utils.qbs @@ -7,8 +7,6 @@ QtcLibrary { cpp.defines: ["QTCREATOR_UTILS_LIB"] cpp.includePaths: [ ".", "..", "../..", - "../3rdparty/botan/build", - "ssh", buildDirectory ] @@ -23,7 +21,6 @@ QtcLibrary { Depends { name: "cpp" } Depends { name: "Qt"; submodules: ['widgets', 'network', 'script', 'concurrent'] } - Depends { name: "Botan" } Depends { name: "app_version_header" } files: [ @@ -178,60 +175,6 @@ QtcLibrary { "images/crumblepath-segment.png", "images/removesubmitfield.png", "images/triangle_vert.png", - "ssh/sftpchannel.h", - "ssh/sftpchannel_p.h", - "ssh/sftpdefs.cpp", - "ssh/sftpdefs.h", - "ssh/sftpincomingpacket.cpp", - "ssh/sftpincomingpacket_p.h", - "ssh/sftpoperation.cpp", - "ssh/sftpoperation_p.h", - "ssh/sftpoutgoingpacket.cpp", - "ssh/sftpoutgoingpacket_p.h", - "ssh/sftppacket.cpp", - "ssh/sftppacket_p.h", - "ssh/sshbotanconversions_p.h", - "ssh/sshcapabilities_p.h", - "ssh/sshchannel.cpp", - "ssh/sshchannel_p.h", - "ssh/sshchannelmanager.cpp", - "ssh/sshchannelmanager_p.h", - "ssh/sshconnection.h", - "ssh/sshconnection_p.h", - "ssh/sshconnectionmanager.cpp", - "ssh/sshconnectionmanager.h", - "ssh/sshcryptofacility.cpp", - "ssh/sshcryptofacility_p.h", - "ssh/ssherrors.h", - "ssh/sshexception_p.h", - "ssh/sshincomingpacket_p.h", - "ssh/sshkeyexchange.cpp", - "ssh/sshkeyexchange_p.h", - "ssh/sshkeypasswordretriever_p.h", - "ssh/sshoutgoingpacket.cpp", - "ssh/sshoutgoingpacket_p.h", - "ssh/sshpacket.cpp", - "ssh/sshpacket_p.h", - "ssh/sshpacketparser.cpp", - "ssh/sshpacketparser_p.h", - "ssh/sshpseudoterminal.h", - "ssh/sshremoteprocess.cpp", - "ssh/sshremoteprocess.h", - "ssh/sshremoteprocess_p.h", - "ssh/sshremoteprocessrunner.cpp", - "ssh/sshremoteprocessrunner.h", - "ssh/sshsendfacility.cpp", - "ssh/sshsendfacility_p.h", - "ssh/sshkeypasswordretriever.cpp", - "ssh/sftpchannel.cpp", - "ssh/sshcapabilities.cpp", - "ssh/sshconnection.cpp", - "ssh/sshincomingpacket.cpp", - "ssh/sshkeygenerator.cpp", - "ssh/sshkeygenerator.h", - "ssh/sshkeycreationdialog.cpp", - "ssh/sshkeycreationdialog.h", - "ssh/sshkeycreationdialog.ui" ] Group { @@ -260,7 +203,7 @@ QtcLibrary { } ProductModule { - Depends { name: "Qt"; submodules: ["concurrent", "widgets", "network"] } + Depends { name: "Qt"; submodules: ["concurrent", "widgets" ] } } } diff --git a/src/libs/utils/utils_dependencies.pri b/src/libs/utils/utils_dependencies.pri index e2a3c96f45..e69de29bb2 100644 --- a/src/libs/utils/utils_dependencies.pri +++ b/src/libs/utils/utils_dependencies.pri @@ -1 +0,0 @@ -include(../3rdparty/botan/botan.pri) diff --git a/src/plugins/analyzerbase/analyzermanager.cpp b/src/plugins/analyzerbase/analyzermanager.cpp index 7a0ccbb6cc..3b5f2f17e0 100644 --- a/src/plugins/analyzerbase/analyzermanager.cpp +++ b/src/plugins/analyzerbase/analyzermanager.cpp @@ -74,7 +74,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/plugins/analyzerbase/analyzerstartparameters.h b/src/plugins/analyzerbase/analyzerstartparameters.h index d84bd95771..91a5c8b8aa 100644 --- a/src/plugins/analyzerbase/analyzerstartparameters.h +++ b/src/plugins/analyzerbase/analyzerstartparameters.h @@ -39,7 +39,7 @@ #include #include -#include +#include #include namespace Analyzer { @@ -54,7 +54,7 @@ public: {} StartMode startMode; - Utils::SshConnectionParameters connParams; + QSsh::SshConnectionParameters connParams; Core::Id toolId; QString debuggee; diff --git a/src/plugins/analyzerbase/startremotedialog.cpp b/src/plugins/analyzerbase/startremotedialog.cpp index 25b784f607..9436add8ba 100644 --- a/src/plugins/analyzerbase/startremotedialog.cpp +++ b/src/plugins/analyzerbase/startremotedialog.cpp @@ -35,7 +35,7 @@ #include "ui_startremotedialog.h" #include -#include +#include #include @@ -113,16 +113,16 @@ void StartRemoteDialog::validate() m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid); } -Utils::SshConnectionParameters StartRemoteDialog::sshParams() const +QSsh::SshConnectionParameters StartRemoteDialog::sshParams() const { - Utils::SshConnectionParameters params; + QSsh::SshConnectionParameters params; params.host = m_ui->host->text(); params.userName = m_ui->user->text(); if (m_ui->keyFile->isValid()) { - params.authenticationType = Utils::SshConnectionParameters::AuthenticationByKey; + params.authenticationType = QSsh::SshConnectionParameters::AuthenticationByKey; params.privateKeyFile = m_ui->keyFile->path(); } else { - params.authenticationType = Utils::SshConnectionParameters::AuthenticationByPassword; + params.authenticationType = QSsh::SshConnectionParameters::AuthenticationByPassword; params.password = m_ui->password->text(); } params.port = m_ui->port->value(); diff --git a/src/plugins/analyzerbase/startremotedialog.h b/src/plugins/analyzerbase/startremotedialog.h index 8e74fafb1e..e827d2e0d2 100644 --- a/src/plugins/analyzerbase/startremotedialog.h +++ b/src/plugins/analyzerbase/startremotedialog.h @@ -37,7 +37,7 @@ #include -namespace Utils { class SshConnectionParameters; } +namespace QSsh { class SshConnectionParameters; } namespace Analyzer { @@ -52,7 +52,7 @@ public: explicit StartRemoteDialog(QWidget *parent = 0); virtual ~StartRemoteDialog(); - Utils::SshConnectionParameters sshParams() const; + QSsh::SshConnectionParameters sshParams() const; QString executable() const; QString arguments() const; QString workingDirectory() const; diff --git a/src/plugins/cpaster/cpaster.qbs b/src/plugins/cpaster/cpaster.qbs index c3d596066d..230d02a127 100644 --- a/src/plugins/cpaster/cpaster.qbs +++ b/src/plugins/cpaster/cpaster.qbs @@ -5,7 +5,7 @@ import "../QtcPlugin.qbs" as QtcPlugin QtcPlugin { name: "CodePaster" - Depends { name: "qt"; submodules: ['widgets'] } + Depends { name: "qt"; submodules: ['widgets', 'network'] } Depends { name: "Core" } Depends { name: "TextEditor" } diff --git a/src/plugins/debugger/debugger.qbs b/src/plugins/debugger/debugger.qbs index d5124f9050..9cf8b36f30 100644 --- a/src/plugins/debugger/debugger.qbs +++ b/src/plugins/debugger/debugger.qbs @@ -16,6 +16,7 @@ QtcPlugin { Depends { name: "symbianutils" } Depends { name: "QmlJS" } Depends { name: "QmlDebug" } + Depends { name: "QtcSsh" } Depends { name: "cpp" } cpp.defines: ["DEBUGGER_LIBRARY"] @@ -314,6 +315,7 @@ QtcPlugin { ProductModule { Depends { name: "cpp" } + Depends { name: "QtcSsh" } cpp.includePaths: ["."] } } diff --git a/src/plugins/debugger/debugger_dependencies.pri b/src/plugins/debugger/debugger_dependencies.pri index 0fcdf49d33..e99a0c39f5 100644 --- a/src/plugins/debugger/debugger_dependencies.pri +++ b/src/plugins/debugger/debugger_dependencies.pri @@ -8,3 +8,4 @@ include(../../libs/cplusplus/cplusplus.pri) include(../../libs/utils/utils.pri) include(../../libs/symbianutils/symbianutils.pri) include(../../libs/qmljs/qmljs.pri) +include(../../libs/ssh/ssh.pri) diff --git a/src/plugins/debugger/debuggerplugin.cpp b/src/plugins/debugger/debuggerplugin.cpp index b09ee58486..648b31639d 100644 --- a/src/plugins/debugger/debuggerplugin.cpp +++ b/src/plugins/debugger/debuggerplugin.cpp @@ -1842,9 +1842,9 @@ void DebuggerPluginPrivate::startRemoteEngine() sp.connParams.password = dlg.password(); sp.connParams.timeout = 5; - sp.connParams.authenticationType = Utils::SshConnectionParameters::AuthenticationByPassword; + sp.connParams.authenticationType = QSsh::SshConnectionParameters::AuthenticationByPassword; sp.connParams.port = 22; - sp.connParams.proxyType = Utils::SshConnectionParameters::NoProxy; + sp.connParams.proxyType = QSsh::SshConnectionParameters::NoProxy; sp.executable = dlg.inferiorPath(); sp.serverStartScript = dlg.enginePath(); diff --git a/src/plugins/debugger/debuggerstartparameters.h b/src/plugins/debugger/debuggerstartparameters.h index 4f5ba16ba2..fbb7424bf6 100644 --- a/src/plugins/debugger/debuggerstartparameters.h +++ b/src/plugins/debugger/debuggerstartparameters.h @@ -36,7 +36,7 @@ #include "debugger_global.h" #include "debuggerconstants.h" -#include +#include #include #include #include @@ -119,7 +119,7 @@ public: QByteArray remoteSourcesDir; QString remoteMountPoint; QString localMountDir; - Utils::SshConnectionParameters connParams; + QSsh::SshConnectionParameters connParams; bool requestRemoteSetup; QString debuggerCommand; diff --git a/src/plugins/debugger/gdb/remotegdbprocess.cpp b/src/plugins/debugger/gdb/remotegdbprocess.cpp index cc510c2f88..a5e53ddd9f 100644 --- a/src/plugins/debugger/gdb/remotegdbprocess.cpp +++ b/src/plugins/debugger/gdb/remotegdbprocess.cpp @@ -36,18 +36,19 @@ #include #include -#include +#include #include #include +using namespace QSsh; using namespace Utils; namespace Debugger { namespace Internal { -RemoteGdbProcess::RemoteGdbProcess(const Utils::SshConnectionParameters &connParams, +RemoteGdbProcess::RemoteGdbProcess(const QSsh::SshConnectionParameters &connParams, RemotePlainGdbAdapter *adapter, QObject *parent) : AbstractGdbProcess(parent), m_connParams(connParams), m_state(Inactive), m_adapter(adapter) @@ -92,7 +93,7 @@ void RemoteGdbProcess::realStart(const QString &cmd, const QStringList &args, m_errorOutput.clear(); m_inputToSend.clear(); m_conn = SshConnectionManager::instance().acquireConnection(m_connParams); - connect(m_conn.data(), SIGNAL(error(Utils::SshError)), this, + connect(m_conn.data(), SIGNAL(error(QSsh::SshError)), this, SLOT(handleConnectionError())); if (m_conn->state() == SshConnection::Connected) { handleConnected(); @@ -130,7 +131,7 @@ void RemoteGdbProcess::handleFifoCreationFinished(int exitStatus) return; QTC_ASSERT(m_state == CreatingFifo, return); - if (exitStatus != Utils::SshRemoteProcess::ExitedNormally) { + if (exitStatus != QSsh::SshRemoteProcess::ExitedNormally) { emitErrorExit(tr("Could not create FIFO.")); } else { setState(StartingFifoReader); @@ -172,7 +173,7 @@ void RemoteGdbProcess::handleAppOutputReaderStarted() void RemoteGdbProcess::handleAppOutputReaderFinished(int exitStatus) { - if (exitStatus != Utils::SshRemoteProcess::ExitedNormally) + if (exitStatus != QSsh::SshRemoteProcess::ExitedNormally) emitErrorExit(tr("Application output reader unexpectedly finished.")); } @@ -192,15 +193,15 @@ void RemoteGdbProcess::handleGdbFinished(int exitStatus) QTC_ASSERT(m_state == RunningGdb, return); switch (exitStatus) { - case Utils::SshRemoteProcess::FailedToStart: + case QSsh::SshRemoteProcess::FailedToStart: m_error = tr("Remote GDB failed to start."); setState(Inactive); emit startFailed(); break; - case Utils::SshRemoteProcess::KilledBySignal: + case QSsh::SshRemoteProcess::KilledBySignal: emitErrorExit(tr("Remote GDB crashed.")); break; - case Utils::SshRemoteProcess::ExitedNormally: + case QSsh::SshRemoteProcess::ExitedNormally: const int exitCode = m_gdbProc->exitCode(); setState(Inactive); emit finished(exitCode, QProcess::NormalExit); @@ -229,7 +230,7 @@ qint64 RemoteGdbProcess::write(const QByteArray &data) void RemoteGdbProcess::kill() { if (m_state == RunningGdb) { - Utils::SshRemoteProcess::Ptr killProc + QSsh::SshRemoteProcess::Ptr killProc = m_conn->createRemoteProcess("pkill -SIGKILL -x gdb"); killProc->start(); } else { @@ -246,7 +247,7 @@ void RemoteGdbProcess::interruptInferior() { QTC_ASSERT(m_state == RunningGdb, return); - Utils::SshRemoteProcess::Ptr intProc + QSsh::SshRemoteProcess::Ptr intProc = m_conn->createRemoteProcess("pkill -x -SIGINT gdb"); intProc->start(); } @@ -386,15 +387,15 @@ void RemoteGdbProcess::setState(State newState) if (m_state == Inactive) { if (m_gdbProc) { disconnect(m_gdbProc.data(), 0, this, 0); - m_gdbProc = Utils::SshRemoteProcess::Ptr(); + m_gdbProc = QSsh::SshRemoteProcess::Ptr(); } if (m_appOutputReader) { disconnect(m_appOutputReader.data(), 0, this, 0); - m_appOutputReader = Utils::SshRemoteProcess::Ptr(); + m_appOutputReader = QSsh::SshRemoteProcess::Ptr(); } if (m_fifoCreator) { disconnect(m_fifoCreator.data(), 0, this, 0); - m_fifoCreator = Utils::SshRemoteProcess::Ptr(); + m_fifoCreator = QSsh::SshRemoteProcess::Ptr(); } disconnect(m_conn.data(), 0, this, 0); SshConnectionManager::instance().releaseConnection(m_conn); diff --git a/src/plugins/debugger/gdb/remotegdbprocess.h b/src/plugins/debugger/gdb/remotegdbprocess.h index b9888a5441..8dba88e191 100644 --- a/src/plugins/debugger/gdb/remotegdbprocess.h +++ b/src/plugins/debugger/gdb/remotegdbprocess.h @@ -35,8 +35,8 @@ #include "abstractgdbprocess.h" -#include -#include +#include +#include #include #include @@ -50,7 +50,7 @@ class RemoteGdbProcess : public AbstractGdbProcess { Q_OBJECT public: - RemoteGdbProcess(const Utils::SshConnectionParameters &server, + RemoteGdbProcess(const QSsh::SshConnectionParameters &server, RemotePlainGdbAdapter *adapter, QObject *parent = 0); virtual QByteArray readAllStandardOutput(); @@ -106,11 +106,11 @@ private: void emitErrorExit(const QString &error); void setState(State newState); - Utils::SshConnectionParameters m_connParams; - Utils::SshConnection::Ptr m_conn; - Utils::SshRemoteProcess::Ptr m_gdbProc; - Utils::SshRemoteProcess::Ptr m_appOutputReader; - Utils::SshRemoteProcess::Ptr m_fifoCreator; + QSsh::SshConnectionParameters m_connParams; + QSsh::SshConnection::Ptr m_conn; + QSsh::SshRemoteProcess::Ptr m_gdbProc; + QSsh::SshRemoteProcess::Ptr m_appOutputReader; + QSsh::SshRemoteProcess::Ptr m_fifoCreator; QByteArray m_gdbOutput; QByteArray m_errorOutput; QString m_command; diff --git a/src/plugins/debugger/lldb/lldbenginehost.cpp b/src/plugins/debugger/lldb/lldbenginehost.cpp index 17d4b9be51..9e36567ee8 100644 --- a/src/plugins/debugger/lldb/lldbenginehost.cpp +++ b/src/plugins/debugger/lldb/lldbenginehost.cpp @@ -64,7 +64,7 @@ namespace Debugger { namespace Internal { -SshIODevice::SshIODevice(Utils::SshRemoteProcessRunner *r) +SshIODevice::SshIODevice(QSsh::SshRemoteProcessRunner *r) : runner(r) , buckethead(0) { @@ -150,9 +150,9 @@ LldbEngineHost::LldbEngineHost(const DebuggerStartParameters &startParameters) if (startParameters.startMode == StartRemoteEngine) { m_guestProcess = 0; - Utils::SshRemoteProcessRunner * const runner = new Utils::SshRemoteProcessRunner; - connect (runner, SIGNAL(connectionError(Utils::SshError)), - this, SLOT(sshConnectionError(Utils::SshError))); + QSsh::SshRemoteProcessRunner * const runner = new QSsh::SshRemoteProcessRunner; + connect (runner, SIGNAL(connectionError(QSsh::SshError)), + this, SLOT(sshConnectionError(QSsh::SshError))); runner->run(startParameters.serverStartScript.toUtf8(), startParameters.connParams); setGuestDevice(new SshIODevice(runner)); } else { @@ -199,7 +199,7 @@ LldbEngineHost::~LldbEngineHost() if (m_ssh && m_ssh->isProcessRunning()) { // TODO: openssh doesn't do that - m_ssh->sendSignalToProcess(Utils::SshRemoteProcess::KillSignal); + m_ssh->sendSignalToProcess(QSsh::SshRemoteProcess::KillSignal); } } @@ -212,7 +212,7 @@ void LldbEngineHost::nuke() m_guestProcess->kill(); notifyEngineSpontaneousShutdown(); } -void LldbEngineHost::sshConnectionError(Utils::SshError e) +void LldbEngineHost::sshConnectionError(QSsh::SshError e) { showStatusMessage(tr("SSH connection error: %1").arg(e)); } diff --git a/src/plugins/debugger/lldb/lldbenginehost.h b/src/plugins/debugger/lldb/lldbenginehost.h index 2978033849..5b301b3962 100644 --- a/src/plugins/debugger/lldb/lldbenginehost.h +++ b/src/plugins/debugger/lldb/lldbenginehost.h @@ -34,10 +34,10 @@ #define DEBUGGER_LLDBENGINE_HOST_H #include "ipcenginehost.h" -#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -49,7 +49,7 @@ class SshIODevice : public QIODevice { Q_OBJECT public: - SshIODevice(Utils::SshRemoteProcessRunner *r); + SshIODevice(QSsh::SshRemoteProcessRunner *r); ~SshIODevice(); virtual qint64 bytesAvailable () const; virtual qint64 writeData (const char * data, qint64 maxSize); @@ -59,8 +59,8 @@ private slots: void outputAvailable(const QByteArray &output); void errorOutputAvailable(const QByteArray &output); private: - Utils::SshRemoteProcessRunner *runner; - Utils::SshRemoteProcess::Ptr proc; + QSsh::SshRemoteProcessRunner *runner; + QSsh::SshRemoteProcess::Ptr proc; int buckethead; QQueue buckets; QByteArray startupbuffer; @@ -76,11 +76,11 @@ public: private: QProcess *m_guestProcess; - Utils::SshRemoteProcessRunner *m_ssh; + QSsh::SshRemoteProcessRunner *m_ssh; protected: void nuke(); private slots: - void sshConnectionError(Utils::SshError); + void sshConnectionError(QSsh::SshError); void finished(int, QProcess::ExitStatus); void stderrReady(); }; diff --git a/src/plugins/git/git.qbs b/src/plugins/git/git.qbs index 8f61ffb9b6..3b05df1a78 100644 --- a/src/plugins/git/git.qbs +++ b/src/plugins/git/git.qbs @@ -5,7 +5,7 @@ import "../QtcPlugin.qbs" as QtcPlugin QtcPlugin { name: "Git" - Depends { name: "qt"; submodules: ['widgets'] } + Depends { name: "qt"; submodules: ['widgets', 'network'] } Depends { name: "Core" } Depends { name: "TextEditor" } Depends { name: "Find" } diff --git a/src/plugins/madde/maddedevicetester.cpp b/src/plugins/madde/maddedevicetester.cpp index 3fa4b96189..7a8ba98549 100644 --- a/src/plugins/madde/maddedevicetester.cpp +++ b/src/plugins/madde/maddedevicetester.cpp @@ -36,12 +36,12 @@ #include #include -#include +#include #include using namespace RemoteLinux; -using namespace Utils; +using namespace QSsh; namespace Madde { namespace Internal { diff --git a/src/plugins/madde/maddedevicetester.h b/src/plugins/madde/maddedevicetester.h index 2dc176ae36..46248f2c64 100644 --- a/src/plugins/madde/maddedevicetester.h +++ b/src/plugins/madde/maddedevicetester.h @@ -36,7 +36,7 @@ #include -namespace Utils { +namespace QSsh { class SshRemoteProcessRunner; } @@ -73,7 +73,7 @@ private: RemoteLinux::GenericLinuxDeviceTester * const m_genericTester; State m_state; TestResult m_result; - Utils::SshRemoteProcessRunner *m_processRunner; + QSsh::SshRemoteProcessRunner *m_processRunner; QSharedPointer m_deviceConfiguration; QByteArray m_stdout; QByteArray m_stderr; diff --git a/src/plugins/madde/maddeuploadandinstallpackagesteps.cpp b/src/plugins/madde/maddeuploadandinstallpackagesteps.cpp index d1b224d4a5..348fb58f31 100644 --- a/src/plugins/madde/maddeuploadandinstallpackagesteps.cpp +++ b/src/plugins/madde/maddeuploadandinstallpackagesteps.cpp @@ -42,7 +42,7 @@ #include #include #include -#include +#include using namespace RemoteLinux; diff --git a/src/plugins/madde/maemodeploybymountsteps.cpp b/src/plugins/madde/maemodeploybymountsteps.cpp index 3407aae74a..35469614ad 100644 --- a/src/plugins/madde/maemodeploybymountsteps.cpp +++ b/src/plugins/madde/maemodeploybymountsteps.cpp @@ -48,7 +48,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/plugins/madde/maemodeploymentmounter.cpp b/src/plugins/madde/maemodeploymentmounter.cpp index 5a43ec4c8d..94a42e14b2 100644 --- a/src/plugins/madde/maemodeploymentmounter.cpp +++ b/src/plugins/madde/maemodeploymentmounter.cpp @@ -39,11 +39,11 @@ #include #include #include -#include +#include using namespace Qt4ProjectManager; using namespace RemoteLinux; -using namespace Utils; +using namespace QSsh; namespace Madde { namespace Internal { @@ -82,7 +82,7 @@ void MaemoDeploymentMounter::setupMounts(const SshConnection::Ptr &connection, m_devConf = devConf; m_mounter->setConnection(m_connection, m_devConf); m_buildConfig = bc; - connect(m_connection.data(), SIGNAL(error(Utils::SshError)), + connect(m_connection.data(), SIGNAL(error(QSsh::SshError)), SLOT(handleConnectionError())); setState(UnmountingOldDirs); unmount(); diff --git a/src/plugins/madde/maemodeploymentmounter.h b/src/plugins/madde/maemodeploymentmounter.h index 8e58628ffa..57e9320801 100644 --- a/src/plugins/madde/maemodeploymentmounter.h +++ b/src/plugins/madde/maemodeploymentmounter.h @@ -41,7 +41,7 @@ #include #include -namespace Utils { class SshConnection; } +namespace QSsh { class SshConnection; } namespace Qt4ProjectManager { class Qt4BuildConfiguration; } namespace RemoteLinux { @@ -61,7 +61,7 @@ public: ~MaemoDeploymentMounter(); // Connection must be in connected state. - void setupMounts(const QSharedPointer &connection, + void setupMounts(const QSharedPointer &connection, const QSharedPointer &devConf, const QList &mountSpecs, const Qt4ProjectManager::Qt4BuildConfiguration *bc); @@ -93,7 +93,7 @@ private: void setState(State newState); State m_state; - QSharedPointer m_connection; + QSharedPointer m_connection; QSharedPointer m_devConf; MaemoRemoteMounter * const m_mounter; RemoteLinux::RemoteLinuxUsedPortsGatherer * const m_portsGatherer; diff --git a/src/plugins/madde/maemodeviceconfigwizard.cpp b/src/plugins/madde/maemodeviceconfigwizard.cpp index 0db737173e..40628569eb 100644 --- a/src/plugins/madde/maemodeviceconfigwizard.cpp +++ b/src/plugins/madde/maemodeviceconfigwizard.cpp @@ -45,7 +45,7 @@ #include #include #include -#include +#include #include #include @@ -56,6 +56,7 @@ using namespace ProjectExplorer; using namespace RemoteLinux; +using namespace QSsh; using namespace Utils; namespace Madde { @@ -443,7 +444,7 @@ private: Q_SLOT void deployKey() { - using namespace Utils; + using namespace QSsh; m_ui->deviceAddressLineEdit->setEnabled(false); m_ui->passwordLineEdit->setEnabled(false); m_ui->deployButton->setEnabled(false); @@ -561,19 +562,19 @@ IDevice::Ptr MaemoDeviceConfigWizard::device() { bool doTest; QString freePortsSpec; - Utils::SshConnectionParameters sshParams; + QSsh::SshConnectionParameters sshParams; sshParams.userName = defaultUser(d->wizardData.deviceType); sshParams.host = d->wizardData.hostName; sshParams.port = d->wizardData.sshPort; if (d->wizardData.machineType == LinuxDeviceConfiguration::Emulator) { - sshParams.authenticationType = Utils::SshConnectionParameters::AuthenticationByPassword; + sshParams.authenticationType = QSsh::SshConnectionParameters::AuthenticationByPassword; sshParams.password = d->wizardData.deviceType == Core::Id(MeeGoOsType) ? QLatin1String("meego") : QString(); sshParams.timeout = 30; freePortsSpec = QLatin1String("13219,14168"); doTest = false; } else { - sshParams.authenticationType = Utils::SshConnectionParameters::AuthenticationByKey; + sshParams.authenticationType = QSsh::SshConnectionParameters::AuthenticationByKey; sshParams.privateKeyFile = d->wizardData.privateKeyFilePath; sshParams.timeout = 10; freePortsSpec = QLatin1String("10000-10100"); diff --git a/src/plugins/madde/maemopublisherfremantlefree.cpp b/src/plugins/madde/maemopublisherfremantlefree.cpp index 896a138513..30c5679cef 100644 --- a/src/plugins/madde/maemopublisherfremantlefree.cpp +++ b/src/plugins/madde/maemopublisherfremantlefree.cpp @@ -47,7 +47,7 @@ #include #include #include -#include +#include #include #include @@ -59,7 +59,7 @@ using namespace Core; using namespace Qt4ProjectManager; using namespace RemoteLinux; -using namespace Utils; +using namespace QSsh; namespace Madde { namespace Internal { diff --git a/src/plugins/madde/maemopublisherfremantlefree.h b/src/plugins/madde/maemopublisherfremantlefree.h index 298612fba5..39a692e964 100644 --- a/src/plugins/madde/maemopublisherfremantlefree.h +++ b/src/plugins/madde/maemopublisherfremantlefree.h @@ -32,7 +32,7 @@ #ifndef MAEMOPUBLISHERFREMANTLEFREE_H #define MAEMOPUBLISHERFREMANTLEFREE_H -#include +#include #include #include @@ -45,7 +45,7 @@ namespace Qt4ProjectManager { class Qt4BuildConfiguration; } -namespace Utils { +namespace QSsh { class SshRemoteProcessRunner; } @@ -118,9 +118,9 @@ private: State m_state; QString m_tmpProjectDir; QProcess *m_process; - Utils::SshConnectionParameters m_sshParams; + QSsh::SshConnectionParameters m_sshParams; QString m_remoteDir; - Utils::SshRemoteProcessRunner *m_uploader; + QSsh::SshRemoteProcessRunner *m_uploader; QByteArray m_scpOutput; QList m_filesToUpload; QString m_resultString; diff --git a/src/plugins/madde/maemoqemumanager.h b/src/plugins/madde/maemoqemumanager.h index a5c61b7899..1c6c635a79 100644 --- a/src/plugins/madde/maemoqemumanager.h +++ b/src/plugins/madde/maemoqemumanager.h @@ -45,10 +45,6 @@ QT_FORWARD_DECLARE_CLASS(QAction) QT_FORWARD_DECLARE_CLASS(QStringList) -namespace Utils { -class FileSystemWatcher; -} - namespace ProjectExplorer { class BuildConfiguration; class Project; @@ -56,13 +52,9 @@ class RunConfiguration; class Target; } -namespace QtSupport { -class BaseQtVersion; -} - -namespace RemoteLinux { -class RemoteLinuxRunConfiguration; -} +namespace QtSupport { class BaseQtVersion; } +namespace RemoteLinux { class RemoteLinuxRunConfiguration; } +namespace Utils { class FileSystemWatcher; } namespace Madde { namespace Internal { diff --git a/src/plugins/madde/maemoremotecopyfacility.cpp b/src/plugins/madde/maemoremotecopyfacility.cpp index 574a2efcba..3a267e591c 100644 --- a/src/plugins/madde/maemoremotecopyfacility.cpp +++ b/src/plugins/madde/maemoremotecopyfacility.cpp @@ -34,13 +34,13 @@ #include "maemoglobal.h" #include -#include -#include +#include +#include #include using namespace RemoteLinux; -using namespace Utils; +using namespace QSsh; namespace Madde { namespace Internal { diff --git a/src/plugins/madde/maemoremotecopyfacility.h b/src/plugins/madde/maemoremotecopyfacility.h index 2a19950e5d..b0b4c51fab 100644 --- a/src/plugins/madde/maemoremotecopyfacility.h +++ b/src/plugins/madde/maemoremotecopyfacility.h @@ -40,7 +40,7 @@ #include #include -namespace Utils { +namespace QSsh { class SshConnection; class SshRemoteProcessRunner; } @@ -59,7 +59,7 @@ public: explicit MaemoRemoteCopyFacility(QObject *parent = 0); ~MaemoRemoteCopyFacility(); - void copyFiles(const QSharedPointer &connection, + void copyFiles(const QSharedPointer &connection, const QSharedPointer &devConf, const QList &deployables, const QString &mountPoint); void cancel(); @@ -81,8 +81,8 @@ private: void copyNextFile(); void setFinished(); - Utils::SshRemoteProcessRunner *m_copyRunner; - Utils::SshRemoteProcessRunner *m_killProcess; + QSsh::SshRemoteProcessRunner *m_copyRunner; + QSsh::SshRemoteProcessRunner *m_killProcess; QSharedPointer m_devConf; QList m_deployables; QString m_mountPoint; diff --git a/src/plugins/madde/maemoremotemounter.cpp b/src/plugins/madde/maemoremotemounter.cpp index c8de89c90a..fec15a2e8a 100644 --- a/src/plugins/madde/maemoremotemounter.cpp +++ b/src/plugins/madde/maemoremotemounter.cpp @@ -35,8 +35,8 @@ #include "maemoglobal.h" #include "qt4maemotarget.h" -#include -#include +#include +#include #include #include #include @@ -47,6 +47,7 @@ using namespace Qt4ProjectManager; using namespace RemoteLinux; +using namespace QSsh; using namespace Utils; namespace Madde { diff --git a/src/plugins/madde/maemoremotemounter.h b/src/plugins/madde/maemoremotemounter.h index 8c4fa5c00d..5644d40712 100644 --- a/src/plugins/madde/maemoremotemounter.h +++ b/src/plugins/madde/maemoremotemounter.h @@ -43,13 +43,13 @@ QT_FORWARD_DECLARE_CLASS(QTimer) -namespace Utils { -class PortList; +namespace QSsh { class SftpChannel; class SshConnection; class SshRemoteProcess; } +namespace Utils { class PortList; } namespace Qt4ProjectManager { class Qt4BuildConfiguration; } namespace RemoteLinux { @@ -68,7 +68,7 @@ public: ~MaemoRemoteMounter(); // Must already be connected. - void setConnection(const QSharedPointer &connection, + void setConnection(const QSharedPointer &connection, const QSharedPointer &devConf); void setBuildConfiguration(const Qt4ProjectManager::Qt4BuildConfiguration *bc); @@ -123,11 +123,11 @@ private: int remotePort; }; - QSharedPointer m_connection; + QSharedPointer m_connection; QSharedPointer m_devConf; QList m_mountSpecs; - QSharedPointer m_mountProcess; - QSharedPointer m_unmountProcess; + QSharedPointer m_mountProcess; + QSharedPointer m_unmountProcess; typedef QSharedPointer ProcPtr; QList m_utfsServers; diff --git a/src/plugins/madde/maemorunconfiguration.cpp b/src/plugins/madde/maemorunconfiguration.cpp index c71bb6ba28..d548b49735 100644 --- a/src/plugins/madde/maemorunconfiguration.cpp +++ b/src/plugins/madde/maemorunconfiguration.cpp @@ -42,7 +42,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/plugins/madde/maemosshrunner.cpp b/src/plugins/madde/maemosshrunner.cpp index 219b93515a..5d29a169fb 100644 --- a/src/plugins/madde/maemosshrunner.cpp +++ b/src/plugins/madde/maemosshrunner.cpp @@ -38,11 +38,11 @@ #include #include #include -#include +#include using namespace Qt4ProjectManager; using namespace RemoteLinux; -using namespace Utils; +using namespace QSsh; namespace Madde { namespace Internal { diff --git a/src/plugins/remotelinux/abstractremotelinuxdeployservice.cpp b/src/plugins/remotelinux/abstractremotelinuxdeployservice.cpp index 33433d61f8..70d45ff453 100644 --- a/src/plugins/remotelinux/abstractremotelinuxdeployservice.cpp +++ b/src/plugins/remotelinux/abstractremotelinuxdeployservice.cpp @@ -36,8 +36,8 @@ #include #include -#include -#include +#include +#include #include #include @@ -45,7 +45,7 @@ #include using namespace Qt4ProjectManager; -using namespace Utils; +using namespace QSsh; namespace RemoteLinux { namespace Internal { @@ -263,7 +263,7 @@ void AbstractRemoteLinuxDeployService::handleDeviceSetupDone(bool success) d->state = Connecting; d->connection = SshConnectionManager::instance().acquireConnection(d->deviceConfiguration->sshParameters()); - connect(d->connection.data(), SIGNAL(error(Utils::SshError)), + connect(d->connection.data(), SIGNAL(error(QSsh::SshError)), SLOT(handleConnectionFailure())); if (d->connection->state() == SshConnection::Connected) { handleConnected(); diff --git a/src/plugins/remotelinux/abstractremotelinuxdeployservice.h b/src/plugins/remotelinux/abstractremotelinuxdeployservice.h index e0f4fb5d3f..8ea3120c8b 100644 --- a/src/plugins/remotelinux/abstractremotelinuxdeployservice.h +++ b/src/plugins/remotelinux/abstractremotelinuxdeployservice.h @@ -39,7 +39,7 @@ #include QT_FORWARD_DECLARE_CLASS(QString) -namespace Utils { class SshConnection; } +namespace QSsh { class SshConnection; } namespace Qt4ProjectManager { class Qt4BuildConfiguration; @@ -82,7 +82,7 @@ signals: protected: const Qt4ProjectManager::Qt4BuildConfiguration *qt4BuildConfiguration() const; QSharedPointer deviceConfiguration() const; - QSharedPointer connection() const; + QSharedPointer connection() const; void saveDeploymentTimeStamp(const DeployableFile &deployableFile); bool hasChangedSinceLastDeployment(const DeployableFile &deployableFile) const; diff --git a/src/plugins/remotelinux/abstractuploadandinstallpackageservice.cpp b/src/plugins/remotelinux/abstractuploadandinstallpackageservice.cpp index cc70ffb79b..a7fc3dc9b4 100644 --- a/src/plugins/remotelinux/abstractuploadandinstallpackageservice.cpp +++ b/src/plugins/remotelinux/abstractuploadandinstallpackageservice.cpp @@ -37,7 +37,7 @@ #include "remotelinuxpackageinstaller.h" #include -#include +#include #include #include diff --git a/src/plugins/remotelinux/genericdirectuploadservice.cpp b/src/plugins/remotelinux/genericdirectuploadservice.cpp index 72a0e6aec0..74ada44f94 100644 --- a/src/plugins/remotelinux/genericdirectuploadservice.cpp +++ b/src/plugins/remotelinux/genericdirectuploadservice.cpp @@ -34,16 +34,16 @@ #include "deployablefile.h" #include -#include -#include -#include +#include +#include +#include #include #include #include #include -using namespace Utils; +using namespace QSsh; namespace RemoteLinux { namespace Internal { @@ -131,8 +131,8 @@ void GenericDirectUploadService::handleSftpInitialized() } Q_ASSERT(!d->filesToUpload.isEmpty()); - connect(d->uploader.data(), SIGNAL(finished(Utils::SftpJobId,QString)), - SLOT(handleUploadFinished(Utils::SftpJobId,QString))); + connect(d->uploader.data(), SIGNAL(finished(QSsh::SftpJobId,QString)), + SLOT(handleUploadFinished(QSsh::SftpJobId,QString))); d->state = Uploading; uploadNextFile(); } @@ -146,7 +146,7 @@ void GenericDirectUploadService::handleSftpInitializationFailed(const QString &m handleDeploymentDone(); } -void GenericDirectUploadService::handleUploadFinished(Utils::SftpJobId jobId, const QString &errorMsg) +void GenericDirectUploadService::handleUploadFinished(QSsh::SftpJobId jobId, const QString &errorMsg) { Q_UNUSED(jobId); diff --git a/src/plugins/remotelinux/genericdirectuploadservice.h b/src/plugins/remotelinux/genericdirectuploadservice.h index 320afb6a45..1f697ca98a 100644 --- a/src/plugins/remotelinux/genericdirectuploadservice.h +++ b/src/plugins/remotelinux/genericdirectuploadservice.h @@ -35,7 +35,7 @@ #include "abstractremotelinuxdeployservice.h" #include "remotelinux_export.h" -#include +#include #include @@ -66,7 +66,7 @@ public: private slots: void handleSftpInitialized(); void handleSftpInitializationFailed(const QString &errorMessage); - void handleUploadFinished(Utils::SftpJobId jobId, const QString &errorMsg); + void handleUploadFinished(QSsh::SftpJobId jobId, const QString &errorMsg); void handleMkdirFinished(int exitStatus); void handleLnFinished(int exitStatus); void handleStdOutData(); diff --git a/src/plugins/remotelinux/genericlinuxdeviceconfigurationwidget.cpp b/src/plugins/remotelinux/genericlinuxdeviceconfigurationwidget.cpp index 3a270c7490..f7e4850d15 100644 --- a/src/plugins/remotelinux/genericlinuxdeviceconfigurationwidget.cpp +++ b/src/plugins/remotelinux/genericlinuxdeviceconfigurationwidget.cpp @@ -33,12 +33,13 @@ #include "ui_genericlinuxdeviceconfigurationwidget.h" #include -#include -#include +#include +#include #include using namespace RemoteLinux; +using namespace QSsh; using namespace Utils; GenericLinuxDeviceConfigurationWidget::GenericLinuxDeviceConfigurationWidget( diff --git a/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizard.cpp b/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizard.cpp index 6d8be5cd03..764a4d5761 100644 --- a/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizard.cpp +++ b/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizard.cpp @@ -38,7 +38,7 @@ #include using namespace ProjectExplorer; -using namespace Utils; +using namespace QSsh; namespace RemoteLinux { namespace Internal { @@ -76,7 +76,7 @@ GenericLinuxDeviceConfigurationWizard::~GenericLinuxDeviceConfigurationWizard() IDevice::Ptr GenericLinuxDeviceConfigurationWizard::device() { - Utils::SshConnectionParameters sshParams; + QSsh::SshConnectionParameters sshParams; sshParams.host = d->setupPage.hostName(); sshParams.userName = d->setupPage.userName(); sshParams.port = 22; diff --git a/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizardpages.cpp b/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizardpages.cpp index b4ba390c6d..e7ea64e155 100644 --- a/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizardpages.cpp +++ b/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizardpages.cpp @@ -50,6 +50,7 @@ public: } // namespace Internal +using namespace QSsh; using namespace Utils; GenericLinuxDeviceConfigurationWizardSetupPage::GenericLinuxDeviceConfigurationWizardSetupPage(QWidget *parent) : diff --git a/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizardpages.h b/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizardpages.h index e5b22c5feb..71930503c3 100644 --- a/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizardpages.h +++ b/src/plugins/remotelinux/genericlinuxdeviceconfigurationwizardpages.h @@ -33,7 +33,7 @@ #include "remotelinux_export.h" -#include +#include #include @@ -57,7 +57,7 @@ public: QString configurationName() const; QString hostName() const; QString userName() const; - Utils::SshConnectionParameters::AuthenticationType authenticationType() const; + QSsh::SshConnectionParameters::AuthenticationType authenticationType() const; QString password() const; QString privateKeyFilePath() const; diff --git a/src/plugins/remotelinux/linuxdeviceconfiguration.cpp b/src/plugins/remotelinux/linuxdeviceconfiguration.cpp index a478926755..d05fc3a23a 100644 --- a/src/plugins/remotelinux/linuxdeviceconfiguration.cpp +++ b/src/plugins/remotelinux/linuxdeviceconfiguration.cpp @@ -39,13 +39,15 @@ #include "remotelinux_constants.h" #include -#include +#include #include #include #include +using namespace QSsh; using namespace Utils; + typedef SshConnectionParameters::AuthenticationType AuthType; namespace RemoteLinux { diff --git a/src/plugins/remotelinux/linuxdeviceconfiguration.h b/src/plugins/remotelinux/linuxdeviceconfiguration.h index 6fa22eb9be..ab9bcdceed 100644 --- a/src/plugins/remotelinux/linuxdeviceconfiguration.h +++ b/src/plugins/remotelinux/linuxdeviceconfiguration.h @@ -38,10 +38,8 @@ #include -namespace Utils { -class SshConnectionParameters; -class PortList; -} +namespace QSsh { class SshConnectionParameters; } +namespace Utils { class PortList; } namespace RemoteLinux { namespace Internal { @@ -60,10 +58,10 @@ public: ~LinuxDeviceConfiguration(); Utils::PortList freePorts() const; - Utils::SshConnectionParameters sshParameters() const; + QSsh::SshConnectionParameters sshParameters() const; MachineType machineType() const; - void setSshParameters(const Utils::SshConnectionParameters &sshParameters); + void setSshParameters(const QSsh::SshConnectionParameters &sshParameters); void setFreePorts(const Utils::PortList &freePorts); static QString defaultPrivateKeyFilePath(); diff --git a/src/plugins/remotelinux/linuxdevicetester.cpp b/src/plugins/remotelinux/linuxdevicetester.cpp index e7b32afa51..6a9845e8dc 100644 --- a/src/plugins/remotelinux/linuxdevicetester.cpp +++ b/src/plugins/remotelinux/linuxdevicetester.cpp @@ -35,10 +35,10 @@ #include "remotelinuxusedportsgatherer.h" #include -#include -#include +#include +#include -using namespace Utils; +using namespace QSsh; namespace RemoteLinux { namespace Internal { @@ -86,7 +86,7 @@ void GenericLinuxDeviceTester::testDevice(const LinuxDeviceConfiguration::ConstP d->deviceConfiguration = deviceConfiguration; d->connection = SshConnection::create(deviceConfiguration->sshParameters()); connect(d->connection.data(), SIGNAL(connected()), SLOT(handleConnected())); - connect(d->connection.data(), SIGNAL(error(Utils::SshError)), + connect(d->connection.data(), SIGNAL(error(QSsh::SshError)), SLOT(handleConnectionFailure())); emit progressMessage(tr("Connecting to host...")); diff --git a/src/plugins/remotelinux/linuxdevicetester.h b/src/plugins/remotelinux/linuxdevicetester.h index 7e2ef787ab..58a3f10ed7 100644 --- a/src/plugins/remotelinux/linuxdevicetester.h +++ b/src/plugins/remotelinux/linuxdevicetester.h @@ -39,7 +39,7 @@ QT_FORWARD_DECLARE_CLASS(QString) -namespace Utils { +namespace QSsh { class SshConnection; } @@ -80,7 +80,7 @@ public: void testDevice(const QSharedPointer &deviceConfiguration); void stopTest(); - QSharedPointer connection() const; + QSharedPointer connection() const; private slots: void handleConnected(); diff --git a/src/plugins/remotelinux/packageuploader.cpp b/src/plugins/remotelinux/packageuploader.cpp index 4e17ad1104..fbdb058def 100644 --- a/src/plugins/remotelinux/packageuploader.cpp +++ b/src/plugins/remotelinux/packageuploader.cpp @@ -33,10 +33,10 @@ #include "packageuploader.h" #include -#include -#include +#include +#include -using namespace Utils; +using namespace QSsh; namespace RemoteLinux { namespace Internal { @@ -61,15 +61,15 @@ void PackageUploader::uploadPackage(const SshConnection::Ptr &connection, m_localFilePath = localFilePath; m_remoteFilePath = remoteFilePath; m_connection = connection; - connect(m_connection.data(), SIGNAL(error(Utils::SshError)), + connect(m_connection.data(), SIGNAL(error(QSsh::SshError)), SLOT(handleConnectionFailure())); m_uploader = m_connection->createSftpChannel(); connect(m_uploader.data(), SIGNAL(initialized()), this, SLOT(handleSftpChannelInitialized())); connect(m_uploader.data(), SIGNAL(initializationFailed(QString)), this, SLOT(handleSftpChannelInitializationFailed(QString))); - connect(m_uploader.data(), SIGNAL(finished(Utils::SftpJobId,QString)), - this, SLOT(handleSftpJobFinished(Utils::SftpJobId,QString))); + connect(m_uploader.data(), SIGNAL(finished(QSsh::SftpJobId,QString)), + this, SLOT(handleSftpJobFinished(QSsh::SftpJobId,QString))); m_uploader->initialize(); } diff --git a/src/plugins/remotelinux/packageuploader.h b/src/plugins/remotelinux/packageuploader.h index 3c671257bd..d29adf80b5 100644 --- a/src/plugins/remotelinux/packageuploader.h +++ b/src/plugins/remotelinux/packageuploader.h @@ -37,9 +37,9 @@ #include #include -#include +#include -namespace Utils { +namespace QSsh { class SftpChannel; class SshConnection; } @@ -55,7 +55,7 @@ public: ~PackageUploader(); // Connection has to be established already. - void uploadPackage(const QSharedPointer &connection, + void uploadPackage(const QSharedPointer &connection, const QString &localFilePath, const QString &remoteFilePath); void cancelUpload(); @@ -67,7 +67,7 @@ private slots: void handleConnectionFailure(); void handleSftpChannelInitialized(); void handleSftpChannelInitializationFailed(const QString &error); - void handleSftpJobFinished(Utils::SftpJobId job, const QString &error); + void handleSftpJobFinished(QSsh::SftpJobId job, const QString &error); private: enum State { InitializingSftp, Uploading, Inactive }; @@ -76,8 +76,8 @@ private: void setState(State newState); State m_state; - QSharedPointer m_connection; - QSharedPointer m_uploader; + QSharedPointer m_connection; + QSharedPointer m_uploader; QString m_localFilePath; QString m_remoteFilePath; }; diff --git a/src/plugins/remotelinux/publickeydeploymentdialog.cpp b/src/plugins/remotelinux/publickeydeploymentdialog.cpp index 605e6a49a4..6b10673884 100644 --- a/src/plugins/remotelinux/publickeydeploymentdialog.cpp +++ b/src/plugins/remotelinux/publickeydeploymentdialog.cpp @@ -34,7 +34,7 @@ #include "sshkeydeployer.h" #include -#include +#include #include #include diff --git a/src/plugins/remotelinux/remotelinux.qbs b/src/plugins/remotelinux/remotelinux.qbs index e3ae54956c..84e1151e0e 100644 --- a/src/plugins/remotelinux/remotelinux.qbs +++ b/src/plugins/remotelinux/remotelinux.qbs @@ -11,6 +11,7 @@ QtcPlugin { Depends { name: "ProjectExplorer" } Depends { name: "Qt4ProjectManager" } Depends { name: "QtSupport" } + Depends { name: "QtcSsh" } Depends { name: "cpp" } cpp.includePaths: [ @@ -133,6 +134,7 @@ QtcPlugin { ProductModule { Depends { name: "Core" } + Depends { name: "QtcSsh" } } } diff --git a/src/plugins/remotelinux/remotelinuxapplicationrunner.cpp b/src/plugins/remotelinux/remotelinuxapplicationrunner.cpp index 1485db51c5..95101ec82a 100644 --- a/src/plugins/remotelinux/remotelinuxapplicationrunner.cpp +++ b/src/plugins/remotelinux/remotelinuxapplicationrunner.cpp @@ -37,13 +37,14 @@ #include #include -#include -#include -#include +#include +#include +#include #include using namespace Qt4ProjectManager; +using namespace QSsh; using namespace Utils; namespace RemoteLinux { @@ -79,9 +80,9 @@ public: const QString commandPrefix; const PortList initialFreePorts; - Utils::SshConnection::Ptr connection; - Utils::SshRemoteProcess::Ptr runner; - Utils::SshRemoteProcess::Ptr cleaner; + QSsh::SshConnection::Ptr connection; + QSsh::SshRemoteProcess::Ptr runner; + QSsh::SshRemoteProcess::Ptr cleaner; PortList freePorts; int exitStatus; @@ -400,13 +401,13 @@ void AbstractRemoteLinuxApplicationRunner::handleDeviceSetupDone(bool success) d->exitStatus = -1; d->freePorts = d->initialFreePorts; connect(d->connection.data(), SIGNAL(connected()), SLOT(handleConnected())); - connect(d->connection.data(), SIGNAL(error(Utils::SshError)), + connect(d->connection.data(), SIGNAL(error(QSsh::SshError)), SLOT(handleConnectionFailure())); if (d->connection->state() == SshConnection::Connected) { handleConnected(); } else { emit reportProgress(tr("Connecting to device...")); - if (d->connection->state() == Utils::SshConnection::Unconnected) + if (d->connection->state() == QSsh::SshConnection::Unconnected) d->connection->connectToHost(); } } diff --git a/src/plugins/remotelinux/remotelinuxapplicationrunner.h b/src/plugins/remotelinux/remotelinuxapplicationrunner.h index 92f2b053c0..0961b9ab1a 100644 --- a/src/plugins/remotelinux/remotelinuxapplicationrunner.h +++ b/src/plugins/remotelinux/remotelinuxapplicationrunner.h @@ -36,10 +36,8 @@ #include #include -namespace Utils { -class PortList; -class SshConnection; -} +namespace QSsh { class SshConnection; } +namespace Utils { class PortList; } namespace RemoteLinux { class LinuxDeviceConfiguration; @@ -64,7 +62,7 @@ public: void startExecution(const QByteArray &remoteCall); - QSharedPointer connection() const; + QSharedPointer connection() const; QSharedPointer devConfig() const; const RemoteLinuxUsedPortsGatherer *usedPortsGatherer() const; Utils::PortList *freePorts(); diff --git a/src/plugins/remotelinux/remotelinuxcustomcommanddeployservice.cpp b/src/plugins/remotelinux/remotelinuxcustomcommanddeployservice.cpp index 29653c84bd..11d2bc435b 100644 --- a/src/plugins/remotelinux/remotelinuxcustomcommanddeployservice.cpp +++ b/src/plugins/remotelinux/remotelinuxcustomcommanddeployservice.cpp @@ -34,11 +34,11 @@ #include "linuxdeviceconfiguration.h" #include -#include +#include #include -using namespace Utils; +using namespace QSsh; namespace RemoteLinux { namespace Internal { diff --git a/src/plugins/remotelinux/remotelinuxdebugsupport.cpp b/src/plugins/remotelinux/remotelinuxdebugsupport.cpp index 73344d11bc..176df268e5 100644 --- a/src/plugins/remotelinux/remotelinuxdebugsupport.cpp +++ b/src/plugins/remotelinux/remotelinuxdebugsupport.cpp @@ -47,7 +47,7 @@ #include #include -using namespace Utils; +using namespace QSsh; using namespace Debugger; using namespace ProjectExplorer; diff --git a/src/plugins/remotelinux/remotelinuxenvironmentreader.cpp b/src/plugins/remotelinux/remotelinuxenvironmentreader.cpp index 0703e97b0c..5fd0213aed 100644 --- a/src/plugins/remotelinux/remotelinuxenvironmentreader.cpp +++ b/src/plugins/remotelinux/remotelinuxenvironmentreader.cpp @@ -33,7 +33,7 @@ #include "linuxdeviceconfiguration.h" #include "remotelinuxrunconfiguration.h" -#include +#include namespace RemoteLinux { namespace Internal { @@ -60,7 +60,7 @@ void RemoteLinuxEnvironmentReader::start(const QString &environmentSetupCommand) return; m_stop = false; if (!m_remoteProcessRunner) - m_remoteProcessRunner = new Utils::SshRemoteProcessRunner(this); + m_remoteProcessRunner = new QSsh::SshRemoteProcessRunner(this); connect(m_remoteProcessRunner, SIGNAL(connectionError()), SLOT(handleConnectionFailure())); connect(m_remoteProcessRunner, SIGNAL(processClosed(int)), SLOT(remoteProcessFinished(int))); connect(m_remoteProcessRunner, SIGNAL(processOutputAvailable(QByteArray)), @@ -102,16 +102,16 @@ void RemoteLinuxEnvironmentReader::handleCurrentDeviceConfigChanged() void RemoteLinuxEnvironmentReader::remoteProcessFinished(int exitCode) { - Q_ASSERT(exitCode == Utils::SshRemoteProcess::FailedToStart - || exitCode == Utils::SshRemoteProcess::KilledBySignal - || exitCode == Utils::SshRemoteProcess::ExitedNormally); + Q_ASSERT(exitCode == QSsh::SshRemoteProcess::FailedToStart + || exitCode == QSsh::SshRemoteProcess::KilledBySignal + || exitCode == QSsh::SshRemoteProcess::ExitedNormally); if (m_stop) return; disconnect(m_remoteProcessRunner, 0, this, 0); m_env.clear(); - if (exitCode == Utils::SshRemoteProcess::ExitedNormally) { + if (exitCode == QSsh::SshRemoteProcess::ExitedNormally) { if (!m_remoteOutput.isEmpty()) { m_env = Utils::Environment(m_remoteOutput.split(QLatin1Char('\n'), QString::SkipEmptyParts)); diff --git a/src/plugins/remotelinux/remotelinuxenvironmentreader.h b/src/plugins/remotelinux/remotelinuxenvironmentreader.h index 5e80faf697..7f49be8edb 100644 --- a/src/plugins/remotelinux/remotelinuxenvironmentreader.h +++ b/src/plugins/remotelinux/remotelinuxenvironmentreader.h @@ -37,7 +37,7 @@ #include #include -namespace Utils { +namespace QSsh { class SshRemoteProcessRunner; } @@ -80,7 +80,7 @@ private: Utils::Environment m_env; QSharedPointer m_devConfig; RemoteLinuxRunConfiguration *m_runConfig; - Utils::SshRemoteProcessRunner *m_remoteProcessRunner; + QSsh::SshRemoteProcessRunner *m_remoteProcessRunner; }; } // namespace Internal diff --git a/src/plugins/remotelinux/remotelinuxpackageinstaller.cpp b/src/plugins/remotelinux/remotelinuxpackageinstaller.cpp index b2c7d20c3a..3a060d2185 100644 --- a/src/plugins/remotelinux/remotelinuxpackageinstaller.cpp +++ b/src/plugins/remotelinux/remotelinuxpackageinstaller.cpp @@ -34,11 +34,11 @@ #include "linuxdeviceconfiguration.h" #include -#include +#include #include -using namespace Utils; +using namespace QSsh; namespace RemoteLinux { namespace Internal { @@ -50,8 +50,8 @@ public: bool isRunning; LinuxDeviceConfiguration::ConstPtr deviceConfig; - Utils::SshRemoteProcessRunner *installer; - Utils::SshRemoteProcessRunner *killProcess; + QSsh::SshRemoteProcessRunner *installer; + QSsh::SshRemoteProcessRunner *killProcess; }; } // namespace Internal diff --git a/src/plugins/remotelinux/remotelinuxprocesslist.cpp b/src/plugins/remotelinux/remotelinuxprocesslist.cpp index 29c4deb321..347f17d0f8 100644 --- a/src/plugins/remotelinux/remotelinuxprocesslist.cpp +++ b/src/plugins/remotelinux/remotelinuxprocesslist.cpp @@ -33,12 +33,12 @@ #include "linuxdeviceconfiguration.h" #include -#include +#include #include #include -using namespace Utils; +using namespace QSsh; namespace RemoteLinux { namespace Internal { diff --git a/src/plugins/remotelinux/remotelinuxrunconfiguration.cpp b/src/plugins/remotelinux/remotelinuxrunconfiguration.cpp index 03e935c8c4..f39af40b42 100644 --- a/src/plugins/remotelinux/remotelinuxrunconfiguration.cpp +++ b/src/plugins/remotelinux/remotelinuxrunconfiguration.cpp @@ -51,6 +51,7 @@ using namespace ProjectExplorer; using namespace Qt4ProjectManager; +using namespace QSsh; using namespace Utils; namespace RemoteLinux { diff --git a/src/plugins/remotelinux/remotelinuxrunconfiguration.h b/src/plugins/remotelinux/remotelinuxrunconfiguration.h index 77ad9b16e3..55ae2a740a 100644 --- a/src/plugins/remotelinux/remotelinuxrunconfiguration.h +++ b/src/plugins/remotelinux/remotelinuxrunconfiguration.h @@ -44,9 +44,7 @@ class Qt4BaseTarget; class Qt4ProFileNode; } // namespace Qt4ProjectManager -namespace Utils { -class PortList; -} +namespace Utils { class PortList; } namespace RemoteLinux { class LinuxDeviceConfiguration; diff --git a/src/plugins/remotelinux/remotelinuxusedportsgatherer.cpp b/src/plugins/remotelinux/remotelinuxusedportsgatherer.cpp index 23c442b52d..5dc94bbc68 100644 --- a/src/plugins/remotelinux/remotelinuxusedportsgatherer.cpp +++ b/src/plugins/remotelinux/remotelinuxusedportsgatherer.cpp @@ -34,12 +34,13 @@ #include #include -#include -#include -#include +#include +#include +#include #include +using namespace QSsh; using namespace Utils; namespace RemoteLinux { @@ -75,7 +76,7 @@ void RemoteLinuxUsedPortsGatherer::start(const LinuxDeviceConfiguration::ConstPt QTC_ASSERT(!d->connection, return); d->portsToCheck = devConf->freePorts(); d->connection = SshConnectionManager::instance().acquireConnection(devConf->sshParameters()); - connect(d->connection.data(), SIGNAL(error(Utils::SshError)), SLOT(handleConnectionError())); + connect(d->connection.data(), SIGNAL(error(QSsh::SshError)), SLOT(handleConnectionError())); if (d->connection->state() == SshConnection::Connected) { handleConnectionEstablished(); return; diff --git a/src/plugins/remotelinux/sshkeydeployer.cpp b/src/plugins/remotelinux/sshkeydeployer.cpp index 955373c266..fcdd31a8dd 100644 --- a/src/plugins/remotelinux/sshkeydeployer.cpp +++ b/src/plugins/remotelinux/sshkeydeployer.cpp @@ -30,13 +30,13 @@ **************************************************************************/ #include "sshkeydeployer.h" -#include +#include #include #include #include -using namespace Utils; +using namespace QSsh; namespace RemoteLinux { namespace Internal { diff --git a/src/plugins/remotelinux/sshkeydeployer.h b/src/plugins/remotelinux/sshkeydeployer.h index 5debd0e194..28c9d262eb 100644 --- a/src/plugins/remotelinux/sshkeydeployer.h +++ b/src/plugins/remotelinux/sshkeydeployer.h @@ -35,7 +35,7 @@ #include -namespace Utils { +namespace QSsh { class SshConnectionParameters; } @@ -52,7 +52,7 @@ public: explicit SshKeyDeployer(QObject *parent = 0); ~SshKeyDeployer(); - void deployPublicKey(const Utils::SshConnectionParameters &sshParams, + void deployPublicKey(const QSsh::SshConnectionParameters &sshParams, const QString &keyFilePath); void stopDeployment(); diff --git a/src/plugins/remotelinux/startgdbserverdialog.cpp b/src/plugins/remotelinux/startgdbserverdialog.cpp index 2723658693..9cc6d5d06c 100644 --- a/src/plugins/remotelinux/startgdbserverdialog.cpp +++ b/src/plugins/remotelinux/startgdbserverdialog.cpp @@ -43,8 +43,8 @@ #include #include #include -#include -#include +#include +#include #include #include @@ -70,6 +70,7 @@ using namespace Core; using namespace ProjectExplorer; +using namespace QSsh; using namespace Utils; const char LastSysroot[] = "RemoteLinux/LastSysroot"; diff --git a/src/plugins/valgrind/callgrind/callgrindcontroller.cpp b/src/plugins/valgrind/callgrind/callgrindcontroller.cpp index ea765081bf..8cbd1a124d 100644 --- a/src/plugins/valgrind/callgrind/callgrindcontroller.cpp +++ b/src/plugins/valgrind/callgrind/callgrindcontroller.cpp @@ -38,7 +38,7 @@ #include #include -#include +#include #include @@ -236,8 +236,8 @@ void CallgrindController::foundRemoteFile() m_remoteFile = m_findRemoteFile->readAllStandardOutput().trimmed(); m_sftp = m_ssh->createSftpChannel(); - connect(m_sftp.data(), SIGNAL(finished(Utils::SftpJobId,QString)), - this, SLOT(sftpJobFinished(Utils::SftpJobId,QString))); + connect(m_sftp.data(), SIGNAL(finished(QSsh::SftpJobId,QString)), + this, SLOT(sftpJobFinished(QSsh::SftpJobId,QString))); connect(m_sftp.data(), SIGNAL(initialized()), this, SLOT(sftpInitialized())); m_sftp->initialize(); } @@ -251,10 +251,10 @@ void CallgrindController::sftpInitialized() dataFile.setAutoRemove(false); dataFile.close(); - m_downloadJob = m_sftp->downloadFile(m_remoteFile, m_tempDataFile, Utils::SftpOverwriteExisting); + m_downloadJob = m_sftp->downloadFile(m_remoteFile, m_tempDataFile, QSsh::SftpOverwriteExisting); } -void CallgrindController::sftpJobFinished(Utils::SftpJobId job, const QString &error) +void CallgrindController::sftpJobFinished(QSsh::SftpJobId job, const QString &error) { QTC_ASSERT(job == m_downloadJob, return); diff --git a/src/plugins/valgrind/callgrind/callgrindcontroller.h b/src/plugins/valgrind/callgrind/callgrindcontroller.h index a8b66f7418..55bc6134ea 100644 --- a/src/plugins/valgrind/callgrind/callgrindcontroller.h +++ b/src/plugins/valgrind/callgrind/callgrindcontroller.h @@ -37,9 +37,9 @@ #include -#include -#include -#include +#include +#include +#include namespace Valgrind { @@ -89,7 +89,7 @@ private Q_SLOTS: void foundRemoteFile(); void sftpInitialized(); - void sftpJobFinished(Utils::SftpJobId job, const QString &error); + void sftpJobFinished(QSsh::SftpJobId job, const QString &error); private: void cleanupTempFile(); @@ -102,11 +102,11 @@ private: Option m_lastOption; // remote callgrind support - Utils::SshConnection::Ptr m_ssh; + QSsh::SshConnection::Ptr m_ssh; QString m_tempDataFile; - Utils::SshRemoteProcess::Ptr m_findRemoteFile; - Utils::SftpChannel::Ptr m_sftp; - Utils::SftpJobId m_downloadJob; + QSsh::SshRemoteProcess::Ptr m_findRemoteFile; + QSsh::SftpChannel::Ptr m_sftp; + QSsh::SftpJobId m_downloadJob; QByteArray m_remoteFile; }; diff --git a/src/plugins/valgrind/valgrindprocess.cpp b/src/plugins/valgrind/valgrindprocess.cpp index b85a720487..b739f35a5a 100644 --- a/src/plugins/valgrind/valgrindprocess.cpp +++ b/src/plugins/valgrind/valgrindprocess.cpp @@ -148,7 +148,7 @@ void LocalValgrindProcess::readyReadStandardOutput() //////////////////////// -RemoteValgrindProcess::RemoteValgrindProcess(const Utils::SshConnectionParameters &sshParams, +RemoteValgrindProcess::RemoteValgrindProcess(const QSsh::SshConnectionParameters &sshParams, QObject *parent) : ValgrindProcess(parent) , m_params(sshParams) @@ -156,7 +156,7 @@ RemoteValgrindProcess::RemoteValgrindProcess(const Utils::SshConnectionParameter , m_pid(0) {} -RemoteValgrindProcess::RemoteValgrindProcess(const Utils::SshConnection::Ptr &connection, QObject *parent) +RemoteValgrindProcess::RemoteValgrindProcess(const QSsh::SshConnection::Ptr &connection, QObject *parent) : ValgrindProcess(parent) , m_params(connection->connectionParameters()) , m_connection(connection) @@ -179,14 +179,14 @@ void RemoteValgrindProcess::run(const QString &valgrindExecutable, const QString // connect to host and wait for connection if (!m_connection) - m_connection = Utils::SshConnection::create(m_params); + m_connection = QSsh::SshConnection::create(m_params); - if (m_connection->state() != Utils::SshConnection::Connected) { + if (m_connection->state() != QSsh::SshConnection::Connected) { connect(m_connection.data(), SIGNAL(connected()), this, SLOT(connected())); - connect(m_connection.data(), SIGNAL(error(Utils::SshError)), - this, SLOT(error(Utils::SshError))); - if (m_connection->state() == Utils::SshConnection::Unconnected) + connect(m_connection.data(), SIGNAL(error(QSsh::SshError)), + this, SLOT(error(QSsh::SshError))); + if (m_connection->state() == QSsh::SshConnection::Unconnected) m_connection->connectToHost(); } else { connected(); @@ -195,7 +195,7 @@ void RemoteValgrindProcess::run(const QString &valgrindExecutable, const QString void RemoteValgrindProcess::connected() { - QTC_ASSERT(m_connection->state() == Utils::SshConnection::Connected, return); + QTC_ASSERT(m_connection->state() == QSsh::SshConnection::Connected, return); // connected, run command QString cmd; @@ -219,14 +219,14 @@ void RemoteValgrindProcess::connected() m_process->start(); } -Utils::SshConnection::Ptr RemoteValgrindProcess::connection() const +QSsh::SshConnection::Ptr RemoteValgrindProcess::connection() const { return m_connection; } void RemoteValgrindProcess::processStarted() { - QTC_ASSERT(m_connection->state() == Utils::SshConnection::Connected, return); + QTC_ASSERT(m_connection->state() == QSsh::SshConnection::Connected, return); // find out what PID our process has @@ -278,10 +278,10 @@ void RemoteValgrindProcess::standardError() emit processOutput(m_process->readAllStandardError(), Utils::StdErrFormat); } -void RemoteValgrindProcess::error(Utils::SshError error) +void RemoteValgrindProcess::error(QSsh::SshError error) { switch (error) { - case Utils::SshTimeoutError: + case QSsh::SshTimeoutError: m_error = QProcess::Timedout; break; default: @@ -294,7 +294,7 @@ void RemoteValgrindProcess::error(Utils::SshError error) void RemoteValgrindProcess::close() { - QTC_ASSERT(m_connection->state() == Utils::SshConnection::Connected, return); + QTC_ASSERT(m_connection->state() == QSsh::SshConnection::Connected, return); if (m_process) { if (m_pid) { const QString killTemplate = QString("kill -%2 %1" // kill @@ -304,7 +304,7 @@ void RemoteValgrindProcess::close() const QString brutalKill = killTemplate.arg("SIGKILL"); const QString remoteCall = niceKill + QLatin1String("; sleep 1; ") + brutalKill; - Utils::SshRemoteProcess::Ptr cleanup = m_connection->createRemoteProcess(remoteCall.toUtf8()); + QSsh::SshRemoteProcess::Ptr cleanup = m_connection->createRemoteProcess(remoteCall.toUtf8()); cleanup->start(); } } @@ -315,12 +315,12 @@ void RemoteValgrindProcess::closed(int status) QTC_ASSERT(m_process, return); m_errorString = m_process->errorString(); - if (status == Utils::SshRemoteProcess::FailedToStart) { + if (status == QSsh::SshRemoteProcess::FailedToStart) { m_error = QProcess::FailedToStart; emit ValgrindProcess::error(QProcess::FailedToStart); - } else if (status == Utils::SshRemoteProcess::ExitedNormally) { + } else if (status == QSsh::SshRemoteProcess::ExitedNormally) { emit finished(m_process->exitCode(), QProcess::NormalExit); - } else if (status == Utils::SshRemoteProcess::KilledBySignal) { + } else if (status == QSsh::SshRemoteProcess::KilledBySignal) { m_error = QProcess::Crashed; emit finished(m_process->exitCode(), QProcess::CrashExit); } diff --git a/src/plugins/valgrind/valgrindprocess.h b/src/plugins/valgrind/valgrindprocess.h index 857ead960e..18927f47b1 100644 --- a/src/plugins/valgrind/valgrindprocess.h +++ b/src/plugins/valgrind/valgrindprocess.h @@ -36,8 +36,8 @@ #define VALGRIND_RUNNER_P_H #include -#include -#include +#include +#include #include namespace Valgrind { @@ -118,9 +118,9 @@ class RemoteValgrindProcess : public ValgrindProcess Q_OBJECT public: - explicit RemoteValgrindProcess(const Utils::SshConnectionParameters &sshParams, + explicit RemoteValgrindProcess(const QSsh::SshConnectionParameters &sshParams, QObject *parent = 0); - explicit RemoteValgrindProcess(const Utils::SshConnection::Ptr &connection, + explicit RemoteValgrindProcess(const QSsh::SshConnection::Ptr &connection, QObject *parent = 0); virtual bool isRunning() const; @@ -139,21 +139,21 @@ public: virtual qint64 pid() const; - Utils::SshConnection::Ptr connection() const; + QSsh::SshConnection::Ptr connection() const; private slots: void closed(int); void connected(); - void error(Utils::SshError error); + void error(QSsh::SshError error); void processStarted(); void findPIDOutputReceived(); void standardOutput(); void standardError(); private: - Utils::SshConnectionParameters m_params; - Utils::SshConnection::Ptr m_connection; - Utils::SshRemoteProcess::Ptr m_process; + QSsh::SshConnectionParameters m_params; + QSsh::SshConnection::Ptr m_connection; + QSsh::SshRemoteProcess::Ptr m_process; QString m_workingDir; QString m_valgrindExe; QStringList m_valgrindArgs; @@ -162,7 +162,7 @@ private: QString m_errorString; QProcess::ProcessError m_error; qint64 m_pid; - Utils::SshRemoteProcess::Ptr m_findPID; + QSsh::SshRemoteProcess::Ptr m_findPID; }; } // namespace Valgrind diff --git a/src/plugins/valgrind/valgrindrunner.cpp b/src/plugins/valgrind/valgrindrunner.cpp index 25a0c8aad0..5900c28b52 100644 --- a/src/plugins/valgrind/valgrindrunner.cpp +++ b/src/plugins/valgrind/valgrindrunner.cpp @@ -38,8 +38,8 @@ #include #include -#include -#include +#include +#include #include @@ -70,7 +70,7 @@ public: QString debuggeeArguments; QString workingdir; Analyzer::StartMode startMode; - Utils::SshConnectionParameters connParams; + QSsh::SshConnectionParameters connParams; }; void ValgrindRunner::Private::run(ValgrindProcess *_process) @@ -178,12 +178,12 @@ void ValgrindRunner::setStartMode(Analyzer::StartMode startMode) d->startMode = startMode; } -const Utils::SshConnectionParameters &ValgrindRunner::connectionParameters() const +const QSsh::SshConnectionParameters &ValgrindRunner::connectionParameters() const { return d->connParams; } -void ValgrindRunner::setConnectionParameters(const Utils::SshConnectionParameters &connParams) +void ValgrindRunner::setConnectionParameters(const QSsh::SshConnectionParameters &connParams) { d->connParams = connParams; } diff --git a/src/plugins/valgrind/valgrindrunner.h b/src/plugins/valgrind/valgrindrunner.h index 1531024cef..12917c930c 100644 --- a/src/plugins/valgrind/valgrindrunner.h +++ b/src/plugins/valgrind/valgrindrunner.h @@ -38,7 +38,7 @@ #include #include -#include +#include #include @@ -76,8 +76,8 @@ public: void setStartMode(Analyzer::StartMode startMode); Analyzer::StartMode startMode() const; - void setConnectionParameters(const Utils::SshConnectionParameters &connParams); - const Utils::SshConnectionParameters &connectionParameters() const; + void setConnectionParameters(const QSsh::SshConnectionParameters &connParams); + const QSsh::SshConnectionParameters &connectionParameters() const; void waitForFinished() const; diff --git a/tests/manual/ssh/errorhandling/main.cpp b/tests/manual/ssh/errorhandling/main.cpp index b999877f2c..67f0a4faaa 100644 --- a/tests/manual/ssh/errorhandling/main.cpp +++ b/tests/manual/ssh/errorhandling/main.cpp @@ -30,9 +30,9 @@ ** **************************************************************************/ -#include -#include -#include +#include +#include +#include #include #include @@ -40,7 +40,7 @@ #include #include -using namespace Utils; +using namespace QSsh; class Test : public QObject { Q_OBJECT @@ -129,7 +129,7 @@ private slots: qApp->quit(); } - void handleError(Utils::SshError error) + void handleError(QSsh::SshError error) { if (m_testSet.isEmpty()) { qDebug("Error: Received error %d, but no test was running.", error); @@ -173,8 +173,8 @@ private: SLOT(handleDisconnected())); connect(m_connection.data(), SIGNAL(dataAvailable(QString)), this, SLOT(handleDataAvailable(QString))); - connect(m_connection.data(), SIGNAL(error(Utils::SshError)), this, - SLOT(handleError(Utils::SshError))); + connect(m_connection.data(), SIGNAL(error(QSsh::SshError)), this, + SLOT(handleError(QSsh::SshError))); const TestItem &nextItem = m_testSet.first(); m_timeoutTimer.stop(); m_timeoutTimer.setInterval(qMax(10000, nextItem.params.timeout * 1000)); diff --git a/tests/manual/ssh/remoteprocess/argumentscollector.cpp b/tests/manual/ssh/remoteprocess/argumentscollector.cpp index 64cc0ef883..b5acea9968 100644 --- a/tests/manual/ssh/remoteprocess/argumentscollector.cpp +++ b/tests/manual/ssh/remoteprocess/argumentscollector.cpp @@ -36,15 +36,16 @@ #include +using namespace QSsh; + using namespace std; -using namespace Utils; ArgumentsCollector::ArgumentsCollector(const QStringList &args) : m_arguments(args) { } -Utils::SshConnectionParameters ArgumentsCollector::collect(bool &success) const +QSsh::SshConnectionParameters ArgumentsCollector::collect(bool &success) const { SshConnectionParameters parameters; try { diff --git a/tests/manual/ssh/remoteprocess/argumentscollector.h b/tests/manual/ssh/remoteprocess/argumentscollector.h index e2dc69e0b3..d7189597f1 100644 --- a/tests/manual/ssh/remoteprocess/argumentscollector.h +++ b/tests/manual/ssh/remoteprocess/argumentscollector.h @@ -33,7 +33,7 @@ #ifndef ARGUMENTSCOLLECTOR_H #define ARGUMENTSCOLLECTOR_H -#include +#include #include @@ -41,7 +41,7 @@ class ArgumentsCollector { public: ArgumentsCollector(const QStringList &args); - Utils::SshConnectionParameters collect(bool &success) const; + QSsh::SshConnectionParameters collect(bool &success) const; private: struct ArgumentErrorException { @@ -54,7 +54,7 @@ private: bool checkAndSetIntArg(int &pos, int &val, bool &alreadyGiven, const char *opt) const; bool checkForNoProxy(int &pos, - Utils::SshConnectionParameters::ProxyType &type, + QSsh::SshConnectionParameters::ProxyType &type, bool &alreadyGiven) const; const QStringList m_arguments; diff --git a/tests/manual/ssh/remoteprocess/main.cpp b/tests/manual/ssh/remoteprocess/main.cpp index 01203bd2d0..0fac815a29 100644 --- a/tests/manual/ssh/remoteprocess/main.cpp +++ b/tests/manual/ssh/remoteprocess/main.cpp @@ -33,7 +33,7 @@ #include "argumentscollector.h" #include "remoteprocesstest.h" -#include +#include #include #include @@ -42,13 +42,11 @@ #include #include -using namespace Utils; - int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); bool parseSuccess; - const Utils::SshConnectionParameters ¶meters + const QSsh::SshConnectionParameters ¶meters = ArgumentsCollector(app.arguments()).collect(parseSuccess); if (!parseSuccess) return EXIT_FAILURE; diff --git a/tests/manual/ssh/remoteprocess/remoteprocesstest.cpp b/tests/manual/ssh/remoteprocess/remoteprocesstest.cpp index 47eaaee77b..2f0557491a 100644 --- a/tests/manual/ssh/remoteprocess/remoteprocesstest.cpp +++ b/tests/manual/ssh/remoteprocess/remoteprocesstest.cpp @@ -32,7 +32,7 @@ #include "remoteprocesstest.h" -#include +#include #include #include @@ -40,7 +40,7 @@ #include -using namespace Utils; +using namespace QSsh; const QByteArray StderrOutput("ChannelTest"); @@ -93,8 +93,8 @@ void RemoteProcessTest::handleProcessStarted() } else { m_started = true; if (m_state == TestingCrash) { - Utils::SshRemoteProcessRunner * const killer - = new Utils::SshRemoteProcessRunner(this); + QSsh::SshRemoteProcessRunner * const killer + = new QSsh::SshRemoteProcessRunner(this); killer->run("pkill -9 sleep", m_sshParams); } else if (m_state == TestingIoDevice) { connect(m_catProcess.data(), SIGNAL(readyRead()), SLOT(handleReadyRead())); @@ -214,9 +214,9 @@ void RemoteProcessTest::handleProcessClosed(int exitStatus) } std::cout << "Ok.\nTesting I/O device functionality... " << std::flush; m_state = TestingIoDevice; - m_sshConnection = Utils::SshConnection::create(m_sshParams); + m_sshConnection = QSsh::SshConnection::create(m_sshParams); connect(m_sshConnection.data(), SIGNAL(connected()), SLOT(handleConnected())); - connect(m_sshConnection.data(), SIGNAL(error(Utils::SshError)), + connect(m_sshConnection.data(), SIGNAL(error(QSsh::SshError)), SLOT(handleConnectionError())); m_sshConnection->connectToHost(); m_timeoutTimer->start(); @@ -308,7 +308,7 @@ void RemoteProcessTest::handleReadyRead() << qPrintable(testString()) << "', got '" << qPrintable(data) << "'." << std::endl; qApp->exit(1); } - Utils::SshRemoteProcessRunner * const killer = new Utils::SshRemoteProcessRunner(this); + QSsh::SshRemoteProcessRunner * const killer = new QSsh::SshRemoteProcessRunner(this); killer->run("pkill -9 cat", m_sshParams); break; } diff --git a/tests/manual/ssh/remoteprocess/remoteprocesstest.h b/tests/manual/ssh/remoteprocess/remoteprocesstest.h index e203eaea63..0eb59ab0f6 100644 --- a/tests/manual/ssh/remoteprocess/remoteprocesstest.h +++ b/tests/manual/ssh/remoteprocess/remoteprocesstest.h @@ -33,7 +33,7 @@ #ifndef REMOTEPROCESSTEST_H #define REMOTEPROCESSTEST_H -#include +#include #include @@ -44,7 +44,7 @@ class RemoteProcessTest : public QObject { Q_OBJECT public: - RemoteProcessTest(const Utils::SshConnectionParameters ¶ms); + RemoteProcessTest(const QSsh::SshConnectionParameters ¶ms); ~RemoteProcessTest(); void run(); @@ -70,13 +70,13 @@ private: void handleSuccessfulCrashTest(); void handleSuccessfulIoTest(); - const Utils::SshConnectionParameters m_sshParams; + const QSsh::SshConnectionParameters m_sshParams; QTimer * const m_timeoutTimer; QTextStream *m_textStream; - Utils::SshRemoteProcessRunner * const m_remoteRunner; - Utils::SshRemoteProcess::Ptr m_catProcess; - Utils::SshRemoteProcess::Ptr m_echoProcess; - Utils::SshConnection::Ptr m_sshConnection; + QSsh::SshRemoteProcessRunner * const m_remoteRunner; + QSsh::SshRemoteProcess::Ptr m_catProcess; + QSsh::SshRemoteProcess::Ptr m_echoProcess; + QSsh::SshConnection::Ptr m_sshConnection; QByteArray m_remoteStdout; QByteArray m_remoteStderr; QByteArray m_remoteData; diff --git a/tests/manual/ssh/sftp/argumentscollector.cpp b/tests/manual/ssh/sftp/argumentscollector.cpp index d51a00c3d6..a10fea0ee4 100644 --- a/tests/manual/ssh/sftp/argumentscollector.cpp +++ b/tests/manual/ssh/sftp/argumentscollector.cpp @@ -34,8 +34,9 @@ #include +using namespace QSsh; + using namespace std; -using namespace Utils; ArgumentsCollector::ArgumentsCollector(const QStringList &args) : m_arguments(args) diff --git a/tests/manual/ssh/sftp/argumentscollector.h b/tests/manual/ssh/sftp/argumentscollector.h index 62fb79061d..ca67456b89 100644 --- a/tests/manual/ssh/sftp/argumentscollector.h +++ b/tests/manual/ssh/sftp/argumentscollector.h @@ -53,8 +53,7 @@ private: bool checkAndSetStringArg(int &pos, QString &arg, const char *opt) const; bool checkAndSetIntArg(int &pos, int &val, bool &alreadyGiven, const char *opt) const; - bool checkForNoProxy(int &pos, - Utils::SshConnectionParameters::ProxyType &type, + bool checkForNoProxy(int &pos, QSsh::SshConnectionParameters::ProxyType &type, bool &alreadyGiven) const; const QStringList m_arguments; diff --git a/tests/manual/ssh/sftp/main.cpp b/tests/manual/ssh/sftp/main.cpp index 8a0a244b91..231cb14909 100644 --- a/tests/manual/ssh/sftp/main.cpp +++ b/tests/manual/ssh/sftp/main.cpp @@ -33,8 +33,8 @@ #include "argumentscollector.h" #include "sftptest.h" -#include -#include +#include +#include #include #include @@ -43,14 +43,11 @@ #include #include -using namespace Utils; - int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); bool parseSuccess; - const Parameters parameters - = ArgumentsCollector(app.arguments()).collect(parseSuccess); + const Parameters parameters = ArgumentsCollector(app.arguments()).collect(parseSuccess); if (!parseSuccess) return EXIT_FAILURE; SftpTest sftpTest(parameters); diff --git a/tests/manual/ssh/sftp/parameters.h b/tests/manual/ssh/sftp/parameters.h index 81b473239c..7a29245269 100644 --- a/tests/manual/ssh/sftp/parameters.h +++ b/tests/manual/ssh/sftp/parameters.h @@ -33,10 +33,10 @@ #ifndef PARAMETERS_H #define PARAMETERS_H -#include +#include struct Parameters { - Utils::SshConnectionParameters sshParams; + QSsh::SshConnectionParameters sshParams; int smallFileCount; int bigFileSize; }; diff --git a/tests/manual/ssh/sftp/sftptest.cpp b/tests/manual/ssh/sftp/sftptest.cpp index 8083ce0498..b1372d6f3c 100644 --- a/tests/manual/ssh/sftp/sftptest.cpp +++ b/tests/manual/ssh/sftp/sftptest.cpp @@ -40,7 +40,7 @@ #include -using namespace Utils; +using namespace QSsh; SftpTest::SftpTest(const Parameters ¶ms) : m_parameters(params), m_state(Inactive), m_error(false), @@ -64,7 +64,7 @@ void SftpTest::run() m_connection = SshConnection::create(m_parameters.sshParams); connect(m_connection.data(), SIGNAL(connected()), this, SLOT(handleConnected())); - connect(m_connection.data(), SIGNAL(error(Utils::SshError)), this, + connect(m_connection.data(), SIGNAL(error(QSsh::SshError)), this, SLOT(handleError())); connect(m_connection.data(), SIGNAL(disconnected()), this, SLOT(handleDisconnected())); @@ -87,11 +87,11 @@ void SftpTest::handleConnected() SLOT(handleChannelInitialized())); connect(m_channel.data(), SIGNAL(initializationFailed(QString)), this, SLOT(handleChannelInitializationFailure(QString))); - connect(m_channel.data(), SIGNAL(finished(Utils::SftpJobId,QString)), - this, SLOT(handleJobFinished(Utils::SftpJobId,QString))); + connect(m_channel.data(), SIGNAL(finished(QSsh::SftpJobId,QString)), + this, SLOT(handleJobFinished(QSsh::SftpJobId,QString))); connect(m_channel.data(), - SIGNAL(fileInfoAvailable(Utils::SftpJobId,QList)), - SLOT(handleFileInfo(Utils::SftpJobId,QList))); + SIGNAL(fileInfoAvailable(QSsh::SftpJobId,QList)), + SLOT(handleFileInfo(QSsh::SftpJobId,QList))); connect(m_channel.data(), SIGNAL(closed()), this, SLOT(handleChannelClosed())); m_state = InitializingChannel; @@ -200,7 +200,7 @@ void SftpTest::handleChannelClosed() m_connection->disconnectFromHost(); } -void SftpTest::handleJobFinished(Utils::SftpJobId job, const QString &error) +void SftpTest::handleJobFinished(QSsh::SftpJobId job, const QString &error) { switch (m_state) { case UploadingSmall: diff --git a/tests/manual/ssh/sftp/sftptest.h b/tests/manual/ssh/sftp/sftptest.h index ca84e850c2..8ca5a3dff4 100644 --- a/tests/manual/ssh/sftp/sftptest.h +++ b/tests/manual/ssh/sftp/sftptest.h @@ -35,8 +35,8 @@ #include "parameters.h" -#include -#include +#include +#include #include #include @@ -60,12 +60,12 @@ private slots: void handleDisconnected(); void handleChannelInitialized(); void handleChannelInitializationFailure(const QString &reason); - void handleJobFinished(Utils::SftpJobId job, const QString &error); - void handleFileInfo(Utils::SftpJobId job, const QList &fileInfoList); + void handleJobFinished(QSsh::SftpJobId job, const QString &error); + void handleFileInfo(QSsh::SftpJobId job, const QList &fileInfoList); void handleChannelClosed(); private: - typedef QHash JobMap; + typedef QHash JobMap; typedef QSharedPointer FilePtr; enum State { Inactive, Connecting, InitializingChannel, UploadingSmall, DownloadingSmall, @@ -78,36 +78,36 @@ private: QString cmpFileName(const QString &localFileName) const; QString remoteFilePath(const QString &localFileName) const; void earlyDisconnectFromHost(); - bool checkJobId(Utils::SftpJobId job, Utils::SftpJobId expectedJob, const char *activity); - bool handleJobFinished(Utils::SftpJobId job, JobMap &jobMap, + bool checkJobId(QSsh::SftpJobId job, QSsh::SftpJobId expectedJob, const char *activity); + bool handleJobFinished(QSsh::SftpJobId job, JobMap &jobMap, const QString &error, const char *activity); - bool handleJobFinished(Utils::SftpJobId job, Utils::SftpJobId expectedJob, const QString &error, + bool handleJobFinished(QSsh::SftpJobId job, QSsh::SftpJobId expectedJob, const QString &error, const char *activity); - bool handleBigJobFinished(Utils::SftpJobId job, Utils::SftpJobId expectedJob, + bool handleBigJobFinished(QSsh::SftpJobId job, QSsh::SftpJobId expectedJob, const QString &error, const char *activity); bool compareFiles(QFile *orig, QFile *copy); const Parameters m_parameters; State m_state; bool m_error; - Utils::SshConnection::Ptr m_connection; - Utils::SftpChannel::Ptr m_channel; + QSsh::SshConnection::Ptr m_connection; + QSsh::SftpChannel::Ptr m_channel; QList m_localSmallFiles; JobMap m_smallFilesUploadJobs; JobMap m_smallFilesDownloadJobs; JobMap m_smallFilesRemovalJobs; FilePtr m_localBigFile; - Utils::SftpJobId m_bigFileUploadJob; - Utils::SftpJobId m_bigFileDownloadJob; - Utils::SftpJobId m_bigFileRemovalJob; - Utils::SftpJobId m_mkdirJob; - Utils::SftpJobId m_statDirJob; - Utils::SftpJobId m_lsDirJob; - Utils::SftpJobId m_rmDirJob; + QSsh::SftpJobId m_bigFileUploadJob; + QSsh::SftpJobId m_bigFileDownloadJob; + QSsh::SftpJobId m_bigFileRemovalJob; + QSsh::SftpJobId m_mkdirJob; + QSsh::SftpJobId m_statDirJob; + QSsh::SftpJobId m_lsDirJob; + QSsh::SftpJobId m_rmDirJob; QElapsedTimer m_bigJobTimer; QString m_remoteDirPath; - Utils::SftpFileInfo m_dirInfo; - QList m_dirContents; + QSsh::SftpFileInfo m_dirInfo; + QList m_dirContents; }; diff --git a/tests/manual/ssh/sftpfsmodel/main.cpp b/tests/manual/ssh/sftpfsmodel/main.cpp index 4525c0b30e..655a00d74f 100644 --- a/tests/manual/ssh/sftpfsmodel/main.cpp +++ b/tests/manual/ssh/sftpfsmodel/main.cpp @@ -31,14 +31,12 @@ **************************************************************************/ #include "window.h" -#include -#include +#include +#include #include #include -using namespace Utils; - int main(int argc, char *argv[]) { QApplication app(argc, argv); diff --git a/tests/manual/ssh/sftpfsmodel/window.cpp b/tests/manual/ssh/sftpfsmodel/window.cpp index d3ca3dccda..ee3637bb2f 100644 --- a/tests/manual/ssh/sftpfsmodel/window.cpp +++ b/tests/manual/ssh/sftpfsmodel/window.cpp @@ -34,8 +34,8 @@ #include "modeltest.h" -#include -#include +#include +#include #include #include @@ -45,7 +45,7 @@ #include #include -using namespace Utils; +using namespace QSsh; SftpFsWindow::SftpFsWindow(QWidget *parent) : QDialog(parent), m_ui(new Ui::Window) { @@ -75,8 +75,8 @@ void SftpFsWindow::connectToHost() connect(m_fsModel, SIGNAL(sftpOperationFailed(QString)), SLOT(handleSftpOperationFailed(QString))); connect(m_fsModel, SIGNAL(connectionError(QString)), SLOT(handleConnectionError(QString))); - connect(m_fsModel, SIGNAL(sftpOperationFinished(Utils::SftpJobId,QString)), - SLOT(handleSftpOperationFinished(Utils::SftpJobId,QString))); + connect(m_fsModel, SIGNAL(sftpOperationFinished(QSsh::SftpJobId,QString)), + SLOT(handleSftpOperationFinished(QSsh::SftpJobId,QString))); m_fsModel->setSshConnection(sshParams); m_ui->fsView->setModel(m_fsModel); } diff --git a/tests/manual/ssh/sftpfsmodel/window.h b/tests/manual/ssh/sftpfsmodel/window.h index b403df0582..5e6d5ee4a9 100644 --- a/tests/manual/ssh/sftpfsmodel/window.h +++ b/tests/manual/ssh/sftpfsmodel/window.h @@ -29,7 +29,7 @@ ** Nokia at qt-info@nokia.com. ** **************************************************************************/ -#include +#include #include @@ -37,7 +37,7 @@ QT_BEGIN_NAMESPACE namespace Ui { class Window; } QT_END_NAMESPACE -namespace Utils { class SftpFileSystemModel; } +namespace QSsh { class SftpFileSystemModel; } class SftpFsWindow : public QDialog { @@ -51,9 +51,9 @@ private slots: void downloadFile(); void handleConnectionError(const QString &errorMessage); void handleSftpOperationFailed(const QString &errorMessage); - void handleSftpOperationFinished(Utils::SftpJobId jobId, const QString &error); + void handleSftpOperationFinished(QSsh::SftpJobId jobId, const QString &error); private: - Utils::SftpFileSystemModel *m_fsModel; + QSsh::SftpFileSystemModel *m_fsModel; Ui::Window *m_ui; }; diff --git a/tests/manual/ssh/shell/main.cpp b/tests/manual/ssh/shell/main.cpp index ba51eae3c0..4767d2cb7a 100644 --- a/tests/manual/ssh/shell/main.cpp +++ b/tests/manual/ssh/shell/main.cpp @@ -32,7 +32,7 @@ #include "../remoteprocess/argumentscollector.h" #include "shell.h" -#include +#include #include #include @@ -41,13 +41,11 @@ #include #include -using namespace Utils; - int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); bool parseSuccess; - const Utils::SshConnectionParameters ¶meters + const QSsh::SshConnectionParameters ¶meters = ArgumentsCollector(app.arguments()).collect(parseSuccess); if (!parseSuccess) return EXIT_FAILURE; diff --git a/tests/manual/ssh/shell/shell.cpp b/tests/manual/ssh/shell/shell.cpp index fe5a58dd8c..392f4af29c 100644 --- a/tests/manual/ssh/shell/shell.cpp +++ b/tests/manual/ssh/shell/shell.cpp @@ -31,8 +31,8 @@ **************************************************************************/ #include "shell.h" -#include -#include +#include +#include #include #include @@ -41,16 +41,16 @@ #include #include -using namespace Utils; +using namespace QSsh; -Shell::Shell(const Utils::SshConnectionParameters ¶meters, QObject *parent) +Shell::Shell(const QSsh::SshConnectionParameters ¶meters, QObject *parent) : QObject(parent), m_connection(SshConnection::create(parameters)), m_stdin(new QFile(this)) { connect(m_connection.data(), SIGNAL(connected()), SLOT(handleConnected())); connect(m_connection.data(), SIGNAL(dataAvailable(QString)), SLOT(handleShellMessage(QString))); - connect(m_connection.data(), SIGNAL(error(Utils::SshError)), SLOT(handleConnectionError())); + connect(m_connection.data(), SIGNAL(error(QSsh::SshError)), SLOT(handleConnectionError())); } Shell::~Shell() diff --git a/tests/manual/ssh/shell/shell.h b/tests/manual/ssh/shell/shell.h index 6ab410cc66..9e6e5c364b 100644 --- a/tests/manual/ssh/shell/shell.h +++ b/tests/manual/ssh/shell/shell.h @@ -35,7 +35,7 @@ #include #include -namespace Utils { +namespace QSsh { class SshConnection; class SshConnectionParameters; class SshRemoteProcess; @@ -51,7 +51,7 @@ class Shell : public QObject { Q_OBJECT public: - Shell(const Utils::SshConnectionParameters ¶meters, QObject *parent = 0); + Shell(const QSsh::SshConnectionParameters ¶meters, QObject *parent = 0); ~Shell(); void run(); @@ -67,8 +67,8 @@ private slots: void handleStdin(); private: - QSharedPointer m_connection; - QSharedPointer m_shell; + QSharedPointer m_connection; + QSharedPointer m_shell; QFile * const m_stdin; }; diff --git a/tests/manual/ssh/ssh.pri b/tests/manual/ssh/ssh.pri index b27a328c0c..f4c70a5d74 100644 --- a/tests/manual/ssh/ssh.pri +++ b/tests/manual/ssh/ssh.pri @@ -1,16 +1,13 @@ QT = core network include (../../../qtcreator.pri) -include (../../../src/plugins/coreplugin/coreplugin.pri) - -LIBS += -L$$IDE_PLUGIN_PATH/Nokia +include (../../../src/libs/ssh/ssh.pri) macx:QMAKE_LFLAGS += -Wl,-rpath,\"$$IDE_BIN_PATH/..\" INCLUDEPATH *= $$IDE_SOURCE_TREE/src/plugins -LIBS *= -L$$IDE_LIBRARY_PATH/Nokia +LIBS *= -L$$IDE_LIBRARY_PATH unix { - QMAKE_LFLAGS += -Wl,-rpath,\"$$IDE_PLUGIN_PATH/..\" - QMAKE_LFLAGS += -Wl,-rpath,\"$$IDE_PLUGIN_PATH/Nokia\" + QMAKE_LFLAGS += -Wl,-rpath,\"$$IDE_LIBRARY_PATH\" } CONFIG += console -- cgit v1.2.1