cleanup unused parameter warnings
[mech_eap.git] / util_radius.cpp
index 0237eed..083b3e7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, JANET(UK)
+ * Copyright (c) 2011, JANET(UK)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * SUCH DAMAGE.
  */
 
+/*
+ * RADIUS attribute provider implementation.
+ */
+
 #include "gssapiP_eap.h"
 
 /* stuff that should be provided by libradsec/libfreeradius-radius */
@@ -116,14 +120,14 @@ alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
 static bool
 isSecretAttributeP(uint16_t attrid, uint16_t vendor)
 {
-    bool ret = false;
+    bool bSecretAttribute = false;
 
     switch (vendor) {
     case VENDORPEC_MS:
         switch (attrid) {
         case PW_MS_MPPE_SEND_KEY:
         case PW_MS_MPPE_RECV_KEY:
-            ret = true;
+            bSecretAttribute = true;
             break;
         default:
             break;
@@ -132,7 +136,7 @@ isSecretAttributeP(uint16_t attrid, uint16_t vendor)
         break;
     }
 
-    return ret;
+    return bSecretAttribute;
 }
 
 static bool
@@ -142,28 +146,28 @@ isSecretAttributeP(uint32_t attribute)
 }
 
 static bool
-isHiddenAttributeP(uint16_t attrid, uint16_t vendor)
+isInternalAttributeP(uint16_t attrid, uint16_t vendor)
 {
-    bool ret = false;
+    bool bInternalAttribute = false;
 
     /* should have been filtered */
     assert(!isSecretAttributeP(attrid, vendor));
 
     switch (vendor) {
     case VENDORPEC_UKERNA:
-        ret = true;
+        bInternalAttribute = true;
         break;
     default:
         break;
     }
 
-    return ret;
+    return bInternalAttribute;
 }
 
 static bool
-isHiddenAttributeP(uint32_t attribute)
+isInternalAttributeP(uint32_t attribute)
 {
-    return isHiddenAttributeP(ATTRID(attribute), VENDOR(attribute));
+    return isInternalAttributeP(ATTRID(attribute), VENDOR(attribute));
 }
 
 /*
@@ -196,7 +200,8 @@ copyAvps(const VALUE_PAIR *src)
 }
 
 bool
-gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
+gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
+                                                void *data) const
 {
     VALUE_PAIR *vp;
     std::vector <std::string> seen;
@@ -205,7 +210,8 @@ gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addA
         gss_buffer_desc attribute;
         char attrid[64];
 
-        if (isHiddenAttributeP(vp->attribute))
+        /* Don't advertise attributes that are internal to the GSS-EAP mechanism */
+        if (isInternalAttributeP(vp->attribute))
             continue;
 
         if (alreadyAddedAttributeP(seen, vp))
@@ -226,16 +232,89 @@ gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addA
     return true;
 }
 
-void
+uint32_t
+getAttributeId(const gss_buffer_t attr)
+{
+    OM_uint32 tmpMinor;
+    gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
+    DICT_ATTR *da;
+    char *s;
+    uint32_t attrid = 0;
+
+    if (attr->length < radiusUrnPrefix.length ||
+        memcmp(attr->value, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
+        return 0;
+
+    /* need to duplicate because attr may not be NUL terminated */
+    duplicateBuffer(*attr, &strAttr);
+    s = (char *)strAttr.value + radiusUrnPrefix.length;
+
+    if (isdigit(*s)) {
+        attrid = strtoul(s, NULL, 10);
+    } else {
+        da = dict_attrbyname(s);
+        if (da != NULL)
+            attrid = da->attr;
+    }
+
+    gss_release_buffer(&tmpMinor, &strAttr);
+
+    return attrid;
+}
+
+bool
+gss_eap_radius_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
+                                           uint32_t attrid,
+                                           const gss_buffer_t value)
+{
+    OM_uint32 major = GSS_S_UNAVAILABLE, minor;
+
+    if (!isSecretAttributeP(attrid) &&
+        !isInternalAttributeP(attrid)) {
+        deleteAttribute(attrid);
+
+        major = gssEapRadiusAddAvp(&minor, &m_vps,
+                                   ATTRID(attrid), VENDOR(attrid), 
+                                   value);
+    }
+
+    return !GSS_ERROR(major);
+}
+
+bool
 gss_eap_radius_attr_provider::setAttribute(int complete,
                                            const gss_buffer_t attr,
                                            const gss_buffer_t value)
 {
+    uint32_t attrid = getAttributeId(attr);
+
+    if (!attrid)
+        return false;
+
+    return setAttribute(complete, attrid, value);
 }
 
-void
-gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
+bool
+gss_eap_radius_attr_provider::deleteAttribute(uint32_t attrid)
+{
+    if (isSecretAttributeP(attrid) || isInternalAttributeP(attrid) ||
+        pairfind(m_vps, attrid) == NULL)
+        return false;
+
+    pairdelete(&m_vps, attrid);
+
+    return true;
+}
+
+bool
+gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t attr)
 {
+    uint32_t attrid = getAttributeId(attr);
+
+    if (!attrid)
+        return false;
+
+    return deleteAttribute(attrid);
 }
 
 bool
@@ -246,34 +325,12 @@ gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
                                            gss_buffer_t display_value,
                                            int *more) const
 {
-    OM_uint32 tmpMinor;
-    gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
-    DICT_ATTR *da;
     uint32_t attrid;
-    char *s;
 
-    duplicateBuffer(*attr, &strAttr);
-    s = (char *)strAttr.value;
-
-    if (attr->length < radiusUrnPrefix.length ||
-        memcmp(s, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
+    attrid = getAttributeId(attr);
+    if (!attrid)
         return false;
 
-    s += radiusUrnPrefix.length;
-
-    if (isdigit(*s)) {
-        attrid = strtoul(s, NULL, 10);
-    } else {
-        da = dict_attrbyname(s);
-        if (da == NULL) {
-            gss_release_buffer(&tmpMinor, &strAttr);
-            return false;
-        }
-        attrid = da->attr;
-    }
-
-    gss_release_buffer(&tmpMinor, &strAttr);
-
     return getAttribute(attrid, authenticated, complete,
                         value, display_value, more);
 }
@@ -291,9 +348,6 @@ gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
 
     *more = 0;
 
-    if (isHiddenAttributeP(attrid))
-        return false;
-
     if (i == -1)
         i = 0;
 
@@ -374,7 +428,7 @@ gss_eap_radius_attr_provider::getAttribute(uint16_t attribute,
 
 gss_any_t
 gss_eap_radius_attr_provider::mapToAny(int authenticated,
-                                       gss_buffer_t type_id) const
+                                       gss_buffer_t type_id GSSEAP_UNUSED) const
 {
     if (authenticated && !m_authenticated)
         return (gss_any_t)NULL;
@@ -383,7 +437,7 @@ gss_eap_radius_attr_provider::mapToAny(int authenticated,
 }
 
 void
-gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
+gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
                                                     gss_any_t input) const
 {
     pairfree((VALUE_PAIR **)&input);
@@ -392,9 +446,25 @@ gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
 bool
 gss_eap_radius_attr_provider::init(void)
 {
+    struct rs_context *radContext;
+
     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
                                        "urn:ietf:params:gss-eap:radius-avp",
-                                       gss_eap_radius_attr_provider::createAttrContext);
+                                       createAttrContext);
+
+#if 1
+    /*
+     * This hack is necessary in order to force the loading of the global
+     * dictionary, otherwise accepting reauthentication tokens fails unless
+     * the acceptor has already accepted a normal authentication token.
+     */
+    if (rs_context_create(&radContext, RS_DICT_FILE) != 0) {
+        return false;
+    }
+
+    rs_context_destroy(radContext);
+#endif
+
     return true;
 }
 
@@ -415,7 +485,7 @@ gssEapRadiusAddAvp(OM_uint32 *minor,
                    VALUE_PAIR **vps,
                    uint16_t attribute,
                    uint16_t vendor,
-                   gss_buffer_t buffer)
+                   const gss_buffer_t buffer)
 {
     uint32_t attrid = VENDORATTR(vendor, attribute);
     unsigned char *p = (unsigned char *)buffer->value;
@@ -425,8 +495,12 @@ gssEapRadiusAddAvp(OM_uint32 *minor,
         VALUE_PAIR *vp;
         size_t n = remain;
 
-        if (n > MAX_STRING_LEN)
-            n = MAX_STRING_LEN;
+       /*
+         * There's an extra byte of padding; RADIUS AVPs can only
+         * be 253 octets.
+         */
+        if (n >= MAX_STRING_LEN)
+            n = MAX_STRING_LEN - 1;
 
         vp = paircreate(attrid, PW_TYPE_OCTETS);
         if (vp == NULL) {
@@ -456,8 +530,12 @@ gssEapRadiusGetRawAvp(OM_uint32 *minor,
     uint32_t attr = VENDORATTR(vendor, attribute);
 
     *vp = pairfind(vps, attr);
+    if (*vp == NULL) {
+        *minor = GSSEAP_NO_SUCH_ATTR;
+        return GSS_S_UNAVAILABLE;
+    }
 
-    return (*vp == NULL) ? GSS_S_UNAVAILABLE : GSS_S_COMPLETE;
+    return GSS_S_COMPLETE;
 }
 
 OM_uint32
@@ -476,8 +554,10 @@ gssEapRadiusGetAvp(OM_uint32 *minor,
     buffer->value = NULL;
 
     vp = pairfind(vps, attr);
-    if (vp == NULL)
+    if (vp == NULL) {
+        *minor = GSSEAP_NO_SUCH_ATTR;
         return GSS_S_UNAVAILABLE;
+    }
 
     do {
         buffer->length += vp->length;
@@ -514,14 +594,20 @@ gssEapRadiusFreeAvps(OM_uint32 *minor,
 OM_uint32
 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
 {
-    return gss_eap_radius_attr_provider::init()
-        ? GSS_S_COMPLETE : GSS_S_FAILURE;
+    if (!gss_eap_radius_attr_provider::init()) {
+        *minor = GSSEAP_RADSEC_INIT_FAILURE;
+        return GSS_S_FAILURE;
+    }
+
+    return GSS_S_COMPLETE;
 }
 
 OM_uint32
 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
 {
     gss_eap_radius_attr_provider::finalize();
+
+    *minor = 0;
     return GSS_S_COMPLETE;
 }
 
@@ -592,10 +678,11 @@ avpImport(VALUE_PAIR **pVp,
     remain -= 4;
 
     da = dict_attrbyvalue(attrid);
-    if (da == NULL)
-        goto fail;
-
-    vp = pairalloc(da);
+    if (da != NULL) {
+        vp = pairalloc(da);
+    } else {
+        vp = paircreate(attrid, PW_TYPE_STRING);
+    }
     if (vp == NULL) {
         throw new std::bad_alloc;
         goto fail;
@@ -617,13 +704,8 @@ avpImport(VALUE_PAIR **pVp,
         remain -= 5;
         break;
     case PW_TYPE_STRING:
-        /* check enough room to NUL terminate */
-        if (p[0] == MAX_STRING_LEN)
-            goto fail;
-        else
-        /* fallthrough */
     default:
-        if (p[0] > MAX_STRING_LEN)
+        if (p[0] >= MAX_STRING_LEN)
             goto fail;
 
         vp->length = (uint32_t)p[0];
@@ -644,7 +726,9 @@ avpImport(VALUE_PAIR **pVp,
     return true;
 
 fail:
-    pairbasicfree(vp);
+    if (vp != NULL)
+        pairbasicfree(vp);
+    *pVp = NULL;
     return false;
 }
 
@@ -711,29 +795,25 @@ gss_eap_radius_attr_provider::getExpiryTime(void) const
     return time(NULL) + vp->lvalue;
 }
 
-/* partition error namespace so it does not conflict with krb5 */
-#define ERROR_TABLE_BASE_rse (46882560L)
-
-#define RS_TO_COM_ERR(rse)                  ((rse) == RSE_OK ? 0 : (rse) + ERROR_TABLE_BASE_rse)
-#define COM_TO_RS_ERR(err)                  ((err) > ERROR_TABLE_BASE_rse && \
-                                             (err) <= (ERROR_TABLE_BASE_rse + RSE_SOME_ERROR) ? \
-                                             (err) - ERROR_TABLE_BASE_rse : RSE_SOME_ERROR)
-
 OM_uint32
 gssEapRadiusMapError(OM_uint32 *minor,
                      struct rs_error *err)
 {
-    int code = RSE_OK;
+    int code;
 
-    if (err != NULL)
-        code = rs_err_code(err, 0);
-    else
-        code = RSE_SOME_ERROR;
+    assert(err != NULL);
 
-    *minor = RS_TO_COM_ERR(code);
+    code = rs_err_code(err, 0);
 
-    gssEapSaveStatusInfo(*minor, "radsec: %s", rs_err_msg(err, 0));
+    if (code == RSE_OK) {
+        *minor = 0;
+        return GSS_S_COMPLETE;
+    }
+
+    *minor = ERROR_TABLE_BASE_rse + code;
 
+    gssEapSaveStatusInfo(*minor, "%s", rs_err_msg(err));
     rs_err_free(err);
+
     return GSS_S_FAILURE;
 }