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