resurrect password change support again
[heimdal.git] / lib / krb5 / warn.c
blob34a2f726c16dd929fb995d2d9621f29f765658ad
1 /*
2 * Copyright (c) 1997 - 2001 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"
35 #include <err.h>
37 static krb5_error_code _warnerr(krb5_context context, int do_errtext,
38 krb5_error_code code, int level, const char *fmt, va_list ap)
39 __attribute__((__format__(__printf__, 5, 0)));
41 static krb5_error_code
42 _warnerr(krb5_context context, int do_errtext,
43 krb5_error_code code, int level, const char *fmt, va_list ap)
45 char xfmt[7] = "";
46 const char *args[2], **arg;
47 char *msg = NULL;
48 const char *err_str = NULL;
49 krb5_error_code ret;
51 args[0] = args[1] = NULL;
52 arg = args;
53 if(fmt){
54 strlcat(xfmt, "%s", sizeof(xfmt));
55 if(do_errtext)
56 strlcat(xfmt, ": ", sizeof(xfmt));
57 ret = vasprintf(&msg, fmt, ap);
58 if(ret < 0 || msg == NULL)
59 return ENOMEM;
60 *arg++ = msg;
62 if(context && do_errtext){
63 strlcat(xfmt, "%s", sizeof(xfmt));
65 err_str = krb5_get_error_message(context, code);
66 if (err_str != NULL) {
67 *arg = err_str;
68 } else {
69 *arg= "<unknown error>";
73 if(context && context->warn_dest)
74 krb5_log(context, context->warn_dest, level, xfmt, args[0], args[1]);
75 else
76 warnx(xfmt, args[0], args[1]);
77 free(msg);
78 krb5_free_error_message(context, err_str);
79 return 0;
82 #define FUNC(ETEXT, CODE, LEVEL) \
83 krb5_error_code ret; \
84 va_list ap; \
85 va_start(ap, fmt); \
86 ret = _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap); \
87 va_end(ap);
89 #define FUNC_NORET(ETEXT, CODE, LEVEL) \
90 va_list ap; \
91 va_start(ap, fmt); \
92 (void) _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap); \
93 va_end(ap);
95 #undef __attribute__
96 #define __attribute__(X)
98 /**
99 * Log a warning to the log, default stderr, include the error from
100 * the last failure.
102 * @param context A Kerberos 5 context.
103 * @param code error code of the last error
104 * @param fmt message to print
105 * @param ap arguments
107 * @ingroup krb5_error
110 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
111 krb5_vwarn(krb5_context context, krb5_error_code code,
112 const char *fmt, va_list ap)
113 __attribute__ ((format (printf, 3, 0)))
115 return _warnerr(context, 1, code, 1, fmt, ap);
119 * Log a warning to the log, default stderr, include the error from
120 * the last failure.
122 * @param context A Kerberos 5 context.
123 * @param code error code of the last error
124 * @param fmt message to print
126 * @ingroup krb5_error
129 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
130 krb5_warn(krb5_context context, krb5_error_code code, const char *fmt, ...)
131 __attribute__ ((format (printf, 3, 4)))
133 FUNC(1, code, 1);
134 return ret;
138 * Log a warning to the log, default stderr.
140 * @param context A Kerberos 5 context.
141 * @param fmt message to print
142 * @param ap arguments
144 * @ingroup krb5_error
147 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
148 krb5_vwarnx(krb5_context context, const char *fmt, va_list ap)
149 __attribute__ ((format (printf, 2, 0)))
151 return _warnerr(context, 0, 0, 1, fmt, ap);
155 * Log a warning to the log, default stderr.
157 * @param context A Kerberos 5 context.
158 * @param fmt message to print
160 * @ingroup krb5_error
163 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
164 krb5_warnx(krb5_context context, const char *fmt, ...)
165 __attribute__ ((format (printf, 2, 3)))
167 FUNC(0, 0, 1);
168 return ret;
172 * Log a warning to the log, default stderr, include bthe error from
173 * the last failure and then exit.
175 * @param context A Kerberos 5 context
176 * @param eval the exit code to exit with
177 * @param code error code of the last error
178 * @param fmt message to print
179 * @param ap arguments
181 * @ingroup krb5_error
184 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
185 krb5_verr(krb5_context context, int eval, krb5_error_code code,
186 const char *fmt, va_list ap)
187 __attribute__ ((noreturn, format (printf, 4, 0)))
189 _warnerr(context, 1, code, 0, fmt, ap);
190 exit(eval);
191 UNREACHABLE(return 0);
195 * Log a warning to the log, default stderr, include bthe error from
196 * the last failure and then exit.
198 * @param context A Kerberos 5 context
199 * @param eval the exit code to exit with
200 * @param code error code of the last error
201 * @param fmt message to print
203 * @ingroup krb5_error
206 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
207 krb5_err(krb5_context context, int eval, krb5_error_code code,
208 const char *fmt, ...)
209 __attribute__ ((noreturn, format (printf, 4, 5)))
211 FUNC_NORET(1, code, 0);
212 exit(eval);
213 UNREACHABLE(return 0);
217 * Log a warning to the log, default stderr, and then exit.
219 * @param context A Kerberos 5 context
220 * @param eval the exit code to exit with
221 * @param fmt message to print
222 * @param ap arguments
224 * @ingroup krb5_error
227 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
228 krb5_verrx(krb5_context context, int eval, const char *fmt, va_list ap)
229 __attribute__ ((noreturn, format (printf, 3, 0)))
231 _warnerr(context, 0, 0, 0, fmt, ap);
232 exit(eval);
233 UNREACHABLE(return 0);
237 * Log a warning to the log, default stderr, and then exit.
239 * @param context A Kerberos 5 context
240 * @param eval the exit code to exit with
241 * @param fmt message to print
243 * @ingroup krb5_error
246 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
247 krb5_errx(krb5_context context, int eval, const char *fmt, ...)
248 __attribute__ ((noreturn, format (printf, 3, 4)))
250 FUNC_NORET(0, 0, 0);
251 exit(eval);
252 UNREACHABLE(return 0);
256 * Log a warning to the log, default stderr, include bthe error from
257 * the last failure and then abort.
259 * @param context A Kerberos 5 context
260 * @param code error code of the last error
261 * @param fmt message to print
262 * @param ap arguments
264 * @ingroup krb5_error
267 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
268 krb5_vabort(krb5_context context, krb5_error_code code,
269 const char *fmt, va_list ap)
270 __attribute__ ((noreturn, format (printf, 3, 0)))
272 _warnerr(context, 1, code, 0, fmt, ap);
273 abort();
274 UNREACHABLE(return 0);
278 * Log a warning to the log, default stderr, include the error from
279 * the last failure and then abort.
281 * @param context A Kerberos 5 context
282 * @param code error code of the last error
283 * @param fmt message to print
285 * @ingroup krb5_error
288 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
289 krb5_abort(krb5_context context, krb5_error_code code, const char *fmt, ...)
290 __attribute__ ((noreturn, format (printf, 3, 4)))
292 FUNC_NORET(1, code, 0);
293 abort();
294 UNREACHABLE(return 0);
297 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
298 krb5_vabortx(krb5_context context, const char *fmt, va_list ap)
299 __attribute__ ((noreturn, format (printf, 2, 0)))
301 _warnerr(context, 0, 0, 0, fmt, ap);
302 abort();
303 UNREACHABLE(return 0);
307 * Log a warning to the log, default stderr, and then abort.
309 * @param context A Kerberos 5 context
310 * @param code error code of the last error
311 * @param fmt message to print
313 * @ingroup krb5_error
316 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
317 krb5_abortx(krb5_context context, const char *fmt, ...)
318 __attribute__ ((noreturn, format (printf, 2, 3)))
320 FUNC_NORET(0, 0, 0);
321 abort();
322 UNREACHABLE(return 0);
326 * Set the default logging facility.
328 * @param context A Kerberos 5 context
329 * @param fac Facility to use for logging.
331 * @ingroup krb5_error
334 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
335 krb5_set_warn_dest(krb5_context context, krb5_log_facility *fac)
337 context->warn_dest = fac;
338 return 0;
342 * Get the default logging facility.
344 * @param context A Kerberos 5 context
346 * @ingroup krb5_error
349 KRB5_LIB_FUNCTION krb5_log_facility * KRB5_LIB_CALL
350 krb5_get_warn_dest(krb5_context context)
352 return context->warn_dest;