>From 8d6bf622a29f1786019429c92586aefa60a8505b Mon Sep 17 00:00:00 2001
From: Michal Zidek <mzidek@redhat.com>
Date: Wed, 22 Aug 2012 15:16:26 +0200
Subject: [PATCH] Fix: IPv6 address with square brackets doesn't work.

https://fedorahosted.org/sssd/ticket/1365
---
 src/providers/ad/ad_common.c     | 13 +++++++++++++
 src/providers/ipa/ipa_common.c   | 13 +++++++++++++
 src/providers/krb5/krb5_common.c | 13 +++++++++++++
 src/providers/ldap/ldap_common.c | 13 +++++++++++++
 4 files changed, 52 insertions(+)

diff --git a/src/providers/ad/ad_common.c b/src/providers/ad/ad_common.c
index 90cfe41..90198b7 100644
--- a/src/providers/ad/ad_common.c
+++ b/src/providers/ad/ad_common.c
@@ -156,6 +156,7 @@ ad_servers_init(TALLOC_CTX *mem_ctx,
     char **list;
     char *ad_domain;
     TALLOC_CTX *tmp_ctx;
+    int len;
 
     tmp_ctx = talloc_new(NULL);
     if (!tmp_ctx) return ENOMEM;
@@ -194,6 +195,18 @@ ad_servers_init(TALLOC_CTX *mem_ctx,
             continue;
         }
 
+        /* It could be ipv6 address in square brackets. Removing
+         * the brackets. */
+        if (list[i] && list[i][0] == '[') {
+            len = strlen(list[i]);
+            if (len < 3) {
+                ret = EINVAL;
+                goto done;
+            }
+            memmove(list[i], &list[i][1], len - 2);
+            list[i][len - 2] = '\0';
+        }
+
         ret = be_fo_add_server(bectx, AD_SERVICE_NAME, list[i], 0, NULL, primary);
         if (ret && ret != EEXIST) {
             DEBUG(SSSDBG_FATAL_FAILURE, ("Failed to add server\n"));
diff --git a/src/providers/ipa/ipa_common.c b/src/providers/ipa/ipa_common.c
index 6ad6784..ebeef8b 100644
--- a/src/providers/ipa/ipa_common.c
+++ b/src/providers/ipa/ipa_common.c
@@ -808,6 +808,7 @@ errno_t ipa_servers_init(struct be_ctx *ctx,
     char *ipa_domain;
     int ret = 0;
     int i;
+    int len;
 
     tmp_ctx = talloc_new(NULL);
     if (!tmp_ctx) {
@@ -847,6 +848,18 @@ errno_t ipa_servers_init(struct be_ctx *ctx,
             continue;
         }
 
+        /* It could be ipv6 address in square brackets. Removing
+         * the brackets. */
+        if (list[i] && list[i][0] == '[') {
+            len = strlen(list[i]);
+            if (len < 3) {
+                ret = EINVAL;
+                goto done;
+            }
+            memmove(list[i], &list[i][1], len - 2);
+            list[i][len - 2] = '\0';
+        }
+
         ret = be_fo_add_server(ctx, "IPA", list[i], 0, NULL, primary);
         if (ret && ret != EEXIST) {
             DEBUG(SSSDBG_FATAL_FAILURE, ("Failed to add server\n"));
diff --git a/src/providers/krb5/krb5_common.c b/src/providers/krb5/krb5_common.c
index bd7a302..92145f5 100644
--- a/src/providers/krb5/krb5_common.c
+++ b/src/providers/krb5/krb5_common.c
@@ -475,6 +475,7 @@ errno_t krb5_servers_init(struct be_ctx *ctx,
     char **list = NULL;
     errno_t ret = 0;
     int i;
+    int len;
     char *port_str;
     long port;
     char *server_spec;
@@ -564,6 +565,18 @@ errno_t krb5_servers_init(struct be_ctx *ctx,
             }
         }
 
+        /* It could be ipv6 address in square brackets. Removing
+         * the brackets. */
+        if (server_spec && server_spec[0] == '[') {
+            len = strlen(server_spec);
+            if (len < 3) {
+                ret = EINVAL;
+                goto done;
+            }
+            memmove(server_spec, &server_spec[1], len - 2);
+            server_spec[len - 2] = '\0';
+        }
+
         ret = be_fo_add_server(ctx, service_name, server_spec, (int) port,
                                list[i], primary);
         if (ret && ret != EEXIST) {
diff --git a/src/providers/ldap/ldap_common.c b/src/providers/ldap/ldap_common.c
index ce75875..d2f29e5 100644
--- a/src/providers/ldap/ldap_common.c
+++ b/src/providers/ldap/ldap_common.c
@@ -1118,6 +1118,7 @@ errno_t sdap_urls_init(struct be_ctx *ctx,
     LDAPURLDesc *lud;
     errno_t ret = 0;
     int i;
+    int len;
 
     tmp_ctx = talloc_new(NULL);
     if (!tmp_ctx) {
@@ -1185,6 +1186,18 @@ errno_t sdap_urls_init(struct be_ctx *ctx,
 
         talloc_steal(service, list[i]);
 
+        /* It could be ipv6 address in square brackets. Removing
+         * the brackets. */
+        if (lud->lud_host && lud->lud_host[0] == '[') {
+            len = strlen(lud->lud_host);
+            if (len < 3) {
+                ret = EINVAL;
+                goto done;
+            }
+            memmove(lud->lud_host, &lud->lud_host[1], len - 2);
+            lud->lud_host[len - 2] = '\0';
+        }
+
         ret = be_fo_add_server(ctx, service->name, lud->lud_host,
                                lud->lud_port, list[i], primary);
         ldap_free_urldesc(lud);
-- 
1.7.11.2

