s4:heimdal: import lorikeet-heimdal-200911122202 (commit 9291fd2d101f3eecec550178634f...
[samba.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         strlcat(xfmt, "%s", sizeof(xfmt));
63
64         err_str = krb5_get_error_message(context, code);
65         if (err_str != NULL) {
66             *arg = err_str;
67         } else {
68             *arg= "<unknown error>";
69         }
70     }
71         
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;
79 }
80
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);
87
88 #undef __attribute__
89 #define __attribute__(X)
90
91 /**
92  * Log a warning to the log, default stderr, include the error from
93  * the last failure.
94  *
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
99  *
100  * @ingroup krb5_error
101  */
102
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)))
107 {
108     return _warnerr(context, 1, code, 1, fmt, ap);
109 }
110
111 /**
112  * Log a warning to the log, default stderr, include the error from
113  * the last failure.
114  *
115  * @param context A Kerberos 5 context.
116  * @param code error code of the last error
117  * @param fmt message to print
118  *
119  * @ingroup krb5_error
120  */
121
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)))
125 {
126     FUNC(1, code, 1);
127     return ret;
128 }
129
130 /**
131  * Log a warning to the log, default stderr.
132  *
133  * @param context A Kerberos 5 context.
134  * @param fmt message to print
135  * @param ap arguments
136  *
137  * @ingroup krb5_error
138  */
139
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)))
143 {
144     return _warnerr(context, 0, 0, 1, fmt, ap);
145 }
146
147 /**
148  * Log a warning to the log, default stderr.
149  *
150  * @param context A Kerberos 5 context.
151  * @param fmt message to print
152  *
153  * @ingroup krb5_error
154  */
155
156 krb5_error_code KRB5_LIB_FUNCTION
157 krb5_warnx(krb5_context context, const char *fmt, ...)
158      __attribute__ ((format (printf, 2, 3)))
159 {
160     FUNC(0, 0, 1);
161     return ret;
162 }
163
164 /**
165  * Log a warning to the log, default stderr, include bthe error from
166  * the last failure and then exit.
167  *
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
173  *
174  * @ingroup krb5_error
175  */
176
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)))
181 {
182     _warnerr(context, 1, code, 0, fmt, ap);
183     exit(eval);
184 }
185
186 /**
187  * Log a warning to the log, default stderr, include bthe error from
188  * the last failure and then exit.
189  *
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
194  *
195  * @ingroup krb5_error
196  */
197
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)))
202 {
203     FUNC(1, code, 0);
204     exit(eval);
205 }
206
207 /**
208  * Log a warning to the log, default stderr, and then exit.
209  *
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
214  *
215  * @ingroup krb5_error
216  */
217
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)))
221 {
222     _warnerr(context, 0, 0, 0, fmt, ap);
223     exit(eval);
224 }
225
226 /**
227  * Log a warning to the log, default stderr, and then exit.
228  *
229  * @param context A Kerberos 5 context
230  * @param eval the exit code to exit with
231  * @param fmt message to print
232  *
233  * @ingroup krb5_error
234  */
235
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)))
239 {
240     FUNC(0, 0, 0);
241     exit(eval);
242 }
243
244 /**
245  * Log a warning to the log, default stderr, include bthe error from
246  * the last failure and then abort.
247  *
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
252  *
253  * @ingroup krb5_error
254  */
255
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)))
260 {
261     _warnerr(context, 1, code, 0, fmt, ap);
262     abort();
263 }
264
265 /**
266  * Log a warning to the log, default stderr, include the error from
267  * the last failure and then abort.
268  *
269  * @param context A Kerberos 5 context
270  * @param code error code of the last error
271  * @param fmt message to print
272  *
273  * @ingroup krb5_error
274  */
275
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)))
279 {
280     FUNC(1, code, 0);
281     abort();
282 }
283
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)))
287 {
288     _warnerr(context, 0, 0, 0, fmt, ap);
289     abort();
290 }
291
292 /**
293  * Log a warning to the log, default stderr, and then abort.
294  *
295  * @param context A Kerberos 5 context
296  * @param code error code of the last error
297  * @param fmt message to print
298  *
299  * @ingroup krb5_error
300  */
301
302 krb5_error_code KRB5_LIB_FUNCTION
303 krb5_abortx(krb5_context context, const char *fmt, ...)
304      __attribute__ ((noreturn, format (printf, 2, 3)))
305 {
306     FUNC(0, 0, 0);
307     abort();
308 }
309
310 /**
311  * Set the default logging facility.
312  *
313  * @param context A Kerberos 5 context
314  * @param fac Facility to use for logging.
315  *
316  * @ingroup krb5_error
317  */
318
319 krb5_error_code KRB5_LIB_FUNCTION
320 krb5_set_warn_dest(krb5_context context, krb5_log_facility *fac)
321 {
322     context->warn_dest = fac;
323     return 0;
324 }
325
326 /**
327  * Get the default logging facility.
328  *
329  * @param context A Kerberos 5 context
330  *
331  * @ingroup krb5_error
332  */
333
334 krb5_log_facility * KRB5_LIB_FUNCTION
335 krb5_get_warn_dest(krb5_context context)
336 {
337     return context->warn_dest;
338 }