http_negotiate.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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 http://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(HAVE_GSSAPI) && !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
  24. #include "urldata.h"
  25. #include "sendf.h"
  26. #include "curl_gssapi.h"
  27. #include "rawstr.h"
  28. #include "curl_base64.h"
  29. #include "http_negotiate.h"
  30. #include "curl_memory.h"
  31. #include "url.h"
  32. #define _MPRINTF_REPLACE /* use our functions only */
  33. #include <curl/mprintf.h>
  34. /* The last #include file should be: */
  35. #include "memdebug.h"
  36. static int
  37. get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
  38. {
  39. OM_uint32 major_status, minor_status;
  40. gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
  41. char name[2048];
  42. const char* service = "HTTP";
  43. token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
  44. conn->host.name) + 1;
  45. if(token.length + 1 > sizeof(name))
  46. return EMSGSIZE;
  47. snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
  48. conn->host.name);
  49. token.value = (void *) name;
  50. major_status = gss_import_name(&minor_status,
  51. &token,
  52. GSS_C_NT_HOSTBASED_SERVICE,
  53. server);
  54. return GSS_ERROR(major_status) ? -1 : 0;
  55. }
  56. CURLcode Curl_input_negotiate(struct connectdata *conn, bool proxy,
  57. const char *header)
  58. {
  59. struct SessionHandle *data = conn->data;
  60. struct negotiatedata *neg_ctx = proxy?&data->state.proxyneg:
  61. &data->state.negotiate;
  62. OM_uint32 major_status, minor_status, discard_st;
  63. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  64. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  65. int ret;
  66. size_t len;
  67. size_t rawlen = 0;
  68. CURLcode result;
  69. if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
  70. /* We finished successfully our part of authentication, but server
  71. * rejected it (since we're again here). Exit with an error since we
  72. * can't invent anything better */
  73. Curl_cleanup_negotiate(data);
  74. return CURLE_LOGIN_DENIED;
  75. }
  76. if(neg_ctx->server_name == NULL &&
  77. get_gss_name(conn, proxy, &neg_ctx->server_name))
  78. return CURLE_OUT_OF_MEMORY;
  79. header += strlen("Negotiate");
  80. while(*header && ISSPACE(*header))
  81. header++;
  82. len = strlen(header);
  83. if(len > 0) {
  84. result = Curl_base64_decode(header, (unsigned char **)&input_token.value,
  85. &rawlen);
  86. if(result)
  87. return result;
  88. if(!rawlen)
  89. return CURLE_BAD_CONTENT_ENCODING;
  90. input_token.length = rawlen;
  91. DEBUGASSERT(input_token.value != NULL);
  92. }
  93. major_status = Curl_gss_init_sec_context(data,
  94. &minor_status,
  95. &neg_ctx->context,
  96. neg_ctx->server_name,
  97. &Curl_spnego_mech_oid,
  98. GSS_C_NO_CHANNEL_BINDINGS,
  99. &input_token,
  100. &output_token,
  101. TRUE,
  102. NULL);
  103. Curl_safefree(input_token.value);
  104. neg_ctx->status = major_status;
  105. if(GSS_ERROR(major_status)) {
  106. if(output_token.value)
  107. gss_release_buffer(&discard_st, &output_token);
  108. Curl_gss_log_error(conn->data, minor_status,
  109. "gss_init_sec_context() failed: ");
  110. return CURLE_OUT_OF_MEMORY;
  111. }
  112. if(!output_token.value || !output_token.length) {
  113. if(output_token.value)
  114. gss_release_buffer(&discard_st, &output_token);
  115. return CURLE_OUT_OF_MEMORY;
  116. }
  117. neg_ctx->output_token = output_token;
  118. return CURLE_OK;
  119. }
  120. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  121. {
  122. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  123. &conn->data->state.negotiate;
  124. char *encoded = NULL;
  125. size_t len = 0;
  126. char *userp;
  127. CURLcode result;
  128. OM_uint32 discard_st;
  129. result = Curl_base64_encode(conn->data,
  130. neg_ctx->output_token.value,
  131. neg_ctx->output_token.length,
  132. &encoded, &len);
  133. if(result) {
  134. gss_release_buffer(&discard_st, &neg_ctx->output_token);
  135. neg_ctx->output_token.value = NULL;
  136. neg_ctx->output_token.length = 0;
  137. return result;
  138. }
  139. if(!encoded || !len) {
  140. gss_release_buffer(&discard_st, &neg_ctx->output_token);
  141. neg_ctx->output_token.value = NULL;
  142. neg_ctx->output_token.length = 0;
  143. return CURLE_REMOTE_ACCESS_DENIED;
  144. }
  145. userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
  146. encoded);
  147. if(proxy) {
  148. Curl_safefree(conn->allocptr.proxyuserpwd);
  149. conn->allocptr.proxyuserpwd = userp;
  150. }
  151. else {
  152. Curl_safefree(conn->allocptr.userpwd);
  153. conn->allocptr.userpwd = userp;
  154. }
  155. Curl_safefree(encoded);
  156. return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
  157. }
  158. static void cleanup(struct negotiatedata *neg_ctx)
  159. {
  160. OM_uint32 minor_status;
  161. if(neg_ctx->context != GSS_C_NO_CONTEXT)
  162. gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
  163. if(neg_ctx->output_token.value)
  164. gss_release_buffer(&minor_status, &neg_ctx->output_token);
  165. if(neg_ctx->server_name != GSS_C_NO_NAME)
  166. gss_release_name(&minor_status, &neg_ctx->server_name);
  167. memset(neg_ctx, 0, sizeof(*neg_ctx));
  168. }
  169. void Curl_cleanup_negotiate(struct SessionHandle *data)
  170. {
  171. cleanup(&data->state.negotiate);
  172. cleanup(&data->state.proxyneg);
  173. }
  174. #endif /* HAVE_GSSAPI && !CURL_DISABLE_HTTP && USE_SPNEGO */