http_ntlm.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
  26. /*
  27. * NTLM details:
  28. *
  29. * https://davenport.sourceforge.net/ntlm.html
  30. * https://www.innovation.ch/java/ntlm.html
  31. */
  32. #include "urldata.h"
  33. #include "sendf.h"
  34. #include "strcase.h"
  35. #include "http_ntlm.h"
  36. #include "curl_ntlm_core.h"
  37. #include "curlx/base64.h"
  38. #include "vauth/vauth.h"
  39. #include "url.h"
  40. #include "curlx/strparse.h"
  41. /* SSL backend-specific #if branches in this file must be kept in the order
  42. documented in curl_ntlm_core. */
  43. #ifdef USE_WINDOWS_SSPI
  44. #include "curl_sspi.h"
  45. #endif
  46. /* The last 2 #include files should be in this order */
  47. #include "curl_memory.h"
  48. #include "memdebug.h"
  49. CURLcode Curl_input_ntlm(struct Curl_easy *data,
  50. bool proxy, /* if proxy or not */
  51. const char *header) /* rest of the www-authenticate:
  52. header */
  53. {
  54. /* point to the correct struct with this */
  55. curlntlm *state;
  56. CURLcode result = CURLE_OK;
  57. struct connectdata *conn = data->conn;
  58. state = proxy ? &conn->proxy_ntlm_state : &conn->http_ntlm_state;
  59. if(checkprefix("NTLM", header)) {
  60. struct ntlmdata *ntlm = Curl_auth_ntlm_get(conn, proxy);
  61. if(!ntlm)
  62. return CURLE_FAILED_INIT;
  63. header += strlen("NTLM");
  64. curlx_str_passblanks(&header);
  65. if(*header) {
  66. unsigned char *hdr;
  67. size_t hdrlen;
  68. result = curlx_base64_decode(header, &hdr, &hdrlen);
  69. if(!result) {
  70. struct bufref hdrbuf;
  71. Curl_bufref_init(&hdrbuf);
  72. Curl_bufref_set(&hdrbuf, hdr, hdrlen, curl_free);
  73. result = Curl_auth_decode_ntlm_type2_message(data, &hdrbuf, ntlm);
  74. Curl_bufref_free(&hdrbuf);
  75. }
  76. if(result)
  77. return result;
  78. *state = NTLMSTATE_TYPE2; /* We got a type-2 message */
  79. }
  80. else {
  81. if(*state == NTLMSTATE_LAST) {
  82. infof(data, "NTLM auth restarted");
  83. Curl_auth_ntlm_remove(conn, proxy);
  84. }
  85. else if(*state == NTLMSTATE_TYPE3) {
  86. infof(data, "NTLM handshake rejected");
  87. Curl_auth_ntlm_remove(conn, proxy);
  88. *state = NTLMSTATE_NONE;
  89. return CURLE_REMOTE_ACCESS_DENIED;
  90. }
  91. else if(*state >= NTLMSTATE_TYPE1) {
  92. infof(data, "NTLM handshake failure (internal error)");
  93. return CURLE_REMOTE_ACCESS_DENIED;
  94. }
  95. *state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
  96. }
  97. }
  98. return result;
  99. }
  100. /*
  101. * This is for creating NTLM header output
  102. */
  103. CURLcode Curl_output_ntlm(struct Curl_easy *data, bool proxy)
  104. {
  105. char *base64 = NULL;
  106. size_t len = 0;
  107. CURLcode result = CURLE_OK;
  108. struct bufref ntlmmsg;
  109. /* point to the address of the pointer that holds the string to send to the
  110. server, which is for a plain host or for an HTTP proxy */
  111. char **allocuserpwd;
  112. /* point to the username, password, service and host */
  113. const char *userp;
  114. const char *passwdp;
  115. const char *service = NULL;
  116. const char *hostname = NULL;
  117. /* point to the correct struct with this */
  118. struct ntlmdata *ntlm;
  119. curlntlm *state;
  120. struct auth *authp;
  121. struct connectdata *conn = data->conn;
  122. DEBUGASSERT(conn);
  123. DEBUGASSERT(data);
  124. if(proxy) {
  125. #ifndef CURL_DISABLE_PROXY
  126. allocuserpwd = &data->state.aptr.proxyuserpwd;
  127. userp = data->state.aptr.proxyuser;
  128. passwdp = data->state.aptr.proxypasswd;
  129. service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
  130. data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
  131. hostname = conn->http_proxy.host.name;
  132. state = &conn->proxy_ntlm_state;
  133. authp = &data->state.authproxy;
  134. #else
  135. return CURLE_NOT_BUILT_IN;
  136. #endif
  137. }
  138. else {
  139. allocuserpwd = &data->state.aptr.userpwd;
  140. userp = data->state.aptr.user;
  141. passwdp = data->state.aptr.passwd;
  142. service = data->set.str[STRING_SERVICE_NAME] ?
  143. data->set.str[STRING_SERVICE_NAME] : "HTTP";
  144. hostname = conn->host.name;
  145. state = &conn->http_ntlm_state;
  146. authp = &data->state.authhost;
  147. }
  148. ntlm = Curl_auth_ntlm_get(conn, proxy);
  149. if(!ntlm)
  150. return CURLE_OUT_OF_MEMORY;
  151. authp->done = FALSE;
  152. /* not set means empty */
  153. if(!userp)
  154. userp = "";
  155. if(!passwdp)
  156. passwdp = "";
  157. #ifdef USE_WINDOWS_SSPI
  158. if(!Curl_pSecFn) {
  159. /* not thread safe and leaks - use curl_global_init() to avoid */
  160. CURLcode err = Curl_sspi_global_init();
  161. if(!Curl_pSecFn)
  162. return err;
  163. }
  164. #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
  165. ntlm->sslContext = conn->sslContext;
  166. #endif
  167. #endif
  168. Curl_bufref_init(&ntlmmsg);
  169. /* connection is already authenticated, do not send a header in future
  170. * requests so go directly to NTLMSTATE_LAST */
  171. if(*state == NTLMSTATE_TYPE3)
  172. *state = NTLMSTATE_LAST;
  173. switch(*state) {
  174. case NTLMSTATE_TYPE1:
  175. default: /* for the weird cases we (re)start here */
  176. /* Create a type-1 message */
  177. result = Curl_auth_create_ntlm_type1_message(data, userp, passwdp, service,
  178. hostname, ntlm, &ntlmmsg);
  179. if(!result) {
  180. DEBUGASSERT(Curl_bufref_len(&ntlmmsg) != 0);
  181. result = curlx_base64_encode((const char *) Curl_bufref_ptr(&ntlmmsg),
  182. Curl_bufref_len(&ntlmmsg), &base64, &len);
  183. if(!result) {
  184. free(*allocuserpwd);
  185. *allocuserpwd = curl_maprintf("%sAuthorization: NTLM %s\r\n",
  186. proxy ? "Proxy-" : "",
  187. base64);
  188. free(base64);
  189. if(!*allocuserpwd)
  190. result = CURLE_OUT_OF_MEMORY;
  191. }
  192. }
  193. break;
  194. case NTLMSTATE_TYPE2:
  195. /* We already received the type-2 message, create a type-3 message */
  196. result = Curl_auth_create_ntlm_type3_message(data, userp, passwdp,
  197. ntlm, &ntlmmsg);
  198. if(!result && Curl_bufref_len(&ntlmmsg)) {
  199. result = curlx_base64_encode((const char *) Curl_bufref_ptr(&ntlmmsg),
  200. Curl_bufref_len(&ntlmmsg), &base64, &len);
  201. if(!result) {
  202. free(*allocuserpwd);
  203. *allocuserpwd = curl_maprintf("%sAuthorization: NTLM %s\r\n",
  204. proxy ? "Proxy-" : "",
  205. base64);
  206. free(base64);
  207. if(!*allocuserpwd)
  208. result = CURLE_OUT_OF_MEMORY;
  209. else {
  210. *state = NTLMSTATE_TYPE3; /* we send a type-3 */
  211. authp->done = TRUE;
  212. }
  213. }
  214. }
  215. break;
  216. case NTLMSTATE_LAST:
  217. /* since this is a little artificial in that this is used without any
  218. outgoing auth headers being set, we need to set the bit by force */
  219. if(proxy)
  220. data->info.proxyauthpicked = CURLAUTH_NTLM;
  221. else
  222. data->info.httpauthpicked = CURLAUTH_NTLM;
  223. Curl_safefree(*allocuserpwd);
  224. authp->done = TRUE;
  225. break;
  226. }
  227. Curl_bufref_free(&ntlmmsg);
  228. return result;
  229. }
  230. #endif /* !CURL_DISABLE_HTTP && USE_NTLM */