2011-04-13 08:42:33 +02:00
|
|
|
/**************************************************************************
|
2011-02-03 09:59:50 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file is part of Qt Creator
|
2011-02-03 09:59:50 +01:00
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2011-02-03 09:59:50 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2011-02-03 09:59:50 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2011-04-13 08:42:33 +02:00
|
|
|
**
|
|
|
|
|
** 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.
|
2011-02-03 09:59:50 +01:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-02-03 09:59:50 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
2011-02-03 09:59:50 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2011-02-03 09:59:50 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
**************************************************************************/
|
2011-07-22 12:38:57 +02:00
|
|
|
#include "sshkeydeployer.h"
|
2011-02-03 09:59:50 +01:00
|
|
|
|
2011-02-14 16:34:17 +01:00
|
|
|
#include <utils/ssh/sshremoteprocessrunner.h>
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <utils/fileutils.h>
|
2011-02-03 09:59:50 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFile>
|
|
|
|
|
#include <QSharedPointer>
|
2011-02-03 09:59:50 +01:00
|
|
|
|
2011-02-14 16:34:17 +01:00
|
|
|
using namespace Utils;
|
2011-02-03 09:59:50 +01:00
|
|
|
|
2011-05-25 11:23:25 +02:00
|
|
|
namespace RemoteLinux {
|
2011-08-02 12:20:16 +02:00
|
|
|
namespace Internal {
|
2011-02-03 09:59:50 +01:00
|
|
|
|
2011-08-02 12:20:16 +02:00
|
|
|
class SshKeyDeployerPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2011-11-09 14:19:50 +01:00
|
|
|
SshRemoteProcessRunner deployProcess;
|
2011-08-02 12:20:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
SshKeyDeployer::SshKeyDeployer(QObject *parent)
|
2011-09-15 09:10:10 +02:00
|
|
|
: QObject(parent), d(new Internal::SshKeyDeployerPrivate)
|
2011-02-03 09:59:50 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-22 12:38:57 +02:00
|
|
|
SshKeyDeployer::~SshKeyDeployer()
|
2011-02-03 09:59:50 +01:00
|
|
|
{
|
|
|
|
|
cleanup();
|
2011-09-15 09:10:10 +02:00
|
|
|
delete d;
|
2011-02-03 09:59:50 +01:00
|
|
|
}
|
|
|
|
|
|
2011-07-22 12:38:57 +02:00
|
|
|
void SshKeyDeployer::deployPublicKey(const SshConnectionParameters &sshParams,
|
2011-02-03 09:59:50 +01:00
|
|
|
const QString &keyFilePath)
|
|
|
|
|
{
|
|
|
|
|
cleanup();
|
|
|
|
|
|
2011-03-30 15:15:15 +02:00
|
|
|
Utils::FileReader reader;
|
|
|
|
|
if (!reader.fetch(keyFilePath)) {
|
|
|
|
|
emit error(tr("Public key error: %1").arg(reader.errorString()));
|
2011-02-03 09:59:50 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-09 17:38:19 +01:00
|
|
|
connect(&d->deployProcess, SIGNAL(connectionError()), SLOT(handleConnectionFailure()));
|
2011-11-09 14:19:50 +01:00
|
|
|
connect(&d->deployProcess, SIGNAL(processClosed(int)), SLOT(handleKeyUploadFinished(int)));
|
2011-02-03 09:59:50 +01:00
|
|
|
const QByteArray command = "test -d .ssh "
|
|
|
|
|
"|| mkdir .ssh && chmod 0700 .ssh && echo '"
|
2011-03-30 15:15:15 +02:00
|
|
|
+ reader.data() + "' >> .ssh/authorized_keys && chmod 0600 .ssh/authorized_keys";
|
2011-11-09 14:19:50 +01:00
|
|
|
d->deployProcess.run(command, sshParams);
|
2011-02-03 09:59:50 +01:00
|
|
|
}
|
|
|
|
|
|
2011-07-22 12:38:57 +02:00
|
|
|
void SshKeyDeployer::handleConnectionFailure()
|
2011-02-03 09:59:50 +01:00
|
|
|
{
|
|
|
|
|
cleanup();
|
2011-11-09 17:38:19 +01:00
|
|
|
emit error(tr("Connection failed: %1").arg(d->deployProcess.lastConnectionErrorString()));
|
2011-02-03 09:59:50 +01:00
|
|
|
}
|
|
|
|
|
|
2011-07-22 12:38:57 +02:00
|
|
|
void SshKeyDeployer::handleKeyUploadFinished(int exitStatus)
|
2011-02-03 09:59:50 +01:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
|
|
|
|
|
|| exitStatus == SshRemoteProcess::KilledBySignal
|
|
|
|
|
|| exitStatus == SshRemoteProcess::ExitedNormally);
|
|
|
|
|
|
2011-11-30 14:39:17 +01:00
|
|
|
const int exitCode = d->deployProcess.processExitCode();
|
|
|
|
|
const QString errorMsg = d->deployProcess.processErrorString();
|
2011-02-03 09:59:50 +01:00
|
|
|
cleanup();
|
|
|
|
|
if (exitStatus == SshRemoteProcess::ExitedNormally && exitCode == 0)
|
|
|
|
|
emit finishedSuccessfully();
|
|
|
|
|
else
|
|
|
|
|
emit error(tr("Key deployment failed: %1.").arg(errorMsg));
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-22 12:38:57 +02:00
|
|
|
void SshKeyDeployer::stopDeployment()
|
2011-02-03 09:59:50 +01:00
|
|
|
{
|
|
|
|
|
cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-22 12:38:57 +02:00
|
|
|
void SshKeyDeployer::cleanup()
|
2011-02-03 09:59:50 +01:00
|
|
|
{
|
2011-11-09 14:19:50 +01:00
|
|
|
disconnect(&d->deployProcess, 0, this, 0);
|
2011-02-03 09:59:50 +01:00
|
|
|
}
|
|
|
|
|
|
2011-05-25 11:23:25 +02:00
|
|
|
} // namespace RemoteLinux
|