http_negotiate.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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. #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. static void
  62. log_gss_error(struct connectdata *conn, OM_uint32 error_status,
  63. const char *prefix)
  64. {
  65. OM_uint32 maj_stat, min_stat;
  66. OM_uint32 msg_ctx = 0;
  67. gss_buffer_desc status_string;
  68. char buf[1024];
  69. size_t len;
  70. snprintf(buf, sizeof(buf), "%s", prefix);
  71. len = strlen(buf);
  72. do {
  73. maj_stat = gss_display_status(&min_stat,
  74. error_status,
  75. GSS_C_MECH_CODE,
  76. GSS_C_NO_OID,
  77. &msg_ctx,
  78. &status_string);
  79. if(sizeof(buf) > len + status_string.length + 1) {
  80. snprintf(buf + len, sizeof(buf) - len,
  81. ": %s", (char*) status_string.value);
  82. len += status_string.length;
  83. }
  84. gss_release_buffer(&min_stat, &status_string);
  85. } while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
  86. infof(conn->data, "%s\n", buf);
  87. }
  88. /* returning zero (0) means success, everything else is treated as "failure"
  89. with no care exactly what the failure was */
  90. int Curl_input_negotiate(struct connectdata *conn, bool proxy,
  91. const char *header)
  92. {
  93. struct SessionHandle *data = conn->data;
  94. struct negotiatedata *neg_ctx = proxy?&data->state.proxyneg:
  95. &data->state.negotiate;
  96. OM_uint32 major_status, minor_status, discard_st;
  97. gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
  98. gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
  99. int ret;
  100. size_t len;
  101. size_t rawlen = 0;
  102. CURLcode result;
  103. if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
  104. /* We finished successfully our part of authentication, but server
  105. * rejected it (since we're again here). Exit with an error since we
  106. * can't invent anything better */
  107. Curl_cleanup_negotiate(data);
  108. return -1;
  109. }
  110. if(neg_ctx->server_name == NULL &&
  111. (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
  112. return ret;
  113. header += strlen("Negotiate");
  114. while(*header && ISSPACE(*header))
  115. header++;
  116. len = strlen(header);
  117. if(len > 0) {
  118. result = Curl_base64_decode(header, (unsigned char **)&input_token.value,
  119. &rawlen);
  120. if(result || rawlen == 0)
  121. return -1;
  122. input_token.length = rawlen;
  123. DEBUGASSERT(input_token.value != NULL);
  124. }
  125. major_status = Curl_gss_init_sec_context(data,
  126. &minor_status,
  127. &neg_ctx->context,
  128. neg_ctx->server_name,
  129. &Curl_spnego_mech_oid,
  130. GSS_C_NO_CHANNEL_BINDINGS,
  131. &input_token,
  132. &output_token,
  133. NULL);
  134. Curl_safefree(input_token.value);
  135. neg_ctx->status = major_status;
  136. if(GSS_ERROR(major_status)) {
  137. if(output_token.value)
  138. gss_release_buffer(&discard_st, &output_token);
  139. log_gss_error(conn, minor_status, "gss_init_sec_context() failed: ");
  140. return -1;
  141. }
  142. if(!output_token.value || !output_token.length) {
  143. if(output_token.value)
  144. gss_release_buffer(&discard_st, &output_token);
  145. return -1;
  146. }
  147. neg_ctx->output_token = output_token;
  148. return 0;
  149. }
  150. CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
  151. {
  152. struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
  153. &conn->data->state.negotiate;
  154. char *encoded = NULL;
  155. size_t len = 0;
  156. char *userp;
  157. CURLcode result;
  158. OM_uint32 discard_st;
  159. result = Curl_base64_encode(conn->data,
  160. neg_ctx->output_token.value,
  161. neg_ctx->output_token.length,
  162. &encoded, &len);
  163. if(result) {
  164. gss_release_buffer(&discard_st, &neg_ctx->output_token);
  165. neg_ctx->output_token.value = NULL;
  166. neg_ctx->output_token.length = 0;
  167. return result;
  168. }
  169. if(!encoded || !len) {
  170. gss_release_buffer(&discard_st, &neg_ctx->output_token);
  171. neg_ctx->output_token.value = NULL;
  172. neg_ctx->output_token.length = 0;
  173. return CURLE_REMOTE_ACCESS_DENIED;
  174. }
  175. userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
  176. encoded);
  177. if(proxy) {
  178. Curl_safefree(conn->allocptr.proxyuserpwd);
  179. conn->allocptr.proxyuserpwd = userp;
  180. }
  181. else {
  182. Curl_safefree(conn->allocptr.userpwd);
  183. conn->allocptr.userpwd = userp;
  184. }
  185. Curl_safefree(encoded);
  186. return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
  187. }
  188. static void cleanup(struct negotiatedata *neg_ctx)
  189. {
  190. OM_uint32 minor_status;
  191. if(neg_ctx->context != GSS_C_NO_CONTEXT)
  192. gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
  193. if(neg_ctx->output_token.value)
  194. gss_release_buffer(&minor_status, &neg_ctx->output_token);
  195. if(neg_ctx->server_name != GSS_C_NO_NAME)
  196. gss_release_name(&minor_status, &neg_ctx->server_name);
  197. memset(neg_ctx, 0, sizeof(*neg_ctx));
  198. }
  199. void Curl_cleanup_negotiate(struct SessionHandle *data)
  200. {
  201. cleanup(&data->state.negotiate);
  202. cleanup(&data->state.proxyneg);
  203. }
  204. #endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */
  205. #endif /* HAVE_GSSAPI */