remuse unused mutable keyword
[mech_eap.orig] / util_attr.cpp
index 74cf43b..5414749 100644 (file)
 #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];
 
-gss_eap_attr_ctx *
-gss_eap_attr_ctx::createAttrContext(void)
+void
+gss_eap_attr_ctx::registerProvider(unsigned int type,
+                                   const char *prefix,
+                                   gss_eap_attr_create_provider factory)
 {
-    gss_eap_attr_ctx *ctx;
+    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);
 
-    ctx = new gss_eap_attr_ctx;
+    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)
-            ctx->m_providers[i] = provider;
+        provider = (gssEapAttrFactories[i])();
+
+        m_providers[i] = provider;
     }
+}
 
-    return ctx;
+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;
 }
 
-bool
-gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *source,
-                                          const gss_eap_attr_provider *ctx)
+const gss_buffer_t
+gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
 {
-    if (!gss_eap_attr_provider::initFromExistingContext(this, ctx))
-        return false;
+    if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_LOCAL)
+        return GSS_C_NO_BUFFER;
 
+    return &gssEapAttrPrefixes[type];
+}
+
+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;
 
         provider = m_providers[i];
         if (provider != NULL) {
-            if (!provider->initFromExistingContext(this, provider))
+            if (!provider->initFromExistingContext(this, manager->m_providers[i]))
                 return false;
         }
     }
@@ -83,13 +118,9 @@ gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *source,
 }
 
 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;
-
     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
         gss_eap_attr_provider *provider;
 
@@ -109,24 +140,6 @@ gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
         delete m_providers[i];
 }
 
-bool
-gss_eap_attr_ctx::init(void)
-{
-    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();
-}
-
-void
-gss_eap_attr_ctx::finalize(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();
-}
-
 gss_eap_attr_provider *
 gss_eap_attr_ctx::getProvider(unsigned int type) const
 {
@@ -304,25 +317,34 @@ gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
 }
 
 void
-gss_eap_attr_ctx::marshall(gss_buffer_t buffer) const
+gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
 {
-    /* For now, just marshall the RADIUS context. */
+    m_providers[ATTR_TYPE_RADIUS]->exportToBuffer(buffer);
 }
 
 bool
-gss_eap_attr_ctx::unmarshall(const gss_eap_attr_ctx *ctx,
-                             const gss_buffer_t buffer)
+gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
 {
     unsigned int i;
+    bool ret;
 
-    for (i = 0; i < ATTR_TYPE_MAX; i++) {
+    ret = m_providers[ATTR_TYPE_RADIUS]->initFromBuffer(this, buffer);
+    if (!ret)
+        return false;
+
+    for (i = ATTR_TYPE_RADIUS + 1; i < ATTR_TYPE_MAX; i++) {
         gss_eap_attr_provider *provider = m_providers[i];
+
+        ret = provider->initFromGssContext(this,
+                                           GSS_C_NO_CREDENTIAL,
+                                           GSS_C_NO_CONTEXT);
+        if (!ret)
+            break;
     }
 
-    return false;
+    return ret;
 }
 
-
 /*
  * C wrappers
  */
@@ -334,49 +356,6 @@ mapException(OM_uint32 *minor, std::exception &e)
     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;
-
-    for (i = ATTR_TYPE_MIN;
-         i < sizeof(attributePrefixes) / sizeof(attributePrefixes[0]);
-         i++)
-    {
-        if (bufferEqual(&attributePrefixes[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 &attributePrefixes[type];
-}
-
 void
 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
                                          gss_buffer_t prefix,
@@ -571,10 +550,12 @@ gssEapExportAttrContext(OM_uint32 *minor,
         buffer->value = NULL;
 
         return GSS_S_COMPLETE;
-    };
+    }
 
     try {
-        name->attrCtx->marshall(buffer);
+        name->attrCtx->exportToBuffer(buffer);
+        if (buffer->length == 0)
+            return GSS_S_FAILURE;
     } catch (std::exception &e) {
         return mapException(minor, e);
     }
@@ -587,8 +568,24 @@ gssEapImportAttrContext(OM_uint32 *minor,
                         gss_buffer_t buffer,
                         gss_name_t name)
 {
-    if (buffer->length)
-        GSSEAP_NOT_IMPLEMENTED;
+    gss_eap_attr_ctx *ctx = NULL;
+
+    assert(name->attrCtx == NULL);
+
+    if (buffer->length != 0) {
+        try {
+            ctx = new gss_eap_attr_ctx();
+
+            if (!ctx->initFromBuffer(buffer)) {
+                delete ctx;
+                return GSS_S_DEFECTIVE_TOKEN;
+            }
+            name->attrCtx = ctx;
+        } catch (std::exception &e) {
+            delete ctx;
+            return mapException(minor, e);
+        }
+    }
 
     return GSS_S_COMPLETE;
 }
@@ -598,19 +595,21 @@ gssEapDuplicateAttrContext(OM_uint32 *minor,
                            gss_name_t in,
                            gss_name_t out)
 {
+    gss_eap_attr_ctx *ctx = NULL;
+
+    assert(out->attrCtx == NULL);
+
     try {
         if (in->attrCtx != NULL) {
-            gss_eap_attr_ctx *ctx = new gss_eap_attr_ctx;
-
-            out->attrCtx = new gss_eap_attr_ctx;
-            if (!ctx->initFromExistingContext(NULL, in->attrCtx)) {
+            ctx = new gss_eap_attr_ctx();
+            if (!ctx->initFromExistingContext(in->attrCtx)) {
                 delete ctx;
                 return GSS_S_FAILURE;
             }
             out->attrCtx = ctx;
-        } else
-            out->attrCtx = NULL;
+        }
     } catch (std::exception &e) {
+        delete ctx;
         return mapException(minor, e);
     }
 
@@ -667,19 +666,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);
     }
@@ -693,8 +699,10 @@ gssEapCreateAttrContext(gss_cred_id_t gssCred,
 {
     gss_eap_attr_ctx *ctx;
 
-    ctx = gss_eap_attr_ctx::createAttrContext();
-    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;
     }