forked from CollaboraOnline/online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuarantineUtil.cpp
197 lines (159 loc) · 6.49 KB
/
QuarantineUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* -*- 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"
#include "COOLWSD.hpp"
#include "DocumentBroker.hpp"
#include <common/Common.hpp>
#include <common/StringVector.hpp>
#include <common/Log.hpp>
#include <common/JailUtil.hpp>
namespace Quarantine
{
bool isQuarantineEnabled()
{
return COOLWSD::getConfigValue<bool>("quarantine_files[@enable]", false);
}
void createQuarantineMap()
{
if (!isQuarantineEnabled())
return;
std::vector<std::string> files;
Poco::File(COOLWSD::QuarantinePath).list(files);
COOLWSD::QuarantineMap.clear();
std::vector<StringToken> tokens;
std::string decoded;
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(COOLWSD::QuarantinePath + file);
tokens.clear();
decoded.clear();
}
}
void removeQuarantine()
{
if (!isQuarantineEnabled())
return;
FileUtil::removeFile(COOLWSD::QuarantinePath, true);
}
// 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 quarantineSize()
{
if (!isQuarantineEnabled())
return 0;
std::vector<std::string> files;
Poco::File(COOLWSD::QuarantinePath).list(files);
std::size_t size = 0;
for (const auto& file : files)
{
FileUtil::Stat f(COOLWSD::QuarantinePath + file);
if (f.hardLinkCount() == 1)
size += f.size();
}
return size;
}
void makeQuarantineSpace()
{
if (!isQuarantineEnabled())
return;
std::size_t sizeLimit = COOLWSD::getConfigValue<std::size_t>("quarantine_files.limit_dir_size_mb", 0)*1024*1024;
std::vector<std::string> files;
Poco::File(COOLWSD::QuarantinePath).list(files);
std::sort(files.begin(), files.end());
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();
std::size_t currentSize = quarantineSize();
auto index = files.begin();
while (index != files.end() && !files.empty())
{
FileUtil::Stat file(COOLWSD::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;
if ( (file.hardLinkCount() == 1) && (isExpired || (currentSize >= sizeLimit)) )
{
currentSize -= file.size();
FileUtil::removeFile(COOLWSD::QuarantinePath + *index, true);
files.erase(index);
}
else
index++;
}
}
void clearOldQuarantineVersions(const std::string& Wopiscr)
{
if (!isQuarantineEnabled())
return;
std::size_t maxVersionCount = COOLWSD::getConfigValue<std::size_t>("quarantine_files.max_versions_to_maintain", 2);
std::string decoded;
Poco::URI::decode(Wopiscr, decoded);
while (COOLWSD::QuarantineMap[decoded].size() > maxVersionCount)
{
FileUtil::removeFile(COOLWSD::QuarantineMap[decoded][0]);
COOLWSD::QuarantineMap[decoded].erase(COOLWSD::QuarantineMap[decoded].begin());
}
}
bool quarantineFile(DocumentBroker* docBroker, const std::string& docName)
{
if (!isQuarantineEnabled())
return false;
std::string docKey;
Poco::URI::encode(docBroker->getDocKey(), "?#/", docKey);
const auto timeNow = std::chrono::system_clock::now();
const std::string ts = std::to_string(std::chrono::duration_cast<std::chrono::seconds>(timeNow.time_since_epoch()).count());
std::string sourcefilePath;
if(JailUtil::isBindMountingEnabled())
{
sourcefilePath = COOLWSD::ChildRoot + "tmp/cool-" + docBroker->getJailId() +
"/user/docs/" + docBroker->getJailId() + '/' + docName;
}
else
{
sourcefilePath = COOLWSD::ChildRoot + docBroker->getJailId() + "/tmp/user/docs/" +
docBroker->getJailId() + '/' + docName;
}
std::string linkedFileName = ts + '_' + std::to_string(docBroker->getPid()) + '_' + docKey + '_' + docName;
std::string linkedFilePath = COOLWSD::QuarantinePath + linkedFileName;
auto& fileList = COOLWSD::QuarantineMap[docBroker->getDocKey()];
if(!fileList.empty())
{
FileUtil::Stat sourceStat(sourcefilePath);
FileUtil::Stat lastFileStat(fileList[fileList.size()-1]);
if(lastFileStat.inodeNumber() == sourceStat.inodeNumber())
{
LOG_INF("Quarantining of file " << sourcefilePath << " to " << linkedFilePath
<< " is skipped because this file version is already quarantined.");
return false;
}
}
makeQuarantineSpace();
int result_link = link(sourcefilePath.c_str(),linkedFilePath.c_str());
if (result_link == 0)
{
fileList.emplace_back(linkedFilePath);
clearOldQuarantineVersions(docKey);
makeQuarantineSpace();
LOG_INF("Quarantined " << sourcefilePath << " to " << linkedFilePath);
return true;
}
else
{
int saved_errno = errno;
LOG_ERR("Quarantining of file " << sourcefilePath << " to " << linkedFilePath << " failed: "
<< Util::symbolicErrno(saved_errno) << ": " << std::strerror(saved_errno));
return false;
}
return false;
}
}