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

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