2
0

http_negotiate.c 6.3 KB

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