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

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