http_negotiate.c 6.0 KB

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