s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / third_party / heimdal / lib / base / error_string.c
1 /*
2  * Copyright (c) 2001, 2003, 2005 - 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 #include "baselocl.h"
35
36 #undef __attribute__
37 #define __attribute__(x)
38
39 void
40 heim_clear_error_message(heim_context context)
41 {
42     if (context->error_string)
43         free(context->error_string);
44     context->error_code = 0;
45     context->error_string = NULL;
46 }
47
48 void
49 heim_set_error_message(heim_context context, heim_error_code ret,
50                        const char *fmt, ...)
51     __attribute__ ((__format__ (__printf__, 3, 4)))
52 {
53     va_list ap;
54
55     va_start(ap, fmt);
56     heim_vset_error_message(context, ret, fmt, ap);
57     va_end(ap);
58 }
59
60 void
61 heim_vset_error_message(heim_context context, heim_error_code ret,
62                         const char *fmt, va_list args)
63     __attribute__ ((__format__ (__printf__, 3, 0)))
64 {
65     int r;
66
67     if (context == NULL)
68         return;
69     if (context->error_string) {
70         free(context->error_string);
71         context->error_string = NULL;
72     }
73     context->error_code = ret;
74     r = vasprintf(&context->error_string, fmt, args);
75     if (r < 0)
76         context->error_string = NULL;
77     if (context->error_string)
78         heim_debug(context, 200, "error message: %s: %d", context->error_string, ret);
79 }
80
81 void
82 heim_prepend_error_message(heim_context context, heim_error_code ret,
83                            const char *fmt, ...)
84     __attribute__ ((__format__ (__printf__, 3, 4)))
85 {
86     va_list ap;
87
88     va_start(ap, fmt);
89     heim_vprepend_error_message(context, ret, fmt, ap);
90     va_end(ap);
91 }
92
93 void
94 heim_vprepend_error_message(heim_context context, heim_error_code ret,
95                             const char *fmt, va_list args)
96     __attribute__ ((__format__ (__printf__, 3, 0)))
97 {
98     char *str = NULL, *str2 = NULL;
99
100     if (context == NULL || context->error_code != ret ||
101         vasprintf(&str, fmt, args) < 0 || str == NULL)
102         return;
103     if (context->error_string) {
104         int e;
105
106         e = asprintf(&str2, "%s: %s", str, context->error_string);
107         free(context->error_string);
108         if (e < 0 || str2 == NULL)
109             context->error_string = NULL;
110         else
111             context->error_string = str2;
112         free(str);
113     } else
114         context->error_string = str;
115 }
116
117 const char *
118 heim_get_error_message(heim_context context, heim_error_code code)
119 {
120     const char *cstr = NULL;
121     char *str = NULL;
122     char buf[128];
123     int free_context = 0;
124
125     if (code == 0)
126         return strdup("Success");
127
128     /*
129      * The MIT version of this function ignores the krb5_context
130      * and several widely deployed applications call krb5_get_error_message()
131      * with a NULL context in order to translate an error code as a
132      * replacement for error_message().  Another reason a NULL context
133      * might be provided is if the krb5_init_context() call itself
134      * failed.
135      */
136     if (context &&
137         context->error_string &&
138         (code == context->error_code || context->error_code == 0) &&
139         (cstr = strdup(context->error_string)))
140         return cstr;
141
142     if (context == NULL && (context = heim_context_init()))
143         free_context = 1;
144     if (context)
145         cstr = com_right_r(context->et_list, code, buf, sizeof(buf));
146     if (free_context)
147         heim_context_free(&context);
148
149     if (cstr || (cstr = error_message(code)))
150         return strdup(cstr);
151     if (asprintf(&str, "<unknown error: %d>", (int)code) == -1 || str == NULL)
152         return NULL;
153     return str;
154 }
155
156 const char *
157 heim_get_error_string(heim_context context)
158 {
159     if (context && context->error_string)
160         return strdup(context->error_string);
161     return NULL;
162 }
163
164 int
165 heim_have_error_string(heim_context context)
166 {
167     return context->error_string != NULL;
168 }
169
170 void
171 heim_free_error_message(heim_context context, const char *msg)
172 {
173     free(rk_UNCONST(msg));
174 }