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