http_negotiate.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, 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. struct Curl_easy *data = conn->data;
  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. if(proxy) {
  45. userp = conn->proxyuser;
  46. passwdp = conn->proxypasswd;
  47. service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
  48. data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
  49. host = conn->proxy.name;
  50. neg_ctx = &data->state.proxyneg;
  51. }
  52. else {
  53. userp = conn->user;
  54. passwdp = conn->passwd;
  55. service = data->set.str[STRING_SERVICE_NAME] ?
  56. data->set.str[STRING_SERVICE_NAME] : "HTTP";
  57. host = conn->host.name;
  58. neg_ctx = &data->state.negotiate;
  59. }
  60. /* Not set means empty */
  61. if(!userp)
  62. userp = "";
  63. if(!passwdp)
  64. passwdp = "";
  65. /* Obtain the input token, if any */
  66. header += strlen("Negotiate");
  67. while(*header && ISSPACE(*header))
  68. header++;
  69. len = strlen(header);
  70. if(!len) {
  71. /* Is this the first call in a new negotiation? */
  72. if(neg_ctx->context) {
  73. /* The server rejected our authentication and hasn't suppled any more
  74. negotiation mechanisms */
  75. return CURLE_LOGIN_DENIED;
  76. }
  77. }
  78. /* Initilise the security context and decode our challenge */
  79. return Curl_auth_decode_spnego_message(data, userp, passwdp, service, host,
  80. header, neg_ctx);
  81. }
  82. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  83. {
  84. struct negotiatedata *neg_ctx = proxy ? &conn->data->state.proxyneg :
  85. &conn->data->state.negotiate;
  86. char *base64 = NULL;
  87. size_t len = 0;
  88. char *userp;
  89. CURLcode result;
  90. result = Curl_auth_create_spnego_message(conn->data, neg_ctx, &base64, &len);
  91. if(result)
  92. return result;
  93. userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
  94. base64);
  95. if(proxy) {
  96. Curl_safefree(conn->allocptr.proxyuserpwd);
  97. conn->allocptr.proxyuserpwd = userp;
  98. }
  99. else {
  100. Curl_safefree(conn->allocptr.userpwd);
  101. conn->allocptr.userpwd = userp;
  102. }
  103. free(base64);
  104. return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
  105. }
  106. void Curl_cleanup_negotiate(struct Curl_easy *data)
  107. {
  108. Curl_auth_spnego_cleanup(&data->state.negotiate);
  109. Curl_auth_spnego_cleanup(&data->state.proxyneg);
  110. }
  111. #endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */