>From 98caad1ce16e4198aa93fc1d489cf3aacb42c185 Mon Sep 17 00:00:00 2001
From: Simo Sorce <simo@redhat.com>
Date: Sun, 13 Jan 2013 13:23:31 -0500
Subject: [PATCH 01/11] Add realm info to sss_domain_info

---
 src/confdb/confdb.h                          |  1 +
 src/providers/ad/ad_access.c                 |  2 +-
 src/providers/ipa/ipa_hbac_common.c          |  2 +-
 src/providers/ipa/ipa_subdomains_id.c        |  5 +++--
 src/providers/krb5/krb5_utils.c              |  3 ++-
 src/providers/ldap/sdap_access.c             |  2 +-
 src/responder/common/responder_get_domains.c |  1 +
 src/tests/sysdb-tests.c                      |  6 +++---
 src/util/domain_info_utils.c                 | 12 +++++++++++-
 src/util/util.h                              |  1 +
 10 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/src/confdb/confdb.h b/src/confdb/confdb.h
index 81323dda742c71028d2e53532f3092539362001a..45412538aa3c2bd2ea5946b185725e2807ec2632 100644
--- a/src/confdb/confdb.h
+++ b/src/confdb/confdb.h
@@ -232,6 +232,7 @@ struct sss_domain_info {
     struct sss_domain_info **subdomains;
     uint32_t subdomain_count;
     struct sss_domain_info *parent;
+    char *realm;
     char *flat_name;
     char *domain_id;
     struct timeval subdomains_last_checked;
diff --git a/src/providers/ad/ad_access.c b/src/providers/ad/ad_access.c
index 9508ffb273016bb575ee606f5c41fdf8c8e76fe2..0fa7d6d3afab4599a847bb0b319c31c09f52bda3 100644
--- a/src/providers/ad/ad_access.c
+++ b/src/providers/ad/ad_access.c
@@ -45,7 +45,7 @@ ad_access_handler(struct be_req *breq)
     /* Handle subdomains */
     if (strcasecmp(pd->domain, be_ctx->domain->name) != 0) {
         domain = new_subdomain(breq, be_ctx->domain,
-                               pd->domain, NULL, NULL);
+                               pd->domain, NULL, NULL, NULL);
         if (domain == NULL) {
             DEBUG(SSSDBG_OP_FAILURE, ("new_subdomain failed.\n"));
             be_req_terminate(breq, DP_ERR_FATAL, PAM_SYSTEM_ERR, NULL);
diff --git a/src/providers/ipa/ipa_hbac_common.c b/src/providers/ipa/ipa_hbac_common.c
index 90ec0ce4aee275e43736b31c2343357ea66f1c10..8a1c91ea958d41e551fd7cb15d9fa210ebd01366 100644
--- a/src/providers/ipa/ipa_hbac_common.c
+++ b/src/providers/ipa/ipa_hbac_common.c
@@ -448,7 +448,7 @@ hbac_ctx_to_eval_request(TALLOC_CTX *mem_ctx,
     /* Get user the user name and groups,
      * take care of subdomain users as well */
     if (strcasecmp(pd->domain, domain->name) != 0) {
-        user_dom = new_subdomain(tmp_ctx, domain, pd->domain, NULL, NULL);
+        user_dom = new_subdomain(tmp_ctx, domain, pd->domain, NULL, NULL, NULL);
         if (user_dom == NULL) {
             DEBUG(SSSDBG_OP_FAILURE, ("new_subdomain failed.\n"));
             ret = ENOMEM;
diff --git a/src/providers/ipa/ipa_subdomains_id.c b/src/providers/ipa/ipa_subdomains_id.c
index 481b998065eec29a52b9c39fba4017ff10098481..264eb03ce262fa297218f263ca150e022168be59 100644
--- a/src/providers/ipa/ipa_subdomains_id.c
+++ b/src/providers/ipa/ipa_subdomains_id.c
@@ -76,8 +76,9 @@ struct tevent_req *ipa_get_subdom_acct_send(TALLOC_CTX *memctx,
     }
 
     state->domain = new_subdomain(state, state->ctx->be->domain, ar->domain,
-                          get_flat_name_from_subdomain_name(ctx->be,ar->domain),
-                          NULL);
+                        NULL,
+                        get_flat_name_from_subdomain_name(ctx->be,ar->domain),
+                        NULL);
     if (state->domain == NULL) {
         DEBUG(SSSDBG_OP_FAILURE, ("new_subdomain failed.\n"));
         ret = ENOMEM;
diff --git a/src/providers/krb5/krb5_utils.c b/src/providers/krb5/krb5_utils.c
index e71f6c4dfd1f5aa3c63da78b74a9b1c94419a369..97718699800f87b5c0398fba2a5d9f4abfc3963d 100644
--- a/src/providers/krb5/krb5_utils.c
+++ b/src/providers/krb5/krb5_utils.c
@@ -1173,7 +1173,8 @@ errno_t get_domain_or_subdomain(TALLOC_CTX *mem_ctx, struct be_ctx *be_ctx,
 
     if (domain_name != NULL &&
         strcasecmp(domain_name, be_ctx->domain->name) != 0) {
-        *dom = new_subdomain(mem_ctx, be_ctx->domain, domain_name, NULL, NULL);
+        *dom = new_subdomain(mem_ctx, be_ctx->domain,
+                             domain_name, NULL, NULL, NULL);
         if (*dom == NULL) {
             DEBUG(SSSDBG_OP_FAILURE, ("new_subdomain failed.\n"));
             return ENOMEM;
diff --git a/src/providers/ldap/sdap_access.c b/src/providers/ldap/sdap_access.c
index ee20a84a7a18300bf4c2021e2c98dc18f68dcd37..aebf45240810b322bab636870adbbff376a0e0cb 100644
--- a/src/providers/ldap/sdap_access.c
+++ b/src/providers/ldap/sdap_access.c
@@ -128,7 +128,7 @@ sdap_access_send(TALLOC_CTX *mem_ctx,
     /* Get original user DN, take care of subdomain users as well */
     if (strcasecmp(pd->domain, be_ctx->domain->name) != 0) {
         user_dom = new_subdomain(state, be_ctx->domain, pd->domain,
-                                 NULL, NULL);
+                                 NULL, NULL, NULL);
         if (user_dom == NULL) {
             DEBUG(SSSDBG_OP_FAILURE, ("new_subdomain failed.\n"));
             ret = ENOMEM;
diff --git a/src/responder/common/responder_get_domains.c b/src/responder/common/responder_get_domains.c
index d40f373ea78a546e56364b506c82d13d3294e0f8..e52d61c28cb3d49dd704d90f124e5bf4ed4fab7e 100644
--- a/src/responder/common/responder_get_domains.c
+++ b/src/responder/common/responder_get_domains.c
@@ -313,6 +313,7 @@ process_subdomains(struct sss_domain_info *domain)
                                  subdomains[c]->name, domain->name));
         new_sd_list[c] =  new_subdomain(new_sd_list, domain,
                                         subdomains[c]->name,
+                                        subdomains[c]->realm,
                                         subdomains[c]->flat_name,
                                         subdomains[c]->id);
         if (new_sd_list[c] == NULL) {
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index 42331297ff9dd98c82961b0082f8cffe478c394f..6c723c72a8eb1b5155af31d6c65658767c7bec25 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -4552,7 +4552,7 @@ START_TEST(test_sysdb_subdomain_store_user)
                             ret, strerror(ret));
 
     subdomain = new_subdomain(test_ctx, test_ctx->domain, "test.sub",
-                              NULL, NULL);
+                              NULL, NULL, NULL);
     fail_unless(subdomain != NULL, "new_subdomain failed.");
 
     ret = sysdb_store_user(subdomain->sysdb, subdomain, "subdomuser",
@@ -4608,7 +4608,7 @@ START_TEST(test_sysdb_subdomain_user_ops)
                             ret, strerror(ret));
 
     subdomain = new_subdomain(test_ctx, test_ctx->domain, "test.sub",
-                              NULL, NULL);
+                              NULL, NULL, NULL);
     fail_unless(subdomain != NULL, "new_subdomain failed.");
 
     ret = sysdb_store_user(subdomain->sysdb, subdomain, "subdomuser",
@@ -4660,7 +4660,7 @@ START_TEST(test_sysdb_subdomain_group_ops)
                             ret, strerror(ret));
 
     subdomain = new_subdomain(test_ctx, test_ctx->domain, "test.sub",
-                              NULL, NULL);
+                              NULL, NULL, NULL);
     fail_unless(subdomain != NULL, "new_subdomain failed.");
 
     ret = sysdb_store_group(subdomain->sysdb, subdomain,
diff --git a/src/util/domain_info_utils.c b/src/util/domain_info_utils.c
index d0f709ece2be14893f6ae858c66f96a066221885..c45c83d64236f8af6c092f461ebc79fa2e454cb4 100644
--- a/src/util/domain_info_utils.c
+++ b/src/util/domain_info_utils.c
@@ -25,6 +25,7 @@
 struct sss_domain_info *new_subdomain(TALLOC_CTX *mem_ctx,
                                       struct sss_domain_info *parent,
                                       const char *name,
+                                      const char *realm,
                                       const char *flat_name,
                                       const char *id)
 {
@@ -55,6 +56,14 @@ struct sss_domain_info *new_subdomain(TALLOC_CTX *mem_ctx,
         goto fail;
     }
 
+    if (realm != NULL) {
+        dom->realm = talloc_strdup(dom, realm);
+        if (dom->realm == NULL) {
+            DEBUG(SSSDBG_OP_FAILURE, ("Failed to copy realm name.\n"));
+            goto fail;
+        }
+    }
+
     if (flat_name != NULL) {
         dom->flat_name = talloc_strdup(dom, flat_name);
         if (dom->flat_name == NULL) {
@@ -105,7 +114,8 @@ fail:
 struct sss_domain_info *copy_subdomain(TALLOC_CTX *mem_ctx,
                                        struct sss_domain_info *subdomain)
 {
-    return new_subdomain(mem_ctx, subdomain->parent, subdomain->name,
+    return new_subdomain(mem_ctx, subdomain->parent,
+                         subdomain->name, subdomain->realm,
                          subdomain->flat_name, subdomain->domain_id);
 }
 
diff --git a/src/util/util.h b/src/util/util.h
index df1ee3b08df71c18bd5201ce12ac6c0fbae1251d..a2b2a2d3e48d6ba916b4d1d5a62537f85af7e6ce 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -569,6 +569,7 @@ void to_sized_string(struct sized_string *out, const char *in);
 struct sss_domain_info *new_subdomain(TALLOC_CTX *mem_ctx,
                                       struct sss_domain_info *parent,
                                       const char *name,
+                                      const char *realm,
                                       const char *flat_name,
                                       const char *id);
 struct sss_domain_info *copy_subdomain(TALLOC_CTX *mem_ctx,
-- 
1.8.1.2

