2014-01-22 21:58:53 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2014 Samoilenko Yuri<kinnalru@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-06-21 20:32:18 +01:00
|
|
|
#include "mounter.h"
|
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
#include <QDir>
|
2014-09-21 23:45:59 +01:00
|
|
|
#include <QDebug>
|
2014-01-22 21:58:53 +00:00
|
|
|
|
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2015-06-21 20:32:18 +01:00
|
|
|
#include "mountloop.h"
|
2014-09-21 23:45:59 +01:00
|
|
|
#include "sftp_debug.h"
|
2016-07-05 13:27:53 +01:00
|
|
|
#include "kdeconnectconfig.h"
|
2014-01-22 21:58:53 +00:00
|
|
|
|
2015-03-09 05:12:55 +00:00
|
|
|
Mounter::Mounter(SftpPlugin* sftp)
|
2014-01-22 21:58:53 +00:00
|
|
|
: QObject(sftp)
|
|
|
|
, m_sftp(sftp)
|
2015-09-08 09:46:59 +01:00
|
|
|
, m_proc(nullptr)
|
2015-06-21 20:32:18 +01:00
|
|
|
, m_mountPoint(sftp->mountPoint())
|
2014-01-22 21:58:53 +00:00
|
|
|
, m_started(false)
|
2015-06-21 20:32:18 +01:00
|
|
|
{
|
|
|
|
|
2016-11-26 14:12:38 +00:00
|
|
|
connect(m_sftp, &SftpPlugin::packageReceived, this, &Mounter::onPakcageReceived);
|
2014-01-22 21:58:53 +00:00
|
|
|
|
2016-11-26 14:12:38 +00:00
|
|
|
connect(&m_connectTimer, &QTimer::timeout, this, &Mounter::onMountTimeout);
|
2014-01-22 21:58:53 +00:00
|
|
|
|
2016-11-26 14:12:38 +00:00
|
|
|
connect(this, &Mounter::mounted, &m_connectTimer, &QTimer::stop);
|
|
|
|
connect(this, &Mounter::failed, &m_connectTimer, &QTimer::stop);
|
2014-01-22 21:58:53 +00:00
|
|
|
|
|
|
|
m_connectTimer.setInterval(10000);
|
|
|
|
m_connectTimer.setSingleShot(true);
|
2015-03-09 05:12:55 +00:00
|
|
|
|
2016-11-26 14:12:38 +00:00
|
|
|
QTimer::singleShot(0, this, &Mounter::start);
|
2015-10-18 01:31:17 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Created mounter";
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Mounter::~Mounter()
|
|
|
|
{
|
2015-10-18 01:31:17 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Destroy mounter";
|
2016-02-17 13:06:37 +00:00
|
|
|
//FIXME: We don't unmount becuase it crashes if we try to unmount while the filesystem is being used. Potential memory leak.
|
|
|
|
//unmount();
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Mounter::wait()
|
|
|
|
{
|
|
|
|
if (m_started)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Starting loop to wait for mount";
|
2014-01-22 21:58:53 +00:00
|
|
|
|
|
|
|
MountLoop loop;
|
2016-11-26 14:12:38 +00:00
|
|
|
connect(this, &Mounter::mounted, &loop, &MountLoop::successed);
|
|
|
|
connect(this, &Mounter::failed, &loop, &MountLoop::failed);
|
2014-01-22 21:58:53 +00:00
|
|
|
return loop.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mounter::onPakcageReceived(const NetworkPackage& np)
|
|
|
|
{
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.get<bool>(QStringLiteral("stop"), false))
|
2014-01-22 21:58:53 +00:00
|
|
|
{
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "SFTP server stopped";
|
2014-01-22 21:58:53 +00:00
|
|
|
unmount();
|
|
|
|
return;
|
|
|
|
}
|
2014-02-14 19:12:40 +00:00
|
|
|
|
|
|
|
//This is the previous code, to access sftp server using KIO. Now we are
|
|
|
|
//using the external binary sshfs, and accessing it as a local filesystem.
|
|
|
|
/*
|
2014-09-21 21:23:31 +01:00
|
|
|
* QUrl url;
|
|
|
|
* url.setScheme("sftp");
|
2014-02-14 19:12:40 +00:00
|
|
|
* url.setHost(np.get<QString>("ip"));
|
|
|
|
* url.setPort(np.get<QString>("port").toInt());
|
2014-09-21 21:23:31 +01:00
|
|
|
* url.setUserName(np.get<QString>("user"));
|
|
|
|
* url.setPassword(np.get<QString>("password"));
|
2014-02-14 19:12:40 +00:00
|
|
|
* url.setPath(np.get<QString>("path"));
|
|
|
|
* new KRun(url, 0);
|
|
|
|
* Q_EMIT mounted();
|
|
|
|
*/
|
|
|
|
|
2015-10-18 01:31:17 +01:00
|
|
|
unmount();
|
|
|
|
|
|
|
|
m_proc = new KProcess(this);
|
2014-01-22 21:58:53 +00:00
|
|
|
m_proc->setOutputChannelMode(KProcess::MergedChannels);
|
|
|
|
|
2016-11-26 14:12:38 +00:00
|
|
|
connect(m_proc, &QProcess::started, this, &Mounter::onStarted);
|
2015-10-18 01:31:17 +01:00
|
|
|
connect(m_proc, SIGNAL(error(QProcess::ProcessError)), SLOT(onError(QProcess::ProcessError)));
|
|
|
|
connect(m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(onFinished(int,QProcess::ExitStatus)));
|
2014-02-14 16:12:15 +00:00
|
|
|
|
2015-06-21 20:32:18 +01:00
|
|
|
QDir().mkpath(m_mountPoint);
|
2014-02-14 19:12:40 +00:00
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
const QString program = QStringLiteral("sshfs");
|
2014-10-10 23:01:21 +01:00
|
|
|
|
|
|
|
QString path;
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("multiPaths"))) path = '/';
|
|
|
|
else path = np.get<QString>(QStringLiteral("path"));
|
2014-10-10 23:01:21 +01:00
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
const QStringList arguments = QStringList()
|
2016-11-26 14:38:08 +00:00
|
|
|
<< QStringLiteral("%1@%2:%3")
|
|
|
|
.arg(np.get<QString>(QStringLiteral("user")))
|
|
|
|
.arg(np.get<QString>(QStringLiteral("ip")))
|
2014-10-10 23:01:21 +01:00
|
|
|
.arg(path)
|
2015-06-21 20:32:18 +01:00
|
|
|
<< m_mountPoint
|
2016-11-26 14:38:08 +00:00
|
|
|
<< QStringLiteral("-p") << np.get<QString>(QStringLiteral("port"))
|
|
|
|
<< QStringLiteral("-f")
|
|
|
|
<< QStringLiteral("-F") << QStringLiteral("/dev/null") //Do not use ~/.ssh/config
|
|
|
|
<< QStringLiteral("-o") << "IdentityFile=" + KdeConnectConfig::instance()->privateKeyPath()
|
|
|
|
<< QStringLiteral("-o") << QStringLiteral("StrictHostKeyChecking=no") //Do not ask for confirmation because it is not a known host
|
|
|
|
<< QStringLiteral("-o") << QStringLiteral("UserKnownHostsFile=/dev/null") //Prevent storing as a known host
|
|
|
|
<< QStringLiteral("-o") << QStringLiteral("HostKeyAlgorithms=ssh-dss") //https://bugs.kde.org/show_bug.cgi?id=351725
|
|
|
|
<< QStringLiteral("-o") << QStringLiteral("password_stdin")
|
2016-02-17 12:51:28 +00:00
|
|
|
;
|
2015-08-25 05:27:24 +01:00
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
m_proc->setProgram(program, arguments);
|
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Starting process: " << m_proc->program().join(QStringLiteral(" "));
|
2014-01-22 21:58:53 +00:00
|
|
|
m_proc->start();
|
2016-02-17 12:51:28 +00:00
|
|
|
|
|
|
|
//qCDebug(KDECONNECT_PLUGIN_SFTP) << "Passing password: " << np.get<QString>("password").toLatin1();
|
2016-11-26 14:38:08 +00:00
|
|
|
m_proc->write(np.get<QString>(QStringLiteral("password")).toLatin1());
|
2016-02-17 12:51:28 +00:00
|
|
|
m_proc->write("\n");
|
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mounter::onStarted()
|
|
|
|
{
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Process started";
|
2014-01-22 21:58:53 +00:00
|
|
|
m_started = true;
|
|
|
|
Q_EMIT mounted();
|
2015-10-18 01:31:17 +01:00
|
|
|
|
|
|
|
//m_proc->setStandardOutputFile("/tmp/kdeconnect-sftp.out");
|
|
|
|
//m_proc->setStandardErrorFile("/tmp/kdeconnect-sftp.err");
|
|
|
|
connect(m_proc, &KProcess::readyReadStandardError, [this]() {
|
2015-06-21 20:32:18 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "stderr: " << m_proc->readAll();
|
|
|
|
});
|
2015-10-18 01:31:17 +01:00
|
|
|
connect(m_proc, &KProcess::readyReadStandardOutput, [this]() {
|
2015-06-21 20:32:18 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "stdout:" << m_proc->readAll();
|
|
|
|
});
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mounter::onError(QProcess::ProcessError error)
|
|
|
|
{
|
|
|
|
if (error == QProcess::FailedToStart)
|
|
|
|
{
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Process failed to start";
|
2014-01-22 21:58:53 +00:00
|
|
|
m_started = false;
|
|
|
|
Q_EMIT failed(i18n("Failed to start sshfs"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mounter::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
|
|
|
{
|
|
|
|
if (exitStatus == QProcess::NormalExit)
|
|
|
|
{
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Process finished (exit code: " << exitCode << ")";
|
2015-03-09 05:12:55 +00:00
|
|
|
Q_EMIT unmounted();
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-21 20:32:18 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Process failed (exit code:" << exitCode << ")";
|
2014-01-22 21:58:53 +00:00
|
|
|
Q_EMIT failed(i18n("Error when accessing to filesystem"));
|
|
|
|
}
|
|
|
|
|
2015-10-18 01:31:17 +01:00
|
|
|
unmount();
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mounter::onMountTimeout()
|
|
|
|
{
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Timeout: device not responding";
|
2014-01-22 21:58:53 +00:00
|
|
|
Q_EMIT failed(i18n("Failed to mount filesystem: device not responding"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mounter::start()
|
|
|
|
{
|
2016-06-21 19:07:12 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_SFTP_REQUEST, {{"startBrowsing", true}});
|
2014-06-14 19:35:00 +01:00
|
|
|
m_sftp->sendPackage(np);
|
2014-01-22 21:58:53 +00:00
|
|
|
|
|
|
|
m_connectTimer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mounter::unmount()
|
|
|
|
{
|
2015-10-18 01:31:17 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Unmount" << m_proc;
|
2014-01-22 21:58:53 +00:00
|
|
|
if (m_proc)
|
|
|
|
{
|
2015-10-18 01:31:17 +01:00
|
|
|
auto toDestroy = m_proc;
|
|
|
|
m_proc = nullptr; //So we don't reenter this code path when onFinished gets called
|
|
|
|
toDestroy->kill();
|
|
|
|
delete toDestroy;
|
|
|
|
//Free mount point (won't always succeed if the path is in use)
|
2016-11-26 14:38:08 +00:00
|
|
|
KProcess::execute(QStringList() << QStringLiteral("fusermount") << QStringLiteral("-u") << m_mountPoint, 10000);
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
2015-10-18 01:31:17 +01:00
|
|
|
m_started = false;
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
|
|
|
|