s4:heimdal: import lorikeet-heimdal-200906080040 (commit 904d0124b46eed7a8ad6e5b73e89...
[metze/samba/wip.git] / source4 / heimdal / lib / krb5 / warn.c
1 /*
2  * Copyright (c) 1997 - 2001 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 #include <err.h>
36
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)));
40         
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)
44 {
45     char xfmt[7] = "";
46     const char *args[2], **arg;
47     char *msg = NULL;
48     const char *err_str = NULL;
49
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;
60     }
61     if(context && do_errtext){
62         const char *err_msg;
63
64         strlcat(xfmt, "%s", sizeof(xfmt));
65
66         err_str = krb5_get_error_message(context, code);
67         if (err_str != NULL) {
68             *arg++ = err_str;
69         } else {
70             err_msg = krb5_get_err_text(context, code);
71             if (err_msg)
72                 *arg++ = err_msg;
73             else
74                 *arg++ = "<unknown error>";
75         }
76     }
77         
78     if(context && context->warn_dest)
79         krb5_log(context, context->warn_dest, level, xfmt, args[0], args[1]);
80     else
81         warnx(xfmt, args[0], args[1]);
82     free(msg);
83     krb5_free_error_message(context, err_str);
84     return 0;
85 }
86
87 #define FUNC(ETEXT, CODE, LEVEL)                                        \
88     krb5_error_code ret;                                                \
89     va_list ap;                                                         \
90     va_start(ap, fmt);                                                  \
91     ret = _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap);               \
92     va_end(ap);
93
94 #undef __attribute__
95 #define __attribute__(X)
96
97 /**
98  * Log a warning to the log, default stderr, include the error from
99  * the last failure.
100  *
101  * @param context A Kerberos 5 context.
102  * @param code error code of the last error
103  * @param fmt message to print
104  * @param ap arguments
105  *
106  * @ingroup krb5_error
107  */
108
109 krb5_error_code KRB5_LIB_FUNCTION
110 krb5_vwarn(krb5_context context, krb5_error_code code,
111            const char *fmt, va_list ap)
112      __attribute__ ((format (printf, 3, 0)))
113 {
114     return _warnerr(context, 1, code, 1, fmt, ap);
115 }
116
117 /**
118  * Log a warning to the log, default stderr, include the error from
119  * the last failure.
120  *
121  * @param context A Kerberos 5 context.
122  * @param code error code of the last error
123  * @param fmt message to print
124  *
125  * @ingroup krb5_error
126  */
127
128 krb5_error_code KRB5_LIB_FUNCTION
129 krb5_warn(krb5_context context, krb5_error_code code, const char *fmt, ...)
130      __attribute__ ((format (printf, 3, 4)))
131 {
132     FUNC(1, code, 1);
133     return ret;
134 }
135
136 /**
137  * Log a warning to the log, default stderr.
138  *
139  * @param context A Kerberos 5 context.
140  * @param fmt message to print
141  * @param ap arguments
142  *
143  * @ingroup krb5_error
144  */
145
146 krb5_error_code KRB5_LIB_FUNCTION
147 krb5_vwarnx(krb5_context context, const char *fmt, va_list ap)
148      __attribute__ ((format (printf, 2, 0)))
149 {
150     return _warnerr(context, 0, 0, 1, fmt, ap);
151 }
152
153 /**
154  * Log a warning to the log, default stderr.
155  *
156  * @param context A Kerberos 5 context.
157  * @param fmt message to print
158  *
159  * @ingroup krb5_error
160  */
161
162 krb5_error_code KRB5_LIB_FUNCTION
163 krb5_warnx(krb5_context context, const char *fmt, ...)
164      __attribute__ ((format (printf, 2, 3)))
165 {
166     FUNC(0, 0, 1);
167     return ret;
168 }
169
170 /**
171  * Log a warning to the log, default stderr, include bthe error from
172  * the last failure and then exit.
173  *
174  * @param context A Kerberos 5 context
175  * @param eval the exit code to exit with
176  * @param code error code of the last error
177  * @param fmt message to print
178  * @param ap arguments
179  *
180  * @ingroup krb5_error
181  */
182
183 krb5_error_code KRB5_LIB_FUNCTION
184 krb5_verr(krb5_context context, int eval, krb5_error_code code,
185           const char *fmt, va_list ap)
186      __attribute__ ((noreturn, format (printf, 4, 0)))
187 {
188     _warnerr(context, 1, code, 0, fmt, ap);
189     exit(eval);
190 }
191
192 /**
193  * Log a warning to the log, default stderr, include bthe error from
194  * the last failure and then exit.
195  *
196  * @param context A Kerberos 5 context
197  * @param eval the exit code to exit with
198  * @param code error code of the last error
199  * @param fmt message to print
200  *
201  * @ingroup krb5_error
202  */
203
204 krb5_error_code KRB5_LIB_FUNCTION
205 krb5_err(krb5_context context, int eval, krb5_error_code code,
206          const char *fmt, ...)
207      __attribute__ ((noreturn, format (printf, 4, 5)))
208 {
209     FUNC(1, code, 0);
210     exit(eval);
211 }
212
213 /**
214  * Log a warning to the log, default stderr, and then exit.
215  *
216  * @param context A Kerberos 5 context
217  * @param eval the exit code to exit with
218  * @param fmt message to print
219  * @param ap arguments
220  *
221  * @ingroup krb5_error
222  */
223
224 krb5_error_code KRB5_LIB_FUNCTION
225 krb5_verrx(krb5_context context, int eval, const char *fmt, va_list ap)
226      __attribute__ ((noreturn, format (printf, 3, 0)))
227 {
228     _warnerr(context, 0, 0, 0, fmt, ap);
229     exit(eval);
230 }
231
232 /**
233  * Log a warning to the log, default stderr, and then exit.
234  *
235  * @param context A Kerberos 5 context
236  * @param eval the exit code to exit with
237  * @param fmt message to print
238  *
239  * @ingroup krb5_error
240  */
241
242 krb5_error_code KRB5_LIB_FUNCTION
243 krb5_errx(krb5_context context, int eval, const char *fmt, ...)
244      __attribute__ ((noreturn, format (printf, 3, 4)))
245 {
246     FUNC(0, 0, 0);
247     exit(eval);
248 }
249
250 /**
251  * Log a warning to the log, default stderr, include bthe error from
252  * the last failure and then abort.
253  *
254  * @param context A Kerberos 5 context
255  * @param code error code of the last error
256  * @param fmt message to print
257  * @param ap arguments
258  *
259  * @ingroup krb5_error
260  */
261
262 krb5_error_code KRB5_LIB_FUNCTION
263 krb5_vabort(krb5_context context, krb5_error_code code,
264             const char *fmt, va_list ap)
265      __attribute__ ((noreturn, format (printf, 3, 0)))
266 {
267     _warnerr(context, 1, code, 0, fmt, ap);
268     abort();
269 }
270
271 /**
272  * Log a warning to the log, default stderr, include bthe error from
273  * the last failure and then abort.
274  *
275  * @param context A Kerberos 5 context
276  * @param code error code of the last error
277  * @param fmt message to print
278  *
279  * @ingroup krb5_error
280  */
281
282 krb5_error_code KRB5_LIB_FUNCTION
283 krb5_abort(krb5_context context, krb5_error_code code, const char *fmt, ...)
284      __attribute__ ((noreturn, format (printf, 3, 4)))
285 {
286     FUNC(1, code, 0);
287     abort();
288 }
289
290 krb5_error_code KRB5_LIB_FUNCTION
291 krb5_vabortx(krb5_context context, const char *fmt, va_list ap)
292      __attribute__ ((noreturn, format (printf, 2, 0)))
293 {
294     _warnerr(context, 0, 0, 0, fmt, ap);
295     abort();
296 }
297
298 /**
299  * Log a warning to the log, default stderr, and then abort.
300  *
301  * @param context A Kerberos 5 context
302  * @param code error code of the last error
303  * @param fmt message to print
304  *
305  * @ingroup krb5_error
306  */
307
308 krb5_error_code KRB5_LIB_FUNCTION
309 krb5_abortx(krb5_context context, const char *fmt, ...)
310      __attribute__ ((noreturn, format (printf, 2, 3)))
311 {
312     FUNC(0, 0, 0);
313     abort();
314 }
315
316 /**
317  * Set the default logging facility.
318  *
319  * @param context A Kerberos 5 context
320  * @param fac Facility to use for logging.
321  *
322  * @ingroup krb5_error
323  */
324
325 krb5_error_code KRB5_LIB_FUNCTION
326 krb5_set_warn_dest(krb5_context context, krb5_log_facility *fac)
327 {
328     context->warn_dest = fac;
329     return 0;
330 }
331
332 /**
333  * Get the default logging facility.
334  *
335  * @param context A Kerberos 5 context
336  *
337  * @ingroup krb5_error
338  */
339
340 krb5_log_facility * KRB5_LIB_FUNCTION
341 krb5_get_warn_dest(krb5_context context)
342 {
343     return context->warn_dest;
344 }