dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

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

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