curl_sspi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef USE_WINDOWS_SSPI
  26. #include <curl/curl.h>
  27. #include "curl_sspi.h"
  28. #include "strdup.h"
  29. #include "curlx/multibyte.h"
  30. #include "system_win32.h"
  31. #include "curlx/warnless.h"
  32. /* The last #include files should be: */
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. /* Pointer to SSPI dispatch table */
  36. PSecurityFunctionTable Curl_pSecFn = NULL;
  37. /*
  38. * Curl_sspi_global_init()
  39. *
  40. * This is used to load the Security Service Provider Interface (SSPI)
  41. * dynamic link library portably across all Windows versions, without
  42. * the need to directly link libcurl, nor the application using it, at
  43. * build time.
  44. *
  45. * Once this function has been executed, Windows SSPI functions can be
  46. * called through the Security Service Provider Interface dispatch table.
  47. *
  48. * Parameters:
  49. *
  50. * None.
  51. *
  52. * Returns CURLE_OK on success.
  53. */
  54. CURLcode Curl_sspi_global_init(void)
  55. {
  56. /* If security interface is not yet initialized try to do this */
  57. if(!Curl_pSecFn) {
  58. /* Get pointer to Security Service Provider Interface dispatch table */
  59. #ifdef __MINGW32CE__
  60. Curl_pSecFn = InitSecurityInterfaceW();
  61. #else
  62. Curl_pSecFn = InitSecurityInterface();
  63. #endif
  64. if(!Curl_pSecFn)
  65. return CURLE_FAILED_INIT;
  66. }
  67. return CURLE_OK;
  68. }
  69. /*
  70. * Curl_sspi_global_cleanup()
  71. *
  72. * This deinitializes the Security Service Provider Interface from libcurl.
  73. *
  74. * Parameters:
  75. *
  76. * None.
  77. */
  78. void Curl_sspi_global_cleanup(void)
  79. {
  80. if(Curl_pSecFn) {
  81. Curl_pSecFn = NULL;
  82. }
  83. }
  84. /*
  85. * Curl_create_sspi_identity()
  86. *
  87. * This is used to populate an SSPI identity structure based on the supplied
  88. * username and password.
  89. *
  90. * Parameters:
  91. *
  92. * userp [in] - The username in the format User or Domain\User.
  93. * passwdp [in] - The user's password.
  94. * identity [in/out] - The identity structure.
  95. *
  96. * Returns CURLE_OK on success.
  97. */
  98. CURLcode Curl_create_sspi_identity(const char *userp, const char *passwdp,
  99. SEC_WINNT_AUTH_IDENTITY *identity)
  100. {
  101. xcharp_u useranddomain;
  102. xcharp_u user, dup_user;
  103. xcharp_u domain, dup_domain;
  104. xcharp_u passwd, dup_passwd;
  105. size_t domlen = 0;
  106. domain.const_tchar_ptr = TEXT("");
  107. /* Initialize the identity */
  108. memset(identity, 0, sizeof(*identity));
  109. useranddomain.tchar_ptr = curlx_convert_UTF8_to_tchar(userp);
  110. if(!useranddomain.tchar_ptr)
  111. return CURLE_OUT_OF_MEMORY;
  112. user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('\\'));
  113. if(!user.const_tchar_ptr)
  114. user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('/'));
  115. if(user.tchar_ptr) {
  116. domain.tchar_ptr = useranddomain.tchar_ptr;
  117. domlen = user.tchar_ptr - useranddomain.tchar_ptr;
  118. user.tchar_ptr++;
  119. }
  120. else {
  121. user.tchar_ptr = useranddomain.tchar_ptr;
  122. domain.const_tchar_ptr = TEXT("");
  123. domlen = 0;
  124. }
  125. /* Setup the identity's user and length */
  126. dup_user.tchar_ptr = Curl_tcsdup(user.tchar_ptr);
  127. if(!dup_user.tchar_ptr) {
  128. curlx_unicodefree(useranddomain.tchar_ptr);
  129. return CURLE_OUT_OF_MEMORY;
  130. }
  131. identity->User = dup_user.tbyte_ptr;
  132. identity->UserLength = curlx_uztoul(_tcslen(dup_user.tchar_ptr));
  133. dup_user.tchar_ptr = NULL;
  134. /* Setup the identity's domain and length */
  135. dup_domain.tchar_ptr = malloc(sizeof(TCHAR) * (domlen + 1));
  136. if(!dup_domain.tchar_ptr) {
  137. curlx_unicodefree(useranddomain.tchar_ptr);
  138. return CURLE_OUT_OF_MEMORY;
  139. }
  140. _tcsncpy(dup_domain.tchar_ptr, domain.tchar_ptr, domlen);
  141. *(dup_domain.tchar_ptr + domlen) = TEXT('\0');
  142. identity->Domain = dup_domain.tbyte_ptr;
  143. identity->DomainLength = curlx_uztoul(domlen);
  144. dup_domain.tchar_ptr = NULL;
  145. curlx_unicodefree(useranddomain.tchar_ptr);
  146. /* Setup the identity's password and length */
  147. passwd.tchar_ptr = curlx_convert_UTF8_to_tchar(passwdp);
  148. if(!passwd.tchar_ptr)
  149. return CURLE_OUT_OF_MEMORY;
  150. dup_passwd.tchar_ptr = Curl_tcsdup(passwd.tchar_ptr);
  151. if(!dup_passwd.tchar_ptr) {
  152. curlx_unicodefree(passwd.tchar_ptr);
  153. return CURLE_OUT_OF_MEMORY;
  154. }
  155. identity->Password = dup_passwd.tbyte_ptr;
  156. identity->PasswordLength = curlx_uztoul(_tcslen(dup_passwd.tchar_ptr));
  157. dup_passwd.tchar_ptr = NULL;
  158. curlx_unicodefree(passwd.tchar_ptr);
  159. /* Setup the identity's flags */
  160. identity->Flags = (unsigned long)
  161. #ifdef UNICODE
  162. SEC_WINNT_AUTH_IDENTITY_UNICODE;
  163. #else
  164. SEC_WINNT_AUTH_IDENTITY_ANSI;
  165. #endif
  166. return CURLE_OK;
  167. }
  168. /*
  169. * Curl_sspi_free_identity()
  170. *
  171. * This is used to free the contents of an SSPI identifier structure.
  172. *
  173. * Parameters:
  174. *
  175. * identity [in/out] - The identity structure.
  176. */
  177. void Curl_sspi_free_identity(SEC_WINNT_AUTH_IDENTITY *identity)
  178. {
  179. if(identity) {
  180. Curl_safefree(identity->User);
  181. Curl_safefree(identity->Password);
  182. Curl_safefree(identity->Domain);
  183. }
  184. }
  185. #endif /* USE_WINDOWS_SSPI */