1226031Sstas/*-
2226031Sstas * Copyright (c) 2005 Doug Rabson
3226031Sstas * All rights reserved.
4226031Sstas *
5226031Sstas * Redistribution and use in source and binary forms, with or without
6226031Sstas * modification, are permitted provided that the following conditions
7226031Sstas * are met:
8226031Sstas * 1. Redistributions of source code must retain the above copyright
9226031Sstas *    notice, this list of conditions and the following disclaimer.
10226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer in the
12226031Sstas *    documentation and/or other materials provided with the distribution.
13226031Sstas *
14226031Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24226031Sstas * SUCH DAMAGE.
25226031Sstas *
26226031Sstas *	$FreeBSD: src/lib/libgssapi/gss_display_status.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
27226031Sstas */
28226031Sstas/*
29226031Sstas * Copyright (c) 1998 - 2005 Kungliga Tekniska H��gskolan
30226031Sstas * (Royal Institute of Technology, Stockholm, Sweden).
31226031Sstas * All rights reserved.
32226031Sstas *
33226031Sstas * Redistribution and use in source and binary forms, with or without
34226031Sstas * modification, are permitted provided that the following conditions
35226031Sstas * are met:
36226031Sstas *
37226031Sstas * 1. Redistributions of source code must retain the above copyright
38226031Sstas *    notice, this list of conditions and the following disclaimer.
39226031Sstas *
40226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
41226031Sstas *    notice, this list of conditions and the following disclaimer in the
42226031Sstas *    documentation and/or other materials provided with the distribution.
43226031Sstas *
44226031Sstas * 3. Neither the name of the Institute nor the names of its contributors
45226031Sstas *    may be used to endorse or promote products derived from this software
46226031Sstas *    without specific prior written permission.
47226031Sstas *
48226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
49226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
52226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58226031Sstas * SUCH DAMAGE.
59226031Sstas */
60226031Sstas
61226031Sstas#include "mech_locl.h"
62226031Sstas
63226031Sstasstatic const char *
64226031Sstascalling_error(OM_uint32 v)
65226031Sstas{
66226031Sstas    static const char *msgs[] = {
67226031Sstas	NULL,			/* 0 */
68226031Sstas	"A required input parameter could not be read.", /*  */
69226031Sstas	"A required output parameter could not be written.", /*  */
70226031Sstas	"A parameter was malformed"
71226031Sstas    };
72226031Sstas
73226031Sstas    v >>= GSS_C_CALLING_ERROR_OFFSET;
74226031Sstas
75226031Sstas    if (v == 0)
76226031Sstas	return "";
77226031Sstas    else if (v >= sizeof(msgs)/sizeof(*msgs))
78226031Sstas	return "unknown calling error";
79226031Sstas    else
80226031Sstas	return msgs[v];
81226031Sstas}
82226031Sstas
83226031Sstasstatic const char *
84226031Sstasroutine_error(OM_uint32 v)
85226031Sstas{
86226031Sstas    static const char *msgs[] = {
87226031Sstas	"Function completed successfully",			/* 0 */
88226031Sstas	"An unsupported mechanism was requested",
89226031Sstas	"An invalid name was supplied",
90226031Sstas	"A supplied name was of an unsupported type",
91226031Sstas	"Incorrect channel bindings were supplied",
92226031Sstas	"An invalid status code was supplied",
93226031Sstas	"A token had an invalid MIC",
94226031Sstas	"No credentials were supplied, "
95226031Sstas	"or the credentials were unavailable or inaccessible.",
96226031Sstas	"No context has been established",
97226031Sstas	"A token was invalid",
98226031Sstas	"A credential was invalid",
99226031Sstas	"The referenced credentials have expired",
100226031Sstas	"The context has expired",
101226031Sstas	"Miscellaneous failure (see text)",
102226031Sstas	"The quality-of-protection requested could not be provide",
103226031Sstas	"The operation is forbidden by local security policy",
104226031Sstas	"The operation or option is not available",
105226031Sstas	"The requested credential element already exists",
106226031Sstas	"The provided name was not a mechanism name.",
107226031Sstas    };
108226031Sstas
109226031Sstas    v >>= GSS_C_ROUTINE_ERROR_OFFSET;
110226031Sstas
111226031Sstas    if (v >= sizeof(msgs)/sizeof(*msgs))
112226031Sstas	return "unknown routine error";
113226031Sstas    else
114226031Sstas	return msgs[v];
115226031Sstas}
116226031Sstas
117226031Sstasstatic const char *
118226031Sstassupplementary_error(OM_uint32 v)
119226031Sstas{
120226031Sstas    static const char *msgs[] = {
121226031Sstas	"normal completion",
122226031Sstas	"continuation call to routine required",
123226031Sstas	"duplicate per-message token detected",
124226031Sstas	"timed-out per-message token detected",
125226031Sstas	"reordered (early) per-message token detected",
126226031Sstas	"skipped predecessor token(s) detected"
127226031Sstas    };
128226031Sstas
129226031Sstas    v >>= GSS_C_SUPPLEMENTARY_OFFSET;
130226031Sstas
131226031Sstas    if (v >= sizeof(msgs)/sizeof(*msgs))
132226031Sstas	return "unknown routine error";
133226031Sstas    else
134226031Sstas	return msgs[v];
135226031Sstas}
136226031Sstas
137226031Sstas
138226031SstasGSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
139226031Sstasgss_display_status(OM_uint32 *minor_status,
140226031Sstas    OM_uint32 status_value,
141226031Sstas    int status_type,
142226031Sstas    const gss_OID mech_type,
143226031Sstas    OM_uint32 *message_content,
144226031Sstas    gss_buffer_t status_string)
145226031Sstas{
146226031Sstas	OM_uint32 major_status;
147226031Sstas
148226031Sstas	_mg_buffer_zero(status_string);
149226031Sstas	*message_content = 0;
150226031Sstas
151226031Sstas	major_status = _gss_mg_get_error(mech_type, status_type,
152226031Sstas					 status_value, status_string);
153226031Sstas	if (major_status == GSS_S_COMPLETE) {
154226031Sstas
155226031Sstas	    *message_content = 0;
156226031Sstas	    *minor_status = 0;
157226031Sstas	    return GSS_S_COMPLETE;
158226031Sstas	}
159226031Sstas
160226031Sstas	*minor_status = 0;
161226031Sstas	switch (status_type) {
162226031Sstas	case GSS_C_GSS_CODE: {
163226031Sstas	    	char *buf = NULL;
164226031Sstas		int e;
165226031Sstas
166226031Sstas		if (GSS_SUPPLEMENTARY_INFO(status_value))
167226031Sstas		    e = asprintf(&buf, "%s", supplementary_error(
168226031Sstas		        GSS_SUPPLEMENTARY_INFO(status_value)));
169226031Sstas		else
170226031Sstas		    e = asprintf (&buf, "%s %s",
171226031Sstas		        calling_error(GSS_CALLING_ERROR(status_value)),
172226031Sstas			routine_error(GSS_ROUTINE_ERROR(status_value)));
173226031Sstas
174226031Sstas		if (e < 0 || buf == NULL)
175226031Sstas		    break;
176226031Sstas
177226031Sstas		status_string->length = strlen(buf);
178226031Sstas		status_string->value  = buf;
179226031Sstas
180226031Sstas		return GSS_S_COMPLETE;
181226031Sstas	}
182226031Sstas	case GSS_C_MECH_CODE: {
183226031Sstas		OM_uint32 maj_junk, min_junk;
184226031Sstas		gss_buffer_desc oid;
185226031Sstas		char *buf = NULL;
186226031Sstas		int e;
187226031Sstas
188226031Sstas		maj_junk = gss_oid_to_str(&min_junk, mech_type, &oid);
189226031Sstas		if (maj_junk != GSS_S_COMPLETE) {
190226031Sstas		    oid.value = rk_UNCONST("unknown");
191226031Sstas		    oid.length = 7;
192226031Sstas		}
193226031Sstas
194226031Sstas		e = asprintf (&buf, "unknown mech-code %lu for mech %.*s",
195226031Sstas			  (unsigned long)status_value,
196226031Sstas			  (int)oid.length, (char *)oid.value);
197226031Sstas		if (maj_junk == GSS_S_COMPLETE)
198226031Sstas		    gss_release_buffer(&min_junk, &oid);
199226031Sstas
200226031Sstas		if (e < 0 || buf == NULL)
201226031Sstas		    break;
202226031Sstas
203226031Sstas		status_string->length = strlen(buf);
204226031Sstas		status_string->value  = buf;
205226031Sstas
206226031Sstas		return GSS_S_COMPLETE;
207226031Sstas	}
208226031Sstas	}
209226031Sstas	_mg_buffer_zero(status_string);
210226031Sstas	return (GSS_S_BAD_STATUS);
211226031Sstas}
212