From 6d66c2c465861ff2558f2574eddf8315628ccc6d Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Wed, 29 Jun 2016 16:30:39 +0200 Subject: SYSDB: Allow passing a context to sysdb upgrade functions We decide on whether to upgrade or not based on a pointer value, not a boolean. This pointer points to a structure that the upgrade invoker (typically the monitor) can use to fill auxilary data the sysdb upgrade has no means of instantiating. Reviewed-by: Sumit Bose --- src/db/sysdb.h | 9 +++++++-- src/db/sysdb_init.c | 51 +++++++++++++++++++++++++++++++++----------------- src/db/sysdb_private.h | 7 ++++++- src/monitor/monitor.c | 4 +++- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/db/sysdb.h b/src/db/sysdb.h index b881310bf..2fa97c2f5 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -616,10 +616,15 @@ int sysdb_init(TALLOC_CTX *mem_ctx, struct sss_domain_info *domains); /* Same as sysdb_init, but additionally allows to change - * file ownership of the sysdb databases. */ + * file ownership of the sysdb databases and allow the + * upgrade via passing a context. */ +struct sysdb_upgrade_ctx { + int unused; +}; + int sysdb_init_ext(TALLOC_CTX *mem_ctx, struct sss_domain_info *domains, - bool allow_upgrade, + struct sysdb_upgrade_ctx *upgrade_ctx, bool chown_dbfile, uid_t uid, gid_t gid); diff --git a/src/db/sysdb_init.c b/src/db/sysdb_init.c index 7298c2b18..babd8ab92 100644 --- a/src/db/sysdb_init.c +++ b/src/db/sysdb_init.c @@ -364,6 +364,7 @@ static errno_t sysdb_ts_cache_upgrade(TALLOC_CTX *mem_ctx, static errno_t sysdb_domain_cache_upgrade(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb, + struct sysdb_dom_upgrade_ctx *upgrade_ctx, struct ldb_context *ldb, struct sss_domain_info *domain, const char *cur_version, @@ -611,7 +612,7 @@ done: static int sysdb_domain_cache_connect(struct sysdb_ctx *sysdb, struct sss_domain_info *domain, - bool allow_upgrade) + struct sysdb_dom_upgrade_ctx *upgrade_ctx) { errno_t ret; const char *version; @@ -628,15 +629,15 @@ static int sysdb_domain_cache_connect(struct sysdb_ctx *sysdb, &ldb, &version); switch (ret) { case ERR_SYSDB_VERSION_TOO_OLD: - if (allow_upgrade == false) { + if (upgrade_ctx == NULL) { DEBUG(SSSDBG_FATAL_FAILURE, "DB version too old [%s], expected [%s] for domain %s!\n", version, SYSDB_VERSION, domain->name); goto done; } - ret = sysdb_domain_cache_upgrade(tmp_ctx, sysdb, ldb, domain, version, - &version); + ret = sysdb_domain_cache_upgrade(tmp_ctx, sysdb, upgrade_ctx, + ldb, domain, version, &version); if (ret != EOK) { goto done; } @@ -676,7 +677,7 @@ done: static int sysdb_timestamp_cache_connect(struct sysdb_ctx *sysdb, struct sss_domain_info *domain, - bool allow_upgrade) + struct sysdb_dom_upgrade_ctx *upgrade_ctx) { errno_t ret; const char *version; @@ -699,7 +700,7 @@ static int sysdb_timestamp_cache_connect(struct sysdb_ctx *sysdb, &ldb, &version); switch (ret) { case ERR_SYSDB_VERSION_TOO_OLD: - if (allow_upgrade == false) { + if (upgrade_ctx == NULL) { DEBUG(SSSDBG_FATAL_FAILURE, "DB version too old [%s], expected [%s] for domain %s!\n", version, SYSDB_VERSION, domain->name); @@ -785,7 +786,7 @@ 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 allow_upgrade, + struct sysdb_dom_upgrade_ctx *upgrade_ctx, struct sysdb_ctx **_ctx) { TALLOC_CTX *tmp_ctx = NULL; @@ -815,7 +816,7 @@ int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx, "Timestamp file for %s: %s\n", domain->name, sysdb->ldb_ts_file); } - ret = sysdb_domain_cache_connect(sysdb, domain, allow_upgrade); + ret = sysdb_domain_cache_connect(sysdb, domain, upgrade_ctx); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Could not open the sysdb cache [%d]: %s\n", @@ -823,7 +824,7 @@ int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx, goto done; } - ret = sysdb_timestamp_cache_connect(sysdb, domain, allow_upgrade); + ret = sysdb_timestamp_cache_connect(sysdb, domain, upgrade_ctx); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Could not open the timestamp cache [%d]: %s\n", @@ -842,12 +843,12 @@ done: int sysdb_init(TALLOC_CTX *mem_ctx, struct sss_domain_info *domains) { - return sysdb_init_ext(mem_ctx, domains, false, false, 0, 0); + return sysdb_init_ext(mem_ctx, domains, NULL, false, 0, 0); } int sysdb_init_ext(TALLOC_CTX *mem_ctx, struct sss_domain_info *domains, - bool allow_upgrade, + struct sysdb_upgrade_ctx *upgrade_ctx, bool chown_dbfile, uid_t uid, gid_t gid) @@ -855,8 +856,10 @@ int sysdb_init_ext(TALLOC_CTX *mem_ctx, struct sss_domain_info *dom; struct sysdb_ctx *sysdb; int ret; + TALLOC_CTX *tmp_ctx; + struct sysdb_dom_upgrade_ctx *dom_upgrade_ctx; - if (allow_upgrade) { + if (upgrade_ctx != NULL) { /* check if we have an old sssd.ldb to upgrade */ ret = sysdb_check_upgrade_02(domains, DB_PATH); if (ret != EOK) { @@ -864,16 +867,27 @@ int sysdb_init_ext(TALLOC_CTX *mem_ctx, } } + tmp_ctx = talloc_new(mem_ctx); + if (tmp_ctx == NULL) { + return ENOMEM; + } + /* open a db for each domain */ for (dom = domains; dom; dom = dom->next) { + if (upgrade_ctx) { + dom_upgrade_ctx = talloc_zero(tmp_ctx, + struct sysdb_dom_upgrade_ctx); + } else { + dom_upgrade_ctx = NULL; + } - ret = sysdb_domain_init_internal(mem_ctx, dom, DB_PATH, - allow_upgrade, &sysdb); + ret = sysdb_domain_init_internal(tmp_ctx, dom, DB_PATH, + dom_upgrade_ctx, &sysdb); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Cannot connect to database for %s: [%d]: %s\n", dom->name, ret, sss_strerror(ret)); - return ret; + goto done; } if (chown_dbfile) { @@ -882,14 +896,17 @@ int sysdb_init_ext(TALLOC_CTX *mem_ctx, DEBUG(SSSDBG_CRIT_FAILURE, "Cannot chown databases for %s: [%d]: %s\n", dom->name, ret, sss_strerror(ret)); - return ret; + goto done; } } dom->sysdb = talloc_move(dom, &sysdb); } - return EOK; + ret = EOK; +done: + talloc_free(tmp_ctx); + return ret; } int sysdb_domain_init(TALLOC_CTX *mem_ctx, diff --git a/src/db/sysdb_private.h b/src/db/sysdb_private.h index fda33dc74..af3bde1b1 100644 --- a/src/db/sysdb_private.h +++ b/src/db/sysdb_private.h @@ -127,10 +127,15 @@ errno_t sysdb_ldb_connect(TALLOC_CTX *mem_ctx, const char *filename, int flags, struct ldb_context **_ldb); + +struct sysdb_dom_upgrade_ctx { + int unused; +}; + int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx, struct sss_domain_info *domain, const char *db_path, - bool allow_upgrade, + struct sysdb_dom_upgrade_ctx *upgrade_ctx, struct sysdb_ctx **_ctx); /* Upgrade routines */ diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c index 38ac44e4b..e515f0f59 100644 --- a/src/monitor/monitor.c +++ b/src/monitor/monitor.c @@ -2352,6 +2352,7 @@ static int monitor_process_init(struct mt_ctx *ctx, int num_providers; int ret; int error; + struct sysdb_upgrade_ctx db_up_ctx; /* Set up the environment variable for the Kerberos Replay Cache */ ret = confdb_get_string(ctx->cdb, ctx, @@ -2453,7 +2454,8 @@ static int monitor_process_init(struct mt_ctx *ctx, if (!tmp_ctx) { return ENOMEM; } - ret = sysdb_init_ext(tmp_ctx, ctx->domains, true, + + ret = sysdb_init_ext(tmp_ctx, ctx->domains, &db_up_ctx, true, ctx->uid, ctx->gid); if (ret != EOK) { SYSDB_VERSION_ERROR_DAEMON(ret); -- cgit