s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / source4 / heimdal / lib / base / warn.c
1 /*
2  * Copyright (c) 1997 - 2020 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 #if defined(_MSC_VER)
35 # pragma warning(disable: 4646)
36 # pragma warning(disable: 4716)
37 #endif
38
39 #include "baselocl.h"
40 #include <err.h>
41
42 static heim_error_code _warnerr(heim_context context, int do_errtext,
43          heim_error_code code, int level, const char *fmt, va_list ap)
44         __attribute__ ((__format__ (__printf__, 5, 0)));
45
46 static heim_error_code
47 _warnerr(heim_context context, int do_errtext,
48          heim_error_code code, int level, const char *fmt, va_list ap)
49 {
50     char xfmt[7] = "";
51     const char *args[2], **arg;
52     char *msg = NULL;
53     const char *err_str = NULL;
54     heim_error_code ret;
55
56     args[0] = args[1] = NULL;
57     arg = args;
58     if(fmt){
59         strlcat(xfmt, "%s", sizeof(xfmt));
60         if(do_errtext)
61             strlcat(xfmt, ": ", sizeof(xfmt));
62         ret = vasprintf(&msg, fmt, ap);
63         if(ret < 0 || msg == NULL)
64             return ENOMEM;
65         *arg++ = msg;
66     }
67     if (context && do_errtext) {
68         strlcat(xfmt, "%s", sizeof(xfmt));
69
70         err_str = heim_get_error_message(context, code);
71         if (err_str != NULL) {
72             *arg = err_str;
73         } else {
74             *arg= "<unknown error>";
75         }
76     }
77
78     if (context && heim_get_warn_dest(context))
79         heim_log(context, heim_get_warn_dest(context), level, xfmt, args[0],
80                  args[1]);
81     else
82         warnx(xfmt, args[0], args[1]);
83     free(msg);
84     heim_free_error_message(context, err_str);
85     return 0;
86 }
87
88 #define FUNC(ETEXT, CODE, LEVEL)                                        \
89     heim_error_code ret;                                                \
90     va_list ap;                                                         \
91     va_start(ap, fmt);                                                  \
92     ret = _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap);               \
93     va_end(ap);
94
95 #undef __attribute__
96 #define __attribute__(X)
97
98 /**
99  * Log a warning to the log, default stderr, include the error from
100  * the last failure.
101  *
102  * @param context A Kerberos 5 context.
103  * @param code error code of the last error
104  * @param fmt message to print
105  * @param ap arguments
106  *
107  * @ingroup heim_error
108  */
109
110 heim_error_code
111 heim_vwarn(heim_context context, heim_error_code code,
112            const char *fmt, va_list ap)
113      __attribute__ ((__format__ (__printf__, 3, 0)))
114 {
115     return _warnerr(context, 1, code, 1, fmt, ap);
116 }
117
118 /**
119  * Log a warning to the log, default stderr, include the error from
120  * the last failure.
121  *
122  * @param context A Kerberos 5 context.
123  * @param code error code of the last error
124  * @param fmt message to print
125  *
126  * @ingroup heim_error
127  */
128
129 heim_error_code
130 heim_warn(heim_context context, heim_error_code code, const char *fmt, ...)
131      __attribute__ ((__format__ (__printf__, 3, 4)))
132 {
133     FUNC(1, code, 1);
134     return ret;
135 }
136
137 /**
138  * Log a warning to the log, default stderr.
139  *
140  * @param context A Kerberos 5 context.
141  * @param fmt message to print
142  * @param ap arguments
143  *
144  * @ingroup heim_error
145  */
146
147 heim_error_code
148 heim_vwarnx(heim_context context, const char *fmt, va_list ap)
149      __attribute__ ((__format__ (__printf__, 2, 0)))
150 {
151     return _warnerr(context, 0, 0, 1, fmt, ap);
152 }
153
154 /**
155  * Log a warning to the log, default stderr.
156  *
157  * @param context A Kerberos 5 context.
158  * @param fmt message to print
159  *
160  * @ingroup heim_error
161  */
162
163 heim_error_code
164 heim_warnx(heim_context context, const char *fmt, ...)
165      __attribute__ ((__format__ (__printf__, 2, 3)))
166 {
167     FUNC(0, 0, 1);
168     return ret;
169 }