curl_ntlm.c 6.2 KB

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