Blame SOURCES/0003-sssctl-sssctl-config-check-alternative-config-file.patch

a4c784
From 61f4aaa56ea876fb75c1366c938818b7799408ab Mon Sep 17 00:00:00 2001
a4c784
From: Tomas Halman <thalman@redhat.com>
a4c784
Date: Wed, 29 Apr 2020 16:40:36 +0200
a4c784
Subject: [PATCH] sssctl: sssctl config-check alternative config file
a4c784
a4c784
The sssctl config-check now allows to specify alternative config
a4c784
file so it can be tested before rewriting system configuration.
a4c784
a4c784
    sssctl config-check -c ./sssd.conf
a4c784
a4c784
Configuration snippets are looked up in the same place under
a4c784
conf.d directory. It would be in ./conf.d/ for the example above.
a4c784
a4c784
Resolves:
a4c784
https://github.com/SSSD/sssd/issues/5142
a4c784
a4c784
Reviewed-by: Pawel Polawski <ppolawsk@redhat.com>
a4c784
---
a4c784
 src/confdb/confdb.h              |  6 ++--
a4c784
 src/tools/sssctl/sssctl_config.c | 56 ++++++++++++++++++++++++++++----
a4c784
 2 files changed, 53 insertions(+), 9 deletions(-)
a4c784
a4c784
diff --git a/src/confdb/confdb.h b/src/confdb/confdb.h
a4c784
index 0a5593232..a2b58e12a 100644
a4c784
--- a/src/confdb/confdb.h
a4c784
+++ b/src/confdb/confdb.h
a4c784
@@ -40,8 +40,10 @@
a4c784
 
a4c784
 #define CONFDB_DEFAULT_CFG_FILE_VER 2
a4c784
 #define CONFDB_FILE "config.ldb"
a4c784
-#define SSSD_CONFIG_FILE SSSD_CONF_DIR"/sssd.conf"
a4c784
-#define CONFDB_DEFAULT_CONFIG_DIR SSSD_CONF_DIR"/conf.d"
a4c784
+#define SSSD_CONFIG_FILE_NAME "sssd.conf"
a4c784
+#define SSSD_CONFIG_FILE SSSD_CONF_DIR"/"SSSD_CONFIG_FILE_NAME
a4c784
+#define CONFDB_DEFAULT_CONFIG_DIR_NAME "conf.d"
a4c784
+#define CONFDB_DEFAULT_CONFIG_DIR SSSD_CONF_DIR"/"CONFDB_DEFAULT_CONFIG_DIR_NAME
a4c784
 #define SSSD_MIN_ID 1
a4c784
 #define SSSD_LOCAL_MINID 1000
a4c784
 #define CONFDB_DEFAULT_SHELL_FALLBACK "/bin/sh"
a4c784
diff --git a/src/tools/sssctl/sssctl_config.c b/src/tools/sssctl/sssctl_config.c
a4c784
index 74395b61c..de9f3de6e 100644
a4c784
--- a/src/tools/sssctl/sssctl_config.c
a4c784
+++ b/src/tools/sssctl/sssctl_config.c
a4c784
@@ -34,6 +34,29 @@
a4c784
 
a4c784
 
a4c784
 #ifdef HAVE_LIBINI_CONFIG_V1_3
a4c784
+
a4c784
+static char *sssctl_config_snippet_path(TALLOC_CTX *ctx, const char *path)
a4c784
+{
a4c784
+    char *tmp = NULL;
a4c784
+    const char delimiter = '/';
a4c784
+    char *dpos = NULL;
a4c784
+
a4c784
+    tmp = talloc_strdup(ctx, path);
a4c784
+    if (!tmp) {
a4c784
+        return NULL;
a4c784
+    }
a4c784
+
a4c784
+    dpos = strrchr(tmp, delimiter);
a4c784
+    if (dpos != NULL) {
a4c784
+        ++dpos;
a4c784
+        *dpos = '\0';
a4c784
+    } else {
a4c784
+        *tmp = '\0';
a4c784
+    }
a4c784
+
a4c784
+    return talloc_strdup_append(tmp, CONFDB_DEFAULT_CONFIG_DIR_NAME);
a4c784
+}
a4c784
+
a4c784
 errno_t sssctl_config_check(struct sss_cmdline *cmdline,
a4c784
                             struct sss_tool_ctx *tool_ctx,
a4c784
                             void *pvt)
a4c784
@@ -47,8 +70,15 @@ errno_t sssctl_config_check(struct sss_cmdline *cmdline,
a4c784
     size_t num_ra_error, num_ra_success;
a4c784
     char **strs = NULL;
a4c784
     TALLOC_CTX *tmp_ctx = NULL;
a4c784
-
a4c784
-    ret = sss_tool_popt(cmdline, NULL, SSS_TOOL_OPT_OPTIONAL, NULL, NULL);
a4c784
+    const char *config_path = NULL;
a4c784
+    const char *config_snippet_path = NULL;
a4c784
+    struct poptOption long_options[] = {
a4c784
+        {"config", 'c', POPT_ARG_STRING, &config_path,
a4c784
+            0, _("Specify a non-default config file"), NULL},
a4c784
+        POPT_TABLEEND
a4c784
+    };
a4c784
+
a4c784
+    ret = sss_tool_popt(cmdline, long_options, SSS_TOOL_OPT_OPTIONAL, NULL, NULL);
a4c784
     if (ret != EOK) {
a4c784
         DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
a4c784
         return ret;
a4c784
@@ -62,17 +92,29 @@ errno_t sssctl_config_check(struct sss_cmdline *cmdline,
a4c784
         goto done;
a4c784
     }
a4c784
 
a4c784
+    if (config_path != NULL) {
a4c784
+        config_snippet_path = sssctl_config_snippet_path(tmp_ctx, config_path);
a4c784
+        if (config_snippet_path == NULL) {
a4c784
+            DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create snippet path\n");
a4c784
+            ret = ENOMEM;
a4c784
+            goto done;
a4c784
+        }
a4c784
+    } else {
a4c784
+        config_path = SSSD_CONFIG_FILE;
a4c784
+        config_snippet_path = CONFDB_DEFAULT_CONFIG_DIR;
a4c784
+    }
a4c784
+
a4c784
     ret = sss_ini_read_sssd_conf(init_data,
a4c784
-                                 SSSD_CONFIG_FILE,
a4c784
-                                 CONFDB_DEFAULT_CONFIG_DIR);
a4c784
+                                 config_path,
a4c784
+                                 config_snippet_path);
a4c784
 
a4c784
     if (ret == ERR_INI_OPEN_FAILED) {
a4c784
-        PRINT("Failed to open %s\n", SSSD_CONFIG_FILE);
a4c784
+        PRINT("Failed to open %s\n", config_path);
a4c784
         goto done;
a4c784
     }
a4c784
 
a4c784
     if (!sss_ini_exists(init_data)) {
a4c784
-        PRINT("File %1$s does not exist.\n", SSSD_CONFIG_FILE);
a4c784
+        PRINT("File %1$s does not exist.\n", config_path);
a4c784
     }
a4c784
 
a4c784
     if (ret == ERR_INI_INVALID_PERMISSION) {
a4c784
@@ -83,7 +125,7 @@ errno_t sssctl_config_check(struct sss_cmdline *cmdline,
a4c784
 
a4c784
     if (ret == ERR_INI_PARSE_FAILED) {
a4c784
         PRINT("Failed to load configuration from %s.\n",
a4c784
-              SSSD_CONFIG_FILE);
a4c784
+              config_path);
a4c784
         goto done;
a4c784
     }
a4c784
 
a4c784
-- 
a4c784
2.21.1
a4c784