aboutsummaryrefslogtreecommitdiff
path: root/CPP/Windows/SecurityUtils.cpp
diff options
context:
space:
mode:
authorIgor Pavlov <87184205+ip7z@users.noreply.github.com>2023-06-21 00:00:00 +0000
committerIgor Pavlov <87184205+ip7z@users.noreply.github.com>2023-12-17 14:59:19 +0500
commit5b39dc76f1bc82f941d5c800ab9f34407a06b53a (patch)
treefe5e17420300b715021a76328444088d32047963 /CPP/Windows/SecurityUtils.cpp
parent93be7d4abfd4233228f58ee1fbbcd76d91be66a4 (diff)
download7zip-23.01.tar.gz
7zip-23.01.tar.bz2
7zip-23.01.zip
23.0123.01
Diffstat (limited to 'CPP/Windows/SecurityUtils.cpp')
-rw-r--r--CPP/Windows/SecurityUtils.cpp38
1 files changed, 18 insertions, 20 deletions
diff --git a/CPP/Windows/SecurityUtils.cpp b/CPP/Windows/SecurityUtils.cpp
index ede83fa..d4282d0 100644
--- a/CPP/Windows/SecurityUtils.cpp
+++ b/CPP/Windows/SecurityUtils.cpp
@@ -4,9 +4,6 @@
4 4
5#include "SecurityUtils.h" 5#include "SecurityUtils.h"
6 6
7#define MY_CAST_FUNC (void(*)())
8// #define MY_CAST_FUNC
9
10namespace NWindows { 7namespace NWindows {
11namespace NSecurity { 8namespace NSecurity {
12 9
@@ -35,7 +32,7 @@ bool MyLookupAccountSid(LPCTSTR systemName, PSID sid,
35 32
36static void SetLsaString(LPWSTR src, PLSA_UNICODE_STRING dest) 33static void SetLsaString(LPWSTR src, PLSA_UNICODE_STRING dest)
37{ 34{
38 size_t len = (size_t)wcslen(src); 35 const size_t len = (size_t)wcslen(src);
39 dest->Length = (USHORT)(len * sizeof(WCHAR)); 36 dest->Length = (USHORT)(len * sizeof(WCHAR));
40 dest->MaximumLength = (USHORT)((len + 1) * sizeof(WCHAR)); 37 dest->MaximumLength = (USHORT)((len + 1) * sizeof(WCHAR));
41 dest->Buffer = src; 38 dest->Buffer = src;
@@ -72,13 +69,14 @@ typedef BOOL (WINAPI * Func_LookupAccountNameW)(
72static PSID GetSid(LPWSTR accountName) 69static PSID GetSid(LPWSTR accountName)
73{ 70{
74 #ifndef _UNICODE 71 #ifndef _UNICODE
75 HMODULE hModule = GetModuleHandle(TEXT("Advapi32.dll")); 72 const HMODULE hModule = GetModuleHandle(TEXT("advapi32.dll"));
76 if (hModule == NULL) 73 if (!hModule)
77 return NULL; 74 return NULL;
78 Func_LookupAccountNameW lookupAccountNameW = (Func_LookupAccountNameW) 75 const
79 MY_CAST_FUNC 76 Func_LookupAccountNameW lookupAccountNameW = Z7_GET_PROC_ADDRESS(
80 GetProcAddress(hModule, "LookupAccountNameW"); 77 Func_LookupAccountNameW, hModule,
81 if (lookupAccountNameW == NULL) 78 "LookupAccountNameW");
79 if (!lookupAccountNameW)
82 return NULL; 80 return NULL;
83 #endif 81 #endif
84 82
@@ -88,21 +86,21 @@ static PSID GetSid(LPWSTR accountName)
88 #ifdef _UNICODE 86 #ifdef _UNICODE
89 ::LookupAccountNameW 87 ::LookupAccountNameW
90 #else 88 #else
91 lookupAccountNameW 89 lookupAccountNameW
92 #endif 90 #endif
93 (NULL, accountName, NULL, &sidLen, NULL, &domainLen, &sidNameUse)) 91 (NULL, accountName, NULL, &sidLen, NULL, &domainLen, &sidNameUse))
94 { 92 {
95 if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) 93 if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
96 { 94 {
97 PSID pSid = ::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sidLen); 95 const PSID pSid = ::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sidLen);
98 LPWSTR domainName = (LPWSTR)::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (domainLen + 1) * sizeof(WCHAR)); 96 LPWSTR domainName = (LPWSTR)::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (domainLen + 1) * sizeof(WCHAR));
99 BOOL res = 97 const BOOL res =
100 #ifdef _UNICODE 98 #ifdef _UNICODE
101 ::LookupAccountNameW 99 ::LookupAccountNameW
102 #else 100 #else
103 lookupAccountNameW 101 lookupAccountNameW
104 #endif 102 #endif
105 (NULL, accountName, pSid, &sidLen, domainName, &domainLen, &sidNameUse); 103 (NULL, accountName, pSid, &sidLen, domainName, &domainLen, &sidNameUse);
106 ::HeapFree(GetProcessHeap(), 0, domainName); 104 ::HeapFree(GetProcessHeap(), 0, domainName);
107 if (res) 105 if (res)
108 return pSid; 106 return pSid;
@@ -111,7 +109,7 @@ static PSID GetSid(LPWSTR accountName)
111 return NULL; 109 return NULL;
112} 110}
113 111
114#define MY__SE_LOCK_MEMORY_NAME L"SeLockMemoryPrivilege" 112#define Z7_WIN_SE_LOCK_MEMORY_NAME L"SeLockMemoryPrivilege"
115 113
116bool AddLockMemoryPrivilege() 114bool AddLockMemoryPrivilege()
117{ 115{
@@ -131,13 +129,13 @@ bool AddLockMemoryPrivilege()
131 != 0) 129 != 0)
132 return false; 130 return false;
133 LSA_UNICODE_STRING userRights; 131 LSA_UNICODE_STRING userRights;
134 wchar_t s[128] = MY__SE_LOCK_MEMORY_NAME; 132 wchar_t s[128] = Z7_WIN_SE_LOCK_MEMORY_NAME;
135 SetLsaString(s, &userRights); 133 SetLsaString(s, &userRights);
136 WCHAR userName[256 + 2]; 134 WCHAR userName[256 + 2];
137 DWORD size = 256; 135 DWORD size = 256;
138 if (!GetUserNameW(userName, &size)) 136 if (!GetUserNameW(userName, &size))
139 return false; 137 return false;
140 PSID psid = GetSid(userName); 138 const PSID psid = GetSid(userName);
141 if (psid == NULL) 139 if (psid == NULL)
142 return false; 140 return false;
143 bool res = false; 141 bool res = false;
@@ -176,7 +174,7 @@ bool AddLockMemoryPrivilege()
176 res = true; 174 res = true;
177 } 175 }
178 */ 176 */
179 NTSTATUS status = policy.AddAccountRights(psid, &userRights); 177 const NTSTATUS status = policy.AddAccountRights(psid, &userRights);
180 if (status == 0) 178 if (status == 0)
181 res = true; 179 res = true;
182 // ULONG res = LsaNtStatusToWinError(status); 180 // ULONG res = LsaNtStatusToWinError(status);