/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- krb5_clear_error_message
- krb5_set_error_message
- krb5_vset_error_message
- krb5_get_error_string
- krb5_have_error_string
- krb5_get_error_message
- krb5_free_error_message
- krb5_free_error_string
- krb5_set_error_string
- krb5_vset_error_string
- krb5_clear_error_string
1 /*
2 * Copyright (c) 2001, 2003, 2005 - 2006 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 RCSID("$Id$");
37
38 #undef __attribute__
39 #define __attribute__(X)
40
41 /**
42 * Clears the error message from the Kerberos 5 context.
43 *
44 * @param context The Kerberos 5 context to clear
45 *
46 * @ingroup krb5_error
47 */
48
49 void KRB5_LIB_FUNCTION
50 krb5_clear_error_message(krb5_context context)
/* [<][>][^][v][top][bottom][index][help] */
51 {
52 HEIMDAL_MUTEX_lock(context->mutex);
53 if (context->error_string)
54 free(context->error_string);
55 context->error_code = 0;
56 context->error_string = NULL;
57 HEIMDAL_MUTEX_unlock(context->mutex);
58 }
59
60 /**
61 * Set the context full error string for a specific error code.
62 * The error that is stored should be internationalized.
63 *
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.
68 *
69 * @ingroup krb5_error
70 */
71
72 void KRB5_LIB_FUNCTION
73 krb5_set_error_message(krb5_context context, krb5_error_code ret,
/* [<][>][^][v][top][bottom][index][help] */
74 const char *fmt, ...)
75 __attribute__ ((format (printf, 3, 4)))
76 {
77 va_list ap;
78
79 va_start(ap, fmt);
80 krb5_vset_error_message (context, ret, fmt, ap);
81 va_end(ap);
82 }
83
84 /**
85 * Set the context full error string for a specific error code.
86 *
87 * @param context Kerberos 5 context
88 * @param ret The error code
89 * @param fmt Error string for the error code
90 * @param args printf(3) style parameters.
91 *
92 * @ingroup krb5_error
93 */
94
95
96 void KRB5_LIB_FUNCTION
97 krb5_vset_error_message (krb5_context context, krb5_error_code ret,
/* [<][>][^][v][top][bottom][index][help] */
98 const char *fmt, va_list args)
99 __attribute__ ((format (printf, 3, 0)))
100 {
101
102 krb5_clear_error_message(context);
103 HEIMDAL_MUTEX_lock(context->mutex);
104 context->error_code = ret;
105 vasprintf(&context->error_string, fmt, args);
106 HEIMDAL_MUTEX_unlock(context->mutex);
107 }
108
109
110 /**
111 * Return the error message in context. On error or no error string,
112 * the function returns NULL.
113 *
114 * @param context Kerberos 5 context
115 *
116 * @return an error string, needs to be freed with
117 * krb5_free_error_message(). The functions return NULL on error.
118 *
119 * @ingroup krb5_error
120 */
121
122 char * KRB5_LIB_FUNCTION
123 krb5_get_error_string(krb5_context context)
/* [<][>][^][v][top][bottom][index][help] */
124 {
125 char *ret = NULL;
126
127 HEIMDAL_MUTEX_lock(context->mutex);
128 if (context->error_string)
129 ret = strdup(context->error_string);
130 HEIMDAL_MUTEX_unlock(context->mutex);
131 return ret;
132 }
133
134 krb5_boolean KRB5_LIB_FUNCTION
135 krb5_have_error_string(krb5_context context)
/* [<][>][^][v][top][bottom][index][help] */
136 {
137 char *str;
138 HEIMDAL_MUTEX_lock(context->mutex);
139 str = context->error_string;
140 HEIMDAL_MUTEX_unlock(context->mutex);
141 return str != NULL;
142 }
143
144 /**
145 * Return the error message for `code' in context. On memory
146 * allocation error the function returns NULL.
147 *
148 * @param context Kerberos 5 context
149 * @param code Error code related to the error
150 *
151 * @return an error string, needs to be freed with
152 * krb5_free_error_message(). The functions return NULL on error.
153 *
154 * @ingroup krb5_error
155 */
156
157 const char * KRB5_LIB_FUNCTION
158 krb5_get_error_message(krb5_context context, krb5_error_code code)
/* [<][>][^][v][top][bottom][index][help] */
159 {
160 const char *cstr;
161 char *str;
162
163 HEIMDAL_MUTEX_lock(context->mutex);
164 if (context->error_string &&
165 (code == context->error_code || context->error_code == 0))
166 {
167 str = strdup(context->error_string);
168 if (str) {
169 HEIMDAL_MUTEX_unlock(context->mutex);
170 return str;
171 }
172 }
173 HEIMDAL_MUTEX_unlock(context->mutex);
174
175 cstr = krb5_get_err_text(context, code);
176 if (cstr)
177 return strdup(cstr);
178
179 if (asprintf(&str, "<unknown error: %d>", (int)code) == -1)
180 return NULL;
181
182 return str;
183 }
184
185
186 /**
187 * Free the error message returned by krb5_get_error_message().
188 *
189 * @param context Kerberos context
190 * @param msg error message to free, returned byg
191 * krb5_get_error_message().
192 *
193 * @ingroup krb5_error
194 */
195
196 void KRB5_LIB_FUNCTION
197 krb5_free_error_message(krb5_context context, const char *msg)
/* [<][>][^][v][top][bottom][index][help] */
198 {
199 free(rk_UNCONST(msg));
200 }
201
202 #ifndef HEIMDAL_SMALLER
203
204 /**
205 * Free the error message returned by krb5_get_error_string(),
206 * deprecated, use krb5_free_error_message().
207 *
208 * @param context Kerberos context
209 * @param msg error message to free
210 *
211 * @ingroup krb5_deprecated
212 */
213
214 void KRB5_LIB_FUNCTION
215 krb5_free_error_string(krb5_context context, char *str)
/* [<][>][^][v][top][bottom][index][help] */
216 __attribute__((deprecated))
217 {
218 krb5_free_error_message(context, str);
219 }
220
221 /**
222 * Set the error message returned by krb5_get_error_string(),
223 * deprecated, use krb5_set_error_message().
224 *
225 * @param context Kerberos context
226 * @param msg error message to free
227 *
228 * @ingroup krb5_deprecated
229 */
230
231 krb5_error_code KRB5_LIB_FUNCTION
232 krb5_set_error_string(krb5_context context, const char *fmt, ...)
/* [<][>][^][v][top][bottom][index][help] */
233 __attribute__((format (printf, 2, 3))) __attribute__((deprecated))
234 {
235 va_list ap;
236
237 va_start(ap, fmt);
238 krb5_vset_error_message (context, 0, fmt, ap);
239 va_end(ap);
240 return 0;
241 }
242
243 /**
244 * Set the error message returned by krb5_get_error_string(),
245 * deprecated, use krb5_set_error_message().
246 *
247 * @param context Kerberos context
248 * @param msg error message to free
249 *
250 * @ingroup krb5_deprecated
251 */
252
253 krb5_error_code KRB5_LIB_FUNCTION
254 krb5_vset_error_string(krb5_context context, const char *fmt, va_list args)
/* [<][>][^][v][top][bottom][index][help] */
255 __attribute__ ((format (printf, 2, 0))) __attribute__((deprecated))
256 {
257 krb5_vset_error_message(context, 0, fmt, args);
258 return 0;
259 }
260
261 /**
262 * Clar the error message returned by krb5_get_error_string(),
263 * deprecated, use krb5_clear_error_message().
264 *
265 * @param context Kerberos context
266 *
267 * @ingroup krb5_deprecated
268 */
269
270 void KRB5_LIB_FUNCTION
271 krb5_clear_error_string(krb5_context context)
/* [<][>][^][v][top][bottom][index][help] */
272 __attribute__((deprecated))
273 {
274 krb5_clear_error_message(context);
275 }
276
277 #endif /* !HEIMDAL_SMALLER */