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