From f1576dc717c2a997f8f191238b6414298a41ce65 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Mon, 22 Sep 2014 18:37:44 +0200 Subject: [PATCH 7/8] sysdb: sysdb_apply_default_override The default view is special in the sense that it is the baseline for every other view and that it always applies even if there is no view defined. To avoid useless additional processing the default view overrides are written directly to the corresponding cached object. --- src/db/sysdb.h | 4 ++++ src/db/sysdb_views.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/db/sysdb.h b/src/db/sysdb.h index 6381475..8af5454 100644 --- a/src/db/sysdb.h +++ b/src/db/sysdb.h @@ -421,6 +421,10 @@ errno_t sysdb_update_view_name(struct sysdb_ctx *sysdb, const char *view_name); errno_t sysdb_get_view_name(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb, char **view_name); +errno_t sysdb_apply_default_override(struct sysdb_ctx *sysdb, + struct sysdb_attrs *override_attrs, + struct ldb_dn *obj_dn); + /* Sysdb initialization. * call this function *only* once to initialize the database and get * the sysdb ctx */ diff --git a/src/db/sysdb_views.c b/src/db/sysdb_views.c index 6a9b36b..d4c97cd 100644 --- a/src/db/sysdb_views.c +++ b/src/db/sysdb_views.c @@ -406,3 +406,70 @@ done: talloc_zfree(tmp_ctx); return ret; } + +errno_t sysdb_apply_default_override(struct sysdb_ctx *sysdb, + struct sysdb_attrs *override_attrs, + struct ldb_dn *obj_dn) +{ + int ret; + TALLOC_CTX *tmp_ctx; + struct sysdb_attrs *attrs; + size_t c; + struct ldb_message_element *el = NULL; + const char *allowed_attrs[] = { SYSDB_UIDNUM, + SYSDB_GIDNUM, + SYSDB_GECOS, + SYSDB_HOMEDIR, + SYSDB_SHELL, + SYSDB_NAME, + NULL }; + bool override_attrs_found = false; + + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n"); + return ENOMEM; + } + + attrs = sysdb_new_attrs(tmp_ctx); + if (attrs == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "sysdb_new_attrs failed.\n"); + ret = ENOMEM; + goto done; + } + + for (c = 0; allowed_attrs[c] != NULL; c++) { + ret = sysdb_attrs_get_el_ext(override_attrs, allowed_attrs[c], false, + &el); + if (ret == EOK) { + override_attrs_found = true; + ret = sysdb_attrs_add_val(attrs, allowed_attrs[c], &el->values[0]); + if (ret != EOK) { + DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_add_val failed.\n"); + goto done; + } + DEBUG(SSSDBG_TRACE_ALL, "Override [%s] with [%.*s] for [%s].\n", + allowed_attrs[c], + (int) el->values[0].length, + el->values[0].data, + ldb_dn_get_linearized(obj_dn)); + } else if (ret != ENOENT) { + DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_get_el_ext failed.\n"); + goto done; + } + } + + if (override_attrs_found) { + ret = sysdb_set_entry_attr(sysdb, obj_dn, attrs, SYSDB_MOD_REP); + if (ret != EOK) { + DEBUG(SSSDBG_OP_FAILURE, "sysdb_set_entry_attr failed.\n"); + goto done; + } + } + + ret = EOK; + +done: + talloc_free(tmp_ctx); + return ret; +} -- 1.8.3.1