refactory: s/initFrom/initWith/g
[moonshot.git] / mech_eap / util_radius.cpp
index 1d5efab..d1ee24b 100644 (file)
@@ -63,12 +63,12 @@ gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
 }
 
 bool
-gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
+gss_eap_radius_attr_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
                                                       const gss_eap_attr_provider *ctx)
 {
     const gss_eap_radius_attr_provider *radius;
 
-    if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
+    if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx))
         return false;
 
     radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
@@ -82,11 +82,11 @@ gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *ma
 }
 
 bool
-gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
+gss_eap_radius_attr_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
                                                  const gss_cred_id_t gssCred,
                                                  const gss_ctx_id_t gssCtx)
 {
-    if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
+    if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
         return false;
 
     if (gssCtx != GSS_C_NO_CONTEXT) {
@@ -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;
@@ -639,7 +639,7 @@ avpToJson(const VALUE_PAIR *vp)
         char *b64;
 
         if (base64Encode(vp->vp_octets, vp->length, &b64) < 0)
-            throw new std::bad_alloc;
+            throw std::bad_alloc();
 
         obj.set("value", b64);
         GSSEAP_FREE(b64);
@@ -661,7 +661,8 @@ jsonToAvp(VALUE_PAIR **pVp, JSONObject &obj)
 
     JSONObject type = obj["type"];
     JSONObject value = obj["value"];
-    if (type.isnull() || value.isnull())
+
+    if (!type.isInteger())
         goto fail;
 
     attrid = type.integer();
@@ -669,10 +670,11 @@ jsonToAvp(VALUE_PAIR **pVp, JSONObject &obj)
     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;
     }
 
@@ -680,14 +682,20 @@ jsonToAvp(VALUE_PAIR **pVp, JSONObject &obj)
     case PW_TYPE_INTEGER:
     case PW_TYPE_IPADDR:
     case PW_TYPE_DATE:
+        if (!value.isInteger())
+            goto fail;
+
         vp->length = 4;
         vp->lvalue = value.integer();
         break;
     case PW_TYPE_STRING: {
+        if (!value.isString())
+            goto fail;
+
         const char *str = value.string();
-        size_t len;
+        size_t len = strlen(str);
 
-        if (str == NULL || (len = strlen(str)) >= MAX_STRING_LEN)
+        if (len >= MAX_STRING_LEN)
             goto fail;
 
         vp->length = len;
@@ -696,20 +704,33 @@ jsonToAvp(VALUE_PAIR **pVp, JSONObject &obj)
     }
     case PW_TYPE_OCTETS:
     default: {
+        if (!value.isString())
+            goto fail;
+
         const char *str = value.string();
-        int len;
+        size_t stringLen = strlen(str);
 
         /* this optimization requires base64Decode only understand packed encoding */
-        if (str == NULL ||
-            strlen(str) >= BASE64_EXPAND(MAX_STRING_LEN))
-            goto fail;
-
-        len = base64Decode(str, vp->vp_octets);
-        if (len < 0)
+        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;
     }
     }