http_ntlm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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. * http://davenport.sourceforge.net/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 "rawstr.h"
  34. #include "http_ntlm.h"
  35. #include "curl_ntlm_wb.h"
  36. #include "vauth/vauth.h"
  37. #include "url.h"
  38. #include "curl_printf.h"
  39. #if defined(USE_NSS)
  40. #include "vtls/nssg.h"
  41. #elif defined(USE_WINDOWS_SSPI)
  42. #include "curl_sspi.h"
  43. #endif
  44. /* The last #include files should be: */
  45. #include "curl_memory.h"
  46. #include "memdebug.h"
  47. #if DEBUG_ME
  48. # define DEBUG_OUT(x) x
  49. #else
  50. # define DEBUG_OUT(x) Curl_nop_stmt
  51. #endif
  52. CURLcode Curl_input_ntlm(struct connectdata *conn,
  53. bool proxy, /* if proxy or not */
  54. const char *header) /* rest of the www-authenticate:
  55. header */
  56. {
  57. /* point to the correct struct with this */
  58. struct ntlmdata *ntlm;
  59. CURLcode result = CURLE_OK;
  60. ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
  61. if(checkprefix("NTLM", header)) {
  62. header += strlen("NTLM");
  63. while(*header && ISSPACE(*header))
  64. header++;
  65. if(*header) {
  66. result = Curl_auth_decode_ntlm_type2_message(conn->data, header, ntlm);
  67. if(result)
  68. return result;
  69. ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */
  70. }
  71. else {
  72. if(ntlm->state == NTLMSTATE_LAST) {
  73. infof(conn->data, "NTLM auth restarted\n");
  74. Curl_http_ntlm_cleanup(conn);
  75. }
  76. else if(ntlm->state == NTLMSTATE_TYPE3) {
  77. infof(conn->data, "NTLM handshake rejected\n");
  78. Curl_http_ntlm_cleanup(conn);
  79. ntlm->state = NTLMSTATE_NONE;
  80. return CURLE_REMOTE_ACCESS_DENIED;
  81. }
  82. else if(ntlm->state >= NTLMSTATE_TYPE1) {
  83. infof(conn->data, "NTLM handshake failure (internal error)\n");
  84. return CURLE_REMOTE_ACCESS_DENIED;
  85. }
  86. ntlm->state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
  87. }
  88. }
  89. return result;
  90. }
  91. /*
  92. * This is for creating ntlm header output
  93. */
  94. CURLcode Curl_output_ntlm(struct connectdata *conn, bool proxy)
  95. {
  96. char *base64 = NULL;
  97. size_t len = 0;
  98. CURLcode result;
  99. /* point to the address of the pointer that holds the string to send to the
  100. server, which is for a plain host or for a HTTP proxy */
  101. char **allocuserpwd;
  102. /* point to the name and password for this */
  103. const char *userp;
  104. const char *passwdp;
  105. /* point to the correct struct with this */
  106. struct ntlmdata *ntlm;
  107. struct auth *authp;
  108. DEBUGASSERT(conn);
  109. DEBUGASSERT(conn->data);
  110. #ifdef USE_NSS
  111. if(CURLE_OK != Curl_nss_force_init(conn->data))
  112. return CURLE_OUT_OF_MEMORY;
  113. #endif
  114. if(proxy) {
  115. allocuserpwd = &conn->allocptr.proxyuserpwd;
  116. userp = conn->proxyuser;
  117. passwdp = conn->proxypasswd;
  118. ntlm = &conn->proxyntlm;
  119. authp = &conn->data->state.authproxy;
  120. }
  121. else {
  122. allocuserpwd = &conn->allocptr.userpwd;
  123. userp = conn->user;
  124. passwdp = conn->passwd;
  125. ntlm = &conn->ntlm;
  126. authp = &conn->data->state.authhost;
  127. }
  128. authp->done = FALSE;
  129. /* not set means empty */
  130. if(!userp)
  131. userp = "";
  132. if(!passwdp)
  133. passwdp = "";
  134. #ifdef USE_WINDOWS_SSPI
  135. if(s_hSecDll == NULL) {
  136. /* not thread safe and leaks - use curl_global_init() to avoid */
  137. CURLcode err = Curl_sspi_global_init();
  138. if(s_hSecDll == NULL)
  139. return err;
  140. }
  141. #endif
  142. switch(ntlm->state) {
  143. case NTLMSTATE_TYPE1:
  144. default: /* for the weird cases we (re)start here */
  145. /* Create a type-1 message */
  146. result = Curl_auth_create_ntlm_type1_message(userp, passwdp, ntlm, &base64,
  147. &len);
  148. if(result)
  149. return result;
  150. if(base64) {
  151. free(*allocuserpwd);
  152. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  153. proxy ? "Proxy-" : "",
  154. base64);
  155. free(base64);
  156. if(!*allocuserpwd)
  157. return CURLE_OUT_OF_MEMORY;
  158. DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
  159. }
  160. break;
  161. case NTLMSTATE_TYPE2:
  162. /* We already received the type-2 message, create a type-3 message */
  163. result = Curl_auth_create_ntlm_type3_message(conn->data, userp, passwdp,
  164. ntlm, &base64, &len);
  165. if(result)
  166. return result;
  167. if(base64) {
  168. free(*allocuserpwd);
  169. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  170. proxy ? "Proxy-" : "",
  171. base64);
  172. free(base64);
  173. if(!*allocuserpwd)
  174. return CURLE_OUT_OF_MEMORY;
  175. DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
  176. ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */
  177. authp->done = TRUE;
  178. }
  179. break;
  180. case NTLMSTATE_TYPE3:
  181. /* connection is already authenticated,
  182. * don't send a header in future requests */
  183. ntlm->state = NTLMSTATE_LAST;
  184. /* fall-through */
  185. case NTLMSTATE_LAST:
  186. Curl_safefree(*allocuserpwd);
  187. authp->done = TRUE;
  188. break;
  189. }
  190. return CURLE_OK;
  191. }
  192. void Curl_http_ntlm_cleanup(struct connectdata *conn)
  193. {
  194. Curl_auth_ntlm_cleanup(&conn->ntlm);
  195. Curl_auth_ntlm_cleanup(&conn->proxyntlm);
  196. #if defined(NTLM_WB_ENABLED)
  197. Curl_ntlm_wb_cleanup(conn);
  198. #endif
  199. }
  200. #endif /* !CURL_DISABLE_HTTP && USE_NTLM */