system_win32.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. ws2_32Dll = GetModuleHandleA("ws2_32");
  99. if(ws2_32Dll) {
  100. Curl_FreeAddrInfoExW = CURLX_FUNCTION_CAST(FREEADDRINFOEXW_FN,
  101. GetProcAddress(ws2_32Dll, "FreeAddrInfoExW"));
  102. Curl_GetAddrInfoExCancel = CURLX_FUNCTION_CAST(GETADDRINFOEXCANCEL_FN,
  103. GetProcAddress(ws2_32Dll, "GetAddrInfoExCancel"));
  104. Curl_GetAddrInfoExW = CURLX_FUNCTION_CAST(GETADDRINFOEXW_FN,
  105. GetProcAddress(ws2_32Dll, "GetAddrInfoExW"));
  106. }
  107. #endif
  108. /* curlx_verify_windows_version must be called during init at least once
  109. because it has its own initialization routine. */
  110. if(curlx_verify_windows_version(6, 0, 0, PLATFORM_WINNT,
  111. VERSION_GREATER_THAN_EQUAL)) {
  112. Curl_isVistaOrGreater = TRUE;
  113. }
  114. else
  115. Curl_isVistaOrGreater = FALSE;
  116. if(curlx_verify_windows_version(6, 2, 0, PLATFORM_WINNT,
  117. VERSION_GREATER_THAN_EQUAL)) {
  118. Curl_isWindows8OrGreater = TRUE;
  119. }
  120. else
  121. Curl_isWindows8OrGreater = FALSE;
  122. QueryPerformanceFrequency(&Curl_freq);
  123. return CURLE_OK;
  124. }
  125. /* Curl_win32_cleanup() is the opposite of Curl_win32_init() */
  126. void Curl_win32_cleanup(long init_flags)
  127. {
  128. Curl_FreeAddrInfoExW = NULL;
  129. Curl_GetAddrInfoExCancel = NULL;
  130. Curl_GetAddrInfoExW = NULL;
  131. if(s_hIpHlpApiDll) {
  132. FreeLibrary(s_hIpHlpApiDll);
  133. s_hIpHlpApiDll = NULL;
  134. Curl_if_nametoindex = NULL;
  135. }
  136. #ifdef USE_WINDOWS_SSPI
  137. Curl_sspi_global_cleanup();
  138. #endif
  139. if(init_flags & CURL_GLOBAL_WIN32) {
  140. #ifdef USE_WINSOCK
  141. WSACleanup();
  142. #endif
  143. }
  144. }
  145. #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
  146. #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
  147. #endif
  148. #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
  149. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
  150. #endif
  151. /* We use our own typedef here since some headers might lack these */
  152. typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
  153. /* See function definitions in winbase.h */
  154. #ifdef UNICODE
  155. # ifdef _WIN32_WCE
  156. # define LOADLIBARYEX L"LoadLibraryExW"
  157. # else
  158. # define LOADLIBARYEX "LoadLibraryExW"
  159. # endif
  160. #else
  161. # define LOADLIBARYEX "LoadLibraryExA"
  162. #endif
  163. /*
  164. * Curl_load_library()
  165. *
  166. * This is used to dynamically load DLLs using the most secure method available
  167. * for the version of Windows that we are running on.
  168. *
  169. * Parameters:
  170. *
  171. * filename [in] - The filename or full path of the DLL to load. If only the
  172. * filename is passed then the DLL will be loaded from the
  173. * Windows system directory.
  174. *
  175. * Returns the handle of the module on success; otherwise NULL.
  176. */
  177. HMODULE Curl_load_library(LPCTSTR filename)
  178. {
  179. #ifndef CURL_WINDOWS_APP
  180. HMODULE hModule = NULL;
  181. LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
  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. /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
  187. and above */
  188. pLoadLibraryEx =
  189. CURLX_FUNCTION_CAST(LOADLIBRARYEX_FN,
  190. (GetProcAddress(hKernel32, LOADLIBARYEX)));
  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. /* Load the DLL from the Windows system directory */
  221. /** !checksrc! disable BANNEDFUNC 1 **/
  222. hModule = pLoadLibraryEx ?
  223. pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  224. LoadLibrary(path);
  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. bool Curl_win32_impersonating(void)
  237. {
  238. HANDLE token = NULL;
  239. if(OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &token)) {
  240. CloseHandle(token);
  241. return TRUE;
  242. }
  243. return FALSE;
  244. }
  245. #endif /* _WIN32 */