http_negotiate.c 6.6 KB

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