• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   if(curlx_verify_windows_version(6, 0, PLATFORM_WINNT,
106                                   VERSION_GREATER_THAN_EQUAL)) {
107     Curl_isVistaOrGreater = TRUE;
108   }
109   else
110     Curl_isVistaOrGreater = FALSE;
111 
112   QueryPerformanceFrequency(&Curl_freq);
113   return CURLE_OK;
114 }
115 
116 /* Curl_win32_cleanup() is the opposite of Curl_win32_init() */
Curl_win32_cleanup(long init_flags)117 void Curl_win32_cleanup(long init_flags)
118 {
119   if(s_hIpHlpApiDll) {
120     FreeLibrary(s_hIpHlpApiDll);
121     s_hIpHlpApiDll = NULL;
122     Curl_if_nametoindex = NULL;
123   }
124 
125 #ifdef USE_WINDOWS_SSPI
126   Curl_sspi_global_cleanup();
127 #endif
128 
129   if(init_flags & CURL_GLOBAL_WIN32) {
130 #ifdef USE_WINSOCK
131     WSACleanup();
132 #endif
133   }
134 }
135 
136 #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
137 #define LOAD_WITH_ALTERED_SEARCH_PATH  0x00000008
138 #endif
139 
140 #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
141 #define LOAD_LIBRARY_SEARCH_SYSTEM32   0x00000800
142 #endif
143 
144 /* We use our own typedef here since some headers might lack these */
145 typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
146 
147 /* See function definitions in winbase.h */
148 #ifdef UNICODE
149 #  ifdef _WIN32_WCE
150 #    define LOADLIBARYEX  L"LoadLibraryExW"
151 #  else
152 #    define LOADLIBARYEX  "LoadLibraryExW"
153 #  endif
154 #else
155 #  define LOADLIBARYEX    "LoadLibraryExA"
156 #endif
157 
158 /*
159  * Curl_load_library()
160  *
161  * This is used to dynamically load DLLs using the most secure method available
162  * for the version of Windows that we are running on.
163  *
164  * Parameters:
165  *
166  * filename  [in] - The filename or full path of the DLL to load. If only the
167  *                  filename is passed then the DLL will be loaded from the
168  *                  Windows system directory.
169  *
170  * Returns the handle of the module on success; otherwise NULL.
171  */
Curl_load_library(LPCTSTR filename)172 HMODULE Curl_load_library(LPCTSTR filename)
173 {
174 #ifndef CURL_WINDOWS_APP
175   HMODULE hModule = NULL;
176   LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
177 
178   /* Get a handle to kernel32 so we can access it's functions at runtime */
179   HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
180   if(!hKernel32)
181     return NULL;
182 
183   /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
184      and above */
185   pLoadLibraryEx =
186     CURLX_FUNCTION_CAST(LOADLIBRARYEX_FN,
187                         (GetProcAddress(hKernel32, LOADLIBARYEX)));
188 
189   /* Detect if there's already a path in the filename and load the library if
190      there is. Note: Both back slashes and forward slashes have been supported
191      since the earlier days of DOS at an API level although they are not
192      supported by command prompt */
193   if(_tcspbrk(filename, TEXT("\\/"))) {
194     /** !checksrc! disable BANNEDFUNC 1 **/
195     hModule = pLoadLibraryEx ?
196       pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
197       LoadLibrary(filename);
198   }
199   /* Detect if KB2533623 is installed, as LOAD_LIBRARY_SEARCH_SYSTEM32 is only
200      supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
201      Server 2008 R2 with this patch or natively on Windows 8 and above */
202   else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
203     /* Load the DLL from the Windows system directory */
204     hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
205   }
206   else {
207     /* Attempt to get the Windows system path */
208     UINT systemdirlen = GetSystemDirectory(NULL, 0);
209     if(systemdirlen) {
210       /* Allocate space for the full DLL path (Room for the null terminator
211          is included in systemdirlen) */
212       size_t filenamelen = _tcslen(filename);
213       TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
214       if(path && GetSystemDirectory(path, systemdirlen)) {
215         /* Calculate the full DLL path */
216         _tcscpy(path + _tcslen(path), TEXT("\\"));
217         _tcscpy(path + _tcslen(path), filename);
218 
219         /* Load the DLL from the Windows system directory */
220         /** !checksrc! disable BANNEDFUNC 1 **/
221         hModule = pLoadLibraryEx ?
222           pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
223           LoadLibrary(path);
224 
225       }
226       free(path);
227     }
228   }
229   return hModule;
230 #else
231   /* the Universal Windows Platform (UWP) can't do this */
232   (void)filename;
233   return NULL;
234 #endif
235 }
236 
237 #endif /* WIN32 */
238