http_ntlm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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. #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 connectdata *conn,
  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. ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
  63. state = proxy ? &conn->proxy_ntlm_state : &conn->http_ntlm_state;
  64. if(checkprefix("NTLM", header)) {
  65. header += strlen("NTLM");
  66. while(*header && ISSPACE(*header))
  67. header++;
  68. if(*header) {
  69. result = Curl_auth_decode_ntlm_type2_message(conn->data, header, ntlm);
  70. if(result)
  71. return result;
  72. *state = NTLMSTATE_TYPE2; /* We got a type-2 message */
  73. }
  74. else {
  75. if(*state == NTLMSTATE_LAST) {
  76. infof(conn->data, "NTLM auth restarted\n");
  77. Curl_http_auth_cleanup_ntlm(conn);
  78. }
  79. else if(*state == NTLMSTATE_TYPE3) {
  80. infof(conn->data, "NTLM handshake rejected\n");
  81. Curl_http_auth_cleanup_ntlm(conn);
  82. *state = NTLMSTATE_NONE;
  83. return CURLE_REMOTE_ACCESS_DENIED;
  84. }
  85. else if(*state >= NTLMSTATE_TYPE1) {
  86. infof(conn->data, "NTLM handshake failure (internal error)\n");
  87. return CURLE_REMOTE_ACCESS_DENIED;
  88. }
  89. *state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
  90. }
  91. }
  92. return result;
  93. }
  94. /*
  95. * This is for creating ntlm header output
  96. */
  97. CURLcode Curl_output_ntlm(struct connectdata *conn, bool proxy)
  98. {
  99. char *base64 = NULL;
  100. size_t len = 0;
  101. CURLcode result;
  102. /* point to the address of the pointer that holds the string to send to the
  103. server, which is for a plain host or for a HTTP proxy */
  104. char **allocuserpwd;
  105. /* point to the username, password, service and host */
  106. const char *userp;
  107. const char *passwdp;
  108. const char *service = NULL;
  109. const char *hostname = NULL;
  110. /* point to the correct struct with this */
  111. struct ntlmdata *ntlm;
  112. curlntlm *state;
  113. struct auth *authp;
  114. DEBUGASSERT(conn);
  115. DEBUGASSERT(conn->data);
  116. if(proxy) {
  117. allocuserpwd = &conn->allocptr.proxyuserpwd;
  118. userp = conn->http_proxy.user;
  119. passwdp = conn->http_proxy.passwd;
  120. service = conn->data->set.str[STRING_PROXY_SERVICE_NAME] ?
  121. conn->data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
  122. hostname = conn->http_proxy.host.name;
  123. ntlm = &conn->proxyntlm;
  124. state = &conn->proxy_ntlm_state;
  125. authp = &conn->data->state.authproxy;
  126. }
  127. else {
  128. allocuserpwd = &conn->allocptr.userpwd;
  129. userp = conn->user;
  130. passwdp = conn->passwd;
  131. service = conn->data->set.str[STRING_SERVICE_NAME] ?
  132. conn->data->set.str[STRING_SERVICE_NAME] : "HTTP";
  133. hostname = conn->host.name;
  134. ntlm = &conn->ntlm;
  135. state = &conn->http_ntlm_state;
  136. authp = &conn->data->state.authhost;
  137. }
  138. authp->done = FALSE;
  139. /* not set means empty */
  140. if(!userp)
  141. userp = "";
  142. if(!passwdp)
  143. passwdp = "";
  144. #ifdef USE_WINDOWS_SSPI
  145. if(s_hSecDll == NULL) {
  146. /* not thread safe and leaks - use curl_global_init() to avoid */
  147. CURLcode err = Curl_sspi_global_init();
  148. if(s_hSecDll == NULL)
  149. return err;
  150. }
  151. #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
  152. ntlm->sslContext = conn->sslContext;
  153. #endif
  154. #endif
  155. switch(*state) {
  156. case NTLMSTATE_TYPE1:
  157. default: /* for the weird cases we (re)start here */
  158. /* Create a type-1 message */
  159. result = Curl_auth_create_ntlm_type1_message(conn->data, userp, passwdp,
  160. service, hostname,
  161. ntlm, &base64,
  162. &len);
  163. if(result)
  164. return result;
  165. if(base64) {
  166. free(*allocuserpwd);
  167. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  168. proxy ? "Proxy-" : "",
  169. base64);
  170. free(base64);
  171. if(!*allocuserpwd)
  172. return CURLE_OUT_OF_MEMORY;
  173. DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
  174. }
  175. break;
  176. case NTLMSTATE_TYPE2:
  177. /* We already received the type-2 message, create a type-3 message */
  178. result = Curl_auth_create_ntlm_type3_message(conn->data, userp, passwdp,
  179. ntlm, &base64, &len);
  180. if(result)
  181. return result;
  182. if(base64) {
  183. free(*allocuserpwd);
  184. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  185. proxy ? "Proxy-" : "",
  186. base64);
  187. free(base64);
  188. if(!*allocuserpwd)
  189. return CURLE_OUT_OF_MEMORY;
  190. DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
  191. *state = NTLMSTATE_TYPE3; /* we send a type-3 */
  192. authp->done = TRUE;
  193. }
  194. break;
  195. case NTLMSTATE_TYPE3:
  196. /* connection is already authenticated,
  197. * don't send a header in future requests */
  198. *state = NTLMSTATE_LAST;
  199. /* FALLTHROUGH */
  200. case NTLMSTATE_LAST:
  201. Curl_safefree(*allocuserpwd);
  202. authp->done = TRUE;
  203. break;
  204. }
  205. return CURLE_OK;
  206. }
  207. void Curl_http_auth_cleanup_ntlm(struct connectdata *conn)
  208. {
  209. Curl_auth_cleanup_ntlm(&conn->ntlm);
  210. Curl_auth_cleanup_ntlm(&conn->proxyntlm);
  211. #if defined(NTLM_WB_ENABLED)
  212. Curl_http_auth_cleanup_ntlm_wb(conn);
  213. #endif
  214. }
  215. #endif /* !CURL_DISABLE_HTTP && USE_NTLM */