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