remuse unused mutable keyword
[mech_eap.orig] / util_attr.cpp
index 0e62d34..5414749 100644 (file)
 #include <exception>
 #include <new>
 
-static gss_eap_attr_create_factory
-gss_eap_attr_factories[ATTR_TYPE_MAX] = {
-    gss_eap_radius_attr_source::createAttrContext,
-    gss_eap_saml_assertion_source::createAttrContext,
-    gss_eap_saml_attr_source::createAttrContext,
-    gss_eap_shib_attr_source::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_source *source;
+        gss_eap_attr_provider *provider;
 
-        source = (gss_eap_attr_factories[i])();
+        provider = (gssEapAttrFactories[i])();
 
-        m_sources[i] = source;
+        m_providers[i] = provider;
     }
 }
 
-bool
-gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager,
-                                          const gss_eap_attr_source *source)
+unsigned int
+gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
 {
-    if (!gss_eap_attr_source::initFromExistingContext(this, source))
-        return false;
+    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];
+}
+
+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_source *source;
+        gss_eap_attr_provider *provider;
 
-        source = m_sources[i];
-        if (source != NULL) {
-            if (!source->initFromExistingContext(this, source))
+        provider = m_providers[i];
+        if (provider != NULL) {
+            if (!provider->initFromExistingContext(this, manager->m_providers[i]))
                 return false;
         }
     }
@@ -76,19 +118,15 @@ gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager,
 }
 
 bool
-gss_eap_attr_ctx::initFromGssContext(const gss_eap_attr_ctx *manager,
-                                     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_source::initFromGssContext(this, cred, ctx))
-        return false;
-
     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_source *source;
+        gss_eap_attr_provider *provider;
 
-        source = m_sources[i];
-        if (source != NULL) {
-            if (!source->initFromGssContext(this, cred, ctx))
+        provider = m_providers[i];
+        if (provider != NULL) {
+            if (!provider->initFromGssContext(this, cred, ctx))
                 return false;
         }
     }
@@ -99,41 +137,23 @@ gss_eap_attr_ctx::initFromGssContext(const gss_eap_attr_ctx *manager,
 gss_eap_attr_ctx::~gss_eap_attr_ctx(void)
 {
     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++)
-        delete m_sources[i];
-}
-
-bool
-gss_eap_attr_ctx::init(void)
-{
-    return gss_eap_radius_attr_source::init() &&
-           gss_eap_saml_assertion_source::init() &&
-           gss_eap_saml_attr_source::init() &&
-           gss_eap_shib_attr_source::init();
-}
-
-void
-gss_eap_attr_ctx::finalize(void)
-{
-    gss_eap_shib_attr_source::finalize();
-    gss_eap_saml_attr_source::finalize();
-    gss_eap_saml_assertion_source::finalize();
-    gss_eap_radius_attr_source::finalize();
+        delete m_providers[i];
 }
 
-gss_eap_attr_source *
+gss_eap_attr_provider *
 gss_eap_attr_ctx::getProvider(unsigned int type) const
 {
-    return m_sources[type];
+    return m_providers[type];
 }
 
-gss_eap_attr_source *
+gss_eap_attr_provider *
 gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
 {
     unsigned int type;
 
     type = attributePrefixToType(prefix);
 
-    return m_sources[type];
+    return m_providers[type];
 }
 
 void
@@ -143,13 +163,13 @@ gss_eap_attr_ctx::setAttribute(int complete,
 {
     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
     unsigned int type;
-    gss_eap_attr_source *source;
+    gss_eap_attr_provider *provider;
 
     decomposeAttributeName(attr, &type, &suffix);
 
-    source = m_sources[type];
-    if (source != NULL) {
-        source->setAttribute(complete,
+    provider = m_providers[type];
+    if (provider != NULL) {
+        provider->setAttribute(complete,
                                (type == ATTR_TYPE_LOCAL) ? attr : &suffix,
                                value);
                                
@@ -161,13 +181,13 @@ gss_eap_attr_ctx::deleteAttribute(const gss_buffer_t attr)
 {
     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
     unsigned int type;
-    gss_eap_attr_source *source;
+    gss_eap_attr_provider *provider;
 
     decomposeAttributeName(attr, &type, &suffix);
 
-    source = m_sources[type];
-    if (source != NULL) {
-        source->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
+    provider = m_providers[type];
+    if (provider != NULL) {
+        provider->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
     }
 }
 
@@ -178,13 +198,13 @@ 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_source *source;
+        gss_eap_attr_provider *provider;
 
-        source = m_sources[i];
-        if (source == NULL)
+        provider = m_providers[i];
+        if (provider == NULL)
             continue;
 
-        ret = source->getAttributeTypes(cb, data);
+        ret = provider->getAttributeTypes(cb, data);
         if (ret == false)
             break;
     }
@@ -198,7 +218,7 @@ struct eap_gss_get_attr_types_args {
 };
 
 static bool
-addAttribute(const gss_eap_attr_source *source,
+addAttribute(const gss_eap_attr_provider *provider,
              const gss_buffer_t attribute,
              void *data)
 {
@@ -235,15 +255,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_source *source;
+        gss_eap_attr_provider *provider;
 
         args.type = i;
 
-        source = m_sources[i];
-        if (source == NULL)
+        provider = m_providers[i];
+        if (provider == NULL)
             continue;
 
-        ret = source->getAttributeTypes(addAttribute, (void *)&args);
+        ret = provider->getAttributeTypes(addAttribute, (void *)&args);
         if (ret == false)
             break;
     }
@@ -265,20 +285,20 @@ gss_eap_attr_ctx::getAttribute(const gss_buffer_t attr,
 {
     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
     unsigned int type;
-    gss_eap_attr_source *source;
+    gss_eap_attr_provider *provider;
     bool ret;
 
     decomposeAttributeName(attr, &type, &suffix);
 
-    source = m_sources[type];
-    if (source == NULL) {
+    provider = m_providers[type];
+    if (provider == NULL) {
         *more = 0;
         return false;
     }
 
-    ret = source->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
-                               authenticated, complete,
-                               value, display_value, more);
+    ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
+                                 authenticated, complete,
+                                 value, display_value, more);
 
     return ret;
 }
@@ -299,26 +319,25 @@ gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
 void
 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
 {
-    m_sources[ATTR_TYPE_RADIUS]->exportToBuffer(buffer);
+    m_providers[ATTR_TYPE_RADIUS]->exportToBuffer(buffer);
 }
 
 bool
-gss_eap_attr_ctx::initFromBuffer(const gss_eap_attr_ctx *manager,
-                                 const gss_buffer_t buffer)
+gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
 {
     unsigned int i;
     bool ret;
 
-    ret = m_sources[ATTR_TYPE_RADIUS]->initFromBuffer(this, buffer);
+    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_source *source = m_sources[i];
+        gss_eap_attr_provider *provider = m_providers[i];
 
-        ret = source->initFromGssContext(this,
-                                         GSS_C_NO_CREDENTIAL,
-                                         GSS_C_NO_CONTEXT);
+        ret = provider->initFromGssContext(this,
+                                           GSS_C_NO_CREDENTIAL,
+                                           GSS_C_NO_CONTEXT);
         if (!ret)
             break;
     }
@@ -326,7 +345,6 @@ gss_eap_attr_ctx::initFromBuffer(const gss_eap_attr_ctx *manager,
     return ret;
 }
 
-
 /*
  * C wrappers
  */
@@ -338,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,
@@ -575,10 +550,12 @@ gssEapExportAttrContext(OM_uint32 *minor,
         buffer->value = NULL;
 
         return GSS_S_COMPLETE;
-    };
+    }
 
     try {
         name->attrCtx->exportToBuffer(buffer);
+        if (buffer->length == 0)
+            return GSS_S_FAILURE;
     } catch (std::exception &e) {
         return mapException(minor, e);
     }
@@ -597,9 +574,9 @@ gssEapImportAttrContext(OM_uint32 *minor,
 
     if (buffer->length != 0) {
         try {
-            ctx = new gss_eap_attr_ctx;
+            ctx = new gss_eap_attr_ctx();
 
-            if (!ctx->initFromBuffer(NULL, buffer)) {
+            if (!ctx->initFromBuffer(buffer)) {
                 delete ctx;
                 return GSS_S_DEFECTIVE_TOKEN;
             }
@@ -624,7 +601,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;
             }
@@ -688,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);
     }
@@ -714,8 +699,10 @@ 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;
     }