summaryrefslogtreecommitdiffstats
path: root/src/db/sysdb_subdomains.c
diff options
context:
space:
mode:
authorSimo Sorce <simo@redhat.com>2013-01-14 14:55:05 -0500
committerJakub Hrozek <jhrozek@redhat.com>2013-02-10 22:08:46 +0100
commit1187a07ed4207c1c326fdf83915dddfe472b8620 (patch)
tree203d17d6afe7b390bab6a57cca25d8290c92312c /src/db/sysdb_subdomains.c
parent3912262270a6449ebe1d3e92c27c217b4044f894 (diff)
downloadsssd-1187a07ed4207c1c326fdf83915dddfe472b8620.tar.gz
sssd-1187a07ed4207c1c326fdf83915dddfe472b8620.tar.xz
sssd-1187a07ed4207c1c326fdf83915dddfe472b8620.zip
Add sysdb_subdomain_store() function
Replaces sysdb_add_subdomain_attributes and is a public sysdb interface.
Diffstat (limited to 'src/db/sysdb_subdomains.c')
-rw-r--r--src/db/sysdb_subdomains.c135
1 files changed, 98 insertions, 37 deletions
diff --git a/src/db/sysdb_subdomains.c b/src/db/sysdb_subdomains.c
index 2b1de1265..d6692b6de 100644
--- a/src/db/sysdb_subdomains.c
+++ b/src/db/sysdb_subdomains.c
@@ -302,11 +302,24 @@ done:
return ret;
}
-static errno_t sysdb_add_subdomain_attributes(struct sysdb_ctx *sysdb,
- struct sysdb_subdom *domain_info)
+errno_t sysdb_subdomain_store(struct sysdb_ctx *sysdb,
+ const char *name, const char *realm,
+ const char *flat_name, const char *domain_id)
{
TALLOC_CTX *tmp_ctx;
struct ldb_message *msg;
+ struct ldb_dn *dn;
+ struct ldb_result *res;
+ const char *attrs[] = {"cn",
+ SYSDB_SUBDOMAIN_REALM,
+ SYSDB_SUBDOMAIN_FLAT,
+ SYSDB_SUBDOMAIN_ID,
+ NULL};
+ const char *tmp_str;
+ bool store = false;
+ int realm_flags = 0;
+ int flat_flags = 0;
+ int id_flags = 0;
int ret;
tmp_ctx = talloc_new(NULL);
@@ -314,72 +327,117 @@ static errno_t sysdb_add_subdomain_attributes(struct sysdb_ctx *sysdb,
return ENOMEM;
}
- msg = ldb_msg_new(tmp_ctx);
- if (msg == NULL) {
- ret = ENOMEM;
+ dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_DOM_BASE, name);
+ if (dn == NULL) {
+ ret = EIO;
+ goto done;
+ }
+ ret = ldb_search(sysdb->ldb, tmp_ctx, &res,
+ dn, LDB_SCOPE_BASE, attrs, NULL);
+ if (ret != LDB_SUCCESS) {
+ ret = EIO;
goto done;
}
- msg->dn = ldb_dn_new_fmt(msg, sysdb->ldb, SYSDB_DOM_BASE,
- domain_info->name);
- if (msg->dn == NULL) {
- ret = ENOMEM;
+ if (res->count == 0) {
+ ret = sysdb_domain_create(sysdb, name);
+ if (ret) {
+ goto done;
+ }
+ store = true;
+ if (realm) realm_flags = LDB_FLAG_MOD_ADD;
+ if (flat_name) flat_flags = LDB_FLAG_MOD_ADD;
+ if (domain_id) id_flags = LDB_FLAG_MOD_ADD;
+ } else if (res->count != 1) {
+ ret = EINVAL;
goto done;
+ } else { /* 1 found */
+ if (realm) {
+ tmp_str = ldb_msg_find_attr_as_string(res->msgs[0],
+ SYSDB_SUBDOMAIN_REALM, NULL);
+ if (!tmp_str || strcasecmp(tmp_str, realm) != 0) {
+ realm_flags = LDB_FLAG_MOD_REPLACE;
+ }
+ }
+ if (flat_name) {
+ tmp_str = ldb_msg_find_attr_as_string(res->msgs[0],
+ SYSDB_SUBDOMAIN_FLAT, NULL);
+ if (!tmp_str || strcasecmp(tmp_str, flat_name) != 0) {
+ flat_flags = LDB_FLAG_MOD_REPLACE;
+ }
+ }
+ if (domain_id) {
+ tmp_str = ldb_msg_find_attr_as_string(res->msgs[0],
+ SYSDB_SUBDOMAIN_ID, NULL);
+ if (!tmp_str || strcasecmp(tmp_str, domain_id) != 0) {
+ id_flags = LDB_FLAG_MOD_REPLACE;
+ }
+ }
}
- ret = ldb_msg_add_empty(msg, SYSDB_OBJECTCLASS, LDB_FLAG_MOD_ADD, NULL);
- if (ret != LDB_SUCCESS) {
- ret = sysdb_error_to_errno(ret);
+ if (!store && realm_flags == 0 && flat_flags == 0 && id_flags == 0) {
+ ret = EOK;
goto done;
}
- ret = ldb_msg_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_SUBDOMAIN_CLASS);
- if (ret != LDB_SUCCESS) {
- ret = sysdb_error_to_errno(ret);
+ msg = ldb_msg_new(tmp_ctx);
+ if (msg == NULL) {
+ ret = ENOMEM;
goto done;
}
+ msg->dn = dn;
+
+ if (store) {
+ ret = ldb_msg_add_empty(msg, SYSDB_OBJECTCLASS, LDB_FLAG_MOD_ADD, NULL);
+ if (ret != LDB_SUCCESS) {
+ ret = sysdb_error_to_errno(ret);
+ goto done;
+ }
+
+
+ ret = ldb_msg_add_string(msg, SYSDB_OBJECTCLASS, SYSDB_SUBDOMAIN_CLASS);
+ if (ret != LDB_SUCCESS) {
+ ret = sysdb_error_to_errno(ret);
+ goto done;
+ }
+ }
- if (domain_info->realm != NULL) {
- ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_REALM, LDB_FLAG_MOD_ADD,
- NULL);
+ if (realm_flags) {
+ ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_REALM, realm_flags, NULL);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
- ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_REALM,
- domain_info->realm);
+ ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_REALM, realm);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
}
- if (domain_info->flat_name != NULL) {
- ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_FLAT, LDB_FLAG_MOD_ADD,
- NULL);
+ if (flat_flags) {
+ ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_FLAT, flat_flags, NULL);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
- ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_FLAT,
- domain_info->flat_name);
+ ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_FLAT, flat_name);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
}
- if (domain_info->id != NULL) {
- ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_ID, LDB_FLAG_MOD_ADD,
- NULL);
+ if (id_flags) {
+ ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_ID, id_flags, NULL);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
}
- ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_ID, domain_info->id);
+ ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_ID, domain_id);
if (ret != LDB_SUCCESS) {
ret = sysdb_error_to_errno(ret);
goto done;
@@ -389,8 +447,7 @@ static errno_t sysdb_add_subdomain_attributes(struct sysdb_ctx *sysdb,
ret = ldb_modify(sysdb->ldb, msg);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_FATAL_FAILURE, ("Failed to add subdomain attributes to "
- "[%s]: [%d][%s]!\n",
- domain_info->name, ret,
+ "[%s]: [%d][%s]!\n", name, ret,
ldb_errstring(sysdb->ldb)));
ret = sysdb_error_to_errno(ret);
goto done;
@@ -462,18 +519,22 @@ errno_t sysdb_update_subdomains(struct sss_domain_info *domain,
}
if (d == cur_subdomains_count) {
+ struct sss_domain_info *ns;
+
DEBUG(SSSDBG_TRACE_FUNC, ("Adding sub-domain [%s].\n",
subdoms[c].name));
- ret = sysdb_domain_create(domain->sysdb, subdoms[c].name);
- if (ret != EOK) {
- DEBUG(SSSDBG_OP_FAILURE, ("sysdb_domain_create failed.\n"));
+
+ ns = new_subdomain(tmp_ctx, domain,
+ subdoms[c].name, subdoms[c].realm,
+ subdoms[c].flat_name, subdoms[c].id);
+ if (!ns) {
+ DEBUG(SSSDBG_OP_FAILURE, ("new_subdomain failed.\n"));
goto done;
}
- ret = sysdb_add_subdomain_attributes(domain->sysdb, &subdoms[c]);
+ ret = sysdb_subdomain_create(ns);
if (ret != EOK) {
- DEBUG(SSSDBG_OP_FAILURE,
- ("sysdb_add_subdomain_attributes failed.\n"));
+ DEBUG(SSSDBG_OP_FAILURE, ("sysdb_subdomain_create failed.\n"));
goto done;
}
}