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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
#include "stdafx.h"
#include "cleaningops.h"
#include <libcommon/filesystem.h>
#include <libcommon/fileenumerator.h>
#include <libcommon/string.h>
#include <libcommon/error.h>
#include <libcommon/memory.h>
#include <libcommon/security.h>
#include <libcommon/process/process.h>
#include <optional>
#include <filesystem>
#include <utility>
#include <functional>
#include <processthreadsapi.h>
#include <mullvad-nsis.h>
namespace
{
//
// Returns range in lhs that is also present in rhs.
// Equalence/equivalence determined by 'comp'.
//
// Returns pair<lhsBegin, lhsBegin> if there is no mirrored range.
//
template<typename ForwardIterator>
std::pair<ForwardIterator, ForwardIterator>
mirrored_range
(
ForwardIterator lhsBegin, ForwardIterator lhsEnd,
ForwardIterator rhsBegin, ForwardIterator rhsEnd,
std::function<bool(const typename ForwardIterator::value_type &, const typename ForwardIterator::value_type &)> comp
)
{
ForwardIterator begin = lhsBegin;
while (lhsBegin != lhsEnd
&& rhsBegin != rhsEnd)
{
if (false == comp(*lhsBegin, *rhsBegin))
{
break;
}
++lhsBegin;
++rhsBegin;
}
return std::make_pair(begin, lhsBegin);
}
template<typename IterType>
std::wstring ConstructUserPath(const std::wstring &users, const std::wstring &user,
const std::pair<IterType, IterType> &tokens)
{
auto path = std::filesystem::path(users);
path.append(user);
std::for_each(tokens.first, tokens.second, [&](const std::wstring &token)
{
path.append(token);
});
return path;
}
std::wstring GetSystemUserLocalAppData()
{
std::vector<uint16_t> buffer(256);
size_t bufferSize = buffer.size();
GET_LOCAL_APPDATA:
auto result = get_system_local_appdata(buffer.data(), &bufferSize);
if (Status::InsufficientBufferSize == result)
{
buffer.resize(bufferSize);
goto GET_LOCAL_APPDATA;
}
if (Status::Ok != result)
{
THROW_ERROR("Failed to acquire system app data path");
}
return std::wstring(reinterpret_cast<wchar_t *>(buffer.data()));
}
std::filesystem::path GetSystemCacheDirectory()
{
const auto programData = common::fs::GetKnownFolderPath(FOLDERID_ProgramData);
return std::filesystem::path(programData).append(L"Mullvad VPN").append(L"cache");
}
template <class It>
size_t EqualTokensCount(It lhsBegin, It lhsEnd, It rhsBegin, It rhsEnd)
{
auto mirror = mirrored_range
(
lhsBegin, lhsEnd, rhsBegin, rhsEnd,
[](const std::wstring &lhs, const std::wstring &rhs)
{
return 0 == _wcsicmp(lhs.c_str(), rhs.c_str());
}
);
return static_cast<size_t>(std::distance(mirror.first, mirror.second));
}
} // anonymous namespace
namespace cleaningops
{
//
// Migrate cache for versions <= 2020.8-beta2.
//
void MigrateCacheServiceUser()
{
const auto newCacheDir = GetSystemCacheDirectory();
common::fs::Mkdir(newCacheDir);
const auto localAppData = GetSystemUserLocalAppData();
const auto oldCacheDir = std::filesystem::path(localAppData).append(L"Mullvad VPN");
common::fs::ScopedNativeFileSystem nativeFileSystem;
common::security::AddAdminToObjectDacl(oldCacheDir, SE_FILE_OBJECT);
{
common::fs::FileEnumerator files(oldCacheDir);
auto notNamedSet = std::make_unique<common::fs::FilterNotNamedSet>();
notNamedSet->addObject(L"account-history.json");
notNamedSet->addObject(L"settings.json");
notNamedSet->addObject(L"device.json");
files.addFilter(std::move(notNamedSet));
files.addFilter(std::make_unique<common::fs::FilterFiles>());
WIN32_FIND_DATAW file;
while (files.next(file))
{
const auto source = std::filesystem::path(files.getDirectory()).append(file.cFileName);
const auto target = std::filesystem::path(newCacheDir).append(file.cFileName);
std::filesystem::rename(source, target);
}
}
//
// This fails unless the directory is empty. Settings remain in this directory.
//
RemoveDirectoryW(std::wstring(L"\\\\?\\").append(oldCacheDir).c_str());
}
void RemoveLogsCacheCurrentUser()
{
const auto localAppData = common::fs::GetKnownFolderPath(FOLDERID_LocalAppData);
const auto appdir = std::filesystem::path(localAppData).append(L"Mullvad VPN");
std::filesystem::remove_all(appdir);
const auto roamingAppData = common::fs::GetKnownFolderPath(FOLDERID_RoamingAppData);
const auto roamingAppdir = std::filesystem::path(roamingAppData).append(L"Mullvad VPN");
std::error_code dummy;
std::filesystem::remove_all(roamingAppdir, dummy);
}
void RemoveLogsCacheOtherUsers()
{
//
// Determine relative path to "local app data" from home directory.
//
// Beware, the local app data path may be overridden from its default location
// as a node somewhere beneath the home directory.
//
auto localAppData = common::fs::GetKnownFolderPath(FOLDERID_LocalAppData);
auto roamingAppData = common::fs::GetKnownFolderPath(FOLDERID_RoamingAppData);
auto homeDir = common::fs::GetKnownFolderPath(FOLDERID_Profile);
//
// Tokenize to get rid of slashes pointing in different directions.
//
auto localAppDataTokens = common::string::Tokenize(localAppData, L"\\/");
auto roamingAppDataTokens = common::string::Tokenize(roamingAppData, L"\\/");
auto homeDirTokens = common::string::Tokenize(homeDir, L"\\/");
auto equalTokensCount = EqualTokensCount(
localAppDataTokens.begin(),
localAppDataTokens.end(),
homeDirTokens.begin(),
homeDirTokens.end()
);
//
// Abort if "local app data" is not beneath home dir.
//
if (equalTokensCount < homeDirTokens.size())
{
return;
}
auto relativeLocalAppData = std::make_pair(std::next(localAppDataTokens.begin(), equalTokensCount), localAppDataTokens.end());
using StringVectorConstIter = std::vector<std::wstring>::const_iterator;
std::optional<std::pair<StringVectorConstIter, StringVectorConstIter>> relativeRoamingAppData;
const auto roamingTokensCount = EqualTokensCount(
roamingAppDataTokens.begin(),
roamingAppDataTokens.end(),
homeDirTokens.begin(),
homeDirTokens.end()
);
if (roamingTokensCount >= homeDirTokens.size())
{
relativeRoamingAppData = std::make_optional(std::make_pair(std::next(roamingAppDataTokens.cbegin(), equalTokensCount), roamingAppDataTokens.cend()));
}
auto currentUser = *homeDirTokens.rbegin();
//
// Find all other users and construct the most plausible path for their
// respective app data dirs.
//
auto parentHomeDir = common::fs::GetKnownFolderPath(FOLDERID_UserProfiles);
common::fs::FileEnumerator files(parentHomeDir);
files.addFilter(std::make_unique<common::fs::FilterDirectories>());
files.addFilter(std::make_unique<common::fs::FilterNotRelativeDirs>());
auto notNamedSet = std::make_unique<common::fs::FilterNotNamedSet>();
notNamedSet->addObject(std::wstring(currentUser));
notNamedSet->addObject(L"All Users"); // Redirects to 'c:\programdata'.
notNamedSet->addObject(L"Public"); // Shared documents, not an actual user or user template.
files.addFilter(std::move(notNamedSet));
WIN32_FIND_DATAW file;
while (files.next(file))
{
const auto userLocalAppData = ConstructUserPath(files.getDirectory(), file.cFileName, relativeLocalAppData);
const auto target = std::filesystem::path(userLocalAppData).append(L"Mullvad VPN");
std::error_code dummy;
std::filesystem::remove_all(target, dummy);
if (relativeRoamingAppData.has_value())
{
const auto userRoamingAppData = ConstructUserPath(files.getDirectory(), file.cFileName, relativeRoamingAppData.value());
const auto roamingTarget = std::filesystem::path(userRoamingAppData).append(L"Mullvad VPN");
std::filesystem::remove_all(roamingTarget, dummy);
}
}
}
void RemoveLogsServiceUser()
{
const auto programData = common::fs::GetKnownFolderPath(FOLDERID_ProgramData);
const auto appdir = std::filesystem::path(programData).append(L"Mullvad VPN");
{
common::fs::FileEnumerator files(appdir);
files.addFilter(std::make_unique<common::fs::FilterFiles>());
WIN32_FIND_DATAW file;
while (files.next(file))
{
const auto target = std::filesystem::path(files.getDirectory()).append(file.cFileName);
std::error_code dummy;
std::filesystem::remove(target, dummy);
}
}
RemoveDirectoryW(std::wstring(L"\\\\?\\").append(appdir).c_str());
}
void RemoveCacheServiceUser()
{
const auto cacheDir = GetSystemCacheDirectory();
std::error_code dummy;
std::filesystem::remove_all(cacheDir, dummy);
const auto appdir = cacheDir.parent_path();
RemoveDirectoryW(std::wstring(L"\\\\?\\").append(appdir).c_str());
}
void RemoveSettingsServiceUser()
{
const auto localAppData = GetSystemUserLocalAppData();
const auto mullvadAppData = std::filesystem::path(localAppData).append(L"Mullvad VPN");
common::fs::ScopedNativeFileSystem nativeFileSystem;
common::security::AddAdminToObjectDacl(mullvadAppData, SE_FILE_OBJECT);
std::filesystem::remove_all(mullvadAppData);
}
void RemoveRelayCacheServiceUser()
{
const auto cacheFile = GetSystemCacheDirectory().append(L"relays.json");
std::filesystem::remove(cacheFile);
}
void RemoveApiAddressCacheServiceUser()
{
const auto cacheFile = GetSystemCacheDirectory().append(L"api-ip-address.txt");
std::filesystem::remove(cacheFile);
}
}
|