http_ntlm.c 7.1 KB

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