>From f93af6cb1bec949d42ef6ff21aff04273c53d5d4 Mon Sep 17 00:00:00 2001
From: Simo Sorce <simo@redhat.com>
Date: Mon, 14 Jan 2013 12:22:22 -0500
Subject: [PATCH 03/12] Update main domain info in place

---
 src/db/sysdb.h                               |  5 +--
 src/db/sysdb_subdomains.c                    | 61 +++++++++++++---------------
 src/providers/ipa/ipa_subdomains.c           |  9 ++--
 src/responder/common/responder_get_domains.c | 25 ++----------
 4 files changed, 38 insertions(+), 62 deletions(-)

diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index a5b037c6efddc975511411dcb4ab47b33b031962..0e4e67e3af8f820a1b34a71d5f5eb021ce994b87 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -373,10 +373,7 @@ errno_t sysdb_update_subdomains(struct sss_domain_info *domain,
                                 int num_subdoms,
                                 struct sysdb_subdom *subdoms);
 
-errno_t sysdb_master_domain_get_info(TALLOC_CTX *mem_ctx,
-                                     struct sysdb_ctx *sysdb,
-                                     struct sss_domain_info *domain,
-                                     struct sysdb_subdom **info);
+errno_t sysdb_master_domain_update(struct sss_domain_info *domain);
 
 errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
                                      struct sss_domain_info *domain,
diff --git a/src/db/sysdb_subdomains.c b/src/db/sysdb_subdomains.c
index eea27aadfad892dd49a8c81a349f1304029d338c..5ee4df06e9de8a937d95913aa5b817a9f16e3d7d 100644
--- a/src/db/sysdb_subdomains.c
+++ b/src/db/sysdb_subdomains.c
@@ -108,16 +108,12 @@ done:
     return ret;
 }
 
-errno_t sysdb_master_domain_get_info(TALLOC_CTX *mem_ctx,
-                                     struct sysdb_ctx *sysdb,
-                                     struct sss_domain_info *domain,
-                                     struct sysdb_subdom **_info)
+errno_t sysdb_master_domain_update(struct sss_domain_info *domain)
 {
     errno_t ret;
     TALLOC_CTX *tmp_ctx;
     const char *tmp_str;
     struct ldb_dn *basedn;
-    struct sysdb_subdom *info;
     struct ldb_result *res;
     const char *attrs[] = {"cn",
                            SYSDB_SUBDOMAIN_REALM,
@@ -130,19 +126,14 @@ errno_t sysdb_master_domain_get_info(TALLOC_CTX *mem_ctx,
         return ENOMEM;
     }
 
-    info = talloc_zero(tmp_ctx, struct sysdb_subdom);
-    if (info == NULL) {
-        ret = ENOMEM;
-        goto done;
-    }
-
-    basedn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_DOM_BASE, domain->name);
+    basedn = ldb_dn_new_fmt(tmp_ctx, domain->sysdb->ldb,
+                            SYSDB_DOM_BASE, domain->name);
     if (basedn == NULL) {
         ret = EIO;
         goto done;
     }
-    ret = ldb_search(sysdb->ldb, tmp_ctx, &res, basedn, LDB_SCOPE_BASE, attrs,
-                     NULL);
+    ret = ldb_search(domain->sysdb->ldb, tmp_ctx, &res,
+                     basedn, LDB_SCOPE_BASE, attrs, NULL);
     if (ret != LDB_SUCCESS) {
         ret = EIO;
         goto done;
@@ -162,9 +153,11 @@ errno_t sysdb_master_domain_get_info(TALLOC_CTX *mem_ctx,
 
     tmp_str = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SUBDOMAIN_REALM,
                                           NULL);
-    if (tmp_str != NULL) {
-        info->realm = talloc_strdup(info, tmp_str);
-        if (info->realm == NULL) {
+    if (tmp_str != NULL &&
+        (domain->realm == NULL || strcasecmp(tmp_str, domain->realm) != 0)) {
+        talloc_free(domain->realm);
+        domain->realm = talloc_strdup(domain, tmp_str);
+        if (domain->realm == NULL) {
             ret = ENOMEM;
             goto done;
         }
@@ -172,9 +165,12 @@ errno_t sysdb_master_domain_get_info(TALLOC_CTX *mem_ctx,
 
     tmp_str = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SUBDOMAIN_FLAT,
                                           NULL);
-    if (tmp_str != NULL) {
-        info->flat_name = talloc_strdup(info, tmp_str);
-        if (info->flat_name == NULL) {
+    if (tmp_str != NULL &&
+        (domain->flat_name == NULL ||
+         strcasecmp(tmp_str, domain->flat_name) != 0)) {
+        talloc_free(domain->flat_name);
+        domain->flat_name = talloc_strdup(domain, tmp_str);
+        if (domain->flat_name == NULL) {
             ret = ENOMEM;
             goto done;
         }
@@ -182,15 +178,17 @@ errno_t sysdb_master_domain_get_info(TALLOC_CTX *mem_ctx,
 
     tmp_str = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SUBDOMAIN_ID,
                                           NULL);
-    if (tmp_str != NULL) {
-        info->id = talloc_strdup(info, tmp_str);
-        if (info->id == NULL) {
+    if (tmp_str != NULL &&
+        (domain->domain_id == NULL ||
+         strcasecmp(tmp_str, domain->domain_id) != 0)) {
+        talloc_free(domain->domain_id);
+        domain->domain_id = talloc_strdup(domain, tmp_str);
+        if (domain->domain_id == NULL) {
             ret = ENOMEM;
             goto done;
         }
     }
 
-    *_info = talloc_steal(mem_ctx, info);
 done:
     talloc_free(tmp_ctx);
     return ret;
@@ -204,14 +202,13 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
     struct ldb_message *msg;
     int ret;
     bool do_update = false;
-    struct sysdb_subdom *current_info;
 
     tmp_ctx = talloc_new(NULL);
     if (tmp_ctx == NULL) {
         return ENOMEM;
     }
 
-    ret = sysdb_master_domain_get_info(tmp_ctx, sysdb, domain, &current_info);
+    ret = sysdb_master_domain_update(domain);
     if (ret != EOK) {
         goto done;
     }
@@ -230,8 +227,8 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
     }
 
     if (domain_info->realm != NULL &&
-        (current_info->realm == NULL ||
-         strcmp(current_info->realm, domain_info->realm) != 0) ) {
+        (domain->realm == NULL ||
+         strcmp(domain->realm, domain_info->realm) != 0) ) {
         ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_REALM,
                                 LDB_FLAG_MOD_REPLACE, NULL);
         if (ret != LDB_SUCCESS) {
@@ -250,8 +247,8 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
     }
 
     if (domain_info->flat_name != NULL &&
-        (current_info->flat_name == NULL ||
-         strcmp(current_info->flat_name, domain_info->flat_name) != 0) ) {
+        (domain->flat_name == NULL ||
+         strcmp(domain->flat_name, domain_info->flat_name) != 0) ) {
         ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_FLAT,
                                 LDB_FLAG_MOD_REPLACE, NULL);
         if (ret != LDB_SUCCESS) {
@@ -270,8 +267,8 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
     }
 
     if (domain_info->id != NULL &&
-        (current_info->id == NULL ||
-         strcmp(current_info->id, domain_info->id) != 0) ) {
+        (domain->domain_id == NULL ||
+         strcmp(domain->domain_id, domain_info->id) != 0) ) {
         ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_ID, LDB_FLAG_MOD_REPLACE,
                                 NULL);
         if (ret != LDB_SUCCESS) {
diff --git a/src/providers/ipa/ipa_subdomains.c b/src/providers/ipa/ipa_subdomains.c
index 34a3f1be7f9936c2b983855d5abfe2c1aad2a959..6d7bfb329b62683cc4b149b5f9871878ca2ffd96 100644
--- a/src/providers/ipa/ipa_subdomains.c
+++ b/src/providers/ipa/ipa_subdomains.c
@@ -756,7 +756,6 @@ static void ipa_subdomains_handler_ranges_done(struct tevent_req *req)
     size_t reply_count;
     struct sysdb_attrs **reply = NULL;
     struct ipa_subdomains_req_ctx *ctx;
-    struct sysdb_subdom *domain_info;
     struct range_info **range_list = NULL;
     struct sysdb_ctx *sysdb;
     struct sss_domain_info *domain;
@@ -787,14 +786,14 @@ static void ipa_subdomains_handler_ranges_done(struct tevent_req *req)
     }
 
 
-    ret = sysdb_master_domain_get_info(ctx, sysdb, domain, &domain_info);
+    ret = sysdb_master_domain_update(domain);
     if (ret != EOK) {
         goto done;
     }
 
-    if (domain_info->flat_name == NULL ||
-        domain_info->id == NULL ||
-        domain_info->name == NULL) {
+    if (domain->flat_name == NULL ||
+        domain->domain_id == NULL ||
+        domain->realm == NULL) {
 
         ctx->search_base_iter = 0;
         ctx->search_bases = ctx->sd_ctx->master_search_bases;
diff --git a/src/responder/common/responder_get_domains.c b/src/responder/common/responder_get_domains.c
index 1ad9fbfaec2033fbe3e8782308d853e8fe6c2d2b..7049d60a88dc43dd853fda582719dfd7a96ce2a7 100644
--- a/src/responder/common/responder_get_domains.c
+++ b/src/responder/common/responder_get_domains.c
@@ -283,7 +283,6 @@ process_subdomains(struct sss_domain_info *domain)
     size_t c;
     size_t subdomain_count;
     struct sss_domain_info **subdomains;
-    struct sysdb_subdom *master_info;
 
     /* Retrieve all subdomains of this domain from sysdb
      * and create their struct sss_domain_info representations
@@ -308,31 +307,15 @@ process_subdomains(struct sss_domain_info *domain)
         subdomains[c]->next = subdomains[c + 1];
     }
 
-    if (domain->flat_name == NULL || domain->domain_id == NULL) {
-        ret = sysdb_master_domain_get_info(domain, domain->sysdb,
-                                           domain, &master_info);
+    if (domain->realm == NULL ||
+        domain->flat_name == NULL ||
+        domain->domain_id == NULL) {
+        ret = sysdb_master_domain_update(domain);
         if (ret != EOK) {
                 DEBUG(SSSDBG_FUNC_DATA, ("sysdb_master_domain_get_info " \
                                          "failed.\n"));
                 goto done;
         }
-
-        if (domain->flat_name == NULL) {
-            domain->flat_name = talloc_strdup(domain, master_info->flat_name);
-            if (domain->flat_name == NULL) {
-                DEBUG(SSSDBG_MINOR_FAILURE, ("talloc_strdup failed, ignoring"));
-            }
-        }
-
-        if (domain->domain_id == NULL) {
-            domain->domain_id = talloc_strdup(domain, master_info->id);
-            if (domain->domain_id == NULL) {
-                DEBUG(SSSDBG_MINOR_FAILURE, ("talloc_strdup failed, ignoring"));
-            }
-        }
-        talloc_free(master_info);
-        DEBUG(SSSDBG_TRACE_LIBS, ("Adding flat name [%s] to domain [%s].\n",
-                                  domain->flat_name, domain->name));
     }
 
     errno = 0;
-- 
1.8.0.1

