Path: blob/main/crypto/heimdal/lib/krb5/error_string.c
34878 views
/*1* Copyright (c) 2001, 2003, 2005 - 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 "krb5_locl.h"3435#undef __attribute__36#define __attribute__(x)3738/**39* Clears the error message from the Kerberos 5 context.40*41* @param context The Kerberos 5 context to clear42*43* @ingroup krb5_error44*/4546KRB5_LIB_FUNCTION void KRB5_LIB_CALL47krb5_clear_error_message(krb5_context context)48{49HEIMDAL_MUTEX_lock(context->mutex);50if (context->error_string)51free(context->error_string);52context->error_code = 0;53context->error_string = NULL;54HEIMDAL_MUTEX_unlock(context->mutex);55}5657/**58* Set the context full error string for a specific error code.59* The error that is stored should be internationalized.60*61* The if context is NULL, no error string is stored.62*63* @param context Kerberos 5 context64* @param ret The error code65* @param fmt Error string for the error code66* @param ... printf(3) style parameters.67*68* @ingroup krb5_error69*/7071KRB5_LIB_FUNCTION void KRB5_LIB_CALL72krb5_set_error_message(krb5_context context, krb5_error_code ret,73const char *fmt, ...)74__attribute__ ((format (printf, 3, 4)))75{76va_list ap;7778va_start(ap, fmt);79krb5_vset_error_message (context, ret, fmt, ap);80va_end(ap);81}8283/**84* Set the context full error string for a specific error code.85*86* The if context is NULL, no error string is stored.87*88* @param context Kerberos 5 context89* @param ret The error code90* @param fmt Error string for the error code91* @param args printf(3) style parameters.92*93* @ingroup krb5_error94*/959697KRB5_LIB_FUNCTION void KRB5_LIB_CALL98krb5_vset_error_message (krb5_context context, krb5_error_code ret,99const char *fmt, va_list args)100__attribute__ ((format (printf, 3, 0)))101{102int r;103104if (context == NULL)105return;106107HEIMDAL_MUTEX_lock(context->mutex);108if (context->error_string) {109free(context->error_string);110context->error_string = NULL;111}112context->error_code = ret;113r = vasprintf(&context->error_string, fmt, args);114if (r < 0)115context->error_string = NULL;116HEIMDAL_MUTEX_unlock(context->mutex);117}118119/**120* Prepend the context full error string for a specific error code.121* The error that is stored should be internationalized.122*123* The if context is NULL, no error string is stored.124*125* @param context Kerberos 5 context126* @param ret The error code127* @param fmt Error string for the error code128* @param ... printf(3) style parameters.129*130* @ingroup krb5_error131*/132133KRB5_LIB_FUNCTION void KRB5_LIB_CALL134krb5_prepend_error_message(krb5_context context, krb5_error_code ret,135const char *fmt, ...)136__attribute__ ((format (printf, 3, 4)))137{138va_list ap;139140va_start(ap, fmt);141krb5_vprepend_error_message(context, ret, fmt, ap);142va_end(ap);143}144145/**146* Prepend the contexts's full error string for a specific error code.147*148* The if context is NULL, no error string is stored.149*150* @param context Kerberos 5 context151* @param ret The error code152* @param fmt Error string for the error code153* @param args printf(3) style parameters.154*155* @ingroup krb5_error156*/157158KRB5_LIB_FUNCTION void KRB5_LIB_CALL159krb5_vprepend_error_message(krb5_context context, krb5_error_code ret,160const char *fmt, va_list args)161__attribute__ ((format (printf, 3, 0)))162{163char *str = NULL, *str2 = NULL;164165if (context == NULL)166return;167168HEIMDAL_MUTEX_lock(context->mutex);169if (context->error_code != ret) {170HEIMDAL_MUTEX_unlock(context->mutex);171return;172}173if (vasprintf(&str, fmt, args) < 0 || str == NULL) {174HEIMDAL_MUTEX_unlock(context->mutex);175return;176}177if (context->error_string) {178int e;179180e = asprintf(&str2, "%s: %s", str, context->error_string);181free(context->error_string);182if (e < 0 || str2 == NULL)183context->error_string = NULL;184else185context->error_string = str2;186free(str);187} else188context->error_string = str;189HEIMDAL_MUTEX_unlock(context->mutex);190}191192193/**194* Return the error message in context. On error or no error string,195* the function returns NULL.196*197* @param context Kerberos 5 context198*199* @return an error string, needs to be freed with200* krb5_free_error_message(). The functions return NULL on error.201*202* @ingroup krb5_error203*/204205KRB5_LIB_FUNCTION char * KRB5_LIB_CALL206krb5_get_error_string(krb5_context context)207{208char *ret = NULL;209210HEIMDAL_MUTEX_lock(context->mutex);211if (context->error_string)212ret = strdup(context->error_string);213HEIMDAL_MUTEX_unlock(context->mutex);214return ret;215}216217KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL218krb5_have_error_string(krb5_context context)219{220char *str;221HEIMDAL_MUTEX_lock(context->mutex);222str = context->error_string;223HEIMDAL_MUTEX_unlock(context->mutex);224return str != NULL;225}226227/**228* Return the error message for `code' in context. On memory229* allocation error the function returns NULL.230*231* @param context Kerberos 5 context232* @param code Error code related to the error233*234* @return an error string, needs to be freed with235* krb5_free_error_message(). The functions return NULL on error.236*237* @ingroup krb5_error238*/239240KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL241krb5_get_error_message(krb5_context context, krb5_error_code code)242{243char *str = NULL;244const char *cstr = NULL;245char buf[128];246int free_context = 0;247248if (code == 0)249return strdup("Success");250251/*252* The MIT version of this function ignores the krb5_context253* and several widely deployed applications call krb5_get_error_message()254* with a NULL context in order to translate an error code as a255* replacement for error_message(). Another reason a NULL context256* might be provided is if the krb5_init_context() call itself257* failed.258*/259if (context)260{261HEIMDAL_MUTEX_lock(context->mutex);262if (context->error_string &&263(code == context->error_code || context->error_code == 0))264{265str = strdup(context->error_string);266}267HEIMDAL_MUTEX_unlock(context->mutex);268269if (str)270return str;271}272else273{274if (krb5_init_context(&context) == 0)275free_context = 1;276}277278if (context)279cstr = com_right_r(context->et_list, code, buf, sizeof(buf));280281if (free_context)282krb5_free_context(context);283284if (cstr)285return strdup(cstr);286287cstr = error_message(code);288if (cstr)289return strdup(cstr);290291if (asprintf(&str, "<unknown error: %d>", (int)code) == -1 || str == NULL)292return NULL;293294return str;295}296297298/**299* Free the error message returned by krb5_get_error_message().300*301* @param context Kerberos context302* @param msg error message to free, returned byg303* krb5_get_error_message().304*305* @ingroup krb5_error306*/307308KRB5_LIB_FUNCTION void KRB5_LIB_CALL309krb5_free_error_message(krb5_context context, const char *msg)310{311free(rk_UNCONST(msg));312}313314315/**316* Return the error string for the error code. The caller must not317* free the string.318*319* This function is deprecated since its not threadsafe.320*321* @param context Kerberos 5 context.322* @param code Kerberos error code.323*324* @return the error message matching code325*326* @ingroup krb5327*/328329KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL330krb5_get_err_text(krb5_context context, krb5_error_code code)331KRB5_DEPRECATED_FUNCTION("Use X instead")332{333const char *p = NULL;334if(context != NULL)335p = com_right(context->et_list, code);336if(p == NULL)337p = strerror(code);338if (p == NULL)339p = "Unknown error";340return p;341}342343344