andykimpe / rpms / 389-ds-base

Forked from rpms/389-ds-base 6 months ago
Clone

Blame SOURCES/0030-Ticket-49231-fix-sasl-mech-handling.patch

74ca47
From 88a0ce3c3f89244a77dfa618c8a5064bda30f376 Mon Sep 17 00:00:00 2001
74ca47
From: William Brown <firstyear@redhat.com>
74ca47
Date: Wed, 26 Apr 2017 15:48:30 +1000
74ca47
Subject: [PATCH] Ticket 49231 - fix sasl mech handling
74ca47
74ca47
Bug Description:  In our sasl code we had two issues. One was that
74ca47
we did not correctly apply the list of sasl allowed mechs to our
74ca47
rootdse list in ids_sasl_listmech. The second was that on config
74ca47
reset, we did not correctly set null to the value.
74ca47
74ca47
Fix Description:  Fix the handling of the mech lists to allow
74ca47
reset, and allow the mech list to be updated properly.
74ca47
74ca47
https://pagure.io/389-ds-base/issue/49231
74ca47
74ca47
Author: wibrown
74ca47
74ca47
Review by: mreynolds (Thanks!)
74ca47
---
74ca47
 dirsrvtests/tests/suites/sasl/allowed_mechs.py | 43 ++++++++++++++++++
74ca47
 ldap/servers/slapd/charray.c                   | 48 +++++++++++++++++---
74ca47
 ldap/servers/slapd/libglobs.c                  | 62 ++++++++++++++++++++------
74ca47
 ldap/servers/slapd/proto-slap.h                |  1 +
74ca47
 ldap/servers/slapd/saslbind.c                  | 21 ++++++++-
74ca47
 ldap/servers/slapd/slap.h                      |  1 +
74ca47
 ldap/servers/slapd/slapi-private.h             |  1 +
74ca47
 7 files changed, 156 insertions(+), 21 deletions(-)
74ca47
 create mode 100644 dirsrvtests/tests/suites/sasl/allowed_mechs.py
74ca47
74ca47
diff --git a/dirsrvtests/tests/suites/sasl/allowed_mechs.py b/dirsrvtests/tests/suites/sasl/allowed_mechs.py
74ca47
new file mode 100644
74ca47
index 0000000..a3e385e
74ca47
--- /dev/null
74ca47
+++ b/dirsrvtests/tests/suites/sasl/allowed_mechs.py
74ca47
@@ -0,0 +1,43 @@
74ca47
+# --- BEGIN COPYRIGHT BLOCK ---
74ca47
+# Copyright (C) 2017 Red Hat, Inc.
74ca47
+# All rights reserved.
74ca47
+#
74ca47
+# License: GPL (version 3 or any later version).
74ca47
+# See LICENSE for details.
74ca47
+# --- END COPYRIGHT BLOCK ---
74ca47
+#
74ca47
+
74ca47
+import pytest
74ca47
+import ldap
74ca47
+
74ca47
+import time
74ca47
+
74ca47
+from lib389.topologies import topology_st
74ca47
+
74ca47
+def test_sasl_allowed_mechs(topology_st):
74ca47
+    standalone = topology_st.standalone
74ca47
+
74ca47
+    # Get the supported mechs. This should contain PLAIN, GSSAPI, EXTERNAL at least
74ca47
+    orig_mechs = standalone.rootdse.supported_sasl()
74ca47
+    print(orig_mechs)
74ca47
+    assert('GSSAPI' in orig_mechs)
74ca47
+    assert('PLAIN' in orig_mechs)
74ca47
+    assert('EXTERNAL' in orig_mechs)
74ca47
+
74ca47
+    # Now edit the supported mechs. CHeck them again.
74ca47
+    standalone.config.set('nsslapd-allowed-sasl-mechanisms', 'EXTERNAL, PLAIN')
74ca47
+
74ca47
+    limit_mechs = standalone.rootdse.supported_sasl()
74ca47
+    print(limit_mechs)
74ca47
+    assert('PLAIN' in limit_mechs)
74ca47
+    assert('EXTERNAL' in limit_mechs)
74ca47
+    assert('GSSAPI' not in limit_mechs)
74ca47
+
74ca47
+    # Do a config reset
74ca47
+    standalone.config.reset('nsslapd-allowed-sasl-mechanisms')
74ca47
+
74ca47
+    # check the supported list is the same as our first check.
74ca47
+    final_mechs = standalone.rootdse.supported_sasl()
74ca47
+    print(final_mechs)
74ca47
+    assert(set(final_mechs) == set(orig_mechs))
74ca47
+
74ca47
diff --git a/ldap/servers/slapd/charray.c b/ldap/servers/slapd/charray.c
74ca47
index 5551dcc..6b89714 100644
74ca47
--- a/ldap/servers/slapd/charray.c
74ca47
+++ b/ldap/servers/slapd/charray.c
74ca47
@@ -348,8 +348,9 @@ slapi_str2charray_ext( char *str, char *brkstr, int allow_dups )
74ca47
             }
74ca47
         }
74ca47
 
74ca47
-        if ( !dup_found )
74ca47
+        if ( !dup_found ) {
74ca47
             res[i++] = slapi_ch_strdup( s );
74ca47
+        }
74ca47
     }
74ca47
     res[i] = NULL;
74ca47
 
74ca47
@@ -413,10 +414,11 @@ charray_subtract(char **a, char **b, char ***c)
74ca47
     char **bp, **cp, **tmp;
74ca47
     char **p;
74ca47
 
74ca47
-    if (c)
74ca47
+    if (c) {
74ca47
         tmp = *c = cool_charray_dup(a);
74ca47
-    else
74ca47
+    } else {
74ca47
         tmp = a;
74ca47
+    }
74ca47
 
74ca47
     for (cp = tmp; cp && *cp; cp++) {
74ca47
         for (bp = b; bp && *bp; bp++) {
74ca47
@@ -433,12 +435,48 @@ charray_subtract(char **a, char **b, char ***c)
74ca47
             for (p = cp+1; *p && *p == (char *)SUBTRACT_DEL; p++)
74ca47
                 ;
74ca47
             *cp = *p;    
74ca47
-            if (*p == NULL)
74ca47
+            if (*p == NULL) {
74ca47
                 break;
74ca47
-            else
74ca47
+            } else {
74ca47
                 *p = SUBTRACT_DEL;
74ca47
+            }
74ca47
+        }
74ca47
+    }
74ca47
+}
74ca47
+
74ca47
+/*
74ca47
+ * Provides the intersection of two arrays.
74ca47
+ * IE if you have:
74ca47
+ * (A, B, C)
74ca47
+ * (B, D, E)
74ca47
+ * result is (B,)
74ca47
+ * a and b are NOT consumed in the process.
74ca47
+ */
74ca47
+char **
74ca47
+charray_intersection(char **a, char **b) {
74ca47
+    char **result;
74ca47
+    size_t rp = 0;
74ca47
+
74ca47
+    if (a == NULL || b == NULL) {
74ca47
+        return NULL;
74ca47
+    }
74ca47
+
74ca47
+    size_t a_len = 0;
74ca47
+    /* Find how long A is. */
74ca47
+    for (; a[a_len] != NULL; a_len++);
74ca47
+
74ca47
+    /* Allocate our result, it can't be bigger than A */
74ca47
+    result = (char **)slapi_ch_calloc(1, sizeof(char *) * (a_len + 1));
74ca47
+
74ca47
+    /* For each in A, see if it's in b */
74ca47
+    for (size_t i = 0; a[i] != NULL; i++) {
74ca47
+        if (charray_get_index(b, a[i]) != -1) {
74ca47
+            result[rp] = slapi_ch_strdup(a[i]);
74ca47
+            rp++;
74ca47
         }
74ca47
     }
74ca47
+
74ca47
+    return result;
74ca47
 }
74ca47
 
74ca47
 int
74ca47
diff --git a/ldap/servers/slapd/libglobs.c b/ldap/servers/slapd/libglobs.c
74ca47
index 0e818a9..2fc9fbf 100644
74ca47
--- a/ldap/servers/slapd/libglobs.c
74ca47
+++ b/ldap/servers/slapd/libglobs.c
74ca47
@@ -7090,9 +7090,30 @@ config_set_entryusn_import_init( const char *attrname, char *value,
74ca47
     return retVal;
74ca47
 }
74ca47
 
74ca47
+char **
74ca47
+config_get_allowed_sasl_mechs_array(void)
74ca47
+{
74ca47
+    /*
74ca47
+     * array of mechs. If is null, returns NULL thanks to ch_array_dup.
74ca47
+     * Caller must free!
74ca47
+     */
74ca47
+    char **retVal;
74ca47
+    slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
74ca47
+
74ca47
+    CFG_LOCK_READ(slapdFrontendConfig);
74ca47
+    retVal = slapi_ch_array_dup(slapdFrontendConfig->allowed_sasl_mechs_array);
74ca47
+    CFG_UNLOCK_READ(slapdFrontendConfig);
74ca47
+
74ca47
+    return retVal;
74ca47
+}
74ca47
+
74ca47
 char *
74ca47
-config_get_allowed_sasl_mechs()
74ca47
+config_get_allowed_sasl_mechs(void)
74ca47
 {
74ca47
+    /*
74ca47
+     * Space seperated list of allowed mechs
74ca47
+     * if this is NULL, means *all* mechs are allowed!
74ca47
+     */
74ca47
     char *retVal;
74ca47
     slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
74ca47
 
74ca47
@@ -7113,22 +7134,35 @@ config_set_allowed_sasl_mechs(const char *attrname, char *value, char *errorbuf,
74ca47
         return LDAP_SUCCESS;
74ca47
     }
74ca47
 
74ca47
-    /* cyrus sasl doesn't like comma separated lists */
74ca47
-    remove_commas(value);
74ca47
+    /* During a reset, the value is "", so we have to handle this case. */
74ca47
+    if (strcmp(value, "") != 0) {
74ca47
+        /* cyrus sasl doesn't like comma separated lists */
74ca47
+        remove_commas(value);
74ca47
+
74ca47
+        if(invalid_sasl_mech(value)){
74ca47
+            slapi_log_err(SLAPI_LOG_ERR,"config_set_allowed_sasl_mechs",
74ca47
+                    "Invalid value/character for sasl mechanism (%s).  Use ASCII "
74ca47
+                    "characters, upto 20 characters, that are upper-case letters, "
74ca47
+                    "digits, hyphens, or underscores\n", value);
74ca47
+            return LDAP_UNWILLING_TO_PERFORM;
74ca47
+        }
74ca47
 
74ca47
-    if(invalid_sasl_mech(value)){
74ca47
-        slapi_log_err(SLAPI_LOG_ERR,"config_set_allowed_sasl_mechs",
74ca47
-                "Invalid value/character for sasl mechanism (%s).  Use ASCII "
74ca47
-                "characters, upto 20 characters, that are upper-case letters, "
74ca47
-                "digits, hyphens, or underscores\n", value);
74ca47
-        return LDAP_UNWILLING_TO_PERFORM;
74ca47
+        CFG_LOCK_WRITE(slapdFrontendConfig);
74ca47
+        slapi_ch_free_string(&slapdFrontendConfig->allowed_sasl_mechs);
74ca47
+        slapi_ch_array_free(slapdFrontendConfig->allowed_sasl_mechs_array);
74ca47
+        slapdFrontendConfig->allowed_sasl_mechs = slapi_ch_strdup(value);
74ca47
+        slapdFrontendConfig->allowed_sasl_mechs_array = slapi_str2charray_ext(value, " ", 0);
74ca47
+        CFG_UNLOCK_WRITE(slapdFrontendConfig);
74ca47
+    } else {
74ca47
+        /* If this value is "", we need to set the list to *all* possible mechs */
74ca47
+        CFG_LOCK_WRITE(slapdFrontendConfig);
74ca47
+        slapi_ch_free_string(&slapdFrontendConfig->allowed_sasl_mechs);
74ca47
+        slapi_ch_array_free(slapdFrontendConfig->allowed_sasl_mechs_array);
74ca47
+        slapdFrontendConfig->allowed_sasl_mechs = NULL;
74ca47
+        slapdFrontendConfig->allowed_sasl_mechs_array = NULL;
74ca47
+        CFG_UNLOCK_WRITE(slapdFrontendConfig);
74ca47
     }
74ca47
 
74ca47
-    CFG_LOCK_WRITE(slapdFrontendConfig);
74ca47
-    slapi_ch_free_string(&slapdFrontendConfig->allowed_sasl_mechs);
74ca47
-    slapdFrontendConfig->allowed_sasl_mechs = slapi_ch_strdup(value);
74ca47
-    CFG_UNLOCK_WRITE(slapdFrontendConfig);
74ca47
-
74ca47
     return LDAP_SUCCESS;
74ca47
 }
74ca47
 
74ca47
diff --git a/ldap/servers/slapd/proto-slap.h b/ldap/servers/slapd/proto-slap.h
74ca47
index fdb4bf0..9696ead 100644
74ca47
--- a/ldap/servers/slapd/proto-slap.h
74ca47
+++ b/ldap/servers/slapd/proto-slap.h
74ca47
@@ -553,6 +553,7 @@ size_t config_get_ndn_cache_size(void);
74ca47
 int config_get_ndn_cache_enabled(void);
74ca47
 int config_get_return_orig_type_switch(void);
74ca47
 char *config_get_allowed_sasl_mechs(void);
74ca47
+char **config_get_allowed_sasl_mechs_array(void);
74ca47
 int config_set_allowed_sasl_mechs(const char *attrname, char *value, char *errorbuf, int apply);
74ca47
 int config_get_schemamod(void);
74ca47
 int config_set_ignore_vattrs(const char *attrname, char *value, char *errorbuf, int apply);
74ca47
diff --git a/ldap/servers/slapd/saslbind.c b/ldap/servers/slapd/saslbind.c
74ca47
index 2d6fb64..6e544e6 100644
74ca47
--- a/ldap/servers/slapd/saslbind.c
74ca47
+++ b/ldap/servers/slapd/saslbind.c
74ca47
@@ -744,7 +744,10 @@ void ids_sasl_server_new(Connection *conn)
74ca47
  */
74ca47
 char **ids_sasl_listmech(Slapi_PBlock *pb)
74ca47
 {
74ca47
-    char **ret, **others;
74ca47
+    char **ret;
74ca47
+    char **config_ret;
74ca47
+    char **sup_ret;
74ca47
+    char **others;
74ca47
     const char *str;
74ca47
     char *dupstr;
74ca47
     sasl_conn_t *sasl_conn;
74ca47
@@ -754,7 +757,7 @@ char **ids_sasl_listmech(Slapi_PBlock *pb)
74ca47
     PR_ASSERT(pb);
74ca47
 
74ca47
     /* hard-wired mechanisms and slapi plugin registered mechanisms */
74ca47
-    ret = slapi_get_supported_saslmechanisms_copy();
74ca47
+    sup_ret = slapi_get_supported_saslmechanisms_copy();
74ca47
 
74ca47
     if (pb->pb_conn == NULL) return ret;
74ca47
 
74ca47
@@ -777,6 +780,20 @@ char **ids_sasl_listmech(Slapi_PBlock *pb)
74ca47
     }
74ca47
     PR_ExitMonitor(pb->pb_conn->c_mutex);
74ca47
 
74ca47
+    /* Get the servers "allowed" list */
74ca47
+    config_ret = config_get_allowed_sasl_mechs_array();
74ca47
+
74ca47
+    /* Remove any content that isn't in the allowed list */
74ca47
+    if (config_ret != NULL) {
74ca47
+        /* Get the set of supported mechs in the insection of the two */
74ca47
+        ret = charray_intersection(sup_ret, config_ret);
74ca47
+        charray_free(sup_ret);
74ca47
+        charray_free(config_ret);
74ca47
+    } else {
74ca47
+        /* The allowed list was empty, just take our supported list. */
74ca47
+        ret = sup_ret;
74ca47
+    }
74ca47
+
74ca47
     slapi_log_err(SLAPI_LOG_TRACE, "ids_sasl_listmech", "<=\n");
74ca47
 
74ca47
     return ret;
74ca47
diff --git a/ldap/servers/slapd/slap.h b/ldap/servers/slapd/slap.h
74ca47
index abfad20..5e44cc8 100644
74ca47
--- a/ldap/servers/slapd/slap.h
74ca47
+++ b/ldap/servers/slapd/slap.h
74ca47
@@ -2577,6 +2577,7 @@ typedef struct _slapdFrontendConfig {
74ca47
   int pagedsizelimit;
74ca47
   char *default_naming_context; /* Default naming context (normalized) */
74ca47
   char *allowed_sasl_mechs;     /* comma/space separated list of allowed sasl mechs */
74ca47
+  char **allowed_sasl_mechs_array;     /* Array of allow sasl mechs */
74ca47
   int sasl_max_bufsize;         /* The max receive buffer size for SASL */
74ca47
 
74ca47
   /* disk monitoring */
74ca47
diff --git a/ldap/servers/slapd/slapi-private.h b/ldap/servers/slapd/slapi-private.h
74ca47
index d9547d8..3f732e8 100644
74ca47
--- a/ldap/servers/slapd/slapi-private.h
74ca47
+++ b/ldap/servers/slapd/slapi-private.h
74ca47
@@ -831,6 +831,7 @@ int charray_remove(char **a, const char *s, int freeit);
74ca47
 char ** cool_charray_dup( char **a );
74ca47
 void cool_charray_free( char **array );
74ca47
 void charray_subtract( char **a, char **b, char ***c );
74ca47
+char **charray_intersection(char **a, char **b);
74ca47
 int charray_get_index(char **array, char *s);
74ca47
 int charray_normdn_add(char ***chararray, char *dn, char *errstr);
74ca47
 
74ca47
-- 
74ca47
2.9.3
74ca47