lib/gssapi/krb5: implement GSS_C_CHANNEL_BOUND_FLAG for gss_init_sec_context()
[heimdal.git] / lib / krb5 / error_string.c
blobda86b375f83c6dca20d6f7a46586f4629b307892
1 /*
2 * Copyright (c) 2001, 2003, 2005 - 2020 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "krb5_locl.h"
36 #undef __attribute__
37 #define __attribute__(x)
39 /**
40 * Clears the error message from the Kerberos 5 context.
42 * @param context The Kerberos 5 context to clear
44 * @ingroup krb5_error
47 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
48 krb5_clear_error_message(krb5_context context)
50 heim_clear_error_message(context->hcontext);
53 /**
54 * Set the context full error string for a specific error code.
55 * The error that is stored should be internationalized.
57 * The if context is NULL, no error string is stored.
59 * @param context Kerberos 5 context
60 * @param ret The error code
61 * @param fmt Error string for the error code
62 * @param ... printf(3) style parameters.
64 * @ingroup krb5_error
67 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
68 krb5_set_error_message(krb5_context context, krb5_error_code ret,
69 const char *fmt, ...)
70 __attribute__ ((__format__ (__printf__, 3, 4)))
72 va_list ap;
74 va_start(ap, fmt);
75 krb5_vset_error_message (context, ret, fmt, ap);
76 va_end(ap);
79 /**
80 * Set the context full error string for a specific error code.
82 * The if context is NULL, no error string is stored.
84 * @param context Kerberos 5 context
85 * @param ret The error code
86 * @param fmt Error string for the error code
87 * @param args printf(3) style parameters.
89 * @ingroup krb5_error
93 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
94 krb5_vset_error_message(krb5_context context, krb5_error_code ret,
95 const char *fmt, va_list args)
96 __attribute__ ((__format__ (__printf__, 3, 0)))
98 const char *msg;
100 if (context == NULL)
101 return;
103 heim_vset_error_message(context->hcontext, ret, fmt, args);
104 msg = heim_get_error_message(context->hcontext, ret);
105 if (msg) {
106 _krb5_debug(context, 100, "error message: %s: %d", msg, ret);
107 heim_free_error_message(context->hcontext, msg);
112 * Prepend the context full error string for a specific error code.
113 * The error that is stored should be internationalized.
115 * The if context is NULL, no error string is stored.
117 * @param context Kerberos 5 context
118 * @param ret The error code
119 * @param fmt Error string for the error code
120 * @param ... printf(3) style parameters.
122 * @ingroup krb5_error
125 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
126 krb5_prepend_error_message(krb5_context context, krb5_error_code ret,
127 const char *fmt, ...)
128 __attribute__ ((__format__ (__printf__, 3, 4)))
130 va_list ap;
132 va_start(ap, fmt);
133 krb5_vprepend_error_message(context, ret, fmt, ap);
134 va_end(ap);
138 * Prepend the contexts's full error string for a specific error code.
140 * The if context is NULL, no error string is stored.
142 * @param context Kerberos 5 context
143 * @param ret The error code
144 * @param fmt Error string for the error code
145 * @param args printf(3) style parameters.
147 * @ingroup krb5_error
150 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
151 krb5_vprepend_error_message(krb5_context context, krb5_error_code ret,
152 const char *fmt, va_list args)
153 __attribute__ ((__format__ (__printf__, 3, 0)))
155 if (context)
156 heim_vprepend_error_message(context->hcontext, ret, fmt, args);
160 * Return the error message for `code' in context. On memory
161 * allocation error the function returns NULL.
163 * @param context Kerberos 5 context
164 * @param code Error code related to the error
166 * @return an error string, needs to be freed with
167 * krb5_free_error_message(). The functions return NULL on error.
169 * @ingroup krb5_error
172 KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
173 krb5_get_error_message(krb5_context context, krb5_error_code code)
175 const char *cstr = NULL;
177 if (code == 0)
178 return strdup("Success");
181 * The MIT version of this function ignores the krb5_context
182 * and several widely deployed applications call krb5_get_error_message()
183 * with a NULL context in order to translate an error code as a
184 * replacement for error_message(). Another reason a NULL context
185 * might be provided is if the krb5_init_context() call itself
186 * failed.
188 if (context == NULL && krb5_init_context(&context) == 0) {
189 cstr = heim_get_error_message(context->hcontext, code);
190 krb5_free_context(context);
191 } else if (context) {
192 cstr = heim_get_error_message(context->hcontext, code);
193 } else {
194 cstr = heim_get_error_message(NULL, code);
196 return cstr;
201 * Free the error message returned by krb5_get_error_message().
203 * @param context Kerberos context
204 * @param msg error message to free, returned byg
205 * krb5_get_error_message().
207 * @ingroup krb5_error
210 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
211 krb5_free_error_message(krb5_context context, const char *msg)
213 heim_free_error_message(context ? context->hcontext : NULL, msg);
218 * Return the error string for the error code. The caller must not
219 * free the string.
221 * This function is deprecated since its not threadsafe.
223 * @param context Kerberos 5 context.
224 * @param code Kerberos error code.
226 * @return the error message matching code
228 * @ingroup krb5
231 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
232 krb5_get_err_text(krb5_context context, krb5_error_code code)
233 KRB5_DEPRECATED_FUNCTION("Use krb5_get_error_message instead")
235 return krb5_get_error_message(context, code);