s4:heimdal: import lorikeet-heimdal-201001120029 (commit a5e675fed7c5db8a7370b77ed0bf...
[Samba/ekacnet.git] / source4 / heimdal / lib / krb5 / error_string.c
blobadab6f5e84c64c132ad67aa2de957d9570e01bc7
1 /*
2 * Copyright (c) 2001, 2003, 2005 - 2006 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 HEIMDAL_MUTEX_lock(context->mutex);
51 if (context->error_string)
52 free(context->error_string);
53 context->error_code = 0;
54 context->error_string = NULL;
55 HEIMDAL_MUTEX_unlock(context->mutex);
58 /**
59 * Set the context full error string for a specific error code.
60 * The error that is stored should be internationalized.
62 * @param context Kerberos 5 context
63 * @param ret The error code
64 * @param fmt Error string for the error code
65 * @param ... printf(3) style parameters.
67 * @ingroup krb5_error
70 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
71 krb5_set_error_message(krb5_context context, krb5_error_code ret,
72 const char *fmt, ...)
73 __attribute__ ((format (printf, 3, 4)))
75 va_list ap;
77 va_start(ap, fmt);
78 krb5_vset_error_message (context, ret, fmt, ap);
79 va_end(ap);
82 /**
83 * Set the context full error string for a specific error code.
85 * @param context Kerberos 5 context
86 * @param ret The error code
87 * @param fmt Error string for the error code
88 * @param args printf(3) style parameters.
90 * @ingroup krb5_error
94 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
95 krb5_vset_error_message (krb5_context context, krb5_error_code ret,
96 const char *fmt, va_list args)
97 __attribute__ ((format (printf, 3, 0)))
100 krb5_clear_error_message(context);
101 HEIMDAL_MUTEX_lock(context->mutex);
102 context->error_code = ret;
103 vasprintf(&context->error_string, fmt, args);
104 HEIMDAL_MUTEX_unlock(context->mutex);
108 * Prepend the context full error string for a specific error code.
109 * The error that is stored should be internationalized.
111 * @param context Kerberos 5 context
112 * @param ret The error code
113 * @param fmt Error string for the error code
114 * @param ... printf(3) style parameters.
116 * @ingroup krb5_error
119 void KRB5_LIB_FUNCTION
120 krb5_prepend_error_message(krb5_context context, krb5_error_code ret,
121 const char *fmt, ...)
122 __attribute__ ((format (printf, 3, 4)))
124 va_list ap;
126 va_start(ap, fmt);
127 krb5_vprepend_error_message(context, ret, fmt, ap);
128 va_end(ap);
132 * Prepend the contexts's full error string for a specific error code.
134 * @param context Kerberos 5 context
135 * @param ret The error code
136 * @param fmt Error string for the error code
137 * @param args printf(3) style parameters.
139 * @ingroup krb5_error
142 void KRB5_LIB_FUNCTION
143 krb5_vprepend_error_message(krb5_context context, krb5_error_code ret,
144 const char *fmt, va_list args)
145 __attribute__ ((format (printf, 3, 0)))
147 char *str, *str2;
148 HEIMDAL_MUTEX_lock(context->mutex);
149 if (context->error_code != ret) {
150 HEIMDAL_MUTEX_unlock(context->mutex);
151 return;
153 vasprintf(&str, fmt, args);
154 if (context->error_string) {
155 int e;
157 e = asprintf(&str2, "%s: %s", str, context->error_string);
158 free(context->error_string);
159 if (e < 0)
160 context->error_string = NULL;
161 else
162 context->error_string = str2;
163 free(str);
164 } else
165 context->error_string = str;
166 HEIMDAL_MUTEX_unlock(context->mutex);
171 * Return the error message in context. On error or no error string,
172 * the function returns NULL.
174 * @param context Kerberos 5 context
176 * @return an error string, needs to be freed with
177 * krb5_free_error_message(). The functions return NULL on error.
179 * @ingroup krb5_error
182 KRB5_LIB_FUNCTION char * KRB5_LIB_CALL
183 krb5_get_error_string(krb5_context context)
185 char *ret = NULL;
187 HEIMDAL_MUTEX_lock(context->mutex);
188 if (context->error_string)
189 ret = strdup(context->error_string);
190 HEIMDAL_MUTEX_unlock(context->mutex);
191 return ret;
194 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
195 krb5_have_error_string(krb5_context context)
197 char *str;
198 HEIMDAL_MUTEX_lock(context->mutex);
199 str = context->error_string;
200 HEIMDAL_MUTEX_unlock(context->mutex);
201 return str != NULL;
205 * Return the error message for `code' in context. On memory
206 * allocation error the function returns NULL.
208 * @param context Kerberos 5 context
209 * @param code Error code related to the error
211 * @return an error string, needs to be freed with
212 * krb5_free_error_message(). The functions return NULL on error.
214 * @ingroup krb5_error
217 KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
218 krb5_get_error_message(krb5_context context, krb5_error_code code)
220 char *str;
222 HEIMDAL_MUTEX_lock(context->mutex);
223 if (context->error_string &&
224 (code == context->error_code || context->error_code == 0))
226 str = strdup(context->error_string);
227 if (str) {
228 HEIMDAL_MUTEX_unlock(context->mutex);
229 return str;
232 HEIMDAL_MUTEX_unlock(context->mutex);
234 if (code == 0)
235 return strdup("Success");
237 const char *msg;
238 char buf[128];
239 msg = com_right_r(context->et_list, code, buf, sizeof(buf));
240 if (msg)
241 return strdup(msg);
244 if (asprintf(&str, "<unknown error: %d>", (int)code) == -1)
245 return NULL;
247 return str;
252 * Free the error message returned by krb5_get_error_message().
254 * @param context Kerberos context
255 * @param msg error message to free, returned byg
256 * krb5_get_error_message().
258 * @ingroup krb5_error
261 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
262 krb5_free_error_message(krb5_context context, const char *msg)
264 free(rk_UNCONST(msg));
269 * Return the error string for the error code. The caller must not
270 * free the string.
272 * This function is deprecated since its not threadsafe.
274 * @param context Kerberos 5 context.
275 * @param code Kerberos error code.
277 * @return the error message matching code
279 * @ingroup krb5
282 KRB5_DEPRECATED
283 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
284 krb5_get_err_text(krb5_context context, krb5_error_code code)
286 const char *p = NULL;
287 if(context != NULL)
288 p = com_right(context->et_list, code);
289 if(p == NULL)
290 p = strerror(code);
291 if (p == NULL)
292 p = "Unknown error";
293 return p;