1 /* $NetBSD: display_status.c,v 1.3 2023/06/19 21:41:43 christos Exp $ */
2
3 /*
4 * Copyright (c) 1998 - 2006 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include "gsskrb5_locl.h"
37
38 static const char *
calling_error(OM_uint32 v)39 calling_error(OM_uint32 v)
40 {
41 static const char *msgs[] = {
42 NULL, /* 0 */
43 "A required input parameter could not be read.", /* */
44 "A required output parameter could not be written.", /* */
45 "A parameter was malformed"
46 };
47
48 v >>= GSS_C_CALLING_ERROR_OFFSET;
49
50 if (v == 0)
51 return "";
52 else if (v >= sizeof(msgs)/sizeof(*msgs))
53 return "unknown calling error";
54 else
55 return msgs[v];
56 }
57
58 static const char *
routine_error(OM_uint32 v)59 routine_error(OM_uint32 v)
60 {
61 static const char *msgs[] = {
62 NULL, /* 0 */
63 "An unsupported mechanism was requested",
64 "An invalid name was supplied",
65 "A supplied name was of an unsupported type",
66 "Incorrect channel bindings were supplied",
67 "An invalid status code was supplied",
68 "A token had an invalid MIC",
69 "No credentials were supplied, or the credentials were unavailable or inaccessible.",
70 "No context has been established",
71 "A token was invalid",
72 "A credential was invalid",
73 "The referenced credentials have expired",
74 "The context has expired",
75 "Miscellaneous failure (see text)",
76 "The quality-of-protection requested could not be provide",
77 "The operation is forbidden by local security policy",
78 "The operation or option is not available",
79 "The requested credential element already exists",
80 "The provided name was not a mechanism name.",
81 };
82
83 v >>= GSS_C_ROUTINE_ERROR_OFFSET;
84
85 if (v == 0)
86 return "";
87 else if (v >= sizeof(msgs)/sizeof(*msgs))
88 return "unknown routine error";
89 else
90 return msgs[v];
91 }
92
93 static const char *
supplementary_error(OM_uint32 v)94 supplementary_error(OM_uint32 v)
95 {
96 static const char *msgs[] = {
97 "normal completion",
98 "continuation call to routine required",
99 "duplicate per-message token detected",
100 "timed-out per-message token detected",
101 "reordered (early) per-message token detected",
102 "skipped predecessor token(s) detected"
103 };
104
105 v >>= GSS_C_SUPPLEMENTARY_OFFSET;
106
107 if (v >= sizeof(msgs)/sizeof(*msgs))
108 return "unknown routine error";
109 else
110 return msgs[v];
111 }
112
113 void
_gsskrb5_clear_status(void)114 _gsskrb5_clear_status (void)
115 {
116 krb5_context context;
117
118 if (_gsskrb5_init (&context) != 0)
119 return;
120 krb5_clear_error_message(context);
121 }
122
123 void
_gsskrb5_set_status(int ret,const char * fmt,...)124 _gsskrb5_set_status (int ret, const char *fmt, ...)
125 {
126 krb5_context context;
127 va_list args;
128 char *str;
129 int e;
130
131 if (_gsskrb5_init (&context) != 0)
132 return;
133
134 va_start(args, fmt);
135 e = vasprintf(&str, fmt, args);
136 va_end(args);
137 if (e >= 0 && str) {
138 krb5_set_error_message(context, ret, "%s", str);
139 free(str);
140 }
141 }
142
_gsskrb5_display_status(OM_uint32 * minor_status,OM_uint32 status_value,int status_type,const gss_OID mech_type,OM_uint32 * message_context,gss_buffer_t status_string)143 OM_uint32 GSSAPI_CALLCONV _gsskrb5_display_status
144 (OM_uint32 *minor_status,
145 OM_uint32 status_value,
146 int status_type,
147 const gss_OID mech_type,
148 OM_uint32 *message_context,
149 gss_buffer_t status_string)
150 {
151 krb5_context context;
152 char *buf = NULL;
153 int e = 0;
154
155 GSSAPI_KRB5_INIT (&context);
156
157 status_string->length = 0;
158 status_string->value = NULL;
159
160 if (gss_oid_equal(mech_type, GSS_C_NO_OID) == 0 &&
161 gss_oid_equal(mech_type, GSS_KRB5_MECHANISM) == 0) {
162 *minor_status = 0;
163 return GSS_C_GSS_CODE;
164 }
165
166 if (status_type == GSS_C_GSS_CODE) {
167 if (GSS_SUPPLEMENTARY_INFO(status_value))
168 e = asprintf(&buf, "%s",
169 supplementary_error(GSS_SUPPLEMENTARY_INFO(status_value)));
170 else
171 e = asprintf (&buf, "%s %s",
172 calling_error(GSS_CALLING_ERROR(status_value)),
173 routine_error(GSS_ROUTINE_ERROR(status_value)));
174 } else if (status_type == GSS_C_MECH_CODE) {
175 const char *buf2 = krb5_get_error_message(context, status_value);
176 if (buf2) {
177 buf = strdup(buf2);
178 krb5_free_error_message(context, buf2);
179 } else {
180 e = asprintf(&buf, "unknown mech error-code %u",
181 (unsigned)status_value);
182 }
183 } else {
184 *minor_status = EINVAL;
185 return GSS_S_BAD_STATUS;
186 }
187
188 if (e < 0 || buf == NULL) {
189 *minor_status = ENOMEM;
190 return GSS_S_FAILURE;
191 }
192
193 *message_context = 0;
194 *minor_status = 0;
195
196 status_string->length = strlen(buf);
197 status_string->value = buf;
198
199 return GSS_S_COMPLETE;
200 }
201