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

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