use shibresolver API properly
[mech_eap.git] / util_attr.cpp
index 1a891bf..0fe8d17 100644 (file)
 
 #include "gssapiP_eap.h"
 
+#include <typeinfo>
 #include <string>
 #include <exception>
 #include <new>
 
-static gss_eap_attr_create_provider gssEapAttrFactories[ATTR_TYPE_MAX];
-static gss_buffer_desc gssEapAttrPrefixes[ATTR_TYPE_MAX];
+static gss_eap_attr_create_provider gssEapAttrFactories[ATTR_TYPE_MAX + 1];
+static gss_buffer_desc gssEapAttrPrefixes[ATTR_TYPE_MAX + 1];
 
+/*
+ * Register a provider for a particular type and prefix
+ */
 void
 gss_eap_attr_ctx::registerProvider(unsigned int type,
                                    const char *prefix,
                                    gss_eap_attr_create_provider factory)
 {
-    assert(type < ATTR_TYPE_MAX);
+    assert(type <= ATTR_TYPE_MAX);
 
     assert(gssEapAttrFactories[type] == NULL);
 
@@ -58,33 +62,46 @@ gss_eap_attr_ctx::registerProvider(unsigned int type,
     }
 }
 
+/*
+ * Unregister a provider
+ */
 void
 gss_eap_attr_ctx::unregisterProvider(unsigned int type)
 {
-    assert(type < ATTR_TYPE_MAX);
+    assert(type <= ATTR_TYPE_MAX);
 
     gssEapAttrFactories[type] = NULL;
     gssEapAttrPrefixes[type].value = NULL;
     gssEapAttrPrefixes[type].length = 0;
 }
 
+/*
+ * Create an attribute context, that manages instances of providers
+ */
 gss_eap_attr_ctx::gss_eap_attr_ctx(void)
 {
-    for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
+    for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
         gss_eap_attr_provider *provider;
 
-        provider = (gssEapAttrFactories[i])();
+        if (gssEapAttrFactories[i] != NULL) {
+            provider = (gssEapAttrFactories[i])();
+        } else {
+            provider = NULL;
+        }
 
         m_providers[i] = provider;
     }
 }
 
+/*
+ * Convert an attribute prefix to a type
+ */
 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++) {
+    for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
         if (bufferEqual(&gssEapAttrPrefixes[i], prefix))
             return i;
     }
@@ -92,60 +109,112 @@ gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
     return ATTR_TYPE_LOCAL;
 }
 
+/*
+ * Convert a type to an attribute prefix
+ */
 const gss_buffer_t
 gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
 {
-    if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_LOCAL)
+    if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_MAX)
         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 *manager)
 {
-    for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider;
+    bool ret = true;
 
-        provider = m_providers[i];
-        if (provider != NULL) {
-            if (!provider->initFromExistingContext(this, manager->m_providers[i]))
-                return false;
-        }
+    for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
+        gss_eap_attr_provider *provider = m_providers[i];
+
+        if (provider == NULL)
+            continue;
+
+        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_cred_id_t cred,
                                      const gss_ctx_id_t ctx)
 {
-    for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider;
+    bool ret = true;
 
-        provider = m_providers[i];
-        if (provider != NULL) {
-            if (!provider->initFromGssContext(this, cred, ctx))
-                return false;
-        }
+    for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
+        gss_eap_attr_provider *provider = m_providers[i];
+
+        if (provider == NULL)
+            continue;
+
+        ret = provider->initFromGssContext(this, cred, ctx);
+        if (ret == false)
+            break;
+    }
+
+    return ret;
+}
+
+/*
+ * Initialize a context from an exported context or name token
+ */
+bool
+gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
+{
+    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 true;
+    return ret;
 }
 
 gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
 {
-    for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++)
+    for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++)
         delete m_providers[i];
 }
 
+/*
+ * Locate provider for a given type
+ */
 gss_eap_attr_provider *
 gss_eap_attr_ctx::getProvider(unsigned int type) const
 {
+    assert(type >= ATTR_TYPE_MIN && type <= ATTR_TYPE_MAX);
     return m_providers[type];
 }
 
+/*
+ * Locate provider for a given prefix
+ */
 gss_eap_attr_provider *
 gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
 {
@@ -156,6 +225,19 @@ gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
     return m_providers[type];
 }
 
+/*
+ * Get primary provider. Only the primary provider is serialised when
+ * gss_export_sec_context() or gss_export_name_composite() is called.
+ */
+gss_eap_attr_provider *
+gss_eap_attr_ctx::getPrimaryProvider(void) const
+{
+    return m_providers[ATTR_TYPE_MIN];
+}
+
+/*
+ * Set an attribute
+ */
 void
 gss_eap_attr_ctx::setAttribute(int complete,
                                const gss_buffer_t attr,
@@ -172,10 +254,14 @@ gss_eap_attr_ctx::setAttribute(int complete,
         provider->setAttribute(complete,
                                (type == ATTR_TYPE_LOCAL) ? attr : &suffix,
                                value);
-                               
+    } else {
+        /* XXX TODO throw exception */
     }
 }
 
+/*
+ * Delete an attrbiute
+ */
 void
 gss_eap_attr_ctx::deleteAttribute(const gss_buffer_t attr)
 {
@@ -186,21 +272,22 @@ gss_eap_attr_ctx::deleteAttribute(const gss_buffer_t attr)
     decomposeAttributeName(attr, &type, &suffix);
 
     provider = m_providers[type];
-    if (provider != NULL) {
+    if (provider != NULL)
         provider->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
-    }
 }
 
+/*
+ * Enumerate attribute types with callback
+ */
 bool
 gss_eap_attr_ctx::getAttributeTypes(gss_eap_attr_enumeration_cb cb, void *data) const
 {
     bool ret = false;
     size_t i;
 
-    for (i = 0; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider;
+    for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
+        gss_eap_attr_provider *provider = m_providers[i];
 
-        provider = m_providers[i];
         if (provider == NULL)
             continue;
 
@@ -223,7 +310,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;
 
@@ -232,12 +318,15 @@ 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;
 }
 
+/*
+ * Enumerate attribute types, output is buffer set
+ */
 bool
 gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
 {
@@ -254,12 +343,11 @@ 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;
+    for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
+        gss_eap_attr_provider *provider = m_providers[i];
 
         args.type = i;
 
-        provider = m_providers[i];
         if (provider == NULL)
             continue;
 
@@ -268,13 +356,15 @@ gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
             break;
     }
 
-    if (ret == false) {
+    if (ret == false)
         gss_release_buffer_set(&minor, attrs);
-    }
 
     return ret;
 }
 
+/*
+ * Get attribute with given name
+ */
 bool
 gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
                                int *authenticated,
@@ -291,10 +381,8 @@ gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
     decomposeAttributeName(attr, &type, &suffix);
 
     provider = m_providers[type];
-    if (provider == NULL) {
-        *more = 0;
+    if (provider == NULL)
         return false;
-    }
 
     ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
                                  authenticated, complete,
@@ -303,59 +391,105 @@ gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
     return ret;
 }
 
+/*
+ * Map attribute context to C++ object
+ */
 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;
+    gss_buffer_desc suffix;
+
+    decomposeAttributeName(type_id, &type, &suffix);
+
+    provider = m_providers[type];
+    if (provider == NULL)
+        return (gss_any_t)NULL;
+
+    return provider->mapToAny(authenticated, &suffix);
 }
 
+/*
+ * Release mapped context
+ */
 void
 gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
                                         gss_any_t input) const
 {
+    unsigned int type;
+    gss_eap_attr_provider *provider;
+    gss_buffer_desc suffix;
+
+    decomposeAttributeName(type_id, &type, &suffix);
+
+    provider = m_providers[type];
+    if (provider != NULL)
+        provider->releaseAnyNameMapping(&suffix, input);
 }
 
+/*
+ * Export attribute context to buffer
+ */
 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_buffer_t buffer)
+/*
+ * Return soonest expiry time of providers
+ */
+time_t
+gss_eap_attr_ctx::getExpiryTime(void) const
 {
     unsigned int i;
-    bool ret;
-
-    ret = m_providers[ATTR_TYPE_RADIUS]->initFromBuffer(this, buffer);
-    if (!ret)
-        return false;
+    time_t expiryTime = 0;
 
-    for (i = ATTR_TYPE_RADIUS + 1; i < ATTR_TYPE_MAX; i++) {
+    for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
         gss_eap_attr_provider *provider = m_providers[i];
+        time_t providerExpiryTime;
 
-        ret = provider->initFromGssContext(this,
-                                           GSS_C_NO_CREDENTIAL,
-                                           GSS_C_NO_CONTEXT);
-        if (!ret)
-            break;
+        if (provider == NULL)
+            continue;
+
+        providerExpiryTime = provider->getExpiryTime();
+        if (providerExpiryTime == 0)
+            continue;
+
+        if (expiryTime == 0 || providerExpiryTime < expiryTime)
+            expiryTime = providerExpiryTime;
     }
 
-    return ret;
+    return expiryTime;
 }
 
 /*
- * C wrappers
+ * Map C++ exception to GSS status
  */
-
 static OM_uint32
 mapException(OM_uint32 *minor, std::exception &e)
 {
-    *minor = 0;
-    return GSS_S_FAILURE;
+    OM_uint32 major = GSS_S_FAILURE;
+
+    /* XXX TODO implement other mappings */
+    if (typeid(e) == typeid(std::bad_alloc))
+        *minor = ENOMEM;
+    else
+        *minor = 0;
+
+#ifdef GSSEAP_DEBUG
+    /* rethrow for now for debugging */
+    throw e;
+#endif
+
+    return major;
 }
 
+/*
+ * Decompose attribute name into prefix and suffix
+ */
 void
 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
                                          gss_buffer_t prefix,
@@ -383,6 +517,23 @@ gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
     }
 }
 
+/*
+ * Decompose attribute name into type and suffix
+ */
+void
+gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
+                                         unsigned int *type,
+                                         gss_buffer_t suffix)
+{
+    gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
+
+    decomposeAttributeName(attribute, &prefix, suffix);
+    *type = attributePrefixToType(&prefix);
+}
+
+/*
+ * Compose attribute name from prefix, suffix; returns C++ string
+ */
 std::string
 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
                                        const gss_buffer_t suffix)
@@ -402,6 +553,9 @@ gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
     return str;
 }
 
+/*
+ * Compose attribute name from type, suffix; returns C++ string
+ */
 std::string
 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
                                        const gss_buffer_t suffix)
@@ -411,6 +565,9 @@ gss_eap_attr_ctx::composeAttributeName(unsigned int type,
     return composeAttributeName(prefix, suffix);
 }
 
+/*
+ * Compose attribute name from prefix, suffix; returns GSS buffer
+ */
 void
 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
                                        const gss_buffer_t suffix,
@@ -426,17 +583,9 @@ gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
     }
 }
 
-void
-gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
-                                         unsigned int *type,
-                                         gss_buffer_t suffix)
-{
-    gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
-
-    decomposeAttributeName(attribute, &prefix, suffix);
-    *type = attributePrefixToType(&prefix);
-}
-
+/*
+ * Compose attribute name from type, suffix; returns GSS buffer
+ */
 void
 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
                                        const gss_buffer_t suffix,
@@ -447,6 +596,9 @@ gss_eap_attr_ctx::composeAttributeName(unsigned int type,
     return composeAttributeName(prefix, suffix, attribute);
 }
 
+/*
+ * C wrappers
+ */
 OM_uint32
 gssEapInquireName(OM_uint32 *minor,
                   gss_name_t name,
@@ -554,8 +706,6 @@ gssEapExportAttrContext(OM_uint32 *minor,
 
     try {
         name->attrCtx->exportToBuffer(buffer);
-        if (buffer->length == 0)
-            return GSS_S_FAILURE;
     } catch (std::exception &e) {
         return mapException(minor, e);
     }
@@ -623,6 +773,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) {
@@ -662,37 +815,10 @@ gssEapReleaseAttrContext(OM_uint32 *minor,
     return GSS_S_COMPLETE;
 }
 
-OM_uint32
-gssEapAttrProvidersInit(OM_uint32 *minor)
-{
-    try {
-        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_FAILURE;
-}
-
-OM_uint32
-gssEapAttrProvidersFinalize(OM_uint32 *minor)
-{
-    try {
-        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);
-    }
-
-    return GSS_S_COMPLETE;
-}
-
+/*
+ * Public accessor for initialisng a context from a GSS context. Also
+ * sets expiry time on GSS context as a side-effect.
+ */
 struct gss_eap_attr_ctx *
 gssEapCreateAttrContext(gss_cred_id_t gssCred,
                         gss_ctx_id_t gssCtx)
@@ -707,5 +833,7 @@ gssEapCreateAttrContext(gss_cred_id_t gssCred,
         return NULL;
     }
 
+    gssCtx->expiryTime = ctx->getExpiryTime();
+
     return ctx;
 }