Blame SOURCES/0149-VALIDATORS-Detect-inherit_from-in-normal-domain.patch

bb7cd1
From b94b578fac8f94d42fd6fb691438d2dbe5248309 Mon Sep 17 00:00:00 2001
bb7cd1
From: =?UTF-8?q?Michal=20=C5=BDidek?= <mzidek@redhat.com>
bb7cd1
Date: Wed, 31 May 2017 14:21:02 +0200
bb7cd1
Subject: [PATCH 149/152] VALIDATORS: Detect inherit_from in normal domain
bb7cd1
MIME-Version: 1.0
bb7cd1
Content-Type: text/plain; charset=UTF-8
bb7cd1
Content-Transfer-Encoding: 8bit
bb7cd1
bb7cd1
This patch adds new sssd specific validator. In the future we
bb7cd1
can add more checks in it, but currently it only checks if
bb7cd1
the option inherit_from is used on normal domain and reports
bb7cd1
error if it is.
bb7cd1
bb7cd1
Resolves:
bb7cd1
https://pagure.io/SSSD/sssd/issue/3356
bb7cd1
bb7cd1
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
bb7cd1
---
bb7cd1
 src/config/cfg_rules.ini             |  3 ++
bb7cd1
 src/tests/cmocka/test_config_check.c | 22 +++++++++++++++
bb7cd1
 src/util/sss_ini.c                   | 53 +++++++++++++++++++++++++++++++++++-
bb7cd1
 3 files changed, 77 insertions(+), 1 deletion(-)
bb7cd1
bb7cd1
diff --git a/src/config/cfg_rules.ini b/src/config/cfg_rules.ini
bb7cd1
index 2c8c0cb98ed039c374c827775798f61369c1521e..744446478e5d5489cd86d8e15ce8e178cf5e3a91 100644
bb7cd1
--- a/src/config/cfg_rules.ini
bb7cd1
+++ b/src/config/cfg_rules.ini
bb7cd1
@@ -711,3 +711,6 @@ option = ad_server
bb7cd1
 option = ad_backup_server
bb7cd1
 option = ad_site
bb7cd1
 option = use_fully_qualified_names
bb7cd1
+
bb7cd1
+[rule/sssd_checks]
bb7cd1
+validator = sssd_checks
bb7cd1
diff --git a/src/tests/cmocka/test_config_check.c b/src/tests/cmocka/test_config_check.c
bb7cd1
index 8fc0b01f3ef3fe03152efd979a3e96c21ba567cc..bab3226c004fb9495471af7c7d3f6861552d8a86 100644
bb7cd1
--- a/src/tests/cmocka/test_config_check.c
bb7cd1
+++ b/src/tests/cmocka/test_config_check.c
bb7cd1
@@ -217,6 +217,27 @@ void config_check_test_good_sections(void **state)
bb7cd1
     config_check_test_common(cfg_str, 0, expected_errors);
bb7cd1
 }
bb7cd1
 
bb7cd1
+void config_check_test_inherit_from_in_normal_dom(void **state)
bb7cd1
+{
bb7cd1
+    char cfg_str[] = "[domain/A.test]\n"
bb7cd1
+                     "inherit_from = domain\n";
bb7cd1
+    const char *expected_errors[] = {
bb7cd1
+        "[rule/sssd_checks]: Attribute 'inherit_from' is not allowed in "
bb7cd1
+        "section 'domain/A.test'. Check for typos.",
bb7cd1
+    };
bb7cd1
+
bb7cd1
+    config_check_test_common(cfg_str, 1, expected_errors);
bb7cd1
+}
bb7cd1
+
bb7cd1
+void config_check_test_inherit_from_in_app_dom(void **state)
bb7cd1
+{
bb7cd1
+    char cfg_str[] = "[application/A.test]\n"
bb7cd1
+                     "inherit_from = domain\n";
bb7cd1
+    const char *expected_errors[] = { NULL };
bb7cd1
+
bb7cd1
+    config_check_test_common(cfg_str, 0, expected_errors);
bb7cd1
+}
bb7cd1
+
bb7cd1
 int main(int argc, const char *argv[])
bb7cd1
 {
bb7cd1
     poptContext pc;
bb7cd1
@@ -235,6 +256,7 @@ int main(int argc, const char *argv[])
bb7cd1
         cmocka_unit_test(config_check_test_bad_pac_option_name),
bb7cd1
         cmocka_unit_test(config_check_test_bad_ifp_option_name),
bb7cd1
         cmocka_unit_test(config_check_test_good_sections),
bb7cd1
+        cmocka_unit_test(config_check_test_inherit_from_in_normal_dom),
bb7cd1
     };
bb7cd1
 
bb7cd1
     /* Set debug level to invalid value so we can decide if -d 0 was used. */
bb7cd1
diff --git a/src/util/sss_ini.c b/src/util/sss_ini.c
bb7cd1
index e56006c05555d6e0c5e726e83771abce5a72b139..175a4cfaba7ea964aee174e928d5e3c1e81de638 100644
bb7cd1
--- a/src/util/sss_ini.c
bb7cd1
+++ b/src/util/sss_ini.c
bb7cd1
@@ -561,12 +561,63 @@ error:
bb7cd1
 }
bb7cd1
 
bb7cd1
 #ifdef HAVE_LIBINI_CONFIG_V1_3
bb7cd1
+/* Here we can put custom SSSD specific checks that can not be implemented
bb7cd1
+ * using libini validators */
bb7cd1
+static int custom_sssd_checks(const char *rule_name,
bb7cd1
+                              struct ini_cfgobj *rules_obj,
bb7cd1
+                              struct ini_cfgobj *config_obj,
bb7cd1
+                              struct ini_errobj *errobj,
bb7cd1
+                              void **data)
bb7cd1
+{
bb7cd1
+    char **cfg_sections = NULL;
bb7cd1
+    int num_cfg_sections;
bb7cd1
+    struct value_obj *vo = NULL;
bb7cd1
+    char dom_prefix[] = "domain/";
bb7cd1
+    int ret;
bb7cd1
+
bb7cd1
+    /* Get all sections in configuration */
bb7cd1
+    cfg_sections = ini_get_section_list(config_obj, &num_cfg_sections, &ret;;
bb7cd1
+    if (ret != EOK) {
bb7cd1
+        goto done;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    /* Check if a normal domain section (not application domains) has option
bb7cd1
+     * inherit_from and report error if it does */
bb7cd1
+    for (int i = 0; i < num_cfg_sections; i++) {
bb7cd1
+        if (strncmp(dom_prefix, cfg_sections[i], strlen(dom_prefix)) == 0) {
bb7cd1
+            ret = ini_get_config_valueobj(cfg_sections[i],
bb7cd1
+                                          "inherit_from",
bb7cd1
+                                          config_obj,
bb7cd1
+                                          INI_GET_NEXT_VALUE,
bb7cd1
+                                          &vo);
bb7cd1
+            if (vo != NULL) {
bb7cd1
+                ret = ini_errobj_add_msg(errobj,
bb7cd1
+                                         "Attribute 'inherit_from' is not "
bb7cd1
+                                         "allowed in section '%s'. Check for "
bb7cd1
+                                         "typos.",
bb7cd1
+                                         cfg_sections[i]);
bb7cd1
+                if (ret != EOK) {
bb7cd1
+                    goto done;
bb7cd1
+                }
bb7cd1
+            }
bb7cd1
+        }
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    ret = EOK;
bb7cd1
+done:
bb7cd1
+    ini_free_section_list(cfg_sections);
bb7cd1
+    return EOK;
bb7cd1
+}
bb7cd1
+
bb7cd1
 static int sss_ini_call_validators_errobj(struct sss_ini_initdata *data,
bb7cd1
                                           const char *rules_path,
bb7cd1
                                           struct ini_errobj *errobj)
bb7cd1
 {
bb7cd1
     int ret;
bb7cd1
     struct ini_cfgobj *rules_cfgobj = NULL;
bb7cd1
+    struct ini_validator custom_sssd = { "sssd_checks", custom_sssd_checks,
bb7cd1
+                                         NULL };
bb7cd1
+    struct ini_validator *sss_validators[] = { &custom_sssd, NULL };
bb7cd1
 
bb7cd1
     ret = ini_rules_read_from_file(rules_path, &rules_cfgobj);
bb7cd1
     if (ret != EOK) {
bb7cd1
@@ -575,7 +626,7 @@ static int sss_ini_call_validators_errobj(struct sss_ini_initdata *data,
bb7cd1
         goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
-    ret = ini_rules_check(rules_cfgobj, data->sssd_config, NULL, errobj);
bb7cd1
+    ret = ini_rules_check(rules_cfgobj, data->sssd_config, sss_validators, errobj);
bb7cd1
     if (ret != EOK) {
bb7cd1
         DEBUG(SSSDBG_FATAL_FAILURE,
bb7cd1
               "ini_rules_check failed %d [%s]\n", ret, strerror(ret));
bb7cd1
-- 
bb7cd1
2.9.4
bb7cd1