Move error functions from krb5 to base
[metze/heimdal/wip.git] / lib / krb5 / error_string.c
1 /*
2  * Copyright (c) 2001, 2003, 2005 - 2020 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
34 #include "krb5_locl.h"
35
36 #undef __attribute__
37 #define __attribute__(x)
38
39 /**
40  * Clears the error message from the Kerberos 5 context.
41  *
42  * @param context The Kerberos 5 context to clear
43  *
44  * @ingroup krb5_error
45  */
46
47 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
48 krb5_clear_error_message(krb5_context context)
49 {
50     heim_clear_error_message(context->hcontext);
51 }
52
53 /**
54  * Set the context full error string for a specific error code.
55  * The error that is stored should be internationalized.
56  *
57  * The if context is NULL, no error string is stored.
58  *
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.
63  *
64  * @ingroup krb5_error
65  */
66
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)))
71 {
72     va_list ap;
73
74     va_start(ap, fmt);
75     krb5_vset_error_message (context, ret, fmt, ap);
76     va_end(ap);
77 }
78
79 /**
80  * Set the context full error string for a specific error code.
81  *
82  * The if context is NULL, no error string is stored.
83  *
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.
88  *
89  * @ingroup krb5_error
90  */
91
92
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)))
97 {
98     if (context) {
99         const char *msg;
100
101         heim_vset_error_message(context->hcontext, ret, fmt, args);
102         msg = heim_get_error_message(context->hcontext, ret);
103         if (msg) {
104             _krb5_debug(context, 100, "error message: %s: %d", msg, ret);
105             heim_free_error_message(context->hcontext, msg);
106         }
107     }
108 }
109
110 /**
111  * Prepend the context full error string for a specific error code.
112  * The error that is stored should be internationalized.
113  *
114  * The if context is NULL, no error string is stored.
115  *
116  * @param context Kerberos 5 context
117  * @param ret The error code
118  * @param fmt Error string for the error code
119  * @param ... printf(3) style parameters.
120  *
121  * @ingroup krb5_error
122  */
123
124 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
125 krb5_prepend_error_message(krb5_context context, krb5_error_code ret,
126                            const char *fmt, ...)
127     __attribute__ ((__format__ (__printf__, 3, 4)))
128 {
129     va_list ap;
130
131     va_start(ap, fmt);
132     krb5_vprepend_error_message(context, ret, fmt, ap);
133     va_end(ap);
134 }
135
136 /**
137  * Prepend the contexts's full error string for a specific error code.
138  *
139  * The if context is NULL, no error string is stored.
140  *
141  * @param context Kerberos 5 context
142  * @param ret The error code
143  * @param fmt Error string for the error code
144  * @param args printf(3) style parameters.
145  *
146  * @ingroup krb5_error
147  */
148
149 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
150 krb5_vprepend_error_message(krb5_context context, krb5_error_code ret,
151                             const char *fmt, va_list args)
152     __attribute__ ((__format__ (__printf__, 3, 0)))
153 {
154     if (context)
155         heim_vprepend_error_message(context->hcontext, ret, fmt, args);
156 }
157
158 /**
159  * Return the error message for `code' in context. On memory
160  * allocation error the function returns NULL.
161  *
162  * @param context Kerberos 5 context
163  * @param code Error code related to the error
164  *
165  * @return an error string, needs to be freed with
166  * krb5_free_error_message(). The functions return NULL on error.
167  *
168  * @ingroup krb5_error
169  */
170
171 KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
172 krb5_get_error_message(krb5_context context, krb5_error_code code)
173 {
174     const char *cstr = NULL;
175
176     if (code == 0)
177         return strdup("Success");
178
179     /*
180      * The MIT version of this function ignores the krb5_context
181      * and several widely deployed applications call krb5_get_error_message()
182      * with a NULL context in order to translate an error code as a
183      * replacement for error_message().  Another reason a NULL context
184      * might be provided is if the krb5_init_context() call itself
185      * failed.
186      */
187     if (context == NULL && krb5_init_context(&context) == 0) {
188         cstr = heim_get_error_message(context->hcontext, code);
189         krb5_free_context(context);
190     } else if (context) {
191         cstr = heim_get_error_message(context->hcontext, code);
192     } else {
193         cstr = heim_get_error_message(NULL, code);
194     }
195     return cstr;
196 }
197
198
199 /**
200  * Free the error message returned by krb5_get_error_message().
201  *
202  * @param context Kerberos context
203  * @param msg error message to free, returned byg
204  *        krb5_get_error_message().
205  *
206  * @ingroup krb5_error
207  */
208
209 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
210 krb5_free_error_message(krb5_context context, const char *msg)
211 {
212     heim_free_error_message(context ? context->hcontext : NULL, msg);
213 }
214
215
216 /**
217  * Return the error string for the error code. The caller must not
218  * free the string.
219  *
220  * This function is deprecated since its not threadsafe.
221  *
222  * @param context Kerberos 5 context.
223  * @param code Kerberos error code.
224  *
225  * @return the error message matching code
226  *
227  * @ingroup krb5
228  */
229
230 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
231 krb5_get_err_text(krb5_context context, krb5_error_code code)
232     KRB5_DEPRECATED_FUNCTION("Use krb5_get_error_message instead")
233 {
234     return krb5_get_error_message(context, code);
235 }