maek noncopyable classes
[mech_eap.orig] / util_attr.cpp
index 5228645..0e62d34 100644 (file)
 #include <exception>
 #include <new>
 
-static gss_eap_attr_create_cb
+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
+    gss_eap_radius_attr_source::createAttrContext,
+    gss_eap_saml_assertion_source::createAttrContext,
+    gss_eap_saml_attr_source::createAttrContext,
+    gss_eap_shib_attr_source::createAttrContext
 };
 
-gss_eap_attr_ctx *
-gss_eap_attr_ctx::createAttrContext(gss_cred_id_t gssCred,
-                                    gss_ctx_id_t gssCtx)
+gss_eap_attr_ctx::gss_eap_attr_ctx(void)
 {
-    gss_eap_attr_ctx *ctx;
+    for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
+        gss_eap_attr_source *source;
+
+        source = (gss_eap_attr_factories[i])();
+
+        m_sources[i] = source;
+    }
+}
 
-    ctx = new gss_eap_attr_ctx(NULL, gssCred, gssCtx);
+bool
+gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager,
+                                          const gss_eap_attr_source *source)
+{
+    if (!gss_eap_attr_source::initFromExistingContext(this, source))
+        return false;
 
     for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider;
+        gss_eap_attr_source *source;
 
-        provider = (gss_eap_attr_factories[i])(ctx, gssCred, gssCtx);
-        if (provider != NULL)
-            ctx->m_providers[i] = provider;
+        source = m_sources[i];
+        if (source != NULL) {
+            if (!source->initFromExistingContext(this, source))
+                return false;
+        }
     }
 
-    return ctx;
+    return true;
+}
+
+bool
+gss_eap_attr_ctx::initFromGssContext(const gss_eap_attr_ctx *manager,
+                                     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;
+
+        source = m_sources[i];
+        if (source != NULL) {
+            if (!source->initFromGssContext(this, cred, ctx))
+                return false;
+        }
+    }
+
+    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];
+        delete m_sources[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();
+    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_provider::finalize();
-    gss_eap_saml_attr_provider::finalize();
-    gss_eap_saml_assertion_provider::finalize();
-    gss_eap_radius_attr_provider::finalize();
+    gss_eap_shib_attr_source::finalize();
+    gss_eap_saml_attr_source::finalize();
+    gss_eap_saml_assertion_source::finalize();
+    gss_eap_radius_attr_source::finalize();
 }
 
-gss_eap_attr_provider *
+gss_eap_attr_source *
 gss_eap_attr_ctx::getProvider(unsigned int type) const
 {
-    return m_providers[type];
+    return m_sources[type];
 }
 
-gss_eap_attr_provider *
+gss_eap_attr_source *
 gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
 {
     unsigned int type;
 
     type = attributePrefixToType(prefix);
 
-    return m_providers[type];
-}
-
-gss_eap_attr_ctx::gss_eap_attr_ctx(const gss_eap_attr_ctx &ctx)
-    : gss_eap_attr_provider(ctx)
-{
-    for (unsigned int i = 0; i < ATTR_TYPE_MAX; i++) {
-        if (ctx.m_providers[i] != NULL) {
-            m_providers[i] = (gss_eap_attr_factories[i])(&ctx,
-                                                         GSS_C_NO_CREDENTIAL,
-                                                         GSS_C_NO_CONTEXT);
-        }
-    }
+    return m_sources[type];
 }
 
 void
@@ -122,13 +143,13 @@ gss_eap_attr_ctx::setAttribute(int complete,
 {
     gss_buffer_desc suffix = GSS_C_EMPTY_BUFFER;
     unsigned int type;
-    gss_eap_attr_provider *provider;
+    gss_eap_attr_source *source;
 
     decomposeAttributeName(attr, &type, &suffix);
 
-    provider = m_providers[type];
-    if (provider != NULL) {
-        provider->setAttribute(complete,
+    source = m_sources[type];
+    if (source != NULL) {
+        source->setAttribute(complete,
                                (type == ATTR_TYPE_LOCAL) ? attr : &suffix,
                                value);
                                
@@ -140,13 +161,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_provider *provider;
+    gss_eap_attr_source *source;
 
     decomposeAttributeName(attr, &type, &suffix);
 
-    provider = m_providers[type];
-    if (provider != NULL) {
-        provider->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
+    source = m_sources[type];
+    if (source != NULL) {
+        source->deleteAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix);
     }
 }
 
@@ -157,13 +178,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_provider *provider;
+        gss_eap_attr_source *source;
 
-        provider = m_providers[i];
-        if (provider == NULL)
+        source = m_sources[i];
+        if (source == NULL)
             continue;
 
-        ret = provider->getAttributeTypes(cb, data);
+        ret = source->getAttributeTypes(cb, data);
         if (ret == false)
             break;
     }
@@ -177,7 +198,7 @@ struct eap_gss_get_attr_types_args {
 };
 
 static bool
-addAttribute(const gss_eap_attr_provider *provider,
+addAttribute(const gss_eap_attr_source *source,
              const gss_buffer_t attribute,
              void *data)
 {
@@ -214,15 +235,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;
+        gss_eap_attr_source *source;
 
         args.type = i;
 
-        provider = m_providers[i];
-        if (provider == NULL)
+        source = m_sources[i];
+        if (source == NULL)
             continue;
 
-        ret = provider->getAttributeTypes(addAttribute, (void *)&args);
+        ret = source->getAttributeTypes(addAttribute, (void *)&args);
         if (ret == false)
             break;
     }
@@ -244,20 +265,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_provider *provider;
+    gss_eap_attr_source *source;
     bool ret;
 
     decomposeAttributeName(attr, &type, &suffix);
 
-    provider = m_providers[type];
-    if (provider == NULL) {
+    source = m_sources[type];
+    if (source == NULL) {
         *more = 0;
         return false;
     }
 
-    ret = provider->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
-                                 authenticated, complete,
-                                 value, display_value, more);
+    ret = source->getAttribute(type == ATTR_TYPE_LOCAL ? attr : &suffix,
+                               authenticated, complete,
+                               value, display_value, more);
 
     return ret;
 }
@@ -276,21 +297,33 @@ 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_sources[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_eap_attr_ctx *manager,
+                                 const gss_buffer_t buffer)
 {
-    int i;
+    unsigned int i;
+    bool ret;
 
-    for (i = 0; i < ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider = m_providers[i];
+    ret = m_sources[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];
+
+        ret = source->initFromGssContext(this,
+                                         GSS_C_NO_CREDENTIAL,
+                                         GSS_C_NO_CONTEXT);
+        if (!ret)
+            break;
     }
+
+    return ret;
 }
 
 
@@ -339,7 +372,7 @@ gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
     return ATTR_TYPE_LOCAL;
 }
 
-gss_buffer_t
+const gss_buffer_t
 gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
 {
     if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_LOCAL)
@@ -375,44 +408,52 @@ gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
     }
 }
 
-void
+std::string
 gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
-                                       const gss_buffer_t suffix,
-                                       gss_buffer_t attribute)
+                                       const gss_buffer_t suffix)
 {
-    size_t len = 0;
-    char *p;
-
-    attribute->length = 0;
-    attribute->value = NULL;
+    std::string str;
 
     if (prefix == GSS_C_NO_BUFFER || prefix->length == 0)
-        return;
+        return str;
+
+    str.append((const char *)prefix->value, prefix->length);
 
-    len = prefix->length;
     if (suffix != GSS_C_NO_BUFFER) {
-        len += 1 + suffix->length;
+        str.append(" ");
+        str.append((const char *)suffix->value, suffix->length);
     }
 
-    attribute->value = GSSEAP_MALLOC(len + 1);
-    if (attribute->value == NULL) {
-        throw new std::bad_alloc;
-    }
-    attribute->length = len;
+    return str;
+}
 
-    p = (char *)attribute->value;
-    memcpy(p, prefix->value, prefix->length);
-    if (suffix != NULL) {
-        p[prefix->length] = ' ';
-        memcpy(p + prefix->length + 1, suffix->value, suffix->length);
-    }
+std::string
+gss_eap_attr_ctx::composeAttributeName(unsigned int type,
+                                       const gss_buffer_t suffix)
+{
+    const gss_buffer_t prefix = attributeTypeToPrefix(type);
 
-    p[attribute->length] = '\0';
+    return composeAttributeName(prefix, suffix);
+}
+
+void
+gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
+                                       const gss_buffer_t suffix,
+                                       gss_buffer_t attribute)
+{
+    std::string str = composeAttributeName(prefix, suffix);
+
+    if (str.length() != 0) {
+        return duplicateBuffer(str, attribute);
+    } else {
+        attribute->length = 0;
+        attribute->value = NULL;
+    }
 }
 
 void
 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
-                                         unsigned int*type,
+                                         unsigned int *type,
                                          gss_buffer_t suffix)
 {
     gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
@@ -428,7 +469,7 @@ gss_eap_attr_ctx::composeAttributeName(unsigned int type,
 {
     gss_buffer_t prefix = attributeTypeToPrefix(type);
 
-    composeAttributeName(prefix, suffix, attribute);
+    return composeAttributeName(prefix, suffix, attribute);
 }
 
 OM_uint32
@@ -529,11 +570,15 @@ gssEapExportAttrContext(OM_uint32 *minor,
                         gss_name_t name,
                         gss_buffer_t buffer)
 {
-    if (name->attrCtx == NULL)
-        return GSS_S_UNAVAILABLE;
+    if (name->attrCtx == NULL) {
+        buffer->length = 0;
+        buffer->value = NULL;
+
+        return GSS_S_COMPLETE;
+    };
 
     try {
-        name->attrCtx->marshall(buffer);
+        name->attrCtx->exportToBuffer(buffer);
     } catch (std::exception &e) {
         return mapException(minor, e);
     }
@@ -546,7 +591,26 @@ gssEapImportAttrContext(OM_uint32 *minor,
                         gss_buffer_t buffer,
                         gss_name_t name)
 {
-    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(NULL, 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;
 }
 
 OM_uint32
@@ -554,12 +618,20 @@ 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)
-            out->attrCtx = new gss_eap_attr_ctx(*(in->attrCtx));
-        else
-            out->attrCtx = NULL;
+        if (in->attrCtx != NULL) {
+            if (!ctx->initFromExistingContext(NULL, in->attrCtx)) {
+                delete ctx;
+                return GSS_S_FAILURE;
+            }
+            out->attrCtx = ctx;
+        }
     } catch (std::exception &e) {
+        delete ctx;
         return mapException(minor, e);
     }
 
@@ -637,9 +709,16 @@ gssEapAttrProvidersFinalize(OM_uint32 *minor)
 }
 
 struct gss_eap_attr_ctx *
-gssEapCreateAttrContext(gss_cred_id_t cred,
-                        gss_ctx_id_t ctx)
+gssEapCreateAttrContext(gss_cred_id_t gssCred,
+                        gss_ctx_id_t gssCtx)
 {
-    assert(ctx != GSS_C_NO_CONTEXT);
-    return gss_eap_attr_ctx::createAttrContext(cred, ctx);
+    gss_eap_attr_ctx *ctx;
+
+    ctx = new gss_eap_attr_ctx;
+    if (!ctx->initFromGssContext(NULL, gssCred, gssCtx)) {
+        delete ctx;
+        return NULL;
+    }
+
+    return ctx;
 }