>From 127eda4840e195f2aeac1f6781b62103004a63c3 Mon Sep 17 00:00:00 2001
From: Ondrej Kos <okos@redhat.com>
Date: Tue, 21 Aug 2012 16:03:32 +0200
Subject: [PATCH] Consolidation of functions that make realm upper-case

---
 src/providers/ad/ad_common.c       |  7 +------
 src/providers/ipa/ipa_common.c     | 10 ++--------
 src/providers/ipa/ipa_subdomains.c | 18 +-----------------
 src/util/usertools.c               | 21 +++++++++++++++++++++
 src/util/util.h                    |  3 +++
 5 files changed, 28 insertions(+), 31 deletions(-)

diff --git a/src/providers/ad/ad_common.c b/src/providers/ad/ad_common.c
index 90cfe412f068bed56459436c89767a595c05c4db..7568798ab6cdaac387e42f8baafed5c7197e009c 100644
--- a/src/providers/ad/ad_common.c
+++ b/src/providers/ad/ad_common.c
@@ -33,7 +33,6 @@ ad_get_common_options(TALLOC_CTX *mem_ctx,
 {
     errno_t ret;
     int gret;
-    size_t i;
     struct ad_options *opts = NULL;
     char *domain;
     char *server;
@@ -98,16 +97,12 @@ ad_get_common_options(TALLOC_CTX *mem_ctx,
 
 
     /* Always use the upper-case AD domain for the kerberos realm */
-    realm = talloc_strdup(opts, domain);
+    realm = get_uppercase_realm(opts, domain);
     if (!realm) {
         ret = ENOMEM;
         goto done;
     }
 
-    for (i = 0; realm[i]; i++) {
-        realm[i] = toupper(realm[i]);
-    }
-
     ret = dp_opt_set_string(opts->basic, AD_KRB5_REALM, realm);
     if (ret != EOK) {
         goto done;
diff --git a/src/providers/ipa/ipa_common.c b/src/providers/ipa/ipa_common.c
index 6ad67845d2eb44ef0e6b8fbbd873e26f41dc2830..2e50473812e2e976604655c6b46fe507b05a1440 100644
--- a/src/providers/ipa/ipa_common.c
+++ b/src/providers/ipa/ipa_common.c
@@ -47,7 +47,6 @@ int ipa_get_options(TALLOC_CTX *memctx,
     char *realm;
     char *ipa_hostname;
     int ret;
-    int i;
     char hostname[HOST_NAME_MAX + 1];
 
     opts = talloc_zero(memctx, struct ipa_options);
@@ -95,18 +94,13 @@ int ipa_get_options(TALLOC_CTX *memctx,
     /* First check whether the realm has been manually specified */
     realm = dp_opt_get_string(opts->basic, IPA_KRB5_REALM);
     if (!realm) {
-        /* No explicit krb5_realm, use the IPA domain */
-        realm = talloc_strdup(opts, domain);
+        /* No explicit krb5_realm, use the IPA domain, transform to upper-case */
+        realm = get_uppercase_realm(opts, domain);
         if (!realm) {
             ret = ENOMEM;
             goto done;
         }
 
-        /* Use the upper-case IPA domain for the kerberos realm */
-        for (i = 0; realm[i]; i++) {
-            realm[i] = toupper(realm[i]);
-        }
-
         ret = dp_opt_set_string(opts->basic, IPA_KRB5_REALM,
                                 realm);
         if (ret != EOK) {
diff --git a/src/providers/ipa/ipa_subdomains.c b/src/providers/ipa/ipa_subdomains.c
index 98c7de3465f4f67e397d176f7b49906c830442c2..1da2b8cd40224d3b92a098bebfe8240181332ced 100644
--- a/src/providers/ipa/ipa_subdomains.c
+++ b/src/providers/ipa/ipa_subdomains.c
@@ -178,22 +178,6 @@ 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_subdom_parse(TALLOC_CTX *memctx,
                                 struct sysdb_attrs *attrs,
                                 struct sysdb_subdom *subdom)
@@ -219,7 +203,7 @@ static errno_t ipa_subdom_parse(TALLOC_CTX *memctx,
     if (subdom->realm == NULL) {
         /* Add Realm as upper(domain name), this is generally always correct
          * with AD domains */
-        subdom->realm = name_to_realm(memctx, subdom->name);
+        subdom->realm = get_uppercase_realm(memctx, subdom->name);
         if (!subdom->realm) {
             return ENOMEM;
         }
diff --git a/src/util/usertools.c b/src/util/usertools.c
index 36641d49f9f10e355a44b00814967175774b791b..adef2b0021170e9bb21e1c0b2e9a57f460141572 100644
--- a/src/util/usertools.c
+++ b/src/util/usertools.c
@@ -45,6 +45,27 @@ char *get_username_from_uid(TALLOC_CTX *mem_ctx, uid_t uid)
     return username;
 }
 
+/* Function returns given realm name as new uppercase string */
+char *get_uppercase_realm(TALLOC_CTX *memctx, const char *name)
+{
+    char *realm;
+    char *c;
+
+    realm = talloc_strdup(memctx, name);
+    if (!realm) {
+        return NULL;
+    }
+
+    c = realm;
+    while(*c != '\0') {
+        *c = toupper(*c);
+        c++;
+    }
+
+    return realm;
+}
+
+
 static int sss_names_ctx_destructor(struct sss_names_ctx *snctx)
 {
     if (snctx->re) {
diff --git a/src/util/util.h b/src/util/util.h
index b51aebbd826ef840014940e762ebe2d0f81729f1..7ff91af56baaab4c4bff10f225d8616f35cfde9a 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -28,6 +28,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <strings.h>
+#include <ctype.h>
 #include <errno.h>
 #include <libintl.h>
 #include <limits.h>
@@ -388,6 +389,8 @@ int password_destructor(void *memctx);
 /* from usertools.c */
 char *get_username_from_uid(TALLOC_CTX *mem_ctx, uid_t uid);
 
+char *get_uppercase_realm(TALLOC_CTX *memctx, const char *name);
+
 struct sss_names_ctx {
     char *re_pattern;
     char *fq_fmt;
-- 
1.7.11.2

