1/*
2 * Copyright (c) 1998 - 2006 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 "gsskrb5_locl.h"
35
36#undef HEIMDAL_PRINTF_ATTRIBUTE
37#define HEIMDAL_PRINTF_ATTRIBUTE(x)
38
39static const char *
40calling_error(OM_uint32 v)
41{
42    static const char *msgs[] = {
43	NULL,			/* 0 */
44	"A required input parameter could not be read.", /*  */
45	"A required output parameter could not be written.", /*  */
46	"A parameter was malformed"
47    };
48
49    v >>= GSS_C_CALLING_ERROR_OFFSET;
50
51    if (v == 0)
52	return "";
53    else if (v >= sizeof(msgs)/sizeof(*msgs))
54	return "unknown calling error";
55    else
56	return msgs[v];
57}
58
59static const char *
60routine_error(OM_uint32 v)
61{
62    static const char *msgs[] = {
63	NULL,			/* 0 */
64	"An unsupported mechanism was requested",
65	"An invalid name was supplied",
66	"A supplied name was of an unsupported type",
67	"Incorrect channel bindings were supplied",
68	"An invalid status code was supplied",
69	"A token had an invalid MIC",
70	"No credentials were supplied, "
71	"or the credentials were unavailable or inaccessible.",
72	"No context has been established",
73	"A token was invalid",
74	"A credential was invalid",
75	"The referenced credentials have expired",
76	"The context has expired",
77	"Miscellaneous failure (see text)",
78	"The quality-of-protection requested could not be provide",
79	"The operation is forbidden by local security policy",
80	"The operation or option is not available",
81	"The requested credential element already exists",
82	"The provided name was not a mechanism name.",
83    };
84
85    v >>= GSS_C_ROUTINE_ERROR_OFFSET;
86
87    if (v == 0)
88	return "";
89    else if (v >= sizeof(msgs)/sizeof(*msgs))
90	return "unknown routine error";
91    else
92	return msgs[v];
93}
94
95static const char *
96supplementary_error(OM_uint32 v)
97{
98    static const char *msgs[] = {
99	"normal completion",
100	"continuation call to routine required",
101	"duplicate per-message token detected",
102	"timed-out per-message token detected",
103	"reordered (early) per-message token detected",
104	"skipped predecessor token(s) detected"
105    };
106
107    v >>= GSS_C_SUPPLEMENTARY_OFFSET;
108
109    if (v >= sizeof(msgs)/sizeof(*msgs))
110	return "unknown routine error";
111    else
112	return msgs[v];
113}
114
115void
116_gsskrb5_clear_status (void)
117{
118    krb5_context context;
119
120    if (_gsskrb5_init (&context) != 0)
121	return;
122    krb5_clear_error_message(context);
123}
124
125void
126_gsskrb5_set_status (int ret, const char *fmt, ...)
127    HEIMDAL_PRINTF_ATTRIBUTE((printf, 2, 3))
128{
129    krb5_context context;
130    va_list args;
131    char *str;
132    int e;
133
134    if (_gsskrb5_init (&context) != 0)
135	return;
136
137    va_start(args, fmt);
138    e = vasprintf(&str, fmt, args);
139    va_end(args);
140    if (e >= 0 && str) {
141	krb5_set_error_message(context, ret, "%s", str);
142	free(str);
143    }
144}
145
146OM_uint32 GSSAPI_CALLCONV _gsskrb5_display_status
147(OM_uint32		*minor_status,
148 OM_uint32		 status_value,
149 int			 status_type,
150 const gss_OID	 mech_type,
151 OM_uint32		*message_context,
152 gss_buffer_t	 status_string)
153{
154    krb5_context context;
155    char *buf = NULL;
156    int e = 0;
157
158    GSSAPI_KRB5_INIT (&context);
159
160    status_string->length = 0;
161    status_string->value = NULL;
162
163    if (gss_oid_equal(mech_type, GSS_C_NO_OID) == 0 &&
164	gss_oid_equal(mech_type, GSS_KRB5_MECHANISM) == 0) {
165	*minor_status = 0;
166	return GSS_C_GSS_CODE;
167    }
168
169    if (status_type == GSS_C_GSS_CODE) {
170	if (GSS_SUPPLEMENTARY_INFO(status_value))
171	    e = asprintf(&buf, "%s",
172			 supplementary_error(GSS_SUPPLEMENTARY_INFO(status_value)));
173	else
174	    e = asprintf (&buf, "%s %s",
175			  calling_error(GSS_CALLING_ERROR(status_value)),
176			  routine_error(GSS_ROUTINE_ERROR(status_value)));
177    } else if (status_type == GSS_C_MECH_CODE) {
178	const char *buf2 = krb5_get_error_message(context, status_value);
179	if (buf2) {
180	    buf = strdup(buf2);
181	    krb5_free_error_message(context, buf2);
182	} else {
183	    e = asprintf(&buf, "unknown mech error-code %u",
184			 (unsigned)status_value);
185	}
186    } else {
187	*minor_status = EINVAL;
188	return GSS_S_BAD_STATUS;
189    }
190
191    if (e < 0 || buf == NULL) {
192	*minor_status = ENOMEM;
193	return GSS_S_FAILURE;
194    }
195
196    *message_context = 0;
197    *minor_status = 0;
198
199    status_string->length = strlen(buf);
200    status_string->value  = buf;
201
202    return GSS_S_COMPLETE;
203}
204