[SSSD] [PATCH] Save all data to sysdb in one transaction

Sumit Bose sbose at redhat.com
Thu Sep 9 12:29:25 UTC 2010


On Wed, Sep 08, 2010 at 09:52:31AM -0400, Stephen Gallagher wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On 09/08/2010 09:50 AM, Sumit Bose wrote:
> > On Wed, Sep 08, 2010 at 09:33:04AM -0400, Stephen Gallagher wrote:
> > On 07/08/2010 12:57 PM, Stephen Gallagher wrote:
> >>>> Just a reminder that it's been about a month since there was any
> >>>> activity on this patch.
> >>>>
> > 
> > *bump* Any word on this one, Sumit? Would you prefer that I have someone
> > else look into this?
> > 
> >> ah, sorry, I have a patch which addresses the comments in my tree, but I
> >> need to rebase it. I will send it tomorrow or Friday.
> > 
> 
> Okay, no problem. If there are any further review comments at that
> point, I'm going to turn it over to Jakub to finish up.

rebased versions are attached. It was necessary to add some
modifications during the rebase. I have done a couple of online and
offline test but it would be nice if someone else can run additional
tests.

bye,
Sumit

> 
> - -- 
> Stephen Gallagher
> RHCE 804006346421761
> 
> Delivering value year after year.
> Red Hat ranks #1 in value among software vendors.
> http://www.redhat.com/promo/vendor/
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
> Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/
> 
> iEYEARECAAYFAkyHlR8ACgkQeiVVYja6o6MKkgCeNCEYKVfiOvZLl09I+zZJ7ZQ2
> YAkAn2hu0LXBr3I7AIjJN9nOFWP8CTXT
> =yHsK
> -----END PGP SIGNATURE-----
-------------- next part --------------
From 60db9e05332137d13ca437029419630e8c52b99f Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose at redhat.com>
Date: Wed, 2 Jun 2010 11:45:24 +0200
Subject: [PATCH 1/2] Handle host objects like other objects

---
 src/providers/ipa/ipa_access.c |  309 +++++++++++++++++++++++-----------------
 src/providers/ipa/ipa_access.h |    3 +-
 2 files changed, 183 insertions(+), 129 deletions(-)

diff --git a/src/providers/ipa/ipa_access.c b/src/providers/ipa/ipa_access.c
index 3fb8191..d1ba6c7 100644
--- a/src/providers/ipa/ipa_access.c
+++ b/src/providers/ipa/ipa_access.c
@@ -164,6 +164,144 @@ static errno_t hbac_sysdb_data_recv(TALLOC_CTX *mem_ctx,
     return EOK;
 }
 
+static errno_t set_local_and_remote_host_info(TALLOC_CTX *mem_ctx,
+                                              size_t host_count,
+                                              struct sysdb_attrs **host_list,
+                                              const char *local_hostname,
+                                              const char *remote_hostname,
+                                              struct hbac_host_info **local_hhi,
+                                              struct hbac_host_info **remote_hhi)
+
+{
+    size_t c;
+    int ret;
+    struct hbac_host_info *hhi;
+    struct ldb_message_element *el;
+    TALLOC_CTX *tmp_ctx;
+
+    if (local_hostname == NULL || *local_hostname == '\0') {
+        DEBUG(1, ("Missing local hostname.\n"));
+        ret = EINVAL;
+        goto done;
+    }
+
+    if (host_count == 0) {
+        DEBUG(1, ("No host data available.\n"));
+        ret = EINVAL;
+        goto done;
+    }
+
+    tmp_ctx = talloc_new(mem_ctx);
+    if (tmp_ctx == NULL) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    for (c = 0; c < host_count; c++) {
+        hhi = talloc_zero(tmp_ctx, struct hbac_host_info);
+        if (hhi == NULL) {
+            DEBUG(1, ("talloc_zero failed.\n"));
+            ret = ENOMEM;
+            goto done;
+        }
+
+        ret = sysdb_attrs_get_el(host_list[c], SYSDB_ORIG_DN, &el);
+        if (ret != EOK) {
+            DEBUG(1, ("sysdb_attrs_get_el failed.\n"));
+            goto done;
+        }
+        if (el->num_values == 0) {
+            DEBUG(1, ("Missing OriginalDN.\n"));
+            ret = EINVAL;
+            goto done;
+        }
+        DEBUG(9, ("OriginalDN: [%.*s].\n", el->values[0].length,
+                                           (char *)el->values[0].data));
+        hhi->dn = talloc_strndup(hhi, (char *)el->values[0].data,
+                                 el->values[0].length);
+        if (hhi->dn == NULL) {
+            DEBUG(1, ("talloc_strndup failed.\n"));
+            ret = ENOMEM;
+            goto done;
+        }
+
+        ret = sysdb_attrs_get_el(host_list[c], IPA_HOST_SERVERHOSTNAME, &el);
+        if (ret != EOK) {
+            DEBUG(1, ("sysdb_attrs_get_el failed.\n"));
+            goto done;
+        }
+        if (el->num_values == 0) {
+            DEBUG(1, ("Missing ServerHostName.\n"));
+            ret = EINVAL;
+            goto done;
+        }
+        DEBUG(9, ("ServerHostName: [%.*s].\n", el->values[0].length,
+                                               (char *)el->values[0].data));
+        hhi->serverhostname = talloc_strndup(hhi, (char *)el->values[0].data,
+                                             el->values[0].length);
+        if (hhi->serverhostname == NULL) {
+            DEBUG(1, ("talloc_strndup failed.\n"));
+            ret = ENOMEM;
+            goto done;
+        }
+
+        ret = sysdb_attrs_get_el(host_list[c], IPA_HOST_FQDN, &el);
+        if (ret != EOK) {
+            DEBUG(1, ("sysdb_attrs_get_el failed.\n"));
+            goto done;
+        }
+        if (el->num_values == 0) {
+            DEBUG(1, ("Missing FQDN.\n"));
+            ret = EINVAL;
+            goto done;
+        }
+        DEBUG(9, ("FQDN: [%.*s].\n", el->values[0].length,
+                                     (char *)el->values[0].data));
+        hhi->fqdn = talloc_strndup(hhi, (char *)el->values[0].data,
+                                   el->values[0].length);
+        if (hhi->fqdn == NULL) {
+            DEBUG(1, ("talloc_strndup failed.\n"));
+            ret = ENOMEM;
+            goto done;
+        }
+
+        ret = sysdb_attrs_get_string_array(host_list[c], SYSDB_ORIG_MEMBEROF,
+                                           hhi, &hhi->memberof);
+        if (ret != EOK) {
+            if (ret != ENOENT) {
+                DEBUG(1, ("sysdb_attrs_get_string_array failed.\n"));
+                goto done;
+            }
+
+            hhi->memberof = talloc_array(hhi, const char *, 1);
+            if (hhi->memberof == NULL) {
+                DEBUG(1, ("talloc_array failed.\n"));
+                ret = ENOMEM;
+                goto done;
+            }
+            hhi->memberof[0] = NULL;
+        }
+
+        if (strcmp(hhi->fqdn, local_hostname) == 0 ||
+            strcmp(hhi->serverhostname, local_hostname) == 0) {
+            *local_hhi = talloc_steal(mem_ctx, hhi);
+        }
+
+        if (remote_hostname != NULL && *remote_hostname != '\0') {
+            if (strcmp(hhi->fqdn, remote_hostname) == 0 ||
+                strcmp(hhi->serverhostname, remote_hostname) == 0) {
+                *remote_hhi = talloc_steal(mem_ctx, hhi);
+            }
+        }
+    }
+
+    ret = EOK;
+
+done:
+    talloc_free(tmp_ctx);
+    return ret;
+}
+
 static void ipa_access_reply(struct hbac_ctx *hbac_ctx, int pam_status)
 {
     struct be_req *be_req = hbac_ctx->be_req;
@@ -607,7 +745,7 @@ static struct tevent_req *hbac_get_host_info_send(TALLOC_CTX *memctx,
         goto fail;
     }
 
-    state->host_attrs = talloc_array(state, const char *, 7);
+    state->host_attrs = talloc_array(state, const char *, 8);
     if (state->host_attrs == NULL) {
         DEBUG(1, ("Failed to allocate host attribute list.\n"));
         ret = ENOMEM;
@@ -619,7 +757,8 @@ static struct tevent_req *hbac_get_host_info_send(TALLOC_CTX *memctx,
     state->host_attrs[3] = "objectClass";
     state->host_attrs[4] = SYSDB_ORIG_DN;
     state->host_attrs[5] = SYSDB_ORIG_MEMBEROF;
-    state->host_attrs[6] = NULL;
+    state->host_attrs[6] = IPA_UNIQUE_ID;
+    state->host_attrs[7] = NULL;
 
     if (hbac_ctx_is_offline(state->hbac_ctx)) {
         ret = hbac_sysdb_data_recv(state, hbac_ctx_sysdb(state->hbac_ctx),
@@ -628,7 +767,7 @@ static struct tevent_req *hbac_get_host_info_send(TALLOC_CTX *memctx,
                                    state->host_attrs,
                                    &state->host_reply_count,
                                    &state->host_reply_list);
-        if (ret) {
+        if (ret != EOK) {
             DEBUG(1, ("hbac_sysdb_data_recv failed.\n"));
             goto fail;
         }
@@ -687,15 +826,27 @@ static void hbac_get_host_memberof_done(struct tevent_req *subreq)
     hbac_get_host_memberof(req, false);
 }
 
-static bool hbac_is_known_host(struct hbac_host_info **hhi, const char *fqdn)
+static bool hbac_is_known_host(size_t host_reply_count,
+                               struct sysdb_attrs **host_reply_list,
+                               const char *fqdn)
 {
-    if (!hhi || !fqdn) {
+    int i;
+    const char *new_fqdn;
+    int ret;
+
+    if (!host_reply_list || !fqdn) {
         return false;
     }
 
-    int i;
-    for (i = 0; hhi[i] != NULL; i++) {
-        if(strcmp(hhi[i]->fqdn, fqdn) == 0) {
+    for (i = 0; i < host_reply_count; i++) {
+        ret = sysdb_attrs_get_string(host_reply_list[i], IPA_HOST_FQDN,
+                                     &new_fqdn);
+        if (ret != 0) {
+            DEBUG(1, ("missing FQDN in new HBAC host record\n"));
+            continue;
+        }
+
+        if(strcmp(new_fqdn, fqdn) == 0) {
             return true;
         }
     }
@@ -713,7 +864,6 @@ static void hbac_get_host_memberof(struct tevent_req *req, bool offline)
     int ret;
     int i;
     struct ldb_message_element *el;
-    struct hbac_host_info **hhi;
     char *object_name;
     const char *fqdn_attrs[] = { IPA_HOST_FQDN, NULL };
     const char *fqdn;
@@ -721,104 +871,6 @@ static void hbac_get_host_memberof(struct tevent_req *req, bool offline)
     size_t cached_count;
     struct ldb_message **cached_entries = 0;
 
-    hhi = talloc_array(state, struct hbac_host_info *, state->host_reply_count + 1);
-    if (hhi == NULL) {
-        DEBUG(1, ("talloc_array failed.\n"));
-        ret = ENOMEM;
-        goto fail;
-    }
-    memset(hhi, 0,
-           sizeof(struct hbac_host_info *) * (state->host_reply_count + 1));
-
-    if (state->host_reply_count == 0) {
-        DEBUG(1, ("No hosts not found in IPA server.\n"));
-    }
-
-    for (i = 0; i < state->host_reply_count; i++) {
-        hhi[i] = talloc_zero(hhi, struct hbac_host_info);
-        if (hhi[i] == NULL) {
-            ret = ENOMEM;
-            goto fail;
-        }
-
-        ret = sysdb_attrs_get_el(state->host_reply_list[i], SYSDB_ORIG_DN, &el);
-        if (ret != EOK) {
-            DEBUG(1, ("sysdb_attrs_get_el failed.\n"));
-            goto fail;
-        }
-        if (el->num_values == 0) {
-            ret = EINVAL;
-            goto fail;
-        }
-        DEBUG(9, ("OriginalDN: [%.*s].\n", el->values[0].length,
-                                           (char *)el->values[0].data));
-        hhi[i]->dn = talloc_strndup(hhi, (char *)el->values[0].data,
-                                   el->values[0].length);
-        if (hhi[i]->dn == NULL) {
-            ret = ENOMEM;
-            goto fail;
-        }
-
-        ret = sysdb_attrs_get_el(state->host_reply_list[i],
-                                 IPA_HOST_SERVERHOSTNAME, &el);
-        if (ret != EOK) {
-            DEBUG(1, ("sysdb_attrs_get_el failed.\n"));
-            goto fail;
-        }
-        if (el->num_values == 0) {
-            ret = EINVAL;
-            goto fail;
-        }
-        DEBUG(9, ("ServerHostName: [%.*s].\n", el->values[0].length,
-                                               (char *)el->values[0].data));
-        hhi[i]->serverhostname = talloc_strndup(hhi, (char *)el->values[0].data,
-                                               el->values[0].length);
-        if (hhi[i]->serverhostname == NULL) {
-            ret = ENOMEM;
-            goto fail;
-        }
-
-        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"));
-            goto fail;
-        }
-        if (el->num_values == 0) {
-            ret = EINVAL;
-            goto fail;
-        }
-        DEBUG(9, ("FQDN: [%.*s].\n", el->values[0].length,
-                                     (char *)el->values[0].data));
-        hhi[i]->fqdn = talloc_strndup(hhi, (char *)el->values[0].data,
-                                               el->values[0].length);
-        if (hhi[i]->fqdn == NULL) {
-            ret = ENOMEM;
-            goto fail;
-        }
-
-        ret = sysdb_attrs_get_string_array(state->host_reply_list[i],
-                                          offline ? SYSDB_ORIG_MEMBEROF :
-                                                    IPA_MEMBEROF,
-                                          hhi, &(hhi[i]->memberof));
-        if (ret != EOK) {
-            if (ret != ENOENT) {
-                DEBUG(1, ("sysdb_attrs_get_string_array failed.\n"));
-                goto fail;
-            }
-
-            hhi[i]->memberof = talloc_array(hhi, const char *, 1);
-            if (hhi[i]->memberof == NULL) {
-                DEBUG(1, ("talloc_array failed.\n"));
-                ret = ENOMEM;
-                goto fail;
-            }
-            hhi[i]->memberof[0] = NULL;
-        }
-    }
-
-    state->hbac_host_info = hhi;
-
     if (offline) {
         tevent_req_done(req);
         return;
@@ -854,7 +906,8 @@ static void hbac_get_host_memberof(struct tevent_req *req, bool offline)
         fqdn = ldb_msg_find_attr_as_string(cached_entries[i], IPA_HOST_FQDN, NULL);
         if (!fqdn) {
             DEBUG(1, ("missing FQDN in cached HBAC host record\n"));
-        } else if (hbac_is_known_host(hhi, fqdn)) {
+        } else if (hbac_is_known_host(state->host_reply_count,
+                                      state->host_reply_list, fqdn)) {
             continue;
         } else {
             DEBUG(9, ("deleting obsolete HBAC host record for %s\n", fqdn));
@@ -922,14 +975,21 @@ fail:
 }
 
 static int hbac_get_host_info_recv(struct tevent_req *req, TALLOC_CTX *memctx,
-                                   struct hbac_host_info ***hhi)
+                                   size_t *hbac_hosts_count,
+                                   struct sysdb_attrs ***hbac_hosts_list)
 {
+    size_t c;
     struct hbac_get_host_info_state *state = tevent_req_data(req,
                                                struct hbac_get_host_info_state);
 
     TEVENT_REQ_RETURN_ON_ERROR(req);
 
-    *hhi = talloc_steal(memctx, state->hbac_host_info);
+    *hbac_hosts_count = state->host_reply_count;
+    *hbac_hosts_list = talloc_steal(memctx, state->host_reply_list);
+    for (c = 0; c < state->host_reply_count; c++) {
+        talloc_steal(memctx, state->host_reply_list[c]);
+    }
+
     return EOK;
 }
 
@@ -1910,10 +1970,9 @@ static void hbac_get_host_info_done(struct tevent_req *req)
     int pam_status = PAM_SYSTEM_ERR;
     const char *ipa_hostname;
     struct hbac_host_info *local_hhi = NULL;
-    int i;
 
-    talloc_zfree(hbac_ctx->hbac_host_info);
-    ret = hbac_get_host_info_recv(req, hbac_ctx, &hbac_ctx->hbac_host_info);
+    ret = hbac_get_host_info_recv(req, hbac_ctx, &hbac_ctx->hbac_hosts_count,
+                                  &hbac_ctx->hbac_hosts_list);
     talloc_zfree(req);
 
     if (!hbac_check_step_result(hbac_ctx, ret)) {
@@ -1926,21 +1985,15 @@ static void hbac_get_host_info_done(struct tevent_req *req)
         goto fail;
     }
 
-    for (i = 0; hbac_ctx->hbac_host_info[i] != NULL; i++) {
-        if (strcmp(hbac_ctx->hbac_host_info[i]->fqdn, ipa_hostname) == 0 ||
-            strcmp(hbac_ctx->hbac_host_info[i]->serverhostname,
-                   ipa_hostname) == 0) {
-            local_hhi = hbac_ctx->hbac_host_info[i];
-        }
-        if (hbac_ctx->pd->rhost != NULL && *hbac_ctx->pd->rhost != '\0') {
-            if (strcmp(hbac_ctx->hbac_host_info[i]->fqdn,
-                       hbac_ctx->pd->rhost) == 0 ||
-                strcmp(hbac_ctx->hbac_host_info[i]->serverhostname,
-                       hbac_ctx->pd->rhost) == 0) {
-                hbac_ctx->remote_hhi = hbac_ctx->hbac_host_info[i];
-            }
-        }
-    }
+    ret = set_local_and_remote_host_info(hbac_ctx, hbac_ctx->hbac_hosts_count,
+                                         hbac_ctx->hbac_hosts_list, ipa_hostname,
+                                         hbac_ctx->pd->rhost, &local_hhi,
+                                         &hbac_ctx->remote_hhi);
+    if (ret != EOK) {
+        DEBUG(1, ("set_local_and_remote_host_info failed.\n"));
+        goto fail;
+     }
+
     if (local_hhi == NULL) {
         DEBUG(1, ("Missing host info for [%s].\n", ipa_hostname));
         pam_status = PAM_PERM_DENIED;
diff --git a/src/providers/ipa/ipa_access.h b/src/providers/ipa/ipa_access.h
index b43c712..ba56700 100644
--- a/src/providers/ipa/ipa_access.h
+++ b/src/providers/ipa/ipa_access.h
@@ -52,7 +52,8 @@ struct hbac_ctx {
     struct time_rules_ctx *tr_ctx;
     struct be_req *be_req;
     struct pam_data *pd;
-    struct hbac_host_info **hbac_host_info;
+    struct sysdb_attrs **hbac_hosts_list;
+    size_t hbac_hosts_count;
     struct hbac_host_info *remote_hhi;
     struct sysdb_attrs **hbac_rule_list;
     size_t hbac_rule_count;
-- 
1.7.2.2

-------------- next part --------------
From a4eb026f30f75b0b2ade17ffef12a32a7a87d587 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose at redhat.com>
Date: Wed, 2 Jun 2010 11:36:11 +0200
Subject: [PATCH 2/2] Save all data to sysdb in one transaction

---
 src/providers/ipa/ipa_access.c |  353 +++++++++++++++-------------------------
 1 files changed, 131 insertions(+), 222 deletions(-)

diff --git a/src/providers/ipa/ipa_access.c b/src/providers/ipa/ipa_access.c
index d1ba6c7..223bf16 100644
--- a/src/providers/ipa/ipa_access.c
+++ b/src/providers/ipa/ipa_access.c
@@ -319,6 +319,127 @@ static void ipa_access_reply(struct hbac_ctx *hbac_ctx, int pam_status)
     }
 }
 
+static errno_t hbac_save_list(struct sysdb_ctx *sysdb, bool delete_subdir,
+                              const char *subdir, struct sss_domain_info *domain,
+                              const char *naming_attribute, size_t count,
+                              struct sysdb_attrs **list)
+{
+    int ret;
+    size_t c;
+    struct ldb_dn *base_dn;
+    const char *object_name;
+    struct ldb_message_element *el;
+    TALLOC_CTX *tmp_ctx;
+
+    tmp_ctx = talloc_new(NULL);
+    if (tmp_ctx == NULL) {
+        DEBUG(1, ("talloc_new failed.\n"));
+        return ENOMEM;
+    }
+
+    if (delete_subdir) {
+        base_dn = sysdb_custom_subtree_dn(sysdb, tmp_ctx, domain->name, subdir);
+        if (base_dn == NULL) {
+            ret = ENOMEM;
+            goto done;
+        }
+
+        ret = sysdb_delete_recursive(tmp_ctx, sysdb, base_dn, true);
+        if (ret != EOK) {
+            DEBUG(1, ("sysdb_delete_recursive failed.\n"));
+            goto done;
+        }
+    }
+
+    for (c = 0; c < count; c++) {
+        ret = sysdb_attrs_get_el(list[c], naming_attribute, &el);
+        if (ret != EOK) {
+            DEBUG(1, ("sysdb_attrs_get_el failed.\n"));
+            goto done;
+        }
+        if (el->num_values == 0) {
+            DEBUG(1, ("IPA_UNIQUE_ID not found.\n"));
+            ret = EINVAL;
+            goto done;
+        }
+        object_name = talloc_strndup(tmp_ctx, (const char *)el->values[0].data,
+                                     el->values[0].length);
+        if (object_name == NULL) {
+            DEBUG(1, ("talloc_strndup failed.\n"));
+            ret = ENOMEM;
+            goto done;
+        }
+        DEBUG(9, ("Object name: [%s].\n", object_name));
+
+        ret = sysdb_store_custom(tmp_ctx, sysdb, domain, object_name, subdir,
+                                 list[c]);
+        if (ret != EOK) {
+            DEBUG(1, ("sysdb_store_custom failed.\n"));
+            goto done;
+        }
+    }
+
+    ret = EOK;
+
+done:
+    talloc_free(tmp_ctx);
+    return ret;
+}
+
+static errno_t hbac_save_data_to_sysdb(struct hbac_ctx *hbac_ctx)
+{
+    int ret;
+    bool in_transaction = false;
+    struct sysdb_ctx *sysdb = hbac_ctx_sysdb(hbac_ctx);
+    struct sss_domain_info *domain = hbac_ctx_be(hbac_ctx)->domain;
+
+    ret = sysdb_transaction_start(sysdb);
+    if (ret != EOK) {
+        DEBUG(1, ("sysdb_transaction_start failed.\n"));
+        return ret;
+    }
+    in_transaction = true;
+
+    ret = hbac_save_list(sysdb, true, HBAC_SERVICES_SUBDIR, domain,
+                         IPA_UNIQUE_ID, hbac_ctx->hbac_services_count,
+                         hbac_ctx->hbac_services_list);
+    if (ret != EOK) {
+        DEBUG(1, ("hbac_save_list failed.\n"));
+        goto done;
+    }
+
+    ret = hbac_save_list(sysdb, true, HBAC_RULES_SUBDIR, domain,
+                         IPA_UNIQUE_ID, hbac_ctx->hbac_rule_count,
+                         hbac_ctx->hbac_rule_list);
+    if (ret != EOK) {
+        DEBUG(1, ("hbac_save_list failed.\n"));
+        goto done;
+    }
+
+    ret = hbac_save_list(sysdb, false, HBAC_HOSTS_SUBDIR, domain,
+                         IPA_HOST_FQDN, hbac_ctx->hbac_hosts_count,
+                         hbac_ctx->hbac_hosts_list);
+    if (ret != EOK) {
+        DEBUG(1, ("hbac_save_list failed.\n"));
+        goto done;
+    }
+
+    ret = sysdb_transaction_commit(sysdb);
+    if (ret != EOK) {
+        DEBUG(1, ("sysdb_transaction_commit failed.\n"));
+        goto done;
+    }
+    in_transaction = false;
+
+    ret = EOK;
+
+done:
+    if (in_transaction) {
+        sysdb_transaction_cancel(sysdb);
+    }
+    return ret;
+}
+
 struct hbac_get_service_data_state {
     struct hbac_ctx *hbac_ctx;
     const char *basedn;
@@ -446,15 +567,7 @@ static void hbac_services_get_done(struct tevent_req *subreq)
                                                       struct tevent_req);
     struct hbac_get_service_data_state *state = tevent_req_data(req,
                                             struct hbac_get_service_data_state);
-    struct sysdb_ctx *sysdb;
-    struct sss_domain_info *domain;
     int ret;
-    bool in_transaction = false;
-    struct ldb_dn *base_dn;
-    int i;
-    struct ldb_message_element *el;
-    char *object_name;
-
 
     ret = hbac_sdap_data_recv(subreq, state, &state->services_reply_count,
                               &state->services_reply_list);
@@ -464,81 +577,8 @@ static void hbac_services_get_done(struct tevent_req *subreq)
         return;
     }
 
-    if (state->offline) {
-        tevent_req_done(req);
-        return;
-    }
-
-    sysdb = hbac_ctx_sysdb(state->hbac_ctx);
-    domain = hbac_ctx_be(state->hbac_ctx)->domain;
-
-    ret = sysdb_transaction_start(sysdb);
-    if (ret != EOK) {
-        DEBUG(1, ("sysdb_transaction_start failed.\n"));
-        tevent_req_error(req, ret);
-        return;
-    }
-    in_transaction = true;
-
-    base_dn = sysdb_custom_subtree_dn(sysdb, state,
-                                      domain->name,
-                                      HBAC_SERVICES_SUBDIR);
-    if (base_dn == NULL) {
-        ret = ENOMEM;
-        goto fail;
-    }
-
-    ret = sysdb_delete_recursive(state, sysdb, base_dn, true);
-    if (ret) {
-        DEBUG(1, ("sysdb_delete_recursive failed.\n"));
-        goto fail;
-    }
-
-    for (i = 0; i < state->services_reply_count; i++) {
-        ret = sysdb_attrs_get_el(state->services_reply_list[i], IPA_UNIQUE_ID,
-                                 &el);
-        if (ret != EOK) {
-            DEBUG(1, ("sysdb_attrs_get_el failed.\n"));
-            goto fail;
-        }
-        if (el->num_values == 0) {
-            ret = EINVAL;
-            goto fail;
-        }
-        object_name = talloc_strndup(state, (const char *)el->values[0].data,
-                                     el->values[0].length);
-        if (object_name == NULL) {
-            ret = ENOMEM;
-            goto fail;
-        }
-        DEBUG(9, ("Object name: [%s].\n", object_name));
-
-        ret = sysdb_store_custom(state, sysdb,
-                                 domain, object_name,
-                                 HBAC_SERVICES_SUBDIR,
-                                 state->services_reply_list[i]);
-        if (ret) {
-            DEBUG(1, ("sysdb_store_custom failed.\n"));
-            goto fail;
-        }
-    }
-
-    ret = sysdb_transaction_commit(sysdb);
-    if (ret) {
-        DEBUG(1, ("sysdb_transaction_commit failed.\n"));
-        goto fail;
-    }
-    in_transaction = false;
-
     tevent_req_done(req);
     return;
-
-fail:
-    if (in_transaction) {
-        sysdb_transaction_cancel(sysdb);
-    }
-    tevent_req_error(req, ret);
-    return;
 }
 
 static int hbac_get_service_data_recv(struct tevent_req *req,
@@ -863,8 +903,6 @@ static void hbac_get_host_memberof(struct tevent_req *req, bool offline)
     bool in_transaction = false;
     int ret;
     int i;
-    struct ldb_message_element *el;
-    char *object_name;
     const char *fqdn_attrs[] = { IPA_HOST_FQDN, NULL };
     const char *fqdn;
 
@@ -922,38 +960,6 @@ static void hbac_get_host_memberof(struct tevent_req *req, bool offline)
 
     talloc_zfree(cached_entries);
 
-    for (i = 0; i < state->host_reply_count; i++) {
-
-        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"));
-            goto fail;
-        }
-        if (el->num_values == 0) {
-            ret = EINVAL;
-            goto fail;
-        }
-        object_name = talloc_strndup(state, (const char *)el->values[0].data,
-                                     el->values[0].length);
-        if (object_name == NULL) {
-            ret = ENOMEM;
-            goto fail;
-        }
-        DEBUG(9, ("Fqdn [%s].\n", object_name));
-
-        ret = sysdb_store_custom(state, sysdb,
-                                 domain,
-                                 object_name,
-                                 HBAC_HOSTS_SUBDIR,
-                                 state->host_reply_list[i]);
-
-        if (ret) {
-            DEBUG(1, ("sysdb_store_custom failed.\n"));
-            goto fail;
-        }
-    }
-
     ret = sysdb_transaction_commit(sysdb);
     if (ret) {
         DEBUG(1, ("sysdb_transaction_commit failed.\n"));
@@ -1009,7 +1015,6 @@ struct hbac_get_rules_state {
     int current_item;
 };
 
-static void hbac_rule_get(struct tevent_req *req, bool offline);
 static void hbac_rule_get_done(struct tevent_req *subreq);
 
 static struct tevent_req *hbac_get_rules_send(TALLOC_CTX *memctx,
@@ -1116,7 +1121,7 @@ static struct tevent_req *hbac_get_rules_send(TALLOC_CTX *memctx,
             DEBUG(1, ("hbac_sysdb_data_recv failed.\n"));
             goto fail;
         }
-        hbac_rule_get(req, true);
+        tevent_req_done(req);
         tevent_req_post(req, hbac_ctx_ev(state->hbac_ctx));
         return req;
     }
@@ -1168,113 +1173,8 @@ static void hbac_rule_get_done(struct tevent_req *subreq)
         return;
     }
 
-    hbac_rule_get(req, false);
-}
-
-static void hbac_rule_get(struct tevent_req *req, bool offline)
-{
-    struct hbac_get_rules_state *state =
-                        tevent_req_data(req, struct hbac_get_rules_state);
-    bool in_transaction = false;
-    struct sysdb_ctx *sysdb;
-    struct sss_domain_info *domain;
-    int ret;
-    int i;
-    struct ldb_message_element *el;
-    struct ldb_dn *hbac_base_dn;
-    char *object_name;
-
-    for (i = 0; i < state->hbac_reply_count; i++) {
-        ret = sysdb_attrs_get_el(state->hbac_reply_list[i], SYSDB_ORIG_DN, &el);
-        if (ret != EOK) {
-            DEBUG(1, ("sysdb_attrs_get_el failed.\n"));
-            goto fail;
-        }
-        if (el->num_values == 0) {
-            DEBUG(1, ("Missing original DN.\n"));
-            ret = EINVAL;
-            goto fail;
-        }
-        DEBUG(9, ("OriginalDN: [%s].\n", (const char *)el->values[0].data));
-    }
-
-    if (offline) {
-        tevent_req_done(req);
-        return;
-    }
-
-    sysdb = hbac_ctx_sysdb(state->hbac_ctx);
-    domain = hbac_ctx_be(state->hbac_ctx)->domain;
-
-    ret = sysdb_transaction_start(sysdb);
-    if (ret != EOK) {
-        tevent_req_error(req, ret);
-        return;
-    }
-    in_transaction = true;
-
-    hbac_base_dn = sysdb_custom_subtree_dn(sysdb, state,
-                                           domain->name,
-                                           HBAC_RULES_SUBDIR);
-    if (hbac_base_dn == NULL) {
-        ret = ENOMEM;
-        goto fail;
-    }
-    ret = sysdb_delete_recursive(state, sysdb, hbac_base_dn, true);
-    if (ret) {
-        DEBUG(1, ("sysdb_delete_recursive_send failed.\n"));
-        goto fail;
-    }
-
-
-    for (i = 0; i < state->hbac_reply_count; i++) {
-
-        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"));
-            goto fail;
-        }
-        if (el->num_values == 0) {
-            ret = EINVAL;
-            goto fail;
-        }
-        object_name = talloc_strndup(state, (const char *)el->values[0].data,
-                                     el->values[0].length);
-        if (object_name == NULL) {
-            ret = ENOMEM;
-            goto fail;
-        }
-        DEBUG(9, ("IPAUniqueId: [%s].\n", object_name));
-
-        ret = sysdb_store_custom(state, sysdb,
-                                 domain,
-                                 object_name,
-                                 HBAC_RULES_SUBDIR,
-                                 state->hbac_reply_list[i]);
-
-        if (ret) {
-            DEBUG(1, ("sysdb_store_custom_send failed.\n"));
-            goto fail;
-        }
-    }
-
-    ret = sysdb_transaction_commit(sysdb);
-    if (ret) {
-        DEBUG(1, ("sysdb_transaction_commit failed.\n"));
-        goto fail;
-    }
-    in_transaction = false;
-
     tevent_req_done(req);
     return;
-
-fail:
-    if (in_transaction) {
-        sysdb_transaction_cancel(sysdb);
-    }
-    tevent_req_error(req, ret);
-    return;
 }
 
 static int hbac_get_rules_recv(struct tevent_req *req, TALLOC_CTX *memctx,
@@ -2070,6 +1970,15 @@ static void hbac_get_service_data_done(struct tevent_req *req)
         hbac_ctx->user_dn = 0;
     }
 
+    if (!hbac_ctx_is_offline(hbac_ctx)) {
+        ret = hbac_save_data_to_sysdb(hbac_ctx);
+        if (ret != EOK) {
+            DEBUG(1, ("Failed to save data, "
+                      "offline authentication might not work.\n"));
+            /* This is not a fatal error. */
+        }
+    }
+
     hbac_ctx->groups_count = 0;
     talloc_zfree(hbac_ctx->groups);
 
-- 
1.7.2.2



More information about the sssd-devel mailing list