1adb0ddaeSAssar Westerlund /*
2c19800e8SDoug Rabson * Copyright (c) 2001, 2003, 2005 - 2006 Kungliga Tekniska Högskolan
3adb0ddaeSAssar Westerlund * (Royal Institute of Technology, Stockholm, Sweden).
4adb0ddaeSAssar Westerlund * All rights reserved.
5adb0ddaeSAssar Westerlund *
6adb0ddaeSAssar Westerlund * Redistribution and use in source and binary forms, with or without
7adb0ddaeSAssar Westerlund * modification, are permitted provided that the following conditions
8adb0ddaeSAssar Westerlund * are met:
9adb0ddaeSAssar Westerlund *
10adb0ddaeSAssar Westerlund * 1. Redistributions of source code must retain the above copyright
11adb0ddaeSAssar Westerlund * notice, this list of conditions and the following disclaimer.
12adb0ddaeSAssar Westerlund *
13adb0ddaeSAssar Westerlund * 2. Redistributions in binary form must reproduce the above copyright
14adb0ddaeSAssar Westerlund * notice, this list of conditions and the following disclaimer in the
15adb0ddaeSAssar Westerlund * documentation and/or other materials provided with the distribution.
16adb0ddaeSAssar Westerlund *
17adb0ddaeSAssar Westerlund * 3. Neither the name of the Institute nor the names of its contributors
18adb0ddaeSAssar Westerlund * may be used to endorse or promote products derived from this software
19adb0ddaeSAssar Westerlund * without specific prior written permission.
20adb0ddaeSAssar Westerlund *
21adb0ddaeSAssar Westerlund * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22adb0ddaeSAssar Westerlund * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23adb0ddaeSAssar Westerlund * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24adb0ddaeSAssar Westerlund * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25adb0ddaeSAssar Westerlund * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26adb0ddaeSAssar Westerlund * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27adb0ddaeSAssar Westerlund * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28adb0ddaeSAssar Westerlund * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29adb0ddaeSAssar Westerlund * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30adb0ddaeSAssar Westerlund * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31adb0ddaeSAssar Westerlund * SUCH DAMAGE.
32adb0ddaeSAssar Westerlund */
33adb0ddaeSAssar Westerlund
34adb0ddaeSAssar Westerlund #include "krb5_locl.h"
35adb0ddaeSAssar Westerlund
36c19800e8SDoug Rabson #undef __attribute__
37adb0ddaeSAssar Westerlund #define __attribute__(x)
38adb0ddaeSAssar Westerlund
39adb0ddaeSAssar Westerlund /**
40adb0ddaeSAssar Westerlund * Clears the error message from the Kerberos 5 context.
41c19800e8SDoug Rabson *
42adb0ddaeSAssar Westerlund * @param context The Kerberos 5 context to clear
43adb0ddaeSAssar Westerlund *
44c19800e8SDoug Rabson * @ingroup krb5_error
45adb0ddaeSAssar Westerlund */
46adb0ddaeSAssar Westerlund
47c19800e8SDoug Rabson KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_clear_error_message(krb5_context context)48adb0ddaeSAssar Westerlund krb5_clear_error_message(krb5_context context)
49adb0ddaeSAssar Westerlund {
50c19800e8SDoug Rabson HEIMDAL_MUTEX_lock(context->mutex);
51adb0ddaeSAssar Westerlund if (context->error_string)
52adb0ddaeSAssar Westerlund free(context->error_string);
53c19800e8SDoug Rabson context->error_code = 0;
54adb0ddaeSAssar Westerlund context->error_string = NULL;
55adb0ddaeSAssar Westerlund HEIMDAL_MUTEX_unlock(context->mutex);
56adb0ddaeSAssar Westerlund }
57adb0ddaeSAssar Westerlund
58c19800e8SDoug Rabson /**
59adb0ddaeSAssar Westerlund * Set the context full error string for a specific error code.
60adb0ddaeSAssar Westerlund * The error that is stored should be internationalized.
61c19800e8SDoug Rabson *
62adb0ddaeSAssar Westerlund * The if context is NULL, no error string is stored.
63adb0ddaeSAssar Westerlund *
64adb0ddaeSAssar Westerlund * @param context Kerberos 5 context
65adb0ddaeSAssar Westerlund * @param ret The error code
66adb0ddaeSAssar Westerlund * @param fmt Error string for the error code
67adb0ddaeSAssar Westerlund * @param ... printf(3) style parameters.
68adb0ddaeSAssar Westerlund *
69adb0ddaeSAssar Westerlund * @ingroup krb5_error
70adb0ddaeSAssar Westerlund */
71adb0ddaeSAssar Westerlund
72adb0ddaeSAssar Westerlund KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_set_error_message(krb5_context context,krb5_error_code ret,const char * fmt,...)73adb0ddaeSAssar Westerlund krb5_set_error_message(krb5_context context, krb5_error_code ret,
74c19800e8SDoug Rabson const char *fmt, ...)
75adb0ddaeSAssar Westerlund __attribute__ ((format (printf, 3, 4)))
76adb0ddaeSAssar Westerlund {
77adb0ddaeSAssar Westerlund va_list ap;
78adb0ddaeSAssar Westerlund
79c19800e8SDoug Rabson va_start(ap, fmt);
80adb0ddaeSAssar Westerlund krb5_vset_error_message (context, ret, fmt, ap);
81adb0ddaeSAssar Westerlund va_end(ap);
82adb0ddaeSAssar Westerlund }
83adb0ddaeSAssar Westerlund
84adb0ddaeSAssar Westerlund /**
85c19800e8SDoug Rabson * Set the context full error string for a specific error code.
86adb0ddaeSAssar Westerlund *
87adb0ddaeSAssar Westerlund * The if context is NULL, no error string is stored.
88adb0ddaeSAssar Westerlund *
89c19800e8SDoug Rabson * @param context Kerberos 5 context
90c19800e8SDoug Rabson * @param ret The error code
91c19800e8SDoug Rabson * @param fmt Error string for the error code
92c19800e8SDoug Rabson * @param args printf(3) style parameters.
93c19800e8SDoug Rabson *
94c19800e8SDoug Rabson * @ingroup krb5_error
95c19800e8SDoug Rabson */
96c19800e8SDoug Rabson
97c19800e8SDoug Rabson
98c19800e8SDoug Rabson KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_vset_error_message(krb5_context context,krb5_error_code ret,const char * fmt,va_list args)99c19800e8SDoug Rabson krb5_vset_error_message (krb5_context context, krb5_error_code ret,
100c19800e8SDoug Rabson const char *fmt, va_list args)
101c19800e8SDoug Rabson __attribute__ ((format (printf, 3, 0)))
102adb0ddaeSAssar Westerlund {
103adb0ddaeSAssar Westerlund int r;
104c19800e8SDoug Rabson
105c19800e8SDoug Rabson if (context == NULL)
106c19800e8SDoug Rabson return;
107c19800e8SDoug Rabson
108c19800e8SDoug Rabson HEIMDAL_MUTEX_lock(context->mutex);
109c19800e8SDoug Rabson if (context->error_string) {
110adb0ddaeSAssar Westerlund free(context->error_string);
111adb0ddaeSAssar Westerlund context->error_string = NULL;
112adb0ddaeSAssar Westerlund }
113c19800e8SDoug Rabson context->error_code = ret;
114adb0ddaeSAssar Westerlund r = vasprintf(&context->error_string, fmt, args);
115adb0ddaeSAssar Westerlund if (r < 0)
116c19800e8SDoug Rabson context->error_string = NULL;
117c19800e8SDoug Rabson HEIMDAL_MUTEX_unlock(context->mutex);
118c19800e8SDoug Rabson }
119c19800e8SDoug Rabson
120c19800e8SDoug Rabson /**
121adb0ddaeSAssar Westerlund * Prepend the context full error string for a specific error code.
122c19800e8SDoug Rabson * The error that is stored should be internationalized.
123c19800e8SDoug Rabson *
124c19800e8SDoug Rabson * The if context is NULL, no error string is stored.
125c19800e8SDoug Rabson *
126c19800e8SDoug Rabson * @param context Kerberos 5 context
127c19800e8SDoug Rabson * @param ret The error code
128c19800e8SDoug Rabson * @param fmt Error string for the error code
129c19800e8SDoug Rabson * @param ... printf(3) style parameters.
130c19800e8SDoug Rabson *
131c19800e8SDoug Rabson * @ingroup krb5_error
132c19800e8SDoug Rabson */
133c19800e8SDoug Rabson
134c19800e8SDoug Rabson KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_prepend_error_message(krb5_context context,krb5_error_code ret,const char * fmt,...)135c19800e8SDoug Rabson krb5_prepend_error_message(krb5_context context, krb5_error_code ret,
136c19800e8SDoug Rabson const char *fmt, ...)
137c19800e8SDoug Rabson __attribute__ ((format (printf, 3, 4)))
138c19800e8SDoug Rabson {
139c19800e8SDoug Rabson va_list ap;
140c19800e8SDoug Rabson
141c19800e8SDoug Rabson va_start(ap, fmt);
142c19800e8SDoug Rabson krb5_vprepend_error_message(context, ret, fmt, ap);
143c19800e8SDoug Rabson va_end(ap);
144c19800e8SDoug Rabson }
145c19800e8SDoug Rabson
146c19800e8SDoug Rabson /**
147c19800e8SDoug Rabson * Prepend the contexts's full error string for a specific error code.
148c19800e8SDoug Rabson *
149c19800e8SDoug Rabson * The if context is NULL, no error string is stored.
150c19800e8SDoug Rabson *
151c19800e8SDoug Rabson * @param context Kerberos 5 context
152c19800e8SDoug Rabson * @param ret The error code
153c19800e8SDoug Rabson * @param fmt Error string for the error code
154c19800e8SDoug Rabson * @param args printf(3) style parameters.
155c19800e8SDoug Rabson *
156 * @ingroup krb5_error
157 */
158
159 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_vprepend_error_message(krb5_context context,krb5_error_code ret,const char * fmt,va_list args)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)))
163 {
164 char *str = NULL, *str2 = NULL;
165
166 if (context == NULL)
167 return;
168
169 HEIMDAL_MUTEX_lock(context->mutex);
170 if (context->error_code != ret) {
171 HEIMDAL_MUTEX_unlock(context->mutex);
172 return;
173 }
174 if (vasprintf(&str, fmt, args) < 0 || str == NULL) {
175 HEIMDAL_MUTEX_unlock(context->mutex);
176 return;
177 }
178 if (context->error_string) {
179 int e;
180
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);
191 }
192
193
194 /**
195 * Return the error message in context. On error or no error string,
196 * the function returns NULL.
197 *
198 * @param context Kerberos 5 context
199 *
200 * @return an error string, needs to be freed with
201 * krb5_free_error_message(). The functions return NULL on error.
202 *
203 * @ingroup krb5_error
204 */
205
206 KRB5_LIB_FUNCTION char * KRB5_LIB_CALL
krb5_get_error_string(krb5_context context)207 krb5_get_error_string(krb5_context context)
208 {
209 char *ret = NULL;
210
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;
216 }
217
218 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
krb5_have_error_string(krb5_context context)219 krb5_have_error_string(krb5_context context)
220 {
221 char *str;
222 HEIMDAL_MUTEX_lock(context->mutex);
223 str = context->error_string;
224 HEIMDAL_MUTEX_unlock(context->mutex);
225 return str != NULL;
226 }
227
228 /**
229 * Return the error message for `code' in context. On memory
230 * allocation error the function returns NULL.
231 *
232 * @param context Kerberos 5 context
233 * @param code Error code related to the error
234 *
235 * @return an error string, needs to be freed with
236 * krb5_free_error_message(). The functions return NULL on error.
237 *
238 * @ingroup krb5_error
239 */
240
241 KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
krb5_get_error_message(krb5_context context,krb5_error_code code)242 krb5_get_error_message(krb5_context context, krb5_error_code code)
243 {
244 char *str = NULL;
245 const char *cstr = NULL;
246 char buf[128];
247 int free_context = 0;
248
249 if (code == 0)
250 return strdup("Success");
251
252 /*
253 * The MIT version of this function ignores the krb5_context
254 * and several widely deployed applications call krb5_get_error_message()
255 * with a NULL context in order to translate an error code as a
256 * replacement for error_message(). Another reason a NULL context
257 * might be provided is if the krb5_init_context() call itself
258 * failed.
259 */
260 if (context)
261 {
262 HEIMDAL_MUTEX_lock(context->mutex);
263 if (context->error_string &&
264 (code == context->error_code || context->error_code == 0))
265 {
266 str = strdup(context->error_string);
267 }
268 HEIMDAL_MUTEX_unlock(context->mutex);
269
270 if (str)
271 return str;
272 }
273 else
274 {
275 if (krb5_init_context(&context) == 0)
276 free_context = 1;
277 }
278
279 if (context)
280 cstr = com_right_r(context->et_list, code, buf, sizeof(buf));
281
282 if (free_context)
283 krb5_free_context(context);
284
285 if (cstr)
286 return strdup(cstr);
287
288 cstr = error_message(code);
289 if (cstr)
290 return strdup(cstr);
291
292 if (asprintf(&str, "<unknown error: %d>", (int)code) == -1 || str == NULL)
293 return NULL;
294
295 return str;
296 }
297
298
299 /**
300 * Free the error message returned by krb5_get_error_message().
301 *
302 * @param context Kerberos context
303 * @param msg error message to free, returned byg
304 * krb5_get_error_message().
305 *
306 * @ingroup krb5_error
307 */
308
309 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_free_error_message(krb5_context context,const char * msg)310 krb5_free_error_message(krb5_context context, const char *msg)
311 {
312 free(rk_UNCONST(msg));
313 }
314
315
316 /**
317 * Return the error string for the error code. The caller must not
318 * free the string.
319 *
320 * This function is deprecated since its not threadsafe.
321 *
322 * @param context Kerberos 5 context.
323 * @param code Kerberos error code.
324 *
325 * @return the error message matching code
326 *
327 * @ingroup krb5
328 */
329
330 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
krb5_get_err_text(krb5_context context,krb5_error_code code)331 krb5_get_err_text(krb5_context context, krb5_error_code code)
332 KRB5_DEPRECATED_FUNCTION("Use X instead")
333 {
334 const char *p = NULL;
335 if(context != NULL)
336 p = com_right(context->et_list, code);
337 if(p == NULL)
338 p = strerror(code);
339 if (p == NULL)
340 p = "Unknown error";
341 return p;
342 }
343