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 isSecretAttributeP(int attrid, int vendor)
142 case RADIUS_VENDOR_ID_MICROSOFT:
144 case RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY:
145 case RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY:
159 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
162 std::vector <std::string> seen;
164 for (vp = m_avps; vp != NULL; vp = vp->next) {
165 gss_buffer_desc attribute;
167 if (isSecretAttributeP(ATTRID(vp->attribute), VENDOR(vp->attribute)))
170 if (alreadyAddedAttributeP(seen, vp))
173 attribute.value = (void *)vp->name;
174 attribute.length = strlen(vp->name);
176 if (!addAttribute(this, &attribute, data))
179 seen.push_back(std::string(vp->name));
186 gss_eap_radius_attr_provider::setAttribute(int complete,
187 const gss_buffer_t attr,
188 const gss_buffer_t value)
193 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
198 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
202 gss_buffer_t display_value,
206 gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
213 duplicateBuffer(*attr, &strAttr);
214 s = (char *)strAttr.value;
216 if (isdigit(((char *)strAttr.value)[0])) {
217 attrid = strtoul(s, NULL, 10);
219 d = rc_dict_findattr(m_rh, (char *)s);
221 gss_release_buffer(&tmpMinor, &strAttr);
227 gss_release_buffer(&tmpMinor, &strAttr);
229 return getAttribute(attrid, authenticated, complete,
230 value, display_value, more);
234 isPrintableAttributeP(VALUE_PAIR *vp)
238 for (i = 0; i < sizeof(vp->strvalue); i++) {
239 if (!isprint(vp->strvalue[i]))
247 gss_eap_radius_attr_provider::getAttribute(int attrid,
252 gss_buffer_t display_value,
259 char name[NAME_LENGTH + 1];
260 char displayString[AUTH_STRING_LEN + 1];
261 gss_buffer_desc valueBuf = GSS_C_EMPTY_BUFFER;
262 gss_buffer_desc displayBuf = GSS_C_EMPTY_BUFFER;
266 if (isSecretAttributeP(attrid, vendor))
269 vp = rc_avpair_get(m_avps, attrid, vendor);
281 } while ((vp = rc_avpair_get(vp->next, attrid, vendor)) != NULL);
286 if (vp->type == PW_TYPE_STRING) {
287 valueBuf.value = (void *)vp->strvalue;
288 valueBuf.length = vp->lvalue;
290 valueBuf.value = (void *)&vp->lvalue;
294 if (value != GSS_C_NO_BUFFER)
295 duplicateBuffer(valueBuf, value);
297 if (display_value != GSS_C_NO_BUFFER &&
298 isPrintableAttributeP(vp)) {
299 if (rc_avpair_tostr(m_rh, vp, name, NAME_LENGTH,
300 displayString, AUTH_STRING_LEN) != 0) {
301 gss_release_buffer(&tmpMinor, value);
305 displayBuf.value = (void *)displayString;
306 displayBuf.length = strlen(displayString);
308 duplicateBuffer(displayBuf, display_value);
311 if (authenticated != NULL)
312 *authenticated = m_authenticated;
313 if (complete != NULL)
323 gss_eap_radius_attr_provider::getAttribute(int attrid,
327 gss_buffer_t display_value,
331 return getAttribute(ATTRID(attrid), VENDOR(attrid),
332 authenticated, complete,
333 value, display_value, more);
337 gss_eap_radius_attr_provider::mapToAny(int authenticated,
338 gss_buffer_t type_id) const
340 if (authenticated && !m_authenticated)
341 return (gss_any_t)NULL;
343 return (gss_any_t)copyAvps(m_avps);
347 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
348 gss_any_t input) const
350 rc_avpair_free((VALUE_PAIR *)input);
354 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
357 buffer->value = NULL;
361 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
362 const gss_buffer_t buffer)
364 if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
367 if (!initFromGssCred(GSS_C_NO_CREDENTIAL))
374 gss_eap_radius_attr_provider::init(void)
376 gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
377 "urn:ietf:params:gss-eap:radius-avp",
378 gss_eap_radius_attr_provider::createAttrContext);
383 gss_eap_radius_attr_provider::finalize(void)
385 gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
388 gss_eap_attr_provider *
389 gss_eap_radius_attr_provider::createAttrContext(void)
391 return new gss_eap_radius_attr_provider;
395 addAvpFromBuffer(OM_uint32 *minor,
401 if (rc_avpair_add(rh, vp, type, buffer->value, buffer->length, 0) == NULL) {
402 return GSS_S_FAILURE;
405 return GSS_S_COMPLETE;
409 getBufferFromAvps(OM_uint32 *minor,
419 buffer->value = NULL;
421 vp = rc_avpair_get(vps, type, 0);
423 return GSS_S_UNAVAILABLE;
426 buffer->length += vp->lvalue;
427 } while (concat && (vp = rc_avpair_get(vp->next, type, 0)) != NULL);
429 buffer->value = GSSEAP_MALLOC(buffer->length);
430 if (buffer->value == NULL) {
432 return GSS_S_FAILURE;
435 p = (unsigned char *)buffer->value;
437 for (vp = rc_avpair_get(vps, type, 0);
438 concat && vp != NULL;
439 vp = rc_avpair_get(vp->next, type, 0)) {
440 memcpy(p, vp->strvalue, vp->lvalue);
445 return GSS_S_COMPLETE;
449 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
451 return gss_eap_radius_attr_provider::init()
452 ? GSS_S_COMPLETE : GSS_S_FAILURE;
456 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
458 gss_eap_radius_attr_provider::finalize();
459 return GSS_S_COMPLETE;
463 gssEapRadiusAllocHandle(OM_uint32 *minor,
464 const gss_cred_id_t cred,
468 const char *config = RC_CONFIG_FILE;
472 if (cred != GSS_C_NO_CREDENTIAL && cred->radiusConfigFile != NULL)
473 config = cred->radiusConfigFile;
475 rh = rc_read_config((char *)config);
479 return GSS_S_FAILURE;
482 if (rc_read_dictionary(rh, rc_conf_str(rh, (char *)"dictionary")) != 0) {
484 return GSS_S_FAILURE;
488 return GSS_S_COMPLETE;