2
0

http_ntlm.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. struct Curl_easy *data = conn->data;
  115. DEBUGASSERT(conn);
  116. DEBUGASSERT(data);
  117. if(proxy) {
  118. #ifndef CURL_DISABLE_PROXY
  119. allocuserpwd = &data->state.aptr.proxyuserpwd;
  120. userp = conn->http_proxy.user;
  121. passwdp = conn->http_proxy.passwd;
  122. service = conn->data->set.str[STRING_PROXY_SERVICE_NAME] ?
  123. conn->data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
  124. hostname = conn->http_proxy.host.name;
  125. ntlm = &conn->proxyntlm;
  126. state = &conn->proxy_ntlm_state;
  127. authp = &conn->data->state.authproxy;
  128. #else
  129. return CURLE_NOT_BUILT_IN;
  130. #endif
  131. }
  132. else {
  133. allocuserpwd = &data->state.aptr.userpwd;
  134. userp = conn->user;
  135. passwdp = conn->passwd;
  136. service = conn->data->set.str[STRING_SERVICE_NAME] ?
  137. conn->data->set.str[STRING_SERVICE_NAME] : "HTTP";
  138. hostname = conn->host.name;
  139. ntlm = &conn->ntlm;
  140. state = &conn->http_ntlm_state;
  141. authp = &conn->data->state.authhost;
  142. }
  143. authp->done = FALSE;
  144. /* not set means empty */
  145. if(!userp)
  146. userp = "";
  147. if(!passwdp)
  148. passwdp = "";
  149. #ifdef USE_WINDOWS_SSPI
  150. if(s_hSecDll == NULL) {
  151. /* not thread safe and leaks - use curl_global_init() to avoid */
  152. CURLcode err = Curl_sspi_global_init();
  153. if(s_hSecDll == NULL)
  154. return err;
  155. }
  156. #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
  157. ntlm->sslContext = conn->sslContext;
  158. #endif
  159. #endif
  160. switch(*state) {
  161. case NTLMSTATE_TYPE1:
  162. default: /* for the weird cases we (re)start here */
  163. /* Create a type-1 message */
  164. result = Curl_auth_create_ntlm_type1_message(conn->data, userp, passwdp,
  165. service, hostname,
  166. ntlm, &base64,
  167. &len);
  168. if(result)
  169. return result;
  170. if(base64) {
  171. free(*allocuserpwd);
  172. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  173. proxy ? "Proxy-" : "",
  174. base64);
  175. free(base64);
  176. if(!*allocuserpwd)
  177. return CURLE_OUT_OF_MEMORY;
  178. DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
  179. }
  180. break;
  181. case NTLMSTATE_TYPE2:
  182. /* We already received the type-2 message, create a type-3 message */
  183. result = Curl_auth_create_ntlm_type3_message(conn->data, userp, passwdp,
  184. ntlm, &base64, &len);
  185. if(result)
  186. return result;
  187. if(base64) {
  188. free(*allocuserpwd);
  189. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  190. proxy ? "Proxy-" : "",
  191. base64);
  192. free(base64);
  193. if(!*allocuserpwd)
  194. return CURLE_OUT_OF_MEMORY;
  195. DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
  196. *state = NTLMSTATE_TYPE3; /* we send a type-3 */
  197. authp->done = TRUE;
  198. }
  199. break;
  200. case NTLMSTATE_TYPE3:
  201. /* connection is already authenticated,
  202. * don't send a header in future requests */
  203. *state = NTLMSTATE_LAST;
  204. /* FALLTHROUGH */
  205. case NTLMSTATE_LAST:
  206. Curl_safefree(*allocuserpwd);
  207. authp->done = TRUE;
  208. break;
  209. }
  210. return CURLE_OK;
  211. }
  212. void Curl_http_auth_cleanup_ntlm(struct connectdata *conn)
  213. {
  214. Curl_auth_cleanup_ntlm(&conn->ntlm);
  215. Curl_auth_cleanup_ntlm(&conn->proxyntlm);
  216. #if defined(NTLM_WB_ENABLED)
  217. Curl_http_auth_cleanup_ntlm_wb(conn);
  218. #endif
  219. }
  220. #endif /* !CURL_DISABLE_HTTP && USE_NTLM */