fix exception to GSS error code mapping
[mech_eap.orig] / util_radius.cpp
index 4e2f6e0..09f4559 100644 (file)
@@ -189,7 +189,7 @@ copyAvps(const VALUE_PAIR *src)
         vpcopy = paircopyvp(vp);
         if (vpcopy == NULL) {
             pairfree(&dst);
-            throw new std::bad_alloc;
+            throw std::bad_alloc();
             return NULL;
         }
         *pDst = vpcopy;
@@ -619,18 +619,10 @@ gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
     return GSS_S_COMPLETE;
 }
 
-static json_t *
+static JSONObject
 avpToJson(const VALUE_PAIR *vp)
 {
-    json_t *obj = json_object();
-
-    if (obj == NULL) {
-        throw new std::bad_alloc;
-        return NULL;
-    }
-
-    /* FIXME check json_object_set_new return value */
-    json_object_set_new(obj, "type", json_integer(vp->attribute));
+    JSONObject obj;
 
     assert(vp->length <= MAX_STRING_LEN);
 
@@ -638,50 +630,51 @@ avpToJson(const VALUE_PAIR *vp)
     case PW_TYPE_INTEGER:
     case PW_TYPE_IPADDR:
     case PW_TYPE_DATE:
-        json_object_set_new(obj, "value", json_integer(vp->lvalue));
+        obj.set("value", vp->lvalue);
         break;
     case PW_TYPE_STRING:
-        json_object_set_new(obj, "value", json_string(vp->vp_strvalue));
+        obj.set("value", vp->vp_strvalue);
         break;
     default: {
         char *b64;
 
-        if (base64Encode(vp->vp_octets, vp->length, &b64) < 0) {
-            json_decref(obj);
-            throw new std::bad_alloc;
-        }
+        if (base64Encode(vp->vp_octets, vp->length, &b64) < 0)
+            throw std::bad_alloc();
 
-        json_object_set_new(obj, "value", json_string(b64));
+        obj.set("value", b64);
         GSSEAP_FREE(b64);
         break;
     }
     }
 
+    obj.set("type", vp->attribute);
+
     return obj;
 }
 
 static bool
-jsonToAvp(VALUE_PAIR **pVp, json_t *obj)
+jsonToAvp(VALUE_PAIR **pVp, JSONObject &obj)
 {
     VALUE_PAIR *vp = NULL;
     DICT_ATTR *da;
     uint32_t attrid;
-    json_t *type, *value;
 
-    type = json_object_get(obj, "type");
-    value = json_object_get(obj, "value");
-    if (type == NULL || value == NULL)
+    JSONObject type = obj["type"];
+    JSONObject value = obj["value"];
+
+    if (!type.isInteger())
         goto fail;
 
-    attrid = json_integer_value(type);
+    attrid = type.integer();
     da = dict_attrbyvalue(attrid);
     if (da != NULL) {
         vp = pairalloc(da);
     } else {
-        vp = paircreate(attrid, PW_TYPE_STRING);
+        /* Assume unknown attributes are octet strings */
+        vp = paircreate(attrid, PW_TYPE_OCTETS);
     }
     if (vp == NULL) {
-        throw new std::bad_alloc;
+        throw std::bad_alloc();
         goto fail;
     }
 
@@ -689,14 +682,20 @@ jsonToAvp(VALUE_PAIR **pVp, json_t *obj)
     case PW_TYPE_INTEGER:
     case PW_TYPE_IPADDR:
     case PW_TYPE_DATE:
+        if (!value.isInteger())
+            goto fail;
+
         vp->length = 4;
-        vp->lvalue = json_integer_value(value);
+        vp->lvalue = value.integer();
         break;
     case PW_TYPE_STRING: {
-        const char *str = json_string_value(value);
-        size_t len;
+        if (!value.isString())
+            goto fail;
+
+        const char *str = value.string();
+        size_t len = strlen(str);
 
-        if (str == NULL || (len = strlen(str)) >= MAX_STRING_LEN)
+        if (len >= MAX_STRING_LEN)
             goto fail;
 
         vp->length = len;
@@ -705,20 +704,33 @@ jsonToAvp(VALUE_PAIR **pVp, json_t *obj)
     }
     case PW_TYPE_OCTETS:
     default: {
-        const char *str = json_string_value(value);
-        int len;
-
-        /* this optimization requires base64Decode only understand packed encoding */
-        if (str == NULL ||
-            strlen(str) >= BASE64_EXPAND(MAX_STRING_LEN))
+        if (!value.isString())
             goto fail;
 
-        len = base64Decode(str, vp->vp_octets);
-        if (len < 0)
+        const char *str = value.string();
+        size_t stringLen = strlen(str);
+
+        /* this optimization requires base64Decode only understand packed encoding */
+        if (stringLen >= BASE64_EXPAND(MAX_STRING_LEN))
             goto fail;
 
-        vp->length = len;
-        vp->vp_octets[len] = '\0';
+        /*
+         * If the attribute is unknown, we don't know its syntax; assume
+         * it is an octet string and, if that fails to decode and will
+         * fit, a string.
+         */
+        size_t valueLen = base64Decode(str, vp->vp_octets);
+        if (valueLen < 0) {
+            if (da == NULL && stringLen < MAX_STRING_LEN) {
+                vp->type = PW_TYPE_STRING;
+                vp->length = stringLen;
+                memcpy(vp->vp_strvalue, str, stringLen + 1);
+            } else
+                goto fail;
+        } else {
+            vp->length = valueLen;
+            vp->vp_octets[valueLen] = '\0';
+        }
         break;
     }
     }
@@ -742,19 +754,18 @@ gss_eap_radius_attr_provider::name(void) const
 
 bool
 gss_eap_radius_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
-                                                json_t *obj)
+                                                 JSONObject &obj)
 {
     VALUE_PAIR **pNext = &m_vps;
-    json_t *attrs;
-    size_t i;
 
     if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
         return false;
 
-    attrs = json_object_get(obj, "attributes");
+    JSONObject attrs = obj["attributes"];
+    size_t nelems = attrs.size();
 
-    for (i = 0; i < json_array_size(attrs); i++) {
-        json_t *attr = json_array_get(attrs, i);
+    for (size_t i = 0; i < nelems; i++) {
+        JSONObject attr = attrs[i];
         VALUE_PAIR *vp;
 
         if (!jsonToAvp(&vp, attr))
@@ -764,6 +775,8 @@ gss_eap_radius_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
         pNext = &vp->next;
     }
 
+    m_authenticated = obj["authenticated"].integer();
+
     return true;
 }
 
@@ -773,27 +786,19 @@ gss_eap_radius_attr_provider::prefix(void) const
     return "urn:ietf:params:gss-eap:radius-avp";
 }
 
-json_t *
+JSONObject
 gss_eap_radius_attr_provider::jsonRepresentation(void) const
 {
-    json_t *obj, *attrs;
-
-    attrs = json_array();
-    if (attrs == NULL)
-        throw new std::bad_alloc;
+    JSONObject obj, attrs = JSONObject::array();
 
     for (VALUE_PAIR *vp = m_vps; vp != NULL; vp = vp->next) {
-        json_t *attr = avpToJson(vp);
-        json_array_append_new(attrs, attr);
+        JSONObject attr = avpToJson(vp);
+        attrs.append(attr);
     }
 
-    obj = json_object();
-    if (obj == NULL) {
-        json_decref(attrs);
-        throw new std::bad_alloc;
-    }
+    obj.set("attributes", attrs);
 
-    json_object_set_new(obj, "attributes", attrs);
+    obj.set("authenticated", m_authenticated);
 
     return obj;
 }