-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
OperationRestoreSecurity.cpp
75 lines (63 loc) · 2.23 KB
/
OperationRestoreSecurity.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
#include "OperationRestoreSecurity.h"
#include "InputOutput.h"
#include "Helpers.h"
#include <fstream>
#include <locale>
#include <codecvt>
ClassFactory<OperationRestoreSecurity> OperationRestoreSecurity::RegisteredFactory(GetCommand());
OperationRestoreSecurity::OperationRestoreSecurity(std::queue<std::wstring> & oArgList, const std::wstring & sCommand) : Operation(oArgList)
{
// exit if there are not enough arguments to parse
const std::vector<std::wstring> sSubArgs = ProcessAndCheckArgs(1, oArgList, L"\\0");
// open the file
std::wifstream fFile(sSubArgs.at(0).c_str());
// adapt the stream to read windows unicode files
(void) fFile.imbue(std::locale(fFile.getloc(), new std::codecvt_utf8<wchar_t,
0x10ffff, std::consume_header>));
// read the file line-by-line
std::wstring sLine;
while (std::getline(fFile, sLine))
{
// parse the file name and descriptor which are separated by a '|' character
// also, sometimes a carriage return appears in the input stream so adding
// it here ensures it is stripped from the very end
std::vector<std::wstring> oLineItems = SplitArgs(sLine, L"\\||\r");
// convert the long string descriptor its binary equivalent
PSECURITY_DESCRIPTOR tDesc;
if (oLineItems.size() != 2 ||
ConvertStringSecurityDescriptorToSecurityDescriptor(oLineItems.at(1).c_str(),
SDDL_REVISION_1, &tDesc, nullptr) == 0)
{
wprintf(L"ERROR: Unable to parse string security descriptor file for restoration.");
std::exit(-1);
}
// update the map
oImportMap[oLineItems.at(0)] = tDesc;
}
// cleanup
fFile.close();
// flag this as being an ace-level action
AppliesToSd = true;
AppliesToDacl = true;
AppliesToSacl = true;
AppliesToOwner = true;
AppliesToGroup = true;
}
bool OperationRestoreSecurity::ProcessSdAction(std::wstring & sFileName, ObjectEntry & tObjectEntry, PSECURITY_DESCRIPTOR & tDescriptor, bool & bDescReplacement)
{
const auto oSecInfo = oImportMap.find(sFileName);
if (oSecInfo != oImportMap.end())
{
// lookup the string in the map
if (bDescReplacement) LocalFree(tDescriptor);
tDescriptor = oSecInfo->second;
bDescReplacement = true;
}
else
{
// update the sid in the ace
InputOutput::AddError(L"Import File Did Not Contain Descriptor");
}
// cleanup
return true;
}