2 * Copyright (c) 2010, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
33 #include "gssapiP_eap.h"
36 gss_eap_radius_attr_provider::copyAvps(const VALUE_PAIR *src)
39 VALUE_PAIR *dst = NULL, **pDst = &dst;
41 for (vp = src; vp != NULL; vp = vp->next) {
44 vp2 = (VALUE_PAIR *)GSSEAP_CALLOC(1, sizeof(*vp2));
49 memcpy(vp2, vp, sizeof(*vp));
58 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
62 m_authenticated = false;
65 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
70 rc_avpair_free(m_avps);
74 gss_eap_radius_attr_provider::initFromGssCred(const gss_cred_id_t cred)
78 return !GSS_ERROR(gssEapRadiusAllocHandle(&minor, cred, &m_rh));
82 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
83 const gss_eap_attr_provider *ctx)
85 const gss_eap_radius_attr_provider *radius;
87 if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
90 if (!initFromGssCred(GSS_C_NO_CREDENTIAL))
93 radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
94 if (radius->m_avps != NULL) {
95 m_avps = copyAvps(radius->getAvps());
102 gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
103 const gss_cred_id_t gssCred,
104 const gss_ctx_id_t gssCtx)
106 if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
109 if (!initFromGssCred(gssCred))
112 if (gssCtx != GSS_C_NO_CONTEXT) {
113 if (gssCtx->acceptorCtx.avps != NULL) {
114 m_avps = copyAvps(gssCtx->acceptorCtx.avps);
124 alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
126 for (std::vector<std::string>::const_iterator a = attrs.begin();
129 if (strcmp(vp->name, (*a).c_str()) == 0)
137 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
140 std::vector <std::string> seen;
142 for (vp = m_avps; vp != NULL; vp = vp->next) {
143 gss_buffer_desc attribute;
145 if (alreadyAddedAttributeP(seen, vp))
148 attribute.value = (void *)vp->name;
149 attribute.length = strlen(vp->name);
151 if (!addAttribute(this, &attribute, data))
154 seen.push_back(std::string(vp->name));
161 gss_eap_radius_attr_provider::setAttribute(int complete,
162 const gss_buffer_t attr,
163 const gss_buffer_t value)
168 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
173 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
177 gss_buffer_t display_value,
181 gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
188 duplicateBuffer(*attr, &strAttr);
189 s = (char *)strAttr.value;
191 if (isdigit(((char *)strAttr.value)[0])) {
192 attrid = strtoul(s, NULL, 10);
194 d = rc_dict_findattr(m_rh, (char *)s);
196 gss_release_buffer(&tmpMinor, &strAttr);
202 gss_release_buffer(&tmpMinor, &strAttr);
204 return getAttribute(attrid, authenticated, complete,
205 value, display_value, more);
209 isPrintableAttributeP(VALUE_PAIR *vp)
213 for (i = 0; i < sizeof(vp->strvalue); i++) {
214 if (!isprint(vp->strvalue[i]))
222 gss_eap_radius_attr_provider::getAttribute(int attrid,
227 gss_buffer_t display_value,
234 char name[NAME_LENGTH + 1];
235 char displayString[AUTH_STRING_LEN + 1];
236 gss_buffer_desc valueBuf = GSS_C_EMPTY_BUFFER;
237 gss_buffer_desc displayBuf = GSS_C_EMPTY_BUFFER;
241 vp = rc_avpair_get(m_avps, attrid, vendor);
253 } while ((vp = rc_avpair_get(vp->next, attrid, vendor)) != NULL);
258 if (vp->type == PW_TYPE_STRING) {
259 valueBuf.value = (void *)vp->strvalue;
260 valueBuf.length = vp->lvalue;
262 valueBuf.value = (void *)&vp->lvalue;
266 if (value != GSS_C_NO_BUFFER)
267 duplicateBuffer(valueBuf, value);
269 if (display_value != GSS_C_NO_BUFFER &&
270 isPrintableAttributeP(vp)) {
271 if (rc_avpair_tostr(m_rh, vp, name, NAME_LENGTH,
272 displayString, AUTH_STRING_LEN) != 0) {
273 gss_release_buffer(&tmpMinor, value);
277 displayBuf.value = (void *)displayString;
278 displayBuf.length = strlen(displayString);
280 duplicateBuffer(displayBuf, display_value);
283 if (authenticated != NULL)
284 *authenticated = m_authenticated;
285 if (complete != NULL)
295 gss_eap_radius_attr_provider::getAttribute(int attrid,
299 gss_buffer_t display_value,
302 return getAttribute(attrid, 0, authenticated, complete,
303 value, display_value, more);
307 gss_eap_radius_attr_provider::mapToAny(int authenticated,
308 gss_buffer_t type_id) const
310 if (authenticated && !m_authenticated)
311 return (gss_any_t)NULL;
313 return (gss_any_t)copyAvps(m_avps);
317 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
318 gss_any_t input) const
320 rc_avpair_free((VALUE_PAIR *)input);
324 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
327 buffer->value = NULL;
331 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
332 const gss_buffer_t buffer)
334 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
337 if (!initFromGssCred(GSS_C_NO_CREDENTIAL))
344 gss_eap_radius_attr_provider::init(void)
346 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
347 "urn:ietf:params:gss-eap:radius-avp",
348 gss_eap_radius_attr_provider::createAttrContext);
353 gss_eap_radius_attr_provider::finalize(void)
355 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
358 gss_eap_attr_provider *
359 gss_eap_radius_attr_provider::createAttrContext(void)
361 return new gss_eap_radius_attr_provider;
365 addAvpFromBuffer(OM_uint32 *minor,
371 if (rc_avpair_add(rh, vp, type, buffer->value, buffer->length, 0) == NULL) {
372 return GSS_S_FAILURE;
375 return GSS_S_COMPLETE;
379 getBufferFromAvps(OM_uint32 *minor,
389 buffer->value = NULL;
391 vp = rc_avpair_get(vps, type, 0);
393 return GSS_S_UNAVAILABLE;
396 buffer->length += vp->lvalue;
397 } while (concat && (vp = rc_avpair_get(vp->next, type, 0)) != NULL);
399 buffer->value = GSSEAP_MALLOC(buffer->length);
400 if (buffer->value == NULL) {
402 return GSS_S_FAILURE;
405 p = (unsigned char *)buffer->value;
407 for (vp = rc_avpair_get(vps, type, 0);
408 concat && vp != NULL;
409 vp = rc_avpair_get(vp->next, type, 0)) {
410 memcpy(p, vp->strvalue, vp->lvalue);
415 return GSS_S_COMPLETE;
419 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
421 return gss_eap_radius_attr_provider::init()
422 ? GSS_S_COMPLETE : GSS_S_FAILURE;
426 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
428 gss_eap_radius_attr_provider::finalize();
429 return GSS_S_COMPLETE;
433 gssEapRadiusAllocHandle(OM_uint32 *minor,
434 const gss_cred_id_t cred,
438 const char *config = RC_CONFIG_FILE;
442 if (cred != GSS_C_NO_CREDENTIAL && cred->radiusConfigFile != NULL)
443 config = cred->radiusConfigFile;
445 rh = rc_read_config((char *)config);
449 return GSS_S_FAILURE;
452 if (rc_read_dictionary(rh, rc_conf_str(rh, (char *)"dictionary")) != 0) {
454 return GSS_S_FAILURE;
458 return GSS_S_COMPLETE;