1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2016 - 2021, Steve Holme, <steve_holme@hotmail.com>.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #if defined(WIN32)
26
27 #include <curl/curl.h>
28 #include "system_win32.h"
29 #include "version_win32.h"
30 #include "curl_sspi.h"
31 #include "warnless.h"
32
33 /* The last #include files should be: */
34 #include "curl_memory.h"
35 #include "memdebug.h"
36
37 LARGE_INTEGER Curl_freq;
38 bool Curl_isVistaOrGreater;
39
40 /* Handle of iphlpapp.dll */
41 static HMODULE s_hIpHlpApiDll = NULL;
42
43 /* Pointer to the if_nametoindex function */
44 IF_NAMETOINDEX_FN Curl_if_nametoindex = NULL;
45
46 /* Curl_win32_init() performs win32 global initialization */
Curl_win32_init(long flags)47 CURLcode Curl_win32_init(long flags)
48 {
49 /* CURL_GLOBAL_WIN32 controls the *optional* part of the initialization which
50 is just for Winsock at the moment. Any required win32 initialization
51 should take place after this block. */
52 if(flags & CURL_GLOBAL_WIN32) {
53 #ifdef USE_WINSOCK
54 WORD wVersionRequested;
55 WSADATA wsaData;
56 int res;
57
58 wVersionRequested = MAKEWORD(2, 2);
59 res = WSAStartup(wVersionRequested, &wsaData);
60
61 if(res)
62 /* Tell the user that we couldn't find a usable */
63 /* winsock.dll. */
64 return CURLE_FAILED_INIT;
65
66 /* Confirm that the Windows Sockets DLL supports what we need.*/
67 /* Note that if the DLL supports versions greater */
68 /* than wVersionRequested, it will still return */
69 /* wVersionRequested in wVersion. wHighVersion contains the */
70 /* highest supported version. */
71
72 if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
73 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
74 /* Tell the user that we couldn't find a usable */
75
76 /* winsock.dll. */
77 WSACleanup();
78 return CURLE_FAILED_INIT;
79 }
80 /* The Windows Sockets DLL is acceptable. Proceed. */
81 #elif defined(USE_LWIPSOCK)
82 lwip_init();
83 #endif
84 } /* CURL_GLOBAL_WIN32 */
85
86 #ifdef USE_WINDOWS_SSPI
87 {
88 CURLcode result = Curl_sspi_global_init();
89 if(result)
90 return result;
91 }
92 #endif
93
94 s_hIpHlpApiDll = Curl_load_library(TEXT("iphlpapi.dll"));
95 if(s_hIpHlpApiDll) {
96 /* Get the address of the if_nametoindex function */
97 IF_NAMETOINDEX_FN pIfNameToIndex =
98 CURLX_FUNCTION_CAST(IF_NAMETOINDEX_FN,
99 (GetProcAddress(s_hIpHlpApiDll, "if_nametoindex")));
100
101 if(pIfNameToIndex)
102 Curl_if_nametoindex = pIfNameToIndex;
103 }
104
105 /* curlx_verify_windows_version must be called during init at least once
106 because it has its own initialization routine. */
107 if(curlx_verify_windows_version(6, 0, PLATFORM_WINNT,
108 VERSION_GREATER_THAN_EQUAL)) {
109 Curl_isVistaOrGreater = TRUE;
110 }
111 else
112 Curl_isVistaOrGreater = FALSE;
113
114 QueryPerformanceFrequency(&Curl_freq);
115 return CURLE_OK;
116 }
117
118 /* Curl_win32_cleanup() is the opposite of Curl_win32_init() */
Curl_win32_cleanup(long init_flags)119 void Curl_win32_cleanup(long init_flags)
120 {
121 if(s_hIpHlpApiDll) {
122 FreeLibrary(s_hIpHlpApiDll);
123 s_hIpHlpApiDll = NULL;
124 Curl_if_nametoindex = NULL;
125 }
126
127 #ifdef USE_WINDOWS_SSPI
128 Curl_sspi_global_cleanup();
129 #endif
130
131 if(init_flags & CURL_GLOBAL_WIN32) {
132 #ifdef USE_WINSOCK
133 WSACleanup();
134 #endif
135 }
136 }
137
138 #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
139 #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
140 #endif
141
142 #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
143 #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
144 #endif
145
146 /* We use our own typedef here since some headers might lack these */
147 typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
148
149 /* See function definitions in winbase.h */
150 #ifdef UNICODE
151 # ifdef _WIN32_WCE
152 # define LOADLIBARYEX L"LoadLibraryExW"
153 # else
154 # define LOADLIBARYEX "LoadLibraryExW"
155 # endif
156 #else
157 # define LOADLIBARYEX "LoadLibraryExA"
158 #endif
159
160 /*
161 * Curl_load_library()
162 *
163 * This is used to dynamically load DLLs using the most secure method available
164 * for the version of Windows that we are running on.
165 *
166 * Parameters:
167 *
168 * filename [in] - The filename or full path of the DLL to load. If only the
169 * filename is passed then the DLL will be loaded from the
170 * Windows system directory.
171 *
172 * Returns the handle of the module on success; otherwise NULL.
173 */
Curl_load_library(LPCTSTR filename)174 HMODULE Curl_load_library(LPCTSTR filename)
175 {
176 #ifndef CURL_WINDOWS_APP
177 HMODULE hModule = NULL;
178 LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
179
180 /* Get a handle to kernel32 so we can access it's functions at runtime */
181 HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
182 if(!hKernel32)
183 return NULL;
184
185 /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
186 and above */
187 pLoadLibraryEx =
188 CURLX_FUNCTION_CAST(LOADLIBRARYEX_FN,
189 (GetProcAddress(hKernel32, LOADLIBARYEX)));
190
191 /* Detect if there's already a path in the filename and load the library if
192 there is. Note: Both back slashes and forward slashes have been supported
193 since the earlier days of DOS at an API level although they are not
194 supported by command prompt */
195 if(_tcspbrk(filename, TEXT("\\/"))) {
196 /** !checksrc! disable BANNEDFUNC 1 **/
197 hModule = pLoadLibraryEx ?
198 pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
199 LoadLibrary(filename);
200 }
201 /* Detect if KB2533623 is installed, as LOAD_LIBRARY_SEARCH_SYSTEM32 is only
202 supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
203 Server 2008 R2 with this patch or natively on Windows 8 and above */
204 else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
205 /* Load the DLL from the Windows system directory */
206 hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
207 }
208 else {
209 /* Attempt to get the Windows system path */
210 UINT systemdirlen = GetSystemDirectory(NULL, 0);
211 if(systemdirlen) {
212 /* Allocate space for the full DLL path (Room for the null terminator
213 is included in systemdirlen) */
214 size_t filenamelen = _tcslen(filename);
215 TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
216 if(path && GetSystemDirectory(path, systemdirlen)) {
217 /* Calculate the full DLL path */
218 _tcscpy(path + _tcslen(path), TEXT("\\"));
219 _tcscpy(path + _tcslen(path), filename);
220
221 /* Load the DLL from the Windows system directory */
222 /** !checksrc! disable BANNEDFUNC 1 **/
223 hModule = pLoadLibraryEx ?
224 pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
225 LoadLibrary(path);
226
227 }
228 free(path);
229 }
230 }
231 return hModule;
232 #else
233 /* the Universal Windows Platform (UWP) can't do this */
234 (void)filename;
235 return NULL;
236 #endif
237 }
238
239 #endif /* WIN32 */
240