curl_sspi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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 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. * Parameters:
  60. *
  61. * None.
  62. *
  63. * Returns CURLE_OK on success.
  64. */
  65. CURLcode Curl_sspi_global_init(void)
  66. {
  67. INITSECURITYINTERFACE_FN pInitSecurityInterface;
  68. /* If security interface is not yet initialized try to do this */
  69. if(!s_hSecDll) {
  70. /* Security Service Provider Interface (SSPI) functions are located in
  71. * security.dll on WinNT 4.0 and in secur32.dll on Win9x. Win2K and XP
  72. * have both these DLLs (security.dll forwards calls to secur32.dll) */
  73. /* Load SSPI dll into the address space of the calling process */
  74. if(Curl_verify_windows_version(4, 0, PLATFORM_WINNT, VERSION_EQUAL))
  75. s_hSecDll = Curl_load_library(TEXT("security.dll"));
  76. else
  77. s_hSecDll = Curl_load_library(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 =
  82. CURLX_FUNCTION_CAST(INITSECURITYINTERFACE_FN,
  83. (GetProcAddress(s_hSecDll, SECURITYENTRYPOINT)));
  84. if(!pInitSecurityInterface)
  85. return CURLE_FAILED_INIT;
  86. /* Get pointer to Security Service Provider Interface dispatch table */
  87. s_pSecFn = pInitSecurityInterface();
  88. if(!s_pSecFn)
  89. return CURLE_FAILED_INIT;
  90. }
  91. return CURLE_OK;
  92. }
  93. /*
  94. * Curl_sspi_global_cleanup()
  95. *
  96. * This deinitializes the Security Service Provider Interface from libcurl.
  97. *
  98. * Parameters:
  99. *
  100. * None.
  101. */
  102. void Curl_sspi_global_cleanup(void)
  103. {
  104. if(s_hSecDll) {
  105. FreeLibrary(s_hSecDll);
  106. s_hSecDll = NULL;
  107. s_pSecFn = NULL;
  108. }
  109. }
  110. /*
  111. * Curl_create_sspi_identity()
  112. *
  113. * This is used to populate a SSPI identity structure based on the supplied
  114. * username and password.
  115. *
  116. * Parameters:
  117. *
  118. * userp [in] - The user name in the format User or Domain\User.
  119. * passwdp [in] - The user's password.
  120. * identity [in/out] - The identity structure.
  121. *
  122. * Returns CURLE_OK on success.
  123. */
  124. CURLcode Curl_create_sspi_identity(const char *userp, const char *passwdp,
  125. SEC_WINNT_AUTH_IDENTITY *identity)
  126. {
  127. xcharp_u useranddomain;
  128. xcharp_u user, dup_user;
  129. xcharp_u domain, dup_domain;
  130. xcharp_u passwd, dup_passwd;
  131. size_t domlen = 0;
  132. domain.const_tchar_ptr = TEXT("");
  133. /* Initialize the identity */
  134. memset(identity, 0, sizeof(*identity));
  135. useranddomain.tchar_ptr = curlx_convert_UTF8_to_tchar((char *)userp);
  136. if(!useranddomain.tchar_ptr)
  137. return CURLE_OUT_OF_MEMORY;
  138. user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('\\'));
  139. if(!user.const_tchar_ptr)
  140. user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('/'));
  141. if(user.tchar_ptr) {
  142. domain.tchar_ptr = useranddomain.tchar_ptr;
  143. domlen = user.tchar_ptr - useranddomain.tchar_ptr;
  144. user.tchar_ptr++;
  145. }
  146. else {
  147. user.tchar_ptr = useranddomain.tchar_ptr;
  148. domain.const_tchar_ptr = TEXT("");
  149. domlen = 0;
  150. }
  151. /* Setup the identity's user and length */
  152. dup_user.tchar_ptr = _tcsdup(user.tchar_ptr);
  153. if(!dup_user.tchar_ptr) {
  154. curlx_unicodefree(useranddomain.tchar_ptr);
  155. return CURLE_OUT_OF_MEMORY;
  156. }
  157. identity->User = dup_user.tbyte_ptr;
  158. identity->UserLength = curlx_uztoul(_tcslen(dup_user.tchar_ptr));
  159. dup_user.tchar_ptr = NULL;
  160. /* Setup the identity's domain and length */
  161. dup_domain.tchar_ptr = malloc(sizeof(TCHAR) * (domlen + 1));
  162. if(!dup_domain.tchar_ptr) {
  163. curlx_unicodefree(useranddomain.tchar_ptr);
  164. return CURLE_OUT_OF_MEMORY;
  165. }
  166. _tcsncpy(dup_domain.tchar_ptr, domain.tchar_ptr, domlen);
  167. *(dup_domain.tchar_ptr + domlen) = TEXT('\0');
  168. identity->Domain = dup_domain.tbyte_ptr;
  169. identity->DomainLength = curlx_uztoul(domlen);
  170. dup_domain.tchar_ptr = NULL;
  171. curlx_unicodefree(useranddomain.tchar_ptr);
  172. /* Setup the identity's password and length */
  173. passwd.tchar_ptr = curlx_convert_UTF8_to_tchar((char *)passwdp);
  174. if(!passwd.tchar_ptr)
  175. return CURLE_OUT_OF_MEMORY;
  176. dup_passwd.tchar_ptr = _tcsdup(passwd.tchar_ptr);
  177. if(!dup_passwd.tchar_ptr) {
  178. curlx_unicodefree(passwd.tchar_ptr);
  179. return CURLE_OUT_OF_MEMORY;
  180. }
  181. identity->Password = dup_passwd.tbyte_ptr;
  182. identity->PasswordLength = curlx_uztoul(_tcslen(dup_passwd.tchar_ptr));
  183. dup_passwd.tchar_ptr = NULL;
  184. curlx_unicodefree(passwd.tchar_ptr);
  185. /* Setup the identity's flags */
  186. identity->Flags = SECFLAG_WINNT_AUTH_IDENTITY;
  187. return CURLE_OK;
  188. }
  189. /*
  190. * Curl_sspi_free_identity()
  191. *
  192. * This is used to free the contents of a SSPI identifier structure.
  193. *
  194. * Parameters:
  195. *
  196. * identity [in/out] - The identity structure.
  197. */
  198. void Curl_sspi_free_identity(SEC_WINNT_AUTH_IDENTITY *identity)
  199. {
  200. if(identity) {
  201. Curl_safefree(identity->User);
  202. Curl_safefree(identity->Password);
  203. Curl_safefree(identity->Domain);
  204. }
  205. }
  206. #endif /* USE_WINDOWS_SSPI */