>From cc4b347c955f93d640772fad197a55a1b9034525 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Mon, 21 Apr 2014 21:33:36 +0200
Subject: [PATCH 1/3] LDAP: Fix one-by-one bug in sdap_copy_opts

The sdap_copy_opts function copied all the arguments except for the
sentinel.
---
 Makefile.am                    |  3 +++
 src/providers/ldap/sdap.c      |  8 +++++-
 src/tests/ipa_ldap_opt-tests.c | 55 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/Makefile.am b/Makefile.am
index dc8a99771893ebd9fecb6e1d3ecc9cc2f8bcd8b4..f5a6594b0b25b733f0cd40ac1038ba5bef49e1fc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1213,6 +1213,9 @@ auth_tests_LDADD = \
 
 ipa_ldap_opt_tests_SOURCES = \
     src/providers/data_provider_opts.c \
+    src/providers/ldap/sdap.c \
+    src/providers/ldap/sdap_range.c \
+    src/util/sss_ldap.c \
     src/tests/ipa_ldap_opt-tests.c
 ipa_ldap_opt_tests_CFLAGS = \
     $(AM_CFLAGS) \
diff --git a/src/providers/ldap/sdap.c b/src/providers/ldap/sdap.c
index aa6b0e921767ab97f025998276e16ec9abdfc03f..568d2e73a85e84691b76cad8dceeb2fe0f69491e 100644
--- a/src/providers/ldap/sdap.c
+++ b/src/providers/ldap/sdap.c
@@ -36,7 +36,7 @@ int sdap_copy_map(TALLOC_CTX *memctx,
     struct sdap_attr_map *map;
     int i;
 
-    map = talloc_array(memctx, struct sdap_attr_map, num_entries);
+    map = talloc_array(memctx, struct sdap_attr_map, num_entries + 1);
     if (!map) {
         return ENOMEM;
     }
@@ -64,6 +64,12 @@ int sdap_copy_map(TALLOC_CTX *memctx,
               map[i].name ? map[i].name : "");
     }
 
+    /* Include the sentinel */
+    map[num_entries].def_name = NULL;
+    map[num_entries].sys_name = NULL;
+    map[num_entries].opt_name = NULL;
+    map[num_entries].name     = NULL;
+
     *_map = map;
     return EOK;
 }
diff --git a/src/tests/ipa_ldap_opt-tests.c b/src/tests/ipa_ldap_opt-tests.c
index 25a094082dc369092f10ad823d98909027dd9a7e..bbb49935dec47983c4d04ef73385a4ed8a1ea372 100644
--- a/src/tests/ipa_ldap_opt-tests.c
+++ b/src/tests/ipa_ldap_opt-tests.c
@@ -48,6 +48,14 @@ struct test_domain test_domains[] = {
     { NULL, NULL}
 };
 
+/* Mock parsing search base without overlinking the test */
+errno_t sdap_parse_search_base(TALLOC_CTX *mem_ctx,
+                               struct dp_option *opts, int class,
+                               struct sdap_search_base ***_search_bases)
+{
+    return EOK;
+}
+
 START_TEST(test_domain_to_basedn)
 {
     int ret;
@@ -226,6 +234,49 @@ START_TEST(test_copy_opts)
 }
 END_TEST
 
+START_TEST(test_copy_sdap_map)
+{
+    errno_t ret;
+    struct sdap_attr_map *out_map;
+
+    ret = sdap_copy_map(global_talloc_context,
+                        rfc2307_user_map, SDAP_OPTS_USER, &out_map);
+    fail_unless(ret == EOK, "[%s]", strerror(ret));
+    fail_unless(out_map[SDAP_OPTS_USER].name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].def_name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].sys_name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].opt_name == NULL);
+    talloc_free(out_map);
+
+    ret = sdap_copy_map(global_talloc_context,
+                        rfc2307bis_user_map, SDAP_OPTS_USER, &out_map);
+    fail_unless(ret == EOK, "[%s]", strerror(ret));
+    fail_unless(out_map[SDAP_OPTS_USER].name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].def_name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].sys_name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].opt_name == NULL);
+    talloc_free(out_map);
+
+    ret = sdap_copy_map(global_talloc_context,
+                        ipa_user_map, SDAP_OPTS_USER, &out_map);
+    fail_unless(ret == EOK, "[%s]", strerror(ret));
+    fail_unless(out_map[SDAP_OPTS_USER].name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].def_name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].sys_name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].opt_name == NULL);
+    talloc_free(out_map);
+
+    ret = sdap_copy_map(global_talloc_context,
+                        gen_ad2008r2_user_map, SDAP_OPTS_USER, &out_map);
+    fail_unless(ret == EOK, "[%s]", strerror(ret));
+    fail_unless(out_map[SDAP_OPTS_USER].name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].def_name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].sys_name == NULL);
+    fail_unless(out_map[SDAP_OPTS_USER].opt_name == NULL);
+    talloc_free(out_map);
+}
+END_TEST
+
 Suite *ipa_ldap_opt_suite (void)
 {
     Suite *s = suite_create ("ipa_ldap_opt");
@@ -245,6 +296,10 @@ Suite *ipa_ldap_opt_suite (void)
     tcase_add_test (tc_dp_opts, test_copy_opts);
     suite_add_tcase (s, tc_dp_opts);
 
+    TCase *tc_sdap_opts = tcase_create ("sdap_opts");
+    tcase_add_test (tc_sdap_opts, test_copy_sdap_map);
+    suite_add_tcase (s, tc_sdap_opts);
+
     return s;
 }
 
-- 
1.9.0

