in progress use DDF to serialise names
[moonshot.git] / mech_eap / util_attr.cpp
index 2b4d7c4..80d2781 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <typeinfo>
 #include <string>
+#include <sstream>
 #include <exception>
 #include <new>
 
@@ -95,14 +96,12 @@ gssEapAttrProvidersFinalize(OM_uint32 *minor)
 }
 
 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);
@@ -110,13 +109,6 @@ gss_eap_attr_ctx::registerProvider(unsigned int type,
     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;
-    }
 }
 
 /*
@@ -128,8 +120,6 @@ gss_eap_attr_ctx::unregisterProvider(unsigned int type)
     assert(type <= ATTR_TYPE_MAX);
 
     gssEapAttrFactories[type] = NULL;
-    gssEapAttrPrefixes[type].value = NULL;
-    gssEapAttrPrefixes[type].length = 0;
 }
 
 /*
@@ -156,12 +146,22 @@ gss_eap_attr_ctx::gss_eap_attr_ctx(void)
  * Convert an attribute prefix to a type
  */
 unsigned int
-gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
+gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix) const
 {
     unsigned int i;
 
     for (i = ATTR_TYPE_MIN; i < ATTR_TYPE_MAX; i++) {
-        if (bufferEqual(&gssEapAttrPrefixes[i], prefix))
+        const char *pprefix;
+
+        if (!providerEnabled(i))
+            continue;
+
+        pprefix = m_providers[i]->prefix();
+        if (pprefix == NULL)
+            continue;
+
+        if (strlen(pprefix) == prefix->length &&
+            memcmp(pprefix, prefix->value, prefix->length) == 0)
             return i;
     }
 
@@ -171,13 +171,22 @@ gss_eap_attr_ctx::attributePrefixToType(const gss_buffer_t prefix)
 /*
  * Convert a type to an attribute prefix
  */
-const gss_buffer_t
-gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type)
+gss_buffer_desc
+gss_eap_attr_ctx::attributeTypeToPrefix(unsigned int type) const
 {
+    gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
+
     if (type < ATTR_TYPE_MIN || type >= ATTR_TYPE_MAX)
-        return GSS_C_NO_BUFFER;
+        return prefix;
+
+    if (!providerEnabled(type))
+        return prefix;
 
-    return &gssEapAttrPrefixes[type];
+    prefix.value = (void *)m_providers[type]->prefix();
+    if (prefix.value != NULL)
+        prefix.length = strlen((char *)prefix.value);
+
+    return prefix;
 }
 
 bool
@@ -265,86 +274,67 @@ gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
     return ret;
 }
 
-#define UPDATE_REMAIN(n)    do {                \
-        p += (n);                               \
-        remain -= (n);                          \
-    } while (0)
+static DDF
+findSourceForProvider(DDF &sources, const char *key)
+{
+    DDF source = sources.first();
 
-#define CHECK_REMAIN(n)     do {                \
-        if (remain < (n)) {                     \
-            return false;                       \
-        }                                       \
-    } while (0)
+    while (!source.isnull()) {
+        DDF obj = source.getmember(key);
+
+        if (strcmp(key, source.name()) == 0)
+            break;
+
+        source = sources.next();
+    }
+
+    return source;
+}
 
-/*
- * Initialize a context from an exported context or name token
- */
 bool
-gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
+gss_eap_attr_ctx::unmarshallAndInit(DDF &obj)
 {
     bool ret = false;
-    size_t remain = buffer->length;
-    unsigned char *p = (unsigned char *)buffer->value;
-    bool didInit[ATTR_TYPE_MAX + 1];
+    bool foundSource[ATTR_TYPE_MAX + 1];
     unsigned int type;
 
     for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++)
-        didInit[type] = false;
-
-    /* flags */
-    CHECK_REMAIN(4);
-    m_flags = load_uint32_be(p);
-    UPDATE_REMAIN(4);
-
-    while (remain) {
-        OM_uint32 type;
-        gss_buffer_desc providerToken;
-        gss_eap_attr_provider *provider;
-
-        /* TLV encoding of provider type, length, value */
-        CHECK_REMAIN(4);
-        type = load_uint32_be(p);
-        UPDATE_REMAIN(4);
+        foundSource[type] = false;
 
-        CHECK_REMAIN(4);
-        providerToken.length = load_uint32_be(p);
-        UPDATE_REMAIN(4);
+    if (obj["version"].integer() != 1)
+        return false;
 
-        CHECK_REMAIN(providerToken.length);
-        providerToken.value = p;
-        UPDATE_REMAIN(providerToken.length);
+    m_flags = obj["flags"].integer();
 
-        if (type < ATTR_TYPE_MIN || type > ATTR_TYPE_MAX ||
-            didInit[type])
-            return false;
+    DDF sources = obj["sources"];
 
+    /* Initialize providers from serialized state */
+    for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++) {
         if (!providerEnabled(type)) {
             releaseProvider(type);
             continue;
         }
 
-        provider = m_providers[type];
+        gss_eap_attr_provider *provider = m_providers[type];
+        const char *key = provider->marshallingKey();
+        if (key == NULL)
+            continue;
 
-        ret = provider->initFromBuffer(this, &providerToken);
-        if (ret == false) {
+        DDF source = findSourceForProvider(sources, key);
+        if (source.isnull() ||
+            !provider->unmarshallAndInit(this, source)) {
             releaseProvider(type);
-            break;
+            return false;
         }
-        didInit[type] = true;
-    }
 
-    if (ret == false)
-        return ret;
+        foundSource[type] = true;
+    }
 
-    /*
-     * The call the initFromGssContext methods for attribute
-     * providers that can initialize themselves from other
-     * providers.
-     */
+    /* Initialize remaining providers from initialized providers */ 
     for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++) {
         gss_eap_attr_provider *provider;
 
-        if (didInit[type])
+        if (foundSource[type] || !providerEnabled(type))
             continue;
 
         provider = m_providers[type];
@@ -354,10 +344,62 @@ gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
                                            GSS_C_NO_CONTEXT);
         if (ret == false) {
             releaseProvider(type);
-            break;
+            return false;
         }
     }
 
+    return true;
+}
+
+DDF
+gss_eap_attr_ctx::marshall(void) const
+{
+    DDF obj(NULL);
+    unsigned int i;
+
+    obj.addmember("version").integer(1);
+    obj.addmember("flags").integer(m_flags);
+
+    DDF sources = obj.addmember("sources").list();
+
+    for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
+        gss_eap_attr_provider *provider = m_providers[i];
+
+        if (provider == NULL)
+            continue; /* provider not initialised */
+
+        const char *key = provider->marshallingKey();
+        if (key == NULL)
+            continue; /* provider does not have state */
+
+        DDF source = provider->marshall();
+        sources.add(source.name(key));
+    }
+
+    return obj;
+}
+
+/*
+ * Initialize a context from an exported context or name token
+ */
+bool
+gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
+{
+    bool ret;
+
+    if (buffer->length == 0)
+        return false;
+
+    DDF obj(NULL);
+    std::string str((const char *)buffer->value, buffer->length);
+    std::istringstream source(str);
+
+    source >> obj;
+
+    ret = unmarshallAndInit(obj);
+
+    obj.destroy();
+
     return ret;
 }
 
@@ -378,19 +420,6 @@ gss_eap_attr_ctx::getProvider(unsigned int type) const
 }
 
 /*
- * Locate provider for a given prefix
- */
-gss_eap_attr_provider *
-gss_eap_attr_ctx::getProvider(const gss_buffer_t prefix) const
-{
-    unsigned int type;
-
-    type = attributePrefixToType(prefix);
-
-    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.
  */
@@ -475,7 +504,8 @@ struct eap_gss_get_attr_types_args {
 };
 
 static bool
-addAttribute(const gss_eap_attr_provider *provider GSSEAP_UNUSED,
+addAttribute(const gss_eap_attr_ctx *manager,
+             const gss_eap_attr_provider *provider GSSEAP_UNUSED,
              const gss_buffer_t attribute,
              void *data)
 {
@@ -484,7 +514,7 @@ addAttribute(const gss_eap_attr_provider *provider GSSEAP_UNUSED,
     OM_uint32 major, minor;
 
     if (args->type != ATTR_TYPE_LOCAL) {
-        gss_eap_attr_ctx::composeAttributeName(args->type, attribute, &qualified);
+        manager->composeAttributeName(args->type, attribute, &qualified);
         major = gss_add_buffer_set_member(&minor, &qualified, &args->attrs);
         gss_release_buffer(&minor, &qualified);
     } else {
@@ -605,51 +635,15 @@ gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
 void
 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
 {
-    OM_uint32 tmpMinor;
-    gss_buffer_desc providerTokens[ATTR_TYPE_MAX + 1];
-    size_t length = 4; /* m_flags */
-    unsigned char *p;
-    unsigned int i;
+    DDF obj = marshall();
+    std::ostringstream sink;
 
-    for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
-        providerTokens[i].length = 0;
-        providerTokens[i].value = NULL;
-    }
-
-    for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
-        gss_eap_attr_provider *provider = m_providers[i];
-
-        if (provider == NULL)
-            continue;
+    sink << obj;
+    std::string str = sink.str();
 
-        provider->exportToBuffer(&providerTokens[i]);
+    duplicateBuffer(str, buffer);
 
-        if (providerTokens[i].value != NULL)
-            length += 8 + providerTokens[i].length;
-    }
-
-    buffer->length = length;
-    buffer->value = GSSEAP_MALLOC(length);
-    if (buffer->value == NULL)
-        throw new std::bad_alloc;
-
-    p = (unsigned char *)buffer->value;
-    store_uint32_be(m_flags, p);
-    p += 4;
-
-    for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
-        if (providerTokens[i].value == NULL)
-            continue;
-
-        store_uint32_be(i, p);
-        p += 4;
-        store_uint32_be(providerTokens[i].length, p);
-        p += 4;
-        memcpy(p, providerTokens[i].value, providerTokens[i].length);
-        p += providerTokens[i].length;
-
-        gss_release_buffer(&tmpMinor, &providerTokens[i]);
-    }
+    obj.destroy();
 }
 
 /*
@@ -759,7 +753,7 @@ gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
 void
 gss_eap_attr_ctx::decomposeAttributeName(const gss_buffer_t attribute,
                                          unsigned int *type,
-                                         gss_buffer_t suffix)
+                                         gss_buffer_t suffix) const
 {
     gss_buffer_desc prefix = GSS_C_EMPTY_BUFFER;
 
@@ -796,9 +790,9 @@ std::string
 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
                                        const gss_buffer_t suffix)
 {
-    const gss_buffer_t prefix = attributeTypeToPrefix(type);
+    gss_buffer_desc prefix = attributeTypeToPrefix(type);
 
-    return composeAttributeName(prefix, suffix);
+    return composeAttributeName(&prefix, suffix);
 }
 
 /*
@@ -825,11 +819,11 @@ gss_eap_attr_ctx::composeAttributeName(const gss_buffer_t prefix,
 void
 gss_eap_attr_ctx::composeAttributeName(unsigned int type,
                                        const gss_buffer_t suffix,
-                                       gss_buffer_t attribute)
+                                       gss_buffer_t attribute) const
 {
-    gss_buffer_t prefix = attributeTypeToPrefix(type);
+    gss_buffer_desc prefix = attributeTypeToPrefix(type);
 
-    return composeAttributeName(prefix, suffix, attribute);
+    return composeAttributeName(&prefix, suffix, attribute);
 }
 
 /*