Path: blob/main/crypto/heimdal/lib/gssapi/krb5/display_status.c
34923 views
/*1* Copyright (c) 1998 - 2006 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "gsskrb5_locl.h"3435static const char *36calling_error(OM_uint32 v)37{38static const char *msgs[] = {39NULL, /* 0 */40"A required input parameter could not be read.", /* */41"A required output parameter could not be written.", /* */42"A parameter was malformed"43};4445v >>= GSS_C_CALLING_ERROR_OFFSET;4647if (v == 0)48return "";49else if (v >= sizeof(msgs)/sizeof(*msgs))50return "unknown calling error";51else52return msgs[v];53}5455static const char *56routine_error(OM_uint32 v)57{58static const char *msgs[] = {59NULL, /* 0 */60"An unsupported mechanism was requested",61"An invalid name was supplied",62"A supplied name was of an unsupported type",63"Incorrect channel bindings were supplied",64"An invalid status code was supplied",65"A token had an invalid MIC",66"No credentials were supplied, "67"or the credentials were unavailable or inaccessible.",68"No context has been established",69"A token was invalid",70"A credential was invalid",71"The referenced credentials have expired",72"The context has expired",73"Miscellaneous failure (see text)",74"The quality-of-protection requested could not be provide",75"The operation is forbidden by local security policy",76"The operation or option is not available",77"The requested credential element already exists",78"The provided name was not a mechanism name.",79};8081v >>= GSS_C_ROUTINE_ERROR_OFFSET;8283if (v == 0)84return "";85else if (v >= sizeof(msgs)/sizeof(*msgs))86return "unknown routine error";87else88return msgs[v];89}9091static const char *92supplementary_error(OM_uint32 v)93{94static const char *msgs[] = {95"normal completion",96"continuation call to routine required",97"duplicate per-message token detected",98"timed-out per-message token detected",99"reordered (early) per-message token detected",100"skipped predecessor token(s) detected"101};102103v >>= GSS_C_SUPPLEMENTARY_OFFSET;104105if (v >= sizeof(msgs)/sizeof(*msgs))106return "unknown routine error";107else108return msgs[v];109}110111void112_gsskrb5_clear_status (void)113{114krb5_context context;115116if (_gsskrb5_init (&context) != 0)117return;118krb5_clear_error_message(context);119}120121void122_gsskrb5_set_status (int ret, const char *fmt, ...)123{124krb5_context context;125va_list args;126char *str;127int e;128129if (_gsskrb5_init (&context) != 0)130return;131132va_start(args, fmt);133e = vasprintf(&str, fmt, args);134va_end(args);135if (e >= 0 && str) {136krb5_set_error_message(context, ret, "%s", str);137free(str);138}139}140141OM_uint32 GSSAPI_CALLCONV _gsskrb5_display_status142(OM_uint32 *minor_status,143OM_uint32 status_value,144int status_type,145const gss_OID mech_type,146OM_uint32 *message_context,147gss_buffer_t status_string)148{149krb5_context context;150char *buf = NULL;151int e = 0;152153GSSAPI_KRB5_INIT (&context);154155status_string->length = 0;156status_string->value = NULL;157158if (gss_oid_equal(mech_type, GSS_C_NO_OID) == 0 &&159gss_oid_equal(mech_type, GSS_KRB5_MECHANISM) == 0) {160*minor_status = 0;161return GSS_C_GSS_CODE;162}163164if (status_type == GSS_C_GSS_CODE) {165if (GSS_SUPPLEMENTARY_INFO(status_value))166e = asprintf(&buf, "%s",167supplementary_error(GSS_SUPPLEMENTARY_INFO(status_value)));168else169e = asprintf (&buf, "%s %s",170calling_error(GSS_CALLING_ERROR(status_value)),171routine_error(GSS_ROUTINE_ERROR(status_value)));172} else if (status_type == GSS_C_MECH_CODE) {173const char *buf2 = krb5_get_error_message(context, status_value);174if (buf2) {175buf = strdup(buf2);176krb5_free_error_message(context, buf2);177} else {178e = asprintf(&buf, "unknown mech error-code %u",179(unsigned)status_value);180}181} else {182*minor_status = EINVAL;183return GSS_S_BAD_STATUS;184}185186if (e < 0 || buf == NULL) {187*minor_status = ENOMEM;188return GSS_S_FAILURE;189}190191*message_context = 0;192*minor_status = 0;193194status_string->length = strlen(buf);195status_string->value = buf;196197return GSS_S_COMPLETE;198}199200201