2 * Copyright (c) 2001, 2003, 2005 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
37 #define __attribute__(x)
40 * Clears the error message from the Kerberos 5 context.
42 * @param context The Kerberos 5 context to clear
47 void KRB5_LIB_FUNCTION
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
);
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.
70 void KRB5_LIB_FUNCTION
71 krb5_set_error_message(krb5_context context
, krb5_error_code ret
,
73 __attribute__ ((format (printf
, 3, 4)))
78 krb5_vset_error_message (context
, ret
, fmt
, ap
);
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.
94 void KRB5_LIB_FUNCTION
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)))
127 krb5_vset_error_message (context
, ret
, fmt
, 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)))
148 HEIMDAL_MUTEX_lock(context
->mutex
);
149 if (context
->error_code
!= ret
) {
150 HEIMDAL_MUTEX_unlock(context
->mutex
);
153 vasprintf(&str
, fmt
, args
);
154 if (context
->error_string
) {
157 e
= asprintf(&str2
, "%s: %s", str
, context
->error_string
);
158 free(context
->error_string
);
160 context
->error_string
= NULL
;
162 context
->error_string
= str2
;
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 char * KRB5_LIB_FUNCTION
183 krb5_get_error_string(krb5_context context
)
187 HEIMDAL_MUTEX_lock(context
->mutex
);
188 if (context
->error_string
)
189 ret
= strdup(context
->error_string
);
190 HEIMDAL_MUTEX_unlock(context
->mutex
);
194 krb5_boolean KRB5_LIB_FUNCTION
195 krb5_have_error_string(krb5_context context
)
198 HEIMDAL_MUTEX_lock(context
->mutex
);
199 str
= context
->error_string
;
200 HEIMDAL_MUTEX_unlock(context
->mutex
);
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 const char * KRB5_LIB_FUNCTION
218 krb5_get_error_message(krb5_context context
, krb5_error_code code
)
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
);
228 HEIMDAL_MUTEX_unlock(context
->mutex
);
232 HEIMDAL_MUTEX_unlock(context
->mutex
);
235 return strdup("Success");
238 str
= com_right_r(context
->et_list
, code
, buf
, sizeof(buf
));
243 if (asprintf(&str
, "<unknown error: %d>", (int)code
) == -1)
251 * Free the error message returned by krb5_get_error_message().
253 * @param context Kerberos context
254 * @param msg error message to free, returned byg
255 * krb5_get_error_message().
257 * @ingroup krb5_error
260 void KRB5_LIB_FUNCTION
261 krb5_free_error_message(krb5_context context
, const char *msg
)
263 free(rk_UNCONST(msg
));