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