Better error reporting through com_err
[mech_eap.git] / util_radius.cpp
index b8eee67..3aef0db 100644 (file)
@@ -46,71 +46,19 @@ static gss_buffer_desc radiusUrnPrefix = {
 
 static VALUE_PAIR *copyAvps(const VALUE_PAIR *src);
 
-static struct rs_error *
-radiusAllocHandle(const char *configFile,
-                  rs_handle **pHandle)
-{
-    rs_handle *rh;
-    struct rs_alloc_scheme ralloc;
-
-    *pHandle = NULL;
-
-    if (configFile == NULL || configFile[0] == '\0')
-        configFile = RS_CONFIG_FILE;
-
-    if (rs_context_create(&rh, RS_DICT_FILE) != 0)
-        return NULL;
-
-    ralloc.calloc = GSSEAP_CALLOC;
-    ralloc.malloc = GSSEAP_MALLOC;
-    ralloc.free = GSSEAP_FREE;
-    ralloc.realloc = GSSEAP_REALLOC;
-
-    rs_context_set_alloc_scheme(rh, &ralloc);
-
-    if (rs_context_read_config(rh, configFile) != 0) {
-        rs_context_destroy(rh);
-        return rs_err_ctx_pop(rh);
-    }
-
-    *pHandle = rh;
-    return NULL;
-}
-
 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
 {
-    m_rh = NULL;
     m_vps = NULL;
     m_authenticated = false;
 }
 
 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
 {
-    if (m_rh != NULL)
-        rs_context_destroy(m_rh);
     if (m_vps != NULL)
         pairfree(&m_vps);
 }
 
 bool
-gss_eap_radius_attr_provider::allocRadHandle(const std::string &configFile)
-{
-    m_configFile.assign(configFile);
-
-    /*
-     * Currently none of the FreeRADIUS functions we use here actually take
-     * a handle, so we may as well leave it as NULL.
-     */
-#if 0
-    radiusAllocHandle(m_configFile.c_str(), &m_rh);
-
-    return (m_rh != NULL);
-#else
-    return true;
-#endif
-}
-
-bool
 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
                                                       const gss_eap_attr_provider *ctx)
 {
@@ -121,12 +69,11 @@ gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *ma
 
     radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
 
-    if (!allocRadHandle(radius->m_configFile))
-        return false;
-
     if (radius->m_vps != NULL)
         m_vps = copyAvps(const_cast<VALUE_PAIR *>(radius->getAvps()));
 
+    m_authenticated = radius->m_authenticated;
+
     return true;
 }
 
@@ -135,22 +82,18 @@ gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager
                                                  const gss_cred_id_t gssCred,
                                                  const gss_ctx_id_t gssCtx)
 {
-    std::string configFile(RS_CONFIG_FILE);
-
     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
         return false;
 
-    if (gssCred != GSS_C_NO_CREDENTIAL && gssCred->radiusConfigFile != NULL)
-        configFile.assign(gssCred->radiusConfigFile);
-
-    if (!allocRadHandle(configFile))
-        return false;
-
     if (gssCtx != GSS_C_NO_CONTEXT) {
         if (gssCtx->acceptorCtx.vps != NULL) {
             m_vps = copyAvps(gssCtx->acceptorCtx.vps);
             if (m_vps == NULL)
                 return false;
+
+            /* We assume libradsec validated this for us */
+            assert(pairfind(m_vps, PW_MESSAGE_AUTHENTICATOR) != NULL);
+            m_authenticated = true;
         }
     }
 
@@ -469,7 +412,6 @@ gss_eap_radius_attr_provider::createAttrContext(void)
 
 OM_uint32
 gssEapRadiusAddAvp(OM_uint32 *minor,
-                   rs_handle *rh,
                    VALUE_PAIR **vps,
                    uint16_t attribute,
                    uint16_t vendor,
@@ -572,82 +514,18 @@ 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;
+    }
 
-OM_uint32
-gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
-{
-    gss_eap_radius_attr_provider::finalize();
     return GSS_S_COMPLETE;
 }
 
-/* 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)
+gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
 {
-    int code = RSE_OK;
-
-    if (err != NULL)
-        code = rs_err_code(err, 0);
-    else
-        code = RSE_SOME_ERROR;
-
-    *minor = RS_TO_COM_ERR(code);
-
-    gssEapSaveStatusInfo(*minor, "radsec: %s", rs_err_msg(err, 0));
-
-    rs_err_free(err);
-    return GSS_S_FAILURE;
-}
-
-OM_uint32
-gssEapRadiusAllocConn(OM_uint32 *minor,
-                      const gss_cred_id_t cred,
-                      gss_ctx_id_t ctx)
-{
-    struct gss_eap_acceptor_ctx *actx = &ctx->acceptorCtx;
-    const char *configFile = NULL;
-    const char *configStanza = "gss-eap";
-    struct rs_error *err;
-
-    assert(actx->radHandle == NULL);
-    assert(actx->radConn == NULL);
-
-    if (cred != GSS_C_NO_CREDENTIAL) {
-        if (cred->radiusConfigFile != NULL)
-            configFile = cred->radiusConfigFile;
-        if (cred->radiusConfigStanza != NULL)
-            configStanza = cred->radiusConfigStanza;
-    }
-
-    err = radiusAllocHandle(configFile, &actx->radHandle);
-    if (err != NULL || actx->radHandle == NULL) {
-        return gssEapRadiusMapError(minor, err);
-    }
-
-    if (rs_conn_create(actx->radHandle, &actx->radConn, configStanza) != 0) {
-        return gssEapRadiusMapError(minor, rs_err_conn_pop(actx->radConn));
-    }
-
-    /* XXX TODO rs_conn_select_server does not exist yet */
-#if 0
-    if (actx->radServer != NULL) {
-        if (rs_conn_select_server(actx->radConn, actx->radServer) != 0)
-            return gssEapRadiusMapError(minor, rs_err_conn_pop(actx->radConn));
-    }
-#endif
-
-    *minor = 0;
+    gss_eap_radius_attr_provider::finalize();
     return GSS_S_COMPLETE;
 }
 
@@ -667,8 +545,7 @@ avpSize(const VALUE_PAIR *vp)
 }
 
 static bool
-avpExport(rs_handle *rh,
-          const VALUE_PAIR *vp,
+avpExport(const VALUE_PAIR *vp,
           unsigned char **pBuffer,
           size_t *pRemain)
 {
@@ -701,8 +578,7 @@ avpExport(rs_handle *rh,
 }
 
 static bool
-avpImport(rs_handle *rh,
-          VALUE_PAIR **pVp,
+avpImport(VALUE_PAIR **pVp,
           unsigned char **pBuffer,
           size_t *pRemain)
 {
@@ -782,65 +658,33 @@ gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
 {
     unsigned char *p = (unsigned char *)buffer->value;
     size_t remain = buffer->length;
-    OM_uint32 configFileLen, count;
     VALUE_PAIR **pNext = &m_vps;
 
     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
         return false;
 
-    if (remain < 4)
-        return false;
-
-    configFileLen = load_uint32_be(p);
-    p += 4;
-    remain -= 4;
-
-    if (remain < configFileLen)
-        return false;
-
-    std::string configFile((char *)p, configFileLen);
-    p += configFileLen;
-    remain -= configFileLen;
-
-    if (!allocRadHandle(configFile))
-        return false;
-
-    if (remain < 4)
-        return false;
-
-    count = load_uint32_be(p);
-    p += 4;
-    remain -= 4;
-
     do {
         VALUE_PAIR *attr;
 
-        if (!avpImport(m_rh, &attr, &p, &remain))
+        if (!avpImport(&attr, &p, &remain))
             return false;
 
         *pNext = attr;
         pNext = &attr->next;
-
-        count--;
     } while (remain != 0);
 
-    if (count != 0)
-        return false;
-
     return true;
 }
 
 void
 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
 {
-    OM_uint32 count = 0;
     VALUE_PAIR *vp;
     unsigned char *p;
-    size_t remain = 4 + m_configFile.length() + 4;
+    size_t remain = 0;
 
     for (vp = m_vps; vp != NULL; vp = vp->next) {
         remain += avpSize(vp);
-        count++;
     }
 
     buffer->value = GSSEAP_MALLOC(remain);
@@ -852,20 +696,8 @@ gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
 
     p = (unsigned char *)buffer->value;
 
-    store_uint32_be(m_configFile.length(), p);
-    p += 4;
-    remain -= 4;
-
-    memcpy(p, m_configFile.c_str(), m_configFile.length());
-    p += m_configFile.length();
-    remain -= m_configFile.length();
-
-    store_uint32_be(count, p);
-    p += 4;
-    remain -= 4;
-
     for (vp = m_vps; vp != NULL; vp = vp->next) {
-        avpExport(m_rh, vp, &p, &remain);
+        avpExport(vp, &p, &remain);
     }
 
     assert(remain == 0);
@@ -882,3 +714,28 @@ gss_eap_radius_attr_provider::getExpiryTime(void) const
 
     return time(NULL) + vp->lvalue;
 }
+
+/* partition error namespace so it does not conflict with krb5 */
+#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;
+
+    if (err != NULL)
+        code = rs_err_code(err, 0);
+    else
+        code = RSE_SOME_ERROR;
+
+    *minor = RS_TO_COM_ERR(code);
+
+    gssEapSaveStatusInfo(*minor, "%s", rs_err_msg(err, 0));
+
+    rs_err_free(err);
+    return GSS_S_FAILURE;
+}