>From e9f891ad0e09807fe3602646b977e6e38aa8588c Mon Sep 17 00:00:00 2001
From: Simo Sorce <simo@redhat.com>
Date: Thu, 19 Jul 2012 19:15:57 -0400
Subject: [PATCH 11/16] Add realm paramter to subdomain list

This will be used later for setting domain_realm mappings in krb5.conf
---
 src/db/sysdb.h                     |    8 +++--
 src/db/sysdb_subdomains.c          |   58 ++++++++++++++++++++++++++++++++++++
 src/providers/ipa/ipa_subdomains.c |   27 +++++++++++++++++
 src/tests/sysdb-tests.c            |    6 ++--
 4 files changed, 93 insertions(+), 6 deletions(-)

diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 20641a753ef7d140f075e077f9afc0498c15f8de..0e2404c70c58970d7533c5604c65babb40630c70 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -142,6 +142,7 @@
 
 #define SYSDB_SSH_PUBKEY "sshPublicKey"
 
+#define SYSDB_SUBDOMAIN_REALM "realmName"
 #define SYSDB_SUBDOMAIN_FLAT "flatName"
 #define SYSDB_SUBDOMAIN_ID "domainID"
 
@@ -241,9 +242,10 @@ struct sysdb_attrs {
 struct sysdb_attrs *sysdb_new_attrs(TALLOC_CTX *mem_ctx);
 
 struct sysdb_subdom {
-    char *name;
-    char *flat_name;
-    char *id;
+    const char *realm;
+    const char *name;
+    const char *flat_name;
+    const char *id;
 };
 
 struct range_info {
diff --git a/src/db/sysdb_subdomains.c b/src/db/sysdb_subdomains.c
index 533874f66bfb202887bbff7ed54b87cae77f5f35..8f1df88be0d6c4f97f7bb7a6ba367822980b58d3 100644
--- a/src/db/sysdb_subdomains.c
+++ b/src/db/sysdb_subdomains.c
@@ -32,6 +32,7 @@ errno_t sysdb_get_subdomains(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
     TALLOC_CTX *tmp_ctx;
     struct ldb_result *res;
     const char *attrs[] = {"cn",
+                           SYSDB_SUBDOMAIN_REALM,
                            SYSDB_SUBDOMAIN_FLAT,
                            SYSDB_SUBDOMAIN_ID,
                            NULL};
@@ -86,6 +87,16 @@ errno_t sysdb_get_subdomains(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
         }
 
         tmp_str = ldb_msg_find_attr_as_string(res->msgs[i],
+                                              SYSDB_SUBDOMAIN_REALM, NULL);
+        if (tmp_str != NULL) {
+            list[i]->realm = talloc_strdup(list, tmp_str);
+            if (list[i]->realm == NULL) {
+                ret = ENOMEM;
+                goto done;
+            }
+        }
+
+        tmp_str = ldb_msg_find_attr_as_string(res->msgs[i],
                                               SYSDB_SUBDOMAIN_FLAT, NULL);
         if (tmp_str != NULL) {
             list[i]->flat_name = talloc_strdup(list, tmp_str);
@@ -126,6 +137,7 @@ errno_t sysdb_master_domain_get_info(TALLOC_CTX *mem_ctx,
     struct sysdb_subdom *info;
     struct ldb_result *res;
     const char *attrs[] = {"cn",
+                           SYSDB_SUBDOMAIN_REALM,
                            SYSDB_SUBDOMAIN_FLAT,
                            SYSDB_SUBDOMAIN_ID,
                            NULL};
@@ -166,6 +178,16 @@ errno_t sysdb_master_domain_get_info(TALLOC_CTX *mem_ctx,
         goto done;
     }
 
+    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) {
+            ret = ENOMEM;
+            goto done;
+        }
+    }
+
     tmp_str = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_SUBDOMAIN_FLAT,
                                           NULL);
     if (tmp_str != NULL) {
@@ -224,6 +246,26 @@ errno_t sysdb_master_domain_add_info(struct sysdb_ctx *sysdb,
         goto done;
     }
 
+    if (domain_info->realm != NULL &&
+        (current_info->realm == NULL ||
+         strcmp(current_info->realm, domain_info->realm) != 0) ) {
+        ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_REALM,
+                                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_REALM,
+                                 domain_info->realm);
+        if (ret != LDB_SUCCESS) {
+            ret = sysdb_error_to_errno(ret);
+            goto done;
+        }
+
+        do_update = true;
+    }
+
     if (domain_info->flat_name != NULL &&
         (current_info->flat_name == NULL ||
          strcmp(current_info->flat_name, domain_info->flat_name) != 0) ) {
@@ -322,6 +364,22 @@ static errno_t sysdb_add_subdomain_attributes(struct sysdb_ctx *sysdb,
         goto done;
     }
 
+    if (domain_info->realm != NULL) {
+        ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_REALM, LDB_FLAG_MOD_ADD,
+                                NULL);
+        if (ret != LDB_SUCCESS) {
+            ret = sysdb_error_to_errno(ret);
+            goto done;
+        }
+
+        ret = ldb_msg_add_string(msg, SYSDB_SUBDOMAIN_REALM,
+                                 domain_info->realm);
+        if (ret != LDB_SUCCESS) {
+            ret = sysdb_error_to_errno(ret);
+            goto done;
+        }
+    }
+
     if (domain_info->flat_name != NULL) {
         ret = ldb_msg_add_empty(msg, SYSDB_SUBDOMAIN_FLAT, LDB_FLAG_MOD_ADD,
                                 NULL);
diff --git a/src/providers/ipa/ipa_subdomains.c b/src/providers/ipa/ipa_subdomains.c
index e842e24bff1546d10e11b9c1dffe5d2dd2deda05..8a49c63439d96b6ec79d0a13e944903bc83932fd 100644
--- a/src/providers/ipa/ipa_subdomains.c
+++ b/src/providers/ipa/ipa_subdomains.c
@@ -25,6 +25,7 @@
 #include "providers/ldap/sdap_async.h"
 #include "providers/ipa/ipa_subdomains.h"
 #include "providers/ipa/ipa_common.h"
+#include <ctype.h>
 
 #define SUBDOMAINS_FILTER "objectclass=ipaNTTrustedDomain"
 #define MASTER_DOMAIN_FILTER "objectclass=ipaNTDomainAttrs"
@@ -151,6 +152,22 @@ done:
     return ret;
 }
 
+static char *name_to_realm(TALLOC_CTX *memctx, const char *name)
+{
+    char *realm;
+    char *p;
+
+    realm = talloc_strdup(memctx, name);
+    if (!realm) {
+        return NULL;
+    }
+    for (p = realm; *p; p++) {
+        *p = toupper(*p);
+    }
+
+    return realm;
+}
+
 static errno_t ipa_subdomains_parse_results(struct be_subdom_req *sd_data,
                                             size_t count,
                                             struct sysdb_attrs **reply)
@@ -187,6 +204,16 @@ static errno_t ipa_subdomains_parse_results(struct be_subdom_req *sd_data,
             goto done;
         }
 
+        /* Add Realm as upper(domain name), this is generally always correct
+         * with AD domains */
+        new_domain_list[c]->realm = name_to_realm(new_domain_list[c],
+                                                  new_domain_list[c]->name);
+        if (!new_domain_list[c]->realm) {
+            DEBUG(SSSDBG_OP_FAILURE, ("talloc_strdup failed.\n"));
+            ret = ENOMEM;
+            goto done;
+        }
+
         ret = sysdb_attrs_get_string(reply[c], IPA_FLATNAME, &value);
         if (ret == EOK) {
             new_domain_list[c]->flat_name = talloc_strdup(new_domain_list[c],
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index bf7f157ed58341b549cc5a080d081fc452a0887c..1eb9503f0a1df8af2ff18ab4020ed4d9a7810e21 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -3388,9 +3388,9 @@ START_TEST(test_sysdb_original_dn_case_insensitive)
 }
 END_TEST
 
-struct sysdb_subdom dom1 =  { "dom1.sub", "dom1", "S-1" };
-struct sysdb_subdom dom2 =  { "dom2.sub", "dom2", "S-2" };
-struct sysdb_subdom dom_t = { "test.sub", "test", "S-3" };
+struct sysdb_subdom dom1 =  { "DOM1.SUB", "dom1.sub", "dom1", "S-1" };
+struct sysdb_subdom dom2 =  { "DOM2.SUB", "dom2.sub", "dom2", "S-2" };
+struct sysdb_subdom dom_t = { "TEST.SUB", "test.sub", "test", "S-3" };
 
 START_TEST(test_sysdb_subdomain_create)
 {
-- 
1.7.10.4

