diff options
author | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2022-07-16 00:00:00 +0000 |
---|---|---|
committer | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2023-12-17 14:35:38 +0500 |
commit | 93be7d4abfd4233228f58ee1fbbcd76d91be66a4 (patch) | |
tree | 9b11631a70ca7150321838d9d6bd473e52d6684d /CPP/Windows | |
parent | a3e1d227377188734b82f023f96f8e25dc40f3e6 (diff) | |
download | 7zip-22.01.tar.gz 7zip-22.01.tar.bz2 7zip-22.01.zip |
22.0122.01
Diffstat (limited to 'CPP/Windows')
-rw-r--r-- | CPP/Windows/MemoryLock.cpp | 2 | ||||
-rw-r--r-- | CPP/Windows/SecurityUtils.cpp | 13 | ||||
-rw-r--r-- | CPP/Windows/SecurityUtils.h | 83 | ||||
-rw-r--r-- | CPP/Windows/Shell.cpp | 19 |
4 files changed, 57 insertions, 60 deletions
diff --git a/CPP/Windows/MemoryLock.cpp b/CPP/Windows/MemoryLock.cpp index fdfbeb9..24866b1 100644 --- a/CPP/Windows/MemoryLock.cpp +++ b/CPP/Windows/MemoryLock.cpp | |||
@@ -21,7 +21,7 @@ typedef BOOL (WINAPI * Func_LookupPrivilegeValue)(LPCTSTR lpSystemName, LPCTSTR | |||
21 | typedef BOOL (WINAPI * Func_AdjustTokenPrivileges)(HANDLE TokenHandle, BOOL DisableAllPrivileges, | 21 | typedef BOOL (WINAPI * Func_AdjustTokenPrivileges)(HANDLE TokenHandle, BOOL DisableAllPrivileges, |
22 | PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength); | 22 | PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength); |
23 | } | 23 | } |
24 | #define GET_PROC_ADDR(fff, name) Func_ ## fff my_ ## fff = (Func_ ## fff)GetProcAddress(hModule, name) | 24 | #define GET_PROC_ADDR(fff, name) Func_ ## fff my_ ## fff = (Func_ ## fff) (void(*)()) GetProcAddress(hModule, name) |
25 | #endif | 25 | #endif |
26 | 26 | ||
27 | bool EnablePrivilege(LPCTSTR privilegeName, bool enable) | 27 | bool EnablePrivilege(LPCTSTR privilegeName, bool enable) |
diff --git a/CPP/Windows/SecurityUtils.cpp b/CPP/Windows/SecurityUtils.cpp index 8a7f45c..ede83fa 100644 --- a/CPP/Windows/SecurityUtils.cpp +++ b/CPP/Windows/SecurityUtils.cpp | |||
@@ -4,6 +4,9 @@ | |||
4 | 4 | ||
5 | #include "SecurityUtils.h" | 5 | #include "SecurityUtils.h" |
6 | 6 | ||
7 | #define MY_CAST_FUNC (void(*)()) | ||
8 | // #define MY_CAST_FUNC | ||
9 | |||
7 | namespace NWindows { | 10 | namespace NWindows { |
8 | namespace NSecurity { | 11 | namespace NSecurity { |
9 | 12 | ||
@@ -50,8 +53,10 @@ static void MyLookupSids(CPolicy &policy, PSID ps) | |||
50 | } | 53 | } |
51 | */ | 54 | */ |
52 | 55 | ||
56 | extern "C" { | ||
57 | |||
53 | #ifndef _UNICODE | 58 | #ifndef _UNICODE |
54 | typedef BOOL (WINAPI * LookupAccountNameWP)( | 59 | typedef BOOL (WINAPI * Func_LookupAccountNameW)( |
55 | LPCWSTR lpSystemName, | 60 | LPCWSTR lpSystemName, |
56 | LPCWSTR lpAccountName, | 61 | LPCWSTR lpAccountName, |
57 | PSID Sid, | 62 | PSID Sid, |
@@ -62,13 +67,17 @@ typedef BOOL (WINAPI * LookupAccountNameWP)( | |||
62 | ); | 67 | ); |
63 | #endif | 68 | #endif |
64 | 69 | ||
70 | } | ||
71 | |||
65 | static PSID GetSid(LPWSTR accountName) | 72 | static PSID GetSid(LPWSTR accountName) |
66 | { | 73 | { |
67 | #ifndef _UNICODE | 74 | #ifndef _UNICODE |
68 | HMODULE hModule = GetModuleHandle(TEXT("Advapi32.dll")); | 75 | HMODULE hModule = GetModuleHandle(TEXT("Advapi32.dll")); |
69 | if (hModule == NULL) | 76 | if (hModule == NULL) |
70 | return NULL; | 77 | return NULL; |
71 | LookupAccountNameWP lookupAccountNameW = (LookupAccountNameWP)GetProcAddress(hModule, "LookupAccountNameW"); | 78 | Func_LookupAccountNameW lookupAccountNameW = (Func_LookupAccountNameW) |
79 | MY_CAST_FUNC | ||
80 | GetProcAddress(hModule, "LookupAccountNameW"); | ||
72 | if (lookupAccountNameW == NULL) | 81 | if (lookupAccountNameW == NULL) |
73 | return NULL; | 82 | return NULL; |
74 | #endif | 83 | #endif |
diff --git a/CPP/Windows/SecurityUtils.h b/CPP/Windows/SecurityUtils.h index 8966dfd..c0d7b12 100644 --- a/CPP/Windows/SecurityUtils.h +++ b/CPP/Windows/SecurityUtils.h | |||
@@ -7,6 +7,31 @@ | |||
7 | 7 | ||
8 | #include "Defs.h" | 8 | #include "Defs.h" |
9 | 9 | ||
10 | #ifndef _UNICODE | ||
11 | |||
12 | extern "C" { | ||
13 | typedef NTSTATUS (NTAPI *Func_LsaOpenPolicy)(PLSA_UNICODE_STRING SystemName, | ||
14 | PLSA_OBJECT_ATTRIBUTES ObjectAttributes, ACCESS_MASK DesiredAccess, PLSA_HANDLE PolicyHandle); | ||
15 | typedef NTSTATUS (NTAPI *Func_LsaClose)(LSA_HANDLE ObjectHandle); | ||
16 | typedef NTSTATUS (NTAPI *Func_LsaAddAccountRights)(LSA_HANDLE PolicyHandle, | ||
17 | PSID AccountSid, PLSA_UNICODE_STRING UserRights, ULONG CountOfRights ); | ||
18 | #define MY_STATUS_NOT_IMPLEMENTED ((NTSTATUS)0xC0000002L) | ||
19 | } | ||
20 | |||
21 | #define POLICY_FUNC_CALL(fff, str) \ | ||
22 | if (hModule == NULL) return MY_STATUS_NOT_IMPLEMENTED; \ | ||
23 | Func_ ## fff v = (Func_ ## fff) (void(*)()) GetProcAddress(hModule, str); \ | ||
24 | if (!v) return MY_STATUS_NOT_IMPLEMENTED; \ | ||
25 | const NTSTATUS res = v | ||
26 | |||
27 | #else | ||
28 | |||
29 | #define POLICY_FUNC_CALL(fff, str) \ | ||
30 | const NTSTATUS res = ::fff | ||
31 | |||
32 | #endif | ||
33 | |||
34 | |||
10 | namespace NWindows { | 35 | namespace NWindows { |
11 | namespace NSecurity { | 36 | namespace NSecurity { |
12 | 37 | ||
@@ -53,15 +78,9 @@ public: | |||
53 | 78 | ||
54 | }; | 79 | }; |
55 | 80 | ||
56 | #ifndef _UNICODE | ||
57 | typedef NTSTATUS (NTAPI *LsaOpenPolicyP)(PLSA_UNICODE_STRING SystemName, | ||
58 | PLSA_OBJECT_ATTRIBUTES ObjectAttributes, ACCESS_MASK DesiredAccess, PLSA_HANDLE PolicyHandle); | ||
59 | typedef NTSTATUS (NTAPI *LsaCloseP)(LSA_HANDLE ObjectHandle); | ||
60 | typedef NTSTATUS (NTAPI *LsaAddAccountRightsP)(LSA_HANDLE PolicyHandle, | ||
61 | PSID AccountSid, PLSA_UNICODE_STRING UserRights, ULONG CountOfRights ); | ||
62 | #define MY_STATUS_NOT_IMPLEMENTED ((NTSTATUS)0xC0000002L) | ||
63 | #endif | ||
64 | 81 | ||
82 | |||
83 | |||
65 | struct CPolicy | 84 | struct CPolicy |
66 | { | 85 | { |
67 | protected: | 86 | protected: |
@@ -82,43 +101,17 @@ public: | |||
82 | NTSTATUS Open(PLSA_UNICODE_STRING systemName, PLSA_OBJECT_ATTRIBUTES objectAttributes, | 101 | NTSTATUS Open(PLSA_UNICODE_STRING systemName, PLSA_OBJECT_ATTRIBUTES objectAttributes, |
83 | ACCESS_MASK desiredAccess) | 102 | ACCESS_MASK desiredAccess) |
84 | { | 103 | { |
85 | #ifndef _UNICODE | ||
86 | if (hModule == NULL) | ||
87 | return MY_STATUS_NOT_IMPLEMENTED; | ||
88 | LsaOpenPolicyP lsaOpenPolicy = (LsaOpenPolicyP)GetProcAddress(hModule, "LsaOpenPolicy"); | ||
89 | if (lsaOpenPolicy == NULL) | ||
90 | return MY_STATUS_NOT_IMPLEMENTED; | ||
91 | #endif | ||
92 | |||
93 | Close(); | 104 | Close(); |
94 | return | 105 | POLICY_FUNC_CALL (LsaOpenPolicy, "LsaOpenPolicy") |
95 | #ifdef _UNICODE | ||
96 | ::LsaOpenPolicy | ||
97 | #else | ||
98 | lsaOpenPolicy | ||
99 | #endif | ||
100 | (systemName, objectAttributes, desiredAccess, &_handle); | 106 | (systemName, objectAttributes, desiredAccess, &_handle); |
107 | return res; | ||
101 | } | 108 | } |
102 | 109 | ||
103 | NTSTATUS Close() | 110 | NTSTATUS Close() |
104 | { | 111 | { |
105 | if (_handle == NULL) | 112 | if (_handle == NULL) |
106 | return 0; | 113 | return 0; |
107 | 114 | POLICY_FUNC_CALL (LsaClose, "LsaClose") | |
108 | #ifndef _UNICODE | ||
109 | if (hModule == NULL) | ||
110 | return MY_STATUS_NOT_IMPLEMENTED; | ||
111 | LsaCloseP lsaClose = (LsaCloseP)GetProcAddress(hModule, "LsaClose"); | ||
112 | if (lsaClose == NULL) | ||
113 | return MY_STATUS_NOT_IMPLEMENTED; | ||
114 | #endif | ||
115 | |||
116 | NTSTATUS res = | ||
117 | #ifdef _UNICODE | ||
118 | ::LsaClose | ||
119 | #else | ||
120 | lsaClose | ||
121 | #endif | ||
122 | (_handle); | 115 | (_handle); |
123 | _handle = NULL; | 116 | _handle = NULL; |
124 | return res; | 117 | return res; |
@@ -137,21 +130,9 @@ public: | |||
137 | 130 | ||
138 | NTSTATUS AddAccountRights(PSID accountSid, PLSA_UNICODE_STRING userRights, ULONG countOfRights) | 131 | NTSTATUS AddAccountRights(PSID accountSid, PLSA_UNICODE_STRING userRights, ULONG countOfRights) |
139 | { | 132 | { |
140 | #ifndef _UNICODE | 133 | POLICY_FUNC_CALL (LsaAddAccountRights, "LsaAddAccountRights") |
141 | if (hModule == NULL) | ||
142 | return MY_STATUS_NOT_IMPLEMENTED; | ||
143 | LsaAddAccountRightsP lsaAddAccountRights = (LsaAddAccountRightsP)GetProcAddress(hModule, "LsaAddAccountRights"); | ||
144 | if (lsaAddAccountRights == NULL) | ||
145 | return MY_STATUS_NOT_IMPLEMENTED; | ||
146 | #endif | ||
147 | |||
148 | return | ||
149 | #ifdef _UNICODE | ||
150 | ::LsaAddAccountRights | ||
151 | #else | ||
152 | lsaAddAccountRights | ||
153 | #endif | ||
154 | (_handle, accountSid, userRights, countOfRights); | 134 | (_handle, accountSid, userRights, countOfRights); |
135 | return res; | ||
155 | } | 136 | } |
156 | NTSTATUS AddAccountRights(PSID accountSid, PLSA_UNICODE_STRING userRights) | 137 | NTSTATUS AddAccountRights(PSID accountSid, PLSA_UNICODE_STRING userRights) |
157 | { return AddAccountRights(accountSid, userRights, 1); } | 138 | { return AddAccountRights(accountSid, userRights, 1); } |
diff --git a/CPP/Windows/Shell.cpp b/CPP/Windows/Shell.cpp index d0f9032..071833c 100644 --- a/CPP/Windows/Shell.cpp +++ b/CPP/Windows/Shell.cpp | |||
@@ -258,14 +258,21 @@ bool BrowseForFolder(HWND owner, LPCTSTR title, | |||
258 | 258 | ||
259 | #ifndef _UNICODE | 259 | #ifndef _UNICODE |
260 | 260 | ||
261 | typedef BOOL (WINAPI * SHGetPathFromIDListWP)(LPCITEMIDLIST pidl, LPWSTR pszPath); | 261 | extern "C" { |
262 | typedef BOOL (WINAPI * Func_SHGetPathFromIDListW)(LPCITEMIDLIST pidl, LPWSTR pszPath); | ||
263 | typedef LPITEMIDLIST (WINAPI * Func_SHBrowseForFolderW)(LPBROWSEINFOW lpbi); | ||
264 | } | ||
265 | |||
266 | #define MY_CAST_FUNC (void(*)()) | ||
267 | // #define MY_CAST_FUNC | ||
262 | 268 | ||
263 | bool GetPathFromIDList(LPCITEMIDLIST itemIDList, UString &path) | 269 | bool GetPathFromIDList(LPCITEMIDLIST itemIDList, UString &path) |
264 | { | 270 | { |
265 | path.Empty(); | 271 | path.Empty(); |
266 | SHGetPathFromIDListWP shGetPathFromIDListW = (SHGetPathFromIDListWP) | 272 | Func_SHGetPathFromIDListW shGetPathFromIDListW = (Func_SHGetPathFromIDListW) |
273 | MY_CAST_FUNC | ||
267 | ::GetProcAddress(::GetModuleHandleW(L"shell32.dll"), "SHGetPathFromIDListW"); | 274 | ::GetProcAddress(::GetModuleHandleW(L"shell32.dll"), "SHGetPathFromIDListW"); |
268 | if (shGetPathFromIDListW == 0) | 275 | if (!shGetPathFromIDListW) |
269 | return false; | 276 | return false; |
270 | const unsigned len = MAX_PATH * 2; | 277 | const unsigned len = MAX_PATH * 2; |
271 | bool result = BOOLToBool(shGetPathFromIDListW(itemIDList, path.GetBuf(len))); | 278 | bool result = BOOLToBool(shGetPathFromIDListW(itemIDList, path.GetBuf(len))); |
@@ -273,14 +280,14 @@ bool GetPathFromIDList(LPCITEMIDLIST itemIDList, UString &path) | |||
273 | return result; | 280 | return result; |
274 | } | 281 | } |
275 | 282 | ||
276 | typedef LPITEMIDLIST (WINAPI * SHBrowseForFolderWP)(LPBROWSEINFOW lpbi); | ||
277 | 283 | ||
278 | static bool BrowseForFolder(LPBROWSEINFOW browseInfo, UString &resultPath) | 284 | static bool BrowseForFolder(LPBROWSEINFOW browseInfo, UString &resultPath) |
279 | { | 285 | { |
280 | NWindows::NCOM::CComInitializer comInitializer; | 286 | NWindows::NCOM::CComInitializer comInitializer; |
281 | SHBrowseForFolderWP shBrowseForFolderW = (SHBrowseForFolderWP) | 287 | Func_SHBrowseForFolderW shBrowseForFolderW = (Func_SHBrowseForFolderW) |
288 | MY_CAST_FUNC | ||
282 | ::GetProcAddress(::GetModuleHandleW(L"shell32.dll"), "SHBrowseForFolderW"); | 289 | ::GetProcAddress(::GetModuleHandleW(L"shell32.dll"), "SHBrowseForFolderW"); |
283 | if (shBrowseForFolderW == 0) | 290 | if (!shBrowseForFolderW) |
284 | return false; | 291 | return false; |
285 | LPITEMIDLIST itemIDList = shBrowseForFolderW(browseInfo); | 292 | LPITEMIDLIST itemIDList = shBrowseForFolderW(browseInfo); |
286 | if (itemIDList == NULL) | 293 | if (itemIDList == NULL) |