curl_gssapi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_GSSAPI
  26. #include "curl_gssapi.h"
  27. #include "sendf.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. #if defined(__GNUC__)
  33. #define CURL_ALIGN8 __attribute__((aligned(8)))
  34. #else
  35. #define CURL_ALIGN8
  36. #endif
  37. #if defined(__GNUC__) && defined(__APPLE__)
  38. #pragma GCC diagnostic push
  39. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  40. #endif
  41. gss_OID_desc Curl_spnego_mech_oid CURL_ALIGN8 = {
  42. 6, (char *)"\x2b\x06\x01\x05\x05\x02"
  43. };
  44. gss_OID_desc Curl_krb5_mech_oid CURL_ALIGN8 = {
  45. 9, (char *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"
  46. };
  47. OM_uint32 Curl_gss_init_sec_context(
  48. struct Curl_easy *data,
  49. OM_uint32 *minor_status,
  50. gss_ctx_id_t *context,
  51. gss_name_t target_name,
  52. gss_OID mech_type,
  53. gss_channel_bindings_t input_chan_bindings,
  54. gss_buffer_t input_token,
  55. gss_buffer_t output_token,
  56. const bool mutual_auth,
  57. OM_uint32 *ret_flags)
  58. {
  59. OM_uint32 req_flags = GSS_C_REPLAY_FLAG;
  60. if(mutual_auth)
  61. req_flags |= GSS_C_MUTUAL_FLAG;
  62. if(data->set.gssapi_delegation & CURLGSSAPI_DELEGATION_POLICY_FLAG) {
  63. #ifdef GSS_C_DELEG_POLICY_FLAG
  64. req_flags |= GSS_C_DELEG_POLICY_FLAG;
  65. #else
  66. infof(data, "WARNING: support for CURLGSSAPI_DELEGATION_POLICY_FLAG not "
  67. "compiled in");
  68. #endif
  69. }
  70. if(data->set.gssapi_delegation & CURLGSSAPI_DELEGATION_FLAG)
  71. req_flags |= GSS_C_DELEG_FLAG;
  72. return gss_init_sec_context(minor_status,
  73. GSS_C_NO_CREDENTIAL, /* cred_handle */
  74. context,
  75. target_name,
  76. mech_type,
  77. req_flags,
  78. 0, /* time_req */
  79. input_chan_bindings,
  80. input_token,
  81. NULL, /* actual_mech_type */
  82. output_token,
  83. ret_flags,
  84. NULL /* time_rec */);
  85. }
  86. #define GSS_LOG_BUFFER_LEN 1024
  87. static size_t display_gss_error(OM_uint32 status, int type,
  88. char *buf, size_t len) {
  89. OM_uint32 maj_stat;
  90. OM_uint32 min_stat;
  91. OM_uint32 msg_ctx = 0;
  92. gss_buffer_desc status_string = GSS_C_EMPTY_BUFFER;
  93. do {
  94. maj_stat = gss_display_status(&min_stat,
  95. status,
  96. type,
  97. GSS_C_NO_OID,
  98. &msg_ctx,
  99. &status_string);
  100. if(maj_stat == GSS_S_COMPLETE && status_string.length > 0) {
  101. if(GSS_LOG_BUFFER_LEN > len + status_string.length + 3) {
  102. len += msnprintf(buf + len, GSS_LOG_BUFFER_LEN - len,
  103. "%.*s. ", (int)status_string.length,
  104. (char *)status_string.value);
  105. }
  106. }
  107. gss_release_buffer(&min_stat, &status_string);
  108. } while(!GSS_ERROR(maj_stat) && msg_ctx);
  109. return len;
  110. }
  111. /*
  112. * Curl_gss_log_error()
  113. *
  114. * This is used to log a GSS-API error status.
  115. *
  116. * Parameters:
  117. *
  118. * data [in] - The session handle.
  119. * prefix [in] - The prefix of the log message.
  120. * major [in] - The major status code.
  121. * minor [in] - The minor status code.
  122. */
  123. void Curl_gss_log_error(struct Curl_easy *data, const char *prefix,
  124. OM_uint32 major, OM_uint32 minor)
  125. {
  126. char buf[GSS_LOG_BUFFER_LEN];
  127. size_t len = 0;
  128. if(major != GSS_S_FAILURE)
  129. len = display_gss_error(major, GSS_C_GSS_CODE, buf, len);
  130. display_gss_error(minor, GSS_C_MECH_CODE, buf, len);
  131. infof(data, "%s%s", prefix, buf);
  132. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  133. (void)data;
  134. (void)prefix;
  135. #endif
  136. }
  137. #if defined(__GNUC__) && defined(__APPLE__)
  138. #pragma GCC diagnostic pop
  139. #endif
  140. #endif /* HAVE_GSSAPI */