>From a6f8c243f7e408d8adbf2f7d92916250b882a8cc Mon Sep 17 00:00:00 2001
From: Simo Sorce <simo@redhat.com>
Date: Mon, 14 Jan 2013 13:43:52 -0500
Subject: [PATCH 04/12] Refactor sysdb_master_domain_add_info()

---
 src/db/sysdb.h                     |  6 +++---
 src/db/sysdb_subdomains.c          | 43 +++++++++++++++++---------------------
 src/providers/ipa/ipa_subdomains.c | 30 ++++++--------------------
 3 files changed, 28 insertions(+), 51 deletions(-)

diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 0e4e67e3af8f820a1b34a71d5f5eb021ce994b87..9134b777812df9025b0cc893531cb05fec6790a0 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -375,9 +375,9 @@ errno_t sysdb_update_subdomains(struct sss_domain_info *domain,
 
 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,
-                                     struct sysdb_subdom *domain_info);
+errno_t sysdb_master_domain_add_info(struct sss_domain_info *domain,
+                                     const char *realm, const char *flat,
+                                     const char *id);
 
 errno_t sysdb_get_ranges(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
                              size_t *range_count,
diff --git a/src/db/sysdb_subdomains.c b/src/db/sysdb_subdomains.c
index 5ee4df06e9de8a937d95913aa5b817a9f16e3d7d..2b1de12659c2c7f781a91e165b91d2141dd83466 100644
--- a/src/db/sysdb_subdomains.c
+++ b/src/db/sysdb_subdomains.c
@@ -194,9 +194,9 @@ done:
     return ret;
 }
 
-errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
-                                     struct sss_domain_info *domain,
-                                     struct sysdb_subdom *domain_info)
+errno_t sysdb_master_domain_add_info(struct sss_domain_info *domain,
+                                     const char *realm, const char *flat,
+                                     const char *id)
 {
     TALLOC_CTX *tmp_ctx;
     struct ldb_message *msg;
@@ -219,16 +219,15 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
         goto done;
     }
 
-    msg->dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
+    msg->dn = ldb_dn_new_fmt(tmp_ctx, domain->sysdb->ldb,
                              SYSDB_DOM_BASE, domain->name);
     if (msg->dn == NULL) {
         ret = EIO;
         goto done;
     }
 
-    if (domain_info->realm != NULL &&
-        (domain->realm == NULL ||
-         strcmp(domain->realm, domain_info->realm) != 0) ) {
+    if (realm != NULL && (domain->realm == NULL ||
+                          strcmp(domain->realm, realm) != 0)) {
         ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_REALM,
                                 LDB_FLAG_MOD_REPLACE, NULL);
         if (ret != LDB_SUCCESS) {
@@ -236,8 +235,7 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
             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;
@@ -246,9 +244,8 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
         do_update = true;
     }
 
-    if (domain_info->flat_name != NULL &&
-        (domain->flat_name == NULL ||
-         strcmp(domain->flat_name, domain_info->flat_name) != 0) ) {
+    if (flat != NULL && (domain->flat_name == NULL ||
+                         strcmp(domain->flat_name, flat) != 0)) {
         ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_FLAT,
                                 LDB_FLAG_MOD_REPLACE, NULL);
         if (ret != LDB_SUCCESS) {
@@ -256,8 +253,7 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
             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);
         if (ret != LDB_SUCCESS) {
             ret = sysdb_error_to_errno(ret);
             goto done;
@@ -266,17 +262,16 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
         do_update = true;
     }
 
-    if (domain_info->id != NULL &&
-        (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 (id != NULL && (domain->domain_id == NULL ||
+                       strcmp(domain->domain_id, id) != 0)) {
+        ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_ID,
+                                LDB_FLAG_MOD_REPLACE, 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, id);
         if (ret != LDB_SUCCESS) {
             ret = sysdb_error_to_errno(ret);
             goto done;
@@ -290,12 +285,11 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
         goto done;
     }
 
-    ret = ldb_modify(sysdb->ldb, msg);
+    ret = ldb_modify(domain->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,
-                                     ldb_errstring(sysdb->ldb)));
+                                     "[%s]: [%d][%s]!\n", domain->name, ret,
+                                     ldb_errstring(domain->sysdb->ldb)));
         ret = sysdb_error_to_errno(ret);
         goto done;
     }
@@ -307,6 +301,7 @@ done:
 
     return ret;
 }
+
 static errno_t sysdb_add_subdomain_attributes(struct sysdb_ctx *sysdb,
                                              struct sysdb_subdom *domain_info)
 {
diff --git a/src/providers/ipa/ipa_subdomains.c b/src/providers/ipa/ipa_subdomains.c
index 6d7bfb329b62683cc4b149b5f9871878ca2ffd96..e02d8212a6d840d0a0249088bd4112f351c95353 100644
--- a/src/providers/ipa/ipa_subdomains.c
+++ b/src/providers/ipa/ipa_subdomains.c
@@ -821,8 +821,6 @@ static void ipa_subdomains_handler_master_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;
-    const char *tmp_str;
 
     ctx = tevent_req_callback_data(req, struct ipa_subdomains_req_ctx);
 
@@ -834,36 +832,21 @@ static void ipa_subdomains_handler_master_done(struct tevent_req *req)
     }
 
     if (reply_count) {
-        domain_info = talloc_zero(ctx, struct sysdb_subdom);
-        if (domain_info == NULL) {
-            ret = ENOMEM;
-            goto done;
-        }
+        const char *flat = NULL;
+        const char *id = NULL;
 
-        ret = sysdb_attrs_get_string(reply[0], IPA_FLATNAME, &tmp_str);
+        ret = sysdb_attrs_get_string(reply[0], IPA_FLATNAME, &flat);
         if (ret != EOK) {
             goto done;
         }
-        domain_info->flat_name = talloc_strdup(domain_info, tmp_str);
-        if (domain_info->flat_name == NULL) {
-            ret = ENOMEM;
-            goto done;
-        }
 
-        ret = sysdb_attrs_get_string(reply[0], IPA_SID, &tmp_str);
+        ret = sysdb_attrs_get_string(reply[0], IPA_SID, &id);
         if (ret != EOK) {
             goto done;
         }
-        domain_info->id = talloc_strdup(domain_info, tmp_str);
-        if (domain_info->id == NULL) {
-            ret = ENOMEM;
-            goto done;
-        }
 
-        ret = sysdb_master_domain_add_info(ctx->sd_ctx->be_ctx->domain->sysdb,
-                                           ctx->sd_ctx->be_ctx->domain,
-                                           domain_info);
-        goto done;
+        ret = sysdb_master_domain_add_info(ctx->sd_ctx->be_ctx->domain,
+                                           NULL, flat, id);
     } else {
         ctx->search_base_iter++;
         ret = ipa_subdomains_handler_get(ctx, IPA_SUBDOMAINS_MASTER);
@@ -884,7 +867,6 @@ static void ipa_subdomains_handler_master_done(struct tevent_req *req)
         }
 
         ret = EIO;
-        goto done;
     }
 
 done:
-- 
1.8.0.1

