xref: /freebsd/crypto/heimdal/lib/krb5/warn.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
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 RCSID("$Id: warn.c 19086 2006-11-21 08:06:40Z lha $");
38 
39 static krb5_error_code _warnerr(krb5_context context, int do_errtext,
40 	 krb5_error_code code, int level, const char *fmt, va_list ap)
41 	__attribute__((__format__(__printf__, 5, 0)));
42 
43 static krb5_error_code
44 _warnerr(krb5_context context, int do_errtext,
45 	 krb5_error_code code, int level, const char *fmt, va_list ap)
46 {
47     char xfmt[7] = "";
48     const char *args[2], **arg;
49     char *msg = NULL;
50     char *err_str = NULL;
51 
52     args[0] = args[1] = NULL;
53     arg = args;
54     if(fmt){
55 	strlcat(xfmt, "%s", sizeof(xfmt));
56 	if(do_errtext)
57 	    strlcat(xfmt, ": ", sizeof(xfmt));
58 	vasprintf(&msg, fmt, ap);
59 	if(msg == NULL)
60 	    return ENOMEM;
61 	*arg++ = msg;
62     }
63     if(context && do_errtext){
64 	const char *err_msg;
65 
66 	strlcat(xfmt, "%s", sizeof(xfmt));
67 
68 	err_str = krb5_get_error_string(context);
69 	if (err_str != NULL) {
70 	    *arg++ = err_str;
71 	} else {
72 	    err_msg = krb5_get_err_text(context, code);
73 	    if (err_msg)
74 		*arg++ = err_msg;
75 	    else
76 		*arg++ = "<unknown error>";
77 	}
78     }
79 
80     if(context && context->warn_dest)
81 	krb5_log(context, context->warn_dest, level, xfmt, args[0], args[1]);
82     else
83 	warnx(xfmt, args[0], args[1]);
84     free(msg);
85     free(err_str);
86     return 0;
87 }
88 
89 #define FUNC(ETEXT, CODE, LEVEL)					\
90     krb5_error_code ret;						\
91     va_list ap;								\
92     va_start(ap, fmt);							\
93     ret = _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap); 		\
94     va_end(ap);
95 
96 #undef __attribute__
97 #define __attribute__(X)
98 
99 krb5_error_code KRB5_LIB_FUNCTION
100 krb5_vwarn(krb5_context context, krb5_error_code code,
101 	   const char *fmt, va_list ap)
102      __attribute__ ((format (printf, 3, 0)))
103 {
104     return _warnerr(context, 1, code, 1, fmt, ap);
105 }
106 
107 
108 krb5_error_code KRB5_LIB_FUNCTION
109 krb5_warn(krb5_context context, krb5_error_code code, const char *fmt, ...)
110      __attribute__ ((format (printf, 3, 4)))
111 {
112     FUNC(1, code, 1);
113     return ret;
114 }
115 
116 krb5_error_code KRB5_LIB_FUNCTION
117 krb5_vwarnx(krb5_context context, const char *fmt, va_list ap)
118      __attribute__ ((format (printf, 2, 0)))
119 {
120     return _warnerr(context, 0, 0, 1, fmt, ap);
121 }
122 
123 krb5_error_code KRB5_LIB_FUNCTION
124 krb5_warnx(krb5_context context, const char *fmt, ...)
125      __attribute__ ((format (printf, 2, 3)))
126 {
127     FUNC(0, 0, 1);
128     return ret;
129 }
130 
131 krb5_error_code KRB5_LIB_FUNCTION
132 krb5_verr(krb5_context context, int eval, krb5_error_code code,
133 	  const char *fmt, va_list ap)
134      __attribute__ ((noreturn, format (printf, 4, 0)))
135 {
136     _warnerr(context, 1, code, 0, fmt, ap);
137     exit(eval);
138 }
139 
140 
141 krb5_error_code KRB5_LIB_FUNCTION
142 krb5_err(krb5_context context, int eval, krb5_error_code code,
143 	 const char *fmt, ...)
144      __attribute__ ((noreturn, format (printf, 4, 5)))
145 {
146     FUNC(1, code, 0);
147     exit(eval);
148 }
149 
150 krb5_error_code KRB5_LIB_FUNCTION
151 krb5_verrx(krb5_context context, int eval, const char *fmt, va_list ap)
152      __attribute__ ((noreturn, format (printf, 3, 0)))
153 {
154     _warnerr(context, 0, 0, 0, fmt, ap);
155     exit(eval);
156 }
157 
158 krb5_error_code KRB5_LIB_FUNCTION
159 krb5_errx(krb5_context context, int eval, const char *fmt, ...)
160      __attribute__ ((noreturn, format (printf, 3, 4)))
161 {
162     FUNC(0, 0, 0);
163     exit(eval);
164 }
165 
166 krb5_error_code KRB5_LIB_FUNCTION
167 krb5_vabort(krb5_context context, krb5_error_code code,
168 	    const char *fmt, va_list ap)
169      __attribute__ ((noreturn, format (printf, 3, 0)))
170 {
171     _warnerr(context, 1, code, 0, fmt, ap);
172     abort();
173 }
174 
175 
176 krb5_error_code KRB5_LIB_FUNCTION
177 krb5_abort(krb5_context context, krb5_error_code code, const char *fmt, ...)
178      __attribute__ ((noreturn, format (printf, 3, 4)))
179 {
180     FUNC(1, code, 0);
181     abort();
182 }
183 
184 krb5_error_code KRB5_LIB_FUNCTION
185 krb5_vabortx(krb5_context context, const char *fmt, va_list ap)
186      __attribute__ ((noreturn, format (printf, 2, 0)))
187 {
188     _warnerr(context, 0, 0, 0, fmt, ap);
189     abort();
190 }
191 
192 krb5_error_code KRB5_LIB_FUNCTION
193 krb5_abortx(krb5_context context, const char *fmt, ...)
194      __attribute__ ((noreturn, format (printf, 2, 3)))
195 {
196     FUNC(0, 0, 0);
197     abort();
198 }
199 
200 krb5_error_code KRB5_LIB_FUNCTION
201 krb5_set_warn_dest(krb5_context context, krb5_log_facility *fac)
202 {
203     context->warn_dest = fac;
204     return 0;
205 }
206 
207 krb5_log_facility * KRB5_LIB_FUNCTION
208 krb5_get_warn_dest(krb5_context context)
209 {
210     return context->warn_dest;
211 }
212