2017-12-18 00:38:43 +03:00
|
|
|
#include "propagateuploadencrypted.h"
|
|
|
|
#include "clientsideencryptionjobs.h"
|
|
|
|
#include "networkjobs.h"
|
|
|
|
#include "clientsideencryption.h"
|
|
|
|
#include "account.h"
|
|
|
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QUrl>
|
2017-12-21 00:32:38 +03:00
|
|
|
#include <QFile>
|
|
|
|
#include <QTemporaryFile>
|
2017-12-29 18:54:50 +03:00
|
|
|
#include <QLoggingCategory>
|
2018-01-23 23:31:00 +03:00
|
|
|
#include <QMimeDatabase>
|
2017-12-18 00:38:43 +03:00
|
|
|
|
|
|
|
namespace OCC {
|
|
|
|
|
2018-01-21 23:59:19 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcPropagateUploadEncrypted, "nextcloud.sync.propagator.upload.encrypted", QtInfoMsg)
|
2017-12-29 18:54:50 +03:00
|
|
|
|
2020-06-30 12:21:02 +03:00
|
|
|
PropagateUploadEncrypted::PropagateUploadEncrypted(OwncloudPropagator *propagator, const QString &remoteParentPath, SyncFileItemPtr item, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, _propagator(propagator)
|
2020-06-23 19:13:20 +03:00
|
|
|
, _remoteParentPath(remoteParentPath)
|
|
|
|
, _item(item)
|
|
|
|
, _metadata(nullptr)
|
2017-12-18 00:38:43 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateUploadEncrypted::start()
|
|
|
|
{
|
2020-06-29 19:16:59 +03:00
|
|
|
const auto rootPath = [=]() {
|
2020-09-22 12:47:40 +03:00
|
|
|
const auto result = _propagator->remotePath();
|
2020-06-29 19:16:59 +03:00
|
|
|
if (result.startsWith('/')) {
|
|
|
|
return result.mid(1);
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
const auto absoluteRemoteParentPath = [=]{
|
|
|
|
auto path = QString(rootPath + _remoteParentPath);
|
|
|
|
if (path.endsWith('/')) {
|
|
|
|
path.chop(1);
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
2020-12-08 18:20:29 +03:00
|
|
|
/* If the file is in a encrypted folder, which we know, we wouldn't be here otherwise,
|
|
|
|
* we need to do the long road:
|
|
|
|
* find the ID of the folder.
|
|
|
|
* lock the folder using it's id.
|
|
|
|
* download the metadata
|
|
|
|
* update the metadata
|
|
|
|
* upload the file
|
|
|
|
* upload the metadata
|
|
|
|
* unlock the folder.
|
|
|
|
*/
|
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Folder is encrypted, let's get the Id from it.";
|
|
|
|
auto job = new LsColJob(_propagator->account(), absoluteRemoteParentPath, this);
|
|
|
|
job->setProperties({"resourcetype", "http://owncloud.org/ns:fileid"});
|
|
|
|
connect(job, &LsColJob::directoryListingSubfolders, this, &PropagateUploadEncrypted::slotFolderEncryptedIdReceived);
|
|
|
|
connect(job, &LsColJob::finishedWithError, this, &PropagateUploadEncrypted::slotFolderEncryptedIdError);
|
|
|
|
job->start();
|
2017-12-18 00:38:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We try to lock a folder, if it's locked we try again in one second.
|
|
|
|
* if it's still locked we try again in one second. looping untill one minute.
|
|
|
|
* -> fail.
|
|
|
|
* the 'loop': /
|
|
|
|
* slotFolderEncryptedIdReceived -> slotTryLock -> lockError -> stillTime? -> slotTryLock
|
|
|
|
* \
|
|
|
|
* -> success.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void PropagateUploadEncrypted::slotFolderEncryptedIdReceived(const QStringList &list)
|
|
|
|
{
|
2018-01-21 23:59:19 +03:00
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Received id of folder, trying to lock it so we can prepare the metadata";
|
2017-12-18 00:38:43 +03:00
|
|
|
auto job = qobject_cast<LsColJob *>(sender());
|
|
|
|
const auto& folderInfo = job->_folderInfos.value(list.first());
|
|
|
|
_folderLockFirstTry.start();
|
|
|
|
slotTryLock(folderInfo.fileId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateUploadEncrypted::slotTryLock(const QByteArray& fileId)
|
|
|
|
{
|
|
|
|
auto *lockJob = new LockEncryptFolderApiJob(_propagator->account(), fileId, this);
|
|
|
|
connect(lockJob, &LockEncryptFolderApiJob::success, this, &PropagateUploadEncrypted::slotFolderLockedSuccessfully);
|
|
|
|
connect(lockJob, &LockEncryptFolderApiJob::error, this, &PropagateUploadEncrypted::slotFolderLockedError);
|
|
|
|
lockJob->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateUploadEncrypted::slotFolderLockedSuccessfully(const QByteArray& fileId, const QByteArray& token)
|
|
|
|
{
|
2018-01-21 23:59:19 +03:00
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Folder" << fileId << "Locked Successfully for Upload, Fetching Metadata";
|
2017-12-18 00:38:43 +03:00
|
|
|
// Should I use a mutex here?
|
|
|
|
_currentLockingInProgress = true;
|
|
|
|
_folderToken = token;
|
|
|
|
_folderId = fileId;
|
2021-01-21 18:07:30 +03:00
|
|
|
_isFolderLocked = true;
|
2017-12-18 00:38:43 +03:00
|
|
|
|
|
|
|
auto job = new GetMetadataApiJob(_propagator->account(), _folderId);
|
|
|
|
connect(job, &GetMetadataApiJob::jsonReceived,
|
2018-03-25 22:54:08 +03:00
|
|
|
this, &PropagateUploadEncrypted::slotFolderEncryptedMetadataReceived);
|
|
|
|
connect(job, &GetMetadataApiJob::error,
|
|
|
|
this, &PropagateUploadEncrypted::slotFolderEncryptedMetadataError);
|
|
|
|
|
2017-12-18 00:38:43 +03:00
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
2018-03-25 22:54:08 +03:00
|
|
|
void PropagateUploadEncrypted::slotFolderEncryptedMetadataError(const QByteArray& fileId, int httpReturnCode)
|
|
|
|
{
|
2018-07-02 13:45:44 +03:00
|
|
|
Q_UNUSED(fileId);
|
|
|
|
Q_UNUSED(httpReturnCode);
|
2020-07-09 13:17:42 +03:00
|
|
|
qCDebug(lcPropagateUploadEncrypted()) << "Error Getting the encrypted metadata. Pretend we got empty metadata.";
|
|
|
|
FolderMetadata emptyMetadata(_propagator->account());
|
|
|
|
emptyMetadata.encryptedMetadata();
|
|
|
|
auto json = QJsonDocument::fromJson(emptyMetadata.encryptedMetadata());
|
|
|
|
slotFolderEncryptedMetadataReceived(json, httpReturnCode);
|
2018-03-25 22:54:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateUploadEncrypted::slotFolderEncryptedMetadataReceived(const QJsonDocument &json, int statusCode)
|
2017-12-18 00:38:43 +03:00
|
|
|
{
|
2018-01-21 23:59:19 +03:00
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Metadata Received, Preparing it for the new file." << json.toVariant();
|
2017-12-18 00:38:43 +03:00
|
|
|
|
2017-12-21 00:32:38 +03:00
|
|
|
// Encrypt File!
|
2018-01-30 00:04:50 +03:00
|
|
|
_metadata = new FolderMetadata(_propagator->account(), json.toJson(QJsonDocument::Compact), statusCode);
|
2017-12-18 00:38:43 +03:00
|
|
|
|
2020-09-22 12:47:40 +03:00
|
|
|
QFileInfo info(_propagator->fullLocalPath(_item->_file));
|
2018-03-29 17:09:04 +03:00
|
|
|
const QString fileName = info.fileName();
|
2018-01-23 13:36:15 +03:00
|
|
|
|
|
|
|
// Find existing metadata for this file
|
2018-03-29 17:09:04 +03:00
|
|
|
bool found = false;
|
2018-01-23 13:36:15 +03:00
|
|
|
EncryptedFile encryptedFile;
|
2018-04-04 16:13:46 +03:00
|
|
|
const QVector<EncryptedFile> files = _metadata->files();
|
2018-04-04 01:08:34 +03:00
|
|
|
|
2018-04-04 16:13:46 +03:00
|
|
|
for(const EncryptedFile &file : files) {
|
2018-03-29 17:09:04 +03:00
|
|
|
if (file.originalFilename == fileName) {
|
2018-01-23 13:36:15 +03:00
|
|
|
encryptedFile = file;
|
2018-03-29 17:09:04 +03:00
|
|
|
found = true;
|
2018-01-23 13:36:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-12 14:50:51 +03:00
|
|
|
|
2018-03-29 17:09:04 +03:00
|
|
|
// New encrypted file so set it all up!
|
|
|
|
if (!found) {
|
|
|
|
encryptedFile.encryptionKey = EncryptionHelper::generateRandom(16);
|
2018-02-09 13:40:06 +03:00
|
|
|
encryptedFile.encryptedFilename = EncryptionHelper::generateRandomFilename();
|
2018-03-29 17:09:04 +03:00
|
|
|
encryptedFile.initializationVector = EncryptionHelper::generateRandom(16);
|
2018-01-23 13:36:15 +03:00
|
|
|
encryptedFile.fileVersion = 1;
|
|
|
|
encryptedFile.metadataKey = 1;
|
2018-03-29 17:09:04 +03:00
|
|
|
encryptedFile.originalFilename = fileName;
|
|
|
|
|
2018-06-14 22:26:07 +03:00
|
|
|
QMimeDatabase mdb;
|
2018-01-23 23:31:00 +03:00
|
|
|
encryptedFile.mimetype = mdb.mimeTypeForFile(info).name().toLocal8Bit();
|
2020-08-31 13:05:05 +03:00
|
|
|
|
|
|
|
// Other clients expect "httpd/unix-directory" instead of "inode/directory"
|
|
|
|
// Doesn't matter much for us since we don't do much about that mimetype anyway
|
|
|
|
if (encryptedFile.mimetype == QByteArrayLiteral("inode/directory")) {
|
|
|
|
encryptedFile.mimetype = QByteArrayLiteral("httpd/unix-directory");
|
|
|
|
}
|
2018-01-23 13:36:15 +03:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:13:20 +03:00
|
|
|
_item->_encryptedFileName = _remoteParentPath + QLatin1Char('/') + encryptedFile.encryptedFilename;
|
2020-12-01 14:45:01 +03:00
|
|
|
_item->_isEncrypted = true;
|
2018-01-23 13:36:15 +03:00
|
|
|
|
2018-01-21 23:59:19 +03:00
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Creating the encrypted file.";
|
2017-12-21 02:00:27 +03:00
|
|
|
|
2020-06-17 20:18:04 +03:00
|
|
|
if (info.isDir()) {
|
|
|
|
_completeFileName = encryptedFile.encryptedFilename;
|
|
|
|
} else {
|
|
|
|
QFile input(info.absoluteFilePath());
|
|
|
|
QFile output(QDir::tempPath() + QDir::separator() + encryptedFile.encryptedFilename);
|
|
|
|
|
|
|
|
QByteArray tag;
|
|
|
|
bool encryptionResult = EncryptionHelper::fileEncryption(
|
|
|
|
encryptedFile.encryptionKey,
|
|
|
|
encryptedFile.initializationVector,
|
|
|
|
&input, &output, tag);
|
|
|
|
|
|
|
|
if (!encryptionResult) {
|
|
|
|
qCDebug(lcPropagateUploadEncrypted()) << "There was an error encrypting the file, aborting upload.";
|
2021-01-21 18:07:30 +03:00
|
|
|
connect(this, &PropagateUploadEncrypted::folderUnlocked, this, &PropagateUploadEncrypted::error);
|
|
|
|
unlockFolder();
|
2020-06-17 20:18:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedFile.authenticationTag = tag;
|
|
|
|
_completeFileName = output.fileName();
|
2018-03-26 22:44:46 +03:00
|
|
|
}
|
2018-01-21 23:59:19 +03:00
|
|
|
|
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Creating the metadata for the encrypted file.";
|
2018-01-22 17:24:54 +03:00
|
|
|
|
2017-12-21 01:30:51 +03:00
|
|
|
_metadata->addEncryptedFile(encryptedFile);
|
2017-12-23 01:05:39 +03:00
|
|
|
_encryptedFile = encryptedFile;
|
2017-12-21 00:32:38 +03:00
|
|
|
|
2018-01-21 23:59:19 +03:00
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Metadata created, sending to the server.";
|
2018-01-30 00:04:50 +03:00
|
|
|
|
|
|
|
if (statusCode == 404) {
|
|
|
|
auto job = new StoreMetaDataApiJob(_propagator->account(),
|
|
|
|
_folderId,
|
|
|
|
_metadata->encryptedMetadata());
|
|
|
|
connect(job, &StoreMetaDataApiJob::success, this, &PropagateUploadEncrypted::slotUpdateMetadataSuccess);
|
|
|
|
connect(job, &StoreMetaDataApiJob::error, this, &PropagateUploadEncrypted::slotUpdateMetadataError);
|
|
|
|
job->start();
|
|
|
|
} else {
|
|
|
|
auto job = new UpdateMetadataApiJob(_propagator->account(),
|
2017-12-23 01:05:39 +03:00
|
|
|
_folderId,
|
|
|
|
_metadata->encryptedMetadata(),
|
|
|
|
_folderToken);
|
|
|
|
|
2018-01-30 00:04:50 +03:00
|
|
|
connect(job, &UpdateMetadataApiJob::success, this, &PropagateUploadEncrypted::slotUpdateMetadataSuccess);
|
|
|
|
connect(job, &UpdateMetadataApiJob::error, this, &PropagateUploadEncrypted::slotUpdateMetadataError);
|
|
|
|
job->start();
|
|
|
|
}
|
2017-12-23 01:05:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateUploadEncrypted::slotUpdateMetadataSuccess(const QByteArray& fileId)
|
|
|
|
{
|
2018-07-02 13:45:44 +03:00
|
|
|
Q_UNUSED(fileId);
|
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Uploading of the metadata success, Encrypting the file";
|
|
|
|
QFileInfo outputInfo(_completeFileName);
|
|
|
|
|
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Encrypted Info:" << outputInfo.path() << outputInfo.fileName() << outputInfo.size();
|
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Finalizing the upload part, now the actuall uploader will take over";
|
|
|
|
emit finalized(outputInfo.path() + QLatin1Char('/') + outputInfo.fileName(),
|
2020-06-23 19:13:20 +03:00
|
|
|
_remoteParentPath + QLatin1Char('/') + outputInfo.fileName(),
|
2018-07-02 13:45:44 +03:00
|
|
|
outputInfo.size());
|
2017-12-23 01:05:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateUploadEncrypted::slotUpdateMetadataError(const QByteArray& fileId, int httpErrorResponse)
|
|
|
|
{
|
2018-01-21 23:59:19 +03:00
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Update metadata error for folder" << fileId << "with error" << httpErrorResponse;
|
2018-03-25 22:54:08 +03:00
|
|
|
qCDebug(lcPropagateUploadEncrypted()) << "Unlocking the folder.";
|
2021-01-21 18:07:30 +03:00
|
|
|
connect(this, &PropagateUploadEncrypted::folderUnlocked, this, &PropagateUploadEncrypted::error);
|
|
|
|
unlockFolder();
|
2017-12-18 00:38:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateUploadEncrypted::slotFolderLockedError(const QByteArray& fileId, int httpErrorCode)
|
|
|
|
{
|
2018-07-02 13:45:44 +03:00
|
|
|
Q_UNUSED(httpErrorCode);
|
|
|
|
/* try to call the lock from 5 to 5 seconds
|
|
|
|
* and fail if it's more than 5 minutes. */
|
|
|
|
QTimer::singleShot(5000, this, [this, fileId]{
|
|
|
|
if (!_currentLockingInProgress) {
|
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Error locking the folder while no other update is locking it up.";
|
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Perhaps another client locked it.";
|
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Abort";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perhaps I should remove the elapsed timer if the lock is from this client?
|
|
|
|
if (_folderLockFirstTry.elapsed() > /* five minutes */ 1000 * 60 * 5 ) {
|
2020-05-03 23:54:50 +03:00
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "One minute passed, ignoring more attempts to lock the folder.";
|
2018-07-02 13:45:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
slotTryLock(fileId);
|
|
|
|
});
|
|
|
|
|
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Folder" << fileId << "Coundn't be locked.";
|
2017-12-18 00:38:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateUploadEncrypted::slotFolderEncryptedIdError(QNetworkReply *r)
|
|
|
|
{
|
2018-07-02 13:45:44 +03:00
|
|
|
Q_UNUSED(r);
|
|
|
|
qCDebug(lcPropagateUploadEncrypted) << "Error retrieving the Id of the encrypted folder.";
|
2017-12-18 00:38:43 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 18:07:30 +03:00
|
|
|
void PropagateUploadEncrypted::unlockFolder()
|
2018-03-25 22:54:08 +03:00
|
|
|
{
|
2021-01-21 18:07:30 +03:00
|
|
|
ASSERT(!_isUnlockRunning);
|
2021-01-21 14:19:41 +03:00
|
|
|
|
2021-01-21 18:07:30 +03:00
|
|
|
if (_isUnlockRunning) {
|
|
|
|
qWarning() << "Double-call to unlockFolder.";
|
|
|
|
return;
|
2021-01-21 14:19:41 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 18:07:30 +03:00
|
|
|
_isUnlockRunning = true;
|
|
|
|
|
2018-03-25 22:54:08 +03:00
|
|
|
qDebug() << "Calling Unlock";
|
|
|
|
auto *unlockJob = new UnlockEncryptFolderApiJob(_propagator->account(),
|
|
|
|
_folderId, _folderToken, this);
|
|
|
|
|
2021-01-21 18:07:30 +03:00
|
|
|
connect(unlockJob, &UnlockEncryptFolderApiJob::success, [this](const QByteArray &folderId) {
|
2021-01-21 14:19:41 +03:00
|
|
|
qDebug() << "Successfully Unlocked";
|
|
|
|
_folderToken = "";
|
|
|
|
_folderId = "";
|
2021-01-21 18:07:30 +03:00
|
|
|
_isFolderLocked = false;
|
2021-01-21 14:19:41 +03:00
|
|
|
|
|
|
|
emit folderUnlocked(folderId, 200);
|
|
|
|
_isUnlockRunning = false;
|
|
|
|
});
|
2021-01-21 18:07:30 +03:00
|
|
|
connect(unlockJob, &UnlockEncryptFolderApiJob::error, [this](const QByteArray &folderId, int httpStatus) {
|
2021-01-21 14:19:41 +03:00
|
|
|
qDebug() << "Unlock Error";
|
|
|
|
|
|
|
|
emit folderUnlocked(folderId, httpStatus);
|
|
|
|
_isUnlockRunning = false;
|
|
|
|
});
|
2018-03-25 22:54:08 +03:00
|
|
|
unlockJob->start();
|
|
|
|
}
|
|
|
|
|
2017-12-18 00:38:43 +03:00
|
|
|
} // namespace OCC
|