use JSON instead of DDF marshalling
[mech_eap.git] / util_attr.cpp
index a24064a..28e6356 100644 (file)
@@ -38,7 +38,9 @@
 
 #include <typeinfo>
 #include <string>
+#include <sstream>
 #include <exception>
+#include <stdexcept>
 #include <new>
 
 /* lazy initialisation */
@@ -95,14 +97,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 +110,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 +121,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 +147,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 +172,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,49 +275,138 @@ gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
     return ret;
 }
 
-/*
- * 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::initWithJsonObject(json_t *obj)
 {
-    bool ret;
-    gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
-    gss_buffer_desc primaryBuf;
+    bool ret = false;
+    bool foundSource[ATTR_TYPE_MAX + 1];
+    unsigned int type;
+    json_t *sources;
 
-    if (buffer->length < 4)
-        return false;
+    for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++)
+        foundSource[type] = false;
 
-    m_flags = load_uint32_be(buffer->value);
+    if (json_integer_value(json_object_get(obj, "version")) != 1)
+        return false;
 
-    primaryBuf.length = buffer->length - 4;
-    primaryBuf.value = (char *)buffer->value + 4;
+    m_flags = json_integer_value(json_object_get(obj, "flags"));
 
-    ret = primaryProvider->initFromBuffer(this, &primaryBuf);
-    if (ret == false)
-        return ret;
+    sources = json_object_get(obj, "sources");
 
-    for (unsigned int i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
+    /* Initialize providers from serialized state */
+    for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++) {
         gss_eap_attr_provider *provider;
+        const char *key;
+        json_t *source;
 
-        if (!providerEnabled(i)) {
-            releaseProvider(i);
+        if (!providerEnabled(type)) {
+            releaseProvider(type);
             continue;
         }
 
-        provider = m_providers[i];
-        if (provider == primaryProvider)
+        provider = m_providers[type];
+        key = provider->name();
+        if (key == NULL)
             continue;
 
+        source = json_object_get(sources, key);
+        if (source != NULL &&
+            !provider->initWithJsonObject(this, source)) {
+            releaseProvider(type);
+            return false;
+        }
+
+        foundSource[type] = true;
+    }
+
+    /* Initialize remaining providers from initialized providers */
+    for (type = ATTR_TYPE_MIN; type <= ATTR_TYPE_MAX; type++) {
+        gss_eap_attr_provider *provider;
+
+        if (foundSource[type] || !providerEnabled(type))
+            continue;
+
+        provider = m_providers[type];
+
         ret = provider->initFromGssContext(this,
                                            GSS_C_NO_CREDENTIAL,
                                            GSS_C_NO_CONTEXT);
         if (ret == false) {
-            releaseProvider(i);
-            break;
+            releaseProvider(type);
+            return false;
         }
     }
 
+    return true;
+}
+
+json_t *
+gss_eap_attr_ctx::jsonRepresentation(void) const
+{
+    json_t *obj, *sources;
+    unsigned int i;
+
+    obj = json_object();
+    if (obj == NULL) {
+        throw new std::bad_alloc;
+    }
+
+    /* FIXME check json_object_set_new return value */
+    json_object_set_new(obj, "version", json_integer(1));
+    json_object_set_new(obj, "flags", json_integer(m_flags));
+
+    sources = json_object();
+    if (sources == NULL) {
+        json_decref(obj);
+        throw new std::bad_alloc;
+    }
+
+    for (i = ATTR_TYPE_MIN; i <= ATTR_TYPE_MAX; i++) {
+        gss_eap_attr_provider *provider;
+        const char *key;
+
+        provider = m_providers[i];
+        if (provider == NULL)
+            continue; /* provider not initialised */
+
+        key = provider->name();
+        if (key == NULL)
+            continue; /* provider does not have state */
+
+        json_t *source = provider->jsonRepresentation();
+        json_object_set_new(sources, key, source);
+    }
+
+    json_object_set_new(obj, "sources", sources);
+
+    return obj;
+}
+
+/*
+ * Initialize a context from an exported context or name token
+ */
+bool
+gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
+{
+    OM_uint32 major, minor;
+    bool ret;
+    char *s;
+    json_error_t error;
+    json_t *obj;
+
+    major = bufferToString(&minor, buffer, &s);
+    if (GSS_ERROR(major))
+        return false;
+
+    obj = json_loads(s, 0, &error);
+    if (obj != NULL) {
+        ret = initWithJsonObject(obj);
+        json_decref(obj);
+    } else
+        ret = false;
+
+    GSSEAP_FREE(s);
+
     return ret;
 }
 
@@ -328,19 +427,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.
  */
@@ -425,7 +511,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)
 {
@@ -434,7 +521,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 {
@@ -456,10 +543,8 @@ gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
     unsigned int i;
 
     major = gss_create_empty_buffer_set(&minor, attrs);
-    if (GSS_ERROR(major)) {
+    if (GSS_ERROR(major))
         throw new std::bad_alloc;
-        return false;
-    }
 
     args.attrs = *attrs;
 
@@ -555,23 +640,33 @@ gss_eap_attr_ctx::releaseAnyNameMapping(gss_buffer_t type_id,
 void
 gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
 {
-    const gss_eap_attr_provider *primaryProvider = getPrimaryProvider();
-    gss_buffer_desc tmp;
-    unsigned char *p;
-    OM_uint32 tmpMinor;
+    OM_uint32 minor;
+    json_t *obj;
+    char *s;
+
+    obj = jsonRepresentation();
+    if (obj == NULL) {
+        std::string error("gss_eap_attr_ctx::exportToBuffer::jsonRepresentation");
+        throw new std::runtime_error(error); /* XXX */
+    }
+
+#if 0
+    json_dumpf(obj, stdout, JSON_INDENT(3));
+#endif
 
-    primaryProvider->exportToBuffer(&tmp);
+    s = json_dumps(obj, JSON_COMPACT);
+    if (s == NULL) {
+        json_decref(obj);
+        std::string error("gss_eap_attr_ctx::exportToBuffer: json_dumps");
+        throw new std::runtime_error(error); /* XXX */
+    }
 
-    buffer->length = 4 + tmp.length;
-    buffer->value = GSSEAP_MALLOC(buffer->length);
-    if (buffer->value == NULL)
+    if (GSS_ERROR(makeStringBuffer(&minor, s, buffer))) {
+        json_decref(obj);
         throw new std::bad_alloc;
+    }
 
-    p = (unsigned char *)buffer->value;
-    store_uint32_be(m_flags, p);
-    memcpy(p + 4, tmp.value, tmp.length);
-
-    gss_release_buffer(&tmpMinor, &tmp);
+    json_decref(obj);
 }
 
 /*
@@ -681,7 +776,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;
 
@@ -718,9 +813,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);
 }
 
 /*
@@ -747,11 +842,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);
 }
 
 /*