cleanup unused parameter warnings
[mech_eap.git] / display_status.c
1 /*
2  * Copyright (c) 2011, JANET(UK)
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  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Function for converting mechanism error codes to strings.
35  */
36
37 #include "gssapiP_eap.h"
38
39 static GSSEAP_THREAD_ONCE gssEapStatusInfoKeyOnce = GSSEAP_ONCE_INITIALIZER;
40 static GSSEAP_THREAD_KEY gssEapStatusInfoKey;
41
42 struct gss_eap_status_info {
43     OM_uint32 code;
44     char *message;
45     struct gss_eap_status_info *next;
46 };
47
48 static void
49 destroyStatusInfo(void *arg)
50 {
51     struct gss_eap_status_info *p = arg, *next;
52
53     for (p = arg; p != NULL; p = next) {
54         next = p->next;
55         GSSEAP_FREE(p->message);
56         GSSEAP_FREE(p);
57     }
58 }
59
60 static void
61 createStatusInfoKey(void)
62 {
63     GSSEAP_KEY_CREATE(&gssEapStatusInfoKey, destroyStatusInfo);
64 }
65
66 /*
67  * Associate a message with a mechanism (minor) status code. This function
68  * takes ownership of the message regardless of success. The message must
69  * be explicitly cleared, if required, so it is suggested that a specific
70  * minor code is either always or never associated with a message, to avoid
71  * dangling (and potentially confusing) error messages.
72  */
73 static void
74 saveStatusInfoNoCopy(OM_uint32 minor, char *message)
75 {
76     struct gss_eap_status_info **next = NULL, *p;
77
78     GSSEAP_ONCE(&gssEapStatusInfoKeyOnce, createStatusInfoKey);
79
80     p = GSSEAP_GETSPECIFIC(gssEapStatusInfoKey);
81     for (; p != NULL; p = p->next) {
82         if (p->code == minor) {
83             /* Set message in-place */
84             if (p->message != NULL)
85                 GSSEAP_FREE(p->message);
86             p->message = message;
87             return;
88         }
89         next = &p->next;
90     }
91
92     p = GSSEAP_CALLOC(1, sizeof(*p));
93     if (p == NULL) {
94         if (message != NULL)
95             GSSEAP_FREE(message);
96         return;
97     }
98
99     p->code = minor;
100     p->message = message;
101
102     if (next != NULL)
103         *next = p;
104     else
105         GSSEAP_SETSPECIFIC(gssEapStatusInfoKey, p);
106 }
107
108 static const char *
109 getStatusInfo(OM_uint32 minor)
110 {
111     struct gss_eap_status_info *p;
112
113     GSSEAP_ONCE(&gssEapStatusInfoKeyOnce, createStatusInfoKey);
114
115     for (p = GSSEAP_GETSPECIFIC(gssEapStatusInfoKey);
116          p != NULL;
117          p = p->next) {
118         if (p->code == minor)
119             return p->message;
120     }
121
122     return NULL;
123 }
124
125 void
126 gssEapSaveStatusInfo(OM_uint32 minor, const char *format, ...)
127 {
128     char *s;
129     int n;
130     va_list ap;
131
132     va_start(ap, format);
133     n = vasprintf(&s, format, ap);
134     va_end(ap);
135
136     if (n >= 0)
137         saveStatusInfoNoCopy(minor, s);
138 }
139
140 OM_uint32
141 gss_display_status(OM_uint32 *minor,
142                    OM_uint32 status_value,
143                    int status_type,
144                    gss_OID mech_type,
145                    OM_uint32 *message_context,
146                    gss_buffer_t status_string)
147 {
148     OM_uint32 major;
149     krb5_context krbContext = NULL;
150     const char *errMsg;
151
152     status_string->length = 0;
153     status_string->value = NULL;
154
155     if (!gssEapIsMechanismOid(mech_type)) {
156         *minor = GSSEAP_WRONG_MECH;
157         return GSS_S_BAD_MECH;
158     }
159
160     if (status_type != GSS_C_MECH_CODE ||
161         *message_context != 0) {
162         /* we rely on the mechglue for GSS_C_GSS_CODE */
163         *minor = 0;
164         return GSS_S_BAD_STATUS;
165     }
166
167     errMsg = getStatusInfo(status_value);
168     if (errMsg == NULL) {
169         GSSEAP_KRB_INIT(&krbContext);
170
171         /* Try the com_err message */
172         errMsg = krb5_get_error_message(krbContext, status_value);
173     }
174
175     if (errMsg != NULL) {
176         major = makeStringBuffer(minor, errMsg, status_string);
177     } else {
178         major = GSS_S_COMPLETE;
179         *minor = 0;
180     }
181
182     if (krbContext != NULL)
183         krb5_free_error_message(krbContext, errMsg);
184
185     return major;
186 }