From ce0b07060ec7b95bdf23da2868c88e29bc6cad24 Mon Sep 17 00:00:00 2001 From: Alexey Tikhonov Date: Fri, 30 Aug 2024 18:20:10 +0200 Subject: [PATCH 1/4] TOOLS: skip confdb_init if no context ptr provided --- src/tools/common/sss_tools.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/tools/common/sss_tools.c b/src/tools/common/sss_tools.c index b7200554c26..9aa182ab049 100644 --- a/src/tools/common/sss_tools.c +++ b/src/tools/common/sss_tools.c @@ -90,11 +90,15 @@ static void sss_tool_common_opts(struct sss_tool_ctx *tool_ctx, static errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx, struct confdb_ctx **_confdb) { - struct confdb_ctx *confdb; char *path; errno_t ret; struct stat statbuf; + if (_confdb == NULL) { + DEBUG(SSSDBG_FATAL_FAILURE, "Bad argument\n"); + return EFAULT; + } + path = talloc_asprintf(mem_ctx, "%s/%s", DB_PATH, CONFDB_FILE); if (path == NULL) { return ENOMEM; @@ -108,7 +112,7 @@ static errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx, return ret; } - ret = confdb_init(mem_ctx, &confdb, path); + ret = confdb_init(mem_ctx, _confdb, path); talloc_zfree(path); if (ret != EOK) { DEBUG(SSSDBG_FATAL_FAILURE, "Unable to connect to config DB [%d]: %s\n", @@ -116,10 +120,6 @@ static errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx, return ret; } - if (_confdb != NULL) { - *_confdb = confdb; - } - return EOK; } From c78f328ba679da64be332c4e39affd4220698feb Mon Sep 17 00:00:00 2001 From: Alexey Tikhonov Date: Fri, 30 Aug 2024 18:36:02 +0200 Subject: [PATCH 2/4] TOOLS: get rid of code duplication Take a note this also enforces check for existance of config.ldb in additional code paths (this is intentional side effect, see a008accecd6d0b35e8d57d738ee3d05863aa7d0f) --- src/tools/common/sss_tools.c | 33 ++------------------------------- src/tools/common/sss_tools.h | 2 +- src/tools/sssctl/sssctl_data.c | 2 +- src/tools/sssctl/sssctl_logs.c | 2 +- 4 files changed, 5 insertions(+), 34 deletions(-) diff --git a/src/tools/common/sss_tools.c b/src/tools/common/sss_tools.c index 9aa182ab049..73828757476 100644 --- a/src/tools/common/sss_tools.c +++ b/src/tools/common/sss_tools.c @@ -87,10 +87,9 @@ static void sss_tool_common_opts(struct sss_tool_ctx *tool_ctx, poptFreeContext(pc); } -static errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx, - struct confdb_ctx **_confdb) +errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx, struct confdb_ctx **_confdb) { - char *path; + static const char *path = DB_PATH"/"CONFDB_FILE; errno_t ret; struct stat statbuf; @@ -99,11 +98,6 @@ static errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx, return EFAULT; } - path = talloc_asprintf(mem_ctx, "%s/%s", DB_PATH, CONFDB_FILE); - if (path == NULL) { - return ENOMEM; - } - ret = stat(path, &statbuf); if (ret != 0) { ret = errno; @@ -113,7 +107,6 @@ static errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx, } ret = confdb_init(mem_ctx, _confdb, path); - talloc_zfree(path); if (ret != EOK) { DEBUG(SSSDBG_FATAL_FAILURE, "Unable to connect to config DB [%d]: %s\n", ret, sss_strerror(ret)); @@ -599,25 +592,3 @@ errno_t sss_tool_parse_name(TALLOC_CTX *mem_ctx, return ret; } - -errno_t sss_tool_connect_to_confdb(TALLOC_CTX *ctx, struct confdb_ctx **cdb_ctx) -{ - int ret; - char *confdb_path = NULL; - - confdb_path = talloc_asprintf(ctx, "%s/%s", DB_PATH, CONFDB_FILE); - if (confdb_path == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, - "Could not allocate memory for confdb path\n"); - return ENOMEM; - } - - ret = confdb_init(ctx, cdb_ctx, confdb_path); - if (ret != EOK) { - DEBUG(SSSDBG_CRIT_FAILURE, - "Could not initialize connection to the confdb\n"); - } - - talloc_free(confdb_path); - return ret; -} diff --git a/src/tools/common/sss_tools.h b/src/tools/common/sss_tools.h index 69d35bde0c6..45526e4fe81 100644 --- a/src/tools/common/sss_tools.h +++ b/src/tools/common/sss_tools.h @@ -101,6 +101,6 @@ errno_t sss_tool_parse_name(TALLOC_CTX *mem_ctx, struct sss_domain_info **_domain); -errno_t sss_tool_connect_to_confdb(TALLOC_CTX *ctx, struct confdb_ctx **cdb_ctx); +errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx, struct confdb_ctx **_confdb); #endif /* SRC_TOOLS_COMMON_SSS_TOOLS_H_ */ diff --git a/src/tools/sssctl/sssctl_data.c b/src/tools/sssctl/sssctl_data.c index beeb6361cac..43bfd73fc86 100644 --- a/src/tools/sssctl/sssctl_data.c +++ b/src/tools/sssctl/sssctl_data.c @@ -362,7 +362,7 @@ static errno_t sssctl_cache_index_action(enum sysdb_index_actions action, if (domains == NULL) { /* If the user selected no domain, act on all of them */ - ret = sss_tool_connect_to_confdb(tmp_ctx, &confdb); + ret = sss_tool_confdb_init(tmp_ctx, &confdb); if (ret != EOK) { DEBUG(SSSDBG_OP_FAILURE, "Could not connect to configuration database.\n"); diff --git a/src/tools/sssctl/sssctl_logs.c b/src/tools/sssctl/sssctl_logs.c index 75e855bb901..c719dcbf0b2 100644 --- a/src/tools/sssctl/sssctl_logs.c +++ b/src/tools/sssctl/sssctl_logs.c @@ -567,7 +567,7 @@ errno_t sssctl_debug_level(struct sss_cmdline *cmdline, targets = get_targets(ctx, pc_services, pc_domains); CHECK(targets == NULL, fini, "Could not allocate memory."); - ret = sss_tool_connect_to_confdb(ctx, &ctx->confdb); + ret = sss_tool_confdb_init(ctx, &ctx->confdb); CHECK(ret != EOK, fini, "Could not connect to configuration database."); ret = get_confdb_sections(ctx, ctx->confdb, &ctx->sections); From 12d38cc1a3939a27b2b652ceb86c8dedbda43ab2 Mon Sep 17 00:00:00 2001 From: Alexey Tikhonov Date: Fri, 30 Aug 2024 18:51:34 +0200 Subject: [PATCH 3/4] TOOLS: use `sss_tool_confdb_init()` everywhere Take a note this also enforces check for existance of config.ldb in additional code paths (this is intentional side effect, see a008accecd6d0b35e8d57d738ee3d05863aa7d0f) --- src/tools/sss_cache.c | 16 ++++------------ src/tools/sss_seed.c | 11 ++--------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/src/tools/sss_cache.c b/src/tools/sss_cache.c index 79de13ac872..75daddddbb8 100644 --- a/src/tools/sss_cache.c +++ b/src/tools/sss_cache.c @@ -27,6 +27,7 @@ #include "util/util.h" #include "tools/tools_util.h" +#include "tools/common/sss_tools.h" #include "db/sysdb.h" #include "db/sysdb_services.h" #include "db/sysdb_autofs.h" @@ -661,22 +662,13 @@ static errno_t invalidate_entry(TALLOC_CTX *ctx, static errno_t init_domains(struct cache_tool_ctx *ctx, const char *domain) { - char *confdb_path; int ret; struct sss_domain_info *dinfo; - confdb_path = talloc_asprintf(ctx, "%s/%s", DB_PATH, CONFDB_FILE); - if (confdb_path == NULL) { - return ENOMEM; - } - - /* Connect to the conf db */ - ret = confdb_init(ctx, &ctx->confdb, confdb_path); - talloc_free(confdb_path); + ret = sss_tool_confdb_init(ctx, &ctx->confdb); if (ret != EOK) { - DEBUG(SSSDBG_CRIT_FAILURE, - "Could not initialize connection to the confdb\n"); - return ret; + ERROR("Can't find configuration db, was SSSD configured and run?\n"); + return ERR_NO_DOMAIN_ENABLED; } if (domain) { diff --git a/src/tools/sss_seed.c b/src/tools/sss_seed.c index 07327da67db..b1c30bebc1d 100644 --- a/src/tools/sss_seed.c +++ b/src/tools/sss_seed.c @@ -33,6 +33,7 @@ #include "util/util.h" #include "db/sysdb.h" #include "tools/tools_util.h" +#include "tools/common/sss_tools.h" #include "confdb/confdb.h" #ifndef BUFSIZE @@ -628,7 +629,6 @@ static int seed_init_db(TALLOC_CTX *mem_ctx, struct sysdb_ctx **_sysdb) { TALLOC_CTX *tmp_ctx = NULL; - char *confdb_path = NULL; struct confdb_ctx *confdb = NULL; struct sss_domain_info *domain = NULL; int ret = EOK; @@ -639,14 +639,7 @@ static int seed_init_db(TALLOC_CTX *mem_ctx, goto done; } - /* setup confdb */ - confdb_path = talloc_asprintf(tmp_ctx, "%s/%s", DB_PATH, CONFDB_FILE); - if (confdb_path == NULL) { - ret = ENOMEM; - goto done; - } - - ret = confdb_init(tmp_ctx, &confdb, confdb_path); + ret = sss_tool_confdb_init(tmp_ctx, &confdb); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Could not initialize connection to the confdb\n"); From 0c3d9a00c1ea3e1f0112d7f4e371cfa9776732e8 Mon Sep 17 00:00:00 2001 From: Alexey Tikhonov Date: Wed, 4 Sep 2024 12:09:53 +0200 Subject: [PATCH 4/4] CONFDB: move sanity check closer to a place where argument is really used --- src/confdb/confdb.c | 5 +++++ src/tools/common/sss_tools.c | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/confdb/confdb.c b/src/confdb/confdb.c index 21e96634e61..252c557ce9a 100644 --- a/src/confdb/confdb.c +++ b/src/confdb/confdb.c @@ -650,6 +650,11 @@ int confdb_init(TALLOC_CTX *mem_ctx, int ret = EOK; mode_t old_umask; + if (cdb_ctx == NULL) { + DEBUG(SSSDBG_FATAL_FAILURE, "Bad argument\n"); + return EFAULT; + } + cdb = talloc_zero(mem_ctx, struct confdb_ctx); if (!cdb) return ENOMEM; diff --git a/src/tools/common/sss_tools.c b/src/tools/common/sss_tools.c index 73828757476..7c0a2d3a5a9 100644 --- a/src/tools/common/sss_tools.c +++ b/src/tools/common/sss_tools.c @@ -93,11 +93,6 @@ errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx, struct confdb_ctx **_confdb) errno_t ret; struct stat statbuf; - if (_confdb == NULL) { - DEBUG(SSSDBG_FATAL_FAILURE, "Bad argument\n"); - return EFAULT; - } - ret = stat(path, &statbuf); if (ret != 0) { ret = errno;