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