-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_homedir.cpp
405 lines (365 loc) · 12.3 KB
/
move_homedir.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* @file move_homedir.cpp
* @author András Attila Gerendás
* @brief Updates home directory in the registry
* @date 2018.03.16.
*
* This utility renames all entries for a specific user to another user in the
* registry. Particularly useful when moving the home directory.
*
* Requires administrative privileges in order to run correctly.
*/
#include <iostream>
#include "Windows.h"
#include "Winreg.h"
#include <io.h>
#include <fcntl.h>
#include <string>
#include <tchar.h>
#include <stdio.h>
#define DEBUG false
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
#define NAME_BUFFER 1024
#define FROM_NAME L"Users\\from"
#define TO_NAME L"Users\\to"
/**
* @class RegKey
*
* @brief Representation of a key in the registry.
*
* @date 2018.03.16.
*/
class RegKey {
/** @brief The depth of the current key. Used to detect stack overflow. */
int depth;
/** @brief The error code of the registry API */
DWORD errorCode;
/** @brief Evaulates whether the key was successfully opened */
bool isValidb;
/** @brief Handle of the key */
HKEY key;
/** @brief The name of the key */
TCHAR name[NAME_BUFFER];
/** @brief Number of subkeys of the key */
DWORD subkeyCount;
/** @brief Size of the longest subkey */
DWORD longestSubkeySize;
/** @brief Size of the longest subclass */
DWORD longestSubClassSize;
/** @brief Number of values */
DWORD valueCount;
/** @brief Length of the name of the longest value */
DWORD longestValueName;
/** @brief Information describing the longest value */
DWORD longestValueData;
/** @brief Size of the security descriptor */
DWORD securityDescriptorSize;
/** @brief The last write time */
FILETIME lastWriteTime;
public:
/**
* @fn RegKey(HKEY parent, TCHAR* name, int depth)
*
* @brief Creates a new registry key under the parent key
*
* @date 2018.03.16.
*
* @param parent Handle of the parent.
* @param [in,out] name If non-null, the name.
* @param depth The depth of the key from the root of the hive
*/
RegKey(HKEY parent, TCHAR* name, int depth)
{
this->depth = depth;
_tcscpy_s(this->name, NAME_BUFFER, name);
errorCode = RegOpenKeyEx(parent, name, 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY,
&key);
isValidb = (errorCode == ERROR_SUCCESS);
if (isValidb) {
getInfo();
}
else {
if (errorCode == ERROR_ACCESS_DENIED && DEBUG) {
std::wcout << "Access denied. Are you an administrator?" << "\n";
}
else if (errorCode != ERROR_FILE_NOT_FOUND && DEBUG) {
std::wcout << "Error during key open: " << errorCode << "\n";
}
}
}
/**
* @fn ~RegKey()
*
* @brief Closes the key if it was opened successfully in the first place
*
* @date 2018.03.16.
*/
~RegKey()
{
if (isValidb) {
RegCloseKey(key);
}
}
/**
* @fn void getInfo()
*
* @brief Retrieves metadata of the key (used for subkey count mainly)
*
* @date 2018.03.16.
*/
void getInfo()
{
RegQueryInfoKey(
key, // key handle
NULL, // buffer for class name
NULL, // size of class string
NULL, // reserved
&subkeyCount, // number of subkeys
&longestSubkeySize, // longest subkey size
&longestSubClassSize, // longest class string
&valueCount, // number of values for this key
&longestValueName, // longest value name
&longestValueData, // longest value data
&securityDescriptorSize, // security descriptor
&lastWriteTime); // last write time
}
TCHAR* getName()
{
return name;
}
HKEY getKey()
{
return key;
}
DWORD getSubkeyCount()
{
return subkeyCount;
}
DWORD getValueCount()
{
return valueCount;
}
DWORD getLongestValueData()
{
return longestValueData;
}
/**
* @fn bool isValid()
*
* @brief Query whether the key was successfully opened
*
* @date 2018.03.16.
*
* @return True if valid, false if not.
*/
bool isValid()
{
return isValidb;
}
/**
* @fn int getDepth()
*
* @brief Retrieves the current distance from the root of the hive
*
* @date 2018.03.16.
*
* @return The depth from the root of the hive
*/
int getDepth()
{
return depth;
}
DWORD getErrorCode()
{
return errorCode;
}
};
/**
* @fn std::wstring Replace(const std::wstring& haystack, const std::wstring& needle,
* const std::wstring& replacement)
*
* @brief Replaces all occurences of the needle in the haystack.
*
* Does not modify the original string.
*
* @date 2018.03.16.
*
* @param haystack The whole original string.
* @param needle The string to be replaced.
* @param replacement The replacement.
*
* @return A std::wstring containing the replaced string.
*/
std::wstring Replace(const std::wstring& haystack, const std::wstring& needle,
const std::wstring& replacement)
{
std::wstring value = haystack;
size_t pos = 0;
while (true) {
pos = value.find(needle, pos);
if (pos == std::wstring::npos)
break;
value.replace(pos, needle.length(), replacement);
pos += replacement.length();
}
return value;
}
/**
* @fn bool iter(RegKey *keyHolder, int& count)
*
* @brief Iterates over the subkeys and prints the values of the key. Recursive function.
*
* If it finds a matching value it replaces the home directory
* information in the value.
* Note: The extensive use of heap memory is to avoid stack overflow.
*
* @date 2018.03.16.
*
* @param [in,out] keyHolder If non-null, the key holder.
* @param [in,out] count Number of values which match.
*
* @return True if it succeeds, false if it fails.
*/
bool iter(RegKey *keyHolder, int& count)
{
DWORD errValue;
std::wcout << "Iterating through (" << keyHolder->getDepth() << ") " <<
keyHolder->getName() << ":\n";
for (DWORD i = 0; i < keyHolder->getSubkeyCount(); i++) {
DWORD maxKeyName = MAX_KEY_LENGTH;
TCHAR *keyName = new TCHAR[MAX_KEY_LENGTH];
FILETIME lastWriteTime;
if ((errValue = RegEnumKeyEx(keyHolder->getKey(), i, keyName, &maxKeyName, NULL,
NULL, NULL, &lastWriteTime)) != ERROR_SUCCESS) {
std::wcout << "Error: " << errValue << "\n";
delete keyName;
return false;
}
RegKey *subKey = new RegKey(keyHolder->getKey(), keyName,
keyHolder->getDepth() + 1);
/* This is to workaround registry virtualization */
if (!subKey->isValid() && subKey->getErrorCode() != ERROR_FILE_NOT_FOUND) {
if (DEBUG || subKey->getErrorCode() != ERROR_ACCESS_DENIED) {
std::wcout << "Error: creation of subkey " << keyName << "\n";
}
/* Access denial should not be a problem here */
if (subKey->getErrorCode() != ERROR_ACCESS_DENIED) {
delete keyName;
delete subKey;
return false;
}
}
std::wcout << i << ": " << keyName << "\n";
/* Only iterate through the key if it's valid */
if (subKey->isValid() && !iter(subKey, count)) {
delete keyName;
delete subKey;
return false;
}
delete keyName;
delete subKey;
}
std::wcout << "Values for class " << keyHolder->getName() << ":\n";
for (DWORD i = 0; i < keyHolder->getValueCount(); i++) {
DWORD maxKeyValue = MAX_VALUE_NAME;
TCHAR* valueName = new TCHAR[MAX_VALUE_NAME];
if ((errValue = RegEnumValue(keyHolder->getKey(), i, valueName, &maxKeyValue,
NULL, NULL, NULL, NULL)) != ERROR_SUCCESS) {
std::wcout << "Error: " << errValue << "\n";
delete valueName;
return false;
}
std::wcout << i << ": " << valueName << "\n";
/* We do not know the size of the value to be retrieved, so assume the worst */
TCHAR *data = new TCHAR[keyHolder->getLongestValueData() * 2 + 2];
memset(data, 0, keyHolder->getLongestValueData() * 2 + 2);
DWORD type, size = keyHolder->getLongestValueData() * 2 + 2;
if ((errValue = RegGetValue(keyHolder->getKey(), NULL, valueName, RRF_RT_REG_SZ,
&type, data, &size)) != ERROR_SUCCESS) {
/* Unsupported type only means we encountered a non-string value */
if (errValue != ERROR_UNSUPPORTED_TYPE) {
if (errValue == ERROR_MORE_DATA) {
std::wcout << "Maximum length: " << keyHolder->getLongestValueData() << "\n";
}
std::wcout << "Error during value retrival: " << errValue << "\n";
delete data;
delete valueName;
return false;
}
}
if (errValue == ERROR_SUCCESS) {
/*Only replace the string if it matches what we search for */
if (wcsstr(data, FROM_NAME) != NULL) {
count++;
std::wcout << "key: " << keyHolder->getName() << " valueName: " << i << ": " <<
valueName << "\n";
std::wstring replaced(data);
replaced = Replace(replaced, FROM_NAME, TO_NAME);
std::wcout << i << " value: " << data << "\n";
std::wcout << i << " new value: " << replaced << "\n";
DWORD setRes = RegSetValueEx(keyHolder->getKey(), valueName, 0, REG_SZ,
(LPBYTE)replaced.c_str(),
((DWORD)replaced.length() + 1) * (DWORD)sizeof(WCHAR));
if (setRes != ERROR_SUCCESS) {
delete valueName;
delete data;
return false;
}
}
}
delete valueName;
delete data;
}
return true;
}
/**
* @fn int main()
*
* @brief Main entry-point for this application
*
* Hives need to be iterated separately, but they use a common count.
*
* @date 2018.03.16.
*
* @return Exit-code for the process - 0 for success, else an error code.
*/
int main()
{
//https://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app
_setmode(_fileno(stdout), _O_U16TEXT);
/* Used to hold the values which match the replacement criterium */
int count = 0;
RegKey classesRoot(HKEY_CLASSES_ROOT, L"", 0);
if (!classesRoot.isValid()) {
return -1;
}
iter(&classesRoot, count);
RegKey currentUser(HKEY_CURRENT_USER, L"", 0);
if (!currentUser.isValid()) {
return -1;
}
iter(¤tUser, count);
RegKey localMachine(HKEY_LOCAL_MACHINE, L"", 0);
if (!localMachine.isValid()) {
return -1;
}
iter(&localMachine, count);
RegKey users(HKEY_USERS, L"", 0);
if (!users.isValid()) {
return -1;
}
iter(&users, count);
RegKey currentConfig(HKEY_CURRENT_CONFIG, L"", 0);
if (!currentConfig.isValid()) {
return -1;
}
iter(¤tConfig, count);
std::wcout << "Number of results: " << count << "\n";
/* This is to ensure the program is also usable from the desktop */
do {
std::wcout << '\n' << "Press the return key to continue...";
}
while (std::wcin.get() != '\n');
return 0;
}