http_ntlm.c 7.7 KB

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