curl_sspi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, 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 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. #ifdef USE_WINDOWS_SSPI
  24. #include <curl/curl.h>
  25. #include "curl_sspi.h"
  26. #include "curl_multibyte.h"
  27. #include "system_win32.h"
  28. #include "warnless.h"
  29. /* The last #include files should be: */
  30. #include "curl_memory.h"
  31. #include "memdebug.h"
  32. /* We use our own typedef here since some headers might lack these */
  33. typedef PSecurityFunctionTable (APIENTRY *INITSECURITYINTERFACE_FN)(VOID);
  34. /* See definition of SECURITY_ENTRYPOINT in sspi.h */
  35. #ifdef UNICODE
  36. # ifdef _WIN32_WCE
  37. # define SECURITYENTRYPOINT L"InitSecurityInterfaceW"
  38. # else
  39. # define SECURITYENTRYPOINT "InitSecurityInterfaceW"
  40. # endif
  41. #else
  42. # define SECURITYENTRYPOINT "InitSecurityInterfaceA"
  43. #endif
  44. /* Handle of security.dll or secur32.dll, depending on Windows version */
  45. HMODULE s_hSecDll = NULL;
  46. /* Pointer to SSPI dispatch table */
  47. PSecurityFunctionTable s_pSecFn = NULL;
  48. /*
  49. * Curl_sspi_global_init()
  50. *
  51. * This is used to load the Security Service Provider Interface (SSPI)
  52. * dynamic link library portably across all Windows versions, without
  53. * the need to directly link libcurl, nor the application using it, at
  54. * build time.
  55. *
  56. * Once this function has been executed, Windows SSPI functions can be
  57. * called through the Security Service Provider Interface dispatch table.
  58. */
  59. CURLcode Curl_sspi_global_init(void)
  60. {
  61. INITSECURITYINTERFACE_FN pInitSecurityInterface;
  62. /* If security interface is not yet initialized try to do this */
  63. if(!s_hSecDll) {
  64. /* Security Service Provider Interface (SSPI) functions are located in
  65. * security.dll on WinNT 4.0 and in secur32.dll on Win9x. Win2K and XP
  66. * have both these DLLs (security.dll forwards calls to secur32.dll) */
  67. /* Load SSPI dll into the address space of the calling process */
  68. if(Curl_verify_windows_version(4, 0, PLATFORM_WINNT, VERSION_EQUAL))
  69. s_hSecDll = Curl_load_library(TEXT("security.dll"));
  70. else
  71. s_hSecDll = Curl_load_library(TEXT("secur32.dll"));
  72. if(!s_hSecDll)
  73. return CURLE_FAILED_INIT;
  74. /* Get address of the InitSecurityInterfaceA function from the SSPI dll */
  75. pInitSecurityInterface = (INITSECURITYINTERFACE_FN)
  76. GetProcAddress(s_hSecDll, SECURITYENTRYPOINT);
  77. if(!pInitSecurityInterface)
  78. return CURLE_FAILED_INIT;
  79. /* Get pointer to Security Service Provider Interface dispatch table */
  80. s_pSecFn = pInitSecurityInterface();
  81. if(!s_pSecFn)
  82. return CURLE_FAILED_INIT;
  83. }
  84. return CURLE_OK;
  85. }
  86. /*
  87. * Curl_sspi_global_cleanup()
  88. *
  89. * This deinitializes the Security Service Provider Interface from libcurl.
  90. */
  91. void Curl_sspi_global_cleanup(void)
  92. {
  93. if(s_hSecDll) {
  94. FreeLibrary(s_hSecDll);
  95. s_hSecDll = NULL;
  96. s_pSecFn = NULL;
  97. }
  98. }
  99. /*
  100. * Curl_create_sspi_identity()
  101. *
  102. * This is used to populate a SSPI identity structure based on the supplied
  103. * username and password.
  104. *
  105. * Parameters:
  106. *
  107. * userp [in] - The user name in the format User or Domain\User.
  108. * passdwp [in] - The user's password.
  109. * identity [in/out] - The identity structure.
  110. *
  111. * Returns CURLE_OK on success.
  112. */
  113. CURLcode Curl_create_sspi_identity(const char *userp, const char *passwdp,
  114. SEC_WINNT_AUTH_IDENTITY *identity)
  115. {
  116. xcharp_u useranddomain;
  117. xcharp_u user, dup_user;
  118. xcharp_u domain, dup_domain;
  119. xcharp_u passwd, dup_passwd;
  120. size_t domlen = 0;
  121. domain.const_tchar_ptr = TEXT("");
  122. /* Initialize the identity */
  123. memset(identity, 0, sizeof(*identity));
  124. useranddomain.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)userp);
  125. if(!useranddomain.tchar_ptr)
  126. return CURLE_OUT_OF_MEMORY;
  127. user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('\\'));
  128. if(!user.const_tchar_ptr)
  129. user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('/'));
  130. if(user.tchar_ptr) {
  131. domain.tchar_ptr = useranddomain.tchar_ptr;
  132. domlen = user.tchar_ptr - useranddomain.tchar_ptr;
  133. user.tchar_ptr++;
  134. }
  135. else {
  136. user.tchar_ptr = useranddomain.tchar_ptr;
  137. domain.const_tchar_ptr = TEXT("");
  138. domlen = 0;
  139. }
  140. /* Setup the identity's user and length */
  141. dup_user.tchar_ptr = _tcsdup(user.tchar_ptr);
  142. if(!dup_user.tchar_ptr) {
  143. Curl_unicodefree(useranddomain.tchar_ptr);
  144. return CURLE_OUT_OF_MEMORY;
  145. }
  146. identity->User = dup_user.tbyte_ptr;
  147. identity->UserLength = curlx_uztoul(_tcslen(dup_user.tchar_ptr));
  148. dup_user.tchar_ptr = NULL;
  149. /* Setup the identity's domain and length */
  150. dup_domain.tchar_ptr = malloc(sizeof(TCHAR) * (domlen + 1));
  151. if(!dup_domain.tchar_ptr) {
  152. Curl_unicodefree(useranddomain.tchar_ptr);
  153. return CURLE_OUT_OF_MEMORY;
  154. }
  155. _tcsncpy(dup_domain.tchar_ptr, domain.tchar_ptr, domlen);
  156. *(dup_domain.tchar_ptr + domlen) = TEXT('\0');
  157. identity->Domain = dup_domain.tbyte_ptr;
  158. identity->DomainLength = curlx_uztoul(domlen);
  159. dup_domain.tchar_ptr = NULL;
  160. Curl_unicodefree(useranddomain.tchar_ptr);
  161. /* Setup the identity's password and length */
  162. passwd.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)passwdp);
  163. if(!passwd.tchar_ptr)
  164. return CURLE_OUT_OF_MEMORY;
  165. dup_passwd.tchar_ptr = _tcsdup(passwd.tchar_ptr);
  166. if(!dup_passwd.tchar_ptr) {
  167. Curl_unicodefree(passwd.tchar_ptr);
  168. return CURLE_OUT_OF_MEMORY;
  169. }
  170. identity->Password = dup_passwd.tbyte_ptr;
  171. identity->PasswordLength = curlx_uztoul(_tcslen(dup_passwd.tchar_ptr));
  172. dup_passwd.tchar_ptr = NULL;
  173. Curl_unicodefree(passwd.tchar_ptr);
  174. /* Setup the identity's flags */
  175. identity->Flags = SECFLAG_WINNT_AUTH_IDENTITY;
  176. return CURLE_OK;
  177. }
  178. void Curl_sspi_free_identity(SEC_WINNT_AUTH_IDENTITY *identity)
  179. {
  180. if(identity) {
  181. Curl_safefree(identity->User);
  182. Curl_safefree(identity->Password);
  183. Curl_safefree(identity->Domain);
  184. }
  185. }
  186. #endif /* USE_WINDOWS_SSPI */