system_win32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2016 - 2017, 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.haxx.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. #include "curl_setup.h"
  23. #if defined(WIN32)
  24. #include <curl/curl.h>
  25. #include "system_win32.h"
  26. /* The last #include files should be: */
  27. #include "curl_memory.h"
  28. #include "memdebug.h"
  29. #if defined(USE_WINDOWS_SSPI) || (!defined(CURL_DISABLE_TELNET) && \
  30. defined(USE_WINSOCK))
  31. #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
  32. #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
  33. #endif
  34. #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
  35. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
  36. #endif
  37. /* We use our own typedef here since some headers might lack these */
  38. typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
  39. /* See function definitions in winbase.h */
  40. #ifdef UNICODE
  41. # ifdef _WIN32_WCE
  42. # define LOADLIBARYEX L"LoadLibraryExW"
  43. # else
  44. # define LOADLIBARYEX "LoadLibraryExW"
  45. # endif
  46. #else
  47. # define LOADLIBARYEX "LoadLibraryExA"
  48. #endif
  49. #endif /* USE_WINDOWS_SSPI || (!CURL_DISABLE_TELNET && USE_WINSOCK) */
  50. /*
  51. * Curl_verify_windows_version()
  52. *
  53. * This is used to verify if we are running on a specific windows version.
  54. *
  55. * Parameters:
  56. *
  57. * majorVersion [in] - The major version number.
  58. * minorVersion [in] - The minor version number.
  59. * platform [in] - The optional platform identifier.
  60. * condition [in] - The test condition used to specifier whether we are
  61. * checking a version less then, equal to or greater than
  62. * what is specified in the major and minor version
  63. * numbers.
  64. *
  65. * Returns TRUE if matched; otherwise FALSE.
  66. */
  67. bool Curl_verify_windows_version(const unsigned int majorVersion,
  68. const unsigned int minorVersion,
  69. const PlatformIdentifier platform,
  70. const VersionCondition condition)
  71. {
  72. bool matched = FALSE;
  73. #if defined(CURL_WINDOWS_APP)
  74. /* We have no way to determine the Windows version from Windows apps,
  75. so let's assume we're running on the target Windows version. */
  76. const WORD fullVersion = MAKEWORD(minorVersion, majorVersion);
  77. const WORD targetVersion = (WORD)_WIN32_WINNT;
  78. switch(condition) {
  79. case VERSION_LESS_THAN:
  80. matched = targetVersion < fullVersion;
  81. break;
  82. case VERSION_LESS_THAN_EQUAL:
  83. matched = targetVersion <= fullVersion;
  84. break;
  85. case VERSION_EQUAL:
  86. matched = targetVersion == fullVersion;
  87. break;
  88. case VERSION_GREATER_THAN_EQUAL:
  89. matched = targetVersion >= fullVersion;
  90. break;
  91. case VERSION_GREATER_THAN:
  92. matched = targetVersion > fullVersion;
  93. break;
  94. }
  95. if(matched && (platform == PLATFORM_WINDOWS)) {
  96. /* we're always running on PLATFORM_WINNT */
  97. matched = FALSE;
  98. }
  99. #elif !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_WIN2K) || \
  100. (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
  101. OSVERSIONINFO osver;
  102. memset(&osver, 0, sizeof(osver));
  103. osver.dwOSVersionInfoSize = sizeof(osver);
  104. /* Find out Windows version */
  105. if(GetVersionEx(&osver)) {
  106. /* Verify the Operating System version number */
  107. switch(condition) {
  108. case VERSION_LESS_THAN:
  109. if(osver.dwMajorVersion < majorVersion ||
  110. (osver.dwMajorVersion == majorVersion &&
  111. osver.dwMinorVersion < minorVersion))
  112. matched = TRUE;
  113. break;
  114. case VERSION_LESS_THAN_EQUAL:
  115. if(osver.dwMajorVersion <= majorVersion &&
  116. osver.dwMinorVersion <= minorVersion)
  117. matched = TRUE;
  118. break;
  119. case VERSION_EQUAL:
  120. if(osver.dwMajorVersion == majorVersion &&
  121. osver.dwMinorVersion == minorVersion)
  122. matched = TRUE;
  123. break;
  124. case VERSION_GREATER_THAN_EQUAL:
  125. if(osver.dwMajorVersion >= majorVersion &&
  126. osver.dwMinorVersion >= minorVersion)
  127. matched = TRUE;
  128. break;
  129. case VERSION_GREATER_THAN:
  130. if(osver.dwMajorVersion > majorVersion ||
  131. (osver.dwMajorVersion == majorVersion &&
  132. osver.dwMinorVersion > minorVersion))
  133. matched = TRUE;
  134. break;
  135. }
  136. /* Verify the platform identifier (if necessary) */
  137. if(matched) {
  138. switch(platform) {
  139. case PLATFORM_WINDOWS:
  140. if(osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
  141. matched = FALSE;
  142. break;
  143. case PLATFORM_WINNT:
  144. if(osver.dwPlatformId != VER_PLATFORM_WIN32_NT)
  145. matched = FALSE;
  146. default: /* like platform == PLATFORM_DONT_CARE */
  147. break;
  148. }
  149. }
  150. }
  151. #else
  152. ULONGLONG cm = 0;
  153. OSVERSIONINFOEX osver;
  154. BYTE majorCondition;
  155. BYTE minorCondition;
  156. BYTE spMajorCondition;
  157. BYTE spMinorCondition;
  158. switch(condition) {
  159. case VERSION_LESS_THAN:
  160. majorCondition = VER_LESS;
  161. minorCondition = VER_LESS;
  162. spMajorCondition = VER_LESS_EQUAL;
  163. spMinorCondition = VER_LESS_EQUAL;
  164. break;
  165. case VERSION_LESS_THAN_EQUAL:
  166. majorCondition = VER_LESS_EQUAL;
  167. minorCondition = VER_LESS_EQUAL;
  168. spMajorCondition = VER_LESS_EQUAL;
  169. spMinorCondition = VER_LESS_EQUAL;
  170. break;
  171. case VERSION_EQUAL:
  172. majorCondition = VER_EQUAL;
  173. minorCondition = VER_EQUAL;
  174. spMajorCondition = VER_GREATER_EQUAL;
  175. spMinorCondition = VER_GREATER_EQUAL;
  176. break;
  177. case VERSION_GREATER_THAN_EQUAL:
  178. majorCondition = VER_GREATER_EQUAL;
  179. minorCondition = VER_GREATER_EQUAL;
  180. spMajorCondition = VER_GREATER_EQUAL;
  181. spMinorCondition = VER_GREATER_EQUAL;
  182. break;
  183. case VERSION_GREATER_THAN:
  184. majorCondition = VER_GREATER;
  185. minorCondition = VER_GREATER;
  186. spMajorCondition = VER_GREATER_EQUAL;
  187. spMinorCondition = VER_GREATER_EQUAL;
  188. break;
  189. default:
  190. return FALSE;
  191. }
  192. memset(&osver, 0, sizeof(osver));
  193. osver.dwOSVersionInfoSize = sizeof(osver);
  194. osver.dwMajorVersion = majorVersion;
  195. osver.dwMinorVersion = minorVersion;
  196. if(platform == PLATFORM_WINDOWS)
  197. osver.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
  198. else if(platform == PLATFORM_WINNT)
  199. osver.dwPlatformId = VER_PLATFORM_WIN32_NT;
  200. cm = VerSetConditionMask(cm, VER_MAJORVERSION, majorCondition);
  201. cm = VerSetConditionMask(cm, VER_MINORVERSION, minorCondition);
  202. cm = VerSetConditionMask(cm, VER_SERVICEPACKMAJOR, spMajorCondition);
  203. cm = VerSetConditionMask(cm, VER_SERVICEPACKMINOR, spMinorCondition);
  204. if(platform != PLATFORM_DONT_CARE)
  205. cm = VerSetConditionMask(cm, VER_PLATFORMID, VER_EQUAL);
  206. if(VerifyVersionInfo(&osver, (VER_MAJORVERSION | VER_MINORVERSION |
  207. VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR),
  208. cm))
  209. matched = TRUE;
  210. #endif
  211. return matched;
  212. }
  213. #if defined(USE_WINDOWS_SSPI) || (!defined(CURL_DISABLE_TELNET) && \
  214. defined(USE_WINSOCK))
  215. /*
  216. * Curl_load_library()
  217. *
  218. * This is used to dynamically load DLLs using the most secure method available
  219. * for the version of Windows that we are running on.
  220. *
  221. * Parameters:
  222. *
  223. * filename [in] - The filename or full path of the DLL to load. If only the
  224. * filename is passed then the DLL will be loaded from the
  225. * Windows system directory.
  226. *
  227. * Returns the handle of the module on success; otherwise NULL.
  228. */
  229. HMODULE Curl_load_library(LPCTSTR filename)
  230. {
  231. HMODULE hModule = NULL;
  232. LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
  233. /* Get a handle to kernel32 so we can access it's functions at runtime */
  234. HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
  235. if(!hKernel32)
  236. return NULL;
  237. /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
  238. and above */
  239. pLoadLibraryEx = (LOADLIBRARYEX_FN) GetProcAddress(hKernel32, LOADLIBARYEX);
  240. /* Detect if there's already a path in the filename and load the library if
  241. there is. Note: Both back slashes and forward slashes have been supported
  242. since the earlier days of DOS at an API level although they are not
  243. supported by command prompt */
  244. if(_tcspbrk(filename, TEXT("\\/"))) {
  245. /** !checksrc! disable BANNEDFUNC 1 **/
  246. hModule = pLoadLibraryEx ?
  247. pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  248. LoadLibrary(filename);
  249. }
  250. /* Detect if KB2533623 is installed, as LOAD_LIBARY_SEARCH_SYSTEM32 is only
  251. supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
  252. Server 2008 R2 with this patch or natively on Windows 8 and above */
  253. else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
  254. /* Load the DLL from the Windows system directory */
  255. hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
  256. }
  257. else {
  258. /* Attempt to get the Windows system path */
  259. UINT systemdirlen = GetSystemDirectory(NULL, 0);
  260. if(systemdirlen) {
  261. /* Allocate space for the full DLL path (Room for the null terminator
  262. is included in systemdirlen) */
  263. size_t filenamelen = _tcslen(filename);
  264. TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
  265. if(path && GetSystemDirectory(path, systemdirlen)) {
  266. /* Calculate the full DLL path */
  267. _tcscpy(path + _tcslen(path), TEXT("\\"));
  268. _tcscpy(path + _tcslen(path), filename);
  269. /* Load the DLL from the Windows system directory */
  270. /** !checksrc! disable BANNEDFUNC 1 **/
  271. hModule = pLoadLibraryEx ?
  272. pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  273. LoadLibrary(path);
  274. }
  275. free(path);
  276. }
  277. }
  278. return hModule;
  279. }
  280. #endif /* USE_WINDOWS_SSPI || (!CURL_DISABLE_TELNET && USE_WINSOCK) */
  281. #endif /* WIN32 */