From eea2776ff9ce027f048ba23ccea5132c26cfca43 Mon Sep 17 00:00:00 2001 From: Alexey Tikhonov Date: Wed, 4 Sep 2024 13:48:38 +0200 Subject: [PATCH] SYSDB: only monitor (and tests) should create cache files Everything else (providers, responders, tools) should only connect to. --- src/db/sysdb.h | 1 + src/db/sysdb_init.c | 14 ++++++++--- src/db/sysdb_private.h | 1 + src/monitor/monitor.c | 2 +- src/tests/auth-tests.c | 2 +- src/tests/cmocka/test_ldap_id_cleanup.c | 2 +- src/tests/cmocka/test_sysdb_views.c | 2 +- src/tests/common.h | 6 +++++ src/tests/common_dom.c | 32 +++++++++++++++++++++++-- src/tests/sysdb-tests.c | 2 +- src/tests/sysdb_ssh-tests.c | 2 +- 11 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/db/sysdb.h b/src/db/sysdb.h index 1c7c8f5e8c7..710a51eee92 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -804,6 +804,7 @@ struct sysdb_upgrade_ctx { int sysdb_init_ext(TALLOC_CTX *mem_ctx, struct sss_domain_info *domains, + bool dont_create_cache, struct sysdb_upgrade_ctx *upgrade_ctx); /* used to initialize only one domain database. diff --git a/src/db/sysdb_init.c b/src/db/sysdb_init.c index a078cd9a18a..84049403516 100644 --- a/src/db/sysdb_init.c +++ b/src/db/sysdb_init.c @@ -776,10 +776,12 @@ static int sysdb_timestamp_cache_connect(struct sysdb_ctx *sysdb, int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx, struct sss_domain_info *domain, const char *db_path, + bool dont_create_cache, struct sysdb_dom_upgrade_ctx *upgrade_ctx, struct sysdb_ctx **_ctx) { TALLOC_CTX *tmp_ctx = NULL; + bool ldb_file_missing; struct sysdb_ctx *sysdb; int ret; @@ -799,6 +801,11 @@ int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx, if (ret != EOK) { goto done; } + ldb_file_missing = (access(sysdb->ldb_file, F_OK) == -1 && errno == ENOENT); + if (ldb_file_missing && dont_create_cache) { + ret = ENOENT; + goto done; + } DEBUG(SSSDBG_FUNC_DATA, "DB File for %s: %s\n", domain->name, sysdb->ldb_file); if (sysdb->ldb_ts_file) { @@ -833,11 +840,12 @@ int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx, int sysdb_init(TALLOC_CTX *mem_ctx, struct sss_domain_info *domains) { - return sysdb_init_ext(mem_ctx, domains, NULL); + return sysdb_init_ext(mem_ctx, domains, true, NULL); } int sysdb_init_ext(TALLOC_CTX *mem_ctx, struct sss_domain_info *domains, + bool dont_create_cache, struct sysdb_upgrade_ctx *upgrade_ctx) { struct sss_domain_info *dom; @@ -868,7 +876,7 @@ int sysdb_init_ext(TALLOC_CTX *mem_ctx, dom_upgrade_ctx = NULL; } - ret = sysdb_domain_init_internal(tmp_ctx, dom, DB_PATH, + ret = sysdb_domain_init_internal(tmp_ctx, dom, DB_PATH, dont_create_cache, dom_upgrade_ctx, &sysdb); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, @@ -892,5 +900,5 @@ int sysdb_domain_init(TALLOC_CTX *mem_ctx, struct sysdb_ctx **_ctx) { return sysdb_domain_init_internal(mem_ctx, domain, - db_path, NULL, _ctx); + db_path, true, NULL, _ctx); } diff --git a/src/db/sysdb_private.h b/src/db/sysdb_private.h index ce3f40124ca..f06f8cb7a4b 100644 --- a/src/db/sysdb_private.h +++ b/src/db/sysdb_private.h @@ -164,6 +164,7 @@ struct sysdb_dom_upgrade_ctx { int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx, struct sss_domain_info *domain, const char *db_path, + bool dont_create_cache, struct sysdb_dom_upgrade_ctx *upgrade_ctx, struct sysdb_ctx **_ctx); diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c index eebef40040e..6a921251e40 100644 --- a/src/monitor/monitor.c +++ b/src/monitor/monitor.c @@ -1556,7 +1556,7 @@ static int monitor_process_init(struct mt_ctx *ctx) } db_up_ctx.cdb = ctx->cdb; - ret = sysdb_init_ext(tmp_ctx, ctx->domains, &db_up_ctx); + ret = sysdb_init_ext(tmp_ctx, ctx->domains, false, &db_up_ctx); if (ret != EOK) { DEBUG(SSSDBG_TRACE_FUNC, "sysdb_init_ext() failed: '%s'\n", sss_strerror(ret)); diff --git a/src/tests/auth-tests.c b/src/tests/auth-tests.c index c8f99b63aef..5062a3ed4a4 100644 --- a/src/tests/auth-tests.c +++ b/src/tests/auth-tests.c @@ -134,7 +134,7 @@ static int setup_sysdb_tests(struct sysdb_test_ctx **ctx) return ret; } - ret = sssd_domain_init(test_ctx, test_ctx->confdb, "FILES", + ret = test_domain_init(test_ctx, test_ctx->confdb, "FILES", TESTS_PATH, &test_ctx->domain); if (ret != EOK) { ck_abort_msg("Could not initialize connection to the sysdb (%d)", ret); diff --git a/src/tests/cmocka/test_ldap_id_cleanup.c b/src/tests/cmocka/test_ldap_id_cleanup.c index ae5b0dd663b..e168aa301db 100644 --- a/src/tests/cmocka/test_ldap_id_cleanup.c +++ b/src/tests/cmocka/test_ldap_id_cleanup.c @@ -99,7 +99,7 @@ static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate) "config/domain/FILES", "cache_credentials", val); assert_int_equal(ret, EOK); - ret = sssd_domain_init(test_ctx, test_ctx->confdb, "FILES", + ret = test_domain_init(test_ctx, test_ctx->confdb, "FILES", TESTS_PATH, &test_ctx->domain); assert_int_equal(ret, EOK); diff --git a/src/tests/cmocka/test_sysdb_views.c b/src/tests/cmocka/test_sysdb_views.c index d3a6ed5707c..b7b06337ca4 100644 --- a/src/tests/cmocka/test_sysdb_views.c +++ b/src/tests/cmocka/test_sysdb_views.c @@ -107,7 +107,7 @@ static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate) "config/domain/FILES", "cache_credentials", val); assert_int_equal(ret, EOK); - ret = sssd_domain_init(test_ctx, test_ctx->confdb, "FILES", + ret = test_domain_init(test_ctx, test_ctx->confdb, "FILES", TESTS_PATH, &test_ctx->domain); assert_int_equal(ret, EOK); diff --git a/src/tests/common.h b/src/tests/common.h index 662db17ca08..323785dd2b7 100644 --- a/src/tests/common.h +++ b/src/tests/common.h @@ -92,6 +92,12 @@ struct sss_test_ctx *create_ev_test_ctx(TALLOC_CTX *mem_ctx); void reset_ldb_errstrings(struct sss_domain_info *dom); +errno_t test_domain_init(TALLOC_CTX *mem_ctx, + struct confdb_ctx *cdb, + const char *domain_name, + const char *db_path, + struct sss_domain_info **_domain); + struct sss_test_ctx * create_multidom_test_ctx(TALLOC_CTX *mem_ctx, const char *tests_path, diff --git a/src/tests/common_dom.c b/src/tests/common_dom.c index 267d5f50f9a..a00c024c7e6 100644 --- a/src/tests/common_dom.c +++ b/src/tests/common_dom.c @@ -181,6 +181,34 @@ void reset_ldb_errstrings(struct sss_domain_info *dom) } } +errno_t test_domain_init(TALLOC_CTX *mem_ctx, + struct confdb_ctx *cdb, + const char *domain_name, + const char *db_path, + struct sss_domain_info **_domain) +{ + /* This is a replacement of `sssd_domain_init()` + * that uses `sysdb_domain_init_internal()` instead of `sysdb_domain_init()` + * under the hood to let tests create sysdb cache files + */ + int ret; + + ret = confdb_get_domain(cdb, domain_name, _domain); + if (ret != EOK) { + DEBUG(SSSDBG_OP_FAILURE, "Error retrieving domain configuration.\n"); + return ret; + } + + ret = sysdb_domain_init_internal(mem_ctx, *_domain, + db_path, false, NULL, &(*_domain)->sysdb); + if (ret != EOK) { + DEBUG(SSSDBG_OP_FAILURE, "Error opening cache database.\n"); + return ret; + } + + return EOK; +} + static errno_t mock_domain(TALLOC_CTX *mem_ctx, struct confdb_ctx *cdb, @@ -192,9 +220,9 @@ mock_domain(TALLOC_CTX *mem_ctx, errno_t ret; /* initialize sysdb */ - ret = sssd_domain_init(mem_ctx, cdb, name, db_path, &domain); + ret = test_domain_init(mem_ctx, cdb, name, db_path, &domain); if (ret != EOK) { - DEBUG(SSSDBG_CRIT_FAILURE, "sssd_domain_init() of %s failed " + DEBUG(SSSDBG_CRIT_FAILURE, "test_domain_init() of %s failed " "[%d]: %s\n", name, ret, sss_strerror(ret)); goto done; } diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c index f9f347da7ab..8d055203985 100644 --- a/src/tests/sysdb-tests.c +++ b/src/tests/sysdb-tests.c @@ -151,7 +151,7 @@ static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate) return ret; } - ret = sssd_domain_init(test_ctx, test_ctx->confdb, "FILES", + ret = test_domain_init(test_ctx, test_ctx->confdb, "FILES", TESTS_PATH, &test_ctx->domain); if (ret != EOK) { ck_abort_msg("Could not initialize connection to the sysdb (%d)", ret); diff --git a/src/tests/sysdb_ssh-tests.c b/src/tests/sysdb_ssh-tests.c index f21e50c19ee..393049015d6 100644 --- a/src/tests/sysdb_ssh-tests.c +++ b/src/tests/sysdb_ssh-tests.c @@ -129,7 +129,7 @@ static int setup_sysdb_tests(struct sysdb_test_ctx **ctx) return ret; } - ret = sssd_domain_init(test_ctx, test_ctx->confdb, "FILES", + ret = test_domain_init(test_ctx, test_ctx->confdb, "FILES", TESTS_PATH, &test_ctx->domain); if (ret != EOK) { ck_abort_msg("Could not initialize connection to the sysdb (%d)", ret);