curl_sspi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
  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 http://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. #ifdef USE_WINDOWS_SSPI
  24. #include <curl/curl.h>
  25. #include "curl_sspi.h"
  26. #define _MPRINTF_REPLACE /* use our functions only */
  27. #include <curl/mprintf.h>
  28. #include "curl_memory.h"
  29. /* The last #include file should be: */
  30. #include "memdebug.h"
  31. /* We use our own typedef here since some headers might lack these */
  32. typedef PSecurityFunctionTable (APIENTRY *INITSECURITYINTERFACE_FN)(VOID);
  33. /* See definition of SECURITY_ENTRYPOINT in sspi.h */
  34. #ifdef UNICODE
  35. # ifdef _WIN32_WCE
  36. # define SECURITYENTRYPOINT L"InitSecurityInterfaceW"
  37. # else
  38. # define SECURITYENTRYPOINT "InitSecurityInterfaceW"
  39. # endif
  40. #else
  41. # define SECURITYENTRYPOINT "InitSecurityInterfaceA"
  42. #endif
  43. /* Handle of security.dll or secur32.dll, depending on Windows version */
  44. HMODULE s_hSecDll = NULL;
  45. /* Pointer to SSPI dispatch table */
  46. PSecurityFunctionTable s_pSecFn = NULL;
  47. /*
  48. * Curl_sspi_global_init()
  49. *
  50. * This is used to load the Security Service Provider Interface (SSPI)
  51. * dynamic link library portably across all Windows versions, without
  52. * the need to directly link libcurl, nor the application using it, at
  53. * build time.
  54. *
  55. * Once this function has been executed, Windows SSPI functions can be
  56. * called through the Security Service Provider Interface dispatch table.
  57. */
  58. CURLcode Curl_sspi_global_init(void)
  59. {
  60. OSVERSIONINFO osver;
  61. INITSECURITYINTERFACE_FN pInitSecurityInterface;
  62. /* If security interface is not yet initialized try to do this */
  63. if(!s_hSecDll) {
  64. /* Find out Windows version */
  65. memset(&osver, 0, sizeof(osver));
  66. osver.dwOSVersionInfoSize = sizeof(osver);
  67. if(!GetVersionEx(&osver))
  68. return CURLE_FAILED_INIT;
  69. /* Security Service Provider Interface (SSPI) functions are located in
  70. * security.dll on WinNT 4.0 and in secur32.dll on Win9x. Win2K and XP
  71. * have both these DLLs (security.dll forwards calls to secur32.dll) */
  72. /* Load SSPI dll into the address space of the calling process */
  73. if(osver.dwPlatformId == VER_PLATFORM_WIN32_NT
  74. && osver.dwMajorVersion == 4)
  75. s_hSecDll = LoadLibrary(TEXT("security.dll"));
  76. else
  77. s_hSecDll = LoadLibrary(TEXT("secur32.dll"));
  78. if(!s_hSecDll)
  79. return CURLE_FAILED_INIT;
  80. /* Get address of the InitSecurityInterfaceA function from the SSPI dll */
  81. pInitSecurityInterface = (INITSECURITYINTERFACE_FN)
  82. GetProcAddress(s_hSecDll, SECURITYENTRYPOINT);
  83. if(!pInitSecurityInterface)
  84. return CURLE_FAILED_INIT;
  85. /* Get pointer to Security Service Provider Interface dispatch table */
  86. s_pSecFn = pInitSecurityInterface();
  87. if(!s_pSecFn)
  88. return CURLE_FAILED_INIT;
  89. }
  90. return CURLE_OK;
  91. }
  92. /*
  93. * Curl_sspi_global_cleanup()
  94. *
  95. * This deinitializes the Security Service Provider Interface from libcurl.
  96. */
  97. void Curl_sspi_global_cleanup(void)
  98. {
  99. if(s_hSecDll) {
  100. FreeLibrary(s_hSecDll);
  101. s_hSecDll = NULL;
  102. s_pSecFn = NULL;
  103. }
  104. }
  105. #endif /* USE_WINDOWS_SSPI */