curl_ntlm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 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. #ifdef 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 "url.h"
  38. #include "curl_memory.h"
  39. #define _MPRINTF_REPLACE /* use our functions only */
  40. #include <curl/mprintf.h>
  41. #if defined(USE_NSS)
  42. #include "vtls/nssg.h"
  43. #elif defined(USE_WINDOWS_SSPI)
  44. #include "curl_sspi.h"
  45. #endif
  46. /* The last #include file should be: */
  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. #ifdef USE_NSS
  62. result = Curl_nss_force_init(conn->data);
  63. if(result)
  64. return result;
  65. #endif
  66. ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
  67. if(checkprefix("NTLM", header)) {
  68. header += strlen("NTLM");
  69. while(*header && ISSPACE(*header))
  70. header++;
  71. if(*header) {
  72. result = Curl_ntlm_decode_type2_message(conn->data, header, ntlm);
  73. if(CURLE_OK != result)
  74. return result;
  75. ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */
  76. }
  77. else {
  78. if(ntlm->state == NTLMSTATE_TYPE3) {
  79. infof(conn->data, "NTLM handshake rejected\n");
  80. Curl_http_ntlm_cleanup(conn);
  81. ntlm->state = NTLMSTATE_NONE;
  82. return CURLE_REMOTE_ACCESS_DENIED;
  83. }
  84. else if(ntlm->state >= NTLMSTATE_TYPE1) {
  85. infof(conn->data, "NTLM handshake failure (internal error)\n");
  86. return CURLE_REMOTE_ACCESS_DENIED;
  87. }
  88. ntlm->state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
  89. }
  90. }
  91. return result;
  92. }
  93. /*
  94. * This is for creating ntlm header output
  95. */
  96. CURLcode Curl_output_ntlm(struct connectdata *conn,
  97. bool proxy)
  98. {
  99. char *base64 = NULL;
  100. size_t len = 0;
  101. CURLcode error;
  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 name and password for this */
  106. const char *userp;
  107. const char *passwdp;
  108. /* point to the correct struct with this */
  109. struct ntlmdata *ntlm;
  110. struct auth *authp;
  111. DEBUGASSERT(conn);
  112. DEBUGASSERT(conn->data);
  113. #ifdef USE_NSS
  114. if(CURLE_OK != Curl_nss_force_init(conn->data))
  115. return CURLE_OUT_OF_MEMORY;
  116. #endif
  117. if(proxy) {
  118. allocuserpwd = &conn->allocptr.proxyuserpwd;
  119. userp = conn->proxyuser;
  120. passwdp = conn->proxypasswd;
  121. ntlm = &conn->proxyntlm;
  122. authp = &conn->data->state.authproxy;
  123. }
  124. else {
  125. allocuserpwd = &conn->allocptr.userpwd;
  126. userp = conn->user;
  127. passwdp = conn->passwd;
  128. ntlm = &conn->ntlm;
  129. authp = &conn->data->state.authhost;
  130. }
  131. authp->done = FALSE;
  132. /* not set means empty */
  133. if(!userp)
  134. userp = "";
  135. if(!passwdp)
  136. passwdp = "";
  137. #ifdef USE_WINDOWS_SSPI
  138. if(s_hSecDll == NULL) {
  139. /* not thread safe and leaks - use curl_global_init() to avoid */
  140. CURLcode err = Curl_sspi_global_init();
  141. if(s_hSecDll == NULL)
  142. return err;
  143. }
  144. #endif
  145. switch(ntlm->state) {
  146. case NTLMSTATE_TYPE1:
  147. default: /* for the weird cases we (re)start here */
  148. /* Create a type-1 message */
  149. error = Curl_ntlm_create_type1_message(userp, passwdp, ntlm, &base64,
  150. &len);
  151. if(error)
  152. return error;
  153. if(base64) {
  154. Curl_safefree(*allocuserpwd);
  155. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  156. proxy ? "Proxy-" : "",
  157. base64);
  158. free(base64);
  159. if(!*allocuserpwd)
  160. return CURLE_OUT_OF_MEMORY;
  161. DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
  162. }
  163. break;
  164. case NTLMSTATE_TYPE2:
  165. /* We already received the type-2 message, create a type-3 message */
  166. error = Curl_ntlm_create_type3_message(conn->data, userp, passwdp,
  167. ntlm, &base64, &len);
  168. if(error)
  169. return error;
  170. if(base64) {
  171. Curl_safefree(*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, "**** %s\n ", *allocuserpwd));
  179. ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */
  180. authp->done = TRUE;
  181. }
  182. break;
  183. case NTLMSTATE_TYPE3:
  184. /* connection is already authenticated,
  185. * don't send a header in future requests */
  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. #ifdef USE_WINDOWS_SSPI
  195. Curl_ntlm_sspi_cleanup(&conn->ntlm);
  196. Curl_ntlm_sspi_cleanup(&conn->proxyntlm);
  197. #elif defined(NTLM_WB_ENABLED)
  198. Curl_ntlm_wb_cleanup(conn);
  199. #else
  200. (void)conn;
  201. #endif
  202. #ifndef USE_WINDOWS_SSPI
  203. Curl_safefree(conn->ntlm.target_info);
  204. conn->ntlm.target_info_len = 0;
  205. Curl_safefree(conn->proxyntlm.target_info);
  206. conn->proxyntlm.target_info_len = 0;
  207. #endif
  208. }
  209. #endif /* USE_NTLM */