import radius state
[mech_eap.git] / util_radius.cpp
1 /*
2  * Copyright (c) 2010, 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 #include "gssapiP_eap.h"
34
35 VALUE_PAIR *
36 gss_eap_radius_attr_provider::copyAvps(const VALUE_PAIR *in)
37 {
38     return NULL;
39 }
40
41 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
42 {
43     m_avps = NULL;
44     m_authenticated = false;
45 }
46
47 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
48 {
49     if (m_avps != NULL)
50         rc_avpair_free(m_avps);
51 }
52
53 bool
54 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
55                                                       const gss_eap_attr_provider *ctx)
56 {
57     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
58         return false;
59
60     return true;
61 }
62
63 bool
64 gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
65                                                  const gss_cred_id_t gssCred,
66                                                  const gss_ctx_id_t gssCtx)
67 {
68     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
69         return false;
70
71     return true;
72 }
73
74 bool
75 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
76 {
77     return true;
78 }
79
80 void
81 gss_eap_radius_attr_provider::setAttribute(int complete,
82                                            const gss_buffer_t attr,
83                                            const gss_buffer_t value)
84 {
85 }
86
87 void
88 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
89 {
90 }
91
92 bool
93 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
94                                            int *authenticated,
95                                            int *complete,
96                                            gss_buffer_t value,
97                                            gss_buffer_t display_value,
98                                            int *more) const
99 {
100     return false;
101 }
102
103 bool
104 gss_eap_radius_attr_provider::getAttribute(unsigned int attr,
105                                            int *authenticated,
106                                            int *complete,
107                                            gss_buffer_t value,
108                                            gss_buffer_t display_value,
109                                            int *more) const
110 {
111     return false;
112 }
113
114 gss_any_t
115 gss_eap_radius_attr_provider::mapToAny(int authenticated,
116                                        gss_buffer_t type_id) const
117 {
118     if (authenticated && !m_authenticated)
119         return (gss_any_t)NULL;
120
121     return (gss_any_t)copyAvps(m_avps);
122 }
123
124 void
125 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
126                                                     gss_any_t input) const
127 {
128     rc_avpair_free((VALUE_PAIR *)input);
129 }
130
131 void
132 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
133 {
134     buffer->length = 0;
135     buffer->value = NULL;
136 }
137
138 bool
139 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
140                                              const gss_buffer_t buffer)
141 {
142     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
143         return false;
144
145     return true;
146 }
147
148 bool
149 gss_eap_radius_attr_provider::init(void)
150 {
151     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
152                                        "urn:ietf:params:gss-eap:radius-avp",
153                                        gss_eap_radius_attr_provider::createAttrContext);
154     return true;
155 }
156
157 void
158 gss_eap_radius_attr_provider::finalize(void)
159 {
160     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
161 }
162
163 gss_eap_attr_provider *
164 gss_eap_radius_attr_provider::createAttrContext(void)
165 {
166     return new gss_eap_radius_attr_provider;
167 }
168
169 OM_uint32
170 addAvpFromBuffer(OM_uint32 *minor,
171                  rc_handle *rh,
172                  VALUE_PAIR **vp,
173                  int type,
174                  gss_buffer_t buffer)
175 {
176     if (rc_avpair_add(rh, vp, type, buffer->value, buffer->length, 0) == NULL) {
177         return GSS_S_FAILURE;
178     }
179
180     return GSS_S_COMPLETE;
181 }
182
183 OM_uint32
184 getBufferFromAvps(OM_uint32 *minor,
185                   VALUE_PAIR *vps,
186                   int type,
187                   gss_buffer_t buffer,
188                   int concat)
189 {
190     VALUE_PAIR *vp;
191     unsigned char *p;
192
193     buffer->length = 0;
194     buffer->value = NULL;
195
196     vp = rc_avpair_get(vps, type, 0);
197     if (vp == NULL)
198         return GSS_S_UNAVAILABLE;
199
200     do {
201         buffer->length += vp->lvalue;
202     } while (concat && (vp = rc_avpair_get(vp->next, type, 0)) != NULL);
203
204     buffer->value = GSSEAP_MALLOC(buffer->length);
205     if (buffer->value == NULL) {
206         *minor = ENOMEM;
207         return GSS_S_FAILURE;
208     }
209
210     p = (unsigned char *)buffer->value;
211
212     for (vp = rc_avpair_get(vps, type, 0);
213          concat && vp != NULL;
214          vp = rc_avpair_get(vp->next, type, 0)) {
215         memcpy(p, vp->strvalue, vp->lvalue);
216         p += vp->lvalue;
217     }
218
219     *minor = 0;
220     return GSS_S_COMPLETE;
221 }
222
223 OM_uint32
224 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
225 {
226     return gss_eap_radius_attr_provider::init()
227         ? GSS_S_COMPLETE : GSS_S_FAILURE;
228 }
229
230 OM_uint32
231 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
232 {
233     gss_eap_radius_attr_provider::finalize();
234     return GSS_S_COMPLETE;
235 }