/crypto/heimdal/lib/gssapi/krb5/display_status.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 199 lines · 143 code · 24 blank · 32 comment · 30 complexity · e30d715cb914c17b4b305b27fc96f2b4 MD5 · raw file

  1. /*
  2. * Copyright (c) 1998 - 2006 Kungliga Tekniska Hรถgskolan
  3. * (Royal Institute of Technology, Stockholm, Sweden).
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * 3. Neither the name of the Institute nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "gsskrb5_locl.h"
  34. static const char *
  35. calling_error(OM_uint32 v)
  36. {
  37. static const char *msgs[] = {
  38. NULL, /* 0 */
  39. "A required input parameter could not be read.", /* */
  40. "A required output parameter could not be written.", /* */
  41. "A parameter was malformed"
  42. };
  43. v >>= GSS_C_CALLING_ERROR_OFFSET;
  44. if (v == 0)
  45. return "";
  46. else if (v >= sizeof(msgs)/sizeof(*msgs))
  47. return "unknown calling error";
  48. else
  49. return msgs[v];
  50. }
  51. static const char *
  52. routine_error(OM_uint32 v)
  53. {
  54. static const char *msgs[] = {
  55. NULL, /* 0 */
  56. "An unsupported mechanism was requested",
  57. "An invalid name was supplied",
  58. "A supplied name was of an unsupported type",
  59. "Incorrect channel bindings were supplied",
  60. "An invalid status code was supplied",
  61. "A token had an invalid MIC",
  62. "No credentials were supplied, "
  63. "or the credentials were unavailable or inaccessible.",
  64. "No context has been established",
  65. "A token was invalid",
  66. "A credential was invalid",
  67. "The referenced credentials have expired",
  68. "The context has expired",
  69. "Miscellaneous failure (see text)",
  70. "The quality-of-protection requested could not be provide",
  71. "The operation is forbidden by local security policy",
  72. "The operation or option is not available",
  73. "The requested credential element already exists",
  74. "The provided name was not a mechanism name.",
  75. };
  76. v >>= GSS_C_ROUTINE_ERROR_OFFSET;
  77. if (v == 0)
  78. return "";
  79. else if (v >= sizeof(msgs)/sizeof(*msgs))
  80. return "unknown routine error";
  81. else
  82. return msgs[v];
  83. }
  84. static const char *
  85. supplementary_error(OM_uint32 v)
  86. {
  87. static const char *msgs[] = {
  88. "normal completion",
  89. "continuation call to routine required",
  90. "duplicate per-message token detected",
  91. "timed-out per-message token detected",
  92. "reordered (early) per-message token detected",
  93. "skipped predecessor token(s) detected"
  94. };
  95. v >>= GSS_C_SUPPLEMENTARY_OFFSET;
  96. if (v >= sizeof(msgs)/sizeof(*msgs))
  97. return "unknown routine error";
  98. else
  99. return msgs[v];
  100. }
  101. void
  102. _gsskrb5_clear_status (void)
  103. {
  104. krb5_context context;
  105. if (_gsskrb5_init (&context) != 0)
  106. return;
  107. krb5_clear_error_message(context);
  108. }
  109. void
  110. _gsskrb5_set_status (int ret, const char *fmt, ...)
  111. {
  112. krb5_context context;
  113. va_list args;
  114. char *str;
  115. int e;
  116. if (_gsskrb5_init (&context) != 0)
  117. return;
  118. va_start(args, fmt);
  119. e = vasprintf(&str, fmt, args);
  120. va_end(args);
  121. if (e >= 0 && str) {
  122. krb5_set_error_message(context, ret, "%s", str);
  123. free(str);
  124. }
  125. }
  126. OM_uint32 GSSAPI_CALLCONV _gsskrb5_display_status
  127. (OM_uint32 *minor_status,
  128. OM_uint32 status_value,
  129. int status_type,
  130. const gss_OID mech_type,
  131. OM_uint32 *message_context,
  132. gss_buffer_t status_string)
  133. {
  134. krb5_context context;
  135. char *buf = NULL;
  136. int e = 0;
  137. GSSAPI_KRB5_INIT (&context);
  138. status_string->length = 0;
  139. status_string->value = NULL;
  140. if (gss_oid_equal(mech_type, GSS_C_NO_OID) == 0 &&
  141. gss_oid_equal(mech_type, GSS_KRB5_MECHANISM) == 0) {
  142. *minor_status = 0;
  143. return GSS_C_GSS_CODE;
  144. }
  145. if (status_type == GSS_C_GSS_CODE) {
  146. if (GSS_SUPPLEMENTARY_INFO(status_value))
  147. e = asprintf(&buf, "%s",
  148. supplementary_error(GSS_SUPPLEMENTARY_INFO(status_value)));
  149. else
  150. e = asprintf (&buf, "%s %s",
  151. calling_error(GSS_CALLING_ERROR(status_value)),
  152. routine_error(GSS_ROUTINE_ERROR(status_value)));
  153. } else if (status_type == GSS_C_MECH_CODE) {
  154. const char *buf2 = krb5_get_error_message(context, status_value);
  155. if (buf2) {
  156. buf = strdup(buf2);
  157. krb5_free_error_message(context, buf2);
  158. } else {
  159. e = asprintf(&buf, "unknown mech error-code %u",
  160. (unsigned)status_value);
  161. }
  162. } else {
  163. *minor_status = EINVAL;
  164. return GSS_S_BAD_STATUS;
  165. }
  166. if (e < 0 || buf == NULL) {
  167. *minor_status = ENOMEM;
  168. return GSS_S_FAILURE;
  169. }
  170. *message_context = 0;
  171. *minor_status = 0;
  172. status_string->length = strlen(buf);
  173. status_string->value = buf;
  174. return GSS_S_COMPLETE;
  175. }