s3: Add vfs_aio_posix
[Samba/gebeck_regimport.git] / source4 / heimdal / lib / krb5 / error_string.c
blobbebd4c490ee1855d52d718fcb87ac16fce54a14a
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 * The if context is NULL, no error string is stored.
64 * @param context Kerberos 5 context
65 * @param ret The error code
66 * @param fmt Error string for the error code
67 * @param ... printf(3) style parameters.
69 * @ingroup krb5_error
72 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
73 krb5_set_error_message(krb5_context context, krb5_error_code ret,
74 const char *fmt, ...)
75 __attribute__ ((format (printf, 3, 4)))
77 va_list ap;
79 va_start(ap, fmt);
80 krb5_vset_error_message (context, ret, fmt, ap);
81 va_end(ap);
84 /**
85 * Set the context full error string for a specific error code.
87 * The if context is NULL, no error string is stored.
89 * @param context Kerberos 5 context
90 * @param ret The error code
91 * @param fmt Error string for the error code
92 * @param args printf(3) style parameters.
94 * @ingroup krb5_error
98 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
99 krb5_vset_error_message (krb5_context context, krb5_error_code ret,
100 const char *fmt, va_list args)
101 __attribute__ ((format (printf, 3, 0)))
103 int r;
105 if (context == NULL)
106 return;
108 HEIMDAL_MUTEX_lock(context->mutex);
109 if (context->error_string) {
110 free(context->error_string);
111 context->error_string = NULL;
113 context->error_code = ret;
114 r = vasprintf(&context->error_string, fmt, args);
115 if (r < 0)
116 context->error_string = NULL;
117 HEIMDAL_MUTEX_unlock(context->mutex);
121 * Prepend the context full error string for a specific error code.
122 * The error that is stored should be internationalized.
124 * The if context is NULL, no error string is stored.
126 * @param context Kerberos 5 context
127 * @param ret The error code
128 * @param fmt Error string for the error code
129 * @param ... printf(3) style parameters.
131 * @ingroup krb5_error
134 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
135 krb5_prepend_error_message(krb5_context context, krb5_error_code ret,
136 const char *fmt, ...)
137 __attribute__ ((format (printf, 3, 4)))
139 va_list ap;
141 va_start(ap, fmt);
142 krb5_vprepend_error_message(context, ret, fmt, ap);
143 va_end(ap);
147 * Prepend the contexts's full error string for a specific error code.
149 * The if context is NULL, no error string is stored.
151 * @param context Kerberos 5 context
152 * @param ret The error code
153 * @param fmt Error string for the error code
154 * @param args printf(3) style parameters.
156 * @ingroup krb5_error
159 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
160 krb5_vprepend_error_message(krb5_context context, krb5_error_code ret,
161 const char *fmt, va_list args)
162 __attribute__ ((format (printf, 3, 0)))
164 char *str = NULL, *str2 = NULL;
166 if (context == NULL)
167 return;
169 HEIMDAL_MUTEX_lock(context->mutex);
170 if (context->error_code != ret) {
171 HEIMDAL_MUTEX_unlock(context->mutex);
172 return;
174 if (vasprintf(&str, fmt, args) < 0 || str == NULL) {
175 HEIMDAL_MUTEX_unlock(context->mutex);
176 return;
178 if (context->error_string) {
179 int e;
181 e = asprintf(&str2, "%s: %s", str, context->error_string);
182 free(context->error_string);
183 if (e < 0 || str2 == NULL)
184 context->error_string = NULL;
185 else
186 context->error_string = str2;
187 free(str);
188 } else
189 context->error_string = str;
190 HEIMDAL_MUTEX_unlock(context->mutex);
195 * Return the error message in context. On error or no error string,
196 * the function returns NULL.
198 * @param context Kerberos 5 context
200 * @return an error string, needs to be freed with
201 * krb5_free_error_message(). The functions return NULL on error.
203 * @ingroup krb5_error
206 KRB5_LIB_FUNCTION char * KRB5_LIB_CALL
207 krb5_get_error_string(krb5_context context)
209 char *ret = NULL;
211 HEIMDAL_MUTEX_lock(context->mutex);
212 if (context->error_string)
213 ret = strdup(context->error_string);
214 HEIMDAL_MUTEX_unlock(context->mutex);
215 return ret;
218 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
219 krb5_have_error_string(krb5_context context)
221 char *str;
222 HEIMDAL_MUTEX_lock(context->mutex);
223 str = context->error_string;
224 HEIMDAL_MUTEX_unlock(context->mutex);
225 return str != NULL;
229 * Return the error message for `code' in context. On memory
230 * allocation error the function returns NULL.
232 * @param context Kerberos 5 context
233 * @param code Error code related to the error
235 * @return an error string, needs to be freed with
236 * krb5_free_error_message(). The functions return NULL on error.
238 * @ingroup krb5_error
241 KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
242 krb5_get_error_message(krb5_context context, krb5_error_code code)
244 char *str;
246 HEIMDAL_MUTEX_lock(context->mutex);
247 if (context->error_string &&
248 (code == context->error_code || context->error_code == 0))
250 str = strdup(context->error_string);
251 if (str) {
252 HEIMDAL_MUTEX_unlock(context->mutex);
253 return str;
256 HEIMDAL_MUTEX_unlock(context->mutex);
258 if (code == 0)
259 return strdup("Success");
261 const char *msg;
262 char buf[128];
263 msg = com_right_r(context->et_list, code, buf, sizeof(buf));
264 if (msg)
265 return strdup(msg);
268 if (asprintf(&str, "<unknown error: %d>", (int)code) == -1 || str == NULL)
269 return NULL;
271 return str;
276 * Free the error message returned by krb5_get_error_message().
278 * @param context Kerberos context
279 * @param msg error message to free, returned byg
280 * krb5_get_error_message().
282 * @ingroup krb5_error
285 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
286 krb5_free_error_message(krb5_context context, const char *msg)
288 free(rk_UNCONST(msg));
293 * Return the error string for the error code. The caller must not
294 * free the string.
296 * This function is deprecated since its not threadsafe.
298 * @param context Kerberos 5 context.
299 * @param code Kerberos error code.
301 * @return the error message matching code
303 * @ingroup krb5
306 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
307 krb5_get_err_text(krb5_context context, krb5_error_code code)
308 KRB5_DEPRECATED_FUNCTION("Use X instead")
310 const char *p = NULL;
311 if(context != NULL)
312 p = com_right(context->et_list, code);
313 if(p == NULL)
314 p = strerror(code);
315 if (p == NULL)
316 p = "Unknown error";
317 return p;