xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/warn.c (revision 241bea01a19bbb306af27777a870b86d41cb3fda)
1*241bea01Schristos /*	$NetBSD: warn.c,v 1.3 2019/12/15 22:50:50 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include "krb5_locl.h"
37ca1c9b0cSelric #include <err.h>
38ca1c9b0cSelric 
39ca1c9b0cSelric static krb5_error_code _warnerr(krb5_context context, int do_errtext,
40ca1c9b0cSelric 	 krb5_error_code code, int level, const char *fmt, va_list ap)
41ca1c9b0cSelric 	__attribute__ ((__format__ (__printf__, 5, 0)));
42ca1c9b0cSelric 
43ca1c9b0cSelric static krb5_error_code
_warnerr(krb5_context context,int do_errtext,krb5_error_code code,int level,const char * fmt,va_list ap)44ca1c9b0cSelric _warnerr(krb5_context context, int do_errtext,
45ca1c9b0cSelric 	 krb5_error_code code, int level, const char *fmt, va_list ap)
46ca1c9b0cSelric {
47ca1c9b0cSelric     char xfmt[7] = "";
48ca1c9b0cSelric     const char *args[2], **arg;
49ca1c9b0cSelric     char *msg = NULL;
50ca1c9b0cSelric     const char *err_str = NULL;
51ca1c9b0cSelric     krb5_error_code ret;
52ca1c9b0cSelric 
53ca1c9b0cSelric     args[0] = args[1] = NULL;
54ca1c9b0cSelric     arg = args;
55ca1c9b0cSelric     if(fmt){
56ca1c9b0cSelric 	strlcat(xfmt, "%s", sizeof(xfmt));
57ca1c9b0cSelric 	if(do_errtext)
58ca1c9b0cSelric 	    strlcat(xfmt, ": ", sizeof(xfmt));
59ca1c9b0cSelric 	ret = vasprintf(&msg, fmt, ap);
60ca1c9b0cSelric 	if(ret < 0 || msg == NULL)
61ca1c9b0cSelric 	    return ENOMEM;
62ca1c9b0cSelric 	*arg++ = msg;
63ca1c9b0cSelric     }
64ca1c9b0cSelric     if(context && do_errtext){
65ca1c9b0cSelric 	strlcat(xfmt, "%s", sizeof(xfmt));
66ca1c9b0cSelric 
67ca1c9b0cSelric 	err_str = krb5_get_error_message(context, code);
68ca1c9b0cSelric 	if (err_str != NULL) {
69ca1c9b0cSelric 	    *arg = err_str;
70ca1c9b0cSelric 	} else {
71ca1c9b0cSelric 	    *arg= "<unknown error>";
72ca1c9b0cSelric 	}
73ca1c9b0cSelric     }
74ca1c9b0cSelric 
75ca1c9b0cSelric     if(context && context->warn_dest)
76ca1c9b0cSelric 	krb5_log(context, context->warn_dest, level, xfmt, args[0], args[1]);
77ca1c9b0cSelric     else
78ca1c9b0cSelric 	warnx(xfmt, args[0], args[1]);
79ca1c9b0cSelric     free(msg);
80ca1c9b0cSelric     krb5_free_error_message(context, err_str);
81ca1c9b0cSelric     return 0;
82ca1c9b0cSelric }
83ca1c9b0cSelric 
84ca1c9b0cSelric #define FUNC(ETEXT, CODE, LEVEL)					\
85ca1c9b0cSelric     krb5_error_code ret;						\
86ca1c9b0cSelric     va_list ap;								\
87ca1c9b0cSelric     va_start(ap, fmt);							\
88ca1c9b0cSelric     ret = _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap); 		\
89ca1c9b0cSelric     va_end(ap);
90ca1c9b0cSelric 
91b9d004c6Schristos #define FUNC_NORET(ETEXT, CODE, LEVEL)					\
92b9d004c6Schristos     va_list ap;								\
93b9d004c6Schristos     va_start(ap, fmt);							\
94b9d004c6Schristos     (void) _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap); 		\
95b9d004c6Schristos     va_end(ap);
96b9d004c6Schristos 
97ca1c9b0cSelric #undef __attribute__
98ca1c9b0cSelric #define __attribute__(X)
99ca1c9b0cSelric 
100ca1c9b0cSelric /**
101ca1c9b0cSelric  * Log a warning to the log, default stderr, include the error from
102ca1c9b0cSelric  * the last failure.
103ca1c9b0cSelric  *
104ca1c9b0cSelric  * @param context A Kerberos 5 context.
105ca1c9b0cSelric  * @param code error code of the last error
106ca1c9b0cSelric  * @param fmt message to print
107ca1c9b0cSelric  * @param ap arguments
108ca1c9b0cSelric  *
109ca1c9b0cSelric  * @ingroup krb5_error
110ca1c9b0cSelric  */
111ca1c9b0cSelric 
112ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_vwarn(krb5_context context,krb5_error_code code,const char * fmt,va_list ap)113ca1c9b0cSelric krb5_vwarn(krb5_context context, krb5_error_code code,
114ca1c9b0cSelric 	   const char *fmt, va_list ap)
115d3273b5bSchristos      __attribute__ ((__format__ (__printf__, 3, 0)))
116ca1c9b0cSelric {
117ca1c9b0cSelric     return _warnerr(context, 1, code, 1, fmt, ap);
118ca1c9b0cSelric }
119ca1c9b0cSelric 
120ca1c9b0cSelric /**
121ca1c9b0cSelric  * Log a warning to the log, default stderr, include the error from
122ca1c9b0cSelric  * the last failure.
123ca1c9b0cSelric  *
124ca1c9b0cSelric  * @param context A Kerberos 5 context.
125ca1c9b0cSelric  * @param code error code of the last error
126ca1c9b0cSelric  * @param fmt message to print
127ca1c9b0cSelric  *
128ca1c9b0cSelric  * @ingroup krb5_error
129ca1c9b0cSelric  */
130ca1c9b0cSelric 
131ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_warn(krb5_context context,krb5_error_code code,const char * fmt,...)132ca1c9b0cSelric krb5_warn(krb5_context context, krb5_error_code code, const char *fmt, ...)
133d3273b5bSchristos      __attribute__ ((__format__ (__printf__, 3, 4)))
134ca1c9b0cSelric {
135ca1c9b0cSelric     FUNC(1, code, 1);
136ca1c9b0cSelric     return ret;
137ca1c9b0cSelric }
138ca1c9b0cSelric 
139ca1c9b0cSelric /**
140ca1c9b0cSelric  * Log a warning to the log, default stderr.
141ca1c9b0cSelric  *
142ca1c9b0cSelric  * @param context A Kerberos 5 context.
143ca1c9b0cSelric  * @param fmt message to print
144ca1c9b0cSelric  * @param ap arguments
145ca1c9b0cSelric  *
146ca1c9b0cSelric  * @ingroup krb5_error
147ca1c9b0cSelric  */
148ca1c9b0cSelric 
149ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_vwarnx(krb5_context context,const char * fmt,va_list ap)150ca1c9b0cSelric krb5_vwarnx(krb5_context context, const char *fmt, va_list ap)
151d3273b5bSchristos      __attribute__ ((__format__ (__printf__, 2, 0)))
152ca1c9b0cSelric {
153ca1c9b0cSelric     return _warnerr(context, 0, 0, 1, fmt, ap);
154ca1c9b0cSelric }
155ca1c9b0cSelric 
156ca1c9b0cSelric /**
157ca1c9b0cSelric  * Log a warning to the log, default stderr.
158ca1c9b0cSelric  *
159ca1c9b0cSelric  * @param context A Kerberos 5 context.
160ca1c9b0cSelric  * @param fmt message to print
161ca1c9b0cSelric  *
162ca1c9b0cSelric  * @ingroup krb5_error
163ca1c9b0cSelric  */
164ca1c9b0cSelric 
165ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_warnx(krb5_context context,const char * fmt,...)166ca1c9b0cSelric krb5_warnx(krb5_context context, const char *fmt, ...)
167d3273b5bSchristos      __attribute__ ((__format__ (__printf__, 2, 3)))
168ca1c9b0cSelric {
169ca1c9b0cSelric     FUNC(0, 0, 1);
170ca1c9b0cSelric     return ret;
171ca1c9b0cSelric }
172ca1c9b0cSelric 
173ca1c9b0cSelric /**
174ca1c9b0cSelric  * Log a warning to the log, default stderr, include bthe error from
175ca1c9b0cSelric  * the last failure and then exit.
176ca1c9b0cSelric  *
177ca1c9b0cSelric  * @param context A Kerberos 5 context
178ca1c9b0cSelric  * @param eval the exit code to exit with
179ca1c9b0cSelric  * @param code error code of the last error
180ca1c9b0cSelric  * @param fmt message to print
181ca1c9b0cSelric  * @param ap arguments
182ca1c9b0cSelric  *
183ca1c9b0cSelric  * @ingroup krb5_error
184ca1c9b0cSelric  */
185ca1c9b0cSelric 
186ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_verr(krb5_context context,int eval,krb5_error_code code,const char * fmt,va_list ap)187ca1c9b0cSelric krb5_verr(krb5_context context, int eval, krb5_error_code code,
188ca1c9b0cSelric 	  const char *fmt, va_list ap)
189d3273b5bSchristos      __attribute__ ((__noreturn__, __format__ (__printf__, 4, 0)))
190ca1c9b0cSelric {
191ca1c9b0cSelric     _warnerr(context, 1, code, 0, fmt, ap);
192ca1c9b0cSelric     exit(eval);
193ca1c9b0cSelric     UNREACHABLE(return 0);
194ca1c9b0cSelric }
195ca1c9b0cSelric 
196ca1c9b0cSelric /**
197ca1c9b0cSelric  * Log a warning to the log, default stderr, include bthe error from
198ca1c9b0cSelric  * the last failure and then exit.
199ca1c9b0cSelric  *
200ca1c9b0cSelric  * @param context A Kerberos 5 context
201ca1c9b0cSelric  * @param eval the exit code to exit with
202ca1c9b0cSelric  * @param code error code of the last error
203ca1c9b0cSelric  * @param fmt message to print
204ca1c9b0cSelric  *
205ca1c9b0cSelric  * @ingroup krb5_error
206ca1c9b0cSelric  */
207ca1c9b0cSelric 
208ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_err(krb5_context context,int eval,krb5_error_code code,const char * fmt,...)209ca1c9b0cSelric krb5_err(krb5_context context, int eval, krb5_error_code code,
210ca1c9b0cSelric 	 const char *fmt, ...)
211d3273b5bSchristos      __attribute__ ((__noreturn__, __format__ (__printf__, 4, 5)))
212ca1c9b0cSelric {
213b9d004c6Schristos     FUNC_NORET(1, code, 0);
214ca1c9b0cSelric     exit(eval);
215ca1c9b0cSelric     UNREACHABLE(return 0);
216ca1c9b0cSelric }
217ca1c9b0cSelric 
218ca1c9b0cSelric /**
219ca1c9b0cSelric  * Log a warning to the log, default stderr, and then exit.
220ca1c9b0cSelric  *
221ca1c9b0cSelric  * @param context A Kerberos 5 context
222ca1c9b0cSelric  * @param eval the exit code to exit with
223ca1c9b0cSelric  * @param fmt message to print
224ca1c9b0cSelric  * @param ap arguments
225ca1c9b0cSelric  *
226ca1c9b0cSelric  * @ingroup krb5_error
227ca1c9b0cSelric  */
228ca1c9b0cSelric 
229ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_verrx(krb5_context context,int eval,const char * fmt,va_list ap)230ca1c9b0cSelric krb5_verrx(krb5_context context, int eval, const char *fmt, va_list ap)
231d3273b5bSchristos      __attribute__ ((__noreturn__, __format__ (__printf__, 3, 0)))
232ca1c9b0cSelric {
233ca1c9b0cSelric     _warnerr(context, 0, 0, 0, fmt, ap);
234ca1c9b0cSelric     exit(eval);
235ca1c9b0cSelric     UNREACHABLE(return 0);
236ca1c9b0cSelric }
237ca1c9b0cSelric 
238ca1c9b0cSelric /**
239ca1c9b0cSelric  * Log a warning to the log, default stderr, and then exit.
240ca1c9b0cSelric  *
241ca1c9b0cSelric  * @param context A Kerberos 5 context
242ca1c9b0cSelric  * @param eval the exit code to exit with
243ca1c9b0cSelric  * @param fmt message to print
244ca1c9b0cSelric  *
245ca1c9b0cSelric  * @ingroup krb5_error
246ca1c9b0cSelric  */
247ca1c9b0cSelric 
248ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_errx(krb5_context context,int eval,const char * fmt,...)249ca1c9b0cSelric krb5_errx(krb5_context context, int eval, const char *fmt, ...)
250d3273b5bSchristos      __attribute__ ((__noreturn__, __format__ (__printf__, 3, 4)))
251ca1c9b0cSelric {
252b9d004c6Schristos     FUNC_NORET(0, 0, 0);
253ca1c9b0cSelric     exit(eval);
254ca1c9b0cSelric     UNREACHABLE(return 0);
255ca1c9b0cSelric }
256ca1c9b0cSelric 
257ca1c9b0cSelric /**
258ca1c9b0cSelric  * Log a warning to the log, default stderr, include bthe error from
259ca1c9b0cSelric  * the last failure and then abort.
260ca1c9b0cSelric  *
261ca1c9b0cSelric  * @param context A Kerberos 5 context
262ca1c9b0cSelric  * @param code error code of the last error
263ca1c9b0cSelric  * @param fmt message to print
264ca1c9b0cSelric  * @param ap arguments
265ca1c9b0cSelric  *
266ca1c9b0cSelric  * @ingroup krb5_error
267ca1c9b0cSelric  */
268ca1c9b0cSelric 
269ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_vabort(krb5_context context,krb5_error_code code,const char * fmt,va_list ap)270ca1c9b0cSelric krb5_vabort(krb5_context context, krb5_error_code code,
271ca1c9b0cSelric 	    const char *fmt, va_list ap)
272d3273b5bSchristos      __attribute__ ((__noreturn__, __format__ (__printf__, 3, 0)))
273ca1c9b0cSelric {
274ca1c9b0cSelric     _warnerr(context, 1, code, 0, fmt, ap);
275ca1c9b0cSelric     abort();
276ca1c9b0cSelric     UNREACHABLE(return 0);
277ca1c9b0cSelric }
278ca1c9b0cSelric 
279ca1c9b0cSelric /**
280ca1c9b0cSelric  * Log a warning to the log, default stderr, include the error from
281ca1c9b0cSelric  * the last failure and then abort.
282ca1c9b0cSelric  *
283ca1c9b0cSelric  * @param context A Kerberos 5 context
284ca1c9b0cSelric  * @param code error code of the last error
285ca1c9b0cSelric  * @param fmt message to print
286b9d004c6Schristos  * @param ... arguments for format string
287ca1c9b0cSelric  *
288ca1c9b0cSelric  * @ingroup krb5_error
289ca1c9b0cSelric  */
290ca1c9b0cSelric 
291ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_abort(krb5_context context,krb5_error_code code,const char * fmt,...)292ca1c9b0cSelric krb5_abort(krb5_context context, krb5_error_code code, const char *fmt, ...)
293d3273b5bSchristos      __attribute__ ((__noreturn__, __format__ (__printf__, 3, 4)))
294ca1c9b0cSelric {
295b9d004c6Schristos     FUNC_NORET(1, code, 0);
296ca1c9b0cSelric     abort();
297ca1c9b0cSelric     UNREACHABLE(return 0);
298ca1c9b0cSelric }
299ca1c9b0cSelric 
300ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_vabortx(krb5_context context,const char * fmt,va_list ap)301ca1c9b0cSelric krb5_vabortx(krb5_context context, const char *fmt, va_list ap)
302d3273b5bSchristos      __attribute__ ((__noreturn__, __format__ (__printf__, 2, 0)))
303ca1c9b0cSelric {
304ca1c9b0cSelric     _warnerr(context, 0, 0, 0, fmt, ap);
305ca1c9b0cSelric     abort();
306ca1c9b0cSelric     UNREACHABLE(return 0);
307ca1c9b0cSelric }
308ca1c9b0cSelric 
309ca1c9b0cSelric /**
310ca1c9b0cSelric  * Log a warning to the log, default stderr, and then abort.
311ca1c9b0cSelric  *
312ca1c9b0cSelric  * @param context A Kerberos 5 context
313b9d004c6Schristos  * @param fmt printf format string of message to print
314b9d004c6Schristos  * @param ... arguments for format string
315ca1c9b0cSelric  *
316ca1c9b0cSelric  * @ingroup krb5_error
317ca1c9b0cSelric  */
318ca1c9b0cSelric 
319ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_abortx(krb5_context context,const char * fmt,...)320ca1c9b0cSelric krb5_abortx(krb5_context context, const char *fmt, ...)
321d3273b5bSchristos      __attribute__ ((__noreturn__, __format__ (__printf__, 2, 3)))
322ca1c9b0cSelric {
323b9d004c6Schristos     FUNC_NORET(0, 0, 0);
324ca1c9b0cSelric     abort();
325ca1c9b0cSelric     UNREACHABLE(return 0);
326ca1c9b0cSelric }
327ca1c9b0cSelric 
328ca1c9b0cSelric /**
329ca1c9b0cSelric  * Set the default logging facility.
330ca1c9b0cSelric  *
331ca1c9b0cSelric  * @param context A Kerberos 5 context
332ca1c9b0cSelric  * @param fac Facility to use for logging.
333ca1c9b0cSelric  *
334ca1c9b0cSelric  * @ingroup krb5_error
335ca1c9b0cSelric  */
336ca1c9b0cSelric 
337ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_set_warn_dest(krb5_context context,krb5_log_facility * fac)338ca1c9b0cSelric krb5_set_warn_dest(krb5_context context, krb5_log_facility *fac)
339ca1c9b0cSelric {
340ca1c9b0cSelric     context->warn_dest = fac;
341ca1c9b0cSelric     return 0;
342ca1c9b0cSelric }
343ca1c9b0cSelric 
344ca1c9b0cSelric /**
345ca1c9b0cSelric  * Get the default logging facility.
346ca1c9b0cSelric  *
347ca1c9b0cSelric  * @param context A Kerberos 5 context
348ca1c9b0cSelric  *
349ca1c9b0cSelric  * @ingroup krb5_error
350ca1c9b0cSelric  */
351ca1c9b0cSelric 
352ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_log_facility * KRB5_LIB_CALL
krb5_get_warn_dest(krb5_context context)353ca1c9b0cSelric krb5_get_warn_dest(krb5_context context)
354ca1c9b0cSelric {
355ca1c9b0cSelric     return context->warn_dest;
356ca1c9b0cSelric }
357