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
|
#include "stdafx.h"
#include "cleaningops.h"
#include <libcommon/filesystem.h>
#include <libcommon/fileenumerator.h>
#include <libcommon/string.h>
#include <libcommon/memory.h>
#include <libcommon/security.h>
#include <libcommon/process.h>
#include <filesystem>
#include <utility>
#include <functional>
#include <processthreadsapi.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);
}
std::wstring ConstructLocalAppDataPath(const std::wstring &base, const std::wstring &user,
const std::pair<std::vector<std::wstring>::iterator, std::vector<std::wstring>::iterator> &tokens)
{
auto path = std::filesystem::path(base);
path.append(user);
std::for_each(tokens.first, tokens.second, [&](const std::wstring &token)
{
path.append(token);
});
return path;
}
std::wstring GetSystemUserLocalAppData()
{
common::security::AdjustCurrentProcessTokenPrivilege(L"SeDebugPrivilege");
common::memory::ScopeDestructor sd;
sd += []
{
common::security::AdjustCurrentProcessTokenPrivilege(L"SeDebugPrivilege", false);
};
auto systemDir = common::fs::GetKnownFolderPath(FOLDERID_System, KF_FLAG_DEFAULT, NULL);
auto lsassPath = std::filesystem::path(systemDir).append(L"lsass.exe");
auto lsassPid = common::process::GetProcessIdFromName(lsassPath);
auto processHandle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, lsassPid);
if (nullptr == processHandle)
{
throw std::runtime_error("Failed to access the \"LSASS\" process");
}
HANDLE processToken;
auto status = OpenProcessToken(processHandle, TOKEN_READ | TOKEN_IMPERSONATE | TOKEN_DUPLICATE, &processToken);
CloseHandle(processHandle);
if (FALSE == status)
{
throw std::runtime_error("Failed to acquire process token for the \"LSASS\" process");
}
sd += [&]()
{
CloseHandle(processToken);
};
return common::fs::GetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT, processToken);
}
} // anonymous namespace
namespace cleaningops
{
void RemoveLogsCacheCurrentUser()
{
const auto localAppData = common::fs::GetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT, nullptr);
const auto appdir = std::filesystem::path(localAppData).append(L"Mullvad VPN");
std::filesystem::remove_all(appdir);
}
void RemoveLogsCacheOtherUsers()
{
//
// Determine relative path to "local app data" from home directory.
//
// Beware, the local app data path may be overriden from its default location
// as a node somewhere beneath the home directory.
//
auto localAppData = common::fs::GetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT, nullptr);
auto homeDir = common::fs::GetKnownFolderPath(FOLDERID_Profile, KF_FLAG_DEFAULT, nullptr);
//
// Tokenize to get rid of slashes pointing in different directions.
//
auto localAppDataTokens = common::string::Tokenize(localAppData, L"\\/");
auto homeDirTokens = common::string::Tokenize(homeDir, L"\\/");
auto mirror = mirrored_range
(
localAppDataTokens.begin(), localAppDataTokens.end(),
homeDirTokens.begin(), homeDirTokens.end(),
[](const std::wstring &lhs, const std::wstring &rhs)
{
return 0 == _wcsicmp(lhs.c_str(), rhs.c_str());
}
);
auto equalTokensCount = (size_t)std::distance(mirror.first, mirror.second);
//
// 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());
auto currentUser = *homeDirTokens.rbegin();
//
// Find all other users and construct the most plausible path for their
// respective "local app data" dirs.
//
auto parentHomeDir = common::fs::GetKnownFolderPath(FOLDERID_UserProfiles, KF_FLAG_DEFAULT, nullptr);
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 = ConstructLocalAppDataPath(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);
}
}
void RemoveLogsServiceUser()
{
const auto programData = common::fs::GetKnownFolderPath(FOLDERID_ProgramData, KF_FLAG_DEFAULT, nullptr);
const auto appdir = std::filesystem::path(programData).append(L"Mullvad VPN");
std::filesystem::remove_all(appdir);
}
void RemoveCacheServiceUser()
{
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);
{
common::fs::FileEnumerator files(mullvadAppData);
auto notNamedSet = std::make_unique<common::fs::FilterNotNamedSet>();
notNamedSet->addObject(L"account-history.json");
notNamedSet->addObject(L"settings.json");
files.addFilter(std::move(notNamedSet));
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);
}
}
//
// This fails unless the directory is empty.
// Which is what we want, since removing cache and settings files are separate operations.
//
RemoveDirectoryW(std::wstring(L"\\\\?\\").append(mullvadAppData).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);
{
common::fs::FileEnumerator files(mullvadAppData);
auto filter = std::make_unique<common::fs::FilterNamedSet>();
filter->addObject(L"account-history.json");
filter->addObject(L"settings.json");
files.addFilter(std::move(filter));
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);
}
}
//
// This fails unless the directory is empty.
// Which is what we want, since removing cache and settings files are separate operations.
//
RemoveDirectoryW(std::wstring(L"\\\\?\\").append(mullvadAppData).c_str());
}
void RemoveRelayCacheServiceUser()
{
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);
const auto cacheFile = std::filesystem::path(mullvadAppData).append(L"relays.json");
std::filesystem::remove(cacheFile);
}
}
|