X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=util_radius.cpp;h=314c685b45ee1697e42eeb05b5e03c84e1fccd6d;hb=26ca006e95e900ea1762d0c08ea68533bf954de5;hp=837374949f7e32acee313ce2595740ae0249ecc4;hpb=3e345919cc361c0219f67846a606c9fd55e5824f;p=mech_eap.git diff --git a/util_radius.cpp b/util_radius.cpp index 8373749..314c685 100644 --- a/util_radius.cpp +++ b/util_radius.cpp @@ -32,112 +32,794 @@ #include "gssapiP_eap.h" +/* stuff that should be provided by libradsec/libfreeradius-radius */ +#define VENDORATTR(vendor, attr) (((vendor) << 16) | (attr)) + +#ifndef ATTRID +#define ATTRID(attr) ((attr) & 0xFFFF) +#endif + +static gss_buffer_desc radiusUrnPrefix = { + sizeof("urn:x-radius:") - 1, + (void *)"urn:x-radius:" +}; + +static struct rs_error * +radiusAllocHandle(const char *configFile, + rs_handle **pHandle) +{ + rs_handle *rh; + struct rs_alloc_scheme ralloc; + + *pHandle = NULL; + + if (configFile == NULL || configFile[0] == '\0') + configFile = RS_CONFIG_FILE; + + if (rs_context_create(&rh, RS_DICT_FILE) != 0) + return NULL; + + ralloc.calloc = GSSEAP_CALLOC; + ralloc.malloc = GSSEAP_MALLOC; + ralloc.free = GSSEAP_FREE; + ralloc.realloc = GSSEAP_REALLOC; + + rs_context_set_alloc_scheme(rh, &ralloc); + + if (rs_context_read_config(rh, configFile) != 0) { + rs_context_destroy(rh); + return rs_err_ctx_pop(rh); + } + + *pHandle = rh; + return NULL; +} + +gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void) +{ + m_rh = NULL; + m_vps = NULL; + m_authenticated = false; +} + +gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void) +{ + if (m_rh != NULL) + rs_context_destroy(m_rh); + if (m_vps != NULL) + pairfree(&m_vps); +} + bool -gss_eap_radius_attr_source::initFromExistingContext(const gss_eap_attr_ctx *manager, - const gss_eap_attr_source *ctx) +gss_eap_radius_attr_provider::allocRadHandle(const std::string &configFile) { - if (!gss_eap_attr_source::initFromExistingContext(manager, ctx)) + m_configFile.assign(configFile); + + /* + * Currently none of the FreeRADIUS functions we use here actually take + * a handle, so we may as well leave it as NULL. + */ +#if 0 + radiusAllocHandle(m_configFile.c_str(), &m_rh); + + return (m_rh != NULL); +#else + return true; +#endif +} + +bool +gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager, + const gss_eap_attr_provider *ctx) +{ + const gss_eap_radius_attr_provider *radius; + + if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) return false; + radius = static_cast(ctx); + + if (!allocRadHandle(radius->m_configFile)) + return false; + + if (radius->m_vps != NULL) + m_vps = paircopy(const_cast(radius->getAvps())); + return true; } bool -gss_eap_radius_attr_source::initFromGssContext(const gss_eap_attr_ctx *manager, - const gss_cred_id_t gssCred, - const gss_ctx_id_t gssCtx) +gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager, + const gss_cred_id_t gssCred, + const gss_ctx_id_t gssCtx) { - if (!gss_eap_attr_source::initFromGssContext(manager, gssCred, gssCtx)) + std::string configFile(RS_CONFIG_FILE); + + if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx)) return false; + if (gssCred != GSS_C_NO_CREDENTIAL && gssCred->radiusConfigFile != NULL) + configFile.assign(gssCred->radiusConfigFile); + + if (!allocRadHandle(configFile)) + return false; + + if (gssCtx != GSS_C_NO_CONTEXT) { + if (gssCtx->acceptorCtx.vps != NULL) { + m_vps = paircopy(gssCtx->acceptorCtx.vps); + if (m_vps == NULL) + return false; + } + } + return true; } -gss_eap_radius_attr_source::~gss_eap_radius_attr_source(void) +static bool +alreadyAddedAttributeP(std::vector &attrs, VALUE_PAIR *vp) +{ + for (std::vector::const_iterator a = attrs.begin(); + a != attrs.end(); + ++a) { + if (strcmp(vp->name, (*a).c_str()) == 0) + return true; + } + + return false; +} + +static bool +isHiddenAttributeP(uint16_t attrid, uint16_t vendor) { + bool ret = false; + + switch (vendor) { + case VENDORPEC_MS: + switch (attrid) { + case PW_MS_MPPE_SEND_KEY: + case PW_MS_MPPE_RECV_KEY: + ret = true; + break; + default: + break; + } + case VENDORPEC_UKERNA: + ret = true; + break; + default: + break; + } + + return ret; } bool -gss_eap_radius_attr_source::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const +gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const { + VALUE_PAIR *vp; + std::vector seen; + + for (vp = m_vps; vp != NULL; vp = vp->next) { + gss_buffer_desc attribute; + char attrid[64]; + if (isHiddenAttributeP(ATTRID(vp->attribute), VENDOR(vp->attribute))) + continue; + + if (alreadyAddedAttributeP(seen, vp)) + continue; + + snprintf(attrid, sizeof(attrid), "%s%d", + (char *)radiusUrnPrefix.value, vp->attribute); + + attribute.value = attrid; + attribute.length = strlen(attrid); + + if (!addAttribute(this, &attribute, data)) + return false; + + seen.push_back(std::string(vp->name)); + } + return true; } void -gss_eap_radius_attr_source::setAttribute(int complete, - const gss_buffer_t attr, - const gss_buffer_t value) +gss_eap_radius_attr_provider::setAttribute(int complete, + const gss_buffer_t attr, + const gss_buffer_t value) { } void -gss_eap_radius_attr_source::deleteAttribute(const gss_buffer_t value) +gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value) { } bool -gss_eap_radius_attr_source::getAttribute(const gss_buffer_t attr, - int *authenticated, - int *complete, - gss_buffer_t value, - gss_buffer_t display_value, - int *more) const +gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr, + int *authenticated, + int *complete, + gss_buffer_t value, + gss_buffer_t display_value, + int *more) const { - return false; + OM_uint32 tmpMinor; + gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER; + DICT_ATTR *da; + uint32_t attrid; + char *s; + + duplicateBuffer(*attr, &strAttr); + s = (char *)strAttr.value; + + if (attr->length < radiusUrnPrefix.length || + memcmp(s, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0) + return false; + + s += radiusUrnPrefix.length; + + if (isdigit(*s)) { + attrid = strtoul(s, NULL, 10); + } else { + da = dict_attrbyname(s); + if (da == NULL) { + gss_release_buffer(&tmpMinor, &strAttr); + return false; + } + attrid = da->attr; + } + + gss_release_buffer(&tmpMinor, &strAttr); + + return getAttribute(attrid, authenticated, complete, + value, display_value, more); } bool -gss_eap_radius_attr_source::getAttribute(unsigned int attr, - int *authenticated, - int *complete, - gss_buffer_t value, - gss_buffer_t display_value, - int *more) const +gss_eap_radius_attr_provider::getAttribute(uint16_t vattrid, + uint16_t vendor, + int *authenticated, + int *complete, + gss_buffer_t value, + gss_buffer_t display_value, + int *more) const { - return false; + uint32_t attrid = VENDORATTR(vendor, vattrid); + VALUE_PAIR *vp; + int i = *more, count = 0; + + *more = 0; + + if (isHiddenAttributeP(attrid, vendor)) + return false; + + if (i == -1) + i = 0; + + for (vp = pairfind(m_vps, attrid); + vp != NULL; + vp = pairfind(vp->next, attrid)) { + if (count++ == i) { + if (pairfind(vp->next, attrid) != NULL) + *more = count; + break; + } + } + + if (vp == NULL && *more == 0) + return false; + + if (value != GSS_C_NO_BUFFER) { + gss_buffer_desc valueBuf; + + valueBuf.value = (void *)vp->vp_octets; + valueBuf.length = vp->length; + + duplicateBuffer(valueBuf, value); + } + + if (display_value != GSS_C_NO_BUFFER) { + char displayString[MAX_STRING_LEN]; + gss_buffer_desc displayBuf; + + displayBuf.length = vp_prints_value(displayString, + sizeof(displayString), vp, 0); + displayBuf.value = (void *)displayString; + + duplicateBuffer(displayBuf, display_value); + } + + if (authenticated != NULL) + *authenticated = m_authenticated; + if (complete != NULL) + *complete = true; + + return true; +} + +bool +gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute, + uint16_t vendor, + int *authenticated, + int *complete, + gss_buffer_t value) const +{ + OM_uint32 major, minor; + + major = gssEapRadiusGetAvp(&minor, m_vps, attribute, vendor, value, TRUE); + + if (authenticated != NULL) + *authenticated = m_authenticated; + if (complete != NULL) + *complete = true; + + return !GSS_ERROR(major); +} + +bool +gss_eap_radius_attr_provider::getAttribute(uint32_t attrid, + int *authenticated, + int *complete, + gss_buffer_t value, + gss_buffer_t display_value, + int *more) const +{ + + return getAttribute(ATTRID(attrid), VENDOR(attrid), + authenticated, complete, + value, display_value, more); } gss_any_t -gss_eap_radius_attr_source::mapToAny(int authenticated, +gss_eap_radius_attr_provider::mapToAny(int authenticated, gss_buffer_t type_id) const { - return (gss_any_t)NULL; + if (authenticated && !m_authenticated) + return (gss_any_t)NULL; + + return (gss_any_t)paircopy(m_vps); } void -gss_eap_radius_attr_source::releaseAnyNameMapping(gss_buffer_t type_id, - gss_any_t input) const +gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id, + gss_any_t input) const +{ + pairfree((VALUE_PAIR **)&input); +} + +bool +gss_eap_radius_attr_provider::init(void) { + gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS, + "urn:ietf:params:gss-eap:radius-avp", + gss_eap_radius_attr_provider::createAttrContext); + return true; } void -gss_eap_radius_attr_source::exportToBuffer(gss_buffer_t buffer) const +gss_eap_radius_attr_provider::finalize(void) { + gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS); } -bool -gss_eap_radius_attr_source::initFromBuffer(const gss_eap_attr_ctx *ctx, - const gss_buffer_t buffer) +gss_eap_attr_provider * +gss_eap_radius_attr_provider::createAttrContext(void) { - if (!gss_eap_attr_source::initFromBuffer(ctx, buffer)) - return false; + return new gss_eap_radius_attr_provider; +} + +OM_uint32 +gssEapRadiusAddAvp(OM_uint32 *minor, + rs_handle *rh, + VALUE_PAIR **vps, + uint16_t vattrid, + uint16_t vendor, + gss_buffer_t buffer) +{ + uint32_t attrid = VENDORATTR(vendor, vattrid); + unsigned char *p = (unsigned char *)buffer->value; + size_t remain = buffer->length; + + do { + VALUE_PAIR *vp; + size_t n = remain; + + if (n > MAX_STRING_LEN) + n = MAX_STRING_LEN; + + vp = paircreate(attrid, PW_TYPE_OCTETS); + if (vp == NULL) { + *minor = ENOMEM; + return GSS_S_FAILURE; + } + + memcpy(vp->vp_octets, p, n); + vp->length = n; + + pairadd(vps, vp); + + p += n; + remain -= n; + } while (remain != 0); + + return GSS_S_COMPLETE; +} + +OM_uint32 +gssEapRadiusGetRawAvp(OM_uint32 *minor, + VALUE_PAIR *vps, + uint16_t type, + uint16_t vendor, + VALUE_PAIR **vp) +{ + uint32_t attr = VENDORATTR(vendor, type); + + *vp = pairfind(vps, attr); + + return (*vp == NULL) ? GSS_S_UNAVAILABLE : GSS_S_COMPLETE; +} + +OM_uint32 +gssEapRadiusGetAvp(OM_uint32 *minor, + VALUE_PAIR *vps, + uint16_t type, + uint16_t vendor, + gss_buffer_t buffer, + int concat) +{ + VALUE_PAIR *vp; + unsigned char *p; + uint32_t attr = VENDORATTR(vendor, type); + + buffer->length = 0; + buffer->value = NULL; + + vp = pairfind(vps, attr); + if (vp == NULL) + return GSS_S_UNAVAILABLE; + + do { + buffer->length += vp->length; + } while (concat && (vp = pairfind(vp->next, attr)) != NULL); + + buffer->value = GSSEAP_MALLOC(buffer->length); + if (buffer->value == NULL) { + *minor = ENOMEM; + return GSS_S_FAILURE; + } + + p = (unsigned char *)buffer->value; + + for (vp = pairfind(vps, attr); + concat && vp != NULL; + vp = pairfind(vp->next, attr)) { + memcpy(p, vp->vp_octets, vp->length); + p += vp->length; + } + + *minor = 0; + return GSS_S_COMPLETE; +} + +OM_uint32 +gssEapRadiusFreeAvps(OM_uint32 *minor, + VALUE_PAIR **vps) +{ + pairfree(vps); + *minor = 0; + return GSS_S_COMPLETE; +} + +OM_uint32 +gssEapRadiusAttrProviderInit(OM_uint32 *minor) +{ + return gss_eap_radius_attr_provider::init() + ? GSS_S_COMPLETE : GSS_S_FAILURE; +} + +OM_uint32 +gssEapRadiusAttrProviderFinalize(OM_uint32 *minor) +{ + gss_eap_radius_attr_provider::finalize(); + return GSS_S_COMPLETE; +} + +/* partition error namespace so it does not conflict with krb5 */ +#define ERROR_TABLE_BASE_rse (46882560L) + +#define RS_TO_COM_ERR(rse) ((rse) == RSE_OK ? 0 : (rse) + ERROR_TABLE_BASE_rse) +#define COM_TO_RS_ERR(err) ((err) > ERROR_TABLE_BASE_rse && \ + (err) <= (ERROR_TABLE_BASE_rse + RSE_SOME_ERROR) ? \ + (err) - ERROR_TABLE_BASE_rse : RSE_SOME_ERROR) + +OM_uint32 +gssEapRadiusMapError(OM_uint32 *minor, + struct rs_error *err) +{ + int code = RSE_OK; + + if (err != NULL) + code = rs_err_code(err, 0); + else + code = RSE_SOME_ERROR; + + *minor = RS_TO_COM_ERR(code); + + gssEapSaveStatusInfo(*minor, "radsec: %s", rs_err_msg(err, 0)); + + rs_err_free(err); + return GSS_S_FAILURE; +} + +OM_uint32 +gssEapRadiusAllocConn(OM_uint32 *minor, + const gss_cred_id_t cred, + gss_ctx_id_t ctx) +{ + struct gss_eap_acceptor_ctx *actx = &ctx->acceptorCtx; + const char *configFile = NULL; + const char *configStanza = "gss-eap"; + struct rs_error *err; + + assert(actx->radHandle == NULL); + assert(actx->radConn == NULL); + + if (cred != GSS_C_NO_CREDENTIAL) { + if (cred->radiusConfigFile != NULL) + configFile = cred->radiusConfigFile; + if (cred->radiusConfigStanza != NULL) + configStanza = cred->radiusConfigStanza; + } + + err = radiusAllocHandle(configFile, &actx->radHandle); + if (err != NULL || actx->radHandle == NULL) { + return gssEapRadiusMapError(minor, err); + } + + if (rs_conn_create(actx->radHandle, &actx->radConn, configStanza) != 0) { + return gssEapRadiusMapError(minor, rs_err_conn_pop(actx->radConn)); + } + + /* XXX TODO rs_conn_select_server does not exist yet */ +#if 0 + if (actx->radServer != NULL) { + if (rs_conn_select_server(actx->radConn, actx->radServer) != 0) + return gssEapRadiusMapError(minor, rs_err_conn_pop(actx->radConn)); + } +#endif + + *minor = 0; + 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(rs_handle *rh, + const VALUE_PAIR *vp, + unsigned char **pBuffer, + size_t *pRemain) +{ + unsigned char *p = *pBuffer; + size_t remain = *pRemain; + + assert(remain >= avpSize(vp)); + store_uint32_be(vp->attribute, p); + + 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); + break; + default: + assert(vp->length <= MAX_STRING_LEN); + p[4] = (uint8_t)vp->length; + memcpy(p + 5, vp->vp_octets, vp->length); + break; + } + + *pBuffer += 5 + p[4]; + *pRemain -= 5 + p[4]; + + return true; + +} + +static bool +avpImport(rs_handle *rh, + VALUE_PAIR **pVp, + unsigned char **pBuffer, + size_t *pRemain) +{ + unsigned char *p = *pBuffer; + size_t remain = *pRemain; + VALUE_PAIR *vp = NULL; + DICT_ATTR *da; + OM_uint32 attrid; + + if (remain < avpSize(NULL)) + goto fail; + + attrid = load_uint32_be(p); + p += 4; + remain -= 4; + + da = dict_attrbyvalue(attrid); + if (da == NULL) + goto fail; + + vp = pairalloc(da); + if (vp == NULL) { + throw new std::bad_alloc; + 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; + break; + case PW_TYPE_STRING: + /* check enough room to NUL terminate */ + if (p[0] == MAX_STRING_LEN) + goto fail; + else + /* fallthrough */ + default: + if (p[0] > 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'; + + p += 1 + vp->length; + remain -= 1 + vp->length; + break; + } + + *pVp = vp; + *pBuffer = p; + *pRemain = remain; + + return true; + +fail: + pairbasicfree(vp); return false; } bool -gss_eap_radius_attr_source::init(void) +gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx, + const gss_buffer_t buffer) { + unsigned char *p = (unsigned char *)buffer->value; + size_t remain = buffer->length; + OM_uint32 configFileLen, count; + VALUE_PAIR **pNext = &m_vps; + + if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer)) + return false; + + if (remain < 4) + return false; + + configFileLen = load_uint32_be(p); + p += 4; + remain -= 4; + + if (remain < configFileLen) + return false; + + std::string configFile((char *)p, configFileLen); + p += configFileLen; + remain -= configFileLen; + + if (!allocRadHandle(configFile)) + return false; + + if (remain < 4) + return false; + + count = load_uint32_be(p); + p += 4; + remain -= 4; + + do { + VALUE_PAIR *attr; + + if (!avpImport(m_rh, &attr, &p, &remain)) + return false; + + *pNext = attr; + pNext = &attr->next; + + count--; + } while (remain != 0); + + if (count != 0) + return false; + return true; } void -gss_eap_radius_attr_source::finalize(void) +gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const { + OM_uint32 count = 0; + VALUE_PAIR *vp; + unsigned char *p; + size_t remain = 4 + m_configFile.length() + 4; + + for (vp = m_vps; vp != NULL; vp = vp->next) { + remain += avpSize(vp); + count++; + } + + buffer->value = GSSEAP_MALLOC(remain); + if (buffer->value == NULL) { + throw new std::bad_alloc; + return; + } + buffer->length = remain; + + p = (unsigned char *)buffer->value; + + store_uint32_be(m_configFile.length(), p); + p += 4; + remain -= 4; + + memcpy(p, m_configFile.c_str(), m_configFile.length()); + p += m_configFile.length(); + remain -= m_configFile.length(); + + store_uint32_be(count, p); + p += 4; + remain -= 4; + + for (vp = m_vps; vp != NULL; vp = vp->next) { + avpExport(m_rh, vp, &p, &remain); + } + + assert(remain == 0); } -gss_eap_attr_source * -gss_eap_radius_attr_source::createAttrContext(void) +time_t +gss_eap_radius_attr_provider::getExpiryTime(void) const { - return new gss_eap_radius_attr_source; + VALUE_PAIR *vp; + + vp = pairfind(m_vps, PW_SESSION_TIMEOUT); + if (vp == NULL || vp->lvalue == 0) + return 0; + + return time(NULL) + vp->lvalue; }