#include "krb5_locl.h"
#undef HEIMDAL_PRINTF_ATTRIBUTE
#define HEIMDAL_PRINTF_ATTRIBUTE(x)
KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_clear_error_message(krb5_context context)
{
HEIMDAL_MUTEX_lock(context->mutex);
if (context->error_string)
free(context->error_string);
context->error_code = 0;
context->error_string = NULL;
HEIMDAL_MUTEX_unlock(context->mutex);
}
KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_set_error_message(krb5_context context, krb5_error_code ret,
const char *fmt, ...)
HEIMDAL_PRINTF_ATTRIBUTE((printf, 3, 4))
{
va_list ap;
va_start(ap, fmt);
krb5_vset_error_message (context, ret, fmt, ap);
va_end(ap);
}
KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_vset_error_message (krb5_context context, krb5_error_code ret,
const char *fmt, va_list args)
HEIMDAL_PRINTF_ATTRIBUTE((printf, 3, 0))
{
int r;
if (context == NULL)
return;
HEIMDAL_MUTEX_lock(context->mutex);
if (context->error_string) {
free(context->error_string);
context->error_string = NULL;
}
context->error_code = ret;
r = vasprintf(&context->error_string, fmt, args);
if (r < 0)
context->error_string = NULL;
HEIMDAL_MUTEX_unlock(context->mutex);
if (context->error_string)
_krb5_debugx(context, 100, "set-error: %d: %s", (int)ret, context->error_string);
}
KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_prepend_error_message(krb5_context context, krb5_error_code ret,
const char *fmt, ...)
HEIMDAL_PRINTF_ATTRIBUTE((printf, 3, 4))
{
va_list ap;
va_start(ap, fmt);
krb5_vprepend_error_message(context, ret, fmt, ap);
va_end(ap);
}
KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_vprepend_error_message(krb5_context context, krb5_error_code ret,
const char *fmt, va_list args)
HEIMDAL_PRINTF_ATTRIBUTE((printf, 3, 0))
{
char *str = NULL, *str2 = NULL;
if (context == NULL)
return;
HEIMDAL_MUTEX_lock(context->mutex);
if (context->error_code != ret) {
HEIMDAL_MUTEX_unlock(context->mutex);
return;
}
if (vasprintf(&str, fmt, args) < 0 || str == NULL) {
HEIMDAL_MUTEX_unlock(context->mutex);
return;
}
if (context->error_string) {
int e;
e = asprintf(&str2, "%s: %s", str, context->error_string);
free(context->error_string);
if (e < 0 || str2 == NULL)
context->error_string = NULL;
else
context->error_string = str2;
free(str);
} else
context->error_string = str;
HEIMDAL_MUTEX_unlock(context->mutex);
}
KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
krb5_get_error_message(krb5_context context, krb5_error_code code)
{
char *str = NULL;
const char *cstr = NULL;
char buf[128];
int free_context = 0;
if (code == 0)
return strdup("Success");
if (context)
{
HEIMDAL_MUTEX_lock(context->mutex);
if (context->error_string &&
(code == context->error_code || context->error_code == 0))
{
str = strdup(context->error_string);
}
HEIMDAL_MUTEX_unlock(context->mutex);
if (str)
return str;
}
else
{
if (krb5_init_context(&context) == 0)
free_context = 1;
}
if (context)
cstr = com_right_r(context->et_list, code, buf, sizeof(buf));
if (free_context)
krb5_free_context(context);
if (cstr)
return strdup(cstr);
cstr = error_message(code);
if (cstr)
return strdup(cstr);
if (asprintf(&str, "<unknown error: %d>", (int)code) == -1 || str == NULL)
return NULL;
return str;
}
KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_free_error_message(krb5_context context, const char *msg)
{
free(rk_UNCONST(msg));
}
KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
krb5_get_err_text(krb5_context context, krb5_error_code code)
KRB5_DEPRECATED_FUNCTION("Use krb5_get_error_message instead")
{
const char *p = NULL;
if(context != NULL)
p = com_right(context->et_list, code);
if(p == NULL)
p = strerror(code);
if (p == NULL)
p = "Unknown error";
return p;
}
#ifdef __APPLE__
KRB5_LIB_FUNCTION void KRB5_LIB_CALL
_krb5_vset_cf_error_message (krb5_context context,
krb5_error_code ret,
CFErrorRef error,
const char *fmt,
va_list args)
HEIMDAL_PRINTF_ATTRIBUTE((printf, 4, 0))
{
char *str = NULL, *error_string = NULL;
CFStringRef description = NULL;
int r;
if (error) {
description = CFErrorCopyDescription(error);
if (description) {
str = rk_cfstring2cstring(description);
CFRelease(description);
}
}
r = vasprintf(&error_string, fmt, args);
if (r < 0 || error_string == NULL) {
free(str);
return;
}
if (str) {
krb5_set_error_message(context, ret, "%s: %s", error_string, str);
free(str);
} else {
krb5_set_error_message(context, ret, "%s", error_string);
}
free(error_string);
}
KRB5_LIB_FUNCTION void KRB5_LIB_CALL
_krb5_set_cf_error_message(krb5_context context,
krb5_error_code ret,
CFErrorRef error,
const char *fmt, ...)
HEIMDAL_PRINTF_ATTRIBUTE((printf, 4, 5))
{
va_list ap;
va_start(ap, fmt);
_krb5_vset_cf_error_message(context, ret, error, fmt, ap);
va_end(ap);
}
#endif