1 /*	$NetBSD: display_status.c,v 1.1.1.1 2011/04/13 18:14:45 elric 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, "
70 	"or the credentials were unavailable or inaccessible.",
71 	"No context has been established",
72 	"A token was invalid",
73 	"A credential was invalid",
74 	"The referenced credentials have expired",
75 	"The context has expired",
76 	"Miscellaneous failure (see text)",
77 	"The quality-of-protection requested could not be provide",
78 	"The operation is forbidden by local security policy",
79 	"The operation or option is not available",
80 	"The requested credential element already exists",
81 	"The provided name was not a mechanism name.",
82     };
83 
84     v >>= GSS_C_ROUTINE_ERROR_OFFSET;
85 
86     if (v == 0)
87 	return "";
88     else if (v >= sizeof(msgs)/sizeof(*msgs))
89 	return "unknown routine error";
90     else
91 	return msgs[v];
92 }
93 
94 static const char *
supplementary_error(OM_uint32 v)95 supplementary_error(OM_uint32 v)
96 {
97     static const char *msgs[] = {
98 	"normal completion",
99 	"continuation call to routine required",
100 	"duplicate per-message token detected",
101 	"timed-out per-message token detected",
102 	"reordered (early) per-message token detected",
103 	"skipped predecessor token(s) detected"
104     };
105 
106     v >>= GSS_C_SUPPLEMENTARY_OFFSET;
107 
108     if (v >= sizeof(msgs)/sizeof(*msgs))
109 	return "unknown routine error";
110     else
111 	return msgs[v];
112 }
113 
114 void
_gsskrb5_clear_status(void)115 _gsskrb5_clear_status (void)
116 {
117     krb5_context context;
118 
119     if (_gsskrb5_init (&context) != 0)
120 	return;
121     krb5_clear_error_message(context);
122 }
123 
124 void
_gsskrb5_set_status(int ret,const char * fmt,...)125 _gsskrb5_set_status (int ret, const char *fmt, ...)
126 {
127     krb5_context context;
128     va_list args;
129     char *str;
130     int e;
131 
132     if (_gsskrb5_init (&context) != 0)
133 	return;
134 
135     va_start(args, fmt);
136     e = vasprintf(&str, fmt, args);
137     va_end(args);
138     if (e >= 0 && str) {
139 	krb5_set_error_message(context, ret, "%s", str);
140 	free(str);
141     }
142 }
143 
_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)144 OM_uint32 GSSAPI_CALLCONV _gsskrb5_display_status
145 (OM_uint32		*minor_status,
146  OM_uint32		 status_value,
147  int			 status_type,
148  const gss_OID	 mech_type,
149  OM_uint32		*message_context,
150  gss_buffer_t	 status_string)
151 {
152     krb5_context context;
153     char *buf = NULL;
154     int e = 0;
155 
156     GSSAPI_KRB5_INIT (&context);
157 
158     status_string->length = 0;
159     status_string->value = NULL;
160 
161     if (gss_oid_equal(mech_type, GSS_C_NO_OID) == 0 &&
162 	gss_oid_equal(mech_type, GSS_KRB5_MECHANISM) == 0) {
163 	*minor_status = 0;
164 	return GSS_C_GSS_CODE;
165     }
166 
167     if (status_type == GSS_C_GSS_CODE) {
168 	if (GSS_SUPPLEMENTARY_INFO(status_value))
169 	    e = asprintf(&buf, "%s",
170 			 supplementary_error(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     } else if (status_type == GSS_C_MECH_CODE) {
176 	const char *buf2 = krb5_get_error_message(context, status_value);
177 	if (buf2) {
178 	    buf = strdup(buf2);
179 	    krb5_free_error_message(context, buf2);
180 	} else {
181 	    e = asprintf(&buf, "unknown mech error-code %u",
182 			 (unsigned)status_value);
183 	}
184     } else {
185 	*minor_status = EINVAL;
186 	return GSS_S_BAD_STATUS;
187     }
188 
189     if (e < 0 || buf == NULL) {
190 	*minor_status = ENOMEM;
191 	return GSS_S_FAILURE;
192     }
193 
194     *message_context = 0;
195     *minor_status = 0;
196 
197     status_string->length = strlen(buf);
198     status_string->value  = buf;
199 
200     return GSS_S_COMPLETE;
201 }
202