various fixes, add a sample attribute to exercise code
[mech_eap.git] / util_attr.cpp
index 28a9161..c564c62 100644 (file)
 
 #include "gssapiP_eap.h"
 
+#include <typeinfo>
 #include <string>
 #include <exception>
 #include <new>
 
-static gss_eap_attr_create_factory
-gss_eap_attr_factories[ATTR_TYPE_MAX] = {
-    gss_eap_radius_attr_provider::createAttrContext,
-    gss_eap_saml_assertion_provider::createAttrContext,
-    gss_eap_saml_attr_provider::createAttrContext,
-    gss_eap_shib_attr_provider::createAttrContext
-};
+static gss_eap_attr_create_provider gssEapAttrFactories[ATTR_TYPE_MAX];
+static gss_buffer_desc gssEapAttrPrefixes[ATTR_TYPE_MAX];
+
+void
+gss_eap_attr_ctx::registerProvider(unsigned int type,
+                                   const char *prefix,
+                                   gss_eap_attr_create_provider factory)
+{
+    assert(type < ATTR_TYPE_MAX);
+
+    assert(gssEapAttrFactories[type] == NULL);
+
+    gssEapAttrFactories[type] = factory;
+    if (prefix != NULL) {
+        gssEapAttrPrefixes[type].value = (void *)prefix;
+        gssEapAttrPrefixes[type].length = strlen(prefix);
+    } else {
+        gssEapAttrPrefixes[type].value = NULL;
+        gssEapAttrPrefixes[type].length = 0;
+    }
+}
+
+void
+gss_eap_attr_ctx::unregisterProvider(unsigned int type)
+{
+    assert(type < ATTR_TYPE_MAX);
+
+    gssEapAttrFactories[type] = NULL;
+    gssEapAttrPrefixes[type].value = NULL;
+    gssEapAttrPrefixes[type].length = 0;
+}
 
 gss_eap_attr_ctx::gss_eap_attr_ctx(void)
 {
     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
         gss_eap_attr_provider *provider;
 
-        provider = (gss_eap_attr_factories[i])();
-        if (provider != NULL)
-            m_providers[i] = provider;
+        provider = (gssEapAttrFactories[i])();
+
+        m_providers[i] = provider;
+
+        /* providers should fail in constructor */
+        assert(m_providers[i] != NULL);
+    }
+}
+
+unsigned int
+gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
+{
+    unsigned int i;
+
+    for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_LOCAL; i++) {
+        if (bufferEqual(&gssEapAttrPrefixes[i], prefix))
+            return i;
     }
+
+    return ATTR_TYPE_LOCAL;
 }
 
+const gss_buffer_t
+gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
+{
+    if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_LOCAL)
+        return GSS_C_NO_BUFFER;
+
+    return &gssEapAttrPrefixes[type];
+}
+
+/*
+ * Initialize a context from an existing context.
+ */
 bool
-gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *source,
-                                          const gss_eap_attr_provider *ctx)
+gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
 {
-    if (!gss_eap_attr_provider::initFromExistingContext(this, ctx))
-        return false;
+    bool ret = true;
 
     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider;
+        gss_eap_attr_provider *provider = m_providers[i];
 
-        provider = m_providers[i];
-        if (provider != NULL) {
-            if (!provider->initFromExistingContext(this, provider))
-                return false;
-        }
+        ret = provider->initFromExistingContext(this, manager->m_providers[i]);
+        if (ret == false)
+            break;
     }
 
-    return true;
+    return ret;
 }
 
+/*
+ * Initialize a context from a GSS credential and context.
+ */
 bool
-gss_eap_attr_ctx::initFromGssContext(const gss_eap_attr_ctx *source,
-                                     const gss_cred_id_t cred,
+gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
                                      const gss_ctx_id_t ctx)
 {
-    if (!gss_eap_attr_provider::initFromGssContext(this, cred, ctx))
-        return false;
+    bool ret = true;
 
     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider;
+        gss_eap_attr_provider *provider = m_providers[i];
 
-        provider = m_providers[i];
-        if (provider != NULL) {
-            if (!provider->initFromGssContext(this, cred, ctx))
-                return false;
-        }
+        ret = provider->initFromGssContext(this, cred, ctx);
+        if (ret == false)
+            break;
     }
 
-    return true;
-}
-
-gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
-{
-    for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++)
-        delete m_providers[i];
+    return ret;
 }
 
+/*
+ * Initialize a context from an exported context or name token
+ */
 bool
-gss_eap_attr_ctx::init(void)
+gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
 {
-    return gss_eap_radius_attr_provider::init() &&
-           gss_eap_saml_assertion_provider::init() &&
-           gss_eap_saml_attr_provider::init() &&
-           gss_eap_shib_attr_provider::init();
+    bool ret;
+    gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
+
+    ret = primaryProvider->initFromBuffer(this, buffer);
+    if (ret == false)
+        return ret;
+
+    for (unsigned int i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
+        gss_eap_attr_provider *provider = m_providers[i];
+
+        if (provider == primaryProvider)
+            continue;
+
+        ret = provider->initFromGssContext(this,
+                                           GSS_C_NO_CREDENTIAL,
+                                           GSS_C_NO_CONTEXT);
+        if (ret == false)
+            break;
+    }
+
+    return ret;
 }
 
-void
-gss_eap_attr_ctx::finalize(void)
+gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
 {
-    gss_eap_shib_attr_provider::finalize();
-    gss_eap_saml_attr_provider::finalize();
-    gss_eap_saml_assertion_provider::finalize();
-    gss_eap_radius_attr_provider::finalize();
+    for (unsigned int i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++)
+        delete m_providers[i];
 }
 
 gss_eap_attr_provider *
@@ -136,6 +195,12 @@ gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
     return m_providers[type];
 }
 
+gss_eap_attr_provider *
+gss_eap_attr_ctx::getPrimaryProvider(void) const
+{
+    return m_providers[ATTR_TYPE_RADIUS];
+}
+
 void
 gss_eap_attr_ctx::setAttribute(int complete,
                                const gss_buffer_t attr,
@@ -178,13 +243,7 @@ gss_eap_attr_ctx::getAttributeTypes(gss_eap_attr_enumeration_cb cb, void *data)
     size_t i;
 
     for (i = 0; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider;
-
-        provider = m_providers[i];
-        if (provider == NULL)
-            continue;
-
-        ret = provider->getAttributeTypes(cb, data);
+        ret = m_providers[i]->getAttributeTypes(cb, data);
         if (ret == false)
             break;
     }
@@ -203,7 +262,6 @@ addAttribute(const gss_eap_attr_provider *provider,
              void *data)
 {
     eap_gss_get_attr_types_args *args = (eap_gss_get_attr_types_args *)data;
-    gss_buffer_t prefix = GSS_C_NO_BUFFER;
     gss_buffer_desc qualified;
     OM_uint32 major, minor;
 
@@ -212,10 +270,10 @@ addAttribute(const gss_eap_attr_provider *provider,
         major = gss_add_buffer_set_member(&minor, &qualified, &args->attrs);
         gss_release_buffer(&minor, &qualified);
     } else {
-        major = gss_add_buffer_set_member(&minor, prefix, &args->attrs);
+        major = gss_add_buffer_set_member(&minor, attribute, &args->attrs);
     }
 
-    return GSS_ERROR(major) ? false : true;
+    return GSS_ERROR(major) == false;
 }
 
 bool
@@ -235,22 +293,15 @@ gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
     args.attrs = *attrs;
 
     for (i = 0; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider;
-
         args.type = i;
 
-        provider = m_providers[i];
-        if (provider == NULL)
-            continue;
-
-        ret = provider->getAttributeTypes(addAttribute, (void *)&args);
+        ret = m_providers[i]->getAttributeTypes(addAttribute, (void *)&args);
         if (ret == false)
             break;
     }
 
-    if (ret == false) {
+    if (ret == false)
         gss_release_buffer_set(&minor, attrs);
-    }
 
     return ret;
 }
@@ -271,10 +322,6 @@ gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
     decomposeAttributeName(attr, &type, &suffix);
 
     provider = m_providers[type];
-    if (provider == NULL) {
-        *more = 0;
-        return false;
-    }
 
     ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
                                  authenticated, complete,
@@ -287,45 +334,55 @@ gss_any_t
 gss_eap_attr_ctx::mapToAny(int authenticated,
                            gss_buffer_t type_id) const
 {
-    return NULL;
+    unsigned int type;
+    gss_eap_attr_provider *provider;
+
+    type = attributePrefixToType(type_id);
+
+    provider = m_providers[type];
+
+    return provider->mapToAny(authenticated, type_id);
 }
 
 void
 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
                                         gss_any_t input) const
 {
+    unsigned int type;
+    gss_eap_attr_provider *provider;
+
+    type = attributePrefixToType(type_id);
+
+    provider = m_providers[type];
+
+    provider->releaseAnyNameMapping(type_id, input);
 }
 
 void
 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
 {
-    m_providers[ATTR_TYPE_RADIUS]->exportToBuffer(buffer);
+    getPrimaryProvider()->exportToBuffer(buffer);
 }
 
-bool
-gss_eap_attr_ctx::initFromBuffer(const gss_eap_attr_ctx *ctx,
-                                 const gss_buffer_t buffer)
+time_t
+gss_eap_attr_ctx::getExpiryTime(void) const
 {
     unsigned int i;
-    bool ret;
+    time_t expiryTime = 0;
 
-    ret = m_providers[ATTR_TYPE_RADIUS]->initFromBuffer(this, buffer);
-    if (!ret)
-        return false;
+    for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
+        time_t providerExpiryTime = m_providers[i]->getExpiryTime();
 
-    for (i = ATTR_TYPE_RADIUS + 1; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider = m_providers[i];
+        if (providerExpiryTime == 0)
+            continue;
 
-        ret = provider->initFromGssContext(
-            this, GSS_C_NO_CREDENTIAL, GSS_C_NO_CONTEXT);
-        if (!ret)
-            break;
+        if (expiryTime == 0 || providerExpiryTime < expiryTime)
+            expiryTime = providerExpiryTime;
     }
 
-    return ret;
+    return expiryTime;
 }
 
-
 /*
  * C wrappers
  */
@@ -333,51 +390,20 @@ gss_eap_attr_ctx::initFromBuffer(const gss_eap_attr_ctx *ctx,
 static OM_uint32
 mapException(OM_uint32 *minor, std::exception &e)
 {
-    *minor = 0;
-    return GSS_S_FAILURE;
-}
-
-static gss_buffer_desc attributePrefixes[] = {
-    {
-        /* ATTR_TYPE_RADIUS_AVP */
-        sizeof("urn:ietf:params:gss-eap:radius-avp"),
-        (void *)"urn:ietf:params:gss-eap:radius-avp",
-    },
-    {
-        /* ATTR_TYPE_SAML_AAA_ASSERTION */
-        sizeof("urn:ietf:params:gss-eap:saml-aaa-assertion"),
-        (void *)"urn:ietf:params:gss-eap:saml-aaa-assertion"
-    },
-    {
-        /* ATTR_TYPE_SAML_ATTR */
-        sizeof("urn:ietf:params:gss-eap:saml-attr"),
-        (void *)"urn:ietf:params:gss-eap:saml-attr"
-    },
-};
-
-unsigned int
-gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
-{
-    unsigned int i;
+    OM_uint32 major = GSS_S_FAILURE;
 
-    for (i = ATTR_TYPE_MIN;
-         i < sizeof(attributePrefixes) / sizeof(attributePrefixes[0]);
-         i++)
-    {
-        if (bufferEqual(&attributePrefixes[i], prefix))
-            return i;
-    }
+    /* XXX TODO implement other mappings */
+    if (typeid(e) == typeid(std::bad_alloc))
+        *minor = ENOMEM;
+    else
+        *minor = 0;
 
-    return ATTR_TYPE_LOCAL;
-}
-
-const gss_buffer_t
-gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
-{
-    if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_LOCAL)
-        return GSS_C_NO_BUFFER;
+#ifdef GSSEAP_DEBUG
+    /* rethrow for now for debugging */
+    throw e;
+#endif
 
-    return &attributePrefixes[type];
+    return major;
 }
 
 void
@@ -504,16 +530,16 @@ gssEapGetNameAttribute(OM_uint32 *minor,
     *authenticated = 0;
     *complete = 0;
 
-    value->length = 0;
-    value->value = NULL;
+    if (value != NULL) {
+        value->length = 0;
+        value->value = NULL;
+    }
 
     if (display_value != NULL) {
         display_value->length = 0;
         display_value->value = NULL;
     }
 
-    *more = -1;
-
     if (name->attrCtx == NULL)
         return GSS_S_UNAVAILABLE;
 
@@ -574,7 +600,7 @@ gssEapExportAttrContext(OM_uint32 *minor,
         buffer->value = NULL;
 
         return GSS_S_COMPLETE;
-    };
+    }
 
     try {
         name->attrCtx->exportToBuffer(buffer);
@@ -590,11 +616,15 @@ gssEapImportAttrContext(OM_uint32 *minor,
                         gss_buffer_t buffer,
                         gss_name_t name)
 {
-    if (buffer->length != 0) {
-        gss_eap_attr_ctx *ctx = new gss_eap_attr_ctx;
+    gss_eap_attr_ctx *ctx = NULL;
+
+    assert(name->attrCtx == NULL);
 
+    if (buffer->length != 0) {
         try {
-            if (!ctx->initFromBuffer(NULL, buffer)) {
+            ctx = new gss_eap_attr_ctx();
+
+            if (!ctx->initFromBuffer(buffer)) {
                 delete ctx;
                 return GSS_S_DEFECTIVE_TOKEN;
             }
@@ -619,7 +649,8 @@ gssEapDuplicateAttrContext(OM_uint32 *minor,
 
     try {
         if (in->attrCtx != NULL) {
-            if (!ctx->initFromExistingContext(NULL, in->attrCtx)) {
+            ctx = new gss_eap_attr_ctx();
+            if (!ctx->initFromExistingContext(in->attrCtx)) {
                 delete ctx;
                 return GSS_S_FAILURE;
             }
@@ -640,6 +671,9 @@ gssEapMapNameToAny(OM_uint32 *minor,
                    gss_buffer_t type_id,
                    gss_any_t *output)
 {
+    if (name->attrCtx == NULL)
+        return GSS_S_UNAVAILABLE;
+
     try {
         *output = name->attrCtx->mapToAny(authenticated, type_id);
     } catch (std::exception &e) {
@@ -683,19 +717,26 @@ OM_uint32
 gssEapAttrProvidersInit(OM_uint32 *minor)
 {
     try {
-        gss_eap_attr_ctx::init();
+        if (gss_eap_radius_attr_provider::init()    &&
+            gss_eap_saml_assertion_provider::init() &&
+            gss_eap_saml_attr_provider::init()      &&
+            gss_eap_shib_attr_provider::init())
+            return GSS_S_COMPLETE;
     } catch (std::exception &e) {
         return mapException(minor, e);
     }
 
-    return GSS_S_COMPLETE;
+    return GSS_S_FAILURE;
 }
 
 OM_uint32
 gssEapAttrProvidersFinalize(OM_uint32 *minor)
 {
     try {
-        gss_eap_attr_ctx::finalize();
+        gss_eap_shib_attr_provider::finalize();
+        gss_eap_saml_attr_provider::finalize();
+        gss_eap_saml_assertion_provider::finalize();
+        gss_eap_radius_attr_provider::finalize();
     } catch (std::exception &e) {
         return mapException(minor, e);
     }
@@ -709,11 +750,15 @@ gssEapCreateAttrContext(gss_cred_id_t gssCred,
 {
     gss_eap_attr_ctx *ctx;
 
-    ctx = new gss_eap_attr_ctx;
-    if (!ctx->initFromGssContext(NULL, gssCred, gssCtx)) {
+    assert(gssCtx != GSS_C_NO_CONTEXT);
+
+    ctx = new gss_eap_attr_ctx();
+    if (!ctx->initFromGssContext(gssCred, gssCtx)) {
         delete ctx;
         return NULL;
     }
 
+    gssCtx->expiryTime = ctx->getExpiryTime();
+
     return ctx;
 }