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