This commit is contained in:
Igor Pavlov 2023-06-21 00:00:00 +00:00
parent 93be7d4abf
commit 5b39dc76f1
951 changed files with 39855 additions and 24810 deletions

View file

@ -4,9 +4,6 @@
#include "SecurityUtils.h"
#define MY_CAST_FUNC (void(*)())
// #define MY_CAST_FUNC
namespace NWindows {
namespace NSecurity {
@ -35,7 +32,7 @@ bool MyLookupAccountSid(LPCTSTR systemName, PSID sid,
static void SetLsaString(LPWSTR src, PLSA_UNICODE_STRING dest)
{
size_t len = (size_t)wcslen(src);
const size_t len = (size_t)wcslen(src);
dest->Length = (USHORT)(len * sizeof(WCHAR));
dest->MaximumLength = (USHORT)((len + 1) * sizeof(WCHAR));
dest->Buffer = src;
@ -72,13 +69,14 @@ typedef BOOL (WINAPI * Func_LookupAccountNameW)(
static PSID GetSid(LPWSTR accountName)
{
#ifndef _UNICODE
HMODULE hModule = GetModuleHandle(TEXT("Advapi32.dll"));
if (hModule == NULL)
const HMODULE hModule = GetModuleHandle(TEXT("advapi32.dll"));
if (!hModule)
return NULL;
Func_LookupAccountNameW lookupAccountNameW = (Func_LookupAccountNameW)
MY_CAST_FUNC
GetProcAddress(hModule, "LookupAccountNameW");
if (lookupAccountNameW == NULL)
const
Func_LookupAccountNameW lookupAccountNameW = Z7_GET_PROC_ADDRESS(
Func_LookupAccountNameW, hModule,
"LookupAccountNameW");
if (!lookupAccountNameW)
return NULL;
#endif
@ -88,21 +86,21 @@ static PSID GetSid(LPWSTR accountName)
#ifdef _UNICODE
::LookupAccountNameW
#else
lookupAccountNameW
lookupAccountNameW
#endif
(NULL, accountName, NULL, &sidLen, NULL, &domainLen, &sidNameUse))
(NULL, accountName, NULL, &sidLen, NULL, &domainLen, &sidNameUse))
{
if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
PSID pSid = ::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sidLen);
const PSID pSid = ::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sidLen);
LPWSTR domainName = (LPWSTR)::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (domainLen + 1) * sizeof(WCHAR));
BOOL res =
const BOOL res =
#ifdef _UNICODE
::LookupAccountNameW
#else
lookupAccountNameW
lookupAccountNameW
#endif
(NULL, accountName, pSid, &sidLen, domainName, &domainLen, &sidNameUse);
(NULL, accountName, pSid, &sidLen, domainName, &domainLen, &sidNameUse);
::HeapFree(GetProcessHeap(), 0, domainName);
if (res)
return pSid;
@ -111,7 +109,7 @@ static PSID GetSid(LPWSTR accountName)
return NULL;
}
#define MY__SE_LOCK_MEMORY_NAME L"SeLockMemoryPrivilege"
#define Z7_WIN_SE_LOCK_MEMORY_NAME L"SeLockMemoryPrivilege"
bool AddLockMemoryPrivilege()
{
@ -131,13 +129,13 @@ bool AddLockMemoryPrivilege()
!= 0)
return false;
LSA_UNICODE_STRING userRights;
wchar_t s[128] = MY__SE_LOCK_MEMORY_NAME;
wchar_t s[128] = Z7_WIN_SE_LOCK_MEMORY_NAME;
SetLsaString(s, &userRights);
WCHAR userName[256 + 2];
DWORD size = 256;
if (!GetUserNameW(userName, &size))
return false;
PSID psid = GetSid(userName);
const PSID psid = GetSid(userName);
if (psid == NULL)
return false;
bool res = false;
@ -176,7 +174,7 @@ bool AddLockMemoryPrivilege()
res = true;
}
*/
NTSTATUS status = policy.AddAccountRights(psid, &userRights);
const NTSTATUS status = policy.AddAccountRights(psid, &userRights);
if (status == 0)
res = true;
// ULONG res = LsaNtStatusToWinError(status);