2
0

curl_ntlm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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(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, bool proxy)
  97. {
  98. char *base64 = NULL;
  99. size_t len = 0;
  100. CURLcode result;
  101. /* point to the address of the pointer that holds the string to send to the
  102. server, which is for a plain host or for a HTTP proxy */
  103. char **allocuserpwd;
  104. /* point to the name and password for this */
  105. const char *userp;
  106. const char *passwdp;
  107. /* point to the correct struct with this */
  108. struct ntlmdata *ntlm;
  109. struct auth *authp;
  110. DEBUGASSERT(conn);
  111. DEBUGASSERT(conn->data);
  112. #ifdef USE_NSS
  113. if(CURLE_OK != Curl_nss_force_init(conn->data))
  114. return CURLE_OUT_OF_MEMORY;
  115. #endif
  116. if(proxy) {
  117. allocuserpwd = &conn->allocptr.proxyuserpwd;
  118. userp = conn->proxyuser;
  119. passwdp = conn->proxypasswd;
  120. ntlm = &conn->proxyntlm;
  121. authp = &conn->data->state.authproxy;
  122. }
  123. else {
  124. allocuserpwd = &conn->allocptr.userpwd;
  125. userp = conn->user;
  126. passwdp = conn->passwd;
  127. ntlm = &conn->ntlm;
  128. authp = &conn->data->state.authhost;
  129. }
  130. authp->done = FALSE;
  131. /* not set means empty */
  132. if(!userp)
  133. userp = "";
  134. if(!passwdp)
  135. passwdp = "";
  136. #ifdef USE_WINDOWS_SSPI
  137. if(s_hSecDll == NULL) {
  138. /* not thread safe and leaks - use curl_global_init() to avoid */
  139. CURLcode err = Curl_sspi_global_init();
  140. if(s_hSecDll == NULL)
  141. return err;
  142. }
  143. #endif
  144. switch(ntlm->state) {
  145. case NTLMSTATE_TYPE1:
  146. default: /* for the weird cases we (re)start here */
  147. /* Create a type-1 message */
  148. result = Curl_ntlm_create_type1_message(userp, passwdp, ntlm, &base64,
  149. &len);
  150. if(result)
  151. return result;
  152. if(base64) {
  153. Curl_safefree(*allocuserpwd);
  154. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  155. proxy ? "Proxy-" : "",
  156. base64);
  157. free(base64);
  158. if(!*allocuserpwd)
  159. return CURLE_OUT_OF_MEMORY;
  160. DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
  161. }
  162. break;
  163. case NTLMSTATE_TYPE2:
  164. /* We already received the type-2 message, create a type-3 message */
  165. result = Curl_ntlm_create_type3_message(conn->data, userp, passwdp,
  166. ntlm, &base64, &len);
  167. if(result)
  168. return result;
  169. if(base64) {
  170. Curl_safefree(*allocuserpwd);
  171. *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
  172. proxy ? "Proxy-" : "",
  173. base64);
  174. free(base64);
  175. if(!*allocuserpwd)
  176. return CURLE_OUT_OF_MEMORY;
  177. DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
  178. ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */
  179. authp->done = TRUE;
  180. }
  181. break;
  182. case NTLMSTATE_TYPE3:
  183. /* connection is already authenticated,
  184. * don't send a header in future requests */
  185. Curl_safefree(*allocuserpwd);
  186. authp->done = TRUE;
  187. break;
  188. }
  189. return CURLE_OK;
  190. }
  191. void Curl_http_ntlm_cleanup(struct connectdata *conn)
  192. {
  193. #ifdef USE_WINDOWS_SSPI
  194. Curl_ntlm_sspi_cleanup(&conn->ntlm);
  195. Curl_ntlm_sspi_cleanup(&conn->proxyntlm);
  196. #elif defined(NTLM_WB_ENABLED)
  197. Curl_ntlm_wb_cleanup(conn);
  198. #else
  199. (void)conn;
  200. #endif
  201. #ifndef USE_WINDOWS_SSPI
  202. Curl_safefree(conn->ntlm.target_info);
  203. conn->ntlm.target_info_len = 0;
  204. Curl_safefree(conn->proxyntlm.target_info);
  205. conn->proxyntlm.target_info_len = 0;
  206. #endif
  207. }
  208. #endif /* USE_NTLM */