2021-10-21 14:29:15 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "QuarantineUtil.hpp"
|
|
|
|
|
|
|
|
#include <Poco/Path.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include "ClientSession.hpp"
|
2021-11-18 06:08:14 -06:00
|
|
|
#include "COOLWSD.hpp"
|
2021-10-21 14:29:15 -05:00
|
|
|
#include "DocumentBroker.hpp"
|
2023-05-10 07:19:59 -05:00
|
|
|
#include "FileUtil.hpp"
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-11 05:48:40 -05:00
|
|
|
#include <chrono>
|
2021-10-21 14:29:15 -05:00
|
|
|
#include <common/Common.hpp>
|
|
|
|
#include <common/StringVector.hpp>
|
|
|
|
#include <common/Log.hpp>
|
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
std::string Quarantine::QuarantinePath;
|
|
|
|
|
2023-05-11 05:48:40 -05:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string createQuarantinedFilename(DocumentBroker& docBroker)
|
|
|
|
{
|
|
|
|
std::string docKey;
|
|
|
|
Poco::URI::encode(docBroker.getDocKey(), "?#/", docKey);
|
|
|
|
return '_' + std::to_string(docBroker.getPid()) + '_' + docKey + '_';
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Quarantine::Quarantine(DocumentBroker& docBroker)
|
2023-05-11 05:58:15 -05:00
|
|
|
: _docKey(docBroker.getDocKey())
|
2023-05-11 05:48:40 -05:00
|
|
|
, _quarantinedFilenamePrefix(createQuarantinedFilename(docBroker))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
void Quarantine::initialize(const std::string& path)
|
2021-10-21 14:29:15 -05:00
|
|
|
{
|
2023-05-08 07:11:01 -05:00
|
|
|
if (!COOLWSD::getConfigValue<bool>("quarantine_files[@enable]", false) ||
|
|
|
|
!QuarantinePath.empty())
|
2023-05-08 06:57:30 -05:00
|
|
|
return;
|
2023-05-07 17:07:48 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
std::vector<std::string> files;
|
|
|
|
Poco::File(path).list(files);
|
|
|
|
COOLWSD::QuarantineMap.clear();
|
2021-11-03 02:08:22 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
std::vector<StringToken> tokens;
|
|
|
|
std::string decoded;
|
2021-11-03 02:08:22 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
std::sort(files.begin(), files.end());
|
|
|
|
for (const auto& file : files)
|
|
|
|
{
|
|
|
|
StringVector::tokenize(file.c_str(), file.size(), '_', tokens);
|
|
|
|
Poco::URI::decode(file.substr(tokens[2]._index), decoded);
|
|
|
|
COOLWSD::QuarantineMap[decoded].emplace_back(path + file);
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
tokens.clear();
|
|
|
|
decoded.clear();
|
|
|
|
}
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
QuarantinePath = path;
|
|
|
|
}
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
void Quarantine::removeQuarantine()
|
|
|
|
{
|
|
|
|
if (!isQuarantineEnabled())
|
|
|
|
return;
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
FileUtil::removeFile(QuarantinePath, true);
|
|
|
|
}
|
2023-05-07 17:07:48 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
// returns quarantine directory size in bytes
|
|
|
|
// files with hardlink count of more than 1 is not counted
|
|
|
|
// because they are originally stored in jails
|
|
|
|
std::size_t Quarantine::quarantineSize()
|
|
|
|
{
|
|
|
|
if (!isQuarantineEnabled())
|
|
|
|
return 0;
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
std::vector<std::string> files;
|
|
|
|
Poco::File(QuarantinePath).list(files);
|
|
|
|
std::size_t size = 0;
|
|
|
|
for (const auto& file : files)
|
2021-10-21 14:29:15 -05:00
|
|
|
{
|
2023-05-08 06:57:30 -05:00
|
|
|
FileUtil::Stat f(QuarantinePath + file);
|
2021-11-03 02:08:22 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
if (f.hardLinkCount() == 1)
|
|
|
|
size += f.size();
|
2021-10-21 14:29:15 -05:00
|
|
|
}
|
2023-05-08 06:57:30 -05:00
|
|
|
return size;
|
|
|
|
}
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
void Quarantine::makeQuarantineSpace()
|
|
|
|
{
|
|
|
|
if (!isQuarantineEnabled())
|
|
|
|
return;
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
std::size_t sizeLimit =
|
|
|
|
COOLWSD::getConfigValue<std::size_t>("quarantine_files.limit_dir_size_mb", 0) * 1024 * 1024;
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
std::vector<std::string> files;
|
|
|
|
Poco::File(QuarantinePath).list(files);
|
2021-11-03 02:08:22 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
std::sort(files.begin(), files.end());
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
std::size_t timeLimit = COOLWSD::getConfigValue<std::size_t>("quarantine_files.expiry_min", 30);
|
|
|
|
const auto timeNow = std::chrono::system_clock::now();
|
|
|
|
const auto ts =
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(timeNow.time_since_epoch()).count();
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
std::size_t currentSize = quarantineSize();
|
|
|
|
auto index = files.begin();
|
|
|
|
while (index != files.end() && !files.empty())
|
|
|
|
{
|
|
|
|
FileUtil::Stat file(QuarantinePath + *index);
|
|
|
|
const auto modifyTime = std::chrono::duration_cast<std::chrono::seconds>(
|
|
|
|
file.modifiedTimepoint().time_since_epoch())
|
|
|
|
.count();
|
|
|
|
bool isExpired = static_cast<std::size_t>(ts - modifyTime) > timeLimit * 60;
|
2021-11-10 14:24:01 -06:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
if ((file.hardLinkCount() == 1) && (isExpired || (currentSize >= sizeLimit)))
|
2021-10-21 14:29:15 -05:00
|
|
|
{
|
2023-05-08 06:57:30 -05:00
|
|
|
currentSize -= file.size();
|
|
|
|
FileUtil::removeFile(QuarantinePath + *index, true);
|
|
|
|
files.erase(index);
|
2021-10-21 14:29:15 -05:00
|
|
|
}
|
2023-05-08 06:57:30 -05:00
|
|
|
else
|
|
|
|
index++;
|
2021-10-21 14:29:15 -05:00
|
|
|
}
|
2023-05-08 06:57:30 -05:00
|
|
|
}
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-11 05:46:16 -05:00
|
|
|
void Quarantine::clearOldQuarantineVersions()
|
2023-05-08 06:57:30 -05:00
|
|
|
{
|
|
|
|
if (!isQuarantineEnabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::size_t maxVersionCount = std::max<size_t>(
|
|
|
|
COOLWSD::getConfigValue<std::size_t>("quarantine_files.max_versions_to_maintain", 2), 1);
|
|
|
|
std::string decoded;
|
2023-05-11 05:58:15 -05:00
|
|
|
Poco::URI::decode(_docKey, decoded);
|
2023-05-08 06:57:30 -05:00
|
|
|
while (COOLWSD::QuarantineMap[decoded].size() > maxVersionCount)
|
2021-10-21 14:29:15 -05:00
|
|
|
{
|
2023-05-08 06:57:30 -05:00
|
|
|
FileUtil::removeFile(COOLWSD::QuarantineMap[decoded][0]);
|
|
|
|
COOLWSD::QuarantineMap[decoded].erase(COOLWSD::QuarantineMap[decoded].begin());
|
2021-10-21 14:29:15 -05:00
|
|
|
}
|
2023-05-08 06:57:30 -05:00
|
|
|
}
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-10 07:19:59 -05:00
|
|
|
bool Quarantine::quarantineFile(const std::string& docPath)
|
2023-05-08 06:57:30 -05:00
|
|
|
{
|
|
|
|
if (!isQuarantineEnabled())
|
|
|
|
return false;
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
const auto timeNow = std::chrono::system_clock::now();
|
2023-05-11 05:57:31 -05:00
|
|
|
const auto ts =
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(timeNow.time_since_epoch()).count();
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-11 05:57:31 -05:00
|
|
|
const std::string linkedFilePath =
|
2023-05-11 05:58:15 -05:00
|
|
|
QuarantinePath + std::to_string(ts) + _quarantinedFilenamePrefix + _docName;
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-11 05:58:15 -05:00
|
|
|
auto& fileList = COOLWSD::QuarantineMap[_docKey];
|
2023-05-08 06:57:30 -05:00
|
|
|
if (!fileList.empty())
|
|
|
|
{
|
2023-05-10 07:19:59 -05:00
|
|
|
FileUtil::Stat sourceStat(docPath);
|
2023-05-08 06:57:30 -05:00
|
|
|
FileUtil::Stat lastFileStat(fileList[fileList.size() - 1]);
|
2021-11-23 08:13:59 -06:00
|
|
|
|
2023-05-10 07:19:59 -05:00
|
|
|
if (lastFileStat.isIdenticalTo(sourceStat))
|
2023-05-08 06:57:30 -05:00
|
|
|
{
|
2023-05-10 07:19:59 -05:00
|
|
|
LOG_INF("Quarantining of file ["
|
|
|
|
<< docPath << "] to [" << linkedFilePath
|
|
|
|
<< "] is skipped because this file version is already quarantined");
|
2023-05-08 06:57:30 -05:00
|
|
|
return false;
|
2021-11-23 08:13:59 -06:00
|
|
|
}
|
2023-05-08 06:57:30 -05:00
|
|
|
}
|
2021-11-23 08:13:59 -06:00
|
|
|
|
2023-05-08 06:57:30 -05:00
|
|
|
makeQuarantineSpace();
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-10 07:19:59 -05:00
|
|
|
if (FileUtil::linkOrCopyFile(docPath, linkedFilePath))
|
2023-05-08 06:57:30 -05:00
|
|
|
{
|
|
|
|
fileList.emplace_back(linkedFilePath);
|
2023-05-11 05:46:16 -05:00
|
|
|
clearOldQuarantineVersions();
|
2023-05-08 06:57:30 -05:00
|
|
|
makeQuarantineSpace();
|
2021-10-21 14:29:15 -05:00
|
|
|
|
2023-05-10 07:19:59 -05:00
|
|
|
LOG_INF("Quarantined [" << docPath << "] to [" << linkedFilePath << ']');
|
2023-05-08 06:57:30 -05:00
|
|
|
return true;
|
|
|
|
}
|
2023-04-21 18:28:23 -05:00
|
|
|
|
2023-05-10 07:19:59 -05:00
|
|
|
LOG_ERR("Quarantining of file [" << docPath << "] to [" << linkedFilePath << "] failed");
|
2023-05-08 06:57:30 -05:00
|
|
|
return false;
|
|
|
|
}
|
2023-04-21 18:28:23 -05:00
|
|
|
|
2023-05-08 07:11:01 -05:00
|
|
|
void Quarantine::removeQuarantinedFiles()
|
2023-05-08 06:57:30 -05:00
|
|
|
{
|
2023-05-11 05:58:15 -05:00
|
|
|
for (const auto& file : COOLWSD::QuarantineMap[_docKey])
|
2023-05-08 06:57:30 -05:00
|
|
|
{
|
|
|
|
FileUtil::removeFile(file);
|
2023-04-21 18:28:23 -05:00
|
|
|
}
|
2023-05-08 06:57:30 -05:00
|
|
|
|
2023-05-11 05:58:15 -05:00
|
|
|
COOLWSD::QuarantineMap.erase(_docKey);
|
2021-11-04 08:09:27 -05:00
|
|
|
}
|