X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=mech_eap%2Futil_radius.cpp;h=2411b0435177bba9fe336fe5da76de55d6558ab5;hb=refs%2Fheads%2Fddf-name;hp=083b3e70426434d6712797a9a653da1adac5cb0a;hpb=064949a307c23c739f0079d8c6c8bcd67a8e4613;p=moonshot.git diff --git a/mech_eap/util_radius.cpp b/mech_eap/util_radius.cpp index 083b3e7..2411b04 100644 --- a/mech_eap/util_radius.cpp +++ b/mech_eap/util_radius.cpp @@ -36,6 +36,8 @@ #include "gssapiP_eap.h" +#include + /* stuff that should be provided by libradsec/libfreeradius-radius */ #define VENDORATTR(vendor, attr) (((vendor) << 16) | (attr)) @@ -440,7 +442,8 @@ void gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED, gss_any_t input) const { - pairfree((VALUE_PAIR **)&input); + VALUE_PAIR *vp = (VALUE_PAIR *)input; + pairfree(&vp); } bool @@ -458,7 +461,16 @@ gss_eap_radius_attr_provider::init(void) * dictionary, otherwise accepting reauthentication tokens fails unless * the acceptor has already accepted a normal authentication token. */ - if (rs_context_create(&radContext, RS_DICT_FILE) != 0) { + if (rs_context_create(&radContext) != 0) + return false; + + if (rs_context_read_config(radContext, RS_CONFIG_FILE) != 0) { + rs_context_destroy(radContext); + return false; + } + + if (rs_context_init_freeradius_dict(radContext, NULL)) { + rs_context_destroy(radContext); return false; } @@ -611,71 +623,48 @@ gssEapRadiusAttrProviderFinalize(OM_uint32 *minor) return GSS_S_COMPLETE; } -/* - * Encoding is: - * 4 octet NBO attribute ID | 4 octet attribute length | attribute data - */ -static size_t -avpSize(const VALUE_PAIR *vp) -{ - size_t size = 4 + 1; - - if (vp != NULL) - size += vp->length; - - return size; -} - -static bool -avpExport(const VALUE_PAIR *vp, - unsigned char **pBuffer, - size_t *pRemain) +static DDF +avpMarshall(const VALUE_PAIR *vp) { - unsigned char *p = *pBuffer; - size_t remain = *pRemain; + DDF obj(NULL); - assert(remain >= avpSize(vp)); + obj.addmember("type").integer(vp->attribute); - store_uint32_be(vp->attribute, p); + assert(vp->length <= MAX_STRING_LEN); switch (vp->type) { case PW_TYPE_INTEGER: case PW_TYPE_IPADDR: case PW_TYPE_DATE: - p[4] = 4; - store_uint32_be(vp->lvalue, p + 5); + obj.addmember("value").integer(vp->lvalue); break; - default: - assert(vp->length <= MAX_STRING_LEN); - p[4] = (uint8_t)vp->length; - memcpy(p + 5, vp->vp_octets, vp->length); + case PW_TYPE_STRING: + obj.addmember("value").string(vp->vp_strvalue); break; - } + default: { + XMLSize_t len; + XMLByte *b64 = xercesc::Base64::encode(vp->vp_octets, vp->length, &len); - *pBuffer += 5 + p[4]; - *pRemain -= 5 + p[4]; + if (b64[len - 1] == '\n') + b64[--len] = '\0'; /* XXX there may be embedded newlines */ - return true; + obj.addmember("value").string((char *)b64); + delete b64; + break; + } + } + return obj; } static bool -avpImport(VALUE_PAIR **pVp, - unsigned char **pBuffer, - size_t *pRemain) +avpUnmarshall(VALUE_PAIR **pVp, DDF &obj) { - unsigned char *p = *pBuffer; - size_t remain = *pRemain; VALUE_PAIR *vp = NULL; DICT_ATTR *da; uint32_t attrid; - if (remain < avpSize(NULL)) - goto fail; - - attrid = load_uint32_be(p); - p += 4; - remain -= 4; + attrid = obj["type"].integer(); da = dict_attrbyvalue(attrid); if (da != NULL) { @@ -688,40 +677,42 @@ avpImport(VALUE_PAIR **pVp, goto fail; } - if (remain < p[0]) - goto fail; - switch (vp->type) { case PW_TYPE_INTEGER: case PW_TYPE_IPADDR: case PW_TYPE_DATE: - if (p[0] != 4) - goto fail; - vp->length = 4; - vp->lvalue = load_uint32_be(p + 1); - p += 5; - remain -= 5; + vp->lvalue = obj["value"].integer();; break; - case PW_TYPE_STRING: - default: - if (p[0] >= MAX_STRING_LEN) + case PW_TYPE_STRING: { + const char *str = obj["value"].string(); + size_t len = strlen(str); + if (str == NULL || len >= MAX_STRING_LEN) goto fail; - vp->length = (uint32_t)p[0]; - memcpy(vp->vp_octets, p + 1, vp->length); - - if (vp->type == PW_TYPE_STRING) - vp->vp_strvalue[vp->length] = '\0'; + vp->length = len; + memcpy(vp->vp_strvalue, str, len + 1); + break; + } + case PW_TYPE_OCTETS: + default: { + XMLSize_t len; + const XMLByte *b64 = (const XMLByte *)obj["value"].string(); + XMLByte *data = xercesc::Base64::decode(b64, &len); + if (data == NULL || len >= MAX_STRING_LEN) { + delete data; + goto fail; + } - p += 1 + vp->length; - remain -= 1 + vp->length; + vp->length = len; + memcpy(vp->vp_octets, data, len); + vp->vp_octets[len] = '\0'; + delete data; break; } + } *pVp = vp; - *pBuffer = p; - *pRemain = remain; return true; @@ -732,55 +723,51 @@ fail: return false; } +const char * +gss_eap_radius_attr_provider::marshallingKey(void) const +{ + return "radius"; +} + bool -gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx, - const gss_buffer_t buffer) +gss_eap_radius_attr_provider::unmarshallAndInit(const gss_eap_attr_ctx *ctx, + DDF &obj) { - unsigned char *p = (unsigned char *)buffer->value; - size_t remain = buffer->length; VALUE_PAIR **pNext = &m_vps; - if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer)) + if (!gss_eap_attr_provider::unmarshallAndInit(ctx, obj)) return false; - do { - VALUE_PAIR *attr; + DDF attrs = obj["attributes"]; + DDF attr = attrs.first(); - if (!avpImport(&attr, &p, &remain)) + while (!attr.isnull()) { + VALUE_PAIR *vp; + + if (!avpUnmarshall(&vp, attr)) return false; - *pNext = attr; - pNext = &attr->next; - } while (remain != 0); + *pNext = vp; + pNext = &vp->next; + + attr = attrs.next(); + } return true; } -void -gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const +DDF +gss_eap_radius_attr_provider::marshall(void) const { - VALUE_PAIR *vp; - unsigned char *p; - size_t remain = 0; - - for (vp = m_vps; vp != NULL; vp = vp->next) { - remain += avpSize(vp); - } + DDF obj(NULL); + DDF attrs = obj.structure().addmember("attributes").list(); - buffer->value = GSSEAP_MALLOC(remain); - if (buffer->value == NULL) { - throw new std::bad_alloc; - return; - } - buffer->length = remain; - - p = (unsigned char *)buffer->value; - - for (vp = m_vps; vp != NULL; vp = vp->next) { - avpExport(vp, &p, &remain); + for (VALUE_PAIR *vp = m_vps; vp != NULL; vp = vp->next) { + DDF attr = avpMarshall(vp); + attrs.add(attr); } - assert(remain == 0); + return obj; } time_t