curl_sspi.c 7.0 KB

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