try to guard against multiple shibboleth library initializations
[mech_eap.orig] / util_attr.cpp
index 9868958..01c3135 100644 (file)
@@ -40,7 +40,6 @@
 #include <string>
 #include <sstream>
 #include <exception>
-#include <stdexcept>
 #include <new>
 
 /* lazy initialisation */
@@ -220,7 +219,7 @@ gss_eap_attr_ctx::releaseProvider(unsigned int type)
  * Initialize a context from an existing context.
  */
 bool
-gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
+gss_eap_attr_ctx::initWithExistingContext(const gss_eap_attr_ctx *manager)
 {
     bool ret = true;
 
@@ -236,7 +235,7 @@ gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
 
         provider = m_providers[i];
 
-        ret = provider->initFromExistingContext(this,
+        ret = provider->initWithExistingContext(this,
                                                 manager->m_providers[i]);
         if (ret == false) {
             releaseProvider(i);
@@ -251,7 +250,7 @@ gss_eap_attr_ctx::initFromExistingContext(const gss_eap_attr_ctx *manager)
  * Initialize a context from a GSS credential and context.
  */
 bool
-gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
+gss_eap_attr_ctx::initWithGssContext(const gss_cred_id_t cred,
                                      const gss_ctx_id_t ctx)
 {
     bool ret = true;
@@ -271,7 +270,7 @@ gss_eap_attr_ctx::initFromGssContext(const gss_cred_id_t cred,
 
         provider = m_providers[i];
 
-        ret = provider->initFromGssContext(this, cred, ctx);
+        ret = provider->initWithGssContext(this, cred, ctx);
         if (ret == false) {
             releaseProvider(i);
             break;
@@ -314,7 +313,7 @@ gss_eap_attr_ctx::initWithJsonObject(JSONObject &obj)
             continue;
 
         JSONObject source = sources.get(key);
-        if (!source.isnull() &&
+        if (!source.isNull() &&
             !provider->initWithJsonObject(this, source)) {
             releaseProvider(type);
             return false;
@@ -332,7 +331,7 @@ gss_eap_attr_ctx::initWithJsonObject(JSONObject &obj)
 
         provider = m_providers[type];
 
-        ret = provider->initFromGssContext(this,
+        ret = provider->initWithGssContext(this,
                                            GSS_C_NO_CREDENTIAL,
                                            GSS_C_NO_CONTEXT);
         if (ret == false) {
@@ -378,7 +377,7 @@ gss_eap_attr_ctx::jsonRepresentation(void) const
  * 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::initWithBuffer(const gss_buffer_t buffer)
 {
     OM_uint32 major, minor;
     bool ret;
@@ -390,7 +389,7 @@ gss_eap_attr_ctx::initFromBuffer(const gss_buffer_t buffer)
         return false;
 
     JSONObject obj = JSONObject::load(s, 0, &error);
-    if (!obj.isnull()) {
+    if (!obj.isNull()) {
         ret = initWithJsonObject(obj);
     } else
         ret = false;
@@ -534,7 +533,7 @@ gss_eap_attr_ctx::getAttributeTypes(gss_buffer_set_t *attrs)
 
     major = gss_create_empty_buffer_set(&minor, attrs);
     if (GSS_ERROR(major))
-        throw new std::bad_alloc;
+        throw std::bad_alloc();
 
     args.attrs = *attrs;
 
@@ -636,13 +635,13 @@ gss_eap_attr_ctx::exportToBuffer(gss_buffer_t buffer) const
     JSONObject obj = jsonRepresentation();
 
 #if 0
-    obj.dump(stdout, JSON_INDENT(3));
+    obj.dump(stdout);
 #endif
 
     s = obj.dump(JSON_COMPACT);
 
     if (GSS_ERROR(makeStringBuffer(&minor, s, buffer)))
-        throw new std::bad_alloc;
+        throw std::bad_alloc();
 }
 
 /*
@@ -679,11 +678,15 @@ gss_eap_attr_ctx::mapException(OM_uint32 *minor, std::exception &e) const
     OM_uint32 major;
 
     /* Errors we handle ourselves */
-    major = GSS_S_FAILURE;
-
     if (typeid(e) == typeid(std::bad_alloc)) {
+        major = GSS_S_FAILURE;
         *minor = ENOMEM;
         goto cleanup;
+    } else if (typeid(e) == typeid(JSONException)) {
+        major = GSS_S_BAD_NAME;
+        *minor = GSSEAP_BAD_ATTR_TOKEN;
+        gssEapSaveStatusInfo(*minor, "%s", e.what());
+        goto cleanup;
     }
 
     /* Errors we delegate to providers */
@@ -706,11 +709,6 @@ gss_eap_attr_ctx::mapException(OM_uint32 *minor, std::exception &e) const
     }
 
 cleanup:
-#if 0
-    /* rethrow for now for debugging */
-    throw e;
-#endif
-
     assert(GSS_ERROR(major));
 
     return major;
@@ -878,8 +876,10 @@ gssEapGetNameAttribute(OM_uint32 *minor,
                        gss_buffer_t display_value,
                        int *more)
 {
-    *authenticated = 0;
-    *complete = 0;
+    if (authenticated != NULL)
+        *authenticated = 0;
+    if (complete != NULL)
+        *complete = 0;
 
     if (value != NULL) {
         value->length = 0;
@@ -1001,29 +1001,38 @@ gssEapImportAttrContext(OM_uint32 *minor,
                         gss_name_t name)
 {
     gss_eap_attr_ctx *ctx = NULL;
+    OM_uint32 major = GSS_S_FAILURE;
 
     assert(name->attrCtx == NULL);
 
     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
         return GSS_S_UNAVAILABLE;
 
-    if (buffer->length != 0) {
-        try {
-            ctx = new gss_eap_attr_ctx();
+    if (buffer->length == 0)
+        return GSS_S_COMPLETE;
 
-            if (!ctx->initFromBuffer(buffer)) {
-                delete ctx;
-                *minor = GSSEAP_BAD_ATTR_TOKEN;
-                return GSS_S_DEFECTIVE_TOKEN;
-            }
+    try {
+        ctx = new gss_eap_attr_ctx();
+
+        if (ctx->initWithBuffer(buffer)) {
             name->attrCtx = ctx;
-        } catch (std::exception &e) {
-            delete ctx;
-            return name->attrCtx->mapException(minor, e);
+            major = GSS_S_COMPLETE;
+            *minor = 0;
+        } else {
+            major = GSS_S_BAD_NAME;
+            *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
         }
+    } catch (std::exception &e) {
+        if (ctx != NULL)
+            major = ctx->mapException(minor, e);
     }
 
-    return GSS_S_COMPLETE;
+    assert(major == GSS_S_COMPLETE || name->attrCtx == NULL);
+
+    if (GSS_ERROR(major))
+        delete ctx;
+
+    return major;
 }
 
 OM_uint32
@@ -1032,27 +1041,38 @@ gssEapDuplicateAttrContext(OM_uint32 *minor,
                            gss_name_t out)
 {
     gss_eap_attr_ctx *ctx = NULL;
+    OM_uint32 major = GSS_S_FAILURE;
 
     assert(out->attrCtx == NULL);
 
+    if (in->attrCtx == NULL) {
+        *minor = 0;
+        return GSS_S_COMPLETE;
+    }
+
     if (GSS_ERROR(gssEapAttrProvidersInit(minor)))
         return GSS_S_UNAVAILABLE;
 
     try {
-        if (in->attrCtx != NULL) {
-            ctx = new gss_eap_attr_ctx();
-            if (!ctx->initFromExistingContext(in->attrCtx)) {
-                delete ctx;
-                *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
-                return GSS_S_FAILURE;
-            }
+        ctx = new gss_eap_attr_ctx();
+
+        if (ctx->initWithExistingContext(in->attrCtx)) {
             out->attrCtx = ctx;
+            major = GSS_S_COMPLETE;
+            *minor = 0;
+        } else {
+            major = GSS_S_FAILURE;
+            *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
         }
     } catch (std::exception &e) {
-        delete ctx;
-        return in->attrCtx->mapException(minor, e);
+        major = in->attrCtx->mapException(minor, e);
     }
 
+    assert(major == GSS_S_COMPLETE || out->attrCtx == NULL);
+
+    if (GSS_ERROR(major))
+        delete ctx;
+
     return GSS_S_COMPLETE;
 }
 
@@ -1132,27 +1152,30 @@ gssEapCreateAttrContext(OM_uint32 *minor,
 
     assert(gssCtx != GSS_C_NO_CONTEXT);
 
+    *pAttrContext = NULL;
+
     major = gssEapAttrProvidersInit(minor);
     if (GSS_ERROR(major))
         return major;
 
-    *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
-    major = GSS_S_FAILURE;
-
     try {
+        /* Set *pAttrContext here to for reentrancy */
         *pAttrContext = ctx = new gss_eap_attr_ctx();
-        if (ctx->initFromGssContext(gssCred, gssCtx)) {
-            *minor = 0;
+
+        if (ctx->initWithGssContext(gssCred, gssCtx)) {
+            *pExpiryTime = ctx->getExpiryTime();
             major = GSS_S_COMPLETE;
+            *minor = 0;
+        } else {
+            major = GSS_S_FAILURE;
+            *minor = GSSEAP_ATTR_CONTEXT_FAILURE;
         }
     } catch (std::exception &e) {
         if (ctx != NULL)
             major = ctx->mapException(minor, e);
     }
 
-    if (major == GSS_S_COMPLETE) {
-        *pExpiryTime = ctx->getExpiryTime();
-    } else {
+    if (GSS_ERROR(major)) {
         delete ctx;
         *pAttrContext = NULL;
     }