curl_sspi.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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 "warnless.h"
  28. /* The last #include files should be: */
  29. #include "curl_memory.h"
  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. bool securityDll = FALSE;
  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. DWORD majorVersion = 4;
  68. DWORD platformId = VER_PLATFORM_WIN32_NT;
  69. #if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_WIN2K) || \
  70. (_WIN32_WINNT < _WIN32_WINNT_WIN2K)
  71. OSVERSIONINFO osver;
  72. memset(&osver, 0, sizeof(osver));
  73. osver.dwOSVersionInfoSize = sizeof(osver);
  74. /* Find out Windows version */
  75. if(!GetVersionEx(&osver))
  76. return CURLE_FAILED_INIT;
  77. /* Verify the major version number == 4 and platform id == WIN_NT */
  78. if(osver.dwMajorVersion == majorVersion &&
  79. osver.dwPlatformId == platformId)
  80. securityDll = TRUE;
  81. #else
  82. ULONGLONG cm;
  83. OSVERSIONINFOEX osver;
  84. memset(&osver, 0, sizeof(osver));
  85. osver.dwOSVersionInfoSize = sizeof(osver);
  86. osver.dwMajorVersion = majorVersion;
  87. osver.dwPlatformId = platformId;
  88. cm = VerSetConditionMask(0, VER_MAJORVERSION, VER_EQUAL);
  89. cm = VerSetConditionMask(cm, VER_MINORVERSION, VER_GREATER_EQUAL);
  90. cm = VerSetConditionMask(cm, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
  91. cm = VerSetConditionMask(cm, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);
  92. cm = VerSetConditionMask(cm, VER_PLATFORMID, VER_EQUAL);
  93. /* Verify the major version number == 4 and platform id == WIN_NT */
  94. if(VerifyVersionInfo(&osver, (VER_MAJORVERSION | VER_MINORVERSION |
  95. VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR |
  96. VER_PLATFORMID),
  97. cm))
  98. securityDll = TRUE;
  99. #endif
  100. /* Load SSPI dll into the address space of the calling process */
  101. if(securityDll)
  102. s_hSecDll = LoadLibrary(TEXT("security.dll"));
  103. else
  104. s_hSecDll = LoadLibrary(TEXT("secur32.dll"));
  105. if(!s_hSecDll)
  106. return CURLE_FAILED_INIT;
  107. /* Get address of the InitSecurityInterfaceA function from the SSPI dll */
  108. pInitSecurityInterface = (INITSECURITYINTERFACE_FN)
  109. GetProcAddress(s_hSecDll, SECURITYENTRYPOINT);
  110. if(!pInitSecurityInterface)
  111. return CURLE_FAILED_INIT;
  112. /* Get pointer to Security Service Provider Interface dispatch table */
  113. s_pSecFn = pInitSecurityInterface();
  114. if(!s_pSecFn)
  115. return CURLE_FAILED_INIT;
  116. }
  117. return CURLE_OK;
  118. }
  119. /*
  120. * Curl_sspi_global_cleanup()
  121. *
  122. * This deinitializes the Security Service Provider Interface from libcurl.
  123. */
  124. void Curl_sspi_global_cleanup(void)
  125. {
  126. if(s_hSecDll) {
  127. FreeLibrary(s_hSecDll);
  128. s_hSecDll = NULL;
  129. s_pSecFn = NULL;
  130. }
  131. }
  132. /*
  133. * Curl_create_sspi_identity()
  134. *
  135. * This is used to populate a SSPI identity structure based on the supplied
  136. * username and password.
  137. *
  138. * Parameters:
  139. *
  140. * userp [in] - The user name in the format User or Domain\User.
  141. * passdwp [in] - The user's password.
  142. * identity [in/out] - The identity structure.
  143. *
  144. * Returns CURLE_OK on success.
  145. */
  146. CURLcode Curl_create_sspi_identity(const char *userp, const char *passwdp,
  147. SEC_WINNT_AUTH_IDENTITY *identity)
  148. {
  149. xcharp_u useranddomain;
  150. xcharp_u user, dup_user;
  151. xcharp_u domain, dup_domain;
  152. xcharp_u passwd, dup_passwd;
  153. size_t domlen = 0;
  154. domain.const_tchar_ptr = TEXT("");
  155. /* Initialize the identity */
  156. memset(identity, 0, sizeof(*identity));
  157. useranddomain.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)userp);
  158. if(!useranddomain.tchar_ptr)
  159. return CURLE_OUT_OF_MEMORY;
  160. user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('\\'));
  161. if(!user.const_tchar_ptr)
  162. user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('/'));
  163. if(user.tchar_ptr) {
  164. domain.tchar_ptr = useranddomain.tchar_ptr;
  165. domlen = user.tchar_ptr - useranddomain.tchar_ptr;
  166. user.tchar_ptr++;
  167. }
  168. else {
  169. user.tchar_ptr = useranddomain.tchar_ptr;
  170. domain.const_tchar_ptr = TEXT("");
  171. domlen = 0;
  172. }
  173. /* Setup the identity's user and length */
  174. dup_user.tchar_ptr = _tcsdup(user.tchar_ptr);
  175. if(!dup_user.tchar_ptr) {
  176. Curl_unicodefree(useranddomain.tchar_ptr);
  177. return CURLE_OUT_OF_MEMORY;
  178. }
  179. identity->User = dup_user.tbyte_ptr;
  180. identity->UserLength = curlx_uztoul(_tcslen(dup_user.tchar_ptr));
  181. dup_user.tchar_ptr = NULL;
  182. /* Setup the identity's domain and length */
  183. dup_domain.tchar_ptr = malloc(sizeof(TCHAR) * (domlen + 1));
  184. if(!dup_domain.tchar_ptr) {
  185. Curl_unicodefree(useranddomain.tchar_ptr);
  186. return CURLE_OUT_OF_MEMORY;
  187. }
  188. _tcsncpy(dup_domain.tchar_ptr, domain.tchar_ptr, domlen);
  189. *(dup_domain.tchar_ptr + domlen) = TEXT('\0');
  190. identity->Domain = dup_domain.tbyte_ptr;
  191. identity->DomainLength = curlx_uztoul(domlen);
  192. dup_domain.tchar_ptr = NULL;
  193. Curl_unicodefree(useranddomain.tchar_ptr);
  194. /* Setup the identity's password and length */
  195. passwd.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)passwdp);
  196. if(!passwd.tchar_ptr)
  197. return CURLE_OUT_OF_MEMORY;
  198. dup_passwd.tchar_ptr = _tcsdup(passwd.tchar_ptr);
  199. if(!dup_passwd.tchar_ptr) {
  200. Curl_unicodefree(passwd.tchar_ptr);
  201. return CURLE_OUT_OF_MEMORY;
  202. }
  203. identity->Password = dup_passwd.tbyte_ptr;
  204. identity->PasswordLength = curlx_uztoul(_tcslen(dup_passwd.tchar_ptr));
  205. dup_passwd.tchar_ptr = NULL;
  206. Curl_unicodefree(passwd.tchar_ptr);
  207. /* Setup the identity's flags */
  208. identity->Flags = SECFLAG_WINNT_AUTH_IDENTITY;
  209. return CURLE_OK;
  210. }
  211. void Curl_sspi_free_identity(SEC_WINNT_AUTH_IDENTITY *identity)
  212. {
  213. if(identity) {
  214. Curl_safefree(identity->User);
  215. Curl_safefree(identity->Password);
  216. Curl_safefree(identity->Domain);
  217. }
  218. }
  219. #endif /* USE_WINDOWS_SSPI */