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