From 5328aaeea84268b6d4e26cd33a2b3e8ea89bc349 Mon Sep 17 00:00:00 2001 From: Michal Zidek Date: Tue, 15 Jul 2014 12:00:36 -0400 Subject: Add function confdb_set_string. Part of fix for: https://fedorahosted.org/sssd/ticket/2367 Reviewed-by: Pavel Reichl --- src/confdb/confdb.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/confdb/confdb.h | 22 +++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/confdb/confdb.c b/src/confdb/confdb.c index 15de9616f..ae7abd73f 100644 --- a/src/confdb/confdb.c +++ b/src/confdb/confdb.c @@ -369,6 +369,77 @@ done: return ret; } +int confdb_set_string(struct confdb_ctx *cdb, + const char *section, + const char *attribute, + char *val) +{ + TALLOC_CTX *tmp_ctx; + struct ldb_dn *dn; + char *secdn; + struct ldb_message *msg; + int ret, lret; + + tmp_ctx = talloc_new(NULL); + if (!tmp_ctx) { + return ENOMEM; + } + + ret = parse_section(tmp_ctx, section, &secdn, NULL); + if (ret != EOK) { + goto done; + } + + dn = ldb_dn_new(tmp_ctx, cdb->ldb, secdn); + if (!dn) { + ret = EIO; + goto done; + } + + msg = ldb_msg_new(tmp_ctx); + if (!msg) { + ret = ENOMEM; + goto done; + } + + msg->dn = dn; + + lret = ldb_msg_add_empty(msg, attribute, LDB_FLAG_MOD_REPLACE, NULL); + if (lret != LDB_SUCCESS) { + DEBUG(SSSDBG_MINOR_FAILURE, + "ldb_msg_add_empty failed: [%s]\n", ldb_strerror(lret)); + ret = EIO; + goto done; + } + + lret = ldb_msg_add_string(msg, attribute, val); + if (lret != LDB_SUCCESS) { + DEBUG(SSSDBG_MINOR_FAILURE, + "ldb_msg_add_string failed: [%s]\n", ldb_strerror(lret)); + ret = EIO; + goto done; + } + + lret = ldb_modify(cdb->ldb, msg); + if (lret != LDB_SUCCESS) { + DEBUG(SSSDBG_MINOR_FAILURE, + "ldb_modify failed: [%s]\n", ldb_strerror(lret)); + ret = EIO; + goto done; + } + + ret = EOK; + +done: + talloc_free(tmp_ctx); + if (ret != EOK) { + DEBUG(SSSDBG_CRIT_FAILURE, + "Failed to set [%s] from [%s], error [%d] (%s)\n", + attribute, section, ret, strerror(ret)); + } + return ret; +} + int confdb_get_string(struct confdb_ctx *cdb, TALLOC_CTX *ctx, const char *section, const char *attribute, const char *defstr, char **result) diff --git a/src/confdb/confdb.h b/src/confdb/confdb.h index 4ac69ebef..014903c4b 100644 --- a/src/confdb/confdb.h +++ b/src/confdb/confdb.h @@ -460,6 +460,28 @@ int confdb_set_bool(struct confdb_ctx *cdb, const char *attribute, bool val); +/** + * @brief Convenience function to set a single-valued attribute as a string + * + * @param[in] cdb The connection object to the confdb + * @param[in] section The ConfDB section to update. This is constructed from + * the format of the sssd.conf file. All sections start + * with 'config/'. Subsections are separated by slashes. + * e.g. [domain/LDAP] in sssd.conf would translate to + * config/domain/LDAP + * @param[in] attribute The name of the attribute to update + * @param[in] val New value of the attribute. + * + * @return 0 - Successfully retrieved the entry (or used the default) + * @return ENOMEM - There was insufficient memory to complete the operation + * @return EINVAL - The section could not be parsed + * @return EIO - An I/O error occurred while communicating with the ConfDB + */ +int confdb_set_string(struct confdb_ctx *cdb, + const char *section, + const char *attribute, + char *val); + /** * @brief Convenience function to retrieve a single-valued attribute as a * null-terminated array of strings -- cgit