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