2014-01-22 21:58:53 +00:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2014 Samoilenko Yuri <kinnalru@gmail.com>
|
2014-01-22 21:58:53 +00:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2014-01-22 21:58:53 +00:00
|
|
|
*/
|
|
|
|
|
2015-06-21 20:32:18 +01:00
|
|
|
#include "mounter.h"
|
|
|
|
|
2014-09-21 23:45:59 +01:00
|
|
|
#include <QDebug>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <unistd.h>
|
2014-01-22 21:58:53 +00:00
|
|
|
|
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2017-09-05 21:22:55 +01:00
|
|
|
#include "config-sftp.h"
|
2016-07-05 13:27:53 +01:00
|
|
|
#include "kdeconnectconfig.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "mountloop.h"
|
|
|
|
#include "plugin_sftp_debug.h"
|
2014-01-22 21:58:53 +00:00
|
|
|
|
2022-09-10 22:23:52 +01: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_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";
|
2017-03-15 10:34:12 +00:00
|
|
|
unmount(false);
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Mounter::wait()
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (m_started) {
|
2014-01-22 21:58:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-03-15 10:34:12 +00:00
|
|
|
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Starting loop to wait for mount";
|
2017-03-15 10:34:12 +00:00
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
MountLoop loop;
|
2020-08-06 12:51:31 +01:00
|
|
|
connect(this, &Mounter::mounted, &loop, &MountLoop::succeeded);
|
2016-11-26 14:12:38 +00:00
|
|
|
connect(this, &Mounter::failed, &loop, &MountLoop::failed);
|
2014-01-22 21:58:53 +00:00
|
|
|
return loop.exec();
|
|
|
|
}
|
|
|
|
|
2023-08-24 17:14:59 +01:00
|
|
|
void Mounter::onPacketReceived(const NetworkPacket &np)
|
2014-01-22 21:58:53 +00:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (np.get<bool>(QStringLiteral("stop"), false)) {
|
2014-09-21 23:45:59 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "SFTP server stopped";
|
2017-03-15 10:34:12 +00:00
|
|
|
unmount(false);
|
2014-01-22 21:58:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-08 16:09:52 +01:00
|
|
|
|
2019-06-10 15:40:28 +01:00
|
|
|
if (np.has(QStringLiteral("errorMessage"))) {
|
|
|
|
Q_EMIT failed(np.get<QString>(QStringLiteral("errorMessage")));
|
2019-01-22 17:42:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-02-14 19:12:40 +00:00
|
|
|
|
2022-09-10 22:23:52 +01: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.
|
|
|
|
/*
|
|
|
|
* QUrl url;
|
|
|
|
* url.setScheme("sftp");
|
|
|
|
* url.setHost(np.get<QString>("ip"));
|
|
|
|
* url.setPort(np.get<QString>("port").toInt());
|
|
|
|
* url.setUserName(np.get<QString>("user"));
|
|
|
|
* url.setPassword(np.get<QString>("password"));
|
|
|
|
* url.setPath(np.get<QString>("path"));
|
|
|
|
* new KRun(url, 0);
|
|
|
|
* Q_EMIT mounted();
|
|
|
|
*/
|
2014-02-14 19:12:40 +00:00
|
|
|
|
2017-03-15 10:34:12 +00:00
|
|
|
unmount(false);
|
2015-10-18 01:31:17 +01:00
|
|
|
|
2017-03-15 10:34:12 +00:00
|
|
|
m_proc = new KProcess();
|
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);
|
2019-04-28 15:35:44 +01:00
|
|
|
connect(m_proc, &QProcess::errorOccurred, this, &Mounter::onError);
|
2023-07-21 10:49:04 +01:00
|
|
|
connect(m_proc, &QProcess::finished, this, &Mounter::onFinished);
|
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;
|
2022-09-10 22:23:52 +01:00
|
|
|
if (np.has(QStringLiteral("multiPaths")))
|
|
|
|
path = QStringLiteral("/");
|
|
|
|
else
|
|
|
|
path = np.get<QString>(QStringLiteral("path"));
|
2014-10-10 23:01:21 +01:00
|
|
|
|
2017-07-22 10:13:21 +01:00
|
|
|
QHostAddress addr = m_sftp->device()->getLocalIpAddress();
|
|
|
|
if (addr == QHostAddress::Null) {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Device doesn't have a LanDeviceLink, unable to get IP address";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString ip = addr.toString();
|
2021-03-31 03:13:24 +01:00
|
|
|
if (addr.protocol() == QAbstractSocket::IPv6Protocol) {
|
|
|
|
ip.prepend(QLatin1Char('['));
|
|
|
|
ip.append(QLatin1Char(']'));
|
|
|
|
}
|
2017-07-22 10:13:21 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
const QStringList arguments =
|
|
|
|
QStringList() << QStringLiteral("%1@%2:%3")
|
|
|
|
.arg(np.get<QString>(QStringLiteral("user")),
|
|
|
|
ip,
|
|
|
|
path)
|
|
|
|
<< m_mountPoint << QStringLiteral("-p") << np.get<QString>(QStringLiteral("port"))
|
|
|
|
<< QStringLiteral("-s") // This fixes a bug where file chunks are sent out of order and get corrupted on reception
|
|
|
|
<< QStringLiteral("-f") << QStringLiteral("-F") << QStringLiteral("/dev/null") // Do not use ~/.ssh/config
|
|
|
|
<< QStringLiteral("-o") << QStringLiteral("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\\,ssh-rsa") // https://bugs.kde.org/show_bug.cgi?id=351725
|
2022-12-12 20:21:46 +00:00
|
|
|
<< QStringLiteral("-o") << QStringLiteral("PubkeyAcceptedKeyTypes=+ssh-rsa") // https://bugs.kde.org/show_bug.cgi?id=443155
|
2022-09-10 22:23:52 +01:00
|
|
|
<< QStringLiteral("-o") << QStringLiteral("uid=") + QString::number(getuid()) << QStringLiteral("-o")
|
|
|
|
<< QStringLiteral("gid=") + QString::number(getgid()) << QStringLiteral("-o") << QStringLiteral("reconnect") << QStringLiteral("-o")
|
|
|
|
<< QStringLiteral("ServerAliveInterval=30") << QStringLiteral("-o") << QStringLiteral("password_stdin");
|
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
|
|
|
|
2022-09-10 22:23:52 +01: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
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// m_proc->setStandardOutputFile("/tmp/kdeconnect-sftp.out");
|
|
|
|
// m_proc->setStandardErrorFile("/tmp/kdeconnect-sftp.err");
|
2017-03-15 10:34:12 +00:00
|
|
|
|
|
|
|
auto proc = m_proc;
|
2019-02-28 13:18:47 +00:00
|
|
|
connect(m_proc, &KProcess::readyReadStandardError, this, [proc]() {
|
2017-03-15 10:34:12 +00:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "stderr: " << proc->readAll();
|
2015-06-21 20:32:18 +01:00
|
|
|
});
|
2019-02-28 13:18:47 +00:00
|
|
|
connect(m_proc, &KProcess::readyReadStandardOutput, this, [proc]() {
|
2017-03-15 10:34:12 +00:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "stdout:" << proc->readAll();
|
2015-06-21 20:32:18 +01:00
|
|
|
});
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mounter::onError(QProcess::ProcessError error)
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (error == QProcess::FailedToStart) {
|
2019-04-25 19:47:50 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "sshfs process failed to start";
|
2014-01-22 21:58:53 +00:00
|
|
|
m_started = false;
|
|
|
|
Q_EMIT failed(i18n("Failed to start sshfs"));
|
2022-09-10 22:23:52 +01:00
|
|
|
} else if (error == QProcess::ProcessError::Crashed) {
|
2019-04-25 19:47:50 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "sshfs process crashed";
|
|
|
|
m_started = false;
|
|
|
|
Q_EMIT failed(i18n("sshfs process crashed"));
|
|
|
|
} else {
|
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "sshfs process error" << error;
|
|
|
|
m_started = false;
|
|
|
|
Q_EMIT failed(i18n("Unknown error in sshfs"));
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mounter::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (exitStatus == QProcess::NormalExit && exitCode == 0) {
|
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();
|
2022-09-10 22:23:52 +01:00
|
|
|
} else {
|
2015-06-21 20:32:18 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Process failed (exit code:" << exitCode << ")";
|
2019-04-28 20:28:06 +01:00
|
|
|
Q_EMIT failed(i18n("Error when accessing filesystem. sshfs finished with exit code %0").arg(exitCode));
|
2014-01-22 21:58:53 +00:00
|
|
|
}
|
2017-03-15 10:34:12 +00:00
|
|
|
|
|
|
|
unmount(true);
|
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()
|
|
|
|
{
|
2019-06-10 15:40:28 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_SFTP_REQUEST, {{QStringLiteral("startBrowsing"), true}});
|
2018-03-04 19:48:51 +00:00
|
|
|
m_sftp->sendPacket(np);
|
2017-03-15 10:34:12 +00:00
|
|
|
|
2014-01-22 21:58:53 +00:00
|
|
|
m_connectTimer.start();
|
|
|
|
}
|
|
|
|
|
2017-03-15 10:34:12 +00:00
|
|
|
void Mounter::unmount(bool finished)
|
2014-01-22 21:58:53 +00:00
|
|
|
{
|
2015-10-18 01:31:17 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Unmount" << m_proc;
|
2022-09-10 22:23:52 +01:00
|
|
|
if (m_proc) {
|
|
|
|
if (!finished) {
|
|
|
|
// Process is still running, we want to stop it
|
|
|
|
// But when the finished signal come, we might have already gone.
|
|
|
|
// Disconnect everything.
|
2017-03-15 10:34:12 +00:00
|
|
|
m_proc->disconnect();
|
|
|
|
m_proc->kill();
|
|
|
|
|
|
|
|
auto proc = m_proc;
|
|
|
|
m_proc = nullptr;
|
2023-07-21 10:49:04 +01:00
|
|
|
connect(proc, &QProcess::finished, [proc]() {
|
2022-09-10 22:23:52 +01:00
|
|
|
qCDebug(KDECONNECT_PLUGIN_SFTP) << "Free" << proc;
|
|
|
|
proc->deleteLater();
|
2017-03-15 10:34:12 +00:00
|
|
|
});
|
|
|
|
Q_EMIT unmounted();
|
2022-09-10 22:23:52 +01:00
|
|
|
} else
|
2017-03-15 10:34:12 +00:00
|
|
|
m_proc->deleteLater();
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Free mount point (won't always succeed if the path is in use)
|
2017-09-05 21:22:55 +01:00
|
|
|
#if defined(HAVE_FUSERMOUNT)
|
2023-07-25 20:45:36 +01:00
|
|
|
KProcess::execute(QStringList{QStringLiteral("fusermount"), QStringLiteral("-u"), m_mountPoint}, 10000);
|
2017-09-05 21:22:55 +01:00
|
|
|
#else
|
2023-07-25 20:45:36 +01:00
|
|
|
KProcess::execute(QStringList{QStringLiteral("umount"), m_mountPoint}, 10000);
|
2017-09-05 21:22:55 +01:00
|
|
|
#endif
|
2017-03-15 10:34:12 +00:00
|
|
|
m_proc = nullptr;
|
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
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_mounter.cpp"
|