This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathidentity.cpp
59 lines (41 loc) · 1.76 KB
/
identity.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
#include "containerutility.h"
int GetAccountSid(ArgumentStream* Arguments)
{
wchar_t* accountName = Arguments->GetArgument();
if (accountName == nullptr) {
fprintf(stderr, "Usage: containerutility getaccountsid accountname\n\n");
return ERROR_INVALID_PARAMETER;
}
Arguments->Advance();
wchar_t* sidBuffer = new wchar_t[MAX_SID_SIZE];
PSID sid = (PSID) sidBuffer;
LPTSTR referencedDomain = (LPTSTR) new wchar_t[MAX_PATH];
DWORD sidLength = MAX_SID_SIZE;
DWORD referencedDomainLength = MAX_PATH;
SID_NAME_USE sidNameUse;
if (LookupAccountName(nullptr, accountName, sid, &sidLength, referencedDomain, &referencedDomainLength, &sidNameUse) == false) {
LPTSTR messageBuffer;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&messageBuffer, 0, NULL);
fprintf(stderr, "%S\n", messageBuffer);
delete[] sidBuffer;
delete[] referencedDomain;
return GetLastError();
}
LPTSTR stringSid = nullptr;
if (ConvertSidToStringSid(sid, &stringSid) != false) {
fprintf(stdout, "%S", stringSid);
LocalFree(stringSid);
} else {
LPTSTR messageBuffer;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&messageBuffer, 0, NULL);
fprintf(stderr, "%S\n", messageBuffer);
delete[] sidBuffer;
delete[] referencedDomain;
return GetLastError();
}
delete[] sidBuffer;
delete[] referencedDomain;
return ERROR_SUCCESS;
}