http_negotiate.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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 = &data->state.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 = &data->state.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. if(!len) {
  72. /* Is this the first call in a new negotiation? */
  73. if(neg_ctx->context) {
  74. /* The server rejected our authentication and hasn't suppled any more
  75. negotiation mechanisms */
  76. return CURLE_LOGIN_DENIED;
  77. }
  78. }
  79. /* Initialize the security context and decode our challenge */
  80. result = Curl_auth_decode_spnego_message(data, userp, passwdp, service,
  81. host, header, neg_ctx);
  82. if(result)
  83. Curl_auth_spnego_cleanup(neg_ctx);
  84. return result;
  85. }
  86. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  87. {
  88. struct negotiatedata *neg_ctx = proxy ? &conn->data->state.proxyneg :
  89. &conn->data->state.negotiate;
  90. char *base64 = NULL;
  91. size_t len = 0;
  92. char *userp;
  93. CURLcode result;
  94. result = Curl_auth_create_spnego_message(conn->data, neg_ctx, &base64, &len);
  95. if(result)
  96. return result;
  97. userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
  98. base64);
  99. if(proxy) {
  100. Curl_safefree(conn->allocptr.proxyuserpwd);
  101. conn->allocptr.proxyuserpwd = userp;
  102. }
  103. else {
  104. Curl_safefree(conn->allocptr.userpwd);
  105. conn->allocptr.userpwd = userp;
  106. }
  107. free(base64);
  108. return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
  109. }
  110. void Curl_cleanup_negotiate(struct Curl_easy *data)
  111. {
  112. Curl_auth_spnego_cleanup(&data->state.negotiate);
  113. Curl_auth_spnego_cleanup(&data->state.proxyneg);
  114. }
  115. #endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */