http_negotiate.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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_SPNEGO)
  24. #include "urldata.h"
  25. #include "sendf.h"
  26. #include "http_negotiate.h"
  27. #include "vauth/vauth.h"
  28. /* The last 3 #include files should be in this order */
  29. #include "curl_printf.h"
  30. #include "curl_memory.h"
  31. #include "memdebug.h"
  32. CURLcode Curl_input_negotiate(struct connectdata *conn, bool proxy,
  33. const char *header)
  34. {
  35. CURLcode result;
  36. struct Curl_easy *data = conn->data;
  37. size_t len;
  38. /* Point to the username, password, service and host */
  39. const char *userp;
  40. const char *passwdp;
  41. const char *service;
  42. const char *host;
  43. /* Point to the correct struct with this */
  44. struct negotiatedata *neg_ctx;
  45. curlnegotiate state;
  46. if(proxy) {
  47. userp = conn->http_proxy.user;
  48. passwdp = conn->http_proxy.passwd;
  49. service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
  50. data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
  51. host = conn->http_proxy.host.name;
  52. neg_ctx = &conn->proxyneg;
  53. state = conn->proxy_negotiate_state;
  54. }
  55. else {
  56. userp = conn->user;
  57. passwdp = conn->passwd;
  58. service = data->set.str[STRING_SERVICE_NAME] ?
  59. data->set.str[STRING_SERVICE_NAME] : "HTTP";
  60. host = conn->host.name;
  61. neg_ctx = &conn->negotiate;
  62. state = conn->http_negotiate_state;
  63. }
  64. /* Not set means empty */
  65. if(!userp)
  66. userp = "";
  67. if(!passwdp)
  68. passwdp = "";
  69. /* Obtain the input token, if any */
  70. header += strlen("Negotiate");
  71. while(*header && ISSPACE(*header))
  72. header++;
  73. len = strlen(header);
  74. neg_ctx->havenegdata = len != 0;
  75. if(!len) {
  76. if(state == GSS_AUTHSUCC) {
  77. infof(conn->data, "Negotiate auth restarted\n");
  78. Curl_http_auth_cleanup_negotiate(conn);
  79. }
  80. else if(state != GSS_AUTHNONE) {
  81. /* The server rejected our authentication and hasn't supplied any more
  82. negotiation mechanisms */
  83. Curl_http_auth_cleanup_negotiate(conn);
  84. return CURLE_LOGIN_DENIED;
  85. }
  86. }
  87. /* Supports SSL channel binding for Windows ISS extended protection */
  88. #if defined(USE_WINDOWS_SSPI) && defined(SECPKG_ATTR_ENDPOINT_BINDINGS)
  89. neg_ctx->sslContext = conn->sslContext;
  90. #endif
  91. /* Initialize the security context and decode our challenge */
  92. result = Curl_auth_decode_spnego_message(data, userp, passwdp, service,
  93. host, header, neg_ctx);
  94. if(result)
  95. Curl_http_auth_cleanup_negotiate(conn);
  96. return result;
  97. }
  98. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  99. {
  100. struct negotiatedata *neg_ctx = proxy ? &conn->proxyneg :
  101. &conn->negotiate;
  102. struct auth *authp = proxy ? &conn->data->state.authproxy :
  103. &conn->data->state.authhost;
  104. curlnegotiate *state = proxy ? &conn->proxy_negotiate_state :
  105. &conn->http_negotiate_state;
  106. char *base64 = NULL;
  107. size_t len = 0;
  108. char *userp;
  109. CURLcode result;
  110. authp->done = FALSE;
  111. if(*state == GSS_AUTHRECV) {
  112. if(neg_ctx->havenegdata) {
  113. neg_ctx->havemultiplerequests = TRUE;
  114. }
  115. }
  116. else if(*state == GSS_AUTHSUCC) {
  117. if(!neg_ctx->havenoauthpersist) {
  118. neg_ctx->noauthpersist = !neg_ctx->havemultiplerequests;
  119. }
  120. }
  121. if(neg_ctx->noauthpersist ||
  122. (*state != GSS_AUTHDONE && *state != GSS_AUTHSUCC)) {
  123. if(neg_ctx->noauthpersist && *state == GSS_AUTHSUCC) {
  124. infof(conn->data, "Curl_output_negotiate, "
  125. "no persistent authentication: cleanup existing context");
  126. Curl_http_auth_cleanup_negotiate(conn);
  127. }
  128. if(!neg_ctx->context) {
  129. result = Curl_input_negotiate(conn, proxy, "Negotiate");
  130. if(result == CURLE_AUTH_ERROR) {
  131. /* negotiate auth failed, let's continue unauthenticated to stay
  132. * compatible with the behavior before curl-7_64_0-158-g6c6035532 */
  133. authp->done = TRUE;
  134. return CURLE_OK;
  135. }
  136. else if(result)
  137. return result;
  138. }
  139. result = Curl_auth_create_spnego_message(conn->data,
  140. neg_ctx, &base64, &len);
  141. if(result)
  142. return result;
  143. userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
  144. base64);
  145. if(proxy) {
  146. Curl_safefree(conn->allocptr.proxyuserpwd);
  147. conn->allocptr.proxyuserpwd = userp;
  148. }
  149. else {
  150. Curl_safefree(conn->allocptr.userpwd);
  151. conn->allocptr.userpwd = userp;
  152. }
  153. free(base64);
  154. if(userp == NULL) {
  155. return CURLE_OUT_OF_MEMORY;
  156. }
  157. *state = GSS_AUTHSENT;
  158. #ifdef HAVE_GSSAPI
  159. if(neg_ctx->status == GSS_S_COMPLETE ||
  160. neg_ctx->status == GSS_S_CONTINUE_NEEDED) {
  161. *state = GSS_AUTHDONE;
  162. }
  163. #else
  164. #ifdef USE_WINDOWS_SSPI
  165. if(neg_ctx->status == SEC_E_OK ||
  166. neg_ctx->status == SEC_I_CONTINUE_NEEDED) {
  167. *state = GSS_AUTHDONE;
  168. }
  169. #endif
  170. #endif
  171. }
  172. if(*state == GSS_AUTHDONE || *state == GSS_AUTHSUCC) {
  173. /* connection is already authenticated,
  174. * don't send a header in future requests */
  175. authp->done = TRUE;
  176. }
  177. neg_ctx->havenegdata = FALSE;
  178. return CURLE_OK;
  179. }
  180. void Curl_http_auth_cleanup_negotiate(struct connectdata *conn)
  181. {
  182. conn->http_negotiate_state = GSS_AUTHNONE;
  183. conn->proxy_negotiate_state = GSS_AUTHNONE;
  184. Curl_auth_cleanup_spnego(&conn->negotiate);
  185. Curl_auth_cleanup_spnego(&conn->proxyneg);
  186. }
  187. #endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */