2
0

system_win32.c 8.7 KB

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