http_ntlm.c 7.1 KB

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