curl_ntlm.c 6.3 KB

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