From 6b890fe0b9153f9c008ea23ed7ff3450578584cd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20=C5=BDidek?= <mzidek@redhat.com>
Date: Mon, 27 Jun 2016 17:33:14 +0200
Subject: [PATCH 2/4] sssctl: Add config-check command

Fixes:
https://fedorahosted.org/sssd/ticket/2269

sssctl sconfig-check command allows to
call SSSD config file validators on
demand.
---
 Makefile.am                      |   1 +
 src/tools/sssctl/sssctl.c        |   2 +
 src/tools/sssctl/sssctl.h        |   4 ++
 src/tools/sssctl/sssctl_config.c | 150 +++++++++++++++++++++++++++++++++++++++
 src/util/sss_ini.c               |  38 ++++++++++
 src/util/sss_ini.h               |   8 +++
 6 files changed, 203 insertions(+)
 create mode 100644 src/tools/sssctl/sssctl_config.c

diff --git a/Makefile.am b/Makefile.am
index 2410863..170fc2f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1517,6 +1517,7 @@ sssctl_SOURCES = \
     src/tools/sssctl/sssctl_logs.c \
     src/tools/sssctl/sssctl_domains.c \
     src/tools/sssctl/sssctl_sifp.c \
+    src/tools/sssctl/sssctl_config.c \
     $(SSSD_TOOLS_OBJ) \
     $(NULL)
 sssctl_LDADD = \
diff --git a/src/tools/sssctl/sssctl.c b/src/tools/sssctl/sssctl.c
index 58bbbf2..ce5c685 100644
--- a/src/tools/sssctl/sssctl.c
+++ b/src/tools/sssctl/sssctl.c
@@ -270,6 +270,8 @@ int main(int argc, const char **argv)
         SSS_TOOL_DELIMITER("Log files tools:"),
         SSS_TOOL_COMMAND("remove-logs", "Remove existing SSSD log files", sssctl_remove_logs),
         SSS_TOOL_COMMAND("fetch-logs", "Archive SSSD log files in tarball", sssctl_fetch_logs),
+        SSS_TOOL_DELIMITER("Configuration files tools:"),
+        SSS_TOOL_COMMAND("config-check", "Perform static analysis of SSSD configuration", sssctl_config_check),
         {NULL, NULL, NULL}
     };
 
diff --git a/src/tools/sssctl/sssctl.h b/src/tools/sssctl/sssctl.h
index 4bcc6de..0e83a5b 100644
--- a/src/tools/sssctl/sssctl.h
+++ b/src/tools/sssctl/sssctl.h
@@ -96,4 +96,8 @@ errno_t sssctl_netgroup(struct sss_cmdline *cmdline,
                         struct sss_tool_ctx *tool_ctx,
                         void *pvt);
 
+errno_t sssctl_config_check(struct sss_cmdline *cmdline,
+                            struct sss_tool_ctx *tool_ctx,
+                            void *pvt);
+
 #endif /* _SSSCTL_H_ */
diff --git a/src/tools/sssctl/sssctl_config.c b/src/tools/sssctl/sssctl_config.c
new file mode 100644
index 0000000..63fe263
--- /dev/null
+++ b/src/tools/sssctl/sssctl_config.c
@@ -0,0 +1,150 @@
+/*
+    Authors:
+        Michal Židek <mzidek@redhat.com>
+
+    Copyright (C) 2016 Red Hat
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "config.h"
+
+#include <popt.h>
+#include <stdio.h>
+#include <ini_configobj.h>
+
+#include "util/util.h"
+#include "util/sss_ini.h"
+#include "tools/common/sss_tools.h"
+#include "tools/common/sss_process.h"
+#include "tools/sssctl/sssctl.h"
+#include "confdb/confdb.h"
+
+errno_t sssctl_config_check(struct sss_cmdline *cmdline,
+                            struct sss_tool_ctx *tool_ctx,
+                            void *pvt)
+{
+#ifdef HAVE_LIBINI_CONFIG_V1_3
+    errno_t ret;
+    struct ini_errobj *errobj = NULL;
+    struct ini_errobj *errobj_merge_err = NULL;
+    struct ini_errobj *errobj_merge_ok = NULL;
+    struct sss_ini_initdata *init_data;
+    TALLOC_CTX *tmp_ctx = NULL;
+
+    tmp_ctx = talloc_new(NULL);
+    init_data = sss_ini_initdata_init(tmp_ctx);
+    if (!init_data) {
+        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory.\n");
+        ret = ENOMEM;
+        goto done;
+    }
+
+    /* Open config file */
+    ret = sss_ini_config_file_open(init_data, SSSD_CONFIG_FILE);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_TRACE_FUNC,
+              "sss_ini_config_file_open failed: %s [%d]\n", strerror(ret),
+               ret);
+        goto done;
+    }
+
+    /* Check the file permissions */
+    ret = sss_ini_config_access_check(init_data);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE,
+              "Permission check on config file failed.\n");
+        ret = EPERM;
+        goto done;
+    }
+
+    ret = sss_ini_get_config(init_data,
+                             SSSD_CONFIG_FILE,
+                             CONFDB_DEFAULT_CONFIG_DIR);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_FATAL_FAILURE, "Failed to load configuration\n");
+        goto done;
+    }
+
+    ret = ini_errobj_create(&errobj);
+    if (ret) {
+        goto done;
+    }
+
+    /* Read rules */
+    ret = sss_ini_call_validators_errobj(init_data,
+                                         SSSDDATADIR"/cfg_rules.ini",
+                                         errobj);
+    if (ret) {
+        goto done;
+    }
+
+    /* Output from validators */
+    printf(_("Issues identified by validators: %lu\n"),
+           ini_errobj_count(errobj));
+    while(!ini_errobj_no_more_msgs(errobj)) {
+        printf("%s\n", ini_errobj_get_msg(errobj));
+        ini_errobj_next(errobj);
+    }
+
+    /* Merging issues */
+    ret = ini_errobj_create(&errobj_merge_err);
+    if (ret) {
+        goto done;
+    }
+
+    ret = sss_ini_get_merge_error_with_errobj(init_data, errobj_merge_err);
+    if (ret != EOK) {
+        goto done;
+    }
+
+    printf("\n");
+    printf(_("Messages generated during configuration merging: %lu\n"),
+           ini_errobj_count(errobj_merge_err));
+    while(!ini_errobj_no_more_msgs(errobj_merge_err)) {
+        printf("%s\n", ini_errobj_get_msg(errobj_merge_err));
+        ini_errobj_next(errobj_merge_err);
+    }
+
+    /* Used snippet files */
+    ret = ini_errobj_create(&errobj_merge_ok);
+    if (ret) {
+        goto done;
+    }
+
+    ret = sss_ini_get_merge_success_with_errobj(init_data, errobj_merge_ok);
+    if (ret != EOK) {
+        goto done;
+    }
+
+    printf("\n");
+    printf(_("Used configuration snippet files: %lu\n"),
+           ini_errobj_count(errobj_merge_ok));
+    while(!ini_errobj_no_more_msgs(errobj_merge_ok)) {
+        printf("%s\n", ini_errobj_get_msg(errobj_merge_ok));
+        ini_errobj_next(errobj_merge_ok);
+    }
+
+    ret = EOK;
+
+done:
+    ini_errobj_destroy(&errobj);
+    ini_errobj_destroy(&errobj_merge_err);
+    ini_errobj_destroy(&errobj_merge_ok);
+    return ret;
+#else /* !HAVE_LIBINI_CONFIG_V1_3 */
+    ERROR("config-check command is not available\n");
+    return EOK;
+#endif /* HAVE_LIBINI_CONFIG_V1_3 */
+}
diff --git a/src/util/sss_ini.c b/src/util/sss_ini.c
index 78f5490..12966cb 100644
--- a/src/util/sss_ini.c
+++ b/src/util/sss_ini.c
@@ -332,7 +332,45 @@ int sss_ini_get_config(struct sss_ini_initdata *init_data,
 #endif
 }
 
+#ifdef HAVE_LIBINI_CONFIG_V1_3
+int sss_ini_get_merge_success_with_errobj(struct sss_ini_initdata *init_data,
+                                          struct ini_errobj *errobj)
+{
+    int ret;
+    uint32_t i = 0;
+    char *msg = NULL;
+
+    while (ref_array_get(init_data->ra_success_list, i, &msg) != NULL) {
+        ret = ini_errobj_add_msg(errobj, "%s", msg);
+        if (ret != EOK) {
+            return ret;
+        }
+        i++;
+    }
+
+    return EOK;
+}
+#endif /* HAVE_LIBINI_CONFIG_V1_3 */
 
+#ifdef HAVE_LIBINI_CONFIG_V1_3
+int sss_ini_get_merge_error_with_errobj(struct sss_ini_initdata *init_data,
+                                        struct ini_errobj *errobj)
+{
+    int ret;
+    uint32_t i = 0;
+    char *msg = NULL;
+
+    while (ref_array_get(init_data->ra_error_list, i, &msg) != NULL) {
+        ret = ini_errobj_add_msg(errobj, "%s", msg);
+        if (ret != EOK) {
+            return ret;
+        }
+        i++;
+    }
+
+    return EOK;
+}
+#endif /* HAVE_LIBINI_CONFIG_V1_3 */
 
 /* Get configuration object */
 
diff --git a/src/util/sss_ini.h b/src/util/sss_ini.h
index 77943d6..5617815 100644
--- a/src/util/sss_ini.h
+++ b/src/util/sss_ini.h
@@ -92,6 +92,14 @@ int sss_ini_call_validators(struct sss_ini_initdata *data,
 int sss_ini_call_validators_errobj(struct sss_ini_initdata *data,
                                    const char *rules_path,
                                    struct ini_errobj *errobj);
+
+/* Get list of snippet parsing errors as ini_errobj */
+int sss_ini_get_merge_error_with_errobj(struct sss_ini_initdata *init_data,
+                                        struct ini_errobj *errobj);
+
+/* Get list of merged snippets as ini_errobj */
+int sss_ini_get_merge_success_with_errobj(struct sss_ini_initdata *init_data,
+                                          struct ini_errobj *errobj);
 #endif /* HAVE_LIBINI_CONFIG_V1_3 */
 
 
-- 
2.5.0

