system_win32.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2016, Steve Holme, <[email protected]>.
  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 identifer.
  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(_WIN32_WINNT) || !defined(_WIN32_WINNT_WIN2K) || \
  74. (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
  75. OSVERSIONINFO osver;
  76. memset(&osver, 0, sizeof(osver));
  77. osver.dwOSVersionInfoSize = sizeof(osver);
  78. /* Find out Windows version */
  79. if(GetVersionEx(&osver)) {
  80. /* Verify the Operating System version number */
  81. switch(condition) {
  82. case VERSION_LESS_THAN:
  83. if(osver.dwMajorVersion < majorVersion ||
  84. (osver.dwMajorVersion == majorVersion &&
  85. osver.dwMinorVersion < minorVersion))
  86. matched = TRUE;
  87. break;
  88. case VERSION_LESS_THAN_EQUAL:
  89. if(osver.dwMajorVersion <= majorVersion &&
  90. osver.dwMinorVersion <= minorVersion)
  91. matched = TRUE;
  92. break;
  93. case VERSION_EQUAL:
  94. if(osver.dwMajorVersion == majorVersion &&
  95. osver.dwMinorVersion == minorVersion)
  96. matched = TRUE;
  97. break;
  98. case VERSION_GREATER_THAN_EQUAL:
  99. if(osver.dwMajorVersion >= majorVersion &&
  100. osver.dwMinorVersion >= minorVersion)
  101. matched = TRUE;
  102. break;
  103. case VERSION_GREATER_THAN:
  104. if(osver.dwMajorVersion > majorVersion ||
  105. (osver.dwMajorVersion == majorVersion &&
  106. osver.dwMinorVersion > minorVersion))
  107. matched = TRUE;
  108. break;
  109. }
  110. /* Verify the platform identifier (if necessary) */
  111. if(matched && platform != PLATFORM_DONT_CARE) {
  112. switch(platform) {
  113. case PLATFORM_WINDOWS:
  114. if(osver.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
  115. matched = FALSE;
  116. break;
  117. case PLATFORM_WINNT:
  118. if(osver.dwPlatformId != VER_PLATFORM_WIN32_NT)
  119. matched = FALSE;
  120. }
  121. }
  122. }
  123. #else
  124. ULONGLONG cm = 0;
  125. OSVERSIONINFOEX osver;
  126. BYTE majorCondition;
  127. BYTE minorCondition;
  128. BYTE spMajorCondition;
  129. BYTE spMinorCondition;
  130. switch(condition) {
  131. case VERSION_LESS_THAN:
  132. majorCondition = VER_LESS;
  133. minorCondition = VER_LESS;
  134. spMajorCondition = VER_LESS_EQUAL;
  135. spMinorCondition = VER_LESS_EQUAL;
  136. break;
  137. case VERSION_LESS_THAN_EQUAL:
  138. majorCondition = VER_LESS_EQUAL;
  139. minorCondition = VER_LESS_EQUAL;
  140. spMajorCondition = VER_LESS_EQUAL;
  141. spMinorCondition = VER_LESS_EQUAL;
  142. break;
  143. case VERSION_EQUAL:
  144. majorCondition = VER_EQUAL;
  145. minorCondition = VER_EQUAL;
  146. spMajorCondition = VER_GREATER_EQUAL;
  147. spMinorCondition = VER_GREATER_EQUAL;
  148. break;
  149. case VERSION_GREATER_THAN_EQUAL:
  150. majorCondition = VER_GREATER_EQUAL;
  151. minorCondition = VER_GREATER_EQUAL;
  152. spMajorCondition = VER_GREATER_EQUAL;
  153. spMinorCondition = VER_GREATER_EQUAL;
  154. break;
  155. case VERSION_GREATER_THAN:
  156. majorCondition = VER_GREATER;
  157. minorCondition = VER_GREATER;
  158. spMajorCondition = VER_GREATER_EQUAL;
  159. spMinorCondition = VER_GREATER_EQUAL;
  160. break;
  161. default:
  162. return FALSE;
  163. }
  164. memset(&osver, 0, sizeof(osver));
  165. osver.dwOSVersionInfoSize = sizeof(osver);
  166. osver.dwMajorVersion = majorVersion;
  167. osver.dwMinorVersion = minorVersion;
  168. if(platform == PLATFORM_WINDOWS)
  169. osver.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
  170. else if(platform == PLATFORM_WINNT)
  171. osver.dwPlatformId = VER_PLATFORM_WIN32_NT;
  172. cm = VerSetConditionMask(cm, VER_MAJORVERSION, majorCondition);
  173. cm = VerSetConditionMask(cm, VER_MINORVERSION, minorCondition);
  174. cm = VerSetConditionMask(cm, VER_SERVICEPACKMAJOR, spMajorCondition);
  175. cm = VerSetConditionMask(cm, VER_SERVICEPACKMINOR, spMinorCondition);
  176. if(platform != PLATFORM_DONT_CARE)
  177. cm = VerSetConditionMask(cm, VER_PLATFORMID, VER_EQUAL);
  178. if(VerifyVersionInfo(&osver, (VER_MAJORVERSION | VER_MINORVERSION |
  179. VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR),
  180. cm))
  181. matched = TRUE;
  182. #endif
  183. return matched;
  184. }
  185. #if defined(USE_WINDOWS_SSPI) || (!defined(CURL_DISABLE_TELNET) && \
  186. defined(USE_WINSOCK))
  187. /*
  188. * Curl_load_library()
  189. *
  190. * This is used to dynamically load DLLs using the most secure method available
  191. * for the version of Windows that we are running on.
  192. *
  193. * Parameters:
  194. *
  195. * filename [in] - The filename or full path of the DLL to load. If only the
  196. * filename is passed then the DLL will be loaded from the
  197. * Windows system directory.
  198. *
  199. * Returns the handle of the module on success; otherwise NULL.
  200. */
  201. HMODULE Curl_load_library(LPCTSTR filename)
  202. {
  203. HMODULE hModule = NULL;
  204. LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
  205. /* Get a handle to kernel32 so we can access it's functions at runtime */
  206. HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
  207. if(!hKernel32)
  208. return NULL;
  209. /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
  210. and above */
  211. pLoadLibraryEx = (LOADLIBRARYEX_FN) GetProcAddress(hKernel32, LOADLIBARYEX);
  212. /* Detect if there's already a path in the filename and load the library if
  213. there is. Note: Both back slashes and forward slashes have been supported
  214. since the earlier days of DOS at an API level although they are not
  215. supported by command prompt */
  216. if(_tcspbrk(filename, TEXT("\\/"))) {
  217. /** !checksrc! disable BANNEDFUNC 1 **/
  218. hModule = pLoadLibraryEx ?
  219. pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  220. LoadLibrary(filename);
  221. }
  222. /* Detect if KB2533623 is installed, as LOAD_LIBARY_SEARCH_SYSTEM32 is only
  223. supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
  224. Server 2008 R2 with this patch or natively on Windows 8 and above */
  225. else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
  226. /* Load the DLL from the Windows system directory */
  227. hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
  228. }
  229. else {
  230. /* Attempt to get the Windows system path */
  231. UINT systemdirlen = GetSystemDirectory(NULL, 0);
  232. if(systemdirlen) {
  233. /* Allocate space for the full DLL path (Room for the null terminator
  234. is included in systemdirlen) */
  235. size_t filenamelen = _tcslen(filename);
  236. TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
  237. if(path && GetSystemDirectory(path, systemdirlen)) {
  238. /* Calculate the full DLL path */
  239. _tcscpy(path + _tcslen(path), TEXT("\\"));
  240. _tcscpy(path + _tcslen(path), filename);
  241. /* Load the DLL from the Windows system directory */
  242. /** !checksrc! disable BANNEDFUNC 1 **/
  243. hModule = pLoadLibraryEx ?
  244. pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  245. LoadLibrary(path);
  246. }
  247. free(path);
  248. }
  249. }
  250. return hModule;
  251. }
  252. #endif /* USE_WINDOWS_SSPI || (!CURL_DISABLE_TELNET && USE_WINSOCK) */
  253. #endif /* WIN32 */