-
Notifications
You must be signed in to change notification settings - Fork 256
/
winapi.cc
262 lines (210 loc) · 9.27 KB
/
winapi.cc
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2003-2008 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
// Kern Sibbald MMIII
/* @file
* Windows APIs that are different for each system.
* We use pointers to the entry points so that a
* single binary will run on all Windows systems.
*/
#include "include/bareos.h"
// Init with win9x, but maybe set to NT in InitWinAPI
DWORD g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
DWORD g_MinorVersion = 0;
DWORD g_MajorVersion = 0;
// API Pointers
t_OpenProcessToken p_OpenProcessToken = NULL;
t_AdjustTokenPrivileges p_AdjustTokenPrivileges = NULL;
t_LookupPrivilegeValue p_LookupPrivilegeValue = NULL;
t_SetProcessShutdownParameters p_SetProcessShutdownParameters = NULL;
t_CreateFileA p_CreateFileA = NULL;
t_CreateFileW p_CreateFileW = NULL;
t_CreateDirectoryA p_CreateDirectoryA = NULL;
t_CreateDirectoryW p_CreateDirectoryW = NULL;
t_CreateSymbolicLinkA p_CreateSymbolicLinkA = NULL;
t_CreateSymbolicLinkW p_CreateSymbolicLinkW = NULL;
t_OpenEncryptedFileRawA p_OpenEncryptedFileRawA = NULL;
t_OpenEncryptedFileRawW p_OpenEncryptedFileRawW = NULL;
t_ReadEncryptedFileRaw p_ReadEncryptedFileRaw = NULL;
t_WriteEncryptedFileRaw p_WriteEncryptedFileRaw = NULL;
t_CloseEncryptedFileRaw p_CloseEncryptedFileRaw = NULL;
t_wunlink p_wunlink = NULL;
t_wmkdir p_wmkdir = NULL;
#if (_WIN32_WINNT >= 0x0600)
t_GetFileInformationByHandleEx p_GetFileInformationByHandleEx = NULL;
#endif
t_GetFileAttributesA p_GetFileAttributesA = NULL;
t_GetFileAttributesW p_GetFileAttributesW = NULL;
t_GetFileAttributesExA p_GetFileAttributesExA = NULL;
t_GetFileAttributesExW p_GetFileAttributesExW = NULL;
t_SetFileAttributesA p_SetFileAttributesA = NULL;
t_SetFileAttributesW p_SetFileAttributesW = NULL;
t_BackupRead p_BackupRead = NULL;
t_BackupWrite p_BackupWrite = NULL;
t_WideCharToMultiByte p_WideCharToMultiByte = NULL;
t_MultiByteToWideChar p_MultiByteToWideChar = NULL;
t_AttachConsole p_AttachConsole = NULL;
t_FindFirstFileA p_FindFirstFileA = NULL;
t_FindFirstFileW p_FindFirstFileW = NULL;
t_FindNextFileA p_FindNextFileA = NULL;
t_FindNextFileW p_FindNextFileW = NULL;
t_SetCurrentDirectoryA p_SetCurrentDirectoryA = NULL;
t_SetCurrentDirectoryW p_SetCurrentDirectoryW = NULL;
t_GetCurrentDirectoryA p_GetCurrentDirectoryA = NULL;
t_GetCurrentDirectoryW p_GetCurrentDirectoryW = NULL;
t_GetVolumePathNameW p_GetVolumePathNameW = NULL;
t_GetVolumeNameForVolumeMountPointW p_GetVolumeNameForVolumeMountPointW = NULL;
t_SHGetFolderPath p_SHGetFolderPath = NULL;
t_CreateProcessA p_CreateProcessA = NULL;
t_CreateProcessW p_CreateProcessW = NULL;
t_GetLogicalDriveStringsA p_GetLogicalDriveStringsA = NULL;
t_GetLogicalDriveStringsW p_GetLogicalDriveStringsW = NULL;
void InitWinAPIWrapper()
{
OSVERSIONINFO osversioninfo = {sizeof(OSVERSIONINFO), 0, 0, 0, 0, 0};
// Get the current OS version
if (!GetVersionEx(&osversioninfo)) {
g_platform_id = 0;
} else {
g_platform_id = osversioninfo.dwPlatformId;
g_MinorVersion = osversioninfo.dwMinorVersion;
g_MajorVersion = osversioninfo.dwMajorVersion;
}
HMODULE hLib = LoadLibraryA("KERNEL32.DLL");
if (hLib) {
// Get logical drive calls
p_GetLogicalDriveStringsA = (t_GetLogicalDriveStringsA)GetProcAddress(
hLib, "GetLogicalDriveStringsA");
p_GetLogicalDriveStringsW = (t_GetLogicalDriveStringsW)GetProcAddress(
hLib, "GetLogicalDriveStringsW");
// Create process calls
p_CreateProcessA = (t_CreateProcessA)GetProcAddress(hLib, "CreateProcessA");
p_CreateProcessW = (t_CreateProcessW)GetProcAddress(hLib, "CreateProcessW");
// Create file calls
p_CreateFileA = (t_CreateFileA)GetProcAddress(hLib, "CreateFileA");
p_CreateDirectoryA
= (t_CreateDirectoryA)GetProcAddress(hLib, "CreateDirectoryA");
p_CreateSymbolicLinkA
= (t_CreateSymbolicLinkA)GetProcAddress(hLib, "CreateSymbolicLinkA");
#if (_WIN32_WINNT >= 0x0600)
// File Information calls
p_GetFileInformationByHandleEx
= (t_GetFileInformationByHandleEx)GetProcAddress(
hLib, "GetFileInformationByHandleEx");
#endif
// Attribute calls
p_GetFileAttributesA
= (t_GetFileAttributesA)GetProcAddress(hLib, "GetFileAttributesA");
p_GetFileAttributesExA
= (t_GetFileAttributesExA)GetProcAddress(hLib, "GetFileAttributesExA");
p_SetFileAttributesA
= (t_SetFileAttributesA)GetProcAddress(hLib, "SetFileAttributesA");
// Process calls
p_SetProcessShutdownParameters
= (t_SetProcessShutdownParameters)GetProcAddress(
hLib, "SetProcessShutdownParameters");
// Char conversion calls
p_WideCharToMultiByte
= (t_WideCharToMultiByte)GetProcAddress(hLib, "WideCharToMultiByte");
p_MultiByteToWideChar
= (t_MultiByteToWideChar)GetProcAddress(hLib, "MultiByteToWideChar");
// Find files
p_FindFirstFileA = (t_FindFirstFileA)GetProcAddress(hLib, "FindFirstFileA");
p_FindNextFileA = (t_FindNextFileA)GetProcAddress(hLib, "FindNextFileA");
// Get and set directory
p_GetCurrentDirectoryA
= (t_GetCurrentDirectoryA)GetProcAddress(hLib, "GetCurrentDirectoryA");
p_SetCurrentDirectoryA
= (t_SetCurrentDirectoryA)GetProcAddress(hLib, "SetCurrentDirectoryA");
if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
p_CreateFileW = (t_CreateFileW)GetProcAddress(hLib, "CreateFileW");
p_CreateDirectoryW
= (t_CreateDirectoryW)GetProcAddress(hLib, "CreateDirectoryW");
p_CreateSymbolicLinkW
= (t_CreateSymbolicLinkW)GetProcAddress(hLib, "CreateSymbolicLinkW");
// Backup calls
p_BackupRead = (t_BackupRead)GetProcAddress(hLib, "BackupRead");
p_BackupWrite = (t_BackupWrite)GetProcAddress(hLib, "BackupWrite");
p_GetFileAttributesW
= (t_GetFileAttributesW)GetProcAddress(hLib, "GetFileAttributesW");
p_GetFileAttributesExW = (t_GetFileAttributesExW)GetProcAddress(
hLib, "GetFileAttributesExW");
p_SetFileAttributesW
= (t_SetFileAttributesW)GetProcAddress(hLib, "SetFileAttributesW");
p_FindFirstFileW
= (t_FindFirstFileW)GetProcAddress(hLib, "FindFirstFileW");
p_FindNextFileW = (t_FindNextFileW)GetProcAddress(hLib, "FindNextFileW");
p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)GetProcAddress(
hLib, "GetCurrentDirectoryW");
p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)GetProcAddress(
hLib, "SetCurrentDirectoryW");
/*
* Some special stuff we need for VSS
* but static linkage doesn't work on Win 9x
*/
p_GetVolumePathNameW
= (t_GetVolumePathNameW)GetProcAddress(hLib, "GetVolumePathNameW");
p_GetVolumeNameForVolumeMountPointW
= (t_GetVolumeNameForVolumeMountPointW)GetProcAddress(
hLib, "GetVolumeNameForVolumeMountPointW");
p_AttachConsole = (t_AttachConsole)GetProcAddress(hLib, "AttachConsole");
}
}
if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
hLib = LoadLibraryA("MSVCRT.DLL");
if (hLib) {
/* unlink */
p_wunlink = (t_wunlink)GetProcAddress(hLib, "_wunlink");
/* wmkdir */
p_wmkdir = (t_wmkdir)GetProcAddress(hLib, "_wmkdir");
}
hLib = LoadLibraryA("ADVAPI32.DLL");
if (hLib) {
p_OpenProcessToken
= (t_OpenProcessToken)GetProcAddress(hLib, "OpenProcessToken");
p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)GetProcAddress(
hLib, "AdjustTokenPrivileges");
p_LookupPrivilegeValue = (t_LookupPrivilegeValue)GetProcAddress(
hLib, "LookupPrivilegeValueA");
// EFS calls
p_OpenEncryptedFileRawA = (t_OpenEncryptedFileRawA)GetProcAddress(
hLib, "OpenEncryptedFileRawA");
p_OpenEncryptedFileRawW = (t_OpenEncryptedFileRawW)GetProcAddress(
hLib, "OpenEncryptedFileRawW");
p_ReadEncryptedFileRaw = (t_ReadEncryptedFileRaw)GetProcAddress(
hLib, "ReadEncryptedFileRaw");
p_WriteEncryptedFileRaw = (t_WriteEncryptedFileRaw)GetProcAddress(
hLib, "WriteEncryptedFileRaw");
p_CloseEncryptedFileRaw = (t_CloseEncryptedFileRaw)GetProcAddress(
hLib, "CloseEncryptedFileRaw");
}
}
hLib = LoadLibraryA("SHELL32.DLL");
if (hLib) {
p_SHGetFolderPath
= (t_SHGetFolderPath)GetProcAddress(hLib, "SHGetFolderPathA");
} else {
// If SHELL32 isn't found try SHFOLDER for older systems
hLib = LoadLibraryA("SHFOLDER.DLL");
if (hLib) {
p_SHGetFolderPath
= (t_SHGetFolderPath)GetProcAddress(hLib, "SHGetFolderPathA");
}
}
atexit(Win32TSDCleanup);
}