s4:heimdal: import lorikeet-heimdal-200911122202 (commit 9291fd2d101f3eecec550178634f...
[Samba/ekacnet.git] / source4 / heimdal / lib / krb5 / warn.c
blob886a1fe981bb898b28ac8d685fb10bb8de04ffcd
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;
50 args[0] = args[1] = NULL;
51 arg = args;
52 if(fmt){
53 strlcat(xfmt, "%s", sizeof(xfmt));
54 if(do_errtext)
55 strlcat(xfmt, ": ", sizeof(xfmt));
56 vasprintf(&msg, fmt, ap);
57 if(msg == NULL)
58 return ENOMEM;
59 *arg++ = msg;
61 if(context && do_errtext){
62 strlcat(xfmt, "%s", sizeof(xfmt));
64 err_str = krb5_get_error_message(context, code);
65 if (err_str != NULL) {
66 *arg = err_str;
67 } else {
68 *arg= "<unknown error>";
72 if(context && context->warn_dest)
73 krb5_log(context, context->warn_dest, level, xfmt, args[0], args[1]);
74 else
75 warnx(xfmt, args[0], args[1]);
76 free(msg);
77 krb5_free_error_message(context, err_str);
78 return 0;
81 #define FUNC(ETEXT, CODE, LEVEL) \
82 krb5_error_code ret; \
83 va_list ap; \
84 va_start(ap, fmt); \
85 ret = _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap); \
86 va_end(ap);
88 #undef __attribute__
89 #define __attribute__(X)
91 /**
92 * Log a warning to the log, default stderr, include the error from
93 * the last failure.
95 * @param context A Kerberos 5 context.
96 * @param code error code of the last error
97 * @param fmt message to print
98 * @param ap arguments
100 * @ingroup krb5_error
103 krb5_error_code KRB5_LIB_FUNCTION
104 krb5_vwarn(krb5_context context, krb5_error_code code,
105 const char *fmt, va_list ap)
106 __attribute__ ((format (printf, 3, 0)))
108 return _warnerr(context, 1, code, 1, fmt, ap);
112 * Log a warning to the log, default stderr, include the error from
113 * the last failure.
115 * @param context A Kerberos 5 context.
116 * @param code error code of the last error
117 * @param fmt message to print
119 * @ingroup krb5_error
122 krb5_error_code KRB5_LIB_FUNCTION
123 krb5_warn(krb5_context context, krb5_error_code code, const char *fmt, ...)
124 __attribute__ ((format (printf, 3, 4)))
126 FUNC(1, code, 1);
127 return ret;
131 * Log a warning to the log, default stderr.
133 * @param context A Kerberos 5 context.
134 * @param fmt message to print
135 * @param ap arguments
137 * @ingroup krb5_error
140 krb5_error_code KRB5_LIB_FUNCTION
141 krb5_vwarnx(krb5_context context, const char *fmt, va_list ap)
142 __attribute__ ((format (printf, 2, 0)))
144 return _warnerr(context, 0, 0, 1, fmt, ap);
148 * Log a warning to the log, default stderr.
150 * @param context A Kerberos 5 context.
151 * @param fmt message to print
153 * @ingroup krb5_error
156 krb5_error_code KRB5_LIB_FUNCTION
157 krb5_warnx(krb5_context context, const char *fmt, ...)
158 __attribute__ ((format (printf, 2, 3)))
160 FUNC(0, 0, 1);
161 return ret;
165 * Log a warning to the log, default stderr, include bthe error from
166 * the last failure and then exit.
168 * @param context A Kerberos 5 context
169 * @param eval the exit code to exit with
170 * @param code error code of the last error
171 * @param fmt message to print
172 * @param ap arguments
174 * @ingroup krb5_error
177 krb5_error_code KRB5_LIB_FUNCTION
178 krb5_verr(krb5_context context, int eval, krb5_error_code code,
179 const char *fmt, va_list ap)
180 __attribute__ ((noreturn, format (printf, 4, 0)))
182 _warnerr(context, 1, code, 0, fmt, ap);
183 exit(eval);
187 * Log a warning to the log, default stderr, include bthe error from
188 * the last failure and then exit.
190 * @param context A Kerberos 5 context
191 * @param eval the exit code to exit with
192 * @param code error code of the last error
193 * @param fmt message to print
195 * @ingroup krb5_error
198 krb5_error_code KRB5_LIB_FUNCTION
199 krb5_err(krb5_context context, int eval, krb5_error_code code,
200 const char *fmt, ...)
201 __attribute__ ((noreturn, format (printf, 4, 5)))
203 FUNC(1, code, 0);
204 exit(eval);
208 * Log a warning to the log, default stderr, and then exit.
210 * @param context A Kerberos 5 context
211 * @param eval the exit code to exit with
212 * @param fmt message to print
213 * @param ap arguments
215 * @ingroup krb5_error
218 krb5_error_code KRB5_LIB_FUNCTION
219 krb5_verrx(krb5_context context, int eval, const char *fmt, va_list ap)
220 __attribute__ ((noreturn, format (printf, 3, 0)))
222 _warnerr(context, 0, 0, 0, fmt, ap);
223 exit(eval);
227 * Log a warning to the log, default stderr, and then exit.
229 * @param context A Kerberos 5 context
230 * @param eval the exit code to exit with
231 * @param fmt message to print
233 * @ingroup krb5_error
236 krb5_error_code KRB5_LIB_FUNCTION
237 krb5_errx(krb5_context context, int eval, const char *fmt, ...)
238 __attribute__ ((noreturn, format (printf, 3, 4)))
240 FUNC(0, 0, 0);
241 exit(eval);
245 * Log a warning to the log, default stderr, include bthe error from
246 * the last failure and then abort.
248 * @param context A Kerberos 5 context
249 * @param code error code of the last error
250 * @param fmt message to print
251 * @param ap arguments
253 * @ingroup krb5_error
256 krb5_error_code KRB5_LIB_FUNCTION
257 krb5_vabort(krb5_context context, krb5_error_code code,
258 const char *fmt, va_list ap)
259 __attribute__ ((noreturn, format (printf, 3, 0)))
261 _warnerr(context, 1, code, 0, fmt, ap);
262 abort();
266 * Log a warning to the log, default stderr, include the error from
267 * the last failure and then abort.
269 * @param context A Kerberos 5 context
270 * @param code error code of the last error
271 * @param fmt message to print
273 * @ingroup krb5_error
276 krb5_error_code KRB5_LIB_FUNCTION
277 krb5_abort(krb5_context context, krb5_error_code code, const char *fmt, ...)
278 __attribute__ ((noreturn, format (printf, 3, 4)))
280 FUNC(1, code, 0);
281 abort();
284 krb5_error_code KRB5_LIB_FUNCTION
285 krb5_vabortx(krb5_context context, const char *fmt, va_list ap)
286 __attribute__ ((noreturn, format (printf, 2, 0)))
288 _warnerr(context, 0, 0, 0, fmt, ap);
289 abort();
293 * Log a warning to the log, default stderr, and then abort.
295 * @param context A Kerberos 5 context
296 * @param code error code of the last error
297 * @param fmt message to print
299 * @ingroup krb5_error
302 krb5_error_code KRB5_LIB_FUNCTION
303 krb5_abortx(krb5_context context, const char *fmt, ...)
304 __attribute__ ((noreturn, format (printf, 2, 3)))
306 FUNC(0, 0, 0);
307 abort();
311 * Set the default logging facility.
313 * @param context A Kerberos 5 context
314 * @param fac Facility to use for logging.
316 * @ingroup krb5_error
319 krb5_error_code KRB5_LIB_FUNCTION
320 krb5_set_warn_dest(krb5_context context, krb5_log_facility *fac)
322 context->warn_dest = fac;
323 return 0;
327 * Get the default logging facility.
329 * @param context A Kerberos 5 context
331 * @ingroup krb5_error
334 krb5_log_facility * KRB5_LIB_FUNCTION
335 krb5_get_warn_dest(krb5_context context)
337 return context->warn_dest;