2
0

http_ntlm.c 7.8 KB

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