summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimo Sorce <ssorce@redhat.com>2010-03-02 14:34:45 -0500
committerStephen Gallagher <sgallagh@redhat.com>2010-04-12 09:22:14 -0400
commita137f77b4ddff7f0651ffda710cec1f01618d7a9 (patch)
tree9e07617832adeb87d3e4d5b5a66c1697392ecd2a
parent9def019030f844e429c067c7cca27ff99c921527 (diff)
downloadsssd-a137f77b4ddff7f0651ffda710cec1f01618d7a9.tar.gz
sssd-a137f77b4ddff7f0651ffda710cec1f01618d7a9.tar.xz
sssd-a137f77b4ddff7f0651ffda710cec1f01618d7a9.zip
sysdb: convert sysdb_store_custom
-rw-r--r--src/db/sysdb.h14
-rw-r--r--src/db/sysdb_ops.c168
-rw-r--r--src/providers/ipa/ipa_access.c148
-rw-r--r--src/tests/sysdb-tests.c21
4 files changed, 93 insertions, 258 deletions
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 4955ee2ca..758a3c59b 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -516,14 +516,12 @@ struct tevent_req *sysdb_cache_auth_send(TALLOC_CTX *mem_ctx,
int sysdb_cache_auth_recv(struct tevent_req *req, time_t *expire_date,
time_t *delayed_until);
-struct tevent_req *sysdb_store_custom_send(TALLOC_CTX *mem_ctx,
- struct tevent_context *ev,
- struct sysdb_handle *handle,
- struct sss_domain_info *domain,
- const char *object_name,
- const char *subtree_name,
- struct sysdb_attrs *attrs);
-int sysdb_store_custom_recv(struct tevent_req *req);
+int sysdb_store_custom(TALLOC_CTX *mem_ctx,
+ struct sysdb_ctx *ctx,
+ struct sss_domain_info *domain,
+ const char *object_name,
+ const char *subtree_name,
+ struct sysdb_attrs *attrs);
int sysdb_search_custom(TALLOC_CTX *mem_ctx,
struct sysdb_ctx *sysdb,
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
index 4a980ab24..dcc72a039 100644
--- a/src/db/sysdb_ops.c
+++ b/src/db/sysdb_ops.c
@@ -1637,112 +1637,75 @@ done:
/* =Custom Store (replaces-existing-data)================== */
-struct sysdb_store_custom_state {
- struct tevent_context *ev;
- struct sysdb_handle *handle;
- struct sss_domain_info *domain;
-
- const char *object_name;
- const char *subtree_name;
- struct ldb_dn *dn;
- struct sysdb_attrs *attrs;
- struct ldb_message *msg;
-};
-
-static void sysdb_store_custom_done(struct tevent_req *subreq);
-
-struct tevent_req *sysdb_store_custom_send(TALLOC_CTX *mem_ctx,
- struct tevent_context *ev,
- struct sysdb_handle *handle,
- struct sss_domain_info *domain,
- const char *object_name,
- const char *subtree_name,
- struct sysdb_attrs *attrs)
+int sysdb_store_custom(TALLOC_CTX *mem_ctx,
+ struct sysdb_ctx *ctx,
+ struct sss_domain_info *domain,
+ const char *object_name,
+ const char *subtree_name,
+ struct sysdb_attrs *attrs)
{
- struct tevent_req *req, *subreq;
- struct sysdb_store_custom_state *state;
- int ret;
- const char **search_attrs;
- int i;
+ TALLOC_CTX *tmpctx;
+ const char *search_attrs[] = { "*", NULL };
size_t resp_count = 0;
struct ldb_message **resp;
struct ldb_message *msg;
- struct ldb_request *ldbreq;
struct ldb_message_element *el;
bool add_object = false;
+ int ret;
+ int i;
- if (object_name == NULL || subtree_name == NULL) return NULL;
-
- if (handle == NULL) {
- DEBUG(1, ("Sysdb context not available.\n"));
- return NULL;
- }
-
- req = tevent_req_create(mem_ctx, &state, struct sysdb_store_custom_state);
- if (req == NULL) {
- DEBUG(1, ("tevent_req_create failed.\n"));
- return NULL;
+ if (object_name == NULL || subtree_name == NULL) {
+ return EINVAL;
}
- state->ev = ev;
- state->handle = handle;
- state->domain = domain;
- state->object_name = object_name;
- state->subtree_name = subtree_name;
- state->attrs = attrs;
- state->msg = NULL;
- state->dn = sysdb_custom_dn(handle->ctx, state, domain->name, object_name,
- subtree_name);
- if (state->dn == NULL) {
- DEBUG(1, ("sysdb_custom_dn failed.\n"));
- ret = ENOMEM;
- goto fail;
+ ret = ldb_transaction_start(ctx->ldb);
+ if (ret) {
+ return sysdb_error_to_errno(ret);
}
- search_attrs = talloc_array(state, const char *, 2);
- if (search_attrs == NULL) {
- DEBUG(1, ("talloc_array failed.\n"));
+ tmpctx = talloc_new(mem_ctx);
+ if (!tmpctx) {
ret = ENOMEM;
- goto fail;
+ goto done;
}
- search_attrs[0] = "*";
- search_attrs[1] = NULL;
- ret = sysdb_search_custom_by_name(state, state->handle->ctx,
- state->domain,
- state->object_name,
- state->subtree_name,
- search_attrs,
- &resp_count, &resp);
+ ret = sysdb_search_custom_by_name(tmpctx, ctx,
+ domain, object_name, subtree_name,
+ search_attrs, &resp_count, &resp);
if (ret != EOK && ret != ENOENT) {
- goto fail;
+ goto done;
}
if (ret == ENOENT) {
add_object = true;
}
- msg = ldb_msg_new(state);
+ msg = ldb_msg_new(tmpctx);
if (msg == NULL) {
ret = ENOMEM;
- goto fail;
+ goto done;
}
- msg->dn = state->dn;
+ msg->dn = sysdb_custom_dn(ctx, tmpctx,
+ domain->name, object_name, subtree_name);
+ if (!msg->dn) {
+ DEBUG(1, ("sysdb_custom_dn failed.\n"));
+ ret = ENOMEM;
+ goto done;
+ }
- msg->elements = talloc_array(msg, struct ldb_message_element,
- state->attrs->num);
+ msg->elements = talloc_array(msg, struct ldb_message_element, attrs->num);
if (!msg->elements) {
ret = ENOMEM;
- goto fail;
+ goto done;
}
- for (i = 0; i < state->attrs->num; i++) {
- msg->elements[i] = state->attrs->a[i];
+ for (i = 0; i < attrs->num; i++) {
+ msg->elements[i] = attrs->a[i];
if (add_object) {
msg->elements[i].flags = LDB_FLAG_MOD_ADD;
} else {
- el = ldb_msg_find_element(resp[0], state->attrs->a[i].name);
+ el = ldb_msg_find_element(resp[0], attrs->a[i].name);
if (el == NULL) {
msg->elements[i].flags = LDB_FLAG_MOD_ADD;
} else {
@@ -1750,61 +1713,28 @@ struct tevent_req *sysdb_store_custom_send(TALLOC_CTX *mem_ctx,
}
}
}
- msg->num_elements = state->attrs->num;
+ msg->num_elements = attrs->num;
if (add_object) {
- ret = ldb_build_add_req(&ldbreq, state->handle->ctx->ldb, state, msg,
- NULL, NULL, NULL, NULL);
+ ret = ldb_add(ctx->ldb, msg);
} else {
- ret = ldb_build_mod_req(&ldbreq, state->handle->ctx->ldb, state, msg,
- NULL, NULL, NULL, NULL);
+ ret = ldb_modify(ctx->ldb, msg);
}
if (ret != LDB_SUCCESS) {
- DEBUG(1, ("Failed to build request: %s(%d)[%s]\n",
- ldb_strerror(ret), ret,
- ldb_errstring(state->handle->ctx->ldb)));
+ DEBUG(1, ("Failed to store custmo entry: %s(%d)[%s]\n",
+ ldb_strerror(ret), ret, ldb_errstring(ctx->ldb)));
ret = sysdb_error_to_errno(ret);
- goto fail;
- }
-
- subreq = sldb_request_send(state, state->ev, state->handle->ctx->ldb,
- ldbreq);
- if (!subreq) {
- ret = ENOMEM;
- goto fail;
}
- tevent_req_set_callback(subreq, sysdb_store_custom_done, req);
- return req;
-fail:
- DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret)));
- tevent_req_error(req, ret);
- tevent_req_post(req, ev);
- return req;
-}
-
-static void sysdb_store_custom_done(struct tevent_req *subreq)
-{
- struct tevent_req *req = tevent_req_callback_data(subreq,
- struct tevent_req);
- int ret;
-
- ret = sysdb_op_default_recv(subreq);
- talloc_zfree(subreq);
- if (ret != EOK) {
- tevent_req_error(req, ret);
- return;
+done:
+ if (ret) {
+ DEBUG(6, ("Error: %d (%s)\n", ret, strerror(ret)));
+ ldb_transaction_cancel(ctx->ldb);
+ } else {
+ ret = ldb_transaction_commit(ctx->ldb);
}
-
- tevent_req_done(req);
- return;
-}
-
-int sysdb_store_custom_recv(struct tevent_req *req)
-{
- TEVENT_REQ_RETURN_ON_ERROR(req);
-
- return EOK;
+ talloc_zfree(tmpctx);
+ return ret;
}
/* = Custom Delete======================================= */
diff --git a/src/providers/ipa/ipa_access.c b/src/providers/ipa/ipa_access.c
index cd062095b..697d2479e 100644
--- a/src/providers/ipa/ipa_access.c
+++ b/src/providers/ipa/ipa_access.c
@@ -286,9 +286,7 @@ static void hbac_get_host_info_connect_done(struct tevent_req *subreq);
static void hbac_get_host_memberof(struct tevent_req *req,
struct ldb_message **msgs);
static void hbac_get_host_memberof_done(struct tevent_req *subreq);
-static void hbac_get_host_info_sysdb_transaction_started(struct tevent_req *subreq);
-static void hbac_get_host_info_store_prepare(struct tevent_req *req);
-static void hbac_get_host_info_store_done(struct tevent_req *subreq);
+static void hbac_get_host_info_store_trans(struct tevent_req *subreq);
static struct tevent_req *hbac_get_host_info_send(TALLOC_CTX *memctx,
struct tevent_context *ev,
@@ -633,7 +631,7 @@ static void hbac_get_host_memberof(struct tevent_req *req,
ret = ENOMEM;
goto fail;
}
- tevent_req_set_callback(subreq, hbac_get_host_info_sysdb_transaction_started, req);
+ tevent_req_set_callback(subreq, hbac_get_host_info_store_trans, req);
return;
fail:
@@ -641,14 +639,16 @@ fail:
return;
}
-static void hbac_get_host_info_sysdb_transaction_started(
- struct tevent_req *subreq)
+static void hbac_get_host_info_store_trans(struct tevent_req *subreq)
{
struct tevent_req *req = tevent_req_callback_data(subreq,
struct tevent_req);
- struct hbac_get_host_info_state *state = tevent_req_data(req,
- struct hbac_get_host_info_state);
+ struct hbac_get_host_info_state *state =
+ tevent_req_data(req, struct hbac_get_host_info_state);
+ struct ldb_message_element *el;
+ char *object_name;
int ret;
+ int i;
ret = sysdb_transaction_recv(subreq, state, &state->handle);
talloc_zfree(subreq);
@@ -657,22 +657,9 @@ static void hbac_get_host_info_sysdb_transaction_started(
return;
}
- state->current_item = 0;
- hbac_get_host_info_store_prepare(req);
- return;
-}
-
-static void hbac_get_host_info_store_prepare(struct tevent_req *req)
-{
- struct hbac_get_host_info_state *state = tevent_req_data(req,
- struct hbac_get_host_info_state);
- int ret;
- char *object_name;
- struct ldb_message_element *el;
- struct tevent_req *subreq;
+ for (i = 0; i < state->host_reply_count; i++) {
- if (state->current_item < state->host_reply_count) {
- ret = sysdb_attrs_get_el(state->host_reply_list[state->current_item],
+ ret = sysdb_attrs_get_el(state->host_reply_list[i],
IPA_HOST_FQDN, &el);
if (ret != EOK) {
DEBUG(1, ("sysdb_attrs_get_el failed.\n"));
@@ -691,29 +678,23 @@ static void hbac_get_host_info_store_prepare(struct tevent_req *req)
DEBUG(9, ("Fqdn [%s].\n", object_name));
- ret = sysdb_attrs_replace_name(
- state->host_reply_list[state->current_item],
+ ret = sysdb_attrs_replace_name(state->host_reply_list[i],
IPA_HOST_MEMBEROF, SYSDB_ORIG_MEMBEROF);
if (ret != EOK) {
DEBUG(1, ("sysdb_attrs_replace_name failed.\n"));
goto fail;
}
- subreq = sysdb_store_custom_send(state, state->ev,
- state->handle,
- state->sdap_ctx->be->domain,
- object_name,
- HBAC_HOSTS_SUBDIR,
- state->host_reply_list[state->current_item]);
+ ret = sysdb_store_custom(state, state->sysdb,
+ state->sdap_ctx->be->domain,
+ object_name,
+ HBAC_HOSTS_SUBDIR,
+ state->host_reply_list[i]);
- if (subreq == NULL) {
- DEBUG(1, ("sysdb_store_custom_send failed.\n"));
- ret = ENOMEM;
+ if (ret) {
+ DEBUG(1, ("sysdb_store_custom failed.\n"));
goto fail;
}
-
- tevent_req_set_callback(subreq, hbac_get_host_info_store_done, req);
- return;
}
subreq = sysdb_transaction_commit_send(state, state->ev, state->handle);
@@ -731,25 +712,6 @@ fail:
return;
}
-static void hbac_get_host_info_store_done(struct tevent_req *subreq)
-{
- struct tevent_req *req = tevent_req_callback_data(subreq,
- struct tevent_req);
- struct hbac_get_host_info_state *state = tevent_req_data(req,
- struct hbac_get_host_info_state);
- int ret;
-
- ret = sysdb_store_custom_recv(subreq);
- talloc_zfree(subreq);
- if (ret != EOK) {
- tevent_req_error(req, ret);
- return;
- }
-
- state->current_item++;
- hbac_get_host_info_store_prepare(req);
-}
-
static int hbac_get_host_info_recv(struct tevent_req *req, TALLOC_CTX *memctx,
struct hbac_host_info ***hhi)
{
@@ -786,9 +748,7 @@ static void hbac_get_rules_connect_done(struct tevent_req *subreq);
static void hbac_rule_get(struct tevent_req *req,
struct ldb_message **msgs);
static void hbac_rule_get_done(struct tevent_req *subreq);
-static void hbac_rule_sysdb_transaction_started(struct tevent_req *subreq);
-static void hbac_rule_store_prepare(struct tevent_req *req);
-static void hbac_rule_store_done(struct tevent_req *subreq);
+static void hbac_rule_store_trans(struct tevent_req *subreq);
static struct tevent_req *hbac_get_rules_send(TALLOC_CTX *memctx,
struct tevent_context *ev,
@@ -1051,7 +1011,7 @@ static void hbac_rule_get(struct tevent_req *req,
ret = ENOMEM;
goto fail;
}
- tevent_req_set_callback(subreq, hbac_rule_sysdb_transaction_started, req);
+ tevent_req_set_callback(subreq, hbac_rule_store_trans, req);
return;
fail:
@@ -1059,14 +1019,17 @@ fail:
return;
}
-static void hbac_rule_sysdb_transaction_started(struct tevent_req *subreq)
+static void hbac_rule_store_trans(struct tevent_req *subreq)
{
struct tevent_req *req = tevent_req_callback_data(subreq,
struct tevent_req);
- struct hbac_get_rules_state *state = tevent_req_data(req,
- struct hbac_get_rules_state);
- int ret;
+ struct hbac_get_rules_state *state =
+ tevent_req_data(req, struct hbac_get_rules_state);
struct ldb_dn *hbac_base_dn;
+ struct ldb_message_element *el;
+ char *object_name;
+ int ret;
+ int i;
ret = sysdb_transaction_recv(subreq, state, &state->handle);
talloc_zfree(subreq);
@@ -1088,27 +1051,10 @@ static void hbac_rule_sysdb_transaction_started(struct tevent_req *subreq)
goto fail;
}
- state->current_item = 0;
- hbac_rule_store_prepare(req);
- return;
-
-fail:
- tevent_req_error(req, ret);
- return;
-}
-
-static void hbac_rule_store_prepare(struct tevent_req *req)
-{
- struct hbac_get_rules_state *state = tevent_req_data(req,
- struct hbac_get_rules_state);
- int ret;
- struct ldb_message_element *el;
- struct tevent_req *subreq;
- char *object_name;
- if (state->current_item < state->hbac_reply_count) {
+ for (i = 0; i < state->hbac_reply_count; i++) {
- ret = sysdb_attrs_get_el(state->hbac_reply_list[state->current_item],
+ ret = sysdb_attrs_get_el(state->hbac_reply_list[i],
IPA_UNIQUE_ID, &el);
if (ret != EOK) {
DEBUG(1, ("sysdb_attrs_get_el failed.\n"));
@@ -1126,21 +1072,16 @@ static void hbac_rule_store_prepare(struct tevent_req *req)
}
DEBUG(9, ("IPAUniqueId: [%s].\n", object_name));
- subreq = sysdb_store_custom_send(state, state->ev,
- state->handle,
- state->sdap_ctx->be->domain,
- object_name,
- HBAC_RULES_SUBDIR,
- state->hbac_reply_list[state->current_item]);
+ ret = sysdb_store_custom(state, state->sysdb,
+ state->sdap_ctx->be->domain,
+ object_name,
+ HBAC_RULES_SUBDIR,
+ state->hbac_reply_list[i]);
- if (subreq == NULL) {
+ if (ret) {
DEBUG(1, ("sysdb_store_custom_send failed.\n"));
- ret = ENOMEM;
goto fail;
}
-
- tevent_req_set_callback(subreq, hbac_rule_store_done, req);
- return;
}
subreq = sysdb_transaction_commit_send(state, state->ev, state->handle);
@@ -1158,25 +1099,6 @@ fail:
return;
}
-static void hbac_rule_store_done(struct tevent_req *subreq)
-{
- struct tevent_req *req = tevent_req_callback_data(subreq,
- struct tevent_req);
- struct hbac_get_rules_state *state = tevent_req_data(req,
- struct hbac_get_rules_state);
- int ret;
-
- ret = sysdb_store_custom_recv(subreq);
- talloc_zfree(subreq);
- if (ret != EOK) {
- tevent_req_error(req, ret);
- return;
- }
-
- state->current_item++;
- hbac_rule_store_prepare(req);
-}
-
static int hbac_get_rules_recv(struct tevent_req *req, TALLOC_CTX *memctx,
size_t *hbac_rule_count,
struct sysdb_attrs ***hbac_rule_list)
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index d5ca6e41f..008456c21 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -708,8 +708,6 @@ static void test_remove_group_member(struct tevent_req *req)
test_return(data, ret);
}
-static void test_store_custom_done(struct tevent_req *subreq);
-
static void test_store_custom(struct tevent_req *subreq)
{
struct test_data *data = tevent_req_callback_data(subreq,
@@ -728,22 +726,9 @@ static void test_store_custom(struct tevent_req *subreq)
return test_return(data, ENOMEM);
}
- subreq = sysdb_store_custom_send(data, data->ev, data->handle,
- data->ctx->domain, object_name,
- CUSTOM_TEST_CONTAINER, data->attrs);
- if (!subreq) {
- return test_return(data, ENOMEM);
- }
- tevent_req_set_callback(subreq, test_store_custom_done, data);
-}
-
-static void test_store_custom_done(struct tevent_req *subreq)
-{
- struct test_data *data = tevent_req_callback_data(subreq, struct test_data);
- int ret;
-
- ret = sysdb_store_custom_recv(subreq);
- talloc_zfree(subreq);
+ ret = sysdb_store_custom(data, data->handle->ctx,
+ data->ctx->domain, object_name,
+ CUSTOM_TEST_CONTAINER, data->attrs);
return test_return(data, ret);
}